User:Michiexile/MATH198/Lecture 3
From HaskellWiki
Michiexile (Talk | contribs) |
Michiexile (Talk | contribs) |
||
Line 169: | Line 169: | ||
===Homework=== |
===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. |
+ | # * 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. |
+ | # 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 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. |
+ | # 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. |
− | * Write a functor instance for <hask>data F a = F (Int -> a)</hask> |
+ | # Write a functor instance for <hask>data F a = F (Int -> a)</hask> |
− | * Write a functor instance for <hask>data F a = F ((Int -> a) -> Int)</hask> |
+ | # Write a functor instance for <hask>data F a = F ((Int -> a) -> Int)</hask> |
− | * * We could write a pretty printer, or XML library, using the following data type as the core data type: |
+ | # * We could write a pretty printer, or XML library, using the following data type as the core data type: |
<haskell> |
<haskell> |
||
data ML a = Tag a (ML a) | |
data ML a = Tag a (ML a) | |
Revision as of 21:06, 5 October 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.
These notes cover material dispersed in several places of Awodey. The definition of a functor is on page 8. More on functors and natural transformations comes in sections 7.1-7.2, 7.4-7.5, 7.7-7.10.
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
A homomorphism of monoids is a functor of the corresponding one-object categories . The functor takes the single object to the single object, and acts on morphisms by F(g) = f(g).
A homomorphism of posets is a functor of the corresponding category. We have if , so if then .
If we pick some basis for every vector space, then this gives us a functor F from Vect to the category with objects integers and morphisms matrices by:
- F_{0}(V) = dimV
- F_{1}(f) is the matrix representing f in the matrices chosen.
This example relies on the axiom of choice.
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.
A functor is faithful if the induced mapping
is injective for all .
A functor is full if the induced mapping
is surjective for all .
Note that a full subcategory is a subcategory so that the embedding functor is both full and faithful.
3.1 Preservation and reflection
We say that a functor preserves a property P, if whenever P holds in the category C, it does so for the image of F in D.
Thus, the inclusion functor for the category of finite sets into the category of sets preserves both monomorphisms and epimorphisms. Indeed, these properties, in both categories, correspond to injective and surjective functions, respectively; and a surjective (injective) function of finite sets is still surjective (injective) when considered for sets in general.
As another example, consider the category 2 given by , with the single non-identity arrow named f. All arrows in this category are both monomorphic and epimorphic. We can define a functor through , and f mapping to the set function that takes all elements to the value 3. The resulting constant map F(f) is neither epic nor monic, while the morphism f is both.
However, there are properties that functors do preserve:
Proposition Every functor preserves isomorphisms.
Proof Suppose is an isomorphism with inverse f^{ − 1}. Then F(f) has inverse F(f^{ − 1}). Indeed, F(f)F(f^{ − 1}) = F(ff^{ − 1}) = F(1_{Y}) = 1_{F(Y)} and F(f^{ − 1})F(f) = F(f^{ − 1}f) = F(1_{X}) = 1_{F(X)}. QED
We say that a functor reflects a property P, if whenever F(f) has that property, so does f.
A functor is representative if every object in D is isomorphic to some F(X) for .
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
- Write a functor instance for data F a = F (Int -> a)
- Write a functor instance for data F a = F ((Int -> a) -> Int)
- * We could write a pretty printer, or XML library, using the following data type as the core data type:
data ML a = Tag a (ML a) | Str String | Seq [ML a] | Nil
- With this, we can use a specific string-generating function to generate the tagged marked up text, such as, for instance:
prettyprint (Tag a ml) = "<" ++ show a ++ ">" ++ prettyprint ml ++ "</" ++ show a ++ ">" prettyprint (Str s) = s prettyprint (Seq []) = "" prettyprint (Seq (m:ms)) = prettyprint m ++ "\n" ++ prettyprint (Seq ms) prettyprint Nil = ""
- Write an instance of Functor that allows us to apply changes to the tagging type. Then, using the following tagging types:
data HTMLTag = HTML | BODY | P | I | H1 deriving (Show) data XMLTag = DOCUMENT | HEADING | ABSTRACT | TEXT deriving (Show)
- write a function and use it to generate a html document out of:htmlize :: ML XMLTag -> ML HTMLTag
Tag DOCUMENT Seq [ Tag HEADING String "Nobel prize for chromosome find", Tag ABSTRACT String "STOCKHOLM (Reuters) - Three Americans won the Nobel prize for medicine on Monday for revealing the existence and nature of telomerase, an enzyme which helps prevent the fraying of chromosomes that underlies aging and cancer.", Tag TEXT String "Australian-born Elizabeth Blackburn, British-born Jack Szostak and Carol Greider won the prize of 10 million Swedish crowns ($1.42 million), Sweden's Karolinska Institute said.", Tag TEXT String "'The discoveries ... have added a new dimension to our understanding of the cell, shed light on disease mechanisms, and stimulated the development of potential new therapies,' it said.", Tag TEXT String "The trio's work laid the foundation for studies that have linked telomerase and telomeres -- the small caps on the end of chromosomes -- to cancer and age-related conditions.", Tag TEXT String "Work on the enzyme has become a hot area of drug research, particularly in cancer, as it is thought to play a key role in allowing tumor cells to reproduce out of control.", Tag TEXT String "One example, a so-called therapeutic vaccine that targets telomerase, in trials since last year by drug and biotech firms Merck and Geron, could yield a treatment for patients with tumors including lung and prostate cancer.", Tag TEXT String "The Chief Executive of Britain's Medical Research Council said the discovery of telomerase had spawned research of 'huge importance' to the world of science and medicine.", Tag TEXT String "'Their research on chromosomes helped lay the foundations of future work on cancer, stem cells and even human aging, areas that continue to be of huge importance,' Sir Leszek Borysiewicz said in a statement.",