Variant Parametric Types: a Flexible Subtyping Scheme for Generics

Variant Parametric Types: a Flexible Subtyping Scheme for Generics

Variant Parametric Types: A Flexible Subtyping Scheme for Generics ATSUSHI IGARASHI Kyoto University and MIRKO VIROLI Alma Mater Studiorum – Universit`adi Bologna We develop the mechanism of variant parametric types, as a means to enhance synergy between parametric and inclusion polymorphism in object-oriented programming languages. Variant para- metric types are used to control both subtyping between different instantiations of one generic class and the accessibility of their fields and methods. On one hand, one parametric class can be used to derive covariant types, contravariant types, and bivariant types (generally called variant parametric types), by attaching a variance annotation to a type argument. On the other hand, the type system prohibits certain method/field accesses according to variance annotations, when those accesses may otherwise make the program unsafe. By exploiting variant parametric types, a programmer can write generic code abstractions working on a wide range of parametric types in a safe way. For instance, a method that only reads the elements of a container of numbers can be easily modified so as to accept containers of integers, floating point numbers, or any subtype of the number type. Technical subtleties in typing for the proposed mechanism are addressed in terms of an intuitive correspondence between variant parametric types and bounded existential types. Then, for a rigorous argument of correctness of the proposed typing rules, we extend Featherweight GJ—an existing formal core calculus for Java with generics—with variant parametric types and prove type soundness. Categories and Subject Descriptors: D.3.1 [Programming Languages]: Formal Definitions and Theory; D.3.2 [Programming Languages]: Language Classifications—Object-oriented lan- guages; D.3.3 [Programming Languages]: Language Constructs and Features—Classes and objects; Polymorphism; F.3.3 [Logics and Meaning of Programs]: Studies of Program Con- structs—Object-oriented constructs; Type structure General Terms: Design, Languages, Theory Additional Key Words and Phrases: generic classes, Java, language design, language semantics, subtyping, variance This is a revised and extended version of the paper titled “On Variance-Based Subtyping for Para- metric Types” in Proceedings of the 16th European Conference on Object-Oriented Programming (ECOOP2002), Springer LNCS vol. 2374, pages 441–469, 2002. This work was supported in part by Grant-in-Aid for Scientific Research for Young Scientists (B) No. 13780203 and by Grant-in- Aid for Scientific Research on Priority Areas Research No. 13224013, both from MEXT of Japan (Igarashi), and from the Italian PRIN 2004 Project “Extensible Object Systems” (Viroli). Authors’ addresses: A. Igarashi, Graduate School of Informatics, Kyoto University, Yoshida- Honmachi, Sakyo-ku, Kyoto 606-8501, Japan; email: [email protected]; M. Viroli, DEIS, Universit`adi Bologna, via Venezia 52, 47023 Cesena (FC), Italy; email: [email protected]. Permission to make digital/hard copy of all or part of this material without fee for personal or classroom use provided that the copies are not made or distributed for profit or commercial advantage, the ACM copyright/server notice, the title of the publication, and its date appear, and notice is given that copying is by permission of the ACM, Inc. To copy otherwise, to republish, to post on servers, or to redistribute to lists requires prior specific permission and/or a fee. ACM Transactions on Programming Languages and Systems, Vol. TBD, No. TDB, Month Year, Pages 1–51. 2 · A. Igarashi and M. Viroli. 1. INTRODUCTION 1.1 Background The recent development of high-level constructs for object-oriented languages is witnessing renewed interest in the design, implementation, and applications of para- metric polymorphism—also known as generics. Such an interest has been growing mostly due to the emergence and development of the Java programming language. Initially, Java’s designers decided to avoid generic features, and to provide pro- grammers only with inclusion (or subtyping) polymorphism, supported by inher- itance. However, as Java was used to build large-scale applications, it became clear that the introduction of parametric polymorphism would have significantly enhanced programmers’ productivity, as well as the readability, maintainability, and safety of programs. Since then, a number of extensions were proposed ([Oder- sky and Wadler 1997; Bracha et al. 1998; Cartwright and Steele Jr. 1998; Vi- roli and Natali 2000; Viroli 2003; Myers et al. 1997; Agesen et al. 1997] to cite some): Sun Microsystems announced a call for proposals for adding generics to the Java programming language [Sun Microsystems 1998], and finally, Bracha, Oder- sky, Stoutamire, and Wadler’s GJ [Bracha et al. 1998] was chosen as the refer- ence implementation technique for the recent release of Java with generics (JDK 5.0, http://www.java.sun.com). Other than Java, more recently an extension of Microsoft’s .NET Common Language Runtime (CLR) with generics has been studied [Syme and Kennedy 2001]. Given this growing interest in generics, we believe that studying its language constructs will play a key role in increasing the expressiveness of mainstream pro- gramming languages such as Java and C#. In this article, we explore a technique to enhance the synergy between parametric and inclusion polymorphism, with the goal of increasing expressiveness and reuse in object-oriented languages supporting generics. 1.2 Previous Approaches to Subtyping for Generic Types In most of current mainstream object-oriented languages—such as Java, C++, and C#—inclusion polymorphism is supported only through inheritance: class C is considered a subtype of class D if and only if C is declared to be a subclass of D. Ex- tensions of these languages with generics usually adopt a subtyping scheme called pointwise subtyping, which is a straightforward extension of the monomorphic set- ting above: for instance, provided that class Stack<X> is a subclass of Vector<X> (where X is a type parameter) a parametric type Stack<String> is a subtype of Vector<String>; similarly for any type argument. Pointwise subtyping, however, never allows two instantiations of one generic class to be in the subtyping rela- tion. For example, Vector<Integer> and Vector<Number> are not related with pointwise subtyping. Historically, most of well-known attempts to introduce another subtyping scheme for generics were based on the notion of variance, which is used to define a subtype relation between different instantiations of the same generic class. Basically, a ACM Transactions on Programming Languages and Systems, Vol. TBD, No. TDB, Month Year. Variant Parametric Types · 3 generic class C<X> is said to be covariant with respect to X if S <: T implies C<S> <: C<T> (where <: denotes the subtyping relation), and conversely, C<X> is said to be contravariant with respect to X, if S <: T implies C<T> <: C<S>. Also, C<X> is said to be invariant when C<S> <: C<T> holds only if S = T. A familiar example of covariant parametric types is array types in Java, in which, String[] is a subtype of Object[] since String is a subtype of Object. Naive introduction of covariant subtyping, however, makes the type system unsound. For example, given a variable v of type Object[], it may be unsafe to update an element of v with a new Object, because v may actually point to an array of strings—but side-effecting features are not the only source of unsoundness, even purely functional GJ would be unsafe if covariant subtyping was allowed. As a result, Java arrays require every assignment to an array to be checked at run-time: if an incompatible value is to be assigned, an exception will be thrown. There have been a number of proposals [Cook 1989; America and van der Linden 1990; Bracha and Griswold 1993; Bracha 1996] for a sound type system for generics with variance, and most of them take a similar approach: in short, covariance and contravariance can be permitted under certain constraints on the occurrences of type variable X within C<X>’s signature. For example, for a generic class C<X> to be covariant, X must not appear in an argument type or in a type of a writable instance variable and so on. Conversely, in order for C<X> to be contravariant, X must not appear in a return type or in a type of a readable instance variable. Those restrictions are often understood in connection with readability and writability of instance variables. For example, consider a generic collection class whose element type is abstracted as a type parameter; typically, such a class can be covariant if it provides methods only to read elements, while it can be contravariant if it provides methods only to write elements. This approach, which works in principle, actually turns out to pose some diffi- culty in practice. Since variance can be obtained by prohibiting the declaration of possibly dangerous operations, programmers will face a tradeoff between a rich set of subtypes thanks to variance and a rich set of methods in a class. Even worse, this scheme for variance would decrease reusability: one may be forced to declare three very similar classes (or interfaces) for any kind of collection—an invariant one with methods for both reading and writing elements, a covariant one obtained by dropping methods for reading, and a contravariant one obtained by dropping methods for writing. 1.3 Our Approach Our approach here is to let programmers defer the decision about which variance is desirable until a class is employed, rather than when it is declared. To put it more concretely, for any type argument T, a parametric class C<X> may be used to derive the type C<T>, which is invariant as usual, but also the types C<+T> and C<-T>, which are respectively covariant and contravariant; in exchange for variance, certain (potentially unsafe) member accesses through C<+T> and C<-T> are forbidden.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    51 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