[Haskell-beginners] Mixing monads?

Stephen Tetley stephen.tetley at gmail.com
Sun Mar 28 13:03:04 EDT 2010

Hi Ron

Generally you would want a monad transformer for combining the effects
of of two monads - one monad remains as it is, the other is modified
slightly to make it a "transformer" rather than a regular monad.

It looks like Attoparsec doesn't supply a transformer instance so that
obliges you to make CBC a transformer instead - prosaically this means
making CBC an instance of monad transformer class Trans and extending
the type of CBC to be something like "CbcT m a" rather than "CBC a" so
the monad it transforms can be supplied as a parameter "m".

As Attoparsec isn't a transformer you will have to "lift" Attoparsec
operations into the CbcT monad so func will look something like this -
note the change on the second line:

func = do
   x <- lift $ word128
   let r = do
       y <- decryptCBC x
       if (predicate y)
           then return bar
           else -- somehow read more from the parser?
   return r

Best wishes


More information about the Beginners mailing list