4

有谁知道为什么这段代码会失败?

{-# 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.
4

2 回答 2

2

我也看不到 ghc 抱怨什么。我认为这可能是因为它试图为本地绑定提供单态类型,但添加NoMonoLocalBinds到语言杂注并没有改变任何东西。

但是,该代码可以使用最近的 HEAD (7.3.20111026) 进行编译,而且它在没有启用 TypeFamilies 的情况下进行编译,这支持了错误假设。

如果这是你必须解决的真正问题:添加类型签名让 ghc 很高兴。

于 2011-10-28T08:33:05.343 回答
0

好的,这有点超出我的想象,因为我从未在 Haskell 中使用过类型族。但是,您的示例实际上也没有使用类型系列,所以我想我会看看当我从pragma中删除TypeFamilies语言扩展时会发生什么。LANGUAGE事实证明:它编译得很好!:)

所以它很可能是一个 GHC 错误。

话虽这么说,我稍微戳了一下,注意到以下代码在TypeFamilies启用时可以愉快地编译:

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

这可能是荒谬的,因为它推断的类型是mcode :: (Expr m, Monad m) => m (),而不仅仅是,但我的观点是,只有当' 类型以某种方式与' 类型相关联mcode :: Monad m => m ()时,GHC 才似乎很高兴。amcode

不知道这是否有帮助,但它肯定激起了我的好奇心!

于 2011-10-28T06:07:29.350 回答