
Am I right in saying that the type constructor RealWorld is special to the compiler in that it is not defined in any Haskell code but wired in?
Yes, it is a primitive type, albeit a very special one. RealWorld is only used to instantiate the type parameter of the State# type constructor (another primitive type) in the IO monad and in runST.
Am I also correct is saying that the identifier realWorld# is similar in that it isn't defined in any Haskell code, but is primitive to the compiler?
Yes.
I would have expected to find something like:
realWorld# :: State# s realWorld# = undefined
There isn't really a way to define the value of realWorld# in Haskell. Think of realWorld# as a constant of type State# RealWorld.
My instinctive feeling is that GHC treats State# objects specially, so that they only live at compile time but are not present at runtime. This is mentioned in the "Lazy functional state threads" paper:
"By the time the program reaches the code generator, the role of the state values is over, and the code generator arranges to generate no code at all to move around values of type State#" (page 13 in my version of the paper).
They are "almost invisible" at runtime. For example, there must still be a difference between a function of type 'State# s -> a' and a value of type 'a', although the argument doesn't have a value and doesn't use any registers. HTH, Simon
participants (1)
-
Simon Marlow