Category Theory For Programming
Category Theory For Programming
We thank Arnoud van der Leer for their contributions to these notes.
This work is licensed under a Creative Commons “Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)”
license. cba
The source code for this document can be obtained from
https://github.com/benediktahrens/CT4P.
1
Abstract In these lecture notes, we give a brief introduction to some elements of cate-
gory theory. The choice of topics is guided by applications to functional programming.
Firstly, we study initial algebras, which provide a mathematical characterization of
datatypes and recursive functions on them. Secondly, we study monads, which give a
mathematical framework for effects in functional languages. The notes include many
problems and solutions.
2
Contents
1. Introduction 4
1.1. About These Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2. About Category Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3. Learning Material on Category Theory . . . . . . . . . . . . . . . . . . . 4
1.4. Notations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2. Categories 5
2.1. Isomorphisms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2. Sections and Retractions . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3. Monomorphisms and Epimorphisms . . . . . . . . . . . . . . . . . . . . 16
3. Universal Properties 18
3.1. Initial Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.2. Terminal Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.3. (Binary) Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.4. (Binary) Coproducts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4. Functors 22
4.1. Categories as objects of a category? . . . . . . . . . . . . . . . . . . . . 24
5. Natural transformations 26
5.1. Functor categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
5.2. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
5.3. Equivalence of categories . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
6. Adjunctions 30
9. Fusion Property 43
11.Solutions 46
3
1. Introduction
Category theory is a framework which allows one to formally describe and relate math-
ematical structures. By a mathematical structure, we mean, informally, a collection
of things (like types, sets, etc.) and something which transforms one thing to another
thing (like a program, function, etc.).
This framework started as a mathematical theory, but has now proven itself useful
also in the world of Computer Science (and beyond). In this course we will intro-
duce the necessary concepts from category theory with the goal of understanding its
applications in the realm of programming.
4
The rather substantial textbook by Barr and Wells [1] (available for free online)
covers a lot more than we are going to discuss in these notes.
The Catsters [2] provide a lecture series on category theory on YouTube.
1.4. Notations
A list of notations which we use throughout these lectures notes.
• If X is a set and x is an element of X, we write x ∈ X.
• If X is a set and P and Q are properties dependent over the elements of X, we
write P =⇒ Q to express that if P (x) holds for an element x ∈ X, then also
Q(x) should hold for the element x. Moreover, we write P ⇐⇒ Q if P =⇒ Q
and Q =⇒ P .
• If X is a set and P is a property dependent over the elements of X, we write:
(*) ∀x ∈ X : P (x) to express that for every element in X, the property P holds.
(*) ∃x ∈ X : P (x) to express that there exists at least one element in X for
which the property holds.
(*) ∃!x ∈ X : P (x) to express that there exists a unique element in X for which
the property holds.
• Let X and Y be sets. We denote:
(*) X × Y for the (cartesian) product of X and Y .
(*) X ⊔ Y for the disjoint union of X and Y .
2. Categories
Further Reading. The definition of categories is also given in [1, §2.1]. Plenty of
examples of categories are given in [1, §§2.3–2.5].
The definition of categories is also given in [7, §1.1], together with some examples.
There, also isomorphisms are discussed, which we define in Section 2.1.
The tutorial [9] features the definition of categories in [9, §2.1]. It also introduces
the notion of “diagram”, which we do not use in the present notes.
Definition 1. A category C consists of the following data:
1. A collection of objects, denoted by C0 .
2. For any given objects X, Y ∈ C0 , a collection of morphisms from X to Y , denoted
by homC (X, Y ) (or hom(X, Y ) when the category C is clear, or C(X, Y ) or X →
Y ) and which is called a hom-set.
3. For each object X ∈ C0 , a morphism IdX ∈ homC (X, X), called the identity
morphism on X.
5
4. A binary operation
called the composition operator, and written infix without the indices X, Y, Z as
in g ◦ f .
Moreover, this data should satisfy the following properties:
1. (Left unit law) For any morphism f ∈ hom(X, Y ), we have
f ◦ IdX = f.
IdY ◦ f = f.
h ◦ (g ◦ f ) = (h ◦ g) ◦ f.
Intuition 2. So what does a category represent? There are (at least) 3 possible ways
how one can think about this definition:
1. A category represents a type system in the sense that the objects are the types
and each hom-set is the type1 of functions. See Definition 10.
2. A category represents a bag of instances of a particular mathematical structure
(e.g. sets with a notion of addition). The objects are then instances of such a
mathematical theory (e.g. (N, +)) and the morphisms are structure preserving
functions (e.g. functions f which satisfy f (x+y) = f (x)+f (y)). See Examples 4
and 14 and Definition 19.
3. A category represents a directed graph in the sense that an object is a vertex
and a morphism is an edge.
4. Anything (almost at least) can be seen as a category in some exotic way.
Notation 3. Let C be a category.
• We write X ∈ C instead of X ∈ C0 .
• Let X, Y ∈ C be objects. A morphism f ∈ C(X, Y ) can be visualized as
f
X−
→ Y.
1 Inthis case, each hom-set is a type, so isn’t each hom-set an object again? Categories which satisfy
such a property are called cartesian closed.
6
• Let X, Y, Z ∈ C0 objects in C and consider the following morphisms:
Y1 g1 Z
IdX : X → X : x 7→ x.
g ◦ f : X → Z : x 7→ g(f (x)).
Lemma 5. The data of Set satisfies the properties of a category, hence Set is indeed
a category.
Proof. We first show that the left unit law holds. Let X, Y ∈ Set be sets and f ∈
Set(X, Y ) a function. We have to show that IdX · f = f , hence it suffices to show that
they pointwise equal which holds by the following calculation:
7
where the first (resp. second) equality holds by definition of the composition (resp.
identity morphism).
That the right unit law holds is analogous. To show that the associator law holds, let
X, Y, Z, W ∈ Set and f ∈ Set(X, Y ), g ∈ Set(Y, Z) and h ∈ Set(Z, W ). We have to
show h ◦ (g ◦ f ) = (h ◦ g) ◦ f , hence it suffices again to show that they are pointwise
equal which holds by the following calculation:
∀x ∈ X : (h ◦ (g ◦ f )) (x) = h ((g ◦ f )(x)) ,
= h(g(f (x))),
= (h ◦ g)(f (x)),
= ((h ◦ g) ◦ f ) (x),
where the first (resp. second, third, fourth) equality holds by definition of the compo-
sition of h and g ◦f (resp. composition of g and f , composition of h and g, composition
of h ◦ g and f ).
We are now going to describe the category whose collection of objects is given by
collection of Lean types:
Example 6. Consider the following data:
• An object is a Lean type (of some fixed universe).
• If X and Y are Lean types, then is LEAN(X, Y ) the function type X → Y .
• The identity morphism IdX (on X ∈ LEAN0 ) is the identity function on X, i.e.
d e f i d f u n (X : Type ) : X → X := λ x , x .
8
u n d e f 1 : : a −> a
undef1 = u n d efin ed
u n d e f 2 : : a −> a
u n d e f 2 = \ x −> u n d e f i n e d
These are not equal by definition, but we have Id· undef1 = undef2. So by the right
unit law, we must have that undef1 = undef2 (as morphisms in our sought category).
Exercise 9. Read the Haskell wiki page on the category Hask [4].
However, when considering functions to equal when they are pointwise equal, we
can define a category of Haskell types:
Definition 10. The category of Haskell types, denoted by Hask, is the category
specified by the following data:
• An object is a Haskell type.
• If X and Y are Haskell types, then is Hask(X, Y ) the collection of functions
modulo the equivalence relation ∼ defined by identifying pointwise equal func-
tions:
f ∼ g : ⇐⇒ ∀x : X, f (x) = g(x).
i.e. a morphism in Hask is an equivalence class of (Haskell) functions.
• The identity morphism IdX (on X ∈ Hask) is the equivalence class of the identity
function on X.
• The composition of (Haskell) functions is given by the equivalence class of the
composition of functions, i.e., for f ∈ Hask(X, Y ) and g ∈ Hask(Y, Z), the
composition of f and g is the equivalence class of:
g ◦ f : X → Z : λx.g(f (x)).
Example 11. Recall that a preordered set (X, ≤) consists of a set X together with a
binary relation (≤) on X which satisfies the following properties:
• Reflexivity: ∀x ∈ X : x ≤ x.
• Transitivity: ∀x, y, z ∈ X : (x ≤ y ∧ y ≤ z) =⇒ x ≤ z.
Let (X, ≤) be a preordered set. The following data induces a category Pre(X, ≤):
• The objects are the elements of X.
• Let x, y ∈ X be elements. The hom-set hom(x, y) consists of a unique element
if x ≤ y and is empty otherwise.
• We need an identity morphism for each x ∈ X. By reflexitivity (i.e., x ≤ x),
we have that hom(x, x) consists of a unique element, which we take to be the
identity.
9
• We need to define for each x, y, z ∈ X, a composition operator:
where the first (resp. second) inequality holds by f (resp. g) being a morphism of
posets. So our data is indeed well-defined.
That the axioms of a category are satisfied by this data, is exactly the same proof as
showing that Set is a category because the identity and composition are defined in
the same way.
10
Exercise 15 (Solution 211). Is Pos a preorder-category itself? That is, is there at
most one morphism between any two objects?
Question 16. In Example 14, we have shown that Pos is a category. However, by
Example 11, we know that any poset also is a category. So we have that Pos is a
category whose objects are certain categories. Can we also have some category whose
collection of objects is the collection of all categories, and if so, what are the morphisms
of categories?
Solution. Section 4 is devoted completely to this answer.
Lemma 17. Let C be a category. For any object X ∈ C, IdX is the unique morphism
which satisfies the following property: For any Y ∈ C and f ∈ C(X, Y ), we have
f ◦ IdX = f.
Proof. Assume Id˜X also satisfies this property, in particular we have IdX ◦ Id˜X = IdX .
However, by the right unit law, we also must have IdX ◦ Id˜X = Id˜X . Hence, IdX =
Id˜X .
Example 18. In this example we are going to associate a category which captures the
multiplication of the rational numbers. Let C be the category defined by the following
data:
• There is a unique object ⋆.
• The (only) hom-set is given by
hom(⋆, ⋆) = Q,
Q → Q → Q : (p, q) 7→ p · q.
11
Definition 19. Recall that a monoid is a set M equipped with binary operation
m : M → M → M which is associative, i.e.
hom(⋆, ⋆) = M.
Exercise 21 (Solution 212). Let C be a category. When is C of the form Monoid(M, m, e),
i.e. does there exists a monoid (M, m, e) such that C = Monoid(M, m, e)?
Exercise 22 (Solution 213). Define a category Monoid whose objects are monoids,
i.e. define a suitable notion of morphism between monoids and moreover show that
this indeed defines a category.
Exercise 23 (Solution 214). Define a category C whose objects are the natural num-
bers (i.e. C0 = N) and whose hom-sets C(n, m) are given by the (n × m)-matrices.
12
Exercise 24 (Solution 215). Let C be a category. Define a category C op such that
• the objects of C op are the same as those of C; and
• the morphisms C op (X, Y ) are morphisms C(Y, X).
The category C op is called the opposite (category) of C.
Exercise 25. Let G be a directed graph. Then G induces a category Graph(G) as
follows:
• The collection of objects Graph(G)0 is the set of vertices of G.
• The morphisms between object are the (directed) paths, that is, finite sequences
of composible edges, between them.
• For each object x (i.e. vertex), the identity morphism on x is the identity path.
• The composition of morphisms is the composition of paths.
We call Graph(G) the category generated by G. Show that Graph(G) is indeed
a category.
Exercise 26. Argue why the morphisms are chosen to be paths and it is not sufficient
to just take the vertices.
Example 27. Consider the following graph G:
x
i.e. the graph with only object vertex and no edges. The category generated by G is
the category generated is the so-called terminal category, that is, the category with a
single object and a single morphism (the identity morphism of the unique object).
Example 28. Consider the following graph G:
x y
The category generated by G is the category generated is the so-called interval category,
that is, the category with two objects and, besides the identity morphisms, a unique
morphism (living in hom(x, y)).
In the following example we use the following notation:
• If f is a morphism in a category, we denote f 2 := f ◦ f, f 3 := f 2 ◦ f , etc.
• We also label the edges in order to refer to them.
Example 29. Consider the following graph G:
f
x y
g
13
• The collection of objects is {x, y}.
• The hom-sets are given as follows:
– hom(x, x) contains
Idx , g ◦ f, (g ◦ f )2 , (g ◦ f )3 , · · · ,
But these are not the only ones, we also have that each of these can be
precomposed or postcomposed with Idx , however, by the unit laws, we know
that these don’t give us any new morphisms. The same remark holds for
the associativity law. This comment also holds for the upcoming hom-sets.
– hom(y, y) contains
– hom(x, y) contains
f, (f ◦ g) ◦ f, (f ◦ g)2 ◦ f, (f ◦ g)3 ◦ f, · · ·
– hom(y, x) contains
g, (g ◦ f ) ◦ g, (g ◦ f )2 ◦ g, (g ◦ f )3 ◦ g, · · ·
x z
The category generated by G has four objects (namely x, y, z, w) and the hom-sets are:
• hom(y, x), hom(y, z) and hom(z, w) are singleton sets,
• hom(x, y), hom(z, y), hom(x, w), hom(w, x) and hom(w, z) are all empty.
• hom(y, w) consists of the path y → z → w.
• For each vertex v, we have that hom(v, v) consists only of the identity path on
v.
Exercise 31 (Solution 216). Describe the connection between the categories generated
by graphs and the categories associated to preordered sets. What is the property of
anti-symmetry translated under this connection with graphs?
Exercise 32. Define a category Aut whose objects are (deterministic finite) au-
tomata.
14
2.1. Isomorphisms
Further Reading. In this section, we study properties of arrows in a category. More
information on this topic is given in [1, §2.7].
Also, [9, §2.2] briefly discusses isomorphisms.
Definition 33 (Isomorphism). Given a category C, objects a, b ∈ C0 and a morphism
f : a → b in C, we say that f is an isomorphism when there is a morphism g : b → a
(in the other direction!) such that f · g = Id and g · f = Id. We write f : a ∼ = b for a
morphism f that is an isomorphism.
In this case, we call g the inverse of f and f the inverse of g. (The latter is justified
by Exercise 34.)
Exercise 34 (Solution 217). Show that if f : a → b is an isomorphism with inverse
g : b → a, then g is an isomorphism with inverse f .
Exercise 35 (Solution 218). Show that a morphism f : a → b in C is an isomorphism
in at most one way, that is, show that its inverse is unique if it exists.
Exercise 36 (Solution 219). Show that the composition of two isomorphisms is an
isomorphism.
Remark 37. Since any identity morphism is an isomorphism (check this!), we con-
clude by Exercise 36 that given any category C, we always get a new category isos(C)
by restricting the morphisms to be isomorphisms, i.e.
isos(C)0 = C0 , isos(C)(X, Y ) = {f ∈ C(X, Y ) | f is an isomorphism}
and where the identity and composition is the same as in C.
Exercise 38 (Solution 220). Consider the Haskell datatype
d a t a BW = B l a c k | White
Construct two (different!) isomorphisms between BW and the type Bool of booleans.
Exercise 39 (Solution 221). Characterize the isomorphisms in Set.
Exercise 40 (Solution 222). Describe the isomorphisms in Pos.
Exercise 41 (Solution 223). Let (X, ≤) be a poset. Can you characterize the isomor-
phisms in Pos(X, ≤)?
Exercise 42. Can you characterize the isomorphisms in Monoid?
Exercise 43. Let G be the category generated by the following graph:
w
x z
y
Show that the only isomorphisms in G are the identity morphisms (i.e. the identity
paths).
15
2.2. Sections and Retractions
Definition 44 (Section, Retraction). A pair (s, r) of morphisms s : a → b and r : b →
a in C is called a section-retraction pair if r ◦ s = Idb .
In such a case, we call s a section and r a retraction.
Remark 45. Note that a morphism can be a retraction in more than one way, that
is, there can be more than one section s such that r ◦ s = Id.
Intuitively, a section-retraction pair (s, r) of morphisms s : a → b and r : b → a in a
category C provides a way for a to “live inside” b. Note that for a given a and b there
can be many ways for a to live inside b.
Exercise 46 (Solution 224). Construct two different section-retraction pairs between
the type Bool of booleans and the type Int of integers (e.g., in Haskell).
Exercise 47. Show that the type Maybe a is a retract of the type [a] .
Hint: The idea is that Nothing corresponds to the empty list [] and that Just x
corresponds to the one-element list [x] . Make this idea precise by writing back and
forth functions between these types so that they exhibit Maybe a as a retract of [a] .
we have
f ◦ g1 = f ◦ g2 implies g1 = g2 .
Exercise 49 (Solution 225). In the category of sets, show that a morphism f : X → Y
is a monomorphism if and only if it is injective.
Definition 50 (Epi). Let f : a → b be a morphism in C. We say that f is an
epimorphism if, for any two morphisms g1 , g2 : b → z, like in the following diagram,
16
f g1
a b z
g2
we have
g1 ◦ f = g2 ◦ f implies g1 = g2 .
x z
y
resp.
w
x z
y
resp.
w
x z
y
Can you characterize the mono- and epimorphisms in these categories?
17
Exercise 57. Can you characterize the monomorphisms, epimorphisms and isomor-
phisms in the category generated by the following graph:
x y
3. Universal Properties
In this section, we discuss special objects in a category.
Further Reading. On initial and terminal objects, see also [1, §2.7.16] and [7, p.
48ff].
Products and coproducts, other special limits and colimits, and the general definition
of limits and colimits, are discussed in [7, §§5.1, 5.2].
Pierce’s tutorial discusses the (co)limits defined here in [9, §§2.3–2.4], and further
(co)limits in [9, §§2.5–2.7].
A B
A B
18
Exercise 67 (Solution 230). Let (X, ≤) be a poset. Describe what an initial object
looks like in Pos(X, ≤).
Exercise 68 (Solution 231). Let A and A′ be initial objects in C. Construct an
isomorphism i : A ∼
= A′ .
Exercise 69 (Solution 232). Let A be an initial object in C, and let A′ be isomorphic
to A (via an isomorphism i : A ∼
= A′ ). Show that A′ is an initial object of C.
Remark 70. Exercises 68 shows that initial objects in a category C are essentially
unique, that is, they are unique up to (unique) isomorphism.
This justifies using the determinate article: we will say that A is the initial object
of C.
This is more generally the case for any object with a universal property, see, e.g.,
Exercises 82 and 94.
Exercise 71 (Solution 233). Construct a category that does not have an initial object.
Exercise 72 (Solution 240). Let PtSet be the category of pointed sets, that is the
category whose objects are pairs (X, x) with X a set and x ∈ X and a morphism from
(X, x) to (Y, y) is defined as a function f : X → Y such that f (x) = y. Identify an
initial object in PtSet.
Remark 73. The concept of initial object seems trivial and boring in the categories
considered above. However, in complicated categories, initial objects can be compli-
cated and exciting; we will see this in Section 8.
19
Exercise 83 (Solution 244). Let B be a terminal object in C, and let B ′ be isomorphic
to B (via an isomorphism i : B ∼
= B ′ ). Show that B ′ is a terminal object of C.
Exercise 84 (Solution 245). Show that C has a terminal object if and only if C op has
an initial object.
Exercise 85 (Solution 246). Construct a category that does not have an terminal
object.
Q
q1 q2
f
A πl P πr B
20
Exercise 96 (Solution 252). Let C be a category and T ∈ C0 a terminal object. For
any object A ∈ C0 , construct a product of A and T .
Hint: to form an idea what the object A × T should be, solve the exercise first in a
specific category, e.g., in the category of sets or in a category coming from a preordered
set.
Exercise 97 (Solution 253). Let C be a category and A, B ∈ C0 be objects. Show
that the product of A and B exists if and only if the following category has a terminal
object:
• The objects are triples (P, pl : P → A, pr : P → B).
• A morphism from (P, pl , pr ) to (Q, ql , qr ) is a morphism f ∈ C0 such that the
following diagram commutes:
P
pl pr
f
A ql Q qr B
21
Exercise 101. Write a sequence of suitable exercises about coproducts.
Exercise 102. Consider the category with one object and the rational numbers Q as
morphisms. Can you construct an initial object in this category? A terminal object?
Products? Coproducts?
Remark 103. Initial and terminal objects and products and coproducts are special
cases of limits and colimits. We are not studying, in these notes, the general notion
of (co)limit. However, the examples above should suffice for you to understand, in
your own time, other (co)limits, such as
• pullbacks and pushouts;
• products and coproducts of families of objects (not just of pairs of objects); and
• equalizers and coequalizers.
Further Reading. We do not discuss here whether/when/how (co)limits can be trans-
ported along functors. You can find some information on this in [7, §5.3].
4. Functors
An important aspect in computer programming is the transformation of data. For
example, if you have a data type X, then one can consider also the data type List(X)
of lists with values in X. If one thinks of the objects in a category to be data types,
then we can ask even more. If f : X → Y is a function (between the data types), then
this also induces a function from the X-valued lists to the Y -valued lists as follows:
Remark 104. The “. . . ” above are informal — a formal definition would define
List(f ) by structural recursion on lists, of course.
A functor formalizes this phenomenon:
Definition 105. Let C and D be categories. A functor F from C to D consists of
the following data:
• A function
C0 → D0 ,
written as X 7→ F (X).
• For each X, Y ∈ C0 , a function
written as f 7→ F (f ).
22
Moreover, this data should satisfy the following properties:
• (Preserves composition) For f ∈ homC (X, Y ) and g ∈ homC (Y, Z), we have
F (g ◦ f ) = F g ◦ F f .
• (Preserves identity) For X ∈ C, we have F (IdX ) = IdF (X) .
Example 106. The list-functor (on sets), denoted by List, is the functor from Set
to Set defined by the following data:
• The function on objects is given by:
(×A)X := X × A.
Exercise 111. Let A ∈ Set0 . Construct a functor (+A) : Set → Set that, on objects,
is given by
(+A)X := X + A.
Exercise 112. Let C be a category with chosen coproducts, and let A ∈ C0 . Construct
a functor (+A) : C → C that, on objects, is given by
(+A)X := X + A.
Exercise 113. Let R ∈ Set0 be a set. Construct a functor (R →) : Set → Set that,
on objects, is given by
(R →)X := R → X.
23
Exercise 114. Let C be a category and let R ∈ C0 . Construct a functor C(R, −) :
C → Set that, on objects, is given by
Exercise 115. Let (X, ≤X ) and (Y, ≤Y ) be posets. Can you characterize/describe
the functors from Pos(X, ≤X ) to Pos(Y, ≤Y ) ? Before writing out the definitions,
what would you expect the answer to be?
Exercise 116. Let C be a category with chosen products (A × B, πA , πB ) for any two
objects A and B. Construct a functor
(×) : C × C → C
(+) : C × C → C
C0 → C0 : X 7→ X.
C(X, Y ) → C(X, Y ) : f 7→ f.
24
Exercise 120. Show that IdC (defined in Example 119) satisfies the properties of a
functor, i.e. IdC is indeed a functor.
Example 121. Let C, D and E be categories and F : C → D and G : D → E functors.
The composition functor of F with G, denoted by F · G, is the functor specified
by the following data:
• The function on objects is given by
C0 → E0 : X 7→ G(F (X)).
Exercise 122. Show that F · G (defined in Example 121) satisfies the properties of a
functor, i.e. F · G is indeed a functor.
Definition 123. The Category of categories, denoted by Cat, is the category
specified by the following data:
• An object is a category.
• If C, D ∈ Cat are categories, then is Cat(C, D) the collection of all functors from
C to D.
• The identity morphism on a category C is the identity functor on C defined in
Example 119.
• The composition of morphisms, i.e. functors, is the composition of functors
defined in Example 121.
Exercise 124. Show that Cat satisfies the property of a category, i.e. Cat is indeed
a category.
Remark 125. When showing that Cat is a category, one is forced to consider equality
of objects when showing that two functors are equal. This goes against the spirit of
category theory, where we only ever consider equality of (parallel) morphisms. We
want to consider two objects “the same” when they are isomorphic, not when they
are equal. Of course, any two equal objects are isomorphic to each other, but not the
other way round; for instance, in the category of sets, the cartesian product A × B is
isomorphic to B × A, but they are not equal.
To stay within the spirit of category theory, one can instead consider Cat as a
bicategory.2 In a bicategory, one has one more layer of things: objects, morphisms,
and 2-cells between parallel morphisms. One also calls objects “0-cells” and morphisms
“1-cells”, for consistency. Importantly, in a bicategory, the laws concerning 1-cells (as
2 See,e.g., https://ncatlab.org/nlab/show/bicategory#detailedDefn for a definition of bicate-
gories.
25
stated in Definition 1) do not hold up to equality, but only up to isomorphism of
2-cells.
An important example is the bicategory given by the following data, which we only
list partially:
1. 0-cells are categories;
2. 1-cells are functors;
3. 2-cells are natural transformations (see Section 5);
4. composition and identity of 1-cells is composition and identity of functors.
We do not delve into bicategories in these notes; an introductory text is, for instance,
Leinster’s [6].
5. Natural transformations
Definition 126. Let F, G : C → D be functors. A natural transformation α from
F to G consists of the following data:
• For each X ∈ C0 , a morphism αX ∈ D(F (X), G(X)).
Moreover, this data should satisfy the following naturality condition:
For each f ∈ C(X, Y ), the following diagram should commute:
αX
F (X) G(X)
F (f ) G(f )
F (Y ) αY G(Y )
Moreover, we call α a natural isomorphism if for each X ∈ C0 , we have that αX is
an isomorphism in D.
Notation 127. If F, G : C → D are functors. A natural transformation α from F to
G, is denoted as α : F ⇒ G or
F
C α D
26
5.1. Functor categories
Definition 129. Let F : C → D be a functor. The identity natural transforma-
tion IdF on F is given by the following data:
Exercise 130. Show that for any functor F : C → D, the identity natural transfor-
mation IdF satisfies the properties of a natural transformation.
Definition 131. Let F, G, H : C → D be functors and α : F ⇒ G, β : G ⇒ H
be natural transformations. The (vertical) composition of α and β is the natural
transformation ·αβ is given by the following data:
∀X ∈ C0 : (β ◦ α)X := βX ◦ αX .
27
5.2. Exercises
Exercise 138. Let (M, m, e) be a monoid and let Monoid(M, m, e) be its corre-
sponding category. Recall from Exercise 118 that a functor from M to Set is a set X
together with an action of M on X, i.e. a function µ : M × X → X such that
[n] := {0, 1, · · · , n − 1} ,
3 Notice that the objects of FinSet form a subset of the objects of Set, but given any two finite sets
X, Y inFinSet0 , we have F IN SET (X, Y ) = SET (X, Y ). We say in this case that FinSet is a
(full) subcategory of Set.
28
φX
X [|X|]
f U(f )
Y [|Y |]
φY
∀X, Y ∈ C0 : F (X) = F (Y ) =⇒ X = Y.
∀Y ∈ D0 : ∃X ∈ C0 : F (X) = Y.
∀Y ∈ D0 : ∃X ∈ C0 : F (X) ∼
= Y.
G ◦ F → IdC , F ◦ G → IdD
29
Exercise 146. Show that U induces an equivalence of categories.
Exercise 147. Let C be the category whose objects are categories with a unique object
and whose morphisms are functors between these one-object categories, i.e. C is the
(full) subcategory of Cat generated by the categories with a unique object. Show that
C is equivalent to the category Monoid of monoids.
What happens if we do not consider C to consist of those categories with a unique
object, but with a unique object up to isomorphism, i.e. C is the category whose
objects are categories D which satisfy the following property:
∀X, Y ∈ D0 : X ∼
= Y.
∃x : P (x),
6. Adjunctions
Definition 149. A pair (F, G) of functors F : C → D, G : D → C is called an adjoint
pair if for every objects C ∈ C0 and D ∈ D0 , there exists a bijection
which are moreover natural in both C and D, i.e. for each f ∈ C(C1 , C2 ) and g ∈
D(D1 , D2 ), the following diagrams commute:
αC2 ,D
D(F (C2 ), D) C(C2 , G(D))
−◦F (f ) −◦f (3)
If (F, G) is an adjoint pair, we call F the left adjoint of G and we call G the right
adjoint of F and we denote F ⊣ G.
30
Exercise 150. Let F : C → D be a functor. Show that if F has a left (resp. right)
adjoint G, then G must be unique up to isomorphism4 .
Exercise 151. Let U : Monoid → Set be the forgetful functor (defined in Exam-
ple 263) which maps a monoid to its underlying set and let F : Set → Monoid be
the functor which maps a set to the free monoid of this set (defined in Example 266).
Then is (F, U ) an adjoint pair. (Hint: Use Proposition 268).
Exercise 152. Let Y be a set. Show that the functor (induced by the following
mapping on objects)
− × Y : Set → Set : X 7→ X × Y,
has a right adjoint which is given by the functor (induced by the following mapping
on objects)
Set(Y, −) : Set → Set : X 7→ Set(Y, X).
Is there an analoguous statement in the category LEAN instead of Set?
Theorem 153. Let (F, G) be a pair of functors F : C → D, G : D → C. The following
statements are equivalent:
1. (F, G) is an adjoint pair.
2. There exists natural transformations
η : IdC ⇒ G ◦ F, ǫ : F ◦ G ⇒ IdD ,
F (ηC ) ηG(D)
F (C) F (G(F (C))) G(D) G(F (G(D)))
ǫF (C) G(ǫC )
IdF (C) IdG(D)
F (C) G(D)
In case (F, G) satisfies these (equivalent) conditions, we call η the unit of the ad-
junction and ǫ the counit of the adjunction. The equalities in condition 2 are
called the triangle identities.
Proof. First assume that (F, G) is an adjoint pair. We have to define the unit and
counit and show that the triangle identities hold:
• Unit: For each X ∈ C0 , we should first define ηX ∈ C(X, G(F (X))). Since
F ⊣ G, we have a bijection
31
hence, we define ηX := αX,F X (IdF X ). We now show that (ηX )X∈C0 forms a nat-
ural transformation: Assume f ∈ C(X, Y ). We have to show that the following
diagram commutes:
αX,F X (IdF X )
X G(F (X))
f G(F (f ))
Y G(F (Y ))
αY,F Y (IdF Y )
That this square is indeed commutative follows from the following computation:
α−1
GY,Y : C(GY, GY ) → D(F (G(Y )), Y ).
IdF X = α−1
GF X,F X (IdGF (X) ) ◦ F (ηX ).
α(IdF X ) = α(α−1
GF X,F X (IdGF (X) ) ◦ F (ηX ))
α(α−1
GF X,F X (IdGF (X) ) ◦ F (ηX )) = α(α
−1
(IdGF X )) ◦ ηX = IdGF X ◦ ηX = ηX ,
where the first (resp. second) equality holds by naturality (resp. bijectiveness)
of α.
This concludes the proof of (1) =⇒ (2). Now assume that (2) holds. We have to
construct bijections
αX,Y : D(F (X), Y ) → C(X, G(Y )),
which are natural in X and Y . Let g ∈ D(F (X), Y ). Define αX,Y (g) as the composite:
ηX G(g)
X −−→ G(F (X)) −−−→ G(Y ).
32
For the other direction, let f ∈ C(X, G(Y )). Define α−1
X,Y (f ) as the composite:
F (f ) Y ǫ
F X −−−→ F (G(Y )) −−→ Y.
That α and α−1 are inverses of eachother follows from the following computation:
33
• For each f ∈ C(X, T (Y )), a morphism f ∗ ∈ C(T (X), T (Y )).
such that the following properties holds:
∗
1. For each X ∈ C0 , we have ηX = IdT (X) .
2. For each f ∈ C(X, T (Y )), the following diagram commutes:
ηX
X T (X)
f∗
f
T (Y )
3. For each f ∈ C(X, T (Y )) and g ∈ C(Y, T (Z)), the following diagram commutes:
f∗
T (X) T (Y )
g∗
(g∗ ◦f )∗
T (Z)
X 7→ (X + E),
34
Exercise 159 (??). Show how the following assignment induces a Kleisli triple over
the category Set:
X 7→ Pf in (X) := {A ⊆ X | A is finite} .
The resulting monad is called the Monad of nondeterminism.
Exercise 160 (Solution 238). Let R be a set (considered as a set of results). Show
how the following assignment induces a Kleisli triple over the category Set:
X 7→ ContR (X) := (X → R) → R.
X 7→ R → X
35
• For each X, Y ∈ (CT )0 , CT (X, Y ) := C(X, T Y ).
• The identity on X ∈ (CT )0 is ηX .
• The composition of f ∈ CT (X, Y ) and f ∈ CT (Y, Z) is g ∗ ◦ f .
Exercise 166. Show that for every Kleisli triple, its Kleisli category satisfies the
properties of a category.
36
• Morphisms from (X, I, P, S) to (X ′ , I ′ , P ′ , S ′ ) are functions f : X → X ′ such
that the following diagrams commute:
I P S
Z X X ×X X X X
f f ×f f f f
I′
′ ′
P S
X′ X′ × X′ X′ X′ X′
z:1→X
s:X →X
Here, think of “z” standing for “zero”, and “s” standing for “successor”.
37
Example 172. Let F be the endofunctor induced by:
F : Set → Set
X 7→ 1 + (X × X).
e:1→X
m : X × X → X.
φe : 1 → M
⋆ 7→ e
φm : M × M → M
(x, y) 7→ m(x, y)
FD ψ
D
Exercise 175. Let F be the endofunctor defined as in Example 172, i.e. the endo-
functor whose algebras corresponds with monoids. Characterize the F -algebra homo-
morphisms.
Definition 176. Let F : C → C be an endofunctor. The category of F -algebras,
denoted by Alg(F ), is defined by the following data:
• The objects are the F -algebras.
38
• The morphisms are the F -algebra homomorphisms.
• The identity on (C, φ) is given by the identity IdC in C.
• The composition is given by the composition of morphisms in C.
Exercise 177. 1. Draw two diagrams to illustrate Definition 176.
2. Show that Alg(F ) satisfies the properties of a category.
We are interested in initial objects of Alg(F ), if they exist. We call these “initial
F -algebras”. For a general endofunctor F , an initial F -algebra does not exist; but for
many interesting choices of F , such an initial object does exist. Before coming to the
general definition (see Definition 179), we consider an example.
Exercise 178. Consider the functor Maybe : Set → Set.
1. Show that the initial Maybe-algebra is given by the pair (N, [zero, succ]), where
N is the set of natural numbers, and zero : 1 → N and succ : N → N are the
function picking out zero and the successor function, respectively.
2. Given any other Maybe-algebra (X, [z, s]), unpack what it means for the square
of Definition 174 to commute.
3. Compare the data from the previous exercise to a definition of a function f :
N → X by pattern matching (e.g., in Haskell).
Definition 179. Let F : C → C be an endofunctor. An initial F -algebra (if it
exists) is an initial object in Alg(F ). Unfolding the definition, this means that it is an
F -algebra (µF , in) such that for any F -algebra (C, φ), there exists a unique morphism
LφM ∈ C(µF , C) such that the following diagram commutes:
in
F µF µF
F LφM LφM
(6)
FC φ
C
39
Define an endofunctor F : Set → Set such that the F -algebras can be characterized
as triples (X, b1 , b2 ) with X a set and b1 , b2 ∈ X.
Moreover, show that (Bool, true, f alse) is the initial object in Alg([)F ].
Exercise 182. The disjoint union (i.e. the coproduct) of two sets X and Y can also
be characterized as an inductive data type, indeed: It is generated by the following
two constructors:
f : X −> X+Y
g : Y −> X+Y
Define an endofunctor F : Set → Set such that the F -algebras can be characterized
as triples (C, il , ir ) with C a set and il : X → C, ir : Y → C be functions.
Moreover, show that (X + Y, inl, inr) is the initial object in Alg(F ), where X + Y is
the disjoint union of X and Y (i.e. the coproduct in Set) and inl : X → X + Y, inr :
Y → X + Y the canonical inclusions.
Exercise 183. Let Nc be the conatural numbers, i.e. N + {∞}. Consider the maybe
endofunctor (defined in Example 171), i.e. the functor induced by
F : Set → Set : X 7→ 1 + X.
The functions
zeroc : 1 → Nc :⋆ 7→ 0,
(
n + 1, if x := n ∈ N,
succc : Nc → Nc :x 7→
∞, if x := ∞.
form an F -algebra. However, show that (Nc , zeroc , succc ) is not a initial F -algebra.
Exercise 184 (Fusion Property, Solution 257). Let F : C → C be an endofunctor
and let (µF , in) be an initial algebra. Show that for F -algebras (C, φ) and (D, ψ) and
f ∈ C(C, D), we have
f ◦ φ = ψ ◦ F (f ) =⇒ f ◦ LφM = LψM.
Ff f
FD ψ
D
40
Exercise 186 (Solution 258). Let C be a category with an initial object ⊥. Show
that (⊥, Id⊥ ) is the initial algebra for the identity (endo)functor on C.
Exercise 187. Let A be a set and define F to be the functor induced by
FA : Set → Set
X 7→ 1 + (A × X).
Remark 188. In the case of lists, the operator L M is also known as fold , which in
Haskell is defined as follows:
fold : : (a → b → b) → b → ([ a ] → b)
fold f v [] = v
f o l d f v ( x : xs ) = f x ( f o l d f v xs )
Compare the input of fold to the data of an FA -algebra given in Item 1 of Exercise 187.
Exercise 189. Define the following functions as a catamorphism, that is, using fold .
In each case, draw the diagram corresponding to Diagram 6 of Definition 179.
1. sum :: [ Int ] → Int
2. product :: [ Int ] → Int
3. and :: [Bool ] → Bool
4. or :: [Bool ] → Bool
5. (++) :: [a] → [a] → [a]
41
Exercise 190 ([5, §3.1]). Show that (+1) . sum = fold (+) 1, by showing that (+1) . sum
makes Diagram 6 of Definition 179 commute.
Exercise 191. An exercise about (the limits of) representing functions as catamor-
phisms (or rewriting functions using ‘fold‘):
1. We can represent numbers as big-endian binary numbers, in the set List({0, 1}):
the lists with elements in the set {0, 1}. For example, 13 becomes [1, 1, 0, 1],
which we represent as (cons 1 (cons 1 (cons 0 (cons 1 nil )))) . We define the func-
tion bin2int : List({0, 1}) → N, that converts big-endian binary representa-
tions to positive integers. For example, ( bin2int (cons 1 (cons 0 nil ))) = 2 and
( bin2int (cons 1 (cons 1 (cons 0 (cons 1 nil ))))) = 13.
Show that we can give bin2int as a catamorphism. i.e. with F the functor for
which List({0, 1}) is an initial algebra, show that there exists an F -algebra (N, f )
such that Lf M = bin2int.
2. We can also represent a number as a little-endian binary number. Then 13
becomes [1, 0, 1, 1], which we represent as (cons 1 (cons 0 (cons 1 (cons 1 nil )))) .
We define the function bin2int2 : List({0, 1}) → N, that converts little-endian
binary representations back to positive integers.
Why can’t we give bin2int2 as a catamorphism?
(Hint: how would such a catamorphism calculate the values for [1], [0, 1] and
[0, 0, 1]?)
3. Show that there exists an F -algebra (N × N, f ) such that πl ◦ Lf M = bin2int2,
with πl the projection on the first coordinate.
Exercise 192. Let A be a set and define F to be the functor induced by
Show that the initial FA -algebra is given by the set BT ree(A) of A-valued binary
trees.
Remark 193. Notice that in Exercise 187 and Exercise 192, we can consider the
functor FA as a bifunctor where we vary A, i.e.
In particular, under the assumption that for every A ∈ Set the initial FA -algebra
exists, we can wonder if the assignment
can be extended to a functor. The following exercise answers this question positively
for arbitrary categories.
42
Exercise 194 (Solution 259). Let F : C → C → C be a bifunctor such that for any
object A ∈ C, the initial algebra for the functor induced by
FA : C → C : X 7→ F (A, X),
exists. Show how
C0 → C0 : A 7→ µFA ,
induces a functor.
9. Fusion Property
The fusion property of Exercise 184 can be used to “fuse” a composition of functions
into one function, possibly leading to more efficient code.
We are going to exemplify this using the datatypes of lists and of natural numbers.
Recall that (N, [zero, succ]) is the initial Maybe-algebra. We also write (+1) for succ.
Further Reading. The content of this section is very much inspired by [5, §3.2]. You
are strongly encouraged to read that section before reading the present section. In this
section, we show how
Exercise 195 ([5, §3.2]). Show that (+1) . sum = fold (+) 1, by using the fusion prop-
erty of Exercise 184.
P
Solution 196. We also write for sum. Note that sum is defined as the catamorphism
L[zero, (+)]M. The situation is summarized in the following diagram
[nil,cons]
{∗} + N × List(N) List(N)
P P
Id+Id×
[zero,(+)]
{∗} + N × N N L[1,(+)]M
Id+Id×(+1) (+1)
[1,(+)]
{∗} + N × N N
PIn order to show that (+1) . sum = fold (+) 1, in the diagram expressed as (+1) ◦
= L[1, (+)]M, we need to show that the lower rectangle commutes—this is what
Exercise 184 says.
There are two cases to consider: the case of ∗, and the case of a pair (m, n) ∈ N × N.
In the case of ∗, we obtain
(+1) 0 = 1
and in the case of (m, n), we obtain
(+1) ((+) m n) = (+) m ((+1) n)
both of which hold.
43
Exercise 197. Prove that map g . map f = map (g . f) using the fusion property.
Further Reading. In Sections 8 and 9, we have only looked at functions defined via
iteration, that is, functions defined as catamorphisms of some F -algebra. While prim-
itive recursive functions can always be expressed as a catamorphism via tupling (see,
e.g., [5, §4] and [10, §3.1]), it is more natural specify them via a slightly more sophis-
ticated universal property, explained in detail in Vene’s dissertation [10, Chapter 3].
The interested reader might also study the tutorial by Fokkinga [3] or the paper by
Meijer et al. [8].
A guide to further literature on recursion operators is given in [5, §6].
φ
C F (C)
f F (f )
D ψ
F (D)
Exercise 200. Define the category CoAlg(F ) of F -coalgebras analogously to the cat-
egory Alg(F ) of F -algebras (as in Definition 176).
44
Exercise 202. Spell out what it means for a coalgebra to be the terminal coalgebra.
Exercise 203. Let F : C → C be an endofunctor and assume that the terminal
coalgebra (ν F , out) exists. Show that the following properties holds:
1. Id = JoutK.
2. For F -algebras (C, φ) and (D, ψ) and f ∈ C(C, D), we have
ψ ◦ f = F (f ) ◦ φ =⇒ JψK ◦ f = JφK.
out
νF F νF
F : Set → Set : X 7→ 1 + X.
N + {∞} → 1 + N + {∞}
0 7→ ⋆
s(n) 7→ n
∞ 7→ ∞
45
for ν FA . The functions head : Stream(A) → A and tail : Stream(A) → Stream(A) are
given by
respectively. Put differently (recall the definition of the product in Definition 86), we
have
out = hhead, taili : Stream(A) → A × Stream(A)
Given any two functions
h:C→A
t:C→C
the anamorphism Jhh, tiK is the unique solution f : C → Stream(A) of the equation
system
head ◦ f = h
tail ◦ f = f ◦ t
that is, the unique function f : C → Stream(A) making the following square commute:
hh,ti
C A×C
Jhh,tiK Id×Jhh,tiK
hhead,taili
Stream(A) A × Stream(A)
11. Solutions
Solution 210 (Exercise 12). The inequality x ≤ y (resp. y ≤ x) means that we have
a (unique) morphism from f ∈ hom(x, y) (resp. g ∈ hom(y, x)). Consequently, we get
a loop g ◦ f ∈ hom(x, x). Since the hom-sets are either empty or a singleton, we have
g ◦ f = Idx . Hence, antisymmetry means that if IdX = g ◦ f for some f ∈ hom(x, y)
and g ∈ hom(y, x), we must have
x = y, f = Idx = g.
Rephrased a little bit different, we get: The category Pre(X, ≤) has no non-trivial
loops if (X, ≤) is antisymmetric.
46
Solution 211 (Exercise 15). That Pos would be a preorder-category means that each
of the hom-sets is either empty or a singleton. Hence, it is not a preorder-category if
there exists (X, ≤X ) and (Y, ≤Y ) such that Pos((X, ≤X ), (Y, ≤Y )) has more then one
element.
We choose (X, ≤) := (N, ≤) =: (Y, ≤). A morphism f ∈ Pos((N, ≤), (N, ≤)) consists
of a function f : N → N such that the following property holds:
But there are a lot of functions from N to N which satisfies this property, indeed: For
any k ∈ N, we have that
fk : N → N : n 7→ n + k,
is a morphism in Pos. Hence Pos((N, ≤), (N, ≤)) consists of an infinite amount of
distinct morphisms.
Solution 212 (Exercise 21). A category C is of the form (M, m, e) if and only if C
has a unique object. Indeed, if C has a unique object, lets denote this by X, then we
can define a monoid (M, m, e) as follows:
• The underlying set of the monoid is M := C(X, X).
• The multiplication m is given by m(f, g) := g ◦ f .
• The identity element e is given by e := IdX .
That (M, m, e) is indeed a monoid, i.e. satisfies the monoid laws, is just a translation
of the axioms of C being a category.
Solution 213 (Exercise 22). Since a monoid consists of a set M together with a binary
operation m : M → M → M (called the multiplication) and a identity element e ∈ M ,
a suitable morphism of monoids, from (M1 , m1 , e1 ) to (M2 , m2 , e2 ) should consists of
a function f : M → N which preserves the structure (i.e. the multiplication and the
identity element). More precisely:
• Preservation of the multiplication:
We will now prove that the monoids (as objects) and morphisms of monoids (as the
morphisms) carry the data of a category, i.e. we have to define identity morphisms
and the composition of morphisms:
47
• Let (Mi , mi , ei ) be a monoid for i = 1, 2, 3 and let
where the second (resp. third) equality holds since f (resp. g) preserves
the identity element.
So everything is indeed well-defined. So we are now ready to show that composition
of some morphism of monoids f with the identity morphism is again f (both on
the left and right) and that the composition of morphisms of monoids is associative.
This follows immediate since everything is defined using functions and we know that
functions satisfy these properties.
Solution 214 (Exercise 23). In order to make C into a category, we have to define
the identity morphisms and the composition of morphisms.
Let n ∈ N =: C0 be a natural number. The identity on n is an element in C(n, n),
which is the set of n × n-matrices. Hence, for the identity on n, we can take Idn to be
the identity n × n-matrix, i.e. all coefficients are zero except on the diagonal where
everything is 1.
48
Let l, m, n ∈ N be natural numbers and M (resp. N ) be a l × m-matrix (resp.
m × n-matrix). The composition N ◦ M should be an l × n-matrix, hence we define
N ◦ M as the matrix multiplication of M and N , i.e. N ◦ M = M · N .
That this data satisfies the properties of being a category follows immediate because
matrix multiplication is associative and that the multiplication of any matrix M with
the identity matrix (of the right size and both on the left or on the right) is again M .
Solution 215 (Exercise 24). In order to avoid confusion, we use the following notation:
For any f ∈ C(X, Y ) morphism, we denote by f op the corresponding morphism in
C op (Y, X).
Let X ∈ C op 0 = C0 . The identity morphism is defined as the morphism correspond-
op
ing to the identity, i.e. it is (IdX ) .
Let g ∈ C (Z, Y ), f ∈ C (Y, X). The composition is defined as: f op ◦ g op :=
op op op op
op
(g ◦ f ) .
That this data satisfies the properties of a category, follows because C is a category,
indeed:
• That the left unit law holds follows by the right unit law of C as follows:
op
f op ◦ Id op = (Id ◦ f ) = f op ,
where the first equality holds by definition of the opposite composition and the
second holds by the right unit law of C.
The right unit law holds analoguously by the left unit law of C.
• That the associativity holds follows by the associativity of C as follows:
op
(f op ◦ g op ) ◦ hop = (g ◦ f ) ◦ hop
= (h ◦ (g ◦ f ))op
op
= ((h ◦ g) ◦ f ) by associativity of C,
= f op ◦ (h ◦ g)op
= f op ◦ (g op ◦ hop )
Solution 216 (Exercise 31). Any preordered set (X, ≤) can be described by a graph
where the vertices are given by the elements of X and there exists an edge from x to
y if and only if x ≤ y. Hence, if we denote by G the corresponding graph of (X, ≤),
we have that Pos(X, ≤) = Graph(G).
In particular we have that the number of edges is either 0 or 1. Hence, a category gen-
erated by a graph comes from a preordered set if and only if the number of morphisms
in any fixed hom-set is either 0 or 1.
If (X, ≤) is a poset, i.e. we have antisymmetry, then we have that the corresponding
graph (and consequently the corresponding category) have no (non-trivial) loops.
Solution 217 (Exercise 34). That f : a → b is an isomorphism with inverse g means
precisely that g ◦ f = Ida and f ◦ g = Idb . But stating that g is an isomorphism with
inverse f means precisely those conditions. Hence, this hold by definition.
49
Solution 218 (Exercise 35). Let f : a → b be an isomorphism. That f has a unique
inverse means that if g, h : b → a are morphisms such that
h ◦ (g ◦ f ) = (f −1 ◦ g −1 ) ◦ (g ◦ f )
= f ◦ (g −1 ◦ g) ◦ f −1 by associativity,
= f ◦ Idb ◦ f −1 since g −1 inverse of g,
= f ◦ f −1 by unit law,
= Ida since f −1 inverse of f .
50
• Assume f : X → Y is a bijection, i.e.
∀y ∈ Y : ∃!xy ∈ X : f (x) = y
We show that the inverse of f is given by:
g : Y → X : y 7→ xy .
So we have to show g ◦ f = IdX and f ◦ g = IdY . Let x ∈ X, since that
g(f (x)) is the unique element z ∈ X such that f (z) = f (x) (and since x satisfies
this condition), we have g(f (x)) = x. Since this holds for all x ∈ X, we have
g ◦ f = IdX .
Let y ∈ Y and let x := xy be the unique element in X such that f (x) = y. So
by definition of g, we have g(y) = x, hence f (g(y)) = f (x) = y.
• Assume f : X → Y is an isomorphism with inverse f −1 . Let y ∈ Y , we have to
show that there exists a unique x ∈ X such that f (x) = y. Define x := g(y).
Since f ◦ g = IdY , we have y = f (g(y)) = f (x), hence, this x indeed satisfies the
condition. To show that x is unique, let z ∈ X satisfy f (z) = y. That z = x
now follows from g ◦ f = IdX , indeed: z = g(f (z)) = g(y) = x.
Solution 222 (Exercise 40). The isomorphisms in Pos are precisely the bijections
f : (X, ≤X ) → (Y, ≤Y ) such that
x1 ≤X x2 ⇐⇒ f (x1 ) ≤Y f (x2 ), (8)
Indeed:
• Assume f is a bijection which satisfies Eq. (8). Since it is a bijection, we know (by
the solution to Exercise 39), that there exists a function g : (Y, ≤Y ) → (X, ≤X )
such that g ◦ f = Id(X,≤X ) and f ◦ g = Id(Y,≤Y ) . However, this does not conclude
the proof of the first implication, because we do not know appriori, that g is a
morphism of posets. So we have to show
∀y1 , y2 ∈ Y : y1 ≤Y y2 =⇒ g(y1 ) ≤X g(y2 ).
Let y1 , y2 ∈ Y . Since f is bijective, there exist x1 , x2 ∈ X such that f (x1 ) = y1
and f (x2 ) = y2 . If f (x1 ) = y1 ≤Y y2 = f (x2 ), then by Eq. (8), we also have
that x1 ≤ x2 . But by definition of g, we have g(y1 ) = x1 and g(y2 ) = x2 ,
hence g(y1 ) ≤X g(y2 ) which shows that g is an order-preserving morphism, i.e.
g ∈ Pos((Y, ≤Y ), (X, ≤X )).
• Assume f is an isomorphism in Pos with inverse g. Since f is a function which
satisfies g ◦ f = Id(X,≤X ) and f ◦ g = Id(Y,≤Y ) , we have (by the same argument as
in the solution to Exercise 39), that f is a bijection. Hence, it remains to show
that Eq. (8) holds. Let x1 , x2 ∈ X.
If x1 ≤X x2 , then we have f (x1 ) ≤Y f (x2 ) since f ∈ Pos((X, ≤X ), (Y, ≤Y )).
Assume f (x1 ) ≤Y f (x2 ). Since g ∈ Pos((Y, ≤Y ), (X, ≤X )), we have g(f (x1 )) ≤X
g(f (x2 )). But g ◦ f = IdX , hence x1 ≤X x2 .
51
Solution 223 (Exercise 41). First, let (X, ≤Y ) be a preorder. A morphism f : x → y
is an isomorphism if and only if there exists a morphism g : y → x such that g ◦ f = Idx
and f ◦ g = Idy . But, in a preorder category, each hom-set has a unique element if it is
non-empty. So, for any g ∈ hom(y, x) and f ∈ hom(x, y), we always have g ◦ f = Idx
and f ◦ g = Idy . Hence a morphism f : x → y in a preorder-category is an isomorphism
if and only if there exists a morphism g : y → x. The existence of a morphism f : x → y
means precisely that x ≤ y. Hence, isomorphisms in a preorder-category corresponds
with a pair of elements (x, y) in X such that x ≤ y and y ≤ x.
If (X, ≤X ) is a poset, i.e. satisfies antisymmetry, then if x ≤ y and y ≤ x, we must
have x = y. Consequently, in a poset-category, the only isomorphisms are the identity
morphisms (i.e. corresponding with x ≤ x).
Solution 224 (Exercise 46). Consider
b o o l 2 I n t : : B o o l −> I n t
bool2Int False = 0
b o o l 2 I n t True = 1
We can go back, so that we get False and True from 0 and 1:
i n t 2 B o o l : : I n t −> B o o l
i n t 2 B o o l n | n == 0 = False
| o t h e r w i s e = True
However, notice that not only 1 is converted back to True, but also everything other
than 0 is converted to True.
We have
Int2Bool ( bool2Int y) = y
for every y :: Bool, but we don’t have bool2Int (int2Bool x) = x for all x :: Int .
We can say that there is enough room in the type integers for it to host a copy of
the type of booleans, but there isn’t enough room in the type of booleans for it to host
a copy of the type of integers.
But notice that there are other ways in which the type Bool lives inside the type Int
as a retract: for example, we can send False to 13 and True to 17, and then send back
everything bigger than 15 to True and everything else to False .
Solution 225 (Exercise 49). The monomorphisms in Set correspond precisely with
the injective functions, i.e. the functions f : X → Y which satisfy
∀x1 , x2 ∈ X : f (x1 ) = f (x2 ) =⇒ x1 = x2 ,
Indeed:
• Assume f is injective. Let g, h : Z → X be functions such that f ◦ g = f ◦ h. We
have to show g = h, i.e.
∀z ∈ Z : g(z) = h(z).
Since f is injective, it suffices to show
∀z ∈ Z : f (g(z)) = f (h(z)).
But this holds by the condition of g and h. Hence, f is indeed a monomorphism.
52
• Assume f is a monomorphism. We have to show that for each x1 , x2 ∈ X, we
have f (x1 ) = f (x2 ). Let 1 = {⋆} be a singleton set and define
g1 : 1 → X : ⋆ 7→ x1 , g2 : 1 → X : ⋆ 7→ x2 ,
Notice that this proof shows that in an arbitrary category, a section is always a
monomorphism.
Solution 227 (Exercise 54). Let f : a ∼
= b be an isomorphism with inverse f −1 . There
are multiple proofs which one can give, an abstract one (which is indirect in the sense
that we use another exercise/lemma) and a more direct one.
• Indirect proof: By the solution of 52, we know that any section (from a section-
retraction pair) is a monomorphism. An analoguous argument shows that any
retraction (section-retraction pair) is an epimorphism. Hence it suffices to show
that f is both a section and a retraction, but this is immediate because f −1 ◦ f =
Ida and f ◦ f −1 = Idb .
• Direct proof: We first show that f is a monomorphism. Assume g1 , g2 : c → a
are morphisms such that f ◦ g1 = f ◦ g2 . We then have that g1 = g2 because
g1 = Ida ◦ g1 = (f −1 ◦ f ) ◦ g1 since f : a ∼
= b,
= f −1 ◦ (f ◦ g1 ) by associativity,
= f −1 ◦ (f ◦ g2 ) by assumption,
= (f −1 ◦ f ) ◦ g2 by associativity
= Ida ◦ g2 since f : a ∼
=b
= g2 .
53
That f is also an epimorphism is analoguous, indeed: Assume g1 , g2 : b → c are
morphisms such that g1 ◦ f = g2 ◦ f . We then have that g1 = g2 because
g1 = g1 ◦ Idb = g1 ◦ (f ◦ f −1 ) since f : a ∼
= b,
= (g1 ◦ f ) ◦ f −1 by associativity,
= (g2 ◦ f ) ◦ f −1 by assumption,
= g2 ◦ (f ◦ f −1 ) by associativity
= g2 ◦ Idb since f : a ∼
=b
= g2 .
Solution 228 (Exercise 55). Let (X, ≤X ) be a preordered set. Any morphism f ∈
Pos(X, ≤X )(x, y) is always both a monomorphism and an epimorphism because hom-
sets have at most on element. But, by Exercise 41, we know that in a poset (not a
preordered set!), the only isomorphisms are the identity morphisms. Hence, if x ≤ y
but x 6= y (living in a poset), then the corresponding morphism in hom(x, y) is both
an epimorphism and monomorphisms but not an isomorphism.
A concrete example is given by e.g. the poset of truth values {0, 1}. We have 0 ≤ 1
and those are not equal.
Solution 229 (Exercise 65). An initial object (and the only one), is the emptyset ∅,
indeed: Let X be a set. Then there is clearly a unique function ∅ → X.
Solution 230 (Exercise 67). A initial object in Pos(X, ≤) is the minimal object, that
is an element ⊥ ∈ X such that
∀y ∈ X : ⊥ ≤ y. (9)
Indeed: Assume x is an initial object in (X, ≤), i.e. for any other element y ∈ X, there
exists a (unique) morphism x → y, i.e. hence, by definition of the hom-sets, we have
x ≤ y. So x is indeed the minimal object.
Conversely, assume ⊥ is a minimal element, hence, for each y ∈ X, we have ⊥ ≤ y.
Hence hom(⊥, y) is non-empty. So it must contain exactly one element. This means
precisely that it is initial.
A somewhat more compact solution is as follows: By definition of Pos(X, ≤), for
each x ∈ X, we have:
Hence an object x is initial if and only if, x ≤ y for all y ∈ X, if and only if it is
minimal.
Solution 231 (Exercise 68). Let A and B be initial objects in C. By initiality of A
(resp. B), there exists a unique morphism f ∈ C(A, B) (resp. g ∈ C(B, A)). That
f and g are inverses follows because g ◦ f ∈ C(A, A) (resp. f ◦ g ∈ C(B, B)). But
by initiality of A (resp. B), C(A, A) (resp. C(B, B)) has a unique element, but both
g ◦ f, IdA ∈ C(A, A) (resp. f ◦ g, IdB ∈ C(B, B)), hence they must be equal.
54
Solution 232 (Exercise 69). Assume A ∈ C0 is initial, B ∈ C0 an arbitrary object and
i:A∼ = B an isomorphism. We have to show that B is initial, i.e. for each X ∈ C0 ,
there should exists a unique morphism B → X.
Fix such an X. By initiality of A, there exists a (unique) morphism f ∈ C(A, X).
If we denote the inverse of i by j, we have f ◦ j ∈ C(B, X). To show that f ◦ j is the
unique morphism in this hom-set, let g ∈ C(B, X). So we have g ◦ i ∈ C(A, X). By
initiality of A, we have g ◦ i = f . The claim now follows by the following computation:
f ◦ j = (g ◦ i) ◦ j = g ◦ (i ◦ j) = g ◦ IdB = g.
Solution 233 (Exercise 71). We give three solutions to this exercise.
• Consider the category generated by the graph:
x y
This category can not have an initial object since there is no morphism from x
to y or vice versa.
• Consider the category generated by the graph:
f
x g y
This category also can not have an initial object, indeed: There is no morphism
from y to x, hence y can not be initial. But also x can not be initial since f and
g are different morphisms.
• Consider the category Pos(Z, ≤), i.e., the category
... −2 −1 0 1 2 ...
This category can not have an initial object: Suppose it has an initial object
x ∈ Z. Then we have another object x − 1 ∈ Z. Since x is initial, we have a
morphism x → x − 1, which means that x ≤ x − 1, which is absurd. Therefore,
this category does not have an initial object.
Solution 234 (Exercise 156). For any set X, we write
(+) : List(X) × List(X) → List(X)
for list concatenation.
For each set X ∈ Set0 , we define:
ηX : X → List(X) : x 7→ [x] := cons(x, nil).
For each function f ∈ Set0 (X, List(Y )), we define, by list recursion, the following
function:
f ∗ : List(X) → List(Y ) (10)
nil 7→ nil (11)
cons(x, xs) 7→ f x + f ∗ xs (12)
We now show that the properties of a Kleisli triple hold:
55
∗
1. For each set X, we have to show ηX = IdT (X) , that is, for a list ℓ ∈ List(X), we
∗
show ηX (ℓ) = ℓ. We prove this by structural induction on the list ℓ.
∗
In case ℓ = nil, we have, by Eq. (11), that ηX (nil) = nil.
In case ℓ = cons(x, xs), we compute
∗ ∗
ηX (cons(x, xs)) = ηX (x) + ηX (xs) by definition of (−)∗ ,
= ηX (x) + IdListX (xs) by induction hypothesis
= [x] + IdListX (xs) by definition of ηX
= [x] + xs
= cons(x, xs).
2. For each function f : X → Y , we have to show f ∗ (ηX (x)) = f (x), this indeed
holds by the following computation:
where the first equality holds by definition of ηX and the second equality holds
by definition of f ∗ .
3. Let f : X → ListY and g : Y → ListZ be functions, we have to show
g ∗ (f ∗ (ℓ)) = (g ∗ ◦ f )∗ (ℓ),
56
– In case f (x) = cons(y, u), with y ∈ Y and u ∈ ListY , we have:
where the second equality holds by definition of cons, the thirth and
fifth by definition of g ∗ and the fourth by the induction hypothesis.
Solution 235 (Exercise 157). For each set X ∈ Set0 , we define:
ηX : X → BinTree(X) : x 7→ leaf(x).
• If t = branch(t1 , t2 ), then
∗ ∗ ∗ ∗
ηX (t) = ηX (branch(t1 , t2 )) = branch(ηX (t1 ), ηX (t2 )) = branch(t1 , t2 ) = t.
2. For each function f : X → Y , we have to show f ∗ (ηX (a)) = f (a), this indeed
holds by the following computation:
g ∗ (f ∗ (t)) = (g ∗ ◦ f )∗ (t).
57
• If t = branch(t1 , t2 ), then is the left-hand-side given by
g ∗ (f ∗ (t)) = g ∗ (f ∗ (branch(t1 , t2 ))) = g ∗ (branch(f ∗ (t1 ), f ∗ (t2 ))) = branch(g ∗ (f ∗ (t1 )), g ∗ (f ∗ (t2 ))).
iX
l : X → X + E, iX
r : E → X + E.
ηX : X → X + E : x 7→ iX
l (x).
∗ ∗ X
ηX (z) = ηX (il (x)) = ηX (x) = iX
l (x) = z = IdX+E (z).
• If z = iX
r (e), then
∗ ∗ X
ηX (e) = ηX (ir (e)) = iX
r (e) = z = IdX+E (z).
2. For each function f : X → Y , we have to show f ∗ (ηX (x)) = f (x) but this holds
directly by the definition of (−)∗ since ηX (x) = iX
l (x).
g ∗ (f ∗ (z)) = (g ∗ ◦ f )∗ (z).
58
• If z = iX
l (x), then
g ∗ (f ∗ (z)) = g ∗ (f ∗ (iX ∗ ∗ ∗ X ∗ ∗
l (x))) = g (f (x)) = (g ◦ f ) (il (x)) = (g ◦ f ) (z).
• If z = iX
r (e), then
g ∗ (f ∗ (z)) = g ∗ (f ∗ (iX ∗ Y Z ∗ ∗ X ∗ ∗
l (e))) = g (il (e)) = il (e) = (g ◦f ) (il (e)) = (g ◦f ) (z).
2. For each function f : X → Y , we have to show f ∗ (ηX (x)) = f (x) but this holds
directly by the definition of (−)∗ since
[
f ∗ (ηX (x)) = f ∗ ({x}) = f (a) = f (x).
a∈{x}
59
Solution 238 (Exercise 160). For each set X ∈ Set0 , we define:
ηX : X → (X → R) → R : x 7→ (λf.f (x)).
For each function f ∈ Set0 (X, ContR (Y )), we define:
f ∗ : ContR (X) → ContR (Y ) : i 7→ λ(j : Y → R), i(f (−)(j)).
Notice that this is indeed well-defined: Let i ∈ ContR (X), i.e. i : (X → R) → R.
Then f ∗ (i) : (Y → R) → R. Let j : Y → R. Then f (−)(j) : X → R, hence we can
apply it to i and we have i (f (−)(j)) ∈ R.
Let i ∈ ContR (X). We now show that this data satisfies the properties of a Kleisli
triple:
∗
1. For each set X, we have to show ηX = IdContR (X) . Let x ∈ X. The claim then
follows by the following computation:
∗
ηX (i) = λj, i (ηX (−)(j)) = λj, i (λx, j(x)) = i
2. For each function f : X → Y , we have to show f ∗ (ηX (x)) = f (x), this follows
by the following computation:
f ∗ (ηX (x)) = f ∗ (λg, g(x)) = λj, ((λg, g(x)) (f (−)(j))) = λj, (f (x)(j)) = f (x).
60
Solution 239 (Exercise 161). For each set X ∈ Set0 , we define:
ηX : X → (R → X) : x 7→ (λ , x).
f ∗ : (R → X) → (R → Y ) : g 7→ λr, f (g(r))(r).
f ∗ (ηX (x))(r) = f ∗ (λ , x)(r) = (λr′ , f ((λ , x)(r′ ))(r′ )) (r) = f ((λ , x)(r))(r) = f (x)(r).
(g ∗ ◦ f )∗ (φ) = g ∗ (f ∗ (φ)).
RHS =g ∗ (f ∗ (φ))(r)
=g ∗ (λs, f (φ(s))(s)) (r)
=g(f (φ(r))(r))(r).
Thus, the left and right hand sides compute to the same term, hence they are
equal.
Solution 240 (Exercise 72). An initial object is a one-element set ({⋆}, ⋆). Let (X, x)
be a pointed set. Then we have a unique function ⋆ → X that sends ⋆, the chosen
(and only) point of {⋆}, to x, the chosen point of X, namely f : ⋆ 7→ x.
61
Solution 241 (Exercise 79). A terminal set in the category of sets is a one-element
set {⋆}. Given any set X, we have a unique function X → ⋆, since any element of X
must be sent to ⋆.
Solution 242 (Exercise 81). Given a poset (X, ≤), a terminal object in the category
Pos(X, ≤) is exactly a maximal element in X. Indeed, given such a maximal element
x ∈ X = Pos(X, ≤)0 , we have for all y ∈ Pos(X, ≤)0 , since x is maximal, that y ≤ x.
Therefore, we have a morphism f : x → y. By the definition of the hom-sets in
Pos(X, ≤), f is unique and we conclude that x is a terminal object. Conversely, un-
folding the definition of terminal object shows that a terminal object yields a maximal
element.
Solution 243 (Exercise 82). Suppose that we have a category C and two terminal
objects B, B ′ ∈ C0 . Since B ′ is terminal, we have a morphism f : B → B ′ and since
B is terminal, we have a morphism g : B ′ → B. Note that we have two morphisms
from B to B, namely g ◦ f and IdB . Also, because B is terminal, there exists a unique
morphism B → B. Therefore, g ◦ f = IdB . In the same way, we have f ◦ g = IdB ′ .
Therefore, f is the isomorphism (with inverse g) between B and B ′ that we are looking
for.
Solution 244 (Exercise 83). Let C be a category and take B, B ′ ∈ C0 objects in C.
Suppose that we have an isomorphism i : B ∼ = B ′ and that B is a terminal object in
′
C. We have to show that B is terminal. In other words, for all A ∈ C0 , we have to
show that there exists a unique morphism f : A → B ′ .
Now, given such an object A ∈ C0 , we have a morphism f : A → B by terminality
of B. Therefore, we have a morphism g = i ◦ f : A → B ′ . This proves existence. For
uniqueness, suppose that we also have another morphism h : A → B ′ . Then we have
morphisms i−1 ◦ g and i−1 ◦ h from A to B. Since B is terminal, there exists only one
morphism from A to B, so i−1 ◦ g = i−1 ◦ h. Therefore, we have
g = i ◦ (i−1 ◦ g) = i ◦ (i−1 ◦ h) = h
62
• Consider the category generated by the graph:
x y
This category can not have a terminal object since there is no morphism from x
to y or vice versa.
• Consider the category generated by the graph:
f
x g y
This category also can not have a terminal object, indeed: There is no morphism
from y to x, hence x can not be terminal. But also y can not be terminal since
f and g are different morphisms.
• Consider the category Pos(N, ≤), i.e., the category
0 1 2 ...
This category can not have a terminal object: Suppose it has a terminal object
n ∈ N. Then we have another object n + 1 ∈ N. Since n is terminal, we have a
morphism n + 1 → n, which means that n + 1 ≤ n, which is absurd. Therefore,
this category does not have a terminal object.
Solution 247 (Exercise 91). Given A, B ∈ Set0 , we claim that the cartesian product
A × B with the left projection πl : (a, b) 7→ a and right projection πr : (a, b) 7→ b is a
product of A and B.
Indeed, given an object Q ∈ Set0 with morphisms l : Q → A and r : Q → B, we
have a morphism f := hl, ri : Q → A × B, given by q 7→ (l(q), r(q)). For all q ∈ Q,
we have πl ◦ f (q) = πl (l(q), r(q)) = l(q) and πr ◦ f (q) = πr (l(q), r(q)) = r(q), so the
diagram commutes, which proves existence.
Now, for uniqueness, suppose that we also have another morphism g : Q → A × B
that makes the diagram commute. Note that we can write all elements p ∈ A × B as
p = (πl (p), πr (p)). Then we have, for all q ∈ Q,
g(q) = (πl (g(q)), πr (g(q))) = (πl ◦ g(q), πr ◦ g(q)) = (l(q), r(q)) = f (q),
63
This means that we need an object C such that C ≤ A and C ≤ B, and for all
objects D such that D ≤ A and D ≤ B, we also have D ≤ C. Therefore, a product of
A and B is the (unique up to isomorphism) greatest element that is less than or equal
to A and B, if it exists.
Remark 249. The uniqueness up to isomorphism is necessary. For example, consider
the poset-category generated by the following diagram:
A B
X Y
For the product of A and B, the candidates are X and Y , since we have both X ≤ A
and X ≤ B, and Y ≤ A and Y ≤ B. However, X cannot be the product, since if we
are given D = Y , we need Y ≤ X, which is not true. In the same way, Y cannot be
the product, because X ≤ Y does not hold.
Solution 250 (Exercise 94). In a category C, given two objects A, B ∈ C0 , suppose
that we have two products C, C ′ ∈ C0 , with projections πl : C → A, πr : C → B,
πl′ : C ′ → A and πr′ : C ′ → B.
Since C ′ is a product, and we have the morphisms πl and πr , the universal property
gives a morphism f : C → C ′ such that πl = πl′ ◦ f and πr = πr′ ◦ f . In the same way,
we have a morphism g : C ′ → C such that πl′ = πl ◦ g and πr′ = πr ◦ g.
Since C is a product, the universal property gives that there is exactly one morphism
h : C → C such that πl ◦ h = πl and πr ◦ h = πr . The morphism h = IdC satisfies this
property. However, we also have the morphism g ◦ f : C → C, such that πl ◦ g ◦ f =
πl′ ◦ f = πl and πr ◦ g ◦ f = πr′ ◦ f = πr . Therefore, g ◦ f = h = IdC . In the same way,
f ◦ g = IdC ′ , so f : C ∼
= C ′ is an isomorphism, with inverse g.
Solution 251 (Exercise 95). Let C be a category and take objects A, B ∈ C0 . Let
(P, πl : P → A, πr : P → B) be a product of A and B in C. Let P ′ ∈ C0 be another
object, and suppose that we have an isomorphism f : P ∼ = P ′.
We claim that (P , πl , πr ), with πl = πl ◦ f and πr = πr ◦ f −1 is also a product of
′ ′ ′ ′ −1 ′
Q
l r
h
A πl P πr B
f
πl′ πr′
′
P
By the universal property of the product, we have a morphism h : Q → P such that
πl ◦ h = l and πr ◦ h = r. We take g = f ◦ h. We have
πl′ ◦ g = (πl ◦ f −1 ) ◦ (f ◦ h) = πl ◦ h = l
64
and
πr′ ◦ g = (πr ◦ f −1 ) ◦ (f ◦ h) = πr ◦ h = r.
This proves existence.
For uniqueness, suppose that we have two morphisms, g, g ′ : Q → P ′ such that
πl ◦ g = l = πl′ ◦ g ′ and πr′ ◦ g = r = πr′ ◦ g ′ . By the universal property of the product,
′
65
Solution 254 (Exercise 98). Let C be a category with a choice of product (A ×
B, πl , πr ) for any two objects A, B ∈ C0 . Take objects A, B, C, D ∈ C0 and morphisms
f : A → C and g : B → D.
πl πr
A A×B B
f ◦πl g◦πr g
f h
C C×D D
πl′ πr′
A×B
πr πl
B g f A
πl′ πr′
B×A
We have the products (A× B, πl , πr ) and (B × A, πl′ , πr′ ). By the universal properties
of the products A × B and B × A, we get morphisms f : A × B → B × A and
g : B × A → A × B, which make the diagram commute.
By the universal property of the product A × B, there exists a unique morphism
h : A × B → A × B such that πl ◦ h = πl and πr ◦ h = πr . Since IdA×B satisfies this,
we have h = A × B. We also have the morphism g ◦ f : A × B → A × B. We have
πr ◦ g ◦ f = πl′ ◦ f = πr
in
F (µF ) µF
66
Since F (IdµF ) = IdF (µF ) , the morphism IdµF satisfies this property, and therefore
LinM = IdµF .
Solution 257 (Exercise 184). Let F : C → C be an endofunctor and let (µF , in) be
an initial algebra. Given F -algebras (C, φ) and (D, ψ) and f ∈ C(C, D), we have the
following commutative diagrams:
in in
F µF µF F µF µF
F (LφM) LφM F (LψM) LψM
φ ψ
FC C FD D
Now, suppose that f ◦ φ = ψ ◦ F (f ).
By definition, LψM is the unique morphism from µF to D such that
LψM ◦ in = ψ ◦ F (LψM).
(f ◦ LφM) ◦ in = ψ ◦ F (f ◦ LφM).
f ◦ LφM ◦ in = f ◦ φ ◦ F (LφM)
= ψ ◦ F (f ) ◦ F (LφM)
= ψ ◦ F (f ◦ LφM).
f
X = F (X) X
FA : C → C : X 7→ F (A, X),
67
exists.
We define a functor G : C → C by G(A) = µFA . For A, B ∈ C0 , we have an initial FA -
algebra (µFA , inA : FA (µFA ) → µFA ) and an initial FB -algebra (µFB , inB : FB (µFB ) →
µFB ). Now, for all f ∈ C(A, B), we have by the bifunctoriality of F , a morphism
Then (µFB , inB ◦ F ( , µFB )(f ) : FA (µFB ) → µFB ) is an FA -algebra, so by the initiality
of µFA , we have a unique morphism f ′ : µFA → µFB such that
For the second property, given objects A, B, C ∈ C0 , and f : C(A, B) and g : C(B, C),
we have that
inB ◦ F ( , µFB )(f ) ◦ FA (G(f )) = G(f ) ◦ inA
and
inC ◦ F ( , µFC )(g) ◦ FB (G(g)) = G(g) ◦ inB ,
and we know that G(g ◦ f ) : µFA → µFC is the unique morphism such that
68
Then we have the following equality:
0 7→ ⋆
s(n) 7→ n
∞ 7→ ∞
We need to show that for all F -coalgebras (X, g), there exists a unique F -coalgebra
morphism ϕ : (X, g) → (Nc , f ). To this end, take an arbitrary F -coalgebra (X, g).
We first show existence. Borrowing some notation from monads, given the morphism
g : X → F X, we write g ∗ for the induced morphism g ∗ : F X → F X and we write
(g ∗ )n = g ∗ ◦ g ∗ ◦ · · · ◦ g ∗ . We define ϕ : X → Nc as follows: For x ∈ X, if for some
n ∈ N, (g ∗ )n (x) ∈ X and (g ∗ )s(n) (x) = ⋆, we set ϕ(x) = n. Else, we set ϕ(x) = ∞.
We need to show that the following diagram commutes:
g
X 1+X
ϕ Id1 +ϕ
f
N + {∞} 1 + N + {∞}
69
will now show the induction step: suppose that it holds for some n. Take x ∈ X.
If (g ∗ )n (g(x)) = (g ∗ )s(n) (x) ∈ X and (g ∗ )s(n) (g(x)) = (g ∗ )s(s(n)) (x) = ⋆, we have
ψ(g(x)) = n. Then we have f (ψ(x)) = (Id1 +ψ)(g(x)) = n. Therefore, f (ψ(x)) = s(n),
which proves the induction step.
Now, take x ∈ X and suppose that for all n ∈ N, (g ∗ )n (x) ∈ X. If ψ(x) = n for some
n, then (since the diagram commutes), we have ψ(g(x)) = f (ψ(x)) = f (n) = n − 1.
Repeating this n times, we have (Id1 + ψ)((g ∗ )n (x)) = 0. We take x′ = (g ∗ )n (x) ∈ X.
We have ψ(x′ ) = 0 and f (x′ ) = ⋆. However, g(x′ ) ∈ X, so (Id1 + ϕ)(g(x′ )) 6= ⋆ and the
diagram does not commute. Therefore, we cannot have ψ(x) ∈ N, so ψ(x) = ∞ = ϕ(x).
Therefore, ψ = ϕ and we conclude that ϕ is unique, so (Nc , f ) is a terminal F -
coalgebra.
Solution 261 (Exercise 208). We wish to define h, t : N → N such that the following
diagram commutes:
hh,ti
N N×N
nats Id×nats
Stream(N) N × Stream(N)
hhead,taili
This means:
h = IdN , t = succ.
By construction, we have that Eq. (14) and Eq. (15) indeed holds for h := IdN and
t := succ, which shows that the sought function nats : N → Stream(N) is the unique
solution of the equation system
head ◦ nats = Id
tail ◦ nats = nats ◦ succ
and thus can be defined as the anamorphism JhId, succiK.
Solution 262 (Exercise 209). We wish to define h : Stream(A) × Stream(B) → A × B
and t : Stream(A) × Stream(B) → Stream(A) × Stream(B) such that the following
diagram commutes:
70
hh,ti
Stream(A) × Stream(B) A × B × Stream(A) × Stream(B)
zip Id×zip
Stream(A × B) Stream(A × B)
hhead,taili
This means:
By construction, we have that Eq. (16) and Eq. (17) indeed holds for h := head × head
and t := tail × tail,
which shows that the sought function zip : Stream(A) × Stream(B) → Stream(A × B)
is the unique solution to the equation system
71
• The function on objects is given by
Notice that if one defines a category whose objects are sets M together with an
associative binary operation m : M → M → M , then one could analoguously also
define a forgetful functor from Monoid to this category by only forgetting the neutral
element.
Lemma 264. The forgetful functor from Monoid to Set satisfies the properties of a
functor.
Proof. We clearly have that everything is well-defined since the codomain is Set.
That the identity morphism is preserved holds by definition because the identity mor-
phism of (M, m, e) (in Monoid) is given by the identity function and the identity
morphism of M (in Set) is also given by the identity function.
That the composition of morphisms is preserved also holds by definition because the
composition of morphisms (in Monoid) is given by the composition of the underlying
functions which is also the composition in Set.
The forgetful functor F orget from Monoid to Set forgets the algebraic structure
of a monoid and since there are multiple monoid structures on the same set (given
an example of this), hence we do not have that there exists some functor G : Set →
Monoid such that F orget · G is the identity on Monoid. However, to each set, one
can define a monoid which satisfies an important property (this is Proposition 268).
The associated monoid is called the free monoid :
Example 265. Let X be a set. The free monoid generated by X, denoted by
F ree(X), is specified by the following data:
• The underlying set consists of all finite sequences/strings of elements in X (in-
cluding the empty sequence).
• The multiplication is defined by concatenating the sequences, i.e.
72
• The function on morphisms is given as follows: A morphism f ∈ Set(X, Y ) (i.e.
a function) is mapped to the monoidal morphism which is given by pointwise
application of f , i.e.
F reeX
! : X → F ree(X) : x 7→ (x).
This function satisfies the property that any function from X to an arbitrary monoid
corresponds with a unique morphism (of monoids) from the free monoid generated by
X to that monoid:
Proposition 268. Let (M, m, e) be a monoid and X be a set. For any morphism f ∈
Set(X, M ) (i.e. a function), there exists a unique morphism φf ∈ Monoid(F ree(X), (M, m, e)),
such that f = F reeX f
! ·φ .
We have to define separately what happens with the empty sequence. The empty
sequence we map to the identity element e, so in particular we have that the identity
element is preserved under φf , so in order to conclude that φf is a morphism of
monoids, it remains to show that it preserves the binary operation but this is clear by
definition.
That f = F reeX f X
! · φ holds follows immediately by the definition of F ree! and φ ,
f
indeed:
F reeX f
(x) = φf ((x)) = f (x).
! ·φ
73
The following exercise shows that there is a special connection between the forgetful
functor forget : Monoid → Set and the free functor free : Set → Monoid. This
connection expresses that these form a so-called adjoint pair (see Section 6).
Exercise 269. Show that for any set X and monoid (M, m, e), there exist bijections
between the hom-sets:
(M,m,e)
αX : Monoid(F ree(X), (M, m, e)) → Set(X, f orget(M, m, e)).
written as f 7→ F (f ).
Moreover, this data should satisfy the following properties:
• (Preserves composition) For f ∈ homC (X, Y ) and g ∈ homC (Y, Z), we have
F (g ◦ f ) = F (f ) ◦ F (g).
• (Preserves identity) For X ∈ C, we have F (IdX ) = IdF (X) .
Exercise 273. Notice that the preservation of composition has now changed, why is
this the case?
An example of a contravariant functor is given by the powerset-functor:
74
Example 274. Recall that the powerset of a set X, denoted by P(X), is the set of
all subsets of X, i.e.
P(X) := {A | A ⊆ X} .
The contravariant powerset-functor5 (on sets), denoted by P, is the functor from
Set to Set defined by the following data:
• The function on objects is given by:
Set0 → Set0 : X 7→ P(X).
Exercise 275. Show that P, defined in Example 274 satisfies the properties of a
contravariant functor.
References
[1] Michael Barr and Charles Wells. Category Theory for Computing Science. Orig-
inally published: Prentice-Hall International Series in Computer Science, 1995.
url: http://www.tac.mta.ca/tac/reprints/articles/22/tr22abs.html.
[2] The Catsters. Video lectures on category theory. url: https://www.youtube.com/user/TheCatsters.
[3] Maarten M. Fokkinga. 1990. url: https://maartenfokkinga.github.io/utwente/mmf90.pdf.
[4] HaskellWiki. Hask — HaskellWiki. [Online; accessed 9-April-2022]. 2012. url:
https://wiki.haskell.org/index.php?title=Hask&oldid=52908.
[5] Graham Hutton. “A Tutorial on the Universality and Expressiveness of Fold”. In:
J. Funct. Program. 9.4 (1999), pp. 355–372. url: http://journals.cambridge.org/action/displayAbst
[6] Tom Leinster. Basic Bicategories. https://arxiv.org/abs/math/9810017. 1998.
eprint: math/9810017. url: https://arxiv.org/abs/math/9810017.
[7] Tom Leinster. Basic Category Theory. Available under a Creative Commons
license. url: https://arxiv.org/abs/1612.09375.
[8] Erik Meijer, Maarten M. Fokkinga, and Ross Paterson. “Functional Program-
ming with Bananas, Lenses, Envelopes and Barbed Wire”. In: Functional Pro-
gramming Languages and Computer Architecture, 5th ACM Conference, Cam-
bridge, MA, USA, August 26-30, 1991, Proceedings. Ed. by John Hughes. Vol. 523.
Lecture Notes in Computer Science. Springer, 1991, pp. 124–144. doi: 10.1007/3540543961\_7.
url: https://doi.org/10.1007/3540543961%5C_7.
5 Sincea function is mapped to the inverse-image function, one also calls the powerset-functor, an
inverse image-functor
75
[9] Benjamin Pierce. A taste of category theory for computer scientists. Available for
free. 1988. url: https://kilthub.cmu.edu/articles/journal_contribution/A_taste_of_category_t
[10] Varmo Vene. “Categorical programming with inductive and coinductive types”.
PhD thesis. University of Tartu, 2000.
76