(defmacro fnc
"Define an anonymous constraint that can be used with the unifier:
(let [oddc (fnc [x] (odd? x))]
(unifier {:a '?a} {:a 1} :when {'?a oddc})
;;=> {:a 1}
(unifier {:a '?a} {:a 2} :when {'?a oddc})
;;=> nil
)
Note, the constraint will not run until all arguments are fully ground.
Use defnc to define a constraint and assign a toplevel var."
[args & body]
(let [name (symbol (gensym "fnc"))]
`(fn ~args
(letfn [(~name [~@args]
(reify
~'clojure.lang.IFn
(~'invoke [this# a#]
(let [[~@args :as args#] (map #(clojure.core.logic/walk* a# %) ~args)
test# (do ~@body)]
(when test#
((clojure.core.logic/remcg this#) a#))))
clojure.core.logic.protocols/IConstraintOp
(~'rator [_#] '~name)
(~'rands [_#] (filter clojure.core.logic/lvar? (flatten ~args)))
clojure.core.logic.protocols/IReifiableConstraint
(~'reifyc [_# _# r# a#]
(list '~name (map #(clojure.core.logic/-reify r# %) ~args)))
clojure.core.logic.protocols/IRunnable
(~'runnable? [_# s#]
(clojure.core.logic/ground-term? ~args s#))
clojure.core.logic.protocols/IConstraintWatchedStores
(~'watched-stores [_#] #{:clojure.core.logic/subst})))]
(cgoal (~name ~@args))))))
See
for copyright and license details.
Vars in clojure.core.logic/fnc:
Used in 0 other vars
Comments top
No comments for fnc. Log in to add a comment.