Last time we worked through some basic examples of universal properties, specifically singling out quotients, products, and coproducts. There are many many more universal properties that we will mention as we encounter them, but there is one crucial topic in category theory that we have only hinted at: functoriality.
As we’ve repeatedly stressed, the meat of category theory is in the morphisms. One natural question one might ask is, what notion of morphism is there between categories themselves? Indeed, the most straightforward way to see category theoretic concepts in classical mathematics is in a clever choice of functor. For example (and this example isn’t necessary for the rest of the article) one can “associate” to each topological space a group, called the homology group, in such a way that continuous functions on topological spaces translate to group homomorphisms. Moreover, this translation is functorial in the following sense: the group homomorphism associated to a composition is the composition of the associated group homomorphisms. If we denote the association by a subscripted asterisk, then we get the following common formula.
This is the crucial property that maintains the structure of morphisms. Again, this should reinforce the idea that the crucial ingredient of every definition in category theory is its effect on morphisms.
Functors: a Definition
In complete generality, a functor is a mapping between two categories which preserves the structure of morphisms. Formally,
Definition: Let
- For each object
an associated object . - For each morphism
a corresponding morphism . Specifically, for each we have a set-function .
There are two properties that a functor needs to satisfy to “preserve structure.” The first is that the identity morphisms are preserved for every object; that is,
We often denote a functor as we would a function
Let’s look at a few simple examples.
Let
There are many examples of functors from the category
One trivial example of a functor is called the forgetful functor. Let
One interesting way to think of a functor is as a “realization” of one category inside another. In particular, because the composition structure of
The Hom Functor
There is a very important and nontrivial example called the “hom functor” which is motivated by the category of vector spaces. We’ll stick to the concrete example of vector spaces, but the generalization to arbitrary categories is straightforward. If the reader knows absolutely nothing about vector spaces, replace “vector space” with “object” and “linear map” with “morphism.” It won’t quite be correct, but it will get the idea across.
To each vector space
Now the mapping
so a morphism in this category takes as input a linear map
Okay, ready?
The morphisms in this category can be thought of as linear maps
And so if we apply the
But wait a minute! The mapping here is going in the wrong direction: we took a map in one category going from the
We advise the reader to write down the commutative diagram and trace out the compositions to make sure everything works out. But this is a problem, because it makes the hom functor fail the most important requirement. In order to fix this reversal “problem,” we make the following definition:
Definition: A functor
And so the hom functor on vector spaces is a contravariant functor, while all of the other functors we’ve defined in this post are covariant.
There is another way to describe a contravariant functor as a covariant functor which is often used. It involves the idea of an “opposite” category. For any category
We leave it to the reader to verify that this is indeed a category. It is also not hard to see that
Functors as Types
Before we move on to some code, let’s take a step back and look at the big picture (we’ve certainly plowed through enough details up to this point). The main thesis is that functoriality is a valuable property for an operation to have, but it’s not entirely clear why. Even the brightest of readers can only assume such properties are useful for mathematical analysis. It seems that the question we started this series out with, “what does category theory allow us to do that we couldn’t do before?” still has the answer, “nothing.” More relevantly, the question of what functoriality allows us to do is unclear. Indeed, once again the answer is “nothing.” Rather, functoriality in a computation allows one to analyze the behavior of a program. It gives the programmer a common abstraction in which to frame operations, and ease in proving the correctness of one’s algorithms.
In this light, the best we can do in implementing functors in programs is to give a type definition and examples. And in this author’s opinion this series is quickly becoming boring (all of the computational examples are relatively lame), so we will skip the examples in favor of the next post which will analyze more meaty programming constructs from a categorical viewpoint.
So recall the ML type definition of a category, a tuple of operations for source, target, identity, and composition:
datatype ('object, 'arrow)Category =
category of ('arrow -> 'object) *
('arrow -> 'object) *
('object -> 'arrow) *
('arrow * 'arrow -> 'arrow)
And so a functor consists of the two categories involved (as types), and the mapping on objects, and the mapping on morphisms.
datatype ('cObject, 'cArrow, 'dObject, 'dArrow)Functor =
aFunctor of ('cObject, 'cArrow)Category *
('cObject -> 'dObject) *
('cArrow -> 'dArrow) *
('dObject -> 'dArrow)Category
We encourage the reader who is uncomfortable with these type definitions to experiment with them by implementing some of our simpler examples (say, the size functor from sets to integers). Insofar as the basic definitions go, functors are not all that interesting. They become much more interesting when additional structure is imposed on them, and in the distant future we will see a glimpse of this in the form of adjointness. We hope to get around to analyzing statements like “syntax and semantics are adjoint functors.” For the next post in this series, we will take the three beloved functions of functional programming (map, foldl(r), and filter), and see what their categorical properties are.
Until then!
Want to respond? Send me an email, post a webmention, or find me elsewhere on the internet.