Liking cljdoc? Tell your friends :D

meme.alpha.rewrite

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
raw docstring

apply-ruleclj/s

(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.
sourceraw docstring

apply-rulesclj/s

(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.
sourceraw docstring

defrulecljmacro

(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)
sourceraw docstring

defrule-guardcljmacro

(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))))
sourceraw docstring

make-ruleclj/s

(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.
sourceraw docstring

match-patternclj/s

(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)       => {}
sourceraw docstring

match-seqclj/s

(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.
sourceraw docstring

pattern-var?clj/s

(pattern-var? x)

Is this a pattern variable like ?x

Is this a pattern variable like ?x
sourceraw docstring

rewriteclj/s

(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.
sourceraw docstring

rewrite-onceclj/s

(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].
sourceraw docstring

rewrite-once-topclj/s

(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.
sourceraw docstring

rewrite-topclj/s

(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.
sourceraw docstring

ruleclj/s

(rule pattern replacement)
(rule pattern replacement guard)

Shorthand: (rule '(+ ?a 0) '?a)

Shorthand: (rule '(+ ?a 0) '?a)
sourceraw docstring

rulesetcljmacro

(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)
sourceraw docstring

splice-var?clj/s

(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)
sourceraw docstring

substituteclj/s

(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.
sourceraw docstring

var-nameclj/s

(var-name x)

Extract the name from ?x or ??x

Extract the name from ?x or ??x
sourceraw docstring

wildcard?clj/s

(wildcard? x)

Is this the wildcard _ (matches anything, no binding)

Is this the wildcard _ (matches anything, no binding)
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close