# ANN: Espresso.jl - an expression transformation package

**URL:** <https://discourse.julialang.org/t/ann-espresso-jl-an-expression-transformation-package/1641>\
**Category:** Machine Learning\
**Created:** [January 22, 2017, 6:39pm UTC](https://discourse.julialang.org/t/ann-espresso-jl-an-expression-transformation-package/1641 "2017-01-22T18:39:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [January 22, 2017, 6:39pm UTC](https://discourse.julialang.org/t/ann-espresso-jl-an-expression-transformation-package/1641/1 "2017-01-22T18:39:54Z")

</div>

[Espresso.jl](https://github.com/dfdx/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](https://en.wikipedia.org/wiki/Einstein_notation) suitable for describing non-trivial operations on tensors. For example, one can convert a vectorized expression to the Einstein notation as:

```julia
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:

```julia
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](https://github.com/dfdx/Espresso.jl/blob/master/src/Espresso.jl#L6-L53).

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 22, 2017, 7:49pm UTC](https://discourse.julialang.org/t/ann-espresso-jl-an-expression-transformation-package/1641/2 "2017-01-22T19:49:34Z")

</div>

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!
