Skip to content

Instantly share code, notes, and snippets.

@swannodette
Forked from austinhaas/findall.clj
Created May 9, 2013 05:00
Show Gist options
  • Save swannodette/5545653 to your computer and use it in GitHub Desktop.
Save swannodette/5545653 to your computer and use it in GitHub Desktop.
(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