encoding ownership types in java nicholas cameron james noble victoria university of wellington, new...

34
Encoding Ownership Types in Java Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

Upload: monica-horton

Post on 06-Jan-2018

216 views

Category:

Documents


1 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Encoding Ownership Types in Java Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

Encoding Ownership Types in Java

Nicholas CameronJames Noble

Victoria University of Wellington,New Zealand

Page 2: Encoding Ownership Types in Java Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

Ownership types for real life

• Ownership types are great!– (More later...)

Page 3: Encoding Ownership Types in Java Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

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 Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

Ownership types for real life

• There is another way...

Page 5: Encoding Ownership Types in Java Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

Ownership Types

• Are a facilitating type system:– Effects• Parallelisation• Optimisation

– Concurrency– Memory management– Security– ...

Page 6: Encoding Ownership Types in Java Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

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 Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

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 Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

Java

• Generics– List<String>– List<Dog>

Page 9: Encoding Ownership Types in Java Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

Java

• Wildcards

– List<?>– List<? extends Dog>

Page 10: Encoding Ownership Types in Java Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

End of Background

. . .

Page 11: Encoding Ownership Types in Java Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

Basic idea

• We use type parameters to mimic ownership parameters (OGJ)

Page 12: Encoding Ownership Types in Java Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

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 Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

Context parameters

• Become type parameters

Page 14: Encoding Ownership Types in Java Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

Bounds

Page 15: Encoding Ownership Types in Java Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

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 Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

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 Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

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 Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

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 Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

The ‘this’ context

• But, what about nesting?

Page 20: Encoding Ownership Types in Java Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

The ‘this’ context

• Use bounds– class C<Owner, This extends Owner>

• Wildcards inherit declared bounds– C<World, ?>

Page 21: Encoding Ownership Types in Java Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

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 Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

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 Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

The ‘this’ context

Page 24: Encoding Ownership Types in Java Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

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 Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

Nice...

Page 26: Encoding Ownership Types in Java Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

Existential Owners

• and variant ownership

• Use wildcards

Page 27: Encoding Ownership Types in Java Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

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 Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

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 Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

Universes

• rep C C<This, ?>• peer C C<Owner, ?>• any C C<?, ?>

Page 30: Encoding Ownership Types in Java Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

and...

• Ownership Domains• Context-parametric methods• Dynamic aliases• Fields as contexts• Existential downcasting

Page 31: Encoding Ownership Types in Java Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

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 Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

Owners-as-Dominators

• Cannot be enforced by translating compiler

• Requires enforcing well-formedness of intermediate types

Page 33: Encoding Ownership Types in Java Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

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 Nicholas Cameron James Noble Victoria University of Wellington, New Zealand

Thank you!