Assignment 2

Problem 1

In class we discussed Functors and Functor instances for a number of type constructors: [], Maybe, Either a, ID, Const a. For each of these show that the defined fmap function obeys the functor laws:

  1. fmap id x = x
  2. fmap (f.g) x = fmap f $ fmap g x
Recall that fmap for ID is defined as
fmap f (ID x) = ID (f x)
and fmap for Const is
fmap f (Const x) = Const x
It will really help, especially for ID and Const, if you take particular care to distinguish in your thinking the type contructors (Const t1 t2) and (ID t)) from the value constructors (Const x) and (ID x).

Problem 2

Define Functor instances for the constructors (,) a and (->) r: that is define the fmap functions for these constructors; make sure that your fmap functions obey the functor laws. Recall that (,) a is the partially applied type constructors for tuples -- it means tuples whose first element is type a. Similarly (->) r is the partially applied function-type constructor and represents functions whose domain is r. Note: I ran into difficulty experimenting with these because (,) a and (->) r are both already Functor instances and having more than one instance declaration is not allowed. So in order to experiment I did the following:
class MyFunctor f where
   myfmap :: (a -> b) -> f a -> f b
and then I could make new instance declarations like
instance MyFunctor Maybe where
   myfmap f Nothing = Nothing
   myfmap f (Just x) = Just (f x)
and so forth

You are encouraged to work in groups of 2 or 3: make sure that everyone in the group understands each answer.

Assigned 1/28/15

Turn in pdf file of your answers on the course turnin page by 11:59:59 PM on Feb. 2.