Type variable
Encyclopedia
In type theory
and programming language
s, a type variable is a mathematical variable
ranging over types. Even in programming languages that allow mutable variable
s, a type variable remains an abstraction, in the sense that it does not correspond to some memory locations.
Programming language
s that support parametric polymorphism
make use of universally quantified type variables. Languages that support existential types make use of existentially quantified type variables. For example, the following OCaml code defines a polymorphic identity function
that has a universally quantified type, which is printed by the interpreter on the 2nd line:
# let id x = x ;;
val id : 'a -> 'a =
In mathematical notation, the type of the function
Type theory
In mathematics, logic and computer science, type theory is any of several formal systems that can serve as alternatives to naive set theory, or the study of such formalisms in general...
and programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
s, a type variable is a mathematical variable
Variable (mathematics)
In mathematics, a variable is a value that may change within the scope of a given problem or set of operations. In contrast, a constant is a value that remains unchanged, though often unknown or undetermined. The concepts of constants and variables are fundamental to many areas of mathematics and...
ranging over types. Even in programming languages that allow mutable variable
Variable (programming)
In computer programming, a variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents...
s, a type variable remains an abstraction, in the sense that it does not correspond to some memory locations.
Programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
s that support parametric polymorphism
Parametric polymorphism
In programming languages and type theory, parametric polymorphism is a way to make a language more expressive, while still maintaining full static type-safety. Using parametric polymorphism, a function or a data type can be written generically so that it can handle values identically without...
make use of universally quantified type variables. Languages that support existential types make use of existentially quantified type variables. For example, the following OCaml code defines a polymorphic identity function
Identity function
In mathematics, an identity function, also called identity map or identity transformation, is a function that always returns the same value that was used as its argument...
that has a universally quantified type, which is printed by the interpreter on the 2nd line:
# let id x = x ;;
val id : 'a -> 'a =
In mathematical notation, the type of the function
id
is , where a is a type variable.