[Haskell-cafe] About mplus

Jules Bean jules at jellybean.co.uk
Wed Sep 5 03:58:51 EDT 2007


David Benbennick wrote:
> You mean (++) = mplus.  I've wondered that too.  Similarly, one should
> define map = fmap.  And a lot of standard list functions can be
> generalized to MonadPlus, for example you can define
> 
> filter :: (MonadPlus m) => (a -> Bool) -> m a -> m a

Somehow this filter fails my intuition. Thanks to glguy on #haskell for 
showing me that you can define it as

filter p m = m >>= \x -> if p x then return x else mzero

I want filter to commute with mplus:

(filter p m) `mplus` (filter p l) === filter p (m `plus` l)

This is true for lists, and seems to me a natural requirement for filter 
to be considered, well, "a filter", along with the related filter p 
mzero == mzero. Unfortunately many of the MonadPlus instances we have 
don't satisfy that.

Jules



More information about the Haskell-Cafe mailing list