2011-10-28 24 views
4

有沒有人知道爲什麼這段代碼失敗?haskell - 奇怪的模糊類型變量錯誤消息代碼在「where」語句與TypeFamilies擴展

{-# LANGUAGE NoMonomorphismRestriction, 
      TypeFamilies #-} 

module Test where 

asExprTyp :: Expr γ => 
    γ α 
    -> α 
    -> γ α 
asExprTyp x _ = x 

int = undefined :: Integer 

class Expr γ where 
    a :: γ α 

-- this works fine 
b = a `asExprTyp` int 

-- this fails 
mcode = do 
    return() 
    where b = a `asExprTyp` int 

的錯誤如下,

Test.hs:23:15: 
    Ambiguous type variable `γ0' in the constraint: 
    (Expr γ0) arising from a use of `a' 
    Probable fix: add a type signature that fixes these type variable(s) 
    In the first argument of `asExprTyp', namely `a' 
    In the expression: a `asExprTyp` int 
    In an equation for `b': b = a `asExprTyp` int 
Failed, modules loaded: none. 
+0

我在這裏沒有看到問題。你認爲'b'在失敗的表達中有什麼類型? –

+0

@JohnL:'b :: Exprγ=>γInteger' – mergeconflict

+0

@mergeconflict:這是完全合理的。我在考慮將γ作爲關聯類型,但它絕對不是。 –

回答

2

我不明白什麼ghc抱怨。我認爲這可能是因爲它試圖給本地綁定一個單形類型,但將NoMonoLocalBinds添加到語言雜注並沒有改變任何東西。

但是,該代碼與最近的HEAD(7.3.20111026)一起編譯,以及編譯時沒有啓用TypeFamilies的事實,它支持錯誤假設。

如果這是一個真正的問題,你必須解決:添加類型簽名使ghc高興。

+0

是的,看起來像一個錯誤。感謝您檢查頭部信息...我不會在我的沙箱中進行結帳,因爲構建大小爲2 GB。 – gatoatigrado

0

好吧,這是有點超過我的頭,因爲我從來沒有在Haskell使用類型的家庭。但是,您的示例實際上並未使用類型族,所以我想我會看到在從LANGUAGE附註中刪除TypeFamilies語言擴展時會發生什麼情況。結果:它編譯得很好,然後! :)

所以這可能是一個GHC錯誤。

話雖這麼說,我捅了它一下,注意到以下啓用了TypeFamilies愉快編譯:

mcode = do 
      b 
      return() 
     where b = a `asExprTyp` int 

這可能是沒有意義的,因爲它的推斷類型是mcode :: (Expr m, Monad m) => m(),而不是僅僅mcode :: Monad m => m(),但我的觀點是,只有當a的類型以某種方式與mcode的類型相關時,GHC纔會開心。

不知道這是否有幫助,但它絕對引起了我的好奇心!