arXiv:1906.03937v1 [cs.PL] 23 May 2019 nlzn etrso eei Otp systems. type OO generic the of when found features holds adversity analyzing the generics of much overcoming modeling to keys make to also category- approach and we theory.theoretic order-theoretic category combined approach from a tools our believe and We concepts In some of use suggested analysis. systems such type OO by of extensions explore accordingly we type and OO tools, generic theoretic mainstream order of analyze systems—using heart further the relations— at We ordering lie three which these order). between relation partial the third argu- (a and type generic order) ments between partial relation a containment generic (as the the relation construct subtyping to and nominal standard tools some order-theoretic with together novel Fun- relation order) partial subclassing approach. a nominal (as this the use developing we report in damentally, we progress uses and theory, our that order on generics from tools and modeling concepts to approach new type these of progress overly the systems. are hinders generics, main- which of of models complex, are current systems on which type based languages, built the OOP result, nominally-typed a stream a As is nominally-typed languages challenge. similar (OOP) other programming and object-oriented far, Java of so modeling in about mathematical generics the reason that and fact analyze the reflecting to hard been have wild- Java Java, as in cards, features—such Generics some include system. however, type the its increase to of as so expressiveness Java to added been have Generics Abstract nti ae epeetadtie uln fa of outline detailed a present we paper this In F buddgnrc,adJv erasure—that Java and generics, -bounded nomtc eerhIsiue RACt,Aeadi,E Alexandria, SRTA-City, Institute, Research Informatics aaGnrc:A re-hoei Approach Order-Theoretic An Generics: Java ozA AbdelGawad A. Moez [email protected] 1 eei nefcsadprmtrzdtps( types parameterized and interfaces generic Introduction e fJv ul opeesbeol otemost the to only developers Java comprehensible of fully advanced Java sys- type of generic “capture the tem existen- of renders bounded wildcards, notion of the The model on tials 33]. based 32, is which 17, com- conversion,” 18, exceedingly 35, is [36, accurate, plex while wildcards, Java [28], how- Scala drawbacks several generics [1], ever. [3]—has C# Kotlin for and as Support [2], it—such C++ to 26]. OOP similar nominally-typed 34, languages mainstream other sys- 19, and type Java 16, in its 23, of [22, expressiveness tem the enhanced cantly tnitoso eei classes generic of stantiations oyter—htseigypoie ohl nover- cate- in help and to promises theory seemingly order theory—that gory on based generics—one Java “out- being system.” as type basically, the side understood, usually Kotlin— and is and Scala Java that as to such languages both, particular OO Java-based feature not erasure—a but Java lower-bounded and or upper-bounded upper be have can lower edges.” that “rough variables or F type irregularities include of number These a has ics ae.As,prmtrzdtpsaesmtmscalled inte sometimes Java are and types classes types Java parameterized ence to Also, refers thus faces. here ‘classes’ term ug pcfiainta pcf rca at fisgener its of parts system, crucial type specify Specification—that guage bud u anthv oe ons(e alone (let bounds lower have cannot but -bounds o xml,tebuddeitnil oe of model existentials bounded the example, For nti ae epeetteotieo oe of model a of outline the present we paper this In gener- Java in features some of support Further, 2 1 hc,freape etoso h L—h aaLan- Java JLS—the the of sections example, for Check, nti ae nefcsaetetda btatcass Th classes. abstract as treated are interfaces paper this In F bud) idadtp ruet htcan that arguments type wildcard -bounds), , bettypes object e.g. 2,Sc45&Sc 5.1.10]. Sec. & 4.5 Sec [23, , h diino eei classes, generic of addition The , ls types class 2 , . eei types generic 1 gypt oJv a signifi- has Java to ) rjust or , i.e. types refer- in- , ic r- e . 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 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). Java using order-theoretic tools. We now illustrate Formally, with interval types, the Java subtyping the extrapolating value of the approach by suggesting relation can be constructed simply by substituting two extensions of Java generics that are inspired by the wildcards operation wc we discussed above with this approach, as well as offering new order-theoretic a unary operation that constructs intervals of an in- concepts and tools that can be useful in analyzing put poset. We call this operation on posets int, and generic OO type systems. denote it by m.5 In particular, we first suggest how wildcard types Intuitively, the unary int operation constructs an can be generalized to what we call interval types. element (an interval type argument) in the output Then we suggest how F -bounded generics (a.k.a., F - poset corresponding to each pair of elements (types) bounded polymorphism) can be generalized to what in the input (subtyping) poset where the first compo- we call doubly F -bounded generics. Both extensions nent of the pair is less than or equal (i.e., is a subtype) are suggested by the construction of the subtyping of the second component. All constructed elements

4 Formally, the poset of the subtyping relation S is the 5Formally, the poset of the subtyping relation S (with inter- (least) solution of the recursive poset equation val types) is the (least) solution of the recursive poset equation ⋉ ⋉ S = C Cg △ (S) (1) S = C Cg m (S) (2) where C is the subclassing poset and Cg is the subset of generic where C is the subclassing poset and Cg is the subset of generic classes of C. classes of C. Intuitively, Equation (1) can be read as saying that the △ Intuitively, Equation (2) can be read as saying that the m operator constructs wildcard type arguments (and the associ- operator constructs interval type arguments (and the associ- ated containment relation) from the subtyping relation S, then, ated containment relation) from the subtyping relation S, then, while preserving existing subtyping relations, the ⋉ operator while preserving existing subtyping relations, the ⋉ operator pairs the constructed type arguments with generic classes in pairs the constructed type arguments with generic classes in C to construct further types and further subtyping relations. C to construct further types and further subtyping relations.

3 are then ordered (also by the containment relation) are allowed to also have lower bounds, including based on the ordering in the input poset (i.e., the sub- lower F -bounds. Our investigations into dfbg led us, typing relation). Namely, for a pair of elements (i.e., among other conclusions, into making a distinction types) S and T in the input poset where S<:T (i.e., S between valid type arguments and admittable type is a subtype of T, thus defining an interval in the in- arguments, and between valid parameterized types put poset), the int operation constructs the element and admittable parameterized types. ‘[S,T]’, then it orders all such constructed elements To illustrate with an example, type Enum by containment (i.e., in the output poset the interval is an admittable Java parameterized type, but it is type argument ‘[S,T]’ is contained-in (⊑) the inter- not a valid type since the type argument to Enum, val type argument ‘[U,V]’ if and only if U<:S and namely Object, does not satisfy the declared bounds 6 T<:V in the input poset). The formal definition of on the type variable of class Enum. Hence, type int is presented in [11]. Object is an admittable type argument to generic Defining interval types allows immediately noting class Enum, but it is not a valid one. that, when viewed as functions (i.e., generators, or This example illustrates the simple definition of ad- type constructors), generic classes are not endofunc- mittable versus valid parameterized types and type tions (i.e., self-maps), since they do not map types arguments. All reference types are admittable type to types, but they rather map type intervals to types arguments. An admittable type argument passed (i.e., map interval type arguments to interval types). to a generic class (to form a parameterized type) is Further, we also note that generic classes are always valid if and only if it satisfies the bound(s) of the monotonic functions (also called covariant functors corresponding type variable in the class (i.e. is a in category theory parlance). This is because generic subtype of the upper bound and a supertype of the classes, as generators of types from interval type ar- lower bound). An admittable parameterized type is guments, always produce subtypes when they are pro- formed by a generic class instantiated with admit- vided with subintervals as type arguments. See [12] table type arguments. An admittable parameterized for more details. type is then a valid parameterized type if and only if all its type arguments are valid type arguments. *Doubly F -Bounded Generics Motivated by In Java, an intuitive, but somewhat inaccu- generalizing wildcard types to interval types, we next rate, set-theoretic way to think about invalid types define doubly F -bounded generics (dfbg, for short) (i.e., admittable-but-not-valid reference types, such as a generalization of standard F -bounded generics, as Enum) versus valid types (such as where, in dfbg, type variables of generic classes— List) is to consider valid types as denoting which are allowed to have upper F -bounds in Java— non-empty sets of objects (i.e., sets, that are, at the very least, inhabited by the trivial object null as well 6 The int operation typically produces more type arguments as “the bottom object” ⊥, corresponding to compu- than the wc operation does, since int produces a set of type arguments corresponding to all pairs of types in the subtyping tational divergence [4], in addition to possibly many relation where the first type is a subtype of the second (which, other proper non-trivial objects), while considering due to the existence of types Object and Null at the top and invalid types as denoting the empty set (of objects), bottom of the subtyping relation, contains all wildcard type i.e., the uninhabited set that does not contain null, arguments as a subset). (N.B.: If n = |P | is the cardinality of an input poset P , then |wc (P )| = 3n − 2 while |int (P )| = nor even ⊥. In this view, thus, admittable Java types O n2/2, i.e., is on the order of n2/2.) correspond to all (i.e., to empty or non-empty) sets Similar to the wc operation, the int operation orders interval of objects, while all invalid Java types are different type arguments by the containment relation (corresponding to type expressions that, in spite of their (syntactic) dif- the containment relation between intervals of a poset or be- ferences, semantically denote the same empty set of tween paths of a graph). As such, interval types are strictly 7 more expressive than wildcard types (if there is at least one objects. More details on doubly F -bounded generics generic class in the subclassing relation, and is equally expres- sive otherwise). 7For the sake of completeness, examples of Java type ex-

4 are presented in [7]. F -supertypes and discuss their relevance to OO type It is worthy to note that, inspired by functions in systems [12]. In summary, if F is a generic class, analysis (i.e., functions over real numbers R), in the then the F -subtypes of class F are all the parame- investigation of dfbg we (attempt to) use a coinductive terized types that are subtypes of instantiations of logical argument to prove that checking the validity of F with themselves (i.e., any reference type Ty is an some type arguments inside the bounds-declarations F -subtype of class F if and only if Ty <: F). of generic classes is unnecessary. As such, we con- Dually, the F -supertypes of F are all the parame- clude that using admittable type arguments in such a terized types that are supertypes of instantiations of context is allowed. See [7] for details. F with themselves (As such, type Object in Java, for example, is an F -supertype while type Null is an F F *(Co)Inductive Types, F -Subtypes, F -Super- -subtype for all generic classes .) types, and Mutual (Co)Induction In logic, In our experience, F -subtypes and F -supertypes coinductive reasoning can, intuitively, be summarized are useful, mainly, when discussing and analyzing as asserting that a statement is proven to be true if bounded type variables in generic OO type systems there is no (finite or “good”) reason for the statement (e.g., as we do for dfbg). The two notions, for exam- to not hold [25, 13]. Such a statement is then said ple, establish a direct relation and correspondence to be coinductively-justified, or ‘true by coinduction’. between the denoted types and F -algebras and F - Showing the value of the order-theoretic approach yet coalgebras (sometimes also called just algebras and again, order theory (which lattice theory is a subdis- coalgebras) in category theory, and also between the cipline of) is regarded as the most natural context for denoted types and inductive/coinductive mathemat- studying inductive and coinductive mathematical ob- ical objects (i.e., sets, points, predicates, and struc- jects and the associated logical proof principles (see, tural types) in each of set theory, order theory, first- e.g., [29, Ch. 21]).8 order logic, and structural type theory (which is used to model functional programming languages) [8, Tab. Among a few other motivating factors, Tate et 9 al.’s conclusion that Java wildcards are coinductive 1 & 2]. bounded existentials [33] and the use of a coinduc- Noting that classes in Java programs, including tive argument during investigating dfbg, led us to generic classes, are frequently defined mutually-recur- make note of and consider, in more depth and in more sively (e.g., assuming the absence of primitive types generality, the coinductive nature of nominally-typed in Java, the definitions of the fundamental classes OO type systems [12]. Another motivation for study- Object and Boolean are mutually dependent on each ing coinduction is to allow for analyzing the subtyp- other), we were also led to define an order-theoretic ing relation in Java from a coinductive point of view, notion of mutual coinduction to enable studying mu- so as to allow, for example, for subtyping relations tually recursive definitions [14]. Mutually-dependent that are not constructed finitely [12]. definitions are not only frequent in OO programs In the course of studying induction and coinduc- (i.e., in defining methods and classes), but they show tion, we define new notions such as F -subtypes and up also in the definition of OO programming lan- guages themselves (e.g., as in the definition of the pressions that are not admittable (reference) types (let alone Java subtyping relation, where the subtyping relation valid types) are the type expressions int and boolean, since between parameterized types depends on the contain- int and boolean are not reference types, and also type ex- pressions similar to String, since class String is not ment relation between interval type arguments, and, a generic class and thus cannot be applied to or instantiated reciprocally, the containment relation between inter- with any type arguments. (Like all zero-ary type constructors, which take no type arguments, class String constructs only 9In order theory in particular, F -supertypes correspond one type, namely the homonymous type String.) to pre-fixed points, while F -subtypes correspond to post-fixed 8See [8] for a more in depth discussion and comparison of points. In set theory and structural type theory, they cor- formulations of induction and coinduction in different mathe- respond to inductive sets/types and coinductive sets/types, matical disciplines. respectively.

5 val type arguments depends on the subtyping rela- As for any adjunction, to properly define an ad- tion between parameterized types. Also, there ex- junction the two maps of JEA (from the subclassing ists a mutual dependency between the definitions of poset to the subtyping poset, and vice versa) have to valid and admittable types and type arguments). We work in tandem to satisfy a preservation condition. believe the order-theoretic notion of mutual coinduc- This condition indeed holds for Java generics, making tion, as we define it, can be useful in analyzing OO Java erasure and free types two adjoints (parts) of an type systems and also in reasoning about OO soft- adjunction, hence deserving their names as adjoints ware (and imperative software). See [14] for further in an adjunction. In particular, if E is the erasure motivations and details on mutual coinduction. mapping and FT is the free type mapping, and if ≤ denotes the subclassing relation and <: denotes the subtyping relation, then for E and FT to define an Category Theory Given that, in a precise sense, adjunction it should be the case that category theory can be viewed as a (major) general- ization of order theory [21, 30, 31], category theory E(t) ≤ c ⇐⇒ t<: FT (c), (3) can enter into our order-theoretic approach to mod- eling Java generics via using at least three category- for all types t and classes c. theoretic tools: In words, this condition says that the erasure E(t) of a parameterized type t is a subclass of class c if *Adjunctions In the order-theoretic approach to and only if t is a subtype of the free type FT (c) cor- modeling Java generics, a clear distinction is made responding to class c.10 and maintained between classes and subclassing, on The preservation condition expressed by Equa- one hand, and types and subtyping, on the other tion (3) on the current page is equivalent to the state- hand. ment stating that, for any two classes C and D, if D This clear distinction between classes, as type con- is a subclass of (i.e., inherits from) C then all pa- structors, and types, as constructed by classes, allows rameterized types that are instantiations of D, and us to easily see that an adjunction [31] (called a ‘Ga- their subtypes, are subtypes of the free type C lois connection’ in order theory parlance [20]) exists corresponding to class C and vice versa, i.e., if all between subclassing, as a relation between generic instantiations of some class D and their subtypes are and non-generic classes, and subtyping, as a relation subtypes of the free type C corresponding to some between parameterized types. We call this adjunc- class C, then D is a subclass of C. tion the Java Erasure Adjunction (JEA). As stated here, this statement is familiar to OO In JEA, Java erasure, which “erases” the type ar- developers using nominally-typed OO programming guments of a parameterized type, is the left adjoint. languages such as Java, C#, C++, Scala and Kotlin. The notion of a free type, which maps a class to a type It is a true statement in these languages due to the expressing the “most general wildcard instantiation” nominality of subtyping in these languages. Sub- of the class, is the right adjoint of JEA. classing (a.k.a., inheritance), a relation characteris- As the left part (or adjoint) of the adjunction, tic of class-based OOP, is always specified between Java erasure maps a parameterized type to a class 10Consider, for example, the statement (in Java) (by “erasing” the type arguments), while as the right part of the adjunction, the notion of a free type cor- LinkedList ≤ List ⇐⇒ LinkedList <: List responding to a class maps any generic class to the where, in Equation (3) on this page, type variable t is instanti- type expressing the “most general wildcard instanti- ated to the generic type LinkedList for all type arguments ation” of the class (e.g., a generic class C with one T (e.g., String or Integer or ? extends Number) and class type parameter is mapped to the type C as its variable c is instantiated to class List. This statement asserts that class LinkedList in Java is a subclass of List if and only corresponding free type). We call this adjunction the if all instantiations of LinkedList are subtypes of the free type Java Erasure Adjunction (JEA). List—which is a true statement in Java.

6 classes in OO programs using class names. Nominal tive types and coinductive types can be expressed, subtyping asserts a bidirectional correspondence be- more generally, using the category-theoretic notions tween the subtyping relation and the inherently nom- of F -algebras and F -coalgebras (or, algebraic types inal subclassing relation. and coalgebraic types). In the case of non-generic OOP, the correspon- This observation allows us to easily see that F - dence between subtyping and subclassing is ex- subtypes and F -supertypes of a generic class F in pressed, succinctly, by stating that ‘inheritance is a Java program correspond, in a precise category- subtyping’.. In the case of generic OOP, the corre- theoretic sense, to coalgebras and algebras of class spondence is succinctly expressed by stating that ‘in- F , respectively. This further allows easily seeing that heritance is the source of subtyping’. The latter is a free types (as the greatest/largest F -subtypes) are fi- compact expression of Equation (3). nal coalgebras in the Java subtyping category (i.e., Focusing on generic nominally-typed OOP, the ‘in- when the Java subtyping relation is viewed as a cat- heritance is the source of subtyping’ statement and egory, rather than a poset), and that, on the other Equation (3) state, first (in the left-to-right direc- hand, initial algebras rarely exist in the Java subtyp- tion), that subclassing does result in (i.e., is a source ing relation (since, unlike for free types, Java does of) subtyping between reference types, and, secondly not define a general notion of types that correspond (in the right-to-left direction), that subclassing is to least/smallest F -supertypes). the only source of subtyping between reference types Discussing final coalgebras and initial algebras mo- (i.e., that besides subclassing there are no other tivates us to also suggest adding co-free types as the sources for subtyping). least F -supertypes, i.e., as initial algebras, to Java. It should be noted that the notion of the free As the name indicates, cofree types function as duals type corresponding to a class is similar, in a pre- of free types, which, as we discussed, are final coal- cise category-theoretic sense, to the notion of the free gebras in the Java subtyping relation.12 monoid corresponding to a set and of the free cate- We did not investigate co-free types in much depth. gory (a quiver) corresponding to a graph. More de- However, for each generic class C, we suggest the no- tails on free types and JEA are available in [5], [15] tation C for the corresponding co-free type. In and [12]. Java the cofree type C has as its only instance the trivial object null (with the type adding knowl- *Monads Closure and kernel operators in order edge that this object is of type C, and thus can be theory correspond to monads and comonads in cate- used as an instance of class C, which is information gory theory [21, 30]. As such, the discussion of order- that type Null does not always tell, e.g., in lower theoretic (co)induction we presented earlier (which bounds). Further, the cofree type C has as its can be expressed using closure and kernel opera- supertypes all parameterized types that are instanti- tors [20]) can be generalized to involve categories, ations of class C (and their supertypes), and has as using monads and comonads.11 When using mon- its subtypes only the cofree types corresponding to ads from category theory, the discussion of induc- all subclasses of C (including type Null). As such, just like the Java subtyping relation between free 11 Generalizing the discussion of induction/coinduction to types alone (i.e., when the relation is restricted to categories has the benefit of allowing the discussion of induc- these types only) being the same as (i.e., is order- tive and coinductive types while not requiring the subtyping relation to be a complete lattice. This makes category theory isomorphic to) the subclassing relation, the subtyp- and order theory (of non-lattices) closer and better suited to ing relation between co-free types alone is also the study nominally-typed OO type systems, since, by not requir- same as the subclassing relation. ing least upper bounds (lubs) and greatest lower bounds (glbs), the two disciplines allow but do not require the existence of We envision the main use of cofree types to be fixed points (of generators, i.e., type constructors), unlike the case of structural type systems that are modeled by (power) 12In homage to Java’s mascot, we sometimes call cofree types set theory. We further discuss this point in [8, 12]. “coffee types!”.

7 as lower bounds of type variables (in doubly F - with Object to define the largest type interval), and bounded generics) and of interval type arguments (in the model also suggests doubly F -bounded generics interval types), rather than, confusingly, using free as the proper generalization of F -bounded generics.13 types. (In Java, currently, when a free type such as We believe the order-theoretic approach to model- C is used as the lower bound of a wildcard type ing Java generics, when developed in full, can offer an argument—as in ? super C—it has a somewhat accurate model of generics (given the few simplifying different meaning than when the free type is used as assumptions it assumes). The current approach to an upper bound of a wildcard type argument—as in modeling generics is based on bounded existentials, ? extends C—hence hinting at the need for co- which is a notion that is typically beyond the reach of free types. See [7] and [15] for more details. We en- most average Java developers (and even, sometimes, vision ? super C as expressing more closely the advanced Java developers). Given its ultimate de- intended meaning of ? super C.) pendency on finite graphs and posets of the finite nominal subclassing relation, we believe the order- *Operads In category theory, operads are a use- theoretic approach is simpler and more intuitive than ful tool for modeling self-similar phenomena [31]. As the bounded existentials approach. such, operads can be used to construct the generic We believe that the use of bounded existentials as subtyping relation in Java. The construction process a model of Java wildcards is due to most of earlier of the subtyping relation between ground Java types approaches to modeling generics not having nominal we discussed earlier is an iterative process, reflecting typing and subtyping as fundamental characteristics the self-similarity of the relation. As such, an operad of these approaches. Given the vast amount of ear- can be defined to model this construction process. lier research done on functional programming lan- In [6] we presented an outline for defining such an guages, which are largely structurally-typed, most of operad. these earlier approaches to modeling generics were de- veloped, at least initially, with structural typing and subtyping in mind. This led these earlier approaches Discussion In this paper we presented the out- to also adopt the bounded existentials model (of line of an order-theoretic approach to modeling Java bounded parametric polymorphism in functional pro- generics. Unlike many extant models of generic OOP, gramming [27]) as a model for Java wildcards. How- the order-theoretic approach fully embraces nominal ever, the structural and set-theoretic (rather than typing/subtyping, as found in mainstream OOP lan- nominal and order-/category-theoretic) orientation guages such as Java, C#, C++, Scala and Kotlin. of bounded parametric polymorphism, and thus of For example, in agreement with the nominality of bounded existentials, makes them more suited for the subtyping relation in nominally-typed OOP, the modeling polymorphic structurally-typed OOP lan- nominal type inheritance relation is the sole basis for guages (such as ML) but lesser suited for model- defining the subtyping relation in our approach. ing generic nominally-typed OOP languages (such Also, influenced by paths in graphs (which always as Java, C#, Scala, Kotlin, and C++).14 In other have start and end points) and by bounded posets (which always have top and bottom elements), in our 13Further, similar to how vertices in graphs are sometimes approach both upper bounds and lower bounds of treated as trivial (‘zero-length’) paths (or trivial graph inter- type variables and of type arguments are treated, in vals) when necessary, single types in the order-theoretic model are also sometimes treated as ‘trivial interval types’. the same way, as bounds of an interval. 14As we discuss in more detail in [12], the structural versus For example, the order-theoretic model of Java nominal orientation of each approach reflects itself, for exam- generics includes an explicit type Null (in symme- ple, in set-theoretic models (like in structural type theory) ne- try with type Object), and it suggests interval types cessitating the existence of (least and greatest) fixed points of type generators/constructors as models of inductive and as the proper double (i.e., upper and lower) bounded coinductive types, while order-theoretic and category-theoretic generalizations of wildcard types (e.g., Null is paired models (like in nominal type theory) allow the existence of

8 words, we believe having nominal typing and nom- [7] Moez A. AbdelGawad. Doubly F- inal subtyping, and their immediate type-theoretic bounded generics. eprint available at consequences, at the heart of modeling Java generics http://arxiv.org/abs/1808.06052, 2018. allows having a model of Java generics that is sim- pler than extant models. We hope this paper has [8] Moez A. AbdelGawad. Induction, coin- demonstrated so.15 duction, and fixed points: A concise comparative survey. eprint available at http://arxiv.org/abs/1812.10026, 2018. References [9] Moez A. AbdelGawad. Java subtyping as an in- finite self-similar partial graph product. eprint [1] C# Language Specification, Version 5.0. 2015. available at http://arxiv.org/abs/1805.06893, 2018. [2] International Standard ISO/IEC 14882:2017(E) - Programming Language C++. 2017. [10] Moez A. AbdelGawad. Partial Carte- sian graph product. eprint available at [3] Kotlin Language Documentation, v. 1.2. 2018. http://arxiv.org/abs/1805.07155, 2018.

[4] Moez A. AbdelGawad. A domain-theoretic [11] Moez A. AbdelGawad. Towards tam- model of nominally-typed object-oriented pro- ing Java wildcards and extending Java gramming. Electronic Notes in Theoretical Com- with interval types. eprint available at puter Science (Full version preprint available http://arxiv.org/abs/1805.10931, 2018. at http://arxiv.org/abs/1801.06793), 301:3–19, 2014. [12] Moez A. AbdelGawad. Induction, coinduc- tion, and fixed points in programming lan- [5] Moez A. AbdelGawad. Novel uses of category guages type theory. eprint available at theory in modeling OOP (extended abstract). http://arxiv.org/abs/1903.05126, 2019. Accepted (but unpublished) at The Nordic Work- shop on Programming Theory (NWPT’17), [13] Moez A. AbdelGawad. Induction, coinduction, Turku, Finland (Full version preprint available and fixed points: Intuitions and tutorial. eprint at http://arxiv.org/abs/1709.08056), 2017. available at http://arxiv.org/abs/1903.05127, 2019. [6] Moez A. AbdelGawad. Towards a Java subtyp- ing operad. Proceedings of FTfJP’17, Barcelona, [14] Moez A. AbdelGawad. Mutual Spain (Extended version preprint available at coinduction. eprint available at http://arxiv.org/abs/1706.00274), 2017. http://arxiv.org/abs/1903.06514, 2019.

such fixed points, but they do not necessitate their existence [15] Moez A. AbdelGawad. Using category the- to model inductive and coinductive types (since these types ory in modeling generics in object-oriented can be modeled instead by least pre-fixed points and great- programming (outline). eprint available at est post-fixed points in non-lattices order theory, or by initial http://arxiv.org/abs/1905.xxxxx, 2019. algebras and final coalgebras in category theory). 15Collaboration Plea: Given the limited human and finan- cial resources available to him, the author would like to invite [16] Gilad Bracha, Martin Odersky, David interested and qualified parties (such as individual researchers, Stoutamire, and Philip Wadler. Making programming languages research groups, software-development the future safe for the past: Adding genericity companies and corporations, research funding agencies, and to the Java programming language. In Craig other interested parties) to join him in speeding up the devel- opment of the order-/category-theoretic approach to modeling Chambers, editor, OOPSLA, volume 33, pages Java generics. 183–200, Vancouver, BC, October 1998.

9 [17] Nicholas Cameron, Sophia Drossopoulou, and [29] Benjamin C. Pierce. Types and Programming Erik Ernst. A model for Java with wildcards. Languages. MIT Press, 2002. In ECOOP’08, 2008. [30] Hilary A. Priestley. Ordered sets and complete [18] Nicholas Cameron, Erik Ernst, and Sophia lattices: A primer for computer science. In Drossopoulou. Towards an existential types Roy Crole Roland Backhouse and Jeremy Gib- model for Java wildcards. FTfJP’07, 2007. bons, editors, Algebraic and Coalgebraic Meth- ods in the Mathematics of Program Construc- [19] Robert Cartwright and Jr. Steele, Guy L. Com- tion, chapter 2, pages 21–78. Springer, 2002. patible genericity with run-time types for the Java programming language. In Craig Cham- [31] David Spivak. Category theory for the sciences. bers, editor, OOPSLA, volume 33, pages 201– MIT Press, 2014. 215, Vancouver, BC, October 1998. [32] Alexander J. Summers, Nicholas Cameron, Mariangiola Dezani-Ciancaglini, and Sophia [20] B. A. Davey and H. A. Priestley. Introduction to Drossopoulou. Towards a semantic model for Lattices and Order. Cambridge University Press, Java wildcards. FTfJP’10, 2010. 2nd edition, 2002. [33] Ross Tate, Alan Leung, and Sorin Lerner. Tam- [21] Brendan Fong and David Spivak. Seven Sketches ing wildcards in Java’s type system. PLDI’11, in Compositionality: An Invitation to Applied June 4–8, 2011, San Jose, California, USA., Category Theory. Draft, 2018. 2011. [22] James Gosling, Bill Joy, Guy Steele, and Gi- [34] Kresten Krab Thorup and Mads Torgersen. Uni- lad Bracha. The Java Language Specification. fying genericity. In ECOOP 99–Object-Oriented Addison-Wesley, 2005. Programming, pages 186–204. Springer, 1999. [23] James Gosling, Bill Joy, Guy Steele, Gilad [35] Mads Torgersen, Erik Ernst, and Chris- Bracha, Alex Buckley, and Daniel Smith. The tian Plesner Hansen. Wild FJ. In Foundations Java Language Specification. Addison-Wesley, of Object-Oriented Languages, 2005. 2018. [36] Mads Torgersen, Christian Plesner Hansen, Erik [24] Richard Hammack, Wilfried Imrich, and Sandi Ernst, Peter von der Ahé, Gilad Bracha, and Klavzar. Handbook of Product Graphs. CRC Neal Gafter. Adding wildcards to the Java pro- Press, 2nd edition, 2011. gramming language. In SAC, 2004.

[25] Dexter Kozen and Alexandra Silva. Practical coinduction. Mathematical Structures in Com- puter Science, 27(7):1132–1152, 2016.

[26] Angelika Langer. The Java generics FAQ, 2015. http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html.

[27] Robin Milner. A theory of type polymorphism in programming. Journal of Computer and System Sciences, 17:348–375, 1978.

[28] Martin Odersky. The Scala language specifica- tion, v. 2.9, 2014.

10