1

GHCi 告诉我 typeA不是 type 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
4

1 回答 1

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'

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

您可以在此处阅读所有相关信息。相关部分为 2.4.4。

于 2013-04-07T17:50:12.917 回答