F-Bounded Polymorphism for Object-Oriented Programming

F-Bounded Polymorphism for Object-Oriented Programming

F-Bounded Polymorphism for Object-Oriented Programming Peter Canning, William Cook, Walter Hill, Walter Oltho® Hewlett-Packard Laboratories P.O. Box 10490, Palo Alto, CA 94303-0971 John C. Mitchell Department of Computer Science Stanford University, Stanford, CA 94309 Abstract ing was identi¯ed as an important form of polymor- phism in object-oriented programs. This lead to the Bounded quanti¯cation was introduced by Cardelli and development of \bounded quanti¯cation" in [7]. If we Wegner as a means of typing functions that operate uni- view objects as elements of non-recursive record types, formly over all subtypes of a given type. They de¯ned then bounded quanti¯cation provides a useful form of a simple \object" model and used bounded quanti¯ca- polymorphism over objects. However, a more sophis- tion to type-check functions that make sense on all ob- ticated presentation of object-oriented constructs (as jects having a speci¯ed set of \attributes." A more re- in [5, 11, 10]) would allow objects that are elements alistic presentation of object-oriented languages would of recursively-de¯ned types. With recursive types, the allow objects that are elements of recursively-de¯ned Cardelli-Wegner form of bounded quanti¯cation is not types. In this context, bounded quanti¯cation no longer su±ciently expressive to meet its original goal. serves its intended purpose. It is easy to ¯nd func- F-bounded quanti¯cation is a natural extension of tions that makes sense on all objects having a speci- bounded quanti¯cation that seems particularly useful ¯ed set of methods, but which cannot be typed in the in connection with recursive types. The essential idea Cardelli-Wegner system. To provide a basis for typed may be illustrated by comparison with Cardelli-Wegner polymorphic functions in object-oriented languages, we bounded quanti¯cation. Using \⊆" for the subtype re- introduce F-bounded quanti¯cation. Some applications lation, a simple example of a bounded-quanti¯ed type of F-bounded quanti¯cation are presented and seman- is the type 8t ⊆ ¿: t ! t. This is the type of func- tic issues are discussed. Although our original moti- tions which map t to t, for every subtype t of ¿. In vation was to type polymorphic functions over objects, a setting where all structurally similar objects belong F-bounded quanti¯cation is a general form of polymor- to subtypes of a given type, many useful polymorphic phism that seems useful whenever recursive type de¯n- functions will have bounded quanti¯ed types. For ex- itions and subtyping are used. ample, if we type an object by listing its methods and their types, an object with a print method may have type f:::; print: void ! string;:::g, indicating that the 1 Introduction method print produces a print representation of the object. In the view of subtyping presented in [8], Although object-oriented programming has attracted every type of this form will be a subtype of the type increasing interest in recent years, the development fprint: void ! stringg of objects having only a print of polymorphic type systems for object-oriented lan- method. For example, guages has progressed slowly. One reason is that object- oriented languages are often described using terminol- fA: int ! void; print: void ! stringg ogy that sets them apart from functional languages. In ⊆ fprint: void ! stringg: addition, there has been a lack of formal models for object-oriented languages. As a result, it has been A function taking a list of printable objects, a string and di±cult to see how practical polymorphic type sys- returning an object whose print representation matches tems should be adapted for typing object-oriented con- the given string will have the bounded-quanti¯ed type structs. In Cardelli's seminal paper [8], record subtyp- 8t ⊆ fprint: stringg: list[t] ! string ! t. 1 The flexibility of bounded quanti¯cation is dramati- may be imposed on the quanti¯ed type variable. Thus cally reduced when objects belong to recursively-de¯ned Curtis' system subsumes our more modest proposal. types. For example, consider the type However, we believe that our form of quanti¯cation is both su±cient to type-check practically useful object- PartialOrder = f lesseq: PartialOrder ! Bool g. oriented programs, and more tractable. Some evidence for the naturality of F-bounded quanti¯cation is the Each object of this type has a method lesseq which may intriguing connection with F-coalgebras, the dual of a be applied to another object of the same type. We in- standard construction in the category-theoretic charac- tend the result of x:lesseq(y) to be true if x does not terization of type recursion [22]. exceed y in some ordering, and false otherwise. Using The next section surveys relevant background on lesseq, it should be possible to sort lists of PartialOrder strongly-typed object-oriented programming. Section 3 elements, for example. We may easily write a sort- illustrates in detail problems with previous techniques ing function with type 8t ⊆ PartialOrder: list[t] ! list[t]. for dealing with recursive object types. Section 4 intro- However, as we shall see later in the paper, object- duces F-bounded quanti¯cation and demonstrates that oriented versions of the usual partially-ordered types it solves the typing problems presented in the previous such as integers are not subtypes of PartialOrder. There- section. some speculative observations on the semantic fore, our generic sorting function cannot be used in the aspects of F-bounded quanti¯cation. Section 6 summa- way we might expect. To solve this problem and related rizes our contribution and indicates directions for future di±culties with other forms of recursive types we intro- research. duce a generalization of bounded quanti¯cation which we call F-bounded quanti¯cation, for want of better ter- minology. For the example at hand, consider the func- 2 Background tion F from types to types given by 2.1 Objects, Records and Recursive F[t] = f lesseq: t ! bool g. Types We may write a polymorphic sorting function of type A fundamental premise underlying most models of 8t ⊆ F[t]:list[t] ! list[t], where this function is de¯ned on object-oriented programming is that objects may be re- any type t such that t ⊆ F[t]. Since integer ⊆ F[integer], garded as record whose components are functions rep- as explained in Section 3.3, we may apply such a sorting resenting methods [8, 7, 25, 11, 5]. In this model, \mes- function to lists of integers or lists of any other type of sage sending" is implemented by simple component se- objects having lesseq methods. lection. One practical application of F-bounded quanti¯cation Record types are used to describe the protocols [14] is for understanding some problems in the type system or interfaces of objects. A record type is a map- of the Ei®el programming language [18]. Ei®el intro- ping of labels to types. A record consisting of labels duces a special type expression, like Current, to represent l1; : : : ; lj with associated values in types σ1; : : : ; σj has recursion at the type level. Like Current always repre- type fl1: σ1; : : : ; lj: σjg. The ¯elds of the record type sents the `current class': in a class P it simply stands describe messages together with the types of their ar- for P , but when this class is inherited by a subclass C, guments and return values. This view is adopted in the like Current in an inherited attribute is reinterpreted to programming languages Amber [6], Modula-3 [10], and stand for the subclass C. The subclasses of such a class TS [17]. are usefully understood as having interfaces that sat- Even the very simplest \textbook" examples isfy an F-bound. However, as mentioned above, there from object-oriented programming produce recursively- is no necessary subtype relation among types satisfying de¯ned record types [21, 1, 6, 16, 10]. For example, an F-bound. This analysis explains some insecurities in the type of a planar point with four methods is de¯ned the Ei®el type-system, which always allows subclasses recursively as follows. to act as if they are subtypes of their parents. Further Point = f details are given in [12], along with some suggestions for x : void ! Real, correcting Ei®el's problems using the results in this pa- y : void ! Real, per. The error in the Ei®el type system illustrates the move : Real £ Real ! Point, subtlety involved in designing flexible and sound type equal : Point ! Boolean systems for object-oriented languages. g Another type system with a generalized form of bounded quanti¯cation is presented by Curtis [13]. In The body of the recursive type is a record type indi- this system, arbitrary collections of subtype constraints cated by braces f:::g. The use of Point in the return 2 type of move indicates that the move method returns This means, informally, that if assuming s is a sub- an object that has the same type as the original point. type of t allows one to prove that σ is a subtype of ¿, Its use as the argument type of equal indicates that the then the recursive type Rec s: σ is a subtype of Rec t: ¿. equal method takes an argument with the same type. For example, the following type is a supertype of Point: In other words, equal is a `binary' operation on points, viz. for two points p and q, the expression p.equal(q) is Movable = Rec mv. f move : Real £ Real ! mv g meaningful. because the body of Point is a subtype of the body of A useful notation for recursively de¯ned types is the Movable, given the assumption pnt ⊆ mv: form Rec t:A .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    8 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us