All Projects → dfdx → Espresso.jl

dfdx / Espresso.jl

Licence: other
Expression transformation package

Programming Languages

julia
2034 projects
macros
77 projects
metaprogramming
66 projects

Projects that are alternatives of or similar to Espresso.jl

flowpython
tasty feature extensions for python3(NO MAINTENANCE!).
Stars: ✭ 66 (+43.48%)
Mutual labels:  pattern-matching
Qo
Qo - Query Object - Pattern matching and fluent querying in Ruby
Stars: ✭ 351 (+663.04%)
Mutual labels:  pattern-matching
Tiny Glob
Super tiny and ~350% faster alternative to node-glob
Stars: ✭ 710 (+1443.48%)
Mutual labels:  pattern-matching
cats.match
Pattern matching for the monads in the cats Clojure library
Stars: ✭ 49 (+6.52%)
Mutual labels:  pattern-matching
Rascal
The implementation of the Rascal meta-programming language (including interpreter, type checker, parser generator, compiler and JVM based run-time system)
Stars: ✭ 284 (+517.39%)
Mutual labels:  pattern-matching
Defun
A macro to define clojure functions with parameter pattern matching just like erlang or elixir.
Stars: ✭ 432 (+839.13%)
Mutual labels:  pattern-matching
siringa
Minimalist dependency injection library for Python that embraces type annotations syntax
Stars: ✭ 51 (+10.87%)
Mutual labels:  pattern-matching
Ts Pattern
🎨 A complete Pattern Matching library for TypeScript, with smart type inference.
Stars: ✭ 854 (+1756.52%)
Mutual labels:  pattern-matching
Pampy
Pampy: The Pattern Matching for Python you always dreamed of.
Stars: ✭ 3,419 (+7332.61%)
Mutual labels:  pattern-matching
Meander
Tools for transparent data transformation
Stars: ✭ 617 (+1241.3%)
Mutual labels:  pattern-matching
gomatch
Library created for testing JSON against patterns.
Stars: ✭ 41 (-10.87%)
Mutual labels:  pattern-matching
matchete
Simple pattern-matching library for Clojure(Script)
Stars: ✭ 65 (+41.3%)
Mutual labels:  pattern-matching
Patterns
This is an experimental library that has evolved to P1371, proposed for C++23.
Stars: ✭ 479 (+941.3%)
Mutual labels:  pattern-matching
pattern-matching-with-typescript
TypeScript does not have any pattern matching functionality built in. This article shows several ways how you can replicate the core of a simple pattern matcher using a few simple structures and functions within TypeScript. Resulting code will have improved maintainability and better runtime type safety when done right.
Stars: ✭ 70 (+52.17%)
Mutual labels:  pattern-matching
Egison
The Egison Programming Language
Stars: ✭ 800 (+1639.13%)
Mutual labels:  pattern-matching
strings
String helper methods and an inflector
Stars: ✭ 31 (-32.61%)
Mutual labels:  pattern-matching
Whyhaskellmatters
In this article I try to explain why Haskell keeps being such an important language by presenting some of its most important and distinguishing features and detailing them with working code examples. The presentation aims to be self-contained and does not require any previous knowledge of the language.
Stars: ✭ 418 (+808.7%)
Mutual labels:  pattern-matching
Ingraph
Incremental view maintenance for openCypher graph queries.
Stars: ✭ 40 (-13.04%)
Mutual labels:  pattern-matching
Pcre Ocaml
OCaml bindings to PCRE (Perl Compatibility Regular Expressions)
Stars: ✭ 23 (-50%)
Mutual labels:  pattern-matching
Pampy.js
Pampy.js: Pattern Matching for JavaScript
Stars: ✭ 544 (+1082.61%)
Mutual labels:  pattern-matching

Espresso

Build Status

Expression transformation package.

Symbolic manipulation

Espresso provides functions for finding, matching, substituting and rewriting Julia AST. A few examples:

Match power expression and extract its first argument

pat = :(_x ^ 2)  # anything starting with `_` is a placeholder, placeholder matches everything
ex = :(A ^ 2)
matchex(pat, ex)
# ==> Dict{Symbol,Any} with 1 entry:
# ==>   :_x => :A    -- placeholder _x captured symbol :A

Find all function calls with any number of arguments:

pat = :(_f(_a...))    # `_a...` will match 0 or more arguments
ex = quote
    x = foo(3, 5)
    y = bar(x)
    z = baz(y)
end

findex(pat, ex)
# ==> 3-element Array{Any,1}:
# ==>  :(foo(3, 5))
# ==> :(bar(x))   
# ==>  :(baz(y)) 

Substitute symbol y with quux(x):

ex = :(z = 2x + y)
subs(ex, Dict(:y => :(quux(x))))
# ==> :(z = 2x + quux(x))

Rewrite all function calls with corresponding broadcasting:

ex = :(z = foo(x) + bar(y))     # take this expression
pat = :(_f(_a...))              # match recursively to this pattern
rpat = :(_f.(_a...))             # and rewrite to this pattern
rewrite_all(ex, pat, rpat)
# ==> :(z = (+).(foo.(x), bar.(y)))

See rewrite.jl for more expression transformation functions and their parameters.

Expression graph

Sometimes we need more sophisticated transformations including those depending on argument types. Espresso can parse expressions into a graph of basic calls and assignments using ExGraph type, e.g.:

ex = :(z = x ^ 2 * (y + x ^ 2))
g = ExGraph(ex; x=3.0, y=2.0);     # `x` and `y` are example values from which ExGraphs learns types of these vars
evaluate!(g)                       # evaluate all expressions to fill values of intermediate nodes
g
# ==> ExGraph
# ==>   ExNode{input}(x = x | 3.0)
# ==>   ExNode{input}(y = y | 2.0)
# ==>   ExNode{constant}(tmp390 = 2 | 2)
# ==>   ExNode{call}(tmp391 = x ^ tmp390 | 9.0)
# ==>   ExNode{constant}(tmp392 = 2 | 2)
# ==>   ExNode{call}(tmp393 = x ^ tmp392 | 9.0)
# ==>   ExNode{call}(tmp394 = y + tmp393 | 11.0)
# ==>   ExNode{call}(z = tmp391 * tmp394 | 99.0)

Such representation, although somewhat cryptic, is more flexible. For example, using it we can easily get rid of common subexpressions (x ^ 2):

g2 = eliminate_common(g)
# ==> ExGraph
# ==>   ExNode{input}(x = x | 3.0)
# ==>   ExNode{input}(y = y | 2.0)
# ==>   ExNode{constant}(tmp390 = 2 | 2)
# ==>   ExNode{call}(tmp391 = x ^ tmp390 | 9.0)
# ==>   ExNode{call}(tmp394 = y + tmp391 | 11.0)
# ==>   ExNode{call}(z = tmp391 * tmp394 | 99.0)

to_expr and to_expr_kw construct a Julia expression back from ExGraph:

to_expr_kw(g2)
# ==> quote    
# ==>     tmp390 = 2
# ==>     tmp391 = x ^ tmp390
# ==>     tmp394 = y + tmp391
# ==>     z = tmp391 * tmp394
# ==> end

(Somewhat outdated) documentation

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].