不明确的发生 [英] Ambiguous Occurrence

查看:126
本文介绍了不明确的发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习如何编写类型类。我似乎无法编写带有模糊出现编译错误的Ord类型类。

  module练习其中

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

- 最小完整定义:
- (<=)或比较
- 对于复杂类型,使用比较可以更高效。
比较x y
| x == y = EQ
| x <= y = LT
|否则= GT

x <= y =比较x y / = GT
x < y =比较x y == LT
x> = y =比较x y / = LT
x> y =比较x y == GT

- 注意(min x y,max x y)=(x,y)或(y,x)
max x y
| x< = y = y
|否则= x
min x y
| x <= y = x
|否则= y

错误是

  Practice.hs:26:14:
模糊发生`< ='
它可以指Practice.hs中定义的'Practice。< =' 5:10
或`Prelude。< =',
从Practice.hs:1:8-15的`Prelude'导入
...

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

解决方案

问题在于您的函数名称与标准冲突为了解决这个问题,你可以添加一个隐藏冲突名称的显式导入声明:



< (<),(< =),(> =),(> ),最大值,最小值)

...


I am currently learning how to write type classes. I can't seem to write the Ord type class with compile errors of ambiguous occurrence.

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

Errors are

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
...

and so on. I think it is clashing with the Prelude defined version.

解决方案

The problem is that the names of your functions are clashing with the standard ones from the Prelude.

To solve this, you can add an explicit import declaration which hides the conflicting names:

module Practice where

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

...

这篇关于不明确的发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆