Your proof-writing toolkit

A tactic for the next step.

Read the goal first. Then choose a tool. These before-and-after states come from the course’s actual Lean files.

Which tool fits this goal?

Look at the goal after ⊢, or at a fact you have. Its shape usually tells you the next move. This is a starting guide, not a rule: often several tactics work.

Finish or rewrite

  • ⊢ 2 + 2 = 4Both sides work out to the same valuerfldecideStep 6
  • h : P ⊢ PSomething you have is exactly the goalexactStep 10
  • h : a = bYou have an equation and want to swap a for brwStep 9

Look at the goal’s main symbol

  • ⊢ P → Q ⊢ ∀ x, …“If…” or “for every…”: assume it, then continueintroStep 11
  • ⊢ P ∧ Q ⊢ P ↔ QTwo things to prove: split into two goalsconstructorStep 12
  • ⊢ P ∨ QPick the side you can proveleftrightStep 14
  • ⊢ ∃ x, …“There is…”: name one exampleexistsStep 20

Use what you have

  • h : P ∧ Q h : P ∨ QUnpack a fact you have, or follow each casecasesobtainStep 15
  • ⊢ ∀ n : ℕ, …True for every natural number: zero, then the next stepinductionStep 22

Let Lean do the arithmetic

  • h : a + 3 ≤ b ⊢ a < bWhole-number arithmetic with +, − and constantsomegaStep 25
  • (a + b)^2 = …Algebra: expand both sides and compareringStep 26
  • x ≤ y, y ≤ 7 ⊢ x ≤ 7Inequalities that follow by adding up ones you havelinarithStep 28
  • ⊢ 3/4 + 1/4 = 1Calculations with specific numbers, even fractionsnorm_numStep 27

Every tactic, with real before-and-after goals

rfl

For our equality goals, close a goal by definitional equality.

Before the selected line
⊢ 2 + 2 = 4
After the selected line
no goals

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
example : 2 + 2 = 4 := by
  rfl

decide

Run a decision procedure and turn its successful result into a proof.

Before the selected line
⊢ 2 + 2 ≠ 5
After the selected line
no goals

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E07.leanTry it live (opens official Lean editor)
example : 2 + 2 ≠ 5 := by
  decide

exact

Supply a term whose type is the current goal.

Before the selected line
P : Prop
h : P
⊢ P
After the selected line
no goals

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E10.leanTry it live (opens official Lean editor)
example (P : Prop) (h : P) : P := by
  exact h

rw

Rewrite with named equalities or supported equivalences.

Before the selected line
a b : Nat
h : a = b
⊢ a + 1 = b + 1
After the selected line
no goals

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
example (a b : Nat) (h : a = b) : a + 1 = b + 1 := by
  rw [h]

intro

Introduce a function input, implication premise, or universally quantified variable.

Before the selected line
P : Prop
⊢ P → P
After the selected line
P : Prop
h : P
⊢ P

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E11.leanTry it live (opens official Lean editor)
example (P : Prop) : P → P := by
  intro h
  exact h

constructor

Apply a constructor for the goal’s type, leaving goals for its required arguments.

Before the selected line
P Q : Prop
hP : P
hQ : Q
⊢ P ∧ Q
After the selected line
case left
P Q : Prop
hP : P
hQ : Q
⊢ P

case right
P Q : Prop
hP : P
hQ : Q
⊢ Q

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E12.leanTry it live (opens official Lean editor)
example (P Q : Prop) (hP : P) (hQ : Q) : P ∧ Q := by
  constructor
  · exact hP
  · exact hQ

cases

Split a value into all its constructor cases.

Before the selected line
P Q : Prop
h : P ∨ Q
⊢ Q ∨ P
After the selected line
P Q : Prop
h : P ∨ Q
⊢ Q ∨ P

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E15.leanTry it live (opens official Lean editor)
example (P Q : Prop) (h : P ∨ Q) : Q ∨ P := by
  cases h with
  | inl hP =>
    right
    exact hP
  | inr hQ =>
    left
    exact hQ

left

Choose the left constructor of an or goal.

Before the selected line
P Q : Prop
hP : P
⊢ P ∨ Q
After the selected line
P Q : Prop
hP : P
⊢ P

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E14.leanTry it live (opens official Lean editor)
example (P Q : Prop) (hP : P) : P ∨ Q := by
  left
  exact hP

example (P Q : Prop) (hQ : Q) : P ∨ Q := by
  right
  exact hQ

apply

Use a theorem with a matching conclusion, leaving its missing premises as goals.

Before the selected line
P : Prop
h : ¬¬P
⊢ P
After the selected line
P : Prop
h : ¬¬P
⊢ ¬P → False

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E17.leanTry it live (opens official Lean editor)
example (P : Prop) (h : ¬ ¬ P) : P := by
  apply Classical.byContradiction
  intro hn
  exact h hn

specialize

Apply a local hypothesis to arguments and replace it with the result.

Before the selected line
P : Nat → Prop
h : ∀ (n : Nat), P n
⊢ P 3
After the selected line
P : Nat → Prop
h : P 3
⊢ P 3

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E19.leanTry it live (opens official Lean editor)
example : ∀ n : Nat, n = n := by
  intro n
  rfl

example (P : Nat → Prop) (h : ∀ n, P n) : P 3 := by
  specialize h 3
  exact h

exists

Supply an existence witness, then try to finish by reflexivity.

Before the selected line
⊢ ∃ n, n + 1 = 4
After the selected line
no goals

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E20.leanTry it live (opens official Lean editor)
example : ∃ n : Nat, n + 1 = 4 := by
  exists 3

induction

Follow constructors, obtaining hypotheses for smaller recursive inputs.

Before the selected line
n : Nat
⊢ 0 + n = n
After the selected line
n : Nat
⊢ 0 + n = n

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E22.leanTry it live (opens official Lean editor)
theorem trail_zero_add (n : Nat) : 0 + n = n := by
  induction n with
  | zero => rfl
  | succ n ih =>
    change Nat.succ (0 + n) = Nat.succ n
    rw [ih]

change

Replace a goal with a definitionally equal expression.

Before the selected line
case succ
n : Nat
ih : 0 + n = n
⊢ 0 + (n + 1) = n + 1
After the selected line
case succ
n : Nat
ih : 0 + n = n
⊢ (0 + n).succ = n.succ

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E22.leanTry it live (opens official Lean editor)
theorem trail_zero_add (n : Nat) : 0 + n = n := by
  induction n with
  | zero => rfl
  | succ n ih =>
    change Nat.succ (0 + n) = Nat.succ n
    rw [ih]

simp

Simplify using rewrite rules and the configured simplifier lemmas.

Before the selected line
α : Type u_1
xs ys : List α
⊢ (xs ++ ys).length = xs.length + ys.length
After the selected line
no goals

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E32.leanTry it live (opens official Lean editor)
theorem trail_length_append (xs ys : List α) :
    (xs ++ ys).length = xs.length + ys.length := by
  induction xs with
  | nil => simp
  | cons x xs ih =>
    simp [ih, Nat.add_assoc, Nat.add_comm]

omega

Prove supported arithmetic claims over Nat and Int.

Before the selected line
a b : Nat
h : a + 3 ≤ b
⊢ a < b
After the selected line
no goals

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E25.leanTry it live (opens official Lean editor)
example (a b : Nat) (h : a + 3 ≤ b) : a < b := by
  omega

ring

Normalize polynomial expressions to prove supported identities.

Before the selected line
a b : ℝ
⊢ (a + b) ^ 2 = a ^ 2 + 2 * a * b + b ^ 2
After the selected line
no goals

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E26.leanTry it live (opens official Lean editor)
module

import Mathlib.Tactic.Ring
import Mathlib.Basic.Real.Basic

example (a b : ℝ) : (a + b)^2 = a^2 + 2*a*b + b^2 := by
  ring

norm_num

Prove supported concrete numerical facts.

Before the selected line
⊢ 3 / 4 + 1 / 4 = 1
After the selected line
no goals

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E27.leanTry it live (opens official Lean editor)
module

import Mathlib.Tactic.NormNum

example : (3 : ℚ) / 4 + 1 / 4 = 1 := by
  norm_num

linarith

Derive a linear arithmetic consequence of the available facts.

Before the selected line
x y : ℚ
h₁ : x ≤ y
h₂ : y ≤ 7
⊢ x ≤ 7
After the selected line
no goals

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E28.leanTry it live (opens official Lean editor)
module

import Mathlib.Tactic.Linarith

example (x y : ℚ) (h₁ : x ≤ y) (h₂ : y ≤ 7) : x ≤ 7 := by
  linarith

calc

Write a chain of relations, with a justification for every link.

Before the selected line
a b c : ℝ
h₁ : a = b
h₂ : b = c
⊢ a + 1 = c + 1
After the selected line
a b c : ℝ
h₁ : a = b
h₂ : b = c
⊢ a + 1 = c + 1

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E29.leanTry it live (opens official Lean editor)
module

import Mathlib.Basic.Real.Basic

example (a b c : ℝ) (h₁ : a = b) (h₂ : b = c) : a + 1 = c + 1 := by
  calc
    a + 1 = b + 1 := by rw [h₁]
    _ = c + 1 := by rw [h₂]

have

Introduce a local proved fact inside a tactic proof.

Before the selected line
a b : ℝ
⊢ 2 * a * b ≤ a ^ 2 + b ^ 2
After the selected line
a b : ℝ
h : 0 ≤ (a - b) ^ 2
⊢ 2 * a * b ≤ a ^ 2 + b ^ 2

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E30.leanTry it live (opens official Lean editor)
module

import Mathlib.Basic.Real.Basic
import Mathlib.Tactic.Linarith

example (a b : ℝ) : 2*a*b ≤ a^2 + b^2 := by
  have h := sq_nonneg (a - b)
  nlinarith

nlinarith

Use arithmetic reasoning with nonlinear preprocessing.

Before the selected line
a b : ℝ
h : 0 ≤ (a - b) ^ 2
⊢ 2 * a * b ≤ a ^ 2 + b ^ 2
After the selected line
no goals

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E30.leanTry it live (opens official Lean editor)
module

import Mathlib.Basic.Real.Basic
import Mathlib.Tactic.Linarith

example (a b : ℝ) : 2*a*b ≤ a^2 + b^2 := by
  have h := sq_nonneg (a - b)
  nlinarith

unfold

Replace a named definition by its defining expression.

Before the selected line
a b : Nat
h : bump a = bump b
⊢ a = b
After the selected line
a b : Nat
h : a + 1 = b + 1
⊢ a = b

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E35.leanTry it live (opens official Lean editor)
def bump (n : Nat) : Nat := n + 1

theorem bump_injective (a b : Nat) (h : bump a = bump b) : a = b := by
  unfold bump at h
  omega

obtain

Unpack a fact into named components, such as a witness and its property.

Before the selected line
a b : ℕ
ha : Even a
hb : Even b
⊢ Even (a + b)
After the selected line
a b : ℕ
hb : Even b
r : ℕ
hr : a = r + r
⊢ Even (a + b)

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E36.leanTry it live (opens official Lean editor)
module

import Mathlib.Algebra.Ring.Parity

example (a b : ℕ) (ha : Even a) (hb : Even b) : Even (a + b) := by
  obtain ⟨r, hr⟩ := ha
  obtain ⟨s, hs⟩ := hb
  refine ⟨r + s, ?_⟩
  omega

refine

Supply a term with holes that become new goals.

Before the selected line
a b r : ℕ
hr : a = r + r
s : ℕ
hs : b = s + s
⊢ Even (a + b)
After the selected line
a b r : ℕ
hr : a = r + r
s : ℕ
hs : b = s + s
⊢ a + b = r + s + (r + s)

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E36.leanTry it live (opens official Lean editor)
module

import Mathlib.Algebra.Ring.Parity

example (a b : ℕ) (ha : Even a) (hb : Even b) : Even (a + b) := by
  obtain ⟨r, hr⟩ := ha
  obtain ⟨s, hs⟩ := hb
  refine ⟨r + s, ?_⟩
  omega

rcases

Split and name components using a case pattern.

Before the selected line
no goals
After the selected line
case inl
n : ℕ
h : n % 2 = 0
⊢ 2 ∣ n * (n + 1)

case inr
n : ℕ
h : n % 2 = 1
⊢ 2 ∣ n * (n + 1)

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
E38.leanTry it live (opens official Lean editor)
module

import Mathlib.Data.Nat.Basic

example (n : ℕ) : 2 ∣ n * (n + 1) := by
  have h : n % 2 = 0 ∨ n % 2 = 1 := by omega
  rcases h with h | h
  · have hn : 2 ∣ n := Nat.dvd_of_mod_eq_zero h
    exact Nat.dvd_mul_right_of_dvd hn (n + 1)
  · have hn : 2 ∣ n + 1 := Nat.dvd_of_mod_eq_zero (by omega)
    exact Nat.dvd_mul_left_of_dvd hn n

<;>

Run the right-hand tactic on every goal produced by the left-hand tactic.

Before the selected line
s : Switch
⊢ flipSwitch (flipSwitch s) = s
After the selected line
no goals

A tactic may open subgoals. Some constructs, including calc and have, also organize a proof.

Read the checked source
Solutions/E34.leanTry it live (opens official Lean editor)
inductive Light where
  | red | green
deriving DecidableEq
example : Light.red ≠ Light.green := by
  decide

inductive Switch where
  | off | on
def flipSwitch : Switch → Switch
  | .off => .on
  | .on => .off
example (s : Switch) : flipSwitch (flipSwitch s) = s := by
  cases s <;> rfl