[Haskell-cafe] operating on nested monads

Tom Pledger tpledger at ihug.co.nz
Sat Mar 27 20:35:08 EST 2004


Marco Righele wrote:

>Hello everyone,
>
>I have some operations that have to be done in sequence, with
>each one having the result of the previous as input.
>They can fail, so they have signature 
>a -> Maybe b
>Checking for error can be quite tedious so I use monadic operations:
>
>f :: a -> Maybe b
>do	y <- foo x
>	z <- boo y
>	moo z
>
>The problems arise when I try to do the same thing within the IO Monad,
>i.e. the functions have signature
>a->IO (Maybe b)
>
>How can I achieve the same effect (if it is ever possible)?
>I feel like it should be something almost trivial, but I really can't get it.
>

Hi.

How about giving your functions the type

    a -> IO b

and representing failure with either 'fail', 'ioError' or 'throwError'? 
They propagate the same way as Nothing in the Maybe monad.

http://www.haskell.org/ghc/docs/latest/html/libraries/base/System.IO.Error.html#6
http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control.Monad.Error.html

Regards,
Tom




More information about the Haskell-Cafe mailing list