我目前正在学习如何编写类型类。我似乎无法用含糊不清的编译错误来编写Ord类型类。

module Practice where

class  (Eq a) => Ord a  where
    compare              :: a -> a -> Ordering
    (<), (<=), (>=), (>) :: a -> a -> Bool
    max, min             :: a -> a -> a

    -- Minimal complete definition:
    --      (<=) or compare
    -- Using compare can be more efficient for complex types.
    compare x y
         | x == y    =  EQ
         | x <= y    =  LT
         | otherwise =  GT

    x <= y           =  compare x y /= GT
    x <  y           =  compare x y == LT
    x >= y           =  compare x y /= LT
    x >  y           =  compare x y == GT

    -- note that (min x y, max x y) = (x,y) or (y,x)
    max x y
         | x <= y    =  y
         | otherwise =  x
    min x y
         | x <= y    =  x
         | otherwise =  y

错误是
Practice.hs:26:14:
    Ambiguous occurrence `<='
    It could refer to either `Practice.<=', defined at Practice.hs:5:10
                          or `Prelude.<=',
                             imported from `Prelude' at Practice.hs:1:8-15
...

等等。我认为这与Prelude定义的版本冲突。

最佳答案

问题在于您的函数名称与Prelude中的标准函数名称冲突。

为了解决这个问题,您可以添加一个显式的导入声明来隐藏冲突的名称:

module Practice where

import Prelude hiding (Ord, compare, (<), (<=), (>=), (>), max, min)

...

关于haskell - 模棱两可的发生,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16430025/

10-11 18:29