That looks like a bug in `piecewise_fold`. What happens is that it rewrote an `Eq` containing a `Piecewise` into a `Piecewise` containing an `Eq`. Probably it should not try to rewrite the `cond` part of a `ExprCondPair` in this way, or it should be more clever about it.