HOME

TheInfoList



OR:

In the area of
mathematical logic Mathematical logic is the study of Logic#Formal logic, formal logic within mathematics. Major subareas include model theory, proof theory, set theory, and recursion theory (also known as computability theory). Research in mathematical logic com ...
and
computer science Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
known as
type theory In mathematics and theoretical computer science, a type theory is the formal presentation of a specific type system. Type theory is the academic study of type systems. Some type theories serve as alternatives to set theory as a foundation of ...
, a kind is the type of a type constructor or, less commonly, the type of a higher-order type operator ( type constructor). A kind system is essentially a
simply typed lambda calculus The simply typed lambda calculus (), a form of type theory, is a typed interpretation of the lambda calculus with only one type constructor () that builds function types. It is the canonical and simplest example of a typed lambda calculus. The ...
"one level up", endowed with a primitive type, denoted * and called "type", which is the kind of any
data type In computer science and computer programming, a data type (or simply type) is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these ...
which does not need any type parameters. A kind is sometimes confusingly described as the "type of a (data) type", but it is actually more of an
arity In logic, mathematics, and computer science, arity () is the number of arguments or operands taken by a function, operation or relation. In mathematics, arity may also be called rank, but this word can have many other meanings. In logic and ...
specifier. Syntactically, it is natural to consider polymorphic
data type In computer science and computer programming, a data type (or simply type) is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these ...
s to be type constructors, thus non-polymorphic types to be
nullary In logic Logic is the study of correct reasoning. It includes both formal and informal logic. Formal logic is the study of deductively valid inferences or logical truths. It examines how conclusions follow from premises based on the ...
type constructors. But all nullary constructors, thus all monomorphic types, have the same, simplest kind; namely *. Since higher-order type operators are uncommon in
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s, in most programming practice, kinds are used to distinguish between data types and the types of constructors which are used to implement parametric polymorphism. Kinds appear, either explicitly or implicitly, in languages whose type systems account for parametric polymorphism in a programmatically accessible way, such as C++,
Haskell Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
, and Scala.


Examples

* *, pronounced "type", is the kind of all
data type In computer science and computer programming, a data type (or simply type) is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these ...
s seen as
nullary In logic Logic is the study of correct reasoning. It includes both formal and informal logic. Formal logic is the study of deductively valid inferences or logical truths. It examines how conclusions follow from premises based on the ...
type constructors, and also called proper types in this context. This normally includes function types in
functional programming In computer science, functional programming is a programming paradigm where programs are constructed by Function application, applying and Function composition (computer science), composing Function (computer science), functions. It is a declarat ...
languages. * * \rightarrow * is the kind of a unary type constructor, e.g., of a list type constructor. * * \rightarrow * \rightarrow * is the kind of a binary type constructor (via currying), e.g., of a pair type constructor, and also that of a function type constructor (not to be confused with the result of its application, which itself is a function type, thus of kind *) * (* \rightarrow *) \rightarrow * is the kind of a higher-order type operator from unary type constructors to proper types.


Kinds in Haskell

Haskell documentation uses the same arrow for both function ''types'' and ''kinds''. The kind system of
Haskell Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
98Kinds - The Haskell 98 Report
/ref> includes exactly two kinds: **, pronounced "type" is the kind of all
data type In computer science and computer programming, a data type (or simply type) is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these ...
s. *k_1 \rightarrow k_2 is the kind of a unary type constructor, which takes a type of kind k_1 and produces a type of kind k_2. An inhabited type (as proper types are called in Haskell) is a type which has values. For example, ignoring type classes which complicate the picture, 4 is a value of type Int, while , 2, 3/code> is a value of type nt/code> (list of Ints). Therefore, Int and nt/code> have kind *, but so does any function type, for example Int -> Bool or even Int -> Int -> Bool. A type constructor takes one or more type arguments, and produces a data type when enough arguments are supplied, i.e. it supports partial application thanks to currying. This is how Haskell achieves parametric types. For example, the type [] (list) is a type constructor - it takes a single argument to specify the type of the elements of the list. Hence, nt/code> (list of Ints), loat/code> (list of Floats) and even #91;Int]/code> (list of lists of Ints) are valid applications of the [] type constructor. Therefore, [] is a type of kind * \rightarrow *. Because Int has kind *, applying [] to it results in nt/code>, of kind *. The 2-
tuple In mathematics, a tuple is a finite sequence or ''ordered list'' of numbers or, more generally, mathematical objects, which are called the ''elements'' of the tuple. An -tuple is a tuple of elements, where is a non-negative integer. There is o ...
constructor (,) has kind * \rightarrow * \rightarrow *, the 3-tuple constructor (,,) has kind * \rightarrow * \rightarrow * \rightarrow * and so on.


Kind inference

Standard Haskell does not allow polymorphic kinds, in contrast to parametric polymorphism on types, which Haskell supports. For example: data Tree z = Leaf , Fork (Tree z) (Tree z) the kind of z could be anything, including *, but also * \rightarrow * etc. Haskell by default will always infer kinds to be *, unless the type explicitly indicates otherwise (see below). Therefore the type checker will reject this use of Tree: type FunnyTree = Tree [] -- invalid because the kind of [], * \rightarrow * does not match the expected kind for z, which is always *. Higher-order type operators are allowed however. For example: data App unt z = Z (unt z) has kind (* \rightarrow *) \rightarrow * \rightarrow *, i.e. unt is expected to be a unary data constructor, which gets applied to its argument, which must be a type, and returns another type. Glasgow Haskell Compiler (GHC) has the extension PolyKinds, which, together with KindSignatures, allows polymorphic kinds. For example: data Tree (z :: k) = Leaf , Fork (Tree z) (Tree z) type FunnyTree = Tree [] -- OK Since GHC 8.0.1, types and kinds are merged.


See also

* System F-omega * Pure type system


References

* , chapter 29, "Type Operators and Kinding" {{DEFAULTSORT:Kind (Type Theory) Type theory Data types Articles with example Haskell code