Stage 08 · Step 40 of 50
Capstone: reverse, then reverse again.
01The idea
Turn a row of cards around, then turn it around again. You get the original order.
Proving that fact for your own reversal function brings the course together.
02In maths
For every list xs, reversing it twice returns xs.
Picture it
Swipe sideways to see the whole picture.
a to the back. Reversing again, the helper lemma trailReverse_append swaps the two pieces back and reverses each one, so a returns to the front, and the induction hypothesis ih says the tail, reversed twice, is xs again. What is left, trailReverse [x] ++ xs = x :: xs, holds by rfl.03In Lean
This code comes directly from the file checked by Lean.
def trailReverse : List α → List α
| [] => []
| x :: xs => trailReverse xs ++ [x]
theorem trailReverse_append (xs ys : List α) :
trailReverse (xs ++ ys) = trailReverse ys ++ trailReverse xs := by
induction xs with
| nil => simp [trailReverse]
| cons x xs ih => simp [trailReverse, ih, List.append_assoc]
theorem trailReverse_twice (xs : List α) :
trailReverse (trailReverse xs) = xs := by
induction xs with
| nil => rfl
| cons x xs ih =>
rw [trailReverse, trailReverse_append, ih]
rfl
A helper lemma proves a fact needed by a larger argument. Here it explains reversal of an appended pair of lists.
The helper reverses the order of the two pieces. Its own proof uses induction on the first list.
The main proof then uses induction again. In the case with a head and tail, the helper and the induction hypothesis finish the work.
You have reached a proof about a function you defined yourself. Try rebuilding the two lemmas in the live editor without looking at the solution.
04Step by step
Choose a line to see Lean’s recorded goal state after that line.
α : Type u_1 xs ys : List α ⊢ trailReverse (xs ++ ys) = trailReverse ys ++ trailReverse xs
After line 6: trailReverse (xs ++ ys) = trailReverse ys ++ trailReverse xs := by
α : Type u_1 xs ys : List α ⊢ trailReverse (xs ++ ys) = trailReverse ys ++ trailReverse xs
After line 7: induction xs with
α : Type u_1 xs ys : List α ⊢ trailReverse (xs ++ ys) = trailReverse ys ++ trailReverse xs
After line 8: | nil => simp [trailReverse]
no goals
After line 9: | cons x xs ih => simp [trailReverse, ih, List.append_assoc]
no goals
After line 12: trailReverse (trailReverse xs) = xs := by
α : Type u_1 xs : List α ⊢ trailReverse (trailReverse xs) = xs
After line 13: induction xs with
α : Type u_1 xs : List α ⊢ trailReverse (trailReverse xs) = xs
After line 14: | nil => rfl
no goals
After line 15: | cons x xs ih =>
case cons α : Type u_1 x : α xs : List α ih : trailReverse (trailReverse xs) = xs ⊢ trailReverse (trailReverse (x :: xs)) = x :: xs
After line 16: rw [trailReverse, trailReverse_append, ih]
case cons α : Type u_1 x : α xs : List α ih : trailReverse (trailReverse xs) = xs ⊢ trailReverse [x] ++ xs = x :: xs
After line 17: rfl
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.
def slippedReverse : List Nat → List Nat
| [] => []
| x :: xs => slippedReverse xs ++ [x]
example : slippedReverse [1, 2] = [1, 2] := by
rfl
Tactic `rfl` failed: The left-hand side slippedReverse [1, 2] is not definitionally equal to the right-hand side [1, 2] ⊢ slippedReverse [1, 2] = [1, 2]
Reversing once changes the order of this two-element list.
The fix: The capstone theorem reverses twice before comparing with the original list.
07Your turn
sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.
- Warm-up
Use the supplied recursive reversal and verify the three-item result.
Show a hint
The empty list reverses to itself.
One more hint
Reverse the tail, then append the head as a singleton.
- A little stretch
Verify that the generic reversal preserves a singleton list.
Show a hint
A singleton has an empty tail.
One more hint
The recursive definition computes to the same one-element list.
def rev : List Nat → List Nat
| [] => []
| x :: xs => rev xs ++ [x]
example : rev [1, 2, 3] = [3, 2, 1] := by
sorry
def rev2 : List α → List α
| [] => []
| x :: xs => rev2 xs ++ [x]
example (x : α) : rev2 [x] = [x] := by
sorry
Compare with the checked solutions
There are often several valid proofs. These solutions were compiled with the same pinned Lean version.
def rev : List Nat → List Nat
| [] => []
| x :: xs => rev xs ++ [x]
example : rev [1, 2, 3] = [3, 2, 1] := by
rfl
def rev2 : List α → List α
| [] => []
| x :: xs => rev2 xs ++ [x]
example (x : α) : rev2 [x] = [x] := by
rfl
no goals
08A quick check
09New words & tools
Take your time. Progress is yours to mark.