类型A不等于Haskell中的类型A(ghci解释器)?

时间:2013-04-07 17:44:05

标签: haskell types shadowing

GHCi告诉我A类型不是A类型。为什么呢?

>>> data A = A
>>> let x = A
>>> let id A = A
>>> 
>>> data A = A
>>> let x' = A
>>> let id' A = A
>>> 
>>> data A = A
>>>
>>> let y = id' x

<interactive>:18:13:
    Couldn't match expected type `main::Interactive.A'
                with actual type `main::Interactive.A'
    In the first argument of id', namely `x'
    In the expression: id' x
    In an equation for `y': y = id' x

1 个答案:

答案 0 :(得分:7)

GHCi在处理范围界定时有一些奇怪的行为,这是一个较短的会话,清楚地证明了这一点:

Prelude> data A = A
Prelude> let meh A = A
Prelude> data A = A
Prelude> meh A

<interactive>:5:5:
    Couldn't match expected type `main::Interactive.A'
            with actual type `A'
    In the first argument of `meh', namely `A'
    In the expression: meh A
    In an equation for `it': it = meh A

就GHCi而言,你可能也是这样做的:

Prelude> data A = A
Prelude> let meh A = A
Prelude> data A' = A'
Prelude> meh A'

<interactive>:5:5:
    Couldn't match expected type `A' with actual type A'
    In the first argument of `meh', namely A'
    In the expression: meh A'
    In an equation for `it': it = meh A'

它认为它是完全不同的数据类型,只是它们具有相同的名称。

您可以阅读所有相关信息here。相关部分是2.4.4。