Getting F-Bounded Polymorphism into Shape Ben Greenman, Fabian Muehlboeck, and Ross Tate Cornell University
Typesafe Equality + Generic Lists?
(Typesafe) public boolean equals(Object other) { if(other instanceof String){…} else { return false; } } “Hello”.equals(5)
(Typesafe) Equality interface Eq { boolean equalTo(T other); } class String extends Eq { boolean equalTo(String other) {…} … } “Hello”.equalTo(5)
(Typesafe) Equality {“Hello”,”World!”}.equalTo {“Ahoi”,”World!”}
List (read-only)Eq covariant contravariant List List Eq Eq List Eq :>
interface List extends Eq >>
interface List extends Eq >> List <:Eq > Eq >><:Eq > List <:List > String<:Eq Done! List List > Eq >>Eq > StringEq List List >
List extends Eq >> Andrew J. Kennedy and Benjamin C. Pierce On Decidability of Nominal Subtyping with Variance, FOOL-WOOD 2007 Expansive Inheritance! List extends Eq >> List List > Ross Tate, Alan Leung, and Sorin Lerner Taming Wildcards in Java's Type System, PLDI 2011 Nested Contravariance! List extends Eq >>
Trees class Tree extends List Are trees equatable?
Are Trees equatable?
Tree<:List > List <:List > Tree<:Eq List <:Eq Eq >><:Eq Tree<:List > Eq List >List Tree Eq Eq >> List > Tree
Meh!
interface List extends Eq >>
WHY?
interface List extends Eq >> List >
List List > How do we use Eq ? With F-bounded polymorphism! class String extends Eq class Set > NOT as type arguments! NOT for parameters! NOT for fields! NOT for local variables! NOT for return types!
ShapesMaterials Used in Recursive inheritance definitions Recursive type variable constraints Used for Type arguments Method parameters Return types Fields Variables String List Throwable FileStream Eq Comparable Clonable Summable
All Sheep in Scotland are black
Read these 13 million lines of Code! Can I script it? NO! I mean… YES!
Survey 13.5 million lines of open-source generic Java code * taken primarily from the Qualitas Corpus* No class was ever both a shape and a material from 60 projects - Type arguments - Method parameters - Return types - Fields & Variables Recursive - inheritance definitions - type variable constraints
Thou shalt not mix shapes and materials! Material-Shape Separation
ShapesMaterials Used in Recursive inheritance definitions Recursive type variable constraints Used for Type arguments Method parameters Fields Variables Recursive inheritance only through Shapes Well-founded inheritance
Well-founded Material Inheritance class A extends B, D {…} Inheritance hierarchies defined independent of A Materials
Well-founded Material Inheritance class A extends B {…} class B extends A {…} class A extends List {…} class B extends List {…}
Decidable Subtyping With naïve algorithms Proven with a simple measure
Decidable Subtyping Pred ><:Pred > Matrix <:List List ><:List <:Object Done! material interface Pred material interface Matrix extends List >
Decidable Subtyping Pred ><:Pred > Matrix <:List List ><:List <:Object Done!
Decidable Subtyping Pred ><:Pred > Matrix <:List List ><:List <:Object Done!
Decidable Subtyping A extends B > Inheritance well-founded measure function terminates
A extends Eq >> Decidable Subtyping All Materials No Shapes in here
Joins Most precise common supertype someBool ? 42 : “Hello”
Joins Integer extends Clonable String extends Clonable Clonable Clonable > Clonable >>
Computable Material Joins Materials only: Object Integer extends Clonable String extends Clonable someBool ? 42 : “Hello”
Material Joins A extends C B extends C > Finite due to well-founded material inheritance C
Higher Kinds
Conditional Inheritance
Decidable Subtyping Higher Kinds Computable Joins Material-Shape Separation