import Mathlib
import FoML


open MeasureTheory ProbabilityTheory Real

open scoped ENNReal

theorem extracted_formal_statement_7.{u_1, w, u} {Ω : Type u} [inst : MeasurableSpace Ω] {Z : Type w} {n : ℕ}
  {ι : Type u_1} {f : ι → Z → ℝ} {μ : Measure Ω} [inst_1 : IsProbabilityMeasure μ] {X : Ω → Z} [inst_2 : Nonempty ι]
  [inst_3 : Countable ι] (hf : ∀ (i : ι), Measurable (f i ∘ X)) {b : ℝ} (hf' : ∀ (i : ι) (z : Z), |f i z| ≤ b)
  (ω' : Fin n → Ω) (i : ι) :
  |(|∫ (x : Fin n → Ω), ∑ k, (f i (X (ω' k)) - f i (X (x k))) ∂Measure.pi fun x => μ|)| ≤ ↑n * (b + b) := sorry

Tactic state before:
case hf.right.h.hf
Ω : Type u
inst✝¹ : MeasurableSpace Ω
Z : Type w
n : ℕ
ι : Type u_1
f : ι → Z → ℝ
μ : Measure Ω
inst✝ : IsProbabilityMeasure μ
X : Ω → Z
inst_2 : Nonempty ι
inst_3 : Countable ι
hf : ∀ (i : ι), Measurable (f i ∘ X)
b : ℝ
hf' : ∀ (i : ι) (z : Z), |f i z| ≤ b
ω' : Fin n → Ω
i : ι
⊢ |(|∫ (x : Fin n → Ω), ∑ k, (f i (X (ω' k)) - f i (X (x k))) ∂Measure.pi fun x => μ|)| ≤ ↑n * (b + b)

Tactic:
simp only [Finset.sum_sub_distrib, abs_abs]
    rw [abs_le]
    constructor
    ·
  suffices ∀ (x : Fin n → Ω), (-(↑n * (b + b)) ≤ ∑ x : Fin n, f i (X (ω' x)) - ∑ x_1 : Fin n, f i (X (x x_1))) from by
    calc
      _ = ∫ (x : Fin n → Ω), -(↑n * (b + b)) ∂μⁿ := by simp
      _ ≤ ∫ (x : Fin n → Ω), ∑ x : Fin n, f i (X (ω' x)) - ∑ x_1 : Fin n, f i (X (x x_1)) ∂μⁿ :=
        by
        apply MeasureTheory.integral_mono
        · simp
        · constructor
          · refine AEStronglyMeasurable.const_add ?hg.left.hf (∑ x : Fin n, f i (X (ω' x)))
            refine aestronglyMeasurable_iff_aemeasurable.mpr ?hg.left.hf.a
            refine AEMeasurable.neg ?hg.left.hf.a.hf
            have w : ∀ (x_1 : Fin n), (AEMeasurable (fun a ↦ f i (X (a x_1))) μⁿ) :=
              by
              intro x_1
              exact Integrable.aemeasurable (integrable_aux0 hf hf' i x_1)
            apply Measurable.aemeasurable
            apply measurable_pi_sum (hf i)
          · apply @MeasureTheory.hasFiniteIntegral_of_bounded _ _ _ _ _ _ _ (n * (b + b))
            filter_upwards
            intro a
            simp only [norm_eq_abs]
            calc
              _ ≤ |∑ x : Fin n, f i (X (ω' x))| + |∑ x_1 : Fin n, f i (X (a x_1))| := by
                exact abs_sub (∑ x : Fin n, f i (X (ω' x))) (∑ x_1 : Fin n, f i (X (a x_1)))
              _ ≤ (↑n * b) + (↑n * b) :=
                by
                have w : |∑ x : Fin n, f i (X (ω' x))| ≤ ↑n * b := by
                  calc
                    _ ≤ ∑ x : Fin n, |f i (X (ω' x))| := by
                      exact Finset.abs_sum_le_sum_abs (fun i_1 ↦ f i (X (ω' i_1))) Finset.univ
                    _ ≤ ∑ x : Fin n, b := by exact Finset.sum_le_sum fun i_1 a ↦ hf' i (X (ω' i_1))
                    _ = ↑n * b := by simp
                have w' : |∑ x_1 : Fin n, f i (X (a x_1))| ≤ ↑n * b := by
                  calc
                    _ ≤ ∑ x_1 : Fin n, |f i (X (a x_1))| := by
                      exact Finset.abs_sum_le_sum_abs (fun i_1 ↦ f i (X (a i_1))) Finset.univ
                    _ ≤ ∑ x_1 : Fin n, b := by exact Finset.sum_le_sum fun i_1 s ↦ hf' i (X (a i_1))
                    _ = ↑n * b := by simp
                exact add_le_add w w'
              _ = ↑n * (b + b) := by ring
        · exact this
  intro x
  have t : -↑n * b ≤ ∑ x : Fin n, f i (X (ω' x)) := by
    calc
      _ = ∑ x : Fin n, (-b) := by simp
      _ ≤ ∑ x : Fin n, f i (X (ω' x)) :=
        by
        have u : ∀ (x : Fin n), -b ≤ f i (X (ω' x)) := by
          intro x
          exact neg_le_of_abs_le (hf' i (X (ω' x)))
        exact Finset.sum_le_sum fun i a ↦ u i
  have t' : -↑n * b ≤ -∑ x_1 : Fin n, f i (X (x x_1)) :=
    by
    simp only [neg_mul, neg_le_neg_iff]
    calc
      _ ≤ ∑ x_1 : Fin n, b := by
        apply Finset.sum_le_sum
        exact fun i_1 a ↦ le_of_max_le_left (hf' i (X (x i_1)))
      _ = ↑n * b := by simp
  linarith
    ·
  have p :
    (∫ (x : Fin n → Ω), ∑ x : Fin n, f i (X (ω' x)) - ∑ x_1 : Fin n, f i (X (x x_1)) ∂μⁿ) ≤
      (∫ (x : Fin n → Ω), ↑n * (b + b) ∂μⁿ) :=
    by
    to_theorem
      apply
        integral_mono
          apply
        Integrable.sub
          constructor exact aestronglyMeasurable_const exact hasFiniteIntegral_const (∑ x : Fin n, f i (X (ω' x)))
    constructor
    · apply Measurable.aestronglyMeasurable
      apply measurable_pi_sum (hf i)
    · apply HasFiniteIntegral.of_mem_Icc
      filter_upwards
      intro a
      constructor
      · have p : (∑ x_1 : Fin n, (-b)) ≤ ∑ x_1 : Fin n, f i (X (a x_1)) :=
          by
          refine Finset.sum_le_sum ?_
          intro i_1 hi_1
          exact neg_le_of_abs_le (hf' i (X (a i_1)))
        exact p
      · have p : ∑ x_1 : Fin n, f i (X (a x_1)) ≤ (∑ x_1 : Fin n, b) :=
          by
          refine Finset.sum_le_sum ?_
          intro i_1 hi_1
          exact le_of_max_le_left (hf' i (X (a i_1)))
        exact p
    to_theorem
      apply
        integrable_const
          refine
        Pi.le_def.mpr
          ?_
            intro
        i_1
          have q : ∑ x : Fin n, f i (X (ω' x)) ≤ ∑ x : Fin n, b :=
        by
        refine Finset.sum_le_sum ?_
        exact fun i_2 a ↦
          le_of_max_le_left
            (hf' i
              (X
                (ω'
                  i_2)))have q' : ∑ x : Fin n, -b ≤ ∑ x_1 : Fin n, f i (X (i_1 x_1)) :=
        by
        refine Finset.sum_le_sum ?_
        exact fun i_2 a ↦ neg_le_of_abs_le (hf' i (X (i_1 i_2)))
    simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul] at q
    simp only [Finset.sum_neg_distrib, Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul] at q'
    linarith
  have p' : (∫ (x : Fin n → Ω), ↑n * (b + b) ∂μⁿ) = ↑n * (b + b) := by simp
  rw [<- p']
  exact p

Tactic state after: