foundational category-theory 45 min read

Categories & Functors

Objects, morphisms, and functors — the abstract language that unifies disparate ML structures through composition

Overview & Motivation

Machine learning is built on composition. A neural network is a sequence of layers applied one after the other. A data pipeline chains transformations end-to-end. A probabilistic model threads random variables through a sequence of conditional distributions. In every case, the structure that matters is not what the individual pieces are but how they compose.

Category theory makes composition the central idea. A category is a collection of objects and morphisms (structure-preserving maps) between them, equipped with a composition operation that satisfies two axioms: associativity and the existence of identities. That’s it — and from these two axioms, an enormous amount of structure follows.

Why should an ML practitioner care? Because the categories that matter to us are the ones we already work in:

  • Vec — the category of vector spaces and linear maps. Every neural network layer is a morphism in Vec (or a nonlinear enrichment of it). The Spectral Theorem guarantees that symmetric endomorphisms in Vec have complete eigenbases.
  • Meas — the category of measurable spaces and measurable functions. Every random variable is a morphism in Meas. The entire apparatus of measure-theoretic probability is the study of this category’s structure.
  • Set — the category of sets and functions. Data transformations, feature maps, and loss functions are all morphisms in Set.
  • Top — the category of topological spaces and continuous maps. Topological data analysis works in Top and its subcategories.

The power of category theory is not that it replaces these concrete settings — it is that it reveals the compositional patterns they share. A functor is a structure-preserving map between categories: it takes objects to objects and morphisms to morphisms, preserving composition and identities. The forgetful functor from Vec to Set “forgets” the linear structure and remembers only the underlying set. The adjacency matrix construction is a functor from the category of graphs to Vec. Recognizing these as instances of the same concept — functor — is what lets us transfer techniques across domains.

What we cover:

  1. Categories — objects, morphisms, composition, identity, and the two axioms.
  2. A gallery of categories — the concrete categories that ML practitioners work in daily.
  3. Morphism types — isomorphisms, monomorphisms, epimorphisms, endomorphisms, automorphisms.
  4. Functors — covariant and contravariant structure-preserving maps between categories.
  5. Opposite categories, Cat, and endofunctors — the category of categories, duality, and self-maps.
  6. Products, coproducts, and universal properties — characterizing constructions by what they do.
  7. The Hom functor — the functor that reframes linear algebra.
  8. Computational notes — categories and functors in Python, neural networks as categorical composition.

Categories: Objects, Morphisms, and Composition

The central idea is composition. Before we give the formal definition, consider what composition means concretely: if f:ABf: A \to B is a function from set AA to set BB, and g:BCg: B \to C is a function from BB to CC, then the composite gf:ACg \circ f: A \to C is the function that first applies ff, then applies gg. This composite is associative(hg)f=h(gf)(h \circ g) \circ f = h \circ (g \circ f) — and every set has an identity function idA:AA\mathrm{id}_A: A \to A that acts as a no-op for composition. A category axiomatizes exactly these two properties.

Definition 1 (Category).

A category C\mathcal{C} consists of:

  1. A collection Ob(C)\mathrm{Ob}(\mathcal{C}) of objects.
  2. For each pair of objects A,BA, B, a set Hom(A,B)\mathrm{Hom}(A, B) of morphisms (also called arrows) from AA to BB. We write f:ABf: A \to B to mean fHom(A,B)f \in \mathrm{Hom}(A, B).
  3. For each triple of objects A,B,CA, B, C, a composition operation

:Hom(B,C)×Hom(A,B)Hom(A,C)\circ: \mathrm{Hom}(B, C) \times \mathrm{Hom}(A, B) \to \mathrm{Hom}(A, C)

that sends (g,f)(g, f) to gfg \circ f.

  1. For each object AA, an identity morphism idAHom(A,A)\mathrm{id}_A \in \mathrm{Hom}(A, A).

These data satisfy two axioms:

  • Associativity. For all f:ABf: A \to B, g:BCg: B \to C, h:CDh: C \to D:

h(gf)=(hg)fh \circ (g \circ f) = (h \circ g) \circ f

  • Identity. For every morphism f:ABf: A \to B:

fidA=f=idBff \circ \mathrm{id}_A = f = \mathrm{id}_B \circ f

The definition requires remarkably little: objects, arrows between them, a way to compose arrows, identity arrows, and two axioms. Notice what it does not require — it says nothing about what objects or morphisms “are” internally. A morphism is not required to be a function, and an object is not required to be a set. This generality is the source of the theory’s power.

Proposition 1 (Uniqueness of Identity).

In any category C\mathcal{C}, the identity morphism on each object is unique.

Proof.

Suppose ee and ee' are both identity morphisms on object AA. Then:

e=ee=ee = e \circ e' = e'

The first equality holds because ee' is an identity (so ee=ee \circ e' = e), and the second holds because ee is an identity (so ee=ee \circ e' = e'). \blacksquare

The following explorer lets you build small categories from scratch and verify the axioms interactively. Try the presets to see how different structures — a simple chain of arrows, a partially ordered set, even a monoid — all satisfy the same two axioms.

Composition table (g ∘ f)

g ∖ fid_Aid_Bf
id_Aid_A
id_Bid_Bf
ff
Associativity: All composable triples
Identity law: All objects
Objects = colored circlesMorphisms = directed arrowsDashed loops = identity morphismsTable cell = g ∘ f (or — if not composable)

Category definition diagram showing three objects with morphisms, composition, and identity


The power of the definition comes from the range of examples it encompasses. Here are the categories that ML practitioners encounter regularly. In every case, the pattern is the same: objects are mathematical structures, and morphisms are the maps that preserve that structure.

CategoryObjectsMorphismsCompositionIdentity
SetSetsFunctionsFunction compositionIdentity function
VecR_\mathbb{R}Real vector spacesLinear mapsMatrix multiplicationIdentity matrix
GrpGroupsGroup homomorphismsComposition of homomorphismsIdentity homomorphism
TopTopological spacesContinuous mapsCompositionIdentity map
MeasMeasurable spacesMeasurable functionsCompositionIdentity function

Set is the most familiar category. Its objects are sets, its morphisms are functions between sets, and composition is ordinary function composition. For an ML practitioner, Set is the category of data transformations: feature maps, loss functions, and evaluation metrics are all morphisms in Set.

VecR_\mathbb{R} is the workhorse of machine learning. Its objects are finite-dimensional real vector spaces, and its morphisms are linear maps. Every fully connected neural network layer (before the activation function) is a morphism in Vec — a matrix multiplication xWx\mathbf{x} \mapsto W\mathbf{x}. The Spectral Theorem tells us that symmetric endomorphisms in Vec have complete eigenbases — a fact that drives spectral methods throughout ML.

Meas is the category of measure-theoretic probability. Its objects are measurable spaces (X,F)(X, \mathcal{F}), and its morphisms are measurable functions. A random variable X:(Ω,F)(R,B(R))X: (\Omega, \mathcal{F}) \to (\mathbb{R}, \mathcal{B}(\mathbb{R})) is a morphism in Meas. Composition of random variables corresponds to chaining probabilistic transformations.

Beyond these standard examples, there are categories built from algebraic structures:

  • Posets as categories. Any partially ordered set (P,)(P, \leq) defines a category: the objects are the elements of PP, and there is exactly one morphism from aa to bb if and only if aba \leq b. Composition is transitivity: if aba \leq b and bcb \leq c, then aca \leq c. The identity morphism on aa is the reflexivity aaa \leq a.

  • Monoids as one-object categories. A monoid (M,,e)(M, \cdot, e) — a set with an associative binary operation and an identity element — is a category with a single object *. The morphisms are the elements of MM, composition is the monoid operation \cdot, and the identity morphism is ee. The integers (Z,+,0)(\mathbb{Z}, +, 0) form a one-object category with morphisms ,2,1,0,1,2,\ldots, -2, -1, 0, 1, 2, \ldots

  • Discrete categories. A set SS with no structure beyond its elements forms a discrete category: the objects are the elements of SS, and the only morphisms are the identities. Every object is an island.

Gallery of categories showing Set, Vec, Grp, Top, Meas, Poset, and Monoid structures


Morphism Types

Within a category, morphisms have special properties that generalize familiar notions from set theory — injectivity, surjectivity, and bijectivity — to the abstract setting.

Definition 2 (Isomorphism).

A morphism f:ABf: A \to B in a category C\mathcal{C} is an isomorphism if there exists a morphism g:BAg: B \to A such that:

gf=idAandfg=idBg \circ f = \mathrm{id}_A \qquad \text{and} \qquad f \circ g = \mathrm{id}_B

The morphism gg is called the inverse of ff, written f1f^{-1}. If an isomorphism exists between AA and BB, we write ABA \cong B.

Proposition 2 (Uniqueness of Inverses).

If f:ABf: A \to B is an isomorphism, its inverse is unique.

Proof.

Suppose gg and gg' are both inverses of ff. Then:

g=gidB=g(fg)=(gf)g=idAg=gg = g \circ \mathrm{id}_B = g \circ (f \circ g') = (g \circ f) \circ g' = \mathrm{id}_A \circ g' = g'

where we used associativity and the inverse properties. \blacksquare

Proposition 3 (Composition of Isomorphisms).

If f:ABf: A \to B and g:BCg: B \to C are isomorphisms, then gf:ACg \circ f: A \to C is an isomorphism with inverse f1g1f^{-1} \circ g^{-1}.

Proof.

We verify both conditions:

(f1g1)(gf)=f1(g1g)f=f1idBf=f1f=idA(f^{-1} \circ g^{-1}) \circ (g \circ f) = f^{-1} \circ (g^{-1} \circ g) \circ f = f^{-1} \circ \mathrm{id}_B \circ f = f^{-1} \circ f = \mathrm{id}_A

(gf)(f1g1)=g(ff1)g1=gidBg1=gg1=idC(g \circ f) \circ (f^{-1} \circ g^{-1}) = g \circ (f \circ f^{-1}) \circ g^{-1} = g \circ \mathrm{id}_B \circ g^{-1} = g \circ g^{-1} = \mathrm{id}_C

So gfg \circ f is an isomorphism with inverse f1g1f^{-1} \circ g^{-1}. \blacksquare

Definition 3 (Monomorphism).

A morphism f:ABf: A \to B is a monomorphism (or is monic) if it is left-cancellable: for all morphisms g1,g2:ZAg_1, g_2: Z \to A,

fg1=fg2    g1=g2f \circ g_1 = f \circ g_2 \implies g_1 = g_2

Definition 4 (Epimorphism).

A morphism f:ABf: A \to B is an epimorphism (or is epic) if it is right-cancellable: for all morphisms g1,g2:BZg_1, g_2: B \to Z,

g1f=g2f    g1=g2g_1 \circ f = g_2 \circ f \implies g_1 = g_2

In Set, monomorphisms are exactly injective functions and epimorphisms are exactly surjective functions. But this correspondence does not hold in every category.

Proposition 4 (Every Isomorphism is Mono and Epi).

If f:ABf: A \to B is an isomorphism, then ff is both a monomorphism and an epimorphism.

Proof.

Mono: Suppose fg1=fg2f \circ g_1 = f \circ g_2. Applying f1f^{-1} on the left: f1fg1=f1fg2f^{-1} \circ f \circ g_1 = f^{-1} \circ f \circ g_2, so g1=g2g_1 = g_2.

Epi: Suppose g1f=g2fg_1 \circ f = g_2 \circ f. Applying f1f^{-1} on the right: g1ff1=g2ff1g_1 \circ f \circ f^{-1} = g_2 \circ f \circ f^{-1}, so g1=g2g_1 = g_2. \blacksquare

Remark (Mono + Epi ≠ Iso in General).

The converse of Proposition 4 fails in general. The inclusion ZQ\mathbb{Z} \hookrightarrow \mathbb{Q} is both monic and epic in the category Ring of rings and ring homomorphisms, but it is not an isomorphism — there is no ring homomorphism QZ\mathbb{Q} \to \mathbb{Z} that inverts the inclusion. The categorical notions of “injective” and “surjective” are genuinely more general than their set-theoretic counterparts.

Proposition 5 (Composition of Monomorphisms).

If f:ABf: A \to B and g:BCg: B \to C are monomorphisms, then gf:ACg \circ f: A \to C is a monomorphism.

Proof.

Suppose (gf)h1=(gf)h2(g \circ f) \circ h_1 = (g \circ f) \circ h_2 for some h1,h2:ZAh_1, h_2: Z \to A. By associativity, g(fh1)=g(fh2)g \circ (f \circ h_1) = g \circ (f \circ h_2). Since gg is monic, fh1=fh2f \circ h_1 = f \circ h_2. Since ff is monic, h1=h2h_1 = h_2. \blacksquare

Definition 5 (Endomorphism and Automorphism).

A morphism f:AAf: A \to A (with the same source and target) is an endomorphism. The set of all endomorphisms of AA is denoted End(A)=Hom(A,A)\mathrm{End}(A) = \mathrm{Hom}(A, A).

An endomorphism that is also an isomorphism is an automorphism. The set of automorphisms Aut(A)\mathrm{Aut}(A) forms a group under composition.

In Vec, an endomorphism of Rn\mathbb{R}^n is an n×nn \times n matrix. An automorphism is an invertible matrix — an element of the general linear group GL(n,R)\mathrm{GL}(n, \mathbb{R}).

Morphism types diagram showing isomorphism, monomorphism, epimorphism, endomorphism, and automorphism


Functors: Structure-Preserving Maps Between Categories

If categories are mathematical universes with their own objects and composition laws, then functors are the translations between universes. A functor maps objects to objects and morphisms to morphisms, preserving the composition structure.

Definition 6 (Functor (Covariant)).

A (covariant) functor F:CDF: \mathcal{C} \to \mathcal{D} between categories C\mathcal{C} and D\mathcal{D} consists of:

  1. A mapping on objects: for each object AOb(C)A \in \mathrm{Ob}(\mathcal{C}), an object F(A)Ob(D)F(A) \in \mathrm{Ob}(\mathcal{D}).
  2. A mapping on morphisms: for each morphism f:ABf: A \to B in C\mathcal{C}, a morphism F(f):F(A)F(B)F(f): F(A) \to F(B) in D\mathcal{D}.

These mappings satisfy two axioms:

  • Preservation of composition. For all composable morphisms f:ABf: A \to B and g:BCg: B \to C:

F(gf)=F(g)F(f)F(g \circ f) = F(g) \circ F(f)

  • Preservation of identities. For every object AA:

F(idA)=idF(A)F(\mathrm{id}_A) = \mathrm{id}_{F(A)}

Definition 7 (Contravariant Functor).

A contravariant functor F:CDF: \mathcal{C} \to \mathcal{D} reverses the direction of morphisms: for each f:ABf: A \to B in C\mathcal{C}, the image is F(f):F(B)F(A)F(f): F(B) \to F(A) in D\mathcal{D}. The composition axiom becomes:

F(gf)=F(f)F(g)F(g \circ f) = F(f) \circ F(g)

Equivalently, a contravariant functor CD\mathcal{C} \to \mathcal{D} is a covariant functor CopD\mathcal{C}^{\mathrm{op}} \to \mathcal{D}.

Key examples:

  • Forgetful functor U:VecSetU: \mathbf{Vec} \to \mathbf{Set}. Sends each vector space VV to its underlying set U(V)U(V) (forgetting the linear structure) and each linear map TT to the underlying function U(T)U(T). This “forgets” the algebraic structure — a vector space is just a set with extra properties, and a linear map is just a function that happens to be linear.

  • Free functor F:SetVecF: \mathbf{Set} \to \mathbf{Vec}. Sends each set SS to the free vector space F(S)=RSF(S) = \mathbb{R}^S (with basis elements indexed by SS) and each function f:STf: S \to T to the linear extension F(f):RSRTF(f): \mathbb{R}^S \to \mathbb{R}^T. This “freely adds” linear structure.

  • Power set functor P:SetSetP: \mathbf{Set} \to \mathbf{Set}. Sends each set XX to its power set P(X)=2XP(X) = 2^X and each function f:XYf: X \to Y to the direct image map P(f):P(X)P(Y)P(f): P(X) \to P(Y) defined by P(f)(S)=f(S)={f(x)xS}P(f)(S) = f(S) = \{f(x) \mid x \in S\}.

Theorem 1 (Functors Preserve Isomorphisms).

If F:CDF: \mathcal{C} \to \mathcal{D} is a functor and f:ABf: A \to B is an isomorphism in C\mathcal{C}, then F(f):F(A)F(B)F(f): F(A) \to F(B) is an isomorphism in D\mathcal{D} with inverse F(f1)F(f^{-1}).

Proof.

Since ff1=idBf \circ f^{-1} = \mathrm{id}_B and f1f=idAf^{-1} \circ f = \mathrm{id}_A, applying FF:

F(f)F(f1)=F(ff1)=F(idB)=idF(B)F(f) \circ F(f^{-1}) = F(f \circ f^{-1}) = F(\mathrm{id}_B) = \mathrm{id}_{F(B)}

F(f1)F(f)=F(f1f)=F(idA)=idF(A)F(f^{-1}) \circ F(f) = F(f^{-1} \circ f) = F(\mathrm{id}_A) = \mathrm{id}_{F(A)}

So F(f)F(f) is an isomorphism with inverse F(f1)F(f^{-1}). \blacksquare

The following visualizer shows how functors map objects and morphisms between categories. Select a functor from the dropdown to see the mapping in action — notice how the connecting arrows show which objects and morphisms correspond, and how the axiom verifier confirms that composition and identities are preserved.

Functor Axiom Verification
Identity preservation
F(id_ℝ) = id_{{1,2}}F(id_ℝ²) = id_{{a,b,c}}F(id_ℝ³) = id_{{x}}
Composition preservation
F(S∘T) = g∘f [g∘f]
Hover an object to highlight its mapping● Source● Target⇢ Functor map

Functor diagram showing covariant and contravariant mappings between categories


Opposite Categories, Cat, and Endofunctors

Definition 8 (Opposite Category).

Given a category C\mathcal{C}, the opposite category Cop\mathcal{C}^{\mathrm{op}} has:

  • The same objects as C\mathcal{C}.
  • Reversed morphisms: for each f:ABf: A \to B in C\mathcal{C}, there is a morphism fop:BAf^{\mathrm{op}}: B \to A in Cop\mathcal{C}^{\mathrm{op}}.
  • Reversed composition: if gfg \circ f is defined in C\mathcal{C}, then fopgop=(gf)opf^{\mathrm{op}} \circ g^{\mathrm{op}} = (g \circ f)^{\mathrm{op}} in Cop\mathcal{C}^{\mathrm{op}}.

The opposite category is a powerful tool for duality. Every theorem about categories has a dual theorem obtained by replacing C\mathcal{C} with Cop\mathcal{C}^{\mathrm{op}}. Under this substitution, source and target swap, monomorphisms become epimorphisms, products become coproducts, and initial objects become terminal objects. We get two theorems for the price of one.

A contravariant functor F:CDF: \mathcal{C} \to \mathcal{D} is the same thing as a covariant functor F:CopDF: \mathcal{C}^{\mathrm{op}} \to \mathcal{D}. This reformulation often simplifies proofs — instead of tracking arrow reversals, we simply work with the opposite category.

The category Cat. Small categories themselves form a category, written Cat:

  • Objects: Small categories (those whose objects and morphisms form sets).
  • Morphisms: Functors between categories.
  • Composition: Functor composition — (GF)(A)=G(F(A))(G \circ F)(A) = G(F(A)) on objects, (GF)(f)=G(F(f))(G \circ F)(f) = G(F(f)) on morphisms.
  • Identity: The identity functor IdC:CC\mathrm{Id}_\mathcal{C}: \mathcal{C} \to \mathcal{C} sending every object and morphism to itself.

Endofunctors. A functor F:CCF: \mathcal{C} \to \mathcal{C} from a category to itself is an endofunctor. Endofunctors can be iterated: F2=FFF^2 = F \circ F, F3=FFFF^3 = F \circ F \circ F, and so on. The power set functor P:SetSetP: \mathbf{Set} \to \mathbf{Set} is an endofunctor.

A functor F:CDF: \mathcal{C} \to \mathcal{D} is:

  • Faithful if FF is injective on each Hom set: F(f)=F(g)    f=gF(f) = F(g) \implies f = g for all f,g:ABf, g: A \to B.
  • Full if FF is surjective on each Hom set: every morphism F(A)F(B)F(A) \to F(B) in D\mathcal{D} is of the form F(f)F(f) for some f:ABf: A \to B in C\mathcal{C}.
  • Fully faithful if FF is bijective on each Hom set. A fully faithful functor is an “embedding” — it identifies C\mathcal{C} with a subcategory of D\mathcal{D}.

The forgetful functor U:VecSetU: \mathbf{Vec} \to \mathbf{Set} is faithful (distinct linear maps are distinct as functions) but not full (not every function between vector spaces is linear).

Remark (Endofunctors and Monads (Preview)).

An endofunctor T:CCT: \mathcal{C} \to \mathcal{C} equipped with two natural transformations — a unit η:IdT\eta: \mathrm{Id} \Rightarrow T and a multiplication μ:T2T\mu: T^2 \Rightarrow T — satisfying associativity and unit laws is a monad. Monads are the categorical structure underlying probabilistic programming (the Giry monad on Meas), Haskell’s IO, and computational effects more generally. Monads & Comonads develops monads as monoids in the category of endofunctors — the culmination of the Category Theory track.

Opposite categories, Cat, and endofunctors diagram


Products, Coproducts, and Universal Properties

Category theory characterizes mathematical constructions not by what they are made of but by what they do. The tool for this is the universal property: a construction is defined by being the best solution to a particular mapping problem. This is a profound shift in perspective — from internal structure to external relationships.

Definition 9 (Product (Universal Property)).

Let AA and BB be objects in a category C\mathcal{C}. A product of AA and BB is an object A×BA \times B together with morphisms π1:A×BA\pi_1: A \times B \to A and π2:A×BB\pi_2: A \times B \to B (called projections) such that for every object ZZ and morphisms f:ZAf: Z \to A and g:ZBg: Z \to B, there exists a unique morphism h:ZA×Bh: Z \to A \times B satisfying:

π1h=fandπ2h=g\pi_1 \circ h = f \qquad \text{and} \qquad \pi_2 \circ h = g

We write h=f,gh = \langle f, g \rangle.

The word “unique” in the definition is doing all the work. Many objects might come equipped with morphisms to AA and BB, but the product is the one through which all such morphisms factor uniquely.

Definition 10 (Coproduct (Universal Property)).

The coproduct ABA \sqcup B is the dual construction. It comes with inclusions ι1:AAB\iota_1: A \to A \sqcup B and ι2:BAB\iota_2: B \to A \sqcup B such that for every object ZZ and morphisms f:AZf: A \to Z and g:BZg: B \to Z, there exists a unique morphism h:ABZh: A \sqcup B \to Z satisfying:

hι1=fandhι2=gh \circ \iota_1 = f \qquad \text{and} \qquad h \circ \iota_2 = g

We write h=[f,g]h = [f, g].

Proposition 6 (Product Uniqueness).

If (P,π1,π2)(P, \pi_1, \pi_2) and (P,π1,π2)(P', \pi_1', \pi_2') are both products of AA and BB, then PPP \cong P' via a unique isomorphism compatible with the projections.

Proof.

By the universal property of PP, there is a unique h:PPh: P' \to P with π1h=π1\pi_1 \circ h = \pi_1' and π2h=π2\pi_2 \circ h = \pi_2'. By the universal property of PP', there is a unique h:PPh': P \to P' with π1h=π1\pi_1' \circ h' = \pi_1 and π2h=π2\pi_2' \circ h' = \pi_2. Then hh:PPh' \circ h: P' \to P' satisfies π1(hh)=π1\pi_1' \circ (h' \circ h) = \pi_1' and π2(hh)=π2\pi_2' \circ (h' \circ h) = \pi_2'. But idP\mathrm{id}_{P'} also satisfies these equations, so by uniqueness hh=idPh' \circ h = \mathrm{id}_{P'}. Similarly hh=idPh \circ h' = \mathrm{id}_P. \blacksquare

The concrete meaning of products and coproducts depends on the category:

CategoryProduct A×BA \times BCoproduct ABA \sqcup B
SetCartesian productDisjoint union
VecDirect sum ABA \oplus BDirect sum ABA \oplus B
GrpDirect productFree product
TopProduct topologyDisjoint union topology
PosetMeet (greatest lower bound)Join (least upper bound)

In Vec, the product and coproduct coincide — both are the direct sum. This is a special property of abelian categories.

Definition 11 (Initial and Terminal Objects).

An object 0\mathbf{0} is initial if for every object ZZ, there exists a unique morphism 0Z\mathbf{0} \to Z.

An object 1\mathbf{1} is terminal if for every object ZZ, there exists a unique morphism Z1Z \to \mathbf{1}.

In Set, the initial object is the empty set \emptyset (there is exactly one function X\emptyset \to X for any set XX — the empty function), and the terminal object is any singleton set {}\{*\} (for any set XX, the unique function X{}X \to \{*\} sends everything to *). In Vec, the zero vector space {0}\{0\} is both initial and terminal — a zero object.

π₁ ∘ h = f and π₂ ∘ h = g where h = ⟨f, g⟩
Structural morphismsUser-defined morphismsUnique mediating morphism

Products, coproducts, and universal properties diagram


The Hom Functor

The Hom functor is one of the most important constructions in category theory. It takes two objects and produces their “space of maps” — and this construction is itself functorial.

Definition 12 (Hom Functor).

Let C\mathcal{C} be a locally small category (one where each Hom(A,B)\mathrm{Hom}(A, B) is a set). Fix an object ACA \in \mathcal{C}.

The covariant Hom functor Hom(A,):CSet\mathrm{Hom}(A, -): \mathcal{C} \to \mathbf{Set} is defined by:

  • On objects: BHom(A,B)B \mapsto \mathrm{Hom}(A, B).
  • On morphisms: given g:BCg: B \to C, the map Hom(A,g):Hom(A,B)Hom(A,C)\mathrm{Hom}(A, g): \mathrm{Hom}(A, B) \to \mathrm{Hom}(A, C) is post-composition: fgff \mapsto g \circ f.

The contravariant Hom functor Hom(,A):CopSet\mathrm{Hom}(-, A): \mathcal{C}^{\mathrm{op}} \to \mathbf{Set} is defined by:

  • On objects: BHom(B,A)B \mapsto \mathrm{Hom}(B, A).
  • On morphisms: given f:BCf: B \to C, the map Hom(f,A):Hom(C,A)Hom(B,A)\mathrm{Hom}(f, A): \mathrm{Hom}(C, A) \to \mathrm{Hom}(B, A) is pre-composition: ggfg \mapsto g \circ f.

Theorem 2 (Hom is a Bifunctor).

The combined construction Hom(,):Cop×CSet\mathrm{Hom}(-, -): \mathcal{C}^{\mathrm{op}} \times \mathcal{C} \to \mathbf{Set} is a functor — contravariant in the first argument and covariant in the second. Given morphisms f:AAf: A' \to A and g:BBg: B \to B', the induced map:

Hom(f,g):Hom(A,B)Hom(A,B)\mathrm{Hom}(f, g): \mathrm{Hom}(A, B) \to \mathrm{Hom}(A', B')

sends hghfh \mapsto g \circ h \circ f.

Proof.

We verify functoriality. For identities: Hom(idA,idB)(h)=idBhidA=h\mathrm{Hom}(\mathrm{id}_A, \mathrm{id}_B)(h) = \mathrm{id}_B \circ h \circ \mathrm{id}_A = h, so Hom(idA,idB)=idHom(A,B)\mathrm{Hom}(\mathrm{id}_A, \mathrm{id}_B) = \mathrm{id}_{\mathrm{Hom}(A,B)}.

For composition: given f:AAf': A'' \to A', f:AAf: A' \to A, g:BBg: B \to B', g:BBg': B' \to B'':

Hom(ff,gg)(h)=(gg)h(ff)=g(ghf)f=Hom(f,g)(Hom(f,g)(h))\mathrm{Hom}(f \circ f', g' \circ g)(h) = (g' \circ g) \circ h \circ (f \circ f') = g' \circ (g \circ h \circ f) \circ f' = \mathrm{Hom}(f', g')(\mathrm{Hom}(f, g)(h))

so Hom(ff,gg)=Hom(f,g)Hom(f,g)\mathrm{Hom}(f \circ f', g' \circ g) = \mathrm{Hom}(f', g') \circ \mathrm{Hom}(f, g). \blacksquare

The Hom functor concretizes linear algebra. In Vec, Hom(Rm,Rn)\mathrm{Hom}(\mathbb{R}^m, \mathbb{R}^n) is the space of all linear maps from Rm\mathbb{R}^m to Rn\mathbb{R}^n — which is isomorphic to Mat(n×m)\mathrm{Mat}(n \times m), the space of nn-by-mm matrices. The covariant Hom functor Hom(Rm,)\mathrm{Hom}(\mathbb{R}^m, -) sends a vector space VV to the matrix space Mat(dimV×m)\mathrm{Mat}(\dim V \times m) and sends a linear map T:VWT: V \to W to post-composition by TT — which is matrix multiplication on the left.

A functor F:CSetF: \mathcal{C} \to \mathbf{Set} is representable if it is naturally isomorphic to Hom(A,)\mathrm{Hom}(A, -) for some object AA. The representing object AA is unique up to isomorphism. Representable functors are ubiquitous: the functor sending a group to its underlying set is representable (represented by Z\mathbb{Z}), and the functor sending a vector space to its dual is represented by the ground field.

The Yoneda Lemma — the deepest result in basic category theory — says that natural transformations from Hom(A,)\mathrm{Hom}(A, -) to any functor FF are in bijection with elements of F(A)F(A). We develop the Yoneda Lemma fully in Natural Transformations.

Hom functor diagram showing covariant and contravariant cases, and matrix interpretation


Computational Notes

Categories and functors are not only abstract mathematics — they have direct computational implementations. Here we show how to represent finite categories in Python and verify functorial properties programmatically.

A Python category class. A finite category can be represented as a dictionary of Hom sets with a composition function:

class Category:
    """A finite category with explicit objects, morphisms, and composition."""
    def __init__(self, objects, hom, comp_table, identity):
        self.objects = objects          # list of object labels
        self.hom = hom                 # dict: (A, B) -> list of morphism labels
        self.comp_table = comp_table   # dict: (g, f) -> g ∘ f label, or absent
        self.identity = identity       # function: A -> id_A label

    def compose(self, g, f):
        """Return g ∘ f if composable, else None."""
        return self.comp_table.get((g, f))

# Example: the triangle category {A, B, C}
triangle = Category(
    objects=['A', 'B', 'C'],
    hom={
        ('A','A'): ['id_A'], ('B','B'): ['id_B'], ('C','C'): ['id_C'],
        ('A','B'): ['f'],    ('B','C'): ['g'],    ('A','C'): ['gf'],
    },
    comp_table={
        ('g','f'): 'gf', ('id_B','f'): 'f', ('g','id_B'): 'g',
        ('id_A','id_A'): 'id_A', ('gf','id_A'): 'gf',
        ('id_C','gf'): 'gf', ('id_C','g'): 'g', ('f','id_A'): 'f',
        ('id_B','id_B'): 'id_B', ('id_C','id_C'): 'id_C',
    },
    identity=lambda A: f'id_{A}',
)

A functor class with axiom verification:

class Functor:
    """A functor F: C -> D with axiom checking."""
    def __init__(self, source, target, on_objects, on_morphisms):
        self.source = source
        self.target = target
        self.on_obj = on_objects       # dict: src_obj -> tgt_obj
        self.on_mor = on_morphisms     # dict: src_mor -> tgt_mor

    def verify_identities(self):
        """Check F(id_A) = id_{F(A)} for all objects. Returns (ok, violations)."""
        violations = []
        for A in self.source.objects:
            src_id = self.source.identity(A)
            tgt_id = self.target.identity(self.on_obj[A])
            if self.on_mor[src_id] != tgt_id:
                violations.append(A)
        return len(violations) == 0, violations

    def verify_composition(self):
        """Check F(g ∘ f) = F(g) ∘ F(f) for composable pairs. Returns (ok, violations)."""
        violations = []
        for g_label in sum(self.source.hom.values(), []):
            for f_label in sum(self.source.hom.values(), []):
                gf = self.source.compose(g_label, f_label)
                if gf is not None:
                    Fgf = self.on_mor[gf]
                    Fg_Ff = self.target.compose(self.on_mor[g_label], self.on_mor[f_label])
                    if Fgf != Fg_Ff:
                        violations.append((g_label, f_label))
        return len(violations) == 0, violations

Neural networks as categorical composition. A feedforward neural network is a sequence of morphisms in (an enrichment of) Vec:

import torch.nn as nn

# A 3-layer network: R^784 → R^256 → R^128 → R^10
# Each layer is a morphism in Vec (before activation)
network = nn.Sequential(
    nn.Linear(784, 256),   # f: R^784 → R^256
    nn.ReLU(),
    nn.Linear(256, 128),   # g: R^256 → R^128
    nn.ReLU(),
    nn.Linear(128, 10),    # h: R^128 → R^10
)
# The composite h ∘ g ∘ f: R^784 → R^10 is associative
# by construction — (h ∘ g) ∘ f = h ∘ (g ∘ f)

Remark (Neural Networks as Categorical Composition).

Strictly speaking, a ReLU network is not a morphism in Vec because the activation function is nonlinear. The precise categorical setting is the category of smooth manifolds (for differentiable activations) or the category of Lipschitz functions. But the compositional structure — the fact that the whole network is built by composing layers — is the categorical pattern. The linear parts are honest morphisms in Vec, and backpropagation respects the composition via the chain rule: (gf)x=gf(x)fx\frac{\partial(g \circ f)}{\partial x} = \frac{\partial g}{\partial f(x)} \cdot \frac{\partial f}{\partial x}.

The following explorer demonstrates associativity concretely. Select a category — Set, Vec, or Poset — and step through the composition of three morphisms. Both parenthesizations produce the same result, making associativity visceral rather than axiomatic.

Category:
Step 0 / 3
Associativity: h \u2218 (g \u2218 f) = (h \u2218 g) \u2218 fStep through both parenthesizations to see they produce the same composite morphism.

ML connections diagram showing neural network layers as morphisms and functorial data pipelines


Connections & Further Reading

Where this fits

Categories and functors are the foundation of the Category Theory track. Everything that follows builds on the definitions and examples introduced here:

  • Natural Transformations — Natural transformations are morphisms between functors — the next level of abstraction. The Yoneda lemma, which characterizes objects by their morphisms, is the deepest result in basic category theory. Equivariance of neural networks is precisely the naturality condition.

  • Adjunctions — formalizes the free-forgetful pair FUF \dashv U between Set and Vec as the prototypical adjunction. The unit-counit formulation, Galois connections, RAPL theorem, and connections to Lagrangian duality and attention mechanisms.

  • Monads & Comonads — develops monads as monoids in the category of endofunctors [C,C][\mathcal{C}, \mathcal{C}], with the Giry monad providing the categorical foundation of Bayesian probability and the neighborhood comonad unifying GNN message passing — the culmination of the Category Theory track.

Cross-track connections

The language of categories and functors connects to every prior track on formalML:

  • The Spectral Theorem guarantees that symmetric endomorphisms in Vec have complete eigenbases — the eigendecomposition of the graph Laplacian, the covariance matrix in PCA, and the kernel matrix in SVMs all rely on this fact about the category Vec.

  • Measure-Theoretic Probability works in the category Meas of measurable spaces. Random variables are morphisms, and the pushforward of probability measures is a functor.

  • The category of graphs and graph homomorphisms is a concrete example where the adjacency matrix construction is a functor from Graph to Vec.

  • Smooth Manifolds form a category Man where the tangent bundle construction T:ManVectBundT: \mathbf{Man} \to \mathbf{VectBund} is a functor.

  • Convex Analysis: convex sets and affine maps form a category where products are Cartesian products and the subdifferential is functorial.

Notation summary

SymbolMeaning
C,D\mathcal{C}, \mathcal{D}Categories
Ob(C)\mathrm{Ob}(\mathcal{C})Objects of C\mathcal{C}
Hom(A,B)\mathrm{Hom}(A, B)Morphisms from AA to BB
f:ABf: A \to BA morphism from AA to BB
gfg \circ fComposition (apply ff first, then gg)
idA\mathrm{id}_AIdentity morphism on AA
F:CDF: \mathcal{C} \to \mathcal{D}Functor from C\mathcal{C} to D\mathcal{D}
Cop\mathcal{C}^{\mathrm{op}}Opposite category
A×BA \times BProduct
ABA \sqcup BCoproduct
π1,π2\pi_1, \pi_2Projections
ι1,ι2\iota_1, \iota_2Inclusions
!h\exists!\, hUnique existence
End(A)\mathrm{End}(A)Endomorphisms of AA
Aut(A)\mathrm{Aut}(A)Automorphisms of AA
ABA \cong BAA is isomorphic to BB
Set,Vec,Grp,Top,Meas\mathbf{Set}, \mathbf{Vec}, \mathbf{Grp}, \mathbf{Top}, \mathbf{Meas}Named categories
Cat\mathbf{Cat}Category of small categories

Connections

  • The category Vec of finite-dimensional vector spaces over R is the primary running example. The Spectral Theorem guarantees that symmetric endomorphisms in Vec have complete eigenbases. Hom(R^m, R^n) = Mat(n x m) illustrates how the Hom functor concretely produces matrix spaces. spectral-theorem
  • The category Meas of measurable spaces and measurable functions is the categorical setting for probability theory. Random variables are morphisms in Meas, probability measures are functorial, and conditional expectations are morphisms with specific universal properties. measure-theoretic-probability
  • The category Man of smooth manifolds and smooth maps underlies differential geometry. The tangent bundle construction T: Man -> VectBund is a functor. Riemannian geometry studies Man equipped with additional structure (metrics) that must be preserved by morphisms. smooth-manifolds
  • Convex sets and affine maps form a category. Products correspond to Cartesian products of convex sets. The subdifferential is functorial: it sends convex functions to set-valued maps in a composition-preserving way. convex-analysis
  • Graphs and graph homomorphisms form a category Graph. The adjacency matrix construction is a functor from Graph to Vec. Graph neural networks compose morphisms in a learned category, and message passing is a natural transformation (coming in the next topic). graph-laplacians

References & Further Reading

  • book Categories for the Working Mathematician — Mac Lane (1998) The standard graduate reference — Chapters I-IV cover categories, functors, natural transformations, and universal properties
  • book Category Theory — Awodey (2010) A more accessible introduction with excellent examples from algebra, topology, and logic
  • book Category Theory in Context — Riehl (2016) Modern treatment emphasizing the Yoneda lemma and adjunctions — freely available online
  • book An Invitation to Applied Category Theory: Seven Sketches in Compositionality — Fong & Spivak (2019) The applied perspective — connects categories to databases, circuits, and ML pipelines
  • paper Category Theory in Machine Learning — Shiebler, Gavranović & Wilson (2021) Survey of categorical methods in ML: functorial learning, categorical probability, gradient-based learning as lenses
  • book Category Theory for the Sciences — Spivak (2014) Categories as a modeling language for scientific structures