import Mathlib
import FoML


open MeasureTheory ProbabilityTheory Real

open scoped ENNReal

theorem extracted_formal_statement_12.{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 → Ω) (hp : ∀ (i : ι) (j : Fin n), Integrable (fun a => f i (X (a j))) (Measure.pi fun x => μ))
  (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))))
  (hq0 : ∀ (ω x : Fin n → Ω) (k : Fin n), BddAbove (Set.range fun i => f i (X (ω k)) - f i (X (x k))))
  (hq1_1 : ∀ (ω x : Fin n → Ω), BddAbove (Set.range fun i => ∑ k, (f i (X (ω k)) - f i (X (x k)))))
  (hq2 : ∀ (ω x : Fin n → Ω) (k : Fin n), BddBelow (Set.range fun i => f i (X (ω k)) - f i (X (x k))))
  (hq3 : ∀ (ω x : Fin n → Ω), BddBelow (Set.range fun i => ∑ k, (f i (X (ω k)) - f i (X (x k)))))
  (hq1 : ∀ (ω x : Fin n → Ω), BddAbove (Set.range fun i => |∑ k, (f i (X (ω k)) - f i (X (x k)))|)) (C : ℝ)
  (hr0 : ∀ (ω : Fin n → Ω) (k : Fin n), ∀ y ∈ Set.range fun i => f i (X (ω k)), y ≤ C) (D : ℝ)
  (hr1 : ∀ (ω : Fin n → Ω) (k : Fin n), ∀ y ∈ Set.range fun i => f i (X (ω k)), D ≤ y)
  (h_original_goal :
    BddAbove (Set.range fun i => ∫ (x : Fin n → Ω), |∑ k, (f i (X (ω k)) - f i (X (x k)))| ∂Measure.pi fun x => μ)) :
  BddAbove (Set.range fun a => ↑n * (C - D)) := sorry

Tactic state before:
case intro.intro
Ω : 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 : ℝ
hf' : ∀ (i : ι) (z : Z), |f i z| ≤ b
ω : Fin n → Ω
hp : ∀ (i : ι) (j : Fin n), Integrable (fun a => f i (X (a j))) (Measure.pi fun x => μ)
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)))
hq0 : ∀ (ω x : Fin n → Ω) (k : Fin n), BddAbove (Set.range fun i => f i (X (ω k)) - f i (X (x k)))
hq1✝ : ∀ (ω x : Fin n → Ω), BddAbove (Set.range fun i => ∑ k, (f i (X (ω k)) - f i (X (x k))))
hq2 : ∀ (ω x : Fin n → Ω) (k : Fin n), BddBelow (Set.range fun i => f i (X (ω k)) - f i (X (x k)))
hq3 : ∀ (ω x : Fin n → Ω), BddBelow (Set.range fun i => ∑ k, (f i (X (ω k)) - f i (X (x k))))
hq1 : ∀ (ω x : Fin n → Ω), BddAbove (Set.range fun i => |∑ k, (f i (X (ω k)) - f i (X (x k)))|)
C : ℝ
hr0 : ∀ (ω : Fin n → Ω) (k : Fin n), ∀ y ∈ Set.range fun i => f i (X (ω k)), y ≤ C
D : ℝ
hr1 : ∀ (ω : Fin n → Ω) (k : Fin n), ∀ y ∈ Set.range fun i => f i (X (ω k)), D ≤ y
⊢ BddAbove (Set.range fun i => ∫ (x : Fin n → Ω), |∑ k, (f i (X (ω k)) - f i (X (x k)))| ∂Measure.pi fun x => μ)

Tactic:
apply BddAbove.range_mono
    intro a
    suffices (∫ (x : Fin n → Ω), |∑ k : Fin n, (f a (X (ω k)) - f a (X (x k)))| ∂μⁿ) ≤ n * (C - D) from by exact this
    have hr2 : ∀ (x : Fin n → Ω), |∑ k : Fin n, (f a (X (ω k)) - f a (X (x k)))| ≤ ↑n * (C - D) :=
  by
  intro x
  calc
    _ ≤ ∑ k : Fin n, |(f a (X (ω k)) - f a (X (x k)))| := by
      exact Finset.abs_sum_le_sum_abs (fun i ↦ f a (X (ω i)) - f a (X (x i))) Finset.univ
    _ ≤ ∑ k : Fin n, (C - D) :=
      by
      have w0 : ∀ (k : Fin n), |f a (X (ω k)) - f a (X (x k))| ≤ (C - D) :=
        by
        intro k
        refine abs_sub_le_of_le_of_le ?hal ?hau ?hbl ?hbu
        repeat
          apply hr1
          simp only [Set.mem_range]
          use a
          apply hr0
          simp only [Set.mem_range]
          use a
      exact Finset.sum_le_sum fun i a ↦ w0 i
    _ = ↑n * (C - D) := by simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul]
    have u : (∫ (x : Fin n → Ω), |∑ k : Fin n, (f a (X (ω k)) - f a (X (x k)))| ∂μⁿ) ≤ ↑n * (C - D) := by
  calc
    _ ≤ (∫ (x : Fin n → Ω), n * (C - D) ∂μⁿ) := by
      apply integral_mono
      · apply Integrable.abs
        apply Integrable.of_mem_Icc
        · refine Finset.aemeasurable_sum Finset.univ ?_
          intro i hi
          refine AEMeasurable.sub ?_ ?_
          exact aemeasurable_const
          exact Integrable.aemeasurable (hp a i)
        · filter_upwards
          intro a_1
          constructor
          have g : ∑ k : Fin n, (-b - b) ≤ ∑ k : Fin n, (f a (X (ω k)) - f a (X (a_1 k))) :=
            by
            refine Finset.sum_le_sum ?_
            intro i hi
            have q : -b ≤ f a (X (ω i)) := neg_le_of_abs_le (hf' a (X (ω i)))
            have q' : f a (X (a_1 i)) ≤ b := le_of_max_le_left (hf' a (X (a_1 i)))
            linarith
          exact g
          have g' : ∑ k : Fin n, (f a (X (ω k)) - f a (X (a_1 k))) ≤ ∑ k : Fin n, (b + b) :=
            by
            apply Finset.sum_le_sum
            intro i hi
            have q : f a (X (ω i)) ≤ b := le_of_max_le_left (hf' a (X (ω i)))
            have q' : -b ≤ f a (X (a_1 i)) := neg_le_of_abs_le (hf' a (X (a_1 i)))
            linarith
          exact g'
      apply integrable_const
      exact hr2
    _ = n * (C - D) := by simp

Tactic state after:
[case intro.intro.h
 Ω : 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 : ℝ
 hf' : ∀ (i : ι) (z : Z), |f i z| ≤ b
 ω : Fin n → Ω
 hp : ∀ (i : ι) (j : Fin n), Integrable (fun a => f i (X (a j))) (Measure.pi fun x => μ)
 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)))
 hq0 : ∀ (ω x : Fin n → Ω) (k : Fin n), BddAbove (Set.range fun i => f i (X (ω k)) - f i (X (x k)))
 hq1✝ : ∀ (ω x : Fin n → Ω), BddAbove (Set.range fun i => ∑ k, (f i (X (ω k)) - f i (X (x k))))
 hq2 : ∀ (ω x : Fin n → Ω) (k : Fin n), BddBelow (Set.range fun i => f i (X (ω k)) - f i (X (x k)))
 hq3 : ∀ (ω x : Fin n → Ω), BddBelow (Set.range fun i => ∑ k, (f i (X (ω k)) - f i (X (x k))))
 hq1 : ∀ (ω x : Fin n → Ω), BddAbove (Set.range fun i => |∑ k, (f i (X (ω k)) - f i (X (x k)))|)
 C : ℝ
 hr0 : ∀ (ω : Fin n → Ω) (k : Fin n), ∀ y ∈ Set.range fun i => f i (X (ω k)), y ≤ C
 D : ℝ
 hr1 : ∀ (ω : Fin n → Ω) (k : Fin n), ∀ y ∈ Set.range fun i => f i (X (ω k)), D ≤ y
 a : ι
 hr2 : ∀ (x : Fin n → Ω), |∑ k, (f a (X (ω k)) - f a (X (x k)))| ≤ ↑n * (C - D)
 u : (∫ (x : Fin n → Ω), |∑ k, (f a (X (ω k)) - f a (X (x k)))| ∂Measure.pi fun x => μ) ≤ ↑n * (C - D)
 ⊢ (∫ (x : Fin n → Ω), |∑ k, (f a (X (ω k)) - f a (X (x k)))| ∂Measure.pi fun x => μ) ≤ ↑n * (C - D),
 case intro.intro.hbdd
 Ω : 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 : ℝ
 hf' : ∀ (i : ι) (z : Z), |f i z| ≤ b
 ω : Fin n → Ω
 hp : ∀ (i : ι) (j : Fin n), Integrable (fun a => f i (X (a j))) (Measure.pi fun x => μ)
 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)))
 hq0 : ∀ (ω x : Fin n → Ω) (k : Fin n), BddAbove (Set.range fun i => f i (X (ω k)) - f i (X (x k)))
 hq1✝ : ∀ (ω x : Fin n → Ω), BddAbove (Set.range fun i => ∑ k, (f i (X (ω k)) - f i (X (x k))))
 hq2 : ∀ (ω x : Fin n → Ω) (k : Fin n), BddBelow (Set.range fun i => f i (X (ω k)) - f i (X (x k)))
 hq3 : ∀ (ω x : Fin n → Ω), BddBelow (Set.range fun i => ∑ k, (f i (X (ω k)) - f i (X (x k))))
 hq1 : ∀ (ω x : Fin n → Ω), BddAbove (Set.range fun i => |∑ k, (f i (X (ω k)) - f i (X (x k)))|)
 C : ℝ
 hr0 : ∀ (ω : Fin n → Ω) (k : Fin n), ∀ y ∈ Set.range fun i => f i (X (ω k)), y ≤ C
 D : ℝ
 hr1 : ∀ (ω : Fin n → Ω) (k : Fin n), ∀ y ∈ Set.range fun i => f i (X (ω k)), D ≤ y
 ⊢ BddAbove (Set.range fun a => ↑n * (C - D))]