Stage 08 · Step 40 of 50

Capstone: reverse, then reverse again.

Core Lean12 minutesLean 4.36.0

01The idea

Turn a row of cards around, then turn it around again. You get the original order.

Indy · Your guide for this idea
Indy pointing

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

Reverse twice and you are back where you startedTop: the cards a, b, c, d are reversed to d, c, b, a, and reversed again back to a, b, c, d. Below, the induction step, with head x = a and tail xs = b, c, d. First row: x :: xs. Second row, after one trailReverse: by the definition the tail is reversed and the head goes to the back, giving d, c, b followed by a, which is trailReverse xs ++ [x]. Third row, after the second trailReverse: the helper lemma trailReverse_append swaps the two pieces and reverses each one, so a comes first, and the induction hypothesis turns the tail reversed twice back into b, c, d. That leaves the goal trailReverse [x] ++ xs = x :: xs, which rfl closes.abcddcbaabcdtrailReversetrailReversex :: xs(x is a)abcdxsone trailReverse (the definition):reverse the tail, put the head at the backtrailReverse xs ++ [x]dcbatrailReverse again:trailReverse_append swaps the piecesand reverses each; ih turns the tailreversed twice back into xstrailReverse [x] ++ xsabcd⊢ trailReverse [x] ++ xs = x :: xsrflone card reversed is itself

Swipe sideways to see the whole picture.

Reversing once sends the head card 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.

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

After line 6
α : 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

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

    Use the supplied recursive reversal and verify the three-item result.

  2. A little stretch

    Verify that the generic reversal preserves a singleton list.

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

08A quick check

1. Why prove the append helper first?

Read the answer and explanation

It explains how reversal behaves on the append created by recursion. The main induction encounters an appended singleton. The helper turns that into a usable expression.

2. Why does the theorem cover lists of any element type?

Read the answer and explanation

It only changes order, without inspecting element values. The type variable is arbitrary. No property of the elements is required.

One more idea in your toolkit.

Take your time. Progress is yours to mark.