FYI, GHC -O2 is the maximum optimization level. All higher numbers are currently treated the same as -O2.
I’d write that as Left $ i + 1.
Also, I’d write:
pure $! if a then Right i else Left $! i + 1
That makes the Core look better (Edit: only for -O1. -O2 does get the good core without that extra $!), but I don’t know if that was actually causing a leak.