Proving Facts About Lists in Lean 4: Append, Length and Reverse Twice
Here is a problem. You join two lists, xs ++ ys, and you want to prove the length of the result is the length of xs plus the length of ys. You can test it on a hundred examples with #eval, but tests only check the lists you tried. You want a proof for every possible pair of lists.
Lists are built in steps, just like natural numbers. So the same tool from the last lesson works: induction. This lesson proves the length fact, then defines a reverse function and proves that reversing twice gives back the original list.
How lists are built
A List in Lean has two constructors. [] is the empty list, called nil. x :: xs puts the element x in front of the list xs, called cons. The list [1, 2, 3] is shorthand for 1 :: 2 :: 3 :: [].
Appending is defined by recursion on the first list. The empty list followed by ys is ys. And (x :: xs) ++ ys is x :: (xs ++ ys).
lean-- How List and ++ compute #eval [1, 2] ++ [3, 4] -- [1, 2, 3, 4] #eval (1 :: [2, 3]).length -- 3 example : [1, 2] ++ [3] = 1 :: ([2] ++ [3]) := rfl example (x : Nat) (xs ys : List Nat) : (x :: xs) ++ ys = x :: (xs ++ ys) := rfl
The last line holds by rfl even with unknown lists, because it is the definition. That tells you something useful: since ++ splits on its first argument, induction should be on the first list too.
Induction on a list
For lists, induction has one case per constructor. In the nil case you prove the fact for the empty list. In the cons case you get an element x, a shorter list xs, and an induction hypothesis saying the fact already holds for xs. You then prove it for x :: xs.
leantheorem length_app (xs ys : List Nat) : (xs ++ ys).length = xs.length + ys.length := by induction xs with | nil => rw [List.nil_append, List.length_nil, Nat.zero_add] | cons x xs ih => rw [List.cons_append, List.length_cons, List.length_cons, ih] omega
In the nil case, List.nil_append turns [] ++ ys into ys. List.length_nil says the empty list has length 0. Then Nat.zero_add, the library version of what we proved in lesson 12, removes 0 +.
In the cons case, List.cons_append moves x to the front: the goal now starts with (x :: (xs ++ ys)).length. List.length_cons says a cons has length one more than its tail. It is used twice, once on each side. After that, xs ++ ys appears, and ih replaces its length. What is left is xs.length + ys.length + 1 = xs.length + 1 + ys.length, which omega finishes.
Defining reverse
Now define our own reverse. The reverse of the empty list is empty. The reverse of x :: xs is the reverse of xs with x stuck on the end.
lean-- our own reverse def rev : List Nat → List Nat | [] => [] | x :: xs => rev xs ++ [x] #eval rev [1, 2, 3] -- [3, 2, 1] theorem length_rev (xs : List Nat) : (rev xs).length = xs.length := by induction xs with | nil => rfl | cons x xs ih => rw [rev, List.length_append, ih] rfl
Here rw [rev] unfolds one step of the definition. List.length_append is the library version of the length_app theorem we just proved. After ih, the goal is xs.length + [x].length = (x :: xs).length, and both sides compute to the same number, so rfl closes it.
Animation: rev [1, 2, 3] unfolds one cons cell at a time until it reaches [3, 2, 1].
Reverse twice: you need a helper
The real prize is rev (rev xs) = xs. If you try induction directly, the step leaves you with rev (rev xs ++ [x]), and there is no rule for the reverse of an append. You are stuck.
This is the most common situation in list proofs. The fix is to first prove a more general lemma, then use it. The lemma here: reversing xs ++ ys gives rev ys ++ rev xs.
leandef rev : List Nat → List Nat | [] => [] | x :: xs => rev xs ++ [x] theorem rev_append (xs ys : List Nat) : rev (xs ++ ys) = rev ys ++ rev xs := by induction xs with | nil => rw [List.nil_append, rev, List.append_nil] | cons x xs ih => rw [List.cons_append, rev, rev, ih, List.append_assoc] theorem rev_rev (xs : List Nat) : rev (rev xs) = xs := by induction xs with | nil => rfl | cons x xs ih => rw [rev, rev_append, ih] rfl
rev_append uses two library facts. List.append_nil says l ++ [] = l. List.append_assoc says it does not matter how you group three appends. With the helper in place, rev_rev is short: unfold, apply the helper, use the hypothesis. What remains is rev [x] ++ xs = x :: xs, and rev [x] computes to [x], so rfl closes it.
The library already knows
Everything above exists in Lean's core library for the built-in List.reverse. Its versions are List.length_append, List.reverse_reverse and List.reverse_append, and many are simp rules.
lean-- simp knows the library versions of these lemmas example (xs ys : List Nat) : (xs ++ ys).length = xs.length + ys.length := by simp example (xs : List Nat) : xs.reverse.reverse = xs := by simp #check @List.length_append #check @List.reverse_reverse #check @List.reverse_append
So why prove them yourself? Because when you meet a list fact the library does not have, the method is the same: pick the list the definition recurses on, do induction on it, and when you get stuck, look for a general helper lemma.
Try this
On live.lean-lang.org, define a function that doubles every element and prove it keeps the length.
leandef double : List Nat → List Nat | [] => [] | x :: xs => (2 * x) :: double xs theorem length_double (xs : List Nat) : (double xs).length = xs.length := by induction xs with | nil => rfl | cons x xs ih => rw [double, List.length_cons, List.length_cons, ih]
Then try to prove double (xs ++ ys) = double xs ++ double ys with the same pattern.
Sources
- Theorem Proving in Lean 4, Induction and Recursion: https://lean-lang.org/theorem_proving_in_lean4/induction_and_recursion.html
- Theorem Proving in Lean 4, Tactics: https://lean-lang.org/theorem_proving_in_lean4/tactics.html
- Lean Language Reference: https://lean-lang.org/doc/reference/latest/
- Lean 4 web editor: https://live.lean-lang.org/
Prove a fact about your own list function
`count` counts the elements of a list (it is a homemade length). Prove that counting `xs ++ ys` gives the sum of the two counts, by induction on `xs`.
def count {α : Type} : List α → Nat
| [] => 0
| _ :: t => count t + 1
theorem count_append {α : Type} (xs ys : List α) :
count (xs ++ ys) = count xs + count ys := by
sorry
The official Lean editor runs in your browser and checks the file on Lean's server. Replace every sorry until no errors or warnings are left, then mark it solved.
The worked solution unlocks after your first attempt.
Check what you learned
Answer all 3 questions. You need at least 2 right to pass.
1. Why do induction on the first list when proving facts about xs ++ ys?
2. What are the two cases of induction on a List?
3. Why is rev_append needed before proving rev (rev xs) = xs?