Often the simplest way to form a combination of two objects in category theory is to take their coproduct. In this post, we are going to investigate coproduct of monads, beginning as usual from an algebraic perspective.
Sums of Theories
We begin by considering a pair of -monads given by equational presentations. We shall investigate how a suitable method of combining those presentations yields a corresponding operation on monads.
Given two equational presentations, and
, we can define a new equational presentation with:
- Operations the disjoint union
.
- Equations given by the union
.
We shall refer to this as the sum of the two component theories.
This combines the two equational theories such that there is “no interaction between them”. This can be seen by considering the algebras of the resulting theory. An algebra for consists of
- A fixed choice of universe
.
- A
-algebra structure on
.
- A
-algebra structure on
.
The sum of theories induces an operation on the monads induced by these equational presentations. In fact, the resulting monad is the coproduct of
and
.
Coproducts of Free Monads
Forming coproducts of monads via an operation on their algebraic presentations as we have just described has some limitations. In particular:
- To exploit it, we need to identify presentations for our monads of interest.
- We don’t get a description of the monad
directly in terms of the component monads
and
.
- Realistic applications will probably involve categories other that
. We would prefer constructions that generalise smoothly to other settings.
To address some of these problems, we shall begin by examining the simple case of coproducts of free monads. For a pair of signatures and
, the free monads for the corresponding signature functors must satisfy:
This follows immediately from the universal property, or via a simple direct calculation. We then recall that free monads can be constructed using least fixed points, so we have:
We can think of this as building the coproduct in stages. In the first stage, we begin with the set of variables . In subsequent stages, we:
- Apply every operation symbol in
to terms constructed in the previous stage.
- Apply every operation symbol in
to terms constructed in the previous stage.
- Add back in the set of variables
again.
Example: Let and
both contain a single unary operation symbol,
and
respectively. The terms over set of variables
will be:
.
.
- …
This lets us build up the elements of the coproduct of free monads, using the underlying signature functor. This is a step in the right direction, but ideally we would use the monads themselves in the construction. To try and achieve this, we notice that each term consists of layers of operations from one signature, followed by layers of operations from the other signature, eventually applied to variable symbols.
Example: Continuing the previous example, we end up with terms like:
We can see this as a layer of two operations, followed by a layer with one
operation applied to a variable.
Can we build up these layers directly, using the free monads and
? A naive first attempt would be to try the fixed point construction:
If we consider the first few approximants to this fixed point, we get:
.
.
- …
This is not quite what we want. For example, will contain a copy of the variables. So we are going to end up with two many copies of the variables in our fixed point. Intuitively,
muddles together both bare variables and non-trivial terms.
Hopefully this rings a bell. We have previously seen that free monads are ideal monads. Recall that an ideal monad abstracts the idea of “keeping variables separate”. An ideal monad has a subfunctor
such that:
In the case of free monads contains all the guarded terms. These are exactly the terms that are not bare variables. As a second attempt at a fixed point construction, we consider
Unfortunately, this is still not quite right. The first few approximants to this fixed point are:
.
.
- …
The problem now is that this construction will build up some terms in more than one way, resulting in redundant duplicate copies.
Example: Continuing our previous example, we can consider as arising either directly as a term in
, or in two steps by applying
to
, by applying
to
.
To address this, we must be more careful to build up terms in alternating layers of operations from each signature, in a unique way. To achieve this explicit alternation, we instead solve a fixed point equation in two variables:
This will produce terms with alternating layers of operations from the two signatures. will contain terms with a
operation on top, and dually for
. We then recover:
Coproducts of Ideal Monads
So far, we have rearranged the construction of a coproduct of free monads into language which involves fixed points. Has all this reshuffling got us anywhere?
Well we could consider applying the same construction to arbitrary ideal monads. Firstly, we calculate the fixed point:
Assuming the required fixed points exist, we can then form the coproduct as:
It is worth considering what this says. Firstly, from the algebraic perspective we’re now building up equivalence classes of terms under provable equality, not just raw terms. The intuitive reason that this construction works is that the layers cannot interact. We can prove equalities within a layer, but once a layer of terms from the other theory is inserted, this layer is insulated from interacting with anything else. Hence, we can build up the equivalence classes of , not just the underlying terms, inductively in layers, which is rather a pleasing outcome.
In fact, this is true for arbitrary categories and ideal monads, as long as the required fixed points exist. For example, we can also apply this result to the completely iterative monads that we saw previously.
Summing Up
The ideas in this post mainly follows Ghani and Uustalu’s “Coproducts of Ideal Monads”, which is strongly recommended for further reading. There are other explicit constructions for coproducts of comonads, for example in the case of a free monad and an arbitrary monad, as described in Hyland, Plotkin and Power’s “Combining Effects: Sum and Tensor”. “Coproducts of Monads on Set” by Adámek, Milius, Bowler and Levy gives a detailed analysis in the case of -monads. We should also point out that even in the case of
-monads, coproducts of pairs of monads do not always exist.