map and fmap

Aaron Denney wnoise at ofb.net
Sun Aug 20 16:36:34 EDT 2006


On 2006-08-20, John Hughes <rjmh at cs.chalmers.se> wrote:
>
> From: "Jon Fairbairn" <jon.fairbairn at cl.cam.ac.uk>
>
>> To
>> reinforce what Aaron said, if a programme works now, it'll
>> still work if map suddenly means fmap.
>
> Well, this isn't quite true, is it? Here's an example:
>
> class Foldable f where
>   fold :: (a -> a -> a) -> a -> f a -> a
>
> instance Foldable [] where
>   fold = foldr
>
> example = fold (+) 0 (map (+1) (return 2))
>
> example has the value 3 (of course), but if you replace map by fmap then the 
> code no longer compiles.

Solely due to the compiler no longer seeing that list is the only
intermediate type allowed.  But you have to admit this code is a bit
forced.  People won't be combining things quite this way, and will be
passing in values rather than bare returns.

> In any case, I'm dubious about this as a criterion. I would guess that the 
> majority if compiler runs for beginners (and perhaps for the rest of us 
> too!) end in a type error, not a successful compilation, so arguably the 
> quality of error messages when a type-check fails is more important than 
> which programs compile.

Right, like I said, we need to work on better error messages.

-- 
Aaron Denney
-><-



More information about the Haskell-prime mailing list