Gradual Session Types Albert-Ludwigs-Universit at Freiburg Atsushi - - PowerPoint PPT Presentation

gradual session types
SMART_READER_LITE
LIVE PREVIEW

Gradual Session Types Albert-Ludwigs-Universit at Freiburg Atsushi - - PowerPoint PPT Presentation

Gradual Session Types Albert-Ludwigs-Universit at Freiburg Atsushi Igarashi 1 Peter Thiemann 2 Vasco Vasconcelos 3 1 Kyoto University, 2 University of Freiburg, 3 University of Lisbon 31 Jan 2017 Session Types are Good for You communication


slide-1
SLIDE 1

Gradual Session Types

Albert-Ludwigs-Universit¨ at Freiburg

Atsushi Igarashi1 Peter Thiemann2 Vasco Vasconcelos3

1Kyoto University, 2University of Freiburg, 3University of Lisbon

31 Jan 2017

slide-2
SLIDE 2

Session Types are Good for You

communication on typed channels bidirectional and heterogeneous type safety and communication safety (scalable to multi-session) (deadlock freedom)

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 2 / 19

slide-3
SLIDE 3

Example: CurrenCy Conversion Protocol

Client Perspective, fully session typed

cccp = ! CCY. ! Double. cccp-loop cccp-loop = &{ MORE : ? CCY. ? Double. cccp-loop, EXIT : end} Send a currency abbreviation {element of enumeration type} Send an amount while received MORE do Receive a currency abbreviation Receive an amount received EXIT Close the channel

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 3 / 19

slide-4
SLIDE 4

Example: CurrenCy Conversion Protocol

Client Perspective, fully session typed

cccp = ! CCY. ! Double. cccp-loop cccp-loop = &{ MORE : ? CCY. ? Double. cccp-loop, EXIT : end} Send a currency abbreviation {element of enumeration type} Send an amount while received MORE do Receive a currency abbreviation Receive an amount received EXIT Close the channel

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 3 / 19

slide-5
SLIDE 5

Example: CurrenCy Conversion Protocol

Client Perspective, fully session typed

cccp = ! CCY. ! Double. cccp-loop cccp-loop = &{ MORE : ? CCY. ? Double. cccp-loop, EXIT : end} Send a currency abbreviation {element of enumeration type} Send an amount while received MORE do Receive a currency abbreviation Receive an amount received EXIT Close the channel

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 3 / 19

slide-6
SLIDE 6

Example: CurrenCy Conversion Protocol

Client Perspective, fully session typed

cccp = ! CCY. ! Double. cccp-loop cccp-loop = &{ MORE : ? CCY. ? Double. cccp-loop, EXIT : end} Send a currency abbreviation {element of enumeration type} Send an amount while received MORE do Receive a currency abbreviation Receive an amount received EXIT Close the channel

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 3 / 19

slide-7
SLIDE 7

Example: CurrenCy Conversion Protocol

Client Perspective, fully session typed

cccp = ! CCY. ! Double. cccp-loop cccp-loop = &{ MORE : ? CCY. ? Double. cccp-loop, EXIT : end} Send a currency abbreviation {element of enumeration type} Send an amount while received MORE do Receive a currency abbreviation Receive an amount received EXIT Close the channel

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 3 / 19

slide-8
SLIDE 8

Example: CurrenCy Conversion Protocol

Client Perspective, fully session typed

cccp = ! CCY. ! Double. cccp-loop cccp-loop = &{ MORE : ? CCY. ? Double. cccp-loop, EXIT : end} Send a currency abbreviation {element of enumeration type} Send an amount while received MORE do Receive a currency abbreviation Receive an amount received EXIT Close the channel

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 3 / 19

slide-9
SLIDE 9

Example: CurrenCy Conversion Protocol

Client Perspective, fully session typed

cccp = ! CCY. ! Double. cccp-loop cccp-loop = &{ MORE : ? CCY. ? Double. cccp-loop, EXIT : end} Send a currency abbreviation {element of enumeration type} Send an amount while received MORE do Receive a currency abbreviation Receive an amount received EXIT Close the channel

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 3 / 19

slide-10
SLIDE 10

Example: CurrenCy Conversion Protocol

Client Perspective, fully session typed

cccp = ! CCY. ! Double. cccp-loop cccp-loop = &{ MORE : ? CCY. ? Double. cccp-loop, EXIT : end} Send a currency abbreviation {element of enumeration type} Send an amount while received MORE do Receive a currency abbreviation Receive an amount received EXIT Close the channel

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 3 / 19

slide-11
SLIDE 11

Example: CurrenCy Conversion Protocol

Client Perspective, fully session typed

cccp = ! CCY. ! Double. cccp-loop cccp-loop = &{ MORE : ? CCY. ? Double. cccp-loop, EXIT : end} Send a currency abbreviation {element of enumeration type} Send an amount while received MORE do Receive a currency abbreviation Receive an amount received EXIT Close the channel

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 3 / 19

slide-12
SLIDE 12

Example: CurrenCy Conversion Protocol

Client Perspective, fully session typed

cccp = ! CCY. ! Double. cccp-loop cccp-loop = &{ MORE : ? CCY. ? Double. cccp-loop, EXIT : end} Send a currency abbreviation {element of enumeration type} Send an amount while received MORE do Receive a currency abbreviation Receive an amount received EXIT Close the channel

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 3 / 19

slide-13
SLIDE 13

A CCCP Server

Switch of perspective

Let’s look at a SERVER

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 4 / 19

slide-14
SLIDE 14

A CCCP Server

Switch of perspective

Let’s look at a SERVER

Suppose

the first part (before the loop) is typed, but the loop is written in an untyped language.

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 4 / 19

slide-15
SLIDE 15

A CCCP Server

Switch of perspective

Let’s look at a SERVER

Suppose

the first part (before the loop) is typed, but the loop is written in an untyped language.

Gradual typing

provides the protective interface via casts

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 4 / 19

slide-16
SLIDE 16

Excursion: Gradual Typing

Simply-Typed Lambda Calculus

T, U ::= B | T → U | T × U

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 5 / 19

slide-17
SLIDE 17

Excursion: Gradual Typing

Gradual Lambda Calculus

T, U ::= B | T → U | T × U | ? ? ? any simple type Extended with type dynamic ? ? ?

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 5 / 19

slide-18
SLIDE 18

Excursion: Gradual Typing

Simply-Typed Lambda Calculus with Sessions (Mini-GV)

T, U ::= S | B | T → U | T × U R, S ::= end | !T.S | ?T.S Extended with session types

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 5 / 19

slide-19
SLIDE 19

Excursion: Gradual Typing

Gradual Lambda Calculus with Sessions (Mini-GV)

T, U ::= S | B | T → U | T × U | ? ? ? any simple type R, S ::= end | !T.S | ?T.S | ? ? ?

  • any session type

Extended with type dynamic ? ? ? and dynamic session ? ? ?

  • Extended with session types

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 5 / 19

slide-20
SLIDE 20

Excursion: Gradual Typing

Gradual Lambda Calculus with Sessions (Mini-GV)

T, U ::= S | B | T → U | T × U | ? ? ? any simple type R, S ::= end | !T.S | ?T.S | ? ? ?

  • any session type

Extended with type dynamic ? ? ? and dynamic session ? ? ?

  • Extended with session types

Mediated by explicit casts M : T ⇒ U

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 5 / 19

slide-21
SLIDE 21

Excursion: Gradual Typing

Gradual Lambda Calculus with Sessions (Mini-GV)

T, U ::= S | B | T → U | T × U | ? ? ? any simple type R, S ::= end | !T.S | ?T.S | ? ? ?

  • any session type

Extended with type dynamic ? ? ? and dynamic session ? ? ?

  • Extended with session types

Mediated by explicit casts M : T ⇒ U Complication: session types are linear

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 5 / 19

slide-22
SLIDE 22

Example CCCP Server with Gradual Typing Barrier

# typed part def cccp_server (c: CCCP ): (ccy , c) = receive (c) (amt , c) = receive (c) amt = toEUR(ccy , amt) # normalize to EUR exchange_rates (CAST(amt : Double => ?), CAST(c : CCCP -LOOP => ?)) # untyped part def exchange_rates (amt , c): for cy , r in available_rates (): sendDyn (True , c) sendDyn (cy , c) sendDyn (r * amt , c) else: sendDyn (False , c)

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 6 / 19

slide-23
SLIDE 23

What Can Go Wrong

def exchange_rates (amt , c): for cy , r in available_rates (): sendDyn (True , c) # MORE sendDyn (cy , c) # cy sendDyn (r * amt , c) # c else: sendDyn (False , c) # EXIT

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 7 / 19

slide-24
SLIDE 24

What Can Go Wrong

def exchange_rates (amt , c): for cy , r in available_rates (): sendDyn (True , c) # MORE Encoding of MORE = 1? sendDyn (cy , c) # cy sendDyn (r * amt , c) # c else: sendDyn (False , c) # EXIT

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 7 / 19

slide-25
SLIDE 25

What Can Go Wrong

def exchange_rates (amt , c): for cy , r in available_rates (): sendDyn (True , c) # MORE Encoding of MORE = 1? sendDyn (cy , c) # cy Number between 1 and N? sendDyn (r * amt , c) # c else: sendDyn (False , c) # EXIT

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 7 / 19

slide-26
SLIDE 26

What Can Go Wrong

def exchange_rates (amt , c): for cy , r in available_rates (): sendDyn (True , c) # MORE Encoding of MORE = 1? sendDyn (cy , c) # cy Number between 1 and N? sendDyn (r * amt , c) # c Uncoordinated use of channel else: sendDyn (False , c) # EXIT

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 7 / 19

slide-27
SLIDE 27

What Can Go Wrong

def exchange_rates (amt , c): for cy , r in available_rates (): sendDyn (True , c) # MORE Encoding of MORE = 1? sendDyn (cy , c) # cy Number between 1 and N? sendDyn (r * amt , c) # c Uncoordinated use of channel else: sendDyn (False , c) # EXIT Final send could be missing altogether

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 7 / 19

slide-28
SLIDE 28

Casting

Fundamental Theorem of Software Engineering

All problems in computer science can be solved by another level

  • f indirection.

David Wheeler/Butler Lampson

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 8 / 19

slide-29
SLIDE 29

Casting

Fundamental Theorem of Software Engineering

All problems in computer science can be solved by another level

  • f indirection.

David Wheeler/Butler Lampson

Casting linear values

Casting a value of linear type to ? ? ? introduces a wrapper. The wrapper is unrestricted. Casting the wrapped linear value unwraps it and locks the wrapper. Casting the locked wrapper via an alias raises blame. Releasing the linear value restores it into the wrapping and releases the lock.

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 8 / 19

slide-30
SLIDE 30

Cast to Dynamic

Cast to DYN

:S => (?)

:S

:(?)

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 9 / 19

slide-31
SLIDE 31

Cast from Dynamic

Cast from DYN

:S

:(?) => R locked

:S => R

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 10 / 19

slide-32
SLIDE 32

Cast from Dynamic/Blame

Cast from DYN

:(?) => R locked BLAME

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 11 / 19

slide-33
SLIDE 33

Release to Dynamic

Release to DYN

locked :S => (?)

:S

:(?)

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 12 / 19

slide-34
SLIDE 34

Garbage Collect

Garbage Collect

:S

BLAME

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 13 / 19

slide-35
SLIDE 35

Example code: sendDyn / receiveDyn

sendDyn : ? ? ? → ? ? ? → ? ? ? sendDyn = λx : ? ? ?. λc : ? ? ?. send x (c : ? ? ? ⇒!? ? ?. ? ? ? ) : ? ? ? ⇒ ? ? ? receiveDyn : ? ? ? → ? ? ? receiveDyn = λc : ? ? ?. fst(receive (c : ? ? ? ⇒?? ? ?. ? ? ? ))

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 14 / 19

slide-36
SLIDE 36

The Fine Print

Linear pairs and functions

needed for handling sessions cast to ? ? ? treated in the same way

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 15 / 19

slide-37
SLIDE 37

The Fine Print

Linear pairs and functions

needed for handling sessions cast to ? ? ? treated in the same way

Subtyping

unrestricted ⊑ linear consistent subtyping (for admissible casts)

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 15 / 19

slide-38
SLIDE 38

The Fine Print

Linear pairs and functions

needed for handling sessions cast to ? ? ? treated in the same way

Subtyping

unrestricted ⊑ linear consistent subtyping (for admissible casts)

Done?

No!

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 15 / 19

slide-39
SLIDE 39

Cast from Dynamic to Dynamic

Cast from DYN

:(?) :(?) => R locked :(?) => R

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 16 / 19

slide-40
SLIDE 40

Cast from Dynamic to Dynamic

Cast from DYN

:(?) :(?) => R locked :(?) => R

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 17 / 19

slide-41
SLIDE 41

Cast from Dynamic to Dynamic — Solution

Cast from DYN

:(?) : (?) => R : (?) => S :(?)

locked

: ~S => R

locked

: S => S Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 18 / 19

slide-42
SLIDE 42

Conclusions

Gradual session types Type soundness, blame preservation Fulfill (most of) refined criteria for gradual typing

Embedding of typed code Embedding of untyped code Gradual guarantee: n/a

Igarashi, Thiemann, Vasconcelos Gradual Session Types 31 Jan 2017 19 / 19