-- Mathlib/Algebra/MvPolynomial/SchwartzZippel.lean
/-- The **Schwartz-Zippel lemma**

For a nonzero multivariable polynomial `p` over an integral domain, the probability that `p`
evaluates to zero at points drawn at random from a product of finite subsets `S i` of the integral
domain is bounded by the supremum of `∑ i, degᵢ s / #(S i)` ranging over monomials `s` of `p`. -/
lemma schwartz_zippel_sup_sum :
    ∀ {n} {p : MvPolynomial (Fin n) R} (hp : p ≠ 0) (S : Fin n → Finset R),
      #{x ∈ S ^^ n | eval x p = 0} / ∏ i, (#(S i) : ℚ≥0) ≤
        p.support.sup fun s ↦ ∑ i, (s i / #(S i) : ℚ≥0)
  | 0, p, hp, S => by
    -- Because `p` is a polynomial over zero variables, it is constant.
    rw [p.eq_C_of_isEmpty] at *
    simp [C_ne_zero.mp hp]
    -- Now, assume that the theorem holds for all polynomials in `n` variables.
  | n + 1, p, hp, S => by
    -- We can consider `p` to be a polynomial over multivariable polynomials in one fewer variables.
    set p' : Polynomial (MvPolynomial (Fin n) R) := finSuccEquiv R n p with hp'
    -- Since `p` is not identically zero, there is some `k` such that `pₖ` is not identically zero.
    -- WLOG `k` is the largest such.
    set k := p'.natDegree with hk
    set pₖ := p'.leadingCoeff with hpₖ
    have hp'₀ : p' ≠ 0 := EmbeddingLike.map_ne_zero_iff.2 hp
    have hpₖ₀ : pₖ ≠ 0

Proof state:
R : Type u_1
inst✝² : CommRing R
inst✝¹ : IsDomain R
inst✝ : DecidableEq R
n : ℕ
p : MvPolynomial (Fin (n + 1)) R
hp : p ≠ 0
S : Fin (n + 1) → Finset R
p' : Polynomial (MvPolynomial (Fin n) R) := (finSuccEquiv R n) p
hp' : p' = (finSuccEquiv R n) p
k : ℕ := p'.natDegree
hk : k = p'.natDegree
pₖ : MvPolynomial (Fin n) R := p'.leadingCoeff
hpₖ : pₖ = p'.leadingCoeff
hp'₀ : p' ≠ 0
⊢ ↑(#(filter (fun x => (eval x) p = 0) (piFinset fun i => S i))) / ∏ i : Fin (n + 1), ↑(#(S i)) ≤
    p.support.sup fun s => ∑ i : Fin (n + 1), ↑(s i) / ↑(#(S i))

Tactic:
have hpₖ₀ : pₖ ≠ 0 := by simpa [pₖ, k]

Result:
R : Type u_1
inst✝² : CommRing R
inst✝¹ : IsDomain R
inst✝ : DecidableEq R
n : ℕ
p : MvPolynomial (Fin (n + 1)) R
hp : p ≠ 0
S : Fin (n + 1) → Finset R
p' : Polynomial (MvPolynomial (Fin n) R) := (finSuccEquiv R n) p
hp' : p' = (finSuccEquiv R n) p
k : ℕ := p'.natDegree
hk : k = p'.natDegree
pₖ : MvPolynomial (Fin n) R := p'.leadingCoeff
hpₖ : pₖ = p'.leadingCoeff
hp'₀ : p' ≠ 0
hpₖ₀ : pₖ ≠ 0
⊢ ↑(#(filter (fun x => (eval x) p = 0) (piFinset fun i => S i))) / ∏ i : Fin (n + 1), ↑(#(S i)) ≤
    p.support.sup fun s => ∑ i : Fin (n + 1), ↑(s i) / ↑(#(S i))

Proof state:
R : Type u_1
inst✝² : CommRing R
inst✝¹ : IsDomain R
inst✝ : DecidableEq R
n : ℕ
p : MvPolynomial (Fin (n + 1)) R
hp : p ≠ 0
S : Fin (n + 1) → Finset R
p' : Polynomial (MvPolynomial (Fin n) R) := (finSuccEquiv R n) p
hp' : p' = (finSuccEquiv R n) p
k : ℕ := p'.natDegree
hk : k = p'.natDegree
pₖ : MvPolynomial (Fin n) R := p'.leadingCoeff
hpₖ : pₖ = p'.leadingCoeff
hp'₀ : p' ≠ 0
hpₖ₀ : pₖ ≠ 0
⊢ ∑ _xₜ ∈ piFinset fun i => tail S i, degreeOf 0 p = degreeOf 0 p * ∏ i : Fin n, #(S i.succ)

Tactic:
simp [mul_comm, tail]

Result:
no goals

Proof state:
case calc_1.h₂.hab.h.h
R : Type u_1
inst✝² : CommRing R
inst✝¹ : IsDomain R
inst✝ : DecidableEq R
n : ℕ
p : MvPolynomial (Fin (n + 1)) R
hp : p ≠ 0
S : Fin (n + 1) → Finset R
p' : Polynomial (MvPolynomial (Fin n) R) := (finSuccEquiv R n) p
hp' : p' = (finSuccEquiv R n) p
k : ℕ := p'.natDegree
hk : k = p'.natDegree
pₖ : MvPolynomial (Fin n) R := p'.leadingCoeff
hpₖ : pₖ = p'.leadingCoeff
hp'₀ : p' ≠ 0
hpₖ₀ : pₖ ≠ 0
xₜ : Fin n → R
hxₜ : xₜ ∈ filter (fun xₜ => (eval xₜ) pₖ ≠ 0) (piFinset fun i => tail S i)
pₓ : Polynomial R := Polynomial.map (eval xₜ) p'
hpₓ : pₓ = Polynomial.map (eval xₜ) p'
hpₓdeg : pₓ.natDegree = k
hpₓ₀ : pₓ ≠ 0
⊢ #(filter (fun x₀ => (eval (Fin.cons x₀ xₜ)) p = 0) (S 0)) ≤ degreeOf 0 p

Tactic:
calc
  #{x₀ ∈ S 0 | eval (cons x₀ xₜ) p = 0} ≤ #pₓ.roots.toFinset := by
    gcongr
    simp +contextual [subset_iff, eval_eq_eval_mv_eval', pₓ, hpₓ₀, p']
  _ ≤ Multiset.card pₓ.roots := pₓ.roots.toFinset_card_le
  _ ≤ pₓ.natDegree := pₓ.card_roots'
  _ = k := hpₓdeg
  _ ≤ p.degreeOf 0 := by
    have :
      (ofLex (AddMonoidAlgebra.supDegree toLex p'.leadingCoeff)).cons k ∈ p.support := by
      rwa [← support_coeff_finSuccEquiv, mem_support_iff, ← hp', hk,
        ← Polynomial.leadingCoeff, ← hpₖ, ← leadingCoeff_toLex,
        AddMonoidAlgebra.leadingCoeff_ne_zero toLex.injective]
    simpa using monomial_le_degreeOf 0 this

Result:
no goals

Proof state:
R : Type u_1
inst✝² : CommRing R
inst✝¹ : IsDomain R
inst✝ : DecidableEq R
n : ℕ
p : MvPolynomial (Fin (n + 1)) R
hp : p ≠ 0
S : Fin (n + 1) → Finset R
p' : Polynomial (MvPolynomial (Fin n) R) := (finSuccEquiv R n) p
hp' : p' = (finSuccEquiv R n) p
k : ℕ := p'.natDegree
hk : k = p'.natDegree
pₖ : MvPolynomial (Fin n) R := p'.leadingCoeff
hpₖ : pₖ = p'.leadingCoeff
hp'₀ : p' ≠ 0
hpₖ₀ : pₖ ≠ 0
xₜ : Fin n → R
hxₜ : xₜ ∈ filter (fun xₜ => (eval xₜ) pₖ ≠ 0) (piFinset fun i => tail S i)
pₓ : Polynomial R := Polynomial.map (eval xₜ) p'
hpₓ : pₓ = Polynomial.map (eval xₜ) p'
hpₓdeg : pₓ.natDegree = k
hpₓ₀ : pₓ ≠ 0
⊢ Finsupp.cons k (ofLex (AddMonoidAlgebra.supDegree (⇑toLex) p'.leadingCoeff)) ∈ p.support

Tactic:
rwa [← support_coeff_finSuccEquiv, mem_support_iff, ← hp', hk,
  ← Polynomial.leadingCoeff, ← hpₖ, ← leadingCoeff_toLex,
  AddMonoidAlgebra.leadingCoeff_ne_zero toLex.injective]

Result:
no goals

Proof state:
R : Type u_1
inst✝² : CommRing R
inst✝¹ : IsDomain R
inst✝ : DecidableEq R
n : ℕ
p : MvPolynomial (Fin (n + 1)) R
hp : p ≠ 0
S : Fin (n + 1) → Finset R
p' : Polynomial (MvPolynomial (Fin n) R) := (finSuccEquiv R n) p
hp' : p' = (finSuccEquiv R n) p
k : ℕ := p'.natDegree
hk : k = p'.natDegree
pₖ : MvPolynomial (Fin n) R := p'.leadingCoeff
hpₖ : pₖ = p'.leadingCoeff
hp'₀ : p' ≠ 0
hpₖ₀ : pₖ ≠ 0
⊢ ↑(#(filter (fun x => (eval (tail x)) pₖ = 0) (piFinset fun i => S i))) / ∏ i : Fin (n + 1), ↑(#(S i)) =
    ↑(#(S 0)) * ↑(#(filter (fun xₜ => (eval xₜ) pₖ = 0) (piFinset fun i => tail S i))) /
      (↑(#(S 0)) * ∏ i : Fin n, ↑(#(S i.succ)))

Tactic:
rw [card_consEquiv_filter_piFinset S fun x ↦ eval x pₖ = 0, prod_univ_succ, tail_def]

Result:
R : Type u_1
inst✝² : CommRing R
inst✝¹ : IsDomain R
inst✝ : DecidableEq R
n : ℕ
p : MvPolynomial (Fin (n + 1)) R
hp : p ≠ 0
S : Fin (n + 1) → Finset R
p' : Polynomial (MvPolynomial (Fin n) R) := (finSuccEquiv R n) p
hp' : p' = (finSuccEquiv R n) p
k : ℕ := p'.natDegree
hk : k = p'.natDegree
pₖ : MvPolynomial (Fin n) R := p'.leadingCoeff
hpₖ : pₖ = p'.leadingCoeff
hp'₀ : p' ≠ 0
hpₖ₀ : pₖ ≠ 0
⊢ ↑(#(S 0) * #(filter (fun r => (eval r) pₖ = 0) (piFinset fun k => S k.succ))) /
      (↑(#(S 0)) * ∏ i : Fin n, ↑(#(S i.succ))) =
    ↑(#(S 0)) * ↑(#(filter (fun xₜ => (eval xₜ) pₖ = 0) (piFinset fun i => (fun k => S k.succ) i))) /
      (↑(#(S 0)) * ∏ i : Fin n, ↑(#(S i.succ)))

Proof state:
R : Type u_1
inst✝² : CommRing R
inst✝¹ : IsDomain R
inst✝ : DecidableEq R
n : ℕ
p : MvPolynomial (Fin (n + 1)) R
hp : p ≠ 0
S : Fin (n + 1) → Finset R
p' : Polynomial (MvPolynomial (Fin n) R) := (finSuccEquiv R n) p
hp' : p' = (finSuccEquiv R n) p
k : ℕ := p'.natDegree
hk : k = p'.natDegree
pₖ : MvPolynomial (Fin n) R := p'.leadingCoeff
hpₖ : pₖ = p'.leadingCoeff
hp'₀ : p' ≠ 0
hpₖ₀ : pₖ ≠ 0
⊢ ↑(#(filter (fun x => (eval x) p = 0) (piFinset fun i => S i))) / ∏ i : Fin (n + 1), ↑(#(S i)) =
    ↑(#(filter (fun x => (eval x) p = 0 ∧ (eval (tail x)) pₖ = 0 ∨ (eval x) p = 0 ∧ (eval (tail x)) pₖ ≠ 0)
              (piFinset fun i => S i)) +
          #(filter (fun a => ((eval a) p = 0 ∧ (eval (tail a)) pₖ = 0) ∧ (eval a) p = 0 ∧ (eval (tail a)) pₖ ≠ 0)
              (piFinset fun i => S i))) /
      ∏ i : Fin (n + 1), ↑(#(S i))

Tactic:
simp [← and_or_left, em, and_and_and_comm]

Result:
no goals