Lean 4 for Game Makers: Rules Proved for Every Input
Lean 4 is a programming language that also checks proofs. You write a rule of your game, a claim about it such as "moving gems never loses one", and a proof. Lean accepts the proof only if it covers every input and every list of moves, of any length. It is the hardest of the three tools on this site, so keep it for the few core rules that the rest of your game leans on.
What is Lean, in plain words?
The book Theorem Proving in Lean 4 says Lean can be seen equally well as a programming language and as a system for proofs, one where every claim has to be backed by a proof. A test tries some examples and hopes the rest behave. A proof in Lean covers all of them, including the ones nobody thought to try.
Your AI writes the proof. Lean checks it. The AI cannot talk Lean into a proof that does not hold, and that is the point.
It is the hardest of the three. When is it worth it?
Be honest with yourself here. Proofs take longer to write than checks with TLA+ or Z3. An AI gets stuck more often, and when the rule changes, the proof has to change too.
It is worth it for laws that many systems rely on:
- The inventory never loses or duplicates items, however they move between bag, chest, shop and trade.
- Health stays between zero and the maximum, whatever heals, poisons or upgrades hit it.
- A crafting recipe never creates items from nothing.
- Money never goes below zero.
- Saving and then loading gives back the same game.
It is not worth it for balance numbers, where Z3 is quicker, for the order of events in quests and puzzles, where TLA+ is quicker, or for a part of the game that still changes every day.
A bag, a chest and a proof
An invented game where gems move between your bag and a chest. Save this as Chest.lean:
-- An invented game: gems move between your bag and a chest.
structure Store where
bag : Nat
chest : Nat
inductive Action where
| put (n : Nat) -- move n gems from the bag into the chest
| take (n : Nat) -- move n gems from the chest into the bag
def step (s : Store) : Action → Store
| .put n => if n ≤ s.bag then { bag := s.bag - n, chest := s.chest + n } else s
| .take n => if n ≤ s.chest then { bag := s.bag + n, chest := s.chest - n } else s
def play (s : Store) : List Action → Store
| [] => s
| a :: rest => play (step s a) rest
def total (s : Store) : Nat := s.bag + s.chest
-- One move never loses or creates a gem.
theorem step_keeps_total (s : Store) (a : Action) :
total (step s a) = total s := by
cases a with
| put n => by_cases h : n ≤ s.bag <;> simp [step, total, h] <;> omega
| take n => by_cases h : n ≤ s.chest <;> simp [step, total, h] <;> omega
-- Any list of moves, of any length, never loses or creates a gem.
theorem play_keeps_total (s : Store) (moves : List Action) :
total (play s moves) = total s := by
induction moves generalizing s with
| nil => rfl
| cons a rest ih => simp only [play, ih, step_keeps_total]
#print axioms play_keeps_total
Read it from the top. step is one move: put n gems into the chest, but only if the bag holds that many, and otherwise nothing happens. play makes a whole list of moves, one after another. total counts the gems in both places.
Then come two claims. step_keeps_total says one move never changes the total. Its proof looks at both kinds of move, with and without enough gems, and leaves the sums to omega, the part of Lean that does whole-number arithmetic. play_keeps_total says the same for any list of moves. Its proof works by induction: with no moves the total is plainly the same, and if it holds for the rest of a list, then it still holds with one more move in front, because of step_keeps_total. So it holds for every list, whether three moves or three million.
Run lean Chest.lean:
'play_keeps_total' depends on axioms: [propext, Quot.sound]
No errors, so both proofs went through. The last line lists the basic building blocks the proof stands on. These two are part of Lean itself. The name to look out for is sorryAx, which comes up below.
What Lean says when the rule is wrong
Take out the check in the put line, so it reads | .put n => { bag := s.bag - n, chest := s.chest + n }, the way a quick change might drop it. Lean now says (shortened):
Chest.lean:24:69: error: omega could not prove the goal:
a possible counterexample may satisfy the constraints
c ≥ 0
b ≥ 0
a ≥ 0
a - b ≥ 1
a - b ≥ 1
where
a := ↑n
b := ↑s.bag
c := ↑s.chest
'play_keeps_total' depends on axioms: [propext, sorryAx, Quot.sound]
Read the where part: a is the number of gems you put in and b is what the bag holds, and a - b ≥ 1 means you put in more than you have. Whole numbers in Lean never go below zero, so 3 minus 5 is 0, much like a game counter that is kept from showing a minus. A bag of 3 that puts in 5 ends up with 0 in the bag and 5 in the chest. Two gems came from nothing. That is a duplication bug, and a test that only ever clicks the button when you have enough gems would never find it.
Notice sorryAx in the last line. It means the proof has a hole.
Why sorry is the word to watch
In Lean, sorry means "trust me, I will prove this later". It is handy while you work. But Lean only warns about it, with warning: declaration uses `sorry`, and still finishes without an error. An AI that is stuck may reach for it. So ask your AI never to leave a sorry in, and to show you the output of #print axioms. If sorryAx is in that list, the proof is not finished, however good the rest looks.
One more honest limit: the proof is about the Lean copy of your rule. If your game is written in another language, ask your AI to put both versions side by side and to run them on many of the same moves, to show you that they agree.
How do I install and run it?
The Lean website calls VS Code with the official Lean 4 extension the recommended way to install Lean. The extension's setup guide walks you through the rest, and Lean then shows its messages inside VS Code. On the command line, lean Chest.lean checks a file. This example needs nothing beyond Lean itself and ran with Lean 4.35.0. Or let your AI set it up.
Why does this matter more as the game grows?
The more systems use your inventory, such as shops, crafting, trading and saves, the more one hole in it costs, and the harder that hole is to find by playing. A proved law stays true for any number of moves. And when an AI changes the code later, the proof breaks at once and points to the place, instead of a player finding the duplicated gems months later.
Paste this to your AI
Prove one core rule of my game in Lean 4. Use core Lean only, no extra libraries, unless I agree.
1. Ask me which rule must always hold, for example "items are never lost or duplicated", "health stays between 0 and max", "crafting never creates items from nothing". If I do not know, suggest the rules most of my game relies on, with the place in the code for each.
2. Write the rule's data and moves in Lean, as close to my game's code as you can. Show me which file and line each Lean definition copies.
3. State the law as a theorem about one move, then about any list of moves, and prove both. The second one by induction over the list.
4. Never use sorry, admit or new axioms. If you cannot finish a proof, stop and tell me exactly where you are stuck. Do not weaken the theorem to make it go through without asking me.
5. Run lean on the file and show me the real output, including the output of #print axioms. If sorryAx is in that list, say that the proof is not finished.
6. Break the rule on purpose once and show me what Lean says.
7. Test that my game's code and the Lean copy give the same results on many moves, and show me what you tested.
Never say "proved" unless Lean accepted it without sorry. Say what the theorem covers and what it does not.
Which tool for which question?
| Question | Tool |
|---|---|
| Can things happen in an order that breaks the game? Softlocks, quests, turns, saving | TLA+ and TLC |
| Is there any number or setting that breaks this? Balance, prices, timing | Z3 |
| Must this law hold forever, for every input and any number of moves? | Lean 4, this page |
Most games need Lean for very few rules, if any. If you do not know yet where your game breaks, start with My game keeps breaking.
Was this page helpful?
Next step
Blueprint
Storage Chest
Here gems never vanish between bag and chest; the blueprint builds the whole chest, with a lock, limits and every kind of item, proved in Lean, and shows how a move split in two puts a gem in both places.