Type families
A family of types
A type class maps a type to a dictionary of functions. A type family maps a type to a type: it is a function at the type level, defined by equations that the compiler matches on the argument. The pragma is TypeFamilies and the keyword is type family:
type family Rep a where
Rep Int = Int
Rep Bool = String
Rep Int is Int and Rep Bool is String, decided at compile time. The family is a function of the type and not a class: there is no dictionary and no instance, only the equations. A signature that mentions Rep a promises a result whose type depends on the argument.
A closed type family
The two equations above are a closed family: the list is complete and in this module, and no other module can add a case. It is the form to use when the set of types is known, and the error when a type has no equation is clear:
{-# LANGUAGE TypeFamilies #-}
module Main (main) where
type family Rep a where
Rep Int = Int
Rep Bool = String
class Encode a where
encode :: a -> Rep a
instance Encode Int where
encode n = n * 2
instance Encode Bool where
encode b = if b then "yes" else "no"
main :: IO ()
main = do
print (encode (21 :: Int))
print (encode True)
ghc -Wall -O1 -o rep rep.hs
./rep
42
"yes"
The class that uses it
The method encode :: a -> Rep a returns a different type for each instance, and that is allowed because the family is total for the instances that exist:
- For Int the method is Int -> Int, so the body n * 2 type checks.
- For Bool it is Bool -> String, so the body returns "yes" or "no".
- The two calls in main are printed with different types: 42 is a number and "yes" is a string with its quotes.
The alternative without a family is a method that returns a fixed type, and a String for both instances would be the common choice; the family is what lets the Int case stay a number and skip the conversion. That is a real convenience in a library and a real complication in an error message.
A type with no equation
A Double has no instance and the family has no equation for it, so the call fails twice: no Encode Double and no Rep Double for the result of encode. The compiler reports both, and the first one is the one to read:
{-# LANGUAGE TypeFamilies #-}
module Main (main) where
type family Rep a where
Rep Int = Int
Rep Bool = String
class Encode a where
encode :: a -> Rep a
instance Encode Int where
encode n = n * 2
instance Encode Bool where
encode b = if b then "yes" else "no"
main :: IO ()
main = print (encode (1.5 :: Double))
ghc -Wall -fno-code nodouble.hs
nodouble.hs:19:8: error: [GHC-39999]
• No instance for ‘Show (Rep Double)’ arising from a use of ‘print’
• In the expression: print (encode (1.5 :: Double))
In an equation for ‘main’: main = print (encode (1.5 :: Double))
|
19 | main = print (encode (1.5 :: Double))
| ^^^^^
The second error in the same build says No instance for ‘Encode Double’, which is the root cause; the first is the consequence, because without the family result the printer has nothing to show. A closed family makes this a compile error at the call, which is the point: a type that nobody wrote an equation for cannot reach run time.
Open families and associated types
The other form is an open family, where the equations live in the instances of a class and any module may add one:
class Container f where
type Elem f
empty :: f
insert :: Elem f -> f -> f
instance Container [a] where
type Elem [a] = a
empty = []
insert = (:)
Elem is an associated type: a family declared inside the class, with its equations supplied by each instance. This is how containers gives a Map a key type and a value type without a second parameter on every function, and how a modern library keeps a class and its associated types in one place. The closed form of the lesson and the open form here are two tools of the same extension, and the choice is whether the set of cases is yours to finish.
The next lesson takes the same idea one level up: a function that is polymorphic in its argument in a way the caller chooses, which needs forall inside a type.
Exercises
- Add Rep Char = String and an instance for Char.
- Write a family that maps a type to the type of a list of it and use it in a class.
- Explain the difference between a closed and an open family with an example each.
- Make the Encode class return String for every instance and compare the two designs.
- Add an associated type to a class of your own and an instance for Maybe.
- Read the two errors for encode (1.5 :: Double) and say which one names the root cause.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a type family define?
Answer
A function from a type to a type
The equations map types to types, decided by the compiler.
-
What is a closed type family?
Answer
A family whose equations are all in one module
The closed form lists every case and no other module can add one.
-
What does
encode :: a -> Rep amean forInt?Answer
The method is Int -> Int there
Rep Int is Int, so the instance body returns a number, as the output 42 shows.
-
What does this program print?
{-# LANGUAGE TypeFamilies #-} module Main (main) where type family Rep a where Rep Int = Int class Encode a where encode :: a -> Rep a instance Encode Int where encode n = n + 1 main :: IO () main = print (encode (41 :: Int))Answer
42
Rep Int is Int and the instance adds one, so the result is 42.
-
What error does a type with no equation give?
Answer
A compile error about the missing instance and the family result
The family has no case for Double, so the call cannot type check.
-
What is an associated type?
Answer
A family declared inside a class and defined by its instances
The class declares the family and each instance supplies the equation.
-
What is
Rep Boolwith the equations of this lesson?Answer
StringThe equation Rep Bool = String makes the family result of Bool the type String.
-
Which pragma enables the syntax of this lesson?
Answer
TypeFamiliesTypeFamilies enables both closed families and associated types.
-
What does an open family allow that a closed one does not?
Answer
Another module can add an equation
The open form leaves the set of cases open to the instances.
-
What does this program print?
module Main (main) where class Encode a where encode :: a -> String instance Encode Int where encode n = show (n * 2) main :: IO () main = putStrLn (encode (21 :: Int))Answer
42
The method returns a String here and putStrLn prints it without quotes.
The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev