Lectures onType Theory
Dimension types and units of measure
appendix sectionrules

Dimension types and units of measure

Dimension expressions are finite-support exponent maps d:BDZ. Multiplication, inverse, and integer power are pointwise addition, negation, and scalar multiplication. Compound units and source types are u::=u0uuu1un,τ::=αNum[d]ττ,σ::=α¯δ¯.τ. The unit environment gives primitive units positive rational scales and closed dimensions and extends homomorphically to all four unit forms.

Ordinary and dimension variables are disjoint sorts. Formation is

αA
A;Dα type
F-TVar
d:BDZ finitely supported
A;DNum[d] type
F-Num
A;Dτ1 typeA;Dτ2 type
A;Dτ1τ2 type
F-Arrow

Type equivalence extends dimension equality structurally, and conversion is Γe:ττ=DτΓe:τDConv. The source terms and arithmetic rules are e::=xq@uλx.eeelet x=e in ee+eeee/een,

U(u)=(su,du)
ΓUq@u:Num[du]
D-Lit
ΓUe1:Num[d]ΓUe2:Num[d]
ΓUe1+e2:Num[d]
D-Add
ΓUe1:Num[d1]ΓUe2:Num[d2]
ΓUe1e2:Num[d1d2]
D-Mul
ΓUe1:Num[d1]ΓUe2:Num[d2]
ΓUe1/e2:Num[d1d21]
D-Div
ΓUe:Num[d]
ΓUen:Num[dn]
D-Pow

For E=(di=Dei)i=1m, list unknowns δ¯=(δ1,,δn), put Mij=di(δj)ei(δj),ci=bBGrbei(b)di(b), and compute a Smith form UMV=S. With z=Vz and c=Uc, each pivot equation is (zi)si=ci. It succeeds exactly when si divides every generator exponent in ci; a zero row requires ci=1. Free columns become fresh dimension parameters, and back-substitution gives the principal substitution ρE. The deterministic policy chooses the first nonzero row-major pivot, uses canonical extended-Euclidean coefficients, repairs the first offending entry, makes pivots positive, and names free columns left to right from a globally fresh dimension supply.

The sorted type unifier munify decomposes arrows and ordinary variables as in HM and accumulates every Num[d]Num[e] of one call into one dimension store. The dimension-aware inference judgment is IU(Γ,e)=(S,τ). Besides inherited W, its exact binary clauses first compute (S1,τ1)=I(Γ,e1) and (S2,τ2)=I(Γ[S1],e2). Addition chooses fresh δ, computes U=munify(τ1[S2]Num[δ],τ2Num[δ]), and returns (S1;S2;U,Num[δ[U]]). Multiplication and division choose fresh δ1,δ2, solve the corresponding two numeric equations, and return S1;S2;U together with Num[δ1[U]δ2[U]]orNum[δ1[U]δ2[U]1]. Power computes (S,τ)=I(Γ,e), solves U=munify(τNum[δ]), and returns (S;U,Num[δ[U]n]).

The complete canonical core is a::=xqdλ(x:τ).aaalet x=a in aaaanaritherr,v::=qdλ(x:τ).a,E::=[]EavElet x=E in aEavEEn, where {+,,/}. Core contexts contain monotypes only; C-Var and C-Let are monomorphic:

Δ(x)=τ
Δx:τ
C-Var
Δ,x:τa:υ
Δλ(x:τ).a:τυ
C-Lam
Δa1:τυΔa2:τ
Δa1a2:υ
C-App
Δa1:τΔ,x:τa2:υ
Δlet x=a1 in a2:υ
C-Let

The core numeric rules are

Δqd:Num[d]
C-Lit
Δa1:Num[d]Δa2:Num[d]
Δa1+a2:Num[d]
C-Add
Δa1:Num[d1]Δa2:Num[d2]
Δa1a2:Num[d1d2]
C-Mul
Δa1:Num[d1]Δa2:Num[d2]
Δa1/a2:Num[d1d21]
C-Div
Δa:Num[d]
Δan:Num[dn]
C-Pow
Δaritherr:τ
C-ArithErr

The functional roots are (λ(x:τ).b)vb[v/x],let x=v in bb[v/x], with capture-avoiding substitution. Compatibility decomposes E into one-hole frames F: FrFr for a root step, and only Faritherr propagates in one step. Source elaboration is derivation directed. It inserts inferred lambda annotations and maps a generalized source-let declaration to a template θa[θ]; each variable instance emits the specialized template, so no polymorphic value reaches the runtime core. Literal elaboration is q@uqsudu; the arithmetic and zero-domain roots are exactly those displayed in the owning chapter.

Search the book

Type to search the local edition.