🏛️ Atlas✒️Formalizations.Combinatorics.Caballero.AngularPeaks view on GitHub ↗29 theorems
/-Copyright (c) 2026 Carles Marín. All rights reserved.Released under Apache 2.0 license as described in the file LICENSE.Authors: Carles Marín, Claude (AI assistant)-/module public import Mathlib.Order.Interval.Finset.Defspublic import Mathlib.Data.Int.Intervalpublic import Mathlib.Algebra.Order.Floor.Ringpublic import Mathlib.Basic.Real.Basicpublic import Mathlib.Algebra.Order.Archimedean.Real.Basicpublic import Mathlib.MeasureTheory.Measure.Lebesgue.Basicpublic import Mathlib.Tactic.Linarithpublic import Mathlib.Tactic.NormNumpublic import Mathlib.Tactic.FieldSimppublic import Mathlib.Tactic.Positivitypublic import Mathlib.Tactic.Ringpublic import Mathlib.Tactic.Push /-!# Exact peaks of the angular ambiguity profile (Caballero, a = 2) For every b >= 3, conditional on the named hypothesis that the list of Lemma 9.3 of Part I is thecomplete classification of ambiguous words: the ambiguous fraction of the section at slope 2/3 is(b+3)/(3b+4), at slope 1/3 it is (3b+1)/(3b+2). The combinatorial core (which cells are ambiguous)is proved for the transcribed list; the measure bridge turns the cell count into a length. ## Main results * `CaballeroPeaks.pico_dos_tercios_condicional`: ambiguous fraction at slope 2/3 is (b+3)/(3b+4), b >= 3, under ClasificacionI* `CaballeroPeaks.pico_un_tercio_condicional`: ambiguous fraction at slope 1/3 is (3b+1)/(3b+2), b >= 3, under ClasificacionI* `CaballeroPeaks.celdas_dos_tercios`: cell word G(k) is in the list of Lemma 9.3 iff k in S (combinatorial core) ## References * C. Marín, How Many Dyck Words Do the Divisors of an Integer Make? Exact Counts and Quasipolynomial Growth (Part I), Zenodo (2026), doi:10.5281/zenodo.24536372 (concept DOI): Lemma 9.3, the sizes in the proof of Theorem 9.4 and Lemma 7.1 enter as the named hypothesis `ClasificacionI`.* Part III (in preparation), Proposition 6.-/ @[expose] public section namespace CaballeroPeaks open Finset MeasureTheory /-- A word: the five values at `h = -2, -1, 0, 1, 2`. -/abbrev Palabra := Int × Int × Int × Int × Int /-- The clipping of Part I: values are clamped to the interval `[-b-1, b]`. -/def clip (b v : Int) : Int := min b (max (-b - 1) v) /-- The three cases of `clip` (below, inside, above), used to feed `omega` with `clip` as an atom. -/theorem clip_casos (b v : Int) (hb : 0 ≤ b) :    (v ≤ -b - 1 ∧ clip b v = -b - 1) ∨ (-b - 1 ≤ v ∧ v ≤ b ∧ clip b v = v) ∨ (b ≤ v ∧ clip b v = b) := by  unfold clip  omega /-- If the second argument lies strictly inside `(-b-1, b)`, equal clippings force equal arguments. -/theorem clip_interior (b v w : Int) (h : clip b v = clip b w) (h0 : -b - 1 < w) (h1 : w < b) : v = w := by  unfold clip at h  omega /-- `O₁(s) = (⌊(s+h)/3⌋)_h`, clipped, for `h = -2, …, 2`. -/def O1 (b s : Int) : Palabra :=  (clip b ((s - 2) / 3), clip b ((s - 1) / 3), clip b (s / 3), clip b ((s + 1) / 3), clip b ((s + 2) / 3)) /-- `O₂(s) = (s + h(b+1))_h`, clipped. -/def O2 (b s : Int) : Palabra :=  (clip b (s - 2 * (b + 1)), clip b (s - (b + 1)), clip b s, clip b (s + (b + 1)), clip b (s + 2 * (b + 1))) /-- `G(s) = (⌊(s+2h)/3⌋)_h`, clipped. -/def G (b s : Int) : Palabra :=  (clip b ((s - 4) / 3), clip b ((s - 2) / 3), clip b (s / 3), clip b ((s + 2) / 3), clip b ((s + 4) / 3)) /-- A word lies in one of the classes listed in Lemma 9.3 of Part I for `a = 2`: `O(0)` (Lemma 7.1), `O(s)` with `O₁(s) ≠ O₂(s)`, `A_k`, `B` (only for `b = 3w+2`), `C`, `D`, `E`, `F`. -/def enLista (b : Int) (w : Palabra) : Prop :=  (∃ m1 m2 : Int, 0 ≤ m1 ∧ (m2 = 2 * m1 ∨ m2 = 2 * m1 + 1) ∧      w = (-(min b m2) - 1, -(min b m1) - 1, 0, min b m1, min b m2)) ∨  (∃ s : Int, 1 ≤ s ∧ s ≤ 3 * b ∧ O1 b s ≠ O2 b s ∧ (w = O1 b s ∨ w = O2 b s)) ∨  (∃ k : Int, 1 ≤ k ∧ 2 * k ≤ b ∧      (w = (-2 * k - 1, -k, 0, k, 2 * k) ∨ w = (-2 * k, -k, 0, k, 2 * k - 1))) ∨  (∃ v : Int, 1 ≤ v ∧ b = 3 * v + 2 ∧      (w = (-v - 2, -1, v, 2 * v + 2, b) ∨ w = (-v - 1, -1, v, 2 * v + 1, b) ∨       w = (-v - 2, -1, v + 1, 2 * v + 2, b) ∨ w = (-v - 1, 0, v, 2 * v + 1, b) ∨       w = (-v - 2, 0, v + 1, 2 * v + 2, b) ∨ w = (-v - 1, 0, v + 1, 2 * v + 1, b))) ∨  (w = (-b, -1, b - 1, b, b) ∨ w = G b b ∨ w = (-b, 0, b - 1, b, b) ∨ w = G b (b + 1)) ∨  (∃ i e : Int, 0 ≤ i ∧ 3 * i ≤ b - 5 ∧ 0 ≤ e ∧ e ≤ 1 ∧      (w = (b - 6 - 3 * i, b - 4 - 2 * i + e, b - 1 - i, b, b) ∨       w = (b - 5 - 3 * i, b - 4 - 2 * i + e, b - 2 - i, b, b))) ∨  (∃ i : Int, 0 ≤ i ∧ 2 * i ≤ b - 5 ∧      (w = (b - 6 - 2 * i, b - 2 - i, b, b, b) ∨ w = (b - 5 - 2 * i, b - 3 - i, b, b, b))) ∨  (∃ x : Int, -1 ≤ x ∧ x ≤ b - 3 ∧      (w = (x, b - 1, b, b, b) ∨ w = G b (2 * b + 2 + x) ∨ (x = b - 4 ∧ w = (b - 3, b - 2, b, b, b)))) /-- The set `S = {0, b, b+1} ∪ [2b+1, 3b-1] ∪ {3b+1}` of ambiguous cells at slope `θ = 2/3`. -/def enS (b k : Int) : Prop :=  k = 0 ∨ k = b ∨ k = b + 1 ∨ (2 * b + 1 ≤ k ∧ k ≤ 3 * b - 1) ∨ k = 3 * b + 1set_option maxHeartbeats 1000000 in/-- Slope `θ = 2/3`: for `0 ≤ k ≤ 3b+3`, the word `G(k)` lies in the list iff `k ∈ S`. -/theorem celdas_dos_tercios (b k : Int) (hb : 3 ≤ b) (hk0 : 0 ≤ k) (hk1 : k ≤ 3 * b + 3) :    enLista b (G b k) ↔ enS b k := by  constructor  · intro h    simp only [enLista, enS, G, O1, O2, Prod.mk.injEq, ne_eq] at h ⊢    rcases h with ⟨m1, m2, h⟩ | ⟨s, h⟩ | ⟨k', h⟩ | ⟨v, h⟩ | h | ⟨i, e, h⟩ | ⟨i, h⟩ | ⟨x, h⟩    case inr.inr.inr.inr.inr.inr.inr =>      obtain ⟨hx0, hx1, h⟩ := h      rcases h with h | ⟨h1, h2, h3, -, -⟩ | ⟨-, h⟩      · simp only [clip] at h; omega      ·        have e1 := clip_interior b _ _ h1 (by omega) (by omega)        have e2 := clip_interior b _ _ h2 (by omega) (by omega)        have e3 := clip_interior b _ _ h3 (by omega) (by omega)        have hk3 : k % 3 = 0 ∨ k % 3 = 1 ∨ k % 3 = 2 := by omega        have hs3 : (2 * b + 2 + x) % 3 = 0 ∨ (2 * b + 2 + x) % 3 = 1 ∨ (2 * b + 2 + x) % 3 = 2 := by omega        rcases hk3 with hk3 | hk3 | hk3 <;> rcases hs3 with hs3 | hs3 | hs3 <;> omega      · simp only [clip] at h; omega    all_goals (simp only [clip] at h ⊢; omega)  · intro h    simp only [enS] at h    rcases h with h | h | h | h | h    ·      refine Or.inl ⟨0, 1, by omega, by omega, ?_⟩      subst h      simp only [G, clip, Prod.mk.injEq]      omega    ·      subst h      exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (Or.inr (Or.inl rfl))))))    ·      subst h      exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (Or.inr (Or.inr (Or.inr rfl)))))))    ·      refine Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨k - 2 * b - 2, by omega, by omega, ?_⟩))))))      refine Or.inr (Or.inl ?_)      have : 2 * b + 2 + (k - 2 * b - 2) = k := by omega      rw [this]    ·      refine Or.inr (Or.inl ⟨3 * b, by omega, by omega, ?_, Or.inl ?_⟩)      · simp only [O1, O2, clip, ne_eq, Prod.mk.injEq]        omega      · subst h        simp only [G, O1, clip, Prod.mk.injEq]        omega set_option maxHeartbeats 1000000 in/-- Slope `θ = 1/3`: for `0 ≤ k ≤ 3b+1`, the word `O₁(k)` lies in the list iff `k ≤ 3b`. -/theorem celdas_un_tercio (b k : Int) (hb : 3 ≤ b) (hk0 : 0 ≤ k) (hk1 : k ≤ 3 * b + 1) :    enLista b (O1 b k) ↔ k ≤ 3 * b := by  constructor  · intro h    simp only [enLista, G, O1, O2, clip, Prod.mk.injEq, ne_eq] at h    rcases h with ⟨m1, m2, h⟩ | ⟨s, h⟩ | ⟨k', h⟩ | ⟨v, h⟩ | h | ⟨i, e, h⟩ | ⟨i, h⟩ | ⟨x, h⟩ <;> omega  · intro h    by_cases h0 : k = 0    ·      refine Or.inl ⟨0, 0, by omega, by omega, ?_⟩      subst h0      simp only [O1, clip, Prod.mk.injEq]      omega    · refine Or.inr (Or.inl ⟨k, by omega, h, ?_, Or.inl rfl⟩)      simp only [O1, O2, clip, ne_eq, Prod.mk.injEq]      omega /-- Conditional form: if the classification of Part I is complete and correct (named hypothesis `ClasificacionI`: a word is ambiguous iff it lies in the list), the cell statements hold for ambiguity. The hypothesis gathers Lemma 9.3, the sizes in the proof of Theorem 9.4 and Lemma 7.1 of Part I. -/theorem picos_condicional (Amb : Int → Palabra → Prop)    (ClasificacionI : ∀ b w, 3 ≤ b → (Amb b w ↔ enLista b w)) (b : Int) (hb : 3 ≤ b) :    (∀ k, 0 ≤ k → k ≤ 3 * b + 3 → (Amb b (G b k) ↔ enS b k)) ∧    (∀ k, 0 ≤ k → k ≤ 3 * b + 1 → (Amb b (O1 b k) ↔ k ≤ 3 * b)) :=  ⟨fun k h0 h1 => (ClasificacionI b _ hb).trans (celdas_dos_tercios b k hb h0 h1),   fun k h0 h1 => (ClasificacionI b _ hb).trans (celdas_un_tercio b k hb h0 h1)⟩ /-- Boundary check: cell `3b` carries the `F`-word with `x = b - 2`, just outside the range of `F`. -/theorem control_celda_3b (b : Int) (hb : 3 ≤ b) : G b (3 * b) = (b - 2, b - 1, b, b, b) := by  simp only [G, clip, Prod.mk.injEq]  omega /-- Cell `3b` is not in the list (so extending `F` by one step would change the count to `b + 4`). -/theorem control_3b_fuera (b : Int) (hb : 3 ≤ b) : ¬ enLista b (G b (3 * b)) := by  rw [celdas_dos_tercios b (3 * b) hb (by omega) (by omega)]  simp only [enS]  omega /-- Word of cell `j` on the diagonal section `θ = 1`: `(clip(j+h))_h`. -/def Dg (b j : Int) : Palabra :=  (clip b (j - 2), clip b (j - 1), clip b j, clip b (j + 1), clip b (j + 2)) set_option maxHeartbeats 1000000 in/-- Diagonal section: for `0 ≤ j ≤ b+1`, the word of cell `j` is not in the list. -/theorem diagonal_fuera (b j : Int) (hb : 3 ≤ b) (hj0 : 0 ≤ j) (hj1 : j ≤ b + 1) : ¬ enLista b (Dg b j) := by  intro h  simp only [enLista, Dg, G, O1, O2, Prod.mk.injEq, ne_eq] at h  rcases h with ⟨m1, m2, h⟩ | ⟨s, h⟩ | ⟨k', h⟩ | ⟨v, h⟩ | h | ⟨i, e, h⟩ | ⟨i, h⟩ | ⟨x, h⟩  all_goals (simp only [clip] at h; omega) set_option maxHeartbeats 1000000 in/-- The top cell `(b,b,b,b,b)` of the diagonal is not in the list. -/theorem diagonal_superior_fuera (b : Int) (hb : 3 ≤ b) : ¬ enLista b (b, b, b, b, b) := by  intro h  simp only [enLista, G, O1, O2, Prod.mk.injEq, ne_eq] at h  rcases h with ⟨m1, m2, h⟩ | ⟨s, h⟩ | ⟨k', h⟩ | ⟨v, h⟩ | h | ⟨i, e, h⟩ | ⟨i, h⟩ | ⟨x, h⟩  all_goals ((try simp only [clip] at h) ; omega) /-- PARTIAL lemma (not Part III's diagonal theorem): for `b ≥ 3` and under `ClasificacionI`, the listed diagonal words `Dg b j` (`0 ≤ j ≤ b+1`) and the top word are unambiguous. It does not prove that these words exhaust the cells of the section, nor `A(1) = 0`, nor the cases `b = 1, 2` (gate verdict NO_FIEL of 30-11 for that reading). -/theorem diagonal_condicional (Amb : Int → Palabra → Prop)    (ClasificacionI : ∀ b w, 3 ≤ b → (Amb b w ↔ enLista b w)) (b : Int) (hb : 3 ≤ b) :    (∀ j, 0 ≤ j → j ≤ b + 1 → ¬ Amb b (Dg b j)) ∧ ¬ Amb b (b, b, b, b, b) :=  ⟨fun j h0 h1 h => diagonal_fuera b j hb h0 h1 ((ClasificacionI b _ hb).mp h),   fun h => diagonal_superior_fuera b hb ((ClasificacionI b _ hb).mp h)⟩  /-- Step 1: if `L ∈ (k/3, (k+1)/3)` then `⌊L + m/3⌋ = ⌊(k+m)/3⌋` for every integer `m`. With `m = p·h` this gives the word of cell `k` at slope `θ = p/3`. -/theorem suelo_celda (k m : ℤ) (L : ℝ) (h0 : (k : ℝ) / 3 < L) (h1 : L < ((k : ℝ) + 1) / 3) :    ⌊L + (m : ℝ) / 3⌋ = (k + m) / 3 := by  rw [Int.floor_eq_iff]  have hq := Int.mul_ediv_add_emod (k + m) 3  have hr0 : 0 ≤ (k + m) % 3 := Int.emod_nonneg _ (by norm_num)  have hr2 : (k + m) % 3 ≤ 2 := by omega  have hkm : ((k : ℝ) + m) = 3 * (((k + m) / 3 : ℤ) : ℝ) + (((k + m) % 3 : ℤ) : ℝ) := by exact_mod_cast hq.symm  have hr0' : (0 : ℝ) ≤ (((k + m) % 3 : ℤ) : ℝ) := by exact_mod_cast hr0  have hr2' : (((k + m) % 3 : ℤ) : ℝ) ≤ 2 := by exact_mod_cast hr2  constructor <;> linarith instance (b : Int) : DecidablePred (enS b) := fun k => by unfold enS; infer_instance /-- Number of ambiguous cells at `θ = 2/3`: `|S ∩ [0, 3b+3]| = b + 3`. -/theorem card_S (b : ℤ) (hb : 3 ≤ b) :    ((Icc (0 : ℤ) (3 * b + 3)).filter (fun k => enS b k)).card = (b + 3).toNat := by  have e : (Icc (0 : ℤ) (3 * b + 3)).filter (fun k => enS b k) =      insert 0 (insert b (insert (b + 1) (insert (3 * b + 1) (Icc (2 * b + 1) (3 * b - 1))))) := by    ext k    rw [mem_filter]    simp only [mem_Icc, mem_insert, enS]    omega  rw [e, card_insert_of_notMem (by simp only [mem_insert, mem_Icc]; omega),    card_insert_of_notMem (by simp only [mem_insert, mem_Icc]; omega),    card_insert_of_notMem (by simp only [mem_insert, mem_Icc]; omega),    card_insert_of_notMem (by simp only [mem_Icc]; omega), Int.card_Icc]  omega /-- Number of ambiguous cells at `θ = 1/3`: `3b + 1`. -/theorem card_un_tercio (b : ℤ) (hb : 3 ≤ b) :    ((Icc (0 : ℤ) (3 * b + 1)).filter (fun k => k ≤ 3 * b)).card = (3 * b + 1).toNat := by  have e : (Icc (0 : ℤ) (3 * b + 1)).filter (fun k => k ≤ 3 * b) = Icc 0 (3 * b) := by    ext k    simp only [mem_filter, mem_Icc]    omega  rw [e, Int.card_Icc]  omega /-- Total cells: `3b + 4` at `θ = 2/3` and `3b + 2` at `θ = 1/3`. -/theorem card_total (b : ℤ) (hb : 3 ≤ b) :    (Icc (0 : ℤ) (3 * b + 3)).card = (3 * b + 4).toNat ∧ (Icc (0 : ℤ) (3 * b + 1)).card = (3 * b + 2).toNat := by  rw [Int.card_Icc, Int.card_Icc]  omega  /-- Word of the section `θ = 2/3` at the point `L`: `(clip ⌊L + 2h/3⌋)_h`. -/noncomputable def palabra23 (b : ℤ) (L : ℝ) : Palabra :=  (clip b ⌊L + ((-4 : ℤ) : ℝ) / 3⌋, clip b ⌊L + ((-2 : ℤ) : ℝ) / 3⌋, clip b ⌊L + ((0 : ℤ) : ℝ) / 3⌋,   clip b ⌊L + ((2 : ℤ) : ℝ) / 3⌋, clip b ⌊L + ((4 : ℤ) : ℝ) / 3⌋) /-- Ambiguous set of the section `θ = 2/3` (with respect to the list). -/def U23 (b : ℤ) : Set ℝ := {L | 0 ≤ L ∧ L ≤ (b : ℝ) + 4 / 3 ∧ enLista b (palabra23 b L)} /-- On the open cell `(k/3, (k+1)/3)` the word of the section `θ = 2/3` is `G(k)`. -/theorem palabra23_celda (b k : ℤ) (L : ℝ) (h0 : (k : ℝ) / 3 < L) (h1 : L < ((k : ℝ) + 1) / 3) :    palabra23 b L = G b k := by  unfold palabra23 G  rw [suelo_celda k (-4) L h0 h1, suelo_celda k (-2) L h0 h1, suelo_celda k 0 L h0 h1, suelo_celda k 2 L h0 h1,    suelo_celda k 4 L h0 h1, show k + -4 = k - 4 by omega, show k + -2 = k - 2 by omega, show k + 0 = k by omega] /-- The cells of `S`, as a finite set. -/def SFin (b : ℤ) : Finset ℤ := (Finset.Icc (0 : ℤ) (3 * b + 3)).filter (fun k => enS b k) /-- Union of the open cells of `S`. -/noncomputable def V23 (b : ℤ) : Set ℝ := ⋃ k ∈ SFin b, Set.Ioo ((k : ℝ) / 3) (((k : ℝ) + 1) / 3) /-- The cut points `j/3`, `0 ≤ j ≤ 3b+4`. -/noncomputable def C23 (b : ℤ) : Set ℝ := (fun j : ℤ => (j : ℝ) / 3) '' (Finset.Icc (0 : ℤ) (3 * b + 4) : Set ℤ) /-- Lower bound of the sandwich: the open cells of `S` are ambiguous. -/theorem V_sub_U (b : ℤ) (hb : 3 ≤ b) : V23 b ⊆ U23 b := by  intro L hL  simp only [V23, Set.mem_iUnion, Set.mem_Ioo, SFin, Finset.mem_filter, Finset.mem_Icc] at hL  obtain ⟨k, ⟨⟨hk0, hk1⟩, hS⟩, h0, h1⟩ := hL  have hk0' : (0 : ℝ) ≤ k := by exact_mod_cast hk0  have hk1' : (k : ℝ) ≤ 3 * b + 3 := by exact_mod_cast hk1  refine ⟨by linarith, by linarith, ?_⟩  rw [palabra23_celda b k L h0 h1]  exact (celdas_dos_tercios b k hb hk0 hk1).mpr hS /-- Upper bound of the sandwich: an ambiguous point is in an open cell of `S` or is a cut point. -/theorem U_sub_VC (b : ℤ) (hb : 3 ≤ b) : U23 b ⊆ V23 b ∪ C23 b := by  rintro L ⟨hL0, hL1, hw⟩  by_cases hc : ((⌊3 * L⌋ : ℤ) : ℝ) = 3 * L  ·    right    refine ⟨⌊3 * L⌋, ?_, by show ((⌊3 * L⌋ : ℤ) : ℝ) / 3 = L; rw [hc]; ring⟩    simp only [Finset.coe_Icc, Set.mem_Icc]    constructor    · have : (0 : ℝ) ≤ ((⌊3 * L⌋ : ℤ) : ℝ) := by rw [hc]; linarith      exact_mod_cast this    · have : ((⌊3 * L⌋ : ℤ) : ℝ) ≤ 3 * b + 4 := by rw [hc]; linarith      exact_mod_cast this  · left    set k := ⌊3 * L⌋ with hkdef    have hlo : (k : ℝ) < 3 * L := lt_of_le_of_ne (Int.floor_le _) hc    have hhi : 3 * L < (k : ℝ) + 1 := Int.lt_floor_add_one _    have h0 : (k : ℝ) / 3 < L := by linarith    have h1 : L < ((k : ℝ) + 1) / 3 := by linarith    have hk0 : 0 ≤ k := by      have : (-1 : ℝ) < k := by linarith      have : (-1 : ℤ) < k := by exact_mod_cast this      omega    have hk1 : k ≤ 3 * b + 3 := by      have : (k : ℝ) < 3 * b + 4 := by linarith      have : k < 3 * b + 4 := by exact_mod_cast this      omega    rw [palabra23_celda b k L h0 h1] at hw    simp only [V23, Set.mem_iUnion, Set.mem_Ioo, SFin, Finset.mem_filter, Finset.mem_Icc]    exact ⟨k, ⟨⟨hk0, hk1⟩, (celdas_dos_tercios b k hb hk0 hk1).mp hw⟩, h0, h1⟩ /-- The open cells of `S` have total length `(b+3)/3`. -/theorem vol_V (b : ℤ) (hb : 3 ≤ b) : volume (V23 b) = ENNReal.ofReal (((b : ℝ) + 3) / 3) := by  rw [V23, measure_biUnion_finset]  · simp only [Real.volume_Ioo]    have e : ∀ k : ℤ, ((k : ℝ) + 1) / 3 - (k : ℝ) / 3 = 1 / 3 := fun k => by ring    simp only [e, Finset.sum_const, nsmul_eq_mul]    have hc : (SFin b).card = (b + 3).toNat := card_S b hb    rw [hc]    have hb3 : (0 : ℤ) ≤ b + 3 := by omega    rw [show (((b + 3).toNat : ℕ) : ENNReal) = ENNReal.ofReal (((b + 3).toNat : ℕ) : ℝ) by      rw [ENNReal.ofReal_natCast]]    rw [← ENNReal.ofReal_mul (by positivity)]    congr 1    have : (((b + 3).toNat : ℕ) : ℝ) = (b : ℝ) + 3 := by      have := Int.toNat_of_nonneg hb3      exact_mod_cast this    rw [this]    ring  ·    intro i _ j _ hij    refine Set.disjoint_left.mpr ?_    intro L hi hj    simp only [Set.mem_Ioo] at hi hj    have a : (i : ℝ) < j + 1 := by linarith [hi.1, hj.2]    have c : (j : ℝ) < i + 1 := by linarith [hj.1, hi.2]    have a' : i < j + 1 := by exact_mod_cast a    have c' : j < i + 1 := by exact_mod_cast c    exact hij (by omega)  · intro k _    exact measurableSet_Ioo /-- The cut points have measure zero. -/theorem vol_C_cero (b : ℤ) : volume (C23 b) = 0 :=  ((Finset.Icc (0 : ℤ) (3 * b + 4)).finite_toSet.image _).measure_zero _ /-- Length of the ambiguous set at `θ = 2/3`: `(b+3)/3`. -/theorem vol_U23 (b : ℤ) (hb : 3 ≤ b) : volume (U23 b) = ENNReal.ofReal (((b : ℝ) + 3) / 3) := by  apply le_antisymm  · calc volume (U23 b) ≤ volume (V23 b ∪ C23 b) := measure_mono (U_sub_VC b hb)      _ ≤ volume (V23 b) + volume (C23 b) := measure_union_le _ _      _ = ENNReal.ofReal (((b : ℝ) + 3) / 3) := by rw [vol_V b hb, vol_C_cero, add_zero]  · rw [← vol_V b hb]    exact measure_mono (V_sub_U b hb) /-- Proposition 6 of Part III, peak at `θ = 2/3`, with respect to the list: the ambiguous fraction of the section `[0, b + 4/3]` is `(b+3)/(3b+4)`. -/theorem pico_dos_tercios (b : ℤ) (hb : 3 ≤ b) :    (volume (U23 b)).toReal / ((b : ℝ) + 4 / 3) = ((b : ℝ) + 3) / (3 * b + 4) := by  have hb' : (3 : ℝ) ≤ b := by exact_mod_cast hb  rw [vol_U23 b hb, ENNReal.toReal_ofReal (by positivity)]  field_simp /-- Proposition 6 of Part III, peak at `θ = 2/3`, conditional on `ClasificacionI`: the ambiguous fraction of the section is `(b+3)/(3b+4)` for every `b ≥ 3`. -/theorem pico_dos_tercios_condicional (Amb : Int → Palabra → Prop)    (ClasificacionI : ∀ b w, 3 ≤ b → (Amb b w ↔ enLista b w)) (b : ℤ) (hb : 3 ≤ b) :    (volume {L : ℝ | 0 ≤ L ∧ L ≤ (b : ℝ) + 4 / 3 ∧ Amb b (palabra23 b L)}).toReal / ((b : ℝ) + 4 / 3)      = ((b : ℝ) + 3) / (3 * b + 4) := by  have e : {L : ℝ | 0 ≤ L ∧ L ≤ (b : ℝ) + 4 / 3 ∧ Amb b (palabra23 b L)} = U23 b := by    ext L    simp only [U23, Set.mem_ofPred_eq, ClasificacionI b _ hb]  rw [e]  exact pico_dos_tercios b hb /-- Word of the section `θ = 1/3` at the point `L`: `(clip ⌊L + h/3⌋)_h`. -/noncomputable def palabra13 (b : ℤ) (L : ℝ) : Palabra :=  (clip b ⌊L + ((-2 : ℤ) : ℝ) / 3⌋, clip b ⌊L + ((-1 : ℤ) : ℝ) / 3⌋, clip b ⌊L + ((0 : ℤ) : ℝ) / 3⌋,   clip b ⌊L + ((1 : ℤ) : ℝ) / 3⌋, clip b ⌊L + ((2 : ℤ) : ℝ) / 3⌋) /-- Ambiguous set of the section `θ = 1/3` (with respect to the list). -/def U13 (b : ℤ) : Set ℝ := {L | 0 ≤ L ∧ L ≤ (b : ℝ) + 2 / 3 ∧ enLista b (palabra13 b L)} /-- On the open cell `(k/3, (k+1)/3)` the word of the section `θ = 1/3` is `O₁(k)`. -/theorem palabra13_celda (b k : ℤ) (L : ℝ) (h0 : (k : ℝ) / 3 < L) (h1 : L < ((k : ℝ) + 1) / 3) :    palabra13 b L = O1 b k := by  unfold palabra13 O1  rw [suelo_celda k (-2) L h0 h1, suelo_celda k (-1) L h0 h1, suelo_celda k 0 L h0 h1, suelo_celda k 1 L h0 h1,    suelo_celda k 2 L h0 h1, show k + -2 = k - 2 by omega, show k + -1 = k - 1 by omega, show k + 0 = k by omega] /-- The ambiguous cells at `θ = 1/3`, as a finite set. -/def SFin13 (b : ℤ) : Finset ℤ := (Finset.Icc (0 : ℤ) (3 * b + 1)).filter (fun k => k ≤ 3 * b) /-- Union of the open ambiguous cells at `θ = 1/3`. -/noncomputable def V13 (b : ℤ) : Set ℝ := ⋃ k ∈ SFin13 b, Set.Ioo ((k : ℝ) / 3) (((k : ℝ) + 1) / 3) /-- The cut points `j/3`, `0 ≤ j ≤ 3b+2`. -/noncomputable def C13 (b : ℤ) : Set ℝ := (fun j : ℤ => (j : ℝ) / 3) '' (Finset.Icc (0 : ℤ) (3 * b + 2) : Set ℤ) /-- Lower bound of the sandwich at `θ = 1/3`. -/theorem V13_sub_U13 (b : ℤ) (hb : 3 ≤ b) : V13 b ⊆ U13 b := by  intro L hL  simp only [V13, Set.mem_iUnion, Set.mem_Ioo, SFin13, Finset.mem_filter, Finset.mem_Icc] at hL  obtain ⟨k, ⟨⟨hk0, hk1⟩, hS⟩, h0, h1⟩ := hL  have hk0' : (0 : ℝ) ≤ k := by exact_mod_cast hk0  have hk1' : (k : ℝ) ≤ 3 * b + 1 := by exact_mod_cast hk1  refine ⟨by linarith, by linarith, ?_⟩  rw [palabra13_celda b k L h0 h1]  exact (celdas_un_tercio b k hb hk0 hk1).mpr hS /-- Upper bound of the sandwich at `θ = 1/3`. -/theorem U13_sub_VC (b : ℤ) (hb : 3 ≤ b) : U13 b ⊆ V13 b ∪ C13 b := by  rintro L ⟨hL0, hL1, hw⟩  by_cases hc : ((⌊3 * L⌋ : ℤ) : ℝ) = 3 * L  · right    refine ⟨⌊3 * L⌋, ?_, by show ((⌊3 * L⌋ : ℤ) : ℝ) / 3 = L; rw [hc]; ring⟩    simp only [Finset.coe_Icc, Set.mem_Icc]    constructor    · have : (0 : ℝ) ≤ ((⌊3 * L⌋ : ℤ) : ℝ) := by rw [hc]; linarith      exact_mod_cast this    · have : ((⌊3 * L⌋ : ℤ) : ℝ) ≤ 3 * b + 2 := by rw [hc]; linarith      exact_mod_cast this  · left    set k := ⌊3 * L⌋ with hkdef    have hlo : (k : ℝ) < 3 * L := lt_of_le_of_ne (Int.floor_le _) hc    have hhi : 3 * L < (k : ℝ) + 1 := Int.lt_floor_add_one _    have h0 : (k : ℝ) / 3 < L := by linarith    have h1 : L < ((k : ℝ) + 1) / 3 := by linarith    have hk0 : 0 ≤ k := by      have : (-1 : ℝ) < k := by linarith      have : (-1 : ℤ) < k := by exact_mod_cast this      omega    have hk1 : k ≤ 3 * b + 1 := by      have : (k : ℝ) < 3 * b + 2 := by linarith      have : k < 3 * b + 2 := by exact_mod_cast this      omega    rw [palabra13_celda b k L h0 h1] at hw    simp only [V13, Set.mem_iUnion, Set.mem_Ioo, SFin13, Finset.mem_filter, Finset.mem_Icc]    exact ⟨k, ⟨⟨hk0, hk1⟩, (celdas_un_tercio b k hb hk0 hk1).mp hw⟩, h0, h1⟩ /-- The open ambiguous cells at `θ = 1/3` have total length `(3b+1)/3`. -/theorem vol_V13 (b : ℤ) (hb : 3 ≤ b) : volume (V13 b) = ENNReal.ofReal ((3 * (b : ℝ) + 1) / 3) := by  rw [V13, measure_biUnion_finset]  · simp only [Real.volume_Ioo]    have e : ∀ k : ℤ, ((k : ℝ) + 1) / 3 - (k : ℝ) / 3 = 1 / 3 := fun k => by ring    simp only [e, Finset.sum_const, nsmul_eq_mul]    have hc : (SFin13 b).card = (3 * b + 1).toNat := card_un_tercio b hb    rw [hc]    have hb3 : (0 : ℤ) ≤ 3 * b + 1 := by omega    rw [show (((3 * b + 1).toNat : ℕ) : ENNReal) = ENNReal.ofReal (((3 * b + 1).toNat : ℕ) : ℝ) by      rw [ENNReal.ofReal_natCast]]    rw [← ENNReal.ofReal_mul (by positivity)]    congr 1    have : (((3 * b + 1).toNat : ℕ) : ℝ) = 3 * (b : ℝ) + 1 := by      have := Int.toNat_of_nonneg hb3      exact_mod_cast this    rw [this]    ring  · intro i _ j _ hij    refine Set.disjoint_left.mpr ?_    intro L hi hj    simp only [Set.mem_Ioo] at hi hj    have a : (i : ℝ) < j + 1 := by linarith [hi.1, hj.2]    have c : (j : ℝ) < i + 1 := by linarith [hj.1, hi.2]    have a' : i < j + 1 := by exact_mod_cast a    have c' : j < i + 1 := by exact_mod_cast c    exact hij (by omega)  · intro k _    exact measurableSet_Ioo /-- Length of the ambiguous set at `θ = 1/3`: `(3b+1)/3`. -/theorem vol_U13 (b : ℤ) (hb : 3 ≤ b) : volume (U13 b) = ENNReal.ofReal ((3 * (b : ℝ) + 1) / 3) := by  apply le_antisymm  · calc volume (U13 b) ≤ volume (V13 b ∪ C13 b) := measure_mono (U13_sub_VC b hb)      _ ≤ volume (V13 b) + volume (C13 b) := measure_union_le _ _      _ = ENNReal.ofReal ((3 * (b : ℝ) + 1) / 3) := by        rw [vol_V13 b hb, show volume (C13 b) = 0 from          ((Finset.Icc (0 : ℤ) (3 * b + 2)).finite_toSet.image _).measure_zero _, add_zero]  · rw [← vol_V13 b hb]    exact measure_mono (V13_sub_U13 b hb) /-- Proposition 6 of Part III, peak at `θ = 1/3`, with respect to the list: `(3b+1)/(3b+2)`. -/theorem pico_un_tercio (b : ℤ) (hb : 3 ≤ b) :    (volume (U13 b)).toReal / ((b : ℝ) + 2 / 3) = (3 * (b : ℝ) + 1) / (3 * b + 2) := by  have hb' : (3 : ℝ) ≤ b := by exact_mod_cast hb  rw [vol_U13 b hb, ENNReal.toReal_ofReal (by positivity)]  field_simp /-- Proposition 6 of Part III, peak at `θ = 1/3`, conditional on `ClasificacionI`: the ambiguous fraction of the section is `(3b+1)/(3b+2)` for every `b ≥ 3`. -/theorem pico_un_tercio_condicional (Amb : Int → Palabra → Prop)    (ClasificacionI : ∀ b w, 3 ≤ b → (Amb b w ↔ enLista b w)) (b : ℤ) (hb : 3 ≤ b) :    (volume {L : ℝ | 0 ≤ L ∧ L ≤ (b : ℝ) + 2 / 3 ∧ Amb b (palabra13 b L)}).toReal / ((b : ℝ) + 2 / 3)      = (3 * (b : ℝ) + 1) / (3 * b + 2) := by  have e : {L : ℝ | 0 ≤ L ∧ L ≤ (b : ℝ) + 2 / 3 ∧ Amb b (palabra13 b L)} = U13 b := by    ext L    simp only [U13, Set.mem_ofPred_eq, ClasificacionI b _ hb]  rw [e]  exact pico_un_tercio b hb end CaballeroPeaks