
Assuming A, B, C are monadic operation. How do you read the following function: do A 'mplus' B C I expect this to translate to: (A 'mplus' B) >>= C Can I then say it is equivalent to: (A >>=C) mplus (B >>=C) Thanks, Daryoush

Daryoush Mehrtash wrote:
Assuming A, B, C are monadic operation. How do you read the following function:
do A 'mplus' B C
I expect this to translate to:
(A 'mplus' B) >>= C
It translates to (A `mplus` B) >> C
Can I then say it is equivalent to:
(A >>=C) mplus (B >>=C)
Sounds like a desirable law, but it doesn't always hold. See also http://www.haskell.org/haskellwiki/MonadPlus Currently, there's no consensus concerning the laws that MonadPlus should obey. Regards, apfelmus

On Thu, 2008-10-09 at 20:20 +0200, apfelmus wrote:
Daryoush Mehrtash wrote:
Assuming A, B, C are monadic operation. How do you read the following function:
do A 'mplus' B C
I expect this to translate to:
(A 'mplus' B) >>= C
It translates to
(A `mplus` B) >> C
Can I then say it is equivalent to:
(A >>=C) mplus (B >>=C)
Sounds like a desirable law, but it doesn't always hold.
It holds for a couple of interesting monads, though. In particular, it holds for [] and for back-tracking or parallel parsers.
See also
http://www.haskell.org/haskellwiki/MonadPlus
Currently, there's no consensus concerning the laws that MonadPlus should obey.
jcc

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
participants (4)
-
apfelmus
-
Daryoush Mehrtash
-
Jonathan Cast
-
Ryan Ingram