Ben Greenman Fabian Muehlboeck and Ross Tate Cornell University Typesafe Equality Generic Lists Typesafe Equality override public boolean equalsObject other ifother ID: 683028
Download Presentation The PPT/PDF document "Getting F-Bounded Polymorphism into Shap..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Getting F-Bounded Polymorphism into Shape
Ben Greenman, Fabian Muehlboeck, and Ross TateCornell UniversitySlide2Slide3
Typesafe
Equality + Generic Lists?Slide4
(Typesafe) Equality
@override
public
boolean
equals(Object other) {
if(other instanceof String){…}
else { return false; }}
“Hello”.equals
(5)Slide5
(Typesafe) Equality
interface
Eq
<T> {
boolean equalTo
(T other);}
class String extends
Eq<String> {
boolean equalTo
(String other) {…} …
}
“Hello”.
equalTo
(5)Slide6
(Typesafe
) Equality
{“
Hello”,”World
!”}
.equalTo
{“Ahoi
”,”World!”}
{inc
,
}
.equalTo
{
,id}
Slide7
List (read-only)
Eq
covariant
contravariant
List<String>
List<Object>
Eq
<Object>
Eq
<String>
List<out T>
Eq
<in T>
:>
:>Slide8
i
nterface List<out T>
extends
Eq
<List<
Eq
<T>>>Slide9
i
nterface List<out T>
extends
Eq
<List<Eq
<T>>>
List<String>
<:
Eq<List<String>>
Eq
<List<
Eq
<String>>>
<:
Eq
<List<String>>
List<String>
<:
List<
Eq
<String>>
String
<:
Eq
<String>
Done!
List<String>
<:
Eq
<List<String>>
Eq
<List<
Eq
<String>>>
<:
Eq
<List<String>>
List<String>
<:
List<
Eq
<String>>
String
<:
Eq
<String>
Done!
List<String>
List<
Eq
<String>>
Eq
<
List<
Eq
<String>>
>
Eq
<
List<String>
>
String
Eq
<String>
List<
String
>
List<
Eq
<String>
>Slide10
List<T> extends
Eq
<List<
Eq
<T>>>
Andrew J. Kennedy and Benjamin C. Pierce
On Decidability of Nominal Subtyping with Variance
, FOOL-WOOD 2007
Expansive Inheritance!
List<T> extends Eq
<List<Eq<T>>>
List<T
>
List<
Eq
<T>>
Ross Tate, Alan Leung, and
Sorin
Lerner
Taming Wildcards in Java's Type System
, PLDI 2011
Nested
Contravariance
!
List<T> extends
Eq
<List<
Eq
<T>>>
List<T> extends
Eq
<List<
Eq
<T>>>Slide11
Trees
class Tree extends List<Tree>
Are trees equatable?Slide12
Are Trees equatable?Slide13
Are Trees
equatable?
Tree
<:
List<
Eq
<Tree>>
List<Tree>
<:
List<
Eq
<Tree>>
Tree
<:
Eq
<Tree>
List<Tree>
<:
Eq
<Tree>
Eq
<List<
Eq
<Tree>>>
<:
Eq
<Tree>
Tree
<:
List<
Eq
<Tree>>
Tree
<:
List<
Eq
<Tree>>
List<Tree>
<:
List<
Eq
<Tree>>
Tree
<:
Eq
<Tree>
List<Tree>
<:
Eq
<Tree>
Eq
<List<
Eq
<Tree>>>
<:
Eq
<Tree>
Tree
<:
List<
Eq
<Tree>>
Eq
<Tree>
List<
Eq
<Tree>
>
List<
Tree
>
Tree
Eq
<
Tree
>
Eq
<
List<
Eq
<Tree>>
>
List<
Eq
<Tree>>
TreeSlide14
Meh!Slide15
i
nterface List<out T>
extends
Eq
<List<
Eq
<T>>>Slide16Slide17
WHY?Slide18
i
nterface List<out T>
extends
Eq
<List<
Eq
<T>>>
List<
Eq
<T
>>Slide19
List
<
String
>
List
<
Eq<
…>>
How do we use
Eq
<
…>?
With F-bounded polymorphism!
class String extends
Eq
<
String
>
class Set
<
T extends
Eq
<
T
>>
NOT as type arguments!
NOT for parameters!
NOT for fields!
NOT for local variables!
NOT for return types!Slide20
Shapes
Materials
Used in
Recursive inheritance definitions
Recursive type variable constraints
Used for
Type arguments
Method parametersReturn typesFieldsVariables
String
List<T>
Throwable
FileStream
Eq
<T>
Comparable<T>
Clonable
<T>
Summable
<T>Slide21
All Sheep in Scotland are blackSlide22
Read these 13 million lines of Code!
Can I script it?
NO!
I mean… YES!Slide23
Survey
13.5 million lines of open-source generic Java code
*http
://qualitascorpus.com
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 & VariablesRecursive
- inheritance definitions
- type variable
constraintsSlide24
Thou shalt not
mix shapes
and materials!
Material-Shape SeparationSlide25Slide26
Shapes
Materials
Used in
Recursive inheritance definitions
Recursive type variable
constraints
Used for
Type argumentsMethod parametersFieldsVariables
Recursive inheritance only through Shapes
Well-founded inheritanceSlide27
Well-founded Material Inheritance
class A extends B<C>, D<E> {…}
Inheritance hierarchies defined
independent of A
MaterialsSlide28
Well-founded Material Inheritance
class A extends B {…}
class B extends A {…}
class A extends List<B> {…}
class B extends List<A> {…}Slide29
Decidable Subtyping
With naïve algorithmsProven with a simple measureSlide30
Decidable Subtyping
Pred
<List<Object>>
<:
Pred
<Matrix<String>>
Matrix<String>
<:
List<Object>
List<List<String>>
<:
List<Object>
List<String>
<:
Object
Done!
Pred
<List<Object>>
<:
Pred
<Matrix<String>>
Matrix<String>
<:
List<Object>
List<List<String>>
<:
List<Object>
List<String>
<:
Object
Done!
material interface
Pred
<in T>
material interface Matrix<out T>
extends List<List<T>>Slide31
Decidable Subtyping
Pred
<
List<Object>
>
<:
Pred
<
Matrix<String>>
Matrix<String>
<:
List<Object>
List<
List<String>
>
<:
List<
Object
>
List<String>
<:
Object
Done!
Pred
<
List<Object>
>
<:
Pred
<
Matrix<String>
>
Matrix<String>
<:
List<Object>
List<
List<String>
>
<:
List<
Object
>
List<String>
<:
Object
Done!Slide32
Decidable Subtyping
Pred
<List<Object>>
<:
Pred
<Matrix<String>>
Matrix<String
>
<:
List<Object>
List<List<String
>>
<:
List<Object>
List<String>
<:
Object
Done!
Pred
<List<Object>>
<:
Pred
<Matrix<String>>
Matrix<String
>
<:
List<Object>
List<List<String
>>
<:
List<Object>
List<String>
<:
Object
Done!Slide33
Decidable Subtyping
A<T> extends B<C<T>>
Inheritance well-founded
measure function terminates
Slide34
A<in T> extends
Eq
<A<List<T>>>
A<in T> extends
Eq
<
A<List<T>>
>Decidable Subtyping
All Materials
No Shapes in here
Slide35Slide36Slide37
Joins
Most precise common supertype
someBool
? 42 : “Hello”
String
Integer = ?
Slide38
Joins
Integer extends
Clonable
<Integer>
String extends
Clonable
<String>
String
Integer = ?
Clonable<Object>
Clonable<
Clonable
<
Object
>>
Clonable
<
Clonable
<
Clonable
<
Object
>>>Slide39
Computable Material Joins
Materials only: Object
Integer extends
Clonable
<Integer>
String extends
Clonable
<String>
String
Integer = ?
someBool ? 42 : “Hello”Slide40
Recursion on
and
E<
’>
Recursion
on
and
E<
’>
MaterialJoins
A<T> extends C<T>
B<T> extends C<E<T>>
A<
>
B<
’>
Finite due to
well-founded
material inheritance
C<…>
C<
> C<E<
’>>
Slide41
Higher KindsSlide42Slide43
Conditional InheritanceSlide44
Decidable Subtyping
Higher Kinds
Computable Joins
Material-Shape SeparationSlide45Slide46