A Semantics for Approximate Program Transformations

Edwin M. Westbrook, Swarat Chaudhuri

Abstract: An approximate program transformation is a transformation that can change the semantics of a program within a specified empirical error bound. Such transformations have wide applications: they can decrease computation time, power consumption, and memory usage, and can, in some cases, allow implementations of incomputable operations. Correctness proofs of approximate program transformations are by definition quantitative. Unfortunately, unlike with standard program transformations, there is as of yet no modular way to prove correctness of an approximate transformation itself. Error bounds must be proved for each transformed program individually, and must be re-proved each time a program is modified or a different set of approximations are applied. In this paper, we give a semantics that enables quantitative reasoning about a large class of approximate program transformations in a local, composable way. Our semantics is based on a notion of distance between programs that defines what it means for an approximate transformation to be correct up to an error bound. The key insight is that distances between programs cannot in general be formulated in terms of metric spaces and real numbers. Instead, our semantics admits natural notions of distance for each type construct; for example, numbers are used as distances for numerical data, functions are used as distances for functional data, an polymorphic lambda-terms are used as distances for polymorphic data. We then show how our semantics applies to two example approximations: replacing reals with floating-point numbers, and loop perforation.

Introduction

In the pursuit of computational efficiency, developers frequently employ "approximate" techniques. These include replacing high-precision real numbers with hardware-efficient floating-point values, utilizing lossy data compression, or "perforating" loops by skipping iterations. While these optimizations can significantly reduce resource consumption—such as execution time and energy usage—they fundamentally alter the program's semantics.

The primary obstacle to the widespread adoption of these techniques is the difficulty of verification. Proving that an approximate program remains within acceptable error bounds is currently a "monolithic" task. Any minor modification to the program often necessitates a complete re-verification of the error bounds from scratch. There is a distinct lack of modularity; we cannot easily compose proofs of correctness for individual components to reason about the whole system. This research addresses this gap by providing a mathematical foundation for reasoning about these trade-offs locally and compositionally.

Methodology: The $F^{ADT+}$ Calculus

The authors propose a formal semantic framework based on an extension of System F (a standard polymorphic lambda calculus), dubbed $F^{ADT+}$. This language is designed to support algebraic datatypes and potentially incomputable operations, which are essential for modeling exact real-number specifications.

The core innovation of this framework is the approximation type. Rather than a standard type that merely describes a value's structure, an approximation type is defined as a "ternary logical relation." This relation links three distinct entities:

  1. The Exact Expression ($e$): The ideal mathematical specification.
  2. The Approximate Expression ($a$): The optimized, real-world implementation.
  3. The Error Expression ($q$): A program that quantifies the distance between $e$ and $a$.

By formalizing these relations, the authors derive a set of compositionality rules. These rules function as a "calculus of errors," enabling a verifier to automatically determine the total error of a complex program by aggregating the known errors of its constituent modules.

Formal inference rules for the $F^{ADT+}$ calculus, enabling compositional error derivation.

Key Insights: Errors as Programs

A pivotal realization in this work is that for higher-order data, such as functions, error cannot be represented as a static scalar value. Instead, the error itself must be a program. For instance, if one approximates the sin(x) function with a simpler identity function $\lambda x.x$ near zero, the resulting error is not a fixed number; it is a function of both the input value $x$ and any error already present in $x$.

Conceptual diagram of error propagation when approximating a sine function with an identity function.

This approach enables local reasoning. A developer can prove a specific transformation—like the sin approximation—correct once in isolation. This proof remains valid regardless of the context in which the function is eventually used. Furthermore, this semantics can relate programs across different data types, such as a specification using infinite-precision Real numbers and an implementation using 32-bit Float values.

Results and Applications

The framework's utility is demonstrated through several key results:

  • Soundness: The authors provide mathematical proofs ensuring that if the system derives an error bound $q$, the actual distance between the exact and approximate results will never exceed $q$.
  • Higher-Order Analysis: The framework enables what is believed to be the first floating-point error analysis capable of handling higher-order functions and polymorphic programs.
  • Unified Reasoning: The research shows that disparate approximation techniques—ranging from numerical floating-point conversion to structural changes like loop perforation—can be analyzed using the same underlying mathematical machinery.

Limitations and Future Directions

While the framework provides a robust foundation, several areas for improvement remain. The current implementation for floating-point analysis relies on interval arithmetic, which can produce overly conservative error bounds. Integrating more precise methods, such as affine arithmetic, is a potential future direction. Additionally, the current work focuses on deterministic "hard" error bounds. Extending the semantics to handle probabilistic approximations—where a result is "mostly" correct—is a significant area for future research. Finally, while the rules for composition are defined, the engineering challenge of building a fully automated tool for large-scale, real-world codebases persists.

Summary updated

BibTeX
@inproceedings{westbrook2013semantics,
  author = {Edwin M. Westbrook and Swarat Chaudhuri},
  title = {{A Semantics for Approximate Program Transformations}},
  booktitle = {DROPS},
  year = {2013},
  doi = {10.4230/lipics.csl.2021.23},
}
Figure