Part I · Executable Specifications Ch 04 · 35 min · ProveIt/Ch04.lean

Gear 1: TLA+ Model Checker in 35 Lines

Demystifying state-space exploration by building a breadth-first protocol bug-finder from scratch.

5-Line Distributed Bug · Unfenced Lease Split-Brain
structure LeaseSys where
  leaderEpoch : Nat  -- coordinator's current lease epoch
  staleInFlight : Bool -- client 1 (epoch 1) packet paused in network
  storageVal  : String -- 'init' | 'v2_committed' | 'v1_stale'
  epoch2Done  : Bool -- client 2 (epoch 2) already ACKed commit to user!
Socratic Gate · Commit to a Prediction Unanswered · Locks on commit

Before spending two hours trying to prove `epoch2Done = true → storageVal ≠ "v1_stale"` in Lean, what happens if we run a 4-step breadth-first search (BFS) over all possible action interleavings?

TLA+ Without the Mystery: `init`, `nextStates`, `safe`

Leslie Lamport's TLA+ taught systems engineers to view any distributed protocol, cache coherence controller, or WAL engine as a state machine: an initial state init : S, a transition function nextStates : S → List (Action × S) returning all enabled next steps, and an invariant safe : S → Bool.

Because Lean 4 is simultaneously a fast functional programming language and an interactive theorem prover, you can write a generic TLA+-style BFS model checker in 35 lines of pure Lean—and execute it directly at compile time with #eval or #guard!

Why Two Gears Beat Either Gear Alone

Model checking (Gear 1) finds shallow protocol bugs automatically with concrete traces, yet must cap integer bounds and queue lengths so the search terminates. Deductive proof (Gear 2) handles infinite integers and unbounded message histories, yet requires human/AI invariant insight. Running Gear 1 first inside the same Lean file gives you instant falsification before investing in proofs.

⊢ ProveIt/Ch04.lean · 35-Line Executable BFS Trace Finder
Step 1 / 1
Lean Infoview Open Goal
Hands-On Exercise (ProveIt/Ch04.lean):

In `ProveIt/Ch04.lean` (and live in Browser Lab 2), run Gear 1 BFS to uncover a split-brain trace, add monotonic epoch fencing, and verify bounded exhaustion with `#guard`.

Finished the Socratic gate, Infoview trace, and playground run?
Record completion in local browser storage to track your 20-unit progress.