In todays post, we move away from the fancy 2-categorical formal monad theory we have been exploring recently, and commence a new topic. Beginning in a fairly elementary manner, we consider a generalisation of the notion of monad which comes up frequently in practice; that of relative monads.
Does a monad have to be an endofunctor?
As we have seen many times, a monad consists of an endofunctor , along with unit and multiplication natural transformations satisfying some equations. These equations say that this structure is a monoid in the endofunctor category
, with respect to the composition monoidal structure.
We will now consider how we can generalise the notion of monad so that can be an arbitrary functor rather than just an endofunctor. Lets explore how we might try and make this work.
Firstly, we fix an arbitrary functor . As a first step to viewing
as the functor component of a monad, we would like a suitable generalisation of unit for this structure. For an ordinary monad, this would be a natural transformation:
Unfortunately, this wont type check now that has more general type, as natural transformations must go between a pair of functors of the same type. To fix this problem, we shall introduce a second functor
, and take our unit to be of type:
The functor is an extra parameter that didn’t appear in the definition of a conventional monad, so we are heading towards a definition that is relative to this choice of functor
. So maybe we’re looking for monoids in the functor category
?
To see if this is the case, we also need to consider the type of the multiplication natural transformation. For an ordinary monad, this has type:
.
Here we hit on a harder problem than before, as we cannot compose with itself, as the domain and codomain don’t match now we’ve removed the restriction to working with endofunctors. What to do?
We seem to have reached a bit of a dead end, so lets try addressing the same problem from a different direction. We have seen an alternative definition of monads before, using the extension or Kleisli form. Recall that this consists of three pieces of data:
- A function of type
.
- An
-indexed family of unit morphisms
.
- A family of extension operations on morphisms
, indexed by pairs of
objects.
We also require the following three axioms to hold:
.
.
.
Using what we learned from our first attempt to generalise to more general functors, we are going to try and adjust this definition relative to an endofunctor . So from our previous analysis, we will need a function
and a family of unit maps
.
It remains to generalise the extension operations. Unlike with the definition of monad multiplication, the type of the extension operations for a monad do not involve any composites of the form , so life is more straightforward. We simply adjust the types as follows:
.
The three equations above still type check, and so it is natural to require that they hold. This gives us our definition of -relative monad, as a triple consisting of:
- A function
.
- An
-indexed family of unit morphisms
.
- A family of extension operations on morphisms
, indexed by pairs of
objects.
These should satisfy the three equations above. As a first check that this definition is reasonable, we note the following implications.
- The function
extends to a functor of type
with action
.
- Now
is a functor, we can show that the family of unit morphism form a natural transformation
.
- The extension operations
are natural in both variables. That is:
.
As further evidence that this definition is reasonable, it is a legitimate generalisation of ordinary monads.
Example: A -relative monad is the same thing as an ordinary monad.
We also have the following source of many additional relative monads.
Example: Given an ordinary monad on category
, and a functor
, we get a
-relative monad with object map
,
unit morphisms
.
The extension maps for the ordinary monad restrict to act on morphisms with codomains of the form
for our relative monad.
The previous examples are often referred to as trivial, as they are built from ordinary monads. This underplays their importance, as in many cases it can be more natural to work with data in the form of a relative monad, rather than an ordinary monad, or to explore the relationship between the two. We will explore examples of this type in more detail in later posts, along with more general examples.
Conclusion
We have introduced a generalisation of the notion of monad, removing the restriction to endofunctors. I have to admit that when I initially encountered this definition, my first instinct was that the whole thing looked rather arbitrary, and a bit of a hack. I think this knee jerk opinion was wrong, and that actually relative monads are a natural and important concept. This revised opinion is based on the following evidence:
- Relative monads occur naturally in practice, and also yield a starting point for further useful generalisation.
- It is possible to develop a useful body of theory at the level of relative monads, so the abstraction is sufficiently powerful to do useful mathematical work.
- Even when other formulations are possible, it is often convenient to present certain mathematical constructions in the form of relative monads, making them a practical mathematical tool.
We will see examples illustrating the claims above in future posts.
Another instructive aspect of relative monads is how we benefit from having multiple formulations of the same mathematical abstraction. In this case, it turned out to be more convenient to generalise the extension or Kleisli form of a monad. By contrast, the monoid formulation, often preferred by mathematicians, proved more challenging. We will return to the question of whether relative monads can be seen as monoids in a later post.
Further reading: The standard starting point for relative monads is probably the paper “Monads need not be endofunctors” by Altenkirch, Chapman and Uustalu. It is worth finding the longer journal version if possible, as this contains more details and proofs than the shorter conference paper that typically crops up first on internet searches. For those wanting more theory and recent developments, I’d recommend reading the PhD thesis of Nathanael Arkor, which also contains a very enjoyable introduction to the topic.
Acknowledgements: I am grateful to Zhixuan Yang who identified a confusing mathematical typo in an earlier version of this post.