/- leanprover/lean4:v4.33.0 mathlib v4.33.0 -//-This is a Lean formalization of a solution to Erdős Problem 424.https://www.erdosproblems.com/forum/thread/424Informal authors:- ChatGPT 5.6 Pro- Samuel KorskyFormal authors:- Codex- Boris AlexeevURLs:- https://www.erdosproblems.com/forum/thread/424/proof-claims#proof-claim-91- https://drive.google.com/file/d/1SGSUQhNB8KL75VPqdfAq9yzFNEYL6Mwn/view- https://github.com/google-deepmind/formal-conjectures/blob/main/FormalConjectures/ErdosProblems/424.lean-/import Mathlib/-!# Erdős Problem 424 (Hofstadter's `ab - 1` problem)-/namespace Erdos424/-- The recursively generated set in Erdős Problem 424. Using an inductivepredicate makes the phrase "the smallest set" literal: its constructors areexactly the two seeds and the closure rule. -/inductive Generated : ℕ → Prop where| two : Generated 2| three : Generated 3| mul_sub_one {a b : ℕ} : Generated a → Generated b → a ≠ b →Generated (a * b - 1)namespace Generatedtheorem five : Generated 5 := bysimpa using mul_sub_one two three (by norm_num)theorem nine : Generated 9 := bysimpa using mul_sub_one two five (by norm_num)theorem fourteen : Generated 14 := bysimpa using mul_sub_one three five (by norm_num)theorem seventeen : Generated 17 := bysimpa using mul_sub_one two nine (by norm_num)/-- Every generated number is positive. -/theorem pos {n : ℕ} (hn : Generated n) : 0 < n := byinduction hn with| two => norm_num| three => norm_num| @mul_sub_one a b ha hb hab iha ihb =>have hab' : 1 < a * b := byrcases lt_or_gt_of_ne hab with hab | hab <;> nlinarithomegaend Generated/-- The affine generator `T_a(x) = ax - 1`. -/def T (a x : ℕ) : ℕ := a * x - 1/-- Apply a word of affine generators from left to right. -/def eval : List ℕ → ℕ → ℕ| [], x => x| a :: w, x => eval w (T a x)@[simp] theorem eval_cons (a : ℕ) (w : List ℕ) (x : ℕ) :eval (a :: w) x = eval w (T a x) := rfltheorem lt_T_self {a x : ℕ} (ha : 2 ≤ a) (hx : 1 < x) : x < T a x := bysimp only [T]have hax : x + 2 ≤ a * x := by nlinarithomega/-- Starting above every multiplier, a word made from generated multiplierscan be evaluated inside the generated set. The strict growth simultaneouslyguarantees that every use of the closure rule has distinct inputs. -/theorem eval_generated_of_lt(w : List ℕ) {x : ℕ} (hx : Generated x) (hx1 : 1 < x)(hgen : ∀ a ∈ w, Generated a) (htwo : ∀ a ∈ w, 2 ≤ a)(hlt : ∀ a ∈ w, a < x) :Generated (eval w x) ∧ x ≤ eval w x := byinduction w generalizing x with| nil => exact ⟨hx, le_rfl⟩| cons a w ih =>have ha_gen : Generated a := hgen a (by simp)have ha_two : 2 ≤ a := htwo a (by simp)have ha_lt : a < x := hlt a (by simp)have hxT : x < T a x := lt_T_self ha_two hx1have ha_ne_x : a ≠ x := Nat.ne_of_lt ha_lthave hT_gen : Generated (T a x) := byexact Generated.mul_sub_one ha_gen hx ha_ne_xhave hT1 : 1 < T a x := hx1.trans hxThave hgen' : ∀ b ∈ w, Generated b := byintro b hbexact hgen b (by simp [hb])have htwo' : ∀ b ∈ w, 2 ≤ b := byintro b hbexact htwo b (by simp [hb])have hlt' : ∀ b ∈ w, b < T a x := byintro b hbexact (hlt b (by simp [hb])).trans_le hxT.leobtain ⟨hfinal, hmono⟩ := ih hT_gen hT1 hgen' htwo' hlt'exact ⟨hfinal, hxT.le.trans hmono⟩/-- The five multipliers used by the paper. -/def multipliers : Finset ℕ := {2, 3, 5, 9, 14}theorem mem_multipliers_generated {a : ℕ} (ha : a ∈ multipliers) : Generated a := bysimp only [multipliers, Finset.mem_insert, Finset.mem_singleton] at harcases ha with rfl | rfl | rfl | rfl | rfl· exact Generated.two· exact Generated.three· exact Generated.five· exact Generated.nine· exact Generated.fourteentheorem mem_multipliers_two_le {a : ℕ} (ha : a ∈ multipliers) : 2 ≤ a := bysimp only [multipliers, Finset.mem_insert, Finset.mem_singleton] at harcases ha with rfl | rfl | rfl | rfl | rfl <;> norm_numtheorem mem_multipliers_lt_seventeen {a : ℕ} (ha : a ∈ multipliers) : a < 17 := bysimp only [multipliers, Finset.mem_insert, Finset.mem_singleton] at harcases ha with rfl | rfl | rfl | rfl | rfl <;> norm_num/-- Every word over the paper's five multipliers evaluates at 17 to an elementof the generated set, with distinct inputs at every operation. -/theorem eval_at_seventeen_generated (w : List ℕ)(hw : ∀ a ∈ w, a ∈ multipliers) : Generated (eval w 17) := byexact (eval_generated_of_lt w Generated.seventeen (by norm_num)(fun a ha ↦ mem_multipliers_generated (hw a ha))(fun a ha ↦ mem_multipliers_two_le (hw a ha))(fun a ha ↦ mem_multipliers_lt_seventeen (hw a ha))).1/-! ## Affine words -//-- Evaluation over the integers, where subtraction does not truncate. -/def evalInt : List ℕ → ℤ → ℤ| [], x => x| a :: w, x => evalInt w ((a : ℤ) * x - 1)/-- Constant term of the affine map represented by a word. -/def offset : List ℕ → ℤ| [] => 0| _ :: w => offset w - (w.prod : ℤ)@[simp] theorem evalInt_cons (a : ℕ) (w : List ℕ) (x : ℤ) :evalInt (a :: w) x = evalInt w ((a : ℤ) * x - 1) := rfl/-- A word acts as `x ↦ slope * x + offset`. -/theorem evalInt_affine (w : List ℕ) (x : ℤ) :evalInt w x = (w.prod : ℤ) * x + offset w := byinduction w generalizing x with| nil =>simp only [evalInt, offset, List.prod_nil, Nat.cast_one, one_mul, add_zero]| cons a w ih =>rw [evalInt_cons, ih]simp only [List.prod_cons, Nat.cast_mul, offset]ring/-- On inputs above 1, integer evaluation agrees with natural-numberevaluation for words whose multipliers are at least 2. -/theorem eval_cast_eq_evalInt (w : List ℕ) {x : ℕ} (hx : 1 < x)(htwo : ∀ a ∈ w, 2 ≤ a) : (eval w x : ℤ) = evalInt w x := byinduction w generalizing x with| nil => rfl| cons a w ih =>have ha : 2 ≤ a := htwo a (by simp)have hxT : x < T a x := lt_T_self ha hxhave htwo' : ∀ b ∈ w, 2 ≤ b := byintro b hbexact htwo b (by simp [hb])rw [eval_cons, evalInt_cons, ih (hx.trans hxT) htwo']simp only [T]rw [Int.ofNat_sub]· norm_num· nlinarith/-- Two words of the same slope but different offsets give different valuesat every natural input where no subtraction truncates. -/theorem eval_ne_of_prod_eq_offset_ne{u v : List ℕ} {x : ℕ} (hx : 1 < x)(hu : ∀ a ∈ u, 2 ≤ a) (hv : ∀ a ∈ v, 2 ≤ a)(hprod : u.prod = v.prod) (hoffset : offset u ≠ offset v) :eval u x ≠ eval v x := byintro hevalhave hcast : (eval u x : ℤ) = (eval v x : ℤ) := by exact_mod_cast hevalrw [eval_cast_eq_evalInt u hx hu, eval_cast_eq_evalInt v hx hv,evalInt_affine, evalInt_affine, hprod] at hcastexact hoffset (add_left_cancel hcast)/-! ## Prime exponents and the common slope -//-- Exponents of `2,3,5,7` contributed by one allowed multiplier. -/def primeIncrement (a : ℕ) : Fin 4 → ℕ :=match a with| 2 => ![1, 0, 0, 0]| 3 => ![0, 1, 0, 0]| 5 => ![0, 0, 1, 0]| 9 => ![0, 2, 0, 0]| 14 => ![1, 0, 0, 1]| _ => ![0, 0, 0, 0]/-- Prime-exponent vector accumulated along a word. -/def exponents (w : List ℕ) : Fin 4 → ℕ :=fun i ↦ (w.map fun a ↦ primeIncrement a i).sum@[simp] theorem exponents_nil (i : Fin 4) : exponents [] i = 0 := bysimp [exponents]@[simp] theorem exponents_cons (a : ℕ) (w : List ℕ) (i : Fin 4) :exponents (a :: w) i = primeIncrement a i + exponents w i := bysimp [exponents]theorem allowed_factorization {a : ℕ} (ha : a ∈ multipliers) :a = 2 ^ primeIncrement a 0 * 3 ^ primeIncrement a 1 *5 ^ primeIncrement a 2 * 7 ^ primeIncrement a 3 := bysimp only [multipliers, Finset.mem_insert, Finset.mem_singleton] at harcases ha with rfl | rfl | rfl | rfl | rfl <;> decide/-- Factorization of the slope of an allowed word. -/theorem prod_factorization (w : List ℕ) (hw : ∀ a ∈ w, a ∈ multipliers) :w.prod = 2 ^ exponents w 0 * 3 ^ exponents w 1 *5 ^ exponents w 2 * 7 ^ exponents w 3 := byinduction w with| nil => norm_num [exponents]| cons a w ih =>have ha : a ∈ multipliers := hw a (by simp)have hw' : ∀ b ∈ w, b ∈ multipliers := byintro b hbexact hw b (by simp [hb])rw [List.prod_cons, ih hw']simp only [exponents_cons]nth_rewrite 1 [allowed_factorization ha]simp only [pow_add]ring/-- The imbalance vector `H` from the paper. -/def imbalance (w : List ℕ) : Fin 3 → ℤ :=![(exponents w 0 : ℤ) - 31 * exponents w 3,(exponents w 1 : ℤ) - 26 * exponents w 3,(exponents w 2 : ℤ) - 11 * exponents w 3]/-- The common base slope `q = 2^31 3^26 5^11 7`. -/def q : ℕ := 2 ^ 31 * 3 ^ 26 * 5 ^ 11 * 7theorem exponents_eq_of_imbalance_eq_zero {w : List ℕ}(hH : imbalance w = 0) :exponents w 0 = 31 * exponents w 3 ∧exponents w 1 = 26 * exponents w 3 ∧exponents w 2 = 11 * exponents w 3 := byhave h0 := congrFun hH (0 : Fin 3)have h1 := congrFun hH (1 : Fin 3)have h2 := congrFun hH (2 : Fin 3)simp [imbalance, Matrix.cons_val_zero, Matrix.cons_val_one] at h0 h1 h2omega/-- Returning to the same imbalance forces slope `q^m`. -/theorem prod_eq_q_pow_of_imbalance_eq_zero (w : List ℕ)(hw : ∀ a ∈ w, a ∈ multipliers) (hH : imbalance w = 0) :w.prod = q ^ exponents w 3 := byobtain ⟨h2, h3, h5⟩ := exponents_eq_of_imbalance_eq_zero hHrw [prod_factorization w hw, h2, h3, h5]simp only [q, mul_pow, pow_mul]/-! ## The finite interval coding -//-- The twenty rational endpoints displayed in Section 2 of the paper. -/def endpoint : Fin 20 → ℚ :=![1 / 9, 10 / 81, 1 / 7, 1 / 5, 2 / 9, 2 / 7, 1 / 3,10 / 27, 2 / 5, 11 / 27, 3 / 7, 37 / 81, 5 / 9, 3 / 5,2 / 3, 19 / 27, 59 / 81, 4 / 5, 23 / 27, 1]def leftIndex (i : Fin 19) : Fin 20 := ⟨i, by omega⟩def rightIndex (i : Fin 19) : Fin 20 := ⟨i + 1, by omega⟩/-- The half-open state interval `K_i`; Lean uses zero-based indices. -/def K (i : Fin 19) : Set ℚ := Set.Ico (endpoint (leftIndex i)) (endpoint (rightIndex i))def width (i : Fin 19) : ℚ := endpoint (rightIndex i) - endpoint (leftIndex i)theorem endpoint_strictMono : StrictMono endpoint := byrw [Fin.strictMono_iff_lt_succ]intro ifin_cases i <;>norm_num [endpoint, Matrix.cons_val_two, Matrix.cons_val_three]theorem width_pos (i : Fin 19) : 0 < width i := byexact sub_pos.mpr (endpoint_strictMono (by simp [leftIndex, rightIndex]))structure Transition wheresource : Fin 19multiplier : ℕfirst : Fin 19last : Fin 19/-- The 23 rows in the interval-transition table. -/def transitions : List Transition :=[⟨0, 14, 12, 15⟩,⟨1, 14, 16, 18⟩, ⟨1, 9, 0, 4⟩,⟨2, 9, 5, 16⟩,⟨3, 9, 17, 18⟩,⟨4, 5, 0, 9⟩,⟨5, 5, 10, 13⟩,⟨6, 5, 14, 17⟩,⟨7, 5, 18, 18⟩, ⟨7, 3, 0, 2⟩,⟨8, 3, 3, 3⟩,⟨9, 3, 4, 4⟩,⟨10, 3, 5, 6⟩,⟨11, 3, 7, 13⟩,⟨12, 3, 14, 16⟩, ⟨12, 2, 0, 2⟩,⟨13, 3, 17, 18⟩, ⟨13, 2, 3, 5⟩,⟨14, 2, 6, 8⟩,⟨15, 2, 9, 10⟩,⟨16, 2, 11, 12⟩,⟨17, 2, 13, 14⟩,⟨18, 2, 15, 18⟩]def T_rat (a : ℕ) (x : ℚ) : ℚ := a * x - 1/-- Endpoint form of an exact interval identity. Positivity of the multiplierthen says that the image of the source interval is exactly the consecutiveblock from `first` through `last`. -/def Transition.Exact (tr : Transition) : Prop :=tr.first ≤ tr.last ∧ 0 < tr.multiplier ∧T_rat tr.multiplier (endpoint (leftIndex tr.source)) =endpoint (leftIndex tr.first) ∧T_rat tr.multiplier (endpoint (rightIndex tr.source)) =endpoint (rightIndex tr.last)/-- Machine-checked certificate for every rational interval identity in thetable. -/theorem transitions_exact : ∀ tr ∈ transitions, tr.Exact := byintro tr htrsimp only [transitions, List.mem_cons, List.not_mem_nil, or_false] at htrrcases htr withrfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl |rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rflall_goalssimp [Transition.Exact, T_rat, endpoint, leftIndex, rightIndex,Matrix.cons_val_two, Matrix.cons_val_three] ; norm_num/-- The four choices `α₀,...,α₃`. -/abbrev Assignment := Fin 4/-- Multiplier prescribed by an assignment at a state. -/def prescribed (α : Assignment) (i : Fin 19) : ℕ :=if i = 0 then 14else if i = 1 then if α = 1 then 9 else 14else if i = 2 ∨ i = 3 then 9else if i = 4 ∨ i = 5 ∨ i = 6 then 5else if i = 7 then if α = 2 then 3 else 5else if i = 8 ∨ i = 9 ∨ i = 10 ∨ i = 11 then 3else if i = 12 ∨ i = 13 then if α = 3 then 2 else 3else 2theorem prescribed_mem_multipliers (α : Assignment) (i : Fin 19) :prescribed α i ∈ multipliers := by decide +reverttheorem prescribed_ge_two (α : Assignment) (i : Fin 19) : 2 ≤ prescribed α i :=mem_multipliers_two_le (prescribed_mem_multipliers α i)/-- A destination state allowed by the selected row of the table. -/def allowedNext (α : Assignment) (i j : Fin 19) : Prop :=transitions.any (fun tr ↦ decide (tr.source = i ∧tr.multiplier = prescribed α i ∧ tr.first ≤ j ∧ j ≤ tr.last)) = trueinstance (α : Assignment) (i j : Fin 19) : Decidable (allowedNext α i j) := byunfold allowedNextinfer_instance/-- Place-dependent transition probability from Section 2. -/def probability (α : Assignment) (i j : Fin 19) : ℚ :=if allowedNext α i j then width j / (prescribed α i * width i) else 0set_option maxHeartbeats 1000000 in-- For some reason, adding 19 rational numbers is nontrivial.private theorem probability_row_sum_zero (i : Fin 19) :∑ j : Fin 19, probability 0 i j = 1 := byfin_cases i <;>simp [probability, allowedNext, transitions, prescribed, Fin.sum_univ_succ] <;>norm_num [width, endpoint, leftIndex, rightIndex,Matrix.cons_val_two, Matrix.cons_val_three]set_option maxHeartbeats 1000000 in-- For some reason, adding 19 rational numbers is nontrivial.private theorem probability_row_sum_one (i : Fin 19) :∑ j : Fin 19, probability 1 i j = 1 := byfin_cases i <;>simp [probability, allowedNext, transitions, prescribed, Fin.sum_univ_succ] <;>norm_num [width, endpoint, leftIndex, rightIndex,Matrix.cons_val_two, Matrix.cons_val_three]set_option maxHeartbeats 1000000 in-- For some reason, adding 19 rational numbers is nontrivial.private theorem probability_row_sum_two (i : Fin 19) :∑ j : Fin 19, probability 2 i j = 1 := byfin_cases i <;>simp [probability, allowedNext, transitions, prescribed, Fin.sum_univ_succ] <;>norm_num [width, endpoint, leftIndex, rightIndex,Matrix.cons_val_two, Matrix.cons_val_three]set_option maxHeartbeats 1000000 in-- For some reason, adding 19 rational numbers is nontrivial.private theorem probability_row_sum_three (i : Fin 19) :∑ j : Fin 19, probability 3 i j = 1 := byfin_cases i <;>simp [probability, allowedNext, transitions, prescribed, Fin.sum_univ_succ] <;>norm_num [width, endpoint, leftIndex, rightIndex,Matrix.cons_val_two, Matrix.cons_val_three]/-- The transition probabilities in every row sum to one. -/theorem probability_row_sum (α : Assignment) (i : Fin 19) :∑ j : Fin 19, probability α i j = 1 := byfin_cases α· exact probability_row_sum_zero i· exact probability_row_sum_one i· exact probability_row_sum_two i· exact probability_row_sum_three i/-! ## Stationary distributions and drift data -//-- Increment of the imbalance vector caused by one multiplier. -/def imbalanceIncrement (a : ℕ) : Fin 3 → ℚ :=![(primeIncrement a 0 : ℚ) - 31 * primeIncrement a 3,(primeIncrement a 1 : ℚ) - 26 * primeIncrement a 3,(primeIncrement a 2 : ℚ) - 11 * primeIncrement a 3]/-- The four stationary mean increments printed in the paper. -/def listedDrift (α : Assignment) : Fin 3 → ℚ :=match α with| 0 => ![82502/209179, 60472/209179, 22315/209179]| 1 => ![4122816/9240032, 3176276/9240032, 1187745/9240032]| 2 => ![-17374/154297, 2952/154297, -12847/154297]| 3 => ![-410774/938829, -384064/938829, -82315/938829]/-- Positive barycentric coordinates witnessing that zero lies strictlyinside the tetrahedron of drift vectors. -/def baryWeight : Fin 4 → ℚ :=![90039171528246091/965033798855459200,48712935304722/153863807215475,114688358060991/465076529568896,118388840756943/344654928162664]theorem baryWeight_pos (α : Assignment) : 0 < baryWeight α := byfin_cases α <;> norm_num [baryWeight]/-- Exact positive barycentric relation among the four drift vectors. -/theorem weighted_drift_sum (k : Fin 3) :∑ α : Assignment, baryWeight α * listedDrift α k = 0 := byfin_cases k <;>norm_num [baryWeight, listedDrift, Fin.sum_univ_succ,Matrix.cons_val_two, Matrix.cons_val_three] <;>simp <;> norm_num/-- Number of generated positive integers at most `x`. -/noncomputable def countUpTo (x : ℕ) : ℕ := byclassicalexact ((Finset.Icc 1 x).filter Generated).card/-- The exact conclusion called "positive lower density" in the paper. -/def HasPositiveLowerDensity : Prop :=∃ c : ℝ, 0 < c ∧ ∀ᶠ x : ℕ in Filter.atTop,c * (x : ℝ) ≤ (countUpTo x : ℝ)/-! ## From common-slope affine words to density -/theorem prod_pos_of_one_le (w : List ℕ) (hw : ∀ a ∈ w, 1 ≤ a) : 0 < w.prod := byinduction w with| nil => simp| cons a w ih =>have ha : 1 ≤ a := hw a (by simp)have htail : ∀ b ∈ w, 1 ≤ b := byintro b hbexact hw b (by simp [hb])simp only [List.prod_cons]exact Nat.mul_pos (by omega) (ih htail)theorem eval_le_prod_mul (w : List ℕ) {x : ℕ} (hw : ∀ a ∈ w, 1 ≤ a) :eval w x ≤ w.prod * x := byinduction w generalizing x with| nil =>simp only [eval, List.prod_nil, one_mul]exact le_rfl| cons a w ih =>have htail : ∀ b ∈ w, 1 ≤ b := byintro b hbexact hw b (by simp [hb])calceval (a :: w) x = eval w (T a x) := rfl_ ≤ w.prod * T a x := ih htail_ ≤ w.prod * (a * x) := Nat.mul_le_mul_left _ (Nat.sub_le _ _)_ = (a :: w).prod * x := by simp [mul_assoc, mul_left_comm]theorem countUpTo_mono : Monotone countUpTo := byintro x y hxyclassicalsimp only [countUpTo]apply Finset.card_le_cardintro n hnsimp only [Finset.mem_filter, Finset.mem_Icc] at hn ⊢exact ⟨⟨hn.1.1, hn.1.2.trans hxy⟩, hn.2⟩theorem card_le_countUpTo {S : Finset ℕ} {x : ℕ}(hS : ∀ n ∈ S, Generated n ∧ n ≤ x) : S.card ≤ countUpTo x := byclassicalsimp only [countUpTo]apply Finset.card_le_cardintro n hnhave h := hS n hnsimp only [Finset.mem_filter, Finset.mem_Icc]exact ⟨⟨Generated.pos h.1, h.2⟩, h.1⟩/-! ## Filling the gaps between geometric scales -//-- A lower bound by a fixed proportion at all sufficiently large geometricscales implies the paper's positive-lower-density conclusion. -/theorem positiveLowerDensity_of_geometric_count {base denominator : ℕ}(hbase : 1 < base) (hdenominator : 0 < denominator)(hgeom : ∃ r₀ : ℕ, ∀ r ≥ r₀,base ^ r ≤ denominator * countUpTo (17 * base ^ r)) :HasPositiveLowerDensity := byobtain ⟨r₀, hgeom⟩ := hgeomlet c : ℝ := 1 / (17 * base * denominator : ℕ)refine ⟨c, ?_, ?_⟩· dsimp [c]positivity· filter_upwards [Filter.eventually_ge_atTop (17 * base ^ r₀)] with x hxlet y := x / 17let r := Nat.log base yhave hyPow : base ^ r₀ ≤ y := bydsimp [y]apply (Nat.le_div_iff_mul_le (by norm_num : 0 < 17)).2simpa [mul_comm] using hxhave hy : y ≠ 0 := byintro hy0rw [hy0] at hyPowexact (Nat.not_succ_le_zero _)((Nat.pow_pos (Nat.zero_lt_one.trans hbase)).trans_le hyPow)have hr : r₀ ≤ r := byexact Nat.le_log_of_pow_le hbase hyPowhave hscale : 17 * base ^ r ≤ x := byhave hp := Nat.pow_log_le_self base hyhave hmul : 17 * base ^ r ≤ 17 * y := Nat.mul_le_mul_left 17 hpexact hmul.trans (Nat.mul_div_le x 17)have hnext : y < base ^ (r + 1) := bysimpa [r, Nat.succ_eq_add_one] using Nat.lt_pow_succ_log_self hbase yhave hxnext : x < 17 * base ^ (r + 1) := bycalcx < 17 * (x / 17 + 1) := Nat.lt_mul_div_succ x (by norm_num)_ = 17 * (y + 1) := by rfl_ ≤ 17 * base ^ (r + 1) := Nat.mul_le_mul_left 17 hnexthave hcountMono : countUpTo (17 * base ^ r) ≤ countUpTo x :=countUpTo_mono hscalehave hgeomr := hgeom r hrhave hpowCount : base ^ r ≤ denominator * countUpTo x :=hgeomr.trans (Nat.mul_le_mul_left denominator hcountMono)have hxCount : x ≤ 17 * base * (denominator * countUpTo x) := bycalcx ≤ 17 * base ^ (r + 1) := hxnext.le_ = 17 * base * base ^ r := by rw [pow_succ]; ring_ ≤ 17 * base * (denominator * countUpTo x) :=Nat.mul_le_mul_left (17 * base) hpowCounthave hxCount' : x ≤ (17 * base * denominator) * countUpTo x := bycalcx ≤ 17 * base * (denominator * countUpTo x) := hxCount_ = (17 * base * denominator) * countUpTo x := by ringhave hxCountReal : (x : ℝ) ≤(17 * base * denominator : ℕ) * (countUpTo x : ℝ) := byexact_mod_cast hxCount'dsimp [c]rw [one_div, inv_mul_eq_div]apply (div_le_iff₀ ?_).2· simpa [mul_assoc, mul_comm, mul_left_comm] using hxCountReal· positivity/-! ## Typed interval paths and the telescoping identity -//-- A path through the interval graph. The endpoints are type indices, socomposability is enforced by Lean. Each step stores the assignment used andproof that its transition is allowed. -/inductive IntervalPath : Fin 19 → Fin 19 → Type where| nil (i : Fin 19) : IntervalPath i i| cons {i j k : Fin 19} (α : Assignment) (h : allowedNext α i j)(tail : IntervalPath j k) : IntervalPath i knamespace IntervalPathdef word {i j : Fin 19} : IntervalPath i j → List ℕ| .nil _ => []| .cons α _ tail => prescribed α i :: tail.worddef mass {i j : Fin 19} : IntervalPath i j → ℚ| .nil _ => 1| @IntervalPath.cons i j _ α _ tail => probability α i j * tail.mass@[simp] theorem word_cons {i j k : Fin 19} (α : Assignment)(h : allowedNext α i j) (tail : IntervalPath j k) :(IntervalPath.cons α h tail).word = prescribed α i :: tail.word := rfl@[simp] theorem mass_cons {i j k : Fin 19} (α : Assignment)(h : allowedNext α i j) (tail : IntervalPath j k) :(IntervalPath.cons α h tail).mass = probability α i j * tail.mass := rfltheorem probability_of_allowed {α : Assignment} {i j : Fin 19}(h : allowedNext α i j) :probability α i j = width j / (prescribed α i * width i) := bysimp [probability, h]theorem word_mem_multipliers {i j : Fin 19} (p : IntervalPath i j) :∀ a ∈ p.word, a ∈ multipliers := byinduction p with| nil =>intro a hasimp only [word, List.not_mem_nil] at ha| @cons i j k α h tail ih =>intro a hasimp only [word_cons, List.mem_cons] at harcases ha with rfl | ha· exact prescribed_mem_multipliers α i· exact ih a hatheorem word_two_le {i j : Fin 19} (p : IntervalPath i j) :∀ a ∈ p.word, 2 ≤ a := byintro a haexact mem_multipliers_two_le (p.word_mem_multipliers a ha)/-- Equation (1): transition probabilities telescope to the ratio of endpointinterval lengths divided by the affine slope. The assignments may vary fromstep to step. -/theorem mass_eq_width_div {i j : Fin 19} (p : IntervalPath i j) :p.mass = width j / ((p.word.prod : ℚ) * width i) := byinduction p with| nil i =>simp only [mass, word, List.prod_nil, Nat.cast_one, one_mul]field_simp [ne_of_gt (width_pos i)]| @cons i j k α h tail ih =>rw [mass_cons, probability_of_allowed h, ih, word_cons, List.prod_cons,Nat.cast_mul]have hi : width i ≠ 0 := ne_of_gt (width_pos i)have hj : width j ≠ 0 := ne_of_gt (width_pos j)have ha : (prescribed α i : ℚ) ≠ 0 := byexact_mod_cast (show prescribed α i ≠ 0 byhave := prescribed_ge_two α iomega)have hp : (tail.word.prod : ℚ) ≠ 0 := byexact_mod_cast (Nat.ne_of_gt (prod_pos_of_one_le tail.word(fun a ha ↦ (tail.word_two_le a ha).trans' (by omega))))field_simpend IntervalPath/-! ## Finite-chain certificates -/theorem probability_nonneg (α : Assignment) (i j : Fin 19) :0 ≤ probability α i j := byby_cases h : allowedNext α i j· rw [IntervalPath.probability_of_allowed h]have ha : (0 : ℚ) ≤ prescribed α i := by positivityexact div_nonneg (width_pos j).le (mul_nonneg ha (width_pos i).le)· simp [probability, h]/-! ## The drift tetrahedron -/def listedDriftReal (α : Assignment) (k : Fin 3) : ℝ := listedDrift α kdef driftDot (v : Fin 3 → ℝ) (α : Assignment) : ℝ :=∑ k : Fin 3, v k * listedDriftReal α ktheorem weighted_drift_real (k : Fin 3) :∑ α : Assignment, (baryWeight α : ℝ) * listedDriftReal α k = 0 := bysimpa [listedDriftReal] usingcongrArg (fun x : ℚ ↦ (x : ℝ)) (weighted_drift_sum k)theorem weighted_driftDot (v : Fin 3 → ℝ) :∑ α : Assignment, (baryWeight α : ℝ) * driftDot v α = 0 := bycalc∑ α : Assignment, (baryWeight α : ℝ) * driftDot v α =∑ k : Fin 3, v k *(∑ α : Assignment, (baryWeight α : ℝ) * listedDriftReal α k) := bysimp only [driftDot, Finset.mul_sum]rw [Finset.sum_comm]apply Finset.sum_congr rflintro k _apply Finset.sum_congr rflintro α _ring_ = 0 := by simp [weighted_drift_real]/-- The first three drift vectors span `ℝ³`. -/theorem eq_zero_of_first_three_driftDot_eq_zero {v : Fin 3 → ℝ}(h0 : driftDot v 0 = 0) (h1 : driftDot v 1 = 0)(h2 : driftDot v 2 = 0) : v = 0 := bynorm_num [driftDot, listedDriftReal, listedDrift, Fin.sum_univ_succ] at h0 h1 h2funext kfin_cases k· change v 0 = 0linear_combination(2317276647762287 / 49432245464000 : ℝ) * h0 -(620783464892 / 15762833375 : ℝ) * h1 -(1460755466599 / 1977289818560 : ℝ) * h2· change v 1 = 0linear_combination-(3381371791278219 / 98864490928000 : ℝ) * h0 +(495150792302 / 15762833375 : ℝ) * h1 +(18486937517763 / 3954579637120 : ℝ) * h2· change v 2 = 0linear_combination-(1761160941937753 / 24716122732000 : ℝ) * h0 +(953310271496 / 15762833375 : ℝ) * h1 -(9824239195199 / 988644909280 : ℝ) * h2/-- Pointwise form of the statement that zero is in the interior of the drifttetrahedron: every nonzero direction has strictly negative dot product with atleast one drift. -/theorem exists_driftDot_neg {v : Fin 3 → ℝ} (hv : v ≠ 0) :∃ α : Assignment, driftDot v α < 0 := byby_contra hsimp only [not_exists, not_lt] at hhave hterm : ∀ α ∈ (Finset.univ : Finset Assignment),0 ≤ (baryWeight α : ℝ) * driftDot v α := byintro α _exact mul_nonneg (by exact_mod_cast (baryWeight_pos α).le) (h α)have hzero := (Finset.sum_eq_zero_iff_of_nonneg hterm).1 (weighted_driftDot v)have hdot (α : Assignment) : driftDot v α = 0 := byhave hz := hzero α (Finset.mem_univ α)exact (mul_eq_zero.mp hz).resolve_left (byexact_mod_cast (baryWeight_pos α).ne')exact hv (eq_zero_of_first_three_driftDot_eq_zero (hdot 0) (hdot 1) (hdot 2))/-- Minimum of the four drift dot products. -/def minDriftDot (v : Fin 3 → ℝ) : ℝ :=min (driftDot v 0) (min (driftDot v 1) (min (driftDot v 2) (driftDot v 3)))theorem minDriftDot_le (v : Fin 3 → ℝ) (α : Assignment) :minDriftDot v ≤ driftDot v α := byfin_cases α <;> simp [minDriftDot]theorem minDriftDot_neg {v : Fin 3 → ℝ} (hv : v ≠ 0) : minDriftDot v < 0 := byobtain ⟨α, hα⟩ := exists_driftDot_neg hvexact (minDriftDot_le v α).trans_lt hαtheorem continuous_driftDot (α : Assignment) :Continuous (fun v : Fin 3 → ℝ ↦ driftDot v α) := byapply continuous_finsetSumintro k _exact (continuous_apply k).mul continuous_consttheorem continuous_minDriftDot : Continuous minDriftDot := byexact (continuous_driftDot 0).min ((continuous_driftDot 1).min((continuous_driftDot 2).min (continuous_driftDot 3)))/-- Quantitative drift consequence used in Lemma 2. Compactness of the unitsphere upgrades pointwise negativity to a uniform negative constant. -/theorem exists_uniform_negative_drift :∃ δ : ℝ, 0 < δ ∧ ∀ v : Fin 3 → ℝ, ‖v‖ = 1 → minDriftDot v ≤ -δ := bylet e : Fin 3 → ℝ := Pi.single 0 1have heNorm : ‖e‖ = 1 := by simp [e, Pi.norm_single]have hSphere : (Metric.sphere (0 : Fin 3 → ℝ) 1).Nonempty := byrefine ⟨e, ?_⟩simpa [Metric.mem_sphere, dist_zero_left] using heNormobtain ⟨v, hvSphere, hvMax⟩ :=(isCompact_sphere (0 : Fin 3 → ℝ) 1).exists_isMaxOn hSpherecontinuous_minDriftDot.continuousOnhave hvNorm : ‖v‖ = 1 := bysimpa [Metric.mem_sphere, dist_zero_left] using hvSpherehave hvNe : v ≠ 0 := byintro hvrw [hv, norm_zero] at hvNormnorm_num at hvNormhave hvNeg : minDriftDot v < 0 := minDriftDot_neg hvNerefine ⟨-minDriftDot v, neg_pos.mpr hvNeg, ?_⟩intro u huNormhave huSphere : u ∈ Metric.sphere (0 : Fin 3 → ℝ) 1 := bysimpa [Metric.mem_sphere, dist_zero_left] using huNormsimpa using hvMax huSphere/-! ## Exact finite Poisson equationsThe paper uses the Poisson equation for each of its four finite Markovchains. The following rational vectors are exact certificates for thoseequations, normalized to vanish at state 18.-//-- A rational solution of the vector-valued Poisson equation for eachstationary assignment. -/def poissonSolution (α : Assignment) : Fin 19 → Fin 3 → ℚ :=match α with| 0 =>![![-92474603/2928506, -754100497/29285060, -157916873/14642530],![-141489037/4601938, -1193514713/46019380, -1016602093/92038760],![-1344010/627537, 18690823/7530444, 11052155/30121776],![-134965/418358, 366968/209179, -223305/3346864],![-5089025/836716, -4703447/1673432, 1128693/3346864],![-847919/418358, 413833/418358, 3811471/3346864],![-598777/418358, 164803/1045895, 20651439/16734320],![-82502/209179, -60472/209179, 186864/209179],![-299969/418358, 515675/209179, -580345/3346864],![-5419033/836716, -3513791/1673432, 771653/3346864],![-7231387/3346864, 44722847/33468640, 71878267/66937280],![-1862623/836716, 3390443/2091790, 6428039/33468640],![-812525/418358, 529703/418358, 1026335/3346864],![-134965/418358, 157789/209179, -223305/3346864],![-142033/418358, -16613/209179, 2845999/3346864],![-1424395/418358, -1779727/4183580, 5068333/8367160],![-18591689/12132382, 14821723/12132382, 11738933/97059056],![811053/2928506, 245214/1464253, 515835/3346864],![0, 0, 0]]| 1 =>![![-18218137/577502, -59408041/2310008, -99531215/9240032],![-244093727/44467654, 128903569/177870616, -271195465/711482464],![-37590230/18191313, 745570207/291061008, 464215267/1164244032],![-236545/577502, 1920191/1155004, -238977/2310008],![-32776595/8085028, -43322975/64680224, 308810745/258720896],![-8281797/4042514, 7810709/8085028, 4566315/4042514],![-817701/577502, 401365/2310008, 11462711/9240032],![-128838/288751, -794069/2310008, 8052287/9240032],![-494221/577502, 5356321/2310008, -2143653/9240032],![-36384059/8085028, -876683/64680224, 275553885/258720896],![-71731617/32340112, 330044815/258720896, 1086194759/1034883584],![-2450175/1155004, 31996559/18480064, 17466859/73920256],![-1067185/577502, 6309223/4620016, 6402399/18480064],![-236545/577502, 765187/1155004, -238977/2310008],![-241517/577502, -187691/1155004, 471885/577502],![-1525927/577502, 1752829/4620016, 17143453/18480064],![-24831461/16747558, 21338725/16747558, 19016669/133980464],![570537/4042514, 389745/16170056, 6259599/64680224],![0, 0, 0]]| 2 =>![![-70369625/2160158, -113547139/4320316, -6887181/617188],![-104473735/3394534, -176188709/6789068, -75067917/6789068],![-1645928/462891, 1333993/771485, -753691/4628910],![-45427/308594, 5701947/3085940, -3119/3085940],![-4738835/617188, -4518359/1234376, -321747/1234376],![-768017/308594, 458693/617188, 596047/617188],![-863695/308594, -352161/617188, 445457/617188],![-2069882/154297, -5571617/771485, -12296049/3085940],![-10679/308594, 8728847/3085940, 253821/3085940],![-4669339/617188, -3307599/1234376, -218971/1234376],![-6201025/2468752, 5674595/4937504, 4652415/4937504],![-2382673/617188, 926495/1234376, -518733/1234376],![-1092827/308594, 1279677/3085940, -902409/3085940],![-45427/308594, 2616007/3085940, -3119/3085940],![-1745479/308594, -1796229/617188, -704491/617188],![-1100161/308594, -315251/617188, 336781/617188],![-23705255/8949226, 56129683/89492260, -26607071/89492260],![-2164725/2160158, -1584873/3085940, -7021593/21601580],![0, 0, 0]]| 3 =>![![-1115788487/32859015, -182354567/6571803, -372698614/32859015],![-6383806027/206542380, -2153946893/82616952, -4578452623/413084760],![-228316579/67595688, 190330739/135191376, 390245/135191376],![1045843/2503544, 12388597/5007088, 1537835/5007088],![-14159963/2503544, -12265589/5007088, 2221421/5007088],![-34533503/7510632, -17583545/15021264, 8022361/15021264],![-63544399/37553160, -888629/15021264, 88445129/75106320],![410774/938829, 384064/938829, 1021144/938829],![6423721/7510632, 58332079/15021264, 5930545/15021264],![-39193697/7510632, -15630479/15021264, 7981303/15021264],![-433980767/150212640, 43547075/60085056, 271371337/300425280],![-294149719/75106320, 6110059/30042528, -30132271/150212640],![-30532829/2503544, -41208115/5007088, -19551805/5007088],![-21242167/7510632, -11462785/15021264, 8124545/15021264],![6399233/7510632, 13991927/15021264, 17289929/15021264],![-15333271/6258860, 944543/2503544, 10409221/12517720],![-366525975/72602776, -291104273/145205552, -183323039/145205552],![-1337967/17524808, 8804151/35049616, 29668185/35049616],![0, 0, 0]]/-- The transition operator applied to one coordinate of a function on thenineteen interval states. -/def expectedNext (α : Assignment) (g : Fin 19 → ℚ) (i : Fin 19) : ℚ :=∑ j : Fin 19, probability α i j * g jset_option maxHeartbeats 2000000 in-- This expands 57 entries of the first exact rational Poisson certificate.private theorem poisson_equation_zero (i : Fin 19) (k : Fin 3) :poissonSolution 0 i k - expectedNext 0 (fun j ↦ poissonSolution 0 j k) i =imbalanceIncrement (prescribed 0 i) k - listedDrift 0 k := byfin_cases i <;> fin_cases k <;>simp [poissonSolution, expectedNext, probability, allowedNext, transitions,prescribed, width, endpoint, leftIndex, rightIndex, imbalanceIncrement,primeIncrement, listedDrift, Fin.sum_univ_succ, Matrix.cons_val_two,Matrix.cons_val_three] <;>norm_numset_option maxHeartbeats 2000000 in-- This expands 57 entries of the second exact rational Poisson certificate.private theorem poisson_equation_one (i : Fin 19) (k : Fin 3) :poissonSolution 1 i k - expectedNext 1 (fun j ↦ poissonSolution 1 j k) i =imbalanceIncrement (prescribed 1 i) k - listedDrift 1 k := byfin_cases i <;> fin_cases k <;>simp [poissonSolution, expectedNext, probability, allowedNext, transitions,prescribed, width, endpoint, leftIndex, rightIndex, imbalanceIncrement,primeIncrement, listedDrift, Fin.sum_univ_succ, Matrix.cons_val_two,Matrix.cons_val_three] <;>norm_numset_option maxHeartbeats 2000000 in-- This expands 57 entries of the third exact rational Poisson certificate.private theorem poisson_equation_two (i : Fin 19) (k : Fin 3) :poissonSolution 2 i k - expectedNext 2 (fun j ↦ poissonSolution 2 j k) i =imbalanceIncrement (prescribed 2 i) k - listedDrift 2 k := byfin_cases i <;> fin_cases k <;>simp [poissonSolution, expectedNext, probability, allowedNext, transitions,prescribed, width, endpoint, leftIndex, rightIndex, imbalanceIncrement,primeIncrement, listedDrift, Fin.sum_univ_succ, Matrix.cons_val_two,Matrix.cons_val_three] <;>norm_numset_option maxHeartbeats 2000000 in-- This expands 57 entries of the fourth exact rational Poisson certificate.private theorem poisson_equation_three (i : Fin 19) (k : Fin 3) :poissonSolution 3 i k - expectedNext 3 (fun j ↦ poissonSolution 3 j k) i =imbalanceIncrement (prescribed 3 i) k - listedDrift 3 k := byfin_cases i <;> fin_cases k <;>simp [poissonSolution, expectedNext, probability, allowedNext, transitions,prescribed, width, endpoint, leftIndex, rightIndex, imbalanceIncrement,primeIncrement, listedDrift, Fin.sum_univ_succ, Matrix.cons_val_two,Matrix.cons_val_three] <;>norm_num/-- Exact verification of `g - P g = h - β` for all four assignments,nineteen states, and three imbalance coordinates. -/theorem poisson_equation (α : Assignment) (i : Fin 19) (k : Fin 3) :poissonSolution α i k - expectedNext α (fun j ↦ poissonSolution α j k) i =imbalanceIncrement (prescribed α i) k - listedDrift α k := byfin_cases α· exact poisson_equation_zero i k· exact poisson_equation_one i k· exact poisson_equation_two i k· exact poisson_equation_three i k/-! ## Finite blocks with a fixed assignment -//-- Iteration of the transition operator belonging to one assignment. -/def iterateExpected (α : Assignment) : ℕ → (Fin 19 → ℚ) → Fin 19 → ℚ| 0, f => f| n + 1, f => fun i ↦ expectedNext α (iterateExpected α n f) i/-- Expected accumulated imbalance in one coordinate during `n` steps withthe assignment held fixed. -/def expectedAccum (α : Assignment) : ℕ → Fin 19 → Fin 3 → ℚ| 0, _, _ => 0| n + 1, i, k =>imbalanceIncrement (prescribed α i) k +expectedNext α (fun j ↦ expectedAccum α n j k) i@[simp] theorem iterateExpected_succ (α : Assignment) (n : ℕ)(f : Fin 19 → ℚ) (i : Fin 19) :iterateExpected α (n + 1) f i =expectedNext α (iterateExpected α n f) i := rfl@[simp] theorem expectedAccum_succ (α : Assignment) (n : ℕ)(i : Fin 19) (k : Fin 3) :expectedAccum α (n + 1) i k =imbalanceIncrement (prescribed α i) k +expectedNext α (fun j ↦ expectedAccum α n j k) i := rfltheorem expectedNext_const (α : Assignment) (c : ℚ) (i : Fin 19) :expectedNext α (fun _ ↦ c) i = c := bysimp only [expectedNext, ← Finset.sum_mul, probability_row_sum, one_mul]theorem expectedNext_add (α : Assignment) (f g : Fin 19 → ℚ) (i : Fin 19) :expectedNext α (fun j ↦ f j + g j) i =expectedNext α f i + expectedNext α g i := bysimp only [expectedNext, mul_add, Finset.sum_add_distrib]theorem expectedNext_sub (α : Assignment) (f g : Fin 19 → ℚ) (i : Fin 19) :expectedNext α (fun j ↦ f j - g j) i =expectedNext α f i - expectedNext α g i := bysimp only [expectedNext, mul_sub, Finset.sum_sub_distrib]/-- The finite Poisson certificate telescopes over an arbitrary block. -/theorem expectedAccum_eq_poisson (α : Assignment) (n : ℕ)(i : Fin 19) (k : Fin 3) :expectedAccum α n i k =(n : ℚ) * listedDrift α k + poissonSolution α i k -iterateExpected α n (fun j ↦ poissonSolution α j k) i := byinduction n generalizing i with| zero => simp [expectedAccum, iterateExpected]| succ n ih =>rw [expectedAccum_succ]simp_rw [ih]rw [expectedNext_sub, expectedNext_add]simp only [expectedNext_const]rw [iterateExpected_succ]have hp := poisson_equation α i kdsimp [expectedNext] at hp ⊢norm_num only [Nat.cast_add, Nat.cast_one]linear_combination -hpset_option maxHeartbeats 800000 in-- This checks all 228 entries of the four exact rational Poisson tables./-- Every entry of every exact Poisson solution is bounded by 40. -/theorem poissonSolution_abs_le (α : Assignment) (i : Fin 19) (k : Fin 3) :|poissonSolution α i k| ≤ 40 := byfin_cases α <;> fin_cases i <;> fin_cases k <;>simp [poissonSolution, Matrix.cons_val_two, Matrix.cons_val_three] <;>norm_numtheorem expectedNext_mono (α : Assignment) {f g : Fin 19 → ℚ}(hfg : ∀ j, f j ≤ g j) (i : Fin 19) :expectedNext α f i ≤ expectedNext α g i := byapply Finset.sum_le_sumintro j _exact mul_le_mul_of_nonneg_left (hfg j) (probability_nonneg α i j)theorem iterateExpected_bounds (α : Assignment) (n : ℕ){f : Fin 19 → ℚ} {B : ℚ} (hf : ∀ i, -B ≤ f i ∧ f i ≤ B)(i : Fin 19) :-B ≤ iterateExpected α n f i ∧ iterateExpected α n f i ≤ B := byinduction n generalizing i with| zero => exact hf i| succ n ih =>rw [iterateExpected_succ]constructor· rw [← expectedNext_const α (-B) i]exact expectedNext_mono α (fun j ↦ (ih j).1) i· rw [← expectedNext_const α B i]exact expectedNext_mono α (fun j ↦ (ih j).2) i/-- The expected block increment differs from `n` times the stationary driftby at most 80 in every coordinate, uniformly in the initial interval. -/theorem expectedAccum_error_abs_le (α : Assignment) (n : ℕ)(i : Fin 19) (k : Fin 3) :|expectedAccum α n i k - (n : ℚ) * listedDrift α k| ≤ 80 := byrw [expectedAccum_eq_poisson]ring_nfhave hi := poissonSolution_abs_le α i khave hi' := abs_le.mp hihave hj := iterateExpected_bounds α n(B := (40 : ℚ)) (f := fun j ↦ poissonSolution α j k)(fun j ↦ (abs_le.mp (poissonSolution_abs_le α j k))) irw [abs_le]constructor <;> linarith/-! ### Enumerating finite blocks -//-- The integer imbalance increment of one multiplier. -/def imbalanceIncrementInt (a : ℕ) : Fin 3 → ℤ :=![(primeIncrement a 0 : ℤ) - 31 * primeIncrement a 3,(primeIncrement a 1 : ℤ) - 26 * primeIncrement a 3,(primeIncrement a 2 : ℤ) - 11 * primeIncrement a 3]theorem imbalanceIncrementInt_cast (a : ℕ) (k : Fin 3) :(imbalanceIncrementInt a k : ℚ) = imbalanceIncrement a k := byfin_cases k <;> simp [imbalanceIncrementInt, imbalanceIncrement]/-- Endpoint of a tuple of successive interval choices. -/def blockEndpoint : (n : ℕ) → Fin 19 → (Fin n → Fin 19) → Fin 19| 0, i, _ => i| n + 1, _, w => blockEndpoint n (w 0) (Fin.tail w)/-- Probability of a tuple of successive interval choices under a fixedassignment. Tuples containing a forbidden transition have weight zero. -/def blockWeight (α : Assignment) :(n : ℕ) → Fin 19 → (Fin n → Fin 19) → ℚ| 0, _, _ => 1| n + 1, i, w =>probability α i (w 0) * blockWeight α n (w 0) (Fin.tail w)/-- Net integer imbalance accumulated along a finite block. -/def blockIncrement (α : Assignment) :(n : ℕ) → Fin 19 → (Fin n → Fin 19) → Fin 3 → ℤ| 0, _, _, _ => 0| n + 1, i, w, k =>imbalanceIncrementInt (prescribed α i) k +blockIncrement α n (w 0) (Fin.tail w) k/-- Multiplier word read along a finite block. -/def blockWord (α : Assignment) :(n : ℕ) → Fin 19 → (Fin n → Fin 19) → List ℕ| 0, _, _ => []| n + 1, i, w =>prescribed α i :: blockWord α n (w 0) (Fin.tail w)@[simp] theorem blockWeight_cons (α : Assignment) (n : ℕ) (i j : Fin 19)(w : Fin n → Fin 19) :blockWeight α (n + 1) i (Fin.cons j w) =probability α i j * blockWeight α n j w := bysimp [blockWeight]@[simp] theorem blockIncrement_cons (α : Assignment) (n : ℕ) (i j : Fin 19)(w : Fin n → Fin 19) (k : Fin 3) :blockIncrement α (n + 1) i (Fin.cons j w) k =imbalanceIncrementInt (prescribed α i) k + blockIncrement α n j w k := bysimp [blockIncrement]@[simp] theorem blockWord_cons (α : Assignment) (n : ℕ) (i j : Fin 19)(w : Fin n → Fin 19) :blockWord α (n + 1) i (Fin.cons j w) =prescribed α i :: blockWord α n j w := bysimp [blockWord]theorem sum_tuple_succ {A R : Type*} [Fintype A][AddCommMonoid R] (n : ℕ) (F : (Fin (n + 1) → A) → R) :∑ w, F w = ∑ a : A, ∑ u : Fin n → A, F (Fin.cons a u) := bylet e := Fin.consEquiv (fun _ : Fin (n + 1) ↦ A)calc∑ w, F w = ∑ z : A × (Fin n → A), F (e z) := (Equiv.sum_comp e F).symm_ = ∑ a : A, ∑ u : Fin n → A, F (Fin.cons a u) := byrw [Fintype.sum_prod_type]rfl/-- Block-path probabilities sum to one. -/theorem blockWeight_sum (α : Assignment) (n : ℕ) (i : Fin 19) :∑ w : Fin n → Fin 19, blockWeight α n i w = 1 := byinduction n generalizing i with| zero => simp [blockWeight]| succ n ih =>rw [sum_tuple_succ]simp_rw [blockWeight_cons, ← Finset.mul_sum, ih, mul_one]exact probability_row_sum α itheorem blockWeight_nonneg (α : Assignment) (n : ℕ) (i : Fin 19)(w : Fin n → Fin 19) : 0 ≤ blockWeight α n i w := byinduction n generalizing i with| zero => simp [blockWeight]| succ n ih =>rw [show w = Fin.cons (w 0) (Fin.tail w) from (Fin.cons_self_tail w).symm,blockWeight_cons]exact mul_nonneg (probability_nonneg α i (w 0)) (ih (w 0) (Fin.tail w))/-- The path enumeration realizes `expectedAccum`. -/theorem sum_blockWeight_mul_increment (α : Assignment) (n : ℕ)(i : Fin 19) (k : Fin 3) :∑ w : Fin n → Fin 19,blockWeight α n i w * (blockIncrement α n i w k : ℚ) =expectedAccum α n i k := byinduction n generalizing i with| zero => simp [blockWeight, blockIncrement, expectedAccum]| succ n ih =>rw [sum_tuple_succ, expectedAccum_succ]simp_rw [blockWeight_cons, blockIncrement_cons, Int.cast_add,imbalanceIncrementInt_cast, mul_add, Finset.sum_add_distrib]have hfirst :(∑ j : Fin 19, ∑ w : Fin n → Fin 19,probability α i j * blockWeight α n j w *imbalanceIncrement (prescribed α i) k) =imbalanceIncrement (prescribed α i) k := bycalc_ = ∑ j : Fin 19, probability α i j *(∑ w : Fin n → Fin 19, blockWeight α n j w *imbalanceIncrement (prescribed α i) k) := byapply Finset.sum_congr rflintro j _rw [Finset.mul_sum]simp only [mul_assoc]_ = ∑ j : Fin 19, probability α i j *((∑ w : Fin n → Fin 19, blockWeight α n j w) *imbalanceIncrement (prescribed α i) k) := bysimp_rw [Finset.sum_mul]_ = ∑ j : Fin 19, probability α i j *imbalanceIncrement (prescribed α i) k := bysimp_rw [blockWeight_sum, one_mul]_ = imbalanceIncrement (prescribed α i) k := byrw [← Finset.sum_mul, probability_row_sum, one_mul]have hsecond :(∑ j : Fin 19, ∑ w : Fin n → Fin 19,probability α i j * blockWeight α n j w *(blockIncrement α n j w k : ℚ)) =∑ j : Fin 19, probability α i j * expectedAccum α n j k := byapply Finset.sum_congr rflintro j _calc_ = probability α i j *(∑ w : Fin n → Fin 19,blockWeight α n j w * (blockIncrement α n j w k : ℚ)) := byrw [Finset.mul_sum]simp only [mul_assoc]_ = probability α i j * expectedAccum α n j k := by rw [ih]rw [hfirst, hsecond]rfltheorem blockIncrement_eq_imbalance (α : Assignment) (n : ℕ)(i : Fin 19) (w : Fin n → Fin 19) :blockIncrement α n i w = imbalance (blockWord α n i w) := byinduction n generalizing i with| zero =>funext kfin_cases k <;> simp [blockIncrement, blockWord, imbalance, exponents]| succ n ih =>rw [show w = Fin.cons (w 0) (Fin.tail w) from (Fin.cons_self_tail w).symm]funext krw [blockIncrement_cons, blockWord_cons, ih]fin_cases k <;>simp [imbalanceIncrementInt, imbalance, exponents_cons] <;> ring/-! ## Deterministic drift feedback -//-- The integer imbalance vector viewed in `ℝ³`. -/def imbalanceReal (h : Fin 3 → ℤ) : Fin 3 → ℝ := fun k ↦ h k/-- Squared Euclidean length, used as the Lyapunov function. -/def squareLength (h : Fin 3 → ℤ) : ℝ :=∑ k : Fin 3, (h k : ℝ) ^ 2/-- A deterministic minimizer among the four drift directions. -/noncomputable def minAssignment (v : Fin 3 → ℝ) : Assignment :=Classical.choose (Finset.exists_min_image Finset.univ (driftDot v)Finset.univ_nonempty)theorem minAssignment_le (v : Fin 3 → ℝ) (α : Assignment) :driftDot v (minAssignment v) ≤ driftDot v α := byexact (Classical.choose_spec (Finset.exists_min_image Finset.univ (driftDot v)Finset.univ_nonempty)).2 α (Finset.mem_univ α)/-- The feedback assignment used for the next block. -/noncomputable def feedback (h : Fin 3 → ℤ) : Assignment :=minAssignment (imbalanceReal h)/-- A fixed positive uniform drift constant supplied by compactness of theunit sphere. -/noncomputable def driftDelta : ℝ := Classical.choose exists_uniform_negative_drifttheorem driftDelta_pos : 0 < driftDelta :=(Classical.choose_spec exists_uniform_negative_drift).1theorem minDriftDot_le_neg_delta {v : Fin 3 → ℝ} (hv : ‖v‖ = 1) :minDriftDot v ≤ -driftDelta :=(Classical.choose_spec exists_uniform_negative_drift).2 v hvtheorem exists_assignment_le_minDriftDot (v : Fin 3 → ℝ) :∃ α : Assignment, driftDot v α ≤ minDriftDot v := bylet α := minAssignment vrefine ⟨α, ?_⟩apply le_min· exact minAssignment_le v 0· apply le_min· exact minAssignment_le v 1· exact le_min (minAssignment_le v 2) (minAssignment_le v 3)theorem driftDot_smul (c : ℝ) (v : Fin 3 → ℝ) (α : Assignment) :driftDot (c • v) α = c * driftDot v α := byrw [driftDot, driftDot, Finset.mul_sum]apply Finset.sum_congr rflintro k _simp only [Pi.smul_apply, smul_eq_mul]ringtheorem norm_imbalanceReal_pos {h : Fin 3 → ℤ} (hh : h ≠ 0) :0 < ‖imbalanceReal h‖ := byrw [norm_pos_iff]intro hzapply hhfunext khave hk := congrFun hz khave hk' : (h k : ℝ) = 0 := by simpa [imbalanceReal] using hkexact_mod_cast hk'/-- The chosen stationary drift has a uniformly negative radial component. -/theorem feedback_drift_le (h : Fin 3 → ℤ) :driftDot (imbalanceReal h) (feedback h) ≤-driftDelta * ‖imbalanceReal h‖ := byby_cases hh : h = 0· subst hhave hz : imbalanceReal (0 : Fin 3 → ℤ) = 0 := byfunext ksimp [imbalanceReal]rw [hz, norm_zero]simp [driftDot]· let r := ‖imbalanceReal h‖have hr : 0 < r := norm_imbalanceReal_pos hhlet v : Fin 3 → ℝ := r⁻¹ • imbalanceReal hhave hv : ‖v‖ = 1 := byrw [show v = r⁻¹ • imbalanceReal h from rfl, norm_smul]simp only [Real.norm_eq_abs, abs_inv, abs_of_pos hr]exact inv_mul_cancel₀ hr.ne'obtain ⟨α, hα⟩ := exists_assignment_le_minDriftDot vhave hαδ : driftDot v α ≤ -driftDelta :=hα.trans (minDriftDot_le_neg_delta hv)have hscale : imbalanceReal h = r • v := bysimp only [v, smul_smul]rw [mul_inv_cancel₀ hr.ne']simpcalcdriftDot (imbalanceReal h) (feedback h) ≤driftDot (imbalanceReal h) α := minAssignment_le _ α_ = r * driftDot v α := by rw [hscale, driftDot_smul]_ ≤ r * (-driftDelta) := mul_le_mul_of_nonneg_left hαδ hr.le_ = -driftDelta * ‖imbalanceReal h‖ := by rw [show r = ‖imbalanceReal h‖ from rfl]; ring/-! ### Uniform finite-block estimates -/theorem prescribed_increment_abs_le (α : Assignment) (i : Fin 19) (k : Fin 3) :|imbalanceIncrementInt (prescribed α i) k| ≤ 30 := byhave ha := prescribed_mem_multipliers α isimp only [multipliers, Finset.mem_insert, Finset.mem_singleton] at harcases ha with ha | ha | ha | ha | ha <;> rw [ha] <;>fin_cases k <;>simp [imbalanceIncrementInt, primeIncrement, Matrix.cons_val_two,Matrix.cons_val_three]theorem blockIncrement_abs_le (α : Assignment) (n : ℕ) (i : Fin 19)(w : Fin n → Fin 19) (k : Fin 3) :|blockIncrement α n i w k| ≤ 30 * n := byinduction n generalizing i with| zero => simp [blockIncrement]| succ n ih =>rw [show w = Fin.cons (w 0) (Fin.tail w) from (Fin.cons_self_tail w).symm,blockIncrement_cons]calc|imbalanceIncrementInt (prescribed α i) k +blockIncrement α n (w 0) (Fin.tail w) k| ≤|imbalanceIncrementInt (prescribed α i) k| +|blockIncrement α n (w 0) (Fin.tail w) k| := abs_add_le _ __ ≤ 30 + 30 * n := add_le_add (prescribed_increment_abs_le α i k)(ih (w 0) (Fin.tail w))_ = 30 * (n + 1) := by ringtheorem blockIncrement_square_sum_le (α : Assignment) (n : ℕ) (i : Fin 19)(w : Fin n → Fin 19) :(∑ k : Fin 3, (blockIncrement α n i w k : ℝ) ^ 2) ≤ 2700 * n ^ 2 := byhave hbound (k : Fin 3) :|(blockIncrement α n i w k : ℝ)| ≤ 30 * n := byexact_mod_cast blockIncrement_abs_le α n i w khave hsquare (k : Fin 3) :(blockIncrement α n i w k : ℝ) ^ 2 ≤ (30 * (n : ℝ)) ^ 2 := byrw [sq_le_sq]simpa [abs_of_nonneg (by positivity : (0 : ℝ) ≤ 30 * n)] using hbound kcalc_ ≤ ∑ _k : Fin 3, (30 * (n : ℝ)) ^ 2 :=Finset.sum_le_sum fun k _ ↦ hsquare k_ = 2700 * n ^ 2 := by norm_num [Fin.sum_univ_succ]; ringtheorem sum_blockWeight_real (α : Assignment) (n : ℕ) (i : Fin 19) :∑ w : Fin n → Fin 19, (blockWeight α n i w : ℝ) = 1 := byexact_mod_cast blockWeight_sum α n itheorem expected_block_square_le (α : Assignment) (n : ℕ) (i : Fin 19) :(∑ w : Fin n → Fin 19, (blockWeight α n i w : ℝ) *∑ k : Fin 3, (blockIncrement α n i w k : ℝ) ^ 2) ≤(2700 : ℝ) * (n : ℝ) ^ 2 := bycalc_ ≤ ∑ w : Fin n → Fin 19, (blockWeight α n i w : ℝ) *((2700 : ℝ) * (n : ℝ) ^ 2) := byapply Finset.sum_le_sumintro w _apply mul_le_mul_of_nonneg_left (blockIncrement_square_sum_le α n i w)exact_mod_cast blockWeight_nonneg α n i w_ = (2700 : ℝ) * (n : ℝ) ^ 2 := byrw [← Finset.sum_mul, sum_blockWeight_real, one_mul]theorem sum_blockWeight_mul_increment_real (α : Assignment) (n : ℕ)(i : Fin 19) (k : Fin 3) :∑ w : Fin n → Fin 19, (blockWeight α n i w : ℝ) *(blockIncrement α n i w k : ℝ) = expectedAccum α n i k := byexact_mod_cast sum_blockWeight_mul_increment α n i ktheorem expectedAccum_error_abs_le_real (α : Assignment) (n : ℕ)(i : Fin 19) (k : Fin 3) :|(expectedAccum α n i k : ℝ) - n * listedDriftReal α k| ≤ 80 := bysimp only [listedDriftReal]exact_mod_cast expectedAccum_error_abs_le α n i k/-- Expected Lyapunov increment over a block, expressed as a finite path sum. -/def expectedSquareIncrement (α : Assignment) (n : ℕ)(i : Fin 19) (h : Fin 3 → ℤ) : ℝ :=∑ w : Fin n → Fin 19, (blockWeight α n i w : ℝ) *(squareLength (fun k ↦ h k + blockIncrement α n i w k) - squareLength h)theorem expectedSquareIncrement_le (α : Assignment) (n : ℕ)(i : Fin 19) (h : Fin 3 → ℤ) :expectedSquareIncrement α n i h ≤2 * n * driftDot (imbalanceReal h) α +480 * ‖imbalanceReal h‖ + 2700 * n ^ 2 := byhave hcoord (k : Fin 3) : |(h k : ℝ)| ≤ ‖imbalanceReal h‖ := byhave hk : ‖imbalanceReal h k‖₊ ≤Finset.univ.sup (fun j : Fin 3 ↦ ‖imbalanceReal h j‖₊) :=Finset.le_sup (s := Finset.univ)(f := fun j : Fin 3 ↦ ‖imbalanceReal h j‖₊) (Finset.mem_univ k)change ‖imbalanceReal h k‖ ≤ ‖imbalanceReal h‖change ‖imbalanceReal h k‖ ≤(↑(Finset.univ.sup (fun j : Fin 3 ↦ ‖imbalanceReal h j‖₊)) : ℝ)exact_mod_cast hkhave herr (k : Fin 3) := expectedAccum_error_abs_le_real α n i khave hdotError :(∑ k : Fin 3, (h k : ℝ) * (expectedAccum α n i k : ℝ)) ≤n * driftDot (imbalanceReal h) α + 240 * ‖imbalanceReal h‖ := byhave hterm (k : Fin 3) :(h k : ℝ) * ((expectedAccum α n i k : ℝ) - n * listedDriftReal α k) ≤80 * ‖imbalanceReal h‖ := bycalc_ ≤ |(h k : ℝ) * ((expectedAccum α n i k : ℝ) -n * listedDriftReal α k)| := le_abs_self __ = |(h k : ℝ)| * |(expectedAccum α n i k : ℝ) -n * listedDriftReal α k| := abs_mul _ __ ≤ ‖imbalanceReal h‖ * 80 :=mul_le_mul (hcoord k) (herr k) (abs_nonneg _) (norm_nonneg _)_ = 80 * ‖imbalanceReal h‖ := by ringcalc_ = n * driftDot (imbalanceReal h) α +∑ k : Fin 3, (h k : ℝ) * ((expectedAccum α n i k : ℝ) -n * listedDriftReal α k) := bysimp only [driftDot, imbalanceReal, Fin.sum_univ_succ]ring_ ≤ n * driftDot (imbalanceReal h) α +∑ _k : Fin 3, 80 * ‖imbalanceReal h‖ := byexact add_le_add_right(Finset.sum_le_sum fun k _ ↦ hterm k)(n * driftDot (imbalanceReal h) α)_ = n * driftDot (imbalanceReal h) α + 240 * ‖imbalanceReal h‖ := bynorm_num [Fin.sum_univ_succ]ringhave hexpand : expectedSquareIncrement α n i h =2 * (∑ k : Fin 3, (h k : ℝ) * (expectedAccum α n i k : ℝ)) +∑ w : Fin n → Fin 19, (blockWeight α n i w : ℝ) *∑ k : Fin 3, (blockIncrement α n i w k : ℝ) ^ 2 := byhave hpoint (w : Fin n → Fin 19) :squareLength (fun k ↦ h k + blockIncrement α n i w k) - squareLength h =2 * (∑ k : Fin 3, (h k : ℝ) * (blockIncrement α n i w k : ℝ)) +∑ k : Fin 3, (blockIncrement α n i w k : ℝ) ^ 2 := bysimp only [squareLength, Int.cast_add, Fin.sum_univ_succ]ringsimp only [expectedSquareIncrement]simp_rw [hpoint, mul_add, Finset.sum_add_distrib]apply congrArg₂ (· + ·)· calc(∑ w : Fin n → Fin 19, (blockWeight α n i w : ℝ) *(2 * ∑ k : Fin 3, (h k : ℝ) *(blockIncrement α n i w k : ℝ))) =∑ w : Fin n → Fin 19, ∑ k : Fin 3,2 * (h k : ℝ) * ((blockWeight α n i w : ℝ) *(blockIncrement α n i w k : ℝ)) := byapply Finset.sum_congr rflintro w _simp only [Fin.sum_univ_succ]ring_ = ∑ k : Fin 3, ∑ w : Fin n → Fin 19,2 * (h k : ℝ) * ((blockWeight α n i w : ℝ) *(blockIncrement α n i w k : ℝ)) := Finset.sum_comm_ = 2 * (∑ k : Fin 3, (h k : ℝ) *(expectedAccum α n i k : ℝ)) := byrw [Finset.mul_sum]apply Finset.sum_congr rflintro k _rw [← Finset.mul_sum, sum_blockWeight_mul_increment_real]ring· rflrw [hexpand]calc_ ≤ 2 * (n * driftDot (imbalanceReal h) α +240 * ‖imbalanceReal h‖) + 2700 * n ^ 2 :=add_le_add (mul_le_mul_of_nonneg_left hdotError (by norm_num))(expected_block_square_le α n i)_ = 2 * n * driftDot (imbalanceReal h) α +480 * ‖imbalanceReal h‖ + 2700 * n ^ 2 := by ring/-- A block length large enough for its negative stationary drift to dominatethe uniformly bounded Poisson correction. -/noncomputable def controlBlockLength : ℕ :=Classical.choose (exists_nat_gt (241 / driftDelta))theorem controlBlockLength_drift :241 < (controlBlockLength : ℝ) * driftDelta := byhave h := Classical.choose_spec (exists_nat_gt (241 / driftDelta))apply (div_lt_iff₀ driftDelta_pos).mpsimpa only [controlBlockLength] using htheorem controlBlockLength_pos : 0 < controlBlockLength := byby_contra hhave hz : controlBlockLength = 0 := Nat.eq_zero_of_not_pos hhave hN := controlBlockLength_driftrw [hz] at hNnorm_num at hN/-- A finite radius outside which the block Lyapunov drift is at most `-1`. -/noncomputable def controlRadius : ℕ := 2700 * controlBlockLength ^ 2 + 1theorem controlled_block_drift (i : Fin 19) (h : Fin 3 → ℤ)(hout : controlRadius < ‖imbalanceReal h‖) :expectedSquareIncrement (feedback h) controlBlockLength i h ≤ -1 := bycalcexpectedSquareIncrement (feedback h) controlBlockLength i h ≤2 * controlBlockLength * driftDot (imbalanceReal h) (feedback h) +480 * ‖imbalanceReal h‖ + 2700 * controlBlockLength ^ 2 :=expectedSquareIncrement_le _ _ _ __ ≤ 2 * controlBlockLength *(-driftDelta * ‖imbalanceReal h‖) +480 * ‖imbalanceReal h‖ + 2700 * controlBlockLength ^ 2 := byhave hm := mul_le_mul_of_nonneg_left (feedback_drift_le h)(show 0 ≤ (2 : ℝ) * controlBlockLength by positivity)norm_num at hmlinarith_ ≤ -1 := byhave hN := controlBlockLength_driftdsimp [controlRadius] at houtpush_cast at houtnlinarith [norm_nonneg (imbalanceReal h)]/-! ## Elementary finite-choice Markov chainsThis file develops only the hitting-time facts needed below. Keeping thesample space as a finite choice at every step makes all one-step expectationsfinite sums and avoids importing an undeveloped general Markov-chain API.-/structure FiniteChoiceChain (S Ω : Type*) [Fintype Ω] wherenext : S → Ω → Sprobability : S → Ω → ℝprobability_nonneg : ∀ x ω, 0 ≤ probability x ωprobability_sum : ∀ x, ∑ ω, probability x ω = 1namespace FiniteChoiceChainvariable {S Ω : Type*} [Fintype Ω] [DecidableEq S]variable (K : FiniteChoiceChain S Ω) (C : Finset S)/-- Probability of avoiding `C` through time `n`, when time zero is included. -/def survival : ℕ → S → ℝ| 0, x => if x ∈ C then 0 else 1| n + 1, x =>if x ∈ C then 0 else ∑ ω, K.probability x ω * survival n (K.next x ω)/-- Probability of first hitting `C` exactly at time `n`, with the endpointsummed over all members of `C`. -/def hitTotal : ℕ → S → ℝ| 0, x => if x ∈ C then 1 else 0| n + 1, x =>if x ∈ C then 0 else ∑ ω, K.probability x ω * hitTotal n (K.next x ω)/-- Expected hitting time truncated at `n`. -/def truncatedHit : ℕ → S → ℝ| 0, _ => 0| n + 1, x =>if x ∈ C then 0 else1 + ∑ ω, K.probability x ω * truncatedHit n (K.next x ω)@[simp] theorem survival_zero_of_mem {x : S} (hx : x ∈ C) :K.survival C 0 x = 0 := by simp [survival, hx]@[simp] theorem survival_zero_of_not_mem {x : S} (hx : x ∉ C) :K.survival C 0 x = 1 := by simp [survival, hx]@[simp] theorem survival_succ_of_mem (n : ℕ) {x : S} (hx : x ∈ C) :K.survival C (n + 1) x = 0 := by simp [survival, hx]theorem survival_succ_of_not_mem (n : ℕ) {x : S} (hx : x ∉ C) :K.survival C (n + 1) x =∑ ω, K.probability x ω * K.survival C n (K.next x ω) := bysimp [survival, hx]@[simp] theorem hitTotal_succ_of_mem (n : ℕ) {x : S} (hx : x ∈ C) :K.hitTotal C (n + 1) x = 0 := by simp [hitTotal, hx]theorem hitTotal_succ_of_not_mem (n : ℕ) {x : S} (hx : x ∉ C) :K.hitTotal C (n + 1) x =∑ ω, K.probability x ω * K.hitTotal C n (K.next x ω) := bysimp [hitTotal, hx]theorem truncatedHit_succ_of_not_mem (n : ℕ) {x : S} (hx : x ∉ C) :K.truncatedHit C (n + 1) x =1 + ∑ ω, K.probability x ω * K.truncatedHit C n (K.next x ω) := bysimp [truncatedHit, hx]theorem survival_of_mem (n : ℕ) {x : S} (hx : x ∈ C) :K.survival C n x = 0 := bycases n with| zero => exact survival_zero_of_mem K C hx| succ n => exact survival_succ_of_mem K C n hxtheorem hitTotal_nonneg (n : ℕ) (x : S) : 0 ≤ K.hitTotal C n x := byinduction n generalizing x with| zero => simp only [hitTotal]; split_ifs <;> norm_num| succ n ih =>by_cases hx : x ∈ C· simp [hitTotal, hx]· rw [hitTotal_succ_of_not_mem K C n hx]exact Finset.sum_nonneg fun ω _ ↦mul_nonneg (K.probability_nonneg x ω) (ih (K.next x ω))theorem survival_nonneg (n : ℕ) (x : S) : 0 ≤ K.survival C n x := byinduction n generalizing x with| zero => simp only [survival]; split_ifs <;> norm_num| succ n ih =>by_cases hx : x ∈ C· simp [survival, hx]· rw [survival_succ_of_not_mem K C n hx]exact Finset.sum_nonneg fun ω _ ↦mul_nonneg (K.probability_nonneg x ω) (ih (K.next x ω))theorem survival_le_one (n : ℕ) (x : S) : K.survival C n x ≤ 1 := byinduction n generalizing x with| zero => simp only [survival]; split_ifs <;> norm_num| succ n ih =>by_cases hx : x ∈ C· simp [survival, hx]· rw [survival_succ_of_not_mem K C n hx, ← K.probability_sum x]apply Finset.sum_le_sumintro ω _simpa only [mul_one] usingmul_le_mul_of_nonneg_left (ih (K.next x ω)) (K.probability_nonneg x ω)/-- Exact one-step tail recursion. -/theorem hitTotal_succ_add_survival_succ (n : ℕ) (x : S) :K.hitTotal C (n + 1) x + K.survival C (n + 1) x =K.survival C n x := byinduction n generalizing x with| zero =>by_cases hx : x ∈ C· simp [hx]· rw [hitTotal_succ_of_not_mem K C 0 hx,survival_succ_of_not_mem K C 0 hx]rw [← Finset.sum_add_distrib]simp_rw [← mul_add]have hbase (y : S) : K.hitTotal C 0 y + K.survival C 0 y = 1 := byby_cases hy : y ∈ C <;> simp [hitTotal, survival, hy]simp_rw [hbase, mul_one, K.probability_sum]simp [survival, hx]| succ n ih =>by_cases hx : x ∈ C· simp [hx]· rw [hitTotal_succ_of_not_mem K C (n + 1) hx,survival_succ_of_not_mem K C (n + 1) hx,survival_succ_of_not_mem K C n hx, ← Finset.sum_add_distrib]apply Finset.sum_congr rflintro ω _rw [← mul_add, ih]/-- The probabilities of hitting by time `N` and surviving through time `N`partition the sample space. -/theorem sum_hitTotal_add_survival (N : ℕ) (x : S) :(∑ n ∈ Finset.range (N + 1), K.hitTotal C n x) +K.survival C N x = 1 := byinduction N with| zero =>by_cases hx : x ∈ C <;> simp [hitTotal, survival, hx]| succ N ih =>rw [Finset.sum_range_succ]have htail := hitTotal_succ_add_survival_succ K C N xlinariththeorem sum_hitTotal_le_one (N : ℕ) (x : S) :(∑ n ∈ Finset.range (N + 1), K.hitTotal C n x) ≤ 1 := byhave hpartition := sum_hitTotal_add_survival K C N xlinarith [survival_nonneg K C N x]/-- Truncation increases by the current survival probability. -/theorem truncatedHit_succ_eq_add_survival (n : ℕ) (x : S) :K.truncatedHit C (n + 1) x =K.truncatedHit C n x + K.survival C n x := byinduction n generalizing x with| zero =>by_cases hx : x ∈ C <;> simp [truncatedHit, survival, hx]| succ n ih =>by_cases hx : x ∈ C· simp [hx, truncatedHit]· rw [truncatedHit_succ_of_not_mem K C (n + 1) hx,truncatedHit_succ_of_not_mem K C n hx,survival_succ_of_not_mem K C n hx]simp_rw [ih]simp_rw [mul_add, Finset.sum_add_distrib]ring/-- Foster's drift inequality bounds all truncated hitting-time means. -/theorem truncatedHit_le_of_drift (V : S → ℝ) (hV : ∀ x, 0 ≤ V x)(hdrift : ∀ x, x ∉ C →(∑ ω, K.probability x ω * (V (K.next x ω) - V x)) ≤ -1)(n : ℕ) (x : S) : K.truncatedHit C n x ≤ V x := byinduction n generalizing x with| zero => simpa [truncatedHit] using hV x| succ n ih =>by_cases hx : x ∈ C· simp [truncatedHit, hx, hV x]· rw [truncatedHit_succ_of_not_mem K C n hx]calc_ ≤ 1 + ∑ ω, K.probability x ω * V (K.next x ω) := byapply add_le_add_rightapply Finset.sum_le_sumintro ω _exact mul_le_mul_of_nonneg_left (ih (K.next x ω))(K.probability_nonneg x ω)_ ≤ V x := byhave hd := hdrift x hxsimp_rw [mul_sub] at hdrw [Finset.sum_sub_distrib, ← Finset.sum_mul, K.probability_sum,one_mul] at hdlinarith/-! ### Positive returns from a point already in the finite set -/def returnSurvival (K : FiniteChoiceChain S Ω) (C : Finset S) : ℕ → S → ℝ| 0, _ => 1| n + 1, x => ∑ ω, K.probability x ω * K.survival C n (K.next x ω)def returnHitTotal (K : FiniteChoiceChain S Ω) (C : Finset S) : ℕ → S → ℝ| 0, _ => 0| n + 1, x => ∑ ω, K.probability x ω * K.hitTotal C n (K.next x ω)def returnTruncated (K : FiniteChoiceChain S Ω) (C : Finset S) : ℕ → S → ℝ| 0, _ => 0| n + 1, x => 1 + ∑ ω, K.probability x ω * K.truncatedHit C n (K.next x ω)theorem returnSurvival_nonneg (n : ℕ) (x : S) :0 ≤ K.returnSurvival C n x := bycases n with| zero => simp [returnSurvival]| succ n =>simp only [returnSurvival]exact Finset.sum_nonneg fun ω _ ↦ mul_nonneg (K.probability_nonneg x ω)(survival_nonneg K C n (K.next x ω))theorem returnHitTotal_nonneg (n : ℕ) (x : S) :0 ≤ K.returnHitTotal C n x := bycases n with| zero => simp [returnHitTotal]| succ n =>simp only [returnHitTotal]exact Finset.sum_nonneg fun ω _ ↦ mul_nonneg (K.probability_nonneg x ω)(hitTotal_nonneg K C n (K.next x ω))theorem return_hit_add_survival (n : ℕ) (x : S) :K.returnHitTotal C (n + 1) x + K.returnSurvival C (n + 1) x =K.returnSurvival C n x := bycases n with| zero =>simp only [returnHitTotal, returnSurvival]rw [← Finset.sum_add_distrib]simp_rw [← mul_add]have hbase (y : S) : K.hitTotal C 0 y + K.survival C 0 y = 1 := byby_cases hy : y ∈ C <;> simp [hitTotal, survival, hy]simp_rw [hbase, mul_one, K.probability_sum]| succ n =>simp only [returnHitTotal, returnSurvival]rw [← Finset.sum_add_distrib]apply Finset.sum_congr rflintro ω _rw [← mul_add, hitTotal_succ_add_survival_succ]theorem returnTruncated_succ_eq_add_survival (n : ℕ) (x : S) :K.returnTruncated C (n + 1) x =K.returnTruncated C n x + K.returnSurvival C n x := bycases n with| zero => simp [returnTruncated, returnSurvival, truncatedHit]| succ n =>simp only [returnTruncated, returnSurvival]simp_rw [truncatedHit_succ_eq_add_survival]simp_rw [mul_add, Finset.sum_add_distrib]ringtheorem return_partialMean_add_tail (N : ℕ) (x : S) :(∑ n ∈ Finset.range (N + 1), (n : ℝ) * K.returnHitTotal C n x) +N * K.returnSurvival C N x = K.returnTruncated C N x := byinduction N with| zero => simp [returnTruncated]| succ N ih =>rw [Finset.sum_range_succ]have hhit := return_hit_add_survival K C N xhave htrunc := returnTruncated_succ_eq_add_survival K C N xnorm_num only [Nat.cast_add, Nat.cast_one]nlinariththeorem returnTruncated_le (V : S → ℝ) (hV : ∀ x, 0 ≤ V x)(hdrift : ∀ x, x ∉ C →(∑ ω, K.probability x ω * (V (K.next x ω) - V x)) ≤ -1)(n : ℕ) (x : S) :K.returnTruncated C n x ≤ 1 + ∑ ω, K.probability x ω * V (K.next x ω) := bycases n with| zero =>simp only [returnTruncated]exact add_nonneg (by norm_num) (Finset.sum_nonneg fun ω _ ↦mul_nonneg (K.probability_nonneg x ω) (hV (K.next x ω)))| succ n =>simp only [returnTruncated]apply add_le_add_rightapply Finset.sum_le_sumintro ω _exact mul_le_mul_of_nonneg_left(truncatedHit_le_of_drift K C V hV hdrift n (K.next x ω))(K.probability_nonneg x ω)theorem return_mul_survival_le_truncated (n : ℕ) (x : S) :(n : ℝ) * K.returnSurvival C n x ≤ K.returnTruncated C n x := byinduction n with| zero => simp [returnTruncated]| succ n ih =>rw [returnTruncated_succ_eq_add_survival K C n x,Nat.cast_add, Nat.cast_one]have hmono : K.returnSurvival C (n + 1) x ≤ K.returnSurvival C n x := byhave h := return_hit_add_survival K C n xlinarith [returnHitTotal_nonneg K C (n + 1) x]have hle : K.returnSurvival C (n + 1) x ≤ 1 := bycases n with| zero =>simp only [returnSurvival]rw [← K.probability_sum x]apply Finset.sum_le_sumintro ω _simpa only [mul_one] usingmul_le_mul_of_nonneg_left (survival_le_one K C 0 (K.next x ω))(K.probability_nonneg x ω)| succ n =>exact hmono.trans (bysimp only [returnSurvival]rw [← K.probability_sum x]apply Finset.sum_le_sumintro ω _simpa only [mul_one] using mul_le_mul_of_nonneg_left(survival_le_one K C n (K.next x ω)) (K.probability_nonneg x ω))nlinariththeorem returnSurvival_tendsto_zero (V : S → ℝ) (hV : ∀ x, 0 ≤ V x)(hdrift : ∀ x, x ∉ C →(∑ ω, K.probability x ω * (V (K.next x ω) - V x)) ≤ -1)(x : S) : Filter.Tendsto (fun n ↦ K.returnSurvival C n x)Filter.atTop (nhds 0) := bylet B := 1 + ∑ ω, K.probability x ω * V (K.next x ω)have hB : 0 ≤ B := bydsimp [B]exact add_nonneg (by norm_num) (Finset.sum_nonneg fun ω _ ↦mul_nonneg (K.probability_nonneg x ω) (hV (K.next x ω)))have hbound (n : ℕ) :K.returnSurvival C (n + 1) x ≤ B / (n + 1 : ℝ) := byhave hm := return_mul_survival_le_truncated K C (n + 1) xhave ht := returnTruncated_le K C V hV hdrift (n + 1) xapply (le_div_iff₀ (by positivity : (0 : ℝ) < n + 1)).2calcK.returnSurvival C (n + 1) x * (n + 1 : ℝ) =(n + 1 : ℝ) * K.returnSurvival C (n + 1) x := by ring_ ≤ K.returnTruncated C (n + 1) x := by simpa using hm_ ≤ B := by simpa [B] using hthave hupper : Filter.Tendsto (fun n : ℕ ↦ B / (n + 1 : ℝ))Filter.atTop (nhds 0) := bysimpa [div_eq_mul_inv] usingtendsto_const_nhds.mul(tendsto_one_div_add_atTop_nhds_zero_nat (𝕜 := ℝ))have hshift : Filter.Tendsto (fun n ↦ K.returnSurvival C (n + 1) x)Filter.atTop (nhds 0) := byapply squeeze_zero'(Filter.Eventually.of_forall fun n ↦ returnSurvival_nonneg K C (n + 1) x)(Filter.Eventually.of_forall hbound) hupperexact (Filter.tendsto_add_atTop_iff_nat 1).mp (by simpa [Nat.add_comm] using hshift)theorem returnHitTotal_hasSum_one (V : S → ℝ) (hV : ∀ x, 0 ≤ V x)(hdrift : ∀ x, x ∉ C →(∑ ω, K.probability x ω * (V (K.next x ω) - V x)) ≤ -1)(x : S) : HasSum (fun n ↦ K.returnHitTotal C n x) 1 := byrw [hasSum_iff_tendsto_nat_of_nonneg(fun n ↦ returnHitTotal_nonneg K C n x) 1]have hsurv := returnSurvival_tendsto_zero K C V hV hdrift xhave hpartial (N : ℕ) :∑ n ∈ Finset.range (N + 1), K.returnHitTotal C n x =1 - K.returnSurvival C N x := byinduction N with| zero => simp [returnHitTotal, returnSurvival]| succ N ih =>rw [Finset.sum_range_succ, ih]have htail := return_hit_add_survival K C N xlinarithhave hshift : Filter.Tendsto(fun N ↦ ∑ n ∈ Finset.range (N + 1), K.returnHitTotal C n x)Filter.atTop (nhds 1) := byrw [show (fun N ↦ ∑ n ∈ Finset.range (N + 1), K.returnHitTotal C n x) =fun N ↦ 1 - K.returnSurvival C N x from funext hpartial]simpa using tendsto_const_nhds.sub hsurvexact (Filter.tendsto_add_atTop_iff_nat 1).mp (bysimpa [Nat.add_comm] using hshift)theorem weighted_returnHitTotal_summable (V : S → ℝ) (hV : ∀ x, 0 ≤ V x)(hdrift : ∀ x, x ∉ C →(∑ ω, K.probability x ω * (V (K.next x ω) - V x)) ≤ -1)(x : S) : Summable (fun n : ℕ ↦ (n : ℝ) * K.returnHitTotal C n x) := bylet B := 1 + ∑ ω, K.probability x ω * V (K.next x ω)apply summable_of_sum_range_le (c := B)· intro nexact mul_nonneg (Nat.cast_nonneg _) (returnHitTotal_nonneg K C n x)· intro Ncases N with| zero =>dsimp [B]exact add_nonneg (by norm_num) (Finset.sum_nonneg fun ω _ ↦mul_nonneg (K.probability_nonneg x ω) (hV (K.next x ω)))| succ N =>have htail := return_partialMean_add_tail K C N xhave ht := returnTruncated_le K C V hV hdrift N xhave hzero : (0 : ℝ) ≤ N * K.returnSurvival C N x :=mul_nonneg (Nat.cast_nonneg _) (returnSurvival_nonneg K C N x)dsimp [B]linarith/-! ### Endpoints of finite-set excursions -//-- Probability of first hitting `C` at the specified point and exact time. -/def hitAt (K : FiniteChoiceChain S Ω) (C : Finset S) (y : S) : ℕ → S → ℝ| 0, x => if x ∈ C ∧ x = y then 1 else 0| n + 1, x => if x ∈ C then 0 else∑ ω, K.probability x ω * K.hitAt C y n (K.next x ω)theorem hitAt_nonneg (y : S) (n : ℕ) (x : S) : 0 ≤ K.hitAt C y n x := byinduction n generalizing x with| zero => simp only [hitAt]; split_ifs <;> norm_num| succ n ih =>by_cases hx : x ∈ C· simp [hitAt, hx]· simp only [hitAt, hx, if_false]exact Finset.sum_nonneg fun ω _ ↦ mul_nonneg (K.probability_nonneg x ω)(ih (K.next x ω))theorem sum_hitAt_eq_hitTotal (n : ℕ) (x : S) :∑ y ∈ C, K.hitAt C y n x = K.hitTotal C n x := byinduction n generalizing x with| zero =>by_cases hx : x ∈ C· simp [hitAt, hitTotal, hx]· simp [hitAt, hitTotal, hx]| succ n ih =>by_cases hx : x ∈ C· simp [hitAt, hitTotal, hx]· simp only [hitAt, hitTotal, hx, if_false]rw [Finset.sum_comm]apply Finset.sum_congr rflintro ω _rw [← Finset.mul_sum, ih]/-- Probability of a positive first return to `C` ending at `y`. -/def returnAt (K : FiniteChoiceChain S Ω) (C : Finset S) (y : S) : ℕ → S → ℝ| 0, _ => 0| n + 1, x => ∑ ω, K.probability x ω * K.hitAt C y n (K.next x ω)theorem returnAt_nonneg (y : S) (n : ℕ) (x : S) :0 ≤ K.returnAt C y n x := bycases n with| zero => simp [returnAt]| succ n =>simp only [returnAt]exact Finset.sum_nonneg fun ω _ ↦ mul_nonneg (K.probability_nonneg x ω)(hitAt_nonneg K C y n (K.next x ω))theorem sum_returnAt_eq_returnHitTotal (n : ℕ) (x : S) :∑ y ∈ C, K.returnAt C y n x = K.returnHitTotal C n x := bycases n with| zero => simp [returnAt, returnHitTotal]| succ n =>simp only [returnAt, returnHitTotal]rw [Finset.sum_comm]apply Finset.sum_congr rflintro ω _rw [← Finset.mul_sum, sum_hitAt_eq_hitTotal]theorem returnAt_le_returnHitTotal {y : S} (hy : y ∈ C) (n : ℕ) (x : S) :K.returnAt C y n x ≤ K.returnHitTotal C n x := byrw [← sum_returnAt_eq_returnHitTotal K C n x]exact Finset.single_le_sum (fun z _ ↦ returnAt_nonneg K C z n x) hytheorem returnAt_summable (V : S → ℝ) (hV : ∀ x, 0 ≤ V x)(hdrift : ∀ x, x ∉ C →(∑ ω, K.probability x ω * (V (K.next x ω) - V x)) ≤ -1){y : S} (hy : y ∈ C) (x : S) : Summable (fun n ↦ K.returnAt C y n x) := byapply Summable.of_nonneg_of_le (fun n ↦ returnAt_nonneg K C y n x)(fun n ↦ returnAt_le_returnHitTotal K C hy n x)exact (returnHitTotal_hasSum_one K C V hV hdrift x).summable/-- Transition probability of the chain induced by successive visits to `C`. -/noncomputable def inducedProbability (K : FiniteChoiceChain S Ω) (C : Finset S)(x y : S) : ℝ := ∑' n, K.returnAt C y n xtheorem inducedProbability_nonneg (x y : S) : 0 ≤ K.inducedProbability C x y := byexact tsum_nonneg fun n ↦ returnAt_nonneg K C y n xtheorem inducedProbability_row_sum (V : S → ℝ) (hV : ∀ x, 0 ≤ V x)(hdrift : ∀ x, x ∉ C →(∑ ω, K.probability x ω * (V (K.next x ω) - V x)) ≤ -1)(x : S) : ∑ y ∈ C, K.inducedProbability C x y = 1 := byhave hsummable (y : S) (hy : y ∈ C) :Summable (fun n ↦ K.returnAt C y n x) :=returnAt_summable K C V hV hdrift hy xsimp only [inducedProbability]rw [← Summable.tsum_finsetSum hsummable]simp_rw [sum_returnAt_eq_returnHitTotal]exact (returnHitTotal_hasSum_one K C V hV hdrift x).tsum_eqend FiniteChoiceChain/-! ## The feedback-controlled augmented chain -/abbrev ImbalanceState := Fin 3 → ℤabbrev AugmentedState := Fin 19 × ImbalanceStateabbrev BlockChoice := Fin controlBlockLength → Fin 19/-- One controlled block, including its endpoint and net imbalance. -/noncomputable def controlledNext (x : AugmentedState) (w : BlockChoice) :AugmentedState :=(blockEndpoint controlBlockLength x.1 w,fun k ↦ x.2 k + blockIncrement (feedback x.2) controlBlockLength x.1 w k)noncomputable def controlledProbability (x : AugmentedState) (w : BlockChoice) : ℝ :=blockWeight (feedback x.2) controlBlockLength x.1 wnoncomputable def controlledChain : FiniteChoiceChain AugmentedState BlockChoice wherenext := controlledNextprobability := controlledProbabilityprobability_nonneg := byintro x wchange (0 : ℝ) ≤ (blockWeight (feedback x.2) controlBlockLength x.1 w : ℝ)exact_mod_cast blockWeight_nonneg (feedback x.2) controlBlockLength x.1 wprobability_sum := byintro xexact sum_blockWeight_real (feedback x.2) controlBlockLength x.1noncomputable abbrev BoundedCoordinate :={z : ℤ // z ∈ Finset.Icc (-(controlRadius : ℤ)) controlRadius}/-- The finite collection of integer imbalance vectors in the control box. -/noncomputable def boundedImbalances : Finset ImbalanceState :=Finset.univ.image fun f : Fin 3 → BoundedCoordinate ↦ fun k ↦ (f k : ℤ)theorem mem_boundedImbalances_iff (h : ImbalanceState) :h ∈ boundedImbalances ↔∀ k, -(controlRadius : ℤ) ≤ h k ∧ h k ≤ controlRadius := byconstructor· intro hh ksimp only [boundedImbalances, Finset.mem_image] at hhobtain ⟨f, _, rfl⟩ := hhexact Finset.mem_Icc.mp (f k).property· intro hhlet f : Fin 3 → BoundedCoordinate := fun k ↦ ⟨h k, Finset.mem_Icc.mpr (hh k)⟩simp only [boundedImbalances, Finset.mem_image]exact ⟨f, Finset.mem_univ f, by rfl⟩/-- The finite small set used in the Foster argument. -/noncomputable def controlSet : Finset AugmentedState :=Finset.univ.product boundedImbalancestheorem mem_controlSet_iff (x : AugmentedState) :x ∈ controlSet ↔∀ k, -(controlRadius : ℤ) ≤ x.2 k ∧ x.2 k ≤ controlRadius := bysimp [controlSet, mem_boundedImbalances_iff]theorem controlRadius_lt_norm_of_not_mem {x : AugmentedState} (hx : x ∉ controlSet) :controlRadius < ‖imbalanceReal x.2‖ := byby_contra happly hxapply (mem_controlSet_iff x).mprhave hnorm : ‖imbalanceReal x.2‖ ≤ controlRadius := le_of_not_gt hhave hcoords := (pi_norm_le_iff_of_nonneg(by positivity : (0 : ℝ) ≤ controlRadius)).mp hnormintro khave hk := hcoords krw [Real.norm_eq_abs, abs_le] at hkhave hk' : (-(controlRadius : ℝ) ≤ (x.2 k : ℝ) ∧(x.2 k : ℝ) ≤ controlRadius) := by simpa [imbalanceReal] using hkexact_mod_cast hk'def controlledLyapunov (x : AugmentedState) : ℝ := squareLength x.2theorem controlledLyapunov_nonneg (x : AugmentedState) : 0 ≤ controlledLyapunov x := bysimp only [controlledLyapunov, squareLength]exact Finset.sum_nonneg fun k _ ↦ sq_nonneg (x.2 k : ℝ)theorem controlled_drift (x : AugmentedState) (hx : x ∉ controlSet) :(∑ w : BlockChoice, controlledChain.probability x w *(controlledLyapunov (controlledChain.next x w) - controlledLyapunov x)) ≤ -1 := bysimpa [controlledChain, controlledNext, controlledProbability, controlledLyapunov,expectedSquareIncrement] usingcontrolled_block_drift x.1 x.2 (controlRadius_lt_norm_of_not_mem hx)theorem controlled_return_mean_finite (x : AugmentedState) :Summable (fun n : ℕ ↦ (n : ℝ) *controlledChain.returnHitTotal controlSet n x) :=controlledChain.weighted_returnHitTotal_summable controlSet controlledLyapunovcontrolledLyapunov_nonneg controlled_drift xtheorem controlled_returnAt_summable {y : AugmentedState} (hy : y ∈ controlSet)(x : AugmentedState) :Summable (fun n ↦ controlledChain.returnAt controlSet y n x) :=controlledChain.returnAt_summable controlSet controlledLyapunovcontrolledLyapunov_nonneg controlled_drift hy x/-! ## Repeated excursions to the finite control set -//-- Cauchy convolution, written using the finite antidiagonal of a naturalnumber. -/def convolution (f g : ℕ → ℝ) (n : ℕ) : ℝ :=∑ p ∈ Finset.HasAntidiagonal.antidiagonal n, f p.1 * g p.2theorem summable_prod_of_nonnegative {f g : ℕ → ℝ}(hf0 : ∀ n, 0 ≤ f n) (hg0 : ∀ n, 0 ≤ g n)(hf : Summable f) (hg : Summable g) :Summable fun p : ℕ × ℕ ↦ f p.1 * g p.2 := byrw [summable_prod_of_nonneg (fun p ↦ mul_nonneg (hf0 p.1) (hg0 p.2))]constructor· intro nexact hg.mul_left (f n)· simpa only [hg.tsum_mul_left] using hf.mul_right (∑' m, g m)theorem convolution_nonneg {f g : ℕ → ℝ}(hf0 : ∀ n, 0 ≤ f n) (hg0 : ∀ n, 0 ≤ g n) (n : ℕ) :0 ≤ convolution f g n := byexact Finset.sum_nonneg fun p _ ↦ mul_nonneg (hf0 p.1) (hg0 p.2)theorem convolution_summable {f g : ℕ → ℝ}(hf0 : ∀ n, 0 ≤ f n) (hg0 : ∀ n, 0 ≤ g n)(hf : Summable f) (hg : Summable g) : Summable (convolution f g) := byhave hprod := summable_prod_of_nonnegative hf0 hg0 hf hgexact summable_sum_mul_antidiagonal_of_summable_mul hprodtheorem tsum_convolution {f g : ℕ → ℝ}(hf0 : ∀ n, 0 ≤ f n) (hg0 : ∀ n, 0 ≤ g n)(hf : Summable f) (hg : Summable g) :∑' n, convolution f g n = (∑' n, f n) * ∑' n, g n := byexact (hf.tsum_mul_tsum_eq_tsum_sum_antidiagonal hg(summable_prod_of_nonnegative hf0 hg0 hf hg)).symmtheorem weighted_convolution_eq (f g : ℕ → ℝ) (n : ℕ) :(n : ℝ) * convolution f g n =convolution (fun a ↦ (a : ℝ) * f a) g n +convolution f (fun b ↦ (b : ℝ) * g b) n := bysimp only [convolution, Finset.mul_sum]rw [← Finset.sum_add_distrib]apply Finset.sum_congr rflintro p hphave hpn : p.1 + p.2 = n := Finset.HasAntidiagonal.mem_antidiagonal.mp hprw [← hpn, Nat.cast_add]ringtheorem weighted_convolution_summable {f g : ℕ → ℝ}(hf0 : ∀ n, 0 ≤ f n) (hg0 : ∀ n, 0 ≤ g n)(hf : Summable f) (hg : Summable g)(hfw : Summable fun n : ℕ ↦ (n : ℝ) * f n)(hgw : Summable fun n : ℕ ↦ (n : ℝ) * g n) :Summable fun n : ℕ ↦ (n : ℝ) * convolution f g n := byhave hleft := convolution_summable(fun n ↦ mul_nonneg (Nat.cast_nonneg n) (hf0 n)) hg0 hfw hghave hright := convolution_summable hf0(fun n ↦ mul_nonneg (Nat.cast_nonneg n) (hg0 n)) hf hgwhave heq : (fun n : ℕ ↦ (n : ℝ) * convolution f g n) =fun n ↦ convolution (fun a ↦ (a : ℝ) * f a) g n +convolution f (fun b ↦ (b : ℝ) * g b) n := byfunext nexact weighted_convolution_eq f g nrw [heq]exact hleft.add hrighttheorem tsum_weighted_convolution {f g : ℕ → ℝ}(hf0 : ∀ n, 0 ≤ f n) (hg0 : ∀ n, 0 ≤ g n)(hf : Summable f) (hg : Summable g)(hfw : Summable fun n : ℕ ↦ (n : ℝ) * f n)(hgw : Summable fun n : ℕ ↦ (n : ℝ) * g n) :(∑' n : ℕ, (n : ℝ) * convolution f g n) =(∑' n : ℕ, (n : ℝ) * f n) * (∑' n, g n) +(∑' n, f n) * ∑' n : ℕ, (n : ℝ) * g n := byhave hleft := convolution_summable(fun n ↦ mul_nonneg (Nat.cast_nonneg n) (hf0 n)) hg0 hfw hghave hright := convolution_summable hf0(fun n ↦ mul_nonneg (Nat.cast_nonneg n) (hg0 n)) hf hgwrw [show (fun n : ℕ ↦ (n : ℝ) * convolution f g n) =fun n ↦ convolution (fun a ↦ (a : ℝ) * f a) g n +convolution f (fun b ↦ (b : ℝ) * g b) n fromfunext (weighted_convolution_eq f g)]rw [hleft.tsum_add hright,tsum_convolution (fun n ↦ mul_nonneg (Nat.cast_nonneg n) (hf0 n))hg0 hfw hg,tsum_convolution hf0(fun n ↦ mul_nonneg (Nat.cast_nonneg n) (hg0 n)) hf hgw]theorem summable_finset_sum {A : Type*} (s : Finset A) (f : A → ℕ → ℝ)(hf : ∀ a ∈ s, Summable (f a)) :Summable fun n ↦ ∑ a ∈ s, f a n := byclassicalinduction s using Finset.induction_on with| empty => simp| @insert a s ha ih =>simp only [Finset.mem_insert] at hfsimpa [ha] using (hf a (Or.inl rfl)).add(ih fun b hb ↦ hf b (Or.inr hb))theorem controlled_weighted_returnAt_summable {y : AugmentedState}(hy : y ∈ controlSet) (x : AugmentedState) :Summable fun n : ℕ ↦ (n : ℝ) * controlledChain.returnAt controlSet y n x := byapply Summable.of_nonneg_of_le(fun n ↦ mul_nonneg (Nat.cast_nonneg n)(controlledChain.returnAt_nonneg controlSet y n x))(fun n ↦ mul_le_mul_of_nonneg_left(controlledChain.returnAt_le_returnHitTotal controlSet hy n x)(Nat.cast_nonneg n))exact controlled_return_mean_finite x/-- One plus the sum of the mean return times over the finite control set. -/noncomputable def excursionMeanBound : ℝ :=1 + ∑ x ∈ controlSet,∑' n : ℕ, (n : ℝ) * controlledChain.returnHitTotal controlSet n xtheorem excursionMeanBound_pos : 0 < excursionMeanBound := bydsimp only [excursionMeanBound]have hsum : 0 ≤ ∑ x ∈ controlSet,∑' n : ℕ, (n : ℝ) * controlledChain.returnHitTotal controlSet n x :=Finset.sum_nonneg fun x _ ↦ tsum_nonneg fun n ↦mul_nonneg (Nat.cast_nonneg n)(controlledChain.returnHitTotal_nonneg controlSet n x)linariththeorem returnMean_le_excursionMeanBound {x : AugmentedState} (hx : x ∈ controlSet) :(∑' n : ℕ, (n : ℝ) * controlledChain.returnHitTotal controlSet n x) ≤excursionMeanBound := byhave hsingle :(∑' n : ℕ, (n : ℝ) * controlledChain.returnHitTotal controlSet n x) ≤∑ y ∈ controlSet,∑' n : ℕ, (n : ℝ) *controlledChain.returnHitTotal controlSet n y := byexact Finset.single_le_sum (fun y _ ↦ tsum_nonneg fun n ↦mul_nonneg (Nat.cast_nonneg n)(controlledChain.returnHitTotal_nonneg controlSet n y)) hxdsimp only [excursionMeanBound]linariththeorem sum_weighted_returnAt (x : AugmentedState) :(∑ y ∈ controlSet,∑' n : ℕ, (n : ℝ) * controlledChain.returnAt controlSet y n x) =∑' n : ℕ, (n : ℝ) *controlledChain.returnHitTotal controlSet n x := byhave hs (y : AugmentedState) (hy : y ∈ controlSet) :=controlled_weighted_returnAt_summable hy xrw [← Summable.tsum_finsetSum hs]apply tsum_congrintro nrw [← Finset.mul_sum,controlledChain.sum_returnAt_eq_returnHitTotal controlSet n x]/-- The zero imbalance at interval zero is a concrete member of the finitecontrol set. -/def originState : AugmentedState := (0, fun _ ↦ 0)theorem originState_mem_controlSet : originState ∈ controlSet := byrw [mem_controlSet_iff]intro jsimp [originState]/-! ## Finite-choice paths -//-- A noncomputable indicator, convenient for finite sums over predicates. -/noncomputable def selectedWeight (P : Prop) (r : ℝ) : ℝ := byclassicalexact if P then r else 0theorem selectedWeight_mul_left (P : Prop) (a r : ℝ) :selectedWeight P (a * r) = a * selectedWeight P r := byclassicalby_cases hP : P <;> simp [selectedWeight, hP]theorem sum_subtype_eq_selected {A : Type*} [Fintype A](P : A → Prop) [Fintype {a : A // P a}] (g : A → ℝ) :(∑ x : {a : A // P a}, g x.1) = ∑ a, selectedWeight (P a) (g a) := byclassicalrw [← Finset.sum_subtype (Finset.univ.filter P) (by simp) g]simpa only [selectedWeight] using Finset.sum_filter P gtheorem sum_finset_subtype {A : Type*} (s : Finset A) (g : A → ℝ) :(∑ x : ↑s, g x.1) = ∑ x ∈ s, g x := byclassicalsimpa using Finset.sum_attach s gtheorem sum_subtype_eq_sum_attach {A : Type*} (s : Finset A) (g : ↑s → ℝ) :(∑ x : ↑s, g x) = ∑ x ∈ s.attach, g x := byclassicalrflnamespace FiniteChoiceChainvariable {S Ω : Type*} [Fintype Ω] [DecidableEq S]/-- Endpoint after a tuple of choices. -/def pathEndpoint (K : FiniteChoiceChain S Ω) :(n : ℕ) → S → (Fin n → Ω) → S| 0, x, _ => x| n + 1, x, w => K.pathEndpoint n (K.next x (w 0)) (Fin.tail w)/-- Product of the one-step choice probabilities along a tuple. -/def pathWeight (K : FiniteChoiceChain S Ω) :(n : ℕ) → S → (Fin n → Ω) → ℝ| 0, _, _ => 1| n + 1, x, w =>K.probability x (w 0) * K.pathWeight n (K.next x (w 0)) (Fin.tail w)/-- A path that hits `C` for the first time at its endpoint `y`; time zerocounts as a hit. -/def IsHitPath (K : FiniteChoiceChain S Ω) (C : Finset S) (y : S) :(n : ℕ) → S → (Fin n → Ω) → Prop| 0, x, _ => x ∈ C ∧ x = y| n + 1, x, w => x ∉ C ∧K.IsHitPath C y n (K.next x (w 0)) (Fin.tail w)/-- A positive first return to `C`, ending at `y`. -/def IsReturnPath (K : FiniteChoiceChain S Ω) (C : Finset S) (y : S) :(n : ℕ) → S → (Fin n → Ω) → Prop| 0, _, _ => False| n + 1, x, w =>K.IsHitPath C y n (K.next x (w 0)) (Fin.tail w)omit [DecidableEq S] in@[simp] theorem pathEndpoint_cons (K : FiniteChoiceChain S Ω) (n : ℕ)(x : S) (a : Ω) (w : Fin n → Ω) :K.pathEndpoint (n + 1) x (Fin.cons a w) =K.pathEndpoint n (K.next x a) w := bysimp [pathEndpoint]omit [DecidableEq S] in@[simp] theorem pathWeight_cons (K : FiniteChoiceChain S Ω) (n : ℕ)(x : S) (a : Ω) (w : Fin n → Ω) :K.pathWeight (n + 1) x (Fin.cons a w) =K.probability x a * K.pathWeight n (K.next x a) w := bysimp [pathWeight]omit [DecidableEq S] in@[simp] theorem isHitPath_zero (K : FiniteChoiceChain S Ω) (C : Finset S)(x y : S) (w : Fin 0 → Ω) : K.IsHitPath C y 0 x w ↔ x ∈ C ∧ x = y :=Iff.rflomit [DecidableEq S] in@[simp] theorem isHitPath_cons (K : FiniteChoiceChain S Ω) (C : Finset S)(n : ℕ) (x y : S) (a : Ω) (w : Fin n → Ω) :K.IsHitPath C y (n + 1) x (Fin.cons a w) ↔x ∉ C ∧ K.IsHitPath C y n (K.next x a) w := bysimp [IsHitPath]omit [DecidableEq S] in@[simp] theorem isReturnPath_zero (K : FiniteChoiceChain S Ω) (C : Finset S)(x y : S) (w : Fin 0 → Ω) : ¬ K.IsReturnPath C y 0 x w := bysimp [IsReturnPath]omit [DecidableEq S] in@[simp] theorem isReturnPath_cons (K : FiniteChoiceChain S Ω) (C : Finset S)(n : ℕ) (x y : S) (a : Ω) (w : Fin n → Ω) :K.IsReturnPath C y (n + 1) x (Fin.cons a w) ↔K.IsHitPath C y n (K.next x a) w := bysimp [IsReturnPath]theorem sum_hitPath_weight (K : FiniteChoiceChain S Ω) (C : Finset S)(y : S) (n : ℕ) (x : S) :∑ w : Fin n → Ω,selectedWeight (K.IsHitPath C y n x w) (K.pathWeight n x w) =K.hitAt C y n x := byclassicalinduction n generalizing x with| zero =>by_cases hxy : x ∈ C ∧ x = y <;>simp [selectedWeight, IsHitPath, hitAt, pathWeight, hxy]| succ n ih =>rw [sum_tuple_succ]by_cases hx : x ∈ C· simp [selectedWeight, IsHitPath, hitAt, hx]· simp only [isHitPath_cons, hx, not_false_eq_true,true_and, pathWeight_cons, hitAt, if_false]apply Finset.sum_congr rflintro a _simp_rw [selectedWeight_mul_left]rw [← Finset.mul_sum, ih]theorem sum_returnPath_weight (K : FiniteChoiceChain S Ω) (C : Finset S)(y : S) (n : ℕ) (x : S) :∑ w : Fin n → Ω,selectedWeight (K.IsReturnPath C y n x w) (K.pathWeight n x w) =K.returnAt C y n x := byclassicalcases n with| zero => simp [selectedWeight, IsReturnPath, returnAt]| succ n =>rw [sum_tuple_succ]simp only [isReturnPath_cons, pathWeight_cons, returnAt]apply Finset.sum_congr rflintro a _simp_rw [selectedWeight_mul_left]rw [← Finset.mul_sum, sum_hitPath_weight]end FiniteChoiceChain/-! ## Recurrence in a finite stochastic kernel -/structure FiniteKernel (F : Type*) [Fintype F] whereprobability : F → F → ℝprobability_nonneg : ∀ x y, 0 ≤ probability x yprobability_sum : ∀ x, ∑ y, probability x y = 1namespace FiniteKernelvariable {F : Type*} [Fintype F] [DecidableEq F] [Nonempty F]def Edge (P : FiniteKernel F) (x y : F) : Prop := 0 < P.probability x ydef EdgePath (P : FiniteKernel F) : F → List F → F → Prop| x, [], y => x = y| x, z :: zs, y => P.Edge x z ∧ P.EdgePath z zs yomit [DecidableEq F] [Nonempty F] intheorem EdgePath.append (P : FiniteKernel F) {x y z : F} {u v : List F}(hu : P.EdgePath x u y) (hv : P.EdgePath y v z) :P.EdgePath x (u ++ v) z := byinduction u generalizing x with| nil => simpa [EdgePath] using hu ▸ hv| cons a u ih =>obtain ⟨hxa, hau⟩ := huexact ⟨hxa, ih hau⟩noncomputable def reachable (P : FiniteKernel F) (x : F) : Finset F :=byclassicalexact Finset.univ.filter fun y ↦ ∃ w, P.EdgePath x w yomit [DecidableEq F] [Nonempty F] intheorem mem_reachable_iff (P : FiniteKernel F) (x y : F) :y ∈ P.reachable x ↔ ∃ w, P.EdgePath x w y := byclassicalsimp [reachable]omit [DecidableEq F] [Nonempty F] intheorem self_mem_reachable (P : FiniteKernel F) (x : F) : x ∈ P.reachable x := byexact (P.mem_reachable_iff x x).2 ⟨[], rfl⟩omit [DecidableEq F] [Nonempty F] intheorem reachable_mono {P : FiniteKernel F} {x y : F} (hxy : y ∈ P.reachable x) :P.reachable y ⊆ P.reachable x := byintro z hzobtain ⟨u, hu⟩ := (P.mem_reachable_iff x y).1 hxyobtain ⟨v, hv⟩ := (P.mem_reachable_iff y z).1 hzexact (P.mem_reachable_iff x z).2 ⟨u ++ v, hu.append P hv⟩/-- A state whose reachable set has minimum cardinality. -/noncomputable def recurrentState (P : FiniteKernel F) : F :=Classical.choose (Finset.exists_min_image Finset.univ(fun x ↦ (P.reachable x).card) Finset.univ_nonempty)omit [DecidableEq F] intheorem recurrentState_minimal (P : FiniteKernel F) (x : F) :(P.reachable P.recurrentState).card ≤ (P.reachable x).card := byexact (Classical.choose_spec (Finset.exists_min_image Finset.univ(fun x ↦ (P.reachable x).card) Finset.univ_nonempty)).2 x (Finset.mem_univ x)omit [DecidableEq F] intheorem recurrent_reachable_eq {P : FiniteKernel F} {y : F}(hy : y ∈ P.reachable P.recurrentState) :P.reachable y = P.reachable P.recurrentState := byapply Finset.eq_of_subset_of_card_le (reachable_mono hy)exact P.recurrentState_minimal yomit [DecidableEq F] intheorem recurrentState_reachable_from {P : FiniteKernel F} {y : F}(hy : y ∈ P.reachable P.recurrentState) :P.recurrentState ∈ P.reachable y := byrw [recurrent_reachable_eq hy]exact P.self_mem_reachable P.recurrentStateomit [DecidableEq F] intheorem reachable_closed_edge {P : FiniteKernel F} {x y : F}(hx : x ∈ P.reachable P.recurrentState) (hxy : P.Edge x y) :y ∈ P.reachable P.recurrentState := byobtain ⟨u, hu⟩ := (P.mem_reachable_iff P.recurrentState x).1 hxexact (P.mem_reachable_iff P.recurrentState y).2⟨u ++ [y], hu.append P ⟨hxy, rfl⟩⟩noncomputable abbrev RecurrentClass (P : FiniteKernel F) :={x : F // x ∈ P.reachable P.recurrentState}omit [DecidableEq F] intheorem probability_zero_outside {P : FiniteKernel F}(x : P.RecurrentClass) {y : F}(hy : y ∉ P.reachable P.recurrentState) : P.probability x.1 y = 0 := byapply le_antisymm· apply le_of_not_gtintro hposexact hy (reachable_closed_edge x.2 hpos)· exact P.probability_nonneg x.1 y/-- Restriction to the closed minimal reachable class. -/noncomputable def recurrentKernel (P : FiniteKernel F) :FiniteKernel P.RecurrentClass whereprobability x y := P.probability x.1 y.1probability_nonneg x y := P.probability_nonneg x.1 y.1probability_sum x := byclassicalrw [← P.probability_sum x.1]rw [← Finset.sum_subtype (P.reachable P.recurrentState)(fun y ↦ by simp) (fun y ↦ P.probability x.1 y)]apply Finset.sum_subset (by simp)intro y _ hyexact probability_zero_outside x hyomit [DecidableEq F] intheorem lift_edgePath_recurrent {P : FiniteKernel F} {x y : F} {w : List F}(hx : x ∈ P.reachable P.recurrentState)(hy : y ∈ P.reachable P.recurrentState) (hw : P.EdgePath x w y) :∃ v : List P.RecurrentClass,P.recurrentKernel.EdgePath ⟨x, hx⟩ v ⟨y, hy⟩ := byinduction w generalizing x with| nil =>subst xexact ⟨[], rfl⟩| cons z zs ih =>have hz := reachable_closed_edge hx hw.1obtain ⟨v, hv⟩ := ih hz hw.2refine ⟨⟨z, hz⟩ :: v, ?_⟩exact ⟨hw.1, hv⟩noncomputable def recurrentClassBase (P : FiniteKernel F) : P.RecurrentClass :=⟨P.recurrentState, P.self_mem_reachable P.recurrentState⟩omit [DecidableEq F] intheorem recurrentKernel_reaches_base (P : FiniteKernel F)(x : P.RecurrentClass) :P.recurrentClassBase ∈ P.recurrentKernel.reachable x := byhave hs := P.recurrentState_reachable_from x.2obtain ⟨w, hw⟩ := (P.mem_reachable_iff x.1 P.recurrentState).1 hsobtain ⟨v, hv⟩ := lift_edgePath_recurrent x.2(P.self_mem_reachable P.recurrentState) hwexact (P.recurrentKernel.mem_reachable_iff x P.recurrentClassBase).2 ⟨v, hv⟩/-! ### A uniform hitting estimate in a finite communicating class -/noncomputable def toChain (P : FiniteKernel F) : FiniteChoiceChain F F wherenext _ y := yprobability := P.probabilityprobability_nonneg := P.probability_nonnegprobability_sum := P.probability_sumdef pathProbability (P : FiniteKernel F) : F → List F → ℝ| _, [] => 1| x, y :: ys => P.probability x y * P.pathProbability y ysomit [DecidableEq F] [Nonempty F] intheorem probability_le_one (P : FiniteKernel F) (x y : F) :P.probability x y ≤ 1 := byrw [← P.probability_sum x]exact Finset.single_le_sum (fun z _ ↦ P.probability_nonneg x z) (Finset.mem_univ y)omit [DecidableEq F] [Nonempty F] intheorem pathProbability_nonneg (P : FiniteKernel F) (x : F) (w : List F) :0 ≤ P.pathProbability x w := byinduction w generalizing x with| nil => simp [pathProbability]| cons y ys ih =>exact mul_nonneg (P.probability_nonneg x y) (ih y)omit [DecidableEq F] [Nonempty F] intheorem pathProbability_le_one (P : FiniteKernel F) (x : F) (w : List F) :P.pathProbability x w ≤ 1 := byinduction w generalizing x with| nil => simp [pathProbability]| cons y ys ih =>simp only [pathProbability]nlinarith [P.probability_nonneg x y, P.probability_le_one x y,P.pathProbability_nonneg y ys, ih y]omit [DecidableEq F] [Nonempty F] intheorem pathProbability_pos {P : FiniteKernel F} {x y : F} {w : List F}(hw : P.EdgePath x w y) : 0 < P.pathProbability x w := byinduction w generalizing x with| nil => simp [pathProbability]| cons z zs ih =>exact mul_pos hw.1 (ih hw.2)omit [Nonempty F] intheorem survival_antitone (P : FiniteKernel F) (C : Finset F) (x : F) :Antitone fun n ↦ P.toChain.survival C n x := byapply antitone_nat_of_succ_leintro nhave htail := P.toChain.hitTotal_succ_add_survival_succ C n xlinarith [P.toChain.hitTotal_nonneg C (n + 1) x]omit [Nonempty F] intheorem pathProbability_le_hitBy {P : FiniteKernel F} {x s : F} {w : List F}(hw : P.EdgePath x w s) :P.pathProbability x w ≤1 - P.toChain.survival {s} w.length x := byinduction w generalizing x with| nil =>subst xsimp [pathProbability]| cons y ys ih =>by_cases hxs : x = s· subst xrw [P.toChain.survival_of_mem {s} (y :: ys).length(Finset.mem_singleton_self s)]simp only [sub_zero]exact P.pathProbability_le_one s (y :: ys)· have hxmem : x ∉ ({s} : Finset F) := by simpahave hrest := ih hw.2have hhit0 (z : F) :0 ≤ 1 - P.toChain.survival {s} ys.length z := bylinarith [P.toChain.survival_le_one {s} ys.length z]have hone :1 - P.toChain.survival {s} (y :: ys).length x =∑ z, P.probability x z *(1 - P.toChain.survival {s} ys.length z) := byrw [List.length_cons,P.toChain.survival_succ_of_not_mem {s} ys.length hxmem]calc1 - ∑ z, P.toChain.probability x z *P.toChain.survival {s} ys.length (P.toChain.next x z) =(∑ z, P.probability x z) -∑ z, P.toChain.probability x z *P.toChain.survival {s} ys.length (P.toChain.next x z) := byrw [P.probability_sum]_ = ∑ z, (P.probability x z -P.toChain.probability x z *P.toChain.survival {s} ys.length (P.toChain.next x z)) := byrw [Finset.sum_sub_distrib]_ = ∑ z, P.probability x z *(1 - P.toChain.survival {s} ys.length z) := byapply Finset.sum_congr rflintro z _simp only [toChain]ringrw [hone]calcP.pathProbability x (y :: ys) =P.probability x y * P.pathProbability y ys := rfl_ ≤ P.probability x y *(1 - P.toChain.survival {s} ys.length y) :=mul_le_mul_of_nonneg_left hrest (P.probability_nonneg x y)_ ≤ ∑ z, P.probability x z *(1 - P.toChain.survival {s} ys.length z) :=Finset.single_le_sum (fun z _ ↦mul_nonneg (P.probability_nonneg x z) (hhit0 z)) (Finset.mem_univ y)noncomputable def route (P : FiniteKernel F) (s : F)(hreach : ∀ x, s ∈ P.reachable x) (x : F) : List F :=Classical.choose ((P.mem_reachable_iff x s).1 (hreach x))omit [DecidableEq F] [Nonempty F] intheorem route_spec (P : FiniteKernel F) (s : F)(hreach : ∀ x, s ∈ P.reachable x) (x : F) :P.EdgePath x (P.route s hreach x) s :=Classical.choose_spec ((P.mem_reachable_iff x s).1 (hreach x))noncomputable def routeLength (P : FiniteKernel F) (s : F)(hreach : ∀ x, s ∈ P.reachable x) : ℕ :=1 + ∑ x, (P.route s hreach x).lengthnoncomputable def routeMinorization (P : FiniteKernel F) (s : F)(hreach : ∀ x, s ∈ P.reachable x) : ℝ :=∏ x, P.pathProbability x (P.route s hreach x)omit [DecidableEq F] [Nonempty F] intheorem routeLength_pos (P : FiniteKernel F) (s : F)(hreach : ∀ x, s ∈ P.reachable x) : 0 < P.routeLength s hreach := bysimp [routeLength]omit [DecidableEq F] [Nonempty F] intheorem route_length_lt (P : FiniteKernel F) (s : F)(hreach : ∀ x, s ∈ P.reachable x) (x : F) :(P.route s hreach x).length < P.routeLength s hreach := bydsimp only [routeLength]have hle : (P.route s hreach x).length ≤∑ y, (P.route s hreach y).length :=Finset.single_le_sum(fun y _ ↦ Nat.zero_le (P.route s hreach y).length) (Finset.mem_univ x)omegaomit [DecidableEq F] [Nonempty F] intheorem routeMinorization_pos (P : FiniteKernel F) (s : F)(hreach : ∀ x, s ∈ P.reachable x) :0 < P.routeMinorization s hreach := byapply Finset.prod_posintro x _exact pathProbability_pos (P.route_spec s hreach x)omit [DecidableEq F] [Nonempty F] intheorem routeMinorization_le_route (P : FiniteKernel F) (s : F)(hreach : ∀ x, s ∈ P.reachable x) (x : F) :P.routeMinorization s hreach ≤P.pathProbability x (P.route s hreach x) := byclassicalrw [routeMinorization, ← Finset.mul_prod_erase Finset.univ _ (Finset.mem_univ x)]nth_rewrite 2 [← mul_one (P.pathProbability x (P.route s hreach x))]apply mul_le_mul_of_nonneg_left· exact Finset.prod_le_one (fun y _ ↦ P.pathProbability_nonneg y _)(fun y _ ↦ P.pathProbability_le_one y _)· exact P.pathProbability_nonneg x _omit [Nonempty F] intheorem uniform_survival_bound (P : FiniteKernel F) (s : F)(hreach : ∀ x, s ∈ P.reachable x) (x : F) :P.toChain.survival {s} (P.routeLength s hreach) x ≤1 - P.routeMinorization s hreach := byhave hpath := P.pathProbability_le_hitBy (P.route_spec s hreach x)have hmono := P.survival_antitone {s} x(Nat.le_of_lt (P.route_length_lt s hreach x))have hminor := P.routeMinorization_le_route s hreach xlinarithomit [Nonempty F] intheorem survival_add_le (P : FiniteKernel F) (C : Finset F) (c : ℝ) {n : ℕ}(hn : ∀ y, P.toChain.survival C n y ≤ c) (m : ℕ) (x : F) :P.toChain.survival C (m + n) x ≤c * P.toChain.survival C m x := byinduction m generalizing x with| zero =>by_cases hx : x ∈ C· simp [P.toChain.survival_of_mem C n hx,P.toChain.survival_zero_of_mem C hx]· simpa [P.toChain.survival_zero_of_not_mem C hx] using hn x| succ m ih =>by_cases hx : x ∈ C· simp [P.toChain.survival_of_mem C (m + 1 + n) hx,P.toChain.survival_of_mem C (m + 1) hx]· rw [show m + 1 + n = (m + n) + 1 by omega,P.toChain.survival_succ_of_not_mem C (m + n) hx,P.toChain.survival_succ_of_not_mem C m hx]calc∑ y, P.probability x y * P.toChain.survival C (m + n) y ≤∑ y, P.probability x y *(c * P.toChain.survival C m y) := byapply Finset.sum_le_sumintro y _exact mul_le_mul_of_nonneg_left (ih y) (P.probability_nonneg x y)_ = c * ∑ y, P.probability x y *P.toChain.survival C m y := byrw [Finset.mul_sum]apply Finset.sum_congr rflintro y _ringomit [DecidableEq F] [Nonempty F] intheorem minorization_le_one (P : FiniteKernel F) (s : F)(hreach : ∀ x, s ∈ P.reachable x) :P.routeMinorization s hreach ≤ 1 := byclassicalexact (P.routeMinorization_le_route s hreach s).trans(P.pathProbability_le_one s (P.route s hreach s))omit [Nonempty F] intheorem survival_blocks (P : FiniteKernel F) (s : F)(hreach : ∀ x, s ∈ P.reachable x) (k : ℕ) (x : F) :P.toChain.survival {s} (P.routeLength s hreach * k) x ≤(1 - P.routeMinorization s hreach) ^ k := bylet r := 1 - P.routeMinorization s hreachhave hr0 : 0 ≤ r := sub_nonneg.mpr (P.minorization_le_one s hreach)induction k with| zero => exact P.toChain.survival_le_one {s} 0 x| succ k ih =>have hcontract := P.survival_add_le {s} r(P.uniform_survival_bound s hreach) (P.routeLength s hreach * k) xrw [Nat.mul_succ]calcP.toChain.survival {s}(P.routeLength s hreach * k + P.routeLength s hreach) x ≤r * P.toChain.survival {s} (P.routeLength s hreach * k) x := hcontract_ ≤ r * r ^ k := mul_le_mul_of_nonneg_left ih hr0_ = r ^ (k + 1) := by rw [pow_succ']omit [Nonempty F] intheorem returnSurvival_antitone (P : FiniteKernel F) (C : Finset F) (x : F) :Antitone fun n ↦ P.toChain.returnSurvival C n x := byapply antitone_nat_of_succ_leintro nhave htail := P.toChain.return_hit_add_survival C n xlinarith [P.toChain.returnHitTotal_nonneg C (n + 1) x]omit [Nonempty F] intheorem returnSurvival_blocks (P : FiniteKernel F) (s : F)(hreach : ∀ x, s ∈ P.reachable x) (k : ℕ) :P.toChain.returnSurvival {s} (P.routeLength s hreach * k + 1) s ≤(1 - P.routeMinorization s hreach) ^ k := bysimp only [FiniteChoiceChain.returnSurvival]calc∑ y, P.probability s y *P.toChain.survival {s} (P.routeLength s hreach * k) y ≤∑ y, P.probability s y *((1 - P.routeMinorization s hreach) ^ k) := byapply Finset.sum_le_sumintro y _exact mul_le_mul_of_nonneg_left (P.survival_blocks s hreach k y)(P.probability_nonneg s y)_ = (1 - P.routeMinorization s hreach) ^ k := byrw [← Finset.sum_mul, P.probability_sum, one_mul]theorem summable_pow_nat_div {L : ℕ} (hL : 0 < L) {r : ℝ}(hr0 : 0 ≤ r) (hr1 : r < 1) :Summable fun n : ℕ ↦ r ^ (n / L) := bylet : NeZero L := ⟨hL.ne'⟩have hgeom : Summable fun n : ℕ ↦ r ^ n :=summable_geometric_of_lt_one hr0 hr1have hprod : Summable fun p : ℕ × Fin L ↦ r ^ p.1 := byrw [summable_prod_of_nonneg (fun p ↦ pow_nonneg hr0 p.1)]constructor· intro nexact (hasSum_fintype (fun _ : Fin L ↦ r ^ n)).summable· simpa only [tsum_fintype, Finset.sum_const, nsmul_eq_mul,Finset.card_univ, Fintype.card_fin, mul_comm] usinghgeom.mul_left (L : ℝ)have hcomp := ((Nat.divModEquiv L).summable_iff).2 hprodsimpa [Function.comp_def, Nat.divModEquiv] using hcompomit [Nonempty F] intheorem returnSurvival_summable (P : FiniteKernel F) (s : F)(hreach : ∀ x, s ∈ P.reachable x) :Summable fun n ↦ P.toChain.returnSurvival {s} n s := bylet L := P.routeLength s hreachlet r := 1 - P.routeMinorization s hreachhave hL : 0 < L := P.routeLength_pos s hreachhave hr0 : 0 ≤ r := sub_nonneg.mpr (P.minorization_le_one s hreach)have hr1 : r < 1 := bydsimp only [r]linarith [P.routeMinorization_pos s hreach]have hbound (n : ℕ) : P.toChain.returnSurvival {s} (n + 1) s ≤r ^ (n / L) := byhave hindex : L * (n / L) + 1 ≤ n + 1 := byapply Nat.add_le_add_rightsimpa [Nat.mul_comm] using Nat.div_mul_le_self n Lexact (P.returnSurvival_antitone {s} s hindex).trans(P.returnSurvival_blocks s hreach (n / L))have hshift : Summable fun n ↦ P.toChain.returnSurvival {s} (n + 1) s := byapply Summable.of_nonneg_of_le(fun n ↦ P.toChain.returnSurvival_nonneg {s} (n + 1) s) hboundexact summable_pow_nat_div hL hr0 hr1exact (summable_nat_add_iff 1).1 (by simpa [Nat.add_comm] using hshift)omit [Nonempty F] intheorem returnTruncated_eq_sum_survival (P : FiniteKernel F)(C : Finset F) (N : ℕ) (x : F) :P.toChain.returnTruncated C N x =∑ n ∈ Finset.range N, P.toChain.returnSurvival C n x := byinduction N with| zero => simp [FiniteChoiceChain.returnTruncated]| succ N ih =>rw [P.toChain.returnTruncated_succ_eq_add_survival C N x, ih,Finset.sum_range_succ]omit [Nonempty F] intheorem returnHitTotal_hasSum_one_finite (P : FiniteKernel F) (s : F)(hreach : ∀ x, s ∈ P.reachable x) :HasSum (fun n ↦ P.toChain.returnHitTotal {s} n s) 1 := byrw [hasSum_iff_tendsto_nat_of_nonneg(fun n ↦ P.toChain.returnHitTotal_nonneg {s} n s) 1]have hzero : Filter.Tendsto (fun n ↦ P.toChain.returnSurvival {s} n s)Filter.atTop (nhds 0) :=(P.returnSurvival_summable s hreach).tendsto_atTop_zerohave hpartial (N : ℕ) :∑ n ∈ Finset.range (N + 1), P.toChain.returnHitTotal {s} n s =1 - P.toChain.returnSurvival {s} N s := byinduction N with| zero => simp [FiniteChoiceChain.returnHitTotal,FiniteChoiceChain.returnSurvival]| succ N ih =>rw [Finset.sum_range_succ, ih]have htail := P.toChain.return_hit_add_survival {s} N slinarithhave hlim : Filter.Tendsto(fun N ↦ ∑ n ∈ Finset.range (N + 1),P.toChain.returnHitTotal {s} n s) Filter.atTop (nhds 1) := byrw [show (fun N ↦ ∑ n ∈ Finset.range (N + 1),P.toChain.returnHitTotal {s} n s) =fun N ↦ 1 - P.toChain.returnSurvival {s} N s from funext hpartial]simpa using tendsto_const_nhds.sub hzeroexact (Filter.tendsto_add_atTop_iff_nat 1).mp (bysimpa [Nat.add_comm] using hlim)end FiniteKernel/-! ## A recurrent augmented state in the induced control-set chain -/abbrev ControlState := {x : AugmentedState // x ∈ controlSet}noncomputable instance : Nonempty ControlState :=⟨⟨originState, originState_mem_controlSet⟩⟩/-- The stochastic kernel obtained by observing successive positive returnsto the finite control set. -/noncomputable def inducedControlKernel : FiniteKernel ControlState whereprobability x y := controlledChain.inducedProbability controlSet x.1 y.1probability_nonneg x y :=controlledChain.inducedProbability_nonneg controlSet x.1 y.1probability_sum x := byrw [show (∑ y : ControlState,controlledChain.inducedProbability controlSet x.1 y.1) =∑ y ∈ controlSet,controlledChain.inducedProbability controlSet x.1 y byexact sum_finset_subtype controlSet(fun y ↦ controlledChain.inducedProbability controlSet x.1 y)]exact controlledChain.inducedProbability_row_sum controlSet controlledLyapunovcontrolledLyapunov_nonneg controlled_drift x.1abbrev RecurrentControlClass := inducedControlKernel.RecurrentClass/-- The base point of the minimal closed communicating class. -/noncomputable def recurrentControlState : RecurrentControlClass :=inducedControlKernel.recurrentClassBasenoncomputable def recurrentAugmentedState : AugmentedState :=recurrentControlState.1.1theorem recurrentAugmentedState_mem : recurrentAugmentedState ∈ controlSet :=recurrentControlState.1.2theorem recurrentClass_reaches_base (x : RecurrentControlClass) :recurrentControlState ∈ inducedControlKernel.recurrentKernel.reachable x :=inducedControlKernel.recurrentKernel_reaches_base xtheorem recurrent_visit_return_total :HasSum (fun n ↦inducedControlKernel.recurrentKernel.toChain.returnHitTotal{recurrentControlState} n recurrentControlState) 1 :=inducedControlKernel.recurrentKernel.returnHitTotal_hasSum_one_finiterecurrentControlState recurrentClass_reaches_base/-! ## Lifting recurrence through control-set excursion durations -/noncomputable sectionabbrev EmbeddedKernel := inducedControlKernel.recurrentKernelabbrev EmbeddedChain := EmbeddedKernel.toChainabbrev EmbeddedState := RecurrentControlClass/-- Joint mass of an excursion duration and its next embedded state. -/noncomputable def holdingMass (x y : EmbeddedState) (n : ℕ) : ℝ :=controlledChain.returnAt controlSet y.1.1 n x.1.1theorem holdingMass_nonneg (x y : EmbeddedState) (n : ℕ) :0 ≤ holdingMass x y n :=controlledChain.returnAt_nonneg controlSet y.1.1 n x.1.1theorem holdingMass_summable (x y : EmbeddedState) :Summable fun n ↦ holdingMass x y n :=controlled_returnAt_summable y.1.2 x.1.1theorem tsum_holdingMass (x y : EmbeddedState) :(∑' n, holdingMass x y n) = EmbeddedKernel.probability x y := byrfltheorem weighted_holdingMass_summable (x y : EmbeddedState) :Summable fun n : ℕ ↦ (n : ℝ) * holdingMass x y n :=controlled_weighted_returnAt_summable y.1.2 x.1.1theorem sum_holdingMean_le (x : EmbeddedState) :(∑ y, ∑' n : ℕ, (n : ℝ) * holdingMass x y n) ≤excursionMeanBound := bylet g : ControlState → ℝ := fun y ↦∑' n : ℕ, (n : ℝ) *controlledChain.returnAt controlSet y.1 n x.1.1have hsub₁ : (∑ y, ∑' n : ℕ, (n : ℝ) * holdingMass x y n) ≤∑ y : ControlState, g y := byrw [show (∑ y : EmbeddedState,∑' n : ℕ, (n : ℝ) * holdingMass x y n) =∑ y ∈ inducedControlKernel.reachable inducedControlKernel.recurrentState,g y bysimpa [holdingMass, g] using(sum_finset_subtype(inducedControlKernel.reachable inducedControlKernel.recurrentState)g)]apply Finset.sum_le_sum_of_subset_of_nonneg (Finset.subset_univ _)intro y _ _exact tsum_nonneg fun n ↦ mul_nonneg (Nat.cast_nonneg n)(controlledChain.returnAt_nonneg controlSet y.1 n x.1.1)have hall : (∑ y : ControlState, g y) =∑ y ∈ controlSet,∑' n : ℕ, (n : ℝ) *controlledChain.returnAt controlSet y n x.1.1 := bysimpa [g] using (sum_finset_subtype controlSet fun y ↦∑' n : ℕ, (n : ℝ) *controlledChain.returnAt controlSet y n x.1.1)calc_ ≤ ∑ y : ControlState, g y := hsub₁_ ≤ ∑ y ∈ controlSet,∑' n : ℕ, (n : ℝ) *controlledChain.returnAt controlSet y n x.1.1 := hall.le_ = ∑' n : ℕ, (n : ℝ) *controlledChain.returnHitTotal controlSet n x.1.1 :=sum_weighted_returnAt x.1.1_ ≤ excursionMeanBound := returnMean_le_excursionMeanBound x.1.2/-- First hit of the base state after exactly `k` embedded visits and totalunderlying block duration `n`. -/noncomputable def holdingHit : ℕ → ℕ → EmbeddedState → ℝ| 0, n, x => if n = 0 ∧ x = recurrentControlState then 1 else 0| k + 1, n, x =>if x = recurrentControlState then 0 else∑ y, convolution (holdingMass x y) (fun b ↦ holdingHit k b y) n/-- Positive return of the base state after exactly `k` embedded visits. -/noncomputable def holdingReturn : ℕ → ℕ → ℝ| 0, _ => 0| k + 1, n =>∑ y, convolution (holdingMass recurrentControlState y)(fun b ↦ holdingHit k b y) ntheorem holdingHit_nonneg (k n : ℕ) (x : EmbeddedState) :0 ≤ holdingHit k n x := byinduction k generalizing n x with| zero =>simp only [holdingHit]split_ifs <;> norm_num| succ k ih =>simp only [holdingHit]split_ifs· norm_num· exact Finset.sum_nonneg fun y _ ↦convolution_nonneg (holdingMass_nonneg x y) (fun b ↦ ih b y) ntheorem holdingReturn_nonneg (k n : ℕ) : 0 ≤ holdingReturn k n := bycases k with| zero => simp [holdingReturn]| succ k =>simp only [holdingReturn]exact Finset.sum_nonneg fun y _ ↦ convolution_nonneg(holdingMass_nonneg recurrentControlState y)(fun b ↦ holdingHit_nonneg k b y) ntheorem holdingHit_summable_and_mass (k : ℕ) (x : EmbeddedState) :Summable (fun n ↦ holdingHit k n x) ∧(∑' n, holdingHit k n x) =EmbeddedChain.hitAt {recurrentControlState} recurrentControlState k x := byinduction k generalizing x with| zero =>by_cases hx : x = recurrentControlState· subst xhave hs : HasSum (fun n : ℕ ↦ if n = 0 then (1 : ℝ) else 0) 1 :=hasSum_ite_eq 0 1constructor· simpa [holdingHit] using hs.summable· rw [show (fun n ↦ holdingHit 0 n recurrentControlState) =fun n ↦ if n = 0 then (1 : ℝ) else 0 byfunext nsimp [holdingHit]]rw [hs.tsum_eq]simp [FiniteChoiceChain.hitAt]· have hs : HasSum (fun _ : ℕ ↦ (0 : ℝ)) 0 := hasSum_zeroconstructor· simp [holdingHit, hx]· rw [show (fun n ↦ holdingHit 0 n x) = fun _ ↦ (0 : ℝ) byfunext nsimp [holdingHit, hx]]rw [tsum_zero]simp [FiniteChoiceChain.hitAt, hx]| succ k ih =>by_cases hx : x = recurrentControlState· subst xconstructor· have hz : (fun n ↦ holdingHit (k + 1) n recurrentControlState) =fun _ ↦ (0 : ℝ) := byfunext nsimp [holdingHit]rw [hz]exact summable_zero· simp [holdingHit, FiniteChoiceChain.hitAt]· have hconv (y : EmbeddedState) : Summable(convolution (holdingMass x y) (fun b ↦ holdingHit k b y)) :=convolution_summable (holdingMass_nonneg x y)(fun b ↦ holdingHit_nonneg k b y) (holdingMass_summable x y) (ih y).1have hsum : Summable (fun n ↦ holdingHit (k + 1) n x) := bysimp only [holdingHit, hx, if_false]exact summable_finset_sum Finset.univ(fun y n ↦ convolution (holdingMass x y)(fun b ↦ holdingHit k b y) n) (by simpa using hconv)refine ⟨hsum, ?_⟩simp only [holdingHit, hx, if_false]rw [Summable.tsum_finsetSum (fun y _ ↦ hconv y)]have hxmem : x ∉ ({recurrentControlState} : Finset EmbeddedState) := bysimpa using hxsimp only [FiniteChoiceChain.hitAt, hxmem, if_false, EmbeddedChain,FiniteKernel.toChain]apply Finset.sum_congr rflintro y _rw [tsum_convolution (holdingMass_nonneg x y)(fun b ↦ holdingHit_nonneg k b y) (holdingMass_summable x y) (ih y).1,tsum_holdingMass, (ih y).2]rfltheorem holdingReturn_summable_and_mass (k : ℕ) :Summable (fun n ↦ holdingReturn k n) ∧(∑' n, holdingReturn k n) =EmbeddedChain.returnAt {recurrentControlState} recurrentControlState krecurrentControlState := bycases k with| zero =>constructor· simp [holdingReturn]· simp [holdingReturn, FiniteChoiceChain.returnAt]| succ k =>have hconv (y : EmbeddedState) : Summable(convolution (holdingMass recurrentControlState y)(fun b ↦ holdingHit k b y)) :=convolution_summable (holdingMass_nonneg recurrentControlState y)(fun b ↦ holdingHit_nonneg k b y)(holdingMass_summable recurrentControlState y)(holdingHit_summable_and_mass k y).1have hsum : Summable (fun n ↦ holdingReturn (k + 1) n) := bysimp only [holdingReturn]exact summable_finset_sum Finset.univ(fun y n ↦ convolution (holdingMass recurrentControlState y)(fun b ↦ holdingHit k b y) n) (by simpa using hconv)refine ⟨hsum, ?_⟩simp only [holdingReturn]rw [Summable.tsum_finsetSum (fun y _ ↦ hconv y)]simp only [FiniteChoiceChain.returnAt, EmbeddedChain, FiniteKernel.toChain]apply Finset.sum_congr rflintro y _rw [tsum_convolution (holdingMass_nonneg recurrentControlState y)(fun b ↦ holdingHit_nonneg k b y)(holdingMass_summable recurrentControlState y)(holdingHit_summable_and_mass k y).1, tsum_holdingMass,(holdingHit_summable_and_mass k y).2]rfltheorem holdingHit_weighted_summable (k : ℕ) (x : EmbeddedState) :Summable fun n : ℕ ↦ (n : ℝ) * holdingHit k n x := byinduction k generalizing x with| zero =>have hz : (fun n : ℕ ↦ (n : ℝ) * holdingHit 0 n x) =fun _ ↦ (0 : ℝ) := byfunext nby_cases h : n = 0 ∧ x = recurrentControlState <;>simp [holdingHit, h]rw [hz]exact summable_zero| succ k ih =>by_cases hx : x = recurrentControlState· subst xhave hz : (fun n : ℕ ↦ (n : ℝ) *holdingHit (k + 1) n recurrentControlState) = fun _ ↦ (0 : ℝ) := byfunext nsimp [holdingHit]rw [hz]exact summable_zero· have hconv (y : EmbeddedState) : Summable fun n : ℕ ↦ (n : ℝ) *convolution (holdingMass x y) (fun b ↦ holdingHit k b y) n :=weighted_convolution_summable (holdingMass_nonneg x y)(fun b ↦ holdingHit_nonneg k b y) (holdingMass_summable x y)(holdingHit_summable_and_mass k y).1(weighted_holdingMass_summable x y) (ih y)have hfinite := summable_finset_sum Finset.univ(fun y n ↦ (n : ℝ) * convolution (holdingMass x y)(fun b ↦ holdingHit k b y) n) (by simpa using hconv)simpa only [holdingHit, hx, if_false, Finset.mul_sum] using hfinitetheorem holdingReturn_weighted_summable (k : ℕ) :Summable fun n : ℕ ↦ (n : ℝ) * holdingReturn k n := bycases k with| zero =>simp [holdingReturn]| succ k =>have hconv (y : EmbeddedState) : Summable fun n : ℕ ↦ (n : ℝ) *convolution (holdingMass recurrentControlState y)(fun b ↦ holdingHit k b y) n :=weighted_convolution_summable(holdingMass_nonneg recurrentControlState y)(fun b ↦ holdingHit_nonneg k b y)(holdingMass_summable recurrentControlState y)(holdingHit_summable_and_mass k y).1(weighted_holdingMass_summable recurrentControlState y)(holdingHit_weighted_summable k y)have hfinite := summable_finset_sum Finset.univ(fun y n ↦ (n : ℝ) * convolution(holdingMass recurrentControlState y)(fun b ↦ holdingHit k b y) n) (by simpa using hconv)simpa only [holdingReturn, Finset.mul_sum] using hfinitetheorem tsum_weighted_holdingHit_succ {k : ℕ} {x : EmbeddedState}(hx : x ≠ recurrentControlState) :(∑' n : ℕ, (n : ℝ) * holdingHit (k + 1) n x) =∑ y : EmbeddedState,((∑' n : ℕ, (n : ℝ) * holdingMass x y n) *EmbeddedChain.hitAt {recurrentControlState} recurrentControlState k y +EmbeddedKernel.probability x y *∑' n : ℕ, (n : ℝ) * holdingHit k n y) := byhave hconv (y : EmbeddedState) : Summable fun n : ℕ ↦ (n : ℝ) *convolution (holdingMass x y) (fun b ↦ holdingHit k b y) n :=weighted_convolution_summable (holdingMass_nonneg x y)(fun b ↦ holdingHit_nonneg k b y) (holdingMass_summable x y)(holdingHit_summable_and_mass k y).1 (weighted_holdingMass_summable x y)(holdingHit_weighted_summable k y)simp only [holdingHit, hx, if_false, Finset.mul_sum]rw [Summable.tsum_finsetSum (by simpa using hconv)]apply Finset.sum_congr rflintro y _rw [tsum_weighted_convolution (holdingMass_nonneg x y)(fun b ↦ holdingHit_nonneg k b y) (holdingMass_summable x y)(holdingHit_summable_and_mass k y).1 (weighted_holdingMass_summable x y)(holdingHit_weighted_summable k y), (holdingHit_summable_and_mass k y).2,tsum_holdingMass]theorem tsum_weighted_holdingReturn_succ (k : ℕ) :(∑' n : ℕ, (n : ℝ) * holdingReturn (k + 1) n) =∑ y : EmbeddedState,((∑' n : ℕ, (n : ℝ) * holdingMass recurrentControlState y n) *EmbeddedChain.hitAt {recurrentControlState} recurrentControlState k y +EmbeddedKernel.probability recurrentControlState y *∑' n : ℕ, (n : ℝ) * holdingHit k n y) := byhave hconv (y : EmbeddedState) : Summable fun n : ℕ ↦ (n : ℝ) *convolution (holdingMass recurrentControlState y)(fun b ↦ holdingHit k b y) n :=weighted_convolution_summable(holdingMass_nonneg recurrentControlState y)(fun b ↦ holdingHit_nonneg k b y)(holdingMass_summable recurrentControlState y)(holdingHit_summable_and_mass k y).1(weighted_holdingMass_summable recurrentControlState y)(holdingHit_weighted_summable k y)simp only [holdingReturn, Finset.mul_sum]rw [Summable.tsum_finsetSum (by simpa using hconv)]apply Finset.sum_congr rflintro y _rw [tsum_weighted_convolution (holdingMass_nonneg recurrentControlState y)(fun b ↦ holdingHit_nonneg k b y)(holdingMass_summable recurrentControlState y)(holdingHit_summable_and_mass k y).1(weighted_holdingMass_summable recurrentControlState y)(holdingHit_weighted_summable k y), (holdingHit_summable_and_mass k y).2,tsum_holdingMass]noncomputable def holdingHitMean (k : ℕ) (x : EmbeddedState) : ℝ :=∑' n : ℕ, (n : ℝ) * holdingHit k n xnoncomputable def holdingReturnMean (k : ℕ) : ℝ :=∑' n : ℕ, (n : ℝ) * holdingReturn k n@[simp] theorem holdingHitMean_zero (x : EmbeddedState) : holdingHitMean 0 x = 0 := byhave hz : (fun n : ℕ ↦ (n : ℝ) * holdingHit 0 n x) = fun _ ↦ (0 : ℝ) := byfunext nby_cases h : n = 0 ∧ x = recurrentControlState <;> simp [holdingHit, h]simp [holdingHitMean, hz]@[simp] theorem holdingHitMean_base (k : ℕ) :holdingHitMean k recurrentControlState = 0 := bycases k with| zero => simp| succ k =>have hz : (fun n : ℕ ↦ (n : ℝ) *holdingHit (k + 1) n recurrentControlState) = fun _ ↦ (0 : ℝ) := byfunext nsimp [holdingHit]simp [holdingHitMean, hz]@[simp] theorem holdingReturnMean_zero : holdingReturnMean 0 = 0 := bysimp [holdingReturnMean, holdingReturn]theorem embedded_hitAt_partial_le_one (N : ℕ) (x : EmbeddedState) :(∑ k ∈ Finset.range (N + 1),EmbeddedChain.hitAt {recurrentControlState} recurrentControlState k x) ≤ 1 := byhave heq (k : ℕ) :EmbeddedChain.hitAt {recurrentControlState} recurrentControlState k x =EmbeddedChain.hitTotal {recurrentControlState} k x := byrw [← EmbeddedChain.sum_hitAt_eq_hitTotal{recurrentControlState} k x]simpsimp_rw [heq]exact EmbeddedChain.sum_hitTotal_le_one {recurrentControlState} N xtheorem holdingHitMean_partial_le (N : ℕ) (x : EmbeddedState) :(∑ k ∈ Finset.range (N + 1), holdingHitMean k x) ≤excursionMeanBound *EmbeddedChain.truncatedHit {recurrentControlState} N x := byinduction N generalizing x with| zero => simp [FiniteChoiceChain.truncatedHit]| succ N ih =>by_cases hx : x = recurrentControlState· subst xsimp [FiniteChoiceChain.truncatedHit]· have hxmem : x ∉ ({recurrentControlState} : Finset EmbeddedState) := bysimpa using hxrw [Finset.sum_range_succ']simp only [holdingHitMean_zero, add_zero]have hsplit :(∑ k ∈ Finset.range (N + 1), holdingHitMean (k + 1) x) =∑ y : EmbeddedState,((∑' n : ℕ, (n : ℝ) * holdingMass x y n) *(∑ k ∈ Finset.range (N + 1),EmbeddedChain.hitAt {recurrentControlState}recurrentControlState k y) +EmbeddedKernel.probability x y *(∑ k ∈ Finset.range (N + 1), holdingHitMean k y)) := bysimp_rw [holdingHitMean, tsum_weighted_holdingHit_succ hx]rw [Finset.sum_comm]apply Finset.sum_congr rflintro y _rw [Finset.sum_add_distrib, ← Finset.mul_sum, ← Finset.mul_sum]rw [hsplit]calc_ ≤ ∑ y : EmbeddedState,((∑' n : ℕ, (n : ℝ) * holdingMass x y n) +EmbeddedKernel.probability x y *(excursionMeanBound *EmbeddedChain.truncatedHit {recurrentControlState} N y)) := byapply Finset.sum_le_sumintro y _apply add_le_add· exact mul_le_of_le_one_right(tsum_nonneg fun n ↦ mul_nonneg (Nat.cast_nonneg n)(holdingMass_nonneg x y n))(embedded_hitAt_partial_le_one N y)· exact mul_le_mul_of_nonneg_left (ih y)(EmbeddedKernel.probability_nonneg x y)_ = (∑ y : EmbeddedState,∑' n : ℕ, (n : ℝ) * holdingMass x y n) +excursionMeanBound *(∑ y : EmbeddedState, EmbeddedKernel.probability x y *EmbeddedChain.truncatedHit {recurrentControlState} N y) := byrw [Finset.sum_add_distrib]congr 1rw [Finset.mul_sum]apply Finset.sum_congr rflintro y _ring_ ≤ excursionMeanBound + excursionMeanBound *(∑ y : EmbeddedState, EmbeddedKernel.probability x y *EmbeddedChain.truncatedHit {recurrentControlState} N y) := byexact add_le_add_left (sum_holdingMean_le x) __ = excursionMeanBound *EmbeddedChain.truncatedHit {recurrentControlState} (N + 1) x := byrw [EmbeddedChain.truncatedHit_succ_of_not_mem{recurrentControlState} N hxmem]simp only [EmbeddedChain, FiniteKernel.toChain]ringtheorem holdingReturnMean_partial_le (N : ℕ) :(∑ k ∈ Finset.range (N + 1), holdingReturnMean k) ≤excursionMeanBound * EmbeddedChain.returnTruncated{recurrentControlState} N recurrentControlState := bycases N with| zero => simp [FiniteChoiceChain.returnTruncated]| succ N =>rw [Finset.sum_range_succ']simp only [holdingReturnMean_zero, add_zero]have hsplit :(∑ k ∈ Finset.range (N + 1), holdingReturnMean (k + 1)) =∑ y : EmbeddedState,((∑' n : ℕ, (n : ℝ) *holdingMass recurrentControlState y n) *(∑ k ∈ Finset.range (N + 1),EmbeddedChain.hitAt {recurrentControlState}recurrentControlState k y) +EmbeddedKernel.probability recurrentControlState y *(∑ k ∈ Finset.range (N + 1), holdingHitMean k y)) := bysimp_rw [holdingReturnMean, holdingHitMean,tsum_weighted_holdingReturn_succ]rw [Finset.sum_comm]apply Finset.sum_congr rflintro y _rw [Finset.sum_add_distrib, ← Finset.mul_sum, ← Finset.mul_sum]rw [hsplit]calc_ ≤ ∑ y : EmbeddedState,((∑' n : ℕ, (n : ℝ) *holdingMass recurrentControlState y n) +EmbeddedKernel.probability recurrentControlState y *(excursionMeanBound *EmbeddedChain.truncatedHit {recurrentControlState} N y)) := byapply Finset.sum_le_sumintro y _apply add_le_add· exact mul_le_of_le_one_right(tsum_nonneg fun n ↦ mul_nonneg (Nat.cast_nonneg n)(holdingMass_nonneg recurrentControlState y n))(embedded_hitAt_partial_le_one N y)· exact mul_le_mul_of_nonneg_left (holdingHitMean_partial_le N y)(EmbeddedKernel.probability_nonneg recurrentControlState y)_ = (∑ y : EmbeddedState,∑' n : ℕ, (n : ℝ) * holdingMass recurrentControlState y n) +excursionMeanBound *(∑ y : EmbeddedState,EmbeddedKernel.probability recurrentControlState y *EmbeddedChain.truncatedHit {recurrentControlState} N y) := byrw [Finset.sum_add_distrib]congr 1rw [Finset.mul_sum]apply Finset.sum_congr rflintro y _ring_ ≤ excursionMeanBound + excursionMeanBound *(∑ y : EmbeddedState,EmbeddedKernel.probability recurrentControlState y *EmbeddedChain.truncatedHit {recurrentControlState} N y) := byexact add_le_add_left (sum_holdingMean_le recurrentControlState) __ = excursionMeanBound * EmbeddedChain.returnTruncated{recurrentControlState} (N + 1) recurrentControlState := bysimp only [FiniteChoiceChain.returnTruncated, EmbeddedChain,FiniteKernel.toChain]ringnoncomputable def recurrentReturnDurationBound : ℝ :=excursionMeanBound *∑' k, EmbeddedChain.returnSurvival{recurrentControlState} k recurrentControlStatetheorem holdingReturnMean_summable : Summable holdingReturnMean := byapply summable_of_sum_range_le (c := recurrentReturnDurationBound)· intro kexact tsum_nonneg fun n ↦ mul_nonneg (Nat.cast_nonneg n)(holdingReturn_nonneg k n)· intro Ncases N with| zero =>simp only [Finset.range_zero, Finset.sum_empty]exact mul_nonneg excursionMeanBound_pos.le(tsum_nonneg fun k ↦ EmbeddedChain.returnSurvival_nonneg{recurrentControlState} k recurrentControlState)| succ N =>refine (holdingReturnMean_partial_le N).trans ?_apply mul_le_mul_of_nonneg_left _ excursionMeanBound_pos.lerw [EmbeddedKernel.returnTruncated_eq_sum_survival{recurrentControlState} N recurrentControlState]exact (EmbeddedKernel.returnSurvival_summable recurrentControlStaterecurrentClass_reaches_base).sum_le_tsum (Finset.range N)(fun k _ ↦ EmbeddedChain.returnSurvival_nonneg{recurrentControlState} k recurrentControlState)theorem embedded_returnAt_eq_returnHitTotal (k : ℕ) :EmbeddedChain.returnAt {recurrentControlState} recurrentControlState krecurrentControlState =EmbeddedChain.returnHitTotal {recurrentControlState} krecurrentControlState := byrw [← EmbeddedChain.sum_returnAt_eq_returnHitTotal{recurrentControlState} k recurrentControlState]simpend/-! ## Injectivity of the interval coding -/def inverseT (a : ℕ) (x : ℚ) : ℚ := (x + 1) / adef inverseWord : List ℕ → ℚ → ℚ| [], x => x| a :: w, x => inverseT a (inverseWord w x)def intervalMidpoint (i : Fin 19) : ℚ :=(endpoint (leftIndex i) + endpoint (rightIndex i)) / 2theorem intervalMidpoint_mem (i : Fin 19) : intervalMidpoint i ∈ K i := byhave h := width_pos isimp only [width] at hconstructor <;> simp only [intervalMidpoint] <;> linarith/-- Finite certificate saying that every allowed destination lies inside theimage interval of its source. -/theorem allowed_endpoint_bounds (α : Assignment) (i j : Fin 19)(h : allowedNext α i j) :T_rat (prescribed α i) (endpoint (leftIndex i)) ≤ endpoint (leftIndex j) ∧endpoint (rightIndex j) ≤T_rat (prescribed α i) (endpoint (rightIndex i)) := byobtain ⟨tr, htr, hrow⟩ := List.any_eq_true.mp hhave hrow' : tr.source = i ∧ tr.multiplier = prescribed α i ∧tr.first ≤ j ∧ j ≤ tr.last := of_decide_eq_true hrowrcases hrow' with ⟨hsource, hmultiplier, hfirst, hlast⟩have hexact := transitions_exact tr htrhave hleftIndex : leftIndex tr.first ≤ leftIndex j := bysimpa [leftIndex] using hfirsthave hrightIndex : rightIndex j ≤ rightIndex tr.last := bysimpa [rightIndex] using hlastconstructor· calcT_rat (prescribed α i) (endpoint (leftIndex i)) =T_rat tr.multiplier (endpoint (leftIndex tr.source)) := byrw [hsource, hmultiplier]_ = endpoint (leftIndex tr.first) := hexact.2.2.1_ ≤ endpoint (leftIndex j) := endpoint_strictMono.monotone hleftIndex· calcendpoint (rightIndex j) ≤ endpoint (rightIndex tr.last) :=endpoint_strictMono.monotone hrightIndex_ = T_rat tr.multiplier (endpoint (rightIndex tr.source)) :=hexact.2.2.2.symm_ = T_rat (prescribed α i) (endpoint (rightIndex i)) := byrw [hsource, hmultiplier]theorem inverseT_mem_of_allowed {α : Assignment} {i j : Fin 19}(h : allowedNext α i j) {x : ℚ} (hx : x ∈ K j) :inverseT (prescribed α i) x ∈ K i := byhave ha : (0 : ℚ) < prescribed α i := byexact_mod_cast (show 0 < prescribed α i byexact (prescribed_ge_two α i).trans' (by omega))have hb := allowed_endpoint_bounds α i j hsimp only [K, Set.mem_Ico] at hx ⊢simp only [T_rat] at hbconstructor· apply (le_div_iff₀ ha).2linarith [hb.1, hx.1]· apply (div_lt_iff₀ ha).2linarith [hb.2, hx.2]theorem mem_K_unique {i j : Fin 19} {x : ℚ}(hi : x ∈ K i) (hj : x ∈ K j) : i = j := byapply Fin.extby_contra hnercases lt_or_gt_of_ne hne with hij | hji· have hindex : rightIndex i ≤ leftIndex j := byapply Fin.mk_le_mk.mpromegahave hend := endpoint_strictMono.monotone hindexsimp only [K, Set.mem_Ico] at hi hjlinarith· have hindex : rightIndex j ≤ leftIndex i := byapply Fin.mk_le_mk.mpromegahave hend := endpoint_strictMono.monotone hindexsimp only [K, Set.mem_Ico] at hi hjlinarithnamespace IntervalPathdef destinations {i j : Fin 19} : IntervalPath i j → List (Fin 19)| .nil _ => []| @IntervalPath.cons _ j _ _ _ tail => j :: tail.destinationsdef append {i j k : Fin 19} : IntervalPath i j → IntervalPath j k →IntervalPath i k| .nil _, q => q| .cons α h tail, q => .cons α h (tail.append q)@[simp] theorem word_append {i j k : Fin 19} (p : IntervalPath i j)(q : IntervalPath j k) : (p.append q).word = p.word ++ q.word := byinduction p with| nil => rfl| cons α h tail ih => simp [append, ih]@[simp] theorem destinations_append {i j k : Fin 19} (p : IntervalPath i j)(q : IntervalPath j k) :(p.append q).destinations = p.destinations ++ q.destinations := byinduction p with| nil => rfl| cons α h tail ih => simp [append, destinations, ih]@[simp] theorem mass_append {i j k : Fin 19} (p : IntervalPath i j)(q : IntervalPath j k) : (p.append q).mass = p.mass * q.mass := byinduction p with| nil => simp [append, mass]| cons α h tail ih => simp [append, ih, mul_assoc]theorem inverseWord_mem_source {i j : Fin 19} (p : IntervalPath i j){x : ℚ} (hx : x ∈ K j) : inverseWord p.word x ∈ K i := byinduction p with| nil => simpa [word, inverseWord] using hx| @cons i j k α h tail ih =>simp only [word_cons, inverseWord]exact inverseT_mem_of_allowed h (ih hx)/-- For fixed endpoints, the multiplier word uniquely determines the entiresequence of interval destinations. Assignments themselves are deliberatelyignored here. -/theorem destinations_eq_of_word_eq {i k : Fin 19} (p q : IntervalPath i k)(hword : p.word = q.word) : p.destinations = q.destinations := byinduction p with| nil i =>cases q with| nil => rfl| cons α h tail => simp [word] at hword| @cons i j k α h tail ih =>cases q with| nil => simp [word] at hword| @cons _ j' _ β h' tail' =>have htail : tail.word = tail'.word := (List.cons.inj hword).2let z := inverseWord tail.word (intervalMidpoint k)have hzj : z ∈ K j := byexact tail.inverseWord_mem_source (intervalMidpoint_mem k)have hzj' : z ∈ K j' := bydsimp only [z]rw [htail]exact tail'.inverseWord_mem_source (intervalMidpoint_mem k)have hj : j = j' := mem_K_unique hzj hzj'subst j'simp only [destinations]rw [ih tail' htail]theorem destinations_eq_of_word_eq_of_endpoint_eq {i j k : Fin 19}(p : IntervalPath i j) (q : IntervalPath i k) (hend : j = k)(hword : p.word = q.word) : p.destinations = q.destinations := bycases hendexact destinations_eq_of_word_eq p q hwordend IntervalPath/-! ### Controlled blocks as typed interval paths -/theorem FiniteChoiceChain.pathWeight_nonneg {S Ω : Type*} [Fintype Ω](K : FiniteChoiceChain S Ω) (n : ℕ) (x : S)(w : Fin n → Ω) : 0 ≤ K.pathWeight n x w := byinduction n generalizing x with| zero => simp [FiniteChoiceChain.pathWeight]| succ n ih =>rw [show w = Fin.cons (w 0) (Fin.tail w) from (Fin.cons_self_tail w).symm,K.pathWeight_cons]exact mul_nonneg (K.probability_nonneg x (w 0))(ih (K.next x (w 0)) (Fin.tail w))noncomputable def blockIntervalPath (α : Assignment) :(n : ℕ) → (i : Fin 19) → (w : Fin n → Fin 19) →0 < blockWeight α n i w → IntervalPath i (blockEndpoint n i w)| 0, i, _, _ => .nil i| n + 1, i, w, hpos => byrw [show w = Fin.cons (w 0) (Fin.tail w) from(Fin.cons_self_tail w).symm, blockWeight_cons] at hposhave hp0 := probability_nonneg α i (w 0)have htail0 := blockWeight_nonneg α n (w 0) (Fin.tail w)have hp : 0 < probability α i (w 0) := by nlinarithhave htail : 0 < blockWeight α n (w 0) (Fin.tail w) := by nlinarithhave hallowed : allowedNext α i (w 0) := byby_contra hnotsimp [probability, hnot] at hpexact .cons α hallowed (blockIntervalPath α n (w 0) (Fin.tail w) htail)@[simp] theorem blockIntervalPath_word (α : Assignment) (n : ℕ)(i : Fin 19) (w : Fin n → Fin 19) (hpos : 0 < blockWeight α n i w) :(blockIntervalPath α n i w hpos).word = blockWord α n i w := byinduction n generalizing i with| zero => rfl| succ n ih =>simp only [blockIntervalPath, IntervalPath.word, blockWord]congr 1exact ih (w 0) (Fin.tail w) _@[simp] theorem blockIntervalPath_destinations (α : Assignment) (n : ℕ)(i : Fin 19) (w : Fin n → Fin 19) (hpos : 0 < blockWeight α n i w) :(blockIntervalPath α n i w hpos).destinations = List.ofFn w := byinduction n generalizing i with| zero => rfl| succ n ih =>simp only [blockIntervalPath, IntervalPath.destinations, List.ofFn_succ]congr 1exact ih (w 0) (Fin.tail w) _theorem inverseT_injective {a : ℕ} (ha : 0 < a) :Function.Injective (inverseT a) := byintro x y hsimp only [inverseT] at hhave haQ : (a : ℚ) ≠ 0 := by exact_mod_cast ha.ne'exact add_right_cancel ((div_left_inj' haQ).mp h)/-- Inside a block the assignment is fixed. Consequently, equality of twopulled-back points forces equality of every interval destination in theblock, even when the terminal points are initially different. -/theorem blockChoice_eq_of_inverse_eq (α : Assignment) (n : ℕ) (i : Fin 19){u v : Fin n → Fin 19} (hu : 0 < blockWeight α n i u)(hv : 0 < blockWeight α n i v) {xu xv : ℚ}(hxu : xu ∈ K (blockEndpoint n i u))(hxv : xv ∈ K (blockEndpoint n i v))(hinv : inverseWord (blockWord α n i u) xu =inverseWord (blockWord α n i v) xv) :u = v ∧ xu = xv := byinduction n generalizing i with| zero =>constructor· exact Subsingleton.elim u v· simpa [blockWord, inverseWord] using hinv| succ n ih =>rw [show u = Fin.cons (u 0) (Fin.tail u) from(Fin.cons_self_tail u).symm, blockWeight_cons] at hurw [show v = Fin.cons (v 0) (Fin.tail v) from(Fin.cons_self_tail v).symm, blockWeight_cons] at hvhave hpu0 := probability_nonneg α i (u 0)have hpv0 := probability_nonneg α i (v 0)have htu0 := blockWeight_nonneg α n (u 0) (Fin.tail u)have htv0 := blockWeight_nonneg α n (v 0) (Fin.tail v)have htu : 0 < blockWeight α n (u 0) (Fin.tail u) := by nlinarithhave htv : 0 < blockWeight α n (v 0) (Fin.tail v) := by nlinarithchange xu ∈ K (blockEndpoint n (u 0) (Fin.tail u)) at hxuchange xv ∈ K (blockEndpoint n (v 0) (Fin.tail v)) at hxvhave huInner : inverseWord (blockWord α n (u 0) (Fin.tail u)) xu ∈K (u 0) := bysimpa only [blockIntervalPath_word] using(blockIntervalPath α n (u 0) (Fin.tail u) htu).inverseWord_mem_source hxuhave hvInner : inverseWord (blockWord α n (v 0) (Fin.tail v)) xv ∈K (v 0) := bysimpa only [blockIntervalPath_word] using(blockIntervalPath α n (v 0) (Fin.tail v) htv).inverseWord_mem_source hxvhave hinner : inverseWord (blockWord α n (u 0) (Fin.tail u)) xu =inverseWord (blockWord α n (v 0) (Fin.tail v)) xv := byapply inverseT_injective (show 0 < prescribed α i byexact (prescribed_ge_two α i).trans' (by omega))simpa [blockWord, inverseWord] using hinvhave hfirst : u 0 = v 0 := mem_K_unique huInner (hinner ▸ hvInner)rw [← hfirst] at htv hxv hinnerhave htail := ih (u 0) htu htv hxu hxv hinnerconstructor· rw [← Fin.cons_self_tail u, ← Fin.cons_self_tail v]exact Fin.cons_inj.mpr ⟨hfirst, htail.1⟩· exact htail.2noncomputable def controlledPathWord :(n : ℕ) → AugmentedState → (Fin n → BlockChoice) → List ℕ| 0, _, _ => []| n + 1, x, w =>blockWord (feedback x.2) controlBlockLength x.1 (w 0) ++controlledPathWord n (controlledNext x (w 0)) (Fin.tail w)noncomputable def flattenBlockChoices :(n : ℕ) → (Fin n → BlockChoice) → List (Fin 19)| 0, _ => []| n + 1, w => List.ofFn (w 0) ++ flattenBlockChoices n (Fin.tail w)theorem flattenBlockChoices_injective (n : ℕ) :Function.Injective (flattenBlockChoices n) := byinduction n with| zero =>intro u v _exact Subsingleton.elim u v| succ n ih =>intro u v huvsimp only [flattenBlockChoices] at huvhave hhead : List.ofFn (u 0) = List.ofFn (v 0) :=List.append_inj_left huv (by simp)have hzero : u 0 = v 0 := List.ofFn_injective hheadhave htailList : flattenBlockChoices n (Fin.tail u) =flattenBlockChoices n (Fin.tail v) := byrw [hzero] at huvexact List.append_cancel_left huvhave htail : Fin.tail u = Fin.tail v := ih htailListrw [← Fin.cons_self_tail u, ← Fin.cons_self_tail v]exact Fin.cons_inj.mpr ⟨hzero, htail⟩noncomputable def controlledIntervalPath :(n : ℕ) → (x : AugmentedState) → (w : Fin n → BlockChoice) →0 < controlledChain.pathWeight n x w →IntervalPath x.1 (controlledChain.pathEndpoint n x w).1| 0, x, _, _ => .nil x.1| n + 1, x, w, hpos => byrw [show w = Fin.cons (w 0) (Fin.tail w) from(Fin.cons_self_tail w).symm, controlledChain.pathWeight_cons] at hposchange 0 < controlledChain.probability x (w 0) *controlledChain.pathWeight n (controlledNext x (w 0)) (Fin.tail w) at hposhave hp0 := controlledChain.probability_nonneg x (w 0)have ht0 := controlledChain.pathWeight_nonneg n(controlledNext x (w 0)) (Fin.tail w)have hp : 0 < controlledChain.probability x (w 0) := by nlinarithhave ht : 0 < controlledChain.pathWeight n(controlledNext x (w 0)) (Fin.tail w) := by nlinarithhave hpq : 0 < blockWeight (feedback x.2) controlBlockLength x.1 (w 0) := bychange 0 < (blockWeight (feedback x.2) controlBlockLength x.1 (w 0) : ℝ)at hpexact_mod_cast hpexact (blockIntervalPath (feedback x.2) controlBlockLength x.1 (w 0) hpq).append(controlledIntervalPath n (controlledNext x (w 0)) (Fin.tail w) ht)set_option backward.isDefEq.respectTransparency false in@[simp] theorem controlledIntervalPath_word (n : ℕ) (x : AugmentedState)(w : Fin n → BlockChoice) (hpos : 0 < controlledChain.pathWeight n x w) :(controlledIntervalPath n x w hpos).word = controlledPathWord n x w := byinduction n generalizing x with| zero => rfl| succ n ih =>rw [controlledIntervalPath, controlledPathWord]dsimp only [id]rw [IntervalPath.word_append, blockIntervalPath_word]exact congrArg(fun z ↦ blockWord (feedback x.2) controlBlockLength x.1 (w 0) ++ z)(ih (controlledNext x (w 0)) (Fin.tail w) _)set_option backward.isDefEq.respectTransparency false in@[simp] theorem controlledIntervalPath_destinations (n : ℕ)(x : AugmentedState) (w : Fin n → BlockChoice)(hpos : 0 < controlledChain.pathWeight n x w) :(controlledIntervalPath n x w hpos).destinations = flattenBlockChoices n w := byinduction n generalizing x with| zero => rfl| succ n ih =>rw [controlledIntervalPath, flattenBlockChoices]dsimp only [id]rw [IntervalPath.destinations_append, blockIntervalPath_destinations]exact congrArg(fun z ↦ List.ofFn (w 0) ++ z)(ih (controlledNext x (w 0)) (Fin.tail w) _)/-- Positive controlled paths with the same initial and final augmented stateare uniquely determined by their multiplier word. -/theorem controlledPathWord_injective_of_return (n : ℕ) (x : AugmentedState){u v : Fin n → BlockChoice}(hu : 0 < controlledChain.pathWeight n x u)(hv : 0 < controlledChain.pathWeight n x v)(huend : controlledChain.pathEndpoint n x u = x)(hvend : controlledChain.pathEndpoint n x v = x)(hword : controlledPathWord n x u = controlledPathWord n x v) :u = v := byhave hend : (controlledChain.pathEndpoint n x u).1 =(controlledChain.pathEndpoint n x v).1 := by rw [huend, hvend]let pu := controlledIntervalPath n x u hulet pv := controlledIntervalPath n x v hvhave hdest : pu.destinations = pv.destinations := byapply IntervalPath.destinations_eq_of_word_eq_of_endpoint_eq pu pv hendsimpa [pu, pv] using hwordapply flattenBlockChoices_injective nsimpa [pu, pv] using hdest/-! ### Algebra and probability of a positive controlled path -/theorem exponents_append (u v : List ℕ) (i : Fin 4) :exponents (u ++ v) i = exponents u i + exponents v i := bysimp [exponents]theorem imbalance_append (u v : List ℕ) :imbalance (u ++ v) = fun i ↦ imbalance u i + imbalance v i := byfunext ifin_cases i <;> simp [imbalance, exponents_append] <;> ringtheorem blockWord_mem_multipliers (α : Assignment) (n : ℕ) (i : Fin 19)(w : Fin n → Fin 19) : ∀ a ∈ blockWord α n i w, a ∈ multipliers := byinduction n generalizing i with| zero => simp [blockWord]| succ n ih =>rw [show w = Fin.cons (w 0) (Fin.tail w) from(Fin.cons_self_tail w).symm]simp only [blockWord_cons, List.mem_cons]intro a harcases ha with rfl | ha· exact prescribed_mem_multipliers α i· exact ih (w 0) (Fin.tail w) a hatheorem controlledPathWord_mem_multipliers (n : ℕ) (x : AugmentedState)(w : Fin n → BlockChoice) :∀ a ∈ controlledPathWord n x w, a ∈ multipliers := byinduction n generalizing x with| zero => simp [controlledPathWord]| succ n ih =>rw [show w = Fin.cons (w 0) (Fin.tail w) from(Fin.cons_self_tail w).symm]simp only [controlledPathWord, List.mem_append]intro a harcases ha with ha | ha· exact blockWord_mem_multipliers _ _ _ _ a ha· exact ih (controlledNext x (w 0)) (Fin.tail w) a ha@[simp] theorem blockWord_length (α : Assignment) (n : ℕ) (i : Fin 19)(w : Fin n → Fin 19) : (blockWord α n i w).length = n := byinduction n generalizing i with| zero => rfl| succ n ih =>simp [blockWord, ih]@[simp] theorem controlledPathWord_length (n : ℕ) (x : AugmentedState)(w : Fin n → BlockChoice) :(controlledPathWord n x w).length = controlBlockLength * n := byinduction n generalizing x with| zero => simp [controlledPathWord]| succ n ih =>rw [show w = Fin.cons (w 0) (Fin.tail w) from(Fin.cons_self_tail w).symm]simp only [controlledPathWord, List.length_append, blockWord_length, ih]ringtheorem controlledPathWord_imbalance (n : ℕ) (x : AugmentedState)(w : Fin n → BlockChoice) :imbalance (controlledPathWord n x w) = fun i ↦(controlledChain.pathEndpoint n x w).2 i - x.2 i := byinduction n generalizing x with| zero =>funext ifin_cases i <;> simp [controlledPathWord, FiniteChoiceChain.pathEndpoint,imbalance, exponents]| succ n ih =>rw [show w = Fin.cons (w 0) (Fin.tail w) from(Fin.cons_self_tail w).symm]rw [controlledPathWord, imbalance_append,← blockIncrement_eq_imbalance]rw [ih]simp only [FiniteChoiceChain.pathEndpoint_cons, controlledChain,controlledNext]funext isimp only [Fin.cons_zero, Fin.tail_cons]ring@[simp] theorem blockIntervalPath_mass (α : Assignment) (n : ℕ)(i : Fin 19) (w : Fin n → Fin 19) (hpos : 0 < blockWeight α n i w) :(blockIntervalPath α n i w hpos).mass = blockWeight α n i w := byinduction n generalizing i with| zero => rfl| succ n ih =>simp only [blockIntervalPath, blockWeight]exact congrArg(fun z ↦ probability α i (w 0) * z)(ih (w 0) (Fin.tail w) _)set_option backward.isDefEq.respectTransparency false in@[simp] theorem controlledIntervalPath_mass_real (n : ℕ) (x : AugmentedState)(w : Fin n → BlockChoice) (hpos : 0 < controlledChain.pathWeight n x w) :((controlledIntervalPath n x w hpos).mass : ℝ) =controlledChain.pathWeight n x w := byinduction n generalizing x with| zero =>norm_num [controlledIntervalPath, IntervalPath.mass,FiniteChoiceChain.pathWeight]| succ n ih =>rw [controlledIntervalPath]dsimp only [id]rw [IntervalPath.mass_append, blockIntervalPath_mass, Rat.cast_mul]simp only [controlledChain, controlledProbability,FiniteChoiceChain.pathWeight]exact congrArg(fun z ↦(blockWeight (feedback x.2) controlBlockLength x.1 (w 0) : ℝ) * z)(ih (controlledNext x (w 0)) (Fin.tail w) _)theorem controlledPathWeight_eq_reciprocal (n : ℕ) (x : AugmentedState)(w : Fin n → BlockChoice) (hpos : 0 < controlledChain.pathWeight n x w)(hend : controlledChain.pathEndpoint n x w = x) :controlledChain.pathWeight n x w =1 / (controlledPathWord n x w).prod := bylet p := controlledIntervalPath n x w hposhave hp : p.mass = 1 / (p.word.prod : ℚ) := byrw [p.mass_eq_width_div]have hwidth : width (controlledChain.pathEndpoint n x w).1 = width x.1 := byrw [hend]rw [hwidth]have hw : width x.1 ≠ 0 := ne_of_gt (width_pos x.1)field_simphave hpReal : (p.mass : ℝ) = 1 / (p.word.prod : ℝ) := byrw [hp]simp [Function.comp_def]simpa [p] using hpRealtheorem controlledPathWord_imbalance_zero (n : ℕ) (x : AugmentedState)(w : Fin n → BlockChoice) (hend : controlledChain.pathEndpoint n x w = x) :imbalance (controlledPathWord n x w) = 0 := byrw [controlledPathWord_imbalance, hend]funext isimp/-! ## Concrete first-return records -//-- A first return to the control set, viewed as an explicit tuple ofcontrolled block choices. -/abbrev ControlReturnSegment (n : ℕ) (x y : AugmentedState) :={w : Fin n → BlockChoice //controlledChain.IsReturnPath controlSet y n x w}noncomputable instance controlReturnSegmentFintype (n : ℕ)(x y : AugmentedState) : Fintype (ControlReturnSegment n x y) := byclassicalexact Fintype.ofInjective Subtype.val Subtype.val_injective/-- A concrete realization of a first hit of the recurrent embedded stateafter `k` visits to the control set and `n` original controlled blocks. -/def HoldingHitRecord : ℕ → ℕ → EmbeddedState → Type| 0, n, x => ULift {_unit : Unit // n = 0 ∧ x = recurrentControlState}| k + 1, n, x =>{_guard : Unit // x ≠ recurrentControlState} ×Σ p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n},Σ y : EmbeddedState,ControlReturnSegment p.1.1 x.1.1 y.1.1 ×HoldingHitRecord k p.1.2 y/-- A concrete positive first return to the recurrent embedded state. -/def RecurrentLoopRecord : ℕ → ℕ → Type| 0, _ => Empty| k + 1, n =>Σ p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n},Σ y : EmbeddedState,ControlReturnSegment p.1.1 recurrentAugmentedState y.1.1 ×HoldingHitRecord k p.1.2 ynoncomputable instance holdingHitRecordFintype (k n : ℕ) (x : EmbeddedState) :Fintype (HoldingHitRecord k n x) := byclassicalinduction k generalizing n x with| zero =>simp only [HoldingHitRecord]letI : Fintype {_unit : Unit // n = 0 ∧ x = recurrentControlState} :=Fintype.ofInjective Subtype.val Subtype.val_injectiveexact Fintype.ofEquiv _ Equiv.ulift.symm| succ k ih =>simp only [HoldingHitRecord]letI : Fintype {_guard : Unit // x ≠ recurrentControlState} :=Fintype.ofInjective Subtype.val Subtype.val_injectiveletI (p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n})(y : EmbeddedState) : Fintype (HoldingHitRecord k p.1.2 y) :=ih p.1.2 yletI (p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n})(y : EmbeddedState) :Fintype (ControlReturnSegment p.1.1 x.1.1 y.1.1) :=Fintype.ofInjective Subtype.val Subtype.val_injectiveinfer_instancenoncomputable instance recurrentLoopRecordFintype (k n : ℕ) :Fintype (RecurrentLoopRecord k n) := byclassicalcases k with| zero =>change Fintype Emptyexact Fintype.ofFinite Empty| succ k =>simp only [RecurrentLoopRecord]letI (p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n})(y : EmbeddedState) : Fintype (HoldingHitRecord k p.1.2 y) :=holdingHitRecordFintype k p.1.2 yletI (p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n})(y : EmbeddedState) :Fintype (ControlReturnSegment p.1.1 recurrentAugmentedState y.1.1) :=Fintype.ofInjective Subtype.val Subtype.val_injectiveinfer_instancenoncomputable def holdingHitRecordWeight :{k n : ℕ} → {x : EmbeddedState} → HoldingHitRecord k n x → ℝ| 0, _, _, _ => 1| _ + 1, _, x, ⟨_, p, _, w, r⟩ =>controlledChain.pathWeight p.1.1 x.1.1 w.1 * holdingHitRecordWeight rnoncomputable def recurrentLoopRecordWeight :{k n : ℕ} → RecurrentLoopRecord k n → ℝ| 0, _, r => r.elim| _ + 1, _, ⟨p, _, w, r⟩ =>controlledChain.pathWeight p.1.1 recurrentAugmentedState w.1 *holdingHitRecordWeight rnoncomputable def holdingHitRecordChoices :{k n : ℕ} → {x : EmbeddedState} → HoldingHitRecord k n x → List BlockChoice| 0, _, _, _ => []| _ + 1, _, _, ⟨_, _, _, w, r⟩ =>List.ofFn w.1 ++ holdingHitRecordChoices rnoncomputable def recurrentLoopRecordChoices :{k n : ℕ} → RecurrentLoopRecord k n → List BlockChoice| 0, _, r => r.elim| _ + 1, _, ⟨_, _, w, r⟩ =>List.ofFn w.1 ++ holdingHitRecordChoices r@[simp] theorem holdingHitRecordChoices_length {k n : ℕ} {x : EmbeddedState}(r : HoldingHitRecord k n x) :(holdingHitRecordChoices (k := k) (n := n) (x := x) r).length = n := byinduction k generalizing n x with| zero =>simp only [HoldingHitRecord] at rhave hn := r.down.2.1subst nrfl| succ k ih =>obtain ⟨guard, p, y, w, tail⟩ := rsimp only [holdingHitRecordChoices, List.length_append, List.length_ofFn,ih tail]exact Finset.HasAntidiagonal.mem_antidiagonal.mp p.2@[simp] theorem recurrentLoopRecordChoices_length {k n : ℕ}(r : RecurrentLoopRecord k n) :(recurrentLoopRecordChoices (k := k) (n := n) r).length = n := bycases k with| zero => exact r.elim| succ k =>obtain ⟨p, y, w, tail⟩ := rsimp only [recurrentLoopRecordChoices, List.length_append, List.length_ofFn,holdingHitRecordChoices_length]exact Finset.HasAntidiagonal.mem_antidiagonal.mp p.2theorem sum_controlReturnSegment_weight (n : ℕ) (x y : AugmentedState) :(∑ w : ControlReturnSegment n x y,controlledChain.pathWeight n x w.1) =controlledChain.returnAt controlSet y n x := byclassicalrw [sum_subtype_eq_selected]exact controlledChain.sum_returnPath_weight controlSet y n xtheorem sum_holdingHitRecordWeight (k n : ℕ) (x : EmbeddedState) :(∑ r : HoldingHitRecord k n x, holdingHitRecordWeight r) =holdingHit k n x := byclassicalinduction k generalizing n x with| zero =>by_cases h : n = 0 ∧ x = recurrentControlState· rcases h with ⟨rfl, rfl⟩let r0 : HoldingHitRecord 0 0 recurrentControlState :=ULift.up ⟨(), rfl, rfl⟩have huniv : (Finset.univ : Finset (HoldingHitRecord 0 0recurrentControlState)) = {r0} := byext rsimp only [Finset.mem_univ, true_iff, Finset.mem_singleton]rcases r with ⟨⟨u, hu⟩⟩cases urflrw [huniv]simp [holdingHitRecordWeight, holdingHit]· let : IsEmpty (HoldingHitRecord 0 n x) :=⟨fun r ↦ h r.down.2⟩rw [show holdingHit 0 n x = 0 by simp [holdingHit, h]]apply Finset.sum_eq_zerointro r _exact isEmptyElim r| succ k ih =>by_cases hx : x = recurrentControlState· subst xlet : IsEmpty (HoldingHitRecord (k + 1) n recurrentControlState) :=⟨fun r ↦ r.1.2 rfl⟩rw [show holdingHit (k + 1) n recurrentControlState = 0 bysimp [holdingHit]]apply Finset.sum_eq_zerointro r _exact isEmptyElim r· let : Fintype {_guard : Unit // x ≠ recurrentControlState} :=Fintype.ofInjective Subtype.val Subtype.val_injectivelet (p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n})(y : EmbeddedState) : Fintype (HoldingHitRecord k p.1.2 y) :=holdingHitRecordFintype k p.1.2 ylet (p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n})(y : EmbeddedState) :Fintype (ControlReturnSegment p.1.1 x.1.1 y.1.1) :=Fintype.ofInjective Subtype.val Subtype.val_injectivelet expandedFintype :Fintype ({_guard : Unit // x ≠ recurrentControlState} ×Σ p : {p : ℕ × ℕ //p ∈ Finset.HasAntidiagonal.antidiagonal n},Σ y : EmbeddedState,ControlReturnSegment p.1.1 x.1.1 y.1.1 ×HoldingHitRecord k p.1.2 y) :=inferInstancelet recordEquiv :HoldingHitRecord (k + 1) n x ≃({_guard : Unit // x ≠ recurrentControlState} ×Σ p : {p : ℕ × ℕ //p ∈ Finset.HasAntidiagonal.antidiagonal n},Σ y : EmbeddedState,ControlReturnSegment p.1.1 x.1.1 y.1.1 ×HoldingHitRecord k p.1.2 y) :=Equiv.refl _have hsum := @Fintype.sum_equiv(HoldingHitRecord (k + 1) n x)({_guard : Unit // x ≠ recurrentControlState} ×Σ p : {p : ℕ × ℕ //p ∈ Finset.HasAntidiagonal.antidiagonal n},Σ y : EmbeddedState,ControlReturnSegment p.1.1 x.1.1 y.1.1 ×HoldingHitRecord k p.1.2 y) ℝ(holdingHitRecordFintype (k + 1) n x) expandedFintypeinferInstance recordEquiv(fun r ↦ holdingHitRecordWeight r)(fun r ↦controlledChain.pathWeight r.2.1.1.1 x.1.1 r.2.2.2.1.1 *holdingHitRecordWeight r.2.2.2.2)(fun _ ↦ rfl)rw [hsum]rw [Fintype.sum_prod_type]simp_rw [Fintype.sum_sigma, Fintype.sum_prod_type]simp_rw [← Finset.mul_sum, ih]simp_rw [← Finset.sum_mul, sum_controlReturnSegment_weight]rw [show (∑ _guard : {_guard : Unit // x ≠ recurrentControlState},∑ p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n},∑ y : EmbeddedState,controlledChain.returnAt controlSet y.1.1 p.1.1 x.1.1 *holdingHit k p.1.2 y) =∑ p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n},∑ y : EmbeddedState,controlledChain.returnAt controlSet y.1.1 p.1.1 x.1.1 *holdingHit k p.1.2 y by simp [hx]]rw [sum_subtype_eq_sum_attach]rw [Finset.sum_comm]simp only [holdingHit, hx, if_false, convolution]apply Finset.sum_congr rflintro y _simpa only [holdingMass] usingFinset.sum_attach (Finset.HasAntidiagonal.antidiagonal n)(fun p ↦ controlledChain.returnAt controlSet y.1.1 p.1 x.1.1 *holdingHit k p.2 y)theorem sum_recurrentLoopRecordWeight (k n : ℕ) :(∑ r : RecurrentLoopRecord k n, recurrentLoopRecordWeight r) =holdingReturn k n := byclassicalcases k with| zero =>rw [show holdingReturn 0 n = 0 by simp [holdingReturn]]apply Finset.sum_eq_zerointro r _exact r.elim| succ k =>let (p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n})(y : EmbeddedState) : Fintype (HoldingHitRecord k p.1.2 y) :=holdingHitRecordFintype k p.1.2 ylet (p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n})(y : EmbeddedState) :Fintype (ControlReturnSegment p.1.1 recurrentAugmentedState y.1.1) :=Fintype.ofInjective Subtype.val Subtype.val_injectivelet expandedFintype :Fintype (Σ p : {p : ℕ × ℕ //p ∈ Finset.HasAntidiagonal.antidiagonal n},Σ y : EmbeddedState,ControlReturnSegment p.1.1 recurrentAugmentedState y.1.1 ×HoldingHitRecord k p.1.2 y) :=inferInstancelet recordEquiv :RecurrentLoopRecord (k + 1) n ≃(Σ p : {p : ℕ × ℕ //p ∈ Finset.HasAntidiagonal.antidiagonal n},Σ y : EmbeddedState,ControlReturnSegment p.1.1 recurrentAugmentedState y.1.1 ×HoldingHitRecord k p.1.2 y) :=Equiv.refl _have hsum := @Fintype.sum_equiv(RecurrentLoopRecord (k + 1) n)(Σ p : {p : ℕ × ℕ //p ∈ Finset.HasAntidiagonal.antidiagonal n},Σ y : EmbeddedState,ControlReturnSegment p.1.1 recurrentAugmentedState y.1.1 ×HoldingHitRecord k p.1.2 y) ℝ(recurrentLoopRecordFintype (k + 1) n) expandedFintypeinferInstance recordEquiv(fun r ↦ recurrentLoopRecordWeight r)(fun r ↦controlledChain.pathWeight r.1.1.1 recurrentAugmentedStater.2.2.1.1 *holdingHitRecordWeight r.2.2.2)(fun _ ↦ rfl)rw [hsum]rw [Fintype.sum_sigma]simp_rw [Fintype.sum_sigma, Fintype.sum_prod_type]simp_rw [← Finset.mul_sum, sum_holdingHitRecordWeight]simp_rw [← Finset.sum_mul, sum_controlReturnSegment_weight]rw [sum_subtype_eq_sum_attach, Finset.sum_comm]simp only [holdingReturn, convolution]apply Finset.sum_congr rflintro y _simpa only [holdingMass, recurrentAugmentedState] usingFinset.sum_attach (Finset.HasAntidiagonal.antidiagonal n)(fun p ↦ controlledChain.returnAt controlSet y.1.1 p.1recurrentAugmentedState * holdingHit k p.2 y)/-! ### Flat finite-choice lists -/namespace FiniteChoiceChaindef listEndpoint {S Ω : Type*} [Fintype Ω] (K : FiniteChoiceChain S Ω) :S → List Ω → S| x, [] => x| x, a :: w => K.listEndpoint (K.next x a) wdef listWeight {S Ω : Type*} [Fintype Ω] (K : FiniteChoiceChain S Ω) :S → List Ω → ℝ| _, [] => 1| x, a :: w => K.probability x a * K.listWeight (K.next x a) wtheorem listEndpoint_append {S Ω : Type*} [Fintype Ω](K : FiniteChoiceChain S Ω) (x : S) (u v : List Ω) :K.listEndpoint x (u ++ v) = K.listEndpoint (K.listEndpoint x u) v := byinduction u generalizing x with| nil => rfl| cons a u ih => exact ih (K.next x a)theorem listWeight_append {S Ω : Type*} [Fintype Ω](K : FiniteChoiceChain S Ω) (x : S) (u v : List Ω) :K.listWeight x (u ++ v) =K.listWeight x u * K.listWeight (K.listEndpoint x u) v := byinduction u generalizing x with| nil => simp [listWeight, listEndpoint]| cons a u ih =>simp only [List.cons_append, listWeight, listEndpoint, ih]ringtheorem listWeight_nonneg {S Ω : Type*} [Fintype Ω](K : FiniteChoiceChain S Ω) (x : S) (u : List Ω) :0 ≤ K.listWeight x u := byinduction u generalizing x with| nil => simp [listWeight]| cons a u ih =>exact mul_nonneg (K.probability_nonneg x a) (ih (K.next x a))def IsHitList {S Ω : Type*} [Fintype Ω] [DecidableEq S](K : FiniteChoiceChain S Ω) (C : Finset S) (y : S) : S → List Ω → Prop| x, [] => x ∈ C ∧ x = y| x, a :: w => x ∉ C ∧ K.IsHitList C y (K.next x a) wdef IsReturnList {S Ω : Type*} [Fintype Ω] [DecidableEq S](K : FiniteChoiceChain S Ω) (C : Finset S) (y : S) : S → List Ω → Prop| _, [] => False| x, a :: w => K.IsHitList C y (K.next x a) wtheorem listEndpoint_ofFn {S Ω : Type*} [Fintype Ω](K : FiniteChoiceChain S Ω) (n : ℕ) (x : S) (w : Fin n → Ω) :K.listEndpoint x (List.ofFn w) = K.pathEndpoint n x w := byinduction n generalizing x with| zero => rfl| succ n ih =>rw [List.ofFn_succ, listEndpoint, ih]have htail : Fin.tail w = fun i ↦ w i.succ := byfunext irflrw [← htail]exact (K.pathEndpoint_cons n x (w 0) (Fin.tail w)).symmtheorem listWeight_ofFn {S Ω : Type*} [Fintype Ω](K : FiniteChoiceChain S Ω) (n : ℕ) (x : S) (w : Fin n → Ω) :K.listWeight x (List.ofFn w) = K.pathWeight n x w := byinduction n generalizing x with| zero => rfl| succ n ih =>rw [List.ofFn_succ, listWeight, ih]have htail : Fin.tail w = fun i ↦ w i.succ := byfunext irflrw [← htail]exact (K.pathWeight_cons n x (w 0) (Fin.tail w)).symmtheorem isHitList_of_isHitPath {S Ω : Type*} [Fintype Ω] [DecidableEq S](K : FiniteChoiceChain S Ω) (C : Finset S) (y : S) (n : ℕ)(x : S) (w : Fin n → Ω) (h : K.IsHitPath C y n x w) :K.IsHitList C y x (List.ofFn w) := byinduction n generalizing x with| zero => simpa [IsHitList] using h| succ n ih =>rw [show w = Fin.cons (w 0) (Fin.tail w) from(Fin.cons_self_tail w).symm] at hrw [List.ofFn_succ]change x ∉ C ∧ K.IsHitList C y (K.next x (w 0))(List.ofFn (Fin.tail w))exact ⟨h.1, ih _ _ h.2⟩theorem isReturnList_of_isReturnPath {S Ω : Type*} [Fintype Ω][DecidableEq S] (K : FiniteChoiceChain S Ω) (C : Finset S) (y : S)(n : ℕ) (x : S) (w : Fin n → Ω) (h : K.IsReturnPath C y n x w) :K.IsReturnList C y x (List.ofFn w) := bycases n with| zero => exact (K.isReturnPath_zero C x y w h).elim| succ n =>rw [show w = Fin.cons (w 0) (Fin.tail w) from(Fin.cons_self_tail w).symm] at hrw [List.ofFn_succ]change K.IsHitList C y (K.next x (w 0))(List.ofFn (Fin.tail w))exact K.isHitList_of_isHitPath C y n _ _ htheorem isReturnPath_length_pos {S Ω : Type*} [Fintype Ω](K : FiniteChoiceChain S Ω) (C : Finset S) (y : S) (n : ℕ)(x : S) (w : Fin n → Ω) (h : K.IsReturnPath C y n x w) : 0 < n := byclassicalcases n with| zero => exact (K.isReturnPath_zero C x y w h).elim| succ n => omegatheorem isHitList_endpoint {S Ω : Type*} [Fintype Ω] [DecidableEq S](K : FiniteChoiceChain S Ω) (C : Finset S) (y x : S) (w : List Ω)(h : K.IsHitList C y x w) : K.listEndpoint x w = y := byinduction w generalizing x with| nil => exact h.2| cons a w ih => exact ih _ h.2theorem isReturnList_endpoint {S Ω : Type*} [Fintype Ω] [DecidableEq S](K : FiniteChoiceChain S Ω) (C : Finset S) (y x : S) (w : List Ω)(h : K.IsReturnList C y x w) : K.listEndpoint x w = y := bycases w with| nil => exact h.elim| cons a w => exact K.isHitList_endpoint C y _ w h/-- Two first-hit prefixes of one common choice list have the same cut point. -/theorem isHitList_append_unique {S Ω : Type*} [Fintype Ω] [DecidableEq S](K : FiniteChoiceChain S Ω) (C : Finset S) {x y z : S}{u v r s : List Ω} (hu : K.IsHitList C y x u)(hv : K.IsHitList C z x v) (happend : u ++ r = v ++ s) :u = v ∧ r = s := byinduction u generalizing x v with| nil =>cases v with| nil => exact ⟨rfl, happend⟩| cons b v => exact (hv.1 hu.1).elim| cons a u ih =>cases v with| nil => exact (hu.1 hv.1).elim| cons b v =>have hab : a = b := (List.cons.inj happend).1subst bhave h := ih hu.2 hv.2 (List.cons.inj happend).2exact ⟨congrArg (List.cons a) h.1, h.2⟩theorem isReturnList_append_unique {S Ω : Type*} [Fintype Ω][DecidableEq S] (K : FiniteChoiceChain S Ω) (C : Finset S){x y z : S} {u v r s : List Ω} (hu : K.IsReturnList C y x u)(hv : K.IsReturnList C z x v) (happend : u ++ r = v ++ s) :u = v ∧ r = s := bycases u with| nil => exact hu.elim| cons a u =>cases v with| nil => exact hv.elim| cons b v =>have hab : a = b := (List.cons.inj happend).1subst bhave htail := (List.cons.inj happend).2have h := K.isHitList_append_unique C hu hv htailexact ⟨congrArg (List.cons a) h.1, h.2⟩end FiniteChoiceChainnoncomputable def controlledListWord : AugmentedState → List BlockChoice → List ℕ| _, [] => []| x, w :: tail =>blockWord (feedback x.2) controlBlockLength x.1 w ++controlledListWord (controlledNext x w) tailtheorem controlledListWord_ofFn (n : ℕ) (x : AugmentedState)(w : Fin n → BlockChoice) :controlledListWord x (List.ofFn w) = controlledPathWord n x w := byinduction n generalizing x with| zero => rfl| succ n ih =>rw [show w = Fin.cons (w 0) (Fin.tail w) from(Fin.cons_self_tail w).symm]simp [controlledListWord, controlledPathWord, ih]rfltheorem holdingHitRecordWeight_eq_listWeight {k n : ℕ} {x : EmbeddedState}(r : HoldingHitRecord k n x) :holdingHitRecordWeight r =controlledChain.listWeight x.1.1(holdingHitRecordChoices (k := k) (n := n) (x := x) r) := byinduction k generalizing n x with| zero =>simp only [HoldingHitRecord] at rhave hn := r.down.2.1subst nrfl| succ k ih =>obtain ⟨guard, p, y, w, tail⟩ := rsimp only [holdingHitRecordWeight, holdingHitRecordChoices]rw [controlledChain.listWeight_append,controlledChain.listWeight_ofFn]have hw := controlledChain.isReturnList_of_isReturnPath controlSet y.1.1p.1.1 x.1.1 w.1 w.2rw [controlledChain.isReturnList_endpoint controlSet y.1.1 x.1.1(List.ofFn w.1) hw, ← ih tail]theorem recurrentLoopRecordWeight_eq_listWeight {k n : ℕ}(r : RecurrentLoopRecord k n) :recurrentLoopRecordWeight r = controlledChain.listWeightrecurrentAugmentedState(recurrentLoopRecordChoices (k := k) (n := n) r) := bycases k with| zero => exact r.elim| succ k =>obtain ⟨p, y, w, tail⟩ := rsimp only [recurrentLoopRecordWeight, recurrentLoopRecordChoices]rw [controlledChain.listWeight_append,controlledChain.listWeight_ofFn]have hw := controlledChain.isReturnList_of_isReturnPath controlSet y.1.1p.1.1 recurrentAugmentedState w.1 w.2rw [controlledChain.isReturnList_endpoint controlSet y.1.1recurrentAugmentedState (List.ofFn w.1) hw,← holdingHitRecordWeight_eq_listWeight tail]theorem holdingHitRecord_endpoint {k n : ℕ} {x : EmbeddedState}(r : HoldingHitRecord k n x) : controlledChain.listEndpoint x.1.1(holdingHitRecordChoices (k := k) (n := n) (x := x) r) =recurrentAugmentedState := byinduction k generalizing n x with| zero =>simp only [HoldingHitRecord] at rhave hx := r.down.2.2subst xrfl| succ k ih =>obtain ⟨guard, p, y, w, tail⟩ := rrw [holdingHitRecordChoices, controlledChain.listEndpoint_append]have hw := controlledChain.isReturnList_of_isReturnPath controlSet y.1.1p.1.1 x.1.1 w.1 w.2rw [controlledChain.isReturnList_endpoint controlSet y.1.1 x.1.1(List.ofFn w.1) hw]exact ih tailtheorem recurrentLoopRecord_endpoint {k n : ℕ}(r : RecurrentLoopRecord k n) : controlledChain.listEndpointrecurrentAugmentedState(recurrentLoopRecordChoices (k := k) (n := n) r) =recurrentAugmentedState := bycases k with| zero => exact r.elim| succ k =>obtain ⟨p, y, w, tail⟩ := rrw [recurrentLoopRecordChoices, controlledChain.listEndpoint_append]have hw := controlledChain.isReturnList_of_isReturnPath controlSet y.1.1p.1.1 recurrentAugmentedState w.1 w.2rw [controlledChain.isReturnList_endpoint controlSet y.1.1recurrentAugmentedState (List.ofFn w.1) hw]exact holdingHitRecord_endpoint tailtheorem holdingHitRecordChoices_injective (k n : ℕ) (x : EmbeddedState) :Function.Injective (holdingHitRecordChoices :HoldingHitRecord k n x → List BlockChoice) := byinduction k generalizing n x with| zero =>intro r s _simp only [HoldingHitRecord] at r sapply ULift.extapply Subtype.extexact Subsingleton.elim _ _| succ k ih =>intro r s hrsobtain ⟨gr, p, y, w, tail⟩ := robtain ⟨gs, q, z, v, tail'⟩ := ssimp only [holdingHitRecordChoices] at hrshave hwret := controlledChain.isReturnList_of_isReturnPath controlSet y.1.1p.1.1 x.1.1 w.1 w.2have hvret := controlledChain.isReturnList_of_isReturnPath controlSet z.1.1q.1.1 x.1.1 v.1 v.2have hsplit := controlledChain.isReturnList_append_unique controlSethwret hvret hrshave hlen : p.1.1 = q.1.1 := bysimpa using congrArg List.length hsplit.1have hpSum := Finset.HasAntidiagonal.mem_antidiagonal.mp p.2have hqSum := Finset.HasAntidiagonal.mem_antidiagonal.mp q.2have hrest : p.1.2 = q.1.2 := by omegahave hpval : p.1 = q.1 := Prod.ext hlen hresthave hp : p = q := Subtype.ext hpvalsubst qhave hwval : w.1 = v.1 := List.ofFn_injective hsplit.1have hyval : y.1.1 = z.1.1 := byhave hyend := controlledChain.isReturnList_endpoint controlSet y.1.1x.1.1 (List.ofFn w.1) hwrethave hzend := controlledChain.isReturnList_endpoint controlSet z.1.1x.1.1 (List.ofFn v.1) hvretrw [hsplit.1] at hyendexact hyend.symm.trans hzendhave hy : y = z := byapply Subtype.extapply Subtype.extexact hyvalsubst zhave hw : w = v := Subtype.ext hwvalsubst vhave ht : tail = tail' := ih p.1.2 y hsplit.2subst tail'have hg : gr = gs := Subsingleton.elim gr gssubst gsrfltheorem recurrentLoopRecordChoices_injective (k n : ℕ) :Function.Injective (recurrentLoopRecordChoices :RecurrentLoopRecord k n → List BlockChoice) := bycases k with| zero =>intro rexact r.elim| succ k =>intro r s hrsobtain ⟨p, y, w, tail⟩ := robtain ⟨q, z, v, tail'⟩ := ssimp only [recurrentLoopRecordChoices] at hrshave hwret := controlledChain.isReturnList_of_isReturnPath controlSet y.1.1p.1.1 recurrentAugmentedState w.1 w.2have hvret := controlledChain.isReturnList_of_isReturnPath controlSet z.1.1q.1.1 recurrentAugmentedState v.1 v.2have hsplit := controlledChain.isReturnList_append_unique controlSethwret hvret hrshave hlen : p.1.1 = q.1.1 := bysimpa using congrArg List.length hsplit.1have hpSum := Finset.HasAntidiagonal.mem_antidiagonal.mp p.2have hqSum := Finset.HasAntidiagonal.mem_antidiagonal.mp q.2have hrest : p.1.2 = q.1.2 := by omegahave hpval : p.1 = q.1 := Prod.ext hlen hresthave hp : p = q := Subtype.ext hpvalsubst qhave hwval : w.1 = v.1 := List.ofFn_injective hsplit.1have hyval : y.1.1 = z.1.1 := byhave hyend := controlledChain.isReturnList_endpoint controlSet y.1.1recurrentAugmentedState (List.ofFn w.1) hwrethave hzend := controlledChain.isReturnList_endpoint controlSet z.1.1recurrentAugmentedState (List.ofFn v.1) hvretrw [hsplit.1] at hyendexact hyend.symm.trans hzendhave hy : y = z := byapply Subtype.extapply Subtype.extexact hyvalsubst zhave hw : w = v := Subtype.ext hwvalsubst vhave ht : tail = tail' := holdingHitRecordChoices_injective k p.1.2 y hsplit.2subst tail'rfl/-! ### The controlled word carried by a loop record -/noncomputable def recurrentLoopRecordTuple {k n : ℕ}(r : RecurrentLoopRecord k n) : Fin n → BlockChoice := fun i ↦(recurrentLoopRecordChoices (k := k) (n := n) r).get(Fin.cast (recurrentLoopRecordChoices_length r).symm i)@[simp] theorem ofFn_recurrentLoopRecordTuple {k n : ℕ}(r : RecurrentLoopRecord k n) :List.ofFn (recurrentLoopRecordTuple r) =recurrentLoopRecordChoices (k := k) (n := n) r := byapply List.ext_get· simp· intro i hi hjsimp [recurrentLoopRecordTuple]noncomputable def recurrentLoopRecordWord {k n : ℕ}(r : RecurrentLoopRecord k n) : List ℕ :=controlledListWord recurrentAugmentedState(recurrentLoopRecordChoices (k := k) (n := n) r)@[simp] theorem recurrentLoopRecordWord_eq_controlledPathWord {k n : ℕ}(r : RecurrentLoopRecord k n) : recurrentLoopRecordWord r =controlledPathWord n recurrentAugmentedState(recurrentLoopRecordTuple r) := byrw [recurrentLoopRecordWord, ← controlledListWord_ofFn,ofFn_recurrentLoopRecordTuple]theorem recurrentLoopRecord_pathEndpoint {k n : ℕ}(r : RecurrentLoopRecord k n) :controlledChain.pathEndpoint n recurrentAugmentedState(recurrentLoopRecordTuple r) = recurrentAugmentedState := byrw [← controlledChain.listEndpoint_ofFn, ofFn_recurrentLoopRecordTuple]exact recurrentLoopRecord_endpoint rtheorem recurrentLoopRecordWeight_eq_pathWeight {k n : ℕ}(r : RecurrentLoopRecord k n) : recurrentLoopRecordWeight r =controlledChain.pathWeight n recurrentAugmentedState(recurrentLoopRecordTuple r) := byrw [recurrentLoopRecordWeight_eq_listWeight,← controlledChain.listWeight_ofFn, ofFn_recurrentLoopRecordTuple]theorem recurrentLoopRecordWeight_nonneg {k n : ℕ}(r : RecurrentLoopRecord k n) : 0 ≤ recurrentLoopRecordWeight r := byrw [recurrentLoopRecordWeight_eq_listWeight]exact controlledChain.listWeight_nonneg _ _theorem recurrentLoopRecordWord_mem_multipliers {k n : ℕ}(r : RecurrentLoopRecord k n) :∀ a ∈ recurrentLoopRecordWord r, a ∈ multipliers := byrw [recurrentLoopRecordWord_eq_controlledPathWord]exact controlledPathWord_mem_multipliers n recurrentAugmentedState(recurrentLoopRecordTuple r)@[simp] theorem recurrentLoopRecordWord_length {k n : ℕ}(r : RecurrentLoopRecord k n) :(recurrentLoopRecordWord r).length = controlBlockLength * n := byrw [recurrentLoopRecordWord_eq_controlledPathWord,controlledPathWord_length]theorem recurrentLoopRecord_imbalance_zero {k n : ℕ}(r : RecurrentLoopRecord k n) : imbalance (recurrentLoopRecordWord r) = 0 := byrw [recurrentLoopRecordWord_eq_controlledPathWord]exact controlledPathWord_imbalance_zero n recurrentAugmentedState(recurrentLoopRecordTuple r) (recurrentLoopRecord_pathEndpoint r)theorem recurrentLoopRecordWeight_eq_reciprocal {k n : ℕ}(r : RecurrentLoopRecord k n) (hpos : 0 < recurrentLoopRecordWeight r) :recurrentLoopRecordWeight r = 1 / ((recurrentLoopRecordWord r).prod : ℝ) := byrw [recurrentLoopRecordWeight_eq_pathWeight] at hpos ⊢rw [recurrentLoopRecordWord_eq_controlledPathWord]exact controlledPathWeight_eq_reciprocal n recurrentAugmentedState(recurrentLoopRecordTuple r) hpos (recurrentLoopRecord_pathEndpoint r)theorem recurrentLoopRecord_blockLength_pos {k n : ℕ}(r : RecurrentLoopRecord k n) : 0 < n := bycases k with| zero => exact r.elim| succ k =>obtain ⟨p, y, w, tail⟩ := rhave hp : 0 < p.1.1 :=controlledChain.isReturnPath_length_pos controlSet y.1.1 p.1.1recurrentAugmentedState w.1 w.2have hsum := Finset.HasAntidiagonal.mem_antidiagonal.mp p.2omegatheorem recurrentLoopRecordWord_ne_nil {k n : ℕ}(r : RecurrentLoopRecord k n) : recurrentLoopRecordWord r ≠ [] := byintro hnilhave hlen := congrArg List.length hnilsimp only [recurrentLoopRecordWord_length, List.length_nil] at hlenhave hN := controlBlockLength_poshave hn := recurrentLoopRecord_blockLength_pos rhave hprod : 0 < controlBlockLength * n := Nat.mul_pos hN hnomeganoncomputable def recurrentLoopReward {k n : ℕ}(r : RecurrentLoopRecord k n) : ℕ :=exponents (recurrentLoopRecordWord r) 3theorem recurrentLoopRecord_prod_eq_q_pow {k n : ℕ}(r : RecurrentLoopRecord k n) :(recurrentLoopRecordWord r).prod = q ^ recurrentLoopReward r := byexact prod_eq_q_pow_of_imbalance_eq_zero (recurrentLoopRecordWord r)(recurrentLoopRecordWord_mem_multipliers r)(recurrentLoopRecord_imbalance_zero r)theorem prod_two_le_of_ne_nil {w : List ℕ} (hne : w ≠ [])(htwo : ∀ a ∈ w, 2 ≤ a) : 2 ≤ w.prod := byobtain ⟨a, tail, rfl⟩ := List.exists_cons_of_ne_nil hnehave ha := htwo a (by simp)have htail : 1 ≤ tail.prod := byapply Nat.one_le_iff_ne_zero.mprapply List.prod_ne_zerointro hzerohave hb2 := htwo 0 (by simp [hzero])omegasimp only [List.prod_cons]nlinariththeorem recurrentLoopReward_pos {k n : ℕ} (r : RecurrentLoopRecord k n) :0 < recurrentLoopReward r := byby_contra hnothave hz : recurrentLoopReward r = 0 := Nat.eq_zero_of_not_pos hnothave hprod := recurrentLoopRecord_prod_eq_q_pow rrw [hz, pow_zero] at hprodhave htwo : ∀ a ∈ recurrentLoopRecordWord r, 2 ≤ a := byintro a haexact mem_multipliers_two_le (recurrentLoopRecordWord_mem_multipliers r a ha)have := prod_two_le_of_ne_nil (recurrentLoopRecordWord_ne_nil r) htwoomegatheorem recurrentLoopRecordWeight_eq_q_reward {k n : ℕ}(r : RecurrentLoopRecord k n) (hpos : 0 < recurrentLoopRecordWeight r) :recurrentLoopRecordWeight r = 1 / (q ^ recurrentLoopReward r : ℕ) := byrw [recurrentLoopRecordWeight_eq_reciprocal r hpos,recurrentLoopRecord_prod_eq_q_pow]/-! ### Recovering the number of embedded visits from the flat path -/namespace FiniteChoiceChaindef listVisitCount {S Ω : Type*} [Fintype Ω] [DecidableEq S](K : FiniteChoiceChain S Ω) (C : Finset S) : S → List Ω → ℕ| _, [] => 0| x, a :: w => (if K.next x a ∈ C then 1 else 0) +K.listVisitCount C (K.next x a) wtheorem listVisitCount_append {S Ω : Type*} [Fintype Ω] [DecidableEq S](K : FiniteChoiceChain S Ω) (C : Finset S) (x : S)(u v : List Ω) : K.listVisitCount C x (u ++ v) =K.listVisitCount C x u +K.listVisitCount C (K.listEndpoint x u) v := byinduction u generalizing x with| nil => simp [listVisitCount, listEndpoint]| cons a u ih =>simp only [List.cons_append, listVisitCount, listEndpoint, ih]omegatheorem isHitList_visitCount {S Ω : Type*} [Fintype Ω] [DecidableEq S](K : FiniteChoiceChain S Ω) (C : Finset S) (y x : S) (w : List Ω)(h : K.IsHitList C y x w) :K.listVisitCount C x w = if x ∈ C then 0 else 1 := bycases w with| nil => simp [listVisitCount, h.1]| cons a w =>have hx : x ∉ C := h.1have ih := K.isHitList_visitCount C y (K.next x a) w h.2simp only [listVisitCount, hx, if_false]by_cases hn : K.next x a ∈ C <;> simp [hn] at ih ⊢ <;> omegatermination_by w.lengththeorem isReturnList_visitCount {S Ω : Type*} [Fintype Ω] [DecidableEq S](K : FiniteChoiceChain S Ω) (C : Finset S) (y x : S) (w : List Ω)(h : K.IsReturnList C y x w) : K.listVisitCount C x w = 1 := bycases w with| nil => exact h.elim| cons a w =>have ih := K.isHitList_visitCount C y (K.next x a) w hsimp only [listVisitCount]by_cases hn : K.next x a ∈ C <;> simp [hn] at ih ⊢ <;> omegaend FiniteChoiceChaintheorem holdingHitRecord_visitCount {k n : ℕ} {x : EmbeddedState}(r : HoldingHitRecord k n x) : controlledChain.listVisitCount controlSetx.1.1 (holdingHitRecordChoices (k := k) (n := n) (x := x) r) = k := byinduction k generalizing n x with| zero => simp [holdingHitRecordChoices,FiniteChoiceChain.listVisitCount]| succ k ih =>obtain ⟨guard, p, y, w, tail⟩ := rrw [holdingHitRecordChoices, controlledChain.listVisitCount_append]have hw := controlledChain.isReturnList_of_isReturnPath controlSet y.1.1p.1.1 x.1.1 w.1 w.2rw [controlledChain.isReturnList_visitCount controlSet y.1.1 x.1.1(List.ofFn w.1) hw]rw [controlledChain.isReturnList_endpoint controlSet y.1.1 x.1.1(List.ofFn w.1) hw, ih tail]omegatheorem recurrentLoopRecord_visitCount {k n : ℕ}(r : RecurrentLoopRecord k n) : controlledChain.listVisitCount controlSetrecurrentAugmentedState(recurrentLoopRecordChoices (k := k) (n := n) r) = k := bycases k with| zero => exact r.elim| succ k =>obtain ⟨p, y, w, tail⟩ := rrw [recurrentLoopRecordChoices, controlledChain.listVisitCount_append]have hw := controlledChain.isReturnList_of_isReturnPath controlSet y.1.1p.1.1 recurrentAugmentedState w.1 w.2rw [controlledChain.isReturnList_visitCount controlSet y.1.1recurrentAugmentedState (List.ofFn w.1) hw]rw [controlledChain.isReturnList_endpoint controlSet y.1.1recurrentAugmentedState (List.ofFn w.1) hw,holdingHitRecord_visitCount tail]omega/-- A loop record is determined globally by its flat choice list, includingits two duration indices. -/theorem recurrentLoopRecord_sigma_choices_injective :Function.Injective (fun r : Σ k : ℕ, Σ n : ℕ, RecurrentLoopRecord k n ↦recurrentLoopRecordChoices (k := r.1) (n := r.2.1) r.2.2) := byintro r s hrsobtain ⟨k, n, r⟩ := robtain ⟨l, m, s⟩ := shave hnm : n = m := bysimpa using congrArg List.length hrssubst mhave hkl : k = l := byhave hr := recurrentLoopRecord_visitCount rhave hs := recurrentLoopRecord_visitCount schange recurrentLoopRecordChoices r = recurrentLoopRecordChoices s at hrsrw [hrs] at hromegasubst lhave hrs' : r = s := recurrentLoopRecordChoices_injective k n hrssubst srfl/-! ### First-return status with respect to the recurrent base itself -/namespace FiniteChoiceChaintheorem isHitList_append_of_subset {S Ω : Type*} [Fintype Ω][DecidableEq S] (K : FiniteChoiceChain S Ω) {B C : Finset S}(hBC : B ⊆ C) {x y z : S} {u v : List Ω}(hu : K.IsHitList C y x u) (hv : K.IsHitList B z y v) :K.IsHitList B z x (u ++ v) := byinduction u generalizing x with| nil =>have hxy := hu.2subst xexact hv| cons a u ih =>have hxB : x ∉ B := fun hx ↦ hu.1 (hBC hx)exact ⟨hxB, ih hu.2⟩theorem isReturnList_append_hit_of_subset {S Ω : Type*} [Fintype Ω][DecidableEq S] (K : FiniteChoiceChain S Ω) {B C : Finset S}(hBC : B ⊆ C) {x y z : S} {u v : List Ω}(hu : K.IsReturnList C y x u) (hv : K.IsHitList B z y v) :K.IsReturnList B z x (u ++ v) := bycases u with| nil => exact hu.elim| cons a u =>exact K.isHitList_append_of_subset hBC hu hvtheorem isHitList_of_returnList_of_not_mem {S Ω : Type*} [Fintype Ω][DecidableEq S] (K : FiniteChoiceChain S Ω) (B : Finset S){x y : S} {u : List Ω} (hx : x ∉ B) (hu : K.IsReturnList B y x u) :K.IsHitList B y x u := bycases u with| nil => exact hu.elim| cons a u => exact ⟨hx, hu⟩end FiniteChoiceChaintheorem holdingHitRecord_isHitBase {k n : ℕ} {x : EmbeddedState}(r : HoldingHitRecord k n x) : controlledChain.IsHitList{recurrentAugmentedState} recurrentAugmentedState x.1.1(holdingHitRecordChoices (k := k) (n := n) (x := x) r) := byinduction k generalizing n x with| zero =>simp only [HoldingHitRecord] at rhave hn := r.down.2.1have hx := r.down.2.2subst nsubst xchange recurrentAugmentedState ∈ ({recurrentAugmentedState} :Finset AugmentedState) ∧ recurrentAugmentedState = recurrentAugmentedStatesimp| succ k ih =>obtain ⟨guard, p, y, w, tail⟩ := rhave hseg := controlledChain.isReturnList_of_isReturnPath controlSet y.1.1p.1.1 x.1.1 w.1 w.2have hsub : ({recurrentAugmentedState} : Finset AugmentedState) ⊆controlSet := byintro z hzsimp only [Finset.mem_singleton] at hzsubst zexact recurrentAugmentedState_memhave hreturn := controlledChain.isReturnList_append_hit_of_subset hsubhseg (ih tail)have hx : x.1.1 ∉ ({recurrentAugmentedState} : Finset AugmentedState) := bysimp only [Finset.mem_singleton]intro heqapply guard.2apply Subtype.extapply Subtype.extexact heqexact controlledChain.isHitList_of_returnList_of_not_mem{recurrentAugmentedState} hx hreturntheorem recurrentLoopRecord_isReturnBase {k n : ℕ}(r : RecurrentLoopRecord k n) : controlledChain.IsReturnList{recurrentAugmentedState} recurrentAugmentedState recurrentAugmentedState(recurrentLoopRecordChoices (k := k) (n := n) r) := bycases k with| zero => exact r.elim| succ k =>obtain ⟨p, y, w, tail⟩ := rhave hseg := controlledChain.isReturnList_of_isReturnPath controlSet y.1.1p.1.1 recurrentAugmentedState w.1 w.2have hsub : ({recurrentAugmentedState} : Finset AugmentedState) ⊆controlSet := byintro z hzsimp only [Finset.mem_singleton] at hzsubst zexact recurrentAugmentedState_memexact controlledChain.isReturnList_append_hit_of_subset hsub hseg(holdingHitRecord_isHitBase tail)/-! ## The first-return reward distribution -/abbrev FirstReturnLoop := Σ k : ℕ, Σ n : ℕ, RecurrentLoopRecord k nnoncomputable def firstReturnLoopWeight (r : FirstReturnLoop) : ℝ :=recurrentLoopRecordWeight r.2.2noncomputable def firstReturnLoopWord (r : FirstReturnLoop) : List ℕ :=recurrentLoopRecordWord r.2.2noncomputable def firstReturnLoopReward (r : FirstReturnLoop) : ℕ :=recurrentLoopReward r.2.2theorem firstReturnLoopWeight_nonneg (r : FirstReturnLoop) :0 ≤ firstReturnLoopWeight r :=recurrentLoopRecordWeight_nonneg r.2.2theorem firstReturnLoopReward_pos (r : FirstReturnLoop) :0 < firstReturnLoopReward r := recurrentLoopReward_pos r.2.2theorem firstReturnLoopWeight_eq_q_reward (r : FirstReturnLoop)(hpos : 0 < firstReturnLoopWeight r) :firstReturnLoopWeight r = 1 / (q ^ firstReturnLoopReward r : ℕ) :=recurrentLoopRecordWeight_eq_q_reward r.2.2 hpostheorem firstReturnLoopWord_mem_multipliers (r : FirstReturnLoop) :∀ a ∈ firstReturnLoopWord r, a ∈ multipliers :=recurrentLoopRecordWord_mem_multipliers r.2.2theorem firstReturnLoopWord_length (r : FirstReturnLoop) :(firstReturnLoopWord r).length = controlBlockLength * r.2.1 :=recurrentLoopRecordWord_length r.2.2theorem firstReturnLoopWeight_summable : Summable firstReturnLoopWeight := byhave hk (k : ℕ) : Summable fun z : Σ n : ℕ, RecurrentLoopRecord k n ↦recurrentLoopRecordWeight z.2 := byrw [summable_sigma_of_nonneg fun z ↦ recurrentLoopRecordWeight_nonneg z.2]constructor· intro nexact Summable.of_finite· have heq : (fun n ↦ ∑' r : RecurrentLoopRecord k n,recurrentLoopRecordWeight r) = fun n ↦ holdingReturn k n := byfunext nrw [tsum_fintype, sum_recurrentLoopRecordWeight]rw [heq]exact (holdingReturn_summable_and_mass k).1rw [summable_sigma_of_nonneg firstReturnLoopWeight_nonneg]constructor· exact hk· have heq : (fun k ↦ ∑' z : Σ n : ℕ, RecurrentLoopRecord k n,recurrentLoopRecordWeight z.2) =fun k ↦ EmbeddedChain.returnHitTotal {recurrentControlState} krecurrentControlState := byfunext krw [(hk k).tsum_sigma]simp_rw [tsum_fintype, sum_recurrentLoopRecordWeight,(holdingReturn_summable_and_mass k).2,embedded_returnAt_eq_returnHitTotal]change Summable fun k ↦ ∑' z : Σ n : ℕ, RecurrentLoopRecord k n,recurrentLoopRecordWeight z.2rw [heq]exact recurrent_visit_return_total.summabletheorem tsum_firstReturnLoopWeight : (∑' r, firstReturnLoopWeight r) = 1 := byhave hk (k : ℕ) : Summable fun z : Σ n : ℕ, RecurrentLoopRecord k n ↦recurrentLoopRecordWeight z.2 := byrw [summable_sigma_of_nonneg fun z ↦ recurrentLoopRecordWeight_nonneg z.2]constructor· intro nexact Summable.of_finite· simpa only [tsum_fintype, sum_recurrentLoopRecordWeight] using(holdingReturn_summable_and_mass k).1rw [firstReturnLoopWeight_summable.tsum_sigma]change (∑' k, ∑' z : Σ n : ℕ, RecurrentLoopRecord k n,recurrentLoopRecordWeight z.2) = 1simp_rw [(hk _).tsum_sigma, tsum_fintype, sum_recurrentLoopRecordWeight,(holdingReturn_summable_and_mass _).2,embedded_returnAt_eq_returnHitTotal]exact recurrent_visit_return_total.tsum_eqtheorem weighted_firstReturnLoopDuration_summable :Summable fun r : FirstReturnLoop ↦(r.2.1 : ℝ) * firstReturnLoopWeight r := byrw [summable_sigma_of_nonneg fun r ↦ mul_nonneg (Nat.cast_nonneg r.2.1)(firstReturnLoopWeight_nonneg r)]constructor· intro kchange Summable fun z : Σ n : ℕ, RecurrentLoopRecord k n ↦(z.1 : ℝ) * recurrentLoopRecordWeight z.2rw [summable_sigma_of_nonneg fun z ↦ mul_nonneg (Nat.cast_nonneg z.1)(recurrentLoopRecordWeight_nonneg z.2)]constructor· intro nexact Summable.of_finite· have heq : (fun n ↦ ∑' r : RecurrentLoopRecord k n,(n : ℝ) * recurrentLoopRecordWeight r) =fun n : ℕ ↦ (n : ℝ) * holdingReturn k n := byfunext nrw [tsum_fintype, ← Finset.mul_sum, sum_recurrentLoopRecordWeight]rw [heq]exact holdingReturn_weighted_summable k· have heq : (fun k ↦ ∑' z : Σ n : ℕ, RecurrentLoopRecord k n,(z.1 : ℝ) * recurrentLoopRecordWeight z.2) = holdingReturnMean := byfunext khave hk : Summable fun z : Σ n : ℕ, RecurrentLoopRecord k n ↦(z.1 : ℝ) * recurrentLoopRecordWeight z.2 := byrw [summable_sigma_of_nonneg fun z ↦ mul_nonneg (Nat.cast_nonneg z.1)(recurrentLoopRecordWeight_nonneg z.2)]constructor· intro nexact Summable.of_finite· simpa only [tsum_fintype, ← Finset.mul_sum,sum_recurrentLoopRecordWeight] using holdingReturn_weighted_summable krw [hk.tsum_sigma]simp_rw [tsum_fintype, ← Finset.mul_sum, sum_recurrentLoopRecordWeight]rflchange Summable fun k ↦ ∑' z : Σ n : ℕ, RecurrentLoopRecord k n,(z.1 : ℝ) * recurrentLoopRecordWeight z.2rw [heq]exact holdingReturnMean_summabletheorem primeIncrement_seven_le_one {a : ℕ} (ha : a ∈ multipliers) :primeIncrement a 3 ≤ 1 := bysimp only [multipliers, Finset.mem_insert, Finset.mem_singleton] at harcases ha with rfl | rfl | rfl | rfl | rfl <;> decidetheorem exponent_seven_le_length (w : List ℕ)(hw : ∀ a ∈ w, a ∈ multipliers) : exponents w 3 ≤ w.length := byinduction w with| nil => simp| cons a w ih =>have ha := primeIncrement_seven_le_one (hw a (by simp))have htail : ∀ b ∈ w, b ∈ multipliers := byintro b hbexact hw b (by simp [hb])simp only [exponents_cons, List.length_cons]have ht := ih htailomegatheorem firstReturnLoopReward_le_duration (r : FirstReturnLoop) :firstReturnLoopReward r ≤ controlBlockLength * r.2.1 := bycalcfirstReturnLoopReward r ≤ (firstReturnLoopWord r).length :=exponent_seven_le_length _ (firstReturnLoopWord_mem_multipliers r)_ = controlBlockLength * r.2.1 := firstReturnLoopWord_length rtheorem weighted_firstReturnLoopReward_summable :Summable fun r : FirstReturnLoop ↦(firstReturnLoopReward r : ℝ) * firstReturnLoopWeight r := byapply Summable.of_nonneg_of_le(fun r ↦ mul_nonneg (Nat.cast_nonneg _) (firstReturnLoopWeight_nonneg r))(fun r ↦ byhave hcast : (firstReturnLoopReward r : ℝ) ≤(controlBlockLength * r.2.1 : ℕ) := byexact_mod_cast firstReturnLoopReward_le_duration rexact mul_le_mul_of_nonneg_right hcast(firstReturnLoopWeight_nonneg r))simpa only [Nat.cast_mul, mul_assoc] usingweighted_firstReturnLoopDuration_summable.mul_left(controlBlockLength : ℝ)theorem firstReturnLoopWord_injective_of_pos :Set.InjOn firstReturnLoopWord {r | 0 < firstReturnLoopWeight r} := byintro r hr s hs hwordobtain ⟨k, n, r⟩ := robtain ⟨l, m, s⟩ := shave hnm : n = m := byhave hlen := congrArg List.length hwordsimp only [firstReturnLoopWord, recurrentLoopRecordWord_length] at hlenhave hN := controlBlockLength_posexact Nat.mul_left_cancel hN hlensubst mhave htuple : recurrentLoopRecordTuple r = recurrentLoopRecordTuple s := byapply controlledPathWord_injective_of_return n recurrentAugmentedState· rwa [← recurrentLoopRecordWeight_eq_pathWeight]· rwa [← recurrentLoopRecordWeight_eq_pathWeight]· exact recurrentLoopRecord_pathEndpoint r· exact recurrentLoopRecord_pathEndpoint s· simpa only [firstReturnLoopWord,recurrentLoopRecordWord_eq_controlledPathWord] using hwordhave hchoices : recurrentLoopRecordChoices r = recurrentLoopRecordChoices s := byrw [← ofFn_recurrentLoopRecordTuple, ← ofFn_recurrentLoopRecordTuple,htuple]exact recurrentLoopRecord_sigma_choices_injective hchoicesnoncomputable def loopRewardMass (m : ℕ) : ℝ :=∑' r : FirstReturnLoop,selectedWeight (firstReturnLoopReward r = m) (firstReturnLoopWeight r)theorem selectedWeight_nonneg {P : Prop} {x : ℝ} (hx : 0 ≤ x) :0 ≤ selectedWeight P x := byclassicalby_cases hP : P <;> simp [selectedWeight, hP, hx]theorem selectedWeight_le {P : Prop} {x : ℝ} (hx : 0 ≤ x) :selectedWeight P x ≤ x := byclassicalby_cases hP : P <;> simp [selectedWeight, hP, hx]theorem loopRewardMass_nonneg (m : ℕ) : 0 ≤ loopRewardMass m :=tsum_nonneg fun r ↦ selectedWeight_nonneg (firstReturnLoopWeight_nonneg r)theorem rewardIndicator_hasSum (r : FirstReturnLoop) :HasSum (fun m ↦ selectedWeight (firstReturnLoopReward r = m)(firstReturnLoopWeight r)) (firstReturnLoopWeight r) := bysimpa [selectedWeight, eq_comm] using(hasSum_ite_eq (firstReturnLoopReward r) (firstReturnLoopWeight r))theorem loopRewardMass_summable : Summable loopRewardMass := byhave hpair : Summable fun p : FirstReturnLoop × ℕ ↦selectedWeight (firstReturnLoopReward p.1 = p.2)(firstReturnLoopWeight p.1) := byrw [summable_prod_of_nonneg fun p ↦selectedWeight_nonneg (firstReturnLoopWeight_nonneg p.1)]constructor· exact fun r ↦ (rewardIndicator_hasSum r).summable· have heq : (fun r ↦ ∑' m, selectedWeight(firstReturnLoopReward r = m) (firstReturnLoopWeight r)) =firstReturnLoopWeight := byfunext rexact (rewardIndicator_hasSum r).tsum_eqrw [heq]exact firstReturnLoopWeight_summablehave hswap : Summable fun p : ℕ × FirstReturnLoop ↦selectedWeight (firstReturnLoopReward p.2 = p.1)(firstReturnLoopWeight p.2) := bysimpa [Function.comp_def] using hpair.comp_injective(show Function.Injective(fun p : ℕ × FirstReturnLoop ↦ (p.2, p.1)) byexact (Equiv.prodComm ℕ FirstReturnLoop).injective)rw [summable_prod_of_nonneg fun p ↦selectedWeight_nonneg (firstReturnLoopWeight_nonneg p.2)] at hswapchange Summable fun m ↦∑' r : FirstReturnLoop,selectedWeight (firstReturnLoopReward r = m) (firstReturnLoopWeight r)exact hswap.2theorem tsum_loopRewardMass : (∑' m, loopRewardMass m) = 1 := byhave hpair : Summable fun p : FirstReturnLoop × ℕ ↦selectedWeight (firstReturnLoopReward p.1 = p.2)(firstReturnLoopWeight p.1) := byrw [summable_prod_of_nonneg fun p ↦selectedWeight_nonneg (firstReturnLoopWeight_nonneg p.1)]exact ⟨fun r ↦ (rewardIndicator_hasSum r).summable, bysimpa only [(rewardIndicator_hasSum _).tsum_eq] usingfirstReturnLoopWeight_summable⟩have hswap : Summable fun p : ℕ × FirstReturnLoop ↦selectedWeight (firstReturnLoopReward p.2 = p.1)(firstReturnLoopWeight p.2) := bysimpa [Function.comp_def] using hpair.comp_injective(show Function.Injective(fun p : ℕ × FirstReturnLoop ↦ (p.2, p.1)) byexact (Equiv.prodComm ℕ FirstReturnLoop).injective)calc(∑' m, loopRewardMass m) = ∑' p : ℕ × FirstReturnLoop,selectedWeight (firstReturnLoopReward p.2 = p.1)(firstReturnLoopWeight p.2) := byrw [hswap.tsum_prod]rfl_ = ∑' p : FirstReturnLoop × ℕ,selectedWeight (firstReturnLoopReward p.1 = p.2)(firstReturnLoopWeight p.1) :=((Equiv.prodComm FirstReturnLoop ℕ).tsum_eq _).symm_ = ∑' r, ∑' m, selectedWeight (firstReturnLoopReward r = m)(firstReturnLoopWeight r) := hpair.tsum_prod_ = ∑' r, firstReturnLoopWeight r := byapply tsum_congrexact fun r ↦ (rewardIndicator_hasSum r).tsum_eq_ = 1 := tsum_firstReturnLoopWeighttheorem weightedRewardIndicator_hasSum (r : FirstReturnLoop) :HasSum (fun m : ℕ ↦ (m : ℝ) *selectedWeight (firstReturnLoopReward r = m) (firstReturnLoopWeight r))((firstReturnLoopReward r : ℝ) * firstReturnLoopWeight r) := byhave h := hasSum_ite_eq (firstReturnLoopReward r)((firstReturnLoopReward r : ℝ) * firstReturnLoopWeight r)have hfun : (fun m : ℕ ↦ (m : ℝ) *selectedWeight (firstReturnLoopReward r = m) (firstReturnLoopWeight r)) =fun m ↦ if m = firstReturnLoopReward r then(firstReturnLoopReward r : ℝ) * firstReturnLoopWeight r else 0 := byfunext mby_cases hm : firstReturnLoopReward r = m· subst msimp [selectedWeight]· simp [selectedWeight, hm, Ne.symm hm]rw [hfun]exact htheorem weighted_loopRewardMass_summable :Summable fun m : ℕ ↦ (m : ℝ) * loopRewardMass m := byhave hpair : Summable fun p : FirstReturnLoop × ℕ ↦ (p.2 : ℝ) *selectedWeight (firstReturnLoopReward p.1 = p.2)(firstReturnLoopWeight p.1) := byrw [summable_prod_of_nonneg fun p ↦ mul_nonneg (Nat.cast_nonneg p.2)(selectedWeight_nonneg (firstReturnLoopWeight_nonneg p.1))]constructor· exact fun r ↦ (weightedRewardIndicator_hasSum r).summable· have heq : (fun r ↦ ∑' m : ℕ, (m : ℝ) * selectedWeight(firstReturnLoopReward r = m) (firstReturnLoopWeight r)) =fun r ↦ (firstReturnLoopReward r : ℝ) * firstReturnLoopWeight r := byfunext rexact (weightedRewardIndicator_hasSum r).tsum_eqrw [heq]exact weighted_firstReturnLoopReward_summablehave hswap : Summable fun p : ℕ × FirstReturnLoop ↦ (p.1 : ℝ) *selectedWeight (firstReturnLoopReward p.2 = p.1)(firstReturnLoopWeight p.2) := bysimpa [Function.comp_def] using hpair.comp_injective(show Function.Injective(fun p : ℕ × FirstReturnLoop ↦ (p.2, p.1)) byexact (Equiv.prodComm ℕ FirstReturnLoop).injective)rw [summable_prod_of_nonneg fun p ↦ mul_nonneg (Nat.cast_nonneg p.1)(selectedWeight_nonneg (firstReturnLoopWeight_nonneg p.2))] at hswaphave hslices (m : ℕ) : Summable fun r : FirstReturnLoop ↦selectedWeight (firstReturnLoopReward r = m) (firstReturnLoopWeight r) :=Summable.of_nonneg_of_le(fun r ↦ selectedWeight_nonneg (firstReturnLoopWeight_nonneg r))(fun r ↦ selectedWeight_le (firstReturnLoopWeight_nonneg r))firstReturnLoopWeight_summablehave heq : (fun m : ℕ ↦ (m : ℝ) * loopRewardMass m) =fun m : ℕ ↦ ∑' r : FirstReturnLoop, (m : ℝ) *selectedWeight (firstReturnLoopReward r = m) (firstReturnLoopWeight r) := byfunext mexact ((hslices m).tsum_mul_left (m : ℝ)).symmrw [heq]exact hswap.2theorem loopRewardMass_zero : loopRewardMass 0 = 0 := byrw [loopRewardMass]calc(∑' r : FirstReturnLoop,selectedWeight (firstReturnLoopReward r = 0) (firstReturnLoopWeight r)) =∑' _r : FirstReturnLoop, (0 : ℝ) := byapply tsum_congrintro rhave hr := firstReturnLoopReward_pos rsimp [selectedWeight, hr.ne']_ = 0 := tsum_zero/-! ### Finiteness and the exact counting interpretation -/theorem one_le_primeIncrement_sum {a : ℕ} (ha : a ∈ multipliers) :1 ≤ primeIncrement a 0 + primeIncrement a 1 +primeIncrement a 2 + primeIncrement a 3 := bysimp only [multipliers, Finset.mem_insert, Finset.mem_singleton] at harcases ha with rfl | rfl | rfl | rfl | rfl <;> decidetheorem length_le_exponent_sum (w : List ℕ)(hw : ∀ a ∈ w, a ∈ multipliers) :w.length ≤ exponents w 0 + exponents w 1 + exponents w 2 + exponents w 3 := byinduction w with| nil => simp| cons a w ih =>have ha := one_le_primeIncrement_sum (hw a (by simp))have htail : ∀ b ∈ w, b ∈ multipliers := byintro b hbexact hw b (by simp [hb])simp only [List.length_cons, exponents_cons]have ht := ih htailomegatheorem firstReturnLoopWord_length_le (r : FirstReturnLoop) :(firstReturnLoopWord r).length ≤ 69 * firstReturnLoopReward r := byhave hlen := length_le_exponent_sum (firstReturnLoopWord r)(firstReturnLoopWord_mem_multipliers r)have hzero := recurrentLoopRecord_imbalance_zero r.2.2have heq := exponents_eq_of_imbalance_eq_zero hzerorcases heq with ⟨h0, h1, h2⟩dsimp only [firstReturnLoopWord, firstReturnLoopReward,recurrentLoopReward] at hlen h0 h1 h2 ⊢omegaabbrev AllowedMultiplier := {a : ℕ // a ∈ multipliers}abbrev BoundedMultiplierWord (L : ℕ) :=Σ n : Fin (L + 1), Fin n → AllowedMultipliernoncomputable def boundedMultiplierWordCode (L : ℕ) (w : List ℕ)(hL : w.length ≤ L) (hw : ∀ a ∈ w, a ∈ multipliers) :BoundedMultiplierWord L :=⟨⟨w.length, by omega⟩, fun i ↦ ⟨w.get i, hw _ (List.get_mem w i)⟩⟩def decodeBoundedMultiplierWord {L : ℕ} (c : BoundedMultiplierWord L) : List ℕ :=List.ofFn fun i ↦ (c.2 i).1@[simp] theorem decode_boundedMultiplierWordCode (L : ℕ) (w : List ℕ)(hL : w.length ≤ L) (hw : ∀ a ∈ w, a ∈ multipliers) :decodeBoundedMultiplierWord (boundedMultiplierWordCode L w hL hw) = w := byexact List.ofFn_get wabbrev PositiveLoopOfReward (m : ℕ) :={r : FirstReturnLoop // firstReturnLoopReward r = m ∧0 < firstReturnLoopWeight r}noncomputable instance positiveLoopOfRewardFintype (m : ℕ) :Fintype (PositiveLoopOfReward m) := bylet encode : PositiveLoopOfReward m → BoundedMultiplierWord (69 * m) :=fun r ↦ boundedMultiplierWordCode (69 * m) (firstReturnLoopWord r.1)(by simpa [r.2.1] using firstReturnLoopWord_length_le r.1)(firstReturnLoopWord_mem_multipliers r.1)apply Fintype.ofInjective encodeintro r s hrsapply Subtype.extapply firstReturnLoopWord_injective_of_pos r.2.2 s.2.2have hdecode := congrArg decodeBoundedMultiplierWord hrssimpa [encode] using hdecodenoncomputable def firstReturnLoopCount (m : ℕ) : ℕ :=Fintype.card (PositiveLoopOfReward m)theorem loopRewardMass_eq_count (m : ℕ) :loopRewardMass m = (firstReturnLoopCount m : ℝ) / (q ^ m : ℕ) := byhave hsub : loopRewardMass m =∑' r : PositiveLoopOfReward m, firstReturnLoopWeight r.1 := byrw [loopRewardMass]calc(∑' r : FirstReturnLoop,selectedWeight (firstReturnLoopReward r = m) (firstReturnLoopWeight r)) =∑' r : FirstReturnLoop,({r | firstReturnLoopReward r = m ∧ 0 < firstReturnLoopWeight r} :Set FirstReturnLoop).indicator firstReturnLoopWeight r := byapply tsum_congrintro rby_cases hr : firstReturnLoopReward r = m· by_cases hp : 0 < firstReturnLoopWeight r· simp [selectedWeight, hr, hp]· have hz : firstReturnLoopWeight r = 0 :=le_antisymm (le_of_not_gt hp) (firstReturnLoopWeight_nonneg r)simp [selectedWeight, hr, hz]· simp [selectedWeight, hr]_ = ∑' r : PositiveLoopOfReward m, firstReturnLoopWeight r.1 :=(tsum_subtype{r : FirstReturnLoop | firstReturnLoopReward r = m ∧0 < firstReturnLoopWeight r} firstReturnLoopWeight).symmrw [hsub, tsum_fintype]have hterm (r : PositiveLoopOfReward m) :firstReturnLoopWeight r.1 = 1 / (q ^ m : ℕ) := byrw [firstReturnLoopWeight_eq_q_reward r.1 r.2.2, r.2.1]simp_rw [hterm]simp [firstReturnLoopCount, div_eq_mul_inv]/-! ## An elementary bounded-window renewal lemmaInstead of importing the arithmetic renewal theorem, we use the simpler factthat a positive-integer renewal process crosses every level, and finite meanmakes a fixed overshoot window carry uniformly positive mass. -/section AbstractRenewalvariable (p : ℕ → ℝ)variable (hp0 : ∀ n, 0 ≤ p n)variable (hp : Summable p)variable (hpMass : (∑' n, p n) = 1)variable (hpZero : p 0 = 0)noncomputable def distributionTail (d : ℕ) : ℝ :=∑' t, p (t + d)include hp0 intheorem distributionTail_nonneg (d : ℕ) :0 ≤ distributionTail p d := tsum_nonneg fun t ↦ hp0 (t + d)include hp intheorem distributionTail_summable (d : ℕ) :Summable fun t ↦ p (t + d) := (summable_nat_add_iff d).2 hpinclude hp intheorem distributionTail_succ (d : ℕ) :distributionTail p d = p d + distributionTail p (d + 1) := byrw [distributionTail, (distributionTail_summable p hp d).tsum_eq_zero_add]congr 1· simpapply tsum_congrintro tcongr 1omegainclude hp hpMass hpZero intheorem distributionTail_one : distributionTail p 1 = 1 := byhave h := distributionTail_succ p hp (d := 0)have hzero : distributionTail p 0 = 1 := byrw [distributionTail]simpa using hpMassrw [hzero, hpZero, zero_add] at hexact h.symmnoncomputable def natAddEquivIci (d : ℕ) : ℕ ≃ {j : ℕ // d ≤ j} wheretoFun t := ⟨d + t, by omega⟩invFun j := j.1 - dleft_inv t := by simpright_inv j := byapply Subtype.extsimp onlyomegatheorem distributionTail_eq_tsum_ge (d : ℕ) :distributionTail p d =∑' j, selectedWeight (d ≤ j) (p j) := bycalcdistributionTail p d = ∑' t : ℕ, p (d + t) := byapply tsum_congrintro trw [Nat.add_comm]_ = ∑' j : {j : ℕ // d ≤ j}, p j.1 :=(natAddEquivIci d).tsum_eq (fun j : {j : ℕ // d ≤ j} ↦ p j.1)_ = ∑' j : ℕ, Set.indicator {j : ℕ | d ≤ j} p j :=tsum_subtype {j : ℕ | d ≤ j} p_ = ∑' j : ℕ, selectedWeight (d ≤ j) (p j) := byapply tsum_congrintro jby_cases h : d ≤ j <;> simp [selectedWeight, Set.indicator, h]include hp intheorem distributionTail_split (d L : ℕ) :distributionTail p d =(∑ j ∈ Finset.range L, p (j + d)) + distributionTail p (d + L) := byhave h := (distributionTail_summable p hp d).sum_add_tsum_nat_add Lsimp only [distributionTail]rw [← h]congr 1apply tsum_congrintro jcongr 1omega/-- Renewal mass at a level, including the empty renewal at zero. -/noncomputable def renewalMass : ℕ → ℝ| 0 => 1| n + 1 => ∑ s : Fin (n + 1), renewalMass s * p (n + 1 - s)termination_by n => ndecreasing_by exact s.isLt@[simp] theorem renewalMass_zero : renewalMass p 0 = 1 := byrw [renewalMass]theorem renewalMass_succ (n : ℕ) : renewalMass p (n + 1) =∑ s ∈ Finset.range (n + 1), renewalMass p s * p (n + 1 - s) := byrw [renewalMass]exact Fin.sum_univ_eq_sum_range(fun s ↦ renewalMass p s * p (n + 1 - s)) (n + 1)include hp0 intheorem renewalMass_nonneg (n : ℕ) : 0 ≤ renewalMass p n := byinduction n using Nat.strong_induction_on with| h n ih =>cases n with| zero => simp| succ n =>rw [renewalMass_succ]exact Finset.sum_nonneg fun s hs ↦ mul_nonneg(ih s (by simpa using hs)) (hp0 _)noncomputable def crossingMass (M : ℕ) : ℝ :=∑ s ∈ Finset.range M, renewalMass p s * distributionTail p (M - s)include hp hpMass hpZero intheorem crossingMass_one {M : ℕ} (hM : 0 < M) :crossingMass p M = 1 := byinduction M, hM using Nat.le_induction with| base =>simp [crossingMass, distributionTail_one p hp hpMass hpZero]| succ M hM ih =>have hMpos : 0 < M := by omegahave htail (s : ℕ) (hs : s ∈ Finset.range M) :distributionTail p (M - s) =p (M - s) + distributionTail p (M + 1 - s) := byrw [distributionTail_succ p hp]simp only [Finset.mem_range] at hsrw [show M - s + 1 = M + 1 - s by omega]rw [crossingMass, Finset.sum_range_succ]simp only [Nat.add_sub_cancel_left]rw [distributionTail_one p hp hpMass hpZero, mul_one]have hrenew := renewalMass_succ p Mhave hsplit : crossingMass p M =(∑ s ∈ Finset.range M, renewalMass p s * p (M - s)) +∑ s ∈ Finset.range M,renewalMass p s * distributionTail p (M + 1 - s) := byrw [crossingMass, ← Finset.sum_add_distrib]apply Finset.sum_congr rflintro s hsrw [htail s hs, mul_add]rw [ih] at hsplitrw [show renewalMass p M =∑ s ∈ Finset.range M, renewalMass p s * p (M - s) bycases M with| zero => omega| succ m => exact renewalMass_succ p m]linarithinclude hp0 hp hpMass hpZero intheorem renewalMass_le_one (n : ℕ) : renewalMass p n ≤ 1 := bycases n with| zero => simp| succ n =>rw [renewalMass_succ]have hterm (s : ℕ) (hs : s ∈ Finset.range (n + 1)) :renewalMass p s * p (n + 1 - s) ≤renewalMass p s * distributionTail p (n + 1 - s) := byapply mul_le_mul_of_nonneg_left _ (renewalMass_nonneg p hp0 s)have ht := distributionTail_succ p hp (n + 1 - s)linarith [distributionTail_nonneg p hp0 (n + 1 - s + 1)]calc_ ≤ ∑ s ∈ Finset.range (n + 1),renewalMass p s * distributionTail p (n + 1 - s) :=Finset.sum_le_sum hterm_ = crossingMass p (n + 1) := rfl_ = 1 := crossingMass_one p hp hpMass hpZero (by omega)noncomputable def goodCrossingMass (M K : ℕ) : ℝ :=∑ s ∈ Finset.range M, renewalMass p s *∑ j ∈ Finset.range (K + 1), p (j + (M - s))noncomputable def badCrossingMass (M K : ℕ) : ℝ :=∑ s ∈ Finset.range M, renewalMass p s *distributionTail p (M + K + 1 - s)include hp intheorem good_add_bad_eq_crossing (M K : ℕ) :goodCrossingMass p M K + badCrossingMass p M K = crossingMass p M := byrw [goodCrossingMass, badCrossingMass, crossingMass,← Finset.sum_add_distrib]apply Finset.sum_congr rflintro s hsrw [← mul_add]have hsplit := distributionTail_split p hp (M - s) (K + 1)have heq : M - s + (K + 1) = M + K + 1 - s := bysimp only [Finset.mem_range] at hsomegarw [heq] at hsplitrw [hsplit]include hp0 intheorem goodCrossingMass_le_window {M : ℕ} (hM : 0 < M) (K : ℕ) :goodCrossingMass p M K ≤∑ j ∈ Finset.range (K + 1), renewalMass p (M + j) := byrw [goodCrossingMass]simp_rw [Finset.mul_sum]rw [Finset.sum_comm]apply Finset.sum_le_sumintro j hjhave hrec : renewalMass p (M + j) =∑ s ∈ Finset.range (M + j), renewalMass p s * p (M + j - s) := bycases M with| zero => omega| succ M =>rw [Nat.succ_add]exact renewalMass_succ p (M + j)rw [hrec]have hrewrite :(∑ s ∈ Finset.range M, renewalMass p s * p (j + (M - s))) =∑ s ∈ Finset.range M, renewalMass p s * p (M + j - s) := byapply Finset.sum_congr rflintro s hscongr 2simp only [Finset.mem_range] at hsomegarw [hrewrite]apply Finset.sum_le_sum_of_subset_of_nonneg· intro s hssimp only [Finset.mem_range] at hs ⊢omega· intro s hs _exact mul_nonneg (renewalMass_nonneg p hp0 s) (hp0 _)noncomputable def badJumpMass (M K j : ℕ) : ℝ :=∑ s ∈ Finset.range M,selectedWeight (M + K + 1 ≤ s + j) (renewalMass p s * p j)include hp0 intheorem badJumpMass_nonneg (M K j : ℕ) :0 ≤ badJumpMass p M K j := byexact Finset.sum_nonneg fun s _ ↦ selectedWeight_nonneg(mul_nonneg (renewalMass_nonneg p hp0 s) (hp0 j))include hp0 hp hpMass hpZero intheorem badJumpMass_summable (M K : ℕ) :Summable fun j ↦ badJumpMass p M K j := byapply Summable.of_nonneg_of_le (badJumpMass_nonneg p hp0 M K)· intro jcalcbadJumpMass p M K j ≤ ∑ _s ∈ Finset.range M, p j := byapply Finset.sum_le_sumintro s _refine (selectedWeight_le(mul_nonneg (renewalMass_nonneg p hp0 s) (hp0 j))).trans ?_simpa only [one_mul] using mul_le_mul_of_nonneg_right(renewalMass_le_one p hp0 hp hpMass hpZero s) (hp0 j)_ = (M : ℝ) * p j := by simp· exact hp.mul_left (M : ℝ)include hp0 hp intheorem badCrossingMass_eq_tsum_badJump (M K : ℕ) :badCrossingMass p M K = ∑' j, badJumpMass p M K j := byhave hbase (s : ℕ) (hs : s ∈ Finset.range M) : Summable fun j ↦selectedWeight (M + K + 1 - s ≤ j) (p j) := byapply Summable.of_nonneg_of_le(fun j ↦ selectedWeight_nonneg (hp0 j))(fun j ↦ selectedWeight_le (hp0 j))exact hphave hsumm (s : ℕ) (hs : s ∈ Finset.range M) : Summable fun j ↦selectedWeight (M + K + 1 - s ≤ j) (renewalMass p s * p j) := byapply Summable.of_nonneg_of_le(fun j ↦ selectedWeight_nonneg(mul_nonneg (renewalMass_nonneg p hp0 s) (hp0 j)))(fun j ↦ selectedWeight_le(mul_nonneg (renewalMass_nonneg p hp0 s) (hp0 j)))exact hp.mul_left (renewalMass p s)have hmul (s : ℕ) (hs : s ∈ Finset.range M) :renewalMass p s *(∑' j, selectedWeight (M + K + 1 - s ≤ j) (p j)) =∑' j, selectedWeight (M + K + 1 - s ≤ j)(renewalMass p s * p j) := byrw [← (hbase s hs).tsum_mul_left]apply tsum_congrintro jexact (selectedWeight_mul_left _ _ _).symmrw [badCrossingMass]simp_rw [distributionTail_eq_tsum_ge p]have hfinite :(∑ s ∈ Finset.range M, renewalMass p s *∑' j, selectedWeight (M + K + 1 - s ≤ j) (p j)) =∑ s ∈ Finset.range M, ∑' j,selectedWeight (M + K + 1 - s ≤ j)(renewalMass p s * p j) := byapply Finset.sum_congr rflintro s hsexact hmul s hsrw [hfinite]rw [← Summable.tsum_finsetSum hsumm]apply tsum_congrintro jsimp only [badJumpMass]apply Finset.sum_congr rflintro s hscongr 1apply propextexact Nat.sub_le_iff_le_add'include hp0 hp hpMass hpZero intheorem badJumpMass_le (M K j : ℕ) :badJumpMass p M K j ≤ (j : ℝ) * p j := byclassicalby_cases hM : M = 0· subst Msimpa [badJumpMass] using(mul_nonneg (Nat.cast_nonneg j) (hp0 j))let E := (Finset.range M).filter fun s ↦ M + K + 1 ≤ s + jhave hrewrite : badJumpMass p M K j =∑ s ∈ E, renewalMass p s * p j := bysimp only [badJumpMass, E, selectedWeight]rw [Finset.sum_filter]apply Finset.sum_congr rflintro s hsby_cases h : M + K + 1 ≤ s + j <;> simp only [h, if_true, if_false]rw [hrewrite]have hcard : E.card ≤ j := byhave hsub : E ⊆ Finset.Icc (M + K + 1 - j) (M - 1) := byintro s hssimp only [E, Finset.mem_filter, Finset.mem_range] at hssimp only [Finset.mem_Icc]omegahave hc := Finset.card_le_card hsubrw [Nat.card_Icc] at hcomegacalc(∑ s ∈ E, renewalMass p s * p j) ≤ ∑ _s ∈ E, p j := byapply Finset.sum_le_sumintro s _simpa only [one_mul] using mul_le_mul_of_nonneg_right(renewalMass_le_one p hp0 hp hpMass hpZero s) (hp0 j)_ = (E.card : ℝ) * p j := by simp_ ≤ (j : ℝ) * p j := byexact mul_le_mul_of_nonneg_right (by exact_mod_cast hcard) (hp0 j)theorem badJumpMass_zero_of_le (M K j : ℕ) (hj : j ≤ K) :badJumpMass p M K j = 0 := byapply Finset.sum_eq_zerointro s hshave hslt : s < M := by simpa using hshave hnot : ¬M + K + 1 ≤ s + j := by omegasimp [selectedWeight, hnot]noncomputable def weightedDistributionTail (K : ℕ) : ℝ :=∑' t, ((t + K + 1 : ℕ) : ℝ) * p (t + K + 1)theorem weightedDistributionTail_summable(hpw : Summable fun j : ℕ ↦ (j : ℝ) * p j) (K : ℕ) :Summable fun t ↦ ((t + K + 1 : ℕ) : ℝ) * p (t + K + 1) := bysimpa only [Nat.add_assoc] using(summable_nat_add_iff (K + 1)).2 hpwset_option maxHeartbeats 800000 in-- Expanding the two infinite sums in the crossing estimate needs a larger bound.include hp0 hp hpMass hpZero intheorem badCrossingMass_le_weightedTail(hpw : Summable fun j : ℕ ↦ (j : ℝ) * p j) (M K : ℕ) :badCrossingMass p M K ≤ weightedDistributionTail p K := byrw [badCrossingMass_eq_tsum_badJump p hp0 hp M K]have hg := badJumpMass_summable p hp0 hp hpMass hpZero M Khave hsplit := hg.sum_add_tsum_nat_add (K + 1)have hhead : (∑ j ∈ Finset.range (K + 1), badJumpMass p M K j) = 0 := byapply Finset.sum_eq_zerointro j hjapply badJumpMass_zero_of_le p M K jsimpa only [Finset.mem_range, Nat.lt_succ_iff] using hjrw [hhead, zero_add] at hsplitrw [← hsplit]have hbadTail : Summable fun t ↦ badJumpMass p M K (t + (K + 1)) :=(summable_nat_add_iff (K + 1)).2 hgexact hbadTail.tsum_le_tsum(fun t ↦ badJumpMass_le p hp0 hp hpMass hpZero M K (t + (K + 1)))(weightedDistributionTail_summable p hpw K)theorem weightedDistributionTail_tendsto_zero :Filter.Tendsto (weightedDistributionTail p) Filter.atTop (nhds 0) := bylet f : ℕ → ℝ := fun j ↦ (j : ℝ) * p jhave hbase := tendsto_sum_nat_add fhave hshift := (Filter.tendsto_add_atTop_iff_nat 1).mpr hbasechange Filter.Tendsto(fun n ↦ ∑' k : ℕ, ((k + (n + 1) : ℕ) : ℝ) * p (k + (n + 1)))Filter.atTop (nhds 0)exact hshiftinclude hp0 hp hpMass hpZero intheorem exists_uniform_renewal_window(hpw : Summable fun j : ℕ ↦ (j : ℝ) * p j) :∃ K : ℕ, ∀ M : ℕ, 0 < M →(1 : ℝ) / 2 <∑ j ∈ Finset.range (K + 1), renewalMass p (M + j) := byhave hevent : ∀ᶠ K : ℕ in Filter.atTop,weightedDistributionTail p K < (1 : ℝ) / 2 :=(weightedDistributionTail_tendsto_zero p).eventually(by exact Iio_mem_nhds (by norm_num : (0 : ℝ) < 1 / 2))obtain ⟨K, hK⟩ := hevent.existsrefine ⟨K, ?_⟩intro M hMhave hcross := crossingMass_one p hp hpMass hpZero hMhave hsplit := good_add_bad_eq_crossing p hp M Khave hbad := badCrossingMass_le_weightedTail p hp0 hp hpMass hpZero hpw M Khave hgood : (1 : ℝ) / 2 < goodCrossingMass p M K := bylinarithexact hgood.trans_le (goodCrossingMass_le_window p hp0 hM K)end AbstractRenewal/-! ## Finite concatenations of first-return loops -//-- Ordered concatenations whose total reward is exactly `n`. In thesuccessor case, `s` is the reward left for the tail. -/inductive LoopSequence : ℕ → Type| nil : LoopSequence 0| cons {n : ℕ} (s : Fin (n + 1))(atom : PositiveLoopOfReward (n + 1 - s.1))(tail : LoopSequence s.1) : LoopSequence (n + 1)def loopSequenceZeroEquiv : Unit ≃ LoopSequence 0 wheretoFun _ := .nilinvFun r := by cases r; exact ()left_inv u := by cases u; rflright_inv r := by cases r; rfldef loopSequenceSuccEquiv (n : ℕ) :(Σ s : Fin (n + 1),PositiveLoopOfReward (n + 1 - s.1) × LoopSequence s.1) ≃LoopSequence (n + 1) wheretoFun r := .cons r.1 r.2.1 r.2.2invFun r := bycases r with| cons s atom tail => exact ⟨s, atom, tail⟩left_inv r := by cases r; rflright_inv r := by cases r; rflnoncomputable instance loopSequenceFintype : (n : ℕ) → Fintype (LoopSequence n)| 0 => Fintype.ofEquiv Unit loopSequenceZeroEquiv| n + 1 => byletI (s : Fin (n + 1)) : Fintype (LoopSequence s.1) :=loopSequenceFintype s.1exact Fintype.ofEquiv(Σ s : Fin (n + 1),PositiveLoopOfReward (n + 1 - s.1) × LoopSequence s.1)(loopSequenceSuccEquiv n)termination_by n => ndecreasing_by exact s.2noncomputable def loopSequenceCount (n : ℕ) : ℕ :=Fintype.card (LoopSequence n)@[simp] theorem loopSequenceCount_zero : loopSequenceCount 0 = 1 := byunfold loopSequenceCountrw [Fintype.card_congr loopSequenceZeroEquiv.symm]exact Fintype.card_unittheorem loopSequenceCount_succ (n : ℕ) :loopSequenceCount (n + 1) =∑ s ∈ Finset.range (n + 1),firstReturnLoopCount (n + 1 - s) * loopSequenceCount s := byunfold loopSequenceCountrw [Fintype.card_congr (loopSequenceSuccEquiv n).symm]simp only [Fintype.card_sigma, Fintype.card_prod, firstReturnLoopCount]exact Fin.sum_univ_eq_sum_range(fun s : ℕ => Fintype.card (PositiveLoopOfReward (n + 1 - s)) *Fintype.card (LoopSequence s)) (n + 1)theorem q_pos : 0 < q := by norm_num [q]/-- The renewal mass is exactly the number of ordered loop concatenationsdivided by their common reciprocal slope. -/theorem renewalMass_eq_sequenceCount (n : ℕ) :renewalMass loopRewardMass n = (loopSequenceCount n : ℝ) / (q ^ n : ℕ) := byinduction n using Nat.strong_induction_on with| h n ih =>cases n with| zero => simp| succ n =>rw [renewalMass_succ, loopSequenceCount_succ]rw [Nat.cast_sum]simp only [Nat.cast_mul]rw [Finset.sum_div]apply Finset.sum_congr rflintro s hshave hslt : s < n + 1 := by simpa using hsrw [ih s hslt, loopRewardMass_eq_count]have hsum : s + (n + 1 - s) = n + 1 := by omegapush_castrw [div_mul_div_comm, ← pow_add, hsum]ringnoncomputable def loopSequenceChoices : {n : ℕ} → LoopSequence n → List BlockChoice| 0, .nil => []| _ + 1, .cons _ atom tail =>recurrentLoopRecordChoices atom.1.2.2 ++ loopSequenceChoices tailnoncomputable def loopSequenceWord : {n : ℕ} → LoopSequence n → List ℕ| 0, .nil => []| _ + 1, .cons _ atom tail =>firstReturnLoopWord atom.1 ++ loopSequenceWord tailtheorem controlledListWord_append (x : AugmentedState) (u v : List BlockChoice) :controlledListWord x (u ++ v) = controlledListWord x u ++controlledListWord (controlledChain.listEndpoint x u) v := byinduction u generalizing x with| nil => rfl| cons a u ih =>simp only [List.cons_append, controlledListWord]rw [ih]simp only [FiniteChoiceChain.listEndpoint]rw [List.append_assoc]rfltheorem inverseWord_append (u v : List ℕ) (z : ℚ) :inverseWord (u ++ v) z = inverseWord u (inverseWord v z) := byinduction u with| nil => rfl| cons a u ih => simp [inverseWord, ih]@[simp] theorem controlledListWord_length (x : AugmentedState)(u : List BlockChoice) :(controlledListWord x u).length = controlBlockLength * u.length := byinduction u generalizing x with| nil => simp [controlledListWord]| cons a u ih =>simp only [controlledListWord, List.length_append, blockWord_length,List.length_cons, ih]ringtheorem controlledListWord_mem_multipliers (x : AugmentedState)(u : List BlockChoice) :∀ a ∈ controlledListWord x u, a ∈ multipliers := byhave h := controlledPathWord_mem_multipliers u.length x u.getsimpa only [← controlledListWord_ofFn, List.ofFn_get] using htheorem controlledListWord_prod_two_le (x : AugmentedState){u : List BlockChoice} (hu : u ≠ []) : 2 ≤ (controlledListWord x u).prod := byapply prod_two_le_of_ne_nil· intro hnilhave hlen := congrArg List.length hnilsimp only [controlledListWord_length, List.length_nil] at hlenhave hN := controlBlockLength_poshave hulen : 0 < u.length := List.length_pos_iff.mpr huhave hprod : 0 < controlBlockLength * u.length := Nat.mul_pos hN hulenomega· intro a haexact mem_multipliers_two_le (controlledListWord_mem_multipliers x u a ha)theorem controlledList_inverse_mem (x : AugmentedState)(u : List BlockChoice) (hu : 0 < controlledChain.listWeight x u){z : ℚ} (hz : z ∈ K (controlledChain.listEndpoint x u).1) :inverseWord (controlledListWord x u) z ∈ K x.1 := byhave hpath : 0 < controlledChain.pathWeight u.length x u.get := byrwa [← controlledChain.listWeight_ofFn, List.ofFn_get]have hmem := (controlledIntervalPath u.length x u.get hpath).inverseWord_mem_source(by simpa only [← controlledChain.listEndpoint_ofFn, List.ofFn_get] using hz)simpa only [controlledIntervalPath_word, ← controlledListWord_ofFn,List.ofFn_get] using hmemset_option maxHeartbeats 800000 in-- The proof recursively peels variable-length blocks and normalizes their products./-- Two positive controlled paths with the same pulled-back point and thesame slope have identical block choices. This is the graph-directedinjectivity statement that excludes global nonfree affine identities. -/theorem controlledLists_eq_of_inverse_eq (x : AugmentedState)(u v : List BlockChoice)(hu : 0 < controlledChain.listWeight x u)(hv : 0 < controlledChain.listWeight x v){zu zv : ℚ}(hzu : zu ∈ K (controlledChain.listEndpoint x u).1)(hzv : zv ∈ K (controlledChain.listEndpoint x v).1)(hinv : inverseWord (controlledListWord x u) zu =inverseWord (controlledListWord x v) zv)(hprod : (controlledListWord x u).prod =(controlledListWord x v).prod) : u = v ∧ zu = zv := byinduction u generalizing x v zu zv with| nil =>cases v with| nil => exact ⟨rfl, by simpa [controlledListWord, inverseWord] using hinv⟩| cons b v =>have htwo := controlledListWord_prod_two_le x(u := b :: v) (by simp)change 1 = (controlledListWord x (b :: v)).prod at hprodrw [← hprod] at htwoomega| cons a u ih =>cases v with| nil =>have htwo := controlledListWord_prod_two_le x(u := a :: u) (by simp)change (controlledListWord x (a :: u)).prod = 1 at hprodrw [hprod] at htwoomega| cons b v =>simp only [FiniteChoiceChain.listWeight] at hu hvhave hpa0 := controlledChain.probability_nonneg x ahave hpb0 := controlledChain.probability_nonneg x bhave htu0 := controlledChain.listWeight_nonneg (controlledNext x a) uhave htv0 := controlledChain.listWeight_nonneg (controlledNext x b) vhave hpa : 0 < controlledChain.probability x a :=pos_of_mul_pos_left hu htu0have hpb : 0 < controlledChain.probability x b :=pos_of_mul_pos_left hv htv0have htu : 0 < controlledChain.listWeight (controlledNext x a) u :=pos_of_mul_pos_right hu hpa0have htv : 0 < controlledChain.listWeight (controlledNext x b) v :=pos_of_mul_pos_right hv hpb0let yu := inverseWord (controlledListWord (controlledNext x a) u) zulet yv := inverseWord (controlledListWord (controlledNext x b) v) zvhave hyu : yu ∈ K (controlledNext x a).1 :=controlledList_inverse_mem (controlledNext x a) u htu hzuhave hyv : yv ∈ K (controlledNext x b).1 :=controlledList_inverse_mem (controlledNext x b) v htv hzvhave hblockA : 0 < blockWeight (feedback x.2) controlBlockLength x.1 a := bychange 0 < (blockWeight (feedback x.2) controlBlockLength x.1 a : ℝ)at hpaexact_mod_cast hpahave hblockB : 0 < blockWeight (feedback x.2) controlBlockLength x.1 b := bychange 0 < (blockWeight (feedback x.2) controlBlockLength x.1 b : ℝ)at hpbexact_mod_cast hpbhave hblockInv : inverseWord(blockWord (feedback x.2) controlBlockLength x.1 a) yu =inverseWord (blockWord (feedback x.2) controlBlockLength x.1 b) yv := bysimpa only [controlledListWord, inverseWord_append, yu, yv] using hinvhave hblock := blockChoice_eq_of_inverse_eq (feedback x.2)controlBlockLength x.1 hblockA hblockB hyu hyv hblockInvhave hab := hblock.1subst bhave htailProd :(controlledListWord (controlledNext x a) u).prod =(controlledListWord (controlledNext x a) v).prod := bysimp only [controlledListWord, List.prod_append] at hprodhave hblockProd : 0 <(blockWord (feedback x.2) controlBlockLength x.1 a).prod := byexact List.prod_pos (fun z hz ↦ byhave hz2 := mem_multipliers_two_le(blockWord_mem_multipliers _ _ _ _ z hz)omega)exact Nat.eq_of_mul_eq_mul_left hblockProd hprodhave hrec := ih (x := controlledNext x a) (v := v)htu htv hzu hzv hblock.2 htailProdexact ⟨congrArg (List.cons a) hrec.1, hrec.2⟩theorem loopSequence_endpoint {n : ℕ} (r : LoopSequence n) :controlledChain.listEndpoint recurrentAugmentedState(loopSequenceChoices (n := n) r) =recurrentAugmentedState := byinduction r with| nil => rfl| cons s atom tail ih =>rw [loopSequenceChoices, controlledChain.listEndpoint_append,recurrentLoopRecord_endpoint atom.1.2.2]exact ihtheorem loopSequence_listWeight_pos {n : ℕ} (r : LoopSequence n) :0 < controlledChain.listWeight recurrentAugmentedState(loopSequenceChoices (n := n) r) := byinduction r with| nil => simp [loopSequenceChoices, FiniteChoiceChain.listWeight]| cons s atom tail ih =>rw [loopSequenceChoices, controlledChain.listWeight_append,recurrentLoopRecord_endpoint atom.1.2.2,← recurrentLoopRecordWeight_eq_listWeight]exact mul_pos atom.2.2 ihtheorem loopSequenceWord_eq_controlled {n : ℕ} (r : LoopSequence n) :loopSequenceWord (n := n) r = controlledListWord recurrentAugmentedState(loopSequenceChoices (n := n) r) := byinduction r with| nil => rfl| cons s atom tail ih =>simp only [loopSequenceWord, loopSequenceChoices]rw [controlledListWord_append, recurrentLoopRecord_endpoint, ← ih]rfltheorem loopSequenceWord_mem_multipliers {n : ℕ} (r : LoopSequence n) :∀ a ∈ loopSequenceWord (n := n) r, a ∈ multipliers := byinduction r with| nil => simp [loopSequenceWord]| cons s atom tail ih =>simp only [loopSequenceWord, List.mem_append]intro a harcases ha with ha | ha· exact firstReturnLoopWord_mem_multipliers atom.1 a ha· exact ih a hatheorem loopSequenceWord_prod {n : ℕ} (r : LoopSequence n) :(loopSequenceWord (n := n) r).prod = q ^ n := byinduction r with| nil => simp [loopSequenceWord]| @cons n s atom tail ih =>simp only [loopSequenceWord, List.prod_append]have hatom := recurrentLoopRecord_prod_eq_q_pow atom.1.2.2change (firstReturnLoopWord atom.1).prod = q ^ firstReturnLoopReward atom.1at hatomrw [hatom, atom.2.1, ih, ← pow_add]congr 1omegatheorem loopSequenceChoices_injective (n : ℕ) :Function.Injective (loopSequenceChoices (n := n) :LoopSequence n → List BlockChoice) := byinduction n using Nat.strong_induction_on with| h n ih =>intro r t hchoicescases r with| nil => cases t; rfl| @cons n s atom tail =>cases t with| @cons _ s' atom' tail' =>simp only [loopSequenceChoices] at hchoiceshave hret := recurrentLoopRecord_isReturnBase atom.1.2.2have hret' := recurrentLoopRecord_isReturnBase atom'.1.2.2have hsplit := controlledChain.isReturnList_append_unique{recurrentAugmentedState} hret hret' hchoiceshave hatomVal : atom.1 = atom'.1 :=recurrentLoopRecord_sigma_choices_injective hsplit.1have hs : s = s' := byapply Fin.exthave hr := atom.2.1have ht := atom'.2.1rw [hatomVal] at hromegasubst s'have hatom : atom = atom' := Subtype.ext hatomValsubst atom'have htail : tail = tail' := ih s.1 s.2 hsplit.2subst tail'rfltheorem controlledListWord_injective_of_return {u v : List BlockChoice}(hu : 0 < controlledChain.listWeight recurrentAugmentedState u)(hv : 0 < controlledChain.listWeight recurrentAugmentedState v)(huend : controlledChain.listEndpoint recurrentAugmentedState u =recurrentAugmentedState)(hvend : controlledChain.listEndpoint recurrentAugmentedState v =recurrentAugmentedState)(hword : controlledListWord recurrentAugmentedState u =controlledListWord recurrentAugmentedState v) : u = v := byhave hz : intervalMidpoint recurrentAugmentedState.1 ∈K recurrentAugmentedState.1 := intervalMidpoint_mem _have hinv := congrArg(fun w ↦ inverseWord w (intervalMidpoint recurrentAugmentedState.1)) hwordhave hprod := congrArg List.prod hwordexact (controlledLists_eq_of_inverse_eq recurrentAugmentedState u v hu hv(by simpa [huend] using hz) (by simpa [hvend] using hz)hinv hprod).1theorem loopSequenceWord_injective (n : ℕ) :Function.Injective (loopSequenceWord (n := n) : LoopSequence n → List ℕ) := byintro r s hwordapply loopSequenceChoices_injective napply controlledListWord_injective_of_return· exact loopSequence_listWeight_pos r· exact loopSequence_listWeight_pos s· exact loopSequence_endpoint r· exact loopSequence_endpoint s· simpa only [← loopSequenceWord_eq_controlled] using hwordtheorem inverseWord_eq_sub_div (w : List ℕ) (z : ℚ)(hw : ∀ a ∈ w, 0 < a) :inverseWord w z = (z - (offset w : ℚ)) / (w.prod : ℚ) := byinduction w with| nil => simp [inverseWord, offset]| cons a w ih =>have ha : 0 < a := hw a (by simp)have htail : ∀ b ∈ w, 0 < b := byintro b hbexact hw b (by simp [hb])have hprod : (w.prod : ℚ) ≠ 0 := byhave hprodNat : w.prod ≠ 0 := byapply List.prod_ne_zerointro hzeroexact (htail 0 hzero).ne' rflexact_mod_cast hprodNathave haQ : (a : ℚ) ≠ 0 := by exact_mod_cast ha.ne'rw [inverseWord, ih htail]simp only [inverseT, offset, Int.cast_sub, Int.cast_natCast,List.prod_cons, Nat.cast_mul]field_simpring/-- At fixed total reward, different loop concatenations have differentaffine offsets. -/theorem loopSequence_offset_injective (n : ℕ) :Set.InjOn (fun r : LoopSequence n ↦offset (loopSequenceWord (n := n) r)) Set.univ := byintro r _ s _ hoffsetchange offset (loopSequenceWord (n := n) r) =offset (loopSequenceWord (n := n) s) at hoffsetapply loopSequenceChoices_injective nhave hrpos : ∀ a ∈ loopSequenceWord (n := n) r, 0 < a := byintro a hahave ha2 := mem_multipliers_two_le (loopSequenceWord_mem_multipliers r a ha)omegahave hspos : ∀ a ∈ loopSequenceWord (n := n) s, 0 < a := byintro a hahave ha2 := mem_multipliers_two_le (loopSequenceWord_mem_multipliers s a ha)omegahave hprod : (loopSequenceWord (n := n) r).prod =(loopSequenceWord (n := n) s).prod := byrw [loopSequenceWord_prod, loopSequenceWord_prod]have hinv : inverseWord (loopSequenceWord (n := n) r)(intervalMidpoint recurrentAugmentedState.1) =inverseWord (loopSequenceWord (n := n) s)(intervalMidpoint recurrentAugmentedState.1) := byrw [inverseWord_eq_sub_div _ _ hrpos, inverseWord_eq_sub_div _ _ hspos,hoffset, hprod]have hchoices := controlledLists_eq_of_inverse_eq recurrentAugmentedState(loopSequenceChoices (n := n) r) (loopSequenceChoices (n := n) s)(loopSequence_listWeight_pos r) (loopSequence_listWeight_pos s)(by rw [loopSequence_endpoint]; exact intervalMidpoint_mem _)(by rw [loopSequence_endpoint]; exact intervalMidpoint_mem _)(by simpa only [← loopSequenceWord_eq_controlled] using hinv)(by simpa only [← loopSequenceWord_eq_controlled] using hprod)exact hchoices.1noncomputable def loopSequenceWordFamily (n : ℕ) : Finset (List ℕ) :=Finset.univ.image (loopSequenceWord : LoopSequence n → List ℕ)theorem loopSequenceWordFamily_card (n : ℕ) :(loopSequenceWordFamily n).card = loopSequenceCount n := byrw [loopSequenceWordFamily, Finset.card_image_of_injective _(loopSequenceWord_injective n)]rfltheorem mem_loopSequenceWordFamily {n : ℕ} {w : List ℕ}(hw : w ∈ loopSequenceWordFamily n) :∃ r : LoopSequence n, loopSequenceWord (n := n) r = w := bysimpa [loopSequenceWordFamily] using hwtheorem loopSequenceWordFamily_data {n : ℕ} (hn : 0 < n) :(∀ w ∈ loopSequenceWordFamily n,w ≠ [] ∧ (∀ a ∈ w, a ∈ multipliers) ∧ w.prod = q ^ n) ∧(∀ u ∈ loopSequenceWordFamily n, ∀ v ∈ loopSequenceWordFamily n,u ≠ v → offset u ≠ offset v) := byconstructor· intro w hwobtain ⟨r, rfl⟩ := mem_loopSequenceWordFamily hwrefine ⟨?_, loopSequenceWord_mem_multipliers r, loopSequenceWord_prod r⟩intro hemptyhave hprod := loopSequenceWord_prod rrw [hempty, List.prod_nil] at hprodhave hq : 1 < q ^ n := one_lt_pow₀ (by norm_num [q] : 1 < q) hn.ne'omega· intro u hu v hv huv hoffobtain ⟨r, rfl⟩ := mem_loopSequenceWordFamily huobtain ⟨s, rfl⟩ := mem_loopSequenceWordFamily hvhave hrs := loopSequence_offset_injective n (Set.mem_univ r)(Set.mem_univ s) hoffexact huv (congrArg loopSequenceWord hrs)/-- Pigeonholing the fixed renewal window selects one common slope at everyscale. -/theorem exists_loopSequence_count_at_scale :∃ K : ℕ, ∀ M : ℕ, 0 < M → ∃ j ∈ Finset.range (K + 1),q ^ (M + j) < 2 * (K + 1) * loopSequenceCount (M + j) := byobtain ⟨K, hK⟩ := exists_uniform_renewal_window loopRewardMassloopRewardMass_nonneg loopRewardMass_summable tsum_loopRewardMassloopRewardMass_zero weighted_loopRewardMass_summablerefine ⟨K, ?_⟩intro M hMhave hwindow := hK M hMby_contra hnonepush Not at hnonehave hterm (j : ℕ) (hj : j ∈ Finset.range (K + 1)) :renewalMass loopRewardMass (M + j) ≤(1 : ℝ) / (2 * (K + 1)) := byrw [renewalMass_eq_sequenceCount]have hjbound := hnone j hjhave hqReal : (0 : ℝ) < q := by exact_mod_cast q_poshave hq : (0 : ℝ) < (q ^ (M + j) : ℕ) := byexact_mod_cast pow_pos q_pos (M + j)have hjReal : (2 * (K + 1) : ℝ) * loopSequenceCount (M + j) ≤(q ^ (M + j) : ℕ) := by exact_mod_cast hjboundhave hD : (0 : ℝ) < 2 * (K + 1) := by positivityapply (div_le_div_iff₀ hq hD).2simpa [mul_assoc, mul_comm, mul_left_comm] using hjRealhave hsum : (∑ j ∈ Finset.range (K + 1),renewalMass loopRewardMass (M + j)) ≤ (1 : ℝ) / 2 := bycalc_ ≤ ∑ _j ∈ Finset.range (K + 1),(1 : ℝ) / (2 * (K + 1)) := Finset.sum_le_sum hterm_ = (1 : ℝ) / 2 := bysimpfield_simplinarith/-! ## Unconditional completion of the proof -/theorem q_one_lt_unconditional : 1 < q := by norm_num [q]theorem geometric_count_from_loop_sequences :∃ denominator r₀ : ℕ, 0 < denominator ∧ ∀ r ≥ r₀,q ^ r ≤ denominator * countUpTo (17 * q ^ r) := byclassicalobtain ⟨K, hscale⟩ := exists_loopSequence_count_at_scalelet D : ℕ := 2 * (K + 1)let denominator : ℕ := q ^ K * Drefine ⟨denominator, K + 1, ?_, ?_⟩· dsimp [denominator, D]exact Nat.mul_pos (pow_pos q_pos K) (by omega)· intro r hrlet M := r - Khave hM : 0 < M := bydsimp [M]omegahave hrEq : M + K = r := bydsimp [M]omegaobtain ⟨j, hj, hcount⟩ := hscale M hMhave hjle : j ≤ K := bysimpa only [Finset.mem_range, Nat.lt_succ_iff] using hjlet n := M + jlet W := loopSequenceWordFamily nhave hn : 0 < n := by dsimp [n]; omegahave hdata := loopSequenceWordFamily_data hnlet V : Finset ℕ := W.image fun w ↦ eval w 17have hinj : Set.InjOn (fun w : List ℕ ↦ eval w 17) W := byintro u hu v hv hevalby_contra huvhave huData := hdata.1 u huhave hvData := hdata.1 v hvhave huTwo : ∀ a ∈ u, 2 ≤ a := byintro a haexact mem_multipliers_two_le (huData.2.1 a ha)have hvTwo : ∀ a ∈ v, 2 ≤ a := byintro a haexact mem_multipliers_two_le (hvData.2.1 a ha)have hprod : u.prod = v.prod := huData.2.2.trans hvData.2.2.symmexact (eval_ne_of_prod_eq_offset_ne (by norm_num) huTwo hvTwo hprod(hdata.2 u hu v hv huv)) hevalhave hVcard : V.card = W.card := Finset.card_image_of_injOn hinjhave hV : ∀ x ∈ V, Generated x ∧ x ≤ 17 * q ^ r := byintro x hxsimp only [V, Finset.mem_image] at hxobtain ⟨w, hw, rfl⟩ := hxhave hwData := hdata.1 w hwhave hgenerated : Generated (eval w 17) :=eval_at_seventeen_generated w hwData.2.1have hone : ∀ a ∈ w, 1 ≤ a := byintro a haexact (mem_multipliers_two_le (hwData.2.1 a ha)).trans' (by omega)have heval : eval w 17 ≤ w.prod * 17 := eval_le_prod_mul w honerw [hwData.2.2] at hevalhave hnle : n ≤ r := bydsimp [n]omegahave hpow : q ^ n ≤ q ^ r := Nat.pow_le_pow_right q_pos hnleexact ⟨hgenerated, heval.trans (bysimpa [mul_comm] using Nat.mul_le_mul_left 17 hpow)⟩have hVle : W.card ≤ countUpTo (17 * q ^ r) := byrw [← hVcard]exact card_le_countUpTo hVhave hWcard : W.card = loopSequenceCount n := bydsimp [W]exact loopSequenceWordFamily_card nhave hscaleNat : q ^ n ≤ D * W.card := byrw [hWcard]dsimp [n, D] at hcount ⊢omegahave hMcount : q ^ M ≤ D * countUpTo (17 * q ^ r) := byhave hpowM : q ^ M ≤ q ^ n := byapply Nat.pow_le_pow_right q_posdsimp [n]omegaexact hpowM.trans (hscaleNat.trans (Nat.mul_le_mul_left D hVle))dsimp [denominator]calcq ^ r = q ^ (M + K) := by rw [hrEq]_ = q ^ M * q ^ K := pow_add q M K_ ≤ (D * countUpTo (17 * q ^ r)) * q ^ K :=Nat.mul_le_mul_right (q ^ K) hMcount_ = q ^ K * D * countUpTo (17 * q ^ r) := by ring/-- Erdős Problem 424, fully formalized with no recurrence or renewalassumption left as a hypothesis. -/theorem theorem_one : HasPositiveLowerDensity := byobtain ⟨denominator, r₀, hdenominator, hgeom⟩ :=geometric_count_from_loop_sequencesexact positiveLowerDensity_of_geometric_count q_one_lt_unconditionalhdenominator ⟨r₀, hgeom⟩open Set/-!## Compatibility with `formal-conjectures`The three definitions below are the definitions used in`FormalConjectures/ErdosProblems/424.lean`. They describe the generated setby finite stages, whereas the main development uses the inductive predicate`Generated`.The linked conjecture currently uses `Set.HasPosDensity`, which asks for theexistence of a full natural-density limit. Erdős's intended question, andthe theorem proved by Hofstadter's argument, concern positive lower density.The theorem `erdos_424` below states that conclusion directlyfor the `formal-conjectures` generated set.-//-- The new numbers obtained from `A` by applying `x * y - 1` to distinctmembers of `A`. This agrees verbatim with the `formal-conjectures`definition. -/def nextGeneration (A : Set ℕ) : Set ℕ :={z : ℕ | ∃ x y, x ∈ A ∧ y ∈ A ∧ x ≠ y ∧ z = x * y - 1}/-- The finite-stage closure used by `formal-conjectures`. -/def sequenceSet (n : ℕ) : Set ℕ :=Nat.rec {2, 3} (fun _ previous => previous ∪ nextGeneration previous) n/-- The union of all finite stages, as in `formal-conjectures`. -/def generatedSet : Set ℕ := ⋃ n : ℕ, sequenceSet ntheorem sequenceSet_subset_succ (n : ℕ) :sequenceSet n ⊆ sequenceSet (n + 1) := byintro x hxexact Or.inl hxtheorem sequenceSet_mono : Monotone sequenceSet :=monotone_nat_of_le_succ sequenceSet_subset_succtheorem mem_sequenceSet_generated {n x : ℕ} (hx : x ∈ sequenceSet n) :Generated x := byinduction n generalizing x with| zero =>simp only [sequenceSet] at hxrcases hx with rfl | rfl· exact Generated.two· exact Generated.three| succ n ih =>rw [sequenceSet] at hxrcases hx with hx | hx· exact ih hx· obtain ⟨a, b, ha, hb, hab, rfl⟩ := hxexact Generated.mul_sub_one (ih ha) (ih hb) habtheorem generated_mem_generatedSet {x : ℕ} (hx : Generated x) :x ∈ generatedSet := byrw [generatedSet, Set.mem_iUnion]induction hx with| two => exact ⟨0, by simp [sequenceSet]⟩| three => exact ⟨0, by simp [sequenceSet]⟩| @mul_sub_one a b ha hb hab iha ihb =>obtain ⟨na, hna⟩ := ihaobtain ⟨nb, hnb⟩ := ihbrefine ⟨max na nb + 1, ?_⟩rw [sequenceSet]rightexact ⟨a, b,sequenceSet_mono (Nat.le_max_left na nb) hna,sequenceSet_mono (Nat.le_max_right na nb) hnb,hab, rfl⟩theorem mem_generatedSet_generated {x : ℕ} (hx : x ∈ generatedSet) :Generated x := byrw [generatedSet, Set.mem_iUnion] at hxobtain ⟨n, hn⟩ := hxexact mem_sequenceSet_generated hn/-- The staged set in `formal-conjectures` is exactly the inductivelygenerated set used in the proof. -/theorem mem_generatedSet_iff (x : ℕ) : x ∈ generatedSet ↔ Generated x :=⟨mem_generatedSet_generated, generated_mem_generatedSet⟩open Classical intheorem countUpTo_eq_generatedSet_count (x : ℕ) :countUpTo x =((Finset.Icc 1 x).filter fun n ↦ n ∈ generatedSet).card := byclassicalsimp only [countUpTo]apply congrArg Finset.cardext nsimp only [Finset.mem_filter, Finset.mem_Icc]rw [mem_generatedSet_iff]open Classical in/-- The intended positive-lower-density statement of Erdős Problem 424,expressed using the exact `nextGeneration`, `sequenceSet`, and `generatedSet`definitions from `formal-conjectures`. -/theorem generatedSet_has_positive_lower_density :∃ c : ℝ, 0 < c ∧ ∀ᶠ x : ℕ in Filter.atTop,c * (x : ℝ) ≤(((Finset.Icc 1 x).filter fun n ↦ n ∈ generatedSet).card : ℝ) := byobtain ⟨c, hc, hbound⟩ := theorem_onerefine ⟨c, hc, ?_⟩filter_upwards [hbound] with x hxrwa [← countUpTo_eq_generatedSet_count]open Classical in/-- The intended problem statement in the staged-set formulation. -/theorem erdos_424 :∃ c : ℝ, 0 < c ∧ ∀ᶠ x : ℕ in Filter.atTop,c * (x : ℝ) ≤(((Finset.Icc 1 x).filter fun n ↦ n ∈ generatedSet).card : ℝ) :=generatedSet_has_positive_lower_densityend Erdos424alias _root_.Erdos424.erdos_424_lower_density := _root_.Erdos424.erdos_424