[Haskell-cafe] IO Exceptions through monad transformers stack

Nicola Gigante nicola.gigante at gmail.com
Tue Oct 7 21:25:20 UTC 2014


Hi,

I’m not sure if this is the right list for “base” questions like this,
if there are any more specific lists please let me know.

I’m writing a parser and it works in a stack of monad transformers
defined in this way:

type Parser = StateT ParserState (ExceptT ParserError IO)

The base monad is IO because the parse could have the need to load
other files included by the file that’s being parsed.

The ExceptT transformer make it easy for the lexer and the parser
to signal errors. At the end I have a runParser function that compose
runExceptT and evalStateT and provides me with a nice Either that
contains the parsed result or a value of type ParserError.

The ParserError data type is the following:

data ParserError = LexingError <fields> | ParsingError <fields>

Currently, whenever the parser encounter an error I simply do
throwError $ ParsingError something…, for example.

What I would like to do is to report in this way also the possible
IO errors that can occur, by adding a IOError constructor to the
datatype above and having runParser return such a value if
any operation on the underlying IO monad throws an exception.

How can I do something like that? The root of the problem is that
I still have to grasp how the IO exceptions relate to all the other
ExceptT, ErrorT, MonadError, types and how all the functions like
throwError, catchError, catch, try, and so on work together, in a
stack of transformers.

Any clarification or pointer would be very appreciated

Thank you very much,
Nicola


More information about the Haskell-Cafe mailing list