Exercise 5 of the Haskell Typeclassopedia Section 3.2 asks for a proof or counterexample on the statement
The composition of two Functors is also a Functor.
I thought at first that this was talking about composing the fmap
methods defined by two separate instances of a Functor
, but that doesn't really make sense, since the types wouldn't match up as far as I can tell. For two types f
and f'
, the types of fmap
would be fmap :: (a -> b) -> f a -> f b
and fmap :: (a -> b) -> f' a -> f' b
, and that doesn't really seem composable. So what does it mean to compose two Functors
?
A Functor
gives two mappings: one on the type level mapping types to types (this is the x
in instance Functor x where
), and one on the computation level mapping functions to functions (this is the x
in fmap = x
). You are thinking about composing the computation-level mapping, but should be thinking about composing the type-level mapping; e.g., given
newtype Compose f g x = Compose (f (g x))
can you write
instance (Functor f, Functor g) => Functor (Compose f g)
? If not, why not?