78
A Programming Model and Language for Concurrent and Distributed Object-Oriented Systems Jan Schäfer University of Kaiserslautern Department of Computer Science Supervisors Prof. Dr. Arnd Poetzsch-Heffter University of Kaiserslautern Prof. Dr. Einar Broch Johnsen University of Olso 5. November 2010 1

A Programming Model and Language for Concurrent and ...A Programming Model and Language for Concurrent and Distributed Object-Oriented Systems Jan Schäfer ... Concurrency in Mainstream

  • Upload
    others

  • View
    28

  • Download
    0

Embed Size (px)

Citation preview

A Programming Model and Language for Concurrent andDistributed Object-Oriented Systems

Jan Schäfer

University of KaiserslauternDepartment of Computer Science

Supervisors

Prof. Dr. Arnd Poetzsch-HeffterUniversity of Kaiserslautern

Prof. Dr. Einar Broch JohnsenUniversity of Olso

5. November 2010

1

Motivation The Free Lunch is Over

“The Free Lunch is Over”

• Since about 5 years computer processors are not getting fasteranymore, instead they are getting more parallel.

• Software must be parallelizable to profit from new processorgenerations.

• Leading to “A Fundamental Turn Toward Concurrency inSoftware” – Herb Sutter

• The Internet is everywhere⇒ distributed systems

• Non-expert programmers have to handle concurrency anddistribution

2

Motivation State-of-the-Art in Practice

State-of-the-Art in Practice

Concurrency in Mainstream OOLs (e.g. Java and C#)

• Concurrently running threads

• Shared state

• Synchronization with locks and monitors

Problems

• Error-prone (data races, deadlocks)

• Not very modular

• Not suitable for components

• Not suitable for distributed programming

• Too difficult for most (all) software developers

3

Motivation State-of-the-Art in Practice

State-of-the-Art in Practice

Concurrency in Mainstream OOLs (e.g. Java and C#)

• Concurrently running threads

• Shared state

• Synchronization with locks and monitors

Problems

• Error-prone (data races, deadlocks)

• Not very modular

• Not suitable for components

• Not suitable for distributed programming

• Too difficult for most (all) software developers

4

Motivation State-of-the-Art in Practice

State-of-the-Art in Practice

Concurrency in Mainstream OOLs (e.g. Java and C#)

• Concurrently running threads

• Shared state

• Synchronization with locks and monitors

Problems

• Error-prone (data races, deadlocks)

• Not very modular

• Not suitable for components

• Not suitable for distributed programming

• Too difficult for most (all) software developers

“Avoid threads whenever possible”– John Ousterhout (Sun Microsystems, 1996)

5

Motivation Goal

Goal

Develop, formalize, and implement a programming model and languagethat

1 Forbids data races

2 Integrate with Object-Orientation

3 Provide a notion of OO-Components

4 Support modularity

5 Suited for distributed programming

6 Support sequential OOP

7 Efficiently implementable

8 Easy to learn and to use

6

Motivation Goal

Goal

Develop, formalize, and implement a programming model and languagethat

1 Forbids data races

2 Integrate with Object-Orientation

3 Provide a notion of OO-Components

4 Support modularity

5 Suited for distributed programming

6 Support sequential OOP

7 Efficiently implementable

8 Easy to learn and to use

7

Motivation Outline

1 Motivation

2 The CoBox Model

3 Related Work

4 Formalization

5 The JCoBox Language

6 Practical Evaluation

7 Conclusions

8

The CoBox Model

1 Motivation

2 The CoBox Model

3 Related Work

4 Formalization

5 The JCoBox Language

6 Practical Evaluation

7 Conclusions

9

The CoBox Model Basic Idea

Basic Idea

object thread reference

LEGEND

10

The CoBox Model Object Structuring

Object Structuring

cobox object thread reference

LEGEND

11

The CoBox Model Thread Structuring

Thread Structuring

cobox object thread reference

LEGEND

12

The CoBox Model Cooperative Multi-Tasking

Cooperative Multi-Tasking

cobox object active task ready task reference

LEGEND

13

The CoBox Model Cooperative Multi-Tasking

Cooperative Multi-Tasking

ready queue

active task

suspend set create

activatesuspend

yield

resume

active task ready tasks suspended Tasks task scheduling

LEGEND

14

The CoBox Model Cooperative Multi-Tasking

Cooperative Multi-Tasking

cobox object active task ready task reference

LEGEND

15

The CoBox Model Far References

Far References

cobox object active task ready task near ref. far ref.

LEGEND

16

The CoBox Model Asynchronous Calls and Futures

Asynchronous Calls and Futures

future active task ready task near ref. far ref. resolves

LEGEND

17

The CoBox Model Suspending on a Future

Suspending on a Future

future active task ready task suspended task resolves

LEGEND

18

The CoBox Model Suspending on a Future

Suspending on a Future

future active task ready task suspended task resolves

LEGEND

19

The CoBox Model Suspending on a Future

Suspending on a Future

future active task ready task suspended task resolves

LEGEND

20

The CoBox Model Suspending on a Future

Suspending on a Future

future active task ready task suspended task resolves

LEGEND

21

The CoBox Model Further Features

Further Features

• Promises

• Immutable Objects (passed-by-reference)

• Transfer Objects (passed-by-copy)

22

The CoBox Model Goal Review

Goal Review

Develop, formalize, and implement a programming model andlanguage that

1 Forbids data races

2 Integrates with Object-Orientation

3 Provides a notion of OO-Components

4 Supports modularity

5 Suited for distributed programming

6 Supports sequential OOP

7 Efficiently implementable

8 Easy to learn and to use

23

The CoBox Model Goal Review

Goal Review

Develop, formalize, and implement a programming model andlanguage that

1 Forbids data races

2 Integrates with Object-Orientation

3 Provides a notion of OO-Components

4 Supports modularity

5 Suited for distributed programming

6 Supports sequential OOP

7 Efficiently implementable

8 Easy to learn and to use

24

The CoBox Model Goal Review

Goal Review

Develop, formalize, and implement a programming model andlanguage that

1 Forbids data races

2 Integrates with Object-Orientation

3 Provides a notion of OO-Components

4 Supports modularity

5 Suited for distributed programming

6 Supports sequential OOP

7 Efficiently implementable

8 Easy to learn and to use

25

The CoBox Model Goal Review

Goal Review

Develop, formalize, and implement a programming model andlanguage that

1 Forbids data races

2 Integrates with Object-Orientation

3 Provides a notion of OO-Components

4 Supports modularity

5 Suited for distributed programming

6 Supports sequential OOP

7 Efficiently implementable

8 Easy to learn and to use

26

The CoBox Model Goal Review

Goal Review

Develop, formalize, and implement a programming model andlanguage that

1 Forbids data races

2 Integrates with Object-Orientation

3 Provides a notion of OO-Components

4 Supports modularity

5 Suited for distributed programming

6 Supports sequential OOP

7 Efficiently implementable

8 Easy to learn and to use

27

The CoBox Model Goal Review

Goal Review

Develop, formalize, and implement a programming model andlanguage that

1 Forbids data races

2 Integrates with Object-Orientation

3 Provides a notion of OO-Components

4 Supports modularity

5 Suited for distributed programming

6 Supports sequential OOP

7 Efficiently implementable

8 Easy to learn and to use

28

The CoBox Model Goal Review

Goal Review

Develop, formalize, and implement a programming model andlanguage that

1 Forbids data races

2 Integrates with Object-Orientation

3 Provides a notion of OO-Components

4 Supports modularity

5 Suited for distributed programming

6 Supports sequential OOP

7 Efficiently implementable

8 Easy to learn and to use

29

The CoBox Model Goal Review

Goal Review

Develop, formalize, and implement a programming model andlanguage that

1 Forbids data races

2 Integrates with Object-Orientation

3 Provides a notion of OO-Components

4 Supports modularity

5 Suited for distributed programming

6 Supports sequential OOP

7 Efficiently implementable ?

8 Easy to learn and to use ?

30

Related Work

1 Motivation

2 The CoBox Model

3 Related Work

4 Formalization

5 The JCoBox Language

6 Practical Evaluation

7 Conclusions

31

Related Work Actor-Based Programming

Actor-Based Programming (Active Objects)

Concept

• Every object has its own thread

• State is encapsulated

• Communication by asynchronous messages

Problems

• Local state is limited

• Concurrency too fine-grained

• Only one control flow per object

32

Related Work Actor-Based Programming

Actor-Based Programming (Active Objects)

Concept

• Every object has its own thread

• State is encapsulated

• Communication by asynchronous messages

Problems

• Local state is limited

• Concurrency too fine-grained

• Only one control flow per object

33

Related Work Actor-Based Programming

Actor-Based Programming (Active Objects)

Concept

• Every object has its own thread

• State is encapsulated

• Communication by asynchronous messages

Problems

• Local state is limited

• Concurrency too fine-grained

• Only one control flow per object

34

Related Work Active Object Extensions

Active Object Extensions

ASP (Caromel et al., 2004)

• Active objects with multiple passive objects

• But passive objects not accessible from outside

E (Miller et al., 2005)

• Multiple accessible objects

• But only one control flow

Creol (Johnsen and Owe, 2007)

• Multiple cooperatively scheduled control flows

• Integrated with futures. Only single objects.

35

Related Work Active Object Extensions

Active Object Extensions

ASP (Caromel et al., 2004)

• Active objects with multiple passive objects

• But passive objects not accessible from outside

E (Miller et al., 2005)

• Multiple accessible objects

• But only one control flow

Creol (Johnsen and Owe, 2007)

• Multiple cooperatively scheduled control flows

• Integrated with futures. Only single objects.

36

Related Work Active Object Extensions

Active Object Extensions

ASP (Caromel et al., 2004)

• Active objects with multiple passive objects

• But passive objects not accessible from outside

E (Miller et al., 2005)

• Multiple accessible objects

• But only one control flow

Creol (Johnsen and Owe, 2007)

• Multiple cooperatively scheduled control flows

• Integrated with futures. Only single objects.

37

Related Work Active Object Extensions

Active Object Extensions

ASP (Caromel et al., 2004)

• Active objects with multiple passive objects

• But passive objects not accessible from outside

E (Miller et al., 2005)

• Multiple accessible objects

• But only one control flow

Creol (Johnsen and Owe, 2007)

• Multiple cooperatively scheduled control flows

• Integrated with futures. Only single objects.

38

Formalization

1 Motivation

2 The CoBox Model

3 Related Work

4 Formalization

5 The JCoBox Language

6 Practical Evaluation

7 Conclusions

39

Formalization Semantics

Formal Semantics

Rationale

• Precise Semantics

• Provable Properties

Approach

• Formal core language with coboxes

• Static semantics• Small-step operational semantics

É Implemented in Maude

• Properties

40

Formalization Semantics

Formal Semantics

Rationale

• Precise Semantics

• Provable Properties

Approach

• Formal core language with coboxes

• Static semantics• Small-step operational semantics

É Implemented in Maude

• Properties

41

Formalization Formal Core Language

Formal Core Language

p ∈ Prog ::= D e

d ∈ D ⊆ ClassDecl ::= µ class c extends c′ { τ f ; H }µ ∈ Modifier ::= cobox | transfer | plain

h ∈ H ⊆ MethDecl ::= τ m(τ x){e}e ∈ Expr ::= x | null | let x = e in e′ | e.f | e.f = e′ | new c [in e] |

e.m(e) | e!m(e) | e.get | e.await | yield | promise τ | e.fut | e.resolve e′

τ ∈ Type ::= c | F⟨τ⟩ | P⟨τ⟩

42

Formalization Dynamic Entities

Dynamic Entities

Config ::= K configurations

k ∈ K ⊆ Comp ::= b | ρ components

b ∈ B ⊆ CoBox ::= B⟨κb, O, T , t⟩ coboxes

ρ ∈ P ⊆ Prom ::= P⟨κp, O, vε⟩ promises

o ∈ O ⊆ Obj ::= O⟨ι, c, v⟩ objects

| F⟨ι,κp, vε⟩ futures

t ∈ T ⊆ Tsk ::= T⟨e⟩ tasks

v ⊆ Value ::= r | null values

vε ⊆ OptValue ::= v | ε optional values

r ∈ R ⊆ Ref ::= κ.ι | κp references

κ ∈ CompId ::= κb | κp component identifiers

e ∈ E ⊆ Expr ::= . . . | v extended expressions

ι ∈ ObjId object identifiers

κb ∈ CoBoxId cobox identifiers including MAIN

κp ∈ PromId promise identifiers

43

Formalization Reduction Rules

Reduction Rules

Global Rules

K −→ K ′

CoBox-Local Rules

b −→b b′

Global Integration Rule

b −→b b′

K ·∪ b −→ K ∪ b′

44

Formalization CoBox-Local Reduction Rules

CoBox-Local Reduction Rules (subset)

(R-Let)

B⟨κb, O, T , t · T⟨e [let x = v in e]⟩⟩ −→b B⟨κb, O, T , t · T⟨e [[v/x]e]⟩⟩(R-NewObjLocal)

e = new c ∨ e = new c in κb.ι′ ¬coboxcl(c) ι /∈ oids(O)

B⟨κb, O, T , t · T⟨e [e]⟩⟩ −→b B⟨κb, O ∪ O⟨ι, c, init(c)⟩, T , t · T⟨e [κb.ι]⟩⟩(R-DirectCall)

O⟨ι, c, _⟩ ∈ O e′ = mexpr(c, m,κb.ι, v)

B⟨κb, O, T , t · T⟨e [κb.ι.m(v)]⟩⟩ −→b B⟨κb, O, T , t · T⟨e [e′]⟩⟩(R-FieldSelect)

O⟨ι, c, v⟩ ∈ O fields(c) = τ f

B⟨κb, O, T , t · T⟨e [κb.ι.fi ]⟩⟩ −→b B⟨κb, O, T , t · T⟨e [vi ]⟩⟩(R-FieldUpdate)

o = O⟨ι, c, v⟩ fields(c) = τ f o′ = O⟨ι, c, v[vi = v]⟩B⟨κb, O ·∪ o, T , t · T⟨e [κb.ι.fi = v]⟩⟩ −→b B⟨κb, O ∪ o′, T , t · T⟨e [v]⟩⟩

45

Formalization Global Reduction Rules

Global Reduction Rules (subset)

(R-NewObjFar)

plaincl(c) b = B⟨κ′b, O′, T ′, t′⟩ ι /∈ oids(O′) b′ = B⟨κ′b, O′ ∪ O⟨ι, c, init(c)⟩, T ′, t

′⟩K ·∪ b ·∪ B⟨κb, O, T , t · T⟨e [new c in κ′b.ι′]⟩⟩ −→ K ∪ b′ ∪ B⟨κb, O, T , t · T⟨e [κ′b.ι]⟩⟩

(R-NewCoBox)

coboxcl(c) κ′b fresh b = B⟨κ′b, {O⟨ι, c, init(c)⟩},∅,•⟩K ·∪ B⟨κb, O, T , t · T⟨e [new c]⟩⟩ −→ K ∪ B⟨κb, O, T , t · T⟨e [κ′b.ι]⟩⟩ ∪ b

(R-PromNew)

κp fresh

K ·∪ B⟨κb, O, T , t · T⟨e [promise τ]⟩⟩ −→ K ∪ B⟨κb, O, T , t · T⟨e [κp]⟩⟩ ∪ P⟨κp,∅,ε⟩(R-PromResolve)

ρ = P⟨κp,∅,ε⟩ (O′, v ′) = copy(κb, O, v ,κp,∅) ρ′ = P⟨κp, O′, v ′⟩K ·∪ρ ·∪ B⟨κb, O, T , t · T⟨e [κp.resolve v]⟩⟩ −→ K ∪ρ′ ∪ B⟨κb, O, T , t · T⟨e [null]⟩⟩

(R-PromFut)

P⟨κp, _, _⟩ ∈ K ι /∈ oids(O)

K ·∪ B⟨κb, O, T , t · T⟨e [κp.fut]⟩⟩ −→ K ∪ B⟨κb, O ∪ F⟨ι,κp,ε⟩, T , t · T⟨e [κb.ι]⟩⟩

46

Formalization Rules in Maude

Rules in Maude (subset)

rl [R-Let] :let x = v in e => toCxt([ v / x ] e) .

crl [R-NewObjLocal] :conf(p, K cb(k,O,T, TL : tsk( ep [ new c ] )))

=> conf(p, K cb(k,O obj(i,c,init(p,c)),T,TL : tsk( ep [ oref(k,i) ] )))if i := freshObjId(O) /\

not isCoBoxClass(p,c) .

crl [R-NewObjLocalIn] :conf(p, K cb(k,O,T, TL : tsk( ep [ new c in oref(k,i’)] )))

=> conf(p, K cb(k,O obj(i,c,init(p,c)),T,TL : tsk( ep [ oref(k,i) ] )))if i := freshObjId(O) /\

not isCoBoxClass(p,c) .

crl [R-DirectCall] :conf(p, K cb(k,O,T,TL : tsk( ep [ oref(k, i) . m ( V ) ])))

=> conf(p, K cb(k,O,T,TL : tsk( ep [ mexpr(p,c,oref(k,i),m,V) ])))if O’ obj(i,c,vm) := O .

rl [R-FieldSelect] :cb(k,O obj(i,c,vm),T, TL : tsk( ep [ oref(k,i) . x ] ))

=> cb(k,O obj(i,c,vm),T, TL : tsk( ep [ vm[x] ] )) .

rl [R-FieldUpdate] :cb(k,O obj(i,c,(vm,x |-> v’)),T, TL : tsk( ep [ oref(k,i) . x = v’’ ] ))

=> cb(k,O obj(i,c,(vm,x |-> v’’)),T,TL : tsk( ep [ v’’ ] )) .

47

Formalization Properties

Properties

• Type-SoundnessÉ PreservationÉ Progress

• Data race freedom

• Single-cobox determinism

• Sequential program determinism

• Sequential Java equivalence

48

The JCoBox Language

1 Motivation

2 The CoBox Model

3 Related Work

4 Formalization

5 The JCoBox Language

6 Practical Evaluation

7 Conclusions

49

The JCoBox Language JCoBox

JCoBox - Design Decisions

• How to realize sequential programing?

• How to create coboxes?

• How to assign objects to coboxes?

• How to express asynchronous method calls?

• How to express task scheduling?

50

The JCoBox Language JCoBox

JCoBox

• Based on standard sequential Java 5

• CoBox classes: @CoBox• Asynchronous method calls: x!m()

É Alternative: async(x).m()

• Futures: Fut<T>É fut.await() – cooperative claimingÉ fut.get() – exclusive claiming

• Cooperative scheduling: yield()

• Immutable classes: @Immutable

• Transfer classes: @Transfer

51

The JCoBox Language Implementation

Implementation

JCoBox Compiler

• Compiler based on Polyglot Java + JL5

• Generates Java code

Bytecode Rewriter

• Rewrites bytecode (compiled from Java using JCoBox annotations)

• Only supports encoded syntax

52

The JCoBox Language Performance

Performance Evaluation

Languages

• Scala (v2.7.5, v2.7.7)

• Clojure v1.0.0

• JCoBox

Platforms

• Sun JDK 1.6.16 (-Xmx1024)

• Linux 2.6

• Five machines (1 to 4 cores)

Benchmarks

• Big Ping Ponga

• Chameneosb

• Actor Ringb

aSrinivasan and Mycroft, 2008bThe Computer Language

Benchmarks Game53

The JCoBox Language Performance

Results

Ato

mC

lient

Ato

mSe

rver

Ath

lon

Cor

e2D

uoO

pter

onX

eon

Ato

mC

lient

Ato

mSe

rver

Ath

lon

Cor

e2D

uoO

pter

onX

eon

Ato

mC

lient

Ato

mSe

rver

Ath

lon

Cor

e2D

uoO

pter

onX

eon

Ato

mC

lient

Ato

mSe

rver

Ath

lon

Cor

e2D

uoO

pter

onX

eon

2

4

6

8

JCoBox

timeout

programs × platforms

rela

tive

exec

utio

nti

me

JCoBoxScala-2.7.7Scala-2.7.5Clojure

pingpong chameneos ringmsg ringnodes

54

The JCoBox Language Goal Review

Goal Review

Develop, formalize, and implement a programming model and languagethat

1 Forbids data races

2 Integrates with Object-Orientation

3 Provides a notion of OO-Components

4 Supports modularity

5 Suited for distributed programming

6 Supports sequential OOP

7 Efficiently implementable ?

8 Easy to learn and to use ?

55

The JCoBox Language Goal Review

Goal Review

Develop, formalize, and implement a programming model and languagethat

1 Forbids data races

2 Integrates with Object-Orientation

3 Provides a notion of OO-Components

4 Supports modularity

5 Suited for distributed programming

6 Supports sequential OOP

7 Efficiently implementable

8 Easy to learn and to use ?

56

Practical Evaluation

1 Motivation

2 The CoBox Model

3 Related Work

4 Formalization

5 The JCoBox Language

6 Practical Evaluation

7 Conclusions

57

Practical Evaluation

Practical Evaluation

Case Studies

• CoMusic (0.5 kloc)

• FourWins (4 kloc)

• The CoCoME Example (2 kloc)

• Chat Application (0.9 kloc)

58

Practical Evaluation

Practical Evaluation

Case Studies

• CoMusic (0.5 kloc)

• FourWins (4 kloc)

• The CoCoME Example (2 kloc)

• Chat Application (0.9 kloc)

Key Features

• Legacy Java interaction

• Internet access

59

Practical Evaluation

Practical Evaluation

Case Studies

• CoMusic (0.5 kloc)

• FourWins (4 kloc)

• The CoCoME Example (2 kloc)

• Chat Application (0.9 kloc)

Key Features

• Multi-Core utilization

• Interactive

60

Practical Evaluation

Practical Evaluation

Case Studies

• CoMusic (0.5 kloc)

• FourWins (4 kloc)

• The CoCoME Example (2 kloc)

• Chat Application (0.9 kloc)

Key Features

• Distributed

• Component-Based

• Implemented by a student

61

Practical Evaluation

Practical Evaluation

Case Studies

• CoMusic (0.5 kloc)

• FourWins (4 kloc)

• The CoCoME Example (2 kloc)

• Chat Application (0.9 kloc)

Key Features

• Distributed

62

Practical Evaluation

CoMusic Runtime Structure

*

SongModel

SongGUI Swing Objects

DownCtrl

DownProcess

SongPlayer

SongStatus

SongStatus

I/O Objects

I/O Objects *

*

*

*

Disk

Internet

*node group

cobox

standard object

transfer object

legacy Java object

synchronous access

asynchronous access

LEGEND

63

Practical Evaluation Goal Review

Goal Review

Develop, formalize, and implement a programming model and languagethat

1 Forbids data races

2 Integrates with Object-Orientation

3 Provides a notion of OO-Components

4 Supports modularity

5 Suited for distributed programming

6 Supports sequential OOP

7 Efficiently implementable

8 Easy to learn and to use ?

64

Practical Evaluation Goal Review

Goal Review

Develop, formalize, and implement a programming model and languagethat

1 Forbids data races

2 Integrates with Object-Orientation

3 Provides a notion of OO-Components

4 Supports modularity

5 Suited for distributed programming

6 Supports sequential OOP

7 Efficiently implementable

8 Easy to learn and to use ?

65

Conclusions

1 Motivation

2 The CoBox Model

3 Related Work

4 Formalization

5 The JCoBox Language

6 Practical Evaluation

7 Conclusions

66

Conclusions Contributions

Contributions

1 Development of a new concurrency model for OO

2 Formalization of the model (executable in Maude)

3 Development and implementation of a practical programminglanguage

4 Evaluation of performance and practicability

67

Thank You.

Thank You.

68

Back Up

Back Up

69

Back Up Publications

Publications

• ECOOP 2010 - JCoBox: Generalizing Active Objects to ConcurrentComponents

• IWMSE 2010 - Writing concurrent desktop applications in anactor-based programming model

• FMOODS 2008 - CoBoxes: Unifying Active Objects and StructuredHeaps

70

Back Up Outlook

Outlook

• Parallel State Access

• Different Local Execution Models

• Hierarchical Components

• Distributed Systems

• Efficient Multi-Core Execution

• Specification and Verification

71

Back Up Data Exchange

Data Exchange

Immutable Objects

• Never change their state after construction

• Shared between coboxes

Transfer Objects

• Always referenced by near references

• Copied when passed to a different cobox

72

Back Up Data Exchange

Preservation and Progress Lemmas

Lemma (Preservation)

Let p be the implicit, fixed program with `p p. Let Kn be a configurationand Σ a reference typing with Σ � Kn. Assume Kn −→ Kn+1. Then thereexists a Σ′ with Σ ⊆Σ′ and Σ′ � Kn+1.

Lemma (Progress)

Let p be the implicit, fixed program with `p p. Let Kn be a configurationwhich is not terminal, and Σ be a reference typing with Σ � Kn. Thenthere is a Kn+1 with Kn −→ Kn+1.

73

Back Up Type System

Type Rules

(T-Program)p = D e uniqueNames(D)

noCycles(D) `∗d D ∅;∅ `e e

`p p

(T-Decl)noSelfExtend(c, c′) noFieldHide(c)

noOverload(H) modifierOk(c, c′) c `∗h H `∗t τ`d _ class c extends c′{ τ f ; H }

(T-Method)h = τ m(τ x){e} nodups(this · x)

overrideOk(c, m) this : c, x : τ;∅ `e e :< τ

c `h h

(T-Null)` τ

Θ `e null : τ

(T-Var)Γ (x) = τ

Γ ;Σ `e x : τ

(T-Let)Γ ;Σ `e e : τ Γ , x : τ;Σ `e e′ : τ′

Γ ;Σ `e let x = e in e′ : τ′

(T-New)`t c

Θ `e new c : c

(T-NewIn)`t c Θ `e e : c′ plaincl(c)

Θ `e new c in e : c

(T-FieldSelect)Θ `e e : c fields(c) = τ f

Θ `e e.fi : τi

(T-FieldUpdate)Θ `e e.f : τ Θ `e e′ :< τ

Θ `e e.f = e′ : τ

(T-Yield)`t τ

Θ `e yield : τ

(T-FutAwait)Θ `e e : F⟨τ⟩Θ `e e.await : τ

(T-FutGet)Θ `e e : F⟨τ⟩Θ `e e.get : τ

(T-PromNew)`t τ

Θ `e promise τ : P⟨τ⟩

(T-PromResolve)Θ `e e : P⟨τ⟩ Θ `e e′ :< τ

Θ `e e.resolve e′ : τ

(T-PromFut)Θ `e e : P⟨τ⟩Θ `e e.fut : F⟨τ⟩

(T-DirectCall)Θ `e e : c Θ `∗e e :< τ

mtype(c, m) = τ . τ

Θ `e e.m(e) : τ

(T-AsyncCall)Θ `e e.m(e) : τ

Θ `e e!m(e) : F⟨τ⟩

(T-Sub)Θ `e e : τ′ τ′ <: τ

Θ `e e :< τ

(T-TypeCl)definedcl(c)

`t c

(T-TypeFut)`t τ

`t F⟨τ⟩

(T-TypeProm)`t τ

`t P⟨τ⟩

74

Back Up Example: Chat Application

Example: Chat Application

interface Client {

void onChatMsg(Msg m);

}

interface Server {

Session connect(Client c);

}

interface Session {

void publish(Msg m);

void keepAlive();

void close();

}

75

Back Up Example: Chat Application

Example: Chat Application

Server

Session

Session

Client

Client

GUI

GUI

interface Client {

void onChatMsg(Msg m);

}

interface Server {

Session connect(Client c);

}

interface Session {

void publish(Msg m);

void keepAlive();

void close();

}

76

Back Up Example: Chat Application

Example: Chat Application

Server

Session

Session

Client

Client

GUI

GUI

interface Client {

void onChatMsg(Msg m);

}

interface Server {

Session connect(Client c);

}

interface Session {

void publish(Msg m);

void keepAlive();

void close();

}

77

Back Up Example: Chat Application

JCoBox Implementation@CoBox class AClient implements Client {Session session; GUI gui = new GUI();Client() {gui!register(this);

}void connect(Server s) {Fut<Session> f = s!connect(this);session = f.await();session!sendMsg("Hello!");this!ensureAliveness();

}void ensureAliveness() {while (!stopped) {session!keepAlive();yield(1, TimeUnit.SECONDS);

} }void onChatMsg(Msg m) {gui!showMsg(m);

}void msgEntered(Msg m) {session!publish(m);

}}

@CoBox class AServer implements Server {List<ASession> sessions = ......Session connect(Client c) {

ASession s = new ASession(c);sessions.add(s);return s;

}class ASession implements Session {

Client client;long lastAliveSignal;ASession(Client c) {client = c;

}void publish(Msg m) {for (ASession s : sessions) {

s.client!onChatMsg(m);} }...

}}@Immutable class Msg { ... }

78