gpt4 book ai didi

haskell - 模棱两可的情况

转载 作者:行者123 更新时间:2023-12-02 11:38:30 25 4
gpt4 key购买 nike

我目前正在学习如何编写类型类。我似乎无法编写出现不明确编译错误的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com