IO-System

David Sabel DavidSabel@web.de
Tue, 1 Oct 2002 17:47:43 +0200


> > I'm, analysing the IO-system of the GHC and need more
> > information about
> > it.
> > More precisely: How switches the run-time-system between (normal)
> > evaluation and performing of IO-actions?
> >
> > I read the rts-paper, but didn't find any information about this.
> >
> > Can somebody give a short review, or are ther any other
> > papers available?
>
> The runtime system mostly has no concept of IO vs. non-IO evaluation.
> The IO monad is implemented as the following type:
>
>   newtype IO a = IO (State# RealWorld ->
>    (# State# RealWorld, a #)
>
> which means that an IO action is simply a function from the world state
> to a pair of a new world state and a value.  In other words, it's a
> straightfoward state monad, except that we use some optimised
> representations to eliminate some of the overhead of passing the state
> around.
>
> Cheers,
> Simon

In the GHC users guide Chapter 7.2.12 is the single primitve value
realWorld# of the state of the world provided.
This value is also used in the implementation of unsafePerformIO
(GHC.IOBase), where it is applied as the argument
to the IO action, and so on (after performing the action it's discarded).

My question is now: Is the same value (realWorld#) applied while using
normal monadic - not unsafe - IO, more precisely
is a programm main::IO () executing the expression (main realWorld#)?

David