solution

Solutions to the exercises of DaoFP, Chapter 8: DaoFP Chapter 8 Exercises. Index: Map of Content.

Solution 8.2.1

Exercise 8.2.1

It picks two objects and an arrow (identities go to identities). Functors are the arrows of .

Sources: DaoFP Exercise 8.2.1.

Solution 8.2.2

Exercise 8.2.2

and likewise , so is an Isomorphism with inverse .

Sources: DaoFP Exercise 8.2.2.

Solution 8.3.1

program — Exercise 8.3.1

instance Functor WithInt where
  fmap f (WithInt a n) = WithInt (f a) n

fmap id = id and fmap (g . f) = fmap g . fmap f hold since n is untouched.

Sources: DaoFP Exercise 8.3.1.

Solution 8.3.2

program — Exercise 8.3.2

newtype Identity a = Identity a
instance Functor Identity where
  fmap f (Identity a) = Identity (f a)

Sources: DaoFP Exercise 8.3.2.

Solution 8.3.3

program — Exercise 8.3.3

data Constant c a = Constant c
instance Functor (Constant c) where
  fmap _ (Constant c) = Constant c

It is the Constant Functor : every arrow goes to the identity on .

Sources: DaoFP Exercise 8.3.3.

Solution 8.3.4

program — Exercise 8.3.4

instance Bifunctor MoreThanA where
  bimap g h (More a mb) = More (g a) (fmap h mb)

Sources: DaoFP Exercise 8.3.4.

Solution 8.5.1

program — Exercise 8.5.1

The composite is contravariant:

instance (Functor g, Contravariant f) => Contravariant (Compose g f) where
  contramap h (Compose gfa) = Compose (fmap (contramap h) gfa)

See Contravariant Functor.

Sources: DaoFP Exercise 8.5.1.