Your Code Has Bugs. Lean4 Has Proofs: Formal Verification for Engineers — Varun Pant, AWS
Formal verification provides mathematical proof that code matches its specification, and combining it with AI coding agents enables spec-driven development at scale.
Highlights
3 clips · 3 min of 10 min-
- Humans own the specification while machines generate code and proofs
- Specifications act as living artifacts upstream from implementation
- Formal verification tools mathematically prove code matches its spec for all inputs
-
- Lean uses tactics as moves to navigate a tree toward proving a theorem
- Interactive proof assistants allow backtracking when a tactical branch fails
- A small trusted kernel independently verifies the final proof
-
- Strata translates high-level intermediate representations from any language to a core IR
- A unified IR allows dispatching programs to Lean proof engines or SMT solvers
- Open-source tools bridge diverse programming languages to formal verification pipelines
That’s all the highlights.
Questions this video answers
9 · every answer cites the moment it was said01 Why aren't traditional testing and LLM judges enough for modern AI-generated code? 2 sources · 0:24
Traditional tests only check a subset of inputs and human code review cannot keep up with high agent speeds. Additionally, using an LM as a judge for code is probabilistic and cannot guarantee correctness for every single input.
02 How does formal verification differ from probabilistic testing? 2 sources · 0:51
Formal verification provides a mathematical proof that your code satisfies its specification for every possible input. Probabilistic testing or LLM judging only checks some inputs and cannot guarantee absolute correctness.
03 What is spec-driven development and how do AI coding agents fit into it? 2 sources · 1:19
In spec-driven development, humans write and validate the specification upstream, while AI coding agents implement the code from it. The formal verification tool then proves that the implementation matches the validated specification.
04 How does the Lean proof assistant work using the chess analogy? 2 sources · 3:27
Lean uses tactics as moves on a chessboard to prove a theorem goal by traversing a tree of possibilities. If a path fails, you backtrack and try another branch until a kernel independently confirms the final proof.
05 How was zlib formally verified using Lean? 2 sources · 4:41
An open-source project converted the zlib C compression library into Lean code. The AI generated a formal spec, wrote the code in Lean, decomposed the problem into helper lemma subgoals, and proved the theorem.
06 How does AWS use Lean for Cedar authorization policies? 2 sources · 6:15
AWS writes the functional specification of Cedar in Lean while the production code runs in Rust. They use differential random testing nightly to ensure both give the exact same output for the same inputs.
07 What is an SMT solver and how does it compare to an interactive proof assistant? 1 source · 7:17
An SMT solver is a powerful automated calculator where you feed in a formula and it returns whether it is satisfiable or unsatisfiable. Unlike an interactive proof assistant like Lean which functions like a chessboard with tactics, a solver computes the result automatically.
08 How does Verus use pre and post conditions in Rust code? 2 sources · 7:35
Verus allows developers to add requirements and specifications directly into Rust code using pre and post condition keywords. These static checks are enforced by the verifier and then erased at runtime like ghost code.
09 What is Strata and how does it support multiple programming languages? 2 sources · 8:32
Strata is an open-source tool being developed by AWS that lets you create custom compiler-like dialects for any programming language. It lowers high-level code into a low-level intermediate representation called Strata core so it can be dispatched to verification engines.
Section-by-section summary
2 parts
Mathematical Proofs Replace Probabilistic Testing
Traditional testing and human reviews cannot guarantee correctness across all possible inputs, but formal verification provides absolute mathematical proof. Using a specification-driven approach, developers define correct behavior upstream, and AI agents generate code that formal verification tools validate. Lean 4 acts as both a programming language and a proof assistant, leveraging interactive tactics and a small trusted kernel to verify theorems.
Applying Formal Verification to Production Code
Real-world projects demonstrate how formal verification can be applied to production languages like C and Rust using Lean specifications and differential testing. Tools like Verus and Eneus enable deductive verification using SMT solvers and pre- and post-condition annotations. Emerging frameworks like Strata aim to generalize this by lowering any programming language into a unified intermediate representation for automated verification.
n next · p previous clip · t highlights/full
< > slower/faster · / search what was said
-
Formal verification3× · first 0:42
Mathematical proof that code satisfies its specification for all inputs.
-
Lean12× · first 1:25
A programming language and interactive proof assistant used for definitions and proofs.
-
Tactics3× · first 3:06
Moves used in an interactive proof assistant to traverse a tree and prove a goal.
-
Cedar1× · first 6:06
An open-source authorization policy language used by AWS.
-
Solvers3× · first 7:03
Powerful automated calculators that take formulas and return satisfiable or unsatisfiable.
-
Verus1× · first 7:30
An open-source tool that uses the Z3 solver for static verification of Rust code.
-
Strata2× · first 8:34
An open-source tool allowing any programming language to be lowered into a core representation for verification.
1,461 words · about 6 min to read · YouTube’s automatic captions
Mathematical Proofs Replace Probabilistic Testing
Coding agents are generating more code than ever. Builders are generating hundreds and thousands of PRs every week. How do you know that this is correct? Using LM as a judge for the code? Well, that's probabilistic. Tests? They only check some inputs, not all. Human code review doesn't scale to match agent speed. None of these can say for all inputs the code is correct.
Formal verification can. Hi, I'm Varun Pant. I build AI products at AWS leading teams at in formal verification. Formal verification provides mathematical proof that code is correct.
1 Highlight · Spec-Driven Development with Formal Verification
For all inputs. You write what correct means, which is the specification, and a formal verification tool proves that your code satisfies it. If the proof passes, it holds for every possible input. How do you use this? Well, one way is back driven development, for example, with Kiro. You write what the specification is, which is what correct means.
Either you write it formally, for example, directly in Lean, or you write it in natural language, and you let the AI auto formalize it. Now, this is really important. You then validate the specification. So, either the human reviews it, or you test that it holds on some inputs. And this is important because the specification is upstream. It's a living, breathing artifact that the builder interacts with. You want this to be correct.
Everything else is downstream from this. The AI coding agent then goes and implements from the specification. And the formal verification tool proves that the implementation matches the specification. So, humans own the specification and machines own the code and proof. Lean is a programming language and a
proof assistant. It is the same language for the definitions and proofs. There's no translation layer. It is implemented in Lean, which means it's very extensible. And this is important. It has a small trusted kernel. Proofs can be exported and independently checked. So, here's an example of a Lean file which has both the code and proof in the same language. At the top, you'll see the code, which is a function that reverses a list in Lean. So, reverse of one, two, and three gives three, two, and one.
And right in the middle, you'll see a theorem. This is the proof. And this theorem prover approves a property which says that reverse of A plus B is in fact reverse of B plus reverse of A. And this holds for every possible input. How do you do this? You have something called as tactics which do the work, which we'll get to in a second. And the kernel, remember the small trusted kernel?
2 Highlight · Understanding Lean Proof Assistants Through Chess
That checks the work. A good analogy to understand the Lean proof assistant is that of chess. So, in chess, your goal is to checkmate the opponent. And you make a bunch of moves. You move the knight, you move the bishop. Similarly, in Lean, you have a bunch of tactics which are your moves. And it's the same chess board. It's interactive. You want to prove the goal, the theorem, checkmate. And you're kind of going down a tree. So, you're traversing the tree, you're trying different tactics. Maybe for some goals, you're not able to prove it, so you backtrack and then you try another a branch of the tree. Very similar to chess.
And finally, you get a goal that hopefully proves the theorem, and then that small independent kernel confirms and checks it. The kernel catches the mistake. So, here's an example at the top where an
incor- incorrect proof is rejected immediately. And you only need to trust the small kernel. The good thing is that you can have multiple independent kernels. You yourself can actually go write one. It's completely open source. You have kernels in C++, Rust, Lean. That's uh a link to
Applying Formal Verification to Production Code
the Arena Lang where you can go and add a kernel. So, let's look at some examples where you can put this to practice. The first one is having the specification and code both being in Lean. Now, this is open source Andreo. AI converted zlib, which is a C compression library, to Lean. Now, granted this happened over a week or so.
But, kind of going back to our specification methodology that we mentioned where you had specification at the top and then verification for the code, we'll kind of see the same thing here. So, the natural language specification says that you decompress the output of compress returning the original data. And then, you have an AI that generates the formal spec. Now, remember this is important. Checking the specification is key.
After you do that, the AI goes and writes your code in Lean, and then generates these helper lemma subgoals, and proves the theorem. And at the bottom, you can see that it's verified with that small independent kernel. So, what you just saw was that AI decomposed the problem into lemmas, which are subgoals. It proved each of them using tactics. Remember the chess moves that we were making?
And it assembled it into a final theorem. Checkmate. And the kernel checked it. And this particular example had 32,000 lines of proof. So, it was pretty big. Let's take another example. What if you have code in Rust? Well, you can write the functional specification of it or the model in Lean. An example of that is Cedar. Cedar is an open-source authorization policy language, which is used by AWS verified permissions and access.
The specification of Cedar is written in Lean. The production code runs in Rust. Why is this important? Because let's take an example. You have forbid Trump's permit. You want to make sure that for any forbid policy being satisfied, the request is always denied. This is key. Here you can see the example of what I was talking about, which is you have the Rust production code and you have the functional specification in Lean, and you run differential random testing to check that both of those for the same inputs give the same output.
And there's about 100 million differential random tests uh run nightly. No version ships until this is satisfied. Let's take another example. What if you have code in Rust and you want to deductively verify with Lean or solvers? Before we go there, let's quickly talk about this new term solvers. So, remember we spoke of Lean being this chessboard interactive where you're making a bunch of moves trying to checkmate.
A solver is a calculator, a very powerful one. You feed in a formula and it returns an output. In this case, satisfiable or unsatisfiable. So, an example of this is Verus, also an open-source tool. It uses this solver, this very powerful calculator, Z3. And if folks are familiar with adding annotations, it's kind of similar to that where you can add specifications in the form of that. And the code is in line. So, you see these two requires and ensure keywords, that's what we call a pre and post condition. What must be true before and what must be true after.
And this is a static check. It's enforced by the verifier and erased at runtime. So, almost like ghost code. Another example of this is Eneus, which uses the mid-level intermediate representation for Rust and does a functional translation to Lean. And right after that, you use the same theorem prover, the same chessboard that we spoke of.
Now, you may be asking, well, what if I have any programming language?
3 Highlight · Lowering Any Language to Lean with Strata
We at AWS have been working on an open-source tool called Strata. This is work in progress, but the idea is that you can have any programming language and you yourself can create what we call a dialect. Think of this like a compiler. You have a high-level intermediate representation and you lower it down to a low-level intermediate representation, which is what Strata core is. Now, this is written in Lean.
After you have all of these programs talking in the same language, which is the Strata core, you can dispatch it to any of the engines. For example, the Lean proof, remember the chessboard, or the very powerful calculator, SMT
solvers, or model checkers. So, you can get started with this today. You can go to Lean in in your browser with the link I pasted, and you can pick your most critical code, write what correct means, which is the specification, which is very important, and then you can let your coding agent implement it and your formal verification tool prove it.
So, hopefully in this brave new world, we have software and systems that are not probably correct, but probably correct. Thank you.


