From Sets to Categories: The External Universe and Category Axioms
Part 4 of the “From Sets to Categories” series, exploring mathematical foundations through the lens of composition.
Do we really need to open the thing up to understand it?
If you hand me a calculator, I don’t need to unscrew the back before adding up the shopping. I press the buttons and read the answer. I haven’t learned everything about how it’s built, but I can still learn what it does.
That’s the change of viewpoint we’re trying here. Can we describe our mathematical things by how they connect and what happens when we use them, rather than always looking inside?
Some of the next few ideas may feel like a long way round something you already understand. Fair enough. The reason for trying it is to see whether one small collection of rules can explain several things that initially look quite different.
For over a century, mathematicians treated set theory by inspecting internal elements (), membership tests, and explicit lists.
In the 1960s, Francis William Lawvere introduced the Elementary Theory of the Category of Sets (ETCS), demonstrating that set theory can be formulated without referring to the internal membership of sets:
You can define the entirety of set theory without looking inside a set.
By replacing internal elements with external arrows, concepts such as elements, function evaluation, singletons, empty sets, and products can be reconstructed purely through composition.
graph LR
subgraph ETCS ["ETCS: Point as an Arrow"]
One["Terminal Object (1)"] -->|"x (an element)"| X["Set X"]
X -->|"f (a function)"| Y["Set Y"]
One -.->|"f ∘ x (function application)"| Y
end
1. What is an Element?
If we are forbidden from opening up a set to look at its contents, how do we define an element ?
Consider the functions from the singleton set into a three-element set :
graph LR
subgraph Singleton ["1 = {*}"]
star["*"]
end
subgraph Target ["X = { a, b, c }"]
a["a"]
b["b"]
c["c"]
end
star -->|"x_a"| a
star -->|"x_b"| b
star -->|"x_c"| c
How many functions exist from to ? Exactly three:
- A function mapping to .
- A function mapping to .
- A function mapping to .
Every element of corresponds to exactly one unique function from the singleton set into :
An element of an object is defined as an arrow . In category theory, these are called generalized points.
If this feels like a complicated way to pick something, that’s fair.
Imagine a little machine with one button and no settings. Press it and it always prints “tea”. Another machine always prints “coffee”. If all we’re interested in is the printed answer, telling you which answer each machine gives tells you everything you need to know about it.
That’s what the arrow from the one-element set is doing: picking one answer. We haven’t made “tea” any more interesting by drawing an arrow to it. We’ve found another way to describe the choice, one we can connect to the other arrows on the page.
2. Defining the Singleton Externally (Terminal Object)
This element definition seems circular: it relies on the concept of the singleton set . But how can we define a “one-element set” if we aren’t allowed to count elements?
We look for a universal property that uniquely distinguishes the singleton set from every other set in the universe.
In Part 1, we noted that for every set , there is a unique function that maps all elements of to the single element of :
No other set has this property. If a set has zero elements, there are zero functions into it from non-empty sets. If a set has two elements, there are functions into it. Only the singleton set has exactly one incoming arrow from every object.
Definition (Terminal Object): An object in a category is terminal if for every object , there exists a unique morphism from to :
graph TD
A["Object A"] -->|"!"| One((1))
B["Object B"] -->|"!"| One
C["Object C"] -->|"!"| One
If two objects and are both terminal, the universal property guarantees unique arrows and . Their compositions must equal the respective identity morphisms, proving:
The singleton set is uniquely defined up to unique isomorphism
without counting to one. In Haskell, this is the () type:
const () :: a -> ().
What if the only reply you could send was “received”?
I send you a shopping list: “received”. I send you a birthday invitation: “received”. Whatever the message, that’s the only reply available. We’re leaving aside whether you actually agree to do the shopping or come to the party.
With only that one possible answer, there’s no decision about what to send back. Add “yes” and “no” instead, and now we need a rule for choosing. It’s that lack of choice in the one-answer case that the arrows above describe. “Terminal” is the name for this property; it doesn’t mean we’ve reached the last step of a process.
3. The Empty Set Externally (Initial Object)
What is the categorical dual of the terminal object? Reverse the arrows:
Definition (Initial Object): An object in a category is initial if for every object , there exists a unique morphism from to :
graph TD
Zero((0)) -->|"!"| A["Object A"]
Zero -->|"!"| B["Object B"]
Zero -->|"!"| C["Object C"]
In the category of sets, the initial object is the empty
set
.
There is exactly one function from the empty set to any set: the empty
function (in Haskell, absurd :: Void -> a).
Duality reveals the symmetry of the extremes:
- Terminal Object (): Every object maps into it in exactly one way. (The consumer of all data; Unit).
- Initial Object (): It maps into every object in exactly one way. (The uninstantiable source; Void).
A blank shopping list is still something you can hand me.
It might not make for a very exciting trip to the shops, but the list exists. You can fold it up and put it in your pocket. That’s different from having no possible thing to hand over in the first place.
The empty set is the second idea: there isn’t even one item we could give the rule as an input. So if I ask you to assign an answer to every item in it, you’ve already finished. There are no items and no choices to make. That’s why there is just one such assignment, whatever set of answers we allow. It isn’t a magic machine producing answers from nothing; it never receives an input at all.
4. Function Application as Composition
In traditional programming, you pass an argument to a function:
In category theory, there are no “arguments”. Everything is a morphism.
If an element is represented by the arrow , and a function is an arrow , how do you “apply” the function to the element?
You simply compose them:
graph LR
One((1)) -->|"x"| X((X))
X -->|"f"| Y((Y))
One -.->|"f ∘ x"| Y
The composite arrow is a morphism from into . By our earlier definition, a morphism is an element of .
Function evaluation is not a separate primitive machine operation. It is merely the composition of an element arrow with a transformation arrow.
Let’s use the one-button machine again.
This time, pressing the button always gives us 3. Feed that answer into a second machine that doubles whatever number it gets. Press the button, out comes 3, then out comes 6.
We could put both machines in one box with one button on the front. That box would always give us 6. That’s all the joined-up arrows are saying here.
We still did the familiar calculation “double 3”. We’ve just described it as picking a number and then passing it along. The advantage isn’t better arithmetic; it’s being able to describe this with the same connecting rule we’ve been using elsewhere.
5. The Formal Axioms of a Category
The primary set-theoretic concepts (sets, elements, pairs, sums, singletons, empty sets, and function evaluations) can all be defined using objects, arrows, and composition.
We are ready to define the general structure that encompasses all of this: the Category.
Formal Definition: A category consists of:
- A collection of Objects: .
- For every pair of objects , a collection of Morphisms (arrows): .
- A binary Composition operator:
satisfying two axioms:
- Identity Law: For every object , there exists an identity morphism such that for any :
- Associativity Law: For all composable morphisms , , and :
graph LR
A((A)) -->|"f"| B((B))
B -->|"g"| C((C))
C -->|"h"| D((D))
A -.->|"g ∘ f"| C
B -.->|"h ∘ g"| D
A ==>|"h ∘ (g ∘ f) = (h ∘ g) ∘ f"| D
A -->|"id_A"| A
That is the entire definition. There are no other rules.
This is the small set of rules I wanted to get to.
Suppose we’re tidying the wording on some labels. We have one rule for removing extra spaces, another for turning letters into capitals, and another for adding “For delivery:” at the start. Each takes some text and gives us text back, so we can follow one with another.
We can also leave a label alone. And we can bundle several rules into one job without changing their order or what they do.
Keep all those rules, every combination we can make from them, and the option of doing nothing. That’s an example of a category. The one object is the set of all possible pieces of text; the arrows are the rules. There’s a lot of notation above, but these are the promises it’s asking us to keep.
6. The Single-Object Universe: Monoids
A category does not require multiple objects or complex networks. You do not need multiple objects to have a valid category.
What happens when a category has exactly one object?
graph LR
S(( * ))
S -->|"rotate_90"| S
S -->|"invert_color"| S
S -->|"scale_2x"| S
S -->|"id (do_nothing)"| S
In a single-object category:
- There is only one object, which we can label .
- Every morphism has source and target : . These self-arrows are called endomorphisms.
- Any two morphisms can be composed: for , the composition is always defined.
- Composition is associative: .
- There is an identity morphism .
Now recall the classical definition of a Monoid from abstract algebra:
A monoid is a set equipped with an associative binary operation and an identity element such that:
Hang on, didn’t we just have lots of different labels?
Yes, and we still do. When we say “one object” in that example, we mean the whole set of possible texts. We aren’t saying every label has the same words on it.
All our tidying rules start with text and finish with text. Draw that whole set as one dot, and each rule becomes an arrow looping back to it. There can be plenty of different arrows around that one dot.
The word “monoid” packages up those rules and how we follow one with another. One object, lots of possible actions. That’s the distinction to keep in mind.
They are the exact same structure:
| Classical Abstract Algebra | Category Theory (Single-Object View) |
|---|---|
| Set of elements | The collection of morphisms |
| Element | A single self-arrow |
| Binary operation | Arrow composition |
| Identity element | Identity morphism |
| Associativity of | Associativity of |
A monoid is simply a category with exactly one object.
In software engineering, monoids enable parallel aggregation, logging, and folding:
Stringconcatenation with""as identity.- Integer addition with
0as identity (Sum). - Integer multiplication with
1as identity (Product). - List concatenation with
[]as identity. - Function endomorphisms
a -> awithidas identity under function composition.
// In Gleam, monoidal structure allows arbitrary reduction
pub fn fold_monoid(items: List(m), empty: m, combine: fn(m, m) -> m) -> m {
case items {
[] -> empty
[first, ..rest] -> combine(first, fold_monoid(rest, empty, combine))
}
}Viewing a monoid through the lens of category theory shows that an algebraic operation is equivalent to consecutive traversal of arrows around a single point.
We can try these rules with three scraps of paper.
Write “milk” on the first, “bread” on the second, and “tea” on the third. We want one list, in that order. You could copy the first two onto a list and add the third, or copy the last two together and put the first in front. Either way: milk, bread, tea.
A blank list doesn’t add any shopping, whichever end we join it to. So we have a way to combine lists, freedom to group the joining work, and an empty list that changes nothing. Those are the monoid rules, with the shopping still attached. We haven’t said we can shuffle the items; we’re keeping their order.
Part 5 examines functors and natural transformations as morphisms between categories.