Quindi ho una funzione apply :: proxy tf -> tf Int -> tf Int
che accetta un proxy destinato a trasportare una famiglia di tipi e applica Int a tale famiglia di tipi per determinare il tipo del secondo argomento e il valore restituito. Tuttavia, ricevo alcune risposte confuse da GHC.Tipo Famiglia Polymorphism
{-# 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
rifiuta di compilare con GHC sputando questo errore:
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()
Confusamente, commentando test1
e l'utilizzo di un let vincolante nel test2
rende GHC felice e tutto compila bene. Qualcuno può spiegare cosa sta succedendo qui?