Extending as well as possible
Here are three problems that look unrelated.
- You know a function only on the integers, say , and you want a monotone function on all of that is “as close as possible” to it. From below, the answer is ; from above, .
- A subgroup of a group acts on a set or a vector space . You want a -action that is “freely generated” by the -action. The answer is the induced representation .
- You know a topological space for each standard simplex , and you want to turn every simplicial set into a space, gluing simplices the way the simplicial set says. The answer is geometric realisation.
In each case there is a functor defined on a “small piece” , a functor embedding (or merely mapping) that piece into a larger world, and we want the best functor that extends . In general no functor extends on the nose — but there is a best approximation from the left and a best approximation from the right. These are the left and right Kan extensions, and they turn out to be so universal that Saunders Mac Lane ended the first edition of Categories for the Working Mathematician (1971) with a section titled “All concepts are Kan extensions”.
This lesson assumes adjunctions, limits and colimits and the Yoneda lemma.
The picture: a triangle that need not commute
Draw and as two sides of a triangle. We want to fill in the third side.
Asking for is usually hopeless: if sends two objects to the same object , then would have to equal both and . Category theory's answer is to ask instead for a natural transformation — “ is at least as big as along ” — and to take the smallest such . Dually, a right Kan extension is the largest with .
The poset case makes this concrete. A poset is a category with at most one arrow between two objects, a functor between posets is a monotone map, and a natural transformation exists exactly when pointwise. The smallest monotone with is forced: must be at least whenever , so the best choice is
That supremum is the poset shadow of the general formula: a left Kan extension at is the colimit of everything that maps to .
Definitions
Throughout, and are functors. For a functor , write , and for a natural transformation write for the whiskered transformation with components .
Definition— left Kan extension
A left Kan extension of along is a functor together with a natural transformation (the unit) such that for every functor and every natural transformation there is a unique natural transformation with
Equivalently, is a bijection for every . We write ; it is unique up to unique isomorphism, as any universal object is.
Definition— right Kan extension
A right Kan extension of along is a functor with a natural transformation (the counit) such that for every and every there is a unique with . Equivalently, .
If every has a left (respectively right) Kan extension along , the bijections above say precisely that and are the left and right adjoints of restriction along , the functor , :
To compute Kan extensions we need comma categories.
Definition— comma categories over and under an object
For an object , the comma category has as objects the pairs with and ; a morphism is a morphism with . It comes with the projection , . Dually, has objects and morphisms with . (Mathlib calls these CostructuredArrow K d and StructuredArrow d K.)
Definition— pointwise Kan extension
The left Kan extension is pointwise when, for every ,
the colimit of , and dually . In words: the left Kan extension at glues together the values of everything that maps to , identified along the arrows between them.
Examples, non-examples and boundary cases
Example— Floor and ceiling
Let be the inclusion and the inclusion into the complete lattice of extended integers. By the poset formula,
Every monotone that agrees with on the integers satisfies : the Kan extensions are the extreme extensions. Since is an order embedding, both restrict back to on .
Example— Limits and colimits
Let be the terminal category and the unique functor. A functor is an object , and a natural transformation is a family of maps compatible with all — a cone over with vertex . The universal property of says that there is a universal cone. Hence
each existing exactly when the (co)limit does. (A full proof is in the next section.) For the discrete category on two objects this gives products and coproducts: the right Kan extension of to is — the universal property explored in the trail The product, by its maps.
Example— Extending along a non-injective functor: no strict extension
Let (discrete source) and with , . Then has elements and has . Neither restricts back to : has elements, not . The unit is the coproduct inclusion — a genuine map, not an identity.
Example— Induced and coinduced actions
A group is a category with one object and one arrow for each ; a functor is a -set. For a subgroup with inclusion and an -set :
- The comma category has one object for each (an arrow ), and an arrow for each . Its colimit glues to , so , the induced -set, with elements when is finite.
- Dually , the coinduced -set of -equivariant maps, with elements.
For and , acting on itself: the free -set on one generator. The experiment below computes all of these as colimits and limits.
Example— Non-example: a Kan extension that does not exist
Let be the inclusion of the discrete poset into with , and let send , in the discrete two-element poset . A monotone with would need and , which is impossible. So there is no extension at all, and certainly no Kan extension. The pointwise formula fails precisely because the join does not exist.
The main theorems
Theorem— Theorem 1 (the pointwise formula)
Suppose that for every the colimit over exists, with coprojections . Then extends uniquely to a functor with , and with the pair is a left Kan extension of along .
Proof
Functoriality. An arrow induces a functor , , with . The maps therefore form a cocone over , so there is a unique with . Both and , and both and , satisfy the same equations on coprojections, so they agree by the uniqueness clause of the colimit.
The unit is natural. For in we must show . The left side is by definition of . On the right, is a morphism of , so the cocone condition gives .
Existence of factorisations. Let and . For fixed , the maps form a cocone over : if , then , and naturality of gives
So there is a unique with . It is natural in , because for both and send to . Finally .
Uniqueness. If also satisfies , then, using and naturality of ,
so by the uniqueness of maps out of a colimit.
∎
Theorem— Corollary (existence)
If is small, is locally small and has all small colimits, then every has a pointwise left Kan extension along every , so . Dually, if has all small limits, .
Proof
Each is small: its objects form the set (a set because is small and locally small), and its morphisms are among those of . So the colimits in Theorem 1 exist. The dual statement is Theorem 1 applied to and .
∎
Theorem— Proposition 2 (extension along a fully faithful functor)
If is fully faithful and is pointwise, then the unit is a natural isomorphism: the Kan extension really extends .
Proof
In the object is terminal: given , full faithfulness gives a unique with , which is exactly a unique morphism . The colimit of a diagram over a category with a terminal object is its value at , with coprojection at an isomorphism. Here that coprojection is .
∎
The hypothesis matters: the two-point example above shows what happens for a that is not injective on objects.
Theorem— Proposition 3 (limits are right Kan extensions)
For , an object with a cone is a right Kan extension of along if and only if is a limit of .
Proof
Functors are objects, and natural transformations between them are morphisms. A transformation is a cone with vertex , and for the composite is the cone . So the universal property of — every equals for a unique — is word for word the universal property of a limit.
∎
Theorem— Theorem 4 (adjoints are Kan extensions; Mac Lane)
A functor has a left adjoint if and only if the right Kan extension exists and is preserved by , meaning that is a right Kan extension of along . In that case is the left adjoint, with counit . In fact, if then is preserved by every functor out of .
Proof
() Let with unit , counit , and triangle identities and . Let be any functor; we show is a right Kan extension of along . Given and , put . Then, by naturality of at and the first triangle identity,
If is another solution, the second triangle identity and naturality of at give
Taking shows ; taking shows that preserves it.
() Suppose is and is . Apply the universal property of to the identity transformation : there is a unique with , which is one triangle identity. For the other, both and are transformations , and the universal property of says there is only one with . The identity qualifies, and so does : by naturality of at the arrow ,
Hence . Both triangle identities hold, so with unit and counit .
∎
Note the twist: a left adjoint is a right Kan extension, , and dually a right adjoint is a left Kan extension, . In posets: the ceiling function, left adjoint to , is an infimum; the floor, right adjoint, is a supremum.
Theorem— Theorem 5 (extending along the Yoneda embedding)
Let be small, cocomplete and locally small, , and the Yoneda embedding. Then exists and is given by
the colimit over the category of elements of (objects: pairs , ). Moreover ; is left adjoint to the “nerve” , ; and is, up to isomorphism, the unique colimit-preserving functor whose composite with is isomorphic to .
Proof
Sketch. By the Yoneda lemma an arrow is the same as an element , and this identifies the comma category with (up to size: is locally small because is small, so is small). Theorem 1 gives the formula. The Yoneda embedding is fully faithful, so Proposition 2 gives . For the adjunction, maps out of a colimit are compatible families:
the second step because a natural transformation is exactly an assignment compatible with restriction. Being a left adjoint, preserves colimits. Uniqueness: every presheaf is canonically the colimit of representables over its category of elements (the density theorem, which is the case ), so a colimit-preserving functor is determined by its values on representables. Full details: Riehl (2016), Chapter 6, or Mac Lane and Moerdijk (1992), Chapter I.
∎
This “nerve and realisation” pattern appears all over mathematics: with , , it gives geometric realisation ; with , the poset , it gives the nerve of a category and its left adjoint.
Kan extensions in Lean
The Lean module has two parts.
Part 1 (independent): the thin case. For preorders and a complete lattice , everything above can be proved from scratch, because in a thin category a natural transformation exists exactly when there is a pointwise inequality, and colimits and limits in a complete lattice are suprema and infima. The definitions are the pointwise formulas:
/-- The **pointwise left Kan extension** of `f : C → E` along `k : C → D`, for a preorder `D`
and a complete lattice `E` (the order of `C`, if any, plays no role in the formula):
`lan k f d = ⨆ { f c | k c ≤ d }`.
This is the formula `(Lan_k f)(d) = colim_{(k ↓ d)} f`: in a thin category the comma category
`(k ↓ d)` is the set of `c` with `k c ≤ d`, and a colimit in a complete lattice is a supremum. -/
def lan (k : C → D) (f : C → E) (d : D) : E :=
⨆ (c : C) (_ : k c ≤ d), f c/-- The **pointwise right Kan extension**: `ran k f d = ⨅ { f c | d ≤ k c }`, the limit of `f`
over the comma category `(d ↓ k)`. -/
def ran (k : C → D) (f : C → E) (d : D) : E :=
⨅ (c : C) (_ : d ≤ k c), f clan k f is a functor, the unit exists, and the universal property holds — the poset shadow of Theorem 1:
/-- `lan k f` is a functor, i.e. monotone: if `d ≤ d'` then every `c` with `k c ≤ d` also has
`k c ≤ d'`, so the supremum can only grow. (This is how a pointwise Kan extension acts on
arrows: an arrow `d → d'` induces a functor `(k ↓ d) → (k ↓ d')` and hence a map of colimits.) -/
theorem lan_monotone (k : C → D) (f : C → E) : Monotone (lan k f) := by
intro d d' h
exact iSup₂_le fun c hc => le_iSup₂_of_le c (hc.trans h) le_rfl/-- The **unit** `f ⇒ (Lan_k f) ∘ k`: `f c ≤ lan k f (k c)`, because `c` itself lies in the
comma category `(k ↓ k c)` (via the identity `k c ≤ k c`). -/
theorem le_lan_comp (k : C → D) (f : C → E) (c : C) : f c ≤ lan k f (k c) :=
le_iSup₂_of_le c le_rfl le_rfl/-- The **universal property** of the left Kan extension. For every monotone `g : D → E`:
`Lan_k f ≤ g` if and only if `f ≤ g ∘ k`.
(→) compose with the unit: `f c ≤ lan k f (k c) ≤ g (k c)`.
(←) each term `f c` with `k c ≤ d` satisfies `f c ≤ g (k c) ≤ g d` by monotonicity of `g`.
In a thin category, arrows are unique when they exist, so this *is* the statement that every
`f ⇒ g ∘ k` factors uniquely through the unit. -/
theorem lan_le_iff (k : C → D) (f : C → E) {g : D → E} (hg : Monotone g) :
lan k f ≤ g ↔ ∀ c, f c ≤ g (k c) := by
constructor
· intro h c
exact (le_lan_comp k f c).trans (h (k c))
· intro h d
exact iSup₂_le fun c hc => (h c).trans (hg hc)Any functor with this universal property is lan k f (uniqueness of universal objects), and along an order embedding the extension restricts back to f (Proposition 2):
/-- **Uniqueness**: any monotone `L : D → E` with the universal property of a left Kan
extension equals the pointwise formula. As always with universal properties, the proof plays
the two universal properties against each other. -/
theorem lan_unique (k : C → D) (f : C → E) {L : D → E} (hL : Monotone L)
(huniv : ∀ g : D → E, Monotone g → (L ≤ g ↔ ∀ c, f c ≤ g (k c))) :
L = lan k f := by
apply le_antisymm
· exact (huniv (lan k f) (lan_monotone k f)).2 (le_lan_comp k f)
· exact (lan_le_iff k f hL).2 ((huniv L hL).1 le_rfl)/-- **Kan extension along a fully faithful functor is a genuine extension.** If `k` is an order
embedding (`k c ≤ k c' ↔ c ≤ c'`, the thin version of "fully faithful") and `f` is monotone,
then `lan k f (k c) = f c`: the unit is an isomorphism. The comma category `(k ↓ k c)` then
has the terminal object `c`, and a colimit over a category with a terminal object is the value
there. -/
theorem lan_comp_of_embedding [Preorder C] {k : C → D} (hk : ∀ c c', k c ≤ k c' ↔ c ≤ c') {f : C → E}
(hf : Monotone f) (c : C) : lan k f (k c) = f c := by
apply le_antisymm
· exact iSup₂_le fun c' hc' => hf ((hk c' c).1 hc')
· exact le_lan_comp k f cRead as an adjunction between the preorders of monotone maps, the universal property is :
/-- **`Lan_k` is left adjoint to restriction along `k`.** On monotone maps (functors),
`Lan_k ⊣ k^*` where `k^* g = g ∘ k`: this is a Galois connection between the preorders of
functors `C →o E` and `D →o E` (ordered pointwise, i.e. by the existence of natural
transformations). It is `lan_le_iff` read as an adjunction. -/
theorem lan_gc [Preorder C] (k : C →o D) :
GaloisConnection (fun f : C →o E => (⟨lan k f, lan_monotone k f⟩ : D →o E))
(fun g : D →o E => g.comp k) := by
intro f g
exact lan_le_iff k f g.monotoneThe right Kan extension is dual, and limits and colimits are Kan extensions along the map to Unit (Proposition 3):
/-- `ran k f` is monotone: if `d ≤ d'`, every `c` with `d' ≤ k c` also has `d ≤ k c`, so the
infimum defining `ran k f d` is taken over a larger family and can only be smaller. -/
theorem ran_monotone (k : C → D) (f : C → E) : Monotone (ran k f) := by
intro d d' h
exact le_iInf₂ fun c hc => iInf₂_le_of_le c (h.trans hc) le_rfl/-- The **counit** of the right Kan extension: `(Ran_k f) ∘ k ⇒ f`, i.e.
`ran k f (k c) ≤ f c`, because `c` lies in `(k c ↓ k)`. -/
theorem ran_comp_le (k : C → D) (f : C → E) (c : C) : ran k f (k c) ≤ f c :=
iInf₂_le_of_le c le_rfl le_rfl/-- The universal property of the **right** Kan extension, dual to `lan_le_iff`: for monotone
`g : D → E`, `g ≤ Ran_k f` if and only if `g ∘ k ≤ f`. -/
theorem le_ran_iff (k : C → D) (f : C → E) {g : D → E} (hg : Monotone g) :
g ≤ ran k f ↔ ∀ c, g (k c) ≤ f c := by
constructor
· intro h c
exact (h (k c)).trans (ran_comp_le k f c)
· intro h d
exact le_iInf₂ fun c hc => (hg hc).trans (h c)/-- **A limit is a right Kan extension along the functor to the terminal category.** Along
`! : C → Unit`, the comma category `(() ↓ !)` is all of `C`, so `Ran_! f` at the unique object
is `⨅ c, f c`, the infimum — which is the limit of `f` in the complete lattice `E`. -/
theorem ran_const_eq_iInf (f : C → E) : ran (fun _ : C => ()) f () = ⨅ c, f c := by
simp [ran]/-- Dually, a **colimit is a left Kan extension along `! : C → Unit`**: `Lan_! f () = ⨆ c, f c`. -/
theorem lan_const_eq_iSup (f : C → E) : lan (fun _ : C => ()) f () = ⨆ c, f c := by
simp [lan]Theorem 4 for complete lattices, where adjunctions are Galois connections: a left adjoint is , a right adjoint is , and has a left adjoint exactly when it preserves . The last result is the adjoint functor theorem for complete lattices.
/-- **A left adjoint is a right Kan extension of the identity.** If `F ⊣ G` (a Galois
connection `F x ≤ a ↔ x ≤ G a`), then `F = Ran_G id`, i.e.
`F x = ⨅ { a | x ≤ G a }`. In words: `F x` is the best approximation of `x` from above by
things of the form "an `a` with `x ≤ G a`". -/
theorem left_adjoint_eq_ran {F : X → A} {G : A → X} (gc : GaloisConnection F G) :
F = ran G id := by
funext x
apply le_antisymm
· exact le_iInf₂ fun a hx => (gc x a).2 hx
· exact iInf₂_le_of_le (F x) (gc.le_u_l x) le_rfl/-- Dually, **a right adjoint is a left Kan extension of the identity**: if `F ⊣ G` then
`G = Lan_F id`, i.e. `G a = ⨆ { x | F x ≤ a }` — the best approximation of `a` from below. -/
theorem right_adjoint_eq_lan {F : X → A} {G : A → X} (gc : GaloisConnection F G) :
G = lan F id := by
funext a
apply le_antisymm
· exact le_iSup₂_of_le (G a) (gc.l_u_le a) le_rfl
· exact iSup₂_le fun x hx => (gc x a).1 hx/-- **Mac Lane's criterion for adjoints, for complete lattices.** A monotone `G : A → X` has a
left adjoint if and only if `G` *preserves* the right Kan extension `Ran_G id`, meaning
`G ∘ Ran_G id = Ran_G G`:
`G (⨅ { a | x ≤ G a }) = ⨅ { G a | x ≤ G a }` for every `x`.
In a complete lattice `Ran_G id` always exists (it is the formula); the content is preservation.
The left adjoint is then `Ran_G id` (see `left_adjoint_eq_ran`). -/
theorem exists_left_adjoint_iff {G : A → X} (hG : Monotone G) :
(∃ F : X → A, GaloisConnection F G) ↔ ∀ x, G (ran G id x) = ran G G x := by
constructor
· rintro ⟨F, gc⟩ x
rw [← left_adjoint_eq_ran gc]
apply le_antisymm
· exact le_iInf₂ fun a hx => hG ((gc x a).2 hx)
· exact iInf₂_le_of_le (F x) (gc.le_u_l x) le_rfl
· intro h
refine ⟨ran G id, fun x a => ⟨fun hxa => ?_, fun hxa => iInf₂_le_of_le a hxa le_rfl⟩⟩
-- `x ≤ G (Ran_G id x)` because `G` preserves the infimum, and each term is `≥ x`.
have hx : x ≤ G (ran G id x) := by
rw [h x]
exact le_iInf₂ fun _ hc => hc
exact hx.trans (hG hxa)/-- **The adjoint functor theorem for complete lattices.** If `G : A → X` preserves all
infima, then `G` has a left adjoint. Preserving all infima includes preserving the particular
infimum `Ran_G id x = ⨅ { a | x ≤ G a }`, so this follows from `exists_left_adjoint_iff`;
monotonicity comes from preserving binary infima. -/
theorem exists_left_adjoint_of_map_sInf {G : A → X}
(hG : ∀ s : Set A, G (sInf s) = ⨅ a ∈ s, G a) : ∃ F : X → A, GaloisConnection F G := by
have mono : Monotone G := by
intro a b hab
have h := hG {a, b}
rw [sInf_pair, inf_eq_left.2 hab] at h
rw [h]
exact iInf₂_le b (by simp)
refine (exists_left_adjoint_iff mono).2 fun x => ?_
have hs : ran G id x = sInf { a | x ≤ G a } := by
simp [ran, sInf_eq_iInf]
rw [hs, hG]
simp [ran]Part 2 (library): the general theory. Mathlib has Kan extensions for arbitrary categories (Functor.LeftExtension, Functor.IsLeftKanExtension, Functor.leftKanExtension, pointwise Kan extensions in Mathlib.CategoryTheory.Functor.KanExtension.Pointwise). Two results of this lesson, in full generality, by applying Mathlib's theorems: is left adjoint to restriction, and the pointwise formula with its existence conditions (Theorem 1 and its corollary).
/-- **In general, `Lan_L` is left adjoint to restriction along `L`.** For any functor
`L : C ⥤ D` such that every `F : C ⥤ H` has a left Kan extension along `L`, the functor
`L.lan : (C ⥤ H) ⥤ (D ⥤ H)` is a left adjoint; its right adjoint is precomposition with `L`.
This applies Mathlib's `Functor.lanAdjunction` (with `Adjunction.isLeftAdjoint`). -/
theorem lan_isLeftAdjoint {C D H : Type*} [Category C] [Category D] [Category H]
(L : C ⥤ D) [∀ F : C ⥤ H, L.HasLeftKanExtension F] :
(L.lan : (C ⥤ H) ⥤ (D ⥤ H)).IsLeftAdjoint :=
(L.lanAdjunction H).isLeftAdjoint/-- **The pointwise formula, in general.** If `C` is small, `D` is locally small (its
hom-sets live in the same universe) and `H` has all small colimits, then the left Kan extension
of `F : C ⥤ H` along `L : C ⥤ D` exists and its value at `d` is the colimit of `F` over the
comma category `(L ↓ d)` (`CostructuredArrow L d` in Mathlib).
This applies Mathlib's `Functor.leftKanExtensionObjIsoColimit`; the existence of the colimits
is found by instance search from `HasColimits H`. -/
theorem lan_obj_iso_colimit {C D H : Type*} [SmallCategory C] [Category D] [Category H]
(L : C ⥤ D) (F : C ⥤ H) (d : D) [HasColimits H] :
Nonempty ((L.leftKanExtension F).obj d ≅ colimit (CostructuredArrow.proj L d ⋙ F)) :=
⟨L.leftKanExtensionObjIsoColimit F d⟩What is and is not formalised. Theorems 1–4 are proved here independently only in the thin case (posets valued in complete lattices); the general pointwise formula and adjunction are cited from Mathlib. Theorem 4 in general (with absolute Kan extensions) and Theorem 5 (extension along Yoneda) are not formalised in this module.
Computing Kan extensions of finite sets
For functors into finite sets the pointwise formulas are algorithms: a colimit is a disjoint union glued along the arrows (a union–find computation), and a limit is the set of compatible families. The experiment computes Lan and Ran along a map of finite posets, a colimit over a diagram with a terminal object, and induced and coinduced actions of the subgroups of .
"""Pointwise Kan extensions of finite set-valued functors, computed exactly.
The pointwise formulas say that a left Kan extension is a colimit over a
comma category and a right Kan extension is a limit. For diagrams of finite
sets both can be computed exactly: a colimit is a disjoint union glued by a
union-find structure, a limit is the set of compatible families. We use this
to (1) compare Lan and Ran along an inclusion of finite posets (coproducts
versus products, initial versus terminal objects), (2) see that a colimit over
a category with a terminal object is the value there, and (3) induce and
coinduce actions of subgroups H of S3, checking |G x_H X| = [G:H] |X| and
|Hom_H(G, X)| = |X|^[G:H]. This is exact, exhaustive computation in finite
cases; the general theorems are proved in the lesson.
"""
from itertools import permutations, product
def colimit(sets, arrows):
"""Colimit of a finite diagram of finite sets.
``sets`` maps each object to a list of elements; ``arrows`` is a list of
``(source, target, function)``. Returns the equivalence classes of the
disjoint union {(object, element)} under (s, x) ~ (t, function(x)).
"""
parent = {(o, x): (o, x) for o in sets for x in sets[o]}
def find(a):
while parent[a] != a:
parent[a] = parent[parent[a]]
a = parent[a]
return a
for source, target, fn in arrows:
for x in sets[source]:
ra, rb = find((source, x)), find((target, fn(x)))
if ra != rb:
parent[ra] = rb
classes = {}
for a in parent:
classes.setdefault(find(a), set()).add(a)
return [frozenset(c) for c in classes.values()]
def limit(sets, arrows):
"""Limit of a finite diagram of finite sets: all compatible families.
A family picks one element of each set; it is compatible when
``function(family[source]) == family[target]`` for every arrow. The empty
diagram has exactly one (empty) family: the limit is a one-point set.
"""
objects = list(sets)
families = []
for values in product(*(sets[o] for o in objects)):
family = dict(zip(objects, values))
if all(fn(family[s]) == family[t] for s, t, fn in arrows):
families.append(family)
return families
def thin_diagram(objects, leq, sets, fmap):
"""Restrict a functor on a poset to ``objects`` (all arrows c < c')."""
arrows = [
(c, c2, (lambda x, c=c, c2=c2: fmap(c, c2, x)))
for c in objects
for c2 in objects
if c != c2 and leq(c, c2)
]
return {c: sets[c] for c in objects}, arrows
def lan_size(C, leq_c, k, leq_d, d, sets, fmap):
"""|Lan_k F (d)|: the colimit of F over the comma category (k | d)."""
comma = [c for c in C if leq_d(k(c), d)]
return len(colimit(*thin_diagram(comma, leq_c, sets, fmap)))
def ran_size(C, leq_c, k, leq_d, d, sets, fmap):
"""|Ran_k F (d)|: the limit of F over the comma category (d | k)."""
comma = [c for c in C if leq_d(d, k(c))]
return len(limit(*thin_diagram(comma, leq_c, sets, fmap)))
def compose(p, q):
"""Composite permutation p o q (first q, then p) of tuples."""
return tuple(p[i] for i in q)
S3 = sorted(permutations(range(3)))
def induced_diagram(G, H, X, act):
"""F o proj on the comma category (i | *) for i : BH -> BG.
Objects are g in G; for g2 in G and h in H there is an arrow
g2 h -> g2 acting on X by h. Its colimit is G x_H X.
"""
sets = {g: list(X) for g in G}
arrows = [(compose(g2, h), g2, (lambda x, h=h: act(h, x)))
for g2 in G for h in H]
return sets, arrows
def coinduced_diagram(G, H, X, act):
"""F o proj on (* | i): arrows g -> h g acting by h; limit Hom_H(G, X)."""
sets = {g: list(X) for g in G}
arrows = [(g, compose(h, g), (lambda x, h=h: act(h, x)))
for g in G for h in H]
return sets, arrows
def sign(p):
"""Sign of a permutation tuple (+1 or -1), by counting inversions."""
inv = sum(1 for i in range(len(p)) for j in range(i) if p[j] > p[i])
return -1 if inv % 2 else 1
# Poset example: C = {a, b} discrete inside D = {bot < a, b < top}.
POSET_C = ["a", "b"]
POSET_D = ["bot", "a", "b", "top"]
POSET_SETS = {"a": [0, 1], "b": [0, 1, 2]}
def leq_d(x, y):
"""The order of D = {bot < a, b < top}."""
return x == y or x == "bot" or y == "top"
def leq_discrete(x, y):
"""The discrete order: only x <= x."""
return x == y
# Chain example: 0 < 1 < 2 with F(0) -> F(1) -> F(2).
CHAIN = [0, 1, 2]
CHAIN_SETS = {0: list(range(4)), 1: list(range(3)), 2: list(range(2))}
def chain_map(c, c2, x):
"""F(c <= c2): halve from F(0) to F(1), take parity from F(1) to F(2)."""
if c == 0 and c2 >= 1:
x, c = x // 2, 1
if c == 1 and c2 == 2:
x = x % 2
return x
def subgroup_cases():
"""(name, H, X, action) for the induction table."""
swap = [(0, 1, 2), (1, 0, 2)]
a3 = [p for p in S3 if sign(p) == 1]
return [
("H = <(0 1)>, X = {+1,-1}, sign", swap, [1, -1],
lambda h, x: sign(h) * x),
("H = A3, X = {0,1,2}, rotation", a3, [0, 1, 2],
lambda h, x: h[x]),
("H = 1, X = {*}", [(0, 1, 2)], ["*"], lambda h, x: x),
]
def main():
print("1. Lan and Ran along C = {a, b} (discrete) -> D = {bot < a, b < top}")
print(" with |F a| = 2, |F b| = 3:")
print(" d |Lan F(d)| |Ran F(d)|")
for d in POSET_D:
lan = lan_size(POSET_C, leq_discrete, lambda c: c, leq_d, d,
POSET_SETS, None)
ran = ran_size(POSET_C, leq_discrete, lambda c: c, leq_d, d,
POSET_SETS, None)
print(f" {d:<5} {lan:>10} {ran:>10}")
print(" Lan(top) is the coproduct 2 + 3, Ran(bot) the product 2 x 3;")
print(" Lan(bot) = 0 is an empty colimit, Ran(top) = 1 an empty limit.")
print()
print("2. Along C = {0 < 1 < 2} -> 1 with |F| = 4, 3, 2:")
sets, arrows = thin_diagram(CHAIN, lambda x, y: x <= y, CHAIN_SETS,
chain_map)
print(f" colim F has {len(colimit(sets, arrows))} elements = |F(2)|"
f" (2 is terminal); lim F has {len(limit(sets, arrows))} = |F(0)|.")
print()
print("3. Kan extension along BH -> BG, G = S3 (|G| = 6):")
print(" case [G:H] |Lan| |Ran|")
for name, H, X, act in subgroup_cases():
index = len(S3) // len(H)
lan = len(colimit(*induced_diagram(S3, H, X, act)))
ran = len(limit(*coinduced_diagram(S3, H, X, act)))
print(f" {name:<37} {index:>5} {lan:>5} {ran:>5}")
print(" Lan = G x_H X has [G:H]|X| elements; Ran = Hom_H(G, X) has")
print(" |X|^[G:H]. For H = 1, Lan is G acting on itself: the free G-set.")
print()
print("Epistemic status: exact computation of finite colimits and limits;")
print("it confirms the pointwise formulas and the induction counts only in")
print("these finite examples. The general statements (the pointwise")
print("formula and its consequences) are theorems proved in the lesson.")
if __name__ == "__main__":
main()What to notice: at the top of the poset, Lan gives the coproduct (); at the bottom, Ran gives the product (); at and , both restrict back to , as Proposition 2 predicts for an order embedding. For groups, induction multiplies by the index while coinduction exponentiates. This is exact computation in finitely many cases — it illustrates the theorems but does not prove them.
Seeing the pointwise formula
A left Kan extension at is a colimit: each object of contributes a coprojection, and each morphism forces a triangle to commute.
In the poset example of the experiment ( discrete inside ), the comma category at is and the one under is as well, so the two extensions produce the two universal constructions on the pair :
Try it yourself: in the experiment's code, change the sizes of and in POSET_SETS and predict the table before running it. Then compare part 2 of the experiment: there the indexing category is the chain , which has the terminal object , so the colimit is and, dually, the limit is the value at the initial object.
Exercises
1. Kan extensions
Warm-upHumanLet and be posets, a complete lattice, monotone and monotone, all viewed as thin categories and functors. Which formula gives the pointwise left Kan extension ?
2. Kan extensions
Warm-upHumanLet be the functor from the discrete category with two objects to the terminal category, and let have and . How many elements does have?
3. Kan extensions
CoreHumanLet be a subgroup of index in a finite group , let be the inclusion of one-object categories, and let be an -set (a functor ) with . How many elements does the right Kan extension have?
4. Kan extensions
CoreHumanIn the proof that the pointwise formula gives a left Kan extension, given and , we define by . For this to make sense, the maps must form a cocone over : for we need . Which fact gives this?
5. Kan extensions
CoreHumanWhich situation gives a functor with no left Kan extension along ?
6. Kan extensions
CoreHumanWhich of these are Kan extensions? (Choose all that apply.)
7. Kan extensions
StretchHumanSuppose with , unit and counit . Which statement is correct?
8. Kan extensions
CoreFormalIn the lesson's Lean file, lan k f d = ⨆ (c : C) (_ : k c ≤ d), f c. Which Lean statement says that is left adjoint to restriction along (for monotone , on monotone maps into a complete lattice )?
9. Kan extensions
CoreComputationalLet be a finite set of sample points in , the inclusion and any function, given as a dictionary {point: value}. Write lan_value(f, d) returning (use float('-inf') for an empty supremum) and ran_value(f, d) returning (float('inf') if empty). Then check: when is not monotone, is its running maximum, the smallest monotone function above on the sample points.
Common misconceptions
Misconception
“Kan extensions always exist.” They do not: the two-point example above has no extension at all, because the target lacks a join. The standard sufficient condition is: small, locally small, and cocomplete (for ) or complete (for ). The local smallness of is easy to forget: it is what makes the comma categories small. Not every Kan extension that exists is pointwise either; in practice the pointwise ones are the ones that behave well.
Misconception
Confusing left and right. Left Kan extensions are computed by colimits over — “everything that maps to ” — and right Kan extensions by limits over . Mnemonic: floor is , ceiling is . But beware the twist: a left adjoint is a right Kan extension, , because its counit has the shape of a Kan extension counit.
Misconception
“A Kan extension restricts back to the original functor.” Only when the unit is an isomorphism, which holds for pointwise extensions along fully faithful (Proposition 2). Along , .
Misconception
“A Kan extension is just some extension.” The universal property is the point: is the initial functor equipped with . In the floor example infinitely many monotone functions extend the integers; the Kan extensions are the two extreme ones.
Viewpoints
Algebra— Induction, Frobenius reciprocity and extension of scalars
For a subgroup and the inclusion , restriction is restriction of representations from to . For functors into , the pointwise formula gives
and the adjunctions are Frobenius reciprocity: , and its dual. For linear representations and , induction and coinduction are isomorphic; for -sets they are not ( versus elements), which the experiment shows. More generally, a ring homomorphism is a functor between one-object -enriched categories, and the enriched left Kan extension of a module along it is extension of scalars , with coextension on the right; this needs the enriched theory of enriched categories, where Kan extensions are coends.
Topology— Inverse images of presheaves and geometric realisation
Let be continuous and write for the poset of open sets. Preimage is a monotone map . For a presheaf on , the pushforward is just precomposition, , so its left adjoint is a left Kan extension along . The pointwise formula computes it: the comma category at an open consists of the opens with , i.e. , so
the familiar formula for the presheaf inverse image (the sheaf inverse image is its sheafification; see sheaves). Geometric realisation of simplicial sets is the left Kan extension of along the Yoneda embedding (Theorem 5), with right adjoint the singular complex. Beyond this lesson: in homotopy theory one needs homotopy Kan extensions (derived functors of , such as homotopy colimits), and in ∞-categories Kan extensions are defined up to coherent homotopy.
Analysis— Best approximations from below and above
Let be monotone and the inclusion, with values in the complete lattice . Then
Both extend (since is an order embedding), every monotone extension of satisfies , and at an irrational the two are the one-sided limits and . They differ exactly at the jumps of , and a monotone real-valued function has at most countably many jumps (disjoint open intervals each contain a rational). So the monotone extension of to is unique if and only if has no jumps at irrational points. The Python exercise computes these envelopes on finitely many sample points.
Logic— Quantifiers are Kan extensions
Regard a function as a functor between discrete categories, and a predicate on as a functor . Then the pointwise formulas give
and is Lawvere's discovery that : quantifiers are adjoint to substitution. For a projection these are the ordinary quantifiers over . See categorical logic.
Computation— Codensity monads and faster programs
For any functor , if the right Kan extension exists it carries a canonical monad structure, the codensity monad of ; when has a left adjoint , is the monad of the adjunction (by Theorem 4, applied with ). A classical example (Kennison and Gildenhuys, 1971): the codensity monad of the inclusion is the ultrafilter monad. In functional programming, Haskell's Codensity m a = forall b. (a -> m b) -> m b is written as an end, and Voigtländer (2008) used it to turn left-nested binds of free monads from quadratic into linear time.
All concepts are Kan extensions
Mac Lane's slogan collects the results of this lesson:
- limits and colimits are Kan extensions along (Proposition 3);
- adjoints are Kan extensions of identities, and (Theorem 4);
- density and Yoneda: the density theorem says — every presheaf is canonically a colimit of representables (Theorem 5 with );
- monads arise as codensity monads .
Even Kan extensions are Kan extensions: the definition uses only functors, natural transformations and their composites, so it makes sense in any 2-category, and the pointwise formula becomes a weighted colimit (a coend, ) in the enriched setting of Kelly (1982). The slogan is an exaggeration — not every concept is usefully a Kan extension — but it records how often a construction turns out to be “the best approximation along a functor”.
Wonder Trail
The product is the right Kan extension of the pair along the functor from the two-object discrete category to . This trail explores its universal property from first principles.
Wonder Trail · Optional · never gradedThe product, by its mapsCan you describe the set of pairs without ever looking inside it — only by how functions go into it?Follow the trailHistory and global context
- Daniel M. Kan (born in the Netherlands, later at MIT) introduced adjoint functors in his paper Adjoint functors (Transactions of the American Mathematical Society, 1958), working in homotopy theory and simplicial sets. The same paper constructs what are now called Kan extensions, by the colimit and limit formulas.
- In the 1960s, the Grothendieck school's work on topos theory (SGA 4, 1963–64) made systematic use of extending functors from a site to all presheaves by colimits, and F. William Lawvere showed that quantifiers are adjoint to substitution (1969).
- Saunders Mac Lane's Categories for the Working Mathematician (1971) ends its chapter on Kan extensions with the section “All concepts are Kan extensions”.
- Eduardo Dubuc (Argentina) developed Kan extensions in enriched category theory (Springer Lecture Notes 145, 1970), and G. M. Kelly (Sydney) made pointwise enriched Kan extensions central in Basic Concepts of Enriched Category Theory (1982).
- Today Kan extensions are basic tools in homotopy theory (homotopy Kan extensions, ∞-categorical Kan extensions in Lurie's Higher Topos Theory, 2009), in representation theory, in algebraic geometry, and in functional programming.
Further reading
- Saunders Mac Lane, Categories for the Working Mathematician (1971; 2nd edition 1998), Chapter X — Kan extensions, pointwise extensions, density, and “all concepts are Kan extensions”.
- Emily Riehl, Category Theory in Context (2016), Chapter 6 — a modern account with many examples.
- Fosco Loregian, (Co)end Calculus (2021) — Kan extensions as coends, with applications.
- G. M. Kelly, Basic Concepts of Enriched Category Theory (1982), Chapter 4 — enriched and pointwise Kan extensions.
- Emily Riehl, Categorical Homotopy Theory (2014) — homotopy Kan extensions and derived functors.
- Daniel M. Kan, “Adjoint functors”, Transactions of the American Mathematical Society 87 (1958) — the original paper.
- Saunders Mac Lane and Ieke Moerdijk, Sheaves in Geometry and Logic (1992), Chapter I — presheaves, colimits of representables and the nerve–realisation adjunction.