
On May 8, 2010, at 02:16 , Ivan Lazar Miljenovic wrote:
David Menendez
writes: That does not invoke fail.
Let's take a simpler example: do { x <- Nothing; stmt }. This translates to
let ok x = do { stmt } ok _ = fail "..." in Nothing >>= ok
By the definition of (>>=) for Maybe, 'ok' is never called.
As I said in another email: does not the "x <- Nothing" itself call fail as it expects x to be an actual value wrapped in Just?
It's not a call, it's a definition as shown above. The simpler translation is:
x <- y
becomes
y >>= \x ->
(note incomplete expression; the next line must complete it) and the refutable pattern match takes place in the lambda binding. But because of the whole "fail" thing, instead of letting pattern match failure be caught by the lambda binding it gets handled specially beforehand, which is especially silly when in most cases fail is defined to do the same thing as the lambda binding would. I'm suggesting (as is David, I think) that a saner definition would let the lambda binding randle refutable patterns, and for something like Maybe (>>=) can decide how to deal with it in the usual way. Otherwise you're either using a default fail that duplicates the lambda binding, or if you want custom handling (as with Maybe and Either that propagate Nothing/Left _ respectively) you end up reimplementing part of (>>=) as fail, which is just dumb. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH