User:Michiexile/MATH198/Lecture 3
From HaskellWiki
Michiexile (Talk | contribs) |
Michiexile (Talk | contribs) |
||
Line 77: | Line 77: | ||
</haskell> | </haskell> | ||
− | |||
+ | ===The category of categories=== | ||
+ | We define a category <math>Cat</math> by setting objects to be all small categories, and arrows to be all functors between them. Being graph homomorphisms, functors compose, their composition fulfills all requirements on forming a category. It is sometimes useful to argue about a category <math>CAT</math> of all small and most large categories. The issue here is that allowing <math>CAT\in CAT_0</math> opens up for set-theoretical paradoxes. | ||
− | |||
+ | ====Isomorphisms in Cat and equivalences of categories==== | ||
+ | |||
+ | The definition of an isomorphism holds as is in <math>Cat</math>. However, isomorphisms of categories are too restrictive a concept. | ||
+ | |||
+ | To see this, recall the category <math>Monoid</math>, where each object is a monoid, and each arrow is a monoid homomorphism. We can form a one-object category out of each monoid, and the method to do this is functorial - i.e. does the right thing to arrows to make the whole process a functor. | ||
+ | |||
+ | Specifically, if <math>h:M\to N</math> is a monoid homomorphism, we create a new functor <math>C(h):C(M)\to C(N)</math> by setting <math>C(h)[0](*) = *</math> and <math>C(h)[1](m) = h(m)</math>. This creates a functor from <math>Monoid</math> to <math>Cat</math>. The domain can be further restricted to a full subcategory <math>OOC</math> of <math>Cat</math>, consisting of all the 1-object categories. We can also define a functor <math>U:OOC\to Monoid</math> by <math>U(C) = C[1]</math> with the monoidal structure on <math>U(C)</math> given by the composition in <math>C</math>. For an arrow <math>F:A\to B</math> we define <math>U(F) = F[1]</math>. | ||
+ | |||
+ | These functors take a monoid, builds a one-object category, and hits all of them; and takes a one-object category and builds a monoid. Both functors respect the monoidal structures - yet these are not an isomorphism pair. The clou here is that our construction of <math>C(M)</math> from <math>M</math> requires us to choose something for the one object of the category. And choosing different objects gives us different categories. | ||
+ | |||
+ | Thus, the composition <math>CU</math> is not the identity; there is no guarantee that we will pick the object we started with in the construction in <math>C</math>. Nevertheless, we would be inclined to regard the categories <math>Monoid</math> and <math>OOC</math> as essentially the same. The solution is to introduce a different kind of sameness: Definition A functor <math>F:C\to D</math> is an equivalence of categories if there is a functor <math>G:D\to C</math> and: | ||
+ | * A family <math>u_C:C\to G(F(C))</math> of isomorphisms in <math>C</math> indexed by the objects of <math>C</math>, such that for every arrow <math>f:C\to C': G(F(f)) = u_{C'}\circ f\circ u_C^{-1}</math>. | ||
+ | * A family <math>u_D:D\to F(G(D))</math> of isomorphisms in <math>D</math> indexed by the objects of <math>D</math>, such that for every arrow <math>f:D\to D': F(G(f)) = u_{D'}\circ f\circ u_D^{-1}</math>. | ||
+ | The functor <math>G</math> in the definition is called a pseudo-inverse of <math>F</math>. | ||
+ | |||
+ | |||
+ | ====Natural transformations==== | ||
+ | |||
+ | |||
+ | The families of morphisms required in the definition of an equivalence show up in more places. Suppose we have two functors <math>F:A\to B</math> and <math>G:A\to B</math>. Definition <math>A</math> natural transformation <math>\alpha:F\to G</math> is a family of arrows <math>\alpha a:F(a)\to G(a)</math> indexed by the objects of <math>A</math> such that for any arrow <math>s:a\to b</math> in <math>G(s)\circ\alpha a = \alpha b\circ F(s)</math> (draw diagram) | ||
+ | |||
+ | The commutativity of the corresponding diagram is called the ''naturality condition'' on <math>\alpha</math>, and the arrow <math>\alpha a</math> is called the ''component'' of the natural transformation <math>\alpha</math> at the object <math>a</math>. | ||
+ | |||
+ | Given two natural transformations <math>\alpha: F\to G</math> and <math>\beta: G\to H</math>, we can define a composition <math>\beta\circ\alpha</math> componentwise as <math>(\beta\circ\alpha)(a) = \beta a \circ \alpha a</math>. | ||
+ | |||
+ | '''Proposition''' The composite of two natural transformations is also a natural | ||
+ | transformation. | ||
+ | |||
+ | '''Proposition''' Given two categories <math>C,D</math> the collection of all functors <math>C\to D</math> form a category <math>Func(C,D)</math> with objects functors and morphisms natural transformations between these functors. | ||
+ | |||
+ | |||
+ | ===Properties of functors=== | ||
+ | |||
+ | The process of forming homsets within a category <math>C</math> gives, for any object <math>A</math>, two different functors <math>Hom(A,-)</math>: <math>X\mapsto Hom(A,X)</math> and <math>Hom(-,A): X\mapsto Hom(X,A)</math>. Functoriality for <math>Hom(A,-)</math> is easy: <math>Hom(A,f)</math> is the map that takes some <math>g:A\to X</math> and transforms it into <math>fg:A\to Y</math>. | ||
+ | |||
+ | Functoriality for <math>Hom(-,A)</math> is more involved. We can view this as a functor either from <math>C^op</math>, or as a different kind of functor. If we just work with <math>C^op</math>, then no additional definitions are needed - but we need an intuition for the dual categories. | ||
+ | |||
+ | Alternatively, we introduce a new concept of a contravariant functor. A contravariant functor <math>F:C\to D</math> is some map of categories, just like a functor is, but such that <math>F(1[X]) = 1[F(X)]</math>, as usual, but such that for a <math>f:A\to B</math>, the functor image is some <math>F(f):F(B)\to F(A)</math>, and the composition is <math>F(gf) = F(f)F(g)</math>. The usual kind of functors are named ''covariant''. | ||
+ | |||
+ | * Full | ||
+ | * Faithful | ||
+ | |||
+ | ===Applications for functors=== | ||
+ | * CS applications | ||
+ | |||
+ | * State machines and monoid actions | ||
+ | |||
+ | ===Homework=== | ||
− | + | * * Recall that a category is called ''discrete'' if it has no arrows other than the identities. Show that a small category <math>A</math> is discrete if and only if every set function <math>A_0\to B_0</math>, for every small category <math>B</math>, is the object part of a unique functor <math>A\to B</math>. Analogously, we define a small category <math>B</math> to be indiscrete if for every small category <math>A</math>, every set function <math>A_0\to B_0</math> is the object part of a unique functor <math>A\to B</math>. Characterise indiscrete categories by the objects and arrows they have. | |
+ | * Show that the category of vectorspaces is equivalent to the category with objects integers and arrows matrices. | ||
+ | * Prove the propositions in the section on natural transformations. | ||
+ | * Prove that <hask>listToMaybe :: [a] -> Maybe a</hask> is a natural transformation from the list functor to the maybe functor. Is <hask>catMaybes</hask> a natural transformation? Between which functors? Find three more natural transformations defined in the standard Haskell library. |
Revision as of 22:45, 28 September 2009
IMPORTANT NOTE: THESE NOTES ARE STILL UNDER DEVELOPMENT. PLEASE WAIT UNTIL AFTER THE LECTURE WITH HANDING ANYTHING IN, OR TREATING THE NOTES AS READY TO READ.
Contents |
1 Functors
We've spent quite a bit of time talking about categories, and special entities in them - morphisms and objects, and special kinds of them, and properties we can find.
And one of the main messages visible so far is that as soon as we have an algebraic structure, and homomorphisms, this forms a category. More importantly, many algebraic structures, and algebraic theories, can be captured by studying the structure of the category they form.
So obviously, in order to understand Category Theory, one key will be to understand homomorphisms between categories.
1.1 Homomorphisms of categories
A category is a graph, so a homomorphism of a category should be a homomorphism of a graph that respect the extra structure. Thus, we are led to the definition:
Definition A functor from a category C to a category D is a graph homomorphism F_{0},F_{1} between the underlying graphs such that for every object :
- F_{1}(gf) = F_{1}(g)F_{1}(f)
Note: We shall consistently use F in place of F_{0} and F_{1}. The context should be able to tell you whether you are mapping an object or a morphism at any given moment.
1.1.1 Examples and non-examples
- Monoid homomorphisms
- Monotone functions between posets
- Pick a basis for every vectorspace, send and to the matrix representing that morphism in the chosen bases.
1.2 Interpreting functors in Haskell
One example of particular interest to us is the category Hask. A functor in Hask is something that takes a type, and returns a new type. Not only that, we also require that it takes arrows and return new arrows. So let's pick all this apart for a minute or two.
Taking a type and returning a type means that you are really building a polymorphic type class: you have a family of types parametrized by some type variable. For each typeThe rules we expect a Functor to obey seem obvious: translating from the categorical intuition we arrive at the rules
- andfmap id = id
- fmap (g . f) = fmap g . fmap f
data Boring a = Boring instance Functor Boring where fmap f = const Boring
data List a = Nil | Cons a (List a) instance Functor List where fmap f Nil = Nil fmap f (Cons x lst) = Cons (f x) (fmap f lst) data Maybe a = Nothing | Just a instance Functor Maybe where fmap f Nothing = Nothing fmap f (Just x) = Just (f x) data Either b a = Left b | Right a instance Functor (Either b) where fmap f (Left x) = Left x fmap f (Right y) = Right (f y) data LeafTree a = Leaf a | Node [LeafTree a] instance Functor LeafTree where fmap f (Node subtrees) = Node (map (fmap f) subtrees) fmap f (Leaf x) = Leaf (f x) data NodeTree a = Leaf | Node a [NodeTree a] instance Functor NodeTree where fmap f Leaf = Leaf fmap f (Node x subtrees) = Node (f x) (map (fmap f) subtrees)
2 The category of categories
We define a category Cat by setting objects to be all small categories, and arrows to be all functors between them. Being graph homomorphisms, functors compose, their composition fulfills all requirements on forming a category. It is sometimes useful to argue about a category CAT of all small and most large categories. The issue here is that allowing opens up for set-theoretical paradoxes.
2.1 Isomorphisms in Cat and equivalences of categories
The definition of an isomorphism holds as is in Cat. However, isomorphisms of categories are too restrictive a concept.
To see this, recall the category Monoid, where each object is a monoid, and each arrow is a monoid homomorphism. We can form a one-object category out of each monoid, and the method to do this is functorial - i.e. does the right thing to arrows to make the whole process a functor.
Specifically, if is a monoid homomorphism, we create a new functor by setting C(h)[0]( * ) = * and C(h)[1](m) = h(m). This creates a functor from Monoid to Cat. The domain can be further restricted to a full subcategory OOC of Cat, consisting of all the 1-object categories. We can also define a functor by U(C) = C[1] with the monoidal structure on U(C) given by the composition in C. For an arrow we define U(F) = F[1].
These functors take a monoid, builds a one-object category, and hits all of them; and takes a one-object category and builds a monoid. Both functors respect the monoidal structures - yet these are not an isomorphism pair. The clou here is that our construction of C(M) from M requires us to choose something for the one object of the category. And choosing different objects gives us different categories.
Thus, the composition CU is not the identity; there is no guarantee that we will pick the object we started with in the construction in C. Nevertheless, we would be inclined to regard the categories Monoid and OOC as essentially the same. The solution is to introduce a different kind of sameness: Definition A functor is an equivalence of categories if there is a functor and:
- A family of isomorphisms in C indexed by the objects of C, such that for every arrow .
- A family of isomorphisms in D indexed by the objects of D, such that for every arrow .
The functor G in the definition is called a pseudo-inverse of F.
2.2 Natural transformations
The families of morphisms required in the definition of an equivalence show up in more places. Suppose we have two functors and . Definition A natural transformation is a family of arrows indexed by the objects of A such that for any arrow in (draw diagram)
The commutativity of the corresponding diagram is called the naturality condition on α, and the arrow αa is called the component of the natural transformation α at the object a.
Given two natural transformations and , we can define a composition componentwise as .
Proposition The composite of two natural transformations is also a natural transformation.
Proposition Given two categories C,D the collection of all functors form a category Func(C,D) with objects functors and morphisms natural transformations between these functors.
3 Properties of functors
The process of forming homsets within a category C gives, for any object A, two different functors Hom(A, − ): and . Functoriality for Hom(A, − ) is easy: Hom(A,f) is the map that takes some and transforms it into .
Functoriality for Hom( − ,A) is more involved. We can view this as a functor either from C^{o}p, or as a different kind of functor. If we just work with C^{o}p, then no additional definitions are needed - but we need an intuition for the dual categories.
Alternatively, we introduce a new concept of a contravariant functor. A contravariant functor is some map of categories, just like a functor is, but such that F(1[X]) = 1[F(X)], as usual, but such that for a , the functor image is some , and the composition is F(gf) = F(f)F(g). The usual kind of functors are named covariant.
- Full
- Faithful
4 Applications for functors
- CS applications
- State machines and monoid actions
5 Homework
- * Recall that a category is called discrete if it has no arrows other than the identities. Show that a small category A is discrete if and only if every set function , for every small category B, is the object part of a unique functor . Analogously, we define a small category B to be indiscrete if for every small category A, every set function is the object part of a unique functor . Characterise indiscrete categories by the objects and arrows they have.
- Show that the category of vectorspaces is equivalent to the category with objects integers and arrows matrices.
- Prove the propositions in the section on natural transformations.
- Prove that is a natural transformation from the list functor to the maybe functor. IslistToMaybe :: [a] -> Maybe aa natural transformation? Between which functors? Find three more natural transformations defined in the standard Haskell library.catMaybes