Your Code Has Bugs. Lean4 Has Proofs: Formal Verification for Engineers — Varun Pant, AWS https://www.youtube.com/watch?v=lRa9sPaMyy4 AI Engineer · 10 min · 137 sentences · sentence-level transcript from YouTube's caption track; each line begins at the time shown [00:01] [music] [00:12] Coding agents are generating more code than ever. [00:16] Builders are generating hundreds and thousands of PRs every week. [00:21] How do you know that this is correct? [00:24] Using LM as a judge for the code? [00:26] Well, that's probabilistic. [00:29] Tests? [00:30] They only check some inputs, not all. [00:33] Human code review doesn't scale to match agent speed. [00:37] None of these can say for all inputs the code is correct. [00:42] Formal verification can. [00:44] Hi, I'm Varun Pant. [00:45] I build AI products at AWS leading teams at in formal verification. [00:51] Formal verification provides mathematical proof that code is correct. [00:56] For all inputs. [00:57] You write what correct means, which is the specification, and a formal verification tool proves that your code satisfies it. [01:05] If the proof passes, it holds for every possible input. [01:11] How do you use this? [01:13] Well, one way is back driven development, for example, with Kiro. [01:19] You write what the specification is, which is what correct means. [01:23] 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. [01:32] Now, this is really important. [01:33] You then validate the specification. [01:36] So, either the human reviews it, or you test that it holds on some inputs. [01:41] And this is important because the specification is upstream. [01:43] It's a living, breathing artifact that the builder interacts with. [01:47] You want this to be correct. [01:49] Everything else is downstream from this. [01:52] The AI coding agent then goes and implements from the specification. [01:57] And the formal verification tool proves that the implementation matches the specification. [02:04] So, humans own the specification and machines own the code and proof. [02:10] Lean is a programming language and a proof assistant. [02:14] It is the same language for the definitions and proofs. [02:17] There's no translation layer. [02:20] It is implemented in Lean, which means it's very extensible. [02:23] And this is important. [02:24] It has a small trusted kernel. [02:27] Proofs can be exported and independently checked. [02:33] So, here's an example of a Lean file which has both the code and proof in the same language. [02:39] At the top, you'll see the code, which is a function that reverses a list in Lean. [02:44] So, reverse of one, two, and three gives three, two, and one. [02:47] And right in the middle, you'll see a theorem. [02:51] This is the proof. [02:52] 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. [03:04] How do you do this? [03:05] You have something called as tactics which do the work, which we'll get to in a second. [03:09] And the kernel, remember the small trusted kernel? [03:12] That checks the work. [03:16] A good analogy to understand the Lean proof assistant is that of chess. [03:20] So, in chess, your goal is to checkmate the opponent. [03:23] And you make a bunch of moves. [03:24] You move the knight, you move the bishop. [03:27] Similarly, in Lean, you have a bunch of tactics which are your moves. [03:32] And it's the same chess board. [03:34] It's interactive. [03:36] You want to prove the goal, the theorem, checkmate. [03:39] And you're kind of going down a tree. [03:42] So, you're traversing the tree, you're trying different tactics. [03:45] Maybe for some goals, you're not able to prove it, so you backtrack and then you try another a branch of the tree. [03:51] Very similar to chess. [03:53] And finally, you get a goal that hopefully proves the theorem, and then that small independent kernel confirms and checks it. [04:03] The kernel catches the mistake. [04:05] So, here's an example at the top where an incor- incorrect proof is rejected immediately. [04:11] And you only need to trust the small kernel. [04:14] The good thing is that you can have multiple independent kernels. [04:17] You yourself can actually go write one. [04:19] It's completely open source. [04:20] You have kernels in C++, Rust, Lean. [04:23] That's uh a link to the Arena Lang where you can go and add a kernel. [04:30] So, let's look at some examples where you can put this to practice. [04:35] The first one is having the specification and code both being in Lean. [04:41] Now, this is open source Andreo. [04:44] AI converted zlib, which is a C compression library, to Lean. [04:47] Now, granted this happened over a week or so. [04:52] 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. [05:03] So, the natural language specification says that you decompress the output of compress returning the original data. [05:11] And then, you have an AI that generates the formal spec. [05:14] Now, remember this is important. [05:16] Checking the specification is key. [05:19] After you do that, the AI goes and writes your code in Lean, and then generates these helper lemma subgoals, and proves the theorem. [05:28] And at the bottom, you can see that it's verified with that small independent kernel. [05:34] So, what you just saw was that AI decomposed the problem into lemmas, which are subgoals. [05:39] It proved each of them using tactics. [05:41] Remember the chess moves that we were making? [05:44] And it assembled it into a final theorem. [05:47] Checkmate. [05:49] And the kernel checked it. [05:51] And this particular example had 32,000 lines of proof. [05:54] So, it was pretty big. [05:58] Let's take another example. [05:59] What if you have code in Rust? [06:01] Well, you can write the functional specification of it or the model in Lean. [06:06] An example of that is Cedar. [06:08] Cedar is an open-source authorization policy language, which is used by AWS verified permissions and access. [06:15] The specification of Cedar is written in Lean. [06:19] The production code runs in Rust. [06:22] Why is this important? [06:23] Because let's take an example. [06:25] You have forbid Trump's permit. [06:27] You want to make sure that for any forbid policy being satisfied, the request is always denied. [06:32] This is key. [06:36] 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 [06:45] to check that both of those for the same inputs give the same output. [06:50] And there's about 100 million differential random tests uh run nightly. [06:54] No version ships until this is satisfied. [06:59] Let's take another example. [07:01] What if you have code in Rust and you want to deductively verify with Lean or solvers? [07:07] Before we go there, let's quickly talk about this new term solvers. [07:10] So, remember we spoke of Lean being this chessboard interactive where you're making a bunch of moves trying to checkmate. [07:17] A solver is a calculator, a very powerful one. [07:21] You feed in a formula and it returns an output. [07:25] In this case, satisfiable or unsatisfiable. [07:30] So, an example of this is Verus, also an open-source tool. [07:35] It uses this solver, this very powerful calculator, Z3. [07:39] 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. [07:47] And the code is in line. [07:50] So, you see these two requires and ensure keywords, that's what we call a pre and post condition. [07:56] What must be true before and what must be true after. [08:01] And this is a static check. [08:02] It's enforced by the verifier and erased at runtime. [08:05] So, almost like ghost code. [08:09] Another example of this is Eneus, which uses the mid-level intermediate representation for Rust and does a functional translation to Lean. [08:18] And right after that, you use the same theorem prover, the same chessboard that we spoke of. [08:26] Now, you may be asking, well, what if I have any programming language? [08:32] We at AWS have been working on an open-source tool called Strata. [08:37] 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. [08:46] Think of this like a compiler. [08:47] You have a high-level intermediate representation and you lower it down to a low-level intermediate representation, which is what Strata core is. [08:55] Now, this is written in Lean. [08:58] 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. [09:05] For example, the Lean proof, remember the chessboard, or the very powerful calculator, SMT solvers, or model checkers. [09:17] So, you can get started with this today. [09:19] 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, [09:29] which is very important, and then you can let your coding agent implement it and your formal verification tool prove it. [09:38] So, hopefully in this brave new world, we have software and systems that are not probably correct, but probably correct. [09:46] Thank you. [10:01] [music]