Modular Session Types for Objects Simon Gay, Nils Gesbert, Antnio - - PowerPoint PPT Presentation

modular session types for objects
SMART_READER_LITE
LIVE PREVIEW

Modular Session Types for Objects Simon Gay, Nils Gesbert, Antnio - - PowerPoint PPT Presentation

Modular Session Types for Objects Simon Gay, Nils Gesbert, Antnio Ravara, Vasco Vasconcelos University of Glasgow, INRIA Grenoble Rhne-Alpes, Universidade Nova de Lisboa, Universidade de Lisboa 19th April 2011 Gay, Gesbert, Ravara and


slide-1
SLIDE 1

Modular Session Types for Objects

Simon Gay, Nils Gesbert, António Ravara, Vasco Vasconcelos

University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisboa, Universidade de Lisboa

19th April 2011

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 1 / 12

slide-2
SLIDE 2

Session Types for Objects

Several methods available: external choice {hasNext : S, close : S’}

Object branches / Client selects by calling a method

Dependency on a method result: internal choice <OK : S, ERROR : S’>

Object selects by returning a label / Client branches

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 2 / 12

slide-3
SLIDE 3

Session Types for Objects

Several methods available: external choice {hasNext : S, close : S’}

Object branches / Client selects by calling a method

Dependency on a method result: internal choice <OK : S, ERROR : S’>

Object selects by returning a label / Client branches

s e s s i o n I n i t where I n i t = {open : OK: Open , ERROR: I n i t } Open = { hasNext : TRUE: Read , FALSE : Close , c l o s e : I n i t } Read = { read : Open , c l o s e : I n i t } Close = { c l o s e : I n i t }

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 2 / 12

slide-4
SLIDE 4

How it works

Calling a method advances the session type of the object

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 3 / 12

slide-5
SLIDE 5

How it works

Calling a method advances the session type of the object If the continuation is an internal choice, client must switch on the result to resolve it

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 3 / 12

slide-6
SLIDE 6

How it works

Calling a method advances the session type of the object If the continuation is an internal choice, client must switch on the result to resolve it but the result can be stored and switched on later

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 3 / 12

slide-7
SLIDE 7

How it works

Calling a method advances the session type of the object If the continuation is an internal choice, client must switch on the result to resolve it but the result can be stored and switched on later Objects are linear but may be stored in fields of other objects

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 3 / 12

slide-8
SLIDE 8

How it works

Calling a method advances the session type of the object If the continuation is an internal choice, client must switch on the result to resolve it but the result can be stored and switched on later Objects are linear but may be stored in fields of other objects External (abstract) type of an object: session type, S

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 3 / 12

slide-9
SLIDE 9

How it works

Calling a method advances the session type of the object If the continuation is an internal choice, client must switch on the result to resolve it but the result can be stored and switched on later Objects are linear but may be stored in fields of other objects External (abstract) type of an object: session type, S S contains method signatures

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 3 / 12

slide-10
SLIDE 10

How it works

Calling a method advances the session type of the object If the continuation is an internal choice, client must switch on the result to resolve it but the result can be stored and switched on later Objects are linear but may be stored in fields of other objects External (abstract) type of an object: session type, S S contains method signatures Internal state of an object: type of its fields, C[F]

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 3 / 12

slide-11
SLIDE 11

How it works

Calling a method advances the session type of the object If the continuation is an internal choice, client must switch on the result to resolve it but the result can be stored and switched on later Objects are linear but may be stored in fields of other objects External (abstract) type of an object: session type, S S contains method signatures Internal state of an object: type of its fields, C[F] Judgements: Expressions: Γ ∗ r ⊲ e : T ⊳ Γ′ ∗ r′ r = current object

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 3 / 12

slide-12
SLIDE 12

How it works

Calling a method advances the session type of the object If the continuation is an internal choice, client must switch on the result to resolve it but the result can be stored and switched on later Objects are linear but may be stored in fields of other objects External (abstract) type of an object: session type, S S contains method signatures Internal state of an object: type of its fields, C[F] Judgements: Expressions: Γ ∗ r ⊲ e : T ⊳ Γ′ ∗ r′ r = current object For a method body: this : C[F], x : T ′ ∗ this ⊲ e : T ⊳ this : C[F ′] ∗ this

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 3 / 12

slide-13
SLIDE 13

How it works

Calling a method advances the session type of the object If the continuation is an internal choice, client must switch on the result to resolve it but the result can be stored and switched on later Objects are linear but may be stored in fields of other objects External (abstract) type of an object: session type, S S contains method signatures Internal state of an object: type of its fields, C[F] Judgements: Expressions: Γ ∗ r ⊲ e : T ⊳ Γ′ ∗ r′ r = current object For a method body: this : C[F], x : T ′ ∗ this ⊲ e : T ⊳ this : C[F ′] ∗ this Internal/External state compatibility: F ⊢ C : S Coinductively checks method bodies in order

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 3 / 12

slide-14
SLIDE 14

Subtyping

Coinductively defined on sessions: An object with more methods can be safely used in place of an object with less methods An object with less internal choice (more deterministic) can be safely used in place of an object with more internal choice Covariance on result types and continuation session, contravariance on parameter types

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 4 / 12

slide-15
SLIDE 15

Subtyping

Coinductively defined on sessions: An object with more methods can be safely used in place of an object with less methods An object with less internal choice (more deterministic) can be safely used in place of an object with more internal choice Covariance on result types and continuation session, contravariance on parameter types Properties: if F ′ <: F and F ⊢ C : S then F ′ ⊢ C : S if S <: S′ and F ⊢ C : S then F ⊢ C : S′

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 4 / 12

slide-16
SLIDE 16

Link types

If field f has type {T ′ m(T) : l : Sll∈E, . . .}

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 5 / 12

slide-17
SLIDE 17

Link types

If field f has type {T ′ m(T) : l : Sll∈E, . . .} then: the result of f .m(x) is a label l in E this result indicates which state f is in

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 5 / 12

slide-18
SLIDE 18

Link types

If field f has type {T ′ m(T) : l : Sll∈E, . . .} then: the result of f .m(x) is a label l in E this result indicates which state f is in it has type link f

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 5 / 12

slide-19
SLIDE 19

Link types

If field f has type {T ′ m(T) : l : Sll∈E, . . .} then: the result of f .m(x) is a label l in E this result indicates which state f is in it has type link f T ′ is linkthis

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 5 / 12

slide-20
SLIDE 20

Link types

If field f has type {T ′ m(T) : l : Sll∈E, . . .} then: the result of f .m(x) is a label l in E this result indicates which state f is in it has type link f T ′ is linkthis In the body of m, a variant field typing is constructed

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 5 / 12

slide-21
SLIDE 21

Selected Typing Rules

(T-Label) Γ ∗ r ⊲ l : {l} ⊳ Γ ∗ r (T-New) Γ ∗ r ⊲ new C() : C.session ⊳ Γ ∗ r Γ ∗ r ⊲ e : T ′

j ⊳ Γ′ ∗ r′

Γ′(r′.f ) = {Ti mi(T ′

i ) : Si}i∈I

j ∈ I T = link f if Tj = linkthis, T = Tj otherwise (T-Call) Γ ∗ r ⊲ f .mj(e) : T ⊳ Γ′{r′.f → Sj} ∗ r′ Γ ∗ r ⊲ e : link f ⊳ Γ′ ∗ r′ Γ′(r′.f ) = l : Sll∈E ′ E ′ ⊆ E ∀l ∈ E ′, Γ′{r′.f → Sl} ∗ r′ ⊲ el : T ⊳ Γ′′ ∗ r′ (T-SwitchLink) Γ ∗ r ⊲ switch (e) {l : el}l∈E : T ⊳ Γ′′ ∗ r′ Γ ∗ r ⊲ e : E ⊳ Γ′ ∗ r′ Γ′(r′) = C[F ′] F ′ is a record (T-VarF) Γ ∗ r ⊲ e : linkthis ⊳ Γ′{r′ → C[l : F ′l∈E]} ∗ r′ − − → Null f ⊢ C : S (T-Class) ⊢ class C {S; f ; M}

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 6 / 12

slide-22
SLIDE 22

Properties of the sequential system

Subject Reduction

program state = heap, expression, current object: (h ∗ r; e) internal system checks compatibility between Γ and h

Progress

if (h ∗ r; e) is well-typed then either e is a value or (h ∗ r; e) reduces

Conformance

the sequence of method calls on an object is a trace of the declared session of its class

Implementation: Bica (demo by Zua Caldeira at 4:30)

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 7 / 12

slide-23
SLIDE 23

Channels

Translation of channel session types to class session types X = X µX.Σ = µX.Σ ? [T] . Σ = {T receive(Null) : Σ} ! [T] . Σ = {Null send(T) : Σ} & {l : Σl}l∈E = {linkthis receive(Null) : l : Σll∈E} ⊕ {l : Σl}l∈E = {Null send({l}) : Σl}l∈E

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 8 / 12

slide-24
SLIDE 24

Example: communicating with a file server

File server with channel session type:

F i l e Ch an n el = &{OPEN: ? S t r i n g .⊕{OK: CanRead , ERROR: F i l eCh an n el } QUIT : End} CanRead = &{READ: ⊕{EOF: FileChannel , DATA: ! S t r i n g . CanRead } , CLOSE : F i l eCh an n e l }

Translated (client-side) as:

ClientCh = { send ({OPEN} ) : { send ( S t r i n g ) : { r e c e i v e : OK: CanRead , ERROR: ClientCh }} , send ({QUIT } ) : {}} CanRead = {send ({READ} ) : { r e c e i v e : EOF: ClientCh , DATA: { r e c e i v e : CanRead} } , send ({CLOSE} ) : ClientCh }

Would like to expose interface:

s e s s i o n I n i t where I n i t = {open : OK: Open , ERROR: I n i t } Open = { hasNext : TRUE: Read , FALSE : Close , c l o s e : I n i t } Read = { read : Open , c l o s e : I n i t } Close = { c l o s e : I n i t }

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 9 / 12

slide-25
SLIDE 25

Results

Subject Reduction Communication Safety (as with usual binary session types)

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 10 / 12

slide-26
SLIDE 26

F ⊢ C : S

For any class C, we define the relation F ⊢ C : S between field typings F and session types S as the largest relation such that F ⊢ C : S implies: If S ≡ {Ti mi(T ′

i ) : Si}i∈I, then F is not a variant and for all i in I,

there is a definition mi(xi) {ei} in the declaration of class C such that we have F; xi : T ′

i ⊲ ei : Ti ⊳ Fi; ∅ with Fi such that Fi ⊢ C : Si.

If S ≡ l : Sll∈E, then F = l : Fll∈E ′ with E ′ ⊆ E and for any l in E ′ we have Fl ⊢ C : Sl.

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 11 / 12

slide-27
SLIDE 27

Operational semantics

(R-Seq) (h ∗ r; v; e) − → (h ∗ r; e) m(x) {e} ∈ h(r.f ).class (R-Call) (h ∗ r; f .m(v)) − → (h ∗ r.f ; return e{v/x}) (R-Return) (h ∗ r.f ; return v) − → (h ∗ r; v) l0 ∈ E (R-Switch) (h ∗ r; switch (l0) {l : el}l∈E) − → (h ∗ r; el0) h(r).f = v (R-Swap) (h ∗ r; f ↔ v′) − → (h{r.f → v′} ∗ r; v)

  • fresh

C.fields = f (R-New) (h ∗ r; new C()) − → (h, {o = C[ f = − → null]} ∗ r; o) (h ∗ r; e) − → (h′ ∗ r′; e′) (R-Context) (h ∗ r; E[e]) − → (h′ ∗ r′; E[e′])

Gay, Gesbert, Ravara and Vasconcelos (University of Glasgow, INRIA Grenoble – Rhône-Alpes, Universidade Nova de Lisb Modular session types for objects 19th April 2011 12 / 12