In this post, we introduce the continuation monad. This monad is slightly less well-behaved that some of the previous examples we have encountered, and a source of counterexamples to naive conjectures. Along the way, we will touch on some previous topics, such as monad morphisms, Kleisli and Eilenberg-Moore categories, and commutative monads.
The Monad Construction
We begin by establishing a relatively simple, but important form of adjunction. We do this at a greater level of generality than some of our earlier posts, to highlight the fairly minimal requirements. This adjunction then yields our monad in the usual way.
For any symmetric monoidal category , the monoidal closure means that for every object
, there is an adjunction:
For a fixed object , the functor
is contravariant. That is, we can view it as having type
or
as is convenient to us.
Using the definitions, closure adjunction and symmetry, we have a series of natural bijections between the following homsets, involving the contravariant functor :
- Morphisms
in
- Morphisms
in
- Morphisms
in
- Morphisms
in
- Morphisms
These natural bijections establish that there is an adjunction:
and therefore a monad on :
where and
are the unit and counit of the adjunction. This monad is known as the continuation monad.
The Continuation Monad on Sets
To get a handle on what is going on, lets consider the special case where is the Cartesian closed category of sets and functions. The unit of the adjunction is then:
and the counit is the same map, but in . The multiplication of the monad at
is:
In the above, we’ve used lambda notation for inline functions, and added selected type annotations to emphasise how the input data is being used. Notice no copying or reuse of arguments takes place. This specific example lives in a Cartesian closed category, but the construction is inherently symmetric monoidal in nature, and therefore linear in its use of its arguments.
Kleisli Structure
A Kleisli morphism of type is a morphism in the underlying category of type
. In the case of
, this is a function:
From a computer science perspective, we can think of an element as a continuation. This is how a computation should continue once it has computed a value of type
, eventually yielding a result of type
. An element
is a computation yielding a
, that requires a continuation as a parameter, telling it how to continue to yield final result of type
.
Eilenberg-Moore Algebras
The Eilenberg-Moore algebras can be difficult to describe in the general case. We focus attention on a particular result. Consider the two element set . The functor
is the contravariant powerset functor. An element
can be seen a characteristic function, mapping elements appearing in a subset to
, and the others to
. This means that it has a left adjoint, as we already know, and their is an equivalent between the category of Eilenberg-Moore algebras, and
. Therefore, in this case:
It is well known (in the right circles!) that is equivalent to the category
of complete atomic Boolean algebras and morphisms preserving their joins and meets.
Remark: Notice there is a pronounced different here to considering complete Boolean algebras and their usual morphisms preserving the algebraic structure. In this case it is well known the forgetful functor to does not have a left adjoint, as the free complete Boolean algebra on three generators does not exist. This is an instructive counterexample to the naive intuition that these algebraic constructions always “just work out”. Once you move beyond collection of operations of a fixed cardinality bound, you need to be much more careful.
A topos is a category rather similar to the category of sets and function, and is probably the simplest example. Toposes are Cartesian closed, and have an object
known as a subobject classifier, which can be thought of as a collection of truth values. The functor
is the analog of the contravariant powerset functor on
, and again is monadic, so for any topos
, we have a wide generalisation of the equivalence above:
This result is important for establishing finite completeness of toposes, via their finite completeness, and theory of Eilenberg-Moore categories.
Monad Morphisms and Algebras
We now turn to an interesting property of the continuation monad, that can be thought of as a very general representation result.
Consider a term in the algebraic theory of monoids, for example:
where come from some set
of variables. How can we evaluate this term in a particular monoid, say
?
Firstly, we need to know what values to use for each variable, which we could specify via a function . Secondly, we really only need to specify the value for each equivalence class of terms, so the elements of
, where
is the free monoid (list) monad. So a crude first step is we need a function:
Given what we have seen in this post, it seems natural to rearrange this to emphasise the terms are the input, into the form:
Obviously, the way we evaluate terms should not depend on which set we use to name the variables, so we expect this actually to be a family of maps, natural in .
The algebraic structure also suggests that:
- The value for a term that is just a variable should be the value assigned to that variable.
- Evaluation should be compatible with substitution of terms.
These final two conditions correspond to the monad morphism axioms. So we are interested in monad morphisms of type
Obviously, there’s nothing special about our example of the list monad, so we’re really interested in monad morphisms of the form:
It turns out, there is a bijection between:
- Eilenberg-Moore algebra
.
- Monad morphisms
For monads, the monad morphism resulting from an Eilenberg-Moore algebra has action:
In the other direction, give a monad morphism, we get an Eilenberg-Moore algebra with action:
In fact, this result goes through in much greater generality, but discussing the technical details would get distracting. Interested readers can refer to Kock’s “On Double Dualization Monads”.
This correspondence between algebras and monad morphisms opens up new possibilities. To see this, as a preliminary step, say that two monad morphisms:
commute if the following equality holds:
where and
are the two double strength morphism. With this in place, we can say that two Eilenberg-Moore algebras
and
commute if their corresponding monad morphisms
commute as defined above. We then say an algebra is commutative if it commutes with itself.
This extends the notion of commutativity we have encountered previously in two directions:
- We consider commutativity for individual algebras, rather than for a monad as a whole.
- We can consider mixed forms of commutativity, for algebras of potentially different monads on the same base object.
We can also relate this form of commutativity with the previous definition. The following are equivalent for a monad .
- Every Eilenberg-Moore algebra is commutative.
- Every monad morphism with domain
commutes with itself.
- The monad
is commutative.
This theorem deepens our understanding of commutativity, allowing us to relate algebra-wise and monad-wise notions.
Conclusion
This post is relatively long. Despite this, we have only very briefly mentioned continuations, which are a fiddly topic in themselves. The continuation monad is an intriguing example of a monad, which can be hard to grasp as first sight. It is what is known as a monad without rank, which limits the scope of the algebraic techniques we have encountered previously.
Further reading: Johnstone’s “Stone Spaces” discusses the complete atomic Boolean algebra duality with , and that the free complete Boolean algebra doesn’t exist. His “Sketches of an Elephant” gives a comprehensive account of topos theory, with the important monadicity theorem which we discussed appearing in the first volume.