A Calculus for Units of Measure with Conversion
Eric Allen
Abstract
Programs that compute with physical quantities often need to convert between units of measurement, and as the Mars Climate Orbiter showed, these conversions can be a rich source of errors. Meanwhile, typed unit calculi, our most rigorous formalisms for checking physical units in a program, have excluded unit conversions. Through this exclusion, they have established a powerful property: well-typed programs are invariant under rescaling (no program can depend on how big a meter is). But losing the ability to convert between units is a significant cost. In contrast, practical languages provide conversion but no invariance theorem. We present ΛS, a typed lambda calculus with conversion, quantification over units and dimensions, and vectors and linear maps with per-component units, and we determine exactly how much invariance survives conversion. For terms without unit constants we prove two abstraction theorems: (i) a convert-free term is invariant under every rescaling; (ii) a term with conversions is invariant under every rescaling that scales all units of one dimension by the same factor. The condition in (ii) cannot be weakened: under any other rescaling, some conversion of a nonzero value is not invariant. For first-order programs, a verified decision procedure returns one of three verdicts: it certifies that no error in the declared conversion factors can change the program's answer, names the accumulated ratio through which such an error would scale it, or declines. A verified checker decides whether unit declarations are consistent and determine every conversion factor, and extracts each factor exactly. We also prove adequacy, erasure, and the n-variable Pi theorem of dimensional analysis with conversion. Every theorem is mechanized in Lean 4, and the evaluator compiles to a native binary.
Create a lesson
Related papers
Augmenting Rewrite Rule Sets via Knuth-Bendix Completion
Michael Schifferer, Marcel Ullrich, Sebastian Hack
Associativity and Commutativity in Equality Saturation
Tarik Rosin, Marcel Ullrich, Sebastian Hack
Fixing the Fixpoint: A Formal Theory of Convergence Detection for Incremental Recursive Computation
Chengxi Yang, Tej Chajed, Thomas Reps
Automatically Building Machine-Checked Assurance Cases from C Codebases to Requirements
Haokun Li, Zhongyi Wang, Guanyan Li et al.
Freely Generated Categorical Structures and Automatic Differentiation, PhD Thesis (Introduction and Conclusion)
Fernando Lucatelli Nunes
DatalogBench: Evaluating Large Language Models on Text-to-Datalog Synthesis
Yuan Li, Hanyun Jiang, Guowei Tian et al.