Abstract
Numerical stability is a cornerstone of reliable scientific computing, yet verifying it remains a manual and error-prone task. We present Bean, a first-order functional language that leverages a graded coeffect system and linear types to automatically derive sound backward error bounds. By interpreting programs through the lens of categorical semantics, Bean provides the first compositional framework for backward error analysis, matching established theoretical bounds across a variety of linear algebra benchmarks while ensuring mathematical soundness.
Introduction
Floating-point arithmetic is inherently imprecise. Because computers represent real numbers with a finite number of bits, every elementary operation introduces a rounding error. In fields such as machine learning and scientific simulation, these tiny errors can accumulate, potentially rendering a mathematically sound algorithm useless in practice.
Numerical analysts typically evaluate these errors using two metrics:
- Forward Error: The distance between the computed result and the true mathematical answer.
- Backward Error: The smallest change required to the input such that the computed result becomes the exact answer for that perturbed input.
Backward error analysis (BEA) is considered the "gold standard" for determining numerical stability. An algorithm is stable if its backward error is small—meaning it provides the right answer to "nearly" the right problem. Despite its importance, BEA has historically been a manual exercise. Automated tools have struggled with BEA because it is traditionally non-compositional; knowing the stability of individual components does not easily translate to the stability of the whole program.
The Bean Language
Bean is a first-order functional language designed to make BEA compositional and automatable. It achieves this through three primary technical innovations:
Graded Coeffect System
Bean’s type system tracks "grades" on every variable. These grades are quantitative bounds representing the maximum relative perturbation (backward error) allowed for a specific input. This allows the compiler to reason about how errors propagate backward from the output to the inputs.
Strict Linearity
To solve the composition problem, Bean enforces a linear type system. In standard programming, variables can be duplicated freely. However, in BEA, if a variable is used twice, the perturbations required to justify the output might conflict. By forcing linearity (or requiring explicit "discrete" conversion), Bean ensures that error flows are tracked soundly and without ambiguity.
Backward Error Lenses
Every Bean program is interpreted through a novel categorical semantics based on "lenses." A program is viewed as a triple: the ideal mathematical function, the actual floating-point implementation, and a "backward map" that serves as a witness to the existence of the input perturbation.
Evaluation and Benchmarks
The authors evaluated the Bean prototype (implemented in OCaml) using a suite of classic numerical linear algebra benchmarks, including:
- Basic Operations: Dot product and summation.
- Linear Algebra: Matrix-vector multiplication (up to $50 \times 50$ matrices) and triangular linear solvers.
- Polynomials: A comparison between "Naive" evaluation and "Horner’s Method."
The results were compared against the theoretical worst-case bounds established in standard texts, such as Nicholas Higham’s Accuracy and Stability of Numerical Algorithms.
Key Insights
Compositionality via Linearity
The most significant contribution of Bean is proving that backward error analysis can be automated if the language enforces linear types. This allows developers to build stability proofs for large, complex programs by composing small, stable primitives.
Stability vs. Accuracy
Bean reinforces the principle that a stable algorithm is often more desirable than an accurate one. Because a stable algorithm's errors are indistinguishable from noise in the input data, the implementation details become effectively invisible to the final error analysis.
Auditing Horner’s Method
While Horner’s method is the standard for efficient polynomial evaluation, Bean’s fine-grained analysis revealed that it can actually incur higher backward error for high-order coefficients compared to naive evaluation. This demonstrates Bean's utility in performing deeper, more nuanced stability audits than are typically feasible with manual analysis.
Results and Performance
- Theoretical Alignment: For all standard benchmarks, Bean’s automatically inferred bounds exactly matched the worst-case theoretical bounds found in literature.
- Soundness: Unlike optimization-based tools that provide estimates, Bean provides a formal guarantee. If a program passes the type checker, the derived bound is mathematically certain.
- Scalability: The prototype scales linearly with the number of operations, capable of analyzing programs with 1,000 floating-point operations in approximately 10 seconds.
Limitations and Future Work
While Bean represents a significant step forward, several challenges remain. The language is currently first-order, lacking higher-order functions like map or fold. The strict linearity requirement, while powerful for proofs, can be restrictive for certain algorithms that reuse variables in complex ways. Additionally, the current framework handles componentwise relative error; future work aims to address "structured" backward error (e.g., ensuring a symmetric matrix remains symmetric under perturbation) and transcendental functions like sin or log.
Summary updated