Skip to content

Commit b350b61

Browse files
committed
document context
1 parent 005d32d commit b350b61

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

src/matchers.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ Base.show(io::IO, s::Segment) = (print(io, "~~"); print(io, s.name))
2929
makesegment(s::Symbol, keys) = (push!(keys, s); Segment(s))
3030

3131
"""
32-
A wrapper for slot and segment predicates which allows them to
33-
take two arguments: the value and a Context
32+
A wrapper indicating that the function inside must be called with
33+
2 arguments. An expression, and the current context.
3434
"""
3535
struct Contextual{F}
3636
f::F

src/rule_dsl.jl

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function (r::Rule)(term, ctx=EmptyCtx())
4040
end
4141

4242
"""
43-
`@rule LHS => RHS`
43+
@rule LHS => RHS
4444
4545
Creates a `Rule` object. A rule object is callable, and takes an expression and rewrites
4646
it if it matches the LHS pattern to the RHS pattern, returns `nothing` otherwise.
@@ -141,6 +141,19 @@ sin((a + c))
141141
```
142142
143143
Predicate function gets an array of values if attached to a segment variable (`~~x`).
144+
145+
**Context**:
146+
147+
_In predicates_: Contextual predicates are functions wrapped in the `Contextual` type.
148+
The function is called with 2 arguments: the expression and a context object
149+
passed during a call to the Rule object (maybe done by passing a context to `simplify` or
150+
a `RuleSet` object).
151+
152+
The function can use the inputs however it wants, and must return a boolean indicating
153+
whether the predicate holds or not.
154+
155+
_In the consequent pattern_: Use `(@ctx)` to access the context object on the right hand side
156+
of an expression.
144157
"""
145158
macro rule(expr)
146159
@assert expr.head == :call && expr.args[1] == :(=>)
@@ -206,11 +219,12 @@ end
206219
#### Rulesets
207220

208221
"""
209-
RuleSet(rules::Vector{AbstractRules})(expr; depth=typemax(Int), applyall=false, recurse=true)
222+
RuleSet(rules::Vector{AbstractRules}, context=EmptyCtx())(expr; depth=typemax(Int), applyall=false, recurse=true)
210223
211-
`RuleSet` is an `AbstractRule` which applies the given `rules` throughout an `expr`.
224+
`RuleSet` is an `AbstractRule` which applies the given `rules` throughout an `expr` with the
225+
context `context`.
212226
213-
`RuleSet(rules)(expr)` Note that this only applies the rules in one pass, not until there are no
227+
Note that this only applies the rules in one pass, not until there are no
214228
changes to be applied. Use `SymbolicUtils.fixpoint(ruleset, expr)` to apply a RuleSet until there
215229
are no changes.
216230

src/simplify.jl

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
##### Numeric simplification
22

3-
default_rules(::EmptyCtx) = SIMPLIFY_RULES
43
"""
5-
simplify(x, [rules=SIMPLIFY_RULES]; fixpoint=true, applyall=true, recurse=true)
4+
default_rules(context::T)::RuleSet
65
7-
Apply a `RuleSet` of rules provided in `rules`. By default
8-
these rules are `SymbolicUtils.SIMPLIFY_RULES`. If `fixpoint=true`
9-
repeatedly applies the set of rules until there are no changes.
6+
The `RuleSet` to be used by default for a given context. Julia packages
7+
defining their own context types should define this method.
8+
9+
By default, returns SIMPLIFY_RULES
10+
"""
11+
default_rules(::Any) = SIMPLIFY_RULES
12+
13+
"""
14+
simplify(x, ctx=EmptyCtx();
15+
rules=default_rules(ctx),
16+
fixpoint=true,
17+
applyall=true,
18+
recurse=true)
19+
20+
Simplify an expression by applying `rules` until there are no changes.
21+
The second argument, the context is passed to every [`Contextual`](#Contextual)
22+
predicate and can be accessed as `(@ctx)` in the right hand side of `@rule` expression.
23+
24+
By default the context is an `EmptyCtx()` -- which means there is no contextual information.
25+
Any arbitrary type can be used as a context, and packages defining their own contexts
26+
should define `default_rules(ctx::TheContextType)` to return a `RuleSet` that will
27+
be used by default while simplifying under that context.
28+
29+
If `fixpoint=true` this will repeatedly apply the set of rules until there are no changes.
1030
Applies them once if `fixpoint=false`.
1131
1232
The `applyall` and `recurse` keywords are forwarded to the enclosed
13-
`RuleSet`.
33+
`RuleSet`, they are mainly used for internal optimization.
1434
"""
1535
function simplify(x, ctx=EmptyCtx(); rules=default_rules(ctx), fixpoint=true, applyall=true, recurse=true)
1636
if fixpoint

0 commit comments

Comments
 (0)