Error Reference › Lean 4
Lean 4

Unexpected token; expected term (Lean 4)

The parser hit something it did not expect at that position, so Lean could not even read the code; the message says what it expected instead.

What it looks like

Main.lean:5:0: error: unexpected token 'theorem'; expected term

What does "Unexpected token; expected term (Lean 4)" mean?

This is a parse error, raised before any type checking. Lean prints unexpected token 'x' (or unexpected identifier), then expected and the things that could have come next. Here a definition ended with := and nothing after it, so the next command's keyword arrived where a term was expected. Parsing resumes at the next command, so errors inside the broken part only appear once this one is fixed.

What causes "Unexpected token; expected term (Lean 4)"?

  • Lean 3 / mathlib3 syntax: begin … end, λ x, …, assume, or commas between tactics.
  • A missing := or by, or a definition left unfinished before the next command.
  • Indentation: a continuation line not indented, so the block ends early.
  • A keyword (such as fun, at, from or end) used as a name, or unbalanced brackets.

How do I confirm the cause?

  1. Go to the line and column in the message; the problem is at that token or just before it.
  2. If the code came from a Lean 3 source (mathlib3, older tutorials), expect several of these — it needs porting, not patching.

How do I fix "Unexpected token; expected term (Lean 4)"?

  • Look at the reported position and just before it — the real mistake is often on the previous line.
  • Port Lean 3 syntax: fun x => … instead of λ x, …; := by with indented tactics instead of begin … end; one tactic per line or separated by ;.
  • Finish the incomplete definition, or put sorry there for now.
  • Check bracket pairs: ( ), ⟨ ⟩ and { }.

How do I stop it happening again?

  • Use examples written for Lean 4 and current Mathlib.
  • Fix parse errors as soon as the editor shows them; they hide other errors in the same declaration.
Got a different error? Paste it into the Explain This Error tool → Identifies the family and the fix — runs locally, nothing uploaded.

Related errors