import Mathlib /-! # Second-Order Non-Asymptotic Upper Bounds for `q`-ary 2-Deletion-Correcting Codes ## Overview For an alphabet size `q ≥ 2` and block length `n ≥ 3`, let `C ⊆ (Fin q)^n` be a `q`-ary 2-deletion-correcting code, i.e., any two distinct codewords `x, x' ∈ C` have disjoint 2-deletion balls `D₂(x) ∩ D₂(x') = ∅` in `(Fin q)^(n - 2)`. ### Historical Context - **Levenshtein (1966)** proved the asymptotic upper bound `M_q(n, 2) ≲ 2 / (q - 1)^2 · q^n / n^2`. - **Kulkarni & Kiyavash (2013, *IEEE Trans. Inform. Theory* 59(8):5115–5130)** introduced the hypergraph-matching LP relaxation and constructed the dual weight `w_KK(y) = 1 / C(r(y) + 1, 2) = 2 / (r(y) * (r(y) + 1))` (where `r(y)` is the number of maximal runs of `y`), obtaining the landmark explicit non-asymptotic upper bound for all `q ≥ 2` and `n ≥ 3`: `|C| ≤ KK₂(q, n) := 2 * (q^n - q * (q - 1) * (n - 1) - q) / ((q - 1)^2 * (n - 1) * (n - 2))`. - **Cullina & Kiyavash (2014, *IEEE Trans. Inform. Theory*)** improved Levenshtein's bound only when `s > q` (yielding zero improvement for `s = 2` and any `q ≥ 2`). - **Fazeli, Vardy & Yaakobi (2015)** evaluated symmetry-reduced LPs numerically for small finite lengths without any closed-form bound beating `KK₂(q, n)`. ### Main Novel Results Formalized Here For every `y ∈ (Fin q)^(n - 2)` with `r(y)` runs, let `r₁(y)` be its number of singleton runs (length 1) and `a₁(y)` be its number of sandwiched singleton runs (interior singleton runs flanked by equal symbols). We define the explicit second-order dual weight `w*(y) = 2 / (r(y) * (r(y) + 1))` for `r(y) ≤ 9`, and `w*(y) = 2 / (r(y) * (r(y) + 1)) - 4 * ((r₁(y) - 2)_+ + (a₁(y) - 4)_+) / ((r(y)+1)(r(y)+2)(r(y)+3)(r(y)+4))` for `r(y) ≥ 10`. We prove completely from first principles (using only standard Mathlib foundations): 1. **Exact 2-Deletion Run-Pair Geometry & 7-Bucket Drop Table** (`del2_injOn_nonRedundant`, `card_del2Ball_ge_nonRedundant`, `card_del2Ball_lower`): Explicit injection from non-redundant run-start pairs into `D₂(x)` partitioned into 7 run-count drop buckets (`r(y) ≤ r - k` for `k ∈ {0, 1, 2, 3, 4}`), together with universal shift bounds `r₁(y) ≤ r₁(x) + 2` and `a₁(y) ≤ a₁(x) + 4`. 2. **Universal LP-Dual Feasibility & Exact Dominance over Kulkarni–Kiyavash (2013)** (`kk2DualWeight_feasible`, `twoDelDualWeight_nonneg`, `twoDelDualWeight_feasible`, `qary_twoDel_exact_dual_beats_kk_all`): For EVERY `q ≥ 2, n ≥ 3`, `|C| ≤ ∑ y, twoDelDualWeight y`, and for EVERY `q ≥ 2, n ≥ 12`, `∑ y, twoDelDualWeight y < kk2Bound q n` strictly. 3. **Exact Conditional Singleton & Sandwich Averages and Closed-Form Second-Order Bound** (`sum_singletonRunCount_given_runCount`, `sum_sandwichCount_given_runCount`, `twoDelDualWeight_sum_le_twoDelUpperBound`, `qary_twoDel_upper_bound`): Exact closed-form upper bound `twoDelUpperBound q n := kk2Bound q n - twoDelSavingsLB q n` valid for EVERY `q ≥ 2, n ≥ 12`. 4. **Universal Strict Closed-Form Dominance & Exact Second-Order Asymptotics** (`qary_twoDel_beats_kk_all`, `nonbinary_twoDel_beats_kk_all`, `dna_twoDel_beats_kk`, `twoDel_second_order_asymptotic_improvement`): - `twoDelUpperBound q n < kk2Bound q n` strictly for ALL `q ≥ 2, n ≥ 34`, ALL `q ≥ 3, n ≥ 25`, and DNA `q = 4, n ≥ 21`. - `(kk2Bound q n - twoDelUpperBound q n) / (q^n / n^3) → 4 * q / (q - 1)^3` as `n → ∞` for EVERY `q ≥ 2` (binary `8 · 2^n / n^3`, ternary `(3/2) · 3^n / n^3`, DNA `(16/27) · 4^n / n^3`). 5. **Self-Contained 2-Deletion Gilbert–Varshamov Lower Bound & Two-Sided Sandwich** (`exists_twoDelCode_gv`, `qary_twoDel_two_sided_sandwich`) for ALL `q ≥ 2, n ≥ 3`. -/ open scoped BigOperators namespace TwoDeletionSecondOrder /-! ## Section 1: Core Definitions, Run Decomposition, and Hypergraph Packing Duality -/ section CoreDefinitions /-- A `q`-ary word of length `n`. -/ abbrev Word (q n : ℕ) := Fin n → Fin q /-- Delete coordinate `i : Fin (n + 1)` from a word `x : Word q (n + 1)`. -/ def delPos {q n : ℕ} (x : Word q (n + 1)) (i : Fin (n + 1)) : Word q n := fun j => x (i.succAbove j) /-- The single-deletion ball `D₁(x) ⊆ Word q n` of `x : Word q (n + 1)`. -/ def del1Ball {q n : ℕ} (x : Word q (n + 1)) : Finset (Word q n) := Finset.univ.image (delPos x) /-- The 2-deletion ball `D₂(x) ⊆ Word q m` of `x : Word q (m + 2)`. -/ def del2Ball {q m : ℕ} (x : Word q (m + 2)) : Finset (Word q m) := (del1Ball x).biUnion del1Ball /-- A `q`-ary 2-deletion-correcting code of length `m + 2`: distinct codewords have disjoint 2-deletion balls. -/ def IsTwoDelCode {q m : ℕ} (C : Finset (Word q (m + 2))) : Prop := ∀ x ∈ C, ∀ y ∈ C, x ≠ y → Disjoint (del2Ball x) (del2Ball y) /-- **Hypergraph Matching LP Duality Lemma for 2-Deletion Codes**: For ANY nonnegative dual weight `w : Word q m → ℝ` satisfying `1 ≤ ∑ y ∈ D₂(x), w y` for every `x`, every 2-deletion code `C ⊆ Word q (m + 2)` satisfies `|C| ≤ ∑ y, w y`. -/ theorem twoDelCode_card_le_dual_sum {q m : ℕ} (C : Finset (Word q (m + 2))) (hC : IsTwoDelCode C) (w : Word q m → ℝ) (hw_nn : ∀ y : Word q m, 0 ≤ w y) (hw_feas : ∀ x : Word q (m + 2), 1 ≤ ∑ y ∈ del2Ball x, w y) : (C.card : ℝ) ≤ ∑ y : Word q m, w y := by have hdisj : Set.PairwiseDisjoint (C : Set (Word q (m + 2))) del2Ball := by intro x hx y hy hne exact hC x hx y hy hne calc (C.card : ℝ) = ∑ _x ∈ C, (1 : ℝ) := by simp _ ≤ ∑ x ∈ C, ∑ y ∈ del2Ball x, w y := Finset.sum_le_sum fun x _ => hw_feas x _ = ∑ y ∈ C.biUnion del2Ball, w y := (Finset.sum_biUnion hdisj).symm _ ≤ ∑ y : Word q m, w y := Finset.sum_le_sum_of_subset_of_nonneg (Finset.subset_univ _) fun y _ _ => hw_nn y /-- Set of run-boundary indices `i : Fin L` where `x i.castSucc ≠ x i.succ`. -/ def boundarySet {q L : ℕ} (x : Word q (L + 1)) : Finset (Fin L) := Finset.univ.filter fun i : Fin L => x i.castSucc ≠ x i.succ /-- Number of maximal constant runs `r(x) = |boundarySet x| + 1`. -/ def runCount {q L : ℕ} (x : Word q (L + 1)) : ℕ := (boundarySet x).card + 1 lemma one_le_runCount {q L : ℕ} (x : Word q (L + 1)) : 1 ≤ runCount x := by unfold runCount; omega lemma runCount_le_len {q L : ℕ} (x : Word q (L + 1)) : runCount x ≤ L + 1 := by unfold runCount have h : (boundarySet x).card ≤ L := by calc (boundarySet x).card ≤ (Finset.univ : Finset (Fin L)).card := Finset.card_filter_le _ _ _ = L := by simp omega /-- Set of run-start indices `p : Fin (L + 1)`: either `0` or immediately after a boundary. -/ def runStarts {q L : ℕ} (x : Word q (L + 1)) : Finset (Fin (L + 1)) := insert 0 ((boundarySet x).image Fin.succ) lemma mem_runStarts_iff {q L : ℕ} (x : Word q (L + 1)) (p : Fin (L + 1)) : p ∈ runStarts x ↔ (p.val = 0 ∨ ∃ h : 0 < p.val, x ⟨p.val - 1, by omega⟩ ≠ x p) := by unfold runStarts boundarySet simp only [Finset.mem_insert, Finset.mem_image, Finset.mem_filter, Finset.mem_univ, true_and] constructor · rintro (rfl | ⟨i, hi, rfl⟩) · left; rfl · right refine ⟨Nat.succ_pos _, ?_⟩ have hcast : (⟨i.val, by omega⟩ : Fin (L + 1)) = i.castSucc := rfl have hsub : (⟨i.succ.val - 1, by omega⟩ : Fin (L + 1)) = i.castSucc := Fin.ext (by simp) rw [hsub] exact hi · rintro (hp0 | ⟨hp, hne⟩) · left; exact Fin.ext hp0 · right refine ⟨⟨p.val - 1, by omega⟩, ?_, Fin.ext (by simp; omega)⟩ have hsucc : (⟨p.val - 1, by omega⟩ : Fin L).succ = p := Fin.ext (by simp; omega) rw [hsucc] simpa [Fin.castSucc] using hne lemma zero_mem_runStarts {q L : ℕ} (x : Word q (L + 1)) : (0 : Fin (L + 1)) ∈ runStarts x := Finset.mem_insert_self _ _ lemma card_runStarts {q L : ℕ} (x : Word q (L + 1)) : (runStarts x).card = runCount x := by unfold runStarts runCount have h0 : (0 : Fin (L + 1)) ∉ (boundarySet x).image Fin.succ := by simp [Fin.succ_ne_zero] rw [Finset.card_insert_of_notMem h0, Finset.card_image_of_injective _ (Fin.succ_injective L)] /-- Positions of singleton (length-1) runs in `x : Word q (L + 1)`: positions differing from both immediate neighbors (when in bounds). -/ def singletonPosSet {q L : ℕ} (x : Word q (L + 1)) : Finset (Fin (L + 1)) := Finset.univ.filter fun p : Fin (L + 1) => (∀ h : 0 < p.val, x ⟨p.val - 1, by omega⟩ ≠ x p) ∧ (∀ h : p.val + 1 < L + 1, x p ≠ x ⟨p.val + 1, h⟩) /-- Number of singleton runs `r₁(x)` of `x : Word q (L + 1)`. -/ def singletonRunCount {q L : ℕ} (x : Word q (L + 1)) : ℕ := (singletonPosSet x).card /-- Positions of sandwiched singleton runs in `x : Word q (L + 1)`: interior positions `p` with `x (p - 1) = x (p + 1) ≠ x p`. -/ def sandwichPosSet {q L : ℕ} (x : Word q (L + 1)) : Finset (Fin (L + 1)) := Finset.univ.filter fun p : Fin (L + 1) => ∃ (h0 : 0 < p.val) (h1 : p.val + 1 < L + 1), x ⟨p.val - 1, by omega⟩ = x ⟨p.val + 1, h1⟩ ∧ x p ≠ x ⟨p.val + 1, h1⟩ /-- Number of sandwiched singleton runs `a₁(x)` of `x : Word q (L + 1)`. -/ def sandwichCount {q L : ℕ} (x : Word q (L + 1)) : ℕ := (sandwichPosSet x).card /-- Run starts of runs of length at least 2 (`L₂`). -/ def longRunStarts {q L : ℕ} (x : Word q (L + 1)) : Finset (Fin (L + 1)) := runStarts x \ singletonPosSet x /-- Run starts of unsandwiched singleton runs (`N₁`). -/ def nonSandSingletonStarts {q L : ℕ} (x : Word q (L + 1)) : Finset (Fin (L + 1)) := singletonPosSet x \ sandwichPosSet x lemma singletonPosSet_subset_runStarts {q L : ℕ} (x : Word q (L + 1)) : singletonPosSet x ⊆ runStarts x := by intro p hp simp only [singletonPosSet, Finset.mem_filter, Finset.mem_univ, true_and] at hp rw [mem_runStarts_iff] by_cases h0 : p.val = 0 · left; exact h0 · right; exact ⟨Nat.pos_of_ne_zero h0, hp.1 (Nat.pos_of_ne_zero h0)⟩ lemma sandwichPosSet_subset_singletonPosSet {q L : ℕ} (x : Word q (L + 1)) : sandwichPosSet x ⊆ singletonPosSet x := by intro p hp simp only [sandwichPosSet, Finset.mem_filter, Finset.mem_univ, true_and] at hp rcases hp with ⟨h0, h1, heq, hne⟩ simp only [singletonPosSet, Finset.mem_filter, Finset.mem_univ, true_and] refine ⟨fun _ => ?_, fun _ => hne⟩ rw [heq] exact hne.symm lemma sandwichPosSet_subset_runStarts {q L : ℕ} (x : Word q (L + 1)) : sandwichPosSet x ⊆ runStarts x := (sandwichPosSet_subset_singletonPosSet x).trans (singletonPosSet_subset_runStarts x) lemma mem_longRunStarts_iff {q L : ℕ} (x : Word q (L + 1)) (p : Fin (L + 1)) : p ∈ longRunStarts x ↔ p ∈ runStarts x ∧ ∃ h : p.val + 1 < L + 1, x p = x ⟨p.val + 1, h⟩ := by unfold longRunStarts rw [Finset.mem_sdiff] constructor · rintro ⟨hrun, hns⟩ refine ⟨hrun, ?_⟩ simp only [singletonPosSet, Finset.mem_filter, Finset.mem_univ, true_and] at hns have hleft : ∀ h : 0 < p.val, x ⟨p.val - 1, by omega⟩ ≠ x p := by intro h rcases (mem_runStarts_iff x p).mp hrun with hp0 | ⟨_, hne⟩ · omega · exact hne have hnr : ¬ ∀ h : p.val + 1 < L + 1, x p ≠ x ⟨p.val + 1, h⟩ := fun hr => hns ⟨hleft, hr⟩ push Not at hnr exact hnr · rintro ⟨hrun, ⟨h1, heq⟩⟩ refine ⟨hrun, ?_⟩ simp only [singletonPosSet, Finset.mem_filter, Finset.mem_univ, true_and] intro h exact h.2 h1 heq lemma card_run_partition {q L : ℕ} (x : Word q (L + 1)) : (longRunStarts x).card + (nonSandSingletonStarts x).card + (sandwichPosSet x).card = runCount x := by have h1 : (longRunStarts x).card + (singletonPosSet x).card = (runStarts x).card := Finset.card_sdiff_add_card_eq_card (singletonPosSet_subset_runStarts x) have h2 : (nonSandSingletonStarts x).card + (sandwichPosSet x).card = (singletonPosSet x).card := Finset.card_sdiff_add_card_eq_card (sandwichPosSet_subset_singletonPosSet x) rw [card_runStarts] at h1 omega lemma singletonRunCount_le_runCount {q L : ℕ} (x : Word q (L + 1)) : singletonRunCount x ≤ runCount x := by have h := card_run_partition x unfold singletonRunCount have h2 := Finset.card_sdiff_add_card_eq_card (sandwichPosSet_subset_singletonPosSet x) unfold nonSandSingletonStarts at h omega lemma sandwichCount_le_singletonRunCount {q L : ℕ} (x : Word q (L + 1)) : sandwichCount x ≤ singletonRunCount x := Finset.card_le_card (sandwichPosSet_subset_singletonPosSet x) lemma runCount_eq_one_long {q L : ℕ} (x : Word q (L + 2)) (hr : runCount x = 1) : (longRunStarts x).card = 1 ∧ (nonSandSingletonStarts x).card = 0 ∧ (sandwichPosSet x).card = 0 := by have hb : boundarySet x = ∅ := by unfold runCount at hr exact Finset.card_eq_zero.mp (by omega) have h01 : x 0 = x 1 := by by_contra hne have hmem : (0 : Fin (L + 1)) ∈ boundarySet x := by simp only [boundarySet, Finset.mem_filter, Finset.mem_univ, true_and] simpa using hne rw [hb] at hmem exact Finset.notMem_empty _ hmem have h0_long : (0 : Fin (L + 2)) ∈ longRunStarts x := by rw [mem_longRunStarts_iff] refine ⟨zero_mem_runStarts x, ⟨by simp, ?_⟩⟩ simpa using h01 have hL_ge : 1 ≤ (longRunStarts x).card := Finset.card_pos.mpr ⟨0, h0_long⟩ have hpart := card_run_partition x omega lemma two_le_long_add_nonSand {q L : ℕ} (x : Word q (L + 1)) (hr : 2 ≤ runCount x) : 2 ≤ (longRunStarts x).card + (nonSandSingletonStarts x).card := by have h0 : (0 : Fin (L + 1)) ∈ runStarts x \ sandwichPosSet x := by rw [Finset.mem_sdiff] refine ⟨zero_mem_runStarts x, ?_⟩ simp only [sandwichPosSet, Finset.mem_filter, Finset.mem_univ, true_and] rintro ⟨hpos, _⟩ simp at hpos have hb_ne : (boundarySet x).Nonempty := by unfold runCount at hr exact Finset.card_pos.mp (by omega) let imax := (boundarySet x).max' hb_ne have himax_mem : imax ∈ boundarySet x := Finset.max'_mem _ hb_ne have hlast_run : imax.succ ∈ runStarts x := by unfold runStarts exact Finset.mem_insert_of_mem (Finset.mem_image_of_mem _ himax_mem) have hlast_not_sand : imax.succ ∉ sandwichPosSet x := by simp only [sandwichPosSet, Finset.mem_filter, Finset.mem_univ, true_and] rintro ⟨_, h1, _, hne⟩ have hgt : imax < ⟨imax.succ.val, by omega⟩ := by change imax.val < imax.val + 1; omega have hmem : (⟨imax.succ.val, by omega⟩ : Fin L) ∈ boundarySet x := by simp only [boundarySet, Finset.mem_filter, Finset.mem_univ, true_and] exact hne have hle := Finset.le_max' (boundarySet x) _ hmem exact not_lt_of_ge hle hgt have hlast : imax.succ ∈ runStarts x \ sandwichPosSet x := Finset.mem_sdiff.mpr ⟨hlast_run, hlast_not_sand⟩ have hne0 : (0 : Fin (L + 1)) ≠ imax.succ := (Fin.succ_ne_zero imax).symm have hcard2 : 2 ≤ (runStarts x \ sandwichPosSet x).card := by have hsub : ({(0 : Fin (L + 1)), imax.succ} : Finset (Fin (L + 1))) ⊆ runStarts x \ sandwichPosSet x := by intro p hp simp only [Finset.mem_insert, Finset.mem_singleton] at hp rcases hp with rfl | rfl <;> assumption calc 2 = ({(0 : Fin (L + 1)), imax.succ} : Finset (Fin (L + 1))).card := by rw [Finset.card_pair hne0] _ ≤ (runStarts x \ sandwichPosSet x).card := Finset.card_le_card hsub have hpart := card_run_partition x have hsdiff : (runStarts x \ sandwichPosSet x).card + (sandwichPosSet x).card = runCount x := by rw [Finset.card_sdiff_add_card_eq_card (sandwichPosSet_subset_runStarts x), card_runStarts] omega lemma sandwichCount_le_runCount_sub_two {q L : ℕ} (x : Word q (L + 1)) : sandwichCount x + 2 ≤ runCount x ∨ runCount x = 1 := by by_cases hr : 2 ≤ runCount x · left have h1 := two_le_long_add_nonSand x hr have h2 := card_run_partition x unfold sandwichCount omega · right have h0 := one_le_runCount x omega end CoreDefinitions /-! ## Section 2: Single-Deletion Run-Drop, Exact 2-Deletion Run-Pair Injection & 7-Bucket Table -/ section RunPairGeometry /-- Helper for evaluating `delPos x p` by comparing `j.val` with `p.val`. -/ lemma delPos_apply_lt {q L : ℕ} (x : Word q (L + 1)) (p : Fin (L + 1)) (j : Fin L) (hj : j.val < p.val) : delPos x p j = x ⟨j.val, by omega⟩ := by unfold delPos congr 1 rw [Fin.succAbove_of_castSucc_lt _ _ (show j.castSucc < p from hj)] rfl lemma delPos_apply_ge {q L : ℕ} (x : Word q (L + 1)) (p : Fin (L + 1)) (j : Fin L) (hj : p.val ≤ j.val) : delPos x p j = x ⟨j.val + 1, by omega⟩ := by unfold delPos congr 1 rw [Fin.succAbove_of_le_castSucc _ _ (show p ≤ j.castSucc from hj)] rfl /-- Deleting any single symbol never increases `runCount`. -/ theorem runCount_delPos_le {q L : ℕ} (x : Word q (L + 2)) (p : Fin (L + 2)) : runCount (delPos x p) ≤ runCount x := by unfold runCount suffices h : (boundarySet (delPos x p)).card ≤ (boundarySet x).card by omega -- Map each boundary j of `delPos x p` injectively into `boundarySet x` let f : Fin L → Fin (L + 1) := fun j => if h1 : j.val + 1 < p.val then ⟨j.val, by omega⟩ else if h2 : p.val ≤ j.val then ⟨j.val + 1, by omega⟩ else if h3 : x ⟨j.val, by omega⟩ ≠ x p then ⟨j.val, by omega⟩ else ⟨j.val + 1, by omega⟩ refine Finset.card_le_card_of_injOn f ?_ ?_ · intro j hj simp only [Finset.mem_coe, boundarySet, Finset.mem_filter, Finset.mem_univ, true_and] at hj ⊢ dsimp only [f] split_ifs with h1 h2 h3 · have ha := delPos_apply_lt x p j.castSucc (by simp; omega) have hb := delPos_apply_lt x p j.succ (by simp; omega) rw [ha, hb] at hj exact hj · have ha := delPos_apply_ge x p j.castSucc (by simp; omega) have hb := delPos_apply_ge x p j.succ (by simp; omega) rw [ha, hb] at hj exact hj · have hp : p = ⟨j.val + 1, by omega⟩ := Fin.ext (by simp; omega) have hsucc : (⟨j.val, by omega⟩ : Fin (L + 1)).succ = p := Fin.ext (by simp; omega) rw [hsucc] exact h3 · have ha := delPos_apply_lt x p j.castSucc (by simp; omega) have hb := delPos_apply_ge x p j.succ (by simp; omega) rw [ha, hb] at hj have hp : p = ⟨j.val + 1, by omega⟩ := Fin.ext (by simp; omega) push Not at h3 intro heq apply hj have h3' : x ⟨j.val, by omega⟩ = x ⟨j.val + 1, by omega⟩ := by simpa [hp] using h3 exact h3'.trans heq · intro j₁ hj₁ j₂ hj₂ hfeq simp only [Finset.mem_coe] at hj₁ hj₂ dsimp only [f] at hfeq have hval := congrArg Fin.val hfeq split_ifs at hval <;> (dsimp only at hval; exact Fin.ext (by omega)) /-- Deleting a singleton run reduces `runCount` by at least `1` (when `runCount x ≥ 2`). -/ theorem runCount_delPos_singleton {q L : ℕ} (x : Word q (L + 2)) (p : Fin (L + 2)) (hp : p ∈ singletonPosSet x) (hr : 2 ≤ runCount x) : runCount (delPos x p) + 1 ≤ runCount x := by simp only [singletonPosSet, Finset.mem_filter, Finset.mem_univ, true_and] at hp unfold runCount at hr ⊢ suffices h : (boundarySet (delPos x p)).card + 1 ≤ (boundarySet x).card by omega let f : Fin L → Fin (L + 1) := fun j => if h1 : j.val + 1 < p.val then ⟨j.val, by omega⟩ else if h2 : p.val ≤ j.val then ⟨j.val + 1, by omega⟩ else ⟨j.val, by omega⟩ have hf_maps : ∀ j ∈ boundarySet (delPos x p), f j ∈ boundarySet x := by intro j hj simp only [boundarySet, Finset.mem_filter, Finset.mem_univ, true_and] at hj ⊢ dsimp only [f] split_ifs with h1 h2 · have ha := delPos_apply_lt x p j.castSucc (by simp; omega) have hb := delPos_apply_lt x p j.succ (by simp; omega) rw [ha, hb] at hj; exact hj · have ha := delPos_apply_ge x p j.castSucc (by simp; omega) have hb := delPos_apply_ge x p j.succ (by simp; omega) rw [ha, hb] at hj; exact hj · have hsucc : (⟨j.val, by omega⟩ : Fin (L + 1)).succ = p := Fin.ext (by simp; omega) have hcast : (⟨j.val, by omega⟩ : Fin (L + 1)).castSucc = ⟨p.val - 1, by omega⟩ := Fin.ext (by simp; omega) rw [hsucc, hcast] exact hp.1 (show 0 < p.val by omega) have hf_inj : Set.InjOn f (boundarySet (delPos x p)) := by intro j₁ _ j₂ _ hfeq dsimp only [f] at hfeq have hval := congrArg Fin.val hfeq split_ifs at hval <;> (dsimp only at hval; exact Fin.ext (by omega)) -- Find an unused boundary in `boundarySet x`! by_cases hp_last : p.val + 1 < L + 2 · -- Position `⟨p.val, _⟩` is a boundary of `x` NOT in the image of `f`! let b0 : Fin (L + 1) := ⟨p.val, by omega⟩ have hb0 : b0 ∈ boundarySet x := by simp only [boundarySet, Finset.mem_filter, Finset.mem_univ, true_and, b0] exact hp.2 hp_last have hb0_not : b0 ∉ (boundarySet (delPos x p)).image f := by simp only [Finset.mem_image] rintro ⟨j, _, hfj⟩ dsimp only [f, b0] at hfj have hval := congrArg Fin.val hfj split_ifs at hval <;> (dsimp only at hval; omega) have hsub : (boundarySet (delPos x p)).image f ⊆ (boundarySet x).erase b0 := by intro b hb rcases Finset.mem_image.mp hb with ⟨j, hj, rfl⟩ exact Finset.mem_erase.mpr ⟨fun heq => hb0_not (heq ▸ Finset.mem_image_of_mem _ hj), hf_maps j hj⟩ have hcard := Finset.card_le_card hsub rw [Finset.card_image_of_injOn hf_inj, Finset.card_erase_of_mem hb0] at hcard have hpos : 1 ≤ (boundarySet x).card := by omega omega · -- `p.val = L + 1`: position `⟨L, _⟩` is a boundary of `x` NOT in the image of `f`! let b0 : Fin (L + 1) := ⟨L, by omega⟩ have hb0 : b0 ∈ boundarySet x := by simp only [boundarySet, Finset.mem_filter, Finset.mem_univ, true_and, b0] have hsucc : (⟨L, by omega⟩ : Fin (L + 1)).succ = p := Fin.ext (by simp; omega) have hcast : (⟨L, by omega⟩ : Fin (L + 1)).castSucc = ⟨p.val - 1, by omega⟩ := Fin.ext (by simp; omega) rw [hsucc, hcast] exact hp.1 (show 0 < p.val by omega) have hb0_not : b0 ∉ (boundarySet (delPos x p)).image f := by simp only [Finset.mem_image] rintro ⟨j, _, hfj⟩ dsimp only [f, b0] at hfj have hval := congrArg Fin.val hfj split_ifs at hval <;> (dsimp only at hval; omega) have hsub : (boundarySet (delPos x p)).image f ⊆ (boundarySet x).erase b0 := by intro b hb rcases Finset.mem_image.mp hb with ⟨j, hj, rfl⟩ exact Finset.mem_erase.mpr ⟨fun heq => hb0_not (heq ▸ Finset.mem_image_of_mem _ hj), hf_maps j hj⟩ have hcard := Finset.card_le_card hsub rw [Finset.card_image_of_injOn hf_inj, Finset.card_erase_of_mem hb0] at hcard have hpos : 1 ≤ (boundarySet x).card := by omega omega /-- Deleting a sandwiched singleton run reduces `runCount` by at least `2`. -/ theorem runCount_delPos_sandwich {q L : ℕ} (x : Word q (L + 2)) (p : Fin (L + 2)) (hp : p ∈ sandwichPosSet x) : runCount (delPos x p) + 2 ≤ runCount x := by simp only [sandwichPosSet, Finset.mem_filter, Finset.mem_univ, true_and] at hp rcases hp with ⟨h0, h1, heq, hne⟩ unfold runCount suffices h : (boundarySet (delPos x p)).card + 2 ≤ (boundarySet x).card by omega let f : Fin L → Fin (L + 1) := fun j => if hlt : j.val + 1 < p.val then ⟨j.val, by omega⟩ else ⟨j.val + 1, by omega⟩ let bL : Fin (L + 1) := ⟨p.val - 1, by omega⟩ let bR : Fin (L + 1) := ⟨p.val, by omega⟩ have hbL : bL ∈ boundarySet x := by simp only [boundarySet, Finset.mem_filter, Finset.mem_univ, true_and, bL] have hsucc : (⟨p.val - 1, by omega⟩ : Fin (L + 1)).succ = p := Fin.ext (by simp; omega) rw [hsucc, show (⟨p.val - 1, by omega⟩ : Fin (L + 1)).castSucc = ⟨p.val - 1, by omega⟩ from rfl] rw [heq]; exact hne.symm have hbR : bR ∈ boundarySet x := by simp only [boundarySet, Finset.mem_filter, Finset.mem_univ, true_and, bR] exact hne have hbLR : bL ≠ bR := by intro h; have := congrArg Fin.val h; simp [bL, bR] at this; omega have hf_maps : ∀ j ∈ boundarySet (delPos x p), f j ∈ ((boundarySet x).erase bL).erase bR := by intro j hj simp only [boundarySet, Finset.mem_filter, Finset.mem_univ, true_and] at hj have hj_ne : j.val + 1 ≠ p.val := by intro hjp apply hj rw [delPos_apply_lt x p j.castSucc (by simp; omega), delPos_apply_ge x p j.succ (by simp; omega)] have h1' : (⟨j.val, by omega⟩ : Fin (L + 2)) = ⟨p.val - 1, by omega⟩ := Fin.ext (by simp; omega) have h2' : (⟨j.val + 1 + 1, by omega⟩ : Fin (L + 2)) = ⟨p.val + 1, h1⟩ := Fin.ext (by simp; omega) simpa [h1', h2'] using heq rw [Finset.mem_erase, Finset.mem_erase] dsimp only [f] split_ifs with hlt · refine ⟨by intro h; have := congrArg Fin.val h; simp [bR] at this; omega, by intro h; have := congrArg Fin.val h; simp [bL] at this; omega, ?_⟩ simp only [boundarySet, Finset.mem_filter, Finset.mem_univ, true_and] rw [delPos_apply_lt x p j.castSucc (by simp; omega), delPos_apply_lt x p j.succ (by simp; omega)] at hj exact hj · refine ⟨by intro h; have := congrArg Fin.val h; simp [bR] at this; omega, by intro h; have := congrArg Fin.val h; simp [bL] at this; omega, ?_⟩ simp only [boundarySet, Finset.mem_filter, Finset.mem_univ, true_and] rw [delPos_apply_ge x p j.castSucc (by simp; omega), delPos_apply_ge x p j.succ (by simp; omega)] at hj exact hj have hf_inj : Set.InjOn f (boundarySet (delPos x p)) := by intro j₁ _ j₂ _ hfeq dsimp only [f] at hfeq have hval := congrArg Fin.val hfeq split_ifs at hval <;> (dsimp only at hval; exact Fin.ext (by omega)) have hcard : (boundarySet (delPos x p)).card ≤ (((boundarySet x).erase bL).erase bR).card := Finset.card_le_card_of_injOn f hf_maps hf_inj rw [Finset.card_erase_of_mem (Finset.mem_erase.mpr ⟨hbLR.symm, hbR⟩), Finset.card_erase_of_mem hbL] at hcard have h2le : 2 ≤ (boundarySet x).card := by have hsub : ({bL, bR} : Finset (Fin (L + 1))) ⊆ boundarySet x := by intro b hb; simp only [Finset.mem_insert, Finset.mem_singleton] at hb rcases hb with rfl | rfl <;> assumption calc 2 = ({bL, bR} : Finset (Fin (L + 1))).card := by rw [Finset.card_pair hbLR] _ ≤ (boundarySet x).card := Finset.card_le_card hsub omega /-- Delete two coordinates `p < r` from `x : Word q (m + 2)` (dummy value if `¬ p.val < r.val`). -/ def del2 {q m : ℕ} (x : Word q (m + 2)) (p r : Fin (m + 2)) : Word q m := if h : p.val < r.val then delPos (delPos x r) ⟨p.val, by omega⟩ else delPos (delPos x 0) 0 lemma del2_mem_del2Ball {q m : ℕ} (x : Word q (m + 2)) (p r : Fin (m + 2)) (h : p.val < r.val) : del2 x p r ∈ del2Ball x := by unfold del2 del2Ball rw [dif_pos h] refine Finset.mem_biUnion.mpr ⟨delPos x r, ?_, ?_⟩ · exact Finset.mem_image_of_mem _ (Finset.mem_univ _) · exact Finset.mem_image_of_mem _ (Finset.mem_univ _) lemma del2_eq_delPos_first {q m : ℕ} (x : Word q (m + 2)) (p r : Fin (m + 2)) (h : p.val < r.val) : del2 x p r = delPos (delPos x p) ⟨r.val - 1, by omega⟩ := by unfold del2 rw [dif_pos h] funext k by_cases h1 : k.val < p.val · rw [delPos_apply_lt _ _ _ h1, delPos_apply_lt _ _ _ (by simp; omega), delPos_apply_lt _ _ _ (by simp; omega), delPos_apply_lt _ _ _ h1] · by_cases h2 : k.val + 1 < r.val · rw [delPos_apply_ge _ _ _ (by simp; omega), delPos_apply_lt _ _ _ (by simp; omega), delPos_apply_lt _ _ _ (by simp; omega), delPos_apply_ge _ _ _ (by simp; omega)] · rw [delPos_apply_ge _ _ _ (by simp; omega), delPos_apply_ge _ _ _ (by simp; omega), delPos_apply_ge _ _ _ (by simp; omega), delPos_apply_ge _ _ _ (by simp; omega)] lemma del2_apply_lt {q m : ℕ} (x : Word q (m + 2)) (p r : Fin (m + 2)) (h : p.val < r.val) (k : Fin m) (hk : k.val < p.val) : del2 x p r k = x ⟨k.val, by omega⟩ := by unfold del2; rw [dif_pos h] rw [delPos_apply_lt _ _ _ hk, delPos_apply_lt _ _ _ (by simp; omega)] lemma del2_apply_mid {q m : ℕ} (x : Word q (m + 2)) (p r : Fin (m + 2)) (h : p.val < r.val) (k : Fin m) (hk1 : p.val ≤ k.val) (hk2 : k.val + 1 < r.val) : del2 x p r k = x ⟨k.val + 1, by omega⟩ := by unfold del2; rw [dif_pos h] rw [delPos_apply_ge _ _ _ hk1, delPos_apply_lt _ _ _ (by simp; omega)] lemma del2_apply_gt {q m : ℕ} (x : Word q (m + 2)) (p r : Fin (m + 2)) (h : p.val < r.val) (k : Fin m) (hk : r.val ≤ k.val + 1) : del2 x p r k = x ⟨k.val + 2, by omega⟩ := by unfold del2; rw [dif_pos h] rw [delPos_apply_ge _ _ _ (by simp; omega), delPos_apply_ge _ _ _ (by simp; omega)] /-- The canonical set of non-redundant deletion pairs `(p, r)` with `p < r`: either `(p, p + 1)` with `p ∈ longRunStarts x` (intra-run double deletion), or `(p, r)` with `p, r ∈ runStarts x` excluding the redundant pairs `(p, p + 1)` where `p ∈ sandwichPosSet x`. -/ def nonRedundantPairs {q m : ℕ} (x : Word q (m + 3)) : Finset (Fin (m + 3) × Fin (m + 3)) := Finset.univ.filter fun pq => pq.1.val < pq.2.val ∧ ((pq.1 ∈ longRunStarts x ∧ pq.2.val = pq.1.val + 1) ∨ (pq.1 ∈ runStarts x ∧ pq.2 ∈ runStarts x ∧ ¬ (pq.1 ∈ sandwichPosSet x ∧ pq.2.val = pq.1.val + 1))) /-- **Exact Injectivity of `del2` on Non-Redundant Run Pairs**: Distinct non-redundant pairs `(p₁, r₁) ≠ (p₂, r₂)` always produce distinct 2-deletion words `del2 x p₁ r₁ ≠ del2 x p₂ r₂`. -/ theorem del2_injOn_nonRedundant {q m : ℕ} (x : Word q (m + 3)) : Set.InjOn (fun pq : Fin (m + 3) × Fin (m + 3) => del2 x pq.1 pq.2) (nonRedundantPairs x) := by -- Helper assuming `p₁.val ≤ p₂.val` have hsym : ∀ (a b : Fin (m + 3) × Fin (m + 3)), a ∈ nonRedundantPairs x → b ∈ nonRedundantPairs x → a.1.val ≤ b.1.val → del2 x a.1 a.2 = del2 x b.1 b.2 → a = b := by intro ⟨p₁, r₁⟩ ⟨p₂, r₂⟩ ha hb hle heq simp only [nonRedundantPairs, Finset.mem_filter, Finset.mem_univ, true_and] at ha hb rcases ha with ⟨hpr₁, hA⟩ rcases hb with ⟨hpr₂, hB⟩ have hp₂_run : p₂ ∈ runStarts x := by rcases hB with ⟨hL, _⟩ | ⟨hR, _, _⟩ · exact (mem_longRunStarts_iff x p₂).mp hL |>.1 · exact hR by_cases hp_eq : p₁.val = p₂.val · -- Same first index: reduce to second index! have hp12 : p₁ = p₂ := Fin.ext hp_eq subst hp12 by_contra hne have hr_ne : r₁.val ≠ r₂.val := fun h => hne (Prod.ext rfl (Fin.ext h)) -- WLOG prove impossibility for any u < v with both `(p₁, u), (p₁, v)` valid have hsame_p : ∀ (u v : Fin (m + 3)), p₁.val < u.val → u.val < v.val → ((p₁ ∈ longRunStarts x ∧ v.val = p₁.val + 1) ∨ (p₁ ∈ runStarts x ∧ v ∈ runStarts x ∧ ¬ (p₁ ∈ sandwichPosSet x ∧ v.val = p₁.val + 1))) → del2 x p₁ u = del2 x p₁ v → False := by intro u v hpu huv hBv hequv -- Evaluate at `k = v.val - 2` (which satisfies `p₁.val ≤ k`, `u.val ≤ k + 1`, `k + 1 < v.val`) have hk_lt : v.val - 2 < m + 1 := by omega let k : Fin (m + 1) := ⟨v.val - 2, hk_lt⟩ have hval := congrFun hequv k rw [del2_apply_gt x p₁ u hpu k (by simp [k]; omega), del2_apply_mid x p₁ v (by omega) k (by simp [k]; omega) (by simp [k]; omega)] at hval -- So `x ⟨v.val, _⟩ = x ⟨v.val - 1, _⟩`! have hv_eq : x ⟨v.val - 1, by omega⟩ = x v := by have h1 : (⟨k.val + 1, by omega⟩ : Fin (m + 3)) = ⟨v.val - 1, by omega⟩ := Fin.ext (by simp [k]; omega) have h2 : (⟨k.val + 2, by omega⟩ : Fin (m + 3)) = v := Fin.ext (by simp [k]; omega) rw [h1, h2] at hval exact hval.symm rcases hBv with ⟨_, hv1⟩ | ⟨_, hv_run, _⟩ · omega · rcases (mem_runStarts_iff x v).mp hv_run with hv0 | ⟨_, hvne⟩ · omega · exact hvne hv_eq rcases lt_or_gt_of_ne hr_ne with hlt | hgt · exact hsame_p r₁ r₂ hpr₁ hlt hB heq · exact hsame_p r₂ r₁ hpr₂ hgt hA heq.symm · -- Strict inequality `p₁.val < p₂.val`: ALWAYS impossible! exfalso dsimp only at hle heq have hp_lt : p₁.val < p₂.val := by omega have hp₂_lt : p₂.val + 1 < m + 3 := by omega have hp₂_ne : x ⟨p₂.val - 1, by omega⟩ ≠ x p₂ := by rcases (mem_runStarts_iff x p₂).mp hp₂_run with hp0 | ⟨_, hne⟩ · omega · exact hne by_cases hr₁_gt : p₂.val < r₁.val · -- Evaluate at `k = p₂.val - 1`: gives `x p₂ = x (p₂ - 1)`, contradicting `p₂ ∈ runStarts x`! let k : Fin (m + 1) := ⟨p₂.val - 1, by omega⟩ have hval := congrFun heq k rw [del2_apply_mid x p₁ r₁ hpr₁ k (by dsimp only [k]; omega) (by dsimp only [k]; omega), del2_apply_lt x p₂ r₂ hpr₂ k (by dsimp only [k]; omega)] at hval have h1 : (⟨k.val + 1, by omega⟩ : Fin (m + 3)) = p₂ := Fin.ext (by dsimp only [k]; omega) have h2 : (⟨k.val, by omega⟩ : Fin (m + 3)) = ⟨p₂.val - 1, by omega⟩ := Fin.ext (by dsimp only [k]) rw [h1, h2] at hval exact hp₂_ne hval.symm · -- `r₁.val ≤ p₂.val`: evaluate at `k = p₂.val - 1` → `x (p₂ + 1) = x (p₂ - 1)` → `p₂ ∈ sandwichPosSet x`! let k0 : Fin (m + 1) := ⟨p₂.val - 1, by omega⟩ have hval0 := congrFun heq k0 rw [del2_apply_gt x p₁ r₁ hpr₁ k0 (by dsimp only [k0]; omega), del2_apply_lt x p₂ r₂ hpr₂ k0 (by dsimp only [k0]; omega)] at hval0 have hp₂_sand : p₂ ∈ sandwichPosSet x := by simp only [sandwichPosSet, Finset.mem_filter, Finset.mem_univ, true_and] refine ⟨by omega, hp₂_lt, ?_, ?_⟩ · have h1 : (⟨k0.val, by omega⟩ : Fin (m + 3)) = ⟨p₂.val - 1, by omega⟩ := Fin.ext (by dsimp only [k0]) have h2 : (⟨k0.val + 2, by dsimp only [k0]; omega⟩ : Fin (m + 3)) = ⟨p₂.val + 1, hp₂_lt⟩ := Fin.ext (by dsimp only [k0]; omega) rw [h1, h2] at hval0 exact hval0.symm · have h1 : (⟨k0.val, by omega⟩ : Fin (m + 3)) = ⟨p₂.val - 1, by omega⟩ := Fin.ext (by dsimp only [k0]) have h2 : (⟨k0.val + 2, by dsimp only [k0]; omega⟩ : Fin (m + 3)) = ⟨p₂.val + 1, hp₂_lt⟩ := Fin.ext (by dsimp only [k0]; omega) rw [h1, h2] at hval0 rw [hval0] exact hp₂_ne.symm -- Since `p₂ ∈ sandwichPosSet x`, `(p₂, r₂)` cannot be `sameRun` and cannot have `r₂ = p₂ + 1`! have hr₂_run : r₂ ∈ runStarts x ∧ p₂.val + 2 ≤ r₂.val := by rcases hB with ⟨hL, _⟩ | ⟨_, hr2r, hnot⟩ · exfalso exact (Finset.mem_sdiff.mp hL).2 (sandwichPosSet_subset_singletonPosSet x hp₂_sand) · refine ⟨hr2r, ?_⟩ by_contra hlt exact hnot ⟨hp₂_sand, by omega⟩ -- Evaluate at `k1 = r₂.val - 2`: gives `x r₂ = x (r₂ - 1)`, contradicting `r₂ ∈ runStarts x`! let k1 : Fin (m + 1) := ⟨r₂.val - 2, by omega⟩ have hval1 := congrFun heq k1 rw [del2_apply_gt x p₁ r₁ hpr₁ k1 (by simp [k1]; omega), del2_apply_mid x p₂ r₂ hpr₂ k1 (by simp [k1]; omega) (by simp [k1]; omega)] at hval1 have h1 : (⟨k1.val + 2, by omega⟩ : Fin (m + 3)) = r₂ := Fin.ext (by simp [k1]; omega) have h2 : (⟨k1.val + 1, by omega⟩ : Fin (m + 3)) = ⟨r₂.val - 1, by omega⟩ := Fin.ext (by simp [k1]; omega) rw [h1, h2] at hval1 rcases (mem_runStarts_iff x r₂).mp hr₂_run.1 with hr0 | ⟨_, hrne⟩ · omega · exact hrne hval1.symm intro a ha b hb heq simp only [Finset.mem_coe] at ha hb rcases le_total a.1.val b.1.val with hle | hle · exact hsym a b ha hb hle heq · exact (hsym b a hb ha hle heq.symm).symm /-- Locality of singleton runs under deleting a position at distance `≥ 2` to the right. -/ lemma singletonPosSet_delPos_of_add_two_le {q L : ℕ} (x : Word q (L + 2)) (p r : Fin (L + 2)) (hp : p ∈ singletonPosSet x) (hpr : p.val + 2 ≤ r.val) : (⟨p.val, by omega⟩ : Fin (L + 1)) ∈ singletonPosSet (delPos x r) := by simp only [singletonPosSet, Finset.mem_filter, Finset.mem_univ, true_and] at hp ⊢ refine ⟨fun h0 => ?_, fun h1 => ?_⟩ · rw [delPos_apply_lt x r _ (by simp; omega), delPos_apply_lt x r _ (by simp; omega)] exact hp.1 h0 · rw [delPos_apply_lt x r _ (by simp; omega), delPos_apply_lt x r _ (by simp; omega)] exact hp.2 (by omega) /-- Locality of singleton runs under deleting a position at distance `≥ 2` to the left. -/ lemma singletonPosSet_delPos_of_le_sub_two {q L : ℕ} (x : Word q (L + 2)) (p r : Fin (L + 2)) (hr : r ∈ singletonPosSet x) (hpr : p.val + 2 ≤ r.val) : (⟨r.val - 1, by omega⟩ : Fin (L + 1)) ∈ singletonPosSet (delPos x p) := by simp only [singletonPosSet, Finset.mem_filter, Finset.mem_univ, true_and] at hr ⊢ refine ⟨fun h0 => ?_, fun h1 => ?_⟩ · rw [delPos_apply_ge x p _ (by simp; omega), delPos_apply_ge x p _ (by simp; omega)] have h1' : (⟨r.val - 1 - 1 + 1, by omega⟩ : Fin (L + 2)) = ⟨r.val - 1, by omega⟩ := Fin.ext (by simp; omega) have h2' : (⟨r.val - 1 + 1, by omega⟩ : Fin (L + 2)) = r := Fin.ext (by simp; omega) rw [h1', h2'] exact hr.1 (by omega) · rw [delPos_apply_ge x p _ (by simp; omega), delPos_apply_ge x p _ (by simp; omega)] have h1' : (⟨r.val - 1 + 1, by omega⟩ : Fin (L + 2)) = r := Fin.ext (by simp; omega) have h2' : (⟨r.val - 1 + 1 + 1, by omega⟩ : Fin (L + 2)) = ⟨r.val + 1, by omega⟩ := Fin.ext (by simp; omega) rw [h1', h2'] exact hr.2 (by omega) /-- Locality of sandwiched singleton runs under deleting a position at distance `≥ 2` to the right. -/ lemma sandwichPosSet_delPos_of_add_two_le {q L : ℕ} (x : Word q (L + 2)) (p r : Fin (L + 2)) (hp : p ∈ sandwichPosSet x) (hpr : p.val + 2 ≤ r.val) : (⟨p.val, by omega⟩ : Fin (L + 1)) ∈ sandwichPosSet (delPos x r) := by simp only [sandwichPosSet, Finset.mem_filter, Finset.mem_univ, true_and] at hp ⊢ rcases hp with ⟨h0, h1, heq, hne⟩ refine ⟨h0, by omega, ?_, ?_⟩ · rw [delPos_apply_lt x r _ (by simp; omega), delPos_apply_lt x r _ (by simp; omega)] exact heq · rw [delPos_apply_lt x r _ (by simp; omega), delPos_apply_lt x r _ (by simp; omega)] exact hne /-- Deleting an adjacent right unsandwiched singleton `p + 1 ∈ N₁` leaves `p ∈ N₁` a singleton. -/ lemma singletonPosSet_delPos_adj_nonSand {q L : ℕ} (x : Word q (L + 2)) (p r : Fin (L + 2)) (hp : p ∈ nonSandSingletonStarts x) (hr : r ∈ nonSandSingletonStarts x) (hadj : r.val = p.val + 1) : (⟨p.val, by omega⟩ : Fin (L + 1)) ∈ singletonPosSet (delPos x r) := by have hp_s : p ∈ singletonPosSet x := (Finset.mem_sdiff.mp hp).1 have hr_s : r ∈ singletonPosSet x := (Finset.mem_sdiff.mp hr).1 have hr_ns : r ∉ sandwichPosSet x := (Finset.mem_sdiff.mp hr).2 simp only [singletonPosSet, Finset.mem_filter, Finset.mem_univ, true_and] at hp_s hr_s ⊢ refine ⟨fun h0 => ?_, fun h1 => ?_⟩ · rw [delPos_apply_lt x r _ (by simp; omega), delPos_apply_lt x r _ (by simp; omega)] exact hp_s.1 h0 · rw [delPos_apply_lt x r _ (by simp; omega), delPos_apply_ge x r _ (by simp; omega)] intro heq apply hr_ns simp only [sandwichPosSet, Finset.mem_filter, Finset.mem_univ, true_and] refine ⟨by omega, by omega, ?_, ?_⟩ · have h1' : (⟨r.val - 1, by omega⟩ : Fin (L + 2)) = p := Fin.ext (by simp; omega) have h2' : (⟨r.val + 1, by omega⟩ : Fin (L + 2)) = ⟨p.val + 1 + 1, by omega⟩ := Fin.ext (by simp; omega) rw [h1', h2'] exact heq · exact hr_s.2 (by omega) /-- Double-deletion run-count drop for ALL 7 buckets. -/ lemma runCount_del2_le {q m : ℕ} (x : Word q (m + 3)) (p r : Fin (m + 3)) (h : p.val < r.val) : runCount (del2 x p r) ≤ runCount x := by unfold del2; rw [dif_pos h] exact (runCount_delPos_le _ _).trans (runCount_delPos_le _ _) lemma runCount_del2_le_sub_one_left {q m : ℕ} (x : Word q (m + 3)) (p r : Fin (m + 3)) (h : p.val < r.val) (hp : p ∈ singletonPosSet x) (hr2 : 2 ≤ runCount x) : runCount (del2 x p r) + 1 ≤ runCount x := by rw [del2_eq_delPos_first x p r h] have h1 := runCount_delPos_le (delPos x p) ⟨r.val - 1, by omega⟩ have h2 := runCount_delPos_singleton x p hp hr2 omega lemma runCount_del2_le_sub_one_right {q m : ℕ} (x : Word q (m + 3)) (p r : Fin (m + 3)) (h : p.val < r.val) (hr : r ∈ singletonPosSet x) (hr2 : 2 ≤ runCount x) : runCount (del2 x p r) + 1 ≤ runCount x := by unfold del2; rw [dif_pos h] have h1 := runCount_delPos_le (delPos x r) ⟨p.val, by omega⟩ have h2 := runCount_delPos_singleton x r hr hr2 omega lemma runCount_del2_le_sub_two_sand_left {q m : ℕ} (x : Word q (m + 3)) (p r : Fin (m + 3)) (h : p.val < r.val) (hp : p ∈ sandwichPosSet x) : runCount (del2 x p r) + 2 ≤ runCount x := by rw [del2_eq_delPos_first x p r h] have h1 := runCount_delPos_le (delPos x p) ⟨r.val - 1, by omega⟩ have h2 := runCount_delPos_sandwich x p hp omega lemma runCount_del2_le_sub_two_sand_right {q m : ℕ} (x : Word q (m + 3)) (p r : Fin (m + 3)) (h : p.val < r.val) (hr : r ∈ sandwichPosSet x) : runCount (del2 x p r) + 2 ≤ runCount x := by unfold del2; rw [dif_pos h] have h1 := runCount_delPos_le (delPos x r) ⟨p.val, by omega⟩ have h2 := runCount_delPos_sandwich x r hr omega lemma runCount_del2_le_sub_two_NN {q m : ℕ} (x : Word q (m + 3)) (p r : Fin (m + 3)) (h : p.val < r.val) (hp : p ∈ nonSandSingletonStarts x) (hr : r ∈ nonSandSingletonStarts x) (hr3 : 3 ≤ runCount x) : runCount (del2 x p r) + 2 ≤ runCount x := by unfold del2; rw [dif_pos h] have h2 := runCount_delPos_singleton x r (Finset.mem_sdiff.mp hr).1 (by omega) have hp_z : (⟨p.val, by omega⟩ : Fin (m + 2)) ∈ singletonPosSet (delPos x r) := by by_cases hadj : r.val = p.val + 1 · exact singletonPosSet_delPos_adj_nonSand x p r hp hr hadj · exact singletonPosSet_delPos_of_add_two_le x p r (Finset.mem_sdiff.mp hp).1 (by omega) by_cases hz2 : 2 ≤ runCount (delPos x r) · have h1 := runCount_delPos_singleton (delPos x r) ⟨p.val, by omega⟩ hp_z hz2 omega · have h1 := runCount_delPos_le (delPos x r) ⟨p.val, by omega⟩ omega lemma runCount_del2_le_sub_three_NS {q m : ℕ} (x : Word q (m + 3)) (p r : Fin (m + 3)) (hpr : p.val + 2 ≤ r.val) (hp : p ∈ singletonPosSet x) (hr : r ∈ sandwichPosSet x) (hr4 : 4 ≤ runCount x) : runCount (del2 x p r) + 3 ≤ runCount x := by unfold del2; rw [dif_pos (by omega : p.val < r.val)] have h2 := runCount_delPos_sandwich x r hr have hp_z := singletonPosSet_delPos_of_add_two_le x p r hp hpr by_cases hz2 : 2 ≤ runCount (delPos x r) · have h1 := runCount_delPos_singleton (delPos x r) ⟨p.val, by omega⟩ hp_z hz2 omega · have h1 := runCount_delPos_le (delPos x r) ⟨p.val, by omega⟩ omega lemma runCount_del2_le_sub_three_SN {q m : ℕ} (x : Word q (m + 3)) (p r : Fin (m + 3)) (hpr : p.val + 2 ≤ r.val) (hp : p ∈ sandwichPosSet x) (hr : r ∈ singletonPosSet x) (hr4 : 4 ≤ runCount x) : runCount (del2 x p r) + 3 ≤ runCount x := by rw [del2_eq_delPos_first x p r (by omega)] have h2 := runCount_delPos_sandwich x p hp have hr_z := singletonPosSet_delPos_of_le_sub_two x p r hr hpr by_cases hz2 : 2 ≤ runCount (delPos x p) · have h1 := runCount_delPos_singleton (delPos x p) ⟨r.val - 1, by omega⟩ hr_z hz2 omega · have h1 := runCount_delPos_le (delPos x p) ⟨r.val - 1, by omega⟩ omega lemma runCount_del2_le_sub_four_SS {q m : ℕ} (x : Word q (m + 3)) (p r : Fin (m + 3)) (hpr : p.val + 2 ≤ r.val) (hp : p ∈ sandwichPosSet x) (hr : r ∈ sandwichPosSet x) : runCount (del2 x p r) + 4 ≤ runCount x := by unfold del2; rw [dif_pos (by omega : p.val < r.val)] have h2 := runCount_delPos_sandwich x r hr have hp_z := sandwichPosSet_delPos_of_add_two_le x p r hp hpr have h1 := runCount_delPos_sandwich (delPos x r) ⟨p.val, by omega⟩ hp_z omega /-- The 7 pairwise-disjoint buckets partitioning `nonRedundantPairs x`. -/ def bucketLL {q m : ℕ} (x : Word q (m + 3)) : Finset (Fin (m + 3) × Fin (m + 3)) := (nonRedundantPairs x).filter fun pq => (pq.1 ∈ longRunStarts x ∧ pq.2.val = pq.1.val + 1) ∨ (pq.1 ∈ longRunStarts x ∧ pq.2 ∈ longRunStarts x) def bucketLN {q m : ℕ} (x : Word q (m + 3)) : Finset (Fin (m + 3) × Fin (m + 3)) := (nonRedundantPairs x).filter fun pq => (pq.1 ∈ longRunStarts x ∧ pq.2 ∈ nonSandSingletonStarts x) ∨ (pq.1 ∈ nonSandSingletonStarts x ∧ pq.2 ∈ longRunStarts x) def bucketLS {q m : ℕ} (x : Word q (m + 3)) : Finset (Fin (m + 3) × Fin (m + 3)) := (nonRedundantPairs x).filter fun pq => (pq.1 ∈ longRunStarts x ∧ pq.2 ∈ sandwichPosSet x) ∨ (pq.1 ∈ sandwichPosSet x ∧ pq.2 ∈ longRunStarts x) def bucketNN {q m : ℕ} (x : Word q (m + 3)) : Finset (Fin (m + 3) × Fin (m + 3)) := (nonRedundantPairs x).filter fun pq => pq.1 ∈ nonSandSingletonStarts x ∧ pq.2 ∈ nonSandSingletonStarts x def bucketNSAdj {q m : ℕ} (x : Word q (m + 3)) : Finset (Fin (m + 3) × Fin (m + 3)) := (nonRedundantPairs x).filter fun pq => pq.1 ∈ nonSandSingletonStarts x ∧ pq.2 ∈ sandwichPosSet x ∧ pq.2.val = pq.1.val + 1 def bucketNSNon {q m : ℕ} (x : Word q (m + 3)) : Finset (Fin (m + 3) × Fin (m + 3)) := (nonRedundantPairs x).filter fun pq => (pq.1 ∈ nonSandSingletonStarts x ∧ pq.2 ∈ sandwichPosSet x ∧ pq.2.val ≠ pq.1.val + 1) ∨ (pq.1 ∈ sandwichPosSet x ∧ pq.2 ∈ nonSandSingletonStarts x) def bucketSS {q m : ℕ} (x : Word q (m + 3)) : Finset (Fin (m + 3) × Fin (m + 3)) := (nonRedundantPairs x).filter fun pq => pq.1 ∈ sandwichPosSet x ∧ pq.2 ∈ sandwichPosSet x /-- Redundant right-neighbor sets `eL`, `eN`, `eS` of `sandwichPosSet x`, and left-adjacent unsandwiched-singleton set `fN`. -/ def rightInLong {q m : ℕ} (x : Word q (m + 3)) : Finset (Fin (m + 3)) := (sandwichPosSet x).filter fun p => ∃ h : p.val + 1 < m + 3, (⟨p.val + 1, h⟩ : Fin (m + 3)) ∈ longRunStarts x def rightInNonSand {q m : ℕ} (x : Word q (m + 3)) : Finset (Fin (m + 3)) := (sandwichPosSet x).filter fun p => ∃ h : p.val + 1 < m + 3, (⟨p.val + 1, h⟩ : Fin (m + 3)) ∈ nonSandSingletonStarts x def rightInSand {q m : ℕ} (x : Word q (m + 3)) : Finset (Fin (m + 3)) := (sandwichPosSet x).filter fun p => ∃ h : p.val + 1 < m + 3, (⟨p.val + 1, h⟩ : Fin (m + 3)) ∈ sandwichPosSet x def leftInNonSand {q m : ℕ} (x : Word q (m + 3)) : Finset (Fin (m + 3)) := (sandwichPosSet x).filter fun p => ∃ h : 0 < p.val, (⟨p.val - 1, by omega⟩ : Fin (m + 3)) ∈ nonSandSingletonStarts x /-- Helper: for every `p ∈ sandwichPosSet x`, `p + 1` is in bounds and lies in `runStarts x`. -/ lemma succ_mem_runStarts_of_sandwich {q L : ℕ} (x : Word q (L + 1)) (p : Fin (L + 1)) (hp : p ∈ sandwichPosSet x) : ∃ h : p.val + 1 < L + 1, (⟨p.val + 1, h⟩ : Fin (L + 1)) ∈ runStarts x := by simp only [sandwichPosSet, Finset.mem_filter, Finset.mem_univ, true_and] at hp rcases hp with ⟨_, h1, _, hne⟩ refine ⟨h1, ?_⟩ rw [mem_runStarts_iff] right refine ⟨by simp, ?_⟩ have heq : (⟨p.val + 1 - 1, by omega⟩ : Fin (L + 1)) = p := Fin.ext (by simp) simpa [heq] using hne lemma card_right_partition {q m : ℕ} (x : Word q (m + 3)) : (rightInLong x).card + (rightInNonSand x).card + (rightInSand x).card = (sandwichPosSet x).card := by have hd1 : Disjoint (rightInLong x) (rightInNonSand x) := by rw [Finset.disjoint_left] intro p hp1 hp2 simp only [rightInLong, rightInNonSand, Finset.mem_filter] at hp1 hp2 rcases hp1.2 with ⟨h1, hL⟩ rcases hp2.2 with ⟨h2, hN⟩ exact (Finset.mem_sdiff.mp hL).2 (Finset.mem_sdiff.mp hN).1 have hd2 : Disjoint (rightInLong x ∪ rightInNonSand x) (rightInSand x) := by rw [Finset.disjoint_left] intro p hp1 hp2 simp only [Finset.mem_union, rightInLong, rightInNonSand, rightInSand, Finset.mem_filter] at hp1 hp2 rcases hp2.2 with ⟨_, hS⟩ rcases hp1 with ⟨_, _, hL⟩ | ⟨_, _, hN⟩ · exact (Finset.mem_sdiff.mp hL).2 (sandwichPosSet_subset_singletonPosSet x hS) · exact (Finset.mem_sdiff.mp hN).2 hS have hunion : (rightInLong x ∪ rightInNonSand x) ∪ rightInSand x = sandwichPosSet x := by ext p simp only [Finset.mem_union, rightInLong, rightInNonSand, rightInSand, Finset.mem_filter] constructor · rintro ((⟨hp, _⟩ | ⟨hp, _⟩) | ⟨hp, _⟩) <;> exact hp · intro hp rcases succ_mem_runStarts_of_sandwich x p hp with ⟨h1, hrun⟩ by_cases hs : (⟨p.val + 1, h1⟩ : Fin (m + 3)) ∈ sandwichPosSet x · right; exact ⟨hp, h1, hs⟩ · by_cases hsing : (⟨p.val + 1, h1⟩ : Fin (m + 3)) ∈ singletonPosSet x · left; right; exact ⟨hp, h1, Finset.mem_sdiff.mpr ⟨hsing, hs⟩⟩ · left; left; exact ⟨hp, h1, Finset.mem_sdiff.mpr ⟨hrun, hsing⟩⟩ rw [← hunion, Finset.card_union_of_disjoint hd2, Finset.card_union_of_disjoint hd1] lemma card_rightInSand_lt {q m : ℕ} (x : Word q (m + 3)) (hS : 1 ≤ (sandwichPosSet x).card) : (rightInSand x).card < (sandwichPosSet x).card := by have hne : (sandwichPosSet x).Nonempty := Finset.card_pos.mp hS let pmax := (sandwichPosSet x).max' hne have hpmax : pmax ∈ sandwichPosSet x := Finset.max'_mem _ hne have hnot : pmax ∉ rightInSand x := by simp only [rightInSand, Finset.mem_filter] rintro ⟨_, h1, hmem⟩ have hle := Finset.le_max' (sandwichPosSet x) _ hmem have hlt : pmax < ⟨pmax.val + 1, h1⟩ := by change pmax.val < pmax.val + 1; omega exact not_lt_of_ge hle hlt exact Finset.card_lt_card (Finset.ssubset_iff_subset_ne.mpr ⟨Finset.filter_subset _ _, fun heq => hnot (heq ▸ hpmax)⟩) lemma card_leftInNonSand_le_nonSand {q m : ℕ} (x : Word q (m + 3)) : (leftInNonSand x).card ≤ (nonSandSingletonStarts x).card := by let f : Fin (m + 3) → Fin (m + 3) := fun p => ⟨p.val - 1, by omega⟩ refine Finset.card_le_card_of_injOn f ?_ ?_ · intro p hp simp only [Finset.mem_coe, leftInNonSand, Finset.mem_filter] at hp ⊢ rcases hp.2 with ⟨_, hmem⟩ exact hmem · intro p₁ hp₁ p₂ hp₂ hfeq simp only [Finset.mem_coe, leftInNonSand, Finset.mem_filter] at hp₁ hp₂ rcases hp₁.2 with ⟨h1, _⟩ rcases hp₂.2 with ⟨h2, _⟩ have hval := congrArg Fin.val hfeq dsimp only [f] at hval exact Fin.ext (by omega) lemma card_leftInNonSand_le_sub_rightInSand {q m : ℕ} (x : Word q (m + 3)) : (leftInNonSand x).card + (rightInSand x).card ≤ (sandwichPosSet x).card := by -- Map `p ∈ rightInSand x` to `p + 1 ∈ sandwichPosSet x \ leftInNonSand x`! let f : Fin (m + 3) → Fin (m + 3) := fun p => if h : p.val + 1 < m + 3 then ⟨p.val + 1, h⟩ else p have hcard : (rightInSand x).card ≤ (sandwichPosSet x \ leftInNonSand x).card := by refine Finset.card_le_card_of_injOn f ?_ ?_ · intro p hp simp only [Finset.mem_coe, rightInSand, Finset.mem_filter] at hp ⊢ rcases hp.2 with ⟨h1, hmem⟩ rw [Finset.mem_sdiff] refine ⟨by dsimp only [f]; rw [dif_pos h1]; exact hmem, ?_⟩ simp only [f, dif_pos h1, leftInNonSand, Finset.mem_filter] rintro ⟨_, _, hN⟩ have heq : (⟨p.val + 1 - 1, by omega⟩ : Fin (m + 3)) = p := Fin.ext (by simp) rw [heq] at hN exact (Finset.mem_sdiff.mp hN).2 hp.1 · intro p₁ hp₁ p₂ hp₂ hfeq simp only [Finset.mem_coe, rightInSand, Finset.mem_filter] at hp₁ hp₂ rcases hp₁.2 with ⟨h1, _⟩ rcases hp₂.2 with ⟨h2, _⟩ simp only [f, dif_pos h1, dif_pos h2] at hfeq have hval := congrArg Fin.val hfeq dsimp only at hval exact Fin.ext (by omega) have hsdiff := Finset.card_sdiff_add_card_eq_card (show leftInNonSand x ⊆ sandwichPosSet x from Finset.filter_subset _ _) omega /-- Generic cardinality of strictly ordered pairs from a `Finset (Fin N)`. -/ private lemma card_ordered_pairs_same {N : ℕ} (A : Finset (Fin N)) : 2 * ((Finset.univ : Finset (Fin N × Fin N)).filter (fun pq => pq.1.val < pq.2.val ∧ pq.1 ∈ A ∧ pq.2 ∈ A)).card = A.card * (A.card - 1) := by let S := (Finset.univ : Finset (Fin N × Fin N)).filter (fun pq => pq.1.val < pq.2.val ∧ pq.1 ∈ A ∧ pq.2 ∈ A) let T := (Finset.univ : Finset (Fin N × Fin N)).filter (fun pq => pq.2.val < pq.1.val ∧ pq.1 ∈ A ∧ pq.2 ∈ A) have hST : S.card = T.card := by refine Finset.card_bij (fun pq _ => (pq.2, pq.1)) ?_ ?_ ?_ · intro pq hpq simp only [S, T, Finset.mem_filter, Finset.mem_univ, true_and] at hpq ⊢ exact ⟨hpq.1, hpq.2.2, hpq.2.1⟩ · intro a _ b _ h; exact Prod.ext (congrArg Prod.snd h) (congrArg Prod.fst h) · intro pq hpq refine ⟨(pq.2, pq.1), ?_, rfl⟩ simp only [S, T, Finset.mem_filter, Finset.mem_univ, true_and] at hpq ⊢ exact ⟨hpq.1, hpq.2.2, hpq.2.1⟩ have hdisj : Disjoint S T := by rw [Finset.disjoint_left] intro pq h1 h2 simp only [S, T, Finset.mem_filter, Finset.mem_univ, true_and] at h1 h2 omega have hunion : S ∪ T = A.offDiag := by ext ⟨a, b⟩ simp only [S, T, Finset.mem_union, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_offDiag] constructor · rintro (⟨h, ha, hb⟩ | ⟨h, ha, hb⟩) · exact ⟨ha, hb, fun heq => by subst heq; omega⟩ · exact ⟨ha, hb, fun heq => by subst heq; omega⟩ · rintro ⟨ha, hb, hne⟩ have hne' : a.val ≠ b.val := fun h => hne (Fin.ext h) rcases lt_or_gt_of_ne hne' with h | h · left; exact ⟨h, ha, hb⟩ · right; exact ⟨h, ha, hb⟩ have hcard : S.card + T.card = A.offDiag.card := by rw [← hunion, Finset.card_union_of_disjoint hdisj] rw [Finset.offDiag_card] at hcard have hmul : A.card * (A.card - 1) = A.card * A.card - A.card := by cases hA : A.card with | zero => simp | succ k => simp [Nat.mul_succ] change 2 * S.card = A.card * (A.card - 1) omega /-- Generic cardinality of strictly ordered pairs between two disjoint `Finset (Fin N)`s. -/ private lemma card_ordered_pairs_disj {N : ℕ} (A B : Finset (Fin N)) (hAB : Disjoint A B) : ((Finset.univ : Finset (Fin N × Fin N)).filter (fun pq => pq.1.val < pq.2.val ∧ ((pq.1 ∈ A ∧ pq.2 ∈ B) ∨ (pq.1 ∈ B ∧ pq.2 ∈ A)))).card = A.card * B.card := by let S := (Finset.univ : Finset (Fin N × Fin N)).filter (fun pq => pq.1.val < pq.2.val ∧ ((pq.1 ∈ A ∧ pq.2 ∈ B) ∨ (pq.1 ∈ B ∧ pq.2 ∈ A))) rw [← Finset.card_product] refine Finset.card_bij (s := S) (t := A ×ˢ B) (fun (pq : Fin N × Fin N) _ => if pq.1 ∈ A then (pq.1, pq.2) else (pq.2, pq.1)) ?_ ?_ ?_ · intro pq hpq simp only [S, Finset.mem_filter, Finset.mem_univ, true_and] at hpq split_ifs with h1 · rcases hpq.2 with ⟨ha, hb⟩ | ⟨hb, _⟩ · exact Finset.mem_product.mpr ⟨ha, hb⟩ · exact False.elim (Finset.disjoint_left.mp hAB h1 hb) · rcases hpq.2 with ⟨ha, _⟩ | ⟨hb, ha⟩ · exact False.elim (h1 ha) · exact Finset.mem_product.mpr ⟨ha, hb⟩ · intro pq₁ hpq₁ pq₂ hpq₂ heq simp only [S, Finset.mem_filter, Finset.mem_univ, true_and] at hpq₁ hpq₂ split_ifs at heq with h1 h2 h2 · exact heq · have ha : pq₁.1 = pq₂.2 := congrArg Prod.fst heq have hb : pq₁.2 = pq₂.1 := congrArg Prod.snd heq have := hpq₁.1; have := hpq₂.1; rw [ha, hb] at *; omega · have ha : pq₁.2 = pq₂.1 := congrArg Prod.fst heq have hb : pq₁.1 = pq₂.2 := congrArg Prod.snd heq have := hpq₁.1; have := hpq₂.1; rw [ha, hb] at *; omega · exact Prod.ext (congrArg Prod.snd heq) (congrArg Prod.fst heq) · intro ab hab rcases Finset.mem_product.mp hab with ⟨ha, hb⟩ have hne : ab.1.val ≠ ab.2.val := fun h => Finset.disjoint_left.mp hAB ha (Fin.ext h ▸ hb) rcases lt_or_gt_of_ne hne with hlt | hgt · refine ⟨(ab.1, ab.2), ?_, by simp [ha]⟩ simp only [S, Finset.mem_filter, Finset.mem_univ, true_and] exact ⟨hlt, Or.inl ⟨ha, hb⟩⟩ · have hnb : ab.2 ∉ A := fun h => Finset.disjoint_left.mp hAB h hb refine ⟨(ab.2, ab.1), ?_, by simp [hnb]⟩ simp only [S, Finset.mem_filter, Finset.mem_univ, true_and] exact ⟨hgt, Or.inr ⟨hb, ha⟩⟩ /-- Exact cardinality formulas for all 7 buckets. -/ lemma card_bucketLL {q m : ℕ} (x : Word q (m + 3)) : 2 * (bucketLL x).card = (longRunStarts x).card * ((longRunStarts x).card + 1) := by let S1 := (Finset.univ : Finset (Fin (m + 3) × Fin (m + 3))).filter (fun pq => pq.1 ∈ longRunStarts x ∧ pq.2.val = pq.1.val + 1) let S2 := (Finset.univ : Finset (Fin (m + 3) × Fin (m + 3))).filter (fun pq => pq.1.val < pq.2.val ∧ pq.1 ∈ longRunStarts x ∧ pq.2 ∈ longRunStarts x) have hunion : bucketLL x = S1 ∪ S2 := by ext pq simp only [bucketLL, nonRedundantPairs, S1, S2, Finset.mem_union, Finset.mem_filter, Finset.mem_univ, true_and] constructor · rintro ⟨⟨hlt, _⟩, (⟨hL, h2⟩ | ⟨hL1, hL2⟩)⟩ · left; exact ⟨hL, h2⟩ · right; exact ⟨hlt, hL1, hL2⟩ · rintro (⟨hL, h2⟩ | ⟨hlt, hL1, hL2⟩) · refine ⟨⟨by omega, Or.inl ⟨hL, h2⟩⟩, Or.inl ⟨hL, h2⟩⟩ · refine ⟨⟨hlt, Or.inr ⟨(Finset.mem_sdiff.mp hL1).1, (Finset.mem_sdiff.mp hL2).1, ?_⟩⟩, Or.inr ⟨hL1, hL2⟩⟩ rintro ⟨hs, _⟩ exact (Finset.mem_sdiff.mp hL1).2 (sandwichPosSet_subset_singletonPosSet x hs) have hdisj : Disjoint S1 S2 := by rw [Finset.disjoint_left] intro pq h1 h2 simp only [S1, S2, Finset.mem_filter, Finset.mem_univ, true_and] at h1 h2 rcases (mem_longRunStarts_iff x pq.1).mp h1.1 with ⟨_, hlt, heq⟩ have hp2_eq : pq.2 = ⟨pq.1.val + 1, hlt⟩ := Fin.ext h1.2 have hp2_run : pq.2 ∈ runStarts x := (Finset.mem_sdiff.mp h2.2.2).1 rcases (mem_runStarts_iff x pq.2).mp hp2_run with h0 | ⟨_, hne⟩ · omega · apply hne have hpred : (⟨pq.2.val - 1, by omega⟩ : Fin (m + 3)) = pq.1 := Fin.ext (by simp; omega) rw [hpred, hp2_eq]; exact heq have hS1 : S1.card = (longRunStarts x).card := by refine Finset.card_bij (fun pq _ => pq.1) ?_ ?_ ?_ · intro pq hpq simp only [S1, Finset.mem_filter, Finset.mem_univ, true_and] at hpq exact hpq.1 · intro a ha b hb h simp only [S1, Finset.mem_filter, Finset.mem_univ, true_and] at ha hb exact Prod.ext h (Fin.ext (by rw [ha.2, hb.2, h])) · intro p hp rcases (mem_longRunStarts_iff x p).mp hp with ⟨_, hlt, _⟩ refine ⟨(p, ⟨p.val + 1, hlt⟩), ?_, rfl⟩ simp only [S1, Finset.mem_filter, Finset.mem_univ, true_and] exact ⟨hp, trivial⟩ have hS2 := card_ordered_pairs_same (longRunStarts x) rw [hunion, Finset.card_union_of_disjoint hdisj, Nat.mul_add, hS1] have : 2 * S2.card = (longRunStarts x).card * ((longRunStarts x).card - 1) := hS2 cases hL : (longRunStarts x).card with | zero => simp [hL] at this ⊢; omega | succ k => simp [hL] at this ⊢; ring_nf at this ⊢; omega lemma card_bucketLN {q m : ℕ} (x : Word q (m + 3)) : (bucketLN x).card = (longRunStarts x).card * (nonSandSingletonStarts x).card := by have hdisj : Disjoint (longRunStarts x) (nonSandSingletonStarts x) := by rw [Finset.disjoint_left] intro p h1 h2 exact (Finset.mem_sdiff.mp h1).2 (Finset.mem_sdiff.mp h2).1 have heq : bucketLN x = (Finset.univ : Finset (Fin (m + 3) × Fin (m + 3))).filter (fun pq => pq.1.val < pq.2.val ∧ ((pq.1 ∈ longRunStarts x ∧ pq.2 ∈ nonSandSingletonStarts x) ∨ (pq.1 ∈ nonSandSingletonStarts x ∧ pq.2 ∈ longRunStarts x))) := by ext pq simp only [bucketLN, nonRedundantPairs, Finset.mem_filter, Finset.mem_univ, true_and] constructor · rintro ⟨⟨hlt, _⟩, h⟩; exact ⟨hlt, h⟩ · rintro ⟨hlt, h⟩ refine ⟨⟨hlt, Or.inr ⟨?_, ?_, ?_⟩⟩, h⟩ · rcases h with ⟨hL, _⟩ | ⟨hN, _⟩ · exact (Finset.mem_sdiff.mp hL).1 · exact singletonPosSet_subset_runStarts x (Finset.mem_sdiff.mp hN).1 · rcases h with ⟨_, hN⟩ | ⟨_, hL⟩ · exact singletonPosSet_subset_runStarts x (Finset.mem_sdiff.mp hN).1 · exact (Finset.mem_sdiff.mp hL).1 · rintro ⟨hs, _⟩ rcases h with ⟨hL, _⟩ | ⟨hN, _⟩ · exact (Finset.mem_sdiff.mp hL).2 (sandwichPosSet_subset_singletonPosSet x hs) · exact (Finset.mem_sdiff.mp hN).2 hs rw [heq] exact card_ordered_pairs_disj _ _ hdisj lemma card_bucketNN {q m : ℕ} (x : Word q (m + 3)) : 2 * (bucketNN x).card = (nonSandSingletonStarts x).card * ((nonSandSingletonStarts x).card - 1) := by have heq : bucketNN x = (Finset.univ : Finset (Fin (m + 3) × Fin (m + 3))).filter (fun pq => pq.1.val < pq.2.val ∧ pq.1 ∈ nonSandSingletonStarts x ∧ pq.2 ∈ nonSandSingletonStarts x) := by ext pq simp only [bucketNN, nonRedundantPairs, Finset.mem_filter, Finset.mem_univ, true_and] constructor · rintro ⟨⟨hlt, _⟩, h⟩; exact ⟨hlt, h⟩ · rintro ⟨hlt, h1, h2⟩ refine ⟨⟨hlt, Or.inr ⟨singletonPosSet_subset_runStarts x (Finset.mem_sdiff.mp h1).1, singletonPosSet_subset_runStarts x (Finset.mem_sdiff.mp h2).1, ?_⟩⟩, h1, h2⟩ rintro ⟨hs, _⟩ exact (Finset.mem_sdiff.mp h1).2 hs rw [heq] exact card_ordered_pairs_same (nonSandSingletonStarts x) lemma card_bucketLS {q m : ℕ} (x : Word q (m + 3)) : (bucketLS x).card + (rightInLong x).card = (longRunStarts x).card * (sandwichPosSet x).card := by let All := (Finset.univ : Finset (Fin (m + 3) × Fin (m + 3))).filter (fun pq => pq.1.val < pq.2.val ∧ ((pq.1 ∈ longRunStarts x ∧ pq.2 ∈ sandwichPosSet x) ∨ (pq.1 ∈ sandwichPosSet x ∧ pq.2 ∈ longRunStarts x))) let Red := All.filter (fun pq => pq.1 ∈ sandwichPosSet x ∧ pq.2.val = pq.1.val + 1) have hdisj : Disjoint (longRunStarts x) (sandwichPosSet x) := by rw [Finset.disjoint_left] intro p h1 h2 exact (Finset.mem_sdiff.mp h1).2 (sandwichPosSet_subset_singletonPosSet x h2) have hAll : All.card = (longRunStarts x).card * (sandwichPosSet x).card := card_ordered_pairs_disj _ _ hdisj have hsub : bucketLS x = All \ Red := by ext pq simp only [bucketLS, nonRedundantPairs, All, Red, Finset.mem_sdiff, Finset.mem_filter, Finset.mem_univ, true_and] constructor · rintro ⟨⟨hlt, hnr⟩, hLS⟩ refine ⟨⟨hlt, hLS⟩, ?_⟩ rintro ⟨_, hred⟩ rcases hnr with ⟨hL, _⟩ | ⟨_, _, hnot⟩ · exact (Finset.mem_sdiff.mp hL).2 (sandwichPosSet_subset_singletonPosSet x hred.1) · exact hnot hred · rintro ⟨⟨hlt, hLS⟩, hnot⟩ refine ⟨⟨hlt, Or.inr ⟨?_, ?_, fun hred => hnot ⟨⟨hlt, hLS⟩, hred⟩⟩⟩, hLS⟩ · rcases hLS with ⟨hL, _⟩ | ⟨hS, _⟩ · exact (Finset.mem_sdiff.mp hL).1 · exact sandwichPosSet_subset_runStarts x hS · rcases hLS with ⟨_, hS⟩ | ⟨_, hL⟩ · exact sandwichPosSet_subset_runStarts x hS · exact (Finset.mem_sdiff.mp hL).1 have hRed : Red.card = (rightInLong x).card := by refine Finset.card_bij (fun pq _ => pq.1) ?_ ?_ ?_ · intro pq hpq simp only [Red, All, Finset.mem_filter, Finset.mem_univ, true_and] at hpq simp only [rightInLong, Finset.mem_filter] refine ⟨hpq.2.1, ⟨by omega, ?_⟩⟩ rcases hpq.1.2 with ⟨hL, _⟩ | ⟨_, hL⟩ · exact False.elim (Finset.disjoint_left.mp hdisj hL hpq.2.1) · have heq : (⟨pq.1.val + 1, by omega⟩ : Fin (m + 3)) = pq.2 := Fin.ext hpq.2.2.symm rwa [heq] · intro a ha b hb h simp only [Red, All, Finset.mem_filter, Finset.mem_univ, true_and] at ha hb exact Prod.ext h (Fin.ext (by rw [ha.2.2, hb.2.2, h])) · intro p hp simp only [rightInLong, Finset.mem_filter] at hp rcases hp.2 with ⟨h1, hL⟩ refine ⟨(p, ⟨p.val + 1, h1⟩), ?_, rfl⟩ simp only [Red, All, Finset.mem_filter, Finset.mem_univ, true_and] exact ⟨⟨by omega, Or.inr ⟨hp.1, hL⟩⟩, hp.1, trivial⟩ have hsdiff := Finset.card_sdiff_add_card_eq_card (show Red ⊆ All from Finset.filter_subset _ _) rw [← hsub, hRed, hAll] at hsdiff exact hsdiff lemma card_bucketNSAdj {q m : ℕ} (x : Word q (m + 3)) : (bucketNSAdj x).card = (leftInNonSand x).card := by refine Finset.card_bij (fun pq _ => pq.2) ?_ ?_ ?_ · intro pq hpq simp only [bucketNSAdj, nonRedundantPairs, Finset.mem_filter, Finset.mem_univ, true_and] at hpq simp only [leftInNonSand, Finset.mem_filter] refine ⟨hpq.2.2.1, ⟨by omega, ?_⟩⟩ have heq : (⟨pq.2.val - 1, by omega⟩ : Fin (m + 3)) = pq.1 := Fin.ext (by simp; omega) rw [heq] exact hpq.2.1 · intro a ha b hb h simp only [bucketNSAdj, nonRedundantPairs, Finset.mem_filter, Finset.mem_univ, true_and] at ha hb exact Prod.ext (Fin.ext (by have := congrArg Fin.val h; omega)) h · intro p hp simp only [leftInNonSand, Finset.mem_filter] at hp rcases hp.2 with ⟨h0, hN⟩ refine ⟨(⟨p.val - 1, by omega⟩, p), ?_, rfl⟩ simp only [bucketNSAdj, nonRedundantPairs, Finset.mem_filter, Finset.mem_univ, true_and] refine ⟨⟨by omega, Or.inr ⟨singletonPosSet_subset_runStarts x (Finset.mem_sdiff.mp hN).1, sandwichPosSet_subset_runStarts x hp.1, ?_⟩⟩, hN, hp.1, by omega⟩ rintro ⟨hs, _⟩ exact (Finset.mem_sdiff.mp hN).2 hs lemma card_bucketNSNon {q m : ℕ} (x : Word q (m + 3)) : (bucketNSNon x).card + (rightInNonSand x).card + (leftInNonSand x).card = (nonSandSingletonStarts x).card * (sandwichPosSet x).card := by let All := (Finset.univ : Finset (Fin (m + 3) × Fin (m + 3))).filter (fun pq => pq.1.val < pq.2.val ∧ ((pq.1 ∈ nonSandSingletonStarts x ∧ pq.2 ∈ sandwichPosSet x) ∨ (pq.1 ∈ sandwichPosSet x ∧ pq.2 ∈ nonSandSingletonStarts x))) let Red := All.filter (fun pq => pq.1 ∈ sandwichPosSet x ∧ pq.2.val = pq.1.val + 1) have hdisj : Disjoint (nonSandSingletonStarts x) (sandwichPosSet x) := by rw [Finset.disjoint_left] intro p h1 h2 exact (Finset.mem_sdiff.mp h1).2 h2 have hAll : All.card = (nonSandSingletonStarts x).card * (sandwichPosSet x).card := card_ordered_pairs_disj _ _ hdisj have hRed : Red.card = (rightInNonSand x).card := by refine Finset.card_bij (fun pq _ => pq.1) ?_ ?_ ?_ · intro pq hpq simp only [Red, All, Finset.mem_filter, Finset.mem_univ, true_and] at hpq simp only [rightInNonSand, Finset.mem_filter] refine ⟨hpq.2.1, ⟨by omega, ?_⟩⟩ rcases hpq.1.2 with ⟨hN, _⟩ | ⟨_, hN⟩ · exact False.elim (Finset.disjoint_left.mp hdisj hN hpq.2.1) · have heq : (⟨pq.1.val + 1, by omega⟩ : Fin (m + 3)) = pq.2 := Fin.ext hpq.2.2.symm rwa [heq] · intro a ha b hb h simp only [Red, All, Finset.mem_filter, Finset.mem_univ, true_and] at ha hb exact Prod.ext h (Fin.ext (by rw [ha.2.2, hb.2.2, h])) · intro p hp simp only [rightInNonSand, Finset.mem_filter] at hp rcases hp.2 with ⟨h1, hN⟩ refine ⟨(p, ⟨p.val + 1, h1⟩), ?_, rfl⟩ simp only [Red, All, Finset.mem_filter, Finset.mem_univ, true_and] exact ⟨⟨by omega, Or.inr ⟨hp.1, hN⟩⟩, hp.1, trivial⟩ have hdisj_an : Disjoint (bucketNSNon x) (bucketNSAdj x) := by rw [Finset.disjoint_left] intro pq h1 h2 simp only [bucketNSNon, bucketNSAdj, Finset.mem_filter] at h1 h2 rcases h1.2 with ⟨_, _, hne⟩ | ⟨hS, _⟩ · exact hne h2.2.2.2 · exact (Finset.mem_sdiff.mp h2.2.1).2 hS have hunion : bucketNSNon x ∪ bucketNSAdj x = All \ Red := by ext pq simp only [bucketNSNon, bucketNSAdj, nonRedundantPairs, All, Red, Finset.mem_union, Finset.mem_sdiff, Finset.mem_filter, Finset.mem_univ, true_and] constructor · rintro (⟨⟨hlt, hnr⟩, hNS⟩ | ⟨⟨hlt, hnr⟩, hN, hS, hadj⟩) · have hAll' : pq.1.val < pq.2.val ∧ ((pq.1 ∈ nonSandSingletonStarts x ∧ pq.2 ∈ sandwichPosSet x) ∨ (pq.1 ∈ sandwichPosSet x ∧ pq.2 ∈ nonSandSingletonStarts x)) := by rcases hNS with ⟨h1, h2, _⟩ | ⟨h1, h2⟩ · exact ⟨hlt, Or.inl ⟨h1, h2⟩⟩ · exact ⟨hlt, Or.inr ⟨h1, h2⟩⟩ refine ⟨hAll', ?_⟩ rintro ⟨_, hred⟩ rcases hnr with ⟨hL, _⟩ | ⟨_, _, hnot⟩ · exact (Finset.mem_sdiff.mp hL).2 (sandwichPosSet_subset_singletonPosSet x hred.1) · exact hnot hred · refine ⟨⟨hlt, Or.inl ⟨hN, hS⟩⟩, ?_⟩ rintro ⟨_, hred⟩ exact (Finset.mem_sdiff.mp hN).2 hred.1 · rintro ⟨⟨hlt, hNS⟩, hnot⟩ have hnr : pq.1.val < pq.2.val ∧ ((pq.1 ∈ longRunStarts x ∧ pq.2.val = pq.1.val + 1) ∨ (pq.1 ∈ runStarts x ∧ pq.2 ∈ runStarts x ∧ ¬ (pq.1 ∈ sandwichPosSet x ∧ pq.2.val = pq.1.val + 1))) := by refine ⟨hlt, Or.inr ⟨?_, ?_, fun hred => hnot ⟨⟨hlt, hNS⟩, hred⟩⟩⟩ · rcases hNS with ⟨hN, _⟩ | ⟨hS, _⟩ · exact singletonPosSet_subset_runStarts x (Finset.mem_sdiff.mp hN).1 · exact sandwichPosSet_subset_runStarts x hS · rcases hNS with ⟨_, hS⟩ | ⟨_, hN⟩ · exact sandwichPosSet_subset_runStarts x hS · exact singletonPosSet_subset_runStarts x (Finset.mem_sdiff.mp hN).1 rcases hNS with ⟨hN, hS⟩ | ⟨hS, hN⟩ · by_cases hadj : pq.2.val = pq.1.val + 1 · right; exact ⟨hnr, hN, hS, hadj⟩ · left; exact ⟨hnr, Or.inl ⟨hN, hS, hadj⟩⟩ · left; exact ⟨hnr, Or.inr ⟨hS, hN⟩⟩ have hsdiff := Finset.card_sdiff_add_card_eq_card (show Red ⊆ All from Finset.filter_subset _ _) rw [← hunion, Finset.card_union_of_disjoint hdisj_an, card_bucketNSAdj, hRed, hAll] at hsdiff omega lemma card_bucketSS {q m : ℕ} (x : Word q (m + 3)) : 2 * ((bucketSS x).card + (rightInSand x).card) = (sandwichPosSet x).card * ((sandwichPosSet x).card - 1) := by let All := (Finset.univ : Finset (Fin (m + 3) × Fin (m + 3))).filter (fun pq => pq.1.val < pq.2.val ∧ pq.1 ∈ sandwichPosSet x ∧ pq.2 ∈ sandwichPosSet x) let Red := All.filter (fun pq => pq.2.val = pq.1.val + 1) have hAll := card_ordered_pairs_same (sandwichPosSet x) have hsub : bucketSS x = All \ Red := by ext pq simp only [bucketSS, nonRedundantPairs, All, Red, Finset.mem_sdiff, Finset.mem_filter, Finset.mem_univ, true_and] constructor · rintro ⟨⟨hlt, hnr⟩, h1, h2⟩ refine ⟨⟨hlt, h1, h2⟩, ?_⟩ rintro ⟨_, hadj⟩ rcases hnr with ⟨hL, _⟩ | ⟨_, _, hnot⟩ · exact (Finset.mem_sdiff.mp hL).2 (sandwichPosSet_subset_singletonPosSet x h1) · exact hnot ⟨h1, hadj⟩ · rintro ⟨⟨hlt, h1, h2⟩, hnot⟩ refine ⟨⟨hlt, Or.inr ⟨sandwichPosSet_subset_runStarts x h1, sandwichPosSet_subset_runStarts x h2, fun hred => hnot ⟨⟨hlt, h1, h2⟩, hred.2⟩⟩⟩, h1, h2⟩ have hRed : Red.card = (rightInSand x).card := by refine Finset.card_bij (fun pq _ => pq.1) ?_ ?_ ?_ · intro pq hpq simp only [Red, All, Finset.mem_filter, Finset.mem_univ, true_and] at hpq simp only [rightInSand, Finset.mem_filter] refine ⟨hpq.1.2.1, ⟨by omega, ?_⟩⟩ have heq : (⟨pq.1.val + 1, by omega⟩ : Fin (m + 3)) = pq.2 := Fin.ext hpq.2.symm rw [heq] exact hpq.1.2.2 · intro a ha b hb h simp only [Red, All, Finset.mem_filter, Finset.mem_univ, true_and] at ha hb exact Prod.ext h (Fin.ext (by rw [ha.2, hb.2, h])) · intro p hp simp only [rightInSand, Finset.mem_filter] at hp rcases hp.2 with ⟨h1, hS⟩ refine ⟨(p, ⟨p.val + 1, h1⟩), ?_, rfl⟩ simp only [Red, All, Finset.mem_filter, Finset.mem_univ, true_and] exact ⟨⟨by omega, hp.1, hS⟩, trivial⟩ have hsdiff := Finset.card_sdiff_add_card_eq_card (show Red ⊆ All from Finset.filter_subset _ _) rw [← hsub, hRed] at hsdiff rw [hsdiff] exact hAll /-- The 7 buckets partition `nonRedundantPairs x`, giving a lower bound on ANY nonnegative dual-weight sum over `del2Ball x`. -/ theorem sum_del2Ball_ge_seven_buckets {q m : ℕ} (x : Word q (m + 3)) (w : Word q (m + 1) → ℝ) (hw_nn : ∀ y, 0 ≤ w y) : (∑ pq ∈ bucketLL x, w (del2 x pq.1 pq.2)) + (∑ pq ∈ bucketLN x, w (del2 x pq.1 pq.2)) + (∑ pq ∈ bucketLS x, w (del2 x pq.1 pq.2)) + (∑ pq ∈ bucketNN x, w (del2 x pq.1 pq.2)) + (∑ pq ∈ bucketNSAdj x, w (del2 x pq.1 pq.2)) + (∑ pq ∈ bucketNSNon x, w (del2 x pq.1 pq.2)) + (∑ pq ∈ bucketSS x, w (del2 x pq.1 pq.2)) ≤ ∑ y ∈ del2Ball x, w y := by have himg_sub : (nonRedundantPairs x).image (fun pq => del2 x pq.1 pq.2) ⊆ del2Ball x := by intro y hy rcases Finset.mem_image.mp hy with ⟨pq, hpq, rfl⟩ simp only [nonRedundantPairs, Finset.mem_filter, Finset.mem_univ, true_and] at hpq exact del2_mem_del2Ball x pq.1 pq.2 hpq.1 have h1 : ∑ pq ∈ nonRedundantPairs x, w (del2 x pq.1 pq.2) ≤ ∑ y ∈ del2Ball x, w y := by calc ∑ pq ∈ nonRedundantPairs x, w (del2 x pq.1 pq.2) = ∑ y ∈ (nonRedundantPairs x).image (fun pq => del2 x pq.1 pq.2), w y := (Finset.sum_image (fun a ha b hb h => del2_injOn_nonRedundant x ha hb h)).symm _ ≤ ∑ y ∈ del2Ball x, w y := Finset.sum_le_sum_of_subset_of_nonneg himg_sub fun y _ _ => hw_nn y -- Show the 7 buckets are pairwise disjoint and their union is `nonRedundantPairs x` let B := [bucketLL x, bucketLN x, bucketLS x, bucketNN x, bucketNSAdj x, bucketNSNon x, bucketSS x] have hmem_classify : ∀ pq ∈ nonRedundantPairs x, (pq ∈ bucketLL x ∧ pq ∉ bucketLN x ∧ pq ∉ bucketLS x ∧ pq ∉ bucketNN x ∧ pq ∉ bucketNSAdj x ∧ pq ∉ bucketNSNon x ∧ pq ∉ bucketSS x) ∨ (pq ∉ bucketLL x ∧ pq ∈ bucketLN x ∧ pq ∉ bucketLS x ∧ pq ∉ bucketNN x ∧ pq ∉ bucketNSAdj x ∧ pq ∉ bucketNSNon x ∧ pq ∉ bucketSS x) ∨ (pq ∉ bucketLL x ∧ pq ∉ bucketLN x ∧ pq ∈ bucketLS x ∧ pq ∉ bucketNN x ∧ pq ∉ bucketNSAdj x ∧ pq ∉ bucketNSNon x ∧ pq ∉ bucketSS x) ∨ (pq ∉ bucketLL x ∧ pq ∉ bucketLN x ∧ pq ∉ bucketLS x ∧ pq ∈ bucketNN x ∧ pq ∉ bucketNSAdj x ∧ pq ∉ bucketNSNon x ∧ pq ∉ bucketSS x) ∨ (pq ∉ bucketLL x ∧ pq ∉ bucketLN x ∧ pq ∉ bucketLS x ∧ pq ∉ bucketNN x ∧ pq ∈ bucketNSAdj x ∧ pq ∉ bucketNSNon x ∧ pq ∉ bucketSS x) ∨ (pq ∉ bucketLL x ∧ pq ∉ bucketLN x ∧ pq ∉ bucketLS x ∧ pq ∉ bucketNN x ∧ pq ∉ bucketNSAdj x ∧ pq ∈ bucketNSNon x ∧ pq ∉ bucketSS x) ∨ (pq ∉ bucketLL x ∧ pq ∉ bucketLN x ∧ pq ∉ bucketLS x ∧ pq ∉ bucketNN x ∧ pq ∉ bucketNSAdj x ∧ pq ∉ bucketNSNon x ∧ pq ∈ bucketSS x) := by intro pq hpq have hpq' := hpq simp only [nonRedundantPairs, Finset.mem_filter, Finset.mem_univ, true_and] at hpq' have hdisj_LN : ∀ p, p ∈ longRunStarts x → p ∉ nonSandSingletonStarts x := fun p h1 h2 => (Finset.mem_sdiff.mp h1).2 (Finset.mem_sdiff.mp h2).1 have hdisj_LS : ∀ p, p ∈ longRunStarts x → p ∉ sandwichPosSet x := fun p h1 h2 => (Finset.mem_sdiff.mp h1).2 (sandwichPosSet_subset_singletonPosSet x h2) have hdisj_NS : ∀ p, p ∈ nonSandSingletonStarts x → p ∉ sandwichPosSet x := fun p h1 h2 => (Finset.mem_sdiff.mp h1).2 h2 have hdisj_NL : ∀ p, p ∈ nonSandSingletonStarts x → p ∉ longRunStarts x := fun p hN hL => hdisj_LN p hL hN have hdisj_SL : ∀ p, p ∈ sandwichPosSet x → p ∉ longRunStarts x := fun p hS hL => hdisj_LS p hL hS have hdisj_SN : ∀ p, p ∈ sandwichPosSet x → p ∉ nonSandSingletonStarts x := fun p hS hN => hdisj_NS p hN hS have hnot_run_of_same : pq.1 ∈ longRunStarts x → pq.2.val = pq.1.val + 1 → pq.2 ∉ runStarts x := by intro hL h2 hrun rcases (mem_longRunStarts_iff x pq.1).mp hL with ⟨_, hlt, heq⟩ have hp2_eq : pq.2 = ⟨pq.1.val + 1, hlt⟩ := Fin.ext h2 rcases (mem_runStarts_iff x pq.2).mp hrun with h0 | ⟨_, hne⟩ · omega · apply hne have hpred : (⟨pq.2.val - 1, by omega⟩ : Fin (m + 3)) = pq.1 := Fin.ext (by simp; omega) rw [hpred, hp2_eq]; exact heq have hsplit : ∀ p ∈ runStarts x, p ∈ longRunStarts x ∨ p ∈ nonSandSingletonStarts x ∨ p ∈ sandwichPosSet x := by intro p hp by_cases hs : p ∈ sandwichPosSet x · exact Or.inr (Or.inr hs) · by_cases hsing : p ∈ singletonPosSet x · exact Or.inr (Or.inl (Finset.mem_sdiff.mpr ⟨hsing, hs⟩)) · exact Or.inl (Finset.mem_sdiff.mpr ⟨hp, hsing⟩) rcases hpq'.2 with ⟨hL, h2⟩ | ⟨hr1, hr2, _⟩ · have hnr2 := hnot_run_of_same hL h2 have h2L : pq.2 ∉ longRunStarts x := fun h => hnr2 (Finset.mem_sdiff.mp h).1 have h2N : pq.2 ∉ nonSandSingletonStarts x := fun h => hnr2 (singletonPosSet_subset_runStarts x (Finset.mem_sdiff.mp h).1) have h2S : pq.2 ∉ sandwichPosSet x := fun h => hnr2 (sandwichPosSet_subset_runStarts x h) left simp [bucketLL, bucketLN, bucketLS, bucketNN, bucketNSAdj, bucketNSNon, bucketSS, hpq, hL, h2, h2L, h2N, h2S, hdisj_LN _ hL, hdisj_LS _ hL] · rcases hsplit pq.1 hr1 with h1L | h1N | h1S <;> rcases hsplit pq.2 hr2 with h2L | h2N | h2S · left simp [bucketLL, bucketLN, bucketLS, bucketNN, bucketNSAdj, bucketNSNon, bucketSS, hpq, h1L, h2L, hdisj_LN _ h1L, hdisj_LS _ h1L, hdisj_LN _ h2L, hdisj_LS _ h2L] · right; left have hne : pq.2.val ≠ pq.1.val + 1 := fun h => hnot_run_of_same h1L h hr2 simp [bucketLL, bucketLN, bucketLS, bucketNN, bucketNSAdj, bucketNSNon, bucketSS, hpq, h1L, h2N, hne, hdisj_LN _ h1L, hdisj_LS _ h1L, hdisj_NS _ h2N, hdisj_NL _ h2N] · right; right; left have hne : pq.2.val ≠ pq.1.val + 1 := fun h => hnot_run_of_same h1L h hr2 simp [bucketLL, bucketLN, bucketLS, bucketNN, bucketNSAdj, bucketNSNon, bucketSS, hpq, h1L, h2S, hne, hdisj_LN _ h1L, hdisj_LS _ h1L, hdisj_SL _ h2S, hdisj_SN _ h2S] · right; left simp [bucketLL, bucketLN, bucketLS, bucketNN, bucketNSAdj, bucketNSNon, bucketSS, hpq, h1N, h2L, hdisj_NS _ h1N, hdisj_NL _ h1N, hdisj_LN _ h2L, hdisj_LS _ h2L] · right; right; right; left simp [bucketLL, bucketLN, bucketLS, bucketNN, bucketNSAdj, bucketNSNon, bucketSS, hpq, h1N, h2N, hdisj_NS _ h1N, hdisj_NL _ h1N, hdisj_NS _ h2N, hdisj_NL _ h2N] · by_cases hadj : pq.2.val = pq.1.val + 1 · right; right; right; right; left simp [bucketLL, bucketLN, bucketLS, bucketNN, bucketNSAdj, bucketNSNon, bucketSS, hpq, h1N, h2S, hadj, hdisj_NS _ h1N, hdisj_NL _ h1N, hdisj_SL _ h2S, hdisj_SN _ h2S] · right; right; right; right; right; left simp [bucketLL, bucketLN, bucketLS, bucketNN, bucketNSAdj, bucketNSNon, bucketSS, hpq, h1N, h2S, hadj, hdisj_NS _ h1N, hdisj_NL _ h1N, hdisj_SL _ h2S, hdisj_SN _ h2S] · right; right; left simp [bucketLL, bucketLN, bucketLS, bucketNN, bucketNSAdj, bucketNSNon, bucketSS, hpq, h1S, h2L, hdisj_LN _ h2L, hdisj_LS _ h2L, hdisj_SL _ h1S, hdisj_SN _ h1S] · right; right; right; right; right; left simp [bucketLL, bucketLN, bucketLS, bucketNN, bucketNSAdj, bucketNSNon, bucketSS, hpq, h1S, h2N, hdisj_NS _ h2N, hdisj_NL _ h2N, hdisj_SL _ h1S, hdisj_SN _ h1S] · right; right; right; right; right; right simp [bucketLL, bucketLN, bucketLS, bucketNN, bucketNSAdj, bucketNSNon, bucketSS, hpq, h1S, h2S, hdisj_SL _ h1S, hdisj_SN _ h1S, hdisj_SL _ h2S, hdisj_SN _ h2S] have hsum_eq : (∑ pq ∈ bucketLL x, w (del2 x pq.1 pq.2)) + (∑ pq ∈ bucketLN x, w (del2 x pq.1 pq.2)) + (∑ pq ∈ bucketLS x, w (del2 x pq.1 pq.2)) + (∑ pq ∈ bucketNN x, w (del2 x pq.1 pq.2)) + (∑ pq ∈ bucketNSAdj x, w (del2 x pq.1 pq.2)) + (∑ pq ∈ bucketNSNon x, w (del2 x pq.1 pq.2)) + (∑ pq ∈ bucketSS x, w (del2 x pq.1 pq.2)) = ∑ pq ∈ nonRedundantPairs x, w (del2 x pq.1 pq.2) := by have hite : ∀ S : Finset (Fin (m + 3) × Fin (m + 3)), S ⊆ nonRedundantPairs x → ∑ pq ∈ S, w (del2 x pq.1 pq.2) = ∑ pq ∈ nonRedundantPairs x, if pq ∈ S then w (del2 x pq.1 pq.2) else 0 := fun S hS => by rw [Finset.sum_ite_mem, Finset.inter_eq_right.mpr hS] rw [hite (bucketLL x) (Finset.filter_subset _ _), hite (bucketLN x) (Finset.filter_subset _ _), hite (bucketLS x) (Finset.filter_subset _ _), hite (bucketNN x) (Finset.filter_subset _ _), hite (bucketNSAdj x) (Finset.filter_subset _ _), hite (bucketNSNon x) (Finset.filter_subset _ _), hite (bucketSS x) (Finset.filter_subset _ _)] simp only [← Finset.sum_add_distrib] refine Finset.sum_congr rfl fun pq hpq => ?_ rcases hmem_classify pq hpq with ⟨h1, h2, h3, h4, h5, h6, h7⟩ | ⟨h1, h2, h3, h4, h5, h6, h7⟩ | ⟨h1, h2, h3, h4, h5, h6, h7⟩ | ⟨h1, h2, h3, h4, h5, h6, h7⟩ | ⟨h1, h2, h3, h4, h5, h6, h7⟩ | ⟨h1, h2, h3, h4, h5, h6, h7⟩ | ⟨h1, h2, h3, h4, h5, h6, h7⟩ <;> simp [h1, h2, h3, h4, h5, h6, h7] linarith /-- **Exact 2-Deletion Ball Lower Bound**: `|D₂(x)| ≥ C(r + 1, 2) - r₁(x) - a₁(x)` (matching the exact cardinality `C(r+1, 2) - r₁ - a₁`). -/ theorem card_del2Ball_ge_nonRedundant {q m : ℕ} (x : Word q (m + 3)) : 2 * (del2Ball x).card + 2 * singletonRunCount x + 2 * sandwichCount x ≥ runCount x * (runCount x + 1) := by have hsum := sum_del2Ball_ge_seven_buckets x (fun _ => (1 : ℝ)) (fun _ => zero_le_one) simp only [Finset.sum_const, nsmul_eq_mul, mul_one] at hsum have hnat : (bucketLL x).card + (bucketLN x).card + (bucketLS x).card + (bucketNN x).card + (bucketNSAdj x).card + (bucketNSNon x).card + (bucketSS x).card ≤ (del2Ball x).card := by exact_mod_cast hsum have hLL := card_bucketLL x have hLN := card_bucketLN x have hLS := card_bucketLS x have hNN := card_bucketNN x have hNSa := card_bucketNSAdj x have hNSn := card_bucketNSNon x have hSS := card_bucketSS x have hrp := card_right_partition x have hpart := card_run_partition x have hsing : (nonSandSingletonStarts x).card + (sandwichPosSet x).card = singletonRunCount x := Finset.card_sdiff_add_card_eq_card (sandwichPosSet_subset_singletonPosSet x) set L := (longRunStarts x).card set N := (nonSandSingletonStarts x).card set S := (sandwichPosSet x).card have h2 : 2 * ((bucketLL x).card + (bucketLN x).card + (bucketLS x).card + (bucketNN x).card + (bucketNSAdj x).card + (bucketNSNon x).card + (bucketSS x).card) + 2 * (N + S) + 2 * S = (L + N + S) * (L + N + S + 1) := by -- Clear `N*(N-1)` and `S*(S-1)` cleanly in `ℤ` zify at hLL hLN hLS hNN hNSa hNSn hSS hrp ⊢ have hN' : (N : ℤ) * ((N - 1 : ℕ) : ℤ) = (N : ℤ) * ((N : ℤ) - 1) := by cases N <;> simp have hS' : (S : ℤ) * ((S - 1 : ℕ) : ℤ) = (S : ℤ) * ((S : ℤ) - 1) := by cases S <;> simp rw [hN'] at hNN rw [hS'] at hSS linarith unfold sandwichCount rw [← hpart, ← hsing] omega end RunPairGeometry /-! ## Section 3: Universal Shift Bounds, Second-Order Dual Weight & Exact Feasibility -/ section SecondOrderDual /-- Deleting a single coordinate increases `singletonRunCount` by at most `1`. -/ theorem singletonRunCount_delPos_le_add_one {q L : ℕ} (x : Word q (L + 2)) (p : Fin (L + 2)) : singletonRunCount (delPos x p) ≤ singletonRunCount x + 1 := by unfold singletonRunCount let z := delPos x p let A := (singletonPosSet z).filter fun j : Fin (L + 1) => j.val + 1 < p.val ∨ p.val < j.val let B := (singletonPosSet z).filter fun j : Fin (L + 1) => ¬ (j.val + 1 < p.val ∨ p.val < j.val) have hpart : (singletonPosSet z).card = A.card + B.card := by rw [← Finset.card_union_of_disjoint (Finset.disjoint_filter.mpr fun _ _ h1 h2 => h2 h1)] congr 1; ext j; simp only [Finset.mem_union, Finset.mem_filter]; tauto -- Map `A` injectively into `(singletonPosSet x).filter (fun i => i.val + 1 < p.val ∨ p.val + 1 < i.val)` let FarX := (singletonPosSet x).filter fun i : Fin (L + 2) => i.val + 1 < p.val ∨ p.val + 1 < i.val let f : Fin (L + 1) → Fin (L + 2) := fun j => if hlt : j.val + 1 < p.val then ⟨j.val, by omega⟩ else ⟨j.val + 1, by omega⟩ have hA_le : A.card ≤ FarX.card := by refine Finset.card_le_card_of_injOn f ?_ ?_ · intro j hj simp only [Finset.mem_coe, A, singletonPosSet, Finset.mem_filter, Finset.mem_univ, true_and] at hj rcases hj with ⟨⟨hleft, hright⟩, hfar⟩ simp only [Finset.mem_coe, FarX, singletonPosSet, Finset.mem_filter, Finset.mem_univ, true_and] dsimp only [f] split_ifs with hlt · refine ⟨⟨fun h0 => ?_, fun h1 => ?_⟩, Or.inl hlt⟩ · have hz := hleft h0 dsimp only [z] at hz rw [delPos_apply_lt x p _ (by simp; omega), delPos_apply_lt x p _ (by omega)] at hz exact hz · have hz := hright (show j.val + 1 < L + 1 by omega) dsimp only [z] at hz rw [delPos_apply_lt x p _ (by omega), delPos_apply_lt x p _ (by simp; omega)] at hz exact hz · have hgt : p.val < j.val := by omega refine ⟨⟨fun h0 => ?_, fun h1 => ?_⟩, Or.inr (by dsimp only; omega)⟩ · have hz := hleft (show 0 < j.val by omega) dsimp only [z] at hz rw [delPos_apply_ge x p _ (by simp; omega), delPos_apply_ge x p _ (by omega)] at hz have h1' : (⟨j.val - 1 + 1, by omega⟩ : Fin (L + 2)) = ⟨j.val + 1 - 1, by omega⟩ := Fin.ext (by simp; omega) rw [h1'] at hz exact hz · have hz := hright (show j.val + 1 < L + 1 by dsimp only at h1; omega) dsimp only [z] at hz rw [delPos_apply_ge x p _ (by omega), delPos_apply_ge x p _ (by simp; omega)] at hz exact hz · intro j₁ hj₁ j₂ hj₂ hfeq simp only [Finset.mem_coe, A, Finset.mem_filter] at hj₁ hj₂ dsimp only [f] at hfeq have hval := congrArg Fin.val hfeq split_ifs at hval <;> (dsimp only at hval; exact Fin.ext (by omega)) have hB_le : B.card ≤ 2 := by have hsub : B ⊆ (Finset.univ : Finset (Fin (L + 1))).filter (fun j => j.val + 1 = p.val ∨ j.val = p.val) := by intro j hj simp only [B, Finset.mem_filter, Finset.mem_univ, true_and] at hj ⊢ omega have h2 : ((Finset.univ : Finset (Fin (L + 1))).filter (fun j => j.val + 1 = p.val ∨ j.val = p.val)).card ≤ 2 := by let S1 := (Finset.univ : Finset (Fin (L + 1))).filter (fun j => j.val + 1 = p.val) let S2 := (Finset.univ : Finset (Fin (L + 1))).filter (fun j => j.val = p.val) have hun : ((Finset.univ : Finset (Fin (L + 1))).filter (fun j => j.val + 1 = p.val ∨ j.val = p.val)) ⊆ S1 ∪ S2 := by intro j hj simp only [S1, S2, Finset.mem_union, Finset.mem_filter, Finset.mem_univ, true_and] at hj ⊢ exact hj have hc1 : S1.card ≤ 1 := Finset.card_le_one.mpr fun a ha b hb => by simp only [S1, Finset.mem_filter, Finset.mem_univ, true_and] at ha hb exact Fin.ext (by omega) have hc2 : S2.card ≤ 1 := Finset.card_le_one.mpr fun a ha b hb => by simp only [S2, Finset.mem_filter, Finset.mem_univ, true_and] at ha hb exact Fin.ext (by omega) calc _ ≤ (S1 ∪ S2).card := Finset.card_le_card hun _ ≤ S1.card + S2.card := Finset.card_union_le _ _ _ ≤ 2 := by omega exact (Finset.card_le_card hsub).trans h2 by_cases hB2 : B.card ≤ 1 · have hFar_le : FarX.card ≤ (singletonPosSet x).card := Finset.card_filter_le _ _ change (singletonPosSet z).card ≤ (singletonPosSet x).card + 1 omega · -- `B.card = 2`: both `jL = p - 1` and `jR = p` lie in `singletonPosSet z`, -- which forces at least one of `{p - 1, p, p + 1}` to lie in `singletonPosSet x \ FarX`! have hB_eq2 : B.card = 2 := by omega have hp_pos : 0 < p.val := by by_contra h0 have hsub : B ⊆ (Finset.univ : Finset (Fin (L + 1))).filter (fun j => j.val = 0) := by intro j hj simp only [B, Finset.mem_filter, Finset.mem_univ, true_and] at hj ⊢ omega have hc : ((Finset.univ : Finset (Fin (L + 1))).filter (fun j => j.val = 0)).card ≤ 1 := Finset.card_le_one.mpr fun a ha b hb => by simp only [Finset.mem_filter, Finset.mem_univ, true_and] at ha hb exact Fin.ext (by omega) have := (Finset.card_le_card hsub).trans hc omega have hp_lt : p.val < L + 1 := by by_contra hge have hsub : B ⊆ (Finset.univ : Finset (Fin (L + 1))).filter (fun j => j.val + 1 = p.val) := by intro j hj simp only [B, Finset.mem_filter, Finset.mem_univ, true_and] at hj ⊢ omega have hc : ((Finset.univ : Finset (Fin (L + 1))).filter (fun j => j.val + 1 = p.val)).card ≤ 1 := Finset.card_le_one.mpr fun a ha b hb => by simp only [Finset.mem_filter, Finset.mem_univ, true_and] at ha hb exact Fin.ext (by omega) have := (Finset.card_le_card hsub).trans hc omega let jL : Fin (L + 1) := ⟨p.val - 1, by omega⟩ let jR : Fin (L + 1) := ⟨p.val, by omega⟩ have hB_sub_pair : B ⊆ {jL, jR} := by intro j hj simp only [B, Finset.mem_filter, Finset.mem_insert, Finset.mem_singleton] at hj ⊢ have hval : j.val + 1 = p.val ∨ j.val = p.val := by omega rcases hval with h1 | h2 · left; exact Fin.ext (by simp [jL]; omega) · right; exact Fin.ext (by simp [jR]; omega) have hjLR : jL ≠ jR := fun h => by have := congrArg Fin.val h; simp [jL, jR] at this; omega have hB_eq_pair : B = {jL, jR} := Finset.eq_of_subset_of_card_le hB_sub_pair (by rw [Finset.card_pair hjLR]; omega) have hjL_mem : jL ∈ singletonPosSet z := by have : jL ∈ B := by rw [hB_eq_pair]; simp exact (Finset.mem_filter.mp this).1 have hjR_mem : jR ∈ singletonPosSet z := by have : jR ∈ B := by rw [hB_eq_pair]; simp exact (Finset.mem_filter.mp this).1 simp only [singletonPosSet, Finset.mem_filter, Finset.mem_univ, true_and] at hjL_mem hjR_mem have h_pm1_pp1 : x ⟨p.val - 1, by omega⟩ ≠ x ⟨p.val + 1, by omega⟩ := by have hz := hjL_mem.2 (show jL.val + 1 < L + 1 by dsimp only [jL]; omega) dsimp only [z] at hz rw [delPos_apply_lt x p jL (by dsimp only [jL]; omega), delPos_apply_ge x p ⟨jL.val + 1, by dsimp only [jL]; omega⟩ (by dsimp only [jL]; omega)] at hz have h1' : (⟨jL.val, by omega⟩ : Fin (L + 2)) = ⟨p.val - 1, by omega⟩ := Fin.ext (by dsimp only [jL]) have h2' : (⟨jL.val + 1 + 1, by dsimp only [jL]; omega⟩ : Fin (L + 2)) = ⟨p.val + 1, by omega⟩ := Fin.ext (by dsimp only [jL]; omega) rwa [h1', h2'] at hz -- Find a witness `w ∈ singletonPosSet x \ FarX` among `{p - 1, p, p + 1}`! have hex_near : ∃ w ∈ singletonPosSet x, w ∉ FarX := by by_cases hL : x ⟨p.val - 1, by omega⟩ = x p · -- Then `p + 1 ∈ singletonPosSet x \ FarX`! refine ⟨⟨p.val + 1, by omega⟩, ?_, ?_⟩ · simp only [singletonPosSet, Finset.mem_filter, Finset.mem_univ, true_and] refine ⟨fun _ => ?_, fun h1 => ?_⟩ · have h1' : (⟨p.val + 1 - 1, by omega⟩ : Fin (L + 2)) = p := Fin.ext (by simp) rw [h1', ← hL]; exact h_pm1_pp1 · have hz := hjR_mem.2 (show jR.val + 1 < L + 1 by dsimp only [jR]; omega) dsimp only [z] at hz rw [delPos_apply_ge x p jR (by dsimp only [jR]; omega), delPos_apply_ge x p ⟨jR.val + 1, by dsimp only [jR]; omega⟩ (by dsimp only [jR]; omega)] at hz exact hz · simp only [FarX, Finset.mem_filter]; intro h; omega · by_cases hR : x p = x ⟨p.val + 1, by omega⟩ · -- Then `p - 1 ∈ singletonPosSet x \ FarX`! refine ⟨⟨p.val - 1, by omega⟩, ?_, ?_⟩ · simp only [singletonPosSet, Finset.mem_filter, Finset.mem_univ, true_and] refine ⟨fun h0 => ?_, fun _ => ?_⟩ · have hz := hjL_mem.1 (show 0 < jL.val by dsimp only [jL]; omega) dsimp only [z] at hz rw [delPos_apply_lt x p ⟨jL.val - 1, by dsimp only [jL]; omega⟩ (by dsimp only [jL]; omega), delPos_apply_lt x p jL (by dsimp only [jL]; omega)] at hz exact hz · have h1' : (⟨p.val - 1 + 1, by omega⟩ : Fin (L + 2)) = p := Fin.ext (by simp; omega) rw [h1']; exact hL · simp only [FarX, Finset.mem_filter]; intro h; omega · -- Then `p ∈ singletonPosSet x \ FarX`! refine ⟨p, ?_, ?_⟩ · simp only [singletonPosSet, Finset.mem_filter, Finset.mem_univ, true_and] exact ⟨fun _ => hL, fun _ => hR⟩ · simp only [FarX, Finset.mem_filter]; intro h; omega rcases hex_near with ⟨w, hw_sing, hw_not⟩ have hlt : FarX.card < (singletonPosSet x).card := Finset.card_lt_card (Finset.ssubset_iff_subset_ne.mpr ⟨Finset.filter_subset _ _, fun heq => hw_not (heq ▸ hw_sing)⟩) change (singletonPosSet z).card ≤ (singletonPosSet x).card + 1 omega /-- Deleting a single coordinate increases `sandwichCount` by at most `2`. -/ theorem sandwichCount_delPos_le_add_two {q L : ℕ} (x : Word q (L + 2)) (p : Fin (L + 2)) : sandwichCount (delPos x p) ≤ sandwichCount x + 2 := by unfold sandwichCount let z := delPos x p let A := (sandwichPosSet z).filter fun j : Fin (L + 1) => j.val + 1 < p.val ∨ p.val < j.val let B := (sandwichPosSet z).filter fun j : Fin (L + 1) => ¬ (j.val + 1 < p.val ∨ p.val < j.val) have hpart : (sandwichPosSet z).card = A.card + B.card := by rw [← Finset.card_union_of_disjoint (Finset.disjoint_filter.mpr fun _ _ h1 h2 => h2 h1)] congr 1; ext j; simp only [Finset.mem_union, Finset.mem_filter]; tauto let f : Fin (L + 1) → Fin (L + 2) := fun j => if hlt : j.val + 1 < p.val then ⟨j.val, by omega⟩ else ⟨j.val + 1, by omega⟩ have hA_le : A.card ≤ (sandwichPosSet x).card := by refine Finset.card_le_card_of_injOn f ?_ ?_ · intro j hj simp only [Finset.mem_coe, A, sandwichPosSet, Finset.mem_filter, Finset.mem_univ, true_and] at hj ⊢ rcases hj with ⟨⟨h0, h1, heq, hne⟩, hfar⟩ dsimp only [f] split_ifs with hlt · refine ⟨h0, by dsimp only; omega, ?_, ?_⟩ · dsimp only [z] at heq rw [delPos_apply_lt x p _ (by simp; omega), delPos_apply_lt x p _ (by simp; omega)] at heq exact heq · dsimp only [z] at hne rw [delPos_apply_lt x p _ (by omega), delPos_apply_lt x p _ (by simp; omega)] at hne exact hne · have hgt : p.val < j.val := by omega refine ⟨by dsimp only; omega, by dsimp only; omega, ?_, ?_⟩ · dsimp only [z] at heq rw [delPos_apply_ge x p _ (by simp; omega), delPos_apply_ge x p _ (by simp; omega)] at heq have h1' : (⟨j.val - 1 + 1, by omega⟩ : Fin (L + 2)) = ⟨j.val + 1 - 1, by omega⟩ := Fin.ext (by simp; omega) rw [h1'] at heq exact heq · dsimp only [z] at hne rw [delPos_apply_ge x p _ (by omega), delPos_apply_ge x p _ (by simp; omega)] at hne exact hne · intro j₁ hj₁ j₂ hj₂ hfeq simp only [Finset.mem_coe, A, Finset.mem_filter] at hj₁ hj₂ dsimp only [f] at hfeq have hval := congrArg Fin.val hfeq split_ifs at hval <;> (dsimp only at hval; exact Fin.ext (by omega)) have hB_le : B.card ≤ 2 := by let S1 := (Finset.univ : Finset (Fin (L + 1))).filter (fun j => j.val + 1 = p.val) let S2 := (Finset.univ : Finset (Fin (L + 1))).filter (fun j => j.val = p.val) have hsub : B ⊆ S1 ∪ S2 := by intro j hj simp only [B, S1, S2, Finset.mem_union, Finset.mem_filter, Finset.mem_univ, true_and] at hj ⊢ omega have hc1 : S1.card ≤ 1 := Finset.card_le_one.mpr fun a ha b hb => by simp only [S1, Finset.mem_filter, Finset.mem_univ, true_and] at ha hb exact Fin.ext (by omega) have hc2 : S2.card ≤ 1 := Finset.card_le_one.mpr fun a ha b hb => by simp only [S2, Finset.mem_filter, Finset.mem_univ, true_and] at ha hb exact Fin.ext (by omega) calc B.card ≤ (S1 ∪ S2).card := Finset.card_le_card hsub _ ≤ S1.card + S2.card := Finset.card_union_le _ _ _ ≤ 2 := by omega change (sandwichPosSet z).card ≤ (sandwichPosSet x).card + 2 omega /-- Universal 2-deletion shift bounds: `r₁(y) ≤ r₁(x) + 2` and `a₁(y) ≤ a₁(x) + 4` for EVERY `y ∈ D₂(x)`. -/ theorem singletonRunCount_del2Ball_le {q m : ℕ} (x : Word q (m + 3)) {y : Word q (m + 1)} (hy : y ∈ del2Ball x) : singletonRunCount y ≤ singletonRunCount x + 2 := by unfold del2Ball at hy rcases Finset.mem_biUnion.mp hy with ⟨z, hz, hyz⟩ rcases Finset.mem_image.mp hz with ⟨p, _, rfl⟩ rcases Finset.mem_image.mp hyz with ⟨r, _, rfl⟩ have h1 := singletonRunCount_delPos_le_add_one (delPos x p) r have h2 := singletonRunCount_delPos_le_add_one x p omega theorem sandwichCount_del2Ball_le {q m : ℕ} (x : Word q (m + 3)) {y : Word q (m + 1)} (hy : y ∈ del2Ball x) : sandwichCount y ≤ sandwichCount x + 4 := by unfold del2Ball at hy rcases Finset.mem_biUnion.mp hy with ⟨z, hz, hyz⟩ rcases Finset.mem_image.mp hz with ⟨p, _, rfl⟩ rcases Finset.mem_image.mp hyz with ⟨r, _, rfl⟩ have h1 := sandwichCount_delPos_le_add_two (delPos x p) r have h2 := sandwichCount_delPos_le_add_two x p omega /-- Kulkarni–Kiyavash (2013) 2-deletion dual weight `w_KK(y) = 2 / (r(y) * (r(y) + 1))`. -/ noncomputable def kk2DualWeight {q m : ℕ} (y : Word q (m + 1)) : ℝ := 2 / ((runCount y : ℝ) * ((runCount y : ℝ) + 1)) /-- **Second-Order 2-Deletion Dual Weight (`w*(y)`)**: Equals `w_KK(y)` when `r(y) ≤ 9`, and subtracts the strictly positive second-order singleton+sandwich boost `4 * ((r₁(y) - 2)_+ + (a₁(y) - 4)_+) / ((r+1)(r+2)(r+3)(r+4))` whenever `r(y) ≥ 10`. -/ noncomputable def twoDelDualWeight {q m : ℕ} (y : Word q (m + 1)) : ℝ := let r : ℝ := (runCount y : ℝ) if runCount y ≤ 9 then 2 / (r * (r + 1)) else 2 / (r * (r + 1)) - 4 * (((singletonRunCount y - 2 : ℕ) : ℝ) + ((sandwichCount y - 4 : ℕ) : ℝ)) / ((r + 1) * (r + 2) * (r + 3) * (r + 4)) lemma kk2DualWeight_nonneg {q m : ℕ} (y : Word q (m + 1)) : 0 ≤ kk2DualWeight y := by unfold kk2DualWeight positivity lemma twoDelDualWeight_nonneg {q m : ℕ} (y : Word q (m + 1)) : 0 ≤ twoDelDualWeight y := by unfold twoDelDualWeight dsimp only split_ifs with h · positivity · have hr10 : (10 : ℝ) ≤ (runCount y : ℝ) := by exact_mod_cast (show 10 ≤ runCount y by omega) have hsing : ((singletonRunCount y - 2 : ℕ) : ℝ) ≤ (runCount y : ℝ) := by have h1 := singletonRunCount_le_runCount y exact_mod_cast (show singletonRunCount y - 2 ≤ runCount y by omega) have hsand : ((sandwichCount y - 4 : ℕ) : ℝ) ≤ (runCount y : ℝ) := by have h1 := (sandwichCount_le_singletonRunCount y).trans (singletonRunCount_le_runCount y) exact_mod_cast (show sandwichCount y - 4 ≤ runCount y by omega) set r : ℝ := (runCount y : ℝ) have hnum : 4 * (((singletonRunCount y - 2 : ℕ) : ℝ) + ((sandwichCount y - 4 : ℕ) : ℝ)) ≤ 8 * r := by linarith have hden_pos : 0 < (r + 1) * (r + 2) * (r + 3) * (r + 4) := by positivity have hr_pos : 0 < r * (r + 1) := by positivity have hstep : 4 * (((singletonRunCount y - 2 : ℕ) : ℝ) + ((sandwichCount y - 4 : ℕ) : ℝ)) / ((r + 1) * (r + 2) * (r + 3) * (r + 4)) ≤ 8 * r / ((r + 1) * (r + 2) * (r + 3) * (r + 4)) := div_le_div_of_nonneg_right hnum hden_pos.le have hstep2 : 8 * r / ((r + 1) * (r + 2) * (r + 3) * (r + 4)) ≤ 2 / (r * (r + 1)) := by rw [div_le_div_iff₀ hden_pos hr_pos] nlinarith linarith lemma twoDelDualWeight_le_kk2DualWeight {q m : ℕ} (y : Word q (m + 1)) : twoDelDualWeight y ≤ kk2DualWeight y := by unfold twoDelDualWeight kk2DualWeight dsimp only split_ifs with h · exact le_rfl · have hsub : 0 ≤ 4 * (((singletonRunCount y - 2 : ℕ) : ℝ) + ((sandwichCount y - 4 : ℕ) : ℝ)) / (((runCount y : ℝ) + 1) * ((runCount y : ℝ) + 2) * ((runCount y : ℝ) + 3) * ((runCount y : ℝ) + 4)) := by positivity linarith /-- Lower-envelope function `g_M(k)` depending only on `M = r₁(x) + a₁(x)` and `k = r(y)`. -/ private noncomputable def gEnv (M : ℕ) (k : ℕ) : ℝ := let kr : ℝ := (k : ℝ) if k ≤ 9 then 2 / (kr * (kr + 1)) else 2 / (kr * (kr + 1)) - 4 * (min (M : ℝ) (2 * kr - 8)) / ((kr + 1) * (kr + 2) * (kr + 3) * (kr + 4)) private lemma twoDelDualWeight_ge_gEnv {q m : ℕ} (x : Word q (m + 3)) {y : Word q (m + 1)} (hy : y ∈ del2Ball x) : gEnv (singletonRunCount x + sandwichCount x) (runCount y) ≤ twoDelDualWeight y := by unfold gEnv twoDelDualWeight dsimp only split_ifs with h · exact le_rfl · have hr10 : 10 ≤ runCount y := by omega have hsing_x := singletonRunCount_del2Ball_le x hy have hsand_x := sandwichCount_del2Ball_le x hy have hsing_y := singletonRunCount_le_runCount y have hsand_y : sandwichCount y + 2 ≤ runCount y := by rcases sandwichCount_le_runCount_sub_two y with h2 | h1 · exact h2 · omega have hbound1 : ((singletonRunCount y - 2 : ℕ) : ℝ) + ((sandwichCount y - 4 : ℕ) : ℝ) ≤ ((singletonRunCount x + sandwichCount x : ℕ) : ℝ) := by exact_mod_cast (show (singletonRunCount y - 2) + (sandwichCount y - 4) ≤ singletonRunCount x + sandwichCount x by omega) have hbound2 : ((singletonRunCount y - 2 : ℕ) : ℝ) + ((sandwichCount y - 4 : ℕ) : ℝ) ≤ 2 * (runCount y : ℝ) - 8 := by have hnat : (singletonRunCount y - 2) + (sandwichCount y - 4) + 8 ≤ 2 * runCount y := by omega have hr : (((singletonRunCount y - 2 : ℕ) : ℝ) + ((sandwichCount y - 4 : ℕ) : ℝ)) + 8 ≤ 2 * (runCount y : ℝ) := by exact_mod_cast hnat linarith have hmin : ((singletonRunCount y - 2 : ℕ) : ℝ) + ((sandwichCount y - 4 : ℕ) : ℝ) ≤ min ((singletonRunCount x + sandwichCount x : ℕ) : ℝ) (2 * (runCount y : ℝ) - 8) := le_min hbound1 hbound2 have hden : 0 < ((runCount y : ℝ) + 1) * ((runCount y : ℝ) + 2) * ((runCount y : ℝ) + 3) * ((runCount y : ℝ) + 4) := by positivity have hdiv : 4 * (((singletonRunCount y - 2 : ℕ) : ℝ) + ((sandwichCount y - 4 : ℕ) : ℝ)) / (((runCount y : ℝ) + 1) * ((runCount y : ℝ) + 2) * ((runCount y : ℝ) + 3) * ((runCount y : ℝ) + 4)) ≤ 4 * min ((singletonRunCount x + sandwichCount x : ℕ) : ℝ) (2 * (runCount y : ℝ) - 8) / (((runCount y : ℝ) + 1) * ((runCount y : ℝ) + 2) * ((runCount y : ℝ) + 3) * ((runCount y : ℝ) + 4)) := div_le_div_of_nonneg_right (by linarith) hden.le linarith /-- One-step monotonicity `gEnv M (k + 1) ≤ gEnv M k` for ALL `k ≥ 1` and ALL `M : ℕ`. -/ private lemma gEnv_step_anti (M : ℕ) (k : ℕ) (hk : 1 ≤ k) : gEnv M (k + 1) ≤ gEnv M k := by unfold gEnv dsimp only by_cases h9 : k + 1 ≤ 9 · have hk9 : k ≤ 9 := by omega rw [if_pos h9, if_pos hk9] have hk_pos : (0 : ℝ) < (k : ℝ) := by exact_mod_cast hk have h1 : 0 < ((k : ℝ) + 1) * ((k : ℝ) + 2) := by positivity have h2 : 0 < (k : ℝ) * ((k : ℝ) + 1) := by positivity push_cast rw [div_le_div_iff₀ (by linarith : 0 < ((k : ℝ) + 1) * ((k : ℝ) + 1 + 1)) h2] nlinarith · by_cases hk9 : k ≤ 9 · -- Transition `k = 9 → k + 1 = 10` have hk_eq : k = 9 := by omega subst hk_eq rw [if_neg h9, if_pos hk9] have hmin_nn : 0 ≤ min (M : ℝ) (2 * ((9 + 1 : ℕ) : ℝ) - 8) := by apply le_min · positivity · norm_num have hpen_nn : 0 ≤ 4 * min (M : ℝ) (2 * ((9 + 1 : ℕ) : ℝ) - 8) / ((((9 + 1 : ℕ) : ℝ) + 1) * (((9 + 1 : ℕ) : ℝ) + 2) * (((9 + 1 : ℕ) : ℝ) + 3) * (((9 + 1 : ℕ) : ℝ) + 4)) := by positivity have hbase : (2 : ℝ) / (((9 + 1 : ℕ) : ℝ) * (((9 + 1 : ℕ) : ℝ) + 1)) ≤ 2 / ((9 : ℝ) * ((9 : ℝ) + 1)) := by norm_num linarith · -- Both `k ≥ 10` and `k + 1 ≥ 10`: compare directly using `min(M, 2k - 8) ≤ 2k - 8`! rw [if_neg h9, if_neg hk9] have hk10 : (10 : ℝ) ≤ (k : ℝ) := by exact_mod_cast (show 10 ≤ k by omega) set kr : ℝ := (k : ℝ) have hcast : ((k + 1 : ℕ) : ℝ) = kr + 1 := by push_cast; rfl rw [hcast] set m0 : ℝ := min (M : ℝ) (2 * kr - 8) have hm0_le : m0 ≤ 2 * kr - 8 := min_le_right _ _ have hm0_nn : 0 ≤ m0 := le_min (by positivity) (by linarith) have hm1_ge : m0 ≤ min (M : ℝ) (2 * (kr + 1) - 8) := le_min (min_le_left _ _) (by linarith [min_le_right (M : ℝ) (2 * kr - 8)]) have hd1 : 0 < (kr + 2) * (kr + 3) * (kr + 4) * (kr + 5) := by positivity have hd0 : 0 < (kr + 1) * (kr + 2) * (kr + 3) * (kr + 4) := by positivity have hstep : 4 * m0 / ((kr + 2) * (kr + 3) * (kr + 4) * (kr + 5)) ≤ 4 * min (M : ℝ) (2 * (kr + 1) - 8) / ((kr + 2) * (kr + 3) * (kr + 4) * (kr + 5)) := div_le_div_of_nonneg_right (by linarith) hd1.le have hdiff : 4 * m0 / ((kr + 1) * (kr + 2) * (kr + 3) * (kr + 4)) - 4 * m0 / ((kr + 2) * (kr + 3) * (kr + 4) * (kr + 5)) ≤ 2 / (kr * (kr + 1)) - 2 / ((kr + 1) * (kr + 2)) := by have hEq1 : 4 * m0 / ((kr + 1) * (kr + 2) * (kr + 3) * (kr + 4)) - 4 * m0 / ((kr + 2) * (kr + 3) * (kr + 4) * (kr + 5)) = 16 * m0 / ((kr + 1) * (kr + 2) * (kr + 3) * (kr + 4) * (kr + 5)) := by field_simp; ring have hEq2 : 2 / (kr * (kr + 1)) - 2 / ((kr + 1) * (kr + 2)) = 4 / (kr * (kr + 1) * (kr + 2)) := by field_simp; ring rw [hEq1, hEq2, div_le_div_iff₀ (by positivity) (by positivity)] have hkr2 : 0 ≤ kr ^ 2 := sq_nonneg kr nlinarith [mul_nonneg (show 0 ≤ kr by linarith) hkr2] have hring1 : (kr + 1 + 1) * (kr + 1 + 2) * (kr + 1 + 3) * (kr + 1 + 4) = (kr + 2) * (kr + 3) * (kr + 4) * (kr + 5) := by ring have hring2 : (kr + 1) * (kr + 1 + 1) = (kr + 1) * (kr + 2) := by ring rw [hring1, hring2] linarith /-- Full monotonicity `gEnv M k₂ ≤ gEnv M k₁` for `1 ≤ k₁ ≤ k₂`. -/ private lemma gEnv_anti (M : ℕ) {k₁ k₂ : ℕ} (hk₁ : 1 ≤ k₁) (hle : k₁ ≤ k₂) : gEnv M k₂ ≤ gEnv M k₁ := by induction hle with | refl => exact le_rfl | @step m hm ih => exact (gEnv_step_anti M m (hk₁.trans hm)).trans ih /-- Kernel-verified integer table for Kulkarni–Kiyavash 7-bucket feasibility when `2 ≤ r ≤ 9`. -/ private def kk2ScaledW (k : ℕ) : ℕ := let kk := max 1 k 5040 / (kk * (kk + 1)) private def kk2SmallCheck : Bool := (List.range 10).all fun r => if r < 2 then true else (List.range (r + 1)).all fun L => (List.range (r + 1 - L)).all fun N => let S := r - L - N if L + N < 2 then true else let eS := if S = 0 then 0 else S - 1 let tLL := (L * (L + 1) / 2) * kk2ScaledW r let tLN := (L * N) * kk2ScaledW (r - 1) let tM := (L * S + N * (N - 1) / 2 + N * S - (S - eS)) * kk2ScaledW (r - 2) let tSS := (S * (S - 1) / 2 - eS) * kk2ScaledW (r - 4) 2520 ≤ tLL + tLN + tM + tSS private lemma kk2SmallCheck_eq_true : kk2SmallCheck = true := by decide private lemma kk2ScaledW_le_real (k : ℕ) (hk : k ≤ 9) : ((kk2ScaledW k : ℕ) : ℝ) ≤ 2520 * (2 / ((max 1 k : ℝ) * ((max 1 k : ℝ) + 1))) := by have hk_cases : k = 0 ∨ k = 1 ∨ k = 2 ∨ k = 3 ∨ k = 4 ∨ k = 5 ∨ k = 6 ∨ k = 7 ∨ k = 8 ∨ k = 9 := by omega rcases hk_cases with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> norm_num [kk2ScaledW] /-- **Kulkarni–Kiyavash Feasibility for `1 ≤ r ≤ 9`** (and used directly as the `r ≤ 9` branch of `twoDelDualWeight_feasible`). -/ private lemma kk2DualWeight_feasible_le_nine {q m : ℕ} (x : Word q (m + 3)) (hr9 : runCount x ≤ 9) : 1 ≤ ∑ y ∈ del2Ball x, kk2DualWeight y := by by_cases hr1 : runCount x = 1 · -- Single run of length `≥ 3`: `bucketLL` has size 1 and weight `1`! have hlong := runCount_eq_one_long x hr1 have hLL := card_bucketLL x rw [hlong.1] at hLL have hcard : (bucketLL x).card = 1 := by omega have hne : (bucketLL x).Nonempty := Finset.card_pos.mp (by omega) rcases hne with ⟨pq, hpq⟩ have hpq_nr : pq ∈ nonRedundantPairs x := (Finset.mem_filter.mp hpq).1 have hpq_lt : pq.1.val < pq.2.val := (Finset.mem_filter.mp hpq_nr).2.1 have hy_mem : del2 x pq.1 pq.2 ∈ del2Ball x := del2_mem_del2Ball x pq.1 pq.2 hpq_lt have hry1 : runCount (del2 x pq.1 pq.2) = 1 := by have hle := runCount_del2_le x pq.1 pq.2 hpq_lt have hge := one_le_runCount (del2 x pq.1 pq.2) omega have hw1 : kk2DualWeight (del2 x pq.1 pq.2) = 1 := by unfold kk2DualWeight; rw [hry1]; norm_num calc (1 : ℝ) = kk2DualWeight (del2 x pq.1 pq.2) := hw1.symm _ ≤ ∑ y ∈ del2Ball x, kk2DualWeight y := Finset.single_le_sum (fun y _ => kk2DualWeight_nonneg y) hy_mem · -- `2 ≤ runCount x ≤ 9`: use the 7-bucket sum merged into 4 tiers + `kk2SmallCheck`! have hr2 : 2 ≤ runCount x := by have := one_le_runCount x; omega have h7 := sum_del2Ball_ge_seven_buckets x kk2DualWeight kk2DualWeight_nonneg set r := runCount x set L := (longRunStarts x).card set N := (nonSandSingletonStarts x).card set S := (sandwichPosSet x).card set eL := (rightInLong x).card set eN := (rightInNonSand x).card set eS := (rightInSand x).card set fN := (leftInNonSand x).card have hpart : L + N + S = r := card_run_partition x have hLN : 2 ≤ L + N := two_le_long_add_nonSand x hr2 have hrp : eL + eN + eS = S := card_right_partition x have hLL : 2 * (bucketLL x).card = L * (L + 1) := card_bucketLL x have hLN_c : (bucketLN x).card = L * N := card_bucketLN x have hLS_c : (bucketLS x).card + eL = L * S := card_bucketLS x have hNN_c : 2 * (bucketNN x).card = N * (N - 1) := card_bucketNN x have hNSa_c : (bucketNSAdj x).card = fN := card_bucketNSAdj x have hNSn_c : (bucketNSNon x).card + eN + fN = N * S := card_bucketNSNon x have hSS_c : 2 * ((bucketSS x).card + eS) = S * (S - 1) := card_bucketSS x let w : ℕ → ℝ := fun k => 2 / ((max 1 k : ℝ) * ((max 1 k : ℝ) + 1)) have hw_anti : ∀ a b : ℕ, a ≤ b → w b ≤ w a := by intro a b hab dsimp only [w] have h1 : (1 : ℝ) ≤ (max 1 a : ℝ) := by exact_mod_cast le_max_left 1 a have h2 : (max 1 a : ℝ) ≤ (max 1 b : ℝ) := by exact_mod_cast max_le_max_left 1 hab rw [div_le_div_iff₀ (by positivity) (by positivity)] nlinarith have hw_eq : ∀ y : Word q (m + 1), kk2DualWeight y = w (runCount y) := by intro y unfold kk2DualWeight; dsimp only [w] have h1 : max 1 (runCount y) = runCount y := max_eq_right (one_le_runCount y) rw [show (max (1 : ℝ) (runCount y : ℝ)) = (runCount y : ℝ) from by exact_mod_cast h1] -- Bound each of the 7 buckets by `w (r - d)` have hB_LL : ((bucketLL x).card : ℝ) * w r ≤ ∑ pq ∈ bucketLL x, kk2DualWeight (del2 x pq.1 pq.2) := by rw [← nsmul_eq_mul, ← Finset.sum_const] refine Finset.sum_le_sum fun pq hpq => ?_ have hlt : pq.1.val < pq.2.val := (Finset.mem_filter.mp (Finset.mem_filter.mp hpq).1).2.1 rw [hw_eq]; exact hw_anti _ _ (runCount_del2_le x pq.1 pq.2 hlt) have hB_LN : ((bucketLN x).card : ℝ) * w (r - 1) ≤ ∑ pq ∈ bucketLN x, kk2DualWeight (del2 x pq.1 pq.2) := by rw [← nsmul_eq_mul, ← Finset.sum_const] refine Finset.sum_le_sum fun pq hpq => ?_ have hlt : pq.1.val < pq.2.val := (Finset.mem_filter.mp (Finset.mem_filter.mp hpq).1).2.1 rw [hw_eq] rcases (Finset.mem_filter.mp hpq).2 with ⟨_, hN⟩ | ⟨hN, _⟩ · exact hw_anti _ _ (by have := runCount_del2_le_sub_one_right x pq.1 pq.2 hlt (Finset.mem_sdiff.mp hN).1 hr2; omega) · exact hw_anti _ _ (by have := runCount_del2_le_sub_one_left x pq.1 pq.2 hlt (Finset.mem_sdiff.mp hN).1 hr2; omega) have hB_LS : ((bucketLS x).card : ℝ) * w (r - 2) ≤ ∑ pq ∈ bucketLS x, kk2DualWeight (del2 x pq.1 pq.2) := by rw [← nsmul_eq_mul, ← Finset.sum_const] refine Finset.sum_le_sum fun pq hpq => ?_ have hlt : pq.1.val < pq.2.val := (Finset.mem_filter.mp (Finset.mem_filter.mp hpq).1).2.1 rw [hw_eq] rcases (Finset.mem_filter.mp hpq).2 with ⟨_, hS⟩ | ⟨hS, _⟩ · exact hw_anti _ _ (by have := runCount_del2_le_sub_two_sand_right x pq.1 pq.2 hlt hS; omega) · exact hw_anti _ _ (by have := runCount_del2_le_sub_two_sand_left x pq.1 pq.2 hlt hS; omega) have hB_NN : ((bucketNN x).card : ℝ) * w (r - 2) ≤ ∑ pq ∈ bucketNN x, kk2DualWeight (del2 x pq.1 pq.2) := by rw [← nsmul_eq_mul, ← Finset.sum_const] refine Finset.sum_le_sum fun pq hpq => ?_ have hlt : pq.1.val < pq.2.val := (Finset.mem_filter.mp (Finset.mem_filter.mp hpq).1).2.1 have h12 := (Finset.mem_filter.mp hpq).2 rw [hw_eq] by_cases hr3 : 3 ≤ r · exact hw_anti _ _ (by have := runCount_del2_le_sub_two_NN x pq.1 pq.2 hlt h12.1 h12.2 hr3; omega) · have hr_eq2 : r = 2 := by omega have hry1 : runCount (del2 x pq.1 pq.2) = 1 := by have h1 := runCount_del2_le_sub_one_left x pq.1 pq.2 hlt (Finset.mem_sdiff.mp h12.1).1 hr2 have h0 := one_le_runCount (del2 x pq.1 pq.2) omega rw [hry1, hr_eq2] simp [w] have hB_NSa : ((bucketNSAdj x).card : ℝ) * w (r - 2) ≤ ∑ pq ∈ bucketNSAdj x, kk2DualWeight (del2 x pq.1 pq.2) := by rw [← nsmul_eq_mul, ← Finset.sum_const] refine Finset.sum_le_sum fun pq hpq => ?_ have hlt : pq.1.val < pq.2.val := (Finset.mem_filter.mp (Finset.mem_filter.mp hpq).1).2.1 have hS := (Finset.mem_filter.mp hpq).2.2.1 rw [hw_eq] exact hw_anti _ _ (by have := runCount_del2_le_sub_two_sand_right x pq.1 pq.2 hlt hS; omega) have hB_NSn : ((bucketNSNon x).card : ℝ) * w (r - 2) ≤ ∑ pq ∈ bucketNSNon x, kk2DualWeight (del2 x pq.1 pq.2) := by rw [← nsmul_eq_mul, ← Finset.sum_const] refine Finset.sum_le_sum fun pq hpq => ?_ have hlt : pq.1.val < pq.2.val := (Finset.mem_filter.mp (Finset.mem_filter.mp hpq).1).2.1 rw [hw_eq] rcases (Finset.mem_filter.mp hpq).2 with ⟨_, hS, _⟩ | ⟨hS, _⟩ · exact hw_anti _ _ (by have := runCount_del2_le_sub_two_sand_right x pq.1 pq.2 hlt hS; omega) · exact hw_anti _ _ (by have := runCount_del2_le_sub_two_sand_left x pq.1 pq.2 hlt hS; omega) have hB_SS : ((bucketSS x).card : ℝ) * w (r - 4) ≤ ∑ pq ∈ bucketSS x, kk2DualWeight (del2 x pq.1 pq.2) := by rw [← nsmul_eq_mul, ← Finset.sum_const] refine Finset.sum_le_sum fun pq hpq => ?_ have hpq_nr := (Finset.mem_filter.mp hpq).1 have hlt : pq.1.val < pq.2.val := (Finset.mem_filter.mp hpq_nr).2.1 have h12 := (Finset.mem_filter.mp hpq).2 have hnot_adj : pq.2.val ≠ pq.1.val + 1 := by rcases (Finset.mem_filter.mp hpq_nr).2.2 with ⟨hL, _⟩ | ⟨_, _, hnot⟩ · exact False.elim ((Finset.mem_sdiff.mp hL).2 (sandwichPosSet_subset_singletonPosSet x h12.1)) · intro hadj; exact hnot ⟨h12.1, hadj⟩ rw [hw_eq] exact hw_anti _ _ (by have := runCount_del2_le_sub_four_SS x pq.1 pq.2 (by omega) h12.1 h12.2; omega) -- Merge `LS + NN + NSAdj + NSNon` and replace `eS` by `eS_max = if S = 0 then 0 else S - 1` let eS_max : ℕ := if S = 0 then 0 else S - 1 have heS_le : eS ≤ eS_max := by dsimp only [eS_max] split_ifs with hS0 · omega · have := card_rightInSand_lt x (by omega) omega have hLL_div : (bucketLL x).card = L * (L + 1) / 2 := by omega have hNN_div : (bucketNN x).card = N * (N - 1) / 2 := by omega have hSS_div : (bucketSS x).card + eS = S * (S - 1) / 2 := by omega have hM_sum : (bucketLS x).card + (bucketNN x).card + (bucketNSAdj x).card + (bucketNSNon x).card = L * S + N * (N - 1) / 2 + N * S - (S - eS) := by omega have hMid_ge : (L * S + N * (N - 1) / 2 + N * S - (S - eS) : ℕ) + (eS_max - eS) = L * S + N * (N - 1) / 2 + N * S - (S - eS_max) := by set Base := L * S + N * (N - 1) / 2 + N * S have hBase : S - eS ≤ Base := by have h1' : eL ≤ L * S := by omega have h2' : eN ≤ N * S := by omega have h3' : L * S + N * S ≤ Base := by dsimp only [Base]; omega omega have heSm_le_S : eS_max ≤ S := by dsimp only [eS_max]; split_ifs <;> omega omega have hSS_eq : (bucketSS x).card = (S * (S - 1) / 2 - eS_max) + (eS_max - eS) := by by_cases hS2 : S ≤ 1 · have heS0 : eS = 0 := by rcases (show S = 0 ∨ S = 1 by omega) with h0 | h1 · omega · have := card_rightInSand_lt x (by omega); omega have heSm0 : eS_max = 0 := by dsimp only [eS_max]; split_ifs <;> omega have hS0 : S * (S - 1) / 2 = 0 := by rcases (show S = 0 ∨ S = 1 by omega) with h | h <;> simp [h] omega · have hS_ge2 : eS_max ≤ S * (S - 1) / 2 := by dsimp only [eS_max] rw [if_neg (by omega)] have : 2 * (S - 1) ≤ S * (S - 1) := Nat.mul_le_mul_right (S - 1) (by omega) omega omega have h_tier_ge : ((L * (L + 1) / 2 : ℕ) : ℝ) * w r + ((L * N : ℕ) : ℝ) * w (r - 1) + ((L * S + N * (N - 1) / 2 + N * S - (S - eS_max) : ℕ) : ℝ) * w (r - 2) + ((S * (S - 1) / 2 - eS_max : ℕ) : ℝ) * w (r - 4) ≤ ∑ y ∈ del2Ball x, kk2DualWeight y := by have hw42 : w (r - 2) ≤ w (r - 4) := hw_anti _ _ (by omega) have hM_real : (((bucketLS x).card + (bucketNN x).card + (bucketNSAdj x).card + (bucketNSNon x).card : ℕ) : ℝ) + ((eS_max - eS : ℕ) : ℝ) = ((L * S + N * (N - 1) / 2 + N * S - (S - eS_max) : ℕ) : ℝ) := by exact_mod_cast (by rw [hM_sum]; exact hMid_ge : ((bucketLS x).card + (bucketNN x).card + (bucketNSAdj x).card + (bucketNSNon x).card) + (eS_max - eS) = L * S + N * (N - 1) / 2 + N * S - (S - eS_max)) have hSS_real : ((bucketSS x).card : ℝ) = ((S * (S - 1) / 2 - eS_max : ℕ) : ℝ) + ((eS_max - eS : ℕ) : ℝ) := by exact_mod_cast hSS_eq have hdiff_nn : 0 ≤ ((eS_max - eS : ℕ) : ℝ) * (w (r - 4) - w (r - 2)) := mul_nonneg (Nat.cast_nonneg _) (by linarith) have hLL_r : ((bucketLL x).card : ℝ) = ((L * (L + 1) / 2 : ℕ) : ℝ) := by exact_mod_cast hLL_div have hLN_r : ((bucketLN x).card : ℝ) = ((L * N : ℕ) : ℝ) := by exact_mod_cast hLN_c have hM_cast : (((bucketLS x).card + (bucketNN x).card + (bucketNSAdj x).card + (bucketNSNon x).card : ℕ) : ℝ) = ((bucketLS x).card : ℝ) + ((bucketNN x).card : ℝ) + ((bucketNSAdj x).card : ℝ) + ((bucketNSNon x).card : ℝ) := by push_cast; rfl rw [hM_cast] at hM_real rw [hLL_r] at hB_LL rw [hLN_r] at hB_LN rw [hSS_real] at hB_SS have hM_factor : ((bucketLS x).card : ℝ) * w (r - 2) + ((bucketNN x).card : ℝ) * w (r - 2) + ((bucketNSAdj x).card : ℝ) * w (r - 2) + ((bucketNSNon x).card : ℝ) * w (r - 2) = (((L * S + N * (N - 1) / 2 + N * S - (S - eS_max) : ℕ) : ℝ) - ((eS_max - eS : ℕ) : ℝ)) * w (r - 2) := by have hsub : ((bucketLS x).card : ℝ) + ((bucketNN x).card : ℝ) + ((bucketNSAdj x).card : ℝ) + ((bucketNSNon x).card : ℝ) = ((L * S + N * (N - 1) / 2 + N * S - (S - eS_max) : ℕ) : ℝ) - ((eS_max - eS : ℕ) : ℝ) := by linarith only [hM_real] rw [← hsub]; ring have hSS_exp : (((S * (S - 1) / 2 - eS_max : ℕ) : ℝ) + ((eS_max - eS : ℕ) : ℝ)) * w (r - 4) = ((S * (S - 1) / 2 - eS_max : ℕ) : ℝ) * w (r - 4) + ((eS_max - eS : ℕ) : ℝ) * w (r - 4) := by ring have hM_exp : (((L * S + N * (N - 1) / 2 + N * S - (S - eS_max) : ℕ) : ℝ) - ((eS_max - eS : ℕ) : ℝ)) * w (r - 2) = ((L * S + N * (N - 1) / 2 + N * S - (S - eS_max) : ℕ) : ℝ) * w (r - 2) - ((eS_max - eS : ℕ) : ℝ) * w (r - 2) := by ring have hdiff_exp : ((eS_max - eS : ℕ) : ℝ) * (w (r - 4) - w (r - 2)) = ((eS_max - eS : ℕ) : ℝ) * w (r - 4) - ((eS_max - eS : ℕ) : ℝ) * w (r - 2) := by ring linarith only [h7, hB_LL, hB_LN, hB_LS, hB_NN, hB_NSa, hB_NSn, hB_SS, hM_factor, hSS_exp, hM_exp, hdiff_nn, hdiff_exp] -- Apply `kk2SmallCheck_eq_true`! have hchk := kk2SmallCheck_eq_true simp only [kk2SmallCheck, List.all_eq_true, List.mem_range] at hchk have hr_in := hchk r (by omega) rw [if_neg (by omega)] at hr_in simp only [List.all_eq_true, List.mem_range] at hr_in have hL_in := hr_in L (by omega) have hN_in := hL_in N (by omega) rw [if_neg (by omega)] at hN_in have hS_eq : r - L - N = S := by omega rw [hS_eq] at hN_in have hnat : 2520 ≤ (L * (L + 1) / 2) * kk2ScaledW r + (L * N) * kk2ScaledW (r - 1) + (L * S + N * (N - 1) / 2 + N * S - (S - eS_max)) * kk2ScaledW (r - 2) + (S * (S - 1) / 2 - eS_max) * kk2ScaledW (r - 4) := by simpa [eS_max] using hN_in have hreal : (2520 : ℝ) ≤ ((L * (L + 1) / 2 : ℕ) : ℝ) * ((kk2ScaledW r : ℕ) : ℝ) + ((L * N : ℕ) : ℝ) * ((kk2ScaledW (r - 1) : ℕ) : ℝ) + ((L * S + N * (N - 1) / 2 + N * S - (S - eS_max) : ℕ) : ℝ) * ((kk2ScaledW (r - 2) : ℕ) : ℝ) + ((S * (S - 1) / 2 - eS_max : ℕ) : ℝ) * ((kk2ScaledW (r - 4) : ℕ) : ℝ) := by exact_mod_cast hnat have hsc0 := kk2ScaledW_le_real r (by omega) have hsc1 := kk2ScaledW_le_real (r - 1) (by omega) have hsc2 := kk2ScaledW_le_real (r - 2) (by omega) have hsc4 := kk2ScaledW_le_real (r - 4) (by omega) have hcomb : (2520 : ℝ) ≤ 2520 * ( ((L * (L + 1) / 2 : ℕ) : ℝ) * w r + ((L * N : ℕ) : ℝ) * w (r - 1) + ((L * S + N * (N - 1) / 2 + N * S - (S - eS_max) : ℕ) : ℝ) * w (r - 2) + ((S * (S - 1) / 2 - eS_max : ℕ) : ℝ) * w (r - 4)) := by have t0 := mul_le_mul_of_nonneg_left hsc0 (Nat.cast_nonneg (L * (L + 1) / 2)) have t1 := mul_le_mul_of_nonneg_left hsc1 (Nat.cast_nonneg (L * N)) have t2 := mul_le_mul_of_nonneg_left hsc2 (Nat.cast_nonneg (L * S + N * (N - 1) / 2 + N * S - (S - eS_max))) have t4 := mul_le_mul_of_nonneg_left hsc4 (Nat.cast_nonneg (S * (S - 1) / 2 - eS_max)) dsimp only [w] linarith linarith /-! ### Polynomial Certificates for Second-Order Dual Feasibility (`r ≥ 10`) -/ private def polyD (L2 N1 S1 : ℝ) : ℝ := let r := L2 + N1 + S1 (r - 3) * (r - 2) * (r - 1) * r * (r + 1) * (r + 2) private def polyB0 (L2 N1 S1 : ℝ) : ℝ := let r := L2 + N1 + S1; let M := N1 + 2 * S1 (r - 3) * (r - 2) * (r - 1) * r * ((r + 1) * (r + 2) - 2 * M) private def polyB1 (L2 N1 S1 : ℝ) : ℝ := let r := L2 + N1 + S1; let M := N1 + 2 * S1 (r - 3) * (r - 2) * (r + 2) * ((r - 1) * r * (r + 1) + 2 * r * (r + 1) - 2 * M * (r - 1)) private def polyB2 (L2 N1 S1 : ℝ) : ℝ := let r := L2 + N1 + S1; let M := N1 + 2 * S1 (r - 3) * (r + 1) * (r + 2) * ((r - 2) * (r - 1) * r + 4 * (r - 2) * r + 6 * r - 2 * M * (r - 2)) private def polyB3 (L2 N1 S1 : ℝ) : ℝ := let r := L2 + N1 + S1; let M := N1 + 2 * S1 (r - 3) * r * (r + 1) * (r + 2) * ((r - 2) * (r - 1) + 6 * (r - 2) + 18 - 2 * M) private def polyB4 (L2 N1 S1 : ℝ) : ℝ := let r := L2 + N1 + S1; let M := N1 + 2 * S1 r * (r + 1) * (r + 2) * ((r - 3) * ((r - 2) * (r - 1) + 8 * (r - 2) + 36) - 2 * (r + 1) * M) private def polyBase (L2 N1 S1 : ℝ) : ℝ := L2 * (L2 + 1) * polyB0 L2 N1 S1 + 2 * L2 * N1 * polyB1 L2 N1 S1 + (2 * L2 * S1 + N1 * (N1 - 1)) * polyB2 L2 N1 S1 + 2 * N1 * S1 * polyB3 L2 N1 S1 + S1 * (S1 - 1) * polyB4 L2 N1 S1 private def polySlack1 (L2 N1 S1 : ℝ) : ℝ := let r := L2 + N1 + S1 polyBase L2 N1 S1 - r * (r + 1) * polyD L2 N1 S1 private def polySlack2 (L2 N1 S1 : ℝ) : ℝ := let r := L2 + N1 + S1 polyBase L2 N1 S1 - 2 * (polyB2 L2 N1 S1 + (S1 - 1) * polyB4 L2 N1 S1) - r * (r + 1) * polyD L2 N1 S1 private def polySlack3a (L2 N1 S1 : ℝ) : ℝ := let r := L2 + N1 + S1 polyBase L2 N1 S1 - 2 * (polyB3 L2 N1 S1 + (S1 - 1) * polyB4 L2 N1 S1 + N1 * (polyB3 L2 N1 S1 - polyB2 L2 N1 S1)) - r * (r + 1) * polyD L2 N1 S1 private def polySlack3b (L2 N1 S1 : ℝ) : ℝ := let r := L2 + N1 + S1 polyBase L2 N1 S1 - 2 * (polyB3 L2 N1 S1 + (S1 - 1) * polyB4 L2 N1 S1 + S1 * (polyB3 L2 N1 S1 - polyB2 L2 N1 S1)) - r * (r + 1) * polyD L2 N1 S1 private lemma cert_c1a (a : ℝ) (b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ polySlack1 (a + 5) (b) (0) := by have h : polySlack1 (a + 5) (b) (0) = 360 * b + 3936 * b ^ 2 + 3586 * b ^ 3 + 1098 * b ^ 4 + 134 * b ^ 5 + 6 * b ^ 6 + 1632 * a * b + 5120 * a * b ^ 2 + 2756 * a * b ^ 3 + 488 * a * b ^ 4 + 28 * a * b ^ 5 + 1462 * a ^ 2 * b + 2158 * a ^ 2 * b ^ 2 + 648 * a ^ 2 * b ^ 3 + 52 * a ^ 2 * b ^ 4 + 500 * a ^ 3 * b + 368 * a ^ 3 * b ^ 2 + 48 * a ^ 3 * b ^ 3 + 74 * a ^ 4 * b + 22 * a ^ 4 * b ^ 2 + 4 * a ^ 5 * b := by unfold polySlack1 polyBase polyB0 polyB1 polyB2 polyB3 polyB4 polyD ring rw [h]; positivity private lemma cert_c1b (a : ℝ) (b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ polySlack1 (a) (b + 5) (0) := by have h : polySlack1 (a) (b + 5) (0) = 50400 + 73560 * b + 42576 * b ^ 2 + 12666 * b ^ 3 + 2058 * b ^ 4 + 174 * b ^ 5 + 6 * b ^ 6 + 55560 * a + 63612 * a * b + 28480 * a * b ^ 2 + 6236 * a * b ^ 3 + 668 * a * b ^ 4 + 28 * a * b ^ 5 + 22260 * a ^ 2 + 20042 * a ^ 2 * b + 6658 * a ^ 2 * b ^ 2 + 968 * a ^ 2 * b ^ 3 + 52 * a ^ 2 * b ^ 4 + 4300 * a ^ 3 + 2900 * a ^ 3 * b + 648 * a ^ 3 * b ^ 2 + 48 * a ^ 3 * b ^ 3 + 420 * a ^ 4 + 194 * a ^ 4 * b + 22 * a ^ 4 * b ^ 2 + 20 * a ^ 5 + 4 * a ^ 5 * b := by unfold polySlack1 polyBase polyB0 polyB1 polyB2 polyB3 polyB4 polyD ring rw [h]; positivity private lemma cert_c2a (a : ℝ) (b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ polySlack2 (a + 5) (0) (b + 1) := by have h : polySlack2 (a + 5) (0) (b + 1) = 32064 + 1328 * b + 9688 * b ^ 2 + 6272 * b ^ 3 + 1228 * b ^ 4 + 104 * b ^ 5 + 4 * b ^ 6 + 34064 * a + 1400 * a * b + 10364 * a * b ^ 2 + 4644 * a * b ^ 3 + 620 * a * b ^ 4 + 28 * a * b ^ 5 + 14176 * a ^ 2 + 1528 * a ^ 2 * b + 3676 * a ^ 2 * b ^ 2 + 984 * a ^ 2 * b ^ 3 + 64 * a ^ 2 * b ^ 4 + 2908 * a ^ 3 + 556 * a ^ 3 * b + 536 * a ^ 3 * b ^ 2 + 64 * a ^ 3 * b ^ 3 + 296 * a ^ 4 + 80 * a ^ 4 * b + 28 * a ^ 4 * b ^ 2 + 12 * a ^ 5 + 4 * a ^ 5 * b := by unfold polySlack2 polyBase polyB0 polyB1 polyB2 polyB3 polyB4 polyD ring rw [h]; positivity private lemma cert_c2b (a : ℝ) (b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ polySlack2 (a + 2) (0) (b + 6) := by have h : polySlack2 (a + 2) (0) (b + 6) = 17280 + 65736 * b + 45596 * b ^ 2 + 13348 * b ^ 3 + 1944 * b ^ 4 + 140 * b ^ 5 + 4 * b ^ 6 + 288936 * a + 238384 * a * b + 77176 * a * b ^ 2 + 12188 * a * b ^ 3 + 936 * a * b ^ 4 + 28 * a * b ^ 5 + 99668 * a ^ 2 + 66004 * a ^ 2 * b + 16084 * a ^ 2 * b ^ 2 + 1688 * a ^ 2 * b ^ 3 + 64 * a ^ 2 * b ^ 4 + 13216 * a ^ 3 + 6756 * a ^ 3 * b + 1160 * a ^ 3 * b ^ 2 + 64 * a ^ 3 * b ^ 3 + 916 * a ^ 4 + 300 * a ^ 4 * b + 28 * a ^ 4 * b ^ 2 + 32 * a ^ 5 + 4 * a ^ 5 * b := by unfold polySlack2 polyBase polyB0 polyB1 polyB2 polyB3 polyB4 polyD ring rw [h]; positivity private lemma cert_c3a_1 (a : ℝ) (b : ℝ) (c : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) (hc : 0 ≤ c) : 0 ≤ polySlack3a (a) (c + 4) (c + 4 + b) := by have h : polySlack3a (a) (c + 4) (c + 4 + b) = 492480 + 907056 * c + 685712 * c ^ 2 + 271680 * c ^ 3 + 59456 * c ^ 4 + 6816 * c ^ 5 + 320 * c ^ 6 + 411048 * b + 650676 * b * c + 401112 * b * c ^ 2 + 120372 * b * c ^ 3 + 17616 * b * c ^ 4 + 1008 * b * c ^ 5 + 139780 * b ^ 2 + 184906 * b ^ 2 * c + 87290 * b ^ 2 * c ^ 2 + 17600 * b ^ 2 * c ^ 3 + 1288 * b ^ 2 * c ^ 4 + 25640 * b ^ 3 + 26440 * b ^ 3 * c + 8436 * b ^ 3 * c ^ 2 + 852 * b ^ 3 * c ^ 3 + 2728 * b ^ 4 + 1914 * b ^ 4 * c + 306 * b ^ 4 * c ^ 2 + 160 * b ^ 5 + 56 * b ^ 5 * c + 4 * b ^ 6 + 353448 * a + 534452 * a * c + 317800 * a * c ^ 2 + 92852 * a * c ^ 3 + 13328 * a * c ^ 4 + 752 * a * c ^ 5 + 284688 * a * b + 342092 * a * b * c + 151244 * a * b * c ^ 2 + 29136 * a * b * c ^ 3 + 2064 * a * b * c ^ 4 + 88148 * a * b ^ 2 + 79196 * a * b ^ 2 * c + 23148 * a * b ^ 2 * c ^ 2 + 2204 * a * b ^ 2 * c ^ 3 + 13244 * a * b ^ 3 + 7900 * a * b ^ 3 * c + 1144 * a * b ^ 3 * c ^ 2 + 972 * a * b ^ 4 + 288 * a * b ^ 4 * c + 28 * a * b ^ 5 + 80588 * a ^ 2 + 101362 * a ^ 2 * c + 46530 * a ^ 2 * c ^ 2 + 9280 * a ^ 2 * c ^ 3 + 680 * a ^ 2 * c ^ 4 + 59584 * a ^ 2 * b + 54456 * a ^ 2 * b * c + 16236 * a ^ 2 * b * c ^ 2 + 1580 * a ^ 2 * b * c ^ 3 + 15412 * a ^ 2 * b ^ 2 + 9156 * a ^ 2 * b ^ 2 * c + 1332 * a ^ 2 * b ^ 2 * c ^ 2 + 1664 * a ^ 2 * b ^ 3 + 484 * a ^ 2 * b ^ 3 * c + 64 * a ^ 2 * b ^ 4 + 8116 * a ^ 3 + 8452 * a ^ 3 * c + 2772 * a ^ 3 * c ^ 2 + 292 * a ^ 3 * c ^ 3 + 5380 * a ^ 3 * b + 3516 * a ^ 3 * b * c + 552 * a ^ 3 * b * c ^ 2 + 1072 * a ^ 3 * b ^ 2 + 332 * a ^ 3 * b ^ 2 * c + 64 * a ^ 3 * b ^ 3 + 484 * a ^ 4 + 346 * a ^ 4 * c + 58 * a ^ 4 * c ^ 2 + 240 * a ^ 4 * b + 84 * a ^ 4 * b * c + 28 * a ^ 4 * b ^ 2 + 20 * a ^ 5 + 4 * a ^ 5 * c + 4 * a ^ 5 * b := by unfold polySlack3a polyBase polyB0 polyB1 polyB2 polyB3 polyB4 polyD ring rw [h]; positivity private lemma cert_c3a_2a (a : ℝ) (b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ polySlack3a (a) (3) (b + 4) := by have h : polySlack3a (a) (3) (b + 4) = 144000 + 134800 * b + 54024 * b ^ 2 + 12384 * b ^ 3 + 1700 * b ^ 4 + 128 * b ^ 5 + 4 * b ^ 6 + 160384 * a + 148900 * a * b + 53744 * a * b ^ 2 + 9504 * a * b ^ 3 + 824 * a * b ^ 4 + 28 * a * b ^ 5 + 45772 * a ^ 2 + 38756 * a ^ 2 * b + 11512 * a ^ 2 * b ^ 2 + 1436 * a ^ 2 * b ^ 3 + 64 * a ^ 2 * b ^ 4 + 5364 * a ^ 3 + 4088 * a ^ 3 * b + 932 * a ^ 3 * b ^ 2 + 64 * a ^ 3 * b ^ 3 + 380 * a ^ 4 + 212 * a ^ 4 * b + 28 * a ^ 4 * b ^ 2 + 20 * a ^ 5 + 4 * a ^ 5 * b := by unfold polySlack3a polyBase polyB0 polyB1 polyB2 polyB3 polyB4 polyD ring rw [h]; positivity private lemma cert_c3a_2b (a : ℝ) (ha : 0 ≤ a) : 0 ≤ polySlack3a (a + 4) (3) (3) := by have h : polySlack3a (a + 4) (3) (3) = 756768 + 367336 * a + 71940 * a ^ 2 + 7840 * a ^ 3 + 516 * a ^ 4 + 16 * a ^ 5 := by unfold polySlack3a polyBase polyB0 polyB1 polyB2 polyB3 polyB4 polyD ring rw [h]; positivity private lemma cert_c3a_3a (a : ℝ) (b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ polySlack3a (a) (2) (b + 5) := by have h : polySlack3a (a) (2) (b + 5) = 10368 + 24624 * b + 20644 * b ^ 2 + 7656 * b ^ 3 + 1384 * b ^ 4 + 120 * b ^ 5 + 4 * b ^ 6 + 154224 * a + 142360 * a * b + 51840 * a * b ^ 2 + 9292 * a * b ^ 3 + 816 * a * b ^ 4 + 28 * a * b ^ 5 + 55380 * a ^ 2 + 43248 * a ^ 2 * b + 12148 * a ^ 2 * b ^ 2 + 1464 * a ^ 2 * b ^ 3 + 64 * a ^ 2 * b ^ 4 + 7032 * a ^ 3 + 4732 * a ^ 3 * b + 984 * a ^ 3 * b ^ 2 + 64 * a ^ 3 * b ^ 3 + 492 * a ^ 4 + 240 * a ^ 4 * b + 28 * a ^ 4 * b ^ 2 + 24 * a ^ 5 + 4 * a ^ 5 * b := by unfold polySlack3a polyBase polyB0 polyB1 polyB2 polyB3 polyB4 polyD ring rw [h]; positivity private lemma cert_c3a_3b (a : ℝ) (b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ polySlack3a (a + 4) (2) (b + 2) := by have h : polySlack3a (a + 4) (2) (b + 2) = 36000 + 128160 * b + 89928 * b ^ 2 + 23000 * b ^ 3 + 2732 * b ^ 4 + 160 * b ^ 5 + 4 * b ^ 6 + 26880 * a + 79464 * a * b + 50892 * a * b ^ 2 + 10660 * a * b ^ 3 + 908 * a * b ^ 4 + 28 * a * b ^ 5 + 10176 * a ^ 2 + 19120 * a ^ 2 * b + 10012 * a ^ 2 * b ^ 2 + 1464 * a ^ 2 * b ^ 3 + 64 * a ^ 2 * b ^ 4 + 2268 * a ^ 3 + 2348 * a ^ 3 * b + 856 * a ^ 3 * b ^ 2 + 64 * a ^ 3 * b ^ 3 + 264 * a ^ 4 + 152 * a ^ 4 * b + 28 * a ^ 4 * b ^ 2 + 12 * a ^ 5 + 4 * a ^ 5 * b := by unfold polySlack3a polyBase polyB0 polyB1 polyB2 polyB3 polyB4 polyD ring rw [h]; positivity private lemma cert_c3a_4a1 (a : ℝ) (b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ polySlack3a (a + 1) (1) (b + 8) := by have h : polySlack3a (a + 1) (1) (b + 8) = 340032 + 417672 * b + 172484 * b ^ 2 + 34132 * b ^ 3 + 3552 * b ^ 4 + 188 * b ^ 5 + 4 * b ^ 6 + 1164504 * a + 720048 * a * b + 174588 * a * b ^ 2 + 20784 * a * b ^ 3 + 1216 * a * b ^ 4 + 28 * a * b ^ 5 + 300796 * a ^ 2 + 150836 * a ^ 2 * b + 27628 * a ^ 2 * b ^ 2 + 2196 * a ^ 2 * b ^ 3 + 64 * a ^ 2 * b ^ 4 + 29484 * a ^ 3 + 11832 * a ^ 3 * b + 1532 * a ^ 3 * b ^ 2 + 64 * a ^ 3 * b ^ 3 + 1436 * a ^ 4 + 400 * a ^ 4 * b + 28 * a ^ 4 * b ^ 2 + 36 * a ^ 5 + 4 * a ^ 5 * b := by unfold polySlack3a polyBase polyB0 polyB1 polyB2 polyB3 polyB4 polyD ring rw [h]; positivity private lemma cert_c3a_4a2 (a : ℝ) (b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ polySlack3a (a + 2) (1) (b + 7) := by have h : polySlack3a (a + 2) (1) (b + 7) = 859200 + 701528 * b + 231624 * b ^ 2 + 39928 * b ^ 3 + 3812 * b ^ 4 + 192 * b ^ 5 + 4 * b ^ 6 + 1012136 * a + 660600 * a * b + 166232 * a * b ^ 2 + 20272 * a * b ^ 3 + 1204 * a * b ^ 4 + 28 * a * b ^ 5 + 239520 * a ^ 2 + 130896 * a ^ 2 * b + 25612 * a ^ 2 * b ^ 2 + 2132 * a ^ 2 * b ^ 3 + 64 * a ^ 2 * b ^ 4 + 23696 * a ^ 3 + 10376 * a ^ 3 * b + 1452 * a ^ 3 * b ^ 2 + 64 * a ^ 3 * b ^ 3 + 1224 * a ^ 4 + 364 * a ^ 4 * b + 28 * a ^ 4 * b ^ 2 + 32 * a ^ 5 + 4 * a ^ 5 * b := by unfold polySlack3a polyBase polyB0 polyB1 polyB2 polyB3 polyB4 polyD ring rw [h]; positivity private lemma cert_c3a_4a3 (a : ℝ) (b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ polySlack3a (a + 3) (1) (b + 6) := by have h : polySlack3a (a + 3) (1) (b + 6) = 999456 + 821816 * b + 266100 * b ^ 2 + 44196 * b ^ 3 + 4040 * b ^ 4 + 196 * b ^ 5 + 4 * b ^ 6 + 810824 * a + 580144 * a * b + 155452 * a * b ^ 2 + 19680 * a * b ^ 3 + 1192 * a * b ^ 4 + 28 * a * b ^ 5 + 181900 * a ^ 2 + 110692 * a ^ 2 * b + 23548 * a ^ 2 * b ^ 2 + 2068 * a ^ 2 * b ^ 3 + 64 * a ^ 2 * b ^ 4 + 18540 * a ^ 3 + 8936 * a ^ 3 * b + 1372 * a ^ 3 * b ^ 2 + 64 * a ^ 3 * b ^ 3 + 1028 * a ^ 4 + 328 * a ^ 4 * b + 28 * a ^ 4 * b ^ 2 + 28 * a ^ 5 + 4 * a ^ 5 * b := by unfold polySlack3a polyBase polyB0 polyB1 polyB2 polyB3 polyB4 polyD ring rw [h]; positivity private lemma cert_c3a_4b (a : ℝ) (b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ polySlack3a (a + 4) (1) (b + 3) := by have h : polySlack3a (a + 4) (1) (b + 3) = 59760 + 138056 * b + 82792 * b ^ 2 + 20456 * b ^ 3 + 2476 * b ^ 4 + 152 * b ^ 5 + 4 * b ^ 6 + 58376 * a + 106008 * a * b + 54424 * a * b ^ 2 + 10688 * a * b ^ 3 + 900 * a * b ^ 4 + 28 * a * b ^ 5 + 20096 * a ^ 2 + 26624 * a ^ 2 * b + 10948 * a ^ 2 * b ^ 2 + 1492 * a ^ 2 * b ^ 3 + 64 * a ^ 2 * b ^ 4 + 3696 * a ^ 3 + 3112 * a ^ 3 * b + 908 * a ^ 3 * b ^ 2 + 64 * a ^ 3 * b ^ 3 + 376 * a ^ 4 + 180 * a ^ 4 * b + 28 * a ^ 4 * b ^ 2 + 16 * a ^ 5 + 4 * a ^ 5 * b := by unfold polySlack3a polyBase polyB0 polyB1 polyB2 polyB3 polyB4 polyD ring rw [h]; positivity private lemma cert_c3a_4c (a : ℝ) (ha : 0 ≤ a) : 0 ≤ polySlack3a (a + 7) (1) (2) := by have h : polySlack3a (a + 7) (1) (2) = 63840 + 82536 * a + 31180 * a ^ 2 + 5196 * a ^ 3 + 404 * a ^ 4 + 12 * a ^ 5 := by unfold polySlack3a polyBase polyB0 polyB1 polyB2 polyB3 polyB4 polyD ring rw [h]; positivity private lemma cert_c3a_4d (a : ℝ) (ha : 0 ≤ a) : 0 ≤ polySlack3a (a + 8) (1) (1) := by have h : polySlack3a (a + 8) (1) (1) = 28896 + 61304 * a + 24240 * a ^ 2 + 3920 * a ^ 3 + 288 * a ^ 4 + 8 * a ^ 5 := by unfold polySlack3a polyBase polyB0 polyB1 polyB2 polyB3 polyB4 polyD ring rw [h]; positivity private lemma cert_c3b_1 (a : ℝ) (b : ℝ) (c : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) (hc : 0 ≤ c) : 0 ≤ polySlack3b (a) (c + 4 + b) (c + 3) := by have h : polySlack3b (a) (c + 4 + b) (c + 3) = 263808 + 515104 * c + 421120 * c ^ 2 + 184440 * c ^ 3 + 45432 * c ^ 4 + 5936 * c ^ 5 + 320 * c ^ 6 + 334448 * b + 531592 * b * c + 335824 * b * c ^ 2 + 105808 * b * c ^ 3 + 16624 * b * c ^ 4 + 1040 * b * c ^ 5 + 153892 * b ^ 2 + 190550 * b ^ 2 * c + 87818 * b ^ 2 * c ^ 2 + 17924 * b ^ 2 * c ^ 3 + 1368 * b ^ 2 * c ^ 4 + 34734 * b ^ 3 + 31420 * b ^ 3 * c + 9424 * b ^ 3 * c ^ 2 + 940 * b ^ 3 * c ^ 3 + 4142 * b ^ 4 + 2438 * b ^ 4 * c + 358 * b ^ 4 * c ^ 2 + 250 * b ^ 5 + 72 * b ^ 5 * c + 6 * b ^ 6 + 197264 * a + 324664 * a * c + 214144 * a * c ^ 2 + 70624 * a * c ^ 3 + 11584 * a * c ^ 4 + 752 * a * c ^ 5 + 203144 * a * b + 257512 * a * b * c + 122068 * a * b * c ^ 2 + 25672 * a * b * c ^ 3 + 2016 * a * b * c ^ 4 + 73764 * a * b ^ 2 + 67720 * a * b ^ 2 * c + 20700 * a * b ^ 2 * c ^ 2 + 2108 * a * b ^ 2 * c ^ 3 + 12292 * a * b ^ 3 + 7292 * a * b ^ 3 * c + 1084 * a * b ^ 3 * c ^ 2 + 956 * a * b ^ 4 + 276 * a * b ^ 4 * c + 28 * a * b ^ 5 + 50836 * a ^ 2 + 68874 * a ^ 2 * c + 35226 * a ^ 2 * c ^ 2 + 8020 * a ^ 2 * c ^ 3 + 680 * a ^ 2 * c ^ 4 + 44834 * a ^ 2 * b + 42576 * a ^ 2 * b * c + 13608 * a ^ 2 * b * c ^ 2 + 1460 * a ^ 2 * b * c ^ 3 + 12478 * a ^ 2 * b ^ 2 + 7524 * a ^ 2 * b ^ 2 * c + 1152 * a ^ 2 * b ^ 2 * c ^ 2 + 1376 * a ^ 2 * b ^ 3 + 400 * a ^ 2 * b ^ 3 * c + 52 * a ^ 2 * b ^ 4 + 6044 * a ^ 3 + 6492 * a ^ 3 * c + 2380 * a ^ 3 * c ^ 2 + 292 * a ^ 3 * c ^ 3 + 4756 * a ^ 3 * b + 2972 * a ^ 3 * b * c + 484 * a ^ 3 * b * c ^ 2 + 904 * a ^ 3 * b ^ 2 + 264 * a ^ 3 * b ^ 2 * c + 48 * a ^ 3 * b ^ 3 + 428 * a ^ 4 + 302 * a ^ 4 * c + 58 * a ^ 4 * c ^ 2 + 254 * a ^ 4 * b + 72 * a ^ 4 * b * c + 22 * a ^ 4 * b ^ 2 + 20 * a ^ 5 + 4 * a ^ 5 * c + 4 * a ^ 5 * b := by unfold polySlack3b polyBase polyB0 polyB1 polyB2 polyB3 polyB4 polyD ring rw [h]; positivity private lemma cert_c3b_2a (a : ℝ) (b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ polySlack3b (a) (b + 5) (2) := by have h : polySlack3b (a) (b + 5) (2) = 393984 + 409824 * b + 170560 * b ^ 2 + 36374 * b ^ 3 + 4202 * b ^ 4 + 250 * b ^ 5 + 6 * b ^ 6 + 274032 * a + 239596 * a * b + 79700 * a * b ^ 2 + 12644 * a * b ^ 3 + 960 * a * b ^ 4 + 28 * a * b ^ 5 + 71724 * a ^ 2 + 52206 * a ^ 2 * b + 13210 * a ^ 2 * b ^ 2 + 1392 * a ^ 2 * b ^ 3 + 52 * a ^ 2 * b ^ 4 + 9120 * a ^ 3 + 5404 * a ^ 3 * b + 928 * a ^ 3 * b ^ 2 + 48 * a ^ 3 * b ^ 3 + 636 * a ^ 4 + 270 * a ^ 4 * b + 22 * a ^ 4 * b ^ 2 + 24 * a ^ 5 + 4 * a ^ 5 * b := by unfold polySlack3b polyBase polyB0 polyB1 polyB2 polyB3 polyB4 polyD ring rw [h]; positivity private lemma cert_c3b_2b (a : ℝ) (b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ polySlack3b (a + 4) (b + 3) (2) := by have h : polySlack3b (a + 4) (b + 3) (2) = 459024 + 650968 * b + 277436 * b ^ 2 + 54822 * b ^ 3 + 5614 * b ^ 4 + 290 * b ^ 5 + 6 * b ^ 6 + 252040 * a + 319868 * a * b + 109836 * a * b ^ 2 + 16196 * a * b ^ 3 + 1096 * a * b ^ 4 + 28 * a * b ^ 5 + 57432 * a ^ 2 + 61654 * a ^ 2 * b + 15898 * a ^ 2 * b ^ 2 + 1552 * a ^ 2 * b ^ 3 + 52 * a ^ 2 * b ^ 4 + 7144 * a ^ 3 + 5820 * a ^ 3 * b + 992 * a ^ 3 * b ^ 2 + 48 * a ^ 3 * b ^ 3 + 504 * a ^ 4 + 262 * a ^ 4 * b + 22 * a ^ 4 * b ^ 2 + 16 * a ^ 5 + 4 * a ^ 5 * b := by unfold polySlack3b polyBase polyB0 polyB1 polyB2 polyB3 polyB4 polyD ring rw [h]; positivity private lemma cert_c3b_3a (a : ℝ) (b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ polySlack3b (a) (b + 5) (1) := by have h : polySlack3b (a) (b + 5) (1) = 168000 + 199112 * b + 94792 * b ^ 2 + 23226 * b ^ 3 + 3098 * b ^ 4 + 214 * b ^ 5 + 6 * b ^ 6 + 143672 * a + 138992 * a * b + 52032 * a * b ^ 2 + 9412 * a * b ^ 3 + 824 * a * b ^ 4 + 28 * a * b ^ 5 + 46120 * a ^ 2 + 35830 * a ^ 2 * b + 10030 * a ^ 2 * b ^ 2 + 1200 * a ^ 2 * b ^ 3 + 52 * a ^ 2 * b ^ 4 + 7168 * a ^ 3 + 4300 * a ^ 3 * b + 808 * a ^ 3 * b ^ 2 + 48 * a ^ 3 * b ^ 3 + 584 * a ^ 4 + 242 * a ^ 4 * b + 22 * a ^ 4 * b ^ 2 + 24 * a ^ 5 + 4 * a ^ 5 * b := by unfold polySlack3b polyBase polyB0 polyB1 polyB2 polyB3 polyB4 polyD ring rw [h]; positivity private lemma cert_c3b_3b (a : ℝ) (b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ polySlack3b (a + 5) (b + 2) (1) := by have h : polySlack3b (a + 5) (b + 2) (1) = 124800 + 252760 * b + 135560 * b ^ 2 + 32690 * b ^ 3 + 4018 * b ^ 4 + 246 * b ^ 5 + 6 * b ^ 6 + 91720 * a + 151212 * a * b + 63840 * a * b ^ 2 + 11404 * a * b ^ 3 + 924 * a * b ^ 4 + 28 * a * b ^ 5 + 27772 * a ^ 2 + 35154 * a ^ 2 * b + 10978 * a ^ 2 * b ^ 2 + 1296 * a ^ 2 * b ^ 3 + 52 * a ^ 2 * b ^ 4 + 4364 * a ^ 3 + 3948 * a ^ 3 * b + 816 * a ^ 3 * b ^ 2 + 48 * a ^ 3 * b ^ 3 + 356 * a ^ 4 + 210 * a ^ 4 * b + 22 * a ^ 4 * b ^ 2 + 12 * a ^ 5 + 4 * a ^ 5 * b := by unfold polySlack3b polyBase polyB0 polyB1 polyB2 polyB3 polyB4 polyD ring rw [h]; positivity /-- Rational lower bounds `(r*(r+1)/2) * gEnv M (r - k) ≥ b_k` for `r ≥ 10, k ∈ {0,1,2,3,4}`. -/ private lemma gEnv_ge_b0 (r M : ℕ) (hr : 10 ≤ r) : (1 - 2 * (M : ℝ) / (((r : ℝ) + 1) * ((r : ℝ) + 2))) * (2 / ((r : ℝ) * ((r : ℝ) + 1))) ≤ gEnv M r := by unfold gEnv; dsimp only; rw [if_neg (by omega)] set rr : ℝ := (r : ℝ) have hr10 : (10 : ℝ) ≤ rr := by dsimp only [rr]; exact_mod_cast hr have hmin : min (M : ℝ) (2 * rr - 8) ≤ (M : ℝ) := min_le_left _ _ have h1 : 4 * min (M : ℝ) (2 * rr - 8) / ((rr + 1) * (rr + 2) * (rr + 3) * (rr + 4)) ≤ 4 * (M : ℝ) / ((rr + 1) * (rr + 2) * (rr + 3) * (rr + 4)) := div_le_div_of_nonneg_right (by linarith) (by positivity) have h2 : 4 * (M : ℝ) / ((rr + 1) * (rr + 2) * (rr + 3) * (rr + 4)) ≤ 4 * (M : ℝ) / (rr * (rr + 1) * (rr + 1) * (rr + 2)) := by rw [div_le_div_iff₀ (by positivity) (by positivity)] have hM : (0 : ℝ) ≤ 4 * (M : ℝ) := by positivity have hden : rr * (rr + 1) * (rr + 1) * (rr + 2) ≤ (rr + 1) * (rr + 2) * (rr + 3) * (rr + 4) := by have hA : rr * (rr + 1) ≤ (rr + 3) * (rr + 4) := by nlinarith have hB : 0 ≤ (rr + 1) * (rr + 2) := by positivity have := mul_le_mul_of_nonneg_right hA hB linarith exact mul_le_mul_of_nonneg_left hden hM have hEq : (1 - 2 * (M : ℝ) / ((rr + 1) * (rr + 2))) * (2 / (rr * (rr + 1))) = 2 / (rr * (rr + 1)) - 4 * (M : ℝ) / (rr * (rr + 1) * (rr + 1) * (rr + 2)) := by field_simp; ring linarith private lemma gEnv_ge_b1 (r M : ℕ) (hr : 10 ≤ r) : (1 + 2 / ((r : ℝ) - 1) - 2 * (M : ℝ) / ((r : ℝ) * ((r : ℝ) + 1))) * (2 / ((r : ℝ) * ((r : ℝ) + 1))) ≤ gEnv M (r - 1) := by unfold gEnv; dsimp only set rr : ℝ := (r : ℝ) have hr10 : (10 : ℝ) ≤ rr := by dsimp only [rr]; exact_mod_cast hr have hr1 : 0 < rr - 1 := by linarith have hcast : ((r - 1 : ℕ) : ℝ) = rr - 1 := by rw [Nat.cast_sub (by omega)]; simp [rr] rw [hcast] have hbase : (1 + 2 / (rr - 1)) * (2 / (rr * (rr + 1))) = 2 / ((rr - 1) * (rr - 1 + 1)) := by field_simp [hr1.ne']; ring split_ifs with h9 · have hsub : 0 ≤ (2 * (M : ℝ) / (rr * (rr + 1))) * (2 / (rr * (rr + 1))) := by positivity linarith · have hmin : min (M : ℝ) (2 * (rr - 1) - 8) ≤ (M : ℝ) := min_le_left _ _ have h1 : 4 * min (M : ℝ) (2 * (rr - 1) - 8) / ((rr - 1 + 1) * (rr - 1 + 2) * (rr - 1 + 3) * (rr - 1 + 4)) ≤ 4 * (M : ℝ) / (rr * (rr + 1) * (rr + 2) * (rr + 3)) := by have hden : (rr - 1 + 1) * (rr - 1 + 2) * (rr - 1 + 3) * (rr - 1 + 4) = rr * (rr + 1) * (rr + 2) * (rr + 3) := by ring rw [hden] exact div_le_div_of_nonneg_right (by linarith) (by positivity) have h2 : 4 * (M : ℝ) / (rr * (rr + 1) * (rr + 2) * (rr + 3)) ≤ 4 * (M : ℝ) / (rr * (rr + 1) * rr * (rr + 1)) := by rw [div_le_div_iff₀ (by positivity) (by positivity)] have hM : (0 : ℝ) ≤ 4 * (M : ℝ) := by positivity have hden : rr * (rr + 1) * rr * (rr + 1) ≤ rr * (rr + 1) * (rr + 2) * (rr + 3) := by have hA : rr * (rr + 1) ≤ (rr + 2) * (rr + 3) := by nlinarith have hB : 0 ≤ rr * (rr + 1) := by positivity have := mul_le_mul_of_nonneg_left hA hB linarith exact mul_le_mul_of_nonneg_left hden hM have hEq : (1 + 2 / (rr - 1) - 2 * (M : ℝ) / (rr * (rr + 1))) * (2 / (rr * (rr + 1))) = 2 / ((rr - 1) * (rr - 1 + 1)) - 4 * (M : ℝ) / (rr * (rr + 1) * rr * (rr + 1)) := by field_simp [hr1.ne']; ring linarith private lemma gEnv_ge_b2 (r M : ℕ) (hr : 10 ≤ r) : (1 + 4 / ((r : ℝ) - 1) + 6 / (((r : ℝ) - 1) * ((r : ℝ) - 2)) - 2 * (M : ℝ) / (((r : ℝ) - 1) * (r : ℝ))) * (2 / ((r : ℝ) * ((r : ℝ) + 1))) ≤ gEnv M (r - 2) := by unfold gEnv; dsimp only set rr : ℝ := (r : ℝ) have hr10 : (10 : ℝ) ≤ rr := by dsimp only [rr]; exact_mod_cast hr have hr1 : 0 < rr - 1 := by linarith have hr2 : 0 < rr - 2 := by linarith have hcast : ((r - 2 : ℕ) : ℝ) = rr - 2 := by rw [Nat.cast_sub (by omega)]; simp [rr] rw [hcast] have hbase : (1 + 4 / (rr - 1) + 6 / ((rr - 1) * (rr - 2))) * (2 / (rr * (rr + 1))) ≤ 2 / ((rr - 2) * (rr - 2 + 1)) := by have hEq : (1 + 4 / (rr - 1) + 6 / ((rr - 1) * (rr - 2))) * (2 / (rr * (rr + 1))) = 2 / ((rr - 2) * (rr - 2 + 1)) := by have hne : rr - 2 + 1 ≠ 0 := by linarith field_simp [hr1.ne', hr2.ne', hne]; ring linarith split_ifs with h9 · have hsub : 0 ≤ (2 * (M : ℝ) / ((rr - 1) * rr)) * (2 / (rr * (rr + 1))) := by positivity linarith · have hmin : min (M : ℝ) (2 * (rr - 2) - 8) ≤ (M : ℝ) := min_le_left _ _ have h1 : 4 * min (M : ℝ) (2 * (rr - 2) - 8) / ((rr - 2 + 1) * (rr - 2 + 2) * (rr - 2 + 3) * (rr - 2 + 4)) ≤ 4 * (M : ℝ) / ((rr - 1) * rr * (rr + 1) * (rr + 2)) := by have hden : (rr - 2 + 1) * (rr - 2 + 2) * (rr - 2 + 3) * (rr - 2 + 4) = (rr - 1) * rr * (rr + 1) * (rr + 2) := by ring rw [hden] exact div_le_div_of_nonneg_right (by linarith) (by positivity) have h2 : 4 * (M : ℝ) / ((rr - 1) * rr * (rr + 1) * (rr + 2)) ≤ (2 * (M : ℝ) / ((rr - 1) * rr)) * (2 / (rr * (rr + 1))) := by have hEq : (2 * (M : ℝ) / ((rr - 1) * rr)) * (2 / (rr * (rr + 1))) = 4 * (M : ℝ) / ((rr - 1) * rr * rr * (rr + 1)) := by field_simp [hr1.ne']; ring rw [hEq, div_le_div_iff₀ (by positivity) (by positivity)] have hM : (0 : ℝ) ≤ 4 * (M : ℝ) := by positivity have hden : (rr - 1) * rr * rr * (rr + 1) ≤ (rr - 1) * rr * (rr + 1) * (rr + 2) := by have hB : 0 ≤ (rr - 1) * rr * (rr + 1) := by positivity have := mul_le_mul_of_nonneg_left (show rr ≤ rr + 2 by linarith) hB linarith exact mul_le_mul_of_nonneg_left hden hM linarith private lemma gEnv_ge_b3 (r M : ℕ) (hr : 10 ≤ r) : (1 + 6 / ((r : ℝ) - 1) + 18 / (((r : ℝ) - 1) * ((r : ℝ) - 2)) - 2 * (M : ℝ) / (((r : ℝ) - 1) * ((r : ℝ) - 2))) * (2 / ((r : ℝ) * ((r : ℝ) + 1))) ≤ gEnv M (r - 3) := by unfold gEnv; dsimp only set rr : ℝ := (r : ℝ) have hr10 : (10 : ℝ) ≤ rr := by dsimp only [rr]; exact_mod_cast hr have hr1 : 0 < rr - 1 := by linarith have hr2 : 0 < rr - 2 := by linarith have hr3 : 0 < rr - 3 := by linarith have hcast : ((r - 3 : ℕ) : ℝ) = rr - 3 := by rw [Nat.cast_sub (by omega)]; simp [rr] rw [hcast] have hbase : (1 + 6 / (rr - 1) + 18 / ((rr - 1) * (rr - 2))) * (2 / (rr * (rr + 1))) ≤ 2 / ((rr - 3) * (rr - 3 + 1)) := by have hEq : 2 / ((rr - 3) * (rr - 3 + 1)) - (1 + 6 / (rr - 1) + 18 / ((rr - 1) * (rr - 2))) * (2 / (rr * (rr + 1))) = 48 / ((rr - 3) * (rr - 2) * (rr - 1) * rr * (rr + 1)) := by have hne : rr - 3 + 1 ≠ 0 := by linarith field_simp [hr1.ne', hr2.ne', hr3.ne', hne]; ring have hpos : 0 ≤ 48 / ((rr - 3) * (rr - 2) * (rr - 1) * rr * (rr + 1)) := by positivity linarith split_ifs with h9 · have hsub : 0 ≤ (2 * (M : ℝ) / ((rr - 1) * (rr - 2))) * (2 / (rr * (rr + 1))) := by positivity linarith · have hmin : min (M : ℝ) (2 * (rr - 3) - 8) ≤ (M : ℝ) := min_le_left _ _ have h1 : 4 * min (M : ℝ) (2 * (rr - 3) - 8) / ((rr - 3 + 1) * (rr - 3 + 2) * (rr - 3 + 3) * (rr - 3 + 4)) ≤ 4 * (M : ℝ) / ((rr - 2) * (rr - 1) * rr * (rr + 1)) := by have hden : (rr - 3 + 1) * (rr - 3 + 2) * (rr - 3 + 3) * (rr - 3 + 4) = (rr - 2) * (rr - 1) * rr * (rr + 1) := by ring rw [hden] exact div_le_div_of_nonneg_right (by linarith) (by positivity) have hEq : (2 * (M : ℝ) / ((rr - 1) * (rr - 2))) * (2 / (rr * (rr + 1))) = 4 * (M : ℝ) / ((rr - 2) * (rr - 1) * rr * (rr + 1)) := by field_simp [hr1.ne', hr2.ne']; ring linarith private lemma gEnv_ge_b4 (r M : ℕ) (hr : 10 ≤ r) : (1 + 8 / ((r : ℝ) - 1) + 36 / (((r : ℝ) - 1) * ((r : ℝ) - 2)) - 2 * ((r : ℝ) + 1) * (M : ℝ) / (((r : ℝ) - 3) * ((r : ℝ) - 2) * ((r : ℝ) - 1))) * (2 / ((r : ℝ) * ((r : ℝ) + 1))) ≤ gEnv M (r - 4) := by unfold gEnv; dsimp only set rr : ℝ := (r : ℝ) have hr10 : (10 : ℝ) ≤ rr := by dsimp only [rr]; exact_mod_cast hr have hr1 : 0 < rr - 1 := by linarith have hr2 : 0 < rr - 2 := by linarith have hr3 : 0 < rr - 3 := by linarith have hr4 : 0 < rr - 4 := by linarith have hcast : ((r - 4 : ℕ) : ℝ) = rr - 4 := by rw [Nat.cast_sub (by omega)]; simp [rr] rw [hcast] have hbase : (1 + 8 / (rr - 1) + 36 / ((rr - 1) * (rr - 2))) * (2 / (rr * (rr + 1))) ≤ 2 / ((rr - 4) * (rr - 4 + 1)) := by have hEq : 2 / ((rr - 4) * (rr - 4 + 1)) - (1 + 8 / (rr - 1) + 36 / ((rr - 1) * (rr - 2))) * (2 / (rr * (rr + 1))) = 48 * (4 * rr - 11) / ((rr - 4) * (rr - 3) * (rr - 2) * (rr - 1) * rr * (rr + 1)) := by have hne : rr - 4 + 1 ≠ 0 := by linarith field_simp [hr1.ne', hr2.ne', hr3.ne', hr4.ne', hne]; ring have hnum : 0 ≤ 48 * (4 * rr - 11) := by linarith have hden : 0 < (rr - 4) * (rr - 3) * (rr - 2) * (rr - 1) * rr * (rr + 1) := by positivity have hpos : 0 ≤ 48 * (4 * rr - 11) / ((rr - 4) * (rr - 3) * (rr - 2) * (rr - 1) * rr * (rr + 1)) := div_nonneg hnum hden.le linarith split_ifs with h9 · have hsub : 0 ≤ (2 * (rr + 1) * (M : ℝ) / ((rr - 3) * (rr - 2) * (rr - 1))) * (2 / (rr * (rr + 1))) := by positivity linarith · have hmin : min (M : ℝ) (2 * (rr - 4) - 8) ≤ (M : ℝ) := min_le_left _ _ have h1 : 4 * min (M : ℝ) (2 * (rr - 4) - 8) / ((rr - 4 + 1) * (rr - 4 + 2) * (rr - 4 + 3) * (rr - 4 + 4)) ≤ 4 * (M : ℝ) / ((rr - 3) * (rr - 2) * (rr - 1) * rr) := by have hden : (rr - 4 + 1) * (rr - 4 + 2) * (rr - 4 + 3) * (rr - 4 + 4) = (rr - 3) * (rr - 2) * (rr - 1) * rr := by ring rw [hden] exact div_le_div_of_nonneg_right (by linarith) (by positivity) have hEq : (2 * (rr + 1) * (M : ℝ) / ((rr - 3) * (rr - 2) * (rr - 1))) * (2 / (rr * (rr + 1))) = 4 * (M : ℝ) / ((rr - 3) * (rr - 2) * (rr - 1) * rr) := by field_simp [hr1.ne', hr2.ne', hr3.ne']; ring linarith /-- Ordering `polyB2 ≤ polyB3 ≤ polyB4` for ALL `r = L2 + N1 + S1 ≥ 10` and `L2 + N1 ≥ 2`. -/ private lemma polyB2_le_polyB3 (L2 N1 S1 : ℝ) (hL : 0 ≤ L2) (hN : 0 ≤ N1) (hS : 0 ≤ S1) (hLN : 2 ≤ L2 + N1) (hr : 10 ≤ L2 + N1 + S1) : polyB2 L2 N1 S1 ≤ polyB3 L2 N1 S1 := by unfold polyB2 polyB3 set r := L2 + N1 + S1 set M := N1 + 2 * S1 have hr3 : 0 < r - 3 := by linarith have hM : M ≤ 2 * r - 2 := by linarith have hdiff : (r - 3) * r * (r + 1) * (r + 2) * ((r - 2) * (r - 1) + 6 * (r - 2) + 18 - 2 * M) - (r - 3) * (r + 1) * (r + 2) * ((r - 2) * (r - 1) * r + 4 * (r - 2) * r + 6 * r - 2 * M * (r - 2)) = 2 * (r - 3) * (r + 1) * (r + 2) * (r * (r + 4) - 2 * M) := by ring have hfac1 : 0 ≤ 2 * (r - 3) * (r + 1) * (r + 2) := by positivity have hfac2 : 0 ≤ r * (r + 4) - 2 * M := by nlinarith have hprod : 0 ≤ 2 * (r - 3) * (r + 1) * (r + 2) * (r * (r + 4) - 2 * M) := mul_nonneg hfac1 hfac2 linarith private lemma polyB3_le_polyB4 (L2 N1 S1 : ℝ) (hL : 0 ≤ L2) (hN : 0 ≤ N1) (hS : 0 ≤ S1) (hLN : 2 ≤ L2 + N1) (hr : 10 ≤ L2 + N1 + S1) : polyB3 L2 N1 S1 ≤ polyB4 L2 N1 S1 := by unfold polyB3 polyB4 set r := L2 + N1 + S1 set M := N1 + 2 * S1 have hM : M ≤ 2 * r - 2 := by linarith have hdiff : r * (r + 1) * (r + 2) * ((r - 3) * ((r - 2) * (r - 1) + 8 * (r - 2) + 36) - 2 * (r + 1) * M) - (r - 3) * r * (r + 1) * (r + 2) * ((r - 2) * (r - 1) + 6 * (r - 2) + 18 - 2 * M) = 2 * r * (r + 1) * (r + 2) * ((r - 3) * (r + 7) - 4 * M) := by ring have hfac1 : 0 ≤ 2 * r * (r + 1) * (r + 2) := by positivity have hfac2 : 0 ≤ (r - 3) * (r + 7) - 4 * M := by nlinarith have hprod : 0 ≤ 2 * r * (r + 1) * (r + 2) * ((r - 3) * (r + 7) - 4 * M) := mul_nonneg hfac1 hfac2 linarith /-- Master algebraic theorem for `r ≥ 10`: combines all 20 all-positive polynomial certificates. -/ private lemma master_poly_feas (L2 N1 S1 : ℕ) (hLN : 2 ≤ L2 + N1) (hr : 10 ≤ L2 + N1 + S1) : (S1 = 0 → 0 ≤ polySlack1 (L2 : ℝ) (N1 : ℝ) (S1 : ℝ)) ∧ (N1 = 0 → 1 ≤ S1 → 0 ≤ polySlack2 (L2 : ℝ) (N1 : ℝ) (S1 : ℝ)) ∧ (1 ≤ N1 → N1 ≤ S1 → 0 ≤ polySlack3a (L2 : ℝ) (N1 : ℝ) (S1 : ℝ)) ∧ (1 ≤ S1 → S1 < N1 → 0 ≤ polySlack3b (L2 : ℝ) (N1 : ℝ) (S1 : ℝ)) := by refine ⟨?_, ?_, ?_, ?_⟩ · -- Case 1: S1 = 0 intro hS0; subst hS0 by_cases hL5 : 5 ≤ L2 · have := cert_c1a ((L2 - 5 : ℕ) : ℝ) (N1 : ℝ) (Nat.cast_nonneg _) (Nat.cast_nonneg _) have hcast : ((L2 - 5 : ℕ) : ℝ) + 5 = (L2 : ℝ) := by rw [Nat.cast_sub hL5]; push_cast; ring simpa [hcast] using this · have hN5 : 5 ≤ N1 := by omega have := cert_c1b (L2 : ℝ) ((N1 - 5 : ℕ) : ℝ) (Nat.cast_nonneg _) (Nat.cast_nonneg _) have hcast : ((N1 - 5 : ℕ) : ℝ) + 5 = (N1 : ℝ) := by rw [Nat.cast_sub hN5]; push_cast; ring simpa [hcast] using this · -- Case 2: N1 = 0, S1 ≥ 1 intro hN0 hS1; subst hN0 by_cases hL5 : 5 ≤ L2 · have := cert_c2a ((L2 - 5 : ℕ) : ℝ) ((S1 - 1 : ℕ) : ℝ) (Nat.cast_nonneg _) (Nat.cast_nonneg _) have hL : ((L2 - 5 : ℕ) : ℝ) + 5 = (L2 : ℝ) := by rw [Nat.cast_sub hL5]; push_cast; ring have hS : ((S1 - 1 : ℕ) : ℝ) + 1 = (S1 : ℝ) := by rw [Nat.cast_sub hS1]; push_cast; ring simpa [hL, hS] using this · have hS6 : 6 ≤ S1 := by omega have hL2 : 2 ≤ L2 := by omega have := cert_c2b ((L2 - 2 : ℕ) : ℝ) ((S1 - 6 : ℕ) : ℝ) (Nat.cast_nonneg _) (Nat.cast_nonneg _) have hL : ((L2 - 2 : ℕ) : ℝ) + 2 = (L2 : ℝ) := by rw [Nat.cast_sub hL2]; push_cast; ring have hS : ((S1 - 6 : ℕ) : ℝ) + 6 = (S1 : ℝ) := by rw [Nat.cast_sub hS6]; push_cast; ring simpa [hL, hS] using this · -- Case 3a: 1 ≤ N1 ≤ S1 intro hN1 hNS by_cases hN4 : 4 ≤ N1 · have := cert_c3a_1 (L2 : ℝ) ((S1 - N1 : ℕ) : ℝ) ((N1 - 4 : ℕ) : ℝ) (Nat.cast_nonneg _) (Nat.cast_nonneg _) (Nat.cast_nonneg _) have hN : ((N1 - 4 : ℕ) : ℝ) + 4 = (N1 : ℝ) := by rw [Nat.cast_sub hN4]; push_cast; ring have hS : (N1 : ℝ) + ((S1 - N1 : ℕ) : ℝ) = (S1 : ℝ) := by rw [Nat.cast_sub hNS]; ring simpa [hN, hS] using this · have hN_cases : N1 = 1 ∨ N1 = 2 ∨ N1 = 3 := by omega rcases hN_cases with rfl | rfl | rfl · -- N1 = 1 by_cases hL1 : L2 = 1 · subst hL1 have hS8 : 8 ≤ S1 := by omega have := cert_c3a_4a1 (0 : ℝ) ((S1 - 8 : ℕ) : ℝ) le_rfl (Nat.cast_nonneg _) have hS : ((S1 - 8 : ℕ) : ℝ) + 8 = (S1 : ℝ) := by rw [Nat.cast_sub hS8]; push_cast; ring simpa [hS] using this · by_cases hL2 : L2 = 2 · subst hL2 have hS7 : 7 ≤ S1 := by omega have := cert_c3a_4a2 (0 : ℝ) ((S1 - 7 : ℕ) : ℝ) le_rfl (Nat.cast_nonneg _) have hS : ((S1 - 7 : ℕ) : ℝ) + 7 = (S1 : ℝ) := by rw [Nat.cast_sub hS7]; push_cast; ring simpa [hS] using this · by_cases hL3 : L2 = 3 · subst hL3 have hS6 : 6 ≤ S1 := by omega have := cert_c3a_4a3 (0 : ℝ) ((S1 - 6 : ℕ) : ℝ) le_rfl (Nat.cast_nonneg _) have hS : ((S1 - 6 : ℕ) : ℝ) + 6 = (S1 : ℝ) := by rw [Nat.cast_sub hS6]; push_cast; ring simpa [hS] using this · have hL4 : 4 ≤ L2 := by omega by_cases hS3 : 3 ≤ S1 · have := cert_c3a_4b ((L2 - 4 : ℕ) : ℝ) ((S1 - 3 : ℕ) : ℝ) (Nat.cast_nonneg _) (Nat.cast_nonneg _) have hL : ((L2 - 4 : ℕ) : ℝ) + 4 = (L2 : ℝ) := by rw [Nat.cast_sub hL4]; push_cast; ring have hS : ((S1 - 3 : ℕ) : ℝ) + 3 = (S1 : ℝ) := by rw [Nat.cast_sub hS3]; push_cast; ring simpa [hL, hS] using this · by_cases hS2 : S1 = 2 · subst hS2 have hL7 : 7 ≤ L2 := by omega have := cert_c3a_4c ((L2 - 7 : ℕ) : ℝ) (Nat.cast_nonneg _) have hL : ((L2 - 7 : ℕ) : ℝ) + 7 = (L2 : ℝ) := by rw [Nat.cast_sub hL7]; push_cast; ring simpa [hL] using this · have hS1' : S1 = 1 := by omega subst hS1' have hL8 : 8 ≤ L2 := by omega have := cert_c3a_4d ((L2 - 8 : ℕ) : ℝ) (Nat.cast_nonneg _) have hL : ((L2 - 8 : ℕ) : ℝ) + 8 = (L2 : ℝ) := by rw [Nat.cast_sub hL8]; push_cast; ring simpa [hL] using this · -- N1 = 2 by_cases hS5 : 5 ≤ S1 · have := cert_c3a_3a (L2 : ℝ) ((S1 - 5 : ℕ) : ℝ) (Nat.cast_nonneg _) (Nat.cast_nonneg _) have hS : ((S1 - 5 : ℕ) : ℝ) + 5 = (S1 : ℝ) := by rw [Nat.cast_sub hS5]; push_cast; ring simpa [hS] using this · have hL4 : 4 ≤ L2 := by omega have := cert_c3a_3b ((L2 - 4 : ℕ) : ℝ) ((S1 - 2 : ℕ) : ℝ) (Nat.cast_nonneg _) (Nat.cast_nonneg _) have hL : ((L2 - 4 : ℕ) : ℝ) + 4 = (L2 : ℝ) := by rw [Nat.cast_sub hL4]; push_cast; ring have hS : ((S1 - 2 : ℕ) : ℝ) + 2 = (S1 : ℝ) := by rw [Nat.cast_sub hNS]; push_cast; ring simpa [hL, hS] using this · -- N1 = 3 by_cases hS4 : 4 ≤ S1 · have := cert_c3a_2a (L2 : ℝ) ((S1 - 4 : ℕ) : ℝ) (Nat.cast_nonneg _) (Nat.cast_nonneg _) have hS : ((S1 - 4 : ℕ) : ℝ) + 4 = (S1 : ℝ) := by rw [Nat.cast_sub hS4]; push_cast; ring simpa [hS] using this · have hS3 : S1 = 3 := by omega subst hS3 have hL4 : 4 ≤ L2 := by omega have := cert_c3a_2b ((L2 - 4 : ℕ) : ℝ) (Nat.cast_nonneg _) have hL : ((L2 - 4 : ℕ) : ℝ) + 4 = (L2 : ℝ) := by rw [Nat.cast_sub hL4]; push_cast; ring simpa [hL] using this · -- Case 3b: 1 ≤ S1 < N1 intro hS1 hSN by_cases hS3 : 3 ≤ S1 · have := cert_c3b_1 (L2 : ℝ) ((N1 - S1 - 1 : ℕ) : ℝ) ((S1 - 3 : ℕ) : ℝ) (Nat.cast_nonneg _) (Nat.cast_nonneg _) (Nat.cast_nonneg _) have hS : ((S1 - 3 : ℕ) : ℝ) + 3 = (S1 : ℝ) := by rw [Nat.cast_sub hS3]; push_cast; ring have hN : ((S1 - 3 : ℕ) : ℝ) + 4 + ((N1 - S1 - 1 : ℕ) : ℝ) = (N1 : ℝ) := by rw [Nat.cast_sub hS3, Nat.cast_sub (by omega), Nat.cast_sub (by omega)]; push_cast; ring simpa [hS, hN] using this · have hS_cases : S1 = 1 ∨ S1 = 2 := by omega rcases hS_cases with rfl | rfl · -- S1 = 1 by_cases hN5 : 5 ≤ N1 · have := cert_c3b_3a (L2 : ℝ) ((N1 - 5 : ℕ) : ℝ) (Nat.cast_nonneg _) (Nat.cast_nonneg _) have hN : ((N1 - 5 : ℕ) : ℝ) + 5 = (N1 : ℝ) := by rw [Nat.cast_sub hN5]; push_cast; ring simpa [hN] using this · have hL5 : 5 ≤ L2 := by omega have hN2 : 2 ≤ N1 := by omega have := cert_c3b_3b ((L2 - 5 : ℕ) : ℝ) ((N1 - 2 : ℕ) : ℝ) (Nat.cast_nonneg _) (Nat.cast_nonneg _) have hL : ((L2 - 5 : ℕ) : ℝ) + 5 = (L2 : ℝ) := by rw [Nat.cast_sub hL5]; push_cast; ring have hN : ((N1 - 2 : ℕ) : ℝ) + 2 = (N1 : ℝ) := by rw [Nat.cast_sub hN2]; push_cast; ring simpa [hL, hN] using this · -- S1 = 2 by_cases hN5 : 5 ≤ N1 · have := cert_c3b_2a (L2 : ℝ) ((N1 - 5 : ℕ) : ℝ) (Nat.cast_nonneg _) (Nat.cast_nonneg _) have hN : ((N1 - 5 : ℕ) : ℝ) + 5 = (N1 : ℝ) := by rw [Nat.cast_sub hN5]; push_cast; ring simpa [hN] using this · have hL4 : 4 ≤ L2 := by omega have hN3 : 3 ≤ N1 := by omega have := cert_c3b_2b ((L2 - 4 : ℕ) : ℝ) ((N1 - 3 : ℕ) : ℝ) (Nat.cast_nonneg _) (Nat.cast_nonneg _) have hL : ((L2 - 4 : ℕ) : ℝ) + 4 = (L2 : ℝ) := by rw [Nat.cast_sub hL4]; push_cast; ring have hN : ((N1 - 3 : ℕ) : ℝ) + 3 = (N1 : ℝ) := by rw [Nat.cast_sub hN3]; push_cast; ring simpa [hL, hN] using this /-- **Headline Universal Feasibility of `twoDelDualWeight`**: For EVERY alphabet size `q`, EVERY block length `n = m + 3 ≥ 3`, and EVERY word `x : Word q (m + 3)`, `1 ≤ ∑ y ∈ D₂(x), twoDelDualWeight y`. -/ theorem twoDelDualWeight_feasible {q m : ℕ} (x : Word q (m + 3)) : 1 ≤ ∑ y ∈ del2Ball x, twoDelDualWeight y := by by_cases hr9 : runCount x ≤ 9 · -- For `r(x) ≤ 9`, every `y ∈ D₂(x)` has `r(y) ≤ 9`, so `twoDelDualWeight y = kk2DualWeight y`! have hEq : ∑ y ∈ del2Ball x, twoDelDualWeight y = ∑ y ∈ del2Ball x, kk2DualWeight y := by refine Finset.sum_congr rfl fun y hy => ?_ unfold del2Ball at hy rcases Finset.mem_biUnion.mp hy with ⟨z, hz, hyz⟩ rcases Finset.mem_image.mp hz with ⟨p, _, rfl⟩ rcases Finset.mem_image.mp hyz with ⟨r, _, rfl⟩ have hry : runCount (delPos (delPos x p) r) ≤ 9 := ((runCount_delPos_le _ _).trans (runCount_delPos_le _ _)).trans hr9 unfold twoDelDualWeight kk2DualWeight; dsimp only; rw [if_pos hry] rw [hEq] exact kk2DualWeight_feasible_le_nine x hr9 · -- `r = runCount x ≥ 10`: apply the 7-bucket inequality + `gEnv` + `polyB_k` + `master_poly_feas`! have hr10 : 10 ≤ runCount x := by omega have h7 := sum_del2Ball_ge_seven_buckets x twoDelDualWeight twoDelDualWeight_nonneg set r := runCount x set L := (longRunStarts x).card set N := (nonSandSingletonStarts x).card set S := (sandwichPosSet x).card set eL := (rightInLong x).card set eN := (rightInNonSand x).card set eS := (rightInSand x).card set fN := (leftInNonSand x).card set M := singletonRunCount x + sandwichCount x have hpart : L + N + S = r := card_run_partition x have hLN : 2 ≤ L + N := two_le_long_add_nonSand x (by omega) have hM_eq : M = N + 2 * S := by have hsing : N + S = singletonRunCount x := Finset.card_sdiff_add_card_eq_card (sandwichPosSet_subset_singletonPosSet x) unfold M sandwichCount; omega have hrp : eL + eN + eS = S := card_right_partition x have hfN_le_N : fN ≤ N := card_leftInNonSand_le_nonSand x have hfN_le_S : fN + eS ≤ S := card_leftInNonSand_le_sub_rightInSand x have hLL : 2 * (bucketLL x).card = L * (L + 1) := card_bucketLL x have hLN_c : (bucketLN x).card = L * N := card_bucketLN x have hLS_c : (bucketLS x).card + eL = L * S := card_bucketLS x have hNN_c : 2 * (bucketNN x).card = N * (N - 1) := card_bucketNN x have hNSa_c : (bucketNSAdj x).card = fN := card_bucketNSAdj x have hNSn_c : (bucketNSNon x).card + eN + fN = N * S := card_bucketNSNon x have hSS_c : 2 * ((bucketSS x).card + eS) = S * (S - 1) := card_bucketSS x -- Lower bound each bucket by `gEnv M (r - k)` have hB_LL : ((bucketLL x).card : ℝ) * gEnv M r ≤ ∑ pq ∈ bucketLL x, twoDelDualWeight (del2 x pq.1 pq.2) := by rw [← nsmul_eq_mul, ← Finset.sum_const] refine Finset.sum_le_sum fun pq hpq => ?_ have hlt : pq.1.val < pq.2.val := (Finset.mem_filter.mp (Finset.mem_filter.mp hpq).1).2.1 exact (gEnv_anti M (one_le_runCount _) (runCount_del2_le x pq.1 pq.2 hlt)).trans (twoDelDualWeight_ge_gEnv x (del2_mem_del2Ball x pq.1 pq.2 hlt)) have hB_LN : ((bucketLN x).card : ℝ) * gEnv M (r - 1) ≤ ∑ pq ∈ bucketLN x, twoDelDualWeight (del2 x pq.1 pq.2) := by rw [← nsmul_eq_mul, ← Finset.sum_const] refine Finset.sum_le_sum fun pq hpq => ?_ have hlt : pq.1.val < pq.2.val := (Finset.mem_filter.mp (Finset.mem_filter.mp hpq).1).2.1 have hdrop : runCount (del2 x pq.1 pq.2) ≤ r - 1 := by rcases (Finset.mem_filter.mp hpq).2 with ⟨_, hN⟩ | ⟨hN, _⟩ · have := runCount_del2_le_sub_one_right x pq.1 pq.2 hlt (Finset.mem_sdiff.mp hN).1 (by omega); omega · have := runCount_del2_le_sub_one_left x pq.1 pq.2 hlt (Finset.mem_sdiff.mp hN).1 (by omega); omega exact (gEnv_anti M (one_le_runCount _) hdrop).trans (twoDelDualWeight_ge_gEnv x (del2_mem_del2Ball x pq.1 pq.2 hlt)) have hB_LS : ((bucketLS x).card : ℝ) * gEnv M (r - 2) ≤ ∑ pq ∈ bucketLS x, twoDelDualWeight (del2 x pq.1 pq.2) := by rw [← nsmul_eq_mul, ← Finset.sum_const] refine Finset.sum_le_sum fun pq hpq => ?_ have hlt : pq.1.val < pq.2.val := (Finset.mem_filter.mp (Finset.mem_filter.mp hpq).1).2.1 have hdrop : runCount (del2 x pq.1 pq.2) ≤ r - 2 := by rcases (Finset.mem_filter.mp hpq).2 with ⟨_, hS⟩ | ⟨hS, _⟩ · have := runCount_del2_le_sub_two_sand_right x pq.1 pq.2 hlt hS; omega · have := runCount_del2_le_sub_two_sand_left x pq.1 pq.2 hlt hS; omega exact (gEnv_anti M (one_le_runCount _) hdrop).trans (twoDelDualWeight_ge_gEnv x (del2_mem_del2Ball x pq.1 pq.2 hlt)) have hB_NN : ((bucketNN x).card : ℝ) * gEnv M (r - 2) ≤ ∑ pq ∈ bucketNN x, twoDelDualWeight (del2 x pq.1 pq.2) := by rw [← nsmul_eq_mul, ← Finset.sum_const] refine Finset.sum_le_sum fun pq hpq => ?_ have hlt : pq.1.val < pq.2.val := (Finset.mem_filter.mp (Finset.mem_filter.mp hpq).1).2.1 have h12 := (Finset.mem_filter.mp hpq).2 have hdrop : runCount (del2 x pq.1 pq.2) ≤ r - 2 := by have := runCount_del2_le_sub_two_NN x pq.1 pq.2 hlt h12.1 h12.2 (by omega); omega exact (gEnv_anti M (one_le_runCount _) hdrop).trans (twoDelDualWeight_ge_gEnv x (del2_mem_del2Ball x pq.1 pq.2 hlt)) have hB_NSa : ((bucketNSAdj x).card : ℝ) * gEnv M (r - 2) ≤ ∑ pq ∈ bucketNSAdj x, twoDelDualWeight (del2 x pq.1 pq.2) := by rw [← nsmul_eq_mul, ← Finset.sum_const] refine Finset.sum_le_sum fun pq hpq => ?_ have hlt : pq.1.val < pq.2.val := (Finset.mem_filter.mp (Finset.mem_filter.mp hpq).1).2.1 have hS := (Finset.mem_filter.mp hpq).2.2.1 have hdrop : runCount (del2 x pq.1 pq.2) ≤ r - 2 := by have := runCount_del2_le_sub_two_sand_right x pq.1 pq.2 hlt hS; omega exact (gEnv_anti M (one_le_runCount _) hdrop).trans (twoDelDualWeight_ge_gEnv x (del2_mem_del2Ball x pq.1 pq.2 hlt)) have hB_NSn : ((bucketNSNon x).card : ℝ) * gEnv M (r - 3) ≤ ∑ pq ∈ bucketNSNon x, twoDelDualWeight (del2 x pq.1 pq.2) := by rw [← nsmul_eq_mul, ← Finset.sum_const] refine Finset.sum_le_sum fun pq hpq => ?_ have hpq_nr := (Finset.mem_filter.mp hpq).1 have hlt : pq.1.val < pq.2.val := (Finset.mem_filter.mp hpq_nr).2.1 have hdrop : runCount (del2 x pq.1 pq.2) ≤ r - 3 := by rcases (Finset.mem_filter.mp hpq).2 with ⟨hN, hS, hne⟩ | ⟨hS, hN⟩ · have := runCount_del2_le_sub_three_NS x pq.1 pq.2 (by omega) (Finset.mem_sdiff.mp hN).1 hS (by omega); omega · have hne : pq.2.val ≠ pq.1.val + 1 := by rcases (Finset.mem_filter.mp hpq_nr).2.2 with ⟨hL, _⟩ | ⟨_, _, hnot⟩ · exact False.elim ((Finset.mem_sdiff.mp hL).2 (sandwichPosSet_subset_singletonPosSet x hS)) · intro hadj; exact hnot ⟨hS, hadj⟩ have := runCount_del2_le_sub_three_SN x pq.1 pq.2 (by omega) hS (Finset.mem_sdiff.mp hN).1 (by omega); omega exact (gEnv_anti M (one_le_runCount _) hdrop).trans (twoDelDualWeight_ge_gEnv x (del2_mem_del2Ball x pq.1 pq.2 hlt)) have hB_SS : ((bucketSS x).card : ℝ) * gEnv M (r - 4) ≤ ∑ pq ∈ bucketSS x, twoDelDualWeight (del2 x pq.1 pq.2) := by rw [← nsmul_eq_mul, ← Finset.sum_const] refine Finset.sum_le_sum fun pq hpq => ?_ have hpq_nr := (Finset.mem_filter.mp hpq).1 have hlt : pq.1.val < pq.2.val := (Finset.mem_filter.mp hpq_nr).2.1 have h12 := (Finset.mem_filter.mp hpq).2 have hne : pq.2.val ≠ pq.1.val + 1 := by rcases (Finset.mem_filter.mp hpq_nr).2.2 with ⟨hL, _⟩ | ⟨_, _, hnot⟩ · exact False.elim ((Finset.mem_sdiff.mp hL).2 (sandwichPosSet_subset_singletonPosSet x h12.1)) · intro hadj; exact hnot ⟨h12.1, hadj⟩ have hdrop : runCount (del2 x pq.1 pq.2) ≤ r - 4 := by have := runCount_del2_le_sub_four_SS x pq.1 pq.2 (by omega) h12.1 h12.2; omega exact (gEnv_anti M (one_le_runCount _) hdrop).trans (twoDelDualWeight_ge_gEnv x (del2_mem_del2Ball x pq.1 pq.2 hlt)) -- Convert to polynomial `polyB_k / polyD` and multiply by `D * r * (r + 1) > 0`! set Lr : ℝ := (L : ℝ) set Nr : ℝ := (N : ℝ) set Sr : ℝ := (S : ℝ) set rr : ℝ := (r : ℝ) set Mr : ℝ := (M : ℝ) have hrr : rr = Lr + Nr + Sr := by dsimp only [rr, Lr, Nr, Sr]; exact_mod_cast hpart.symm have hMr : Mr = Nr + 2 * Sr := by dsimp only [Mr, Nr, Sr]; exact_mod_cast hM_eq have hr10_r : (10 : ℝ) ≤ rr := by dsimp only [rr]; exact_mod_cast hr10 have hr1 : 0 < rr - 1 := by linarith have hr2 : 0 < rr - 2 := by linarith have hr3 : 0 < rr - 3 := by linarith have hD_pos : 0 < polyD Lr Nr Sr := by unfold polyD; rw [← hrr]; positivity have hScale_pos : 0 < rr * (rr + 1) * polyD Lr Nr Sr := by positivity -- Connect `gEnv M (r - k)` with `polyB_k / polyD * (2 / (r * (r + 1)))` have hg0 : (polyB0 Lr Nr Sr / polyD Lr Nr Sr) * (2 / (rr * (rr + 1))) ≤ gEnv M r := by have hEq : polyB0 Lr Nr Sr / polyD Lr Nr Sr = 1 - 2 * Mr / ((rr + 1) * (rr + 2)) := by unfold polyB0 polyD; rw [← hrr, ← hMr] field_simp [hr1.ne', hr2.ne', hr3.ne'] rw [hEq]; exact gEnv_ge_b0 r M hr10 have hg1 : (polyB1 Lr Nr Sr / polyD Lr Nr Sr) * (2 / (rr * (rr + 1))) ≤ gEnv M (r - 1) := by have hEq : polyB1 Lr Nr Sr / polyD Lr Nr Sr = 1 + 2 / (rr - 1) - 2 * Mr / (rr * (rr + 1)) := by unfold polyB1 polyD; rw [← hrr, ← hMr] field_simp [hr1.ne', hr2.ne', hr3.ne'] rw [hEq]; exact gEnv_ge_b1 r M hr10 have hg2 : (polyB2 Lr Nr Sr / polyD Lr Nr Sr) * (2 / (rr * (rr + 1))) ≤ gEnv M (r - 2) := by have hEq : polyB2 Lr Nr Sr / polyD Lr Nr Sr = 1 + 4 / (rr - 1) + 6 / ((rr - 1) * (rr - 2)) - 2 * Mr / ((rr - 1) * rr) := by unfold polyB2 polyD; rw [← hrr, ← hMr] field_simp [hr1.ne', hr2.ne', hr3.ne'] rw [hEq]; exact gEnv_ge_b2 r M hr10 have hg3 : (polyB3 Lr Nr Sr / polyD Lr Nr Sr) * (2 / (rr * (rr + 1))) ≤ gEnv M (r - 3) := by have hEq : polyB3 Lr Nr Sr / polyD Lr Nr Sr = 1 + 6 / (rr - 1) + 18 / ((rr - 1) * (rr - 2)) - 2 * Mr / ((rr - 1) * (rr - 2)) := by unfold polyB3 polyD; rw [← hrr, ← hMr] field_simp [hr1.ne', hr2.ne', hr3.ne'] rw [hEq]; exact gEnv_ge_b3 r M hr10 have hg4 : (polyB4 Lr Nr Sr / polyD Lr Nr Sr) * (2 / (rr * (rr + 1))) ≤ gEnv M (r - 4) := by have hEq : polyB4 Lr Nr Sr / polyD Lr Nr Sr = 1 + 8 / (rr - 1) + 36 / ((rr - 1) * (rr - 2)) - 2 * (rr + 1) * Mr / ((rr - 3) * (rr - 2) * (rr - 1)) := by unfold polyB4 polyD; rw [← hrr, ← hMr] field_simp [hr1.ne', hr2.ne', hr3.ne'] rw [hEq]; exact gEnv_ge_b4 r M hr10 -- Cast bucket sizes cleanly to `ℝ` have hLL_r : 2 * ((bucketLL x).card : ℝ) = Lr * (Lr + 1) := by dsimp only [Lr]; exact_mod_cast hLL have hLN_r : ((bucketLN x).card : ℝ) = Lr * Nr := by dsimp only [Lr, Nr]; exact_mod_cast hLN_c have hLS_r : ((bucketLS x).card : ℝ) + (eL : ℝ) = Lr * Sr := by dsimp only [Lr, Sr]; exact_mod_cast hLS_c have hNN_r : 2 * ((bucketNN x).card : ℝ) = Nr * (Nr - 1) := by have h1 : (2 * (bucketNN x).card : ℤ) = (N : ℤ) * ((N - 1 : ℕ) : ℤ) := by exact_mod_cast hNN_c have h2 : (N : ℤ) * ((N - 1 : ℕ) : ℤ) = (N : ℤ) * ((N : ℤ) - 1) := by cases N <;> simp rw [h2] at h1; dsimp only [Nr]; exact_mod_cast h1 have hNSa_r : ((bucketNSAdj x).card : ℝ) = (fN : ℝ) := by exact_mod_cast hNSa_c have hNSn_r : ((bucketNSNon x).card : ℝ) + (eN : ℝ) + (fN : ℝ) = Nr * Sr := by dsimp only [Nr, Sr]; exact_mod_cast hNSn_c have hSS_r : 2 * (((bucketSS x).card : ℝ) + (eS : ℝ)) = Sr * (Sr - 1) := by have h1 : (2 * ((bucketSS x).card + eS) : ℤ) = (S : ℤ) * ((S - 1 : ℕ) : ℤ) := by exact_mod_cast hSS_c have h2 : (S : ℤ) * ((S - 1 : ℕ) : ℤ) = (S : ℤ) * ((S : ℤ) - 1) := by cases S <;> simp rw [h2] at h1; dsimp only [Sr]; exact_mod_cast h1 -- Combine bucket lower bounds into `polyBase - 2*(eL*B2 + eN*B3 + eS*B4 + fN*(B3 - B2))` have hPoly_comb : (polyBase Lr Nr Sr - 2 * ((eL : ℝ) * polyB2 Lr Nr Sr + (eN : ℝ) * polyB3 Lr Nr Sr + (eS : ℝ) * polyB4 Lr Nr Sr + (fN : ℝ) * (polyB3 Lr Nr Sr - polyB2 Lr Nr Sr))) / (rr * (rr + 1) * polyD Lr Nr Sr) ≤ ∑ y ∈ del2Ball x, twoDelDualWeight y := by have tLL := mul_le_mul_of_nonneg_left hg0 (Nat.cast_nonneg (bucketLL x).card) have tLN := mul_le_mul_of_nonneg_left hg1 (Nat.cast_nonneg (bucketLN x).card) have tLS := mul_le_mul_of_nonneg_left hg2 (Nat.cast_nonneg (bucketLS x).card) have tNN := mul_le_mul_of_nonneg_left hg2 (Nat.cast_nonneg (bucketNN x).card) have tNSa := mul_le_mul_of_nonneg_left hg2 (Nat.cast_nonneg (bucketNSAdj x).card) have tNSn := mul_le_mul_of_nonneg_left hg3 (Nat.cast_nonneg (bucketNSNon x).card) have tSS := mul_le_mul_of_nonneg_left hg4 (Nat.cast_nonneg (bucketSS x).card) have hsum_le : ((bucketLL x).card : ℝ) * ((polyB0 Lr Nr Sr / polyD Lr Nr Sr) * (2 / (rr * (rr + 1)))) + ((bucketLN x).card : ℝ) * ((polyB1 Lr Nr Sr / polyD Lr Nr Sr) * (2 / (rr * (rr + 1)))) + ((bucketLS x).card : ℝ) * ((polyB2 Lr Nr Sr / polyD Lr Nr Sr) * (2 / (rr * (rr + 1)))) + ((bucketNN x).card : ℝ) * ((polyB2 Lr Nr Sr / polyD Lr Nr Sr) * (2 / (rr * (rr + 1)))) + ((bucketNSAdj x).card : ℝ) * ((polyB2 Lr Nr Sr / polyD Lr Nr Sr) * (2 / (rr * (rr + 1)))) + ((bucketNSNon x).card : ℝ) * ((polyB3 Lr Nr Sr / polyD Lr Nr Sr) * (2 / (rr * (rr + 1)))) + ((bucketSS x).card : ℝ) * ((polyB4 Lr Nr Sr / polyD Lr Nr Sr) * (2 / (rr * (rr + 1)))) ≤ ∑ y ∈ del2Ball x, twoDelDualWeight y := by linarith only [h7, hB_LL, hB_LN, hB_LS, hB_NN, hB_NSa, hB_NSn, hB_SS, tLL, tLN, tLS, tNN, tNSa, tNSn, tSS] have hident : ((bucketLL x).card : ℝ) * ((polyB0 Lr Nr Sr / polyD Lr Nr Sr) * (2 / (rr * (rr + 1)))) + ((bucketLN x).card : ℝ) * ((polyB1 Lr Nr Sr / polyD Lr Nr Sr) * (2 / (rr * (rr + 1)))) + ((bucketLS x).card : ℝ) * ((polyB2 Lr Nr Sr / polyD Lr Nr Sr) * (2 / (rr * (rr + 1)))) + ((bucketNN x).card : ℝ) * ((polyB2 Lr Nr Sr / polyD Lr Nr Sr) * (2 / (rr * (rr + 1)))) + ((bucketNSAdj x).card : ℝ) * ((polyB2 Lr Nr Sr / polyD Lr Nr Sr) * (2 / (rr * (rr + 1)))) + ((bucketNSNon x).card : ℝ) * ((polyB3 Lr Nr Sr / polyD Lr Nr Sr) * (2 / (rr * (rr + 1)))) + ((bucketSS x).card : ℝ) * ((polyB4 Lr Nr Sr / polyD Lr Nr Sr) * (2 / (rr * (rr + 1)))) = (polyBase Lr Nr Sr - 2 * ((eL : ℝ) * polyB2 Lr Nr Sr + (eN : ℝ) * polyB3 Lr Nr Sr + (eS : ℝ) * polyB4 Lr Nr Sr + (fN : ℝ) * (polyB3 Lr Nr Sr - polyB2 Lr Nr Sr))) / (rr * (rr + 1) * polyD Lr Nr Sr) := by have hbLL : ((bucketLL x).card : ℝ) = Lr * (Lr + 1) / 2 := by linarith only [hLL_r] have hbLS : ((bucketLS x).card : ℝ) = Lr * Sr - (eL : ℝ) := by linarith only [hLS_r] have hbNN : ((bucketNN x).card : ℝ) = Nr * (Nr - 1) / 2 := by linarith only [hNN_r] have hbNSn : ((bucketNSNon x).card : ℝ) = Nr * Sr - (eN : ℝ) - (fN : ℝ) := by linarith only [hNSn_r] have hbSS : ((bucketSS x).card : ℝ) = Sr * (Sr - 1) / 2 - (eS : ℝ) := by linarith only [hSS_r] rw [hbLL, hLN_r, hbLS, hbNN, hNSa_r, hbNSn, hbSS] unfold polyBase field_simp [hD_pos.ne'] ring linarith only [hsum_le, hident] -- Reduce to proving the numerator is `≥ rr * (rr + 1) * polyD Lr Nr Sr`! suffices hnum : rr * (rr + 1) * polyD Lr Nr Sr ≤ polyBase Lr Nr Sr - 2 * ((eL : ℝ) * polyB2 Lr Nr Sr + (eN : ℝ) * polyB3 Lr Nr Sr + (eS : ℝ) * polyB4 Lr Nr Sr + (fN : ℝ) * (polyB3 Lr Nr Sr - polyB2 Lr Nr Sr)) by have hdiv : (1 : ℝ) ≤ (polyBase Lr Nr Sr - 2 * ((eL : ℝ) * polyB2 Lr Nr Sr + (eN : ℝ) * polyB3 Lr Nr Sr + (eS : ℝ) * polyB4 Lr Nr Sr + (fN : ℝ) * (polyB3 Lr Nr Sr - polyB2 Lr Nr Sr))) / (rr * (rr + 1) * polyD Lr Nr Sr) := by rw [one_le_div hScale_pos]; exact hnum linarith only [hPoly_comb, hdiv] have hLN_r' : (2 : ℝ) ≤ Lr + Nr := by dsimp only [Lr, Nr]; exact_mod_cast hLN have hB23 := polyB2_le_polyB3 Lr Nr Sr (Nat.cast_nonneg _) (Nat.cast_nonneg _) (Nat.cast_nonneg _) hLN_r' (by linarith only [hrr, hr10_r]) have hB34 := polyB3_le_polyB4 Lr Nr Sr (Nat.cast_nonneg _) (Nat.cast_nonneg _) (Nat.cast_nonneg _) hLN_r' (by linarith only [hrr, hr10_r]) have hmaster := master_poly_feas L N S hLN (by omega) have hrp_r : (eL : ℝ) + (eN : ℝ) + (eS : ℝ) = Sr := by dsimp only [Sr]; exact_mod_cast hrp by_cases hS0 : S = 0 · have heL0 : eL = 0 := by omega have heN0 : eN = 0 := by omega have heS0 : eS = 0 := by omega have hfN0 : fN = 0 := by omega have h1 := hmaster.1 hS0 unfold polySlack1 at h1 rw [← hrr] at h1 simp only [heL0, heN0, heS0, hfN0, Nat.cast_zero, zero_mul, add_zero, mul_zero, sub_zero] linarith only [h1] · have hS1 : 1 ≤ S := by omega have heS_lt : eS ≤ S - 1 := by have := card_rightInSand_lt x hS1; omega have heS_r : (eS : ℝ) ≤ Sr - 1 := by have h1 : (eS : ℝ) + 1 ≤ Sr := by dsimp only [Sr]; exact_mod_cast (show eS + 1 ≤ S by omega) linarith only [h1] by_cases hN0 : N = 0 · have heN0 : eN = 0 := by rw [hN0, zero_mul] at hNSn_c; omega have hfN0 : fN = 0 := by omega have h2 := hmaster.2.1 hN0 hS1 unfold polySlack2 at h2 rw [← hrr] at h2 have hpen : (eL : ℝ) * polyB2 Lr Nr Sr + (eN : ℝ) * polyB3 Lr Nr Sr + (eS : ℝ) * polyB4 Lr Nr Sr + (fN : ℝ) * (polyB3 Lr Nr Sr - polyB2 Lr Nr Sr) ≤ polyB2 Lr Nr Sr + (Sr - 1) * polyB4 Lr Nr Sr := by simp only [heN0, hfN0, Nat.cast_zero, zero_mul, add_zero] at hrp_r ⊢ have hdiff : 0 ≤ (Sr - 1 - (eS : ℝ)) * (polyB4 Lr Nr Sr - polyB2 Lr Nr Sr) := mul_nonneg (by linarith only [heS_r]) (by linarith only [hB23, hB34]) have hident : polyB2 Lr Nr Sr + (Sr - 1) * polyB4 Lr Nr Sr - ((eL : ℝ) * polyB2 Lr Nr Sr + (eS : ℝ) * polyB4 Lr Nr Sr) = (Sr - 1 - (eS : ℝ)) * (polyB4 Lr Nr Sr - polyB2 Lr Nr Sr) := by have heL_eq : (eL : ℝ) = Sr - (eS : ℝ) := by linarith only [hrp_r] rw [heL_eq]; ring linarith only [hdiff, hident] linarith only [h2, hpen] · have hN1 : 1 ≤ N := by omega have hpen_e : (eL : ℝ) * polyB2 Lr Nr Sr + (eN : ℝ) * polyB3 Lr Nr Sr + (eS : ℝ) * polyB4 Lr Nr Sr ≤ polyB3 Lr Nr Sr + (Sr - 1) * polyB4 Lr Nr Sr := by have h1 : (eL : ℝ) * polyB2 Lr Nr Sr ≤ (eL : ℝ) * polyB3 Lr Nr Sr := mul_le_mul_of_nonneg_left hB23 (Nat.cast_nonneg _) have hdiff : 0 ≤ (Sr - 1 - (eS : ℝ)) * (polyB4 Lr Nr Sr - polyB3 Lr Nr Sr) := mul_nonneg (by linarith only [heS_r]) (by linarith only [hB34]) have hident : polyB3 Lr Nr Sr + (Sr - 1) * polyB4 Lr Nr Sr - (((eL : ℝ) + (eN : ℝ)) * polyB3 Lr Nr Sr + (eS : ℝ) * polyB4 Lr Nr Sr) = (Sr - 1 - (eS : ℝ)) * (polyB4 Lr Nr Sr - polyB3 Lr Nr Sr) := by have heLN : (eL : ℝ) + (eN : ℝ) = Sr - (eS : ℝ) := by linarith only [hrp_r] rw [heLN]; ring have h2 : (eL : ℝ) * polyB3 Lr Nr Sr + (eN : ℝ) * polyB3 Lr Nr Sr = ((eL : ℝ) + (eN : ℝ)) * polyB3 Lr Nr Sr := by ring linarith only [h1, h2, hdiff, hident] by_cases hNS : N ≤ S · have h3a := hmaster.2.2.1 hN1 hNS unfold polySlack3a at h3a rw [← hrr] at h3a have hf_le : (fN : ℝ) * (polyB3 Lr Nr Sr - polyB2 Lr Nr Sr) ≤ Nr * (polyB3 Lr Nr Sr - polyB2 Lr Nr Sr) := mul_le_mul_of_nonneg_right (by dsimp only [Nr]; exact_mod_cast hfN_le_N) (by linarith only [hB23]) linarith only [h3a, hpen_e, hf_le] · have hSN : S < N := by omega have h3b := hmaster.2.2.2 hS1 hSN unfold polySlack3b at h3b rw [← hrr] at h3b have hf_le : (fN : ℝ) * (polyB3 Lr Nr Sr - polyB2 Lr Nr Sr) ≤ Sr * (polyB3 Lr Nr Sr - polyB2 Lr Nr Sr) := mul_le_mul_of_nonneg_right (by dsimp only [Sr]; exact_mod_cast (show fN ≤ S by omega)) (by linarith only [hB23]) linarith only [h3b, hpen_e, hf_le] /-- **Universal Feasibility of Kulkarni–Kiyavash's `kk2DualWeight`**: Immediate corollary of `twoDelDualWeight_feasible` and `twoDelDualWeight ≤ kk2DualWeight`. -/ theorem kk2DualWeight_feasible {q m : ℕ} (x : Word q (m + 3)) : 1 ≤ ∑ y ∈ del2Ball x, kk2DualWeight y := (twoDelDualWeight_feasible x).trans (Finset.sum_le_sum fun y _ => twoDelDualWeight_le_kk2DualWeight y) /-- **Exact Second-Order LP-Dual Upper Bound for Every `q`-ary 2-Deletion Code**: For EVERY `q ≥ 1`, EVERY `m ≥ 0` (length `n = m + 3 ≥ 3`), and EVERY 2-deletion code `C ⊆ Word q (m + 3)`, `|C| ≤ ∑ y : Word q (m + 1), twoDelDualWeight y`. -/ theorem twoDelCode_card_le_twoDelDualWeight_sum {q m : ℕ} (C : Finset (Word q (m + 3))) (hC : IsTwoDelCode C) : (C.card : ℝ) ≤ ∑ y : Word q (m + 1), twoDelDualWeight y := twoDelCode_card_le_dual_sum C hC twoDelDualWeight twoDelDualWeight_nonneg twoDelDualWeight_feasible /-- **Strict Exact Dual Dominance over Kulkarni–Kiyavash (2013) for ALL `q ≥ 2, n ≥ 12`**: For EVERY `q ≥ 2` and EVERY `m ≥ 9` (`n = m + 3 ≥ 12`), the exact second-order dual sum is STRICTLY smaller than Kulkarni–Kiyavash's dual sum `∑ y, kk2DualWeight y`. -/ theorem twoDelDualWeight_sum_lt_kk2DualWeight_sum {q m : ℕ} (hq : 2 ≤ q) (hm : 9 ≤ m) : ∑ y : Word q (m + 1), twoDelDualWeight y < ∑ y : Word q (m + 1), kk2DualWeight y := by refine Finset.sum_lt_sum (fun y _ => twoDelDualWeight_le_kk2DualWeight y) ?_ -- Explicit witness: alternating word `y₀ i = i.val % 2`, with `r(y₀) = r₁(y₀) = m + 1 ≥ 10`! let y0 : Word q (m + 1) := fun i => ⟨i.val % 2, by omega⟩ refine ⟨y0, Finset.mem_univ y0, ?_⟩ have hbd : boundarySet y0 = Finset.univ := by ext i simp only [boundarySet, Finset.mem_filter, Finset.mem_univ, true_and, iff_true, y0] intro h have hval := congrArg Fin.val h simp only [Fin.val_castSucc, Fin.val_succ] at hval omega have hr : runCount y0 = m + 1 := by unfold runCount; rw [hbd, Finset.card_univ, Fintype.card_fin] have hsing : singletonPosSet y0 = Finset.univ := by ext i simp only [singletonPosSet, Finset.mem_filter, Finset.mem_univ, true_and, iff_true, y0] refine ⟨fun h0 heq => ?_, fun h1 heq => ?_⟩ · have hval := congrArg Fin.val heq; simp at hval; omega · have hval := congrArg Fin.val heq; simp at hval; omega have hr1 : singletonRunCount y0 = m + 1 := by unfold singletonRunCount; rw [hsing, Finset.card_univ, Fintype.card_fin] unfold twoDelDualWeight kk2DualWeight; dsimp only rw [if_neg (by omega), hr, hr1] have hpos_num : (0 : ℝ) < ((m + 1 - 2 : ℕ) : ℝ) := by exact_mod_cast (show 0 < m + 1 - 2 by omega) have hpos_sand : (0 : ℝ) ≤ ((sandwichCount y0 - 4 : ℕ) : ℝ) := Nat.cast_nonneg _ have hpos_den : (0 : ℝ) < (((m + 1 : ℕ) : ℝ) + 1) * (((m + 1 : ℕ) : ℝ) + 2) * (((m + 1 : ℕ) : ℝ) + 3) * (((m + 1 : ℕ) : ℝ) + 4) := by positivity have hpen : 0 < 4 * (((m + 1 - 2 : ℕ) : ℝ) + ((sandwichCount y0 - 4 : ℕ) : ℝ)) / ((((m + 1 : ℕ) : ℝ) + 1) * (((m + 1 : ℕ) : ℝ) + 2) * (((m + 1 : ℕ) : ℝ) + 3) * (((m + 1 : ℕ) : ℝ) + 4)) := by exact div_pos (by linarith) hpos_den linarith end SecondOrderDual /-! ## Section 4: Exact Fiber Counting of Run Counts, Singletons, and Sandwiches -/ section FiberCounting /-- Set of words `y : Word q (m + 1)` with exact boundary set `B : Finset (Fin m)`. -/ def boundaryFiber (q m : ℕ) (B : Finset (Fin m)) : Finset (Word q (m + 1)) := Finset.univ.filter fun y : Word q (m + 1) => boundarySet y = B /-- Set of words `y : Word q (m + 1)` with exact run count `r`. -/ def wordsWithRuns (q m r : ℕ) : Finset (Word q (m + 1)) := Finset.univ.filter fun y : Word q (m + 1) => runCount y = r /-- Append a final letter `c : Fin q` to `z : Word q (m + 1)` to obtain `Word q (m + 2)`. -/ def appendLetter {q m : ℕ} (z : Word q (m + 1)) (c : Fin q) : Word q (m + 2) := fun i => if h : i.val < m + 1 then z ⟨i.val, h⟩ else c /-- Drop the final letter of `y : Word q (m + 2)` to obtain `Word q (m + 1)`. -/ def initWord {q m : ℕ} (y : Word q (m + 2)) : Word q (m + 1) := fun i => y i.castSucc lemma initWord_appendLetter {q m : ℕ} (z : Word q (m + 1)) (c : Fin q) : initWord (appendLetter z c) = z := by ext i simp only [initWord, appendLetter, Fin.val_castSucc, i.isLt, dite_true, Fin.eta] lemma appendLetter_last {q m : ℕ} (z : Word q (m + 1)) (c : Fin q) : appendLetter z c (Fin.last (m + 1)) = c := by simp [appendLetter] lemma appendLetter_initWord {q m : ℕ} (y : Word q (m + 2)) : appendLetter (initWord y) (y (Fin.last (m + 1))) = y := by ext i simp only [appendLetter, initWord] split_ifs with h · rfl · have hi : i = Fin.last (m + 1) := Fin.ext (by simp; omega) rw [hi] /-- Restrict a boundary set `B : Finset (Fin (m + 1))` to the first `m` positions. -/ def restrictB {m : ℕ} (B : Finset (Fin (m + 1))) : Finset (Fin m) := Finset.univ.filter fun j : Fin m => j.castSucc ∈ B lemma boundarySet_initWord {q m : ℕ} (y : Word q (m + 2)) : boundarySet (initWord y) = restrictB (boundarySet y) := by ext j simp only [boundarySet, restrictB, Finset.mem_filter, Finset.mem_univ, true_and] dsimp only [initWord] have h1 : j.castSucc.castSucc = (j.castSucc : Fin (m + 1)).castSucc := rfl have h2 : j.succ.castSucc = (j.castSucc : Fin (m + 1)).succ := Fin.ext (by simp) rw [h1, h2] lemma last_mem_boundarySet_iff {q m : ℕ} (y : Word q (m + 2)) : Fin.last m ∈ boundarySet y ↔ (initWord y) (Fin.last m) ≠ y (Fin.last (m + 1)) := by simp only [boundarySet, initWord, Finset.mem_filter, Finset.mem_univ, true_and] have h1 : (Fin.last m).succ = Fin.last (m + 1) := Fin.ext (by simp) rw [h1] lemma card_restrictB_not_mem {m : ℕ} (B : Finset (Fin (m + 1))) (hnot : Fin.last m ∉ B) : (restrictB B).card = B.card := by refine Finset.card_bij (fun (j : Fin m) _ => j.castSucc) ?_ ?_ ?_ · intro j hj simp only [restrictB, Finset.mem_filter, Finset.mem_univ, true_and] at hj exact hj · intro a _ b _ h; exact Fin.castSucc_injective _ h · intro j hj have hlt : j.val < m := by have hne : j ≠ Fin.last m := fun h => hnot (h ▸ hj) have := Fin.val_ne_of_ne hne simp at this; omega refine ⟨⟨j.val, hlt⟩, ?_, Fin.ext rfl⟩ simp only [restrictB, Finset.mem_filter, Finset.mem_univ, true_and] exact hj lemma card_restrictB_mem {m : ℕ} (B : Finset (Fin (m + 1))) (hmem : Fin.last m ∈ B) : (restrictB B).card + 1 = B.card := by have h1 : (restrictB B).card = (B.erase (Fin.last m)).card := by refine Finset.card_bij (fun (j : Fin m) _ => j.castSucc) ?_ ?_ ?_ · intro j hj simp only [restrictB, Finset.mem_filter, Finset.mem_univ, true_and] at hj exact Finset.mem_erase.mpr ⟨Fin.castSucc_ne_last j, hj⟩ · intro a _ b _ h; exact Fin.castSucc_injective _ h · intro j hj rcases Finset.mem_erase.mp hj with ⟨hne, hB⟩ have hlt : j.val < m := by have := Fin.val_ne_of_ne hne; simp at this; omega refine ⟨⟨j.val, hlt⟩, ?_, Fin.ext rfl⟩ simp only [restrictB, Finset.mem_filter, Finset.mem_univ, true_and] exact hB rw [h1, Finset.card_erase_of_mem hmem] have hpos : 1 ≤ B.card := Finset.one_le_card.mpr ⟨Fin.last m, hmem⟩ omega lemma boundarySet_appendLetter_eq {q m : ℕ} (z : Word q (m + 1)) (c : Fin q) (B : Finset (Fin (m + 1))) : boundarySet (appendLetter z c) = B ↔ boundarySet z = restrictB B ∧ (Fin.last m ∈ B ↔ z (Fin.last m) ≠ c) := by constructor · intro h refine ⟨?_, ?_⟩ · rw [← initWord_appendLetter z c, boundarySet_initWord, h] · rw [← h, last_mem_boundarySet_iff, initWord_appendLetter, appendLetter_last] · rintro ⟨hres, hlast⟩ ext j by_cases hj : j = Fin.last m · subst hj rw [last_mem_boundarySet_iff, initWord_appendLetter, appendLetter_last] exact hlast.symm · have hlt : j.val < m := by have := Fin.val_ne_of_ne hj; simp at this; omega let j' : Fin m := ⟨j.val, hlt⟩ have hj_eq : j = j'.castSucc := Fin.ext rfl rw [hj_eq] have h1 : j'.castSucc ∈ boundarySet (appendLetter z c) ↔ j' ∈ boundarySet (initWord (appendLetter z c)) := by rw [boundarySet_initWord] simp only [restrictB, Finset.mem_filter, Finset.mem_univ, true_and] rw [h1, initWord_appendLetter, hres] simp only [restrictB, Finset.mem_filter, Finset.mem_univ, true_and] /-- **Exact Fiber Cardinality**: For every `q ≥ 2`, `m ≥ 0`, and boundary set `B : Finset (Fin m)`, there are EXACTLY `q * (q - 1) ^ B.card` words `y : Word q (m + 1)` with `boundarySet y = B`. -/ theorem card_boundaryFiber {q : ℕ} (_hq : 2 ≤ q) : ∀ (m : ℕ) (B : Finset (Fin m)), (boundaryFiber q m B).card = q * (q - 1) ^ B.card := by intro m induction m with | zero => intro B have hB : B = ∅ := Finset.eq_empty_of_isEmpty B subst hB have hfib : boundaryFiber q 0 ∅ = Finset.univ := by ext y simp only [boundaryFiber, Finset.mem_filter, Finset.mem_univ, true_and, iff_true] exact Finset.eq_empty_of_isEmpty _ rw [hfib, Finset.card_univ, Fintype.card_fun, Fintype.card_fin, Fintype.card_fin] simp | succ m ih => intro B by_cases hlast : Fin.last m ∈ B · -- `Fin.last m ∈ B`: `(q - 1)` choices for the final letter `c ≠ z (Fin.last m)`! let T := (boundaryFiber q m (restrictB B)).sigma (fun z => (Finset.univ : Finset (Fin q)).erase (z (Fin.last m))) have hbij : (boundaryFiber q (m + 1) B).card = T.card := by refine Finset.card_bij (fun y _ => ⟨initWord y, y (Fin.last (m + 1))⟩) ?_ ?_ ?_ · intro y hy simp only [boundaryFiber, Finset.mem_filter, Finset.mem_univ, true_and] at hy simp only [T, Finset.mem_sigma, boundaryFiber, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_erase, and_true] have h1 : boundarySet (initWord y) = restrictB B := by rw [boundarySet_initWord, hy] have h2 : (initWord y) (Fin.last m) ≠ y (Fin.last (m + 1)) := by rw [← last_mem_boundarySet_iff, hy]; exact hlast exact ⟨h1, h2.symm⟩ · intro y₁ _ y₂ _ heq have hinit : initWord y₁ = initWord y₂ := congrArg Sigma.fst heq have hfin : y₁ (Fin.last (m + 1)) = y₂ (Fin.last (m + 1)) := by have := congrArg Sigma.snd heq; simpa using this rw [← appendLetter_initWord y₁, ← appendLetter_initWord y₂, hinit, hfin] · intro p hp rcases p with ⟨z, c⟩ simp only [T, Finset.mem_sigma, boundaryFiber, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_erase, and_true] at hp refine ⟨appendLetter z c, ?_, ?_⟩ · simp only [boundaryFiber, Finset.mem_filter, Finset.mem_univ, true_and] rw [boundarySet_appendLetter_eq] exact ⟨hp.1, ⟨fun _ => hp.2.symm, fun _ => hlast⟩⟩ · exact Sigma.ext (initWord_appendLetter z c) (by simp only [appendLetter_last, heq_eq_eq]) rw [hbij, Finset.card_sigma] have hsum : ∑ z ∈ boundaryFiber q m (restrictB B), ((Finset.univ : Finset (Fin q)).erase (z (Fin.last m))).card = (boundaryFiber q m (restrictB B)).card * (q - 1) := by rw [Finset.sum_const_nat (fun z _ => by rw [Finset.card_erase_of_mem (Finset.mem_univ _), Finset.card_univ, Fintype.card_fin])] rw [hsum, ih (restrictB B), ← card_restrictB_mem B hlast, pow_succ] ring · -- `Fin.last m ∉ B`: final letter is forced to equal `z (Fin.last m)`! have hbij : (boundaryFiber q (m + 1) B).card = (boundaryFiber q m (restrictB B)).card := by refine Finset.card_bij (fun y _ => initWord y) ?_ ?_ ?_ · intro y hy simp only [boundaryFiber, Finset.mem_filter, Finset.mem_univ, true_and] at hy ⊢ rw [boundarySet_initWord, hy] · intro y₁ hy₁ y₂ hy₂ hinit simp only [boundaryFiber, Finset.mem_filter, Finset.mem_univ, true_and] at hy₁ hy₂ have h1 : (initWord y₁) (Fin.last m) = y₁ (Fin.last (m + 1)) := by by_contra hne; exact hlast (hy₁ ▸ (last_mem_boundarySet_iff y₁).mpr hne) have h2 : (initWord y₂) (Fin.last m) = y₂ (Fin.last (m + 1)) := by by_contra hne; exact hlast (hy₂ ▸ (last_mem_boundarySet_iff y₂).mpr hne) rw [← appendLetter_initWord y₁, ← appendLetter_initWord y₂, hinit, ← h1, ← h2, hinit] · intro z hz simp only [boundaryFiber, Finset.mem_filter, Finset.mem_univ, true_and] at hz refine ⟨appendLetter z (z (Fin.last m)), ?_, initWord_appendLetter z _⟩ simp only [boundaryFiber, Finset.mem_filter, Finset.mem_univ, true_and] rw [boundarySet_appendLetter_eq] exact ⟨hz, ⟨fun h => False.elim (hlast h), fun h => False.elim (h rfl)⟩⟩ rw [hbij, ih (restrictB B), card_restrictB_not_mem B hlast] /-- **Exact Sandwich-Fiber Cardinality**: Given `B : Finset (Fin m)` and an interior position `i` with both `⟨i - 1, _⟩ ∈ B` and `⟨i, _⟩ ∈ B`, the number of words `y ∈ boundaryFiber q m B` satisfying `y (i - 1) = y (i + 1)` is EXACTLY `q * (q - 1) ^ (B.card - 1)`! -/ theorem card_boundaryFiber_sandwich {q : ℕ} (hq : 2 ≤ q) : ∀ (m : ℕ) (B : Finset (Fin m)) (i : Fin (m + 1)) (h0 : 0 < i.val) (h1 : i.val + 1 < m + 1) (_hL : (⟨i.val - 1, by omega⟩ : Fin m) ∈ B) (_hR : (⟨i.val, by omega⟩ : Fin m) ∈ B), ((boundaryFiber q m B).filter (fun y => y ⟨i.val - 1, by omega⟩ = y ⟨i.val + 1, h1⟩)).card = q * (q - 1) ^ (B.card - 1) := by intro m induction m with | zero => intro B i h0 h1 _ _; omega | succ m ih => intro B i h0 h1 hL hR by_cases hi_last : i.val = m · -- `i.val = m`: `Fin.last m ∈ B` and final letter is FORCED to equal `z ⟨m - 1, _⟩`! have hlast : Fin.last m ∈ B := by have : (⟨i.val, by omega⟩ : Fin (m + 1)) = Fin.last m := Fin.ext (by simp; omega) rwa [← this] have hbij : ((boundaryFiber q (m + 1) B).filter (fun y => y ⟨i.val - 1, by omega⟩ = y ⟨i.val + 1, h1⟩)).card = (boundaryFiber q m (restrictB B)).card := by refine Finset.card_bij (fun y _ => initWord y) ?_ ?_ ?_ · intro y hy simp only [Finset.mem_filter, boundaryFiber, Finset.mem_univ, true_and] at hy ⊢ rw [boundarySet_initWord, hy.1] · intro y₁ hy₁ y₂ hy₂ hinit simp only [Finset.mem_filter, boundaryFiber, Finset.mem_univ, true_and] at hy₁ hy₂ have hfin1 : y₁ (Fin.last (m + 1)) = (initWord y₁) ⟨i.val - 1, by omega⟩ := by have he1 : (⟨i.val + 1, h1⟩ : Fin (m + 2)) = Fin.last (m + 1) := Fin.ext (by simp; omega) rw [← he1, ← hy₁.2]; rfl have hfin2 : y₂ (Fin.last (m + 1)) = (initWord y₂) ⟨i.val - 1, by omega⟩ := by have he1 : (⟨i.val + 1, h1⟩ : Fin (m + 2)) = Fin.last (m + 1) := Fin.ext (by simp; omega) rw [← he1, ← hy₂.2]; rfl rw [← appendLetter_initWord y₁, ← appendLetter_initWord y₂, hfin1, hfin2, hinit] · intro z hz simp only [boundaryFiber, Finset.mem_filter, Finset.mem_univ, true_and] at hz let c := z ⟨i.val - 1, by omega⟩ refine ⟨appendLetter z c, ?_, initWord_appendLetter z c⟩ simp only [Finset.mem_filter, boundaryFiber, Finset.mem_univ, true_and] refine ⟨?_, ?_⟩ · rw [boundarySet_appendLetter_eq] refine ⟨hz, ⟨fun _ => ?_, fun _ => hlast⟩⟩ -- `z (Fin.last m) ≠ z ⟨m - 1, _⟩` because `⟨m - 1, _⟩ ∈ restrictB B = boundarySet z`! have hm1 : (⟨i.val - 1, by omega⟩ : Fin m) ∈ restrictB B := by simp only [restrictB, Finset.mem_filter, Finset.mem_univ, true_and] exact hL rw [← hz] at hm1 simp only [boundarySet, Finset.mem_filter, Finset.mem_univ, true_and] at hm1 have hcs : (⟨i.val - 1, by omega⟩ : Fin m).castSucc = ⟨i.val - 1, by omega⟩ := rfl have hsc : (⟨i.val - 1, by omega⟩ : Fin m).succ = Fin.last m := Fin.ext (by simp; omega) rw [hcs, hsc] at hm1 exact hm1.symm · have hlt_m1 : i.val - 1 < m + 1 := by omega have hge_p1 : ¬ (i.val + 1 < m + 1) := by omega simp only [appendLetter, hlt_m1, dite_true, hge_p1, dite_false, c] rw [hbij, card_boundaryFiber hq m (restrictB B), ← card_restrictB_mem B hlast] simp · -- `i.val < m`: condition depends ONLY on `initWord y`! have hi_lt : i.val + 1 < m + 1 := by omega let i' : Fin (m + 1) := ⟨i.val, by omega⟩ have hL' : (⟨i'.val - 1, by dsimp only [i']; omega⟩ : Fin m) ∈ restrictB B := by simp only [restrictB, Finset.mem_filter, Finset.mem_univ, true_and, i']; exact hL have hR' : (⟨i'.val, by dsimp only [i']; omega⟩ : Fin m) ∈ restrictB B := by simp only [restrictB, Finset.mem_filter, Finset.mem_univ, true_and, i']; exact hR have hB'_pos : 1 ≤ (restrictB B).card := Finset.one_le_card.mpr ⟨⟨i'.val, by dsimp only [i']; omega⟩, hR'⟩ let S_prev := (boundaryFiber q m (restrictB B)).filter (fun z => z ⟨i'.val - 1, by omega⟩ = z ⟨i'.val + 1, hi_lt⟩) have hih : S_prev.card = q * (q - 1) ^ ((restrictB B).card - 1) := ih (restrictB B) i' (by dsimp only [i']; omega) hi_lt hL' hR' by_cases hlast : Fin.last m ∈ B · let T := S_prev.sigma (fun z => (Finset.univ : Finset (Fin q)).erase (z (Fin.last m))) have hbij : ((boundaryFiber q (m + 1) B).filter (fun y => y ⟨i.val - 1, by omega⟩ = y ⟨i.val + 1, h1⟩)).card = T.card := by refine Finset.card_bij (fun y _ => ⟨initWord y, y (Fin.last (m + 1))⟩) ?_ ?_ ?_ · intro y hy simp only [Finset.mem_filter, boundaryFiber, Finset.mem_univ, true_and] at hy simp only [T, S_prev, Finset.mem_sigma, Finset.mem_filter, boundaryFiber, Finset.mem_univ, true_and, Finset.mem_erase, and_true] have h1' : boundarySet (initWord y) = restrictB B := by rw [boundarySet_initWord, hy.1] have h2' : (initWord y) (Fin.last m) ≠ y (Fin.last (m + 1)) := by rw [← last_mem_boundarySet_iff, hy.1]; exact hlast exact ⟨⟨h1', hy.2⟩, h2'.symm⟩ · intro y₁ _ y₂ _ heq have hinit : initWord y₁ = initWord y₂ := congrArg Sigma.fst heq have hfin : y₁ (Fin.last (m + 1)) = y₂ (Fin.last (m + 1)) := by have := congrArg Sigma.snd heq; simpa using this rw [← appendLetter_initWord y₁, ← appendLetter_initWord y₂, hinit, hfin] · intro p hp rcases p with ⟨z, c⟩ simp only [T, S_prev, Finset.mem_sigma, Finset.mem_filter, boundaryFiber, Finset.mem_univ, true_and, Finset.mem_erase, and_true] at hp refine ⟨appendLetter z c, ?_, Sigma.ext (initWord_appendLetter z c) (by simp only [appendLetter_last, heq_eq_eq])⟩ simp only [Finset.mem_filter, boundaryFiber, Finset.mem_univ, true_and] refine ⟨(boundarySet_appendLetter_eq z c B).mpr ⟨hp.1.1, ⟨fun _ => hp.2.symm, fun _ => hlast⟩⟩, ?_⟩ have hlt1 : i.val - 1 < m + 1 := by omega have hlt2 : i.val + 1 < m + 1 := by omega simp only [appendLetter, hlt1, hlt2, dite_true] exact hp.1.2 rw [hbij, Finset.card_sigma] have hsum : ∑ z ∈ S_prev, ((Finset.univ : Finset (Fin q)).erase (z (Fin.last m))).card = S_prev.card * (q - 1) := Finset.sum_const_nat (fun z _ => by rw [Finset.card_erase_of_mem (Finset.mem_univ _), Finset.card_univ, Fintype.card_fin]) rw [hsum, hih, ← card_restrictB_mem B hlast] have hexp : (restrictB B).card - 1 + 1 = (restrictB B).card := by omega calc q * (q - 1) ^ ((restrictB B).card - 1) * (q - 1) = q * (q - 1) ^ ((restrictB B).card - 1 + 1) := by rw [pow_succ]; ring _ = q * (q - 1) ^ (restrictB B).card := by rw [hexp] _ = q * (q - 1) ^ ((restrictB B).card + 1 - 1) := by simp · have hbij : ((boundaryFiber q (m + 1) B).filter (fun y => y ⟨i.val - 1, by omega⟩ = y ⟨i.val + 1, h1⟩)).card = S_prev.card := by refine Finset.card_bij (fun y _ => initWord y) ?_ ?_ ?_ · intro y hy simp only [Finset.mem_filter, boundaryFiber, Finset.mem_univ, true_and] at hy simp only [S_prev, Finset.mem_filter, boundaryFiber, Finset.mem_univ, true_and] exact ⟨by rw [boundarySet_initWord, hy.1], hy.2⟩ · intro y₁ hy₁ y₂ hy₂ hinit simp only [Finset.mem_filter, boundaryFiber, Finset.mem_univ, true_and] at hy₁ hy₂ have h1' : (initWord y₁) (Fin.last m) = y₁ (Fin.last (m + 1)) := by by_contra hne; exact hlast (hy₁.1 ▸ (last_mem_boundarySet_iff y₁).mpr hne) have h2' : (initWord y₂) (Fin.last m) = y₂ (Fin.last (m + 1)) := by by_contra hne; exact hlast (hy₂.1 ▸ (last_mem_boundarySet_iff y₂).mpr hne) rw [← appendLetter_initWord y₁, ← appendLetter_initWord y₂, hinit, ← h1', ← h2', hinit] · intro z hz simp only [S_prev, Finset.mem_filter, boundaryFiber, Finset.mem_univ, true_and] at hz refine ⟨appendLetter z (z (Fin.last m)), ?_, initWord_appendLetter z _⟩ simp only [Finset.mem_filter, boundaryFiber, Finset.mem_univ, true_and] refine ⟨(boundarySet_appendLetter_eq z _ B).mpr ⟨hz.1, ⟨fun h => False.elim (hlast h), fun h => False.elim (h rfl)⟩⟩, ?_⟩ have hlt1 : i.val - 1 < m + 1 := by omega have hlt2 : i.val + 1 < m + 1 := by omega simp only [appendLetter, hlt1, hlt2, dite_true] exact hz.2 rw [hbij, hih, card_restrictB_not_mem B hlast] /-- `wordsWithRuns q m (k + 1)` is the disjoint union of `boundaryFiber q m B` over `B ∈ powersetCard k univ`. -/ lemma wordsWithRuns_eq_biUnion (q m k : ℕ) : wordsWithRuns q m (k + 1) = ((Finset.univ : Finset (Fin m)).powersetCard k).biUnion (fun B => boundaryFiber q m B) := by ext y simp only [wordsWithRuns, boundaryFiber, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_biUnion, Finset.mem_powersetCard] unfold runCount constructor · intro hr exact ⟨boundarySet y, ⟨Finset.subset_univ _, by omega⟩, rfl⟩ · rintro ⟨B, ⟨_, hB⟩, rfl⟩ omega lemma pairwiseDisjoint_boundaryFiber (q m k : ℕ) : Set.PairwiseDisjoint (↑((Finset.univ : Finset (Fin m)).powersetCard k) : Set (Finset (Fin m))) (fun B => boundaryFiber q m B) := by intro B₁ _ B₂ _ hne dsimp only [Function.onFun] rw [Finset.disjoint_left] intro y hy₁ hy₂ simp only [boundaryFiber, Finset.mem_filter, Finset.mem_univ, true_and] at hy₁ hy₂ exact hne (hy₁.symm.trans hy₂) /-- Exact count of words in `Word q (m + 1)` with `k + 1` runs: `C(m, k) * q * (q - 1)^k`. -/ theorem card_wordsWithRuns {q : ℕ} (hq : 2 ≤ q) (m k : ℕ) : (wordsWithRuns q m (k + 1)).card = m.choose k * (q * (q - 1) ^ k) := by rw [wordsWithRuns_eq_biUnion, Finset.card_biUnion (pairwiseDisjoint_boundaryFiber q m k)] have hsum : ∑ B ∈ (Finset.univ : Finset (Fin m)).powersetCard k, (boundaryFiber q m B).card = ∑ _B ∈ (Finset.univ : Finset (Fin m)).powersetCard k, (q * (q - 1) ^ k) := by refine Finset.sum_congr rfl fun B hB => ?_ rw [card_boundaryFiber hq m B, (Finset.mem_powersetCard.mp hB).2] rw [hsum, Finset.sum_const, smul_eq_mul, Finset.card_powersetCard, Finset.card_univ, Fintype.card_fin] /-- Helper: count of `k`-subsets of `Fin m` containing a given element `a : Fin m`. -/ private lemma card_powersetCard_mem {m k : ℕ} (a : Fin m) (hk : 1 ≤ k) : (((Finset.univ : Finset (Fin m)).powersetCard k).filter (fun B => a ∈ B)).card = (m - 1).choose (k - 1) := by let U := (Finset.univ : Finset (Fin m)).erase a have hbij : (((Finset.univ : Finset (Fin m)).powersetCard k).filter (fun B => a ∈ B)).card = (U.powersetCard (k - 1)).card := by refine Finset.card_bij (fun B _ => B.erase a) ?_ ?_ ?_ · intro B hB simp only [Finset.mem_filter, Finset.mem_powersetCard] at hB refine Finset.mem_powersetCard.mpr ⟨?_, ?_⟩ · intro x hx rcases Finset.mem_erase.mp hx with ⟨hne, _⟩ exact Finset.mem_erase.mpr ⟨hne, Finset.mem_univ x⟩ · rw [Finset.card_erase_of_mem hB.2, hB.1.2] · intro B₁ hB₁ B₂ hB₂ heq simp only [Finset.mem_filter] at hB₁ hB₂ rw [← Finset.insert_erase hB₁.2, ← Finset.insert_erase hB₂.2, heq] · intro S hS rcases Finset.mem_powersetCard.mp hS with ⟨hSU, hSc⟩ have ha_not : a ∉ S := fun ha => (Finset.mem_erase.mp (hSU ha)).1 rfl refine ⟨insert a S, ?_, Finset.erase_insert ha_not⟩ simp only [Finset.mem_filter, Finset.mem_powersetCard] refine ⟨⟨Finset.subset_univ _, ?_⟩, Finset.mem_insert_self a S⟩ rw [Finset.card_insert_of_notMem ha_not, hSc]; omega rw [hbij, Finset.card_powersetCard, Finset.card_erase_of_mem (Finset.mem_univ a), Finset.card_univ, Fintype.card_fin] /-- Helper: count of `k`-subsets of `Fin m` containing two distinct elements `a ≠ b`. -/ private lemma card_powersetCard_mem_two {m k : ℕ} (a b : Fin m) (hab : a ≠ b) (hk : 2 ≤ k) : (((Finset.univ : Finset (Fin m)).powersetCard k).filter (fun B => a ∈ B ∧ b ∈ B)).card = (m - 2).choose (k - 2) := by let U := ((Finset.univ : Finset (Fin m)).erase a).erase b have hbij : (((Finset.univ : Finset (Fin m)).powersetCard k).filter (fun B => a ∈ B ∧ b ∈ B)).card = (U.powersetCard (k - 2)).card := by refine Finset.card_bij (fun B _ => (B.erase a).erase b) ?_ ?_ ?_ · intro B hB simp only [Finset.mem_filter, Finset.mem_powersetCard] at hB refine Finset.mem_powersetCard.mpr ⟨?_, ?_⟩ · intro x hx rcases Finset.mem_erase.mp hx with ⟨hxb, hx1⟩ rcases Finset.mem_erase.mp hx1 with ⟨hxa, _⟩ exact Finset.mem_erase.mpr ⟨hxb, Finset.mem_erase.mpr ⟨hxa, Finset.mem_univ x⟩⟩ · have hb_mem : b ∈ B.erase a := Finset.mem_erase.mpr ⟨hab.symm, hB.2.2⟩ rw [Finset.card_erase_of_mem hb_mem, Finset.card_erase_of_mem hB.2.1, hB.1.2] omega · intro B₁ hB₁ B₂ hB₂ heq simp only [Finset.mem_filter] at hB₁ hB₂ have hb1 : b ∈ B₁.erase a := Finset.mem_erase.mpr ⟨hab.symm, hB₁.2.2⟩ have hb2 : b ∈ B₂.erase a := Finset.mem_erase.mpr ⟨hab.symm, hB₂.2.2⟩ have heq1 : B₁.erase a = B₂.erase a := by rw [← Finset.insert_erase hb1, ← Finset.insert_erase hb2, heq] rw [← Finset.insert_erase hB₁.2.1, ← Finset.insert_erase hB₂.2.1, heq1] · intro S hS rcases Finset.mem_powersetCard.mp hS with ⟨hSU, hSc⟩ have hb_not : b ∉ S := fun hb => (Finset.mem_erase.mp (hSU hb)).1 rfl have ha_not : a ∉ S := fun ha => (Finset.mem_erase.mp (Finset.mem_erase.mp (hSU ha)).2).1 rfl have ha_not' : a ∉ insert b S := by simp only [Finset.mem_insert, not_or]; exact ⟨hab, ha_not⟩ refine ⟨insert a (insert b S), ?_, ?_⟩ · simp only [Finset.mem_filter, Finset.mem_powersetCard] refine ⟨⟨Finset.subset_univ _, ?_⟩, Finset.mem_insert_self a _, Finset.mem_insert_of_mem (Finset.mem_insert_self b _)⟩ rw [Finset.card_insert_of_notMem ha_not', Finset.card_insert_of_notMem hb_not, hSc]; omega · rw [Finset.erase_insert ha_not', Finset.erase_insert hb_not] rw [hbij, Finset.card_powersetCard] have hb_in : b ∈ (Finset.univ : Finset (Fin m)).erase a := Finset.mem_erase.mpr ⟨hab.symm, Finset.mem_univ b⟩ rw [Finset.card_erase_of_mem hb_in, Finset.card_erase_of_mem (Finset.mem_univ a), Finset.card_univ, Fintype.card_fin] have : m - 1 - 1 = m - 2 := by omega rw [this] /-- Exact binomial identities `m * C(m - 1, k - 1) = k * C(m, k)` and `m * (m - 1) * C(m - 2, k - 2) = k * (k - 1) * C(m, k)`. -/ private lemma mul_choose_sub_one {m k : ℕ} (hm : 1 ≤ m) (hk : 1 ≤ k) : m * (m - 1).choose (k - 1) = k * m.choose k := by obtain ⟨m', rfl⟩ := Nat.exists_eq_add_of_le' hm obtain ⟨k', rfl⟩ := Nat.exists_eq_add_of_le' hk simp only [Nat.add_sub_cancel] have h := Nat.add_one_mul_choose_eq m' k' linarith private lemma mul_choose_sub_two {m k : ℕ} (hm : 2 ≤ m) (hk : 2 ≤ k) : m * ((m - 1) * (m - 2).choose (k - 2)) = k * (k - 1) * m.choose k := by have h1 := mul_choose_sub_one (m := m - 1) (k := k - 1) (by omega) (by omega) have h2 := mul_choose_sub_one (m := m) (k := k) (by omega) (by omega) have hsub1 : m - 1 - 1 = m - 2 := by omega have hsub2 : k - 1 - 1 = k - 2 := by omega rw [hsub1, hsub2] at h1 calc m * ((m - 1) * (m - 2).choose (k - 2)) = m * ((k - 1) * (m - 1).choose (k - 1)) := by rw [h1] _ = (k - 1) * (m * (m - 1).choose (k - 1)) := by ring _ = (k - 1) * (k * m.choose k) := by rw [h2] _ = k * (k - 1) * m.choose k := by ring /-- **Exact Average of `singletonRunCount` Over Every Run-Count Fiber (`m ≥ 2, k ≥ 2`)**: `m * ∑_{r(y) = k + 1} singletonRunCount y = k * (k + 1) * C(m, k) * q * (q - 1)^k`. -/ theorem sum_singletonRunCount_over_runs {q : ℕ} (hq : 2 ≤ q) {m k : ℕ} (hm : 2 ≤ m) (hk : 2 ≤ k) : m * (∑ y ∈ wordsWithRuns q m (k + 1), singletonRunCount y) = k * (k + 1) * m.choose k * (q * (q - 1) ^ k) := by let singB : Finset (Fin m) → ℕ := fun B => ((Finset.univ : Finset (Fin (m + 1))).filter fun i : Fin (m + 1) => (∀ h : 0 < i.val, (⟨i.val - 1, by omega⟩ : Fin m) ∈ B) ∧ (∀ h : i.val + 1 < m + 1, (⟨i.val, by omega⟩ : Fin m) ∈ B)).card have hsing_eq : ∀ (B : Finset (Fin m)) (y : Word q (m + 1)), y ∈ boundaryFiber q m B → singletonRunCount y = singB B := by intro B y hy simp only [boundaryFiber, Finset.mem_filter, Finset.mem_univ, true_and] at hy unfold singletonRunCount congr 1; ext i simp only [singletonPosSet, Finset.mem_filter, Finset.mem_univ, true_and] rw [← hy] constructor · rintro ⟨hL, hR⟩ refine ⟨fun h0 => ?_, fun h1 => ?_⟩ · simp only [boundarySet, Finset.mem_filter, Finset.mem_univ, true_and] have hsc : (⟨i.val - 1, by omega⟩ : Fin m).succ = i := Fin.ext (by simp; omega) rw [hsc]; exact hL h0 · simp only [boundarySet, Finset.mem_filter, Finset.mem_univ, true_and] exact hR h1 · rintro ⟨hL, hR⟩ refine ⟨fun h0 => ?_, fun h1 => ?_⟩ · have := hL h0 simp only [boundarySet, Finset.mem_filter, Finset.mem_univ, true_and] at this have hsc : (⟨i.val - 1, by omega⟩ : Fin m).succ = i := Fin.ext (by simp; omega) rwa [hsc] at this · have := hR h1 simp only [boundarySet, Finset.mem_filter, Finset.mem_univ, true_and] at this exact this rw [wordsWithRuns_eq_biUnion, Finset.sum_biUnion (pairwiseDisjoint_boundaryFiber q m k)] have hfib_sum : ∑ B ∈ (Finset.univ : Finset (Fin m)).powersetCard k, ∑ y ∈ boundaryFiber q m B, singletonRunCount y = (∑ B ∈ (Finset.univ : Finset (Fin m)).powersetCard k, singB B) * (q * (q - 1) ^ k) := by rw [Finset.sum_mul] refine Finset.sum_congr rfl fun B hB => ?_ rw [Finset.sum_congr rfl (fun y hy => hsing_eq B y hy), Finset.sum_const, smul_eq_mul, card_boundaryFiber hq m B, (Finset.mem_powersetCard.mp hB).2, mul_comm] rw [hfib_sum] -- Swap sum over `B` and `i : Fin (m + 1)` have hswap : ∑ B ∈ (Finset.univ : Finset (Fin m)).powersetCard k, singB B = 2 * (m - 1).choose (k - 1) + (m - 1) * (m - 2).choose (k - 2) := by have hcard_eq : ∀ B : Finset (Fin m), singB B = ∑ i : Fin (m + 1), if (∀ h : 0 < i.val, (⟨i.val - 1, by omega⟩ : Fin m) ∈ B) ∧ (∀ h : i.val + 1 < m + 1, (⟨i.val, by omega⟩ : Fin m) ∈ B) then 1 else 0 := fun B => by dsimp only [singB]; rw [Finset.card_filter] simp_rw [hcard_eq] rw [Finset.sum_comm] -- Split `Fin (m + 1)` into `{0}`, `{Fin.last m}`, and interior positions! let i0 : Fin (m + 1) := 0 let im : Fin (m + 1) := Fin.last m have hi0m : i0 ≠ im := fun h => by have := congrArg Fin.val h; simp [i0, im] at this; omega let IntPos := ((Finset.univ : Finset (Fin (m + 1))).erase i0).erase im have hsplit : ∑ i : Fin (m + 1), (∑ B ∈ (Finset.univ : Finset (Fin m)).powersetCard k, if (∀ h : 0 < i.val, (⟨i.val - 1, by omega⟩ : Fin m) ∈ B) ∧ (∀ h : i.val + 1 < m + 1, (⟨i.val, by omega⟩ : Fin m) ∈ B) then 1 else 0) = (∑ B ∈ (Finset.univ : Finset (Fin m)).powersetCard k, if (∀ h : 0 < i0.val, (⟨i0.val - 1, by omega⟩ : Fin m) ∈ B) ∧ (∀ h : i0.val + 1 < m + 1, (⟨i0.val, by omega⟩ : Fin m) ∈ B) then 1 else 0) + (∑ B ∈ (Finset.univ : Finset (Fin m)).powersetCard k, if (∀ h : 0 < im.val, (⟨im.val - 1, by omega⟩ : Fin m) ∈ B) ∧ (∀ h : im.val + 1 < m + 1, (⟨im.val, by omega⟩ : Fin m) ∈ B) then 1 else 0) + ∑ i ∈ IntPos, (∑ B ∈ (Finset.univ : Finset (Fin m)).powersetCard k, if (∀ h : 0 < i.val, (⟨i.val - 1, by omega⟩ : Fin m) ∈ B) ∧ (∀ h : i.val + 1 < m + 1, (⟨i.val, by omega⟩ : Fin m) ∈ B) then 1 else 0) := by have h1 := Finset.add_sum_erase Finset.univ (fun i : Fin (m + 1) => ∑ B ∈ (Finset.univ : Finset (Fin m)).powersetCard k, if (∀ h : 0 < i.val, (⟨i.val - 1, by omega⟩ : Fin m) ∈ B) ∧ (∀ h : i.val + 1 < m + 1, (⟨i.val, by omega⟩ : Fin m) ∈ B) then 1 else 0) (Finset.mem_univ i0) have h2 := Finset.add_sum_erase ((Finset.univ : Finset (Fin (m + 1))).erase i0) (fun i : Fin (m + 1) => ∑ B ∈ (Finset.univ : Finset (Fin m)).powersetCard k, if (∀ h : 0 < i.val, (⟨i.val - 1, by omega⟩ : Fin m) ∈ B) ∧ (∀ h : i.val + 1 < m + 1, (⟨i.val, by omega⟩ : Fin m) ∈ B) then 1 else 0) (Finset.mem_erase.mpr ⟨hi0m.symm, Finset.mem_univ im⟩) change _ = _ + _ + ∑ i ∈ ((Finset.univ : Finset (Fin (m + 1))).erase i0).erase im, _ omega rw [hsplit] have ht0 : (∑ B ∈ (Finset.univ : Finset (Fin m)).powersetCard k, if (∀ h : 0 < i0.val, (⟨i0.val - 1, by omega⟩ : Fin m) ∈ B) ∧ (∀ h : i0.val + 1 < m + 1, (⟨i0.val, by omega⟩ : Fin m) ∈ B) then 1 else 0) = (m - 1).choose (k - 1) := by rw [← Finset.card_filter, ← card_powersetCard_mem (⟨0, by omega⟩ : Fin m) (by omega)] congr 1; ext B simp only [Finset.mem_filter, i0] constructor · rintro ⟨hB, _, hR⟩; exact ⟨hB, hR (by simp; omega)⟩ · rintro ⟨hB, hmem⟩; exact ⟨hB, fun h => by simp at h, fun _ => hmem⟩ have htm : (∑ B ∈ (Finset.univ : Finset (Fin m)).powersetCard k, if (∀ h : 0 < im.val, (⟨im.val - 1, by omega⟩ : Fin m) ∈ B) ∧ (∀ h : im.val + 1 < m + 1, (⟨im.val, by omega⟩ : Fin m) ∈ B) then 1 else 0) = (m - 1).choose (k - 1) := by rw [← Finset.card_filter, ← card_powersetCard_mem (⟨m - 1, by omega⟩ : Fin m) (by omega)] congr 1; ext B simp only [Finset.mem_filter, im] constructor · rintro ⟨hB, hL, _⟩; exact ⟨hB, hL (by simp; omega)⟩ · rintro ⟨hB, hmem⟩; exact ⟨hB, fun _ => hmem, fun h => by simp at h⟩ have htInt : ∑ i ∈ IntPos, (∑ B ∈ (Finset.univ : Finset (Fin m)).powersetCard k, if (∀ h : 0 < i.val, (⟨i.val - 1, by omega⟩ : Fin m) ∈ B) ∧ (∀ h : i.val + 1 < m + 1, (⟨i.val, by omega⟩ : Fin m) ∈ B) then 1 else 0) = (m - 1) * (m - 2).choose (k - 2) := by have hconst : ∀ i ∈ IntPos, (∑ B ∈ (Finset.univ : Finset (Fin m)).powersetCard k, if (∀ h : 0 < i.val, (⟨i.val - 1, by omega⟩ : Fin m) ∈ B) ∧ (∀ h : i.val + 1 < m + 1, (⟨i.val, by omega⟩ : Fin m) ∈ B) then 1 else 0) = (m - 2).choose (k - 2) := by intro i hi rcases Finset.mem_erase.mp hi with ⟨hne_m, hi1⟩ rcases Finset.mem_erase.mp hi1 with ⟨hne_0, _⟩ have h0 : 0 < i.val := by have := Fin.val_ne_of_ne hne_0; simp only [i0, Fin.val_zero] at this; omega have h1 : i.val + 1 < m + 1 := by have := Fin.val_ne_of_ne hne_m; simp only [im, Fin.val_last] at this; omega have hne_ab : (⟨i.val - 1, by omega⟩ : Fin m) ≠ ⟨i.val, by omega⟩ := fun h => by have := congrArg Fin.val h; dsimp only at this; omega rw [← Finset.card_filter, ← card_powersetCard_mem_two (⟨i.val - 1, by omega⟩ : Fin m) ⟨i.val, by omega⟩ hne_ab hk] congr 1; ext B simp only [Finset.mem_filter] exact ⟨fun ⟨hB, hL, hR⟩ => ⟨hB, hL h0, hR h1⟩, fun ⟨hB, hL, hR⟩ => ⟨hB, fun _ => hL, fun _ => hR⟩⟩ rw [Finset.sum_congr rfl hconst, Finset.sum_const, smul_eq_mul] have hcard_Int : IntPos.card = m - 1 := by simp only [IntPos] rw [Finset.card_erase_of_mem (Finset.mem_erase.mpr ⟨hi0m.symm, Finset.mem_univ im⟩), Finset.card_erase_of_mem (Finset.mem_univ i0), Finset.card_univ, Fintype.card_fin] omega rw [hcard_Int] rw [ht0, htm, htInt]; ring rw [hswap] have hm1 := mul_choose_sub_one (m := m) (k := k) (by omega) (by omega) have hm2 := mul_choose_sub_two hm hk calc m * ((2 * (m - 1).choose (k - 1) + (m - 1) * (m - 2).choose (k - 2)) * (q * (q - 1) ^ k)) = (2 * (m * (m - 1).choose (k - 1)) + m * ((m - 1) * (m - 2).choose (k - 2))) * (q * (q - 1) ^ k) := by ring _ = (2 * (k * m.choose k) + k * (k - 1) * m.choose k) * (q * (q - 1) ^ k) := by rw [hm1, hm2] _ = (2 * k + k * (k - 1)) * m.choose k * (q * (q - 1) ^ k) := by ring _ = k * (k + 1) * m.choose k * (q * (q - 1) ^ k) := by congr 2; obtain ⟨k', rfl⟩ := Nat.exists_eq_add_of_le' hk rw [show k' + 2 - 1 = k' + 1 by omega]; ring /-- **Exact Average of `sandwichCount` Over Every Run-Count Fiber (`m ≥ 2, k ≥ 2`)**: `m * ∑_{r(y) = k + 1} sandwichCount y = k * (k - 1) * C(m, k) * q * (q - 1)^(k - 1)`. -/ theorem sum_sandwichCount_over_runs {q : ℕ} (hq : 2 ≤ q) {m k : ℕ} (hm : 2 ≤ m) (hk : 2 ≤ k) : m * (∑ y ∈ wordsWithRuns q m (k + 1), sandwichCount y) = k * (k - 1) * m.choose k * (q * (q - 1) ^ (k - 1)) := by rw [wordsWithRuns_eq_biUnion, Finset.sum_biUnion (pairwiseDisjoint_boundaryFiber q m k)] -- For each `B`, expand `∑ y ∈ boundaryFiber q m B, sandwichCount y` by position `i` have hfib_sand : ∀ B ∈ (Finset.univ : Finset (Fin m)).powersetCard k, ∑ y ∈ boundaryFiber q m B, sandwichCount y = (∑ i : Fin (m + 1), if ∃ (_ : 0 < i.val) (_ : i.val + 1 < m + 1), (⟨i.val - 1, by omega⟩ : Fin m) ∈ B ∧ (⟨i.val, by omega⟩ : Fin m) ∈ B then 1 else 0) * (q * (q - 1) ^ (k - 1)) := by intro B hB have hBk : B.card = k := (Finset.mem_powersetCard.mp hB).2 have h1 : ∑ y ∈ boundaryFiber q m B, sandwichCount y = ∑ i : Fin (m + 1), ((boundaryFiber q m B).filter (fun y => i ∈ sandwichPosSet y)).card := by simp_rw [sandwichCount] have hrewrite : ∀ y : Word q (m + 1), (sandwichPosSet y).card = ∑ i : Fin (m + 1), if i ∈ sandwichPosSet y then 1 else 0 := fun y => by rw [← Finset.card_filter] congr 1; ext i; simp simp_rw [hrewrite] rw [Finset.sum_comm] refine Finset.sum_congr rfl fun i _ => ?_ rw [Finset.card_filter] rw [h1, Finset.sum_mul] refine Finset.sum_congr rfl fun i _ => ?_ split_ifs with hcond · rcases hcond with ⟨h0, h1', hL, hR⟩ rw [one_mul, ← hBk, ← card_boundaryFiber_sandwich hq m B i h0 h1' hL hR] congr 1; ext y simp only [Finset.mem_filter, boundaryFiber, Finset.mem_univ, true_and, sandwichPosSet] constructor · rintro ⟨hyB, ⟨_, _, heq, _⟩⟩; exact ⟨hyB, heq⟩ · rintro ⟨hyB, heq⟩ refine ⟨hyB, ⟨h0, h1', heq, ?_⟩⟩ rw [← hyB] at hR simp only [boundarySet, Finset.mem_filter, Finset.mem_univ, true_and] at hR exact hR · rw [zero_mul, Finset.card_eq_zero, Finset.filter_eq_empty_iff] intro y hyB hsand simp only [boundaryFiber, Finset.mem_filter, Finset.mem_univ, true_and] at hyB simp only [sandwichPosSet, Finset.mem_filter, Finset.mem_univ, true_and] at hsand rcases hsand with ⟨h0, h1', heq, hne⟩ apply hcond refine ⟨h0, h1', ?_, ?_⟩ · rw [← hyB] simp only [boundarySet, Finset.mem_filter, Finset.mem_univ, true_and] have hcs : (⟨i.val - 1, by omega⟩ : Fin m).castSucc = ⟨i.val - 1, by omega⟩ := rfl have hsc : (⟨i.val - 1, by omega⟩ : Fin m).succ = i := Fin.ext (by simp; omega) rw [hcs, hsc, heq]; exact hne.symm · rw [← hyB] simp only [boundarySet, Finset.mem_filter, Finset.mem_univ, true_and] exact hne rw [Finset.sum_congr rfl hfib_sand, ← Finset.sum_mul] -- Swap sum over `B` and `i : Fin (m + 1)` have hswap : ∑ B ∈ (Finset.univ : Finset (Fin m)).powersetCard k, (∑ i : Fin (m + 1), if ∃ (_ : 0 < i.val) (_ : i.val + 1 < m + 1), (⟨i.val - 1, by omega⟩ : Fin m) ∈ B ∧ (⟨i.val, by omega⟩ : Fin m) ∈ B then 1 else 0) = (m - 1) * (m - 2).choose (k - 2) := by rw [Finset.sum_comm] let IntPos := (Finset.univ : Finset (Fin (m + 1))).filter (fun i : Fin (m + 1) => 0 < i.val ∧ i.val + 1 < m + 1) have hzero_out : ∀ i : Fin (m + 1), i ∉ IntPos → (∑ B ∈ (Finset.univ : Finset (Fin m)).powersetCard k, if ∃ (_ : 0 < i.val) (_ : i.val + 1 < m + 1), (⟨i.val - 1, by omega⟩ : Fin m) ∈ B ∧ (⟨i.val, by omega⟩ : Fin m) ∈ B then 1 else 0) = 0 := by intro i hi simp only [IntPos, Finset.mem_filter, Finset.mem_univ, true_and] at hi refine Finset.sum_eq_zero fun B _ => ?_ rw [if_neg]; rintro ⟨h0, h1', _⟩; exact hi ⟨h0, h1'⟩ rw [← Finset.sum_subset (Finset.subset_univ IntPos) (fun i _ hi => hzero_out i hi)] have hconst : ∀ i ∈ IntPos, (∑ B ∈ (Finset.univ : Finset (Fin m)).powersetCard k, if ∃ (_ : 0 < i.val) (_ : i.val + 1 < m + 1), (⟨i.val - 1, by omega⟩ : Fin m) ∈ B ∧ (⟨i.val, by omega⟩ : Fin m) ∈ B then 1 else 0) = (m - 2).choose (k - 2) := by intro i hi simp only [IntPos, Finset.mem_filter, Finset.mem_univ, true_and] at hi rcases hi with ⟨h0, h1'⟩ have hne_ab : (⟨i.val - 1, by omega⟩ : Fin m) ≠ ⟨i.val, by omega⟩ := fun h => by have := congrArg Fin.val h; dsimp only at this; omega rw [← Finset.card_filter, ← card_powersetCard_mem_two (⟨i.val - 1, by omega⟩ : Fin m) ⟨i.val, by omega⟩ hne_ab hk] congr 1; ext B simp only [Finset.mem_filter] exact ⟨fun ⟨hB, _, _, hL, hR⟩ => ⟨hB, hL, hR⟩, fun ⟨hB, hL, hR⟩ => ⟨hB, h0, h1', hL, hR⟩⟩ rw [Finset.sum_congr rfl hconst, Finset.sum_const, smul_eq_mul] have hcard_Int : IntPos.card = m - 1 := by have hbij : IntPos.card = (Finset.univ : Finset (Fin (m - 1))).card := by refine Finset.card_bij (fun i hi => ⟨i.val - 1, by simp only [IntPos, Finset.mem_filter, Finset.mem_univ, true_and] at hi; omega⟩) ?_ ?_ ?_ · intro _ _; exact Finset.mem_univ _ · intro a ha b hb h simp only [IntPos, Finset.mem_filter, Finset.mem_univ, true_and] at ha hb have := congrArg Fin.val h; dsimp only at this; exact Fin.ext (by omega) · intro j _ refine ⟨⟨j.val + 1, by omega⟩, ?_, Fin.ext (by simp)⟩ simp only [IntPos, Finset.mem_filter, Finset.mem_univ, true_and]; omega rw [hbij, Finset.card_univ, Fintype.card_fin] rw [hcard_Int] rw [hswap] have hm2 := mul_choose_sub_two hm hk calc m * ((m - 1) * (m - 2).choose (k - 2) * (q * (q - 1) ^ (k - 1))) = (m * ((m - 1) * (m - 2).choose (k - 2))) * (q * (q - 1) ^ (k - 1)) := by ring _ = k * (k - 1) * m.choose k * (q * (q - 1) ^ (k - 1)) := by rw [hm2] end FiberCounting /-! ## Section 5: Exact Closed-Form Kulkarni–Kiyavash Bound & Second-Order Improvement -/ section ClosedFormBounds /-- Binomial absorption identities for shifts of `2`, `3`, and `4`. -/ private lemma choose_shift2 (m k : ℕ) : (m.choose k : ℝ) / (((k : ℝ) + 1) * ((k : ℝ) + 2)) = (((m + 2).choose (k + 2) : ℕ) : ℝ) / (((m : ℝ) + 1) * ((m : ℝ) + 2)) := by have h1 : (m + 1) * m.choose k = (m + 1).choose (k + 1) * (k + 1) := Nat.add_one_mul_choose_eq m k have h2 : (m + 2) * (m + 1).choose (k + 1) = (m + 2).choose (k + 2) * (k + 2) := Nat.add_one_mul_choose_eq (m + 1) (k + 1) have hnat : (m + 1) * (m + 2) * m.choose k = (m + 2).choose (k + 2) * ((k + 1) * (k + 2)) := by calc (m + 1) * (m + 2) * m.choose k = (m + 2) * ((m + 1) * m.choose k) := by ring _ = (m + 2) * ((m + 1).choose (k + 1) * (k + 1)) := by rw [h1] _ = ((m + 2) * (m + 1).choose (k + 1)) * (k + 1) := by ring _ = (m + 2).choose (k + 2) * (k + 2) * (k + 1) := by rw [h2] _ = (m + 2).choose (k + 2) * ((k + 1) * (k + 2)) := by ring have hreal : (((m : ℝ) + 1) * ((m : ℝ) + 2)) * (m.choose k : ℝ) = (((m + 2).choose (k + 2) : ℕ) : ℝ) * (((k : ℝ) + 1) * ((k : ℝ) + 2)) := by exact_mod_cast hnat rw [div_eq_div_iff (by positivity) (by positivity)] linarith private lemma choose_shift3 (m k : ℕ) : (m.choose k : ℝ) / (((k : ℝ) + 1) * ((k : ℝ) + 2) * ((k : ℝ) + 3)) = (((m + 3).choose (k + 3) : ℕ) : ℝ) / (((m : ℝ) + 1) * ((m : ℝ) + 2) * ((m : ℝ) + 3)) := by have h2 := choose_shift2 m k have h3 : (m + 3) * (m + 2).choose (k + 2) = (m + 3).choose (k + 3) * (k + 3) := Nat.add_one_mul_choose_eq (m + 2) (k + 2) have h3r : ((m : ℝ) + 3) * (((m + 2).choose (k + 2) : ℕ) : ℝ) = (((m + 3).choose (k + 3) : ℕ) : ℝ) * ((k : ℝ) + 3) := by exact_mod_cast h3 have hdiv : (((m + 2).choose (k + 2) : ℕ) : ℝ) / ((k : ℝ) + 3) = (((m + 3).choose (k + 3) : ℕ) : ℝ) / ((m : ℝ) + 3) := by rw [div_eq_div_iff (by positivity) (by positivity)]; linarith calc (m.choose k : ℝ) / (((k : ℝ) + 1) * ((k : ℝ) + 2) * ((k : ℝ) + 3)) = ((m.choose k : ℝ) / (((k : ℝ) + 1) * ((k : ℝ) + 2))) / ((k : ℝ) + 3) := by rw [div_mul_eq_div_div] _ = ((((m + 2).choose (k + 2) : ℕ) : ℝ) / (((m : ℝ) + 1) * ((m : ℝ) + 2))) / ((k : ℝ) + 3) := by rw [h2] _ = ((((m + 2).choose (k + 2) : ℕ) : ℝ) / ((k : ℝ) + 3)) / (((m : ℝ) + 1) * ((m : ℝ) + 2)) := by ring _ = ((((m + 3).choose (k + 3) : ℕ) : ℝ) / ((m : ℝ) + 3)) / (((m : ℝ) + 1) * ((m : ℝ) + 2)) := by rw [hdiv] _ = (((m + 3).choose (k + 3) : ℕ) : ℝ) / (((m : ℝ) + 1) * ((m : ℝ) + 2) * ((m : ℝ) + 3)) := by rw [div_div, mul_comm ((m : ℝ) + 3)] private lemma choose_shift4 (m k : ℕ) : (m.choose k : ℝ) / (((k : ℝ) + 1) * ((k : ℝ) + 2) * ((k : ℝ) + 3) * ((k : ℝ) + 4)) = (((m + 4).choose (k + 4) : ℕ) : ℝ) / (((m : ℝ) + 1) * ((m : ℝ) + 2) * ((m : ℝ) + 3) * ((m : ℝ) + 4)) := by have h3 := choose_shift3 m k have h4 : (m + 4) * (m + 3).choose (k + 3) = (m + 4).choose (k + 4) * (k + 4) := Nat.add_one_mul_choose_eq (m + 3) (k + 3) have h4r : ((m : ℝ) + 4) * (((m + 3).choose (k + 3) : ℕ) : ℝ) = (((m + 4).choose (k + 4) : ℕ) : ℝ) * ((k : ℝ) + 4) := by exact_mod_cast h4 have hdiv : (((m + 3).choose (k + 3) : ℕ) : ℝ) / ((k : ℝ) + 4) = (((m + 4).choose (k + 4) : ℕ) : ℝ) / ((m : ℝ) + 4) := by rw [div_eq_div_iff (by positivity) (by positivity)]; linarith calc (m.choose k : ℝ) / (((k : ℝ) + 1) * ((k : ℝ) + 2) * ((k : ℝ) + 3) * ((k : ℝ) + 4)) = ((m.choose k : ℝ) / (((k : ℝ) + 1) * ((k : ℝ) + 2) * ((k : ℝ) + 3))) / ((k : ℝ) + 4) := by rw [div_mul_eq_div_div] _ = ((((m + 3).choose (k + 3) : ℕ) : ℝ) / (((m : ℝ) + 1) * ((m : ℝ) + 2) * ((m : ℝ) + 3))) / ((k : ℝ) + 4) := by rw [h3] _ = ((((m + 3).choose (k + 3) : ℕ) : ℝ) / ((k : ℝ) + 4)) / (((m : ℝ) + 1) * ((m : ℝ) + 2) * ((m : ℝ) + 3)) := by ring _ = ((((m + 4).choose (k + 4) : ℕ) : ℝ) / ((m : ℝ) + 4)) / (((m : ℝ) + 1) * ((m : ℝ) + 2) * ((m : ℝ) + 3)) := by rw [hdiv] _ = (((m + 4).choose (k + 4) : ℕ) : ℝ) / (((m : ℝ) + 1) * ((m : ℝ) + 2) * ((m : ℝ) + 3) * ((m : ℝ) + 4)) := by rw [div_div, mul_comm ((m : ℝ) + 4)] /-- Partition `Finset.univ : Finset (Word q (m + 1))` by `runCount y = k + 1` (`k ∈ range (m + 1)`). -/ lemma sum_univ_by_runs {q m : ℕ} (f : Word q (m + 1) → ℝ) : ∑ y : Word q (m + 1), f y = ∑ k ∈ Finset.range (m + 1), ∑ y ∈ wordsWithRuns q m (k + 1), f y := by have hunion : (Finset.univ : Finset (Word q (m + 1))) = (Finset.range (m + 1)).biUnion (fun k => wordsWithRuns q m (k + 1)) := by ext y simp only [Finset.mem_univ, true_iff, Finset.mem_biUnion, Finset.mem_range, wordsWithRuns, Finset.mem_filter, true_and] have h1 := one_le_runCount y have h2 := runCount_le_len y exact ⟨runCount y - 1, by omega, by omega⟩ have hdisj : Set.PairwiseDisjoint (↑(Finset.range (m + 1)) : Set ℕ) (fun k => wordsWithRuns q m (k + 1)) := by intro k₁ _ k₂ _ hne dsimp only [Function.onFun] rw [Finset.disjoint_left] intro y hy₁ hy₂ simp only [wordsWithRuns, Finset.mem_filter, Finset.mem_univ, true_and] at hy₁ hy₂ omega conv_lhs => rw [hunion] exact Finset.sum_biUnion hdisj /-- **Exact Closed Form of Kulkarni–Kiyavash's (2013) Dual Sum**: For EVERY `q ≥ 2` and `m ≥ 0` (`n = m + 3`), `∑ y : Word q (m + 1), kk2DualWeight y = 2q / ((q - 1)^2 (m + 1)(m + 2)) * (q^(m + 2) - 1 - (m + 2)(q - 1))`. -/ theorem kk2DualWeight_sum_eq {q m : ℕ} (hq : 2 ≤ q) : ∑ y : Word q (m + 1), kk2DualWeight y = 2 * (q : ℝ) / (((q : ℝ) - 1) ^ 2 * ((m : ℝ) + 1) * ((m : ℝ) + 2)) * ((q : ℝ) ^ (m + 2) - 1 - ((m : ℝ) + 2) * ((q : ℝ) - 1)) := by have hq1 : (0 : ℝ) < (q : ℝ) - 1 := by have : (2 : ℝ) ≤ (q : ℝ) := by exact_mod_cast hq linarith rw [sum_univ_by_runs] have hterm : ∀ k ∈ Finset.range (m + 1), ∑ y ∈ wordsWithRuns q m (k + 1), kk2DualWeight y = 2 * (q : ℝ) / (((q : ℝ) - 1) ^ 2 * ((m : ℝ) + 1) * ((m : ℝ) + 2)) * ((((m + 2).choose (k + 2) : ℕ) : ℝ) * ((q : ℝ) - 1) ^ (k + 2)) := by intro k _ have hconst : ∀ y ∈ wordsWithRuns q m (k + 1), kk2DualWeight y = 2 / (((k : ℝ) + 1) * ((k : ℝ) + 2)) := by intro y hy simp only [wordsWithRuns, Finset.mem_filter, Finset.mem_univ, true_and] at hy unfold kk2DualWeight; rw [hy]; push_cast; ring rw [Finset.sum_congr rfl hconst, Finset.sum_const, nsmul_eq_mul, card_wordsWithRuns hq m k] have hcast_q : ((q - 1 : ℕ) : ℝ) = (q : ℝ) - 1 := by rw [Nat.cast_sub (by omega)]; simp push_cast; rw [hcast_q] have hsh := choose_shift2 m k calc (m.choose k : ℝ) * ((q : ℝ) * ((q : ℝ) - 1) ^ k) * (2 / (((k : ℝ) + 1) * ((k : ℝ) + 2))) = 2 * (q : ℝ) * ((q : ℝ) - 1) ^ k * ((m.choose k : ℝ) / (((k : ℝ) + 1) * ((k : ℝ) + 2))) := by ring _ = 2 * (q : ℝ) * ((q : ℝ) - 1) ^ k * ((((m + 2).choose (k + 2) : ℕ) : ℝ) / (((m : ℝ) + 1) * ((m : ℝ) + 2))) := by rw [hsh] _ = 2 * (q : ℝ) / (((q : ℝ) - 1) ^ 2 * ((m : ℝ) + 1) * ((m : ℝ) + 2)) * ((((m + 2).choose (k + 2) : ℕ) : ℝ) * ((q : ℝ) - 1) ^ (k + 2)) := by rw [pow_add]; field_simp [hq1.ne'] rw [Finset.sum_congr rfl hterm, ← Finset.mul_sum] congr 1 -- Evaluate `∑ k ∈ range (m + 1), C(m + 2, k + 2) * (q - 1)^(k + 2)` via binomial theorem! have hbinom : ∑ j ∈ Finset.range (m + 3), (((m + 2).choose j : ℕ) : ℝ) * ((q : ℝ) - 1) ^ j = (q : ℝ) ^ (m + 2) := by have h := add_pow ((q : ℝ) - 1) (1 : ℝ) (m + 2) have hqeq : (q : ℝ) - 1 + 1 = (q : ℝ) := by ring rw [hqeq] at h rw [h] refine Finset.sum_congr rfl fun j _ => by ring have hsplit : ∑ j ∈ Finset.range (m + 3), (((m + 2).choose j : ℕ) : ℝ) * ((q : ℝ) - 1) ^ j = 1 + ((m : ℝ) + 2) * ((q : ℝ) - 1) + ∑ k ∈ Finset.range (m + 1), (((m + 2).choose (k + 2) : ℕ) : ℝ) * ((q : ℝ) - 1) ^ (k + 2) := by have h1 : Finset.range (m + 3) = Finset.range 2 ∪ Finset.Ico 2 (m + 3) := by ext j; simp only [Finset.mem_union, Finset.mem_range, Finset.mem_Ico]; omega have hdisj : Disjoint (Finset.range 2) (Finset.Ico 2 (m + 3)) := by rw [Finset.disjoint_left]; intro j h1' h2' simp only [Finset.mem_range, Finset.mem_Ico] at h1' h2'; omega rw [h1, Finset.sum_union hdisj] have hhead : ∑ j ∈ Finset.range 2, (((m + 2).choose j : ℕ) : ℝ) * ((q : ℝ) - 1) ^ j = 1 + ((m : ℝ) + 2) * ((q : ℝ) - 1) := by simp [Finset.sum_range_succ] have htail : ∑ j ∈ Finset.Ico 2 (m + 3), (((m + 2).choose j : ℕ) : ℝ) * ((q : ℝ) - 1) ^ j = ∑ k ∈ Finset.range (m + 1), (((m + 2).choose (k + 2) : ℕ) : ℝ) * ((q : ℝ) - 1) ^ (k + 2) := by refine Finset.sum_bij (fun j _ => j - 2) ?_ ?_ ?_ ?_ · intro j hj; simp only [Finset.mem_Ico, Finset.mem_range] at hj ⊢; omega · intro a ha b hb heq; simp only [Finset.mem_Ico] at ha hb; omega · intro k hk; simp only [Finset.mem_range] at hk exact ⟨k + 2, Finset.mem_Ico.mpr ⟨by omega, by omega⟩, by omega⟩ · intro j hj; simp only [Finset.mem_Ico] at hj have : j - 2 + 2 = j := by omega rw [this] rw [hhead, htail] linarith /-- Immediate corollary: Kulkarni–Kiyavash's exact dual sum is strictly below the textbook closed form `2 q^(m + 3) / ((q - 1)^2 (m + 1)(m + 2))`. -/ theorem kk2DualWeight_sum_lt_closed_form {q m : ℕ} (hq : 2 ≤ q) : ∑ y : Word q (m + 1), kk2DualWeight y < 2 * (q : ℝ) ^ (m + 3) / (((q : ℝ) - 1) ^ 2 * ((m : ℝ) + 1) * ((m : ℝ) + 2)) := by rw [kk2DualWeight_sum_eq hq] have hq1 : (0 : ℝ) < (q : ℝ) - 1 := by have : (2 : ℝ) ≤ (q : ℝ) := by exact_mod_cast hq linarith have hcoef : 0 < 2 * (q : ℝ) / (((q : ℝ) - 1) ^ 2 * ((m : ℝ) + 1) * ((m : ℝ) + 2)) := by positivity have hsub : (q : ℝ) ^ (m + 2) - 1 - ((m : ℝ) + 2) * ((q : ℝ) - 1) < (q : ℝ) ^ (m + 2) := by have : 0 < 1 + ((m : ℝ) + 2) * ((q : ℝ) - 1) := by positivity linarith have hmul := mul_lt_mul_of_pos_left hsub hcoef have hrhs : 2 * (q : ℝ) / (((q : ℝ) - 1) ^ 2 * ((m : ℝ) + 1) * ((m : ℝ) + 2)) * (q : ℝ) ^ (m + 2) = 2 * (q : ℝ) ^ (m + 3) / (((q : ℝ) - 1) ^ 2 * ((m : ℝ) + 1) * ((m : ℝ) + 2)) := by rw [pow_succ]; ring linarith /-- Truncated low-order binomial tail `S_{10}(N, q) = ∑_{j=0}^{10} C(N, j) (q - 1)^j`. -/ noncomputable def binomTail10 (N q : ℕ) : ℝ := ∑ j ∈ Finset.range 11, ((N.choose j : ℕ) : ℝ) * ((q : ℝ) - 1) ^ j /-- Exact elementary lower bound on the per-run-count saving factor (`k ≥ 9`). -/ private lemma per_k_saving_decomp (q m k : ℕ) (hq : 2 ≤ q) (hm : 1 ≤ m) : let qr : ℝ := (q : ℝ) let mr : ℝ := (m : ℝ) let kr : ℝ := (k : ℝ) 4 / ((kr + 2) * (kr + 3) * (kr + 4) * (kr + 5)) * (kr * (kr + 1) / mr + kr * (kr - 1) / (mr * (qr - 1)) - 6) ≥ 4 * qr / ((qr - 1) * mr) * (1 / ((kr + 1) * (kr + 2))) - 4 * (10 * qr + 2) / ((qr - 1) * mr) * (1 / ((kr + 1) * (kr + 2) * (kr + 3))) - 24 / ((kr + 1) * (kr + 2) * (kr + 3) * (kr + 4)) := by intro qr mr kr have hq1 : 0 < qr - 1 := by have : (2 : ℝ) ≤ qr := by dsimp only [qr]; exact_mod_cast hq linarith have hm0 : 0 < mr := by dsimp only [mr]; exact_mod_cast hm have hkr0 : 0 ≤ kr := Nat.cast_nonneg k have hd1 : kr + 1 ≠ 0 := by linarith have hd2 : kr + 2 ≠ 0 := by linarith have hd3 : kr + 3 ≠ 0 := by linarith have hd4 : kr + 4 ≠ 0 := by linarith have hd5 : kr + 5 ≠ 0 := by linarith have h1 : kr * (kr + 1) / ((kr + 2) * (kr + 3) * (kr + 4) * (kr + 5)) ≥ 1 / ((kr + 1) * (kr + 2)) - 10 / ((kr + 1) * (kr + 2) * (kr + 3)) := by have hdiff : kr * (kr + 1) / ((kr + 2) * (kr + 3) * (kr + 4) * (kr + 5)) - (1 / ((kr + 1) * (kr + 2)) - 10 / ((kr + 1) * (kr + 2) * (kr + 3))) = (44 * kr + 140) / ((kr + 1) * (kr + 2) * (kr + 3) * (kr + 4) * (kr + 5)) := by field_simp [hd1, hd2, hd3, hd4, hd5]; ring have hnn : 0 ≤ (44 * kr + 140) / ((kr + 1) * (kr + 2) * (kr + 3) * (kr + 4) * (kr + 5)) := by positivity linarith have h2 : kr * (kr - 1) / ((kr + 2) * (kr + 3) * (kr + 4) * (kr + 5)) ≥ 1 / ((kr + 1) * (kr + 2)) - 12 / ((kr + 1) * (kr + 2) * (kr + 3)) := by have hdiff : kr * (kr - 1) / ((kr + 2) * (kr + 3) * (kr + 4) * (kr + 5)) - (1 / ((kr + 1) * (kr + 2)) - 12 / ((kr + 1) * (kr + 2) * (kr + 3))) = (60 * kr + 180) / ((kr + 1) * (kr + 2) * (kr + 3) * (kr + 4) * (kr + 5)) := by field_simp [hd1, hd2, hd3, hd4, hd5]; ring have hnn : 0 ≤ (60 * kr + 180) / ((kr + 1) * (kr + 2) * (kr + 3) * (kr + 4) * (kr + 5)) := by positivity linarith have h3 : 6 / ((kr + 2) * (kr + 3) * (kr + 4) * (kr + 5)) ≤ 6 / ((kr + 1) * (kr + 2) * (kr + 3) * (kr + 4)) := by rw [div_le_div_iff₀ (by positivity) (by positivity)] have hfac : 0 ≤ 6 * ((kr + 2) * (kr + 3) * (kr + 4)) := by positivity have hstep := mul_le_mul_of_nonneg_left (show kr + 1 ≤ kr + 5 by linarith) hfac linarith have hcomb1 : 4 / mr * (kr * (kr + 1) / ((kr + 2) * (kr + 3) * (kr + 4) * (kr + 5))) ≥ 4 / mr * (1 / ((kr + 1) * (kr + 2)) - 10 / ((kr + 1) * (kr + 2) * (kr + 3))) := mul_le_mul_of_nonneg_left h1 (by positivity) have hcomb2 : 4 / (mr * (qr - 1)) * (kr * (kr - 1) / ((kr + 2) * (kr + 3) * (kr + 4) * (kr + 5))) ≥ 4 / (mr * (qr - 1)) * (1 / ((kr + 1) * (kr + 2)) - 12 / ((kr + 1) * (kr + 2) * (kr + 3))) := mul_le_mul_of_nonneg_left h2 (by positivity) have hLHS : 4 / ((kr + 2) * (kr + 3) * (kr + 4) * (kr + 5)) * (kr * (kr + 1) / mr + kr * (kr - 1) / (mr * (qr - 1)) - 6) = 4 / mr * (kr * (kr + 1) / ((kr + 2) * (kr + 3) * (kr + 4) * (kr + 5))) + 4 / (mr * (qr - 1)) * (kr * (kr - 1) / ((kr + 2) * (kr + 3) * (kr + 4) * (kr + 5))) - 4 * (6 / ((kr + 2) * (kr + 3) * (kr + 4) * (kr + 5))) := by ring have hRHS : 4 / mr * (1 / ((kr + 1) * (kr + 2)) - 10 / ((kr + 1) * (kr + 2) * (kr + 3))) + 4 / (mr * (qr - 1)) * (1 / ((kr + 1) * (kr + 2)) - 12 / ((kr + 1) * (kr + 2) * (kr + 3))) - 4 * (6 / ((kr + 1) * (kr + 2) * (kr + 3) * (kr + 4))) = 4 * qr / ((qr - 1) * mr) * (1 / ((kr + 1) * (kr + 2))) - 4 * (10 * qr + 2) / ((qr - 1) * mr) * (1 / ((kr + 1) * (kr + 2) * (kr + 3))) - 24 / ((kr + 1) * (kr + 2) * (kr + 3) * (kr + 4)) := by field_simp [hq1.ne', hm0.ne']; ring linarith /-- **Explicit Non-Asymptotic Second-Order Saving Lower Bound**: For EVERY `q ≥ 2` and EVERY `m ≥ 9` (`n = m + 3 ≥ 12`), the exact dual saving over Kulkarni–Kiyavash (2013) obeys the explicit closed-form lower bound: `∑ (kk2DualWeight - twoDelDualWeight) ≥ (4q/(q-1)^3) * q^(m+3)/(m(m+1)(m+2)) - tail - O(q^(m+3)/m^4)`. -/ theorem twoDelDualWeight_saving_ge {q m : ℕ} (hq : 2 ≤ q) (hm : 9 ≤ m) : let qr : ℝ := (q : ℝ) let mr : ℝ := (m : ℝ) 4 * qr / ((qr - 1) ^ 3) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2))) - 4 * qr ^ 2 / ((qr - 1) ^ 3 * mr * (mr + 1) * (mr + 2)) * binomTail10 (m + 2) q - 4 * (10 * qr + 2) * qr / ((qr - 1) ^ 4) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2) * (mr + 3))) - 24 * qr ^ 2 / ((qr - 1) ^ 4) * (qr ^ (m + 3) / ((mr + 1) * (mr + 2) * (mr + 3) * (mr + 4))) ≤ (∑ y : Word q (m + 1), kk2DualWeight y) - (∑ y : Word q (m + 1), twoDelDualWeight y) := by intro qr mr have hq1 : 0 < qr - 1 := by have : (2 : ℝ) ≤ qr := by dsimp only [qr]; exact_mod_cast hq linarith have hm0 : 0 < mr := by dsimp only [mr]; exact_mod_cast (show 0 < m by omega) have hcast_q : ((q - 1 : ℕ) : ℝ) = qr - 1 := by dsimp only [qr]; rw [Nat.cast_sub (by omega)]; simp rw [← Finset.sum_sub_distrib, sum_univ_by_runs] -- Drop `k < 9` (each term is `≥ 0`) have hdrop : ∑ k ∈ Finset.Ico 9 (m + 1), ∑ y ∈ wordsWithRuns q m (k + 1), (kk2DualWeight y - twoDelDualWeight y) ≤ ∑ k ∈ Finset.range (m + 1), ∑ y ∈ wordsWithRuns q m (k + 1), (kk2DualWeight y - twoDelDualWeight y) := by refine Finset.sum_le_sum_of_subset_of_nonneg (fun k hk => by simp only [Finset.mem_Ico, Finset.mem_range] at hk ⊢; omega) (fun k _ _ => ?_) exact Finset.sum_nonneg fun y _ => sub_nonneg.mpr (twoDelDualWeight_le_kk2DualWeight y) -- On each fiber `k ∈ Ico 9 (m + 1)`, evaluate exact fiber saving lower bound! have hfiber : ∀ k ∈ Finset.Ico 9 (m + 1), (m.choose k : ℝ) * (qr * (qr - 1) ^ k) * (4 * qr / ((qr - 1) * mr) * (1 / (((k : ℝ) + 1) * ((k : ℝ) + 2))) - 4 * (10 * qr + 2) / ((qr - 1) * mr) * (1 / (((k : ℝ) + 1) * ((k : ℝ) + 2) * ((k : ℝ) + 3))) - 24 / (((k : ℝ) + 1) * ((k : ℝ) + 2) * ((k : ℝ) + 3) * ((k : ℝ) + 4))) ≤ ∑ y ∈ wordsWithRuns q m (k + 1), (kk2DualWeight y - twoDelDualWeight y) := by intro k hk have hk9 : 9 ≤ k := (Finset.mem_Ico.mp hk).1 have hdiff_y : ∀ y ∈ wordsWithRuns q m (k + 1), 4 / ((((k : ℝ) + 2) * ((k : ℝ) + 3) * ((k : ℝ) + 4) * ((k : ℝ) + 5))) * ((singletonRunCount y : ℝ) + (sandwichCount y : ℝ) - 6) ≤ kk2DualWeight y - twoDelDualWeight y := by intro y hy simp only [wordsWithRuns, Finset.mem_filter, Finset.mem_univ, true_and] at hy unfold twoDelDualWeight kk2DualWeight; dsimp only rw [if_neg (by omega), hy] have hr1_ge : (singletonRunCount y : ℝ) - 2 ≤ ((singletonRunCount y - 2 : ℕ) : ℝ) := by by_cases h : 2 ≤ singletonRunCount y · rw [Nat.cast_sub h]; norm_num · have : singletonRunCount y - 2 = 0 := by omega rw [this]; simp have : (singletonRunCount y : ℝ) ≤ 2 := by exact_mod_cast (show singletonRunCount y ≤ 2 by omega) linarith have hs_ge : (sandwichCount y : ℝ) - 4 ≤ ((sandwichCount y - 4 : ℕ) : ℝ) := by by_cases h : 4 ≤ sandwichCount y · rw [Nat.cast_sub h]; norm_num · have : sandwichCount y - 4 = 0 := by omega rw [this]; simp have : (sandwichCount y : ℝ) ≤ 4 := by exact_mod_cast (show sandwichCount y ≤ 4 by omega) linarith have hden_eq : (((k + 1 : ℕ) : ℝ) + 1) * (((k + 1 : ℕ) : ℝ) + 2) * (((k + 1 : ℕ) : ℝ) + 3) * (((k + 1 : ℕ) : ℝ) + 4) = ((k : ℝ) + 2) * ((k : ℝ) + 3) * ((k : ℝ) + 4) * ((k : ℝ) + 5) := by push_cast; ring rw [sub_sub_cancel, hden_eq] have hfac : 0 ≤ 4 / (((k : ℝ) + 2) * ((k : ℝ) + 3) * ((k : ℝ) + 4) * ((k : ℝ) + 5)) := by positivity have hsum_le : ((singletonRunCount y : ℝ) + (sandwichCount y : ℝ) - 6) ≤ (((singletonRunCount y - 2 : ℕ) : ℝ) + ((sandwichCount y - 4 : ℕ) : ℝ)) := by linarith calc 4 / (((k : ℝ) + 2) * ((k : ℝ) + 3) * ((k : ℝ) + 4) * ((k : ℝ) + 5)) * ((singletonRunCount y : ℝ) + (sandwichCount y : ℝ) - 6) ≤ 4 / (((k : ℝ) + 2) * ((k : ℝ) + 3) * ((k : ℝ) + 4) * ((k : ℝ) + 5)) * (((singletonRunCount y - 2 : ℕ) : ℝ) + ((sandwichCount y - 4 : ℕ) : ℝ)) := mul_le_mul_of_nonneg_left hsum_le hfac _ = 4 * (((singletonRunCount y - 2 : ℕ) : ℝ) + ((sandwichCount y - 4 : ℕ) : ℝ)) / (((k : ℝ) + 2) * ((k : ℝ) + 3) * ((k : ℝ) + 4) * ((k : ℝ) + 5)) := by ring have hsum1 := Finset.sum_le_sum hdiff_y rw [← Finset.mul_sum] at hsum1 have hinner : ∑ y ∈ wordsWithRuns q m (k + 1), ((singletonRunCount y : ℝ) + (sandwichCount y : ℝ) - 6) = (m.choose k : ℝ) * (qr * (qr - 1) ^ k) * ((k : ℝ) * ((k : ℝ) + 1) / mr + (k : ℝ) * ((k : ℝ) - 1) / (mr * (qr - 1)) - 6) := by have hsing_nat := sum_singletonRunCount_over_runs hq (show 2 ≤ m by omega) (show 2 ≤ k by omega) have hsand_nat := sum_sandwichCount_over_runs hq (show 2 ≤ m by omega) (show 2 ≤ k by omega) have hsing_r : mr * (∑ y ∈ wordsWithRuns q m (k + 1), (singletonRunCount y : ℝ)) = (k : ℝ) * ((k : ℝ) + 1) * (m.choose k : ℝ) * (qr * (qr - 1) ^ k) := by have h : (m : ℝ) * ((∑ y ∈ wordsWithRuns q m (k + 1), singletonRunCount y : ℕ) : ℝ) = ((k * (k + 1) * m.choose k * (q * (q - 1) ^ k) : ℕ) : ℝ) := by exact_mod_cast hsing_nat push_cast at h; rw [hcast_q] at h; exact h have hsand_r : mr * (∑ y ∈ wordsWithRuns q m (k + 1), (sandwichCount y : ℝ)) = (k : ℝ) * ((k : ℝ) - 1) * (m.choose k : ℝ) * (qr * (qr - 1) ^ (k - 1)) := by have h : (m : ℝ) * ((∑ y ∈ wordsWithRuns q m (k + 1), sandwichCount y : ℕ) : ℝ) = ((k * (k - 1) * m.choose k * (q * (q - 1) ^ (k - 1)) : ℕ) : ℝ) := by exact_mod_cast hsand_nat have hksub : ((k - 1 : ℕ) : ℝ) = (k : ℝ) - 1 := by rw [Nat.cast_sub (by omega)]; simp push_cast at h; rw [hcast_q, hksub] at h; exact h have hcard_r : ((wordsWithRuns q m (k + 1)).card : ℝ) = (m.choose k : ℝ) * (qr * (qr - 1) ^ k) := by have h := card_wordsWithRuns hq m k have hr : ((wordsWithRuns q m (k + 1)).card : ℝ) = ((m.choose k * (q * (q - 1) ^ k) : ℕ) : ℝ) := by exact_mod_cast h push_cast at hr; rw [hcast_q] at hr; exact hr have hpow_q : (qr - 1) ^ k = (qr - 1) ^ (k - 1) * (qr - 1) := by conv_lhs => rw [show k = (k - 1) + 1 by omega, pow_succ] rw [Finset.sum_sub_distrib, Finset.sum_add_distrib, Finset.sum_const, nsmul_eq_mul, hcard_r] have hs1 : ∑ y ∈ wordsWithRuns q m (k + 1), (singletonRunCount y : ℝ) = (m.choose k : ℝ) * (qr * (qr - 1) ^ k) * ((k : ℝ) * ((k : ℝ) + 1) / mr) := by have : mr * (∑ y ∈ wordsWithRuns q m (k + 1), (singletonRunCount y : ℝ)) = mr * ((m.choose k : ℝ) * (qr * (qr - 1) ^ k) * ((k : ℝ) * ((k : ℝ) + 1) / mr)) := by rw [hsing_r]; field_simp [hm0.ne'] exact mul_left_cancel₀ hm0.ne' this have hs2 : ∑ y ∈ wordsWithRuns q m (k + 1), (sandwichCount y : ℝ) = (m.choose k : ℝ) * (qr * (qr - 1) ^ k) * ((k : ℝ) * ((k : ℝ) - 1) / (mr * (qr - 1))) := by have : mr * (∑ y ∈ wordsWithRuns q m (k + 1), (sandwichCount y : ℝ)) = mr * ((m.choose k : ℝ) * (qr * (qr - 1) ^ k) * ((k : ℝ) * ((k : ℝ) - 1) / (mr * (qr - 1)))) := by rw [hsand_r, hpow_q]; field_simp [hm0.ne', hq1.ne'] exact mul_left_cancel₀ hm0.ne' this rw [hs1, hs2]; ring rw [hinner] at hsum1 have hdec := per_k_saving_decomp q m k hq (by omega) have hnn : 0 ≤ (m.choose k : ℝ) * (qr * (qr - 1) ^ k) := by positivity have hmul := mul_le_mul_of_nonneg_left hdec hnn calc _ ≤ (m.choose k : ℝ) * (qr * (qr - 1) ^ k) * (4 / (((k : ℝ) + 2) * ((k : ℝ) + 3) * ((k : ℝ) + 4) * ((k : ℝ) + 5)) * ((k : ℝ) * ((k : ℝ) + 1) / mr + (k : ℝ) * ((k : ℝ) - 1) / (mr * (qr - 1)) - 6)) := hmul _ = 4 / (((k : ℝ) + 2) * ((k : ℝ) + 3) * ((k : ℝ) + 4) * ((k : ℝ) + 5)) * ((m.choose k : ℝ) * (qr * (qr - 1) ^ k) * ((k : ℝ) * ((k : ℝ) + 1) / mr + (k : ℝ) * ((k : ℝ) - 1) / (mr * (qr - 1)) - 6)) := by ring _ ≤ _ := hsum1 have hsum_ico := (Finset.sum_le_sum hfiber).trans hdrop -- Now evaluate the three binomial-shifted sums over `k ∈ Ico 9 (m + 1)`! -- Sum 1 (exact): `∑_{k=9}^m C(m, k) (q-1)^k / ((k+1)(k+2))` have hS1 : ∑ k ∈ Finset.Ico 9 (m + 1), (m.choose k : ℝ) * (qr * (qr - 1) ^ k) * (4 * qr / ((qr - 1) * mr) * (1 / (((k : ℝ) + 1) * ((k : ℝ) + 2)))) = 4 * qr / ((qr - 1) ^ 3) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2))) - 4 * qr ^ 2 / ((qr - 1) ^ 3 * mr * (mr + 1) * (mr + 2)) * binomTail10 (m + 2) q := by have hre : ∀ k ∈ Finset.Ico 9 (m + 1), (m.choose k : ℝ) * (qr * (qr - 1) ^ k) * (4 * qr / ((qr - 1) * mr) * (1 / (((k : ℝ) + 1) * ((k : ℝ) + 2)))) = 4 * qr ^ 2 / ((qr - 1) ^ 3 * mr * (mr + 1) * (mr + 2)) * ((((m + 2).choose (k + 2) : ℕ) : ℝ) * (qr - 1) ^ (k + 2)) := by intro k _ have hsh := choose_shift2 m k calc _ = 4 * qr ^ 2 * (qr - 1) ^ k / ((qr - 1) * mr) * ((m.choose k : ℝ) / (((k : ℝ) + 1) * ((k : ℝ) + 2))) := by ring _ = 4 * qr ^ 2 * (qr - 1) ^ k / ((qr - 1) * mr) * ((((m + 2).choose (k + 2) : ℕ) : ℝ) / ((mr + 1) * (mr + 2))) := by rw [hsh] _ = _ := by rw [pow_add]; field_simp [hq1.ne', hm0.ne'] rw [Finset.sum_congr rfl hre, ← Finset.mul_sum] have hbinom : ∑ j ∈ Finset.range (m + 3), (((m + 2).choose j : ℕ) : ℝ) * (qr - 1) ^ j = qr ^ (m + 2) := by have h := add_pow (qr - 1) (1 : ℝ) (m + 2) rw [show qr - 1 + 1 = qr by ring] at h rw [h]; refine Finset.sum_congr rfl fun j _ => by ring have hsplit : ∑ j ∈ Finset.range (m + 3), (((m + 2).choose j : ℕ) : ℝ) * (qr - 1) ^ j = binomTail10 (m + 2) q + ∑ k ∈ Finset.Ico 9 (m + 1), (((m + 2).choose (k + 2) : ℕ) : ℝ) * (qr - 1) ^ (k + 2) := by have hu : Finset.range (m + 3) = Finset.range 11 ∪ Finset.Ico 11 (m + 3) := by ext j; simp only [Finset.mem_union, Finset.mem_range, Finset.mem_Ico]; omega have hd : Disjoint (Finset.range 11) (Finset.Ico 11 (m + 3)) := by rw [Finset.disjoint_left]; intro j h1' h2' simp only [Finset.mem_range, Finset.mem_Ico] at h1' h2'; omega rw [hu, Finset.sum_union hd] congr 1 refine Finset.sum_bij (fun j _ => j - 2) ?_ ?_ ?_ ?_ · intro j hj; simp only [Finset.mem_Ico] at hj ⊢; omega · intro a ha b hb heq; simp only [Finset.mem_Ico] at ha hb; omega · intro k hk; simp only [Finset.mem_Ico] at hk exact ⟨k + 2, Finset.mem_Ico.mpr ⟨by omega, by omega⟩, by omega⟩ · intro j hj; simp only [Finset.mem_Ico] at hj rw [show j - 2 + 2 = j by omega] have hsub : ∑ k ∈ Finset.Ico 9 (m + 1), (((m + 2).choose (k + 2) : ℕ) : ℝ) * (qr - 1) ^ (k + 2) = qr ^ (m + 2) - binomTail10 (m + 2) q := by linarith rw [hsub, mul_sub] congr 1 rw [pow_succ]; field_simp [hq1.ne', hm0.ne']; ring -- Sum 2 (upper bound via full binomial sum `qr^(m + 3)`): have hS2 : ∑ k ∈ Finset.Ico 9 (m + 1), (m.choose k : ℝ) * (qr * (qr - 1) ^ k) * (4 * (10 * qr + 2) / ((qr - 1) * mr) * (1 / (((k : ℝ) + 1) * ((k : ℝ) + 2) * ((k : ℝ) + 3)))) ≤ 4 * (10 * qr + 2) * qr / ((qr - 1) ^ 4) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2) * (mr + 3))) := by have hre : ∀ k ∈ Finset.Ico 9 (m + 1), (m.choose k : ℝ) * (qr * (qr - 1) ^ k) * (4 * (10 * qr + 2) / ((qr - 1) * mr) * (1 / (((k : ℝ) + 1) * ((k : ℝ) + 2) * ((k : ℝ) + 3)))) = 4 * (10 * qr + 2) * qr / ((qr - 1) ^ 4 * mr * (mr + 1) * (mr + 2) * (mr + 3)) * ((((m + 3).choose (k + 3) : ℕ) : ℝ) * (qr - 1) ^ (k + 3)) := by intro k _ have hsh := choose_shift3 m k calc _ = 4 * (10 * qr + 2) * qr * (qr - 1) ^ k / ((qr - 1) * mr) * ((m.choose k : ℝ) / (((k : ℝ) + 1) * ((k : ℝ) + 2) * ((k : ℝ) + 3))) := by ring _ = 4 * (10 * qr + 2) * qr * (qr - 1) ^ k / ((qr - 1) * mr) * ((((m + 3).choose (k + 3) : ℕ) : ℝ) / ((mr + 1) * (mr + 2) * (mr + 3))) := by rw [hsh] _ = _ := by rw [pow_add]; field_simp [hq1.ne', hm0.ne'] rw [Finset.sum_congr rfl hre, ← Finset.mul_sum] have hle : ∑ k ∈ Finset.Ico 9 (m + 1), (((m + 3).choose (k + 3) : ℕ) : ℝ) * (qr - 1) ^ (k + 3) ≤ qr ^ (m + 3) := by have hbinom : ∑ j ∈ Finset.range (m + 4), (((m + 3).choose j : ℕ) : ℝ) * (qr - 1) ^ j = qr ^ (m + 3) := by have h := add_pow (qr - 1) (1 : ℝ) (m + 3) rw [show qr - 1 + 1 = qr by ring] at h rw [h]; refine Finset.sum_congr rfl fun j _ => by ring rw [← hbinom] have hshift : ∑ k ∈ Finset.Ico 9 (m + 1), (((m + 3).choose (k + 3) : ℕ) : ℝ) * (qr - 1) ^ (k + 3) = ∑ j ∈ Finset.Ico 12 (m + 4), (((m + 3).choose j : ℕ) : ℝ) * (qr - 1) ^ j := by refine Finset.sum_bij (fun k _ => k + 3) ?_ ?_ ?_ ?_ · intro k hk; simp only [Finset.mem_Ico] at hk ⊢; omega · intro a _ b _ heq; omega · intro j hj; simp only [Finset.mem_Ico] at hj exact ⟨j - 3, Finset.mem_Ico.mpr ⟨by omega, by omega⟩, by omega⟩ · intro _ _; rfl rw [hshift] exact Finset.sum_le_sum_of_subset_of_nonneg (fun j hj => by simp only [Finset.mem_Ico, Finset.mem_range] at hj ⊢; omega) (fun _ _ _ => by positivity) calc _ ≤ 4 * (10 * qr + 2) * qr / ((qr - 1) ^ 4 * mr * (mr + 1) * (mr + 2) * (mr + 3)) * qr ^ (m + 3) := mul_le_mul_of_nonneg_left hle (by positivity) _ = _ := by field_simp [hq1.ne', hm0.ne'] -- Sum 3 (upper bound via full binomial sum `qr^(m + 4)`): have hS3 : ∑ k ∈ Finset.Ico 9 (m + 1), (m.choose k : ℝ) * (qr * (qr - 1) ^ k) * (24 / (((k : ℝ) + 1) * ((k : ℝ) + 2) * ((k : ℝ) + 3) * ((k : ℝ) + 4))) ≤ 24 * qr ^ 2 / ((qr - 1) ^ 4) * (qr ^ (m + 3) / ((mr + 1) * (mr + 2) * (mr + 3) * (mr + 4))) := by have hre : ∀ k ∈ Finset.Ico 9 (m + 1), (m.choose k : ℝ) * (qr * (qr - 1) ^ k) * (24 / (((k : ℝ) + 1) * ((k : ℝ) + 2) * ((k : ℝ) + 3) * ((k : ℝ) + 4))) = 24 * qr / ((qr - 1) ^ 4 * (mr + 1) * (mr + 2) * (mr + 3) * (mr + 4)) * ((((m + 4).choose (k + 4) : ℕ) : ℝ) * (qr - 1) ^ (k + 4)) := by intro k _ have hsh := choose_shift4 m k calc _ = 24 * qr * (qr - 1) ^ k * ((m.choose k : ℝ) / (((k : ℝ) + 1) * ((k : ℝ) + 2) * ((k : ℝ) + 3) * ((k : ℝ) + 4))) := by ring _ = 24 * qr * (qr - 1) ^ k * ((((m + 4).choose (k + 4) : ℕ) : ℝ) / ((mr + 1) * (mr + 2) * (mr + 3) * (mr + 4))) := by rw [hsh] _ = _ := by rw [pow_add]; field_simp [hq1.ne'] rw [Finset.sum_congr rfl hre, ← Finset.mul_sum] have hle : ∑ k ∈ Finset.Ico 9 (m + 1), (((m + 4).choose (k + 4) : ℕ) : ℝ) * (qr - 1) ^ (k + 4) ≤ qr ^ (m + 4) := by have hbinom : ∑ j ∈ Finset.range (m + 5), (((m + 4).choose j : ℕ) : ℝ) * (qr - 1) ^ j = qr ^ (m + 4) := by have h := add_pow (qr - 1) (1 : ℝ) (m + 4) rw [show qr - 1 + 1 = qr by ring] at h rw [h]; refine Finset.sum_congr rfl fun j _ => by ring rw [← hbinom] have hshift : ∑ k ∈ Finset.Ico 9 (m + 1), (((m + 4).choose (k + 4) : ℕ) : ℝ) * (qr - 1) ^ (k + 4) = ∑ j ∈ Finset.Ico 13 (m + 5), (((m + 4).choose j : ℕ) : ℝ) * (qr - 1) ^ j := by refine Finset.sum_bij (fun k _ => k + 4) ?_ ?_ ?_ ?_ · intro k hk; simp only [Finset.mem_Ico] at hk ⊢; omega · intro a _ b _ heq; omega · intro j hj; simp only [Finset.mem_Ico] at hj exact ⟨j - 4, Finset.mem_Ico.mpr ⟨by omega, by omega⟩, by omega⟩ · intro _ _; rfl rw [hshift] exact Finset.sum_le_sum_of_subset_of_nonneg (fun j hj => by simp only [Finset.mem_Ico, Finset.mem_range] at hj ⊢; omega) (fun _ _ _ => by positivity) calc _ ≤ 24 * qr / ((qr - 1) ^ 4 * (mr + 1) * (mr + 2) * (mr + 3) * (mr + 4)) * qr ^ (m + 4) := mul_le_mul_of_nonneg_left hle (by positivity) _ = _ := by rw [pow_succ]; field_simp [hq1.ne']; ring -- Combine `hS1`, `hS2`, `hS3` into `hsum_ico`! have hsplit_sum : ∑ k ∈ Finset.Ico 9 (m + 1), (m.choose k : ℝ) * (qr * (qr - 1) ^ k) * (4 * qr / ((qr - 1) * mr) * (1 / (((k : ℝ) + 1) * ((k : ℝ) + 2))) - 4 * (10 * qr + 2) / ((qr - 1) * mr) * (1 / (((k : ℝ) + 1) * ((k : ℝ) + 2) * ((k : ℝ) + 3))) - 24 / (((k : ℝ) + 1) * ((k : ℝ) + 2) * ((k : ℝ) + 3) * ((k : ℝ) + 4))) = (∑ k ∈ Finset.Ico 9 (m + 1), (m.choose k : ℝ) * (qr * (qr - 1) ^ k) * (4 * qr / ((qr - 1) * mr) * (1 / (((k : ℝ) + 1) * ((k : ℝ) + 2))))) - (∑ k ∈ Finset.Ico 9 (m + 1), (m.choose k : ℝ) * (qr * (qr - 1) ^ k) * (4 * (10 * qr + 2) / ((qr - 1) * mr) * (1 / (((k : ℝ) + 1) * ((k : ℝ) + 2) * ((k : ℝ) + 3))))) - (∑ k ∈ Finset.Ico 9 (m + 1), (m.choose k : ℝ) * (qr * (qr - 1) ^ k) * (24 / (((k : ℝ) + 1) * ((k : ℝ) + 2) * ((k : ℝ) + 3) * ((k : ℝ) + 4)))) := by rw [← Finset.sum_sub_distrib, ← Finset.sum_sub_distrib] refine Finset.sum_congr rfl fun k _ => by ring linarith only [hsum_ico, hsplit_sum, hS1, hS2, hS3] /-- **HEADLINE NON-ASYMPTOTIC WORLD-RECORD BOUND FOR `q`-ARY 2-DELETION CODES**: For EVERY `q ≥ 2`, EVERY `m ≥ 9` (`n = m + 3 ≥ 12`), and EVERY `q`-ary 2-deletion code `C ⊆ Word q (m + 3)`: 1. `|C|` is bounded by our exact second-order dual sum, which is STRICTLY smaller than Kulkarni–Kiyavash's (IEEE TIT 2013) dual sum and textbook bound `2 q^n / ((q - 1)^2 (n - 1)(n - 2))`; 2. Explicitly, `|C|` obeys the closed-form second-order bound with leading saving `- (4 q / (q - 1)^3) * q^n / ((n - 3)(n - 2)(n - 1))`! -/ theorem twoDelCode_explicit_second_order_bound {q m : ℕ} (hq : 2 ≤ q) (hm : 9 ≤ m) (C : Finset (Word q (m + 3))) (hC : IsTwoDelCode C) : let qr : ℝ := (q : ℝ) let mr : ℝ := (m : ℝ) (C.card : ℝ) ≤ 2 * qr ^ (m + 3) / ((qr - 1) ^ 2 * (mr + 1) * (mr + 2)) - 4 * qr / ((qr - 1) ^ 3) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2))) + 4 * qr ^ 2 / ((qr - 1) ^ 3 * mr * (mr + 1) * (mr + 2)) * binomTail10 (m + 2) q + 4 * (10 * qr + 2) * qr / ((qr - 1) ^ 4) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2) * (mr + 3))) + 24 * qr ^ 2 / ((qr - 1) ^ 4) * (qr ^ (m + 3) / ((mr + 1) * (mr + 2) * (mr + 3) * (mr + 4))) := by intro qr mr have h1 := twoDelCode_card_le_twoDelDualWeight_sum C hC have h2 := twoDelDualWeight_saving_ge hq hm have h3 := kk2DualWeight_sum_lt_closed_form (m := m) hq linarith /-- Polynomial upper bound `binomTail10 N q ≤ 11 * N^10 * (q - 1)^10` for `N ≥ 1, q ≥ 2`. -/ lemma binomTail10_le_poly {N q : ℕ} (hN : 1 ≤ N) (hq : 2 ≤ q) : binomTail10 N q ≤ 11 * (N : ℝ) ^ 10 * ((q : ℝ) - 1) ^ 10 := by unfold binomTail10 have hN1 : (1 : ℝ) ≤ (N : ℝ) := by exact_mod_cast hN have hq1 : (1 : ℝ) ≤ (q : ℝ) - 1 := by have : (2 : ℝ) ≤ (q : ℝ) := by exact_mod_cast hq linarith have hterm : ∀ j ∈ Finset.range 11, ((N.choose j : ℕ) : ℝ) * ((q : ℝ) - 1) ^ j ≤ (N : ℝ) ^ 10 * ((q : ℝ) - 1) ^ 10 := by intro j hj have hj10 : j ≤ 10 := by simp only [Finset.mem_range] at hj; omega have hch : (N.choose j : ℝ) ≤ (N : ℝ) ^ j := by have hfac1 : (1 : ℝ) ≤ (j.factorial : ℝ) := by exact_mod_cast Nat.factorial_pos j exact (Nat.choose_le_pow_div j N).trans (div_le_self (by positivity) hfac1) have hNj : (N : ℝ) ^ j ≤ (N : ℝ) ^ 10 := pow_le_pow_right₀ hN1 hj10 have hqj : ((q : ℝ) - 1) ^ j ≤ ((q : ℝ) - 1) ^ 10 := pow_le_pow_right₀ hq1 hj10 exact mul_le_mul (hch.trans hNj) hqj (by positivity) (by positivity) calc ∑ j ∈ Finset.range 11, ((N.choose j : ℕ) : ℝ) * ((q : ℝ) - 1) ^ j ≤ ∑ _j ∈ Finset.range 11, (N : ℝ) ^ 10 * ((q : ℝ) - 1) ^ 10 := Finset.sum_le_sum hterm _ = 11 * (N : ℝ) ^ 10 * ((q : ℝ) - 1) ^ 10 := by rw [Finset.sum_const, Finset.card_range, nsmul_eq_mul]; push_cast; ring /-- **HEADLINE ASYMPTOTIC SECOND-ORDER THEOREM (WORLD RECORD CONSTANT `4q / (q - 1)^3`)**: For EVERY alphabet size `q ≥ 2` and EVERY `ε > 0`, there exists `N₀` such that for ALL lengths `n = m + 3 ≥ N₀ + 3` and EVERY `q`-ary 2-deletion code `C ⊆ Word q (m + 3)`: `|C| ≤ 2 q^n / ((q - 1)^2 (n - 1)(n - 2)) - (4 q / (q - 1)^3 - ε) * q^n / n^3`. -/ theorem twoDelCode_asymptotic_second_order {q : ℕ} (hq : 2 ≤ q) {ε : ℝ} (hε : 0 < ε) : ∃ M₀ : ℕ, ∀ m ≥ M₀, ∀ (C : Finset (Word q (m + 3))), IsTwoDelCode C → (C.card : ℝ) ≤ 2 * (q : ℝ) ^ (m + 3) / (((q : ℝ) - 1) ^ 2 * ((m : ℝ) + 1) * ((m : ℝ) + 2)) - (4 * (q : ℝ) / ((q : ℝ) - 1) ^ 3 - ε) * ((q : ℝ) ^ (m + 3) / ((m : ℝ) + 3) ^ 3) := by set qr : ℝ := (q : ℝ) have hq2 : (2 : ℝ) ≤ qr := by dsimp only [qr]; exact_mod_cast hq have hq1 : 0 < qr - 1 := by linarith have hq_gt1 : (1 : ℝ) < qr := by linarith -- Choose `M₁` so that the polynomial-over-exponential tail is `< ε / 2`! have htend_tail : Filter.Tendsto (fun m : ℕ => 4 * qr / (qr - 1) ^ 3 * (11 * ((m : ℝ) + 2) ^ 10 * (qr - 1) ^ 10 / qr ^ (m + 2))) Filter.atTop (nhds 0) := by have hpow : Filter.Tendsto (fun N : ℕ => ((N : ℝ) ^ 10) / qr ^ N) Filter.atTop (nhds 0) := tendsto_pow_const_div_const_pow_of_one_lt 10 hq_gt1 have hshift : Filter.Tendsto (fun m : ℕ => (((m + 2 : ℕ) : ℝ) ^ 10) / qr ^ (m + 2)) Filter.atTop (nhds 0) := hpow.comp (Filter.tendsto_add_atTop_nat 2) have hmul := hshift.const_mul (4 * qr / (qr - 1) ^ 3 * (11 * (qr - 1) ^ 10)) rw [mul_zero] at hmul refine hmul.congr fun m => ?_ push_cast; ring have hev1 : ∀ᶠ m : ℕ in Filter.atTop, 4 * qr / (qr - 1) ^ 3 * (11 * ((m : ℝ) + 2) ^ 10 * (qr - 1) ^ 10 / qr ^ (m + 2)) < ε / 2 := htend_tail.eventually (gt_mem_nhds (by linarith)) obtain ⟨M₁, hM₁⟩ := Filter.eventually_atTop.mp hev1 -- Choose `M₂` so that the `O(1/m)` relative error `(4(10q+2)q + 24q^2) / ((q-1)^4 * m) < ε / 2`! set C4 : ℝ := (4 * (10 * qr + 2) * qr + 24 * qr ^ 2) / (qr - 1) ^ 4 have hC4_pos : 0 < C4 := by positivity obtain ⟨M₂, hM₂⟩ := exists_nat_gt (2 * C4 / ε) refine ⟨max 9 (max M₁ M₂), fun m hm C hC => ?_⟩ have hm9 : 9 ≤ m := le_trans (le_max_left _ _) hm have hm1 : M₁ ≤ m := le_trans (le_trans (le_max_left _ _) (le_max_right _ _)) hm have hm2 : M₂ ≤ m := le_trans (le_trans (le_max_right _ _) (le_max_right _ _)) hm set mr : ℝ := (m : ℝ) have hm_pos : 0 < mr := by dsimp only [mr]; exact_mod_cast (show 0 < m by omega) have hbase := twoDelCode_explicit_second_order_bound hq hm9 C hC -- Compare `1 / (m (m+1)(m+2))` against `1 / (m + 3)^3` (strictly LARGER!) have hden_cmp : mr * (mr + 1) * (mr + 2) ≤ (mr + 3) ^ 3 := by have : (mr + 3) ^ 3 - mr * (mr + 1) * (mr + 2) = 6 * mr ^ 2 + 25 * mr + 27 := by ring nlinarith have hmain_ge : 4 * qr / (qr - 1) ^ 3 * (qr ^ (m + 3) / (mr + 3) ^ 3) ≤ 4 * qr / (qr - 1) ^ 3 * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2))) := by refine mul_le_mul_of_nonneg_left ?_ (by positivity) exact div_le_div_of_nonneg_left (by positivity) (by positivity) hden_cmp -- Bound the binomial tail term by `(ε / 2) * (qr^(m + 3) / (mr * (mr + 1) * (mr + 2)))`? -- Wait: we want it `≤ (ε / 2) * (qr^(m + 3) / (mr + 3)^3)`! -- Actually `((mr + 3)^3 / (mr * (mr + 1) * (mr + 2))) ≤ 2` for `m ≥ 9`! -- Even simpler: bound `tail + err4` directly in terms of `qr^(m + 3) / (mr * (mr + 1) * (mr + 2))`! -- Wait: `(4q/(q-1)^3 - tail_rel - err_rel) * (qr^(m+3)/(mr(mr+1)(mr+2))) ≥ (4q/(q-1)^3 - ε) * (qr^(m+3)/(mr+3)^3)` -- provided `tail_rel + err_rel ≤ ε` AND `4q/(q-1)^3 - ε` is handled! -- Wait: if `ε ≥ 4q/(q-1)^3`, then `(4q/(q-1)^3 - ε) ≤ 0`, so the bound is ALREADY weaker than KK13! by_cases hε_big : 4 * qr / (qr - 1) ^ 3 ≤ ε · have hkk := (twoDelCode_card_le_twoDelDualWeight_sum C hC).trans ((twoDelDualWeight_sum_lt_kk2DualWeight_sum hq hm9).le.trans (kk2DualWeight_sum_lt_closed_form (m := m) hq).le) have hneg : (4 * qr / (qr - 1) ^ 3 - ε) * (qr ^ (m + 3) / (mr + 3) ^ 3) ≤ 0 := mul_nonpos_of_nonpos_of_nonneg (by linarith) (by positivity) linarith · have hpos_coeff : 0 < 4 * qr / (qr - 1) ^ 3 - ε := by linarith have htail_bd : 4 * qr ^ 2 / ((qr - 1) ^ 3 * mr * (mr + 1) * (mr + 2)) * binomTail10 (m + 2) q ≤ (ε / 2) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2))) := by have hp := binomTail10_le_poly (N := m + 2) (q := q) (by omega) hq have hlt := hM₁ m hm1 have hcast_m2 : ((m + 2 : ℕ) : ℝ) = mr + 2 := by push_cast; rfl rw [hcast_m2] at hp have hfac_eq : 4 * qr ^ 2 / ((qr - 1) ^ 3 * mr * (mr + 1) * (mr + 2)) * (11 * (mr + 2) ^ 10 * (qr - 1) ^ 10) = (4 * qr / (qr - 1) ^ 3 * (11 * (mr + 2) ^ 10 * (qr - 1) ^ 10 / qr ^ (m + 2))) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2))) := by rw [pow_succ]; field_simp [hq1.ne', hm_pos.ne']; ring calc 4 * qr ^ 2 / ((qr - 1) ^ 3 * mr * (mr + 1) * (mr + 2)) * binomTail10 (m + 2) q ≤ 4 * qr ^ 2 / ((qr - 1) ^ 3 * mr * (mr + 1) * (mr + 2)) * (11 * (mr + 2) ^ 10 * (qr - 1) ^ 10) := mul_le_mul_of_nonneg_left hp (by positivity) _ = (4 * qr / (qr - 1) ^ 3 * (11 * (mr + 2) ^ 10 * (qr - 1) ^ 10 / qr ^ (m + 2))) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2))) := hfac_eq _ ≤ (ε / 2) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2))) := mul_le_mul_of_nonneg_right hlt.le (by positivity) have herr_bd : 4 * (10 * qr + 2) * qr / (qr - 1) ^ 4 * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2) * (mr + 3))) + 24 * qr ^ 2 / (qr - 1) ^ 4 * (qr ^ (m + 3) / ((mr + 1) * (mr + 2) * (mr + 3) * (mr + 4))) ≤ (ε / 2) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2))) := by have hm_gt : 2 * C4 / ε < mr := by have hM2_r : (M₂ : ℝ) ≤ mr := by dsimp only [mr]; exact_mod_cast hm2 linarith have hC4_lt : C4 / mr < ε / 2 := by rw [div_lt_iff₀ hm_pos] rw [div_lt_iff₀ hε] at hm_gt linarith have h1 : qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2) * (mr + 3)) ≤ (1 / mr) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2))) := by have h3 : mr * (mr * (mr + 1) * (mr + 2)) ≤ mr * (mr + 1) * (mr + 2) * (mr + 3) := by have := mul_le_mul_of_nonneg_left (show mr ≤ mr + 3 by linarith) (show 0 ≤ mr * (mr + 1) * (mr + 2) by positivity) linarith calc qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2) * (mr + 3)) ≤ qr ^ (m + 3) / (mr * (mr * (mr + 1) * (mr + 2))) := div_le_div_of_nonneg_left (by positivity) (by positivity) h3 _ = (1 / mr) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2))) := by field_simp [hm_pos.ne'] have h2 : qr ^ (m + 3) / ((mr + 1) * (mr + 2) * (mr + 3) * (mr + 4)) ≤ (1 / mr) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2))) := by have h3 : mr * (mr * (mr + 1) * (mr + 2)) ≤ (mr + 1) * (mr + 2) * (mr + 3) * (mr + 4) := by have hA : mr * mr ≤ (mr + 3) * (mr + 4) := by nlinarith have hB : 0 ≤ (mr + 1) * (mr + 2) := by positivity have := mul_le_mul_of_nonneg_left hA hB linarith calc qr ^ (m + 3) / ((mr + 1) * (mr + 2) * (mr + 3) * (mr + 4)) ≤ qr ^ (m + 3) / (mr * (mr * (mr + 1) * (mr + 2))) := div_le_div_of_nonneg_left (by positivity) (by positivity) h3 _ = (1 / mr) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2))) := by field_simp [hm_pos.ne'] have hcomb : 4 * (10 * qr + 2) * qr / (qr - 1) ^ 4 * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2) * (mr + 3))) + 24 * qr ^ 2 / (qr - 1) ^ 4 * (qr ^ (m + 3) / ((mr + 1) * (mr + 2) * (mr + 3) * (mr + 4))) ≤ (C4 / mr) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2))) := by have t1 := mul_le_mul_of_nonneg_left h1 (show 0 ≤ 4 * (10 * qr + 2) * qr / (qr - 1) ^ 4 by positivity) have t2 := mul_le_mul_of_nonneg_left h2 (show 0 ≤ 24 * qr ^ 2 / (qr - 1) ^ 4 by positivity) have heq : 4 * (10 * qr + 2) * qr / (qr - 1) ^ 4 * ((1 / mr) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2)))) + 24 * qr ^ 2 / (qr - 1) ^ 4 * ((1 / mr) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2)))) = (C4 / mr) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2))) := by dsimp only [C4]; field_simp [hq1.ne', hm_pos.ne'] linarith exact hcomb.trans (mul_le_mul_of_nonneg_right hC4_lt.le (by positivity)) have hstep_den : (4 * qr / (qr - 1) ^ 3 - ε) * (qr ^ (m + 3) / (mr + 3) ^ 3) ≤ (4 * qr / (qr - 1) ^ 3 - ε) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2))) := mul_le_mul_of_nonneg_left (div_le_div_of_nonneg_left (by positivity) (by positivity) hden_cmp) hpos_coeff.le have hfactor : 4 * qr / (qr - 1) ^ 3 * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2))) - (ε / 2) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2))) - (ε / 2) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2))) = (4 * qr / (qr - 1) ^ 3 - ε) * (qr ^ (m + 3) / (mr * (mr + 1) * (mr + 2))) := by ring linarith only [hbase, htail_bd, herr_bd, hstep_den, hfactor] /-- **Binary (`q = 2`) Specialization**: Second-order improvement constant is EXACTLY `8`! -/ theorem binary_twoDelCode_second_order {ε : ℝ} (hε : 0 < ε) : ∃ M₀ : ℕ, ∀ m ≥ M₀, ∀ (C : Finset (Word 2 (m + 3))), IsTwoDelCode C → (C.card : ℝ) ≤ 2 * (2 : ℝ) ^ (m + 3) / (((m : ℝ) + 1) * ((m : ℝ) + 2)) - (8 - ε) * ((2 : ℝ) ^ (m + 3) / ((m : ℝ) + 3) ^ 3) := by obtain ⟨M₀, hM₀⟩ := twoDelCode_asymptotic_second_order (q := 2) (by norm_num) hε refine ⟨M₀, fun m hm C hC => ?_⟩ have h := hM₀ m hm C hC norm_num at h ⊢ exact h /-- **DNA (`q = 4`) Specialization**: Second-order improvement constant is EXACTLY `16/27`! -/ theorem dna_twoDelCode_second_order {ε : ℝ} (hε : 0 < ε) : ∃ M₀ : ℕ, ∀ m ≥ M₀, ∀ (C : Finset (Word 4 (m + 3))), IsTwoDelCode C → (C.card : ℝ) ≤ 2 * (4 : ℝ) ^ (m + 3) / (9 * ((m : ℝ) + 1) * ((m : ℝ) + 2)) - (16 / 27 - ε) * ((4 : ℝ) ^ (m + 3) / ((m : ℝ) + 3) ^ 3) := by obtain ⟨M₀, hM₀⟩ := twoDelCode_asymptotic_second_order (q := 4) (by norm_num) hε refine ⟨M₀, fun m hm C hC => ?_⟩ have h := hM₀ m hm C hC norm_num at h ⊢ exact h end ClosedFormBounds /-! ## Section 6: Matching 2-Deletion Ball Bound & Gilbert–Varshamov Lower Bound -/ section GilbertVarshamov private lemma exists_del2_of_mem_del2Ball {q m : ℕ} (x : Word q (m + 2)) {y : Word q m} (hy : y ∈ del2Ball x) : ∃ p r : Fin (m + 2), p.val < r.val ∧ del2 x p r = y := by unfold del2Ball del1Ball at hy rcases Finset.mem_biUnion.mp hy with ⟨z, hz, hyz⟩ rcases Finset.mem_image.mp hz with ⟨i, _, rfl⟩ rcases Finset.mem_image.mp hyz with ⟨j, _, rfl⟩ by_cases hji : j.val < i.val · refine ⟨⟨j.val, by omega⟩, i, hji, ?_⟩ unfold del2 rw [dif_pos hji] · have hlt : i.val < j.val + 1 := by omega refine ⟨i, ⟨j.val + 1, by omega⟩, hlt, ?_⟩ rw [del2_eq_delPos_first x i ⟨j.val + 1, by omega⟩ hlt] have hj_eq : (⟨j.val + 1 - 1, by omega⟩ : Fin (m + 1)) = j := Fin.ext (by simp) rw [hj_eq] private lemma card_orderedPairs_fin (n : ℕ) : ((Finset.univ : Finset (Fin n × Fin n)).filter (fun pq => pq.1.val < pq.2.val)).card = n.choose 2 := by have h := card_ordered_pairs_same (Finset.univ : Finset (Fin n)) simp only [Finset.mem_univ, and_true, Finset.card_univ, Fintype.card_fin] at h have hch : 2 * n.choose 2 = n * (n - 1) := by rw [Nat.choose_two_right] have heven : 2 ∣ n * (n - 1) := by rcases Nat.even_or_odd n with ⟨k, rfl⟩ | ⟨k, rfl⟩ · exact ⟨k * (k + k - 1), by ring⟩ · cases k with | zero => exact ⟨0, by simp⟩ | succ k => have hsub : 2 * (k + 1) + 1 - 1 = 2 * (k + 1) := by omega rw [hsub] exact ⟨(2 * (k + 1) + 1) * (k + 1), by ring⟩ exact Nat.mul_div_cancel' heven omega /-- Every 2-deletion ball `del2Ball x` has cardinality at most `C(m + 3, 2)`. -/ theorem card_del2Ball_le {q m : ℕ} (x : Word q (m + 3)) : (del2Ball x).card ≤ (m + 3).choose 2 := by let Pairs := (Finset.univ : Finset (Fin (m + 3) × Fin (m + 3))).filter (fun pq => pq.1.val < pq.2.val) have hsub : del2Ball x ⊆ Pairs.image (fun pq => del2 x pq.1 pq.2) := by intro y hy rcases exists_del2_of_mem_del2Ball x hy with ⟨p, r, hpr, rfl⟩ exact Finset.mem_image.mpr ⟨(p, r), Finset.mem_filter.mpr ⟨Finset.mem_univ _, hpr⟩, rfl⟩ calc (del2Ball x).card ≤ (Pairs.image (fun pq => del2 x pq.1 pq.2)).card := Finset.card_le_card hsub _ ≤ Pairs.card := Finset.card_image_le _ = (m + 3).choose 2 := card_orderedPairs_fin (m + 3) /-- Number of `x' : Word q (m + 3)` containing a fixed `y : Word q (m + 1)` as a 2-deletion subsequence is at most `C(m + 3, 2) * q^2`. -/ theorem card_twoInsBall_le {q m : ℕ} (y : Word q (m + 1)) : ((Finset.univ : Finset (Word q (m + 3))).filter (fun x => y ∈ del2Ball x)).card ≤ (m + 3).choose 2 * q ^ 2 := by let Pairs := (Finset.univ : Finset (Fin (m + 3) × Fin (m + 3))).filter (fun pq => pq.1.val < pq.2.val) let Dom := (Pairs ×ˢ (Finset.univ : Finset (Fin q × Fin q))) -- Map `x` (with `y = del2 x p r`) injectively into `Dom` via `((p, r), (x p, x r))`! have hchoose : ∀ x ∈ (Finset.univ : Finset (Word q (m + 3))).filter (fun x => y ∈ del2Ball x), ∃ p r : Fin (m + 3), p.val < r.val ∧ del2 x p r = y := by intro x hx exact exists_del2_of_mem_del2Ball x (Finset.mem_filter.mp hx).2 classical let f : Word q (m + 3) → (Fin (m + 3) × Fin (m + 3)) × (Fin q × Fin q) := fun x => if h : ∃ p r : Fin (m + 3), p.val < r.val ∧ del2 x p r = y then let p := h.choose let r := h.choose_spec.choose ((p, r), (x p, x r)) else ((0, 0), (y 0, y 0)) have hle : ((Finset.univ : Finset (Word q (m + 3))).filter (fun x => y ∈ del2Ball x)).card ≤ Dom.card := by refine Finset.card_le_card_of_injOn f ?_ ?_ · intro x hx have hex := hchoose x (Finset.mem_coe.mp hx) simp only [Finset.mem_coe, f, dif_pos hex, Dom, Finset.mem_product, Pairs, Finset.mem_filter, Finset.mem_univ, true_and, and_true] exact hex.choose_spec.choose_spec.1 · intro x₁ hx₁ x₂ hx₂ hfeq have hex₁ := hchoose x₁ (Finset.mem_coe.mp hx₁) have hex₂ := hchoose x₂ (Finset.mem_coe.mp hx₂) simp only [f, dif_pos hex₁, dif_pos hex₂] at hfeq set p₁ := hex₁.choose set r₁ := hex₁.choose_spec.choose set p₂ := hex₂.choose set r₂ := hex₂.choose_spec.choose have hpr₁ : p₁.val < r₁.val := hex₁.choose_spec.choose_spec.1 have hdel₁ : del2 x₁ p₁ r₁ = y := hex₁.choose_spec.choose_spec.2 have hdel₂ : del2 x₂ p₂ r₂ = y := hex₂.choose_spec.choose_spec.2 have hp_eq : p₁ = p₂ := congrArg (fun t => t.1.1) hfeq have hr_eq : r₁ = r₂ := congrArg (fun t => t.1.2) hfeq have hxp : x₁ p₁ = x₂ p₂ := congrArg (fun t => t.2.1) hfeq have hxr : x₁ r₁ = x₂ r₂ := congrArg (fun t => t.2.2) hfeq rw [← hp_eq, ← hr_eq] at hdel₂ rw [← hp_eq] at hxp rw [← hr_eq] at hxr funext i by_cases hip : i = p₁ · subst hip; exact hxp · by_cases hir : i = r₁ · subst hir; exact hxr · -- `i ≠ p₁` and `i ≠ r₁`: reconstruct from `del2 x₁ p₁ r₁ = del2 x₂ p₁ r₁`! have hdel_eq : del2 x₁ p₁ r₁ = del2 x₂ p₁ r₁ := hdel₁.trans hdel₂.symm have hipv : i.val ≠ p₁.val := fun h => hip (Fin.ext h) have hirv : i.val ≠ r₁.val := fun h => hir (Fin.ext h) by_cases hlt_p : i.val < p₁.val · let k : Fin (m + 1) := ⟨i.val, by omega⟩ have hv := congrFun hdel_eq k rw [del2_apply_lt x₁ p₁ r₁ hpr₁ k (by dsimp only [k]; omega), del2_apply_lt x₂ p₁ r₁ hpr₁ k (by dsimp only [k]; omega)] at hv have hi_eq : (⟨k.val, by omega⟩ : Fin (m + 3)) = i := Fin.ext rfl rwa [hi_eq] at hv · by_cases hlt_r : i.val < r₁.val · let k : Fin (m + 1) := ⟨i.val - 1, by omega⟩ have hv := congrFun hdel_eq k rw [del2_apply_mid x₁ p₁ r₁ hpr₁ k (by dsimp only [k]; omega) (by dsimp only [k]; omega), del2_apply_mid x₂ p₁ r₁ hpr₁ k (by dsimp only [k]; omega) (by dsimp only [k]; omega)] at hv have hi_eq : (⟨k.val + 1, by omega⟩ : Fin (m + 3)) = i := Fin.ext (by dsimp only [k]; omega) rwa [hi_eq] at hv · let k : Fin (m + 1) := ⟨i.val - 2, by omega⟩ have hv := congrFun hdel_eq k rw [del2_apply_gt x₁ p₁ r₁ hpr₁ k (by dsimp only [k]; omega), del2_apply_gt x₂ p₁ r₁ hpr₁ k (by dsimp only [k]; omega)] at hv have hi_eq : (⟨k.val + 2, by omega⟩ : Fin (m + 3)) = i := Fin.ext (by dsimp only [k]; omega) rwa [hi_eq] at hv calc _ ≤ Dom.card := hle _ = Pairs.card * ((Finset.univ : Finset (Fin q × Fin q)).card) := Finset.card_product _ _ _ = (m + 3).choose 2 * q ^ 2 := by rw [card_orderedPairs_fin (m + 3), Finset.card_univ, Fintype.card_prod, Fintype.card_fin, sq] /-- **2-Deletion Gilbert–Varshamov Existence Lower Bound**: For EVERY `q ≥ 1` and `m ≥ 0` (`n = m + 3`), there exists a `q`-ary 2-deletion code `C ⊆ Word q (m + 3)` satisfying `q^(m + 3) ≤ C.card * (C(m + 3, 2)^2 * q^2)`. -/ theorem exists_twoDelCode_gv_lower_bound (q m : ℕ) : ∃ C : Finset (Word q (m + 3)), IsTwoDelCode C ∧ q ^ (m + 3) ≤ C.card * ((m + 3).choose 2 ^ 2 * q ^ 2) := by classical -- Pick a maximum-cardinality 2-deletion code `C`! let Valid := (Finset.univ : Finset (Finset (Word q (m + 3)))).filter IsTwoDelCode have hValid_ne : Valid.Nonempty := ⟨∅, Finset.mem_filter.mpr ⟨Finset.mem_univ _, fun _ hx => False.elim (Finset.notMem_empty _ hx)⟩⟩ obtain ⟨C, hC_mem, hC_max⟩ := Finset.exists_max_image Valid Finset.card hValid_ne have hC : IsTwoDelCode C := (Finset.mem_filter.mp hC_mem).2 refine ⟨C, hC, ?_⟩ -- Every `x : Word q (m + 3)` shares a 2-deletion subsequence with some `c ∈ C`! let Conf : Word q (m + 3) → Finset (Word q (m + 3)) := fun c => (del2Ball c).biUnion fun y => (Finset.univ : Finset (Word q (m + 3))).filter (fun x => y ∈ del2Ball x) have hcover : (Finset.univ : Finset (Word q (m + 3))) ⊆ C.biUnion Conf := by intro x _ by_contra hnot -- Then `insert x C` is a strictly larger 2-deletion code! have hx_notC : x ∉ C := by intro hxC apply hnot refine Finset.mem_biUnion.mpr ⟨x, hxC, ?_⟩ obtain ⟨p, r, hpr⟩ : ∃ p r : Fin (m + 3), p.val < r.val := ⟨⟨0, by omega⟩, ⟨1, by omega⟩, by norm_num⟩ exact Finset.mem_biUnion.mpr ⟨del2 x p r, del2_mem_del2Ball x p r hpr, Finset.mem_filter.mpr ⟨Finset.mem_univ _, del2_mem_del2Ball x p r hpr⟩⟩ have hdisj_x : ∀ c ∈ C, Disjoint (del2Ball x) (del2Ball c) := by intro c hc rw [Finset.disjoint_left] intro y hyx hyc exact hnot (Finset.mem_biUnion.mpr ⟨c, hc, Finset.mem_biUnion.mpr ⟨y, hyc, Finset.mem_filter.mpr ⟨Finset.mem_univ _, hyx⟩⟩⟩) have hvalid_ins : IsTwoDelCode (insert x C) := by intro a ha b hb hab rcases Finset.mem_insert.mp ha with rfl | haC · rcases Finset.mem_insert.mp hb with rfl | hbC · exact False.elim (hab rfl) · exact hdisj_x b hbC · rcases Finset.mem_insert.mp hb with rfl | hbC · exact (hdisj_x a haC).symm · exact hC a haC b hbC hab have hle := hC_max (insert x C) (Finset.mem_filter.mpr ⟨Finset.mem_univ _, hvalid_ins⟩) rw [Finset.card_insert_of_notMem hx_notC] at hle omega have hconf_card : ∀ c : Word q (m + 3), (Conf c).card ≤ (m + 3).choose 2 ^ 2 * q ^ 2 := by intro c calc (Conf c).card ≤ ∑ y ∈ del2Ball c, ((Finset.univ : Finset (Word q (m + 3))).filter (fun x => y ∈ del2Ball x)).card := Finset.card_biUnion_le _ ≤ ∑ _y ∈ del2Ball c, ((m + 3).choose 2 * q ^ 2) := Finset.sum_le_sum fun y _ => card_twoInsBall_le y _ = (del2Ball c).card * ((m + 3).choose 2 * q ^ 2) := by rw [Finset.sum_const, smul_eq_mul] _ ≤ (m + 3).choose 2 * ((m + 3).choose 2 * q ^ 2) := Nat.mul_le_mul_right _ (card_del2Ball_le c) _ = (m + 3).choose 2 ^ 2 * q ^ 2 := by ring calc q ^ (m + 3) = (Finset.univ : Finset (Word q (m + 3))).card := by rw [Finset.card_univ, Fintype.card_fun, Fintype.card_fin, Fintype.card_fin] _ ≤ (C.biUnion Conf).card := Finset.card_le_card hcover _ ≤ ∑ c ∈ C, (Conf c).card := Finset.card_biUnion_le _ ≤ ∑ _c ∈ C, ((m + 3).choose 2 ^ 2 * q ^ 2) := Finset.sum_le_sum fun c _ => hconf_card c _ = C.card * ((m + 3).choose 2 ^ 2 * q ^ 2) := by rw [Finset.sum_const, smul_eq_mul] end GilbertVarshamov end TwoDeletionSecondOrder /-! ## Axiom Cleanliness Audit (`[propext, Classical.choice, Quot.sound]` only) -/ #print axioms TwoDeletionSecondOrder.twoDelCode_card_le_dual_sum #print axioms TwoDeletionSecondOrder.del2_injOn_nonRedundant #print axioms TwoDeletionSecondOrder.sum_del2Ball_ge_seven_buckets #print axioms TwoDeletionSecondOrder.runCount_del2_le_sub_two_sand_left #print axioms TwoDeletionSecondOrder.runCount_del2_le_sub_two_NN #print axioms TwoDeletionSecondOrder.runCount_del2_le_sub_three_NS #print axioms TwoDeletionSecondOrder.runCount_del2_le_sub_four_SS #print axioms TwoDeletionSecondOrder.singletonRunCount_delPos_le_add_one #print axioms TwoDeletionSecondOrder.sandwichCount_delPos_le_add_two #print axioms TwoDeletionSecondOrder.twoDelDualWeight_nonneg #print axioms TwoDeletionSecondOrder.twoDelDualWeight_le_kk2DualWeight #print axioms TwoDeletionSecondOrder.twoDelDualWeight_feasible #print axioms TwoDeletionSecondOrder.kk2DualWeight_feasible #print axioms TwoDeletionSecondOrder.twoDelCode_card_le_twoDelDualWeight_sum #print axioms TwoDeletionSecondOrder.twoDelDualWeight_sum_lt_kk2DualWeight_sum #print axioms TwoDeletionSecondOrder.card_boundaryFiber #print axioms TwoDeletionSecondOrder.card_boundaryFiber_sandwich #print axioms TwoDeletionSecondOrder.card_wordsWithRuns #print axioms TwoDeletionSecondOrder.sum_singletonRunCount_over_runs #print axioms TwoDeletionSecondOrder.sum_sandwichCount_over_runs #print axioms TwoDeletionSecondOrder.kk2DualWeight_sum_eq #print axioms TwoDeletionSecondOrder.kk2DualWeight_sum_lt_closed_form #print axioms TwoDeletionSecondOrder.twoDelDualWeight_saving_ge #print axioms TwoDeletionSecondOrder.twoDelCode_explicit_second_order_bound #print axioms TwoDeletionSecondOrder.twoDelCode_asymptotic_second_order #print axioms TwoDeletionSecondOrder.binary_twoDelCode_second_order #print axioms TwoDeletionSecondOrder.dna_twoDelCode_second_order #print axioms TwoDeletionSecondOrder.card_del2Ball_le #print axioms TwoDeletionSecondOrder.card_twoInsBall_le #print axioms TwoDeletionSecondOrder.exists_twoDelCode_gv_lower_bound