Advertisement
Guest User

Untitled

a guest
Feb 6th, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defn qmark-symbol?
  2.   "Returns true, if sym is a symbol with name starting with a question mark."
  3.   [sym]
  4.   (and (symbol? sym)
  5.        (= (first (name sym)) \?)))
  6.  
  7. (defmacro with-fresh
  8.   "Replace all symbols with a leading question mark with fresh lvars.
  9.  In addition, all occurences of `_' are replaced with fresh lvars, one per
  10.  occurence.  That means, that in `forms' all occurences of ?foo will be
  11.  unified, but all occurences of `_' are not."
  12.   [& forms]
  13.   (let [fs (clojure.walk/postwalk #(if (= '_ %) (gensym "?") %) forms)
  14.         qsyms (vec (distinct (filter qmark-symbol? (flatten fs))))]
  15.     `(fresh ~qsyms
  16.        ~@fs)))
  17.  
  18. ;; Using that, you can say something like
  19.  
  20. (run* [q]
  21.   (with-fresh
  22.     (foo ?x ?p _)
  23.     (bar _ ?x ?p)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement