← back to index

Kan Extensions

Bartosz Milewski · 2017 · Category Theory for Programmers, Ch. 21

Prereqs: 🍞 Ch16 (Adjunctions), Ch20 (Ends and Coends). 12 min.

"All concepts are Kan extensions" (wpMac Lane). Given functors K : I → A and D : I → C, the right Kan extension RanKD : A → C is the best approximation of D along K. It answers: how do you extend a functor defined on a subcategory to the whole category? Limits, colimits, and adjunctions are all special cases.

The problem: extending a functor

You have a functor D : I → C that maps a small category I into C. You also have a functor K : I → A that embeds I into a larger category A. You want to extend D to all of A, not just the image of K. The wpKan extension is the universal solution: the best functor A → C that approximates D, given K.

Scheme

Right Kan extension: the conservative approximation

The right Kan extension RanKD comes with a natural transformation ε : RanKD ∘ K → D. It is universal: for any other functor F with ε' : F ∘ K → D, there is a unique σ : F → RanKD such that ε' factors through ε. In Set, it is computed as an end: RanKD(a) = ∫i Set(A(a, Ki), Di). Read this as: "for each object a in A, consider all morphisms from a into the image of K, and use them to probe D."

A C I K D RanₖD ⇐ ε RanₖD ∘ K → D
Scheme

Left Kan extension: the liberal approximation

The left Kan extension LanKD is the dual. It comes with a unit η : D → LanKD ∘ K, and is universal from the other side. In Set, it is computed as a coend: LanKD(a) = ∫i A(Ki, a) × D(i). Where the right extension takes the tightest constraint (end = limit = universal quantifier), the left extension takes the freest construction (coend = colimit = existential quantifier).

A C I K D LanₖD ⇒ η D → LanₖD ∘ K
Scheme

Kan extensions as Haskell types

In Haskell, the right Kan extension is an end (universal quantification over a type variable), and the left Kan extension is a coend (existential quantification). These match the end/coend formulas exactly.

Scheme

Limits as Kan extensions

A limit of D : I → C is the right Kan extension of D along the unique functor K : I → 1 (the terminal category with one object). The Kan extension RanKD maps the single object of 1 to the limit of D. The universal property of the Kan extension becomes the universal property of the limit cone.

Scheme

Adjunctions as Kan extensions

If L ⊢ R is an adjunction, then L = LanRId and R = RanLId. The left adjoint is the left Kan extension of the identity along the right adjoint, and vice versa. This is Mac Lane's punchline: adjunctions, limits, colimits, ends, coends are all Kan extensions.

Scheme

The free functor as a left Kan extension

Given a type constructor f (not necessarily a functor), its free functor is the left Kan extension of f along the identity. In Haskell: data FreeF f a = forall i. FMap (i → a) (f i). This freely adds fmap to any type constructor. The existential packages up a value of type f i together with a function i → a, and fmap just composes with that function.

Scheme

Notation reference

Blog / Haskell Scheme Meaning
RanKD(ran-K-D a)Right Kan extension of D along K
LanKD(lan-K-D a)Left Kan extension of D along K
i Set(A(a,Ki), Di)(apply min ...)End formula (right Kan in Set)
i A(Ki,a) × Di(apply max ...)Coend formula (left Kan in Set)
Ran k d a = ∀i. (a→k i)→d i(lambda (a->ki) ...)Haskell right Kan type
Lan k d a = ∃i. (k i→a, d i)(list 'lan f val)Haskell left Kan type
Lim D = Ran!D(cartesian-product ...)Limit as right Kan along ! : I→1
FreeF f a = LanId f a(free-f func bx)Free functor as left Kan extension
Neighbors

Milewski chapters

Paper pages that use these concepts

Foundations (Wikipedia)

Translation notes

The blog post works in Haskell where the end formula becomes forall i. (a -> k i) -> d i and the coend formula becomes the existential data Lan k d a = forall i. Lan (k i -> a) (d i). This page uses a finite poset (the weekday ordering) to make the end/coend computable as min/max. In the real formulas, the end is a limit (equalizer of products) and the coend is a colimit (coequalizer of coproducts). The blog post also derives the codensity monad as the right Kan extension of a functor along itself: Ran m m a = forall i. (a -> m i) -> m i, which is the continuation monad when m is an endofunctor. The free functor construction (LanId f) demonstrates that left Kan extensions freely add structure, while right Kan extensions preserve it conservatively. This asymmetry mirrors the left/right adjoint distinction from Chapter 16.

Ready for the real thing? Read the blog post. Start with the universal property diagrams, then trace how the end/coend formulas implement them.