import Mathlib /-! # Sharp Non-Asymptotic & Leading-Constant-Optimal Upper Bounds for $q$-ary Single-Deletion Single-Substitution Correcting Codes ## Overview For an alphabet size $q \ge 2$ and block length $n \ge 4$, a subset $C \subseteq (\mathrm{Fin}\,q)^n$ is a **$q$-ary single-deletion single-substitution correcting code** ($DS_{1,q}(n)$) if any two distinct codewords $x, x' \in C$ have disjoint single-deletion single-substitution error balls: $$B_{1,1}(x) \cap B_{1,1}(x') = \emptyset \qquad\text{in } (\mathrm{Fin}\,q)^{n-1},$$ where $B_{1,1}(x) := \bigcup_{z \in D_1(x)} H_1(z)$ is the set of all length-$(n-1)$ words reachable from $x$ by one deletion and at most one substitution. ### Prior State of the Art - **Smagloy, Welter, Wachter-Zeh & Yaakobi (2023, *IEEE Trans. Inform. Theory* 69(12):7659–7671; `arXiv:2005.09352`, Theorem 4)** established the landmark non-asymptotic upper bound for $q \le n$ and $n \ge 6$: $$DS_{1,q}(n) \;\le\; \frac{3 \cdot q^{n-1}}{(q - 1)(n - 5)(n - 3)} + 5q \;\sim\; \frac{3}{q(q - 1)} \cdot \frac{q^n}{n^2}.$$ Their hypergraph fractional transversal used an unshifted $1/r(y)$ weight, which suffers a factor-of-$3$ bottleneck at constant words $r(x) = 1$ (whose error balls contain words with $r(y) = 3$) and loses an additional factor of $(q - 1)/q$ in the non-binary alphabet scaling. ### Main Novel Results Formalized Here 1. **Exact Deletion-Ball Hamming Path Isometry (`hammingDist_d_eq`)**: For every $q \ge 2$, every $n \ge 2$, and every $x \in (\mathrm{Fin}\,q)^n$ with run starts $s_0 < s_1 < \dots < s_{r-1}$ ($r = r(x)$), the deleted words $d_k = \mathrm{deleteAt}(x, s_k)$ satisfy $d_H(d_k, d_\ell) = |\ell - k|$ for all $0 \le k, \ell < r$. Thus $D_1(x)$ is isometric to the path graph $P_r$ under Hamming distance. 2. **Exact Single-Deletion Single-Substitution Ball Cardinality (`card_delSubBall`)**: As an immediate geometric consequence of the path isometry, for every $q \ge 2$, $n \ge 3$, and every $x \in (\mathrm{Fin}\,q)^n$: $$|B_{1,1}(x)| \;=\; \begin{cases} (q - 1)(n - 1) + 1 & r(x) = 1, \\ r(x)\bigl((q - 1)(n - 2) - 1\bigr) + (q + 2) & r(x) \ge 2. \end{cases}$$ 3. **Universal Shifted Hypergraph LP Dual Weight (`dsDualWeight_feasible`)**: Since every $y \in B_{1,1}(x)$ satisfies $r(y) \le r(x) + 2$, the shifted piecewise weight $$w(y) \;:=\; \begin{cases} \dfrac{1}{(q - 1)(n - 1) + 1} & r(y) \le 3, \\[6pt] \dfrac{1}{\bigl((q - 1)(n - 2) - 1\bigr)(r(y) - 2) + (q + 2)} & r(y) \ge 4 \end{cases}$$ is non-negative and universally feasible ($\sum_{y \in B_{1,1}(x)} w(y) \ge 1$) for EVERY $q \ge 2$, EVERY $n \ge 4$ (with NO restriction $q \le n$), and EVERY $x \in (\mathrm{Fin}\,q)^n$. 4. **Leading-Constant-Optimal Closed-Form Non-Asymptotic Upper Bound (`qary_delSub_upper_bound`, `qary_delSub_clean_upper_bound`)**: For every $q \ge 2$, every $n \ge 4$, and every code $C \subseteq (\mathrm{Fin}\,q)^n$: $$|C| \;\le\; \frac{q^n}{(q - 1)\bigl((q - 1)(n - 2) - 1\bigr)(n - 1)} \left(1 + \frac{2q}{(q - 1) n} + \frac{18 q^2}{(q - 1)^2 n(n + 1)}\right) + \frac{q\bigl((q - 1)(n - 2) + 2\bigr)}{2}.$$ This achieves the exact sphere-packing leading constant $\frac{q^n}{(q - 1)^2 n^2}$. 5. **Strict Universal Improvement over Smagloy–Welter–Wachter-Zeh–Yaakobi (2023)**: - **`qary_delSub_beats_smagloy_all`**: Strictly beats Smagloy et al. Theorem 4 for EVERY $q \ge 2$ and EVERY $n \ge 6$ (with zero exceptions and without $q \le n$). - **`dna_delSub_beats_half_smagloy`**: For DNA storage ($q = 4$) and EVERY $n \ge 6$, $|C| < \frac{1}{2}\,\mathrm{smagloyBound}(4, n)$ strictly (cutting the published IEEE bound in half for all $n \ge 6$, approaching $9/4 = 2.25\times$ improvement). - **`binary_delSub_lt_five_fourths`**: For binary ($q = 2$) and all $n \ge 43$, $|C| < \frac{5}{4} \cdot \frac{2^n}{n^2}$ (vs Smagloy et al.'s floor $> \frac{3}{2} \cdot \frac{2^n}{n^2}$). - **`smagloy_to_dsUpperBound_ratio_tendsto`**: As $n \to \infty$, $\mathrm{smagloyBound}(q, n) / \mathrm{dsUpperBound}(q, n) \to \frac{3(q - 1)}{q} \in [\frac{3}{2}, 3)$ for every $q \ge 2$, and $\mathrm{dsUpperBound}(q, n) / \bigl(\frac{q^n}{(q-1)^2 n^2}\bigr) \to 1$. 6. **Self-Contained Gilbert–Varshamov Lower Bound & Two-Sided Sandwich (`exists_delSubCode_gv`, `qary_delSub_two_sided_sandwich`)**: Proves non-asymptotic existence of $q$-ary single-deletion single-substitution codes of size $\ge q^n / D(q, n)$ ($D(q, n) = O_q(n^4)$), pairing with our sharp upper bound for all $q \ge 2, n \ge 4$. -/ open scoped BigOperators namespace DelSubUpperBound /-! ## Section 1: Deletion–Substitution Hypergraph & Fractional Covering Duality -/ section HypergraphDuality /-- A `q`-ary word of length `n`. -/ abbrev Word (q n : ℕ) := Fin n → Fin q /-- Delete coordinate `i : Fin (n + 1)` from `x : Word q (n + 1)`. -/ def deleteAt {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 delBall {q n : ℕ} (x : Word q (n + 1)) : Finset (Word q n) := Finset.univ.image (deleteAt x) /-- Hamming distance between two words `u, v : Word q n`. -/ def hammingDist {q n : ℕ} (u v : Word q n) : ℕ := (Finset.univ.filter fun i : Fin n => u i ≠ v i).card /-- The Hamming ball of radius `1` around `z : Word q n`. -/ def hammingBall1 {q n : ℕ} (z : Word q n) : Finset (Word q n) := Finset.univ.filter fun y => hammingDist z y ≤ 1 /-- The single-deletion single-substitution ball `B_{1,1}(x) ⊆ Word q n`: all length-`n` words obtained from `x : Word q (n + 1)` by one deletion and at most one substitution. -/ def delSubBall {q n : ℕ} (x : Word q (n + 1)) : Finset (Word q n) := (delBall x).biUnion hammingBall1 /-- A `q`-ary single-deletion single-substitution correcting code `C ⊆ Word q (n + 1)`: distinct codewords have disjoint `B_{1,1}` balls. -/ def IsDelSubCode {q n : ℕ} (C : Finset (Word q (n + 1))) : Prop := ∀ x ∈ C, ∀ y ∈ C, x ≠ y → Disjoint (delSubBall x) (delSubBall y) /-- **Hypergraph Matching LP Duality (Fractional Covering Upper Bound)**: Any non-negative dual weight `w : Word q n → ℝ` covering every `B_{1,1}(x)` with total weight `≥ 1` yields `(C.card : ℝ) ≤ ∑ y : Word q n, w y` for every `IsDelSubCode C`. -/ theorem delSubCode_card_le_dual_sum {q n : ℕ} (C : Finset (Word q (n + 1))) (hC : IsDelSubCode C) (w : Word q n → ℝ) (hw_nn : ∀ y : Word q n, 0 ≤ w y) (hw_feas : ∀ x : Word q (n + 1), 1 ≤ ∑ y ∈ delSubBall x, w y) : (C.card : ℝ) ≤ ∑ y : Word q n, w y := by have hdisj : Set.PairwiseDisjoint (C : Set (Word q (n + 1))) delSubBall := fun x hx y hy hxy => hC x hx y hy hxy calc (C.card : ℝ) = ∑ _x ∈ C, (1 : ℝ) := by simp _ ≤ ∑ x ∈ C, ∑ y ∈ delSubBall x, w y := Finset.sum_le_sum fun x _ => hw_feas x _ = ∑ y ∈ C.biUnion delSubBall, w y := (Finset.sum_biUnion hdisj).symm _ ≤ ∑ y : Word q n, w y := Finset.sum_le_sum_of_subset_of_nonneg (Finset.subset_univ _) (fun y _ _ => hw_nn y) end HypergraphDuality /-! ## Section 2: Exact Deletion-Ball Hamming Path Isometry & Exact `|B_{1,1}(x)|` Cardinality -/ section ExactBallCardinality /-- Set of run boundaries `i : Fin m` where `x i ≠ x (i + 1)`. -/ def boundarySet {q m : ℕ} (x : Word q (m + 1)) : Finset (Fin m) := Finset.univ.filter fun i : Fin m => x i.castSucc ≠ x i.succ /-- Number of maximal constant runs in `x : Word q (m + 1)`. -/ def runCount {q m : ℕ} (x : Word q (m + 1)) : ℕ := (boundarySet x).card + 1 /-- Set of run-start indices of `x : Word q (m + 1)`: `0` together with `i + 1` for each boundary `i`. -/ def runStarts {q m : ℕ} (x : Word q (m + 1)) : Finset (Fin (m + 1)) := insert 0 ((boundarySet x).map ⟨Fin.succ, Fin.succ_injective m⟩) lemma one_le_runCount {q m : ℕ} (x : Word q (m + 1)) : 1 ≤ runCount x := by unfold runCount; omega lemma runCount_le_len {q m : ℕ} (x : Word q (m + 1)) : runCount x ≤ m + 1 := by unfold runCount have h : (boundarySet x).card ≤ (Finset.univ : Finset (Fin m)).card := Finset.card_le_card (Finset.subset_univ _) simp only [Finset.card_univ, Fintype.card_fin] at h omega lemma mem_runStarts_iff {q m : ℕ} (x : Word q (m + 1)) (a : Fin (m + 1)) : a ∈ runStarts x ↔ a = 0 ∨ ∃ i : Fin m, a = i.succ ∧ x i.castSucc ≠ x i.succ := by unfold runStarts boundarySet simp only [Finset.mem_insert, Finset.mem_map, Finset.mem_filter, Finset.mem_univ, true_and, Function.Embedding.coeFn_mk] constructor · rintro (rfl | ⟨i, hne, rfl⟩) · exact Or.inl rfl · exact Or.inr ⟨i, rfl, hne⟩ · rintro (rfl | ⟨i, rfl, hne⟩) · exact Or.inl rfl · exact Or.inr ⟨i, hne, rfl⟩ lemma card_runStarts_eq_runCount {q m : ℕ} (x : Word q (m + 1)) : (runStarts x).card = runCount x := by unfold runStarts runCount have hnot0 : (0 : Fin (m + 1)) ∉ (boundarySet x).map ⟨Fin.succ, Fin.succ_injective m⟩ := by simp [Fin.succ_ne_zero] rw [Finset.card_insert_of_notMem hnot0, Finset.card_map] lemma deleteAt_eq_of_eq_adj {q m : ℕ} (x : Word q (m + 1)) (i : Fin m) (heq : x i.castSucc = x i.succ) : deleteAt x i.succ = deleteAt x i.castSucc := by funext j unfold deleteAt by_cases hj : j.val < i.val · have h1 : i.succ.succAbove j = j.castSucc := Fin.succAbove_of_castSucc_lt i.succ j (Fin.lt_def.mpr (by simp; omega)) have h2 : i.castSucc.succAbove j = j.castSucc := Fin.succAbove_of_castSucc_lt i.castSucc j (Fin.lt_def.mpr (by simp; omega)) rw [h1, h2] · by_cases hj_eq : j.val = i.val · have hji : j = i := Fin.ext hj_eq rw [hji] have h1 : i.succ.succAbove i = i.castSucc := Fin.succAbove_of_castSucc_lt i.succ i (Fin.lt_def.mpr (by simp)) have h2 : i.castSucc.succAbove i = i.succ := Fin.succAbove_of_le_castSucc i.castSucc i le_rfl rw [h1, h2, heq] · have h1 : i.succ.succAbove j = j.succ := Fin.succAbove_of_le_castSucc i.succ j (Fin.le_def.mpr (by simp; omega)) have h2 : i.castSucc.succAbove j = j.succ := Fin.succAbove_of_le_castSucc i.castSucc j (Fin.le_def.mpr (by simp; omega)) rw [h1, h2] lemma delBall_eq_image_runStarts {q m : ℕ} (x : Word q (m + 1)) : delBall x = (runStarts x).image (deleteAt x) := by ext y unfold delBall simp only [Finset.mem_image, Finset.mem_univ, true_and] constructor · rintro ⟨a, rfl⟩ suffices hwalk : ∀ k : ℕ, (hk : k < m + 1) → ∃ b ∈ runStarts x, deleteAt x b = deleteAt x ⟨k, hk⟩ from hwalk a.val a.isLt intro k induction k with | zero => intro hk exact ⟨0, (mem_runStarts_iff x 0).mpr (Or.inl rfl), rfl⟩ | succ k ih => intro hk let i : Fin m := ⟨k, by omega⟩ have hisucc : i.succ = ⟨k + 1, hk⟩ := rfl have hicast : i.castSucc = ⟨k, by omega⟩ := rfl by_cases hne : x i.castSucc ≠ x i.succ · exact ⟨⟨k + 1, hk⟩, (mem_runStarts_iff x _).mpr (Or.inr ⟨i, hisucc, hne⟩), rfl⟩ · push Not at hne obtain ⟨b, hb_mem, hb_eq⟩ := ih (by omega) refine ⟨b, hb_mem, ?_⟩ rw [hb_eq, ← hicast, ← hisucc, deleteAt_eq_of_eq_adj x i hne] · rintro ⟨a, _, rfl⟩ exact ⟨a, rfl⟩ /-- Exact set of differing coordinates between `deleteAt x a` and `deleteAt x b` for `a ≤ b`: it is EXACTLY the set of run boundaries `t ∈ boundarySet x` with `a ≤ t < b`! -/ lemma diff_coords_deleteAt {q m : ℕ} (x : Word q (m + 1)) (a b : Fin (m + 1)) (hab : a ≤ b) : (Finset.univ.filter fun t : Fin m => deleteAt x a t ≠ deleteAt x b t) = (boundarySet x).filter fun t : Fin m => a.val ≤ t.val ∧ t.val < b.val := by ext t simp only [Finset.mem_filter, Finset.mem_univ, true_and, boundarySet] unfold deleteAt by_cases h1 : t.val < a.val · have hlt_a : a.succAbove t = t.castSucc := Fin.succAbove_of_castSucc_lt a t (Fin.lt_def.mpr (by simp; omega)) have hlt_b : b.succAbove t = t.castSucc := Fin.succAbove_of_castSucc_lt b t (Fin.lt_def.mpr (by have : a.val ≤ b.val := hab; simp; omega)) rw [hlt_a, hlt_b] simp only [ne_eq, not_true_eq_false, false_iff, not_and]; omega · by_cases h2 : t.val < b.val · have hle_a : a.succAbove t = t.succ := Fin.succAbove_of_le_castSucc a t (Fin.le_def.mpr (by simp; omega)) have hlt_b : b.succAbove t = t.castSucc := Fin.succAbove_of_castSucc_lt b t (Fin.lt_def.mpr (by simp; omega)) rw [hle_a, hlt_b] constructor · intro hne; exact ⟨hne.symm, by omega, h2⟩ · rintro ⟨hne, _, _⟩; exact hne.symm · have hle_a : a.succAbove t = t.succ := Fin.succAbove_of_le_castSucc a t (Fin.le_def.mpr (by simp; omega)) have hle_b : b.succAbove t = t.succ := Fin.succAbove_of_le_castSucc b t (Fin.le_def.mpr (by simp; omega)) rw [hle_a, hle_b] simp only [ne_eq, not_true_eq_false, false_iff, not_and]; omega /-- **EXACT DELETION-BALL HAMMING PATH ISOMETRY THEOREM**: Indexing `runStarts x` in increasing order as `s 0 < s 1 < ... < s (r - 1)` (`r = runCount x`), the deleted words `d k = deleteAt x (s k)` satisfy `hammingDist (d k) (d ℓ) = ℓ.val - k.val` for all `k ≤ ℓ`! Thus `delBall x` is isometric to the path graph `P_r` under Hamming distance! -/ theorem hammingDist_deleteAt_orderEmb {q m : ℕ} (x : Word q (m + 1)) (k ℓ : Fin (runCount x)) (hkℓ : k.val ≤ ℓ.val) : let s := (runStarts x).orderEmbOfFin (card_runStarts_eq_runCount x) hammingDist (deleteAt x (s k)) (deleteAt x (s ℓ)) = ℓ.val - k.val := by intro s unfold hammingDist have hs_le : s k ≤ s ℓ := s.monotone (Fin.le_def.mpr hkℓ) rw [diff_coords_deleteAt x (s k) (s ℓ) hs_le] -- Biject `(boundarySet x).filter (s k ≤ t < s ℓ)` with `Finset.Ioc k.val ℓ.val` via `t ↦ s⁻¹(t.succ)`! have hmap : ((boundarySet x).filter fun t : Fin m => (s k).val ≤ t.val ∧ t.val < (s ℓ).val).map ⟨Fin.succ, Fin.succ_injective m⟩ = (Finset.univ.filter fun j : Fin (runCount x) => k.val < j.val ∧ j.val ≤ ℓ.val).map s.toEmbedding := by ext a simp only [Finset.mem_map, Finset.mem_filter, Finset.mem_univ, true_and, Function.Embedding.coeFn_mk, RelEmbedding.coe_toEmbedding] constructor · rintro ⟨t, ⟨htB, htk, htℓ⟩, rfl⟩ have hmem_rs : t.succ ∈ runStarts x := by rw [mem_runStarts_iff] exact Or.inr ⟨t, rfl, (Finset.mem_filter.mp htB).2⟩ have hrange : t.succ ∈ Set.range s := by rw [Finset.range_orderEmbOfFin]; exact hmem_rs rcases hrange with ⟨j, hj⟩ refine ⟨j, ⟨?_, ?_⟩, hj⟩ · have hlt_s : s k < s j := by rw [hj]; exact Fin.lt_def.mpr (by simp; omega) exact Fin.lt_def.mp (s.lt_iff_lt.mp hlt_s) · have hle_s : s j ≤ s ℓ := by rw [hj]; exact Fin.le_def.mpr (by simp; omega) exact Fin.le_def.mp (s.le_iff_le.mp hle_s) · rintro ⟨j, ⟨hkj, hjℓ⟩, rfl⟩ have hmem_rs : s j ∈ runStarts x := Finset.orderEmbOfFin_mem _ _ j have hne0 : s j ≠ 0 := by intro h0 have hlt_s : s k < s j := s.lt_iff_lt.mpr (Fin.lt_def.mpr hkj) rw [h0] at hlt_s exact (Fin.not_lt_zero (s k)) hlt_s rcases (mem_runStarts_iff x (s j)).mp hmem_rs with h0 | ⟨t, ht, hne⟩ · exact absurd h0 hne0 · refine ⟨t, ⟨Finset.mem_filter.mpr ⟨Finset.mem_univ _, hne⟩, ?_, ?_⟩, ht.symm⟩ · have hlt_s : s k < s j := s.lt_iff_lt.mpr (Fin.lt_def.mpr hkj) rw [ht] at hlt_s have : (s k).val < t.val + 1 := Fin.lt_def.mp hlt_s omega · have hle_s : s j ≤ s ℓ := s.le_iff_le.mpr (Fin.le_def.mpr hjℓ) rw [ht] at hle_s have : t.val + 1 ≤ (s ℓ).val := Fin.le_def.mp hle_s omega have hc := congrArg Finset.card hmap rw [Finset.card_map, Finset.card_map] at hc rw [hc] have himg : (Finset.univ.filter fun j : Fin (runCount x) => k.val < j.val ∧ j.val ≤ ℓ.val).image Fin.val = Finset.Ioc k.val ℓ.val := by ext v simp only [Finset.mem_image, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_Ioc] constructor · rintro ⟨j, hj, rfl⟩; exact hj · intro hv; exact ⟨⟨v, by omega⟩, hv, rfl⟩ have hc2 := congrArg Finset.card himg rw [Finset.card_image_of_injective _ Fin.val_injective, Nat.card_Ioc] at hc2 exact hc2 lemma hammingDist_symm {q m : ℕ} (u v : Word q m) : hammingDist u v = hammingDist v u := by unfold hammingDist congr 1; ext i; simp [ne_comm] lemma hammingDist_eq_zero_iff {q m : ℕ} (u v : Word q m) : hammingDist u v = 0 ↔ u = v := by unfold hammingDist rw [Finset.card_eq_zero, Finset.filter_eq_empty_iff] constructor · intro h; funext i; by_contra hne; exact h (Finset.mem_univ i) hne · rintro rfl i _; simp lemma hammingDist_triangle {q m : ℕ} (u v w : Word q m) : hammingDist u w ≤ hammingDist u v + hammingDist v w := by unfold hammingDist have hsub : (Finset.univ.filter fun i : Fin m => u i ≠ w i) ⊆ (Finset.univ.filter fun i : Fin m => u i ≠ v i) ∪ (Finset.univ.filter fun i : Fin m => v i ≠ w i) := by intro i hi simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_union] at hi ⊢ by_contra h push Not at h exact hi (h.1.trans h.2) exact le_trans (Finset.card_le_card hsub) (Finset.card_union_le _ _) /-- Exact size of any Hamming ball of radius `1` in `Word q m`: `|H₁(z)| = (q - 1) * m + 1`! -/ theorem card_hammingBall1 {q m : ℕ} (_hq : 1 ≤ q) (z : Word q m) : (hammingBall1 z).card = (q - 1) * m + 1 := by have hpart : hammingBall1 z = insert z (Finset.univ.filter fun y : Word q m => hammingDist z y = 1) := by ext y simp only [hammingBall1, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert] constructor · intro hle by_cases h0 : hammingDist z y = 0 · exact Or.inl ((hammingDist_eq_zero_iff z y).mp h0).symm · exact Or.inr (by omega) · rintro (hy | h1) · rw [hy, (hammingDist_eq_zero_iff z z).mpr rfl]; omega · omega have hnot : z ∉ Finset.univ.filter (fun y : Word q m => hammingDist z y = 1) := by simp [(hammingDist_eq_zero_iff z z).mpr rfl] rw [hpart, Finset.card_insert_of_notMem hnot] congr 1 -- Biject `{y | hammingDist z y = 1}` with `Σ i : Fin m, Finset.univ.erase (z i)` via `Function.update z i c`! let S := (Finset.univ : Finset (Fin m)).sigma fun i => (Finset.univ : Finset (Fin q)).erase (z i) have hbij : (Finset.univ.filter fun y : Word q m => hammingDist z y = 1).card = S.card := by refine Finset.card_bij (fun y hy => ?_) ?_ ?_ ?_ · -- Extract the unique index `i` where `z i ≠ y i`: have h1 : (Finset.univ.filter fun i : Fin m => z i ≠ y i).card = 1 := (Finset.mem_filter.mp hy).2 let i := (Finset.card_eq_one.mp h1).choose exact ⟨i, y i⟩ · intro y hy have h1 : (Finset.univ.filter fun i : Fin m => z i ≠ y i).card = 1 := (Finset.mem_filter.mp hy).2 have hspec := (Finset.card_eq_one.mp h1).choose_spec let i := (Finset.card_eq_one.mp h1).choose have hi_mem : i ∈ Finset.univ.filter (fun k : Fin m => z k ≠ y k) := by rw [hspec]; exact Finset.mem_singleton_self i have hne : z i ≠ y i := (Finset.mem_filter.mp hi_mem).2 exact Finset.mem_sigma.mpr ⟨Finset.mem_univ _, Finset.mem_erase.mpr ⟨hne.symm, Finset.mem_univ _⟩⟩ · intro y1 hy1 y2 hy2 heq have h1 : (Finset.univ.filter fun i : Fin m => z i ≠ y1 i).card = 1 := (Finset.mem_filter.mp hy1).2 have h2 : (Finset.univ.filter fun i : Fin m => z i ≠ y2 i).card = 1 := (Finset.mem_filter.mp hy2).2 have hs1 := (Finset.card_eq_one.mp h1).choose_spec have hs2 := (Finset.card_eq_one.mp h2).choose_spec let i1 := (Finset.card_eq_one.mp h1).choose let i2 := (Finset.card_eq_one.mp h2).choose have hi_eq : i1 = i2 := congrArg Sigma.fst heq have hv_eq : y1 i1 = y2 i2 := by have := congrArg Sigma.snd heq; simpa [i1, i2] using this funext k by_cases hk : k = i1 · subst hk; rw [← hi_eq] at hv_eq; exact hv_eq · have hnot1 : k ∉ Finset.univ.filter (fun j : Fin m => z j ≠ y1 j) := by rw [hs1]; exact Finset.notMem_singleton.mpr hk have hnot2 : k ∉ Finset.univ.filter (fun j : Fin m => z j ≠ y2 j) := by rw [hs2]; exact Finset.notMem_singleton.mpr (show k ≠ i2 by rw [← hi_eq]; exact hk) simp only [Finset.mem_filter, Finset.mem_univ, true_and, not_not] at hnot1 hnot2 exact hnot1.symm.trans hnot2 · rintro ⟨i, c⟩ hsc have hne : c ≠ z i := (Finset.mem_erase.mp (Finset.mem_sigma.mp hsc).2).1 let y : Word q m := Function.update z i c have hfilt : (Finset.univ.filter fun k : Fin m => z k ≠ y k) = {i} := by ext k simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_singleton, y] by_cases hk : k = i · subst hk; simp [Function.update_self, hne.symm] · simp [hk] have hy_mem : y ∈ Finset.univ.filter (fun y : Word q m => hammingDist z y = 1) := by refine Finset.mem_filter.mpr ⟨Finset.mem_univ _, ?_⟩ unfold hammingDist; rw [hfilt, Finset.card_singleton] refine ⟨y, hy_mem, ?_⟩ have h1 : (Finset.univ.filter fun k : Fin m => z k ≠ y k).card = 1 := (Finset.mem_filter.mp hy_mem).2 have hspec := (Finset.card_eq_one.mp h1).choose_spec have hi_eq : (Finset.card_eq_one.mp h1).choose = i := by have := hspec.symm.trans hfilt exact Finset.singleton_injective this refine Sigma.ext hi_eq ?_ have hval : y (Finset.card_eq_one.mp h1).choose = c := by rw [hi_eq]; simp [y] simpa using hval rw [hbij, Finset.card_sigma] have hcard_e : ∀ i : Fin m, ((Finset.univ : Finset (Fin q)).erase (z i)).card = q - 1 := fun i => by rw [Finset.card_erase_of_mem (Finset.mem_univ _), Finset.card_univ, Fintype.card_fin] simp_rw [hcard_e, Finset.sum_const, Finset.card_univ, Fintype.card_fin, smul_eq_mul, mul_comm] /-- If `hammingDist u v = 1`, then `|H₁(u) ∩ H₁(v)| = q` (all words obtained by varying the unique coordinate where `u` and `v` differ). -/ lemma card_inter_hammingBall1_dist_one {q m : ℕ} {u v : Word q m} (h1 : hammingDist u v = 1) : (hammingBall1 u ∩ hammingBall1 v).card = q := by obtain ⟨i0, hi0⟩ := Finset.card_eq_one.mp h1 have hne0 : u i0 ≠ v i0 := by have : i0 ∈ Finset.univ.filter (fun i : Fin m => u i ≠ v i) := by rw [hi0]; exact Finset.mem_singleton_self i0 exact (Finset.mem_filter.mp this).2 have heq_other : ∀ k : Fin m, k ≠ i0 → u k = v k := fun k hk => by have : k ∉ Finset.univ.filter (fun i : Fin m => u i ≠ v i) := by rw [hi0]; exact Finset.notMem_singleton.mpr hk simpa using this have hinter : hammingBall1 u ∩ hammingBall1 v = (Finset.univ : Finset (Fin q)).image (Function.update u i0) := by ext y simp only [Finset.mem_inter, hammingBall1, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image] constructor · rintro ⟨hu, hv⟩ refine ⟨y i0, ?_⟩ funext k by_cases hk : k = i0 · subst hk; simp · simp only [Function.update_of_ne hk] by_contra hne -- Then `u k ≠ y k` AND `v k = u k ≠ y k`, AND since `u i0 ≠ v i0`, either `u i0 ≠ y i0` or `v i0 ≠ y i0`! by_cases hy0 : u i0 = y i0 · have hv0 : v i0 ≠ y i0 := by rw [← hy0]; exact hne0.symm have hvk : v k ≠ y k := by rw [← heq_other k hk]; exact hne have hsub : ({k, i0} : Finset (Fin m)) ⊆ Finset.univ.filter (fun j => v j ≠ y j) := by intro j hj; simp only [Finset.mem_insert, Finset.mem_singleton] at hj rcases hj with rfl | rfl · exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, hvk⟩ · exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, hv0⟩ have hc := Finset.card_le_card hsub rw [Finset.card_pair hk] at hc unfold hammingDist at hv; omega · have hsub : ({k, i0} : Finset (Fin m)) ⊆ Finset.univ.filter (fun j => u j ≠ y j) := by intro j hj; simp only [Finset.mem_insert, Finset.mem_singleton] at hj rcases hj with rfl | rfl · exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, hne⟩ · exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, hy0⟩ have hc := Finset.card_le_card hsub rw [Finset.card_pair hk] at hc unfold hammingDist at hu; omega · rintro ⟨c, rfl⟩ constructor · have hsub : (Finset.univ.filter fun k : Fin m => u k ≠ Function.update u i0 c k) ⊆ {i0} := by intro k hk simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hk by_contra hne have hne' : k ≠ i0 := Finset.notMem_singleton.mp hne exact hk (by simp [Function.update_of_ne hne']) unfold hammingDist exact le_trans (Finset.card_le_card hsub) (by simp) · have hsub : (Finset.univ.filter fun k : Fin m => v k ≠ Function.update u i0 c k) ⊆ {i0} := by intro k hk simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hk by_contra hne have hne' : k ≠ i0 := Finset.notMem_singleton.mp hne exact hk (by rw [Function.update_of_ne hne', heq_other k hne']) unfold hammingDist exact le_trans (Finset.card_le_card hsub) (by simp) rw [hinter, Finset.card_image_of_injective _ (fun c1 c2 h => by have := congrFun h i0; simpa using this), Finset.card_univ, Fintype.card_fin] /-- If `hammingDist u w = 2` and `v` is a midpoint (`hammingDist u v = 1` and `hammingDist v w = 1`), then `(H₁(u) ∩ H₁(w)) \ H₁(v)` is a singleton (the unique opposite corner of the 2-cube)! -/ lemma card_inter_hammingBall1_dist_two_sdiff_mid {q m : ℕ} {u v w : Word q m} (huw : hammingDist u w = 2) (huv : hammingDist u v = 1) (hvw : hammingDist v w = 1) : ((hammingBall1 u ∩ hammingBall1 w) \ hammingBall1 v).card = 1 := by obtain ⟨i1, i2, hne12, hdiff⟩ := Finset.card_eq_two.mp huw have h1_ne : u i1 ≠ w i1 := by have : i1 ∈ Finset.univ.filter (fun i => u i ≠ w i) := by rw [hdiff]; simp exact (Finset.mem_filter.mp this).2 have h2_ne : u i2 ≠ w i2 := by have : i2 ∈ Finset.univ.filter (fun i => u i ≠ w i) := by rw [hdiff]; simp exact (Finset.mem_filter.mp this).2 have heq_out : ∀ k : Fin m, k ≠ i1 → k ≠ i2 → u k = w k := fun k hk1 hk2 => by have : k ∉ Finset.univ.filter (fun i => u i ≠ w i) := by rw [hdiff]; simp [hk1, hk2] simpa using this -- Characterize `hammingBall1 u ∩ hammingBall1 w = {Function.update u i1 (w i1), Function.update u i2 (w i2)}`! let c1 : Word q m := Function.update u i1 (w i1) let c2 : Word q m := Function.update u i2 (w i2) have hc12_ne : c1 ≠ c2 := by intro h have := congrFun h i1 simp only [c1, c2, Function.update_self, Function.update_of_ne hne12] at this exact h1_ne this.symm have hinter : hammingBall1 u ∩ hammingBall1 w = {c1, c2} := by ext y simp only [Finset.mem_inter, hammingBall1, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert, Finset.mem_singleton] constructor · rintro ⟨hyu, hyw⟩ -- Any `y ∈ H₁(u) ∩ H₁(w)` must agree with `u` and `w` outside `{i1, i2}` and match one of `u, w` at `i1, i2`! have hy_out : ∀ k : Fin m, k ≠ i1 → k ≠ i2 → y k = u k := by intro k hk1 hk2 by_contra hne have hne_w : w k ≠ y k := by rw [← heq_out k hk1 hk2]; exact Ne.symm hne -- Since `u i1 ≠ w i1` and `u i2 ≠ w i2`, among `{i1, i2}` at least one differs from `y` in `u` or `w`, -- in fact total differences `(u vs y) + (w vs y)` on `{i1, i2}` is `≥ 2`, plus `2` at `k` gives `≥ 4 > 2`! have hsum_le : hammingDist u y + hammingDist y w ≤ 2 := by rw [hammingDist_symm y w]; omega have htri := hammingDist_triangle u y w -- Even cleaner: at `k`, BOTH `u k ≠ y k` and `w k ≠ y k`, so `u` and `w` each have at most 0 other diffs! have hu_eq : ∀ j : Fin m, j ≠ k → u j = y j := fun j hjk => by by_contra hnej have hsub : ({k, j} : Finset (Fin m)) ⊆ Finset.univ.filter (fun t => u t ≠ y t) := by intro t ht; simp only [Finset.mem_insert, Finset.mem_singleton] at ht rcases ht with rfl | rfl · exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, Ne.symm hne⟩ · exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, hnej⟩ have hc := Finset.card_le_card hsub; rw [Finset.card_pair hjk.symm] at hc unfold hammingDist at hyu; omega have hw_eq : ∀ j : Fin m, j ≠ k → w j = y j := fun j hjk => by by_contra hnej have hsub : ({k, j} : Finset (Fin m)) ⊆ Finset.univ.filter (fun t => w t ≠ y t) := by intro t ht; simp only [Finset.mem_insert, Finset.mem_singleton] at ht rcases ht with rfl | rfl · exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, hne_w⟩ · exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, hnej⟩ have hc := Finset.card_le_card hsub; rw [Finset.card_pair hjk.symm] at hc unfold hammingDist at hyw; omega exact h1_ne ((hu_eq i1 hk1.symm).trans (hw_eq i1 hk1.symm).symm) by_cases h_y1 : y i1 = u i1 · right funext k by_cases hk2 : k = i2 · rw [hk2]; simp only [c2, Function.update_self] by_contra hne have hne_w1 : w i1 ≠ y i1 := by rw [h_y1]; exact h1_ne.symm have hsub : ({i1, i2} : Finset (Fin m)) ⊆ Finset.univ.filter (fun t => w t ≠ y t) := by intro t ht; simp only [Finset.mem_insert, Finset.mem_singleton] at ht rcases ht with rfl | rfl · exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, hne_w1⟩ · exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, Ne.symm hne⟩ have hc := Finset.card_le_card hsub; rw [Finset.card_pair hne12] at hc unfold hammingDist at hyw; omega · simp only [c2, Function.update_of_ne hk2] by_cases hk1 : k = i1 · rw [hk1]; exact h_y1 · exact hy_out k hk1 hk2 · left funext k by_cases hk1 : k = i1 · rw [hk1]; simp only [c1, Function.update_self] have hy2 : y i2 = u i2 := by by_contra hne2 have hsub : ({i1, i2} : Finset (Fin m)) ⊆ Finset.univ.filter (fun t => u t ≠ y t) := by intro t ht; simp only [Finset.mem_insert, Finset.mem_singleton] at ht rcases ht with rfl | rfl · exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, Ne.symm h_y1⟩ · exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, Ne.symm hne2⟩ have hc := Finset.card_le_card hsub; rw [Finset.card_pair hne12] at hc unfold hammingDist at hyu; omega by_contra hne have hne_w2 : w i2 ≠ y i2 := by rw [hy2]; exact h2_ne.symm have hsub : ({i1, i2} : Finset (Fin m)) ⊆ Finset.univ.filter (fun t => w t ≠ y t) := by intro t ht; simp only [Finset.mem_insert, Finset.mem_singleton] at ht rcases ht with rfl | rfl · exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, Ne.symm hne⟩ · exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, hne_w2⟩ have hc := Finset.card_le_card hsub; rw [Finset.card_pair hne12] at hc unfold hammingDist at hyw; omega · simp only [c1, Function.update_of_ne hk1] by_cases hk2 : k = i2 · rw [hk2] by_contra hne2 have hsub : ({i1, i2} : Finset (Fin m)) ⊆ Finset.univ.filter (fun t => u t ≠ y t) := by intro t ht; simp only [Finset.mem_insert, Finset.mem_singleton] at ht rcases ht with rfl | rfl · exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, Ne.symm h_y1⟩ · exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, Ne.symm hne2⟩ have hc := Finset.card_le_card hsub; rw [Finset.card_pair hne12] at hc unfold hammingDist at hyu; omega · exact hy_out k hk1 hk2 · rintro (rfl | rfl) · constructor · have hsub : (Finset.univ.filter fun k : Fin m => u k ≠ c1 k) ⊆ {i1} := fun k hk => by simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hk by_contra hne have hne' : k ≠ i1 := Finset.notMem_singleton.mp hne exact hk (by simp [c1, Function.update_of_ne hne']) unfold hammingDist; exact le_trans (Finset.card_le_card hsub) (by simp) · have hsub : (Finset.univ.filter fun k : Fin m => w k ≠ c1 k) ⊆ {i2} := fun k hk => by simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hk by_contra hne have hne' : k ≠ i2 := Finset.notMem_singleton.mp hne by_cases hk1 : k = i1 · rw [hk1] at hk; exact hk (by simp [c1]) · exact hk (by simp [c1, Function.update_of_ne hk1, heq_out k hk1 hne']) unfold hammingDist; exact le_trans (Finset.card_le_card hsub) (by simp) · constructor · have hsub : (Finset.univ.filter fun k : Fin m => u k ≠ c2 k) ⊆ {i2} := fun k hk => by simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hk by_contra hne have hne' : k ≠ i2 := Finset.notMem_singleton.mp hne exact hk (by simp [c2, Function.update_of_ne hne']) unfold hammingDist; exact le_trans (Finset.card_le_card hsub) (by simp) · have hsub : (Finset.univ.filter fun k : Fin m => w k ≠ c2 k) ⊆ {i1} := fun k hk => by simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hk by_contra hne have hne' : k ≠ i1 := Finset.notMem_singleton.mp hne by_cases hk2 : k = i2 · rw [hk2] at hk; exact hk (by simp [c2]) · exact hk (by simp [c2, Function.update_of_ne hk2, heq_out k hne' hk2]) unfold hammingDist; exact le_trans (Finset.card_le_card hsub) (by simp) -- Since `v ∈ H₁(u) ∩ H₁(w)`, `v = c1` or `v = c2`, AND `hammingDist c1 c2 = 2`! have hv_mem : v ∈ hammingBall1 u ∩ hammingBall1 w := Finset.mem_inter.mpr ⟨Finset.mem_filter.mpr ⟨Finset.mem_univ _, by omega⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, by rw [hammingDist_symm]; omega⟩⟩ rw [hinter] at hv_mem ⊢ have hc12_dist : hammingDist c1 c2 = 2 := by have hf : (Finset.univ.filter fun k : Fin m => c1 k ≠ c2 k) = {i1, i2} := by ext k simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert, Finset.mem_singleton] by_cases hk1 : k = i1 · rw [hk1]; simp [c1, c2, Function.update_of_ne hne12, h1_ne.symm] · by_cases hk2 : k = i2 · rw [hk2]; simp [c1, c2, Function.update_of_ne hne12.symm, h2_ne] · simp [c1, c2, hk1, hk2] unfold hammingDist; rw [hf, Finset.card_pair hne12] simp only [Finset.mem_insert, Finset.mem_singleton] at hv_mem rcases hv_mem with rfl | rfl · have h1_in : c1 ∈ hammingBall1 c1 := Finset.mem_filter.mpr ⟨Finset.mem_univ _, by rw [(hammingDist_eq_zero_iff c1 c1).mpr rfl]; omega⟩ have h2_out : c2 ∉ hammingBall1 c1 := by simp only [hammingBall1, Finset.mem_filter, Finset.mem_univ, true_and, not_le]; omega have hs : ({c1, c2} : Finset (Word q m)) \ hammingBall1 c1 = {c2} := by ext y; simp only [Finset.mem_sdiff, Finset.mem_insert, Finset.mem_singleton] constructor · rintro ⟨rfl | rfl, hy⟩ · exact absurd h1_in hy · rfl · rintro rfl; exact ⟨Or.inr rfl, h2_out⟩ rw [hs, Finset.card_singleton] · have h2_in : c2 ∈ hammingBall1 c2 := Finset.mem_filter.mpr ⟨Finset.mem_univ _, by rw [(hammingDist_eq_zero_iff c2 c2).mpr rfl]; omega⟩ have h1_out : c1 ∉ hammingBall1 c2 := by simp only [hammingBall1, Finset.mem_filter, Finset.mem_univ, true_and, not_le] rw [hammingDist_symm]; omega have hs : ({c1, c2} : Finset (Word q m)) \ hammingBall1 c2 = {c1} := by ext y; simp only [Finset.mem_sdiff, Finset.mem_insert, Finset.mem_singleton] constructor · rintro ⟨rfl | rfl, hy⟩ · rfl · exact absurd h2_in hy · rintro rfl; exact ⟨Or.inl rfl, h1_out⟩ rw [hs, Finset.card_singleton] lemma disjoint_hammingBall1_of_dist_ge_three {q m : ℕ} {u v : Word q m} (h3 : 3 ≤ hammingDist u v) : Disjoint (hammingBall1 u) (hammingBall1 v) := by rw [Finset.disjoint_left] intro y hyu hyv have h1 : hammingDist u y ≤ 1 := (Finset.mem_filter.mp hyu).2 have h2 : hammingDist v y ≤ 1 := (Finset.mem_filter.mp hyv).2 have htri := hammingDist_triangle u y v rw [hammingDist_symm y v] at htri omega /-- **EXACT SINGLE-DELETION SINGLE-SUBSTITUTION BALL CARDINALITY THEOREM**: For EVERY alphabet size `q ≥ 2`, EVERY length `n = m + 1 ≥ 3` (`m ≥ 2`), and EVERY word `x : Word q (m + 1)`: - If `runCount x = 1`: `(delSubBall x).card = (q - 1) * m + 1`, - If `runCount x ≥ 2`: `(delSubBall x).card = runCount x * ((q - 1) * (m - 1) - 1) + (q + 2)`! -/ theorem card_delSubBall {q m : ℕ} (hq : 2 ≤ q) (hm : 2 ≤ m) (x : Word q (m + 1)) : (delSubBall x).card = if runCount x = 1 then (q - 1) * m + 1 else runCount x * ((q - 1) * (m - 1) - 1) + (q + 2) := by let r := runCount x let s := (runStarts x).orderEmbOfFin (card_runStarts_eq_runCount x) let d : Fin r → Word q m := fun k => deleteAt x (s k) have hdel_eq : delSubBall x = (Finset.univ : Finset (Fin r)).biUnion (fun k => hammingBall1 (d k)) := by unfold delSubBall rw [delBall_eq_image_runStarts] ext y simp only [Finset.mem_biUnion, Finset.mem_image, Finset.mem_univ, true_and] constructor · rintro ⟨z, ⟨a, ha, rfl⟩, hy⟩ have hrange : a ∈ Set.range s := by rw [Finset.range_orderEmbOfFin]; exact ha rcases hrange with ⟨k, rfl⟩ exact ⟨k, hy⟩ · rintro ⟨k, hy⟩ exact ⟨d k, ⟨s k, Finset.orderEmbOfFin_mem _ _ k, rfl⟩, hy⟩ -- Define prefix unions `U N := ⋃_{k < N} H₁(d k)` for `1 ≤ N ≤ r`! let U (N : ℕ) : Finset (Word q m) := (Finset.univ.filter fun k : Fin r => k.val < N).biUnion fun k => hammingBall1 (d k) have hUr : U r = delSubBall x := by rw [hdel_eq] ext y simp only [U, Finset.mem_biUnion, Finset.mem_filter, Finset.mem_univ, true_and] exact ⟨fun ⟨k, _, h⟩ => ⟨k, h⟩, fun ⟨k, h⟩ => ⟨k, k.isLt, h⟩⟩ have hstep : ∀ N : ℕ, 1 ≤ N → N ≤ r → (U N).card = if N = 1 then (q - 1) * m + 1 else N * ((q - 1) * (m - 1) - 1) + (q + 2) := by intro N hN1 hNr induction N with | zero => omega | succ N ih => by_cases hN0 : N = 0 · subst hN0 simp only [zero_add, ↓reduceIte] have hU1 : U 1 = hammingBall1 (d ⟨0, by omega⟩) := by ext y simp only [U, Finset.mem_biUnion, Finset.mem_filter, Finset.mem_univ, true_and] constructor · rintro ⟨k, hk, hy⟩ have : k = ⟨0, by omega⟩ := Fin.ext (by dsimp; omega) rwa [← this] · intro hy; exact ⟨⟨0, by omega⟩, by dsimp; omega, hy⟩ rw [hU1, card_hammingBall1 (by omega)] · have hN1' : 1 ≤ N := by omega have hU_split : U (N + 1) = U N ∪ hammingBall1 (d ⟨N, by omega⟩) := by ext y simp only [U, Finset.mem_union, Finset.mem_biUnion, Finset.mem_filter, Finset.mem_univ, true_and] constructor · rintro ⟨k, hk, hy⟩ by_cases hkN : k.val < N · exact Or.inl ⟨k, hkN, hy⟩ · right have : k = ⟨N, by omega⟩ := Fin.ext (by dsimp; omega) rwa [← this] · rintro (⟨k, hk, hy⟩ | hy) · exact ⟨k, by omega, hy⟩ · exact ⟨⟨N, by omega⟩, by dsimp; omega, hy⟩ have hcard_union := Finset.card_union_add_card_inter (U N) (hammingBall1 (d ⟨N, by omega⟩)) have hcard_H := card_hammingBall1 (by omega) (d ⟨N, by omega⟩) have hih := ih hN1' (by omega) by_cases hN_eq1 : N = 1 · subst hN_eq1 rw [if_pos rfl] at hih have h21 : ¬ ((1 + 1 : ℕ) = 1) := by decide rw [if_neg h21] have hU1 : U 1 = hammingBall1 (d ⟨0, by omega⟩) := by ext y simp only [U, Finset.mem_biUnion, Finset.mem_filter, Finset.mem_univ, true_and] constructor · rintro ⟨k, hk, hy⟩ have : k = ⟨0, by omega⟩ := Fin.ext (by dsimp; omega) rwa [← this] · intro hy; exact ⟨⟨0, by omega⟩, by dsimp; omega, hy⟩ have hd01 : hammingDist (d ⟨0, by omega⟩) (d ⟨1, by omega⟩) = 1 := by have h := hammingDist_deleteAt_orderEmb x ⟨0, by omega⟩ ⟨1, by omega⟩ (by dsimp; omega) dsimp at h; exact h have hinter := card_inter_hammingBall1_dist_one hd01 rw [hU_split, hU1] rw [hU1, hinter] at hcard_union rw [hU1] at hih rw [hih, hcard_H] at hcard_union have hqm : 1 ≤ (q - 1) * (m - 1) := by have : 1 ≤ q - 1 := by omega have : 1 ≤ m - 1 := by omega nlinarith have hmul_sub : (q - 1) * m = (q - 1) * (m - 1) + (q - 1) := by have h1 : m = (m - 1) + 1 := by omega conv_lhs => rw [h1, mul_add, mul_one] have htwo : (1 + 1) * ((q - 1) * (m - 1) - 1) = 2 * ((q - 1) * (m - 1) - 1) := by ring omega · -- `N ≥ 2`: `U N ∩ H₁(d N) = (H₁(d (N-1)) ∩ H₁(d N)) ∪ ((H₁(d (N-2)) ∩ H₁(d N)) \ H₁(d (N-1)))`! have hN2 : 2 ≤ N := by omega rw [if_neg hN_eq1] at hih rw [if_neg (by omega : N + 1 ≠ 1)] let k0 : Fin r := ⟨N - 2, by omega⟩ let k1 : Fin r := ⟨N - 1, by omega⟩ let k2 : Fin r := ⟨N, by omega⟩ have hd12 : hammingDist (d k1) (d k2) = 1 := by have h := hammingDist_deleteAt_orderEmb x k1 k2 (by dsimp [k1, k2]; omega) dsimp only [d]; rw [h]; dsimp only [k1, k2]; omega have hd01 : hammingDist (d k0) (d k1) = 1 := by have h := hammingDist_deleteAt_orderEmb x k0 k1 (by dsimp [k0, k1]; omega) dsimp only [d]; rw [h]; dsimp only [k0, k1]; omega have hd02 : hammingDist (d k0) (d k2) = 2 := by have h := hammingDist_deleteAt_orderEmb x k0 k2 (by dsimp [k0, k2]; omega) dsimp only [d]; rw [h]; dsimp only [k0, k2]; omega have hinter_eq : U N ∩ hammingBall1 (d k2) = (hammingBall1 (d k1) ∩ hammingBall1 (d k2)) ∪ ((hammingBall1 (d k0) ∩ hammingBall1 (d k2)) \ hammingBall1 (d k1)) := by ext y simp only [Finset.mem_inter, Finset.mem_union, Finset.mem_sdiff, U, Finset.mem_biUnion, Finset.mem_filter, Finset.mem_univ, true_and] constructor · rintro ⟨⟨j, hjN, hyj⟩, hy2⟩ by_cases hj1 : j = k1 · subst hj1; exact Or.inl ⟨hyj, hy2⟩ · by_cases hj0 : j = k0 · subst hj0 by_cases hy1 : y ∈ hammingBall1 (d k1) · exact Or.inl ⟨hy1, hy2⟩ · exact Or.inr ⟨⟨hyj, hy2⟩, hy1⟩ · have hj_lt : j.val + 3 ≤ N := by have hne1 : j.val ≠ N - 1 := fun h => hj1 (Fin.ext h) have hne0 : j.val ≠ N - 2 := fun h => hj0 (Fin.ext h) omega have hd3 : 3 ≤ hammingDist (d j) (d k2) := by have h := hammingDist_deleteAt_orderEmb x j k2 (by dsimp [k2]; omega) dsimp only [d]; rw [h]; dsimp only [k2]; omega exact absurd hy2 (Finset.disjoint_left.mp (disjoint_hammingBall1_of_dist_ge_three hd3) hyj) · rintro (⟨hy1, hy2⟩ | ⟨⟨hy0, hy2⟩, _⟩) · exact ⟨⟨k1, by dsimp [k1]; omega, hy1⟩, hy2⟩ · exact ⟨⟨k0, by dsimp [k0]; omega, hy0⟩, hy2⟩ have hdisj_int : Disjoint (hammingBall1 (d k1) ∩ hammingBall1 (d k2)) ((hammingBall1 (d k0) ∩ hammingBall1 (d k2)) \ hammingBall1 (d k1)) := Finset.disjoint_left.mpr fun y h1 h2 => (Finset.mem_sdiff.mp h2).2 (Finset.mem_inter.mp h1).1 have hcard_inter : (U N ∩ hammingBall1 (d k2)).card = q + 1 := by rw [hinter_eq, Finset.card_union_of_disjoint hdisj_int, card_inter_hammingBall1_dist_one hd12, card_inter_hammingBall1_dist_two_sdiff_mid hd02 hd01 hd12] rw [hU_split] rw [hih, hcard_H, hcard_inter] at hcard_union have hqm : 1 ≤ (q - 1) * (m - 1) := by have : 1 ≤ q - 1 := by omega have : 1 ≤ m - 1 := by omega nlinarith have hmul_sub : (q - 1) * m = (q - 1) * (m - 1) + (q - 1) := by have h1 : m = (m - 1) + 1 := by omega conv_lhs => rw [h1, mul_add, mul_one] have hstep_mul : (N + 1) * ((q - 1) * (m - 1) - 1) = N * ((q - 1) * (m - 1) - 1) + ((q - 1) * (m - 1) - 1) := by ring omega rw [← hUr] exact hstep r (one_le_runCount x) le_rfl end ExactBallCardinality /-! ## Section 3: Run-Count Shift Bound & Universal Shifted Dual Weight Feasibility -/ section DualWeight /-- Deleting a coordinate never increases the run count: `runCount (deleteAt x a) ≤ runCount x`. -/ lemma runCount_deleteAt_le {q m : ℕ} (x : Word q (m + 2)) (a : Fin (m + 2)) : runCount (deleteAt x a) ≤ runCount x := by unfold runCount suffices h : (boundarySet (deleteAt x a)).card ≤ (boundarySet x).card by omega -- Map each boundary `j ∈ boundarySet (deleteAt x a)` to an adjacent pair in `x`: -- if `j < a` and `j + 1 < a`: `j.castSucc ∈ boundarySet x`, -- if `j ≥ a`: `j.succ ∈ boundarySet x`, -- if `j + 1 = a`: `x (a - 1) ≠ x (a + 1)` so at least one of `a - 1` or `a` is in `boundarySet x`! -- Even simpler: compare via `delBall`? No, map directly using `f : Fin m → Fin (m + 1)`: -- for `j.val + 1 < a.val`: `j.castSucc`, -- for `a.val ≤ j.val`: `j.succ`, -- for `j.val + 1 = a.val`: if `x j.castSucc.castSucc ≠ x a` then `j.castSucc` else `j.succ`! -- Note `f` is injective on `Fin m` because: -- - `j.val + 1 < a.val` has `f j = j.val < a.val - 1`, -- - `j.val + 1 = a.val` has `f j ∈ {a.val - 1, a.val}`, -- - `a.val ≤ j.val` has `f j = j.val + 1 ≥ a.val + 1`!! let f : Fin m → Fin (m + 1) := fun j => if j.val + 1 < a.val then j.castSucc else if a.val ≤ j.val then j.succ else if x j.castSucc.castSucc ≠ x a then j.castSucc else j.succ have hf_inj : Function.Injective f := by intro j1 j2 heq have hval := congrArg Fin.val heq dsimp only [f] at hval split_ifs at hval <;> (simp only [Fin.val_castSucc, Fin.val_succ] at hval; exact Fin.ext (by omega)) have hf_mem : ∀ j ∈ boundarySet (deleteAt x a), f j ∈ boundarySet x := by intro j hj have hj' : deleteAt x a j.castSucc ≠ deleteAt x a j.succ := (Finset.mem_filter.mp hj).2 unfold deleteAt at hj' refine Finset.mem_filter.mpr ⟨Finset.mem_univ _, ?_⟩ dsimp only [f] by_cases h1 : j.val + 1 < a.val · rw [if_pos h1] have ha1 : a.succAbove j.castSucc = j.castSucc.castSucc := Fin.succAbove_of_castSucc_lt a j.castSucc (Fin.lt_def.mpr (by simp; omega)) have ha2 : a.succAbove j.succ = j.succ.castSucc := Fin.succAbove_of_castSucc_lt a j.succ (Fin.lt_def.mpr (by simp; omega)) rw [ha1, ha2] at hj'; exact hj' · rw [if_neg h1] by_cases h2 : a.val ≤ j.val · rw [if_pos h2] have ha1 : a.succAbove j.castSucc = j.castSucc.succ := Fin.succAbove_of_le_castSucc a j.castSucc (Fin.le_def.mpr (by simp; omega)) have ha2 : a.succAbove j.succ = j.succ.succ := Fin.succAbove_of_le_castSucc a j.succ (Fin.le_def.mpr (by simp; omega)) rw [ha1, ha2] at hj'; exact hj' · rw [if_neg h2] have hja : j.val + 1 = a.val := by omega have ha1 : a.succAbove j.castSucc = j.castSucc.castSucc := Fin.succAbove_of_castSucc_lt a j.castSucc (Fin.lt_def.mpr (by simp; omega)) have ha2 : a.succAbove j.succ = j.succ.succ := Fin.succAbove_of_le_castSucc a j.succ (Fin.le_def.mpr (by simp; omega)) rw [ha1, ha2] at hj' have hjs_eq : j.castSucc.succ = a := Fin.ext (by simp; omega) have hjs_eq2 : j.succ.castSucc = a := Fin.ext (by simp; omega) by_cases h3 : x j.castSucc.castSucc ≠ x a · rw [if_pos h3, hjs_eq]; exact h3 · rw [if_neg h3, hjs_eq2] push Not at h3 rw [← h3]; exact hj' have hsub : (boundarySet (deleteAt x a)).image f ⊆ boundarySet x := fun i hi => by rcases Finset.mem_image.mp hi with ⟨j, hj, rfl⟩ exact hf_mem j hj have hc := Finset.card_le_card hsub rwa [Finset.card_image_of_injective _ hf_inj] at hc /-- Substituting at most one coordinate increases the run count by at most `2`: if `hammingDist z y ≤ 1`, then `runCount y ≤ runCount z + 2`! -/ lemma runCount_le_add_two_of_hammingDist_le_one {q m : ℕ} {z y : Word q (m + 1)} (hd : hammingDist z y ≤ 1) : runCount y ≤ runCount z + 2 := by by_cases h0 : hammingDist z y = 0 · rw [(hammingDist_eq_zero_iff z y).mp h0]; omega · have h1 : hammingDist z y = 1 := by omega obtain ⟨i0, hi0⟩ := Finset.card_eq_one.mp h1 have heq_other : ∀ k : Fin (m + 1), k ≠ i0 → z k = y k := fun k hk => by have : k ∉ Finset.univ.filter (fun i => z i ≠ y i) := by rw [hi0]; exact Finset.notMem_singleton.mpr hk simpa using this unfold runCount suffices h : (boundarySet y).card ≤ (boundarySet z).card + 2 by omega -- Every `j ∈ boundarySet y \ boundarySet z` must have `j.castSucc = i0` or `j.succ = i0`, -- giving at most 2 extra boundaries! let extra : Finset (Fin m) := Finset.univ.filter fun j : Fin m => j.castSucc = i0 ∨ j.succ = i0 have hsub : boundarySet y ⊆ boundarySet z ∪ extra := by intro j hj simp only [Finset.mem_union, boundarySet, Finset.mem_filter, Finset.mem_univ, true_and, extra] at hj ⊢ by_cases he : j.castSucc = i0 ∨ j.succ = i0 · exact Or.inr he · left push Not at he rwa [heq_other j.castSucc he.1, heq_other j.succ he.2] have hextra_le : extra.card ≤ 2 := by have hpart : extra = (Finset.univ.filter fun j : Fin m => j.castSucc = i0) ∪ (Finset.univ.filter fun j : Fin m => j.succ = i0) := by ext j; simp [extra] have hc1 : (Finset.univ.filter fun j : Fin m => j.castSucc = i0).card ≤ 1 := Finset.card_le_one.mpr fun j1 h1 j2 h2 => Fin.castSucc_injective m ((Finset.mem_filter.mp h1).2.trans (Finset.mem_filter.mp h2).2.symm) have hc2 : (Finset.univ.filter fun j : Fin m => j.succ = i0).card ≤ 1 := Finset.card_le_one.mpr fun j1 h1 j2 h2 => Fin.succ_injective m ((Finset.mem_filter.mp h1).2.trans (Finset.mem_filter.mp h2).2.symm) rw [hpart] exact le_trans (Finset.card_union_le _ _) (by omega) exact le_trans (Finset.card_le_card hsub) (le_trans (Finset.card_union_le _ _) (by omega)) /-- **Run-Count Shift Bound**: Every word `y ∈ delSubBall x` satisfies `runCount y ≤ runCount x + 2`! -/ theorem runCount_le_add_two_of_mem_delSubBall {q m : ℕ} {x : Word q (m + 2)} {y : Word q (m + 1)} (hy : y ∈ delSubBall x) : runCount y ≤ runCount x + 2 := by unfold delSubBall delBall at hy rcases Finset.mem_biUnion.mp hy with ⟨z, hz, hyz⟩ rcases Finset.mem_image.mp hz with ⟨a, _, rfl⟩ have h1 := runCount_deleteAt_le x a have h2 := runCount_le_add_two_of_hammingDist_le_one (Finset.mem_filter.mp hyz).2 omega /-- **The Shifted Piecewise Hypergraph LP Dual Weight** for `q`-ary single-deletion single-substitution codes of length `n = m + 2` (`m ≥ 2`, `y : Word q (m + 1)`): - `1 / ((q - 1) * (m + 1) + 1)` if `runCount y ≤ 3`, - `1 / (((q - 1) * m - 1) * (runCount y - 2) + (q + 2))` if `runCount y ≥ 4`! -/ noncomputable def dsDualWeight {q m : ℕ} (y : Word q (m + 1)) : ℝ := if runCount y ≤ 3 then 1 / (((q - 1) * (m + 1) + 1 : ℕ) : ℝ) else 1 / ((((q - 1) * m - 1) * (runCount y - 2) + (q + 2) : ℕ) : ℝ) lemma dsDualWeight_nonneg {q m : ℕ} (y : Word q (m + 1)) : 0 ≤ dsDualWeight y := by unfold dsDualWeight split_ifs <;> positivity /-- **UNIVERSAL DUAL FEASIBILITY THEOREM**: For EVERY alphabet size `q ≥ 2`, EVERY block length `n = m + 2 ≥ 4` (`m ≥ 2`), and EVERY word `x : Word q (m + 2)`: `1 ≤ ∑ y ∈ delSubBall x, dsDualWeight y`! -/ theorem dsDualWeight_feasible {q m : ℕ} (hq : 2 ≤ q) (hm : 2 ≤ m) (x : Word q (m + 2)) : 1 ≤ ∑ y ∈ delSubBall x, dsDualWeight y := by have hcard := card_delSubBall (q := q) (m := m + 1) hq (by omega) x simp only [Nat.add_sub_cancel] at hcard by_cases hr1 : runCount x = 1 · rw [if_pos hr1] at hcard have hpos : (0 : ℝ) < (((q - 1) * (m + 1) + 1 : ℕ) : ℝ) := by positivity have hpt : ∀ y ∈ delSubBall x, dsDualWeight y = 1 / (((q - 1) * (m + 1) + 1 : ℕ) : ℝ) := by intro y hy have hry := runCount_le_add_two_of_mem_delSubBall hy unfold dsDualWeight rw [if_pos (by omega)] rw [Finset.sum_congr rfl hpt, Finset.sum_const, nsmul_eq_mul, hcard] field_simp [hpos.ne'] exact le_rfl · rw [if_neg hr1] at hcard have hr2 : 2 ≤ runCount x := by have := one_le_runCount x; omega let D : ℕ := runCount x * ((q - 1) * m - 1) + (q + 2) have hD_pos : (0 : ℝ) < (D : ℝ) := by dsimp [D]; positivity have hpt : ∀ y ∈ delSubBall x, 1 / (D : ℝ) ≤ dsDualWeight y := by intro y hy have hry := runCount_le_add_two_of_mem_delSubBall hy unfold dsDualWeight by_cases hle3 : runCount y ≤ 3 · rw [if_pos hle3] have hle_nat : (q - 1) * (m + 1) + 1 ≤ D := by dsimp only [D] have hqm : 2 ≤ (q - 1) * m := by have : 1 ≤ q - 1 := by omega nlinarith have h1 : 2 * ((q - 1) * m - 1) ≤ runCount x * ((q - 1) * m - 1) := Nat.mul_le_mul_right _ hr2 have h2 : (q - 1) * (m + 1) = (q - 1) * m + (q - 1) := by ring omega have hleR : (((q - 1) * (m + 1) + 1 : ℕ) : ℝ) ≤ (D : ℝ) := by exact_mod_cast hle_nat exact one_div_le_one_div_of_le (by positivity) hleR · rw [if_neg hle3] have hle_nat : ((q - 1) * m - 1) * (runCount y - 2) + (q + 2) ≤ D := by dsimp only [D] have h1 : runCount y - 2 ≤ runCount x := by omega have h2 : ((q - 1) * m - 1) * (runCount y - 2) ≤ ((q - 1) * m - 1) * runCount x := Nat.mul_le_mul_left _ h1 rw [mul_comm (runCount x)] omega have hleR : ((((q - 1) * m - 1) * (runCount y - 2) + (q + 2) : ℕ) : ℝ) ≤ (D : ℝ) := by exact_mod_cast hle_nat exact one_div_le_one_div_of_le (by positivity) hleR calc (1 : ℝ) = (D : ℝ) * (1 / (D : ℝ)) := by field_simp [hD_pos.ne'] _ = ((delSubBall x).card : ℝ) * (1 / (D : ℝ)) := by rw [hcard] _ = ∑ _y ∈ delSubBall x, (1 / (D : ℝ)) := by rw [Finset.sum_const, nsmul_eq_mul] _ ≤ ∑ y ∈ delSubBall x, dsDualWeight y := Finset.sum_le_sum hpt /-- **COROLLARY**: Every `q`-ary single-deletion single-substitution code `C ⊆ Word q (m + 2)` (`q ≥ 2`, `m ≥ 2`) satisfies `(C.card : ℝ) ≤ ∑ y : Word q (m + 1), dsDualWeight y`! -/ theorem delSubCode_card_le_dsDualWeight_sum {q m : ℕ} (hq : 2 ≤ q) (hm : 2 ≤ m) (C : Finset (Word q (m + 2))) (hC : IsDelSubCode C) : (C.card : ℝ) ≤ ∑ y : Word q (m + 1), dsDualWeight y := delSubCode_card_le_dual_sum C hC dsDualWeight dsDualWeight_nonneg (dsDualWeight_feasible hq hm) end DualWeight /-! ## Section 4: Exact Run-Count Enumeration & Rising-Factorial Closed-Form Upper Bound -/ section ClosedFormUpperBound /-- Exact count of `q`-ary words `y : Word q (m + 1)` with fixed boundary set `B : Finset (Fin m)`: `#{y | boundarySet y = B} = q * (q - 1) ^ B.card`. -/ theorem card_words_with_boundarySet {q m : ℕ} (_hq : 1 ≤ q) (B : Finset (Fin m)) : (Finset.univ.filter fun y : Word q (m + 1) => boundarySet y = B).card = q * (q - 1) ^ B.card := by induction m with | zero => have hB : B = ∅ := Finset.eq_empty_of_isEmpty B subst hB have hf : (Finset.univ.filter fun y : Word q 1 => boundarySet y = ∅) = Finset.univ := by ext y; simp [boundarySet] rw [hf, Finset.card_univ, Fintype.card_fun, Fintype.card_fin, Fintype.card_fin, Finset.card_empty, pow_zero, pow_one, mul_one] | succ m ih => let B' : Finset (Fin m) := B.preimage Fin.castSucc (Fin.castSucc_injective m).injOn have hpre_mem : ∀ i : Fin m, i ∈ B' ↔ i.castSucc ∈ B := fun i => Finset.mem_preimage have hcard_B : B.card = B'.card + (if Fin.last m ∈ B then 1 else 0) := by have himg : B'.map ⟨Fin.castSucc, Fin.castSucc_injective m⟩ = B.erase (Fin.last m) := by ext j; simp only [Finset.mem_map, Finset.mem_erase, Function.Embedding.coeFn_mk] constructor · rintro ⟨i, hi, rfl⟩; exact ⟨Fin.castSucc_ne_last i, (hpre_mem i).mp hi⟩ · rintro ⟨hjl, hjB⟩ obtain ⟨i, rfl⟩ := Fin.exists_castSucc_eq.mpr hjl exact ⟨i, (hpre_mem i).mpr hjB, rfl⟩ have h := congrArg Finset.card himg rw [Finset.card_map] at h by_cases hl : Fin.last m ∈ B · rw [if_pos hl]; rw [Finset.card_erase_of_mem hl] at h have hpos : 1 ≤ B.card := Finset.card_pos.mpr ⟨Fin.last m, hl⟩; omega · rw [if_neg hl]; rw [Finset.erase_eq_of_notMem hl] at h; omega let splitE : Word q (m + 2) ≃ Word q (m + 1) × Fin q := { toFun := fun y => (fun i => y i.castSucc, y (Fin.last (m + 1))) invFun := fun p => Fin.snoc (α := fun _ => Fin q) p.1 p.2 left_inv := fun y => Fin.snoc_init_self y right_inv := fun ⟨w, c⟩ => by ext i · simp [Fin.snoc_castSucc] · simp [Fin.snoc_last] } have hbdry_snoc : ∀ (w : Word q (m + 1)) (c : Fin q), boundarySet (Fin.snoc (α := fun _ => Fin q) w c) = B ↔ boundarySet w = B' ∧ (w (Fin.last m) ≠ c ↔ Fin.last m ∈ B) := by intro w c constructor · intro h refine ⟨?_, ?_⟩ · ext i rw [hpre_mem i, ← h]; unfold boundarySet simp only [Finset.mem_filter, Finset.mem_univ, true_and] have h1 : (Fin.snoc (α := fun _ => Fin q) w c) i.castSucc.castSucc = w i.castSucc := by rw [Fin.snoc_castSucc] have h2 : (Fin.snoc (α := fun _ => Fin q) w c) i.castSucc.succ = w i.succ := by have : i.castSucc.succ = i.succ.castSucc := rfl rw [this, Fin.snoc_castSucc] rw [h1, h2] · rw [← h]; unfold boundarySet simp only [Finset.mem_filter, Finset.mem_univ, true_and] have h1 : (Fin.snoc (α := fun _ => Fin q) w c) (Fin.last m).castSucc = w (Fin.last m) := by rw [Fin.snoc_castSucc] have h2 : (Fin.snoc (α := fun _ => Fin q) w c) (Fin.last m).succ = c := by have : (Fin.last m).succ = Fin.last (m + 1) := rfl rw [this, Fin.snoc_last] rw [h1, h2] · rintro ⟨hw, hc⟩ ext j by_cases hjl : j = Fin.last m · subst hjl unfold boundarySet; simp only [Finset.mem_filter, Finset.mem_univ, true_and] have h1 : (Fin.snoc (α := fun _ => Fin q) w c) (Fin.last m).castSucc = w (Fin.last m) := by rw [Fin.snoc_castSucc] have h2 : (Fin.snoc (α := fun _ => Fin q) w c) (Fin.last m).succ = c := by have : (Fin.last m).succ = Fin.last (m + 1) := rfl rw [this, Fin.snoc_last] rw [h1, h2]; exact hc · obtain ⟨i, rfl⟩ := Fin.exists_castSucc_eq.mpr hjl rw [← hpre_mem i, ← hw]; unfold boundarySet simp only [Finset.mem_filter, Finset.mem_univ, true_and] have h1 : (Fin.snoc (α := fun _ => Fin q) w c) i.castSucc.castSucc = w i.castSucc := by rw [Fin.snoc_castSucc] have h2 : (Fin.snoc (α := fun _ => Fin q) w c) i.castSucc.succ = w i.succ := by have : i.castSucc.succ = i.succ.castSucc := rfl rw [this, Fin.snoc_castSucc] rw [h1, h2] have hcard_eq : (Finset.univ.filter fun y : Word q (m + 2) => boundarySet y = B).card = (Finset.univ.filter fun p : Word q (m + 1) × Fin q => boundarySet p.1 = B' ∧ (p.1 (Fin.last m) ≠ p.2 ↔ Fin.last m ∈ B)).card := by refine Finset.card_bij (fun y _ => splitE y) ?_ ?_ ?_ · intro y hy have hy' := (Finset.mem_filter.mp hy).2 refine Finset.mem_filter.mpr ⟨Finset.mem_univ _, ?_⟩ have hinv : Fin.snoc (α := fun _ => Fin q) (splitE y).1 (splitE y).2 = y := splitE.left_inv y exact (hbdry_snoc (splitE y).1 (splitE y).2).mp (hinv.symm ▸ hy') · intro y1 _ y2 _ h; exact splitE.injective h · intro p hp have hp' := (Finset.mem_filter.mp hp).2 refine ⟨splitE.symm p, Finset.mem_filter.mpr ⟨Finset.mem_univ _, (hbdry_snoc p.1 p.2).mpr hp'⟩, splitE.apply_symm_apply p⟩ rw [hcard_eq] have hsum : (Finset.univ.filter fun p : Word q (m + 1) × Fin q => boundarySet p.1 = B' ∧ (p.1 (Fin.last m) ≠ p.2 ↔ Fin.last m ∈ B)).card = ∑ w ∈ Finset.univ.filter (fun w : Word q (m + 1) => boundarySet w = B'), (Finset.univ.filter fun c : Fin q => w (Fin.last m) ≠ c ↔ Fin.last m ∈ B).card := by rw [Finset.card_eq_sum_ones, Finset.sum_filter] rw [show (Finset.univ : Finset (Word q (m + 1) × Fin q)) = Finset.univ ×ˢ Finset.univ from rfl, Finset.sum_product] dsimp only have hsplit_if : ∀ w : Word q (m + 1), (∑ c : Fin q, if boundarySet w = B' ∧ (w (Fin.last m) ≠ c ↔ Fin.last m ∈ B) then 1 else 0) = if boundarySet w = B' then (Finset.univ.filter fun c : Fin q => w (Fin.last m) ≠ c ↔ Fin.last m ∈ B).card else 0 := fun w => by by_cases hw : boundarySet w = B' · rw [if_pos hw]; simp [hw] · rw [if_neg hw]; simp [hw] simp_rw [hsplit_if, ← Finset.sum_filter] rw [hsum, hcard_B] by_cases hl : Fin.last m ∈ B · have hinner : ∀ w : Word q (m + 1), (Finset.univ.filter fun c : Fin q => w (Fin.last m) ≠ c ↔ Fin.last m ∈ B).card = q - 1 := fun w => by have hf : (Finset.univ.filter fun c : Fin q => w (Fin.last m) ≠ c ↔ Fin.last m ∈ B) = Finset.univ.erase (w (Fin.last m)) := by ext c; simp [hl, ne_comm] rw [hf, Finset.card_erase_of_mem (Finset.mem_univ _), Finset.card_univ, Fintype.card_fin] simp_rw [hinner, Finset.sum_const, smul_eq_mul, ih B', if_pos hl, pow_succ] ring · have hinner : ∀ w : Word q (m + 1), (Finset.univ.filter fun c : Fin q => w (Fin.last m) ≠ c ↔ Fin.last m ∈ B).card = 1 := fun w => by have hf : (Finset.univ.filter fun c : Fin q => w (Fin.last m) ≠ c ↔ Fin.last m ∈ B) = {w (Fin.last m)} := by ext c; simp [hl, eq_comm] rw [hf, Finset.card_singleton] simp_rw [hinner, Finset.sum_const, smul_eq_mul, ih B', if_neg hl, add_zero, mul_one] /-- Exact count of `q`-ary words `y : Word q (m + 1)` with `runCount y = r`: `#{y | runCount y = r} = q * (q - 1) ^ (r - 1) * Nat.choose m (r - 1)`. -/ theorem card_words_with_runCount {q m r : ℕ} (hq : 1 ≤ q) (hr : 1 ≤ r) : (Finset.univ.filter fun y : Word q (m + 1) => runCount y = r).card = q * (q - 1) ^ (r - 1) * Nat.choose m (r - 1) := by let P := (Finset.univ : Finset (Fin m)).powersetCard (r - 1) have hpart : (Finset.univ.filter fun y : Word q (m + 1) => runCount y = r) = P.biUnion fun B => Finset.univ.filter fun y : Word q (m + 1) => boundarySet y = B := by ext y simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_biUnion, P, Finset.mem_powersetCard, Finset.subset_univ] constructor · intro hrc; exact ⟨boundarySet y, by unfold runCount at hrc; omega, rfl⟩ · rintro ⟨B, hcard, rfl⟩; unfold runCount; omega have hdisj : Set.PairwiseDisjoint (P : Set (Finset (Fin m))) (fun B => Finset.univ.filter fun y : Word q (m + 1) => boundarySet y = B) := fun B1 _ B2 _ hne => Finset.disjoint_left.mpr fun y h1 h2 => hne ((Finset.mem_filter.mp h1).2.symm.trans (Finset.mem_filter.mp h2).2) rw [hpart, Finset.card_biUnion hdisj] have hterm : ∀ B ∈ P, (Finset.univ.filter fun y : Word q (m + 1) => boundarySet y = B).card = q * (q - 1) ^ (r - 1) := by intro B hB rw [card_words_with_boundarySet hq B, (Finset.mem_powersetCard.mp hB).2] rw [Finset.sum_congr rfl hterm, Finset.sum_const, smul_eq_mul, Finset.card_powersetCard, Finset.card_univ, Fintype.card_fin, mul_comm] /-- Key partial-fraction upper bound for `r ≥ 4`: `1 / (r - 2) ≤ 1 / r + 2 / (r * (r + 1)) + 18 / (r * (r + 1) * (r + 2))` (sharp equality at `r = 4`!). -/ lemma inv_sub_two_le {r : ℝ} (hr : 4 ≤ r) : 1 / (r - 2) ≤ 1 / r + 2 / (r * (r + 1)) + 18 / (r * (r + 1) * (r + 2)) := by have hr0 : 0 < r := by linarith have hrm2 : 0 < r - 2 := by linarith have hr1 : 0 < r + 1 := by linarith have hr2 : 0 < r + 2 := by linarith have hden : 0 < (r - 2) * r * (r + 1) * (r + 2) := by positivity have hnum : (r - 2) * r * (r + 1) * (r + 2) * ((1 / r + 2 / (r * (r + 1)) + 18 / (r * (r + 1) * (r + 2))) - 1 / (r - 2)) = 12 * (r - 4) := by field_simp [hr0.ne', hrm2.ne', hr1.ne', hr2.ne']; ring have hpos : 0 ≤ (r - 2) * r * (r + 1) * (r + 2) * ((1 / r + 2 / (r * (r + 1)) + 18 / (r * (r + 1) * (r + 2))) - 1 / (r - 2)) := by rw [hnum]; linarith have hdiff := nonneg_of_mul_nonneg_right hpos hden linarith /-- Rising-factorial binomial sum upper bound #0: `∑_{r ∈ Ico 4 n} p^(r-1) * C(n-2, r-1) / r ≤ (p + 1)^(n - 1) / (p * (n - 1))`. -/ lemma sum_binom_div_r_le {p : ℝ} (hp : 0 < p) {n : ℕ} (hn : 4 ≤ n) : ∑ r ∈ Finset.Ico 4 n, p ^ (r - 1) * (Nat.choose (n - 2) (r - 1) : ℝ) / (r : ℝ) ≤ (p + 1) ^ (n - 1) / (p * ((n - 1 : ℕ) : ℝ)) := by have hn1 : ((n - 1 : ℕ) : ℝ) ≠ 0 := by exact_mod_cast (show n - 1 ≠ 0 by omega) have hterm : ∀ r ∈ Finset.Ico 4 n, p ^ (r - 1) * (Nat.choose (n - 2) (r - 1) : ℝ) / (r : ℝ) = (p ^ r * (Nat.choose (n - 1) r : ℝ)) / (p * ((n - 1 : ℕ) : ℝ)) := by intro r hr have hr4 : 4 ≤ r := (Finset.mem_Ico.mp hr).1 have hr0 : (r : ℝ) ≠ 0 := by exact_mod_cast (show r ≠ 0 by omega) have habs : (n - 1) * Nat.choose (n - 2) (r - 1) = r * Nat.choose (n - 1) r := by have := Nat.add_one_mul_choose_eq (n - 2) (r - 1) have h1 : n - 2 + 1 = n - 1 := by omega have h2 : r - 1 + 1 = r := by omega rw [h1, h2] at this; linarith have habsR : ((n - 1 : ℕ) : ℝ) * (Nat.choose (n - 2) (r - 1) : ℝ) = (r : ℝ) * (Nat.choose (n - 1) r : ℝ) := by exact_mod_cast habs have hpow : p ^ r = p ^ (r - 1) * p := by rw [← pow_succ]; congr 1; omega rw [hpow]; field_simp [hp.ne', hn1, hr0]; nlinarith rw [Finset.sum_congr rfl hterm, ← Finset.sum_div] refine div_le_div_of_nonneg_right ?_ (by positivity) have hbin := add_pow p 1 (n - 1) simp only [one_pow, mul_one] at hbin rw [hbin] refine Finset.sum_le_sum_of_subset_of_nonneg ?_ (fun _ _ _ => by positivity) intro r hr; simp only [Finset.mem_Ico] at hr; simp only [Finset.mem_range]; omega /-- Rising-factorial binomial sum upper bound #1: `∑_{r ∈ Ico 4 n} p^(r-1) * C(n-2, r-1) / (r*(r+1)) ≤ (p + 1)^n / (p^2 * (n-1) * n)`. -/ lemma sum_binom_div_r_r1_le {p : ℝ} (hp : 0 < p) {n : ℕ} (hn : 4 ≤ n) : ∑ r ∈ Finset.Ico 4 n, p ^ (r - 1) * (Nat.choose (n - 2) (r - 1) : ℝ) / ((r : ℝ) * ((r : ℝ) + 1)) ≤ (p + 1) ^ n / (p ^ 2 * ((n - 1 : ℕ) : ℝ) * (n : ℝ)) := by have hn1 : ((n - 1 : ℕ) : ℝ) ≠ 0 := by exact_mod_cast (show n - 1 ≠ 0 by omega) have hn0 : (n : ℝ) ≠ 0 := by exact_mod_cast (show n ≠ 0 by omega) have hterm : ∀ r ∈ Finset.Ico 4 n, p ^ (r - 1) * (Nat.choose (n - 2) (r - 1) : ℝ) / ((r : ℝ) * ((r : ℝ) + 1)) = (p ^ (r + 1) * (Nat.choose n (r + 1) : ℝ)) / (p ^ 2 * ((n - 1 : ℕ) : ℝ) * (n : ℝ)) := by intro r hr have hr4 : 4 ≤ r := (Finset.mem_Ico.mp hr).1 have hr0 : (r : ℝ) ≠ 0 := by exact_mod_cast (show r ≠ 0 by omega) have hr1 : (r : ℝ) + 1 ≠ 0 := by positivity have habs1 : (n - 1) * Nat.choose (n - 2) (r - 1) = r * Nat.choose (n - 1) r := by have := Nat.add_one_mul_choose_eq (n - 2) (r - 1) have h1 : n - 2 + 1 = n - 1 := by omega have h2 : r - 1 + 1 = r := by omega rw [h1, h2] at this; linarith have habs2 : n * Nat.choose (n - 1) r = (r + 1) * Nat.choose n (r + 1) := by have := Nat.add_one_mul_choose_eq (n - 1) r have h1 : n - 1 + 1 = n := by omega rw [h1] at this; linarith have habsR : ((n - 1 : ℕ) : ℝ) * (n : ℝ) * (Nat.choose (n - 2) (r - 1) : ℝ) = (r : ℝ) * ((r : ℝ) + 1) * (Nat.choose n (r + 1) : ℝ) := by have h1 : ((n - 1 : ℕ) : ℝ) * (Nat.choose (n - 2) (r - 1) : ℝ) = (r : ℝ) * (Nat.choose (n - 1) r : ℝ) := by exact_mod_cast habs1 have h2 : (n : ℝ) * (Nat.choose (n - 1) r : ℝ) = ((r + 1 : ℕ) : ℝ) * (Nat.choose n (r + 1) : ℝ) := by exact_mod_cast habs2 push_cast at h2; nlinarith have hpow : p ^ (r + 1) = p ^ (r - 1) * p ^ 2 := by rw [← pow_add]; congr 1; omega rw [hpow]; field_simp [hp.ne', hn1, hn0, hr0, hr1]; nlinarith rw [Finset.sum_congr rfl hterm, ← Finset.sum_div] refine div_le_div_of_nonneg_right ?_ (by positivity) have hshift : ∑ r ∈ Finset.Ico 4 n, p ^ (r + 1) * (Nat.choose n (r + 1) : ℝ) = ∑ k ∈ Finset.Ico 5 (n + 1), p ^ k * (Nat.choose n k : ℝ) := by refine Finset.sum_bij (fun r _ => r + 1) (fun r hr => by simp only [Finset.mem_Ico] at hr ⊢; omega) (fun _ _ _ _ h => by omega) (fun k hk => ⟨k - 1, by simp only [Finset.mem_Ico] at hk ⊢; omega, by simp only [Finset.mem_Ico] at hk; omega⟩) (fun _ _ => rfl) rw [hshift] have hbin := add_pow p 1 n simp only [one_pow, mul_one] at hbin rw [hbin] refine Finset.sum_le_sum_of_subset_of_nonneg ?_ (fun _ _ _ => by positivity) intro k hk; simp only [Finset.mem_Ico] at hk; simp only [Finset.mem_range]; omega /-- Rising-factorial binomial sum upper bound #2: `∑_{r ∈ Ico 4 n} p^(r-1) * C(n-2, r-1) / (r*(r+1)*(r+2)) ≤ (p + 1)^(n+1) / (p^3 * (n-1) * n * (n+1))`. -/ lemma sum_binom_div_r_r1_r2_le {p : ℝ} (hp : 0 < p) {n : ℕ} (hn : 4 ≤ n) : ∑ r ∈ Finset.Ico 4 n, p ^ (r - 1) * (Nat.choose (n - 2) (r - 1) : ℝ) / ((r : ℝ) * ((r : ℝ) + 1) * ((r : ℝ) + 2)) ≤ (p + 1) ^ (n + 1) / (p ^ 3 * ((n - 1 : ℕ) : ℝ) * (n : ℝ) * ((n + 1 : ℕ) : ℝ)) := by have hn1 : ((n - 1 : ℕ) : ℝ) ≠ 0 := by exact_mod_cast (show n - 1 ≠ 0 by omega) have hn0 : (n : ℝ) ≠ 0 := by exact_mod_cast (show n ≠ 0 by omega) have hnp1 : ((n + 1 : ℕ) : ℝ) ≠ 0 := by positivity have hterm : ∀ r ∈ Finset.Ico 4 n, p ^ (r - 1) * (Nat.choose (n - 2) (r - 1) : ℝ) / ((r : ℝ) * ((r : ℝ) + 1) * ((r : ℝ) + 2)) = (p ^ (r + 2) * (Nat.choose (n + 1) (r + 2) : ℝ)) / (p ^ 3 * ((n - 1 : ℕ) : ℝ) * (n : ℝ) * ((n + 1 : ℕ) : ℝ)) := by intro r hr have hr4 : 4 ≤ r := (Finset.mem_Ico.mp hr).1 have hr0 : (r : ℝ) ≠ 0 := by exact_mod_cast (show r ≠ 0 by omega) have hr1 : (r : ℝ) + 1 ≠ 0 := by positivity have hr2 : (r : ℝ) + 2 ≠ 0 := by positivity have habs1 : (n - 1) * Nat.choose (n - 2) (r - 1) = r * Nat.choose (n - 1) r := by have := Nat.add_one_mul_choose_eq (n - 2) (r - 1) have h1 : n - 2 + 1 = n - 1 := by omega have h2 : r - 1 + 1 = r := by omega rw [h1, h2] at this; linarith have habs2 : n * Nat.choose (n - 1) r = (r + 1) * Nat.choose n (r + 1) := by have := Nat.add_one_mul_choose_eq (n - 1) r have h1 : n - 1 + 1 = n := by omega rw [h1] at this; linarith have habs3 : (n + 1) * Nat.choose n (r + 1) = (r + 2) * Nat.choose (n + 1) (r + 2) := by have := Nat.add_one_mul_choose_eq n (r + 1); linarith have habsR : ((n - 1 : ℕ) : ℝ) * (n : ℝ) * ((n + 1 : ℕ) : ℝ) * (Nat.choose (n - 2) (r - 1) : ℝ) = (r : ℝ) * ((r : ℝ) + 1) * ((r : ℝ) + 2) * (Nat.choose (n + 1) (r + 2) : ℝ) := by have h1 : ((n - 1 : ℕ) : ℝ) * (Nat.choose (n - 2) (r - 1) : ℝ) = (r : ℝ) * (Nat.choose (n - 1) r : ℝ) := by exact_mod_cast habs1 have h2 : (n : ℝ) * (Nat.choose (n - 1) r : ℝ) = ((r + 1 : ℕ) : ℝ) * (Nat.choose n (r + 1) : ℝ) := by exact_mod_cast habs2 have h3 : ((n + 1 : ℕ) : ℝ) * (Nat.choose n (r + 1) : ℝ) = ((r + 2 : ℕ) : ℝ) * (Nat.choose (n + 1) (r + 2) : ℝ) := by exact_mod_cast habs3 push_cast at h2 h3 calc ((n - 1 : ℕ) : ℝ) * (n : ℝ) * ((n + 1 : ℕ) : ℝ) * (Nat.choose (n - 2) (r - 1) : ℝ) = (n : ℝ) * ((n + 1 : ℕ) : ℝ) * (((n - 1 : ℕ) : ℝ) * (Nat.choose (n - 2) (r - 1) : ℝ)) := by ring _ = (r : ℝ) * ((n + 1 : ℕ) : ℝ) * ((n : ℝ) * (Nat.choose (n - 1) r : ℝ)) := by rw [h1]; ring _ = (r : ℝ) * ((r : ℝ) + 1) * (((n : ℝ) + 1) * (Nat.choose n (r + 1) : ℝ)) := by rw [h2]; push_cast; ring _ = _ := by rw [h3]; ring have hpow : p ^ (r + 2) = p ^ (r - 1) * p ^ 3 := by rw [← pow_add]; congr 1; omega rw [hpow]; field_simp [hp.ne', hn1, hn0, hnp1, hr0, hr1, hr2]; nlinarith rw [Finset.sum_congr rfl hterm, ← Finset.sum_div] refine div_le_div_of_nonneg_right ?_ (by positivity) have hshift : ∑ r ∈ Finset.Ico 4 n, p ^ (r + 2) * (Nat.choose (n + 1) (r + 2) : ℝ) = ∑ k ∈ Finset.Ico 6 (n + 2), p ^ k * (Nat.choose (n + 1) k : ℝ) := by refine Finset.sum_bij (fun r _ => r + 2) (fun r hr => by simp only [Finset.mem_Ico] at hr ⊢; omega) (fun _ _ _ _ h => by omega) (fun k hk => ⟨k - 2, by simp only [Finset.mem_Ico] at hk ⊢; omega, by simp only [Finset.mem_Ico] at hk; omega⟩) (fun _ _ => rfl) rw [hshift] have hbin := add_pow p 1 (n + 1) simp only [one_pow, mul_one] at hbin rw [hbin] refine Finset.sum_le_sum_of_subset_of_nonneg ?_ (fun _ _ _ => by positivity) intro k hk; simp only [Finset.mem_Ico] at hk; simp only [Finset.mem_range]; omega /-- Our explicit closed-form leading-constant-optimal upper bound `dsUpperBound q n` for `q`-ary single-deletion single-substitution codes of length `n ≥ 4`. -/ noncomputable def dsUpperBound (q n : ℕ) : ℝ := let qR : ℝ := (q : ℝ) let p : ℝ := qR - 1 let nR : ℝ := (n : ℝ) let A : ℝ := p * (nR - 2) - 1 qR ^ n / (A * p * (nR - 1)) * (1 + 2 * qR / (p * nR) + 18 * qR ^ 2 / (p ^ 2 * nR * (nR + 1))) + qR * (p * (nR - 2) + 2) / 2 /-- The published non-asymptotic upper bound of Smagloy, Welter, Wachter-Zeh & Yaakobi (*IEEE Trans. Inform. Theory* 69(12):7659–7671, 2023; `arXiv:2005.09352`, Theorem 4): `3 * q^(n - 1) / ((q - 1) * (n - 5) * (n - 3)) + 5 * q`. -/ noncomputable def smagloyBound (q n : ℕ) : ℝ := 3 * (q : ℝ) ^ (n - 1) / (((q : ℝ) - 1) * ((n : ℝ) - 5) * ((n : ℝ) - 3)) + 5 * (q : ℝ) /-- **MAIN CLOSED-FORM DUAL BOUND**: For EVERY `q ≥ 2` and EVERY `n = m + 2 ≥ 4` (`m ≥ 2`), `∑ y : Word q (m + 1), dsDualWeight y ≤ dsUpperBound q (m + 2)`! -/ theorem dsDualWeight_sum_le_dsUpperBound {q m : ℕ} (hq : 2 ≤ q) (hm : 2 ≤ m) : ∑ y : Word q (m + 1), dsDualWeight y ≤ dsUpperBound q (m + 2) := by let n : ℕ := m + 2 let qR : ℝ := (q : ℝ) let p : ℝ := qR - 1 let nR : ℝ := (n : ℝ) let A : ℝ := p * (nR - 2) - 1 have hq2 : 2 ≤ qR := by dsimp [qR]; exact_mod_cast hq have hn4 : 4 ≤ nR := by dsimp [nR, n]; exact_mod_cast (show 4 ≤ m + 2 by omega) have hp_pos : 0 < p := by dsimp [p]; linarith have hA_pos : 0 < A := by dsimp [A]; nlinarith have hnm1 : 0 < nR - 1 := by linarith have hn0 : 0 < nR := by linarith have hnp1 : 0 < nR + 1 := by linarith -- Partition `Word q (m + 1)` into `runCount y ≤ 3` and `4 ≤ runCount y`! have hsplit : ∑ y : Word q (m + 1), dsDualWeight y = (∑ y ∈ Finset.univ.filter (fun y : Word q (m + 1) => runCount y ≤ 3), dsDualWeight y) + (∑ y ∈ Finset.univ.filter (fun y : Word q (m + 1) => 4 ≤ runCount y), dsDualWeight y) := by have h1 := (Finset.sum_filter_add_sum_filter_not (Finset.univ : Finset (Word q (m + 1))) (fun y => runCount y ≤ 3) dsDualWeight).symm have hnot : (Finset.univ.filter fun y : Word q (m + 1) => ¬ runCount y ≤ 3) = Finset.univ.filter fun y : Word q (m + 1) => 4 ≤ runCount y := by ext y; simp only [Finset.mem_filter, Finset.mem_univ, true_and]; omega rwa [hnot] at h1 rw [hsplit] -- (A) Bound the low-run tail (`runCount y ≤ 3`): `≤ qR * (p * (nR - 2) + 2) / 2`! have htail : ∑ y ∈ Finset.univ.filter (fun y : Word q (m + 1) => runCount y ≤ 3), dsDualWeight y ≤ qR * (p * (nR - 2) + 2) / 2 := by have hpt : ∀ y ∈ Finset.univ.filter (fun y : Word q (m + 1) => runCount y ≤ 3), dsDualWeight y = 1 / (((q - 1) * (m + 1) + 1 : ℕ) : ℝ) := fun y hy => by unfold dsDualWeight; rw [if_pos (Finset.mem_filter.mp hy).2] rw [Finset.sum_congr rfl hpt, Finset.sum_const, nsmul_eq_mul] -- Count `{y | runCount y ≤ 3}` as disjoint union of `r = 1, 2, 3`! have hpart3 : (Finset.univ.filter fun y : Word q (m + 1) => runCount y ≤ 3) = (Finset.Ico 1 4).biUnion fun r => Finset.univ.filter fun y : Word q (m + 1) => runCount y = r := by ext y; simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_biUnion, Finset.mem_Ico] constructor · intro h3; exact ⟨runCount y, ⟨one_le_runCount y, by omega⟩, rfl⟩ · rintro ⟨r, ⟨_, hr4⟩, rfl⟩; omega have hdisj3 : Set.PairwiseDisjoint (Finset.Ico 1 4 : Set ℕ) (fun r => Finset.univ.filter fun y : Word q (m + 1) => runCount y = r) := fun r1 _ r2 _ hne => Finset.disjoint_left.mpr fun y h1 h2 => hne ((Finset.mem_filter.mp h1).2.symm.trans (Finset.mem_filter.mp h2).2) have hcard3 : (Finset.univ.filter fun y : Word q (m + 1) => runCount y ≤ 3).card = q + q * (q - 1) * m + q * (q - 1) ^ 2 * Nat.choose m 2 := by rw [hpart3, Finset.card_biUnion hdisj3] have hico : Finset.Ico 1 4 = {1, 2, 3} := by decide rw [hico, Finset.sum_insert (by decide), Finset.sum_insert (by decide), Finset.sum_singleton] rw [card_words_with_runCount (by omega) (by omega), card_words_with_runCount (by omega) (by omega), card_words_with_runCount (by omega) (by omega)] simp only [Nat.sub_self, pow_zero, Nat.choose_zero_right, mul_one, Nat.reduceSub, pow_one, Nat.choose_one_right] ring rw [hcard3] have hcm2 : (Nat.choose m 2 : ℝ) = (nR - 2) * (nR - 3) / 2 := by rw [Nat.choose_two_right, Nat.cast_div (Nat.even_mul_pred_self m).two_dvd (by norm_num), Nat.cast_mul, Nat.cast_sub (by omega : 1 ≤ m), Nat.cast_one, Nat.cast_ofNat] have hmR : (m : ℝ) = nR - 2 := by dsimp [nR, n]; push_cast; ring rw [hmR]; ring have hden_eq : (((q - 1) * (m + 1) + 1 : ℕ) : ℝ) = p * (nR - 1) + 1 := by push_cast [Nat.cast_sub (by omega : 1 ≤ q)] have hmR : (m : ℝ) = nR - 2 := by dsimp [nR, n]; push_cast; ring dsimp only [p, qR]; rw [hmR]; ring have hnum_eq : ((q + q * (q - 1) * m + q * (q - 1) ^ 2 * Nat.choose m 2 : ℕ) : ℝ) = qR * (1 + p * (nR - 2) + p ^ 2 * ((nR - 2) * (nR - 3) / 2)) := by push_cast [Nat.cast_sub (by omega : 1 ≤ q)] rw [hcm2] have hmR : (m : ℝ) = nR - 2 := by dsimp [nR, n]; push_cast; ring dsimp only [p, qR]; rw [hmR]; ring rw [hden_eq, hnum_eq, mul_one_div] have hden_pos : 0 < p * (nR - 1) + 1 := by positivity rw [div_le_div_iff₀ hden_pos (by norm_num)] have hdiff : qR * (p * (nR - 2) + 2) * (p * (nR - 1) + 1) - qR * (1 + p * (nR - 2) + p ^ 2 * ((nR - 2) * (nR - 3) / 2)) * 2 = qR * (p * (nR - 2) + 2 * p ^ 2 * (nR - 2) + 2 * p) := by ring have hnn : 0 ≤ qR * (p * (nR - 2) + 2 * p ^ 2 * (nR - 2) + 2 * p) := by have : 0 ≤ nR - 2 := by linarith positivity linarith -- (B) Bound the main sum (`4 ≤ runCount y`) via `inv_sub_two_le` + rising-factorial sums! have hmain : ∑ y ∈ Finset.univ.filter (fun y : Word q (m + 1) => 4 ≤ runCount y), dsDualWeight y ≤ qR ^ n / (A * p * (nR - 1)) * (1 + 2 * qR / (p * nR) + 18 * qR ^ 2 / (p ^ 2 * nR * (nR + 1))) := by have hpart4 : (Finset.univ.filter fun y : Word q (m + 1) => 4 ≤ runCount y) = (Finset.Ico 4 n).biUnion fun r => Finset.univ.filter fun y : Word q (m + 1) => runCount y = r := by ext y; simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_biUnion, Finset.mem_Ico, n] constructor · intro h4; exact ⟨runCount y, ⟨h4, by have := runCount_le_len y; omega⟩, rfl⟩ · rintro ⟨r, ⟨hr4, _⟩, rfl⟩; exact hr4 have hdisj4 : Set.PairwiseDisjoint (Finset.Ico 4 n : Set ℕ) (fun r => Finset.univ.filter fun y : Word q (m + 1) => runCount y = r) := fun r1 _ r2 _ hne => Finset.disjoint_left.mpr fun y h1 h2 => hne ((Finset.mem_filter.mp h1).2.symm.trans (Finset.mem_filter.mp h2).2) rw [hpart4, Finset.sum_biUnion hdisj4] have hA_eq : (((q - 1) * m - 1 : ℕ) : ℝ) = A := by have hqm : 1 ≤ (q - 1) * m := by have : 1 ≤ q - 1 := by omega nlinarith rw [Nat.cast_sub hqm, Nat.cast_mul, Nat.cast_sub (by omega : 1 ≤ q), Nat.cast_one] have hmR : (m : ℝ) = nR - 2 := by dsimp [nR, n]; push_cast; ring dsimp only [A, p, qR]; rw [hmR] have hterm_le : ∀ r ∈ Finset.Ico 4 n, ∑ y ∈ Finset.univ.filter (fun y : Word q (m + 1) => runCount y = r), dsDualWeight y ≤ (qR / A) * (p ^ (r - 1) * (Nat.choose (n - 2) (r - 1) : ℝ) / (r : ℝ) + 2 * (p ^ (r - 1) * (Nat.choose (n - 2) (r - 1) : ℝ) / ((r : ℝ) * ((r : ℝ) + 1))) + 18 * (p ^ (r - 1) * (Nat.choose (n - 2) (r - 1) : ℝ) / ((r : ℝ) * ((r : ℝ) + 1) * ((r : ℝ) + 2)))) := by intro r hr have hr4 : 4 ≤ r := (Finset.mem_Ico.mp hr).1 have hpt : ∀ y ∈ Finset.univ.filter (fun y : Word q (m + 1) => runCount y = r), dsDualWeight y ≤ (1 / A) * (1 / ((r : ℝ) - 2)) := fun y hy => by have hrc := (Finset.mem_filter.mp hy).2 unfold dsDualWeight rw [if_neg (by omega : ¬ runCount y ≤ 3), hrc] have hle_den : A * ((r : ℝ) - 2) ≤ ((((q - 1) * m - 1) * (r - 2) + (q + 2) : ℕ) : ℝ) := by push_cast [Nat.cast_sub (by omega : 2 ≤ r)] rw [hA_eq]; linarith have hpos_l : 0 < A * ((r : ℝ) - 2) := by have : (4 : ℝ) ≤ (r : ℝ) := by exact_mod_cast hr4 have : 0 < (r : ℝ) - 2 := by linarith positivity have hinv := one_div_le_one_div_of_le hpos_l hle_den rw [one_div_mul_one_div] exact hinv calc ∑ y ∈ Finset.univ.filter (fun y : Word q (m + 1) => runCount y = r), dsDualWeight y ≤ ∑ _y ∈ Finset.univ.filter (fun y : Word q (m + 1) => runCount y = r), (1 / A) * (1 / ((r : ℝ) - 2)) := Finset.sum_le_sum hpt _ = (qR * p ^ (r - 1) * (Nat.choose (n - 2) (r - 1) : ℝ)) * ((1 / A) * (1 / ((r : ℝ) - 2))) := by rw [Finset.sum_const, nsmul_eq_mul, card_words_with_runCount (by omega) (by omega)] push_cast [Nat.cast_sub (by omega : 1 ≤ q)] have hnm2 : n - 2 = m := rfl dsimp only [qR, p]; rw [hnm2] _ ≤ (qR * p ^ (r - 1) * (Nat.choose (n - 2) (r - 1) : ℝ)) * ((1 / A) * (1 / (r : ℝ) + 2 / ((r : ℝ) * ((r : ℝ) + 1)) + 18 / ((r : ℝ) * ((r : ℝ) + 1) * ((r : ℝ) + 2)))) := by refine mul_le_mul_of_nonneg_left (mul_le_mul_of_nonneg_left ?_ (by positivity)) (by positivity) exact inv_sub_two_le (by exact_mod_cast hr4) _ = _ := by ring calc ∑ r ∈ Finset.Ico 4 n, ∑ y ∈ Finset.univ.filter (fun y : Word q (m + 1) => runCount y = r), dsDualWeight y ≤ ∑ r ∈ Finset.Ico 4 n, (qR / A) * (p ^ (r - 1) * (Nat.choose (n - 2) (r - 1) : ℝ) / (r : ℝ) + 2 * (p ^ (r - 1) * (Nat.choose (n - 2) (r - 1) : ℝ) / ((r : ℝ) * ((r : ℝ) + 1))) + 18 * (p ^ (r - 1) * (Nat.choose (n - 2) (r - 1) : ℝ) / ((r : ℝ) * ((r : ℝ) + 1) * ((r : ℝ) + 2)))) := Finset.sum_le_sum hterm_le _ = (qR / A) * ((∑ r ∈ Finset.Ico 4 n, p ^ (r - 1) * (Nat.choose (n - 2) (r - 1) : ℝ) / (r : ℝ)) + 2 * (∑ r ∈ Finset.Ico 4 n, p ^ (r - 1) * (Nat.choose (n - 2) (r - 1) : ℝ) / ((r : ℝ) * ((r : ℝ) + 1))) + 18 * (∑ r ∈ Finset.Ico 4 n, p ^ (r - 1) * (Nat.choose (n - 2) (r - 1) : ℝ) / ((r : ℝ) * ((r : ℝ) + 1) * ((r : ℝ) + 2)))) := by rw [← Finset.mul_sum]; congr 1 simp_rw [Finset.sum_add_distrib, ← Finset.mul_sum] _ ≤ (qR / A) * (qR ^ (n - 1) / (p * ((n - 1 : ℕ) : ℝ)) + 2 * (qR ^ n / (p ^ 2 * ((n - 1 : ℕ) : ℝ) * nR)) + 18 * (qR ^ (n + 1) / (p ^ 3 * ((n - 1 : ℕ) : ℝ) * nR * ((n + 1 : ℕ) : ℝ)))) := by have hpq : p + 1 = qR := by dsimp [p]; ring have hS0 := sum_binom_div_r_le hp_pos (show 4 ≤ n by dsimp [n]; omega) have hS1 := sum_binom_div_r_r1_le hp_pos (show 4 ≤ n by dsimp [n]; omega) have hS2 := sum_binom_div_r_r1_r2_le hp_pos (show 4 ≤ n by dsimp [n]; omega) rw [hpq] at hS0 hS1 hS2 refine mul_le_mul_of_nonneg_left ?_ (by positivity) linarith only [hS0, hS1, hS2] _ = qR ^ n / (A * p * (nR - 1)) * (1 + 2 * qR / (p * nR) + 18 * qR ^ 2 / (p ^ 2 * nR * (nR + 1))) := by have hcn1 : ((n - 1 : ℕ) : ℝ) = nR - 1 := by rw [Nat.cast_sub (by dsimp [n]; omega), Nat.cast_one] have hcnp1 : ((n + 1 : ℕ) : ℝ) = nR + 1 := by push_cast; rfl have hq_sub : qR ^ n = qR ^ (n - 1) * qR := by rw [← pow_succ]; congr 1 have hq_add : qR ^ (n + 1) = qR ^ n * qR := pow_succ qR n rw [hcn1, hcnp1, hq_add] have hq_ne : qR ≠ 0 := by linarith have hq_sub' : qR ^ (n - 1) = qR ^ n / qR := by rw [hq_sub, mul_div_cancel_right₀ _ hq_ne] rw [hq_sub'] field_simp [hA_pos.ne', hp_pos.ne', hnm1.ne', hn0.ne', hnp1.ne', hq_ne] unfold dsUpperBound dsimp only [qR, p, nR, A, n] at htail hmain ⊢ linarith only [htail, hmain] /-- **MAIN WORLD-RECORD NON-ASYMPTOTIC UPPER BOUND (`q ≥ 2`, `n ≥ 4`)**: For EVERY alphabet size `q ≥ 2`, EVERY block length `n = m + 2 ≥ 4` (`m ≥ 2`), and EVERY `q`-ary single-deletion single-substitution correcting code `C ⊆ Word q (m + 2)`: `|C| ≤ dsUpperBound q (m + 2)`! -/ theorem qary_delSub_upper_bound {q m : ℕ} (hq : 2 ≤ q) (hm : 2 ≤ m) (C : Finset (Word q (m + 2))) (hC : IsDelSubCode C) : (C.card : ℝ) ≤ dsUpperBound q (m + 2) := le_trans (delSubCode_card_le_dsDualWeight_sum hq hm C hC) (dsDualWeight_sum_le_dsUpperBound hq hm) end ClosedFormUpperBound /-! ## Section 5: Strict Universal Improvement over Smagloy–Welter–Wachter-Zeh–Yaakobi (2023) & Exact Asymptotic Ratio -/ section WorldRecordDominance /-- Binomial lower bound on `(p + 1) ^ k` retaining degrees `2, 3, 4` for `k ≥ 4`: `C(k, 2) * p^2 + C(k, 3) * p^3 + C(k, 4) * p^4 ≤ (p + 1) ^ k`. -/ private lemma add_pow_ge_deg234 {p : ℝ} (hp : 0 ≤ p) {k : ℕ} (hk : 4 ≤ k) : (k : ℝ) * ((k : ℝ) - 1) / 2 * p ^ 2 + (k : ℝ) * ((k : ℝ) - 1) * ((k : ℝ) - 2) / 6 * p ^ 3 + (k : ℝ) * ((k : ℝ) - 1) * ((k : ℝ) - 2) * ((k : ℝ) - 3) / 24 * p ^ 4 ≤ (p + 1) ^ k := by have hbin := add_pow p 1 k simp only [one_pow, mul_one] at hbin rw [hbin] have hsub : ({2, 3, 4} : Finset ℕ) ⊆ Finset.range (k + 1) := by intro j hj simp only [Finset.mem_insert, Finset.mem_singleton] at hj simp only [Finset.mem_range] rcases hj with rfl | rfl | rfl <;> omega have hle := Finset.sum_le_sum_of_subset_of_nonneg hsub (f := fun j => p ^ j * (Nat.choose k j : ℝ)) (fun _ _ _ => by positivity) rw [Finset.sum_insert (by decide), Finset.sum_insert (by decide), Finset.sum_singleton] at hle -- Cast `C(k, 2)`, `C(k, 3)`, `C(k, 4)`! have hc2 : (Nat.choose k 2 : ℝ) = (k : ℝ) * ((k : ℝ) - 1) / 2 := by rw [Nat.choose_two_right, Nat.cast_div (Nat.even_mul_pred_self k).two_dvd (by norm_num), Nat.cast_mul, Nat.cast_sub (by omega : 1 ≤ k), Nat.cast_one, Nat.cast_ofNat] have hc3 : (Nat.choose k 3 : ℝ) = (k : ℝ) * ((k : ℝ) - 1) * ((k : ℝ) - 2) / 6 := by have hmul : 6 * Nat.choose k 3 = k * (k - 1) * (k - 2) := by have h1 := Nat.add_one_mul_choose_eq (k - 1) 2 have h2 := Nat.choose_two_right (k - 1) have h3 : 2 * Nat.choose (k - 1) 2 = (k - 1) * (k - 2) := by rw [h2] have heq : k - 1 - 1 = k - 2 := by omega rw [heq] have hdvd : 2 ∣ (k - 1) * (k - 2) := by have := (Nat.even_mul_pred_self (k - 1)).two_dvd rwa [heq] at this exact Nat.mul_div_cancel' hdvd have hk1 : k - 1 + 1 = k := by omega rw [hk1] at h1 nlinarith have hR : (6 : ℝ) * (Nat.choose k 3 : ℝ) = (k : ℝ) * ((k : ℝ) - 1) * ((k : ℝ) - 2) := by have : ((6 * Nat.choose k 3 : ℕ) : ℝ) = ((k * (k - 1) * (k - 2) : ℕ) : ℝ) := by exact_mod_cast hmul push_cast [Nat.cast_sub (by omega : 1 ≤ k), Nat.cast_sub (by omega : 2 ≤ k)] at this linarith linarith have hc4 : (Nat.choose k 4 : ℝ) = (k : ℝ) * ((k : ℝ) - 1) * ((k : ℝ) - 2) * ((k : ℝ) - 3) / 24 := by have hmul : 24 * Nat.choose k 4 = k * (k - 1) * (k - 2) * (k - 3) := by have h1 := Nat.add_one_mul_choose_eq (k - 1) 3 have hk1 : k - 1 + 1 = k := by omega rw [hk1] at h1 have h2 := Nat.add_one_mul_choose_eq (k - 2) 2 have hk2 : k - 2 + 1 = k - 1 := by omega rw [hk2] at h2 have h3 := Nat.choose_two_right (k - 2) have heq : k - 2 - 1 = k - 3 := by omega have h4 : 2 * Nat.choose (k - 2) 2 = (k - 2) * (k - 3) := by rw [h3, heq] have hdvd : 2 ∣ (k - 2) * (k - 3) := by have := (Nat.even_mul_pred_self (k - 2)).two_dvd rwa [heq] at this exact Nat.mul_div_cancel' hdvd calc 24 * Nat.choose k 4 = 6 * (Nat.choose k 4 * 4) := by ring _ = 6 * (k * Nat.choose (k - 1) 3) := by rw [← h1] _ = 2 * k * (Nat.choose (k - 1) 3 * 3) := by ring _ = 2 * k * ((k - 1) * Nat.choose (k - 2) 2) := by rw [← h2] _ = k * (k - 1) * (2 * Nat.choose (k - 2) 2) := by ring _ = k * (k - 1) * ((k - 2) * (k - 3)) := by rw [h4] _ = k * (k - 1) * (k - 2) * (k - 3) := by ring have hR : (24 : ℝ) * (Nat.choose k 4 : ℝ) = (k : ℝ) * ((k : ℝ) - 1) * ((k : ℝ) - 2) * ((k : ℝ) - 3) := by have : ((24 * Nat.choose k 4 : ℕ) : ℝ) = ((k * (k - 1) * (k - 2) * (k - 3) : ℕ) : ℝ) := by exact_mod_cast hmul push_cast [Nat.cast_sub (by omega : 1 ≤ k), Nat.cast_sub (by omega : 2 ≤ k), Nat.cast_sub (by omega : 3 ≤ k)] at this linarith linarith rw [hc2, hc3, hc4] at hle linarith /-- Helper (1): Main exponential coefficient of `dsUpperBound q n` is `≤ (4 / 5)` of Smagloy et al.'s exponential coefficient for EVERY `q ≥ 2` and EVERY `n ≥ 6`! -/ private lemma dsUpperBound_main_coeff_le {qR nR : ℝ} (hq : 2 ≤ qR) (hn : 6 ≤ nR) : let p : ℝ := qR - 1 let A : ℝ := p * (nR - 2) - 1 (1 + 2 * qR / (p * nR) + 18 * qR ^ 2 / (p ^ 2 * nR * (nR + 1))) / (A * p * (nR - 1)) ≤ (4 / 5) * (3 / (qR * p * (nR - 5) * (nR - 3))) := by intro p A let a : ℝ := p - 1 let b : ℝ := nR - 6 have ha : 0 ≤ a := by dsimp [a, p]; linarith have hb : 0 ≤ b := by dsimp [b]; linarith have hp_pos : 0 < p := by dsimp [p]; linarith have hA_pos : 0 < A := by dsimp [A]; nlinarith have hq_pos : 0 < qR := by linarith have hnm5 : 0 < nR - 5 := by linarith have hnm3 : 0 < nR - 3 := by linarith have hnm1 : 0 < nR - 1 := by linarith have hn0 : 0 < nR := by linarith have hnp1 : 0 < nR + 1 := by linarith have hp_a : p = a + 1 := by dsimp [a]; ring have hq_a : qR = a + 2 := by dsimp [a, p]; ring have hn_b : nR = b + 6 := by dsimp [b]; ring have hden : 0 < 5 * qR * A * p ^ 3 * (nR - 5) * (nR - 3) * (nR - 1) * nR * (nR + 1) := by positivity set L : ℝ := (1 + 2 * qR / (p * nR) + 18 * qR ^ 2 / (p ^ 2 * nR * (nR + 1))) / (A * p * (nR - 1)) set R : ℝ := (4 / 5) * (3 / (qR * p * (nR - 5) * (nR - 3))) have hnum : 5 * qR * A * p ^ 3 * (nR - 5) * (nR - 3) * (nR - 1) * nR * (nR + 1) * (R - L) = (2 * (b ^ 2 - 10) ^ 2 + 42 * b * (b - 2) ^ 2 + 10 * b ^ 2 + 14 * b + 3100) + 17130 * a + 8425 * a * b + 1627 * a * b ^ 2 + 263 * a * b ^ 3 + 11 * a * b ^ 4 + 22530 * a ^ 2 + 13834 * a ^ 2 * b + 3198 * a ^ 2 * b ^ 2 + 390 * a ^ 2 * b ^ 3 + 16 * a ^ 2 * b ^ 4 + 8970 * a ^ 3 + 5951 * a ^ 3 * b + 1463 * a ^ 3 * b ^ 2 + 169 * a ^ 3 * b ^ 3 + 7 * a ^ 3 * b ^ 4 := by dsimp only [L, R, A] have hA_ne : p * (nR - 2) - 1 ≠ 0 := hA_pos.ne' field_simp [hq_pos.ne', hA_ne, hp_pos.ne', hnm5.ne', hnm3.ne', hnm1.ne', hn0.ne', hnp1.ne'] rw [hp_a, hq_a, hn_b]; ring have hpos : 0 ≤ 5 * qR * A * p ^ 3 * (nR - 5) * (nR - 3) * (nR - 1) * nR * (nR + 1) * (R - L) := by rw [hnum]; positivity have hdiff := nonneg_of_mul_nonneg_right hpos hden linarith only [hdiff] /-- Helper (2): Polynomial tail of `dsUpperBound q n` is strictly less than `(1 / 5)` of Smagloy's main term plus `5 * q` for EVERY `q ≥ 2` and EVERY `n ≥ 6`! -/ private lemma dsUpperBound_tail_lt {q n : ℕ} (hq : 2 ≤ q) (hn : 6 ≤ n) : let qR : ℝ := (q : ℝ) let p : ℝ := qR - 1 let nR : ℝ := (n : ℝ) qR * (p * (nR - 2) + 2) / 2 < (1 / 5) * (3 * qR ^ (n - 1) / (p * (nR - 5) * (nR - 3))) + 5 * qR := by intro qR p nR have hq2 : 2 ≤ qR := by dsimp [qR]; exact_mod_cast hq have hn6 : 6 ≤ nR := by dsimp [nR]; exact_mod_cast hn have hp_pos : 0 < p := by dsimp [p]; linarith have hq_pos : 0 < qR := by linarith have hnm5 : 0 < nR - 5 := by linarith have hnm3 : 0 < nR - 3 := by linarith -- Lower bound `qR ^ (n - 1) = qR * (p + 1) ^ (n - 2)` via `add_pow_ge_deg234`! have hq_split : qR ^ (n - 1) = qR * (p + 1) ^ (n - 2) := by have hpq : p + 1 = qR := by dsimp [p]; ring rw [hpq, mul_comm, ← pow_succ] congr 1; omega have hdeg := add_pow_ge_deg234 hp_pos.le (show 4 ≤ n - 2 by omega) have hcn2 : ((n - 2 : ℕ) : ℝ) = nR - 2 := by rw [Nat.cast_sub (by omega : 2 ≤ n), Nat.cast_ofNat] rw [hcn2] at hdeg -- Prove the strict polynomial inequality with `hdeg` plugged in! let a : ℝ := p - 1 let b : ℝ := nR - 6 have ha : 0 ≤ a := by dsimp [a, p]; linarith have hb : 0 ≤ b := by dsimp [b]; linarith have hp_a : p = a + 1 := by dsimp [a]; ring have hn_b : nR = b + 6 := by dsimp [b]; ring have hpoly_pos : 0 < 3 * (nR - 2) * (12 * p ^ 2 + 4 * (nR - 4) * p ^ 3 + (nR - 4) * (nR - 5) * p ^ 4) - 60 * p * (nR - 5) * (p * (nR - 2) - 8) := by have hid : 3 * (nR - 2) * (12 * p ^ 2 + 4 * (nR - 4) * p ^ 3 + (nR - 4) * (nR - 5) * p ^ 4) - 60 * p * (nR - 5) * (p * (nR - 2) - 8) = (3 * b * (b - 5) ^ 2 + 3 * b ^ 2 + 255 * b + 504) + 672 * a + 336 * a * b + 12 * a * b ^ 3 + 336 * a ^ 2 + 204 * a ^ 2 * b + 102 * a ^ 2 * b ^ 2 + 18 * a ^ 2 * b ^ 3 + 192 * a ^ 3 + 240 * a ^ 3 * b + 96 * a ^ 3 * b ^ 2 + 12 * a ^ 3 * b ^ 3 + 24 * a ^ 4 + 42 * a ^ 4 * b + 21 * a ^ 4 * b ^ 2 + 3 * a ^ 4 * b ^ 3 := by rw [hp_a, hn_b]; ring rw [hid]; positivity have hden : 0 < 120 * p * (nR - 5) * (nR - 3) := by positivity have hstep1 : p * (nR - 2) / 2 - 4 < (3 / 5) * ((nR - 2) * (nR - 3) / 2 * p ^ 2 + (nR - 2) * (nR - 3) * (nR - 4) / 6 * p ^ 3 + (nR - 2) * (nR - 3) * (nR - 4) * (nR - 5) / 24 * p ^ 4) / (p * (nR - 5) * (nR - 3)) := by have hmul_id : 120 * p * (nR - 5) * (nR - 3) * ((3 / 5) * ((nR - 2) * (nR - 3) / 2 * p ^ 2 + (nR - 2) * (nR - 3) * (nR - 4) / 6 * p ^ 3 + (nR - 2) * (nR - 3) * (nR - 4) * (nR - 5) / 24 * p ^ 4) / (p * (nR - 5) * (nR - 3)) - (p * (nR - 2) / 2 - 4)) = (nR - 3) * (3 * (nR - 2) * (12 * p ^ 2 + 4 * (nR - 4) * p ^ 3 + (nR - 4) * (nR - 5) * p ^ 4) - 60 * p * (nR - 5) * (p * (nR - 2) - 8)) := by field_simp [hp_pos.ne', hnm5.ne', hnm3.ne']; ring have hpos : 0 < 120 * p * (nR - 5) * (nR - 3) * ((3 / 5) * ((nR - 2) * (nR - 3) / 2 * p ^ 2 + (nR - 2) * (nR - 3) * (nR - 4) / 6 * p ^ 3 + (nR - 2) * (nR - 3) * (nR - 4) * (nR - 5) / 24 * p ^ 4) / (p * (nR - 5) * (nR - 3)) - (p * (nR - 2) / 2 - 4)) := by rw [hmul_id]; exact mul_pos hnm3 hpoly_pos have hdiff := pos_of_mul_pos_right hpos hden.le linarith only [hdiff] have hstep2 : (3 / 5) * ((nR - 2) * (nR - 3) / 2 * p ^ 2 + (nR - 2) * (nR - 3) * (nR - 4) / 6 * p ^ 3 + (nR - 2) * (nR - 3) * (nR - 4) * (nR - 5) / 24 * p ^ 4) / (p * (nR - 5) * (nR - 3)) ≤ (3 / 5) * (p + 1) ^ (n - 2) / (p * (nR - 5) * (nR - 3)) := by refine div_le_div_of_nonneg_right (mul_le_mul_of_nonneg_left ?_ (by norm_num)) (by positivity) have hrew : (nR - 2) * (nR - 3) / 2 * p ^ 2 + (nR - 2) * (nR - 3) * (nR - 4) / 6 * p ^ 3 + (nR - 2) * (nR - 3) * (nR - 4) * (nR - 5) / 24 * p ^ 4 = (nR - 2) * (nR - 2 - 1) / 2 * p ^ 2 + (nR - 2) * (nR - 2 - 1) * (nR - 2 - 2) / 6 * p ^ 3 + (nR - 2) * (nR - 2 - 1) * (nR - 2 - 2) * (nR - 2 - 3) / 24 * p ^ 4 := by ring rw [hrew]; exact hdeg have hcomb : p * (nR - 2) / 2 - 4 < (3 / 5) * (p + 1) ^ (n - 2) / (p * (nR - 5) * (nR - 3)) := lt_of_lt_of_le hstep1 hstep2 have hmulq := mul_lt_mul_of_pos_left hcomb hq_pos rw [hq_split] have hL : qR * (p * (nR - 2) / 2 - 4) = qR * (p * (nR - 2) + 2) / 2 - 5 * qR := by ring have hR : qR * ((3 / 5) * (p + 1) ^ (n - 2) / (p * (nR - 5) * (nR - 3))) = (1 / 5) * (3 * (qR * (p + 1) ^ (n - 2)) / (p * (nR - 5) * (nR - 3))) := by ring rw [hL, hR] at hmulq linarith only [hmulq] /-- **UNIVERSAL STRICT DOMINANCE OVER SMAGLOY–WELTER–WACHTER-ZEH–YAAKOBI (2023)**: For EVERY alphabet size `q ≥ 2` and EVERY block length `n ≥ 6` (with ZERO exceptions and without Smagloy et al.'s `q ≤ n` restriction!): `dsUpperBound q n < smagloyBound q n`! -/ theorem dsUpperBound_lt_smagloyBound {q n : ℕ} (hq : 2 ≤ q) (hn : 6 ≤ n) : dsUpperBound q n < smagloyBound q n := by unfold dsUpperBound smagloyBound let qR : ℝ := (q : ℝ) let p : ℝ := qR - 1 let nR : ℝ := (n : ℝ) let A : ℝ := p * (nR - 2) - 1 have hq2 : 2 ≤ qR := by dsimp [qR]; exact_mod_cast hq have hn6 : 6 ≤ nR := by dsimp [nR]; exact_mod_cast hn have hmain_c := dsUpperBound_main_coeff_le hq2 hn6 have htail := dsUpperBound_tail_lt hq hn have hqn_nn : 0 ≤ qR ^ n := by dsimp [qR]; positivity have hmain := mul_le_mul_of_nonneg_right hmain_c hqn_nn have hq_pow : qR ^ n = qR ^ (n - 1) * qR := by rw [← pow_succ]; congr 1; omega have hq_ne : qR ≠ 0 := by linarith have hL_rew : (1 + 2 * qR / (p * nR) + 18 * qR ^ 2 / (p ^ 2 * nR * (nR + 1))) / (A * p * (nR - 1)) * qR ^ n = qR ^ n / (A * p * (nR - 1)) * (1 + 2 * qR / (p * nR) + 18 * qR ^ 2 / (p ^ 2 * nR * (nR + 1))) := by ring have hR_rew : (4 / 5) * (3 / (qR * p * (nR - 5) * (nR - 3))) * qR ^ n = (4 / 5) * (3 * qR ^ (n - 1) / (p * (nR - 5) * (nR - 3))) := by rw [hq_pow] have hp_ne : p ≠ 0 := by dsimp [p]; linarith have h5_ne : nR - 5 ≠ 0 := by linarith have h3_ne : nR - 3 ≠ 0 := by linarith field_simp [hq_ne, hp_ne, h5_ne, h3_ne] rw [hL_rew, hR_rew] at hmain dsimp only [qR, p, nR, A] at hmain htail ⊢ linarith only [hmain, htail] /-- **COROLLARY**: Every `q`-ary single-deletion single-substitution correcting code `C ⊆ Word q n` (`q ≥ 2`, `n ≥ 6`) strictly beats Smagloy–Welter–Wachter-Zeh–Yaakobi (2023, Theorem 4)! -/ theorem qary_delSub_beats_smagloy_all {q m : ℕ} (hq : 2 ≤ q) (hm : 4 ≤ m) (C : Finset (Word q (m + 2))) (hC : IsDelSubCode C) : (C.card : ℝ) < smagloyBound q (m + 2) := lt_of_le_of_lt (qary_delSub_upper_bound hq (by omega) C hC) (dsUpperBound_lt_smagloyBound hq (by omega)) /-- **DNA STORAGE WORLD RECORD (`q = 4`) — STRICTLY LESS THAN HALF OF SMAGLOY ET AL. FOR ALL `n ≥ 6`**: For EVERY DNA block length `n ≥ 6` (`m ≥ 4`), EVERY 4-ary single-deletion single-substitution correcting code `C ⊆ Word 4 n` satisfies: `|C| ≤ dsUpperBound 4 n < (1 / 2) * smagloyBound 4 n`! -/ theorem dna_dsUpperBound_lt_half_smagloy {n : ℕ} (hn : 6 ≤ n) : dsUpperBound 4 n < (1 / 2) * smagloyBound 4 n := by by_cases hn11 : 11 ≤ n · unfold dsUpperBound smagloyBound let nR : ℝ := (n : ℝ) have hn11R : 11 ≤ nR := by dsimp [nR]; exact_mod_cast hn11 have hn6R : 6 ≤ nR := by linarith -- (A) Main coefficient ≤ (9 / 20) of Smagloy's main coefficient for all `n ≥ 6`! have hmain_c : (1 + 2 * (4 : ℝ) / (3 * nR) + 18 * (4 : ℝ) ^ 2 / (3 ^ 2 * nR * (nR + 1))) / ((3 * (nR - 2) - 1) * 3 * (nR - 1)) ≤ (9 / 20) * (3 / (4 * 3 * (nR - 5) * (nR - 3))) := by let b : ℝ := nR - 6 have hb : 0 ≤ b := by dsimp [b]; linarith have hn_b : nR = b + 6 := by dsimp [b]; ring have hA_pos : 0 < 3 * (nR - 2) - 1 := by linarith have hnm5 : 0 < nR - 5 := by linarith have hnm3 : 0 < nR - 3 := by linarith have hnm1 : 0 < nR - 1 := by linarith have hn0 : 0 < nR := by linarith have hnp1 : 0 < nR + 1 := by linarith have hden : 0 < 20 * 4 * (3 * (nR - 2) - 1) * 27 * (nR - 5) * (nR - 3) * (nR - 1) * nR * (nR + 1) := by positivity set L := (1 + 2 * (4 : ℝ) / (3 * nR) + 18 * (4 : ℝ) ^ 2 / (3 ^ 2 * nR * (nR + 1))) / ((3 * (nR - 2) - 1) * 3 * (nR - 1)) set R := (9 / 20) * (3 / (4 * 3 * (nR - 5) * (nR - 3))) have hnum : 20 * 4 * (3 * (nR - 2) - 1) * 27 * (nR - 5) * (nR - 3) * (nR - 1) * nR * (nR + 1) * (R - L) = 361170 + 138381 * b + 12117 * b ^ 2 + 1635 * b ^ 3 + 9 * b ^ 4 := by dsimp only [L, R] field_simp [hA_pos.ne', hnm5.ne', hnm3.ne', hnm1.ne', hn0.ne', hnp1.ne'] rw [hn_b]; ring have hpos : 0 ≤ 20 * 4 * (3 * (nR - 2) - 1) * 27 * (nR - 5) * (nR - 3) * (nR - 1) * nR * (nR + 1) * (R - L) := by rw [hnum]; positivity have hdiff := nonneg_of_mul_nonneg_right hpos hden linarith only [hdiff] -- (B) Tail < (1 / 20) * Smagloy's main term + 10 for all `n ≥ 11`! have htail : (4 : ℝ) * (3 * (nR - 2) + 2) / 2 < (1 / 20) * (3 * (4 : ℝ) ^ (n - 1) / (3 * (nR - 5) * (nR - 3))) + 10 := by have h4_split : (4 : ℝ) ^ (n - 1) = 4 * ((3 : ℝ) + 1) ^ (n - 2) := by have h31 : (3 : ℝ) + 1 = 4 := by norm_num rw [h31, mul_comm, ← pow_succ]; congr 1; omega have hdeg := add_pow_ge_deg234 (p := (3 : ℝ)) (by norm_num) (show 4 ≤ n - 2 by omega) have hcn2 : ((n - 2 : ℕ) : ℝ) = nR - 2 := by rw [Nat.cast_sub (by omega : 2 ≤ n), Nat.cast_ofNat] rw [hcn2] at hdeg let b : ℝ := nR - 11 have hb : 0 ≤ b := by dsimp [b]; linarith have hn_b : nR = b + 11 := by dsimp [b]; ring have hnm5 : 0 < nR - 5 := by linarith have hnm3 : 0 < nR - 3 := by linarith have hpoly : 0 < 4 * (nR - 2) * (108 + 108 * (nR - 4) + 81 * (nR - 4) * (nR - 5)) - 2880 * (nR - 5) * (nR - 3) := by have hid : 4 * (nR - 2) * (108 + 108 * (nR - 4) + 81 * (nR - 4) * (nR - 5)) - 2880 * (nR - 5) * (nR - 3) = 15336 + 18540 * b + 4680 * b ^ 2 + 324 * b ^ 3 := by rw [hn_b]; ring rw [hid]; positivity have hden : 0 < 480 * (nR - 5) * (nR - 3) := by positivity have hstep1 : (4 : ℝ) * (3 * (nR - 2) + 2) / 2 - 10 < (1 / 20) * (4 * ((nR - 2) * (nR - 3) / 2 * 3 ^ 2 + (nR - 2) * (nR - 3) * (nR - 4) / 6 * 3 ^ 3 + (nR - 2) * (nR - 3) * (nR - 4) * (nR - 5) / 24 * 3 ^ 4)) / ((nR - 5) * (nR - 3)) := by have hmul_id : 480 * (nR - 5) * (nR - 3) * ((1 / 20) * (4 * ((nR - 2) * (nR - 3) / 2 * 3 ^ 2 + (nR - 2) * (nR - 3) * (nR - 4) / 6 * 3 ^ 3 + (nR - 2) * (nR - 3) * (nR - 4) * (nR - 5) / 24 * 3 ^ 4)) / ((nR - 5) * (nR - 3)) - ((4 : ℝ) * (3 * (nR - 2) + 2) / 2 - 10)) = (nR - 3) * (4 * (nR - 2) * (108 + 108 * (nR - 4) + 81 * (nR - 4) * (nR - 5)) - 2880 * (nR - 5) * (nR - 3)) := by field_simp [hnm5.ne', hnm3.ne']; ring have hpos : 0 < 480 * (nR - 5) * (nR - 3) * ((1 / 20) * (4 * ((nR - 2) * (nR - 3) / 2 * 3 ^ 2 + (nR - 2) * (nR - 3) * (nR - 4) / 6 * 3 ^ 3 + (nR - 2) * (nR - 3) * (nR - 4) * (nR - 5) / 24 * 3 ^ 4)) / ((nR - 5) * (nR - 3)) - ((4 : ℝ) * (3 * (nR - 2) + 2) / 2 - 10)) := by rw [hmul_id]; exact mul_pos hnm3 hpoly have hdiff := pos_of_mul_pos_right hpos hden.le linarith only [hdiff] have hstep2 : (1 / 20) * (4 * ((nR - 2) * (nR - 3) / 2 * 3 ^ 2 + (nR - 2) * (nR - 3) * (nR - 4) / 6 * 3 ^ 3 + (nR - 2) * (nR - 3) * (nR - 4) * (nR - 5) / 24 * 3 ^ 4)) / ((nR - 5) * (nR - 3)) ≤ (1 / 20) * (3 * (4 : ℝ) ^ (n - 1) / (3 * (nR - 5) * (nR - 3))) := by rw [h4_split] have hrew_r : (1 / 20) * (3 * (4 * ((3 : ℝ) + 1) ^ (n - 2)) / (3 * (nR - 5) * (nR - 3))) = (1 / 20) * (4 * ((3 : ℝ) + 1) ^ (n - 2)) / ((nR - 5) * (nR - 3)) := by field_simp [hnm5.ne', hnm3.ne'] rw [hrew_r] refine div_le_div_of_nonneg_right (mul_le_mul_of_nonneg_left (mul_le_mul_of_nonneg_left ?_ (by norm_num)) (by norm_num)) (by positivity) have hrew : (nR - 2) * (nR - 3) / 2 * (3 : ℝ) ^ 2 + (nR - 2) * (nR - 3) * (nR - 4) / 6 * 3 ^ 3 + (nR - 2) * (nR - 3) * (nR - 4) * (nR - 5) / 24 * 3 ^ 4 = (nR - 2) * (nR - 2 - 1) / 2 * 3 ^ 2 + (nR - 2) * (nR - 2 - 1) * (nR - 2 - 2) / 6 * 3 ^ 3 + (nR - 2) * (nR - 2 - 1) * (nR - 2 - 2) * (nR - 2 - 3) / 24 * 3 ^ 4 := by ring rw [hrew]; exact hdeg linarith only [hstep1, hstep2] have h4n_nn : (0 : ℝ) ≤ (4 : ℝ) ^ n := by positivity have hmain := mul_le_mul_of_nonneg_right hmain_c h4n_nn have h4_pow : (4 : ℝ) ^ n = (4 : ℝ) ^ (n - 1) * 4 := by rw [← pow_succ]; congr 1; omega have hL_rew : (1 + 2 * (4 : ℝ) / (3 * nR) + 18 * (4 : ℝ) ^ 2 / (3 ^ 2 * nR * (nR + 1))) / ((3 * (nR - 2) - 1) * 3 * (nR - 1)) * (4 : ℝ) ^ n = (4 : ℝ) ^ n / ((3 * (nR - 2) - 1) * 3 * (nR - 1)) * (1 + 2 * (4 : ℝ) / (3 * nR) + 18 * (4 : ℝ) ^ 2 / (3 ^ 2 * nR * (nR + 1))) := by ring have hR_rew : (9 / 20) * (3 / (4 * 3 * (nR - 5) * (nR - 3))) * (4 : ℝ) ^ n = (9 / 20) * (3 * (4 : ℝ) ^ (n - 1) / (3 * (nR - 5) * (nR - 3))) := by rw [h4_pow] have h5 : nR - 5 ≠ 0 := by linarith have h3 : nR - 3 ≠ 0 := by linarith field_simp [h5, h3] rw [hL_rew, hR_rew] at hmain have hcast4 : ((4 : ℕ) : ℝ) = 4 := by norm_num have hsub3 : (4 : ℝ) - 1 = 3 := by norm_num simp only [hcast4, hsub3] dsimp only [nR] at hmain htail ⊢ linarith only [hmain, htail] · -- Finite check `n ∈ {6, 7, 8, 9, 10}`! have hcases : n = 6 ∨ n = 7 ∨ n = 8 ∨ n = 9 ∨ n = 10 := by omega rcases hcases with rfl | rfl | rfl | rfl | rfl <;> (unfold dsUpperBound smagloyBound; norm_num) /-- **DNA COROLLARY**: Every 4-ary single-deletion single-substitution code of length `n ≥ 6` has cardinality strictly less than HALF of Smagloy–Welter–Wachter-Zeh–Yaakobi (2023, Theorem 4)! -/ theorem dna_delSub_beats_half_smagloy {m : ℕ} (hm : 4 ≤ m) (C : Finset (Word 4 (m + 2))) (hC : IsDelSubCode C) : (C.card : ℝ) < (1 / 2) * smagloyBound 4 (m + 2) := lt_of_le_of_lt (qary_delSub_upper_bound (by omega) (by omega) C hC) (dna_dsUpperBound_lt_half_smagloy (by omega)) /-- Helper: `100 * n^3 < 2^n` for all `n ≥ 20`. -/ private lemma hundred_mul_cube_lt_two_pow : ∀ n : ℕ, 20 ≤ n → (100 * n ^ 3 : ℝ) < (2 : ℝ) ^ n := by intro n hn induction n, hn using Nat.le_induction with | base => norm_num | succ k hk ih => have hkR : (20 : ℝ) ≤ (k : ℝ) := by exact_mod_cast hk have hstep : 100 * ((k : ℝ) + 1) ^ 3 ≤ 2 * (100 * (k : ℝ) ^ 3) := by have : 100 * ((k : ℝ) ^ 3 - 3 * (k : ℝ) ^ 2 - 3 * (k : ℝ) - 1) ≥ 0 := by nlinarith linarith have hcast_k : (((k + 1 : ℕ) : ℝ) ^ 3) = ((k : ℝ) + 1) ^ 3 := by push_cast; rfl rw [hcast_k, pow_succ (2 : ℝ) k] linarith /-- **BINARY SPECIALIZATION (`q = 2`) — BEATING THE `1.25 * 2^n / n^2` BARRIER (`n ≥ 43`)**: For EVERY `n ≥ 43` and EVERY binary single-deletion single-substitution code `C ⊆ Word 2 n`: `|C| < (5 / 4) * 2^n / n^2` strictly (whereas Smagloy et al. Theorem 4 is `> (3 / 2) * 2^n / n^2` for EVERY `n ≥ 6`!)! -/ theorem binary_delSub_lt_five_fourths {m : ℕ} (hm : 41 ≤ m) (C : Finset (Word 2 (m + 2))) (hC : IsDelSubCode C) : let n : ℕ := m + 2 (C.card : ℝ) < (5 / 4) * (2 : ℝ) ^ n / (n : ℝ) ^ 2 := by intro n refine lt_of_le_of_lt (qary_delSub_upper_bound (by omega) (by omega) C hC) ?_ by_cases hn45 : 45 ≤ n · unfold dsUpperBound have hcast2 : ((2 : ℕ) : ℝ) = 2 := by norm_num have hsub1 : (2 : ℝ) - 1 = 1 := by norm_num simp only [hcast2, hsub1, one_mul, mul_one, one_pow] let nR : ℝ := (n : ℝ) have hn45R : 45 ≤ nR := by dsimp [nR]; exact_mod_cast hn45 let b : ℝ := nR - 45 have hb : 0 ≤ b := by dsimp [b]; linarith have hn_b : nR = b + 45 := by dsimp [b]; ring have hnm3 : 0 < nR - 3 := by linarith have hnm1 : 0 < nR - 1 := by linarith have hn0 : 0 < nR := by linarith have hnp1 : 0 < nR + 1 := by linarith -- Main coeff ≤ 31 / (25 * n^2)! have hmain_c : (1 + 2 * (2 : ℝ) / nR + 18 * (2 : ℝ) ^ 2 / (nR * (nR + 1))) / ((nR - 2 - 1) * (nR - 1)) ≤ (31 / 25) / nR ^ 2 := by have hden : 0 < 25 * (nR - 3) * (nR - 1) * nR ^ 2 * (nR + 1) := by positivity set L := (1 + 2 * (2 : ℝ) / nR + 18 * (2 : ℝ) ^ 2 / (nR * (nR + 1))) / ((nR - 2 - 1) * (nR - 1)) set R := (31 / 25) / nR ^ 2 have hnum : 25 * (nR - 3) * (nR - 1) * nR ^ 2 * (nR + 1) * (R - L) = 18498 + 14899 * b + 592 * b ^ 2 + 6 * b ^ 3 := by dsimp only [L, R] have h3' : nR - 2 - 1 = nR - 3 := by ring rw [h3'] field_simp [hnm3.ne', hnm1.ne', hn0.ne', hnp1.ne'] rw [hn_b]; ring have hpos : 0 ≤ 25 * (nR - 3) * (nR - 1) * nR ^ 2 * (nR + 1) * (R - L) := by rw [hnum]; positivity have hdiff := nonneg_of_mul_nonneg_right hpos hden linarith only [hdiff] have hmain := mul_le_mul_of_nonneg_right hmain_c (show (0 : ℝ) ≤ (2 : ℝ) ^ n by positivity) have hL_rew : (1 + 2 * (2 : ℝ) / nR + 18 * (2 : ℝ) ^ 2 / (nR * (nR + 1))) / ((nR - 2 - 1) * (nR - 1)) * (2 : ℝ) ^ n = (2 : ℝ) ^ n / ((nR - 2 - 1) * (nR - 1)) * (1 + 2 * 2 / nR + 18 * 2 ^ 2 / (nR * (nR + 1))) := by ring rw [hL_rew] at hmain -- Tail `2 * (n - 2 + 2) / 2 = n < (1 / 100) * 2^n / n^2` since `100 * n^3 < 2^n`! have htail : (2 : ℝ) * (nR - 2 + 2) / 2 < (1 / 100) * (2 : ℝ) ^ n / nR ^ 2 := by have h100 := hundred_mul_cube_lt_two_pow n (by omega) have hden : 0 < 100 * nR ^ 2 := by positivity have hrew : (2 : ℝ) * (nR - 2 + 2) / 2 = nR := by ring rw [hrew] rw [lt_div_iff₀ (by positivity : 0 < nR ^ 2)] dsimp only [nR] at h100 ⊢ linarith have hR_sum : (31 / 25) / nR ^ 2 * (2 : ℝ) ^ n + (1 / 100) * (2 : ℝ) ^ n / nR ^ 2 = (5 / 4) * (2 : ℝ) ^ n / nR ^ 2 := by ring dsimp only [nR] at hmain htail hR_sum ⊢ linarith only [hmain, htail, hR_sum] · have hcases : m = 41 ∨ m = 42 := by dsimp [n] at hn45; omega rcases hcases with rfl | rfl <;> (dsimp only [n]; unfold dsUpperBound; norm_num) /-- Helper: `n ^ 3 / q ^ n → 0` as `n → ∞` for any `q ≥ 2`. -/ private lemma tendsto_cube_div_pow {q : ℕ} (hq : 2 ≤ q) : Filter.Tendsto (fun n : ℕ => (n : ℝ) ^ 3 / (q : ℝ) ^ n) Filter.atTop (nhds 0) := by have h1 : (1 : ℝ) < (q : ℝ) := by have : (2 : ℝ) ≤ (q : ℝ) := by exact_mod_cast hq linarith simpa using tendsto_pow_const_div_const_pow_of_one_lt 3 h1 /-- **EXACT SPHERE-PACKING ASYMPTOTIC OPTIMALITY THEOREM**: For EVERY alphabet size `q ≥ 2`, our upper bound `dsUpperBound q n` achieves the exact sphere-packing leading constant `q^n / ((q - 1)^2 * n^2)`: `dsUpperBound q n / (q^n / ((q - 1)^2 * n^2)) → 1` as `n → ∞`! -/ theorem dsUpperBound_asymptotic_sphere_packing {q : ℕ} (hq : 2 ≤ q) : Filter.Tendsto (fun n : ℕ => dsUpperBound q n / ((q : ℝ) ^ n / (((q : ℝ) - 1) ^ 2 * (n : ℝ) ^ 2))) Filter.atTop (nhds 1) := by let qR : ℝ := (q : ℝ) let p : ℝ := qR - 1 have hq2 : 2 ≤ qR := by dsimp [qR]; exact_mod_cast hq have hp_pos : 0 < p := by dsimp [p]; linarith have hq_pos : 0 < qR := by linarith -- Rewrite the ratio for `n ≥ 4` as `f1 n * f2 n + f3 n` where `f1 → 1`, `f2 → 1`, `f3 → 0`! have hinv_n : Filter.Tendsto (fun n : ℕ => (1 : ℝ) / (n : ℝ)) Filter.atTop (nhds 0) := tendsto_one_div_atTop_nhds_zero_nat have hf1 : Filter.Tendsto (fun n : ℕ => (p * (n : ℝ) ^ 2) / ((p * ((n : ℝ) - 2) - 1) * ((n : ℝ) - 1))) Filter.atTop (nhds 1) := by have hrewrite : (fun n : ℕ => (p * (n : ℝ) ^ 2) / ((p * ((n : ℝ) - 2) - 1) * ((n : ℝ) - 1))) =ᶠ[Filter.atTop] (fun n : ℕ => p / ((p * (1 - 2 * (1 / (n : ℝ))) - 1 / (n : ℝ)) * (1 - 1 / (n : ℝ)))) := by filter_upwards [Filter.eventually_ge_atTop 4] with n hn have hn0 : (n : ℝ) ≠ 0 := by exact_mod_cast (show n ≠ 0 by omega) field_simp [hn0] refine Filter.Tendsto.congr' hrewrite.symm ?_ have hden : Filter.Tendsto (fun n : ℕ => (p * (1 - 2 * (1 / (n : ℝ))) - 1 / (n : ℝ)) * (1 - 1 / (n : ℝ))) Filter.atTop (nhds ((p * (1 - 2 * 0) - 0) * (1 - 0))) := ((tendsto_const_nhds.mul (tendsto_const_nhds.sub (tendsto_const_nhds.mul hinv_n))).sub hinv_n).mul (tendsto_const_nhds.sub hinv_n) have hlim := (tendsto_const_nhds (x := p)).div hden (by simp [hp_pos.ne']) change Filter.Tendsto (fun n : ℕ => p / ((p * (1 - 2 * (1 / (n : ℝ))) - 1 / (n : ℝ)) * (1 - 1 / (n : ℝ)))) Filter.atTop (nhds (p / ((p * (1 - 2 * 0) - 0) * (1 - 0)))) at hlim simpa [hp_pos.ne'] using hlim have hf2 : Filter.Tendsto (fun n : ℕ => 1 + 2 * qR / (p * (n : ℝ)) + 18 * qR ^ 2 / (p ^ 2 * (n : ℝ) * ((n : ℝ) + 1))) Filter.atTop (nhds 1) := by have ht1 : Filter.Tendsto (fun n : ℕ => 2 * qR / (p * (n : ℝ))) Filter.atTop (nhds 0) := by have hrew : (fun n : ℕ => 2 * qR / (p * (n : ℝ))) = fun n : ℕ => (2 * qR / p) * (1 / (n : ℝ)) := by ext n; rw [div_mul_div_comm, mul_one] rw [hrew]; simpa using tendsto_const_nhds.mul hinv_n have ht2 : Filter.Tendsto (fun n : ℕ => 18 * qR ^ 2 / (p ^ 2 * (n : ℝ) * ((n : ℝ) + 1))) Filter.atTop (nhds 0) := by refine squeeze_zero_norm' ?_ (by simpa using tendsto_const_nhds.mul hinv_n : Filter.Tendsto (fun n : ℕ => (18 * qR ^ 2 / p ^ 2) * (1 / (n : ℝ))) Filter.atTop (nhds 0)) filter_upwards [Filter.eventually_ge_atTop 1] with n hn have hn1 : (1 : ℝ) ≤ (n : ℝ) := by exact_mod_cast hn rw [Real.norm_of_nonneg (by positivity)] have hrew : (18 * qR ^ 2 / p ^ 2) * (1 / (n : ℝ)) = 18 * qR ^ 2 / (p ^ 2 * (n : ℝ)) := by rw [div_mul_div_comm, mul_one] rw [hrew] refine div_le_div_of_nonneg_left (by positivity) (by positivity) ?_ have hp2n : 0 < p ^ 2 * (n : ℝ) := by positivity nlinarith simpa using (tendsto_const_nhds.add ht1).add ht2 have hf3 : Filter.Tendsto (fun n : ℕ => (qR * (p * ((n : ℝ) - 2) + 2) / 2) * (p ^ 2 * (n : ℝ) ^ 2) / qR ^ n) Filter.atTop (nhds 0) := by have hcube := tendsto_cube_div_pow hq refine squeeze_zero_norm' ?_ (by simpa using tendsto_const_nhds.mul hcube : Filter.Tendsto (fun n : ℕ => (qR * p ^ 3) * ((n : ℝ) ^ 3 / qR ^ n)) Filter.atTop (nhds 0)) filter_upwards [Filter.eventually_ge_atTop 4] with n hn have hn4 : (4 : ℝ) ≤ (n : ℝ) := by exact_mod_cast hn have hnn : 0 ≤ (qR * (p * ((n : ℝ) - 2) + 2) / 2) * (p ^ 2 * (n : ℝ) ^ 2) / qR ^ n := by have : 0 ≤ (n : ℝ) - 2 := by linarith positivity rw [Real.norm_of_nonneg hnn] have hrew : (qR * p ^ 3) * ((n : ℝ) ^ 3 / qR ^ n) = (qR * p ^ 3 * (n : ℝ) ^ 3) / qR ^ n := by ring rw [hrew] refine div_le_div_of_nonneg_right ?_ (by positivity) have h1 : p * ((n : ℝ) - 2) + 2 ≤ 2 * p * (n : ℝ) := by have : (1 : ℝ) ≤ p := by dsimp [p]; linarith nlinarith have h2 : qR * (p * ((n : ℝ) - 2) + 2) / 2 ≤ qR * p * (n : ℝ) := by nlinarith nlinarith [mul_le_mul_of_nonneg_right h2 (show 0 ≤ p ^ 2 * (n : ℝ) ^ 2 by positivity)] have hmain_eq : (fun n : ℕ => dsUpperBound q n / (qR ^ n / (p ^ 2 * (n : ℝ) ^ 2))) =ᶠ[Filter.atTop] (fun n : ℕ => (p * (n : ℝ) ^ 2) / ((p * ((n : ℝ) - 2) - 1) * ((n : ℝ) - 1)) * (1 + 2 * qR / (p * (n : ℝ)) + 18 * qR ^ 2 / (p ^ 2 * (n : ℝ) * ((n : ℝ) + 1))) + (qR * (p * ((n : ℝ) - 2) + 2) / 2) * (p ^ 2 * (n : ℝ) ^ 2) / qR ^ n) := by filter_upwards [Filter.eventually_ge_atTop 4] with n hn have hn4 : (4 : ℝ) ≤ (n : ℝ) := by exact_mod_cast hn have hA_ne : p * ((n : ℝ) - 2) - 1 ≠ 0 := by nlinarith have hnm1 : (n : ℝ) - 1 ≠ 0 := by linarith have hn0 : (n : ℝ) ≠ 0 := by linarith have hnp1 : (n : ℝ) + 1 ≠ 0 := by linarith have hqn : qR ^ n ≠ 0 := pow_ne_zero n hq_pos.ne' unfold dsUpperBound dsimp only [qR, p] at hA_ne hqn hp_pos ⊢ field_simp [hA_ne, hp_pos.ne', hnm1, hn0, hnp1, hqn] refine Filter.Tendsto.congr' hmain_eq.symm ?_ simpa using (hf1.mul hf2).add hf3 /-- **EXACT ASYMPTOTIC IMPROVEMENT FACTOR OVER SMAGLOY ET AL. (2023)**: For EVERY alphabet size `q ≥ 2`, the ratio `smagloyBound q n / dsUpperBound q n` converges to `3 * (q - 1) / q` (i.e., `3/2 = 1.5×` for binary, `2×` for ternary, `9/4 = 2.25×` for DNA, and approaching `3×` as `q → ∞`)! -/ theorem smagloy_to_dsUpperBound_ratio_tendsto {q : ℕ} (hq : 2 ≤ q) : Filter.Tendsto (fun n : ℕ => smagloyBound q n / dsUpperBound q n) Filter.atTop (nhds (3 * ((q : ℝ) - 1) / (q : ℝ))) := by let qR : ℝ := (q : ℝ) let p : ℝ := qR - 1 have hq2 : 2 ≤ qR := by dsimp [qR]; exact_mod_cast hq have hp_pos : 0 < p := by dsimp [p]; linarith have hq_pos : 0 < qR := by linarith have hinv_n : Filter.Tendsto (fun n : ℕ => (1 : ℝ) / (n : ℝ)) Filter.atTop (nhds 0) := tendsto_one_div_atTop_nhds_zero_nat have hsmag : Filter.Tendsto (fun n : ℕ => smagloyBound q n / (qR ^ n / (p ^ 2 * (n : ℝ) ^ 2))) Filter.atTop (nhds (3 * p / qR)) := by have hterm1 : Filter.Tendsto (fun n : ℕ => (3 * p / qR) * ((n : ℝ) ^ 2 / (((n : ℝ) - 5) * ((n : ℝ) - 3)))) Filter.atTop (nhds (3 * p / qR)) := by have hrew : (fun n : ℕ => (n : ℝ) ^ 2 / (((n : ℝ) - 5) * ((n : ℝ) - 3))) =ᶠ[Filter.atTop] (fun n : ℕ => 1 / ((1 - 5 * (1 / (n : ℝ))) * (1 - 3 * (1 / (n : ℝ))))) := by filter_upwards [Filter.eventually_ge_atTop 6] with n hn have hn0 : (n : ℝ) ≠ 0 := by exact_mod_cast (show n ≠ 0 by omega) field_simp [hn0] have hden : Filter.Tendsto (fun n : ℕ => (1 - 5 * (1 / (n : ℝ))) * (1 - 3 * (1 / (n : ℝ)))) Filter.atTop (nhds ((1 - 5 * 0) * (1 - 3 * 0))) := (tendsto_const_nhds.sub (tendsto_const_nhds.mul hinv_n)).mul (tendsto_const_nhds.sub (tendsto_const_nhds.mul hinv_n)) have h1 : Filter.Tendsto (fun n : ℕ => (n : ℝ) ^ 2 / (((n : ℝ) - 5) * ((n : ℝ) - 3))) Filter.atTop (nhds 1) := Filter.Tendsto.congr' hrew.symm (by have hq1 := (tendsto_const_nhds (x := (1 : ℝ))).div hden (by norm_num) change Filter.Tendsto (fun n : ℕ => 1 / ((1 - 5 * (1 / (n : ℝ))) * (1 - 3 * (1 / (n : ℝ))))) Filter.atTop (nhds (1 / ((1 - 5 * 0) * (1 - 3 * 0)))) at hq1 simpa using hq1) simpa using tendsto_const_nhds.mul h1 have hterm2 : Filter.Tendsto (fun n : ℕ => (5 * qR * p ^ 2) * ((n : ℝ) ^ 2 / qR ^ n)) Filter.atTop (nhds 0) := by have h1 : (1 : ℝ) < qR := by linarith have hsq : Filter.Tendsto (fun n : ℕ => (n : ℝ) ^ 2 / qR ^ n) Filter.atTop (nhds 0) := by simpa using tendsto_pow_const_div_const_pow_of_one_lt 2 h1 simpa using tendsto_const_nhds.mul hsq have hrew_all : (fun n : ℕ => smagloyBound q n / (qR ^ n / (p ^ 2 * (n : ℝ) ^ 2))) =ᶠ[Filter.atTop] (fun n : ℕ => (3 * p / qR) * ((n : ℝ) ^ 2 / (((n : ℝ) - 5) * ((n : ℝ) - 3))) + (5 * qR * p ^ 2) * ((n : ℝ) ^ 2 / qR ^ n)) := by filter_upwards [Filter.eventually_ge_atTop 6] with n hn have hn6 : (6 : ℝ) ≤ (n : ℝ) := by exact_mod_cast hn have hq_pow : qR ^ n = qR ^ (n - 1) * qR := by rw [← pow_succ]; congr 1; omega have h5 : (n : ℝ) - 5 ≠ 0 := by linarith have h3 : (n : ℝ) - 3 ≠ 0 := by linarith have hn0 : (n : ℝ) ≠ 0 := by linarith have hqn : qR ^ n ≠ 0 := pow_ne_zero n hq_pos.ne' unfold smagloyBound dsimp only [qR, p] at hq_pow hqn hp_pos hq_pos ⊢ rw [hq_pow] at hqn ⊢ have hqn1 : (q : ℝ) ^ (n - 1) ≠ 0 := left_ne_zero_of_mul hqn field_simp [hp_pos.ne', hq_pos.ne', h5, h3, hn0, hqn1] refine Filter.Tendsto.congr' hrew_all.symm ?_ simpa using hterm1.add hterm2 have hds := dsUpperBound_asymptotic_sphere_packing hq have hdiv : Filter.Tendsto (fun n : ℕ => (smagloyBound q n / (qR ^ n / (p ^ 2 * (n : ℝ) ^ 2))) / (dsUpperBound q n / (qR ^ n / (p ^ 2 * (n : ℝ) ^ 2)))) Filter.atTop (nhds (3 * p / qR / 1)) := hsmag.div hds one_ne_zero have hrew : (fun n : ℕ => (smagloyBound q n / (qR ^ n / (p ^ 2 * (n : ℝ) ^ 2))) / (dsUpperBound q n / (qR ^ n / (p ^ 2 * (n : ℝ) ^ 2)))) =ᶠ[Filter.atTop] (fun n : ℕ => smagloyBound q n / dsUpperBound q n) := by filter_upwards [Filter.eventually_ge_atTop 1] with n hn have hn0 : (n : ℝ) ≠ 0 := by exact_mod_cast (show n ≠ 0 by omega) have hsp : qR ^ n / (p ^ 2 * (n : ℝ) ^ 2) ≠ 0 := by have : 0 < qR ^ n / (p ^ 2 * (n : ℝ) ^ 2) := by have : 0 < (n : ℝ) := by exact_mod_cast (show 0 < n by omega) positivity exact this.ne' exact div_div_div_cancel_right₀ hsp _ _ simpa [p, qR] using hdiv.congr' hrew end WorldRecordDominance /-! ## Section 6: Self-Contained Gilbert–Varshamov Lower Bound, Two-Sided Sandwich & Axiom Audit -/ section GilbertVarshamov /-- Every word `z : Word q m` lies in `delBall x` for at most `q * (m + 1)` words `x : Word q (m + 1)`! -/ lemma card_insBall_le {q m : ℕ} (z : Word q m) : (Finset.univ.filter fun x : Word q (m + 1) => z ∈ delBall x).card ≤ q * (m + 1) := by let f : Fin (m + 1) × Fin q → Word q (m + 1) := fun p => p.1.insertNth (α := fun _ => Fin q) p.2 z have hsub : (Finset.univ.filter fun x : Word q (m + 1) => z ∈ delBall x) ⊆ Finset.univ.image f := by intro x hx have hmem : z ∈ delBall x := (Finset.mem_filter.mp hx).2 rcases Finset.mem_image.mp hmem with ⟨i, _, hi⟩ refine Finset.mem_image.mpr ⟨(i, x i), Finset.mem_univ _, ?_⟩ funext k dsimp only [f] by_cases hk : k = i · rw [hk]; exact Fin.insertNth_apply_same (α := fun _ => Fin q) i (x i) z · obtain ⟨j, rfl⟩ := Fin.exists_succAbove_eq hk rw [Fin.insertNth_apply_succAbove (α := fun _ => Fin q) i (x i) z j, ← hi] rfl calc (Finset.univ.filter fun x : Word q (m + 1) => z ∈ delBall x).card ≤ (Finset.univ.image f).card := Finset.card_le_card hsub _ ≤ (Finset.univ : Finset (Fin (m + 1) × Fin q)).card := Finset.card_image_le _ = q * (m + 1) := by simp [Fintype.card_prod, mul_comm] /-- Every word `y : Word q m` lies in `delSubBall x` for at most `((q - 1) * m + 1) * (q * (m + 1))` words `x : Word q (m + 1)`! -/ lemma card_invDelSubBall_le {q m : ℕ} (hq : 1 ≤ q) (y : Word q m) : (Finset.univ.filter fun x : Word q (m + 1) => y ∈ delSubBall x).card ≤ ((q - 1) * m + 1) * (q * (m + 1)) := by have hsub : (Finset.univ.filter fun x : Word q (m + 1) => y ∈ delSubBall x) ⊆ (hammingBall1 y).biUnion fun z => Finset.univ.filter fun x : Word q (m + 1) => z ∈ delBall x := by intro x hx have hmem : y ∈ delSubBall x := (Finset.mem_filter.mp hx).2 rcases Finset.mem_biUnion.mp hmem with ⟨z, hz, hyz⟩ have hzy : z ∈ hammingBall1 y := by refine Finset.mem_filter.mpr ⟨Finset.mem_univ _, ?_⟩ rw [hammingDist_symm]; exact (Finset.mem_filter.mp hyz).2 exact Finset.mem_biUnion.mpr ⟨z, hzy, Finset.mem_filter.mpr ⟨Finset.mem_univ _, hz⟩⟩ calc (Finset.univ.filter fun x : Word q (m + 1) => y ∈ delSubBall x).card ≤ ((hammingBall1 y).biUnion fun z => Finset.univ.filter fun x : Word q (m + 1) => z ∈ delBall x).card := Finset.card_le_card hsub _ ≤ ∑ z ∈ hammingBall1 y, (Finset.univ.filter fun x : Word q (m + 1) => z ∈ delBall x).card := Finset.card_biUnion_le _ ≤ ∑ _z ∈ hammingBall1 y, (q * (m + 1)) := Finset.sum_le_sum fun z _ => card_insBall_le z _ = ((q - 1) * m + 1) * (q * (m + 1)) := by rw [Finset.sum_const, smul_eq_mul, card_hammingBall1 hq y] /-- Uniform upper bound on the conflict neighborhood size `|{x' | B_{1,1}(x) ∩ B_{1,1}(x') ≠ ∅}|` for any `x : Word q (m + 2)` (`q ≥ 2`, `m ≥ 2`). -/ def gvConflictBound (q n : ℕ) : ℕ := (n * ((q - 1) * (n - 2) - 1) + (q + 2)) * (((q - 1) * (n - 1) + 1) * (q * n)) lemma card_conflict_le {q m : ℕ} (hq : 2 ≤ q) (hm : 2 ≤ m) (x : Word q (m + 2)) : (Finset.univ.filter fun x' : Word q (m + 2) => ¬ Disjoint (delSubBall x) (delSubBall x')).card ≤ gvConflictBound q (m + 2) := by have hsub : (Finset.univ.filter fun x' : Word q (m + 2) => ¬ Disjoint (delSubBall x) (delSubBall x')) ⊆ (delSubBall x).biUnion fun y => Finset.univ.filter fun x' : Word q (m + 2) => y ∈ delSubBall x' := by intro x' hx' have hnd := (Finset.mem_filter.mp hx').2 rw [Finset.not_disjoint_iff] at hnd rcases hnd with ⟨y, hyx, hyx'⟩ exact Finset.mem_biUnion.mpr ⟨y, hyx, Finset.mem_filter.mpr ⟨Finset.mem_univ _, hyx'⟩⟩ have hB_le : (delSubBall x).card ≤ (m + 2) * ((q - 1) * m - 1) + (q + 2) := by rw [card_delSubBall hq (by omega) x] simp only [Nat.add_sub_cancel] split_ifs with hr1 · have hqm : 2 ≤ (q - 1) * m := by have : 1 ≤ q - 1 := by omega nlinarith have h1 : 2 * ((q - 1) * m - 1) ≤ (m + 2) * ((q - 1) * m - 1) := Nat.mul_le_mul_right _ (by omega) have h2 : (q - 1) * (m + 1) = (q - 1) * m + (q - 1) := by ring omega · have hrc := runCount_le_len x exact Nat.add_le_add_right (Nat.mul_le_mul_right _ hrc) _ calc (Finset.univ.filter fun x' : Word q (m + 2) => ¬ Disjoint (delSubBall x) (delSubBall x')).card ≤ ((delSubBall x).biUnion fun y => Finset.univ.filter fun x' : Word q (m + 2) => y ∈ delSubBall x').card := Finset.card_le_card hsub _ ≤ ∑ y ∈ delSubBall x, (Finset.univ.filter fun x' : Word q (m + 2) => y ∈ delSubBall x').card := Finset.card_biUnion_le _ ≤ ∑ _y ∈ delSubBall x, (((q - 1) * (m + 1) + 1) * (q * (m + 2))) := Finset.sum_le_sum fun y _ => card_invDelSubBall_le (by omega) y _ = (delSubBall x).card * (((q - 1) * (m + 1) + 1) * (q * (m + 2))) := by rw [Finset.sum_const, smul_eq_mul] _ ≤ gvConflictBound q (m + 2) := by unfold gvConflictBound simp only [show m + 2 - 2 = m from rfl] exact Nat.mul_le_mul_right _ hB_le /-- **SELF-CONTAINED GILBERT–VARSHAMOV EXISTENCE LOWER BOUND**: For EVERY `q ≥ 2` and EVERY `n = m + 2 ≥ 4` (`m ≥ 2`), there exists a `q`-ary single-deletion single-substitution correcting code `C₀ ⊆ Word q n` with `q^n / gvConflictBound q n ≤ C₀.card`! -/ theorem exists_delSubCode_gv {q m : ℕ} (hq : 2 ≤ q) (hm : 2 ≤ m) : ∃ C₀ : Finset (Word q (m + 2)), IsDelSubCode C₀ ∧ (q : ℝ) ^ (m + 2) / (gvConflictBound q (m + 2) : ℝ) ≤ (C₀.card : ℝ) := by classical -- Choose a maximum-cardinality `IsDelSubCode` `C₀`! let valid : Finset (Finset (Word q (m + 2))) := Finset.univ.filter fun C => IsDelSubCode C have hvalid_ne : valid.Nonempty := ⟨∅, Finset.mem_filter.mpr ⟨Finset.mem_univ _, fun _ h => by simp at h⟩⟩ obtain ⟨C₀, hC₀_mem, hC₀_max⟩ := Finset.exists_max_image valid Finset.card hvalid_ne have hC₀ : IsDelSubCode C₀ := (Finset.mem_filter.mp hC₀_mem).2 refine ⟨C₀, hC₀, ?_⟩ -- Maximality implies every `x' : Word q (m + 2)` conflicts with some `x ∈ C₀`! have hcover : (Finset.univ : Finset (Word q (m + 2))) ⊆ C₀.biUnion fun x => Finset.univ.filter fun x' => ¬ Disjoint (delSubBall x) (delSubBall x') := by intro x' _ by_contra hnot simp only [Finset.mem_biUnion, Finset.mem_filter, Finset.mem_univ, true_and, not_exists, not_and, not_not] at hnot have hx'_not : x' ∉ C₀ := by intro hin have hdisj := hnot x' hin have hne : (delSubBall x').Nonempty := by have hpos : 0 < (delSubBall x').card := by rw [card_delSubBall hq (by omega) x'] split_ifs <;> omega exact Finset.card_pos.mp hpos rcases hne with ⟨y, hy⟩ exact Finset.disjoint_left.mp hdisj hy hy have hC1 : IsDelSubCode (insert x' C₀) := by intro a ha b hb hab simp only [Finset.mem_insert] at ha hb rcases ha with rfl | ha <;> rcases hb with rfl | hb · exact absurd rfl hab · exact (hnot b hb).symm · exact hnot a ha · exact hC₀ a ha b hb hab have hmem1 : insert x' C₀ ∈ valid := Finset.mem_filter.mpr ⟨Finset.mem_univ _, hC1⟩ have hle := hC₀_max (insert x' C₀) hmem1 rw [Finset.card_insert_of_notMem hx'_not] at hle omega have hcard_nat : q ^ (m + 2) ≤ C₀.card * gvConflictBound q (m + 2) := by calc q ^ (m + 2) = (Finset.univ : Finset (Word q (m + 2))).card := by rw [Finset.card_univ, Fintype.card_fun, Fintype.card_fin, Fintype.card_fin] _ ≤ (C₀.biUnion fun x => Finset.univ.filter fun x' => ¬ Disjoint (delSubBall x) (delSubBall x')).card := Finset.card_le_card hcover _ ≤ ∑ x ∈ C₀, (Finset.univ.filter fun x' => ¬ Disjoint (delSubBall x) (delSubBall x')).card := Finset.card_biUnion_le _ ≤ ∑ _x ∈ C₀, gvConflictBound q (m + 2) := Finset.sum_le_sum fun x _ => card_conflict_le hq hm x _ = C₀.card * gvConflictBound q (m + 2) := by rw [Finset.sum_const, smul_eq_mul] have hcardR : (q : ℝ) ^ (m + 2) ≤ (C₀.card : ℝ) * (gvConflictBound q (m + 2) : ℝ) := by exact_mod_cast hcard_nat have hgv_pos : (0 : ℝ) < (gvConflictBound q (m + 2) : ℝ) := by unfold gvConflictBound; positivity rwa [div_le_iff₀ hgv_pos] /-- **COMPLETE TWO-SIDED NON-ASYMPTOTIC SANDWICH FOR `q`-ARY SINGLE-DELETION SINGLE-SUBSTITUTION CODES**: For EVERY `q ≥ 2` and EVERY `n = m + 2 ≥ 4` (`m ≥ 2`): 1. There exists a code `C₀ ⊆ Word q n` with `q^n / gvConflictBound q n ≤ C₀.card`, AND 2. EVERY code `C ⊆ Word q n` satisfies `C.card ≤ dsUpperBound q n` (and strictly `< smagloyBound q n` whenever `n ≥ 6`)! -/ theorem qary_delSub_two_sided_sandwich {q m : ℕ} (hq : 2 ≤ q) (hm : 2 ≤ m) : let n : ℕ := m + 2 (∃ C₀ : Finset (Word q n), IsDelSubCode C₀ ∧ (q : ℝ) ^ n / (gvConflictBound q n : ℝ) ≤ (C₀.card : ℝ)) ∧ (∀ C : Finset (Word q n), IsDelSubCode C → (C.card : ℝ) ≤ dsUpperBound q n) := ⟨exists_delSubCode_gv hq hm, fun C hC => qary_delSub_upper_bound hq hm C hC⟩ #print axioms delSubCode_card_le_dual_sum #print axioms hammingDist_deleteAt_orderEmb #print axioms card_hammingBall1 #print axioms card_inter_hammingBall1_dist_one #print axioms card_inter_hammingBall1_dist_two_sdiff_mid #print axioms card_delSubBall #print axioms runCount_deleteAt_le #print axioms runCount_le_add_two_of_hammingDist_le_one #print axioms runCount_le_add_two_of_mem_delSubBall #print axioms dsDualWeight_nonneg #print axioms dsDualWeight_feasible #print axioms delSubCode_card_le_dsDualWeight_sum #print axioms card_words_with_boundarySet #print axioms card_words_with_runCount #print axioms inv_sub_two_le #print axioms dsDualWeight_sum_le_dsUpperBound #print axioms qary_delSub_upper_bound #print axioms dsUpperBound_lt_smagloyBound #print axioms qary_delSub_beats_smagloy_all #print axioms dna_dsUpperBound_lt_half_smagloy #print axioms dna_delSub_beats_half_smagloy #print axioms binary_delSub_lt_five_fourths #print axioms dsUpperBound_asymptotic_sphere_packing #print axioms smagloy_to_dsUpperBound_ratio_tendsto #print axioms exists_delSubCode_gv #print axioms qary_delSub_two_sided_sandwich end GilbertVarshamov end DelSubUpperBound