-
-
Save hugoduncan/e44877d0a1b69fa3ed89 to your computer and use it in GitHub Desktop.
applying guards
This file contains 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
(defn rule->logic-terms | |
"Takes a rule, specified as a pattern, a production and zero or more guards, | |
and return logic terms that encode them." | |
[rule] | |
(let [[pattern production & guards] (prep rule)] | |
{:rule (or (-> rule meta :name) (first rule)) | |
:pattern (recursive-partial-map pattern) | |
:production production | |
:guards (fn [] | |
(if (seq guards) | |
(fn [substitutions] | |
(reduce | |
(fn [subs [op & args]] | |
((apply (op-map op op) args) subs)) | |
substitutions | |
guards)) | |
s#))})) | |
(defn matching-productions | |
"Takes an expression, and applies rules to it, returning a sequence | |
of valid productions." | |
[expr rules] | |
(run* [q] | |
(fresh [pattern production guards rule rule-name] | |
(membero | |
{:pattern pattern :production production :guards guards :rule rule} | |
rules) | |
(== expr pattern) | |
(== q {:production production :rule rule}) | |
(project [guards] (guards))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment