[Haskell-cafe] fmap vs. liftM

Henning Thielemann lemming at henning-thielemann.de
Mon Feb 4 09:29:24 EST 2008


On Mon, 4 Feb 2008, 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
>
> was True regardless of 'm'. Is there any exception? If so, why? I've
> even done s/fmap/liftM/g and s/liftM/fmap/g in the past for
> consistency =).

Problem is that from the idea Functor is a superclass of Monad, with the
property that "fmap == liftM". The first relation could have been
expressed in Haskell 98 but was not done (forgotten?) in the standard
libraries. The second relation can even not be expressed in Haskell 98. So
it's only cosmetic, if you use 'liftM' instead of 'fmap' in order to avoid
an explicit 'Functor' constraint in a function.


More information about the Haskell-Cafe mailing list