Haskell 类型家族多态性
因此,我有一个函数Haskell 类型家族多态性,haskell,polymorphism,type-families,Haskell,Polymorphism,Type Families,因此,我有一个函数apply::proxy tf->tf Int->tf Int,它接受一个用于携带类型族的代理,并将Int应用于该类型族,以确定第二个参数的类型和返回值。 然而,我从GHC那里得到了一些令人困惑的回答 {-# LANGUAGE TypeFamilies #-} import Data.Proxy type family F (a :: *) :: * where F Int = () f :: Proxy F f = Proxy apply :: proxy t
apply::proxy tf->tf Int->tf Int
,它接受一个用于携带类型族的代理,并将Int应用于该类型族,以确定第二个参数的类型和返回值。
然而,我从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感到高兴,并且一切都可以正常编译。有人能解释一下这是怎么回事吗
所以我有一个函数apply::proxy tf->tf Int->tf Int
,它接受一个用于携带类型族的代理
你不能这样做。类型族必须始终完全应用,就像它们是其泛化的类型同义词一样。类型变量永远不能实例化为未饱和的类型族
这是GHC 7.8.3中的一个bug,它没有从开始就拒绝您的程序
f :: Proxy F