Why do we have map, fmap and liftM?

ListHaskellMonadsRedundancyFunctor

List Problem Overview


map :: (a -> b) -> [a] -> [b]

fmap :: Functor f => (a -> b) -> f a -> f b

liftM :: Monad m => (a -> b) -> m a -> m b

Why do we have three different functions that do essentially the same thing?

List Solutions


Solution 1 - List

map exists to simplify operations on lists and for historical reasons (see https://stackoverflow.com/questions/6824255/whats-the-point-of-map-in-haskell).

>You might ask why we need a separate map function. Why not just do away with the current list-only map function, and rename fmap to map instead? Well, that’s a good question. The usual argument is that someone just learning Haskell, when using map incorrectly, would much rather see an error about lists than about Functors.

-- Typeclassopedia, page 20

fmap and liftM exist because monads were not automatically functors in Haskell:

>The fact that we have both fmap and liftM is an unfortunate consequence of the fact that the Monad type class does not require a Functor instance, even though mathematically speaking, every monad is a functor. However, fmap and liftM are essentially interchangeable, since it is a bug (in a social rather than technical sense) for any type to be an instance of Monad without also being an instance of Functor.

-- Typeclassopedia, page 33

Edit: agustuss's history of map and fmap: >That's not actually how it happens. What happened was that the type of map was generalized to cover Functor in Haskell 1.3. I.e., in Haskell 1.3 fmap was called map. This change was then reverted in Haskell 1.4 and fmap was introduced. The reason for this change was pedagogical; when teaching Haskell to beginners the very general type of map made error messages more difficult to understand. In my opinion this wasn't the right way to solve the problem.

-- https://stackoverflow.com/questions/6824255/whats-the-point-of-map-in-haskell/6827776#6827776

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfredoverflowView Question on Stackoverflow
Solution 1 - Listli.davidmView Answer on Stackoverflow