module Cat.Diagram.Monad.Codensity where
Codensity monadsπ
Let be a functor with a left adjoint Some pretty standard abstract nonsense tells us that the composite is a monad in with the unit and multiplication inherited from the adjunction The easiest cases to picture are when is and is the βunderlying setβ functor from an algebraic category (like monoids or groups). Whatβs slightly more interesting is that functors without left adjoints may also induce monads!
This is called the codensity monad of the functor and it exists whenever admits limits indexed by categories the size of When does have a left adjoint, its codensity monad coincides with the ordinary monad-from-an-adjunction construction. Rather than unfolding the necessary limits here, though, we defer to general categorical machinery: right Kan extensions.
The really, really short of it is that the codensity monad of is the right Kan extension of along itself,
module _ (F : Functor A B) (R : Ran F F) where open Ran R private module A = Cat A module B = Cat B module F = Func F
Constructing the monad structure on the functor is a bit involved, but it does serve as a very illustrative application of the universal property of right Kan extensions. Recall that, by definition, if we have a natural transformation (for our choice of functor then this extends to a (unique) natural transformation
For example, the unit map is obtained by extending the identity natural transformation which is implicit witnessing commutativity of the β β triangle below.
unit-nt : Id Fβ F => F unit-nt .Ξ· x = B.id unit-nt .is-natural x y f = B.id-comm-sym
For the multiplication map, observe that we can piece together a natural transformation
using the canonical natural transformation Extending this map, then, gives us a natural transformation
mult-nt : (Ext Fβ Ext) Fβ F => F mult-nt .Ξ· x = eps .Ξ· x B.β Ext.β (eps .Ξ· x) mult-nt .is-natural x y f = (eps .Ξ· y B.β Ext.β (eps .Ξ· y)) B.β Ext.β (Ext.β (F.β f)) β‘β¨ Ext.extendr (eps .is-natural _ _ _) β©β‘ (eps .Ξ· y B.β Ext.Fβ (F.β f)) B.β Ext.β (eps .Ξ· x) β‘β¨ B.pushl (eps .is-natural _ _ _) β©β‘ F.β f B.β eps .Ξ· x B.β Ext.β (eps .Ξ· x) β Codensity : Monad B Codensity .M = Ext Codensity .unit = Ο unit-nt Codensity .mult = Ο mult-nt
Proving that these two extended natural transformations really do comprise a monad structure is a routine application of the uniqueness properties of right Kan extensions. The real noise comes from having to construct auxiliary natural transformations representing each pair of maps we want to compute with.
Codensity .left-ident {x = x} = path Ξ·β x where natβ : Ext => Ext natβ .Ξ· x = Ο mult-nt .Ξ· x B.β Ext.β (Ο unit-nt .Ξ· x) natβ .is-natural x y f = Ext.extendr (Ο unit-nt .is-natural x y f) β B.pushl (Ο mult-nt .is-natural _ _ _) abstract path : natβ β‘ idnt path = Ο-uniqβ eps (ext Ξ» x β sym ( B.pulll (Ο-comm Ξ·β x) β Ext.cancelr (Ο-comm Ξ·β x))) (ext Ξ» _ β B.intror refl) Codensity .right-ident {x = x} = path Ξ·β x where natβ : Ext => Ext natβ .Ξ· x = Ο mult-nt .Ξ· x B.β Ο unit-nt .Ξ· (Ext.β x) natβ .is-natural x y f = B.extendr (Ο unit-nt .is-natural _ _ _) β B.pushl (Ο mult-nt .is-natural _ _ _) abstract path : natβ β‘ idnt path = Ο-uniqβ eps (ext Ξ» x β sym $ B.pulll (Ο-comm Ξ·β x) Β·Β· B.pullr (sym (Ο unit-nt .is-natural _ _ _)) Β·Β· B.cancell (Ο-comm Ξ·β x)) (ext Ξ» _ β B.intror refl) Codensity .mult-assoc {x = x} = path Ξ·β x where mult' : (Ext Fβ Ext Fβ Ext) Fβ F => F mult' .Ξ· x = eps .Ξ· x B.β Ext.β (mult-nt .Ξ· x) mult' .is-natural x y f = Ext.extendr (mult-nt .is-natural _ _ _) β B.pushl (eps .is-natural _ _ _) sigβ : Ext Fβ Ext Fβ Ext => Ext sigβ .Ξ· x = Ο mult-nt .Ξ· x B.β Ext.β (Ο mult-nt .Ξ· x) sigβ .is-natural x y f = Ext.extendr (Ο mult-nt .is-natural _ _ _) β B.pushl (Ο mult-nt .is-natural _ _ _) sigβ : Ext Fβ Ext Fβ Ext => Ext sigβ .Ξ· x = Ο mult-nt .Ξ· x B.β Ο mult-nt .Ξ· (Ext.β x) sigβ .is-natural x y f = B.extendr (Ο mult-nt .is-natural _ _ _) β B.pushl (Ο mult-nt .is-natural _ _ _) abstract path : sigβ β‘ sigβ path = Ο-uniqβ {M = Ext Fβ Ext Fβ Ext} mult' (ext Ξ» x β sym $ B.pulll (Ο-comm Ξ·β x) β Ext.pullr (Ο-comm Ξ·β x)) (ext Ξ» x β sym $ B.pulll (Ο-comm Ξ·β x) Β·Β· B.pullr (sym (Ο mult-nt .is-natural _ _ _)) Β·Β· B.pulll (Ο-comm Ξ·β x) β Ext.pullr refl)
To understand what the codensity monad represents, recall that adjoints can be understood as efficient solutions to βoptimisation problemsβ. But when a functor does not admit a left adjoint, we conclude that there is no most efficient solution; This doesnβt mean that we canβt approximate a solution, though! And indeed, this kind of approximation is exactly what right Kan extensions are for.