[Haskell-beginners] Re: Understanding Monadic I/O

Heinrich Apfelmus apfelmus at quantentunnel.de
Thu Jan 14 04:36:14 EST 2010


Markus Böhm wrote:
> Hi, I'd like to understand in principle, how monadic I/O actions and
> combinators (>>=) are translated into imperative C--, to be executed
> sequentially.
> 
> Does sequencing of IO actions mean nesting of C-- functions and
> passing values/state via additional function parameters?
>
> Is there any material with examples available?
> 
> P.S.: Just to understand the magic better. I need it.

Well, the key point of the IO monad is of course that the internals are
abstracted away. I'm not sure you will get much understanding out of them.


If I am informed correctly, GHC represents IO as

   newtype IO a = IO ( World -> (# a, World #) )

But  World  is just a dummy type, it models the execution order as data
dependencies so that the optimizer doesn't mess with it. In other word,
 World  is stripped out completely, primitives like  putChar  are pretty
much just compiled down to foreign function calls in C--.

There are other possibilities, I believe NHC and YHC represent IO as

   newtype IO a = IO ( World -> a )

Again, the  World  argument is just a dummy.


Regards,
Heinrich Apfelmus

--
http://apfelmus.nfshost.com



More information about the Beginners mailing list