Differences in pattern matching syntax?

Simon Peyton-Jones simonpj at microsoft.com
Fri Jan 16 11:07:09 EST 2009


OK once I bit the bullet and looked at the code the bug was obvious.

When you pattern match
        Ru{rrsrt = AlwaysExpr} -> error "blah"
GHC uses, well,  pattern-matching to see if rrsrt is AlwaysExpr.

But when you say

        Ru{} -> if (rrsrt r == AlwaysExpr)  then error "blah" else ...

then GHC uses the (==) operation for the data type RuleType (of which AlwaysExpr) is a data constructor.

Sadly you have not defined it. You just say

   instance Eq RuleType

That uses the default methods for equality. Its equivalent to
   instance Eq RuleType where
      (==) a b = not (a /= b)
        (/=) a b = not (a == b)

So it's not surprising that you get a loop.  You probably wanted to use "deriving Eq" on your data type declaration for RuleType, or
        deriving instance Eq RuleType


So, clearly not a bug in GHC; but it would be more felicitous if it gave you a warning about the instance declaration for Eq RuleType.  The difficulty is that it's not clear when to warn; it's ok to use default methods, but you must define *either* (==) *or* (/=).

Simon


More information about the Glasgow-haskell-users mailing list