Skip to content

Instantly share code, notes, and snippets.

@hugoduncan
Created November 24, 2012 02:32
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 hugoduncan/e44877d0a1b69fa3ed89 to your computer and use it in GitHub Desktop.
Save hugoduncan/e44877d0a1b69fa3ed89 to your computer and use it in GitHub Desktop.
applying guards
(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