Java Generics: an Order-Theoretic Approach (Detailed Outline)
Total Page:16
File Type:pdf, Size:1020Kb
Java Generics: An Order-Theoretic Approach Moez A. AbdelGawad Informatics Research Institute, SRTA-City, Alexandria, Egypt [email protected] Abstract Introduction The addition of generic classes, generic interfaces and parameterized types (i.e., in- 1 Generics have been added to Java so as to increase the stantiations of generic classes ) to Java has signifi- expressiveness of its type system. Generics in Java, cantly enhanced the expressiveness of its type sys- however, include some features—such as Java wild- tem [22, 23, 16, 19, 34, 26]. Support for generics cards, F -bounded generics, and Java erasure—that in Java and other mainstream nominally-typed OOP have been hard to analyze and reason about so far, languages similar to it—such as C# [1], Scala [28], reflecting the fact that the mathematical modeling of C++ [2], and Kotlin [3]—has several drawbacks how- generics in Java and other similar nominally-typed ever. object-oriented programming (OOP) languages is a For example, the bounded existentials model of challenge. As a result, the type systems of main- Java wildcards, while accurate, is exceedingly com- stream nominally-typed OOP languages, which are plex [36, 35, 18, 17, 32, 33]. The notion of “capture built based on current models of generics, are overly conversion,” which is based on the bounded existen- complex, which hinders the progress of these type tials model of wildcards, renders the generic type sys- systems. tem of Java fully comprehensible only to the most 2 In this paper we present a detailed outline of a advanced of Java developers . new approach to modeling Java generics that uses Further, support of some features in Java gener- concepts and tools from order theory, and we report ics has a number of irregularities or “rough edges.” on our progress in developing this approach. Fun- These include type variables that can have upper damentally, we use the nominal subclassing relation F -bounds but cannot have lower bounds (let alone (as a partial order) together with some standard and lower F -bounds), wildcard type arguments that can novel order-theoretic tools to construct the generic be upper-bounded or lower-bounded but not both, nominal subtyping relation (as a partial order) and and Java erasure—a feature particular to Java and arXiv:1906.03937v1 [cs.PL] 23 May 2019 the containment relation between generic type argu- Java-based OO languages such as Scala and Kotlin— ments (a third partial order). We further analyze that is usually understood, basically, as being “out- the relation between these three ordering relations— side the type system.” which lie at the heart of mainstream generic OO type In this paper we present the outline of a model of systems—using order theoretic tools, and accordingly Java generics—one based on order theory and cate- we explore extensions of OO type systems suggested gory theory—that seemingly promises to help in over- by such analysis. In our approach we also make 1In this paper interfaces are treated as abstract classes. The use of some concepts and tools from category theory. term ‘classes’ here thus refers to Java classes and Java inter- We believe a combined order-theoretic and category- faces. Also, parameterized types are sometimes called refer- ence types, object types, class types, generic types, or just types. theoretic approach to modeling generics holds the 2 Check, for example, sections of the JLS—the Java Lan- keys to overcoming much of the adversity found when guage Specification—that specify crucial parts of its generic analyzing features of generic OO type systems. type system, e.g., [23, Sec 4.5 & Sec. 5.1.10]. 1 coming many if not most of the difficulties met when inition of ppp is the order-theoretic counterpart of modeling generics using current approaches. Our the definition of partial Cartesian product of graphs approach uses concepts from order theory such as presented in [10]. products of posets, intervals over posets, and induc- Second, we define a unary wildcards (also called tive/coinductive mathematical objects, and it also triangle) operation on posets, which we abbreviate uses in an elementary way some simple tools and con- wc and denote by △. The wc operation produces cepts from category theory such as algebras, coalge- as output a (roughly triangle-shaped) poset out of bras, adjunctions, monads and operads. an input bounded poset (i.e., a poset with top and As an application that demonstrates its value, our bottom elements). approach enables us to suggest how the type system Intuitively, the wc operation constructs three ele- of Java and similar nominally-typed OOP languages ments (modeling three type arguments) in the output can be simultaneously streamlined and extended to poset corresponding to each element (a type) in the support new features such as interval types, lower F - input poset. The three output elements are two wild- bounds, and co-free types. As a side-benefit, the ap- card type arguments and one non-wildcard type ar- proach also allows us to develop some mathematical gument. These three elements are then ordered (by a vocabulary (such as F -subtypes, F -supertypes, and relation called containment) based on the ordering in free types) for referring to some notions in generic the input poset (the subtyping relation). Namely, for nominally-typed OO type systems that have been an element (i.e., a type) T in the input poset, the wc mathematically unidentified so far. operation constructs the three elements (i.e., three type arguments) ‘? <: T’ (a synonym for the Java Constructing The Generic Java Subtyping Re- wildcard type argument ‘? extends T’), ‘T <: ?’ (a lation Nominally-typed OOP languages such as synonym for ‘? super T’), and the non-wildcard type Java, C#, C++, Scala and Kotlin are class-based. argument ‘T’, then it orders these three elements Two fundamental ordering relations in statically- by containment (where the non-wildcard type argu- typed class-based OOP languages are the subclassing ment ‘T’ is contained-in both wildcard type argu- (also called inheritance) relation between classes and ments ‘? <: T’ and ‘T <: ?’). The formal definition the subtyping relation between reference types. of the wildcards operation wc is presented in [9]. The first step in our order-theoretic approach to In OO programming languages, the subtyping re- Java generics is defining an abstract partial product lation between ground types (i.e., ones with no type operation on posets (i.e., ordered sets), which we call variables) is the basis for the full generic subtyping ppp (for partial poset product) and denote by ⋉. The relation (i.e., with type variables) [29]. Under some ppp operation constructs a product-like output poset simple simplifying assumptions, the subclassing (i.e., given two input posets and a subset of the first input inheritance) relation between Java classes can be used poset. to construct the subtyping relation between ground Intuitively, the ppp operation on two posets simply generic Java types, iteratively, using the ppp and wc pairs some elements of the first input poset with all operations. elements of the second input poset, then it adds the In particular, the subtyping relation is constructed, unpaired elements of the first poset to the resulting iteratively, as a the result of the ppp of two posets product. The ppp operation then orders the result- (relative to a subset of the first), where ing elements (i.e., pairs and non-pairs) based on the orderings in the two input posets.3 The formal def- • The first input poset to the ppp (⋉) operation is that of the subclassing relation (i.e., of classes 3For comparison, a standard (also called tensor or direct) product of two posets pairs all elements of the first poset with ordered by inheritance), all those of the second, then orders the resulting pairs based on the orderings in the two input posets [20] (which is the same Cartesian product—of the two directed graphs corresponding as the tensor product—sometimes, imprecisely, also called the to Hasse diagrams of the two input posets [24, Ch. 32]). 2 • The subset of the first poset (which the ppp op- relation between ground generic Java types we pre- eration is defined relative to) corresponds to the sented above. subset of generic classes (which is always a sub- Induction and coinduction are mathematical con- set of the first subclassing poset), and cepts that are best studied in the context of order theory and category theory. As such, during our • The second input poset to the ppp operation discussion of doubly F -bounded generics in particu- is that of type arguments (ordered by contain- lar, the order-theoretic approach naturally leads us to ment), which is produced by applying the unary consider viewing generic OO classes as generators (of wc (△) operation to the subtyping relation pro- types) over the poset of types ordered by the nom- duced by the previous iteration in the construc- inal subtyping relation (i.e., the subtyping relation tion process. between ground generic Java types), and, thus, to in- volve (co)inductive types and mutual (co)inductive The iterative process of constructing the subtyping types in the discussion. relation between ground generic types in Java was first presented in [6]. And a summary of the it- erative construction process together with recursive *Interval Types Intuitively, interval types gener- poset equations that formalize the process—and that alize wildcard types by generalizing wildcard type ar- use the ⋉ and △ operators—are presented in [9].4 guments to interval type arguments. Interval type ar- guments are type arguments that can have any two types as the upper and lower bounds of the type argu- Extending Java Generics and Extending Its ment, as long as the lower bound is a subtype of the Order-Theoretic Model As we just discussed, upper bound (so as to define, in an order-theoretic the order-theoretic approach to Java generics led to sense, an interval over the poset of the subtyping re- constructing the basis of the subtyping relation in lation).