Polymonads

Abstract

From their semantic origins to their use in structuring effectful computations, monads are now also used as a programming pattern to structure code in a number of important scenarios, including for program verification, for information flow tracking, to compute complexity bounds, and for defining precise type-and-effect analyses. However, whilst these examples are inspired by monads they are not strictly speaking monadic but rather something more general. The first contribution of this paper is the definition of a new categorical structure, the polymonad, which explains these notquite-monadic constructions, and subsumes well-known concepts including monads, layered monads, and systems of monads and monad morphisms, among others. We show how to interpret polymonads in the setting of System Fω. While this interpretaion enables several useful programming idioms, programming directly with polymonads in Fω is far from convenient. Addressing this concern, the second contribution of this paper is core ML-like programming language, λPM, with a polymorphic type inference algorithm that infers a principal polymonadic type for a program and elaborates it to Fω in a coherent way—all unambiguous typing derivations produce elaborated programs with the same semantics. The resulting programming style in λPM is powerful and lightweight, as we demonstrate through a series of examples. 1