Lectures onType Theory
Chapter 33
Chapter 33Optional

Modal Effect Types and Source-to-Met Encodings

Prerequisites. Direct starred prerequisites: Chapter 32; Chapter 25 supplies the row interface used in the first source card. No later core chapter depends on this route.

Consider two library functions that both return unit. A row system might assign f:1ask1,g:11, while a capability system makes the authority of f an extra block parameter. The annotations live in different places, but each says how the ambient effect context changes while the function body runs. Writing that change as a modality separates it from the ordinary arrow: fr:[ask](11),gr:[](11). Here [E] discards the current effect context and installs E. A relative modality D instead extends the ambient context by D: [E](F)=E,D(F)=D,F. If the ambient context is F={write}, then [{ask}](F)={ask},ask(F)={ask,write}. An absolute modality forgets the ambient permission; an extension modality retains it.

The comparison requires three calculi, not one notation with three spellings.

  1. Met[T] is the modal target. Its effect structure T determines equality and inclusion of effect contexts.

  2. System Fε is a row-annotated source. Its encoding uses absolute modalities over scoped rows.

  3. System C is a capability source with first-class values and second-class blocks. Its encoding uses effect variables and modal boxes over sets.

Both source languages translate to the modal target. There is no translation here from System Fε to System C, no translation in the opposite direction, and no claim that either source translation is an inverse.

Exercise 33.1 — Absolute versus relative

★☆☆ Let F={read}. Compute [{write}](F), write(F), and ([{write}]read)(F), using the left-to-right convention for composition. State which result retains the ambient read effect.

The modal target

An effect structure T consists of a kinding relation ΓD:Eff for finite extensions and an equivalence ΓDTD, with well-formedness closed under concatenation. Effect contexts are D::=,Dϵ,D,E,F::=ϵD,E. Kinding and equivalence extend componentwise. Subeffecting is derived: EeFD.ΓE,DTF. Simple rows forbid duplicate labels and identify reorderings. Scoped rows permit duplicates and commute adjacent distinct labels. Sets permit labels and effect variables and quotient by idempotent commutative union. The row encoding uses the scoped-row instance Rsc; the capability encoding uses the set instance S.

For progress, the effect structure must satisfy two validity conditions: EeE=,e,E  eE. Together they say that a label contained in an effect context occurs there syntactically. The three instances above satisfy both conditions.

The modalities of Met[T] are μ,ν::=[E]D,[E](F)=E,D(F)=D,F. Composition is read from left to right: μ[E]=[E],[E]D=[D,E],D1D2=D2,D1. Thus (μν)(E)=ν(μ(E)). The identity is id=.

Kinds distinguish ambient-independent types from arbitrary types: K::=AbsAnyEff, The only proper subkinding step places Abs below Any. Every operation parameter and result has kind Abs. Types, terms, contexts, and the principal judgment are A,B::=1αABαK.AμA,M,N::=x()λxA.MMNΛαK.VMAmodμVletνmodμx=VinMdoMlocal:ABinMhandleμMwithH,V,W::=()xλxA.MΛαK.VmodμVVAletνmodμx=VinW,Γ::=Γ,α:KΓ,x:μFAΓ,lock(μF)Γ,:AB,ΓM:A@E. Write ΓAMetB for structural type equivalence and use the same role on modalities; its only non-syntactic premises are the selected effect structure’s T equations. The complete kinding, equivalence, and context-formation rules are in subappendix A.30. The grammar of V contains complex values: type application and modal elimination may reduce internally while remaining admissible beneath a value-restricted construct. Only this class may appear in modal introduction, modal elimination, and type abstraction. Fix a global operation signature Σ. A runtime label context Ω::=Ω,:AB records the fresh labels generated by local declarations. The full target judgment is ΩΓM:A@E; displays omit the left component when it does not change. The subscript F records the effect context on which a modality acts. Ordinary bindings abbreviate x:idFA. The composite locks(Γ) multiplies the locks to the right of a variable. A variable of pure kind crosses every lock; another variable may cross only when its binding modality transforms to that composite.

The modality-transformation judgment Γμν@F has the two characteristic clauses Eeμ(F)[E]μ@FD1,GeD2,Gfor every FeGD1D2@F. The universal quantifier in the second premise prevents an extension coercion from becoming invalid after ambient subeffecting. Subappendix A.30 prints the selected kinding, variable, modal, operation, handler, and reduction rules.

Modal introduction and elimination have the shapes Γ,lock(μF)V:A@μ(F)ΓmodμV:μA@F and Γ,lock(νF)V:μA@ν(F)Γ,x:(νμ)FAM:B@FΓletνmodμx=VinM:B@F. Both constructs are value restricted. In particular, mod[](do()) is not a suspension: if arbitrary computations were admitted, it could hide an unhandled request under an empty ambient context.

For an operation :AB, a handler clause set is H={return xN,prN}. The parameter μ determines the modal type r:μ(BB). Its rule requires μid@F and μμμ@F, permitting zero or multiple handler uses. These are hypotheses of the rule, not equations for every modality.

Reduction is call by value. The value normal forms are U::=()xλxA.MΛαK.VmodμU. Figure 2, §3.6, p. 17 of Tang and Lindley [TL26] omits () from this grammar even though unit has no reduction; the displayed grammar makes that necessary case explicit. A local declaration generates a fresh runtime label. Modal beta reduction removes matching introduction and elimination forms. Handler return wraps its result with μ; operation handling wraps the reified continuation with μ. If K contains no handler for , handleμK[doU]withHN[U/p,modμ(λy.handleμK[y]withH)/r]. The side condition makes dispatch choose the dynamically nearest matching handler.

Definition 33.1 — Normal form at an effect context

A closed term is normal at E when it is a value normal form U or has the form K[doU] with eE.

Theorem 33.2 — Met safety

Let T satisfy the two validity conditions. If ΩM:A@E, then some configuration (N;Ω) satisfies (M;Ω)(N;Ω), or M is normal at E. If ΩΓM:A@E and (M;Ω)(N;Ω), then ΩΓN:A@E.

Proof of Theorem 33.2 — Met safety

Source import. This is Tang and Lindley’s Theorems 3.7–3.8, §3.7, p. 18 [TL26]. Their canonical-forms, weakening, substitution, and subeffecting lemmas close the two inductions. The operation case of progress uses validity to recover an occurrence of the stuck label in E; the handler-operation case of preservation uses the typed reified continuation. Subappendix D.34 records this imported boundary. ◻

At E=, validity excludes the request form, so a closed well-typed term either steps or is a value. This is effect safety, not termination.

Exercise 33.2 — Why validity matters

★★☆ Suppose an alleged effect structure validates e. Show which conclusion of theorem 33.2 no longer yields empty-effect safety. Do not claim that subject reduction fails.

Exercise 33.3 — A modal resumption

★★☆ For μ=[E], compute μ. State the types assigned to the return value and resumption in the handler rule, and identify the two comonadic premises required at ambient context F.

System Fε: the row source

System Fε is a fine-grain call-by-value calculus with scoped rows. Its card is independent of chapter 25’s inference calculus: A,B::=1αAEBαK.A,E::=ϵ,E,V::=()xλExA.MΛαK.Vhandler H,M::=return VVWVAdoVlet x=MinN,H::={prN}. Rows retain duplicate labels and are equal modulo permutation. The judgments are ΓvV:A and ΓcM:A!E. In particular, Σ()=ABΓvV:AΓcdoV:B!(,E) and a one-operation handler has type handler H:((1,EA)EA). It has no source return clause. The full selected rules appear in subappendix A.30.

Define r into Met[Rsc] by translating rows homomorphically and setting AEBr=[Er](ArBr),λExA.Mr=mod[Er](λxAr.Mr),(V:AEB)Wr=letmod[Er]f=VrinfWr,doVr=doVr. Return, let, and type abstraction/application translate homomorphically. The handler translation supplies the missing source return clause and uses a modality-parameterized target handler. For source result type A, handler{prN}r=mod[E](λf.handle[E](letmod[,E]f=finf())with{return xletmod[,E]x=xinx,prNr}). The annotations E and ,E are part of this type-directed translation.

Theorem 33.3 — Row-to-Met preservation

If ΓcM:A!E, then ΓrMr:Ar@Er. If ΓvV:A, then ΓrVr:Ar. If M is well typed and MN, then MrNr.

Proof of Theorem 33.3 — Row-to-Met preservation

Source import. These are Tang and Lindley’s Theorems 4.1–4.2, §4.2, p. 20 [TL26]. The typing induction uses the absolute-modality translation of arrows. The operational induction is on one source step; beta steps take target modal-beta steps, and the handler step uses the translated return and operation clauses. The conclusion is multi-step preservation, not a one-step lockstep simulation. ◻

Exercise 33.4 — Translate a row function

★★☆ Assume Σ()=AB. Translate λ,ExA.dox and its application to a value V. Give the translated function type and every modal elimination form. Then explain why replacing the absolute modality by changes the ambient-effect contract.

System C: the capability source

System C separates values, second-class blocks, and computations: A::=1T@C,T::=(A¯;f¯:T¯)B,C::={f¯},V::=x()box P,P::=f{(x¯:A¯;f¯:T¯)M}unbox V,M::=return VP(V¯;Q¯)let x=MinNdef f=PinNtry{fABM}with{p,rN}. The judgments ΓP:TC and ΓM:AC track capability sets. A tracked binding f:T contributes {f}; a transparent binding f:CT contributes the known set C. Blocks are not values unless explicitly boxed. This distinction is a premise of System C safety; the modal target does not recreate it by notation.

The translation into Met[S] assigns an effect variable f^ to every tracked capability: {f1,,fn}c=f^1,,f^n,T@Cc=[Cc]Tc,(A¯;f¯:T¯)Bc=f^¯.f^¯(A¯c[f¯^]T¯cBc). A box uses [Cc]; a block abstraction quantifies its effect variables and uses f^¯. To keep the two target binders visibly distinct, write f^ for the effect variable associated with a tracked source block and f~ for the term obtained by eliminating that block’s box. The type-directed context translation is c=,Γ,x:Ac=Γc,x:Ac,Γ,f:Tc=Γc,f^:Eff,f:[f^]Tc,f~:[f^]Tc,Γ,f:CTc=Γc,f:[Cc]Tc,f~:[Cc]Tc. The ordinary value clauses are homomorphic. Boxing and the three block clauses are box P:T@Cc=mod[Cc]Pc,fc=f~. {(x¯:A¯;f¯:T¯)M}c=Λf^¯.modf^¯(λx¯A¯c.λf¯[f¯^]T¯c.letmod[f¯^]f¯~=f¯inMc). unbox V:TCc=letmod[Cc]x=Vcinx. Barred modal lets abbreviate one elimination per block parameter, from left to right. An unmarked elimination uses the identity elimination modality. The remaining non-handler computation clauses make the same discipline explicit: return Vc=Vc,let x=MinNc=let x=McinNc,def f=P:TCinNc=let f=mod[Cc]Pcinletmod[Cc]f~=finNc,P(V¯;Q¯j:TjCj)c=letmodCjc¯x=PcCjc¯inxVc¯(mod[Cjc]Qjc)¯. Thus a call first instantiates one quantified effect variable with each actual block’s capability set, then eliminates the resulting extension box.

For the handler, let Hf,C={p,rN}. Its translation is the following binding-sensitive schema: try{fABM}with Hf,Cc=localf:AcBcinletmodfg=(Λf^.modf^(λf.letmod[f^]f~=finMc))finhandle[Cc](g(mod[f](modid(λxAc.dofx))))withHf,Cc,Hf,Cc={return xletmod[f,Cc]x=xinx,fprletmod[Cc]r~=rinNc}. The label f is fresh. The translation instantiates the handled block’s f^ with f, passes a boxed operation implementation, and exposes the resumption under [Cc]. Erasing either binder or the local label changes target scope.

Theorem 33.4 — Capability-to-Met preservation

If ΓM:AC in System C, then ΓcMc:Ac@Cc. If ΓvV:A, then ΓcVc:Ac; if ΓbP:TC, then ΓcPc:Tc@Cc. If a well-typed runtime configuration reduces from (M;Ω) to (N;Ω), then its translation reduces in zero or more Met steps from (Mc;Ωc) to (Nc;Ωc).

Proof of Theorem 33.4 — Capability-to-Met preservation

Source import. These are Tang and Lindley’s Theorems 5.1–5.2, §5.2, p. 23 [TL26]. Type preservation is simultaneous over value, block, and computation derivations. The block-call case instantiates one effect variable for each actual block argument; the handler case introduces the fresh local operation and types the translated resumption. Semantics preservation is a multi-step simulation on typed configurations because local-label generation changes the runtime instance context. ◻

Exercise 33.5 — Translate a capability block

★★☆ Let f:(11) and translate {(x:1;f:11)f(x)}. Show the quantified effect variable, extension modality, absolute box on the block argument, and elimination that binds its hatted target variable.

Exercise 33.6 — The handler hypothesis

★★★ In the System-C handler translation, identify where freshness of f is used in typing and where it is used in operational preservation. Explain why choosing an active label invalidates the argument even when it has the same operation signature.

What the common target does and does not prove

The encodings expose a common shape: row annotationAEB[E](AB),capability parameter(A¯;f¯:T¯)Bf^¯.f^¯(). They do not establish FεsrcC. The domains have different syntax, typing judgments, runtime configurations, and abstraction boundaries. A shared codomain supplies a comparison language; it does not supply a map between the domains.

The 2025 Met paper develops modal effect types and its METL surface language [TWD^+25]. The retained METL artifact implements a bidirectional type checker, interpreter, and paper examples. It does not implement either source-to-Met encoding and is not executable evidence for theorem 33.3, theorem 33.4. The earlier contextual-modal calculus is a separate design, not an earlier name for Met [ZN21].

Capability boxes refine the relation between scope-based capabilities and type-visible boxes, with safety depending on System C’s boxing and second-class discipline [BSLBG22]. Locality and effect reflection ask which effects are admitted, reflected, or confined at a boundary [Whi26]; this chapter uses that work only as a comparison card. Modal effect types here are also distinct from dependent multimodal calculi: their contexts carry no dependent modes or mode-morphism substitutions.

Exercise 33.7 — One handler through both encodings

★★★ Fix Σ(ask)=11. In System Fε, handle the thunk λasku1.doasku with the clause askprreturnp. In System C, express the same single request and clause as try{f11f(())}with{p,rreturnp}. Reconstruct the modal skeleton of each translation. Mark the absolute box that records the row in the first, and the quantified effect variable, extension box, boxed operation implementation, and fresh local label in the second. Finally identify one hypothesis used only by the corresponding preservation proof in each case.

Exercise 33.8 — No triangle from a span

★★☆ Draw the two proved translation arrows and list one syntactic and one dynamic difference between their domains. State the additional data needed to derive a behavior-preserving translation from one source to the other.

Sources.

The target calculus, two source cards, and preservation pairs follow Tang and Lindley [TL26]. The modal design and METL artifact boundary follow Tang et al. [TWD^+25]. The comparison uses System C boxes [BSLBG22], the contextual-modal predecessor [ZN21], and locality/effect reflection [Whi26].

Suggested first pass.

Exercise 33.9, Exercise 33.10, Exercise 33.12 form the suggested first pass. No exercise in this optional seminar is a prerequisite for a core chapter.

Exercise 33.9

★★☆ Derive modal introduction followed by elimination for mod[E](λx.x) at ambient F. Print the lock, tagged binding produced by elimination, and variable-access premise. Then replace the function type by a pure base type and locate the branch of the variable rule that no longer needs a modality transformation.

Exercise 33.10

★★★ Prove the application and one-operation-handler cases of theorem 33.3. For the handler case, calculate the modal types of the handled thunk, returned value, and resumption. Trace the source contraction and its nonempty target reduction sequence.

Exercise 33.11

★★★ Prove the block-abstraction and block-call cases of theorem 33.4. State the simultaneous induction hypotheses and show how substituting actual capability sets for quantified effect variables produces the translated result type.

Exercise 33.12

★★☆ For each claim, name the theorem that proves it or give a counterexample:

  1. a closed empty-effect Met term cannot stop at an operation request;

  2. row-to-Met preservation yields a row-to-capability compiler;

  3. passing the METL example suite proves the System-C translation;

  4. capability-box safety implies ownership noninterference.

Exercise 33.13

★★★ Practical project.modal-translation-model Implement the finite model in appendix E. Represent absolute and extension modalities, one row arrow, one capability block, and their two translations as data. Preserve duplicate row labels, instantiate capability parameters by explicit effect variables, and reject modal elimination at the wrong ambient context.

The accepted run has eight named cases. Three one-site, typechecking mutations—turning absolute replacement into extension, contracting duplicate rows, and omitting capability-effect instantiation—must each make the frozen oracle fail. The artifact is a finite observation model, not a proof of either preservation theorem.

Search the book

Type to search the local edition.