Rewrite through a chain

Record each step of an equality argument in a readable calc proof.

17 minBeginnercalcintermediate expressionsone justification per step
0/13 completed in this course

A single rw hides the route it takes. When an argument has several equalities in a row, a calc block writes each intermediate expression down, and every line supplies its own justification.

The first line of a calc states the first step in full, for example calc a + 1 = b + 1 := by rw [hab]. Each later line starts with an underscore, which stands for the right-hand side of the line before, and states only where the next step goes.

This is more informative than asking automation to normalize everything. A reader sees that the argument first moves from a to b, then from b to c, and when a step is wrong, Lean reports that line and not the whole chain.

The justification of each step is an ordinary proof, so any tactic can appear there: a rewrite, omega for arithmetic, or rfl. The additions by one are deliberately carried through the chain, because rewriting works inside a larger expression.

Worked example

example (x y z : Nat) (hxy : x = y + 1) (hyz : y = z) : x = z + 1 := by
  calc x = y + 1 := by rw [hxy]
    _ = z + 1 := by rw [hyz]

Two steps, each with its own reason. Your exercise has the same shape with the additions on both sides of the goal.

Takeaway

Equality is a substitution rule. calc turns several substitutions into a proof whose intermediate steps remain visible.

Exercise 1 of 3

Use hab and hbc in a two-step calc proof from a + 1 to c + 1.

Suggested steps
  1. Open a calc chain at a + 1
  2. Justify the first step
  3. Continue from _ and justify the second step

These marks only recognize text. Another correct proof may use different steps; Lean checks whether it works.

Exercise.lean
1theorem exercise (a b c : Nat) (hab : a = b) (hbc : b = c) : a + 1 = c + 1 := by
given
Loading editor…

Where you start. Everything above ⊢ may be assumed; the line below it is what you must prove.

a b c:Nat
hab:a = b
hbc:b = c
⊢a + 1 = c + 1
ReadyLn 1, Col 1Lean 4
Draft saved in this browser
Stuck?
Recall and apply

Knowledge check

Answer without looking back, then check your reasoning.

0of 3
correct
Question 1 of 3What does _ mean in the second line of a calc block?
Question 2 of 3Why show the middle expression b + 1?
Question 3 of 3Which tactics may justify a step of a calc chain?