错误:类出现“Ord”不明确

发布于 2024-10-01 21:33:45 字数 507 浏览 4 评论 0原文

data (Ord a) => Stree a = Null 
| Fork (Stree a) a (Stree a)

mkStree :: (Ord a) => [a] -> Stree a
mkStree [] = Null
mkStree (x:xs) = Fork (mkStree smaller) x (mkStree larger)
               where (smaller,larger) = partition (<= x) xs                    
partition :: (a->Bool) -> [a] -> ([a],[a])
partition p xs = ([ x | x <- xs, p x],
                  [ y | y <- xs, (not . p) y])

我该如何解决这个故障 -->不明确的类出现“Ord” * 可以参考:Hugs.Prelude.Ord

data (Ord a) => Stree a = Null 
| Fork (Stree a) a (Stree a)

mkStree :: (Ord a) => [a] -> Stree a
mkStree [] = Null
mkStree (x:xs) = Fork (mkStree smaller) x (mkStree larger)
               where (smaller,larger) = partition (<= x) xs                    
partition :: (a->Bool) -> [a] -> ([a],[a])
partition p xs = ([ x | x <- xs, p x],
                  [ y | y <- xs, (not . p) y])

how can i fix this failure --> Ambiguous class occurrence "Ord"
* Could refer to: Hugs.Prelude.Ord

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

匿名的好友 2024-10-08 21:33:45

我无法重现此错误,但我可以进行一些猜测。 “不明确的类出现”错误意味着作用域中存在多个“Ord”定义,因此真正的问题不在您在此处包含的代码中。问题是您导入的模块之一由于某种原因重新定义了“Ord”,或者您在代码中重新定义了它。无论哪种方式,唯一可行的方法是隐藏或限定其他定义(例如 Prelude 中的默认定义)。如果您尝试使用非标准 Ord 实现,则需要导入隐藏 Ord 的 Prelude:

import Prelude hiding(Ord)

如果您不是故意使用非标准 Ord,那么您需要弄清楚第二个 Ord 的位置来自并删除或隐藏它。不过,您的问题中没有足够的信息让我能够说出如何做到这一点。我希望错误消息也比您在此处列出的内容长,因为它应该显示 Ord 的两个定义的位置。

I cannot reproduce this error, but I can take some guesses. The "Ambiguous class occurrence" error means that there is more than one definition of "Ord" in scope, so the real problem is not in the code you've included here. The problem is either that one of the modules you're importing redefines "Ord" for some reason, or you're redefining it in your code. Either way, the only way that can work is if other definitions (such as the default one in the Prelude) are hidden or qualified. If you're trying to use a non-standard Ord implementation, you'll need to import the Prelude hiding Ord:

import Prelude hiding(Ord)

If you're not intentionally using a non-standard Ord, then you'll need to figure out where the second one is coming from and remove or hide it. There's not enough information in your question for me to be able to say how to do so, though. I would expect the error message is also longer than what you've listed here, as it should show the location of both definitions of Ord.

假装爱人 2024-10-08 21:33:45

如果从数据声明中删除 Ord 上下文会发生什么?

data Stree a = Null | Fork (Stree a) a (Stree a)

数据声明上的类上下文非常不直观,我怀疑这可能会导致 Hugs 出错。

无论如何,您都不应该将上下文放在数据或新类型声明上。它们对于某些 GHC 扩展很有用,但我认为 Hugs 不支持任何这些情况。

What happens if you remove the Ord context from the data declaration?

data Stree a = Null | Fork (Stree a) a (Stree a)

Class contexts on data declarations are pretty non-intuitive, and I suspect that may be causing Hugs to error.

In any case, you shouldn't put contexts on data or newtype declarations. They're useful with certain GHC extensions, but I don't think Hugs supports any of those cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文