Category Theory
Total Page:16
File Type:pdf, Size:1020Kb
CMPT 481/731 Functional Programming Fall 2008 Category Theory Category theory is a generalization of set theory. By having only a limited number of carefully chosen requirements in the definition of a category, we are able to produce a general algebraic structure with a large number of useful properties, yet permit the theory to apply to many different specific mathematical systems. Using concepts from category theory in the design of a programming language leads to some powerful and general programming constructs. While it is possible to use the resulting constructs without knowing anything about category theory, understanding the underlying theory helps. Category Theory F. Warren Burton 1 CMPT 481/731 Functional Programming Fall 2008 Definition of a Category A category is: 1. a collection of ; 2. a collection of ; 3. operations assigning to each arrow (a) an object called the domain of , and (b) an object called the codomain of often expressed by ; 4. an associative composition operator assigning to each pair of arrows, and , such that ,a composite arrow ; and 5. for each object , an identity arrow, satisfying the law that for any arrow , . Category Theory F. Warren Burton 2 CMPT 481/731 Functional Programming Fall 2008 In diagrams, we will usually express and (that is ) by The associative requirement for the composition operators means that when and are both defined, then . This allow us to think of arrows defined by paths throught diagrams. Category Theory F. Warren Burton 3 CMPT 481/731 Functional Programming Fall 2008 I will sometimes write for flip , since is less confusing than Category Theory F. Warren Burton 4 CMPT 481/731 Functional Programming Fall 2008 Examples The category that we will use the most has all possible Haskell ground types (i.e. not polymorphic types) as objects and all possible non-polymorphic Haskell functions, except those that make use of seq in some way (e.g. $!), as arrows. (A polymorphic datatype corresponds to an infinite collection of non-polymorphic datatypes, and similarly, a polymorphic function corresponds to the infinite collection of the possible non-polymorphic instances of the functions.) We will call this category Hask . Let us complete the obvious details of the definition of and confirm that satisfies the definition of a category. We need to exclude seq and related functions defined in terms of seq, or make some other change, for to be a category. Will will consider this later when we get to domain theory. Category Theory F. Warren Burton 5 CMPT 481/731 Functional Programming Fall 2008 Set ( Set ): Category theory grew out of a generalization of set theory. Sets and functions between them may be viewed as a category. Objects are sets and arrows are total functions. Usually subsets are regarded as distinct sets. For example, the set of integers is not related to the set of reals. Each function is associated with a particular domain and codomain. Discrete Categories: A discrete category is one in which the only arrows are the identity arrows. A single set can be viewed as a discrete category, where each element of the set is an object, although this is not usually particularly useful. The category, , is the one object discrete category, which is sometimes useful. (Note: Later we will use to denote a terminal (or final) object in a category. ) Category Theory F. Warren Burton 6 CMPT 481/731 Functional Programming Fall 2008 Power Sets: A power set of a set is the set of all subsets of the set . For any set, we have a category where the objects of the category are the elements of the power set. There is at most one arrow between any two objects. There is an arrow from to iff . Any Partial Order: The power set of a set is a partial order. In a similar manner, any partial order can be view as a category. All Partial Orders( Poset ): There is also a category where the objects are partial orders and the arrows are monotonic functions between these partial orders. Category Theory F. Warren Burton 7 CMPT 481/731 Functional Programming Fall 2008 Predicate Calculus: We can view the predicate calculus as a category, where predicates are objects and there is an arrow from to if and only if implies . Note that implies itself, so we have identity arrows, and logical implication is transitive, so composition is well defined. There is never more than one arrow between any two predicates. Category Theory F. Warren Burton 8 CMPT 481/731 Functional Programming Fall 2008 Any Group: Any group can be viewed as a category with a single object. The arrows correspond to the elements of the group. The arrow composition operator corresponds to the binary group operation. Certain other algebraic structures can be viewed as categories in a similar manner. All Groups ( Grp ): There is also a category where each group is an object and arrows are homomorphisms between groups. Category Theory F. Warren Burton 9 CMPT 481/731 Functional Programming Fall 2008 A homomorphism is a structure preserving mapping from one set to another. For most algebraic structures, you can build categories where the arrows are homomorphisms. For example, a homomorphism from the group of under , with identity element , to the under , with identity element , must satisfy the following equations. The exponential function (with any base) satisfies this condition, as does the constant function . Category Theory F. Warren Burton 10 CMPT 481/731 Functional Programming Fall 2008 Graphs and graph homomorphism form another category of this nature. Functors (see the following page) are homomorphisms between categories. Category Theory F. Warren Burton 11 CMPT 481/731 Functional Programming Fall 2008 Functors A functor from category to is a pair of functions, 1. - - 2. - - such that 1. If is an arrow in and then , 2. , and 3. whenever is defined. Category Theory F. Warren Burton 12 CMPT 481/731 Functional Programming Fall 2008 We write to express that is a functor from to . Usually we will write for itself, and also for both and . Category Theory F. Warren Burton 13 CMPT 481/731 Functional Programming Fall 2008 Functors, as defined above, are sometimes called covariant functors . Once in a while, we may want a functor that reverses the direction of the arrows. That is, we would like to change If is an arrow in and then , to If is an arrow in and then , Of course, this also means that whenever is defined must be changed to whenever is defined. Functors that reverse the direction of the arrows are called contravariant functors . Category Theory F. Warren Burton 14 CMPT 481/731 Functional Programming Fall 2008 Examples : In , objects are Haskell types, so the object part of a functor, , is a function from types to types. Therefore, a type constructor is suitable for use as the object function of a functor. For example is suitable. In this case = map completes the definition of a functor. Category Theory F. Warren Burton 15 CMPT 481/731 Functional Programming Fall 2008 We can think of this functor as lifting up to a category of list types, which is a subcategory of . map f [a] [b] f a b Category Theory F. Warren Burton 16 CMPT 481/731 Functional Programming Fall 2008 This idea of lifting a type with a type constructor is common in Haskell . Unfortunately, with this sort of functor in Haskell , we can’t use the same name for both the function on objects and the function on arrows, as is usually done in category theory. Instead we have a functor class class Functor f where fmap :: (a -> b) -> f a -> f b This allows us to associate an fmap function with a type constructor and use fmap as a generic name for the function on arrows for a type constructor viewed as the object function part of a functor. Notice that while we have previously had classes for types (e.g. Eq, Show), Functor is a constructor class whose members are type constructors rather than types. Category Theory F. Warren Burton 17 CMPT 481/731 Functional Programming Fall 2008 The Haskell Standard Prelude declares the list type to be a functor by instance Functor [] where fmap = map so that fmap can be used instead of map in contexts where is it clear that we are working with lists. Category Theory F. Warren Burton 18 CMPT 481/731 Functional Programming Fall 2008 Two other functor instances are defined in the Haskell Standard Prelude. data Maybe a = Nothing | Just a deriving (Eq, Ord, Read, Show) instance Functor Maybe where fmap f Nothing = Nothing fmap f (Just x) = Just (f x) instance Functor IO where fmap f x = x >>= (return . f) We won’t worry about the IO type until later in the course. Category Theory F. Warren Burton 19 CMPT 481/731 Functional Programming Fall 2008 Quiz: 1. Find a functor . 2. Prove that Maybe together with its fmap instance obeys the functor laws. 3. Show that for any category there exists an identity functor for that category. 4. Show that the composition of two compatible functors is a functor. Category Theory F. Warren Burton 20 CMPT 481/731 Functional Programming Fall 2008 The Category of (small) Categories: A small category is a category where the collection of objects is a set and the collection of arrows is a set. The category of all small categories ( Cat ): The category has all small categories as objects and all functors between these as arrows. Of course, is not a small category and therefore not an object within itself, thereby keeping Bertrand Russell happy. Category Theory F. Warren Burton 21 CMPT 481/731 Functional Programming Fall 2008 Duality Given any category , it is possible to produce another category, , that is identical to except that all arrows are reversed.