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幸福和一切編譯罰款。任何人都可以解釋這裏發生了什麼?