Skip to content

Instantly share code, notes, and snippets.

@hiredman
Created January 28, 2013 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiredman/4651836 to your computer and use it in GitHub Desktop.
Save hiredman/4651836 to your computer and use it in GitHub Desktop.
treeo
(defn treeo
"relation for trees represented as maps a la
clojure.xml/parse. `children` is the key of the map where children
are. `root` is the root of the tree, can be a logic var. `m` is a
map to \"pattern match\" against."
[children root m]
(if (map? m)
(if-let [x (get m children)]
(if (coll? x)
(logic/conde
[(logic/fresh [content]
(logic/featurec root (assoc m children content))
(logic/everyg (fn [element]
(logic/fresh [new-root]
(logic/membero new-root content)
(treeo children new-root element)))
x))]
[(logic/fresh [x y]
(logic/featurec root {children x})
(logic/membero y x)
(treeo children y m))]))
(logic/featurec root m))
(logic/== root m)))
(def xmlo (partial treeo :content))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment