Revisiting Lean and TLA+: How Formal Methods Might Actually Help Software Development
Lately, my X feed has been full of posts about finding software bugs with Lean or TLA+. To be honest, my background in formal methods is pretty thin. Back in university, I wrote some Prolog, and that led me to try a tut
Lately, my X feed has been full of posts about finding software bugs with Lean or TLA+.
To be honest, my background in formal methods is pretty thin. Back in university, I wrote some Prolog, and that led me to try a tutorial for Coq (which I just learned was renamed Rocq). I remember almost none of it. A couple of years into my career, I read Amazon's famous paper on using TLA+ for distributed systems (How Amazon Web Services Uses Formal Methods) and even picked up a book on it. My takeaway back then was that formal methods only made sense for a very narrow slice of software engineering, so I stopped digging.
As I watched the recent wave of posts, though, a thought crossed my mind: could this help catch overlooked edge cases and spec holes in the kind of business workflow redesigns I work on today?
So I went back and took another look at where formal methods stand now. This post is just my notes from sorting it all out.
Redrawing the map of formal methods
The major approaches to formal methods seem to fall into three types:
- Automated logic-based verification (Dafny and similar tools): Proves program correctness by checking that every valid input produces the expected output. You translate the logic and its conditions into logical formulas and let an SMT solver check them. In terms of use cases, this seems to overlap quite a bit with PBT (Property-Based Testing), which tests programs with randomly generated inputs — except here it checks all inputs mathematically rather than sampling them.
- Model checking (TLA+ / TLC): You specify the initial state for each actor, the state transitions, and the conditions that define valid states, and the tool exhaustively explores possible execution sequences to check whether the system can ever reach an invalid state. It is commonly used to verify distributed systems and concurrent designs.
- Proof assistants (Lean, Rocq): Much like writing a math proof or working through a competitive programming problem, you build a proof step by step — breaking things into smaller lemmas, doing case splits, using induction — while the machine strictly checks that there are no logical gaps. My understanding is that these are used when general-purpose solvers hit a wall, such as when the search space explodes. They show up often in cryptographic libraries, compilers, and binary-level safety verification.
Finding bugs with Lean
Let's start with Lean, since it is getting the most buzz right now. The basic setup is straightforward: you have a program (the implementation), you have what it is supposed to do (the specification), and you compare the two to look for mismatches.
Take a real example Mistral highlighted in its Leanstral 1.5 release: a previously unreported bug in a Rust variable-length integer library. Inside the sign handling during decoding, passing the maximum integer value caused an overflow.
From what Mistral reported, they had an agent infer the intended behavior from the code and try to prove in Lean that it works for all inputs; when the proof failed and produced a counterexample, it led straight to this bug.
Looking more closely at why Lean and Rocq examples are suddenly everywhere, they seem to split into two categories depending on whether "what the code should do" is already unambiguously defined.
The first category is properties where the goal is clear upfront, with no need for an LLM to guess the spec — things like memory safety, crash freedom, or standardized cryptographic specs. Here is a quick summary of what I found:
-
Cryptographic arithmetic and security specs: Proving that an optimized implementation built with bit operations and limb arrays matches its mathematical definition, or proving protocol security properties (Google DeepMind's AlphaProof Code proved core protocol properties of Signal's
libsignalin Lean down to AArch64 machine code). - Assembly and binary-level memory safety: Proving that no memory instruction reads or writes outside permitted bounds and that the binary will not crash (such as Anthropic and Theorem verifying fuzz-test entry points across 123 open-source libraries in Rocq, or NoCompiler, which adds types directly to RISC-V assembly and backs them with a type checker verified in Lean 4).
In this category, the LLM does not have to guess what the program is supposed to do. It just writes proof steps or type annotations, and Lean or Rocq decides deterministically whether they hold. When the spec itself has no ambiguity, formal methods can reach this far without relying on an LLM to interpret intent.
The second category is checking business logic or edge cases in existing code that lacks a written spec. Mistral's Leanstral 1.5 experiment mentioned above fits right here: the LLM inferred properties from context and tried to prove or disprove them. Across 57 repositories, it flagged 47 property violations, of which 11 turned out to be real bugs and 5 were previously unreported on GitHub (including that variable-length integer library bug).
For underspecified existing codebases like this, my honest impression of where things stand today is roughly: "We let an LLM run formal methods, it hands back a list of candidate bugs, some of them turn out to be real, and we have no idea how many were missed." That 11-out-of-47 ratio sums it up well. Even when a counterexample is logically airtight, it is only a real bug if the spec the LLM guessed matches what the author actually intended. A human still has to make the final call on whether that was really the intended spec (we will set aside the minor issue that humans often cannot remember what undocumented code was supposed to do either).
As a side note, while reading "we tried this on real repos" posts, one thing caught my eye: why are the source-code translation targets almost always written in Rust? I assumed it was because Rust's strict type system and ownership rules make it easier to model, and when I checked, that turned out to be right. The Rust-to-Lean translator Aeneas, used in Mistral's experiment, leverages safe Rust's ownership and borrowing rules so it can translate code into clean pure functions in Lean without wrestling with low-level memory management. Trying to do the same faithfully for a dynamically typed language like Python gets much harder, since a single operator can mean many different things depending on runtime types and side effects.
Finding bugs with TLA+
Compared with code-level proofs in Lean, business-workflow redesign usually happens a layer above programming. What you have on the table is not source code, but flowcharts, data-flow diagrams, policy docs, and piles of meeting notes.
If you use an LLM to turn those diagrams into a TLA+ state-transition model, and pull "allowed states" and "rules that must hold" from the docs and meeting notes to look for contradictions, TLA+ / TLC looks much better suited for finding workflow holes outside the code layer. Most real operational incidents are not single arithmetic mistakes — they happen when individual steps all look reasonable on their own, yet break a business rule when they occur in an unexpected order.
I went back and re-read AWS's case study, and it looks like they used TLA+ on DynamoDB and S3 for the exact same reason: to catch subtle ordering bugs in distributed protocols at the design stage, in sequences that ordinary tests would almost never hit.
Take a simple purchase-approval workflow as an example. The condition for a valid state is: if an order has been placed, the ordered amount must match the approved amount. Start from an initial state of "$1,000 requested, not approved, not ordered," put request, approval, quote revision, and order placement into the model as transitions, and let TLC explore. It hands back a counterexample trace like this:
- Requester submits a purchase request for $1,000.
- Manager approves $1,000 (requested: $1,000, approved: $1,000).
- Requester revises the quote to $1,500 (requested: $1,500, approved: $1,000).
- System checks only that the request is "approved" and places the order for the current requested amount of $1,500 — no longer a valid state.
It looks obvious enough to laugh at in four lines, yet real systems fail in the exact same shape all the time — like a delayed payment webhook arriving after a hold expires and confirming a double booking.
Of course, things can still slip through here too. If nobody puts "revising a quote after approval" into the model as a possible action, TLC will never explore it (and real production incidents almost always come from that one move nobody thought to write down). And meeting notes or business docs are often vague or incomplete, so just like with Lean, a human still has to check whether the rules and transitions the LLM extracted actually match what the team agreed on.
Even with that caveat, the division of labor feels practical: humans and LLMs pull possible actions and rules from diagrams and docs, and TLC searches for combinations that contradict each other. Because a counterexample comes out as one concrete sequence of steps, having an LLM translate a raw TLC trace into business terms (or a sequence diagram) could make it a genuinely useful artifact for discussing questions like "should editing the amount invalidate approval, or should we lock edits after approval?" with stakeholders.
What comes after "writing testable code"
Long before AI coding took off, "write code that is easy to test" became a standard design habit.
More recently, as AI has lowered the cost of writing code, more teams have started experimenting with moving to Rust — a language that is expensive for humans to learn and write, yet pays off in safety and performance (as I covered in an earlier post).
I suspect formal methods may follow a similar arc. Right now, for most existing code, the realistic entry point looks like Leanstral: let an LLM run formal tools, produce candidate bugs, and have humans check the hits.
As AI lowers the cost of writing models and proofs further, though, we may gradually see "design and write code so it is easy to verify" spread in places where correctness really matters. When I think about it like this, I start wondering if Rust really is going to take over.
So after Coq in university and TLA+ a couple of years into my career, I am hoping third time's the charm — this time I want to actually put formal methods to use.
Originally published on my blog.
Related: Visualizing Code: Making Use of Deterministic Computation; Agentic Development: How to scale it?; LLMs in Late 2026: How the Models and My Development Setup Changed
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.