[Haskell-cafe] IO (Either a Error) question

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Sat May 8 02:38:57 EDT 2010


On May 8, 2010, at 02:16 , Ivan Lazar Miljenovic wrote:
> David Menendez <dave at zednenem.com> 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 at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH


-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20100508/20f322ec/PGP.bin


More information about the Haskell-Cafe mailing list