Kinds and DataKinds
A type has a kind
A value has a type and a type has a kind. Values of type Int are numbers, and the type Int has kind * (written Type in newer code); a type constructor such as Maybe takes a type of kind * and gives one, so its kind is * -> *. GHCi prints it with :kind:
:kind Setting
:kind Int
Setting :: Level -> *
Int :: *
Setting is the type of the previous lesson shape: a newtype with a parameter of kind Level, a type of its own. The kind annotation (l :: Level) is what makes the parameter a promoted value rather than an arbitrary type, and it is a compile-time fact with no run-time cost.
Promoted constructors
With DataKinds, the constructors of a data type are also available at the type level, written with a leading quote: 'Low and 'High are types of kind Level. The data type itself becomes a kind, and a parameter can be one of its constructors:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
module Main (main) where
data Level = Low | High
newtype Setting (l :: Level) = Setting Int
deriving (Show)
low :: Setting 'Low
low = Setting 1
high :: Setting 'High
high = Setting 9
main :: IO ()
main = do
print low
print high
ghc -Wall -O1 -o kinds kinds.hs
./kinds
Setting 1
Setting 9
The two values have different types, Setting 'Low and Setting 'High, and the run-time representation is the same Int in both. The Show instance was derived once for the whole family, which is why both print with the constructor name; the parameter is a phantom and disappears at run time.
A parameter that carries no value
A parameter that appears only in the type is called phantom, and its job is to record something the type checker can use and the program does not store. The common uses are:
- A unit of measure, such as Meters against Feet, so adding them is a type error.
- A stage of a document, such as Draft against Published, so publishing twice is a type error.
- A checked format or a permissions marker, which the next lesson but three takes further.
The alternative is a tag field inside the record, which is a value the program can get wrong at run time; the phantom is a check the compiler makes once. The cost is that every function that changes the tag has to be written with the two types, which is the same work as validating a tag and done earlier.
The wrong parameter
A function that wants the high setting rejects the low one, and the error names the two promoted constructors:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
module Main (main) where
data Level = Low | High
newtype Setting (l :: Level) = Setting Int
deriving (Show)
applyHigh :: Setting 'High -> Int
applyHigh (Setting n) = n
main :: IO ()
main = print (applyHigh (Setting 1 :: Setting 'Low))
ghc -Wall -fno-code wrong.hs
wrong.hs:15:26: error: [GHC-83865]
• Couldn't match type ‘Low’ with ‘High’
Expected: Setting High
Actual: Setting Low
• In the first argument of ‘applyHigh’, namely
'(Setting 1 :: Setting 'Low)'
In the first argument of ‘print’, namely
'(applyHigh (Setting 1 :: Setting 'Low))'
• In the expression: print (applyHigh (Setting 1 :: Setting 'Low))
|
15 | main = print (applyHigh (Setting 1 :: Setting 'Low))
| ^^^^^^^^^^^^^^^^^^^^^^^^^
The annotation :: Setting 'Low is needed because a bare Setting 1 has a parameter the compiler cannot choose; once it is written, the mismatch is reported at the call and not somewhere inside the function. That is the whole benefit: the tag check happens once, at compile time, and the run-time value is still one Int.
What DataKinds buys
The extension is a compile-time tool and it is worth it when a value has a small set of states that must not be confused. It is not worth it when the set is large, because every state has to be written as a type and every transition as a function. The next lesson, on deriving strategies, stays at the level of the instances; the one after it, on phantom types, is the same idea with a shorter name.
Exercises
- Add a Medium constructor to Level and a value of the new type.
- Write raise :: Setting 'Low -> Setting 'High and print the result.
- Give Setting a field of the level type as well and compare the two designs.
- Use :kind on Maybe, Either String and [] and write down the three kinds.
- Explain why the phantom parameter costs nothing at run time.
- Add a second phantom parameter for the currency of an amount and explain what the type now prevents.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is a kind?
Answer
The type of a type
Kinds describe types the way types describe values.
-
What does
:kind Settingprint for the newtype of the lesson?Answer
Setting :: Level -> *The parameter has kind Level and the result is a type of kind *.
-
What does DataKinds promote?
Answer
The constructors of a data type to the type level
Low and High become types, written with a leading quote.
-
What does this program print?
{-# LANGUAGE DataKinds #-} {-# LANGUAGE KindSignatures #-} module Main (main) where data Level = Low | High newtype Setting (l :: Level) = Setting Int deriving (Show) main :: IO () main = print (Setting 5 :: Setting 'Low)Answer
Setting 5
The derived Show prints the constructor and the Int, and the phantom is not shown.
-
Why is a phantom parameter free at run time?
Answer
It appears only in types, so no value is stored for it
The representation is the Int and nothing else; the parameter is erased.
-
What does this program print?
module Main (main) where data Level = Low | High deriving (Eq) main :: IO () main = print (High == Low)Answer
False
The two constructors are different, so == gives False.
-
What is the error when a
Setting 'Lowis passed to a function that wants'High?Answer
A type mismatch between the promoted constructors Low and High
The two types differ only in the phantom, and the checker compares them.
-
What is a good use of a phantom parameter?
Answer
A unit of measure or a stage that must not be confused
The phantom records a compile-time distinction at no run-time cost.
-
What does this program print?
{-# LANGUAGE DataKinds #-} {-# LANGUAGE KindSignatures #-} module Main (main) where data Unit = Metre | Second newtype Amount (u :: Unit) = Amount Double main :: IO () main = putStrLn "ok"Answer
ok
The phantom type is declared and not used, and the program prints its line.
-
Which two pragmas does the lesson need?
Answer
DataKindsandKindSignaturesDataKinds promotes the constructors and KindSignatures writes the (l :: Level) annotation.
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