Automatic Certified Differentiation in Lean
Ramy Shahin
cs.SC
Oct 26, 2026 · v1
cs.MS
TL;DR
Implements certified automatic differentiation via Lean metaprogramming, composing Mathlib derivative theorems into step-wise correctness certificates.
Abstract
Automatic differentiation (the automatic computation of numerical function derivatives) has many applications, especially in scientific computing and machine learning. Several approaches to automate the calculation of derivatives have been presented. Common challenges facing those approaches include (1) having to define derivative rules for different classes of functions, (2) having to manipulate program symbols to understand the semantics of the function being differentiated, and construct the derivative function, and (3), more importantly, ensuring that the calculated derivative is correct. In this paper, we present an approach for automatic certified differentiation using the Lean proof assistant. By certified, we mean a correctness certificate is computed together with the derivative function and a domain on which the derivative is guaranteed to be valid. Our approach leverages existing derivative theorems included in the Mathlib Lean library instead of having to define our own rules. We also utilize Lean metaprogramming to manipulate the symbolic structure of the input function, and to construct the derivative function. The correctness certificate is computed step-wise as we apply Mathlib theorems. To make the search efficient, we index the derivative theorems by the function they differentiate. We evaluate our approach on 500 randomly generated functions: with the index, all of them are differentiated, with a median time of 0.18\,s per function, 8.6 times faster than without the index.
Problem
Automatic differentiation tools usually need hand-written derivative rules, must manipulate program symbols to build the derivative function, and give no guarantee that the computed derivative is correct.
Approach
Lean metaprogramming manipulates the symbolic structure of an input function and constructs its derivative. Mathlib's existing derivative theorems are applied step-wise, so no custom rules are defined, and they produce a correctness certificate together with a domain on which the derivative is valid. Derivative theorems are indexed by the function they differentiate to make the search efficient.
Results
All 500 randomly generated test functions were differentiated, with a median time of 0.18 s per function using the index. This is 8.6 times faster than the same search without the index.