AFTD Auto-Formalizing Theoretical Domains

bpe_partial_new_self_count_le

theoremverified

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

Statement

theorem bpe_partial_new_self_count_le
    (a b z : ℕ) (s t : List ℕ) (hz : z ∉ s)
    (h : bpe_partial_merge a b z s t)  :
    bpe_pair_count z z t ≤ bpe_pair_count b 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 is proved by induction on the length of the input list s. Prepending an element to a list is first shown to never decrease pair counts and to increase them by at most one. In the inductive step, if the head symbol is not merged, or is merged to z but followed by a symbol other than z, no new (z, z) pair is created and the bound follows from the induction hypothesis and monotonicity. If the merge produces adjacent occurrences (z, z), the freshness assumption z ∉ s forces the second z to arise from another merge of (a, b) immediately following the first, meaning s begins with a, b, a, b. The middle elements of this prefix expose a reversed pair (b, a), providing the count needed in s to compensate for the newly formed (z, z) in t.

  1. Base case of prepending bounds: prepending an element to the empty list leaves the pair count at 0, satisfying both bounds.

    When the list L has length at most 0, it must be empty. Prepending w yields [w]. The definition of bpe_pair_count evaluates to 0 on both [] and [w], so 0 ≤ 0 ≤ 0 + 1 holds trivially.

    lines 1–7

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

    theorem bpe_pns_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. Inductive step of prepending bounds: prepending any element w to a list of length at most n + 1 preserves monotonicity and increases the pair count by at most 1.

    If L is empty, prepending gives [w], which has count 0. If L = u :: L', we case-split on whether (w, u) = (a, b). If they match, bpe_pair_count a b (w :: u :: L') = 1 + bpe_pair_count a b L'. By the inductive hypothesis on L', count(L') ≤ count(u :: L') ≤ count(L') + 1, from which the lower bound follows; the upper bound is established by an additional case split on L' and a second application of the induction hypothesis. If (w, u) ≠ (a, b), the count is unchanged upon prepending w, so both bounds hold immediately.

    lines 8–28

    uses List.length_cons, bpe_pns_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_pns_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_pns_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]; omega
    
  3. Monotonicity of prepending: prepending any element to a list never decreases the target pair count.

    This extracts the first conjunct (count(L) ≤ count(w :: L)) of bpe_pns_count_cons_bounds applied to L with bound n = L.length.

    lines 30–31

    uses bpe_pns_count_cons_bounds, le_rfl

    theorem bpe_pns_cons_le (a b w : ℕ) (L : List ℕ) : bpe_pair_count a b L ≤ bpe_pair_count a b (w :: L) :=
      (bpe_pns_count_cons_bounds a b L.length w L le_rfl).1
  4. Base cases for the main theorem: lists of length at most 1 cannot produce any (z, z) pairs.

    When s has length at most 0, s = [], so the partial merge forces t = [] and 0 ≤ 0 holds. Similarly, when s = [] or s = [x0], the partial merge forces t = [] or t = [x0], and the pair count is 0 on lists of length at most 1.

    lines 33–41

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

    theorem bpe_pns_main (a b z : ℕ) :
        ∀ (n : ℕ) (s t : List ℕ), s.length ≤ n → z ∉ s → bpe_partial_merge a b z s t →
          bpe_pair_count z z t ≤ bpe_pair_count b 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. Inductive step, unmerged head: if the first symbol x0 is not merged, the inequality holds by the inductive hypothesis and monotonicity.

    Assume s = x0 :: y0 :: xs. Since z ∉ s, x0 ≠ z and z ∉ y0 :: xs. In the branch where x0 is kept unmerged, t = x0 :: out where out is a partial merge of y0 :: xs. By the inductive hypothesis, count(z, z, out) ≤ count(b, a, y0 :: xs). Since x0 ≠ z, prepending x0 to out cannot create a new (z, z) pair, so count(z, z, x0 :: out) = count(z, z, out). By prepending monotonicity (bpe_pns_cons_le), count(b, a, y0 :: xs) ≤ count(b, a, x0 :: y0 :: xs), completing the step.

    lines 42–56

    uses List.length_cons, List.mem_cons_self, List.mem_cons_of_mem, bpe_pns_main, le_trans, bpe_pns_cons_le

      | 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_pns_main a b z 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 = z) := fun h => hz0 h.1
              have hc1 : bpe_pair_count z z (x0 :: o1 :: out') = bpe_pair_count z z (o1 :: out') := by
                simp [bpe_pair_count, hxo]
              rw [hc1]
              exact le_trans ih' (bpe_pns_cons_le b a x0 (y0 :: xs))
  6. Inductive step, merged head followed by z: the second z must arise from another merge of (a, b), forcing s to begin with a :: b :: a :: b :: xs'.

    When x0 = a and y0 = b are merged into z, t = z :: out where out is a partial merge of xs. If out is empty, count is 0. If out begins with z, then t starts with z :: z, producing 1 + count(z, z, out'). Because z ∉ xs, xs cannot produce z by passthrough; thus xs must have length at least 2 and begin with x1 = a and x2 = b that merge into the second z, leaving tail xs' merged to out2 = out'. By the inductive hypothesis on xs', count(z, z, out2) ≤ count(b, a, xs').

    lines 57–79

    uses List.mem_cons_self, List.cons.injEq, List.mem_cons_of_mem, bpe_pns_main

          · subst x0 y0
            match out, hout with
            | [], _ => simp [bpe_pair_count]
            | o :: out', hout' =>
              by_cases hoz : o = z
              · subst o
                have hc1 : bpe_pair_count z z (z :: z :: out') = 1 + bpe_pair_count z z out' := by
                  simp [bpe_pair_count]
                match xs, hout', hs, hzxs with
                | [], hout'', _, _ => simp [bpe_partial_merge] at hout''
                | [x1], hout'', _, hzxs' =>
                  simp [bpe_partial_merge] at hout''
                  exact absurd (hout''.1 ▸ List.mem_cons_self) hzxs'
                | x1 :: x2 :: xs', hout'', hs', hzxs' =>
                  rcases hout'' with ⟨out2, hout2, -⟩ | ⟨hx1, hx2, out2, hout2, hpm⟩
                  · simp only [List.cons.injEq] at hout2
                    exact absurd (hout2.1 ▸ List.mem_cons_self) hzxs'
                  · simp only [List.cons.injEq] at hout2
                    obtain ⟨-, hoe⟩ := hout2
                    have hzxs'' : z ∉ xs' := by
                      intro e; exact hzxs' (List.mem_cons_of_mem _ (List.mem_cons_of_mem _ e))
                    have ih2 := bpe_pns_main a b z n xs' out2 (by simp at hs' ⊢; omega) hzxs'' hpm
                    subst x1 x2
  7. Inductive step, merged head followed by z concluded: the prefix a :: b :: a :: b contains a (b, a) pair, so 1 + count(b, a, xs') ≤ count(b, a, s).

    In s = a :: b :: a :: b :: xs', we lower-bound the count of (b, a). If a = b, s begins with four identical symbols, giving 1 + count(a, a, a :: a :: xs'), which is at least 1 + count(a, a, xs') by applying prepending monotonicity twice. If a ≠ b, the first pair (a, b) does not match (b, a), but the middle pair (b, a) matches, yielding 1 + count(b, a, b :: xs') ≥ 1 + count(b, a, xs'). In both cases, the new (z, z) pair in t is offset by the (b, a) pair in s.

    lines 80–95

    uses bpe_pns_cons_le

                    have hlow : 1 + bpe_pair_count b a xs' ≤ bpe_pair_count b a (a :: b :: a :: b :: xs') := by
                      by_cases hab : a = b
                      · subst hab
                        have h3 : bpe_pair_count a a (a :: a :: a :: a :: xs') = 1 + bpe_pair_count a a (a :: a :: xs') := by
                          simp [bpe_pair_count]
                        rw [h3]
                        have := bpe_pns_cons_le a a a xs'
                        have := bpe_pns_cons_le a a a (a :: xs')
                        omega
                      · have hh : ¬ (a = b ∧ b = a) := fun h => hab h.1
                        have h3 : bpe_pair_count b a (a :: b :: a :: b :: xs') = 1 + bpe_pair_count b a (b :: xs') := by
                          simp [bpe_pair_count, hh]
                        rw [h3]
                        have := bpe_pns_cons_le b a b xs'
                        omega
                    rw [hc1, hoe]; omega
  8. Inductive step, merged head followed by o ≠ z: no new (z, z) pair is formed at the head.

    If out = o :: out' with o ≠ z, the pair (z, o) is not (z, z), so count(z, z, z :: o :: out') = count(z, z, o :: out'). By the inductive hypothesis on xs, this is at most count(b, a, xs). Prepending monotonicity applied twice gives count(b, a, xs) ≤ count(b, a, b :: xs) ≤ count(b, a, a :: b :: xs), closing this branch.

    lines 96–100

    uses bpe_pns_main, le_trans, bpe_pns_cons_le

              · have hc1 : bpe_pair_count z z (z :: o :: out') = bpe_pair_count z z (o :: out') := by
                  simp [bpe_pair_count, hoz]
                have ih := bpe_pns_main a b z n xs (o :: out') (by omega) hzxs hout'
                rw [hc1]
                exact le_trans ih (le_trans (bpe_pns_cons_le b a b xs) (bpe_pns_cons_le b a a (b :: xs)))
  9. The main theorem follows immediately by choosing the length bound to be the length of s.

    The theorem bpe_partial_new_self_count_le is established by directly instantiating bpe_pns_main with n = s.length, le_rfl, and the provided hypotheses.

    lines 102–106

    uses bpe_pns_main, le_rfl

    theorem bpe_partial_new_self_count_le
        (a b z : ℕ) (s t : List ℕ) (hz : z ∉ s)
        (h : bpe_partial_merge a b z s t)  :
        bpe_pair_count z z t ≤ bpe_pair_count b a s := by
      exact bpe_pns_main a b z s.length s t le_rfl hz h
List.length_eq_zero_iff.mp
A list of length zero is the empty list.
Nat.le_zero.mp
A natural number that is at most zero equals zero.
List.length_cons
The length of a list with an element prepended is one plus the length of the tail.
bpe_pns_count_cons_bounds
Prepending an element to a list does not decrease the count of a target pair and increases it by at most one.
le_rfl
Any element is less than or equal to itself (reflexivity of the preorder).
bpe_pns_cons_le
Prepending an element to a list does not decrease the count of a target pair (monotonicity).
List.mem_cons_self
An element is a member of the list obtained by prepending it to any list.
List.mem_cons_of_mem
If an element belongs to a list, it also belongs to the list obtained by prepending any element.
bpe_pns_main
Under a partial merge of (a, b) into fresh symbol z for a list of length at most n, the nonoverlapping count of (z, z) in the result is bounded by the count of (b, a) in the original list.
le_trans
Inequality is transitive: if a ≤ b and b ≤ c, then a ≤ c.
List.cons.injEq
Two nonempty 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_pns_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_pns_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_pns_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_pns_cons_le (a b w : ℕ) (L : List ℕ) : bpe_pair_count a b L ≤ bpe_pair_count a b (w :: L) :=  (bpe_pns_count_cons_bounds a b L.length w L le_rfl).1theorem bpe_pns_main (a b z : ℕ) :    ∀ (n : ℕ) (s t : List ℕ), s.length ≤ n → z ∉ s → bpe_partial_merge a b z s t →      bpe_pair_count z z t ≤ bpe_pair_count b 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 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_pns_main a b z 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 = z) := fun h => hz0 h.1          have hc1 : bpe_pair_count z z (x0 :: o1 :: out') = bpe_pair_count z z (o1 :: out') := by            simp [bpe_pair_count, hxo]          rw [hc1]          exact le_trans ih' (bpe_pns_cons_le b a x0 (y0 :: xs))      · subst x0 y0        match out, hout with        | [], _ => simp [bpe_pair_count]        | o :: out', hout' =>          by_cases hoz : o = z          · subst o            have hc1 : bpe_pair_count z z (z :: z :: out') = 1 + bpe_pair_count z z out' := by              simp [bpe_pair_count]            match xs, hout', hs, hzxs with            | [], hout'', _, _ => simp [bpe_partial_merge] at hout''            | [x1], hout'', _, hzxs' =>              simp [bpe_partial_merge] at hout''              exact absurd (hout''.1 ▸ List.mem_cons_self) hzxs'            | x1 :: x2 :: xs', hout'', hs', hzxs' =>              rcases hout'' with ⟨out2, hout2, -⟩ | ⟨hx1, hx2, out2, hout2, hpm⟩              · simp only [List.cons.injEq] at hout2                exact absurd (hout2.1 ▸ List.mem_cons_self) hzxs'              · simp only [List.cons.injEq] at hout2                obtain ⟨-, hoe⟩ := hout2                have hzxs'' : z ∉ xs' := by                  intro e; exact hzxs' (List.mem_cons_of_mem _ (List.mem_cons_of_mem _ e))                have ih2 := bpe_pns_main a b z n xs' out2 (by simp at hs' ⊢; omega) hzxs'' hpm                subst x1 x2                have hlow : 1 + bpe_pair_count b a xs' ≤ bpe_pair_count b a (a :: b :: a :: b :: xs') := by                  by_cases hab : a = b                  · subst hab                    have h3 : bpe_pair_count a a (a :: a :: a :: a :: xs') = 1 + bpe_pair_count a a (a :: a :: xs') := by                      simp [bpe_pair_count]                    rw [h3]                    have := bpe_pns_cons_le a a a xs'                    have := bpe_pns_cons_le a a a (a :: xs')                    omega                  · have hh : ¬ (a = b ∧ b = a) := fun h => hab h.1                    have h3 : bpe_pair_count b a (a :: b :: a :: b :: xs') = 1 + bpe_pair_count b a (b :: xs') := by                      simp [bpe_pair_count, hh]                    rw [h3]                    have := bpe_pns_cons_le b a b xs'                    omega                rw [hc1, hoe]; omega          · have hc1 : bpe_pair_count z z (z :: o :: out') = bpe_pair_count z z (o :: out') := by              simp [bpe_pair_count, hoz]            have ih := bpe_pns_main a b z n xs (o :: out') (by omega) hzxs hout'            rw [hc1]            exact le_trans ih (le_trans (bpe_pns_cons_le b a b xs) (bpe_pns_cons_le b a a (b :: xs)))theorem bpe_partial_new_self_count_le    (a b z : ℕ) (s t : List ℕ) (hz : z ∉ s)    (h : bpe_partial_merge a b z s t)  :    bpe_pair_count z z t ≤ bpe_pair_count b a s := by  exact bpe_pns_main a b z s.length s t le_rfl hz h
See it in the graphDiscuss this resultChallenge it