Kind (type Theory)
   HOME

TheInfoList



OR:

In the area of
mathematical logic Mathematical logic is the study of logic, formal logic within mathematics. Major subareas include model theory, proof theory, set theory, and recursion theory. Research in mathematical logic commonly addresses the mathematical properties of for ...
and
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
known as
type theory In mathematics, logic, and computer science, a type theory is the formal presentation of a specific type system, and in general type theory is the academic study of type systems. Some type theories serve as alternatives to set theory as a foundat ...
, a kind is the type of a
type constructor In the area of mathematical logic and computer science known as type theory, a type constructor is a feature of a typed formal language that builds new types from old ones. Basic types are considered to be built using nullary type constructors. Som ...
or, less commonly, the type of a higher-order type operator. A kind system is essentially a
simply typed lambda calculus The simply typed lambda calculus (\lambda^\to), a form of type theory, is a typed interpretation of the lambda calculus with only one type constructor (\to) that builds function types. It is the canonical and simplest example of a typed lambda cal ...
"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 set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
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 Arity () is the number of arguments or operands taken by a function, operation or relation in logic, mathematics, and computer science. In mathematics, arity may also be named ''rank'', but this word can have many other meanings in mathematics. In ...
specifier. Syntactically, it is natural to consider polymorphic types to be type constructors, thus non-polymorphic types to be
nullary Arity () is the number of arguments or operands taken by a function, operation or relation in logic, mathematics, and computer science. In mathematics, arity may also be named ''rank'', but this word can have many other meanings in mathematics. In ...
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. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
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 In programming languages and type theory, parametric polymorphism allows a single piece of code to be given a "generic" type, using variables in place of actual types, and then instantiated with particular types as needed. Parametrically polymorph ...
. Kinds appear, either explicitly or implicitly, in languages whose type systems account for parametric polymorphism in a programatically accessible way, such as
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
,
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 has pioneered a number of programming lang ...
and Scala.Generics of a Higher Kind
/ref>


Examples

* *, pronounced "type", is the kind of all
data type In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
s seen as
nullary Arity () is the number of arguments or operands taken by a function, operation or relation in logic, mathematics, and computer science. In mathematics, arity may also be named ''rank'', but this word can have many other meanings in mathematics. In ...
type constructors, and also called proper types in this context. This normally includes function types in
functional programming language In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that m ...
s. * * \rightarrow * is the kind of a unary
type constructor In the area of mathematical logic and computer science known as type theory, a type constructor is a feature of a typed formal language that builds new types from old ones. Basic types are considered to be built using nullary type constructors. Som ...
, e.g. of a
list type In computer science, a list or sequence is an abstract data type that represents a finite number of ordered values, where the same value may occur more than once. An instance of a list is a computer representation of the mathematical concept of ...
constructor. * * \rightarrow * \rightarrow * is the kind of a
binary Binary may refer to: Science and technology Mathematics * Binary number, a representation of numbers using only two digits (0 and 1) * Binary function, a function that takes two arguments * Binary operation, a mathematical operation that t ...
type constructor (via
currying In mathematics and computer science, currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument. For example, currying a function f that ...
), e.g. of a
pair type In programming languages and type theory, a product of ''types'' is another, compounded, type in a structure. The "operands" of the product are types, and the structure of a product type is determined by the fixed order of the operands in the prod ...
constructor, and also that of a
function type In computer science and mathematical logic, a function type (or arrow type or exponential) is the type of a variable or parameter to which a function has or can be assigned, or an argument or result type of a higher-order function taking or returni ...
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

(''Note'': Haskell documentation uses the same arrow for both function types and kinds.) The kind system of
Haskell 98 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 has pioneered a number of programming lang ...
Kinds - 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 set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
s. *k_1 \rightarrow k_2 is the kind of a unary
type constructor In the area of mathematical logic and computer science known as type theory, a type constructor is a feature of a typed formal language that builds new types from old ones. Basic types are considered to be built using nullary type constructors. Som ...
, 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 instance, ignoring
type class In computer science, a type class is a type system construct that supports ad hoc polymorphism. This is achieved by adding constraints to type variables in parametrically polymorphic types. Such a constraint typically involves a type class T and ...
es 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 instance 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 In computer science, partial application (or partial function application) refers to the process of fixing a number of arguments to a function, producing another function of smaller arity. Given a function f \colon (X \times Y \times Z) \to N , ...
thanks to currying. This is how Haskell achieves parametric types. For instance, 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 Loat or LOAT may refer to: People * Lily Loat (1880–1958), British anti-vaccination activist * William Leonard Stevenson Loat (1871–1932), British archaeologist, naturalist, and collector Other * Trausdorf Airport (ICAO: LOAT), a former pu ...
/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 ordered list (sequence) of elements. An -tuple is a sequence (or ordered list) of elements, where is a non-negative integer. There is only one 0-tuple, referred to as ''the empty tuple''. An -tuple is defi ...
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 kind Polymorphism, polymorphic, polymorph, polymorphous, or polymorphy may refer to: Computing * Polymorphism (computer science), the ability in programming to present the same programming interface for differing underlying forms * Ad hoc polymorphism ...
s. This is in contrast to
parametric polymorphism In programming languages and type theory, parametric polymorphism allows a single piece of code to be given a "generic" type, using variables in place of actual types, and then instantiated with particular types as needed. Parametrically polymorph ...
on types, which is supported in Haskell. For instance, in the following 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 the following 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 instance: 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. 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