[Haskell] Types of when and unless in Control.Monad

Henning Thielemann lemming at henning-thielemann.de
Tue Jun 5 22:49:26 CEST 2012

On Tue, 5 Jun 2012, Andres Löh wrote:

>> Using mapM instead of mapM_ can lead to a memory leak and I think the
>> confusion between mapM and mapM_ was the initial reason to add the
>> unused-binding warning to GHC. And it actually helped me spotting a lot of
>> calls to mapM that have to be calls to mapM_.
> The different space behaviour is a very good reason to make the
> distinction for mapM and mapM_. However, it's not a good reason to
> choose arbitrarily restrictive types for perfectly generic functions
> in other cases IMHO. At least I don't see that there's a performance
> reason not to generalize when or unless, as their implementation would
> stay the same.

The type of 'when' is not arbitrarily restrictive. It makes sure, that no 
monadic result is ignored by accident.

Btw. the mapM memory leak can be easily and accidentally resurrected by 

   when_ b (mapM f xs)


   f :: a -> m ()
   when_ :: Bool -> m a -> m ()

More information about the Libraries mailing list