bpe_greedy_step_utility_eq_max
A greedy full-merge step decreases the string length by exactly the maximum nonoverlapping adjacent-pair count, including the unchanged step when this count is zero.
Statement
theorem bpe_greedy_step_utility_eq_max
(s t : List ℕ) (h : bpe_greedy_step s t) :
s.length - t.length = bpe_max_pair_count s- Source
- Helper for the two-merge case of OP-1287 and arXiv:2611.08671v1, Theorem 12.
- Verified
- 19 Nov 2026
- Axioms
Classical.choiceQuot.soundpropext- Built on 4
- bpe_greedy_stepA greedy full-merge step selects any most frequent adjacent pair and a fresh symbol; a string without adjacent pairs remains unchanged.
- bpe_full_mergeThe string obtained by scanning left to right and replacing every available nonoverlapping occurrence of the selected pair with the given…
- bpe_pair_countThe number of occurrences of the ordered symbol pair replaced by the left-to-right nonoverlapping full merge.
- bpe_max_pair_countThe largest nonoverlapping full-merge pair count among adjacent pairs of the current string, with maximum zero for a string shorter than…
- Used by 2
- bpe_three_two_step_bound_of_numeric_boundsFor two greedy steps s to u to v, if the optimal three-merge utility is at most the two-merge optimum plus the initial maximum pair count,…
- bpe_retyping_three_of_numeric_boundsIf the three-merge optimum is at most three initial maximum pair counts, it is at most the two-merge optimum plus one initial maximum, and…
The proof, explained
The theorem states that a greedy Byte Pair Encoding (BPE) step decreases the string length by exactly the maximum non-overlapping count of any adjacent pair. We case-split on whether any pair can be merged: if the maximum pair count is zero, the string is unchanged and the length decrease is trivially zero. When a greedy merge is performed on a pair (a, b) achieving the maximum count, the claim reduces via an auxiliary lemma to showing that merging all non-overlapping occurrences of (a, b) reduces the string length by exactly the pair count. That lemma is proved by structural induction matching the recursion of the merge operation, supported by a helper lemma establishing that merging never increases string length.
For the base cases of the induction on bpe_full_merge (the empty list and any single-element list), merging does not increase list length: |bpe_full_merge a b z s| ≤ |s|.
We set up an induction on the list s using bpe_full_merge.induct, matching the recursion pattern of bpe_full_merge. For the empty list [] and single-element lists [x], bpe_full_merge leaves the list unchanged by definition, so both lengths are identical (0 ≤ 0 and 1 ≤ 1), which simplifies immediately.
uses
bpe_full_merge.inductrefine bpe_full_merge.induct a b (fun s => (bpe_full_merge a b z s).length ≤ s.length) ?_ ?_ ?_ ?_ s · simp [bpe_full_merge] · intro x simp [bpe_full_merge]
When the list starts with a matching pair (x, y) = (a, b), merging does not increase the length.
Assuming x = a and y = b, bpe_full_merge replaces the prefix x :: y with the single token z, giving z :: bpe_full_merge a b z xs. Rewriting the lengths with List.length_cons, the merged list has length |bpe_full_merge a b z xs| + 1, while the original list has length |xs| + 2. Since |bpe_full_merge a b z xs| ≤ |xs| by the inductive hypothesis, the inequality follows by linear arithmetic.
uses
List.length_cons· intro x y xs hxy ih have hm : bpe_full_merge a b z (x :: y :: xs) = z :: bpe_full_merge a b z xs := by simp [bpe_full_merge, hxy] rw [hm] simp only [List.length_cons] at ih ⊢ omega
When the first two elements do not match (a, b), merging does not increase the length, completing the proof of op1287_full_merge_length_le.
Assuming ¬(x = a ∧ y = b), bpe_full_merge keeps the head element x and recurses on y :: xs. Rewriting lengths with List.length_cons gives length |bpe_full_merge a b z (y :: xs)| + 1 for the merged list and |xs| + 2 for the original list. The inequality follows directly from the inductive hypothesis |bpe_full_merge a b z (y :: xs)| ≤ |xs| + 1 by linear arithmetic.
uses
List.length_cons· intro x y xs hxy ih have hm : bpe_full_merge a b z (x :: y :: xs) = x :: bpe_full_merge a b z (y :: xs) := by simp [bpe_full_merge, hxy] rw [hm] simp only [List.length_cons] at ih ⊢ omega
For the base cases of the induction (the empty list and any single-element list), the length decrease from merging equals the pair count: |s| - |bpe_full_merge a b z s| = bpe_pair_count a b s.
We prove op1287_full_merge_length_sub by induction on s using bpe_full_merge.induct. For the empty list [] and any single-element list [x], bpe_full_merge leaves the list unchanged and no adjacent pair can occur, so both the pair count and the length difference evaluate to 0 by definition.
uses
bpe_full_merge.inductrefine bpe_full_merge.induct a b (fun s => s.length - (bpe_full_merge a b z s).length = bpe_pair_count a b s) ?_ ?_ ?_ ?_ s · simp [bpe_full_merge, bpe_pair_count] · intro x simp [bpe_full_merge, bpe_pair_count]
When the list begins with a matching pair (x, y) = (a, b), the decrease in length equals the pair count.
Assume x = a and y = b. The merge replaces x :: y with z, giving z :: bpe_full_merge a b z xs, while bpe_pair_count increments by 1. Rewriting lengths with List.length_cons, the length difference is (|xs| + 2) - (|bpe_full_merge a b z xs| + 1). Invoking op1287_full_merge_length_le ensures |bpe_full_merge a b z xs| ≤ |xs|, preventing underflow in natural number subtraction; combined with the inductive hypothesis, linear arithmetic shows the length decrease is 1 + bpe_pair_count a b xs.
uses
List.length_cons,op1287_full_merge_length_le· intro x y xs hxy ih have hm : bpe_full_merge a b z (x :: y :: xs) = z :: bpe_full_merge a b z xs := by simp [bpe_full_merge, hxy] have hc : bpe_pair_count a b (x :: y :: xs) = 1 + bpe_pair_count a b xs := by simp [bpe_pair_count, hxy] rw [hm, hc] simp only [List.length_cons] at ih ⊢ have hle := op1287_full_merge_length_le a b z xs omega
When the first two elements do not match (a, b), the decrease in length equals the pair count, completing the proof of op1287_full_merge_length_sub.
Assume ¬(x = a ∧ y = b). The function bpe_full_merge keeps the head x and recurses on y :: xs, and the pair count is that of y :: xs. Rewriting lengths with List.length_cons yields (|xs| + 2) - (|bpe_full_merge a b z (y :: xs)| + 1). Using op1287_full_merge_length_le on y :: xs to prevent subtraction underflow, the inductive hypothesis and linear arithmetic conclude that the length decrease matches bpe_pair_count a b (y :: xs).
uses
List.length_cons,op1287_full_merge_length_le· intro x y xs hxy ih have hm : bpe_full_merge a b z (x :: y :: xs) = x :: bpe_full_merge a b z (y :: xs) := by simp [bpe_full_merge, hxy] have hc : bpe_pair_count a b (x :: y :: xs) = bpe_pair_count a b (y :: xs) := by simp [bpe_pair_count, hxy] rw [hm, hc] simp only [List.length_cons] at ih ⊢ have hle := op1287_full_merge_length_le a b z (y :: xs) simp only [List.length_cons] at hle omega
In a greedy step where the maximum pair count is 0, the list is unchanged and the length decrease is 0.
We case-split on the definition of bpe_greedy_step s t. In the first branch, no adjacent pair occurs (bpe_max_pair_count s = 0) and t = s. Substituting t = s, the length difference |s| - |s| = 0 simplifies directly to the maximum pair count 0.
rcases h with ⟨hmax, rfl⟩ | ⟨a, b, z, hpos, hcount, hz, rfl⟩ · simp [hmax]
In a greedy step where an optimal pair is merged, the decrease in length equals the maximum pair count, completing the proof of the theorem.
In the second branch, t = bpe_full_merge a b z s for a pair (a, b) achieving the maximum count bpe_pair_count a b s = bpe_max_pair_count s. Rewriting the length decrease with op1287_full_merge_length_sub produces bpe_pair_count a b s, which equals bpe_max_pair_count s by hypothesis.
uses
op1287_full_merge_length_sub· rw [op1287_full_merge_length_sub, hcount]
bpe_full_merge.induct- The custom structural induction principle for bpe_full_merge: to prove a property for all lists, it suffices to prove it for the empty list, for single-element lists, for lists starting with a matching pair (a, b) assuming it holds for the remaining tail, and for lists starting with a non-matching pair assuming it holds from the second element onward.
List.length_cons- The length of a list with an element prepended is one more than the length of the tail: |x :: xs| = |xs| + 1.
op1287_full_merge_length_le- Merging all non-overlapping occurrences of a pair (a, b) into a new token z never increases the length of the list s: |bpe_full_merge a b z s| ≤ |s|.
op1287_full_merge_length_sub- The decrease in length of a list s after merging all non-overlapping occurrences of (a, b) into z equals the count of such occurrences: |s| - |bpe_full_merge a b z s| = bpe_pair_count a b s.
- In Lean, natural number subtraction m - n truncates at 0 when m < n. The helper lemma op1287_full_merge_length_le is needed in steps 5 and 6 to guarantee that the merged list length never exceeds the original list length, ensuring that algebraic rearrangements of natural subtraction such as (N + 2) - (L + 1) = 1 + (N - L) are valid without underflow.
Written by a model (gemini-3.8-flash) from the Lean proof and the goal states Lean reported at each step. Lean verified the proof; this explanation is a reading aid and is not itself machine-checked.
Lean source view module on GitHub
Choose a tactic — a marked line — to see the goals Lean had before and after it, as in Lean’s infoview. Step with ← → or j k.
private lemma op1287_full_merge_length_le (a b z : ℕ) (s : List ℕ) : (bpe_full_merge a b z s).length ≤ s.length := by refine bpe_full_merge.induct a b (fun s => (bpe_full_merge a b z s).length ≤ s.length) ?_ ?_ ?_ ?_ s · simp [bpe_full_merge] · intro x simp [bpe_full_merge] · intro x y xs hxy ih have hm : bpe_full_merge a b z (x :: y :: xs) = z :: bpe_full_merge a b z xs := by simp [bpe_full_merge, hxy] rw [hm] simp only [List.length_cons] at ih ⊢ omega · intro x y xs hxy ih have hm : bpe_full_merge a b z (x :: y :: xs) = x :: bpe_full_merge a b z (y :: xs) := by simp [bpe_full_merge, hxy] rw [hm] simp only [List.length_cons] at ih ⊢ omegaprivate lemma op1287_full_merge_length_sub (a b z : ℕ) (s : List ℕ) : s.length - (bpe_full_merge a b z s).length = bpe_pair_count a b s := by refine bpe_full_merge.induct a b (fun s => s.length - (bpe_full_merge a b z s).length = bpe_pair_count a b s) ?_ ?_ ?_ ?_ s · simp [bpe_full_merge, bpe_pair_count] · intro x simp [bpe_full_merge, bpe_pair_count] · intro x y xs hxy ih have hm : bpe_full_merge a b z (x :: y :: xs) = z :: bpe_full_merge a b z xs := by simp [bpe_full_merge, hxy] have hc : bpe_pair_count a b (x :: y :: xs) = 1 + bpe_pair_count a b xs := by simp [bpe_pair_count, hxy] rw [hm, hc] simp only [List.length_cons] at ih ⊢ have hle := op1287_full_merge_length_le a b z xs omega · intro x y xs hxy ih have hm : bpe_full_merge a b z (x :: y :: xs) = x :: bpe_full_merge a b z (y :: xs) := by simp [bpe_full_merge, hxy] have hc : bpe_pair_count a b (x :: y :: xs) = bpe_pair_count a b (y :: xs) := by simp [bpe_pair_count, hxy] rw [hm, hc] simp only [List.length_cons] at ih ⊢ have hle := op1287_full_merge_length_le a b z (y :: xs) simp only [List.length_cons] at hle omegatheorem bpe_greedy_step_utility_eq_max (s t : List ℕ) (h : bpe_greedy_step s t) : s.length - t.length = bpe_max_pair_count s := by rcases h with ⟨hmax, rfl⟩ | ⟨a, b, z, hpos, hcount, hz, rfl⟩ · simp [hmax] · rw [op1287_full_merge_length_sub, hcount]