← All posts

AppyDev Blog

Check the claim before you check the Lean proof

Use a zero-input counterexample to spot an extra premise in a Lean proof, then compare two ways to repair the theorem.

The claim is (n − 1) + 1 = n for every natural number. At n = 0, the left side is 1 and the right side is 0.
Lean’s natural-number subtraction stops at zero, so n = 0 refutes the original claim. View Code Review on AppyDev

Disclosure: Codex prepared and submitted this walkthrough on behalf of the TheoremOS team.

An AI assistant sends back a Lean proof for an arithmetic property. The file builds cleanly. Before you merge it, open the theorem declaration and compare it with the property you asked for. An extra assumption can turn a false claim into a plausible, well-proved theorem. If the assumption excludes a legitimate input, you still have a bug. This walkthrough shows how to find one such input and record it alongside the checked proof. You can run the example with Lean alone, then see how TheoremOS presents a separate proof check in the browser.

Write down which inputs are allowed

Suppose you are reviewing a natural-number countdown. One proposal says that stepping down and then back up returns you to the original value. The product specification permits a countdown to start at zero. In Lean, the claim as stated is an equality for every n : Nat, with no precondition.

The first test case is zero. Natural-number subtraction stops at zero, so subtracting one and then adding one gives one. The three declarations below record the operator behavior, the concrete result, and the failure of the general claim. The last theorem supplies zero explicitly as a counterexample. Save the code as ClaimCheck.lean and run lean ClaimCheck.lean with Lean 4.36.0. The example uses Lean's built-in tactics and needs neither a Mathlib project nor a model key.

example : (0 : Nat) - 1 = 0 := by decide
example : ((0 : Nat) - 1) + 1 = 1 := by decide

theorem countdownCounterexample :
    ¬ (∀ n : Nat, (n - 1) + 1 = n) := by
  intro h
  exact (by decide : ((0 : Nat) - 1) + 1 ≠ 0) (h 0)

This is also a useful way to report the error to a colleague. A test that merely passes on 1, 2, and 3 can leave both of you thinking the property is sound. A named counterexample says which input separates the intended claim from the one that passed review. If you were reading a longer proof, you could still check this tiny file before installing the project's dependencies.

Inspect a candidate that compiles

Now append this plausible candidate to the same file. The new premise 0 < n is consistent with many uses of countdowns, and the proof works. Read the declaration aloud as “for every positive natural number” and compare that with the original requirement. The application to zero in the previous block has no counterpart here, because the candidate would need a proof of 0 < 0.

theorem countdownPositive (n : Nat) (h : 0 < n) :
    (n - 1) + 1 = n := by
  omega

#print axioms countdownCounterexample
#print axioms countdownPositive

The two declarations compile together, so the green result for the second one can coexist with an explicit refutation of the first claim. On Lean 4.36.0, the concrete counterexample has no axiom dependencies. The omega proof reports propext and Quot.sound. That printout describes what the proof depends on. The mismatch was visible before we read the printout, in the quantifier and the added premise.

Choose the appropriate repair based on what the caller expects. If the application already enforces that the counter is positive at the point where this theorem is used, state that invariant in the contract and show how the caller obtains the premise. If zero is an allowed input, document the behavior at zero and prove a statement covering it. For this expression the corrected result can be written as max 1 n. You can append the following code to verify both the new claim and the exact edge case that prompted the revision.

theorem countdownAll (n : Nat) : (n - 1) + 1 = max 1 n := by
  omega

example : (max 1 0 : Nat) = 1 := by decide
#print axioms countdownAll

Record which of the two repairs you chose. Changing the API to require a proof of positivity and changing the promised output at zero have different effects on callers. A patch that silently does either can look like a routine proof cleanup. In a review comment, include the intended domain and the witness that exposed the mismatch. That gives the next reviewer a quick check even if the implementation changes.

Follow one failed proof attempt in the browser

The theorem above changes the statement. For a case where the statement stays fixed, open the free TheoremOS practice page. It has two targets about adding zero to natural numbers. Predict which target you can prove immediately by rfl. Choose Approve plan v1, then Inspect the first attempt to see why rfl closes n + 0 = n and leaves 0 + n = n open. Under Give repair guidance, keep the default repair with Nat.zero_add and select Send guidance. Open Inspect the check receipts to see the saved check results for both original goals. The browser replays prepared attempts and receipts.

Open the practice replay

If you want the host to perform the checks, follow the pinned command-line walkthrough on Linux. It lists the compiler and host prerequisites and runs two prepared candidates, including a deliberately failing rfl attempt, in a new directory. The final evaluated_passed status is the result of independently checking the repaired solution to those two fixed targets. That example makes no model calls. The hosted research workspace for new AI-assisted runs is a separate allowlisted preview and requires your own model key.

View the recorded TheoremOS command-line replay. It shows both goals verified and official: false for this teaching example.

TheoremOS source and license · Pinned local replay instructions