Equality with an unknown input

Prove n + 0 = n and learn when computation can work with a variable.

13 minBeginnertheoremgoal statetactic moderfl
0/13 completed in this course

In Lean foundations every claim was about concrete values, such as 4 + 4 = 8. This course is about claims for every value, and about how a proof is built, one step at a time. Here (n : Nat) before the colon means the theorem must hold for any natural number n. You cannot pick a convenient one.

Read the goal state from top to bottom: n : Nat is the available value, and ⊢ n + 0 = n is the claim to prove. The statement is a law about every n, not a test at one input.

For equality, rfl works when both sides reduce to the same expression. The definition of natural-number addition computes by inspecting its second argument. In n + 0, that argument is already zero, so the expression reduces to n even though n itself is unknown.

Order matters for computation. 0 + n is stuck on its unknown second argument; rfl does not prove 0 + n = n. The equality is still true, but proving it takes reasoning rather than computation; Proofs about your programs introduces a tactic for exactly that.

Worked example

-- Example 1: both sides compute to 10, so rfl succeeds.
example : 2 * 5 = 10 := by
  rfl

-- Example 2 (commented out, because it fails): n + 1 = 1 + n is true for
-- every n, but the two sides do not reduce to the same expression. This is
-- the message rfl reports:
--   example (n : Nat) : n + 1 = 1 + n := by rfl
--
--   Tactic `rfl` failed: The left-hand side
--     n + 1
--   is not definitionally equal to the right-hand side
--     1 + n
--   n : Nat
--   ⊢ n + 1 = 1 + n
Step through the proof1 / 2

At the start of the proof on line 2. Everything above ⊢ may be used; the line below it is what must be proved.

⊢2 * 5 = 10

An example is a theorem without a name, checked exactly the same way. Example 1 computes. Example 2 cannot, so it is shown as a comment together with Lean’s reply; the message prints the two sides Lean was left holding. Reading those two lines is a faster habit than trying another tactic.

Takeaway

A variable does not always stop computation. For an equality, ask whether both sides reduce to the same expression; n + 0 does, even when n is unknown.

Exercise 1 of 2

Prove that adding zero on the right leaves every natural number unchanged.

Suggested steps
  1. Close the goal by computation

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

Exercise.lean
1theorem exercise (n : Nat) : n + 0 = n := by
given
Loading editor…

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

n:Nat
⊢n + 0 = n
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 rfl check?
Question 2 of 3Which of these can rfl close?
Question 3 of 3What sits above the ⊢ line in the Goal panel?