(not in final form)
This is the center piece of my Short of Introduction to Monads. While we have already seen many examples of categories, functors and natural transformations (without calling them that), we will now meet the mathematical definitions. This will allow us, in the next lecture, to define a monad
I started the lecture with the following tower of abstractions:
In the last lecture we have seen our first definition of algebraic structures as mathematical objects specified by operations and equations.
Now we are going to present an axiomatic theory of structure.
In the lecture we went through the Axioms and then tried to characterize the polymorphic functions/natural transformations of the types indicated in polymorphic.hs do exist. We skipped the other material in this write-up.
Programs: polymorphic
.
In terms of the hierarchy of abstractions above, the important example is that, given a signature
Instead of studying the structure of an algebra, we are going to study the structure of the collection of all algebras.
To remember the axioms of a category, it will be good to know that a monoid is also a category. In fact, as we will see, we can think of a category as a many-sorted, or typed, monoid.
Important parts of category theory are inspired by the fact that every order is also a category. But this perspective will not be exploited in this course.
Just a quick remark on monoids and orders as categories: I find it intriguing that category theory combines the two most common mathematical models for irreversible phenomena, monoids and orders, in one definition. [1]
We just list the definitions. It may be helpful to interleave reading the definitions with the examples of the next section.
One way to understand the definition of a category is as a "many-sorted" monoid. As we have seen in the first lecture, one can think of a monoid as a set of functions
Definition: A category
We write
For all
For all
We write
and for all
Exercise: Show that a one-object category is exactly a monoid.
A category in which there is at most one arrow between any two objects is called a thin category.
Exercise: Show that a thin category is exactly an order (that is, a reflexive and transitive relation).
A functor generalises the notion of monoid homomorphism from monoids to categories.
Definition: Let
is a function from the objects of
that preserves identities and composition.
Exercise: Show that a functor betwee one-object category is exactly a monoid homomorphism. Similarly, a functor between thin categories is exactly an order preserving map.
Exercise: Show that data List a = Nil | Cons a (List a)
in Haskell defines a functor. What other type constructors in Haskell are functors?
We introduced categories and functors, but, so far, conceptually, we did not do anything remarkable from the point of view of mathematics. Indeed, catgories and functors are just a variation on a familiar theme that we illustrate with the table below.
structure | morphism |
---|---|
monoid | monoid homomorphism |
order | monotone map |
graph | graph homomorphism |
category | functor |
But now we will see the first definition that sets category theory apart. In fact, the story goes, categories and functors where discovered when Eilenberg and Mac Lane tried to capture the concept of a "natural map" mathematically. Technically, we extend the table above so that one obtains a good notion of "category of funtors". [2]
structure | morphism |
---|---|
functor | natural transformation |
Definition: Let
is a parameterised collection
…
for all
The paradigmatic examples of a category are
A large amount of category theory can be understood as an axiomatic characterisation of these two examples.[3] So we look at these examples first. The other examples are less important for now, but illustrate that category theory reaches much further.
The example of types and programs is very closely related to the example of sets and function. For the purposes of this section the difference is only one of emphasis and perspective.
The operation
that maps an alphabet
Exercise: Define
Notation: In work motivated in terms of programming languages this functor is often denoted by a
is a type, then [a]
is the type of lists over a
. To see this for yourself, head over to lists
and enter the following in the console:
:type reverse
reverse
is a predefined function that reverses a list. The type of reverse is
reverse :: [a] -> [a]
Here, a
is a type variable that can be instantiated with any type. Indeed, the algorithm for reversing a list does not depend on the type of the elements of the list.
Now it is easy to explain the idea behind the definition of a natural transformation:
reverse
is a natural transformation.reverse
can be implemented generically for all types of elements.Exercise: Show that reverse
is a natural transformation.
In programming languages terminology, naturality is an example of parametric polymorphism.
Exercise: What other examples of parametric polymorphic functions can you find?
We have just seen a first glimpse of type theory. There is a bit more in the homework below. Ask me if you want suggestions for further reading.
The category
If this course was an introduction to category theory, we would now revisit definitions and construction that are defined in set-theory in terms of the elementship relation such as, for example, the cartesian product
Notice that this definition makes crucial use of
So how can we define the product of two sets in the language of category theory?
In fact, and maybe surprisingly, it is possible to develop set theory in the language of category theory. Ask me if you want to know more about this. (This area of mathematics is known as topos theory.)
For now, we only need to feel the comfort of a familiar example. If in doubt of how to think of a category, just think in terms of sets and functions. [4]
Exercise: Let
for all
There is an opportunity in the homework to pursue this a bit further.
Slogan: Mathematical structures of any kind organise themselves in a category.
The cateogy of
All of these categories are similar to the category of sets. We can think of the extra structure as eliminating some of the functions, namely those that are not structure preserving. Categories of structures are subcategories of the category of sets.
Categories of structures are the main topic of many introductory category theory books such as the ones by Mac Lane and by Adamek, Herrlich and Strecker.
What I mean here are categories where the arrows are not functions but relations. These categories have rather different properties than categories where arrows are functions.
Two prominent approaches to categories of relations are allegories and cartesian bicategories.
We have already seen two main examples: Every monoid is a category. Every order is a category. With some extra work one can also include metric spaces and variations into this picture.
The development of these ideas leads to enriched category theory.
In any reasonable logic there is a proof that
From this point of view, a category is a logical theory and, vice versa, a logical theory is a category.
This idea turned out to be very fruitful. Categorical logic is a rich area of logic with close relations to topos theory, type theory, homotopy type theory.
(if there is time … a digression in some ways, but indispensable to better understand natural transformations, polymorphism, … now delegated to one or two optional separate lectures)
Yoneda's lemma
Corollary: Every group is a group of permutations. Every monoid is a monoid of functions.
(I'd love to do this, but I think there will be no time and it is a digression anyway when the aim are monads … moreover, before doing this one should spend some time on natural transformations)
Something from the theory of species.
Functors as mathematical structures up to isomorphism.
Deriving one example of a combinatorial formula.
These exercises are about polymorphic functions/natural transformations.
The first exercise is, in my opinion, essential for a deeper understanding of category theory, but it can be skipped if one is interested mainly in the monad story.
From a programmer's point of view, implement as many functions of the types indicated in polymorphic
as you can. From a mathematicians point of view try to characterise, for any given type, the set of all functions (considered as natural transformation between functors
This exercise is really the same as the previous one, now focussing more on the mathematics side than on the programming side.
Exercise (Optional): These exercises (in particular the 2nd and 4th) are more challenging. Try them if you want to learn more about category theory. If you just want the fastest route to monads, ignore them.
The next exercise is one of the simplest examples of a natural transformation that is quotienting with respect to a set of equations. It illustrates that in mathematics there are functors and natural transformation that do not necessarily have an immediate implementation in a programming language.
Recall the definitions of the functors
and show that is a natural transformation.
There is so much to say … get in touch if you want to know more … I may fill in sth later …
As a first introduction to category theory, I like the book "Conceptual Mathematics" by Lawvere and Schanuel but as far as I can see it is not available online. Neither is the classical "Categories for the Working Mathematician" by Mac Lane nor the monograph "Algebraic Theories" by Manes. But there is a lot of excellent material available.
The first book on category theory I read as a PhD student was
I think it still is a very good introduction. A more advanced book, and an excellent source on toposes and monads (called triples there), is
Another classic and important reference is
More recent introductions to category, which I have not read myself, but which look very promising and commendable are
An excellent book for learning more advanced category theory as it is used in modern mathematics is
Category theory is an axiomatic theory of strucure. But each category is also an example of mathematical structure. This self-reference plays an important role in various advanced areas. ↩︎
This remark goes beyond the scope of these lectures. One can axiomatise the set of all functions in category theoretic terms, obtaining the notion of a so-called cartesian closed category. The notion of natural transformation can be justified by the fact that it makes the category of categories a cartesian closed category. ↩︎
In fact, there is more than one category of sets and there is more than one category of types and programs. But we will have no time to pursue this further for now. ↩︎
There are interesting categories in which the "sets and functions" intuition is misleading. But we will not encounter them in this short introduction. ↩︎
A more widely used notation for
For example, there is only one function/natural transformation of type a->a
. There are only two functions/natural transformations of type a->a->a
. Etc ↩︎