Created
March 8, 2012 19:52
-
-
Save ohpauleez/2003000 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(comment | |
Given some rules like: | |
{(if ?x ?y nil) (when ?x ?y) | |
(when true . ?x) (do . ?x)} | |
And the `unify` and `check-form` functions below, | |
I would like to apply `unify` until I `unify` returns nil. | |
This means I can take a form and apply multiple rules in succession | |
(if true ?x nil) => (when true x?) => (do ?x) | |
I've been able to achieve this with loop/recur (`loop-check-form`) , but I was curious if | |
core.logic can do this for me | |
) | |
(defn unify | |
"TODO jonas" | |
[expr rule] | |
(let [[r s] (#'logic/prep rule) | |
alt (first (logic/run* [alt] | |
(logic/== expr r) | |
(logic/== s alt)))] | |
(when alt | |
{:expr expr | |
:rule rule | |
:alt (seq alt) | |
:line (-> expr meta :line)}))) | |
(defn check-form | |
"Given an expression/line/form, return a map containing the alternative suggestion info, or `nil`" | |
([expr] | |
(check-form expr all-rules)) | |
([expr rules] | |
(when (sequential? expr) | |
(some #(unify expr %) rules)))) | |
(defn loop-check-form | |
([expr] | |
(check-form expr all-rules)) | |
([expr rules] | |
(when (sequential? expr) | |
(loop [expr expr | |
alt-map nil] | |
(if-let [new-alt-map (some #(unify expr %) rules)] | |
(recur (:alt new-alt-map) | |
new-alt-map) | |
alt-map))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment