Category Theory: The Mathematics of Mathematics — From Subway Maps to Code
This article introduces category theory using a subway map analogy, explaining objects, morphisms, composition, functors, natural transformations, and universal properties, showing how these abstract concepts unify mathematics and appear in everyday programming patterns like function composition and map.
From Subway Maps to Category Theory
Category theory is often jokingly called "abstract nonsense" by mathematicians, yet it has become the universal language of modern mathematics and has quietly entered programmers' daily work. This article requires no advanced mathematical background — only the ability to understand dots and arrows.
Open a subway map: it shows only stations and connecting lines, omitting buildings, streets, and rivers. Yet this abstract diagram lets you travel from any station to any other. Mathematics has a similar need: many branches (sets, groups, vector spaces, topological spaces) each have their own "things" and "transformations between things." In the 1940s, Samuel Eilenberg and Saunders Mac Lane noticed that many deep results do not depend on what the "things" look like internally, but only on how they transform and connect. In 1945 they published a paper formally introducing the concept of a "category."
The core idea of category theory can be summarized in one sentence: Instead of staring at objects themselves, look at the relationships between objects. It is the subway map of the mathematical world.
What Is a Category: Objects, Morphisms, and Composition
A category consists of four components:
Objects : Think of them as dots on a diagram, denoted A, B, C…
Morphisms : Arrows from one object to another, e.g., f: A → B.
Composition : If there is f: A → B and g: B → C, then there must be an arrow from A directly to C, written g∘f.
Identity morphisms : Every object has a "do-nothing" arrow, id_A: A → A.
Note that g∘f reads right-to-left: first do f, then do g. This matches the familiar function notation g(f(x)) — first apply f to x, then apply g to the result. Beginners often reverse the order; watching the animation a few times builds intuition.
Two Rules: Associativity and Identity
With dots and arrows, two rules make it a category.
First rule: Associativity. For three composable arrows f, g, h, composing f and g first then h gives the same arrow as composing g and h first then f: h∘(g∘f) = (h∘g)∘f.
This is like subway transfers: however you mentally group the segments, you still arrive at the same destination. Because of associativity, we can omit parentheses and write h∘g∘f.
Second rule: Identity. Identity arrows act like 1 or 0 in arithmetic: composing any arrow with an identity before or after leaves it unchanged: f∘id_A = f, id_B∘f = f.
That's it — only two rules. The requirements are surprisingly minimal, yet precisely because they are minimal, structures satisfying them appear throughout mathematics.
Categories Everywhere
Seemingly unrelated examples are all categories:
Category Set : objects are sets, arrows are functions, composition is function composition.
Order category (≤) : objects are integers, an arrow a → b exists iff a ≤ b, composition is transitivity (a ≤ b and b ≤ c implies a ≤ c), identity is a ≤ a.
Programming language category : objects are types (Int, String), arrows are functions (e.g., toString: Int → String), composition is chaining functions.
A single group as a category : one object, arrows are group elements, composition is group multiplication.
The second example is especially interesting: in the "less-than-or-equal" category, there is at most one arrow between any two numbers, composition is exactly transitivity, and identity is reflexivity. Middle-school inequalities already contain categorical structure.
Conversely, not everything is a category. If people are objects and "knows" is the arrow: you know Wang, Wang knows Li, but you may not know Li — composition fails, so it's not a category. The test is simply: can arrows be chained head-to-tail?
Isomorphism: Not "What Is It?" But "How Is It Connected?"
If there are arrows f: A → B and g: B → A such that g∘f = id_A and f∘g = id_B, then A and B are isomorphic .
For example, the sets {red, green, blue} and {1, 2, 3} can be paired one-to-one, allowing lossless round-trip conversion. In the category of sets, they are "essentially the same"; only the labels differ.
This embodies the central philosophy of category theory: An object is defined by its relationships with all other objects. This idea was later rigorously expressed as the famous Yoneda Lemma , considered one of the most important theorems in category theory. In plain words: "Tell me your circle of friends, and I'll tell you who you are."
Functors: Translators Between Categories
Since categories are structures themselves, there can be "arrows" between categories — these are functors .
A functor F: C → D does two things: maps each object A in C to an object F(A) in D, and each arrow f to an arrow F(f). Crucially, it preserves structure — translating after composing equals composing after translating: F(g∘f) = F(g)∘F(f).
A map is a perfect real-world analogy: real-world locations correspond to dots on paper, roads to lines; walking road A then road B in reality corresponds to tracing line A then line B on the map. The map may distort distances, but "what connects to what" remains unchanged.
Mathematics has many functors. The "forgetful functor" maps a group to its underlying set, forgetting the multiplication but keeping elements. Algebraic topology maps a space to its fundamental group, using algebra to study shape.
Natural Transformations: Comparison Tables Between Translations
Given two functors F and G both from C to D, we can compare the translations. A natural transformation α assigns to each object A in C an arrow α_A: F(A) → G(A) such that for any arrow f: A → B in C, the following square commutes: G(f)∘α_A = α_B∘F(f).
"Natural" means the transformation does not depend on any special choice; it treats all objects uniformly. A programming example: "take the first element of a list" works the same way for lists of integers, strings, etc. Transforming each element first then taking the first gives the same result as taking the first then transforming — this is naturality in action.
Mac Lane later recalled: categories were invented to define functors, and functors were invented to define natural transformations. Natural transformations are what the theory originally aimed to capture.
Universal Properties: Defining Things by Their Relationships
Category theory has a unique way of defining things called universal properties : instead of describing internal composition, describe the "best position" an object occupies in the web of relationships.
Take the product . The product A×B comes with projection arrows π₁: A×B → A and π₂: A×B → B. Its universality means: for any object X with arrows f: X → A and g: X → B, there exists a unique arrow ⟨f, g⟩: X → A×B such that π₁∘⟨f, g⟩ = f and π₂∘⟨f, g⟩ = g.
Remarkably, the same definition in different categories yields different but corresponding concepts: in Set, product is the Cartesian product (set of ordered pairs); in the ≤ category, product is the minimum of two numbers; in programming, product is a tuple or struct. One definition unifies three seemingly unrelated ideas.
Similarly, initial objects and terminal objects : in Set, the empty set has exactly one function to any set (initial); any singleton set has exactly one function from any set (terminal).
Category Theory in Programming
If you write code, you already use category theory. The essence of programming is composition : combining small functions into larger ones.
const compose = (g, f) => x => g(f(x));
const addOne = x => x + 1;
const double = x => x * 2;
compose(double, addOne)(3); // (3 + 1) * 2 = 8
[1, 2, 3].map(addOne); // [2, 3, 4]Here map is a shadow of a functor. List turns type Int into List<Int>, and map lifts a plain function Int → Int to List<Int> → List<Int> while preserving the list's shape.
Programmers often hear the intimidating phrase: "A monad is just a monoid in the category of endofunctors." It sounds scary, but the intuition is simple: like Promise's then or Option's flatMap, it lets computations that "might fail" or "need to wait" chain as smoothly as ordinary functions. Languages like Haskell, Scala, and Rust draw direct inspiration from category theory.
Summary
Categories consist of dots, arrows, and composition, obeying only associativity and identity. Isomorphism tells us "relationships determine identity." Functors translate between categories. Natural transformations compare translations. Universal properties define things by their relational roles.
Category theory is called "the mathematics of mathematics" not because it is harder, but because it steps back to see the common skeleton shared by different mathematical branches. Next time you look at a subway map, remember: you're not just seeing a transit diagram — you're seeing a small category.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Model Perspective
Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
