【发布时间】:2011-02-22 11:37:44
【问题描述】:
这听起来很愚蠢,但我无法理解。为什么可以输入表达式 [] == [] ?更具体地说,哪个类型(在 Eq 类中)被推断为列表元素的类型?
在 ghci 会话中,我看到以下内容:
Prelude> :t (==[])
(==[]) :: (Eq [a]) => [a] -> Bool
但约束Eq [a] 也暗示Eq a,如下所示:
Prelude> (==[]) ([]::[IO ()])
<interactive>:1:1:
No instance for (Eq (IO ()))
arising from use of `==' at <interactive>:1:1-2
Probable fix: add an instance declaration for (Eq (IO ()))
In the definition of `it': it = (== []) ([] :: [IO ()])
因此,在 []==[] 中,类型检查器必须假定列表元素是类 Eq 中的某个类型 a。但是哪一个? [] 的类型只是 [a],这肯定比 Eq a => [a] 更通用。
恕我直言,这应该是模棱两可的,至少在 Haskell 98 中(这就是我们正在谈论的)
【问题讨论】:
-
使用 GHC(不是 GHCi)我有
Ambiguous type variable `a' in the constraint `Eq a' arising from a use of `==' -
@Kenny - 是的,这就是我在 ghci 中尝试之前所期望的 - 我的惊喜越大。感谢您的提示,现在我的世界秩序已恢复 :)
-
有趣的是,Hugs 给出了表达式
[] == []类型Eq a => Bool。 -
对不起“necroposting”,但没有办法指定
[] == []的上下文是吗?没有专门[]自己。
标签: haskell types type-inference type-systems