Combining IO and state monads

Graham Klyne gk@ninebynine.org
Thu, 15 May 2003 12:14:31 +0100


I've written and tested a collection of Haskell functions, and now I want 
to put them together into a "proper" program.

The pattern of evaluation that I envisage is a program which maintains a 
workspace, and performs a sequence of read and write operations that 
reference and update this workspace, eventually returning some value based 
on the final state of the workspace.

The workspace would seem to be appropriately manipulated using a form of 
state monad.  And the I/O operations would be performed through an IO 
monad.  What I'm unsure about is the best way to combine these so that the 
real-world state (IO) and workspace state are updated (threaded?) in parallel.

I think I can imagine a solution that defines a new monad consisting of an 
IO paired with a state monad, and then implementing the monadic operators 
and access functions to manage the combined I/O and state updates.  I 
haven't thought through the details, but my intuition is that it might be a 
fair amount of additional code.

I notice the XML toolbox implements a similar idea [1], though in that case 
the state is not a separate monad, just combined with IO in a new monad type.

[1] http://www.fh-wedel.de/~si/HXmlToolbox/hdoc/MonadStateIO.html

I imagine that this is a common requirement, for which there exists an 
appropriately packaged solution.  Is there a standard solution I should 
look to for this kind of functionality?  Or is there some completely 
different approach that I've overlooked?

#g


-------------------
Graham Klyne
<GK@NineByNine.org>
PGP: 0FAA 69FF C083 000B A2E9  A131 01B9 1C7A DBCA CB5E