Dependent Intersection: a New Way of Defining Records in Type Theory ∗
Total Page:16
File Type:pdf, Size:1020Kb
Dependent Intersection: A New Way of Defining Records in Type Theory ¤ Alexei Kopylov Department of Computer Science Cornell University Ithaca, NY 14853, USA Abstract T [x1; : : : ; xn] for expressions that may contain free vari- ables x1,..., xn (and probably some other free variables), Records and dependent records are a powerful tool and T [t1; : : : ; tn] for substitution terms ti’s for all free oc- for programming, representing mathematical concepts, and currences of xi’s). program verification. In the last decade several type sys- Martin-Lof’s¨ type theory has the following judgments: tems with records as primitive types were proposed. The A Type A is a well-formed type question is arose: whether it is possible to define record A = BA and B are (intentionally) equal types type in existent type theories using standard types without a 2 A a has type A introducing new primitives. a = b 2 A a and b are equal as elements of type A It was known that independent records can be defined in The NuPRL type theory also has subtyping relation. Al- type theories with dependent functions or intersection. On though it is not essential for our work, we should mention the other hand dependent records cannot be formed using that membership and subtyping in NuPRL are extensional. standard types. Hickey introduced a complex notion of very For example, A ⊆ B does not say anything about struc- dependent functions to represent dependent records. In the ture of these types, but only means that if x 2 A then current paper we extend Martin-Lof’s¨ type theory with a x 2 B. As a result the type checking and subtyping are simpler type constructor dependent intersection, i.e., the in- undecidable. On the other hand, type equality (A = B) is tersection of two types, where the second type may depend intensional. We will use A =e B for extensional equality: ∆ on elements of the first one (not to be confused with the in- A =e B = (A ⊆ B)&(B ⊆ A). tersection of a family of types). This new type constructor The NuPRL type theory has also an intersection type. allows us to define dependent records in a very simple way. The intersection of two types A and B is a new type con- It also allows us to define the set type constructor. taining elements that are both in A and B. For example, ¸x:x + 1 is an element of the type (Z ! Z) \ (N ! N). Two elements are considered to be equal as elements of the 1 Introduction type A \ B if they are equal in both types A and B. ¡ 1.1 Type Theory Example 1 Let A = N ! N and B = Z ! Z (where Z¡ is a type of negative integers). Let id be ¸x:x and abs We will use the NuPRL type theory [6], which is an ex- be ¸x:jxj. Then id and abs are both elements of the type tension of Martin-Lof’s¨ type theory [16]. Martin-Lof’s¨ type A\B. Although id and abs are equal as elements of the type theory allows dependent types. That is, type expression may N ! N (because these two functions do not differ on N), id ¡ contain free variables ranging over arbitrary types. For ex- and abs are different as elements of Z ! Z. Therefore, ample, we can form an expression T [x] = [0::x] which rep- id 6= abs 2 A \ B. resents an initial sequent of natural numbers. This expres- In Martin-Lof’s¨ type theory types are first-class objects. sion is a type when x 2 N. (Some notations: we will use There is the universe type U that contain types that were ¤This work was supported in part by the DoD Multidisciplinary Uni- formed without using of U. versity Research Initiative (MURI) program administered by the Office Our work is implemented in a setting of the NuPRL type of Naval Research (ONR) under Grant N00014-01-1-0765, the Defense Advanced Research Projects Agency (DARPA) under Grant F30602-98-2- theory, namely in the MetaPRL system [12, 13] . See the- 0198, and by NSF Grant CCR 0204193. ories itt disect and itt record in Logical Theories 1 in [13]. All proofs except the proof of the semantical The- treat the record type as a new primitive. In the current pa- orem 10 are machine-checked. We believe that most of our per we are interesting in the following natural question: is results could be adapted to any type theory that allows bi- it possible to express the notion of records in usual type the- nary intersection and dependent types. ories without record type as primitives? This question is especially interesting for pure mathematical proof systems. 1.2 Records As we saw records are a handy tool to represent algebraic structures. On the other hand records do not seem to be In general, records are tuples of labeled fields, where the basic mathematical concept that should be included in each field may have its own type. In dependent records the foundation of mathematics. Records should be rather (or more formally dependently typed records) the type of defined in terms of more abstract mathematical concepts. components may depend on values of the other components. It is known that it is possible to define independent Since we have the type of types U, values of record compo- records in a sufficient powerful type theory that has depen- nents may be types. This makes the notion of dependent dent functions [11] or intersection [21]. On the other hand, records very powerful. Dependent records may be used to there is no known way to form dependent records in stan- represent algebraic structures (such as groups) and modules dard Martin-Lof’s¨ type theory [4]. However, Hickey [11] in programming languages like SML or Haskell (see for ex- showed that dependent records can be formed in an exten- ample [3, 10]). sion of Martin-Lof’s¨ type theory. Namely, he introduced a new type of very dependent functions. This type is pow- Example 2 One can define the signature for ordered set as erful enough to express dependent records in a type the- a dependent record type: ory and provides a mathematical foundation of dependent records. Unfortunately the type of very dependent func- OrdSetSig = ft : U; less : t ! t ! Boolg tions is very complex itself. The rules and the semantics This definition can be understood as an algebraic structure probably is more complicated for this type than for depen- as well as an interface of a module in a programing lan- dent records. The question is whether there is a simpler way guage. to add dependent records to a type theory. In this paper we extend the NuPRL type theory with a Example 3 The proposition-as-type principle allows us to simpler and easier to understand primitive type constructor, add the property of ordered sets as a new component: dependent intersection. This is a natural generalization of the standard intersection introduced in [8] and [20]. Depen- OrdSet=ft:U; less:t ! t ! Bool; axm : Ord(t; less)g dent intersection is an intersection of two types, where the where Ord(t; less) is a predicate stating that less is a second type may depend on elements of the first one. This transitive irreflexive relation on t. Here axm is a new field type constructor is built by analogy to dependent products: that defines the axiom of the algebraic structure of ordered elements of dependent product are pairs where the type of sets (or specification of the module type OrdSet). the second component may depend on the first component. We will show that dependent intersection allows us to de- Example 4 In type theories with equality, manifested fine the record type in a very simple way. Our definition of fields ([15]) may be also represented as specification. records is extensionally equal to Hickey’s ones, but is far simpler. Moreover our constructors (unlike Hickey’s) allow IntOrdSetSig = ft:U; less:t!t!Bool; mnf:t=Zg us to extend record types. For example, having a definition of monoids we can define groups by extending this defini- is a signature where t is bound to be the type of integers. tion rather than repeating the definition of monoid. From a mathematical point of view the record type is similar to the product type. The essential difference is 1.3 The Set Type Constructor the subtyping property: we can extend a record type with new fields and get a subtype of the original record type. The NuPRL type theory has a primitive type constructor E.g. OrdSet and IntOrdSetSig defined above are sub- for subset types. By definition, the set type fx : T j P [x]g types of OrdSetSig. The subtyping property is important is a subtype of T , which contains only such elements x of in mathematics: we can apply all theorems about monoid’s T that satisfy property P [x] (see [6]). to smaller types such as groups. It is also essential in pro- graming for inheritance and abstractions. Example 5 The type of natural numbers is defined as N = Different type theories with records were proposed fn : Z j n ¸ 0g. Without set types we would have to define both for proof systems as well as for programming lan- N as n : Z £ (n ¸ 0). In this case we would not have the guages ([10, 15, 9, 3, 4, 19] and others). These systems subtyping property N ⊆ Z. 2 It turns out that dependent intersection can be also used Γ ` A Type Γ; x : A ` B[x] Type to define a set type.