Search code examples
haskellfunctor

Does the First Functor Law follow from the Second?


According to this question the 2nd Functor law is implied by the 1st in Haskell:

1st Law: fmap id = id
2nd Law : fmap (g . h) = (fmap g) . (fmap h)

Is the reverse true? Starting from 2nd law, and setting g equal to id, can I reason the following and get the 1st law?

fmap (id . h) x = (fmap id) . (fmap h) x
fmap h x = (fmap id) . (fmap h) x
x' = (fmap id) x'
fmap id = id

where x' = fmap h x


Solution

  • No

    data Break a = Yes | No
    
    instance Functor Break where
       fmap f _ = No
    

    clearly the second law holds

       fmap (f . g) = const No = const No . fmap g = fmap f . fmap g
    

    but, the first law does not. The problem with your argument is not all x' are of the form fmap f x