Category theory
My understanding of this field of mathematics, also known as "the abstraction of all abstractions". Apparently if you try to abstract categories, you just get more categories.
Supposedly category theory is the foundation of many helpful software patterns and how to compose them. I am reading Categories for Programmers to decide for myself whether this is true, and discover whether there are programming super-powers for real life hidden in the abstract land of categories.
Basic stuff:
- At its core, a category is a class (collection, more generic than a set) of objects, and a class of morphisms between these objects. The morphisms are closed under composition: if
(the class of objects in category ), and (the class of morphisms between objects in category ) such that and , then must also be in . Don't forget, you need identity functions for all objects in the morphisms class too. - However, beyond that, you're free to choose the class of objects and morphisms between them. You can limit your study to numerical functions by choosing the object class to be
, then add the necessary functions , and that in and of itself is a category! But to make it more interesting, you can add (and then the closure under composition with other operators, of course, which I will not repeat again when we extend a category). But what if we add the curried version of , meaning we add , which means we are adding to our objects class (along with a new identity function, which I will not repeat again)? - Categories are not necessarily closed under product or sum, but you can extend a category to include
or for any two objects. - Because of the correspondence between functions on products
and their curried equivalents , I will use curried notation here out because it makes it easier to think of collections of functions. - We will often reduce categories in question to their simplest example, like a single object (
) category and three (non-identity) functions (which as you'll see later, is a categorical monoid).
Examples of categories:
- Simple Haskell types (including generics, products and sums, ie
) and functions between them form a category. I'll bet that even if you add higher-order types (ie Maybe a,List a) it still forms a category, but I'm not sure yet.
Concepts within categories
- Two objects
are isomorphic if there exist such that and . We say that form an isomorphism between . - The initial object is the object which has exactly one morphism going to every object in the category. It does not necessarily exist (ie integers as elements with
relation as morphisms). If it exists, it is unique up to isomorphism between initial objects. Initial objects are isomorphic to each other (proving this is a fun warm up), and also, their isomorphism is unique (which often ends up being important for constructions). - The terminal object is the object which has exactly one morphism going from every object in the category. It is the dual of the initial object (ie if a category has an initial object, then when constructing the dual category (aka reversing the arrows), the initial object becomes the terminal object).
Constructing categories from categories
- Dual categories. The "dual category" or "opposite category"
of a category has all the arrows reversed; the morphisms of the dual are constructed by swapping the domains and codomains of every morphism in the category. For morphisms defined by composition in (ie to fulfil closure of morphisms under composition), say , this corresponds to constructing in . - Dual categories are important because for every theorem you prove about a category, you get another theorem for free about its dual. Hence defining concepts like functor and monad also gives us concepts of cofunctor and comonad.
- Product of objects. Given two objects
, the product is an object (perhaps written or ) with two projections (morphisms) and such that for any other , there is a unique morphism such that "factorises" the projections , meaning that and . does not always exist, but when it does, it's unique up to unique isomorphism. - The "product factoriser" is a function
such that factorise p q = \x -> (p x, q x).
- The "product factoriser" is a function
- Coproduct (disjoint sum) of objects. Given objects
the coproduct is an object (perhaps written ) with two injections and such that for any other , there is a unique morphism such that and . - We can define the "coproduct factoriser" as
where factorise i j (Left a) = i aandfactorise i j (Right b) = j b
- We can define the "coproduct factoriser" as
Further concepts with categories
- Free categories are constructions of categories from things that conceptually make sense as categories, but need a bit of filling in. For example, making a directed graph into a category by constructing an object class of the nodes, and a morphism class of the edges, but you'll probably need to add to the morphisms until you have all identity functions (edges from nodes to themselves) and transitive edges (if
are both in the edges then must be in the morphisms). - Thin categories have at most one morphism from any object
to any object ( included). E.g. a preorder, which is a set with relation where for some (but not necessarily all) elements, we have , where you may have cycles ( , but not necessarily ). - A partial order is where
. That is, you can't have cycles between different elements anymore. Partial orders can be sorted with topological sort (because, of course, you can't sort with a cycle). A partial order with a set is called a "poset". - A total order is a set
and a relation where every pair of elements is related; that is, for all pairs .
- A partial order is where
- Monoids make sense as sets and categories.
- Set (aka usual) monoid. A set
with an identity element and a binary operation , where is associative and for all . - Categorical monoid. A category with a single object
and non-identity (example) morphisms . The morphisms need not be all the possible mappings from S to itself, just an arbitrary handful. We can extract a set monoid from a categorical one; the morphisms are the elements of the set, the binary operator is composition, and associativity of the binary operator follows from composition associativity in the category. Likewise, we can construct this category from a set monoid, so we can think of them as the same.
- Set (aka usual) monoid. A set