-- Lean 4 proof step from Mathlib.MeasureTheory.Measure.Prokhorov (goal)
Goal:
E : Type u_1
inst : MeasurableSpace E
inst_1 : TopologicalSpace E
inst_2 : T2Space E
inst_3 : BorelSpace E
u : ℕ → ℝ≥0
K : ℕ → Set E
C : ℝ≥0
hu : Tendsto u atTop (𝓝 0)
hK : ∀ (n : ℕ), IsCompact (K n)
h : NormalSpace E ∨ Monotone K
I :
  ∀ (μ : FiniteMeasure E) (n : ℕ),
    ∑ i ∈ Finset.range (n + 1), μ.restrict (disjointed K i) = μ.restrict ((partialSups K : (a : ℕ) → Set E) n)
A : ∀ (n : ℕ), IsCompact.{u_1} (X := E) ((partialSups K : (a : ℕ) → Set E) n)
f : Ultrafilter (FiniteMeasure E)
hf : {μ : FiniteMeasure E | μ.mass ≤ C ∧ ∀ (n : ℕ), (μ : (a : Set E) → ℝ≥0) (K n)ᶜ ≤ u n} ∈ f
ν : (n : ℕ) → FiniteMeasure E
ν_reg : ∀ (n : ℕ), (↑(ν n) : Measure E).InnerRegular
νK : ∀ (n : ℕ), Eq (α := ℝ≥0) ((ν n : (a : Set E) → ℝ≥0) ((partialSups K : (a : ℕ) → Set E) n)ᶜ : ℝ≥0) (0 : ℝ≥0)
hν :
  ∀ (n : ℕ), Tendsto (fun (ρ : FiniteMeasure E) ↦ ρ.restrict (disjointed K n)) (↑f : Filter (FiniteMeasure E)) (𝓝 (ν n))
B : ((Measure.sum fun (n : ℕ) ↦ (↑(ν n) : Measure E) : Measure E) : (a : Set E) → ℝ≥0∞) univ ≤ (↑C : ℝ≥0∞)
μ : FiniteMeasure E :=
  Subtype.mk (p := fun (μ : Measure E) ↦ IsFiniteMeasure μ) (Measure.sum fun (n : ℕ) ↦ (↑(ν n) : Measure E)) ⋯
g : E →ᵇ ℝ
ε : ℝ
εpos : ε > 0
I1 :
  ∀ᶠ (n : ℕ) in atTop,
    dist (α := ℝ)
        (@integral _ _ _ _ inst (∑ i ∈ Finset.range (n + 1), (↑(ν i) : Measure E)) fun (x : E) ↦ (g : (a : E) → ℝ) x)
        (∫ (x : E), (g : (a : E) → ℝ) x ∂(↑μ : Measure E)) <
      ε / 3
I2 : ∀ᶠ (n : ℕ) in atTop, ‖g‖ * (↑(u n) : ℝ) < ε / 3
n : ℕ
hn :
  dist (α := ℝ)
      (@integral _ _ _ _ inst (∑ i ∈ Finset.range (n + 1), (↑(ν i) : Measure E)) fun (x : E) ↦ (g : (a : E) → ℝ) x)
      (∫ (x : E), (g : (a : E) → ℝ) x ∂(↑μ : Measure E)) <
    ε / 3
h'n : ‖g‖ * (↑(u n) : ℝ) < ε / 3
this :
  Tendsto (β := ℝ)
    (fun (ρ : FiniteMeasure E) ↦
      @integral _ _ _ _ inst (↑(∑ i ∈ Finset.range (n + 1), ρ.restrict (disjointed K i)) : Measure E) fun (x : E) ↦
        (g : (a : E) → ℝ) x)
    (↑f : Filter (FiniteMeasure E))
    (𝓝 (@integral _ _ _ _ inst (↑(∑ i ∈ Finset.range (n + 1), ν i) : Measure E) fun (x : E) ↦ (g : (a : E) → ℝ) x))
ρ : FiniteMeasure E
hρ :
  dist (α := ℝ)
      (@integral _ _ _ _ inst (↑(∑ i ∈ Finset.range (n + 1), ρ.restrict (disjointed K i)) : Measure E) fun (x : E) ↦
        (g : (a : E) → ℝ) x)
      (@integral _ _ _ _ inst (↑(∑ i ∈ Finset.range (n + 1), ν i) : Measure E) fun (x : E) ↦ (g : (a : E) → ℝ) x) <
    ε / 3
h'ρ : ρ.mass ≤ C ∧ ∀ (n : ℕ), (ρ : (a : Set E) → ℝ≥0) (K n)ᶜ ≤ u n
calc :
  dist (α := ℝ) (∫ (x : E), (g : (a : E) → ℝ) x ∂(↑ρ : Measure E))
          (@integral _ _ _ _ inst (∑ i ∈ Finset.range (n + 1), (↑(ρ.restrict (disjointed K i)) : Measure E))
            fun (x : E) ↦ (g : (a : E) → ℝ) x) +
        dist (α := ℝ)
          (@integral _ _ _ _ inst (∑ i ∈ Finset.range (n + 1), (↑(ρ.restrict (disjointed K i)) : Measure E))
            fun (x : E) ↦ (g : (a : E) → ℝ) x)
          (@integral _ _ _ _ inst (∑ i ∈ Finset.range (n + 1), (↑(ν i) : Measure E)) fun (x : E) ↦
            (g : (a : E) → ℝ) x) +
      dist (α := ℝ)
        (@integral _ _ _ _ inst (∑ i ∈ Finset.range (n + 1), (↑(ν i) : Measure E)) fun (x : E) ↦ (g : (a : E) → ℝ) x)
        (∫ (x : E), (g : (a : E) → ℝ) x ∂(↑μ : Measure E)) <
    ε / 3 + ε / 3 + ε / 3
⊢ dist (α := ℝ) (∫ (x : E), (g : (a : E) → ℝ) x ∂(↑(id ρ) : Measure E))
      (∫ (x : E), (g : (a : E) → ℝ) x ∂(↑μ : Measure E)) <
    ε
have calc₁ : ε / 3 + ε / 3 + ε / 3 = ε
Tactic: calc dist (∫ (x : E), g x ∂ρ) (∫ (x : E), g x ∂μ)
    -- we separate away the tails from the sums up to `n`
    _ ≤ dist (∫ (x : E), g x ∂ρ)
          (∫ x, g x ∂(∑ i ∈ Finset.range (n + 1), ρ.restrict (disjointed K i)))
        + dist (∫ x, g x ∂(∑ i ∈ Finset.range (n + 1), ρ.restrict (disjointed K i)))
          (∫ x, g x ∂(∑ i ∈ Finset.range (n + 1), ν i))
        + dist (∫ x, g x ∂(∑ i ∈ Finset.range (n + 1), ν i)) (∫ (x : E), g x ∂μ) :=
      dist_triangle4 _ _ _ _
    -- each term is bounded by `ε / 3` by design.
    _ < ε / 3 + ε / 3 + ε / 3 := by
      gcongr
      · have : ρ = ρ.restrict (partialSups K n)ᶜ +
            ∑ i ∈ Finset.range (n + 1), ρ.restrict (disjointed K i) := by
          rw [I, ← FiniteMeasure.restrict_union disjoint_compl_left (A n).measurableSet]
          simp
        nth_rewrite 1 [this]
        rw [toMeasure_add, integral_add_measure (g.integrable _) (g.integrable _)]
        simp only [toMeasure_sum, dist_add_self_left]
        calc ‖∫ x, g x ∂(ρ.restrict ((partialSups K) n)ᶜ)‖
        _ ≤ ∫ x, ‖g x‖ ∂(ρ.restrict ((partialSups K) n)ᶜ) := norm_integral_le_integral_norm _
        _ ≤ ∫ x, ‖g‖ ∂(ρ.restrict ((partialSups K) n)ᶜ : Measure E) := by
          apply integral_mono_of_nonneg
          · filter_upwards with x using by positivity
          · simp
          · filter_upwards with x using norm_coe_le_norm g x
        _ = ‖g‖ * ρ ((partialSups K) n)ᶜ := by simp [mul_comm]
        _ ≤ ‖g‖ * ρ (K n)ᶜ := by gcongr; apply le_partialSups
        _ ≤ ‖g‖ * u n := by gcongr; exact h'ρ.2 n
        _ < ε / 3 := h'n
      · simpa using hρ
    _ = ε := by ring