Yet Another Monad Tutorial

Ross Paterson
Wed, 13 Aug 2003 10:27:07 +0100

On Tue, Aug 12, 2003 at 03:04:39PM -0400, Antony Courtney wrote:
> So then, in your view, what *is* an IO action?
> One conceptual model is that an IO action with type (IO a) denotes a 
> function of type World -> (World,a).

But the IO monad is not a state monad, because other agents may change
the state of the world while a Haskell program is running.

It's more accurate to say that the program returns an action to be
executed by the environment:

	data IO a = Return a | Invoke (SysCall (IO a))
	data SysCall r
		= GetChar (Char -> r)
		| PutChar Char r
		| ...

In addition to the usual arguments, the system call is given a return
continuation to enter on completion.  (This was the model of interactive
I/O used by Moggi in "An Abstract View of Programming Languages", and
is also used by Fudgets.)  Of course the interpretation the environment
places on GetChar, etc is outside of the functional world.

That simple model omits exceptions.  One way of adding them is

	data IO a
		= Return a
		| Error IOError
		| Invoke (SysCall (IO a)) (IOError -> IO a)