[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Pattern guards.



Original-Via: uk.ac.ukc; Thu, 27 Sep 90 17:04:38 BST
From: Kent Karlsson <kent>
Date: Thu, 27 Sep 90 17:54:33 +0200
To: haskell@cs.glasgow.ac.uk
Subject: Pattern guards.
Original-Sender: kent%se.chalmers.cs@sunic.sunet.se
Sender: haskell-request@cs.glasgow.ac.uk

			PATTERN GUARDS
			==============

Why must guards be at the end of a pattern?  There are occasions
where it is useful to have them in middle of a pattern.  Let's take
a simple example where it does not matter first (using "===" for 
meta-equality):

f 0 0 = ...
===
f    0                           0                         = ...
===
f    x | (x == fromInteger 0)    y | (y == fromInteger 0)  = ...

(The syntax would now have to require guard expressions to be atomic.)

  As a second example let's look at another simple example where it
does matter (for the degree of laziness) whether the guards can be
put inside the pattern (and "evaluated inside" too) or must be at the end:

h 0 (C1 x) = ...
===
h   0                           (C1 x)   = ...
===
h   y | (y == fromInteger 0)    (C1 x)   = ...

If the guard was to be moved to the end:

h   y                           (C1 x)   | (y == fromInteger 0) = ...

the "pattern matching" would be done "from right to left" in this example, 
if it was the "h 0 (C1 x) = ..." clause that was transformed into
"h y (C1 x) | (y == fromInteger 0) = ...", whereas Haskell specifies that
it should be done from left to right, which is still the case with
"h y | (y == fromInteger 0) (C1 x) = ..."

(I might not recommend a programmer to put a guard in the middle of a 
pattern, but that could be left as a convention, rather than a restriction.)


			/kent k