8
所以我有一个函数apply :: proxy tf -> tf Int -> tf 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
结合使得GHC幸福和一切编译罚款。任何人都可以解释这里发生了什么?