Lean Tactics
Logic and Proof · Higher
In Lean, each step of a proof is a tactic. Each tactic stands for a move we make when writing a proof: substituting, citing a known fact, splitting into cases, giving an example, induction, contradiction.
In Lean, every step of a proof is a tactic. A tactic is not new mathematics. It is one of the moves we already make when we write a proof, such as substituting, splitting into cases or giving an example, written as an instruction Lean can check.
The tactics below are grouped by the mathematical move they stand for. Each one comes with that move, how to write it and a minimal example. In the examples, the "goal" is what still has to be proved and an "assumption" is something we already know.
Equations and substitution
rfl
The two sides of the equation are exactly the same, so the statement holds.
Goal: 37 * x + q = 37 * x + q
rfl -- proof complete
rfl only works when the two sides are written the same way (or become the same when the definitions are worked out). If the sides need rewriting first, use rw.
rw
Substitute using a known equation. If h proves a = b, then rw [h] replaces every a in the goal with b; rw [← h] goes the other way and replaces b with a.
Assumption h : y = x + 7
Goal: 2 * y = 2 * (x + 7)
rw [h] -- the goal becomes 2 * (x + 7) = 2 * (x + 7), and rw closes it with rfl
The brackets can also hold the name of a theorem, such as rw [add_zero]: Lean first finds the first place in the goal of the form ? + 0, say b + 0, and then replaces every b + 0 with b. It also works on an assumption: rw [h] at h2 rewrites the assumption h2.
nth_rewrite
Substitute in one place only. If h : a = b, nth_rewrite 2 [h] replaces only the 2nd occurrence of a with b and leaves the rest alone.
Assumption h : 2 = succ 1
Goal: 2 + 2 = 4
nth_rewrite 2 [h] -- the goal becomes 2 + succ 1 = 4
symm
Swap the two sides of = or ≠: the goal a = b becomes b = a, and a ≠ b becomes b ≠ a. symm at h does the same to the assumption h. < and ≤ cannot be swapped this way.
Using what we know
exact
The statement to prove is exactly something we already know, so we cite it.
Assumption h : x + 1 = 5
Goal: x + 1 = 5
exact h -- proof complete
exact can also take a theorem with its arguments, for example exact add_zero 3 proves 3 + 0 = 3.
apply
Work backwards. We know "if P, then Q" and want Q, so it is enough to prove P.
Assumption h : x = 3 → y = 4
Goal: y = 4
apply h -- the goal becomes x = 3
apply h at h2 works forwards: from P (h2), use h to get Q.
have
Prove an intermediate result first, give it a name and use it later. Use it to break a long proof into steps.
have h3 : x = 2 := by linarith
If …, then …
intro
To prove "if P, then Q," assume P and prove Q under that assumption.
Goal: x = 2 → x + 1 = 3
intro h -- new assumption h : x = 2; the goal becomes x + 1 = 3
intro also proves "for all x, …": take an arbitrary x and show it satisfies the condition. It also proves "not P" (¬ P): assume P, and the goal becomes a contradiction (False).
And
constructor
To prove "A and B," prove A and prove B. The goal splits into two goals, proved one at a time.
Goal: x = 2 ∧ y = 3
constructor -- two goals: x = 2 and y = 3
obtain
If we know "A and B," we know A and we know B, so we split it into two assumptions. If we know "there is a k with P(k)," we take that k together with the condition it satisfies.
Assumption h : x = 2 ∧ y = 3
obtain ⟨h1, h2⟩ := h -- gives h1 : x = 2 and h2 : y = 3
Assumption h : ∃ k, x = 2 * k
obtain ⟨k, hk⟩ := h -- gives k and hk : x = 2 * k
Or
left / right
To prove "A or B," it is enough to prove one of them. left chooses to prove A, right chooses to prove B. If you choose the wrong side, the proof gets stuck and you go back and choose the other one.
rcases
If we know "A or B," we split into two cases: in the first A holds, in the second B holds. The statement must be proved in both cases.
Assumption h : x = 1 ∨ x = 2
rcases h with h1 | h2 -- two goals: one with h1 : x = 1, one with h2 : x = 2
There exists and for all
use
To prove "there is an x such that …," give a specific x and check that it works.
Goal: ∃ k, 7 = 2 * k + 1
use 3 -- the goal becomes 7 = 2 * 3 + 1
If we know "there is an x such that …," use obtain above to take out that x.
specialize
If we know "for all x, …," we may substitute any specific value. specialize h 1 replaces x with 1 in h.
Induction
induction
To prove a statement for every natural number n, prove it for n = 0, then prove "if it holds for d, it holds for d + 1 (that is, succ d)."
induction n with d hd
-- two goals: the base case n = 0 and the inductive step (hd is the induction hypothesis)
Proof by contradiction
by_contra
Assume the statement is false and reach a contradiction. by_contra h adds "the statement is false" as the assumption h, and the goal becomes "a contradiction" (written False).
push_neg
Move a "not" inward: "not true for all x" becomes "there is an x for which it fails," and "there is no x with P" becomes "for all x, P fails."
Calculation
These tactics do calculations and work out inequalities for us. Use them once the idea of the proof is clear and only the computing is left.
norm_num
Works out expressions with specific numbers, such as 2 * 3 + 1 = 7 or 7 ≠ 8. norm_num at h works out the assumption h.
ring
Proves identities: both sides are the same after expanding and combining like terms, such as (a + b) ^ 2 = a ^ 2 + 2 * a * b + b ^ 2.
linarith
Draws conclusions from linear equations and inequalities. For example, from x > 2 and y ≥ x it gets y > 1.
nlinarith
Like linarith, but it also multiplies pairs of known facts together, so it handles some quadratic inequalities.
positivity
Proves an expression is positive or nonnegative, such as x ^ 2 + 1 > 0.
Where to practice
These tactics appear one by one in the course Writing Proofs in Lean, where every section has a proof box you can type them into.
Updated