ChorEff
Choreographing Effects
Effect handlers in choreographic programming
Jonghyun Jung* · Cong Ma * · Yizhou Zhang
University of Waterloo
* equal contribution
Hi, I'm Cong. This is joint work with Jonghyun Jung and Yizhou Zhang. The story of this talk: I'll introduce choreographies, then a classic problem called knowledge of choice, and then show how it generalizes to a new problem, knowledge of control, once we add non-local control transfer through effect handlers. One running example throughout. (~20s)
This work
Non-local control flow in distributed programming
choreographies + effect handlers
The core contribution in one line: we bring non-local control flow, the kind you get from exceptions, early exits and effect handlers, to distributed programming. [click] We do it by combining choreographies with effect handlers. (~15s)
Choreographies
➜ def shop (title, budget) =
➜ if inStock title then
➜ let price = priceOf title in
➜ if price <= budget then
➜ ship title
else
()
else
()
● Buyer ● Seller
Our running example: a buyer B buys a book from a seller S. Here it is as an ordinary sequential program, with one program counter. [click] Check the inventory; [click] if the book is in stock, get the price; [click] if the price is within budget, [click] ship it. Nothing distributed yet. (~30s)
Choreographieslocated value comselect
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
locations / processes / machines
Now make it distributed. The function takes the processes involved, B and S. [click] We call them locations, or processes: think of each as a separate machine. And every value lives at exactly one process, which shows up in its type: the title and the budget are both at the buyer, string@B and int@B. (~30s)
Choreographieslocated value comselect
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
From now on, color shows location. Green is the buyer, red is the seller. The title and budget are green because they live at the buyer. inStock, priceOf and ship are red because they run at the seller, and so is the price they produce. Keep an eye on where green meets red. (~25s)
Choreographieslocated value comselect
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
But now the program doesn't type-check. inStock runs at the seller, and the title is at the buyer. A computation can only use values located at a single process. The same goes for priceOf and ship, which also run at the seller. Same problem further down: the price comes from the seller, but the budget is at the buyer, so they can't be compared. (~40s)
Choreographieslocated value comselect
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
The fix is communication. com moves a value from one process to another: the title goes from B to S, and the price comes back from S to B. Now there are two copies of the title and of the price, each copy keeps the same name, and its color says where it lives: green title at the buyer, red title at the seller. Every value is now used where it lives, which you can see from the colors: no green meets red anymore. The program type-checks as a choreography. (~50s)
Choreographieslocated value comselect
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
Now look at the first condition. inStock is computed only at the seller, so only the seller knows which branch we're in. But the buyer has to follow the same branch: in one it receives a price, in the other nothing. The buyer can't see the condition. This is the knowledge of choice problem. (~35s)
Choreographieslocated value comselect
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
The fix is selection. In each branch, the seller sends a label to the buyer with select: in_stock or out_of_stock. The red shading pairs the seller's condition with the selects that announce it. On the buyer's side, this becomes a match on the received label. (~35s)
Choreographieslocated value comselect
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
The second condition is the mirror image. Comparing the price with the budget happens only at the buyer, but the seller needs to know whether to ship the book. Same problem, the other direction. (~25s)
Choreographieslocated value comselect
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
So the buyer selects buy or cancel to tell the seller, shaded green with the buyer's condition. This is knowledge of choice in general: whoever owns a condition must inform everyone else involved in the branches. Blue is communication, orange is selection. (~35s)
Endpoint projection
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" ->
select (B, S) "cancel"
send S "cancel"
| "cancel" -> ()
select (S, B) "out_of_stock"
| "out_of_stock" -> ()
send B "out_of_stock"
Endpoint projection turns the choreography into one program per process. Each click projects one line of the choreography to both the buyer and the seller; communications take two clicks, sender first. com (B, S) becomes send S at the buyer, [click] and recv B at the seller. The seller's condition: the seller keeps the if, the buyer can't see it, so it vanishes there. [click] The seller selects a label, [click] and the buyer matches on it. [click] The rest follows the same rules, so it projects all at once: priceOf and ship run only at the seller, com (S, B) becomes send and recv, and the buyer's condition is the mirror image, with the buyer selecting buy or cancel and the seller matching on the label. [click] Together, the projected programs form a network. (~1m15s)
This work
choreographies + effect handlers
Back to the big picture. So far we've seen the first ingredient, choreographies. [click] Now the second ingredient: effect handlers. (~10s)
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
[click] Back to our shop function, as a plain sequential program, now returning the price it paid. [click] Now a bigger program around it. finance runs the transaction. [click] It sets the budget aside from the wallet, then calls shop. [click] If shopping fails, the budget is refunded; [click] if it succeeds, the seller is paid and the change goes back to the wallet. But how does shop tell finance that it failed? [click] Instead of returning unit when it fails, shop performs Abort. Control leaves shop immediately and jumps to the handler in finance, which refunds the wallet. When shopping succeeds, shop returns the price, and the return clause pays. The code that detects failure and the code that handles it live in different functions: that's non-local control transfer. (~70s)
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
First, recall what knowledge of choice is about. An if picks one of two branches, and the condition decides which. Inside a single program that's a local control transfer: the arrows go from the condition to the branch it selects, a few lines down. In a choreography, the hard part is that the condition lives at one process, so the others need to be told which arrow was taken. [click] So local control transfer, in a distributed program, needs knowledge of choice. [click] And choreographies already solve it with select: right after each condition, its owner tells the others which branch was taken. (~40s)
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
Now the same picture for effects. A perform also picks where execution continues, but the target is not a few lines down: it's the Abort clause of a handler, in a different function, possibly far away. That's a non-local control transfer. So every process that runs the handler, including ones that never saw shop, must learn which of these arrows was taken. That's knowledge of control. (~40s)
Knowledge of control
Non-local control transfer in distributed programs
needs knowledge of control
A perform is a non-local control transfer. Now the processes running the handler must learn where control went, and they may be far from the code that performed. [click] We call this knowledge of control. It is a new problem, and this work solves it. (~20s)
Knowledge of control
knowledge of choice: if knowledge of control: perform
who have the knowledge condition owner ?
who needs to know branch processes ?
how to send select?
how to receive receive and branch ?
Let's compare the two. [click] Who has the knowledge? For an if, the single process that evaluates the condition. [click] Who needs it? The processes that appear in the branches, right next to the condition. [click] How is it sent? The owner selects a label right after the condition. [click] How is it received? Each process in the branches receives the label and branches on it. [click] For a perform, all four answers are open: several processes may be involved in the perform, and the handler isn't known statically. Answering these four questions is our design. (~1m)
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
Who knows about a perform? Here is a handler whose body has two blocks. In the first, the buyer and the seller try to buy the book, and abort if it is out of stock. In the second, the seller sends the title to an auditor A, who audits it. Let's run it. [click] S sends the title to B. [click] Then, if out of stock, perform Abort. [click] Otherwise S sends the title to A. [click] Focus on the perform. [click] Let's project it to the three processes; I only keep the lines that matter. B and S are in the block where the perform is written, so they clearly perform Abort. But what about A, which is not in that block? [click] Suppose A does not take part: the perform is only by B and S. If the book is out of stock, B and S jump straight to the handler, skipping the send to the auditor. A was never told, so it sits at recv S, waiting for a title that will never come: deadlock. [click] So A must perform Abort too, even though it is in a different block. Now all three jump to the handler together. [click] These are the core processes of the handler: every process in its body, between handle and with. They all take part in every perform, so they all know when control leaves the body. That answers who has the knowledge. (~1m30s)
Knowledge of control
knowledge of choice: if knowledge of control: perform
who knows condition owner core processes
who needs the knowledge branch processes ?
how to send select?
how to receive receive and branch ?
Back to the table. Who knows that control left the handler body? The core processes: every process in the body of handle … with. They all perform together, so they all know. The last question remains: who else needs to know? (~15s)
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 )
Bank K
def finance(K) (wallet ) =
match recv B with
| "abort" ->
wallet := wallet + budget
| "deal" ->
let price = recv B in
wallet := wallet + (budget - price )
Who needs to know? Here is the full finance function as a choreography, with a bank K holding the wallet. The body of the handler, the call to shop, involves only the buyer and the seller: those are the core processes, and they all perform. (~20s)
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 )
Bank K
def finance(K) (wallet ) =
match recv B with
| "abort" ->
wallet := wallet + budget
| "deal" ->
let price = recv B in
wallet := wallet + (budget - price )
Now look at the handler clauses. They involve the buyer and the bank: the bank refunds or keeps the price, and in the return clause the buyer sends the bank the price. The bank is not in the body at all, so it never performs. Yet what it does next depends on which clause runs. (~25s)
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 )
Bank K
def finance(K) (wallet ) =
match recv B with
| "abort" ->
wallet := wallet + budget
| "deal" ->
let price = recv B in
wallet := wallet + (budget - price )
{ Buyer , Bank } handler processes ∖ { Buyer , Seller } core processes = { Bank } peripheral processes
Who needs the knowledge? Take the processes in the handler clauses, the buyer and the bank, and remove the core processes, the buyer and the seller. The buyer is core: it performed, so it already knows. What is left, the bank, has to be told which clause runs. We call these peripheral processes: they appear in the handler but not in the body, and a core process must tell them how control left the body. (~35s)
Knowledge of control
knowledge of choice: if knowledge of control: perform
who knows condition owner core processes
who needs to know branch processes peripheral processes
how to send the knowledge select?
how to receive receive and branch ?
So, who needs to know? The core processes know because they perform. The peripheral processes need to know too, and they must be told. Two questions remain: how to send the knowledge, and how to receive it. (~15s)
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 )
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 )
How do we send the knowledge? The core processes, the buyer and the seller, all performed, so they all know which clause runs. The bank, the peripheral process, does not. (~15s)
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 )
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 )
a core process selects
Any one core process can tell the peripherals, with an ordinary select at the start of each handler clause. Here the buyer selects "abort" or "deal" to the bank. select (S, K) works just as well: the seller is core too, so it also knows which clause runs. (~25s)
Knowledge of control
knowledge of choice: if knowledge of control: perform
who knows condition owner core processes
who needs to know branch processes peripheral processes
how to send selecta core process selects
how to receive the knowledge receive and branch ?
Sending is answered: one core process selects, at the start of each handler clause. Last question: how is it received? (~10s)
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 )
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
How does the bank receive it? Project the handler onto the bank. [click] Each select becomes an arm: the bank waits for the label from the buyer and matches on it, "abort" or "deal", and each arm runs the bank's own part of that clause. [click] But a handler can fire many times, think of a Yield in a loop, so the bank cannot listen just once. Look at k: in the choreography, k is the continuation of the Abort clause. [click] The bank's projection is wrapped in fix, a recursive choice, and fix binds the same k to 'go back and wait for the next label'. When the core processes resume by calling k, the bank calling k means it listens again. Abort never calls k, so here the loop simply ends. [click] So listen in a loop until the body returns. (~40s)
Knowledge of control
knowledge of choice: if knowledge of control: perform
who knows condition owner core processes
who needs to know branch processes peripheral processes
how to send selecta core process selects
how to receive receive and branch recv and match in a loop
And receiving: peripherals listen in a loop. That completes the table. For an if, a select tells everyone the choice. For a perform, all core processes perform together, any one of them selects to tell the peripherals, and the peripherals listen in a fix loop. That is knowledge of control. (~30s)
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
EPP
Our correctness result is a bisimulation between a choreography and its projection, which we draw as a square: the choreography on top, the network below, projection going down. Here is a tiny choreography: the bank calls forever, a server loop that by design never returns, and meanwhile the buyer sends the title to the seller. Its projection: the buyer sends, the seller receives, the bank runs forever. (~30s)
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
EPP network step
In the network, processes run independently. The buyer and the seller communicate right away, while the bank is still running forever. That is the bottom edge of the square. (~15s)
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
?
EPP network step ✗ call-by-value
no matching step
To close the square, the choreography must take a matching step on top. Under call-by-value it cannot: the com sits behind forever(K), which never returns, so the choreography is stuck forever while the network has moved. The bisimulation fails. (~25s)
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
EPP network step ✓ semi-lenient EPP
step out of order
The semi-lenient strategy lets the choreography step past a sequence, out of order, as long as the step does not depend on the bound value or involve the processes still computing it. The com uses neither forever's result nor K, so the choreography takes the top edge, and projecting the result gives exactly the network on the right. The square closes. This strategy comes from lambda-chi by Plyukhin, Qin and Montesi; we build effect handlers on top of it. (~35s)
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 B ✓ K ✗
Semi-lenient is also too strong. Start with the choreography: K calls forever, then a function applied to unit, whose body M involves K and B. [click] Project to K: forever, then K's part of the application. [click] Project to B: just its part of the application, with nothing in front. [click] In the choreography, app turns the application into let x = () in M, and the let substitutes the unit. Both steps sit after forever(K), and B is free to step, so the semi-lenient choreography takes them right away. [click] Can the network follow? B can: one ordinary beta step brings it to its projection of M. [click] K cannot: it is a call-by-value process stuck in forever, and it will never reach its own application. (~45s)
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′⟧
Project the new choreography: K must already be at the projection of M. B can get there for real with an ordinary beta step, since it has nothing to wait for. But the real K is a call-by-value process stuck in forever; it will never reach its own application. So the real network takes a step, but only B moves: B is at its projection of M, K is still in front of its application. That network is not the projection of the new choreography, and it never will be: the network can never become the projection of the new choreography, so the plain square fails again, now from the other side. (~30s)
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-lenient EPP B: step K: ⇝ prophecy
guess future steps
The fix, from lambda-chi, is the prophecy relation. It is a relation on networks used only in the proof, never at run time. A prophecy step may rewrite anywhere inside a process, even after a call that never returns, so it can take K's beta step now, the step K would take if forever ever returned: it guesses the future. B's beta step stays an ordinary network step; only K's is prophesied, shown in pink. With it, the projection of the old choreography prophesies the projection of the new one. (~35s)
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
Lambda-chi's prophecy relation has four rules: compute, prune, commute and bottom. Effect handlers need two more. [click] FixUnfold: a peripheral process listens for the handler clause in a fix loop. When the choreography takes a handle step, the peripheral has not received the label yet, so prophecy unfolds its loop one round to catch up. [click] HandleRet: when the handled computation M will not perform the handled effect, the choreography has effectively already reached the return clause, so prophecy lets the network apply the handler's return clause early. Checking that M performs no such effect takes an effect-typing judgment, which is why our prophecy relation is typed. (~45s)
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 .
We prove ChorEff correct, building on the semi-lenient semantics and the prophecy technique of Plyukhin, Qin and Montesi. Our main theorem: for any closed, well-typed choreography, the network projected from it and the choreography itself are weakly bisimilar, up to prophecy steps. So every run of the choreography is a run of the distributed program, and vice versa. [click] As a consequence, projected programs are deadlock-free: if the network gets stuck, every process has finished with a value. No process waits forever, as the auditor did in our broken example. (~40s)
Implementation in OCaml
ChorEff is embedded in OCaml 5, which already has effect handlers. Choreographic code sits inside a [%%choreff ...] block in an ordinary .ml file. [click] A PPX rewriter runs before OCaml's own type checker. It makes two passes over the syntax tree: first it checks the choreographic types, which only track locations, then it projects every expression to every process. [click] The output is one ordinary OCaml function per process, numbered by position in the process list: shop_0 for B and shop_1 for S. [click] The OCaml compiler then checks the base types and compiles them. [click] They link against a small runtime library that provides the communication primitives; it uses rendezvous channels and OCaml domains. (~35s)
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
Here is our bookshop, cut down to its core: the buyer sends a title, and the seller ships it or aborts with an effect if it is out of stock. Let's turn it into ChorEff code, one step at a time; I've left out type annotations and other details that don't matter here. [click] It goes inside a [%%choreff ...] block. That's an OCaml extension node: OCaml's parser reads its contents, so they must be valid OCaml syntax, and the PPX rewrites it before the compiler sees it. [click] Locations, which we showed with colour, become [@at ...] attributes. [click] Local computations are wrapped in [%at S ...]: S runs ordinary OCaml code on values it owns. The handler itself becomes an OCaml 5 effect handler; I've left that detail out. (~60s)
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
And here is what the PPX generates, tidied up. The buyer's function, shop_0, just sends the title; nothing else involves it. [click] The seller's function, shop_1, receives the title and runs the rest, with the handler now a plain OCaml 5 effect handler and the local computations unwrapped. Each function takes the other process's name as a parameter, and the runtime supplies send and recv. (~30s)
choreographies + effect handlers
solves knowledge of control
Choreographing Effects
Jonghyun Jung* · Cong Ma * · Yizhou Zhang
University of Waterloo
* equal contribution
To wrap up: we bring non-local control flow to distributed programming by combining choreographies with effect handlers. The key problem is knowledge of control: who has it, who needs it, and how to send and receive it. ChorEff answers all four. Thank you; happy to take questions. (~20s)