[Haskell-cafe] Re: how do you deal with mplus?

Ryan Ingram ryani.spam at gmail.com
Thu Oct 9 14:32:18 EDT 2008


So, the law under discussion is
(m `mplus` n) >>= f  ==  (m >>= f) `mplus` (n >>= f)

Definitely holds for lists and other backtracking searches, but it
definitely doesn't hold for non-backtracking monads like Maybe.

In particular:

divide n 0 = mzero
divide n x = return (n `div` x)

(Just 0 `mplus` Just 1) >>= divide 100
   = Just 0 >>= divide 100  -- definition of MonadPlus Maybe
   = divide 100 0 -- definition of Monad Maybe
   = Nothing -- definition of divide

whereas

(Just 0 >>= divide 100) `mplus` (Just 1 >>= divide 100)
 = divide 100 0 `mplus` (Just 1 >>= divide 100)
 = Nothing `mplus` (Just 1 >>= divide 100)
 = Just 1 >>= divide 100
 = divide 100 1
 = Just 100

  -- ryan


More information about the Haskell-Cafe mailing list