Proposal: add new function "check" to Control.Monad

Sebastian Fischer sebf at
Mon Aug 24 03:59:24 EDT 2009

> Add check function to Control.Monad
>    check :: (MonadPlus m) => (a -> Bool) -> a -> m a
>    check p a
>        | p a = return a
>        | otherwise = mzero

I agree that such a function is occasionally useful. But I doubt your  

> Rationale: [...]
> but check is clearly more generally useful than guard. (guard = flip
> (check . const) ())

This is a bit like saying that concatMap is more generally useful than  
map and concat because:

     map f = concatMap ((:[]).f)
     concat = concatMap id

Although this is correct, map and concat are smaller pieces that can  
be easily combined to concatMap:

     concatMap f = concat . map f

So the question is whether check is useful enough to be included as a  
shortcut for a combination of simpler primitives (as was decided for  

If 'check' is added then I would prefer this definition:

   check f x = guard (f x) >> return x

It emphasises how 'check' is combined from simpler parts.

> so we would expect a function
>   mfilter = (join .) . liftM . check
> to be useful.  Should that also be added?

I would not object and prefer this definition:

     mfilter f m = m >>= check f

It seems simpler.


Underestimating the novelty of the future is a time-honored tradition.

More information about the Libraries mailing list