introduction to session types

197
Introduction to Session Types Ornela Dardha 1 School of Computing Science University of Glasgow, UK BETTY Summer School 2016 1 UK EPSRC project From Data Types to Session Types: A Basis for Concurrency and Distribution (EP/K034413/1)

Upload: others

Post on 11-Sep-2021

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Session Types

Introduction to Session Types

Ornela Dardha1

School of Computing ScienceUniversity of Glasgow, UK

BETTY Summer School 2016

1UK EPSRC project From Data Types to Session Types: A Basis forConcurrency and Distribution (EP/K034413/1)

Page 2: Introduction to Session Types

Session Types in One Slide

• In complex distributed systems communicating participantsagree on a protocol to follow, specifying type and direction ofdata exchanged.

• Session types are a type formalism used to model structuredcommunication-based programming.

• Guarantee privacy, communication safety and session fidelity.

• Designed for• π- calculus• functional languages• object-oriented languages• binary or multiparty communication• ...

Page 3: Introduction to Session Types

Outline1 Origin of Session Types

2 Session Types by Example

3 Session Types Formally

4 Foundation of Session TypesSession Types and Standard π-calculus TypesSession Types and Linear Logic

5 SubtypingTwo Subtyping Relations for SessionsSubtyping by Encoding

6 Session Types and Programming Languages (I)

7 Multiparty Session Types

8 Session Types and Programming Languages (II)ScribbleMungoStMungoScribble + Mungo + StMungo for typechecking SMTP

9 Advanced Topics

Page 4: Introduction to Session Types

Session Types2

• Session types were born more than 20 years ago.

• The π- calculus is the original and most used framework.

• The seminal works:

• Honda, “Types for Dyadic Interaction”, CONCUR 1993.

• Takeuchi, Honda & Kubo, “An Interaction-Based Languageand its Typing System”, PARLE 1994.

• Honda, Vasconcelos & Kubo, “Language Primitives and TypeDiscipline for Structured Communication-BasedProgramming”, ESOP 1998.

2I thank Simon J. Gay for borrowing some of his slides

Page 5: Introduction to Session Types

Session Types

• Since their appearance, session types have developed into asignificant theme in programming languages.

• Computing has moved from the era of data processing to theera of communication.

• Data types codify the structure of data and make it availableto programming tools.

• Session types codify the structure of communication andmake it available to programming tools.

Page 6: Introduction to Session Types

Outline1 Origin of Session Types

2 Session Types by Example

3 Session Types Formally

4 Foundation of Session TypesSession Types and Standard π-calculus TypesSession Types and Linear Logic

5 SubtypingTwo Subtyping Relations for SessionsSubtyping by Encoding

6 Session Types and Programming Languages (I)

7 Multiparty Session Types

8 Session Types and Programming Languages (II)ScribbleMungoStMungoScribble + Mungo + StMungo for typechecking SMTP

9 Advanced Topics

Page 7: Introduction to Session Types

The Maths Server and Client: Types /Protocols

• The session type of the server’s channel endpoint:

S , & add : ?Int.?Int.!Int.S ,neg : ?Int.!Int.Squit : end

• The session type of the client’s channel endpoint:

C , ⊕ add : !Int.!Int.?Int.C ,neg : !Int.?Int.Cquit : end

Duality: S = C

Page 8: Introduction to Session Types

The Maths Server and Client: Types /Protocols

• The session type of the server’s channel endpoint:

S , & add : ?Int.?Int.!Int.S ,neg : ?Int.!Int.Squit : end

• The session type of the client’s channel endpoint:

C , ⊕ add : !Int.!Int.?Int.C ,neg : !Int.?Int.Cquit : end

Duality: S = C

Page 9: Introduction to Session Types

The Maths Server and Client: Types /Protocols

• The session type of the server’s channel endpoint:

S , & add : ?Int.?Int.!Int.S ,neg : ?Int.!Int.Squit : end

• The session type of the client’s channel endpoint:

C , ⊕ add : !Int.!Int.?Int.C ,neg : !Int.?Int.Cquit : end

Duality: S = C

Page 10: Introduction to Session Types

The Maths Server and Client: Types /Protocols

Legend

• &: branch/offer/external choice;

• ⊕: select/internal choice;

• ?Int.T : input Int, continue as T ;

• !Int.T : output Int, continue as T ;

• “·” indicates sequencing;

• add, neg, quit: choice labels, all different;

• end marks the end of the protocol.

Page 11: Introduction to Session Types

The Maths Server: Program and Type

A server srv, parametrised in its channel endpoint x of type S :

srv(x : S) = x . add : x?(a : Int).x?(b : Int).x!〈a + b〉.srv(x),neg : x?(a : Int).x!〈−a〉.srv(x)quit : 0

S = & add : ?Int.?Int.!Int.S ,neg : ?Int.!Int.Squit : end

Page 12: Introduction to Session Types

The Maths Client: Program and Type

A client clt, parametrised in its channel endpoint x of type C :

clt(x : C ) = x /neg .x!〈2〉.x?(a : Int).x / quit.P(a)

C = ⊕ add : !Int.!Int.?Int.C ,neg : !Int.?Int.Cquit : end

Page 13: Introduction to Session Types

Client/Server Interaction(π- calculus OS)

(νc : S)(srv(c+) | clt(c−))

↓(νc : ?Int.!Int.S)(c+?(a : Int).c+!〈−a〉.srv(c+) | c−!〈2〉.c−?(a : Int).c− / quit.P(a))

↓(νc : !Int.S)(c+!〈−2〉.srv(c+) | c−?(a : Int).c− / quit.P(a))

↓(νc : S)(srv(c+) | c− / quit.P(−2))

↓(νc : end)(0 | P(−2))

≡P(−2)

Page 14: Introduction to Session Types

Client/Server Interaction(π- calculus OS)

(νc : S)(srv(c+) | clt(c−))↓

(νc : ?Int.!Int.S)(c+?(a : Int).c+!〈−a〉.srv(c+) | c−!〈2〉.c−?(a : Int).c− / quit.P(a))

↓(νc : !Int.S)(c+!〈−2〉.srv(c+) | c−?(a : Int).c− / quit.P(a))

↓(νc : S)(srv(c+) | c− / quit.P(−2))

↓(νc : end)(0 | P(−2))

≡P(−2)

Page 15: Introduction to Session Types

Client/Server Interaction(π- calculus OS)

(νc : S)(srv(c+) | clt(c−))↓

(νc : ?Int.!Int.S)(c+?(a : Int).c+!〈−a〉.srv(c+) | c−!〈2〉.c−?(a : Int).c− / quit.P(a))↓

(νc : !Int.S)(c+!〈−2〉.srv(c+) | c−?(a : Int).c− / quit.P(a))

↓(νc : S)(srv(c+) | c− / quit.P(−2))

↓(νc : end)(0 | P(−2))

≡P(−2)

Page 16: Introduction to Session Types

Client/Server Interaction(π- calculus OS)

(νc : S)(srv(c+) | clt(c−))↓

(νc : ?Int.!Int.S)(c+?(a : Int).c+!〈−a〉.srv(c+) | c−!〈2〉.c−?(a : Int).c− / quit.P(a))↓

(νc : !Int.S)(c+!〈−2〉.srv(c+) | c−?(a : Int).c− / quit.P(a))↓

(νc : S)(srv(c+) | c− / quit.P(−2))

↓(νc : end)(0 | P(−2))

≡P(−2)

Page 17: Introduction to Session Types

Client/Server Interaction(π- calculus OS)

(νc : S)(srv(c+) | clt(c−))↓

(νc : ?Int.!Int.S)(c+?(a : Int).c+!〈−a〉.srv(c+) | c−!〈2〉.c−?(a : Int).c− / quit.P(a))↓

(νc : !Int.S)(c+!〈−2〉.srv(c+) | c−?(a : Int).c− / quit.P(a))↓

(νc : S)(srv(c+) | c− / quit.P(−2))↓

(νc : end)(0 | P(−2))

≡P(−2)

Page 18: Introduction to Session Types

Client/Server Interaction(π- calculus OS)

(νc : S)(srv(c+) | clt(c−))↓

(νc : ?Int.!Int.S)(c+?(a : Int).c+!〈−a〉.srv(c+) | c−!〈2〉.c−?(a : Int).c− / quit.P(a))↓

(νc : !Int.S)(c+!〈−2〉.srv(c+) | c−?(a : Int).c− / quit.P(a))↓

(νc : S)(srv(c+) | c− / quit.P(−2))↓

(νc : end)(0 | P(−2))≡

P(−2)

Page 19: Introduction to Session Types

Establishing a Connection

• The server listens on a standard channel a of type ]S , andreceives a session channel for srv to use.

server(a) = a?(x : S).srv(x)

• The global declaration a : ]S advertises the server and itsprotocol.

• The client creates a session channel and sends it to the server.

client(a) = (νc : S)(a!〈c+〉.clt(c−))

• After one step, execution proceeds as before.

Page 20: Introduction to Session Types

Establishing a Connection

• The server listens on a standard channel a of type ]S , andreceives a session channel for srv to use.

server(a) = a?(x : S).srv(x)

• The global declaration a : ]S advertises the server and itsprotocol.

• The client creates a session channel and sends it to the server.

client(a) = (νc : S)(a!〈c+〉.clt(c−))

• After one step, execution proceeds as before.

Page 21: Introduction to Session Types

Establishing a Connection

• The server listens on a standard channel a of type ]S , andreceives a session channel for srv to use.

server(a) = a?(x : S).srv(x)

• The global declaration a : ]S advertises the server and itsprotocol.

• The client creates a session channel and sends it to the server.

client(a) = (νc : S)(a!〈c+〉.clt(c−))

• After one step, execution proceeds as before.

Page 22: Introduction to Session Types

Establishing a Connection

• The server listens on a standard channel a of type ]S , andreceives a session channel for srv to use.

server(a) = a?(x : S).srv(x)

• The global declaration a : ]S advertises the server and itsprotocol.

• The client creates a session channel and sends it to the server.

client(a) = (νc : S)(a!〈c+〉.clt(c−))

• After one step, execution proceeds as before.

Page 23: Introduction to Session Types

Outline1 Origin of Session Types

2 Session Types by Example

3 Session Types Formally

4 Foundation of Session TypesSession Types and Standard π-calculus TypesSession Types and Linear Logic

5 SubtypingTwo Subtyping Relations for SessionsSubtyping by Encoding

6 Session Types and Programming Languages (I)

7 Multiparty Session Types

8 Session Types and Programming Languages (II)ScribbleMungoStMungoScribble + Mungo + StMungo for typechecking SMTP

9 Advanced Topics

Page 24: Introduction to Session Types

Session Types: Key Features

• Duality: the relationship between the types of oppositeendpoints of a session channel.

• Linearity: each channel endpoint occurs exactly once in acollection of parallel processes.

• The structure of session types matches the structure ofcommunication.

• Session types change as communication occurs.

• Connection is established among participants.

Page 25: Introduction to Session Types

Session Types: Key Features

• Duality: the relationship between the types of oppositeendpoints of a session channel.

• Linearity: each channel endpoint occurs exactly once in acollection of parallel processes.

• The structure of session types matches the structure ofcommunication.

• Session types change as communication occurs.

• Connection is established among participants.

Page 26: Introduction to Session Types

Session Types: Key Features

• Duality: the relationship between the types of oppositeendpoints of a session channel.

• Linearity: each channel endpoint occurs exactly once in acollection of parallel processes.

• The structure of session types matches the structure ofcommunication.

• Session types change as communication occurs.

• Connection is established among participants.

Page 27: Introduction to Session Types

Session Types: Key Features

• Duality: the relationship between the types of oppositeendpoints of a session channel.

• Linearity: each channel endpoint occurs exactly once in acollection of parallel processes.

• The structure of session types matches the structure ofcommunication.

• Session types change as communication occurs.

• Connection is established among participants.

Page 28: Introduction to Session Types

Session Types: Key Features

• Duality: the relationship between the types of oppositeendpoints of a session channel.

• Linearity: each channel endpoint occurs exactly once in acollection of parallel processes.

• The structure of session types matches the structure ofcommunication.

• Session types change as communication occurs.

• Connection is established among participants.

Page 29: Introduction to Session Types

Properties of Session Types

• Communication Safety: the exchanged data has the expectedtype.

• Session Fidelity: the session channel has the expectedstructure.

• Privacy: the session channel is owned only by thecommunicating parties.

Main Theorem: at runtime, communication follows the protocol.

Page 30: Introduction to Session Types

Properties of Session Types

• Communication Safety: the exchanged data has the expectedtype.

• Session Fidelity: the session channel has the expectedstructure.

• Privacy: the session channel is owned only by thecommunicating parties.

Main Theorem: at runtime, communication follows the protocol.

Page 31: Introduction to Session Types

Properties of Session Types

• Communication Safety: the exchanged data has the expectedtype.

• Session Fidelity: the session channel has the expectedstructure.

• Privacy: the session channel is owned only by thecommunicating parties.

Main Theorem: at runtime, communication follows the protocol.

Page 32: Introduction to Session Types

Properties of Session Types

• Communication Safety: the exchanged data has the expectedtype.

• Session Fidelity: the session channel has the expectedstructure.

• Privacy: the session channel is owned only by thecommunicating parties.

Main Theorem: at runtime, communication follows the protocol.

Page 33: Introduction to Session Types

The Calculus and Typing Rules

Page 34: Introduction to Session Types

The Calculus: Types

S ::= end termination!T .S send?T .S receive⊕li : Sii∈I select&li : Sii∈I branch

T ::= S session typeBool boolean type]T standard channel type. . . other type constructs

Page 35: Introduction to Session Types

The Calculus: Terms

P,Q ::= 0 inactionP | Q composition(νx)P restrictionxp!〈vq〉.P outputxp?(y).P inputxp / lj .P selectionxp . li : Pii∈I branching

v ::= x , y channeltrue | false boolean values. . . other values

p, q are optional polarities for channels, being + or −

Page 36: Introduction to Session Types

Typing Rules

(T-Par)

Γ1 ` P Γ2 ` Q

Γ1 + Γ2 ` P | Q

(T-Res)

Γ, x+ : S , x− : S ` P

Γ ` (νx)P

(T-In)

Γ, xp : S , y : T ` P

Γ, xp : ?T .S ` xp?(y).P

(T-Out)

Γ1, xp : S ` P Γ2 ` vq : T

(Γ1, xp : !T .S) + Γ2 ` xp!〈vq〉.P

(T-Brch)

Γ, xp : Si ` Pi ∀i ∈ I

Γ, xp : &li : Sii∈I ` xp . li : Pii∈I

(T-Sel)

Γ, xp : Sj ` P j ∈ I

Γ, xp : ⊕li : Sii∈I ` xp / lj .P

Gay & Hole, “Subtyping for Session Types in the Pi Calculus”.ESOP 1999, Acta Informatica 2005.

Page 37: Introduction to Session Types

Typing Rules

(T-Par)

Γ1 ` P Γ2 ` Q

Γ1 + Γ2 ` P | Q

(T-Res)

Γ, x+ : S , x− : S ` P

Γ ` (νx)P

(T-In)

Γ, xp : S , y : T ` P

Γ, xp : ?T .S ` xp?(y).P

(T-Out)

Γ1, xp : S ` P Γ2 ` vq : T

(Γ1, xp : !T .S) + Γ2 ` xp!〈vq〉.P

(T-Brch)

Γ, xp : Si ` Pi ∀i ∈ I

Γ, xp : &li : Sii∈I ` xp . li : Pii∈I

(T-Sel)

Γ, xp : Sj ` P j ∈ I

Γ, xp : ⊕li : Sii∈I ` xp / lj .P

Gay & Hole, “Subtyping for Session Types in the Pi Calculus”.ESOP 1999, Acta Informatica 2005.

Page 38: Introduction to Session Types

Combination of Typing Contexts

Γ + x+ : S = Γ, x+ : S if x , x+ /∈ dom(Γ)

Γ + x− : S = Γ, x− : S if x , x− /∈ dom(Γ)

Γ + x : T = Γ, x : T if x , x+, x− /∈ dom(Γ)

(Γ, x : T ) + x : T = Γ, x : T if T is not a session type

Page 39: Introduction to Session Types

Exercise: Is it well typed?

(νx)(x+?(t : Bool).0 | x−!〈true〉.0)

X

(νx)(x+!〈t〉.0 | x−!〈true〉.0) ×

(νx)(x−!〈false〉.0 | x+?(t : Bool).0 | x+?(w : Bool).0) ×

(νx)(νy)(y−!〈42〉.x+?(z : Int).0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(νy)(x+?(z : Int).y−!〈42〉.0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(x− / k.0 | x+ . li : Pii∈I .0) ×

Page 40: Introduction to Session Types

Exercise: Is it well typed?

(νx)(x+?(t : Bool).0 | x−!〈true〉.0) X

(νx)(x+!〈t〉.0 | x−!〈true〉.0) ×

(νx)(x−!〈false〉.0 | x+?(t : Bool).0 | x+?(w : Bool).0) ×

(νx)(νy)(y−!〈42〉.x+?(z : Int).0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(νy)(x+?(z : Int).y−!〈42〉.0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(x− / k.0 | x+ . li : Pii∈I .0) ×

Page 41: Introduction to Session Types

Exercise: Is it well typed?

(νx)(x+?(t : Bool).0 | x−!〈true〉.0) X

(νx)(x+!〈t〉.0 | x−!〈true〉.0)

×

(νx)(x−!〈false〉.0 | x+?(t : Bool).0 | x+?(w : Bool).0) ×

(νx)(νy)(y−!〈42〉.x+?(z : Int).0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(νy)(x+?(z : Int).y−!〈42〉.0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(x− / k.0 | x+ . li : Pii∈I .0) ×

Page 42: Introduction to Session Types

Exercise: Is it well typed?

(νx)(x+?(t : Bool).0 | x−!〈true〉.0) X

(νx)(x+!〈t〉.0 | x−!〈true〉.0) ×

(νx)(x−!〈false〉.0 | x+?(t : Bool).0 | x+?(w : Bool).0) ×

(νx)(νy)(y−!〈42〉.x+?(z : Int).0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(νy)(x+?(z : Int).y−!〈42〉.0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(x− / k.0 | x+ . li : Pii∈I .0) ×

Page 43: Introduction to Session Types

Exercise: Is it well typed?

(νx)(x+?(t : Bool).0 | x−!〈true〉.0) X

(νx)(x+!〈t〉.0 | x−!〈true〉.0) ×

(νx)(x−!〈false〉.0 | x+?(t : Bool).0 | x+?(w : Bool).0)

×

(νx)(νy)(y−!〈42〉.x+?(z : Int).0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(νy)(x+?(z : Int).y−!〈42〉.0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(x− / k.0 | x+ . li : Pii∈I .0) ×

Page 44: Introduction to Session Types

Exercise: Is it well typed?

(νx)(x+?(t : Bool).0 | x−!〈true〉.0) X

(νx)(x+!〈t〉.0 | x−!〈true〉.0) ×

(νx)(x−!〈false〉.0 | x+?(t : Bool).0 | x+?(w : Bool).0) ×

(νx)(νy)(y−!〈42〉.x+?(z : Int).0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(νy)(x+?(z : Int).y−!〈42〉.0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(x− / k.0 | x+ . li : Pii∈I .0) ×

Page 45: Introduction to Session Types

Exercise: Is it well typed?

(νx)(x+?(t : Bool).0 | x−!〈true〉.0) X

(νx)(x+!〈t〉.0 | x−!〈true〉.0) ×

(νx)(x−!〈false〉.0 | x+?(t : Bool).0 | x+?(w : Bool).0) ×

(νx)(νy)(y−!〈42〉.x+?(z : Int).0 | x−!〈11〉.y+?(w : Int).0)

X

(νx)(νy)(x+?(z : Int).y−!〈42〉.0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(x− / k.0 | x+ . li : Pii∈I .0) ×

Page 46: Introduction to Session Types

Exercise: Is it well typed?

(νx)(x+?(t : Bool).0 | x−!〈true〉.0) X

(νx)(x+!〈t〉.0 | x−!〈true〉.0) ×

(νx)(x−!〈false〉.0 | x+?(t : Bool).0 | x+?(w : Bool).0) ×

(νx)(νy)(y−!〈42〉.x+?(z : Int).0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(νy)(x+?(z : Int).y−!〈42〉.0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(x− / k.0 | x+ . li : Pii∈I .0) ×

Page 47: Introduction to Session Types

Exercise: Is it well typed?

(νx)(x+?(t : Bool).0 | x−!〈true〉.0) X

(νx)(x+!〈t〉.0 | x−!〈true〉.0) ×

(νx)(x−!〈false〉.0 | x+?(t : Bool).0 | x+?(w : Bool).0) ×

(νx)(νy)(y−!〈42〉.x+?(z : Int).0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(νy)(x+?(z : Int).y−!〈42〉.0 | x−!〈11〉.y+?(w : Int).0)

X

(νx)(x− / k.0 | x+ . li : Pii∈I .0) ×

Page 48: Introduction to Session Types

Exercise: Is it well typed?

(νx)(x+?(t : Bool).0 | x−!〈true〉.0) X

(νx)(x+!〈t〉.0 | x−!〈true〉.0) ×

(νx)(x−!〈false〉.0 | x+?(t : Bool).0 | x+?(w : Bool).0) ×

(νx)(νy)(y−!〈42〉.x+?(z : Int).0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(νy)(x+?(z : Int).y−!〈42〉.0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(x− / k.0 | x+ . li : Pii∈I .0) ×

Page 49: Introduction to Session Types

Exercise: Is it well typed?

(νx)(x+?(t : Bool).0 | x−!〈true〉.0) X

(νx)(x+!〈t〉.0 | x−!〈true〉.0) ×

(νx)(x−!〈false〉.0 | x+?(t : Bool).0 | x+?(w : Bool).0) ×

(νx)(νy)(y−!〈42〉.x+?(z : Int).0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(νy)(x+?(z : Int).y−!〈42〉.0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(x− / k.0 | x+ . li : Pii∈I .0)

×

Page 50: Introduction to Session Types

Exercise: Is it well typed?

(νx)(x+?(t : Bool).0 | x−!〈true〉.0) X

(νx)(x+!〈t〉.0 | x−!〈true〉.0) ×

(νx)(x−!〈false〉.0 | x+?(t : Bool).0 | x+?(w : Bool).0) ×

(νx)(νy)(y−!〈42〉.x+?(z : Int).0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(νy)(x+?(z : Int).y−!〈42〉.0 | x−!〈11〉.y+?(w : Int).0) X

(νx)(x− / k.0 | x+ . li : Pii∈I .0) ×

Page 51: Introduction to Session Types

Outline1 Origin of Session Types

2 Session Types by Example

3 Session Types Formally

4 Foundation of Session TypesSession Types and Standard π-calculus TypesSession Types and Linear Logic

5 SubtypingTwo Subtyping Relations for SessionsSubtyping by Encoding

6 Session Types and Programming Languages (I)

7 Multiparty Session Types

8 Session Types and Programming Languages (II)ScribbleMungoStMungoScribble + Mungo + StMungo for typechecking SMTP

9 Advanced Topics

Page 52: Introduction to Session Types

Outline1 Origin of Session Types

2 Session Types by Example

3 Session Types Formally

4 Foundation of Session TypesSession Types and Standard π-calculus TypesSession Types and Linear Logic

5 SubtypingTwo Subtyping Relations for SessionsSubtyping by Encoding

6 Session Types and Programming Languages (I)

7 Multiparty Session Types

8 Session Types and Programming Languages (II)ScribbleMungoStMungoScribble + Mungo + StMungo for typechecking SMTP

9 Advanced Topics

Page 53: Introduction to Session Types

Research Timeline

Milner, Parrow, Walker 1989/1992

Honda 1993

Takeuchi, Honda, Kubo 1994

Honda, Vasconcelos, Kubo 1998

To be continued...

Milner 1993

Pierce, Sangiorgi 1993

Kobayashi, Pierce, Turner 1996

Sangiorgi 1998

Page 54: Introduction to Session Types

On standard types for π- calculus

• ]T : channel used in input/output to transmit data of type T .

• iT/oT : channel used only in input/output to transmit data oftype T . [PS93]

• `iT/`oT : channel used only in input/output and exactly onceto transmit data of type T . [KPT96]

• 〈li : Ti 〉i∈I : labelled disjoint union of types. [Sangio98]

Page 55: Introduction to Session Types

On standard types for π- calculus

• ]T : channel used in input/output to transmit data of type T .

• iT/oT : channel used only in input/output to transmit data oftype T . [PS93]

• `iT/`oT : channel used only in input/output and exactly onceto transmit data of type T . [KPT96]

• 〈li : Ti 〉i∈I : labelled disjoint union of types. [Sangio98]

Page 56: Introduction to Session Types

On standard types for π- calculus

• ]T : channel used in input/output to transmit data of type T .

• iT/oT : channel used only in input/output to transmit data oftype T . [PS93]

• `iT/`oT : channel used only in input/output and exactly onceto transmit data of type T . [KPT96]

• 〈li : Ti 〉i∈I : labelled disjoint union of types. [Sangio98]

Page 57: Introduction to Session Types

On standard types for π- calculus

• ]T : channel used in input/output to transmit data of type T .

• iT/oT : channel used only in input/output to transmit data oftype T . [PS93]

• `iT/`oT : channel used only in input/output and exactly onceto transmit data of type T . [KPT96]

• 〈li : Ti 〉i∈I : labelled disjoint union of types. [Sangio98]

Page 58: Introduction to Session Types

Key words for standard π- types

For session-typed π- calculus:

1 Structure

2 Duality

3 Restriction

4 Branch/Select

1 Linearity forces a π channel to be used exactly once.

2 Capability of input/output of the same π channel splitbetween two partners.

3 Restriction construct permits the creation of fresh private πchannels.

4 Variant type permits choice.

Page 59: Introduction to Session Types

Key words for standard π- types

For session-typed π- calculus:

1 Structure

2 Duality

3 Restriction

4 Branch/Select

1 Linearity forces a π channel to be used exactly once.

2 Capability of input/output of the same π channel splitbetween two partners.

3 Restriction construct permits the creation of fresh private πchannels.

4 Variant type permits choice.

Page 60: Introduction to Session Types

Bridging the two worlds

To which extent session constructs are more complex and moreexpressive than the standard π- calculus constructs?

Page 61: Introduction to Session Types

Research Timeline

Milner, Parrow, Walker 1989/1992

Kobayashi 2007

Gay, Gesbert, Ravara 2008

Demangeon, Honda 2011

Dardha, Giachino, Sangiorgi 2012

Dardha 2014

Honda 1993

Takeuchi, Honda, Kubo 1994

Honda, Vasconcelos, Kubo 1998

To be continued...

Milner 1993

Pierce, Sangiorgi 1993

Kobayashi, Pierce, Turner 1996

Sangiorgi 1998

Page 62: Introduction to Session Types

Research Timeline

Milner, Parrow, Walker 1989/1992

Kobayashi 2007

Dardha, Giachino, Sangiorgi 2012

Dardha 2014

Honda 1993

Takeuchi, Honda, Kubo 1994

Honda, Vasconcelos, Kubo 1998

To be continued...

Milner 1993

Pierce, Sangiorgi 1993

Sangiorgi 1998

Kobayashi, Pierce, Turner 1996

Page 63: Introduction to Session Types

Milner, Parrow and Walker 1989/1992A calculus of mobile processes

Kobayashi 2007Type systems for concurrent programs

Gay, Gesbert and Ravara 2008Session types as generic process types

Demangeon and Honda 2011Full abstraction in a subtyped pi-calculus

with linear types

Dardha, Giachino and Sangiorgi 2012Session types revisited

Dardha 2014Recursive session types revisited

Honda 1993Types for dyadic interaction

Takeuchi, Honda and Kubo 1994An interaction based language and its typing system

Honda, Vasconcelos and Kubo 1998Language primitives and type discipline for

structured communication-based programming

To be continued...

Milner 1993The polyadic pi-calculus: a tutorial

Pierce and Sangiorgi 1993Typing and subtyping for mobile processes

Sangiorgi 1998An interpretation of typed objects

into typed pi-calculus

Kobayashi, Pierce and Turner 1996Linearity and the pi-calculus

Page 64: Introduction to Session Types

Key idea of the encoding

Encoding is based on:

1 Linearity of π- calculus channel types;

2 Input/Output channel capabilities;

3 Continuation-Passing principle.

4 Variant types for the π- calculus.

Page 65: Introduction to Session Types

Intuition of the encoding

• Session types are encoded as linear channel types.

• ? and ! are encoded as `i and `o .

• &li : Sii∈I and ⊕li : Sii∈I are encoded using varianttypes.

• Continuation of a session type becomes carried type.

• Dual operations in continuation become equal when carried.

Page 66: Introduction to Session Types

Why is this interesting?

Benefits of the encoding:

1 Large reusability of standard typed π- calculus theory.

2 Derivation of properties for session π- calculus from thestandard typed π- calculus. (e.g. SR, TS)

3 Elimination of redundancy in the syntax of types and termsand in the theory.

4 Encoding is robust (subtyping, polymorphism, higher-order).

5 Expressivity result for session types.

Page 67: Introduction to Session Types

Encoding Finite Session Types: Example

LetS = ?Int.?Int.!Bool.end

ThenJSK = `i [Int, `i [Int, `o [Bool, ∅[]]]]

Page 68: Introduction to Session Types

Encoding Finite Session Types: Example

LetS = ?Int.?Int.!Bool.end

ThenJSK = `i [Int, `i [Int, `o [Bool, ∅[]]]]

Page 69: Introduction to Session Types

Encoding Finite Session Types: Example

LetS = ?Int.?Int.!Bool.end

ThenJSK = `i [Int, `i [Int, `o [Bool, ∅[]]]]

Page 70: Introduction to Session Types

Encoding Finite Session Types: Example

LetS = ?Int.?Int.!Bool.end

ThenJSK = `i [Int, `i [Int, `o [Bool, ∅[]]]]

Page 71: Introduction to Session Types

Encoding Finite Session Types: Example

LetS = ?Int.?Int.!Bool.end

ThenJSK = `i [Int, `i [Int, `o [Bool, ∅[]]]]

Page 72: Introduction to Session Types

Encoding Finite Session Types: Example

LetS = !Int.!Int.?Bool.end

ThenJSK = `o [Int, `i [Int, `o [Bool, ∅[]]]]

Page 73: Introduction to Session Types

Remark

The encoding of dual types is as follows:

JSK = `i [Int, `i [Int, `o [Bool, ∅[]]]]

andJSK = `o [Int, `i [Int, `o [Bool, ∅[]]]]

Remarkduality on session types boils down to opposite capabilities (i/o) ofchannel types, only in the outermost level!

Page 74: Introduction to Session Types

Remark

The encoding of dual types is as follows:

JSK = `i [Int, `i [Int, `o [Bool, ∅[]]]]

andJSK = `o [Int, `i [Int, `o [Bool, ∅[]]]]

Remarkduality on session types boils down to opposite capabilities (i/o) ofchannel types, only in the outermost level!

Page 75: Introduction to Session Types

Encoding of Session Types: Formally

JendK , ∅[]J!T .SK , `o [JT K, JSK]

J?T .SK , `i [JT K, JSK]

J⊕li : Sii∈I K , `o [〈li : JSiK〉i∈I ]J&li : Sii∈I K , `i [〈li : JSiK〉i∈I ]

Page 76: Introduction to Session Types

Properties of the Encoding

TheoremEncoding preserves typability of programs.

TheoremEncoding preserves evaluation of programs.

LemmaEncoding of dual session types gives dual linear π- types.

Page 77: Introduction to Session Types

Outline1 Origin of Session Types

2 Session Types by Example

3 Session Types Formally

4 Foundation of Session TypesSession Types and Standard π-calculus TypesSession Types and Linear Logic

5 SubtypingTwo Subtyping Relations for SessionsSubtyping by Encoding

6 Session Types and Programming Languages (I)

7 Multiparty Session Types

8 Session Types and Programming Languages (II)ScribbleMungoStMungoScribble + Mungo + StMungo for typechecking SMTP

9 Advanced Topics

Page 78: Introduction to Session Types

Propositions as Types3

propositions as typesproofs as programs

normalisation of proofs as evaluation of programs

Intuitionistic Natural Deduction ↔ Simply-Typed Lambda CalculusQuantification over propositions ↔ Polymorphism

Modal Logical ↔ Monads (state, exceptions)

3I thank Phil Wadler for these two slides!

Page 79: Introduction to Session Types

Propositions as Types

propositions as typesproofs as programs

normalisation of proofs as evaluation of programs

Intuitionistic Natural Deduction ↔ Simply-Typed Lambda CalculusQuantification over propositions ↔ Polymorphism

Modal Logical ↔ Monads (state, exceptions)

??? ↔ Process Calculus

Page 80: Introduction to Session Types

Session Types and Linear Logic (1)

• What is the Curry-Howard correspondence for concurrency?

• Caires & Pfenning (2010) established a correspondencebetween intuitionistic linear logic and session typed π- calculus.

• Later on, Wadler (2012) established a correspondencebetween classical linear logic and session typed π- calculus.

Page 81: Introduction to Session Types

Session Types and Linear Logic (1)

• What is the Curry-Howard correspondence for concurrency?

• Caires & Pfenning (2010) established a correspondencebetween intuitionistic linear logic and session typed π- calculus.

• Later on, Wadler (2012) established a correspondencebetween classical linear logic and session typed π- calculus.

Page 82: Introduction to Session Types

Session Types and Linear Logic (1)

• What is the Curry-Howard correspondence for concurrency?

• Caires & Pfenning (2010) established a correspondencebetween intuitionistic linear logic and session typed π- calculus.

• Later on, Wadler (2012) established a correspondencebetween classical linear logic and session typed π- calculus.

Page 83: Introduction to Session Types

Session Types and Linear Logic (2)

propositions as session typesproofs as π- processes

cut reduction as communication

Page 84: Introduction to Session Types

Session Types and Classical Linear Logic(1)

• A O B is interpreted as “input A then behave like B” (?A.B)

• A⊗ B is interpreted as “output A then behave like B” (!A.B)

• & and ⊕ are interpreted as branch and select.

• The correspondence has led to a re-examination of all aspectsof session types, from a logical viewpoint.

Page 85: Introduction to Session Types

Session Types and Classical Linear Logic(1)

• A O B is interpreted as “input A then behave like B” (?A.B)

• A⊗ B is interpreted as “output A then behave like B” (!A.B)

• & and ⊕ are interpreted as branch and select.

• The correspondence has led to a re-examination of all aspectsof session types, from a logical viewpoint.

Page 86: Introduction to Session Types

Session Types and Classical Linear Logic(1)

• A O B is interpreted as “input A then behave like B” (?A.B)

• A⊗ B is interpreted as “output A then behave like B” (!A.B)

• & and ⊕ are interpreted as branch and select.

• The correspondence has led to a re-examination of all aspectsof session types, from a logical viewpoint.

Page 87: Introduction to Session Types

Session Types and Classical Linear Logic(1)

• A O B is interpreted as “input A then behave like B” (?A.B)

• A⊗ B is interpreted as “output A then behave like B” (!A.B)

• & and ⊕ are interpreted as branch and select.

• The correspondence has led to a re-examination of all aspectsof session types, from a logical viewpoint.

Page 88: Introduction to Session Types

Session Types and Classical Linear Logic(2)

Wadler 2012; Caires 2014 (@Luca Cardelli Fest)

(T-O)

P ` ∆, y :A, x :B

x?(y).P ` ∆, x :A O B

(T-⊗)P ` ∆, y :A Q ` ∆′, x :B

x!(y).(P | Q) ` ∆,∆′, x :A⊗ B

(T-cut)

P ` ∆, x :A Q ` ∆′, x :A

(νx)(P | Q) ` ∆,∆′

(T-&)

Pi ` ∆, x :Ai ∀i ∈ I

x . li : Pii∈I ` ∆, x :&li : Aii∈I

(T-⊕)P ` ∆, x :Aj j ∈ I

x / lj .P ` ∆, x :⊕li : Aii∈I

Page 89: Introduction to Session Types

Outline1 Origin of Session Types

2 Session Types by Example

3 Session Types Formally

4 Foundation of Session TypesSession Types and Standard π-calculus TypesSession Types and Linear Logic

5 SubtypingTwo Subtyping Relations for SessionsSubtyping by Encoding

6 Session Types and Programming Languages (I)

7 Multiparty Session Types

8 Session Types and Programming Languages (II)ScribbleMungoStMungoScribble + Mungo + StMungo for typechecking SMTP

9 Advanced Topics

Page 90: Introduction to Session Types

Defining subtyping 4

S ≤ T

• Safe Substitutability (cf Liskov & Wing 1994): “it is safe touse a value of type S where a value of type T is expected”.

• ...Meaning: No violation of the runtime safety that the typesystem guarantees.

• Set Inclusion: in semantic subtyping (cf Castagna et al.)

JSK ⊆ JT K

• Property Preservation: (cf Liskov & Wing 1994)

∀φ. (∀x : T . φ(x)) =⇒ (∀y : S . φ(y))

4I thank Luca Padovani for borrowing these two slides

Page 91: Introduction to Session Types

Defining subtyping 4

S ≤ T

• Safe Substitutability (cf Liskov & Wing 1994): “it is safe touse a value of type S where a value of type T is expected”.

• ...Meaning: No violation of the runtime safety that the typesystem guarantees.

• Set Inclusion: in semantic subtyping (cf Castagna et al.)

JSK ⊆ JT K

• Property Preservation: (cf Liskov & Wing 1994)

∀φ. (∀x : T . φ(x)) =⇒ (∀y : S . φ(y))

4I thank Luca Padovani for borrowing these two slides

Page 92: Introduction to Session Types

Defining subtyping 4

S ≤ T

• Safe Substitutability (cf Liskov & Wing 1994): “it is safe touse a value of type S where a value of type T is expected”.

• ...Meaning: No violation of the runtime safety that the typesystem guarantees.

• Set Inclusion: in semantic subtyping (cf Castagna et al.)

JSK ⊆ JT K

• Property Preservation: (cf Liskov & Wing 1994)

∀φ. (∀x : T . φ(x)) =⇒ (∀y : S . φ(y))

4I thank Luca Padovani for borrowing these two slides

Page 93: Introduction to Session Types

Defining subtyping 4

S ≤ T

• Safe Substitutability (cf Liskov & Wing 1994): “it is safe touse a value of type S where a value of type T is expected”.

• ...Meaning: No violation of the runtime safety that the typesystem guarantees.

• Set Inclusion: in semantic subtyping (cf Castagna et al.)

JSK ⊆ JT K

• Property Preservation: (cf Liskov & Wing 1994)

∀φ. (∀x : T . φ(x)) =⇒ (∀y : S . φ(y))

4I thank Luca Padovani for borrowing these two slides

Page 94: Introduction to Session Types

Examples

• Set Inclusion:

Even ≤ Int if and only if JEvenK ≤ JIntK

• Property Preservation:

x : Int, y : Int, c : Color ≤ x : Int, y : Int

• φ(Point) = “Point has an x field”.• φ(Point) = “Point has an y field”.

Page 95: Introduction to Session Types

Outline1 Origin of Session Types

2 Session Types by Example

3 Session Types Formally

4 Foundation of Session TypesSession Types and Standard π-calculus TypesSession Types and Linear Logic

5 SubtypingTwo Subtyping Relations for SessionsSubtyping by Encoding

6 Session Types and Programming Languages (I)

7 Multiparty Session Types

8 Session Types and Programming Languages (II)ScribbleMungoStMungoScribble + Mungo + StMungo for typechecking SMTP

9 Advanced Topics

Page 96: Introduction to Session Types

The *Old* Maths Server and Client

• The session type of the server’s channel endpoint:

Sold , & add : ?Int.?Int.!Int.Sold ,neg : ?Int.!Int.Sold

quit : end

• The session type of the client’s channel endpoint:

Cold , ⊕ add : !Int.!Int.?Int.Cold ,neg : !Int.?Int.Cold

quit : end

Page 97: Introduction to Session Types

The *New* Maths Server and Client

• The session type of the server’s channel endpoint:

Snew , & mul : ?Int.?Int.!Int.Snew ,add : ?Int.?Int.!Int.Snew ,neg : ?Int.!Int.Snew

quit : end

• The session type of the client’s channel endpoint:

Cnew , ⊕ add : !Int.!Int.?Int.Cnew ,quit : end

Page 98: Introduction to Session Types

The *New* Maths Server and Client

• The session type of the server’s channel endpoint:

Snew , & mul : ?Int.?Int.!Int.Snew ,add : ?Int.?Int.!Int.Snew ,neg : ?Int.!Int.Snew

quit : end

• The session type of the client’s channel endpoint:

Cnew , ⊕ add : !Int.!Int.?Int.Cnew ,quit : end

Page 99: Introduction to Session Types

Subtyping: Channel Substitutability• Gay & Hole, “Subtyping for Session Types in the Pi

Calculus”. ESOP 1999, Acta Informatica 2005.

• Allow interaction when the client does not know about all ofthe server’s services.

I ⊆ J ∀i ∈ I . (Si <: S ′i )

&li : Sii∈I <: &lj : S ′jj∈J

• Subtyping relation between Sold and Snew :

Sold = &add , neg , quitSnew = &mul , add , neg , quitSold <: Snew

• Then the following holds:

From x : Snew ` srv(x)we can conclude x : Sold ` srv(x)

Page 100: Introduction to Session Types

Subtyping: Channel Substitutability• Gay & Hole, “Subtyping for Session Types in the Pi

Calculus”. ESOP 1999, Acta Informatica 2005.

• Allow interaction when the client does not know about all ofthe server’s services.

I ⊆ J ∀i ∈ I . (Si <: S ′i )

&li : Sii∈I <: &lj : S ′jj∈J

• Subtyping relation between Sold and Snew :

Sold = &add , neg , quitSnew = &mul , add , neg , quitSold <: Snew

• Then the following holds:

From x : Snew ` srv(x)we can conclude x : Sold ` srv(x)

Page 101: Introduction to Session Types

Subtyping: Channel Substitutability• Gay & Hole, “Subtyping for Session Types in the Pi

Calculus”. ESOP 1999, Acta Informatica 2005.

• Allow interaction when the client does not know about all ofthe server’s services.

I ⊆ J ∀i ∈ I . (Si <: S ′i )

&li : Sii∈I <: &lj : S ′jj∈J

• Subtyping relation between Sold and Snew :

Sold = &add , neg , quitSnew = &mul , add , neg , quitSold <: Snew

• Then the following holds:

From x : Snew ` srv(x)we can conclude x : Sold ` srv(x)

Page 102: Introduction to Session Types

Subtyping: Channel Substitutability

• Allow interaction when the client can choose from a smallerset choices than the ones offered by the server.

I ⊇ J ∀j ∈ J. Sj <: S ′j

⊕li : Sii∈I <: ⊕lj : S ′jj∈J

• Subtyping relation between Cold and Cnew :

Cold = ⊕add , neg , quitCnew = ⊕add , quitCold <: Cnew

• Then the following holds:

From x : Cnew ` clt(x)we can conclude x : Cold ` clt(x)

Page 103: Introduction to Session Types

Subtyping: Channel Substitutability

• Allow interaction when the client can choose from a smallerset choices than the ones offered by the server.

I ⊇ J ∀j ∈ J. Sj <: S ′j

⊕li : Sii∈I <: ⊕lj : S ′jj∈J

• Subtyping relation between Cold and Cnew :

Cold = ⊕add , neg , quitCnew = ⊕add , quitCold <: Cnew

• Then the following holds:

From x : Cnew ` clt(x)we can conclude x : Cold ` clt(x)

Page 104: Introduction to Session Types

Subtyping: Channel Substitutability

• Allow interaction when the client can choose from a smallerset choices than the ones offered by the server.

I ⊇ J ∀j ∈ J. Sj <: S ′j

⊕li : Sii∈I <: ⊕lj : S ′jj∈J

• Subtyping relation between Cold and Cnew :

Cold = ⊕add , neg , quitCnew = ⊕add , quitCold <: Cnew

• Then the following holds:

From x : Cnew ` clt(x)we can conclude x : Cold ` clt(x)

Page 105: Introduction to Session Types

Subtyping: Channel Substitutability

• Suppose that Sold has been published.

• To use the server, a client creates a session channel c.

• The client sends c+ : Sold to the server, and keeps c− : Sold .

• The client is not aware that the server expects x : Snew .

• Safe substitutability of channels: Sold <: Snew and it is(semantically) safe for the server to be given c+ : Sold .

Page 106: Introduction to Session Types

Subtyping: Channel Substitutability

• Suppose that Sold has been published.

• To use the server, a client creates a session channel c.

• The client sends c+ : Sold to the server, and keeps c− : Sold .

• The client is not aware that the server expects x : Snew .

• Safe substitutability of channels: Sold <: Snew and it is(semantically) safe for the server to be given c+ : Sold .

Page 107: Introduction to Session Types

Subtyping: Channel Substitutability

• Suppose that Sold has been published.

• To use the server, a client creates a session channel c.

• The client sends c+ : Sold to the server, and keeps c− : Sold .

• The client is not aware that the server expects x : Snew .

• Safe substitutability of channels: Sold <: Snew and it is(semantically) safe for the server to be given c+ : Sold .

Page 108: Introduction to Session Types

Subtyping: Channel Substitutability

• Suppose that Sold has been published.

• To use the server, a client creates a session channel c.

• The client sends c+ : Sold to the server, and keeps c− : Sold .

• The client is not aware that the server expects x : Snew .

• Safe substitutability of channels: Sold <: Snew and it is(semantically) safe for the server to be given c+ : Sold .

Page 109: Introduction to Session Types

Subtyping: Channel Substitutability

• Suppose that Sold has been published.

• To use the server, a client creates a session channel c.

• The client sends c+ : Sold to the server, and keeps c− : Sold .

• The client is not aware that the server expects x : Snew .

• Safe substitutability of channels: Sold <: Snew and it is(semantically) safe for the server to be given c+ : Sold .

Page 110: Introduction to Session Types

Subtyping Rules for Session Types [GH05]

I ⊆ J ∀i ∈ I . Si <: S ′i

&li : Sii∈I <: &lj : S ′jj∈J

I ⊇ J ∀j ∈ J. Sj <: S ′j

⊕li : Sii∈I <: ⊕lj : S ′jj∈J

end <: end

T <: T ′ S <: S ′

?T .S <: ?T ′.S ′

T ′ <: T S <: S ′

!T .S <: !T ′.S ′

?,& are covariant!,⊕ are contravariant

Page 111: Introduction to Session Types

Subtyping Rules for Session Types [GH05]

I ⊆ J ∀i ∈ I . Si <: S ′i

&li : Sii∈I <: &lj : S ′jj∈J

I ⊇ J ∀j ∈ J. Sj <: S ′j

⊕li : Sii∈I <: ⊕lj : S ′jj∈J

end <: end

T <: T ′ S <: S ′

?T .S <: ?T ′.S ′

T ′ <: T S <: S ′

!T .S <: !T ′.S ′

?,& are covariant!,⊕ are contravariant

Page 112: Introduction to Session Types

Exercise: Which is subtype of which?

![Even].end

:>

![Int].end

![![Even].end].end

<:

![![Int].end].end

?[![Even].end].end

:>

?[![Int].end].end

⊕ add : end, quit : end

:>

⊕add : end, neg : end, quit : end

![⊕add : end, quit : end]

<:

![⊕add : end, neg : end, quit : end]

&add : Real, neg : Int

×

&add : Int, neg : Real

Page 113: Introduction to Session Types

Exercise: Which is subtype of which?

![Even].end :> ![Int].end

![![Even].end].end

<:

![![Int].end].end

?[![Even].end].end

:>

?[![Int].end].end

⊕ add : end, quit : end

:>

⊕add : end, neg : end, quit : end

![⊕add : end, quit : end]

<:

![⊕add : end, neg : end, quit : end]

&add : Real, neg : Int

×

&add : Int, neg : Real

Page 114: Introduction to Session Types

Exercise: Which is subtype of which?

![Even].end :> ![Int].end

![![Even].end].end <: ![![Int].end].end

?[![Even].end].end

:>

?[![Int].end].end

⊕ add : end, quit : end

:>

⊕add : end, neg : end, quit : end

![⊕add : end, quit : end]

<:

![⊕add : end, neg : end, quit : end]

&add : Real, neg : Int

×

&add : Int, neg : Real

Page 115: Introduction to Session Types

Exercise: Which is subtype of which?

![Even].end :> ![Int].end

![![Even].end].end <: ![![Int].end].end

?[![Even].end].end :> ?[![Int].end].end

⊕ add : end, quit : end

:>

⊕add : end, neg : end, quit : end

![⊕add : end, quit : end]

<:

![⊕add : end, neg : end, quit : end]

&add : Real, neg : Int

×

&add : Int, neg : Real

Page 116: Introduction to Session Types

Exercise: Which is subtype of which?

![Even].end :> ![Int].end

![![Even].end].end <: ![![Int].end].end

?[![Even].end].end :> ?[![Int].end].end

⊕ add : end, quit : end :> ⊕add : end, neg : end, quit : end

![⊕add : end, quit : end]

<:

![⊕add : end, neg : end, quit : end]

&add : Real, neg : Int

×

&add : Int, neg : Real

Page 117: Introduction to Session Types

Exercise: Which is subtype of which?

![Even].end :> ![Int].end

![![Even].end].end <: ![![Int].end].end

?[![Even].end].end :> ?[![Int].end].end

⊕ add : end, quit : end :> ⊕add : end, neg : end, quit : end

![⊕add : end, quit : end] <: ![⊕add : end, neg : end, quit : end]

&add : Real, neg : Int

×

&add : Int, neg : Real

Page 118: Introduction to Session Types

Exercise: Which is subtype of which?

![Even].end :> ![Int].end

![![Even].end].end <: ![![Int].end].end

?[![Even].end].end :> ?[![Int].end].end

⊕ add : end, quit : end :> ⊕add : end, neg : end, quit : end

![⊕add : end, quit : end] <: ![⊕add : end, neg : end, quit : end]

&add : Real, neg : Int × &add : Int, neg : Real

Page 119: Introduction to Session Types

Subtyping: Process Substitutability

• Carbone, Honda & Yoshida (ESOP 2007);Demangeon & Honda (CONCUR 2011) define subtyping inthe opposite direction: Snew <: Sold .

• They consider a session environment to be the type of aprocess:

x : Snew ` srv(x)

• They want safe substitutability of processes: the new servercan be used in any context where an old server was expected.

• Subsumption gives

x : Sold ` srv(x)

Page 120: Introduction to Session Types

Subtyping: Process Substitutability

• Carbone, Honda & Yoshida (ESOP 2007);Demangeon & Honda (CONCUR 2011) define subtyping inthe opposite direction: Snew <: Sold .

• They consider a session environment to be the type of aprocess:

x : Snew ` srv(x)

• They want safe substitutability of processes: the new servercan be used in any context where an old server was expected.

• Subsumption gives

x : Sold ` srv(x)

Page 121: Introduction to Session Types

Subtyping: Process Substitutability

• Carbone, Honda & Yoshida (ESOP 2007);Demangeon & Honda (CONCUR 2011) define subtyping inthe opposite direction: Snew <: Sold .

• They consider a session environment to be the type of aprocess:

x : Snew ` srv(x)

• They want safe substitutability of processes: the new servercan be used in any context where an old server was expected.

• Subsumption gives

x : Sold ` srv(x)

Page 122: Introduction to Session Types

Subtyping: Process Substitutability

• Carbone, Honda & Yoshida (ESOP 2007);Demangeon & Honda (CONCUR 2011) define subtyping inthe opposite direction: Snew <: Sold .

• They consider a session environment to be the type of aprocess:

x : Snew ` srv(x)

• They want safe substitutability of processes: the new servercan be used in any context where an old server was expected.

• Subsumption gives

x : Sold ` srv(x)

Page 123: Introduction to Session Types

On the Subsumption Rule (1)

• Substitutability of channels:

Γ ` P Γ′ <: Γ

Γ′ ` P

• Example:

x : Snew ` srv(x) x : Sold <: x : Snew

x : Sold ` srv(x)

Page 124: Introduction to Session Types

On the Subsumption Rule (2)

• Substitutability of processes:

Γ ` P Γ <: Γ′

Γ′ ` P

• Example:

x : Snew ` srv(x) x : Snew <: x : Sold

x : Sold ` srv(x)

Simon J. Gay. “Subtyping Supports Safe Session Substitution”.WadlerFest 2016

Page 125: Introduction to Session Types

Outline1 Origin of Session Types

2 Session Types by Example

3 Session Types Formally

4 Foundation of Session TypesSession Types and Standard π-calculus TypesSession Types and Linear Logic

5 SubtypingTwo Subtyping Relations for SessionsSubtyping by Encoding

6 Session Types and Programming Languages (I)

7 Multiparty Session Types

8 Session Types and Programming Languages (II)ScribbleMungoStMungoScribble + Mungo + StMungo for typechecking SMTP

9 Advanced Topics

Page 126: Introduction to Session Types

Subtyping Rules for Standard π- Types

(Sπ- Refl)T ≤ T

T ≤ T ′ T ′ ≤ T ′′

(Sπ- Trans)T ≤ T ′′

T ≤ T ′(Sπ- ii)

`i [T ] ≤ `i [T ′]

T ′ ≤ T(Sπ- oo)

`o [T ] ≤ `o [T ′]

I ⊆ J Ti ≤ T ′j ∀i ∈ I(Sπ- Variant)

〈li : Ti 〉i∈I ≤ 〈lj : T ′j 〉j∈J

Page 127: Introduction to Session Types

Subtyping

TheoremFor all session types S , S ′. S <: S ′ if and only if JSK ≤ JS ′K.

Derived from the encoding:

• Reflexivity and Transitivity of Subtyping.

• Lemmas (e.g., Substitution...) from the corresponding ones inthe π- calculus. derived for free.

Page 128: Introduction to Session Types

Subtyping

TheoremFor all session types S , S ′. S <: S ′ if and only if JSK ≤ JS ′K.

Derived from the encoding:

• Reflexivity and Transitivity of Subtyping.

• Lemmas (e.g., Substitution...) from the corresponding ones inthe π- calculus. derived for free.

Page 129: Introduction to Session Types

More on Subtyping

• Mostrous (2010) extended subtyping to allow some reorderingof messages, when communication is asynchronous.

• Padovani (2011, 2013) has considered another form ofsubtyping, called fair subtyping.

• Chen, Dezani & Yoshida (2014) have studied the precisenessof subtyping: the subtyping relation is sound and complete forsafe substitutability.

Page 130: Introduction to Session Types

More on Subtyping

• Mostrous (2010) extended subtyping to allow some reorderingof messages, when communication is asynchronous.

• Padovani (2011, 2013) has considered another form ofsubtyping, called fair subtyping.

• Chen, Dezani & Yoshida (2014) have studied the precisenessof subtyping: the subtyping relation is sound and complete forsafe substitutability.

Page 131: Introduction to Session Types

More on Subtyping

• Mostrous (2010) extended subtyping to allow some reorderingof messages, when communication is asynchronous.

• Padovani (2011, 2013) has considered another form ofsubtyping, called fair subtyping.

• Chen, Dezani & Yoshida (2014) have studied the precisenessof subtyping: the subtyping relation is sound and complete forsafe substitutability.

Page 132: Introduction to Session Types

Outline1 Origin of Session Types

2 Session Types by Example

3 Session Types Formally

4 Foundation of Session TypesSession Types and Standard π-calculus TypesSession Types and Linear Logic

5 SubtypingTwo Subtyping Relations for SessionsSubtyping by Encoding

6 Session Types and Programming Languages (I)

7 Multiparty Session Types

8 Session Types and Programming Languages (II)ScribbleMungoStMungoScribble + Mungo + StMungo for typechecking SMTP

9 Advanced Topics

Page 133: Introduction to Session Types

Categorising language-basedimplementations of session types

• Binary vs. Multiparty

• Primitive vs. Library vs. External Tool

• Static vs. Dynamic vs. Hybrid checking

Page 134: Introduction to Session Types

Programming Languages with NativeBST: Static Typechecking

Sill:

• Functional programming language that supports session typedmessage passing concurrency.

• Based on the Curry-Howard correspondence of session typesand intuitionistic linear logic (Caires & Pfenning 2010).

• Type preservation; deadlock and race freedom; support ofsubtyping, polymorphism and recursive types.

• Contributors: F. Pfenning, D. Griffith et al.

Page 135: Introduction to Session Types

Programming Languages with NativeBST: Static Typechecking

SePi:

• Concurrent, message-passing programming language based onthe π- calculus.

• Based on synchronous, bidirectional channel basedcommunication.

• Primitives for send/receive as well as offer/select choices.

• Contributors: J. Franco, V.Vasconcelos, D.Mostrous.

Page 136: Introduction to Session Types

Programming Languages with NativeBST: Static Typechecking5

Links:

• Programming language for web applications.

• Binary session types added as language primitives andstatically typechecked.

• Developed at the University of Edinburgh.

5The following list of programming languages is taken fromhttp://simonjf.com/2016/05/28/session-type-implementations.html

Page 137: Introduction to Session Types

Mainstream Programming Languages withBinary Session Types

Haskell:

• effect-sessions: implementation in Concurrent Haskell; statictypechecking. Orchard & Yoshida (POPL 2016)

• simple-session: la ibrary implementation of Haskell sessiontypes. Pucella & Tov (Haskell 2008)

• sessions: yet another embedding of session types in Haskell.Sackman & Eisenbach (TR 2008)

Page 138: Introduction to Session Types

Mainstream Programming Languages withBinary Session Types

Java:

• CO2 Middleware: for Java applications, based on timedsession types; dynamic monitoring for conformance of timingconstraints.Bartoletti et al. (FACS 2015, FORTE 2015)

• (Eventful) Session Java: front-end and runtime library forJava; supports event-driven programming.Hu, Yoshida & Honda (ECOOP 2008);Hu et al. (ECOOP 2010)

Page 139: Introduction to Session Types

Mainstream Programming Languages withBinary Session Types

Scala

• Based on the continuation-passing approach ofKobayashi 2007, and Dardha et al. 2012

• Message ordering is checked statically

• Linearity is checked dynamically.

• Scalas & Yoshida (ECOOP 2016)

Page 140: Introduction to Session Types

Mainstream Programming Languages withBinary Session Types

OCaml: FuSe

• Lightweight implementation of BST in OCaml

• static check of message ordering and dynamic linearity check.Padovani 2015

Rust:

• Implementation of BST in Mozilla’s Rust; use of Rust’s affinetype system. Jespersen, Munksgaard & Larsen (WGP 2015)

Page 141: Introduction to Session Types

Mainstream Programming Languages withBinary Session Types

OCaml: FuSe

• Lightweight implementation of BST in OCaml

• static check of message ordering and dynamic linearity check.Padovani 2015

Rust:

• Implementation of BST in Mozilla’s Rust; use of Rust’s affinetype system. Jespersen, Munksgaard & Larsen (WGP 2015)

Page 142: Introduction to Session Types

Outline1 Origin of Session Types

2 Session Types by Example

3 Session Types Formally

4 Foundation of Session TypesSession Types and Standard π-calculus TypesSession Types and Linear Logic

5 SubtypingTwo Subtyping Relations for SessionsSubtyping by Encoding

6 Session Types and Programming Languages (I)

7 Multiparty Session Types

8 Session Types and Programming Languages (II)ScribbleMungoStMungoScribble + Mungo + StMungo for typechecking SMTP

9 Advanced Topics

Page 143: Introduction to Session Types

Multiparty Session Types (1)

• Binary session types can describe systems with multipleparticipants, but all protocols are pairwise and independent.

• Binary session types cannot constrain the order of twomessages in different protocols.

• Honda, Yoshida & Carbone (POPL 2008) developed a theoryof multiparty session types.

• A global type specifies a multi-party protocol.

• A global type can be projected to local types, which specifythe communication behaviour of each participant.

• Local type checking guarantees communication safety.

Page 144: Introduction to Session Types

Multiparty Session Types (1)

• Binary session types can describe systems with multipleparticipants, but all protocols are pairwise and independent.

• Binary session types cannot constrain the order of twomessages in different protocols.

• Honda, Yoshida & Carbone (POPL 2008) developed a theoryof multiparty session types.

• A global type specifies a multi-party protocol.

• A global type can be projected to local types, which specifythe communication behaviour of each participant.

• Local type checking guarantees communication safety.

Page 145: Introduction to Session Types

Multiparty Session Types (1)

• Binary session types can describe systems with multipleparticipants, but all protocols are pairwise and independent.

• Binary session types cannot constrain the order of twomessages in different protocols.

• Honda, Yoshida & Carbone (POPL 2008) developed a theoryof multiparty session types.

• A global type specifies a multi-party protocol.

• A global type can be projected to local types, which specifythe communication behaviour of each participant.

• Local type checking guarantees communication safety.

Page 146: Introduction to Session Types

Multiparty Session Types (1)

• Binary session types can describe systems with multipleparticipants, but all protocols are pairwise and independent.

• Binary session types cannot constrain the order of twomessages in different protocols.

• Honda, Yoshida & Carbone (POPL 2008) developed a theoryof multiparty session types.

• A global type specifies a multi-party protocol.

• A global type can be projected to local types, which specifythe communication behaviour of each participant.

• Local type checking guarantees communication safety.

Page 147: Introduction to Session Types

Multiparty Session Types (1)

• Binary session types can describe systems with multipleparticipants, but all protocols are pairwise and independent.

• Binary session types cannot constrain the order of twomessages in different protocols.

• Honda, Yoshida & Carbone (POPL 2008) developed a theoryof multiparty session types.

• A global type specifies a multi-party protocol.

• A global type can be projected to local types, which specifythe communication behaviour of each participant.

• Local type checking guarantees communication safety.

Page 148: Introduction to Session Types

Multiparty Session Types (1)

• Binary session types can describe systems with multipleparticipants, but all protocols are pairwise and independent.

• Binary session types cannot constrain the order of twomessages in different protocols.

• Honda, Yoshida & Carbone (POPL 2008) developed a theoryof multiparty session types.

• A global type specifies a multi-party protocol.

• A global type can be projected to local types, which specifythe communication behaviour of each participant.

• Local type checking guarantees communication safety.

Page 149: Introduction to Session Types

Multiparty Session Types (2)

A buyer-seller example from Honda et al:

Buyer1 Seller Buyer2

[Link] [Link]

title

quotequote

quote div 2

ok

quit

address

date branch

Page 150: Introduction to Session Types

Multiparty Session Types (3)

The global type describes the whole protocol:

1. B1→ S : title.

2. S→ B1 : quote.

3. S→ B2 : quote.

4. B1→ B2 : quote.

5. B2→ S :

ok : B2→ S : address.

S→ B2 : date.end,quit : end

Page 151: Introduction to Session Types

Multiparty Session Types (4)

• Projection gives a local type for B1:

S!title.S?quote.B2!quote

and for B2:

S?quote.B1?quote.S ⊕ ok : S!address.S?date.end, quit : end

• Local type checking is similar to binary session types.

• Consistency conditions on the global type guarantee that theprotocol can be realised by independent local participants.

Page 152: Introduction to Session Types

Multiparty Session Types (4)

• Projection gives a local type for B1:

S!title.S?quote.B2!quote

and for B2:

S?quote.B1?quote.S ⊕ ok : S!address.S?date.end, quit : end

• Local type checking is similar to binary session types.

• Consistency conditions on the global type guarantee that theprotocol can be realised by independent local participants.

Page 153: Introduction to Session Types

Multiparty Session Types (4)

• Projection gives a local type for B1:

S!title.S?quote.B2!quote

and for B2:

S?quote.B1?quote.S ⊕ ok : S!address.S?date.end, quit : end

• Local type checking is similar to binary session types.

• Consistency conditions on the global type guarantee that theprotocol can be realised by independent local participants.

Page 154: Introduction to Session Types

Outline1 Origin of Session Types

2 Session Types by Example

3 Session Types Formally

4 Foundation of Session TypesSession Types and Standard π-calculus TypesSession Types and Linear Logic

5 SubtypingTwo Subtyping Relations for SessionsSubtyping by Encoding

6 Session Types and Programming Languages (I)

7 Multiparty Session Types

8 Session Types and Programming Languages (II)ScribbleMungoStMungoScribble + Mungo + StMungo for typechecking SMTP

9 Advanced Topics

Page 155: Introduction to Session Types

Outline1 Origin of Session Types

2 Session Types by Example

3 Session Types Formally

4 Foundation of Session TypesSession Types and Standard π-calculus TypesSession Types and Linear Logic

5 SubtypingTwo Subtyping Relations for SessionsSubtyping by Encoding

6 Session Types and Programming Languages (I)

7 Multiparty Session Types

8 Session Types and Programming Languages (II)ScribbleMungoStMungoScribble + Mungo + StMungo for typechecking SMTP

9 Advanced Topics

Page 156: Introduction to Session Types

Scribble

• Scribble is a language used to describe application-levelprotocols among communicating systems.

• It is based on multiparty session types.

• Allows:• Specification of a protocol in the form of global session type;• Validation of the protocol;• Projection into the communicating participants, i.e., roles.

• Contributors: K.Honda, IC team (part of ABCD).

• Link: www.scribble.org

Page 157: Introduction to Session Types

Scribble

• Scribble is a language used to describe application-levelprotocols among communicating systems.

• It is based on multiparty session types.

• Allows:• Specification of a protocol in the form of global session type;• Validation of the protocol;• Projection into the communicating participants, i.e., roles.

• Contributors: K.Honda, IC team (part of ABCD).

• Link: www.scribble.org

Page 158: Introduction to Session Types

Scribble

• Scribble is a language used to describe application-levelprotocols among communicating systems.

• It is based on multiparty session types.

• Allows:• Specification of a protocol in the form of global session type;• Validation of the protocol;• Projection into the communicating participants, i.e., roles.

• Contributors: K.Honda, IC team (part of ABCD).

• Link: www.scribble.org

Page 159: Introduction to Session Types

Scribble

• Scribble is a language used to describe application-levelprotocols among communicating systems.

• It is based on multiparty session types.

• Allows:• Specification of a protocol in the form of global session type;• Validation of the protocol;• Projection into the communicating participants, i.e., roles.

• Contributors: K.Honda, IC team (part of ABCD).

• Link: www.scribble.org

Page 160: Introduction to Session Types

Scribble

• Scribble is a language used to describe application-levelprotocols among communicating systems.

• It is based on multiparty session types.

• Allows:• Specification of a protocol in the form of global session type;• Validation of the protocol;• Projection into the communicating participants, i.e., roles.

• Contributors: K.Honda, IC team (part of ABCD).

• Link: www.scribble.org

Page 161: Introduction to Session Types

Scribble by example: The BookstoreGlobal Protocol

global protocol Bookstore(role Buyer1 , role Buyer2 ,

role Seller)

book(title) from Buyer1 to Seller;

book(quote) from Seller to Buyer1 , Buyer2;

contribution(quote) from Buyer1 to Buyer2;

choice at Buyer2

ok from Buyer2 to Seller;

deliver(address) from Buyer2 to Seller;

deliver(date) from Seller to Buyer2;

or

quit from Buyer2 to Seller;

Page 162: Introduction to Session Types

The Bookstore Protocol: Buyer1

local protocol Bookstore_Buyer1(self Buyer1 , role

Buyer2 , role Seller)

book(title) to Seller;

book(quote) from Seller;

contribution(quote) to Buyer2;

Page 163: Introduction to Session Types

The Bookstore Protocol: Buyer2

local protocol Bookstore_Buyer2(role Seller , self

Buyer2 , role Buyer1)

book(quote) from Seller;

contribution(quote) from Buyer1;

choice at Buyer2

ok to Seller;

deliver(address) to Seller;

deliver(date) from Seller;

or

quit to Seller;

Page 164: Introduction to Session Types

Outline1 Origin of Session Types

2 Session Types by Example

3 Session Types Formally

4 Foundation of Session TypesSession Types and Standard π-calculus TypesSession Types and Linear Logic

5 SubtypingTwo Subtyping Relations for SessionsSubtyping by Encoding

6 Session Types and Programming Languages (I)

7 Multiparty Session Types

8 Session Types and Programming Languages (II)ScribbleMungoStMungoScribble + Mungo + StMungo for typechecking SMTP

9 Advanced Topics

Page 165: Introduction to Session Types

Mungo

• Mungo is a Java front-end tool that statically checks theorder of method calls.

• Based on the notions of session types and typestate,describing non-uniform objects.

• A Java class is annotated with a typestate. Mungo checksthat method calls follow the declared typestate of an object.

• Contributors: ABCD Glasgow team.Based on Gay et al (POPL 2010);Kouzapas et al. (PPDP 2016)

Page 166: Introduction to Session Types

Mungo

• Mungo is a Java front-end tool that statically checks theorder of method calls.

• Based on the notions of session types and typestate,describing non-uniform objects.

• A Java class is annotated with a typestate. Mungo checksthat method calls follow the declared typestate of an object.

• Contributors: ABCD Glasgow team.Based on Gay et al (POPL 2010);Kouzapas et al. (PPDP 2016)

Page 167: Introduction to Session Types

Mungo

• Mungo is a Java front-end tool that statically checks theorder of method calls.

• Based on the notions of session types and typestate,describing non-uniform objects.

• A Java class is annotated with a typestate. Mungo checksthat method calls follow the declared typestate of an object.

• Contributors: ABCD Glasgow team.Based on Gay et al (POPL 2010);Kouzapas et al. (PPDP 2016)

Page 168: Introduction to Session Types

Mungo

• Mungo is a Java front-end tool that statically checks theorder of method calls.

• Based on the notions of session types and typestate,describing non-uniform objects.

• A Java class is annotated with a typestate. Mungo checksthat method calls follow the declared typestate of an object.

• Contributors: ABCD Glasgow team.Based on Gay et al (POPL 2010);Kouzapas et al. (PPDP 2016)

Page 169: Introduction to Session Types

The FileProtocol Example

typestate FileProtocol

Init =

Status open (): <OK: Open , ERROR: end >

Open =

BooleanEnum hasNext (): <TRUE: Read , FALSE: Close >,

void close (): end

Read =

void read (): Open

Close =

void close (): end

Page 170: Introduction to Session Types

Outline1 Origin of Session Types

2 Session Types by Example

3 Session Types Formally

4 Foundation of Session TypesSession Types and Standard π-calculus TypesSession Types and Linear Logic

5 SubtypingTwo Subtyping Relations for SessionsSubtyping by Encoding

6 Session Types and Programming Languages (I)

7 Multiparty Session Types

8 Session Types and Programming Languages (II)ScribbleMungoStMungoScribble + Mungo + StMungo for typechecking SMTP

9 Advanced Topics

Page 171: Introduction to Session Types

StMungo

• StMungo is a Java-based tool used to translate Scribble localprotocols into typestate.

• After the translation, Mungo is used to statically typecheckthe protocol.

• Contributors: ABCD Glasgow team.Kouzapas et al. (PPDP 2016)

Page 172: Introduction to Session Types

StMungo

• StMungo is a Java-based tool used to translate Scribble localprotocols into typestate.

• After the translation, Mungo is used to statically typecheckthe protocol.

• Contributors: ABCD Glasgow team.Kouzapas et al. (PPDP 2016)

Page 173: Introduction to Session Types

StMungo

• StMungo is a Java-based tool used to translate Scribble localprotocols into typestate.

• After the translation, Mungo is used to statically typecheckthe protocol.

• Contributors: ABCD Glasgow team.Kouzapas et al. (PPDP 2016)

Page 174: Introduction to Session Types

The Bookstore Protocol: Buyer2

local protocol Bookstore_Buyer2(role Seller , self

Buyer2 , role Buyer1)

book(quote) from Seller;

contribution(quote) from Buyer1;

choice at Buyer2

ok to Seller;

deliver(address) to Seller;

deliver(date) from Seller;

or

quit to Seller;

Page 175: Introduction to Session Types

The Buyer2 local protocol as Typestate

typestate Buyer2Protocol

State0 =

quote receive_quoteFromSeller (): State1

State1 =

quote receive_quoteFromBuyer1 (): State2

State2 =

void send_OKToSeller (): State3 ,

void send_QUITToSeller (): State5

State3 =

void send_addressToSeller(address ): State4

State4 =

date receive_dateFromSeller (): end

...

Page 176: Introduction to Session Types

Outline1 Origin of Session Types

2 Session Types by Example

3 Session Types Formally

4 Foundation of Session TypesSession Types and Standard π-calculus TypesSession Types and Linear Logic

5 SubtypingTwo Subtyping Relations for SessionsSubtyping by Encoding

6 Session Types and Programming Languages (I)

7 Multiparty Session Types

8 Session Types and Programming Languages (II)ScribbleMungoStMungoScribble + Mungo + StMungo for typechecking SMTP

9 Advanced Topics

Page 177: Introduction to Session Types

The SMTP Protocol: A Demo

Link: http://www.dcs.gla.ac.uk/research/mungo/

Page 178: Introduction to Session Types

Mainstream Programming Languages withMultiparty Session Types6

Multiparty Session C:

• Static typechecking of MST in C

• Session communication happens via use of a library

• Ng, Yoshida & Honda (TOOLS 2012);Ng et al (HEART 2012)

DinGo Hunter

• External tool to statically analyse Go programs

• Static detection of deadlocks: extracting CFSMs andsynthesising global graphs

• Ng & Yoshida (CC 2016)

6The following list of programming languages is taken fromhttp://simonjf.com/2016/05/28/session-type-implementations.html

Page 179: Introduction to Session Types

Mainstream Programming Languages withMultiparty Session Types

Session Actor

• A Python implementation for combining session types and theactor model of programming.

• Each actor may be involved in multiple roles, in multiplesessions.

• Communication is checked dynamically via compilation ofScribble protocols into CFSMs. Neykova & Yoshida(COORDINATION 2014)

Page 180: Introduction to Session Types

Mainstream Programming Languages withMultiparty Session Types

Python

• SPY: implementation of MST in Python using runtimemonitoring. Neykova (PLACES 2013); Neykova, Yoshida &Hu (RV 2013); Hu et al (RV 2013)

Erlang

• Dynamic monitoring of communication (MST) for Erlangapplications

• Inspired by Session Actor. Simon Fowler (MSc thesis, 2015)

Page 181: Introduction to Session Types

Outline1 Origin of Session Types

2 Session Types by Example

3 Session Types Formally

4 Foundation of Session TypesSession Types and Standard π-calculus TypesSession Types and Linear Logic

5 SubtypingTwo Subtyping Relations for SessionsSubtyping by Encoding

6 Session Types and Programming Languages (I)

7 Multiparty Session Types

8 Session Types and Programming Languages (II)ScribbleMungoStMungoScribble + Mungo + StMungo for typechecking SMTP

9 Advanced Topics

Page 182: Introduction to Session Types

Progress

• Progress is a fundamental property of safe processes.

• A program having progress does not get “stuck”, i.e., a statethat is not designated as a final value and that the languagesemantics does not tell how to evaluate further.

Page 183: Introduction to Session Types

Progress

• Progress is a fundamental property of safe processes.

• A program having progress does not get “stuck”, i.e., a statethat is not designated as a final value and that the languagesemantics does not tell how to evaluate further.

Page 184: Introduction to Session Types

Comparing Properties of Communication

• Deadlock Freedom: communications eventually succeed,unless the whole process diverges. (Standard π)

• Lock Freedom: communications eventually succeed even if thewhole process diverges. (Standard π)

• Progress: In-session communications eventually succeed,provided that a suitable context can be found. (Session π)

Page 185: Introduction to Session Types

Deadlock Freedom vs. Lock Freedom

• Consider the process:

P = (νx)(νy)(

x+?(z).y+!〈z〉 | y−?(w).x−!〈w〉)

It is deadlocked and hence locked!

• Consider the process:

Q = (νx)(x+?(z) | Ω)

It is deadlock-free but locked!

Page 186: Introduction to Session Types

Deadlock Freedom vs. Lock Freedom

• Consider the process:

P = (νx)(νy)(

x+?(z).y+!〈z〉 | y−?(w).x−!〈w〉)

It is deadlocked and hence locked!

• Consider the process:

Q = (νx)(x+?(z) | Ω)

It is deadlock-free but locked!

Page 187: Introduction to Session Types

Research Question

What is the relationship among deadlock freedom, lock freedomand progress?

• Lock freedom is a stronger property than deadlock freedom.

• Progress is a compositional form of lock freedom.(Carbone, Dardha & Montesi 2014)

Page 188: Introduction to Session Types

Research Question

What is the relationship among deadlock freedom, lock freedomand progress?

• Lock freedom is a stronger property than deadlock freedom.

• Progress is a compositional form of lock freedom.(Carbone, Dardha & Montesi 2014)

Page 189: Introduction to Session Types

Research Question

What is the relationship among deadlock freedom, lock freedomand progress?

• Lock freedom is a stronger property than deadlock freedom.

• Progress is a compositional form of lock freedom.(Carbone, Dardha & Montesi 2014)

Page 190: Introduction to Session Types

More Advanced Topics in Sessions

Session types theories include notions and studies in the following.

• Notions of subtyping, polymorphism, higher-order.

• Study of liveness properties: deadlock freedom, lock freedomand progress.

• Asynchrony and synchrony.

• Static typechecking and dynamic monitoring.

• Finite and recursive session types.

• Study of security (e.g., information flow).

• Exceptions, time-outs.

• Point-to-point and broadcasting

• And many more...

Page 191: Introduction to Session Types

Conclusions

• Session Types are a very simple but powerful formalism tomodel protocols in distributed systems.

• Developed for calculi as well as programming languages andvarious paradigms.

• Many interesting features.

• Part of behavioural types, including also contracts,typestates...

Page 192: Introduction to Session Types

Audience!〈ThankYou〉.rec X &

more : Audience?(y : Question).Audience!〈Answer〉.X ,quit : end

Page 193: Introduction to Session Types

References I

Lorenzo Bettini, Mario Coppo, Loris D’Antoni, Marco DeLuca, Mariangiola Dezani-Ciancaglini, and Nobuko Yoshida.Global progress in dynamically interleaved multiparty sessions.In CONCUR, pages 418–433, 2008.

Marco Carbone and Søren Debois.A graphical approach to progress for structuredcommunication in web services.In ICE, pages 13–27, 2010.

Tzu-Chun Chen, Mariangiola Dezani-Ciancaglini, and NobukoYoshida.On the preciseness of subtyping in session types.In PPDP. ACM, 2014.

Page 194: Introduction to Session Types

References II

Marco Carbone, Ornela Dardha, and Fabrizio Montesi.Progress as compositional lock-freedom.In COORDINATION, volume 8459 of LNCS, pages 49–64.Springer, 2014.

Luıs Caires and Frank Pfenning.Session types as intuitionistic linear propositions.In CONCUR, pages 222–236, 2010.

Ornela Dardha, Elena Giachino, and Davide Sangiorgi.Session types revisited.In PPDP, pages 139–150, New York, NY, USA, 2012. ACM.

Romain Demangeon and Kohei Honda.Full abstraction in a subtyped pi-calculus with linear types.In CONCUR, pages 280–296, 2011.

Page 195: Introduction to Session Types

References III

Simon J. Gay.Subtyping supports safe session substitution.In A List of Successes That Can Change the World - EssaysDedicated to Philip Wadler on the Occasion of His 60thBirthday, volume 9600 of LNCS, pages 95–108. Springer, 2016.

Simon J. Gay and Malcolm Hole.Subtyping for session types in the pi calculus.Acta Informatica, 42(2-3):191–225, nov 2005.

Kohei Honda, Nobuko Yoshida, and Marco Carbone.Multiparty asynchronous session types.In POPL, volume 43(1), pages 273–284. ACM, 2008.

Naoki Kobayashi.A type system for lock-free processes.Inf. Comput., 177(2):122–159, 2002.

Page 196: Introduction to Session Types

References IV

Naoki Kobayashi.A new type system for deadlock-free processes.In CONCUR, pages 233–247, 2006.

Luca Padovani.Fair subtyping for multi-party session types.In COORDINATION, pages 127–141, Berlin, Heidelberg, 2011.Springer-Verlag.

Luca Padovani.Fair subtyping for open session types.In ICALP, 2013.

Vasco T. Vasconcelos.Fundamentals of session types.Information Computation, 217:52–70, 2012.

Page 197: Introduction to Session Types

References V

Philip Wadler.Propositions as sessions.In ICFP, pages 273–286, 2012.