[Haskell-cafe] Why were unfailable patterns removed and "fail" added to Monad?
michael at snoyman.com
Fri Jan 20 07:40:50 CET 2012
On Jan 20, 2012 8:31 AM, "John Meacham" <john at repetae.net> wrote:
> > 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
> 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.
I mention later that this is a "feature, not a bug" to some people, but I'm
not one of them. The convenience of having this feature is IMO far
outweighed by the cost of the runtime errors it can produce if you use the
pattern matching in the wrong monad (e.g., IO, Reader, Writer...).
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe