Skip to content

Instantly share code, notes, and snippets.

@austinhaas
Created May 9, 2013 04:54
Show Gist options
  • Save austinhaas/5545629 to your computer and use it in GitHub Desktop.
Save austinhaas/5545629 to your computer and use it in GitHub Desktop.
core.logic findall sketch
(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])))
@austinhaas
Copy link
Author

I'm not exactly sure how nonground terms should be handled in the results. I'm leaning towards caveat emptor at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment