import Mathlib.InformationTheory.Hamming import Mathlib.Data.Nat.Choose.Sum import Mathlib.Data.Nat.Choose.Central import Mathlib.Data.Fintype.Powerset import Mathlib.Tactic /-! # Exact Non-Asymptotic Geometry, Capacity Ceilings, Curvature Reversal, # and Phase Transitions of Kanerva's Discrete Sparse Distributed Memory This file establishes a self-contained, non-asymptotic deterministic theory of Pentti Kanerva's Discrete Sparse Distributed Memory (SDM) over the binary hypercube `Word n := Fin n → Bool` equipped with Hamming distance `hammingDist`: 1. **Exact Counter-to-Kernel Equivalence & Diagonal Dominance**: Kanerva's physical hard-location write/read counter sum equals the Hamming-ball overlap kernel sum, and universal exact 1-step heteroassociative retrieval is equivalent to strict diagonal dominance of the overlap Gram matrix. 2. **Exact Combinatorial Geometry of the Full-Cube Overlap Kernel `I_n(r, d)`**: - Exact Parity-Plateau Law: `I_n(r, 2m + 1) = I_n(r, 2m + 2)`. - Exact Central-Binomial Step-Drop: `I_n(r, 2m) = I_n(r, 2m + 1) + C(2m, m) * C(n - 2m - 1, r - m)`. - Exact Single-Sum Representation & Finite Support (`I_n(r, d) = 0 ↔ d > 2r`). - Exact Complement Duality: `I_n(r, d) + 2|B_{n-1-r}| = 2^n + I_n(n - 1 - r, d)`. 3. **Exact Super-Exponential Step-Contraction & Matching Decay Exponent**: For all `2r < n` and `m ≤ r`, `((n-2m-1)^2 + 2(n-2r) - 1) * I_n(r, 2m+2) ≤ 4(r-m)(n-r-m-1) * I_n(r, 2m)`, yielding global exponential decay `(4p(1-p))^⌈d/2⌉` with constant `1`, paired with a matching non-asymptotic lower bound proving `4p(1-p)` is exact. 4. **Sharp Structural Separation from Transformer Softmax Attention**: - Curvature Reversal: strictly log-concave at `r = 2` (`n ≥ 5`), strictly log-convex at short distances (`I(2)^2 < I(0) * I(4)`) for EVERY `3 ≤ r < n/2`, and strictly log-concave at `d = 2r`. - Parity-Deadlock Voronoi Basin Dichotomy: `Δ = 4m + 1` achieves full Voronoi radius `2m`, whereas `Δ = 4m + 3` suffers exact zero-margin deadlock on the outer Voronoi shell for ALL `r`. 5. **Exact Universal Hard-Location Capacity Ceiling (`M(s̄ + γ) ≤ 2H`) & Tightness**: Exact Margin-Load Conservation Law `∑_μ margin(μ) = ∑_{a ∈ A} d(a)(2 - d(a))`, yielding `∑_μ |B_{A,r}(x_μ)| + Mγ ≤ 2H` and `Mγ ≤ H`, proved 100% tight for every `M ≥ 1` via an explicit Hamming-ball construction in `{0, 1}^M` at `r = 1`, plus the exact blind-spot covering tradeoff. 6. **Sharp Critical Access-Radius Phase Transition at `r_c = n/2`**: Exponential deterministic capacity & macroscopic 1-step basins with Gilbert-Varshamov existence for `r < n/2`, collapsing abruptly to `M ≤ 3` the instant `2r ≥ n - 1` (attained at `n = 5, r = 2`), `M ≤ 2` once `3|B_r| ≥ 2·2^n`, and `M ≤ 1` at `r = n`. 7. **Exact Spurious 3-Mixture Attractor Phase Transition in Autoassociative SDM**: Exact fixed-point threshold `2r ≥ d`, exact IFF 1-bit basin criterion `I_n(r, d-1) < 2·I_n(r, d+1)`, universal destabilization whenever `8r(n-r) ≤ n^2`, and an explicit subcritical stable witness. -/ open Finset BigOperators namespace KanervaSDM /-! ## Section 1: Core Kanerva Discrete SDM Definitions & Counter-to-Kernel Equivalence -/ /-- Binary words of length `n` forming the discrete hypercube `{0, 1}^n`. -/ abbrev Word (n : ℕ) := Fin n → Bool /-- Bipolar encoding `Bool → {-1, +1}` used in Kanerva SDM counters and read-outs. -/ def bipolar (b : Bool) : ℤ := if b then 1 else -1 lemma bipolar_mul_self (b : Bool) : bipolar b * bipolar b = 1 := by cases b <;> rfl lemma bipolar_not (b : Bool) : bipolar (!b) = -bipolar b := by cases b <;> rfl /-- Hard locations in `A` activated by address `x` within Hamming access radius `r`. -/ def activeSet {n : ℕ} (A : Finset (Word n)) (r : ℕ) (x : Word n) : Finset (Word n) := A.filter (fun a => hammingDist x a ≤ r) /-- Exact Kanerva SDM overlap kernel: number of shared active hard locations in `A`. -/ def overlap {n : ℕ} (A : Finset (Word n)) (r : ℕ) (x y : Word n) : ℕ := (activeSet A r x ∩ activeSet A r y).card /-- Counter value stored at hard location `a` on payload coordinate `j`. -/ def sdmCounter {n M k : ℕ} (A : Finset (Word n)) (r : ℕ) (X : Fin M → Word n) (V : Fin M → Word k) (a : Word n) (j : Fin k) : ℤ := ∑ μ : Fin M, if a ∈ activeSet A r (X μ) then bipolar (V μ j) else 0 /-- Kanerva SDM read-out pre-activation at query `ξ` on payload coordinate `j`. -/ def sdmRead {n M k : ℕ} (A : Finset (Word n)) (r : ℕ) (X : Fin M → Word n) (V : Fin M → Word k) (ξ : Word n) (j : Fin k) : ℤ := ∑ a ∈ activeSet A r ξ, sdmCounter A r X V a j /-- Strict 1-step exact retrieval of target payload `v` from query `ξ`. -/ def StrictRetrieves {n M k : ℕ} (A : Finset (Word n)) (r : ℕ) (X : Fin M → Word n) (V : Fin M → Word k) (ξ : Word n) (v : Word k) : Prop := ∀ j : Fin k, 0 < bipolar (v j) * sdmRead A r X V ξ j /-- Universal exact 1-step heteroassociative retrieval across all payload assignments `V`. -/ def UniversalRetrievable {n M : ℕ} (A : Finset (Word n)) (r : ℕ) (X : Fin M → Word n) (k : ℕ) : Prop := ∀ (V : Fin M → Word k) (μ : Fin M), StrictRetrieves A r X V (X μ) (V μ) /-- Strict diagonal dominance of the Kanerva overlap Gram matrix with integer margin `γ`. -/ def StrictDiagDom {n M : ℕ} (A : Finset (Word n)) (r : ℕ) (X : Fin M → Word n) (γ : ℕ) : Prop := ∀ μ : Fin M, (∑ ν ∈ Finset.univ.erase μ, overlap A r (X μ) (X ν)) + γ ≤ overlap A r (X μ) (X μ) /-- Write-load of hard location `a`: number of stored memories whose radius-`r` ball covers `a`. -/ def writeLoad {n M : ℕ} (A : Finset (Word n)) (r : ℕ) (X : Fin M → Word n) (a : Word n) : ℕ := (Finset.univ.filter (fun μ : Fin M => a ∈ activeSet A r (X μ))).card lemma overlap_symm {n : ℕ} (A : Finset (Word n)) (r : ℕ) (x y : Word n) : overlap A r x y = overlap A r y x := by simp [overlap, Finset.inter_comm] lemma overlap_self {n : ℕ} (A : Finset (Word n)) (r : ℕ) (x : Word n) : overlap A r x x = (activeSet A r x).card := by simp [overlap] /-- **Theorem 0 (Exact Bilinear Counter-to-Kernel Equivalence)**: Kanerva's physical hard-location write/read counter sum equals the overlap-kernel weighted sum. -/ theorem sdmRead_eq_kernel_sum {n M k : ℕ} (A : Finset (Word n)) (r : ℕ) (X : Fin M → Word n) (V : Fin M → Word k) (ξ : Word n) (j : Fin k) : sdmRead A r X V ξ j = ∑ μ : Fin M, (overlap A r ξ (X μ) : ℤ) * bipolar (V μ j) := by unfold sdmRead sdmCounter rw [Finset.sum_comm] refine Finset.sum_congr rfl fun μ _ => ?_ rw [← Finset.sum_filter, Finset.sum_const, nsmul_eq_mul, Finset.filter_mem_eq_inter] rfl /-- **Theorem 1.1 (Exact Equivalence of Universal Heteroassociative Retrieval and Strict Diagonal Dominance)**: For any non-empty payload width `k ≥ 1`, an SDM configuration `(A, r, X)` universally retrieves every payload assignment `V : Fin M → Word k` in 1 step if and only if its overlap Gram matrix is strictly diagonally dominant (`γ = 1`). -/ theorem universalRetrievable_iff_strictDiagDom {n M k : ℕ} (hk : 0 < k) (A : Finset (Word n)) (r : ℕ) (X : Fin M → Word n) : UniversalRetrievable A r X k ↔ StrictDiagDom A r X 1 := by constructor · intro hU μ let j0 : Fin k := ⟨0, hk⟩ let V : Fin M → Word k := fun ν _ => decide (ν = μ) have hpos := hU V μ j0 rw [sdmRead_eq_kernel_sum] at hpos have hVμ : bipolar (V μ j0) = 1 := by simp [V, bipolar] have hsplit : (∑ ν : Fin M, (overlap A r (X μ) (X ν) : ℤ) * bipolar (V ν j0)) = (overlap A r (X μ) (X μ) : ℤ) - ∑ ν ∈ Finset.univ.erase μ, (overlap A r (X μ) (X ν) : ℤ) := by rw [← Finset.add_sum_erase _ _ (Finset.mem_univ μ), hVμ, mul_one, sub_eq_add_neg, ← Finset.sum_neg_distrib] congr 1 refine Finset.sum_congr rfl fun ν hν => ?_ have hne : ν ≠ μ := Finset.ne_of_mem_erase hν simp [V, hne, bipolar] rw [hVμ, one_mul, hsplit] at hpos have hcast : ((∑ ν ∈ Finset.univ.erase μ, overlap A r (X μ) (X ν) : ℕ) : ℤ) < (overlap A r (X μ) (X μ) : ℤ) := by push_cast linarith exact Nat.succ_le_of_lt (Int.ofNat_lt.mp hcast) · intro hDD V μ j rw [sdmRead_eq_kernel_sum, Finset.mul_sum, ← Finset.add_sum_erase _ _ (Finset.mem_univ μ)] have hself : bipolar (V μ j) * ((overlap A r (X μ) (X μ) : ℤ) * bipolar (V μ j)) = (overlap A r (X μ) (X μ) : ℤ) := by calc bipolar (V μ j) * ((overlap A r (X μ) (X μ) : ℤ) * bipolar (V μ j)) = (overlap A r (X μ) (X μ) : ℤ) * (bipolar (V μ j) * bipolar (V μ j)) := by ring _ = (overlap A r (X μ) (X μ) : ℤ) := by rw [bipolar_mul_self, mul_one] rw [hself] have hbound : -(∑ ν ∈ Finset.univ.erase μ, (overlap A r (X μ) (X ν) : ℤ)) ≤ ∑ ν ∈ Finset.univ.erase μ, bipolar (V μ j) * ((overlap A r (X μ) (X ν) : ℤ) * bipolar (V ν j)) := by rw [← Finset.sum_neg_distrib] refine Finset.sum_le_sum fun ν _ => ?_ have h1 : -1 ≤ bipolar (V μ j) * bipolar (V ν j) := by cases V μ j <;> cases V ν j <;> decide have h2 : (0 : ℤ) ≤ (overlap A r (X μ) (X ν) : ℤ) := Nat.cast_nonneg _ have h3 := mul_le_mul_of_nonneg_left h1 h2 linarith have hdd := hDD μ have hddZ : ((∑ ν ∈ Finset.univ.erase μ, overlap A r (X μ) (X ν) : ℕ) : ℤ) + 1 ≤ (overlap A r (X μ) (X μ) : ℤ) := by exact_mod_cast hdd push_cast at hddZ linarith /-! ## Section 2: Exact Combinatorial Geometry of the Full-Cube Kanerva Overlap Kernel -/ /-- Full-hypercube Hamming ball of radius `r` around `x`. -/ def fullBall (n r : ℕ) (x : Word n) : Finset (Word n) := activeSet Finset.univ r x /-- Full-hypercube Kanerva overlap `|B_r(x) ∩ B_r(y)|`. -/ def fullOverlap (n r : ℕ) (x y : Word n) : ℕ := overlap Finset.univ r x y /-- Volume of a Hamming ball of radius `r` in `{0, 1}^n`: `∑_{k=0}^r C(n, k)`. -/ def ballVol (n r : ℕ) : ℕ := ∑ k ∈ Finset.range (r + 1), n.choose k /-- Shifted Hamming-ball volume: `ballVol n (r - k)` if `k ≤ r`, else `0`. -/ def ballVolShift (n r k : ℕ) : ℕ := if k ≤ r then ballVol n (r - k) else 0 /-- Closed-form Hamming-ball intersection count `I_n(r, d)`. -/ def overlapFormula (n r d : ℕ) : ℕ := ∑ i ∈ Finset.range (d + 1), d.choose i * ballVolShift (n - d) r (max i (d - i)) /-- Central-binomial step-drop term `t_j = C(2j, j) * C(n - 2j - 1, r - j)` (zero if `r < j`). -/ def stepTerm (n r j : ℕ) : ℕ := if j ≤ r then (2 * j).choose j * (n - 2 * j - 1).choose (r - j) else 0 lemma ballVol_zero_right (n : ℕ) : ballVol n 0 = 1 := by simp [ballVol] lemma ballVol_succ_right (n r : ℕ) : ballVol n (r + 1) = ballVol n r + n.choose (r + 1) := by simp [ballVol, Finset.sum_range_succ] lemma ballVol_succ_left (n r : ℕ) : ballVol (n + 1) (r + 1) = ballVol n (r + 1) + ballVol n r := by induction r with | zero => simp [ballVol, Finset.sum_range_succ] omega | succ r ih => rw [ballVol_succ_right, ih, ballVol_succ_right n (r + 1), ballVol_succ_right n r, Nat.choose_succ_succ] ring lemma ballVol_mono {n r₁ r₂ : ℕ} (h : r₁ ≤ r₂) : ballVol n r₁ ≤ ballVol n r₂ := by unfold ballVol exact Finset.sum_le_sum_of_subset (Finset.range_mono (Nat.succ_le_succ h)) lemma ballVol_eq_two_pow {n r : ℕ} (h : n ≤ r) : ballVol n r = 2 ^ n := by unfold ballVol have hsub : Finset.range (n + 1) ⊆ Finset.range (r + 1) := Finset.range_mono (Nat.succ_le_succ h) rw [← Finset.sum_subset hsub] · exact Nat.sum_range_choose n · intro k _ hk simp only [Finset.mem_range, not_lt] at hk exact Nat.choose_eq_zero_of_lt (Nat.lt_of_succ_le hk) lemma ballVolShift_succ_left (a r k : ℕ) : ballVolShift (a + 1) r k = ballVolShift a r k + ballVolShift a r (k + 1) := by unfold ballVolShift by_cases h1 : k + 1 ≤ r · have h0 : k ≤ r := by omega have hsub : r - k = (r - (k + 1)) + 1 := by omega simp only [h0, h1, ↓reduceIte, hsub, ballVol_succ_left] · by_cases h0 : k ≤ r · have heq : k = r := by omega subst heq simp [ballVol_zero_right] · simp [h0, h1] lemma ballVolShift_diff (a r k : ℕ) : ballVolShift a r k = ballVolShift a r (k + 1) + (if k ≤ r then a.choose (r - k) else 0) := by unfold ballVolShift by_cases h1 : k + 1 ≤ r · have h0 : k ≤ r := by omega have hsub : r - k = (r - (k + 1)) + 1 := by omega simp only [h0, h1, ↓reduceIte, hsub, ballVol_succ_right] · by_cases h0 : k ≤ r · have heq : k = r := by omega subst heq simp [ballVol_zero_right] · simp [h0, h1] lemma ballVolShift_eq_zero_of_gt {a r k : ℕ} (h : r < k) : ballVolShift a r k = 0 := by simp [ballVolShift, Nat.not_le_of_lt h] lemma ballVolShift_pos_of_le {a r k : ℕ} (h : k ≤ r) : 0 < ballVolShift a r k := by simp only [ballVolShift, h, ↓reduceIte] unfold ballVol have hmem : 0 ∈ Finset.range (r - k + 1) := Finset.mem_range.mpr (Nat.succ_pos _) have hle := Finset.single_le_sum (f := fun j => a.choose j) (fun _ _ => Nat.zero_le _) hmem simp only [Nat.choose_zero_right] at hle omega /-- Standard binomial absorption identity `(n + 1) * C(n, k) = (k + 1) * C(n + 1, k + 1)`. -/ lemma succ_mul_choose_eq' (n k : ℕ) : (n + 1) * n.choose k = (k + 1) * (n + 1).choose (k + 1) := by induction n generalizing k with | zero => cases k with | zero => simp | succ k => simp only [Nat.choose_zero_succ, mul_zero] rw [Nat.choose_eq_zero_of_lt (by omega : 1 < k + 2), mul_zero] | succ n ih => cases k with | zero => simp | succ k => have h1 := ih k have h2 := ih (k + 1) have hP1 : (n + 1).choose (k + 1) = n.choose k + n.choose (k + 1) := Nat.choose_succ_succ n k have hP2 : (n + 2).choose (k + 2) = (n + 1).choose (k + 1) + (n + 1).choose (k + 2) := Nat.choose_succ_succ (n + 1) (k + 1) calc (n + 2) * (n + 1).choose (k + 1) = (n + 1) * (n.choose k + n.choose (k + 1)) + (n + 1).choose (k + 1) := by rw [hP1]; ring _ = (n + 1) * n.choose k + (n + 1) * n.choose (k + 1) + (n + 1).choose (k + 1) := by ring _ = (k + 1) * (n + 1).choose (k + 1) + (k + 2) * (n + 1).choose (k + 2) + (n + 1).choose (k + 1) := by rw [h1, h2] _ = (k + 2) * ((n + 1).choose (k + 1) + (n + 1).choose (k + 2)) := by ring _ = (k + 2) * (n + 2).choose (k + 2) := by rw [hP2] /-- Auxiliary: splitting `hammingDist` along the disagreement set `D` of `x` and `y`. -/ lemma hammingDist_split {n : ℕ} (x y z : Word n) : let D : Finset (Fin n) := Finset.univ.filter (fun i => x i ≠ y i) hammingDist x z = (D.filter (fun i => z i ≠ x i)).card + (Dᶜ.filter (fun i => z i ≠ x i)).card ∧ hammingDist y z = (D.card - (D.filter (fun i => z i ≠ x i)).card) + (Dᶜ.filter (fun i => z i ≠ x i)).card := by intro D constructor · rw [hammingDist] have hpart : (Finset.univ.filter (fun i => x i ≠ z i)) = (D.filter (fun i => z i ≠ x i)) ∪ (Dᶜ.filter (fun i => z i ≠ x i)) := by ext i simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_union, Finset.mem_compl, D] by_cases hxy : x i = y i <;> simp [hxy, ne_comm] rw [hpart, Finset.card_union_of_disjoint] rw [Finset.disjoint_left] intro i hi1 hi2 simp only [Finset.mem_filter, Finset.mem_compl] at hi1 hi2 exact hi2.1 hi1.1 · rw [hammingDist] have hpart : (Finset.univ.filter (fun i => y i ≠ z i)) = (D.filter (fun i => z i = x i)) ∪ (Dᶜ.filter (fun i => z i ≠ x i)) := by ext i simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_union, Finset.mem_compl, D] cases hxi : x i <;> cases hyi : y i <;> cases hzi : z i <;> simp rw [hpart, Finset.card_union_of_disjoint] · congr 1 have hunion : D = (D.filter (fun i => z i = x i)) ∪ (D.filter (fun i => z i ≠ x i)) := by ext i; simp only [Finset.mem_union, Finset.mem_filter]; by_cases h : z i = x i <;> simp [h] have hdisj : Disjoint (D.filter (fun i => z i = x i)) (D.filter (fun i => z i ≠ x i)) := by rw [Finset.disjoint_left]; intro i hi1 hi2; simp only [Finset.mem_filter] at hi1 hi2; exact hi2.2 hi1.2 have hcard := congrArg Finset.card hunion rw [Finset.card_union_of_disjoint hdisj] at hcard omega · rw [Finset.disjoint_left] intro i hi1 hi2 simp only [Finset.mem_filter, Finset.mem_compl] at hi1 hi2 exact hi2.1 hi1.1 /-- **Theorem 2.1 (Exact Isometry-Invariant Formula for Full-Cube Kanerva Overlap)**: For any `x, y ∈ {0, 1}^n` and radius `r`, `|B_r(x) ∩ B_r(y)| = overlapFormula n r (d_H(x, y))`. -/ theorem fullOverlap_eq_overlapFormula (n r : ℕ) (x y : Word n) : fullOverlap n r x y = overlapFormula n r (hammingDist x y) := by let D : Finset (Fin n) := Finset.univ.filter (fun i => x i ≠ y i) have hDcard : D.card = hammingDist x y := rfl have hDcCard : Dᶜ.card = n - hammingDist x y := by rw [Finset.card_compl, Fintype.card_fin, hDcard] let S : Finset (Finset (Fin n) × Finset (Fin n)) := (D.powerset ×ˢ Dᶜ.powerset).filter (fun p => max p.1.card (D.card - p.1.card) + p.2.card ≤ r) have hbij : fullOverlap n r x y = S.card := by unfold fullOverlap overlap activeSet refine Finset.card_bij (fun z _ => (D.filter (fun i => z i ≠ x i), Dᶜ.filter (fun i => z i ≠ x i))) ?_ ?_ ?_ · intro z hz simp only [Finset.mem_inter, Finset.mem_filter, Finset.mem_univ, true_and] at hz have hsp := hammingDist_split x y z simp only [S, Finset.mem_filter, Finset.mem_product, Finset.mem_powerset] refine ⟨⟨Finset.filter_subset _ _, Finset.filter_subset _ _⟩, ?_⟩ dsimp only [D] at hsp ⊢ omega · intro z1 _ z2 _ heq have h1 := congrArg Prod.fst heq have h2 := congrArg Prod.snd heq funext i by_cases hi : i ∈ D · have hm : (z1 i ≠ x i) ↔ (z2 i ≠ x i) := by have := Finset.ext_iff.mp h1 i simpa [hi] using this cases hxi : x i <;> cases hz1 : z1 i <;> cases hz2 : z2 i <;> simp_all · have hic : i ∈ Dᶜ := Finset.mem_compl.mpr hi have hm : (z1 i ≠ x i) ↔ (z2 i ≠ x i) := by have := Finset.ext_iff.mp h2 i simpa [hic] using this cases hxi : x i <;> cases hz1 : z1 i <;> cases hz2 : z2 i <;> simp_all · intro ⟨A, B⟩ hAB simp only [S, Finset.mem_filter, Finset.mem_product, Finset.mem_powerset] at hAB rcases hAB with ⟨⟨hAD, hBDc⟩, hle⟩ let z : Word n := fun i => if i ∈ A ∪ B then !(x i) else x i have hA_eq : D.filter (fun i => z i ≠ x i) = A := by ext i simp only [Finset.mem_filter, z, Finset.mem_union] by_cases hiA : i ∈ A · have hiD : i ∈ D := hAD hiA simp [hiD, hiA] · by_cases hiB : i ∈ B · have hiDc : i ∈ Dᶜ := hBDc hiB have hiD : i ∉ D := Finset.mem_compl.mp hiDc simp [hiD, hiA] · simp [hiA, hiB] have hB_eq : Dᶜ.filter (fun i => z i ≠ x i) = B := by ext i simp only [Finset.mem_filter, z, Finset.mem_union] by_cases hiB : i ∈ B · have hiDc : i ∈ Dᶜ := hBDc hiB simp [hiDc, hiB] · by_cases hiA : i ∈ A · have hiD : i ∈ D := hAD hiA have hiDc : i ∉ Dᶜ := by simp [hiD] simp [hiDc, hiB] · simp [hiA, hiB] refine ⟨z, ?_, Prod.ext hA_eq hB_eq⟩ have hsp := hammingDist_split x y z simp only [Finset.mem_inter, Finset.mem_filter, Finset.mem_univ, true_and] have hA_eq' : (Finset.univ.filter (fun i => x i ≠ y i)).filter (fun i => z i ≠ x i) = A := hA_eq have hB_eq' : (Finset.univ.filter (fun i => x i ≠ y i))ᶜ.filter (fun i => z i ≠ x i) = B := hB_eq dsimp only at hsp rw [hA_eq', hB_eq'] at hsp have hcard_eq : (Finset.univ.filter (fun i => x i ≠ y i)).card = D.card := rfl rw [hcard_eq] at hsp omega rw [hbij] have hS : S.card = ∑ i ∈ Finset.range (D.card + 1), (D.powersetCard i).card * (Dᶜ.powerset.filter (fun B => max i (D.card - i) + B.card ≤ r)).card := by have h1 : S.card = ∑ A ∈ D.powerset, (Dᶜ.powerset.filter (fun B => max A.card (D.card - A.card) + B.card ≤ r)).card := by rw [Finset.card_eq_sum_ones, ← Finset.sum_fiberwise_of_maps_to (g := Prod.fst) (t := D.powerset)] · refine Finset.sum_congr rfl fun A hA => ?_ rw [← Finset.card_eq_sum_ones] refine Finset.card_bij (fun p _ => p.2) ?_ ?_ ?_ · intro ⟨A', B'⟩ hp simp only [S, Finset.mem_filter, Finset.mem_product, Finset.mem_powerset] at hp rcases hp with ⟨⟨⟨_, hB'⟩, hle⟩, rfl⟩ exact Finset.mem_filter.mpr ⟨Finset.mem_powerset.mpr hB', hle⟩ · intro ⟨A1, B1⟩ hp1 ⟨A2, B2⟩ hp2 hB simp only [Finset.mem_filter] at hp1 hp2 exact Prod.ext (hp1.2.trans hp2.2.symm) hB · intro B hB simp only [Finset.mem_filter, Finset.mem_powerset] at hB exact ⟨(A, B), by simp [S, Finset.mem_powerset.mp hA, hB.1, hB.2], rfl⟩ · intro p hp simp only [S, Finset.mem_filter, Finset.mem_product] at hp exact hp.1.1 rw [h1] have hpow : D.powerset = (Finset.range (D.card + 1)).biUnion (fun i => D.powersetCard i) := by ext A simp only [Finset.mem_powerset, Finset.mem_biUnion, Finset.mem_range, Finset.mem_powersetCard] constructor · intro hAD exact ⟨A.card, Nat.lt_succ_of_le (Finset.card_le_card hAD), hAD, rfl⟩ · rintro ⟨_, _, hAD, _⟩ exact hAD rw [hpow, Finset.sum_biUnion] · refine Finset.sum_congr rfl fun i _ => ?_ have hconst : ∑ A ∈ D.powersetCard i, (Dᶜ.powerset.filter (fun B => max A.card (D.card - A.card) + B.card ≤ r)).card = ∑ _A ∈ D.powersetCard i, (Dᶜ.powerset.filter (fun B => max i (D.card - i) + B.card ≤ r)).card := by refine Finset.sum_congr rfl fun A hA => ?_ rw [(Finset.mem_powersetCard.mp hA).2] rw [hconst, Finset.sum_const, smul_eq_mul] · intro i _ j _ hij change Disjoint (D.powersetCard i) (D.powersetCard j) rw [Finset.disjoint_left] intro A h1 h2 have := (Finset.mem_powersetCard.mp h1).2 have := (Finset.mem_powersetCard.mp h2).2 omega rw [hS, hDcard] unfold overlapFormula refine Finset.sum_congr rfl fun i _ => ?_ rw [Finset.card_powersetCard, hDcard] congr 1 set m := max i (hammingDist x y - i) unfold ballVolShift by_cases hm : m ≤ r · simp only [hm, ↓reduceIte] have hfilt : Dᶜ.powerset.filter (fun B => m + B.card ≤ r) = (Finset.range (r - m + 1)).biUnion (fun j => Dᶜ.powersetCard j) := by ext B simp only [Finset.mem_filter, Finset.mem_powerset, Finset.mem_biUnion, Finset.mem_range, Finset.mem_powersetCard] constructor · rintro ⟨hB, hle⟩ exact ⟨B.card, by omega, hB, rfl⟩ · rintro ⟨j, hj, hB, rfl⟩ exact ⟨hB, by omega⟩ rw [hfilt, Finset.card_biUnion] · unfold ballVol refine Finset.sum_congr rfl fun j _ => ?_ rw [Finset.card_powersetCard, hDcCard] · intro j1 _ j2 _ hne change Disjoint (Dᶜ.powersetCard j1) (Dᶜ.powersetCard j2) rw [Finset.disjoint_left] intro B h1 h2 have := (Finset.mem_powersetCard.mp h1).2 have := (Finset.mem_powersetCard.mp h2).2 omega · simp only [hm, ↓reduceIte] rw [Finset.card_eq_zero, Finset.filter_eq_empty_iff] intro B _ omega /-- Corollary: Every full Hamming ball of radius `r` in `{0, 1}^n` has exact volume `ballVol n r`. -/ theorem fullBall_card_eq_ballVol (n r : ℕ) (x : Word n) : (fullBall n r x).card = ballVol n r := by have h := fullOverlap_eq_overlapFormula n r x x unfold fullOverlap at h rw [overlap_self, hammingDist_self] at h simpa [fullBall, overlapFormula, ballVolShift] using h lemma overlapFormula_zero_right (n r : ℕ) : overlapFormula n r 0 = ballVol n r := by simp [overlapFormula, ballVolShift] /-- Auxiliary Pascal reindexing helper for sums over `range (d + 2)`. -/ lemma sum_choose_succ_mul (d : ℕ) (f : ℕ → ℕ) : ∑ i ∈ Finset.range (d + 2), (d + 1).choose i * f i = ∑ i ∈ Finset.range (d + 1), d.choose i * (f i + f (i + 1)) := by rw [Finset.sum_range_succ'] simp only [Nat.choose_zero_right, one_mul] have h1 : ∑ i ∈ Finset.range (d + 1), (d + 1).choose (i + 1) * f (i + 1) = (∑ i ∈ Finset.range (d + 1), d.choose i * f (i + 1)) + (∑ i ∈ Finset.range (d + 1), d.choose (i + 1) * f (i + 1)) := by rw [← Finset.sum_add_distrib] refine Finset.sum_congr rfl fun i _ => ?_ rw [Nat.choose_succ_succ, add_mul] rw [h1] have h2 : f 0 + ∑ i ∈ Finset.range (d + 1), d.choose (i + 1) * f (i + 1) = ∑ i ∈ Finset.range (d + 1), d.choose i * f i := by have hsplit := Finset.sum_range_succ' (fun i => d.choose i * f i) d have hlast : ∑ i ∈ Finset.range (d + 1), d.choose (i + 1) * f (i + 1) = ∑ i ∈ Finset.range d, d.choose (i + 1) * f (i + 1) := by rw [Finset.sum_range_succ] simp rw [hlast, hsplit] simp [add_comm] have h3 : ∑ i ∈ Finset.range (d + 1), d.choose i * (f i + f (i + 1)) = (∑ i ∈ Finset.range (d + 1), d.choose i * f i) + (∑ i ∈ Finset.range (d + 1), d.choose i * f (i + 1)) := by rw [← Finset.sum_add_distrib] refine Finset.sum_congr rfl fun i _ => mul_add _ _ _ rw [h3, ← h2] ring /-- **Theorem 2.2 (Exact Parity-Plateau Law)**: For every `n, r` and every `m` with `2m + 2 ≤ n`, the Kanerva overlap is identically flat across every odd-to-even step: `I_n(r, 2m + 1) = I_n(r, 2m + 2)`. -/ theorem overlapFormula_parity_plateau {n r m : ℕ} (h : 2 * m + 2 ≤ n) : overlapFormula n r (2 * m + 1) = overlapFormula n r (2 * m + 2) := by unfold overlapFormula have hsub : n - (2 * m + 1) = (n - (2 * m + 2)) + 1 := by omega rw [hsub] rw [sum_choose_succ_mul (2 * m + 1) (fun i => ballVolShift (n - (2 * m + 2)) r (max i (2 * m + 2 - i)))] refine Finset.sum_congr rfl fun i hi => ?_ rw [Finset.mem_range] at hi rw [ballVolShift_succ_left] congr 1 by_cases him : i ≤ m · have h1 : max i (2 * m + 1 - i) = 2 * m + 1 - i := by omega have h2 : max i (2 * m + 2 - i) = 2 * m + 2 - i := by omega have h3 : max (i + 1) (2 * m + 2 - (i + 1)) = 2 * m + 1 - i := by omega rw [h1, h2, h3, add_comm] congr 2; omega · have h1 : max i (2 * m + 1 - i) = i := by omega have h2 : max i (2 * m + 2 - i) = i := by omega have h3 : max (i + 1) (2 * m + 2 - (i + 1)) = i + 1 := by omega rw [h1, h2, h3] /-- **Theorem 2.3 (Exact Central-Binomial Step-Drop Formula)**: For every `n, r, m` with `2m + 1 ≤ n`, `I_n(r, 2m) = I_n(r, 2m + 1) + C(2m, m) * C(n - 2m - 1, r - m)`. -/ theorem overlapFormula_step_drop {n r m : ℕ} (h : 2 * m + 1 ≤ n) : overlapFormula n r (2 * m) = overlapFormula n r (2 * m + 1) + stepTerm n r m := by unfold overlapFormula have hsub : n - 2 * m = (n - (2 * m + 1)) + 1 := by omega rw [hsub] rw [sum_choose_succ_mul (2 * m) (fun i => ballVolShift (n - (2 * m + 1)) r (max i (2 * m + 1 - i)))] have hm_mem : m ∈ Finset.range (2 * m + 1) := Finset.mem_range.mpr (by omega) rw [← Finset.add_sum_erase _ _ hm_mem, ← Finset.add_sum_erase _ _ hm_mem] have hrest : ∑ i ∈ (Finset.range (2 * m + 1)).erase m, (2 * m).choose i * ballVolShift (n - (2 * m + 1) + 1) r (max i (2 * m - i)) = ∑ i ∈ (Finset.range (2 * m + 1)).erase m, (2 * m).choose i * (ballVolShift (n - (2 * m + 1)) r (max i (2 * m + 1 - i)) + ballVolShift (n - (2 * m + 1)) r (max (i + 1) (2 * m + 1 - (i + 1)))) := by refine Finset.sum_congr rfl fun i hi => ?_ have hne : i ≠ m := Finset.ne_of_mem_erase hi have hlt : i < 2 * m + 1 := Finset.mem_range.mp (Finset.mem_of_mem_erase hi) rw [ballVolShift_succ_left] congr 1 by_cases him : i < m · have h1 : max i (2 * m - i) = 2 * m - i := by omega have h2 : max i (2 * m + 1 - i) = 2 * m + 1 - i := by omega have h3 : max (i + 1) (2 * m + 1 - (i + 1)) = 2 * m - i := by omega rw [h1, h2, h3, add_comm] congr 2; omega · have h1 : max i (2 * m - i) = i := by omega have h2 : max i (2 * m + 1 - i) = i := by omega have h3 : max (i + 1) (2 * m + 1 - (i + 1)) = i + 1 := by omega rw [h1, h2, h3] rw [hrest] have h1 : max m (2 * m - m) = m := by omega have h2 : max m (2 * m + 1 - m) = m + 1 := by omega have h3 : max (m + 1) (2 * m + 1 - (m + 1)) = m + 1 := by omega rw [h1, h2, h3, ballVolShift_succ_left, ballVolShift_diff (n - (2 * m + 1)) r m] have hsub2 : n - (2 * m + 1) = n - 2 * m - 1 := by omega rw [hsub2] unfold stepTerm split_ifs <;> ring /-- Exact finite-support vanishing: `I_n(r, d) = 0` whenever `2r < d`. -/ theorem overlapFormula_eq_zero_of_two_mul_lt {n r d : ℕ} (h : 2 * r < d) : overlapFormula n r d = 0 := by unfold overlapFormula refine Finset.sum_eq_zero fun i _ => ?_ have hm : r < max i (d - i) := by omega rw [ballVolShift_eq_zero_of_gt hm, mul_zero] /-- Strict positivity whenever `d ≤ 2r` and `d ≤ n`. -/ theorem overlapFormula_pos_of_le_two_mul {n r d : ℕ} (hr : d ≤ 2 * r) (_hn : d ≤ n) : 0 < overlapFormula n r d := by unfold overlapFormula let i0 : ℕ := d / 2 have hi0 : i0 ∈ Finset.range (d + 1) := Finset.mem_range.mpr (by omega) have hterm : 0 < d.choose i0 * ballVolShift (n - d) r (max i0 (d - i0)) := by apply Nat.mul_pos · exact Nat.choose_pos (by omega) · exact ballVolShift_pos_of_le (by omega) exact lt_of_lt_of_le hterm (Finset.single_le_sum (f := fun i => d.choose i * ballVolShift (n - d) r (max i (d - i))) (fun _ _ => Nat.zero_le _) hi0) /-- **Theorem 2.4 (Exact Support Characterization)**: For `d ≤ n`, `I_n(r, d) = 0 ↔ 2r < d`. -/ theorem overlapFormula_eq_zero_iff {n r d : ℕ} (hn : d ≤ n) : overlapFormula n r d = 0 ↔ 2 * r < d := by constructor · intro hz by_contra hle exact Nat.ne_of_gt (overlapFormula_pos_of_le_two_mul (by omega) hn) hz · exact overlapFormula_eq_zero_of_two_mul_lt /-- Combined even-to-even step-drop valid for ALL `2r < n` and `m ≤ r`. -/ lemma overlapFormula_even_step_subcrit {n r m : ℕ} (hr : 2 * r < n) (hm : m ≤ r) : overlapFormula n r (2 * m) = overlapFormula n r (2 * m + 2) + stepTerm n r m := by by_cases h2 : 2 * m + 2 ≤ n · rw [overlapFormula_step_drop (by omega), overlapFormula_parity_plateau h2] · have hmr : m = r := by omega subst hmr rw [overlapFormula_step_drop (by omega)] rw [overlapFormula_eq_zero_of_two_mul_lt (by omega : 2 * m < 2 * m + 1), overlapFormula_eq_zero_of_two_mul_lt (by omega : 2 * m < 2 * m + 2)] /-- **Theorem 2.5 (Exact Single-Sum Closed Form for Subcritical Radii `2r < n`)**: For `2r < n` and any `m ≤ r + 1`, `I_n(r, 2m) = ∑_{j = m}^r C(2j, j) * C(n - 2j - 1, r - j)`. -/ theorem overlapFormula_single_sum {n r m : ℕ} (hr : 2 * r < n) (hm : m ≤ r + 1) : overlapFormula n r (2 * m) = ∑ j ∈ Finset.Ico m (r + 1), stepTerm n r j := by obtain ⟨k, hk⟩ : ∃ k, m + k = r + 1 := ⟨r + 1 - m, by omega⟩ induction k generalizing m with | zero => have hmeq : m = r + 1 := by omega subst hmeq simp only [Finset.Ico_self, Finset.sum_empty] exact overlapFormula_eq_zero_of_two_mul_lt (by omega) | succ k ih => have hmle : m ≤ r := by omega rw [overlapFormula_even_step_subcrit hr hmle, show 2 * m + 2 = 2 * (m + 1) by omega, ih (m := m + 1) (by omega) (by omega), Finset.sum_eq_sum_Ico_succ_bot (by omega : m < r + 1)] ring /-- Monotonicity of `overlapFormula n r d` in `d` (for all `d₁ ≤ d₂ ≤ n`). -/ theorem overlapFormula_antitone {n r d₁ d₂ : ℕ} (h12 : d₁ ≤ d₂) (h2n : d₂ ≤ n) : overlapFormula n r d₂ ≤ overlapFormula n r d₁ := by obtain ⟨k, rfl⟩ := Nat.exists_eq_add_of_le h12 induction k with | zero => exact le_refl _ | succ k ih => have hstep : overlapFormula n r (d₁ + (k + 1)) ≤ overlapFormula n r (d₁ + k) := by set d := d₁ + k have hd : d + 1 ≤ n := by omega rcases Nat.even_or_odd d with ⟨m, hm⟩ | ⟨m, hm⟩ · have hdeq : d = 2 * m := by omega rw [show d₁ + (k + 1) = 2 * m + 1 by omega, hdeq, overlapFormula_step_drop (by omega)] exact Nat.le_add_right _ _ · have hdeq : d = 2 * m + 1 := by omega rw [show d₁ + (k + 1) = 2 * m + 2 by omega, hdeq, ← overlapFormula_parity_plateau (by omega)] exact le_trans hstep (ih (by omega) (by omega)) /-- Auxiliary: flipping all bits complements Hamming distances to `n`. -/ lemma hammingDist_not_add {n : ℕ} (u z : Word n) : hammingDist u z + hammingDist (fun i => !u i) z = n := by rw [hammingDist, hammingDist] have hdisj : Disjoint (Finset.univ.filter (fun i : Fin n => u i ≠ z i)) (Finset.univ.filter (fun i : Fin n => (!u i) ≠ z i)) := by rw [Finset.disjoint_left] intro i h1 h2 simp only [Finset.mem_filter] at h1 h2 cases h1' : u i <;> cases h2' : z i <;> simp_all have hunion : (Finset.univ.filter (fun i : Fin n => u i ≠ z i)) ∪ (Finset.univ.filter (fun i : Fin n => (!u i) ≠ z i)) = Finset.univ := by ext i simp only [Finset.mem_union, Finset.mem_filter, Finset.mem_univ, true_and, iff_true] cases u i <;> cases z i <;> decide rw [← Finset.card_union_of_disjoint hdisj, hunion, Finset.card_univ, Fintype.card_fin] /-- **Theorem 2.6 (Exact Complement Duality of Kanerva Overlap)**: For all `n`, all `r < n`, and all `d ≤ n`: `I_n(r, d) + 2 * |B_{n-1-r}| = 2^n + I_n(n - 1 - r, d)`. -/ theorem overlapFormula_complement_duality {n r d : ℕ} (hr : r < n) (hd : d ≤ n) : overlapFormula n r d + 2 * ballVol n (n - 1 - r) = 2 ^ n + overlapFormula n (n - 1 - r) d := by let x : Word n := fun _ => false let y : Word n := fun i => decide (i.val < d) have hdist : hammingDist x y = d := by rw [hammingDist] have hf : (Finset.univ.filter (fun i : Fin n => x i ≠ y i)) = Finset.univ.filter (fun i : Fin n => i.val < d) := by ext i; simp [x, y] rw [hf] have hbij : (Finset.univ.filter (fun i : Fin n => i.val < d)).card = (Finset.range d).card := by refine Finset.card_bij (fun i _ => i.val) ?_ ?_ ?_ · intro i hi; simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hi exact Finset.mem_range.mpr hi · intro i _ j _ h; exact Fin.ext h · intro a ha; rw [Finset.mem_range] at ha exact ⟨⟨a, by omega⟩, by simp [ha], rfl⟩ rw [hbij, Finset.card_range] have hflip : ∀ u : Word n, (fullBall n r u)ᶜ = fullBall n (n - 1 - r) (fun i => !u i) := by intro u ext z simp only [Finset.mem_compl, fullBall, activeSet, Finset.mem_filter, Finset.mem_univ, true_and, not_le] have hsum := hammingDist_not_add u z omega let B1 := fullBall n r x let B2 := fullBall n r y let C1 := fullBall n (n - 1 - r) (fun i => !x i) let C2 := fullBall n (n - 1 - r) (fun i => !y i) have hC1 : C1 = B1ᶜ := (hflip x).symm have hC2 : C2 = B2ᶜ := (hflip y).symm have hcard_word : Fintype.card (Word n) = 2 ^ n := by simp [Word] have hdist_flip : hammingDist (fun i => !x i) (fun i => !y i) = d := by rw [← hdist, hammingDist, hammingDist] congr 1; ext i; simp have hI1 : (B1 ∩ B2).card = overlapFormula n r d := by have := fullOverlap_eq_overlapFormula n r x y rw [hdist] at this exact this have hI2 : (C1 ∩ C2).card = overlapFormula n (n - 1 - r) d := by have := fullOverlap_eq_overlapFormula n (n - 1 - r) (fun i => !x i) (fun i => !y i) rw [hdist_flip] at this exact this have hC1_card : C1.card = ballVol n (n - 1 - r) := fullBall_card_eq_ballVol n (n - 1 - r) _ have hC2_card : C2.card = ballVol n (n - 1 - r) := fullBall_card_eq_ballVol n (n - 1 - r) _ have hdemorgan : (B1 ∩ B2)ᶜ = C1 ∪ C2 := by rw [hC1, hC2, Finset.compl_inter] have hcompl : (B1 ∩ B2).card + (B1 ∩ B2)ᶜ.card = 2 ^ n := by rw [← hcard_word, Finset.card_add_card_compl] have hie := Finset.card_union_add_card_inter C1 C2 rw [← hdemorgan] at hie rw [hI1, hI2, hC1_card, hC2_card] at * omega /-- Corollary: `ballVol n r + ballVol n (n - 1 - r) = 2 ^ n` for `r < n`. -/ lemma ballVol_add_compl {n r : ℕ} (hr : r < n) : ballVol n r + ballVol n (n - 1 - r) = 2 ^ n := by have h := overlapFormula_complement_duality (d := 0) hr (Nat.zero_le n) rw [overlapFormula_zero_right, overlapFormula_zero_right] at h omega /-! ## Section 3: Exact Super-Exponential Step-Contraction Law & Matching Decay Exponent -/ /-- Exact rational recurrence between consecutive step-drop terms `t_m` and `t_{m+1}`: `(m + 1) * (n - 2m - 1) * (n - 2m - 2) * t_{m+1} = 2 * (2m + 1) * (r - m) * (n - r - m - 1) * t_m`. -/ lemma stepTerm_succ_mul {n r m : ℕ} (hm : m < r) (hr : 2 * r < n) : (m + 1) * (n - 2 * m - 1) * (n - 2 * m - 2) * stepTerm n r (m + 1) = 2 * (2 * m + 1) * (r - m) * (n - r - m - 1) * stepTerm n r m := by have hm1 : m + 1 ≤ r := by omega have hm0 : m ≤ r := by omega simp only [stepTerm, hm1, hm0, ↓reduceIte] have hcb : (m + 1) * (2 * (m + 1)).choose (m + 1) = 2 * (2 * m + 1) * (2 * m).choose m := by have h3 : (2 * m + 1).choose m = (2 * m + 1).choose (m + 1) := by have := Nat.choose_symm (by omega : m ≤ 2 * m + 1) have hsub : 2 * m + 1 - m = m + 1 := by omega rw [hsub] at this exact this.symm have h4 : (2 * (m + 1)).choose (m + 1) = 2 * (2 * m + 1).choose m := by rw [show 2 * (m + 1) = (2 * m + 1) + 1 by omega, Nat.choose_succ_succ, ← h3] ring have h5 : (2 * m + 1) * (2 * m).choose m = (m + 1) * (2 * m + 1).choose (m + 1) := succ_mul_choose_eq' (2 * m) m rw [h4, h3] linarith set a := r - m - 1 set b := n - r - m - 2 have ha1 : r - m = a + 1 := by omega have hb1 : n - r - m - 1 = b + 1 := by omega have hN1 : n - 2 * m - 1 = a + b + 2 := by omega have hN2 : n - 2 * m - 2 = a + b + 1 := by omega have hN3 : n - 2 * (m + 1) - 1 = a + b := by omega have hR1 : r - (m + 1) = a := by omega rw [hR1, hN3, ha1, hb1, hN1, hN2] have hob : (a + b + 2) * (a + b + 1) * (a + b).choose a = (a + 1) * (b + 1) * (a + b + 2).choose (a + 1) := by have h1 := succ_mul_choose_eq' (a + b + 1) a have h2 : (a + b + 1) * (a + b).choose a = (b + 1) * (a + b + 1).choose a := by have hs1 := Nat.choose_symm (by omega : a ≤ a + b) have hs2 := Nat.choose_symm (by omega : a ≤ a + b + 1) have hsub1 : a + b - a = b := by omega have hsub2 : a + b + 1 - a = b + 1 := by omega rw [hsub1] at hs1 rw [hsub2] at hs2 rw [← hs1, ← hs2] exact succ_mul_choose_eq' (a + b) b calc (a + b + 2) * (a + b + 1) * (a + b).choose a = (a + b + 2) * ((a + b + 1) * (a + b).choose a) := by ring _ = (a + b + 2) * ((b + 1) * (a + b + 1).choose a) := by rw [h2] _ = (b + 1) * ((a + b + 2) * (a + b + 1).choose a) := by ring _ = (b + 1) * ((a + 1) * (a + b + 2).choose (a + 1)) := by rw [h1] _ = (a + 1) * (b + 1) * (a + b + 2).choose (a + 1) := by ring calc (m + 1) * (a + b + 2) * (a + b + 1) * ((2 * (m + 1)).choose (m + 1) * (a + b).choose a) = ((m + 1) * (2 * (m + 1)).choose (m + 1)) * ((a + b + 2) * (a + b + 1) * (a + b).choose a) := by ring _ = (2 * (2 * m + 1) * (2 * m).choose m) * ((a + 1) * (b + 1) * (a + b + 2).choose (a + 1)) := by rw [hcb, hob] _ = 2 * (2 * m + 1) * (a + 1) * (b + 1) * ((2 * m).choose m * (a + b + 2).choose (a + 1)) := by ring /-- **Key Algebraic Backward-Induction Certificate**: For all `2r < n` and all `0 ≤ m ≤ r`: `(n - 2r)^2 * I_n(r, 2m + 2) ≤ 4 * (r - m) * (n - r - m - 1) * stepTerm n r m`. -/ theorem overlapFormula_backward_cert {n r m : ℕ} (hr : 2 * r < n) (hm : m ≤ r) : (n - 2 * r) ^ 2 * overlapFormula n r (2 * m + 2) ≤ 4 * (r - m) * (n - r - m - 1) * stepTerm n r m := by obtain ⟨k, hk⟩ : ∃ k, m + k = r := ⟨r - m, by omega⟩ induction k generalizing m with | zero => have hmeq : m = r := by omega subst hmeq rw [overlapFormula_eq_zero_of_two_mul_lt (by omega)] simp | succ k ih => have hmlt : m < r := by omega have hih := ih (m := m + 1) (by omega) (by omega) have hstep : overlapFormula n r (2 * m + 2) = overlapFormula n r (2 * (m + 1) + 2) + stepTerm n r (m + 1) := by have := overlapFormula_even_step_subcrit hr (show m + 1 ≤ r by omega) simpa [mul_add] using this rw [hstep, mul_add] have h1 : (n - 2 * r) ^ 2 * overlapFormula n r (2 * (m + 1) + 2) + (n - 2 * r) ^ 2 * stepTerm n r (m + 1) ≤ ((n - 2 * r) ^ 2 + 4 * (r - (m + 1)) * (n - r - (m + 1) - 1)) * stepTerm n r (m + 1) := by linarith refine le_trans h1 ?_ set a := r - m set b := n - r - m - 1 have ha : 1 ≤ a := by omega have hab : a ≤ b := by omega have hD : n - 2 * r = b - a + 1 := by omega have hsum1 : n - 2 * m - 1 = a + b := by omega have hsum2 : n - 2 * m - 2 = a + b - 1 := by omega have ha' : r - (m + 1) = a - 1 := by omega have hb' : n - r - (m + 1) - 1 = b - 1 := by omega have hrec := stepTerm_succ_mul hmlt hr have hpoly : (n - 2 * r) ^ 2 + 4 * (r - (m + 1)) * (n - r - (m + 1) - 1) ≤ (n - 2 * m - 1) * (n - 2 * m - 2) := by rw [hD, ha', hb', hsum1, hsum2] zify [ha, hab, show 1 ≤ b by omega, show 1 ≤ a + b by omega] nlinarith have h4 : 2 * (2 * m + 1) ≤ 4 * (m + 1) := by omega have hmul1 : ((n - 2 * r) ^ 2 + 4 * (r - (m + 1)) * (n - r - (m + 1) - 1)) * stepTerm n r (m + 1) ≤ (n - 2 * m - 1) * (n - 2 * m - 2) * stepTerm n r (m + 1) := Nat.mul_le_mul_right _ hpoly refine le_trans hmul1 ?_ have hmpos : 0 < m + 1 := Nat.succ_pos m refine Nat.le_of_mul_le_mul_left ?_ hmpos calc (m + 1) * ((n - 2 * m - 1) * (n - 2 * m - 2) * stepTerm n r (m + 1)) = (m + 1) * (n - 2 * m - 1) * (n - 2 * m - 2) * stepTerm n r (m + 1) := by ring _ = 2 * (2 * m + 1) * (r - m) * (n - r - m - 1) * stepTerm n r m := hrec _ ≤ 4 * (m + 1) * (r - m) * (n - r - m - 1) * stepTerm n r m := by gcongr _ = (m + 1) * (4 * (r - m) * (n - r - m - 1) * stepTerm n r m) := by ring /-- **Theorem 3.1 (Sharp Finite-`n` Super-Exponential Step-Contraction Law)**: For ALL `n, r, m` with `2r < n` and `m ≤ r`, `((n - 2r)^2 + 4(r - m)(n - r - m - 1)) * I_n(r, 2m + 2) ≤ 4(r - m)(n - r - m - 1) * I_n(r, 2m)`. -/ theorem overlapFormula_step_ratio_sharp {n r m : ℕ} (hr : 2 * r < n) (hm : m ≤ r) : ((n - 2 * r) ^ 2 + 4 * (r - m) * (n - r - m - 1)) * overlapFormula n r (2 * m + 2) ≤ 4 * (r - m) * (n - r - m - 1) * overlapFormula n r (2 * m) := by have hcert := overlapFormula_backward_cert hr hm have hstep := overlapFormula_even_step_subcrit hr hm rw [hstep, mul_add] linarith /-- Denominator identity showing the exact super-exponential denominator strictly beats `(n - 2m - 1)^2`. -/ lemma sharp_denom_identity {n r m : ℕ} (hr : 2 * r < n) (hm : m ≤ r) : (n - 2 * r) ^ 2 + 4 * (r - m) * (n - r - m - 1) = (n - 2 * m - 1) ^ 2 + 2 * (n - 2 * r) - 1 := by set a := r - m set b := n - r - m - 1 have hD : n - 2 * r = b - a + 1 := by omega have hS : n - 2 * m - 1 = a + b := by omega have hab : a ≤ b := by omega rw [hD, hS] zify [hab, show 1 ≤ (a + b) ^ 2 + 2 * (b - a + 1) by omega] ring /-- **Corollary 3.2 (Uniform Step-Contraction by `4p(1-p)`)**: For ALL `2r < n` and ALL `m ≤ r`, `n^2 * I_n(r, 2m + 2) ≤ 4 * r * (n - r) * I_n(r, 2m)`. -/ theorem overlapFormula_step_ratio_uniform {n r m : ℕ} (hr : 2 * r < n) (hm : m ≤ r) : n ^ 2 * overlapFormula n r (2 * m + 2) ≤ 4 * r * (n - r) * overlapFormula n r (2 * m) := by have hxle : 4 * (r - m) * (n - r - m - 1) ≤ 4 * r * (n - r) := by have h1 : r - m ≤ r := Nat.sub_le _ _ have h2 : n - r - m - 1 ≤ n - r := by omega gcongr have hD2 : (n - 2 * r) ^ 2 + 4 * r * (n - r) = n ^ 2 := by zify [show 2 * r ≤ n by omega, show r ≤ n by omega] ring have hcert := overlapFormula_backward_cert hr hm have hstep := overlapFormula_even_step_subcrit hr hm have hcert2 : (n - 2 * r) ^ 2 * overlapFormula n r (2 * m + 2) ≤ 4 * r * (n - r) * stepTerm n r m := le_trans hcert (Nat.mul_le_mul_right _ hxle) rw [← hD2, add_mul, hstep, mul_add] linarith /-- **Theorem 3.3 (Global Non-Asymptotic Exponential Decay Law — Constant 1)**: For ALL `2r < n` and ALL `m : ℕ`: `n^(2m) * I_n(r, 2m) ≤ (4 * r * (n - r))^m * I_n(r, 0)`. -/ theorem overlapFormula_exp_decay {n r : ℕ} (hr : 2 * r < n) (m : ℕ) : n ^ (2 * m) * overlapFormula n r (2 * m) ≤ (4 * r * (n - r)) ^ m * overlapFormula n r 0 := by induction m with | zero => simp | succ m ih => by_cases hm : m ≤ r · have hstep := overlapFormula_step_ratio_uniform hr hm calc n ^ (2 * (m + 1)) * overlapFormula n r (2 * (m + 1)) = n ^ (2 * m) * (n ^ 2 * overlapFormula n r (2 * m + 2)) := by rw [show 2 * (m + 1) = 2 * m + 2 by omega, pow_add]; ring _ ≤ n ^ (2 * m) * (4 * r * (n - r) * overlapFormula n r (2 * m)) := Nat.mul_le_mul_left _ hstep _ = (4 * r * (n - r)) * (n ^ (2 * m) * overlapFormula n r (2 * m)) := by ring _ ≤ (4 * r * (n - r)) * ((4 * r * (n - r)) ^ m * overlapFormula n r 0) := Nat.mul_le_mul_left _ ih _ = (4 * r * (n - r)) ^ (m + 1) * overlapFormula n r 0 := by rw [pow_succ']; ring · rw [overlapFormula_eq_zero_of_two_mul_lt (by omega)] simp /-- Arbitrary-distance exponential decay (including odd distances via parity plateau): for `2r < n` and `d ≤ n`, letting `m = (d + 1) / 2 = ⌈d / 2⌉`, `n^(2m) * I_n(r, d) ≤ (4 * r * (n - r))^m * I_n(r, 0)`. -/ theorem overlapFormula_exp_decay_any {n r d : ℕ} (hr : 2 * r < n) (hd : d ≤ n) : n ^ (2 * ((d + 1) / 2)) * overlapFormula n r d ≤ (4 * r * (n - r)) ^ ((d + 1) / 2) * overlapFormula n r 0 := by rcases Nat.even_or_odd d with ⟨m, hm⟩ | ⟨m, hm⟩ · have hdeq : d = 2 * m := by omega have hdiv : (d + 1) / 2 = m := by omega rw [hdiv, hdeq] exact overlapFormula_exp_decay hr m · have hdeq : d = 2 * m + 1 := by omega have hdiv : (d + 1) / 2 = m + 1 := by omega rw [hdiv, hdeq] by_cases h2 : 2 * m + 2 ≤ n · rw [overlapFormula_parity_plateau h2] exact overlapFormula_exp_decay hr (m + 1) · rw [overlapFormula_eq_zero_of_two_mul_lt (by omega)] simp /-- Exact binomial tail bound for `2r < n`: `(n - 2r) * ballVol n r ≤ (n - r) * C(n, r)`. -/ theorem ballVol_tail_bound {n r : ℕ} (hr : 2 * r < n) : (n - 2 * r) * ballVol n r ≤ (n - r) * n.choose r := by induction r with | zero => simp [ballVol_zero_right] | succ r ih => have hr0 : 2 * r < n := by omega have hih := ih hr0 rw [ballVol_succ_right, mul_add] have hrec : (r + 1) * n.choose (r + 1) = (n - r) * n.choose r := by have h1 := Nat.choose_succ_right_eq n r linarith have h1 : (n - 2 * (r + 1)) * ballVol n r ≤ (n - 2 * r) * ballVol n r := Nat.mul_le_mul_right _ (by omega) have h2 : (n - 2 * (r + 1)) * ballVol n r ≤ (r + 1) * n.choose (r + 1) := by rw [hrec]; exact le_trans h1 hih have h3 : (n - 2 * (r + 1)) + (r + 1) = n - (r + 1) := by omega calc (n - 2 * (r + 1)) * ballVol n r + (n - 2 * (r + 1)) * n.choose (r + 1) ≤ (r + 1) * n.choose (r + 1) + (n - 2 * (r + 1)) * n.choose (r + 1) := by linarith _ = ((n - 2 * (r + 1)) + (r + 1)) * n.choose (r + 1) := by ring _ = (n - (r + 1)) * n.choose (r + 1) := by rw [h3] /-- Exact elementary lower bound on central binomial coefficients: `4^m ≤ (2m + 1) * C(2m, m)`. -/ lemma centralBinom_lower_bound (m : ℕ) : 4 ^ m ≤ (2 * m + 1) * (2 * m).choose m := by have hsum : ∑ k ∈ Finset.range (2 * m + 1), (2 * m).choose k = 4 ^ m := by rw [Nat.sum_range_choose, show (4 : ℕ) ^ m = 2 ^ (2 * m) by rw [pow_mul]; norm_num] rw [← hsum] calc ∑ k ∈ Finset.range (2 * m + 1), (2 * m).choose k ≤ ∑ _k ∈ Finset.range (2 * m + 1), (2 * m).choose m := by refine Finset.sum_le_sum fun k _ => ?_ simpa using Nat.choose_le_middle k (2 * m) _ = (2 * m + 1) * (2 * m).choose m := by rw [Finset.sum_const, Finset.card_range, smul_eq_mul] /-- Auxiliary falling-ratio lower bound for outer binomial coefficients. -/ lemma outer_choose_lower {n r m : ℕ} (hr : 2 * r < n) (hm : m ≤ r) : (n - r) * ((r - m + 1) * (n - r - m)) ^ m * n.choose r ≤ n ^ (2 * m + 1) * (n - 2 * m - 1).choose (r - m) := by induction m with | zero => simp only [pow_zero, mul_one, Nat.sub_zero, Nat.mul_zero, zero_add, pow_one] have heq : (n - r) * n.choose r = n * (n - 1).choose r := by have h1 : n * (n - 1).choose (n - 1 - r) = (n - r) * n.choose (n - r) := by obtain ⟨N, rfl⟩ : ∃ N, n = N + 1 := ⟨n - 1, by omega⟩ obtain ⟨k, hk⟩ : ∃ k, r + k = N := ⟨N - r, by omega⟩ have hsub1 : N + 1 - 1 - r = k := by omega have hsub2 : N + 1 - r = k + 1 := by omega rw [hsub1, hsub2] exact succ_mul_choose_eq' N k rw [Nat.choose_symm (by omega : r ≤ n - 1), Nat.choose_symm (by omega : r ≤ n)] at h1 exact h1.symm exact le_of_eq heq | succ m ih => have hm0 : m ≤ r := by omega have hih := ih hm0 have hrec : (n - 2 * m - 1) * (n - 2 * m - 2) * (n - 2 * (m + 1) - 1).choose (r - (m + 1)) = (r - m) * (n - r - m - 1) * (n - 2 * m - 1).choose (r - m) := by have hst := stepTerm_succ_mul (n := n) (r := r) (m := m) (by omega) hr have hcb : (m + 1) * (2 * (m + 1)).choose (m + 1) = 2 * (2 * m + 1) * (2 * m).choose m := by have h3 : (2 * m + 1).choose m = (2 * m + 1).choose (m + 1) := by have := Nat.choose_symm (by omega : m ≤ 2 * m + 1) have hsub : 2 * m + 1 - m = m + 1 := by omega rw [hsub] at this; exact this.symm have h4 : (2 * (m + 1)).choose (m + 1) = 2 * (2 * m + 1).choose m := by rw [show 2 * (m + 1) = (2 * m + 1) + 1 by omega, Nat.choose_succ_succ, ← h3]; ring have h5 : (2 * m + 1) * (2 * m).choose m = (m + 1) * (2 * m + 1).choose (m + 1) := succ_mul_choose_eq' (2 * m) m rw [h4, h3]; linarith simp only [stepTerm, show m + 1 ≤ r by omega, show m ≤ r by omega, ↓reduceIte] at hst have hpos : 0 < 2 * (2 * m + 1) * (2 * m).choose m := Nat.mul_pos (Nat.mul_pos (by omega) (by omega)) (Nat.choose_pos (by omega)) apply Nat.eq_of_mul_eq_mul_left hpos calc (2 * (2 * m + 1) * (2 * m).choose m) * ((n - 2 * m - 1) * (n - 2 * m - 2) * (n - 2 * (m + 1) - 1).choose (r - (m + 1))) = ((m + 1) * (2 * (m + 1)).choose (m + 1)) * ((n - 2 * m - 1) * (n - 2 * m - 2) * (n - 2 * (m + 1) - 1).choose (r - (m + 1))) := by rw [← hcb] _ = (m + 1) * (n - 2 * m - 1) * (n - 2 * m - 2) * ((2 * (m + 1)).choose (m + 1) * (n - 2 * (m + 1) - 1).choose (r - (m + 1))) := by ring _ = 2 * (2 * m + 1) * (r - m) * (n - r - m - 1) * ((2 * m).choose m * (n - 2 * m - 1).choose (r - m)) := hst _ = (2 * (2 * m + 1) * (2 * m).choose m) * ((r - m) * (n - r - m - 1) * (n - 2 * m - 1).choose (r - m)) := by ring have hstep_le : ((r - (m + 1) + 1) * (n - r - (m + 1))) ≤ (r - m + 1) * (n - r - m) := by have h1 : r - (m + 1) + 1 ≤ r - m + 1 := by omega have h2 : n - r - (m + 1) ≤ n - r - m := by omega gcongr have hbase : r - (m + 1) + 1 = r - m := by omega have hbase2 : n - r - (m + 1) = n - r - m - 1 := by omega have hn2 : (n - 2 * m - 1) * (n - 2 * m - 2) ≤ n ^ 2 := by have h1 : n - 2 * m - 1 ≤ n := by omega have h2 : n - 2 * m - 2 ≤ n := by omega rw [sq]; exact Nat.mul_le_mul h1 h2 calc (n - r) * ((r - (m + 1) + 1) * (n - r - (m + 1))) ^ (m + 1) * n.choose r = ((r - m) * (n - r - m - 1)) * ((n - r) * ((r - (m + 1) + 1) * (n - r - (m + 1))) ^ m * n.choose r) := by rw [pow_succ', hbase, hbase2]; ring _ ≤ ((r - m) * (n - r - m - 1)) * ((n - r) * ((r - m + 1) * (n - r - m)) ^ m * n.choose r) := by gcongr _ ≤ ((r - m) * (n - r - m - 1)) * (n ^ (2 * m + 1) * (n - 2 * m - 1).choose (r - m)) := Nat.mul_le_mul_left _ hih _ = n ^ (2 * m + 1) * ((r - m) * (n - r - m - 1) * (n - 2 * m - 1).choose (r - m)) := by ring _ = n ^ (2 * m + 1) * ((n - 2 * m - 1) * (n - 2 * m - 2) * (n - 2 * (m + 1) - 1).choose (r - (m + 1))) := by rw [← hrec] _ ≤ n ^ (2 * m + 1) * (n ^ 2 * (n - 2 * (m + 1) - 1).choose (r - (m + 1))) := by gcongr _ = n ^ (2 * (m + 1) + 1) * (n - 2 * (m + 1) - 1).choose (r - (m + 1)) := by rw [show 2 * (m + 1) + 1 = (2 * m + 1) + 2 by omega, pow_add]; ring /-- **Theorem 3.4 (Matching Non-Asymptotic Exponential Lower Bound)**: For ALL `2r < n` and ALL `m ≤ r`: `(n - 2r) * (4 * (r - m + 1) * (n - r - m))^m * I_n(r, 0) ≤ (2m + 1) * n^(2m + 1) * I_n(r, 2m)`. Together with Theorem 3.3, this proves that `4p(1-p) = 4r(n-r)/n^2` is the **exact** exponential decay base of Kanerva's SDM overlap kernel. -/ theorem overlapFormula_exp_lower_bound {n r m : ℕ} (hr : 2 * r < n) (hm : m ≤ r) : (n - 2 * r) * (4 * (r - m + 1) * (n - r - m)) ^ m * overlapFormula n r 0 ≤ (2 * m + 1) * n ^ (2 * m + 1) * overlapFormula n r (2 * m) := by have hterm_le : stepTerm n r m ≤ overlapFormula n r (2 * m) := by rw [overlapFormula_single_sum hr (by omega)] exact Finset.single_le_sum (fun _ _ => Nat.zero_le _) (Finset.mem_Ico.mpr ⟨le_refl m, by omega⟩) have htail := ballVol_tail_bound hr have hcb := centralBinom_lower_bound m have hout := outer_choose_lower hr hm rw [overlapFormula_zero_right] have hpow : (4 * (r - m + 1) * (n - r - m)) ^ m = 4 ^ m * ((r - m + 1) * (n - r - m)) ^ m := by rw [mul_assoc, mul_pow] rw [hpow] calc (n - 2 * r) * (4 ^ m * ((r - m + 1) * (n - r - m)) ^ m) * ballVol n r = 4 ^ m * ((r - m + 1) * (n - r - m)) ^ m * ((n - 2 * r) * ballVol n r) := by ring _ ≤ 4 ^ m * ((r - m + 1) * (n - r - m)) ^ m * ((n - r) * n.choose r) := Nat.mul_le_mul_left _ htail _ = 4 ^ m * ((n - r) * ((r - m + 1) * (n - r - m)) ^ m * n.choose r) := by ring _ ≤ ((2 * m + 1) * (2 * m).choose m) * (n ^ (2 * m + 1) * (n - 2 * m - 1).choose (r - m)) := Nat.mul_le_mul hcb hout _ = (2 * m + 1) * n ^ (2 * m + 1) * stepTerm n r m := by simp only [stepTerm, hm, ↓reduceIte]; ring _ ≤ (2 * m + 1) * n ^ (2 * m + 1) * overlapFormula n r (2 * m) := Nat.mul_le_mul_left _ hterm_le /-! ## Section 4: Sharp Structural Separation from Transformer Softmax Attention -/ /-- **Theorem 4.1 (Short-Distance Log-Concavity at `r = 2`)**: For `r = 2` and every `n ≥ 5`, Kanerva's overlap kernel is strictly log-concave at short distance: `I_n(2, 0) * I_n(2, 4) < I_n(2, 2)^2`. -/ theorem overlapFormula_log_concave_r2 {n : ℕ} (hn : 5 ≤ n) : overlapFormula n 2 0 * overlapFormula n 2 4 < overlapFormula n 2 2 ^ 2 := by have hr : 2 * 2 < n := by omega have hc4 : (4 : ℕ).choose 2 = 6 := by decide have hc2 : (2 : ℕ).choose 1 = 2 := by decide have h4 : overlapFormula n 2 4 = 6 := by rw [show (4 : ℕ) = 2 * 2 by rfl, overlapFormula_single_sum hr (by omega)] simp [stepTerm, hc4] have h2 : overlapFormula n 2 2 = 2 * n := by rw [show (2 : ℕ) = 2 * 1 by rfl, overlapFormula_single_sum hr (by omega)] rw [Finset.sum_eq_sum_Ico_succ_bot (by omega : 1 < 3), Finset.sum_eq_sum_Ico_succ_bot (by omega : 2 < 3)] simp only [Finset.Ico_self, Finset.sum_empty, add_zero, stepTerm, show (1 : ℕ) ≤ 2 by omega, show (2 : ℕ) ≤ 2 by omega, ↓reduceIte] have h1 : (2 * 1).choose 1 = 2 := hc2 have h2' : (2 * 2).choose 2 = 6 := hc4 have h3 : (n - 2 * 1 - 1).choose (2 - 1) = n - 3 := by rw [show 2 - 1 = 1 by rfl, Nat.choose_one_right]; omega have h4' : (n - 2 * 2 - 1).choose (2 - 2) = 1 := by rw [show 2 - 2 = 0 by rfl, Nat.choose_zero_right] rw [h1, h2', h3, h4'] omega have h0 : overlapFormula n 2 0 = (n - 1).choose 2 + 2 * n := by rw [show (0 : ℕ) = 2 * 0 by rfl, overlapFormula_even_step_subcrit hr (by omega)] simp only [Nat.mul_zero, zero_add, h2, stepTerm, Nat.zero_le, ↓reduceIte, Nat.choose_zero_right, one_mul, Nat.sub_zero] ring rw [h0, h2, h4] have hch : 2 * (n - 1).choose 2 = (n - 1) * (n - 2) := by rw [Nat.choose_two_right, Nat.mul_div_cancel' (Nat.even_mul_pred_self (n - 1)).two_dvd, show n - 1 - 1 = n - 2 by omega] have hscaled : 2 * (((n - 1).choose 2 + 2 * n) * 6) < 2 * (2 * n) ^ 2 := by have hmul : 2 * (((n - 1).choose 2 + 2 * n) * 6) = 6 * ((n - 1) * (n - 2)) + 24 * n := by linarith rw [hmul] zify [show 1 ≤ n by omega, show 2 ≤ n by omega] nlinarith omega /-- Opaque algebraic factorization step avoiding kernel `let`-unfolding in degree-12 terms. -/ private lemma log_convex_factor_step (q1 q2 q3 s1 s2 s3 : ℕ) (hq1 : 0 < q1) (hs2 : 0 < s2) (hs3 : 0 < s3) (h2q1 : 2 * q1 < s1) (hred : 4 * q1 * s2 * s3 < (s1 - 2 * q1) * (6 * q2 * s3 + 20 * q2 * q3)) : (2 * q1 * s2 * s3) ^ 2 + (2 * q1 * s2 * s3) * (6 * q1 * q2 * s3 + 20 * q1 * q2 * q3) < (s1 * s2 * s3) * (6 * q1 * q2 * s3 + 20 * q1 * q2 * q3) := by have hF_pos : 0 < q1 * s2 * s3 := Nat.mul_pos (Nat.mul_pos hq1 hs2) hs3 have hmul := Nat.mul_lt_mul_of_pos_left hred hF_pos have hLHS : (q1 * s2 * s3) * (4 * q1 * s2 * s3) = (2 * q1 * s2 * s3) ^ 2 := by ring have hRHS : (q1 * s2 * s3) * ((s1 - 2 * q1) * (6 * q2 * s3 + 20 * q2 * q3)) + (2 * q1 * s2 * s3) * (6 * q1 * q2 * s3 + 20 * q1 * q2 * q3) = (s1 * s2 * s3) * (6 * q1 * q2 * s3 + 20 * q1 * q2 * q3) := by have hle : 2 * q1 ≤ s1 := le_of_lt h2q1 zify [hle] ring omega /-- Fast degree-6 positivity certificate for short-distance log-convexity. -/ private lemma log_convex_poly_pos (a d : ℤ) (ha : 0 ≤ a) (hd : 0 ≤ d) : 4 * (a + 3) * (a + d + 3) * (2 * a + d + 4) * (2 * a + d + 3) * (2 * a + d + 2) * (2 * a + d + 1) < ((2 * a + d + 6) * (2 * a + d + 5) - 2 * (a + 3) * (a + d + 3)) * (6 * (a + 2) * (a + d + 2) * (2 * a + d + 2) * (2 * a + d + 1) + 20 * (a + 2) * (a + 1) * (a + d + 2) * (a + d + 1)) := by have hident : ((2 * a + d + 6) * (2 * a + d + 5) - 2 * (a + 3) * (a + d + 3)) * (6 * (a + 2) * (a + d + 2) * (2 * a + d + 2) * (2 * a + d + 1) + 20 * (a + 2) * (a + 1) * (a + d + 2) * (a + d + 1)) - 4 * (a + 3) * (a + d + 3) * (2 * a + d + 4) * (2 * a + d + 3) * (2 * a + d + 2) * (2 * a + d + 1) = 672 + 1144 * d + 548 * d ^ 2 + 80 * d ^ 3 + 4 * d ^ 4 + 2288 * a + 3184 * a * d + 1344 * a * d ^ 2 + 266 * a * d ^ 3 + 44 * a * d ^ 4 + 2 * a * d ^ 5 + 3184 * a ^ 2 + 3552 * a ^ 2 * d + 1282 * a ^ 2 * d ^ 2 + 252 * a ^ 2 * d ^ 3 + 26 * a ^ 2 * d ^ 4 + 2368 * a ^ 3 + 2032 * a ^ 3 * d + 568 * a ^ 3 * d ^ 2 + 72 * a ^ 3 * d ^ 3 + 1016 * a ^ 4 + 600 * a ^ 4 * d + 96 * a ^ 4 * d ^ 2 + 240 * a ^ 5 + 72 * a ^ 5 * d + 24 * a ^ 6 := by ring have hpos : (0 : ℤ) < 672 + 1144 * d + 548 * d ^ 2 + 80 * d ^ 3 + 4 * d ^ 4 + 2288 * a + 3184 * a * d + 1344 * a * d ^ 2 + 266 * a * d ^ 3 + 44 * a * d ^ 4 + 2 * a * d ^ 5 + 3184 * a ^ 2 + 3552 * a ^ 2 * d + 1282 * a ^ 2 * d ^ 2 + 252 * a ^ 2 * d ^ 3 + 26 * a ^ 2 * d ^ 4 + 2368 * a ^ 3 + 2032 * a ^ 3 * d + 568 * a ^ 3 * d ^ 2 + 72 * a ^ 3 * d ^ 3 + 1016 * a ^ 4 + 600 * a ^ 4 * d + 96 * a ^ 4 * d ^ 2 + 240 * a ^ 5 + 72 * a ^ 5 * d + 24 * a ^ 6 := by positivity linarith /-- Opaque transfer from scaled weights `P0..P3` to step terms `t0..t3`. -/ private lemma log_convex_transfer {P0 P1 P2 P3 t0 t1 t2 t3 : ℕ} (ht3 : 0 < t3) (h0 : P3 * t0 = P0 * t3) (h1 : P3 * t1 = P1 * t3) (h2 : P3 * t2 = P2 * t3) (hcert : P1 ^ 2 + P1 * (P2 + P3) < P0 * (P2 + P3)) : t1 ^ 2 + t1 * (t2 + t3) < t0 * (t2 + t3) := by have hlt : P3 ^ 2 * (t1 ^ 2 + t1 * (t2 + t3)) < P3 ^ 2 * (t0 * (t2 + t3)) := by have hL : P3 ^ 2 * (t1 ^ 2 + t1 * (t2 + t3)) = (P1 ^ 2 + P1 * (P2 + P3)) * t3 ^ 2 := by calc P3 ^ 2 * (t1 ^ 2 + t1 * (t2 + t3)) = (P3 * t1) ^ 2 + (P3 * t1) * (P3 * t2 + P3 * t3) := by ring _ = (P1 * t3) ^ 2 + (P1 * t3) * (P2 * t3 + P3 * t3) := by rw [h1, h2] _ = (P1 ^ 2 + P1 * (P2 + P3)) * t3 ^ 2 := by ring have hR : P3 ^ 2 * (t0 * (t2 + t3)) = (P0 * (P2 + P3)) * t3 ^ 2 := by calc P3 ^ 2 * (t0 * (t2 + t3)) = (P3 * t0) * (P3 * t2 + P3 * t3) := by ring _ = (P0 * t3) * (P2 * t3 + P3 * t3) := by rw [h0, h2] _ = (P0 * (P2 + P3)) * t3 ^ 2 := by ring rw [hL, hR] exact Nat.mul_lt_mul_of_pos_right hcert (Nat.pow_pos ht3) exact Nat.lt_of_mul_lt_mul_left hlt /-- Standalone step-term algebraic certificate with plain parameters `a, d` (no nested `let` unfolding). -/ private lemma log_convex_step_terms (a d t0 t1 t2 t3 : ℕ) (ht3 : 0 < t3) (e0 : (2 * a + d + 6) * (2 * a + d + 5) * t1 = 2 * (a + 3) * (a + d + 3) * t0) (e1 : (2 * a + d + 4) * (2 * a + d + 3) * t2 = 3 * (a + 2) * (a + d + 2) * t1) (e2 : 3 * (2 * a + d + 2) * (2 * a + d + 1) * t3 = 10 * (a + 1) * (a + d + 1) * t2) : t1 ≤ t0 ∧ t1 ^ 2 + t1 * (t2 + t3) < t0 * (t2 + t3) := by have haux : ∀ (q1 q2 q3 s1 s2 s3 : ℕ), 0 < q1 → 0 < q2 → 0 < s2 → 0 < s3 → 2 * q1 < s1 → 4 * q1 * s2 * s3 < (s1 - 2 * q1) * (6 * q2 * s3 + 20 * q2 * q3) → s1 * t1 = 2 * q1 * t0 → s2 * t2 = 3 * q2 * t1 → 3 * s3 * t3 = 10 * q3 * t2 → t1 ≤ t0 ∧ t1 ^ 2 + t1 * (t2 + t3) < t0 * (t2 + t3) := by intro q1 q2 q3 s1 s2 s3 hq1 hq2 hs2 hs3 h2q1 hred he0 he1 he2 have hP2 : (20 * q1 * q2 * q3) * t2 = (6 * q1 * q2 * s3) * t3 := by calc (20 * q1 * q2 * q3) * t2 = (2 * q1 * q2) * (10 * q3 * t2) := by ring _ = (2 * q1 * q2) * (3 * s3 * t3) := by rw [← he2] _ = (6 * q1 * q2 * s3) * t3 := by ring have hP1 : (20 * q1 * q2 * q3) * t1 = (2 * q1 * s2 * s3) * t3 := by have hmul : (3 * q2) * ((20 * q1 * q2 * q3) * t1) = (3 * q2) * ((2 * q1 * s2 * s3) * t3) := by calc (3 * q2) * ((20 * q1 * q2 * q3) * t1) = (20 * q1 * q2 * q3) * (3 * q2 * t1) := by ring _ = (20 * q1 * q2 * q3) * (s2 * t2) := by rw [← he1] _ = s2 * ((20 * q1 * q2 * q3) * t2) := by ring _ = s2 * ((6 * q1 * q2 * s3) * t3) := by rw [hP2] _ = (3 * q2) * ((2 * q1 * s2 * s3) * t3) := by ring exact Nat.eq_of_mul_eq_mul_left (by positivity : 0 < 3 * q2) hmul have hP0 : (20 * q1 * q2 * q3) * t0 = (s1 * s2 * s3) * t3 := by have hmul : (2 * q1) * ((20 * q1 * q2 * q3) * t0) = (2 * q1) * ((s1 * s2 * s3) * t3) := by calc (2 * q1) * ((20 * q1 * q2 * q3) * t0) = (20 * q1 * q2 * q3) * (2 * q1 * t0) := by ring _ = (20 * q1 * q2 * q3) * (s1 * t1) := by rw [← he0] _ = s1 * ((20 * q1 * q2 * q3) * t1) := by ring _ = s1 * ((2 * q1 * s2 * s3) * t3) := by rw [hP1] _ = (2 * q1) * ((s1 * s2 * s3) * t3) := by ring exact Nat.eq_of_mul_eq_mul_left (by positivity : 0 < 2 * q1) hmul have hP1_lt_P0 : 2 * q1 * s2 * s3 < s1 * s2 * s3 := by have hpos : 0 < s2 * s3 := Nat.mul_pos hs2 hs3 calc 2 * q1 * s2 * s3 = (2 * q1) * (s2 * s3) := by ring _ < s1 * (s2 * s3) := Nat.mul_lt_mul_of_pos_right h2q1 hpos _ = s1 * s2 * s3 := by ring have ht1_le : t1 ≤ t0 := by have h1 : (20 * q1 * q2 * q3) * t1 < (20 * q1 * q2 * q3) * t0 := by rw [hP1, hP0]; exact Nat.mul_lt_mul_of_pos_right hP1_lt_P0 ht3 exact le_of_lt (Nat.lt_of_mul_lt_mul_left h1) have hcert := log_convex_factor_step q1 q2 q3 s1 s2 s3 hq1 hs2 hs3 h2q1 hred exact ⟨ht1_le, log_convex_transfer ht3 hP0 hP1 hP2 hcert⟩ have h2q1 : 2 * ((a + 3) * (a + d + 3)) < (2 * a + d + 6) * (2 * a + d + 5) := by nlinarith only [] have hred : 4 * ((a + 3) * (a + d + 3)) * ((2 * a + d + 4) * (2 * a + d + 3)) * ((2 * a + d + 2) * (2 * a + d + 1)) < ((2 * a + d + 6) * (2 * a + d + 5) - 2 * ((a + 3) * (a + d + 3))) * (6 * ((a + 2) * (a + d + 2)) * ((2 * a + d + 2) * (2 * a + d + 1)) + 20 * ((a + 2) * (a + d + 2)) * ((a + 1) * (a + d + 1))) := by have hredZ := log_convex_poly_pos (a : ℤ) (d : ℤ) (Nat.cast_nonneg a) (Nat.cast_nonneg d) have hle : 2 * ((a + 3) * (a + d + 3)) ≤ (2 * a + d + 6) * (2 * a + d + 5) := le_of_lt h2q1 zify [hle] linarith only [hredZ] exact haux ((a + 3) * (a + d + 3)) ((a + 2) * (a + d + 2)) ((a + 1) * (a + d + 1)) ((2 * a + d + 6) * (2 * a + d + 5)) ((2 * a + d + 4) * (2 * a + d + 3)) ((2 * a + d + 2) * (2 * a + d + 1)) (by positivity) (by positivity) (by positivity) (by positivity) h2q1 hred (by linarith only [e0]) (by linarith only [e1]) (by linarith only [e2]) /-- **Theorem 4.2 (Curvature Reversal: Universal Short-Distance Log-Convexity for ALL `3 ≤ r < n/2`)**: For EVERY `n` and EVERY subcritical access radius `3 ≤ r < n/2`, Kanerva's overlap kernel is **strictly log-convex** at short distance: `I_n(r, 2)^2 < I_n(r, 0) * I_n(r, 4)`. This proves an exact non-asymptotic curvature separation from Transformer Softmax Attention `e^{-βd}` (which is log-linear everywhere) and from `r = 2` (which is log-concave). -/ theorem overlapFormula_log_convex_short_distance {n r : ℕ} (hr3 : 3 ≤ r) (hr : 2 * r < n) : overlapFormula n r 2 ^ 2 < overlapFormula n r 0 * overlapFormula n r 4 := by set t0 := stepTerm n r 0 set t1 := stepTerm n r 1 set t2 := stepTerm n r 2 set t3 := stepTerm n r 3 set R := overlapFormula n r 4 have hI2 : overlapFormula n r 2 = R + t1 := by have := overlapFormula_even_step_subcrit hr (show 1 ≤ r by omega) simpa [R, t1] using this have hI0 : overlapFormula n r 0 = R + t1 + t0 := by have := overlapFormula_even_step_subcrit hr (Nat.zero_le r) simpa [hI2, t0] using this have hR_ge : t2 + t3 ≤ R := by have hsum := overlapFormula_single_sum (m := 2) hr (by omega) have hsub : ({2, 3} : Finset ℕ) ⊆ Finset.Ico 2 (r + 1) := by intro x hx simp only [Finset.mem_insert, Finset.mem_singleton] at hx rcases hx with rfl | rfl <;> exact Finset.mem_Ico.mpr (by omega) have hle := Finset.sum_le_sum_of_subset (f := stepTerm n r) hsub rw [Finset.sum_pair (by decide : (2 : ℕ) ≠ 3)] at hle simpa [R, t2, t3, hsum] using hle set a := r - 3 set d := n - 2 * r - 1 have hr0 := stepTerm_succ_mul (n := n) (r := r) (m := 0) (by omega) hr have hr1 := stepTerm_succ_mul (n := n) (r := r) (m := 1) (by omega) hr have hr2 := stepTerm_succ_mul (n := n) (r := r) (m := 2) (by omega) hr have e0 : (2 * a + d + 6) * (2 * a + d + 5) * t1 = 2 * (a + 3) * (a + d + 3) * t0 := by have h1 : n - 2 * 0 - 1 = 2 * a + d + 6 := by omega have h2 : n - 2 * 0 - 2 = 2 * a + d + 5 := by omega have h3 : r - 0 = a + 3 := by omega have h4 : n - r - 0 - 1 = a + d + 3 := by omega rw [h1, h2, h3, h4] at hr0 simpa [t0, t1] using hr0 have e1 : (2 * a + d + 4) * (2 * a + d + 3) * t2 = 3 * (a + 2) * (a + d + 2) * t1 := by have h1 : n - 2 * 1 - 1 = 2 * a + d + 4 := by omega have h2 : n - 2 * 1 - 2 = 2 * a + d + 3 := by omega have h3 : r - 1 = a + 2 := by omega have h4 : n - r - 1 - 1 = a + d + 2 := by omega rw [h1, h2, h3, h4] at hr1 change 2 * (2 * a + d + 4) * (2 * a + d + 3) * t2 = 2 * 3 * (a + 2) * (a + d + 2) * t1 at hr1 linarith have e2 : 3 * (2 * a + d + 2) * (2 * a + d + 1) * t3 = 10 * (a + 1) * (a + d + 1) * t2 := by have h1 : n - 2 * 2 - 1 = 2 * a + d + 2 := by omega have h2 : n - 2 * 2 - 2 = 2 * a + d + 1 := by omega have h3 : r - 2 = a + 1 := by omega have h4 : n - r - 2 - 1 = a + d + 1 := by omega rw [h1, h2, h3, h4] at hr2 simpa [t2, t3] using hr2 have ht3_pos : 0 < t3 := by simp only [t3, stepTerm, hr3, ↓reduceIte] exact Nat.mul_pos (Nat.choose_pos (by omega)) (Nat.choose_pos (by omega)) obtain ⟨ht1_le_t0, ht_cert⟩ := log_convex_step_terms a d t0 t1 t2 t3 ht3_pos e0 e1 e2 rw [hI0, hI2] have hlift : (t0 - t1) * (t2 + t3) ≤ (t0 - t1) * R := Nat.mul_le_mul_left _ hR_ge zify [ht1_le_t0] at hlift ht_cert ⊢ nlinarith only [hlift, ht_cert] /-- **Theorem 4.3 (Long-Distance Log-Concavity at the Support Boundary)**: For every `2r < n`, `I_n(r, 2r - 2) * I_n(r, 2r + 2) < I_n(r, 2r)^2`. Combined with Theorem 4.2, for every `3 ≤ r < n/2`, `log I_n(r, 2m)` exhibits an S-shaped log-convex-to-log-concave transition. -/ theorem overlapFormula_log_concave_support_edge {n r : ℕ} (hr : 2 * r < n) : overlapFormula n r (2 * r - 2) * overlapFormula n r (2 * r + 2) < overlapFormula n r (2 * r) ^ 2 := by rw [overlapFormula_eq_zero_of_two_mul_lt (by omega : 2 * r < 2 * r + 2), mul_zero] exact Nat.pow_pos (overlapFormula_pos_of_le_two_mul (le_refl _) (by omega)) /-- **Theorem 4.4a (Parity-Deadlock Voronoi Basin Dichotomy — Even Shell Captures)**: For `M = 2` stored memories `x₁, x₂` at odd separation `Δ = 4m + 1` and any access radius `m ≤ r` with `r + m + 1 ≤ n`, EVERY query `ξ` in the full Voronoi ball `d_H(ξ, x₁) ≤ 2m` strictly retrieves `x₁` in 1 step for EVERY payload assignment `V`. -/ theorem voronoi_basin_even_shell_captures {n k m r : ℕ} (x₁ x₂ : Word n) (hΔ : hammingDist x₁ x₂ = 4 * m + 1) (hr1 : m ≤ r) (hr2 : r + m + 1 ≤ n) (V : Fin 2 → Word k) (ξ : Word n) (hξ : hammingDist ξ x₁ ≤ 2 * m) : StrictRetrieves Finset.univ r ![x₁, x₂] V ξ (V 0) := by intro j rw [sdmRead_eq_kernel_sum, Fin.sum_univ_two] simp only [Matrix.cons_val_zero, Matrix.cons_val_one] have hd2 : 2 * m + 1 ≤ hammingDist ξ x₂ := by have htri := hammingDist_triangle x₁ ξ x₂ rw [hammingDist_comm x₁ ξ] at htri omega have hd2_le : hammingDist ξ x₂ ≤ n := hammingDist_le_card_fintype.trans (by simp) have hK1 : overlap Finset.univ r ξ x₁ = overlapFormula n r (hammingDist ξ x₁) := fullOverlap_eq_overlapFormula n r ξ x₁ have hK2 : overlap Finset.univ r ξ x₂ = overlapFormula n r (hammingDist ξ x₂) := fullOverlap_eq_overlapFormula n r ξ x₂ have hgt : overlapFormula n r (hammingDist ξ x₂) < overlapFormula n r (hammingDist ξ x₁) := by have hle1 : overlapFormula n r (2 * m) ≤ overlapFormula n r (hammingDist ξ x₁) := overlapFormula_antitone hξ (by omega) have hle2 : overlapFormula n r (hammingDist ξ x₂) ≤ overlapFormula n r (2 * m + 1) := overlapFormula_antitone hd2 hd2_le have hdrop := overlapFormula_step_drop (n := n) (r := r) (m := m) (by omega) have hst_pos : 0 < stepTerm n r m := by simp only [stepTerm, hr1, ↓reduceIte] exact Nat.mul_pos (Nat.choose_pos (by omega)) (Nat.choose_pos (by omega)) omega rw [hK1, hK2] have hself : bipolar (V 0 j) * ((overlapFormula n r (hammingDist ξ x₁) : ℤ) * bipolar (V 0 j)) = (overlapFormula n r (hammingDist ξ x₁) : ℤ) := by calc bipolar (V 0 j) * ((overlapFormula n r (hammingDist ξ x₁) : ℤ) * bipolar (V 0 j)) = (overlapFormula n r (hammingDist ξ x₁) : ℤ) * (bipolar (V 0 j) * bipolar (V 0 j)) := by ring _ = (overlapFormula n r (hammingDist ξ x₁) : ℤ) := by rw [bipolar_mul_self, mul_one] have hother : -(overlapFormula n r (hammingDist ξ x₂) : ℤ) ≤ bipolar (V 0 j) * ((overlapFormula n r (hammingDist ξ x₂) : ℤ) * bipolar (V 1 j)) := by have h1 : -1 ≤ bipolar (V 0 j) * bipolar (V 1 j) := by cases V 0 j <;> cases V 1 j <;> decide have h2 : (0 : ℤ) ≤ (overlapFormula n r (hammingDist ξ x₂) : ℤ) := Nat.cast_nonneg _ have h3 := mul_le_mul_of_nonneg_left h1 h2 linarith rw [mul_add, hself] have hgtZ : (overlapFormula n r (hammingDist ξ x₂) : ℤ) < (overlapFormula n r (hammingDist ξ x₁) : ℤ) := by exact_mod_cast hgt linarith /-- **Theorem 4.4b (Parity-Deadlock Voronoi Basin Dichotomy — Odd Shell Deadlocks for ALL `r`)**: For `M = 2` stored memories at odd separation `Δ = 4m + 3` (`4m + 3 ≤ n`), there EXISTS a query `ξ` strictly inside the Voronoi cell of `x₁` (`d_H(ξ, x₁) = 2m + 1 < 2m + 2 = d_H(ξ, x₂)`) whose Kanerva SDM read-out margin on every differing payload coordinate is **identically zero** for **EVERY** access radius `r : ℕ`. -/ theorem voronoi_basin_odd_shell_deadlock {n k m : ℕ} (hn : 4 * m + 3 ≤ n) : ∃ (x₁ x₂ ξ : Word n), hammingDist x₁ x₂ = 4 * m + 3 ∧ hammingDist ξ x₁ = 2 * m + 1 ∧ hammingDist ξ x₂ = 2 * m + 2 ∧ ∀ (r : ℕ) (V : Fin 2 → Word k) (j : Fin k), V 0 j ≠ V 1 j → sdmRead Finset.univ r ![x₁, x₂] V ξ j = 0 := by let x₁ : Word n := fun _ => false let x₂ : Word n := fun i => decide (i.val < 4 * m + 3) let ξ : Word n := fun i => decide (i.val < 2 * m + 1) have hcard_lt : ∀ t ≤ n, (Finset.univ.filter (fun i : Fin n => i.val < t)).card = t := by intro t ht have hbij : (Finset.univ.filter (fun i : Fin n => i.val < t)).card = (Finset.range t).card := by refine Finset.card_bij (fun i _ => i.val) ?_ ?_ ?_ · intro i hi; simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hi exact Finset.mem_range.mpr hi · intro i _ j _ h; exact Fin.ext h · intro a ha; rw [Finset.mem_range] at ha exact ⟨⟨a, by omega⟩, by simp [ha], rfl⟩ rw [hbij, Finset.card_range] have hd12 : hammingDist x₁ x₂ = 4 * m + 3 := by rw [hammingDist] convert hcard_lt (4 * m + 3) hn using 2 ext i; simp [x₁, x₂] have hdξ1 : hammingDist ξ x₁ = 2 * m + 1 := by rw [hammingDist] convert hcard_lt (2 * m + 1) (by omega) using 2 ext i; simp [x₁, ξ] have hdξ2 : hammingDist ξ x₂ = 2 * m + 2 := by rw [hammingDist] have hf : Finset.univ.filter (fun i : Fin n => ξ i ≠ x₂ i) = Finset.univ.filter (fun i : Fin n => 2 * m + 1 ≤ i.val ∧ i.val < 4 * m + 3) := by ext i simp only [Finset.mem_filter, Finset.mem_univ, true_and, ξ, x₂] by_cases h1 : i.val < 2 * m + 1 <;> by_cases h2 : i.val < 4 * m + 3 <;> simp [h1, h2] <;> omega rw [hf] have hbij : (Finset.univ.filter (fun i : Fin n => 2 * m + 1 ≤ i.val ∧ i.val < 4 * m + 3)).card = (Finset.Ico (2 * m + 1) (4 * m + 3)).card := by refine Finset.card_bij (fun i _ => i.val) ?_ ?_ ?_ · intro i hi; simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hi exact Finset.mem_Ico.mpr hi · intro i _ j _ h; exact Fin.ext h · intro a ha; rw [Finset.mem_Ico] at ha exact ⟨⟨a, by omega⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, ha.1, ha.2⟩, rfl⟩ rw [hbij, Nat.card_Ico]; omega refine ⟨x₁, x₂, ξ, hd12, hdξ1, hdξ2, ?_⟩ intro r V j hne rw [sdmRead_eq_kernel_sum, Fin.sum_univ_two] simp only [Matrix.cons_val_zero, Matrix.cons_val_one] have hK1 : overlap Finset.univ r ξ x₁ = overlapFormula n r (2 * m + 1) := by rw [← hdξ1]; exact fullOverlap_eq_overlapFormula n r ξ x₁ have hK2 : overlap Finset.univ r ξ x₂ = overlapFormula n r (2 * m + 2) := by rw [← hdξ2]; exact fullOverlap_eq_overlapFormula n r ξ x₂ rw [hK1, hK2, overlapFormula_parity_plateau (by omega : 2 * m + 2 ≤ n)] have hsum_bip : bipolar (V 0 j) + bipolar (V 1 j) = 0 := by cases h0 : V 0 j <;> cases h1 : V 1 j <;> simp_all [bipolar] calc (overlapFormula n r (2 * m + 2) : ℤ) * bipolar (V 0 j) + (overlapFormula n r (2 * m + 2) : ℤ) * bipolar (V 1 j) = (overlapFormula n r (2 * m + 2) : ℤ) * (bipolar (V 0 j) + bipolar (V 1 j)) := by ring _ = 0 := by rw [hsum_bip, mul_zero] /-! ## Section 5: Exact Universal Hard-Location Capacity Ceiling (`M(s̄ + γ) ≤ 2H`) & Tightness -/ /-- Double counting: total active incidences equals sum of hard-location write loads. -/ lemma sum_active_eq_sum_writeLoad {n M : ℕ} (A : Finset (Word n)) (r : ℕ) (X : Fin M → Word n) : ∑ μ : Fin M, (activeSet A r (X μ)).card = ∑ a ∈ A, writeLoad A r X a := by have h1 : ∀ μ : Fin M, (activeSet A r (X μ)).card = ∑ a ∈ A, if a ∈ activeSet A r (X μ) then 1 else 0 := by intro μ rw [← Finset.sum_filter, Finset.sum_const, smul_eq_mul, mul_one] congr 1; ext a; simp [activeSet] simp_rw [h1] rw [Finset.sum_comm] refine Finset.sum_congr rfl fun a _ => ?_ unfold writeLoad rw [← Finset.sum_filter, Finset.sum_const, smul_eq_mul, mul_one] /-- Double counting: total off-diagonal overlap equals `∑_{a ∈ A} d(a) * (d(a) - 1)`. -/ lemma sum_offdiag_overlap_eq_sum_load_sq {n M : ℕ} (A : Finset (Word n)) (r : ℕ) (X : Fin M → Word n) : ∑ μ : Fin M, ∑ ν ∈ Finset.univ.erase μ, overlap A r (X μ) (X ν) = ∑ a ∈ A, writeLoad A r X a * (writeLoad A r X a - 1) := by have hpair : ∀ μ ν : Fin M, overlap A r (X μ) (X ν) = ∑ a ∈ A, if a ∈ activeSet A r (X μ) ∧ a ∈ activeSet A r (X ν) then 1 else 0 := by intro μ ν unfold overlap rw [← Finset.sum_filter, Finset.sum_const, smul_eq_mul, mul_one] congr 1; ext a simp only [Finset.mem_inter, Finset.mem_filter, activeSet] tauto have htot : ∑ μ : Fin M, ∑ ν : Fin M, overlap A r (X μ) (X ν) = ∑ a ∈ A, writeLoad A r X a * writeLoad A r X a := by simp_rw [hpair] rw [Finset.sum_comm] have hswap : ∑ ν : Fin M, ∑ μ : Fin M, ∑ a ∈ A, (if a ∈ activeSet A r (X μ) ∧ a ∈ activeSet A r (X ν) then 1 else 0) = ∑ a ∈ A, ∑ μ : Fin M, ∑ ν : Fin M, (if a ∈ activeSet A r (X μ) ∧ a ∈ activeSet A r (X ν) then 1 else 0) := by trans ∑ ν : Fin M, ∑ a ∈ A, ∑ μ : Fin M, (if a ∈ activeSet A r (X μ) ∧ a ∈ activeSet A r (X ν) then 1 else 0) · refine Finset.sum_congr rfl fun ν _ => Finset.sum_comm · rw [Finset.sum_comm] refine Finset.sum_congr rfl fun a _ => Finset.sum_comm rw [hswap] refine Finset.sum_congr rfl fun a _ => ?_ have hprod : ∑ μ : Fin M, ∑ ν : Fin M, (if a ∈ activeSet A r (X μ) ∧ a ∈ activeSet A r (X ν) then (1 : ℕ) else 0) = (∑ μ : Fin M, if a ∈ activeSet A r (X μ) then 1 else 0) * (∑ ν : Fin M, if a ∈ activeSet A r (X ν) then 1 else 0) := by rw [Finset.sum_mul] refine Finset.sum_congr rfl fun μ _ => ?_ by_cases hμ : a ∈ activeSet A r (X μ) <;> simp [hμ] rw [hprod] unfold writeLoad simp only [← Finset.sum_filter, Finset.sum_const, smul_eq_mul, mul_one] have hdiag : ∑ μ : Fin M, overlap A r (X μ) (X μ) = ∑ a ∈ A, writeLoad A r X a := by simp_rw [overlap_self]; exact sum_active_eq_sum_writeLoad A r X have hsplit : ∑ μ : Fin M, ∑ ν : Fin M, overlap A r (X μ) (X ν) = (∑ μ : Fin M, overlap A r (X μ) (X μ)) + (∑ μ : Fin M, ∑ ν ∈ Finset.univ.erase μ, overlap A r (X μ) (X ν)) := by rw [← Finset.sum_add_distrib] refine Finset.sum_congr rfl fun μ _ => ?_ exact (Finset.add_sum_erase _ _ (Finset.mem_univ μ)).symm have hload_split : ∑ a ∈ A, writeLoad A r X a * writeLoad A r X a = (∑ a ∈ A, writeLoad A r X a) + ∑ a ∈ A, writeLoad A r X a * (writeLoad A r X a - 1) := by rw [← Finset.sum_add_distrib] refine Finset.sum_congr rfl fun a _ => ?_ cases hd : writeLoad A r X a with | zero => simp | succ k => simp [Nat.mul_succ]; ring omega /-- **Theorem 5.1 (Exact Margin-Load Conservation Identity)**: For any hard-location set `A ⊆ {0, 1}^n`, radius `r`, and stored addresses `X : Fin M → Word n`, the total diagonal-dominance margin across all `M` memories equals `∑_{a ∈ A} d(a)(2 - d(a))`. -/ theorem margin_load_conservation {n M : ℕ} (A : Finset (Word n)) (r : ℕ) (X : Fin M → Word n) : ∑ μ : Fin M, ((overlap A r (X μ) (X μ) : ℤ) - ∑ ν ∈ Finset.univ.erase μ, (overlap A r (X μ) (X ν) : ℤ)) = ∑ a ∈ A, (writeLoad A r X a : ℤ) * (2 - (writeLoad A r X a : ℤ)) := by rw [Finset.sum_sub_distrib] have h1 : (∑ μ : Fin M, (overlap A r (X μ) (X μ) : ℤ)) = ∑ a ∈ A, (writeLoad A r X a : ℤ) := by have := sum_active_eq_sum_writeLoad A r X simp_rw [overlap_self] exact_mod_cast this have h2 : (∑ μ : Fin M, ∑ ν ∈ Finset.univ.erase μ, (overlap A r (X μ) (X ν) : ℤ)) = ∑ a ∈ A, (writeLoad A r X a : ℤ) * ((writeLoad A r X a : ℤ) - 1) := by have hN := sum_offdiag_overlap_eq_sum_load_sq A r X have hZ : ((∑ μ : Fin M, ∑ ν ∈ Finset.univ.erase μ, overlap A r (X μ) (X ν) : ℕ) : ℤ) = ((∑ a ∈ A, writeLoad A r X a * (writeLoad A r X a - 1) : ℕ) : ℤ) := by exact_mod_cast hN push_cast at hZ rw [hZ] refine Finset.sum_congr rfl fun a _ => ?_ cases hd : writeLoad A r X a with | zero => simp | succ k => push_cast; ring rw [h1, h2, ← Finset.sum_sub_distrib] refine Finset.sum_congr rfl fun a _ => by ring /-- **Theorem 5.2 (Exact Universal Hard-Location Capacity Ceiling: The `2H / (s̄ + γ)` Law)**: For ANY dimension `n`, radius `r`, finite hard-location set `A` of size `H = |A|`, and `M` stored addresses with strict diagonal-dominance margin `γ`, `∑_{μ=1}^M |B_{A,r}(x_μ)| + M * γ ≤ 2 * |A|`. -/ theorem hard_location_capacity_ceiling {n M γ : ℕ} (A : Finset (Word n)) (r : ℕ) (X : Fin M → Word n) (hDD : StrictDiagDom A r X γ) : (∑ μ : Fin M, (activeSet A r (X μ)).card) + M * γ ≤ 2 * A.card := by have hcons := margin_load_conservation A r X have hmarg_ge : (M : ℤ) * (γ : ℤ) ≤ ∑ μ : Fin M, ((overlap A r (X μ) (X μ) : ℤ) - ∑ ν ∈ Finset.univ.erase μ, (overlap A r (X μ) (X ν) : ℤ)) := by have hpt : ∀ μ : Fin M, (γ : ℤ) ≤ (overlap A r (X μ) (X μ) : ℤ) - ∑ ν ∈ Finset.univ.erase μ, (overlap A r (X μ) (X ν) : ℤ) := by intro μ have h := hDD μ have hZ : ((∑ ν ∈ Finset.univ.erase μ, overlap A r (X μ) (X ν) : ℕ) : ℤ) + (γ : ℤ) ≤ (overlap A r (X μ) (X μ) : ℤ) := by exact_mod_cast h push_cast at hZ linarith calc (M : ℤ) * (γ : ℤ) = ∑ _μ : Fin M, (γ : ℤ) := by simp _ ≤ _ := Finset.sum_le_sum fun μ _ => hpt μ have hsum_act : (∑ μ : Fin M, ((activeSet A r (X μ)).card : ℤ)) = ∑ a ∈ A, (writeLoad A r X a : ℤ) := by have h0 : ((∑ μ : Fin M, (activeSet A r (X μ)).card : ℕ) : ℤ) = ((∑ a ∈ A, writeLoad A r X a : ℕ) : ℤ) := by exact_mod_cast sum_active_eq_sum_writeLoad A r X simpa using h0 have hquad : ∀ d : ℤ, d * (2 - d) + d ≤ 2 := by intro d have hnonneg : 0 ≤ (d - 1) * (d - 2) := by rcases le_or_gt d 1 with h | h · exact mul_nonneg_of_nonpos_of_nonpos (by omega) (by omega) · exact mul_nonneg (by omega) (by omega) linarith have htotal : ∑ a ∈ A, ((writeLoad A r X a : ℤ) * (2 - (writeLoad A r X a : ℤ)) + (writeLoad A r X a : ℤ)) ≤ 2 * (A.card : ℤ) := by calc ∑ a ∈ A, ((writeLoad A r X a : ℤ) * (2 - (writeLoad A r X a : ℤ)) + (writeLoad A r X a : ℤ)) ≤ ∑ _a ∈ A, (2 : ℤ) := Finset.sum_le_sum fun a _ => hquad _ _ = 2 * (A.card : ℤ) := by simp [mul_comm] rw [Finset.sum_add_distrib] at htotal have hZ : ((∑ μ : Fin M, (activeSet A r (X μ)).card + M * γ : ℕ) : ℤ) ≤ 2 * (A.card : ℤ) := by push_cast linarith exact_mod_cast hZ /-- **Corollary 5.3 (Private Hard-Location Bound)**: Under `StrictDiagDom A r X γ`, at least `M * γ` hard locations must have write-load `1`, hence `M * γ ≤ |{a ∈ A : d(a) = 1}| ≤ |A|`. -/ theorem hard_location_private_bound {n M γ : ℕ} (A : Finset (Word n)) (r : ℕ) (X : Fin M → Word n) (hDD : StrictDiagDom A r X γ) : M * γ ≤ (A.filter (fun a => writeLoad A r X a = 1)).card ∧ (A.filter (fun a => writeLoad A r X a = 1)).card ≤ A.card := by refine ⟨?_, Finset.card_le_card (Finset.filter_subset _ _)⟩ have hcons := margin_load_conservation A r X have hmarg_ge : (M : ℤ) * (γ : ℤ) ≤ ∑ a ∈ A, (writeLoad A r X a : ℤ) * (2 - (writeLoad A r X a : ℤ)) := by rw [← hcons] calc (M : ℤ) * (γ : ℤ) = ∑ _μ : Fin M, (γ : ℤ) := by simp _ ≤ _ := Finset.sum_le_sum fun μ _ => by have hZ : ((∑ ν ∈ Finset.univ.erase μ, overlap A r (X μ) (X ν) : ℕ) : ℤ) + (γ : ℤ) ≤ (overlap A r (X μ) (X μ) : ℤ) := by exact_mod_cast hDD μ push_cast at hZ; linarith have hpt : ∀ a ∈ A, (writeLoad A r X a : ℤ) * (2 - (writeLoad A r X a : ℤ)) ≤ if writeLoad A r X a = 1 then 1 else 0 := by intro a _ rcases eq_or_ne (writeLoad A r X a) 1 with h1 | hne · simp [h1] · simp only [hne, ↓reduceIte] cases hd : writeLoad A r X a with | zero => simp | succ k => have hk : 1 ≤ k := by omega have hkZ : (1 : ℤ) ≤ (k : ℤ) := by exact_mod_cast hk push_cast nlinarith have hsum_le : ∑ a ∈ A, (writeLoad A r X a : ℤ) * (2 - (writeLoad A r X a : ℤ)) ≤ ((A.filter (fun a => writeLoad A r X a = 1)).card : ℤ) := by calc ∑ a ∈ A, (writeLoad A r X a : ℤ) * (2 - (writeLoad A r X a : ℤ)) ≤ ∑ a ∈ A, (if writeLoad A r X a = 1 then (1 : ℤ) else 0) := Finset.sum_le_sum hpt _ = ((A.filter (fun a => writeLoad A r X a = 1)).card : ℤ) := by rw [← Finset.sum_filter, Finset.sum_const, nsmul_eq_mul, mul_one] exact_mod_cast le_trans hmarg_ge hsum_le /-- **Theorem 5.4 (Exact Sharpness of the `2H / (s̄ + 1)` Ceiling via Real Hamming Balls)**: For EVERY `M : ℕ`, there exists an explicit Hamming-ball SDM `(A, r = 1, X)` in `{0, 1}^M` (using `X μ = e_μ` and `A = {e_μ} ∪ {e_μ ⊕ e_ν : μ < ν}`) achieving: - `2 * |A| = M * (M + 1)`, - `StrictDiagDom A 1 X 1` (hence universal heteroassociative retrieval for any payload width `k ≥ 1`), - exact equality `(∑ μ, |B_{A,1}(X μ)|) + M * 1 = 2 * |A|`. -/ theorem hard_location_capacity_tight_witness (M : ℕ) : ∃ (A : Finset (Word M)) (X : Fin M → Word M), 2 * A.card = M * (M + 1) ∧ StrictDiagDom A 1 X 1 ∧ (∑ μ : Fin M, (activeSet A 1 (X μ)).card) + M * 1 = 2 * A.card := by let e : Fin M → Word M := fun μ i => decide (i = μ) let pairWord : Fin M → Fin M → Word M := fun μ ν i => decide (i = μ ∨ i = ν) let A : Finset (Word M) := Finset.univ.filter (fun a : Word M => (Finset.univ.filter (fun i : Fin M => a i = true)).card = 1 ∨ (Finset.univ.filter (fun i : Fin M => a i = true)).card = 2) have hcardA : A.card = M.choose 1 + M.choose 2 := by let S1 := (Finset.univ : Finset (Fin M)).powersetCard 1 let S2 := (Finset.univ : Finset (Fin M)).powersetCard 2 have hdisj : Disjoint S1 S2 := by rw [Finset.disjoint_left] intro s h1 h2 have := (Finset.mem_powersetCard.mp h1).2 have := (Finset.mem_powersetCard.mp h2).2 omega have hbij : A.card = (S1 ∪ S2).card := by refine Finset.card_bij (fun a _ => Finset.univ.filter (fun i : Fin M => a i = true)) ?_ ?_ ?_ · intro a ha simp only [A, Finset.mem_filter, Finset.mem_univ, true_and] at ha simp only [S1, S2, Finset.mem_union, Finset.mem_powersetCard] rcases ha with h1 | h2 · left; exact ⟨Finset.subset_univ _, h1⟩ · right; exact ⟨Finset.subset_univ _, h2⟩ · intro a1 _ a2 _ heq funext i have hi := Finset.ext_iff.mp heq i simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hi cases h1 : a1 i <;> cases h2 : a2 i <;> simp_all · intro s hs simp only [S1, S2, Finset.mem_union, Finset.mem_powersetCard] at hs let a : Word M := fun i => decide (i ∈ s) have hsupp : Finset.univ.filter (fun i : Fin M => a i = true) = s := by ext i; simp [a] refine ⟨a, ?_, hsupp⟩ simp only [A, Finset.mem_filter, Finset.mem_univ, true_and, hsupp] rcases hs with ⟨_, h1⟩ | ⟨_, h2⟩ · left; exact h1 · right; exact h2 rw [hbij, Finset.card_union_of_disjoint hdisj, Finset.card_powersetCard, Finset.card_powersetCard, Finset.card_univ, Fintype.card_fin] have h2A : 2 * A.card = M * (M + 1) := by rw [hcardA, Nat.choose_one_right, mul_add, Nat.choose_two_right, Nat.mul_div_cancel' (Nat.even_mul_pred_self M).two_dvd] cases M with | zero => simp | succ m => simp; ring have hact_mem : ∀ μ : Fin M, ∀ a : Word M, a ∈ activeSet A 1 (e μ) ↔ a ∈ A ∧ a μ = true := by intro μ a simp only [activeSet, Finset.mem_filter] constructor · rintro ⟨haA, hd⟩ refine ⟨haA, ?_⟩ by_contra hfalse have hfalse' : a μ = false := by cases h : a μ <;> simp_all simp only [A, Finset.mem_filter, Finset.mem_univ, true_and] at haA have hsub : insert μ (Finset.univ.filter (fun i : Fin M => a i = true)) ⊆ Finset.univ.filter (fun i : Fin M => e μ i ≠ a i) := by intro i hi simp only [Finset.mem_insert, Finset.mem_filter, Finset.mem_univ, true_and] at hi ⊢ rcases hi with rfl | hai · simp [e, hfalse'] · have hne : i ≠ μ := by intro h; subst h; simp [hfalse'] at hai simp [e, hne, hai] have hnotmem : μ ∉ Finset.univ.filter (fun i : Fin M => a i = true) := by simp [hfalse'] have hcard := Finset.card_le_card hsub rw [Finset.card_insert_of_notMem hnotmem] at hcard change _ ≤ hammingDist (e μ) a at hcard omega · rintro ⟨haA, htrue⟩ refine ⟨haA, ?_⟩ simp only [A, Finset.mem_filter, Finset.mem_univ, true_and] at haA have hfilt : Finset.univ.filter (fun i : Fin M => e μ i ≠ a i) = (Finset.univ.filter (fun i : Fin M => a i = true)).erase μ := by ext i simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_erase, e] by_cases h : i = μ · subst h; simp [htrue] · cases hai : a i <;> simp [h] rw [hammingDist, hfilt, Finset.card_erase_of_mem (by simp [htrue])] omega have hpair_supp : ∀ μ ν : Fin M, Finset.univ.filter (fun i : Fin M => pairWord μ ν i = true) = {μ, ν} := by intro μ ν; ext i; simp [pairWord] have hpair_mem : ∀ μ ν : Fin M, pairWord μ ν ∈ activeSet A 1 (e μ) := by intro μ ν rw [hact_mem] refine ⟨?_, by simp [pairWord]⟩ simp only [A, Finset.mem_filter, Finset.mem_univ, true_and, hpair_supp] by_cases h : μ = ν · subst h; left; simp · right; exact Finset.card_pair h have hact_card : ∀ μ : Fin M, (activeSet A 1 (e μ)).card = M := by intro μ have hbij : (Finset.univ : Finset (Fin M)).card = (activeSet A 1 (e μ)).card := by refine Finset.card_bij (fun ν _ => pairWord μ ν) (fun ν _ => hpair_mem μ ν) ?_ ?_ · intro ν1 _ ν2 _ heq have h1 := congrFun heq ν1 have h2 := congrFun heq ν2 simp only [pairWord, or_true, decide_true] at h1 h2 by_cases h1μ : ν1 = μ · subst h1μ by_cases h2μ : ν2 = ν1 · exact h2μ.symm · simp [h2μ] at h2 · simp [h1μ] at h1 exact h1 · intro a ha rw [hact_mem] at ha rcases ha with ⟨haA, haμ⟩ simp only [A, Finset.mem_filter, Finset.mem_univ, true_and] at haA let S := Finset.univ.filter (fun i : Fin M => a i = true) have hScard : S.card = 1 ∨ S.card = 2 := haA have hμS : μ ∈ S := by simp [S, haμ] rcases hScard with h1 | h2 · refine ⟨μ, Finset.mem_univ _, ?_⟩ have hS_eq : S = {μ} := Finset.eq_singleton_iff_unique_mem.mpr ⟨hμS, fun x hx => by by_contra hne have hsub : ({μ, x} : Finset (Fin M)) ⊆ S := by intro y hy; simp only [Finset.mem_insert, Finset.mem_singleton] at hy rcases hy with rfl | rfl <;> assumption have := Finset.card_le_card hsub rw [Finset.card_pair (Ne.symm hne)] at this omega⟩ funext i have hi : i ∈ S ↔ i = μ := by rw [hS_eq]; simp simp only [S, Finset.mem_filter, Finset.mem_univ, true_and] at hi cases hai : a i <;> simp_all [pairWord] · have herase : (S.erase μ).card = 1 := by rw [Finset.card_erase_of_mem hμS]; omega obtain ⟨ν, hν⟩ := Finset.card_eq_one.mp herase have hν_mem : ν ∈ S.erase μ := by rw [hν]; simp have hS_eq : S = {μ, ν} := by rw [← Finset.insert_erase hμS, hν] refine ⟨ν, Finset.mem_univ _, ?_⟩ funext i have hi : i ∈ S ↔ i = μ ∨ i = ν := by rw [hS_eq]; simp simp only [S, Finset.mem_filter, Finset.mem_univ, true_and] at hi cases hai : a i <;> simp_all [pairWord] rw [← hbij, Finset.card_univ, Fintype.card_fin] have hoff_one : ∀ μ ν : Fin M, ν ≠ μ → overlap A 1 (e μ) (e ν) = 1 := by intro μ ν hne unfold overlap have heq : activeSet A 1 (e μ) ∩ activeSet A 1 (e ν) = {pairWord μ ν} := by ext a simp only [Finset.mem_inter, hact_mem, Finset.mem_singleton] constructor · rintro ⟨⟨haA, haμ⟩, ⟨_, haν⟩⟩ simp only [A, Finset.mem_filter, Finset.mem_univ, true_and] at haA let S := Finset.univ.filter (fun i : Fin M => a i = true) have hScard : S.card = 1 ∨ S.card = 2 := haA have hsub : ({μ, ν} : Finset (Fin M)) ⊆ S := by intro i hi; simp only [Finset.mem_insert, Finset.mem_singleton] at hi rcases hi with rfl | rfl <;> simp [S, haμ, haν] have hcard_le : 2 ≤ S.card := by have := Finset.card_le_card hsub rwa [Finset.card_pair (Ne.symm hne)] at this have hS_card : S.card = 2 := by omega have hS_eq : S = {μ, ν} := (Finset.eq_of_subset_of_card_le hsub (by rw [Finset.card_pair (Ne.symm hne)]; omega)).symm funext i have hi : i ∈ S ↔ i = μ ∨ i = ν := by rw [hS_eq]; simp simp only [S, Finset.mem_filter, Finset.mem_univ, true_and] at hi cases hai : a i <;> simp_all [pairWord] · rintro rfl have h1 := hpair_mem μ ν rw [hact_mem] at h1 exact ⟨h1, ⟨h1.1, by simp [pairWord]⟩⟩ rw [heq, Finset.card_singleton] refine ⟨A, e, h2A, ?_, ?_⟩ · intro μ rw [overlap_self, hact_card μ] have hsum : ∑ ν ∈ Finset.univ.erase μ, overlap A 1 (e μ) (e ν) = M - 1 := by have hconst : ∑ ν ∈ Finset.univ.erase μ, overlap A 1 (e μ) (e ν) = ∑ _ν ∈ Finset.univ.erase μ, 1 := Finset.sum_congr rfl fun ν hν => hoff_one μ ν (Finset.ne_of_mem_erase hν) rw [hconst, Finset.sum_const, smul_eq_mul, mul_one, Finset.card_erase_of_mem (Finset.mem_univ μ), Finset.card_univ, Fintype.card_fin] rw [hsum] have hMpos : 1 ≤ M := Fin.pos μ omega · simp_rw [hact_card] rw [Finset.sum_const, Finset.card_univ, Fintype.card_fin, smul_eq_mul, h2A] ring /-- **Theorem 5.5 (Hard-Location Blind-Spot & Sphere-Covering Capacity Tradeoff)**: 1. Double-counting incidence identity: `∑_{x ∈ {0,1}^n} |B_{A,r}(x)| = |A| * |B_r|`. 2. Blind-spot existence: if `|A| * |B_r| < 2^n`, there exists an unaddressable word `x` with `activeSet A r x = ∅`. 3. Covering-capacity tradeoff: if every word activates at least `s_min` hard locations, then `s_min * 2^n ≤ |A| * |B_r|` and every `StrictDiagDom A r X γ` configuration obeys `M * (s_min + γ) ≤ 2 * |A|`. -/ theorem sum_activeSet_over_cube {n : ℕ} (A : Finset (Word n)) (r : ℕ) : ∑ x : Word n, (activeSet A r x).card = A.card * ballVol n r := by have h1 : ∀ x : Word n, (activeSet A r x).card = ∑ a ∈ A, if hammingDist x a ≤ r then 1 else 0 := by intro x unfold activeSet rw [← Finset.sum_filter, Finset.sum_const, smul_eq_mul, mul_one] simp_rw [h1] rw [Finset.sum_comm] have h2 : ∀ a ∈ A, (∑ x : Word n, if hammingDist x a ≤ r then 1 else 0) = ballVol n r := by intro a _ rw [← Finset.sum_filter, Finset.sum_const, smul_eq_mul, mul_one] have hball : Finset.univ.filter (fun x : Word n => hammingDist x a ≤ r) = fullBall n r a := by ext x; simp [fullBall, activeSet, hammingDist_comm] rw [hball, fullBall_card_eq_ballVol] rw [Finset.sum_congr rfl h2, Finset.sum_const, smul_eq_mul] theorem blind_spot_of_small_hard_locations {n : ℕ} (A : Finset (Word n)) (r : ℕ) (hsmall : A.card * ballVol n r < 2 ^ n) : ∃ x : Word n, activeSet A r x = ∅ := by by_contra hcov push Not at hcov have hge1 : ∀ x : Word n, 1 ≤ (activeSet A r x).card := fun x => Finset.card_pos.mpr (hcov x) have hsum := sum_activeSet_over_cube A r have hlb : 2 ^ n ≤ ∑ x : Word n, (activeSet A r x).card := by calc 2 ^ n = ∑ _x : Word n, 1 := by simp [Word] _ ≤ ∑ x : Word n, (activeSet A r x).card := Finset.sum_le_sum fun x _ => hge1 x omega theorem covering_capacity_tradeoff {n M s_min γ : ℕ} (A : Finset (Word n)) (r : ℕ) (X : Fin M → Word n) (hcov : ∀ x : Word n, s_min ≤ (activeSet A r x).card) (hDD : StrictDiagDom A r X γ) : s_min * 2 ^ n ≤ A.card * ballVol n r ∧ M * (s_min + γ) ≤ 2 * A.card := by constructor · rw [← sum_activeSet_over_cube A r] calc s_min * 2 ^ n = ∑ _x : Word n, s_min := by simp [Word, mul_comm] _ ≤ ∑ x : Word n, (activeSet A r x).card := Finset.sum_le_sum fun x _ => hcov x · have hceil := hard_location_capacity_ceiling A r X hDD have hact_lb : M * s_min ≤ ∑ μ : Fin M, (activeSet A r (X μ)).card := by calc M * s_min = ∑ _μ : Fin M, s_min := by simp _ ≤ ∑ μ : Fin M, (activeSet A r (X μ)).card := Finset.sum_le_sum fun μ _ => hcov (X μ) have : M * (s_min + γ) = M * s_min + M * γ := mul_add M s_min γ omega /-! ## Section 6: Sharp Critical Access-Radius Phase Transition at `r_c = n / 2` -/ /-- **Theorem 6.1 (Subcritical Macroscopic 1-Step Basin Capture)**: For ANY `M` memories `X : Fin M → Word n` with minimum pairwise Hamming distance `Δ` and any target basin radius `ρ < Δ`: if `(M - 1) * I_n(r, Δ - ρ) < I_n(r, ρ)`, then EVERY query `ξ` within Hamming distance `ρ` of `X μ` strictly retrieves `V μ` in 1 step for EVERY payload `V`. -/ theorem subcritical_basin_capture {n M k r Δ ρ : ℕ} (X : Fin M → Word n) (hsep : ∀ μ ν : Fin M, μ ≠ ν → Δ ≤ hammingDist (X μ) (X ν)) (hΔn : Δ ≤ n) (hρΔ : ρ < Δ) (hcap : (M - 1) * overlapFormula n r (Δ - ρ) < overlapFormula n r ρ) (V : Fin M → Word k) (μ : Fin M) (ξ : Word n) (hξ : hammingDist ξ (X μ) ≤ ρ) : StrictRetrieves Finset.univ r X V ξ (V μ) := by intro j rw [sdmRead_eq_kernel_sum, Finset.mul_sum, ← Finset.add_sum_erase _ _ (Finset.mem_univ μ)] have hself : bipolar (V μ j) * ((overlap Finset.univ r ξ (X μ) : ℤ) * bipolar (V μ j)) = (overlap Finset.univ r ξ (X μ) : ℤ) := by calc bipolar (V μ j) * ((overlap Finset.univ r ξ (X μ) : ℤ) * bipolar (V μ j)) = (overlap Finset.univ r ξ (X μ) : ℤ) * (bipolar (V μ j) * bipolar (V μ j)) := by ring _ = (overlap Finset.univ r ξ (X μ) : ℤ) := by rw [bipolar_mul_self, mul_one] rw [hself] have hKμ : overlapFormula n r ρ ≤ overlap Finset.univ r ξ (X μ) := by have h1 : overlap Finset.univ r ξ (X μ) = overlapFormula n r (hammingDist ξ (X μ)) := fullOverlap_eq_overlapFormula n r ξ (X μ) rw [h1] exact overlapFormula_antitone hξ (by omega) have hKν : ∀ ν ∈ Finset.univ.erase μ, overlap Finset.univ r ξ (X ν) ≤ overlapFormula n r (Δ - ρ) := by intro ν hν have hne : ν ≠ μ := Finset.ne_of_mem_erase hν have h1 : overlap Finset.univ r ξ (X ν) = overlapFormula n r (hammingDist ξ (X ν)) := fullOverlap_eq_overlapFormula n r ξ (X ν) rw [h1] have htri := hammingDist_triangle (X μ) ξ (X ν) rw [hammingDist_comm (X μ) ξ] at htri have hΔle := hsep μ ν hne.symm have hdn : hammingDist ξ (X ν) ≤ n := hammingDist_le_card_fintype.trans (by simp) exact overlapFormula_antitone (by omega) hdn have hsum_le : ∑ ν ∈ Finset.univ.erase μ, overlap Finset.univ r ξ (X ν) ≤ (M - 1) * overlapFormula n r (Δ - ρ) := by calc ∑ ν ∈ Finset.univ.erase μ, overlap Finset.univ r ξ (X ν) ≤ ∑ _ν ∈ Finset.univ.erase μ, overlapFormula n r (Δ - ρ) := Finset.sum_le_sum hKν _ = (Finset.univ.erase μ).card * overlapFormula n r (Δ - ρ) := by rw [Finset.sum_const, smul_eq_mul] _ = (M - 1) * overlapFormula n r (Δ - ρ) := by rw [Finset.card_erase_of_mem (Finset.mem_univ μ), Finset.card_univ, Fintype.card_fin] have hbound : -(∑ ν ∈ Finset.univ.erase μ, (overlap Finset.univ r ξ (X ν) : ℤ)) ≤ ∑ ν ∈ Finset.univ.erase μ, bipolar (V μ j) * ((overlap Finset.univ r ξ (X ν) : ℤ) * bipolar (V ν j)) := by rw [← Finset.sum_neg_distrib] refine Finset.sum_le_sum fun ν _ => ?_ have h1 : -1 ≤ bipolar (V μ j) * bipolar (V ν j) := by cases V μ j <;> cases V ν j <;> decide have h2 : (0 : ℤ) ≤ (overlap Finset.univ r ξ (X ν) : ℤ) := Nat.cast_nonneg _ have h3 := mul_le_mul_of_nonneg_left h1 h2 linarith have hlt : ∑ ν ∈ Finset.univ.erase μ, overlap Finset.univ r ξ (X ν) < overlap Finset.univ r ξ (X μ) := lt_of_le_of_lt hsum_le (lt_of_lt_of_le hcap hKμ) have hltZ : ((∑ ν ∈ Finset.univ.erase μ, overlap Finset.univ r ξ (X ν) : ℕ) : ℤ) < (overlap Finset.univ r ξ (X μ) : ℤ) := by exact_mod_cast hlt push_cast at hltZ linarith /-- **Theorem 6.2 (Subcritical Exponential Deterministic Capacity Law)**: For ANY subcritical access radius `2r < n`, ANY `Δ`-separated memory set `X : Fin M → Word n` (`Δ ≤ n`), letting `m = (Δ + 1) / 2 = ⌈Δ / 2⌉`: if `(M - 1) * (4 * r * (n - r))^m < n^(2m)`, then `StrictDiagDom Finset.univ r X 1` holds (and hence universal 1-step heteroassociative retrieval holds for EVERY payload width `k ≥ 1`). -/ theorem subcritical_exponential_capacity {n M r Δ : ℕ} (hr : 2 * r < n) (hΔn : Δ ≤ n) (X : Fin M → Word n) (hsep : ∀ μ ν : Fin M, μ ≠ ν → Δ ≤ hammingDist (X μ) (X ν)) (hM : (M - 1) * (4 * r * (n - r)) ^ ((Δ + 1) / 2) < n ^ (2 * ((Δ + 1) / 2))) : StrictDiagDom Finset.univ r X 1 := by intro μ have hself : overlap Finset.univ r (X μ) (X μ) = overlapFormula n r 0 := by have := fullOverlap_eq_overlapFormula n r (X μ) (X μ) rw [hammingDist_self] at this exact this have hI0_pos : 0 < overlapFormula n r 0 := overlapFormula_pos_of_le_two_mul (Nat.zero_le _) (Nat.zero_le _) have hKν : ∀ ν ∈ Finset.univ.erase μ, overlap Finset.univ r (X μ) (X ν) ≤ overlapFormula n r Δ := by intro ν hν have h1 : overlap Finset.univ r (X μ) (X ν) = overlapFormula n r (hammingDist (X μ) (X ν)) := fullOverlap_eq_overlapFormula n r (X μ) (X ν) rw [h1] exact overlapFormula_antitone (hsep μ ν (Finset.ne_of_mem_erase hν).symm) (hammingDist_le_card_fintype.trans (by simp)) have hsum : ∑ ν ∈ Finset.univ.erase μ, overlap Finset.univ r (X μ) (X ν) ≤ (M - 1) * overlapFormula n r Δ := by calc ∑ ν ∈ Finset.univ.erase μ, overlap Finset.univ r (X μ) (X ν) ≤ ∑ _ν ∈ Finset.univ.erase μ, overlapFormula n r Δ := Finset.sum_le_sum hKν _ = (M - 1) * overlapFormula n r Δ := by rw [Finset.sum_const, smul_eq_mul, Finset.card_erase_of_mem (Finset.mem_univ μ), Finset.card_univ, Fintype.card_fin] have hdecay := overlapFormula_exp_decay_any hr hΔn set m := (Δ + 1) / 2 have hlt : (M - 1) * overlapFormula n r Δ < overlapFormula n r 0 := by have h1 : n ^ (2 * m) * ((M - 1) * overlapFormula n r Δ) < n ^ (2 * m) * overlapFormula n r 0 := by calc n ^ (2 * m) * ((M - 1) * overlapFormula n r Δ) = (M - 1) * (n ^ (2 * m) * overlapFormula n r Δ) := by ring _ ≤ (M - 1) * ((4 * r * (n - r)) ^ m * overlapFormula n r 0) := Nat.mul_le_mul_left _ hdecay _ = ((M - 1) * (4 * r * (n - r)) ^ m) * overlapFormula n r 0 := by ring _ < n ^ (2 * m) * overlapFormula n r 0 := Nat.mul_lt_mul_of_pos_right hM hI0_pos exact Nat.lt_of_mul_lt_mul_left h1 rw [hself] omega /-- **Theorem 6.3 (Greedy Gilbert-Varshamov Packing Existence on `{0, 1}^n`)**: For ANY `n, M` and `Δ ≥ 1`, whenever `(M - 1) * ballVol n (Δ - 1) < 2^n`, there EXISTS a `Δ`-separated memory set `X : Fin M → Word n`. Together with Theorem 6.2, this proves existence of exponentially many (`M = exp(Ω(n))`) universally retrievable memories for EVERY fixed subcritical ratio `r / n = p < 1/2`. -/ theorem gilbert_varshamov_packing {n M Δ : ℕ} (hΔ : 1 ≤ Δ) (hGV : (M - 1) * ballVol n (Δ - 1) < 2 ^ n) : ∃ X : Fin M → Word n, ∀ μ ν : Fin M, μ ≠ ν → Δ ≤ hammingDist (X μ) (X ν) := by induction M with | zero => exact ⟨Fin.elim0, fun μ => Fin.elim0 μ⟩ | succ m ih => simp only [Nat.add_sub_cancel] at hGV by_cases hm0 : m = 0 · subst hm0 exact ⟨fun _ => fun _ => false, fun μ ν hne => absurd (Fin.ext (by omega)) hne⟩ · have hGV' : (m - 1) * ballVol n (Δ - 1) < 2 ^ n := by have hle : (m - 1) * ballVol n (Δ - 1) ≤ m * ballVol n (Δ - 1) := Nat.mul_le_mul_right _ (Nat.sub_le m 1) omega obtain ⟨X₀, hX₀⟩ := ih hGV' let U : Finset (Word n) := Finset.univ.biUnion (fun μ : Fin m => fullBall n (Δ - 1) (X₀ μ)) have hU_card : U.card < (Finset.univ : Finset (Word n)).card := by have hle : U.card ≤ ∑ μ : Fin m, (fullBall n (Δ - 1) (X₀ μ)).card := Finset.card_biUnion_le have hvol : ∑ μ : Fin m, (fullBall n (Δ - 1) (X₀ μ)).card = m * ballVol n (Δ - 1) := by simp_rw [fullBall_card_eq_ballVol] rw [Finset.sum_const, Finset.card_univ, Fintype.card_fin, smul_eq_mul] have huniv : (Finset.univ : Finset (Word n)).card = 2 ^ n := by simp [Word] omega have hne_compl : (Finset.univ \ U).Nonempty := by rw [Finset.nonempty_iff_ne_empty] intro hemp have hsub : (Finset.univ : Finset (Word n)) ⊆ U := Finset.sdiff_eq_empty_iff_subset.mp hemp have := Finset.card_le_card hsub omega obtain ⟨w, hw⟩ := hne_compl have hw_notU : w ∉ U := (Finset.mem_sdiff.mp hw).2 have hw_far : ∀ μ : Fin m, Δ ≤ hammingDist (X₀ μ) w := by intro μ have hnot : w ∉ fullBall n (Δ - 1) (X₀ μ) := fun hmem => hw_notU (Finset.mem_biUnion.mpr ⟨μ, Finset.mem_univ μ, hmem⟩) simp only [fullBall, activeSet, Finset.mem_filter, Finset.mem_univ, true_and, not_le] at hnot omega let X : Fin (m + 1) → Word n := fun μ => if h : μ.val < m then X₀ ⟨μ.val, h⟩ else w refine ⟨X, fun μ ν hne => ?_⟩ by_cases hμ : μ.val < m <;> by_cases hν : ν.val < m · simp only [X, hμ, hν, ↓reduceDIte] exact hX₀ ⟨μ.val, hμ⟩ ⟨ν.val, hν⟩ (fun h => hne (Fin.ext (by simpa using congrArg Fin.val h))) · simp only [X, hμ, hν, ↓reduceDIte] exact hw_far ⟨μ.val, hμ⟩ · simp only [X, hμ, hν, ↓reduceDIte] rw [hammingDist_comm] exact hw_far ⟨ν.val, hν⟩ · exact absurd (Fin.ext (by omega)) hne /-- **Theorem 6.4 (Exact Full-Cube Universal Capacity Ceiling)**: For EVERY `n, r, M, γ`, any `StrictDiagDom Finset.univ r X γ` configuration satisfies `M * (|B_r| + γ) ≤ 2 * 2^n` (never exceeding twice the Hamming sphere-packing bound!). -/ theorem fullCube_capacity_ceiling {n M γ : ℕ} (r : ℕ) (X : Fin M → Word n) (hDD : StrictDiagDom Finset.univ r X γ) : M * (ballVol n r + γ) ≤ 2 * 2 ^ n := by have h := hard_location_capacity_ceiling Finset.univ r X hDD have hact : ∑ μ : Fin M, (activeSet Finset.univ r (X μ)).card = M * ballVol n r := by have heq : ∀ μ : Fin M, (activeSet Finset.univ r (X μ)).card = ballVol n r := fun μ => fullBall_card_eq_ballVol n r (X μ) simp_rw [heq] rw [Finset.sum_const, Finset.card_univ, Fintype.card_fin, smul_eq_mul] have huniv : (Finset.univ : Finset (Word n)).card = 2 ^ n := by simp [Word] rw [hact, huniv] at h linarith /-- **Theorem 6.5 (Sharp Supercritical Collapse to `M ≤ 3` at `2r ≥ n - 1`)**: The instant `r ≥ (n - 1) / 2` (i.e., `n - 1 ≤ 2r`), universal exact heteroassociative retrieval collapses abruptly from exponential capacity `exp(Ω(n))` to at most **3 memories** (`M ≤ 3`). -/ theorem supercritical_collapse_le_three {n M r : ℕ} (hn : 1 ≤ n) (hr : n - 1 ≤ 2 * r) (X : Fin M → Word n) (hDD : StrictDiagDom Finset.univ r X 1) : M ≤ 3 := by have hceil := fullCube_capacity_ceiling r X hDD have hvol : 2 ^ (n - 1) ≤ ballVol n r := by by_cases hrn : r < n · have hcomp := ballVol_add_compl hrn have hmono : ballVol n (n - 1 - r) ≤ ballVol n r := ballVol_mono (by omega) have hpow : 2 ^ n = 2 * 2 ^ (n - 1) := by conv_lhs => rw [show n = (n - 1) + 1 by omega, pow_succ'] omega · rw [ballVol_eq_two_pow (by omega)] exact Nat.pow_le_pow_right (by decide) (Nat.sub_le n 1) have hpow : 2 ^ n = 2 * 2 ^ (n - 1) := by conv_lhs => rw [show n = (n - 1) + 1 by omega, pow_succ'] have h1 : M * (2 ^ (n - 1) + 1) ≤ 4 * 2 ^ (n - 1) := by have hmul : M * (2 ^ (n - 1) + 1) ≤ M * (ballVol n r + 1) := Nat.mul_le_mul_left M (by omega) omega by_contra hM4 have h4 : 4 ≤ M := by omega have h2 : 4 * (2 ^ (n - 1) + 1) ≤ M * (2 ^ (n - 1) + 1) := Nat.mul_le_mul_right _ h4 omega /-- **Theorem 6.6 (Exact Sharpness of `M ≤ 3`: Explicit `M = 3` Witness at `n = 5, r = 2`)**: At `n = 5, r = 2` (right on the critical boundary `2r = n - 1`), `M = 3` memories achieve strict diagonal dominance (`16 > 6 + 6 = 12`), proving `M ≤ 3` cannot be improved to `M ≤ 2`. -/ theorem supercritical_three_witness_n5_r2 : ∃ X : Fin 3 → Word 5, StrictDiagDom Finset.univ 2 X 1 := by let x0 : Word 5 := fun _ => false let x1 : Word 5 := fun i => decide (i.val < 3) let x2 : Word 5 := fun i => decide (2 ≤ i.val) let X : Fin 3 → Word 5 := fun μ => match μ with | ⟨0, _⟩ => x0 | ⟨1, _⟩ => x1 | ⟨2, _⟩ => x2 have hd01 : hammingDist x0 x1 = 3 := by decide have hd02 : hammingDist x0 x2 = 3 := by decide have hd12 : hammingDist x1 x2 = 4 := by decide have hI0 : overlapFormula 5 2 0 = 16 := by decide have hI3 : overlapFormula 5 2 3 = 6 := by decide have hI4 : overlapFormula 5 2 4 = 6 := by decide refine ⟨X, fun μ => ?_⟩ have hK : ∀ a b : Fin 3, overlap Finset.univ 2 (X a) (X b) = overlapFormula 5 2 (hammingDist (X a) (X b)) := fun a b => fullOverlap_eq_overlapFormula 5 2 (X a) (X b) have he0 : (Finset.univ : Finset (Fin 3)).erase 0 = {1, 2} := by decide have he1 : (Finset.univ : Finset (Fin 3)).erase 1 = {0, 2} := by decide have he2 : (Finset.univ : Finset (Fin 3)).erase 2 = {0, 1} := by decide match μ with | ⟨0, _⟩ => change (∑ ν ∈ Finset.univ.erase (0 : Fin 3), overlap Finset.univ 2 (X 0) (X ν)) + 1 ≤ overlap Finset.univ 2 (X 0) (X 0) rw [he0, Finset.sum_pair (by decide), hK 0 1, hK 0 2, hK 0 0] change overlapFormula 5 2 (hammingDist x0 x1) + overlapFormula 5 2 (hammingDist x0 x2) + 1 ≤ overlapFormula 5 2 (hammingDist x0 x0) rw [hammingDist_self, hd01, hd02, hI0, hI3]; decide | ⟨1, _⟩ => change (∑ ν ∈ Finset.univ.erase (1 : Fin 3), overlap Finset.univ 2 (X 1) (X ν)) + 1 ≤ overlap Finset.univ 2 (X 1) (X 1) rw [he1, Finset.sum_pair (by decide), hK 1 0, hK 1 2, hK 1 1] change overlapFormula 5 2 (hammingDist x1 x0) + overlapFormula 5 2 (hammingDist x1 x2) + 1 ≤ overlapFormula 5 2 (hammingDist x1 x1) rw [hammingDist_self, hammingDist_comm x1 x0, hd01, hd12, hI0, hI3, hI4]; decide | ⟨2, _⟩ => change (∑ ν ∈ Finset.univ.erase (2 : Fin 3), overlap Finset.univ 2 (X 2) (X ν)) + 1 ≤ overlap Finset.univ 2 (X 2) (X 2) rw [he2, Finset.sum_pair (by decide), hK 2 0, hK 2 1, hK 2 2] change overlapFormula 5 2 (hammingDist x2 x0) + overlapFormula 5 2 (hammingDist x2 x1) + 1 ≤ overlapFormula 5 2 (hammingDist x2 x2) rw [hammingDist_self, hammingDist_comm x2 x0, hammingDist_comm x2 x1, hd02, hd12, hI0, hI3, hI4]; decide /-- **Theorem 6.7 (Supercritical Collapse to `M ≤ 2` and Complete Amnesia `M ≤ 1`)**: 1. Once `2 * 2^n ≤ 3 * ballVol n r`, `M ≤ 2` universally. 2. Once `r ≥ n`, `M ≤ 1` universally (complete amnesia). -/ theorem supercritical_collapse_le_two {n M r : ℕ} (hr : 2 * 2 ^ n ≤ 3 * ballVol n r) (X : Fin M → Word n) (hDD : StrictDiagDom Finset.univ r X 1) : M ≤ 2 := by have hceil := fullCube_capacity_ceiling r X hDD by_contra hM3 have h3 : 3 ≤ M := by omega have hmul : 3 * (ballVol n r + 1) ≤ M * (ballVol n r + 1) := Nat.mul_le_mul_right _ h3 omega theorem full_amnesia_r_eq_n {n M r : ℕ} (hr : n ≤ r) (X : Fin M → Word n) (hDD : StrictDiagDom Finset.univ r X 1) : M ≤ 1 := by have hceil := fullCube_capacity_ceiling r X hDD rw [ballVol_eq_two_pow hr] at hceil by_contra hM2 have h2 : 2 ≤ M := by omega have hmul : 2 * (2 ^ n + 1) ≤ M * (2 ^ n + 1) := Nat.mul_le_mul_right _ h2 omega /-! ## Section 7: Exact Spurious 3-Mixture Attractor Phase Transition in Autoassociative SDM -/ /-- Three disjoint-support equidistant binary patterns of Hamming weight `d` in `{0, 1}^n` (`3 * d ≤ n`), with coordinatewise majority `z = 0^n`. -/ def threeEquidistant (n d : ℕ) : Fin 3 → Word n := ![fun i => decide (i.val < d), fun i => decide (d ≤ i.val ∧ i.val < 2 * d), fun i => decide (2 * d ≤ i.val ∧ i.val < 3 * d)] /-- Coordinatewise majority of `threeEquidistant n d`: the all-false word `0^n`. -/ def majorityCenter (n : ℕ) : Word n := fun _ => false private lemma card_fin_Ico {n a b : ℕ} (_hab : a ≤ b) (hbn : b ≤ n) : (Finset.univ.filter (fun i : Fin n => a ≤ i.val ∧ i.val < b)).card = b - a := by have hbij : (Finset.univ.filter (fun i : Fin n => a ≤ i.val ∧ i.val < b)).card = (Finset.Ico a b).card := by refine Finset.card_bij (fun i _ => i.val) ?_ ?_ ?_ · intro i hi; simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hi exact Finset.mem_Ico.mpr hi · intro i _ j _ h; exact Fin.ext h · intro x hx; rw [Finset.mem_Ico] at hx exact ⟨⟨x, by omega⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, hx.1, hx.2⟩, rfl⟩ rw [hbij, Nat.card_Ico] lemma threeEquidistant_dist_center {n d : ℕ} (h3d : 3 * d ≤ n) (μ : Fin 3) : hammingDist (majorityCenter n) (threeEquidistant n d μ) = d := by fin_cases μ · change (Finset.univ.filter (fun i : Fin n => majorityCenter n i ≠ threeEquidistant n d 0 i)).card = d have hf : Finset.univ.filter (fun i : Fin n => majorityCenter n i ≠ threeEquidistant n d 0 i) = Finset.univ.filter (fun i : Fin n => 0 ≤ i.val ∧ i.val < d) := by ext i; simp [majorityCenter, threeEquidistant] rw [hf, card_fin_Ico (Nat.zero_le d) (by omega), Nat.sub_zero] · change (Finset.univ.filter (fun i : Fin n => majorityCenter n i ≠ threeEquidistant n d 1 i)).card = d have hf : Finset.univ.filter (fun i : Fin n => majorityCenter n i ≠ threeEquidistant n d 1 i) = Finset.univ.filter (fun i : Fin n => d ≤ i.val ∧ i.val < 2 * d) := by ext i; simp [majorityCenter, threeEquidistant] rw [hf, card_fin_Ico (by omega) (by omega)]; omega · change (Finset.univ.filter (fun i : Fin n => majorityCenter n i ≠ threeEquidistant n d 2 i)).card = d have hf : Finset.univ.filter (fun i : Fin n => majorityCenter n i ≠ threeEquidistant n d 2 i) = Finset.univ.filter (fun i : Fin n => 2 * d ≤ i.val ∧ i.val < 3 * d) := by ext i; simp [majorityCenter, threeEquidistant] rw [hf, card_fin_Ico (by omega) (by omega)]; omega /-- **Theorem 7.1 (Exact Existence Phase Transition of Spurious 3-Mixture Fixed Points)**: For autoassociative Kanerva SDM storing three equidistant patterns at pairwise distance `2d` (`1 ≤ d`, `3d ≤ n`), their majority state `z = 0^n` is an exact strict fixed point if and only if `d ≤ 2r` (exact threshold `r = ⌈d / 2⌉`). -/ theorem spurious_three_mixture_fixed_iff {n d r : ℕ} (hd : 1 ≤ d) (h3d : 3 * d ≤ n) : StrictRetrieves Finset.univ r (threeEquidistant n d) (threeEquidistant n d) (majorityCenter n) (majorityCenter n) ↔ d ≤ 2 * r := by have hread : ∀ j : Fin n, bipolar (majorityCenter n j) * sdmRead Finset.univ r (threeEquidistant n d) (threeEquidistant n d) (majorityCenter n) j = (overlapFormula n r d : ℤ) * (-(bipolar (threeEquidistant n d 0 j) + bipolar (threeEquidistant n d 1 j) + bipolar (threeEquidistant n d 2 j))) := by intro j rw [sdmRead_eq_kernel_sum, Fin.sum_univ_three] have hK : ∀ μ : Fin 3, overlap Finset.univ r (majorityCenter n) (threeEquidistant n d μ) = overlapFormula n r d := by intro μ have := fullOverlap_eq_overlapFormula n r (majorityCenter n) (threeEquidistant n d μ) rw [threeEquidistant_dist_center h3d μ] at this exact this rw [hK 0, hK 1, hK 2] have hz : bipolar (majorityCenter n j) = -1 := rfl rw [hz] ring have hsum_neg : ∀ j : Fin n, -(bipolar (threeEquidistant n d 0 j) + bipolar (threeEquidistant n d 1 j) + bipolar (threeEquidistant n d 2 j)) = 1 ∨ -(bipolar (threeEquidistant n d 0 j) + bipolar (threeEquidistant n d 1 j) + bipolar (threeEquidistant n d 2 j)) = 3 := by intro j simp only [threeEquidistant, Matrix.cons_val_zero, Matrix.cons_val_one, Matrix.cons_val_two, bipolar] by_cases h1 : j.val < d · left; have h2 : ¬ (d ≤ j.val ∧ j.val < 2 * d) := by omega have h3 : ¬ (2 * d ≤ j.val ∧ j.val < 3 * d) := by omega simp [h1, h2, h3] · by_cases h2 : j.val < 2 * d · left; have h2' : d ≤ j.val ∧ j.val < 2 * d := ⟨by omega, h2⟩ have h3 : ¬ (2 * d ≤ j.val ∧ j.val < 3 * d) := by omega simp [h1, h2', h3] · by_cases h3 : j.val < 3 * d · left; have h2' : ¬ (d ≤ j.val ∧ j.val < 2 * d) := by omega have h3' : 2 * d ≤ j.val ∧ j.val < 3 * d := ⟨by omega, h3⟩ simp [h1, h2', h3'] · right; have h2' : ¬ (d ≤ j.val ∧ j.val < 2 * d) := by omega have h3' : ¬ (2 * d ≤ j.val ∧ j.val < 3 * d) := by omega simp [h1, h2', h3'] constructor · intro hret have h0 := hret ⟨0, by omega⟩ rw [hread] at h0 have hpos : 0 < overlapFormula n r d := by rcases hsum_neg ⟨0, by omega⟩ with h | h <;> rw [h] at h0 <;> omega by_contra hlt have hz := overlapFormula_eq_zero_of_two_mul_lt (n := n) (r := r) (d := d) (by omega) omega · intro hle j rw [hread] have hpos : (0 : ℤ) < (overlapFormula n r d : ℤ) := by exact_mod_cast overlapFormula_pos_of_le_two_mul hle (by omega) rcases hsum_neg j with h | h <;> rw [h] <;> linarith /-- Auxiliary: flipping a single bit `p` of `majorityCenter n` changes distance to `x` by `±1`. -/ lemma hammingDist_singleFlip {n : ℕ} (p : Fin n) (x : Word n) : let ξ : Word n := fun i => decide (i = p) hammingDist ξ x = if x p = true then hammingDist (majorityCenter n) x - 1 else hammingDist (majorityCenter n) x + 1 := by intro ξ rw [hammingDist, hammingDist] by_cases hxp : x p = true · simp only [hxp, ↓reduceIte] have hfilt : Finset.univ.filter (fun i : Fin n => ξ i ≠ x i) = (Finset.univ.filter (fun i : Fin n => majorityCenter n i ≠ x i)).erase p := by ext i simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_erase, ξ, majorityCenter] by_cases hip : i = p · subst hip; simp [hxp] · simp [hip] rw [hfilt, Finset.card_erase_of_mem (by simp [majorityCenter, hxp])] · have hxpf : x p = false := by cases h : x p <;> simp_all simp only [hxpf, Bool.false_eq_true, ↓reduceIte] have hfilt : Finset.univ.filter (fun i : Fin n => ξ i ≠ x i) = insert p (Finset.univ.filter (fun i : Fin n => majorityCenter n i ≠ x i)) := by ext i simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert, ξ, majorityCenter] by_cases hip : i = p · subst hip; simp [hxpf] · simp [hip] rw [hfilt, Finset.card_insert_of_notMem (by simp [majorityCenter, hxpf])] /-- **Theorem 7.2 (Exact IFF 1-Bit Basin Stability Criterion for Spurious 3-Mixtures)**: For `1 ≤ d` and `3d ≤ n`, the spurious majority state `z = 0^n` strictly captures its entire 1-bit Hamming neighborhood `B_1(z)` in 1 autoassociative Kanerva SDM step if and only if `I_n(r, d - 1) < 2 * I_n(r, d + 1)`. -/ theorem spurious_three_mixture_basin_iff {n d r : ℕ} (hd : 1 ≤ d) (h3d : 3 * d ≤ n) : (∀ ξ : Word n, hammingDist ξ (majorityCenter n) ≤ 1 → StrictRetrieves Finset.univ r (threeEquidistant n d) (threeEquidistant n d) ξ (majorityCenter n)) ↔ overlapFormula n r (d - 1) < 2 * overlapFormula n r (d + 1) := by let X := threeEquidistant n d let z := majorityCenter n constructor · intro hcap let p0 : Fin n := ⟨0, by omega⟩ let ξ0 : Word n := fun i => decide (i = p0) have hdist0 : hammingDist ξ0 (majorityCenter n) = 1 := by rw [hammingDist] have hf : Finset.univ.filter (fun i : Fin n => ξ0 i ≠ majorityCenter n i) = {p0} := by ext i; simp [ξ0, majorityCenter] rw [hf, Finset.card_singleton] have hret := hcap ξ0 (by omega) p0 rw [sdmRead_eq_kernel_sum, Fin.sum_univ_three] at hret have hx0 : threeEquidistant n d 0 p0 = true := by simp [threeEquidistant, p0]; omega have hx1 : threeEquidistant n d 1 p0 = false := by simp [threeEquidistant, p0] have hx2 : threeEquidistant n d 2 p0 = false := by simp [threeEquidistant, p0] have hd0 : hammingDist ξ0 (threeEquidistant n d 0) = d - 1 := by have h := hammingDist_singleFlip p0 (threeEquidistant n d 0) dsimp only at h rw [threeEquidistant_dist_center h3d 0, hx0] at h simpa using h have hd1 : hammingDist ξ0 (threeEquidistant n d 1) = d + 1 := by have h := hammingDist_singleFlip p0 (threeEquidistant n d 1) dsimp only at h rw [threeEquidistant_dist_center h3d 1, hx1] at h simpa using h have hd2 : hammingDist ξ0 (threeEquidistant n d 2) = d + 1 := by have h := hammingDist_singleFlip p0 (threeEquidistant n d 2) dsimp only at h rw [threeEquidistant_dist_center h3d 2, hx2] at h simpa using h have hK0 : overlap Finset.univ r ξ0 (threeEquidistant n d 0) = overlapFormula n r (d - 1) := by rw [← hd0]; exact fullOverlap_eq_overlapFormula n r ξ0 (threeEquidistant n d 0) have hK1 : overlap Finset.univ r ξ0 (threeEquidistant n d 1) = overlapFormula n r (d + 1) := by rw [← hd1]; exact fullOverlap_eq_overlapFormula n r ξ0 (threeEquidistant n d 1) have hK2 : overlap Finset.univ r ξ0 (threeEquidistant n d 2) = overlapFormula n r (d + 1) := by rw [← hd2]; exact fullOverlap_eq_overlapFormula n r ξ0 (threeEquidistant n d 2) have hb0 : bipolar (threeEquidistant n d 0 p0) = 1 := by rw [hx0]; rfl have hb1 : bipolar (threeEquidistant n d 1 p0) = -1 := by rw [hx1]; rfl have hb2 : bipolar (threeEquidistant n d 2 p0) = -1 := by rw [hx2]; rfl have hbz : bipolar (majorityCenter n p0) = -1 := rfl rw [hK0, hK1, hK2, hb0, hb1, hb2, hbz] at hret have hltZ : (overlapFormula n r (d - 1) : ℤ) < 2 * (overlapFormula n r (d + 1) : ℤ) := by linarith exact_mod_cast hltZ · intro hstab ξ hξ have hIp_pos : 0 < overlapFormula n r (d + 1) := by omega have hd1_le_2r : d + 1 ≤ 2 * r := by by_contra hgt have hz := overlapFormula_eq_zero_of_two_mul_lt (n := n) (r := r) (d := d + 1) (by omega) omega have hIm_pos : 0 < overlapFormula n r (d - 1) := overlapFormula_pos_of_le_two_mul (by omega) (by omega) rcases Nat.eq_zero_or_pos (hammingDist ξ (majorityCenter n)) with h0 | h1 · have hξz : ξ = majorityCenter n := (hammingDist_eq_zero).mp h0 subst hξz exact (spurious_three_mixture_fixed_iff hd h3d).mpr (by omega) · have hdist1 : hammingDist ξ (majorityCenter n) = 1 := by omega rw [hammingDist] at hdist1 obtain ⟨p, hp⟩ := Finset.card_eq_one.mp hdist1 have hξ_eq : ξ = fun i => decide (i = p) := by funext i have hi : ξ i ≠ majorityCenter n i ↔ i = p := by have := Finset.ext_iff.mp hp i simpa using this by_cases hip : i = p · subst hip have hne : ξ i ≠ false := hi.mpr rfl cases h : ξ i <;> simp_all · have heq : ξ i = false := by by_contra hne; exact hip (hi.mp hne) simp [heq, hip] subst hξ_eq intro j rw [sdmRead_eq_kernel_sum, Fin.sum_univ_three] have hbz : bipolar (majorityCenter n j) = -1 := rfl rw [hbz] have hK_eval : ∀ μ : Fin 3, overlap Finset.univ r (fun i => decide (i = p)) (threeEquidistant n d μ) = if threeEquidistant n d μ p = true then overlapFormula n r (d - 1) else overlapFormula n r (d + 1) := by intro μ have h1 : overlap Finset.univ r (fun i => decide (i = p)) (threeEquidistant n d μ) = overlapFormula n r (hammingDist (fun i => decide (i = p)) (threeEquidistant n d μ)) := fullOverlap_eq_overlapFormula n r (fun i => decide (i = p)) (threeEquidistant n d μ) have h2 := hammingDist_singleFlip p (threeEquidistant n d μ) dsimp only at h2 rw [threeEquidistant_dist_center h3d μ] at h2 rw [h1, h2] split_ifs <;> rfl rw [hK_eval 0, hK_eval 1, hK_eval 2] have hstabZ : (overlapFormula n r (d - 1) : ℤ) < 2 * (overlapFormula n r (d + 1) : ℤ) := by exact_mod_cast hstab have hImZ : (0 : ℤ) < (overlapFormula n r (d - 1) : ℤ) := by exact_mod_cast hIm_pos have hIpZ : (0 : ℤ) < (overlapFormula n r (d + 1) : ℤ) := by exact_mod_cast hIp_pos have hdisj_pt : ∀ q : Fin n, (threeEquidistant n d 0 q = true ∧ threeEquidistant n d 1 q = false ∧ threeEquidistant n d 2 q = false) ∨ (threeEquidistant n d 0 q = false ∧ threeEquidistant n d 1 q = true ∧ threeEquidistant n d 2 q = false) ∨ (threeEquidistant n d 0 q = false ∧ threeEquidistant n d 1 q = false ∧ threeEquidistant n d 2 q = true) ∨ (threeEquidistant n d 0 q = false ∧ threeEquidistant n d 1 q = false ∧ threeEquidistant n d 2 q = false) := by intro q simp only [threeEquidistant, Matrix.cons_val_zero, Matrix.cons_val_one, Matrix.cons_val_two] by_cases h1 : q.val < d · left; refine ⟨by simp [h1], by simp; omega, by simp; omega⟩ · by_cases h2 : q.val < 2 * d · right; left; refine ⟨by simp [h1], by simp; omega, by simp; omega⟩ · by_cases h3 : q.val < 3 * d · right; right; left; refine ⟨by simp [h1], by simp; omega, by simp; omega⟩ · right; right; right; refine ⟨by simp [h1], by simp; omega, by simp; omega⟩ rcases hdisj_pt p with ⟨hp0, hp1, hp2⟩ | ⟨hp0, hp1, hp2⟩ | ⟨hp0, hp1, hp2⟩ | ⟨hp0, hp1, hp2⟩ <;> rcases hdisj_pt j with ⟨hj0, hj1, hj2⟩ | ⟨hj0, hj1, hj2⟩ | ⟨hj0, hj1, hj2⟩ | ⟨hj0, hj1, hj2⟩ <;> rw [hp0, hp1, hp2, hj0, hj1, hj2] <;> simp [bipolar] <;> linarith /-- **Theorem 7.3 (Universal Spurious 3-Mixture Destabilization below `8r(n-r) ≤ n^2`)**: Whenever `2r < n` and `8 * r * (n - r) ≤ n^2` (i.e., `r / n ≤ (2 - √2) / 4 ≈ 0.14645`), for EVERY `1 ≤ d` with `3d ≤ n`, `2 * I_n(r, d + 1) ≤ I_n(r, d - 1)`, so NO spurious 3-mixture can capture its 1-bit neighborhood! -/ theorem spurious_three_mixture_universal_unstable {n d r : ℕ} (hr : 2 * r < n) (h8 : 8 * r * (n - r) ≤ n ^ 2) (hd : 1 ≤ d) (h3d : 3 * d ≤ n) : 2 * overlapFormula n r (d + 1) ≤ overlapFormula n r (d - 1) := by -- Both parities reduce to `I_n(r, 2m + 2) / I_n(r, 2m) ≤ 4r(n-r)/n^2 ≤ 1/2`! have hstep : ∀ m : ℕ, 2 * overlapFormula n r (2 * m + 2) ≤ overlapFormula n r (2 * m) := by intro m by_cases hm : m ≤ r · have hu := overlapFormula_step_ratio_uniform hr hm have hn2_pos : 0 < n ^ 2 := Nat.pow_pos (by omega) apply Nat.le_of_mul_le_mul_left _ hn2_pos calc n ^ 2 * (2 * overlapFormula n r (2 * m + 2)) = 2 * (n ^ 2 * overlapFormula n r (2 * m + 2)) := by ring _ ≤ 2 * (4 * r * (n - r) * overlapFormula n r (2 * m)) := Nat.mul_le_mul_left 2 hu _ = (8 * r * (n - r)) * overlapFormula n r (2 * m) := by ring _ ≤ n ^ 2 * overlapFormula n r (2 * m) := Nat.mul_le_mul_right _ h8 · rw [overlapFormula_eq_zero_of_two_mul_lt (by omega)] simp rcases Nat.even_or_odd d with ⟨m, hm⟩ | ⟨m, hm⟩ · -- d = 2m (m ≥ 1): d - 1 = 2(m-1) + 1 -> plateau -> 2m; d + 1 = 2m + 1 -> plateau -> 2m + 2! have hm1 : 1 ≤ m := by omega have hdm1 : d - 1 = 2 * (m - 1) + 1 := by omega have hdp1 : d + 1 = 2 * m + 1 := by omega rw [hdm1, hdp1, overlapFormula_parity_plateau (by omega : 2 * (m - 1) + 2 ≤ n)] rw [show 2 * (m - 1) + 2 = 2 * m by omega] by_cases h2 : 2 * m + 2 ≤ n · rw [overlapFormula_parity_plateau h2] exact hstep m · -- Only possible if 2m + 1 = n, impossible since 3d = 6m ≤ n! omega · -- d = 2m + 1: d - 1 = 2m, d + 1 = 2m + 2! have hdm1 : d - 1 = 2 * m := by omega have hdp1 : d + 1 = 2 * m + 2 := by omega rw [hdm1, hdp1] exact hstep m /-- **Theorem 7.4 (Subcritical Stable Spurious 3-Mixture Witness at `n = 9, d = 3, r = 4`)**: Even strictly inside the subcritical exponential-capacity regime (`2r = 8 < 9 = n`), at `n = 9, d = 3, r = 4` we have `I_9(4, 2) = 186 < 220 = 2 * I_9(4, 4)`, so the spurious majority state `z = 0^9` strictly captures its entire 1-bit neighborhood in a single SDM step! -/ theorem spurious_three_mixture_stable_witness : 2 * 4 < 9 ∧ ∀ ξ : Word 9, hammingDist ξ (majorityCenter 9) ≤ 1 → StrictRetrieves Finset.univ 4 (threeEquidistant 9 3) (threeEquidistant 9 3) ξ (majorityCenter 9) := by refine ⟨by decide, ?_⟩ rw [spurious_three_mixture_basin_iff (by decide : 1 ≤ 3) (by decide : 3 * 3 ≤ 9)] decide end KanervaSDM /-! ## Section 8: Axiom Verification for All Headline Theorems -/ #print axioms KanervaSDM.sdmRead_eq_kernel_sum #print axioms KanervaSDM.universalRetrievable_iff_strictDiagDom #print axioms KanervaSDM.fullOverlap_eq_overlapFormula #print axioms KanervaSDM.overlapFormula_parity_plateau #print axioms KanervaSDM.overlapFormula_step_drop #print axioms KanervaSDM.overlapFormula_single_sum #print axioms KanervaSDM.overlapFormula_complement_duality #print axioms KanervaSDM.overlapFormula_step_ratio_sharp #print axioms KanervaSDM.overlapFormula_step_ratio_uniform #print axioms KanervaSDM.overlapFormula_exp_decay #print axioms KanervaSDM.overlapFormula_exp_lower_bound #print axioms KanervaSDM.overlapFormula_log_concave_r2 #print axioms KanervaSDM.overlapFormula_log_convex_short_distance #print axioms KanervaSDM.overlapFormula_log_concave_support_edge #print axioms KanervaSDM.voronoi_basin_even_shell_captures #print axioms KanervaSDM.voronoi_basin_odd_shell_deadlock #print axioms KanervaSDM.margin_load_conservation #print axioms KanervaSDM.hard_location_capacity_ceiling #print axioms KanervaSDM.hard_location_private_bound #print axioms KanervaSDM.hard_location_capacity_tight_witness #print axioms KanervaSDM.blind_spot_of_small_hard_locations #print axioms KanervaSDM.covering_capacity_tradeoff #print axioms KanervaSDM.subcritical_basin_capture #print axioms KanervaSDM.subcritical_exponential_capacity #print axioms KanervaSDM.gilbert_varshamov_packing #print axioms KanervaSDM.fullCube_capacity_ceiling #print axioms KanervaSDM.supercritical_collapse_le_three #print axioms KanervaSDM.supercritical_three_witness_n5_r2 #print axioms KanervaSDM.supercritical_collapse_le_two #print axioms KanervaSDM.full_amnesia_r_eq_n #print axioms KanervaSDM.spurious_three_mixture_fixed_iff #print axioms KanervaSDM.spurious_three_mixture_basin_iff #print axioms KanervaSDM.spurious_three_mixture_universal_unstable #print axioms KanervaSDM.spurious_three_mixture_stable_witness