{
  "status": "proved",
  "decl": "ProbabilityTheory.infinite_monkey",
  "lean_source": "import Mathlib.Probability.Independence.Basic\nimport Mathlib.Probability.IdentDistrib\nimport Mathlib.Analysis.SpecificLimits.Basic\n\nopen MeasureTheory Filter Set\nopen scoped ENNReal MeasureTheory ProbabilityTheory\n\nnamespace ProbabilityTheory\n\nvariable {Ω A : Type*} [MeasurableSpace Ω] [MeasurableSpace A]\n  [Fintype A] [MeasurableSingletonClass A]\n  {μ : Measure Ω} [IsProbabilityMeasure μ]\n\nprivate def monkeyBlock (X : ℕ → Ω → A) {k : ℕ} (w : Fin k → A) (n : ℕ) : Set Ω :=\n  ⋂ i : Fin k, X (n + (i : ℕ)) ⁻¹' {w i}\n\nomit [Fintype A] in\nprivate lemma measurableSet_monkeyBlock {X : ℕ → Ω → A} (hX : ∀ n, Measurable (X n))\n    {k : ℕ} (w : Fin k → A) (n : ℕ) : MeasurableSet (monkeyBlock X w n) := by\n  unfold monkeyBlock\n  exact MeasurableSet.iInter fun i ↦ (MeasurableSet.singleton (w i)).preimage (hX _)\n\nomit [Fintype A] [IsProbabilityMeasure μ] in\nprivate lemma measure_monkeyBlock {X : ℕ → Ω → A}\n    (h_indep : iIndepFun X μ) (h_ident : ∀ n, IdentDistrib (X n) (X 0) μ μ)\n    {k : ℕ} (w : Fin k → A) (n : ℕ) :\n    μ (monkeyBlock X w n) = ∏ i, μ (X 0 ⁻¹' {w i}) := by\n  have hinj : Function.Injective (fun i : Fin k ↦ n + (i : ℕ)) := by\n    intro i j hij\n    exact Fin.ext (Nat.add_left_cancel hij)\n  have hi := ProbabilityTheory.iIndepFun.precomp hinj h_indep\n  rw [monkeyBlock]\n  have hset : (⋂ i : Fin k, X (n + (i : ℕ)) ⁻¹' {w i}) =\n      ⋂ i ∈ (Finset.univ : Finset (Fin k)), X (n + (i : ℕ)) ⁻¹' {w i} := by simp\n  rw [hset, hi.measure_inter_preimage_eq_mul Finset.univ\n    (sets := fun i : Fin k ↦ {w i}) (by simp)]\n  apply Finset.prod_congr rfl\n  intro i _\n  exact (h_ident (n + i)).measure_preimage_eq (MeasurableSet.singleton (w i))\n\nprivate lemma block_base_mem_Ico {k N m j : ℕ} (hj : j < m) (i : Fin k) :\n    N + j * k + (i : ℕ) ∈ Finset.Ico N (N + m * k) := by\n  have hmul := Nat.mul_le_mul_right k (Nat.succ_le_iff.mpr hj)\n  have hjmul : j * k + k ≤ m * k := by simpa [Nat.add_mul] using hmul\n  have hi := i.isLt\n  simp only [Finset.mem_Ico]\n  omega\n\nprivate lemma block_next_mem_Ico {k N m : ℕ} (i : Fin k) :\n    N + m * k + (i : ℕ) ∈ Finset.Ico (N + m * k) (N + (m + 1) * k) := by\n  simp only [Finset.mem_Ico, Nat.add_mul]\n  omega\n\nprivate lemma monkeyBlock_misses_measure {X : ℕ → Ω → A} (hX : ∀ n, Measurable (X n))\n    (h_indep : iIndepFun X μ) (h_ident : ∀ n, IdentDistrib (X n) (X 0) μ μ)\n    {k : ℕ} (w : Fin k → A) (N m : ℕ) :\n    μ (⋂ j ∈ Finset.range m, (monkeyBlock X w (N + j * k))ᶜ) =\n      (1 - ∏ i, μ (X 0 ⁻¹' {w i})) ^ m := by\n  induction m with\n  | zero => simp\n  | succ m ihm =>\n      let S := Finset.Ico N (N + m * k)\n      let T := Finset.Ico (N + m * k) (N + (m + 1) * k)\n      have hST : Disjoint S T := by\n        rw [Finset.disjoint_left]\n        intro a haS haT\n        simp only [S, T, Finset.mem_Ico] at haS haT\n        omega\n      let F : (S → A) → Prop := fun z ↦\n        ∀ (j : ℕ) (hj : j < m), ¬ ∀ i : Fin k,\n          z ⟨N + j * k + (i : ℕ), block_base_mem_Ico hj i⟩ = w i\n      let G : (T → A) → Prop := fun z ↦\n        ¬ ∀ i : Fin k, z ⟨N + m * k + (i : ℕ), block_next_mem_Ico i⟩ = w i\n      have hfg : IndepFun\n          (fun ω ↦ F (fun i : S ↦ X i ω))\n          (fun ω ↦ G (fun i : T ↦ X i ω)) μ := by\n        exact (h_indep.indepFun_finset S T hST hX).comp\n          (measurable_of_finite F) (measurable_of_finite G)\n      have hpre : {ω | F (fun i : S ↦ X i ω)} =\n          ⋂ j ∈ Finset.range m, (monkeyBlock X w (N + j * k))ᶜ := by\n        ext ω\n        simp only [Set.mem_ofPred_eq, F, Set.mem_iInter, Finset.mem_range, mem_compl_iff,\n          monkeyBlock, Set.mem_preimage, Set.mem_singleton_iff, not_forall]\n      have hnew : {ω | G (fun i : T ↦ X i ω)} =\n          (monkeyBlock X w (N + m * k))ᶜ := by\n        ext ω\n        simp only [Set.mem_ofPred_eq, G, mem_compl_iff, monkeyBlock, Set.mem_iInter,\n          Set.mem_preimage, Set.mem_singleton_iff]\n      have hmul := hfg.measure_inter_preimage_eq_mul ({True} : Set Prop) ({True} : Set Prop)\n        (by simp) (by simp)\n      rw [show (fun ω ↦ F (fun i : S ↦ X i ω)) ⁻¹' ({True} : Set Prop) =\n          {ω | F (fun i : S ↦ X i ω)} by ext; simp,\n        show (fun ω ↦ G (fun i : T ↦ X i ω)) ⁻¹' ({True} : Set Prop) =\n          {ω | G (fun i : T ↦ X i ω)} by ext; simp,\n        hpre, hnew] at hmul\n      have hsucc : (⋂ j ∈ Finset.range (m + 1), (monkeyBlock X w (N + j * k))ᶜ) =\n          (⋂ j ∈ Finset.range m, (monkeyBlock X w (N + j * k))ᶜ) ∩\n            (monkeyBlock X w (N + m * k))ᶜ := by\n        rw [Finset.range_add_one]\n        simp [inter_comm]\n      rw [hsucc, hmul, ihm, measure_compl (measurableSet_monkeyBlock hX w _)\n          (measure_ne_top μ _), measure_univ, measure_monkeyBlock h_indep h_ident w _,\n        pow_succ]\n\n/-- **Infinite monkey theorem** (Wikidata Q487132).  If independent, identically distributed\nletters are drawn from a finite alphabet and every letter has positive probability, then almost\nsurely every finite word occurs as a contiguous block at arbitrarily large positions (and hence\ninfinitely often).  The assertion is simultaneous for all finite words. -/\ntheorem infinite_monkey (X : ℕ → Ω → A) (hX : ∀ n, Measurable (X n))\n    (h_indep : iIndepFun X μ) (h_ident : ∀ n, IdentDistrib (X n) (X 0) μ μ)\n    (h_pos : ∀ a, 0 < μ (X 0 ⁻¹' {a})) :\n    ∀ᵐ ω ∂μ, ∀ (k : ℕ) (w : Fin k → A) (N : ℕ),\n      ∃ n ≥ N, ∀ i : Fin k, X (n + (i : ℕ)) ω = w i := by\n  rw [ae_all_iff]\n  intro k\n  rw [ae_all_iff]\n  intro w\n  rw [ae_all_iff]\n  intro N\n  by_cases hk : k = 0\n  · subst k\n    filter_upwards [] with ω\n    exact ⟨N, le_rfl, fun i ↦ Fin.elim0 i⟩\n  · have hkpos : 0 < k := Nat.pos_of_ne_zero hk\n    let q : ℝ≥0∞ := ∏ i, μ (X 0 ⁻¹' {w i})\n    have hq0 : q ≠ 0 := Finset.prod_ne_zero_iff.mpr fun i _ ↦ (h_pos (w i)).ne'\n    have hq : 0 < q := bot_lt_iff_ne_bot.mpr hq0\n    have hr : 1 - q < (1 : ℝ≥0∞) :=\n      ENNReal.sub_lt_self ENNReal.one_ne_top one_ne_zero hq.ne'\n    let bad : Set Ω := {ω | ∀ n, N ≤ n → ω ∉ monkeyBlock X w n}\n    have hbad_le (m : ℕ) : μ bad ≤ (1 - q) ^ m := by\n      calc\n        μ bad ≤ μ (⋂ j ∈ Finset.range m, (monkeyBlock X w (N + j * k))ᶜ) := by\n          apply measure_mono\n          intro ω hω\n          simp only [bad, Set.mem_ofPred_eq, Set.mem_iInter, Finset.mem_range, mem_compl_iff] at hω ⊢\n          intro j hj\n          exact hω (N + j * k) (Nat.le_add_right N (j * k))\n        _ = (1 - q) ^ m := by\n          simpa [q] using monkeyBlock_misses_measure hX h_indep h_ident w N m\n    have hbad_zero : μ bad = 0 := by\n      apply le_zero_iff.mp\n      exact ge_of_tendsto (ENNReal.tendsto_pow_atTop_nhds_zero_of_lt_one hr)\n        (Eventually.of_forall hbad_le)\n    rw [ae_iff]\n    have hcompl : {ω | ¬ ∃ n ≥ N, ∀ i : Fin k, X (n + (i : ℕ)) ω = w i} = bad := by\n      ext ω\n      simp only [bad, Set.mem_ofPred_eq, not_exists, monkeyBlock, Set.mem_iInter,\n        Set.mem_preimage, Set.mem_singleton_iff]\n      aesop\n    rwa [hcompl]\n\n#print axioms ProbabilityTheory.infinite_monkey\n\nend ProbabilityTheory\n",
  "build_output": "'ProbabilityTheory.infinite_monkey' depends on axioms: [propext, Classical.choice, Quot.sound]\n",
  "explanation": "On an arbitrary probability space, a word of length k is represented by w : Fin k → A. Independence computes the probability q of one occurrence as the product of its letters' probabilities; identical distribution makes q independent of the starting position, and positivity of every letter makes q > 0. For a cutoff N, the proof examines the disjoint blocks starting at N + j*k. By grouping the underlying coordinates into two disjoint finite intervals and using iIndepFun.indepFun_finset, it proves inductively that the first m blocks all miss with probability (1-q)^m. Since 1-q < 1, these bounds tend to zero, so the event avoiding the word at every position n ≥ N is null. Countable almost-everywhere intersections over k, w, and N give one event on which every finite word occurs arbitrarily late. The k=0 case is handled directly.",
  "fidelity": {
    "finite_alphabet": "[Fintype A] states finiteness; [MeasurableSingletonClass A] makes letter events measurable.",
    "general_probability_space": "Ω is arbitrary, μ is an arbitrary Measure Ω with [IsProbabilityMeasure μ]. No product-space model is assumed.",
    "independent": "The hypothesis is iIndepFun X μ, exactly the requested family-level independence notion.",
    "identically_distributed": "∀ n, IdentDistrib (X n) (X 0) μ μ allows a common non-uniform marginal law.",
    "positive_letters": "∀ a, 0 < μ (X 0 ⁻¹' {a}) requires every letter, but does not require equal probabilities.",
    "contiguous_blocks": "The conclusion X (n+i)=w i for every i : Fin k is exactly the block X n,...,X (n+k-1).",
    "arbitrarily_large_and_infinite": "The quantifier ∀ N, ∃ n ≥ N gives arbitrarily large starts and therefore infinitely many starts for every nonempty word.",
    "simultaneous_event": "The almost-sure quantifier precedes ∀ k w N, so one event serves every finite word and every tail.",
    "edge_cases": "A singleton alphabet is unrestricted. For k=0, the empty word occurs at n=N by the unique elimination principle for Fin 0.",
    "route": "No Borel-Cantelli lemma is imported or used; the proof establishes the exact finite miss probability (1-q)^m and takes its limit."
  },
  "prior_work": {
    "mathlib_master": "At master commit e54979a532247ae4eb9d5578a8fd40a1bc332101, a full archive text search for case-insensitive 'infinite monkey' and 'Q487132' found only docs/1000.yaml, whose Q487132 entry has a title but no decl/decls field. The current missing-theorems page also lists Q487132.",
    "open_prs": "GitHub's open-PR issue-search API returned total_count 0 for both repo:leanprover-community/mathlib4 is:pr is:open \"infinite monkey\" and the corresponding Q487132 query.",
    "github_web": "Web searches for the exact theorem name and Wikidata id in leanprover-community/mathlib4 found no candidate formalization beyond the 1000+ tracking data. This is a text search, not a proof that no differently named equivalent theorem exists.",
    "zulip": "A site-restricted web search found no indexed Lean Zulip discussion. The direct public Zulip search view failed with 'Error loading Zulip', so the Zulip check is UNVERIFIED rather than claimed exhaustive."
  },
  "database_entry": {
    "wikidata_id": "Q487132",
    "declaration": "ProbabilityTheory.infinite_monkey",
    "file": "InfiniteMonkey.lean"
  },
  "stuck": "",
  "sources": [
    "https://leanprover-community.github.io/1000-missing.html",
    "https://raw.githubusercontent.com/leanprover-community/mathlib4/e54979a532247ae4eb9d5578a8fd40a1bc332101/docs/1000.yaml",
    "https://raw.githubusercontent.com/leanprover-community/mathlib4/fc4f59e/Mathlib/Probability/Independence/Basic.lean",
    "https://raw.githubusercontent.com/leanprover-community/mathlib4/fc4f59e/Mathlib/Probability/IdentDistrib.lean",
    "https://api.github.com/search/issues?q=repo%3Aleanprover-community%2Fmathlib4%20is%3Apr%20is%3Aopen%20%22infinite%20monkey%22",
    "https://api.github.com/search/issues?q=repo%3Aleanprover-community%2Fmathlib4%20is%3Apr%20is%3Aopen%20Q487132",
    "https://leanprover.zulipchat.com/#narrow/search/infinite.20monkey.20theorem"
  ]
}
