import Mathlib.Tacticnamespace AdditiveThreshold/-- Subset sums including the empty sum. Each list entry is a separate index. -/def allSums : List ℕ → Finset ℕ| [] => {0}| a::xs => allSums xs ∪ (allSums xs).image (a + ·)theorem zero_mem (xs : List ℕ) : 0 ∈ allSums xs := byinduction xs with| nil => simp [allSums]| cons a xs ih => exact Finset.mem_union_left _ ihprivate theorem add_increases (S : Finset ℕ) (hne : S.Nonempty) (a : ℕ) (ha : 0<a) :S.card+1 ≤ (S ∪ S.image (a + ·)).card := bylet m := S.max' hnehave hm : m ∈ S := Finset.max'_mem _ _have hnew : a+m ∉ S := byintro hxhave hh := Finset.le_max' S (a+m) hxchange a+m ≤ m at hhomegahave hs : insert (a+m) S ⊆ S ∪ S.image (a + ·) := byintro v hvrcases Finset.mem_insert.mp hv with h|h· subst v; exact Finset.mem_union_right _ (Finset.mem_image.mpr ⟨m,hm,rfl⟩)· exact Finset.mem_union_left _ hsimpa [hnew] using Finset.card_le_card hstheorem at_least_one_more (xs : List ℕ) (hp : ∀ i ∈ xs, 0 < i) :xs.length+1 ≤ (allSums xs).card := byinduction xs with| nil => simp [allSums]| cons a xs ih =>have h := add_increases (allSums xs) ⟨0,zero_mem xs⟩ a (hp a (by simp))have hi := ih (by intro i hz; exact hp i (by simp [hz]))simp only [List.length_cons,allSums]; omegatheorem unequal_pair_growth (xs : List ℕ) (a b : ℕ) (ha : 0<a) (hb : 0<b) (hab : a≠b)(hp : ∀ i ∈ xs, 0 < i) : xs.length+4 ≤ (allSums (xs++[a,b])).card := byinduction xs with| nil =>have hset : allSums [a,b] = {0,b,a,a+b} := by ext n; simp [allSums]; tautosimp only [List.nil_append,List.length_nil,Nat.zero_add,hset,Finset.card_insert_eq_ite,Finset.mem_insert,Finset.mem_singleton,Finset.card_singleton]repeat' first | omega | split| cons c xs ih =>have hi := ih (by intro i hz; exact hp i (by simp [hz]))have h := add_increases (allSums (xs++[a,b])) ⟨0,zero_mem _⟩ c (hp c (by simp))simp only [List.length_cons,List.cons_append,allSums]; omega/-- A group of size e+1 supplies e+2 different projected subset sums. -/def gridSize : List ℕ → ℕ| [] => 1| e::es => (e+2)*gridSize estheorem flag_product_bound (es : List ℕ) :2^es.length*(es.sum+2) ≤ 2*gridSize es := byinduction es with| nil => decide| cons e es ih =>simp only [List.length_cons,pow_succ,List.sum_cons,gridSize]have hh := Nat.mul_le_mul_left (e+2) ihnlinarith only [hh, Nat.zero_le (2^es.length*es.sum*e)]theorem two_block_equality (s t : ℕ) (hs : 0<s) (ht : 0<t) :(s+1)*(t+1)=2*(s+t) ↔ s=1 ∨ t=1 := byobtain ⟨a,rfl⟩ := Nat.exists_eq_succ_of_ne_zero (Nat.ne_of_gt hs)obtain ⟨b,rfl⟩ := Nat.exists_eq_succ_of_ne_zero (Nat.ne_of_gt ht)constructor· intro hhave hz : a*b=0 := by nlinarithrcases Nat.mul_eq_zero.mp hz with h|h <;> omega· rintro (h|h) <;> simp at h <;> simp [h] <;> ringprivate theorem two_power_lower (d : ℕ) (hd : 3≤d) : 2*(d:ℤ)+2 ≤ (2:ℤ)^d := byinduction d,hd using Nat.le_induction with| base => norm_num| succ d hd ih =>rw [pow_succ, Nat.cast_succ]; linariththeorem high_rank_gap (d k : ℕ) (hd : 3≤d) (hk : 4≤k) (hdk : d≤k) :(d:ℤ)*(2*k-1)+2 < (2:ℤ)^d*((k:ℤ)-d+2) := byinduction d,hd using Nat.le_induction with| base =>norm_num at *omega| succ d hd ih =>have hdk' : d≤k := by omegahave hgap := ih hdk'have hh := two_power_lower d hdhave hdiff : 1 ≤ (k:ℤ)-d := by omegahave hm := mul_le_mul_of_nonneg_right hh (show 0≤(k:ℤ)-d by omega)have hz : 0 ≤ (d:ℤ)*((k:ℤ)-d-1) := mul_nonneg (by positivity) (by omega)rw [Nat.cast_succ, pow_succ]nlinarith only [hgap,hm,hz]/-- Exact unpruned count for the rank-two shape with r copies of a. -/theorem count_pairs (r q s : ℕ) :2 * (∑ i ∈ Finset.range q, (r*(q-1-i)+s)) =r*q*(q-1)+2*q*s := byinduction q with| zero => simp| succ q ih =>rw [Finset.sum_range_succ']simp only [Nat.succ_sub_one,Nat.sub_zero]have he (i : ℕ) : q-(i+1)=q-1-i := by omegahave hh : (∑ i ∈ Finset.range q, (r*(q-(i+1))+s)) =∑ i ∈ Finset.range q, (r*(q-1-i)+s) :=Finset.sum_congr rfl (fun i _ => congrArg (fun x => r*x+s) (he i))rw [hh]cases q with| zero => simp| succ q =>simp only [Nat.succ_sub_one] at *nlinarith only [ih]theorem repeated_sums (a r : ℕ) :allSums (List.replicate r a) = (Finset.range (r+1)).image (fun j => j*a) := byinduction r with| zero => simp [allSums,Finset.range_one]| succ r ih =>rw [List.replicate_succ, allSums, ih]ext vsimp only [Finset.mem_union,Finset.mem_image,Finset.mem_range]constructor· rintro (⟨j,hj,rfl⟩|⟨_,⟨j,hj,rfl⟩,rfl⟩)· exact ⟨j,by omega,rfl⟩· exact ⟨j+1,by omega,by ring⟩· rintro ⟨j,hj,rfl⟩cases j with| zero => exact Or.inl ⟨0,by omega,rfl⟩| succ j => exact Or.inr ⟨j*a,⟨j,by omega,rfl⟩,by ring⟩theorem repeated_and_one (a b r v : ℕ) :v ∈ allSums (b::List.replicate r a) ↔(∃ j ≤ r, j*a=v) ∨ (∃ j ≤ r, b+j*a=v) := byrw [allSums, repeated_sums]simp only [Finset.mem_union,Finset.mem_image,Finset.mem_range]constructor· rintro (⟨j,hj,rfl⟩|⟨_,⟨j,hj,rfl⟩,rfl⟩)· exact Or.inl ⟨j,by omega,rfl⟩· exact Or.inr ⟨j,by omega,rfl⟩· rintro (⟨j,hj,rfl⟩|⟨j,hj,rfl⟩)· exact Or.inl ⟨j,by omega,rfl⟩· exact Or.inr ⟨j*a,⟨j,by omega,rfl⟩,rfl⟩open Filteropen scoped Topologydef occurs (k : ℕ) (A : Finset ℕ) : Prop :=∃ xs : List ℕ, xs.length=k ∧ (∀ i ∈ xs, 0 < i) ∧ (allSums xs).erase 0 ⊆ Anoncomputable def probability (k N : ℕ) (p : ℝ) : ℝ := byclassicalexact ∑ A ∈ (Finset.Icc 1 N).powerset,if occurs k A then p^A.card*(1-p)^(N-A.card) else 0/-- Existence version of the limiting law stated and proved in the accompanying paper. -/def FiniteSumsLaw : Prop :=∀ k : ℕ, 0 < k → ∀ (p : ℕ → ℝ) (lam : ℝ), 0 < lam →(∀ N, p N ∈ Set.Icc 0 1) →Tendsto (fun N : ℕ => (N:ℝ)^(if k≤3 then k else 2) *(p N)^(if k≤3 then 2^k-1 else 2*k-1)) atTop (𝓝 lam) →Tendsto (fun N => probability k N (p N)) atTop(𝓝 (1-Real.exp (-lam/(if k≤3 then (k.factorial:ℝ)^2 else 2*((k:ℝ)-1)))))#print axioms at_least_one_more#print axioms unequal_pair_growth#print axioms flag_product_bound#print axioms two_block_equality#print axioms high_rank_gap#print axioms count_pairs#print axioms repeated_sums#print axioms repeated_and_oneend AdditiveThreshold