import Mathlib.Algebra.BigOperators.Expect import Mathlib.Data.Fintype.BigOperators import Mathlib.Basic.Real.Basic import Mathlib.LinearAlgebra.Quotient.Basic import Mathlib.Tactic.NormNum import OAI.Computability.UniqueGames.Analysis.SubspaceCountingLemmas import OAI.Computability.UniqueGames.Reduction.BinaryLinear namespace OAI section namespace UniqueGamesTheorem.Fourier.MatrixRestrictions open UniqueGamesTheorem.Integration.BinaryLinear open scoped BigOperators abbrev Ambient (m n : Nat) := Vector m →ₗ[F2] Vector n variable {E C : Type*} [AddCommGroup E] [Module F2 E] [AddCommGroup C] [Module F2 C] abbrev Parameter (W : Submodule F2 E) (C' : Submodule F2 C) := (E ⧸ W) →ₗ[F2] C' /-- The canonical inclusion of `L(E/W,C')` into `L(E,C)`. -/ def embed (W : Submodule F2 E) (C' : Submodule F2 C) (A : Parameter W C') : (E →ₗ[F2] C) := C'.subtype.comp (A.comp W.mkQ) @[simp] theorem embed_apply (W : Submodule F2 E) (C' : Submodule F2 C) (A : Parameter W C') (x : E) : embed W C' A x = (A (W.mkQ x) : C) := rfl theorem embed_injective (W : Submodule F2 E) (C' : Submodule F2 C) : Function.Injective (embed W C') := by intro A B h apply LinearMap.ext intro x obtain ⟨y, rfl⟩ := W.mkQ_surjective x apply Subtype.ext exact congrArg (fun f : (E →ₗ[F2] C) => f y) h theorem embed_vanishes (W : Submodule F2 E) (C' : Submodule F2 C) (A : Parameter W C') : W ≤ LinearMap.ker (embed W C' A) := by intro x hx change (A (W.mkQ x) : C) = 0 have hzero : W.mkQ x = 0 := by simpa using hx simp [hzero] theorem embed_range (W : Submodule F2 E) (C' : Submodule F2 C) (A : Parameter W C') : LinearMap.range (embed W C' A) ≤ C' := by rintro x ⟨y, rfl⟩ exact (A (W.mkQ y)).property /-- These are exactly the maps vanishing on `W` with image in `C'`. -/ theorem exists_embed_iff (W : Submodule F2 E) (C' : Submodule F2 C) (B : (E →ₗ[F2] C)) : (∃ A : Parameter W C', embed W C' A = B) ↔ W ≤ LinearMap.ker B ∧ LinearMap.range B ≤ C' := by constructor · rintro ⟨A, rfl⟩ exact ⟨embed_vanishes W C' A, embed_range W C' A⟩ · rintro ⟨hW, hC⟩ let B' : E →ₗ[F2] C' := B.codRestrict C' (fun x => hC ⟨x, rfl⟩) have hker : W ≤ LinearMap.ker B' := by intro x hx apply Subtype.ext exact hW hx refine ⟨W.liftQ B' hker, ?_⟩ apply LinearMap.ext intro x rfl /-- Quotient rows are unchanged by every admissible restriction perturbation. -/ theorem quotient_embed_zero (W : Submodule F2 E) (C' : Submodule F2 C) (A : Parameter W C') : C'.mkQ.comp (embed W C' A) = 0 := by apply LinearMap.ext intro x change C'.mkQ (embed W C' A x) = 0 exact (Submodule.Quotient.mk_eq_zero C').mpr (A (W.mkQ x)).property def translate (W : Submodule F2 E) (C' : Submodule F2 C) (X : (E →ₗ[F2] C)) (A : Parameter W C') : (E →ₗ[F2] C) := X + embed W C' A theorem translate_injective (W : Submodule F2 E) (C' : Submodule F2 C) (X : (E →ₗ[F2] C)) : Function.Injective (translate W C' X) := by intro A B h exact embed_injective W C' (add_left_cancel h) theorem quotient_translate (W : Submodule F2 E) (C' : Submodule F2 C) (X : (E →ₗ[F2] C)) (A : Parameter W C') : C'.mkQ.comp (translate W C' X A) = C'.mkQ.comp X := by apply LinearMap.ext intro x change C'.mkQ (X x + embed W C' A x) = C'.mkQ (X x) rw [map_add] have h : C'.mkQ (embed W C' A x) = 0 := congrArg (fun f : E →ₗ[F2] (C ⧸ C') => f x) (quotient_embed_zero W C' A) rw [h, add_zero] /-- The perturbation inclusion is itself a binary linear map. -/ def embedding (W : Submodule F2 E) (C' : Submodule F2 C) : Parameter W C' →ₗ[F2] (E →ₗ[F2] C) where toFun := embed W C' map_add' A B := by apply LinearMap.ext intro x rfl map_smul' a A := by apply LinearMap.ext intro x rfl noncomputable def order (W : Submodule F2 E) (C' : Submodule F2 C) : Nat := Module.finrank F2 W + Module.finrank F2 (C ⧸ C') /-- Restrict a real observable to the actual affine quotient-map domain. -/ def restrict (f : (E →ₗ[F2] C) → ℝ) (W : Submodule F2 E) (C' : Submodule F2 C) (X : E →ₗ[F2] C) : Parameter W C' → ℝ := fun A => f (translate W C' X A) noncomputable instance parameterFintype [Finite E] [Finite C] (W : Submodule F2 E) (C' : Submodule F2 C) : Fintype (Parameter W C') := by classical letI : Fintype E := Fintype.ofFinite E letI : Fintype C' := Fintype.ofFinite C' exact Fintype.ofInjective (fun A : Parameter W C' => (A : (E ⧸ W) → C')) DFunLike.coe_injective /-- Every actual restriction domain has at least its zero map. -/ theorem parameter_card_pos [Finite E] [Finite C] (W : Submodule F2 E) (C' : Submodule F2 C) : 0 < Fintype.card (Parameter W C') := Fintype.card_pos /-- An actual row includes its target subspace as part of its index. -/ structure Row (E C : Type*) [AddCommGroup E] [Module F2 E] [AddCommGroup C] [Module F2 C] where target : Submodule F2 C value : E →ₗ[F2] (C ⧸ target) structure Witness (E C : Type*) [AddCommGroup E] [Module F2 E] [AddCommGroup C] [Module F2 C] where vanishing : Submodule F2 E target : Submodule F2 C base : E →ₗ[F2] C noncomputable def samples [Finite E] [Finite C] (t : Witness E C) : List (E →ₗ[F2] C) := (Finset.univ : Finset (Parameter t.vanishing t.target)).toList.map (translate t.vanishing t.target t.base) theorem samples_length [Finite E] [Finite C] (t : Witness E C) : (samples t).length = Fintype.card (Parameter t.vanishing t.target) := by simp [samples] theorem samples_nonempty [Finite E] [Finite C] (t : Witness E C) : samples t ≠ [] := by intro h have hp := parameter_card_pos t.vanishing t.target rw [← samples_length t, h] at hp exact Nat.not_lt_zero 0 hp theorem mem_samples_iff [Finite E] [Finite C] (t : Witness E C) (X : E →ₗ[F2] C) : X ∈ samples t ↔ ∃ A : Parameter t.vanishing t.target, translate t.vanishing t.target t.base A = X := by classical simp [samples] theorem samples_nodup [Finite E] [Finite C] (t : Witness E C) : (samples t).Nodup := by classical exact List.Nodup.map (translate_injective t.vanishing t.target t.base) (Finset.nodup_toList _) /-- Actual affine matrix restrictions instantiate the generic deletion interface. -/ noncomputable def system [Finite E] [Finite C] (r : Nat) : Deletion.RestrictionSystem (E →ₗ[F2] C) (Row E C) (Witness E C) where samples := samples samples_nonempty := samples_nonempty admissible t := order t.vanishing t.target ≤ r row t X := t.target.mkQ.comp X = t.value containingRow t := ⟨t.target, t.target.mkQ.comp t.base⟩ contained t X hX := by obtain ⟨A, rfl⟩ := (mem_samples_iff t X).mp hX exact quotient_translate t.vanishing t.target t.base A /-- Every actual affine restriction of order at most `r` obeys the residual count bound after whole-row deletion. No linear containment assumption remains. -/ theorem remainder_restriction_count [Finite E] [Finite C] (H : (E →ₗ[F2] C) → Bool) (r p q : Nat) (t : Witness E C) (ht : order t.vanishing t.target ≤ r) : q * Deletion.trueCount (Deletion.remainder (system r) H p q) (samples t) ≤ p * Fintype.card (Parameter t.vanishing t.target) := by rw [← samples_length t] exact Deletion.remainder_restrictionGlobal (system r) H p q t ht private theorem trueCount_eq_countP {α : Type*} (H : α → Bool) (xs : List α) : Deletion.trueCount H xs = xs.countP H := by induction xs with | nil => rfl | cons x xs ih => cases hx : H x <;> simp [Deletion.trueCount, ih, hx, Nat.add_comm] /-- Exact numerator for uniform sampling of the actual affine restriction. -/ theorem samples_trueCount [Finite E] [Finite C] (H : (E →ₗ[F2] C) → Bool) (t : Witness E C) : Deletion.trueCount H (samples t) = (Finset.univ.filter fun A : Parameter t.vanishing t.target => H (translate t.vanishing t.target t.base A) = true).card := by classical rw [trueCount_eq_countP] simp only [samples, List.countP_map] symm simpa [Function.comp_def] using (Finset.nodup_toList (Finset.univ : Finset (Parameter t.vanishing t.target))).card_eq_countP (P := fun A => H (translate t.vanishing t.target t.base A) = true) noncomputable def density [Finite E] [Finite C] (H : (E →ₗ[F2] C) → Bool) (t : Witness E C) : ℝ := 𝔼 A : Parameter t.vanishing t.target, if H (translate t.vanishing t.target t.base A) then (1 : ℝ) else 0 theorem density_eq_count_div [Finite E] [Finite C] (H : (E →ₗ[F2] C) → Bool) (t : Witness E C) : density H t = (Deletion.trueCount H (samples t) : ℝ) / Fintype.card (Parameter t.vanishing t.target) := by classical rw [density, Finset.expect_eq_sum_div_card, samples_trueCount] simp theorem dense_iff_lt_density [Finite E] [Finite C] (H : (E →ₗ[F2] C) → Bool) (r p q : Nat) (hq : 0 < q) (t : Witness E C) : Deletion.Dense (system r) H p q t ↔ (p : ℝ) / q < density H t := by rw [density_eq_count_div] have hq' : (0 : ℝ) < q := by exact_mod_cast hq have hcard : (0 : ℝ) < Fintype.card (Parameter t.vanishing t.target) := by exact_mod_cast parameter_card_pos t.vanishing t.target rw [div_lt_div_iff₀ hq' hcard] rw [mul_comm (Deletion.trueCount H (samples t) : ℝ) (q : ℝ)] change p * (samples t).length < q * Deletion.trueCount H (samples t) ↔ _ rw [samples_length] constructor <;> intro h <;> exact_mod_cast h /-- Whole-row deletion gives the normalized real restriction bound on every actual affine restriction of the permitted dimension and codimension. -/ theorem remainder_restriction_density [Finite E] [Finite C] (H : (E →ₗ[F2] C) → Bool) (r p q : Nat) (hq : 0 < q) (t : Witness E C) (ht : order t.vanishing t.target ≤ r) : density (Deletion.remainder (system r) H p q) t ≤ (p : ℝ) / q := by rw [density_eq_count_div] have hcard : (0 : ℝ) < Fintype.card (Parameter t.vanishing t.target) := by exact_mod_cast parameter_card_pos t.vanishing t.target have hq' : (0 : ℝ) < q := by exact_mod_cast hq apply (div_le_div_iff₀ hcard hq').mpr have h := remainder_restriction_count H r p q t ht have hc : (q : ℝ) * Deletion.trueCount (Deletion.remainder (system r) H p q) (samples t) ≤ (p : ℝ) * Fintype.card (Parameter t.vanishing t.target) := by exact_mod_cast h simpa [mul_comm] using hc /-- The same bound expressed as the squared normalized real `L²` norm. This is the restriction-global hypothesis used by the Fourier level theorem. -/ theorem remainder_restriction_sq_expect [Finite E] [Finite C] (H : (E →ₗ[F2] C) → Bool) (r p q : Nat) (hq : 0 < q) (W : Submodule F2 E) (C' : Submodule F2 C) (horder : order W C' ≤ r) (X : E →ₗ[F2] C) : (𝔼 A : Parameter W C', (restrict (fun Y => if Deletion.remainder (system r) H p q Y then (1 : ℝ) else 0) W C' X A) ^ 2) ≤ (p : ℝ) / q := by have heq : (𝔼 A : Parameter W C', (restrict (fun Y => if Deletion.remainder (system r) H p q Y then (1 : ℝ) else 0) W C' X A) ^ 2) = density (Deletion.remainder (system r) H p q) ⟨W, C', X⟩ := by apply Finset.expect_congr rfl intro A _ simp only [restrict] split <;> norm_num rw [heq] exact remainder_restriction_density H r p q hq ⟨W, C', X⟩ horder /-- A dual map killed by the quotient has a unique factor with values in `W`. This is the exact finite object counted in the parity argument of Lemma 5.3. -/ def quotientKernelEquiv (W : Submodule F2 E) : {T : C →ₗ[F2] E // W.mkQ.comp T = 0} ≃ (C →ₗ[F2] W) where toFun T := T.val.codRestrict W (fun x => by apply (Submodule.Quotient.mk_eq_zero W).mp exact congrArg (fun f : C →ₗ[F2] (E ⧸ W) => f x) T.property) invFun T := ⟨W.subtype.comp T, by apply LinearMap.ext intro x exact (Submodule.Quotient.mk_eq_zero W).mpr (T x).property⟩ left_inv T := by apply Subtype.ext apply LinearMap.ext intro x rfl right_inv T := by apply LinearMap.ext intro x rfl theorem quotientKernel_card (W : Submodule F2 E) [Fintype {T : C →ₗ[F2] E // W.mkQ.comp T = 0}] [Fintype (C →ₗ[F2] W)] : Fintype.card {T : C →ₗ[F2] E // W.mkQ.comp T = 0} = Fintype.card (C →ₗ[F2] W) := Fintype.card_congr (quotientKernelEquiv W) open scoped Classical in theorem quotientKernel_filter_card (W : Submodule F2 E) [Fintype (C →ₗ[F2] E)] [Fintype (C →ₗ[F2] W)] : (Finset.univ.filter fun T : C →ₗ[F2] E => W.mkQ.comp T = 0).card = Fintype.card (C →ₗ[F2] W) := by classical rw [← Fintype.card_subtype] exact quotientKernel_card W /-- Every deleted point determines its row once the target subspace is fixed. Thus the union bound ranges over target subspaces, not over row values. -/ theorem deletedSet_iff_exists_target [Finite E] [Finite C] (H : (E →ₗ[F2] C) → Bool) (r p q : Nat) (X : E →ₗ[F2] C) : Deletion.DeletedSet (system r) H p q X ↔ ∃ C' : Submodule F2 C, Deletion.BadRow (system r) H p q ⟨C', C'.mkQ.comp X⟩ := by constructor · rintro ⟨⟨C', Z⟩, hbad, hX⟩ change C'.mkQ.comp X = Z at hX subst Z exact ⟨C', hbad⟩ · rintro ⟨C', hbad⟩ exact ⟨⟨C', C'.mkQ.comp X⟩, hbad, rfl⟩ theorem badRow_codim_le [Finite E] [Finite C] (H : (E →ₗ[F2] C) → Bool) (r p q : Nat) (row : Row E C) (hbad : Deletion.BadRow (system r) H p q row) : Module.finrank F2 (C ⧸ row.target) ≤ r := by rcases hbad with ⟨t, ht, heq, _⟩ have ht' : Module.finrank F2 t.vanishing + Module.finrank F2 (C ⧸ t.target) ≤ r := ht have htarget : t.target = row.target := congrArg Row.target heq rw [← htarget] omega theorem deletedSet_iff_exists_admissible_target [Finite E] [Finite C] (H : (E →ₗ[F2] C) → Bool) (r p q : Nat) (X : E →ₗ[F2] C) : Deletion.DeletedSet (system r) H p q X ↔ ∃ C' : Submodule F2 C, Module.finrank F2 (C ⧸ C') ≤ r ∧ Deletion.BadRow (system r) H p q ⟨C', C'.mkQ.comp X⟩ := by rw [deletedSet_iff_exists_target] constructor · rintro ⟨C', hb⟩ exact ⟨C', badRow_codim_le H r p q ⟨C', C'.mkQ.comp X⟩ hb, hb⟩ · rintro ⟨C', _, hb⟩ exact ⟨C', hb⟩ end UniqueGamesTheorem.Fourier.MatrixRestrictions end end OAI