import Erdos1135.Tao.Section3.FixedTargetPassage
import Erdos1135.Tao.Section3.Prop111RealRate

/-!
# Corrected Ambient Section 3 Scale Pilots

This leaf fixes the ambient-prefix indexing before the general Section 3
telescope.  The top source window is `N_(X^(1/alpha))`; there is no additional
ambient call reaching `N_X`.  The literal `K = 2` and `K = 3` pilots below use
exactly shared rounded windows, so no source perturbation is paid at a join.
-/

namespace Erdos1135
namespace Tao

open Filter
open scoped Topology

noncomputable section

/-- Ratio between consecutive descending ambient boundaries. -/
def taoSection3AmbientRatio : ℝ :=
  taoAlpha⁻¹

/-- Descending ambient boundary `b_i = X^(alpha⁻¹)^i`. -/
def taoSection3AmbientBoundary (X : ℝ) (i : ℕ) : ℝ :=
  X ^ (taoSection3AmbientRatio ^ i)

/-- Inclusive odd logarithmic window based at the `i`-th ambient boundary. -/
def taoSection3AmbientWindow (X : ℝ) (i : ℕ) : Finset ℕ :=
  taoNyOddWindow (taoSection3AmbientBoundary X i) taoAlpha

/-- Fixed-target good probability in one ambient boundary window. -/
def taoSection3AmbientGoodProb (B : ℕ) (X : ℝ) (i : ℕ) : ℝ :=
  logFinsetProb (taoSection3AmbientWindow X i) (syracuseThresholdGood B)

/-- Error paid by one forward fixed-target passage step. -/
def taoSection3AmbientStepError
    (errNoHit errTV : ℕ → ℝ) (q : ℝ) : ℝ :=
  errTV (Nat.floor q) + errNoHit (Nat.floor q)

theorem taoSection3AmbientRatio_pos : 0 < taoSection3AmbientRatio := by
  exact inv_pos.mpr taoAlpha_pos

theorem taoSection3AmbientRatio_lt_one : taoSection3AmbientRatio < 1 := by
  exact inv_lt_one_of_one_lt₀ taoAlpha_one_lt

theorem taoSection3AmbientBoundary_nonneg
    {X : ℝ} (hX : 0 ≤ X) (i : ℕ) :
    0 ≤ taoSection3AmbientBoundary X i :=
  Real.rpow_nonneg hX _

theorem taoSection3AmbientBoundary_pos
    {X : ℝ} (hX : 0 < X) (i : ℕ) :
    0 < taoSection3AmbientBoundary X i :=
  Real.rpow_pos_of_pos hX _

theorem taoSection3AmbientBoundary_one_le
    {X : ℝ} (hX : 1 ≤ X) (i : ℕ) :
    1 ≤ taoSection3AmbientBoundary X i := by
  exact Real.one_le_rpow hX (pow_nonneg taoSection3AmbientRatio_pos.le i)

/-- One index step is exactly one inverse-`alpha` real power. -/
theorem taoSection3AmbientBoundary_succ_eq_rpow_inv
    {X : ℝ} (hX : 0 ≤ X) (i : ℕ) :
    taoSection3AmbientBoundary X (i + 1) =
      (taoSection3AmbientBoundary X i) ^ taoAlpha⁻¹ := by
  simpa only [taoSection3AmbientBoundary, taoSection3AmbientRatio, pow_succ]
    using Real.rpow_mul hX (taoAlpha⁻¹ ^ i) taoAlpha⁻¹

/-- Raising one descending boundary by `alpha` recovers its predecessor. -/
theorem taoSection3AmbientBoundary_succ_rpow
    {X : ℝ} (hX : 0 ≤ X) (i : ℕ) :
    (taoSection3AmbientBoundary X (i + 1)) ^ taoAlpha =
      taoSection3AmbientBoundary X i := by
  rw [taoSection3AmbientBoundary_succ_eq_rpow_inv hX]
  exact Real.rpow_inv_rpow
    (taoSection3AmbientBoundary_nonneg hX i) taoAlpha_pos.ne'

/-- Two descending steps are exactly the second Proposition 1.11 source
power. -/
theorem taoSection3AmbientBoundary_add_two_rpow_sq
    {X : ℝ} (hX : 0 ≤ X) (i : ℕ) :
    (taoSection3AmbientBoundary X (i + 2)) ^ (taoAlpha ^ 2) =
      taoSection3AmbientBoundary X i := by
  have hnext :
      (taoSection3AmbientBoundary X (i + 2)) ^ taoAlpha =
        taoSection3AmbientBoundary X (i + 1) := by
    simpa only [Nat.add_assoc, Nat.reduceAdd] using
      taoSection3AmbientBoundary_succ_rpow hX (i + 1)
  calc
    (taoSection3AmbientBoundary X (i + 2)) ^ (taoAlpha ^ 2) =
        ((taoSection3AmbientBoundary X (i + 2)) ^ taoAlpha) ^ taoAlpha := by
      rw [pow_two, Real.rpow_mul
        (taoSection3AmbientBoundary_nonneg hX (i + 2))]
    _ = (taoSection3AmbientBoundary X (i + 1)) ^ taoAlpha := by rw [hnext]
    _ = taoSection3AmbientBoundary X i :=
      taoSection3AmbientBoundary_succ_rpow hX i

/-- At threshold `b_(i+2)`, Proposition 1.11 compares literally the windows
based at `b_(i+1)` and `b_i`. -/
theorem taoSection3AmbientBoundary_prop111_pair
    {X : ℝ} (hX : 0 ≤ X) (i : ℕ) :
    TaoProp111RealWindowPair
      (taoSection3AmbientBoundary X (i + 2))
      (taoNyLo (taoSection3AmbientBoundary X (i + 1)))
      (taoNyHi (taoSection3AmbientBoundary X (i + 1)) taoAlpha)
      (taoNyLo (taoSection3AmbientBoundary X i))
      (taoNyHi (taoSection3AmbientBoundary X i) taoAlpha) := by
  have hy1 := taoSection3AmbientBoundary_succ_rpow hX (i + 1)
  have hy1' :
      (taoSection3AmbientBoundary X (i + 2)) ^ taoAlpha =
        taoSection3AmbientBoundary X (i + 1) := by
    simpa only [Nat.add_assoc, Nat.reduceAdd] using hy1
  have hy2 := taoSection3AmbientBoundary_add_two_rpow_sq hX i
  simp [TaoProp111RealWindowPair, taoProp111RealY1,
    taoProp111RealY2, hy1', hy2]

/-- Every fixed ambient boundary index eventually defines a nonempty positive
mass Tao window. -/
theorem eventually_taoSection3AmbientWindow_mass_pos (i : ℕ) :
    ∀ᶠ X : ℝ in atTop,
      0 < logFinsetMass (taoSection3AmbientWindow X i) := by
  have hboundary :
      Tendsto (fun X : ℝ => taoSection3AmbientBoundary X i) atTop atTop := by
    exact tendsto_rpow_atTop (pow_pos taoSection3AmbientRatio_pos i)
  exact hboundary.eventually eventually_taoNyOddWindow_mass_pos

/-- A window whose upper real endpoint is below `B` is fixed-target good with
probability exactly one by the time-zero Syracuse iterate. -/
theorem taoSection3Ambient_seed_prob_eq_one
    {B : ℕ} {y : ℝ} (hy : 0 ≤ y)
    (hmass : 0 < logFinsetMass (taoNyOddWindow y taoAlpha))
    (hupper : y ^ taoAlpha ≤ (B : ℝ)) :
    logFinsetProb (taoNyOddWindow y taoAlpha)
      (syracuseThresholdGood B) = 1 := by
  classical
  unfold logFinsetProb
  rw [Finset.filter_eq_self.2]
  · change logFinsetMass (taoNyOddWindow y taoAlpha) /
      logFinsetMass (taoNyOddWindow y taoAlpha) = 1
    exact div_self hmass.ne'
  · intro N hN
    have hmem :
        taoNyLo y ≤ N ∧ N ≤ taoNyHi y taoAlpha ∧ N % 2 = 1 := by
      simpa only [taoNyOddWindow] using oddLogWindow_mem.mp hN
    have hfloorB : Nat.floor (y ^ taoAlpha) ≤ B := by
      have hcast : ((Nat.floor (y ^ taoAlpha) : ℕ) : ℝ) ≤ (B : ℝ) :=
        (Nat.floor_le (Real.rpow_nonneg hy taoAlpha)).trans hupper
      exact_mod_cast hcast
    exact ⟨Nat.odd_iff.mpr hmem.2.2,
      ⟨0, by simpa only [Function.iterate_zero, id_eq] using
        hmem.2.1.trans (by simpa only [taoNyHi] using hfloorB)⟩⟩

/-- Exact ambient `K = 2` pilot.  The only Proposition 1.11 call is at `b₃`
and compares `N_(b₂)` to the top ambient window `N_(b₁)`. -/
theorem taoSection3AmbientK2_of_error_bounds
    {B : ℕ} {X : ℝ} (hB : 1 ≤ B) (hX : 1 ≤ X)
    (hmass₂ : 0 < logFinsetMass (taoSection3AmbientWindow X 2))
    (hmass₁ : 0 < logFinsetMass (taoSection3AmbientWindow X 1))
    (hseed : taoSection3AmbientBoundary X 1 ≤ (B : ℝ))
    {eTV eNoHit : ℝ}
    (hTV :
      syracusePassRealFloorWindowTV
          (taoSection3AmbientBoundary X 3)
          (taoNyLo (taoSection3AmbientBoundary X 2))
          (taoNyHi (taoSection3AmbientBoundary X 2) taoAlpha)
          (taoNyLo (taoSection3AmbientBoundary X 1))
          (taoNyHi (taoSection3AmbientBoundary X 1) taoAlpha)
          (taoSection3AmbientBoundary_one_le hX 3) hmass₂ hmass₁ ≤ eTV)
    (hNoHit :
      syracuseNoHitRealWindowProb
          (taoSection3AmbientBoundary X 3)
          (taoNyLo (taoSection3AmbientBoundary X 1))
          (taoNyHi (taoSection3AmbientBoundary X 1) taoAlpha) hmass₁ ≤ eNoHit) :
    1 - (eTV + eNoHit) ≤ taoSection3AmbientGoodProb B X 1 := by
  have hX0 : 0 ≤ X := zero_le_one.trans hX
  have hseedProb : taoSection3AmbientGoodProb B X 2 = 1 := by
    apply taoSection3Ambient_seed_prob_eq_one
      (y := taoSection3AmbientBoundary X 2)
    · exact taoSection3AmbientBoundary_nonneg hX0 2
    · exact hmass₂
    · rw [show
          (taoSection3AmbientBoundary X 2) ^ taoAlpha =
            taoSection3AmbientBoundary X 1 by
          simpa using taoSection3AmbientBoundary_succ_rpow hX0 1]
      exact hseed
  have hstep :
      taoSection3AmbientGoodProb B X 2 -
          syracusePassRealFloorWindowTV
            (taoSection3AmbientBoundary X 3)
            (taoNyLo (taoSection3AmbientBoundary X 2))
            (taoNyHi (taoSection3AmbientBoundary X 2) taoAlpha)
            (taoNyLo (taoSection3AmbientBoundary X 1))
            (taoNyHi (taoSection3AmbientBoundary X 1) taoAlpha)
            (taoSection3AmbientBoundary_one_le hX 3) hmass₂ hmass₁ -
          syracuseNoHitRealWindowProb
            (taoSection3AmbientBoundary X 3)
            (taoNyLo (taoSection3AmbientBoundary X 1))
            (taoNyHi (taoSection3AmbientBoundary X 1) taoAlpha) hmass₁ ≤
        taoSection3AmbientGoodProb B X 1 := by
    simpa only [taoSection3AmbientGoodProb, taoSection3AmbientWindow,
      taoNyOddWindow] using
      taoSection3_fixedTargetGood_real_oneStep
        (b := B) (x := taoSection3AmbientBoundary X 3)
        (lo₁ := taoNyLo (taoSection3AmbientBoundary X 2))
        (hi₁ := taoNyHi (taoSection3AmbientBoundary X 2) taoAlpha)
        (lo₂ := taoNyLo (taoSection3AmbientBoundary X 1))
        (hi₂ := taoNyHi (taoSection3AmbientBoundary X 1) taoAlpha)
        hB (taoSection3AmbientBoundary_one_le hX 3) hmass₂ hmass₁
  linarith

/-- Exact ambient `K = 3` pilot.  The calls at `b₄` and `b₃` share the
literal middle probability for `N_(b₂)`. -/
theorem taoSection3AmbientK3_of_error_bounds
    {B : ℕ} {X : ℝ} (hB : 1 ≤ B) (hX : 1 ≤ X)
    (hmass₃ : 0 < logFinsetMass (taoSection3AmbientWindow X 3))
    (hmass₂ : 0 < logFinsetMass (taoSection3AmbientWindow X 2))
    (hmass₁ : 0 < logFinsetMass (taoSection3AmbientWindow X 1))
    (hseed : taoSection3AmbientBoundary X 2 ≤ (B : ℝ))
    {eTV₄ eNoHit₄ eTV₃ eNoHit₃ : ℝ}
    (hTV₄ :
      syracusePassRealFloorWindowTV
          (taoSection3AmbientBoundary X 4)
          (taoNyLo (taoSection3AmbientBoundary X 3))
          (taoNyHi (taoSection3AmbientBoundary X 3) taoAlpha)
          (taoNyLo (taoSection3AmbientBoundary X 2))
          (taoNyHi (taoSection3AmbientBoundary X 2) taoAlpha)
          (taoSection3AmbientBoundary_one_le hX 4) hmass₃ hmass₂ ≤ eTV₄)
    (hNoHit₄ :
      syracuseNoHitRealWindowProb
          (taoSection3AmbientBoundary X 4)
          (taoNyLo (taoSection3AmbientBoundary X 2))
          (taoNyHi (taoSection3AmbientBoundary X 2) taoAlpha) hmass₂ ≤ eNoHit₄)
    (hTV₃ :
      syracusePassRealFloorWindowTV
          (taoSection3AmbientBoundary X 3)
          (taoNyLo (taoSection3AmbientBoundary X 2))
          (taoNyHi (taoSection3AmbientBoundary X 2) taoAlpha)
          (taoNyLo (taoSection3AmbientBoundary X 1))
          (taoNyHi (taoSection3AmbientBoundary X 1) taoAlpha)
          (taoSection3AmbientBoundary_one_le hX 3) hmass₂ hmass₁ ≤ eTV₃)
    (hNoHit₃ :
      syracuseNoHitRealWindowProb
          (taoSection3AmbientBoundary X 3)
          (taoNyLo (taoSection3AmbientBoundary X 1))
          (taoNyHi (taoSection3AmbientBoundary X 1) taoAlpha) hmass₁ ≤ eNoHit₃) :
    1 - (eTV₄ + eNoHit₄) - (eTV₃ + eNoHit₃) ≤
      taoSection3AmbientGoodProb B X 1 := by
  have hX0 : 0 ≤ X := zero_le_one.trans hX
  have hseedProb : taoSection3AmbientGoodProb B X 3 = 1 := by
    apply taoSection3Ambient_seed_prob_eq_one
      (y := taoSection3AmbientBoundary X 3)
    · exact taoSection3AmbientBoundary_nonneg hX0 3
    · exact hmass₃
    · rw [show
          (taoSection3AmbientBoundary X 3) ^ taoAlpha =
            taoSection3AmbientBoundary X 2 by
          simpa using taoSection3AmbientBoundary_succ_rpow hX0 2]
      exact hseed
  have hstep₄ :
      taoSection3AmbientGoodProb B X 3 -
          syracusePassRealFloorWindowTV
            (taoSection3AmbientBoundary X 4)
            (taoNyLo (taoSection3AmbientBoundary X 3))
            (taoNyHi (taoSection3AmbientBoundary X 3) taoAlpha)
            (taoNyLo (taoSection3AmbientBoundary X 2))
            (taoNyHi (taoSection3AmbientBoundary X 2) taoAlpha)
            (taoSection3AmbientBoundary_one_le hX 4) hmass₃ hmass₂ -
          syracuseNoHitRealWindowProb
            (taoSection3AmbientBoundary X 4)
            (taoNyLo (taoSection3AmbientBoundary X 2))
            (taoNyHi (taoSection3AmbientBoundary X 2) taoAlpha) hmass₂ ≤
        taoSection3AmbientGoodProb B X 2 := by
    simpa only [taoSection3AmbientGoodProb, taoSection3AmbientWindow,
      taoNyOddWindow] using
      taoSection3_fixedTargetGood_real_oneStep
        (b := B) (x := taoSection3AmbientBoundary X 4)
        (lo₁ := taoNyLo (taoSection3AmbientBoundary X 3))
        (hi₁ := taoNyHi (taoSection3AmbientBoundary X 3) taoAlpha)
        (lo₂ := taoNyLo (taoSection3AmbientBoundary X 2))
        (hi₂ := taoNyHi (taoSection3AmbientBoundary X 2) taoAlpha)
        hB (taoSection3AmbientBoundary_one_le hX 4) hmass₃ hmass₂
  have hstep₃ :
      taoSection3AmbientGoodProb B X 2 -
          syracusePassRealFloorWindowTV
            (taoSection3AmbientBoundary X 3)
            (taoNyLo (taoSection3AmbientBoundary X 2))
            (taoNyHi (taoSection3AmbientBoundary X 2) taoAlpha)
            (taoNyLo (taoSection3AmbientBoundary X 1))
            (taoNyHi (taoSection3AmbientBoundary X 1) taoAlpha)
            (taoSection3AmbientBoundary_one_le hX 3) hmass₂ hmass₁ -
          syracuseNoHitRealWindowProb
            (taoSection3AmbientBoundary X 3)
            (taoNyLo (taoSection3AmbientBoundary X 1))
            (taoNyHi (taoSection3AmbientBoundary X 1) taoAlpha) hmass₁ ≤
        taoSection3AmbientGoodProb B X 1 := by
    simpa only [taoSection3AmbientGoodProb, taoSection3AmbientWindow,
      taoNyOddWindow] using
      taoSection3_fixedTargetGood_real_oneStep
        (b := B) (x := taoSection3AmbientBoundary X 3)
        (lo₁ := taoNyLo (taoSection3AmbientBoundary X 2))
        (hi₁ := taoNyHi (taoSection3AmbientBoundary X 2) taoAlpha)
        (lo₂ := taoNyLo (taoSection3AmbientBoundary X 1))
        (hi₂ := taoNyHi (taoSection3AmbientBoundary X 1) taoAlpha)
        hB (taoSection3AmbientBoundary_one_le hX 3) hmass₂ hmass₁
  linarith

/-- The checked real Proposition 1.11 packet supplies the exact `K = 2`
pilot, while retaining its common eventual rate witnesses. -/
theorem taoSection3AmbientK2_checked
    {B : ℕ} {X : ℝ} (hB : 1 ≤ B) (hX : 1 ≤ X)
    (hmass₂ : 0 < logFinsetMass (taoSection3AmbientWindow X 2))
    (hmass₁ : 0 < logFinsetMass (taoSection3AmbientWindow X 1))
    (hseed : taoSection3AmbientBoundary X 1 ≤ (B : ℝ)) :
    ∃ errNoHit errTV : ℕ → ℝ, ∃ C c : ℝ,
      0 ≤ C ∧ 0 < c ∧
        (∀ᶠ x : ℝ in atTop,
          0 ≤ errNoHit (Nat.floor x) ∧
            0 ≤ errTV (Nat.floor x) ∧
              errNoHit (Nat.floor x) ≤ C * x ^ (-c) ∧
                errTV (Nat.floor x) ≤ C * (Real.log x) ^ (-c)) ∧
          1 - taoSection3AmbientStepError errNoHit errTV
              (taoSection3AmbientBoundary X 3) ≤
            taoSection3AmbientGoodProb B X 1 := by
  rcases taoProp111RealFirstPassageStabilizationRate_checked with
    ⟨errNoHit, errTV, C, c, hC, hc, hrate, hbounds⟩
  have hX0 : 0 ≤ X := zero_le_one.trans hX
  have hpair :
      TaoProp111RealWindowPair
        (taoSection3AmbientBoundary X 3)
        (taoNyLo (taoSection3AmbientBoundary X 2))
        (taoNyHi (taoSection3AmbientBoundary X 2) taoAlpha)
        (taoNyLo (taoSection3AmbientBoundary X 1))
        (taoNyHi (taoSection3AmbientBoundary X 1) taoAlpha) := by
    simpa using taoSection3AmbientBoundary_prop111_pair hX0 1
  have hpacket := hbounds
    (taoSection3AmbientBoundary X 3)
    (taoNyLo (taoSection3AmbientBoundary X 2))
    (taoNyHi (taoSection3AmbientBoundary X 2) taoAlpha)
    (taoNyLo (taoSection3AmbientBoundary X 1))
    (taoNyHi (taoSection3AmbientBoundary X 1) taoAlpha)
    (taoSection3AmbientBoundary_one_le hX 3) hpair hmass₂ hmass₁
  refine ⟨errNoHit, errTV, C, c, hC, hc, hrate, ?_⟩
  simpa only [taoSection3AmbientStepError] using
    taoSection3AmbientK2_of_error_bounds hB hX hmass₂ hmass₁ hseed
      (eTV := errTV (Nat.floor (taoSection3AmbientBoundary X 3)))
      (eNoHit := errNoHit (Nat.floor (taoSection3AmbientBoundary X 3)))
      hpacket.2.2 hpacket.2.1

/-- The checked real Proposition 1.11 packet supplies the exact `K = 3`
pilot with one common error pair and a literally shared middle window. -/
theorem taoSection3AmbientK3_checked
    {B : ℕ} {X : ℝ} (hB : 1 ≤ B) (hX : 1 ≤ X)
    (hmass₃ : 0 < logFinsetMass (taoSection3AmbientWindow X 3))
    (hmass₂ : 0 < logFinsetMass (taoSection3AmbientWindow X 2))
    (hmass₁ : 0 < logFinsetMass (taoSection3AmbientWindow X 1))
    (hseed : taoSection3AmbientBoundary X 2 ≤ (B : ℝ)) :
    ∃ errNoHit errTV : ℕ → ℝ, ∃ C c : ℝ,
      0 ≤ C ∧ 0 < c ∧
        (∀ᶠ x : ℝ in atTop,
          0 ≤ errNoHit (Nat.floor x) ∧
            0 ≤ errTV (Nat.floor x) ∧
              errNoHit (Nat.floor x) ≤ C * x ^ (-c) ∧
                errTV (Nat.floor x) ≤ C * (Real.log x) ^ (-c)) ∧
          1 - taoSection3AmbientStepError errNoHit errTV
                (taoSection3AmbientBoundary X 4) -
              taoSection3AmbientStepError errNoHit errTV
                (taoSection3AmbientBoundary X 3) ≤
            taoSection3AmbientGoodProb B X 1 := by
  rcases taoProp111RealFirstPassageStabilizationRate_checked with
    ⟨errNoHit, errTV, C, c, hC, hc, hrate, hbounds⟩
  have hX0 : 0 ≤ X := zero_le_one.trans hX
  have hpair₄ :
      TaoProp111RealWindowPair
        (taoSection3AmbientBoundary X 4)
        (taoNyLo (taoSection3AmbientBoundary X 3))
        (taoNyHi (taoSection3AmbientBoundary X 3) taoAlpha)
        (taoNyLo (taoSection3AmbientBoundary X 2))
        (taoNyHi (taoSection3AmbientBoundary X 2) taoAlpha) := by
    simpa using taoSection3AmbientBoundary_prop111_pair hX0 2
  have hpair₃ :
      TaoProp111RealWindowPair
        (taoSection3AmbientBoundary X 3)
        (taoNyLo (taoSection3AmbientBoundary X 2))
        (taoNyHi (taoSection3AmbientBoundary X 2) taoAlpha)
        (taoNyLo (taoSection3AmbientBoundary X 1))
        (taoNyHi (taoSection3AmbientBoundary X 1) taoAlpha) := by
    simpa using taoSection3AmbientBoundary_prop111_pair hX0 1
  have hpacket₄ := hbounds
    (taoSection3AmbientBoundary X 4)
    (taoNyLo (taoSection3AmbientBoundary X 3))
    (taoNyHi (taoSection3AmbientBoundary X 3) taoAlpha)
    (taoNyLo (taoSection3AmbientBoundary X 2))
    (taoNyHi (taoSection3AmbientBoundary X 2) taoAlpha)
    (taoSection3AmbientBoundary_one_le hX 4) hpair₄ hmass₃ hmass₂
  have hpacket₃ := hbounds
    (taoSection3AmbientBoundary X 3)
    (taoNyLo (taoSection3AmbientBoundary X 2))
    (taoNyHi (taoSection3AmbientBoundary X 2) taoAlpha)
    (taoNyLo (taoSection3AmbientBoundary X 1))
    (taoNyHi (taoSection3AmbientBoundary X 1) taoAlpha)
    (taoSection3AmbientBoundary_one_le hX 3) hpair₃ hmass₂ hmass₁
  refine ⟨errNoHit, errTV, C, c, hC, hc, hrate, ?_⟩
  simpa only [taoSection3AmbientStepError] using
    taoSection3AmbientK3_of_error_bounds hB hX hmass₃ hmass₂ hmass₁ hseed
      (eTV₄ := errTV (Nat.floor (taoSection3AmbientBoundary X 4)))
      (eNoHit₄ := errNoHit (Nat.floor (taoSection3AmbientBoundary X 4)))
      (eTV₃ := errTV (Nat.floor (taoSection3AmbientBoundary X 3)))
      (eNoHit₃ := errNoHit (Nat.floor (taoSection3AmbientBoundary X 3)))
      hpacket₄.2.2 hpacket₄.2.1 hpacket₃.2.2 hpacket₃.2.1

end


end Tao
end Erdos1135
