Encoding Ownership Types in Java
description
Transcript of Encoding Ownership Types in Java
![Page 1: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/1.jpg)
Encoding Ownership Types in Java
Nicholas CameronJames Noble
Victoria University of Wellington,New Zealand
![Page 2: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/2.jpg)
Ownership types for real life
• Ownership types are great!– (More later...)
![Page 3: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/3.jpg)
Ownership types for real life
• But ownership type systems are big and complex– And writing compilers is hard– And the type systems are not well-understood
![Page 4: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/4.jpg)
Ownership types for real life
• There is another way...
![Page 5: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/5.jpg)
Ownership Types
• Are a facilitating type system:– Effects• Parallelisation• Optimisation
– Concurrency– Memory management– Security– ...
![Page 6: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/6.jpg)
Ownership Types
• When the heap gets large, reasoning gets hard• Solution: break it up into smaller regions– BUT, we don’t program this way
• Nest the regions– Welcome to ownership types!
![Page 7: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/7.jpg)
Ownership Types
• owner:ClassName– this:C– world:D
• owner keyword names the owner of this– owner:C
• Context parameters add flexibility
![Page 8: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/8.jpg)
Java
• Generics– List<String>– List<Dog>
![Page 9: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/9.jpg)
Java
• Wildcards
– List<?>– List<? extends Dog>
![Page 10: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/10.jpg)
End of Background
. . .
![Page 11: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/11.jpg)
Basic idea
• We use type parameters to mimic ownership parameters (OGJ)
![Page 12: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/12.jpg)
An object’s owner
(and the ‘world’ context)
• class C {...}• world:C
• class C<Owner> {...}• C<World>– class World {}
![Page 13: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/13.jpg)
Context parameters
• Become type parameters
![Page 14: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/14.jpg)
Bounds
![Page 15: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/15.jpg)
The ‘this’ context
• This* is where it gets interesting• We depart from OGJ– (OGJ does this with magic)
• Must correspond with the this variable
*no pun intended
![Page 16: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/16.jpg)
The ‘this’ context
• Kind of like another context parameter– class C<Owner, This> { ... }
• We can name This within the class
![Page 17: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/17.jpg)
The ‘this’ context
• But this cannot be named outside the class– So neither should This
• Use a wildcard to hide This
![Page 18: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/18.jpg)
The ‘this’ context
• class E<c1, c2>• world:E<this, owner>
• class E<C1, C2, Owner, This>• E<This, Owner, World, ?>
![Page 19: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/19.jpg)
The ‘this’ context
• But, what about nesting?
![Page 20: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/20.jpg)
The ‘this’ context
• Use bounds– class C<Owner, This extends Owner>
• Wildcards inherit declared bounds– C<World, ?>
![Page 21: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/21.jpg)
The ‘this’ context
• class E<c1, c2>• world:E<this, owner>
• class E<C1, C2, Owner,
This extends Owner>• E<This, Owner, World, ?>
![Page 22: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/22.jpg)
The ‘this’ context• class E<c1, c2>• world:E<this, owner>
• class E<C1, C2, Owner, This extends Owner>• E<This, Owner, World, ?>
• (E<This, Owner, World, ?>) new <This, Owner, World, World>
![Page 23: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/23.jpg)
The ‘this’ context
![Page 24: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/24.jpg)
The ‘this’ context
• The type system thinks there is a hierarchy– X inside Y inside Z inside ...
• But in reality all owners are World
![Page 25: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/25.jpg)
Nice...
![Page 26: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/26.jpg)
Existential Owners
• and variant ownership
• Use wildcards
![Page 27: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/27.jpg)
Inner Classes
• Require inner classes to be able to name surrounding This parameter– Comes naturally with Java generics
![Page 28: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/28.jpg)
Type Parameters
• Work alongside translated context parameters
• class F<X> { ... }• world:F<Dog>
• class F<X, Owner, This> { ... }• F<Dog, World, ?>
![Page 29: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/29.jpg)
Universes
• rep C C<This, ?>• peer C C<Owner, ?>• any C C<?, ?>
![Page 30: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/30.jpg)
and...
• Ownership Domains• Context-parametric methods• Dynamic aliases• Fields as contexts• Existential downcasting
![Page 31: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/31.jpg)
Owners-as-Dominators
• Most of the work is done by the hiding of This using wildcards
• Must ensure it cannot be named indirectly
• Works with the extensions too– Including inner classes
![Page 32: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/32.jpg)
Owners-as-Dominators
• Cannot be enforced by translating compiler
• Requires enforcing well-formedness of intermediate types
![Page 33: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/33.jpg)
Contributions
• Prototype compilers– Ownership types++– Universes
• How to leverage existing compiler technology for OTs
• Formalisation of OTs in Java– Proved sound– Ownership hierarchy is preserved and enforced at
runtime• Better understanding of OTs
![Page 34: Encoding Ownership Types in Java](https://reader036.fdocuments.in/reader036/viewer/2022062802/56814588550346895db26dca/html5/thumbnails/34.jpg)
Thank you!