Combining IO and state monads

Graham Klyne GK@ninebynine.org
Thu, 15 May 2003 21:07:47 +0100


At 15:31 15/05/03 +0200, Tomasz Zielonka wrote:
>You probably want Monad Transformers.
[...]

"Just when you thought it was safe to get back in the water...."

Or, in my case, just when I thought I was beginning to understand how to 
use FP...

This response was very helpful, thank you, even if it has served to show me 
there's still very much more I need to learn.  I've just read through Mark 
Jones' paper [1], and can't claim to fully understand it but I do begin to 
see  the way forward.

It appears that what I want in the near term is to apply "StateT" to 
"IO".  Hopefully, working with that will help to develop my intuitions for 
the more general cases.  (It would help if the GHC libraries were more 
documented, but I guess we can't expect everything on a plate just yet.)

#g
--

[1] Functional Programming with Overloading and Higher-Order Polymorphism, 
Mark P Jones, Advanced School of Functional Programming, 
1995.  http://www.cse.ogi.edu/~mpj/pubs/springschool.html


At 15:31 15/05/03 +0200, Tomasz Zielonka wrote:
>On Thu, May 15, 2003 at 12:14:31PM +0100, Graham Klyne wrote:
> > 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 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?
>
>You probably want Monad Transformers. Both GHC and Hugs provide a
>library of monad transformers for adding state, continuations,
>exceptions, etc. They can be smoothly use with IO monad. Here is an
>example of using state monad transformer.
>
>   import Control.Monad
>   import Control.Monad.Trans
>   import Control.Monad.State
>   import IO (try)
>
>   main :: IO ()
>   main = runStateT m 0 >> return ()
>     where
>       m = do
>           r <- liftIO (try getLine)
>           either
>               (const $ return ())
>               (\l -> do
>                   n <- next
>                   liftIO (putStrLn (show n ++ ": " ++ l))
>                   m)
>               r
>
>       next = do
>           x <- fmap succ get
>           put x
>           return x
>
>Googling for Monad Transformers should give you much info on this topic.
>
>Regards,
>Tom
>
>--
>.signature: Too many levels of symbolic links
>_______________________________________________
>Haskell mailing list
>Haskell@haskell.org
>http://www.haskell.org/mailman/listinfo/haskell

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