We have a filter, that we want to mechanically prepend conditions to, and we don't want the filter to just get bigger every time we do it. One approach is a function to reduce a filter down to its minimal form (while maintaining correctness).
this function optimize
, reduces the ast by propagating constraints over variables
to the other side of the &&
operator, i.e while evaluating the
expression to the right of the &&
we can know that the expression to
the left is necessarily true.
We also know that (in the absence of side-effects), A && B
is
equivalent to B && A
, so we optimize it both ways and choose the shallowest result.
e.g.
x != 6 && x == 6 // given that `x != 6`, reduce `x == 6` to false
x != 6 && false // x && false -> false
false // fully reduced