A common theme in many scientific disciplines is the desire to build up complicated objects out of simpler parts. This compositional perspective allows us to work in a modular fashion, rather than addressing each new problem as a monolith that must be investigated from scratch.
With this in mind, given two monads , it would be very convenient if the composite endofunctor
was also a monad in some canonical way. Unfortunately, this is not always the case. Although this is initially disappointing, the study of when such composites are monads is a rich and interesting subject.
Composing Endofunctors
We begin with some simple concrete examples, to get a sense of the various possibilities when we try to compose two monads as endofunctors. We will say that an endofunctor can be given the structure of a monad, if there exists any
and
satisfying the unit and associativity axioms. We then note the following instructive examples:
- Trivially, for any monad
, both the composites with the identity monad
and
carry the structure of a monad. As an equally trivial corollary of this observation, as there are endofunctors that carry multiple monad structures, this will also be a possibility for composite monads.
- For any monad
, its post composition with the exception monad
carries the structure of a monad.
- The composition of the powerset monad with itself
does not carry the structure of a monad. This was shown by Klin and Salamanca “Iterated Covariant Powerset is not a Monad” in 2018.
This is a pretty mixed picture. There are monads such that composition with their endofunctor on one or both sides results in a monad every time, and at the other extreme, some monads can’t even be composed with themselves.
Distributive Laws
Our aim now is to try and deduce some sufficient conditions under which monads compose. We will consider two monads and
on the same base category. Our notation is a bit fussier than normal, using superscripts on the unit and counit to make it clear which monad is intended.
We would like to find a monad structure on . There is an obvious choice for the unit:
If we trying composing the two multiplications together in the same way, we get:
We now see a problem, we have a natural transformation of type , not the type
which we require. A natural attempt to address this problem would be to introduce some natural transformation
, and form the composite:
Of course, we can’t just use any old natural transformation , it will need to satisfy some axioms in order for the resulting construction to be a monad. It is an interesting exercise to try and deduce these by attempting a proof, which I would highly recommend.
Sufficient conditions are two unit axioms:
and two multiplication axioms:
A natural transformation satisfying these four equations is known as a distributive law. This construction was first discovered by Jon Mock Beck, and they are sometimes referred to as Beck distributive laws for emphasis. Interestingly, the required axioms are exactly that
is both a Kleisli law and an Eilenberg-Moore law, which we encountered previously.
Of course, we haven’t actually demonstrated that such natural transformations can be found, so it’s time for some examples.
Example: For the free monoid (list) monad , and the Abelian group monad
, there is a distributive law:
We can think of the elements of as equivalence classes of products-of-sums, for example:
If we apply the usual distributivity axiom for multiplication over addition, we get the term:
our example term becomes a sum-of-products:
which is an element of . This operation on terms is the idea behind the action of the distributive law, and also inspires the name distributive law. The resulting monad in the free ring monad.
Example: Again for the list and Abelian group monads, there is no distributive law composing in the opposite order, of the type:
This gives us an example of a pair of monads with a distributive law in one order, but not the other.
Example: Similarly to the ring monad example, there is a distributive law between the list and powerset monads:
The action of this distributive is:
As the powerset monad is the free complete join semilattice monad, and list is the free monoid monad, we can view the action of this distributive law algebraically as:
Essentially, this distributive law comes from axioms of the form:
The algebras of the resulting monad are known as quantales.
Example: As a more computational example, consider the exception monad for any object , and and arbitrary monad
. There is a distributive law:
Abstractly, this law has components:
where and
are the coproduct injections. For example, if we take monad
to be the list monad, the distributive law acts on the left component of the coproduct as:
and on the right component:
The next example illustrates that distributive laws are only one aspect of the composition question for monads.
Example: As another negative result, there is no distributive law for the list monad over itself , but
does carry the structure of a monad (as was pointed out by Bartek Klin). So the lack of a distributive law does not spell doom for finding any monad structure at all on the composite endofunctor.
Given the previous example, one might ask what are the advantages of knowing the composite monad arises via a distributive law? The key point is that we then get sensible relationships between the composite and its component parts. There are monad morphisms
and
. These in turn induce functors between the corresponding Kleisli and Eilenberg-Moore categories, as we have seen previously. Furthermore,
lifts to a monad on the Eilenberg-Moore category of
, and
lifts to a monad on the Kleisli category of
.
Conclusion
We have only touched on the topic of distributive laws in this post. Hopefully it’s fairly obvious to see that these ideas will generalise to the 2-categorical and bicategorical definitions of monads we recently discussed. There are results giving sufficient conditions, either for when a distributive laws does exist, or precluding that possibility. There is also a lot of open ground for new results in this area, showing that in some ways our understanding of monads is still rather naive.
For those interested in distributive laws, I would thoroughly recommend Jon Beck’s original 1969 paper, which is a very enjoyable read.
One final note of caution. Distributive laws are a notorious source of difficulty and errors in the literature. If you are working with them, or depending on the results of other, it is worth putting in extra effort to make sure you are on firm ground.
One thought on “Composition and Distributive Laws”