2013-04-07 62 views
1

GHCi告訴我,類型A不是類型A。爲什麼?與作用域打交道時,下面是清楚地表明瞭這縮短會議類型A不等於Haskell(ghci解釋器)中的類型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 

回答

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。

+0

您的第二個代碼片段看起來僞造。 – Dog 2013-04-07 17:57:07

+6

這不是真正的奇怪的範圍行爲。它與變量的行爲完全相同,也是唯一有意義的選擇,因爲第二個聲明可能與第一個聲明不兼容。問題是它們在錯誤消息中看起來相同。 – hammar 2013-04-07 17:57:18

+0

對不起,我忘了' – jozefg 2013-04-07 17:58:53

相關問題