Foundations
Category Theory
Categories, functors, natural transformations, universal properties, adjunctions, and the Yoneda lemma. The language of abstract structure that unifies algebra, topology, logic, and increasingly appears in ML theory.
Why This Matters
Category theory provides a language for describing structure-preserving maps between mathematical objects. Algebra, topology, logic, and programming language theory all use this language. In ML theory, category theory appears in equivariant neural networks (functors preserving group actions), type-theoretic foundations of programming languages (Cartesian closed categories), and the algebraic structure of probability (the Giry monad).
For most ML practitioners, category theory is not a prerequisite for daily work. But for anyone studying equivariant learning, compositional semantics, or the algebraic foundations of probability, it provides precisely the right abstractions. The Yoneda lemma alone justifies learning the basics: it says that an object is completely determined by how other objects map into it.
Core Definitions
Category
A category consists of:
- A collection of objects
- For each pair of objects , a collection of morphisms (arrows) from to
- For each object , an identity morphism
- A composition operation: for and , a morphism
Subject to two axioms:
- Associativity: for all composable triples
- Identity: and for all
Functor
A functor between categories assigns:
- To each object , an object
- To each morphism in , a morphism in
Such that:
- (preserves identities)
- (preserves composition)
A functor is a structure-preserving map between categories.
Natural Transformation
Given functors , a natural transformation assigns to each object a morphism in , such that for every morphism in , the following square commutes:
The morphisms are called the components of . Naturality means the transformation is compatible with all morphisms in the source category.
Standard Examples
The following categories appear throughout mathematics and computer science:
| Category | Objects | Morphisms |
|---|---|---|
| Set | Sets | Functions |
| Grp | Groups | Group homomorphisms |
| Vect | Vector spaces over | Linear maps |
| Top | Topological spaces | Continuous maps |
| Meas | Measurable spaces | Measurable functions |
| Poset | Elements of a poset | gives a unique arrow |
The forgetful functor
The forgetful functor sends each group to its underlying set , and each group homomorphism to itself (viewed as a function between sets). It "forgets" the group structure. Similarly, forgets the vector space structure.
The free functor
The free functor sends a set to the free group on (all finite words in modulo group axioms). A function extends uniquely to a group homomorphism . The free functor and the forgetful functor form an adjunction.
Natural transformation: determinant
Consider two functors from the category of commutative rings to the category of groups: the general linear group functor and the units functor . The determinant defines a natural transformation. Naturality means: for any ring homomorphism , applying entry-wise to a matrix and then taking the determinant equals taking the determinant and then applying .
Universal Properties
Universal Property
An object in a category has a universal property with respect to a construction if every instance of that construction factors uniquely through . Formally, is an initial object in a suitable category of solutions. Universal properties determine objects up to unique isomorphism.
Products, coproducts, limits, colimits, free objects, and tensor products are all defined by universal properties. The power of this approach: instead of constructing an object explicitly and then verifying properties, you specify the universal property and deduce that at most one object (up to isomorphism) can satisfy it.
Product as a universal property
The product of objects and in a category is an object equipped with projection morphisms and , such that for any object with morphisms and , there exists a unique morphism with and . In Set, this is the Cartesian product. In Grp, it is the direct product. In Top, it is the product topology.
The Yoneda Lemma
The Yoneda lemma is the most important result in basic category theory. It says that an object is completely characterized by its relationships with all other objects.
Representable Functor
For an object in a locally small category , the covariant representable functor sends each object to the set and each morphism to the function .
Yoneda Lemma
Statement
For any functor and any object , there is a bijection:
that is natural in both and . Here denotes the set of natural transformations from the representable functor to .
Intuition
A natural transformation is completely determined by . Given any element , you can reconstruct the entire natural transformation by defining for each . Naturality forces this to be the only possibility. So the "space of natural transformations out of a representable" is just the set .
Proof Sketch
Define the bijection explicitly. Given a natural transformation , map it to . Conversely, given , define for each .
To show is natural: for any , we need . Evaluating at : . Functoriality of gives the third equality.
To show this is a bijection: if is any natural transformation, then naturality forces , so is determined by .
Why It Matters
The Yoneda lemma has a corollary called the Yoneda embedding: the functor is a full and faithful embedding of into the functor category . This means every category embeds into a category of "generalized sets." Two objects and are isomorphic if and only if and are naturally isomorphic: an object is determined by its morphisms.
Failure Mode
The Yoneda lemma requires a locally small category (hom-sets are actual sets, not proper classes). For large categories where hom-collections are proper classes, the statement must be reformulated using universe enlargement or other size-management techniques. In practice, all categories arising in applications are locally small.
Adjunctions
Adjunction
An adjunction between categories and consists of functors (the left adjoint) and (the right adjoint) together with a natural bijection:
for all objects and . We write .
Adjunctions are everywhere:
| Left adjoint | Right adjoint | Setting |
|---|---|---|
| Free group | Forgetful functor | Grp Set |
| Set (currying) | ||
| Free vector space | Forgetful functor | Vect Set |
| (existential) | Pullback | Dependent type theory |
The currying adjunction is the categorical statement that a function of two arguments is the same as a function returning a function. This is the foundation of the Curry-Howard-Lambek correspondence connecting logic, computation, and category theory.
When Category Theory Matters (and When It Does Not)
For ML practitioners, category theory is genuinely useful in these specific areas:
- Equivariant neural networks: a -equivariant map is precisely a natural transformation between certain functors. The classification of equivariant linear maps uses Schur's lemma, which is cleanly stated categorically.
- Type theory and programming languages: Cartesian closed categories model the simply typed lambda calculus. Monads (a categorical concept) structure effects in functional programming.
- Compositional semantics: DisCoCat models in NLP use monoidal functors to map grammatical structure to vector space computations.
- Algebraic probability: the Giry monad gives a categorical framework for probability measures.
For most applied ML work (training models, tuning hyperparameters, engineering features), category theory adds no practical value. The honest assessment: learn it if you work on the topics above or if you want the conceptual unification it provides. Skip it if your work is empirical.
Common Confusions
Categories are not just sets with extra structure
A common first impression is that a category is "a set of objects with arrows." But the objects of a category need not form a set (they can form a proper class, as in Set itself). The morphisms carry the real information: two categories with the same objects but different morphisms are completely different. The arrows, not the objects, are what matter.
Functors preserve structure; they do not create it
A functor must preserve composition and identities. It cannot "add" relationships that do not exist in the source. If there is no morphism from to in , the functor says nothing about the relationship between and beyond what is implied by the existing morphisms.
Isomorphism in a category is not equality
Two objects and are isomorphic () if there exist morphisms and with and . In Set, this means a bijection. In Top, a homeomorphism. Category theory works up to isomorphism, not equality. The principle of equivalence: no categorical construction should distinguish between isomorphic objects.
Exercises
Problem
Verify that vector spaces over a field and linear maps form a category Vect. Specifically, check that composition of linear maps is linear and that the identity map is linear.
Problem
Prove the Yoneda lemma for the special case . That is, show:
and explain why this means the Yoneda embedding is full and faithful.
Problem
Show that the free-forgetful adjunction between Grp and Set satisfies by describing both sides explicitly when and .
Problem
A -equivariant neural network layer is a map between -representations satisfying for all . Express this condition as a natural transformation between appropriate functors. What does the Yoneda perspective tell you about classifying such maps?
References
Canonical:
- Mac Lane, Categories for the Working Mathematician (1998), Chapters 1-4 for categories, functors, natural transformations, and adjunctions
- Awodey, Category Theory (2010), Chapters 1-8 for a modern introduction at the graduate level
- Riehl, Category Theory in Context (2016), Chapters 1-6, particularly Chapter 2 for the Yoneda lemma
Accessible:
- Leinster, Basic Category Theory (2014), Chapters 1-4 for a concise treatment aimed at beginners
- Fong & Spivak, An Invitation to Applied Category Theory (2019), for applications-oriented readers
ML connections:
- Shiebler, Gavranovic, Wilson, "Category Theory in Machine Learning" (2021 survey), Sections 2-5 on equivariant networks and compositional models
Next Topics
- Sets, functions, and relations: the prerequisite set theory that categories generalize
- Dependent type theory: where Cartesian closed categories meet proof theory
Last reviewed: April 2026
Prerequisites
Foundations this topic depends on.
- Sets, Functions, and RelationsLayer 0A
- Basic Logic and Proof TechniquesLayer 0A