KUMI v5.0.0
Gorgeous Garnet
Loading...
Searching...
No Matches
Type Theoretic Foundations

Type theory studies type systems, the formal rules by which a language assigns types to its terms and builds new types from existing ones. KUMI implements one family of these constructions, the product types, and borrows from type theory the vocabulary with which its documentation describes them. Tuples and their projections are written in the usual mathematical notation, \( (x_1, \dots, x_n) \) and \( \pi_i \), and records in the notation of Pierce, \( \{l_1 = x_1, \dots, l_n = x_n\} \).


Origin

Bertrand Russell introduced a theory of types in 1908, the ramified theory, to avoid the paradoxes of naive set theory, and Alonzo Church simplified it in 1940 into the simple theory of types, on which the typed lambda calculi rest.

Two styles of typing coexist since: in the Church style, which the examples below follow, every term carries its type, whereas in the Curry style the terms are untyped and the type system assigns types to some of them. Under the Curry-Howard correspondence, the same construction admits a logical reading:

  • a type reads as a proposition,
  • a term of that type, a value or a program, reads as a proof of that proposition.

The Curry-Howard Correspondence

The Curry-Howard correspondence relates intuitionistic logic to the typed lambda calculi: each connective of intuitionistic propositional logic has a counterpart type former, and a proof of a formula corresponds to a term of the matching type. Classical principles such as the excluded middle or Peirce's law have no closed term in the simply typed lambda calculus with products, sums, and the empty type, and Griffin showed in 1990 that control operators such as call/cc receive the types of these principles.

The C++ column gives the nearest construct of the language, C++ having no empty type.

Logic name Logic notation Type notation Type name C++ approximation
Truth \( \top \) \( 1 \) Unit type std::monostate, kumi::unit
Falsity \( \bot \) \( 0 \) Empty type none
Implication \( A \to B \) \( A \to B \) Function type the function type B(A)
Negation \( \neg A \) \( A \to 0 \) Function to the empty type [[noreturn]] void f(A)
Conjunction \( A \land B \) \( A \times B \) Product type std::tuple<A, B>, kumi::tuple<A, B>
Disjunction \( A \lor B \) \( A + B \) Sum type std::variant<A, B>

Fundamentals

A type theory starts from base types and builds every other type from them. The examples below use two base types, nat, whose values are 0 and the iterated applications of the successor \( S : \mathrm{nat} \to \mathrm{nat} \), written 1, 2, and so on, and bool, whose values are the constants true and false. The atomic terms are these constants together with the variables.

A formal variable stands for an arbitrary term of a given type and carries no computation of its own. It is bound when an abstraction introduces it, as \( x \) is in \( \lambda x.\, x \), and free otherwise.

A typing judgment \( \Gamma \vdash t : T \) states that the term \( t \) has the type \( T \) under the context \( \Gamma \), which assigns a type to each free variable of \( t \), so that a constant needs an empty context and a free variable an assumption on its type:

\[ \vdash \mathrm{true} : \mathrm{bool} \qquad \vdash 42 : \mathrm{nat} \qquad x : \mathrm{bool} \vdash x : \mathrm{bool} \]

The type of the functions from \( \sigma \) to \( \tau \) is written \( \sigma \to \tau \). A function type has one argument, so that a function of two arguments is curried and takes them one at a time, and the arrow associates to the right by convention, which drops the parentheses of the type of the curried addition of two natural numbers:

\[ \mathrm{add} : \mathrm{nat} \to (\mathrm{nat} \to \mathrm{nat}) \]

The application of add to one argument yields a function of type \( \mathrm{nat} \to \mathrm{nat} \).

A lambda term \( \lambda x.\, t \) defines a new function by abstraction over the variable \( x \) in the term \( t \). It corresponds to the anonymous function of programming languages, of which the C++ lambda expression is one, and \( \lambda x.\, x \) is the identity function, of type \( T \to T \) for any type \( T \).


Type Cardinality

The cardinality of a set is the number of its elements, and the cardinality of a type, read as the set of its values, is the number of distinct values that an object of that type can hold. The type bool has the cardinality 2, its set of values being \( \{\mathrm{true}, \mathrm{false}\} \), whereas nat has an infinite cardinality.

For a finite type of cardinality \( n \), \( \log_2 n \) bits encode one value, an upper bound of the information that an object of the type carries, and the cardinality turns the constructions of types into arithmetic, by which the cardinality of a compound type follows from the cardinalities of its parts.


Correspondence with KUMI

A lambda term denotes a function, and a well-typed term denotes a value of its type. KUMI stores and decomposes the values of product types: kumi::tuple, kumi::record, and kumi::array hold such values and model the concept kumi::concepts::product_type.

Term Type KUMI
\( \lambda x.\, x \) \( T \to T \) a callable, which kumi::map applies to each element of a product type
\( (x_1, \dots, x_n) \) \( T_1 \times \dots \times T_n \) kumi::tuple, and kumi::array when all the \( T_i \) are equal
projection \( \pi_i \) \( T_1 \times \dots \times T_n \to T_i \) kumi::get at the index \( i - 1 \), which returns a reference to the component
Note
Pierce, Harper, Barendregt, and Wadler treat these notions, see the Bibliography.