Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 640
Panos Avatar asked Nov 24 '12 07:11

Panos


People also ask

What are the functor laws?

Functor Laws If two sequential mapping operations are performed one after the other using two functions, the result should be the same as a single mapping operation with one function that is equivalent to applying the first function to the result of the second.

Is Fmap a functor?

The expression fmap (*2) is a function that takes a functor f over numbers and returns a functor over numbers. That functor can be a list, a Maybe , an Either String, whatever. The expression fmap (replicate 3) will take a functor over any type and return a functor over a list of elements of that type.

Is functor a type class?

Functor in Haskell is a typeclass that provides two methods – fmap and (<$) – for structure-preserving transformations. To implement a Functor instance for a data type, you need to provide a type-specific implementation of fmap – the function we already covered.


1 Answers

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

like image 154
Philip JF Avatar answered Oct 09 '22 18:10

Philip JF