Stage 06 · Step 29 of 50
Write a chain of reasons.
01The idea
A clear explanation leaves a trail of reasons. A calc proof does the same.
Each line connects to the next with a checked justification.
02In maths
If a = b and b = c, then a + 1 = c + 1.
Picture it
Swipe sideways to see the whole picture.
calc proof walks from a + 1 to c + 1 one link at a time, and every link needs its own reason: rw [h₁] for the first, rw [h₂] for the second. The _ at the start of a line means “where the last line ended”. Because equality is transitive, Lean joins the links into a + 1 = c + 1.03In Lean
This code comes directly from the file checked by Lean.
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₂]
The calc syntax writes a chain of related expressions. Equalities can chain because equality is transitive.
A transitive relation lets a relation from a to b combine with one from b to c.
In a calc chain, an underscore can stand for the previous right-hand expression. It is inferred from the neighboring step.
04Step by step
Choose a line to see Lean’s recorded goal state after that line.
a b c : ℝ h₁ : a = b h₂ : b = c ⊢ a + 1 = c + 1
After line 5: example (a b c : ℝ) (h₁ : a = b) (h₂ : b = c) : a + 1 = c + 1 := by
a b c : ℝ h₁ : a = b h₂ : b = c ⊢ a + 1 = c + 1
After line 6: calc
a b c : ℝ h₁ : a = b h₂ : b = c ⊢ a + 1 = c + 1
After line 7: a + 1 = b + 1 := by rw [h₁]
no goals
After line 8: _ = c + 1 := by rw [h₂]
no goals
05Lean says
no goals
A goal is what remains to prove. The symbol ⊢ points to it. Meet the turnstile.
06A common slip
Lean is telling us something useful here.
example : (1 : Nat) = 1 := by
calc
1 = 2 := by rfl
_ = 1 := by rfl
Tactic `rfl` failed: The left-hand side 1 is not definitionally equal to the right-hand side 2 ⊢ 1 = 2 Tactic `rfl` failed: The left-hand side 2 is not definitionally equal to the right-hand side 1 ⊢ 2 = 1
A calc chain cannot pass through an unjustified false equality.
The fix: Every intermediate link must be true and have its own proof.
07Your turn
sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.
- Warm-up
Write the one-step equality chain using the supplied hypothesis.
Show a hint
Both expressions differ only by a and b.
One more hint
Rewrite with the equality hypothesis.
- A little stretch
Write an inequality chain from a to b to c.
Show a hint
Each supplied hypothesis justifies one link.
One more hint
The relation is transitive, so Lean can combine the links.
module
import Mathlib.Basic.Real.Basic
example (a b : ℝ) (h : a = b) : a + 2 = b + 2 := by
sorry
example (a b c : ℝ) (h₁ : a ≤ b) (h₂ : b ≤ c) : a ≤ c := by
sorry
Compare with the checked solutions
There are often several valid proofs. These solutions were compiled with the same pinned Lean version.
module
import Mathlib.Basic.Real.Basic
example (a b : ℝ) (h : a = b) : a + 2 = b + 2 := by
calc
a + 2 = b + 2 := by rw [h]
example (a b c : ℝ) (h₁ : a ≤ b) (h₂ : b ≤ c) : a ≤ c := by
calc
a ≤ b := h₁
_ ≤ c := h₂
no goals
08A quick check
09New words & tools
Take your time. Progress is yours to mark.