---
title: Dimensional Type System (DTS)
url: https://www.emergentmind.com/topics/dimensional-type-system-dts
type: topic
---

# Dimensional Type System (DTS)

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 [2308.09481][2603.16437].

## 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 \( D \), typically represented as integer-valued vectors over a chosen primitive basis (such as \([L, M, T]\) for mechanics). The set
\[
D = \{ L^a M^b T^c \mid a, b, c \in \mathbb{Z} \}
\]
admits group operations of componentwise addition and inversion:
\[
d_1 * d_2 = \mathrm{zipWith}(+)\,(\mathrm{toVec}\,d_1)\,(\mathrm{toVec}\,d_2), \qquad d_1 / d_2 = d_1 * d_2^{-1}.
\]
Special elements include $\mathit{DimLess} = [0,0,0]$, which is the dimensionless unit, and generator dimensions such as $\mathit{Length} = [1,0,0]$.

A physical quantity is encoded as a pair of a real value and a dimension tag:
\[
\mathbf{data}\;\mathit{Quantity}\;:\;\mathit{D}\;\to\;\mathit{Type}\;\mathbf{where}\\
\phantom{space}\mathit{MkQ}\;:\;\{\;d:D\}\;\to\;\mathit{Units}\;\to\;\mathbb{R}\;\to\;\mathit{Quantity}\;d
\]
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
\[
\Gamma;\;C;\;\kappa \;\vdash\; e:\tau
\]
where $\Gamma$ maps variables to dimension-indexed types, $C$ is a set of dimension constraints—homogeneous linear equations of the form $\sum_i n_i d_i = 0$, $n_i \in \mathbb{Z}$—and $\kappa$ is a coeffect context (discussed later).

Type inference proceeds by:
1. Generating fresh type and dimension variables for each subterm.
2. Accumulating constraints via program traversal: e.g., addition requires $d(e_1) = d(e_2)$, multiplication requires $d(res) = d(e_1) + d(e_2)$.
3. Solving the type constraints via conventional unification, and dimension constraints via integer Gaussian elimination.
4. Generalizing over unconstrained type and dimension variables for polymorphism.

Because the number of base-dimensions $k$ is fixed, constraint solving is polynomial-time and, in practice, linear with respect to program size [2603.16437]. 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
\[
\mathit{AreIndep} : \mathrm{Vect}\;k\;D \to \mathit{Type},\\
\mathit{IsDep} : D \to \mathrm{Vect}\;k\;D \to \mathit{Type}
\]
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 $\Pi = \tau^2 g / \ell$, with type-level proofs establishing its dimensionlessness [2308.09481].

## 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) [2603.16437]. Each PSG (Program Semantic Graph) node carries dimension metadata, paired type, value range, chosen numeric representation (e.g., $f64$, $posit<32,2>$), 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 $v$ with its dimension $d(v)$ and inferred value range $[a, b]$. Representation choices are optimized via error-minimization:
\[
r^* = \arg\min_{r \in R} \max_{x\in [a,b]} \frac{|x - \mathrm{round}_r(x)|}{|x|},
\]
where $R$ 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:
- $\mathit{StackScoped}$
- $\mathit{ClosureCaptured}(c)$ (captured in closure at node $c$)
- $\mathit{ReturnEscapes}$ (returned from function)
- $\mathit{ByRefEscapes}$ (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 [2603.16437].

## 6. Support for Dimensional Differentiation and Automatic Differentiation

Dimensional algebra in DTS is closed under differentiation: for $f: \mathbb{R}^{\langle d_1 \rangle} \to \mathbb{R}^{\langle d_2 \rangle}$,
\[
\frac{\partial f}{\partial x} : \mathbb{R}^{\langle d_2 d_1^{-1} \rangle}.
\]
The chain rule is dimensionally reflected by the group law:
\[
\dim(D(g \circ f)) = \dim(Dg) \cdot \dim(Df).
\]
Forward-mode automatic differentiation is supported as a coeffect: it requires no activation tape and only $\mathcal{O}(1)$ 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 [2603.16437].

## 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 [2603.16437].

---

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 [2308.09481][2603.16437].

Source: https://www.emergentmind.com/topics/dimensional-type-system-dts