import Mathlib.Analysis.SpecialFunctions.Gamma.Basicimport Mathlib.Tacticnamespace ProductLayers/-- The largest part is listed first. This recursion also makes sense without sorting. -/def profileCost : List ℕ → ℕ| [] => 0| r::rs => r^2+(r+1)*profileCost rsdef target : ℕ → ℕ| 0 => 0| n+1 => n*(n+1)+1theorem target_polynomial {k : ℕ} (hk : 0<k) : target k+k=k^2+1 := byobtain ⟨n,rfl⟩ := Nat.exists_eq_succ_of_ne_zero (Nat.ne_of_gt hk)simp [target]; ringtheorem step_gap (r n : ℕ) :r^2+(r+1)*target (n+2) = target (r+(n+2)) + r*(n+1)*n := bysimp [target, Nat.add_assoc]; ringprivate theorem step_one (r : ℕ) : r^2+(r+1)*target 1=target (r+1) := bysimp [target]; ringtheorem profile_lower_bound (rs : List ℕ) (hp : ∀ r ∈ rs, 0<r) :target rs.sum ≤ profileCost rs := byinduction rs with| nil => simp [target,profileCost]| cons r rs ih =>have hr := hp r (by simp)have ht := ih (by intro i hi; exact hp i (by simp [hi]))simp only [List.sum_cons,profileCost]apply le_trans _ (Nat.add_le_add_left (Nat.mul_le_mul_left (r+1) ht) _)generalize h : rs.sum = scases s with| zero =>obtain ⟨n,rfl⟩ := Nat.exists_eq_succ_of_ne_zero (Nat.ne_of_gt hr)simp [target]; nlinarith| succ s =>cases s with| zero => exact (step_one r).symm.le| succ n => rw [step_gap]; exact Nat.le_add_right _ _private theorem sum_zero_tail (rs : List ℕ) (hp : ∀ r ∈ rs, 0<r)(hs : rs.sum=0) : rs=[] := bycases rs with| nil => rfl| cons i t =>have := hp i (by simp)simp only [List.sum_cons] at hsomegaprivate theorem sum_one_tail (rs : List ℕ) (hp : ∀ r ∈ rs, 0<r)(hs : rs.sum=1) : rs=[1] := bycases rs with| nil => simp at hs| cons i t =>have := hp i (by simp)simp only [List.sum_cons] at hshave ht := sum_zero_tail t (by intro z hz; exact hp z (by simp [hz])) (by omega)simp [ht, show i=1 by omega]theorem profile_equality (r : ℕ) (rs : List ℕ) (hr : 0<r) (hp : ∀ s ∈ rs, 0<s) :profileCost (r::rs) = target (r+rs.sum) ↔(r=1 ∧ rs=[]) ∨ rs=[1] ∨ rs=[1,1] := byconstructor· intro hechange r^2+(r+1)*profileCost rs = _ at hehave ht := profile_lower_bound rs hprcases rs.sum.eq_zero_or_pos with h|h· have hy := sum_zero_tail rs hp hsubst rssimp [profileCost] at heobtain ⟨n,rfl⟩ := Nat.exists_eq_succ_of_ne_zero (Nat.ne_of_gt hr)simp [target] at heleft; constructor <;> first | rfl | nlinarith· by_cases h1 : rs.sum=1· exact Or.inr (Or.inl (sum_one_tail rs hp h1))· have h2 : 2≤rs.sum := by omegalet n := rs.sum-2have hn : rs.sum=n+2 := by dsimp [n]; omegarw [hn] at he hthave hle := Nat.add_le_add_left (Nat.mul_le_mul_left (r+1) ht) (r^2)rw [step_gap,he] at hlehave hz : n=0 := byby_contra! hposhave hpos' : 0<r*(n+1)*n := by positivityomegahave hs : rs.sum=2 := by omegacases rs with| nil => simp at hs| cons a t =>have ha := hp a (by simp)simp only [List.sum_cons] at hsby_cases hzero : t.sum=0· have hy := sum_zero_tail t (by intro z h'; exact hp z (by simp [h'])) hzerosubst thave ha2 : a=2 := by simpa using hssubst arw [hz] at hesimp [profileCost,target] at henlinarith· have ht' := sum_one_tail t (by intro z h'; exact hp z (by simp [h'])) (by omega)exact Or.inr (Or.inr (by simp [ht',show a=1 by omega]))· rintro (⟨rfl,rfl⟩|rfl|rfl) <;> simp [profileCost,target] <;> ring/-- Triangular prefix data distinguish the label indexed by every tuple of prefix lengths. -/theorem prefix_injective {d : ℕ} (r : Fin d → ℕ)(P : (i : Fin d) → Fin (r i+1) → Fin d → ℕ)(tri : ∀ (i : Fin d) (n : Fin (r i+1)) (j : Fin d), j < i → P i n j = 0)(diag : ∀ i, StrictMono (fun n => P i n i)) :Function.Injective (fun (z : (i : Fin d) → Fin (r i+1)) =>fun j => ∑ i, P i (z i) j) := byclassicalintro z w heby_contra! hnlet T : Finset (Fin d) := Finset.univ.filter (fun i => z i ≠ w i)have hT : T.Nonempty := byby_contra! hemapply hn; funext iby_contra! hhave hx : i ∈ T := by simp [T,h]simp [hem] at hxlet i := T.min' hThave him : i ∈ T := Finset.min'_mem _ _have hi := (Finset.mem_filter.mp him).2have hrest : (∑ j ∈ Finset.univ.erase i, P j (z j) i) =∑ j ∈ Finset.univ.erase i, P j (w j) i := byapply Finset.sum_congr rflintro j hjrcases lt_or_gt_of_ne (Finset.mem_erase.mp hj).1 with hj'|hj'· have hjw : z j=w j := byby_contra! hmhave hmem : j∈T := by simp [T,hm]exact (not_le_of_gt hj') (Finset.min'_le T j hmem)rw [hjw]· rw [tri j _ i hj', tri j _ i hj']have heq := congrFun he ichange ∑ j, P j (z j) i = ∑ j, P j (w j) i at heqrw [← Finset.sum_erase_add _ _ (Finset.mem_univ i),← Finset.sum_erase_add _ _ (Finset.mem_univ i), hrest] at heqexact hi ((diag i).injective (by omega))/-- Products of the parameter factors in the degree layers recover their full weighted product. -/theorem layer_product {ι : Type*} [Fintype ι] (d y : ι → ℕ) (M : ℕ) (hd : ∀ i, d i ≤ M) :(∏ h ∈ Finset.range M, ∏ i, if h < d i then y i else 1) = ∏ i, y i ^ d i := byclassicalrw [Finset.prod_comm]apply Finset.prod_congr rflintro i hihave hr : (Finset.range M).filter (fun h => h < d i) = Finset.range (d i) := byhave := hd iext h; simp; omegarw [Finset.prod_ite]simp [hr]theorem layer_count {ι : Type*} [Fintype ι] (cost : ι → ℕ) (M : ℕ) (hc : ∀ i, cost i ≤ M) :(∑ h ∈ Finset.range M, ∑ i, if h < cost i then 1 else 0) = ∑ i, cost i := byclassicalrw [Finset.sum_comm]apply Finset.sum_congr rflintro i hihave hr : (Finset.range M).filter (fun h => h < cost i) = Finset.range (cost i) := byhave := hc iext h; simp; omegarw [Finset.sum_ite]simp [hr]theorem main_dimensions (r : ℕ) :target (r+2)=r^2+3*(r+1) ∧profileCost [r,1,1] = target (r+2) ∧profileCost [r+1,1] = target (r+2) := byconstructor· simp [target]; ring· constructor <;> simp [target,profileCost] <;> ring/-- The barycentre meets every proper-subset cut strictly in the proper-product paper. -/theorem strict_barycentre_cut {u k : ℕ} (hu : 0<u) (hk : u<k) :k*(2^u-1)<u*(2^k-1) := byobtain ⟨s,hs⟩ := Nat.exists_eq_add_of_le (show u+1≤k by omega)subst kinduction s with| zero =>simp only [Nat.add_zero, pow_succ]have hp : 1 ≤ 2^u := Nat.one_le_pow _ _ (by decide)have hp2 : 1 ≤ 2^u*2 := by omegahave hmul := Nat.mul_le_mul_right (2^u) huhave ha := Nat.sub_add_cancel hphave hb := Nat.sub_add_cancel hp2nlinarith only [hmul, congrArg (fun t => u*t) ha, congrArg (fun t => u*t) hb,ha]| succ s ih =>have hpow : 2^u ≤ 2^(u+1+s) := Nat.pow_le_pow_right (by decide) (by omega)have hmul := Nat.mul_le_mul_right (2^(u+1+s)) huhave hp : 1≤2^(u+1+s) := Nat.one_le_pow _ _ (by decide)have hstep : 2^(u+1+s+1)-1=(2^(u+1+s)-1)+2^(u+1+s) := byrw [pow_succ]; omegachange (u+1+s+1)*(2^u-1) < u*(2^(u+1+s+1)-1)rw [hstep, Nat.mul_add, Nat.add_mul, one_mul]omega/-- Number of prefix choices, including the empty one, on blocks above a degree cut. -/def activeCount (h : ℕ) : List ℕ → ℕ| [] => 1| r::rs => (if h<r then r+1 else 1)*activeCount h rsprivate theorem active_pos (rs : List ℕ) (h : ℕ) : 0<activeCount h rs := byinduction rs with| nil => simp [activeCount]| cons r rs ih =>simp only [activeCount]; split <;> positivityprivate theorem active_one (rs : List ℕ) (h : ℕ) (hb : ∀ r ∈ rs, r≤h) :activeCount h rs=1 := byinduction rs with| nil => rfl| cons r rs ih =>simp [activeCount,not_lt_of_ge (hb r (by simp)),ih (by intro x hx; exact hb x (by simp [hx]))]private theorem active_step (r h : ℕ) (rs : List ℕ) (hb : ∀ z ∈ rs, z≤r) :activeCount h (r::rs)-1 =(r+1)*(activeCount h rs-1)+(if h<r then r else 0) := byby_cases hr : h<r· simp only [activeCount,if_pos hr]have h1 := Nat.sub_add_cancel (show 1≤activeCount h rs from active_pos rs h)have h2 : 1≤(r+1)*activeCount h rs := Nat.one_le_iff_ne_zero.mpr (byhave := active_pos rs h; positivity)have h3 := Nat.sub_add_cancel h2nlinarith only [congrArg (fun t => (r+1)*t) h1, h3]· have h1 := active_one rs h (fun z hz => (hb z hz).trans (by omega))simp [activeCount,hr,h1]theorem profile_layer_formula (rs : List ℕ) (hs : rs.Pairwise (· ≥ ·)) (M : ℕ)(hm : ∀ r ∈ rs, r≤M) :(∑ h ∈ Finset.range M, (activeCount h rs-1)) = profileCost rs := byinduction rs with| nil => simp [activeCount,profileCost]| cons r rs ih =>obtain ⟨hb,ho⟩ := List.pairwise_cons.mp hshave hi := ih ho (by intro z hz; exact hm z (by simp [hz]))have hstep := fun h => active_step r h rs hbsimp_rw [hstep]rw [Finset.sum_add_distrib, ← Finset.mul_sum, hi, Finset.sum_ite]have hr : (Finset.range M).filter (fun h => h<r) = Finset.range r := byhave := hm r (by simp)ext h; simp; omegasimp [hr,profileCost,pow_two,add_comm]/-- Any label counts that contain the prefix choices at every level pay the profile bound. -/theorem counted_lower_bound (rs : List ℕ) (hs : rs.Pairwise (· ≥ ·))(hp : ∀ r ∈ rs, 0<r) (M : ℕ) (hm : ∀ r ∈ rs, r≤M)(counts : ℕ → ℕ) (hcounts : ∀ h<M, activeCount h rs≤counts h+1) :target rs.sum ≤ ∑ h ∈ Finset.range M, counts h := byapply (profile_lower_bound rs hp).transrw [← profile_layer_formula rs hs M hm]apply Finset.sum_le_sumintro h hhhave := hcounts h (Finset.mem_range.mp hh)omegaopen Filteropen scoped Topology/-- Indexed subset products allow equal generator values at distinct indices. -/def labels {k : ℕ} (a : Fin k → ℕ) : Finset ℕ :=((Finset.univ : Finset (Fin k)).powerset.erase ∅).image (fun u => u.prod a)def occurs (k : ℕ) (A : Finset ℕ) : Prop :=∃ a : Fin k → ℕ, (∀ i, 0<a i) ∧ labels a ⊆ Anoncomputable def appearanceProbability (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/-- The statement of the written limiting law for lengths r+2, r at least two. -/def UnrestrictedLaw : Prop :=∀ (r : ℕ), 2≤r → ∀ (p : ℕ → ℝ) (lam : ℝ),(∀ N, p N ∈ Set.Icc 0 1) → 0<lam →Tendsto (fun N : ℕ => (N:ℝ)*Real.log N*(p N)^(target (r+2))) atTop (𝓝 lam) →Tendsto (fun N => appearanceProbability (r+2) N (p N)) atTop(𝓝 (1-Real.exp (-Real.Gamma (1-1/(r:ℝ)) *(((r:ℝ)+1)/(2*(target (r+2):ℝ))*lam)^(1/(r:ℝ)))))#print axioms profile_layer_formula#print axioms counted_lower_bound#print axioms strict_barycentre_cut#print axioms profile_lower_bound#print axioms profile_equality#print axioms prefix_injective#print axioms layer_product#print axioms layer_count#print axioms main_dimensionsend ProductLayers