import Mathlib.Algebra.Order.BigOperators.GroupWithZero.Finsetimport Mathlib.Data.Finset.Powersetimport Mathlib.Order.Preorder.Finiteimport Mathlib.Probability.BorelCantelliimport Mathlib.Tacticnamespace AffineIP/-- Simultaneously extend finitely many translates and positive dilates. -/def Rich (A : Set ℕ) : Prop :=∀ (S P : Finset ℕ) (M : ℕ), ∃ x > M,(∀ s ∈ S, x + s ∈ A) ∧ (∀ p ∈ P, 0 < p → x * p ∈ A)def Good (A : Set ℕ) (s : Finset ℕ) : Prop :=(∀ i ∈ s, 0 < i) ∧ ∀ t ⊆ s, t.Nonempty →(∑ i ∈ t, i) ∈ A ∧ (∏ i ∈ t, i) ∈ Aprivate theorem extend {A : Set ℕ} (h : Rich A) {s : Finset ℕ}(hs : Good A s) (M : ℕ) :∃ x > M, Good A (insert x s) := byclassicallet S := s.powerset.image (fun t => ∑ i ∈ t, i)let P := s.powerset.image (fun t => ∏ i ∈ t, i)obtain ⟨x, hx, hS, hP⟩ := h S P Mrefine ⟨x, hx, ?_, ?_⟩· intro i hircases Finset.mem_insert.mp hi with rfl | hi· omega· exact hs.1 i hi· intro t ht hneby_cases hxt : x ∈ t· let u := t.erase xhave hus : u ⊆ s := byintro i hihave hmem := Finset.mem_erase.mp hiexact (Finset.mem_insert.mp (ht hmem.2)).resolve_left hmem.1have hp : u ∈ s.powerset := Finset.mem_powerset.mpr hushave ha := hS (∑ i ∈ u, i) (Finset.mem_image.mpr ⟨u, hp, rfl⟩)have hb := hP (∏ i ∈ u, i) (Finset.mem_image.mpr ⟨u, hp, rfl⟩)(Finset.prod_pos (fun i hi => hs.1 i (hus hi)))have hn : x ∉ u := Finset.notMem_erase x thave heq : insert x u = t := Finset.insert_erase hxtrw [← heq, Finset.sum_insert hn, Finset.prod_insert hn]exact ⟨ha, hb⟩· apply hs.2 t _ hneintro i hiexact (Finset.mem_insert.mp (ht hi)).resolve_left (fun e => hxt (e ▸ hi))/-- One infinite set of positive integers has both kinds of finite reductions in A. -/theorem infinite_sums_and_products {A : Set ℕ} (h : Rich A) :∃ B : Set ℕ, B.Infinite ∧ (∀ i ∈ B, 0 < i) ∧∀ t : Finset ℕ, (↑t : Set ℕ) ⊆ B → t.Nonempty →(∑ i ∈ t, i) ∈ A ∧ (∏ i ∈ t, i) ∈ A := byclassicallet G := {s : Finset ℕ // Good A s}let val (n : ℕ) (s : G) := (extend h s.property n).choosehave spec (n : ℕ) (s : G) : n < val n s ∧ Good A (insert (val n s) s.val) :=(extend h s.property n).choose_spechave he : Good A ∅ := by simp [Good]let T : ℕ → G := Nat.rec ⟨∅, he⟩ (fun n s => ⟨insert (val n s) s.val, (spec n s).2⟩)have grow (n : ℕ) : (T n).val ⊆ (T (n+1)).val := Finset.subset_insert _ _have mono : Monotone (fun n => (T n).val) := monotone_nat_of_le_succ growlet B : Set ℕ := {i | ∃ n, i ∈ (T n).val}refine ⟨B, Set.infinite_of_forall_exists_gt ?_, ?_, ?_⟩· intro nrefine ⟨val n (T n), ⟨n+1, ?_⟩, (spec n (T n)).1⟩exact Finset.mem_insert_self _ _· rintro i ⟨n, hn⟩exact (T n).property.1 i hn· intro t ht hnehave bounded (u : Finset ℕ) (hu : (↑u : Set ℕ) ⊆ B) : ∃ n, u ⊆ (T n).val := byinduction u using Finset.induction_on with| empty => exact ⟨0, Finset.empty_subset _⟩| @insert a s ha ih =>obtain ⟨j,hj⟩ := hu (Finset.mem_insert_self a s)obtain ⟨k,hk⟩ := ih (fun i hi => hu (Finset.mem_insert_of_mem hi))refine ⟨max j k, Finset.insert_subset ?_ ?_⟩· exact mono (le_max_left j k) hj· exact hk.trans (mono (le_max_right j k))obtain ⟨n, hn⟩ := bounded t htexact (T n).property.2 t hn hneopen MeasureTheory ProbabilityTheory Filteropen scoped ENNRealprivate def block (S P : Finset ℕ) (x : ℕ) : Finset ℕ :=S.image (x + ·) ∪ (P.filter (0 < ·)).image (x * ·)private lemma le_of_mem_block {S P : Finset ℕ} {x y : ℕ} (hy : y ∈ block S P x) : x ≤ y := byrcases Finset.mem_union.mp hy with h | h· obtain ⟨i, _, rfl⟩ := Finset.mem_image.mp homega· obtain ⟨i, hi, rfl⟩ := Finset.mem_image.mp hexact Nat.le_mul_of_pos_right _ (Finset.mem_filter.mp hi).2private lemma block_events {Ω : Type*} [MeasurableSpace Ω] {μ : Measure Ω}{E : ℕ → Set Ω} (hm : ∀ i, MeasurableSet (E i)) (hi : iIndepSet E μ)(b : ℕ → Finset ℕ) (hd : Pairwise (fun i j => Disjoint (b i) (b j))) :iIndepSet (fun n => ⋂ j ∈ b n, E j) μ := byclassicalapply (iIndepSet_iff_meas_biInter (fun _ => MeasurableSet.iInter (fun _ =>MeasurableSet.iInter (fun _ => hm _)))).2intro shave he : (⋂ i ∈ s, ⋂ j ∈ b i, E j) = ⋂ j ∈ s.biUnion b, E j := byext wsimp only [Set.mem_iInter, Finset.mem_biUnion]aesoprw [he, hi.meas_biInter, Finset.prod_biUnion (fun i _ j _ hij => hd hij)]exact Finset.prod_congr rfl (fun i _ => (hi.meas_biInter (b i)).symm)private theorem ae_extension {Ω : Type*} [MeasurableSpace Ω] {μ : Measure Ω}[IsProbabilityMeasure μ] {E : ℕ → Set Ω} (hm : ∀ i, MeasurableSet (E i))(hi : iIndepSet E μ) {p : ℝ≥0∞} (hp : 0 < p) (hle : p ≤ 1) (hprob : ∀ i, 0 < i → p ≤ μ (E i))(S P : Finset ℕ) (M : ℕ) :∀ᵐ w ∂μ, ∃ x > M, (∀ s ∈ S, w ∈ E (x+s)) ∧ (∀ t ∈ P, 0 < t → w ∈ E (x*t)) := byclassicallet trials : ℕ → ℕ := Nat.rec (M+1) (fun _ x => max x ((block S P x).sup id) + 1)have hstep (n : ℕ) : trials n < trials (n+1) := Nat.lt_succ_of_le (le_max_left _ _)have hstrict := strictMono_nat_of_lt_succ hsteplet b (n : ℕ) := block S P (trials n)have disj (i j : ℕ) (hij : i < j) : Disjoint (b i) (b j) := byrefine Finset.disjoint_left.mpr (fun {y} hy hy' => ?_)have h1 : y ≤ (block S P (trials i)).sup id := Finset.le_sup (f := id) hyhave h2 : (block S P (trials i)).sup id < trials (i+1) := Nat.lt_succ_of_le (le_max_right _ _)have h3 : trials (i+1) ≤ trials j := hstrict.monotone hijhave h4 : trials j ≤ y := le_of_mem_block hy'omegahave hd : Pairwise (fun i j => Disjoint (b i) (b j)) := byintro i j hijrcases lt_or_gt_of_ne hij with h | h· exact disj i j h· exact (disj j i h).symmlet events (n : ℕ) := ⋂ j ∈ b n, E jhave hmeas (n : ℕ) : MeasurableSet (events n) := MeasurableSet.iInter(fun _ => MeasurableSet.iInter (fun _ => hm _))have bound (n : ℕ) : p^(S.card+P.card) ≤ μ (events n) := byhave hcard : (b n).card ≤ S.card+P.card :=(Finset.card_union_le _ _).trans (Nat.add_le_add Finset.card_image_le(Finset.card_image_le.trans (Finset.card_filter_le _ _)))change _ ≤ μ (⋂ j ∈ b n, E j)rw [hi.meas_biInter]calc_ ≤ p^(b n).card := pow_le_pow_of_le_one hp.le hle hcard_ = ∏ _j ∈ b n, p := by simp_ ≤ _ := Finset.prod_le_prod' (fun i hi' => hprob i (lt_of_lt_of_le(lt_of_lt_of_le (Nat.succ_pos M) (hstrict.monotone (Nat.zero_le n)))(le_of_mem_block hi')))have hts : (∑' n, μ (events n)) = ∞ := byapply top_uniquecalc∞ = ∑' _n : ℕ, p^(S.card+P.card) := (ENNReal.tsum_const_eq_top_of_ne_zero (pow_ne_zero _ hp.ne')).symm_ ≤ _ := ENNReal.tsum_le_tsum boundhave hfull := measure_limsup_eq_one hmeas (block_events hm hi b hd) htshave hae : limsup events atTop ∈ ae μ := (mem_ae_iff_prob_eq_one (MeasurableSet.measurableSet_limsup hmeas)).2 hfullfilter_upwards [hae] with w hwobtain ⟨n, hn⟩ := (mem_limsup_iff_frequently_mem.mp hw).existshave hh : ∀ j ∈ b n, w ∈ E j := by simpa [events] using hnrefine ⟨trials n, lt_of_lt_of_le (Nat.lt_succ_self M) (hstrict.monotone (Nat.zero_le n)), ?_, ?_⟩· intro s hsexact hh _ (Finset.mem_union_left _ (Finset.mem_image.mpr ⟨s,hs,rfl⟩))· intro t ht htposexact hh _ (Finset.mem_union_right _ (Finset.mem_image.mpr ⟨t,Finset.mem_filter.mpr ⟨ht,htpos⟩,rfl⟩))/-- Independent membership events with a common positive lower bound almost surelycontain all finite sums and products of one infinite set of positive generators. -/theorem ae_infinite_sums_and_products {Ω : Type*} [MeasurableSpace Ω] {μ : Measure Ω}[IsProbabilityMeasure μ] {E : ℕ → Set Ω} (hm : ∀ i, MeasurableSet (E i))(hi : iIndepSet E μ) {p : ℝ≥0∞} (hp : 0 < p) (hle : p ≤ 1) (hprob : ∀ i, 0 < i → p ≤ μ (E i)) :∀ᵐ w ∂μ, ∃ B : Set ℕ, B.Infinite ∧ (∀ i ∈ B, 0 < i) ∧∀ t : Finset ℕ, (↑t : Set ℕ) ⊆ B → t.Nonempty →w ∈ E (∑ i ∈ t, i) ∧ w ∈ E (∏ i ∈ t, i) := byhave rich : ∀ᵐ w ∂μ, Rich {i | w ∈ E i} :=ae_all_iff.2 (fun S => ae_all_iff.2 (fun P => ae_all_iff.2(fun M => ae_extension hm hi hp hle hprob S P M)))filter_upwards [rich] with w hwexact infinite_sums_and_products hw#print axioms ae_infinite_sums_and_productsend AffineIP