AFTD Auto-Formalizing Theoretical Domains

bpe_partial_new_left_count_le

theoremverified

After a partial merge of a,b into a fresh symbol z, the nonoverlapping count of (z,y), for y different from z, is at most the original count of (b,y).

Statement

theorem bpe_partial_new_left_count_le
    (a b z : ℕ) (s t : List ℕ) (hz : z ∉ s)
    (h : bpe_partial_merge a b z s t) (y : ℕ) (hy : y ≠ z) :
    bpe_pair_count z y t ≤ bpe_pair_count b y s
Source
Helper for arXiv:2611.08671v1, Lemma 17, and OP-1287: compare one pair count across a partial merge.
Verified
20 Nov 2026
Axioms
Classical.choiceQuot.soundpropext
Built on 2
  • 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.

The proof, explained

The theorem shows that after partially merging pairs of symbols (a, b) in a sequence s into a fresh symbol z, the nonoverlapping count of any pair (z, y) with y ≠ z in the merged sequence t cannot exceed the original count of (b, y) in s. The proof proceeds by strong induction on the length of s via a helper lemma bpe_pnl_main, supported by a monotonicity lemma bpe_pnl_cons_le showing that prepending any symbol cannot decrease pair counts. If the head pair (a, b) is not merged, the head symbol cannot be z (since z is fresh), so no new (z, y) pair is created and the claim follows by induction; if (a, b) is merged into z, a new (z, y) pair can appear at the head of t only when the next symbol is y, in which case s contains the substring b, y, providing an original (b, y) pair that compensates for the new pair.

  1. Prepending an element w to an empty list changes its pair count by at most 1 and does not decrease it.

    Under the length bound n = 0, the hypothesis |L| ≤ 0 implies |L| = 0 via Nat.le_zero.mp, so L = [] by List.length_eq_zero_iff.mp. Both [] and [w] contain no pairs, so their pair counts evaluate to 0, satisfying 0 ≤ 0 ≤ 0 + 1.

    lines 1–7

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

    theorem bpe_pnl_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]
  2. In the inductive step n + 1, the bounds hold when L is empty, and for L = u :: L' the length hypothesis simplifies to |L'| + 1 ≤ n + 1.

    When L is empty, both counts are 0 as before. When L = u :: L', rewriting with List.length_cons expresses the length of u :: L' as |L'| + 1, setting up case analysis on whether (w, u) = (a, b).

    lines 8–12

    uses List.length_cons

      | n+1, w, L, hL => by
          match L, hL with
          | [], _ => simp [bpe_pair_count]
          | u :: L', hL' =>
            simp only [List.length_cons] at hL'
  3. When w = a and u = b, prepending w to u :: L' preserves the lower bound and increases the count by at most 1.

    Because (w, u) = (a, b), the list w :: u :: L' begins with a target pair, so its count is 1 + count(L'). By the inductive hypothesis on u :: L', count(L') ≤ count(u :: L') ≤ count(L') + 1, giving the lower bound count(u :: L') ≤ 1 + count(L'). For the upper bound 1 + count(L') ≤ count(u :: L') + 1, it suffices that count(L') ≤ count(u :: L'), which holds trivially if L' is empty or by a second inductive call on the tail if L' = v :: L''. Linear arithmetic (omega) closes both goals.

    lines 13–24

    uses bpe_pnl_count_cons_bounds

            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_pnl_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_pnl_count_cons_bounds a b n v L'' (by simp at hL'' ⊢; omega)
                  omega
  4. When (w, u) ≠ (a, b), prepending w to u :: L' leaves the pair count unchanged, satisfying both bounds.

    Since the first two elements do not match (a, b), the recursive definition of bpe_pair_count skips w, so count(w :: u :: L') = count(u :: L'). The bounds count(u :: L') ≤ count(u :: L') ≤ count(u :: L') + 1 hold immediately.

    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. Prepending any element to a list never decreases its pair count: count(L) ≤ count(w :: L).

    The lemma bpe_pnl_cons_le extracts the first conjunct of bpe_pnl_count_cons_bounds, using reflexivity |L| ≤ |L| (le_rfl) as the length bound.

    lines 30–31

    uses bpe_pnl_count_cons_bounds, le_rfl

    theorem bpe_pnl_cons_le (a b w : ℕ) (L : List ℕ) : bpe_pair_count a b L ≤ bpe_pair_count a b (w :: L) :=
      (bpe_pnl_count_cons_bounds a b L.length w L le_rfl).1
  6. Base case n = 0 of bpe_pnl_main: if |s| ≤ 0, then count(z, y, t) ≤ count(b, y, s).

    The hypothesis |s| ≤ 0 forces s = [] via Nat.le_zero.mp and List.length_eq_zero_iff.mp. A partial merge of the empty list must yield t = [], so both pair counts are 0, and 0 ≤ 0 holds by reflexivity (le_rfl).

    lines 33–39

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

    theorem bpe_pnl_main (a b z y : ℕ) (hy : y ≠ z) :
        ∀ (n : ℕ) (s t : List ℕ), s.length ≤ n → z ∉ s → bpe_partial_merge a b z s t →
          bpe_pair_count z y t ≤ bpe_pair_count b y s
      | 0, s, t, hs, _, h => by
          rw [List.length_eq_zero_iff.mp (Nat.le_zero.mp hs)] at h ⊢
          simp [bpe_partial_merge] at h
          subst h; exact le_rfl
  7. In the inductive step n + 1, the inequality holds when s is empty or a singleton list [x₀].

    By definition of bpe_partial_merge, if s = [] then t = [], and if s = [x₀] then t = [x₀]. In both cases, lists of length at most 1 contain no pairs, so both counts evaluate to 0 and 0 ≤ 0 holds.

    lines 40–41

      | n+1, [], t, _, _, h => by simp [bpe_partial_merge] at h; subst h; simp [bpe_pair_count]
      | n+1, [x0], t, _, _, h => by simp [bpe_partial_merge] at h; subst h; simp [bpe_pair_count]
  8. For s = x₀ :: y₀ :: xs, the symbol z does not equal x₀ and does not appear in y₀ :: xs or xs.

    The length of s is simplified with List.length_cons. Because z ∉ s by assumption, z cannot be the head element x₀ (by List.mem_cons_self), nor can it belong to the tail y₀ :: xs or xs (by List.mem_cons_of_mem).

    lines 42–46

    uses List.length_cons, List.mem_cons_self, List.mem_cons_of_mem

      | n+1, x0 :: y0 :: xs, t, hs, hz, h => by
          simp only [List.length_cons] at hs
          have hz0 : x0 ≠ z := fun e => hz (e ▸ List.mem_cons_self)
          have hzt : z ∉ y0 :: xs := fun e => hz (List.mem_cons_of_mem _ e)
          have hzxs : z ∉ xs := fun e => hzt (List.mem_cons_of_mem _ e)
  9. If no merge occurs at the head (t = x₀ :: out), then count(z, y, t) ≤ count(b, y, s).

    Here y₀ :: xs partially merges to out. By the inductive hypothesis bpe_pnl_main on y₀ :: xs, count(z, y, out) ≤ count(b, y, y₀ :: xs). If out is empty, t = [x₀] has count 0. If out = o₁ :: out', the head pair (x₀, o₁) cannot be (z, y) because x₀ ≠ z, so count(z, y, x₀ :: o₁ :: out') = count(z, y, o₁ :: out'). Monotonicity (bpe_pnl_cons_le) shows count(b, y, y₀ :: xs) ≤ count(b, y, x₀ :: y₀ :: xs), and transitivity (le_trans) concludes the step.

    lines 47–56

    uses bpe_pnl_main, le_trans, bpe_pnl_cons_le

          rcases h with ⟨out, rfl, hout⟩ | ⟨hxa, hyb, out, rfl, hout⟩
          · have ih := bpe_pnl_main a b z y hy n (y0 :: xs) out (by simp; omega) hzt hout
            match out, hout, ih with
            | [], _, _ => simp [bpe_pair_count]
            | o1 :: out', hout', ih' =>
              have hxo : ¬ (x0 = z ∧ o1 = y) := fun h => hz0 h.1
              have hc1 : bpe_pair_count z y (x0 :: o1 :: out') = bpe_pair_count z y (o1 :: out') := by
                simp [bpe_pair_count, hxo]
              rw [hc1]
              exact le_trans ih' (bpe_pnl_cons_le b y x0 (y0 :: xs))
  10. If a head merge occurs (x₀ = a, y₀ = b, t = z :: out) and out is empty, the inequality holds.

    Substituting y₀ = b, if out is empty then t = [z], which contains no pairs, giving a count of 0. Since pair counts are non-negative, 0 ≤ count(b, y, s) is immediate.

    lines 57–60

          · subst y0
            match out, hout with
            | [], _ => simp [bpe_pair_count]
            | o :: out', hout' =>
  11. When a head merge occurs with out = o :: out', o = y, and |xs| ≤ 1, the inequality holds.

    Because o = y, the list t begins with z :: y :: out', so its count is 1 + count(z, y, out'). The tail xs cannot be empty because [] cannot merge to a non-empty list. If xs = [x₁], then o = x₁ and out' = [], so count(z, y, t) = 1 + 0 = 1. In s = x₀ :: b :: [o], the adjacent elements b and o form a pair (b, o), ensuring count(b, o, s) ≥ 1.

    lines 61–77

              by_cases hoy : o = y
              · subst hoy
                have hc1 : bpe_pair_count z o (z :: o :: out') = 1 + bpe_pair_count z o out' := by
                  simp [bpe_pair_count]
                match xs, hout', hs, hzxs with
                | [], hout'', _, _ => simp [bpe_partial_merge] at hout''
                | [x1], hout'', _, _ =>
                  simp [bpe_partial_merge] at hout''
                  obtain ⟨rfl, rfl⟩ := hout''
                  rw [hc1]
                  have : bpe_pair_count b o (x0 :: b :: [o]) ≥ 1 := by
                    by_cases hh : x0 = b ∧ b = o
                    · simp [bpe_pair_count, hh]
                    · simp [bpe_pair_count, hh]
                  have h0 : bpe_pair_count z o [] = 0 := by simp [bpe_pair_count]
                  rw [h0]
                  exact this
  12. When a head merge occurs with out = o :: out', o = y, and xs = x₁ :: x₂ :: xs', the inequality holds.

    In the merge of x₁ :: x₂ :: xs' to o :: out', the head pair (x₁, x₂) cannot be merged into z because that would force o = z, contradicting y ≠ z since o = y. Thus x₁ is kept unmerged, so o = x₁ and out' = out₂ by List.cons.injEq, with x₂ :: xs' merging to out₂. By the inductive hypothesis bpe_pnl_main on x₂ :: xs', count(z, o, out₂) ≤ count(b, o, x₂ :: xs'). In s = x₀ :: b :: o :: x₂ :: xs', the substring b, o guarantees at least one (b, o) pair, so count(b, o, s) ≥ 1 + count(b, o, x₂ :: xs') using monotonicity (bpe_pnl_cons_le), which matches the 1 + count on the t side.

    lines 78–98

    uses List.cons.injEq, List.mem_cons_of_mem, bpe_pnl_main, bpe_pnl_cons_le

                | x1 :: x2 :: xs', hout'', hs', hzxs' =>
                  rcases hout'' with ⟨out2, hout2, hpm⟩ | ⟨-, -, out2, hout2, -⟩
                  · simp only [List.cons.injEq] at hout2
                    obtain ⟨hox, hoe⟩ := hout2
                    have hzxs'' : z ∉ x2 :: xs' := fun e => hzxs' (List.mem_cons_of_mem _ e)
                    have ih2 := bpe_pnl_main a b z o hy n (x2 :: xs') out2 (by simp at hs' ⊢; omega) hzxs'' hpm
                    have hlow : 1 + bpe_pair_count b o (x2 :: xs') ≤ bpe_pair_count b o (x0 :: b :: x1 :: x2 :: xs') := by
                      rw [← hox]
                      by_cases hh : x0 = b ∧ b = o
                      · have h3 : bpe_pair_count b o (x0 :: b :: o :: x2 :: xs') = 1 + bpe_pair_count b o (o :: x2 :: xs') := by
                          simp [bpe_pair_count, hh]
                        rw [h3]
                        have := bpe_pnl_cons_le b o o (x2 :: xs')
                        omega
                      · have h3 : bpe_pair_count b o (x0 :: b :: o :: x2 :: xs') = bpe_pair_count b o (b :: o :: x2 :: xs') := by
                          simp [bpe_pair_count, hh]
                        rw [h3]
                        simp [bpe_pair_count]
                    rw [hc1, hoe]; omega
                  · simp only [List.cons.injEq] at hout2
                    exact absurd hout2.1 hy
  13. When a head merge occurs with out = o :: out' and o ≠ y, the inequality holds.

    Because o ≠ y, the head pair (z, o) does not match (z, y), so count(z, y, z :: o :: out') = count(z, y, o :: out'). By the inductive hypothesis bpe_pnl_main on xs, this is at most count(b, y, xs). Prepending b and x₀ to xs only increases the pair count by monotonicity (bpe_pnl_cons_le), and transitivity (le_trans) finishes the step.

    lines 99–104

    uses bpe_pnl_main, le_trans, bpe_pnl_cons_le

              · have hc1 : bpe_pair_count z y (z :: o :: out') = bpe_pair_count z y (o :: out') := by
                  simp [bpe_pair_count, hoy]
                have ih := bpe_pnl_main a b z y hy n xs (o :: out') (by omega) hzxs hout'
                rw [hc1]
                exact le_trans ih (le_trans (bpe_pnl_cons_le b y b xs) (bpe_pnl_cons_le b y x0 (b :: xs)))
    
  14. The main theorem bpe_partial_new_left_count_le holds: count(z, y, t) ≤ count(b, y, s).

    The result follows directly by calling the inductive lemma bpe_pnl_main with n = s.length, using reflexivity s.length ≤ s.length (le_rfl), together with the hypotheses z ∉ s and y ≠ z.

    lines 105–109

    uses bpe_pnl_main, le_rfl

    theorem bpe_partial_new_left_count_le
        (a b z : ℕ) (s t : List ℕ) (hz : z ∉ s)
        (h : bpe_partial_merge a b z s t) (y : ℕ) (hy : y ≠ z) :
        bpe_pair_count z y t ≤ bpe_pair_count b y s := by
      exact bpe_pnl_main a b z y hy s.length s t le_rfl hz h
List.length_eq_zero_iff.mp
A list has length 0 if and only if it is the empty list (forward implication: length 0 implies the list is empty).
Nat.le_zero.mp
If a natural number is at most 0, then it must equal 0.
List.length_cons
The length of a list with an element prepended is one more than the length of the tail.
bpe_pnl_count_cons_bounds
Prepending any element to a list changes its count of a given pair by either 0 or 1; in particular, the count never decreases.
le_rfl
The inequality relation is reflexive: every element is less than or equal to itself.
List.mem_cons_self
An element belongs to any list formed by prepending it to a tail.
List.mem_cons_of_mem
If an element belongs to a list, it also belongs to the list with any element prepended.
bpe_pnl_main
If a fresh symbol z is merged from (a, b) and y ≠ z, then any partial merge of s into t produces at most as many (z, y) pairs in t as there were (b, y) pairs in s.
le_trans
The inequality relation is transitive: if a ≤ b and b ≤ c, then a ≤ c.
bpe_pnl_cons_le
Prepending an element to a list cannot decrease its nonoverlapping pair count.
List.cons.injEq
Two non-empty lists are equal if and only if their heads are equal and their tails are equal.

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_pnl_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_pnl_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_pnl_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_pnl_cons_le (a b w : ℕ) (L : List ℕ) : bpe_pair_count a b L ≤ bpe_pair_count a b (w :: L) :=  (bpe_pnl_count_cons_bounds a b L.length w L le_rfl).1theorem bpe_pnl_main (a b z y : ℕ) (hy : y ≠ z) :    ∀ (n : ℕ) (s t : List ℕ), s.length ≤ n → z ∉ s → bpe_partial_merge a b z s t →      bpe_pair_count z y t ≤ bpe_pair_count b y s  | 0, s, t, hs, _, h => by      rw [List.length_eq_zero_iff.mp (Nat.le_zero.mp hs)] at h ⊢      simp [bpe_partial_merge] at h      subst h; exact le_rfl  | n+1, [], t, _, _, h => by simp [bpe_partial_merge] at h; subst h; simp [bpe_pair_count]  | n+1, [x0], t, _, _, h => by simp [bpe_partial_merge] at h; subst h; simp [bpe_pair_count]  | n+1, x0 :: y0 :: xs, t, hs, hz, h => by      simp only [List.length_cons] at hs      have hz0 : x0 ≠ z := fun e => hz (e ▸ List.mem_cons_self)      have hzt : z ∉ y0 :: xs := fun e => hz (List.mem_cons_of_mem _ e)      have hzxs : z ∉ xs := fun e => hzt (List.mem_cons_of_mem _ e)      rcases h with ⟨out, rfl, hout⟩ | ⟨hxa, hyb, out, rfl, hout⟩      · have ih := bpe_pnl_main a b z y hy n (y0 :: xs) out (by simp; omega) hzt hout        match out, hout, ih with        | [], _, _ => simp [bpe_pair_count]        | o1 :: out', hout', ih' =>          have hxo : ¬ (x0 = z ∧ o1 = y) := fun h => hz0 h.1          have hc1 : bpe_pair_count z y (x0 :: o1 :: out') = bpe_pair_count z y (o1 :: out') := by            simp [bpe_pair_count, hxo]          rw [hc1]          exact le_trans ih' (bpe_pnl_cons_le b y x0 (y0 :: xs))      · subst y0        match out, hout with        | [], _ => simp [bpe_pair_count]        | o :: out', hout' =>          by_cases hoy : o = y          · subst hoy            have hc1 : bpe_pair_count z o (z :: o :: out') = 1 + bpe_pair_count z o out' := by              simp [bpe_pair_count]            match xs, hout', hs, hzxs with            | [], hout'', _, _ => simp [bpe_partial_merge] at hout''            | [x1], hout'', _, _ =>              simp [bpe_partial_merge] at hout''              obtain ⟨rfl, rfl⟩ := hout''              rw [hc1]              have : bpe_pair_count b o (x0 :: b :: [o]) ≥ 1 := by                by_cases hh : x0 = b ∧ b = o                · simp [bpe_pair_count, hh]                · simp [bpe_pair_count, hh]              have h0 : bpe_pair_count z o [] = 0 := by simp [bpe_pair_count]              rw [h0]              exact this            | x1 :: x2 :: xs', hout'', hs', hzxs' =>              rcases hout'' with ⟨out2, hout2, hpm⟩ | ⟨-, -, out2, hout2, -⟩              · simp only [List.cons.injEq] at hout2                obtain ⟨hox, hoe⟩ := hout2                have hzxs'' : z ∉ x2 :: xs' := fun e => hzxs' (List.mem_cons_of_mem _ e)                have ih2 := bpe_pnl_main a b z o hy n (x2 :: xs') out2 (by simp at hs' ⊢; omega) hzxs'' hpm                have hlow : 1 + bpe_pair_count b o (x2 :: xs') ≤ bpe_pair_count b o (x0 :: b :: x1 :: x2 :: xs') := by                  rw [← hox]                  by_cases hh : x0 = b ∧ b = o                  · have h3 : bpe_pair_count b o (x0 :: b :: o :: x2 :: xs') = 1 + bpe_pair_count b o (o :: x2 :: xs') := by                      simp [bpe_pair_count, hh]                    rw [h3]                    have := bpe_pnl_cons_le b o o (x2 :: xs')                    omega                  · have h3 : bpe_pair_count b o (x0 :: b :: o :: x2 :: xs') = bpe_pair_count b o (b :: o :: x2 :: xs') := by                      simp [bpe_pair_count, hh]                    rw [h3]                    simp [bpe_pair_count]                rw [hc1, hoe]; omega              · simp only [List.cons.injEq] at hout2                exact absurd hout2.1 hy          · have hc1 : bpe_pair_count z y (z :: o :: out') = bpe_pair_count z y (o :: out') := by              simp [bpe_pair_count, hoy]            have ih := bpe_pnl_main a b z y hy n xs (o :: out') (by omega) hzxs hout'            rw [hc1]            exact le_trans ih (le_trans (bpe_pnl_cons_le b y b xs) (bpe_pnl_cons_le b y x0 (b :: xs)))theorem bpe_partial_new_left_count_le    (a b z : ℕ) (s t : List ℕ) (hz : z ∉ s)    (h : bpe_partial_merge a b z s t) (y : ℕ) (hy : y ≠ z) :    bpe_pair_count z y t ≤ bpe_pair_count b y s := by  exact bpe_pnl_main a b z y hy s.length s t le_rfl hz h
See it in the graphDiscuss this resultChallenge it