arXiv:2610.36396cs.SCOct 2026

Manuscript Offprint

arXiv:2610.36396

Automatic Certified Differentiation in Lean

Ramy Shahin

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Ramy Shahin [0000−0001−8724−3934]^{[0000-0001-8724-3934]}

Qualgebra

ramy@qualgebra.com

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.

Keywords: automatic differentiation, certified programming, metaprogramming, Lean

1 Introduction

Given a mathematical function, computing its derivative has many practical applications. This includes applications in scientific computing, machine learning, simulation, graphics, and computational finance. The simplest approach is for a programmer to manually analyze the input function and write a program implementing the derivative function, following the differentiation rules in calculus textbooks. However, since those rules have been well studied and understood for centuries, automating this process has been an active area of research since the 1960s [28].

Approaches to computing derivatives automatically are usually classified into numerical, algorithmic, and symbolic differentiation [26,2]. Numerical differentiation treats f as a black box, and approximates its derivative at a given value

R. Shahin

log∣/Sx^\begin{array}{c} log\\ \mid\\ /\\ \widehat{\mathcal{S}x}\end{array}

(a) Syntax tree of log⁡(3/x)\log(3/x).

x≠03/x≠0divlog⁡′(3/x)=x/3deriv-logx≠0d(3/x)dx=−3/x2deriv-div\frac{\frac{x\neq0}{3/x\neq0}\text{div}}{\log^{\prime}(3/x)=x/3}\text{deriv-log}\quad\frac{x\neq0}{\frac{d(3/x)}{dx}=-3/x^{2}}\text{deriv-div} dlog⁡(3/x)dx=−1/x\frac{d\log(3/x)}{dx}=-1/x

deriv-comp

(b) Proof tree of the derivative of f(x)=log⁡(3/x)f(x) = \log(3/x).

Fig.1: Syntax tree of the body of f(x)=log⁡(3/x)f(x) = \log(3/x), and proof derivation of its derivative.

x by the rate of change of f between x and neighboring values. Algorithmic differentiation evaluates the program computing f at a given value x, and propagates derivative values through each elementary operation using the chain rule. It computes the exact value of the derivative at x, but not an expression for the derivative function.

On the other hand, symbolic differentiation is a white-box approach. It works directly on the syntactic expression structure of the function body without having to compute any function values. This is based on the fact that derivative rules are compositional. For example, the chain rule is used to compute the derivative of a composition of two functions (e.g., g(h(x))g(h(x))). Following the syntactic tree structure of the body of function ff, the derivative dfdx\frac{df}{dx} is composed using primitive derivative theorems.

For example, to differentiate the function f=log⁡(3/x)f = \log(3/x), we follow its syntactic structure (Fig. 1a) top-down. The root of the syntax tree is the unary function log, and its only child is the binary function (operator) /. This is a composition of two functions, and its derivative is computed using the chain rule. The two premises of the chain rule in this case are (1) the derivative of the outer function log applied to the inner function 3/x3/x, and (2) the derivative of the inner function 3/x3/x.

Using the logarithm derivative rule, the derivative of log, evaluated at 3/x, is 1/(3/x)=x/31/(3/x) = x/3. This rule only applies where the argument of log is non-zero, i.e., for 3/x≠03/x \neq 0, because the logarithm is not differentiable at zero. In Mathlib, division and the logarithm are total functions, with x/0=0x/0 = 0 and log⁡0=0\log 0 = 0, so side conditions like this one are about differentiability rather than definedness; since 3/0=03/0 = 0, the condition 3/x≠03/x \neq 0 holds exactly when x≠0x \neq 0.

Similarly, using the quotient rule, the derivative of 3/x3/x is (0⋅x−3⋅1)/x2=−3/x2(0 \cdot x - 3 \cdot 1)/x^2 = -3/x^2. The quotient rule requires the denominator to be non-zero, so again the derivative is restricted to x≠0x \neq 0. The derivative of ff now is the product of the two lower-level derivatives, according to the chain rule. This evaluates to (x/3)(−3/x2)=−1/x(x/3)(-3/x^2) = -1/x.

This approach is completely based on formally proved theorems. As a result, a correctness proof is (implicitly) constructed compositionally alongside the derivative function itself. Fig. 1b is the derivation tree of this proof. This tree captures the exact same steps used to compute the derivative. If this proof is made explicit, and returned by the automatic differentiation system together with the derivative function, then the derivative computation is certified, i.e., it computes a value, together with a certificate proving the correctness of that value.

To be able to build such a system, three ingredients are needed:

  1. A formal foundation defining the notion of derivatives down to basic mathematical and logical principles, together with differentiation rules for different forms of mathematical expressions.

  2. A way to validate the correctness of proofs built on top of that formal foundation.

  3. The ability to manipulate the symbolic structure of mathematical functions, and to apply the right rule to the right kind of expression.

The Lean [19] interactive theorem prover together with its Mathlib [5] library provides all three ingredients. (1) Mathlib is a large, community-maintained library of formalized mathematics, including analysis and calculus; its derivative theorems form exactly the required formal foundation. (2) The Lean kernel, which implements a variant of the Calculus of Inductive Constructions (CIC) [3], only admits well-typed terms, so it acts as a proof checker for Mathlib and for proofs built on top of it. (3) Lean supports metaprogramming: programs that manipulate other programs, such as symbolic differentiation, and that extend the syntax of Lean with new commands.

In this paper we present an automatic certified differentiation framework, leveraging the Lean theorem prover, Mathlib, and Lean metaprogramming. The correctness certificate returned together with the derivative function guarantees that the derivative is correct on the computed domain. This is particularly relevant when used in developing safety-critical or mission-critical software systems. Our contributions are:

– An algorithm for automatic certified differentiation that composes existing Mathlib derivative theorems following the structure of the input function, and returns the derivative, a domain on which it is valid, and a proof of its correctness, checked by the Lean kernel (Sec. 3).

– An implementation of the algorithm as a Lean command, including an index of derivative theorems by the function they differentiate, which makes the search efficient (Sec. 4).

– An evaluation on 500 randomly generated functions, all of which are differentiated, with a median time of 0.18s per function, 8.6 times faster than without the index (Sec. 5).

In particular, after covering some background on Mathlib derivative functions (Sec. 2), we present the automatic certified differentiation algorithm in Sec. 3.

R. Shahin

def HasDerivAt { k : Type u} [NontriviallyNormedField k] {F : Type v} [AddCommGroup F] [Module k F] [TopologicalSpace F] [ContinuousSMul k F] (f : k → F) (f' : F) (x : k): Prop (a) Definition of HasDerivAt.

theorem HasDerivAt.comp { k : Type u} [NontriviallyNormedField k] (x : k) { k' : Type u_1} [NontriviallyNormedField k'] [NormedAlgebra k k'] {h : k → k'} {h₂ : k' → k'} {h' h₂' : k'} (hh₂ : HasDerivAt h₂ h₂' (h x)) (hh : HasDerivAt h h' x): HasDerivAt (h₂ ○ h) (h₂' * h') x (c) Chain rule.

theorem HasDerivAt.log {f : R → R} {x f' : R} (hf : HasDerivAt f f' x) (hx : ¬ (f x = 0)) : HasDerivAt (fun (y : R) => Real.log (f y)) (f' / f x) x

(b) Derivative of log composed with f.

Fig. 2: Documentation of some derivative definitions and theorems from Mathlib.

We then cover the design of the library and how it integrates with Lean in Sec. 4. Evaluation results are then presented in Sec. 5, and discussed in Sec. 6. We then compare our approach to related work in Sec. 7, and finally conclude and discuss some future directions in Sec. 8. The library together with the evaluation driver and scripts is available in open source 1^{1}.