This namespace bootstraps the core rule macro and some utility functions.
This namespace bootstraps the core rule macro and some utility functions.
(name-rule name rule)
Attach a rule name to the given object's metadata.
Attach a rule name to the given object's metadata.
(pattern-args pattern)
Return the correctly ordered list of variable names in the pattern for use in defining a function that may be called with the result of calling all-values on a match result.
This function uses pure-pattern to remove all data from the pattern to prevent arg name generation.
This is so complex is because this function is designed to be called during macro expansion. It must eliminate any non-pattern expressions because they could look like matchers and be included in the list of matcher names, which would cause an arity error when the matcher is called. The identity function was chosen arbitrarily to replace all expressions because it could appear in the (? x identity) position, so needs to resolve as a function when the var is resolved.
Return the correctly ordered list of variable names in the pattern for use in defining a function that may be called with the result of calling all-values on a match result. This function uses pure-pattern to remove all data from the pattern to prevent arg name generation. This is so complex is because this function is designed to be called during macro expansion. It must eliminate any non-pattern expressions because they could look like matchers and be included in the list of matcher names, which would cause an arity error when the matcher is called. The identity function was chosen arbitrarily to replace all expressions because it could appear in the (? x identity) position, so needs to resolve as a function when the var is resolved.
(raw-matches match-procedure)
A success continuation that just returns the match 'dictionary', which may be either a dictionary or a function that behaves the same way as calling a map.
See also pattern.match.core/all-values
[[value-dict]] [[symbol-dict]]
A success continuation that just returns the match 'dictionary', which may be either a dictionary or a function that behaves the same way as calling a map. See also [[pattern.match.core/all-values]] [[value-dict]] [[symbol-dict]]
(rebuild-body args env-args ->handler injection-names injection-data)
(rebuild-rule rule
pattern
handler-body
handler-injection-names
handler-injection-data)
Update either the pattern or the handler body (or both) of the given rule.
Both the pattern and the handler-body must be quoted (unlike in rule
, where
the handler-body is not quoted. This is to allow programmatic manipulation
of the existing handler body, or otherwise generating it. The current version
of both is present in the rule metadata.
When rebuilding a rule using eval, anything that may contain local state must be injected. In the handler function, refer to data that will be injected with normal symbols. Provide those symbols as a vector of injection-names. The corresponding data to be injected should be in the same order in injection-data.
Update either the pattern or the handler body (or both) of the given rule. Both the pattern and the handler-body must be quoted (unlike in [[rule]], where the handler-body is not quoted. This is to allow programmatic manipulation of the existing handler body, or otherwise generating it. The current version of both is present in the rule metadata. When rebuilding a rule using eval, anything that may contain local state must be injected. In the handler function, refer to data that will be injected with normal symbols. Provide those symbols as a vector of injection-names. The corresponding data to be injected should be in the same order in injection-data.
(rule pattern)
(rule pattern handler-body)
(rule name pattern handler-body)
Create a single rule. There are 2 arities, both with unique behavior.
Arity 1: [pattern] -> identity rule (see below) Arity 2: [pattern body] -> simple replacement rule Arity 3: [name pattern body] -> named simple replacement rule
If the body
of arity 2 is nil/false the rule fails the same as if it had not
matched at all. If the matcher can backtrack and make another match, it may
attempt tho body/dict expression multiple times. Once the expression returns
a valid replacement value or map, the rule will have matched, the replacement
will be made, and no further backtracking will happen.
All pattern variables are bound with the match data in the handler body. For instance an arity 2 rule binding ?a and ?b that returns the sum of those matches:
(rule '(?a [?b]) (+ a b))
The same rule, named:
(rule add-a-to-b0 '(?a [?b]) (+ a b))
Rules may have unquote and spliced unquote in their definitions even if they are defined as normal quoted lists. The functionality is provided by a ruleset in pattern.r3.rewrite/spliced. It allows the following, but note that splices in rule definitions only happen at compile time:
(rule '[(? a ~my-pred) ~@my-seq-of-things]
{:matched a})
A rule with no handler will act as an identity rule, and will always match if the pattern matches. This may be useful within rule lists or for other higher level rule combinators that make use of the rule metadata in the match expression. For example:
(rule '?->expression)
Or the same rule, named must use the 3 arity:
(rule expression '?->expression (success))
Side note, (rule name '?->e)
seems nice, and I tried it but sometimes one may
want (rule symbol :found)
. It's a recipe for weird breakage so I removed it.
Environment args:
A rule can bind arguments from its environment by attaching metadata to the input rule as follows:
(rule set-var ^{:env-args [var-name]} '?form (sub (set ?var-name ?form)))
Rules can also be called with succeed and fail callbacks
(my-rule data env succeed fail)
Create a single rule. There are 2 arities, both with unique behavior. Arity 1: [pattern] -> identity rule (see below) Arity 2: [pattern body] -> simple replacement rule Arity 3: [name pattern body] -> named simple replacement rule If the `body` of arity 2 is nil/false the rule fails the same as if it had not matched at all. If the matcher can backtrack and make another match, it may attempt tho body/dict expression multiple times. Once the expression returns a valid replacement value or map, the rule will have matched, the replacement will be made, and no further backtracking will happen. All pattern variables are bound with the match data in the handler body. For instance an arity 2 rule binding ?a and ?b that returns the sum of those matches: (rule '(?a [?b]) (+ a b)) The same rule, named: (rule add-a-to-b0 '(?a [?b]) (+ a b)) Rules may have unquote and spliced unquote in their definitions even if they are defined as normal quoted lists. The functionality is provided by a ruleset in pattern.r3.rewrite/spliced. It allows the following, but note that splices in rule definitions only happen at *compile time*: (rule '[(? a ~my-pred) ~@my-seq-of-things] {:matched a}) A rule with no handler will act as an identity rule, and will always match if the pattern matches. This may be useful within rule lists or for other higher level rule combinators that make use of the rule metadata in the match expression. For example: (rule '?->expression) Or the same rule, named must use the 3 arity: (rule expression '?->expression (success)) Side note, `(rule name '?->e)` seems nice, and I tried it but sometimes one may want `(rule symbol :found)`. It's a recipe for weird breakage so I removed it. Environment args: A rule can bind arguments from its environment by attaching metadata to the input rule as follows: (rule set-var ^{:env-args [var-name]} '?form (sub (set ?var-name ?form))) Rules can also be called with succeed and fail callbacks (my-rule data env succeed fail)
(rule-fn-args args env-args)
(rule-fn-body args env-args handler-body)
(rule-fn-body name args env-args handler-body)
(rule-fn-rebuild-body name args env-args handler-body injection-names)
(sub form)
Use the version in the pattern.r3.rewrite namespace.
Use the version in the pattern.r3.rewrite namespace.
(success)
(success x)
(success x env)
Explicitly mark an object as successfully matched when returned from a rule.
The rule will unwrap the data automatically.
Allows rules to return user data directly without failing.
(success false) ;; Allows the rule to return false without failing.
The arity-0 version tells the matcher to use the original input data, also discarding any changes made by patterns that may have recursively matched with the rule.
Explicitly mark an object as successfully matched when returned from a rule. The rule will unwrap the data automatically. Allows rules to return user data directly without failing. (success false) ;; Allows the rule to return false without failing. The arity-0 version tells the matcher to use the original input data, also discarding any changes made by patterns that may have recursively matched with the rule.
(success:env env)
Success but only change the env.
Success but only change the env.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close