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

回答

12

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