[Haskell-beginners] combining two monads

Ovidiu Deac ovidiudeac at gmail.com
Mon Aug 22 18:51:18 CEST 2011


Hi,

I wrote some code and I want to refactor it to use the Error monad
combined with IO operations. I found some examples of monad
transformers but I didn't really get them.

So I have a function in IO monad which does several operations which
might fail. These operations work with files so they have to be in IO
monad but on the other hand I would like to use Error monad such that
I can throw errors.

So far I came up with something like this.

data Config = Config { root :: String }

data ParsedData = ParsedData {...}

data MyError = ConfigError | ParseError | OtherError String

instance Error MyError where
    strMsg = OtherError

loadConfig :: String -> Either MyError (IO Config)
loadConfig fn -> ...might throwError ConfigError

parseFile :: String -> Either MyError (IO ParsedData)
parseFile fn -> .. might throwError ParseError

main -> do
   config <- loadConfig "mycfg.cfg"
   parsedData <- parseFile (root config)
   ...

Can somebody please enlighten me about how to apply monad transformers
in this situation?

Thanks,
Ovidiu



More information about the Beginners mailing list