Lectures onType Theory
Chapter 54
Chapter 54Optional

Two Bases for Graded Types: Calculi and Correspondence

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

Suppose f uses its argument with grade r. In the body f(fx), the outer occurrence of f contributes 1, the inner occurrence lies in an argument scaled by r, and x lies under both applications. Their grades are therefore 1+r and rr. There are two places to record those facts. A judgment may grade the assumptions for f and x, or a type may place a graded box around the argument of f. The annotations look interchangeable. They are not: one presentation has graded functions, while the other has linear functions and a graded modality. This chapter fixes the two nondependent calculi of Liepelt, Marshall, and Orchard and proves only the translations they establish [LMO26].

One algebra, two bases

Fix a preordered semiring R,0,1,+,,q. Addition and multiplication are monotone, with respective units 0 and 1. A graded assumption is written x:rA, while a linear assumption is written x:A. Context addition and scalar multiplication are pointwise: (Δ,x:rA)+(Δ,x:sA)=Δ+Δ,x:r+sA,q(Δ,x:rA)=qΔ,x:qrA. Missing variables carry grade 0. These equations describe annotations; they do not yet say whether a grade is an exact count, an upper bound, a sensitivity, or a security level. That interpretation is fixed by the chosen semiring and preorder.

Exercise 54.1

★☆☆ In the semiring of natural numbers, calculate the grades 1+r and rr of f and x in f(fx) when the arrow of f carries grade r. Repeat in the Boolean semiring and explain why the second calculation cannot count calls.

Linear Base

The Linear Base puts ordinary functions over a context containing linear and graded assumptions: A,B::=KABrA,t,u::=xλx.ttu[t]let[x]=tinu. Write ΓLt:A. The notation gr(Γ) means that every assumption in Γ is graded, while gr0(Γ) means that each has grade 0. The complete typing rules are

x:ALx:A
L-Var
Γ,x:ALt:B
ΓLλx.t:AB
L-Abs
Γ1Lt:ABΓ2Lu:A
Γ1+Γ2Ltu:B
L-App
ΓLt:Agr0(Γ)
Γ+ΓLt:A
L-Weak
Γ,x:ALt:B
Γ,x:[A]1Lt:B
L-Der
ΓLt:Agr(Γ)
rΓL[t]:rA
L-Prom
Γ1Lt:rAΓ2,x:[A]rLu:B
Γ1+Γ2Llet[x]=tinu:B
L-Let
Γ,x:[A]rLt:Brqs
Γ,x:[A]sLt:B
L-Approx

Context addition is undefined when both operands contain the same linear assumption; only shared graded assumptions are added. Thus there is no contraction rule for a linear assumption. Promotion scales an entire graded context; it cannot close over a linear assumption.

Call-by-name reduction has the two principal steps (λx.t)uLt[u/x],let[x]=[t]inuLu[t/x], and congruence in function and scrutinee position. Its reflexive transitive closure is L.

Example 54.1 — One box, two uses

If 2qr, then c:KKC,z:rKLlet[x]=zincxx:C. The two occurrences of x are combined before L-Approx. This is an upper-bound reading only when the preorder is oriented so that larger annotations admit more use.

Exercise 54.2

★★☆ Explain why L-Prom cannot derive y:AL[y]:rA. Then derive the corresponding judgment from y:[A]1 when r=1.

Graded Base

The Graded Base grades the domain of every function: A,B::=KArB,t,u::=xλx.ttu. Every assumption is graded. The complete judgment ΔGt:A is generated by

x:1AGx:A
G-Var
ΔGt:A
Δ,0ΔGt:A
G-Weak
Δ,x:rAGt:Brqs
Δ,x:sAGt:B
G-Approx
Δ,x:rAGt:B
ΔGλx.t:ArB
G-Abs
Δ1Gt:ArBΔ2Gu:A
Δ1+rΔ2Gtu:B
G-App

The principal reduction is (λx.t)uGt[u/x]; congruence is in function position. Thus both bases use call by name.

Lemma 54.2 — Graded substitution

If Δ,x:rAGt:B and ΘGu:A, then Δ+rΘGt[u/x]:B.

Proof of Lemma 54.2 — Graded substitution

Proof. Induct on the first derivation. In the variable case for x, the context has grade 1, so the conclusion is the second premise. Another variable uses G-Weak. The abstraction case applies the induction hypothesis under the binder. In the application case, write the function’s arrow grade as q, and write the grades of x in the function and argument premises as a and b. The conclusion before substitution has context Δ1+qΔ2,x:a+qbA. The two induction hypotheses produce Δ1+aΘ and Δ2+bΘ; rule G-App combines them as (Δ1+aΘ)+q(Δ2+bΘ)=Δ1+qΔ2+(a+qb)Θ, by distributivity and associativity. Weakening and approximation commute with substitution by monotonicity of addition and multiplication. ◻

For h=λf.λx.f(fx), the outer use of f has grade 1, its inner use is scaled by r, and the nested argument grades multiply: Gh:(ArA)1+rArrA. This single derivation separates addition for independent occurrences from multiplication for nested demand. The printed Examples 2.13–2.14 of the source show (1+1) for the grade of (f). Rule G-App, however, scales the inner occurrence by the arbitrary arrow grade (r), giving (1+r); the chapter uses that rule-derived correction throughout [LMO26].

The algebra decides what that calculation means. The following four instantiations use the same syntax but not the same theorem:

Reading Algebra and order Calculation for h
exact use N, ordinary + and , equality order At r=2, f has grade 3 and x grade 4; neither may be enlarged.
upper bound N, ordinary operations, numeric The least derived pair at r=2 is (3,4); G-Approx also admits (5,7).
sensitivity R0, ordinary operations, numeric If the argument position is L-sensitive, the two nested paths give 1+L for f and L2 for x.
security {Lo,Hi}, +=, =, 0=Hi, 1=Lo, approximation ordered HiqLo At r=Hi, the grades are LoHi=Lo and HiHi=Hi.

The exact-use and security rows are instances exhibited by the source; the upper-bound and sensitivity rows are direct semiring calculations. The security derivation does not by itself prove noninterference, and the sensitivity row does not by itself define a metric semantics. The bounded-use and Level-security examples in Granule illustrate the same requirement to fix the algebra before reading an annotation; they do not supply a theorem for the two bases [OLI19].

Exercise 54.3

★★☆ Use lemma 54.2 to prove preservation for the principal beta step. State why the result is preservation of the annotated judgment, not a proof that the annotation is an exact dynamic count.

From Graded Base to Linear Base

The direct translation inserts a box at every graded domain: L[[K]]=K,L[[ArB]]=rL[[A]]L[[B]],L[[x:rA]]=x:[L[[A]]]r,L[[x]]=x,L[[λx.t]]=λy.let[x]=yinL[[t]],L[[tu]]=L[[t]][L[[u]]], where y is fresh. Context translation is pointwise and consequently commutes with addition and scaling.

Theorem 54.3 — Graded-to-linear correspondence

For the calculi and call-by-name relations fixed above:

  1. if ΔGt:A, then L[[Δ]]LL[[t]]:L[[A]];

  2. if tGt, then L[[t]]LL[[t]];

  3. if t=Gt, then L[[t]]=LL[[t]].

Proof of Theorem 54.3 — Graded-to-linear correspondence

Proof. For item 1, induction on typing. The only new cases are abstraction and application. Abstraction translates its graded assumption into x:[L[[A]]]r, eliminates it with L-Let, and then applies L-Abs. Application promotes the translated argument with L-Prom and combines contexts with L-App. The identity L[[rΔ]]=rL[[Δ]] supplies the required context.

For item 2, the principal calculation is L[[(λx.t)u]]=(λy.let[x]=yinL[[t]])[L[[u]]]Llet[x]=[L[[u]]]inL[[t]]LL[[t]][L[[u]]/x]=L[[t[u/x]]]. The congruence case follows from function-position congruence. Item 3 is an induction over the source equational derivation; the beta case is the displayed calculation, and congruence and equivalence rules are homomorphic. These are the three clauses of Theorem 1 of the source; its Appendix B.1 gives the standard weakening and substitution details [LMO26]. ◻

Corollary 54.4 — Grade preservation

The translation changes the base of an assumption but not its grade: L[[rΔ]]=rL[[Δ]],L[[Δ1+Δ2]]=L[[Δ1]]+L[[Δ2]].

Proof of Corollary 54.4 — Grade preservation

Proof. Both equations follow pointwise from L[[x:qA]]=x:[L[[A]]]q. ◻

The converse is false as a direct translation. A term of type A1B does not promise exactly one use: in a semiring where 0=1, the grade carries no such information. Mapping it to AB would manufacture linearity. The source therefore uses continuation passing.

Exercise 54.4

★★☆ Take the one-element semiring. Derive a constant function at an arrow marked 1, and explain why translating that arrow directly to a linear arrow would invalidate the intended assumption discipline.

From Linear Base to Graded Base by continuations

Fix an answer type K. The call-by-name CPS translation is G[[K]]=(K1K)1K,G[[AB]]=((G[[A]]1G[[B]])1K)1K,G[[rA]]=(G[[A]]rK)1K,G[[x]]=λk.xk,G[[λx.t]]=λk.k(λx.G[[t]]),G[[tu]]=λk.G[[t]](λf.f(G[[u]])k),G[[[t]]]=λk.k(G[[t]]),G[[let[x]=tinu]]=λk.G[[t]](λx.G[[u]]k). A linear assumption maps to grade 1; x:[A]r maps to grade r. The continuation is essential: it supplies the multiplication needed to simulate promotion and unboxing.

Theorem 54.5 — Linear-to-graded CPS correspondence

For the source call-by-name relation and full beta reduction in the target:

  1. if ΓLt:A, then G[[Γ]]GG[[t]]:G[[A]];

  2. if tLt, then G[[t]]GG[[t]];

  3. the translation preserves source function beta and modal beta–eta equations, but the result does not claim preservation of function eta.

Proof of Theorem 54.5 — Linear-to-graded CPS correspondence

Proof. Typing is by induction on the Linear Base derivation. Linear variables use grade 1; L-Prom becomes multiplication of the translated context by r; and L-Let becomes graded substitution. The beta simulations are finite full-beta calculations through the displayed continuations. The modal beta calculation substitutes the value passed to the continuation; modal eta cancels the continuation introduced for a box. Function eta is excluded because the CPS type is not definitionally an unboxed function type. This is exactly Theorem 2 and its stated equation boundary in the source; Appendix B.2 verifies every remaining rule case [LMO26]. ◻

Neither theorem asserts inverse translations, full abstraction, normalization, or a common denotational semantics. Products make the limit concrete: a negative-position product in the continuation translation is not the direct image of a Graded Base product. Similar-looking grades therefore support a correspondence, not an identification of calculi.

Exercise 54.5

★★☆ Calculate G[[2(AA)]]. Mark the arrow carrying grade 2, and explain why erasing the two continuation layers would not be a type-preserving simplification.

A separate effect–coeffect calculus

Gaboardi, Katsumata, Orchard, Breuvart, and Uustalu combine effects and coeffects in a third calculus [GyKO^+16]. It is not a corollary of either translation above. Fix an effect preordered monoid E,1,, a coeffect preordered semiring R, a distributive-law format ϕ, and the source’s matched-pair operations connecting the two grades. The type grammar is A,B::=oABTeADrA. The type TeA classifies a computation with effect grade e; the type DrA classifies data made available with coeffect grade r. A judgment has linear assumptions x:A and discharged assumptions x:[A]r. Context sum is defined only when repeated variables are discharged, in which case their grades add. Scalar multiplication r[Γ] is defined only for a fully discharged context. Here is the complete typing card:

x:Ax:A
EC-Ax
Γt:AΓ<:ecΓA<:ecB
Γ,[Δ]0t:B
EC-Sub
Γ,x:At:B
Γλx.t:AB
EC-Abs
Γt:ABΔu:A
Γ+Δtu:B
EC-App
Γt:A
Γt:T1A
EC-Unit
Γt1:TeAΔ,x:At2:TfB
Γ+Δletx=t1int2:TefB
EC-LetT
Γ,x:At:B
Γ,x:[A]1t:B
EC-Der
[Γ]t:B
r[Γ][t]:DrB
EC-Pr
Γt1:DrAΔ,x:[A]rt2:B
Γ+Δlet[x]=t1int2:B
EC-LetD
distr,eϕ:Fr,eϕAGr,eϕA
EC-Dist
op:Aop
EC-Op

Subtyping is covariant in effect grades and contravariant in coeffect grades: A<:ecAefTeA<:ecTfAA<:ecAsrDrA<:ecDsAA<:ecAsr[A]r<:ec[A]s. Arrow subtyping and pointwise context subtyping complete the relation. The eight choices of Fr,eϕ and Gr,eϕ are the source’s four left/right information-flow formats crossed with the choices T-over-D and D-over-T; choosing one is part of fixing the calculus, not a derived equivalence.

One completely typed program exposes both coordinates. Assume d:D2A, p:ATeB, and c:ABC. Then let[x]=dinlety=pxincxy:TeC. The first occurrence of x types px:TeB; the second types cxy:C. Each premise derives x:[A]1, so EC-LetT adds them to x:[A]2, exactly the discharged assumption required by EC-LetD. The effect coordinate is instead e1=e. Thus the coeffect addition 1+1=2 accounts for two contextual uses while effect multiplication composes the producer with a pure continuation. Neither coordinate is a Koka effect row or a QTT usage annotation.

Lemma 54.6 — Two substitution principles

The combined calculus admits exactly the following context transformations: Γ,x:At2:B,Δt1:AΓ+Δt2[t1/x]:B,Γ,x:[A]rt2:B,[Δ]t1:AΓ+r[Δ]t2[t1/x]:B. Here [Δ] means that every assumption of Δ is discharged.

Proof of Lemma 54.6 — Two substitution principles

Proof. These are Lemmas 1 and 2 of the source. Induction on the first derivation proves both. The linear variable case replaces one assumption by Δ. The discharged variable case replaces grade r by rΔ. The modal introduction case is valid only because its premise has a fully discharged context. Every other rule follows from distributivity and monotonicity. ◻

Orienting the calculus’s typed equations gives an operational reduction relation for the fragment without primitive operations. Every oriented equation has the same type and context on both sides. The pure beta rule uses linear substitution; the coeffect beta rule uses coeffectful substitution; effect beta and associativity use the effect-monoid laws; the distributive rules use the fixed matched-pair equations. Hence each principal step preserves its judgment. This is the source’s type-preservation argument for the oriented equations, not a progress theorem. The published semantic theorem is different:

Theorem 54.7 — Equational soundness of combined grading

Let π1 and π2 derive Γt1:A and Γt2:A. If π1=π2 is derivable in the combined equational theory, then their interpretations in every model satisfying the source’s categorical hypotheses are equal.

Proof of Theorem 54.7 — Equational soundness of combined grading

Proof. Induct on the equality derivation. Congruence cases use functoriality. The beta cases use the two substitution lemmas, interpreted respectively by ordinary composition and the graded action. Unit and associativity equations use the monoid and semiring coherence maps; the distributive equation uses the specified interaction between Te and Dr. This is Theorem 1 of Gaboardi et al.; its statement is denotational soundness, not progress [GyKO^+16]. ◻

Exercise 54.6

★★☆ For the displayed program, change the effect grade of p while keeping the coeffect grade of x fixed, and then do the converse. Identify the distinct rule premise affected in each change.

Optional route.

Chapter 47.

Fractional ownership through graded uniqueness

Marshall and Orchard’s fractional-uniqueness calculus assigns reference permissions from a semiring, but adds heaps, borrowing, and uniqueness conditions not present in the two bases above [MO24a]. It permits several read-only fractional references whose incoming permissions sum to 1, while an in-place update requires one reference carrying permission 1.

For the reference occurrences in a term t that target identifier id in heap H, write P(t,H,id)=ρrefs(t)ρpidHp. This abbreviation exposes the heap hypotheses of the source theorems; it is not a Graded Base context grade.

Theorem 54.8 — Fractional-uniqueness safety package

For the source calculus and heap-compatibility relation HhΓ:

  1. If Γt:A, then t is a value or, for every s, Γ0, and H with Hh(Γ0+sΓ), there are H and t such that HtfuHt (Progress, Theorem 6.6).

  2. Under the same quantified heap hypothesis, a step preserves type and produces some Γ with Γt:A and Hh(Γ0+sΓ), provided reference payloads are non-function types (Type Preservation, Theorem 6.7).

  3. For one step as above, P(t,H,id)=1 implies P(t,H,id){0,1}; every identifier fresh from H but present in H has outgoing permission sum 1 (Borrow Safety, Lemma 6.8).

  4. For a multistep reduction to a value, P(t,H,id)=1 implies that the final value contains exactly one permission-1 reference targeting id. Every final reference to an identifier fresh from H is likewise the unique such reference. For a term of the source uniqueness type A, this yields the incoming-and-fresh uniqueness conclusion (Theorem 6.9 and Corollary 6.10).

  5. If Γt1:A, Γt2:A, t1t2, and HhΓ, then both terms multireduce from H to values whose fully beta-reduced, dereferenced forms in their resulting heaps agree (Equational Soundness, Theorem 6.11).

Proof of Theorem 54.8 — Fractional-uniqueness safety package

Proof. The five clauses are imported at their exact source signatures. Progress is by typing induction and heap compatibility. Preservation couples substitution with heap evolution; the non-function payload restriction is used when a stored value is recovered. Borrow safety is an invariant on the sum of permissions targeting each location. Induction over fu gives the multistep clause, from which uniqueness is immediate. Equational soundness then uses preservation plus confluence of the fully dereferenced term theory. The source’s Sections 6.2–6.4 contain the case analysis; none of these statements is transferred to Linear Base or Graded Base. ◻

The pinned Granule 0.9.5.0 evaluator makes the ownership distinction executable. Loading examples.gr accepts exampleBorrow:ColourColour, whose borrow is split into two fractional observations and joined before the unique value is returned. Loading parsum.gr accepts sumFromTo: &f(FloatArrayid)!Int!Int(Float,&f(FloatArrayid)). It then evaluates the parallel shared reads followed by the full-permission reference update to 100.0. Appendix E records the preserved image, exact commands, and hashes. This is implementation evidence for the language design, not a machine-checked proof of theorem 54.8. The chapter’s Kappa supplement checks only a finite grade-correspondence model.

Exercise 54.7

★★★ Construct a permission assignment in which two outgoing references to one identifier each have permission 1. Explain why the assignment is not by itself a counterexample to Borrow Safety—its incoming sum-one premise is absent—and why ordinary graded substitution alone cannot rule it out.

Exercise 54.8

★★★ Research route. Compare the permission-sum invariant with uniqueness attributes from chapter 47. State one implication and one non-implication.

What each result says

System Established result Not licensed here
Graded Base to Linear Base type, operational, and equation preservation inverse, completeness, full abstraction
Linear Base to Graded Base CPS type and operational preservation; stated equations function eta, inverse, normalization
Combined grading two substitutions and categorical equational soundness Koka typing, QTT metatheory, progress theorem
Fractional uniqueness source progress, restricted preservation, borrow safety, uniqueness, equation soundness safety for arbitrary graded calculi

Exact-use, upper-bound, sensitivity, and security readings are therefore examples only when their algebra validates the required order and operations. The translations preserve the written grades; they do not choose their meaning.

Chapter 53 also grades contextual requirements. The named bridge used here is corollary 54.4: the direct translation preserves the same semiring coefficient while moving it from an assumption to a box, and the CPS translation preserves it in the reverse direction. This compares contextual requirements with usage accounting at the two frozen nondependent signatures; it does not transfer a substitution or subject-reduction theorem from chapter 53.

Dependency creates the next obstruction. If a result type B(x) mentions a graded input, substitution must transport B(u) as well as scale the term context. Linear Base elimination above acts only in terms, and the reverse translation fixes a nondependent answer type K; neither correspondence theorem licenses eliminating a graded box into a type or continuation-encoding a dependency. A quantitative dependent calculus therefore needs its own judgments and metatheory.

Sources and theorem boundary

The two-base calculi and Theorems 1–2 come from the ICFP 2026 paper and its extended Appendices B.1–B.2 [LMO26]. Lemmas 1–2 of Section 3 and Theorem 1 of Section 5 supply the combined substitutions and soundness theorem [GyKO^+16]. The fractional route uses Progress 6.6, Preservation 6.7, Borrow Safety Lemma 6.8, Borrow Safety Theorem 6.9, Uniqueness Corollary 6.10, and Equational Soundness Theorem 6.11 of Marshall and Orchard [MO24a]. None of these results turns arbitrary semiring grades into cost, sensitivity, information-flow, or ownership guarantees.

Suggested first pass.

Do exercise 54.9, exercise 54.10 before the implementation problem.

Exercise 54.9

★★☆ Reconstruct the direct translation. Give the complete typing derivation for the translation of λf.λx.f(fx), including the two promotions.

Exercise 54.10

★★☆ Audit the CPS boundary. Reduce the CPS translation of one linear beta redex and one modal beta redex. Locate the full-beta steps that are not source call-by-name steps.

Exercise 54.11

★★☆ Separate coordinates. Extend the combined-grading example with an effectful producer and a duplicated read-only input. Keep the effect monoid and coeffect semiring annotations on different lines of the derivation.

Exercise 54.12

★★★ Practical project.grade-correspondence-checker The implemented calculus is the finite syntax FXApply r t u. Preserve the invariant usage(Apply r t u)=usage(t)+rusage(u) coordinatewise. Run artifacts/ch54-grade-correspondence/corpus.kp and require the named input f(fx) at r=2 to produce (3,4), its direct translation to produce the same pair, and the Boolean interpretation to produce (true,true). Acceptance is the four named PASS lines and empty audit printed in appendix E. Then add one term with an independent and a nested use. Predict both grades before running it, replace multiplication by addition, and require the unchanged nested-grade oracle to reject the mutant.

Search the book

Type to search the local edition.