Problem with 'nested' pattern matching

2002-01-31 Thread carlos . scheidegger
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


___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell


Re: Problem with 'nested' pattern matching

2002-02-01 Thread Rijk J . C . van Haaften

carlos wrote:
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 tried this in Hugs:

data Test
    = C  Bool Bool Test
    | C2 Bool Bool
    | NOT_C2 Int

f :: Test -> Bool
f (C p1 p2 (C2 p3 p4)) = (p1 && p3) || (p2 && p4)
f _ = False 

evaluating f (C True False (NOT_C2 25)) gives False as expected.
Which compiler/interpreter do you use? Which version?

Actually I can't imagine a bug of this kind in any of the mainstream
compilers (GHC, HBC, NHC and NHC) and interpreters (Hugs, GHCi, and HBI),
though on this computer I only can check hugs.

Regards,

Rijk-Jan van Haaften