Created
May 9, 2013 04:54
-
-
Save austinhaas/5545629 to your computer and use it in GitHub Desktop.
core.logic findall sketch
This file contains hidden or 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
(ns pettomato.findall | |
(:refer-clojure :exclude [==]) | |
(:use | |
[clojure.core.logic.protocols]) | |
(:require | |
[clojure.core.logic :refer :all])) | |
(defn findall | |
"A goal that unifies l with a lazy sequence containing all possible | |
instantiations of v that could be generated by applying the goal g | |
to the current substitution." | |
[v g l] | |
(fn [a] | |
(let [xs (map #(walk* % v) (take* (g a))) | |
;; The following line is necessary to remove the ::unbound | |
;; metadata flag from any fresh lvars, otherwise we will | |
;; have problems if we try to unify it later. | |
xs (map #(if (meta %) (with-meta % {}) %) xs) | |
g2 (== l xs)] | |
(g2 a)))) | |
(run* [q] | |
(fresh [x y] | |
(findall x (membero x [1 2 y]) q) | |
(== y 3) | |
(== q [1 2 3]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm not exactly sure how nonground terms should be handled in the results. I'm leaning towards caveat emptor at the moment.