Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Dependent type

From Wikipedia, the free encyclopedia
Type whose definition depends on a value
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 integern{\displaystyle n} may return an array of lengthn{\displaystyle n}, 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]

History

[edit]

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.

Formal definition

[edit]

Loosely speaking, dependent types are similar to the type of anindexed family of sets. More formally, given a typeA:U{\displaystyle A:{\mathcal {U}}} in a universe of typesU{\displaystyle {\mathcal {U}}}, one may have afamily of typesB:AU{\displaystyle B:A\to {\mathcal {U}}}, which assigns to each terma:A{\displaystyle a:A} a typeB(a):U{\displaystyle B(a):{\mathcal {U}}}. We say that the typeB(a){\displaystyle B(a)} varies witha{\displaystyle a}.

Π type

[edit]

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 typesB:AU{\displaystyle B:A\to {\mathcal {U}}} we may construct the type of dependent functionsx:AB(x){\textstyle \prod _{x:A}B(x)}, whose terms are functions that take a terma:A{\displaystyle a:A} and return a term inB(a){\displaystyle B(a)}. For this example, the dependent function type is typically written asx:AB(x){\textstyle \prod _{x:A}B(x)} or(x:A)B(x){\textstyle \prod {(x:A)}B(x)}.

IfB:AU{\displaystyle B:A\to {\mathcal {U}}} is a constant function, the corresponding dependent product type is equivalent to an ordinaryfunction type. That is,x:AB{\textstyle \prod _{x:A}B} is judgmentally equal toAB{\displaystyle A\to B} whenB{\displaystyle B} does not depend onx{\displaystyle x}.

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 writeVec(R,n){\displaystyle \operatorname {Vec} (\mathbb {R} ,n)} forn-tuples ofreal numbers, thenn:NVec(R,n){\textstyle \prod _{n:\mathbb {N} }\operatorname {Vec} (\mathbb {R} ,n)} 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.n:NR{\textstyle \prod _{n:\mathbb {N} }{\mathbb {R} }} is the type of functions from natural numbers to the real numbers, which is written asNR{\displaystyle \mathbb {N} \to \mathbb {R} } in typed lambda calculus.

For a more concrete example, takingA{\displaystyle A} to be the type of unsigned integers from 0 to 255 (the ones that fit into 8 bits or 1 byte) andB(a)=Xa{\displaystyle B(a)=X_{a}} fora:A{\displaystyle a:A}, thenx:AB(x){\textstyle \prod _{x:A}B(x)} devolves into the product ofX0×X1×X2××X253×X254×X255{\displaystyle X_{0}\times X_{1}\times X_{2}\times \ldots \times X_{253}\times X_{254}\times X_{255}}.

Σ type

[edit]

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 typesU{\displaystyle {\mathcal {U}}}, there is a typeA:U{\displaystyle A:{\mathcal {U}}} and a family of typesB:AU{\displaystyle B:A\to {\mathcal {U}}}, then there is a dependent pair typex:AB(x){\textstyle \sum _{x:A}B(x)}. (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(a,b):x:AB(x),{\textstyle (a,b):\sum _{x:A}B(x),} thena:A{\displaystyle a:A} andb:B(a){\displaystyle b:B(a)}. IfB{\displaystyle B} is a constant function, then the dependent pair type becomes (is judgementally equal to) theproduct type, that is, an ordinary Cartesian productA×B{\displaystyle A\times B}.[4]

For a more concrete example, takingA{\displaystyle A} to again be type of unsigned integers from 0 to 255, andB(a){\displaystyle B(a)} to again be equal toXa{\displaystyle X_{a}} for 256 more arbitraryXa{\displaystyle X_{a}}, thenx:AB(x){\textstyle \sum _{x:A}B(x)} devolves into the sumX0+X1+X2++X253+X254+X255{\displaystyle X_{0}+X_{1}+X_{2}+\ldots +X_{253}+X_{254}+X_{255}}.

Example as existential quantification

[edit]

LetA:U{\displaystyle A:{\mathcal {U}}} be some type, and letB:AU{\displaystyle B:A\to {\mathcal {U}}}. By the Curry–Howard correspondence,B{\displaystyle B} can be interpreted as a logicalpredicate on terms ofA{\displaystyle A}. For a givena:A{\displaystyle a:A}, whether the typeB(a){\displaystyle B(a)} is inhabited indicates whethera{\displaystyle a} satisfies this predicate. The correspondence can be extended to existential quantification and dependent pairs: the propositionaAB(a){\displaystyle \exists {a}{\in }A\,B(a)} is trueif and only if the typea:AB(a){\textstyle \sum _{a:A}B(a)} is inhabited.

For example,m:N{\displaystyle m:\mathbb {N} } is less than or equal ton:N{\displaystyle n:\mathbb {N} } if and only if there exists another natural numberk:N{\displaystyle k:\mathbb {N} } such thatm+k=n{\displaystyle m+k=n}. In logic, this statement is codified by existential quantification:

mnkNm+k=n.{\displaystyle m\leq n\iff \exists {k}{\in }\mathbb {N} \,m+k=n.}

This proposition corresponds to the dependent pair type:

k:Nm+k=n.{\displaystyle \sum _{k:\mathbb {N} }m+k=n.}

That is, a proof of the statement thatm{\displaystyle m} is less than or equal ton{\displaystyle n} is a pair that contains both a non-negative numberk{\displaystyle k}, which is the difference betweenm{\displaystyle m} andn{\displaystyle n}, and a proof of the equalitym+k=n{\displaystyle m+k=n}.

Systems of the lambda cube

[edit]

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.

First order dependent type theory

[edit]

The systemλΠ{\displaystyle \lambda \Pi } 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.

Second order dependent type theory

[edit]

The systemλΠ2{\displaystyle \lambda \Pi 2} of second order dependent types is obtained fromλΠ{\displaystyle \lambda \Pi } by allowing quantification over type constructors. In this theory the dependent product operator subsumes both the{\displaystyle \to } operator of simply typed lambda calculus and the{\displaystyle \forall } binder ofSystem F.

Higher order dependently typed polymorphic lambda calculus

[edit]

The higher order systemλΠω{\displaystyle \lambda \Pi \omega } extendsλΠ2{\displaystyle \lambda \Pi 2} 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.

Simultaneous programming language and logic

[edit]

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.

Comparison of languages with dependent types

[edit]

See also:Proof assistant § Comparison
LanguageActively developedParadigm[a]TacticsProof termsTermination checkingTypes can depend on[b]UniversesProof irrelevanceProgram extractionExtraction erases irrelevant terms
AgdaYes[5]Purely functionalFew/limited[c]YesYes (optional)Any termYes (optional)[d]Proof-irrelevant arguments[7] Proof-irrelevant propositions[8]Haskell,JavaScriptYes[7]
ATSYes[9]Functional / imperativeNo[10]YesYesStatic terms[11]?YesYesYes
CayenneNoPurely functionalNoYesNoAny termNoNo??
Gallina
(Rocq (previously known asCoq))
Yes[12]Purely functionalYesYesYesAny termYes[e]Yes[13]Haskell,Scheme,OCamlYes
Dependent MLNo[f]??Yes?Natural numbers????
F*Yes[14]Functional and imperativeYes[15]YesYes (optional)Any pure termYesYesOCaml,F#, andCYes
GuruNo[16]Purely functional[17]hypjoin[18]Yes[17]YesAny termNoYesCarrawayYes
IdrisYes[19]Purely functional[20]Yes[21]YesYes (optional)Any termYesNoYesYes[21]
LeanYesPurely functionalYesYesYesAny termYesYesYesYes
MatitaYes[22]Purely functionalYesYesYesAny termYesYesOCamlYes
NuPRLYesPurely functionalYesYesYesAny termYes?Yes?
PVSYes?Yes???????
SageArchived 2020-11-09 at theWayback MachineNo[g]Purely functionalNoNoNo?No???
SPARKYes[23]ImperativeYes[24]Yes[25]Yes[26]Any term[h]??Ada andC[27]Yes[28]
TwelfYesLogic programming?YesYes (optional)Any (LF) termNoNo??
  1. ^This refers to thecore language, not to any tactic (theorem provingprocedure) or code generation sublanguage.
  2. ^Subject to semantic constraints, such as universe constraints
  3. ^Ring solver[6]
  4. ^Optional universes, optional universe polymorphism, and optional explicitly specified universes
  5. ^Universes, automatically inferred universe constraints (not the same as Agda's universe polymorphism) and optional explicit printing of universe constraints
  6. ^Has been superseded by ATS
  7. ^Last Sage paper and last code snapshot are both dated 2006
  8. ^Static_Predicate for restricted terms, Dynamic_Predicate for Assert-like checking of any term in type cast

See also

[edit]

References

[edit]
  1. ^Hofmann, Martin (1995),Extensional concepts in intensional type theory(PDF)
  2. ^Sørensen, Morten Heine B.; Urzyczyn, Pawel (1998),Lectures on the Curry-Howard Isomorphism,CiteSeerX 10.1.1.17.7385
  3. ^Bove, Ana; Dybjer, Peter (2008).Dependent Types at Work(PDF) (Report). Chalmers University of Technology.
  4. ^abcAltenkirch, Thorsten; Danielsson, Nils Anders; Löh, Andres; Oury, Nicolas (2010)."ΠΣ: Dependent Types without the Sugar"(PDF). In Blume, Matthias; Kobayashi, Naoki; Vidal, Germán (eds.).Functional and Logic Programming, 10th International Symposium, FLOPS 2010, Sendai, Japan, April 19-21, 2010. Proceedings. Lecture Notes in Computer Science. Vol. 6009. Springer. pp. 40–55.doi:10.1007/978-3-642-12251-4_5.
  5. ^"Agda download page".
  6. ^"Agda Ring Solver".
  7. ^ab"Announce: Agda 2.2.8". Archived fromthe original on 2011-07-18. Retrieved2010-09-28.
  8. ^"Agda 2.6.0 changelog".
  9. ^"ATS2 downloads".
  10. ^"email from ATS inventor Hongwei Xi".
  11. ^Xi, Hongwei (March 2017)."Applied Type System: An Approach to Practical Programming with Theorem-Proving"(PDF).arXiv:1703.08683.
  12. ^"Coq changes in Subversion repository".
  13. ^"Introduction of SProp in Coq 8.10".
  14. ^"F* changes on GitHub".GitHub.
  15. ^"F* v0.9.5.0 release notes on GitHub".GitHub.
  16. ^"Guru SVN".
  17. ^abAaron Stump (6 April 2009)."Verified Programming in Guru"(PDF). Archived fromthe original(PDF) on 29 December 2009. Retrieved28 September 2010.
  18. ^Petcher, Adam (May 2008).Deciding Joinability Modulo Ground Equations in Operational Type Theory(PDF) (MSc). Washington University. Retrieved14 October 2010.
  19. ^"Idris git repository".GitHub. 17 May 2022.
  20. ^Brady, Edwin."Idris, a language with dependent types — extended abstract"(PDF).CiteSeerX 10.1.1.150.9442.
  21. ^abBrady, Edwin."How does Idris compare to other dependently-typed programming languages?".
  22. ^"Matita SVN". Archived fromthe original on 2006-05-08. Retrieved2010-09-29.
  23. ^"SPARK installation using ALIRE".
  24. ^"§3.2.4 Subtype Predicates".Ada Reference Manual (2012 ed.).
  25. ^"5.11.6. SPARK Lemma Library".SPARK User's Guide (25.0 ed.).
  26. ^"5.2.8. Contracts for Termination".SPARK User's Guide (25.0 ed.).
  27. ^"1.2. Calling and Using CCG".GNAT Pro Common Code Generator Users's Guide Supplement (25.0 ed.).
  28. ^"Compiling with a non-SPARK Aware Compiler".SPARK User's Guide (25.0 ed.).

Further reading

[edit]

External links

[edit]
Mathematical logic
Set theory
Type theory
Category theory
Retrieved from "https://en.wikipedia.org/w/index.php?title=Dependent_type&oldid=1305046478"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp