Hitting a ladder
I believe I have corrected the bug now. Philip Wadler proposed another solution to solve it by changing the optimisation rules to use information of types instead of the expressions themselves. I also tried looking into this solution, but haven't solved it yet.