有谁知道为什么这段代码失败了?
{-# 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.
最佳答案 我也看不出ghc抱怨什么.我认为这可能是因为它试图给本地绑定提供单态类型,但是将noMonoLocalBinds添加到语言编译指示并没有改变任何东西.
但是,代码与最近的HEAD(7.3.20111026)一样编译,以及它在没有启用TypeFamilies的情况下进行编译的事实,它支持错误假设.
如果这是一个真正的问题,你必须解决:添加类型签名使ghc高兴.