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

Dan*_*ner 9 haskell type-families

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

这编译:

{-# LANGUAGE TypeFamilies #-}

type family F a b

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

f' [a] = f a
Run Code Online (Sandbox Code Playgroud)

而且ghci报告说:

*Main> :t f'
f' :: [a] -> F a b
Run Code Online (Sandbox Code Playgroud)

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

*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.
Run Code Online (Sandbox Code Playgroud)

是什么赋予了?


动机:

在看到这个问题之后,我以为我会成为一个聪明的人,并写一个小笑话解决方案.攻击计划是从类型级别的数字开始(像往常一样),然后编写一个小类型级别的函数Args n a c,它产生n复制的函数类型a并生成一个c.然后我们可以为各种类型写一个小型类,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))
Run Code Online (Sandbox Code Playgroud)

我惊讶地发现这没有打字!

ehi*_*ird 9

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

type family F a

f :: b -> F a
f = undefined

f' :: b -> F a
f' a = f a
Run Code Online (Sandbox Code Playgroud)

我建议向GHC总部报告.