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

6. Tactics as controlled goal transformations

A tactic script constructs a proof term by transforming the proof state. A tactic can introduce a hypothesis, refine a target, create subgoals, or close a goal. The resulting term is checked by Lean’s kernel.

Consider the implication P ∧ Q → Q ∧ P. The tactic proof exposes the intermediate goals of the term proof from the previous chapter.

example (P Q : Prop) : P ∧ Q → Q ∧ P := by
  intro h
  constructor
  · exact h.2
  · exact h.1

intro h adds the premise to the local context. constructor applies the conjunction constructor and creates 2 goals.

Each focusing dot · starts a subproof for one goal. The first proves Q and the second proves P.

Follow the hidden state step by step

A tactic acts on the current proof state, which tracks the open goals and their local contexts. Each goal corresponds to a hole in the proof term. Use the InfoView to see how a tactic changes the goals and which hypotheses are available at each step.

Moving the cursor down the proof above produces this sequence in the InfoView:

-- after `by`
P Q : Prop
⊢ P ∧ Q → Q ∧ P

-- after `intro h`
P Q : Prop
h : P ∧ Q
⊢ Q ∧ P

-- after `constructor`
case left
P Q : Prop
h : P ∧ Q
⊢ Q

case right
P Q : Prop
h : P ∧ Q
⊢ P

-- after `· exact h.2`
case right
P Q : Prop
h : P ∧ Q
⊢ P

For each goal, the lines above ⊢ show the local variables and hypotheses. The expression after ⊢ is the type of the term needed to close that goal.

Inspect the proof term and shared metavariables

Lean builds a proof term from the tactics in a by block. Prefix a tactic or tactic sequence with show_term to see the term it constructs in the InfoView.

example (P Q : Prop) : P ∧ Q → Q ∧ P := by
  show_term
    intro h
    constructor
    · exact h.2
    · exact h.1

In the InfoView, show_term reports exact fun h => ⟨h.right, h.left⟩. Use #print on a named theorem to inspect its finished proof term. Looking at the term can reveal which constructor, function, or elimination rule a tactic applied.

Goals can also share unknown values called metavariables (displayed as ?w or ?m... in the InfoView). Solving one goal can assign a value to a metavariable that also appears in another open goal.

example (a b : Nat) (h : a = b) : ∃ x, x = a ∧ x = b := by
  apply Exists.intro
  constructor
  · rfl
  · exact h

Here apply Exists.intro introduces an unknown witness ?w : Nat. The constructor tactic then creates the goals ?w = a and ?w = b, both of which refer to that witness.

Closing the first goal with rfl assigns ?w := a. This changes the second goal to a = b, which exact h closes. Check the InfoView at the start of each branch to see whether earlier branches have resolved any shared metavariables.

A small working vocabulary

constructor creates a goal for each side of the conjunction. The proof uses h.2 for Q and h.1 for P.

TacticEffectTypical use
intro xIntroduce a function argument or assumptionImplications and universal quantifiers
exact hSupply a term of the target typeFinish a goal using known evidence
apply fMatch the conclusion of f to the target and create goals for its unresolved premisesWork backwards from a theorem
constructorApply a constructorConjunctions and similar targets
cases hSplit according to constructorsDisjunctions, options, lists, equalities
have h : P := ...Establish an intermediate factName a reusable reasoning step
change TReplace the target with a definitionally equal formExpose the intended expression
show TState the target expected at this pointMake inference and intent explicit
example (P Q R : Prop) (hpq : P → Q) (hqr : Q → R) : P → R := by
  intro hp
  have hq : Q := hpq hp
  apply hqr
  exact hq

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

The second proof considers the constructor that produced h. Both branches must build the same target, Q ∨ P, but they have different local evidence.

Read errors as questions about types

“Type mismatch” means the supplied term’s type differs from the expected type. Check the implicit arguments and the direction of any implication involved.

For “Unsolved goals”, inspect each remaining target and its hypotheses. For “Unknown identifier”, check imports, namespace qualification, and spelling.

If apply creates unexpected goals, inspect the theorem with #check. If a short term is hard to understand, use #check @name to expose implicit arguments. Use focusing dots to show which tactics solve each goal in a multi-goal proof.

Temporary holes and completed proofs

During exploration, sorry can stand for a missing proof. Lean may compile the file with a warning because sorry introduces an unchecked assumption.

Fill every hole and audit dependencies where the claim requires it. The workbook checker rejects sorry and admit and treats warnings as errors.

Exercises

  1. Rewrite the term proof of implication composition as a tactic proof.
  2. Prove P ∧ (Q ∧ R) → (P ∧ Q) ∧ R.
  3. Prove P ∨ Q → (P → R) → (Q → R) → R by cases.

For each proof, write down the goal before every tactic. Compare your prediction with the InfoView.