-
-
Save Hamled/b74746e45d99c4fd2dc76ee9a2b8eec8 to your computer and use it in GitHub Desktop.
List size goal operator with core.logic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; These two goals taken from https://spin.atomicobject.com/2015/12/14/logic-programming-clojure-finite-domain-constraints/ | |
(defn sumo [l sum] | |
(fresh [head tail tail-sum] | |
(conde | |
[(== l ()) (== sum 0)] | |
[(conso head tail l) | |
(fd/+ head tail-sum sum) | |
(sumo tail tail-sum)]))) | |
(defn everyo [l f] | |
(fresh [head tail] | |
(conde | |
[(== l ())] | |
[(conso head tail l) | |
(f head) | |
(everyo tail f)]))) | |
(defn sizeo [l size] | |
(fresh [tail tail-size] | |
(conde | |
[(== l ()) (== size 0)] | |
[(resto l tail) | |
(fd/+ size -1 tail-size) | |
(sizeo tail tail-size)]))) | |
(run 1 [l] | |
(fresh [size] | |
(fd/in size (fd/interval 0 2)) | |
(everyo l #(fd/in % (fd/interval 1 10))) | |
(sumo l 10) | |
(sizeo l size))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment