import Mathlib
import FoML


open MeasureTheory ProbabilityTheory Real

open scoped ENNReal

theorem extracted_formal_statement_16.{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 : ℝ} (hb : b ≥ 0)
  (hf' : ∀ (i : ι) (z : Z), |f i z| ≤ b) (hn : ¬n = 0)
  (hr : ∃ C, ∀ (ω : Fin n → Ω) (k : Fin n), ∀ y ∈ Set.range fun i => f i (X (ω k)), y ≤ C)
  (hr' : ∃ C, ∀ (ω : Fin n → Ω) (k : Fin n), ∀ y ∈ Set.range fun i => f i (X (ω k)), C ≤ y)
  (hq : ∀ (ω : Fin n → Ω) (k : Fin n), BddAbove (Set.range fun i => f i (X (ω k))))
  (hq' : ∀ (ω : Fin n → Ω) (k : Fin n), BddBelow (Set.range fun i => f i (X (ω k))))
  (h_original_goal :
    (∫ (x : Fin n → Ω),
        ∫ (x_1 : Fin n → Ω),
          ⨆ i, |∑ k, (f i (X (x k)) - f i (X (x_1 k)))| ∂Measure.pi fun x => μ ∂Measure.pi fun x => μ) =
      ∫ (x : Fin n → Ω × Ω), ⨆ i, |∑ k, (f i (X (x k).1) - f i (X (x k).2))| ∂Measure.pi fun x => μ.prod μ) :
  Integrable (fun ω => ⨆ i, |∑ k, (f i (X (ω.1 k)) - f i (X (ω.2 k)))|)
    ((Measure.pi fun x => μ).prod (Measure.pi fun x => μ)) := sorry

Tactic state before:
Ω : Type u
inst✝³ : MeasurableSpace Ω
Z : Type w
n : ℕ
ι : Type u_1
f : ι → Z → ℝ
μ : Measure Ω
inst✝² : IsProbabilityMeasure μ
X : Ω → Z
inst✝¹ : Nonempty ι
inst✝ : Countable ι
hf : ∀ (i : ι), Measurable (f i ∘ X)
b : ℝ
hb : b ≥ 0
hf' : ∀ (i : ι) (z : Z), |f i z| ≤ b
hn : ¬n = 0
hr : ∃ C, ∀ (ω : Fin n → Ω) (k : Fin n), ∀ y ∈ Set.range fun i => f i (X (ω k)), y ≤ C
hr' : ∃ C, ∀ (ω : Fin n → Ω) (k : Fin n), ∀ y ∈ Set.range fun i => f i (X (ω k)), C ≤ y
hq : ∀ (ω : Fin n → Ω) (k : Fin n), BddAbove (Set.range fun i => f i (X (ω k)))
hq' : ∀ (ω : Fin n → Ω) (k : Fin n), BddBelow (Set.range fun i => f i (X (ω k)))
⊢ (∫ (x : Fin n → Ω),
      ∫ (x_1 : Fin n → Ω),
        ⨆ i, |∑ k, (f i (X (x k)) - f i (X (x_1 k)))| ∂Measure.pi fun x => μ ∂Measure.pi fun x => μ) =
    ∫ (x : Fin n → Ω × Ω), ⨆ i, |∑ k, (f i (X (x k).1) - f i (X (x k).2))| ∂Measure.pi fun x => μ.prod μ

Tactic:
rw [←
  integral_prod fun ω : (Fin n → Ω) × (Fin n → Ω) ↦ ⨆ i, |∑ k : Fin n, (f i (X (ω.1 k)) - f i (X (ω.2 k)))|]
    let mp := measurePreserving_arrowProdEquivProdArrow Ω Ω (Fin n) (fun _ ↦ μ) (fun _ ↦ μ)
    rw [← mp.map_eq, integral_map mp.measurable.aemeasurable]
    · rfl
    ·
  apply Measurable.aestronglyMeasurable
  to_theorem
    apply Measurable.iSup intro i apply Measurable.max refine Finset.measurable_sum Finset.univ ?_ intro i_1 hi_1
  to_theorem apply Measurable.sub apply (hf i).comp refine Measurable.eval ?_ exact measurable_fst apply (hf i).comp
  refine Measurable.eval ?_
  to_theorem
    exact
      measurable_snd
        apply Measurable.neg refine Finset.measurable_sum Finset.univ ?_ intro i_1 hi_1 apply Measurable.sub
  to_theorem
    apply (hf i).comp refine Measurable.eval ?_ exact measurable_fst apply (hf i).comp refine Measurable.eval ?_
  exact measurable_snd

Tactic state after:
[Ω : Type u
 inst✝³ : MeasurableSpace Ω
 Z : Type w
 n : ℕ
 ι : Type u_1
 f : ι → Z → ℝ
 μ : Measure Ω
 inst✝² : IsProbabilityMeasure μ
 X : Ω → Z
 inst✝¹ : Nonempty ι
 inst✝ : Countable ι
 hf : ∀ (i : ι), Measurable (f i ∘ X)
 b : ℝ
 hb : b ≥ 0
 hf' : ∀ (i : ι) (z : Z), |f i z| ≤ b
 hn : ¬n = 0
 hr : ∃ C, ∀ (ω : Fin n → Ω) (k : Fin n), ∀ y ∈ Set.range fun i => f i (X (ω k)), y ≤ C
 hr' : ∃ C, ∀ (ω : Fin n → Ω) (k : Fin n), ∀ y ∈ Set.range fun i => f i (X (ω k)), C ≤ y
 hq : ∀ (ω : Fin n → Ω) (k : Fin n), BddAbove (Set.range fun i => f i (X (ω k)))
 hq' : ∀ (ω : Fin n → Ω) (k : Fin n), BddBelow (Set.range fun i => f i (X (ω k)))
 ⊢ Integrable (fun ω => ⨆ i, |∑ k, (f i (X (ω.1 k)) - f i (X (ω.2 k)))|)
     ((Measure.pi fun x => μ).prod (Measure.pi fun x => μ))]