2
$\begingroup$

The Haskell List: Type -> Type constructor implements the Functor typeclass with function fmap f = map f. This functor that applies the morphism f to each element of a list works, but there are other equally valid functors, for example fmap f = map f . rev which first reverses the list before mapping over it. Is there a concept in category theory that expresses the fact that the first choice of functor is in some sense "more natural" for this type constructor?

$\endgroup$
0

1 Answer 1

3
$\begingroup$

Being a functor requires two properties:

  1. fmap id = id
  2. fmap (f . g) = fmap f . fmap g

The definition fmap fails the first condition:

fmap id = map id . reverse = reverse != id

So that's not a functor at all!

And it even fails the second condition:

fmap (f . g) = map (f . g) . reverse
             = map f . map g . reverse
             = map f . fmap g
             = reverse . map f . reverse . fmap g
             = reverse . fmap f . fmap g
             != fmap f . fmap g

Which is quite clear if you think about it:

  • fmap (f . g) reverses the list once and then maps its elements via f . g
  • fmap f . fmap g reverse the list, maps via g, then reverses the list again and then maps via f!
$\endgroup$
1
  • $\begingroup$ Wow, brainfart! Thanks for clearing that up $\endgroup$
    – gardenhead
    Oct 4, 2017 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.