`([] ==)[]`是如何推断haskell的? [英] How is the type of `([] ==) []` inferred haskell?

查看:159
本文介绍了`([] ==)[]`是如何推断haskell的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来很愚蠢,但我无法得到它。为什么可以输入表达式[] == []?更具体地说,哪一种类型(类Eq中)被推断为列表元素的类型?



在ghci会话中,我看到以下内容:

  Prelude> :t(== [])
(== [])::(等式[a])=> [a] - > Bool

但约束 Eq [a] 这意味着 Eq a 也如下所示:

  Prelude> (== [])([] :: [IO()])

< interactive>:1:1:
(Eq(IO()))$在< interactive>:1:1-2处使用'=='产生的b
$ b可能的修正:为(Eq(IO()))
添加一个实例声明在`it':it =(== [])([] :: [IO()])



<因此,在[] == []中,类型检查器必须假定列表元素是类E中的某个类型a。但是哪一个? []的类型只是[a],这当然比等式a => [a]更笼统。


恕我直言,这应该是模棱两可的,至少在Haskell 98(这就是我们正在谈论的)

解决方案

GHCi has 类型违约的扩展规则,这就是绊倒你的原因。在这种情况下,我相信它会将不明确的类型默认为()。 GHCi表现出不同的微妙方式更好的交互性,但偶尔会导致混淆......


It sounds silly, but I can't get it. Why can the expression [] == [] be typed at all? More specifically, which type (in class Eq) is inferred to the type of list elements?

In a ghci session, I see the following:

Prelude> :t (==[])
(==[]) :: (Eq [a]) => [a] -> Bool

But the constraint Eq [a] implies Eq a also, as is shown here:

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 ()])

Thus, in []==[], the type checker must assume that the list element is some type a that is in class Eq. But which one? The type of [] is just [a], and this is certainly more general than Eq a => [a].

IMHO this should by ambiguous, at least in Haskell 98 (which is what we are talking about)

解决方案

GHCi has extended rules for type defaulting, which is what tripped you up. In this case, I believe it would default the ambiguous type to (). The subtle ways that GHCi behaves differently are nice for the sake of better interactivity, but they do occasionally result in confusion...

这篇关于`([] ==)[]`是如何推断haskell的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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