Foundations of mathematics
Categories and functors
A category packages objects and the arrows between them, subject only to associativity and identity; a functor is a structure-preserving translation between two such worlds, and it turns out that once you fix "how things relate", the objects themselves are determined only up to a unique isomorphism.
IntuitionSame shape, different costume
A translator, a graph, a network of train stations connected by routes, a set of tasks connected by "must happen before" — all of these share one skeleton: some things (objects) and some arrows between them that can be chained. A category makes that skeleton precise; a functor is a way of redrawing one such network inside another while keeping every chain of arrows intact.
UndergraduateCategories: objects, morphisms, composition
Definition: Category
A category consists of a collection of objects and, for every pair, a set of morphisms , together with a composition rule sending and to , and an identity morphism for every object.
Two axioms make this data into a category: composition is associative (, so a chain of arrows has one unambiguous composite, no matter how it is parenthesized) and identities are neutral (, so composing with or never changes a morphism).
| Kind | Action on morphisms | Typical example |
|---|---|---|
| Covariant | Sends to , same direction | The list functor, forgetful functors |
| Contravariant | Sends to , reversed direction | The dual-space functor, the Hom(-,A) presheaf |
AdvancedFunctors and natural transformations
A functor assigns to every object of an object of , and to every morphism a morphism , so that composition and identities are preserved: and . A natural transformation between two functors assigns to every object a morphism in , subject to the naturality square: for every , .
If and are both terminal objects of (there is exactly one morphism from every object into each of them), then : there is an isomorphism between them, and it is the only morphism . The dual statement holds for initial objects, and hence for products by the same argument applied to the category of candidate cones.
Why is it true?
Without this fact, "the" terminal object or "the" product would not be well-defined — different constructions of a product (say, ordered pairs vs. some other encoding) need to be interchangeable, and uniqueness up to unique isomorphism is exactly the precise sense in which they are "the same".
Proof
Since is terminal, every object — in particular — has exactly one morphism into it; call it . Since is terminal, symmetrically there is exactly one .
Consider the composite . Because is terminal, there is exactly one morphism , and is one such morphism; since is also a morphism , uniqueness forces .
By the symmetric argument using the terminality of , the composite must equal : .
A morphism with a two-sided inverse is by definition an isomorphism, so is an isomorphism with inverse . It is the unique morphism because terminality of already says there is only one such morphism at all — was forced from the start, and it simply turned out to be invertible.
If is a functor and is an isomorphism in with inverse , then is an isomorphism in , with inverse .
Why is it true?
This is what makes functors trustworthy translators: a functor can never accidentally break an equivalence into two genuinely different objects, so classifying objects "up to isomorphism" is a question functors respect.
Proof
Since and are mutually inverse, and .
Apply to the first equation. Functors preserve composition, so ; functors preserve identities, so . Combining these with gives .
Apply to the second equation in the same way: and , so from we get .
The two displayed equations say exactly that is a two-sided inverse of . A morphism with a two-sided inverse is an isomorphism, so is an isomorphism with inverse , as claimed.
UndergraduateApplications: functional programming and database migration
Every mainstream functional language has a `Functor` type class precisely because containers like lists, trees and `Maybe`/`Option` are functors on the category of types and functions: `fmap` is on morphisms, and the functor laws , are exactly the axioms above. `Monad` refines this further with two natural transformations (`return` and `join`) satisfying naturality-square-style coherence laws. Outside programming, David Spivak's functorial data migration models a database schema as a small category (tables as objects, foreign keys as morphisms) and a schema migration as a functor between two such categories, so that "moving data correctly" literally means "being a functor" — composition-preservation guarantees that migrating via an intermediate schema gives the same result as migrating directly.
Example: Checking the functor laws for lists
Take for lists, defined by applying a function to every element: . Verify both functor laws on the concrete list with and .
Solution
First law, : applying the identity function element-wise gives , which is exactly . This holds for any list, not just this one, because applying "do nothing" to each element does nothing to the list.
Second law, : compute the left side first. , so .
Now the right side: , and then .
Both sides equal , confirming the law on this example; the general proof is the same computation with instead of , since never reorders or drops elements.
Example: A schema migration as a functor
A schema has tables `Person` and `City`, with a foreign key `livesIn : Person -> City`. A new schema splits `Person` into `Person` and `Contact`, with `hasContact : Person -> Contact` and `livesIn2 : Contact -> City`. Describe the migration as a functor and explain what preserving composition buys you.
Solution
Model each schema as a category: objects are tables, and morphisms are foreign keys composed freely (so has the composite as a generator). The migration sends , , and the morphism to the composite morphism in .
For to be a functor it must send identities to identities (trivial here) and respect composition: any chain of foreign keys built in out of must map to the same chain built out of , in the same order, with no steps skipped or reordered.
This is exactly what the theorem "functors preserve composition" buys an engineer: if a query joins to via in the old schema, translating each table and mapping to the two-step path and then executing the join gives the same answer as translating the whole query as one unit — data migrated table-by-table is guaranteed consistent with data migrated query-by-query, precisely because .
Which pair of equations are the two category axioms?
A contravariant functor sends to a morphism going which way?
In Spivak's functorial data migration, what does a schema migration correspond to?
If is the unique morphism between two terminal objects, what does the theorem say about ?
References
- Saunders Mac Lane (1998). Categories for the Working Mathematician
- Emily Riehl (2016). Category Theory in Context
- David I. Spivak (2012). Functorial Data Migration · arXiv:1009.1166