Kinds
Advanced page. Read data types and polymorphism first; this page assumes both.
Every type has a kind, which says how many type arguments it takes before it
is a type you can have a value of. Int and String have kind Type; Maybe
does not — it needs one argument, so its kind is Type -> Type. Kind arrows read
like function types: Either is Type -> Type -> Type, and (->) is
Type -> Type -> Type as well.
| Type | Kind | Meaning |
|---|---|---|
Int, String, Maybe Int |
Type |
a type with values |
Maybe, List, Tree |
Type -> Type |
a type constructor waiting for one argument |
Either, (,) |
Type -> Type -> Type |
waiting for two |
IO |
Type -> Type |
a type constructor over its result |
A kind is not something you usually write. The compiler infers it from how the type is declared and how it is used.
Inference gives a constructor its kind
data Pair f a = Pair (f a) uses f applied to a, so f must be a
constructor of kind Type -> Type, and Pair itself becomes
(Type -> Type) -> Type -> Type:
data Pair f a = Pair (f a)
pairMaybe :: Pair Maybe Int
pairMaybe = Pair (Just 1)
pairList :: Pair List Int
pairList = Pair [1, 2]
firstOf :: Pair f a -> f a
firstOf (Pair x) = x
main :: IO ()
main = do
putStrLn (show (firstOf pairMaybe))
putStrLn (show (firstOf pairList))
Just 1
[1,2]
Nothing in the declaration said f’s kind; applying f to a is what fixes
it. The same inference applies to a class parameter: class Functor f gets
f :: Type -> Type because a method uses f a
(Higher-kinded types).
Kind errors
Applying a constructor to more arguments than its kind accepts is an error at the type, before any code is generated:
bad :: Maybe Int Bool
bad = Nothing
Main.mog:1:8: type error: type constructor `Maybe` applied to too many arguments
1 | bad :: Maybe Int Bool
| ^^^^^
Passing a type of the wrong kind is reported the same way, naming both sides:
Main.mog:4:18: type error: kind mismatch applying `OnlyColor`: expected `Color`, got `Type`
That example comes from DataKinds, where a type parameter is declared to be a value-level tag rather than an ordinary type.
Kind signatures
A declaration may state the kind of one of its parameters. This is what makes a phantom parameter a tag rather than a type:
data Color = Red | Blue
data Tagged (c :: Color) = Tagged
red :: Tagged 'Red
red = Tagged
Kind signatures are also how a constructor parameter is required to be a container:
data Nested (f :: Type -> Type) a = Nested (f (f a))
nested :: Nested Maybe Int
nested = Nested (Just (Just 1))
A class parameter may be annotated the same way, class Foo (t :: Type -> Type)
where …, when inference alone would not settle it. There are no annotations on
type synonyms: type Id (a :: Type) = a is a parse error in this release, and a
synonym’s parameters take the kind its right-hand side implies.
Which kinds exist
Type is the kind of ordinary types, also exported by Data.Kind. Three others are built in:
Nat— type-level natural numbers, written as literals (Port 80).Symbol— type-level strings, written as string literals (Label "hello").- the kind of any
datatype whose constructors are promoted, such asColorabove.
Both are covered in DataKinds.
A lowercase name in a parameter’s kind annotation is a kind variable:
data Proxy (a :: k) = Proxy declares a at any kind, and the variable is
generalised so one declaration serves several kinds:
data Proxy (a :: k) = Proxy
pInt :: Proxy Int
pInt = Proxy
pList :: Proxy List
pList = Proxy
Two parameters may share one kind variable (data Pair2 (a :: k) (b :: k)),
which requires both arguments to have the same kind. A named kind in the
annotation (Nat, Symbol, a promoted data kind) is not a variable: it fixes
that parameter to that kind.
Inspecting a kind
The REPL answers :kind (or :k) for a type or an expression’s type:
> :kind Maybe
Type -> Type
> :kind Either
Type -> Type -> Type
The same question at a compile error is answered by the message quoted above: kind errors always name the constructor and both kinds.
Limits
- Kind variables only in data declarations. A class parameter’s annotation
still defaults to
Type, andforallbinders take named kinds only. - No kind signatures on type synonyms.
- No promotion of functions or type constructors to the type level; only data constructors and the two literal kinds are promoted (DataKinds).
- No type-level computation. A GADT constructor may fix the index of the value it builds, and matching one refines the index (Data types § GADTs), but the kind system has no functions: kinds are checked for arity and named arguments, never evaluated.
Where next
- Higher-kinded types — classes over
f a, and what they buy. - Type-level data —
Nat,Symboland promoted constructors.