2015-04-24 67 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