This is a contradiction:
(run* [q]
(== q true)
(== q false))
-> ()
Because there can't be a q that is true and false at the same time.
Shouldn't this be a contradiction as well?
(run* [q]
(featurec q {:k true})
(featurec q {:k false}))
-> ((_0 :- (clojure.core.logic/featurec _0 {:k true})
(clojure.core.logic/featurec _0 {:k false})))
I don't know what that :- mean. But since map keys are unique, I suppose there cannot be a map that can have :k to be true as well as false. How can I make sure the second goal doesn't match?
Another Example
(run* [q]
(fresh [x]
(featurec x {:k false})
(== q true)
(featurec x {:k q}))) -> (true)
I am interpreting this as:
x is a map-like with :k as false.
q is true.
q is the value of :k of x
What am I trying to do exactly?
I am trying to use featurec to create goals like:
(defn is-a-foo [x v]
(featurec x {:foo v}))
(defn is-a-bar [x v]
(featurec x {:bar v}))
Then I'd like to be able to say:
(defn foo-implies-bar [x]
(conde [(is-a-foo [x true]) (is-a-bar [x true])])
Related
In core.logic, is there a way to translate the outputs of a program like
(run 2 [q]
(fresh [a]
(featurec q {:foo a})
(membero a '(5))
(conde
((featurec q {:bar 2}))
((featurec q {:baz 2})))))
into the smallest maps that satisfy each solution's constraints, in this case {:foo 5, :bar 2} and {:foo 5, :baz 2}?
You could try this, which is a bit complicated, but it does the job.
(->> (run 1 [q]
(fresh [l1 l2 a]
(membero a '(5))
(emptyo l1)
(conso [:foo a] l1 l2)
(conso [:bar 2] l2 q)))
(first)
(into {}))
or
(run 1 [q]
(fresh [a B]
(membero a '(5))
(== B 2)
(== q {:foo a :bar B})))
I am sure that William Byrd can do better.
I just read the primer for core.logic. It makes sense so far, but I'm not sure where to go to learn more.
Let's say I wanted to write my own constraint, sort of like the membero shown in the primer. This one is called vectoro and constrains things to be a vector.
(defn vectoro [s] ???)
(run* [q]
(conde
[(== q [1 2])]
[(== q :a)])
(vectoro q))
Now I want that to return [1 2]. How do you write vectoro? Is this documented anywhere?
There's a core.logic pred macro that makes this easy:
(run* [q]
(== q [1 2])
(pred q vector?))
=> ([1 2])
(run* [q]
(== q '(1 2))
(pred q vector?))
=> ()
Here's how you might define a vectoro function/contraint (but realize this is essentially the exact same thing pred is doing):
(defn vectoro [a]
(project [a]
(== true (vector? a))))
project is used to operate on the actual/concrete value of the logic variable (LVar). We can then use the plain old predicate vector? and require the result be true. This also works with your example program:
(run* [q]
(conde
[(== q [1 2])]
[(== q :a)])
(vectoro q))
=> ([1 2])
(run* [q]
(conde
[(== q '(1 2))]
[(== q :a)])
(vectoro q))
=> ()
To continue learning, I suggest looking for projects that use core.logic and seeing if their source code teaches you anything.
As for your specific question about vectoro, the "project" function (as in "projection") will probably accomplish what you want, something along the lines of
(defn vectoro [s v]
(core.logic/project [v]
(core.logic/== s (vector? v)))
I'm trying to implement the opposite of membero in clojure.core.logic, but it's returning two values instead of one. Otherwise, it works fine (returns nothing when the value is in the list, and something when it is not).
(defne nonmembero
"A relation where l is a collection, such that l does not contain x"
[x l]
([_ ()])
([_ [head]]
(!= x head))
([_ [head . tail]]
(!= x head)
(nonmembero x tail)))
Example runs:
user> (run* [x] (nonmembero 1 [2 3 4 5]))
(_0 _0)
user> (run* [x] (nonmembero 1 [2 3 1 4 5]))
()
You don't need the second pattern i.e the [_ [head]. This is causing a new branch in search space of core.logic engine and hence leads to the 2 output. The last pattern i.e [head . tail] is enough to handle the case where you have only one element in the list. Now your solution becomes:
(defne nonmembero
"A relation where l is a collection, such that l does not contain x"
[x l]
([_ ()])
([_ [head . tail]]
(!= x head)
(nonmembero x tail)))
Something is wrong with the code above. It finds a solution for the following
(run* [q](== q 1)(nonmembero q [1 2 3])) => (1)
The following gives the expected result
(run* [q](== q 1)(nonmembero2 q [1 2 3])) => ()
where nonmembero2 is
(defn nonmembero2
[x l]
(fresh [h t]
(conde
[(== l ())]
[(conso h t l)
(!= x h)
(nonmembero2 x t)])))
What is the advantage of using fd/distinct in cases where the elements will be in a finite domain instead of distincto?
The following all return ([0 1] [1 0]).
;;; With distincto
(run* [q]
(fresh [x y]
(fd/in x y (fd/interval 1))
(distincto [x y])
(== q [x y])))
;;; With fd/distinct
(run* [q]
(fresh [x y]
(fd/in x y (fd/interval 1))
(fd/distinct [x y])
(== q [x y])))
;;; Without fd at all.
(let [interval [0 1]]
(run* [q]
(fresh [x y]
(membero x interval)
(membero y interval)
(distincto [x y])
(== q [x y]))))
Notably, although it appears you may use distincto in any place where you may use fd/distinct (but not the other way around), the same cannot be said for membero and fd/in.
fd/distinct is much more highly optimized than distincto which must receive any kind of value. fd/distinct under the hood deals with efficient representations of constrained variables all at once using sets, distincto uses the disequality operator != in a pretty simple manner.
I've used clojure for a while but just starting out with core.logic.
Given a domain like 1 2 3 4 I want to get a vector of two vectors back like ([[1 2] [3 4]]).
Note: This is just a simplified version of what I'm really trying to do. :) See: https://github.com/adamhoward/lineup
I found this definition for not-membero on the web:
(defne not-membero [x l]
([_ []])
([_ [?y . ?r]]
(!= x ?y)
(not-membero x ?r)))
And I'm trying to use it like this:
(run 1 [q]
(fresh [w x
y z]
(== q [[w x]
[y z]])
(infd w x y z (domain 1 2 3 4))
(everyg distinctfd [[w x] [y z]])
(everyg #(not-membero % [y z]) [w x])))
Running this in Emacs gives me an Evaluation aborted. message.
When I try switching out membero for not-membero I get back ([[1 2] [1 2]]) which makes sense to me. Every element in the first vector [w x] is a member of the second vector [y z].
But, when I call run 2 I get back ([[1 2] [1 2]] [[1 2] [1 3]]). I don't understand how [[1 2] [1 3]] can be correct for the rules above. Am I not understanding everyg correctly? Any guidance (including rtfmanual, rtfbook, rtfdissertation) will be appreciated.
Thanks.
Edit: May have solved this.
Still not sure about the strange results from membero but instead of the not-membero goal I found that I could do this:
(everyg #(distinctfd (conj [y z] %)) [w x])
Each element of [w x] conj'd to [y z] contains all distinct values. This may be less efficient than not-membero so I'm still open to any help.
Your example seems to be doing the same as this:
(run* [q]
(fresh [w x y z]
(fd/in w x y z (fd/domain 1 2 3 4))
(== q [[w x] [y z]])
(fd/distinct [w x y z])))
But seeing the soccer problem...
(ns test
(:refer-clojure :exclude [==])
(:use clojure.core.logic)
(require [clojure.core.logic.fd :as fd]))
(defn game [g] (fresh [a b c d e] (== g [a b c d e])))
(def players (range 1 11))
(defne not-membero [x l]
([_ []])
([_ [?y . ?r]]
(!= x ?y)
(not-membero x ?r)))
(defne plays [player games]
([_ []])
([_ [?f ?s . ?r]]
(membero player ?f) ; if you play this game
(not-membero player ?s) ; do not play next game
(plays player ?r))
([_ [?f ?s . ?r]]
(membero player ?s)
(not-membero player ?f)
(plays player ?r)))
(defne goalies [games]
([[[_ _ _ _ ?a]
[_ _ _ _ ?b]
[_ _ _ _ ?c]
[_ _ _ _ ?d]]]
(fd/in ?a ?b ?c ?d (apply fd/domain players))
(not-membero 1 [?a ?b ?c ?d]) ; star player != goalie
(fd/distinct [?a ?b ?c ?d]))) ; not goalie twice
(defn -main [& args]
(run 1 [q]
(fresh [a b c d]
(game a)
(game b)
(game c)
(game d)
(== q [a b c d])
(goalies q)
(everyg #(plays % q) players))))