Term rewriting engine.
Patterns: ?x — match anything, bind to x ??x — match zero or more (splice), bind to x (f ?x ?y) — match a list with head f, bind x and y _ — match anything, don't bind
Rules: (defrule name pattern => replacement)
Engine: (rewrite rules expr) — apply rules bottom-up to fixed point (rewrite-once rules expr) — one bottom-up pass (rewrite-top rules expr) — top-level only to fixed point
Term rewriting engine. Patterns: ?x — match anything, bind to x ??x — match zero or more (splice), bind to x (f ?x ?y) — match a list with head f, bind x and y _ — match anything, don't bind Rules: (defrule name pattern => replacement) Engine: (rewrite rules expr) — apply rules bottom-up to fixed point (rewrite-once rules expr) — one bottom-up pass (rewrite-top rules expr) — top-level only to fixed point
(apply-rule rule expr)Try to apply a single rule to an expression. Returns the rewritten expression, or nil if the rule doesn't match.
Try to apply a single rule to an expression. Returns the rewritten expression, or nil if the rule doesn't match.
(apply-rules rules expr)Try each rule in order against an expression. Returns the first successful rewrite, or nil if none match.
Try each rule in order against an expression. Returns the first successful rewrite, or nil if none match.
(defrule rule-name pattern _ replacement)Define a named rule. (defrule identity-plus (+ ?a 0) => ?a)
Define a named rule. (defrule identity-plus (+ ?a 0) => ?a)
(defrule-guard rule-name pattern _ replacement guard)Define a named rule with a guard. (defrule-guard positive-check ?x => :pos (fn [b] (pos? (b 'x))))
Define a named rule with a guard. (defrule-guard positive-check ?x => :pos (fn [b] (pos? (b 'x))))
(make-rule rule-name pattern replacement)(make-rule rule-name pattern replacement guard)Create a rule from a pattern and replacement template. Optionally takes a guard function.
Create a rule from a pattern and replacement template. Optionally takes a guard function.
(match-pattern pattern expr)(match-pattern pattern expr bindings)Match a pattern against an expression. Returns a bindings map {symbol value} on success, nil on failure.
Examples: (match-pattern '?x 42) => {x 42} (match-pattern '(f ?x) '(f 1)) => {x 1} (match-pattern '_ anything) => {}
Match a pattern against an expression.
Returns a bindings map {symbol value} on success, nil on failure.
Examples:
(match-pattern '?x 42) => {x 42}
(match-pattern '(f ?x) '(f 1)) => {x 1}
(match-pattern '_ anything) => {}(match-seq patterns exprs bindings)Match a pattern sequence against an expression sequence. Handles splice variables (??x). Returns bindings map or nil on failure.
Match a pattern sequence against an expression sequence. Handles splice variables (??x). Returns bindings map or nil on failure.
(pattern-var? x)Is this a pattern variable like ?x
Is this a pattern variable like ?x
(rewrite rules expr)(rewrite rules expr max-iters)Apply rules repeatedly (bottom-up) until fixed point or max iterations. Returns the final expression.
Apply rules repeatedly (bottom-up) until fixed point or max iterations. Returns the final expression.
(rewrite-once rules expr)One bottom-up pass: try to rewrite each node, innermost first. Returns [changed? result].
One bottom-up pass: try to rewrite each node, innermost first. Returns [changed? result].
(rewrite-once-top rules expr)Try rules at top level only, return first match or original.
Try rules at top level only, return first match or original.
(rewrite-top rules expr)(rewrite-top rules expr max-iters)Apply rules only at the top level (no descent into children). Repeat until fixed point.
Apply rules only at the top level (no descent into children). Repeat until fixed point.
(rule pattern replacement)(rule pattern replacement guard)Shorthand: (rule '(+ ?a 0) '?a)
Shorthand: (rule '(+ ?a 0) '?a)
(ruleset & forms)Define a vector of rules. (ruleset (+ ?a 0) => ?a (* ?a 1) => ?a)
Define a vector of rules. (ruleset (+ ?a 0) => ?a (* ?a 1) => ?a)
(splice-var? x)Is this a splice variable like ??x (matches zero or more)
Is this a splice variable like ??x (matches zero or more)
(substitute template bindings)Replace pattern variables in template with values from bindings. Handles splice variables — ??x splices its seq into the parent list.
Replace pattern variables in template with values from bindings. Handles splice variables — ??x splices its seq into the parent list.
(var-name x)Extract the name from ?x or ??x
Extract the name from ?x or ??x
(wildcard? x)Is this the wildcard _ (matches anything, no binding)
Is this the wildcard _ (matches anything, no binding)
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |