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

Dog*_*Dog 1 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
Run Code Online (Sandbox Code Playgroud)

Dan*_*zer 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
Run Code Online (Sandbox Code Playgroud)

就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'
Run Code Online (Sandbox Code Playgroud)

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

你可以在这里阅读所有相关内容.相关部分是2.4.4.

  • 这种奇怪的范围行为并不是真的.它与变量的行为完全相同,唯一的选择是有意义的,因为第二个声明可能与第一个声明不兼容.问题是它们在错误消息中看起来是一样的. (6认同)
  • @Dog:在我看来,你只是在这里挑剔,从而浪费每个人的时间.更重要的是,这似乎不是你这种类型的第一个问题.至少可以说,我觉得这很粗鲁. (4认同)
  • @Dog:你有没有想过GHC版本可能会有所不同?腐败记忆绝对不是*原因.你认为什么是"好"的信息?毕竟,两种类型*都被称为"A".这个问题很有趣. (2认同)