类型家族多态性

Type Family Polymorphism

所以我有一个函数 apply :: proxy tf -> tf Int -> tf Int,它接受一个 Proxy 来携带一个类型族,并将 Int 应用于该类型族以确定第二个参数的类型和 return 值。 但是,我从 GHC 收到了一些令人困惑的回复。

{-# LANGUAGE TypeFamilies #-}

import Data.Proxy

type family F (a :: *) :: * where
    F Int = ()

f :: Proxy F
f = Proxy

apply :: proxy tf -> tf Int -> tf Int
apply _ x = x

-- Doesn't typecheck.
test1 :: ()
test1 = apply f ()

-- Typechecks fine
test2 :: ()
test2 = let g = apply f in g ()

test1 拒绝使用 GHC 编译并吐出此错误:

tftest.hs:16:9:
    Couldn't match expected type ‘()’ with actual type ‘F Int’
    In the expression: apply f ()
    In an equation for ‘test1’: test1 = apply f ()

tftest.hs:16:17:
    Couldn't match expected type ‘F Int’ with actual type ‘()’
    In the second argument of ‘apply’, namely ‘()’
    In the expression: apply f ()

令人困惑的是,注释掉 test1 并在 test2 中使用 let 绑定会使 GHC 满意并且一切都可以正常编译。谁能解释一下这是怎么回事?

So I have a function apply :: proxy tf -> tf Int -> tf Int which takes a Proxy intended to carry a type family

你不能这样做。必须始终完全应用类型族,就像它们是概括的类型同义词一样。类型变量永远不能实例化为欠饱和类型族。

这是 GHC 7.8.3 中的一个错误,它尚未拒绝您从

开始的程序
f :: Proxy F