The same pattern, again and again
Here are five facts from five different corners of mathematics.
- If and are functions, then so is ; composing is associative, and the identity function changes nothing.
- If and are group homomorphisms, so is ; the identity of a group is a homomorphism.
- If and are continuous maps of spaces, so is ; the identity map is continuous.
- If is an matrix and a matrix, then is a matrix; , and the identity matrix is neutral.
- If and then ; and .
The last two involve no functions at all — yet they have the same shape: things, arrows between things, a way to chain arrows end to end that does not care about brackets, and arrows that do nothing. In the lesson on composition you met this shape for sets and functions; in the lesson on structure-preserving maps you saw that homomorphisms compose too.
A category is exactly this shape, made precise. Studying it pays twice. First, a theorem proved once for all categories is proved for sets, groups, spaces, matrices and orders at the same time. Second, it changes how we ask questions: instead of looking inside an object at its elements, we look at how it relates to everything else through arrows. Surprisingly much can be seen that way — even whether a function is injective.
Dots, arrows and paths
Picture a category as a diagram of dots (the objects) and arrows between them (the morphisms). Every path of arrows laid end to end can be shortened to a single arrow, its composite, and it does not matter in which order you do the shortening. Each dot has a loop that "stays put", which you can insert into or delete from any path.
Two warnings, to be taken seriously from the start.
- The arrows need not be functions. In the category built from there is one arrow because , and no arrow . The arrow is not a rule that transforms anything; it is just the fact .
- The objects need not have elements. In the category of matrices below, the objects are the natural numbers , and a morphism is an matrix. Asking for "the elements of the object " makes no sense.
So everything we say about a category must be said with objects, arrows, composition and identities alone. That restriction is the source of category theory's power: a definition phrased only with arrows can be applied in every category at once.
Definitions
Definition— category
A category consists of
- a collection of objects ;
- for each pair of objects , a collection of morphisms (or arrows) from to ; we write ;
- for each object , an identity morphism ;
- for each pair , , a composite ;
such that for all , , :
Only composable pairs have a composite: is defined when the target of is the source of . A category is small if its objects and morphisms form sets, and locally small if each is a set. ( itself is locally small but not small: there is no set of all sets.)
Definition— isomorphism
A morphism is an isomorphism if there is a morphism with and . Such a is called an inverse of . Objects and are isomorphic, , if there is an isomorphism between them.
Definition— monomorphism and epimorphism
A morphism is a monomorphism (or is monic) if it can be cancelled on the left:
It is an epimorphism (or is epic) if it can be cancelled on the right: for all .
Definition— opposite category
The opposite of a category has the same objects, and one morphism for each morphism of . Composition is reversed: , and is the identity.
The axioms for are the axioms for read backwards, so is a category, and .
Examples, and some non-examples
Example— Categories of structured sets
| Category | Objects | Morphisms |
|---|---|---|
| sets | functions | |
| groups | group homomorphisms | |
| rings with | ring homomorphisms preserving | |
| vector spaces over a field | linear maps | |
| topological spaces | continuous maps |
In each case composition is composition of functions and the identities are identity functions. So associativity and the unit laws are inherited from (proved in the composition lesson); what must be checked is closure: that composites and identities are again morphisms. For : if are homomorphisms then , and . For : if is open then is open, and . For : composites and identities of linear maps are linear.
Example— Matrices: objects without elements
Fix a field . The category has as objects the natural numbers. A morphism is an matrix over (it turns column vectors of length into column vectors of length ); composition is matrix multiplication, , and . The axioms are and . An isomorphism is an invertible matrix, which forces (rank–nullity). The objects are bare numbers: this category is not "sets with structure".
Example— Preorders and posets
A preorder is a set with a reflexive, transitive relation . It becomes a category: the objects are the elements of , and there is exactly one morphism if , and none otherwise. Composition exists by transitivity, identities by reflexivity, and the laws hold automatically because between two objects there is at most one morphism, so any two parallel composites are equal.
Conversely, a category with at most one morphism between any two objects is a preorder: put when there is a morphism . Examples: ; the subsets of a set ordered by ; the positive integers ordered by divisibility, where and but neither nor .
In a preorder, exactly when and . In a poset (antisymmetric preorder) isomorphic objects are therefore equal.
Example— Monoids: categories with one object
A monoid — see algebraic structures — gives a category with a single object , morphisms the elements of , composition and . Associativity and the unit laws of the category are the monoid axioms. Conversely, the morphisms of any category form a monoid under composition, the endomorphism monoid ; so a category with one object is the same thing as a monoid.
Examples: — morphisms are numbers, and "first , then " is ; the clock monoid ; strings of letters under concatenation; the functions under composition, a monoid that is not commutative. An isomorphism in is an element with a two-sided inverse — a unit of . A group is a one-object category in which every morphism is an isomorphism.
Example— Two very small categories
The category has one object and only its identity. The category has two objects and, besides the identities, one morphism ; it is the poset . The morphism is both monic and epic (there is nothing to cancel), but it is not an isomorphism. So "monic and epic" does not imply "isomorphism" in general — although in it does.
Example— Non-examples
- The integers with an arrow whenever (strictly): composites exist, but there is no identity . Not a category.
- Sets, with morphisms the functions that are at most two-to-one. Identities qualify, but a composite of two two-to-one maps can be four-to-one. Composition is not closed. Not a category.
- A directed graph by itself is not a category: it has arrows but no rule for composing them. (Every directed graph does generate a category, whose morphisms are the paths — the free category on the graph.)
Isomorphisms, monos and epis
Theorem— Theorem 1 (inverses are unique)
Let . If satisfies (a left inverse) and satisfies (a right inverse), then . In particular an isomorphism has exactly one inverse, written .
Proof
Using only the unit laws and associativity,
If and are both two-sided inverses of , then is a left inverse and a right inverse, so .
∎
This is the same calculation that proves inverses in a monoid or group are unique — and now we see why: it uses nothing but the category axioms. The same goes for the next fact. If and are isomorphisms, so is , with inverse : indeed , and similarly on the other side. Hence is an equivalence relation on objects.
Theorem— Theorem 2 (monos and epis in Set)
In , a function is a monomorphism if and only if it is injective, and an epimorphism if and only if it is surjective.
Proof
Mono injective. Suppose is injective and for . For each , , so ; hence . Conversely suppose is monic and . Let be a one-element set and the maps with , . Then , so , so .
Epi surjective. Suppose is surjective and for . Each is for some , so ; hence . Conversely suppose is epic. Let , let be the indicator of the image, if for some and otherwise, and let be constant at . Then (both are constantly ), so , so every is in the image.
∎
The one-element set and the two-element set did all the work: elements of are maps , and subsets of are maps . That is how arrows can see inside objects in .
In other categories, monos and epis can behave differently. In , and the monomorphisms are again the injective morphisms. In every epimorphism is surjective too, but the standard proof is a genuinely clever argument with permutations of the cosets of the image; we do not give it here. In rings, it fails:
Theorem— Theorem 3 (an epimorphism that is not surjective)
In , the inclusion is an epimorphism, but it is not surjective.
Proof
It is not surjective because .
Let be ring homomorphisms with , i.e. for every integer . Take with , . In , , so in the element is a two-sided inverse of , and is a two-sided inverse of . Inverses in the monoid are unique (Theorem 1 for the one-object category ), so . Therefore . So .
∎
The same argument shows that the inclusion of monoids is an epimorphism of monoids: a homomorphism out of is determined by its values on , because must go to the inverse of the image of .
Theorem— Theorem 4 (isomorphisms in monoids and preorders)
(a) In the one-object category of a monoid, a morphism is an isomorphism exactly when is a unit of . (b) In a poset regarded as a category, implies .
Proof
(a) An inverse of the morphism is a morphism with and : precisely a two-sided inverse in . (b) An isomorphism provides morphisms and , that is and ; antisymmetry gives .
∎
So in only and are isomorphisms (compare the lesson on integers), and in the isomorphisms are the residues coprime to .
Duality
Every statement about categories has a dual, obtained by reversing every arrow and every composite: "" becomes "", "" becomes "", and the rest of the statement is kept. For instance, the dual of " is a monomorphism" is " is an epimorphism", and the dual of " has exactly one morphism to every object" is "every object has exactly one morphism to ".
Theorem— Theorem 5 (the duality principle)
If a statement about objects and morphisms holds in every category, so does its dual.
Proof
A statement holds of morphisms in exactly when its dual holds of the corresponding morphisms in , because composition in is composition in reversed. For example, is monic in iff , i.e. iff , i.e. iff is epic in . Now suppose holds in every category, and let be any category. Then holds in , so holds in .
∎
Example— One proof, two theorems
Claim: a composite of monomorphisms is a monomorphism. If , are monic and , then , so (cancel ), so (cancel ).
Dual claim, for free: a composite of epimorphisms is an epimorphism. Likewise, "if is monic then is monic" dualises to "if is epic then is epic", and "every isomorphism is monic" dualises to "every isomorphism is epic".
Categories in Lean and Mathlib
Mathlib has a large library of category theory built on the class CategoryTheory.Category. Two conventions to know before reading the code: composition is written in diagrammatic order, f ≫ g meaning "first f, then g" (our ), and the identity on X is 𝟙 X.
Theorem 1 is the calculation of the lesson, line by line, followed by its corollary and Mathlib's version:
/-- **A left inverse equals a right inverse.** In any category, if `g ∘ f = id_X` and
`f ∘ h = id_Y` then `g = h`. In Mathlib's diagrammatic notation `g ∘ f` is `f ≫ g`.
The proof is the one-line calculation of the lesson:
`g = g ∘ id_Y = g ∘ (f ∘ h) = (g ∘ f) ∘ h = id_X ∘ h = h`, using only the identity and
associativity axioms. Consequently an isomorphism has exactly one inverse. -/
theorem left_inv_eq_right_inv {C : Type u} [Category.{v} C] {X Y : C} {f : X ⟶ Y}
{g h : Y ⟶ X} (hg : f ≫ g = 𝟙 X) (hh : h ≫ f = 𝟙 Y) : g = h :=
calc g = 𝟙 Y ≫ g := (Category.id_comp g).symm
_ = (h ≫ f) ≫ g := by rw [hh]
_ = h ≫ (f ≫ g) := Category.assoc h f g
_ = h ≫ 𝟙 X := by rw [hg]
_ = h := Category.comp_id h/-- **Inverses are unique.** If `g` and `g'` are both two-sided inverses of `f`, then `g = g'`.
This is `left_inv_eq_right_inv` applied to the left inverse `g` and the right inverse `g'`. -/
theorem inverse_unique {C : Type u} [Category.{v} C] {X Y : C} {f : X ⟶ Y} {g g' : Y ⟶ X}
(hg : f ≫ g = 𝟙 X ∧ g ≫ f = 𝟙 Y) (hg' : f ≫ g' = 𝟙 X ∧ g' ≫ f = 𝟙 Y) : g = g' :=
left_inv_eq_right_inv hg.1 hg'.2/-- Mathlib's version: if `f` is an isomorphism (`IsIso f`), its chosen inverse `inv f` equals
any `g` with `f ≫ g = 𝟙 X`. This applies `CategoryTheory.IsIso.inv_eq_of_hom_inv_id`. -/
theorem inv_eq_library {C : Type u} [Category.{v} C] {X Y : C} (f : X ⟶ Y) [IsIso f]
(g : Y ⟶ X) (hg : f ≫ g = 𝟙 X) : inv f = g :=
IsIso.inv_eq_of_hom_inv_id hgThe category of matrices is a genuine instance of Mathlib's class. Its objects are natural numbers (wrapped in a structure so Lean does not confuse them with the order on ); the proofs of the axioms are the laws of matrix multiplication:
/-- The objects of the category of matrices: one object for each natural number `dim`
(a wrapper around `ℕ`, so that Lean does not confuse this category with the order on `ℕ`). -/
structure Mat where
/-- The size of the column vectors at this object. -/
dim : ℕ/-- **The category of rational matrices.** Objects are natural numbers `m, n, …`; a morphism
`m ⟶ n` is an `n × m` matrix (it turns column vectors of length `m` into vectors of length
`n`); composition is matrix multiplication (`f ≫ g = g * f`, "first `f`, then `g`") and the
identity on `n` is the identity matrix. The axioms are the associativity and unit laws of
matrix multiplication (`Matrix.mul_assoc`, `Matrix.one_mul`, `Matrix.mul_one`). The objects are
just numbers: a morphism is not a function between sets of elements. -/
instance matCategory : Category.{0} Mat where
Hom m n := Matrix (Fin n.dim) (Fin m.dim) ℚ
id n := (1 : Matrix (Fin n.dim) (Fin n.dim) ℚ)
comp f g := g * f
id_comp f := Matrix.mul_one f
comp_id f := Matrix.one_mul f
assoc f g h := (Matrix.mul_assoc h g f).symmMathlib already regards every preorder as a category and every monoid as a one-object category (SingleObj). We use these to check two claims of the lesson: parallel morphisms in a preorder are equal, isomorphic elements of a poset are equal, and isomorphisms in are the units of :
/-- In a preorder regarded as a category (Mathlib's instance `Preorder.smallCategory`), there is
a morphism `x ⟶ y` exactly when `x ≤ y`, and **any two morphisms `x ⟶ y` are equal**: a morphism
carries no information beyond the fact `x ≤ y`. This applies `Subsingleton.elim` to Mathlib's
instance saying that hom-types of a preorder category are subsingletons. -/
theorem preorder_hom_unique {α : Type u} [Preorder α] {x y : α} (f g : x ⟶ y) : f = g :=
Subsingleton.elim f g/-- In a **partial order** regarded as a category, isomorphic objects are equal: an isomorphism
`x ≅ y` provides morphisms `x ⟶ y` and `y ⟶ x`, that is `x ≤ y` and `y ≤ x`, and antisymmetry
gives `x = y`. (In a preorder that is not antisymmetric, distinct objects can be isomorphic.) -/
theorem preorder_iso_eq {α : Type u} [PartialOrder α] {x y : α} (e : x ≅ y) : x = y :=
le_antisymm (leOfHom e.hom) (leOfHom e.inv)/-- **A monoid is a category with one object.** Mathlib's `SingleObj M` has a single object
`star M`; its morphisms are the elements of `M`, the identity is `1` and composition is
multiplication: "first `x`, then `y`" is `y * x`. This applies `SingleObj.comp_as_mul`. -/
theorem monoid_comp (M : Type u) [Monoid M] (x y : SingleObj.star M ⟶ SingleObj.star M) :
x ≫ y = y * x :=
SingleObj.comp_as_mul M x y/-- In the one-object category of a monoid `M`, **the isomorphisms are exactly the invertible
elements (units) of `M`.** An inverse of the morphism `x` is an element `y` with
`x ≫ y = y * x = 1` and `y ≫ x = x * y = 1`. So "isomorphism" generalises "invertible element"
— for the monoid `(ℤ, ×)` only `1` and `-1` are isomorphisms. -/
theorem monoid_isIso_iff (M : Type u) [Monoid M] (x : SingleObj.star M ⟶ SingleObj.star M) :
IsIso x ↔ IsUnit (show M from x) := by
constructor
· intro hx
-- the inverse of `x` in the category is a two-sided inverse in the monoid
have h1 : x * inv x = 1 := IsIso.inv_hom_id x
have h2 : inv x * x = 1 := IsIso.hom_inv_id x
exact ⟨⟨x, inv x, h1, h2⟩, rfl⟩
· rintro ⟨u, hu⟩
refine ⟨⟨(u⁻¹ : Mˣ), ?_, ?_⟩⟩
· show (u⁻¹ : Mˣ) * x = 1
rw [← hu, Units.inv_mul]
· show x * (u⁻¹ : Mˣ) = 1
rw [← hu, Units.mul_inv]Theorem 2, for Lean types and functions (the type-theoretic counterpart of ). The proofs use exactly the test objects of the lesson: the one-element type PUnit, and the type of propositions (truth values) in place of . Mathlib's category Type u wraps its morphisms in a structure, and it proves the same facts there:
/-- **In sets, the monomorphisms are the injections.** A function `f : X → Y` is a
monomorphism if it can be cancelled on the left: `f ∘ g = f ∘ h` implies `g = h` for all
`g, h : Z → X`. (Stated here for Lean types and functions, the type-theoretic version of
**Set**.) If `f` is a monomorphism and `f a = f b`, test it against the two maps from a
one-element set picking out `a` and `b`; conversely an injection can be cancelled pointwise. -/
theorem mono_iff_injective {X Y : Type u} (f : X → Y) :
(∀ (Z : Type u) (g h : Z → X), f ∘ g = f ∘ h → g = h) ↔ Function.Injective f := by
constructor
· intro hf a b hab
-- test maps from the one-element type `PUnit`
have := hf PUnit (fun _ => a) (fun _ => b) (funext fun _ => hab)
exact congrFun this PUnit.unit
· intro hf Z g h hgh
funext z
exact hf (congrFun hgh z)/-- **In sets, the epimorphisms are the surjections.** A function `f : X → Y` is an
epimorphism if `g ∘ f = h ∘ f` implies `g = h` for all `g, h : Y → Z`.
If `f` is an epimorphism, compare two maps into the two-element "set of truth values":
`g y = (y is in the image of f)` and `h y = true`. They agree after `f`, so they agree, so every
`y` is in the image. Conversely, if `f` is surjective, every `y` is some `f x`, where `g` and `h`
agree. (`ULift` only moves the type of propositions into the universe of `Y`.) -/
theorem epi_iff_surjective {X Y : Type u} (f : X → Y) :
(∀ (Z : Type u) (g h : Y → Z), g ∘ f = h ∘ f → g = h) ↔ Function.Surjective f := by
constructor
· intro hf y
let g : Y → ULift.{u} Prop := fun y => ULift.up (∃ x, f x = y)
let h : Y → ULift.{u} Prop := fun _ => ULift.up True
have hgh : g ∘ f = h ∘ f := by
funext x
simp only [Function.comp, g, h, ULift.up.injEq, eq_iff_iff, iff_true]
exact ⟨x, rfl⟩
have hy : g y = h y := congrFun (hf _ g h hgh) y
simp only [g, h, ULift.up.injEq, eq_iff_iff, iff_true] at hy
exact hy
· intro hf Z g h hgh
funext y
obtain ⟨x, rfl⟩ := hf y
exact congrFun hgh x/-- Mathlib's versions for its category `Type u` (whose morphisms wrap functions): a morphism is
a monomorphism iff it is injective and an epimorphism iff it is surjective. This applies
`CategoryTheory.mono_iff_injective` and `CategoryTheory.epi_iff_surjective`. -/
theorem types_mono_epi_library {X Y : Type u} (f : X ⟶ Y) :
(Mono f ↔ Function.Injective f) ∧ (Epi f ↔ Function.Surjective f) :=
⟨CategoryTheory.mono_iff_injective f, CategoryTheory.epi_iff_surjective f⟩Theorem 3: the independent proof follows the lesson (inverses are unique), then states the result as an epimorphism in Mathlib's category RingCat. Mathlib knows something stronger — any two ring homomorphisms out of are equal — because they automatically agree on :
/-- **The inclusion `ℤ → ℚ` is an epimorphism of rings**: two ring homomorphisms
`f, g : ℚ → R` that agree on the integers agree everywhere.
Proof, as in the lesson: write `q = a / b` with `b ≠ 0`. In `R`, `f (1/b)` is a two-sided
inverse of `f b`, and `g (1/b)` is a two-sided inverse of `g b = f b`; inverses in a monoid are
unique (Mathlib's monoid lemma `left_inv_eq_right_inv`), so `f (1/b) = g (1/b)`, and then
`f q = f a · f (1/b) = g a · g (1/b) = g q`. -/
theorem rat_hom_eq_of_eq_on_int {R : Type u} [Ring R] (f g : ℚ →+* R)
(h : ∀ n : ℤ, f n = g n) : f = g := by
ext q
have hb : (q.den : ℚ) ≠ 0 := by exact_mod_cast q.den_nz
-- `f (1/b)` and `g (1/b)` are inverses of the same element `f b = g b`
have hfb : f (q.den : ℚ) = g (q.den : ℚ) := by
exact_mod_cast h q.den
have hinv : f (q.den : ℚ)⁻¹ = g (q.den : ℚ)⁻¹ := by
have left : f (q.den : ℚ)⁻¹ * f (q.den : ℚ) = 1 := by
rw [← map_mul, inv_mul_cancel₀ hb, map_one]
have right : g (q.den : ℚ) * g (q.den : ℚ)⁻¹ = 1 := by
rw [← map_mul, mul_inv_cancel₀ hb, map_one]
rw [hfb] at left
exact _root_.left_inv_eq_right_inv left right
calc f q = f ((q.num : ℚ) * (q.den : ℚ)⁻¹) := by rw [← div_eq_mul_inv, Rat.num_div_den]
_ = f q.num * f (q.den : ℚ)⁻¹ := by rw [map_mul]
_ = g q.num * g (q.den : ℚ)⁻¹ := by rw [h q.num, hinv]
_ = g ((q.num : ℚ) * (q.den : ℚ)⁻¹) := by rw [map_mul]
_ = g q := by rw [← div_eq_mul_inv, Rat.num_div_den]/-- The same fact stated in Mathlib's category `RingCat` of rings: the morphism
`ℤ ⟶ ℚ` given by the inclusion is an epimorphism (`Epi`). Two morphisms `g, h : ℚ ⟶ R` with
`ι ≫ g = ι ≫ h` agree on every integer `n`, so `rat_hom_eq_of_eq_on_int` makes them equal. -/
theorem int_to_rat_epi : Epi (RingCat.ofHom (Int.castRingHom ℚ)) := by
constructor
intro R g h hgh
apply RingCat.hom_ext
apply rat_hom_eq_of_eq_on_int
intro n
have := congrArg (fun φ => φ.hom n) hgh
simp only [RingCat.hom_comp, RingCat.hom_ofHom, RingHom.comp_apply, Int.coe_castRingHom]
at this
exact this/-- Mathlib's stronger statement: *any* two ring homomorphisms from `ℚ` to a semiring are equal
(they automatically agree on `ℤ`). This applies `RingHom.ext_rat`. -/
theorem rat_hom_ext_library {R : Type u} [Semiring R] (f g : ℚ →+* R) : f = g :=
RingHom.ext_rat f g/-- …but the inclusion `ℤ → ℚ` is **not surjective**: `1/2` is not an integer. If `n = 1/2` in
`ℚ`, then `2n = 1` in `ℚ`, hence in `ℤ`, which is impossible for an integer `n`. So in the
category of rings, epimorphisms need not be surjective. -/
theorem int_cast_not_surjective : ¬ Function.Surjective (Int.cast : ℤ → ℚ) := by
intro hsurj
obtain ⟨n, hn⟩ := hsurj (1 / 2)
have h2 : ((2 * n : ℤ) : ℚ) = 1 := by
push_cast
rw [hn]
norm_num
have h3 : 2 * n = 1 := by exact_mod_cast h2
omegaFinally, composites of monos, and duality: f.op is the morphism of the opposite category Cᵒᵖ corresponding to f.
/-- **A composite of monomorphisms is a monomorphism.** If `f : X ⟶ Y` and `g : Y ⟶ Z` are
monomorphisms and `a ≫ (f ≫ g) = b ≫ (f ≫ g)`, then `(a ≫ f) ≫ g = (b ≫ f) ≫ g`, so cancelling
`g` gives `a ≫ f = b ≫ f`, and cancelling `f` gives `a = b`. By duality (reverse every arrow)
the same proof shows that a composite of epimorphisms is an epimorphism. -/
theorem mono_comp {C : Type u} [Category.{v} C] {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z)
(hf : Mono f) (hg : Mono g) : Mono (f ≫ g) := by
constructor
intro W a b hab
have h1 : (a ≫ f) ≫ g = (b ≫ f) ≫ g := by
rw [Category.assoc, Category.assoc]
exact hab
have h2 : a ≫ f = b ≫ f := hg.right_cancellation _ _ h1
exact hf.right_cancellation _ _ h2/-- The dual statement, as Mathlib states it: a composite of epimorphisms is an epimorphism.
This applies the instance `CategoryTheory.epi_comp`. -/
theorem epi_comp_library {C : Type u} [Category.{v} C] {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z)
[Epi f] [Epi g] : Epi (f ≫ g) :=
epi_comp f g/-- **Duality.** A morphism `f : X ⟶ Y` of `C` is a monomorphism exactly when the corresponding
morphism `f.op : Y ⟶ X` of the opposite category `Cᵒᵖ` is an epimorphism. Composition in `Cᵒᵖ`
is composition in `C` reversed (`(g ≫ f).op = f.op ≫ g.op`), so "cancellable on the left" in
`C` is literally "cancellable on the right" in `Cᵒᵖ`. -/
theorem mono_iff_epi_op {C : Type u} [Category.{v} C] {X Y : C} (f : X ⟶ Y) :
Mono f ↔ Epi f.op := by
constructor
· intro hf
constructor
intro Z g h hgh
-- `f.op ≫ g = (g.unop ≫ f).op`
apply Quiver.Hom.unop_inj
apply hf.right_cancellation
exact Quiver.Hom.op_inj (by simp at hgh ⊢; exact hgh)
· intro hf
constructor
intro Z g h hgh
apply Quiver.Hom.op_inj
apply hf.left_cancellation
simpa using congrArg Quiver.Hom.op hgh/-- Mathlib's version of the same duality, `CategoryTheory.op_epi_iff`. -/
theorem op_epi_iff_library {C : Type u} [Category.{v} C] {X Y : C} (f : X ⟶ Y) :
Epi f.op ↔ Mono f :=
CategoryTheory.op_epi_iff fWhat is not formalised here: the duality principle as a single meta-theorem about all statements (it is a statement about the language we use, and Lean expresses each instance separately), and the theorem that epimorphisms of groups are surjective.
Deciding mono and epi by brute force
The definitions of monomorphism and epimorphism quantify over every test object . For finite sets a computer can simply try them. The experiment below lists every function between sets with at most three elements, tests both cancellation properties against every map from or to a set with at most two elements, and compares the results with injectivity and surjectivity. It also asks which test sets are needed — and finds the one-element and two-element sets of the proof.
"""Monomorphisms and epimorphisms in finite sets, decided by brute force.
A function f : A -> B is a monomorphism if f . g = f . h forces g = h for
all maps g, h : T -> A, and an epimorphism if g . f = h . f forces g = h
for all maps g, h : B -> T. Both definitions quantify over *every* test
set T. This experiment enumerates every function between sets with at most
three elements, tests the definitions against all test sets with at most
two elements, and compares the answers with injectivity and surjectivity.
It also shows which test sets are needed: a one-element T already detects
monos, but epis need a two-element T. The evidence is exhaustive for these
small sets; the lesson's proof covers all sets.
"""
from itertools import product
def functions(m, n):
"""All functions {0..m-1} -> {0..n-1}, each as a tuple of values."""
return list(product(range(n), repeat=m))
def compose(g, f):
"""The composite g . f of tuples: first f, then g."""
return tuple(g[x] for x in f)
def is_mono(f, n, test_sizes):
"""Is f : m -> n left-cancellable against maps from sets of these sizes?"""
m = len(f)
for t in test_sizes:
maps = functions(t, m)
for g in maps:
for h in maps:
if g != h and compose(f, g) == compose(f, h):
return False
return True
def is_epi(f, n, test_sizes):
"""Is f : m -> n right-cancellable against maps to sets of these sizes?"""
for t in test_sizes:
maps = functions(n, t)
for g in maps:
for h in maps:
if g != h and compose(g, f) == compose(h, f):
return False
return True
def is_injective(f):
"""No two inputs share an output."""
return len(set(f)) == len(f)
def is_surjective(f, n):
"""Every element of {0..n-1} is a value."""
return set(f) == set(range(n))
def tally(m, n, test_sizes=(0, 1, 2)):
"""Counts (functions, monos, injections, epis, surjections) for m -> n."""
fs = functions(m, n)
return (
len(fs),
sum(is_mono(f, n, test_sizes) for f in fs),
sum(is_injective(f) for f in fs),
sum(is_epi(f, n, test_sizes) for f in fs),
sum(is_surjective(f, n) for f in fs),
)
def main():
print("|A| |B| maps monos injective epis surjective")
agree = True
for m in range(4):
for n in range(4):
total, mono, inj, epi, surj = tally(m, n)
agree = agree and mono == inj and epi == surj
print(f"{m:>3} {n:>3} {total:>5} {mono:>6} {inj:>10}"
f" {epi:>5} {surj:>11}")
print(f"monos = injections and epis = surjections: {agree}")
print()
print("Which test sets T are needed?")
fake = sum(is_epi(f, 3, (1,)) for f in functions(2, 3))
print(f" with |T| = 1 only, {fake} of the 9 maps 2 -> 3 pass the epi test,")
print(" although none of them is surjective")
ok = all(is_mono(f, 3, (1,)) == is_injective(f) for f in functions(3, 3))
print(f" |T| = 1 already decides mono for all 27 maps 3 -> 3: {ok}")
print()
print("Epistemic status: exhaustive and exact for sets with at most three")
print("elements, testing against all sets with at most two elements. It is")
print("evidence, not proof, that the pattern continues; the lesson proves")
print("mono = injective and epi = surjective for all sets, using a")
print("one-element and a two-element test set exactly as found here.")
if __name__ == "__main__":
main()This is exhaustive evidence for small sets, not a proof for all sets. But notice what it suggests and what the proof confirms: one-element test sets suffice for monos, while epis need a two-element test set (against one-element test sets every function looks epic).
Explore: paths compose
Build composites along paths in a small diagram of finite sets. Choose arrows one after another; the widget refuses a step when the arrows do not line up (the target of one must be the source of the next). Compare the two bracketings of , and insert identities anywhere in a path to see that nothing changes. Then ask yourself which of the arrows are monomorphisms or epimorphisms of .
Arrows that compose
Click arrows to build a path. Every path composes to a single arrow — and brackets never matter.
Your composite
Start by clicking any arrow.
Two ways to bracket h ∘ g ∘ f
| x | (g∘f)(x) | h((g∘f)(x)) | (h∘g)(f(x)) |
|---|---|---|---|
| 1 | x | 1 | 1 |
| 2 | z | 0 | 0 |
| 3 | x | 1 | 1 |
The last two columns always agree: h ∘ (g ∘ f) = (h ∘ g) ∘ f. That is associativity.
Exercises
1. Categories
Warm-upHumanWhich of these are categories (with the obvious composition)? Choose all that apply.
2. Categories
Warm-upHumanRegard the set , ordered by divisibility, as a category. How many morphisms does it have in total, including the identities?
3. Categories
CoreHumanLet be the monoid of residues modulo under multiplication, and its one-object category. How many of the morphisms of are isomorphisms?
4. Categories
CoreHumanIn the proof that a left inverse and a right inverse of are equal, which axiom justifies the step ?
5. Categories
CoreHumanWhich of these are epimorphisms that are not surjective? Choose all that apply.
6. Categories
CoreHumanTheorem: in every category, if is a monomorphism then is a monomorphism. What does the duality principle give us for free?
7. Categories
CoreFormalMathlib writes composition in diagrammatic order: a ≫ f means "first a, then f", i.e. . Which statement says that f : X ⟶ Y is a monomorphism?
8. Categories
StretchComputationalA function between finite sets is stored as a tuple f of length with values in range(n). Write is_epi(f, n) that decides whether f is an epimorphism directly from the definition, by testing cancellation against all pairs of maps (that is, k ∘ f == l ∘ f must force k == l). Do not test surjectivity directly!
Common misconceptions
Misconception
"Morphisms are functions." In many categories they are, but not in general. In a poset regarded as a category, a morphism is just the fact that ; in it is a matrix; in the category of a monoid it is an element. Definitions must use only composition and identities.
Misconception
"Epimorphisms are surjective." True in and , false in general: the inclusion is an epimorphism of rings, and an epimorphism of monoids. Being epic means being cancellable on the right, nothing more.
Misconception
"Objects have elements." The objects of are numbers and the single object of is a placeholder. Even in , category theory recovers elements of only as morphisms .
Misconception
"Monic and epic means isomorphism." In yes (a bijection has an inverse function). But in the category the arrow is monic and epic and not invertible, and in the inclusion is monic and epic and not an isomorphism.
Misconception
"Being an isomorphism is a property of the underlying function." It depends on the category. The identity map from with the discrete topology to with the usual topology is a continuous bijection — an isomorphism in — but not an isomorphism in , because its inverse is not continuous.
Many ways of seeing
Topology— Homeomorphisms, and paths as morphisms
In the isomorphisms are the homeomorphisms: continuous bijections whose inverses are continuous. The extra condition matters. The map from to the circle is a continuous bijection, but and are not homeomorphic: removing any point disconnects unless it is the endpoint , while removing any point of leaves it connected — and connectedness of complements is preserved by homeomorphisms. So "isomorphic" means "the same as objects of this category", and topology needs its own notion.
Topology also supplies categories whose morphisms are not maps at all. In the fundamental groupoid of a space, the objects are the points of and a morphism is a path from to up to homotopy with fixed ends; composition is "follow one path, then the other". Associativity holds only up to homotopy for actual paths — which is why one passes to homotopy classes — and every morphism is invertible (run the path backwards). The automorphisms of a point form the fundamental group : see homotopy and the fundamental group.
Geometry— Groups are one-object categories of symmetries
A group is a category with one object in which every morphism is invertible. Geometrically, think of the object as a shape and the morphisms as its symmetries: for a square, the eight rotations and reflections of the dihedral group , composed by doing one motion after another. Klein's Erlangen programme (1872) organised geometry by groups of transformations: Euclidean geometry studies what is invariant under isometries, affine geometry what is invariant under affine maps, projective geometry under projective maps. Category theory generalises this from one shape and its symmetries to many shapes and the structure-preserving maps between them — see transformation geometry.
Algebra— Matrices are the category of finite-dimensional vector spaces
Choosing a basis of each finite-dimensional vector space identifies a linear map with an matrix and composition of linear maps with matrix multiplication. So the category , whose objects are mere numbers, captures all of finite-dimensional linear algebra: every finite-dimensional space is isomorphic to some , and the isomorphisms form the general linear group . (Making "captures" precise needs the notion of an equivalence of categories, a later topic.) Algebra is full of such recognitions: monoids are one-object categories, groups are one-object categories with inverses, and a ring is a one-object category whose hom-set is an abelian group with bilinear composition — a preadditive category with one object — which is the starting point of the categorical approach to homological algebra.
Logic— Propositions and proofs
Logical propositions ordered by entailment form a preorder, hence a category: there is a morphism when follows from , identities are " entails ", and composition is chaining entailments. Refining this, one can keep track of which proof shows from : propositions as objects, proofs (up to a suitable equivalence) as morphisms, composition as substituting one proof into another. This is the beginning of categorical logic and of the Curry–Howard–Lambek correspondence between proofs, programs and morphisms of cartesian closed categories — see categorical logic.
Computation— Types and programs compose
In a typed programming language, take the types as objects and the (total, side-effect-free) functions between them as morphisms; composition is running one function on the output of another, and the identity function returns its input. This is why many functional programming languages put composition (. in Haskell, >> in F#) at the centre, and why notions such as functors and monads moved from category theory into programming. The analogy has honest limits: real programs may loop forever or have side effects, so the actual category of a language must be defined with care.
Wonder Trails
History and global context
- Samuel Eilenberg (born in Warsaw, 1913) and Saunders Mac Lane (born in Connecticut, 1909) introduced categories, functors and natural transformations in "General theory of natural equivalences" (Transactions of the AMS, 1945). Their goal was to make precise the word "natural" in algebraic topology; categories were introduced, as they later said, to be able to define functors, and functors to define natural transformations.
- In the 1950s category theory became a working language. Eilenberg and Norman Steenrod axiomatised homology theory with it (announced 1945, in book form 1952); Alexander Grothendieck's "Tôhoku paper" (1957) introduced abelian categories to unify sheaf cohomology and homological algebra; Daniel Kan (1958) introduced adjoint functors.
- F. William Lawvere's thesis (1963) and later work used categories as a foundation for algebra and logic; topos theory followed (Lawvere and Myles Tierney, around 1970). Major schools developed in many places, among them the Sydney school of Max Kelly and Ross Street, and Montreal, Louvain-la-Neuve and Genoa.
- Mac Lane's Categories for the Working Mathematician (1971) became the standard reference. Today categories are used across algebra, topology, logic, computer science and mathematical physics.
Further reading
- Tom Leinster, Basic Category Theory (2014), chapter 1 — concise and friendly, with many examples; freely available from the author.
- Steve Awodey, Category Theory (2006; 2nd ed. 2010) — written with logicians and computer scientists in mind.
- Saunders Mac Lane, Categories for the Working Mathematician (1971; 2nd ed. 1998) — the classic.
- Emily Riehl, Category Theory in Context (2016) — rich in examples from across mathematics.
- F. William Lawvere and Stephen H. Schanuel, Conceptual Mathematics (2nd ed. 2009) — a gentle first introduction.