添加ghci的推断类型签名会导致错误

时间:2011-12-16 09:13:54

标签: haskell type-families

编辑:这是一个非常简单的例子。下面这个例子的动机。

编译:

{-# LANGUAGE TypeFamilies #-}

type family F a b

f :: a -> F a b
f = undefined

f' [a] = f a

ghci报告说:

*Main> :t f'
f' :: [a] -> F a b

但是如果我们将这种类型的签名添加到上面的文件中,它会抱怨:

*Main> :r
[1 of 1] Compiling Main             ( test.hs, interpreted )

test.hs:9:14:
    Couldn't match type `F a b0' with `F a b'
    NB: `F' is a type function, and may not be injective
    In the return type of a call of `f'
    In the expression: f a
    In an equation for `f'': f' [a] = f a
Failed, modules loaded: none.

是什么给出了?


动机:

看到this question之后,我以为我会成为一个聪明的人,并写一个小笑话解决方案。攻击计划是从类型级别的数字开始(像往常一样),然后编写一个小的类型级函数Args n a c,它产生的函数类型需要n ac个产生n。然后我们可以为各种{-# LANGUAGE TypeFamilies #-} data Z = Z data S a = S a type family Args n a c type instance Args Z a c = c type instance Args (S n) a c = a -> Args n a c class OnAll n where onAll :: n -> (b -> a) -> Args n a c -> Args n b c instance OnAll Z where onAll Z f c = c instance OnAll n => OnAll (S n) where onAll (S n) f g b = onAll n f (g (f b)) 编写一个小型类,并在我们的路上。这就是我想要写的内容:

{{1}}

我很惊讶地发现这没有进行类型检查!

1 个答案:

答案 0 :(得分:9)

这是一个GHC错误,可以通过以下进一步简化的示例来证明:

type family F a

f :: b -> F a
f = undefined

f' :: b -> F a
f' a = f a

我建议向GHC总部报告。

相关问题