A Polymorphic Type System for Extensible Records and Variants
Total Page:16
File Type:pdf, Size:1020Kb
A Polymorphic Typ e System for Extensible Records and Variants Benedict R. Gaster and Mark P. Jones Technical rep ort NOTTCS-TR-96-3, November 1996 Department of Computer Science, University of Nottingham, University Park, Nottingham NG7 2RD, England. fbrg,[email protected] Abstract b oard, and another indicating a mouse click at a par- ticular p oint on the screen: Records and variants provide exible ways to construct Event = Char + Point : datatyp es, but the restrictions imp osed by practical typ e systems can prevent them from b eing used in ex- These de nitions are adequate, but they are not par- ible ways. These limitations are often the result of con- ticularly easy to work with in practice. For example, it cerns ab out eciency or typ e inference, or of the di- is easy to confuse datatyp e comp onents when they are culty in providing accurate typ es for key op erations. accessed by their p osition within a pro duct or sum, and This pap er describ es a new typ e system that reme- programs written in this way can b e hard to maintain. dies these problems: it supp orts extensible records and To avoid these problems, many programming lan- variants, with a full complement of p olymorphic op era- guages allow the comp onents of pro ducts, and the al- tions on each; and it o ers an e ective type inference al- ternatives of sums, to b e identi ed using names drawn gorithm and a simple compilation metho d. It is a prac- from some given set of labels. Lab elled pro ducts are tical system that can b e understo o d and implemented more commonly known as records or structs, while la- as a natural extension of languages like Standard ML b elled sums are p erhaps b etter known as variants or and Haskell. In an area that has already received a unions. For example, the Date and Event datatyp es great deal of attention from the research community, describ ed ab ove might b e de ned more attractively as: the typ e system describ ed here is the rst to combine all of these features in a practical framework. Date = Rec fjday :Int ; month : Int ; year :Int jg One imp ortant asp ect of this work is the emphasis Event = Var fjkey :Char ; mouse : Point jg: that it places on the use of rows in the construction of This notation captures a common feature in the con- record and variant typ es. As a result, with only small struction of record and variant types, using rows of the extensions of the core typ e system, we are able to intro- form fjl : ;:::; l : jg to describ e mappings that as- duce new, p owerful op erations on records using features 1 1 n n so ciate a type with each of the (distinct) lab els l . such as row p olymorphism and rst-class lab els. i i Record types are obtained by preceding rows with the symb ol Rec . Variant types are constructed using Var . 1 Intro duction For example, if r = fjl : ;:::; l : jg and e ,::: ,e have 1 1 n n 1 n types ,::: , , resp ectively, then we can form a record 1 n Pro ducts and sums play fundamental roles in the con- (l = e ;:::; l = e ) of type Rec r , or distinct vari- 1 1 n n struction of datatyp es: pro ducts describ e grouping of ants, hl = e i;:::; hl = e i, each of type Var r . Thus, 1 1 n n data items, while sums express choices b etween alter- 0 0 (day = 25; month = 12; year = 1996) and hkey = a i natives. For example, we might represent a date as a represent values of type Date and Event , resp ectively. pro duct, with three integer comp onents sp ecifying the day, month, and year: 1.1 Polymorphism and extensibility Date = Int Int Int : Unfortunately, practical languages are often less exi- ble in the op erations that they provide to manipulate For a simple example of a sum, consider the type of records and variants. For example, many languages| input events to a window system, with one alternative from C to Standard ML (SML)|will only allow the pro- indicating that a character has b een entered on the key- grammer to select the l comp onent, r :l , from a record r 1 selection op erator ( if the typ e of r is uniquely determined at compile-time . :l ) has type: These languages do not supp ort polymorphic op erations 8 :8r :Rec fjl : j r jg ! : on records|such as a general selector function ( :l ) that will extract a value from any record that has an l eld. However, the op erations and types in Wand's system A further weakness in many of these languages is that are unchecked; for example, extending a row with an l they provide no real supp ort for extensibility; there are eld may either add a completely new eld, or replace no general op erators for adding a eld, removing a eld, an existing eld lab elled with l . As a result, some pro- renaming a eld, or replacing a eld (p ossibly with a grams do not have principal types [27]. value of a di erent typ e) in a record value. There have b een many previous attempts to design Flags: Remy has developed a exible treatment for typ e systems for records and variants that supp ort p oly- extensible records and variants in a natural extension of morphism and extensibility. Such work is imp ortant, ML [23]. A key feature of his system is the use of ags not just in its own right, but also in its application to to enco de b oth p ositive and negative information|that the study of ob ject-oriented or database programming is, to indicate which elds must b e present, and which languages where these facilities seem particularly use- must b e absent. Again, a concept of row variables is ful. We will summarize the key features of some of these used to deal with other elds whose presence or absence earlier systems here b efore describing our own prop osal. is not signi cant in a particular situation. For example, the selection op erator has type: Subtyping: Subtyping is one of the most widely used techniques for building typ e systems for records and 8 :8r :Rec fjl : pre ( ) j r jg ! ; variants [3, 5, 4, 21]. We can de ne a subtyping relation by sp ecifying that a row r is a subrow of r , written 1 2 where pre ( ) is a ag indicating the presence of a eld r r , if r contains all the elds of r , and p ossibly 1 2 1 2 of type , and r is a row variable representing the rest more. The intuition here is that, for example, a record of the record. Intuitively, records in Remy's system of typ e Rec r could b e used in any context where a 1 are represented by tuples with a slot for each p ossible value of typ e Rec r is exp ected, and conversely, that a 2 lab el. The type system prevents access to unde ned variant of typ e Var r can b e substituted in any context 2 comp onents, but do es not lead directly to a simple and where a value of typ e Var r is required. In particular, 1 ecient implementation. :l ) can b e treated as a function the selection op erator ( of typ e: Predicates: Harp er and Pierce [8, 7] studied type 8 :8r fjl : jg:Rec r ! : systems for extensible records using predicates on types This op eration is implemented, at least conceptually, to capture information ab out presence or absence of by co ercing from Rec r to a known type|the singleton elds, and to restrict attention to checked op erations. Rec fjl : jg|and then extracting the required eld. One For example, writing r #r for the assertion that the 1 2 weakness of this approach is that information ab out the rows r and r have disjoint sets of lab els, the selection 1 2 other elds in the record is lost, so it is harder to de- op erator op erator ( :l ) has type: scrib e op erations like record extension. For example, 8 :8r :(r #fjl : jg) ) Rec (r k fjl : jg) ! ; observing that b ounded quanti cation is not by itself sucient, Cardelli and Mitchell [5] used an overriding where r k r is the row obtained by merging r and r , 1 2 1 2 op erator on typ es to overcome this problem. and is only de ned if r #r . Harp er and Pierce's work 1 2 do es not deal with variants, type inference, or compila- Row extension: Motivated by studies of ob ject- tion, and do es not provide an op erational interpretation oriented programming, Wand [26] intro duced the con- of predicates. However, their approach to record typing cept of row variables to allow incremental construction was one of the motivating examples in Jones' work on of record and variant typ es. For example, a record of quali ed types [11] where a general framework for type typ e Rec fjl : j r jg has all of the elds of a record of inference and compilation was developed, including a typ e Rec r , together with a eld l of type .