ChorEff

Choreographing Effects

Effect handlers in choreographic programming

Jonghyun Jung* · Cong Ma* · Yizhou Zhang
University of Waterloo
*equal contribution

This work

Non-local control flow
in distributed programming
choreographies + effect handlers

Choreographies

➜def shop (title, budget) =
➜  if inStock title then
➜    let price = priceOf title in
➜    if price <= budget then
➜      ship title
    else
      ()
  else
    ()

Choreographieslocated valuecomselect

def shop(B, S) (title : string@B, budget : int@B) =
  if inStock title then
    let price = priceOf title in
    if price <= budget then
      ship title
    else
      ()
  else
    ()
locations /
processes /
machines

Choreographieslocated valuecomselect

def shop(B, S) (title : string@B, budget : int@B) =
  if inStock title then
    let price = priceOf title in
    if price <= budget then
      ship title
    else
      ()
  else
    ()
● Buyer● Seller

Choreographieslocated valuecomselect

def shop(B, S) (title : string@B, budget : int@B) =
  if inStock title then
    let price = priceOf title in
    if price <= budget then
      ship title
    else
      ()
  else
    ()
● Buyer● Seller

Choreographieslocated valuecomselect

def shop(B, S) (title : string@B, budget : int@B) =
  let title = com (B, S) title in
  if inStock title then
    let price = priceOf title in
    let price = com (S, B) price in
    if price <= budget then
      ship title
    else
      ()
  else
    ()
● Buyer● Seller

Choreographieslocated valuecomselect

def shop(B, S) (title : string@B, budget : int@B) =
  let title = com (B, S) title in
  if inStock title then ✗
    let price = priceOf title in
    let price = com (S, B) price in
    if price <= budget then
      ship title
    else
      ()
  else
    ()
● Buyer● Seller

Choreographieslocated valuecomselect

def shop(B, S) (title : string@B, budget : int@B) =
  let title = com (B, S) title in
  if inStock title then
    select (S, B) "in_stock";
    let price = priceOf title in
    let price = com (S, B) price in
    if price <= budget then
      ship title
    else
      ()
  else
    select (S, B) "out_of_stock"
● Buyer● Seller

Choreographieslocated valuecomselect

def shop(B, S) (title : string@B, budget : int@B) =
  let title = com (B, S) title in
  if inStock title then
    select (S, B) "in_stock";
    let price = priceOf title in
    let price = com (S, B) price in
    if price <= budget then ✗
      ship title
    else
      ()
  else
    select (S, B) "out_of_stock"
● Buyer● Seller

Choreographieslocated valuecomselect

def shop(B, S) (title : string@B, budget : int@B) =
  let title = com (B, S) title in
  if inStock title then
    select (S, B) "in_stock";
    let price = priceOf title in
    let price = com (S, B) price in
    if price <= budget then
      select (B, S) "buy";
      ship title
    else
      select (B, S) "cancel"
  else
    select (S, B) "out_of_stock"
● Buyer● Seller

Endpoint projection

EPP
Choreography
Buyer B
Seller S
def shop(B, S) (title, budget) =
def buyer(S) (title, budget) =
def seller(B) () =
let title = com (B, S) title in
send S title;
let title = recv B in
if inStock title then
·
if inStock title then
select (S, B) "in_stock";
match recv S with | "in_stock" ->
send B "in_stock";
let price = priceOf title in
·
let price = priceOf title in
let price = com (S, B) price in
let price = recv S in
send B price;
if price <= budget then
if price <= budget then
·
select (B, S) "buy";
send S "buy"
match recv B with | "buy" ->
ship title
·
ship title
else
else
·
select (B, S) "cancel"
send S "cancel"
| "cancel" -> ()
else
·
else
select (S, B) "out_of_stock"
| "out_of_stock" -> ()
send B "out_of_stock"
network

This work

choreographies + effect handlers

Effect handlers introduce non-local control transfer

def finance(title, budget, wallet) =
➜  wallet := wallet - budget;
  handle
    shop(title, budget)
  with
  | Abort k ->
➜      wallet := wallet + budget
  | return price ->
➜      wallet := wallet + (budget - price)
def shop(title, budget) =
  if inStock title then
    let price = priceOf title in
    if price <= budget then
      ship title;
      price
    else
      ()perform Abort
  else
    ()perform Abort

Knowledge of choice

def finance(title, budget, wallet) =
  wallet := wallet - budget;
  handle
    shop(title, budget)
  with
  | Abort k ->
      wallet := wallet + budget
  | return price ->
      wallet := wallet + (budget - price)
def shop(title, budget) =
  if inStock title then
    select (S, B) "in_stock";
    let price = priceOf title in
    if price <= budget then
      select (B, S) "buy";
      ship title;
      price
    else
      select (B, S) "cancel";
      perform Abort
  else
    select (S, B) "out_of_stock";
    perform Abort
Local control transfer
in distributed programs
needs knowledge of choice

Knowledge of control

def finance(title, budget, wallet) =
  wallet := wallet - budget;
  handle
    shop(title, budget)
  with
  | Abort k ->
      wallet := wallet + budget
  | return price ->
      wallet := wallet + (budget - price)
def shop(title, budget) =
  if inStock title then
    let price = priceOf title in
    if price <= budget then
      ship title;
      price
    else
      perform Abort
  else
    perform Abort

Knowledge of control

Non-local control transfer
in distributed programs
needs knowledge of control

Knowledge of control

knowledge of choice: ifknowledge of control: perform
who have the knowledgecondition owner?
who needs to knowbranch processes?
how to sendselect?
how to receivereceive and branch?

Who have the knowledge?

Choreography
handle
  if inStock title then
    ➜com (S, B) title
  else
    ➜perform Abort(B, S, A);
  ➜let title = com (S, A) title in
    audit title
with
| Abort k -> ()
Buyer B
handle
  ·
    recv S
  ·
    perform Abort
  ·
    ·
with
​| Abort k -> ()
Seller S
handle
  if ⋯ then
    send B
  else
    perform Abort
  send A
    ·
with
​| Abort k -> ()
Auditor A
handle
  ·
    ·
  ·
     ·perform Abort
  recv S
    audit ⋯
with
​| Abort k -> ()
✗ waits forever at recv S
core processes
of handle … with

Knowledge of control

knowledge of choice: ifknowledge of control: perform
who knowscondition ownercore processes
who needs the knowledgebranch processes?
how to sendselect?
how to receivereceive and branch?

Who needs the knowledge?

Choreography
def finance(B, S, K) (title, budget, wallet) =
  handle
    shop(B, S) (title, budget)
  with
  | Abort k ->
    wallet := wallet + budget
  | return price ->
    let price = com (B, K) price in
    wallet := wallet + (budget - price)

Who needs the knowledge?

Choreography
def finance(B, S, K) (title, budget, wallet) =
  handle
    shop(B, S) (title, budget)
  with
  | Abort k ->
    wallet := wallet + budget
  | return price ->
    let price = com (B, K) price in
    wallet := wallet + (budget - price)

Who needs the knowledge?

Choreography
def finance(B, S, K) (title, budget, wallet) =
  handle
    shop(B, S) (title, budget)
  with
  | Abort k ->
    wallet := wallet + budget
  | return price ->
    let price = com (B, K) price in
    wallet := wallet + (budget - price)
{ Buyer, Bank }handler processes∖{ Buyer, Seller }core processes={ Bank }peripheral processes

Knowledge of control

knowledge of choice: ifknowledge of control: perform
who knowscondition ownercore processes
who needs to knowbranch processesperipheral processes
how to send the knowledgeselect?
how to receivereceive and branch?

How to send the knowledge?

Choreography
def finance(B, S, K) (title, budget, wallet) =
  handle
    shop(B, S) (title, budget)
  with
  | Abort k ->
    wallet := wallet + budget
  | return price ->
    let price = com (B, K) price in
    wallet := wallet + (budget - price)

How to send the knowledge?

Choreography
def finance(B, S, K) (title, budget, wallet) =
  handle
    shop(B, S) (title, budget)
  with
  | Abort k ->
    select (B, K) "abort";select (S, K) also works
    wallet := wallet + budget
  | return price ->
    select (B, K) "deal";
    let price = com (B, K) price in
    wallet := wallet + (budget - price)
a core process selects

Knowledge of control

knowledge of choice: ifknowledge of control: perform
who knowscondition ownercore processes
who needs to knowbranch processesperipheral processes
how to sendselecta core process selects
how to receive the knowledgereceive and branch?

How to receive the knowledge?

Choreography
def finance(B, S, K) (title, budget, wallet) =
  handle
    shop(B, S) (title, budget)
  with
  | Abort k ->
    select (B, K) "abort";
    wallet := wallet + budget
  | return price ->
    select (B, K) "deal";
    let price = com (B, K) price in
    wallet := wallet + (budget - price)
EPP
Bank K
def finance(K) (wallet) =
  fix k.
  match recv B with
  | "abort" ->
    wallet := wallet + budget
  | "deal" ->
    let price = recv B in
    wallet := wallet + (budget - price)
recv and match in a loop

Knowledge of control

knowledge of choice: ifknowledge of control: perform
who knowscondition ownercore processes
who needs to knowbranch processesperipheral processes
how to sendselecta core process selects
how to receivereceive and branchrecv and match in a loop

Semi-lenient semantics

Choreography
forever(K);
com (B, S) title
Network
B: send S title
S: recv B
K: forever()
EPP

Semi-lenient semantics

Choreography
forever(K);
com (B, S) title
Network
B: send S title
S: recv B
K: forever()
Network
B: ()
S: title
K: forever()
EPPnetwork step

Semi-lenient semantics

Choreography
forever(K);
com (B, S) title
Network
B: send S title
S: recv B
K: forever()
Network
B: ()
S: title
K: forever()
Choreography
?
EPPnetwork step✗ call-by-value
no matching step

Semi-lenient semantics

Choreography
forever(K);
com (B, S) title
Network
B: send S title
S: recv B
K: forever()
Network
B: ()
S: title
K: forever()
Choreography
forever(K);
title
EPPnetwork step✓ semi-lenientEPP
step out of order

Prophecy

Choreography
forever(K);
(λx. M) ()
Choreography
forever(K);
M
Network
K: forever();
   (λx. ⟦M⟧K) ()
B: (λx. ⟦M⟧B) ()
Network
K: forever();
   (λx. ⟦M⟧K) () ✗
B: ⟦M⟧B ✓
EPP✓ semi-lenientB ✓K ✗

Prophecy

Choreography
forever(K);
(λx. M) ()
Choreography
forever(K);
M
Network
K: forever();
   (λx. ⟦M⟧K) ()
B: (λx. ⟦M⟧B) ()
Network
K: forever();
   (λx. ⟦M⟧K) () ✗
B: ⟦M⟧B ✓
EPP✓ semi-lenient✗network step
never reaches ⟦M′⟧

Prophecy

Choreography
forever(K);
(λx. M) ()
Choreography
forever(K);
M
Network
K: forever();
   (λx. ⟦M⟧K) ()
B: (λx. ⟦M⟧B) ()
Network
K: forever();
   ⟦M⟧K
B: ⟦M⟧B
EPP✓ semi-lenientEPPB: stepK: ⇝ prophecy
guess future steps

New prophecy rules

FixUnfold
 
fix k. &{li: Mi} ⇝ &{li: Mi[k ≔ fix k. &{li: Mi}]}
HandleRet
(return ↦ λy. M2) ∈ H
handle M with H ⇝ (λy. M2) M

Correctness

Weak bisimulation
For any closed, well-typed, effect-free choreography M,
the relation 𝒩 ⇝* ⟦M⟧ is a weak bisimulation.
Deadlock freedom
For any well-typed, effect-free choreography M,
if ⟦M⟧ evaluates to a network 𝒩 that cannot step further,
then 𝒩(p) is a value for every process p.

Implementation in OCaml

ChorEff
choreography
code
→
PPX
EPP
  1. type check
  2. project
→
Projections
shop_0
shop_1
→
OCaml
compiler
→
runtime
library

Implementation in OCaml

[%%choreff
let shop(B, S) (title [@at B]) =
  let title = com (B, S) title in
  handle
    if [%at S inStock title] then
      [%at S ship title]
    else
      perform Abort(S)
  with
  | Abort k -> ()
]
[%%choreff …]handed to the PPX
[@at B]location
[%at S e]local computation

Implementation in OCaml

Buyer B
let shop_0 proc_S title =
  send proc_S title
Seller S
let shop_1 proc_B _ =
  let title = recv proc_B in
  match
    if inStock title then
      ship title
    else
      perform Abort
  with
  | effect Abort, k -> ()
  | x -> x
choreographies + effect handlers
solves knowledge of control
Choreographing Effects
Jonghyun Jung* · Cong Ma* · Yizhou Zhang
University of Waterloo
*equal contribution
Slide Elapsed 0:00N: hide notes · T: reset timer
Keys
→ Space PgDn next   ← PgUp back
Home / End first / last slide
F fullscreen   N speaker notes   T reset timer
G slide overview   ? this help
Print to PDF (landscape, no margins) for one slide per page.