Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

19. A repeatable debugging and proof workflow

Lean development alternates between choosing a representation and proving that it supports the required properties. Use the current goal to decide what to change or prove next.

Develop a function and its contract together

Start with a small, typed definition and evaluate its boundary cases. State a property that matters to a caller and attempt a proof.

If the proof fails, check whether you need a helper lemma, a stronger invariant, or a different representation. Once proved, make the property part of the module’s interface.

Before using automation, classify the goal:

GoalFirst approach
An implication or universal statementintro
A conjunctionconstructor or a pair of terms
An existentialProvide the witness, then prove its property
A disjunction hypothesiscases
Equality after computationrfl
A known equality in a larger expressionrw
Routine normalisationsimp with relevant definitions or hypotheses
Recursive data propertyInduction, with changing parameters generalised
Natural or integer linear arithmeticomega
A library fact with slightly different notationsimpa using a checked theorem

Use the goal and its local hypotheses to decide which of these starting points applies.

When elaboration fails

Reduce the expression to the smallest failing call and inspect the function’s type. Annotate the first ambiguous value or supply an implicit argument by name. If instance search fails, check that a suitable type-class instance exists and is available through the current imports.

For a universe mismatch, check whether a function declared with Type needs to be universe-polymorphic. For a dependent index mismatch, check whether the indices are definitionally equal or need a proof of equality. If they need a proof, use it to reconcile the indices or redesign the interface if callers routinely need to transport values.

When a tactic fails

Read the goal and its local hypotheses. Identify the main connective or operation and compare the exact target type with the conclusion of the theorem you want to apply.

Check the rewrite direction and see whether simplification has already changed the expression. If the induction hypothesis fails to match the recursive call, compare their arguments and strengthen the statement as needed.

Use conversion mode to focus a rewrite on a specific part of an equality.

example (a b c : Nat) : a + (b + c) = a + (c + b) := by
  conv =>
    lhs
    arg 2
    rw [Nat.add_comm b c]

lhs selects the left side of the equality, and arg 2 then selects the second argument of its outer addition. The rewrite applies within that argument.

When a tactic behaves unexpectedly, inspect the term or the rules it uses.

  • Prefix a tactic block with show_term to see the term Lean constructs.
  • Run simp? to get a suggested simp only [...] invocation. Use it to make the lemma dependencies explicit.
  • If simp stalls or loops, put set_option trace.Meta.Tactic.simp true in immediately before the declaration to trace its rewrites in the InfoView.

When the build fails

Check the selected compiler version, the dependency manifest, and whether the command runs through lake env. If Lean cannot find a module, check that the file path matches the import and that the dependency has been built. Rebuild stale artefacts with the correct toolchain.

If stale artefacts persist, use lake clean and rebuild. The command removes local build products, so the next build may take longer. Keep the source files and manifest that define the project.

A completion checklist

Check that the public types express the intended inputs and outputs, the failure cases are explicit, and the contract specifies when the operation succeeds and what it guarantees. Check that the proofs have no holes and audit axiom dependencies where the claim requires it.

Build the package and run representative boundary tests against the native executable.

Exercises

  1. Introduce a wrong rewrite direction in a working proof and use the goal state to repair it.
  2. Move one declaration into a namespace and diagnose the resulting name-resolution error in a caller.
  3. Take one new feature through the complete loop: definition, boundary examples, contract, proof, native build, and integration test.

You are working independently when you can explain why the next step addresses the current failure, even when you still consult documentation for names and syntax.