import Mathlib.Algebra.MvPolynomial.Basic import Mathlib.Algebra.MvPolynomial.Degrees import Mathlib.Algebra.MvPolynomial.Monad import Mathlib.Algebra.MvPolynomial.Rename import Mathlib.Algebra.MvPolynomial.SchwartzZippel import Mathlib.RingTheory.MvPolynomial.Homogeneous import Mathlib.Algebra.Order.Field.Basic import Mathlib.Data.Fintype.Perm import Mathlib.Algebra.CharP.Defs /-! # Pullback Degree Bound and Hitting Set Size Formalizes: 1. General substitution degree bound: if every coordinate `G i` has `totalDegree ≤ m` and `f` has `totalDegree ≤ d`, then `(f.bind₁ G).totalDegree ≤ d * m`. 2. The `m × m` permanent polynomial `permPoly R m` is homogeneous of degree `m` (with `totalDegree = m` over any nontrivial ring), and so is every coordinate of the Nisan–Wigderson generator `nwGenerator R Φ`. 3. Combination with Schwartz–Zippel (`GridHit.lean`) over any finite seed universe `U`: if `f.bind₁ G ≠ 0`, then the image of the integer grid `{0, ..., d*m}^U` under `G` has cardinality `≤ (d * m + 1) ^ |U|` and contains a point where `f` does not vanish. -/ namespace PermanentPIT.PullbackDegree open MvPolynomial Finset /-- Total degree of a power is bounded by exponent times total degree. -/ theorem totalDegree_pow_le {σ R : Type*} [CommSemiring R] (p : MvPolynomial σ R) (k : ℕ) : (p ^ k).totalDegree ≤ k * p.totalDegree := by induction k with | zero => simp | succ k ih => rw [pow_succ, Nat.succ_mul] exact (MvPolynomial.totalDegree_mul (p ^ k) p).trans (Nat.add_le_add_right ih _) /-- **Substitution total-degree bound.** Substituting polynomials `G i` each of `totalDegree ≤ m` into `f` yields a polynomial of `totalDegree ≤ f.totalDegree * m`. -/ theorem totalDegree_bind₁_le {σ τ R : Type*} [CommSemiring R] (f : MvPolynomial σ R) (G : σ → MvPolynomial τ R) (m : ℕ) (hG : ∀ i, (G i).totalDegree ≤ m) : (MvPolynomial.bind₁ G f).totalDegree ≤ f.totalDegree * m := by classical have hbind : MvPolynomial.bind₁ G f = ∑ s ∈ f.support, MvPolynomial.C (f.coeff s) * ∏ i ∈ s.support, (G i) ^ (s i) := by conv_lhs => rw [f.as_sum] rw [map_sum] refine Finset.sum_congr rfl fun s _ => ?_ rw [MvPolynomial.monomial_eq, map_mul, MvPolynomial.bind₁_C_right, Finsupp.prod, map_prod] congr 1 refine Finset.prod_congr rfl fun i _ => ?_ rw [map_pow, MvPolynomial.bind₁_X_right] rw [hbind] refine (MvPolynomial.totalDegree_finsetSum _ _).trans ?_ rw [Finset.sup_le_iff] intro s hs have hterm : (MvPolynomial.C (f.coeff s) * ∏ i ∈ s.support, (G i) ^ (s i)).totalDegree ≤ (s.sum fun _ e => e) * m := by calc (MvPolynomial.C (f.coeff s) * ∏ i ∈ s.support, (G i) ^ (s i)).totalDegree ≤ (MvPolynomial.C (f.coeff s) : MvPolynomial τ R).totalDegree + (∏ i ∈ s.support, (G i) ^ (s i)).totalDegree := MvPolynomial.totalDegree_mul _ _ _ = (∏ i ∈ s.support, (G i) ^ (s i)).totalDegree := by simp _ ≤ ∑ i ∈ s.support, ((G i) ^ (s i)).totalDegree := MvPolynomial.totalDegree_finsetProd _ _ _ ≤ ∑ i ∈ s.support, s i * (G i).totalDegree := Finset.sum_le_sum fun i _ => totalDegree_pow_le (G i) (s i) _ ≤ ∑ i ∈ s.support, s i * m := Finset.sum_le_sum fun i _ => Nat.mul_le_mul_left (s i) (hG i) _ = (∑ i ∈ s.support, s i) * m := (Finset.sum_mul _ _ _).symm _ = (s.sum fun _ e => e) * m := rfl have hs_le : (s.sum fun _ e => e) ≤ f.totalDegree := by rw [MvPolynomial.totalDegree] exact Finset.le_sup (f := fun s : σ →₀ ℕ => s.sum fun _ e => e) hs exact hterm.trans (Nat.mul_le_mul_right m hs_le) /-- Corollary with explicit degree bound `f.totalDegree ≤ d`. -/ theorem totalDegree_bind₁_le_of_le {σ τ R : Type*} [CommSemiring R] {f : MvPolynomial σ R} {G : σ → MvPolynomial τ R} {d m : ℕ} (hf : f.totalDegree ≤ d) (hG : ∀ i, (G i).totalDegree ≤ m) : (MvPolynomial.bind₁ G f).totalDegree ≤ d * m := (totalDegree_bind₁_le f G m hG).trans (Nat.mul_le_mul_right m hf) /-- `aeval G` coincides with `bind₁ G`. -/ theorem aeval_eq_bind₁ {σ τ R : Type*} [CommSemiring R] (G : σ → MvPolynomial τ R) (f : MvPolynomial σ R) : MvPolynomial.aeval G f = MvPolynomial.bind₁ G f := by induction f using MvPolynomial.induction_on with | C a => simp [MvPolynomial.algebraMap_eq] | add p q hp hq => rw [map_add, map_add, hp, hq] | mul_X p i hp => rw [map_mul, map_mul, hp, MvPolynomial.aeval_X, MvPolynomial.bind₁_X_right] /-- Evaluation commutes with `bind₁`. -/ theorem eval_bind₁ {σ τ R : Type*} [CommSemiring R] (G : σ → MvPolynomial τ R) (f : MvPolynomial σ R) (z : τ → R) : MvPolynomial.eval z (MvPolynomial.bind₁ G f) = MvPolynomial.eval (fun i => MvPolynomial.eval z (G i)) f := by induction f using MvPolynomial.induction_on with | C a => simp | add p q hp hq => simp [hp, hq] | mul_X p i hp => simp [hp] /-- The `m × m` permanent polynomial over a commutative semiring `R`. -/ noncomputable def permPoly (R : Type*) [CommSemiring R] (m : ℕ) : MvPolynomial (Fin m × Fin m) R := ∑ σ : Equiv.Perm (Fin m), ∏ i : Fin m, MvPolynomial.X (i, σ i) /-- Every permutation term in `permPoly` is homogeneous of degree `m`. -/ theorem permTerm_isHomogeneous {R : Type*} [CommSemiring R] (m : ℕ) (σ : Equiv.Perm (Fin m)) : (∏ i : Fin m, (MvPolynomial.X (i, σ i) : MvPolynomial (Fin m × Fin m) R)).IsHomogeneous m := by have h := MvPolynomial.IsHomogeneous.prod (Finset.univ : Finset (Fin m)) (fun i : Fin m => (MvPolynomial.X (i, σ i) : MvPolynomial (Fin m × Fin m) R)) (fun _ => 1) (fun i _ => MvPolynomial.isHomogeneous_X R (i, σ i)) simpa using h /-- `permPoly R m` is homogeneous of degree `m`. -/ theorem permPoly_isHomogeneous (R : Type*) [CommSemiring R] (m : ℕ) : (permPoly R m).IsHomogeneous m := by unfold permPoly exact MvPolynomial.IsHomogeneous.sum _ _ _ fun σ _ => permTerm_isHomogeneous m σ /-- `permPoly R m` has total degree at most `m`. -/ theorem permPoly_totalDegree_le (R : Type*) [CommSemiring R] (m : ℕ) : (permPoly R m).totalDegree ≤ m := (permPoly_isHomogeneous R m).totalDegree_le /-- The monomial exponent vector corresponding to a permutation `σ`. -/ noncomputable def permMonomial {m : ℕ} (σ : Equiv.Perm (Fin m)) : (Fin m × Fin m) →₀ ℕ := ∑ i : Fin m, Finsupp.single (i, σ i) 1 theorem prod_X_eq_monomial_permMonomial {R : Type*} [CommSemiring R] {m : ℕ} (σ : Equiv.Perm (Fin m)) : (∏ i : Fin m, (MvPolynomial.X (i, σ i) : MvPolynomial (Fin m × Fin m) R)) = MvPolynomial.monomial (permMonomial σ) 1 := by classical have hgen : ∀ s : Finset (Fin m), (∏ i ∈ s, (MvPolynomial.X (i, σ i) : MvPolynomial (Fin m × Fin m) R)) = MvPolynomial.monomial (∑ i ∈ s, Finsupp.single (i, σ i) 1) 1 := by intro s induction s using Finset.induction_on with | empty => simp | insert a s ha ih => rw [Finset.prod_insert ha, Finset.sum_insert ha, ih, MvPolynomial.X, MvPolynomial.monomial_mul, one_mul] exact hgen Finset.univ theorem permMonomial_apply {m : ℕ} (σ : Equiv.Perm (Fin m)) (p : Fin m × Fin m) : permMonomial σ p = if σ p.1 = p.2 then 1 else 0 := by classical unfold permMonomial rw [Finsupp.finsetSum_apply] rw [Finset.sum_eq_single p.1] · simp [Finsupp.single_apply, Prod.ext_iff] · intro i _ hi have hne : (i, σ i) ≠ p := fun h => hi (congrArg Prod.fst h) simp [hne] · intro hp exact absurd (Finset.mem_univ p.1) hp theorem permMonomial_injective {m : ℕ} : Function.Injective (@permMonomial m) := by intro σ τ h refine Equiv.ext fun i => ?_ have hi := congrArg (fun f : (Fin m × Fin m) →₀ ℕ => f (i, σ i)) h rw [permMonomial_apply, permMonomial_apply] at hi simp only [↓reduceIte] at hi by_contra hne have hne' : τ i ≠ σ i := fun heq => hne heq.symm simp [hne'] at hi /-- Coefficient of the identity-permutation monomial in `permPoly R m` is `1`. -/ theorem coeff_permPoly_id (R : Type*) [CommSemiring R] (m : ℕ) : (permPoly R m).coeff (permMonomial (1 : Equiv.Perm (Fin m))) = 1 := by classical unfold permPoly rw [MvPolynomial.coeff_sum] rw [Finset.sum_eq_single (1 : Equiv.Perm (Fin m))] · rw [prod_X_eq_monomial_permMonomial, MvPolynomial.coeff_monomial] simp · intro σ _ hσ rw [prod_X_eq_monomial_permMonomial, MvPolynomial.coeff_monomial] have hne : permMonomial σ ≠ permMonomial (1 : Equiv.Perm (Fin m)) := fun h => hσ (permMonomial_injective h) simp [hne] · simp /-- Over any nontrivial semiring, `permPoly R m` is nonzero and has exact `totalDegree = m`. -/ theorem permPoly_ne_zero (R : Type*) [CommSemiring R] [Nontrivial R] (m : ℕ) : permPoly R m ≠ 0 := by intro h have := congrArg (fun p => MvPolynomial.coeff (permMonomial (1 : Equiv.Perm (Fin m))) p) h rw [coeff_permPoly_id, MvPolynomial.coeff_zero] at this exact one_ne_zero this theorem permPoly_totalDegree_eq (R : Type*) [CommSemiring R] [Nontrivial R] (m : ℕ) : (permPoly R m).totalDegree = m := (permPoly_isHomogeneous R m).totalDegree (permPoly_ne_zero R m) /-- The Nisan–Wigderson polynomial generator: coordinate `i` evaluates `perm_m` on the `m^2` seed variables indexed by `Φ i : Fin m × Fin m ↪ U`. -/ noncomputable def nwGenerator (R : Type*) [CommSemiring R] {U : Type*} {n m : ℕ} (Φ : Fin n → (Fin m × Fin m ↪ U)) : Fin n → MvPolynomial U R := fun i => MvPolynomial.rename (Φ i) (permPoly R m) /-- Renaming variables does not increase `totalDegree`. -/ theorem totalDegree_rename_le {σ τ R : Type*} [CommSemiring R] (f : σ → τ) (p : MvPolynomial σ R) : (MvPolynomial.rename f p).totalDegree ≤ p.totalDegree := by rw [MvPolynomial.rename_eq_aeval, aeval_eq_bind₁] have h := totalDegree_bind₁_le p (MvPolynomial.X ∘ f) 1 (fun i => by change (MvPolynomial.X (f i) : MvPolynomial τ R).totalDegree ≤ 1 rw [MvPolynomial.X] exact (MvPolynomial.totalDegree_monomial_le (Finsupp.single (f i) 1) (1 : R)).trans (by simp)) simpa using h /-- Each coordinate of `nwGenerator` has `totalDegree ≤ m`. -/ theorem nwGenerator_totalDegree_le (R : Type*) [CommSemiring R] {U : Type*} {n m : ℕ} (Φ : Fin n → (Fin m × Fin m ↪ U)) (i : Fin n) : (nwGenerator R Φ i).totalDegree ≤ m := (totalDegree_rename_le (Φ i) (permPoly R m)).trans (permPoly_totalDegree_le R m) /-- Substituting the NW permanent generator into a polynomial of total degree `≤ d` yields a polynomial of total degree `≤ d * m`. -/ theorem nwGenerator_pullback_totalDegree_le {U R : Type*} [CommSemiring R] {n m d : ℕ} (Φ : Fin n → (Fin m × Fin m ↪ U)) {f : MvPolynomial (Fin n) R} (hf : f.totalDegree ≤ d) : (MvPolynomial.bind₁ (nwGenerator R Φ) f).totalDegree ≤ d * m := totalDegree_bind₁_le_of_le hf (nwGenerator_totalDegree_le R Φ) /-- Finite-grid nonvanishing consequence of Schwartz–Zippel for any finite variable type `U`. -/ theorem grid_hits_of_card_gt_totalDegree {F U : Type*} [Field F] [Fintype U] [DecidableEq U] (p : MvPolynomial U F) (hp : p ≠ 0) (S : Finset F) (hcard : p.totalDegree < S.card) : ∃ z ∈ Fintype.piFinset (fun _ : U => S), MvPolynomial.eval z p ≠ 0 := by classical let e : U ≃ Fin (Fintype.card U) := Fintype.equivFin U let q : MvPolynomial (Fin (Fintype.card U)) F := MvPolynomial.rename e p have hq_ne : q ≠ 0 := by intro hq apply hp exact MvPolynomial.rename_injective e e.injective (by simpa [q] using hq) have hq_deg : q.totalDegree < S.card := lt_of_le_of_lt (totalDegree_rename_le e p) hcard have hfin : ∃ w ∈ Fintype.piFinset (fun _ : Fin (Fintype.card U) => S), MvPolynomial.eval w q ≠ 0 := by by_contra h push Not at h have hfilter : (Fintype.piFinset (fun _ : Fin (Fintype.card U) => S)).filter (fun w => MvPolynomial.eval w q = 0) = Fintype.piFinset (fun _ : Fin (Fintype.card U) => S) := Finset.filter_eq_self.mpr h have hne : ((S.card : ℚ≥0) ^ Fintype.card U) ≠ 0 := by exact pow_ne_zero _ (by exact_mod_cast (Nat.ne_of_gt (Nat.zero_lt_of_lt hq_deg))) have hsz := MvPolynomial.schwartz_zippel_totalDegree hq_ne S have hleft : (1 : ℚ≥0) ≤ (q.totalDegree : ℚ≥0) / (S.card : ℚ≥0) := by simpa [hfilter, Fintype.card_piFinset_const, hne] using hsz have hcast : (q.totalDegree : ℚ≥0) < (S.card : ℚ≥0) := by exact_mod_cast hq_deg have hpos : (0 : ℚ≥0) < (S.card : ℚ≥0) := by exact_mod_cast (Nat.zero_lt_of_lt hq_deg) have hright : (q.totalDegree : ℚ≥0) / (S.card : ℚ≥0) < 1 := (div_lt_one hpos).2 hcast exact (not_lt_of_ge hleft) hright rcases hfin with ⟨w, hw, heval⟩ refine ⟨w ∘ e, ?_, ?_⟩ · rw [Fintype.mem_piFinset] at hw ⊢ intro u exact hw (e u) · simpa [q, MvPolynomial.eval_rename] using heval /-- The finite hitting set formed as the image of `S^U` under the generator `G`. -/ noncomputable def hittingSet {F U : Type*} [Field F] [Fintype U] [DecidableEq U] {n : ℕ} (G : Fin n → MvPolynomial U F) (S : Finset F) : Finset (Fin n → F) := by classical exact (Fintype.piFinset fun _ : U => S).image (fun z i => MvPolynomial.eval z (G i)) /-- Cardinality of `hittingSet G S` is at most `S.card ^ |U|`. -/ theorem hittingSet_card_le {F U : Type*} [Field F] [Fintype U] [DecidableEq U] {n : ℕ} (G : Fin n → MvPolynomial U F) (S : Finset F) : (hittingSet G S).card ≤ S.card ^ Fintype.card U := by classical unfold hittingSet calc _ ≤ (Fintype.piFinset (fun _ : U => S)).card := Finset.card_image_le _ = S.card ^ Fintype.card U := by rw [Fintype.card_piFinset] simp /-- The integer grid `{0, 1, ..., E}` embedded in a characteristic-zero field `F`. -/ noncomputable def integerGrid (F : Type*) [Field F] (E : ℕ) : Finset F := by classical exact (Finset.range (E + 1)).image (Nat.cast : ℕ → F) /-- Over a field of characteristic zero, `{0, 1, ..., E}` has exact cardinality `E + 1`. -/ theorem integerGrid_card (F : Type*) [Field F] [CharZero F] (E : ℕ) : (integerGrid F E).card = E + 1 := by classical unfold integerGrid rw [Finset.card_image_of_injective _ Nat.cast_injective, Finset.card_range] /-- **Combined Pullback + Schwartz–Zippel Hitting Set Theorem.** Over a field `F` of characteristic zero, if `G : Fin n → MvPolynomial U F` has coordinate degrees `≤ m`, then `H = hittingSet G {0, ..., d*m}` has cardinality `≤ (d * m + 1) ^ |U|` and hits every polynomial `f` of `totalDegree ≤ d` whose pullback `f.bind₁ G` is nonzero. -/ theorem hittingSet_card_and_hit {F U : Type*} [Field F] [CharZero F] [Fintype U] [DecidableEq U] {n m d : ℕ} (G : Fin n → MvPolynomial U F) (hG : ∀ i, (G i).totalDegree ≤ m) : (hittingSet G (integerGrid F (d * m))).card ≤ (d * m + 1) ^ Fintype.card U ∧ ∀ f : MvPolynomial (Fin n) F, f.totalDegree ≤ d → MvPolynomial.bind₁ G f ≠ 0 → ∃ x ∈ hittingSet G (integerGrid F (d * m)), MvPolynomial.eval x f ≠ 0 := by classical have hgrid : (integerGrid F (d * m)).card = d * m + 1 := integerGrid_card F (d * m) refine ⟨?_, ?_⟩ · simpa [hgrid] using hittingSet_card_le G (integerGrid F (d * m)) · intro f hf hnz have hdeg : (MvPolynomial.bind₁ G f).totalDegree < (integerGrid F (d * m)).card := by rw [hgrid] exact Nat.lt_succ_of_le (totalDegree_bind₁_le_of_le hf hG) obtain ⟨z, hz, heval⟩ := grid_hits_of_card_gt_totalDegree (MvPolynomial.bind₁ G f) hnz (integerGrid F (d * m)) hdeg refine ⟨fun i => MvPolynomial.eval z (G i), Finset.mem_image.mpr ⟨z, hz, rfl⟩, ?_⟩ simpa [eval_bind₁] using heval /-- Specialization to the Nisan–Wigderson permanent generator `nwGenerator F Φ`. -/ theorem nwGenerator_hittingSet_card_and_hit {F U : Type*} [Field F] [CharZero F] [Fintype U] [DecidableEq U] {n m d : ℕ} (Φ : Fin n → (Fin m × Fin m ↪ U)) : (hittingSet (nwGenerator F Φ) (integerGrid F (d * m))).card ≤ (d * m + 1) ^ Fintype.card U ∧ ∀ f : MvPolynomial (Fin n) F, f.totalDegree ≤ d → MvPolynomial.bind₁ (nwGenerator F Φ) f ≠ 0 → ∃ x ∈ hittingSet (nwGenerator F Φ) (integerGrid F (d * m)), MvPolynomial.eval x f ≠ 0 := hittingSet_card_and_hit (nwGenerator F Φ) (nwGenerator_totalDegree_le F Φ) /-- If moreover `|U| ≤ 2 * m^4`, then the hitting set has cardinality `≤ (d * m + 1) ^ (2 * m^4)`. -/ theorem nwGenerator_hittingSet_card_le_pow {F U : Type*} [Field F] [CharZero F] [Fintype U] [DecidableEq U] {n m d : ℕ} (Φ : Fin n → (Fin m × Fin m ↪ U)) (hU : Fintype.card U ≤ 2 * m ^ 4) : (hittingSet (nwGenerator F Φ) (integerGrid F (d * m))).card ≤ (d * m + 1) ^ (2 * m ^ 4) := by have h1 := (nwGenerator_hittingSet_card_and_hit (F := F) (d := d) Φ).1 exact h1.trans (Nat.pow_le_pow_right (Nat.succ_pos _) hU) end PermanentPIT.PullbackDegree #print axioms PermanentPIT.PullbackDegree.totalDegree_bind₁_le #print axioms PermanentPIT.PullbackDegree.permPoly_isHomogeneous #print axioms PermanentPIT.PullbackDegree.permPoly_totalDegree_eq #print axioms PermanentPIT.PullbackDegree.nwGenerator_pullback_totalDegree_le #print axioms PermanentPIT.PullbackDegree.hittingSet_card_and_hit #print axioms PermanentPIT.PullbackDegree.nwGenerator_hittingSet_card_and_hit #print axioms PermanentPIT.PullbackDegree.nwGenerator_hittingSet_card_le_pow