Lectures onType Theory
Chapter 58
Chapter 58Optional

Binary Session Types and Typed Protocols

Prerequisites. Direct starred prerequisites: none. No later core chapter depends on this route.

A function type describes one call. A protocol type describes the whole conversation that follows from one channel endpoint. The distinction first appears when a client sends an argument, receives a result, and then reaches the terminal protocol: neither the argument type nor the result type records that order. This chapter fixes a finite, binary, intuitionistic linear session calculus whose proof theory does record it. A later, explicitly marked route studies graded non-linear communication in a different calculus. Results do not transfer between the two cards.

A finite logical session calculus

Session types are propositions of intuitionistic linear logic: A,B::=1ABABABA&B!A. The provider of z:A offers the behavior A on z. The synchronous binary-choice pi-calculus has grammar P,Q::=0PQ(νx)Pxy.Px(y).P!x(y).Px.inl;Px.inr;Px.case(P,Q). A linear context Δ contains endpoints that it must use exactly once; an unrestricted context Γ contains replicated services. The judgment is Γ;ΔP::z:A. Names in Γ, Δ, and z are pairwise disjoint. In the rules, (νx)(PQ) abbreviates (νx)(PQ), and (νy)xy.(PQ) abbreviates (νy)xy.(PQ). Thus P provides the fresh channel y, while Q continues on x. These are typed abbreviations, not new process constructors.

The rules below fix the polarity of every constructor. Exchange is admissible for both contexts; contraction and weakening are admissible only for Γ.

Γ;0::z:1
1R
Γ;ΔP::z:C
Γ;Δ,x:1P::z:C
1L
Γ;Δ1P::y:AΓ;Δ2Q::x:B
Γ;Δ1,Δ2(νy)xy.(PQ)::x:AB
Γ;Δ,y:A,x:BP::z:C
Γ;Δ,x:ABx(y).P::z:C
Γ;Δ,y:AP::x:B
Γ;Δx(y).P::x:AB
μltimapR
Γ;Δ1P::y:AΓ;Δ2,x:BQ::z:C
Γ;Δ1,Δ2,x:AB(νy)xy.(PQ)::z:C
μltimapL
Γ;Δ1P::x:AΓ;Δ2,x:AQ::z:C
Γ;Δ1,Δ2(νx)(PQ)::z:C
Cut

For AB, the provider selects and the client branches; for A&B, the client selects and the provider branches. The full additive rule sheet is

Γ;ΔP::x:A
Γ;Δx.inl;P::x:AB
⊕R_1
Γ;ΔP::x:B
Γ;Δx.inr;P::x:AB
⊕R_2
Γ;Δ,x:AP::z:CΓ;Δ,x:BQ::z:C
Γ;Δ,x:ABx.case(P,Q)::z:C
⊕L
Γ;ΔP::x:AΓ;ΔQ::x:B
Γ;Δx.case(P,Q)::x:A&B
Γ;Δ,x:AP::z:C
Γ;Δ,x:A&Bx.inl;P::z:C
_1
Γ;Δ,x:BP::z:C
Γ;Δ,x:A&Bx.inr;P::z:C
_2

The exponential and persistent-cut rules are

Γ;P::y:A
Γ;!x(y).P::x:!A
!R
Γ,u:A;ΔP::z:C
Γ;Δ,x:!AP[x/u]::z:C
!L
Γ,u:A;Δ,y:AP::z:C
Γ,u:A;Δ(νy)uy.P::z:C
Copy
Γ;P::y:AΓ,u:A;ΔQ::z:C
Γ;Δ(νu)(!u(y).PQ)::z:C
Cut!

The replicated cut requires its provider to have no linear clients. This empty-context premise is the operational reason that copies do not duplicate linear endpoints.

The unit protocol already gives a complete interaction. Define Rx:=(νm)xm.(00),Px:=x(n).x.inl;Rx,Qx:=(νn)xn.(0x.case(x(m).0,0)). Here Px provides x:1((11)1). The process Qx consumes that channel and provides z:1. Every channel of type 1 is discharged by 1L, while the final 0 is typed by 1R. Put A:=1((11)1). The complete derivation has the following spine; the displayed rule sheet determines the omitted nullary 1R leaves: ;0::m:1;0::x:1;n:1Rx::x:11;n:1x.inl;Rx::x:(11)1R1;Px::x:AμltimapR;0::n:1;m:1,x:10::z:1;x:11x(m).0::z:1;x:10::z:1;x:(11)1x.case(x(m).0,0)::z:1L;x:AQx::z:1μltimapL;(νx)(PxQx)::z:1Cut. Here uses of 1L discharge n,m,x:1 in the indicated leaves. Principal reductions give (νx)(PxQx)0(νx)(x.inl;Rxx.case(x(m).0,0))0(νx)(Rxx(m).0)0,10. The interaction sends a request channel, selects the quotation branch, sends the result channel, and reaches inaction. No close or wait prefix has been silently added to the selected calculus.

Exercise 58.1

★☆☆ Treat Nat as a fixed closed protocol for one natural number. Give a type for a service that receives a natural number, sends either a quoted natural number or a refusal, and reaches 1. Write the provider and client actions in order, from each endpoint’s point of view.

Cut elimination is communication

Principal cut reductions synchronize dual rule introductions. Representative cases are (νx)((νy)xy.(PQ)x(y).R)0(νx)(Q(νy)(PR)),(νx)(x.inl;Px.case(Q1,Q2))0(νx)(PQ1),(νx)(0P)Pwhen xfn(P). Reduction is closed under restriction and parallel contexts, up to the structural congruence that reassociates and commutes independent cuts. Replicated cuts spawn one fresh linear instance per request and retain the shared server.

Theorem 58.1 — Type preservation for the selected calculus

If Γ;ΔP::z:A and PQ, then Γ;ΔQ::z:A.

Proof of Theorem 58.1 — Type preservation for the selected calculus

Proof. Proceed by the reduction derivation. A principal step replaces a cut between the introduction rules for the same connective by cuts on its immediate subformulas. The displayed tensor step uses the two tensor premises and the client continuation, then applies Cut first at y:A and then at x:B. The additive cases select an existing premise. The unit case is structural: 1L adds no process action and 1R contributes only inaction. The exponential case uses the empty linear context of !R; admissible renaming gives the fresh instance. Congruence cases rebuild the typing rule around the induction hypothesis. The persistent case uses Copy to spawn a fresh linear instance while retaining the replicated provider. It remains possible that the cut formula is not principal in one premise. Apply the corresponding commuting conversion to move the cut above that premise’s last rule; its derivation height decreases. The induction hypothesis then types the converted reduct, and the inverse commuting conversion restores the required process modulo structural congruence. This is why preservation is stated for reduction closed under structural congruence, rather than for the three principal contractions alone. ◻

Preservation says that a step respects the protocol. Progress needs an additional syntactic guard. Call P live when, up to structural congruence, P(νn~)(π.QR), where π.Q is any nonreplicated guarded process: input, output, left or right selection, or branching. This excludes a process consisting only of replicated servers but includes the intermediate selection state in the worked interaction.

Theorem 58.2 — Closed progress for the selected calculus

If an intuitionistic linear-logic derivation establishes ;P::x:1 and P is live, then there is a Q such that PQ.

Proof of Theorem 58.2 — Closed progress for the selected calculus

Proof. Normalize the typing derivation by commuting cuts past rules that do not act on the cut channel. Two inversions provide the mechanism. First, if the last rule types a guarded prefix whose subject is a, then a occurs in the typing interface with the connective introduced by that prefix. Second, a cut-free derivation of a non-live process has no nonreplicated guarded prefix. These are inductions on the displayed rule sheet; the additive cases inspect both premises and the exponential case is excluded by nonreplication.

Now suppose the normalized derivation is live. Its guard cannot be exposed at the external interface: the linear context is empty, and the offered channel has type 1, whose only right rule is 1R and has no action. The guard is therefore below a cut. Normality makes that cut principal, so its two premises end in rules introducing the same connective on the cut channel. The matching principal contraction applies. This reconstructs the source’s action-inversion and non-live-derivation lemmas for the closed instance. ◻

The source theorem also has a contextual form: a typed live process either reduces internally or exposes a labelled action whose subject is in its interface; when that interface type is !A, the subject is not the offered channel. Fixing the interface at empty Γ,Δ and x:1 removes the second alternative: there is no contextual subject, and 1 admits no labelled action on x [CPT11]. This is a genuine global deadlock-freedom result for this proof-shaped finite calculus. It is not a theorem about arbitrary binary session programs, several independently created sessions, asynchronous queues, fairness, or termination.

Exercise 58.2

★★☆ Explain why preservation alone does not rule out a cycle in which two processes each wait first on a different session. Identify the premise of Theorem 58.2 that prevents using that cycle as a counterexample to the theorem.

Recursive protocols

The finite grammar cannot describe a server that handles arbitrarily many requests. For this section S,T range over the same linear protocol sort as A,B, extended with variables and equi-recursive types: S,T::=tμt.S,μt.SS[μt.S/t]. A body is contractive when every occurrence of its bound variable lies strictly beneath a communication constructor—send, receive, internal choice, or external choice. In particular, μt.t and μt.μu.t are rejected. This chapter makes the additional tail-recursion restriction required by its syntactic duality: at a channel-send or channel-receive constructor, the transmitted session type is closed with respect to every surrounding recursion variable. Thus μt.(At) is admitted when tfv(A), whereas μt.(t1) is not. Recursive variables may still occur in continuations and choice branches.

Write ST for the greatest relation generated coinductively by matching outer constructors and the two unfolding clauses S[μt.S/t]Tμt.STEqUnfoldLST[μt.T/t]Sμt.TEqUnfoldR. For example, the tensor congruence clause requires the same closed transmitted type and related continuations. The other four constructor clauses are defined by the same outer-constructor test. Add recursive conversion explicitly: Γ;Δ,x:SP::z:CSTΓ;Δ,x:TP::z:CTRecConv. This rule is the only way recursive equality enters typing.

The following syntactic duality is an auxiliary endpoint view; the intuitionistic judgment above itself assigns the same cut formula to provider and client. It is defined on the linear fragment, not on the unrestricted service constructor !A: 1=1t=t,AS=ASAS=AS,ST=S&TS&T=ST,μt.S=μt.S. On the contractive, tail-recursive fragment it satisfies μt.S=μt.S,S=S up to recursive session equality. Contractiveness makes observation of the next communication productive; closed transmitted types make the displayed syntactic duality sound.

Lemma 58.3 — Unfolding and duality

For contractive, tail-recursive S, unfolding either endpoint once preserves duality: S[μt.S/t]S[μt.S/t]. Moreover, T-Rec-Conv preserves one-step typing reduction: if its premise types P and PQ, then its conclusion types Q.

Proof of Lemma 58.3 — Unfolding and duality

Proof. First prove the displayed substitution equation by induction on the tail-recursive formation derivation. At send and receive, the recursion substitution does not enter the closed transmitted type; it enters only the continuation, where the induction hypothesis applies. Choice uses the hypothesis branchwise; variables and 1 are immediate. Contractiveness ensures that coinductive comparison exposes one of these constructors after finitely many unfoldings.

For typing preservation, invert T-Rec-Conv. Apply theorem 58.1 to its premise and the same process step, then reapply T-Rec-Conv with the unchanged equality ST. If the redex is a cut exposed only after unfolding, use the substitution equation to obtain matching outer constructors, take the finite principal step, and reapply conversion to the residual continuation. Thus the new case does not assume the desired result. ◻

Contractiveness alone is insufficient. For R=μt.(t1), naively dualizing the recursive syntax gives a received payload R, whereas dualizing the unfolding requires the unchanged payload R. The two need not be equal. The source proves naive syntactic duality sound only for tail-recursive types, meaning that every message type is closed. Arbitrary contractive session types require message closure or one of the paper’s corrected duality functions [GTV20]; neither is silently folded into this local extension.

Exercise 58.3

★★☆ Define a contractive protocol that repeatedly offers either a natural-number request followed by a natural-number answer, or termination. Compute its dual and unfold both sides once.

The finite rules can prescribe the kind and order of messages, but the continuation type cannot depend on the value just received. Adding such a dependency requires index equality, substitution into protocols, and a decision about whether indices are erased or communicated. Those are new proof obligations, not decorations on the finite logical rules.

Optional route.

Chapter 20.

Graded non-linear communication

This section changes calculi. It follows the final journal system of Marshall and Orchard, where a linear channel can be promoted to a shared, replicated, multicast, or graded service [MO24b]. No theorem in this section is obtained by translating the finite logical calculus in section 58.1.

The operational state distinguishes typed terms, processes, and channel configurations. Write a channel configuration as C;Δ, with resource environment Δ. Before stating progress, define buffered(C,Δ): every runtime channel c assigned Chan(Recv A P) by Δ contains at least one head value. The inductive clauses leave empty buffers available at End and Send-typed channels; all other structurally paired configuration clauses are discharged without a buffer-nonemptiness premise. The syntactic predicate resourceAllocator(t) classifies terms whose reduction creates a channel, including reducible uses of the fork primitives. Graded promotion requires ¬resourceAllocator(t). A lambda containing a fork is not yet an allocator because evaluation does not proceed beneath lambdas.

Each non-linear primitive carries its own side conditions:

Shared service.

forkNonLinear requires SingleAction and ExactSemiring. The latter excludes approximate grades. The journal’s surface presentation lists five shapes: EndSend A EndRecv A EndOffer End EndSelect End End. The core runtime-context definition lists only the first three shapes, whereas the surface rules and implementation also admit the two choices. This chapter uses the five-shape surface predicate and records the discrepancy.

Repeated receive.

forkReplicate requires ReceivePrefix; the replicated body begins with a receive or an external offer rather than with an unrelated action.

Multicast.

forkMulticast requires Sends(P). Its channel type contains Graded n P, the recursively defined type function that grades every payload by n; Graded is not a predicate or an additional premise.

Dropping these conditions can strand a client or allocate a different number of endpoints from the one recorded in the configuration type.

Theorem 58.4 — Journal progress and preservation card

Use the final journal calculus. Its primitive rules require SingleAction and ExactSemiring for forkNonLinear, ReceivePrefix for forkReplicate, and Sends for forkMulticast, whose input has type Chan(Graded n P); a promotion body u must satisfy ¬resourceAllocator(u).

  1. If a term t:A, process configuration P:B, and channel configuration C are well typed at runtime context Δ, and buffered(C,Δ), then t is a value or the whole configuration ((Pt);C) takes a global step, possibly changing all three parts.

  2. If a process configuration P:A and channel configuration C are well typed at Δ, and buffered(C,Δ), then every process is a value, forwarder, or watcher, or (P;C) takes a global step.

  3. If a well-typed process/channel configuration at runtime context Δ steps, then there exists a post-context Δ in which the reduct is well typed; the theorem does not assert Δ=Δ.

Proof of Theorem 58.4 — Journal progress and preservation card

Imported proof. The first two clauses are respectively Theorems 1 and 2 on term and global progress in Appendix C of [MO24b]; the third is Theorem 3 on preservation, stated on physical PDF p. 21 and proved in Appendix D on physical PDF pp. 46–53. Their hypotheses mention typed terms, processes, channels, and bufferedness separately. Suppressing those distinctions produces a stronger and false slogan.

A small example ladder separates the primitives. A shared logger uses forkNonLinear for any number of single-action clients. A pool of exactly k workers additionally uses an exact natural grade and an allocator that partitions k. A multicast announcement uses forkMulticast; its Sends witness records that its protocol sends, while the type function Graded n marks every payload for n recipients. These programs share notation, but their side-condition proofs are different.

The pinned 2022 Granule merge supplies an exact version map. Its examples/Sessions.gr contains the PLACES-era sendVec, recvVec, and graded reusable-channel example, while frontend/tests/cases/positive/testConstraintSessions.gr exercises forkNonLinear. In that snapshot its type requires SingleAction but omits the journal rule’s ExactSemiring; this is version evidence, not a derivation of the journal rule. No pinned example or test names forkReplicate or forkMulticast; the file named replicate.gr duplicates an ordinary graded value, not a session. Therefore only the earlier shared-channel fragment has exercising examples and tests in that snapshot, although the other primitives are implemented. The journal core additionally separates term, process, and channel configurations, states buffered progress, and introduces runtime forms and side conditions that the snapshot does not certify. The final article also leaves deadlock freedom as future work. We claim neither that conjecture nor an implementation correspondence with the journal calculus. ◻

Exercise 58.4

★★★ For each of SingleAction, ReceivePrefix, and Sends, describe one smallest process shape that violates it and the stuck or mistyped configuration that the condition excludes.

Remark 58.5

Binary session typing turns a protocol into a linear proof: cut elimination executes the conversation, preservation protects its shape, and the exact closed logical fragment proves progress. Recursive types add an explicitly contractive, tail-recursive observation principle. Graded non-linear communication changes the calculus and therefore changes the theorem hypotheses. Several roles and asynchronous queues require more than a single pair of dual endpoints.

Chapter seminar

The companion Kappa corpus at artifacts/ch58-session-protocols/ encodes finite action traces, pointwise action duality, and compatibility. Its empty trace marks type 1; it is not a close or wait action. The acceptance matrix contains empty termination, tensor communication, both choices, a pre-unrolled request/answer trace, and a deliberately mismatched pair. The executable is a checker for these examples, not an implementation of the process calculus or a mechanization of either theorem card.

Suggested first pass.

Do exercise 58.5, exercise 58.6 before exercise 58.8.

Exercise 58.5

★★☆ Reconstruct the typing derivation for the tensor principal contraction in section 58.2. Identify the two smaller cut formulas and show that the external context is unchanged.

Exercise 58.6

★★☆ Show that (νx)(x.inl;Px.case(Q1,Q2)) is live under the chapter’s definition even when neither P nor Qi begins with input or output. Then explain why the earlier, input/output-only definition would make the progress theorem unusable for this state.

Exercise 58.7

★★☆ For S=μt.(1t), unfold provider and endpoint-dual views once. Exhibit the use of T-Rec-Conv required before the next principal communication and state why tail recursion holds.

Exercise 58.8

★★★ Practical project.binary-session-protocol-checker Run the corpus, add one compatible and one incompatible pre-unrolled trace drawn from a tail-recursive protocol, and explain which outer dual constructors the checker compares at each step. State why this finite test is not a check of recursive duality.

Search the book

Type to search the local edition.