2015-04-24 10 views
8

Bu yüzden, bir tür ailesini taşımayı amaçlayan bir Proxy alan ve ikinci bağımsız değişken ve dönüş değeri türünü belirlemek için bu tür aileye Int uygulayan bir işlevi olan apply :: proxy tf -> tf Int -> tf Int işlevim var. Ancak, GHC'den bazı kafa karıştırıcı yanıtlar alıyorum. Tip Aile Polimorfizmi

{-# 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 bu hatayı tükürme ile derlemek reddediyor:

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() 

Ait, test1 dışında yorum ve test2 yılında bağlama let kullanarak GHC mutlu ediyor ve her şey iyi derler. Burada neler olduğunu açıklayan var mı?

cevap

12

So I have a function apply :: proxy tf -> tf Int -> tf Int which takes a Proxy intended to carry a type family

Bunu yapamazsınız. Tip aileler, genelleştirildikleri türün eşanlamlıları gibi her zaman tam olarak uygulanmalıdır. Bir tip değişkeni, doymamış tipte bir aileye asla uygulanamaz.

O ghc bir hata zaten

f :: Proxy F 
başlayan programınızı reddetmek olmadığını 7.8.3 var