โ† back to index

Products and Coproducts

Bartosz Milewski ยท 2015 ยท Category Theory for Programmers, Ch. 5

Prereqs: ๐Ÿž Chapter 1 (Category), ๐Ÿž Chapter 2 (Types and Functions). 8 min.

Tuples and tagged unions aren't language features. They're universal constructions: the product is the best object with two projections, the coproduct is the best object with two injections. "Best" means every other candidate factors through it uniquely.

Initial and terminal objects

A terminal object has exactly one morphism coming from every other object. In sets, it's a singleton. In Python, it's None: every function can return None, and there's only one way to do it. An initial object has exactly one morphism going to every other object. In sets, it's the empty set. Both are unique up to isomorphism.

Scheme

Universal construction โ€” ranking candidates

Category theory defines objects by their relationships, not their internals. The universal construction is a three-step recipe: (1) define a pattern of objects and morphisms, (2) rank all candidates by whether one factors through another, (3) pick the best one. "Best" means every other candidate factors through it via a unique morphism.

Scheme

Products โ€” tuples from projections

The product of objects a and b is an object c with two projections fst : c โ†’ a and snd : c โ†’ b, such that for any other candidate c' with projections p' and q', there exists a unique morphism m : c' โ†’ c where p' = fst โˆ˜ m and q' = snd โˆ˜ m. In programming, the product is a tuple.

C A B A×B f g m p₁ p₂
Scheme

Coproducts โ€” tagged unions from injections

Reverse the arrows and you get the coproduct. Instead of projections out, you have injections in: Left : a โ†’ c and Right : b โ†’ c. The universal property: for any other candidate c' with injections i' and j', there exists a unique morphism m : c โ†’ c' where i' = m โˆ˜ Left and j' = m โˆ˜ Right. In programming, the coproduct is Either (Haskell) or a tagged union.

A B A+B C i₁ i₂ f g (f,g)
Scheme

Duality โ€” reverse the arrows

Every categorical construction has a dual: reverse all the arrows. The product's dual is the coproduct. The terminal object's dual is the initial object. Projections become injections. If you prove something about products, you get a theorem about coproducts for free. This is the power of the opposite category Cop.

Scheme

Why products and coproducts aren't symmetric in Set

Functions must be defined on their entire domain but don't have to cover their codomain. This makes products and coproducts behave differently in the category of sets. A "bad" product candidate can lose information (too few projections), but a "bad" coproduct candidate can collapse information (map distinct values to the same output). The asymmetry comes from functions themselves, not from the constructions.

Scheme

Notation reference

Blog / Haskell Scheme Meaning
()'()Terminal object (unit)
Void(error ...)Initial object (empty type)
fst :: (a, b) โ†’ a(car p)First projection
snd :: (a, b) โ†’ b(cadr p)Second projection
Left :: a โ†’ Either a b(Left x)Left injection
Right :: b โ†’ Either a b(Right x)Right injection
factorizer p q = \x โ†’ (p x, q x)(factorizer p q)Product factorizer
factorizer i j (Left a) = i a(factorizer-coprod i j)Coproduct factorizer
Neighbors

Milewski chapters

Paper pages that use products and coproducts

Foundations (Wikipedia)

Translation notes

The blog post uses Haskell tuples and Either, plus C++ std::pair and tagged unions. This page uses Scheme lists as pairs and tagged lists as Either. The factorizers are direct translations of Milewski's Haskell definitions. The "ranking candidates" section (universal construction) is presented concretely: a triple is a worse product candidate than a pair because the triple factors through the pair. The blog post also discusses the asymmetry between products and coproducts in Set, which comes from functions being total (defined everywhere) but not necessarily surjective (covering the codomain).

Ready for the real thing? Read the blog post. The universal construction pattern here recurs in every chapter from here on.