[Haskell-cafe] Re: Where do I put the seq?

David Menendez dave at zednenem.com
Thu Aug 20 17:23:04 EDT 2009

On Thu, Aug 20, 2009 at 4:41 PM, Peter Verswyvelen<bugfact at gmail.com> wrote:
> But how does GHC implement the RealWorld internally? I guess this can't be
> done using standard Haskell stuff? It feels to me that if I would implement
> it, I would need seq again, or a strict field, or some incrementing "time"
> value that is a strict argument of each of the IO primitives. In any case, I
> would need strictness to control the dependencies no? I might be wrong
> (again) but this is all very interesting ;-)

The RealWorld is just a token that GHC uses to force IO computations
to have the correct data dependencies. If you look at code like
"putChar 'x' >> getChar", there's no obvious data dependency that
would prevent executing getChar before putChar, so internally the IO
monad passes around the RealWorld token to guarantee the ordering.

I don't know the exact details of GHC's IO internals, but I'd expect
putChar 'x' >> getChar to translate into something like this,

    \rw0 -> let ((), rw1) = putChar# 'x' rw0 in getChar# rw1

The important things to note are (1) getChar# depends on the token
returned by putChar#, thus guaranteeing that putChar# gets executed
first, and (2) putChar# and getChar# are impure and cannot normally be
defined in Haskell.

Dave Menendez <dave at zednenem.com>

More information about the Haskell-Cafe mailing list