Dimensional Type System (DTS)
- Dimensional Type System (DTS) is a formal framework that internalizes dimension algebra via free abelian groups to guarantee unit correctness.
- It extends Hindley–Milner type inference by solving dimension constraints using integer Gaussian elimination, ensuring statically verified operations.
- DTS preserves dimension metadata throughout compilation, guiding numeric representations, memory management, and enabling advanced diagnostics like automatic differentiation.
A Dimensional Type System (DTS) is a formal type-theoretic framework that internalizes the algebra of dimensions within a programming language’s type system. Its principal aim is to statically guarantee dimensional correctness of numerical programs, provide machine-checked support for dimensional analysis—including Buckingham’s Pi theorem—and propagate dimension metadata across the entire compilation chain, driving representation choice, memory layout, and program diagnostics. DTS generalizes the concept of physical units as types via the construction of types indexed by elements of a free abelian group and leverages these invariants both at type checking and throughout all stages of compilation (Botta et al., 2023, Haynes, 17 Mar 2026).
1. Algebraic Foundations and Type-Level Modeling
The core of a DTS is the internalization of dimension exponents as elements of a free abelian group , typically represented as integer-valued vectors over a chosen primitive basis (such as for mechanics). The set
admits group operations of componentwise addition and inversion: Special elements include , which is the dimensionless unit, and generator dimensions such as .
A physical quantity is encoded as a pair of a real value and a dimension tag: ensuring that every scalar value is dimension-indexed. Concrete operations (addition, multiplication, exponentiation) are statically checked and restricted via these dimension indices, e.g., addition is only permitted between quantities sharing the same dimension.
2. Inference Algorithm and Decidability
DTS extends Hindley–Milner type inference with constraints derived from finitely generated abelian groups. Typing judgments are of the form
where maps variables to dimension-indexed types, is a set of dimension constraints—homogeneous linear equations of the form , —and is a coeffect context (discussed later).
Type inference proceeds by:
- Generating fresh type and dimension variables for each subterm.
- Accumulating constraints via program traversal: e.g., addition requires , multiplication requires .
- Solving the type constraints via conventional unification, and dimension constraints via integer Gaussian elimination.
- Generalizing over unconstrained type and dimension variables for polymorphism.
Because the number of base-dimensions is fixed, constraint solving is polynomial-time and, in practice, linear with respect to program size (Haynes, 17 Mar 2026). Principality and completeness are maintained: there exists a most general dimension assignment for every well-typed program.
3. Static Dimensional Guarantees and Dimensional Analysis
DTS statically enforces unit-correctness: violations such as adding length and mass produce compile-time errors. Arithmetic manipulations on dimensions utilize the group structure, supporting full automation for compound unit calculations.
DTS natively encodes Buckingham’s Pi theorem. Independence and dependence among dimension vectors are formalized as
providing type-level proofs of Pi groups. The system verifies that dimensionless combinations and their exponents solve the necessary integer equations. For example, the period of a pendulum is shown to yield the Pi group , with type-level proofs establishing its dimensionlessness (Botta et al., 2023).
4. Compilation and Metadata Preservation
A distinctive property of DTS is the persistence of dimension annotations as codata throughout the entire compilation pipeline, particularly in multi-level IR lowering (e.g., MLIR) (Haynes, 17 Mar 2026). Each PSG (Program Semantic Graph) node carries dimension metadata, paired type, value range, chosen numeric representation (e.g., , ), word width, memory footprint, coeffect (allocation/lifetime), and cache locality information.
At each compilation stage—AST, PSG, MLIR, target IR, codegen—dimensions are retained as metadata, influencing optimization passes such as numeric representation selection and memory layout, but never altering program semantics. At the binary level, dimensions survive as debug metadata (e.g., DWARF-style tags), enabling post hoc static and runtime diagnostics.
5. Numeric Representation, Memory Management, and Coeffects
The DTS metadata informs numeric representation selection by associating each value with its dimension and inferred value range . Representation choices are optimized via error-minimization: where is the set of available target formats. Word width, memory footprint, and, where applicable, quire size (for posits) are derived from this information.
Deterministic Memory Management (DMM) is formalized as a coeffect system. Each value is classified into one of four allocation/lifetime categories:
- (captured in closure at node )
- (returned from function)
- (passed by reference)
This classification propagates via coeffect-typing rules, culminating in allocation strategies that are target-specific but statically checked. The DMM framework enables verified, garbage collection–free lifetime management, essential for high-assurance and embedded targets (Haynes, 17 Mar 2026).
6. Support for Dimensional Differentiation and Automatic Differentiation
Dimensional algebra in DTS is closed under differentiation: for ,
The chain rule is dimensionally reflected by the group law: Forward-mode automatic differentiation is supported as a coeffect: it requires no activation tape and only auxiliary memory per layer, which is statically verified by the PSG coeffect analysis. Reverse-mode (requiring activations) is treated with explicit heap allocation tracking. The same constraint machinery is used to type the derivative graph as for the forward pass (Haynes, 17 Mar 2026).
7. Design-Time Diagnostics and Tooling
The PSG's codata enables powerful, immediate design-time diagnostics:
- Escape diagnostics: Track and visualize escape paths, identify closure captures or returns, and propose refactorings.
- Allocation strategies: Bindings are annotated with stack/arena/heap or hardware region.
- Representation fidelity: Annotate cross-target transfers (e.g., "x86: float64; FPGA: posit32; fidelity=1.0, PCIe latency 100 ns").
- Cache locality: Estimate cache line residency and contiguity for hot loops.
- Automatic differentiation: Annotate memory requirements for forward vs. reverse mode.
These diagnostics are directly queryable from the PSG and require no separate analysis passes. The PSG thus serves as a unified substrate for both compilation and in-depth semantic feedback during development (Haynes, 17 Mar 2026).
DTS synthesizes a machine-checkable algebra of dimensions within the type system, extends classical type inference with abelian group constraints, and maintains dimension metadata as a first-class property throughout compilation. This infrastructure statically eliminates unit errors, automates dimensional analysis, bridges program semantics with numeric representation and memory management, and provides rich, design-time insight into numerical fidelity, allocation, and resource usage (Botta et al., 2023, Haynes, 17 Mar 2026).