Type theory

At the broadest level, type theory is the branch of mathematics and logic that first creates a hierarchy of types, then assigns each mathematical (and possibly other) entity to a type. Objects of a given type are built up from objects of the preceding type. Types in this sense are related to the metaphysical notion of type. Bertrand Russell invented type theory in response to his discovery that naive set theory was afflicted with Russell's paradox. Type theory features prominently in Whitehead and Russell's Principia Mathematica.

In programming language theory, a branch of computer science, type theory provides the formal basis for the design, analysis and study of type systems. Indeed, many computer scientists use the term type theory to refer to the formal study of type systems for programming languages, although some limit it to the study of more abstract formalisms such as typed λ-calculi.

Simple theory of types
The following system is Mendelson's (1997, 289–293) ST. The domain of quantification is partitioned into an ascending hierarchy of types, with all individuals assigned a type. Quantified variables range over only one type; hence the underlying logic is first order logic. ST is "simple" (relative to the type theory of Principia Mathematica) primarily because all members of the domain and codomain of any relation must be of the same type.

There is a lowest type, whose individuals have no members and are members of the second lowest type. Individuals of the lowest type correspond to the urelements of certain set theories. Each type has a next higher type, analogous to the notion of successor in Peano arithmetic. While ST is silent as to whether there is a maximal type, a transfinite number of types poses no difficulty. These facts, reminiscent of the Peano axioms, make it convenient and conventional to assign a natural number to each type, starting with 0 for the lowest type. But type theory does not require a prior definition of the naturals. The symbols peculiar to ST are primed variables and infix ∈. In any given formula, unprimed variables all have the same type, while primed variables (x’) range over the next higher type. The atomic formulas of ST are of two forms, x=y (identity) and y∈x ’. The infix symbol ∈ suggests the intended interpretation, set membership.

All variables appearing in the definition of identity and in the axioms Extensionality and Comprehension, range over individuals of one of two consecutive types. Only unprimed (primed) variables, ranging over the "lower" ("higher") type, can appear to the left (right) of '∈'. The first order formulation of ST rules out quantifying over types. Hence each pair of consecutive types requires its own axiom of Extensionality and of Comprehension, which is possible if Extensionality and Comprehension below are taken as axiom schemata "ranging over" types.


 * Identity, defined by x=y ↔ ∀z’ [x&isin;z’ &harr; y&isin;z’].


 * Extensionality. An axiom schema. ∀x[x&isin;y’ &harr; x&isin;z’] → y’=z’.


 * Let Φ(x) denote any first order formula containing the free variable x.


 * Comprehension. An axiom schema. ∃z’∀x[x&isin;z’ &harr; &Phi;(x)].


 * Remark. Any collection of elements of the same type may form an object of the next higher type. Comprehension is schematic with respect to Φ(x) as well as to types.


 * Infinity. There exists a nonempty binary relation R over the individuals of the lowest type, that is irreflexive, transitive, and strongly connected (∀x, y [xRy &or; yRx]).


 * Remark. Infinity is the only true axiom of ST and is entirely mathematical in nature. It asserts that R is a strict total order, with a domain identical to its codomain. If 0 is assigned to the lowest type, the type of R is 3. Infinity can be satisfied only if the (co)domain of R is infinite, thus forcing the existence of an infinite set. If relations are defined in terms of ordered pairs, this axiom requires a prior definition of ordered pair; the Kuratowski definition, adapted to ST, will do. The literature does not explain why the usual axiom of infinity (there exists an inductive set) of ZFC of other set theories could not be married to ST.

ST reveals how type theory can be made very similar to axiomatic set theory. Moreover, the more elaborate ontology of ST, grounded in what is now called the "iterative conception of set," makes for axiom (schemas) that are far simpler than those of conventional set theories, such as ZFC, with simpler ontologies. Set theories whose point of departure is type theory, but whose axioms, ontology, and terminology differ from the above, include New Foundations and Scott-Potter set theory.

Practical impact of type theory
The most obvious application of type theory is in constructing type checking algorithms in semantic analysis phase of compilers for programming languages.

Type theory is also widely in use in theories of semantics of natural language. The most common construction takes the basic types $$e$$ and $$t$$ for individuals and truth-values, respectively, and defines the set of types recursively as follows:   if $$a$$ and $$b$$ are types, then so is $$\langle a,b\rangle$$.  Nothing except the basic types, and what can be constructed from them by means of the previous clause are types.  A complex type $$\langle a,b\rangle$$ is the type of functions from entities of type $$a$$ to entities of type $$b$$. Thus one has types like $$\langle e,t\rangle$$ which are interpreted as elements of the set of functions from entities to truth-values, i.e. characteristic functions of sets if entities. An expression of type $$\langle\langle e,t\rangle,t\rangle$$ is a function from sets of entities to truth-values, i.e. a (characteristic function of a) set of sets. This latter type is standardly taken to be the type of natural language quantifiers, like  every boy or  nobody (Montague 1973, Barwise and Cooper 1981).

Type system
Definitions of type system vary, but the following one due to Benjamin C. Pierce roughly corresponds to the current consensus in the programming language theory community:

In other words, a type system divides program values into sets called types — this is called a type assignment — and makes certain program behaviors illegal on the basis of the types that are thus assigned. For example, a type system may classify the value "hello" as a string and the value 5 as a number, and prohibit the programmer from adding "hello" to 5 based on that type assignment. In this type system, the program

would be illegal. Hence, any program permitted by the type system would be provably free from the erroneous behavior of adding strings and numbers.

The design and implementation of type systems is a topic nearly as broad as the topic of programming languages itself. In fact, type theory proponents commonly proclaim that the design of type systems is the very essence of programming language design: "Design the type system correctly, and the language will design itself."