From Sets to Categories: Universal Constructions and Duality
Part 3 of the “From Sets to Categories” series, exploring mathematical foundations through the lens of composition.
Let’s book an appointment before we do any more maths.
You need a date and a time, don’t you? “Tuesday” isn’t much help if nobody tells you when to turn up. Once you’ve asked for the appointment, you might get a confirmation with a booking number, or a message saying it’s unavailable.
Keeping two things together, or having one thing or another. That’s where we’re heading. The maths calls these “products” and “sums”, which makes them sound rather less familiar than they are. Keep the appointment in mind while we work through the notation. I want to show you what those names help us describe, not give you another vocabulary test.
In traditional set theory, if you want to create a compound data structure like a pair , you must define it by constructing sets out of sets.
In 1914, Norbert Wiener proposed encoding the ordered pair as:
In 1921, Kazimierz Kuratowski proposed a slightly simpler formula:
These set-theoretic encodings construct ordered pairs by nesting sets. Category theory instead characterizes the ordered pair by its behavioral properties: how a product relates to other objects via arrows.
graph TD
I["Candidate / Impostor I"]
P["Product A × B"]
A["Object A"]
B["Object B"]
I -->|"p₁"| A
I -->|"p₂"| B
P -->|"π₁ (fst)"| A
P -->|"π₂ (snd)"| B
I -.->|"∃! u (mediating arrow)"| P
1. Defining Products Externally
Conceptually, a product of and is a structure that holds an , holds a , and holds nothing else.
To extract the components, any valid product must supply two projection morphisms (or “getters”):
In Haskell or Gleam, these are simply fst and
snd.
// Projections in Gleam
pub fn fst(pair: #(a, b)) -> a {
pair.0
}
pub fn snd(pair: #(a, b)) -> b {
pair.1
}Is having projections enough to define a product?
No. Consider an “impostor product”: the triple . A triple also has projections to (drop the second and third fields) and to (drop the first and third fields). In fact, any infinitely complex structure containing and plus ten extra fields satisfies this condition.
How do we eliminate the impostors without looking inside?
Suppose I ask you for someone’s name and phone number.
You could write those two things on a scrap of paper. Or you could hand me a whole address book, complete with birthdays and notes about who everyone is. Either way, I can find the name and number.
So being able to get those two answers doesn’t tell me whether you’ve given me just the pair I asked for or a lot more besides. That’s the snag with the two arrows above. We need one more rule to pin down what we mean by just the pair.
2. The Universal Property of Products
The key insight is that an impostor is strictly more complex than the true product . An impostor contains extraneous data, and you can always throw extra data away via a function.
Therefore, if is the minimal, canonical product, then every candidate that maps to and must uniquely factor through .
Definition (Categorical Product): The product of two objects and , denoted , is an object equipped with two projections: such that for any object equipped with morphisms and , there exists a unique morphism making the diagram commute:
The morphism is the mediating arrow (often written ). Given an element , it simply constructs the pair .
Because this arrow is unique, there is only one way for the candidate to project its information into the product. The universal property acts as a filter: it pins down as the optimal, lossless representation of pairing.
Let’s put a name and address on an envelope.
Suppose you’ve got a page of someone’s details: their name, address, birthday, favourite colour, all sorts. For this job, we only want their name and address. Once we’ve agreed which name and address to use, what goes into our pair? Those two answers, together. There’s nothing else to decide.
That’s what “unique” is doing here. It isn’t saying there’s only one way to write an envelope. It’s saying that if we’re keeping those two answers unchanged, there’s only one pair of answers we can give. All the arrows are making that quite ordinary requirement precise.
Uniqueness Up to Unique Isomorphism
Suppose two different mathematicians invent two different products for and : and .
- Because has projections to and , the universal property of guarantees a unique arrow .
- Because has projections to and , the universal property of guarantees a unique arrow .
- Composing them, must equal , and must equal .
Therefore:
The universal property does not dictate a single memory layout (Kuratowski’s pairs vs Wiener’s pairs vs a C struct). It guarantees that all valid implementations are uniquely isomorphic.
Yours can look different from mine and still do the same job.
You might write the name above the address, while I put them in two boxes beside each other. We haven’t disagreed about the person or where they live. We’ve just laid out the same two answers differently.
If both forms hold exactly that information, we can copy from yours to mine and back without losing anything. And if we insist on keeping the name as the name and the address as the address, there’s no choice about which answer goes where. That’s the rather long phrase “unique up to unique isomorphism” at work.
3. The Coproduct (Sum)
Now consider the dual problem. Instead of combining and , we want to model or .
In programming, this is a tagged union, sum type, or
Either:
- In Rust:
enum Result<T, E> { Ok(T), Err(E) } - In Gleam:
type Choice { Left(A); Right(B) }
graph TD
A["Object A"]
B["Object B"]
S["Coproduct A + B"]
I["Candidate / Impostor I"]
A -->|"ι₁ (inl)"| S
B -->|"ι₂ (inr)"| S
A -->|"q₁"| I
B -->|"q₂"| I
S -.->|"∃! v (mediating arrow)"| I
To construct a sum , we don’t project out; we inject in. We require two injection morphisms:
What is the universal property of the coproduct? It is the exact mirror image of the product:
Definition (Categorical Coproduct): The coproduct of two objects and , denoted , is an object equipped with two injections: such that for any object equipped with morphisms and , there exists a unique morphism making the diagram commute:
In functional programming, the mediating morphism
is pattern matching (or either):
// Pattern matching is the universal mediating arrow of coproducts
pub fn match_choice(choice: Choice, on_a: fn(A) -> I, on_b: fn(B) -> I) -> I {
case choice {
Left(a) -> on_a(a)
Right(b) -> on_b(b)
}
}If I just tell you “12”, what have I told you?
Perhaps you’ve booked a ticket and I say you’re number 12. Do you have seat 12, or are you twelfth on the waiting list? You’d quite reasonably want to know before setting off.
We need to keep “seat” or “waiting list” alongside the number. Then we can write the right message: tell you where to sit, or explain that you’re still waiting. That’s what the code above does. We say how to handle each possibility, and the label tells us which instructions to follow.
4. Categorical Duality: Flipping the Arrows
Let’s come back to ordering something for delivery.
For home delivery, we need a recipient and an address. Both, not a choice between them. But the order itself might be either a home delivery or a collection from a shop. Different arrangements, and we already know how to deal with both.
With the name-and-address pair, we can ask for either piece of information. With delivery-or-collection, we have to be ready to handle whichever choice was made. Have a look at the arrows below: the first arrangement points out towards the two pieces, and the second points in from the two possibilities.
That’s the connection we’re about to call “duality”. We’re turning the mathematical description around, not claiming we can run a parcel delivery backwards.
Compare the diagrams for the product and the coproduct:
graph LR
subgraph Product ["Product: A × B"]
PA["A"]
PB["B"]
P["A × B"]
P -->|"π₁"| PA
P -->|"π₂"| PB
end
subgraph Coproduct ["Coproduct: A + B"]
CA["A"]
CB["B"]
C["A + B"]
CA -->|"ι₁"| C
CB -->|"ι₂"| C
end
They are the exact same diagram, with every single arrow reversed:
- Projections () become Injections ().
- The incoming mediating arrow () becomes an outgoing mediating arrow ().
- “And” (Product) becomes “Or” (Coproduct).
- Structs and records become Enums and tagged unions.
This relationship is called Categorical Duality.
For any category , we can define the opposite category by keeping the exact same objects and reversing the source and target of every morphism:
The coproduct in is simply the product in .
Every theorem proved for products yields a dual theorem for coproducts by reversing the arrows.
Part 4 applies these constructions to define elements, singletons, and the empty set externally, leading to the formal axioms of a category.