这听起来很愚蠢,但我无法理解。为什么可以输入表达式 [] == [] 呢?更具体地说,哪个类型(在 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 中(这就是我们正在谈论的)