[Haskell-cafe] Re: what is inverse of mzero and return?

Ashley Yakeley ashley at semantic.org
Fri Jan 21 21:16:04 EST 2005

In article <Pine.WNT.4.61.0501211845490.1272 at philo>,
 "S. Alexander Jacobson" <alex at alexjacobson.com> wrote:

> I assume there is a standard name for this 
> class/function:
>    instance Foo [] where
>      foo [] = mzero
>      foo (x:_) = return x
>    instance Foo (Maybe x) where
>      foo Nothing = mzero
>      foo Just x = return x

I don't believe so. I had to write my own classes to do this sort of 

This is also a good opporunity to point out an ambiguity in the standard 
MonadPlus class. All instances satisfy these:

  mplus mzero a = a
  mplus a mzero = a

But only some instances (such as []) satisfy this:

  (mplus a b) >>= c = mplus (a >>= c) (b >>= c)

Other instances (IO, Maybe) satisfy this:

  mplus (return a) b = return a

I think mplus should be separated into two functions. This code shows 
the difference a bit more clearly:

    b <- mplus (return True) (return False)
    if b then mzero else return ()

For the first kind this is the same as "return ()", for the second kind 
it's the same as "mzero".

Ashley Yakeley, Seattle WA

More information about the Haskell-Cafe mailing list