Lectures onType Theory
Chapter 53
Chapter 53Optional

Coeffects and Context-Dependent Computation

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

The terms e1=3,e2=?width+3 can have the same empty effect trace. They do not have the same contextual requirement: e2 needs a binding for the implicit parameter ?width. An effect bounds what evaluation does to an environment. A coeffect bounds what evaluation requires from that environment.

Coeffect scalars and the flat judgment

A coeffect scalar structure is a tuple (C,,c,use,ign,) such that (C,,use) and (C,c,ign) are monoids, (C,) is a preorder, and sequential composition distributes over sharing on both sides: (rcs)t=(rt)c(st),t(rcs)=(tr)c(ts). The element use is the requirement of one variable occurrence; ign is the requirement of an unused binding. The two monoids need not be arithmetic multiplication and addition.

A flat coeffect annotates an entire variable context by one scalar. Choose an operation c:C×CC satisfying (F)rcsrcs. The flat judgment is Γ@fre:τ. Function types carry a latent scalar σtcτ. The four rules used below are

@fignc:ι
F-Const
x:τ@fusex:τ
F-Var
Γ,x:σ@f(rcs)e:τ
Γ@frλx.e:σscτ
F-Abs
Γ1@fre1:σtcτΓ2@fse2:σ
Γ1,Γ2@f(rc(ts))e1e2:τ
F-App

Weakening, exchange, contraction, and subcoeffecting are admissible when the selected flat algebra validates their corresponding context transformations. Condition F is the exact inequality used by beta preservation.

For implicit parameters take C=P(Name×Type),=c=c=,use=ign=,≤=⊆. The primitive access judgment is Γ@f{(?p,τ)}?p:τ. Thus the opening terms have requirements and {(?width,int)}, although both are effect free.

Exercise 53.1

★☆☆ Derive the requirement of ?width+(?height+3) in the implicit-parameter instance. Mark the two uses of set union and verify condition F.

The flat call-by-value card

The first theorem card uses the flat rules just printed and call-by-value beta reduction. A syntactic value v is a pure value when it has a derivation Γ@fusev:σ. In the flat shape there is one scalar regardless of the number of variables in Γ.

Lemma 53.1 — Flat call-by-value substitution

For every flat scalar structure satisfying F, if Γ@fusees:σandΓ1,x:σ,Γ2@frer:τ, then Γ1,Γ,Γ2@frer[es/x]:τ.

Proof of Lemma 53.1 — Flat call-by-value substitution

Proof. Induct on the derivation of the receiving term er. In the variable case er=x, the premise annotation is use, so the substituted derivation is the first hypothesis. A different variable retains its own F-Var derivation and weakens across Γ. In an abstraction, choose its binder outside fv(es)dom(Γ1,Γ,Γ2); the induction hypothesis checks the renamed body and F-Abs restores the same immediate and latent scalars. In an application, apply the induction hypothesis to both premises in which x occurs. Distributivity of over c reconstructs the annotation in F-App. The context-transformation cases use the corresponding flat algebra law. These cases exhaust the derivation. ◻

Theorem 53.2 — Flat call-by-value subject reduction

For the flat calculus satisfying F, if Γ@fre:τ and ee, where beta contracts only a pure value argument, then Γ@fre:τ.

Proof of Theorem 53.2 — Flat call-by-value subject reduction

Proof. The congruence cases rebuild their typing rules. For beta, invert F-App and F-Abs. The value premise has scalar use, so lemma 53.1 types the reduct. The two static context combinations differ by replacing rcs by rcs; condition F and subcoeffecting give the required annotation. This is Theorem 11 of the selected calculus [POM14]. ◻

If beta contracts an arbitrary context-dependent argument, the substitution lemma’s pure-value premise is false. The call-by-value theorem therefore does not justify call-by-name evaluation.

The flat call-by-name card

The second card keeps the flat typing rules but changes the reduction relation. A flat algebra is top-pointed when suse for every sC. It is bottom-pointed when uses for every sC.

Lemma 53.3 — Top-pointed substitution

In a top-pointed flat calculus, if Γ@fses:σ and Γ1,x:σ,Γ2@frer:τ, then Γ1,Γ,Γ2@frer[es/x]:τ.

Proof of Lemma 53.3 — Top-pointed substitution

Proof. Top-pointedness gives suse. Subcoeffecting retypes es at use, so lemma 53.1 applies. ◻

Lemma 53.4 — Bottom-pointed substitution

Assume the flat algebra is bottom-pointed and c==c, with this common operation commutative and idempotent. If Γ@fses:σ and Γ1,x:σ,Γ2@frer:τ, then Γ1,Γ,Γ2@f(rs)er[es/x]:τ.

Proof of Lemma 53.4 — Bottom-pointed substitution

Proof. Induct on the receiving derivation. The x-case uses uses and subcoeffecting. Application combines each copy of the substituted requirement with the surrounding requirement. Commutativity reassociates s with the free-variable context; idempotence merges repeated copies introduced by contraction. Abstraction preserves that combined requirement because c==c. ◻

Theorem 53.5 — Flat call-by-name subject reduction

If a flat calculus satisfies either the hypotheses of lemma 53.3 or those of lemma 53.4, then Γ@fre:τ,eeΓ@fre:τ.

Proof of Theorem 53.5 — Flat call-by-name subject reduction

Proof. Congruence preserves the derivation. In the beta case, inversion of F-App and F-Abs produces the premises of the appropriate substitution lemma. The top-pointed lemma retains r. In the bottom-pointed case, the equality of the three operations and idempotence reduce the substituted annotation to the one in the application conclusion. This is the two-case hypothesis of source Theorem 14 [POM14], not a result for an arbitrary flat algebra. ◻

The implicit-parameter instance is bottom-pointed under inclusion because use=, and its three operations are set union. Hence the call-by-name theorem applies. Flat liveness instead uses a greatest use and belongs to the top-pointed case.

Exercise 53.2

★★☆ Give a three-element preorder in which use is neither greatest nor least. Explain why neither call-by-name substitution lemma applies; do not infer that subject reduction fails without constructing a calculus and a counterexample.

The structural card

A structural coeffect assigns one scalar to each variable. A context x1:τ1,,xn:τn carries a vector R=r1,,rnCn. The empty vector is , vector concatenation is R++S, and scalar multiplication is pointwise: rs1,,sn=rs1,,rsn. The judgment is Γ@sRe:τ. Its abstraction and application rules are

Γ,x:σ@s(R++s)e:τ
Γ@sRλx.e:σscτ
S-Abs
Γ1@sRe1:σtcτΓ2@sSe2:σ
Γ1,Γ2@s(R++(tS))e1e2:τ
S-App

Variables carry use. Weakening appends ign. Exchange swaps a variable and its scalar. Contraction replaces adjacent scalars r,s by rcs. Write Γ@sRctx,θΓ@sR for the context-transformation package generated by these four operations. The package is locally sound when every generator transforms a typing derivation as stated, and locally complete when every weakening, adjacent permutation, adjacent identification, or pointwise increase used in a derivation factors through those generators. The structural card assumes both properties, together with the two scalar monoids and both distributivity equations from section 53.1. This states exactly what the contextual rule may do; it is not an unrestricted structural congruence.

Lemma 53.6 — Structural substitution

If Γ@sSes:σ,Γ1,x:σ,Γ2@s(R1++r++R2)er:τ, then Γ1,Γ,Γ2@s(R1++(rS)++R2)er[es/x]:τ.

Proof of Lemma 53.6 — Structural substitution

Proof. Induct on the receiving derivation. The variable case x has r=use, and the sequential monoid identity gives useS=S. Abstraction extends both the context and vector at the same end; alpha-renaming chooses its binder outside fv(es)dom(Γ1,Γ,Γ2). Application substitutes into each premise and uses distributivity to combine multiple occurrences. Weakening, exchange, and contraction transform the variable and its scalar together; the stated local soundness/completeness conditions reconstruct those rules. No flat operation c occurs. ◻

Theorem 53.7 — Structural subject reduction

For the structural calculus with the scalar laws and locally sound and complete context operations just stated, Γ@sRe:τ,eβeΓ@sRe:τ.

Proof of Theorem 53.7 — Structural subject reduction

Proof. The beta case inverts S-App and S-Abs; the bound variable occupies one unique vector position. Applying lemma 53.6 replaces its scalar r by rS, exactly the vector printed by S-App. Congruence cases rebuild their derivations. Context conversions before or after the redex are transported by local soundness and completeness of weakening, exchange, and contraction. This is source Theorem 8 at the structural signature [POM14]. ◻

For bounded reuse choose (N,×,+,1,0,). The term λx.x+x assigns latent grade 2 to x, because the two occurrences contract 1+1. Applying it to y, whose vector is 1, produces 2×1=2. This is a contextual demand on y, not a claim that evaluation performs two effects.

Exercise 53.3

★☆☆ Derive the vector for (λx.x+x)(y+y). Show separately the contraction that gives the latent 2 and the multiplication that scales the argument vector.

Set models and a dataflow boundary

The implicit-parameter model can be constructed without categorical machinery. Regard every object A as a set. A requirement set is well formed when it assigns at most one type to each name. A finite typed dictionary ρ realizes R, written ρdR, when ρ(?p)τ for every (?p,τ)R. Let DR(A)={(ρ,a)ρ is a finite typed dictionary, ρdR, aA}. For RS, let ρ|R retain exactly the dictionary entries named in R, and define the requirement-forgetting map resRS:DS(A)DR(A),resRS(ρ,a)=(ρ|R,a). If (ρ,(a,b))DRS(A×B), then (ρ,(a,b))((ρ|R,a),(ρ|S,b))DR(A)×DS(B). The two restricted dictionaries are available because ρ realizes every entry in RS. For an access (?p), the map sends (ρ,) to ρ(?p); its domain condition is exactly (?p,τ)R.

The bounded-reuse model is also concrete. For R=r1,,rn, set BR(A1,,An)=A1r1××Anrn. The variable rule selects the sole component of A1. Contraction uses the canonical regrouping Ar×AsAr+s. Substitution uses (As)rAr×s, which is the semantic counterpart of the grade rs in lemma 53.6.

Causal dataflow needs a different operational account. Let a stream environment ρ map each x to values ρ(x)0,ρ(x)1,. The dedicated lookup rule is prevkx,ρ,ndfρ(x)nk(kn). A structural vector R is sound for this lookup fragment when every occurrence prevkxi has kRi. Ordinary syntactic substitution is unsuitable as the operational definition: replacing x by an arbitrary stream expression can duplicate or shift the history demanded by that expression, whereas the machine rule reads a named stream at an explicit time index. The chapter therefore claims only the displayed lookup bound and its immediate closure under arithmetic contexts. It does not claim an independent progress, preservation, or cache-optimality theorem for a complete dataflow language.

The non-transfer boundary can now be read without identifying the cards:

card local hypothesis and non-transfer boundary
flat value One scalar and an argument at use and F do not give arbitrary call-by-name preservation.
flat name Top-pointedness, or bottom-pointedness with the three operations equal, commutative, and idempotent, does not give structural substitution.
structural One scalar per variable and locally sound and complete structural operations do not give either pointed flat theorem.
dataflow lookup A history bound for each stream and a named lookup with kn do not give whole-language subject reduction.

The dictionary and finite-power constructions prove only the displayed local Set calculations. They do not yet supply the indexed comonad, its counit and comultiplication, or the coherence equations needed for a categorical model of all three cards. That construction must wait until those categorical notions have been developed.

Sources.

The earlier unified flat calculus introduces implicit parameters, liveness, and dataflow under one whole-context annotation and supplies the indexed comonadic account [POM13]. It is provenance for the flat motivation, not for the later structural theorem. The archived Petricek–Orchard–Mycroft PDF pages 4–6 print Definitions 1–5 and the structural and flat instances; pages 7–8 print structural substitution, Theorems 8, 11, and 14, and their exact call-by-value, top-pointed, and bottom-pointed hypotheses [POM14]. The longer thesis supplies worked implicit-parameter and dataflow derivations, not a license to move results between the cards [Pet17].

Suggested first pass.

Do exercise 53.4, exercise 53.5 before the implementation problem.

Exercise 53.4

★★☆ For one beta redex, write the substitution premise and conclusion separately for flat call-by-value, top-pointed flat call-by-name, bottom-pointed flat call-by-name, and structural call-by-name. Circle the hypothesis that differs in each row.

Exercise 53.5

★★☆ For R={(?w,int)} and S={(?h,int)}, construct an element d of DRS(int×int) and compute its split image in DR(int)×DS(int). Separately compute resR(resRRS(d)) and the path through S. That second path is resS(resSRS(d)). Prove that both are the same element of D(int×int) by proving the needed restriction equation.

Exercise 53.6

★★☆ For e=prev2x+(prev1y+prev3x), compute the least cache vector. Evaluate e at time n=3 in a symbolic stream environment and list the three entries read.

Exercise 53.7

★★★ Practical project.coeffect-demand-checker Implement implicit-parameter union and structural bounded-reuse inference in Kappa. Maintain the invariant that flat requirements contain every accessed name and that each structural counter equals the syntax-directed occurrence count. The accepted run must report the opening requirements, count (x+x)+(x+y) as x3,y1, and validate the substitution calculation 2×2=4. Mutate contraction from addition to maximum: the mutant must typecheck and audit cleanly but fail the repeated-use oracle. Use artifacts/ch53-coeffect-context/corpus.kp; the accepted run must end with All 5 coeffect corpus cases passed. The program illustrates the two Set calculations and structural substitution; it does not prove any subject-reduction theorem.

Search the book

Type to search the local edition.