2.8. Well-Founded Recursion

So far, all the recursive functions we have written were structurally recursive: each recursive call was made on an immediate subterm of the argument — the tail of a list, the predecessor of a number, a subtree. Lean recognizes this pattern automatically and thereby guarantees that the function terminates. As we saw in the chapter on functional programming, termination is no mere detail: a non-terminating function could introduce contradictory equations and make the logic inconsistent.

Many natural algorithms, however, do not follow this pattern. Division by repeated subtraction calls itself on n - d, which is not a subterm of n; merge sort splits the list into two halves, which are not subterms of it either. For such cases, Lean offers well-founded recursion: the programmer specifies a measure that decreases with each recursive call, according to a well-founded relation (that is, one with no infinite descending chains), and proves — or lets Lean prove — that it does indeed decrease.

In this chapter we study well-founded recursion through three examples: Euclidean division, merging sorted lists, and merge sort. For each of them, we prove the implementation correct. We will also see that proofs about functions defined by well-founded recursion can themselves be written by well-founded recursion.

namespace Ch15

2.8.1. Termination measures

A relation < on a type is well-founded when there is no infinite sequence a_0 > a_1 > a_2 > \cdots. The usual order on the natural numbers is the fundamental example. If, at each recursive call of f, a measure \mu(\text{arguments}) \in \mathbb{N} strictly decreases, then there cannot be an infinite sequence of calls, and f terminates.

In Lean, the measure is given by the termination_by clause, and the proof that it decreases by the decreasing_by clause. If decreasing_by is omitted, Lean tries to prove the decrease automatically with the decreasing_tactic tactic, which combines simplification and arithmetic. When not even termination_by is given, Lean tries to guess a suitable measure among the function's arguments.

The Ackermann function is a classic example of a function that terminates but is not structurally recursive in any of its arguments taken in isolation: the third equation calls ack with first argument m and second argument ack (m + 1) n, which may be huge. The right measure is the pair (m, n), with the lexicographic order: either the first component decreases, or it stays the same and the second one decreases.

def ack : ℕ → ℕ → ℕ | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by m n => (m, n) 9#eval ack 2 3
9

If we need a function that might not terminate, we can mark it as partial. Lean then accepts the definition without a termination proof, but treats it as opaque: nothing can be proved about its behavior. An example is the function that counts the steps of the Collatz sequence until it reaches 1 — nobody knows how to prove that it terminates for every n!

partial def collatz (n : ℕ) : ℕ := if n ≤ 1 then 0 else if n % 2 = 0 then 1 + collatz (n / 2) else 1 + collatz (3 * n + 1) 111#eval collatz 27
111

2.8.2. Euclidean division

The division of n by d can be computed by repeated subtraction: if n < d, the quotient is zero and the remainder is n; otherwise, we subtract d from n, compute recursively, and add one to the quotient. By convention, dividing by zero yields quotient zero and remainder n.

def natDiv (n d : ℕ) : ℕ := if d = 0 ∨ n < d then 0 else 1 + natDiv (n - d) d termination_by n decreasing_by All goals completed! 🐙 def natMod (n d : ℕ) : ℕ := if d = 0 ∨ n < d then n else natMod (n - d) d termination_by n decreasing_by All goals completed! 🐙

The recursive call is made on n - d, which is not a subterm of n. The measure is n itself, and the goal Lean generates in decreasing_by is n - d < n, under the hypothesis ¬ (d = 0 ∨ n < d) — available because we are in the else branch of the if. The omega tactic solves this goal: since d ≠ 0 and d ≤ n, we do have n - d < n. Note that without the condition d = 0 the function would not terminate when d = 0, since n - 0 = n.

(3, 2)#eval (natDiv 17 5, natMod 17 5)
(3, 2)

2.8.2.1. Testing before proving

The specification of Euclidean division is the equation d * q + r = n, with r < d when d ≠ 0. Before proving it, we test it with Plausible:

open Plausible in Unable to find a counter-example#eval Testable.check (∀ n d : ℕ, d * natDiv n d + natMod n d = n)
Unable to find a counter-example

2.8.2.2. Unfolding equations

Functions defined by well-founded recursion do not reduce by definitional computation the way structural ones do; instead, Lean generates equations that characterize them. For natDiv, the equation natDiv.eq_1 states that natDiv n d equals the body of the definition. Combining it with if_pos and if_neg, we obtain lemmas describing the two cases of the function:

theorem natDiv_base (n d : ℕ) (h : d = 0 ∨ n < d) : natDiv n d = 0 := n:ℕd:ℕh:d = 0 ∨ n < d⊢ natDiv n d = 0 All goals completed! 🐙 theorem natDiv_step (n d : ℕ) (h : ¬ (d = 0 ∨ n < d)) : natDiv n d = 1 + natDiv (n - d) d := n:ℕd:ℕh:¬(d = 0 ∨ n < d)⊢ natDiv n d = 1 + natDiv (n - d) d All goals completed! 🐙 theorem natMod_base (n d : ℕ) (h : d = 0 ∨ n < d) : natMod n d = n := n:ℕd:ℕh:d = 0 ∨ n < d⊢ natMod n d = n All goals completed! 🐙 theorem natMod_step (n d : ℕ) (h : ¬ (d = 0 ∨ n < d)) : natMod n d = natMod (n - d) d := n:ℕd:ℕh:¬(d = 0 ∨ n < d)⊢ natMod n d = natMod (n - d) d All goals completed! 🐙

2.8.2.3. Proofs by well-founded recursion

How do we prove a property of natMod? The structure of the proof should mirror that of the function: in the base case, the property follows directly; in the recursive case, we want to use the property for n - d. This is an induction, but not a structural induction on n — the "induction hypothesis" is for n - d, not for n - 1.

The most direct way to do this in Lean is to write the theorem as a recursive function, invoking it for n - d, and to justify termination with the same measure as the function. The theorem below states that the remainder is smaller than the divisor:

theorem natMod_lt (n d : ℕ) (hd : d ≠ 0) : natMod n d < d := n:ℕd:ℕhd:d ≠ 0⊢ natMod n d < d n:ℕd:ℕhd:d ≠ 0h:d = 0 ∨ n < d⊢ natMod n d < dn:ℕd:ℕhd:d ≠ 0h:¬(d = 0 ∨ n < d)⊢ natMod n d < d n:ℕd:ℕhd:d ≠ 0h:d = 0 ∨ n < d⊢ natMod n d < d n:ℕd:ℕhd:d ≠ 0h:d = 0 ∨ n < d⊢ n < d All goals completed! 🐙 n:ℕd:ℕhd:d ≠ 0h:¬(d = 0 ∨ n < d)⊢ natMod n d < d n:ℕd:ℕhd:d ≠ 0h:¬(d = 0 ∨ n < d)⊢ natMod (n - d) d < d All goals completed! 🐙 termination_by n decreasing_by All goals completed! 🐙

In the base case, natMod n d = n and, since d ≠ 0, the hypothesis h guarantees n < d. In the recursive case, natMod n d = natMod (n - d) d, and the recursive call natMod_lt (n - d) d hd provides exactly what we need. The termination_by n clause guarantees that this "induction hypothesis" is legitimate.

The fundamental equation of division is proved in the same way. In the recursive case, we rewrite with the step equations and obtain the goal d * (1 + natDiv (n - d) d) + natMod (n - d) d = n. The recursive call provides d * natDiv (n - d) d + natMod (n - d) d = n - d; distributing the multiplication and using d ≤ n (which follows from h), the goal follows by linear arithmetic.

theorem div_spec (n d : ℕ) : d * natDiv n d + natMod n d = n := n:ℕd:ℕ⊢ d * natDiv n d + natMod n d = n n:ℕd:ℕh:d = 0 ∨ n < d⊢ d * natDiv n d + natMod n d = nn:ℕd:ℕh:¬(d = 0 ∨ n < d)⊢ d * natDiv n d + natMod n d = n n:ℕd:ℕh:d = 0 ∨ n < d⊢ d * natDiv n d + natMod n d = n n:ℕd:ℕh:d = 0 ∨ n < d⊢ d * 0 + n = n All goals completed! 🐙 n:ℕd:ℕh:¬(d = 0 ∨ n < d)⊢ d * natDiv n d + natMod n d = n n:ℕd:ℕh:¬(d = 0 ∨ n < d)⊢ d * (1 + natDiv (n - d) d) + natMod (n - d) d = n n:ℕd:ℕh:¬(d = 0 ∨ n < d)ih:d * natDiv (n - d) d + natMod (n - d) d = n - d⊢ d * (1 + natDiv (n - d) d) + natMod (n - d) d = n have hge : d ≤ n := n:ℕd:ℕ⊢ d * natDiv n d + natMod n d = n All goals completed! 🐙 n:ℕd:ℕh:¬(d = 0 ∨ n < d)ih:d * natDiv (n - d) d + natMod (n - d) d = n - dhge:d ≤ n⊢ d + d * natDiv (n - d) d + natMod (n - d) d = n All goals completed! 🐙 termination_by n decreasing_by All goals completed! 🐙

Note that omega treats natDiv (n - d) d and natMod (n - d) d as opaque variables: after rewriting with Nat.mul_add, the goal is a linear combination of these variables, and the hypothesis ih provides the required relation.

2.8.3. Merging sorted lists

The second example is the function merge, which merges two sorted lists into a single sorted list by comparing the heads and taking the smaller one.

def merge : List ℕ → List ℕ → List ℕ | [], ys => ys | x :: xs, [] => x :: xs | x :: xs, y :: ys => if x ≤ y then x :: merge xs (y :: ys) else y :: merge (x :: xs) ys termination_by xs ys => xs.length + ys.length

Here neither argument decreases in every call: in the first recursive call, the first list shrinks and the second stays the same; in the second, the opposite happens. The right measure is the sum of the lengths, which decreases in both cases. Lean can prove the decrease on its own, so we omit decreasing_by. (In fact, Lean would also find the lexicographic order on the two arguments without our help; we give the measure to make the argument explicit.)

[1, 2, 3, 4, 5, 6]#eval merge [1, 3, 5] [2, 4, 6]
[1, 2, 3, 4, 5, 6]

As before, we extract lemmas for the recursive case from the equation generated by Lean:

theorem merge_le (x : ℕ) (xs : List ℕ) (y : ℕ) (ys : List ℕ) (h : x ≤ y) : merge (x :: xs) (y :: ys) = x :: merge xs (y :: ys) := x:ℕxs:List ℕy:ℕys:List ℕh:x ≤ y⊢ merge (x :: xs) (y :: ys) = x :: merge xs (y :: ys) All goals completed! 🐙 theorem merge_gt (x : ℕ) (xs : List ℕ) (y : ℕ) (ys : List ℕ) (h : ¬ x ≤ y) : merge (x :: xs) (y :: ys) = y :: merge (x :: xs) ys := x:ℕxs:List ℕy:ℕys:List ℕh:¬x ≤ y⊢ merge (x :: xs) (y :: ys) = y :: merge (x :: xs) ys All goals completed! 🐙

2.8.3.1. Specification

The specification uses the inductive predicates Sorted and Perm from the previous chapters:

inductive Sorted : List ℕ → Prop where | nil : Sorted [] | single (x : ℕ) : Sorted [x] | two_or_more (x y : ℕ) {zs : List ℕ} (hle : x ≤ y) (hs : Sorted (y :: zs)) : Sorted (x :: y :: zs) inductive Perm {α : Type} : List α → List α → Prop where | nil : Perm [] [] | skip (x : α) {xs ys : List α} : Perm xs ys → Perm (x :: xs) (x :: ys) | swap (x y : α) (xs : List α) : Perm (x :: y :: xs) (y :: x :: xs) | trans {xs ys zs : List α} : Perm xs ys → Perm ys zs → Perm xs zs local infix:50 " ~ " => Perm theorem Perm_refl {α : Type} (xs : List α) : xs ~ xs := α:Typexs:List α⊢ xs ~ xs induction xs with α:Type⊢ [] ~ [] All goals completed! 🐙 α:Typex:αxs:List αih:xs ~ xs⊢ x :: xs ~ x :: xs All goals completed! 🐙 theorem Perm_sym {α : Type} {xs ys : List α} (h : xs ~ ys) : ys ~ xs := α:Typexs:List αys:List αh:xs ~ ys⊢ ys ~ xs induction h with α:Typexs:List αys:List α⊢ [] ~ [] All goals completed! 🐙 α:Typexs:List αys:List αx:αxs_1:List αys_1:List αa:xs_1 ~ ys_1ih:ys_1 ~ xs_1⊢ x :: ys_1 ~ x :: xs_1 All goals completed! 🐙 α:Typexs:List αys:List αx:αy:αzs:List α⊢ y :: x :: zs ~ x :: y :: zs All goals completed! 🐙 α:Typexs:List αys:List αxs_1:List αys_1:List αzs:List αa:xs_1 ~ ys_1a_1:ys_1 ~ zsih1:ys_1 ~ xs_1ih2:zs ~ ys_1⊢ zs ~ xs_1 All goals completed! 🐙

2.8.3.2. merge produces a sorted list

When proving that merge xs ys is sorted, the interesting case is x :: merge xs (y :: ys) (when x ≤ y). We know, by the induction hypothesis, that merge xs (y :: ys) is sorted, but to put x in front we need to know that x is less than or equal to the head of that list — and we do not know, a priori, what that head is. The way out is to prove something stronger: x is a lower bound for all the elements of merge xs (y :: ys). For this we use two auxiliary lemmas.

The first says that, in a sorted list x :: xs, the head is a lower bound for all the elements of the tail. The proof is by induction on xs, using the transitivity of ≤:

theorem sorted_head_le : ∀ (x : ℕ) (xs : List ℕ), Sorted (x :: xs) → ∀ z ∈ xs, x ≤ z := ⊢ ∀ (x : ℕ) (xs : List ℕ), Sorted (x :: xs) → ∀ z ∈ xs, x ≤ z intro x x:ℕxs:List ℕ⊢ Sorted (x :: xs) → ∀ z ∈ xs, x ≤ z induction xs generalizing x with x:ℕ⊢ Sorted [x] → ∀ z ∈ [], x ≤ z intro _ x:ℕa:Sorted [x]z:ℕ⊢ z ∈ [] → x ≤ z x:ℕa:Sorted [x]z:ℕhz:z ∈ []⊢ x ≤ z All goals completed! 🐙 y:ℕys:List ℕih:∀ (x : ℕ), Sorted (x :: ys) → ∀ z ∈ ys, x ≤ zx:ℕ⊢ Sorted (x :: y :: ys) → ∀ z ∈ y :: ys, x ≤ z intro hs y:ℕys:List ℕih:∀ (x : ℕ), Sorted (x :: ys) → ∀ z ∈ ys, x ≤ zx:ℕhs:Sorted (x :: y :: ys)z:ℕ⊢ z ∈ y :: ys → x ≤ z y:ℕys:List ℕih:∀ (x : ℕ), Sorted (x :: ys) → ∀ z ∈ ys, x ≤ zx:ℕhs:Sorted (x :: y :: ys)z:ℕhz:z ∈ y :: ys⊢ x ≤ z cases hs with y:ℕys:List ℕih:∀ (x : ℕ), Sorted (x :: ys) → ∀ z ∈ ys, x ≤ zx:ℕz:ℕhz:z ∈ y :: yshle:x ≤ yhs':Sorted (y :: ys)⊢ x ≤ z y:ℕys:List ℕih:∀ (x : ℕ), Sorted (x :: ys) → ∀ z ∈ ys, x ≤ zx:ℕz:ℕhz:z = y ∨ z ∈ yshle:x ≤ yhs':Sorted (y :: ys)⊢ x ≤ z ys:List ℕih:∀ (x : ℕ), Sorted (x :: ys) → ∀ z ∈ ys, x ≤ zx:ℕz:ℕhle:x ≤ zhs':Sorted (z :: ys)⊢ x ≤ zy:ℕys:List ℕih:∀ (x : ℕ), Sorted (x :: ys) → ∀ z ∈ ys, x ≤ zx:ℕz:ℕhle:x ≤ yhs':Sorted (y :: ys)hmem:z ∈ ys⊢ x ≤ z ys:List ℕih:∀ (x : ℕ), Sorted (x :: ys) → ∀ z ∈ ys, x ≤ zx:ℕz:ℕhle:x ≤ zhs':Sorted (z :: ys)⊢ x ≤ z All goals completed! 🐙 y:ℕys:List ℕih:∀ (x : ℕ), Sorted (x :: ys) → ∀ z ∈ ys, x ≤ zx:ℕz:ℕhle:x ≤ yhs':Sorted (y :: ys)hmem:z ∈ ys⊢ x ≤ z All goals completed! 🐙

The second says that, if a is a lower bound for all the elements of xs and of ys, then it is a lower bound for all the elements of merge xs ys — after all, merge does not create new elements. The proof is a nested induction: induction on xs and, in the x :: xs case, induction on ys. This form of induction follows the recursion of merge, in which, at each call, either xs or ys shrinks. In the x :: xs, y :: ys case, we distinguish x ≤ y (we use the outer induction hypothesis, ihxs) and ¬ x ≤ y (we use the inner one, ihys).

theorem merge_lb (a : ℕ) : ∀ (xs ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ z := a:ℕ⊢ ∀ (xs ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ z a:ℕxs:List ℕ⊢ ∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ z induction xs with a:ℕ⊢ ∀ (ys : List ℕ), (∀ z ∈ [], a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge [] ys, a ≤ z intro ys a:ℕys:List ℕa_1:∀ z ∈ [], a ≤ z⊢ (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge [] ys, a ≤ z a:ℕys:List ℕa_1:∀ z ∈ [], a ≤ zhys:∀ z ∈ ys, a ≤ z⊢ ∀ z ∈ merge [] ys, a ≤ z a:ℕys:List ℕa_1:∀ z ∈ [], a ≤ zhys:∀ z ∈ ys, a ≤ zz:ℕ⊢ z ∈ merge [] ys → a ≤ z a:ℕys:List ℕa_1:∀ z ∈ [], a ≤ zhys:∀ z ∈ ys, a ≤ zz:ℕhz:z ∈ merge [] ys⊢ a ≤ z a:ℕys:List ℕa_1:∀ z ∈ [], a ≤ zhys:∀ z ∈ ys, a ≤ zz:ℕhz:z ∈ ys⊢ a ≤ z All goals completed! 🐙 a:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ z⊢ ∀ (ys : List ℕ), (∀ z ∈ x :: xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge (x :: xs) ys, a ≤ z a:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zys:List ℕ⊢ (∀ z ∈ x :: xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge (x :: xs) ys, a ≤ z induction ys with a:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ z⊢ (∀ z ∈ x :: xs, a ≤ z) → (∀ z ∈ [], a ≤ z) → ∀ z ∈ merge (x :: xs) [], a ≤ z intro hxs a:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zhxs:∀ z ∈ x :: xs, a ≤ za_1:∀ z ∈ [], a ≤ z⊢ ∀ z ∈ merge (x :: xs) [], a ≤ z a:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zhxs:∀ z ∈ x :: xs, a ≤ za_1:∀ z ∈ [], a ≤ zz:ℕ⊢ z ∈ merge (x :: xs) [] → a ≤ z a:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zhxs:∀ z ∈ x :: xs, a ≤ za_1:∀ z ∈ [], a ≤ zz:ℕhz:z ∈ merge (x :: xs) []⊢ a ≤ z a:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zhxs:∀ z ∈ x :: xs, a ≤ za_1:∀ z ∈ [], a ≤ zz:ℕhz:z ∈ x :: xs⊢ a ≤ z All goals completed! 🐙 a:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zy:ℕys:List ℕihys:(∀ z ∈ x :: xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge (x :: xs) ys, a ≤ z⊢ (∀ z ∈ x :: xs, a ≤ z) → (∀ z ∈ y :: ys, a ≤ z) → ∀ z ∈ merge (x :: xs) (y :: ys), a ≤ z intro hxs a:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zy:ℕys:List ℕihys:(∀ z ∈ x :: xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge (x :: xs) ys, a ≤ zhxs:∀ z ∈ x :: xs, a ≤ zhys:∀ z ∈ y :: ys, a ≤ z⊢ ∀ z ∈ merge (x :: xs) (y :: ys), a ≤ z a:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zy:ℕys:List ℕihys:(∀ z ∈ x :: xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge (x :: xs) ys, a ≤ zhxs:∀ z ∈ x :: xs, a ≤ zhys:∀ z ∈ y :: ys, a ≤ zz:ℕ⊢ z ∈ merge (x :: xs) (y :: ys) → a ≤ z a:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zy:ℕys:List ℕihys:(∀ z ∈ x :: xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge (x :: xs) ys, a ≤ zhxs:∀ z ∈ x :: xs, a ≤ zhys:∀ z ∈ y :: ys, a ≤ zz:ℕhz:z ∈ merge (x :: xs) (y :: ys)⊢ a ≤ z a:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zy:ℕys:List ℕihys:(∀ z ∈ x :: xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge (x :: xs) ys, a ≤ zhxs:∀ z ∈ x :: xs, a ≤ zhys:∀ z ∈ y :: ys, a ≤ zz:ℕhz:z ∈ merge (x :: xs) (y :: ys)h:x ≤ y⊢ a ≤ za:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zy:ℕys:List ℕihys:(∀ z ∈ x :: xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge (x :: xs) ys, a ≤ zhxs:∀ z ∈ x :: xs, a ≤ zhys:∀ z ∈ y :: ys, a ≤ zz:ℕhz:z ∈ merge (x :: xs) (y :: ys)h:¬x ≤ y⊢ a ≤ z a:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zy:ℕys:List ℕihys:(∀ z ∈ x :: xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge (x :: xs) ys, a ≤ zhxs:∀ z ∈ x :: xs, a ≤ zhys:∀ z ∈ y :: ys, a ≤ zz:ℕhz:z ∈ merge (x :: xs) (y :: ys)h:x ≤ y⊢ a ≤ z a:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zy:ℕys:List ℕihys:(∀ z ∈ x :: xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge (x :: xs) ys, a ≤ zhxs:∀ z ∈ x :: xs, a ≤ zhys:∀ z ∈ y :: ys, a ≤ zz:ℕhz:z = x ∨ z ∈ merge xs (y :: ys)h:x ≤ y⊢ a ≤ z a:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zy:ℕys:List ℕhys:∀ z ∈ y :: ys, a ≤ zz:ℕihys:(∀ z_1 ∈ z :: xs, a ≤ z_1) → (∀ z ∈ ys, a ≤ z) → ∀ z_1 ∈ merge (z :: xs) ys, a ≤ z_1hxs:∀ z_1 ∈ z :: xs, a ≤ z_1h:z ≤ y⊢ a ≤ za:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zy:ℕys:List ℕihys:(∀ z ∈ x :: xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge (x :: xs) ys, a ≤ zhxs:∀ z ∈ x :: xs, a ≤ zhys:∀ z ∈ y :: ys, a ≤ zz:ℕh:x ≤ yhmem:z ∈ merge xs (y :: ys)⊢ a ≤ z a:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zy:ℕys:List ℕhys:∀ z ∈ y :: ys, a ≤ zz:ℕihys:(∀ z_1 ∈ z :: xs, a ≤ z_1) → (∀ z ∈ ys, a ≤ z) → ∀ z_1 ∈ merge (z :: xs) ys, a ≤ z_1hxs:∀ z_1 ∈ z :: xs, a ≤ z_1h:z ≤ y⊢ a ≤ z exact hxs z (a:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zy:ℕys:List ℕhys:∀ z ∈ y :: ys, a ≤ zz:ℕihys:(∀ z_1 ∈ z :: xs, a ≤ z_1) → (∀ z ∈ ys, a ≤ z) → ∀ z_1 ∈ merge (z :: xs) ys, a ≤ z_1hxs:∀ z_1 ∈ z :: xs, a ≤ z_1h:z ≤ y⊢ z ∈ z :: xs All goals completed! 🐙) a:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zy:ℕys:List ℕihys:(∀ z ∈ x :: xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge (x :: xs) ys, a ≤ zhxs:∀ z ∈ x :: xs, a ≤ zhys:∀ z ∈ y :: ys, a ≤ zz:ℕh:x ≤ yhmem:z ∈ merge xs (y :: ys)⊢ a ≤ z All goals completed! 🐙 a:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zy:ℕys:List ℕihys:(∀ z ∈ x :: xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge (x :: xs) ys, a ≤ zhxs:∀ z ∈ x :: xs, a ≤ zhys:∀ z ∈ y :: ys, a ≤ zz:ℕhz:z ∈ merge (x :: xs) (y :: ys)h:¬x ≤ y⊢ a ≤ z a:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zy:ℕys:List ℕihys:(∀ z ∈ x :: xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge (x :: xs) ys, a ≤ zhxs:∀ z ∈ x :: xs, a ≤ zhys:∀ z ∈ y :: ys, a ≤ zz:ℕhz:z = y ∨ z ∈ merge (x :: xs) ysh:¬x ≤ y⊢ a ≤ z a:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zys:List ℕihys:(∀ z ∈ x :: xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge (x :: xs) ys, a ≤ zhxs:∀ z ∈ x :: xs, a ≤ zz:ℕhys:∀ z_1 ∈ z :: ys, a ≤ z_1h:¬x ≤ z⊢ a ≤ za:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zy:ℕys:List ℕihys:(∀ z ∈ x :: xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge (x :: xs) ys, a ≤ zhxs:∀ z ∈ x :: xs, a ≤ zhys:∀ z ∈ y :: ys, a ≤ zz:ℕh:¬x ≤ yhmem:z ∈ merge (x :: xs) ys⊢ a ≤ z a:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zys:List ℕihys:(∀ z ∈ x :: xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge (x :: xs) ys, a ≤ zhxs:∀ z ∈ x :: xs, a ≤ zz:ℕhys:∀ z_1 ∈ z :: ys, a ≤ z_1h:¬x ≤ z⊢ a ≤ z exact hys z (a:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zys:List ℕihys:(∀ z ∈ x :: xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge (x :: xs) ys, a ≤ zhxs:∀ z ∈ x :: xs, a ≤ zz:ℕhys:∀ z_1 ∈ z :: ys, a ≤ z_1h:¬x ≤ z⊢ z ∈ z :: ys All goals completed! 🐙) a:ℕx:ℕxs:List ℕihxs:∀ (ys : List ℕ), (∀ z ∈ xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge xs ys, a ≤ zy:ℕys:List ℕihys:(∀ z ∈ x :: xs, a ≤ z) → (∀ z ∈ ys, a ≤ z) → ∀ z ∈ merge (x :: xs) ys, a ≤ zhxs:∀ z ∈ x :: xs, a ≤ zhys:∀ z ∈ y :: ys, a ≤ zz:ℕh:¬x ≤ yhmem:z ∈ merge (x :: xs) ys⊢ a ≤ z All goals completed! 🐙

We also need a simple fact: if a list starts with x, all the elements of its tail are greater than or equal to x, and the tail is sorted, then the whole list is sorted. The proof is a case analysis on the tail:

theorem sorted_cons (x : ℕ) (xs : List ℕ) (hlb : ∀ z ∈ xs, x ≤ z) (hs : Sorted xs) : Sorted (x :: xs) := x:ℕxs:List ℕhlb:∀ z ∈ xs, x ≤ zhs:Sorted xs⊢ Sorted (x :: xs) cases xs with x:ℕhlb:∀ z ∈ [], x ≤ zhs:Sorted []⊢ Sorted [x] All goals completed! 🐙 x:ℕw:ℕws:List ℕhlb:∀ z ∈ w :: ws, x ≤ zhs:Sorted (w :: ws)⊢ Sorted (x :: w :: ws) exact Sorted.two_or_more x w (hlb w (x:ℕw:ℕws:List ℕhlb:∀ z ∈ w :: ws, x ≤ zhs:Sorted (w :: ws)⊢ w ∈ w :: ws All goals completed! 🐙)) hs theorem sorted_tail {x : ℕ} {xs : List ℕ} (h : Sorted (x :: xs)) : Sorted xs := x:ℕxs:List ℕh:Sorted (x :: xs)⊢ Sorted xs cases h with x:ℕ⊢ Sorted [] All goals completed! 🐙 x:ℕy:ℕzs:List ℕhs:Sorted (y :: zs)hle:x ≤ y⊢ Sorted (y :: zs) All goals completed! 🐙

With these lemmas, the main theorem is a nested induction with the same structure as merge_lb. In the x ≤ y case, the head of the result is x; the tail merge xs (y :: ys) is sorted by the induction hypothesis, and x is a lower bound for all its elements by merge_lb, since it is a lower bound for the elements of xs (by the sortedness of x :: xs) and for those of y :: ys (since x ≤ y and y is a lower bound for ys). The ¬ x ≤ y case is symmetric.

theorem merge_sorted : ∀ xs ys, Sorted xs → Sorted ys → Sorted (merge xs ys) := ⊢ ∀ (xs ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys) xs:List ℕ⊢ ∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys) induction xs with ⊢ ∀ (ys : List ℕ), Sorted [] → Sorted ys → Sorted (merge [] ys) intro ys ys:List ℕa:Sorted []⊢ Sorted ys → Sorted (merge [] ys) ys:List ℕa:Sorted []hys:Sorted ys⊢ Sorted (merge [] ys) ys:List ℕa:Sorted []hys:Sorted ys⊢ Sorted ys All goals completed! 🐙 x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)⊢ ∀ (ys : List ℕ), Sorted (x :: xs) → Sorted ys → Sorted (merge (x :: xs) ys) intro ys x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)ys:List ℕhxs:Sorted (x :: xs)⊢ Sorted ys → Sorted (merge (x :: xs) ys) x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)ys:List ℕhxs:Sorted (x :: xs)hys:Sorted ys⊢ Sorted (merge (x :: xs) ys) induction ys with x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)hys:Sorted []⊢ Sorted (merge (x :: xs) []) x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)hys:Sorted []⊢ Sorted (x :: xs); All goals completed! 🐙 x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)⊢ Sorted (merge (x :: xs) (y :: ys)) x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:x ≤ y⊢ Sorted (merge (x :: xs) (y :: ys))x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:¬x ≤ y⊢ Sorted (merge (x :: xs) (y :: ys)) x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:x ≤ y⊢ Sorted (merge (x :: xs) (y :: ys)) x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:x ≤ y⊢ Sorted (x :: merge xs (y :: ys)) x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:x ≤ y⊢ ∀ z ∈ merge xs (y :: ys), x ≤ zx:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:x ≤ y⊢ Sorted (merge xs (y :: ys)) x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:x ≤ y⊢ ∀ z ∈ merge xs (y :: ys), x ≤ z x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:x ≤ y⊢ ∀ z ∈ xs, x ≤ zx:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:x ≤ y⊢ ∀ z ∈ y :: ys, x ≤ z x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:x ≤ y⊢ ∀ z ∈ xs, x ≤ z All goals completed! 🐙 x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:x ≤ y⊢ ∀ z ∈ y :: ys, x ≤ z intro z x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:x ≤ yz:ℕhz:z ∈ y :: ys⊢ x ≤ z x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:x ≤ yz:ℕhz:z = y ∨ z ∈ ys⊢ x ≤ z x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)ys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)z:ℕhys:Sorted (z :: ys)h:x ≤ z⊢ x ≤ zx:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:x ≤ yz:ℕhmem:z ∈ ys⊢ x ≤ z x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)ys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)z:ℕhys:Sorted (z :: ys)h:x ≤ z⊢ x ≤ z All goals completed! 🐙 x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:x ≤ yz:ℕhmem:z ∈ ys⊢ x ≤ z All goals completed! 🐙 x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:x ≤ y⊢ Sorted (merge xs (y :: ys)) All goals completed! 🐙 x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:¬x ≤ y⊢ Sorted (merge (x :: xs) (y :: ys)) x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:¬x ≤ y⊢ Sorted (y :: merge (x :: xs) ys) have hyx : y ≤ x := ⊢ ∀ (xs ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys) All goals completed! 🐙 x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:¬x ≤ yhyx:y ≤ x⊢ ∀ z ∈ merge (x :: xs) ys, y ≤ zx:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:¬x ≤ yhyx:y ≤ x⊢ Sorted (merge (x :: xs) ys) x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:¬x ≤ yhyx:y ≤ x⊢ ∀ z ∈ merge (x :: xs) ys, y ≤ z x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:¬x ≤ yhyx:y ≤ x⊢ ∀ z ∈ x :: xs, y ≤ zx:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:¬x ≤ yhyx:y ≤ x⊢ ∀ z ∈ ys, y ≤ z x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:¬x ≤ yhyx:y ≤ x⊢ ∀ z ∈ x :: xs, y ≤ z intro z x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:¬x ≤ yhyx:y ≤ xz:ℕhz:z ∈ x :: xs⊢ y ≤ z x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:¬x ≤ yhyx:y ≤ xz:ℕhz:z = x ∨ z ∈ xs⊢ y ≤ z xs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)y:ℕys:List ℕhys:Sorted (y :: ys)z:ℕhxs:Sorted (z :: xs)ihys:Sorted ys → Sorted (merge (z :: xs) ys)h:¬z ≤ yhyx:y ≤ z⊢ y ≤ zx:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:¬x ≤ yhyx:y ≤ xz:ℕhmem:z ∈ xs⊢ y ≤ z xs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)y:ℕys:List ℕhys:Sorted (y :: ys)z:ℕhxs:Sorted (z :: xs)ihys:Sorted ys → Sorted (merge (z :: xs) ys)h:¬z ≤ yhyx:y ≤ z⊢ y ≤ z All goals completed! 🐙 x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:¬x ≤ yhyx:y ≤ xz:ℕhmem:z ∈ xs⊢ y ≤ z All goals completed! 🐙 x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:¬x ≤ yhyx:y ≤ x⊢ ∀ z ∈ ys, y ≤ z All goals completed! 🐙 x:ℕxs:List ℕihxs:∀ (ys : List ℕ), Sorted xs → Sorted ys → Sorted (merge xs ys)hxs:Sorted (x :: xs)y:ℕys:List ℕihys:Sorted ys → Sorted (merge (x :: xs) ys)hys:Sorted (y :: ys)h:¬x ≤ yhyx:y ≤ x⊢ Sorted (merge (x :: xs) ys) All goals completed! 🐙

2.8.3.3. merge produces a permutation

To show that xs ++ ys ~ merge xs ys, the delicate case is when the head of the result comes from the second list: we need to relate (x :: xs) ++ (y :: ys) to y :: merge (x :: xs) ys. The lemma Perm_middle states that moving an element from the middle of a concatenation to the front yields a permutation; it is proved by induction on xs, using swap to make the element "pass over" each element of xs.

theorem Perm_middle {α : Type} (x : α) (xs ys : List α) : x :: (xs ++ ys) ~ xs ++ (x :: ys) := α:Typex:αxs:List αys:List α⊢ x :: (xs ++ ys) ~ xs ++ x :: ys induction xs with α:Typex:αys:List α⊢ x :: ([] ++ ys) ~ [] ++ x :: ys All goals completed! 🐙 α:Typex:αys:List αa:αxs:List αih:x :: (xs ++ ys) ~ xs ++ x :: ys⊢ x :: (a :: xs ++ ys) ~ a :: xs ++ x :: ys All goals completed! 🐙 theorem merge_perm : ∀ xs ys, xs ++ ys ~ merge xs ys := ⊢ ∀ (xs ys : List ℕ), xs ++ ys ~ merge xs ys xs:List ℕ⊢ ∀ (ys : List ℕ), xs ++ ys ~ merge xs ys induction xs with ⊢ ∀ (ys : List ℕ), [] ++ ys ~ merge [] ys ys:List ℕ⊢ [] ++ ys ~ merge [] ys ys:List ℕ⊢ [] ++ ys ~ ys All goals completed! 🐙 x:ℕxs:List ℕihxs:∀ (ys : List ℕ), xs ++ ys ~ merge xs ys⊢ ∀ (ys : List ℕ), x :: xs ++ ys ~ merge (x :: xs) ys x:ℕxs:List ℕihxs:∀ (ys : List ℕ), xs ++ ys ~ merge xs ysys:List ℕ⊢ x :: xs ++ ys ~ merge (x :: xs) ys induction ys with x:ℕxs:List ℕihxs:∀ (ys : List ℕ), xs ++ ys ~ merge xs ys⊢ x :: xs ++ [] ~ merge (x :: xs) [] x:ℕxs:List ℕihxs:∀ (ys : List ℕ), xs ++ ys ~ merge xs ys⊢ x :: xs ~ x :: xs All goals completed! 🐙 x:ℕxs:List ℕihxs:∀ (ys : List ℕ), xs ++ ys ~ merge xs ysy:ℕys:List ℕihys:x :: xs ++ ys ~ merge (x :: xs) ys⊢ x :: xs ++ y :: ys ~ merge (x :: xs) (y :: ys) x:ℕxs:List ℕihxs:∀ (ys : List ℕ), xs ++ ys ~ merge xs ysy:ℕys:List ℕihys:x :: xs ++ ys ~ merge (x :: xs) ysh:x ≤ y⊢ x :: xs ++ y :: ys ~ merge (x :: xs) (y :: ys)x:ℕxs:List ℕihxs:∀ (ys : List ℕ), xs ++ ys ~ merge xs ysy:ℕys:List ℕihys:x :: xs ++ ys ~ merge (x :: xs) ysh:¬x ≤ y⊢ x :: xs ++ y :: ys ~ merge (x :: xs) (y :: ys) x:ℕxs:List ℕihxs:∀ (ys : List ℕ), xs ++ ys ~ merge xs ysy:ℕys:List ℕihys:x :: xs ++ ys ~ merge (x :: xs) ysh:x ≤ y⊢ x :: xs ++ y :: ys ~ merge (x :: xs) (y :: ys) x:ℕxs:List ℕihxs:∀ (ys : List ℕ), xs ++ ys ~ merge xs ysy:ℕys:List ℕihys:x :: xs ++ ys ~ merge (x :: xs) ysh:x ≤ y⊢ x :: xs ++ y :: ys ~ x :: merge xs (y :: ys) All goals completed! 🐙 x:ℕxs:List ℕihxs:∀ (ys : List ℕ), xs ++ ys ~ merge xs ysy:ℕys:List ℕihys:x :: xs ++ ys ~ merge (x :: xs) ysh:¬x ≤ y⊢ x :: xs ++ y :: ys ~ merge (x :: xs) (y :: ys) x:ℕxs:List ℕihxs:∀ (ys : List ℕ), xs ++ ys ~ merge xs ysy:ℕys:List ℕihys:x :: xs ++ ys ~ merge (x :: xs) ysh:¬x ≤ y⊢ x :: xs ++ y :: ys ~ y :: merge (x :: xs) ys All goals completed! 🐙

In the second case, the chain of permutations is

(x :: xs) \mathbin{+\!\!+} (y :: ys) \;\sim\; y :: ((x :: xs) \mathbin{+\!\!+} ys) \;\sim\; y :: \mathsf{merge}\ (x :: xs)\ ys

in which the first step is Perm_middle (used in the reverse direction, with Perm_sym) and the second is the inner induction hypothesis, with skip.

2.8.4. Merge sort

With merge in hand, merge sort is simple: we split the list into two parts of roughly equal size, sort each part recursively, and merge the results. To split the list, the function split distributes the elements alternately between the two parts:

def split : List ℕ → List ℕ × List ℕ | [] => ([], []) | [x] => ([x], []) | x :: y :: zs => let p := split zs (x :: p.1, y :: p.2)

split is structurally recursive (the call is made on zs, a subterm of x :: y :: zs). The essential fact about it is the length of the parts: the first has \lceil n/2 \rceil elements and the second \lfloor n/2 \rfloor. The proof is a recursive function with the same structure as split:

theorem split_length : ∀ xs : List ℕ, (split xs).1.length = (xs.length + 1) / 2 ∧ (split xs).2.length = xs.length / 2 ⊢ (split []).1.length = ([].length + 1) / 2 ∧ (split []).2.length = [].length / 2 ⊢ (split []).1.length = ([].length + 1) / 2 ∧ (split []).2.length = [].length / 2 All goals completed! 🐙 x:ℕ⊢ (split [x]).1.length = ([x].length + 1) / 2 ∧ (split [x]).2.length = [x].length / 2 x:ℕ⊢ (split [x]).1.length = ([x].length + 1) / 2 ∧ (split [x]).2.length = [x].length / 2 All goals completed! 🐙 x:ℕy:ℕzs:List ℕ⊢ (split (x :: y :: zs)).1.length = ((x :: y :: zs).length + 1) / 2 ∧ (split (x :: y :: zs)).2.length = (x :: y :: zs).length / 2 x:ℕy:ℕzs:List ℕ⊢ (split (x :: y :: zs)).1.length = ((x :: y :: zs).length + 1) / 2 ∧ (split (x :: y :: zs)).2.length = (x :: y :: zs).length / 2 x:ℕy:ℕzs:List ℕih:(split zs).1.length = (zs.length + 1) / 2 ∧ (split zs).2.length = zs.length / 2⊢ (split (x :: y :: zs)).1.length = ((x :: y :: zs).length + 1) / 2 ∧ (split (x :: y :: zs)).2.length = (x :: y :: zs).length / 2 x:ℕy:ℕzs:List ℕih:(split zs).1.length = (zs.length + 1) / 2 ∧ (split zs).2.length = zs.length / 2⊢ (split zs).1.length + 1 = (zs.length + 1 + 1 + 1) / 2 ∧ (split zs).2.length + 1 = (zs.length + 1 + 1) / 2 All goals completed! 🐙

Now we can define the sort. Lists with fewer than two elements are already sorted; for the others, the two parts produced by split are strictly smaller than the original list, and this is exactly what the decreasing_by clause proves, using split_length. There are two goals, one for each recursive call, and all_goals applies the same argument to both. The hypothesis _h : ¬ xs.length < 2, introduced by the dependent if, is available in the context and is used by omega.

def msort (xs : List ℕ) : List ℕ := if _h : xs.length < 2 then xs else merge (msort (split xs).1) (msort (split xs).2) termination_by xs.length decreasing_by all_goals xs:List ℕ_h:¬xs.length < 2this:(split xs).1.length = (xs.length + 1) / 2 ∧ (split xs).2.length = xs.length / 2⊢ (split xs).2.length < xs.length All goals completed! 🐙 [1, 2, 3, 3, 5, 8, 9]#eval msort [5, 3, 8, 1, 9, 2, 3]
[1, 2, 3, 3, 5, 8, 9]

2.8.4.1. Correctness of msort

The correctness proofs all have the same shape: well-founded recursion on the length of the list, mirroring the definition of msort. We unfold msort with rw [msort], split the cases of the if with split_ifs and, in the recursive case, use the "induction hypotheses" for the two halves.

For sortedness, we need a fact about short lists:

theorem sorted_of_length_lt_two : ∀ xs : List ℕ, xs.length < 2 → Sorted xs | [], _ => Sorted.nil | [x], _ => Sorted.single x head✝¹:ℕhead✝:ℕtail✝:List ℕh:(head✝¹ :: head✝ :: tail✝).length < 2⊢ Sorted (head✝¹ :: head✝ :: tail✝) head✝¹:ℕhead✝:ℕtail✝:List ℕh:(head✝¹ :: head✝ :: tail✝).length < 2⊢ Sorted (head✝¹ :: head✝ :: tail✝) All goals completed! 🐙 theorem msort_sorted (xs : List ℕ) : Sorted (msort xs) := xs:List ℕ⊢ Sorted (msort xs) xs:List ℕ⊢ Sorted (if _h : xs.length < 2 then xs else merge (msort (split xs).1) (msort (split xs).2)) xs:List ℕh:xs.length < 2⊢ Sorted xsxs:List ℕh:¬xs.length < 2⊢ Sorted (merge (msort (split xs).1) (msort (split xs).2)) xs:List ℕh:xs.length < 2⊢ Sorted xs All goals completed! 🐙 xs:List ℕh:¬xs.length < 2⊢ Sorted (merge (msort (split xs).1) (msort (split xs).2)) All goals completed! 🐙 termination_by xs.length decreasing_by all_goals xs:List ℕa✝:∀ (y : List ℕ), InvImage (fun x1 x2 => x1 < x2) (fun x => x.length) y xs → Sorted (msort y)h:¬xs.length < 2this:(split xs).1.length = (xs.length + 1) / 2 ∧ (split xs).2.length = xs.length / 2⊢ (split xs).2.length < xs.length All goals completed! 🐙

For the permutation, we need congruence lemmas: concatenation preserves permutations in each argument. The first is proved by induction on the permutation proof and the second by induction on the left-hand list.

theorem Perm_append_right {α : Type} {xs xs' : List α} (ys : List α) (h : xs ~ xs') : xs ++ ys ~ xs' ++ ys := α:Typexs:List αxs':List αys:List αh:xs ~ xs'⊢ xs ++ ys ~ xs' ++ ys induction h with α:Typexs:List αxs':List αys:List α⊢ [] ++ ys ~ [] ++ ys All goals completed! 🐙 α:Typexs:List αxs':List αys:List αx:αxs_1:List αys_1:List αa:xs_1 ~ ys_1ih:xs_1 ++ ys ~ ys_1 ++ ys⊢ x :: xs_1 ++ ys ~ x :: ys_1 ++ ys All goals completed! 🐙 α:Typexs:List αxs':List αys:List αx:αy:αzs:List α⊢ x :: y :: zs ++ ys ~ y :: x :: zs ++ ys All goals completed! 🐙 α:Typexs:List αxs':List αys:List αxs_1:List αys_1:List αzs:List αa:xs_1 ~ ys_1a_1:ys_1 ~ zsih1:xs_1 ++ ys ~ ys_1 ++ ysih2:ys_1 ++ ys ~ zs ++ ys⊢ xs_1 ++ ys ~ zs ++ ys All goals completed! 🐙 theorem Perm_append_left {α : Type} {ys ys' : List α} (xs : List α) (h : ys ~ ys') : xs ++ ys ~ xs ++ ys' := α:Typeys:List αys':List αxs:List αh:ys ~ ys'⊢ xs ++ ys ~ xs ++ ys' induction xs with α:Typeys:List αys':List αh:ys ~ ys'⊢ [] ++ ys ~ [] ++ ys' All goals completed! 🐙 α:Typeys:List αys':List αh:ys ~ ys'x:αxs:List αih:xs ++ ys ~ xs ++ ys'⊢ x :: xs ++ ys ~ x :: xs ++ ys' All goals completed! 🐙 theorem Perm_append {α : Type} {xs xs' ys ys' : List α} (h1 : xs ~ xs') (h2 : ys ~ ys') : xs ++ ys ~ xs' ++ ys' := Perm.trans (Perm_append_right ys h1) (Perm_append_left xs' h2)

We also need to know that split neither loses nor creates elements. In the x :: y :: zs case, we must relate x :: y :: zs to (x :: l) ++ (y :: r), where zs ~ l ++ r by the induction hypothesis; keeping x fixed at the front (with skip), we go through y :: (l ++ r) and use Perm_middle to move y into its position:

theorem split_perm : ∀ xs : List ℕ, xs ~ (split xs).1 ++ (split xs).2 | [] => Perm.nil | [x] => Perm_refl [x] x:ℕy:ℕzs:List ℕ⊢ x :: y :: zs ~ (split (x :: y :: zs)).1 ++ (split (x :: y :: zs)).2 x:ℕy:ℕzs:List ℕ⊢ x :: y :: zs ~ (split (x :: y :: zs)).1 ++ (split (x :: y :: zs)).2 x:ℕy:ℕzs:List ℕih:zs ~ (split zs).1 ++ (split zs).2⊢ x :: y :: zs ~ (split (x :: y :: zs)).1 ++ (split (x :: y :: zs)).2 x:ℕy:ℕzs:List ℕih:zs ~ (split zs).1 ++ (split zs).2⊢ x :: y :: zs ~ x :: ((split zs).1 ++ y :: (split zs).2) x:ℕy:ℕzs:List ℕih:zs ~ (split zs).1 ++ (split zs).2⊢ y :: zs ~ (split zs).1 ++ y :: (split zs).2 All goals completed! 🐙

Finally, the original list is a permutation of the concatenation of the parts (split_perm), which is a permutation of the concatenation of the sorted parts (induction hypotheses and Perm_append), which is a permutation of their merge (merge_perm):

theorem msort_perm (xs : List ℕ) : xs ~ msort xs := xs:List ℕ⊢ xs ~ msort xs xs:List ℕ⊢ xs ~ if _h : xs.length < 2 then xs else merge (msort (split xs).1) (msort (split xs).2) xs:List ℕh:xs.length < 2⊢ xs ~ xsxs:List ℕh:¬xs.length < 2⊢ xs ~ merge (msort (split xs).1) (msort (split xs).2) xs:List ℕh:xs.length < 2⊢ xs ~ xs All goals completed! 🐙 xs:List ℕh:¬xs.length < 2⊢ xs ~ merge (msort (split xs).1) (msort (split xs).2) xs:List ℕh:¬xs.length < 2h1:(split xs).1 ~ msort (split xs).1⊢ xs ~ merge (msort (split xs).1) (msort (split xs).2) xs:List ℕh:¬xs.length < 2h1:(split xs).1 ~ msort (split xs).1h2:(split xs).2 ~ msort (split xs).2⊢ xs ~ merge (msort (split xs).1) (msort (split xs).2) All goals completed! 🐙 termination_by xs.length decreasing_by all_goals xs:List ℕa✝:∀ (y : List ℕ), InvImage (fun x1 x2 => x1 < x2) (fun x => x.length) y xs → y ~ msort yh:¬xs.length < 2h1:(split xs).1 ~ msort (split xs).1this:(split xs).1.length = (xs.length + 1) / 2 ∧ (split xs).2.length = xs.length / 2⊢ (split xs).2.length < xs.length All goals completed! 🐙

Together, the theorems msort_sorted and msort_perm establish the total correctness of merge sort: it terminates (the definition was accepted by Lean), returns a sorted list and preserves the elements of the input.

end Ch15

2.8.5. Exercises

  1. Define the greatest common divisor by Euclid's algorithm,

    def gcd (a b : ℕ) : ℕ :=
      if b = 0 then a else gcd b (a % b)
    

    giving a suitable termination measure, and prove that gcd a b divides both a and b.

  2. Prove that natDiv n d ≤ n for all n and d.

  3. Prove the uniqueness of Euclidean division: if d * q + r = n and r < d, then q = natDiv n d and r = natMod n d.

  4. Define a function log2 : ℕ → ℕ such that log2 n is the number of times n can be divided by 2 before reaching 1, using well-founded recursion on n, and prove that 2 ^ log2 n ≤ n for n ≥ 1.

  5. Prove that (merge xs ys).length = xs.length + ys.length.

  6. Prove that msort is idempotent: msort (msort xs) = msort xs. Hint: first prove that, if Sorted xs, then msort xs = xs.

  7. Using Plausible, test the claim that msort and the insertion sort from the previous chapter always produce the same result. Then prove it.