A better type signature for `forM_`

Erik de Castro Lopo mle+hs at mega-nerd.com
Fri Apr 1 05:13:05 UTC 2016

John Wiegley wrote:

> >>>>> Erik de Castro Lopo <mle+hs at mega-nerd.com> writes:
> > As pointed out to me on IRC (thanks pjdeport), chaning the type signature of
> > `forM_` to
> >     forM_' :: (Monad m, Foldable t) => t a -> (a -> m ()) -> m ()
> > would have resulted in an error.
> What about mapM_, replicateM_, etc.? In each case, '_' means "if you return a
> value from your action, it is ignored", and not "you must return unit".

Which is the exact problem. I suspect that most people use `forM_` as "assume
the action returns unit" rather than "assume the return value of the action is

FWIW, ignoring a list of unit is perfectly fine, but ignoring a list of `IO ()`
is almost certainly not what people meant.

Erik de Castro Lopo

More information about the Libraries mailing list