Type (synonym) families
In 2008, three years after the introduction of associated types, they were subsumed by type families. Associated types are special type families where the type function is attached to a type-class.
In contrast to associated types, we have top-level type-families that are not associated with a type-class, for example:
type family RepF d type instance RepF (List' a) = (RList a)
The type family RepF
represents a type function, with each instance declaring a value. Put another way, a type family represents a set of types, and each instance represents a set member.
In our example, GenericF
simply uses the top-level type function in its type signatures:
class GenericF d where fromF :: d -> (RepF d) toF :: (RepF d) -> d instance GenericF (List' a) where fromF Nil' = L U fromF (Cons' x xs) = R (Combo x xs) toF (L U) = Nil' toF (R (Combo x xs)) = (Cons' x xs) main = print $ fromF (Cons' 1 Nil')
With associated types, we...