import Mathlib
import FoML


open MeasureTheory ProbabilityTheory

theorem extracted_formal_statement_23.{u_2, u_1} {Ω : Type u_1} [inst : MeasurableSpace Ω] {μ : Measure Ω}
  [inst_1 : IsProbabilityMeasure μ] {𝓧 : Type u_2} {m𝓧 : MeasurableSpace 𝓧} [hnonempty𝓧 : Nonempty 𝓧] {m : ℕ}
  {X' : Ω → 𝓧} (hX'' : Measurable X') {f' : (Fin m → 𝓧) → ℝ} {c' : Fin m → ℝ}
  (hfι : ∀ (i : Fin m) (x : Fin m → 𝓧) (x' : 𝓧), |f' x - f' (Function.update x i x')| ≤ c' i)
  (hf'' : StronglyMeasurable f') {ε : ℝ} (hε : ε ≥ 0) {t : ℝ} (ht : t ≥ 0) (ht' : t * ∑ i, c' i ^ 2 ≤ 1)
  (hX''' : ∀ (i : Fin m), Measurable fun ω => X' (ω i)) :
  let Y := fun k Xk =>
    ∫ (x : Fin m → Ω), (fun ω => f' fun i => if h : ↑i < ↑k then Xk ⟨↑i, h⟩ else X' (ω i)) x ∂Measure.pi fun x => μ;
  let A := fun k Xk =>
    ⨅ x,
      ∫ (x_1 : Fin m → Ω),
        (fun ω => f' fun i => if h : ↑i < ↑k then Xk ⟨↑i, h⟩ else if ↑i = ↑k then x else X' (ω i))
          x_1 ∂Measure.pi fun x => μ;
  let B := fun k Xk =>
    ⨆ x,
      ∫ (x_1 : Fin m → Ω),
        (fun ω => f' fun i => if h : ↑i < ↑k then Xk ⟨↑i, h⟩ else if ↑i = ↑k then x else X' (ω i))
          x_1 ∂Measure.pi fun x => μ;
  (∀ (k : Fin m) (Xk : Fin ↑k → 𝓧) (x : 𝓧),
      Y k.succ (Fin.snoc Xk x) =
        ∫ (x_1 : Fin m → Ω),
          (fun ω => f' fun i => if h : ↑i < ↑k then Xk ⟨↑i, h⟩ else if ↑i = ↑k then x else X' (ω i))
            x_1 ∂Measure.pi fun x => μ) →
    ∀ (x₀ : 𝓧),
      let bdf := |f' fun x => x₀| + ∑ i, c' i;
      (∀ (xi : Fin m → 𝓧), |f' xi| ≤ bdf) →
        (∀ (k : Fin m) (Xk : Fin ↑k → 𝓧) (x : 𝓧),
            Integrable (fun a => f' fun i => if h : ↑i < ↑k then Xk ⟨↑i, h⟩ else if ↑i = ↑k then x else X' (a i))
              (Measure.pi fun x => μ)) →
          (∀ (k : Fin m) (Xk : Fin ↑k → 𝓧) (x : 𝓧), A k Xk ≤ Y k.succ (Fin.snoc Xk x)) →
            (∀ (k : Fin m) (Xk : Fin ↑k → 𝓧),
                𝓧 →
                  BddAbove
                    (Set.range fun x =>
                      ∫ (a : Fin m → Ω),
                        (fun ω => f' fun i => if h : ↑i < ↑k then Xk ⟨↑i, h⟩ else if ↑i = ↑k then x else X' (ω i))
                          a ∂Measure.pi fun x => μ)) →
              (∀ (k : Fin m) (Xk : Fin ↑k → 𝓧) (x : 𝓧), B k Xk ≥ Y k.succ (Fin.snoc Xk x)) →
                (∀ (k : Fin m) (Xk : Fin ↑k → 𝓧), B k Xk ≤ A k Xk + c' k) →
                  (∀ (k : Fin m) (Xk : Fin ↑k → 𝓧),
                      (∫ (ω : Fin m → Ω), Y k.succ (Fin.snoc Xk (X' (ω k))) ∂Measure.pi fun x => μ) = Y k.castSucc Xk) →
                    (∀ (k : Fin m.succ), Measurable (Y k)) →
                      (∀ (k : Fin m.succ) (Xk : Fin ↑k → 𝓧), |Y k Xk| ≤ bdf) →
                        let V := fun k Xk ω => Y k.succ (Fin.snoc Xk (X' (ω k))) - Y k.castSucc Xk;
                        let t'' := 4 * ε * t;
                        t'' ≥ 0 →
                          (∀ (k : Fin m) (Xk : Fin ↑k → 𝓧),
                              (∫ (ω : Fin m → Ω), Real.exp (t'' * V k Xk ω) ∂Measure.pi fun x => μ) ≤
                                Real.exp (t'' ^ 2 * c' k ^ 2 / 8)) →
                            let E := Y 0 fun x => x₀;
                            (∀ (k : ℕ) (h : k ≤ m),
                                Integrable
                                  (fun x =>
                                    Real.exp
                                      (t'' * ((Y ⟨k, Nat.lt_add_one_of_le h⟩ fun i => X' (x (Fin.castLE h i))) - E)))
                                  (Measure.pi fun x => μ)) →
                              ∀ (k : ℕ),
                                (∀ (h : k ≤ m),
                                    (∫ (ω : Fin m → Ω),
                                        Real.exp
                                          (t'' *
                                            ((Y ⟨k, Nat.lt_add_one_of_le h⟩ fun i => X' (ω (Fin.castLE h i))) -
                                              E)) ∂Measure.pi fun x => μ) ≤
                                      Real.exp ((t'' ^ 2 * ∑ i, c' (Fin.castLE h i) ^ 2) / 8)) →
                                  ∀ (h : k + 1 ≤ m),
                                    (∫ (ω : Fin m → Ω),
                                          Real.exp
                                            (t'' *
                                              ((Y ⟨k, Nat.lt_add_one_of_le (Nat.le_of_succ_le h)⟩ fun i =>
                                                  X' (ω (Fin.castLE (Nat.le_of_succ_le h) i))) -
                                                E)) ∂Measure.pi fun x => μ) ≤
                                        Real.exp ((t'' ^ 2 * ∑ i, c' (Fin.castLE (Nat.le_of_succ_le h) i) ^ 2) / 8) →
                                      Integrable
                                          (fun x =>
                                            Real.exp
                                              (t'' *
                                                ((Y ⟨k, Nat.lt_add_one_of_le (Nat.le_of_succ_le h)⟩ fun i =>
                                                    X' (x (Fin.castLE (Nat.le_of_succ_le h) i))) -
                                                  E)))
                                          (Measure.pi fun x => μ) →
                                        (∫ (ω : Fin m → Ω),
                                            Real.exp
                                                (t'' *
                                                  ((Y ⟨k, Nat.lt_succ_of_lt h⟩ fun i => X' (ω (Fin.castLE h ↑↑i))) -
                                                    E)) *
                                              ∫ (ω' : Fin m → Ω),
                                                Real.exp
                                                  (t'' *
                                                    V ⟨k, h⟩ (fun i => X' (ω (Fin.castLE h ↑↑i)))
                                                      ω') ∂Measure.pi fun x => μ ∂Measure.pi fun x => μ) ≤
                                          ∫ (ω : Fin m → Ω),
                                            Real.exp
                                                (t'' *
                                                  ((Y ⟨k, Nat.lt_succ_of_lt h⟩ fun i => X' (ω (Fin.castLE h ↑↑i))) -
                                                    E)) *
                                              Real.exp (t'' ^ 2 * c' ⟨k, h⟩ ^ 2 / 8) ∂Measure.pi fun x => μ := sorry

Tactic state before:
Ω : Type u_1
inst✝¹ : MeasurableSpace Ω
μ : Measure Ω
inst✝ : IsProbabilityMeasure μ
𝓧 : Type u_2
m𝓧 : MeasurableSpace 𝓧
hnonempty𝓧 : Nonempty 𝓧
m : ℕ
X' : Ω → 𝓧
hX'' : Measurable X'
f' : (Fin m → 𝓧) → ℝ
c' : Fin m → ℝ
hfι : ∀ (i : Fin m) (x : Fin m → 𝓧) (x' : 𝓧), |f' x - f' (Function.update x i x')| ≤ c' i
hf'' : StronglyMeasurable f'
ε : ℝ
hε : ε ≥ 0
t : ℝ
ht : t ≥ 0
ht' : t * ∑ i, c' i ^ 2 ≤ 1
hX''' : ∀ (i : Fin m), Measurable fun ω => X' (ω i)
Y : (k : Fin m.succ) → (Fin ↑k → 𝓧) → ℝ :=
  fun k Xk =>
    ∫ (x : Fin m → Ω), (fun ω => f' fun i => if h : ↑i < ↑k then Xk ⟨↑i, h⟩ else X' (ω i)) x ∂Measure.pi fun x => μ
A : (k : Fin m) → (Fin ↑k → 𝓧) → ℝ :=
  fun k Xk =>
    ⨅ x,
      ∫ (x_1 : Fin m → Ω),
        (fun ω => f' fun i => if h : ↑i < ↑k then Xk ⟨↑i, h⟩ else if ↑i = ↑k then x else X' (ω i))
          x_1 ∂Measure.pi fun x => μ
B : (k : Fin m) → (Fin ↑k → 𝓧) → ℝ :=
  fun k Xk =>
    ⨆ x,
      ∫ (x_1 : Fin m → Ω),
        (fun ω => f' fun i => if h : ↑i < ↑k then Xk ⟨↑i, h⟩ else if ↑i = ↑k then x else X' (ω i))
          x_1 ∂Measure.pi fun x => μ
AB_rw :
  ∀ (k : Fin m) (Xk : Fin ↑k → 𝓧) (x : 𝓧),
    Y k.succ (Fin.snoc Xk x) =
      ∫ (x_1 : Fin m → Ω),
        (fun ω => f' fun i => if h : ↑i < ↑k then Xk ⟨↑i, h⟩ else if ↑i = ↑k then x else X' (ω i))
          x_1 ∂Measure.pi fun x => μ
x₀ : 𝓧
bdf : ℝ := |f' fun x => x₀| + ∑ i, c' i
hfbdd : ∀ (xi : Fin m → 𝓧), |f' xi| ≤ bdf
hintegrableAB :
  ∀ (k : Fin m) (Xk : Fin ↑k → 𝓧) (x : 𝓧),
    Integrable (fun a => f' fun i => if h : ↑i < ↑k then Xk ⟨↑i, h⟩ else if ↑i = ↑k then x else X' (a i))
      (Measure.pi fun x => μ)
hAY : ∀ (k : Fin m) (Xk : Fin ↑k → 𝓧) (x : 𝓧), A k Xk ≤ Y k.succ (Fin.snoc Xk x)
hBddAbove :
  ∀ (k : Fin m) (Xk : Fin ↑k → 𝓧),
    𝓧 →
      BddAbove
        (Set.range fun x =>
          ∫ (a : Fin m → Ω),
            (fun ω => f' fun i => if h : ↑i < ↑k then Xk ⟨↑i, h⟩ else if ↑i = ↑k then x else X' (ω i))
              a ∂Measure.pi fun x => μ)
hYB : ∀ (k : Fin m) (Xk : Fin ↑k → 𝓧) (x : 𝓧), B k Xk ≥ Y k.succ (Fin.snoc Xk x)
hAB : ∀ (k : Fin m) (Xk : Fin ↑k → 𝓧), B k Xk ≤ A k Xk + c' k
hmartigale :
  ∀ (k : Fin m) (Xk : Fin ↑k → 𝓧),
    (∫ (ω : Fin m → Ω), Y k.succ (Fin.snoc Xk (X' (ω k))) ∂Measure.pi fun x => μ) = Y k.castSucc Xk
hmeasurableY : ∀ (k : Fin m.succ), Measurable (Y k)
hYbdd : ∀ (k : Fin m.succ) (Xk : Fin ↑k → 𝓧), |Y k Xk| ≤ bdf
V : (k : Fin m) → (Fin ↑k → 𝓧) → (Fin m → Ω) → ℝ := fun k Xk ω => Y k.succ (Fin.snoc Xk (X' (ω k))) - Y k.castSucc Xk
t'' : ℝ := 4 * ε * t
ht'' : t'' ≥ 0
hhoefdding :
  ∀ (k : Fin m) (Xk : Fin ↑k → 𝓧),
    (∫ (ω : Fin m → Ω), Real.exp (t'' * V k Xk ω) ∂Measure.pi fun x => μ) ≤ Real.exp (t'' ^ 2 * c' k ^ 2 / 8)
E : ℝ := Y 0 fun x => x₀
hintegrablelefts :
  ∀ (k : ℕ) (h : k ≤ m),
    Integrable (fun x => Real.exp (t'' * ((Y ⟨k, ⋯⟩ fun i => X' (x (Fin.castLE h i))) - E))) (Measure.pi fun x => μ)
k : ℕ
ih✝ :
  ∀ (h : k ≤ m),
    (∫ (ω : Fin m → Ω), Real.exp (t'' * ((Y ⟨k, ⋯⟩ fun i => X' (ω (Fin.castLE h i))) - E)) ∂Measure.pi fun x => μ) ≤
      Real.exp ((t'' ^ 2 * ∑ i, c' (Fin.castLE h i) ^ 2) / 8)
h : k + 1 ≤ m
ih :
  (∫ (ω : Fin m → Ω), Real.exp (t'' * ((Y ⟨k, ⋯⟩ fun i => X' (ω (Fin.castLE ⋯ i))) - E)) ∂Measure.pi fun x => μ) ≤
    Real.exp ((t'' ^ 2 * ∑ i, c' (Fin.castLE ⋯ i) ^ 2) / 8)
hintegrableleft :
  Integrable (fun x => Real.exp (t'' * ((Y ⟨k, ⋯⟩ fun i => X' (x (Fin.castLE ⋯ i))) - E))) (Measure.pi fun x => μ)
⊢ (∫ (ω : Fin m → Ω),
      Real.exp (t'' * ((Y ⟨k, ⋯⟩ fun i => X' (ω (Fin.castLE h ↑↑i))) - E)) *
        ∫ (ω' : Fin m → Ω),
          Real.exp
            (t'' * V ⟨k, h⟩ (fun i => X' (ω (Fin.castLE h ↑↑i))) ω') ∂Measure.pi fun x => μ ∂Measure.pi fun x => μ) ≤
    ∫ (ω : Fin m → Ω),
      Real.exp (t'' * ((Y ⟨k, ⋯⟩ fun i => X' (ω (Fin.castLE h ↑↑i))) - E)) *
        Real.exp (t'' ^ 2 * c' ⟨k, h⟩ ^ 2 / 8) ∂Measure.pi fun x => μ

Tactic:
simp only [Nat.succ_eq_add_one, V] at hintegrableleft
    apply integral_mono
    ·
  have :
    (fun (ω : Fin m → Ω) ↦
        (t'' * (Y ⟨k, Nat.lt_succ_of_lt h⟩ (fun (i : Fin k) ↦ X' (ω ((Fin.castLE h i)))) - E)).exp *
          ∫ (ω' : Fin m → Ω), (t'' * (V ⟨k, h⟩ fun (i : Fin k) ↦ X' (ω ((Fin.castLE h i)))) ω').exp ∂μⁿ) =
      (fun (ω : Fin m → Ω) ↦
        (∫ (ω' : Fin m → Ω), (t'' * (V ⟨k, h⟩ fun (i : Fin k) ↦ X' (ω ((Fin.castLE h i)))) ω').exp ∂μⁿ) *
          (t'' * (Y ⟨k, Nat.lt_succ_of_lt h⟩ (fun (i : Fin k) ↦ X' (ω ((Fin.castLE h i)))) - E)).exp) :=
    by
    ext ω
    rw [mul_comm]
  rw [this]
  apply Integrable.bdd_mul
  · simp only [Nat.succ_eq_add_one, Fin.coe_eq_castSucc, Fin.castLE_castSucc, V]
    exact hintegrableleft
  · apply StronglyMeasurable.aestronglyMeasurable
    apply StronglyMeasurable.integral_prod_left
    apply StronglyMeasurable.comp_measurable Real.measurable_exp.stronglyMeasurable
    apply Measurable.comp
    · exact measurable_const_mul t''
    · apply Measurable.sub
      · apply (hmeasurableY (⟨k, h⟩ : Fin m).succ).comp
        apply measurable_pi_lambda
        intro i
        if h' : i.1 < k then
          
          have :
            (fun c : (Fin m → Ω) × (Fin m → Ω) ↦
                @Fin.snoc k (fun _ ↦ 𝓧) (fun i : Fin k ↦ X' (c.2 ((Fin.castLE h i)))) (X' (c.1 ⟨k, h⟩)) i) =
              fun c ↦ X' (c.2 (Fin.castLE h i)) :=
            by
            ext c
            dsimp only [Fin.snoc]
            rw [dif_pos h']
            simp
          rw [this]
          apply (hX''' _).comp measurable_snd
        else
          have :
            (fun c : (Fin m → Ω) × (Fin m → Ω) ↦
                @Fin.snoc k (fun _ ↦ 𝓧) (fun i : Fin k ↦ X' (c.2 ((Fin.castLE h i)))) (X' (c.1 ⟨k, h⟩)) i) =
              fun c ↦ X' (c.1 ⟨k, h⟩) :=
            by
            ext c
            dsimp only [Fin.snoc]
            rw [dif_neg h']
            simp
          rw [this]
          apply (hX''' _).comp measurable_fst
      · have :
          (fun a : (Fin m → Ω) × (Fin m → Ω) ↦ Y (⟨k, h⟩ : Fin m).castSucc fun i ↦ X' (a.2 (Fin.castLE h i))) =
            (Y (⟨k, h⟩ : Fin m).castSucc) ∘ (fun a : Fin m → Ω ↦ fun i ↦ X' (a (Fin.castLE h i))) ∘ Prod.snd :=
          rfl
        rw [this]
        apply (hmeasurableY (⟨k, h⟩ : Fin m).castSucc).comp
        apply Measurable.comp _ measurable_snd
        apply measurable_pi_lambda
        intro i
        apply hX'''
  · use (t'' * (bdf + bdf)).exp
    intro ω
    apply abs_expectation_le_of_abs_le_const
    filter_upwards with ω'
    rw [Real.abs_exp]
    apply Real.exp_le_exp.mpr
    apply mul_le_mul_of_nonneg_left _ ht''
    dsimp only [V]
    apply (le_abs.mpr (Or.inl le_rfl)).trans
    apply (abs_sub _ _).trans
    apply add_le_add
    · apply hYbdd
    · apply hYbdd (⟨k, h⟩ : Fin m).castSucc fun i ↦ X' (ω ((Fin.castLE h i)))
    · apply Integrable.mul_const (by simp; exact hintegrableleft) (Real.exp (t'' ^ 2 * c' ⟨k, h⟩ ^ 2 / 8))
    ·
  intro ω
  apply mul_le_mul_of_nonneg_left
  · apply hhoefdding
  · apply Real.exp_nonneg

Tactic state after: