[Haskell-cafe] Why were unfailable patterns removed and "fail" added to Monad?

John Meacham john at repetae.net
Fri Jan 20 07:31:16 CET 2012


> As expected, no warnings. But if I change this "unfailable" code above
> to the following failable version:
>
>    data MyType = Foo | Bar
>
>    test myType = do
>        Foo <- myType
>        return ()
>
> I *still* get no warnings! We didn't make sure the compiler spits out
> warnings. Instead, we guaranteed that it *never* will.

This is actually the right useful behavior. using things like

do
   Just x <- xs
   Just y <- ys
   return (x,y)

will do the right thing, failing if xs or ysresults in Nothing. for
instance, in the list monad, it will create the cross product of the
non Nothing members of the two lists. a parse monad may backtrack and
try another route, the IO monad will create a useful (and
deterministic/catchable) exception pointing to the exact file and line
number of the pattern match. The do notation is the only place in
haskell that allows us to hook into the pattern matching mechanism of
the language in a general way.

    John



More information about the Haskell-Cafe mailing list