Tactic Mode in Lean 4: Proving Step by Step With the Goal View

4 min read

Here is a problem. Term proofs work, but as statements grow they get hard to write in one go. You have to hold the whole proof in your head, nested brackets and all, and Lean only tells you at the end that something does not fit. It would be easier to work step by step, with Lean showing you what is left to prove after each step.

That is what tactic mode gives you. You write by, and then a list of commands called tactics. Each tactic changes the current goal, and the editor shows you the new goal right away.

The same proof, two ways

Start with a fact you already know: if p and q are both true, then q and p are both true. Here it is as a term proof and as a tactic proof.

lean
-- Term proof and tactic proof of the same fact
theorem and_swap_term (p q : Prop) : p ∧ q → q ∧ p :=
  fun h => ⟨h.2, h.1⟩

theorem and_swap_tac (p q : Prop) : p ∧ q → q ∧ p := by
  intro h
  constructor
  · exact h.2
  · exact h.1

Both are accepted, and both produce a proof term in the end. Tactics are a way of building that term with help from Lean, not a different kind of proof.

Reading the goal view

When your cursor sits inside a tactic block, the infoview panel shows the goal. In VS Code it opens on the right side, and live.lean-lang.org shows it next to the editor. At the start of and_swap_tac it looks like this:

text
p q : Prop
⊢ p ∧ q → q ∧ p

Everything above the ⊢ symbol is what you have: variables and hypotheses. The line after ⊢ is what you still need to prove. Learning to read this panel is most of learning tactics. After every line, look at it again.

intro: take the hypothesis

When the goal is an implication A → B, the tactic intro h moves A above the line as a hypothesis named h. The goal becomes B. It also works for "for all" goals, where it introduces the variable.

After intro h the view is:

text
p q : Prop
h : p ∧ q
⊢ q ∧ p

constructor: split the goal

The goal q ∧ p is built from two pieces. The tactic constructor applies the constructor of And, which leaves two goals: q and then p. The infoview now lists both, labelled case left and case right.

The dots · focus on one goal at a time. You can type the dot as \. in the editor. Inside each focused goal, exact h.2 says "this term is exactly the proof you need". h.2 is the second half of h, a proof of q.

Animation: The goal view after each tactic in and_swap_tac, as Lean 4 prints it, down to No goals.

apply: work backwards

exact needs a term that matches the goal completely. apply is looser. If you have hqr : q → r and the goal is r, then apply hqr says "it is enough to prove q". The goal changes from r to q.

lean
-- apply works backwards from the goal
example (p q r : Prop) (hpq : p → q) (hqr : q → r) (hp : p) : r := by
  apply hqr
  apply hpq
  exact hp

Read the goal after each line: r, then q, then p, and finally nothing. This backwards style is common in real proofs. You start from what you want and keep asking "what would be enough?"

cases: open a hypothesis

constructor breaks up a goal. cases breaks up a hypothesis. If you have h : p ∧ q, then cases h gives you its two parts as separate hypotheses. If you have h : p ∨ q, it gives you two goals, one where p holds and one where q holds.

lean
-- cases splits a hypothesis into its pieces
example (p q : Prop) : p ∧ q → q := by
  intro h
  cases h with
  | intro hp hq => exact hq

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

The names after | are the constructors of the type. And has one constructor, intro, with two fields. Or has two, inl and inr, so you get two branches.

Exists in tactic mode

The ∃ ideas from the last lesson carry over. To prove an ∃ goal, give the witness and proof with exact. To use an ∃ hypothesis, open it with cases, which hands you the witness and the fact about it.

lean
-- ∃ in tactic mode: exact with a witness, or take one apart with cases
example : ∃ n : Nat, n + 2 = 5 := by
  exact ⟨3, rfl⟩

example (h : ∃ n : Nat, n > 7) : ∃ m : Nat, m > 6 := by
  cases h with
  | intro n hn => exact ⟨n, Nat.lt_of_succ_lt hn⟩

When you stop too early

If the tactic block ends while goals remain, Lean says so and shows you exactly what is missing.

lean
-- Stopping early: Lean reports the goal that is still open
example (p q : Prop) (hp : p) (hq : q) : p ∧ q := by
  constructor
  · exact hp

The error is unsolved goals, followed by case right and the line ⊢ q. This message is not a failure to be afraid of. It is the most useful message Lean gives you, because it tells you the next thing to do.

Try this

Paste this into live.lean-lang.org and click after each line to watch the goal change.

lean
example (p q : Prop) (hp : p) : p ∨ q := by
  apply Or.inl
  exact hp

Then try proving q ∨ p from the same hp. Which of Or.inl and Or.inr do you need now?

Sources