Lecture 1 wrote proofs as terms. That is the clearest way to see what a proof is, but it is a poor way to find one. A term is written inside out: to prove q ∧ r you must already know the two pieces you will pair up. On paper you work the other way. You look at what you have to prove, take one step that makes it simpler, and look again. Tactics let you work like that in Lean, and almost every proof in Mathlib is written with them.

Lecture 1 ended with the one fact that makes tactics trustworthy: a tactic proof still produces a term, and #print shows it. This lecture takes that as given and builds the working vocabulary. By the end you should be able to read the goal display, choose a tactic from the shape of the goal, know which automation to try and what each one can and cannot do, and read the four error messages you will meet most often.

The page starts with import Mathlib, as a Lean file in the course project would. Three of the tactics below need it: use and ring come from Mathlib, and aesop from the Aesop library that Mathlib depends on. Everything else is part of core Lean. With Mathlib loaded, Lean prints the type Nat as ℕ.

Cheatsheet

Cards for this lecture: constructor, cases, rcases, obtain, have, show, calc, use, exists, refine, rw, simp, unsolved goals. You can run every block on this page in VS Code (open the lean/ folder) or in a notebook started with leanmagic.setup("import Mathlib").

The proof state

When you put the cursor inside a tactic proof, Lean’s infoview shows where you are. What it shows has a precise meaning.

Definition 3.1 (Goal and proof state).

A goal is a list of named hypotheses together with a target type . Lean displays it as the hypotheses, one per line, followed by ⊢ T. A goal stands for a hole in the proof term: a term of type that is still missing and may use the hypotheses. The proof state is the list of goals still open. The proof is finished when the list is empty.

Read this way, a goal of the proof state (Definition 3.1) is the typing judgment of Lecture 1 with the term not yet written. A tactic is what writes it.

Definition 3.2 (Tactic).

A tactic is a program that takes the proof state, fills the first goal’s hole with a piece of term that may contain new holes, and returns the new list of goals: the goals it did not touch, plus one goal for each new hole. Tactics are metaprograms: programs that run inside Lean and write Lean terms.

Here is a proof with five tactic steps, each a tactic in the sense of Definition 3.2. The figure after it shows the goal list after each step, copied from Lean’s output.

import Mathlib
 
theorem lift_and (p q r : Prop) : (p → q) → p ∧ r → q ∧ r := by
  intro hpq h
  obtain ⟨hp, hr⟩ := h
  constructor
  · exact hpq hp
  · exact hr
tactic goal list after the tactic (start) p q r : Prop ⊢ (p → q) → p ∧ r → q ∧ r intro hpq h hpq : p → q h : p ∧ r ⊢ q ∧ r obtain ⟨hp, hr⟩ := h hpq : p → q hp : p hr : r ⊢ q ∧ r constructor case left: hpq hp hr ⊢ q case right: hpq hp hr ⊢ r · exact hpq hp case right: hpq hp hr ⊢ r · exact hr no goals: proof done the kernel now checks the finished term

Read the figure top to bottom. intro hpq h moves the two premises into the context, so the target shrinks to q ∧ r. obtain takes the pair h apart into hp and hr. constructor splits the target into two goals, which Lean names case left and case right after the fields of And. Each exact closes one goal, and the · bullet makes sure it does. In each step the tactic wrote part of a term, and #print lift_and shows the result:

#print lift_and

Lean prints fun p q r hpq h ↦ And.casesOn h fun hp hr ↦ ⟨hpq hp, hr⟩. The obtain became And.casesOn, the function that takes a proof of ∧ apart (Lecture 4 explains where such functions come from), and constructor became the pair ⟨_, _⟩. As in Lecture 1, only this term goes to the kernel.

Remark 3.1 (Tactics are outside the trusted base).

A tactic can be slow, clever or buggy, and it does not matter for correctness. Whatever term a tactic writes, the kernel type-checks it before the theorem is accepted. A bug in a tactic can make it fail, or make it produce a term that the kernel rejects; it cannot make Lean accept a false statement. (Code written to switch the kernel check off is the exception; Lecture 11 shows it, and how leanchecker catches it.) This is the de Bruijn criterion from Lecture 1, and it is why Lean can afford large automatic tactics.

The core vocabulary

Choose a tactic by looking at the shape of the goal: its target, and the hypotheses you have. The table groups the tactics of this lecture by what they do to the goal list.

you want totacticthe goal list afterwards
assume the premise of → or ∀intro hsame goal, h in the context
close the goal with a termexact tgoal removed
reason backwards from a lemmaapply fone goal per premise of f
build an ∧, ↔ or structureconstructorone goal per field
take a hypothesis apartcases, rcases, obtainone goal per constructor
give a witness for ∃use, exists, refinethe property of the witness
prove a fact on the wayhave, show, calca new hypothesis, or a chain
replace equals by equalsrw, simpthe rewritten target
hand the goal to a procedureomega, decide, grind, aesop, exact?closed, or an error (aesop: sometimes a smaller goal)

Introduce, close, reason backwards

The card intro, exact, apply traces these three tactics hole by hole. One point to add: apply f leaves one goal for every premise of f that it cannot fill from the target, in order.

example (p q r : Prop) (hp : p) (hq : q) (h : p → q → r) : r := by
  apply h
  · exact hp
  · exact hq

Split a goal, take a hypothesis apart

constructor works on a target whose type has one constructor with several fields, such as p ∧ q or p ↔ q, and leaves one goal per field. In the other direction, cases h takes a hypothesis apart. It produces one goal per constructor of the hypothesis’s type, and in each goal the fields of that constructor become new hypotheses. For h : p ∨ q that means two goals, one with hp : p and one with hq : q:

example (p q : Prop) (h : p ∨ q) : q ∨ p := by
  cases h with
  | inl hp => exact Or.inr hp
  | inr hq => exact Or.inl hq

rcases does the same with nested patterns: ⟨a, b⟩ takes a pair apart and a | b splits an ∨. obtain pattern := h is rcases h with pattern written the other way round. Here one pattern splits an ∧ whose right half is an ∨; left and right choose a side of the ∨ in the target:

example (p q r : Prop) (h : p ∧ (q ∨ r)) : (p ∧ q) ∨ (p ∧ r) := by
  rcases h with ⟨hp, hq | hr⟩
  · left
    exact ⟨hp, hq⟩
  · right
    exact ⟨hp, hr⟩

Witnesses and holes

A proof of ∃ n, P n is a pair of a witness and a proof that it works (Lecture 1). use 7 supplies the witness and then tries to close what is left with rfl and similar easy steps. exists is the core-Lean version. refine is the general tool: you write the whole term, with ?_ wherever you want a new goal.

example : ∃ n : ℕ, n * n = 49 := by
  use 7
 
example : ∃ n : ℕ, 3 < n ∧ n < 5 := by
  exists 4
 
example : ∃ n : ℕ, 3 < n ∧ n < 5 := by
  refine ⟨4, ?_, ?_⟩
  · decide   -- case refine_1: ⊢ 3 < 4
  · decide   -- case refine_2: ⊢ 4 < 5

The difference between ?_ and _ trips up everyone once. ?_ says “this is a new goal; I will prove it next”. _ says “Lean, work this out yourself from the types”. If Lean cannot, refine fails:

example : ∃ n : ℕ, 3 < n ∧ n < 5 := by
  refine ⟨4, _, _⟩

Lean reports don’t know how to synthesize placeholder once for each _, naming the field it belongs to (left, right) and showing the goal the hole stood for (⊢ 3 < 4, ⊢ 4 < 5). A third error, unsolved goals, follows from the first two.

Forward reasoning: have, show, calc

The tactics so far work backwards from the target. Paper proofs often work forwards: “we first show that…”. have h : A := proof proves A and adds h : A to the context. show T restates the target as T, which must be the same type up to computation; it documents the proof and checks that you know what the goal is. calc writes a chain of equalities or inequalities, one justified step per line, where _ stands for the previous right-hand side:

example (x : ℕ) (h : 2 * x = 10) : x = 5 := by
  have h2 : x + x = 10 := by omega
  show x = 5
  omega
 
example (a b : ℕ) (h : a = b + 1) : a * a = b * b + 2 * b + 1 := by
  calc a * a = (b + 1) * (b + 1) := by rw [h]
    _ = b * b + 2 * b + 1 := by ring

The last step uses ring, a Mathlib tactic that proves equations in commutative rings by normalising both sides.

Rewriting: rw and simp

rw [h] with h : a = b replaces every a in the target by b, then tries to close the goal with rfl. rw [← h] rewrites from right to left, rw [h] at h' rewrites a hypothesis instead of the target, and rw [h1, h2] does several rewrites in turn. The equation can be a hypothesis or any lemma from the library.

example (a b c : ℕ) (h1 : a = 2 * b) (h2 : b = c + 1) :
    a = 2 * c + 2 := by
  rw [h1, h2]   -- ⊢ 2 * (c + 1) = 2 * c + 2
  omega

simp rewrites with a whole database of lemmas marked @[simp], again and again, until none applies. It is the tactic to try when the goal looks “obviously simpler” in some direction. simp only [l₁, l₂] uses just the lemmas you list, and simp? runs simp and tells you which lemmas it used, as a simp only call you can paste in:

example (xs ys : List ℕ) :
    (xs ++ ys).length = ys.length + xs.length := by
  simp?
  omega

Here simp? reports Try this: simp only [List.length_append]. The goal it leaves, xs.length + ys.length = ys.length + xs.length, is arithmetic, and omega finishes it. In a finished proof prefer the simp only form when the proof goes on after it: the @[simp] set changes between library versions, and a plain simp in the middle of a proof can then leave a different goal and break the lines after it.

Automation

Five tactics try to close the goal in one step. Four of them either succeed or fail. aesop can stop half way: when its search fails, it warns aesop: failed to prove the goal after exhaustive search and leaves open the goals its safe steps produced.

  • omega proves goals in linear arithmetic over ℕ and ℤ: sums, multiplication by a number, and / and % by a number. It treats a product of two variables as an opaque atom. Its documentation says it is not yet a complete decision procedure, but it rarely fails on goals of this kind.
  • decide works on statements with no free variables that Lean can evaluate, such as 2 ^ 10 = 1024. It runs a decision procedure for the statement (a Decidable instance) and the kernel checks the computation.
  • grind is a core tactic modelled on SMT solvers. Its documentation lists congruence closure, E-matching (instantiating lemmas), case analysis, linear integer arithmetic and commutative-ring normalisation, all working on one shared set of facts.
  • aesop is a best-first proof search: it applies rules (lemmas and tactics), backtracks on failure, and normalises every goal with simp_all first. It is good at the logical glue between facts.
  • exact? searches the context and the library for a single term that closes the goal, and prints it as Try this: exact …. Replace exact? with what it prints: the search is slow, and the result is the proof you want to keep.
example (x y : ℕ) (h : x < y) : 2 * x + 1 < 2 * y := by omega
 
example : 2 ^ 10 = 1024 := by decide
 
example (f : ℕ → ℕ) (a b : ℕ) (h : a = b) (hb : f b = 3) :
    f a = 3 := by
  grind
 
example (p q : Prop) (h : p ∧ q) : q ∧ p := by aesop
 
example (a b : ℕ) (h1 : a ≤ b) (h2 : b ≤ a) : a = b := by exact?

The last line reports Try this: exact Nat.le_antisymm h1 h2. The grind example needs congruence: from a = b it concludes f a = f b, and then f a = 3.

By the remark on trusted tactics (Remark 3.1), automation costs you time, not safety: every one of these produces a term for the kernel. One exception is worth knowing. native_decide evaluates with compiled code instead of the kernel, and #print axioms shows the extra axiom it relies on. The cheatsheet treats it under decide.

Focusing on goals

When a tactic leaves several goals, later tactics act on the first one. Four tools control this:

  • · focuses on the first goal. The block under it must close that goal, or Lean reports an error at the bullet. Use it whenever there is more than one goal.
  • case name => tac works on the goal with that case name, in any order.
  • t <;> s runs t, then runs s on every goal that t produced.
  • all_goals s runs s on every open goal.
example (p q : Prop) (hp : p) (hq : q) : p ∧ q := by
  constructor
  case right => exact hq
  case left => exact hp
 
example (p q : Prop) (hp : p) (hq : q) : p ∧ q ∧ p := by
  refine ⟨?_, ?_, ?_⟩ <;> assumption

assumption closes a goal whose target is one of the hypotheses.

Reading error messages

Lean’s errors are precise, and with practice each points straight at the fix. Here are the four you will see most. Each block below fails on purpose.

Type mismatch. The term has a different type from the one the goal needs:

example (p q : Prop) (hp : p) (hpq : p → q) : q := by
  exact hp

Lean reports Type mismatch: hp has type p but is expected to have type q. The fix is exact hpq hp. Compare the two types in the message: they tell you what is missing.

Unsolved goals. The tactic block ended with goals still open:

example (p q : Prop) (hp : p) (hq : q) : p ∧ q := by
  constructor
  exact hp

The error, reported at by, is unsolved goals, followed by the goal list that remained: case right, with the hypotheses p q : Prop, hp : p, hq : q and the target ⊢ q. The case names tell you which branch you forgot. The card unsolved goals covers the opposite message, “no goals”, which you get by running a tactic after the proof is done.

Unknown identifier. A name Lean does not know, usually a typo or a missing import:

example (a b : ℕ) : a + b = b + a := by
  exact add_comn a b

Lean reports Unknown identifier add_comn; the lemma is add_comm. Because autoImplicit is off in this course, a misspelt variable in a statement gives the same error instead of silently becoming a new variable. (Written as Nat.add_comn, the same typo is reported as Unknown constant instead.)

Failed rewrite. rw [h] needs the left side of h to appear in the target:

example (a b : ℕ) (h1 : a = b) (h2 : b = 5) : a = 5 := by
  rw [h2]

Lean reports Tactic rewrite failed: Did not find an occurrence of the pattern b in the target expression a = 5, and prints the goal. The target mentions a, not b, so there is nothing to rewrite yet. The fix is rw [h1, h2]: first turn a into b, then b into 5.

Try this

  • Prove lift_and again without obtain, using intro hpq h and then exact ⟨hpq h.1, h.2⟩. #print both versions and compare the terms.
  • For P : ℕ → Prop, prove (∃ x : ℕ, x > 3 ∧ x < 5 ∧ P x) → P 4 with obtain, then with rcases. Which hypotheses tell you the witness is 4, and which tactic turns them into an equation?
  • Take the calc example and replace its two steps with one rw [h] followed by ring. Then try grind on the original goal.
  • Write a proof that ends one tactic too early, and one that runs one tactic too many. Read both error messages.

Readings