简体   繁体   English

定义Eq实例-Haskell

[英]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? Hy,我已经为自然数定义了一个数据结构,并且想要定义一个Eq实例,看看两个数字是否相等,但我不断得到这样的信息:“Eq'的模糊出现。它可能指的是Main.eq或Prelude.eq“你能告诉我,我可能做错了吗?

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! 非常感谢!

Haskell's Prelude (similar to a standard library) defines an Eq class. Haskell的Prelude(类似于标准库)定义了Eq类。 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. 您遇到的问题是Haskell不知道'Eq'是指您定义的类还是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 有关Haskell Prelude及其Eq的更多信息,请访问: http//hackage.haskell.org/package/base-4.6.0.1/docs/Prelude.html#t :Eq

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 . 您已经添加了一个名为Eq的类的定义,该类与Prelude中的类不同,并且编译器抱怨它在您编写instance Eq Nat时不知道您尝试实例化哪一个。

You should remove the declaration of class Eq a where ... from your code. 您应该从代码中删除class Eq a where ...的声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM