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

Jon Fairbairn jon.fairbairn at
Sun Aug 23 05:10:35 EDT 2009

Trac ticket #3453. Two week time frame.

Add check function to Control.Monad

    check :: (MonadPlus m) => (a -> Bool) -> a -> m a
    check p a
        | p a = return a
            | otherwise = mzero


The example that suggested the function to me is this:

   readMaybe :: Read a => String -> Maybe a
       = join . fmap no_trailing_garbage . listToMaybe . reads
         where no_trailing_garbage = fmap fst . check (all isSpace . snd)

but check is clearly more generally useful than guard. (guard = flip
(check . const) ())

I also note in the comments to check that we can define List.filter
like this

   filter = (concat .) . map . check

Now, concat is just join specialised to lists, and map [is fmap, but...]
is liftM, so we would expect a function

   mfilter = (join .) . liftM . check

to be useful.  Should that also be added?

Jón Fairbairn                                 Jon.Fairbairn at

More information about the Libraries mailing list