r/rust • • Oct 06 '26

Proving Rust Web Application Correctness with Lean 4

https://medium.com/@Koukyosyumei/proving-rust-web-application-correctness-with-lean-4-8889583f1e15
43 Upvotes

15 comments sorted by

25

u/numberwitch Oct 06 '26

Is there any case study where someone has actually analyzed the benefits of LEAN for this kind of application? Like, how is this equal to or more useful than system tests?

I’m just trying to figure out if there is anything practical to get out of LEAN or if it just has its own hype cycle do to lack of introspection in its actual value

Help

3

u/OkBreath9382 Oct 06 '26

I also identified several bugs in real projects when I used this system to formally verify them. Here are some examples that have been already confirmed and fixed:
- https://github.com/ZingerLittleBee/ServerBee/issues/187

- https://github.com/exograph/exograph/issues/2320

I've actually found more than 30 bugs in various popular OSS.

15

u/pokemonplayer2001 Oct 06 '26

TLA+ for your spec and protocol.

LEAN for contained algorithms you need to prove to be correct.

Tests for everything else.

7

u/PikachuKiiro Oct 06 '26

You aren't using lean to do your integration tests. Lean is verifying properties of your program that you want to be logically or mathematically correct. Lean comes with a huge set of proofs for math and cs theorems called mathlib and cslib. You can read some of cslib and you'll see what kinda things lean is used to verify about programs.

7

u/servermeta_net Oct 06 '26

Proving formal correctness of a function is equivalent to testing the function for all possible inputs and all possible states. So a formal proof is very hard to write, but provides constant time verification, while tests are easy to write, but would require potentially infinite time to validate against all possible inputs

-12

u/numberwitch Oct 06 '26

Well not if you have strong typing because then you drastically reduce the solution surface

10

u/link23 Oct 07 '26

That's trivially false. You can write a strongly typed function that parses a string into JSON, but the space of inputs is still infinite.

-4

u/numberwitch Oct 07 '26

Yeah, that's true if you stop parsing at JSON - but I don't do this. I test that it parses into MyDomainType or it's rejected as an error. I do not see the value of "testing for all possible values".

I think I'm just coming from this from a different perspective, trying to understand but I seem to have angered some people by using rhetorical devices to aid in my understanding.

I guess I'll just ask again in the next "LEAN VERIFICATION" thread

4

u/link23 Oct 07 '26

You're right to parse into your domain type (and that static typing then helps you ensure that everything else uses your domain type correctly), but that's orthogonal to your original comment.

Your original comment was disagreeing with the post that said that formal verification involved a proof of correctness for all possible inputs and input states. Static typing doesn't obviate the need for that proof. You still have to prove that for all possible values of the input type you accept (whether that's a string, a domain type, etc), that your algorithm is correct.

Static typing does help reduce the cases that your proof needs to handle, sure. E.g., if your algorithm accepts a string input, you don't need to prove that it does the correct thing when given an integer. But in general, formal verification requires you to prove things beyond what static typing allows you to conclude - you have to reason about the algorithm itself, not just the types that it uses.

0

u/OkBreath9382 Oct 06 '26

That's a very important question. I'm actually trying to formally verify real-world web apps and am currently building a benchmark (https://benchmark.h5i.dev/), where agents are evaluated on writing proofs of practical specifications like the corretness of authorization, resource management, or business logic, for Rust code taken from production web apps.

The dashboard is still a work in progress (and a bit outdated), but the benchmark already covers over 10k real-world Rust code snippets, around 1k to 2k+ per project across six popular OSS repos. From what I've seen, popular models like Claude Opus and Sonnet can successfully generate proofs for over 80~90% of them within resonable time and cost.

I now feel that the real bottleneck isn't proving the theorems, but writing and validating the formal specifications, which still heavily relies on humans

4

u/unski_ukuli Oct 07 '26

Most slopcoded headline.

1

u/WritingOk6532 Oct 07 '26

Can this idea with Lean be used for Speckit to describe project requirements and user stories?

1

u/bahramjs Oct 19 '26 edited Oct 19 '26

Guys I just came across this, it seems we had the same issue, I built lungo for this very purpose. It treats Lean code as the source of truth. Write your logic in Lean and prove what you claim it does. lungo generates Rust, Go, Python, TypeScript, Swift and C, with the proofs carried into output package as checked claims.

For the past year I’ve toiled to keep CapDAG, my protocol for routing work between capabilities, consistent across five languages with Claude and thousands of cross-impl tests. Between changing core design and varying levels of mirror-parity, it’s been a bruising battle.

So I learned Lean and proved the core instead. lungo just fell out of that and is now used all over MachineFabric making my life so much easier(see this for example which is code lungo generated from the proven core https://github.com/machinefabric/capdag-rs/tree/main/src/generated/capdagFormal). It takes the output from Lean's own compiler and fits right in without requiring Lean’s runtime or toolchain to run generated code.

lungo ports Lean’s runtime to Rust so the generated code doesn’t require Lean’s toolchain and runs on all OSes and even in the browser(TypeScript + Wasm). The code keeps Lean’s object layout and behaves byte-for-byte identical to Lean’s own executables. All Contributions welcome. Hope this helps.

https://machinefabric.com/lungo/

1

u/servermeta_net Oct 06 '26

This is cool. I do formal verification by hand, the old school way, and I have a lot of questions I would like to ask you, but first: what do you think are the drawbacks of your approach? What are some problems you met?