Pattern guards vs. case (was, unfortunately :Re: interesting example of laziness/ghc optimisation)

Laszlo Nemeth [email protected]
Thu, 1 Mar 2001 19:43:04 +0900 (KST)


* * * Ketil Malde <[email protected]> wrote:

> ut - the converse is not true, is it?  I can write
> 
>     ... = case foo of
>                (Foo f) -> ...
>                (Bar b) -> ...
> 
> ut I can't express that as a pattern-guarded expression, can I?

You probably have already seen John's reply which pretty much says
everything. But if you want to be really weird you can write something
like (I haven't typed this in):

f x | (Foo _) == x = 
f x | (Bar _) == x =

which doesn't buy you anything because it doesn't bind the argument of
Foo to anything. There were a couple of messages a year or so ago
about what can be done with pattern matching and I recall Simon Marlow
posted a few puzzling examples.

> Why is it there at all?  Is there a (rough) guideline for when to
> use one or the other?

Sugar ... but I think in this case is not the 'unhealthy' variety. As
John noted, without it you would have to write nested case statements
which is cumbersome and may lead to suboptimal code i.e. code
duplication and repeated tests (assuming later stages of your compiler
doesn't rearrange the code). In fact, the entire issue of compilation
of pattern matching arises from the desire to select the appropriate
rhs with the minimum number of tests.

Rough guideline? I use guards whenever I can (which means not at all
these days since SML sadly lacks guards (for a good reason!)), because
I find it easy to read it and I hope that a clever pattern matching
compiler takes advantage of the extra information.

--laszlo