Explore alternatives to the state hack
The state hack (-fstate-hack
, on by default, see Note [The state-transformer hack]
in G.C.O.Arity) is a smart hack without which efficient compilation of IO
wouldn't be possible. But fixes such as 69c0e16e made me thinking: Now that we have GHC.Magic.oneShot
, can we get by without?
Suppose we define (in ghc-prim:GHC.Types
):
newtype IO a = IONoEta (State# RealWorld -> (# State# RealWorld, a #))
pattern IO m <- IONoEta m
where
IO m = IONoEta (oneShot m)
Similarly for ST
. I hope that we can get rid of the state hack this way. That would also fix #14596.
Another advantage to this is that we can use IONoEta
on a use by use basis, rather than having the rather unobtrusive {-# OPTIONS_GHC -fno-state-hack #-}
at the top of the module, disconnected from its usage.