AFTD Auto-Formalizing Theoretical Domains

bpe_partial_old_pair_count_le

theoremverified

If neither queried symbol is the fresh output symbol, the nonoverlapping count of that ordered pair cannot increase under a partial merge.

Statement

theorem bpe_partial_old_pair_count_le
    (a b z : ℕ) (s t : List ℕ) (hz : z ∉ s)
    (h : bpe_partial_merge a b z s t) (x y : ℕ) (hx : x ≠ z) (hy : y ≠ z) :
    bpe_pair_count x y t ≤ bpe_pair_count x 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 proof shows that partial merges cannot increase the count of pairs (x, y) when neither x nor y is the newly created symbol z. It proceeds by strong induction on the length of the input list s. In each step, prepending a symbol never decreases pair counts, and because x ≠ z, the merged symbol z cannot create any new occurrences of (x, y), while any occurrence in the output must already correspond to an occurrence in the input.

  1. For any list L of length at most 0, prepending w changes the pair count by at most 1 and does not decrease it. automation

    Since L has length at most 0, it must be empty. The pair count of an empty list is 0, and the pair count of a single-element list [w] is also 0, so the bounds 0 ≤ 0 ≤ 1 hold trivially.

    lines 5–7

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

      | 0, w, L, hL => by
          rw [List.length_eq_zero_iff.mp (Nat.le_zero.mp hL)]
          simp [bpe_pair_count]
  2. If L is empty, prepending w preserves or increases the pair count by at most 1. automation

    When L is empty, count([]) = 0 and count([w]) = 0, so the bounds 0 ≤ 0 ≤ 1 hold immediately.

    lines 8–10

      | n+1, w, L, hL => by
          match L, hL with
          | [], _ => simp [bpe_pair_count]
  3. If L = u :: L' and (w, u) = (a, b), prepending w changes the pair count by at most 1 and does not decrease it.

    Since (w, u) matches the target pair (a, b), the pair count of w :: u :: L' is 1 + count(L'). By induction hypothesis on L', count(L') ≤ count(u :: L') ≤ count(L') + 1. The lower bound count(u :: L') ≤ 1 + count(L') follows by linear arithmetic. The upper bound 1 + count(L') ≤ count(u :: L') + 1 reduces to count(L') ≤ count(u :: L'), which holds by the induction hypothesis on L'.

    lines 11–24

    uses List.length_cons, bpe_pocl_count_cons_bounds

          | 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_pocl_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_pocl_count_cons_bounds a b n v L'' (by simp at hL'' ⊢; omega)
                  omega
  4. If L = u :: L' and (w, u) ≠ (a, b), prepending w preserves the pair count. automation

    Since (w, u) does not match (a, b), the pair count of w :: u :: L' is simply count(u :: L'). The bounds count(u :: L') ≤ count(u :: L') ≤ count(u :: L') + 1 then hold trivially.

    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 a list s of length at most 0, any partial merge preserves or decreases the pair count of (x, y). automation

    Since s has length at most 0, s is the empty list. By definition of partial merge, the output t must also be empty. Both counts are 0, so the inequality holds by reflexivity.

    lines 36–39

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

      | 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
  6. If s is empty, any partial merge preserves or decreases the pair count of (x, y). automation

    When s is empty, the partial merge relation forces t to be empty, and the inequality holds by reflexivity.

    line 40

    uses le_rfl

      | n+1, [], t, _, h => by simp [bpe_partial_merge] at h; subst h; exact le_rfl
  7. If s has length 1, any partial merge preserves or decreases the pair count of (x, y). automation

    When s = [x0], a partial merge cannot merge any pair, so t = [x0], and the claim holds by reflexivity.

    line 41

    uses le_rfl

      | n+1, [x0], t, _, h => by simp [bpe_partial_merge] at h; subst h; exact le_rfl
  8. If the first symbol x0 is kept and the merged tail is empty, the pair count does not increase. automation

    When x0 is kept and out is empty, the output is [x0], which has pair count 0, so the inequality holds trivially.

    lines 42–49

    uses List.length_cons, bpe_pocl_main

      | n+1, x0 :: y0 :: xs, t, hs, h => by
          simp only [List.length_cons] at hs
          rcases h with ⟨out, rfl, hout⟩ | ⟨hxa, hyb, out, rfl, hout⟩
          · -- keep x0
            have ih := bpe_pocl_main a b z x y hx hy n (y0 :: xs) out (by simp; omega) hout
            match out, hout, ih with
            | [], _, _ => simp [bpe_pair_count]
            | o1 :: out', hout', ih' =>
  9. If x0 is kept and out = o1 :: out' with (x0, o1) = (x, y), the pair count does not increase.

    Since (x0, o1) = (x, y), count(t) = 1 + count(out'). Because y ≠ z, the first step on y0 :: xs could not have created z, so o1 must be y0, giving y0 = y. If xs is empty, both lists are [x, y]. If xs = x1 :: xs', the input list also starts with (x, y), so count(s) = 1 + count(x1 :: xs'). By induction hypothesis on the remaining merge, count(out') ≤ count(x1 :: xs'), which completes the inequality.

    lines 50–69

    uses bpe_pocl_main, List.cons.injEq

              by_cases hxo : x0 = x ∧ o1 = y
              · -- o1 is an old symbol, so it is y0 and the rest is a merge of xs
                have hc1 : bpe_pair_count x y (x0 :: o1 :: out') = 1 + bpe_pair_count x y out' := by
                  simp [bpe_pair_count, hxo]
                match xs, hout', hs with
                | [], hout'', _ =>
                  simp [bpe_partial_merge] at hout''
                  obtain ⟨rfl, rfl⟩ := hout''
                  simp [bpe_pair_count, hxo]
                | x1 :: xs', hout'', hs' =>
                  rcases hout'' with ⟨out2, hout2, hpm⟩ | ⟨-, -, out2, hout2, hpm⟩
                  · simp only [List.cons.injEq] at hout2
                    obtain ⟨ho1, hoe⟩ := hout2
                    have hy0 : y0 = y := ho1.symm.trans hxo.2
                    have hc2 : bpe_pair_count x y (x0 :: y0 :: x1 :: xs') = 1 + bpe_pair_count x y (x1 :: xs') := by
                      simp [bpe_pair_count, hxo.1, hy0]
                    have ih2 := bpe_pocl_main a b z x y hx hy n (x1 :: xs') out2 (by simp at hs' ⊢; omega) hpm
                    rw [hc1, hc2, hoe]; omega
                  · simp only [List.cons.injEq] at hout2
                    exact absurd (hxo.2.symm.trans hout2.1) hy
  10. If x0 is kept and out = o1 :: out' with (x0, o1) ≠ (x, y), the pair count does not increase.

    Since the first two output symbols do not form the pair (x, y), count(t) = count(o1 :: out'). By induction hypothesis, count(o1 :: out') ≤ count(y0 :: xs). Since prepending x0 cannot decrease the count (bpe_pocl_cons_le), transitivity yields the result.

    lines 70–73

    uses le_trans, bpe_pocl_cons_le

              · have hc1 : bpe_pair_count x y (x0 :: o1 :: out') = bpe_pair_count x y (o1 :: out') := by
                  simp [bpe_pair_count, hxo]
                rw [hc1]
                exact le_trans ih' (bpe_pocl_cons_le x y x0 (y0 :: xs))
  11. If (x0, y0) is merged into z, the pair count does not increase.

    Here t = z :: out where out is a partial merge of xs. By induction hypothesis, count(out) ≤ count(xs). Since x ≠ z, the symbol z cannot form an (x, y) pair with the next symbol, so count(z :: out) ≤ count(out). Monotonicity of prepending y0 and x0 (bpe_pocl_cons_le) shows count(xs) ≤ count(x0 :: y0 :: xs), completing the bound by transitivity.

    lines 74–82

    uses bpe_pocl_main, le_trans, bpe_pocl_cons_le

          · -- merge a b into z
            have ih := bpe_pocl_main a b z x y hx hy n xs out (by omega) hout
            have hz : bpe_pair_count x y (z :: out) ≤ bpe_pair_count x y out := by
              match out with
              | [] => simp [bpe_pair_count]
              | o :: out' =>
                have : ¬ (z = x ∧ o = y) := fun h => hx h.1.symm
                simp [bpe_pair_count, this]
            exact le_trans hz (le_trans ih (le_trans (bpe_pocl_cons_le x y y0 xs) (bpe_pocl_cons_le x y x0 (y0 :: xs))))
  12. The pair count of (x, y) in t is at most that in s under any partial merge, provided x ≠ z and y ≠ z.

    Specialising the helper theorem bpe_pocl_main to the length of s immediately yields the result.

    lines 84–88

    uses bpe_pocl_main, le_rfl

    theorem bpe_partial_old_pair_count_le
        (a b z : ℕ) (s t : List ℕ) (hz : z ∉ s)
        (h : bpe_partial_merge a b z s t) (x y : ℕ) (hx : x ≠ z) (hy : y ≠ z) :
        bpe_pair_count x y t ≤ bpe_pair_count x y s := by
      exact bpe_pocl_main a b z x y hx hy s.length s t le_rfl h
List.length_eq_zero_iff.mp
A list has length 0 if and only if it is the empty list.
Nat.le_zero.mp
If a natural number is at most 0, it is equal to 0.
List.length_cons
The length of a list with an element prepended is one more than the length of the tail.
bpe_pocl_count_cons_bounds
Prepending an element to a list either preserves the pair count or increases it by 1.
bpe_pocl_cons_le
Prepending an element to a list does not decrease its pair count.
bpe_pocl_main
Under a partial merge, the count of any pair of symbols distinct from the merged symbol does not increase.
List.cons.injEq
Two non-empty lists are equal if and only if their heads are equal and their tails are equal.
le_trans
Inequality on numbers is transitive: if a ≤ b and b ≤ c, then a ≤ c.
le_rfl
Inequality on numbers is reflexive: a ≤ a.

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_pocl_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_pocl_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_pocl_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_pocl_cons_le (a b w : ℕ) (L : List ℕ) : bpe_pair_count a b L ≤ bpe_pair_count a b (w :: L) :=  (bpe_pocl_count_cons_bounds a b L.length w L le_rfl).1theorem bpe_pocl_main (a b z x y : ℕ) (hx : x ≠ z) (hy : y ≠ z) :    ∀ (n : ℕ) (s t : List ℕ), s.length ≤ n → bpe_partial_merge a b z s t →      bpe_pair_count x y t ≤ bpe_pair_count x 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; exact le_rfl  | n+1, [x0], t, _, h => by simp [bpe_partial_merge] at h; subst h; exact le_rfl  | n+1, x0 :: y0 :: xs, t, hs, h => by      simp only [List.length_cons] at hs      rcases h with ⟨out, rfl, hout⟩ | ⟨hxa, hyb, out, rfl, hout⟩      · -- keep x0        have ih := bpe_pocl_main a b z x y hx hy n (y0 :: xs) out (by simp; omega) hout        match out, hout, ih with        | [], _, _ => simp [bpe_pair_count]        | o1 :: out', hout', ih' =>          by_cases hxo : x0 = x ∧ o1 = y          · -- o1 is an old symbol, so it is y0 and the rest is a merge of xs            have hc1 : bpe_pair_count x y (x0 :: o1 :: out') = 1 + bpe_pair_count x y out' := by              simp [bpe_pair_count, hxo]            match xs, hout', hs with            | [], hout'', _ =>              simp [bpe_partial_merge] at hout''              obtain ⟨rfl, rfl⟩ := hout''              simp [bpe_pair_count, hxo]            | x1 :: xs', hout'', hs' =>              rcases hout'' with ⟨out2, hout2, hpm⟩ | ⟨-, -, out2, hout2, hpm⟩              · simp only [List.cons.injEq] at hout2                obtain ⟨ho1, hoe⟩ := hout2                have hy0 : y0 = y := ho1.symm.trans hxo.2                have hc2 : bpe_pair_count x y (x0 :: y0 :: x1 :: xs') = 1 + bpe_pair_count x y (x1 :: xs') := by                  simp [bpe_pair_count, hxo.1, hy0]                have ih2 := bpe_pocl_main a b z x y hx hy n (x1 :: xs') out2 (by simp at hs' ⊢; omega) hpm                rw [hc1, hc2, hoe]; omega              · simp only [List.cons.injEq] at hout2                exact absurd (hxo.2.symm.trans hout2.1) hy          · have hc1 : bpe_pair_count x y (x0 :: o1 :: out') = bpe_pair_count x y (o1 :: out') := by              simp [bpe_pair_count, hxo]            rw [hc1]            exact le_trans ih' (bpe_pocl_cons_le x y x0 (y0 :: xs))      · -- merge a b into z        have ih := bpe_pocl_main a b z x y hx hy n xs out (by omega) hout        have hz : bpe_pair_count x y (z :: out) ≤ bpe_pair_count x y out := by          match out with          | [] => simp [bpe_pair_count]          | o :: out' =>            have : ¬ (z = x ∧ o = y) := fun h => hx h.1.symm            simp [bpe_pair_count, this]        exact le_trans hz (le_trans ih (le_trans (bpe_pocl_cons_le x y y0 xs) (bpe_pocl_cons_le x y x0 (y0 :: xs))))theorem bpe_partial_old_pair_count_le    (a b z : ℕ) (s t : List ℕ) (hz : z ∉ s)    (h : bpe_partial_merge a b z s t) (x y : ℕ) (hx : x ≠ z) (hy : y ≠ z) :    bpe_pair_count x y t ≤ bpe_pair_count x y s := by  exact bpe_pocl_main a b z x y hx hy s.length s t le_rfl h
See it in the graphDiscuss this resultChallenge it