Haskell: `==' 不是 class 的(可见的)方法

Haskell: `==' is not a (visible) method of class

因此,当我编译以下代码时 edited:

instance (Eq a) => PartOrd a where 
        [] == []         = True
        (x:xs) == (y:ys) = x==y && xs==ys
        _ == _           = False
        xs /= ys         = not (xs == ys)

我得到:

  `==' is not a (visible) method of class 'PartOrd'

  `/=' is not a (visible) method of class 'PartOrd'
Failed, modules loaded: none.

我查看了 How to properly instantiate classes in Haskell? 以获得一些说明,但即使我无法修复它。

此外,当我使用自定义运算符时是否相同,例如 =~ 用于 ==/~ 用于 /=,因为我得到相同的错误?

编辑: 根据要求:

class Eq a => PartOrd a where

   partcmp :: a -> a -> Maybe Ordering 
   partcmp x y = case bigger x y of
                   True   -> Just LT
                   False  -> case smaller x y of
                               True  -> Just GT
                               False -> case x == y of
                                          True  -> Just EQ
                                          False -> Nothing      

不清楚您要实现的目标。以下是一些想法:

  1. 当您声明 class 的实例时,例如 instance (Eq a) => PartOrd a,您应该为 PartOrd a 中的函数提供实现(即 partcmp,而不是 ==/=)。编译器准确地告诉你:==/= 在 class Eq 中,而不是 PartOrd,你没有业务定义函数不在 PartOrd。事实上,无论您使用 ===~ 还是 /~,问题仍然存在:您不应该定义 partcmp 以外的任何内容在 instance (Eq a) => PartOrd a(Eq a) => 部分只是说在这些定义中,您可以假设类型签名 (==), (/=) :: a -> a -> a 的函数 ==/= 已经定义。

  2. 为什么要在实例声明中使用列表?您是不是想说 instance (Eq a) => PartOrd [a]?如果你真的 想说 instance (Eq a) => PartOrd a,你将需要在 GHC 中打开 FlexibleInstances(也许还有更多...)。 (但是,instance (Eq a) => PartOrd a 并没有什么意义。)

  3. 最后,函数biggersmaller是从哪里来的呢?您不能在 class (Eq a) => PartOrd a 中使用它们,因为 a 是通用类型。您将需要函数 bigger, smaller :: a -> a -> a,在这种情况下您甚至不需要 PartOrd class.