[Haskell-cafe] fmap vs. liftM

Jonathan Cast jonathanccast at fastmail.fm
Mon Feb 4 21:57:48 EST 2008


On 4 Feb 2008, at 6:22 AM, Felipe Lessa wrote:

> Hi there,
>
> Reading http://www.haskell.org/haskellwiki/Things_to_avoid I found an
> interesting saying:
>
> "By the way, in the case of IO monad the Functor class method fmap and
> the Monad based function liftM are the same."
>
> I always tought that
>
> prop :: (Functor m, Monad m, Eq (m b)) => (a -> b) -> m a -> Bool
> prop f x = fmap f x == liftM f x

Indeed, this is an equation from the equivalence of Haskell and  
category-theoretic monads.  Furthermore, the same thing is explicitly  
asserted by the Haskell 98 Report: [1]

Instances of both Monad and Functor should additionally satisfy the law:

fmap f xs = xs >>= return . f

> was True regardless of 'm'. Is there any exception?

There is only one case I'm aware of: if the author of the type forgot  
to define a functor instance for his monad (I've been guilty of that  
before, at least).

jcc

(I fixed the wiki, btw.)

[1] http://haskell.org/onlinereport/basic.html#sect6.3.6




More information about the Haskell-Cafe mailing list