simplification
Basic algebraic simplification.
This module is a standard library of IR-agnostic rewrite rules. They apply to
any Finch IR whose call and literal nodes implement the CallTerm and
LiteralTerm interfaces, which today means FinchLogic (MapJoin),
FinchNotation (Call), and FinchAssembly (Call).
annihilate(node)
¶
f(a..., z, b...) => z when z is an annihilator for f.
Applied over every dtype, floats included, so nan * 0 and inf * 0 fold to
0 rather than to nan.
TODO: add a safe mode for nan
Source code in src/finch/symbolic/simplification.py
canonicalize_associative(node)
¶
Unwraps singleton n-ary function calls.
- f(x) => x
An n-ary f absorbs its immediate f-children and moves literals to the
front, next to each other:
- f(a..., f(b...), c...) => f(k..., rest...).
A fixed-arity f is instead rotated one step at a time so that literals
bubble up and to the left, where adjacent ones merge:
- f(k1, f(k2, y)) => f(f(k1, k2), y)
- f(f(k, x), y) => f(k, f(x, y))
- f(x, f(k, y)) => f(k, f(x, y))
- f(x, k) => f(k, x)
Source code in src/finch/symbolic/simplification.py
dedup_idempotent(node)
¶
f(a..., x, b..., x, c...) => f(a..., x, b..., c...) for idempotent f.
Source code in src/finch/symbolic/simplification.py
drop_identities(node)
¶
f(a..., e, b...) => f(a..., b...) when e is an identity for f, and
dropping it doesn't change the type of the result.
Source code in src/finch/symbolic/simplification.py
fold_literals(node)
¶
Evaluate literal arguments at compile time.
f(x, y)=>literal(f(x, y))when every argument is a literal. The call disappears, so this is sound for any operator.f(a..., x, y, b...)=>f(a..., f(x, y), b...)folds adjacent literal pairs of an associative n-aryf.