[Paper Review] Exceptionally Monadic Error Handling
This paper introduces a novel monadic structure called 'conjoined monads' that unifies error handling and computation by treating errors as first-class values within a bi-indexed monad. By recognizing that the type and semantics of Haskell's 'catch' operator are a special case of monadic bind, the authors show that computations indexed by both error types and result types naturally form a monad in two ways—offering a uniform, composable foundation for error handling, parser combinators, and exceptions via continuations.
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, the semantics (surprisingly) matches, and this observation has many interesting consequences. For instance, the reader is probably aware that the monadic essence of the (>>=) operator of the error monad $λA.E \lor 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 \lor A$ is, too, a monadic computation that treats still unhandled "errors" as "normal" values and stops when an "error" is finally handled. We show that for an appropriately indexed type of computations such a "conjoined" structure naturally follows from the conventional operational semantics of throw and catch operators. Consequently, we show that this structure uniformly generalizes all conventional monadic error handling mechanisms we are aware of. We also demonstrate several more interesting instances of this structure of which at least bi-indexed monadic parser combinators and conventional exceptions implemented via continuations have immediate practical applications. Finally, we notice that these observations provide surprising perspectives on error handling in general and point to a largely unexplored trail in programming language design space.
Motivation & Objective
- To unify diverse error handling mechanisms in functional programming under a single algebraic structure.
- To demonstrate that the 'catch' operator's type and semantics are a special case of monadic bind, revealing deeper duality in monadic computation.
- To show that bi-indexed monads (indexed by both error and result types) naturally support both pure computation and error handling as first-class monadic operations.
- To provide a foundation for modular, composable, and type-safe error handling that generalizes existing mechanisms like MonadError, ExceptT, and conventional exceptions.
- To advocate for a programming language design that natively supports this structure, avoiding syntactic and compositional pitfalls of current monadic error handling.
Proposed method
- Define a bi-indexed type constructor 'c e a' where 'e' represents error types and 'a' represents result types, and show it forms a monad in both indices.
- Prove that the 'catch' operator has the most general type for monadic error handling by unifying types from operational semantics equations.
- Demonstrate that 'throw' acts as a left zero for bind and 'return' as a left zero for catch, establishing algebraic duality.
- Show that standard monads like 'Either', 'ExceptT', and 'IO' with exceptions are instances of this conjoined monadic structure.
- Construct novel instances such as bi-indexed parser combinators and conventional exceptions via second-rank callCC and the 'ThrowT' transformer.
- Use Scott encoding and continuation-passing style to model conventional exceptions as monadic computations, enabling full composability.
Experimental results
Research questions
- RQ1Can the 'catch' operator in Haskell be formally understood as a special case of monadic bind, and what are the algebraic consequences?
- RQ2What is the most general type for a monadic error-handling operator that satisfies conventional operational semantics?
- RQ3How can monadic error handling be uniformly generalized across different computational effects, including parsing and exceptions?
- RQ4Can conventional exceptions implemented via continuations be expressed as a monadic structure that composes naturally with other monads?
- RQ5What are the implications of this duality for programming language design, particularly in avoiding syntactic and compositional inconsistencies?
Key findings
- The 'catch' operator's type is a special case of monadic bind, and its semantics aligns precisely with the monadic bind operation, revealing a deeper duality.
- The bi-indexed type 'c e a' forms a monad in both the error index 'e' and the result index 'a', enabling dual monadic behavior for normal and error states.
- The 'throw' operator acts as a left zero for bind, and 'return' acts as a left zero for catch, establishing a symmetric algebraic structure.
- The 'ExceptT' monad transformer and 'IO' with exceptions are both instances of the conjoined monad structure, showing broad applicability.
- Parser combinators can be defined as conjoined monads that are precise about error types and reuse standard monadic combinators for error handling.
- Conventional exceptions via 'callCC' can be modeled as a conjoined monad, providing a novel and practical encoding of exceptions as monadic values.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.