Lectures onType Theory
Chapter 99
Chapter 99Optional

Quantitative Dependent Type Theory

Prerequisites. Direct starred prerequisites: Chapter 20, Chapter 40. No later core chapter depends on this route.

The indices 0,1,ω distinguish absence, exact use, and unrestricted use, but the previous chapter fixes those three quantities in advance. It cannot state that a variable is used twice, that a branch costs r, or that substitution multiplies one demand by another. Ordinary dependent typing has the opposite defect: an index may occur in a type without recording whether it survives at run time.

A quantitative judgment puts both facts in one place. Types see the whole context spine; annotations record computational demand. The annotation zero therefore means “available to type formation but absent from this computation.” It does not mean that every two inhabitants of the annotated type are propositionally equal.

Semiring-indexed contexts

Definition 99.1 — Usage semiring

A usage semiring is a structure whose operations (R,+,0,,1) satisfy the semiring laws. It must also satisfy ρ+π=0ρ=0=π,ρπ=0ρ=0 or π=0. A quantitative context has the form Γ=x1:ρ1S1,,xn:ρnSn. Contexts with the same variable and type spine are added and scaled pointwise. Scaling is shallow: ρΓ scales the annotations on the declarations, not annotations occurring inside the types Si.

The natural numbers, the Boolean semiring, and the zero–one–many semiring are examples. Positivity is used when a conclusion has quantity zero: it lets the proof recover that every summand was zero. The zero-product property is used in substitution: if a scaled demand vanishes, either the outer demand or the substituted computation’s demand vanishes.

Definition 99.2 — Judgments

QTT separates context and type formation from terms: Γ,ΓS,ΓST,ΓM:σS,ΓMN:σS, where the output quantity is restricted to σ{0,1}. Quantity zero is the type-level or erased fragment; quantity one is the present fragment. Types are formed in 0Γ, so every variable on which a type depends has zero computational demand.

Allowing an arbitrary output quantity looks more general, but it breaks substitution for the zero–one–many semiring. A term can coerce a function that uses its argument once to one announced at usage ω. Substituting that function into a context split requiring ω=1+ω would require splitting an actual usage-one derivation into usage 1 and ω, which is impossible. Restricting the output to zero or one prevents this hidden subusage step.

Lemma 99.3 — Zero needs nothing

If ΓM:0S, then 0Γ=Γ.

Proof of Lemma 99.3 — Zero needs nothing

Proof. Induct on the typing derivation. A variable derivation at output zero has a zero context by its rule. In every rule that adds premise contexts, the conclusion annotations are sums; positivity forces each summand to be zero. In every rule that scales a premise context, the conclusion annotations are products; the zero-product property separates a zero output multiplier from a zero premise annotation. Constants contribute the zero context. These are all ways in which an annotation enters a conclusion. ◻

The converse is false. A closed Boolean constant can be classified at output quantity one while requiring the zero context. Thus the lemma detects absence of input demand; it does not reconstruct the output quantity.

Exercise 99.1

★★☆ Give a two-element commutative semiring in which positivity fails, and point to the induction step of lemma 99.3 that can no longer recover zero premises. Separately explain why the natural-number semiring satisfies both additional properties.

Dependent quantitative rules

Definition 99.4 — Dependent function rules

Write (x:πS)T for a dependent function whose body uses its argument at quantity π. Its principal rules are

0ΓS0Γ,x:0ST
0Γ(x:πS)T
QTT-Π-F
Γ,x:σπSM:σT
Γλx.M:σ(x:πS)T
QTT-Lam
Γ1M:σ(x:πS)TΓ2N:σSσ=0(π=0 or σ=0)
Γ1+πΓ2MN:σT[N/x]
QTT-App

The side condition selects an erased argument when either the binder demand is zero or the whole application is in the erased fragment. Formation sees x at zero demand because T may depend on x without consuming it at run time.

The variable rule exposes both output fragments:

0Γ,x:σS,0Γ
0Γ,x:σS,0Γx:σS
QTT-Var

where σ{0,1}. At output one it consumes one copy; its erased counterpart is available in a zero context. Function application then performs the semiring calculation visible in its conclusion. If a function uses its argument three times and the argument uses resources 2Δ, the application contributes 3(2Δ)=6Δ.

Definition 99.5 — Quantitative dependent tensor

The type (x:πS)T records that eliminating the pair may use its first component at quantity π. Its formation rule is

0ΓS0Γ,x:0ST
0Γ(x:πS)T
QTT-⊗-F

Pair introduction and elimination have the characteristic context equations

Γ1M:σSΓ2N:σT[M/x]σ=0(π=0 or σ=0)
πΓ1+Γ2(M,N):σ(x:πS)T
QTT-Pair
0Γ1,z:0((x:πS)T)UΓ1P:σ(x:πS)TΓ2,x:σπS,y:σTN:σU[(x,y)/z]0Γ1=0Γ2
Γ1+Γ2let(x,y)=PinN:σU[P/z]
QTT-Let

The equality of zeroed context spines ensures that dependency information is shared even when computational demands differ. The result family is formed over a fresh pair variable z; the premise and conclusion substitute the introduced pair and the actual scrutinee for that same variable. The beta root is Γlet(x,y)=(M,N)inLL[M/x,N/y]:σU[(M,N)/z], with the contexts determined by the rule above. If the scrutinee is a neutral variable, elimination is stuck. The selected core imports this beta equation; it makes no tensor eta claim.

Exercise 99.2

★☆☆ In the natural-number usage semiring, suppose Γ1f:1(x:3A)B and 2Δu:1A. Derive the application context. Now replace the function by one whose binder quantity is zero. State which argument premise must change, use the output-quantity side condition to justify that change, and derive the resulting application context.

Substitution multiplies demand

Theorem 99.6 — Quantitative substitution

The following rules are admissible.

  1. If Γ1,x:ρS,ΓJ is a context, type, or type-equality judgment, Γ2N:0S, and 0Γ1=0Γ2, then Γ1,Γ[N/x]J[N/x].

  2. If Γ1,x:ρS,ΓM:σT,Γ2N:σS, with 0Γ1=0Γ2 and σ=0ρ=0, then (Γ1+ρΓ2),Γ[N/x]M[N/x]:σT[N/x]. The corresponding term-equality rule has the same hypotheses and context.

Proof of Theorem 99.6 — Quantitative substitution

Proof. Prove context formation, type formation, terms, and equalities simultaneously. The variable case makes the context formula necessary. When the selected variable is x, its use is ρ, so replacing it by N demands ρΓ2. When another variable is selected, x has zero demand in that branch and contributes 0Γ2.

For QTT-App, alpha-rename its binder to y and write the application as LM, with result type T[M/y]. Split the demand ρ on the substituted variable x between the function and argument derivations as ρ1+πρ2. The induction hypotheses give the contexts Δ1+ρ1Γ2 and Δ2+ρ2Γ2. Rebuilding the rule yields (Δ1+ρ1Γ2)+π(Δ2+ρ2Γ2)=distrib.Δ1+πΔ2+(ρ1+πρ2)Γ2=demandΔ1+πΔ2+ρΓ2. The rebuilt type is T[N/x][M[N/x]/y], equal to T[M/y][N/x] by capture-avoiding substitution composition.

For QTT-Lam, alpha-rename the binder and use ρ(σπ)=(ρσ)π to rebuild its body demand. The pair case uses distributivity in the same direction as application. In QTT-Let, split the substituted variable’s demand between the scrutinee and body premises as ρ1+ρ2. The induction hypotheses yield Δ1+ρ1Γ2 and Δ2+ρ2Γ2; rebuilding the exact rule gives their sum, which is Δ1+Δ2+(ρ1+ρ2)Γ2 by associativity and commutativity. The zero-spine premise is preserved by clause (1), and capture-avoiding composition identifies U[P/z][N/x] with U[N/x][P[N/x]/z]. Formation and equality cases use clause (1); constants and unit constructors contribute zero. Positivity and the zero-product property discharge the side conditions in precisely the branches where an output quantity is zero. Every rule is therefore reconstructed with the displayed context. ◻

The multiplication ρΓ2 is not optional bookkeeping. Replacing it by Γ2 undercounts an argument used twice; replacing it by ρ+Γ2 is ill sorted because ρ is a scalar and Γ2 is a vector of demands.

Corollary 99.7 — Beta preservation

If Γ(λx.M)N:σT[N/x], then ΓM[N/x]:σT[N/x]. Consequently compatible beta reduction preserves QTT typing in the displayed function-and-tensor fragment.

Proof of Corollary 99.7 — Beta preservation

Proof. Invert QTT-App and QTT-Lam. Their contexts expose exactly the two premises and the scalar ρ required by theorem 99.6(2). Apply that theorem for the root beta step. For a compatible step, induct on the one-hole term context, use the induction hypothesis for its changed premise, and rebuild the enclosing rule. The dependent result classifier is unchanged by substitution composition. ◻

This corollary is preservation for the frozen beta fragment. It does not assert normalization or decidability for Idris 2, whose elaborator, effects, pattern compilation, and run-time system have a larger signature.

Exercise 99.3

★★☆ Write the QTT-Lam case of theorem 99.6(2). Display the scalar equation aligning the substituted body context with the abstraction rule, and state where alpha-renaming is needed.

Zero demand is not proof irrelevance

Let n:0Nat occur in the type Vec(A,n). The zero annotation says that evaluating the term does not inspect the run-time representation of n. The type checker may still distinguish Vec(A,0) from Vec(A,1), and a proof of 0=1 is not thereby created.

Proposition 99.8 — Zero-context boundary

For the QTT judgments of definition 99.2, if ΓM:0S, then every declaration in Γ has quantity zero.

Proof of Proposition 99.8 — Zero-context boundary

Proof. This is lemma 99.3. ◻

No operational erasure map has been defined, so the proposition makes no claim about compiled representations. It also gives neither proof irrelevance nor term equality: the closed terms true and false require no input resources but remain distinguishable by the Boolean eliminator.

Now consider an append operation append:(n:0Nat)(m:0Nat)Vec(A,n)Vec(A,m)Vec(A,n+m). The indices n,m are checked and may occur repeatedly in the result type, but the program traverses the two vector values. In the Idris 2 v0.8.0 tutorial, the corresponding hole context prints 0 beside n, m, and A, and no quantity beside the unrestricted vector arguments. That surface trace witnesses the implementation’s elaborated multiplicities; it is not the proof of theorem 99.6.

The pinned v0.8.0 transcript records the accepted elaboration state as

Main> :t append_rhs
 0 m : Nat
 0 a : Type
 0 n : Nat
   ys : Vect m a
   xs : Vect n a
-------------------------------------
append_rhs : Vect (plus n m) a

and the nearby attempted linear duplicator is rejected with

While processing right hand side of Main.duplicate:
There are 2 uses of linear name x

These are implementation observations from the retained tutorial [Idr26], not premises of the QTT proofs.

A nearby rejected definition has type (x:1A)(y:1A)A and body (x,x). Pair formation adds the two uses, producing demand 1+1, not the binder’s promised 1. An accepted linear state transition instead consumes a door once and returns the new door state, threading the result into the next operation.

Exercise 99.4

★★☆ Give quantitative contexts for both branches of vector append. Verify that the length indices have quantity zero while the vector spine used by the recursive call has positive demand. Explain why replacing the zero on n by one changes run-time availability but not the proposition n+m=m+n.

Suggested first pass.

None of these problems is a prerequisite for a later chapter. Begin with exercise 99.5, then complete exercise 99.7.

Exercise 99.5

★★☆ Reconstruct the QTT-App and QTT-Let cases of quantitative substitution over the natural-number semiring. Annotate each distributivity or associativity step and check the zero-output side conditions separately.

Exercise 99.6

★★★ Use the zero–one–many semiring to reconstruct the failed substitution from the source calculus with an arbitrary output quantity. Exhibit the equation ω=1+ω, the premise derivation that would have to split, and the missing subusage rule. Then show why restricting output quantities to zero and one blocks the construction.

Exercise 99.7

★★★ Practical project.qtt-demand-calculator Implement in Agda or Kappa context vectors over the natural-number and zero–one–many semirings, together with addition, scaling, and the application instance of quantitative substitution. Maintain the invariant that every output vector has the same declaration spine as its inputs. On the named cases twice-of-two, erased-index, and the promise-versus-inferred duplicate-use case, require these exact stdout fragments:

twice-of-two demand 4
erased-index demand 0
promise-vs-inferred rejects: promised 1, inferred many

Also require acceptance of a preserved VX,VY spine and rejection of a mismatched VX,VZ spine. A mutation that makes spine comparison constantly true must fail the mismatch oracle. The calculator checks finite semiring equations; it does not prove admissibility for all QTT derivations or an Idris erasure theorem.

Sources. The judgment forms, positive zero-product usage semiring, zero-needs-nothing lemma, and substitution theorem follow Atkey’s QTT calculus [Atk18]. The exact substitution rules and the counterexample to arbitrary output quantities are on p. 5 of the pinned LICS paper. The Idris 2 v0.8.0 multiplicities tutorial supplies the vector-hole and linear resource traces [Idr26]; those implementation traces are kept separate from the paper calculus’s metatheory.

Search the book

Type to search the local edition.