Expression Rewriting with SymbolicUtils

Impero provides hooks to SymbolicUtils.jl to allow manipuation of the syntax tree. In the following code snippet we take an expression that was originally a "+" operation and convert it to a "*" operation

using Impero, SymbolicUtils, Plots, GraphRecipes
import SymbolicUtils: Chain, Postwalk, Fixpoint
@wrapper a=1 b=2
c = a+b
r1 = @acrule ~ra + ~rb => ~ra * ~rb
new_c = Fixpoint(Postwalk(Chain([r1])))(c);
(a*b)

We can now check to see that this conversion happened succesfully

compute(c)

3

compute(new_c)

2

and even plot to show that the tree has been rewritten

p1 = plot(c); p2 = plot(new_c);
plot(p1,p2)