AFTD Auto-Formalizing Theoretical Domains

bpe_partial_new_right_count_le

theoremverified

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

Statement

theorem bpe_partial_new_right_count_le
    (a b z : ℕ) (s t : List ℕ) (hz : z ∉ s)
    (h : bpe_partial_merge a b z s t) (x : ℕ) (hx : x ≠ z) :
    bpe_pair_count x z t ≤ bpe_pair_count x a 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 states that after partially merging adjacent pairs (a, b) into a fresh symbol z in a sequence s (where z ∉ s and x ≠ z), the nonoverlapping count of (x, z) pairs in the resulting sequence t cannot exceed the original count of (x, a) pairs in s. The argument proceeds by induction on a length bound n for s. Because z does not occur in s and x ≠ z, any pair (x, z) in t must originate from an actual merge of (a, b) into z preceded by x, which directly corresponds to an original pair (x, a) in s. When no such pair is formed at the head, the count does not increase, and when a merge occurs elsewhere, the result follows from the induction hypothesis combined with a monotonicity lemma showing that prepending an element never decreases pair counts.

  1. For an empty list L, prepending an element changes the pair count by between 0 and 1.

    When L has length at most 0, it must be the empty list []. By definition, bpe_pair_count a b [] and bpe_pair_count a b [w] both evaluate to 0, so 0 ≤ 0 ≤ 0 + 1 holds immediately.

    lines 1–7

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

    theorem bpe_pnr_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. When (w, u) = (a, b), prepending w to u :: L' increases the count by at most 1 and does not decrease it.

    Since (w, u) matches (a, b), the pair count of w :: u :: L' is 1 + count(a, b, L'). For the lower bound count(a, b, u :: L') ≤ 1 + count(a, b, L'), the induction hypothesis on u :: L' shows that prepending u increases the count of L' by at most 1. For the upper bound 1 + count(a, b, L') ≤ count(a, b, u :: L') + 1, case analysis on L' and the induction hypothesis on its tail show that prepending u never decreases the count of L'. Linear arithmetic finishes both goals.

    lines 8–24

    uses List.length_cons, bpe_pnr_count_cons_bounds

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

    Because (w, u) is not the target pair, the head does not form a match, so count(a, b, w :: u :: L') = count(a, b, u :: L'). The bounds count ≤ count ≤ count + 1 then hold trivially by reflexivity and arithmetic.

    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
  4. The inequality count(x, z, t) ≤ count(x, a, s) holds whenever s has length at most 1.

    When s has length at most 0 or is empty, the partial merge relation forces t to be empty. When s is a singleton [x0], t must also be [x0]. In all these base cases, lists of length at most 1 contain no pairs, so both counts evaluate to 0, satisfying 0 ≤ 0 by reflexivity.

    lines 33–41

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

    theorem bpe_pnr_main (a b z x : ℕ) (hx : x ≠ z) :
        ∀ (n : ℕ) (s t : List ℕ), s.length ≤ n → z ∉ s → bpe_partial_merge a b z s t →
          bpe_pair_count x z t ≤ bpe_pair_count x a 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]
  5. For s = x0 :: y0 :: xs not containing z, none of x0, y0, or any element of xs equals z.

    Because z ∉ s by assumption, head and tail membership lemmas show that x0 ≠ z, y0 ≠ z, and z ∉ xs. This guarantees that any occurrence of z in the output must be created by a merge of (a, b), rather than inherited from s.

    lines 42–47

    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 hy0 : y0 ≠ z := fun e => hzt (e ▸ List.mem_cons_self)
          have hzxs : z ∉ xs := fun e => hzt (List.mem_cons_of_mem _ e)
  6. When x0 is preserved and forms an (x, z) pair with the head of the output, an (x, a) pair must have existed in s.

    Suppose t = x0 :: out where out is a partial merge of y0 :: xs. If out is empty, t has count 0. If out = o1 :: out' with (x0, o1) = (x, z), the count on t is 1 + count(x, z, out'). Since y0 ≠ z, the merge on y0 :: xs could not have kept y0 as the leading element o1; it must have merged a pair (y0, x1) = (a, b) into z. This implies y0 = a and x0 = x, so s begins with an (x, a) pair, giving count(x, a, s) = 1 + count(x, a, x1 :: xs'). By the induction hypothesis on xs' and monotonicity (bpe_pnr_cons_le), count(x, z, out') ≤ count(x, a, xs') ≤ count(x, a, x1 :: xs'), and adding 1 completes the bound.

    lines 48–71

    uses bpe_pnr_main, List.cons.injEq, List.mem_cons_of_mem, bpe_pnr_cons_le

          rcases h with ⟨out, rfl, hout⟩ | ⟨hxa, hyb, out, rfl, hout⟩
          · have ih := bpe_pnr_main a b z x hx n (y0 :: xs) out (by simp; omega) hzt hout
            match out, hout, ih with
            | [], _, _ => simp [bpe_pair_count]
            | o1 :: out', hout', ih' =>
              by_cases hxo : x0 = x ∧ o1 = z
              · have hc1 : bpe_pair_count x z (x0 :: o1 :: out') = 1 + bpe_pair_count x z out' := by
                  simp [bpe_pair_count, hxo]
                match xs, hout', hs, hzxs with
                | [], hout'', _, _ =>
                  simp [bpe_partial_merge] at hout''
                  exact absurd (hout''.1 ▸ hxo.2) hy0
                | x1 :: xs', hout'', hs', hzxs' =>
                  rcases hout'' with ⟨out2, hout2, -⟩ | ⟨hya, hxb, out2, hout2, hpm⟩
                  · simp only [List.cons.injEq] at hout2
                    exact absurd (hout2.1.symm.trans hxo.2) hy0
                  · simp only [List.cons.injEq] at hout2
                    obtain ⟨-, hoe⟩ := hout2
                    have hzxs'' : z ∉ xs' := fun e => hzxs' (List.mem_cons_of_mem _ e)
                    have ih2 := bpe_pnr_main a b z x hx n xs' out2 (by simp at hs' ⊢; omega) hzxs'' hpm
                    have hc2 : bpe_pair_count x a (x0 :: y0 :: x1 :: xs') = 1 + bpe_pair_count x a (x1 :: xs') := by
                      simp [bpe_pair_count, hxo.1, hya]
                    have hmono := bpe_pnr_cons_le x a x1 xs'
                    rw [hc1, hc2, hoe]; omega
  7. When x0 is preserved but does not form an (x, z) pair with o1, the bound holds by monotonicity.

    Because (x0, o1) ≠ (x, z), the leading element contributes nothing, so count(x, z, x0 :: o1 :: out') = count(x, z, o1 :: out'). By the induction hypothesis on y0 :: xs, this is at most count(x, a, y0 :: xs). Prepending x0 cannot decrease this count by bpe_pnr_cons_le, giving the claim by transitivity.

    lines 72–75

    uses le_trans, bpe_pnr_cons_le

              · have hc1 : bpe_pair_count x z (x0 :: o1 :: out') = bpe_pair_count x z (o1 :: out') := by
                  simp [bpe_pair_count, hxo]
                rw [hc1]
                exact le_trans ih' (bpe_pnr_cons_le x a x0 (y0 :: xs))
  8. When (x0, y0) = (a, b) is merged into z, the bound holds because z cannot start an (x, z) pair.

    Here t = z :: out, where out is a partial merge of xs. Since x ≠ z, the leading symbol z cannot equal x, so count(x, z, z :: out) ≤ count(x, z, out). By the induction hypothesis on xs, this is at most count(x, a, xs). Two applications of monotonicity (bpe_pnr_cons_le) show that count(x, a, xs) ≤ count(x, a, y0 :: xs) ≤ count(x, a, x0 :: y0 :: xs), and transitivity completes the inequality.

    lines 76–83

    uses bpe_pnr_main, le_trans, bpe_pnr_cons_le

          · have ih := bpe_pnr_main a b z x hx n xs out (by omega) hzxs hout
            have hz' : bpe_pair_count x z (z :: out) ≤ bpe_pair_count x z out := by
              match out with
              | [] => simp [bpe_pair_count]
              | o :: out' =>
                have : ¬ (z = x ∧ o = z) := fun h => hx h.1.symm
                simp [bpe_pair_count, this]
            exact le_trans hz' (le_trans ih (le_trans (bpe_pnr_cons_le x a y0 xs) (bpe_pnr_cons_le x a x0 (y0 :: xs))))
  9. The main theorem bpe_partial_new_right_count_le holds for any input lists s and t.

    The theorem follows immediately by applying the inductive lemma bpe_pnr_main with the length bound n chosen to be s.length, using le_rfl to satisfy s.length ≤ s.length.

    lines 85–89

    uses bpe_pnr_main, le_rfl

    theorem bpe_partial_new_right_count_le
        (a b z : ℕ) (s t : List ℕ) (hz : z ∉ s)
        (h : bpe_partial_merge a b z s t) (x : ℕ) (hx : x ≠ z) :
        bpe_pair_count x z t ≤ bpe_pair_count x a s := by
      exact bpe_pnr_main a b z x hx 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).
Nat.le_zero.mp
A natural number less than or equal to 0 must equal 0.
List.length_cons
The length of a prepended list (a :: as) is as.length + 1.
bpe_pnr_count_cons_bounds
Prepending an element w to a list L changes its nonoverlapping pair count by at least 0 and at most 1.
le_rfl
Reflexivity of inequality: a ≤ a.
List.mem_cons_self
An element belongs to any list of which it is the head (a ∈ a :: l).
List.mem_cons_of_mem
If an element belongs to a list l, it also belongs to y :: l.
bpe_pnr_main
In a partial merge of (a, b) into fresh symbol z from s, the count of (x, z) in the result is bounded by the count of (x, a) in s, by induction on the length bound n.
List.cons.injEq
List construction is injective: head :: tail = head' :: tail' if and only if head = head' and tail = tail'.
bpe_pnr_cons_le
Prepending an element to a list never decreases the nonoverlapping pair count.
le_trans
Transitivity of inequality: 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_pnr_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_pnr_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_pnr_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_pnr_cons_le (a b w : ℕ) (L : List ℕ) : bpe_pair_count a b L ≤ bpe_pair_count a b (w :: L) :=  (bpe_pnr_count_cons_bounds a b L.length w L le_rfl).1theorem bpe_pnr_main (a b z x : ℕ) (hx : x ≠ z) :    ∀ (n : ℕ) (s t : List ℕ), s.length ≤ n → z ∉ s → bpe_partial_merge a b z s t →      bpe_pair_count x z t ≤ bpe_pair_count x a 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 hy0 : y0 ≠ z := fun e => hzt (e ▸ List.mem_cons_self)      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_pnr_main a b z x hx n (y0 :: xs) out (by simp; omega) hzt hout        match out, hout, ih with        | [], _, _ => simp [bpe_pair_count]        | o1 :: out', hout', ih' =>          by_cases hxo : x0 = x ∧ o1 = z          · have hc1 : bpe_pair_count x z (x0 :: o1 :: out') = 1 + bpe_pair_count x z out' := by              simp [bpe_pair_count, hxo]            match xs, hout', hs, hzxs with            | [], hout'', _, _ =>              simp [bpe_partial_merge] at hout''              exact absurd (hout''.1 ▸ hxo.2) hy0            | x1 :: xs', hout'', hs', hzxs' =>              rcases hout'' with ⟨out2, hout2, -⟩ | ⟨hya, hxb, out2, hout2, hpm⟩              · simp only [List.cons.injEq] at hout2                exact absurd (hout2.1.symm.trans hxo.2) hy0              · simp only [List.cons.injEq] at hout2                obtain ⟨-, hoe⟩ := hout2                have hzxs'' : z ∉ xs' := fun e => hzxs' (List.mem_cons_of_mem _ e)                have ih2 := bpe_pnr_main a b z x hx n xs' out2 (by simp at hs' ⊢; omega) hzxs'' hpm                have hc2 : bpe_pair_count x a (x0 :: y0 :: x1 :: xs') = 1 + bpe_pair_count x a (x1 :: xs') := by                  simp [bpe_pair_count, hxo.1, hya]                have hmono := bpe_pnr_cons_le x a x1 xs'                rw [hc1, hc2, hoe]; omega          · have hc1 : bpe_pair_count x z (x0 :: o1 :: out') = bpe_pair_count x z (o1 :: out') := by              simp [bpe_pair_count, hxo]            rw [hc1]            exact le_trans ih' (bpe_pnr_cons_le x a x0 (y0 :: xs))      · have ih := bpe_pnr_main a b z x hx n xs out (by omega) hzxs hout        have hz' : bpe_pair_count x z (z :: out) ≤ bpe_pair_count x z out := by          match out with          | [] => simp [bpe_pair_count]          | o :: out' =>            have : ¬ (z = x ∧ o = z) := fun h => hx h.1.symm            simp [bpe_pair_count, this]        exact le_trans hz' (le_trans ih (le_trans (bpe_pnr_cons_le x a y0 xs) (bpe_pnr_cons_le x a x0 (y0 :: xs))))theorem bpe_partial_new_right_count_le    (a b z : ℕ) (s t : List ℕ) (hz : z ∉ s)    (h : bpe_partial_merge a b z s t) (x : ℕ) (hx : x ≠ z) :    bpe_pair_count x z t ≤ bpe_pair_count x a s := by  exact bpe_pnr_main a b z x hx s.length s t le_rfl hz h
See it in the graphDiscuss this resultChallenge it