l3lab/ntp-mathlib huggingface dataset:
@[measurability]
theorem sum_measure [Countable ι] {μ : ι → Measure α} (h : ∀ i, AEMeasurable f (μ i)) :
    AEMeasurable f (sum μ) := by
  nontriviality β
  inhabit β
  set s : ι → Set α := fun i => toMeasurable (μ i) { x | f x ≠ (h i).mk f x }
  have hsμ : ∀ i, μ i (s i) = 0 := by
    intro i
    rw [measure_toMeasurable]
    exact (h i).ae_eq_mk
  have hsm : MeasurableSet (⋂ i, s i) :=
    MeasurableSet.iInter fun i => measurableSet_toMeasurable _ _
  have hs : ∀ i x, x ∉ s i → f x = (h i).mk f x := by
    intro i x hx
    contrapose! hx
    exact subset_toMeasurable _ _ hx
  set g : α → β := (⋂ i, s i).piecewise (const α default) f
  refine' ⟨g, measurable_of_restrict_of_restrict_compl hsm _ _, ae_sum_iff.mpr fun i => _⟩
  · rw [restrict_piecewise]
    simp only [Set.restrict, const]
    exact measurable_const
  · rw [restrict_piecewise_compl, compl_iInter]
    intro t ht
    refine ⟨⋃ i, (h i).mk f ⁻¹' t ∩ (s i)ᶜ, MeasurableSet.iUnion fun i ↦
      (measurable_mk _ ht).inter (measurableSet_toMeasurable _ _).compl, ?_⟩
    

Proof state:
case refine'_2
ι : Type u_1
α : Type u_2
β : Type u_3
γ : Type u_4
δ : Type u_5
R : Type u_6
m0 : MeasurableSpace α
inst✝³ : MeasurableSpace β
inst✝² : MeasurableSpace γ
inst✝¹ : MeasurableSpace δ
f g✝ : α → β
μ✝ ν : Measure α
inst✝ : Countable ι
μ : ι → Measure α
h : ∀ (i : ι), AEMeasurable f
✝ : Nontrivial β
inhabited_h : Inhabited β
s : ι → Set α := fun i => toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}
hsμ : ∀ (i : ι), ↑↑(μ i) (s i) = 0
hsm : MeasurableSet (⋂ i, s i)
hs : ∀ (i : ι), ∀ x ∉ s i, f x = mk f (_ : AEMeasurable f) x
g : α → β := piecewise (⋂ i, s i) (const α default) f
t : Set β
ht : MeasurableSet t
⊢ Subtype.val ⁻¹' ⋃ i, mk f (_ : AEMeasurable f) ⁻¹' t ∩ (s i)ᶜ = Set.restrict (⋃ i, (s i)ᶜ) f ⁻¹' t

Next tactic:
ext ⟨x, hx⟩
@[measurability]
theorem sum_measure [Countable ι] {μ : ι → Measure α} (h : ∀ i, AEMeasurable f (μ i)) :
    AEMeasurable f (sum μ) := by
  nontriviality β
  inhabit β
  set s : ι → Set α := fun i => toMeasurable (μ i) { x | f x ≠ (h i).mk f x }
  have hsμ : ∀ i, μ i (s i) = 0 := by
    intro i
    rw [measure_toMeasurable]
    exact (h i).ae_eq_mk
  have hsm : MeasurableSet (⋂ i, s i) :=
    MeasurableSet.iInter fun i => measurableSet_toMeasurable _ _
  have hs : ∀ i x, x ∉ s i → f x = (h i).mk f x := by
    intro i x hx
    contrapose! hx
    exact subset_toMeasurable _ _ hx
  set g : α → β := (⋂ i, s i).piecewise (const α default) f
  refine' ⟨g, measurable_of_restrict_of_restrict_compl hsm _ _, ae_sum_iff.mpr fun i => _⟩
  · rw [restrict_piecewise]
    simp only [Set.restrict, const]
    exact measurable_const
  · rw [restrict_piecewise_compl, compl_iInter]
    intro t ht
    refine ⟨⋃ i, (h i).mk f ⁻¹' t ∩ (s i)ᶜ, MeasurableSet.iUnion fun i ↦
      (measurable_mk _ ht).inter (measurableSet_toMeasurable _ _).compl, ?_⟩
    ext ⟨x, hx⟩
    

Proof state:
case refine'_2.h.mk
ι : Type u_1
α : Type u_2
β : Type u_3
γ : Type u_4
δ : Type u_5
R : Type u_6
m0 : MeasurableSpace α
inst✝³ : MeasurableSpace β
inst✝² : MeasurableSpace γ
inst✝¹ : MeasurableSpace δ
f g✝ : α → β
μ✝ ν : Measure α
inst✝ : Countable ι
μ : ι → Measure α
h : ∀ (i : ι), AEMeasurable f
✝ : Nontrivial β
inhabited_h : Inhabited β
s : ι → Set α := fun i => toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}
hsμ : ∀ (i : ι), ↑↑(μ i) (s i) = 0
hsm : MeasurableSet (⋂ i, s i)
hs : ∀ (i : ι), ∀ x ∉ s i, f x = mk f (_ : AEMeasurable f) x
g : α → β := piecewise (⋂ i, s i) (const α default) f
t : Set β
ht : MeasurableSet t
x : α
hx : x ∈ ⋃ i, (s i)ᶜ
⊢ { val := x, property := hx } ∈ Subtype.val ⁻¹' ⋃ i, mk f (_ : AEMeasurable f) ⁻¹' t ∩ (s i)ᶜ ↔
    { val := x, property := hx } ∈ Set.restrict (⋃ i, (s i)ᶜ) f ⁻¹' t

Next tactic:
simp only [mem_preimage, mem_iUnion, Subtype.coe_mk, Set.restrict, mem_inter_iff,
      mem_compl_iff] at hx ⊢
@[measurability]
theorem sum_measure [Countable ι] {μ : ι → Measure α} (h : ∀ i, AEMeasurable f (μ i)) :
    AEMeasurable f (sum μ) := by
  nontriviality β
  inhabit β
  set s : ι → Set α := fun i => toMeasurable (μ i) { x | f x ≠ (h i).mk f x }
  have hsμ : ∀ i, μ i (s i) = 0 := by
    intro i
    rw [measure_toMeasurable]
    exact (h i).ae_eq_mk
  have hsm : MeasurableSet (⋂ i, s i) :=
    MeasurableSet.iInter fun i => measurableSet_toMeasurable _ _
  have hs : ∀ i x, x ∉ s i → f x = (h i).mk f x := by
    intro i x hx
    contrapose! hx
    exact subset_toMeasurable _ _ hx
  set g : α → β := (⋂ i, s i).piecewise (const α default) f
  refine' ⟨g, measurable_of_restrict_of_restrict_compl hsm _ _, ae_sum_iff.mpr fun i => _⟩
  · rw [restrict_piecewise]
    simp only [Set.restrict, const]
    exact measurable_const
  · rw [restrict_piecewise_compl, compl_iInter]
    intro t ht
    refine ⟨⋃ i, (h i).mk f ⁻¹' t ∩ (s i)ᶜ, MeasurableSet.iUnion fun i ↦
      (measurable_mk _ ht).inter (measurableSet_toMeasurable _ _).compl, ?_⟩
    ext ⟨x, hx⟩
    simp only [mem_preimage, mem_iUnion, Subtype.coe_mk, Set.restrict, mem_inter_iff,
      mem_compl_iff] at hx ⊢
    

Proof state:
case refine'_2.h.mk
ι : Type u_1
α : Type u_2
β : Type u_3
γ : Type u_4
δ : Type u_5
R : Type u_6
m0 : MeasurableSpace α
inst✝³ : MeasurableSpace β
inst✝² : MeasurableSpace γ
inst✝¹ : MeasurableSpace δ
f g✝ : α → β
μ✝ ν : Measure α
inst✝ : Countable ι
μ : ι → Measure α
h : ∀ (i : ι), AEMeasurable f
✝ : Nontrivial β
inhabited_h : Inhabited β
s : ι → Set α := fun i => toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}
hsμ : ∀ (i : ι), ↑↑(μ i) (s i) = 0
hsm : MeasurableSet (⋂ i, s i)
hs : ∀ (i : ι), ∀ x ∉ s i, f x = mk f (_ : AEMeasurable f) x
g : α → β := piecewise (⋂ i, s i) (const α default) f
t : Set β
ht : MeasurableSet t
x : α
hx : ∃ i, x ∉ toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}
⊢ (∃ i, mk f (_ : AEMeasurable f) x ∈ t ∧ x ∉ toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}) ↔ f x ∈ t

Next tactic:
constructor
@[measurability]
theorem sum_measure [Countable ι] {μ : ι → Measure α} (h : ∀ i, AEMeasurable f (μ i)) :
    AEMeasurable f (sum μ) := by
  nontriviality β
  inhabit β
  set s : ι → Set α := fun i => toMeasurable (μ i) { x | f x ≠ (h i).mk f x }
  have hsμ : ∀ i, μ i (s i) = 0 := by
    intro i
    rw [measure_toMeasurable]
    exact (h i).ae_eq_mk
  have hsm : MeasurableSet (⋂ i, s i) :=
    MeasurableSet.iInter fun i => measurableSet_toMeasurable _ _
  have hs : ∀ i x, x ∉ s i → f x = (h i).mk f x := by
    intro i x hx
    contrapose! hx
    exact subset_toMeasurable _ _ hx
  set g : α → β := (⋂ i, s i).piecewise (const α default) f
  refine' ⟨g, measurable_of_restrict_of_restrict_compl hsm _ _, ae_sum_iff.mpr fun i => _⟩
  · rw [restrict_piecewise]
    simp only [Set.restrict, const]
    exact measurable_const
  · rw [restrict_piecewise_compl, compl_iInter]
    intro t ht
    refine ⟨⋃ i, (h i).mk f ⁻¹' t ∩ (s i)ᶜ, MeasurableSet.iUnion fun i ↦
      (measurable_mk _ ht).inter (measurableSet_toMeasurable _ _).compl, ?_⟩
    ext ⟨x, hx⟩
    simp only [mem_preimage, mem_iUnion, Subtype.coe_mk, Set.restrict, mem_inter_iff,
      mem_compl_iff] at hx ⊢
    constructor
    · 

Proof state:
case refine'_2.h.mk.mp
ι : Type u_1
α : Type u_2
β : Type u_3
γ : Type u_4
δ : Type u_5
R : Type u_6
m0 : MeasurableSpace α
inst✝³ : MeasurableSpace β
inst✝² : MeasurableSpace γ
inst✝¹ : MeasurableSpace δ
f g✝ : α → β
μ✝ ν : Measure α
inst✝ : Countable ι
μ : ι → Measure α
h : ∀ (i : ι), AEMeasurable f
✝ : Nontrivial β
inhabited_h : Inhabited β
s : ι → Set α := fun i => toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}
hsμ : ∀ (i : ι), ↑↑(μ i) (s i) = 0
hsm : MeasurableSet (⋂ i, s i)
hs : ∀ (i : ι), ∀ x ∉ s i, f x = mk f (_ : AEMeasurable f) x
g : α → β := piecewise (⋂ i, s i) (const α default) f
t : Set β
ht : MeasurableSet t
x : α
hx : ∃ i, x ∉ toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}
⊢ (∃ i, mk f (_ : AEMeasurable f) x ∈ t ∧ x ∉ toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}) → f x ∈ t

Next tactic:
rintro ⟨i, hxt, hxs⟩
@[measurability]
theorem sum_measure [Countable ι] {μ : ι → Measure α} (h : ∀ i, AEMeasurable f (μ i)) :
    AEMeasurable f (sum μ) := by
  nontriviality β
  inhabit β
  set s : ι → Set α := fun i => toMeasurable (μ i) { x | f x ≠ (h i).mk f x }
  have hsμ : ∀ i, μ i (s i) = 0 := by
    intro i
    rw [measure_toMeasurable]
    exact (h i).ae_eq_mk
  have hsm : MeasurableSet (⋂ i, s i) :=
    MeasurableSet.iInter fun i => measurableSet_toMeasurable _ _
  have hs : ∀ i x, x ∉ s i → f x = (h i).mk f x := by
    intro i x hx
    contrapose! hx
    exact subset_toMeasurable _ _ hx
  set g : α → β := (⋂ i, s i).piecewise (const α default) f
  refine' ⟨g, measurable_of_restrict_of_restrict_compl hsm _ _, ae_sum_iff.mpr fun i => _⟩
  · rw [restrict_piecewise]
    simp only [Set.restrict, const]
    exact measurable_const
  · rw [restrict_piecewise_compl, compl_iInter]
    intro t ht
    refine ⟨⋃ i, (h i).mk f ⁻¹' t ∩ (s i)ᶜ, MeasurableSet.iUnion fun i ↦
      (measurable_mk _ ht).inter (measurableSet_toMeasurable _ _).compl, ?_⟩
    ext ⟨x, hx⟩
    simp only [mem_preimage, mem_iUnion, Subtype.coe_mk, Set.restrict, mem_inter_iff,
      mem_compl_iff] at hx ⊢
    constructor
    · rintro ⟨i, hxt, hxs⟩
      

Proof state:
case refine'_2.h.mk.mp.intro.intro
ι : Type u_1
α : Type u_2
β : Type u_3
γ : Type u_4
δ : Type u_5
R : Type u_6
m0 : MeasurableSpace α
inst✝³ : MeasurableSpace β
inst✝² : MeasurableSpace γ
inst✝¹ : MeasurableSpace δ
f g✝ : α → β
μ✝ ν : Measure α
inst✝ : Countable ι
μ : ι → Measure α
h : ∀ (i : ι), AEMeasurable f
✝ : Nontrivial β
inhabited_h : Inhabited β
s : ι → Set α := fun i => toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}
hsμ : ∀ (i : ι), ↑↑(μ i) (s i) = 0
hsm : MeasurableSet (⋂ i, s i)
hs : ∀ (i : ι), ∀ x ∉ s i, f x = mk f (_ : AEMeasurable f) x
g : α → β := piecewise (⋂ i, s i) (const α default) f
t : Set β
ht : MeasurableSet t
x : α
hx : ∃ i, x ∉ toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}
i : ι
hxt : mk f (_ : AEMeasurable f) x ∈ t
hxs : x ∉ toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}
⊢ f x ∈ t

Next tactic:
rwa [hs _ _ hxs]
@[measurability]
theorem sum_measure [Countable ι] {μ : ι → Measure α} (h : ∀ i, AEMeasurable f (μ i)) :
    AEMeasurable f (sum μ) := by
  nontriviality β
  inhabit β
  set s : ι → Set α := fun i => toMeasurable (μ i) { x | f x ≠ (h i).mk f x }
  have hsμ : ∀ i, μ i (s i) = 0 := by
    intro i
    rw [measure_toMeasurable]
    exact (h i).ae_eq_mk
  have hsm : MeasurableSet (⋂ i, s i) :=
    MeasurableSet.iInter fun i => measurableSet_toMeasurable _ _
  have hs : ∀ i x, x ∉ s i → f x = (h i).mk f x := by
    intro i x hx
    contrapose! hx
    exact subset_toMeasurable _ _ hx
  set g : α → β := (⋂ i, s i).piecewise (const α default) f
  refine' ⟨g, measurable_of_restrict_of_restrict_compl hsm _ _, ae_sum_iff.mpr fun i => _⟩
  · rw [restrict_piecewise]
    simp only [Set.restrict, const]
    exact measurable_const
  · rw [restrict_piecewise_compl, compl_iInter]
    intro t ht
    refine ⟨⋃ i, (h i).mk f ⁻¹' t ∩ (s i)ᶜ, MeasurableSet.iUnion fun i ↦
      (measurable_mk _ ht).inter (measurableSet_toMeasurable _ _).compl, ?_⟩
    ext ⟨x, hx⟩
    simp only [mem_preimage, mem_iUnion, Subtype.coe_mk, Set.restrict, mem_inter_iff,
      mem_compl_iff] at hx ⊢
    constructor
    · rintro ⟨i, hxt, hxs⟩
      rwa [hs _ _ hxs]
    · 

Proof state:
case refine'_2.h.mk.mpr
ι : Type u_1
α : Type u_2
β : Type u_3
γ : Type u_4
δ : Type u_5
R : Type u_6
m0 : MeasurableSpace α
inst✝³ : MeasurableSpace β
inst✝² : MeasurableSpace γ
inst✝¹ : MeasurableSpace δ
f g✝ : α → β
μ✝ ν : Measure α
inst✝ : Countable ι
μ : ι → Measure α
h : ∀ (i : ι), AEMeasurable f
✝ : Nontrivial β
inhabited_h : Inhabited β
s : ι → Set α := fun i => toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}
hsμ : ∀ (i : ι), ↑↑(μ i) (s i) = 0
hsm : MeasurableSet (⋂ i, s i)
hs : ∀ (i : ι), ∀ x ∉ s i, f x = mk f (_ : AEMeasurable f) x
g : α → β := piecewise (⋂ i, s i) (const α default) f
t : Set β
ht : MeasurableSet t
x : α
hx : ∃ i, x ∉ toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}
⊢ f x ∈ t → ∃ i, mk f (_ : AEMeasurable f) x ∈ t ∧ x ∉ toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}

Next tactic:
rcases hx with ⟨i, hi⟩
@[measurability]
theorem sum_measure [Countable ι] {μ : ι → Measure α} (h : ∀ i, AEMeasurable f (μ i)) :
    AEMeasurable f (sum μ) := by
  nontriviality β
  inhabit β
  set s : ι → Set α := fun i => toMeasurable (μ i) { x | f x ≠ (h i).mk f x }
  have hsμ : ∀ i, μ i (s i) = 0 := by
    intro i
    rw [measure_toMeasurable]
    exact (h i).ae_eq_mk
  have hsm : MeasurableSet (⋂ i, s i) :=
    MeasurableSet.iInter fun i => measurableSet_toMeasurable _ _
  have hs : ∀ i x, x ∉ s i → f x = (h i).mk f x := by
    intro i x hx
    contrapose! hx
    exact subset_toMeasurable _ _ hx
  set g : α → β := (⋂ i, s i).piecewise (const α default) f
  refine' ⟨g, measurable_of_restrict_of_restrict_compl hsm _ _, ae_sum_iff.mpr fun i => _⟩
  · rw [restrict_piecewise]
    simp only [Set.restrict, const]
    exact measurable_const
  · rw [restrict_piecewise_compl, compl_iInter]
    intro t ht
    refine ⟨⋃ i, (h i).mk f ⁻¹' t ∩ (s i)ᶜ, MeasurableSet.iUnion fun i ↦
      (measurable_mk _ ht).inter (measurableSet_toMeasurable _ _).compl, ?_⟩
    ext ⟨x, hx⟩
    simp only [mem_preimage, mem_iUnion, Subtype.coe_mk, Set.restrict, mem_inter_iff,
      mem_compl_iff] at hx ⊢
    constructor
    · rintro ⟨i, hxt, hxs⟩
      rwa [hs _ _ hxs]
    · rcases hx with ⟨i, hi⟩
      

Proof state:
case refine'_2.h.mk.mpr.intro
ι : Type u_1
α : Type u_2
β : Type u_3
γ : Type u_4
δ : Type u_5
R : Type u_6
m0 : MeasurableSpace α
inst✝³ : MeasurableSpace β
inst✝² : MeasurableSpace γ
inst✝¹ : MeasurableSpace δ
f g✝ : α → β
μ✝ ν : Measure α
inst✝ : Countable ι
μ : ι → Measure α
h : ∀ (i : ι), AEMeasurable f
✝ : Nontrivial β
inhabited_h : Inhabited β
s : ι → Set α := fun i => toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}
hsμ : ∀ (i : ι), ↑↑(μ i) (s i) = 0
hsm : MeasurableSet (⋂ i, s i)
hs : ∀ (i : ι), ∀ x ∉ s i, f x = mk f (_ : AEMeasurable f) x
g : α → β := piecewise (⋂ i, s i) (const α default) f
t : Set β
ht : MeasurableSet t
x : α
i : ι
hi : x ∉ toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}
⊢ f x ∈ t → ∃ i, mk f (_ : AEMeasurable f) x ∈ t ∧ x ∉ toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}

Next tactic:
rw [hs _ _ hi]
@[measurability]
theorem sum_measure [Countable ι] {μ : ι → Measure α} (h : ∀ i, AEMeasurable f (μ i)) :
    AEMeasurable f (sum μ) := by
  nontriviality β
  inhabit β
  set s : ι → Set α := fun i => toMeasurable (μ i) { x | f x ≠ (h i).mk f x }
  have hsμ : ∀ i, μ i (s i) = 0 := by
    intro i
    rw [measure_toMeasurable]
    exact (h i).ae_eq_mk
  have hsm : MeasurableSet (⋂ i, s i) :=
    MeasurableSet.iInter fun i => measurableSet_toMeasurable _ _
  have hs : ∀ i x, x ∉ s i → f x = (h i).mk f x := by
    intro i x hx
    contrapose! hx
    exact subset_toMeasurable _ _ hx
  set g : α → β := (⋂ i, s i).piecewise (const α default) f
  refine' ⟨g, measurable_of_restrict_of_restrict_compl hsm _ _, ae_sum_iff.mpr fun i => _⟩
  · rw [restrict_piecewise]
    simp only [Set.restrict, const]
    exact measurable_const
  · rw [restrict_piecewise_compl, compl_iInter]
    intro t ht
    refine ⟨⋃ i, (h i).mk f ⁻¹' t ∩ (s i)ᶜ, MeasurableSet.iUnion fun i ↦
      (measurable_mk _ ht).inter (measurableSet_toMeasurable _ _).compl, ?_⟩
    ext ⟨x, hx⟩
    simp only [mem_preimage, mem_iUnion, Subtype.coe_mk, Set.restrict, mem_inter_iff,
      mem_compl_iff] at hx ⊢
    constructor
    · rintro ⟨i, hxt, hxs⟩
      rwa [hs _ _ hxs]
    · rcases hx with ⟨i, hi⟩
      rw [hs _ _ hi]
      

Proof state:
case refine'_2.h.mk.mpr.intro
ι : Type u_1
α : Type u_2
β : Type u_3
γ : Type u_4
δ : Type u_5
R : Type u_6
m0 : MeasurableSpace α
inst✝³ : MeasurableSpace β
inst✝² : MeasurableSpace γ
inst✝¹ : MeasurableSpace δ
f g✝ : α → β
μ✝ ν : Measure α
inst✝ : Countable ι
μ : ι → Measure α
h : ∀ (i : ι), AEMeasurable f
✝ : Nontrivial β
inhabited_h : Inhabited β
s : ι → Set α := fun i => toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}
hsμ : ∀ (i : ι), ↑↑(μ i) (s i) = 0
hsm : MeasurableSet (⋂ i, s i)
hs : ∀ (i : ι), ∀ x ∉ s i, f x = mk f (_ : AEMeasurable f) x
g : α → β := piecewise (⋂ i, s i) (const α default) f
t : Set β
ht : MeasurableSet t
x : α
i : ι
hi : x ∉ toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}
⊢ mk f (_ : AEMeasurable f) x ∈ t →
    ∃ i, mk f (_ : AEMeasurable f) x ∈ t ∧ x ∉ toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}

Next tactic:
exact fun h => ⟨i, h, hi⟩
@[measurability]
theorem sum_measure [Countable ι] {μ : ι → Measure α} (h : ∀ i, AEMeasurable f (μ i)) :
    AEMeasurable f (sum μ) := by
  nontriviality β
  inhabit β
  set s : ι → Set α := fun i => toMeasurable (μ i) { x | f x ≠ (h i).mk f x }
  have hsμ : ∀ i, μ i (s i) = 0 := by
    intro i
    rw [measure_toMeasurable]
    exact (h i).ae_eq_mk
  have hsm : MeasurableSet (⋂ i, s i) :=
    MeasurableSet.iInter fun i => measurableSet_toMeasurable _ _
  have hs : ∀ i x, x ∉ s i → f x = (h i).mk f x := by
    intro i x hx
    contrapose! hx
    exact subset_toMeasurable _ _ hx
  set g : α → β := (⋂ i, s i).piecewise (const α default) f
  refine' ⟨g, measurable_of_restrict_of_restrict_compl hsm _ _, ae_sum_iff.mpr fun i => _⟩
  · rw [restrict_piecewise]
    simp only [Set.restrict, const]
    exact measurable_const
  · rw [restrict_piecewise_compl, compl_iInter]
    intro t ht
    refine ⟨⋃ i, (h i).mk f ⁻¹' t ∩ (s i)ᶜ, MeasurableSet.iUnion fun i ↦
      (measurable_mk _ ht).inter (measurableSet_toMeasurable _ _).compl, ?_⟩
    ext ⟨x, hx⟩
    simp only [mem_preimage, mem_iUnion, Subtype.coe_mk, Set.restrict, mem_inter_iff,
      mem_compl_iff] at hx ⊢
    constructor
    · rintro ⟨i, hxt, hxs⟩
      rwa [hs _ _ hxs]
    · rcases hx with ⟨i, hi⟩
      rw [hs _ _ hi]
      exact fun h => ⟨i, h, hi⟩
  · 

Proof state:
case refine'_3
ι : Type u_1
α : Type u_2
β : Type u_3
γ : Type u_4
δ : Type u_5
R : Type u_6
m0 : MeasurableSpace α
inst✝³ : MeasurableSpace β
inst✝² : MeasurableSpace γ
inst✝¹ : MeasurableSpace δ
f g✝ : α → β
μ✝ ν : Measure α
inst✝ : Countable ι
μ : ι → Measure α
h : ∀ (i : ι), AEMeasurable f
✝ : Nontrivial β
inhabited_h : Inhabited β
s : ι → Set α := fun i => toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}
hsμ : ∀ (i : ι), ↑↑(μ i) (s i) = 0
hsm : MeasurableSet (⋂ i, s i)
hs : ∀ (i : ι), ∀ x ∉ s i, f x = mk f (_ : AEMeasurable f) x
g : α → β := piecewise (⋂ i, s i) (const α default) f
i : ι
⊢ ∀ᵐ (x : α) ∂μ i, f x = g x

Next tactic:
refine' measure_mono_null (fun x (hx : f x ≠ g x) => _) (hsμ i)
@[measurability]
theorem sum_measure [Countable ι] {μ : ι → Measure α} (h : ∀ i, AEMeasurable f (μ i)) :
    AEMeasurable f (sum μ) := by
  nontriviality β
  inhabit β
  set s : ι → Set α := fun i => toMeasurable (μ i) { x | f x ≠ (h i).mk f x }
  have hsμ : ∀ i, μ i (s i) = 0 := by
    intro i
    rw [measure_toMeasurable]
    exact (h i).ae_eq_mk
  have hsm : MeasurableSet (⋂ i, s i) :=
    MeasurableSet.iInter fun i => measurableSet_toMeasurable _ _
  have hs : ∀ i x, x ∉ s i → f x = (h i).mk f x := by
    intro i x hx
    contrapose! hx
    exact subset_toMeasurable _ _ hx
  set g : α → β := (⋂ i, s i).piecewise (const α default) f
  refine' ⟨g, measurable_of_restrict_of_restrict_compl hsm _ _, ae_sum_iff.mpr fun i => _⟩
  · rw [restrict_piecewise]
    simp only [Set.restrict, const]
    exact measurable_const
  · rw [restrict_piecewise_compl, compl_iInter]
    intro t ht
    refine ⟨⋃ i, (h i).mk f ⁻¹' t ∩ (s i)ᶜ, MeasurableSet.iUnion fun i ↦
      (measurable_mk _ ht).inter (measurableSet_toMeasurable _ _).compl, ?_⟩
    ext ⟨x, hx⟩
    simp only [mem_preimage, mem_iUnion, Subtype.coe_mk, Set.restrict, mem_inter_iff,
      mem_compl_iff] at hx ⊢
    constructor
    · rintro ⟨i, hxt, hxs⟩
      rwa [hs _ _ hxs]
    · rcases hx with ⟨i, hi⟩
      rw [hs _ _ hi]
      exact fun h => ⟨i, h, hi⟩
  · refine' measure_mono_null (fun x (hx : f x ≠ g x) => _) (hsμ i)
    

Proof state:
case refine'_3
ι : Type u_1
α : Type u_2
β : Type u_3
γ : Type u_4
δ : Type u_5
R : Type u_6
m0 : MeasurableSpace α
inst✝³ : MeasurableSpace β
inst✝² : MeasurableSpace γ
inst✝¹ : MeasurableSpace δ
f g✝ : α → β
μ✝ ν : Measure α
inst✝ : Countable ι
μ : ι → Measure α
h : ∀ (i : ι), AEMeasurable f
✝ : Nontrivial β
inhabited_h : Inhabited β
s : ι → Set α := fun i => toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}
hsμ : ∀ (i : ι), ↑↑(μ i) (s i) = 0
hsm : MeasurableSet (⋂ i, s i)
hs : ∀ (i : ι), ∀ x ∉ s i, f x = mk f (_ : AEMeasurable f) x
g : α → β := piecewise (⋂ i, s i) (const α default) f
i : ι
x : α
hx : f x ≠ g x
⊢ x ∈ s i

Next tactic:
contrapose! hx
@[measurability]
theorem sum_measure [Countable ι] {μ : ι → Measure α} (h : ∀ i, AEMeasurable f (μ i)) :
    AEMeasurable f (sum μ) := by
  nontriviality β
  inhabit β
  set s : ι → Set α := fun i => toMeasurable (μ i) { x | f x ≠ (h i).mk f x }
  have hsμ : ∀ i, μ i (s i) = 0 := by
    intro i
    rw [measure_toMeasurable]
    exact (h i).ae_eq_mk
  have hsm : MeasurableSet (⋂ i, s i) :=
    MeasurableSet.iInter fun i => measurableSet_toMeasurable _ _
  have hs : ∀ i x, x ∉ s i → f x = (h i).mk f x := by
    intro i x hx
    contrapose! hx
    exact subset_toMeasurable _ _ hx
  set g : α → β := (⋂ i, s i).piecewise (const α default) f
  refine' ⟨g, measurable_of_restrict_of_restrict_compl hsm _ _, ae_sum_iff.mpr fun i => _⟩
  · rw [restrict_piecewise]
    simp only [Set.restrict, const]
    exact measurable_const
  · rw [restrict_piecewise_compl, compl_iInter]
    intro t ht
    refine ⟨⋃ i, (h i).mk f ⁻¹' t ∩ (s i)ᶜ, MeasurableSet.iUnion fun i ↦
      (measurable_mk _ ht).inter (measurableSet_toMeasurable _ _).compl, ?_⟩
    ext ⟨x, hx⟩
    simp only [mem_preimage, mem_iUnion, Subtype.coe_mk, Set.restrict, mem_inter_iff,
      mem_compl_iff] at hx ⊢
    constructor
    · rintro ⟨i, hxt, hxs⟩
      rwa [hs _ _ hxs]
    · rcases hx with ⟨i, hi⟩
      rw [hs _ _ hi]
      exact fun h => ⟨i, h, hi⟩
  · refine' measure_mono_null (fun x (hx : f x ≠ g x) => _) (hsμ i)
    contrapose! hx
    

Proof state:
case refine'_3
ι : Type u_1
α : Type u_2
β : Type u_3
γ : Type u_4
δ : Type u_5
R : Type u_6
m0 : MeasurableSpace α
inst✝³ : MeasurableSpace β
inst✝² : MeasurableSpace γ
inst✝¹ : MeasurableSpace δ
f g✝ : α → β
μ✝ ν : Measure α
inst✝ : Countable ι
μ : ι → Measure α
h : ∀ (i : ι), AEMeasurable f
✝ : Nontrivial β
inhabited_h : Inhabited β
s : ι → Set α := fun i => toMeasurable (μ i) {x | f x ≠ mk f (_ : AEMeasurable f) x}
hsμ : ∀ (i : ι), ↑↑(μ i) (s i) = 0
hsm : MeasurableSet (⋂ i, s i)
hs : ∀ (i : ι), ∀ x ∉ s i, f x = mk f (_ : AEMeasurable f) x
g : α → β := piecewise (⋂ i, s i) (const α default) f
i : ι
x : α
hx : x ∉ s i
⊢ f x = g x

Next tactic:
refine' (piecewise_eq_of_not_mem _ _ _ _).symm