ANN: Espresso.jl - an expression transformation package

Espresso.jl is a new package for expression matching and transformation. A few functions that might be intersting for people working with Julia AST:

  • matchex - match expression by pattern, extract matching elements;
  • subs - substitute elements of an expression according to substitution table;
  • rewrite - rewrite an expression by matching it to a pattern and replacing corresponding placeholders in substitution expression;
  • simplify - simplify algebraic expression.

For example, consider derivation rule:

f(x) = x^2
f'(x) = 2*x

One may rewrite existing expression to its derivative as easy as:

ex = :(u ^ 2)             # expression to differentiate
pat = :(_x ^ 2)           # _x is a placeholder - it will match actual variable
subsex = :(2 * _x)        # substitution expression
rewrite(ex, pat, subsex)  # gives :(2 * u)

Additionally, Espresso.jl supports extended Einstein notation suitable for describing non-trivial operations on tensors. For example, one can convert a vectorized expression to the Einstein notation as:

to_einstein(:(W*x + b); W=rand(3,4), x=rand(4), b=rand(3))
# quote
#    tmp1[i] = W[i,k] * x[k]
#    tmp2[i] = tmp1[i] + b[i]
# end 

and backward as:

from_einstein(:(W[i,k] * x[k] + b[i]))
# quote
#     tmp1 = W * x
#     tmp2 = tmp1 + b
# end

Other supported functions may be found in README.md and the list of exports.

7 Likes

This is really cool! Thanks for sharing! I fully plan to redo some of the internals of ParameterizedFunctions.jl with this, and hope to use it to easily expand the language. Thanks!