Monday, August 1, 2011

Corrected- IO Monad

This is a correction to the addendum of the previous post, where I claimed that Clean's way of handling IO was flat-out better than Haskell's. A quote from William Tyson (to a mailing list):


At first Haskell did try world passing.  Monads hide the world passing.  Compare the following echo procedures which reads a line and prints it character by character.

== Clean Echo ==

echo :: *World -> *World
echo world
| c = '\n'      = world2
               = echo world2
                       where
                       (c, world1) =: getChar world
                       world2 =: putChar c world1

== Haskell Echo ==

echo :: IO ()
echo = do
       c <- getChar
       putChar c
       unless (c == '\n') echo

==

In Clean, each IO operation gives you a new world which you need to explicitly pass around.  Uniqueness typing ensures that you don't use the same world twice.  In Haskell, the world is implicit.
In other words, the monad is making the syntax nicer, but basically the same thing is going on.

2 comments:

  1. Have a look at an actually fresh approach to effects in http://math.andrej.com/2010/09/28/how-eff-handles-built-in-effects/

    ReplyDelete
  2. Interesting. I can't say I "get it" yet (meaning, place it in the connected web of design-space in a way I understand it), but their concept of effect does look useful from their examples.

    ReplyDelete