The Dao of Functional Programming
The Dao of Functional Programming
Bartosz Milewski
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
1 Clean Slate 1
1.1 Types and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Yin and Yang . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 The Object of Arrows . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2 Composition 5
2.1 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Function application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3 Isomorphisms 11
3.1 Isomorphic Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.2 Naturality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.3 Reasoning with Arrows . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Reversing the Arrows . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
i
ii CONTENTS
4 Sum Types 19
4.1 Bool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.2 Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Short Haskell Digression . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.3 Sum Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Maybe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
4.4 Cocartesian Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
One Plus Zero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Something Plus Zero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Commutativity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Associativity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Functoriality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Symmetric Monoidal Category . . . . . . . . . . . . . . . . . . . . . . . 29
5 Product Types 31
Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Tuples and Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.1 Cartesian Category . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Tuple Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Functoriality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.2 Duality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.3 Monoidal Category . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Monoids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
6 Function Types 39
Elimination rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Introduction rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Currying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Relation to lambda calculus . . . . . . . . . . . . . . . . . . . . . . . . . 42
Modus ponens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
6.1 Sum and Product Revisited . . . . . . . . . . . . . . . . . . . . . . . . . 43
Sum types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Product types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Functoriality revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
6.2 Functoriality of the Function Type . . . . . . . . . . . . . . . . . . . . . 46
6.3 Bicartesian Closed Categories . . . . . . . . . . . . . . . . . . . . . . . . 47
Distributivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
7 Recursion 51
7.1 Natural Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Introduction Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Elimination Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
In Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
7.2 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Elimination Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
7.3 Functoriality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
CONTENTS iii
8 Functors 59
8.1 Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Category of sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Opposite and product categories . . . . . . . . . . . . . . . . . . . . . . 60
8.2 Functors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Functors between categories . . . . . . . . . . . . . . . . . . . . . . . . . 61
8.3 Functors in Programming . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Endofunctors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Bifunctors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Contravariant functors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Profunctors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
8.4 The Hom-Functor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
8.5 Functor Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Category of categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
9 Natural Transformations 69
9.1 Natural Transformations Between Hom-Functors . . . . . . . . . . . . . 69
9.2 Natural Transformation Between Functors . . . . . . . . . . . . . . . . . 71
9.3 Natural Transformations in Programming . . . . . . . . . . . . . . . . . 72
9.4 The Functor Category . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Vertical composition of natural transformations . . . . . . . . . . . . . . 73
Horizontal composition of natural transformations . . . . . . . . . . . . 75
Whiskering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Interchange law . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
9.5 Universal Constructions Revisited . . . . . . . . . . . . . . . . . . . . . 77
Picking objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Cospans as natural transformations . . . . . . . . . . . . . . . . . . . . . 78
Functoriality of cospans . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Sum as a universal cospan . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Product as a universal span . . . . . . . . . . . . . . . . . . . . . . . . . 81
Exponentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
9.6 Limits and Colimits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
9.7 The Yoneda Lemma . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Yoneda lemma in programming . . . . . . . . . . . . . . . . . . . . . . . 86
The contravariant Yoneda lemma . . . . . . . . . . . . . . . . . . . . . . 87
9.8 Yoneda Embedding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
9.9 Representable Functors . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
The guessing game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Representable functors in programming . . . . . . . . . . . . . . . . . . 90
9.10 2-category Cat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
9.11 Useful Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
10 Adjunctions 93
10.1 The Currying Adjunction . . . . . . . . . . . . . . . . . . . . . . . . . . 93
10.2 The Sum and the Product Adjunctions . . . . . . . . . . . . . . . . . . . 94
The diagonal functor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
The sum adjunction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
The product adjunction . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
iv CONTENTS
12 Algebras 129
12.1 Algebras from Endofunctors . . . . . . . . . . . . . . . . . . . . . . . . . 130
12.2 Category of Algebras . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Initial algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
12.3 Lambek’s Lemma and Fixed Points . . . . . . . . . . . . . . . . . . . . . 132
Fixed point in Haskell . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
12.4 Catamorphisms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Lists as initial algebras . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
12.5 Initial Algebra from Universality . . . . . . . . . . . . . . . . . . . . . . 137
CONTENTS v
13 Coalgebras 143
13.1 Coalgebras from Endofunctors . . . . . . . . . . . . . . . . . . . . . . . 143
13.2 Category of Coalgebras . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
13.3 Anamorphisms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Infinite data structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
13.4 Hylomorphisms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
The impedance mismatch . . . . . . . . . . . . . . . . . . . . . . . . . . 148
13.5 Terminal Coalgebra from Universality . . . . . . . . . . . . . . . . . . . 149
13.6 Terminal Coalgebra as a Limit . . . . . . . . . . . . . . . . . . . . . . . 150
14 Monads 153
14.1 Programming with Side Effects . . . . . . . . . . . . . . . . . . . . . . . 153
Partiality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Nondeterminism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Continuation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
14.2 Composing Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
14.3 Alternative Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
14.4 Monad Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Partiality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Nondeterminism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Continuation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
14.5 Do Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
14.6 Monads Categorically . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Monad as a monoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
14.7 Free Monads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Category of monads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Free monad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Stack calculator example . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
14.8 Monoidal Functors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Lax monoidal functors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Functorial strength . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Applicative functors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Closed functors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Monads and applicatives . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
16 Comonads 197
16.1 Comonads in Programming . . . . . . . . . . . . . . . . . . . . . . . . . 197
The Stream comonad . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
16.2 Comonads Categorically . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Comonoids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
16.3 Comonads from Adjunctions . . . . . . . . . . . . . . . . . . . . . . . . 201
Costate comonad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Comonad coalgebras . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Lenses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
20 Enrichment 273
viii CONTENTS
Index 287
PREFACE ix
Preface
Most programming texts, following Brian Kernighan, start with ”Hello World!”. It’s
natural to want to get the immediate gratification of making the computer do your
bidding and print these famous words. But the real mastery of computer programming
goes deeper than that, and rushing into it may only give you a false feeling of power,
when in reality you’re just parroting the masters. If your ambition is just to learn a
useful, well-paid skill then, by all means, write your ”Hello World!” program. There
are tons of books and courses that will teach you to write code in any language of your
choice. However, if you really want to get to the essence of programming, you need to
be patient and persistent.
Conventions
I tried to keep the notation coherent throughout the book. In particular, I decided to
use lowercase letters like a or b for objects in a category and uppercase names like S
for sets, even though sets are objects in the category of sets and functions. Generic
categories have names like C or D, whereas specific categories are named like Set or
Cat.
Programming examples are written in Haskell. Although this is not a Haskell man-
ual, the introduction of language constructs is gradual enough to help the reader nav-
igate the code. The fact that Haskell syntax is often based on mathematical notation
is an additional advantage. Program fragments are written in the following format:
apply :: (a -> b, a) -> b
apply (f, x) = f x
Chapter 1
Clean Slate
Programming starts with types and functions. You probably have some preconceptions
about what types and functions are: get rid of them! They will cloud your mind.
Don’t think about how things are implemented in hardware. What computers are is
just one of the many models of computation. We shouldn’t get attached to it. You can
perform computations in your mind, or with pen and paper. The physical substrate is
irrelevant to the idea of programming.
Paraphrasing Lao Tzu: The type that can be described is not the eternal type. In other
words, type is a primitive notion. It cannot be defined.
Instead of calling it a type, we could as well call it an object or a proposition. These
are the words that are used to describe it in different areas of mathematics (type theory,
category theory, and logic, respectively).
There may be more than one type, so we need a way to name them. We could do it
by pointing fingers at them, but since we want to effectively communicate with other
people, we usually name them. So we’ll talk about type a, b, c; or Int, Bool, Double,
and so on. These are just names.
A type by itself has no meaning. What makes it special is how it connects to other
types. The connections are described by arrows. An arrow has one type as its source
and one type as its target. The target could be the same as the source, in which case
the arrow loops around.
An arrow between types is called a function. An arrow between objects is called
a morphism. An arrow between propositions is called an entailment. These are just
words that are used to describe arrows in different areas of mathematics. You can use
them interchangeably.
A proposition is something that may be true. In logic, we interpret an arrow between
two objects as a entails b, or b is derivable from a.
1
2 CHAPTER 1. CLEAN SLATE
There may be more than one arrow between two types, so we need to name them.
For instance, here’s an arrow called f that goes from type a to type b
f
a−
→b
One way to interpret this is to say that the function f takes an argument of type a
and produces a result of type b. Or that f is a proof that if a is true then b is also true.
Note: The connection between type theory, lambda calculus (which is the founda-
tion of programming), logic, and category theory is known as Curry-Howard-Lambek
correspondence.
Void
A programming language lets us communicate with each other and with computers.
Some languages are easier for the computer to understand, others are closer to the
theory. We will use Haskell as a compromise.
In Haskell, the initial object corresponds to the type called Void. The name for the
terminal type is (), a pair of empty parentheses, pronounced Unit. This notation will
make sense later.
There are infinitely many types in Haskell, and there is a unique function/arrow
from Void to each one of them. All these functions are known under the same name:
absurd.
1.3 Elements
An object has no parts but it may have structure. The structure is defined by the
arrows pointing at the object. We can probe the object with arrows.
In programming and in logic we want our initial object to have no structure. So
we’ll assume that it has no incoming arrows (other than the one that’s looping back
from it). Therefore Void has no structure.
The terminal object has the simplest structure. There is only one incoming arrow
from any object to it: there is only one way of probing it from any direction. In this
respect, the terminal object behaves like an indivisible point. Its only property is that
it exists, and the arrow from any other object proves it.
Because the terminal object is so simple, we can use it to probe other, more complex
objects.
If there is more than one arrow coming from the terminal object to some object a,
it means that a has some structure: there is more than one way of looking at it. Since
the terminal object behaves like a point, we can visualize each arrow from it as picking
a different point or element of its target.
In category theory we say that x is a global element of a if it’s an arrow
x
1−
→a
We’ll often simply call it an element (omitting “global”).
In type theory, x : A means that x is of type A.
In Haskell, we use the double-colon notation instead:
x :: A
(Haskell uses capitalized names for concrete types, and lower-cased names for type
variables.)
We say that x is a term of type A but, categorically, we’ll interpret it as an arrow
x : 1 → A, a global element of A. 1
1
The Haskell type system distinguishes between x :: A and x :: () -> A. However, they denote
the same thing in categorical semantics.
4 CHAPTER 1. CLEAN SLATE
Composition
2.1 Composition
Programming is about composition. Paraphrasing Wittgenstein, one could say: “Of
that which cannot be decomposed one should not speak.” This is not a prohibition,
it’s a statement of fact. The process of studying, understanding, and describing is the
same as the process of decomposing; and our language reflects this.
The reason we have built the vocabulary of objects and arrows is precisely to express
the idea of composition.
Given an arrow f from a to b and an arrow g from b to c, their composition is an
arrow that goes directly from a to c. In other words, if there are two arrows, the target
of one being the same as the source of the other, we can always compose them to get a
third arrow.
a f
b g c
h=g◦f
We read this: “h is equal to g after f .” The order of composition might seem backward,
but this is because we think of functions as taking arguments on the right. In Haskell
we replace the circle with a dot:
h = g . f
This is every program in a nutshell. In order to accomplish h, we decompose it into
simpler problems, f and g. These, in turn, can be decomposed further, and so on.
Now suppose that we were able to decompose g itself into j ◦ k. We have
h = (j ◦ k) ◦ f
h = j ◦ (k ◦ f )
5
6 CHAPTER 2. COMPOSITION
We want to be able to say that we have decomposed h into three simpler problems
h=j◦k◦f
and not have to keep track which decomposition came first. This is called associativity
of composition, and we will assume it from now on.
Composition is the source of two mappings of arrows called pre-composition and
post-composition.
When an arrow f is post-composed after an arrow h, it produces the arrow f ◦ h.
Of course, f can be post-composed only after arrows whose target is the source of f .
Post-composition by f is written as (f ◦ −), leaving a hole for h. As Lao Tzu would
say, “Usefulness of post-composition comes from what is not there.”
Thus an arrow f : a → b induces a mapping of arrows (f ◦ −) that maps arrows
which are probing a to arrows which are probing b.
(f ◦ −)
a b
f
Since objects have no internal structure, when we say that f transforms a to b, this is
exactly what we mean.
Post-composition lets us shift focus from one object to another.
Dually, you can pre-compose f , or apply (− ◦ f ) to arrows originating in b and map
them to arrows originating in a.
f
a b
(− ◦ f )
Pre-composition let us shift the perspective from one observer to another. Notice
that the outgoing arrows are mapped in the direction opposite to the shifting arrow f .
Another way of looking at pre- and post-composition is that they are the result of
partial application of the two-hole composition operator (− ◦ −), in which we pre-fill
one hole or the other with an arrow.
In programming, an outgoing arrow is interpreted as extracting data from the source.
An incoming arrow is interpreted as producing or constructing the target. Outgoing
arrows define the interface, incoming arrows define the constructors.
Do the following exercises to convince yourself that shifts in focus and perspective
are composable.
Exercise 2.1.1. Suppose that you have two arrows, f : a → b and g : b → c. Their
composition g ◦ f induces a mapping of arrows ((g ◦ f ) ◦ −). Show that the result is the
same if you first apply (f ◦ −) and follow it by (g ◦ −). Symbolically:
((g ◦ f ) ◦ −) = (g ◦ −) ◦ (f ◦ −)
2.2. FUNCTION APPLICATION 7
Hint: Pick an arbitrary object x and an arrow h : x → a and see if you get the same
result.
Exercise 2.1.2. Convince yourself that the composition from the previous exercise is
associative. Hint: Start with three composable arrows.
(− ◦ (g ◦ f )) = (− ◦ f ) ◦ (− ◦ g)
These two arrows are composable (they share the object a in the middle) and their
composition is the arrow y from 1 to b. In other words, y is an element of b
y
1 x a f
b
We can write it as
y =f ◦x
We used f to map an element of a to an element of b. Since this is something we
do quite often, we call it the application of a function f to x, and use the shorthand
notation
y = fx
Let’s translate it to Haskell. We start with an element x of a (a shorthand for ()->a)
x :: a
We declare a function f as an element of the “object of arrows” from a to b
f :: a -> b
with the understanding (which will be elaborated upon later) that it corresponds to an
arrow from a to b. The result is an element of b
y :: b
and it is defined as
y = f x
We call this the application of a function to an argument, but we were able to express
it purely in terms of function composition. (Note: In other programming languages
function application requires the use of parentheses, e.g., y = f(x).)
8 CHAPTER 2. COMPOSITION
2.3 Identity
You may think of arrows as representing change: object a becomes object b. An arrow
that loops back represents a change in an object itself. But change has its dual: lack
of change, inaction or, as Lao Tzu would say wu wei.
Every object has a special arrow called the identity, which leaves the object un-
changed. It means that, when you compose this arrow with any other arrow, either
incoming or outgoing, you get that arrow back. Identity arrow does nothing to an
arrow.
An identity arrow on the object a is called ida . So if we have an arrow f : a → b,
we can compose it with identities on either side
idb ◦ f = f = f ◦ ida
or, pictorially:
idb
ida
f
a b
We can easily check what an identity does to elements. Let’s take an element
x : 1 → a and compose it with ida . The result is:
ida ◦ x = x
id id
Void ()
In logic, identity arrow translates to a tautology. It’s a trivial proof that, “if a is
true then a is true.” It’s also called the identity rule.
If identity does nothing then why do we care about it? Imagine going on a trip,
composing a few arrows, and finding yourself back at the starting point. The question
is: Have you done anything, or have you wasted your time? The only way to answer
this question is to compare your path with the identity arrow.
Some round trips bring change, others don’t.
2.3. IDENTITY 9
Exercise 2.3.1. What does (ida ◦−) do to arrows terminating in a? What does (−◦ida )
do to arrows originating from a?
Chapter 3
Isomorphisms
f ◦(g◦h)
id
g◦h
h g f f
a b c d a b
f ◦g
(f ◦g)◦h
f
a b
g
There are two possible round trips. One is g ◦ f , which goes from a to a. The other
is f ◦ g , which goes from b to b.
If both of them result in identities, than we say that g is the inverse of f
g ◦ f = idA
11
12 CHAPTER 3. ISOMORPHISMS
f ◦ g = idB
and we write it as g = f −1 (pronounced f inverse). The arrow f −1 undoes the work of
the arrow f .
Such a pair of arrows is called an isomorphism and the two objects are called
isomorphic.
What does the existence of an isomorphisms tell us about the two objects they
connect?
We have said that objects are described by their interactions with other objects.
So let’s consider what the two isomorphic objects look like from the perspective of an
observer object x. Take an arrow h coming from x to a.
x
h
f
a b
f −1
x
h f ◦h
f
a b
f −1
Similarly, for any arrow probing b there is a corresponding arrow probing a. It is given
by the action of (f −1 ◦ −).
We can move focus back and forth between a and b using the mappings (f ◦ −) and
−1
(f ◦ −).
We can combine these two mappings (see exercise 2.1.1) to form a round trip. The
result is the same as if we applied the composite ((f −1 ◦ f ) ◦ −). But this is equal to
(idA ◦ −) which, as we know from exercise 2.3.1, leaves the arrows unchanged.
Similarly, the round trip induced by f ◦ f −1 leaves the arrows x → b unchanged.
This creates a “buddy system” between the two groups of arrows. Imagine each
arrow sending a message to its buddy, as determined by f or f −1 . Each arrow would
then receive exactly one message, and that would be a message from its buddy. No
arrow would be left behind, and no arrow would receive more than one message. Math-
ematicians call this kind of buddy system a bijection or one-to-one correspondence.
Therefore, arrow by arrow, the two objects a and b look exactly the same from the
perspective of x. Arrow-wise, there is no difference between the two objects.
In particular, if you replace x with the terminal object 1, you’ll see that the two
objects have the same elements. For every element x : 1 → a there is a corresponding
element y : 1 → b, namely y = f ◦ x, and vice versa. There is a bijection between the
elements of isomorphic objects.
Such indistinguishable objects are called isomorphic because they have “the same
shape.” You’ve seen one, you’ve seen them all.
We write this isomorphism as:
3.2. NATURALITY 13
a∼
=b
When dealing with objects, we use isomorphism in place of equality.
In programming, two isomorphic types have the same external behavior. One type
can be implemented in terms of the other and vice versa. One can be replaced by the
other without changing the behavior of the system (except, possibly, the performance).
In classical logic, if B follows from A and A follows from B then A and B are
logically equivalent. We often say that B is true “if and only if” A is true. However,
unlike previous parallels between logic and type theory, this one is not as straightforward
if you consider proofs to be relevant. In fact, it led to the development of a new branch
of fundamental mathematics, homotopy type theory, or HoTT for short.
Exercise 3.1.1. Make an argument that there is a bijection between arrows that are
outgoing from two isomorphic objects. Draw the corresponding diagrams.
Exercise 3.1.3. If there are two terminal objects, show that they are isomorphic
Exercise 3.1.4. Show that the isomorphism from the previous exercise is unique.
3.2 Naturality
We’ve seen that, when two objects are isomorphic, we can switch focus from one to
another using post-composition: either (f ◦ −) or (f −1 ◦ −).
Conversely, to switch between different observers, we use pre-composition.
Indeed, an arrow h probing a from x is related to the arrow h ◦ g probing the same
object from y.
g
x y
h◦g
h
f
a b
f −1
it doesn’t matter if you first pre-compose with g (switch perspective) and then post-
compose with f (switch focus), or first post-compose with f and then pre-compose with
g. Symbolically, we write it as:
(− ◦ g) ◦ (f ◦ −) = (f ◦ −) ◦ (− ◦ g)
g h f
y x a b
Here, the naturality condition is just an identity, but we’ll soon see it generalized
to less trivial circumstances.
Arrows are used to broadcast information about an isomorphism. Naturality tells
us that all objects get a consistent view of it, independent of the path.
We can also reverse the roles of observers and subjects. For instance, using an arrow
h : a → x, the object a can probe an arbitrary object x. If there is an arrow g : x → y,
it can switch focus to y. Switching the perspective to b is done by precomposition with
f −1 .
f −1
f
a b
g◦h
h
x g y
Again, we have the naturality condition, this time from the point of view of the iso-
morphic pair:
(− ◦ f −1 ) ◦ (g ◦ −) = (g ◦ −) ◦ (− ◦ f −1 )
Exercise 3.2.1. Show that both sides of the naturality condition for f −1 , when acting
on h, reduce to:
f −1 h g
b a x y
When two objects a and b are isomorphic, any isomorphism f induces a one-to-one
mapping (f ◦ −) between corresponding sets of arrows.
Suppose that we don’t know if the objects are isomorphic, but we know that there
is an invertible mapping, αx , between sets of arrows impinging on a and b from every
3.3. REASONING WITH ARROWS 15
object x.
x x
(f ◦ −) αx
a b a b
f
Does it mean that the two objects are isomorphic? Can we reconstruct the isomorphism
f from the family of mappings αx ?
Here’s the action of αx on a particular arrow h.
x
αx h
h
a b
This mapping, along with its inverse αx−1 , which takes arrows x → b to arrows x → a,
would play the role of (f ◦ −) and (f −1 ◦ −), if there was indeed an isomorphism f .
The family of maps α describes an “artificial” way of switching focus between our two
objects.
Here’s the same situation from the point of view of another observer y:
x y
h0
αy h0
a b
αy ◦ (− ◦ g)
or we can first let x switch focus to b using αx , and then switch perspective using (−◦g):
(− ◦ g) ◦ αx
In both cases we end up looking at b from y. We’ve done this exercise before, when we
had an isomorphism between a and b, and we’ve found out that the results were the
same. We called it the naturality condition.
16 CHAPTER 3. ISOMORPHISMS
αy (h ◦ g) = (αx h) ◦ g
This way, if we replace all α’s with (f ◦ −), things will work out.
And indeed they do! This is called the Yoneda trick. It’s a way to reconstruct f
from the α’s. Since αx is defined for any object x, it is also defined for the object a. By
definition, αa takes a morphism a → a to a morphism a → b. We know for sure that
there is at least one morphism a → a, namely the identity ida . The isomorphism f we
are seeking is given by:
f = αa (ida )
or, pictorially:
x=a
f =αa (ida )
ida
a b
If this is indeed an isomorphism then our αx should be equal to (f ◦ −) for any x. To
see that, let’s rewrite the naturality condition replacing x with a. We get
αy (h ◦ g) = (αa h) ◦ g
Since both the source and the target of h is a, this equality must also be true for
h = ida
αy (ida ◦ g) = (αa (ida )) ◦ g
But ida ◦ g is equal to g and αa (ida ) is our f , so we get:
αy g = f ◦ g = (f ◦ −)g
the perspective of a. When we knew that the two objects were isomorphic, we were
able to switch perspective to b using (− ◦ f −1 ). This time we have at our disposal a
transformation βx instead.
a b
h
βx h
x
Similarly, if we want to observe another object, y, we will use βy to switch perspectives
between a and b, and so on.
If the two objects x and y are connected by an arrow g : x → y then we also have
an option of switching focus using (g ◦ −). If we want to both switch perspective and
switch focus, there are two ways of doing it. Naturality demands that the results be
equal:
(g ◦ −) ◦ βx = βy ◦ (g ◦ −)
Indeed, if we replace β with (− ◦ f −1 ), we recover the naturality condition for an
isomorphism.
Exercise 3.3.1. Use the trick with the identity morphism to recover f −1 from the
family of mappings β.
Exercise 3.3.2. Using f −1 from the previous exercise, evaluate βy g for an arbitrary
object y and an arbitrary arrow g : a → y.
As Lao Tzu would say: To show an isomorphism, it is often easier to define a natural
transformation between ten thousand arrows than it is to find a pair of arrows between
two objects.
Chapter 4
Sum Types
4.1 Bool
We know how to compose arrows. But how do we compose objects?
We have defined 0 (the initial object) and 1 (the terminal object). What is 2 if not
1 plus 1?
A 2 is an object with two elements: two arrows coming from 1. Let’s call one arrow
True and the other False. Don’t confuse those names with the logical interpretations
of the initial and the terminal objects. These two are arrows.
True False
2
This simple idea can be immediately expressed in Haskell as the definition of a type,
traditionally called Bool, after its inventor George Boole (1815-1864).
data Bool where
True :: () -> Bool
False :: () -> Bool
It corresponds to the same diagram (only with some Haskell renamings):
()
True False
Bool
As we’ve seen before, there is a shortcut notation for elements, so here’s a more
compact version:
data Bool where
True :: Bool
False :: Bool
19
20 CHAPTER 4. SUM TYPES
()
True False
x y
Bool
h
A
In other words, every function Bool->A produces a pair of elements of A.
Given a concrete type A:
h :: Bool -> A
we have:
x = h True
y = h False
where
x :: A
y :: A
Notice the use of the shorthand notation for the application of a function to an element:
h True -- meaning: h . True
We are now ready to complete our definition of Bool by adding the condition that
any function from Bool to A not only produces but is equivalent to a pair of of elements
of A. In other words, a pair of elements uniquely determines a function from Bool.
What this means is that we can interpret the diagram above in two ways: Given h,
we can easily get x and y. But the converse is also true: a pair of elements x and y
uniquely defines h.
We have a “buddy system,” or a bijection, at work here. This time it’s a one-to-one
mapping between a pair of elements (x, y) and an arrow h.
In Haskell, this definition of h is encapsulated in the if, then, else construct. Given
4.1. BOOL 21
x :: A
y :: A
we define the mapping out
h :: Bool -> A
h b = if b then x else y
Here, b is a term of type Bool.
In general, a data type is created using introduction rules and deconstructed using
elimination rules. The Bool data type has two introduction rules, one using True and
another using False. The if, then, else construct defines the elimination rule.
The fact that, given h, we can reconstruct the two terms used to define it, is called
the computation rule. It tells us how to compute the result of h. If we call h with True,
the result is x; if we call it with False, the result is y.
We should never lose sight of the purpose of programming: to decompose complex
problems into a series of simpler ones. The definition of Bool illustrates this idea.
Whenever we have to construct a mapping out of Bool, we decompose it into two
smaller tasks of constructing a pair of elements of the target type. We traded one
larger problem for two simpler ones.
Examples
Let’s do a few examples. We haven’t defined many types yet, so we’ll be limited to
mappings out from Bool to either Void, (), or Bool. Such edge cases, however, may
offer new insights into well known results.
We have decided that there can be no functions (other than identity) with Void as
a target, so we don’t expect any functions from Bool to Void. And indeed, we have
zero pairs of elements of Void.
What about functions from Bool to ()? Since () is terminal, there can be only one
function from Bool to it. And, indeed, this function corresponds to the single possible
pair of functions from () to ()—both being identities. So far so good.
The interesting case is functions from Bool to Bool. Let’s plug Bool in place of A
in our diagram:
()
True False
x y
Bool
h
Bool
How many pairs (x, y) of functions from () to Bool do we have at our disposal? There
are only two such functions, True and False, so we can form four pairs. These are
(T rue, T rue), (F alse, F alse), (T rue, F alse), and (F alse, T rue). Therefore there can
only be four functions from Bool to Bool.
We can write them in Haskell using the if, then, else construct. For instance, the
last one, which we’ll call not is defined as:
22 CHAPTER 4. SUM TYPES
Exercise 4.1.1. Write the implementations of the three other functions Bool->Bool.
4.2 Enumerations
What comes after 0, 1, and 2? An object with three data constructors. For instance:
data RGB where
Red :: RGB
Green :: RGB
Blue :: RGB
If you’re tired of redundant syntax, there is a shorthand for this type of definition:
data RGB = Red | Green | Blue
This introduction rule allows us to construct terms of the type RGB, for instance:
c :: RGB
c = Blue
To define mappings out of RGB, we need a more general elimination pattern. Just like
a function from Bool was determined by two elements, a function from RGB to A is
determined by a triple of elements of A: x, y, and z. We can write such function using
pattern matching:
h :: RGB -> A
h Red = x
h Green = y
h Blue = z
This is just one function whose definition is split into three cases.
It’s possible to use the same syntax for Bool as well, in place of if, then, else:
h :: Bool -> A
h True = x
h False = y
In fact, there is a third way of writing the same thing using the case pattern:
h c = case c of
Red -> x
Green -> y
Blue -> z
or even
h :: Bool -> A
h b = case b of
4.2. ENUMERATIONS 23
True -> x
False -> y
You can use any of these at your convenience when programming.
These patterns will also work for types with four, five, and more data constructors.
For instance, a decimal digit is one of:
data Digit = Zero | One | Two | Three | ... | Nine
There is a giant enumeration of Unicode characters called Char. Their constructors
are given special names: you write the character itself between two apostrophes, e.g.,
c :: Char
c = 'a'
As Lao Tzu would say, a pattern of ten thousand things would take many years to
complete, therefore people came up with the wildcard pattern, the underscore, which
matches everything.
Because the patterns are matched in order, you should make the wildcard pattern
the last:
yesno :: Char -> Bool
yesno c = case c of
'y' -> True
'Y' -> True
_ -> False
But why should we stop at that? The type Int could be thought of as an enu-
meration of integers in the range between −229 and 229 (or more, depending on the
implementation). Of course, exhaustive pattern matching on such ranges is out of the
question, but the principle holds.
In practice, the types Char for Unicode characters, Int for fixed-precision integers,
Double for double-precision floating point numbers, and several others, are built into
the language.
These are not infinite types. Their elements can be enumerated, even if it would
take ten thousand years. The type Integer is infinite, though.
The function absurd works with any type as its target (it’s a polymorphic function),
so it is parameterized by a type variable. Unlike concrete types, type variables must
start with a lowercase letter. Here, a is such a type variable:
absurd :: Void -> a
absurd v = undefined
We use undefined to placate the compiler. In this case, we are absolutely sure that the
function absurd can never be called, because it’s impossible to construct an argument
of type Void.
You may use undefined when you’re only interested in compiling, as opposed to
running, your code. For instance, you may need to plug a function f to check if your
definitions work together:
f :: a -> x
f = undefined
If you want to experiment with defining your own versions of standard types, like
Either, you have to tell the compiler to hide the originals that are defined in the
standard library called the Prelude. Put this line at the top of the file, after the
language pragmas:
import Prelude hiding (Either, Left, Right)
a b
Left Right
a+b
In Haskell, the type a + b is called Either a b. By analogy with Bool, we can define
it as
data Either a b where
Left :: a -> Either a b
Right :: b -> Either a b
(Note the use of lower-case letters for type variables.)
Similarly, the mapping out from a+b to some type c is determined by this commuting
diagram:
a b
Left Right
a+b
f g
h
c
4.3. SUM TYPES 25
Given a function h, we get a pair of functions f and g just by composing it with Left
and Right. Conversely, such a pair of functions uniquely determines h. This is the
elimination rule.
When we want to translate this diagram to Haskell, we need to select elements of
the two types. We can do it by defining the arrows a and b from the terminal object.
1
a b
a b
Left Right
a+b
f g
h
c
h ◦ Left ◦ a = f ◦ a
h ◦ Right ◦ b = g ◦ b
Haskell syntax repeats these equations almost literally, resulting in this pattern-
matching syntax for the definition of h:
h :: Either a b -> c
h (Left a) = f a
h (Right b) = g b
(Again, notice the use of lower-case letters for type variables and the same letters for
terms of that type. Unlike humans, the compilers don’t get confused by this.)
You can also read these equations right to left, and you will see the computation
rules for sum types. The two functions that were used to define h can be recovered by
applying h to terms constructed using Left and Right.
You can also use the case syntax to define h:
h e = case e of
Left a -> f a
Right b -> g b
So what is the essence of a data type? It is but a recipe for manipulating arrows.
Maybe
A very useful data type, Maybe is defined as a sum 1 + a, for any a. This is its definition
in Haskell:
data Maybe a where
Nothing :: () -> Maybe a
Just :: a -> Maybe a
The data constructor Nothing is an arrow from the unit type, and Just constructs
Maybe a from a. Maybe a is isomorphic to Either () a. It can also be defined using
the shorthand notation
26 CHAPTER 4. SUM TYPES
Logic
In logic, the proposition A + B is called the alternative, or logical or. You can prove it
by providing the proof of A or the proof of B. Either one will suffice.
If you want to prove that C follows from A + B, you have to be prepared for two
eventualities: either somebody proved A + B by proving A (and B could be false) or
by proving B (and A could be false). In the first case, you have to show that C follows
from A. In the second case you need a proof that C follows from B. These are exactly
the arrows in the elimination rule for A + B.
1 0
Left Right
1
1+0 x
x
h
¡
a
a
We want to establish a one-to-one mapping between arrows originating in 1 + 0 and the
ones originating in 1. Since h is determined by the pair (x, ¡), we can simply map it to
the arrow x originating in 1. Since there is only one ¡, the mapping is a bijection.
So our βa maps any pair (x, ¡) to x. Conversely, βa−1 maps x to the pair (x, ¡). But
is it a natural transformation?
To answer that, we need to consider what happens when we change focus from a to
some b that is connected to it through an arrow g : a → b. We have two options now:
4.4. COCARTESIAN CATEGORIES 27
• Make h switch focus by post-composing both x and ¡ with g. We get a new pair
(y = g ◦ x, ¡). Follow it by βb .
a+0 f
f
h
¡
x
x
Exercise 4.4.1. Implement, in Haskell, the two functions that form the isomorphism
between Either a Void and a.
Commutativity
There is a nice left-right symmetry in the diagrams that define the sum type, which
suggests that it satisfies the commutativity rule, a + b ∼= b + a.
Let’s consider mappings out of both sides. You can easily see that, for every h that
is determined by a pair (f, g) on the left, there is a corresponding h0 given by a pair
(g, f ) on the right. That establishes the bijection of arrows.
28 CHAPTER 4. SUM TYPES
a b b a
Left Right Left Right
a+b b+a
f g g f
h h0
x x
Exercise 4.4.2. Show that the bijection defined above is natural. Hint: Both f and g
change focus by post-composition with k : x → y.
Exercise 4.4.3. Implement, in Haskell, the function that witnesses the isomorphism
between Either a b and Either b a. Notice that this function is its own inverse.
Associativity
Just like in arithmetic, the sum that we have defined is associative:
(a + b) + c ∼
= a + (b + c)
It’s easy to write the mapping out for the left hand side:
h :: Either (Either a b) c -> x
h (Left (Left a)) = f1 a
h (Left (Right b)) = f2 b
h (Right c) = f3 c
Notice the use of nested patterns like (Left (Left a)), etc. The mapping is fully
defined by a triple of functions. The same functions can be used to define the mapping
out of the right hand side:
h' :: Either a (Either b c) -> x
h' (Left a) = f1 a
h' (Right (Left b)) = f2 b
h' (Right (Right c)) = f3 c
This establishes a one-to-one mapping between triples of functions that define the two
mappings out. This mapping is natural because all changes of focus are done using
post-composition. Therefore the two sides are isomorphic.
This code can also be displayed in diagrammatical form. Here’s the diagram for the
left hand side of the isomorphism:
a b c
L R
a+b R
f2
L
(a + b) + c f3
f1
h
x
4.4. COCARTESIAN CATEGORIES 29
Functoriality
Since the sum is defined by the mapping out property, it was easy to see what happens
when we change focus: it changes “naturally” with the foci of the arrows that define
the product. But what happens when we move the sources of those arrows?
Suppose that we have arrows that map a and b to some a0 and b0 :
f : a → a0
g : b → b0
The composition of these arrows with the constructors Left and Right, respectively, can
be used to define the mapping of the sums:
a b
f Left Right g
a0 a+b b0
h
Left Right
a0 + b0
The pair of arrows, (Left ◦ f, Right ◦ g) uniquely defines the arrow h : a + b → a0 + b0 .
This property of the sum is called functoriality. You can imagine it as allowing you
to transform the two objects inside the sum.
Exercise 4.4.4. Show that functoriality preserves composition. Hint: take two com-
posable arrows, g : b → b0 and g 0 : b0 → b00 and show that applying g 0 ◦ g gives the same
result as first applying g to transform a + b to a + b0 and then applying g 0 to transform
a + b0 to a + b00 .
Exercise 4.4.5. Show that functoriality preserves identity. Hint: use idb and show
that it is mapped to ida+b .
Product Types
We can use sum types to enumerate possible values of a given type, but the encoding
can be wasteful. We needed ten constructors just to encode numbers between zero and
nine.
data Digit = Zero | One | Two | Three | ... | Nine
But if we combine two digits into a single data structure, a two-digit decimal number,
we’ll able to encode a hundred numbers. Or, as Lao Tzu would say, with just four digits
you can encode ten thousand numbers.
A data type that combines two types in this manner is called a product, or a
cartesian product. Its defining quality is the elimination rule: there are two arrows
coming from a × b; one called “fst” goes to a, and another called “snd” goes to b. They
are called projections. They let us retrive a and b from the product a × b.
a×b
fst snd
a b
Suppose that somebody gave you an element of a product, that is an arrow h from
the terminal object 1 to a × b. You can easily retrieve a pair of elements, just by using
composition: an element of a given by
a = fst ◦ h
and an element of b given by
b = snd ◦ h
1
h
a b
a×b
fst snd
a b
In fact, given an arrow from an arbitrary object c to a × b, we can define, by
composition, a pair of arrows f : c → a and g : c → b
31
32 CHAPTER 5. PRODUCT TYPES
c
h g
f
a×b
fst snd
a b
As we did before with the sum type, we can turn this idea around, and use this
diagram to define the product type: A pair of functions f and g should be in one-to-
one correspondence with a mapping in from c to a × b. This is the introduction rule for
the product.
In particular, the mapping out of the terminal object is used in Haskell to define a
product type. Given two elements, a :: A and b :: B, we construct the product
(a, b) :: (A, B)
The built-in syntax for products is just that: a pair of parentheses and a comma in
between. It works both for defining the product of two types (A, B) and the data
constructor (a, b) that takes two elements and pairs them together.
We should never lose sight of the purpose of programming: to decompose complex
problems into a series of simpler ones. We see it again in the definition of the product.
Whenever we have to construct a mapping into the product, we decompose it into
two smaller tasks of constructing a pair of functions, each mapping into one of the
components of the product. This is as simple as saying that, in order to implement
a function that returns a pair of values, it’s enough to implement two functions, each
returning one of the elements of the pair.
Logic
In logic, a product type corresponds to logical conjunction. In order to prove A × B
(A and B), you need to provide the proofs of both A and B. These are the arrows
targeting A and B. The elimination rule says that if you have a proof of A × B, then
you automatically get the proof of A (through fst) and the proof of B (through snd).
Tuple Arithmetic
The identities satisfied by the product can be derived using the mapping-in property.
For instance, to show that a × b ∼= b × a consider the following two diagrams:
x x
h g g h0
f f
a×b b×a
of a program. Symmetry means that the information content of a swapped pair is the
same, but access to it needs to be modified.
Here’s the diagram that can be used to prove that the terminal object is the unit
of the product, 1 × a ∼
= a.
x
x
h
! f
1×a f
fst snd a
1 a
The unique arrow from x to 1 is called ! (pronounced, bang). Because of its uniqueness,
the mapping-in, h, is totally determined by f .
The invertible arrow that witnesses the isomorphism between 1 × a and a is called
the left unitor :
λ: 1 × a → a
Here are some other isomorphisms written in Haskell (without proofs of having the
inverse). This is associativity:
assoc :: ((a, b), c) -> (a, (b, c))
assoc ((a, b), c) = (a, (b, c))
And this is the left unit
lunit :: ((), a) -> a
lunit (_, a) = a
These two functions correspond to the associator
α : (a × b) × c → a × (b × c)
Exercise 5.1.1. Show that the bijection in the proof of left unit is natural. Hint, change
focus using an arrow g : a → b.
h : b + a × b → (1 + a) × b
Exercise 5.1.3. Redo the previous exercise, this time treating h as a mapping out of
a sum.
Exercise 5.1.4. Implement a Haskell function maybeAB :: Either b (a, b) -> (Maybe a, b).
Is this function uniquely defined by its type signature or is there some leeway?
5.2. DUALITY 35
Functoriality
Suppose that we have arrows that map a and b to some a0 and b0 :
f : a → a0
g : b → b0
The composition of these arrows with the projections fst and snd, respectively, can be
used to define the mapping of the products:
a×b
fst h snd
a a0 × b0 b
f g
fst snd
a0 b0
The shorthand notation for this diagram is:
f ×g
a × b −−→ a0 × b0
This property of the product is called functoriality. You can imagine it as allowing
you to transform the two objects inside the product.
5.2 Duality
When a child sees an arrow, it knows which end points at the source, and which points
at the target
a→b
But maybe this is just a preconception. Would the Universe be very different if we
called b the source and a the target?
We would still be able to compose this arrow with this one
b→c
whose “target” b is the same as the same as the “source” of a → b, and the result would
still be an arrow
a→c
only now we would say that it goes from c to a.
In this dual Universe, the object that we call “initial” would be called “terminal,”
because it’s the “target” of unique arrows coming from all objects. Conversely, the
terminal object would be called initial.
Now consider this diagram that we used to define the sum object:
a b
Left Right
a+b
f g
h
c
36 CHAPTER 5. PRODUCT TYPES
In the new interpretation, the arrow h would go “from” an arbitrary object c “to” the
object we call a + b. This arrow is uniquely defined by a pair of arrows (f, g) whose
“source” is c. If we rename Left to fst and Right to snd, we will get the defining diagram
for a product.
A product is the sum with arrows reversed.
Conversely, a sum is the product with arrows reversed.
Every construction in category theory has its dual.
If the direction of arrows is just a matter of interpretation, then what makes sum
types so different from product types, in programming? The difference goes back to one
assumption we made at the start: There are no incoming arrows to the initial object
(other than the identity arrow). This is in contrast with the terminal object having lots
of outgoing arrows, arrows that we used to define (global) elements. In fact, we assume
that every object of interest has elements, and the ones that don’t are isomorphic to
Void.
We’ll see an even deeper difference when we talk about function types.
1×a∼
=a
∼
a×b=b×a
(a × b) × c ∼
= a × (b × c)
and is functorial.
A category in which an operation with these properties is defined is called symmetric
monoidal. We’ve seen a similar structure before when working with sums and the initial
object.
A category can have multiple monoidal structures at the same time. When you don’t
want to name your monoidal structure, you replace the plus sign or the product sign
with a tensor sign, and the neutral element with the letter I. The rules of a symmetric
monoidal category can then be written as:
I ⊗a∼
=a
∼
a⊗b=b⊗a
(a ⊗ b) ⊗ c ∼
= a ⊗ (b ⊗ c)
These isomorphisms are often written as families of invertible arrows called associ-
ators and unitors. If the monoidal category is not symmetric, there is a separate left
and right unitor.
α : (a ⊗ b) ⊗ c → a ⊗ (b ⊗ c)
λ: I ⊗ a → a
ρ: a ⊗ I → a
γ: a ⊗ b → b ⊗ a
5.3. MONOIDAL CATEGORY 37
You may think of a tensor product as the lowest common denominator of product
and sum. It still has an introduction rule, but it requires both objects a and b; and it
has no elimination rule—no projections. Some interesting examples of tensor products
are not even symmetric.
Monoids
Monoids are very simple structures equipped with a binary operation and a unit. Nat-
ural numbers with addition and zero form a monoid. So do natural numbers with
multiplication and one.
The intuition is that a monoid lets you combine two things to get one thing. There
is also one special thing, such that combining it with anything else gives back the same
thing. And the combining must be associative.
What’s not assumed is that the combining is symmetric, or that there is an inverse
element.
The rules that define a monoid are reminiscent of the rules of a category. The
difference is that, in a monoid, any two things are composable, whereas in a category
this is usually not the case: You can only compose two arrows if the target of one is
the source of another. Except, that is, when the category contains only one object, in
which case all arrows are composable.
A category with a single object is called a monoid. The combining operation is the
composition of arrows and the unit is the identity arrow.
This is a perfectly valid definition. In practice, however, we are often interested
in monoids that are embedded in larger categories. In particular, in programming, we
want to be able to define monoids inside the category of types and functions.
In a category, we are forced to define operations in bulk, rather than looking at
individual elements. We start with an object m. A binary operation is a function of
two arguments. Since elements of a product are pairs of elements, we can characterize
a binary operation as an arrow from a product m × m to m:
µ: m × m → m
The unit element can be defined as an arrow from the terminal object 1:
η: 1 → m
We want the result to be the same as the original element of m, but without men-
tioning elements. So we just use the left unitor λ to go from 1 × m to m without
“stirring things up.”
η×idm
1×m m×m
µ
λ
m
Here is the analogous law for the right unit:
idm ×η
m×m m×1
µ
ρ
m
To formulate the law of associativity, we have to start with a triple product and act
on it in bulk. Here, α is the associator that rearranges the product without “stirring
things up.”
α
(m × m) × m m × (m × m)
µ×id id×µ
m×m m×m
µ µ
m
Notice that we didn’t have to assume a lot about the categorical product that we
used with the objects m and 1. In particular we never had to use projections. This
suggests that the above definition will work equally well for a tensor product in a
monoidal category. It doesn’t even have to be symmetric. All we have to assume is
that there is a unit object, that the product is functorial, and that it satisfies the unit
and associativity laws up to isomorphism.
Thus if we replace × with ⊗ and 1 with I, we get a definition of a monoid in an
arbitrary monoidal category.
A monoid in a monoidal category is an object m equipped with two morphisms:
µ: m ⊗ m → m
η: I → m
satisfying the unit and associativity laws:
η⊗idm idm ⊗η
1⊗m m⊗m m⊗1
µ
λ ρ
m
α
(m ⊗ m) ⊗ m m ⊗ (m ⊗ m)
µ⊗idm idm ⊗µ
m⊗m m⊗m
µ µ
m
We used the functoriality of ⊗ in lifting pairs of arrows, as in η ⊗ idm , µ ⊗ idm , etc.
Chapter 6
Function Types
Elimination rule
The defining quality of a function is that it can be applied to an argument to produce
the result. We have defined function application in terms of composition:
1
y
x
a f
b
f : 1 → ba
x: 1 → a
it should produce an element
y: 1 → b
Keep in mind that, here, f denotes an element of ba . Previously, it was an arrow
from a to b.
39
40 CHAPTER 6. FUNCTION TYPES
εa,b : ba × a → b
This way y, the result of the application, is defined by this commuting diagram:
1
y
(f,x)
ba × a εa,b b
Introduction rule
To complete the definition of the function object, we also need the introduction rule.
First, suppose that there is a way of constructing a function object ba from some
other object c. It means that there is an arrow
h : c → ba
We know that we can eliminate the result of h using εa,b , but we have to first multiply
it by a. So let’s first multiply c by a and the use functoriality to map it to ba × a.
Functoriality lets us apply a pair of arrows to a product to get another product.
Here, the pair of arrows is (h, ida ) (we want to turn c into ba , but we’re not interested
in modifying a)
h×id
a
c × a −−−−→ ba × a
We can now follow this with function application to get to b
h×id εa,b
a
c × a −−−−→ ba × a −−→ b
f: c×a→b
c×a
f
h×ida
ba × a ε b
This commuting diagram tells us that, given an h, we can construct an f ; but we
can also demand the converse: Every mapping out of a product, f : c × a → b should
uniquely define a mapping into the exponential, h : c → ba .
We can use this property, this one-to-one correspondence between two sets of arrows,
to define the exponential object. This is the introduction rule for the function object.
We’ve seen that product was defined using its mapping-in property. Function ap-
plication, on the other hand, is defined as a mapping out of a product.
41
Currying
There are several ways of looking at this definition. One is to see it as an example of
currying.
So far we’ve been only considering functions of one argument. This is not a real
limitation, since we can always implement a function of two arguments as a (single-
argument) function from a product. The f in the definition of the function object is
such a function:
f :: (c, a) -> b
h on the other hand is a function that returns a function (object)
h :: c -> (a -> b)
Currying is the isomorphism between these two types.
This isomorphism can be represented in Haskell by a pair of (higher-order) functions.
Since, in Haskell, currying works for any types, these functions are written using type
variables—they are polymorphic:
curry :: ((c, a) -> b) -> (c -> (a -> b))
h = curry f
Of course, written this way, the types of curry and uncurry correspond to function
objects rather than arrows. This distinction is usually glossed over because there is
a one-to-one correspondence between the elements of the exponential and the arrows
that define them. This is easy to see when we replace the arbitrary object c with the
terminal object. We get:
1×a
f
h×ida
ba × a εa,b b
Γ×C
h×idC e
CC × C ε C
This new mapping h can be seen as a constructor of the function object. The resulting
function object represents all functions from C to C that have access to the environment
Γ; that is, to the triple of parameters (a, b, c).
Corresponding to our original expression ax2 + bx + c there is a particular arrow h
that we write as:
λx. ax2 + bx + c
6.1. SUM AND PRODUCT REVISITED 43
This is just notation for the result of the one-to-one mapping between arrows: Given
an arrow e that represents an expression in the context Γ, this mapping produces an
arrow that we call λx.e. In typed lambda calculus, we also specify the type of x:
h = λ (x : a). e
Γ×a
(λx.e)×ida e
ba × a ε b
The environment Γ that provides free parameters for the expression e is a product
of multiple objects representing the types of the parameters (in our example, it was
R × R × R). An empty environment is represented by the terminal object, the unit of
the product. In the latter case, h simply picks an element from the function object.
It’s important to keep in mind that, in general, a function object represents functions
that depend on external parameters. Such functions are called closures. Closures are
functions that capture values from their environment.
Here’s an example of a function returning a closure (for simplicity, we use Double
for all types)
quadratic :: Double -> Double -> Double -> (Double -> Double)
quadratic a b c = \x -> a * x^2 + b * x + c
The same function can be written as a function of four variables:
quadratic :: Double -> Double -> Double -> Double -> Double
quadratic a b c x = a * x^2 + b * x + c
Modus ponens
In logic, the function object corresponds to an implication. An arrow from the terminal
object to the function object is the proof of an implication. Function application ε
corresponds to what logicians call modus ponens: if you have a proof of the implication
A ⇒ B and a proof of A then this constitutes the proof of B.
Sum types
a b
Left Right
a+b
f g
h
c
We said that the pair of arrows (f, g) uniquely determines the mapping h out of the
sum. We can write it concisely using a higher-order function:
h = mapOut (f, g)
where:
mapOut :: (a -> c, b -> c) -> (Either a b -> c)
mapOut (f, g) = \aorb -> case aorb of
Left a -> f a
Right b -> g b
The other direction of the bijection, from h to the pair (f, g), also follows the arrows
of the diagram.
unEither :: (Either a b -> c) -> (a -> c, b -> c)
unEither h = (h . Left, h . Right)
6.1. SUM AND PRODUCT REVISITED 45
Product types
Product types are dually defined by their mapping-in property.
c
h g
f
a×b
fst snd
a b
Functoriality revisited
Both sum and product are functorial, which means that we can apply functions to their
contents. We are ready to translate those diagrams into code.
This is the functoriality of the sum type:
a b
f Left Right g
a0 a+b b0
h
Left Right
a0 + b0
a×b
fst h snd
a a0 × b0 b
f g
fst snd
a0 b0
ba × a0
k×ida g
0
b0a × a0 ε b0
The question is: can we find an arrow g to complete this diagram?
g : ba × a0 → b0
This may sound complicated, but the bottom line is that we require two arrows
between the primed and non-primed objects. The twist is that the first arrow goes
0
from a0 to a, which feels backward from the usual. In order to map ba to b0a we are
asking for a pair of arrows
f : a0 → a
g : b → b0
This is somewhat easier to explain in Haskell. Our goal is to implement a function
a' -> b', given a function h :: a -> b.
This new function takes an argument of the type a' so, before we can pass it to h,
we need to convert a' to a. That’s why we need a function f :: a' -> a.
Since h produces a b, and we want to return a b', we need another function
g :: b -> b'. All this fits nicely into one higher-order function:
dimap :: (a' -> a) -> (b -> b') -> (a -> b) -> (a' -> b')
dimap f g h = g . h . f
Similar to bimap being an interface to the typeclass Bifunctor, dimap is a member of
the typeclass Profunctor.
Distributivity
Multiplication of numbers distributes over addition. Should we expect the same in a
bicartesian closed category?
b×a+c×a∼
= (b + c) × a
The left to right mapping is easy to construct, since it’s simultaneously a map-
ping out of a sum and a mapping into a product. We can construct it by gradually
decomposing it into simpler mappings. In Haskell, this means implementing a function
dist :: Either (b, a) (c, a) -> (Either b c, a)
A mapping out of the sum on the left is given by a pair of arrows:
f : b × a → (b + c) × a
g : c × a → (b + c) × a
We write it in Haskell as:
48 CHAPTER 6. FUNCTION TYPES
dist = either f g
where
f :: (b, a) -> (Either b c, a)
g :: (c, a) -> (Either b c, a)
The where clause is used to introduce the definitions of sub-functions.
Now we need to implement f and g. They are mappings into the product, so each
of them is equivalent to a pair of arrows. For instance, the first one is given by the pair:
f 0 : b × a → (b + c)
f 00 : b × a → a
In Haskell:
f = f' &&& f''
f' :: (b, a) -> Either b c
f'' :: (b, a) -> a
The first arrow can be implemented by projecting the first component b and then using
Left to construct the sum. The second is just the projection snd.
f 0 = Left ◦ fst
f 00 = snd
Recursion
When you step between two mirrors, you see your reflection, the reflection of your
reflection, the reflection of that reflection, and so on. Each reflection is defined in terms
of the previous reflection, but together they produce infinity.
Recursion is a decomposition pattern that splits a single task into many steps, the
number of which is potentially unbounded.
Recursion is based on suspension of disbelief. You are faced with a task that may
take arbitrarily many steps. You tentatively assume that you know how to solve it.
Then you ask yourself the question: ”How would I make the last step if I had the
solution to everything but the last step?”
Z: 1 → N
But we have to be able to define infinitely many arrows to account for the fact that,
for every natural number, there is another number that is one larger than it.
We can formalize this statement by saying: Suppose that we know how to create a
natural number n : 1 → N . How do we make the next step, the step that will point us
to the next number—its successor?
This next step doesn’t have to be any more complex than just post-composing n
with an arrow that loops back from N to N . This arrow should not be the identity,
because we want the successor of a number to be different from that number. But a
single such arrow, which we’ll call S for “successor” will suffice.
The element corresponding to the successor of n is given by the composition:
n S
1−
→N −
→N
(We sometimes draw the same object multiple times in a single diagram, if we want to
straighten the looping arrows.)
51
52 CHAPTER 7. RECURSION
One
Z S
1 N N
Z S S
1 N N N
and so on.
Introduction Rules
The two arrows, Z and S, serve as the introduction rules for the natural number object
N . The twist is that one of them is recursive: S uses N as its source as well as its
target.
Z
1 N
The two introduction rules translate directly to Haskell
data Nat where
Z :: Nat
S :: Nat -> Nat
They can be used to define arbitrary natural numbers; for instance:
zero, one, two :: Nat
zero = Z
one = S zero
two = S one
This definition of natural number type is not very useful in practice. However, it’s
often used in defining type-level naturals, where each number is its own type.
You may encounter this construction under the name of Peano arithmetic.
Elimination Rules
The fact that the introduction rules are recursive complicates the matters slightly when
it comes to defining elimination rules. We will follow the pattern from previous chapters
of first assuming that we are given a mapping out of N :
h: N → a
The introduction rules for N look similar to those for the sum, so we would expect
that h could be split into two arrows. And, indeed, we can easily get the first one by
composing h ◦ Z. This is an arrow that picks an element of a. We call it init:
init : 1 → a
Z S S
1 N N N ...
h h h
init
a a a
a1 = h ◦ S ◦ Z
followed by
a2 = h ◦ S ◦ S ◦ Z
and so on.
We have thus replaced one arrow h with infinitely many arrows an . Granted, the
new arrows are simpler, since they represent elements of a, but there are infinitely many
of them.
The problem is that, no matter how you look at it, an arbitrary mapping out of N
contains infinite amount of information.
We have to drastically simplify the problem. Since we used a single arrow S to
generate all natural numbers, we can try to use a single arrow a → a to generate all
the elements an . We’ll call this arrow step:
Z S
1 N N
h h
init
step
a a
The mappings out of N that are generated by such pairs, init and step, are called
recursive. Not all mappings out of N are recursive. In fact very few are; but recursive
mappings are enough to define the object of natural numbers.
We use the above diagram as the elimination rule. Every recursive mapping out of
N is in one-to-one correspondence with a pair init and step.
This means that the evaluation rule (extracting (init, step) for a given h) cannot be
formulated for an arbitrary arrow h : N → a, only for recursive arrows that have been
defined using a pair (init, step).
The arrow init can be always recovered by composing h ◦ Z. The arrow step is a
solution to the equation:
step ◦ h = h ◦ S
If h was defined using some init and step, then this equation obviously has a solution.
The important part is that we demand that this solution be unique.
54 CHAPTER 7. RECURSION
Intuitively, the pair init and step generate the sequence of elements a0 , a1 , a2 , ... If
two arrows h and h0 are given by the same pair (init, step), it means that the sequences
they generate are the same.
So if h were different from h0 , it would mean that N contains more than just the
sequence of elements Z, SZ, S(SZ), ... For instance, if we added −1 to N (that is, made
Z somebody’s successor), we could have h and h0 differ at −1 and yet be generated by
the same init and step. Uniqueness means there are no natural number before, after,
or in between the numbers generated by Z and S.
The elimination rule we’ve discussed here corresponds to primitive recursion. We’ll
see a more advanced version of this rule, corresponding to the induction principle, in
the chapter on dependent types.
In Programming
The elimination rule can be implemented as a recursive function in Haskell:
rec :: a -> (a -> a) -> (Nat -> a)
rec init step = \n ->
case n of
Z -> init
(S m) -> step (rec init step m)
This single function, which is called a recursor, is enough to implement all recursive
functions of natural numbers. For instance, this is how we could implement addition:
plus :: Nat -> Nat -> Nat
plus n = rec init step
where
init = n
step = S
This function takes n as an argument and produces a function (a closure) that takes
another number and adds n to it.
In practice, programmers prefer to implement recursion directly—an approach that
is equivalent to inlining the recursor rec. The following implementation is arguably
easier to understand:
plus n m = case m of
Z -> n
(S k) -> S (plus k n)
It can be read as: If m is zero then the result is n. Otherwise, if m is a successor of some
k, then the result is the successor of k + n. This is exactly the same as saying that
init = n and step = S.
In imperative languages recursion is often replaced by iteration. Conceptually, iter-
ation seems to be easier to understand, as it corresponds to sequential decomposition.
The steps in the sequence usually follow some natural order. This is in contrast with
recursive decomposition, where we assume that we have done all the work up to the
n’th step, and we combine that result with the next consecutive step.
On the other hand, recursion is more natural when processing recursively defined
data structures, such as lists or trees.
7.2. LISTS 55
The two approaches are equivalent, and compilers often convert recursive functions
to loops in what is called tail recursion optimization.
7.2 Lists
A list of things is either empty or a thing followed by a list of things.
This recursive definition translates into two introduction rules for the type La , the
list of a:
Nil : 1 → La
Cons : a × La → La
The N il element describes an empty list, and Cons constructs a list from a head and
a tail.
The following diagram depicts the relationship between projections and list con-
structors. The projections extract the head and the tail of the list that was constructed
using Cons.
a × La Cons
1
f st Nil
a snd La
This description can be immediately translated to Haskell:
data List a where
Nil :: List a
Cons :: (a, List a) -> List a
Elimination Rule
Given a mapping out, h : La → c, from a list of a to some arbitrary type c, this is how
we can plug it into the definition of the list:
N il Cons
1 La a × La
h ida ×h
init
c step
a×c
We used the functoriality of the product to apply the pair (ida , h) to the product a×La .
Similar to the natural number object, we can try to define two arrows, init = h◦N il
and step. The arrow step is a solution to:
However, given init and step, we can define an h. Such a function is called a fold,
or a list catamorphism.
This is the list recursor in Haskell:
recList :: c -> ((a, c) -> c) -> (List a -> c)
recList init step = \as ->
case as of
Nil -> init
Cons (a, as) -> step (a, recList init step as)
Given init and step, it produces a mapping out of a list.
A list is such a basic data type that Haskell has a built-in syntax for it. The type
List a is written as [a]. The Nil constructor is an empty pair of square brackets, [],
and the Cons constructor is an infix colon :.
We can pattern match on these constructors. A generic mapping out of a list has
the form:
h :: [a] -> c
h [] = -- empty-list case
h (a: as) = -- case for the head and the tail of a non-empty list
Corresponding to the recursor, here’s the type signature of the function foldr (fold
right), which you can find in the standard library:
foldr :: (a -> c -> c) -> c -> [a] -> c
Here’s a possible implementation:
foldr step init = \as ->
case as of
[] -> init
a : as -> step a (foldr step init as)
As an example, we can use foldr to calculate the sum of the elements of a list of
natural numbers:
sum :: [Nat] -> Nat
sum = foldr plus Z
Exercise 7.2.1. Consider what happens when you replace a in the definition of a list
with the terminal object. Hint: What is base-one encoding of natural numbers?
Exercise 7.2.2. How many mappings h : La → 1 + a are there? Can we get all of them
using a list recursor? How about Haskell functions of the signature:
h :: [a] -> Maybe a
Exercise 7.2.3. Implement a function that extracts the third element from a list, if the
list is long enough. Hint: Use Maybe a for the result type.
7.3 Functoriality
Functoriality means, roughly, the ability to transform the “contents” of a data structure.
The contents of a list La is of the type a. Given an arrow f : a → b, we need to define
a mapping of lists h : La → Lb .
7.3. FUNCTORIALITY 57
Lists are defined by the mapping out property, so let’s replace the target c of the
elimination rule by Lb . We get:
N ila Consa
1 La a × La
h ida ×h
init
Lb step
a × Lb
Since we are dealing with two different lists here, we have to distinguish between their
constructors. For instance, we have:
Nila : 1 → La
Nilb : 1 → Lb
and similarly for Cons.
The only candidate for init is N ilb , which is to say that h acting on an empty list
of a’s produces an empty list of b’s:
h ◦ N ila = N ilb
step : a × Lb → Lb
We can take:
step = Consb ◦ (f × idLb )
This corresponds to the Haskell function:
mapList :: (a -> b) -> List a -> List b
mapList f = recList init step
where
init = Nil
step (a, bs) = Cons (f a, bs)
or, using the built-in list syntax and inlining the recursor,
map :: (a -> b) -> [a] -> [b]
map f [] = []
map f (a : as) = f a : map f as
You might wonder what prevents us from choosing step = snd , resulting in:
badMap :: (a -> b) -> [a] -> [b]
badMap f [] = []
badMap f (a : as) = badMap f as
We’ll see, in the next chapter, why this is a bad choice. (Hint: What happens when we
apply badMap to id?)
Chapter 8
Functors
8.1 Categories
So far we’ve only seen one category—that of types and functions. So let’s quickly gather
the essential info about a category.
A category is a collection of objects and arrows that go between them. Every pair
of composable arrows can be composed. The composition is associative, and there is
an identity arrow looping back on every object.
The fact that types and functions form a category can be expressed in Haskell by
defining composition as:
(.) :: (b -> c) -> (a -> b) -> (a -> c)
g . f = \x -> g (f x)
The composition of two functions g after f is a new function that first applies f to its
argument and then applies g to the result.
The identity is a polymorphic “do nothing” function:
id :: a -> a
id x = x
You can easily convince yourself that such composition is associative, and composing
with id does nothing to a function.
Based on the definition of a category, we can come up with all kinds of weird
categories. For instance, there is a category that has no objects and no arrows. It
satisfies all the condition of a category vacuously. There’s another that contains a
single object and a single arrow (can you guess what arrow it is?). There’s one with
two unconnected objects, and one where the two objects are connected by a single
arrow (plus two identity arrows), and so on. These are example of what I call stick-
figure categories.
Category of sets
We can also strip a category of all arrows (except for the identity arrows). Such a
bare-object category is called a discrete category or a set1 . Since we associate arrows
with structure, a set is a category with no structure.
1
Ignoring “size” issues.
59
60 CHAPTER 8. FUNCTORS
Sets form their own category called Set2 . The objects in that category are sets,
and the arrows are functions between sets. Such functions are defined as special kind
of relations, which themselves are defined as sets of pairs.
To lowest approximation, we can model programming in the category of sets. We
often think of types as sets of values, and functions as set-theoretical functions. There’s
nothing wrong with that. In fact all of categorical construction we’ve described so
far have their set-theoretical roots. The categorical product is a generalization of the
cartesian product of sets, the sum is the disjoint union, and so on.
What category theory offers is more precision: the fine distinction between the
structure that is absolutely necessary, and the superfluous details.
A set-theoretical function, for instance, doesn’t fit the definition of a function we
work with as programmers. Functions must have underlying algorithms because they
have to be computable by some physical systems, be it computers or a human brains.
8.2 Functors
We’ve seen examples of functoriality when discussing algebraic data types. The idea is
that such a data type “remembers” the way it was created, and we can manipulate this
memory by applying an arrow to its “contents.”
In some cases this intuition is very convincing: we think of a product type as a pair
that “contains” its ingredients. After all, we can retrieve them using projections.
This is less obvious in the case of function objects. You can visualize a function
object as secretly storing all possible results and using the function argument to index
2
Again, ignoring “size” issues, in particular the non-existence of the set of all sets.
8.2. FUNCTORS 61
into them. A function from Bool is obviously equivalent to a pair of values, one for
True and one for False. It’s a known programming trick to implement some functions
as lookup tables. It’s called memoization.
Even though it’s not practical to memoize functions that take, say, natural numbers
as arguments; we can still conceptualize them as (infinite, or even uncountable) lookup
tables.
If you can think of a data type as a container of values, it makes sense to apply a
function to transform all these values, and create a transformed container. When this
is possible, we say that the data type is functorial.
Again, function types require some more suspension of disbelief. You visualize a
function object as a lookup table, keyed by some type. If you want to use another,
ralated type as your key, you need a function that translates the new key to the original
key. This is why functoriality of the function object has one of the arrows reversed:
dimap :: (a' -> a) -> (b -> b') -> (a -> b) -> (a' -> b')
dimap f g h = g . h . f
You are applying the transformation to a function h :: a -> b that has a “receptor”
that responds to values of type a, and you want to use it to process input of type a'.
This is only possible if you have a converter from a' to a, namely f :: a' -> a.
The idea of a data type “containing” values of another type can be also expressed by
saying that one data type is paremeterized by another. For instance, the type List a
is parameterized by the type a.
In other words, List maps the type a to the type List a. List by itself, without
the argument, is called a type constructor.
Ff : Fa → Fb
a Fa
f Ff
b Fb
We use the same letter, here F , to name both, the mapping of objects and the mapping
of arrows.
62 CHAPTER 8. FUNCTORS
If categories distill the essence of structure, then functors are mappings that preserve
this structure. Objects that are related in the source category are related in the target
category.
The structure of a category is defined by arrows and their composition. Therefore
a functor must preserve composition. What is composed in one category:
h=g◦f
F h = F (g ◦ f ) = F g ◦ F f
We can either compose two arrows in C and map the composite to D, or we can map
individual arrows and then compose them in D. We demand that the result be the
same.
a Fa
f Ff
g Fg
c Fc
Finally, a functor must preserve identity arrows:
F ida = idF a
idF a
F ida
ida
a Fa
These conditions taken together define what it means for a functor to preserve the
structure of a category.
It’s also important to realize what conditions are not part of the definition. For
instance, a functor is allowed to map multiple objects into the same object. It can also
map multiple arrows into the same arrow, as long as the endpoints match.
In the extreme, any category can be mapped to a singleton category with one object
and one arrow.
Also, not all object or arrows in the target category must be covered by a functor.
In the extreme, we can have a functor from the singleton category to any (non-empty)
category. Such a functor picks a single object together with its identity arrow.
A constant functor ∆c is an example of a functor that maps all objects from the
source category to a single object c in the target category, and all arrows from the
source category to a single identity arrow idc .
In category theory, functors are often used for creating models of one category inside
another. The fact that they can merge multiple objects and arrows into one means that
they produce simplified views of the source category. They “abstract” some aspects of
the source category.
8.3. FUNCTORS IN PROGRAMMING 63
The fact that they may only cover parts of the target category means that the
models are embedded in a larger environment.
Functors from some minimalistic, stick-figure, categories can be used to define pat-
terns in larger categories.
Exercise 8.2.1. Describe a functor whose source is the “walking arrow” category. It’s a
stick-figure category with two objects and a single arrow between them (plus the manda-
tory identity arrows).
idb
ida
f
a b
Exercise 8.2.2. The “walking iso” category is just like the “walking arrow” category,
plus one more arrow going back from b to a. Show that a functor from this category
always picks an isomorphism in the target category.
Endofunctors
The first part of the endofunctor is the mapping of types to types. This is done using
type constructors, which are type-level functions.
The list type constructor, List, maps an arbitrary type a to the type List a.
The Maybe type constructor maps a to Maybe a.
The second part of an endofunctor is the mapping of arrows. Given a function
a -> b, we want to be able to define a function List a -> List b, or Maybe a -> Maybe b.
This is the “functoriality” property of these data types that we have discussed before.
Functoriality lets us lift an arbitrary function to a function between transformed types.
Functoriality can be expressed in Haskell using a typeclass. In this case, the typeclass
is parameterized by a type constructor f. We say that f is a Functor if there is a
corresponding mapping of functions called fmap:
class Functor f where
fmap :: (a -> b) -> (f a -> f b)
The compiler knows that f is a type constructor because it’s applied to types, as in f a
and f b.
To prove to the compiler that a particular type constructor is a Functor, we have
to provide the implementation of fmap for it. This is done by defining an instance of
the typeclass Functor. For example:
instance Functor Maybe where
fmap g Nothing = Nothing
fmap g (Just a) = Just (g a)
64 CHAPTER 8. FUNCTORS
A functor must also satisfy some laws: it must preserve composition and identity.
These laws cannot be expressed in Haskell, but should be checked by the programmer.
We have previously seen a definition of badMap that didn’t satisfy the identity laws,
yet it would be accepted by the compiler. It would define an “unlawful” instance of
Functor for the list type constructor [].
There are some elementary functors that might seem trivial, but they serve as
building blocks for other functors.
We have the identity endofunctor that maps all objects to themselves, and all arrows
to themselves.
data Id a = Id a
Exercise 8.3.2. Show that Id is a Functor. Hint: implement the Functor instance
for it.
We also have a constant functor ∆c that maps all objects to a single object c, and
all arrows to the identity arrow on this object. In Haskell, it’s a family of functors
parameterized by the target object c:
data Const c a = Const c
This type constructor ignores its second argument.
Exercise 8.3.3. Show that (Const c) is a Functor. Hint: The type constructor takes
two arguments, but here it’s partially applied to the first argument. It is functorial in
the second argument.
Bifunctors
We have also seen data constructors that take two types as arguments: the product
and the sum. They were functorial as well, but instead of lifting a single function, they
lifted a pair of functions. In category theory, we would define these as functors from
the product category C × C to C.
Such functors map a pair of objects to an object, and a pair of arrows to an arrow.
In Haskell, we treat such functors as members of a separate class called a Bifunctor.
class Bifunctor f where
bimap :: (a -> a') -> (b -> b') -> (f a b -> f a' b')
Again, the compiler deduces that f is a two-argument type constructor because it sees
it applied to two types, e.g., f a b.
To prove to the compiler that a particular type constructor is a Bifunctor, we
define an instance. For example, bifunctoriality of a pair can be defined as:
instance Bifunctor (,) where
bimap g h (a, b) = (g a, h b)
Contravariant functors
Functors from the opposite category C op are called contravariant. They have the prop-
erty of lifting arrows that go in the opposite direction. Regular functors are sometimes
called covariant.
In Haskell, contravariant functors form the typeclass Contravariant:
class Contravariant f where
contramap :: (b -> a) -> (f a -> f b)
It’s often convenient to think of functors in terms of producers and consumers.
In this picture, a (covariant) functor is a producer. You can turn a producer of a’s
to a producer of b’s by applying (using fmap) a function a->b. Conversely, to turn a
consumer of a’s to a consumer of b’s you need a function going in the opposite direction,
b->a.
Example: A predicate is a function returning True or False:
data Predicate a = Predicate (a -> Bool)
It’s easy to see that it’s a contravariant functor:
instance Contravariant Predicate where
contramap f (Predicate h) = Predicate (h . f)
The only non-trivial examples of contravariant functors are variations on the theme
of function objects.
One way to tell if a given function type is covariant or contravariant in one of the
types is by assigning polarities to the types used in its definition. We say that the
return type is in a positive position, so it’s covariant; and the argument type is in the
negative position, so it’s contravariant. But if you put the whole function object in the
negative position of another function, then its polarities get reversed.
Consider this data type:
data Tester a = Tester ((a -> Bool) -> Bool)
It has a in a double-negative, therefore a positive position. This is why it’s a covariant
Functor. It’s a producer of a’s:
instance Functor Tester where
fmap f (Tester g) = Tester g'
where g' h = g (h . f)
Notice that parentheses are important here. A similar function a -> Bool -> Bool
has a in a negative position. That’s because it’s a function of a returning a function
(Bool -> Bool). Equivalently, you may uncurry it to get a function that takes a pair:
(a, Bool) -> Bool. Either way, a ends up in the negative position.
Profunctors
We’ve seen before that the function type is functorial. It lifts two functions at a time,
just like Bifunctor, except that one of the functions goes in the opposite direction.
66 CHAPTER 8. FUNCTORS
Varying b means switching focus from one object to another, so the complete functor
C(a, −) combines all the arrows emanating from a into a coherent view of the category
from the perspective of a. It is “the world according to a.”
Conversely, when we fix the target and vary the source of the hom-functor, we get
a contravariant functor:
C(−, b) : C op → Set
C(g 0 , b) = (− ◦ g 0 )
The functor C(−, b) organizes all the arrows pointing at b into one coherent view.
It is the picture of b “as seen by the world.”
We can now reformulate the results from the chapter on isomorphisms. If two
objects a and b are isomorphic, than their hom-sets are also isomorphic. In particular:
C(a, x) ∼
= C(b, x)
and
C(x, a) ∼
= C(x, b)
We’ll discuss naturality conditions in the next chapter.
import Data.Kind
A kind signature is just like a type signature, except that it can be used to describe
functions operating on types.
Regular types have the kind Type. Type constructors have the kind Type -> Type,
since they map types to types.
Compose takes two type constructors and produces a type constructor, so its kind
signature is:
(Type -> Type) -> (Type -> Type) -> (Type -> Type)
and the full definition is:
data Compose :: (Type -> Type) -> (Type -> Type) -> (Type -> Type)
where
Compose :: (g (f a)) -> Compose g f a
Any two type constructors can be composed this way. There is no requirement, at
this point, that they be functors.
However, if we want to lift a function using the composition of type constructors, g
after f, then they must be functors. This requirement is encoded as a constraint in the
instance declaration:
instance (Functor g, Functor f) => Functor (Compose g f) where
fmap h (Compose gfa) = Compose (fmap (fmap h) gfa)
The constraint (Functor g, Functor f) expresses the condition that both type con-
structors be instances of the Functor class. The constraints are followed by a double
arrow.
The type constructor whose functoriality we are establishing is Compose f g, which
is a partial application of Compose to two functors.
In the implementation of fmap, we pattern match on the data constructor Compose.
Its argument gfa is of the type g (f a). We use one fmap to “get under” g. Then we
use (fmap h) to get under f. The compiler knows which fmap to use by analyzing the
types.
You may visualize a composite functor as a container of containers. For instance,
the composition of [] with Maybe is a list of optional values.
Category of categories
We can view functors as arrows between categories. As we’ve just seen, functors are
composable and it’s easy to check that this composition is associative. We also have
an identity (endo-) functor for every category. So categories themselves seem to form a
category, let’s call it Cat.
And this is where mathematicians start worrying about “size” issues. It’s a short-
hand for saying that there are paradoxes lurking around. So the correct incantation is
that Cat is a category of small categories. But as long as we are not engaged in proofs
of existence, we can ignore size problems.
Chapter 9
Natural Transformations
We’ve seen that, when two objects a and b are isomorphic, they generate bijections
between sets of arrows, which we can now express as isomorphisms between hom-sets:
C(a, x) ∼
= C(b, x)
C(x, a) ∼
= C(x, b)
The converse is not true, though. An isomorphism between hom-sets does not result
in an isomorphism between object unless additional naturality conditions are satis-
fied. We’ll now re-formulate these naturality conditions in progressively more general
settings.
αx
a b
In other words, for every x, there is a mapping of hom-sets:
αx : C(x, a) → C(x, b)
When we vary x, the two hom-sets become two (contravariant) functors, and α can
be seen as a mapping between two functors: C(−, a) and C(−, b). Such a mapping, or a
transformation, is really a family of individual mappings αx , one per each object x in
the category C.
The functor C(−, a) describes the way the worlds sees a, and the functor C(−, b)
describes the way the world sees b.
69
70 CHAPTER 9. NATURAL TRANSFORMATIONS
The transformation α switches back and forth between these two views. Every
component of α, the bijection αx , shows that the view of a from x is isomorphic to the
view of b from x.
The naturality condition we discussed before was the condition:
αy ◦ (− ◦ g) = (− ◦ g) ◦ αx
It relates components of α taken at different objects. In other words, it relates the
views from two different observers x and y, who are connected by the arrow g : y → x.
Both sides of this equation are acting on the hom-set C(x, a). The result is in the
hom-set C(y, b)
Precomposition with g : y → x is also a mapping of hom-sets. In fact it is the
lifting of g by the contravariant hom-functor. We can write it as C(g, a) and C(g, b),
respectively.
The naturality condition can therefore be rewritten as:
αy ◦ C(g, a) = C(g, b) ◦ αx
C(g,a)
C(x, a) C(y, a)
αx αy
C(g,b)
C(x, b) C(y, b)
We can now say that an invertible transformation α between the functors C(−, a) and
C(−, b) that satisfies the naturality condition is equivalent to an isomorphism between
a and b.
We can follow exactly the same reasoning for the outgoing arrows. This time we
start with a transformation β whose components are:
βx : C(a, x) → C(b, x)
The two (covariant) functors C(a, −) and C(b, −) describe the view of the world from
the perspective of a and b, respectively. The invertible transformation β tells us that
these two views are equivalent, and the naturality condition
(g ◦ −) ◦ βx = βy ◦ (g ◦ −)
C(a,g)
C(a, x) C(a, y)
βx βy
C(b,g)
C(b, x) C(b, y)
Ff
Fx Fy
αx αy
Gf
Gx Gy
A family of arrows αx that satisfies the naturality condition is called a natural
transformation.
This is a diagram that shows a pair of categories, two functors between them, and
a natural transformation α between the functors:
F
C α D
G
72 CHAPTER 9. NATURAL TRANSFORMATIONS
Since for every arrow in C there is a corresponding naturality square, we can say that
a natural transformation maps objects to arrows, and arrows to commuting squares.
If every component αx of a natural transformation is an isomorphism, α is called a
natural isomorphism.
We can now restate the main result about isomorphisms: Two objects are isomorphic
if and only if there is a natural isomorphism between their hom-functors (either the
covariant, or the contravariant ones—either one will do).
Natural transformations provide a very convenient high-level way of expressing com-
muting conditions in a variety of situations. We’ll use them in this capacity to refor-
mulate the definitions of algebraic data types.
categories C and D is [C, D], that is the names of the two categories between square
brackets.
The objects in [C, D] are functors, the arrows are natural transformations.
To show that this is indeed a category, we have to define the composition of natural
transformations. This is easy if we keep in mind that components of natural transfor-
mations are regular arrows in the target category. These arrows compose.
Indeed, suppose that we have a natural transformation α between two functors F
and G. We want to compose it with another natural transformation β that goes from
G to H.
α
G
C D
β
H
Let’s look at the components of these transformations at some object x
αx : F x → G x
βx : G x → H x
These are just two arrows in D that are composable. So we can define a composite
natural transformation γ as follows:
γ: F → H
γx = βx ◦ αx
This is called the vertical composition of natural transformations. You’ll see it written
using a dot γ = β · α or a simple juxtaposition γ = βα.
Naturality condition for γ can be shown by pasting together (vertically) two natu-
rality squares for α and β:
Ff
Fx Fy
αx αy
Gf
γx Gx Gy γy
βx βy
Hf
Hx Hy
(idF )x = idF x
9.4. THE FUNCTOR CATEGORY 75
[C, D](F, G)
with the name of the category followed by the names of the two objects (here, functors)
in parentheses.
Exercise 9.4.1. Prove the naturality condition of the composition of natural transfor-
mations:
γy ◦ F f = Hf ◦ γx
Hint: Use the definition of γ and the two naturality conditions for α and β.
F: C →D G: D → E
F0: C → D G0 : D → E
α: F → F0 β : G → G0
Pictorially:
F G
C α D β E
F0 G0
The horizontal composition β ◦ α maps G ◦ F to G0 ◦ F 0 .
Let’s pick an object x in C. We use α to map it to an arrow
αx : F x → F 0 x
What we need in order to define β ◦ α is an arrow from G(F x) to G0 (F 0 x). To get there,
we can use the appropriate component of β
βF 0 x : G(F 0 x) → G0 (F 0 x)
76 CHAPTER 9. NATURAL TRANSFORMATIONS
Altogether, we have
(β ◦ α)x = βF 0 x ◦ G(αx )
(β ◦ α)x = G0 (αx ) ◦ βF x
G(F x)
βF x
Fx G(αx ) G0 (F x)
x αx G(F 0 x) G0 (αx )
βF 0 x
F 0x G0 (F 0 x)
Whiskering
Quite often, horizontal composition is used with one of the natural transformations
being the identity. There is a shorthand notation for such composition. For instance,
β ◦ idF is written as β ◦ F .
Because of the characteristic shape of the diagram, such composition is called
“whiskering”.
G
F β
C D E
G0
In components, we have:
(β ◦ F )x = βF x
C α D G
E
F0
In components:
(G ◦ α)x = G(αx )
In Haskell, the lifting of αx by G is done using fmap, so given:
alpha :: F x -> F' x
the whiskered version would be:
g_alpha :: G (F x) -> G (F' x)
g_alpha = fmap alpha
Again, Haskell’s type inference engine figures out which version of fmap to use (here,
it’s the one from the Functor instance of G).
Interchange law
We can combine vertical composition with horizontal composition, as seen in the fol-
lowing diagram:
F F0
α α0
G G0
C D E
β β0
H H0
The interchange law states that the order of composition doesn’t matter: we can first do
vertical compositions and then the horizontal one, or first do the horizontal compositions
and then the vertical one.
object, how it can be used, and how it interacts with other objects. We are looking at
objects from the utilitarian point of view.
Both approaches have their advantages. The categorical approach came later, be-
cause you need to study a lot of examples before clear patterns emerge. But once you
see the patterns, you discover unexpected connections between things, like the duality
between sums and products.
Defining particular objects through their connections requires looking at possibly
infinite numbers of objects with which they interact.
“Tell me your place in the Universe, and I’ll tell you who you are.”
Defining an object by its mappings-out or mappings-in with respect to all objects
in the category is called a universal construction.
Why are natural transformations so important? It’s because most categorical con-
structions involve commuting diagrams. If we can re-cast these diagrams as naturality
squares, we move one level up the abstraction ladder and gain new valuable insights.
Being able to compress a lot of facts into small elegant formulas helps us see new
patterns. We’ll see, for instance, that natural isomorphisms between hom-sets pop up
all over category theory and eventually lead to the idea of an adjunction.
But first we’ll study several examples in greater detail to get some understanding of
the terse language of category theory. We’ll try, for instance, to decode the statement
that the sum, or the coproduct of two objects, is defined by the following natural
isomorphism:
[2, C](D, ∆x ] ∼
= C(a + b, x)
Picking objects
Even such a simple task as pointing at an object has a special interpretation in category
theory. We have already seen that pointing at an element of a set is equivalent to
selecting a function from the singleton set to it. Similarly, picking an object in a
category is equivalent to selecting a functor from the single-object category. Or it can
be done using a constant functor from another category.
Quite often we want to pick a pair of objects. That, too, can be accomplished by
selecting a functor from a two-object stick-figure category. Similarly, picking an arrow
is equivalent to selecting a functor from the “walking arrow” category, and so on.
By judiciously selecting our functors and natural transformations between them, we
can reformulate all the universal constructions we’ve seen so far.
a b
Left Right
a+b
f g
h
C
9.5. UNIVERSAL CONSTRUCTIONS REVISITED 79
This diagram can be further decomposed into two simpler shapes called cospans:
a b
f g
x
To construct a cospan we first have to pick a pair of objects. To do that we’ll start
with a two-object category 2. We’ll call its objects 1 and 2. We’ll use a functor
D: 2 → C
∆x : 2 → C
to select the object x. This functor maps both 1 and 2 to x (and the two identity arrows
to idx ).
Since both functors go from 2 to C, we can define a natural transformation α between
them. In this case, it’s just a pair of arrows:
α1 : D 1 → ∆x 1
α2 : D 2 → ∆x 2
These are exactly the two arrows f and g in the cospan.
Naturality condition for α is trivial, since there are no arrows (other than identities)
in 2.
There may be many cospans sharing the same three objects—meaning: there may
be many natural transformations between the two functors D and ∆x . These natural
transformations form a hom-set in the functor category [2, C], namely:
[2, C](D, ∆x )
Functoriality of cospans
Let’s consider what happens when we start varying the object x in a cospan. We get a
mapping F from x to the set of cospans:
F x = [2, C](D, ∆x )
This might look very abstract until you remember that natural transformations have
components, and these components are just regular arrows. An element of the left-hand
side is a natural transformation:
µ : D → ∆x
It has two components corresponding to the two objects in 2. For instance, we have
µ1 : D 1 → ∆ x 1
µ1 : a → x
ν : D → ∆y
a b
Left Right
a+b
f g
h
x
C(a + b, x)
[2, C](D, ∆x ) ∼
= C(a + b, x)
Moreover, if we vary the object x, the two sides behave like functors from C to Set.
Therefore it makes sense to ask if this mapping of functors is a natural isomorphism.
Indeed, it can be shown that the naturality condition for this isomorphism translates
into commuting conditions for the triangles in the definition of the sum. So the definition
of the sum can be replaced by a single equation.
α : ∆x → D
a b
Collectively, these natural transformations form a hom-set in the functor category :
[2, C](∆x , D)
[2, C](∆x , D) ∼
= C(x, a × b)
It can be shown that the naturality of this isomorphism guarantees that the triangles
in this diagram commute:
C
f =α1 h g=α2
a×b
fst snd
a = D1 b = D2
Exponentials
The exponentials, or function objects, are defined by this commuting diagram:
x×a
f
h×ida
ba × a εa,b b
82 CHAPTER 9. NATURAL TRANSFORMATIONS
C(x × a, b) ∼
= C(x, ba )
The f in the diagram above is an element of the left-hand side, and h is the corre-
sponding element of the right-hand side. The transformation α maps f to h. In Haskell,
we call it curry. Its inverse, α−1 is known as uncurry.
Unlike in the previous examples, here both hom-sets are in the same category, and
it’s easy to analyze the isomorphism in more detail. In particular, we’d like to see how
the commuting condition:
f = εa,b ◦ (h × ida )
ba × a
id(ba ) ×ida f
ba × a εa,b b
The commuting condition in this case tells us that f = εa,b . In other words, we get the
formula for εa,b in terms of α:
εa,b = α−1 (id(ba ) )
Similarly
9.5. UNIVERSAL CONSTRUCTIONS REVISITED 83
The natural transformation α is just a thin encapsulation of curry; and its inverse
is uncurry:
alpha :: forall a b x. LeftFunctor a b x -> RightFunctor a b x
alpha (LF f) = RF (curry f)
(−◦(g×ida ))
C(y × a, b) C(x × a, b)
αy αx
(−◦g)
C(y, ba ) C(x, ba )
Let’s now apply the Yoneda trick to it and replace y with ba . This also allows us to
substitute g, which now goes for x to ba , with h.
(−◦(h×ida ))
C(ba × a, b) C(x × a, b)
α(ba ) αx
(−◦h)
C(ba , ba ) C(x, ba )
We know that the hom-set C(ba , ba ) contains at least the identity arrow, so we can
pick the element id(ba ) in the lower left corner.
α−1 acting on it produces εa,b in the upper left corner (that’s the uncurry id trick).
Pre-composition with h acting on identity produces h in the lower right corner.
α−1 acting on h produces f in the upper right corner.
(−◦(h×ida ))
εa,b f
α−1 α−1
(−◦h)
id(ba ) h
D3
h
f g
D1 D2
h
D3 x
Since the indexing category may now contain arrows, the naturality conditions for
these diagrams are no longer trivial. The constant functor ∆x shrinks all vertices to
one, so naturality squares turn into triangles. Naturality means that all triangles with
X in their apex must now commute.
The universal cone, if it exists, is called the limit of the diagram D, and is written
as LimD. Universality means that it satisfies the following isomorphism, natural in x:
[J , C](∆x , D) ∼
= C(x, LimD)
Dually, the universal cocone is called a colimit, and is described by the following natural
isomorphism:
[J , C](D, ∆x ) ∼
= C(ColimD, x)
We can now say that a product is a limit, and a sum is a colimit, of a diagram from
the indexing category 2.
Limits and colimits distill the essence of a pattern.
A limit, like a product, is defined by its mapping-in property.
A colimit, like a sum, is defined by its mapping-out property.
There are many interesting limits and colimits, and we’ll see some when we discuss
algebras and coalgebras.
9.7. THE YONEDA LEMMA 85
Exercise 9.6.1. Show that the limit of a “walking arrow” category, that is a two-object
category with an arrow connecting the two objects, has the same elements as the first
object in the diagram (“elements” are the arrows from the terminal object).
αx : C(a, x) → F x
We can now apply the Yoneda trick: substitute a for x and pick the identity ida as the
element of C(a, a). This gives us an element αa (ida ) in the set F a.
86 CHAPTER 9. NATURAL TRANSFORMATIONS
Now the other way around. Take an element p of the set F a. We want to implement
a natural transformation that takes an arrow h from C(a, x) and produces an element
of F x. This is simply done by lifting the arrow h using F . We get a function
Fh: Fa → Fx
We can apply this function to p to get an element of F x. We take this element as the
action of αx on h.
C(a, x) → F x
The isomorphism in the Yoneda lemma is natural not only in a but also in F . In
other words, you can “move” from the functor F to another functor G by applying an
arrow in the functor category, that is a natural transformation. This is quite a leap
in the levels of abstraction, but all the definitions of functoriality and naturality work
equally well in the functor category, where objects are functors, and arrows are natural
transformations.
server. It’s also useful as a program transformation that turns recursive algorithms into
tail-recursive functions.
Continuation-passing style is difficult to work with because the composition of con-
tinuations is highly nontrivial, resulting in what programmers often call a “callback
hell.” Fortunately continuations form a monad, which means their composition can be
automated.
[C op , Set](C(−, a), F ) ∼
= Fa
This is the Haskell implementation of the mapping:
coyoneda :: Contravariant f => (forall x. (x -> a) -> f x) -> f a
coyoneda g = g id
And this is the inverse transformation:
coyoneda_1 :: Contravariant f => f a -> (forall x. (x -> a) -> f x)
coyoneda_1 y = \h -> contramap h y
C op × C → Set
These Set-valued functor categories are common enough that they have special
names. The functors in [C op , Set] are called presheaves, and the ones in [C, Set] are
called co-presheaves. (The names come from algebraic topology.)
Let’s focus our attention on the following reading of the hom-functor:
Y : C → [C op , Set]
Yx = C(−, x)
which can be visualized as the totality of views of x from all possible directions.
Let’s also review its action on arrows. The functor Y lifts an arrow f : x → y to a
mapping of presheaves:
α : C(−, x) → C(−, y)
The component of this natural transformation at some z is a function between hom-sets:
αz : C(z, x) → C(z, y)
[C op , Set](C(−, x), F ) ∼
= Fx
In particular, we can substitute another hom-functor C(−, y) for F :
The left-hand side is the hom-set in the presheaf category and the right-hand side is the
hom-set in C. They are isomorphic, which proves that the embedding is fully faithful.
Let’s have a closer look at this isomorphism. Let’s pick an element of the right-hand
set C(x, y)—an arrow f . The isomorphism maps it to a natural transformation whose
component at z is a function:
C(z, x) → C(z, y)
This mapping is implemented as post-composition (f ◦ −).
In Haskell, we would write it as:
9.9. REPRESENTABLE FUNCTORS 89
((f ◦ g) ◦ −) = (f ◦ −) ◦ (g ◦ −)
Because it preserves composition and identity, this isomorphism also preserves iso-
morphisms. So if x is isomorphic to y then the presheaves C(−, x) and C(−, y) are
isomorphic, and vice versa.
This is exactly the result that we’ve been using all along to prove numerous isomor-
phisms in previous chapters.
C(1, xa ) ∼
= C(1 × a, x) ∼
= C(a, x)
Y : x 7→ C(−, x)
We can find the entire category C, objects and morphisms, embedded inside the
presheaf category as representable functors. The question is, what else is there in the
presheaf category “in between” representable functors?
Just like rational numbers are dense among real numbers, so representables are
“dense” among (co-) presheaves. Every real number may be approximated by rational
90 CHAPTER 9. NATURAL TRANSFORMATIONS
Exercise 9.9.1. Describe limits and colimits as representing objects. What are the
functors they represent?
where
tab n = Stm (g n) (tab (S n))
index stm = \n -> ind n stm
where
ind Z (Stm a _) = a
ind (S n) (Stm _ as) = ind n as
Representable types are useful in implementing memoization of functions.
Exercise 9.9.2. Implement the Representable instance for Pair:
data Pair x = Pair x x
Exercise 9.9.3. Is the constant functor that maps everything to the terminal object
representable? Hint: what’s the logarithm of 1?
In Haskell, such a functor could be implemented as:
data Unit a = U
Implement the instance of Representable for it.
Exercise 9.9.4. The list functor is not representable. But can it be considered a sum
or representables?
Adjunctions
C(a × b, c) ∼
= C(a, cb )
The object c takes the role of the focus on the left hand side; the object a becomes the
observer on the right hand side.
On the left, a is mapped to a product a × b, and on the right, c is exponentiated.
We can spot two functors at play. They are both parameterized by b. On the left
we have the functor (− × b) applied to a. On the right we have the functor (−)b applied
to c.
If we write these functors as:
Lb a = a × b
Rb c = cb
then the natural isomorphism
C(Lb a, c) ∼
= C(a, Rb c)
93
94 CHAPTER 10. ADJUNCTIONS
(− × b) a (−)b
The exponential object ba is sometimes called the internal hom and is written as
[a, b]. This is in contrast to the external hom, which is the set C(a, b). The external
hom is not an object in C (except when C itself is Set). The currying adjunction can
then be written as:
C(a × b, c) ∼
= C(a, [b, c])
The category of (small) categories Cat is cartesian closed, as reflected in this adjunc-
tion between product categories and functor categories that uses the same internal-hom
notation:
Cat(A × B, C) ∼
= Cat(A, [B, C])
C(a + b, x) ∼
= C(a, x) × C(b, x)
where the product on the right-hand side is just a cartesian product of sets, that is the
set of pairs. Moreover, we’ve seen earlier that this bijection is natural in x.
We know that a pair of arrows is a single arrow in the product category. We can,
therefore, look at the elements on the right-hand side as arrows in C × C going from
the object ha, bi to the object hx, xi. The latter can be obtained by acting with the
diagonal functor ∆ on x. We have:
C(a + b, x) ∼
= (C × C)(ha, bi, ∆x)
This is a bijection between hom-sets in two different categories. It satisfies naturality
conditions, so it’s a natural isomorphism.
We can spot a pair of functors here as well. On the left-hand side we have the
functor that takes a pair of objects ha, bi and produces their sum a + b:
(+) : C × C → C
On the right-hand side, we have the diagonal functor ∆ going in the opposite direction:
∆: C → C × C
(+)
C C×C
∆
(+)
a+b ha, bi
x hx, xi
∆
(+) a ∆
C(x, a) × C(x, b) ∼
= C(x, a × b)
96 CHAPTER 10. ADJUNCTIONS
C×C C
(×)
hx, xi x
ha, bi a×b
(×)
∆ a (×)
C(Lx, y) ∼
= D(x, Ry)
C D
R
Lx x
y Ry
R
10.4. LIMITS AND COLIMITS 97
These hom-sets come from two different categories, but sets are just sets. We say that
L is the left adjoint of R, or that R is the right adjoint of L
In Haskell, the simplified version of this could be encoded as a multi-parameter type
class:
class (Functor left, Functor right) => Adjunction left right where
ltor :: (left x -> y) -> (x -> right y)
rtol :: (x -> right y) -> (left x -> y)
It requires the following pragma at the top of the file:
{- # language MultiParamTypeClasses # -}
Therefore, in a bicartesian category, the sum is the left adjoint to the diagonal
functor, and the product is its right adjoint. We can write this very concisely (or we
could impress it in clay, in a modern version of cuneiform):
(+) a ∆ a (×)
Exercise 10.3.1. The hom-set C(Lx, y) on the left-hand side of the adjunction formula
suggests that Lx could be seen as a representing object for some functor (a co-presheaf ).
What is this functor? Hint: It maps a y to a set. What set is it?
Px ∼
= D(x, a)
What is the presheaf for which Ry, in the adjunction formula, is the representing object.
[I, C](∆x , D) ∼
= C(x, LimD )
The hom-set on the left is in the functor category. Its elements are cones, or natural
transformations between the constant functor and the diagram functor. The one on the
right is a hom-set in C.
In a category where all limits exist, we have the adjunction between these two
functors:
∆(−) : C → [I, C]
Lim(−) : [I, C] → C
[I, C](D, ∆x ) ∼
= C(ColimD , x)
Colim a ∆ a Lim
98 CHAPTER 10. ADJUNCTIONS
C D
R
such that
L ◦ R = IdC
IdD = R ◦ L
This definition involves equality of functors, though. What’s worse, acting on objects,
it involves equality of objects:
L(Rx) = x
y = R(Ly)
This is why it’s more proper to talk about a weaker notion of equivalence of categories,
where equalities are replaced by isomorphisms:
L◦R∼
= IdC
IdD ∼
=R◦L
On objects, an equivalence of categories means that a round trip produces an object
that is isomorphic, rather than equal, to the original one. In most cases, this is exactly
what we want.
10.5. UNIT AND COUNIT OF ADJUNCTION 99
C(Lx, y) ∼
= D(x, Ry)
C(Lx, Lx) ∼
= D(x, R(Lx))
We can now use the Yoneda trick and pick the identity arrow idLx on the left. The
isomorphism maps it to a unique arrow on the right, which we’ll call ηx :
ηx : x → R(Lx)
Not only is this mapping defined for every x, but it’s also natural in x. The natural
transformation η is called the unit of the adjunction. If we observe that the x on the
left is the action of the identity functor on x, we can write:
η : IdD → R ◦ L
C(L(Ry), y) ∼
= D(Ry, Ry)
ε : L ◦ R → IdC
Notice that, if those two natural transformations were invertible, they would wit-
ness the equivalence of categories. But this kind of “half-equivalence” is even more
interesting in the context of category theory.
Triangle identities
We can use the unit/counit pair to formulate an equivalent definition of an adjunction.
To do that, we’ll start with two natural transformations:
η : IdD → R ◦ L
ε : L ◦ R → IdC
and impose additional triangle identities.
These identities are derived by noticing that η can be used to replace an identity
functor with the composite R ◦ L, effectively letting us insert R ◦ L anywhere an identity
functor would work.
Similarly, ε can be used to eliminate the composite L ◦ R (i.e., replace it with
identity).
100 CHAPTER 10. ADJUNCTIONS
Here, we used the horizontal composition of natural transformation, with one of them
being the identity transformation (a.k.a., whiskering).
The first triangle identity is the condition that this chain of transformations result
in the identity natural transformation. Pictorially:
L◦η
L L◦R◦L
ε◦L
idL
L
Similarly, the following chain of natural transformations should also compose to
identity:
η◦R R◦ε
R = IdD ◦ R −−→ R ◦ L ◦ R −−→ R ◦ IdC = R
or, pictorially:
η◦R
R R◦L◦R
R◦ε
idR
R
It turns out that an adjunction can be alternatively defined in terms of the two
natural transformations, η and ε, as long as the triangle identities are satisfied:
(ε ◦ L) · (L ◦ η) = idL
(R ◦ ε) · (η ◦ R) = idR
The mapping of hom-sets can be easily recovered. For instance, let’s start with an
arrow f : x → Ry, which is an element of D(x, Ry). We can lift it to
Lf : Lx → L(Ry)
We can then use η to collapse the composite L ◦ R to identity. The result is a mapping
Lx → y, which is an element of C(Lx, y).
The definition of the adjunction using unit and counit is more general in the sense
that it can be translated to a 2-category setting.
Exercise 10.5.1. Given an arrow g : Lx → y implement an arrow x → Ry using ε and
the fact that R is a functor. Hint: Start with the object x and see how you can get from
there to Ry with one stopover.
C(a × b, c) ∼
= C(a, cb )
C(a × b, a × b) ∼
= C(a, (a × b)b )
10.6. DISTRIBUTIVITY 101
Corresponding to the identity arrow on the left, we get the unit of the adjunction:
η : a → (a × b)b
This is a curried version of the product constructor. In Haskell, we write it as:
mkpair :: a -> (b -> (a, b))
mkpair = curry id
The counit is more interesting. Replacing a with cb we get:
C(cb × b, c) ∼
= C(cb , cb )
Corresponding to the identity arrow on the right, we get:
ε : cb × b → c
which is the function application arrow.
In Haskell:
apply :: (b -> c, b) -> c
apply = uncurry id
Exercise 10.5.2. Derive the unit and counit for the sum and product adjunctions.
10.6 Distributivity
In a bicartesian closed category products distribute over sums. We’ve seen one direction
of the proof using universal constructions. Adjunctions combined with the Yoneda
lemma give us more powerful tools to tackle this problem.
We want to show the natural isomorphism:
(b + c) × a ∼
=b×a+c×a
Instead of proving this identity directly, we’ll show that the mappings out from both
sides to an arbitrary object x are isomorphic:
C((b + c) × a, x) ∼
= C(b × a + c × a, x)
The left hand side is a mapping out of a product, so we can apply the currying adjunction
to it:
C((b + c) × a, x) ∼
= C(b + c, xa )
This gives us a mapping out of a sum which, by the sum adjunction is isomorphic to
the product of two mappings:
C(b + c, xa ) ∼
= C(b, xa ) × C(c, xa )
We can now apply the inverse of the currying adjunction to both components:
C(b, xa ) × C(c, xa ) ∼
= C(b × a, x) × C(c × a, x)
Using the inverse of the sum adjunction, we arrive at the final result:
C(b × a, x) × C(c × a, x) ∼
= C(b × a + c × a, x)
Every step in this proof was a natural isomorphism, so their composition is also a
natural isomorphism. By Yoneda lemma, the two objects that form the left- and the
right-hand side of distributivity law are therefore isomorphic.
102 CHAPTER 10. ADJUNCTIONS
Fx x
y Uy
U
M1 µ1 M1 ⊗ M1
η1
I f f ⊗f
η2
M2 µ2 M2 ⊗ M2
f ◦ η1 = η2
f ◦ µ1 = µ2 ◦ (f ⊗ f )
In particular, the category Set is monoidal, with cartesian product and the terminal
object providing the monoidal structure.
Monoids in Set are sets with additional structure. They form their own category
Mon(Set) and there is a forgetful functor U that simply maps the monoid to the set
of its elements. When we say that a monoid is a set, we mean the underlying set.
Free monoid
We want to construct the free functor
F : Set → Mon(Set)
that is adjoint to the forgetful functor U . We start with an arbitrary set X and an
arbitrary monoid m.
On the right-hand side of the adjunction we have the set of functions between two
sets, X and U m. On the left-hand side, we have a set of highly constrained structure-
preserving monoid morphisms from F X to m. How can these two sets be isomorphic?
In Mon(Set), monoids are just sets of elements, and a monoid morphism is a
function between such sets, satisfying additional constraints: it has to preserve unit
and multiplication.
Arrows in Set, on the other hand, are just functions with no additional constraints.
So, in general, there are fewer arrows between monoids than there are between their
underlying sets.
FX X
m Um
U
Here’s the idea: if we want to have a one to one matching between arrows, we want
F X to be much larger than X. This way, there will be many more functions from it to
m—so many that, even after rejecting the ones that don’t preserve the structure, we’ll
still have enough to match every function f : X → U m.
We’ll construct the monoid F X starting from the set X, adding more elements as
necessary. We’ll call X the set of generators of F X.
We’ll construct a monoid morphism g : F X → m starting from the function f . On
generators, x ∈ X, g works the same as f :
gx = f x
ge = e0
104 CHAPTER 10. ADJUNCTIONS
Exercise 10.7.1. What is the unit and the counit of the free monoid adjunction F a U ?
To show that it’s a free monoid, we have to be able to construct a monoid morphism
from the list of a to an arbitrary monoid m, provided we have an (unconstrained)
mapping from a to (the underlying set of) m. We can’t express all of this in Haskell,
but we can define the function:
foldMap :: Monoid m => (a -> m) -> ([a] -> m)
foldMap f = foldr mappend mempty . fmap f
This function transforms the elements of the list to monoidal values using f and then
folds them using mappend, starting with the unit mempty.
It’s easy to see that an empty list is mapped to the monoidal unit. It’s not too
hard to see that a concatenation of two lists is mapped to the monoidal product of the
results. So, indeed, foldMap produces a monoid morphism.
Following the intuition of a free monoid being a domain-specific program, foldMap
provides an interpreter for this program. It performs all the multiplications that have
been postponed. Note that the same program may be interpreted in many ways, de-
pending on the choice of the concrete monoid and the function f.
We’ll come back to free monoids as lists in the chapter on algebras.
Exercise 10.7.2. Write a program that takes a list of integers and interprets it in two
ways: once using the additive and once using the multiplicative monoid of integers.
L0 L
C D E
R0 R
C(L0 (Le), c)
D(Le, R0 c)
C((L0 ◦ L)e, c) ∼
= E(e, (R ◦ R0 )c)
that serves as the identity with respect to composition of adjunctions. Therefore we can
define a category Adj(Cat) in which objects are categories and arrows are adjunctions
(by convention, pointing in the direction of the left adjoint).
Adjunctions can be defined purely in terms of functors and natural transformations,
that is 1-cells and 2-cells in the 2-category Cat. There is nothing special about Cat,
and in fact adjunctions can be defined in any 2-category. Moreover, the category of
adjunctions is itself a 2-category.
Dependent Types
We’ve seen types that depend on other types. They are defined using type construc-
tors with type parameters, like Maybe or []. Most programming languages have some
support for generic data types—data types parameterized by other data types.
Categorically, such types are modeled as functors 1 .
A natural generalization of this idea is to have types that are parameterized by
values. For instance, it’s often advantageous to encode the length of a list in its type.
A list of length zero would have a different type than a list of length one, and so on.
Types parameterized by values are called dependent types. There are languages like
Idris or Agda that have full support for dependent types. It’s also possible to implement
dependent types in Haskell, but support for them is still rather patchy.
The reason for using dependent types in programming is to make programs provably
correct. In order to do that, the compiler must be able to check the assumptions made
by the programmer.
Haskell, with its strong type system, is able to uncover a lot of bugs at compile
time. For instance, it won’t let you write a <> b (infix notation for mappend), unless
you provide the Monoid instance for the type of your variables.
However, within Haskell’s type system, there is no way to express or, much less
enforce, the unit and associativity laws for the monoid. For that, the instance of the
Monoid type class would have to carry with itself proofs of equality (not actual code):
assoc :: m <> (n <> p) = (m <> n) <> p
lunit :: mempty <> m = m
runit :: m <> mempty = m
Dependent types, and equality types in particular, pave the way towards this goal.
The material in this chapter is more advanced, and not used in the rest of the book,
so you may safely skip it on first reading. Also, to avoid confusion between fibers and
functions, I decided to use capital letters for objects in parts of this chapter.
107
108 CHAPTER 11. DEPENDENT TYPES
Exercise 11.1.1. Implement the function tailV that returns the tail of the non-zero-
length vector. Try calling it with emptyV.
Fibrations
Although intuitively easy to visualize, this point of view doesn’t generalize nicely to
category theory, where we don’t like mixing sets with objects. So we turn this picture
on its head and instead of talking about injecting family members into the sum, we
consider a mapping that goes in the opposite direction.
This, again, we can first visualize using sets. We have one big set E describing the
whole family, and a function p called the projection, or a display map, that goes from
E down to the indexing set B (also called the base).
This function will, in general, map multiple elements to one. We can then talk
about the inverse image of a particular element x ∈ B as the set of elements that get
mapped down to it by p. This set is called the fiber and is written p−1 x (even though,
in general, p is not invertible in the usual sense). Seen as a collection of fibers, E is
often called a fiber bundle or just a bundle.
p−1 x
E
x B
...
0 1 2 3 4
Slice categories
In category theory we like to describe things in bulk—defining internal structure of
things by structure-preserving maps between them. Such is the case with fibrations.
If we fix the base object b and consider all possible source objects in the category
C, and all possible projections down to b, we get a slice category C/b (also known as an
over-category).
An object in the slice category is a pair he, pi, with p : e → b. An arrow between
two objects he, pi and he0 , p0 i is an arrow f : e → e0 that commutes with the projections,
that is:
p0 ◦ f = p
Again, the best way to visualize this is to notice that such an arrow maps fibers of
p to fibers of p0 . It’s a “fiber-preserving” mapping between bundles.
f
e e0
p p0
b
Our counted vectors can be seen as objects in the slice category C/N given by pairs
hList(a), lengthi.
Coslice categories
There is also a dual notion of a coslice category c/C, also known as an under-category.
It’s a category of arrows emanating from a fixed object c. Objects in this category
are pairs ha, i : c → ai. Morphisms in c/C are arrows that make the relevant triangles
commute.
c
i j
a f
b
11.2. DEPENDENT TYPES CATEGORICALLY 111
In particular, if the category C has a terminal object 1, then the coslice 1/C has, as
objects, global elements of all objects of C.
There is a forgetful functor U : 1/C → C that maps ha, ii to a. This functor defines
a fibration of 1/C. Objects in the fiber over some a are in one-to-one correspondence
with global elements of a.
1
x y
a f
b
Morphisms of 1/C that correspond to arrows f : a → b map the set of global elements
of a to the set of global elements of b. Thus this fibration justifies our intuition of types
as sets of values, with values represented by global elements of types.
Pullbacks
We’ve seen a lot of examples of commuting squares. Such a square is a graphical
representation of an equation: two path between opposite corners of a square, each a
result of a composition of two morphisms, are equal.
Like with every equality we may want to replace one or more of its components
with an unknown, and try to solve the resulting equation. For instance, we may ask the
question: Is there an object together with two arrows that would complete a commuting
square? If many such objects exist, is there a universal one? If the missing piece of the
puzzle is the upper left corner of a square (the source), we call it a pullback. If it’s the
lower right corner (the target), we call it a pushout.
Let’s start with a particular fibration p : E → B and ask ourselves the question:
what happens when we change the base from B to some A that is related to it through
a mapping f : A → B. Can we “pull the fibers back” along f ?
Again, let’s think about sets first. Imagine picking a fiber in E over some point
y ∈ B that is in the image of f . We can plant this fiber over all points in A that are
in the inverse image f −1 y. If multiple points in A are mapped to the same point in B,
we just duplicate the corresponding fiber. This way, every point in A will have a fiber
sticking out of it. The sum of all these fibers will form a new bundle E 0 .
E0 E
A x1 x2 y B
We have thus constructed a new fibration with the base A. Its projection p0 : E 0 → A
maps each point in a given fiber to the point over which this fiber was planted. There
is also an obvious mapping g : E 0 → E that maps fibers to their corresponding fibers.
By construction, this new fibration hE 0 , p0 i satisfies the condition:
p ◦ g = f ◦ p0
112 CHAPTER 11. DEPENDENT TYPES
x q0
h
g
e0 e
q y
p0 p
f
a b
The angle symbol in the corner of the square is used to mark pullbacks.
If we look at the pullback through the prism of sets and fibrations, E is a bundle
over B, and we are constructing a new bundle E 0 out of the fibers taken from E. Where
we plant these fibers over A is determined by (the inverse image of) f . This procedure
makes E 0 a bundle over both A and B, the latter with the projection p ◦ g = f ◦ p0 .
X in this picture is some other bundle over A with the projection q. It is simulta-
neously a bundle over B with the projection f ◦ q = p ◦ q 0 . The unique mapping h maps
the fibers of X given by q −1 to fibers of E 0 given by p0 .
All mappings in this picture work on fibers. Some of them rearrange fibers over new
bases—that’s what a pullback does. This is analogous to what natural transformations
do to containers. Others modify individual fibers—the mapping h : X → E 0 works
like this. This is analogous to what fmap does to containers. The universal condition
then tells us that q 0 can be factored into a transformation of fibers h, followed by the
rearrangement of fibers g.
11.2. DEPENDENT TYPES CATEGORICALLY 113
It’s worth noting that picking the singleton set as the pullback target gives us
automatically the definition of the cartesian product:
π2
B×F F
y
π1 !
!
B 1
The arrow g injects this fiber back into E. By varying x we can pick different fibers in
E.
Exercise 11.2.1. Show that the pullback with the terminal object as the target is the
product.
Exercise 11.2.2. Show that a pullback can be defined as a limit of the diagram from a
stick-figure category with three objects:
a→b←c
Exercise 11.2.3. Show that a pullback in C with the target b is a product in the slice
category C/b. Hint: Define two projections as morphisms in the slice category. Use
universality of the pullback to show the universality of the product.
Base-change functor
We used a cartesian closed category as a model for programming. To model dependent
types, we need to impose an additional condition: We require the category to be locally
cartesian closed. This is a category in which all slice categories are cartesian closed.
In particular, such categories have all pullbacks, so it’s always possible to change
the base of any fibration. Base change induces a mapping between slice categories that
is functorial.
Given two slice categories C/b and C/a and an arrow between bases f : b → a the
base-change functor f ∗ : C/a → C/b maps a fibration he, pi to the fibration f ∗ he, pi =
hf ∗ e, f ∗ pi, which is given by the pullback:
g
f ∗e e
y
f ∗p p
f
b a
114 CHAPTER 11. DEPENDENT TYPES
Notice that the functor f ∗ goes in the opposite direction to the arrow f .
To visualize the base-change functor let’s consider how it works on sets. We have
the intuition that the fibration p decomposes the set E into fibers over each point of A.
But here we have another fibration f that similarly decomposes B. Let’s call these
fibers in B “patches.” For instance, if A is just a two-element set, then the fibration
given by f splits B into two patches. The pullback takes a fiber from E and plants it
over the whole patch in B. The resulting set f ∗ E looks like a patchwork, where each
patch is planted with clones of a fiber from E.
f ∗E E
B A
f
Since we have a function from B to A that may map many elements to one, the
fibration over B has finer grain than the coarser fibration over A. The simplest, least-
effort way to turn the fibration of E over A to a fibration over B, is to spread the
existing fibers over the patches defined by (the inverse of) f . This is the essence of the
universal construction of the pullback.
You may also think of A as providing an atlas that enumerates all the patches in
the base B.
In particular, if A is a singleton set (the terminal object), then we have only one
fiber (the whole of E) and the bundle f ∗ E is a cartesian product B × E. Such bundle
is called a trivial bundle.
A non-trivial bundle is not a product, but it can be locally decomposed into products.
Just as B is a sum of patches, so f ∗ E is a sum of products of these patches and the
corresponding fibers of E.
As we’ll see soon, in a locally cartesian closed category, the base change functor has
both the left and the right adjoints. The left adjoint is called the dependent sum, and
the right adjoint is called the dependent product (or dependent function).
set—the terminal object. Here’s the universal construction for the product/pullback
(the notation anticipates the target of this construction):
φ
S
φT
π2
q B×F F
y
π1 !
!
B 1
We have also seen that the product can be defined using an adjunction. We can
spot this adjunction in our diagram: for every pair of arrows hφ, qi there is a unique
arrow φT that makes the triangles commute.
Notice that, if we keep q fixed, we get a one-to-one correspondence between the
arrows φ and φT . This will be the adjunction we’re interested in.
We can now put our fibrational glasses on and notice that hS, qi and hB × F, π1 i are
two fibrations over the same base B. The commuting triangle makes φT a morphism
in the slice category C/B, or a fiber-wise mapping. In other words φT is a member of
the hom-set:
S B×F
(C/B) ,
q π1
Since φ is a member of the hom-set C(S, F ), we can rewrite the one-to-one corre-
spondence between φ and φT as an isomorphism of hom-sets:
∼ S B×F
C(S, F ) = (C/B) ,
q π1
In fact, it’s an adjunction in which the left functor is the forgetful functor L : C/B → C
that maps hS, qi to S, thus forgetting the fibration.
If you squint at this adjunction hard enough, you can see the outlines of the definition
of S as a categorical sum (coproduct).
Firstly, on the left you have a mapping out of S. Think of S as the sum of fibers
that are defined by the fibration hS, qi.
Secondly, recall that the fibration hB × F, π1 i can be though of as producing many
copies of F planted over points in B. Then the right hand side of the adjunction looks
like a bunch of arrows, each mapping a different fiber of S to the same fiber F .
φT
B×F F
B
116 CHAPTER 11. DEPENDENT TYPES
C(F1 + F2 , F ) ∼
= (C × C)(hF1 , F2 i, ∆F )
Seen in this light, a dependent sum is just a sum of many fibers. In Set it’s a tagged
union. Each individual set is a fiber of S under q.
We can generalize our diagram by replacing the terminal object with an arbitrary
base A. We now have a fibration hF, pi, and we get the pullback square that defines
the base-change functor f ∗ :
φ
S
φT
g
q f ∗F F
y p
f ∗p
f
B A
Existential quantification
In the propositions as types interpretation, type families correspond to families of propo-
sitions. The dependent sum type Σx:B T (x) corresponds to the proposition: There exists
an x for which T (x) is true:
∃x:B T (x)
Indeed, a term of the type Σx:B T (x) is a pair of an element x : B and an element
y : T (x)—which shows that T (x) is inhabited for some x.
11.4. DEPENDENT PRODUCT 117
At the time of this writing, Haskell’s support for dependent types is limited, so the
implementation of dependent functions requires the use of singleton types. In this case,
the number that is the argument to replicateV is passed as a singleton natural:
data SNat n where
SZ :: SNat Z
SS :: SNat n -> SNat (S n)
(Note that replicateV is a function of two arguments, so it can be either considered a
dependent function of a pair, or a regular function returning a dependent function.)
εB,C : C B × B → C
ε : S(E) × B → E
We can visualize it as picking a section s in S(E) and an element x of the base B and
producing a value in the bundle E. (In physics, this would correspond to measuring a
field at a particular point in spacetime.)
But this time we have to insist that this value be in the correct fiber. If we project
the result of applying ε to (s, x), we should recover the x.
p−1 x
E
ε(s, x)
x B
π2 p
The difference is that now both ε and φ are now morphisms in the slice category C/B.
11.4. DEPENDENT PRODUCT 119
G×B
E
B B
The adjunction tells us that this family of mappings uniquely determines a function
from G to S(E). Every y ∈ G is thus mapped to a different element s of S(E). Therefore
elements of S(E) are in one-to-one correspondence with sections of E .
These are all set-theoretical intuitions. We can generalize them by first noticing
that the right hand side of the adjunction can be easily expressed as a hom-set in the
slice category C/1 over the terminal object.
Indeed, there is one-to-one correspondence between objects X in C and objects hX, !i
in C/1. Arrows in C/1 are arrows of C with no additional constraints. We therefore
have:
G×B E ∼ G S(E)
(C/B) , = (C/1) ,
π2 p ! !
The next step is to “blur the focus” by replacing the terminal object with a more
general base A.
The right-hand side becomes a hom-set in the slice category C/A. G itself gets
coarsely fibrated by some q : G → A.
Remember that G×B can be understood as a pullback along the mapping ! : B → 1,
or a change of base from 1 to B. If we want to replace 1 with A, we should replace
the product G × B with a more general pullback of q. Such a change of base can be
parameterized by a new morphism f : B → A.
π1 g
G×B G f ∗G G
y y q
π2 ! f ∗q
! f
B 1 B A
The result is that, instead of a bunch of G fibers over B, we get a pullback f ∗ G
that is populated by groups of fibers from the fibration q : G → A. This way A serves
as an atlas that enumerates all the patches populated by uniform fibers.
Imagine, for instance, that A is a two-element set. The fibration q will split G into
two fibers. These will serve as our generic fibers. These fibers are now replanted over
the two patches in B to form f ∗ G. The replanting is guided by f −1 .
120 CHAPTER 11. DEPENDENT TYPES
G f ∗G E
A B B
f
φ φT
f ∗G E G Πf E
f ∗q p q
Πf p
B A
To gain some intuition into this adjunction, let’s consider how it works on sets.
• The right hand side operates in a coarsely grained fibration over the atlas A. It is
a family of functions, one function per patch. For every patch we get a function
from the “thick fiber” of G (drawn in blue below) to the “thick fiber” of Πf E
(not shown).
• The left hand side operates in a more finely grained fibration over B. These fibers
are grouped into small bundles over patches. Once we pick a patch (drawn in red
below), we get a family of functions from that patch to the corresponding patch
in E (drawn in green)—a section of a small bundle in E. So, patch-by-patch, we
get small sections of E.
The adjunction tells us that the elements of the “thick fiber” of Πf E correspond to
small sections of E over the same patch.
E G
B p q
f A
11.5. EQUALITY 121
The following exercises shed some light on the role played by f . It can be seen as
localizing the sections of E by restricting them to “neighborhoods” defined by f −1 .
Exercise 11.4.1. Consider what happens when A is a two-element set {0, 1} and f
maps the whole of B to one element, say 1. How would you define the function on the
right-hand side of the adjunction? What should it do to the fiber over 0?
• f ∗ 1 has two types of fibers: singletons over the elements of f −1 (x) and empty sets
otherwise.
Universal quantification
The logical interpretation of the dependent product Πx:B T (x) is a universally quaniti-
fied proposition. An element of Πx:B T (x) is a section—the proof that it’s possible to
select an element from each member of the family T (x). It means that none of them is
empty. In other words, it’s a proof of the proposition:
∀x:B T (x)
11.5 Equality
Our first experience in mathematics involves equality. We learn that
1+1=2
Equational reasoning
Notice that, when defining equality in Haskell, we were already using the equal sign.
For instance, the equal sign in:
equal Z Z = True
tells us that wherever we see the expression equal Z Z we can replace it with True and
vice versa.
This is the principle of substituting equals for equals, which is the basis for equational
reasoning in Haskell. We can’t encode proofs of equality directly in Haskell, but we can
use equational reasoning to reason about Haskell programs. This is one of the main
advantages of pure functional programming. You can’t perform such substitutions in
imperative languages, because of side effects.
If we want to prove that 1 + 1 is 2, we have to first define addition. The definition
can either be recursive in the first or in the second argument. This one recurses in the
second argument:
add :: Nat -> Nat -> Nat
add n Z = n
add n (S m) = S (add n m)
We encode 1 + 1 as:
add (S Z) (S Z)
We can now use the definition of add to simplify this expression. We try to match
the first clause, and we fail, because S Z is not the same as Z. But the second clause
matches. In it, n is an arbitrary number, so we can substitute S Z for it, and get:
add (S Z) (S Z) = S (add (S Z) Z)
In this expression we can perform another substitution of equals using the first clause
of the definition of add (again, with n replaced by S Z):
add (S Z) Z = (S Z)
We arrive at:
add (S Z) (S Z) = S (S Z)
We can clearly see that the right-hand side is the encoding of 2. But we haven’t shown
that our definition of equality is reflexive so, in principle, we don’t know if:
eq (S (S Z)) (S (S Z))
yields True. We have to use step-by-step equational reasoning again:
11.5. EQUALITY 123
equal (S (S Z) (S (S Z)) =
{- second clause of the definition of equal -}
equal (S Z) (S Z) =
{- second clause of the definition of equal -}
equal Z Z =
{- first clause of the definition of equal -}
True
We can use this kind of reasoning to prove statements about concrete numbers, but
we run into problems when reasoning about generic numbers—for instance, showing
that something is true for all n. Using our definition of addition, we can easily show
that add n Z is the same as n. But we can’t prove that add Z n is the same as n. The
latter proof requires the use of induction.
We end up distinguishing between two kinds of equality. One is proven using sub-
stitutions, or rewriting rules, and is called definitional equality. You can think of it
as macro expansion or inline expansion in programming languages. It also involves β-
reductions: performing function application by replacing formal parameters by actual
arguments, as in:
(\x -> x + x) 2 =
{- beta reduction -}
2 + 2
The second more interesting kind of equality is called propositional equality and it
may require actual proofs.
Equality vs isomorphism
We said that category theorists prefer isomorphism over equality—at least when it
comes to objects. It is true that, within the confines of a category, there is no way
to differentiate between isomorphic objects. In general, though, equality is stronger
than isomorphism. This is a problem, because it’s very convenient to be able to substi-
tute equals for equals, but it’s not always clear that one can substitute isomorphic for
isomorphic.
Mathematicians have been struggling with this problem, mostly trying to modify the
definition of isomorphism—but a real breakthrough came when they decided to simul-
taneously weaken the definition of equality. This led to the development of homotopy
type theory, or HoTT for short.
Roughly speaking, in type theory, specifically in Martin-Löf theory of dependent
types, equality is represented as a type, and in order to prove equality one has to
construct an element of that type—in the spirit of the Curry-Howard interpretation.
Furthermore, in HoTT, the proofs themselves can be compared for equality, and so
on ad infinitum. You can picture this by considering proofs of equality not as points
but as some abstract paths that can be morphed into each other; hence the language
of homotopies.
In this setting, instead of isomorphism, which involves strict equalities of arrows:
f ◦ g = id
g ◦ f = id
124 CHAPTER 11. DEPENDENT TYPES
(A = B) ∼
= (A ∼
= B)
Notice that this is an axiom, not a theorem. We can either take it or leave it and the
theory is still valid (at least we think so).
Equality types
Suppose that you want to compare two terms for equality. The first requirement is
that both terms be of the same type. You can’t compare apples with oranges. Don’t
get confused by some programming languages allowing comparisons of unlike terms: in
every such case there is an implicit conversion involved, and the final equality is always
between same-type values.
For every pair of values there is, in principle, a separate type of proofs of equality.
There is a type for 0 = 0, there is a type for 1 = 1, and there is a type for 1 = 0; the
latter hopefully uninhabited.
Equality type, a.k.a., identity type, is therefore a dependent type: it depends on
the two values that we are comparing. It’s usually written as Id A , where A is the type
of both values, or using an infix notation as x =A y (equal sign with the subscript A).
For instance, the type of equality of two zeros is written as Id N (0, 0) or:
0 =N 0
Notice: this is not a statement or a term. It’s a type, like Int or Bool. You can define
a value of this type if you have an introduction rule for it.
Introduction rule
The introduction rule for the equality type is the dependent function:
which can be interpreted in the spirit of propositions as types as the proof of the
statement:
∀x:A x = x
This is the familiar reflexivity: it shows that, for all x of type A, x is equal to itself.
You can apply this function to some concrete value x of type A, and it will produce a
new value of type Id A (x, x).
We can now prove that 0 = 0. We can execute refl N (0) to get a value of the type
0 =N 0. This value is the proof that the type is inhabited, and therefore corresponds to
a true proposition.
This is the only introduction rule for equality, so you might think that all proofs of
equality boil down to “they are equal because they are the same.” Surprisingly, this is
not the case.
11.5. EQUALITY 125
init : T (Z)
126 CHAPTER 11. DEPENDENT TYPES
or, equivalently, the proof of the zeroth proposition; and a dependent function:
This function is interpreted as generating a proof of the (n + 1)st proposition from the
proof of the nth proposition.
The dependent elimination rule for natural numbers tells us that, given such init
and step, there exists a dependent function:
f : Πn:N T (n)
This function is interpreted as providing the proof that T (n) is true for all n.
Moreover, this function, when applied to zero reproduces init:
f (Z) = init
(Here, step(n) produces a function, which is then applied to the value f (n).) These are
the two computation rules for natural numbers.
Notice that the induction principle is not a theorem about natural numbers. It’s
part of the definition of the type of natural numbers.
Not all dependent mappings out of natural numbers can be decomposed into init
and step, just as not all theorems about natural numbers can be proven inductively.
There is no η-conversion rule for natural numbers.
T (x, y, p)
that depends on the pair of values that are being compared x, y : A and the proof of
equality p : Id (x, y).
The function we are trying to construct is:
It’s convenient to think of it as generating a proof that for all points x and y, and
for every proof that the two are equal, the proposition T (x, y, p) is true. Notice that,
potentially, we have a different proposition for every proof that the two points are equal.
11.5. EQUALITY 127
The least that we can demand from T (x, y, p) is that it should be true when x and
y are literally the same, and the equality proof is the obvious refl . This requirement
can be expressed as a dependent function:
Notice that we are not even considering proofs of x = x, other than those given by
reflexivity. Do such proofs exist? We don’t know and we don’t care.
So this is our grounding, the starting point of a journey that should lead us to
defining our f for all pairs of points and all proofs of equality. The intuition is that we
are defining f as a function on a plane (x, y), with a third dimension given by p. To do
that, we’re given something that’s defined on the diagonal (x, x), with p restricted to
refl .
p : Id (x, y)
y
refl
You would think that we need something more, some kind of a step that would
move us from one point to another. But, unlike with natural numbers, there is no next
point or next equality proof to jump to. All we have at our disposal is the function t
and nothing else.
Therefore we postulate that, given a type family T (x, y, p) and a function:
Notice that the equality in the computation rule is definitional equality, not a type.
Equality elimination tells us that it’s alway possible to extend the function t, which
is defined on the diagonal, to the whole 3-d space.
This is a very strong postulate. One way to understand it is to argue that, within
the framework of type theory—which is formulated using the language of introduction
and elimination rules, and the rules for manipulating those—it’s impossible to define a
type family T (x, y, p) that would not satisfy the equality elimination rule.
The closest analogy that we’ve seen so far is the result of parametricity, which states
that, in Haskell, all polymorphic functions between endofunctors are automatically
128 CHAPTER 11. DEPENDENT TYPES
natural transformations. Another example, this time from calculus, is that any analytic
function defined on the real axis has a unique extension to the whole complex plane.
The use of dependent types blurs the boundary between programming and math-
ematics. There is a whole spectrum of languages, with Haskell barely dipping its toes
in dependent types while still firmly established in commercial usage; all the way to
theorem provers, which are helping mathematicians formalize mathematical proofs.
Chapter 12
Algebras
It’s a recipe for building trees. We start with little trees using the Val constructor. We
then plant these seedlings into nodes, and so on.
e2 = Val 2
e3 = Val 3
e5 = Plus e2 e3
e7 = Plus e5 e2
129
130 CHAPTER 12. ALGEBRAS
Ff
Fa Fb
α β
f
a b
f ◦ α = β ◦ Ff
The composition of two algebra morphisms is again an algebra morphism, which can
be seen by pasting together two such diagrams. The identity arrow is also an algebra
morphism, because
ida ◦ α = α ◦ F (ida )
(a functor maps identity to identity).
The commuting condition in the definition of an algebra morphism is very restrictive.
Consider for instance a function that maps an integer to a string. In Haskell there is a
show function (actually, a method of the Show class) that does it. It is not an algebra
morphism from eval to pretty.
Exercise 12.2.1. Show that show is not an algebra morphism. Hint: Consider what
happens to the PlusF node.
Initial algebra
The initial object in the category of algebras is called the initial algebra and, as we’ll
see, it plays a very important role.
By definition, the initial algebra (i, ι) has a unique algebra morphism f from it to
any other algebra (a, α). Diagrammatically:
132 CHAPTER 12. ALGEBRAS
Ff
Fi Fa
ι α
f
i a
This unique morphism is called a catamorphism for the algebra (a, α).
Exercise 12.2.2. Let’s define two algebras for the following functor:
data FloatF x = Num Float | Op x x
The first algebra:
addAlg :: Algebra FloatF Float
addAlg (Num x) = log x
addAlg (Op x y) = x + y
The second algebra:
mulAlg :: Algebra FloatF Float
mulAlg (Num x) = x
mulAlg (Op x y) = x * y
Make a convincing argument that log (logarithm) is an algebra morphism between these
two. (Float is a built-in floating-point number type.)
Fh
Fi F (F i)
ι Fι
h
i Fi
This h is the inverse of ι. To see that, let’s consider the composition ι ◦ h. It is the
arrow at the bottom of the following diagram
Fh Fι
Fi F (F i) Fi
ι Fι ι
h ι
i Fi i
This is a pasting of the original diagram with a trivially commuting diagram. Therefore
the whole rectangle commutes. We can interpret this as ι◦h being an algebra morphism
12.3. LAMBEK’S LEMMA AND FIXED POINTS 133
from (i, ι) to itself. But there already is such an algebra morphism—the identity. So,
by uniqueness of the mapping out from the initial algebra, these two must be equal:
ι ◦ h = idi
Knowing that, we can now go back to the previous diagram, which states that:
h ◦ ι = Fι ◦ Fh
F (ι ◦ h) = F (idi ) = idF i
We have thus shown that h is the inverse of ι, which means that ι is an isomorphism.
In other words:
Fi ∼=i
We interpret this identity as stating that i is a fixed point of F (up to isomorphism).
The action of F on i “doesn’t change it.”
There may be many fixed points, but this one is the least fixed point because there
is an algebra morphism from it to any other fixed point. The least fixed point of an
endofunctor F is denoted µF , so we have:
i = µF
12.4 Catamorphisms
Our goal, as programmers, is to be able to perform a computation over a recursive data
structure—to “fold” it. We now have all the ingredients.
The data structure is defined as a fixed point of a functor. An algebra for this
functor defines the operation we want to perform. We’ve seen the fixed point and the
algebra combined in the following diagram:
Ff
Fi Fa
ι α
f
i a
that defines the catamorphism f for the algebra (a, α).
The final piece of information is the Lambek’s lemma, which tells us that ι could
be inverted because it’s an isomorphism. It means that we can read this diagram as:
f = α ◦ F f ◦ ι−1
and interpret it as a recursive definition of f .
Let’s redraw this diagram using Haskell notation. The catamorphism depends on
the algebra so, for the algebra with the carrier a and the evaluator alg, we’ll have the
catamorphism cata alg.
Examples
We can immediately apply this construction to our earlier examples. You can check
that:
cata eval e9
evaluates to 9 and
cata pretty e9
evaluates to the string "2 + 3 + 4".
Sometimes we want to display the tree on multiple lines with indentation. This
requires passing a depth counter to recursive calls. There is a clever trick that uses a
function type as a carrier:
pretty' :: Algebra ExprF (Int -> String)
pretty' (ValF n) i = indent i ++ show n
pretty' (PlusF f g) i = f (i + 1) ++ "\n" ++
indent i ++ "+" ++ "\n" ++
g (i + 1)
The auxiliary function indent replicates the space character:
indent n = replicate (n * 2) ' '
The result of:
cata pretty' e9 0
when printed, looks like this:
2
+
3
+
4
136 CHAPTER 12. ALGEBRAS
Let’s try defining algebras for other familiar functors. The fixed point of the Maybe
functor:
data Maybe x = Nothing | Just x
after some renaming, is equivalent to the type of natural numbers
data Nat = Z | S Nat
An algebra for this functor consists of a choice of the carrier a and an evaluator:
alg :: Maybe a -> a
The mapping out of Maybe is determined by two things: the value corresponding to
Nothing and a function a->a corresponding to Just. In our discussion of the type of
natural numbers we called these init and step. We can now see that the elimination
rule for Nat is the catamorphism for this algebra.
of such closures. To reverse a list, we apply the result of the catamorphism for this
algebra to an empty list:
reverse :: Fix (ListF a) -> [a]
reverse as = (cata revAlg as) []
This trick is at the core of the fold-left funcion, foldl. Care should be taken when
using it, because of the danger of stack overflow.
Lists are so common that their eliminators (called “folds”) are included in the stan-
dard library. But there are infinitely many possible recursive data structures, each
generated by its own functor, and we can use the same catamorphism on all of them.
It’s worth mentioning that the list construction works in any monoidal category
with coproducts. We can replace the list functor with a more general:
Fx = I + a ⊗ x
where I is the unit object and ⊗ is the tensor product. The solution to the fixed point
equation:
La ∼= I + a ⊗ La
can be formally written as a series:
La = I + a + a ⊗ a + a ⊗ a ⊗ a + ...
La ∼
= I + a ⊗ La
∼
La − a ⊗ La = I
(I − a) ⊗ La ∼
=I
La ∼
= I/(I − a)
La ∼
= I + a + a ⊗ a + a ⊗ a ⊗ a + ...
where the last step uses the formula for the sum of geometric series. Admittedly, the
intermediate steps make no sense, since there is no subtraction or division defined on
objects, yet the final result make sense, and it may be made rigorous by considering a
colimit of a chain of objects.
Exercise 12.5.1. Write a test that takes a list of integers, converts it to the Mu form,
and calculates the sum using cataMu.
these algebras. The construction of this fixed point is a little mysterious, since it involves
tying the recursive knot.
Loosely speaking, the fixed point is reached after we apply the functor infinitely
many times. Then, applying it once more wouldn’t change anything. This idea can be
made precise, if we take it one step at a time. For simplicity, let’s consider algebras in
the category of sets, which has all the nice properties.
We’ve seen, in our examples, that building instances of recursive data structures
always starts with the leaves. The leaves are the parts in the definition of the functor
that don’t depend on the type parameter: the NilF of the list, the ValF of the tree, the
Nothing of the Maybe, etc.
We can tease them out if we apply our functor F to the initial object—the empty
set 0. Since the empty set has no elements, the instances of the type F 0 are leaves only.
Indeed, the only inhabitant of the type Maybe Void is constructed using Nothing.
The only inhabitants of the type ExprF Void are ValF n, where n is an Int.
In other words, F 0 is the “type of leaves” for the functor F . Leaves are trees of
depth one. For the Maybe functor there’s only one—the type of leaves for this functor
is a singleton:
m1 :: Maybe Void
m1 = Nothing
In the second iteration, we apply F to the leaves from the previous step and get
trees of depth at most two. Their type is F (F 0).
For instance, these are all the terms of the type Maybe(Maybe Void):
m2, m2' :: Maybe (Maybe Void)
m2 = Nothing
m2' = Just Nothing
We can continue this process, adding deeper and deeper trees at each step. In the
n-th iteration, the type F n 0 (n-fold application of F to the initial object) describes all
trees of depth up to n. However, for every n, there are still infinitely many trees of
depth greater than n that are not covered.
If we knew how to define F ∞ 0, it would cover all possible trees. The next best thing
we could try is to gather all these partial trees into one infinite sum type. Just like we
have defined sums of two types, we can define sums of many types, including infinitely
many.
An infinite sum (a coproduct):
∞
a
F n0
n=0
is just like a finite sum, except that it has infinitely many constructors in :
0 F0 F (F 0) ... F n0 ...
i1
i0 i2
in
`∞ n0
n=0 F
It has the universal mapping-out property, just like the sum of two types, only with
infinitely many cases. (Obviously, we can’t express it in Haskell.)
140 CHAPTER 12. ALGEBRAS
To construct a tree of depth n, we would first select it from F n 0 and use the n-th
constructor in to inject it into the sum.
There is only one problem: the same tree shape can also be constructed using any
of the F m 0, with m > n.
Indeed, we’ve seen the leaf Nothing appear in Maybe Void and Maybe(Maybe Void).
In fact it shows up in any nonzero power of Maybe acting on Void.
Similarly, Just Nothing shows up in all powers starting with two.
Just(Just(Nothing)) shows up in all powers starting with three, and so on...
But there is a way to get rid of all these duplicates. The trick is to replace the sum
by the colimit. Instead of a diagram consisting of discrete objects, we can construct a
chain. Let’s call this chain Γ, and its colimit i = Colim Γ.
¡ F¡ F (F ¡)
0 F0 F (F 0) ... F n0 ...
i1
i2
i0 in
i
It’s almost the same as the sum, but with additional arrows at the base of the cocone.
These arrows are the liftings of the unique arrow ¡ that goes from the initial object to
F 0 (we called it absurd in Haskell). The effect of these arrows is to collapse the set of
infinitely many copies of the same tree down to just one representative.
To see that, consider for instance a tree of depth n. It can be first found as an
element of F n 0, that is to say, as an arrow t : 1 → F n 0. It is injected into the colimit i
as the composite in ◦ t.
... 1
t0
t
F n (¡)
... F n0 F n+1 0
in
in+1
i
The same shape of a tree is also found in F n+1 0, as the composite t0 = F n (¡) ◦ t. It is
injected into the colimit as the composite in+1 ◦ t0 = in+1 ◦ F n (¡) ◦ t.
This time, however, we have the commuting triangle as the face of the cocone:
in+1 ◦ F n (¡) = in
Colim(F Γ) ∼
= Fi
12.6. INITIAL ALGEBRA AS A COLIMIT 141
F¡ F (F ¡) F 3¡
F0 F (F 0) F 30 ... F n0 ...
j2
j3
j1 jn
Colim(F Γ)
The diagram F Γ is the same as Γ, except that it’s missing the naked initial object at
the start of the chain.
The spokes of the cocone we are looking for are marked in red in the diagram below:
¡ F¡ F (F ¡)
0 F0 F (F 0) ... F n+1 0 ...
¡ F¡ F (F ¡)
F¡ F (F ¡) F 3¡
F0 F (F 0) F 30 ... F n0 ...
j2
j3
j1 jn
Colim(F Γ)
And since i = ColimΓ is the apex of the universal cocone based on Γ, there must be a
unique mapping out of it to Colim(F Γ) which, as we said, was equal to F i:
i → Fi
F¡ F (F ¡)
F0 F (F 0) ... F n0 ...
¡ F¡ F 2¡
0 F0 F 20 ... F n0 ...
i1
i2
i0 in
i
142 CHAPTER 12. ALGEBRAS
Colim(F Γ) ∼
= Fi → i
This shows that i is a carrier of an algebra. In fact, it can be shown that the two
mappings are the inverse of each other, as we would expect from the Lambek’s lemma.
To show that this is indeed the initial algebra, we have to construct a mapping out
of it to an arbitrary algebra (a, α : F a → a). Again, we can use universality, if we can
construct a cocone from Γ to a.
¡ F¡ F (F ¡)
0 F0 F (F 0) ... F n0 ...
f1
f2
f0 fn
a
The zero’th spoke of this cocone goes from 0 to a, so it’s just f0 = ¡.
The first one, F 0 → a, is f1 = α ◦ F f0 , because F f0 : F 0 → F a.
The third one, F (F 0) → a is f2 = α ◦ F f1 . And so on...
The unique mapping from i to a is then our catamorphism. With some more diagram
chasing, it can be shown that it’s indeed an algebra morphism.
Notice that this construction only works if we can “prime” the process by creating
the leaves of the functor. If, on the other hand, F 0 ∼ = 0, then there are no leaves, and
all further iterations will just reproduce the 0.
Chapter 13
Coalgebras
143
144 CHAPTER 13. COALGEBRAS
f
a b
α β τ
Ff
Fa Fb
The interpretation is that it doesn’t matter if we first map the carriers and then
apply the coalgebra β, or first apply the coalgebra α and then apply the arrow to its
contents, using the lifting F f .
Coalgebra morphisms can be composed, and the identity arrow is automaticaly a
coalgebra morphism. It’s easy to see that coalgebras, just like algebras, form a category.
This time, however, we are interested in the terminal object in this category—a
terminal coalgebra. If a terminal coalgebra (t, τ ) exists, it satisfies the dual of the
Lambek’s lemma.
Exercise 13.2.1. Lambek’s lemma: Show that the structure map τ of the terminal
coalgebra (t, τ ) is an isomorphism. Hint: The proof is dual to the one for the initial
algebra.
13.2. CATEGORY OF COALGEBRAS 145
Ft ∼
=t
t = νF
We can also see that there must be a unique algebra morphism (a catamorphism)
from the initial algebra to the terminal coalgebra. That’s because the terminal coalgebra
is an algebra.
Similarly, there is a unique coalgebra morphism from the initial algebra (which is
also a coalgebra) to the terminal coalgebra. In fact, it can be shown that it’s the same
underlying morphism τ : µF → νF in both cases.
In the category of sets, the carrier set of the initial algebra is a subset of the carrier
set of the terminal coalgebra, with the function τ embedding the former in the latter.
νF
τ
µF
We’ll see later that in Haskell the situation is more subtle, because of lazy evaluation.
But, at least for functors that have the leaf component—that is, their action on the
initial object is non-trivial—Haskell’s fixed point type works as a carrier for both the
initial algebra and the terminal coalgebra.
data Fix f where
In :: f (Fix f) -> Fix f
Exercise 13.2.2. Show that, for the identity functor in Set, every object is a fixed
point, the empty set is the least fixed point, and the singleton set is the greatest fixed
point. Hint: The least fixed point must have arrows going to all other fixed points, and
the greatest fixed point must have arrows coming from all other fixed points.
Exercise 13.2.3. Show that the empty set is the carrier of the initial algebra for the
identity functor in Set. Dually, show that the singleton set is this functor’s terminal
coalgebra. Hint: Show that the unique arrows are indeed (co-) algebra morphisms.
146 CHAPTER 13. COALGEBRAS
13.3 Anamorphisms
The terminal coalgebra (t, τ ) is defined by its universal property: there is a unique
coalgebra morphism h from any coalgebra (a, α) to (t, τ ). This morphism is called the
anamorphism. Being a coalgebra morphism, it makes the following diagram commute:
h
a t
α τ
Fh
Fa Ft
Just like with algebras, we can use the Lambek’s lemma to “solve” for h:
h = τ −1 ◦ F h ◦ α
Since the terminal coalgebra (just like the initial algebra) is a fixed point of a functor,
the above recursive formula can be translated directly to Haskell as:
ana :: Functor f => Coalgebra f a -> a -> Fix f
ana coa = In . fmap (ana coa) . coa
Here’s the interpretation of this formula: Given a seed of type a, we first act on it
with the coalgebra coa. This gives us a functorful of seeds. We expand these seeds by
recursively applying the anamorphism using fmap. We then apply the constructor In
to get the final result.
As an example, we can apply the anamorphism to the split coalgebra we defined
earlier: ana split takes a list of integers and creates a sorted tree.
We can then use a catamorphsims to fold this tree into a sorted list. We define an
algebra:
toList :: Algebra TreeF [Int]
toList LeafF = []
toList (NodeF n ns ms) = ns ++ [n] ++ ms
that concatenates the left list with the singleton pivot and the right list. To sort a list
we combine the anamorphism with the catamorphism:
qsort = cata toList . ana split
This gives us a (very inefficient) implementation of quicksort. We’ll come back to it in
the next section.
Infinite data structures are representable in Haskell because of its laziness. Things
are evaluated on the need-to-know basis. Only those parts of an infinite data struc-
ture that are explicitly demanded are calculated; the evaluation of the rest is kept in
suspended animation.
To implement infinite data structures in strict languages, one must resort to repre-
senting values as functions—something Haskell does behind the scenes (these functions
are called thunks).
Let’s look at a simple example: an infinite stream of values. To generate it, we first
define a functor that looks very much like the one we used to generate lists, except
that it lacks the leaf component—the empty list constructor. You may recognize it as
a product functor, with the first component fixed—it describes the stream’s payload:
data StreamF a x = StreamF a x
deriving Functor
An infinite stream is the fixed point of this functor.
type Stream a = Fix (StreamF a)
Here’s a simple coalgebra that uses a single integer n as a seed:
step :: Coalgebra (StreamF Int) Int
step n = StreamF n (n+1)
It stores the seed as a payload, and seeds the next budding stream with n + 1.
The anamorphism for this coalgebra, seeded with zero, generates the stream of all
natural numbers.
allNats :: Stream Int
allNats = ana step 0
In a non-lazy language this anamorphism would run forever, but in Haskell it’s instan-
taneous. The incremental price is paid only when we want to retrive some of the data,
for instance, using these accessors:
head :: Stream a -> a
head (In (StreamF a _)) = a
13.4 Hylomorphisms
The type of the output of an anamorphism is a fixed point of a functor, which is the
same type as the input to a catamorphism. In Haskell, they are both described by the
same data type, Fix f. Therefore it’s possible to compose them together, as we’ve done
when implementing quicksort. In fact, we can combine a coalgebra with an algebra in
one recursive function called a hylomorphism:
hylo :: Functor f => Algebra f b -> Coalgebra f a -> a -> b
hylo alg coa = alg . fmap (hylo alg coa) . coa
We can rewrite quicksort as a hylomorphism:
148 CHAPTER 13. COALGEBRAS
bottom of the type Int. In general, catamorphisms for infinite types don’t terminate,
so we can treat them as returning bottoms.
It should be noted, however, that the inclusion of bottoms complicates the categor-
ical interpretation of Haskell. In particular, many of the universal constructions that
rely on uniqueness of mappings no longer work as advertised.
The “bottom” line is that Haskell code should be treated as an illustration of cate-
gorical concepts rather than a source of rigorous proofs.
There are several ways of defining existential data types in Haskell. We can use the
uncurried version of the anamorphism directly as the data constructor:
data Nu f where
Nu :: forall a f. (a -> f a, a) -> Nu f
Notice that, in Haskell, if we explicitly quantify one type, all other type variables must
also be quantified: here, it’s the type constructor f (however, Nu f is not existential in
f, since it’s an explicit parameter).
We can also omit the quantification altogether:
data Nu f where
Nu :: (a -> f a, a) -> Nu f
This is because type variables that are not arguments to the type constructor are
automatically treated as existentials.
We can also use the more traditional form:
data Nu f = forall a. Nu (a -> f a, a)
(This one requires the quantification of a.)
Finally, although this syntax is not allowed in Haskell, it’s often more intuitive to
write it like this:
data Nu f = Nu (exists a. (a -> f a, a))
(Later we’ll see that existential data types correspond to coends in category theory.)
The constructor of Nu f is literally the (uncurried) anamorphism:
anaNu :: Coalgebra f a -> a -> Nu f
anaNu coa a = Nu (coa, a)
If we are given a stream in the form of Nu (Stream a), we can access its element
using accessors functions. This one extracts the first element:
head :: Nu (StreamF a) -> a
head (Nu (unf, s)) =
let (StreamF a _) = unf s
in a
and this one advances the stream:
tail :: Nu (StreamF a) -> Nu (StreamF a)
tail (Nu (unf, s)) =
let (StreamF _ s') = unf s
in Nu (unf, s')
You can test them on an infinite stream of integers:
allNats = Nu nuArgs
F (F ∞ 1) ∼
= F ∞+1 1 ∼
= F ∞1
To turn this loose reasoning into a rigorous proof, we have to tame the infinity,
which means we have to define some kind of a limiting procedure.
As an example, let’s consider the product functor:
Fa x = a × x
Fa 1 = a × 1 ∼
=a
Fa (Fa 1) = a × (a × 1) ∼
=a×a
1 !
F1
(! is the unique morphism targeting the terminal object 1). This limit has the same
elements as F 1. Similarly, this limit:
LimD2
π0 π2
π1
1 !
F1 F!
F (F 1)
t
π0 πn
π2
π1
1 !
F1 F!
F (F 1) ... F n1 F n!
...
F (F !)
The proof of this fact can be obtained from the analogous proof for initial algebras by
reversing the arrows.
Chapter 14
Monads
What do a wheel, a clay pot, and a wooden house have in common? They are all useful
because of the emptiness in their center.
Lao Tzu says: “The value comes from what is there, but the use comes from what
is not there.”
What does the Maybe functor, the list functor, and the reader functor have in com-
mon? They all have emptiness in their center.
When monads are explained in the context of programming, it’s hard to see the
common pattern when you focus on the functors. To understand monads you have to
look inside functors and in between functions.
153
154 CHAPTER 14. MONADS
this point there are no conditions imposed on f. It doesn’t even have to be a Functor,
much less a monad. This will come later, when we talk about effect composition.
Below is the list of common effects and their pure-function versions.
Partiality
In imperative languages, partiality is often encoded using exceptions. When a function
is called with the “wrong” value for its argument, it throws an exception. In some
languages, the type of exception is encoded in the signature of the function using special
syntax.
In Haskell, a partial computation can be implemented by a function returning the
result inside the Maybe functor. Such a function, when called with the “wrong” argu-
ment, returns Nothing, otherwise is wraps the result in the Just constructor.
If we want to encode more information about the type of the failure, we can use
the Either functor, with the Left traditionally passing the error data (often a simple
String); and Right encapsulating the real return, if available.
The caller of a Maybe-valued function cannot easily ignore the exceptional condition.
In order to extract the value, they have to pattern-match the result and decide how to
deal with Nothing. This is in contrast to the “poor-man’s Maybe” of some imperative
languages where the error condition is encoded using a null pointer.
Logging
Sometimes a computation has to log some values in some external data structure.
Logging or auditing is a side effect that’s particularly dangerous in concurrent programs,
where multiple threads might try to access the same log simultaneously.
The simple solution is for a function to return the computed value paired with the
item to be logged. In other words, a logging computation of the type a -> b can be
replaced by a pure function:
a -> Writer w b
where the Writer functor is a thin encapsulation of the product:
newtype Writer w a = Writer (a, w)
with w being the type of the log.
The caller of this function is then responsible for extracting the value to be logged.
This is a common trick: make the function provide all the data, and let the caller deal
with the effects.
Environment
Some computations need read-only access to some external data stored in the environ-
ment. The read-only environment, instead of being secretly accessed by a computation,
can be simply passed to a function as an additional argument. If we have a computa-
tion a -> b that needs access to some environment e, we replace it with a function
(a, e) -> b. At first, this doesn’t seem to fit the pattern of encoding side effects in
the return type. However, such a function can always be curried to the form:
a -> (e -> b)
14.1. PROGRAMMING WITH SIDE EFFECTS 155
The return type of this function can be encoded in the reader functor, itself parame-
terized by the environment type e:
newtype Reader e a = Reader (e -> a)
This is an example of a delayed side effect. The function:
a -> Reader e a
doesn’t want to deal with effects so it delegates the responsibility to the caller. You
may think of it as producing a script to be executed at a later time. The function
runReader plays the role of a very simple interpreter of this script:
runReader :: Reader e a -> e -> a
runReader (Reader h) e = h e
State
The most common side effect is related to accessing and potentially modifying some
shared state. Unfortunately, shared state is the notorious source of concurrency errors.
This is a serious problem in object oriented languages where stateful objects can be
transparently shared between many clients. In Java, such objects may be provided
with individual mutexes at the cost of impaired performance and the risk of deadlocks.
In functional programming we make state manipulations explicit: we pass the state
as an additional argument and return the modified state paired with the return value.
We replace a stateful computation a -> b with
(a, s) -> (b, s)
where s is the type of state. As before, we can curry such a function to get it to the
form:
a -> (s -> (b, s))
This return type can be encapsulated in the following functor:
newtype State s a = State (s -> (a, s))
The caller of such a function is supposed to retrieve the result and the modified state
by providing the initial state and calling the helper function, the interpreter, runState:
runState :: State s a -> s -> (a, s)
runState (State h) s = h s
Notice that, modulo constructor unpacking, runState is bona fide function application.
Nondeterminism
Imagine performing a quantum experiment that measures the spin of an electron. Half
of the time the spin will be up, and half of the time it will be down. The result is
non-deterministic. One way to describe it is to use the many-worlds interpretation:
when we perform the experiment, the Universe splits into two universes, one for each
result.
What does it mean for a function to be non-deterministic? It means that it will
return different results every time it’s called. We can model this behavior using the
many-worlds interpretation: we let the function return all possible results at once. In
practice, we’ll settle for a (possibly infinite) list of results:
156 CHAPTER 14. MONADS
Input/Output
This is the trickiest side effect because it involves interacting with the external world.
Obviously, we cannot model the whole world inside a computer program. So, in order
to keep the program pure, the interaction has to happen outside of it. The trick is
to let the program generate a script. This script is then passed to the runtime to be
executed. The runtime is the effectful virtual machine that runs the program.
This script itself sits inside the opaque, predefined IO functor. The values hidden in
this functor are not accessible to the program: there is no runIO function. Instead, the
IO value produced by the program is executed, at least conceptually, after the program
is finished.
In reality, because of Haskell’s laziness, the execution of I/O is interleaved with
the rest of the program. Pure functions that comprise the bulk of your program are
evaluated on demand—the demand being driven by the execution of the IO script. If
it weren’t for I/O, nothing would ever be evaluated.
The IO object that is produced by a Haskell program is called main and its type
signature is:
main :: IO ()
It’s the IO functor containing the unit—meaning: there is no useful value other than
the input/output script.
We’ll talk about how IO actions are created soon.
Continuation
We’ve seen that, as a consequences of the Yoneda lemma, we can replace a value of
type a with a function that takes a handler for that value. This handler is called a
continuation. Calling a handler is considered a side effect of a computation. In terms
of pure functions, we encode it as:
a -> Cont r b
where Cont r is the following functor:
newtype Cont r a = Cont ((a -> r) -> r)
It’s the responsibility of the caller of this function to provide the continuation and
retrieve the result:
runCont :: Cont r a -> (a -> r) -> r
runCont (Cont f) k = f k
This is the Functor instance for Cont r:
instance Functor (Cont r) where
-- f :: a -> b
-- k :: b -> r
fmap f c = Cont (\k -> runCont c (k . f))
14.2. COMPOSING EFFECTS 157
Notice that this is a covariant functor because the type a is in a doubly negative position.
First, let’s notice that, when implementing the fish operator, we are given two
functions as arguments. The only thing a function is useful for is to be applied to an
argument. When we apply the first function f :: a -> m b we get a value of the
type m b. At this point we would be stuck, if it weren’t for the fact that m is a functor.
Functoriality lets us apply the second function g :: b -> m c to m b. Indeed the
lifting of g by m is of the type:
m b -> m (m c)
This is almost the result we are looking for, if we could only flatten m(m c) to m c.
This flattening is called join. In other words, if we are given:
join :: m (m a) -> m a
we can implement <=<:
g <=< f = \a -> join (fmap g (f a))
or, using point free notation:
g <=< f = join . fmap g . f
This leads us to the equivalent definition of a monad in terms of join and return:
class Functor m => Monad m where
join :: m (m a) -> m a
return :: a -> m a
This is still not the definition you will find in the standard Haskell Prelude. Since
the fish operator is a generalization of the dot operator, using it is equivalent to point-
free programming. It lets us compose arrows without naming intermediate values.
Although some consider point-free programs more elegant, most programmers find them
difficult to follow.
But function composition is really done in two steps: We apply the first function,
then apply the second function to the result. Explicitly naming the intermediate result
is often helpful in understanding of what’s going on.
To do the same with Kleisli arrows, we have to know how to apply the second Kleisli
arrow to a named monadic value—the result of the first Kleisli arrow. The function
that does that is called bind and is written as an infix operator:
160 CHAPTER 14. MONADS
Partiality
We’ve already seen the version of the Maybe monad implemented using Kleisli compo-
sition. Here’s the more familiar implementation using bind:
instance Monad Maybe where
Nothing >>= k = Nothing
(Just a) >>= k = k a
return = Just
Logging
In order to compose functions that produce logs, we need a way to combine individual
log entries. This is why the writer monad:
newtype Writer w a = Writer (a, w)
requires the type of the log to be an instance of Monoid. This allows us to append logs
and to create an empty log.
14.4. MONAD INSTANCES 161
Environment
The reader monad is a thin encapsulation of a function from the environment to the
return type:
newtype Reader e a = Reader (e -> a)
Here’s the Monad instance:
instance Monad (Reader e) where
ma >>= k = Reader (\e -> let a = runReader ma e
in runReader (k a) e)
return a = Reader (\e -> a)
The implementation of bind for the reader monad creates a function that takes the
environment as its argument. This environment is used twice, first to run ma to get the
value of a, and then to evaluate the value produced by k a.
The implementation of return ignores the environment.
Exercise 14.4.1. Define the Functor and the Monad instance for the following data
type:
newtype E e a = E (e -> Maybe a)
Hint: You may use this handy function:
runE :: E e a -> e -> Maybe a
runE (E f) e = f e
State
Like reader, the state monad is a function type:
newtype State s a = State (s -> (a, s))
Its bind is similar, except that the result of k acting on a is now run with the modified
state s'.
instance Monad (State s) where
st >>= k = State (\s -> let (a, s') = runState st s
in runState (k a) s')
Nondeterminism
For the list monad, let’s consider how we could implement join. It must turn a list of
lists into a single list. This can be done by concatenating all the inner lists using the
library function concat. From there, we can derive the implementation of bind.
instance Monad [] where
as >>= k = concat (fmap k as)
return a = [a]
return constructs a singleton list.
What in imperative languages do using nested loops we can do in Haskell using the
list monad. Think of as as aggregating the result of running the inner loop and k as
the code that runs in the outer loop.
In many ways, Haskell’s list behaves more like what is called an iterator or a gen-
erator in imperative languages. Because of laziness, the elements of the list are rarely
stored in memory all at once. You may also think of a list as a coroutine that produces,
on demand, elements of a sequence.
Continuation
The implementation of bind for the continuation monad:
newtype Cont r a = Cont ((a -> r) -> r)
requires some backward thinking, because of the inherent inversion of control—the
“don’t call us, we’ll call you” principle.
The result of bind is supposed to be a function that takes, as an argument, a
continuation k of the type:
14.5. DO NOTATION 163
k :: b -> r
We have to put together such a function given the two ingredients at our disposal:
ma :: Cont r a
fk :: a -> Cont r b
We’d like to run ma, but for that we need a continuation that would accept an a. We
can build such a continuation: given an a it would call fk. The result is a b-expecting
continuation Cont r b. But that’s exactly the continuation k we have at our disposal.
Taken together, this convoluted process produces the following implementation:
instance Monad (Cont r) where
ma >>= fk = Cont (\k -> runCont ma (\a -> runCont (fk a) k))
return a = Cont (\k -> k a)
As we mentioned earlier, composing continuations is not for the faint of heart. However,
it has to be implemented only once—in the definition of the continuation monad. From
there on, the do notation will make the rest relatively easy.
Input/Output
The IO monad’s implementation is baked into the language. The basic I/O primitives
are available through the library. They are either in the form of Kleisli arrows, or IO
objects (conceptually, Kleisli arrows from the terminal object ()).
For instance, the following object contains a command to read a line from the
standard input:
getLine :: IO String
There is no way to extract the string from it, since it’s not there yet; but the program
can process it through a further series of Kleisli arrows.
The IO monad is the ultimate procrastinator: the composition of its Kleisli arrows
piles up task after task to be executed later by the Haskell runtime.
To output a string followed by a newline, you can use this Kleisli arrow:
putStrLn :: String -> IO ()
Combining the two, you may construct a simple main object:
main :: IO ()
main = getLine >>= putStrLn
which echos a string you type.
14.5 Do Notation
It’s worth repeating that the sole purpose of monads in programming is to let us de-
compose one big Kleisli arrow into multiple smaller ones.
This can be either done directly, in a point-free style, using Kleisli composition <=<;
or by naming intermediate values and binding them to Kleisli arrows using >>=.
Some Kleisli arrows are defined in libraries, others are reusable enough to warrant
out-of-line implementation but, in practice, the majority are implemented as single-shot
inline lambdas.
Here’s a simple example:
164 CHAPTER 14. MONADS
main :: IO ()
main =
getLine >>= \s1 ->
getLine >>= \s2 ->
putStrLn ("Hello " ++ s1 ++ " " ++ s2)
which uses an ad-hoc Kleisli arrow of the type String->IO () defined by the lambda
expression:
\s1 ->
getLine >>= \s2 ->
putStrLn ("Hello " ++ s1 ++ " " ++ s2)
The body of this lambda is further decomposed using another ad-hoc Kleisli arrow:
\s2 -> putStrLn ("Hello " ++ s1 ++ " " ++ s2)
Such constructs are so common that there is special syntax called the do notation
that cuts through a lot of boilerplate. The above code, for instance, can be written as:
main = do
s1 <- getLine
s2 <- getLine
putStrLn ("Hello " ++ s1 ++ " " ++ s2)
The compiler will automatically convert it to a series of nested lambdas. The line
s1<-getLine is usually read as: “s1 gets the result of getLine.”
Here’s another example: a function that uses the list monad to generate all possible
pairs of elements taken from two lists.
pairs :: [a] -> [b] -> [(a, b)]
pairs as bs = do
a <- as
b <- bs
return (a, b)
Notice that the last line in a do block must produce a monadic value—here this is
accomplished using return.
As mentioned before, the do notation makes an easy task of otherwise very cum-
bersome composition of continuations.
Most imperative languages lack the abstraction power to generically define a monad
and instead they attempt to hard-code some of the more common monads. For instance,
they implement exceptions as an alternative to the Either monad, or concurrent tasks
as an alternative to the continuation monad. Some, like C++, introduce coroutines
that mimic Haskell’s do notation.
Exercise 14.5.1. Implement the following function that works for any monad:
ap :: Monad m => m (a -> b) -> m a -> m b
Hint: Use do notation to extract the function and the argument. Use return to return
the result.
Exercise 14.5.2. Rewrite the pairs function using the bind operators and lambdas.
14.6. MONADS CATEGORICALLY 165
Substitution
Consider this simple expression type. It’s parameterized by the type x that we can use
for naming variables:
data Ex x = Val Int
| Var x
| Plus (Ex x) (Ex x)
deriving (Functor, Show)
We can, for instance, construct an expression (2 + a) + b:
ex :: Ex Char
ex = Plus (Plus (Val 2) (Var 'a')) (Var 'b')
We can implement the Monad instance for Ex:
instance Monad Ex where
Val n >>= k = Val n
Var x >>= k = k x
Plus e1 e2 >>= k =
let x = e1 >>= k
y = e2 >>= k
in (Plus x y)
return x = Var x
Now suppose that you want to make a substitution by replacing a with x1 + 2 and
b with x2 (for simplicity, let’s not worry about other letters of the alphabet). This
substitution is represented by the Kleisli arrow sub (as you can see, we are even able
to change the type of variable names from Char to String):
sub :: Char -> Ex String
sub 'a' = Plus (Var "x1") (Val 2)
sub 'b' = Var "x2"
When we bind it to ex
ex' :: Ex String
ex' = ex >>= sub
we get, as expected, a tree corresponding to (2 + (x1 + 2)) + x2 .
Monad as a monoid
Let’s analyze the definition of a monad that uses join:
class Functor m => Monad m where
join :: m (m a) -> m a
return :: a -> m a
We have an endofunctor m and two polymorphic functions.
166 CHAPTER 14. MONADS
In category theory, the functor that defines the monad is traditionally denoted by
T (probably because monads were initially called “triples”). The two polymorphic
functions become natural transformations. The first one, corresponding to join, maps
the “square” of T —or a composition of T with itself—to T :
µ: T ◦ T → T
η : Id → T
µ: m ⊗ m → m
η: i → m
The similarity is striking. This is why we often call the natural transformation µ
monadic multiplication. But in what category can the composition of functors be con-
sidered a tensor product?
Enter the category of endofunctors. Objects in this category are endofunctors and
arrows are natural transformations.
But there’s more structure to that category. Any two endofunctors can be com-
posed, so maybe composition could be considered a tensor product? After all, the only
condition we impose on a tensor product is that it’s functorial in both arguments. That
is, given a pair of arrows:
α: T → T0
β : S → S0
α ⊗ β : T ⊗ S → T 0 ⊗ S0
α ◦ β : T ◦ T 0 → S ◦ S0
But this is just the horizontal composition of natural transformations (now you under-
stand why it’s denoted by a circle).
The unit object in this monoidal category is the identity endofunctor, and unit laws
are satisfied “on the nose,” meaning
Id ◦ T = T = T ◦ Id
We don’t need any unitors. We don’t need any associators either, since functor compo-
sition is automatically associative.
A monoidal category in which unitors and associators are identity morphisms is
called a strict monoidal category.
14.7. FREE MONADS 167
η : Id → T
µ: T ◦ T → T
For this to be a monoid, these arrows must satisfy monoidal laws. Here are the unit
laws (with unitors replaced by strict equalities):
η◦T T ◦η
Id ◦ T T ◦T T ◦ Id
µ
= =
T
=
(T ◦ T ) ◦ T T ◦ (T ◦ T )
µ◦T T ◦µ
T ◦T T ◦T
µ µ
Category of monads
The objects in the category of monads Mon(C) are monads (T, η, µ). We can define an
arrow between two monads (T, η, µ) and (T 0 , η 0 , µ0 ) as a natural transformation between
the two endofunctors:
λ: T → T0
However, since monads are endofunctors with structure, we want these natural trans-
formations to preserve the structure. Preservation of unit means that the following
168 CHAPTER 14. MONADS
λ◦λ
T ◦T T0 ◦ T0
µ µ0
λ
T T0
Another way of looking at Mon(C) is that it’s a category of monoids in [C, C].
Free monad
Now that we have a category of monads, we can define the forgetful functor:
U : Mon(C) → [C, C]
that maps (T, η, µ) to T and a monad morphism to the underlying natural transforma-
tion.
We would like a free monad to be generated by a left adjoint to this forgetful functor.
The problem is that this left adjoint doesn’t always exist. So far we’ve been avoiding
talking about sizes of sets, and it was okay. Unfortunately, monads tend to blow things
up. The bottom line is that free monads exist for some, but not all, endofunctors.
Therefore we can’t define a free monad through an adjunction. Fortunately, in cases of
interest a free monad can be defined as a fixed point of an algebra.
The construction is analogous to how we defined a free monoid as an initial algebra
for the list functor:
data ListF a x = NilF | ConsF a x
or the more general:
Fa x = I + a ⊗ x
This time, however, the monoidal category in which a monad is defined as a monoid is
the category of endofunctors ([C, C], Id, ◦). A free monoid in this category is the initial
algebra for the “list” functor that maps functors to functors:
ΦF G = Id + F ◦ G
(F + G)a = F a + Ga
and on arrows:
(F + G)f = F f + Gf
(We can form a coproduct of two morphisms using the functoriality of the coproduct.
We are assuming that C is co-cartesian, that is all coproducts exist.)
14.7. FREE MONADS 169
The initial algebra is the (least) fixed point of this operator, or the solution to the
identity:
LF ∼ = Id + F ◦ LF
This formula establishes a natural isomorphism between two functors. In particular,
the mapping out of the sum is equivalent to a pair of natural transformations:
Id → LF
F ◦ LF → LF
When translating to Haskell, the components of these transformation become two con-
structors. We get the following data type parameterized by a functor f:
data FreeMonad f a where
Pure :: a -> FreeMonad f a
Free :: f (FreeMonad f a) -> FreeMonad f a
If we think of a functor f as a container of values, the constructor Free takes a
container of (FreeMonad f a) and stashes it away. An arbitrary value of the type
FreeMonad f a is therefore a tree in which every node is a functorful of branches, and
each leaf contains a value of the type a.
Because this definition is recursive, the Functor instance for it is also recursive:
instance Functor f => Functor (FreeMonad f) where
fmap g (Pure a) = Pure (g a)
fmap g (Free ffa) = Free (fmap (fmap g) ffa)
The outer fmap uses the Functor instance of f, while the inner (fmap g) recurses into
the branches.
The monadic unit is just a thin encapsulation of the identity functor:
eta :: a -> FreeMonad f a
eta a = Pure a
Monadic multiplication, or join, is defined recursively:
mu :: Functor f => FreeMonad f (FreeMonad f a) -> FreeMonad f a
mu (Pure fa) = fa
mu (Free ffa) = Free (fmap mu ffa)
The Monad instance for FreeMonad f is therefore:
instance Functor f => Monad (FreeMonad f) where
return a = eta a
m >>= k = mu (fmap k m)
We can also define bind directly:
(Pure a) >>= k = k a
(Free ffa) >>= k = Free (fmap (>>= k) ffa)
A free monad accumulates monadic actions in a tree-like structure without com-
mitting to a particular evaluation strategy. This tree can be “interpreted” using an
algebra. This time it’s an algebra in the category of endofunctors, so its carrier is an
endofunctor G and the structure map is a natural transformation:
α: I + F ◦ G → G
170 CHAPTER 14. MONADS
λ: I → G
ρ: F ◦ G → G
Exercise 14.7.3. Find a functor whose free monad is equivalent to the list monad [a].
For instance, Push is supposed to push an integer on the stack and then call the con-
tinuation k.
The free monad for this functor can be thought of as a tree, with most branches
having just one child, thus forming lists. The exception is the Top node, which has
many children, one per every value of Int.
This is the free monad for this functor:
type FreeStack = FreeMonad StackF
In order to create domain-specific programs we’ll define a few helper functions.
There is a generic one that lifts a functorful of values to a free monad:
liftF :: (Functor f) => f r -> FreeMonad f r
liftF fr = Free $ fmap (Pure) fr
We also need a series of “smart constructors,” which are Kleisli arrows for our free
monad:
push :: Int -> FreeStack ()
push n = liftF (Push n ())
pop :: FreeStack ()
pop = liftF (Pop ())
add :: FreeStack ()
add = liftF (Add ())
Since a free monad is a monad, we can conveniently combine Kleisli arrows using
the do notation. For instance, here’s a toy program that adds two numbers and returns
their sum:
calc :: FreeStack Int
calc = do
push 3
push 4
add
x <- top
pop
return x
In order to execute this program, we need to define an algebra whose carrier is an
endofunctor. Since we want to implement a stack-based calculator, we’ll use a version
of a state functor whose state is a stack—a list of integers. It’s a function type that
takes a list and returns a new list coupled with the continuation:
data StackAction k = St ([Int] -> ([Int], k))
deriving Functor
To run the action, we apply the continuation to the stack:
runAction :: StackAction k -> [Int] -> ([Int], k)
runAction (St k) ns = k ns
172 CHAPTER 14. MONADS
Exercise 14.7.4. Implement a “pretty printer” that displays the program constructed
using our free monad. Hint: Implement the algebra that uses the Const functor as the
carrier:
showAlg :: MAlg StackF (Const String) a
Every time we have more than one instance of some structure, we may ask ourselves
the question: is there a whole category of such things? In this case: do monoidal
categories form their own category? For this to work we would have to define arrows
between monoidal categories.
A monoidal functor F from a monoidal category (C, ⊗, i) to another monoidal cat-
egory (D, ⊕, j) maps tensor product to tensor product and unit to unit—all up to
isomorphism:
Fa ⊕ Fb ∼
= F (a ⊗ b)
∼
j = Fi
Here, on the left-hand side we have the tensor product and the unit in the target
category, and on the right their counterparts in the source category.
If the two monoidal categories in question are not strict, that is the unit and as-
sociativity laws are satisfied only up to isomorphism, there are additional coherency
conditions that ensure that unitors are mapped to unitors and associators are mapped
to associators.
The category of monoidal categories with monoidal functors as arrows is called
MonCat. In fact it’s a 2-category, since one can define structure-preserving natural
transformations between monoidal functors.
η0 : j → F m
µ0 : F m ⊕ F m → F m
Fη: Fi → Fm
F µ : F (m ⊗ m) → F m
What we are missing, in order to implement η 0 and µ0 , are two additional arrows:
j → Fi
F m ⊕ F m → F (m ⊗ m)
A monoidal functor would provide such arrows as halves of the defining isomorphisms.
However, for what we’re trying it accomplish, we don’t need the other halves, so we can
relax the isomorphisms.
174 CHAPTER 14. MONADS
φi : j → F i
φab : F a ⊕ F b → F (a ⊗ b)
Exercise 14.8.1. Implement the Monoidal instance for the list functor.
Functorial strength
There is another way a functor may interact with monoidal structure, one that hides
in plain sight when we do programming. We take it for granted that functions have
access to the environment. Such functions are called closures.
For instance, here’s a function that captures a variable a from the environment and
pairs it with its argument:
\x -> (a, x)
This definition makes no sense in isolation, but it does when the environment contains
the variable a, e.g.,
pairWith :: Int -> (String -> (Int, String))
pairWith a = \x -> (a, x)
The function returned by calling pairWith 5 “encloses” the 5 from its environment.
Now consider the following modification, which returns a singleton list that contains
the closure:
pairWith' :: Int -> [String -> (Int, String)]
pairWith' a = [\x -> (a, x)]
As a programmer you’d be very surprised if this didn’t work. But what we do here is
highly nontrivial: we are smuggling the environment inside the list functor. According
to our model of lambda calculus, a closure is a morphism from the product of the
environment and the function argument: here (Int, String).
The property that lets us smuggle the environment under a functor is called func-
torial strength or tensorial strength and can be implemented in Haskell as:
strength :: Functor f => (e, f a) -> f (e, a)
strength (e, as) = fmap (e, ) as
The notation (e, ) is called a tuple section and is equivalent to the partial application
of the pair constructor: (,) e.
14.8. MONOIDAL FUNCTORS 175
Applicative functors
In programming, the idea of applicative functors arose from the following question: A
functor lets us lift a function of one variable. How can we lift a function of two or more
variables?
By analogy with fmap, we’d like to have a function:
liftA2 :: (a -> b -> c) -> f a -> f b -> f c
A function of two arguments—here, in its curried form—is a function of one ar-
gument returning a function. So, assuming that f is a functor, we can fmap the first
argument of liftA2:
a -> (b -> c)
over the second argument (f a) to get:
f (b -> c)
The problem is, we don’t know how to apply f (b -> c) to the remaining argument
f b.
The class of functors that let us do that is called Applicative. It turns out that,
once we know how to lift a two-argument function, we can lift functions of any number
of arguments, except zero. A zero-argument function is just a value, so lifting it means
implementing a function:
176 CHAPTER 14. MONADS
pure :: a -> f a
Here’s the Haskell definition:
class Functor f => Applicative f where
pure :: a -> f a
(<*>) :: f (a -> b) -> f a -> f b
The application of a functor-ful of functions to a functor-ful of arguments is defined as
an infix operator that is customarily called “splat.”
There is also an infix version of fmap:
(<$>) :: (a -> b) -> f a -> f b
which can be used in this terse implementation of liftA2:
liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
liftA2 g as bs = g <$> as <*> bs
Both operators bind to the left, which makes this syntax mimic regular function appli-
cation.
An applicative functor must also satisfy a set of laws:
pure id <*> v = v -- Identity
pure f <*> pure x = pure (f x) -- Homomorphism
u <*> pure y = pure ($ y) <*> u -- Interchange
pure (.) <*> u <*> v <*> w = u <*> (v <*> w) -- Composition
Exercise 14.8.2. Implement liftA3, a function that lifts a 3-argument function using
an applicative functor.
Closed functors
If you squint at the definition of the splat operator:
(<*>) :: f (a -> b) -> (f a -> f b)
you may see it as mapping a function object to a function object.
This becomes clearer if you consider a functor between two categories, both of them
closed. You may start with a function object ba in the source category and apply the
functor F to it:
F (ba )
Alternatively, you may map the two objects a and b and construct a function object
between them in the target category:
(F b)F a
If we demand that the two ways be isomorphic, we get a strict closed functor. But, as
was the case with monoidal functors, we are more interested in the lax version, which
is equipped with a one-way natural transformation:
F (ba ) → (F b)F a
If F is an endofunctor, this translates directly into the definition of the splat operator.
14.8. MONOIDAL FUNCTORS 177
The full definition of a lax closed functor includes the mapping of the monoidal unit
and some coherence conditions.
In a closed cartesian category, the exponential is related to the cartesian product
through the currying adjunction. It’s no surprise then that, in such a category, lax
monoidal and lax closed endofunctors are the same.
We can easily express this in Haskell:
instance (Functor f, Monoidal f) => Applicative f where
pure a = fmap (const a) unit
fs <*> as = fmap apply (fs >*< as)
where const is a function that ignores its second argument:
const :: a -> b -> a
const a b = a
and apply is the uncurried function application:
apply :: (a -> b, a) -> b
apply (f, a) = f a
The other way around we have:
instance Applicative f => Monoidal f where
unit = pure ()
as >*< bs = (,) <$> as <*> bs
In the latter, we used the pair constructor (,) as a two-argument function.
Of course, the list functor is also a monad, so there is another Applicative instance
based on that. Its splat operator applies every function to every argument.
In programming, monad is more powerful than applicative. That’s because monadic
code lets you examine the contents of a monadic value and branch depending on it. This
is true even for the IO monad which otherwise provides no means of extracting the value,
as in this example:
main :: IO ()
main = do
s <- getLine
if s == "yes"
then putStrLn "Thank you!"
else putStrLn "Next time."
Of course, the inspection of the value is postponed until the runtime interpreter of IO
gets hold of this code.
Applicative composition using the splat operator doesn’t allow for one part of the
computation to inspect the result of the other. This a limitation that can be turned into
an advantage. The absence of dependencies makes it possible to run the computations
in parallel. Haskell’s parallel libraries use applicative programming extensively.
On the other hand, monads let us use the very convenient do syntax, which is
arguably more readable than the applicative syntax. Fortunately, there is a language
extension ApplicativeDo, which instructs the compiler to selectively use applicative
constructs in interpreting do blocks, whenever there are no dependencies.
Exercise 14.8.3. Verify Applicative laws for the zip instance of the list functor.
Chapter 15
C α D
But the same idea can be represented by drawing categories as areas of a plane,
functors as lines between areas, and natural transformations as dots that join line
segments.
The idea is that a functor always goes between a pair of categories, therefore it can
be drawn as a boundary between them. A natural transformation always goes between
a pair of functors, therefore it can be drawn as a dot joining two segments of a line.
G
C D
α
This is an example of a string diagram. You read such a digram bottom-up, left-to-
right (think of the (x, y) system of coordinates).
The bottom of this diagram shows the functor F that goes from C to D. The top
of the diagram shows the functor G that goes between the same two categories. The
transition happens in the middle, where a natural transformation α maps F to G.
179
180 CHAPTER 15. MONADS FROM ADJUNCTIONS
So far it doesn’t seem like we gain a lot by using this new visual representation.
But let’s apply it to something more interesting: vertical composition of natural trans-
formations:
H
β
G
C D
α
F
The corresponding string diagram shows the two categories and three functors be-
tween them joined by two natural transformations.
H
C D
β
G
α
As you can see, you can reconstruct the original diagram from the string diagram by
scanning it bottom-to-top.
Let’s continue with the horizontal composition of natural transformations:
F0 G0
C α D β E
F G
F0 G0
C D E
α β
F G
Drawing two parallel vertical lines, in this new system, corresponds to functor com-
position. You can think of the horizontal composition of natural transformations as
15.1. STRING DIAGRAMS 181
happening along the imaginary horizontal line in the middle of the diagram. But what
if somebody was sloppy in drawing the diagram, and one of the dots was a little higher
than the other? As it turns out, the exact positioning of the dots doesn’t matter, due
to the interchange law.
But first, let’s illustrate whiskering: horizontal composition in which one of the
natural transformation is the identity. We can draw it like this:
F G0
C D E
idF β
F G
But, really, the identity can be inserted at any point on a vertical line, so we don’t even
have to draw it. The following diagram represents the whiskering of β ◦ F .
F G0
C D E
β
F G
F F0
T T
C η C C µ C
Id T ◦T
Since we are dealing with just one category, when translating these diagrams to
string diagrams, we can dispose of the naming (and shading) of categories, and just
draw the strings alone.
T T
η
T µ T
Id
In the first diagram, it’s customary to skip the dashed line corresponding to the identity
functor. The η dot can be used to freely inject a T line into a diagram. Two T lines
can be joined by the µ dot.
String diagrams are especially useful in expressing monad laws. For instance, we
have the left identity law:
µ ◦ (η ◦ T ) = id
which can be visualized as a commuting diagram:
η◦T
Id ◦ T T ◦T
µ
id
T
The corresponding string diagrams representat the equality of the two paths through
this diagram:
T
T µ =
T T
η
You can think of this equality as the result of pulling on the top and bottom strings
resulting in the η appendage being retracted into the straight line.
There is a symmetric right identity law:
T
µ T =
T T
η
15.1. STRING DIAGRAMS 183
T T
µ = µ
µ µ
T T T T T T
L R
D C
The identity functor at the bottom of the diagram is omitted from the picture. The η
dot turns the identity functor below it to the composition R ◦ L above it.
Similarly, the counit can be visualized as a “cap”-shaped string diagram with the
implicit identity functor at the top:
C
ε
D
R L
Triangle identities can be easily expressed using string diagrams. They also make
intuitive sense, as you can imagine pulling on the string from both sides to straighten
the curve.
For instance, this is the first triangle identity.
R R
C ε C
L =
η
D D
R R
184 CHAPTER 15. MONADS FROM ADJUNCTIONS
Exercise 15.1.1. Draw the string diagrams for the second triangle identity.
T
D
L R
D C D
L R L R
Reading this diagram bottom-to-top, we get the following transformation (imagine slic-
ing it horizontally at the dot):
R◦ε◦L
R ◦ L ◦ R ◦ L −−−−→ R ◦ L
T
D
T T
µ=R◦ε◦L
To complete the picture, we can use string diagrams to derive monadic laws using
triangle identities. The trick is to replace all strings in monadic laws by pairs of parallel
strings and then rearrange them according to the rules.
To summarize, every adjunction L a R with the unit η and counit ε defines a monad
(R ◦ L, η, R ◦ ε ◦ L).
We’ll see later that, dually, the other composition, L ◦ R defines a comonad.
Exercise 15.2.1. Draw string diagrams to illustrate monadic laws (unit and associa-
tivity) for the monad derived from an adjunction.
µ=U ◦ε◦F
You can easily convince yourself that left whiskering doesn’t do much here, since it’s
just a lifting of a monoid morphism by the forgetful functor. It keeps the function while
forgetting about its special property of preserving structure.
The right whiskering by F is more interesting. It means taking the component of ε
at a free monoid generated from the set X. This free monoid is defined by:
mempty = []
mappend = (++)
which gives us the definition of µ or join:
join = foldr (++) []
As expected, this is the same as concat.
Ls a = a × s
Rs c = cs
(Rs ◦ Ls )a = (a × s)s
ηa : a → (a × s)s
εc : cs × s → c
µ = Rs ◦ ε ◦ Ls
a1 = idS
and two consecutive actions must combine to an action of their monoidal product:
This choice of the order of multiplication defines what it called the left action. (The
right action has the two monoidal elements swapped on the right-hand side.)
M-sets form a category MSet. The objects are pairs (S, a : M × S → S) and the
arrows are equivariant maps, that is functions between sets that preserve actions.
A function f : S → R is an equivariant mapping from (S, a) to (R, b) if the following
diagram commutes, for every m ∈ M :
f
S R
am bm
f
S R
In other words, it doesn’t matter if we first do the action am , and then map the set; or
first map the set, and then do the corresponding action bm .
There is a forgetful functor U from MSet to Set, which assigns the set S to to the
pair (S, a), thus forgetting the action.
Corresponding to it, there is a free functor F . Its action on a set S produces an
M-set that is a cartesian product of S and M (where M is treated as a set of elements).
An element of this M-set is a pair (x ∈ S, m ∈ M ) and the free action is defined by:
φn : (x, m) 7→ (x, n · m)
MSet(F S, Q) ∼
= Set(S, U Q)
15.3. EXAMPLES OF MONADS FROM ADJUNCTIONS 189
The trick here is to notice that an equivariant mapping on the left is fully determined
by its action on the elements of the form (x, 1).
The unit of this adjunction ηS : S → U (F S) maps an element x to a pair (x, 1).
Compare this with the definition of return for the writer monad:
return a = Writer (a, mempty)
The counit is given by an equivariant map:
εQ : F (U Q) → Q
The left hand side is an M-set constructed by taking the underlying set of Q and
crossing it with M . The original action of Q is forgotten and replaced by the free action.
The obvious choice for counit is:
εQ : (x, m) 7→ am x
µ=U ◦ε◦F
It means replacing Q in the definition of εQ with a free M-set whose action is the free
action. (Whiskering with U doesn’t change anything.)
Compare this with the definition of join for the writer monad:
join :: Monoid m => Writer m (Writer m a) -> Writer m a
join (Writer ( Writer (x, m), m')) = Writer (x, mappend m' m)
C D E
R0 R
15.4. MONAD TRANSFORMERS 191
There are three monads in this picture. There is the inner monad R0 ◦ L0 and the outer
monad R ◦ L as well as the composite R ◦ R0 ◦ L0 ◦ L.
If we call the inner monad T = R0 ◦ L0 , then R ◦ T ◦ L is the composite monad called
the monad transformer, because it transforms the monad T into a new monad.
In our example, Maybe is the inner monad:
Ta = 1 + a
Ls a = a × s
Rs c = cs
The result is:
(1 + a × s)s
or, in Haskell:
s -> Maybe (a, s)
In general, the inner monad T is defined by its unit η i and multiplication µi . The
outer adjunction is defined by its unit η o and counit εo .
The unit of the composite monad is the natural transformation:
η : Id → R ◦ T ◦ L
ηi
ηo
It is the vertical composition of the whiskered inner unit R ◦ η i ◦ L and the outer unit
η o . In components:
i
ηa = R(ηLa ) ◦ ηao
The multiplication of the composite monad is a natural transformation:
µ: R ◦ T ◦ L ◦ R ◦ T ◦ L → R ◦ T ◦ L
L T R
µi
εo
L T R L T R
192 CHAPTER 15. MONADS FROM ADJUNCTIONS
at La. Here, La is the type (a, s). So we are instantiating return as a function
(a, s) -> m (a, s). (The type inferencer will do this automatically for us.)
Next, we are lifting this component of return using R. Here, R is the exponential
(−)s , so it lifts a function by post-composition. It will post-compose return to whatever
function is passed to it. That’s exactly the function that we produced using eta. Notice
that the types match: we are post-composing (a, s) -> m (a, s) after s -> (a, s).
We can write the result of this composition as:
return x = StateT (\s -> return (x, s))
with the data constructor StateT to make the type checker happy. This is the return
of the composite monad in terms of the return of the inner monad.
The same reasoning can be applied to the formula for the component of the com-
posite µ at some c:
µc = R(µiLc ) ◦ (R ◦ T )(εo(T ◦L)c )
15.5. MONAD ALGEBRAS 193
The inner µi is the join of the monad m. Applying R turns it into post-composition.
The outer εo is function application taken at m (a, s) which, inserting the appro-
priate data constructors, can be written as uncurry runStateT:
uncurry runStateT :: (StateT s m a, s) -> m (a, s)
The application of (R ◦ T ) lifts this component of ε using the composition of functors
R and T . The former is implemented as post-composition, and the latter is the fmap
of the monad m.
Putting all this together, we get a point-free formula for join of the state monad
transformer:
join :: StateT s m (StateT s m a) -> StateT s m a
join mma = StateT (join . fmap (uncurry runStateT) . runStateT mma)
Here, the partially applied runStateT mma strips off the data constructor from the
argument mma:
runStateT mma :: s -> m (a, x)
Our earlier example of MaybeState can now be rewritten using a monad transformer:
type MaybeState s a = StateT s Maybe a
The original State monad can be recovered by applying the StateT monad trans-
former to the identity functor, which has a Monad instance defined in the library (notice
that the last type variable a is skipped in this definition):
type State s = StateT s Identity
Other monad transformers follow the same pattern. They are defined in the Monad
Transformer Library, MTL.
return x = Var x
We can instantiate the unit for an arbitrary type, in particular for the carrier type of
our algebra. It makes sense that evaluating Var c, where c is an element of Char should
give us back the same c. In other words, we’d like:
alg . return = id
This condition immediately eliminates a lot of algebras, such as:
alg (Var c) = 'a' -- not compatible with the monad Ex
The second condition we’d like to impose is that the algebra respects substitution.
A monad lets us flatten nested expressions using join. An algebra lets us evaluate such
expressions.
There are two ways of doing that: we can apply the algebra to a flattened expression,
or we can apply it to the inner expression first (using fmap), and then evaluate the
resulting expression.
alg (join mma) = alg (fmap alg mma)
where mma is of the type Ex (Ex Char).
In category theory these two conditions define a monad algebra.
We say that (a, α : T a → a) is a monad algebra for the monad (T, µ, η) if the
following diagrams commute:
ηa Tα
a Ta T (T a) Ta
α µa α
ida
a α
Ta a
Since monad algebras are just special kinds of algebras, they form a sub-category of
algebras. Recall that algebra morphisms are arrows that satisfy the following condition:
Tf
Ta Tb
α β
f
a b
These laws are sometimes called the unit law and the multiplication law for monad
algebras.
In light of this definition, we can re-interpret the second monad-algebra diagram as
asserting that the structure map of a monad algebra is also an algebra morphism from
(T a, µa ) to (a, α). This will come in handy in what follows.
Eilenberg-Moore category
The category of monad algebras for a given monad T on C is called the Eilenberg-Moore
category and is denoted by C T .
There is an obvious forgetful functor U T from C T to C. It maps an algebra (a, α) to
its carrier a, and treats algebra morphisms as regular morphisms between carriers.
There is a free functor F T that is left adjoint to U T . It’s defined to map an object
a of C to a monad algebra with the carrier T a. The structure map of this algebra is the
component of monad multiplication µa : T (T a) → T a.
15.5. MONAD ALGEBRAS 195
ηT a T µa
Ta T (T a) T (T (T a)) T (T a)
µa µT a µa
idT a
µa
Ta T (T a) Ta
The first diagram is just the left monadic unit law in components. The ηT a arrow
corresponds to the whiskering of η ◦ T . The second diagram is the associativity of µ
with the two whiskerings µ ◦ T and T ◦ µ expressed in components.
As is true for all adjunctions, the composition U T ◦ F T is a monad. However this
particular monad is identical to the original monad T .
Indeed, on objects, it first maps a to a free monad algebra (T a, µ) and then forgets
the structure map. The net result is the mapping of a to T a, which is exactly what the
original monad did. On arrows, it lifts an arrow f : a → b using T . The fact that the
arrow T f is an algebra morphism from (T a, µa ) to (T b, µb ) follows from naturality of
µ:
T (T f )
T (T a) T (T B)
µa µb
Tf
Ta TB
To prove that we have an adjunction, we can either construct the natural isomor-
phism between hom-sets, or define two natural transformations to serve as the unit and
the counit of the adjunction.
We define the unit of the adjunction as the monadic unit η of T . The counit is
a natural transformation whose component at (a, α) is an algebra morphism from the
free algebra generated by a, that is (T a, µa ), back to (a, α). As we’ve seen earlier, α
itself is such a morphism. We can therefore pick ε(a,α) = α.
Triangular identities for these definitions of η and ε follow from unit laws for the
monad and the monad algebra.
The whiskering of U T ◦ ε ◦ F T in components means instantiating ε at (T a, µa ),
which produces µa (the action of U T on arrows is trivial).
We have thus shown that, for any monad T we can define the Eilenberg-Moore
category and a pair of adjoint functors that give rise to this monad.
Kleisli category
Inside every Eilenberg-Moore category there is a smaller Kleisli category struggling to
get out. This smaller category is the image of the free functor we have constructed in
the previous section.
Despite appearances, the image of a functor does not necessarily define a subcat-
egory. Granted, it maps identities to identities and composition to composition. The
problem may arise if two arrows that were not composable in the source category be-
come composable in the target category. This may happen if the target of the first
arrow is mapped to the same object as the source of the second arrow. However, the
196 CHAPTER 15. MONADS FROM ADJUNCTIONS
free functor F T maps distinct objects into distinct free algebras, so its image is indeed
a subcategory of C T .
We have encountered the Kleisli category before. There are many ways of construct-
ing the same category, and the simplest way to describe the Kleisli category is in terms
of Kleisli arrows.
A Kleisli category for the monad (T, η, µ) is denoted by CT . Its objects are the same
as the objects of C, but an arrow in CT from a to b is represented by an arrow in C that
goes from a to T b. You may recognize it as the Kleisli arrow a -> m b we’ve defined
before. Because T is a monad, these Kleisli arrows can be composed using the “fish”
operator <=<.
To establish the adjunction, let’s define the left functor LT : C → CT as identity on
objects. We still have to define what it does to arrows. It must map a regular arrow
f : a → b to a Kleisli arrow from a to b. This Kleisli arrow is represented by an arrow
a → T b in C. We pick the composite ηb ◦ f :
f η
b
LT f : a −
→ b −→ Tb
CT (LT a, b) ∼
= C(a, RT b)
Comonads
If it were easily pronounceable, we should probably call side effects “ntext,” because
the dual to side effects is “context.”
Just like we were using Kleisli arrows to deal with side effects, we use co-Kleisli
arrows to deal with contexts.
Let’s start with the familiar example of an environment as a context. We have
previously constructed a reader monad from it, by currying the arrow:
(a, e) -> b
This time, however, we’ll treat it as a co-Kleisli arrow, which is an arrow from a “con-
textualized” argument.
As was the case with monads, we are interested in being able to compose such
arrows. This is relatively easy for the environment-carrying arrows:
composeWithEnv :: ((b, e) -> c) -> ((a, e) -> b) -> ((a, e) -> c)
composeWithEnv g f = \(a, e) -> g (f (a, e), e)
It’s also straightforward to implement an arrow that serves as an identity with
respect to this composition:
idWithEnv :: (a, e) -> a
idWithEnv (a, e) = a
This shows that there is a category in which co-Kleisli arrows serve as morphisms.
197
198 CHAPTER 16. COMONADS
Exercise 16.1.3. Implement a low-pass filter for BiStream from the previous exercise,
which averages over three values: the current one, one from the immediate past, and
one from the immediate future. For electrical engineers: implement a Gaussian filter.
δ: W → W ◦ W
ε : W → Id
=
(W ◦ W ) ◦ W W ◦ (W ◦ W )
δ◦W W ◦δ
W ◦W W ◦W
δ δ
W
Comonoids
We’ve seen how monadic laws followed from monoid laws. We can expect that comonad
laws should follow from a dual version of a monoid.
Indeed, a comonoid is an object in a monoidal category (C, ⊗, I) equipped with two
morphisms called co-multiplication and a co-unit:
δ: W → W ⊗ W
ε: W → I
We can replace the tensor product with endofunctor composition and the unit object
with the identity functor to get the definition of a comonad as a comonoid in the
category of endofunctors.
In Haskell we can define a Comonoid typeclass for the cartesian product:
class Comonoid w where
split :: w -> (w, w)
destroy :: w -> ()
Comonoids are less talked about than their siblings, monoids, mainly because they
are taken for granted. In a cartesian category, every object can be made into a comonoid:
just by using the diagonal mapping ∆a : a → a × a for co-multiplication, and the unique
arrow to the terminal object for counit.
In programming this is something we do without thinking. Co-multiplication means
being able to duplicate a value, and counit means being able to abandon a value.
In Haskell, we can easily implement the Comonoid instance for any type:
16.3. COMONADS FROM ADJUNCTIONS 201
C D
R L
δ =L◦η◦R
202 CHAPTER 16. COMONADS
C D C
R L
Costate comonad
We’ve seen that the state monad can be generated from the currying adjunction between
the product and the exponential. The left functor was defined as a product with some
fixed object s:
Ls a = a × s
and the right functor was the exponentiation, parameterized by the same object s:
Rs c = cs
The composition Ls ◦ Rs generates a comonad called the costate comonad or the store
comonad.
Translated to Haskell, the right functor assigns a function type s->c to c, and the
left functor pairs c with s. The result of the composition is the endofunctor:
data Store s c = St (s -> c) s
or, using GADT notation:
data Store s c where
St :: (s -> c) -> s -> Store s c
The functor instance post-composes the function to the first component of Store:
instance Functor (Store s) where
fmap g (St f s) = St (g . f) s
The counit of this adjunction, which becomes the comonadic extract, is function
application:
extract :: Store s c -> c
extract (St f s) = f s
The unit of this adjunction is a natural transformation η : Id → Rs ◦ Ls . We’ve used it
as the return of the state monad. This is its component at c:
eta :: c -> (s -> (c, s))
eta c = \s -> (c, s)
To get duplicate we need to whisker η it between the two functors:
δ = Ls ◦ η ◦ Rs
Whiskering on the right means taking the component of η at the object Rs c, and
whiskering on the left means lifting this component using Ls . Since Haskell translation
of whiskering is a tricky process, let’s analyze it step-by-step.
16.3. COMONADS FROM ADJUNCTIONS 203
For simplicity, let’s fix the type s to, say, Int. We encapsulate the left functor into
a newtype:
newtype Pair c = P (c, Int)
deriving Functor
and keep the right functor a type synonym:
type Fun c = Int -> c
The unit of the adjunction can be written as a natural transformation using explicit
forall:
eta :: forall c. c -> Fun (Pair c)
eta c = \s -> P (c, s)
We can now implement comultiplication as the whiskering of eta. The whiskering
on the right is encoded in the type signature, by using the component of eta at Fun c.
The whiskering on the left is done by lifting eta using the fmap defined for the Pair
functor. We use the language pragma TypeApplications to make it explicit which
fmap is to be used:
delta :: forall c. Pair (Fun c) -> Pair (Fun (Pair (Fun c)))
delta = fmap @Pair eta
This can be rewritten more explicitly as:
delta (P (f, s)) = P (\s' -> P (f, s'), s)
The Comonad instance can thus be written as:
instance Comonad (Store s) where
extract (St f s) = f s
duplicate (St f s) = St (St f) s
The store comonad is a useful programming concept. To understand that, let’s
consider again the case where s is Int.
We interpret the first component of Store Int c, the function f :: Int -> c, to
be an accessor to an imaginary infinite stream of values, one for each integer.
The second component can be interpreted as the current index. Indeed, extract
uses this index to retrieve the current value.
With this interpretation, duplicate produces an infinite stream of streams, each
shifted by a different offset, and extend performs a convolution on this stream. Of
course, laziness saves the day: only the values we explicitly demand will be evaluated.
Notice also that our earlier example of the Signal comonad is reproduced by
Store Double.
Exercise 16.3.1. A cellular automaton can be implemented using the store comonad.
This is the co-Kleisli arrow describing rule 110:
step :: Store Int Cell -> Cell
step (St f n) =
case (f (n-1), f n, f (n+1)) of
(L, L, L) -> D
(L, D, D) -> D
204 CHAPTER 16. COMONADS
(D, D, D) -> D
_ -> L
A cell can be either live or dead:
data Cell = L | D
deriving Show
Run a few generation of this automaton. Hint: Use the function iterate from the
Prelude.
Comonad coalgebras
Dually to monad algebras we have comonad coalgebras. Given a comonad (W, ε, δ), we
can construct a coalgebra, which consists of a carrier object a and an arrow φ : a → W a.
For this coalgebra to compose nicely with the comonad, we’ll require that we can extract
the value that was injected using φ, and that the lifting of φ is equivalent to duplication,
when acting on the result of φ:
εa Wφ
a Wa W (W a) Wa
φ δa φ
ida
a φ
Wa a
Just like with monad algebras, comonad coalgebras form a category. Given a
comonad (W, ε, δ) in C, its comonad coalgebras form a category called the Eilenberg-
Moore category (sometimes prefixed with co-) C W .
There is a co-Kleisli subcategory of C W denoted by CW
Given a comonad W , we can construct an adjunction using either C W or CW that
reproduces the comonad W . The construction is fully analogous to the one for monads.
Lenses
The coalgebra for the Store comonad is of particular interest. We’ll do some renaming
first: we’ll call the carrier s and the state a. The coalgebra is given by a function:
phi :: s -> Store a s
which is equivalent to a pair of functions:
set :: s -> a -> s
get :: s -> a
Such a pair is called a lens: s is called the source, and a is the focus.
With this interpretation get lets us extract the focus, and set replaces the focus
with a new value to produce a new s.
Lenses were first introduced to describe the retrieval and modification of data in
database records. Then they found application is working with data structures. A
lens objectifies the idea of having read/write access to a part of a larger object. For
instance, a lens can focus on one of the components of a pair or a particular component
of a record. We’ll discuss lenses and optics in the next chapter.
Let’s apply the laws of the comonad coalgebra to a lens. For simplicity, let’s omit
data constructors from the equations. We get the following simplified definitions:
16.3. COMONADS FROM ADJUNCTIONS 205
17.1 Profunctors
In the rarified air of category theory we encounter patterns that are so far removed
from their origins that we have problems visualizing them. It doesn’t help that the
more abstract a pattern gets the more dissimilar the concrete examples of it are.
An arrow from a to b is relatively easy to visualize. We have a very familiar model
for it: a function that consumes elements of a and produces elements of b. A hom-set
is a collection of such arrows.
A functor is an arrow between categories. It consumes objects and arrows from one
category and produces objects and arrows from another. We can think of it a recipe
for building such objects (and arrows) from materials provided by the source category.
In particular, we often think of an endofunctor as a container of building materials.
A profunctor maps a pair of objects ha, bi to a set P ha, bi and a pair of arrows:
hf : s → a, g : b → ti
to a function:
P hf, gi : P ha, bi → P hs, ti
A profunctor is an abstraction that combines elements of many other abstractions.
Since it’s a functor C op × C → Set, we can think of it as constructing a set from a pair
of objects, and a function from a pair of arrows (one of them going in the opposite
direction). This doesn’t help our imagination though.
Fortunately, we have a good model for a profunctor: the hom-functor. The set of
arrows between two objects behaves like a profunctor when you vary the objects. It
also makes sense that there is a difference between varying the source and the target of
the hom-set.
We can, therefore, think of an arbitrary profunctor as generalizing a hom-functor.
A profunctor provides additional bridges between objects, on top of hom-sets that are
already there.
There is, however one big difference between an element of the hom-set C(a, b) and
an element of the set P ha, bi. Elements of hom-sets are arrows, and arrows can be
composed. It’s not immediately obvious how to compose profunctors.
However, the lifting of arrows by a profunctor can be seen as generalizing composition—
just not between profuctors, but between hom-sets and profunctors. For instance, we
207
208 CHAPTER 17. ENDS AND COENDS
f g
s a P b t
Collages
There is no reason to restrict a profunctor to a single category. We can easily define a
profunctor between two categories as a functor P : C op × D → Set. Such a profunctor
can be used to glue two categories together by generating the missing hom-sets from
the objects in C to the objects in D.
A collage (or a cograph) of two categories C and D is a category whose objects are
objects from both categories (a disjoint union). A hom-set between two objects x and
y is either a hom-set in C, if both objects are in C; a hom-set in D, if both are in D; or
the set P hx, yi if x is in C and y is in D. Otherwise the hom-set is empty.
Composition of morphisms is the usual composition, except if one of the morphisms
is an element of P hx, yi. In that case we lift the morphism we’re trying to compose.
It’s easy to see that a collage is indeed a category. The new morphisms that go
between the two sides of the collage are sometimes called heteromorphisms. They can
only go from C to D, never the other way around.
Seen this way, a profunctor C op ×C → Set should really be called an endo-profunctor.
It defines a collage of C with itself.
Exercise 17.1.1. Show that there is a functor from a collage of two categories to a
stick-figure “walking arrow” category that has two objects and one arrow between them
(and two identity arrows).
Exercise 17.1.2. Show that, if there is a functor from C to the walking arrow category
then C can be split into a collage of two categories.
Profunctors as relations
Under a microscope, a profunctor looks like a hom-functor, and the elements of the set
P ha, bi look like individual arrows. But when we zoom out, we can view a profunctor
as a relation between objects. These are not the usual relations; they are proof-relevant
relations.
To understand this concept better, let’s consider a regular functor F : C → Set (in
other words, a co-presheaf). One way to interpret it is to say that it definines a subset
of objects of C, namely those objects that are mapped to non-empty sets. Every element
17.1. PROFUNCTORS 209
of F a is then treated as a proof that a is a member of this subset. If, on the other
hand, F a is an empty set, then a is not a member of the subset.
We can apply the same interpretation to profunctors. If the set P ha, bi is empty,
we say that b is not related to a. If it’s not empty, we say that each element of the set
P ha, bi represents a proof that b is related to a. We can then treat a profunctor as a
proof-relevant relation.
Notice that we don’t assume anything about this relation. It doesn’t have to be
reflexive, as it’s possible for P ha, ai to be empty (in fact, P ha, ai makes sense only for
endo-profunctors). It doesn’t have to be symmetric either.
Since the hom-functor is an example of an (endo-) profunctor, this interpretation
lets us view the hom-functor in a new light: as a built-in proof-relevant relation between
objects in a category. If there’s an arrow between two objects, they are related. Notice
that this relation is reflexive, since C(a, a) is never empty: at the very least, it contains
the identity morphism.
Moreover, as we’ve seen before, hom-functors interact with profunctors. If a is
related to b through P , and the hom-sets C(s, a) and D(b, t) are non-empty, then auto-
matically s is related to t through P . Profunctors are therefore proof-relevant relations
that are compatible with the structure of the categories in which they operate.
We know how to compose a profunctor with hom-functors, but how would we com-
pose two profunctors? We can get a clue from the composition of relations.
Suppose that you want to charge your cellphone, but you don’t have a charger. In
order to connect you to a charger it’s enough that you have a friend who owns a charger.
Any friend will do. You compose the relation of having a friend with the relation of
a person having a charger to get a relation of being able to charge your phone. The
proof that you can charge your phone is a pair of proofs, one of friendship and one of
the possession of a charger.
In general, we say that two objects are related by the composite relation if there
exists an object in the middle that is related to both of them.
the other that b is related to x. This pair then constitutes the proof that b is related
to a.
An existential type can be seen as a generalization of a sum type. We are summing
over all possible types x. Just like a finite sum can be constructed by injecting one of
the alternatives (think of the two constructors of Either), the existential type can be
constructed by picking one particular type for x and injecting it into the definition of
Procompose.
Just as mapping out from a sum type requires a pair of function, one per each
alternative; a mapping out from an existential type requires a family of functions, one
per every type. Such a family, in Haskell, is given by a polymorphic function:
mapOut :: Procompose p q a b -> (forall x. q a x -> p x b -> c) -> c
mapOut (Procompose qax pxb) f = (f qax pxb)
The composition of profunctors is again a profunctor, as can be seen from this
instance:
instance (Profunctor p, Profunctor q) => Profunctor (Procompose p q)
where
dimap l r (Procompose qax pxb) =
Procompose (dimap l id qax) (dimap id r pxb)
This is just saying that you can extend the composite profunctor by extending the first
one on the left and the second one on the right.
The fact that this definition of profunctor composition happens to work in Haskell
is due to parametricity. The language constraints the types of profunctors in a way
that makes it work. In general, though, taking a simple sum over intermediate objects
would result in over-counting, so in category theory we have to compensate for that.
17.2 Coends
The over-counting in the naive definition of profunctor composition happens when two
candidates for the object in the middle are connected by a morphism:
Q P
f
a x y b
We can either extend Q on the right, by lifting Qhid, f i, and use y as the middle object;
or we can extend P on the left, by lifting P hf, idi, and use x as the intermediary.
In order to avoid the double-counting, we have to tweak our definition of a sum type
when applied to profunctors. The resulting construction is called a coend.
First, let’s re-formulate the problem. We are trying to sum over all objects x in the
product:
P ha, xi × Qhx, bi
The double-counting happens because we can open up the gap between the two pro-
functors, as long as there is a morphism that we can fit between them. So we are really
looking at a more general product:
P ha, xi × Qhy, bi
17.2. COENDS 211
The important observation is that, if we fix the endpoints a and b, this product is a
profunctor in hy, xi. This is easily seen after a little rearrangement (up to isomorphism):
Qhy, bi × P ha, xi
We are interested in the sum of the diagonal parts of this profunctor, that is when x is
equal to y.
So let’s see how we would go about defining the sum of all diagonal entries of a
general profunctor P . In fact, this construction works for any functor P : C op × C → D,
not just for Set-valued profunctors.
The sum of the diagonal objects is defined by injections; in this case, one per every
object in C. Here we show just two of them and the dashed line representing all the
rest:
P hy, yi P hx, xi
iy ix
d
If we were defining a sum, we’d make it a universal object equipped with such
injections. But because we are dealing with functors of two variables, we want to
identify the injections that are related by “extending” some common ancestor (here,
P hy, xi). We want the following diagram to commute, whenever there is a connecting
morphism f : x → y:
P hy, xi
P hid,f i P hf,idi
P hy, yi P hx, xi
iy ix
d
This diagram is called a co-wedge, and its commuting condition is called the co-wedge
condition. For every f : x → y, we demand that:
gx = h ◦ i x
212 CHAPTER 17. ENDS AND COENDS
Pictorially, we have:
P hy, xi
P hid,f i P hf,idi
P hy, yi P hx, xi
iy ix
Rx
P hx, xi
gy h gx
a b
Left Right
a+b
f g
h
d
Just like the sum was defined as a universal cospan, a coend is defined as a universal
co-wedge.
In particular, if you were to construct a coend of a Set-valued profunctor, you
would start with a sum (a discriminated union) of all the sets P hx, xi. Then you would
identify all the elements of this sum that satisfy the co-wedge condition. You’d identify
the element a ∈ P hx, xi with the element b ∈ P hy, yi whenever there is an element
c ∈ P hy, xi and a morphism f : x → y, such that:
P hid, f i(c) = b
and
P hf, idi(c) = a
Notice that, in a discrete category (which is just a set of objects with no arrows
between them) the co-wedge condition is trivial (there are no f ’s other than identities),
so a coend is just a straightforward sum (coproduct) of the diagonal objects P hx, xi.
Extranatural transformations
A family of arrows in the target category parameterized by the objects of the source
category can often be combined into a single natural transformation between two func-
tors.
The injections in our definition of a cowedge form a family of functions that is
parameterized by objects, but they don’t neatly fit into a definition of a natural trans-
formation.
P hy, yi P hx, xi
iy ix
d
17.2. COENDS 213
P hy, xi
P hid,f i P hf,idi
P hy, yi P hx, xi
iy ix
d
Indeed, as is the case with the naturality square, it involves the interaction between the
lifting of a morphism f : x → y (here, in two different ways) and the components of the
transformation i.
Granted, the standard naturality condition deals with pairs of functors. Here, the
target is a single object d. But we can always reinterpret it as the output of a constant
functor ∆d : C op × C → D.
The cowedge condition can be interpreted as a special case of the more general
extranatural transformation. An extranatural transformation is a family of arrows:
P : C op × C → E
Q : Dop × D → E
Extranaturality in c means that the following diagram commutes for any morphism
f : c → c0 :
P hc0 , ci
P hid,f i P hf,idi
P hc0 , c0 i P hc, ci
αc0 d αcd
Qhd, di
Extranaturality in d means that the following diagram commutes for any morphism
g : d → d0 :
P hc, ci
αcd αcd0
Qhd, di Qhd0 , d0 i
Qhid,gi Qhg,idi
Qhd, d0 i
Given this definition, we get our cowedge condition as the extranaturality of the
mapping between the profunctor P and the constant profunctor ∆S .
214 CHAPTER 17. ENDS AND COENDS
We can now reformulate the definition of the coend as the object e equipped with
the extranatural transformation i : P → ∆e that is universal among such pairs.
Universality means that for any object d equipped with the extranatural transforma-
tion α : P → ∆d there is a unique morphism h : e → d that factorizes all the components
of α through the components of i:
αx = h ◦ ix
it’s enough to define a family of functions from the diagonal entries of the functor to d:
gx : P hx, xi → d
satisfying the cowedge condition. You can get a lot of mileage from this trick, especially
when combined with the Yoneda lemma. We’ll see examples of this in what follows.
17.3 Ends
Just like a coend generalizes a sum of the diagonal elements of a profunctor—its dual,
an end, generalizes the product. A product is defined by its projections, and so is an
end.
The generalization of a span that we used in the definition of a product would be
an object d with a family of projections, one per every object x:
πx : d → P hx, xi
d
πx πy
P hx, xi P hy, yi
P hid,f i P hf,idi
P hx, yi
The end is a universal wedge. We use the integral sign for it too, but with the
“integration variable” at the bottom.
Z
P hx, xi
x: C
You might be wondering why integrals based on multiplication are rarely used in
calculus. That’s because we can use a logarithm to replace multiplication with addition.
We don’t have this luxury in category theory, so ends and coends are equally important.
To summarize, an end is an object equipped with a family of morphisms (projec-
tions): Z
πa : P hx, xi → P ha, ai
x
satisfying the wedge condition.
It is universal among such objects; that is, for any other object d equipped with a
family of arrows gx satisfying the wedge condition, there is a unique morphism h that
factorizes the family gx through the family πx :
gx = πx ◦ h
216 CHAPTER 17. ENDS AND COENDS
Pictorially, we have:
d
gx h gy
R
x P hx, xi
πx πy
P hx, xi P hy, yi
P hid,f i P hf,idi
P hx, yi
Equivalently, we can say that the end is a pair (e, π) consisting of an object e and an
extranatural transformation π : ∆d → e that is universal among such pairs. The wedge
condition turns out to be a special case of extranaturality condition.
If you were to construct an end of a Set-valued profunctor, you’d start with a
product of all P hx, xi for all objects in the category and then prune the tuples that
don’t satisfy the wedge condition.
In particular, imagine for a moment using the singleton set 1 in place of d. The
family gx would select one element from each set P hx, xi. This would give you a giant
tuple. You’d weed out most of these tuples, leaving only the ones that satisfy the wedge
condition.
Again, in Haskell, due to parametricity, the wedge condition is automatically satis-
fied, and the definition of an end for a profunctor p simplifies to:
type End p = forall x. p x x
The Haskell implementation of an End doesn’t showcase the fact that it is dual to
the Coend. This is because, at the time of this writing, Haskell doesn’t have a built-in
syntax for existential types. If it did, the Coend would be implemented as:
type Coend p = exists x. p x x
The existential/universal duality between a Coend and an End means that it’s easy
to construct a Coend—all you need is to pick one type x for which you have a value of
the type p x x. On the other end, to construct an End you have to provide a whole
family of values p x x, one for every type x. In other words, you need a polymorphic
formula that is parameterized by x. A definition of a polymorphic function is a canonical
example of such a formula.
Fx
F
x αx
G
Gx
The mapping ha, bi → C(F a, Gb) behaves like a profunctor. Its action on a pair of
arrows hf, gi is a combination of pre- and post-composition of lifted arrows:
(Gg) ◦ − ◦ (F f )
Indeed, an element of the set C(F a, Gb) is an arrow h : F a → Gb. We are trying to
lift a pair of arrows f : s → a and g : b → t. We can do it with a pair of arrows in C:
the first one is F f : F s → F a, and the second one is Gg : Gb → Gt. The composition
Gg ◦ h ◦ F f gives us the desired result F s → Gt, which is an element of C(F s, Gt).
Ff h Gg
F s −−→ F a −
→ Gb −−→ Gt
The diagonal parts of this profunctor are good candidates for the components of a
natural transformation. In fact, the end:
Z
C(F x, Gx)
x: B
In category theory, though, we have to check the wedge condition. Plugging in our
profunctor, we get:
R
x C(F x, Gx)
πa πb
(F f ◦ −) (− ◦ Gf )
C(F a, Gb)
R
We can focus on a single element of the set x C(F x, Gx) by instantiating the uni-
versal condition for the singleton set:
218 CHAPTER 17. ENDS AND COENDS
1
αa α αb
R
x C(F x, Gx)
πa πb
(F f ◦ −) (− ◦ Gf )
C(F a, Gb)
It picks the component αa from the hom-set C(F a, Ga) and the component αb from
C(F b, Gb). The wedge condition then boils down to:
F f ◦ αa = αb ◦ Gf
for any f : a → b. This is exactly the naturality condition. So an element α of this end
is indeed a natural transformation.
The set of natural transformations, or the hom-set in the functor category, is thus
given by the end: Z
[C, D](F, G) ∼
= C(F x, Gx)
x: B
As we discussed earlier, to construct an End we have to give it a whole family of
values parameterized by types. Here, these values are the components of a polymorphic
function.
C(a + b, x) ∼
= C(a, x) × C(b, x)
This follows from the universality of the sum: a mapping out of the sum is defined by
a pair of mapping out of the two objects.
17.5. FUBINI RULE 219
It can be shown that an end can be expressed as a limit, and a coend as a colimit.
Therefore, by continuity of the hom-functor, we can always pull out the integral sign
from inside a hom-set. By analogy with the product, we have the mapping-in formula
for an end: Z Z
D d, P ha, ai ∼ = D(d, P ha, ai)
a a
By analogy with the sum, we have a mapping-out formula for the coend:
Z a Z
D P ha, ai, d ∼
= D(P ha, ai, d)
a
These versions of the Yoneda lemma, expressed in terms of ends, are often half-
jokingly called ninja-Yoneda lemmas. The fact that the “integration variable” is explicit
makes them somewhat easier to use in complex formulas.
220 CHAPTER 17. ENDS AND COENDS
There is also a dual set of ninja co-Yoneda lemmas that use coends instead. For a
covariant functor, we have:
Z x: C
C(x, a) × F x ∼
= Fa
Physicists might notice the similarity of these formulas to integrals involving the
Dirac delta function (actually, a distribution). This is why profunctors are sometimes
called distributors, following the adage that “distributors are to functors as distributions
are to functions.” Engineers might notice the similarity of the hom-functor to the
impulse function.
This intuition is often expressed by saying that we can perform the “integration
over x” in this formula that results in replacing x with a in the integrand Gx.
If C is a discrete category, the coend reduces to the sum (coproduct), and the
hom-functor reduces to the unit matrix (the Kronecker delta). The co-Yoneda lemma
becomes: X j
δi vj = vi
j
In fact, a lot of linear algebra translates directly to the theory of Set-valued functors.
You may often view of such functors as vectors in a vector space, in which hom-functors
form a basis.
Yet another name for profunctors, especially in Australia, is “bimodules.” This is
because the lifting of morphisms by a profunctor is somewhat similar to the left and
right actions on sets.
The proof of the co-Yoneda lemma is quite instructive, as it uses a few common
tricks. Most importantly, we rely on the corollary of the Yoneda lemma, which says
that, if all the mappings out from two objects to an arbitrary object are isomorphic,
then the two objects themselves are isomorphic. We’ll start, therefore, with such a
mapping-out to an arbitrary set S:
Z x : C
Set C(x, a) × F x, S
Using the co-continuity of the hom-functor, we can pull out the integral sign, replacing
the coend with an end: Z
Set (C(x, a) × F x, S)
x: C
Since the category of sets is cartesian closed, we can curry the product:
Z
Set C(x, a), S F x
x: C
We can now use the Yoneda lemma to “integrate over x.” The result is S F a . Finally,
in Set, the exponential object is isomorphic to the hom-set:
SF a ∼
= Set(F a, S)
17.7. DAY CONVOLUTION 221
This formula can be translated almost verbatim to category theory. We can start by
replacing the integral with a coend. The problem is, we don’t know how to subtract
objects. We do however know how to add them, in a co-cartesian category.
222 CHAPTER 17. ENDS AND COENDS
Notice that the sum of the arguments to the functions is equal to x. We could
enforce this condition by introducing the Dirac delta function or the “impulse function,”
δ(a + b − x). In category theory we use the hom-functor to do the same. Thus we can
define a convolution of two Set-valued functors:
Z a,b
(F ? G)x = C(a + b, x) × F a × Gb
Informally, if we could define subtraction as the right adjoint to coproduct, we’d write:
Z a,b Z a,b Z b
C(a + b, x) × F a × Gb ∼
= C(a, b − x) × F a × Gb ∼
= F (b − x) × Gb
There is nothing special about coproduct so, in general, Day convolution is defined
for any monoidal category with a tensor product:
Z a,b
(F ? G)x = C(a ⊗ b, x) × F a × Gb
In fact, Day convolution for a monoidal category (C, ⊗, I) endows the category of co-
presheaves [C, Set] with a monoidal structure. It’s easy to check that Day convolution is
associative (up to isomorphism) and that C(I, −) serves as the unit object. For instance,
we have:
Z a,b Z b
(C(I, −) ? G)x = ∼
C(a ⊗ b, x) × C(I, a) × Gb = C(I ⊗ b, x) × Gb ∼
= Gx
So the unit of Day convolution is the Yoneda functor taken at monoidal unit, which
lends itself to the anagrammatic slogan, “ONE of DAY is a YONEDA of ONE.”
If the tensor product is symmetric, then the corresponding Day convolution is also
symmetric (up to isomorphism).
In the special case of a cartesian closed category, we can use the currying adjunction
to simplify the formula:
Z a,b Z a,b Z b
(F ? G)x = C(a × b, x) × F a × Gb ∼
= C(a, x ) × F a × Gb ∼
b
= F (xb ) × Gb
η : C(I, −) → F
µ: F ? F → F
In particular, in a cartesian closed category where the unit is the terminal object, C(1, a)
is isomorphic to a, and the component of unit at a is:
ηa : a → F a
The result is the set of natural transformations that define the second part of the lax
monoidal functor:
(>*<) :: f a -> f b -> f (a, b)
224 CHAPTER 17. ENDS AND COENDS
Free Applicatives
We have just learned that applicative functors are monoids in the monoidal category:
It’s only natural to ask what a free monoid in that category is.
Just like we did with free monads, we’ll construct a free applicative as the initial
algebra, or the least fixed point of the list functor. Recall that the list functor was
defined as:
Φa x = 1 + a ⊗ x
In our case it becomes:
ΦF G = C(I, −) + F ? G
Its fixed point is given by the recursive formula:
AF ∼
= C(I, −) + F ? AF
When translating this to Haskell, we observe that functions from the unit ()->a are
isomorphic to elements of a.
We get two constructor corresponding to the two addends:
data FreeA f x where
DoneA :: x -> FreeA f x
MoreA :: ((a, b) -> x) -> f a -> FreeA f b -> FreeA f x
I have inlined the definition of Day convolution:
data Day f g x where
Day :: ((a, b) -> x) -> f a -> g b -> Day f g x
The easiest way to show that, for any functor f, FreeA f is an applicative is to go
through Monoidal:
class Monoidal f where
unit :: f ()
(>*<) :: f a -> f b -> f (a, b)
The Monoidal instance for free applicative generalizes the idea of list concatenation.
We do the pattern matching on the first list, resulting in two cases.
In the first case, instead of an empty list we have DoneA x. Prepending it to the
second argument doesn’t change the length of the list, but it modifies the type of the
values stored in it. It pairs them with x.
(DoneA x) >*< fry = fmap (x,) fry
The second case is a “list” whose head fa is a functorful of a’s, and the tail frb is
of the type FreeA f b. The two are glued using a function abx :: (a, b) -> x.
(MoreA abx fa frb) >*< fry = MoreA (reassoc abx) fa (frb >*< fry)
To produce the result, we concatenate the two tails using the recursive call to >*< and
prepend fa to it. To glue this head to the new tail we have to provide a function that
re-associates the pairs:
17.8. THE BICATEGORY OF PROFUNCTORS 225
Exercise 17.7.3. Define the Functor instance for the free applicative.
We use the associativity of the product and the fact that we can switch the order of
coends using the Fubini theorem. Both are true only up to isomorphism. We don’t get
associativity “on the nose.”
The identity profunctor turns out to be the hom-functor, which can be written
symbolically as C(−, =), with placeholders for both arguments. For instance:
Z a
(C(−, =) P ) hs, ti = C(s, a) × P ha, ti ∼
= P hs, ti
This is the consequence of the (contravariant) ninja co-Yoneda lemma, which is also an
isomorphism—not an equality.
A category in which categorical laws are satisfied up to isomorphism is called a
bicategory. Notice that such a category must be equipped with 2-cells—morphisms
226 CHAPTER 17. ENDS AND COENDS
Qha, bi P hs, ti
Qhf,gi αhs,ti
Qhs, ti
for every pair of arrows:
hf : s → a, g : b → ti
Monads in a bicategory
We’ve seen before that categories, functors, and natural transformations form a 2-
category Cat. Let’s focus on one object, a category C, that is a 0-cell in Cat. The
1-cells that start and end at this object form a regular category, in this case it’s the
functor category [C, C]. The objects in this category are endo-1-cells of the outer 2-
category Cat. The arrows between them are the 2-cells of the outer 2-category.
This endo-one-cell category is automatically equipped with a monoidal structure.
We define the tensor product as the composition of 1-cells—all 1-cells with the same
source and target compose. The monoidal unit object is the identity 1-cell, I. In [C, C]
this product is the composition of endofunctors and the unit is the identity functor.
If we now focus our attention on just one endo-1-cell F , we can “square” it, that
is use the monoidal product to multiply it by itself. In other words, use the 1-cell
composition to create F ◦ F . We say that F is a monad if we can find 2-cells:
µ: F ◦ F → F
η: I → F
that behave like multiplication and unit, that is make the associativity and unit dia-
grams commute.
F ◦F
C
17.9. EXISTENTIAL LENS 227
In fact a monad can be defined in an arbitrary bicategory, not just the 2-category Cat.
µ: P P → P
η : C(−, =) → P
that satisfy the associativity and unit conditions.
These natural transformations are elements of ends. For instance:
Z Z x
µ∈ Set P ha, xi × P hx, bi, P ha, bi
ha,bi
Here, get extracts the part a from the whole s, and set replaces that part with a new
a. Lens laws help to reinforce this picture. And it’s all done in terms of arrows.
Another way of describing a composite object is to say that it can be split into a
focus and a residue. The trick is that, although we want to know what type the focus
is, we don’t care about the type of the residue. All we need to know about the residue
is that it can be combined with the focus to recreate the whole object.
In Haskell, we would express this idea using an existential type:
data Lens s a where
Lens :: (s -> (c, a), (c, a) -> s) -> Lens s a
This tells us that there exists some unspecified type c such that s can be split into, and
reconstructed from, a product (c, a).
s c
The get/set version of the lens can be derived from this existential form.
toGet :: Lens s a -> (s -> a)
toGet (Lens (l, r)) = snd . l
In fact, we can generalize it to a type-changing lens, in which the focus a can be replaced
with a new focus of a different type b. Replacing a with b will produce a new composite
object t:
c t
17.9. EXISTENTIAL LENS 229
The lens is now parameterized by two pairs of objects: hs, ti for the outer ones, and
ha, bi for the inner ones. The existential residue c remains hidden:
Z c
Lhs, tiha, bi = C(s, c × a) × C(c × b, t)
The product under the coend is the diagonal part of the profunctor that is covariant in
y and contravariant in x:
C(s, y × a) × C(x × b, t)
C(s, y × a) × C(x × b, t)
Lens composition
The main advantage of using lenses is that they compose. A composition of two lenses
lets us zoom in on a subcomponent of a component.
Suppose that we start with a lens that lets us access the focus a and change it to b.
This focus is part of a whole described by the source s and the target t. We also have
a lens that can access the focus of a' and b' inside the whole of a and b. We can now
construct a new lens that can access a' and b' inside of s and t. The trick is to realize
230 CHAPTER 17. ENDS AND COENDS
that we can take, as the new residue, a product of the two residues:
a a0
c0
s c c
b0 b
c0
c c t
compLens :: Lens a b a' b' -> Lens s t a b -> Lens s t a' b'
compLens (Lens l2 r2) (Lens l1 r1) = Lens l3 r3
where l3 = assoc' . bimap id l2 . l1
r3 = r1 . bimap id r2 . assoc
The left mapping in the new lens is given by the following composite:
l (id,l2 ) assoc0
s−
→1
(c, a) −−−−→ (c, (c0 , a0 )) −−−−→ ((c, c0 ), a0 )
assoc (id,r2 ) r
((c, c0 ), b0 ) −−−→ (c, (c0 , b0 )) −−−−→ (c, b) −→
1
t
toGet l3 x
> 42
but also replace it with a value of a different type (here, Char):
toSet l3 x 'z'
> ("Outer",(True,'z'))
Category of lenses
Since lenses can be composed, you might be wondering if there is a category in which
lenses serve as hom-sets.
Indeed, there is a category Lens whose objects are pairs of objects in C, and arrows
from hs, ti to ha, bi are elements of Lhs, tiha, bi.
The formula for the composition of existential lenses is too complicated to be useful
in practice. In the next chapter we’ll see an alternative representation of lenses using
Tambara modules, in which composition is just a composition of functions.
p: E → B
q: E × B → E
Transport law
We interpret q as “transporting” an element of the bundle to a new fiber. This is in
accordance with the get/set lens law, or the transport law, that says that “you get what
you set”:
get (set s a) = a
We say that q(s, a) transports s to a new fiber over a:
p−1 a
E
q(s, a)
s
a B
232 CHAPTER 17. ENDS AND COENDS
p ◦ q = π2
E×B
q
ε×id E
p
B
ε: E → 1
and the unit law for the product. Using a comonoid makes it easier to generalize this
construction to a tensor product in a monoidal category.
Identity law
Here’s the set/get law or the identity law. It says that “nothing changes if you set what
you get”:
set s (get s) = s
We can write it in terms of a comonoidal comultiplication:
δ: E → E × E
δ id×p q
E→
− E × E −−−→ E × B →
− E
p−1 a
E
s = q(s, a)
a B
Composition law
Finally, here’s the set/set law, or the composition law. It says that “the last set wins”:
17.10. LENSES AND FIBRATIONS 233
E×B×B
q×id
id×ε×id
E×B E×B
q
q
E
p−1 a p−1 a0
E
s q(a0 , s0 )
s0 = q(s, a)
a B
a0
Type-changing lens
A type-changing lens generalizes transport to act between bundles. We have to define
a whole family of bundles. We start with a category A whose objects define the types
that we will use for the foci of our lens. In Haskell, this is the full category of Haskell
types.
We use a set B as the combined set of all elements of all types. B is fibrated over
A—the projection π sending an element of B to its corresponding type. You may think
of it as the set of objects of the coslice category 1/A.
The bundle of bundles E is a set that’s fibered over B with the projection p. Since
B itself is fiberdd over A, E is transitively fibered over A, with the composite projection
π ◦ p. It’s this coarser fibration that splits E into a family of bundles. Each of these
bundles represents a different Haskell type. A type-changing lens will move between
these bundles.
B p
π A
234 CHAPTER 17. ENDS AND COENDS
p(q(b, s)) = b
The set/get law (identity):
q(p(s), s) = s
The set/set law (composition):
• Ninja Yoneda: Z
Set(C(a, x), F x) ∼
= Fa
x
• Ninja co-Yoneda: Z x
C(x, a) × F x ∼
= Fa
• Day convolution:
Z a,b
(F ? G)x = C(a ⊗ b, x) × F a × Gb
Chapter 18
Tambara Modules
It’s not often that an obscure corner of category theory gains sudden prominence in
programming. Tambara modules got a new lease on life in their application to profunc-
tor optics. They provide a clever solution to the problem of composing optics. We’ve
seen that, in the case of lenses, the getters compose nicely using function composition,
but the composition of setters involves some shenanigans. The existential represen-
tation doesn’t help much. The profunctor representation, on the other hand, makes
composition a snap.
The situation is somewhat analogous to the problem of composing geometric trans-
formations in graphics programming. For instance, if you try to compose two rotations
around two different axes, the formula for the new axis and the angle is quite compli-
cated. But if you represent rotations as matrices, you can use matrix multiplication; or,
if you represent them as quaternions, you can use quaternion multiplication. Profunctor
representation lets you compose optics using straightforward function composition.
2 3
∗ ∗ ∗
5
235
236 CHAPTER 18. TAMBARA MODULES
α
F∗ G∗
Fm Gm
α
F∗ G∗
It’s a relationship between three functions acting on two sets:
Fm Gm
F∗ G∗
α
Gm ◦ α = α ◦ F m
In other words, if you pick an element x ∈ F ∗, you can map it to G∗ using α and then
apply the transformation corresponding to m; or you can first apply the transformation
F m and then map the result using α. The result is the same in both cases.
Such functions are called equivariant. We often call F m the action of m on the set
F ∗. An equivariant function maps an action on one set to its corresponding action on
another set.
α◦g =h◦α
or, pictorially:
g h
G∗ H∗
α
Notice that this will guarantee that actions are mapped to corresponding actions if, for
instance, g = Gm and h = Hm.
Such tuples are exactly the elements of the end:
Z
Set(F ∗, F ∗)
F
Set(id,α) Set(α,id)
Set(G∗, H∗)
Notice that this is the end over the whole functor category [M, Set], so the wedge
condition relates the entries that are connected by natural transformations. In this
case, natural transformations are equivariant functions. The profunctor under the end
is given by:
P hG, Hi = Set(G∗, H∗)
It is a functor:
P : [M, Set]op × [M, Set] → Set
Its action on morphisms (natural transformations) is:
P hα, βi = β ◦ − ◦ α
238 CHAPTER 18. TAMBARA MODULES
If we pick f to be the element of Set(G∗, G∗) and g to be the element of Set(H∗, H∗),
the wedge condition indeed becomes:
α◦g =h◦α
Cayley’s theorem
In group theory, Cayley’s theorem states that every group is isomorphic to a (subgroup
of the) group of permutations. A group is just a monoid in which every element has an
inverse. Permutations are just functions that map a set to itself.
In category theory, Cayley’s theorem is practically built into the definition of a
monoid and its representations.
The connection between the single-object interpretation and the more traditional
set-of-elements interpretation of a monoid is easy to establish. We do this by construct-
ing the functor F : M → Set that maps ∗ to the special set S that is given by the
hom-set: S = M(∗, ∗). Elements of this set are morphisms in M. We define the action
of F on morphisms as post-composition:
(F m)n = m ◦ n
η: 1 → S
µ: S × S → S
The unit picks the element of S that corresponds to id∗ in M(∗, ∗). Multiplication of
two elements m and n is given by the element that corresponds to m ◦ n.
At the same time we can look at S as an image of F : M → Set, in which case it’s
the functions S → S that form a representation of the monoid. This is the essence of
the Cayley’s theorem.
In programming, the best example of applying the Cayley’s theorem is in the efficient
implementation of list reversal. Recall the naive recursive implementation of reversal:
reverse :: [a] -> [a]
reverse [] = []
reverse (a : as) = reverse as ++ [a]
It splits the list into head and tail, reverses the tail, and appends a singleton made out
of the head to the result. The problem is that every append has to traverse the growing
list resulting in O(N 2 ) performance.
Remember, however, that a list is a (free) monoid:
18.1. TANNAKIAN RECONSTRUCTION 239
The two sets of natural transformations here are hom-sets in [C, Set].
Recall the corollary to the Yoneda lemma that works for any category A:
[A, Set](A(x, −), A(y, −)) ∼
= A(y, x)
We can write it using an end:
Z
Set(A(x, z), A(y, z)) ∼
= A(y, x)
z: C
In particular, we can replace A with the functor category [C, Set]. We get:
Z
Set([C, Set](C(a, −), F ), [C, Set](C(b, −), F )) ∼
= [C, Set](C(b, −), C(a, −))
F : [C,Set]
We can then apply the Yoneda lemma again to the right hand side to get:
C(a, b)
which is exactly the sought after result.
It’s important to realize how the structure of the functor category enters the end
through the wedge condition. It does that through natural transformations. Every
time we have a natural transformation between two functors α : G → H, the following
diagram must commute:
R
F Set(F a, F b)
πG πH
Set(id,α) Set(α,id)
Set(Ga, Hb)
18.1. TANNAKIAN RECONSTRUCTION 241
We can immediately translate this result to Haskell. We replace the end by forall.
The left hand side becomes:
forall f. Functor f => f a -> f b
unId :: Id a -> a
unId (Id a) = a
This kind of reconstruction might seem trivial and pointless. Why would anyone
want to replace function type a->b with a much more complicated type:
type Getter a b = forall f. Functor f => f a -> f b
It’s instructive, though, to think of a->b as the precursor of all optics. It’s a lens that
focuses on the b part of a. It tells us that a contains enough information, in one form
or another, to construct a b. It’s a “getter” or an “accessor.”
Obviously, functions compose. What’s interesting though is that functor represen-
tations also compose, and they compose using simple function composition, as seen in
this example:
boolToStrGetter :: Getter Bool String
boolToStrGetter = toRep (show) . toRep (bool 0 1)
Other optics don’t compose so easily, but their functor (and profunctor) representations
do.
242 CHAPTER 18. TAMBARA MODULES
Pointed getter
Here’s a little toy example in Haskell that illustrates the need for more interesting
reconstructions. It’s an optic that can either act as a getter or it can return a default
value.
data PtdGetter s t = Pt t | Fun (s -> t)
We can apply this getter to a source value and get a result of type t:
apply :: PtdGetter s t -> s -> t
apply (Pt t) _ = t
apply (Fun g) s = g s
These getters compose, but their composition is non-trivial:
composePG :: PtdGetter x t -> PtdGetter s x -> PtdGetter s t
composePG (Pt t) _ = Pt t
composePG (Fun g) (Pt x) = Pt (g x)
composePG (Fun g) (Fun g') = Fun (g . g')
The composition is associative, and there is an identity getter:
idPG :: PtdGetter a a
idPG = Fun id
so we do have a category in which pointed getters form hom-sets.
The functor representation for this toy optic exists, but we have to restrict the type
of functors over which we take the end. Here’s the definition of a class of Pointed
functors:
class Functor f => Pointed f where
eta :: a -> f a
Our PointedGetter is represented by the following Tannakian-like formula:
type PtdGetterF s t = forall f. Pointed f => f s -> f t
This time we are defining a function that is polymorphic not over all functors but over
a restricted class of Pointed functors.
As before, we can apply this optic to retrieve the target. The trick is to encapsulate
the source in the identity functor:
applyF :: PtdGetterF s t -> s -> t
applyF g = unId . g . Id
This works because the identity functor is pointed:
instance Pointed Id where
eta = Id
The equivalence of the two formulations is witnessed by this pair of functions:
toPGF :: PtdGetter s t -> PtdGetterF s t
toPGF (Pt t) = \_ -> eta t
toPGF (Fun g) = fmap g
This time, however, the functor representation has definite advantage over the original:
a composition of two PtdGetterF optics is just function composition.
Exercise 18.1.1. Define two composable PtdGetter optics—for instance, one going
from a pair (Int, Bool) to Int and another from Int to String. Compose them first
using composePG, then convert them to the functor representation, and compose them
using function composition.
η : Id → P
They form their own category, let’s call it Ptd, with morphisms that are natural trans-
formations that preserve the structure. Such a transformation α : (P, η) → (P 0 , η 0 ) must
make the following triangle commute:
a
η η0
αa
Pa P 0a
(F Q)a = a + Qa
ηa = Left
The trick in generalizing the Tannakian reconstruction is to define the end over a
specialized functor category T , but applying the forgetful functor to its functors. We
assume that we have the free/forgetful adjunction F a U between T and [C, Set]:
T (F Q, P ) ∼
= [C, Set](Q, U P )
The mapping T → Set parameterized by the object a, and given by the formula:
P 7→ (U P )a
244 CHAPTER 18. TAMBARA MODULES
is sometimes called a fiber functor, so the end formula can be interpreted as a set
of natural transformations between two fiber functors. Conceptually, a fiber functor
describes an infinitesimal neighborhood of an object. It maps functors to sets but,
more importantly, it maps natural transformations to functions. These functions probe
the environment in which the object lives. In particular, natural transformations in T
are involved in wedge conditions that define the end. In calculus, stalks of sheaves play
a very similar role.
As we did before, we first apply the Yoneda lemma to get:
Z
Set [C, Set] C(a, −), U P , [C, Set] C(s, −), U P
P: T
We can now use the adjunction:
Z
Set T F C(a, −), P , T F C(s, −), P
P: T
We end up with a mapping between two natural transformations in the functor category
T . We can simplify it using the corollary to the Yoneda lemma, resulting in:
T F C(s, −), F C(a, −)
We can apply the adjunction once more:
T C(s, −), (U ◦ F )C(a, −)
and the Yoneda lemma again:
(U ◦ F )C(a, −) s
The final observation is that the compostion U ◦ F of adjoint functors is a monad in
the functor category. Let’s call this monad Φ. The result is the following identity that
will serve as the foundation for profunctor optics:
Z
Set (U P )a, (U P )s ∼
= ΦC(a, −) s
P: T
The right-hand side is the action of the monad Φ = U ◦ F on the representable functor
C(a, −) evaluated at s.
Compare this with the earlier formula for Tannakian reconstruction, especially if we
rewrite it in the following form:
Z
Set(F a, F s) ∼
= C(a, −)s
F : [C,Set]
Keep in mind that, in the derivation of optics, we’ll replace a and s with pairs of
objects ha, bi and hs, ti from C op × C. In that case our functors will become profunctors.
Going back to our toy example, the monad Φ is given by:
(ΦQ)s = s + Qs
which is just the action of the free functor F followed by forgetting the η. Replacing Q
with the representable C(a, −) we get:
s + C(a, s)
In Haskell, this translates directly to our PtdGetter.
The bottom line is that we were able to reconstruct a hom-set in a category of
simple optics from the category of functors with some additional structure.
18.2. PROFUNCTOR LENSES 245
Notice that the pair of hom-sets in this formula can be seen as a single hom-set in the
product category C op × C:
Z c
Lhs, tiha, bi = (C op × C)(c • ha, bi, hs, ti)
Iso
As a quick check of this idea, let’s apply our reconstruction formula to the simple case
of T = C op × C. In that case we don’t need to use the forgetful functors, or the monad
Φ, and we get:
Z
Set P ha, bi, P hs, ti ∼= (C op × C)(ha, bi, −) hs, ti
Ohs, tiha, bi =
P : C op ×C
P hf, gi : P hc × a, c × bi → P hs, ti
P ha, bi → P hc × a, c × bi
And this is exactly the additional structure we shall demand from our profunctor class.
Tambara module
A profunctor P equipped with the family of transformations:
αha,bi,c : P ha, bi → P hc × a, c × bi
P hc0 , ci
P hf,ci P hc0 ,f i
P hc, ci P hc0 , c0 i
αc αc0
Qhc, ci Qhc0 , c0 i
P hc,f i P hf,ci
Qhc, c0 i
P ha, bi
αha,bi,c αha,bi,c0
P hc × a, c × bi P hc0 × a, c0 × bi
αha,bi,c0 ×c ∼
= αhc×a,c×bi,c0 ◦ αha,bi,c
or, pictorially:
αha,bi,c0 ×c
P ha, bi P hc0 × c × a, c0 × c × bi
αha,bi,c αhc×a,c×bi,c0
P hc × a, c × bi
(Q, β). We can either apply α and then ρ, or do ρ first and then β. We want the result
to be the same:
αha,bi,c
P ha, bi P hc × a, c × bi
ρha,bi ρhc×a,c×bi
βha,bi,c
Qha, bi Qhc × a, c × bi
Keep in mind that the structure of the Tambara category is encoded in these natural
transformations. They will determine, through the wedge condition, the shape of the
ends that enter the definition of profunctor lenses.
Profunctor lenses
Now that we have some intuition about how Tambara modules are related to lenses,
let’s go back to our main formula:
Z
Set (U P )ha, bi, (U P )hs, ti ∼= Φ(C op × C)(ha, bi, −) hs, ti
Lhs, tiha, bi =
P: T
This time we’re taking the end over the Tambara category. The only missing part is
the monad Φ = U ◦ F or the functor F that freely generates Tambara modules.
It turns out that, instead of guessing the monad, it’s easier to guess the comonad.
There is a comonad in the category of profunctors that takes a profunctor P and
produces another profunctor ΘP . Here’s the formula:
Z
(ΘP )ha, bi = P hc × a, c × bi
c
You can check that this is indeed a comonad by implementing ε and δ (extract and
duplicate). For instance, ε maps ΘP → P using the projection π1 for the terminal
object (the unit of cartesian product).
What’s interesting about this comonad is that its coalgebras are Tambara modules.
Again, these are coalgebras that map profunctors to profunctors. They are natural
transformations P → ΘP . We can write such a natural transformation as an end:
Z Z Z
Set P ha, bi, (ΘP )ha, bi = Set P ha, bi, P hc × a, c × bi
a,b a,b c
I used the continuity of the hom-functor to pull out the end over c. The resulting end
encodes a set of natural (dinatural in c) transformations that define a Tambara module:
αha,bi,c : P ha, bi → P hc × a, c × bi
In fact, these coalgebras are comonad coalgebras, that is they are compatible with the
comonad Θ. In other words, Tambara modules form the Eilenberg-Moore category of
coalgebras for the comonad Θ.
The left adjoint to Θ is a monad Φ given by the formula:
Z u,v,c
(C op × C) c • hu, vi, hs, ti × P hu, vi
(ΦP )hs, ti =
This adjunction can be easily verified using some end/coend manipulations. The
mapping out of ΦP to some profunctor Q can be written as an end. The coends in
Φ can then be taken out using co-continuity of the hom-functor. Finally, applying the
ninja-Yoneda lemma produces the mapping into ΘQ. We get:
[(C op × C, Set](P Φ, Q) ∼
= [(C op × C, Set](P, ΘQ)
Replacing Q with P we immediately see that the set of algebras for Φ is isomorphic
to the set of coalgebras for Θ. In fact they are monad algebras for Φ. This means that
the Eilenberg-Moore category for the monad Φ is the same as the Tambara category.
Recall that the Eilenberg-Moore construction factorizes a monad into a free/forgetful
adjunction. This is exactly the adjunction we were looking for when deriving the formula
for profunctor optics. What remains is to evaluate the action of Φ on the representable
functor:
Z u,v,c
Φ(C op × C)(ha, bi, −) hs, ti = (C op × C) c • hu, vi, hs, ti × (C op × C) ha, bi, hu, vi
-- p s t -> p a b
lens3 = lens2 . lens1
αha,bi,c : P ha, bi → P hc ⊗ a, c ⊗ bi
You can easily convince yourself that all coherency laws translate directly to this case,
and the derivation of profunctor optics works without change.
Prisms
From the programming point of view there are two obvious monoidal structures to
explore: the product and the sum. We’ve seen that the product gives rise to lenses.
The sum, or the coproduct, gives rise to prisms.
We get the existential representation simply by replacing the product by the sum
in the definition of a lens:
Z c
Phs, tiha, bi = C(s, c + a) × C(c + b, t)
To simplify this, notice that the mapping out of a sum is equivalent to the product of
mappings:
Z c Z c
∼
C(s, c + a) × C(c + b, t) = C(s, c + a) × C(c, t) × C(b, t)
Using the co-Yoneda lemma, we can get rid of the coend to get:
C(s, t + a) × C(b, t)
Traversals
A traversal is a type of optic that focuses on multiple foci at once. Imagine, for instance,
that you have a tree that can have zero or more leaves of type a. A traversal should be
able to get you a list of those nodes. It should also let you replace these nodes with a
new list. And here’s the problem: the length of the list that delivers the replacements
must match the number of nodes, otherwise bad things happen.
A type-safe implementation of a traversals would require us to keep track of the
sizes of lists. In other words, it would require dependent types.
In Haskell, a (non-type-safe) traversal is often written as:
type Traversal s t a b = s -> ([b] -> t, [a])
with the understanding that the sizes of the two lists are determined by s and must be
the same.
When translating traversals to categorical language, we’ll express this condition
using a sum over the sizes of the list. A counted list of size n is an n-tuple, or an
element of an , so we can write:
X
(Set(bn , t) × an )
T Rhs, tiha, bi = Set s,
n
The existential form of a traversal must take into account the fact that the residues
for different n’s should have, in principle, different types. For instance, you can decom-
pose a tree into an n-tuple of leaves an and the residue cn with n holes. So the correct
existential representation for a traversal must involve a coend over all sequences cn that
are indexed by natural numbers:
Z cn X X
T Rhs, tiha, bi = C(s, cm × am ) × C( ck × bk , t)
m k
To show the equivalence of the two representations, we first write the mapping out
of a sum as a product of mappings:
Z c : [N ,C] X Y
C(s, cm × am ) × C(ck × bk , t)
m k
Here, [bk , t] is the internal hom, which is an alternative notation for the exponential
k
object tb .
The next step is to recognize that a product in this formula represents a set of
natural transformations in [N , C]. Indeed, we could write it as an end:
Z
∼
Y
k
C(ck , [b , t] = C(ck , [bk , t])
k k:N
k 7→ ck
k 7→ [bk , t]
18.4. GENERAL OPTICS 253
We can now use the co-Yoneda lemma in the functor category [N , C]:
Z c : [N ,C]
cm × am ) × [N , C] c− , [b− , t] ∼
X X
[bm , t] × am )
C(s, = C(s,
m m
This result is more general than our original formula, but it turns into it when restricted
to the category of sets.
To derive a profunctor representation for traversals, we should look more closely
at the kind of transformations that are involved. We define the action of a functor
c : [N , C] on a as: X
c•a= cm × am
m
These actions can be composed by expanding the formula using distributivity laws:
X X
c • (c0 • a) = cm × ( c0n × an )m
m n
If the target category is Set, this is equivalent to the following Day convolution (for
non-Set categories, one could use the enriched version of Day convolution):
Z m,n
(c ? c0 )k = N (m + n, k) × cm × c0n
αha,bi,c : P ha, bi → P hc • a, c • bi
It turns out that the original derivation of profunctor optics still works for these gener-
alized Tambara modules, and traversals can be written as polymorphic functions:
Z
T Rhs, tiha, bi = Set (U P )ha, bi, (U P )hs, ti
P: T
•: M × C → C
254 CHAPTER 18. TAMBARA MODULES
•: M × D → D
We can then define the mixed optics as:
Z m: M
Ohs, tiha, bi = C(s, m • a) × D(m • b, t)
P : C op × D → Set
and the corresponding Tambara modules that use two separate actions:
αha,bi,m : P ha, bi → P hm • a, m • bi
Exercise 18.4.1. What are the mixed optics for the action of the cartesian product
when one of the categories is the terminal category? What if the first category is C op × C
and the second is terminal?
Chapter 19
Kan Extensions
If category theory keeps raising levels of abstraction it’s because it’s all about discovering
patterns. Once patterns are discovered, it’s time to study patterns formed by these
patterns, and so on.
We’ve seen the same recurring concepts described more and more tersely at higher
and higher levels of abstraction.
For instance, we first defined the product using a universal construction. Then we
saw that the spans in the definition of the product were natural transformations. That
led to the interpretation of the product as a limit. Then we saw that we can define it
using adjunctions. We were able to combine it with the coproduct in one terse formula:
(+) a ∆ a (×)
Lao Tzu said: “If you want to shrink something, you must first allow it to expand.”
Kan extensions raise the level of abstraction even higher. Mac Lane said: “All
concepts are Kan extensions.”
εb,c : [b, c] × b → c
suggests that [b, c] embodies, in a sense, the inverse of multiplication. It plays a similar
role as c/b in:
c/b × b = c
In a typical categorical manner, we may ask the question: What if we replace the
product with something else? The obvious thing, replacing it with a coproduct, doesn’t
255
256 CHAPTER 19. KAN EXTENSIONS
work (thus we have no analog of subtraction). But maybe there are other well-behaved
binary operations that have a right adjoint.
A natural setting for generalizing a product is a monoidal category with a tensor
product ⊗ and a unit object I. If we have an adjunction:
C(a ⊗ b, c) ∼
= C(a, [b, c])
we’ll call the category closed monoidal. In a typical categorical abuse of notation,
unless it leads to confusion, we’ll use the same symbol (a pair of square brackets) for
the monoidal internal hom as we did for the cartesian hom.
The definition of an internal hom works well for a symmetric monoidal category.
If the tensor product is not symmetric, the adjunction defines a left closed monoidal
category. The left internal hom is adjoint to the “post-multiplication” functor (− ⊗ b).
The right-closed structure is defined as the right adjoint to the “pre-multiplication”
functor (b ⊗ −). If both are defined than the category is called bi-closed.
We can then use the currying adjunction in Set (the square brackets stand for the
internal hom in Set):
Z
Set F a, [C(a ⊗ b, x) × Gb, Hx]
x,a,b
Finally, we use the continuity of the hom-set to move the two ends inside the hom-set:
Z Z
Set F a, [C(a ⊗ b, x) × Gb, Hx]
a x,b
Set(A × B, C) ∼
= Set(A, [B, C]) ∼
= Set A, Set(B, C)
We can generalize the above adjunction to the case where B and C are not sets but
objects in some category C. The external hom in any category is always a set. Such an
adjunction defines the action of a set A on an object b:
C(A · b, c) ∼
= Set A, C(b, c)
or a co-power.
You may think of this action as adding together (taking a coproduct of) A copies
of b. In particular, if A is a two-element set 2, we get:
C(2 · b, c) ∼
= Set 2, C(b, c) ∼= C(b, c) × C(b, c) ∼
= C(b + b, c)
In other words,
2·b∼
=b+b
In this sense a co-power defines multiplication as iterated addition.
If we multiply b by the hom-set C(b, c) and take the coend over b’s, the result is
isomorphic to c:
Z b
C(b, c) · b ∼
=c
Indeed, the mappings to an arbitrary x from both sides are isomorphic due to the
Yoneda lemma:
Z b Z
∼
C(b, c) · b, x = Set C(b, c), C(b, x) ∼
C = C(c, x)
b
Set(A · B, C) ∼
= Set A, Set(B, C) ∼
= Set(A × B, C)
C(b, A t c) ∼
= Set A, C(b, c)
You may think of the power as multiplying together A copies of c. Indeed, replacing A
with 2 results in:
C(b, 2 t c) ∼
= Set 2, C(b, c) ∼= C(b, c) × C(b, c) ∼
= C(b, c × c)
In other words:
2tc∼
=c×c
which is a fancy way of writing c2 .
If we power c by the hom-set C(c0 , c) and take an end over all c’s, the result is
isomorphic to c0 : Z
∼ c0
C(c0 , c) t c =
c
258 CHAPTER 19. KAN EXTENSIONS
This follows from the Yoneda lemma. Indeed the mappings from any x to both sides
are isomorphic:
Z Z
C x, C(c , c) t c = Set C(c0 , c), C(x, c) ∼
0 ∼ = C(x, c0 )
c c
In Set, the power decays to the exponential, which is the same as the hom-set:
AtC∼
= CA ∼
= Set(A, C)
Set(B, A t C) ∼
= Set(A, Set(B, C)) ∼
= Set(A × B, C)
∼
= Set(B × A, C) ∼
= Set(B, Set(A, C))
Rc = d
Rc0 = d
L has no chance of undoing it. It can’t map d to both c and c0 . The best it can do is
to map d to an object Ld that has arrows to both c and c0 . These arrows are needed
to define the components of the counit of the adjunction:
εc : Ld → c
εc0 : Ld → c0
L
Ld
εc
εc0 c d
R
c0
counit constrain possible choices, as long as there are arrows connecting these objects
to the image of R.
Obviously, all these constraints mean that an adjunction can only be defined in very
special cases. Kan extensions are even weaker than adjunctions.
If adjoint functors work like inverses, Kan extensions work like fractions.
This is best seen if we redraw the diagrams defining the counit and the unit of an
adjunction. In the first diagram, L seems to play the role of 1/R. In the second diagram
R pretends to be 1/L.
Id Id
C C D D
ε
R L η
L R
D C
The right Kan extension RanP F and the left Kan extension LanP F generalize these
by replacing the identity functor with some functor F : C → D. The Kan extensions
then play the role of fractions F/P . Conceptually, they undo the action of P and follow
it with the action of F .
F F
C D C D
ε
P P η
RanP F LanP F
B B
Just like with adjunctions, the “undoing” is not complete. The composition RanP F ◦
P doesn’t reproduce F ; instead it’s related to it through the natural transformation ε
called the counit. Similiarly, the composition LanP F ◦ P is related to F through the
unit η.
Notice that the more information F discards, the easier it is for Kan extensions to
“invert” the functor P . In as sense, it only has to invert P “modulo F ”.
Here’s the intuition behind Kan extensions. We start with a functor F :
F
C D
There is a second functor P that embeds C in another category B. This embedding
may be lossy and non-surjective. Our task is to extend the definition of F to the whole
of B.
In the ideal world we would like the following diagram to commute:
F
C D
P
KanP F
B
But that would involve equality of functors, which is something we try to avoid at all
cost.
The next best thing would be to ask for a natural isomorphism between the two
paths through this diagram. But even that seems like asking too much. So we finally
settle down on demanding that one path be deformable into another, meaning there is
a one-way natural transformation between them. The direction of this transformation
distinguishes between right and left Kan extensions.
260 CHAPTER 19. KAN EXTENSIONS
ε : RanP F ◦ P → F
F
C D
ε
P
RanP F
B
The pair (RanP F, ε) is universal among such pairs (G, α), where G is a functor
G : B → D and α is a natural transformation:
α: G ◦ P → F
F
C D
α
P
G
B
Universality means that for any such (G, α) there is a unique natural transformation
σ : G → RanP F
F
C D
G
σ
P
RanP F
B
which factorizes α, that is:
α = ε · (σ ◦ P )
Notice that this is a combination of vertical and horizontal compositions of natural
transformations in which σ ◦ P is the whiskering of σ. Here’s the same equation in
terms of string diagrams:
F F
C D C D
ε
= Ran
α
σ
B B
P G P G
[C, D](G ◦ P, F ) ∼
= [B, D](G, RanP F )
19.3. RIGHT KAN EXTENSION 261
For every α that is an element of the left-hand side, there is a unique σ that is an
element of the right-hand side.
In other words, the right Kan extension, if it exists for every F , is the right adjoint
to functor pre-composition:
(− ◦ P ) a RanP
The component of the counit of this adjunction at F is ε.
This is somewhat reminiscent of the currying adjunction:
C(a × b, c) ∼
= C(a, [b, c])
in which the product is replaced by functor composition. (Of course, composition can
be considered a tensor product only in the category of endofunctors.)
D
J C
γ
!
X
1
The following diagrams illustrate this. On the left we have two categories: 1 with
a single object ∗, and J with three objects forming a shape for the diagram. On the
right we have the image of D and the image of X◦!, which is the apex x. The three
components of γ connect the apex x with the diagram. Naturality of γ ensures that all
the triangles commute.
∗ x
γ1 γ2
γ3
1 2 D1 D2
3 D3
The right Kan extension (Ran! D, ε) is the universal such cone. Ran! D is a functor
from 1 to C, so it selects an object in C. This is the apex LimD of the universal cone.
262 CHAPTER 19. KAN EXTENSIONS
Universality means that for any pair (X, γ) there is a natural transformation σ : X →
Ran! D
D
J C
X
σ
!
Ran! D
1
which factorizes γ.
σ has only one component σ∗ , which is an arrow h connecting the apex x to the
apex LimD. The factorization:
γ = ε · (σ◦!)
reads, in components:
γ i = εi ◦ h
It makes the triangles in the following diagram commute:
x
h
γ1 γ2
LimD γ3
ε2
ε1
D1 ε3 D2
D3
This makes LimD the limit of the diagram D.
Here, F is a co-presheaf, that is a functor from B to Set. The right Kan extensions of
F along P generalizes this formula:
Z
(RanP F )b ∼
= Set B(b, P c), F c
c
The proof essentially writes itself: at every step there is only one thing to do. We
start with the adjunction:
[C, D](G ◦ P, F ) ∼
= [B, D](G, RanP F )
use the continuity of the hom-functor to pull the end to the front:
Z Z
∼
= D Gb, B(b, P c) t F c
b c
B(b, P c) ∼
= C(P −1 b, c)
we could use the ninja Yoneda lemma to perform the end and get:
(RanP F )b ∼
= (F ◦ P −1 )b
Since the adjunction is a weakening of the idea of an inverse, this result is in agreement
with the intuition that the Kan extension inverts P .
264 CHAPTER 19. KAN EXTENSIONS
L∼
= RanR Id
Indeed, the counit of the Kan extension is the same as the counit of the adjunction:
Id
C C
ε
R
L
D
We also have to show universality:
R
C D
Id
C C G
α σ
R R
G L
D
D
η : Id → R ◦ L
σ = (α ◦ L) · (G ◦ η)
We could ask the converse question: if RanR Id exists, is it automatically the left
adjoint to R? It turns out that we need one more condition for that: The Kan extension
must be preserved by R, that is:
R ◦ RanR Id ∼
= RanR R
We’ll see in the next section that the right-hand side of this condition defines the
codensity monad.
α = ε · (σ ◦ R)
for the σ that was defined above. Hint: draw the corresponding string diagrams and use
the triangle identity for the adjunction.
19.3. RIGHT KAN EXTENSION 265
Codensity monad
We’ve seen that every adjunction L a F produces a monad F ◦ L. It turns out that this
monad is the right Kan extension of F along F . Interestingly, even if F doesn’t have a
left adjoint, the Kan extension RanF F is still a monad called the codensity monad T F :
T F = RanF F
η : Id → T F
µ: TF ◦ TF → TF
Both follow from universality:
F
C D
F
C D G
α σ
F F
D G T F =RanF F
To get the unit, we replace G with the identity functor Id and α with the identity
natural transformation.
To get multiplication, we replace G with T F ◦ T F and note that we have at our
disposal the counit of the Kan extension:
ε: TF ◦ F → F
α: TF ◦ TF ◦ F → F
as the composite:
id◦ε ε
T F ◦ T F ◦ F −−→ T F ◦ F →
− F
or, using whiskering notation:
α = ε · (T F ◦ ε)
Let’s now show that, if we start from an adjunction:
C(Ld, c) ∼
= D(d, F c)
[C, D](G ◦ F, F ) ∼
= [D, D](G, RanF F )
266 CHAPTER 19. KAN EXTENSIONS
The trick is to rewrite the right-hand side by inserting the Yoneda lemma. Taking the
end over c has the effect of replacing c with Ld:
Z Z
∼
= Set C(Ld, c), D(Gd, F c)
d c
The result is the left hand side of the identity we set out to prove.
Since every monad can be derived from an adjunction, it follows that every monad
is also a codensity monad for this adjunction.
Translating the codensity monad to Haskell, we get:
type Codensity f a = forall c. (a -> f c) -> f c
This is very much like a continuation monad. In fact it turns into continuation monad if
we choose f to be the identity functor. We can think of Codensity as taking a callback
(a -> f c) and calling it when the result of type a becomes available.
Here’s the monad instance:
instance Monad (Codensity f) where
return x = \k -> k x
m >>= kl = \k -> m (\a -> (kl a) k)
Again, this is almost exactly like the continuation monad:
instance Monad (Cont r) where
return x = Cont (\k -> k x)
m >>= kl = Cont (\k -> runCont m (\a -> runCont (kl a) k))
This is why Codensity has the same performance advantages as the continuation pass-
ing style. Since it nests continuations “inside out,” it can be used to optimize long
chains of binds that are produced by do blocks.
This is especially important when using free monads, which accumulate binds in
tree-like structures. When we finally interpret a free monad, these accumulated binds
require traversing the ever growing tree. For every bind, the traversal starts from the
root. Compare this with the earlier example of reversing a list, which was optimized by
accumulating functions in a FIFO queue. The codensity monad offers the same kind of
performance improvement.
19.4. LEFT KAN EXTENSION 267
[B, D](LanP F, G) ∼
= [C, D](F, G ◦ P )
(There are also adjoints to post-composition: they are called Kan lifts.)
Alternatively, LanP F is a functor equipped with a natural transformation called the
unit:
η : F → LanP F ◦ P
F
C D
P η
LanP F
B
The pair (LanP F, η) is universal, meaning that, for any other pair (G, α), where
α: F → G ◦ P
F
C D
G
P σ
LanP F
B
that factorizes α, that is:
α = (σ ◦ P ) · η
P G P G
B B
σ
α
= Lan
η
C D C D
F F
[C, D](F, G ◦ P ) ∼
= [B, D](LanP F, G)
268 CHAPTER 19. KAN EXTENSIONS
! γ
X
1
Here’s an illustrative example of a simple shape consisting of three objects and three
morphisms (not counting identities). The object x is the image of the single object ∗
under the functor X:
1 2 D1 D2
3 D3
γ1 γ2
γ3
∗ x
The colimit is the universal cocone, which is given by the left Kan extension:
Colim D = Lan! D
We can now use the Yoneda lemma to integrate over b, replacing b with P c:
Z
D(F c, G(P c)) ∼
= [C, D](F, G ◦ P )
c
As long as the coend in question exists, it indeed gives us the left adjoint to functor
pre-composition:
[B, D](LanP F, G) ∼= [C, D](F, G ◦ P )
As expected, in Set, the co-power decays to a cartesian product:
Z c
∼
(LanP F ) b = B(P c, b) × F c
When translating this formula to Haskell, we replace the coend with the existential
type. Symbolically:
type Lan p f b = exists c. (p c -> b, f c)
Currently, this is how we would encode the existential:
data Lan p f e where
Lan :: (p c -> b) -> f c -> Lan p f b
If the functor P has a right adjoint, let’s call it P −1 :
B(P c, b) ∼
= C(c, P −1 b)
(LanP F ) b ∼
= (F ◦ P −1 )b
R∼
= LanL Id
Conversely, if the left Kan extension of identity exists and it preserves the functor L:
L ◦ LanL Id ∼
= LanL L
270 CHAPTER 19. KAN EXTENSIONS
than LanL Id is the right adjoint of L. (Incidentally LanF F is called the density
comonad.)
The unit of Kan extension is the same as the unit of the adjunction:
Id
D D
L η
C R
The proof of universality is analogous to the one for the right Kan extension as the left
adjoint.
Co-presheaves, that is functors in [C, Set], can also be tensored using an external tensor
product. An external product of two objects, instead of producing an object in the same
category, picks an object in a different category. In our case, the product of two functors
ends up in the category of co-presheaves on C × C:
¯ : [C, Set] × [C, Set] → [C × C, Set]
⊗
The product of two co-presheaves acting on a pair of objects in C × C, is given by the
formula:
¯
(F ⊗G)ha, bi = F a × Gb
It turns out that Day convolution of two functors can be expressed as a left Kan
extension of their external product along the tensor product in C:
F ?G∼ ¯
= Lan⊗ (F ⊗G)
Pictorially:
¯
F ⊗G
C×C Set
⊗
C ¯
Lan⊗ (F ⊗G)
Using the coend formula for the left Kan extension we get:
Z ha,bi
¯
(Lan⊗ (F ⊗G))c ∼
= ¯
C(a ⊗ b, c) · (F ⊗G)ha, bi
Z ha,bi
∼
= C(a ⊗ b, c) · (F a × Gb)
Since the two functors are Set-valued, the co-power decays to the cartesian product:
Z ha,bi
∼
= C(a ⊗ b, c) × F a × Gb
where
Yha,bi hc, di = (C op × D)(ha, bi, hc, di) = C(c, a) × D(b, d)
is the Yoneda functor.
Indeed, by definition, we have:
Z m Z m,hc,di
∼
(Lanm• Yha,bi hs, ti = (C op × D)(m • hc, di, hs, ti) · Yha,bi hc, di
C op × D Lanhm,ni• Yha,bi
In general, with the Yoneda functor replaced by a general profunctor, the profunctor-
functor: Z m
ΦP = Lanm• P
and use the adjunction that defines the left Kan extension:
Z
∼
= Nat(P, Q ◦ (m • −))
m
• Power:
C(b, A t c) ∼
= Set A, C(b, c)
[C, D](G ◦ P, F ) ∼
= [B, D](G, RanP F )
Z
∼
(RanP F )e = B(e, P c) t F c
c
[B, D](LanP F, G) ∼
= [C, D](F, G ◦ P )
Z c
(LanP F )e ∼
= B(P c, e) · F c
Enrichment
Set-theoretical foundations
Category theory is very frugal at its foundations. But it (reluctantly) draws upon set
theory. In particular the idea of the hom-set, defined as a set of arrows between two
objects, drags in set theory as the prerequisite to category theory. Granted, arrows
form a set only in a locally small category, but that’s a small consolation, considering
that dealing with things that are too big to be sets requires even more theory.
It would be nice if category theory could bootstrap itself, for instance by replacing
hom-sets with more general objects. That’s the idea behind enriched categories. These
hom-object, though, have to come from some other category that has hom-sets and, at
some point we have to fall back on set-theoretical foundations. Nevertheless, having the
option of replacing stuctureless hom-sets with something different expands our ability
to model more complex systems.
Virtually all that we’ve learned about categories can be translated into the realm
of enriched categories. A lot of categorical reasoning involves commuting diagrams,
which express the equality of arrows. In the enriched setting we don’t have arrows
going between objects, so all these constructions will have to be modified.
273
274 CHAPTER 20. ENRICHMENT
Hom-Objects
At first sight, replacing hom-sets with objects might seem like a step backward. After
all, sets have elements, while objects are formless blobs. However, the richness of hom-
objects is encoded in the morphisms of the category they come from. Conceptually,
the fact that sets are structure-less means that there are lots of morphisms (functions)
between them. Having fewer morphisms often means having more structure.
The guiding principle in defining enriched categories is that we should be able to
recover ordinary category theory as a special case. After all hom-sets are objects in the
category Set. In fact we’ve worked really hard to express properties of sets in terms of
functions rather than elements.
Having said that, the very definition of a category in terms of composition and
identity involves morphisms that are elements of hom-sets. So let’s first re-formulate
the primitives of a category without recourse to elements.
Composition of arrows can be defined in bulk as a function between hom-sets:
Instead of talking about the identity arrow, we can use a function from the singleton
set:
ja : 1 → C(a, a)
This shows us that, if we want to replace hom-sets C(a, b) with objects from some
category V, we have to be able to multiply these objects to define composition, and we
need some kind of unit object to define identity. We could ask for V to be cartesian
but, in fact, a monoidal category works just fine. The unit and associativity laws of a
monoidal category translate directly to identity and associativity laws for composition.
Enriched Categories
Let V be a monoidal category with a tensor product ⊗, a unit object I, and the
associator and two unitors (as well as their inverses):
α : (a ⊗ b) ⊗ c → a ⊗ (b ⊗ c)
λ: I ⊗ a → a
ρ: a ⊗ I → a
A category C enriched over V has objects and, for any pair of objects a and b, a hom-
object C(a, b). This hom-object is an object in V. Composition is defined using arrows
in V:
◦ : C(b, c) ⊗ C(a, b) → C(a, c)
ja : I → C(a, a)
20.1. ENRICHED CATEGORIES 275
C(a, d)
λ ρ
I ⊗ C(a, b) C(a, b) C(a, b) ⊗ I C(a, b)
◦ ◦
jb ⊗id id⊗ja
Notice that these are all diagrams in V, where we do have arrows. We still fall back on
set theory, but at a different level.
A category enriched over V is also called a V-category. In what follows we’ll assume
that the enriching category is symmetric monoidal, so we can form opposite and product
categories.
The category C op opposite to a V-category C is obtained by reversing hom-objects,
that is:
C op (a, b) = C(b, a)
Composition in the opposite category involves reversing the order of hom-objects, so it
only works if the tensor product is symmetric.
We can also define a tensor product of V-categories; again, provided that V is
symmetric. The product of two V-categories C ⊗ D has, as objects, pairs of objects, one
from each category. The hom-objects are defined to be tensor products:
◦ : C(c0 , c00 , ) ⊗ D(d0 , d00 ) ⊗ C(c, c0 ) ⊗ D(d, d0 ) → C(c, c00 ) ⊗ D(d, d00 )
Exercise 20.1.2. Show that every V-category C has an underlying ordinary category C0
whose objects are the same, but whose hom-sets are given by (monoidal global) elements
of the hom-objects, that is elements of V(I, C(a, b)).
276 CHAPTER 20. ENRICHMENT
Examples
Seen from this new perspective, the ordinary categories we’ve studied so far were triv-
ially enriched over the monoidal category (Set, ×, 1), with the cartesian product as the
tensor product, and the singleton set as the unit.
Interestingly, a 2-category can be seen as enriched over Cat. Indeed, 1-cells in a
2-category are themselves objects in another category. The 2-cells are just arrows in
that category. In particular the 2-category Cat of small categories is enriched in itself.
It’s hom-objects are functor categories, which are objects in Cat.
Preorders
Enrichment doesn’t always mean adding more stuff. Sometimes it looks more like
impoverishment, as is the case of enriching over a walking arrow category.
This category has just two objects which, for the purpose of this construction, we’ll
call False and True. There is a single arrow from False to True (not counting identity
arrows), which makes False the initial object and True the terminal one.
idFalse idTrue
!
False True
To make this into a monoidal category, we define the tensor product, such that:
True ⊗ True = True
and all other combinations produce False. True is the monoidal unit, since:
True ⊗ x = x
A category enriched over the monoidal walking arrow is called a preorder. A hom-
object C(a, b) between any two objects can be either False or True. We interpret True
to mean that a precedes b in the preorder, which we write as a ≤ b. False means that
the two objects are unrelated.
The important property of composition, as defined by:
C(b, c) ⊗ C(a, b) → C(a, c)
is that, if both hom-objects on the left are True, then the right hand side must also be
True. (It can’t be False, because there is no arrow going from True to False.) In the
preorder interpretation, it means that ≤ is transitive:
b ≤ c ∧ a ≤ b =⇒ a ≤ c
By the same reasoning, the existence of the identity arrow:
ja : True → C(a, a)
means that C(a, a) is always True. In the preorder interpretation, this means that ≤ is
reflexive, a ≤ a.
Notice that a preorder doesn’t preclude cycles and, in particular, it’s possible to
have a ≤ b and b ≤ a without a being equal to b.
A preorder may also be defined without resorting to enrichment as a thin category—
a category in which there is at most one arrow between any two objects.
20.2. V-FUNCTORS 277
Self-enrichment
Any cartesian closed category V can be viewed as self-enriched. This is because every
external hom-set C(a, b) can be replaced by the internal hom ba (the object of arrows).
In fact every monoidal closed category V is self-enriched. Recall that, in a monoidal
closed category we have the hom-functor adjunction:
V(a ⊗ b, c) ∼
= V(a, [b, c])
εb,c : [b, c] ⊗ b → c
The trick is to consider the whole hom-set at once and show that we can always pick a
canonical element in it. We start with the set:
ja : I → [a, a]
Again, we can pick it as a member of the hom-set V(I, [a, a]). We use the adjunction:
We know that this hom-set contains the left unitor λ, so we can use it to define ja .
20.2 V-Functors
An ordinary functor maps objects to objects and arrows to arrows. Similarly, an en-
riched functor F maps object to objects, but instead of acting on individual arrows,
it must map hom-objects to hom-objects. This is only possible if the hom-objects in
the source category C belong to the same category as the hom-objects in the target
category D. In other words, both categories must be enriched over the same V. The
action of F on hom-objects is then defined using arrows in V:
A functor must preserve composition and identity. These can be expressed as com-
muting diagrams in V:
◦
C(b, c) ⊗ C(a, b) C(a, c) I
ja jF a
Fbc ⊗Fab Fac
◦ Faa
D(F b, F c) ⊗ D(F a, F b) D(F a, F b) C(a, a) D(F a, F a)
Notice that we used the same symbol ◦ for two different compositions and the same j
for two different identity mappings. Their meaning can be derived from the context.
As before, all diagrams are in the category V.
The Hom-functor
The hom-functor in a category that is enriched over a monoidal closed category V is an
enriched functor:
HomC : C op ⊗ C → V
Here, in order to define an enriched functor, we have to treat V as self-enriched.
It’s clear how this functor works on (pairs of) objects:
Homha, bi = C(a, b)
(When the category in question is obvious from context, we will omit the subscript C
in HomC .)
To define an enriched functor, we have to define the action of Hom on hom-objects.
Here, the source category is C op ⊗ C and the target category is V, both enriched over
V. Let’s consider a hom-object from ha, a0 i to hb, b0 i. The action of the hom-functor on
this object is an arrow in V:
By definition of the product category, the source is a tensor product of two hom-objects.
The target is the internal hom in V. We are thus looking for an arrow:
This arrow is related through the hom-functor adjunction to the following arrow:
C(b, a) ⊗ C(a0 , b0 ) ⊗ C(a, a0 ) → C(b, b0 )
We can construct this arrow by rearranging the product and applying the composition
twice.
In the enriched setting, the closest we can get to defining an individual morphism
from a to b is to use an arrow from the unit object. We define a (monoidal-global)
element of a hom-object as a morphism:
f : I → C(a, b)
20.2. V-FUNCTORS 279
We can define what it means to lift such an arrow using the hom-functor. For instance,
keeping the first argument constant, we’d define:
as the composite:
λ−1 f ⊗id ◦
C(c, a) −−→ I ⊗ C(c, a) −−−→ C(a, b) ⊗ C(c, a) →
− C(c, b)
ρ−1 id⊗f ◦
C(b, c) −−→ C(b, c) ⊗ I −−−→ C(b, c) ⊗ C(a, b) →
− C(a, c)
A lot of the familiar constructions we’ve studied in ordinary category theory have
their enriched counterparts, with products replaced by tensor products and Set replaced
by V.
Enriched co-presheaves
Co-presheaves, that is Set-valued functors, play an important role in category theory,
so it’s natural to ask what their counterparts are in the enriched setting. The general-
ization of a co-preshef is a V-funtor C → V. This is only possible if V can be made into
a V-category, that is, when it’s monoidal-closed.
An enriched co-presheaf maps object of C to objects of V and it maps hom-objects
of C to internal homs of V:
Fab : C(a, b) → [F a, F b]
Hom : C op ⊗ C → V
C op ⊗ D → V
⊗: V × V → V
Show that if V is monoidal closed, the tensor product defines a V-functor. Hint: Define
its action on internal homs.
280 CHAPTER 20. ENRICHMENT
νa : I → D(F a, Ga)
Naturality condition is a little tricky. The standard naturality square involves the
lifting of an arbitrary arrow f : a → b and the equality of the following compositions:
νb ◦ F f = Gf ◦ νa
Let’s consider the hom-sets that are involved in this equation. We are lifting a morphism
f ∈ C(a, b). The composites on both sides of the equation are the elements of D(F a, Gb).
On the left, we have a composition νb ◦ F f , which is an arrow:
In the enriched setting we have to work with hom-objects rather than hom-sets, and
the selection of the components of the natural transformation is done using the unit I.
We can always produce the unit out of thin air using the inverse of the left or the right
unitor.
Altogether, the naturality condition is expressed as the following commuting dia-
gram:
νb ⊗Fab
I ⊗ C(a, b) D(F b, Gb) ⊗ D(F a, F b)
λ−1 ◦
ρ−1 ◦
Gab ⊗νa
C(a, b) ⊗ I D(Ga, Gb) ⊗ D(F a, Ga)
This also works for an ordinary category, where we can trace two paths through this
diagram by first picking an f from C(a, b). We can then use νb and νa to pick components
of the natural transformation. We also lift f using either F or G. Finally, we use
composition to reproduce the naturality equation.
This diagram can be further simplified if we use our earlier definition of the hom-
functor’s action on global elements of hom-objects. The components of a natural trans-
formation are defined as such global elements. There are two such liftings at our dis-
posal:
D(d, νb ) : D(d, F b) → D(d, Gb)
and:
D(νa , d) : D(Ga, d) → D(F a, d)
20.4. YONEDA LEMMA 281
We get something that looks more like the familiar naturality square:
D(F a, F b)
Fab D(F a,νb )
It turns out that ends and coends can be defined for enriched profunctors, so this
formula works for enriched natural transformations as well. The difference is that,
instead of a set of natural transformations V-nat(F, G), it defines the object of natural
transformations [C, D](F, G) in V.
The definition of the (co-)end of a V-profunctor P : C ⊗ C op → V is analogous to the
definition we’ve seen for ordinary profunctors. For instance, the end is an object e in V
equipped with an extranatural transformation π : e → P that is universal among such
objects.
V-nat(C(c, −), F ) ∼
= V(I, F c)
The strong version of the Yoneda lemma works with objects of V and uses the end
over the internal hom in V to represent the object of natural transformations:
Z
[C(c, x), F x] ∼
= Fc
x
282 CHAPTER 20. ENRICHMENT
x
C(x,Dj)
j k
Dj Dk
l
Dl
This selection of a j’th wire can be described as a function from the singleton set 1:
γj : 1 → C(x, Dj)
γ : ∆1 → C(x, D−)
where ∆1 is a constant functor mapping all objects of J to the singleton set. Naturality
conditions ensure that the triangles forming the sides of the cone commute.
The set of all cones with the apex x is given by the set of natural transformations:
This reformulation gets us closer to the enriched setting, since it rephrases the
problem in terms of hom-sets rather than individual morphisms. We could start by
considering both J and C to be enriched over V, in which case D would be a V-functor.
There is just one problem: how do we define a constant V-functor? Its action on
objects is obvious: it maps all objects to one. But what should it do to hom-objects? An
ordinary constant functor maps all morphisms to the identity morphism. The obvious
generalization would be to map all hom-objects to the unit object I in V. But there’s
no guarantee that for every hom-object C(a, b) we can find an arrow to I; and even if
there was, it might not be unique. In other words, there is no reason to believe that I
is a terminal object.
The solution is to “smear the singularity”: instead of using the constant functor to
select a single wire, we should use some other “weighting” functor W : J → V to select
a thicker “cylinder”. Such a weighted cone with the apex x is an element of the set of
natural transformations:
[J , Set] (W, C(x, D−))
A weighted limit, also known as an indexed limit, limW D, is then defined as the
universal weighted cone. It means that for any weighted cone with the apex x there is
20.6. ENDS AS WEIGHTED LIMITS 283
a unique morphism from x to limW D that factorizes it. The factorization is guaranteed
by the naturality of the isomorphism that defines the weighted limit:
C(x, limW D) ∼
= [J , Set](W, C(x, D−))
The regular, non-weighted limit is often called a conical limit, and it corresponds
to using the constant functor as the weight.
This definition can be translated almost verbatim to the enriched setting by replac-
ing Set with V:
C(x, limW D) ∼= [J , V](W, C(x, D−))
Of course, the meaning of the symbols in this formula is changed. Both sides are now
objects in V. The left-hand side is the hom-object in C, and the right-hand side is the
object of natural transformations between two V-functors.
Dually, a weighted colimit is defined by the natural isomorphism:
C(colimW D, x) ∼
= [J op , V](W, C(D−, x))
P : C op ⊗ C → D
We’ll use this functor as a diagram in D. At this point mathematicians start worrying
about size issues. After all we are embedding a whole category—squared—as a single
diagram in D. Here, we’ll just assume that C is small; that is, its objects form a set.
We want to take a weighted limit of this diagram. The weight must be a V-functor
C op ⊗ C → V. There is one such functor that’s always at our disposal, the hom-functor
HomC . We will use it to define the end as a weighted limit:
Z
P hc, ci = limHom P
c
First, let’s convince ourselves that this formula works in an ordinary (Set-enriched)
category. Since ends are defined by their mapping-in property, let’s consider a mapping
284 CHAPTER 20. ENRICHMENT
from an arbitrary set S to the weighted limit and use the standard Yoneda trick to
show the isomorphism. By definition, we have:
Set(S, limHom P ) ∼
= [C op × C, Set](C(−, =), Set(S, P (−, =))
We can rewrite the set of natural transformations as an end over pairs of objects hc, c0 i:
Z
Set(C(c, c0 ), Set(S, P hc, c0 i))
hc,c0 i
We can now apply the ninja-Yoneda lemma to perform the integration over c0 . The
result is: Z Z
∼
Set(S, P hc, ci) = Set(S, P hc, ci)
c c
where we used continuity to push the end under the hom-functor. Since S was arbitrary,
we conclude that, for ordinary categories:
Z c
lim Hom
P ∼
= P hc, ci
This justifies our use of the weighed limit to define the end in the enriched case.
An analogous formula works for the coend, except that we use the colimit with the
hom-functor in the opposite category HomC op as the weight:
Z
P hc, ci = colimHomCop P
c
Exercise 20.6.1. Show that for ordinary Set-enriched categories the weighted colimit
definition of a coend reproduces the earlier definition. Hint: use the mapping out prop-
erty of the coend.
Exercise 20.6.2. Show that, as long as both sides exist, the following identities hold
in ordinary (Set-enriched) categories (they can be generalized to the enriched setting):
Z
lim D ∼
= W
W j t Dj
j: J
Z j: J
colim D ∼
= W
W j · Dj
Hint: Use the mapping in/out with the Yoneda trick and the definition of power and
copower.
20.7. KAN EXTENSIONS 285
We’ll consider the mapping into it from an arbitrary object d. The derivation follows a
number of simple steps, mostly by expanding the definitions.
We start with:
D(d, (RanP F )e)
and substitute the definition of the Kan extension:
Z
D(d, B(e, P c) t F c)
c
Using the continuity of the hom-functor, we can pull out the end:
Z
D(d, B(e, P c) t F c)
c
D(d, A t d0 ) ∼
= Set A, D(d, d0 )
to get: Z Z
D(d, B(e, P c) t F c) ∼
= Set(B(e, P c), D(d, F c))
c c
This can be written as a set of natural transformation:
The weighted limit is also defined through the set of natural transformations:
D(d, limW F ) ∼
= [C, Set](W, D(d, F −))
(RanP F )e = limB(e,P −) F
This formula becomes the definition of the right Kan extension in the enriched setting.
Similarly, the left Kan extension can be defined as a weighted colimit:
Exercise 20.7.1. Derive the formula for the left Kan extension for ordinary categories.
286 CHAPTER 20. ENRICHMENT
• Weighted colimit:
C(colimW D, x) ∼
= [J op , V](W, C(D−, x))
287
288 INDEX
Rust, 201
under-category, 110
unitor, 34, 36
walking arrow, 63
wedge, 215
Yoneda functor, 88