AFTD Auto-Formalizing Theoretical Domains

op1287_bpe_two_greedy_three_comparator_sharp_witness

theoremverified

On the displayed eight-symbol string, a two-step greedy run saves three symbols and a three-step partial-merge run saves six symbols. The latter deliberately skips an overlapping occurrence in its first merge.

Statement

theorem op1287_bpe_two_greedy_three_comparator_sharp_witness :
    let s : List ℕ := [0,0,1,0,0,0,1,0]
    let g := bpe_full_merge 0 1 3 (bpe_full_merge 0 0 2 s)
    let v : List ℕ := [4,4]
    bpe_steps bpe_greedy_step 2 s g ∧ s.length - g.length = 3 ∧
      bpe_steps bpe_partial_step 3 s v ∧ s.length - v.length = 6
Source
Exact witness for the two-greedy versus three-comparator bound in OP-1287; experiments 34379 and 34380.
Verified
19 Nov 2026
Axioms
Classical.choiceQuot.soundpropext
Built on 8
  • bpe_full_mergeThe string obtained by scanning left to right and replacing every available nonoverlapping occurrence of the selected pair with the given…
  • 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…
  • bpe_greedy_stepA greedy full-merge step selects any most frequent adjacent pair and a fresh symbol; a string without adjacent pairs remains unchanged.
  • 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_stepsExactly k applications of the specified binary relation connect the input and output strings.
  • bpe_worst_case_ratio_three_upper_boundThe worst-case ratio of greedy Byte-Pair Encoding to optimal partial-merge utility for three merges is at most 7/12.
  • 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 constructs an explicit 8-symbol string s = [0, 0, 1, 0, 0, 0, 1, 0] to show that greedy Byte Pair Encoding (BPE) can be strictly suboptimal compared to partial merges. A two-step greedy run merges the most frequent pair (0, 0) left-to-right on the overlapping triple 0, 0, 0 and then merges (0, 1), shortening the string to length 5 for a saving of 3 symbols. In contrast, an alternative three-step run uses a partial merge that deliberately skips the first zero in the triple to merge the second pair (0, 0), creating two identical blocks [2, 1, 0, 2, 1, 0]. Two subsequent full merges of (1, 0) and (2, 3) contract these repeating blocks all the way down to [4, 4] of length 2, saving 6 symbols.

  1. The theorem reduces to verifying the two-step greedy run and the three-step partial-merge run, with the length reductions holding by computation.

    Unfolding the `let`-definitions for s, g, and v, the goal is a four-way conjunction: the greedy execution of length 2, the greedy length reduction 8 - 5 = 3, the partial-merge execution of length 3, and the partial-merge length reduction 8 - 2 = 6. The two arithmetic equalities on lengths are discharged immediately by computation (`decide`), leaving two subgoals for the respective merge sequences.

    lines 10–11

      dsimp only
      refine ⟨?_, by decide, ?_, by decide⟩
  2. A two-step greedy BPE run transforms s into g = [2, 1, 2, 3, 0].

    In the initial string s = [0, 0, 1, 0, 0, 0, 1, 0], the pair (0, 0) attains the maximal count of 2, and symbol 2 is fresh; a full merge produces [2, 1, 2, 0, 1, 0]. In this intermediate string, every adjacent pair occurs at most once, making (0, 1) maximal with count 1; full-merging (0, 1) into fresh symbol 3 yields g = [2, 1, 2, 3, 0]. In both steps, maximality and freshness are verified by `decide`, and the greedy step disjunction is satisfied via `Or.inr`.

    lines 12–13

    uses Or.inr

      · refine ⟨_, Or.inr ⟨0,0,2, by decide, by decide, by decide, rfl⟩,
          _, Or.inr ⟨0,1,3, by decide, by decide, by decide, rfl⟩, rfl⟩
  3. A partial merge of (0, 0) into symbol 2 on s can produce [2, 1, 0, 2, 1, 0].

    In the subsegment 0, 0, 0 of s, a greedy merge would consume the first two zeros. By the definition of `bpe_partial_merge`, one is permitted to skip occurrences; skipping the first zero and merging the second pair instead transforms 0, 0, 0 into 0, 2. The entire string becomes [2, 1, 0, 2, 1, 0], which consists of two repeating identical blocks [2, 1, 0]. The tactic `simp` proves this is a valid partial merge by expanding `bpe_partial_merge`.

    lines 14–15

      · have hfirst : bpe_partial_merge 0 0 2 [0,0,1,0,0,0,1,0] [2,1,0,2,1,0] := by
          simp [bpe_partial_merge]
  4. A three-step partial-merge sequence transforms s into v = [4, 4].

    The derivation chains three steps: first, the partial merge from `hfirst` reaches [2, 1, 0, 2, 1, 0] with 2 fresh; second, full-merging (1, 0) into 3 produces [2, 3, 2, 3] with 3 fresh; third, full-merging (2, 3) into 4 produces [4, 4] with 4 fresh. The second and third steps use `bpe3ub_partial_of_full` to view full merges as partial merges, and `rfl` closes the 0-step base case because the final list evaluates definitionally to [4, 4].

    lines 16–19

    uses Or.inr, bpe3ub_partial_of_full

        refine ⟨[2,1,0,2,1,0], Or.inr ⟨0,0,2, by decide, hfirst⟩,
          _, Or.inr ⟨1,0,3, by decide, bpe3ub_partial_of_full 1 0 3 _⟩,
          _, Or.inr ⟨2,3,4, by decide, bpe3ub_partial_of_full 2 3 4 _⟩, ?_⟩
        rfl
Or.inr
Constructs a proof of the disjunction A ∨ B from a proof of B.
bpe3ub_partial_of_full
Every full merge of a pair (a, b) into a new symbol z is also a valid partial merge.

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

set_option maxRecDepth 8192set_option maxHeartbeats 4000000theorem op1287_bpe_two_greedy_three_comparator_sharp_witness :    let s : List ℕ := [0,0,1,0,0,0,1,0]    let g := bpe_full_merge 0 1 3 (bpe_full_merge 0 0 2 s)    let v : List ℕ := [4,4]    bpe_steps bpe_greedy_step 2 s g ∧ s.length - g.length = 3 ∧      bpe_steps bpe_partial_step 3 s v ∧ s.length - v.length = 6 := by  dsimp only  refine ⟨?_, by decide, ?_, by decide⟩  · refine ⟨_, Or.inr ⟨0,0,2, by decide, by decide, by decide, rfl⟩,      _, Or.inr ⟨0,1,3, by decide, by decide, by decide, rfl⟩, rfl⟩  · have hfirst : bpe_partial_merge 0 0 2 [0,0,1,0,0,0,1,0] [2,1,0,2,1,0] := by      simp [bpe_partial_merge]    refine ⟨[2,1,0,2,1,0], Or.inr ⟨0,0,2, by decide, hfirst⟩,      _, Or.inr ⟨1,0,3, by decide, bpe3ub_partial_of_full 1 0 3 _⟩,      _, Or.inr ⟨2,3,4, by decide, bpe3ub_partial_of_full 2 3 4 _⟩, ?_⟩    rfl
See it in the graphDiscuss this resultChallenge it