Skip to content

Commit 2c0cff4

Browse files
committed
added documentation
1 parent 5251051 commit 2c0cff4

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

docs/src/manual/rewrite.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,23 @@ Notice that the expression was autosimplified before application of the rule.
9797
2(2w + α + β)
9898
```
9999

100+
Note that writing a single tilde `~` as consequent, will make the rule return a dictionary of [slot variable, expression matched].
101+
102+
```jldoctest rewrite
103+
r = @rule (~x + (~y)^(~m)) => ~
104+
105+
r(z+w^α)
106+
107+
# output
108+
Base.ImmutableDict{Symbol, Any} with 5 entries:
109+
:MATCH => z + w^α
110+
:m => α
111+
:y => w
112+
:x => z
113+
:____ => nothing
114+
115+
```
116+
100117
### Predicates for matching
101118

102119
Matcher pattern may contain slot variables with attached predicates, written as `~x::f` where `f` is a function that takes a matched expression and returns a boolean value. Such a slot will be considered a match only if `f` returns true.

src/rule.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,24 @@ If an expression matches LHS entirely, then it is rewritten to the pattern in th
243243
Segment (`~x`) and slot variables (`~~x`) on the RHS will substitute the result of the
244244
matches found for these variables in the LHS.
245245
246+
If the RHS is a single tilde `~`, then the rule returns a a dictionary of
247+
[slot variable, expression matched].
248+
249+
_Example:_
250+
251+
```julia
252+
julia> r = @rule (~x + (~y)^(~m)) => ~
253+
~x + (~y) ^ ~m => (~)
254+
255+
julia> r(a + b^2)
256+
Base.ImmutableDict{Symbol, Any} with 5 entries:
257+
:MATCH => a + b^2
258+
:m => 2
259+
:y => b
260+
:x => a
261+
:____ => nothing
262+
```
263+
246264
**Slot**:
247265
248266
A Slot variable is written as `~x` and matches a single expression. `x` is the name of the variable. If a slot appears more than once in an LHS expression then expression matched at every such location must be equal (as shown by `isequal`).

0 commit comments

Comments
 (0)