Pattern matching, unification, and strategies
Source:vignettes/pattern-matching.Rmd
pattern-matching.Rmd
library(symcompR)
x <- Symbol("x"); y <- Symbol("y"); z <- Symbol("z")
a <- Symbol("a"); b <- Symbol("b"); p <- Symbol("p"); q <- Symbol("q")
f <- Function("f"); g <- Function("g")This vignette covers pattern matching with wildcards,
sympy.unify, sympy.strategies, and the
expression paths of sympy.simplify.epathtools.
Pattern matching
Wild() creates a pattern variable.
sym_match() returns the values of the wildcards for which
the pattern equals the expression, or NULL when there are
none. The exclude argument keeps a wildcard from matching
expressions containing given symbols.
wa <- Wild("a", exclude = list(x)); wb <- Wild("b", exclude = list(x))
sym_match(3 * x + 2, wa * x + wb)
#> $a
#> 3
#>
#> $b
#> 2
sym_match(x^2 + 5, wa * x + wb)
#> NULLsym_replace() replaces every match of a pattern:
u <- Wild("u")
sym_replace(sin(x)^2 + 1, sin(u), cos(u))
#> cos(x)^2 + 1
sym_replace(f(x) + f(y), f(u), g(u))
#> g(x) + g(y)WildFunction() matches any function application:
F <- WildFunction("F")
sym_match(f(x), F)
#> $F
#> f(x)
sym_match(x + cos(x), x + F)
#> $F
#> cos(x)Unification
unify() finds all ways to make two expressions equal by
assigning the given variables, taking commutativity into account.
unify(a + b, x + y, variables = list(x, y))
#> [[1]]
#> [[1]]$x
#> a
#>
#> [[1]]$y
#> b
#>
#>
#> [[2]]
#> [[2]]$y
#> a
#>
#> [[2]]$x
#> brewriterule() turns a pattern and a replacement into a
rule, which returns all the rewritten forms. A condition can restrict
the matches.
rewriterule(p^2, p^3, variables = list(p))(x^2)
#> [[1]]
#> x^3
rewriterule(p^2 + q^3, (p * q)^4, list(p, q))(x^2 + y^3)
#> [[1]]
#> x^4*y^4
rewriterule(x, x + 1, list(x), function(x) isTRUE(x < 10))(Integer(5))
#> [[1]]
#> 1 + 5
rewriterule(x, x + 1, list(x), function(x) isTRUE(x < 10))(Integer(15))
#> list()The unification engine works on generic trees.
unify_compound() builds a node from an operator and
arguments, unify_variable() a variable, and
unify_condvariable() a variable with a condition.
unify_core() returns all the substitutions that unify two
trees.
v <- unify_variable("v")
t1 <- unify_compound("Add", list(1, 2))
t2 <- unify_compound("Add", list(v, 2))
unify_core(t1, t2)
#> [[1]]
#> [[1]]$`V(Lv)`
#> [[1]]$`V(Lv)`$k
#> $arg
#> [1] "v"
#>
#> attr(,"class")
#> [1] "unify_variable"
#>
#> [[1]]$`V(Lv)`$v
#> [1] 1
cv <- unify_condvariable("w", function(e) e > 1)
unify_core(t1, unify_compound("Add", list(1, cv)))
#> [[1]]
#> [[1]]$`C(Lw)`
#> [[1]]$`C(Lw)`$k
#> $arg
#> [1] "w"
#>
#> $valid
#> function (e)
#> e > 1
#> <environment: 0x7b32b7a120>
#>
#> attr(,"class")
#> [1] "unify_condvariable"
#>
#> [[1]]$`C(Lw)`$v
#> [1] 2
unify_core(t1, unify_compound("Add", list(cv, 2)))
#> list()Rewriting with strategies
sympy.strategies provides combinators for building
rewrite systems out of simple rules. A rule is a function from an
expression to an expression; exhaust() applies a rule until
nothing changes, chain() applies rules in sequence, and
greedy() explores a tree of alternatives and keeps the best
result by an objective function.
inc <- function(e) if (isTRUE(is_integer(e)) && as.numeric(e) < 5) e + 1 else e
exhaust(inc)(Integer(1))
#> 5
chain(inc, inc)(Integer(0))
#> 2
best <- greedy(list(function(e) expand(e), function(e) sym_factor(e)),
objective = count_ops)
best(x^2 + 2 * x + 1)
#> (x + 1)^2Rules
A rule takes an expression and returns a new one. The rules of
sympy.strategies.rl work on any node;
strat_new() builds one with a given operator, here a
generic Basic node.
B <- function(...) strat_new(list(type = "Basic"), ...)
e <- B(S(1), S(0), S(2))
e
#> Basic(1, 0, 2)
rm_id(function(a) a == 0)(e)
#> Basic(1, 2)
unpack(B(S(2)))
#> 2
rl$flatten(B(S(1), B(S(2), S(3))))
#> Basic(1, 2, 3)
rl$sort(function(a) as.numeric(a))(B(S(3), S(1), S(2)))
#> Basic(1, 2, 3)
glom(function(a) a, function(a) 1, function(cnt, a) cnt * a)(B(S(2), S(2), S(3)))
#> Basic(4, 3)
distribute("Mul", "Add")(strat_new(list(type = "Mul"), S(2), strat_new(list(type = "Add"), x, y)))
#> 2*x + 2*y
rebuild(strat_new(list(type = "Add"), x, x))
#> 2*x
rl$subs(S(1), S(5))(S(1))
#> 5basic_fns and expr_fns tell the traversals
how to take a node apart and put it back together; expr_fns
evaluates the rebuilt expression.
names(basic_fns)
#> [1] "op" "new" "leaf" "children"
traverse$top_down(function(e) if (isTRUE(e == 1)) S(9) else e)(B(S(1), B(S(1), S(2))))
#> Basic(9, Basic(9, 2))
traverse$bottom_up(function(e) if (isTRUE(is_integer(e))) e + 1 else e)(B(S(1), S(2)))
#> Basic(2, 3)
traverse$top_down(function(e) if (isTRUE(e == 1)) S(9) else e, fns = expr_fns)(x + 1)
#> x + 9Combinators
condition() applies a rule only when a predicate holds,
do_one() applies the first rule that changes the
expression, minimize() keeps the best of several results,
strat_switch() dispatches on a key, typed()
dispatches on the type of the expression, memoize() caches
results, null_safe() and tryit() return the
input when a rule fails, and canon() applies rules
everywhere until nothing changes. strat_identity() is the
rule that does nothing, and strat_debug() prints each
step.
inc <- function(e) e + 1; dec <- function(e) e - 1; dbl <- function(e) 2 * e
condition(function(e) e > 2, inc)(S(1))
#> 1
do_one(strat_identity, inc)(S(1))
#> 2
minimize(inc, dec, objective = as.numeric)(S(3))
#> 2
strat_switch(function(e) e$type, list(Integer = inc))(S(1))
#> 2
by_type <- typed(list(Integer = inc, Symbol = dbl))
vapply(lapply(list(S(3), x, x + 1), by_type), sstr, "")
#> [1] "4" "2*x" "x + 1"
memoize(inc)(S(1))
#> 2
null_safe(function(e) NULL)(S(1))
#> 1
tryit(function(e) stop("no"))(S(1))
#> 1
canon(function(e) if (identical(e$type, "Basic") && length(e$args) == 1) e$args[[1]] else e)(B(B(S(1))))
#> 1
strat_identity(x)
#> x
strat_debug(inc)(S(1))
#> Rule: inc
#> In: 1
#> Out: 2
#> 2Trees of strategies
A list of rules is a choice and strat_seq() a sequence.
greedy() takes the best choice at each step,
brute() tries all of them, and allresults()
returns every result. treeapply() evaluates a tree with
given functions for choices and sequences, and branch holds
the branching versions of the combinators, which return lists of
results.
tree <- list(inc, strat_seq(dec, dbl))
greedy(tree, objective = as.numeric)(S(4))
#> 5
greedy(tree, objective = as.numeric)(S(1))
#> 0
brute(tree, objective = as.numeric)(S(4))
#> 5
allresults(tree)(S(4))
#> [[1]]
#> 5
#>
#> [[2]]
#> 6
treeapply(list(strat_seq(3, 2), strat_seq(4, 1)), list(list = max, seq = min))
#> [1] 2
branch$multiplex(function(e) list(e + 1), function(e) list(e - 1))(S(3))
#> [[1]]
#> 4
#>
#> [[2]]
#> 2Expression paths
epath() selects or modifies parts of an expression with
a path language similar to XPath: /* descends into all
arguments, /Name filters by class, and /[i]
selects by position. EPath() stores a compiled path.
EPath("/*/[0]")
#> EPath('/*/[0]')
e <- Symbol("t") + sin(x + 1) + cos(x + y + sym_E)
epath("/*/*/Symbol", e)
#> [[1]]
#> x
#>
#> [[2]]
#> x
#>
#> [[3]]
#> y
epath("/*/*/Symbol", e, function(s) 2 * s)
#> t + sin(2*x + 1) + cos(2*x + 2*y + E)
nested <- list(list(list(x, 1), 2), list(list(3, y), z))
epath("/*/[0]/Symbol", nested)
#> [[1]]
#> x
#>
#> [[2]]
#> y
epath("/*/[-1]", nested)
#> [[1]]
#> [1] 2
#>
#> [[2]]
#> z