Fix a (major) exponential case

Generating formulas in the error analysis was inefficient
in the presence of loops with many branches.  It would explore
paths exponentially because the visited set was split instead
of shared between all branches.

This change also adds a lot more formula caching.