Lectures onType Theory
Operation trees, CBPV, and deep handlers
appendix sectionrules

Operation trees, CBPV, and deep handlers

For a finite signature Σ(op)=PopRop, well-founded operation trees, return, bind, and the handler fold are t::=Ret(a)Opop(p,k),k:RopTΣA,returna=Ret(a),Ret(a)=f=f(a),Opop(p,k)=f=Opop(p,λr.k(r)=f),foldr,h(Ret(a))=r(a),foldr,h(Opop(p,k))=hop(p,λq.foldr,h(k(q))). Here hop:Pop(RopC)C. A fold descends through a quotient by an effect theory exactly when it equalizes every generating equation under every tree-valued instantiation. It is sufficient, but stronger, for the entire Σ-algebra to satisfy every generating equation under every carrier-valued assignment.

The source calculus used by the call-by-value and call-by-name translations has the complete typing rules

x:τΓ
Γx:τ
S-Var
c:b is declared
Γc:b
S-Const
Γ,x:τe:σ
Γλx.e:τσ
S-Lam
Γe1:τσΓe2:τ
Γe1e2:σ
S-App

Writing (w) for a source constant or lambda, the two big-step relations are

wvw
V-Val
e1vλx.ee2vw2e[w2/x]vw
e1e2vw
V-App
wnw
N-Val
e1nλx.ee[e2/x]nw
e1e2nw
N-App

The effect-free calculus CBPV0 has A::=bUC,C::=FAAC,V::=xcthunkM,M::=returnVM to x.NforceV::=λx.MMV. Its complete typing rules are

x:AΓ
Γvx:A
V-Var
c:b declared
Γvc:b
V-Const
ΓcM:C
ΓvthunkM:UC
V-Thunk
ΓvV:A
ΓcreturnV:FA
C-Return
ΓcM:FAΓ,x:AcN:C
ΓcM to x.N:C
C-To
ΓvV:UC
ΓcforceV:C
C-Force
Γ,x:AcM:C
Γcλx.M:AC
C-Lam
ΓcM:ACΓvV:A
ΓcMV:C
C-App

Weak evaluation uses K::=[]K to x.NKV and the named roots (returnV) to x.NN[V/x],Toforce(thunkM)M,Force(λx.M)VM[V/x],BetaK[M]K[M](MM).Frame

For the annotated extension, A::=bUEC,C::=FAAEC,ΓcM:C!E. The variable and constant rules remain unchanged. The complete replacement rules are

ΓcM:C!E
ΓvthunkM:UEC
V-Thunk^Σ
ΓvV:A
ΓcreturnV:FA!
C-Return^Σ
ΓcM:FA!E1Γ,x:AcN:C!E2
ΓcM to x.N:C!(E1E2)
C-To^Σ
ΓvV:UEC
ΓcforceV:C!E
C-Force^Σ
Γ,x:AcM:C!E
Γcλx.M:AEC!
C-Lam^Σ
ΓcM:AE1C!E0ΓvV:A
ΓcMV:C!(E0E1)
C-App^Σ
Σ(op)=PRΓvV:PΓ,x:RcM:FA!E
ΓcopV(x.M):FA!({op}E)
C-Op
ΓcM:C!EEE
ΓcM:C!E
C-Weaken

The fixed pure state update used by the worked transaction is typed by

ΓvV:S
ΓvV+:S
V-Next

A handler is H={return xNr;opi(pi;ki)Ni}iI, with distinct operation names and H=handled(H). The judgment ΓhH:A[Ein]B[Eout] means all of the following premises hold: EinHEout,Γ,x:AcNr:FB!Eout,Γ,pi:Pi,ki:U(RiEoutFB)cNi:FB!Eout for every handled opi with Σ(opi)=PiRi. Its elimination rule is

ΓcM:FA!EinΓhH:A[Ein]B[Eout]
ΓchandleMwithH:FB!Eout
C-Handle

For an op-open evaluation context Xop, let k^=thunk(λy.handleXop[M[y/x]]withH). The exact deep-handler roots are handle(returnV)withHNr[V/x],HandleReturn,handleXop[opV(x.M)]withHNop[V/p,k^/k],HandleOp when H contains the displayed operation clause. If it does not, the Handle-Forward rule is handleXop[opV(x.M)]withHopV(y.handleXop[M[y/x]]withH). In the first-order agreement fragment, X::=[]X to x.LhandleXwithG,ophandled(G), and general application is excluded; only administrative resumed continuations are admitted.

Search the book

Type to search the local edition.