States and exceptions considered as dual effects
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
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.
💡 Research Summary
The paper investigates two of the most pervasive computational effects in modern programming languages—mutable state and exceptions—through the lens of diagrammatic logics and categorical duality. It begins by pointing out that both effects are notoriously non‑algebraic: state manipulates a global store via “lookup” and “update” operations, while exceptions abruptly alter control flow with “throw” and “catch”. Traditional monadic or effect‑system approaches either treat exceptions as a separate monad or require ad‑hoc equations to capture their control‑flow semantics, which obscures the underlying algebraic structure.
The authors propose a unified, categorical perspective. Starting from a monoidal equational logic, they incrementally enrich the logical framework to accommodate each effect. For state, they model the store as an object S and the combined system as a product A × S. The “lookup” operation corresponds to the first projection π₁: A × S → A, and “update” corresponds to the pairing ⟨id_A, u⟩: A → A × S, where u: A → S supplies a new store value. These are precisely the universal arrows associated with products, and they satisfy the usual product equations.
For exceptions, they model the effect as a coproduct E = 1 + X, where 1 represents the normal continuation and X the set of exception values. “Throw” is the right injection inr: X → 1 + X, while “catch” is expressed as a case analysis (a coproduct eliminator) that maps an exception back into the normal flow. Thus, the exception operations are the canonical arrows of a coproduct.
The central technical contribution is a formal duality theorem: product projections (lookup) are categorically dual to coproduct injections (throw), and product insertions (update) are dual to coproduct case analyses (catch). This duality is not merely a superficial analogy; the authors construct explicit diagrammatic translations that preserve equations, showing that the two effects can be treated as mirror images in a single categorical setting.
Having established the duality, the paper explores the combination of state and exceptions. By interpreting both effects within the same monoidal equational logic, they demonstrate that the corresponding operations commute: performing a lookup before a throw yields the same result as throwing before looking up, and similarly for update and catch. This commutation property implies that the combined effect can be represented by a single composite monad rather than a nested monad transformer stack, simplifying both semantics and implementation.
To validate the theory, the authors present a small functional language extended with mutable references and exception handlers. They give concrete examples—such as an increment function that updates a counter and may throw an overflow exception—and show, via categorical diagrams and equational reasoning, that the program’s behavior respects the duality and commutation results. The examples illustrate how the algebraic presentation eliminates the need for special “exception‑propagation” rules that are typical in operational semantics.
Finally, the paper discusses broader implications. By revealing that exceptions are algebraically dual to state, it opens the door to more uniform effect systems, streamlined compiler optimizations, and modular reasoning tools that can handle both effects simultaneously. The authors suggest future work on extending the duality to other effects (e.g., nondeterminism, I/O), integrating the approach into existing type systems, and exploring automated effect inference based on the categorical signatures introduced. In sum, the work provides a deep, mathematically grounded insight that bridges two historically separate areas of effectful programming, offering a cleaner, more compositional foundation for language designers and theoreticians alike.
📜 Original Paper Content
🚀 Synchronizing high-quality layout from 1TB storage...