Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LOGIC-91: Assorted fixes for constraints / nominal logic interactions. #15

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/main/clojure/clojure/core/logic.clj
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@
(let [vars (var-rands a c)
c (with-id c cid)
cs (reduce (fn [cs v] (add-var cs v c)) this vars)]
(ConstraintStore. (:km cs) (:cm cs) (inc cid) running)))
[(ConstraintStore. (:km cs) (:cm cs) (inc cid) running) c]))

(updatec [this a c]
(let [oc (cm (id c))
Expand Down Expand Up @@ -2957,8 +2957,9 @@
(fn [a]
(let [a (reduce (fn [a x]
(ext-no-check a x (subst-val ::unbound)))
a (unbound-rands a c))]
(assoc a :cs (addc (:cs a) a c)))))
a (unbound-rands a c))
[cs c] (addc (:cs a) a c)]
[(assoc a :cs cs) c])))

(defn updatecg [c]
(fn [a]
Expand Down Expand Up @@ -3100,12 +3101,12 @@
(reify
clojure.lang.IFn
(invoke [_ a]
(if (runnable? c a)
(when-let [a (c a)]
(if (relevant? c a)
((addcg c) a)
(if (relevant? c a)
(let [[a c] ((addcg c) a)]
(if (runnable? c a)
(c a)
a))
((addcg c) a)))
a))
IUnwrapConstraint
(unwrap [_] c)))

Expand Down
6 changes: 6 additions & 0 deletions src/main/clojure/clojure/core/logic/nominal.clj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
(let [t (walk* s t)]
(if (lvar? t)
(let [v (with-meta (lvar) (meta t))
rt (root-val s t)
s (if (subst-val? rt) (ext-no-check s v rt) s)
s (update-dom s (root-var s v) ::nom (fnil (fn [d] (conj d t)) []))
s (update-dom s (root-var s t) ::nom (fnil (fn [d] (conj d v)) []))
s (bind s (suspc v t swap))]
Expand Down Expand Up @@ -303,6 +305,10 @@
(-constrain-tree [t fc s]
(fc (:body t) s))

clojure.core.logic.IForceAnswerTerm
(-force-ans [v x]
(force-ans (:body v)))

INomSwap
(swap-noms [t swap s]
(let [[tbody s] (swap-noms (:body t) swap s)]
Expand Down
42 changes: 41 additions & 1 deletion src/test/clojure/clojure/core/logic/nominal/tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,44 @@
(predc x number? `number?)
(== y 'foo)
(== [x y] q))))
'())))
'()))
(is (= (run* [q]
(nom/fresh [a]
(fresh [x]
(predc x number? `number?)
(== x 1)
(== (nom/tie a [a x]) q))))
[(nom/tie 'a_0 '(a_0 1))]))
(is (= (run* [q]
(nom/fresh [a]
(fresh [x]
(== x 1)
(predc x number? `number?)
(== (nom/tie a [a x]) q))))
[(nom/tie 'a_0 '(a_0 1))]))
(is (= (run* [q]
(nom/fresh [a b c]
(fresh [x]
(== x 1)
(predc x number? `number?)
(== (nom/tie b (nom/tie a [a x])) (nom/tie c q)))))
[(nom/tie 'a_0 '(a_0 1))]))
(is (= (run* [q]
(nom/fresh [a b c]
(fresh [x]
(== x 1)
(treec x #(predc % number? `number?) `number?)
(== (nom/tie b (nom/tie a [a x])) (nom/tie c q)))))
[(nom/tie 'a_0 '(a_0 1))]))
(is (= (run* [q]
(fresh [x]
(nom/fresh [a]
(infd x (interval 1 3))
(== q (nom/tie a x)))))
[(nom/tie 'a_0 1) (nom/tie 'a_0 2) (nom/tie 'a_0 3)]))
(is (= (run* [q]
(nom/fresh [a b c]
(fresh [x]
(infd x (interval 1 3))
(== (nom/tie b (nom/tie a x)) (nom/tie c q)))))
[(nom/tie 'a_0 1) (nom/tie 'a_0 2) (nom/tie 'a_0 3)])))
18 changes: 9 additions & 9 deletions src/test/clojure/clojure/core/logic/tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1795,7 +1795,7 @@
v 1
w (lvar 'w)
c (fdc (+fdc u v w))
cs (addc (make-cs) empty-s c)
[cs c] (addc (make-cs) empty-s c)
sc (first (constraints-for cs empty-s u ::l/fd))]
(is (= c sc))
(is (= (id sc) 0))
Expand All @@ -1809,9 +1809,9 @@
c0 (fdc (+fdc u v w))
x (lvar 'x)
c1 (fdc (+fdc w v x))
cs (-> (make-cs)
(addc empty-s c0)
(addc empty-s c1))
cs (make-cs)
[cs c0] (addc cs empty-s c0)
[cs c1] (addc cs empty-s c1)
sc0 (get (:cm cs) 0)
sc1 (get (:cm cs) 1)]
(is (= sc0 c0)) (is (= (id sc0) 0))
Expand All @@ -1825,7 +1825,7 @@
v 1
w (lvar 'w)
c (fdc (+fdc u v w))
s ((addcg c) empty-s)]
[s c] ((addcg c) empty-s)]
(is (= (count (:km (:cs s))) 2))
(is (= (count (:cm (:cs s))) 1))))

Expand All @@ -1834,7 +1834,7 @@
v 1
w (lvar 'w)
c (fdc (+fdc u v w))
s ((addcg c) empty-s)
[s c] ((addcg c) empty-s)
c (first (constraints-for (:cs s) s u ::l/fd))
s (-> s
(ext-no-check u 1)
Expand Down Expand Up @@ -2249,7 +2249,7 @@
y (lvar 'y)
z (lvar 'z)
c (fdc (+fdc x y z))
cs (addc (make-cs) empty-s c)
[cs c] (addc (make-cs) empty-s c)
cp (get (:cm cs) 0)
cs (remc cs empty-s cp)]
(is (= (:km cs) {}))
Expand All @@ -2265,7 +2265,7 @@
(let [x (lvar 'x)
y (lvar 'y)
c (!=c (list (pair x 1) (pair y 2)))
cs (addc (make-cs) empty-s c)]
[cs c] (addc (make-cs) empty-s c)]
(is (tree-constraint? ((:cm cs) 0)))
(is (= (into #{} (keys (:km cs)))
#{x y}))))
Expand Down Expand Up @@ -2333,7 +2333,7 @@
y (lvar 'y)
c (!=c (list (pair x 1)))
sc (!=c (list (pair x 1) (pair y 2)))
cs (addc (make-cs) empty-s c)]
[cs c] (addc (make-cs) empty-s c)]
))

(deftest test-multi-constraints-1 []
Expand Down