Skip to content

Commit 8612b2d

Browse files
committed
added documentation of defslot code
1 parent f594e03 commit 8612b2d

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

src/matchers.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ function defslot_term_matcher_constructor(term, acSets)
281281
# if no match, try to match with a defslot.
282282
# checks whether it matches the normal part if yes executes foo2
283283
# foo2: adds the pair (default value name, default value) to the found bindings
284-
# after checking predicate and presence in the bindings. If added succesfully
285-
# returns the bindings (foo3), otherwise return nothing
284+
# after checking predicate and presence in the bindings. If added
285+
# successfully returns the bindings (foo3), otherwise return nothing
286286
# <-------------------foo2----------------------------------->
287287
# <-foo3->
288288
result = other_part_matcher((b,n)->defslot_matcher((b,n)->b, (defslot.defaultValue,), b), data, bindings)

src/rule.jl

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,11 @@ it if it matches the LHS pattern to the RHS pattern, returns `nothing` otherwise
240240
The rule language is described below.
241241
242242
LHS can be any possibly nested function call expression where any of the arguments can
243-
optionally be a Slot (`~x`) or a Segment (`~~x`) (described below).
243+
optionally be a Slot (`~x`), Default Value Slot (`~!x` also called DefSlot) or a Segment
244+
(`~~x`) (described below).
244245
245-
If an expression matches LHS entirely, then it is rewritten to the pattern in the RHS
246-
Segment (`~x`) and slot variables (`~~x`) on the RHS will substitute the result of the
246+
If an expression matches LHS entirely, then it is rewritten to the pattern in the RHS.
247+
Slot, DefSlot and Segment variables on the RHS will substitute the result of the
247248
matches found for these variables in the LHS.
248249
249250
If the RHS is a single tilde `~`, then the rule returns a a dictionary of
@@ -306,6 +307,41 @@ julia> r(sin(2a)^2 + cos(a)^2)
306307
# nothing
307308
```
308309
310+
**DefSlot**:
311+
312+
A DefSlot variable is written as `~!x`. Works like a normal slot, but can also take additional values if not present in the expression.
313+
314+
_Example in power:_
315+
```julia
316+
julia> r_pow = @rule (~x)^(~!m) => ~m
317+
(~x) ^ ~(!m) => ~m
318+
319+
julia> r_pow(x^2)
320+
2
321+
322+
julia> r_pow(x)
323+
1
324+
```
325+
326+
_Example in sum:_
327+
```julia
328+
julia> r_sum = @rule ~x + ~!y => ~y
329+
~x + ~(!y) => ~y
330+
331+
julia> r_sum(x+2)
332+
x
333+
334+
julia> r_sum(x)
335+
0
336+
```
337+
338+
Currently DefSlot is implemented in:
339+
Operation | Default value
340+
----------|--------------
341+
* | 1
342+
+ | 0
343+
2nd argument of ^ | 1
344+
309345
**Segment**:
310346
311347
A Segment variable is written as `~~x` and matches zero or more expressions in the

test/rewrite.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ end
114114
@test r_predicate1(x+2) === 2
115115
@test r_predicate1(x+2.0) === nothing
116116

117-
# predicate checked in defslot mathcing process
117+
# predicate checked in defslot matching process
118118
r_predicate2 = @rule x + ~!m::(var->!(var===0)) => ~m
119119
@test r_predicate2(x+1)===1 # matches normally
120120
@test r_predicate2(x)===nothing # doesnt matches bc the default value is 0 and doesnt respect the predicate

0 commit comments

Comments
 (0)