Where Do Monads Come From?
Posted by Tom Leinster
If you know some category theory, you probably know that every functor with a left adjoint induces a monad. But much less well known — and undeservedly so — is that you don’t need your functor to have an adjoint in order for it to induce a monad! Even a functor without a left adjoint induces a monad, just as long as certain limits exist.
This is called the codensity monad of the functor. I’ve just arXived a paper about them (and ultrafilters, which I won’t get to today). This is the first of a short series of posts explaining what’s in my paper.
Today I’ll explain what a codensity monad is.
Here’s the grammar of it: a functor may or may not have a codensity monad. If it does, it’s a monad on , and I’ll call it . In the event that has a left adjoint, , the codensity monad does exist, and it’s simply .
I’ll state the definition of codensity monad in four ways, using varying amounts of categorical vocabulary.
1. The slickest definition: the codensity monad of is the right Kan extension of along itself. (This may or may not exist.) The monad structure comes from the universal property of Kan extensions.
2. Slightly more explicitly, the codensity monad can be defined as an end: for ,
Here is a power in : that is, it’s the product of lots of copies of the object , one for each element of the set . The end might or might not exist.
3. If you’re not comfortable with ends, maybe you’ll prefer the same formula expressed as a limit. Let . Let be the comma category whose objects are maps (or more exactly, pairs where and ). There’s a functor sending this object to , and is defined to be its limit (if it exists).
4. Here’s a fourth and final way to say it. For this, let’s assume that is small and has small limits. Then, à la Kan, the functor induces an adjunction
where I’m calling both functors , though they’re not the same. The left adjoint is defined by . I’ll skip the description of the right adjoint — see the paper if you want to know. Of course, the right adjoint is determined by the fact that it’s a right adjoint: explicitly,
whenever and . Anyway, being an adjunction, it induces a monad, and this is the codensity monad of .
Example Suppose has a left adjoint, . Using any of the descriptions above, you can show that the codensity monad is just the ordinary monad (Proposition 6.1 of my paper, but known for donkeys’ years).
Example Every set has a monoid of endomorphisms , and an action of a monoid on can be described as a map of monoids. Modules over rings, representations of groups, algebras for operads, etc., can all be described in a similar way.
What seems less well known is that algebras for monads can also be described in this way. Let be an object of a category . Under mild hypotheses, has an endomorphism monad , whose crucial feature is that for any other monad on , an -algebra structure on can be described as a map of monads. It is, in fact, the codensity monad of the functor that picks out the single object .
Explicitly, this monad sends an object of to the power . (That’s a special case of the end or limit formula above.) Why is it the case that an -algebra structure on amounts to a monad map ? Keep reading to find out.
Example Here’s something a bit more substantial. Take the category of commutative rings, its full subcategory , and the inclusion functor .
There’s probably no canonical way of turning a ring into a field, and as a matter of fact, doesn’t have a left adjoint. Why not? Here’s one of many possible arguments. The category has an initial object, , and any left adjoint would send to an initial object of . But has no initial object, since there are no homomorphisms between fields of different characteristics.
So, we don’t get an induced monad on by the standard monad-from-adjunction method.
But we do get a codensity monad, . It does its best to turn rings into fields. What it actually does is turn a ring into a product of fields: for a ring ,
where is the set of prime ideals of and is the field of fractions of the quotient ring .
You might wonder whether the unit map embeds our original ring into a product of fields (in other words, whether it’s injective). An obvious necessary condition is that contains no nilpotent elements other than zero. In fact, some basic commutative algebra implies that this is also a sufficient condition (Example 5.1 of my paper).
For instance,
which is the product of one copy each of the prime fields. (It contains as a subring.) Also, for ,
You can rewrite this as
where is the radical of . If you’ve been reading about the ABC conjecture recently, you’ll have met the radical: it’s the product of the distinct prime factors of .
I want to finish by convincing you that codensity monads really do deserve to be thought of as substitutes for monads induced by adjunctions, usable in situations where no adjoint exists.
The first example above was one piece of evidence for this point of view: that if does have a left adjoint, then the codensity monad is the same thing as the monad induced by the adjunction. So, the two constructions agree where they’re both defined, but codensity monads are defined more often.
Here’s some stronger evidence that we’re doing the categorically right thing. Take, as before, a functor , and suppose that the codensity monad exists. Then it’s a theorem that given any monad on , there’s a natural one-to-one correspondence between:
- maps of monads , and
- functors over .
Here “over ” refers to the fact that both and come equipped with functors into (namely, and the forgetful functor); the functor is required to make the obvious triangle commute. If you want a proof, you can find this as Proposition 6.2 of my paper.
(Back in the example of endomorphism monads, maybe you wondered why maps were the same thing as -algebra structures on . It’s because of the result just mentioned: just take to be the functor picking out the object .)
So: the process of constructing codensity monads is adjoint to the process of constructing the category of algebras for a monad. This can be put precisely in a couple of ways, which you can find in Section 6 of my paper. I’ll just mention one.
Fix a category . Not every functor into has a codensity monad, and certainly not every functor into is monadic. Write:
- for the slice category of over
- for the full subcategory consisting of those functors that have a codensity monad
- for the full subcategory of monadic functors.
These are successively smaller, since any monadic functor has a left adjoint and therefore (by our very first example) a codensity monad. So we have
The theorem now is that it’s a reflective subcategory. That is, the inclusion functor has a left adjoint. The adjoint takes a functor and turns it into the forgetful functor .
In other words, the canonical way to force a functor to be monadic is to first take its codensity monad, then form the corresponding monadic functor.
In future installments: ultrafilters, ultraproducts, and: what could it sensibly mean for a vector space to be compact?
Re: Where Do Monads Come From?
This has actually been somewhat common knowledge for a bit now amongst people who are interested in both category theory and Haskell. There we have a
Monad
type class:that identifies monads over the category Hask, which has Haskell types as objects and Haskell functions as arrows, and is enriched in itself (or, something along those lines). Quantifiers enforce (di)naturality conditions, so one can define:
Which is the above end (and always exists). And it was known that a
Monad
definition could be given without knowing anything about f (it is necessarily Hask-valued, but we don’t care what kind of weird category with Hask’s objects we have to make up to make f actually a functor).I have, of course, never seen anything on the more rigorous category theory side about this, so I was never sure how flukey it was (the above is kind of like saying that for all Set-valued functors that come from a category with Set’s objects but not necessarily its morphisms, the above end exists and is a monad on Set). It’s interesting to see it generalized.
We also of course have (made up syntax for ease of presentation):
for the dual, which always has an implementation of a Comonad class.
(Also, this fact was known even earlier for a continuation passing style monad:
f is irrelevant to the
Monad
definition forContT f r
.)