@certik I imagine the workflow as follows:
1. Start with the existing Fortran implementation
function legendre_poly(n, x) result(p)
! Declarations omitted
a = 1.0
b = x
if (n == 0) then
p = a
return
end if
do k = 2, n
c = ((2*k-1)*x*b - (k-1)*a)/k
a = b
b = c
end do
p = b
end function
2. AI generates a mathematical specification in Lean
The exact Legendre polynomials satisfy
P_0(x)=1,\qquad P_1(x)=x,
P_{n+1}(x)=\frac{(2n+1)xP_n(x)-nP_{n-1}(x)}{n+1}.
Suppose we want an absolute error below 10^{-12} for n\leq100 and x\in[-1,1].
-- P is the exact Legendre polynomial over ℝ.
-- Float64 models IEEE double precision (schematic API).
def LegendreSpec (f : Nat → Float64 → Float64) : Prop :=
∀ n x,
n ≤ 100 →
x.isFinite →
|x.toReal| ≤ 1 →
|(f n x).toReal - P n x.toReal| ≤ (1 : ℝ) / 10^12
This is the part the human reviews and approves: the mathematical definition, supported inputs, and required accuracy. The tolerance is only a proposed requirement, not a bound already proved.
3. LFortran translates the actual implementation into Lean
The generated representation might look approximately like this:
-- Schematic: the real translation must model rounding and conversions.
def legendre_fortran (n : Nat) (x : Float64) : Float64 := Id.run do
let mut a := 1.0
let mut b := x
if n == 0 then return a
for k in [2:n+1] do
let c := ((2*k-1)*x*b - (k-1)*a)/k
a := b
b := c
return b
Crucially, this representation comes from the actual Fortran code, rather than a second implementation invented by AI. A verified translator would need to preserve Fortran semantics, including floating-point rounding.
4. AI generates a proof; Lean independently checks it
We could build on an IEEE floating-point formalization such as FloatSpec. Conceptually, an AI-generated proof might have this structure:
import FloatSpec
theorem legendre_correct :
LegendreSpec legendre_fortran := by
unfold LegendreSpec
intro n x hn hfinite hx
-- Apply formal bounds for the rounding of each operation.
have h_rounding : ... := by
...
-- Propagate the bounds through the recurrence by induction.
have h_error : ... := by
...
-- Show that the resulting bound is ≤ 10⁻¹².
...
For example, under suitable assumptions, a standard rounding-error model is
\operatorname{fl}(a\circ b)=(a\circ b)(1+\delta),
\qquad |\delta|\leq2^{-53}.
Writing e_k=|\widehat P_k(x)-P_k(x)|, the proof could derive an inequality of the form
e_k\leq\frac{2k-1}{k}|x|e_{k-1}
+\frac{k-1}{k}e_{k-2}+\varepsilon_k,
where \varepsilon_k bounds the new rounding errors. The AI would try to prove that these errors stay within the approved tolerance for all permitted inputs; Lean would check the proof, not just run sample tests.
The Lean snippets are deliberately simplified and are not a completed, executable proof. In particular, the numerical bound must be established for the actual arithmetic model, including overflow, underflow, and evaluation rules; if it cannot be proved, the result is not proven.
The key idea: Humans review the specifications, AI generates the proofs, and Lean checks whether the actual Fortran implementation satisfies those specifications. The hard foundational task is a semantics-preserving Fortran-to-Lean translator and a suitable floating-point proof library and that infrastructure could then be reused for existing Fortran libraries.