import Erdos1135.ND.Discrepancy.A5ReferenceRealFloorPass

/-!
# A5 real harmonic cross and escape rows

This leaf keeps the two LOG-BLOCK inputs separate.  The cross-branch passage
row transports the checked natural harmonic comparison through the two real
source windows.  The escape row transports the genuine harmonic no-hit event
directly and never passes through the artificial no-hit totalizer.
-/

namespace Erdos1135
namespace ND

open Filter
open scoped Topology

noncomputable section

/-- The checked native Section 5 harmonic no-hit packet is definitionally the
ND logarithmic odd-block law and has the explicit coefficient ten.
-/
theorem pmfProb_logOddBlockPMF_natCast_sourceY_noHit_le_ten
    {B : Nat} (rate : Tao.TaoSection5NoHitRateOutput B)
    (branch : Tao.TaoSection5SourceBranch)
    (hmass : 0 < Tao.logFinsetMass
      (oddBlock (Tao.taoSection5SourceY B branch))) :
    Tao.pmfProb
        (logOddBlockPMF (Tao.taoSection5SourceY B branch) hmass)
        {N | ¬ Tao.syracuseHitsAtMost N.1 B} <=
      10 * (B : Real) ^ (-(1 / 32000 : Real)) := by
  have hmassTao :
      0 < Tao.logFinsetMass
        (Tao.oddLogWindow (Tao.taoSection5SourceLo B branch)
          (Tao.taoSection5SourceHi B branch)) := by
    simpa [oddBlock, Tao.taoNyOddWindow, Tao.taoSection5SourceLo,
      Tao.taoSection5SourceHi, alpha] using hmass
  calc
    Tao.pmfProb
        (logOddBlockPMF (Tao.taoSection5SourceY B branch) hmass)
        {N | ¬ Tao.syracuseHitsAtMost N.1 B} =
      Tao.syracuseNoHitWindowProb B
        (Tao.taoSection5SourceLo B branch)
        (Tao.taoSection5SourceHi B branch) hmassTao := by
          unfold logOddBlockPMF Tao.syracuseNoHitWindowProb
            Tao.oddLogWindowPMF Tao.syracuseNoHitAtMost oddBlock
            Tao.taoNyOddWindow Tao.taoSection5SourceLo
            Tao.taoSection5SourceHi
          rfl
    _ <= Tao.taoSection5NoHitError B :=
      rate.output.noHit_le branch hmassTao
    _ <= 10 * (B : Real) ^ (-(1 / 32000 : Real)) := rate.error_le

/-- The floor harmonic source has positive mass under the common real/floor
normalizer packet.
-/
theorem logFinsetMass_oddBlock_floorSourceY_pos
    {x : Real} (facts : Tao.TaoProp111RealFloorWindowMassFacts x)
    (branch : Tao.TaoSection5SourceBranch) :
    0 < Tao.logFinsetMass
      (oddBlock (Tao.taoSection5SourceY (Nat.floor x) branch)) := by
  have hlower :
      0 < Real.log ((Nat.floor x : Nat) : Real) / 8000 :=
    div_pos facts.log_floor_pos (by norm_num)
  cases branch with
  | alpha =>
      exact hlower.trans_le (by
        simpa [oddBlock, Tao.taoSection5SourceY, alpha] using
          facts.alpha_floor)
  | alphaSq =>
      exact hlower.trans_le (by
        simpa [oddBlock, Tao.taoSection5SourceY, alpha] using
          facts.alphaSq_floor)

/-- Genuine harmonic escape on either real source branch.  The natural
Section 5 power rate pays one scalar harmonic floor-source error and no Hahn
factor.
-/
theorem eventually_pmfProb_logOddBlock_transportSourceY_noHit_le :
    ∀ᶠ x : Real in atTop,
      ∀ (hx : 1 <= x) (branch : Tao.TaoSection5SourceBranch)
        (hReal : 0 < Tao.logFinsetMass
          (oddBlock (transportSourceY x branch))),
        Tao.pmfProb
            (logOddBlockPMF (transportSourceY x branch) hReal)
            {N | ¬ Tao.syracuseHitsAtMostReal N.1 x} <=
          10 * ((Nat.floor x : Nat) : Real) ^
              (-(1 / 32000 : Real)) +
            96000 /
              (((Nat.floor x : Nat) : Real) *
                Real.log ((Nat.floor x : Nat) : Real)) := by
  have hFloorTendsto :
      Tendsto (fun x : Real => Nat.floor x) atTop atTop :=
    tendsto_nat_floor_atTop
  filter_upwards
      [Tao.eventually_taoProp111RealFloorWindowMassFacts,
        hFloorTendsto.eventually Tao.eventually_taoSection5NoHitRateOutput]
      with x facts rate
  intro hx branch hReal
  let B := Nat.floor x
  let hFloor := logFinsetMass_oddBlock_floorSourceY_pos facts branch
  have hNatural :
      Tao.pmfProb
          (logOddBlockPMF (Tao.taoSection5SourceY B branch) hFloor)
          {N | ¬ Tao.syracuseHitsAtMost N.1 B} <=
        10 * (B : Real) ^ (-(1 / 32000 : Real)) := by
    simpa only [B, hFloor] using
      pmfProb_logOddBlockPMF_natCast_sourceY_noHit_le_ten
        rate branch hFloor
  have hFloorEvent :
      Tao.pmfProb
          (logOddBlockPMF (Tao.taoSection5SourceY B branch) hFloor)
          {N | ¬ Tao.syracuseHitsAtMostReal N.1 x} =
        Tao.pmfProb
          (logOddBlockPMF (Tao.taoSection5SourceY B branch) hFloor)
          {N | ¬ Tao.syracuseHitsAtMost N.1 B} := by
    congr 1
    ext N
    simp only [Set.mem_setOf_eq]
    rw [Tao.syracuseHitsAtMostReal_iff_floor (zero_le_one.trans hx)]
  have hdisc :
      |Tao.pmfProb
          (logOddBlockPMF (Tao.taoSection5SourceY B branch) hFloor)
          {N | ¬ Tao.syracuseHitsAtMostReal N.1 x} -
        Tao.pmfProb
          (logOddBlockPMF (transportSourceY x branch) hReal)
          {N | ¬ Tao.syracuseHitsAtMostReal N.1 x}| <=
        96000 / ((B : Real) * Real.log (B : Real)) := by
    unfold logOddBlockPMF
    change
      |Tao.pmfProb
          (Tao.logFinsetPMF
            (oddBlock (Tao.taoSection5SourceY B branch)) hFloor)
          {N | N.1 ∈ {n : Nat | ¬ Tao.syracuseHitsAtMostReal n x}} -
        Tao.pmfProb
          (Tao.logFinsetPMF (oddBlock (transportSourceY x branch)) hReal)
          {N | N.1 ∈ {n : Nat | ¬ Tao.syracuseHitsAtMostReal n x}}| <= _
    rw [Tao.pmfProb_logFinsetPMF, Tao.pmfProb_logFinsetPMF]
    simpa only [B] using
      abs_logFinsetProb_oddBlock_floor_transport_sub_le facts branch
        {N : Nat | ¬ Tao.syracuseHitsAtMostReal N x}
  calc
    Tao.pmfProb
        (logOddBlockPMF (transportSourceY x branch) hReal)
        {N | ¬ Tao.syracuseHitsAtMostReal N.1 x} <=
      Tao.pmfProb
          (logOddBlockPMF (Tao.taoSection5SourceY B branch) hFloor)
          {N | ¬ Tao.syracuseHitsAtMostReal N.1 x} +
        |Tao.pmfProb
            (logOddBlockPMF (Tao.taoSection5SourceY B branch) hFloor)
            {N | ¬ Tao.syracuseHitsAtMostReal N.1 x} -
          Tao.pmfProb
            (logOddBlockPMF (transportSourceY x branch) hReal)
            {N | ¬ Tao.syracuseHitsAtMostReal N.1 x}| := by
        linarith [neg_le_abs
          (Tao.pmfProb
              (logOddBlockPMF (Tao.taoSection5SourceY B branch) hFloor)
              {N | ¬ Tao.syracuseHitsAtMostReal N.1 x} -
            Tao.pmfProb
              (logOddBlockPMF (transportSourceY x branch) hReal)
              {N | ¬ Tao.syracuseHitsAtMostReal N.1 x})]
    _ <= Tao.pmfProb
          (logOddBlockPMF (Tao.taoSection5SourceY B branch) hFloor)
          {N | ¬ Tao.syracuseHitsAtMostReal N.1 x} +
        96000 / ((B : Real) * Real.log (B : Real)) :=
      add_le_add le_rfl hdisc
    _ = Tao.pmfProb
          (logOddBlockPMF (Tao.taoSection5SourceY B branch) hFloor)
          {N | ¬ Tao.syracuseHitsAtMost N.1 B} +
        96000 / ((B : Real) * Real.log (B : Real)) := by
      rw [hFloorEvent]
    _ <= 10 * (B : Real) ^ (-(1 / 32000 : Real)) +
        96000 / ((B : Real) * Real.log (B : Real)) :=
      add_le_add hNatural le_rfl

/-- Any two real harmonic source branches have passage laws within the raw
cross ledger `32*R_B + 4*deltaH`.  No uniform source error occurs.
-/
theorem eventually_ndA5RealHarmonicBranchesPassTV_le
    {c kappa d : Real}
    (hPhase : PhaseGap c kappa)
    (hd : 0 < d) (hd20 : d < 1 / 20)
    (hdk : d < 1 / (2 * kappa)) :
    ∀ᶠ x : Real in atTop,
      ∀ (hx : 1 <= x)
        (branch1 branch2 : Tao.TaoSection5SourceBranch)
        (hRealMass1 : 0 < Tao.logFinsetMass
          (oddBlock (transportSourceY x branch1)))
        (hRealMass2 : 0 < Tao.logFinsetMass
          (oddBlock (transportSourceY x branch2))),
        let B := Nat.floor x
        Tao.taoTV
            (logPassLaw x (transportSourceY x branch1) hx hRealMass1)
            (logPassLaw x (transportSourceY x branch2) hx hRealMass2) <=
          32 * ndA5ReferenceInteriorScalarRate B d +
            4 * (96000 /
              ((B : Real) * Real.log (B : Real))) := by
  have hFloorTendsto :
      Tendsto (fun x : Real => Nat.floor x) atTop atTop :=
    tendsto_nat_floor_atTop
  have hFloorCastTendsto :
      Tendsto (fun x : Real => ((Nat.floor x : Nat) : Real)) atTop atTop :=
    tendsto_natCast_atTop_atTop.comp hFloorTendsto
  have hLogFloorTendsto :
      Tendsto
        (fun x : Real => Real.log ((Nat.floor x : Nat) : Real))
        atTop atTop :=
    Real.tendsto_log_atTop.comp hFloorCastTendsto
  filter_upwards
      [Tao.eventually_taoProp111RealFloorWindowMassFacts,
        hFloorTendsto.eventually
          (eventually_ndA5HarmonicBranchesTotalizedPassTV_le_thirty_two
            hPhase hd hd20 hdk),
        hLogFloorTendsto.eventually_ge_atTop (300000 : Real)]
      with x facts hNaturalTV hlog
  intro hx branch1 branch2 hRealMass1 hRealMass2
  dsimp only
  let B := Nat.floor x
  have hB : 1 <= B := Tao.one_le_floor_of_one_le hx
  have hcount1 : 0 < ndA5BandCount B branch1 := by
    have : 3 <= ndA5BandCount B branch1 :=
      ndA5BandCount_ge_three_of_guards hB (by simpa only [B] using hlog)
    omega
  have hcount2 : 0 < ndA5BandCount B branch2 := by
    have : 3 <= ndA5BandCount B branch2 :=
      ndA5BandCount_ge_three_of_guards hB (by simpa only [B] using hlog)
    omega
  let hFloorMass1 :=
    logFinsetMass_ndA5OddBlock_sourceY_pos hB branch1 hcount1
  let hFloorMass2 :=
    logFinsetMass_ndA5OddBlock_sourceY_pos hB branch2 hcount2
  let hReal1 :=
    logPassLaw x (transportSourceY x branch1) hx hRealMass1
  let hFloor1 :=
    logPassLaw x (Tao.taoSection5SourceY B branch1) hx hFloorMass1
  let hFloor2 :=
    logPassLaw x (Tao.taoSection5SourceY B branch2) hx hFloorMass2
  let hReal2 :=
    logPassLaw x (transportSourceY x branch2) hx hRealMass2
  have hhFloorReal1 :
      Tao.taoTV hFloor1 hReal1 <=
        2 * (96000 / ((B : Real) * Real.log (B : Real))) := by
    simpa only [hFloor1, hReal1, B, hFloorMass1, hRealMass1] using
      taoTV_logPassLaw_floor_transport_le
        facts branch1 hx hFloorMass1 hRealMass1
  have hhRealFloor1 :
      Tao.taoTV hReal1 hFloor1 <=
        2 * (96000 / ((B : Real) * Real.log (B : Real))) := by
    rw [Tao.taoTV_comm]
    exact hhFloorReal1
  have hFloorMiddle :
      Tao.taoTV hFloor1 hFloor2 <=
        32 * ndA5ReferenceInteriorScalarRate B d := by
    have hhFloor1 :
        hFloor1 =
          ndTotalizedPassLaw (Nat.floor x)
            (Tao.one_le_floor_of_one_le hx)
            (ndA5HarmonicSourcePMF (Nat.floor x) branch1
              (Tao.one_le_floor_of_one_le hx) hcount1) := by
      simpa only [hFloor1, hFloorMass1, B] using
        logPassLaw_floorSource_eq_ndTotalizedHarmonic
          hx branch1 hcount1
    have hhFloor2 :
        hFloor2 =
          ndTotalizedPassLaw (Nat.floor x)
            (Tao.one_le_floor_of_one_le hx)
            (ndA5HarmonicSourcePMF (Nat.floor x) branch2
              (Tao.one_le_floor_of_one_le hx) hcount2) := by
      simpa only [hFloor2, hFloorMass2, B] using
        logPassLaw_floorSource_eq_ndTotalizedHarmonic
          hx branch2 hcount2
    rw [hhFloor1, hhFloor2]
    simpa only [B] using
      hNaturalTV branch1 branch2
        (Tao.one_le_floor_of_one_le hx) hcount1 hcount2
  have hhFloorReal2 :
      Tao.taoTV hFloor2 hReal2 <=
        2 * (96000 / ((B : Real) * Real.log (B : Real))) := by
    simpa only [hFloor2, hReal2, B, hFloorMass2, hRealMass2] using
      taoTV_logPassLaw_floor_transport_le
        facts branch2 hx hFloorMass2 hRealMass2
  change Tao.taoTV hReal1 hReal2 <= _
  calc
    Tao.taoTV hReal1 hReal2 <=
        Tao.taoTV hReal1 hFloor1 + Tao.taoTV hFloor1 hReal2 :=
      Tao.taoTV_triangle hReal1 hFloor1 hReal2
    _ <= Tao.taoTV hReal1 hFloor1 +
        (Tao.taoTV hFloor1 hFloor2 + Tao.taoTV hFloor2 hReal2) :=
      add_le_add le_rfl (Tao.taoTV_triangle hFloor1 hFloor2 hReal2)
    _ <= 2 * (96000 / ((B : Real) * Real.log (B : Real))) +
        (32 * ndA5ReferenceInteriorScalarRate B d +
          2 * (96000 / ((B : Real) * Real.log (B : Real)))) :=
      add_le_add hhRealFloor1 (add_le_add hFloorMiddle hhFloorReal2)
    _ = 32 * ndA5ReferenceInteriorScalarRate B d +
        4 * (96000 / ((B : Real) * Real.log (B : Real))) := by ring

/-- Ordered LOG-BLOCK full-L1 row, from the `x^alpha` harmonic source to the
`x^(alpha^2)` harmonic source.
-/
theorem eventually_ndA5RealCrossHarmonicPassTV_le
    {c kappa d : Real}
    (hPhase : PhaseGap c kappa)
    (hd : 0 < d) (hd20 : d < 1 / 20)
    (hdk : d < 1 / (2 * kappa)) :
    ∀ᶠ x : Real in atTop,
      ∀ (hx : 1 <= x),
        ∀ (hAlpha : 0 < Tao.logFinsetMass
          (oddBlock (transportSourceY x .alpha)))
          (hAlphaSq : 0 < Tao.logFinsetMass
            (oddBlock (transportSourceY x .alphaSq))),
        let B := Nat.floor x
        Tao.taoTV
            (logPassLaw x (transportSourceY x .alpha) hx hAlpha)
            (logPassLaw x (transportSourceY x .alphaSq) hx hAlphaSq) <=
          32 * ndA5ReferenceInteriorScalarRate B d +
            4 * (96000 /
              ((B : Real) * Real.log (B : Real))) := by
  filter_upwards
      [eventually_ndA5RealHarmonicBranchesPassTV_le
        hPhase hd hd20 hdk]
      with x hcross
  intro hx hAlpha hAlphaSq
  simpa using hcross hx .alpha .alphaSq hAlpha hAlphaSq

/-- The ordered LOG-BLOCK row controls every output event with the same raw
ledger.  Project full L1 is used directly, with no additional factor.
-/
theorem eventually_abs_pmfProb_ndA5RealCrossHarmonicPass_sub_le
    {c kappa d : Real}
    (hPhase : PhaseGap c kappa)
    (hd : 0 < d) (hd20 : d < 1 / 20)
    (hdk : d < 1 / (2 * kappa)) :
    ∀ᶠ x : Real in atTop,
      ∀ (hx : 1 <= x) (E : Set {M : Nat // M <= Nat.floor x}),
        ∀ (hAlpha : 0 < Tao.logFinsetMass
          (oddBlock (transportSourceY x .alpha)))
          (hAlphaSq : 0 < Tao.logFinsetMass
            (oddBlock (transportSourceY x .alphaSq))),
        let B := Nat.floor x
        |Tao.pmfProb
            (logPassLaw x (transportSourceY x .alpha) hx hAlpha) E -
          Tao.pmfProb
            (logPassLaw x (transportSourceY x .alphaSq) hx hAlphaSq) E| <=
          32 * ndA5ReferenceInteriorScalarRate B d +
            4 * (96000 /
              ((B : Real) * Real.log (B : Real))) := by
  filter_upwards [eventually_ndA5RealCrossHarmonicPassTV_le
      hPhase hd hd20 hdk] with x hcross
  intro hx E hAlpha hAlphaSq
  have hTV := hcross hx hAlpha hAlphaSq
  exact (Tao.abs_pmfProb_sub_le_taoTV _ _ E).trans hTV

end
end ND
end Erdos1135
