Stage 06 · Step 29 of 50

Write a chain of reasons.

Needs Mathlib12 minutesLean 4.36.0

01The idea

A clear explanation leaves a trail of reasons. A calc proof does the same.

Lemmi · Your guide for this idea
Lemmi pointing

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

A calc proof is a chain: every link has its own reasonThree boxes in a row: a + 1, b + 1 and c + 1. Between the first two is a chain link marked equals, justified by rw [h₁], which uses h₁ : a = b. Between the second and third is another link, justified by rw [h₂], which uses h₂ : b = c. A green arc over the top joins the two ends: calc joins them into a + 1 = c + 1. A note under the middle box explains that the next calc line is written _ = c + 1, where the underscore stands for b + 1, the end of the previous link.calc joins them: a + 1 = c + 1=by rw [h₁]uses h₁ : a = b=by rw [h₂]uses h₂ : b = ca + 1b + 1c + 1next line _ = c + 1: the _ stands for b + 1

Swipe sideways to see the whole picture.

A 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.

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₂]

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.

After line 5
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

Captured from Lean 4.36.0
no goals

A goal is what remains to prove. The symbol ⊢ points to it. Meet the turnstile.

07Your turn

sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.

  1. Warm-up

    Write the one-step equality chain using the supplied hypothesis.

  2. A little stretch

    Write an inequality chain from a to b to c.

Exercises/E29.leanTry it live (opens official Lean editor)
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

08A quick check

1. What does each calc step require?

Read the answer and explanation

A justification. Every link is a proposition that needs a proof.

2. What does the underscore stand for in the next calc line?

Read the answer and explanation

The preceding right-hand expression. Lean fills in the expression connecting the two adjacent links.

One more idea in your toolkit.

Take your time. Progress is yours to mark.