Problem with 'nested' pattern matching

carlos.scheidegger@via-rs.net carlos.scheidegger@via-rs.net
Thu, 31 Jan 2002 20:12:48 -0200


<color><param>0100,0100,0100</param>Hello.


	I'm having some trouble trying to understand exactly what's 
behind the rule for pattern-matching with data constructors. The 
code I'm having trouble with is similar to this:


f (C p1 p2 (C2 p3 p4)) = ...

f _ = False


What happens is if f is called with (C p1 p2 (NOT_C2 ...)), I get a 
program error, and not False.


I dug up the semantics of pattern-matching on the Report, which 
says, concerning data constructors:


"4. Matching a non- bot value can occur against three kinds of 
refutable patterns:

(a) Matching a non- bot value against a pattern whose outermost 
component is a constructor defined by data fails if the value being 
matched was created by a different constructor. If the constructors 
are the same, the result of the match is the result of matching the 
sub-patterns left-to-right against the components of the data value: 
if all matches succeed, the overall match succeeds; the first to fail 
or diverge causes the overall match to fail or diverge, respectively."


The "overall" match refers to the whole function or the equation 
being considered? More specifically, in my example: if matching 
against C succeeds, and matching against C2 fails, should the 
implementation try the next equation or give a program error?


I used to think that if C succeeded and C2 failed, the next equation 
would be tried, but it seems that is not what is happening (at least 
with Hugs).


One workaround seems to be to change (C2 p3 p4) into a var 
pattern, and match the C2 constructor inside a separate case 
statement, but this would clutter up the code considerably (maybe 
not in the example case, but I intended to pattern-match against 
some reasonably complex structures)


Is there any solution?

Thank you in advance, and thanks for helping,

Carlos Eduardo Scheidegger

<nofill>