Exceptionally Monadic Error Handling

Jan Malakhovski

We notice that the type of catch :: c a -> (e -> c a) -> c a operator is a special case of monadic bind operator (>>=) :: m a -> (a -> m b) -> m b and the semantic (surprisingly) matches.

For instance, the reader is probably aware that the monadic essence of the (>>=) operator of the error monad λA.E ∨ A is to behave like identity monad for "normal" values and to stop on "errors". The unappreciated fact is that handling of said "errors" with a catch operator of the "flipped" "conjoined" error monad λE.E ∨ A is, too, a monadic computation that treats still unhandled "errors" as "normal" values and stops when an "error" is finally handled.

This fact has several immediate practical consequences for monadic parser combinators and similar structures. More importantly, however, it can be generalized to provide a uniform error handling framework for all monadic error handling mechanisms we are aware of. It also provides some surprising perspectives on error handling in general.

Changelog:

Downloads:

See also: