Relating categories with functors
September 14, 2020 • category theory
Tl;dr — We introduce functors, which allow us to relate different categories. As an applied example, we think of database schemas as categories and database instances as functors.
We often want to relate one category to another. Perhaps we see that one category is embedded inside of another one, or that we can simplify our view of a category by mapping it onto a less complex one, or that one category is in some sense the same as another one. Functors formalize these ideas. They’re like the mathematical version of analogies.
A functor from a category to a category , written as , associates an object in for every object in and a morphism in for every morphism in
Furthermore, must respect the structure that defines a category, namely identities and composition. This means that
for every in and that for any three objects and morphisms in ,
in .
In plain words, these equations say that maps identity morphisms to identity morphisms and that a composition of two morphisms is mapped to the composition of the mappings of the two morphisms.
Examples of functors
Diagrams
Our pictures of categories, now referred to as diagrams, are in fact functors! Formally, a diagram of shape in is a functor .
This definition really confused me when I first encountered it, until I realized that what we draw is the image of the functor. This is a lot like when we plot a function, say, the parabola , we often say that the graph is the function. Here’s a picture that illustrates what I mean:
In this picture is a category with two objects and with two morphisms and that go from to . I’ve also drawn a few sets in the category 1 and a few arrows representing functions between them. We want a diagram of this shape relating to . sends to and to also selects two functions from to to map and to: to and to .
To the right of the dotted line, I’ve drawn the image of the diagram , which is what we would normally draw for .
Isomorphic categories and a category of categories
Each category has an identity functor which, as you might guess, takes each object to itself and each morphism to itself.
Our notation for identity functors and general functors between categories is really similar to the notation used for morphisms within a category. This is no coincidence.
We can define a category of categories by taking the objects to be categories and the morphisms to be functors between categories.
Composition of two functors and is given by the functor defined by sending objects of to and morphisms of to
Following our discussion of isomorphisms in my last post, two categories and are isomorphic if there are functors and such that
Isomorphic categories have exactly the same structure.
Embedding one category within another
Going from a smaller category to a larger category (in terms of objects), we can think of a functor as embedding in if no information is lost, i.e., no objects or morphisms in are collapsed together in .
Collapsing categories
Example from Bool to Set
Recall is the two-element preorder given by false true. We can define a functor by where is the set of all real numbers in between and including 0 and 1. sends to defined by for all in
Constant functors
An extreme case is a constant functor, which maps all objects to a single object in the target category and all morphisms to the identity morphism .
Nonexamples of functors
In the picture below, on the left we have a preorder that consists of three objects where both and are (the corresponding arrows are labeled and ). On the right is a preorder with three objects where (the corresponding arrows are labeled and ).
I’ve also drawn a mapping of objects and morphisms of to which is not a functor. The mapping respects the direction of , but it reverses the direction of , in the sense that has source , which maps to , and target , which maps to , but is mapped to which has source and target .
As we just saw, if a mapping of categories from to reverses morphisms, then the mapping can’t be a functor… unless it reverses all the morphisms, in which case it could be a functor from the opposite category to (as long as it preserves identities and composition of course). Such a functor is known as a contravariant functor. A regular functor is referred to as a covariant functor.
An application: database schemas and instances
A database schema is essentially a finitely-presented category with path equations.
By finitely-presented category, we mean that the category is obtained by interpreting a finite graph as a category (taking vertices as objects and arrows as morphisms), and any relationships between morphisms (often thought of as constraints or business rules) are given by path equations.
Here’s an example schema for a university that is pretty self-explanatory:
We have a constraint Dept.chair.worksIn = Dept, which says that the chair of a department works in that department. This constraint leads to other constraints, like Professor.worksIn.chair.worksIn.chair = Professor.worksIn.chair, which amounts to saying that a professor’s department chair’s chair is themself.
An instance (in time) of a database schema , i.e., actual data, is a functor from to Set. One way to think about this is that the schema is the abstract structure, and the functor is a representation of that structure in the form of concrete data.
Here is a schema :
We can give this a concrete representation as a database instance by defining a functor by setting to be the set of all college students in the US, to be the set of all students at a given university , and to be the power set of all courses at .
selects out the all students at the university who got all A’s, maps students at to the courses they’re taking, and maps students at to the courses they got at least a B in.
If we first filter by the students that get an A in all their courses, then those students’ courses are the same as the courses that they at least got a B in. In other words, , but .
Graphs as database instances
We can even view graphs as database instances of the schema given by the category :
has two objects, one labelled and one labelled . There are two morphisms from to labelled and .
As an example, we can view the graph pictured below as a functor , where , where
maps each arrow to its source vertex, and maps each arrow to its target vertex.
As is discussed in Seven Sketches in Compositionality, you can use functors between database schemas to create special functors to migrate data. The benefit is that you can reason with them and prove that a data migration won’t fail.
Do functors form categories?
Functors relate categories to one another, but can we also think of functors forming their own categories? The answer is yes, and categories whose objects can be interpreted as functors are known as functor categories.
But what are the morphisms in such a category? How do we relate one functor to another? This will be the topic of my next category theory post — natural transformations.
References
See chapter 3 of Seven Sketches in Compositionality for much more detail on all of the topics covered here and more.
- Recall that is the category of sets, where sets are the objects and functions between sets are the morphisms.↩