I've added Add and Mul tags to the Prim datatype for simpler pattern matching. Next: write operations on Code as lifted operations on Ref. Maybe writing as a monad is simpler? Using some lifting: ([Ref a] -> Prim a) -> ([Code a] -> Code a) Conclusion is that the current representation is too cumbersome. The wrapping should be repackaged somehow such that environment threading isn't such a pain.