[Paper Review] States and exceptions considered as dual effects
This paper reveals a deep duality between states and exceptions in programming languages by modeling them through diagrammatic logics, showing that lookup/update operations for states are categorically dual to throw/catch operations for exceptions via the duality of products and coproducts. The key contribution is a formal symmetry that bypasses the non-algebraic challenges of exception handling by isolating the core 'catch' operation from its surrounding control structures.
In this paper we consider the two major computational effects of states and exceptions, from the point of view of diagrammatic logics. We get a surprising result: there exists a symmetry between these two effects, based on the well-known categorical duality between products and coproducts. More precisely, the lookup and update operations for states are respectively dual to the throw and catch operations for exceptions. This symmetry is deeply hidden in the programming languages; in order to unveil it, we start from the monoidal equational logic and we add progressively the logical features which are necessary for dealing with either effect. This approach gives rise to a new point of view on states and exceptions, which bypasses the problems due to the non-algebraicity of handling exceptions.
Motivation & Objective
- To uncover a hidden duality between computational effects of states and exceptions that is obscured in standard programming language design.
- To address the non-algebraic nature of exception handling by isolating the core 'catch' operation from control-flow encapsulation.
- To provide a new formalism based on diagrammatic logics that distinguishes three kinds of programs and two kinds of equations for both effects.
- To demonstrate that the monadic equational logic framework can be extended to reveal symmetries not visible in traditional monad-based or algebraic theory approaches.
- To establish that the handling of exceptions can be understood as a composition of a core 'catch' operation with conditional encapsulation, analogous to state operations.
Proposed method
- The authors use spans of diagrammatic logics to model computational effects, starting from monadic equational logic and progressively adding features for states and exceptions.
- They define three kinds of programs—pure, accessor, and modifier—for both states and exceptions, and distinguish weak and strong equations.
- The duality is formalized by mapping state lookup and update to products, and exception throw and catch to coproducts, exploiting categorical duality.
- Key operations are isolated: 'throw' maps values to exceptions, and 'catch' maps exceptions back to values, forming the core of the duality.
- The approach avoids monads but retains the essence of Moggi’s distinction between values and computations, using a refined semantic framework with decorated syntax.
- Equations relating operations are oriented to reflect operational semantics: left-to-right for Hoare-Floyd state semantics and for exception handling via conditional encapsulation.
Experimental results
Research questions
- RQ1Is there a categorical duality between the fundamental operations of states and exceptions that is obscured in standard programming language semantics?
- RQ2Can the non-algebraic nature of exception handling be resolved by separating the core 'catch' operation from its control-flow encapsulation?
- RQ3How can diagrammatic logics reveal symmetries between computational effects that are hidden in traditional monadic or algebraic treatments?
- RQ4To what extent does the duality between products and coproducts underlie the duality of state and exception effects?
- RQ5Can this duality be generalized to other computational effects or extended to richer language constructs like exponentials or products?
Key findings
- The lookup and update operations for states are categorically dual to the throw and catch operations for exceptions, respectively, via the duality of products and coproducts.
- The core operation in exception handling—mapping an exception to a value—is isolated as 'catch', which is dual to state lookup, while 'throw' is dual to state update.
- The operational semantics of exceptions, traditionally seen as nested conditionals, is shown to arise from encapsulating the core 'catch' operation, revealing its structural duality with state operations.
- The duality is preserved when adding products and coproducts to the language, but not necessarily when distributivity or extensivity is assumed, which breaks the symmetry.
- The framework bypasses the non-algebraicity of exception handling by treating the key 'catch' operation as a primitive, rather than the full 'handle' construct.
- The language with effects for exceptions, Θ_deco,exc, is formally dual to the language for states, Θ_deco,st, and their spans of diagrammatic logics are opposites.
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.