简体   繁体   English

重载类型类 function 时出现多个声明错误

[英]Multiple declarations errors when overloading typeclass function

The following code is a code example for overloading the functions of the partial ordered typeclass (POrd) from a lecture slide.下面的代码是一个代码示例,用于从讲座幻灯片中重载偏序类型类 (POrd) 的函数。

When trying to run this code, I get the following multiple declarations errors for the function 'pcompare' and for the operators.尝试运行此代码时,我收到 function 'pcompare' 和运算符的以下多个声明错误。

> class Eq a => POrd a where
>    pcompare :: a -> a -> Maybe Ordering
>    (~<), (~>), (~<=), (~>=) :: a -> a -> Bool
>
>       -- Minimal complete definition:
>       --      (~<)& (~>) | pcompare
> pcompare x y | x == y       = Just EQ
>              | x ~< y       = Just LT
>              | x ~> y       = Just GT
>              | otherwise    = Nothing
>
> x ~<= y = pcompare x y ==  Just LT || x == y
> x ~<  y = pcompare x y ==  Just LT
> x ~>= y = pcompare x y ==  Just GT || x == y
> x ~>  y = pcompare x y ==  Just GT


I get the following error messages:我收到以下错误消息:

lecture9.lhs:5:1: error:
    Multiple declarations of `pcompare'
    Declared at: lecture9.hs:2:4
                 lecture9.hs:5:1
  |
5 | pcompare x y | x == y       = Just EQ
  | ^^^^^^^^

lecture9.lhs:10:3: error:
    Multiple declarations of `~<='
    Declared at: lecture9.lhs:3:4
                 lecture9.lhs:10:3
   |
10 | x ~<= y = pcompare x y ==  Just LT || x == y
   |   ^^^

lecture9.lhs:11:3: error:
    Multiple declarations of `~<'
    Declared at: lecture9.lhs:3:4
                 lecture9.lhs:11:3
   |
11 | x ~<  y = pcompare x y ==  Just LT
   |   ^^

lecture9.lhs:12:3: error:
    Multiple declarations of `~>='
    Declared at: lecture9.lhs:3:4
                 lecture9.lhs:12:3
   |
12 | x ~>= y = pcompare x y ==  Just GT || x == y
   |   ^^^

lecture9.lhs:13:3: error:
    Multiple declarations of `~>'
    Declared at: lecture9.lhs:3:4
                 lecture9.lhs:13:3
   |
13 | x ~>  y = pcompare x y ==  Just GT
   |   ^^

I don't understand what causes the multiple declarations error in this example, as it looks very similar to the official PartialOrd typeclass implementation.我不明白是什么导致了这个例子中的多重声明错误,因为它看起来与官方的 PartialOrd 类型类实现非常相似。

As the commenters on your question point out, the issue is one of indentation.正如您问题的评论者所指出的那样,问题是缩进之一。 For what it's worth, the > symbols are appropriate considering you're trying to compile a literate Haskell file (.lhs) rather than a regular Haskell file.考虑到它的价值, >符号是合适的,因为您正在尝试编译文字 Haskell 文件 (.lhs) 而不是常规的 Haskell 文件。 (That said, in a regular Haskell file (.hs), they would cause parsing errors.) (也就是说,在常规的 Haskell 文件 (.hs) 中,它们会导致解析错误。)

In Haskell, "top-level" declarations are not indented at all.在 Haskell 中,“顶级”声明根本没有缩进。 For instance, the definition of the POrd class is not indented.例如, POrd class 的定义没有缩进。 On the flip side, non -top-level declarations must be indented.另一方面,顶级声明必须缩进。 The type declarations of pcompare and the other operators are part of the POrd class and, as such, are indented. pcompare和其他运算符的类型声明是POrd class 的一部分,因此是缩进的。

The definitions of pcompare and the other operators must also be indented, but they can technically go in two different places. pcompare和其他运算符的定义也必须缩进,但从技术上讲,它们可以在两个不同的地方使用 go。 They could be defined for particular instances of POrd , or they could be defined as defaults for the POrd class.它们可以为POrd的特定实例定义,或者它们可以定义为POrd class 的默认值 Based on the definitions themselves, it seems these are intended as defaults.根据定义本身,似乎这些都是默认值。

Long story short, if you indent the definitions of pcompare and the other operators, you should be good to go.长话短说,如果你缩进pcompare和其他运算符的定义,你应该对 go 很好。 (Keep in mind that the > still needs to be at the start of the line, so add the extra spaces just after the > , as in > pcompare xy | x == y... .) (请记住, >仍然需要位于行首,因此在>之后添加额外的空格,如> pcompare xy | x == y... 。)

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

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