encoding ownership types in java

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

Upload: yoshe

Post on 12-Jan-2016

48 views

Category:

Documents


1 download

DESCRIPTION

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...). Ownership types for real life. But ownership type systems are big and complex - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Encoding Ownership Types in Java

Encoding Ownership Types in Java

Nicholas CameronJames Noble

Victoria University of Wellington,New Zealand

Page 2: Encoding Ownership Types in Java

Ownership types for real life

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

Page 3: Encoding Ownership Types in Java

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

Ownership types for real life

• There is another way...

Page 5: Encoding Ownership Types in Java

Ownership Types

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

– Concurrency– Memory management– Security– ...

Page 6: Encoding Ownership Types in Java

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

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

Java

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

Page 9: Encoding Ownership Types in Java

Java

• Wildcards

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

Page 10: Encoding Ownership Types in Java

End of Background

. . .

Page 11: Encoding Ownership Types in Java

Basic idea

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

Page 12: Encoding Ownership Types in Java

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

Context parameters

• Become type parameters

Page 14: Encoding Ownership Types in Java

Bounds

Page 15: Encoding Ownership Types in Java

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

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

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

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

The ‘this’ context

• But, what about nesting?

Page 20: Encoding Ownership Types in Java

The ‘this’ context

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

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

Page 21: Encoding Ownership Types in Java

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

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

The ‘this’ context

Page 24: Encoding Ownership Types in Java

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

Nice...

Page 26: Encoding Ownership Types in Java

Existential Owners

• and variant ownership

• Use wildcards

Page 27: Encoding Ownership Types in Java

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

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

Universes

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

Page 30: Encoding Ownership Types in Java

and...

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

Page 31: Encoding Ownership Types in Java

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

Owners-as-Dominators

• Cannot be enforced by translating compiler

• Requires enforcing well-formedness of intermediate types

Page 33: Encoding Ownership Types in Java

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

Thank you!