Verifying Fortran code with Lean A proposal for verifying AI-generated Fortran code with the Lean theorem prover would have AI write both the implementation and a Lean proof checked independently, with humans approving only a mathematical specification. The worked example specifies a Legendre polynomial routine accurate to 1/10^12 for n ≤ 100 and x in [-1,1], with LFortran translating the actual Fortran into Lean and an AI-generated proof building on the FloatSpec IEEE floating-point formalization. The author cautions the Lean snippets are deliberately simplified and not a completed, executable proof, and that the numerical bound must hold for the actual arithmetic model including overflow, underflow and evaluation rules. 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. @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? 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: python 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 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. Wow I love this. This could revolutionize the way we handle software, no need to read code anymore, and it will bring programming back to its mathematical foundations. Many years ago I had a huge argument with the Julia community over using fast-math which rearranges order of operations for performance , they really didn’t like it because when the compiler does that, they can’t “know for sure” that the code still works, even if the original Julia code worked. And my argument was that you have to have tests and the code should be written in such a way that it’s immune to most rearrangement, and you really need this for performance. Doing a Lean proof might settle this argument for good. LFortran in fast-math mode will produce some assembly code, and if Lean can prove that it still satisfies the spec then there is no doubt about correctness for that particular platform. It would be good to generalize this to prove at a higher level in the compiler. For example, the above PR proves it at the level of the source code roughly , as the Lean backend in LFortran just translates the source code to our internal IR no lowering , called ASR, and then ASR → Lean. The fast-math rearrangements might not always be expressible as Fortran code, although I am not 100% sure. But I think there is probably a way to represent them as Lean code, and sometimes it might be platform independent, sometimes platform dependent. Either way, I think doing Lean proofs like this as much as you can is the way to go forward. For example I am planning to implement maximum performance intrinsic functions library sin, cos, erf, etc. , and in the past I just had Fortran tests for those but those are technically not 100% robust, as one cannot check all double precision numbers exhaustively, one can only do a subset , but now we should prove the accuracy with Lean, which proves it for all double precision numbers. Incredible The other question I have is why even bother with Fortran? Why not writing algorithms directly in Lean and have some certified compiler from Lean? I don’t know the answer, I am guessing Fortran and a good compiler can produce higher-performance result for now. One can consider writing the code in Lean and optionally translating to Fortran. Another question I have is regarding what subset of Fortran can be translated to Lean. Specifically, can it handle and prove things about more complex array-based code? Don’t know. But the fact that it could easily prove the legendre poly algorithm above it took about 20 min is very promising and totally usable for large classes of problems, like the intrinsic math function library above. Why not verify the final machine code directly? Instead of translating Fortran to Lean, we could translate the compiled assembly into Lean and prove that it satisfies the required numerical accuracy, even with -ffast-math. This would avoid needing to prove the compiler itself correct. We could use formal instruction-set models for x86-64, ARM64, and RISC-V, and verify each binary against the same mathematical specification. Both approaches are interesting, but Fortran already has optimized compilers and a huge ecosystem of existing scientific software. If we verify the compiled machine code directly, the original programming language becomes largely irrelevant. The same verification approach could work for Fortran, C, Rust, or any other language. In principle, any program with well-defined semantics can be represented mathematically, including arrays, loops, and memory operations. Good points. I suspect verifying the binary directly might be harder, since a well-optimized binary is larger and more complex than the original source code. But for simpler stuff it should be doable for sure, so we should do it. Consequently, I think there is value to formally verify each layer of the compiler: Original source code: already done above After every lowering or optimization pass: the compiler can generate an equivalent Lean code after the pass, and we redo the proof. This can go all the way down: ASR- ASR passes, LLVM IR, assembly and binary. The above verifications are for particular code and particular compiler output. It would be interesting if there is a way to verify that a particular compiler pass is always correct for all code. If so, then we simply verify the initial IR already done above and then by definition applying verified passes will produce code that is also verified. This might be possible for non-fast-math passes. The fast-math passes can break for non-robust code, but I still want them, so I guess for those one proves it for a particular code + passes. So possibly the answer to my discussion with the Julia guys might be that they only wanted compiler passes that can be in principle verified to work for all code non-fast-math and cannot break for non-robust code, while I also want passes that can only be verified for a particular code fast-math , but are known to break for non-robust code. The README.md in the PR above summarizes this thread in one consistent document, with examples and comparison to established approaches/literature. It seems the verification can work This is really cool It would be great if we could integrate performance analysis into a similar framework, covering computational complexity, arithmetic intensity, memory usage, and other performance characteristics. Ideally, we could formally verify both correctness and performance properties, which would dramatically speed up the code review process. After reading the first entry and thinking what an earth-shaking change is happening to traditional computing and what a fascinating idea but that it would take quite a bit of time and effort to try it and came back and a prototype has been constructed and people are already expanding on a revolutionary approach to having answers provided by computing. Tell me, was everything already in a state where this was an easier integration than I imagined or was AI used as a significant part in constructing the prototype. Either one is amazing, but I am curious. I remember long ago when I had not programmed much of anything wishing that I could just provide the computer something more in the syntax of a formula and/or proof and get a result and how actual programming and the realities of floating point math which I was new to at the time shattered that image. I basically see it brought together in a few hours standing on decades of work, but still rendered from concept to preliminary prototype in hours and am a bit stunned. So as well as telling you whether your input produces results within a specified error range it could also tell you what precision is required using that algorithm up to some limit, yes? Knowing my algorithm worked at 16 bits or required 128 bits would be useful.