AFTD Auto-Formalizing Theoretical Domains

bpe_partial_step_utility_le_max

theoremverified

The decrease in string length caused by one partial merge step is at most the largest nonoverlapping pair count in the input string.

Statement

theorem bpe_partial_step_utility_le_max
    (s t : List ℕ) (h : bpe_partial_step s t) :
    s.length - t.length ≤ bpe_max_pair_count s
Source
Helper for the i = 0 case of OP-1287; arXiv:2611.08671v1, Lemma 17.
Verified
20 Nov 2026
Axioms
Classical.choiceQuot.soundpropext
Built on 4
  • bpe_partial_stepA partial merge replaces an arbitrary nonoverlapping subset of one pair by a fresh symbol, with an unchanged step allowed.
  • bpe_partial_mergeThe relation between input and output strings obtained by replacing an arbitrary subset of pairwise nonoverlapping occurrences of the…
  • 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…

The proof, explained

The theorem establishes that a partial merge step decreases string length by at most the largest non-overlapping count of any adjacent pair in the input string. First, prepending an element is shown to never decrease a pair's count (`bpe_psu_count_cons_bounds`), which enables an inductive proof that a partial merge of pair (a, b) decreases string length by at most the count of (a, b) (`bpe_psu_merge_bound`). Next, any pair with positive count must appear as an adjacent pair in the string (`bpe_psu_pos_mem_zip`), placing its count below the supremum over all adjacent pairs (`bpe_psu_count_le_max`). Combining these two inequalities by transitivity proves that the utility of any partial merge step is bounded by the maximum pair count.

  1. In the base case n = 0, the list L must be empty, so the pair count bounds hold trivially.

    Because the length of L is at most 0, `Nat.le_zero.mp` implies its length is 0, and `List.length_eq_zero_iff.mp` rewrites L to the empty list. On the empty list and the singleton list [w], the pair count of (a, b) is 0 by definition, satisfying 0 ≤ 0 ≤ 1.

    lines 6–7

    uses List.length_eq_zero_iff.mp, Nat.le_zero.mp

          rw [List.length_eq_zero_iff.mp (Nat.le_zero.mp hL)]
          simp [bpe_pair_count]
  2. In the inductive case n + 1, if L is empty, the pair count bounds hold trivially.

    When L is empty, both `bpe_pair_count a b []` and `bpe_pair_count a b [w]` evaluate to 0 by definition, so the bounds 0 ≤ 0 ≤ 1 hold immediately.

    lines 9–10

          match L, hL with
          | [], _ => simp [bpe_pair_count]
  3. When L = u :: L' and the first two tokens match the target pair (w = a and u = b), prepending w increases the count of L' by 1, satisfying the required bounds.

    Simplifying the length bound with `List.length_cons` ensures L'.length ≤ n. When (w, u) = (a, b), the pair count on w :: u :: L' is 1 + count(L'). The induction hypothesis `bpe_psu_count_cons_bounds` on L' gives count(L') ≤ count(u :: L') ≤ count(L') + 1. The lower bound count(u :: L') ≤ 1 + count(L') follows from this directly. For the upper bound 1 + count(L') ≤ count(u :: L') + 1, case analysis on L' shows it is trivial if L' is empty, and follows from a second induction hypothesis on the tail if L' = v :: L''.

    lines 12–24

    uses List.length_cons, bpe_psu_count_cons_bounds

            simp only [List.length_cons] at hL'
            by_cases hwu : w = a ∧ u = b
            · have hc : bpe_pair_count a b (w :: u :: L') = 1 + bpe_pair_count a b L' := by
                simp [bpe_pair_count, hwu]
              have ih := bpe_psu_count_cons_bounds a b n u L' (by omega)
              rw [hc]
              constructor
              · omega
              · match L', hL' with
                | [], _ => simp [bpe_pair_count]
                | v :: L'', hL'' =>
                  have ih2 := bpe_psu_count_cons_bounds a b n v L'' (by simp at hL'' ⊢; omega)
                  omega
  4. When L = u :: L' and (w, u) does not match (a, b), the pair count is unchanged by prepending w, so the bounds hold.

    Since (w, u) ≠ (a, b), the pair count on w :: u :: L' is identical to that on u :: L' by definition of `bpe_pair_count`. The bounds reduce to count(u :: L') ≤ count(u :: L') ≤ count(u :: L') + 1, which holds reflexively.

    lines 25–27

            · have hc : bpe_pair_count a b (w :: u :: L') = bpe_pair_count a b (u :: L') := by
                simp [bpe_pair_count, hwu]
              rw [hc]; omega
  5. For strings of length 0 or 1, a partial merge leaves the string unchanged, so the length does not decrease and the bounds hold.

    By definition of `bpe_partial_merge`, if the input string s is empty or a singleton [x], the output string t must equal s. Therefore t.length ≤ s.length and the length decrease is 0, which is at most the pair count of s.

    lines 32–33

      | [], t, h => by simp [bpe_partial_merge] at h; subst h; simp
      | [x], t, h => by simp [bpe_partial_merge] at h; subst h; simp
  6. When the first character is retained without merging, the length decrease does not exceed the pair count of the extended string.

    If t = x :: out with out obtained by partially merging y :: xs, the induction hypothesis `bpe_psu_merge_bound` on y :: xs bounds (y :: xs).length - out.length by count(y :: xs). By `bpe_psu_count_cons_bounds` (using reflexivity `le_rfl`), count(y :: xs) ≤ count(x :: y :: xs). Prepending x preserves the length difference, so the bound holds by linear arithmetic after unfolding lengths with `List.length_cons`.

    lines 35–39

    uses bpe_psu_merge_bound, bpe_psu_count_cons_bounds, le_rfl, List.length_cons

          rcases h with ⟨out, rfl, hout⟩ | ⟨hxa, hyb, out, rfl, hout⟩
          · obtain ⟨h1, h2⟩ := bpe_psu_merge_bound a b z (y :: xs) out hout
            have hb := (bpe_psu_count_cons_bounds a b (y :: xs).length x (y :: xs) le_rfl).1
            simp only [List.length_cons] at h1 h2 ⊢
            omega
  7. When the leading pair (x, y) = (a, b) is merged into token z, the length decrease is bounded by the pair count of x :: y :: xs.

    If x = a and y = b with t = z :: out, the induction hypothesis on xs bounds xs.length - out.length by count(xs). Merging (a, b) decreases the string length by 1 more than on xs, while the pair count of x :: y :: xs is exactly 1 + count(xs) by definition of `bpe_pair_count`. Unfolding lengths with `List.length_cons`, the length decrease matches the count bound.

    lines 40–45

    uses bpe_psu_merge_bound, List.length_cons

          · obtain ⟨h1, h2⟩ := bpe_psu_merge_bound a b z xs out hout
            have hc : bpe_pair_count a b (x :: y :: xs) = 1 + bpe_pair_count a b xs := by
              simp [bpe_pair_count, hxa, hyb]
            rw [hc]
            simp only [List.length_cons] at h1 h2 ⊢
            omega
  8. To show that a positive pair count implies the pair appears in the adjacent zip of the string, the base cases of length 0 and 1 hold vacuously.

    Using the custom induction principle `bpe_pair_count.induct`, for lists of length 0 or 1 the pair count is 0 by definition, contradicting the assumption that the count is strictly positive.

    lines 49–52

    uses bpe_pair_count.induct

      refine bpe_pair_count.induct a b (fun s => 0 < bpe_pair_count a b s → (a, b) ∈ s.zip s.tail) ?_ ?_ ?_ ?_ s h
      · simp [bpe_pair_count]
      · intro x
        simp [bpe_pair_count]
  9. When the leading pair of the string matches (a, b), it occurs as the head of the adjacent zip of the string.

    For s = x :: y :: xs with x = a and y = b, the adjacent zip s.zip s.tail starts with (x, y) = (a, b), so the pair is immediately in the zip.

    lines 53–54

      · intro x y xs hxy _ _
        simp [hxy]
  10. When the leading pair does not match (a, b), the positive count is inherited from the tail, so (a, b) appears in the adjacent zip.

    Because (x, y) ≠ (a, b), the count on x :: y :: xs equals that on y :: xs. By the induction hypothesis, (a, b) belongs to (y :: xs).zip (y :: xs).tail. Using `List.zip_cons_cons` and `List.tail_cons`, this zip embeds into the tail of (x :: y :: xs).zip (x :: y :: xs).tail, so membership follows by `List.mem_cons_of_mem`.

    lines 55–61

    uses List.zip_cons_cons, List.tail_cons, List.mem_cons_of_mem

      · intro x y xs hxy ih hpos
        have hc : bpe_pair_count a b (x :: y :: xs) = bpe_pair_count a b (y :: xs) := by
          simp [bpe_pair_count, hxy]
        rw [hc] at hpos
        have hmem := ih hpos
        simp only [List.zip_cons_cons, List.tail_cons]
        exact List.mem_cons_of_mem _ hmem
  11. If the pair count of (a, b) in s is zero, it is bounded above by the maximum pair count.

    Splitting on whether the count is zero or positive with `Nat.eq_zero_or_pos`, if the count is 0, the inequality 0 ≤ bpe_max_pair_count s holds by `Nat.zero_le`.

    lines 64–65

    uses Nat.eq_zero_or_pos, Nat.zero_le

      rcases Nat.eq_zero_or_pos (bpe_pair_count a b s) with h0 | hpos
      · rw [h0]; exact Nat.zero_le _
  12. If the pair count of (a, b) in s is positive, it is bounded above by the maximum pair count over all adjacent pairs.

    When the count is positive, `bpe_psu_pos_mem_zip` shows that (a, b) appears in s.zip s.tail, and hence in its finite set conversion. By `Finset.le_sup`, the count of (a, b) is bounded above by the supremum of pair counts over all adjacent pairs, which is `bpe_max_pair_count s`.

    lines 66–69

    uses bpe_psu_pos_mem_zip, Finset.le_sup

      · have hmem := bpe_psu_pos_mem_zip a b s hpos
        have hle := Finset.le_sup (s := (s.zip s.tail).toFinset)
          (f := fun p : ℕ × ℕ => bpe_pair_count p.1 p.2 s) (by simpa using hmem)
        simpa [bpe_max_pair_count] using hle
  13. When the partial step is the identity, the decrease in string length is 0, which is at most the maximum pair count.

    A partial step `bpe_partial_step s t` is either an identity step s = t or a partial merge. In the identity case, s.length - s.length = 0, which is non-negative and bounded by any natural number.

    lines 74–75

      rcases h with rfl | ⟨a, b, z, -, hm⟩
      · simp
  14. When the partial step performs a merge, the length decrease is at most the maximum pair count.

    Under a partial merge of pair (a, b) into z, `bpe_psu_merge_bound` bounds the length decrease s.length - t.length by `bpe_pair_count a b s`. In turn, `bpe_psu_count_le_max` bounds this count by `bpe_max_pair_count s`. The conclusion follows by transitivity via `le_trans`.

    line 76

    uses bpe_psu_merge_bound, bpe_psu_count_le_max, le_trans

      · exact le_trans (bpe_psu_merge_bound a b z s t hm).2 (bpe_psu_count_le_max a b s)
List.length_eq_zero_iff.mp
A list has length 0 if and only if it is empty (forward direction: length 0 implies empty).
Nat.le_zero.mp
If a natural number is at most 0, then it equals 0.
List.length_cons
The length of a list with an element prepended is one more than the length of the list.
bpe_psu_count_cons_bounds
Prepending an element to a list changes the non-overlapping count of any pair by at most 1, and never decreases it.
bpe_psu_merge_bound
If string t is obtained from string s by partially merging pair (a, b) into token z, then t is no longer than s and the decrease in length is at most the non-overlapping count of (a, b) in s.
le_rfl
Inequality on preorders is reflexive: any element is less than or equal to itself.
bpe_pair_count.induct
The induction principle tailored to `bpe_pair_count`, with base cases for empty and singleton lists, and inductive steps for matching and non-matching leading pairs.
List.zip_cons_cons
The zip of two non-empty lists is the pair of their heads followed by the zip of their tails.
List.tail_cons
The tail of a non-empty list `a :: as` is `as`.
List.mem_cons_of_mem
If an element belongs to a list, it also belongs to the list with any element prepended.
Nat.eq_zero_or_pos
Every natural number is either zero or strictly positive.
Nat.zero_le
Zero is less than or equal to every natural number.
bpe_psu_pos_mem_zip
If the non-overlapping count of pair (a, b) in string s is strictly positive, then (a, b) appears as an adjacent pair in s.
Finset.le_sup
The value of a function at any element of a finite set is bounded above by the supremum of that function over the set.
bpe_psu_count_le_max
The non-overlapping count of any pair (a, b) in string s is at most the maximum non-overlapping count of any adjacent pair in s.
le_trans
Inequality is transitive: if a ≤ b and b ≤ c, then a ≤ c.

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

theorem bpe_psu_count_cons_bounds (a b : ℕ) :    ∀ (n : ℕ) (w : ℕ) (L : List ℕ), L.length ≤ n →      bpe_pair_count a b L ≤ bpe_pair_count a b (w :: L) ∧      bpe_pair_count a b (w :: L) ≤ bpe_pair_count a b L + 1  | 0, w, L, hL => by      rw [List.length_eq_zero_iff.mp (Nat.le_zero.mp hL)]      simp [bpe_pair_count]  | n+1, w, L, hL => by      match L, hL with      | [], _ => simp [bpe_pair_count]      | u :: L', hL' =>        simp only [List.length_cons] at hL'        by_cases hwu : w = a ∧ u = b        · have hc : bpe_pair_count a b (w :: u :: L') = 1 + bpe_pair_count a b L' := by            simp [bpe_pair_count, hwu]          have ih := bpe_psu_count_cons_bounds a b n u L' (by omega)          rw [hc]          constructor          · omega          · match L', hL' with            | [], _ => simp [bpe_pair_count]            | v :: L'', hL'' =>              have ih2 := bpe_psu_count_cons_bounds a b n v L'' (by simp at hL'' ⊢; omega)              omega        · have hc : bpe_pair_count a b (w :: u :: L') = bpe_pair_count a b (u :: L') := by            simp [bpe_pair_count, hwu]          rw [hc]; omegatheorem bpe_psu_merge_bound (a b z : ℕ) :    ∀ (s t : List ℕ), bpe_partial_merge a b z s t →      t.length ≤ s.length ∧ s.length - t.length ≤ bpe_pair_count a b s  | [], t, h => by simp [bpe_partial_merge] at h; subst h; simp  | [x], t, h => by simp [bpe_partial_merge] at h; subst h; simp  | x :: y :: xs, t, h => by      rcases h with ⟨out, rfl, hout⟩ | ⟨hxa, hyb, out, rfl, hout⟩      · obtain ⟨h1, h2⟩ := bpe_psu_merge_bound a b z (y :: xs) out hout        have hb := (bpe_psu_count_cons_bounds a b (y :: xs).length x (y :: xs) le_rfl).1        simp only [List.length_cons] at h1 h2 ⊢        omega      · obtain ⟨h1, h2⟩ := bpe_psu_merge_bound a b z xs out hout        have hc : bpe_pair_count a b (x :: y :: xs) = 1 + bpe_pair_count a b xs := by          simp [bpe_pair_count, hxa, hyb]        rw [hc]        simp only [List.length_cons] at h1 h2 ⊢        omegatheorem bpe_psu_pos_mem_zip (a b : ℕ) (s : List ℕ) (h : 0 < bpe_pair_count a b s) :    (a, b) ∈ s.zip s.tail := by  refine bpe_pair_count.induct a b (fun s => 0 < bpe_pair_count a b s → (a, b) ∈ s.zip s.tail) ?_ ?_ ?_ ?_ s h  · simp [bpe_pair_count]  · intro x    simp [bpe_pair_count]  · intro x y xs hxy _ _    simp [hxy]  · intro x y xs hxy ih hpos    have hc : bpe_pair_count a b (x :: y :: xs) = bpe_pair_count a b (y :: xs) := by      simp [bpe_pair_count, hxy]    rw [hc] at hpos    have hmem := ih hpos    simp only [List.zip_cons_cons, List.tail_cons]    exact List.mem_cons_of_mem _ hmemtheorem bpe_psu_count_le_max (a b : ℕ) (s : List ℕ) : bpe_pair_count a b s ≤ bpe_max_pair_count s := by  rcases Nat.eq_zero_or_pos (bpe_pair_count a b s) with h0 | hpos  · rw [h0]; exact Nat.zero_le _  · have hmem := bpe_psu_pos_mem_zip a b s hpos    have hle := Finset.le_sup (s := (s.zip s.tail).toFinset)      (f := fun p : ℕ × ℕ => bpe_pair_count p.1 p.2 s) (by simpa using hmem)    simpa [bpe_max_pair_count] using hletheorem bpe_partial_step_utility_le_max    (s t : List ℕ) (h : bpe_partial_step s t) :    s.length - t.length ≤ bpe_max_pair_count s := by  rcases h with rfl | ⟨a, b, z, -, hm⟩  · simp  · exact le_trans (bpe_psu_merge_bound a b z s t hm).2 (bpe_psu_count_le_max a b s)
See it in the graphDiscuss this resultChallenge it