Glossary of tactics
Tactics that are not used in the course but are common in practice are in the
More tactics.
Implications in Lean
exact |
If
P is the target of the current goal
and hp is a term of type P,
then exact hp will close the goal.
Mathematically, this saying “this is exactly what we were required to prove”.
|
intro |
If the target of the current goal is a function P → Q,
then intro hp will produce a hypothesis
hp : P and change the target to Q.
Mathematically, this is saying that in order to define a function from P to Q,
we first need to choose an arbitrary element of P.
|
have |
have is used to create intermediate variables.
If f is a term of type P → Q and
hp is a term of type P, then
have hq := f hp creates the hypothesis hq : Q .
have hp : P creates a new goal with target P and
adds hp : P as a hypothesis to the original goal.
Use this when you know the type of the term you want but not (yet) the term itself.
|
apply |
apply is used for backward reasoning.
If the target of the current goal is Q and
f is a term of type P → Q, then
apply f changes target to P.
Mathematically, this is equivalent to saying “because P implies Q, to prove Q it suffices to prove P”.
|
Proof by contradiction
exfalso |
Changes the target of the current goal to False.
The name derives from “ex falso, quodlibet” which translates to “from contradiction, anything”.
You should use this tactic when there are contradictory hypotheses present.
|
by_cases |
If P : Prop, then by_cases hp : P creates two goals,
the first with a hypothesis hp : P and second with a hypothesis hp : ¬ P.
Mathematically, this is saying either P is true or P is false.
by_cases is the most direct application of the law of excluded middle.
|
by_contra |
If the target of the current goal is Q,
then by_contra changes the target to False and
adds hnq : ¬ Q as a hypothesis.
Mathematically, this is proof by contradiction.
|
push Not |
push Not simplifies negations in the target.
For example, if the target of the current goal is ¬ ¬ P, then
push Not simplifies it to P.
You can also push negations across a hypothesis hp : P using push Not at hp.
|
contrapose! |
If the target of the current goal is P → Q,
then contrapose! changes the target to ¬ Q → ¬ P.
If the target of the current goal is Q
and one of the hypotheses is hp : P,
then contrapose! hp changes the target to ¬ P
and changes the hypothesis to hp : ¬ Q.
Mathematically, this is replacing the target by its contrapositive.
|
And / Or
obtain |
obtain is a general tactic that breaks a complicated term into simpler ones.
If hpq is a term of type P ∧ Q, then
obtain ⟨hp, hq⟩ := hpq breaks it into hp : P and hq : Q.
If fg is a term of type P ↔ Q, then
obtain ⟨f, g⟩ := fg breaks it into f : P → Q and g : Q → P.
If hpq is a term of type P ∨ Q, then
obtain hp | hq := hpq creates two goals and adds the hypothesis hp : P in one and hq : Q in the other.
|
constructor |
constructor is a general tactic that breaks a complicated goal into simpler ones.
If the target of the current goal is P ∧ Q, then
constructor breaks up the goal into two goals with targets P and Q.
If the target of the current goal is P ↔ Q, then
constructor breaks up the goal into two goals with targets P → Q and Q → P.
|
left |
If the target of the current goal is P ∨ Q, then
left changes the target to P. |
right |
If the target of the current goal is P ∨ Q, then
right changes the target to Q. |
Quantifiers
have |
If hp is a term of type ∀ x : X, P x and
y is a term of type X then
have hpy := hp y creates a hypothesis hpy : P y. |
intro |
If the target of the current goal is ∀ x : X, P x, then
intro x creates a hypothesis x : X and
changes the target to P x. |
obtain |
If hp is a term of type ∃ x : X, P x, then
obtain ⟨x, key⟩ := hp breaks it into
x : X and key : P x. |
use |
If the target of the current goal is ∃ x : X, P x
and y is a term of type X, then
use y changes the target to P y and tries to close the goal. |
Proving “trivial” statements
norm_num |
norm_num is Lean’s calculator. If the target has a proof that involves only numbers and arithmetic operations,
then norm_num will close this goal.
If hp : P is an assumption then norm_num at hp tries to simplify hp using basic arithmetic operations.
norm_num [h1, h2] also uses the hypotheses or lemmas h1, h2 as rewriting rules.
|
ring |
ring is Lean’s symbolic manipulator.
If the target has a proof that involves only algebraic operations,
then ring will close the goal.
If hp : P is an assumption then ring at hp tries to simplify hp using basic algebraic operations.
|
linarith |
linarith is Lean’s inequality solver.
It uses the hypotheses in context to prove goals that follow from them by adding up linear (in)equalities.
It cannot handle products of variables such as x * y.
|
omega |
omega is Lean’s solver for linear arithmetic over ℕ and ℤ.
It uses all the hypotheses in context and either closes the goal or fails.
Unlike linarith it understands integers, so it knows things like x < 1 → x = 0
and can handle ℕ subtraction, / and % by numerals, and ≠.
It cannot handle products of variables such as x * y.
|
simp |
simp is a very complex tactic that tries to use theorems from the mathlib library to close the goal.
You should only ever use simp to close a goal because its behavior changes as more theorems get added to the library.
|
Equality
rw |
If f is a term of type P = Q (or P ↔ Q), then
rw [f] searches for P in the target and replaces it with Q.
rw [← f] searches for Q in the target and replaces it with P.
If additionally, hr : R is a hypothesis, then
rw [f] at hr searches for P in the expression R and replaces it with Q.
rw [← f] at hr searches for Q in the expression R and replaces it with P.
Mathematically, this is saying because P = Q, we can replace P with Q (or the other way around).
|
symm |
If the target of the current goal is P = Q, then
symm changes the target to Q = P. |
unfold |
unfold f replaces f in the target by its definition.
unfold f at hp does the same in the hypothesis hp.
For example, unfold Function.Surjective turns Surjective f
into ∀ y, ∃ x, f x = y.
|
induction |
If n : ℕ then induction n with | zero => ... | succ k ih => ...
splits the goal into the base case n = 0 and the inductive step,
where ih is the statement for k and the target is the statement for k + 1. |
set |
set x := e with hx introduces a new name x for the expression e,
replaces every e in the target and the hypotheses with x,
and adds the hypothesis hx : x = e.
Mathematically, this is saying “let x denote e”.
|
norm_cast |
norm_cast tries to clear out coercions between types (e.g. ℕ → ℤ or ℕ → ℚ).
norm_cast at hp tries to clear out coercions in the hypothesis hp.
|
push_cast |
push_cast does the opposite of norm_cast: it pushes coercions towards the variables,
e.g. it rewrites ↑(a + b) as ↑a + ↑b.
push_cast at hp does the same at the hypothesis hp.
|