Skip to content

Commit

Permalink
Fully migrated to core.logic 0.6.5, fixed up some files
Browse files Browse the repository at this point in the history
  • Loading branch information
frenchy64 committed Nov 2, 2011
1 parent 933adcd commit 42ea920
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 51 deletions.
89 changes: 46 additions & 43 deletions src/logic_introduction/decl_model.clj
@@ -1,10 +1,53 @@
(ns logic-introduction.decl-model
(:refer-clojure :exclude [==])
(:refer-clojure :exclude [== compile])
(:import [java.io Writer])
(:use ;[clojure.core.logic [minikanren :exclude [LCons walk lfirst lrest lcons?]] prelude nonrel match disequality]
[clojure.walk :only [walk prewalk postwalk]]
(:use [clojure.walk :only [walk prewalk postwalk]]
[clojure.core.match [core :exclude [swap]]]))

;; See bottom of file for some thoughts


(defprotocol LConsP
(lfirst [this])
(lrest [this]))

(defprotocol LConsPrint
(toShortString [this]))

(deftype LCons [a d]
LConsPrint
(toShortString [this]
(cond
(.. this getClass (isInstance d)) (str a " " (toShortString d))
:else (str a " . " d )))
Object
(toString [this] (cond
(.. this getClass (isInstance d)) (str "(" a " " (toShortString d) ")")
:else (str "(" a " . " d ")")))
LConsP
(lfirst [_] a)
(lrest [_] d))

(defn lcons [a d]
"Constructs a sequence a with an improper tail d if d is a logic variable."
(if (or (coll? d) (nil? d))
(cons a (seq d))
(LCons. a d )))

(defn lcons? [x]
(instance? LCons x))

(defmethod print-method LCons [x ^Writer writer]
(.write writer (str x)))

(defn- rest-nil [n]
(let [r (rest n)]
(if (empty? r)
nil
r)))



(defn- composite?
"Taken from the old `contrib.core/seqable?`. Since the meaning of 'seqable' is
questionable, I will work on phasing it out and using a more meaningful
Expand Down Expand Up @@ -126,13 +169,6 @@



(declare lcons? lfirst lrest)
(defn- rest-nil [n]
(let [r (rest n)]
(if (empty? r)
nil
r)))


(defmethod set-or-equals
[Object Object]
Expand Down Expand Up @@ -202,39 +238,6 @@
(reify-solved ~n)
:logic-introduction.decl-model/NORESULT)))

(defprotocol LConsP
(lfirst [this])
(lrest [this]))

(defprotocol LConsPrint
(toShortString [this]))

(deftype LCons [a d]
LConsPrint
(toShortString [this]
(cond
(.. this getClass (isInstance d)) (str a " " (toShortString d))
:else (str a " . " d )))
Object
(toString [this] (cond
(.. this getClass (isInstance d)) (str "(" a " " (toShortString d) ")")
:else (str "(" a " . " d ")")))
LConsP
(lfirst [_] a)
(lrest [_] d))

(defn lcons [a d]
"Constructs a sequence a with an improper tail d if d is a logic variable."
(if (or (coll? d) (nil? d))
(cons a (seq d))
(LCons. a d )))

(defn lcons? [x]
(instance? LCons x))

(defmethod print-method LCons [x ^Writer writer]
(.write writer (str x)))


;; Handy predicates

Expand Down
4 changes: 2 additions & 2 deletions src/logic_introduction/facts.clj
Expand Up @@ -29,7 +29,7 @@
(tabled [ancestor descendant]
(conde
((derives ancestor descendant))
((exist [i]
((fresh [i]
(derived-ancestor ancestor i)
(derives i descendant))))))

Expand Down Expand Up @@ -62,7 +62,7 @@
;; Quicksort: Pg 70, Art of Prolog
(matche [ts out]
([[?x . ?xs] ?ys]
(exist [littles bigs ls bs]
(fresh [littles bigs ls bs]
(partition-by-preference ?xs ?x littles bigs)
(sort-by-preference littles ls)
(sort-by-preference bigs bs)
Expand Down
4 changes: 2 additions & 2 deletions src/logic_introduction/fetch.clj
Expand Up @@ -24,7 +24,7 @@

(defn fetch-data [fetched-data]
"Output: fetched-data"
(exist [url datastream]
(fresh [url datastream]
(url-to-process url)
(slurpo url datastream)
(read-jsono datastream fetched-data)))
Expand All @@ -42,7 +42,7 @@

(defn process-json-object [json-object]
"Input: json-object"
(exist [date-value indicator-value temp]
(fresh [date-value indicator-value temp]
(json-object-has-value json-object :date date-value)
(json-object-has-value json-object :value indicator-value)
(project [date-value indicator-value]
Expand Down
4 changes: 2 additions & 2 deletions src/logic_introduction/polymorphism.clj
Expand Up @@ -32,12 +32,12 @@
"context is an environment such that expression exp executed in
environment context results in type result-type"
(conde
((exist [poly-res-type]
((fresh [poly-res-type]
(polymorphic-type result-type poly-res-type)
(env-assoc exp context poly-res-type)))
((matche [exp]
([[:apply ?fun ?arg]]
(exist [arg-type fun-type]
(fresh [arg-type fun-type]
(!= ?fun ?arg)
(expression-check context ?arg arg-type)
(expression-check context ?fun [arg-type :> result-type])))))))
5 changes: 3 additions & 2 deletions src/logic_introduction/subs.clj
@@ -1,6 +1,7 @@
(ns logic-introduction.subs
(:use [clojure.core.match.core :only [match]])
(:refer-clojure :exclude [==]))
(:refer-clojure :exclude [== reify]))

;; Skeleton of a minikanren-like language

(def empty-substitution '())

Expand Down

0 comments on commit 42ea920

Please sign in to comment.