~/ ~/documents ~/recommended-reading ~/software ~/pictures github (opens in new tab)

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 (a,b)(a, b), you must define it by constructing sets out of sets.

In 1914, Norbert Wiener proposed encoding the ordered pair (a,b)(a, b) as: {{{a},∅},{{b}}}\{ \{ \{ a \}, \emptyset \}, \{ \{ b \} \} \}

In 1921, Kazimierz Kuratowski proposed a slightly simpler formula: (a,b)={{a},{a,b}}(a, b) = \{ \{ a \}, \{ a, b \} \}

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 AA and BB is a structure that holds an AA, holds a BB, and holds nothing else.

To extract the components, any valid product PP must supply two projection morphisms (or “getters”): π1:P→Aandπ2:P→B\pi_1 : P \to A \quad \text{and} \quad \pi_2 : P \to B

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×B×XA \times B \times X. A triple also has projections to AA (drop the second and third fields) and to BB (drop the first and third fields). In fact, any infinitely complex structure containing AA and BB 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 II is strictly more complex than the true product A×BA \times B. An impostor contains extraneous data, and you can always throw extra data away via a function.

Therefore, if A×BA \times B is the minimal, canonical product, then every candidate II that maps to AA and BB must uniquely factor through A×BA \times B.

Definition (Categorical Product): The product of two objects AA and BB, denoted A×BA \times B, is an object equipped with two projections: π1:A×B→Aandπ2:A×B→B\pi_1 : A \times B \to A \quad \text{and} \quad \pi_2 : A \times B \to B such that for any object II equipped with morphisms p1:I→Ap_1 : I \to A and p2:I→Bp_2 : I \to B, there exists a unique morphism u:I→A×Bu : I \to A \times B making the diagram commute: π1∘u=p1andπ2∘u=p2\pi_1 \circ u = p_1 \quad \text{and} \quad \pi_2 \circ u = p_2

The morphism uu is the mediating arrow (often written ⟨p1,p2⟩\langle p_1, p_2 \rangle). Given an element i∈Ii \in I, it simply constructs the pair (p1(i),p2(i))(p_1(i), p_2(i)).

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 A×BA \times B 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 AA and BB: PP and P′P'.

  1. Because P′P' has projections to AA and BB, the universal property of PP guarantees a unique arrow f:P′→Pf : P' \to P.
  2. Because PP has projections to AA and BB, the universal property of P′P' guarantees a unique arrow g:P→P′g : P \to P'.
  3. Composing them, g∘f:P′→P′g \circ f : P' \to P' must equal idP′\text{id}_{P'}, and f∘g:P→Pf \circ g : P \to P must equal idP\text{id}_P.

Therefore: P≅P′P \cong P'

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 AA and BB, we want to model AA or BB.

In programming, this is a tagged union, sum type, or Either:

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 A+BA + B, we don’t project out; we inject in. We require two injection morphisms: ι1:A→A+Bandι2:B→A+B\iota_1 : A \to A + B \quad \text{and} \quad \iota_2 : B \to A + B

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 AA and BB, denoted A+BA + B, is an object equipped with two injections: ι1:A→A+Bandι2:B→A+B\iota_1 : A \to A + B \quad \text{and} \quad \iota_2 : B \to A + B such that for any object II equipped with morphisms q1:A→Iq_1 : A \to I and q2:B→Iq_2 : B \to I, there exists a unique morphism v:A+B→Iv : A + B \to I making the diagram commute: v∘ι1=q1andv∘ι2=q2v \circ \iota_1 = q_1 \quad \text{and} \quad v \circ \iota_2 = q_2

In functional programming, the mediating morphism vv 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:

This relationship is called Categorical Duality.

For any category 𝒞\mathcal{C}, we can define the opposite category 𝒞op\mathcal{C}^{\text{op}} by keeping the exact same objects and reversing the source and target of every morphism:

f:A→B in 𝒞⇔fop:B→A in 𝒞opf : A \to B \text{ in } \mathcal{C} \iff f^{\text{op}} : B \to A \text{ in } \mathcal{C}^{\text{op}}

The coproduct in 𝒞\mathcal{C} is simply the product in 𝒞op\mathcal{C}^{\text{op}}.

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.


Previous: Part 2: Isomorphism and Structural Sameness

Next: Part 4: The External Universe and Category Axioms