question about RealWorld and realWorld#

Simon Marlow simonmar@microsoft.com
Wed, 31 Jul 2002 10:52:09 +0100


> Am I right in saying that the type constructor=20
> RealWorld is special to the compiler in that it is not defined in=20
> 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=20
> similar in=20
> that it isn't defined in any Haskell code, but is primitive=20
> to the compiler?

Yes.

> I would have expected to find something like:
>=20
>    realWorld# :: State# s
>    realWorld# =3D 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=20
> specially, so that
> they only live at compile time but are not present at=20
> runtime. This is mentioned
> in the "Lazy functional state threads" paper:
>=20
>    "By the time the program reaches the code generator, the=20
> role of the state
>     values is over, and the code generator arranges to=20
> generate no code at all
>     to move around values of type State#" (page 13 in my=20
> 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