I'm reading the second edition of Programming in Haskell and I've came across this sentence:
... there is only one way to make any given parameterised type into a functor, and hence any function with the same polymorphic type as
fmap
must be equal tofmap
.
This doesn't seem right to me, though. I can see that there is only one valid definition of fmap
for each Functor
type, but surely I could define any number of functions with the type (a -> b) -> f a -> f b
which aren't equivalent to each other?
Why is this the case? Or, is it just a mistake by the author?
You've misread what the author was saying.
...any function with the same polymorphic type as
fmap
...
This means, any function with the signature
Functor f => (a -> b) -> f a -> f b
must be equivalant to fmap
. (Unless you permit bottom values, of course.)
That statement is true; it can be seen quite easily if you try to define such a function: because you know nothing about f
except that it's a functor, the only way to obtain a non-⊥ f b
value is by fmapping over the f a
one.
What's a bit less clear cut is the logical implication in the quote:
there is only one way to make any given parameterised type into a functor, and hence any function with the same polymorphic type as fmap must be equal to fmap.
I think what the author means there is, because a Functor f => (a -> b) -> f a -> f b
function must necessarily invoke fmap
, and because fmap
is always the only valid functor-mapping for a parameterised type, any Functor f => (a -> b) -> f a -> f b
will indeed also in practice obey the functor laws, i.e. it will be the fmap
.
I agree that the “hence” is a bit badly phrased, but in principle the quote is correct.