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
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
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
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
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
108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
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
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
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.
Source code in src/finch/symbolic/simplification.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |