Verifying Fortran code with Lean

I think the long-term solution is formal verification using theorem provers like Lean. AI could write Fortran code and generate mathematical specifications describing what the code should do. A formally verified translator would convert the actual code into Lean, where AI-generated correctness proofs could be independently checked.

The key is that humans would only need to review and approve the specifications, rather than thousands of lines of AI-generated code, while Lean verifies that the implementation satisfies them.

1 Like

@Said-H I was wondering about how exactly to do this. Do you have an example in mind how this could work? Some very simplified “proxy”, so that we can see how it could work. Are you thinking we’ll take some Fortran implementation, say the “legendre_poly(n, x)” function, then we translate this very function to Lean’s def code (I am hoping we can have a backend in LFortran to do this automatically). Then what kind of theorems should we prove in Lean about this def legendre_poly code? I guess it would be that it satisfies the “Legendre polynomials” definitions (say from Lean’s Mathlib), as well as some numerical properties (say that it’s accurate to 1e-14 for n <= 100, or something like that)?

So the theorems (without proofs) as well as the signature of the def “implementations” would be part of the “spec” that humans approve? And we just have to fill in the proofs and the actual implementation of def, both using AI?

1 Like

@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.

2 Likes

@Said-H all right, here is a PR that implements all of this as a prototype:

It really works… Wow.