import Erdos1135.KrasikovLagarias.RealCertificate
import Mathlib.Analysis.SpecialFunctions.Pow.Asymptotics

/-!
# Exponent reserve for KL predecessor bounds

A positive constant multiplying a strictly larger power can be absorbed into
the exponent gap.  This module records the generic real statement and the
specific `0.881` to `0.88` step used by the flagship certificate.
-/

namespace Erdos1135
namespace KrasikovLagarias

open Filter

theorem eventually_rpow_le_of_const_mul_rpow_le
    {f : Real → Real} {constant alpha beta : Real}
    (hconstant : 0 < constant) (hexponents : beta < alpha)
    (hbound : ∀ᶠ x in atTop, constant * x ^ alpha ≤ f x) :
    ∀ᶠ x in atTop, x ^ beta ≤ f x := by
  have hfactor : ∀ᶠ x : Real in atTop, 1 ≤ constant * x ^ (alpha - beta) :=
    ((tendsto_rpow_atTop (sub_pos.mpr hexponents)).const_mul_atTop hconstant).eventually
      (eventually_ge_atTop 1)
  filter_upwards [hbound, hfactor, eventually_gt_atTop (0 : Real)] with x hx hfactor hxpos
  calc
    x ^ beta ≤ x ^ beta * (constant * x ^ (alpha - beta)) :=
      le_mul_of_one_le_right (Real.rpow_nonneg hxpos.le _) hfactor
    _ = constant * (x ^ beta * x ^ (alpha - beta)) := by ring
    _ = constant * x ^ alpha := by
      rw [← Real.rpow_add hxpos]
      congr 2
      ring
    _ ≤ f x := hx

theorem eventually_rpow_22_25_le_of_const_mul_rpow_881_1000_le
    {f : Real → Real} {constant : Real} (hconstant : 0 < constant)
    (hbound :
      ∀ᶠ x in atTop, constant * x ^ ((881 : Real) / 1000) ≤ f x) :
    ∀ᶠ x in atTop, x ^ ((22 : Real) / 25) ≤ f x := by
  exact eventually_rpow_le_of_const_mul_rpow_le hconstant (by norm_num) hbound

/-- The `0.001` reserve in the level-18 `0.901` certificate absorbs the
target-dependent positive constant and yields the unit-coefficient exponent
`0.90`. -/
theorem eventually_rpow_9_10_le_of_const_mul_rpow_901_1000_le
    {f : Real → Real} {constant : Real} (hconstant : 0 < constant)
    (hbound :
      ∀ᶠ x in atTop, constant * x ^ ((901 : Real) / 1000) ≤ f x) :
    ∀ᶠ x in atTop, x ^ ((9 : Real) / 10) ≤ f x := by
  exact eventually_rpow_le_of_const_mul_rpow_le hconstant (by norm_num) hbound

end KrasikovLagarias
end Erdos1135
