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

John Meacham
Thu, 1 Mar 2001 01:06:36 -0800

On Thu, Mar 01, 2001 at 09:40:48AM +0100, Ketil Malde wrote:
> (Apologies, I forgot to change the subject)
> Laszlo Nemeth <> writes:
> > * * * Ketil Malde <> wrote:
> > There is no difference. The 'pipe-syntax' (or pattern guards) gets
> > desugared (by the pattern matching compiler) to case statements i.e.:
> > runRandom = \ last max (num > 1) of
> >                              True  -> runRandom (fst new) max (num-1)
> >                              False -> snd new
> But - the converse is not true, is it?  I can write
>      ... = case foo of
>                 (Foo f) -> ...
>                 (Bar b) -> ...
> but I can't express that as a pattern-guarded expression, can I?  My
> impression here is that the PG syntax adds nothing, and is hardly any
> more readable, and less intuitive for migrators from more traditional
> language.  Why is it there at all?  Is there a (rough) guideline for
> when to use one or the other?

this is because pattern guards are not the same as case statements, they
do not do matching and deconstruction of values, but rather are boolean
expressions to be checked _in order_ to determine whether to use that
function body or not. 


f n | n < 0 = -1
f n | n < 5 = 1
f n = 2

this cannot be expressed as a single case statement because you cannot
match 'n' against an expression. (just like you cannot deconstruct a
value in a boolean expression as your above example demonstrates)

this translates to nested if statements rather than a single case
statement. what is confusing is that if statements can be constructed
from case statements too... the above translates to

f n = if n < 0 then -1 else if n < 5 then 1 else 2

or in case notation 

case n < 0 of
 True -> -1 
 False -> case n < 5 of
    True -> 1
    False -> 2

note you will need many nested case statements to deal with patter
guards, they are not equivalent to a single case statement. 

in the core language 'case' is the ONLY alternation primitive so
everything can translate to it at some point. pattern guards are useful
because otherwise you end up with heavily nested if or case statements
which are definately less readable than the pattern-guard syntax.

hint: read '|' as 'such that' so
	f n | n < 0 = ... reads "f of n such that n is less than zero is
	note also that the vertical bar interpreted in this way also
	has mathematical precident so its not completely arbitrary.

Hope this helps and that I am not utterly wrong :)


John Meacham
California Institute of Technology, Alum.