
hello all, why is it not possible to use guards in do-expressions like do (a, b) | a == b <- getPair return "a and b are equal" Cheers, Misha

Hi, Am Dienstag, den 24.10.2006, 00:44 +0300 schrieb Misha Aizatulin:
hello all,
why is it not possible to use guards in do-expressions like
do (a, b) | a == b <- getPair return "a and b are equal"
Probably because it is not well-defined for all Monad what a failure is, i.e. what to do in the other case. or something. Just my guess. Greetings, Joachim -- Joachim "nomeata" Breitner mail: mail@joachim-breitner.de | ICQ# 74513189 | GPG-Key: 4743206C JID: joachimbreitner@amessage.de | http://www.joachim-breitner.de/ Debian Developer: nomeata@debian.org

On 24/10/06, Joachim Breitner
Am Dienstag, den 24.10.2006, 00:44 +0300 schrieb Misha Aizatulin:
hello all,
why is it not possible to use guards in do-expressions like
do (a, b) | a == b <- getPair return "a and b are equal"
Probably because it is not well-defined for all Monad what a failure is, i.e. what to do in the other case. or something. Just my guess.
Exactly the same thing that guards do elsewhere? Count the falsity of the guard as a pattern match failure. It's precisely the same as doing, e.g.: do Just (a, b) <- Nothing return "a and b were Just" Pattern-match failure in a do-block invokes fail in that monad. -- -David House, dmhouse@gmail.com

Joachim Breitner wrote:
Hi,
Am Dienstag, den 24.10.2006, 00:44 +0300 schrieb Misha Aizatulin:
hello all,
why is it not possible to use guards in do-expressions like
do (a, b) | a == b <- getPair return "a and b are equal"
Probably because it is not well-defined for all Monad what a failure is, i.e. what to do in the other case. or something. Just my guess.
No, fail is indeed a method of class Monad, and it is there exactly for this reason, i.e. because pattern matching may fail (even without guards, think of do Just a <- ... ) The restriction is there because guards are not allowed in lambda expressions, for which do-notation is merely syntactic sugar. (Some people have argued for lifting this restriction in Haskell', see http://thread.gmane.org/gmane.comp.lang.haskell.prime/1750/focus=1750) HTH Ben

Hi, Am Dienstag, den 24.10.2006, 12:48 +0200 schrieb Benjamin Franksen:
Am Dienstag, den 24.10.2006, 00:44 +0300 schrieb Misha Aizatulin:
hello all,
why is it not possible to use guards in do-expressions like
do (a, b) | a == b <- getPair return "a and b are equal"
Probably because it is not well-defined for all Monad what a failure is, i.e. what to do in the other case. or something. Just my guess.
No, fail is indeed a method of class Monad, and it is there exactly for this reason, i.e. because pattern matching may fail (even without guards, think of
do Just a <- ...
) The restriction is there because guards are not allowed in lambda expressions, for which do-notation is merely syntactic sugar. (Some people have argued for lifting this restriction in Haskell', see http://thread.gmane.org/gmane.comp.lang.haskell.prime/1750/focus=1750)
Then why is the “guard” function, which can be used in a way to implement what Misha wants, only available in MonadPlus, and not in Monad? Greetings, Joachim -- Joachim Breitner e-Mail: mail@joachim-breitner.de Homepage: http://www.joachim-breitner.de ICQ#: 74513189

Joachim Breitner wrote:
Am Dienstag, den 24.10.2006, 12:48 +0200 schrieb Benjamin Franksen:
Am Dienstag, den 24.10.2006, 00:44 +0300 schrieb Misha Aizatulin:
hello all,
why is it not possible to use guards in do-expressions like
do (a, b) | a == b <- getPair return "a and b are equal"
Probably because it is not well-defined for all Monad what a failure is, i.e. what to do in the other case. or something. Just my guess.
No, fail is indeed a method of class Monad, and it is there exactly for this reason, i.e. because pattern matching may fail (even without guards, think of
do Just a <- ...
) The restriction is there because guards are not allowed in lambda expressions, for which do-notation is merely syntactic sugar. (Some people have argued for lifting this restriction in Haskell', see http://thread.gmane.org/gmane.comp.lang.haskell.prime/1750/focus=1750)
Then why is the ?guard? function, which can be used in a way to implement what Misha wants, only available in MonadPlus, and not in Monad?
This seems to be inconsistent. Anyway, the decision to include fail in class Monad (instead of using MonadZero) has been criticized by far more competent people than me, see this thread http://thread.gmane.org/gmane.comp.lang.haskell.cafe/15656/focus=15666 Ben
participants (4)
-
Benjamin Franksen
-
David House
-
Joachim Breitner
-
Misha Aizatulin