Does the law of the first functor follow from the second?

According to this question , the second law of the functor is implied by the 1st in Haskell:

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

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

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

where x' = fmap hx

+8
source share
2 answers

Not

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

it is clear that the second law holds

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

but in the first law this is not. The problem with your argument is not all x' have the form fmap fx

+11
source

No, it only works in one direction.

Consider this instance of Functor :

 data Foo a = Foo Int a instance Functor Foo where fmap f (Foo _ x) = Foo 5 (fx) 

It satisfies the second law, but not the first.

The last step in your proof is not valid - you have shown that fmap id x' = x' , but this is limited to x' , which are first returned from fmap , not arbitrary values.

+7
source

All Articles