| Type systems |
|---|
| General concepts |
| Major categories |
| Minor categories |
Incomputer science andlogic, adependent type is a type whose definition depends on a value. It is an overlapping feature oftype theory andtype systems. Inintuitionistic type theory, dependent types are used to encode logic'squantifiers like "for all" and "there exists". Infunctional programminglanguages likeAgda,ATS,Rocq (previously known asCoq),F*,Epigram,Idris, andLean, dependent types help reduce bugs by enabling the programmer to assign types that further restrain the set of possible implementations.
Two common examples of dependent types aredependent functions anddependent pairs. The return type of a dependent function may depend on thevalue (not just type) of one of its arguments. For instance, a function that takes a positive integer may return an array of length, where the array length is part of the type of the array. (Note that this is different frompolymorphism andgeneric programming, both of which include the type as an argument.) A dependent pair may have a second value, the type of which depends on the first value. Sticking with the array example, a dependent pair may be used to pair an array with its length in a type-safe way.
Dependent types add complexity to a type system. Deciding theequality of dependent types in a program may require computations. If arbitrary values are allowed in dependent types, then deciding type equality may involve deciding whether two arbitrary programs produce the same result; hence thedecidability oftype checking may depend on the given type theory's semantics of equality, that is, whether the type theory isintensional orextensional.[1]
In 1934,Haskell Curry noticed that the types used intyped lambda calculus, and in itscombinatory logic counterpart, followed the same pattern as axioms inpropositional logic. Going further, for every proof in the logic, there was a matching function (term) in the programming language. One of Curry's examples was the correspondence betweensimply typed lambda calculus andintuitionistic logic.[2]
Predicate logic is an extension of propositional logic, adding quantifiers.Howard andde Bruijn extended lambda calculus to match this more powerful logic by creating types for dependent functions, which correspond to "for all", and dependent pairs, which correspond to "there exists".[3]
Because of this, and other work by Howard, propositions-as-types is known as theCurry–Howard correspondence.
Loosely speaking, dependent types are similar to the type of anindexed family of sets. More formally, given a type in a universe of types, one may have afamily of types, which assigns to each term a type. We say that the type varies with.
A function whose type of return value varies with its argument (i.e. there is no fixedcodomain) is adependent function and the type of this function is calleddependent product type,pi-type (Π type) ordependent function type.[4] From a family of types we may construct the type of dependent functions, whose terms are functions that take a term and return a term in. For this example, the dependent function type is typically written as or.
If is a constant function, the corresponding dependent product type is equivalent to an ordinaryfunction type. That is, is judgmentally equal to when does not depend on.
The name 'Π-type' comes from the idea that these may be viewed as aCartesian product of types. Π-types can also be understood asmodels ofuniversal quantifiers.
For example, if we write forn-tuples ofreal numbers, then would be the type of a function which, given anatural numbern, returns a tuple of real numbers of sizen. The usual function space arises as a special case when the range type does not actually depend on the input. E.g. is the type of functions from natural numbers to the real numbers, which is written as in typed lambda calculus.
For a more concrete example, taking to be the type of unsigned integers from 0 to 255 (the ones that fit into 8 bits or 1 byte) and for, then devolves into the product of.
Thedual of the dependent product type is thedependent pair type,dependent sum type,sigma-type, or (confusingly)dependent product type.[4] Sigma-types can also be understood asexistential quantifiers. Continuing the above example, if, in the universe of types, there is a type and a family of types, then there is a dependent pair type. (The alternative notations are similar to that ofΠ types.)
The dependent pair type captures the idea of an ordered pair where the type of the second term is dependent on the value of the first. If then and. If is a constant function, then the dependent pair type becomes (is judgementally equal to) theproduct type, that is, an ordinary Cartesian product.[4]
For a more concrete example, taking to again be type of unsigned integers from 0 to 255, and to again be equal to for 256 more arbitrary, then devolves into the sum.
Let be some type, and let. By the Curry–Howard correspondence, can be interpreted as a logicalpredicate on terms of. For a given, whether the type is inhabited indicates whether satisfies this predicate. The correspondence can be extended to existential quantification and dependent pairs: the proposition is trueif and only if the type is inhabited.
For example, is less than or equal to if and only if there exists another natural number such that. In logic, this statement is codified by existential quantification:
This proposition corresponds to the dependent pair type:
That is, a proof of the statement that is less than or equal to is a pair that contains both a non-negative number, which is the difference between and, and a proof of the equality.
Henk Barendregt developed thelambda cube as a means of classifying type systems along three axes. The eight corners of the resulting cube-shaped diagram each correspond to a type system, withsimply typed lambda calculus in the least expressive corner, andcalculus of constructions in the most expressive. The three axes of the cube correspond to three different augmentations of the simply typed lambda calculus: the addition of dependent types, the addition of polymorphism, and the addition of higherkinded type constructors (functions from types to types, for example). The lambda cube is generalized further bypure type systems.
The system of pure first order dependent types, corresponding to the logical frameworkLF, is obtained by generalising the function space type of thesimply typed lambda calculus to the dependent product type.
The system of second order dependent types is obtained from by allowing quantification over type constructors. In this theory the dependent product operator subsumes both the operator of simply typed lambda calculus and the binder ofSystem F.
The higher order system extends to all four forms of abstraction from thelambda cube: functions from terms to terms, types to types, terms to types and types to terms. The system corresponds to thecalculus of constructions whose derivative, thecalculus of inductive constructions is the underlying system of Rocq.
The Curry–Howard correspondence implies that types can be constructed that express arbitrarily complex mathematical properties. If the user can supply aconstructive proof that a type isinhabited (i.e., that a value of that type exists) then a compiler can check the proof and convert it into executable computer code that computes the value by carrying out the construction. The proof checking feature makes dependently typed languages closely related toproof assistants. The code-generation aspect provides a powerful approach to formalprogram verification andproof-carrying code, since the code is derived directly from a mechanically verified mathematical proof.
| Language | Actively developed | Paradigm[a] | Tactics | Proof terms | Termination checking | Types can depend on[b] | Universes | Proof irrelevance | Program extraction | Extraction erases irrelevant terms |
|---|---|---|---|---|---|---|---|---|---|---|
| Agda | Yes[5] | Purely functional | Few/limited[c] | Yes | Yes (optional) | Any term | Yes (optional)[d] | Proof-irrelevant arguments[7] Proof-irrelevant propositions[8] | Haskell,JavaScript | Yes[7] |
| ATS | Yes[9] | Functional / imperative | No[10] | Yes | Yes | Static terms[11] | ? | Yes | Yes | Yes |
| Cayenne | No | Purely functional | No | Yes | No | Any term | No | No | ? | ? |
| Gallina (Rocq (previously known asCoq)) | Yes[12] | Purely functional | Yes | Yes | Yes | Any term | Yes[e] | Yes[13] | Haskell,Scheme,OCaml | Yes |
| Dependent ML | No[f] | ? | ? | Yes | ? | Natural numbers | ? | ? | ? | ? |
| F* | Yes[14] | Functional and imperative | Yes[15] | Yes | Yes (optional) | Any pure term | Yes | Yes | OCaml,F#, andC | Yes |
| Guru | No[16] | Purely functional[17] | hypjoin[18] | Yes[17] | Yes | Any term | No | Yes | Carraway | Yes |
| Idris | Yes[19] | Purely functional[20] | Yes[21] | Yes | Yes (optional) | Any term | Yes | No | Yes | Yes[21] |
| Lean | Yes | Purely functional | Yes | Yes | Yes | Any term | Yes | Yes | Yes | Yes |
| Matita | Yes[22] | Purely functional | Yes | Yes | Yes | Any term | Yes | Yes | OCaml | Yes |
| NuPRL | Yes | Purely functional | Yes | Yes | Yes | Any term | Yes | ? | Yes | ? |
| PVS | Yes | ? | Yes | ? | ? | ? | ? | ? | ? | ? |
| SageArchived 2020-11-09 at theWayback Machine | No[g] | Purely functional | No | No | No | ? | No | ? | ? | ? |
| SPARK | Yes[23] | Imperative | Yes[24] | Yes[25] | Yes[26] | Any term[h] | ? | ? | Ada andC[27] | Yes[28] |
| Twelf | Yes | Logic programming | ? | Yes | Yes (optional) | Any (LF) term | No | No | ? | ? |