/-- A monotone function is almost everywhere differentiable, with derivative equal to the
Radon-Nikodym derivative of the associated Stieltjes measure with respect to Lebesgue. -/
theorem Monotone.ae_hasDerivAt {f : ℝ → ℝ} (hf : Monotone f) :
    ∀ᵐ x, HasDerivAt f (rnDeriv hf.stieltjesFunction.measure volume x).toReal x := by
  /- We already know that the Stieltjes function associated to `f` (i.e., `g : x ↦ f (x^+)`) is
    differentiable almost everywhere. We reduce to this statement by sandwiching values of `f` with
    values of `g`, by shifting with `(y - x)^2` (which has no influence on the relevant
    scale `y - x`.)-/
  filter_upwards [hf.stieltjesFunction.ae_hasDerivAt,
    hf.countable_not_continuousAt.ae_not_mem volume] with x hx h'x
  have A : hf.stieltjesFunction x = f x := by
    rw [Classical.not_not, hf.continuousAt_iff_leftLim_eq_rightLim] at h'x
    apply le_antisymm _ (hf.le_rightLim (le_refl _))
    rw [← h'x]
    exact hf.leftLim_le (le_refl _)
  rw [hasDerivAt_iff_tendsto_slope, (nhds_left'_sup_nhds_right' x).symm, tendsto_sup,
    slope_fun_def_field, A] at hx
  -- prove differentiability on the right, by sandwiching with values of `g`
  have L1 : Tendsto (fun y => (f y - f x) / (y - x)) (𝓝[>] x)
      (𝓝 (rnDeriv hf.stieltjesFunction.measure volume x).toReal) := by
    -- limit of a helper function, with a small shift compared to `g`
    have : Tendsto (fun y => (hf.stieltjesFunction (y + -1 * (y - x) ^ 2) - f x) / (y - x)) (𝓝[>] x)
        (𝓝 (rnDeriv hf.stieltjesFunction.measure volume x).toReal) := by
      apply tendsto_apply_add_mul_sq_div_sub (nhds_right'_le_nhds_ne x) hx.2
      apply tendsto_nhdsWithin_of_tendsto_nhds_of_eventually_within
      · apply Tendsto.mono_left _ nhdsWithin_le_nhds
        have : Tendsto (fun y : ℝ => y + -↑1 * (y - x) ^ 2) (𝓝 x) (𝓝 (x + -↑1 * (x - x) ^ 2)) :=
          tendsto_id.add (((tendsto_id.sub_const x).pow 2).const_mul (-1))
        simpa using this
      · have : Ioo x (x + 1) ∈ 𝓝[>] x := by
          apply Ioo_mem_nhdsWithin_Ioi; exact ⟨le_refl _, by linarith⟩
        filter_upwards [this]
        rintro y ⟨hy : x < y, h'y : y < x + 1⟩
        rw [mem_Ioi]
        norm_num; nlinarith
    -- apply the sandwiching argument, with the helper function and `g`
    apply tendsto_of_tendsto_of_tendsto_of_le_of_le' this hx.2
    · filter_upwards [self_mem_nhdsWithin]
      rintro y (hy : x < y)
      have : ↑0 < (y - x) ^ 2 := sq_pos_of_pos (sub_pos.2 hy)
      apply div_le_div_of_le_of_nonneg _ (sub_pos.2 hy).le
      exact (sub_le_sub_iff_right _).2 (hf.rightLim_le (by norm_num; linarith))
    · filter_upwards [self_mem_nhdsWithin]
      rintro y (hy : x < y)
      apply div_le_div_of_le_of_nonneg _ (sub_pos.2 hy).le
      exact (sub_le_sub_iff_right _).2 (hf.le_rightLim (le_refl y))
  -- prove differentiability on the left, by sandwiching with values of `g`
  

Proof state:
case h
f : ℝ → ℝ
hf : Monotone f
x : ℝ
hx :
  Tendsto (fun b => (↑(stieltjesFunction hf) b - f x) / (b - x)) (𝓝[<] x)
      (𝓝 (ENNReal.toReal (rnDeriv (StieltjesFunction.measure (stieltjesFunction hf)) volume x))) ∧
    Tendsto (fun b => (↑(stieltjesFunction hf) b - f x) / (b - x)) (𝓝[>] x)
      (𝓝 (ENNReal.toReal (rnDeriv (StieltjesFunction.measure (stieltjesFunction hf)) volume x)))
h'x : ¬¬ContinuousAt f x
A : ↑(stieltjesFunction hf) x = f x
L1 :
  Tendsto (fun y => (f y - f x) / (y - x)) (𝓝[>] x)
    (𝓝 (ENNReal.toReal (rnDeriv (StieltjesFunction.measure (stieltjesFunction hf)) volume x)))
⊢ HasDerivAt f (ENNReal.toReal (rnDeriv (StieltjesFunction.measure (stieltjesFunction hf)) volume x)) x

Next tactic:
have L2 : Tendsto (fun y => (f y - f x) / (y - x)) (𝓝[<] x)
      (𝓝 (rnDeriv hf.stieltjesFunction.measure volume x).toReal) := by
    -- limit of a helper function, with a small shift compared to `g`
    have : Tendsto (fun y => (hf.stieltjesFunction (y + -1 * (y - x) ^ 2) - f x) / (y - x)) (𝓝[<] x)
        (𝓝 (rnDeriv hf.stieltjesFunction.measure volume x).toReal) := by
      apply tendsto_apply_add_mul_sq_div_sub (nhds_left'_le_nhds_ne x) hx.1
      apply tendsto_nhdsWithin_of_tendsto_nhds_of_eventually_within
      · apply Tendsto.mono_left _ nhdsWithin_le_nhds
        have : Tendsto (fun y : ℝ => y + -↑1 * (y - x) ^ 2) (𝓝 x) (𝓝 (x + -↑1 * (x - x) ^ 2)) :=
          tendsto_id.add (((tendsto_id.sub_const x).pow 2).const_mul (-1))
        simpa using this
      · have : Ioo (x - 1) x ∈ 𝓝[<] x := by
          apply Ioo_mem_nhdsWithin_Iio; exact ⟨by linarith, le_refl _⟩
        filter_upwards [this]
        rintro y ⟨hy : x - 1 < y, h'y : y < x⟩
        rw [mem_Iio]
        norm_num; nlinarith
    -- apply the sandwiching argument, with `g` and the helper function
    apply tendsto_of_tendsto_of_tendsto_of_le_of_le' hx.1 this
    · filter_upwards [self_mem_nhdsWithin]
      rintro y (hy : y < x)
      apply div_le_div_of_nonpos_of_le (sub_neg.2 hy).le
      exact (sub_le_sub_iff_right _).2 (hf.le_rightLim (le_refl _))
    · filter_upwards [self_mem_nhdsWithin]
      rintro y (hy : y < x)
      have : ↑0 < (y - x) ^ 2 := sq_pos_of_neg (sub_neg.2 hy)
      apply div_le_div_of_nonpos_of_le (sub_neg.2 hy).le
      exact (sub_le_sub_iff_right _).2 (hf.rightLim_le (by norm_num; linarith))