Prerequisites. Direct starred prerequisites: none. No later core chapter depends on this route.
Chapter 3 used the discrete base type 𝖭𝖺𝗍. To expose the problem, first imagine an unindexed numeric type 𝖭𝗎𝗆 whose values are rational numbers. Write 𝑞@𝑢 for the literal coefficient 𝑞 expressed in unit 𝑢. With only that unindexed type, both 3@𝗆+2@𝗌 and 100@𝖼𝗆+1@𝗆 have type 𝖭𝗎𝗆. The first adds unlike dimensions; the second adds like dimensions written at different scales. Neither distinction is visible to an ordinary numeric type checker. We therefore reuse the syntax and typing discipline of the pure let-language in chapter 3 and add a small algebra of dimensions. The resulting numeric type is indexed: its dimension appears as an argument, as in 𝖭𝗎𝗆[𝐿]. Inference remains a problem: a programmer may write a generic square function without naming its input dimension, and the algorithm must infer the most general answer.
Dimensions are normalized exponent vectors
Fix a finite set B of base dimensions. In examples we use 𝐿 for length, 𝑇 for time, and 𝑀 for mass. A finite set 𝐷 of dimension variables is disjoint from B. Each 𝛿∈𝐷 is a formal generator that a substitution may later replace by a normalized dimension.
A normalized dimension expression over a finite variable set 𝐷 is an exponent map 𝑑:B∪𝐷→ℤ. Since B∪𝐷 is finite, every such map has finite support. For a generator 𝑔∈B∪𝐷, write 𝑒𝑔 for the map with exponent 1 at 𝑔 and 0 elsewhere; in dimension expressions, the same letter 𝑔 denotes 𝑒𝑔. Surface multiplication is pointwise addition of exponent maps, (𝑑𝑒)(𝑔)=𝑑(𝑔)+𝑒(𝑔); inversion is pointwise negation, and 𝑑𝑛(𝑔)=𝑛𝑑(𝑔). Thus the multiplicative identity 1 is the all-zero map. We use multiplicative notation because it matches dimensional analysis, although the underlying group operation is addition of integer exponents.
Before normalization, surface dimension expressions have grammar ̂𝑑::=1∣𝑔∣̂𝑑̂𝑑∣̂𝑑𝑛(𝑔∈B∪𝐷,𝑛∈ℤ). Write ̂𝑑−1 for the power ̂𝑑−1. The normalization map 𝑁 is defined by 𝑁(1)=0,𝑁(𝑔)=𝑒𝑔,𝑁(̂𝑑̂𝑒)=𝑁(̂𝑑)+𝑁(̂𝑒),𝑁(̂𝑑𝑛)=𝑛𝑁(̂𝑑). From here on 𝑑,𝑒 denote the resulting normalized maps; an instruction to “normalize” a surface expression asks for this map in multiplicative normal form.
When normalized maps 𝑑 and 𝑒 have both been formed over the same variable set 𝐷, write 𝑑=𝐷𝑒 when their coefficients agree at every generator of B∪𝐷. Maps formed over different variable sets are first carried to a common variable set by a substitution; they are not compared as written.
Thus 𝐿𝑇−1⋅𝑇=𝐿,(𝐿𝑇−2)2=𝐿2𝑇−4, whereas 𝐿≠𝐷𝑇. Equality here is decidable by comparing the finitely many integer coefficients. It says nothing about numerical values.
Let 𝐷′ be another finite set of such variables. A dimension-variable substitution𝜌 assigns to each 𝛿∈𝐷 a normalized map 𝜌(𝛿):B∪𝐷′→ℤ. Extend it to base generators by 𝑏[𝜌]=𝑏 and write 𝛿[𝜌]=𝜌(𝛿). Its extension to every normalized dimension is 𝑑[𝜌]=∏𝑏∈B𝑏𝑑(𝑏)∏𝛿∈𝐷𝛿[𝜌]𝑑(𝛿). The products are the multiplicative notation for addition of exponent maps. A commutative group on generators 𝐺 is free abelian when every assignment of the generators to a commutative group 𝐻 extends to exactly one group homomorphism into 𝐻.
The normalized expressions on B∪𝐷 form a free abelian group. Every dimension-variable substitution is a group homomorphism. In particular, if 𝑑=𝐷𝑒, then 𝑑[𝜌]=𝐷𝑒[𝜌].
Proof. Pointwise integer addition is associative and commutative, the zero map is its identity, and 𝑑+(−𝑑)=0 pointwise for every exponent map 𝑑. For the homomorphism law, add the two exponents belonging to each generator: (𝑑𝑒)[𝜌]=∏𝑏∈B𝑏𝑑(𝑏)+𝑒(𝑏)∏𝛿∈𝐷𝛿[𝜌]𝑑(𝛿)+𝑒(𝛿)=⎛⎜
⎜
⎜
⎜⎝∏𝑏∈B𝑏𝑑(𝑏)∏𝛿∈𝐷𝛿[𝜌]𝑑(𝛿)⎞⎟
⎟
⎟
⎟⎠⎛⎜
⎜
⎜
⎜⎝∏𝑏∈B𝑏𝑒(𝑏)∏𝛿∈𝐷𝛿[𝜌]𝑒(𝛿)⎞⎟
⎟
⎟
⎟⎠=𝑑[𝜌]𝑒[𝜌]. Negating every exponent in the same formula gives 𝑑−1[𝜌]=𝑑[𝜌]−1. If 𝑑=𝐷𝑒, then 𝑑(𝑔)=𝑒(𝑔) for every generator 𝑔; substituting those equal coefficients in the same formula gives 𝑑[𝜌]=𝐷𝑒[𝜌]. For freeness, choose arbitrary images ℎ𝑔 of the generators in an abelian group 𝐻, and define 𝜙(𝑑)=∏𝑔∈B∪𝐷ℎ𝑑(𝑔)𝑔. The product is finite because 𝑑 has finite support. The calculation just used for substitution proves that 𝜙 is a homomorphism. Every 𝑑 is the displayed finite product of generator powers; hence any homomorphism with the chosen generator images must have the displayed value 𝜙(𝑑). This proves uniqueness. ◻
A unit environmentU maps each primitive unit name 𝑢 to a positive rational scale𝑠𝑢 and a closed dimension𝑑𝑢:B→ℤ, which contains no dimension variables. The scale is measured relative to a chosen canonical unit system: meters for 𝐿, seconds for 𝑇, and the induced products and powers for compound dimensions. Multiplication by a rational scale keeps every stored coefficient rational. Replacing scales by positive reals changes only the coefficient domain, not the dimension group or its typing theory. For dimensionless angles, choose radians as the canonical unit; degrees and radians then both have dimension 1, while one degree has real scale 𝜋/180. If 𝑢0 ranges over primitive names, compound units have grammar 𝑢::=𝑢0∣𝑢𝑢∣𝑢−1∣𝑢𝑛(𝑛∈ℤ). Their scales and dimensions use 𝑠𝑢𝑣=𝑠𝑢𝑠𝑣,𝑑𝑢𝑣=𝑑𝑢𝑑𝑣,𝑠𝑢−1=𝑠−1𝑢,𝑑𝑢−1=𝑑−1𝑢,𝑠𝑢𝑛=𝑠𝑛𝑢,𝑑𝑢𝑛=𝑑𝑛𝑢. Write U∗(𝑢)=(𝑠𝑢,𝑑𝑢) for this recursive extension of U from primitive names to compound unit expressions.
For meters, centimeters, and seconds, take 𝗆↦(1,𝐿),𝖼𝗆↦(1/100,𝐿),𝗌↦(1,𝑇). The two length units have equal dimensions and different scales. A meter and a second have different dimensions even though both scales happen to be one. Thus 100@𝖼𝗆+1@𝗆 is dimensionally well typed. Its numerical meaning first replaces each coefficient 𝑞 by its canonical coefficient 𝑞𝑠𝑢. The two addends become 1 and 1 at dimension 𝐿, so their sum is the canonical value 2.
A pure dimensioned let-language
The source calculus is deliberately small. Its arithmetic exposes the dimension equations; its lambda and let forms expose polymorphic inference.
𝜏::=𝛼∣𝖭𝗎𝗆[𝑑]∣𝜏→𝜏,𝜎::=∀¯𝛼¯𝛿.𝜏,𝑒::=𝑥∣𝑞@𝑢∣𝜆𝑥.𝑒∣𝑒𝑒∣𝗅𝖾𝗍𝑥=𝑒𝗂𝗇𝑒::=∣𝑒+𝑒∣𝑒∗𝑒∣𝑒/𝑒∣𝑒⟨𝑛⟩. Here 𝑞∈ℚ, 𝑛∈ℤ, and 𝑢 is a well-formed compound unit expression of definition 6.3. The angle brackets in 𝑒⟨𝑛⟩ mark 𝑛 as an integer written in the term syntax, not as a source subexpression. Ordinary type variables 𝛼,𝛽,… and dimension variables 𝛿,𝜖,… are disjoint sorts. Formally, if A is a finite set of ordinary variables, formation is generated by
𝛼∈A
A;𝐷⊢𝛼𝗍𝗒𝗉𝖾
F-TVar
𝑑isanormalizeddimensionover𝐷
A;𝐷⊢𝖭𝗎𝗆[𝑑]𝗍𝗒𝗉𝖾
F-Num
A;𝐷⊢𝜏1𝗍𝗒𝗉𝖾A;𝐷⊢𝜏2𝗍𝗒𝗉𝖾
A;𝐷⊢𝜏1→𝜏2𝗍𝗒𝗉𝖾
F-Arrow
A scheme is formed by checking its body after adding its quantified ordinary and dimension variables to the corresponding sets. Fix a total order on variable names; every bar lists its finite set in that order. The displayed sort order has no semantic effect. Thus 𝖭𝗎𝗆[𝛼] and 𝛼→𝛿 are not merely discouraged; they are ill sorted. Type substitution preserves these sorts and acts on both kinds of variable.
Instantiation and generalization are those of HM, extended to both sorts of variables. Write fdv for free dimension variables, the dimension-sorted analogue of ftv. List each finite difference below in the fixed total order and set GenΓ(𝜏)=∀(ftv(𝜏)∖ftv(Γ))(fdv(𝜏)∖fdv(Γ)).𝜏. The term grammar has no references, assignment, or other effects. Therefore the unrestricted HM generalization rule of chapter 3 is sound; the value restriction used for effectful extensions is not needed here.
Fix the unit environment U throughout this judgment. Use the HM variable, instantiation, lambda, application, generalization, and let rules with the extended type grammar. Numerically, 𝑞@𝑢 has canonical coefficient 𝑞𝑠𝑢; its type records only 𝑑𝑢, because scale changes a coefficient but never changes whether the literal is dimensionally well formed. The additional arithmetic typing rules are
U∗(𝑢)=(𝑠𝑢,𝑑𝑢)
Γ⊢𝑞@𝑢:𝖭𝗎𝗆[𝑑𝑢]
D-Lit
Γ⊢𝑒1:𝖭𝗎𝗆[𝑑]Γ⊢𝑒2:𝖭𝗎𝗆[𝑑]
Γ⊢𝑒1+𝑒2:𝖭𝗎𝗆[𝑑]
D-Add
Γ⊢𝑒1:𝖭𝗎𝗆[𝑑1]Γ⊢𝑒2:𝖭𝗎𝗆[𝑑2]
Γ⊢𝑒1∗𝑒2:𝖭𝗎𝗆[𝑑1𝑑2]
D-Mul
Γ⊢𝑒1:𝖭𝗎𝗆[𝑑1]Γ⊢𝑒2:𝖭𝗎𝗆[𝑑2]
Γ⊢𝑒1/𝑒2:𝖭𝗎𝗆[𝑑1𝑑−12]
D-Div
Γ⊢𝑒:𝖭𝗎𝗆[𝑑]
Γ⊢𝑒⟨𝑛⟩:𝖭𝗎𝗆[𝑑𝑛]
D-Pow
Extend =𝐷 from dimensions to types structurally: a type variable is equivalent only to itself, arrows are compared componentwise, and 𝖭𝗎𝗆[𝑑]=𝐷𝖭𝗎𝗆[𝑒] exactly when 𝑑=𝐷𝑒. The explicit conversion rule is Γ⊢𝑒:𝜏𝜏=𝐷𝜏′Γ⊢𝑒:𝜏′D−Conv. Because annotations store normalized maps, any use of D-Conv can be removed: its premise and conclusion types have the same normalized syntax. We retain the rule so metatheoretic derivations can name the equality step explicitly.
The judgment Γ⊢s𝑒:𝜏 uses the four syntax-directed HM rules of definition 3.31, generalized over both variable sorts, together with D-Lit, D-Add, D-Mul, D-Div, and D-Pow from definition 6.5. All dimension annotations are stored in normal form, so it has no D-Conv rule.
Proof of Lemma 6.7 — Dimension typing is syntax-directed
Proof. For the left-to-right direction, induct on the declarative typing derivation. Handle Inst and Gen as in theorem 3.34, first alpha-renaming the quantified ordinary and dimension variables away from every variable already in the derivation. In an arithmetic case, the induction hypotheses give syntax-directed premises at the numeric types shown in that rule; reapply the same arithmetic rule. A final D-Conv disappears because normalized dimensions satisfying 𝑑=𝐷𝑑′ are the same finite map, and the structural definition of type equivalence never changes an outer constructor. For the right-to-left direction, insert the declarative HM rules and reuse each arithmetic rule unchanged. ◻
If 𝜏=𝐷𝜏′, then the two types have the same outer constructor. If that constructor is an arrow, their domains and codomains are respectively equivalent; if it is 𝖭𝗎𝗆, only the dimension annotation may change. In particular, no type variable or arrow is equivalent to a numeric type.
Proof of Lemma 6.8 — Dimension conversion preserves type shape
Proof. Inspect the three defining clauses. Equal variables have the same variable head. For arrows, the definition compares the domains and codomains. For numeric types, it compares only their dimension annotations. No clause relates two different outer constructors. ◻
The declarative rules assign the function 𝗌𝗊𝗎𝖺𝗋𝖾=𝜆𝑥.𝑥∗𝑥 the monotype 𝖭𝗎𝗆[𝛿]→𝖭𝗎𝗆[𝛿2] under a free generator 𝛿∈𝐷. Generalizing 𝛿 gives the scheme ∀𝛿.𝖭𝗎𝗆[𝛿]→𝖭𝗎𝗆[𝛿2]. Instantiating 𝛿=𝐿 squares a length; instantiating 𝛿=𝑇−1 squares a frequency. By contrast, 3@𝗆+2@𝗌 has no derivation because D-Add would require 𝐿=𝐷𝑇.
★★☆ Derive the displayed scheme for 𝗌𝗊𝗎𝖺𝗋𝖾. Derive and generalize the type of 𝜆𝑥.𝜆𝑦.(𝑥∗𝑦)/𝑦. Normalize only its dimension: cancelling 𝑦 changes behavior at 𝑦=0, where the original divides by zero.
A short mechanics calculation now checks without a special acceleration primitive. The following display abbreviates the empty-environment monotype derivation followed by generalization: 𝖺𝖼𝖼𝖾𝗅=𝜆𝑠.𝜆𝑡.𝑠/(𝑡∗𝑡):∀𝛿𝜖.𝖭𝗎𝗆[𝛿]→𝖭𝗎𝗆[𝜖]→𝖭𝗎𝗆[𝛿𝜖−2]. At 𝑠=100@𝗆 and 𝑡=10@𝗌, the result has dimension 𝐿𝑇−2. Trying to add this result to 𝑠 is rejected.
Solving dimension equations
Ordinary syntactic unification is not enough. The equation 𝛿𝜖=𝐿 has infinitely many solutions and should return one principal family: a substitution containing fresh parameters through which every solution factors. It must not guess a unit for either variable.
Syntactic decomposition treats multiplication as an ordered constructor and therefore cannot respect 𝛿𝜖=𝜖𝛿. Solving for a variable of exponent ±1 repairs the first example: 𝛿=𝐿𝜖−1. A single powered variable already shows the integer obstruction. For 𝑛>0, the equation 𝑧𝑛=𝑐 is the coefficient family 𝑛𝑧(𝑔)=𝑐(𝑔)(𝑔∈B∪𝐷), so it has a solution exactly when 𝑛 divides every coefficient 𝑐(𝑔). With several variables, divisibility may become visible only after a change of integer basis. For example, direct isolation fails on 𝛿2𝜖3=𝐿, where neither exponent is invertible in ℤ, although the principal family 𝛿=𝐿−1𝜂3,𝜖=𝐿𝜂−2 exists for a fresh parameter 𝜂. The row [23] records the exponents of (𝛿,𝜖) in this equation. Elimination needs an invertible column change that replaces this row by [10], exposing the gcd of 2 and 3. The Smith reduction constructed below performs that change for every integer matrix.
A square integer matrix is unimodular when its determinant is 1 or −1; equivalently, its inverse also has integer entries. Such a matrix changes an integer basis without changing the integer combinations it generates. The elementary row and column operations below are unimodular.
Bézout’s identity is the arithmetic step behind the first operation. Let 𝑎,𝑏 be not both zero. If both are nonzero, run the extended Euclidean algorithm on |𝑎|,|𝑏| with remainders 0≤𝑟< the positive divisor. If its back-substitution coefficients are 𝑝0,𝑞0, put 𝑝=sgn(𝑎)𝑝0,𝑞=sgn(𝑏)𝑞0,𝑝𝑎+𝑞𝑏=gcd(𝑎,𝑏)>0. For 𝑏=0, take (𝑝,𝑞)=(sgn(𝑎),0); for 𝑎=0, take (𝑝,𝑞)=(0,sgn(𝑏)). This fixed routine assigns one output pair (𝑝,𝑞) to every ordered pair (𝑎,𝑏)≠(0,0).
Proof. Bézout’s identity gives 𝑝,𝑞∈ℤ with 𝑝𝑎+𝑞𝑏=𝑔. Take 𝑃=[𝑝𝑞−𝑏/𝑔𝑎/𝑔]. Its determinant is (𝑝𝑎+𝑞𝑏)/𝑔=1, and direct multiplication gives both displayed equations. Thus the row or column pair (𝑎,𝑏) can be replaced by (𝑔,0) using invertible integer operations. ◻
Proof. Move a nonzero entry to the upper-left corner. Apply lemma 6.9 successively to the pivot and each other entry of its column using row operations, then to the pivot and each other entry of its row using column operations. The row operations that clear the first column may reintroduce entries to the right of the pivot; the column operations that clear the first row may reintroduce entries below it. We therefore alternate these two clearing phases. If a phase exposes an entry not divisible by the positive pivot, the next two-entry reduction replaces that pivot by a strictly smaller gcd. If every exposed entry is divisible by the pivot, subtracting the corresponding integral multiples clears it without changing the pivot or the already-clear companion line. Thus either the pivot strictly decreases or both the first row and first column become clear. Only finitely many decreases are possible, so the process ends with a clear first row and column and a positive pivot 𝑑.
Suppose an entry 𝑎 of the remaining block is not divisible by 𝑑. Add its column to the first column. That first column now contains both 𝑑 and 𝑎. The two-entry reduction replaces the pivot by gcd(𝑑,𝑎)<𝑑; clear the first row and column again. Each repair strictly decreases the positive pivot, so only finitely many repairs occur. At termination the pivot divides every entry of the remaining block. ◻
After ordering the base dimensions, an exponent map is a vector in ℤ𝑚. An integer lattice is a subgroup of ℤ𝑚 generated by integer linear combinations of finitely many vectors. The exponent vectors of the chosen base dimensions generate the whole integer lattice ℤ𝑚. Multiplication by a matrix in GL𝑚(ℤ) replaces one ordered integer basis by another. For example, relative to the basis (𝐿,𝑇), the proposed basis (𝐿,𝐿𝑇−1) has column matrix [110−1],det[110−1]=−1. It therefore generates the same integer lattice. Explicitly, for every 𝑎,𝑏∈ℤ, 𝐿𝑎𝑇𝑏=𝐿𝑎+𝑏(𝐿𝑇−1)−𝑏.
For every integer 𝑚-by-𝑛 matrix 𝑀, integer row and column operations produce unimodular matrices 𝑈∈GL𝑚(ℤ) and 𝑉∈GL𝑛(ℤ) such that 𝑈𝑀𝑉=𝑆=diag(𝑠1,…,𝑠𝑟,0,…,0),0<𝑠1∣𝑠2∣⋯∣𝑠𝑟. This diagonal matrix 𝑆 is a Smith normal form. Thus GL𝑚(ℤ) is the group of 𝑚-by-𝑚 unimodular integer matrices under multiplication.
Proof of Lemma 6.11 — Smith reduction over the integers
Proof. Induct on min(𝑚,𝑛). The zero matrix is already in the required form. For a nonzero matrix, lemma 6.10 gives 𝑀∼[𝑠100𝑀1],𝑠1>0,𝑠1∣(𝑀1)𝑖𝑗forall𝑖,𝑗. Apply the induction hypothesis to 𝑀1, using operations confined to its rows and columns. Because every entry of 𝑀1 is a multiple of 𝑠1, all integer combinations created by those operations remain multiples of 𝑠1. The first new positive pivot 𝑠2 is therefore divisible by 𝑠1; the same argument repeats in each smaller block. This produces 0<𝑠1∣⋯∣𝑠𝑟 and a zero residual block.
Every operation used in lemma 6.9, lemma 6.10 has a unimodular elementary matrix. Multiplying the recorded row matrices and column matrices gives 𝑈 and 𝑉. Only existence of this diagonal form is used below; uniqueness of its invariant factors is not claimed here. ◻
The principal dimension solver returns a solution through which every solution of the same dimension equations factors. Fix unknown dimension variables ¯𝛿=(𝛿1,…,𝛿𝑛), and let Gr be the finite set of dimension variables declared rigid. For a finite ordered system 𝐸=(𝑑𝑖=𝐷𝑒𝑖)𝑚𝑖=1, move unknown-variable factors to the left and base-dimension factors to the right: 𝑛∏𝑗=1𝛿𝑀𝑖𝑗𝑗=𝑐𝑖,𝑀𝑖𝑗=𝑑𝑖(𝛿𝑗)−𝑒𝑖(𝛿𝑗),𝑐𝑖=∏𝑔∈B∪Gr𝑔𝑒𝑖(𝑔)−𝑑𝑖(𝑔). The list ¯𝛿 is exactly the set of dimension variables occurring in 𝐸 that the solver is permitted to instantiate. Base dimensions and any dimension variables explicitly declared rigid are moved to the right-hand constants instead. Thus no variable occurring in an equation is silently neither solved nor declared rigid. The inference system of this chapter takes Gr=∅ and declares every occurring dimension variable solvable; the rigid clause is recorded for uses of the solver under an ambient scheme or annotation that fixes selected variables. Thus 𝑀 has integer entries, while the unknown vector 𝑧𝑗=𝛿𝑗[𝜌] and the right-hand vector 𝑐𝑖 have entries in the dimension group. The notation 𝑀𝑧=𝑐 abbreviates the multiplicative equations (𝑀𝑧)𝑖=∏𝑗𝑧𝑀𝑖𝑗𝑗=𝑐𝑖; integers act by taking powers.
Compute 𝑈𝑀𝑉=𝑆 as in lemma 6.11, put 𝑧=𝑉𝑧′, and 𝑐′=𝑈𝑐, where integer matrices act on group-valued vectors by the same power-and-product convention. Each pivot row is (𝑧′𝑖)𝑠𝑖=𝑐′𝑖. It has a solution exactly when 𝑠𝑖 divides every generator exponent occurring in 𝑐′𝑖; a zero row requires 𝑐′𝑖=1. The coordinates 𝑧′𝑗 corresponding to zero columns of 𝑆 are unconstrained; name them by fresh dimension parameters ¯𝜂. Solve the pivots, substitute back through 𝑧=𝑉𝑧′, and call the resulting substitution 𝜌𝐸.
A substitution 𝜃solves𝐸 when 𝑑𝑖[𝜃]=𝐷𝑒𝑖[𝜃] for every displayed equation. It is a Gr-relative solution when it solves 𝐸 and fixes every declared rigid variable: 𝛾[𝜃]=𝛾 for 𝛾∈Gr. Every occurrence of “solution” in the solver contract below means this relative solution. A base dimension 𝑏∈B is not a substitution variable, so every substitution satisfies 𝑏[𝜃]=𝑏.
The solver uses the following fixed routine. In each active block, choose the first nonzero entry in row-major order and move it to the upper-left corner. Clear the active column from top to bottom and the active row from left to right, using the canonical extended-gcd coefficients fixed above. If the pivot fails to divide the residual block, repair it with the first offending entry in row-major order and restart those two scans. Normalize every pivot to be positive before recursing on the lower-right block. Allocate parameters to zero columns from left to right using the global dimension-variable supply. The supply begins above every dimension variable in the input equations and the rigid set. If the caller supplies a finite protected set 𝑃, it also begins above every variable in 𝑃. Hence the ordered input tuple (𝐸,Gr,𝑃,𝗌𝗎𝗉𝗉𝗅𝗒) determines (𝑈,𝑉,𝜌𝐸,𝗌𝗎𝗉𝗉𝗅𝗒′). Other Smith reductions still produce mutually instantiable principal answers by theorem 6.13.
For example, solve 𝛿𝜖=𝐿 while declaring 𝜖 rigid. Then ¯𝛿=(𝛿), the matrix is [1], and the right-hand constant is 𝐿𝜖−1. The solver returns 𝛿↦𝐿𝜖−1 and leaves 𝜖 fixed. Without the rigid declaration the same equation instead returns a one-parameter family.
Here is the promised matrix calculation. For 𝛿𝜖=𝐿, 𝑀=[11],𝑧=[𝛿𝜖],𝑐=[𝐿]. Take 𝑈=[1],𝑉=[1−101],𝑈𝑀𝑉=[10]. The transformed equation fixes 𝑧′1=𝐿 and leaves 𝑧′2=𝜂 free. Since 𝑧=𝑉𝑧′, 𝛿[𝜌𝐸]=𝐿𝜂−1,𝜖[𝜌𝐸]=𝜂. Every concrete solution chooses a dimension for 𝜂. A different legal Smith reduction may print a different parameterization, but theorem 6.13 shows that the results are mutual instances on the variables of 𝐸. For 𝛿2=𝐿, the matrix is [2]; its pivot equation 𝑧2=𝐿 fails because 2 does not divide the exponent 1 of 𝐿.
For a genuinely nontrivial Smith step, take 𝛿2𝜖3=𝐿. Here 𝑀0=[23]. Execute the column algorithm rather than guessing its change of basis: 𝑀0=[23],𝐶2←𝐶2−𝐶1,𝑀1=[21],𝐶1↔𝐶2,𝑀2=[12],𝐶2←𝐶2−2𝐶1,𝑀3=[10]. The corresponding elementary matrices multiply to 𝑉=[1−101][0110][1−201]=[−131−2],𝑀0𝑉=[10]. Since det(𝑉)=−1, this is a legal unimodular column transformation. With 𝑧=𝑉𝑧′, the transformed equation fixes 𝑧′1=𝐿 and leaves 𝑧′2=𝜂. Substitution back gives 𝛿[𝜌𝐸]=(𝑧′1)−1(𝑧′2)3=𝐿−1𝜂3,𝜖[𝜌𝐸]=𝑧′1(𝑧′2)−2=𝐿𝜂−2, whose exponents verify 𝛿2𝜖3=𝐿. This is the same style of Smith calculation used in exercise 6.7, whose matrix is instead [46]. The two legal parameterizations illustrate the mutual factorization clause of theorem 6.13. The two columns satisfy [23](−1,1)𝖳=1,[23](3,−2)𝖳=0. Thus the first column gives the pivot and the second spans the homogeneous integer solutions.
Row operations become visible in a two-equation system. Consider 𝛿2=𝐿2,𝛿2𝜖4=𝐿2𝑇4. Its matrix and right-hand vector are 𝑀=[2024],𝑐=[𝐿2𝐿2𝑇4]. Apply 𝑅2←𝑅2−𝑅1. Then 𝑈=[10−11],𝑉=𝐼,𝑈𝑀𝑉=[2004],𝑈𝑐=[𝐿2𝑇4]. Here the second entry of 𝑈𝑐 is the group quotient (𝐿2𝑇4)(𝐿2)−1=𝑇4, not an additive subtraction. The pivot chain is 2∣4, and the transformed equations are (𝑧′1)2=𝐿2 and (𝑧′2)4=𝑇4. They give 𝛿=𝐿 and 𝜖=𝑇. Replacing the second right-hand side by 𝐿2𝑇2 makes the second pivot fail because 4∤2.
If the solver returns 𝜌𝐸, then it is a Gr-relative solution of 𝐸. For every other Gr-relative solution 𝜃, there is a substitution 𝜓 for the fresh parameters that fixes Gr, such that 𝜃={𝛿1,…,𝛿𝑛}𝜌𝐸;𝜓. Here (𝛿1,…,𝛿𝑛) is the ordered unknown list fixed in definition 6.12; the restricted equality means agreement on exactly that list. Both sides already fix the declared rigids. The solver reports failure exactly when 𝐸 has no Gr-relative solution.
Proof of Theorem 6.13 — Dimension-solver soundness and principality
Proof. Invertible integer row operations preserve the solution set of 𝑀𝑧=𝑐, and the change of variables 𝑧=𝑉𝑧′ is bijective because 𝑉−1 is integral. The diagonal system separates into equations (𝑧′𝑖)𝑠𝑖=𝑐′𝑖. Such an equation has a solution in the free abelian group exactly when 𝑠𝑖 divides every generator coefficient of 𝑐′𝑖; a zero row reads 1=𝑐′𝑖. These are precisely the failure tests in definition 6.12.
The dimension group is torsion-free: if 𝑧𝑠𝑖=(𝑧′)𝑠𝑖 for 𝑠𝑖>0, then 𝑧=𝑧′, coefficient by coefficient in its free abelian basis. Hence a passing pivot equation has exactly one solution. Division fixes each pivot coordinate and the remaining coordinates are arbitrary. Naming them ¯𝜂 gives 𝜌𝐸, so substitution back through 𝑉 proves soundness. Any solution of the diagonal system chooses values for exactly those free coordinates. Define 𝜓 by mapping each 𝜂𝑖 to the coordinate selected by 𝜃 and by fixing every declared rigid. Since 𝑧=𝑉𝑧′ is bijective, substitution back gives 𝛿𝑗[𝜃]=𝛿𝑗[𝜌𝐸;𝜓](1≤𝑗≤𝑛). Because the matrix construction never substitutes for a declared rigid, 𝜌𝐸 fixes Gr. This proves principality and completeness in the postfix convention of chapter 3. ◻
Let the dimension vectors of 𝑛 quantities be the columns of an integer matrix 𝐷 of rank 𝑟 over ℚ. The integer kernel of 𝐷 has rank 𝑛−𝑟. Each kernel vector 𝑣 gives the formal dimensionless monomial ∏𝑗𝑞𝑣𝑗𝑗, and a Smith basis gives 𝑛−𝑟 independent such monomials. For a numerical interpretation, require 𝑞𝑗≠0 whenever 𝑣𝑗<0.
Proof of Corollary 6.14 — Integer-exponent Buckingham π count
Proof. Smith reduction sends 𝐷 by invertible integer changes of basis to a matrix with 𝑟 nonzero pivots and 𝑛−𝑟 zero columns: invertibility of 𝑈,𝑉 over ℚ preserves rank, so the pivot count is 𝑟. Writing 𝑈𝐷𝑉=𝑆 gives 𝐷𝑣=0 iff 𝑆(𝑉−1𝑣)=0. Thus the last 𝑛−𝑟 columns of 𝑉, rather than the literal zero columns of 𝑆, form a ℤ-basis of the original integer kernel. Their coordinates freely parameterize that kernel, and 𝐷𝑣=0 says exactly that the monomial’s base-dimension exponents all vanish. This lattice statement is the integer-exponent form of the Buckingham 𝜋 theorem. ◻
For a pendulum, order the quantities as period 𝑝, length 𝑙, and acceleration 𝑔. Their 𝐿,𝑇 exponent columns have kernel generated by (2,−1,1), so 𝑝2𝑔/𝑙 is dimensionless.
Now restrict to positive quantities. A positive change of length and time coordinates has the form 𝑝′=𝑡𝑝,𝑙′=𝜆𝑙,𝑔′=𝜆𝑡−2𝑔(𝑡,𝜆>0). Two positive triples are connected by such a coordinate change exactly when their values of 𝑝2𝑔/𝑙 agree. For the reverse implication, take 𝑡=𝑝′/𝑝 and 𝜆=𝑙′/𝑙. Equality of the two monomials gives 𝑔′=𝑙′𝑙(𝑝𝑝′)2𝑔=𝜆𝑡−2𝑔, which is the transformed acceleration. A ternary relation𝑅 on positive triples assigns a truth value to each triple; a unary predicateΦ on ℝ>0 assigns a truth value to each positive scalar. The relation 𝑅 is invariant under every such coordinate change exactly when some Φ satisfies 𝑅(𝑝,𝑙,𝑔)⟺Φ(𝑝2𝑔/𝑙). For the small-angle pendulum model, the additional physical law is 𝑝=2𝜋√𝑙/𝑔, so 𝑝2𝑔/𝑙=4𝜋2. Square root and the constant 𝜋 belong to that physical model, not to the integer-power source calculus.
Fix one rigid set Gr. Suppose the parameters introduced by 𝜌𝐸 are fresh for vars(𝐸∪𝐹), 𝜌𝐸 is principal among Gr-relative solutions of 𝐸, and after applying it, 𝜌𝐹 is principal among relative solutions of 𝐹[𝜌𝐸] that still fix Gr. Then 𝜌𝐸;𝜌𝐹 fixes Gr and is principal for 𝐸∪𝐹, with factorization equality on the ordered list of all nonrigid dimension variables occurring in 𝐸∪𝐹.
Proof of Lemma 6.15 — Composing principal dimension solves
Proof. Alpha-rename the finitely many parameters of 𝜌𝐸 away from vars(𝐹) before forming the second problem. The renaming preserves the equations and the factorization clause of theorem 6.13. The composite fixes the common rigid set. It solves 𝐸 because every substitution composed after 𝜌𝐸 that fixes Gr preserves its equations, and it solves 𝐹 because 𝜌𝐹 solves the image system. Let 𝜃 be a Gr-relative solution of 𝐸∪𝐹. Principality for 𝐸 factors 𝜃=vars(𝐸)𝜌𝐸;𝜓; extend 𝜓 to agree with 𝜃 on variables occurring only in 𝐹. Then 𝜓 solves 𝐹[𝜌𝐸], so it factors through 𝜌𝐹. Composing the two factors gives the required factorization on the variables constrained by the second problem. If a fresh parameter 𝜂 introduced by 𝜌𝐸 does not occur in 𝐹[𝜌𝐸], then 𝜌𝐹 fixes it; extend the residual factor by 𝜂↦𝜂[𝜓]. If it does occur, the second factorization already determines the same image. This support extension preserves the first factorization and yields the required factorization through 𝜌𝐸;𝜌𝐹 on every variable of 𝐸∪𝐹. ◻
Principal inference
The inference algorithm is Algorithm W from chapter 3 with one larger type unifier and four arithmetic clauses. We state the delta exactly.
The judgment IU(Γ,𝑒)=(𝑆,𝜏) uses the variable, lambda, application, and let clauses of W. Its unifier 𝗆𝗎𝗇𝗂𝖿𝗒𝑃 takes a finite protected set 𝑃, a finite list of type equations, and a dimension store. It decomposes arrows and eliminates ordinary type variables as in chapter 3. Every equation 𝖭𝗎𝗆[𝑑]≐𝖭𝗎𝗆[𝑒] is placed in a list 𝐷 of dimension equations as 𝑑=𝐷𝑒. Let 𝐸 be the pending type equations. Decompose 𝐸 until no type equation remains, then solve 𝐷 once by 𝜌𝐷. Distinct type constructors clash. Thus a single unifier call uses one Smith reduction, while unifier calls made at different syntax nodes compose their answers in W’s ordinary left-to-right order. Lemma 6.15 proves that this incremental composition remains principal.
Explicitly, its essential clauses are shown with the fixed subscript 𝑃 suppressed: 𝗆𝗎𝗇𝗂𝖿𝗒(𝜏1→𝜏2≐𝜌1→𝜌2,𝐸;𝐷)=𝗆𝗎𝗇𝗂𝖿𝗒(𝜏1≐𝜌1,𝜏2≐𝜌2,𝐸;𝐷),𝗆𝗎𝗇𝗂𝖿𝗒(𝛼≐𝜏,𝐸;𝐷)=[𝜏/𝛼];𝗆𝗎𝗇𝗂𝖿𝗒(𝐸[𝜏/𝛼];𝐷)(𝛼∉ftv(𝜏)),𝗆𝗎𝗇𝗂𝖿𝗒(𝖭𝗎𝗆[𝑑]≐𝖭𝗎𝗆[𝑒],𝐸;𝐷)=𝗆𝗎𝗇𝗂𝖿𝗒(𝐸;𝐷,𝑑=𝐷𝑒),𝗆𝗎𝗇𝗂𝖿𝗒(;𝐷)=𝜌𝐷. An occurs check rejects the second clause when its side condition fails; distinct outer constructors clash. Eliminating an ordinary type variable applies its image to both the remaining type equations and every dimension expression nested inside them. Once the type list is empty, one call to definition 6.12 solves the accumulated store 𝐷.
Fresh ordinary type variables and fresh dimension variables come from two disjoint indexed supplies. Before a run, each counter is larger than the index of every variable in the input environment, term annotation, equation lists, and protected set 𝑃. Consequently no variable allocated by the run belongs to 𝑃. A Smith call consumes its free parameters from the dimension supply from left to right; later recursive calls receive the unused suffix. Thus generated variables are fresh for the complete input and are never reused by a sibling call. Recording both initial counters determines every provisional variable and every Smith parameter in an inference trace. The notation 𝗆𝗎𝗇𝗂𝖿𝗒𝑃(𝐸;𝐷) denotes this run with 𝑃 fixed before allocation. Calls written 𝗆𝗎𝗇𝗂𝖿𝗒(𝐸) below abbreviate 𝗆𝗎𝗇𝗂𝖿𝗒∅(𝐸;∅).
Write I for IU. The new synthesis clauses are:
𝑞@𝑢 returns (id,𝖭𝗎𝗆[𝑑𝑢]).
For 𝑒1+𝑒2, compute (𝑆1,𝜏1)=I(Γ,𝑒1),(𝑆2,𝜏2)=I(Γ[𝑆1],𝑒2),𝑈=𝗆𝗎𝗇𝗂𝖿𝗒(𝜏1[𝑆2]≐𝖭𝗎𝗆[𝛿],𝜏2≐𝖭𝗎𝗆[𝛿]) for fresh 𝛿, and return (𝑆1;𝑆2;𝑈,𝖭𝗎𝗆[𝛿[𝑈]]).
Multiplication and division use the same first two calls, then choose fresh 𝛿1,𝛿2 and compute 𝑈=𝗆𝗎𝗇𝗂𝖿𝗒(𝜏1[𝑆2]≐𝖭𝗎𝗆[𝛿1],𝜏2≐𝖭𝗎𝗆[𝛿2]). Return 𝑆1;𝑆2;𝑈 together with, respectively, 𝖭𝗎𝗆[𝛿1[𝑈]𝛿2[𝑈]]or𝖭𝗎𝗆[𝛿1[𝑈]𝛿2[𝑈]−1].
For 𝑒⟨𝑛⟩, compute (𝑆,𝜏)=I(Γ,𝑒), choose fresh 𝛿, and put 𝑈=𝗆𝗎𝗇𝗂𝖿𝗒(𝜏≐𝖭𝗎𝗆[𝛿]). Return (𝑆;𝑈,𝖭𝗎𝗆[𝛿[𝑈]𝑛]).
At let, solve the bound expression before generalizing every ordinary and dimension variable absent from the substituted environment.
Proof of Lemma 6.17 — Termination of dimension-aware unification
Proof. Use the lexicographic triple from lemma 3.24 on the type equation list: its number of ordinary type variables, its constructor count, and its length. Ordinary-variable elimination and constructor decomposition decrease the first or second component. Moving a numeric equation to 𝐷 strictly shortens 𝐸; although 𝐷 grows, the procedure never recurses on that store. When 𝐸 is empty, Smith reduction terminates by lemma 6.11 and returns or fails. These clauses exhaust the recursion. ◻
Fix a finite protected set 𝑃 before the unifier allocates any variable. Let 𝐸 be a finite list of well-sorted type equations and let 𝐷 be a finite list of dimension equations. A two-sorted substitution 𝑄 solves (𝐸;𝐷) when 𝜏[𝑄]=𝐷𝜐[𝑄] for every 𝜏≐𝜐 in 𝐸 and 𝑑[𝑄]=𝐷𝑒[𝑄] for every 𝑑=𝐷𝑒 in 𝐷. The procedure 𝗆𝗎𝗇𝗂𝖿𝗒𝑃 has the following properties.
If 𝗆𝗎𝗇𝗂𝖿𝗒𝑃(𝐸;𝐷)=𝑆, then 𝑆 solves (𝐸;𝐷).
If 𝑄 solves (𝐸;𝐷) and the procedure returns 𝑆, there is a two-sorted substitution 𝑅 such that 𝑄=vars(𝐸,𝐷)𝑆;𝑅.
The procedure reports failure if and only if (𝐸;𝐷) has no solution.
The returned substitution fixes every variable in 𝑃∖vars(𝐸,𝐷). The residual 𝑅 in item 2 can be extended so that the same factorization holds on vars(𝐸,𝐷)∪𝑃 without changing it on the problem variables.
Proof. Induct on the lexicographic termination measure of lemma 6.17. Maintain the stronger claim containing all four clauses.
Reflexive and arrow cases. A reflexive equation is deleted. An arrow equation is replaced by its domain and codomain equations. Structural type equality makes the old and new solution sets identical, so the induction hypothesis proves all four clauses.
Ordinary-variable elimination. Consider 𝛼≐𝜏 with 𝛼∉ftv(𝜏). Every solution 𝑄 satisfies 𝛼[𝑄]=𝜏[𝑄] and therefore factors through [𝜏/𝛼]: define the residual to agree with 𝑄 away from 𝛼. Substitution of [𝜏/𝛼] in the remaining equations is sound in both sorts because ordinary substitution acts structurally on every numeric type and leaves the dimension store sorted. Conversely, any solution of the substituted problem composed after [𝜏/𝛼] solves the original equation and the remaining list. The occurs-check failure is exact: a finite type tree cannot equal a proper subtree containing itself. Orienting 𝜏≐𝛼 to this case does not change the solution set.
Numeric collection. Replacing 𝖭𝗎𝗆[𝑑]≐𝖭𝗎𝗆[𝑒] by 𝑑=𝐷𝑒 in the store preserves the solution set by injectivity of the numeric type constructor. The induction hypothesis therefore applies to the shorter type list. Distinct outer constructors have no common instance by lemma 6.8, so constructor clash is exact failure.
Dimension store. When the type list is empty, theorem 6.13 gives soundness, exact failure, and factorization for the accumulated store. Its substitution is the identity off the store variables and its fresh parameters. Those parameters avoid 𝑃 because the initial dimension-supply counter is larger than every index occurring in 𝑃. This proves the support clause in theorem 6.18(4).
Composing the one-variable eliminations in their execution order with the final dimension substitution proves items 1–3 for the initial problem. For item 4, let 𝛾∈𝑃∖vars(𝐸,𝐷). The returned substitution fixes 𝛾, so extend 𝑅 by 𝛾[𝑅]:=𝛾[𝑄]. This changes no problem-variable image and gives 𝛾[𝑆;𝑅]=𝛾[𝑄]. Repeating the construction for the finite set 𝑃 proves protected factorization. ◻
Run inference on 𝜆𝑥.𝜆𝑦.(𝑥∗𝑦)+3@𝗆. The two binders receive provisional numeric dimensions 𝛿 and 𝜖. Multiplication synthesizes 𝖭𝗎𝗆[𝛿𝜖]; the literal synthesizes 𝖭𝗎𝗆[𝐿]; and the addition clause therefore hands the dimension solver the genuinely coupled equation 𝛿𝜖=𝐷𝐿. Its principal answer is the one-parameter family computed above. Thus the Smith solver is required by programs generated by the printed inference clauses; the worked algebra calculation is one instance of that general argument.
Infer 𝗅𝖾𝗍𝑠=𝜆𝑥.𝑥∗𝑥𝗂𝗇𝑠(3@𝗆). The lambda clause gives 𝑥 fresh ordinary type 𝛼. The multiplication clause introduces fresh dimension variables 𝛿1,𝛿2 and equations 𝛼≐𝖭𝗎𝗆[𝛿1],𝛼≐𝖭𝗎𝗆[𝛿2]. Eliminating 𝛼 leaves 𝛿1=𝐷𝛿2; its principal dimension solution sends both to one fresh parameter 𝜂. Thus the definition has monotype 𝖭𝗎𝗆[𝜂]→𝖭𝗎𝗆[𝜂2], and let-generalization gives 𝑠:∀𝜂.𝖭𝗎𝗆[𝜂]→𝖭𝗎𝗆[𝜂2]. The body instantiates 𝜂 at fresh 𝛿3. Since 3@𝗆:𝖭𝗎𝗆[𝐿], application generates 𝛿3=𝐷𝐿 and fixes its result to 𝖭𝗎𝗆[𝐿2]. Every variable named here is determined by the two supplies declared in definition 6.16.
The substitution order is now visible in every binary clause: the right operand sees Γ[𝑆1], and 𝑆2 acts on the left operand’s type before the common unifier is called. Omitting either action can lose a constraint on a variable inherited from Γ.
if Γ[𝑇]⊢s𝑒:𝜏′, then inference succeeds with (𝑆,𝜏), and there is a substitution 𝑅 such that 𝑇 and 𝑆;𝑅 agree on Γ while 𝜏′=𝐷𝜏[𝑅].
By lemma 6.7, the completeness clause also applies to declarative monotype typings. Consequently generalizing the inferred type gives a principal scheme.
Proof of Theorem 6.21 — Principal dimension inference
Proof. Prove soundness and protected completeness simultaneously by induction on the source term. The strengthened completeness statement quantifies over every finite protected set 𝑃 fixed before the induction begins. At each syntax node, invoke 𝗆𝗎𝗇𝗂𝖿𝗒𝑃 with this same 𝑃. Every generated variable avoids 𝑃, the target substitution, and the target derivation, while theorem 6.18(4) preserves the inherited images of variables in 𝑃. The algorithm stated in definition 6.16 is the instance 𝑃=∅.
Variable and abstraction. The variable clause instantiates its stored prefix with fresh variables. A target instance determines the residual images of exactly those variables; the two prefixes are first standardized apart. For an abstraction, assign its binder a fresh ordinary variable 𝛼 and apply the induction hypothesis to the body while protecting the image of the ambient protected set. The returned type is 𝛼[𝑆]→𝜏, and type substitution applied to the premise reconstructs both soundness and the target arrow.
Application. The recursive calls give Γ[𝑆1;𝑆2]⊢𝑒1:𝜏1[𝑆2],Γ[𝑆1;𝑆2]⊢𝑒2:𝜏2. The mixed unifier 𝑈 solves 𝜏1[𝑆2]≐𝜏2→𝛽 by theorem 6.18(1). Applying 𝑈 to both derivations and using App yields result 𝛽[𝑈] under Γ[𝑆1;𝑆2;𝑈]. For completeness, the first protected induction hypothesis factors the target through 𝑆1. Apply the second while also protecting ftv(𝜏1) and fdv(𝜏1); the target substitution then solves the application equation. Items 2 and 4 of theorem 6.18 factor it through 𝑈 without changing an inherited protected image.
Let. Soundness of the definition gives Γ[𝑆1]⊢𝑒1:𝜏1. Generalize exactly the ordinary and dimension variables absent from Γ[𝑆1], apply 𝑆2 to that declarative derivation, and combine it with the body induction hypothesis by Let. For completeness, standardize both quantified sorts away from the target derivation. The target instance determines their residual images, while the body induction hypothesis preserves the variables free in the substituted environment. No arithmetic premise is hidden in generalization.
Addition. Put Γ12𝑈=Γ[𝑆1;𝑆2;𝑈]. The recursive soundness derivations, after the indicated substitutions, have types Γ12𝑈⊢𝑒1:𝜏1[𝑆2][𝑈],Γ12𝑈⊢𝑒2:𝜏2[𝑈]. The exact equations solved by 𝑈 give 𝜏1[𝑆2][𝑈]=𝖭𝗎𝗆[𝛿[𝑈]]=𝜏2[𝑈]. Rule D-Add therefore derives the returned type. Conversely, inversion of a target D-Add derivation gives one dimension 𝑑′ for both operands. The two protected induction hypotheses make the residual target substitution solve the same mixed equations. The factorization clauses of theorem 6.18 produce the required residual after 𝑈.
Multiplication and division. The same context Γ12𝑈 types the operands at 𝜏1[𝑆2][𝑈]=𝖭𝗎𝗆[𝛿1[𝑈]],𝜏2[𝑈]=𝖭𝗎𝗆[𝛿2[𝑈]]. D-Mul and D-Div return, respectively, 𝖭𝗎𝗆[𝛿1[𝑈]𝛿2[𝑈]]and𝖭𝗎𝗆[𝛿1[𝑈]𝛿2[𝑈]−1]. In the reverse direction, inversion exposes the two target operand dimensions. Protected recursive factorization handles the recursive calls. Items 2 and 4 of theorem 6.18 factor the two numeric equations. The group-homomorphism laws preserve product and inverse in the result.
Power and literal. A literal returns its declared closed dimension. For power, recursive soundness followed by 𝑈 types the operand at 𝖭𝗎𝗆[𝛿[𝑈]], and D-Pow returns 𝖭𝗎𝗆[𝛿[𝑈]𝑛]. Target inversion and mixed-unifier factorization prove completeness; 𝑑𝑛[𝜌]=𝑑[𝜌]𝑛 identifies the result. These cases exhaust the term grammar. Structural recursion and lemma 6.17 terminate every call, and the protected claim at the empty set gives the theorem. ◻
★★☆ Trace inference for 𝗅𝖾𝗍𝑠=𝜆𝑥.𝑥∗𝑥𝗂𝗇𝗅𝖾𝗍𝑎=𝑠(3@𝗆)𝗂𝗇𝑠(2@𝗌). List the provisional ordinary type, every fresh dimension variable, the equations generated while typing the definition and both applications, their principal substitutions, and the generalized scheme for 𝑠. Record the monotype assigned to 𝑎 and the type of the whole term. Exhibit the two distinct instantiating substitutions used at the two variable occurrences of the term variable 𝑠.
The canonical numeric core represents every literal in canonical units. Its types are the source types. Let ⊙∈{+,∗,/}. Core terms, values, and call-by-value evaluation contexts are 𝑎::=𝑥∣――𝑞𝑑∣𝜆(𝑥:𝜏).𝑎∣𝑎𝑎∣𝗅𝖾𝗍𝑥=𝑎𝗂𝗇𝑎∣𝑎⊙𝑎∣𝑎⟨𝑛⟩∣𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋,𝑣::=――𝑞𝑑∣𝜆(𝑥:𝜏).𝑎,E::=[]∣E𝑎∣𝑣E∣𝗅𝖾𝗍𝑥=E𝗂𝗇𝑎∣E⊙𝑎∣𝑣⊙E∣E⟨𝑛⟩. Define the one-frame contexts by 𝐹::=[]𝑎∣𝑣[]∣𝗅𝖾𝗍𝑥=[]𝗂𝗇𝑎∣[]⊙𝑎∣𝑣⊙[]∣[]⟨𝑛⟩. A one-frame context exposes only the next error-propagation position; iterating these frames gives the evaluation contexts E. A core typing context Δ maps term variables to monotypes, never to schemes.
A core value is any term generated by 𝑣 in definition 6.22; in particular, a core numeric value is ――𝑞𝑑:𝖭𝗎𝗆[𝑑]. The error 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋 is not a value and has any result type; it records an ordinary arithmetic-domain failure, not a dimension mismatch: 𝑋Δ⊢𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋:𝜏C−ArithErr. The functional core has monomorphic variables and monomorphic let:
Δ(𝑥)=𝜏
Δ⊢𝑥:𝜏
C-Var
Δ,𝑥:𝜏⊢𝑎:𝜐
Δ⊢𝜆(𝑥:𝜏).𝑎:𝜏→𝜐
C-Lam
Δ⊢𝑎1:𝜏→𝜐Δ⊢𝑎2:𝜏
Δ⊢𝑎1𝑎2:𝜐
C-App
Δ⊢𝑎1:𝜏Δ,𝑥:𝜏⊢𝑎2:𝜐
Δ⊢𝗅𝖾𝗍𝑥=𝑎1𝗂𝗇𝑎2:𝜐
C-Let
Δ⊢――𝑞𝑑:𝖭𝗎𝗆[𝑑]
C-Lit
Δ⊢𝑎1:𝖭𝗎𝗆[𝑑]Δ⊢𝑎2:𝖭𝗎𝗆[𝑑]
Δ⊢𝑎1+𝑎2:𝖭𝗎𝗆[𝑑]
C-Add
Δ⊢𝑎1:𝖭𝗎𝗆[𝑑1]Δ⊢𝑎2:𝖭𝗎𝗆[𝑑2]
Δ⊢𝑎1∗𝑎2:𝖭𝗎𝗆[𝑑1𝑑2]
C-Mul
Δ⊢𝑎1:𝖭𝗎𝗆[𝑑1]Δ⊢𝑎2:𝖭𝗎𝗆[𝑑2]
Δ⊢𝑎1/𝑎2:𝖭𝗎𝗆[𝑑1𝑑−12]
C-Div
Δ⊢𝑎:𝖭𝗎𝗆[𝑑]
Δ⊢𝑎⟨𝑛⟩:𝖭𝗎𝗆[𝑑𝑛]
C-Pow
Core conversion is D-Conv with 𝑒 replaced by 𝑎. Core term substitution 𝑎[𝑣/𝑥] is the capture-avoiding named substitution of definition 1.58; it descends through every core constructor, alpha-renames either binder when required, and leaves type and dimension annotations unchanged.
The two functional roots are value restricted: (𝜆(𝑥:𝜏).𝑏)𝑣⟶𝑏[𝑣/𝑥],𝗅𝖾𝗍𝑥=𝑣𝗂𝗇𝑏⟶𝑏[𝑣/𝑥]. The arithmetic roots include ――𝑝𝑑+――𝑞𝑑⟶――――𝑝+𝑞𝑑,――𝑝𝑑1∗――𝑞𝑑2⟶―――𝑝𝑞𝑑1𝑑2, Division uses rational quotient and dimension 𝑑1𝑑−12 when 𝑞≠0; an integer power uses coefficient 𝑞𝑛 and dimension 𝑑𝑛 when the rational power is defined. In particular, exponent zero is total, including at zero: ――𝑞⟨0⟩𝑑⟶――11. Division by zero, and zero raised to a negative power, reduce respectively by ――𝑝𝑑/――0𝑒⟶𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋,――0⟨𝑛⟩𝑑⟶𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋(𝑛<0). Evaluation is call by value, and the one-frame rule 𝐹⟨𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋⟩⟶𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋 propagates the outcome outward exactly one frame at a time. Using 𝐹 rather than an arbitrary nonempty E prevents a nested error from having both a one-frame and an all-frames propagation step. The compatible closure is 𝐹⟨𝑎⟩⟶𝐹⟨𝑎′⟩ whenever 𝑎⟶𝑎′. Iterating frames yields exactly the full contexts E above. Write 𝑎⟶∗𝑎′ for the reflexive–transitive closure. A core normal form has no outgoing ⟶ step. A core neutral is a core term that is neither a core value nor 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋; a neutral normal form is both core neutral and a core normal form. A terminal outcome is a core value or 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋. For example, typing fixes both successful and failing calculations: ∅⊢――1𝐿+――2𝐿:𝖭𝗎𝗆[𝐿],――1𝐿+――2𝐿⟶――3𝐿, whereas ∅⊢――1𝐿/――0𝑇:𝖭𝗎𝗆[𝐿𝑇−1],――1𝐿/――0𝑇⟶𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋. The first result carries the dimension required by C-Add. The second records a zero-denominator failure at the result type required by C-Div; incompatible dimensions never reach this core because source typing rejects them. Write 𝑎⇓𝑜 when 𝑎⟶∗𝑜 and 𝑜 is a terminal outcome. A step relation is deterministic when one term cannot step to two different successors.
Source let-polymorphism cannot be copied into C-Let: substituting one core value for a variable used at two distinct monotypes would make preservation false. Elaboration therefore specializes source let-bound definitions before core evaluation.
For a two-sorted substitution 𝜃, let 𝑎[𝜃] apply 𝜃 to every core type and dimension annotation. A template environmentΘ matching Γ over Δ assigns to every declaration 𝑥:∀¯𝛼¯𝛿.𝜏0 and every well-sorted instance 𝜃 of its prefix a core term Θ(𝑥)(𝜃) such that Δ⊢Θ(𝑥)(𝜃):𝜏0[𝜃]. It also satisfies the two context-stability conditions ftv(Δ)⊆ftv(Γ),fdv(Δ)⊆fdv(Γ), where variables bound by a scheme prefix are not free in Γ. Hence if 𝑄𝗍 and 𝑄𝖽 are the variables generalized from a type over Γ, then 𝑄𝗍∩ftv(Δ)=∅,𝑄𝖽∩fdv(Δ)=∅. The judgment Γ∣Θ;Δ⊢U𝐷:𝑒:𝜏⇝𝑎 elaborates the displayed syntax-directed source derivation 𝐷. Its variable, literal, lambda, application, and let clauses are
The arithmetic clauses preserve their source constructor and recursively elaborate their premises. E-Let emits no runtime let: each E-Var node for 𝑥 in the finite derivation 𝐷2 emits the required annotated instance of 𝑎1. For a fixed derivation 𝐷, write elabU(𝐷) for its unique output up to alpha-renaming. This notation is deliberately derivation directed; an unannotated source lambda does not by itself determine a core annotation.
For example, a derivation of 𝗅𝖾𝗍𝑥=𝜆𝑦.𝑦𝗂𝗇𝗅𝖾𝗍𝑧=𝑥(1@𝗆)𝗂𝗇(𝜆𝑤.𝑥(1@𝗌))𝑧 uses 𝑥 at dimensions 𝐿 and 𝑇. Template expansion elaborates it to the following term, where 𝐼𝑑:=𝜆(𝑦:𝖭𝗎𝗆[𝑑]).𝑦: (𝜆(𝑤:𝖭𝗎𝗆[𝐿]).𝐼𝑇――1𝑇)(𝐼𝐿――1𝐿). The right application frame first selects the beta redex inside the argument. After that contraction, the outer and final annotated beta roots yield ⟶∗(𝜆(𝑤:𝖭𝗎𝗆[𝐿]).𝐼𝑇――1𝑇)――1𝐿⟶𝐼𝑇――1𝑇⟶――1𝑇. The two copies of the source identity have different core annotations, while every runtime binder remains monomorphic.
The simpler unit calculation elaborates as elabU(𝐷𝗌𝗎𝗆)=――1𝐿+――1𝐿⟶――2𝐿. Here 𝐷𝗌𝗎𝗆 is the unique syntax-directed derivation of 100@𝖼𝗆+1@𝗆 under the displayed unit environment. No cast is inserted between dimensions. Both literals already elaborate to the same canonical dimension and scale.
By the specialization semantics of this chapter, a closed syntax-directed derivation 𝐷 executes with outcome 𝑜 exactly when elabU(𝐷)⇓𝑜. Thus source 𝗅𝖾𝗍 binds a polymorphic template that is expanded at the finitely many E-Var nodes for its binder in 𝐷. In particular, a derivation of 𝗅𝖾𝗍𝑥=(1@𝗆)/(0@𝗌)𝗂𝗇2@𝗆 contains no use of 𝑥. E-Let emits ――2𝐿, so this derivation executes to ――2𝐿; it does not evaluate the unused division.
Proof. Induct on the elaboration derivation. E-Var is exactly the matching condition. E-Lit uses ―――𝑞𝑠𝑢𝑑𝑢:𝖭𝗎𝗆[𝑑𝑢]. In E-Lam, the trivial template for 𝑥 matches the monomorphic declaration 𝑥:𝜏, so the induction hypothesis and C-Lam apply. E-App follows from its two induction hypotheses and C-App. The arithmetic cases reproduce the group operation in their result annotation.
For E-Let, the context-stability conditions give 𝑄𝗍∩ftv(Δ)=∅,𝑄𝖽∩fdv(Δ)=∅. A subsidiary induction on the core typing derivation for 𝑎1 applies 𝜃 to every annotation and gives Δ⊢𝑎1[𝜃]:𝜏1[𝜃] for every instance 𝜃 of the two prefixes; the context is unchanged because their variables are absent from Δ. The extended source and template environments preserve the context-stability conditions: the new source scheme binds the prefix, while the core context is unchanged. Hence they match Γ,𝑥:∀𝑄𝗍𝑄𝖽.𝜏1 over Δ. The body induction hypothesis yields Δ⊢𝑎2:𝜏2, which is the emitted term and required result. ◻
Proof of Lemma 6.26 — Core substitution and numeric canonical forms
Proof. For item 1, induct on the first typing derivation after freshening binders away from 𝑥 and fv(𝑣). Dimension annotations are not term syntax and remain unchanged; every nonbinding constructor reapplies its rule to the induction hypotheses, while variable, lambda, and let use the capture-avoiding clauses.
For item 2, induct on the core typing derivation. Each rule is stable under sorted substitution; in C-Lam and C-Let the substitution acts on the binder’s monotype as well as on the body annotations. Normalized dimension equality is preserved by dimension substitution, so the conversion case reapplies C-Conv.
For item 3, normalize a final conversion and inspect the value grammar. Lambdas have arrow type and numeric literals have their displayed annotation; lemma 6.8 prevents conversion from changing either outer constructor. ◻
Every core term is a terminal outcome, a neutral normal form, or has exactly one decomposition 𝑎=E⟨𝑟⟩, where E is an evaluation context and 𝑟 is one of the following redexes:
Proof. Induct on the term. A variable is a neutral normal form; a numeral and an abstraction are values; and 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋 is a terminal outcome. For an elimination, the selected subterm and the next frame are fixed by outertermfirstselectionselectionafteraleftvalue𝑎1𝑎2𝑎1through[]𝑎2𝑎2through𝑣[]𝗅𝖾𝗍𝑥=𝑎1𝗂𝗇𝑎2𝑎1through𝗅𝖾𝗍𝑥=[]𝗂𝗇𝑎2value-letroot𝑎1⊙𝑎2𝑎1through[]⊙𝑎2𝑎2through𝑣⊙[]𝑎⟨𝑛⟩1𝑎1through[]⟨𝑛⟩powerroot. If the selected subterm has a decomposition, its unique context and redex lift through the listed frame. If it is 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋, the listed frame gives the unique one-frame propagation redex. A selected neutral normal form makes the outer term neutral and normal.
It remains to inspect values. In an application, a lambda operator gives the unique beta root; a numeral operator leaves a neutral normal form. A let with a value definition gives the value-let root. Addition gives a root exactly for two numerals with the same dimension annotation; multiplication and division give a root for two numerals; power gives a root for one numeral. The zero-denominator and negative-power cases select the error root, and every other numeric case selects its unique success root. Any remaining value shape leaves a neutral normal form. The listed outer constructors, frame positions, and root shapes are disjoint, so both decomposition and successor are unique. ◻
Proof of Theorem 6.28 — Core preservation and progress
Proof. First prove preservation for an arbitrary context Δ by induction on the step derivation, peeling any final C-Conv from the typing derivation before the corresponding case. Beta and value-let use lemma 6.26(1). The arithmetic roots preserve the following result annotations: rootresulttype――𝑝𝑑+――𝑞𝑑𝖭𝗎𝗆[𝑑]――𝑝𝑑1∗――𝑞𝑑2𝖭𝗎𝗆[𝑑1𝑑2]――𝑝𝑑1/――𝑞𝑑2𝖭𝗎𝗆[𝑑1𝑑−12]――𝑞⟨𝑛⟩𝑑𝖭𝗎𝗆[𝑑𝑛]. The two arithmetic error roots have the same types as their successful rows by C-ArithErr. For compatible closure, inversion fixes the type of the selected subterm; the induction hypothesis preserves that type, and the rule in the right column rebuilds the surrounding judgment: framerebuildingrule[]𝑎2,𝑣[]C-App𝗅𝖾𝗍𝑥=[]𝗂𝗇𝑎2C-Let[]+𝑎2,𝑣+[]C-Add[]∗𝑎2,𝑣∗[]C-Mul[]/𝑎2,𝑣/[]C-Div[]⟨𝑛⟩C-Pow. An error-propagation root has the surrounding result type by C-ArithErr. If the original typing ended in C-Conv, reapply C-Conv to the preserved premise.
Next prove progress by induction on the closed typing derivation. A variable case is impossible; numerals and lambdas are values; and 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋 is a terminal outcome. For an application, first use the operator induction hypothesis. An operator step lifts through []𝑎2, and an operator error propagates. If the operator is a value, use the argument induction hypothesis through 𝑣[]. When both are values, arrow canonical forms make the operator a lambda, so beta applies. A let evaluates its definition through its frame; a value exposes value-let, and an error propagates.
For binary arithmetic, inspect the left operand and then the right operand. An operand step lifts through its listed frame, and an error propagates. Numeric canonical forms make closed numeric values numerals. C-Add gives two numerals with the same annotation; C-Mul and C-Div give two numeral operands; C-Pow gives one. Division selects its success root when the denominator is nonzero and its error root when it is zero. Power selects the negative-zero error root exactly when 𝑞=0 and 𝑛<0, and otherwise selects its success root. These cases exhaust the typing rules.
Finally, suppose 𝑎⟶𝑎′ and 𝑎⟶𝑎″. Lemma 6.27 gives one evaluation position and one redex, and gives that redex one successor. Hence 𝑎′=𝑎″, so the relation is deterministic. Preservation gives every successor type 𝜏. ◻
Every syntax-directed derivation produced for a closed source term accepted by inference elaborates from the empty template environment to a core term that does not become stuck because two arithmetic operands have incompatible dimensions.
Proof of Corollary 6.29 — Source dimensional safety
Proof. Principal inference supplies the source derivation, and elaboration typing makes its empty-environment elaboration well typed. Core safety then classifies every reachable normal form as a value or 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋; neither is a dimension-mismatch stuck state. ◻
★☆☆ Elaborate 12@𝗂𝗇𝖼𝗁+1@𝖿𝗍 under scales 𝑠𝗂𝗇𝖼𝗁=127/5000 and 𝑠𝖿𝗍=381/1250, both at dimension 𝐿. Reduce the core sum and express the answer in canonical meters.
Choosing centimeters rather than meters as the canonical length unit changes the stored coefficient. It must not change the physical calculation.
The naive induction statement relates only numeric outcomes by 𝑞′=𝜒(𝑑)𝑞. At an application its induction hypotheses say separately that the function and argument terminate, but say nothing about how the function responds to a rescaled argument. Thus the first higher-order case already stops at 𝑓∼𝐴→𝐵𝑔,𝑎∼𝐴𝑎′⟸̸numericequalityalone. The repair is a binary, type-indexed comparison whose arrow clause tests every related input pair.
For this core, 𝖲𝖭(𝑎) means that there is no infinite chain 𝑎=𝑎0⟶𝑎1⟶⋯. Core reduction is finitely branching, so for 𝖲𝖭(𝑎), write 𝜈(𝑎) for the maximum length of a reduction from 𝑎, as justified by lemma 2.47. The core-neutral class of definition 6.24 includes redexes and stuck eliminations; in particular, an open neutral normal form need not be a terminal outcome. Consequently every core normal form is exactly a value, 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋, or a neutral normal form. The judgment 𝑎⇓𝑜 from definition 6.24 supplies the observations in the comparison below.
Write ⟨B⟩ for the free abelian group of closed dimensions generated by B. A scale character is a group homomorphism 𝜒:⟨B⟩→ℚ>0: concretely, choose one positive scale factor for each base dimension and extend multiplicatively. No representation-theoretic meaning of “character” is used. Unit environments U,U′ are 𝜒-coherent when they assign every unit the same dimension and 𝑠′𝑢=𝜒(𝑑𝑢)𝑠𝑢. The unit-change logical relation∼𝐴 is defined recursively on pairs of closed core values at each closed type 𝐴. Numeric values at closed dimension 𝑑 are related when ――𝑞𝑑∼𝖭𝗎𝗆[𝑑]――𝑞′𝑑 iff 𝑞′=𝜒(𝑑)𝑞. Define related outcomes at a closed type 𝐴 by 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋≈𝐴𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋,𝑣≈𝐴𝑣′iff𝑣∼𝐴𝑣′. At an arrow, the closed values 𝑓,𝑔 satisfy 𝑓∼𝐴→𝐵𝑔 when, for every pair of closed values 𝑎∼𝐴𝑎′, the two applications evaluate to related terminal outcomes: 𝑓𝑎⇓𝑜,𝑔𝑎′⇓𝑜′,𝑜≈𝐵𝑜′. There are no other ≈𝐴 pairs, and ∼𝐴 is defined only on closed core values. Thus the arguments themselves change coordinates; this is a relational arrow clause, not equality on the same point. Monomorphic core value environments are related componentwise at their closed context types.
Fix a closing substitution 𝜅 for Γ and related closing core value environments 𝛾,𝛾′. Template environments Θ,Θ′ are 𝜒-related at Γ[𝜅] over 𝛾,𝛾′ when, for every 𝑥:∀𝑄.𝜏0 in Γ and every closed extension 𝜃 of 𝜅 to 𝑄, the core terms Θ(𝑥)(𝜃)[𝛾] and Θ′(𝑥)(𝜃)[𝛾′] terminate at outcomes related by ≈𝜏0[𝜃]. This condition compares specialized code, not a single polymorphic runtime value.
Extend the STLC candidate family by retaining its arrow clause and replacing its atomic clause by 𝑎∈R𝖭𝗎𝗆[𝑑]⟺𝖲𝖭(𝑎)∧∀𝑞∈ℚ∀𝑑′.(𝑎⟶∗――𝑞𝑑′⟹𝑑′=𝑑), so reduction to an error is permitted, while a reachable numeral must carry the candidate’s dimension. A neutral normal form satisfies the implication vacuously. For every closed core type 𝐴, the resulting family has:
normalization: 𝑎∈R𝐴 implies 𝖲𝖭(𝑎);
one-step reduction closure: if 𝑎∈R𝐴 and 𝑎⟶𝑎′, then 𝑎′∈R𝐴;
neutral expansion: if 𝑛 is core neutral and every immediate reduct of 𝑛 belongs to R𝐴, then 𝑛∈R𝐴;
error membership: 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋∈R𝐴.
The family also admits the following core principal expansions. If 𝑣 is a value and 𝑏[𝑣/𝑥]∈R𝐵, then both (𝜆(𝑥:𝐴).𝑏)𝑣 and 𝗅𝖾𝗍𝑥=𝑣𝗂𝗇𝑏 belong to R𝐵. For a numeric root, if its contractum is in the result candidate and its operands are strongly normalizing, then the numeric redex is in that candidate.
Proof of Lemma 6.31 — Saturation for dimension-core candidates
Proof. Induct on the result type, proving the four clauses in their displayed order. At 𝖭𝗎𝗆[𝑑], normalization is the first conjunct. If 𝑎⟶𝑎′, then 𝖲𝖭(𝑎) implies 𝖲𝖭(𝑎′), and every reduction of 𝑎′ to a numeral is a tail of a reduction of 𝑎. This proves reduction closure.
For neutral expansion, suppose every immediate reduct of 𝑛 lies in the numeric candidate. Core syntax gives finitely many immediate reducts. Their reduction trees are well founded by the normalization clause, so adjoining the root 𝑛 proves 𝖲𝖭(𝑛). If 𝑛⟶∗――𝑞𝑑′, the sequence is nonempty because a neutral term is not a numeric introduction. Its first step reaches a candidate member, whose canonical-form implication gives 𝑑′=𝑑. The term 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋 is normal and reaches no numeral, which proves error membership at the numeric type.
At a closed arrow 𝐴→𝐵, choose a fresh variable 𝑧∈R𝐴. For a numeric 𝐴, it is strongly normalizing and has no reduction to a numeral; for an arrow 𝐴, its membership follows from the already-proved neutral expansion clause at that proper subtype. If 𝑒∈R𝐴→𝐵, then 𝑒𝑧∈R𝐵; an infinite reduction of 𝑒 would lift to one of 𝑒𝑧, proving normalization. Reduction closure follows by placing 𝑒⟶𝑒′ under application to an arbitrary member of R𝐴 and using reduction closure at 𝐵. For neutral expansion, fix 𝑎∈R𝐴. Because the operator is neutral rather than a value, call-by-value evaluation can only step the operator: every immediate reduct of 𝑛𝑎 is 𝑛′𝑎 with 𝑛⟶𝑛′. The premise gives 𝑛′∈R𝐴→𝐵, so the arrow clause gives 𝑛′𝑎∈R𝐵. The application 𝑛𝑎 is core neutral, and neutral expansion at 𝐵 admits it. For error membership, fix the same argument. The only immediate step of 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋𝑎 is one-frame propagation to 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋, which belongs to R𝐵 by the type induction hypothesis. Neutral expansion at 𝐵 and the arrow clause give 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋∈R𝐴→𝐵. Closed numeric and arrow types exhaust the candidate indices; candidates are not defined at an open type variable.
For beta principal expansion, both the abstraction and its value argument are values, so the only immediate reduct of (𝜆(𝑥:𝐴).𝑏)𝑣 is 𝑏[𝑣/𝑥]. The beta redex is core neutral; clause 3 admits it. A let with a value definition has only the corresponding substitution root 𝑏[𝑣/𝑥]. It too is core neutral with one immediate reduct in R𝐵, so clause 3 proves value-let expansion.
For numeric principal expansion, induct on the sum of the maximal reduction heights of the operands. An operand step lowers that sum and gives a candidate immediate reduct by the induction hypothesis. When the operands are the numerals required by the root, its immediate root reduct is the contractum assumed to belong to the result candidate. If the selected operand is 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋, the immediate propagation reduct is 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋, which belongs to the result candidate. Hence every immediate reduct belongs. The redex is core neutral, so clause 3 puts it in the candidate. This covers addition, multiplication, division, and power. ◻
Use the reducibility candidates of definition 2.39, lemma 6.31. Suppose Δ⊢𝑎:𝜏. Let 𝜅 be a well-sorted substitution that maps every free ordinary type variable of Δ,𝜏,𝑎 to a closed type and every free dimension variable there to a closed dimension. If 𝛾 maps each declaration 𝑥:𝐴 in Δ to a core term 𝛾(𝑥)∈R𝐴[𝜅], then 𝑎[𝜅][𝛾]∈R𝜏[𝜅].
Proof of Lemma 6.32 — Fundamental lemma for the dimension core
Proof. Fix 𝜅 and induct on the core typing derivation. Every candidate index below is the closed image of the displayed type under 𝜅. Variables use the corresponding component of 𝛾; to reduce clutter, write 𝐴 for 𝐴[𝜅] in candidate subscripts throughout the proof. Before an abstraction, alpha-rename its binder away from the free variables of every term in the finite range of 𝛾. Let 𝐿:=𝜆(𝑥:𝐴[𝜅]).𝑏[𝜅][𝛾]. To prove 𝐿∈R𝐴→𝐵, fix an arbitrary 𝑟∈R𝐴. Clause 1 of lemma 6.31 gives 𝖲𝖭(𝑟), so 𝜈(𝑟) is defined. Induct on 𝜈(𝑟) to prove 𝐿𝑟∈R𝐵. For every immediate step 𝑟⟶𝑟′, reduction closure gives 𝑟′∈R𝐴 and 𝜈(𝑟′)<𝜈(𝑟); the height induction therefore places the corresponding immediate reduct 𝐿𝑟′ in R𝐵. If at least one such step exists, these are all immediate reducts of 𝐿𝑟, and core neutral expansion places 𝐿𝑟 there.
Suppose instead that 𝑟 is normal. By the definition of core neutral, either 𝑟=𝑣 is a value, 𝑟=𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋, or 𝑟 is a neutral normal form. In the value case, the typing induction hypothesis for the body applies to the realizing environment 𝛾,𝑥↦𝑣, yielding 𝑏[𝜅][𝛾,𝑥↦𝑣]=𝛼𝑏[𝜅][𝛾][𝑣/𝑥]∈R𝐵. Core beta principal expansion admits 𝐿𝑣. If 𝑟=𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋, the sole step 𝐿𝑟⟶𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋, followed by error membership and neutral expansion, admits the application. In the remaining case 𝑟 is a neutral normal form. Call by value has neither an argument step nor a beta step, so 𝐿𝑟 is neutral and has no immediate reduct; neutral expansion admits it vacuously. These cases prove the arrow clause for 𝐿 without importing the compatible-beta argument of theorem 2.42.
For C-App, the two typing induction hypotheses give 𝑎1[𝜅][𝛾]∈R𝐴→𝐵 and 𝑎2[𝜅][𝛾]∈R𝐴. The defining arrow clause gives 𝑎1[𝜅][𝛾]𝑎2[𝜅][𝛾]∈R𝐵, which is exactly the substituted application. A numeric literal after 𝜅 is strongly normalizing, and its only reachable numeral is itself with the annotation displayed by C-Lit. Rule C-ArithErr uses the fact that 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋 belongs to every result candidate. A final D-Conv does not change the candidate: normalized dimensions related by =𝐷 are equal maps, and the conversion clauses preserve the type tree.
For C-Let, the definition induction hypothesis gives 𝑎1[𝜅][𝛾]∈R𝐴[𝜅]. Induct on the maximal reduction height of that closed definition. If it takes a step 𝑎1[𝜅][𝛾]⟶𝑎′1, then 𝜈(𝑎′1)<𝜈(𝑎1[𝜅][𝛾]); the height induction puts 𝗅𝖾𝗍𝑥=𝑎′1𝗂𝗇𝑎2[𝜅][𝛾] in the result candidate. This term is precisely the reduct selected by the let evaluation frame. If the definition is a value 𝑣, one-step reduction closure puts 𝑣∈R𝐴[𝜅]. The body induction hypothesis therefore applies with that value bound to 𝑥, and the let root contracts to the resulting candidate member. If it is 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋, one-frame propagation reaches the error in the result candidate. If the definition is a neutral normal form, the let then has no immediate reduct and belongs to the result candidate by neutral expansion. Thus every immediate reduct of the let term belongs to the result candidate, and neutral expansion admits the let term itself.
Consider C-Div as a representative partial-arithmetic case. For reducible 𝑎1 and 𝑎2, induct on the sum of their maximal reduction heights. A step in the left operand, or in the right operand after the left is a value, decreases that sum. At normal operands, an error in the selected position propagates. A neutral normal left operand makes the whole division neutral and normal. If the left operand is a value, a neutral normal right operand does the same; a right error propagates. It remains to consider two values. For numerals ――𝑝𝑑1 and ――𝑞𝑑2, the root cases are ――𝑝𝑑1/――𝑞𝑑2⟶⎧{
{⎨{
{⎩―――𝑝/𝑞𝑑1𝑑−12,𝑞≠0,𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋,𝑞=0. Numeric principal expansion applies in both cases. If either value is a lambda, the division has no root, so the outer term is neutral and normal; neutral expansion admits it vacuously. Hence 𝑎1/𝑎2∈R𝖭𝗎𝗆[𝑑1𝑑−12] in every case.
For addition and multiplication, the same height induction follows their left-to-right frames. A selected error propagates. Two numeral values expose a total root with result annotation 𝑑 for addition and 𝑑1𝑑2 for multiplication; numeric principal expansion concludes. A selected neutral normal operand, or a lambda in either numeric position, leaves a neutral normal outer term. For power, induct on its operand height. A numeral gives ――𝑞⟨𝑛⟩𝑑⟶⎧{
{
{⎨{
{
{⎩𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋,𝑞=0and𝑛<0,――11,𝑛=0,―――𝑞𝑛𝑑𝑛,otherwise. A lambda operand leaves a neutral normal form. Numeric principal expansion or neutral expansion therefore closes every added arithmetic clause. ◻
Proof of Lemma 6.33 — Termination of the dimension core
Proof. Apply lemma 6.32 with the empty environment and the unique empty closing substitution for the closed term. Membership in every candidate entails strong normalization. Follow the deterministic step relation until its strongly normalizing reduction tree reaches a normal form. Core progress says that this normal form is a value or 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋, and determinism makes it unique. Hence it is a terminal outcome reached by ⇓. ◻
Proof of Lemma 6.34 — One-step expansion of core evaluation
Proof. The evaluation of 𝑎′ is a finite sequence 𝑎′⟶∗𝑜 ending in a value or 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋. Prefix that sequence by 𝑎⟶𝑎′; transitivity of the reflexive-transitive closure gives 𝑎⟶∗𝑜 with the same terminal outcome. ◻
First define the scale erasure of a core term, written |𝑎|s: replace every annotated numeral ――𝑞𝑑 by the marker ――∙𝑑, and recurse through every other core constructor. Now define erases recursively on source and elaboration derivations. At a D-Lit node, replace the lookup premise U∗(𝑢)=(𝑠𝑢,𝑑𝑢) by (𝑢,𝑑𝑢). At every elaboration node whose conclusion ends in ⇝𝑎, replace that output by ⇝|𝑎|s; at E-Lit, also replace the lookup premise by (𝑢,𝑑𝑢). Retain every rule label, source term, source type, and other premise, and apply erases recursively to immediate subderivations. Two source/elaboration derivations are scale companions when erases(𝐷)=erases(𝐷′). For example, companion E-Lit nodes for 2@𝑢 may carry 𝑠𝑢=1 and 𝑠′𝑢=100 at dimension 𝐿, and therefore elaborate to ――2𝐿 and ―――200𝐿. Their erased nodes both retain (𝑢,𝐿) and conclude with ――∙𝐿.
Suppose scale-companion syntax-directed derivations 𝐷,𝐷′ elaborate as Γ∣Θ;Δ⊢U𝐷:𝑒:𝜏⇝𝑎,Γ∣Θ′;Δ′⊢U′𝐷′:𝑒:𝜏⇝𝑎′. Let 𝜅 close every free ordinary type and dimension variable, and let closing value environments 𝛾,𝛾′ be componentwise related at Δ[𝜅],Δ′[𝜅]. If Θ,Θ′ are 𝜒-related at Γ[𝜅] over 𝛾,𝛾′, then 𝑎[𝜅][𝛾] and 𝑎′[𝜅][𝛾′] terminate at outcomes 𝑜,𝑜′ with 𝑜≈𝜏[𝜅]𝑜′.
In particular, if scale-companion derivations 𝐷,𝐷′ derive ∅⊢𝑒:𝖭𝗎𝗆[𝑑], their two empty-environment elaborations either both produce 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋, or produce ――𝑞𝑑 and ――𝑞′𝑑 with 𝑞′=𝜒(𝑑)𝑞.
Proof. For item 1, induct on the number of nodes in the common tree erases(𝐷)=erases(𝐷′), pairing the two derivation nodes at each induction step and closing their annotations by 𝜅. Elaboration typing and lemma 6.26(2) type both closed terms, and lemma 6.33 gives their unique outcomes. For a literal, 𝑞𝑠′𝑢=𝑞𝜒(𝑑𝑢)𝑠𝑢=𝜒(𝑑𝑢)(𝑞𝑠𝑢), so the elaborated values are related. In an arithmetic rule, the operand induction hypotheses first give related terminal outcomes. If they are errors, one-frame propagation produces an error on each side. Otherwise they are related numeric values. Addition uses the same closed dimension 𝜅(𝑑) on both operands and distributes the common factor 𝜒(𝜅(𝑑)). Multiplication uses homomorphism: 𝜒(𝜅(𝑑1𝑑2))=𝜒(𝜅(𝑑1))𝜒(𝜅(𝑑2)); division and integer powers use the inverse and power laws. Positivity of every 𝜒(𝑑) gives 𝑞=0 iff 𝜒(𝑑)𝑞=0, so division by zero and a negative power of zero produce 𝖺𝗋𝗂𝗍𝗁𝖾𝗋𝗋 on both sides, never on just one. The variable case is the 𝜒-related-template hypothesis at the selected closed instance. For a lambda, choose related arguments 𝑣∼𝐴𝑣′. The body induction hypothesis under value environments extended by 𝑥↦𝑣,𝑣′ gives related terminal outcomes for the substituted bodies. Each application takes one beta step to its body substitution, so lemma 6.34 supplies the evaluations required by the arrow clause.
For application, matching operator or argument errors propagate through the same frame on both sides. When both operators and both arguments terminate at related values, the arrow clause gives related application outcomes.
For E-Let, fix any closed instance 𝜃 of its generalized prefix. Apply the definition induction hypothesis with the closing extension 𝜃 of 𝜅. Its two specialized definition terms terminate at related outcomes, so the extended template environments are 𝜒-related at the generalized source declaration. The body induction hypothesis then compares the emitted terms directly. No runtime let step is involved: E-Let has already specialized and expanded every use in the finite source derivation. These cases exhaust the elaboration rules and prove item 1. Item 2 applies item 1 with empty template, core, and value environments, then uses the numeric clause of ≈. ◻
For the same physical length, changing the canonical length scale from meters to centimeters uses 𝜒(𝐿)=100. The result ――2𝐿 therefore becomes ―――200𝐿, exactly the same physical quantity in the new coordinate system.
A module is a source component checked against an explicit import and export interface. Separate compilation elaborates each module without inspecting its clients; linking combines the compiled cores after checking that their interfaces agree. The theorem assumes one whole-program unit environment and compares two coherent elaborations of the same source. The type 𝖭𝗎𝗆[𝐿] records a dimension but not a module’s chosen canonical scale. Separate compilation therefore also requires the export interface to expose the scale map. Two modules with different maps may otherwise elaborate the same coefficient incoherently. A link-time coherence check, or a numeric type indexed by the chosen unit, can enforce this invariant.
The exact boundary
A dimension exponent records how physical units compose. It is not a count of how often a variable is used, a security level, or a logical predicate on a number. The expression 𝐿2 says “area” even when the program uses its input once; the dimensionless type 𝖭𝗎𝗆[1] contains both positive and negative numbers. Those observations prevent the dimension group from being silently reused as a resource or refinement judgment.
A transcendental primitive must consume a dimensionless argument. For example, a conservative extension would give Γ⊢𝑒:𝖭𝗎𝗆[1]Γ⊢𝗅𝗈𝗀(𝑒):𝖭𝗎𝗆[1]. Then 𝗅𝗈𝗀(3@𝗆) has no derivation because 𝐿≠𝐷1. The core grammar omits transcendental arithmetic so that its coefficient calculations remain exact; the dimension restriction is the part a larger numeric language should retain.
Two nearby numeric conventions also fall outside this calculus. First, every unit conversion above is multiplicative and therefore preserves zero and addition. An absolute-temperature conversion such as 𝑐↦𝑐+27315/100 is affine: it cannot be represented by any scale 𝑠𝑢, because 0𝑠𝑢=0. Temperature differences still fit the multiplicative fragment, but absolute temperatures need a distinct affine quantity interface. Second, powers have a fixed integer exponent. A generic square-root primitive would have to solve 𝜖2=𝑑; it is not defined for a dimension whose normalized exponent vector contains an odd coefficient. Accepting such an operation would require an explicit divisibility premise or a richer constrained type, not a silent extension of D-Pow.
The use of integer exponents is a modeling choice. With rational exponents, dimensions form a ℚ-vector space: square and higher roots of dimension expressions are always available, and Smith divisibility is replaced by ordinary rational rank. That extension models quantities such as amplitude spectral densities, but gives up the integer-exponent lattice ℤB and its integral failure certificates.
Finally, 𝖭𝗎𝗆[1] merges every dimensionless quantity. It cannot distinguish a pure ratio from an angle, cycle count, neper, or decibel. Radians are dimensionless in SI, while logarithmic units encode convention-dependent transforms of dimensionless ratios; preserving those distinctions requires nominal quantity tags in addition to dimensions. This is a deliberate boundary of the calculus, not a claim that the quantities are interchangeable.
★☆☆ Extend the source unit environment by 𝗄𝗀↦(1,𝑀), taking kilograms as the canonical source mass unit. Let 𝜒(𝐿)=100, 𝜒(𝑇)=1, and 𝜒(𝑀)=1000. Compute 𝜒(𝑀𝐿𝑇−2) and use theorem 6.35 to translate a value 3@𝗄𝗀𝗆𝗌−2 to coherent gram–centimeter–second coefficients. Verify the result directly from the three base factors.
★★☆ Run 𝗆𝗎𝗇𝗂𝖿𝗒 on the ordered store 𝛼≐𝖭𝗎𝗆[𝛿],𝛼≐𝖭𝗎𝗆[𝐿]. Record the ordinary-variable elimination and the residual dimension equation, then replay the returned substitution against both inputs. For an arbitrary solution 𝑅, exhibit a substitution 𝑇 satisfying 𝑅↾{𝛼,𝛿}=(𝜌;𝑇)↾{𝛼,𝛿}, where 𝜌 is the returned answer.
★★★ Run the Smith algorithm on 𝛿4𝜖6=𝐿2. Multiply the elementary column matrices to obtain 𝑉 with [46]𝑉=[20]. Derive the one-parameter principal solution from 𝑧=𝑉𝑧′, verify it by substitution, and explain why replacing the right side by 𝐿 fails the pivot divisibility test.
★★☆ Assume a proposed unit literal elaborates by 𝑞@𝑢↦――――𝑎𝑞+𝑏𝑑. Show that the elaboration preserves addition exactly when 𝑏=0. Then apply the calculation to Celsius-to-Kelvin conversion and explain why a type of absolute temperatures must be distinguished from a type of temperature differences.
★★★Practical project.dimension-inferencer Implement the dimension-expression normalizer and the inference delta of definition 6.16 with disjoint supplies, an ordered equation store, and replay of the returned substitution. Use three public entry points: solveDimSystem returns a solved substitution and unused supply or a divisibility certificate; inferW returns the inferred type, composed substitution, generated store, and both unused supplies; evalCore returns a numeral or aritherr. In artifacts/ch6-dimension-inferencer/, run kappa check, kappa test, kappa run, and kappa audit on corpus.kp.
The acceptance fixtures must infer ∀𝛿.𝖭𝗎𝗆[𝛿]→𝖭𝗎𝗆[𝛿2] for 𝗌𝗊𝗎𝖺𝗋𝖾, reduce 100@𝖼𝗆+1@𝗆 to the canonical coefficient 2 at dimension 𝐿, reject 3@𝗆+2@𝗌, solve and replay 𝛿2𝜖3=𝐿, and reject 𝛿2=𝐿 by the Smith divisibility test. Under 𝑦:𝖭𝗎𝗆[𝛿], infer the scheme ∀𝛼.𝛼→𝖭𝗎𝗆[𝛿] for 𝜆𝑧.𝑦: the environment variable 𝛿 must not be generalized. After the recursive calls for a binary term, record Γ[𝑆1], 𝜏1[𝑆2], and 𝑆1;𝑆2 before invoking the mixed unifier. Ship a check-clean mutation that accepts the odd pivot and show that kappa test rejects it. The bundled corpus checks normalization, simple divisibility, unit conversion, the empty-environment square example, and the mutation oracle. It reports the coupled row 𝛿2𝜖3=𝐿 as Unsupported and uses an empty-environment inference shortcut. Completing the project requires the Smith gcd-rebasing branch and environment-sensitive W calls described above.