[Haskell] a newbie question

Tom Pledger tpledger at ihug.co.nz
Mon Apr 26 22:25:26 EDT 2004


Ben_Yu at asc.aon.com wrote:

>Hi there.
>
>I got this question while I'm messing around with my toy monad.
>
>I was thinking about creating a generic monad that can persist state change
>even when fail is called. The StateT monad will discard the state change so
>it makes it hard to add tracing to the program. (at least to me. If there's
>any nice way of doing this, please kindly instruct me.)
>

Hi.

The following uses a writer monad instead of a state monad for tracing, 
but I think the reverse-the-order-of-the-monad-transformers trick works 
with state monads too.

    import Control.Monad.Error
    import Control.Monad.Writer

    test :: (MonadError String m, MonadWriter String m) => m ()
    test = tell "trace" >> throwError "custard"

    errorWrapsWriter :: (Either String (), String)
    errorWrapsWriter = runWriter (runErrorT test)

    writerWrapsError :: Either String ((), String)
    writerWrapsError = runWriterT test

Regards,
Tom




More information about the Haskell mailing list