Irina Avram
Irina Avram

Reputation: 1532

define Eq instance -Haskell

Hy, I have defined a data structure for natural numbers, and would like define an Eq instance, to see if two numbers are equal or not, but I keep getting the message: "Ambiguous occurence of 'Eq'. It could refer to either Main.eq or Prelude.eq" Could you tell me, what I might be doing wrong?

data Nat = Z | S Nat deriving Show

class Eq a where
  (==) :: a -> a -> Bool    

instance Eq Nat where
  Z == Z = True
  (S x) == (S y) = x == y
  x == y = False

Thanks a lot!

Upvotes: 1

Views: 1335

Answers (2)

Geoffrey Marsi
Geoffrey Marsi

Reputation: 74

Haskell's Prelude (similar to a standard library) defines an Eq class. The problem you're running into is that Haskell doesn't know whether 'Eq' refers to the class you defined or the one built into Haskell.

Consider renaming your class.

More info on the Haskell Prelude and its Eq is here: http://hackage.haskell.org/package/base-4.6.0.1/docs/Prelude.html#t:Eq

Upvotes: 3

porges
porges

Reputation: 30590

You have added a definition of a class called Eq which is different to that in the Prelude, and the compiler is complaining that it doesn't know which one you're trying to instantiate when you write instance Eq Nat.

You should remove the declaration of class Eq a where ... from your code.

Upvotes: 1

Related Questions