haskelltypesghctype-families

Why can't type families/synonyms be partially applied, when we can work around the restriction anyway?


Consider the following Map type function:

type Map :: (k1 -> k2) -> [k1] -> [k2] 
type family Map f l where
  Map _ '[] = '[]
  Map f (x : xs) = f x : Map f xs

I can then define:

type MkPair a = (a, a)

and attempt to do the following:

type Blah = Map MkPair [Int, Char]

but I find out that types synonyms can't be partially applied.

But I can do this slightly more roundabout route:

type Mapper :: k1 -> k2 -> Type
data Mapper k1 k2 where
  Mapper :: Type -> Mapper k1 k2

type MapF :: Mapper k1 k2 -> [k1] -> [k2] 
type family MapF f l where
  MapF _ '[] = '[]
  MapF ft (x : xs) = MapFT ft x : MapF ft xs

type MapFT :: Mapper k1 k2 -> k1 -> k2
type family MapFT kt k1 

data MkPair

type instance MapFT ('Mapper MkPair :: Mapper Type Type) a = (a, a)

type Blah = MapF ('Mapper MkPair :: Mapper Type Type) [Int, Char]

-- This compiles
f :: Proxy Blah -> Proxy '[(Int, Int), (Char, Char)]
f = id 

I can even do this:

data SymMap

type instance MapFT ('Mapper SymMap :: Mapper Symbol Type) a = Proxy a

g :: Proxy (MapF ('Mapper SymMap :: Mapper Symbol Type) ["hello", "world"]) -> Proxy '[Proxy "hello", Proxy "world"]
g = id 

And all is good.

It seems to me I've subverted the "can't partially apply type synonyms" thing. This transformation seems messy but it's also mechanical, and it's not obvious to me what cases this transformation wouldn't work?

So then I ask, what is the purpose of the restriction of not being able to partially apply type synonyms/families, if one can just work around it by moving your synonym into a type family?


Solution

  • Type families can't be partially applied because it breaks type inference. In GHC, the equality f a ~ g b is equivalent to f ~ g and a ~ b. That is no longer the case if f and g are allowed to be type families. You can work around it by distinguishing two kinds of type-level applications, but the use cases probably weren't as clear as they are now at the time when type families were introduced. See also Higher-order type-level programming in Haskell.

    That transformation to work around the lack of partial application is called defunctionalization.