Out of curiosity, I wondered what would happen if I made up a State# RealWorld
token and passed it into an IO action. My intuition was that State# RealWorld
is elided in the final program, so none of the IO actions should actually be evaluating it. But when running the below program, I get an error. Why?
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
import Control.Monad.ST
import GHC.Types
import GHC.Prim
main :: IO ()
main = do
let IO f = putStrLn "asdf"
let (# _, a #) = f rw
IO (\s -> (# s, a #))
where
rw :: State# RealWorld
rw = error "real world inspected!"