A message is a clue

Mistakes belong here.

Lean is telling us something useful. Read the first error, find what it expected, then make one small change.

“Start with one message. The later ones may disappear once you fix the first.”
Typo · Your error-reading companion

Typo thinking

How to read any error

The codeexample : 2 + 2 = 5 := by rflThe real message, part by part
Tactic `rfl` failed: The left-hand side 2 + 2is not definitionally equal to the right-hand side 5 ⊢ 2 + 2 = 5
  1. Which step failed. Here the tactic rfl could not finish its job.
  2. What Lean worked out on the left side of the equation.
  3. What it needed to match on the right side. 2 + 2 is not 5, so they don’t match.
  4. The goal at that moment, after ⊢. This is exactly what still needed proving.
Most Lean errors have this shape: what went wrong, what Lean found, what it expected, and the goal you were working on.
  1. Read only the first errorLater errors often vanish once it is fixed.
  2. Find what Lean expectedLook for words like expected, failed or unknown.
  3. Look at the goalThe line after ⊢ shows what you really had to prove.
  4. Change one thingThen let Lean check again. Small steps, like on the trail.

Twelve common patterns

Pattern 01

The value has the wrong type

TypeMismatch.lean · intentional errorTry it live (opens official Lean editor)
#check (3 : String)
Actual Lean diagnostic
failed to synthesize instance of type class
  OfNat String 3
numerals are polymorphic in Lean, but the numeral `3` cannot be used in a context where the expected type is
  String
due to the absence of the instance above

Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command.

Lean expected one type and received another.

Try this: Read both types. Change the value, the annotation, or the operation so they agree.

Pattern 02

A function received the wrong argument

Application.lean · intentional errorTry it live (opens official Lean editor)
#check Nat.succ "hello"
Actual Lean diagnostic
Application type mismatch: The argument
  "hello"
has type
  String
but is expected to have type
  Nat
in the application
  Nat.succ "hello"

The function’s input type does not match the argument you gave it.

Try this: Check the function’s type, then supply an argument of that type.

Pattern 03

A name cannot be found

UnknownName.lean · intentional errorTry it live (opens official Lean editor)
#check myMissingNumber
Actual Lean diagnostic
Unknown identifier `myMissingNumber`

There is no available declaration with this spelling.

Try this: Check spelling, capitalization, declaration order, namespaces, and imports.

Pattern 04

A tactic name is unknown

UnknownTactic.lean · intentional errorTry it live (opens official Lean editor)
example : True := by
  magic
Actual Lean diagnostic
unknown tactic

unsolved goals
⊢ True

The parser does not recognize this tactic in the current imports.

Try this: Check the name and required import. The cheat sheet lists the tactics this course uses.

Pattern 05

rfl cannot close this equality

Rfl.lean · intentional errorTry it live (opens official Lean editor)
example : 2 + 2 = 5 := by
  rfl
Actual Lean diagnostic
Tactic `rfl` failed: The left-hand side
  2 + 2
is not definitionally equal to the right-hand side
  5

⊢ 2 + 2 = 5

The two sides are not definitionally equal.

Try this: Check the statement. For a true but noncomputational equality, use suitable lemmas or other tactics.

Pattern 06

A goal remains

Unsolved.lean · intentional errorTry it live (opens official Lean editor)
example : True ∧ True := by
  constructor
  · exact True.intro
Actual Lean diagnostic
unsolved goals
case right
⊢ True

The proof finished its tactic block with work still left.

Try this: Read the remaining goals and finish every case or constructor argument.

Pattern 07

The rewrite pattern is absent

Rewrite.lean · intentional errorTry it live (opens official Lean editor)
example (n : Nat) (h : n = 2) : 3 = 3 := by
  rw [h]
Actual Lean diagnostic
Tactic `rewrite` failed: Did not find an occurrence of the pattern
  n
in the target expression
  3 = 3

n : Nat
h : n = 2
⊢ 3 = 3

Lean could not find the expression to replace at the chosen location.

Try this: Inspect the goal and hypothesis. Check the equality’s direction and the rewrite target.

Pattern 08

Nothing can be introduced here

Intro.lean · intentional errorTry it live (opens official Lean editor)
example : True := by
  intro h
Actual Lean diagnostic
Tactic `introN` failed: There are no additional binders or `let` bindings in the goal to introduce

⊢ True

The goal does not expose an appropriate input or implication premise.

Try this: Inspect the current goal. Use intro for a function-like goal, an implication, or a for-all statement.

Pattern 09

The supplied proof does not match

Exact.lean · intentional errorTry it live (opens official Lean editor)
example (P Q : Prop) (h : P) : Q := by
  exact h
Actual Lean diagnostic
Type mismatch
  h
has type
  P
but is expected to have type
  Q

The proof term establishes a different proposition from the goal.

Try this: Read the type of the supplied hypothesis and compare it with the goal.

Pattern 10

A negative literal was used as a Nat

NegativeNat.lean · intentional errorTry it live (opens official Lean editor)
#check (-3 : Nat)
Actual Lean diagnostic
failed to synthesize instance of type class
  Neg Nat

Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command.

Natural numbers have no negative-number interpretation here.

Try this: Use the integer type when negative whole numbers are required.

Pattern 11

No suitable decision procedure

Decide.lean · intentional errorTry it live (opens official Lean editor)
example (P : Prop) : P := by
  decide
Actual Lean diagnostic
Expected type must not contain free variables
  P

Hint: Use the `+revert` option to automatically clean up and revert free variables

An arbitrary proposition does not automatically have a Decidable instance.

Try this: Use a suitable assumption or proof method. Use decide for propositions with a decision procedure.

Pattern 12

The expression is incomplete

Parse.lean · intentional errorTry it live (opens official Lean editor)
example : 2 + 2 = 4 := by
  exact (
Actual Lean diagnostic
unexpected end of input; expected ')', '_' or identifier

The parser expected more syntax before it could read a complete expression.

Try this: Check parentheses, punctuation, and the expression after each defining symbol.