Try   HackMD

An Axiomatic Theory of Structure

Categories, Functors, Natural Transformations

(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

(M,η,μ) as a functor
M
together with two natural transformations
η
and
μ
satisfying the (suitably adapted) monoid laws.

I started the lecture with the following tower of abstractions:

  • Numbers are example of monoids.
  • Monoids are examples of algebras.
  • Algebras organise themselves as categories.

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.

Examples

In terms of the hierarchy of abstractions above, the important example is that, given a signature

Σ, the collection of all
Σ
-algebras together with their homomorphisms forms a category.

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]

Axioms

We just list the definitions. It may be helpful to interleave reading the definitions with the examples of the next section.

Category

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

f:AA closed under composition. Now we generalise this to
f:AB
where the domain
A
of
f
and the codomain
B
of
f
are allowed to be different.

Definition: A category

A consists of a collection of "objects" and for all objects
A,BA
a set
A(A,B)
of "arrows".

We write

f:AB for
fA(A,B)
.

For all

AA there is an "identity" arrow
idA:AA
.

For all

A,B,C there is a "composition" operation
ABC:A(B,C)×A(A,B)A(A,C)

We write

fg=h for
ABC(f,g)=h
. The axioms are for all
h:AB

hidA=h=idBh

and for all
f:CD,g:BC
,
h:AB

(fg)h=f(gh)

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).

Functor

A functor generalises the notion of monoid homomorphism from monoids to categories.

Definition: Let

A,B be two categories. A functor
F:AB

is a function from the objects of

A to the objects of
B
and, for each pair
A,A
of objects of
A
a function
FAA:A(A,A)B(FA,FA)

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?

Natural Transformations

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

F,G:AB. A natural transformation
τ:FG

is a parameterised collection

τA:FAGA satisfying
GfτA=τAFf
, or, in a picture,

for all

f:AA.

More Examples

The paradigmatic examples of a category are

  • the category of sets and functions and
  • the category of types and programs

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.

Categories of types and programs

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

():SetSet

that maps an alphabet

X to the set
X
of words over
X
is a functor.

Exercise: Define

() on functions and verify that the laws of a functor are satisfied.

Notation: In work motivated in terms of programming languages this functor is often denoted by

List. In Haskell it is written using square brackets. Indeed, if 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.
  • Naturality is exactly the property that 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.

Categories of sets

The category

Set has all sets as objects and all functions as arrows. (You may want to check that the axioms of a category are satsified.)

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

A×B={(a,b)aA,bB}

Notice that this definition makes crucial use of

. But the language of category theory only has
id
and
.

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

PX be the set of subsets of
X
. For a function
f:XY
, define
Pf:PXPY
as direct image, that is,
Pf(a)={f(x)xa}

for all

aX.[5] Show that
P
is a functor.

There is an opportunity in the homework to pursue this a bit further.

Categories of structures

Slogan: Mathematical structures of any kind organise themselves in a category.

The cateogy of

  • monoids
  • groups
  • orders
  • topological spaces

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.

Categories of relations

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.

Categories as structures

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.

Categories as logical theories

In any reasonable logic there is a proof that

A implies
A
. We can think of this as the identity
id:AA
in a category. Moreover, whenever I have a proof
f:AB
and a proof
g:BC
then there is a proof
f;g
of
AC
.

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.

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.

Application

(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.

Homework

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.

(Programming) Polymorphic Functions as Natural Transformations

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

SetSet). It may be fewer functions than you think at first sight. [6]

(Maths) Polymorphic Functions as Natural Transformations

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.

  • Define for each function
    AB
    a natural transformation
    A×XB×X
    .
  • Show that each natural transformation
    A×XB×X
    is induced by a function
    AB
    .
  • Recall the definition of
    XA
    as the set of functions
    AX
    . Define for each function
    BA
    a natural transformation
    XAXB
    .
  • Show that each natural transformation
    XAXB
    is induced by a function
    BA
    .

(Maths) Sets as Quotients of Lists

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

List and
P
. Define a function
List(X)PX

and show that is a natural transformation.

Outlook

There is so much to say get in touch if you want to know more I may fill in sth later

References and Further Reading

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


  1. 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. ↩︎

  2. 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. ↩︎

  3. 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. ↩︎

  4. There are interesting categories in which the "sets and functions" intuition is misleading. But we will not encounter them in this short introduction. ↩︎

  5. A more widely used notation for

    Pf(A) is
    f[A]
    . ↩︎

  6. 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 ↩︎