Hassan Shahin
Hassan Shahin

Reputation: 211

Conflict in defining a variable ina typeclass implementation

I am trying to implement Eq class type for the following data:

data Pair a = Pair a a
instance Eq a => Eq (Pair a) where
    (==) (Pair x x) (Pair y y) = (x == y)

I am getting the following error message:

Conflicting definitions for ‘y’ in ....

My questions:

  1. I think I am doing things "By the book", so why I am getting this error?
  2. If I am re-defining y, why there isn't a similar error for x?
  3. Where is this "re-definition" in the above code?

Upvotes: 1

Views: 89

Answers (1)

Vanson Samuel
Vanson Samuel

Reputation: 2089

Say the following call is being made:

(Pair 1 2) == (Pair 3 4)

The value 1 will be assigned to x at this point:

(==) (Pair x...

At this point x is being reassign to the value 2 which is not allowed:

(==) (Pair x x...

Hence the error

As for the answer to your second question, you should have gotten an error for both x, and y, like so:

Test.hs:6:16:
    Conflicting definitions for `x'
    Bound at: Test.hs:6:16
              Test.hs:6:18
    In an equation for `=='

Test.hs:6:27:
    Conflicting definitions for `y'
    Bound at: Test.hs:6:27
              Test.hs:6:29
    In an equation for `=='

Upvotes: 3

Related Questions