[System] You are Leanstral, a Lean 4 proof assistant. Complete the task below using the provided Lean source context. The target mathlib commit is 5e932f97dd25535344f80f9dd8da3aab83df0fe6 (module Mathlib.Probability.Kernel.Disintegration.CDFToKernel).

[User]
Prove the following theorem. The retrieved context below may help.

```lean
lemma IsCondKernelCDF.toKernel_apply {hf : IsCondKernelCDF f κ ν} (p : α × β) :
    hf.toKernel f p = (f p).measure
```

## Retrieved context
-- snippet 1
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.IsRatCondKernelCDF (L78-L81)
lemma IsRatCondKernelCDF.mono (hf : IsRatCondKernelCDF f κ ν) (a : α) :
    ∀ᵐ b ∂(ν a), Monotone (f (a, b)) := by
filter_upwards [hf.isRatStieltjesPoint_ae a] with b hb using hb.mono
```

-- snippet 2
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.IsRatCondKernelCDF (L82-L85)
lemma IsRatCondKernelCDF.tendsto_atTop_one (hf : IsRatCondKernelCDF f κ ν) (a : α) :
    ∀ᵐ b ∂(ν a), Tendsto (f (a, b)) atTop (𝓝 1) := by
filter_upwards [hf.isRatStieltjesPoint_ae a] with b hb using hb.tendsto_atTop_one
```

-- snippet 3
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.IsRatCondKernelCDF (L86-L89)
lemma IsRatCondKernelCDF.tendsto_atBot_zero (hf : IsRatCondKernelCDF f κ ν) (a : α) :
    ∀ᵐ b ∂(ν a), Tendsto (f (a, b)) atBot (𝓝 0) := by
filter_upwards [hf.isRatStieltjesPoint_ae a] with b hb using hb.tendsto_atBot_zero
```

-- snippet 4
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.IsRatCondKernelCDF (L90-L93)
lemma IsRatCondKernelCDF.iInf_rat_gt_eq (hf : IsRatCondKernelCDF f κ ν) (a : α) :
    ∀ᵐ b ∂(ν a), ∀ q, ⨅ r : Ioi q, f (a, b) r = f (a, b) q := by
filter_upwards [hf.isRatStieltjesPoint_ae a] with b hb using hb.iInf_rat_gt_eq
```

-- snippet 5
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.stieltjesOfMeasurableRat_ae_eq (L94-L98)
lemma stieltjesOfMeasurableRat_ae_eq (hf : IsRatCondKernelCDF f κ ν) (a : α) (q : ℚ) :
    (fun b ↦ stieltjesOfMeasurableRat f hf.measurable (a, b) q) =ᵐ[ν a] fun b ↦ f (a, b) q := by
filter_upwards [hf.isRatStieltjesPoint_ae a] with a ha
  rw [stieltjesOfMeasurableRat_eq, toRatCDF_of_isRatStieltjesPoint ha]
```

-- snippet 6
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.setIntegral_stieltjesOfMeasurableRat_rat (L99-L105)
lemma setIntegral_stieltjesOfMeasurableRat_rat (hf : IsRatCondKernelCDF f κ ν) (a : α) (q : ℚ)
    {s : Set β} (hs : MeasurableSet s) :
    ∫ b in s, stieltjesOfMeasurableRat f hf.measurable (a, b) q ∂(ν a)
      = (κ a).real (s ×ˢ Iic (q : ℝ)) := by
rw [setIntegral_congr_ae hs (g := fun b ↦ f (a, b) q) ?_, hf.setIntegral a hs]
  filter_upwards [stieltjesOfMeasurableRat_ae_eq hf a q] with b hb using fun _ ↦ hb
```

-- snippet 7
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.setLIntegral_stieltjesOfMeasurableRat_rat (L106-L116)
lemma setLIntegral_stieltjesOfMeasurableRat_rat [IsFiniteKernel κ] (hf : IsRatCondKernelCDF f κ ν)
    (a : α) (q : ℚ) {s : Set β} (hs : MeasurableSet s) :
    ∫⁻ b in s, ENNReal.ofReal (stieltjesOfMeasurableRat f hf.measurable (a, b) q) ∂(ν a)
      = κ a (s ×ˢ Iic (q : ℝ)) := by
rw [← ofReal_integral_eq_lintegral_ofReal]
  · rw [setIntegral_stieltjesOfMeasurableRat_rat hf a q hs, ofReal_measureReal]
  · refine Integrable.restrict ?_
    rw [integrable_congr (stieltjesOfMeasurableRat_ae_eq hf a q)]
    exact hf.integrable a q
  · exact ae_of_all _ (fun x ↦ stieltjesOfMeasurableRat_nonneg _ _ _)
```

-- snippet 8
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.setLIntegral_stieltjesOfMeasurableRat (L117-L172)
lemma setLIntegral_stieltjesOfMeasurableRat [IsFiniteKernel κ] (hf : IsRatCondKernelCDF f κ ν)
    (a : α) (x : ℝ) {s : Set β} (hs : MeasurableSet s) :
    ∫⁻ b in s, ENNReal.ofReal (stieltjesOfMeasurableRat f hf.measurable (a, b) x) ∂(ν a)
      = κ a (s ×ˢ Iic x) := by
-- We have the result for `x : ℚ` thanks to `setLIntegral_stieltjesOfMeasurableRat_rat`.
  -- We use a monotone convergence argument to extend it to the reals.
  by_cases hρ_zero : (ν a).restrict s = 0
  · rw [hρ_zero, lintegral_zero_measure]
    have ⟨q, hq⟩ := exists_rat_gt x
    suffices κ a (s ×ˢ Iic (q : ℝ)) = 0 by
      symm
      refine measure_mono_null (fun p ↦ ?_) this
      simp only [mem_prod, mem_Iic, and_imp]
      exact fun h1 h2 ↦ ⟨h1, h2.trans hq.le⟩
    suffices (κ a).real (s ×ˢ Iic (q : ℝ)) = 0 by
      rw [measureReal_eq_zero_iff] at this
      simpa [measure_ne_top] using this
    rw [← hf.setIntegral a hs q]
    simp [hρ_zero]
  have h : ∫⁻ b in s, ENNReal.ofReal (stieltjesOfMeasurableRat f hf.measurable (a, b) x) ∂(ν a)
      = ∫⁻ b in s, ⨅ r : { r' : ℚ // x < r' },
        ENNReal.ofReal (stieltjesOfMeasurableRat f hf.measurable (a, b) r) ∂(ν a) := by
    congr with b : 1
    simp_rw [← measure_stieltjesOfMeasurableRat_Iic]
    rw [← Monotone.measure_iInter]
    · congr with y : 1
      simp only [mem_Iic, mem_iInter, Subtype.forall]
      exact le_iff_forall_lt_rat_imp_le
    · exact fun r r' hrr' ↦ Iic_subset_Iic.mpr <| mod_cast hrr'
    · exact fun _ ↦ nullMeasurableSet_Iic
    · obtain ⟨q, hq⟩ := exists_rat_gt x
      exact ⟨⟨q, hq⟩, measure_ne_top _ _⟩
  have h_nonempty : Nonempty { r' : ℚ // x < ↑r' } := by
    obtain ⟨r, hrx⟩ := exists_rat_gt x
    exact ⟨⟨r, hrx⟩⟩
  rw [h, lintegral_iInf_directed_of_measurable hρ_zero fun q : { r' : ℚ // x < ↑r' } ↦ ?_]
  rotate_left
  · intro b
    rw [setLIntegral_stieltjesOfMeasurableRat_rat hf a _ hs]
    exact measure_ne_top _ _
  · refine Monotone.directed_ge fun i j hij b ↦ ?_
    simp_rw [← measure_stieltjesOfMeasurableRat_Iic]
    refine measure_mono (Iic_subset_Iic.mpr ?_)
    exact mod_cast hij
  · refine Measurable.ennreal_ofReal ?_
    exact (measurable_stieltjesOfMeasurableRat hf.measurable _).comp measurable_prodMk_left
  simp_rw [setLIntegral_stieltjesOfMeasurableRat_rat hf _ _ hs]
  rw [← Monotone.measure_iInter]
  · rw [← prod_iInter]
    congr with y
    simp only [mem_iInter, mem_Iic, Subtype.forall]
    exact ⟨le_of_forall_lt_rat_imp_le, fun hyx q hq ↦ hyx.trans hq.le⟩
  · exact fun i j hij ↦ prod_mono_right (by gcongr)
  · exact fun i ↦ (hs.prod measurableSet_Iic).nullMeasurableSet
  · exact ⟨h_nonempty.some, measure_ne_top _ _⟩
```

-- snippet 9
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.lintegral_stieltjesOfMeasurableRat (L173-L178)
lemma lintegral_stieltjesOfMeasurableRat [IsFiniteKernel κ] (hf : IsRatCondKernelCDF f κ ν)
    (a : α) (x : ℝ) :
    ∫⁻ b, ENNReal.ofReal (stieltjesOfMeasurableRat f hf.measurable (a, b) x) ∂(ν a)
      = κ a (univ ×ˢ Iic x) := by
rw [← setLIntegral_univ, setLIntegral_stieltjesOfMeasurableRat hf _ _ MeasurableSet.univ]
```

-- snippet 10
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.integrable_stieltjesOfMeasurableRat (L179-L193)
lemma integrable_stieltjesOfMeasurableRat [IsFiniteKernel κ] (hf : IsRatCondKernelCDF f κ ν)
    (a : α) (x : ℝ) :
    Integrable (fun b ↦ stieltjesOfMeasurableRat f hf.measurable (a, b) x) (ν a) := by
have : (fun b ↦ stieltjesOfMeasurableRat f hf.measurable (a, b) x)
      = fun b ↦ (ENNReal.ofReal (stieltjesOfMeasurableRat f hf.measurable (a, b) x)).toReal := by
    ext t
    rw [ENNReal.toReal_ofReal]
    exact stieltjesOfMeasurableRat_nonneg _ _ _
  rw [this]
  refine integrable_toReal_of_lintegral_ne_top ?_ ?_
  · refine (Measurable.ennreal_ofReal ?_).aemeasurable
    exact (measurable_stieltjesOfMeasurableRat hf.measurable x).comp measurable_prodMk_left
  · rw [lintegral_stieltjesOfMeasurableRat hf]
    exact measure_ne_top _ _
```

-- snippet 11
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.setIntegral_stieltjesOfMeasurableRat (L194-L205)
lemma setIntegral_stieltjesOfMeasurableRat [IsFiniteKernel κ] (hf : IsRatCondKernelCDF f κ ν)
    (a : α) (x : ℝ) {s : Set β} (hs : MeasurableSet s) :
    ∫ b in s, stieltjesOfMeasurableRat f hf.measurable (a, b) x ∂(ν a)
      = (κ a).real (s ×ˢ Iic x) := by
rw [← ENNReal.ofReal_eq_ofReal_iff, ofReal_measureReal]
  rotate_left
  · exact setIntegral_nonneg hs (fun _ _ ↦ stieltjesOfMeasurableRat_nonneg _ _ _)
  · exact ENNReal.toReal_nonneg
  rw [ofReal_integral_eq_lintegral_ofReal, setLIntegral_stieltjesOfMeasurableRat hf _ _ hs]
  · exact (integrable_stieltjesOfMeasurableRat hf _ _).restrict
  · exact ae_of_all _ (fun _ ↦ stieltjesOfMeasurableRat_nonneg _ _ _)
```

-- snippet 12
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.integral_stieltjesOfMeasurableRat (L206-L242)
lemma integral_stieltjesOfMeasurableRat [IsFiniteKernel κ] (hf : IsRatCondKernelCDF f κ ν)
    (a : α) (x : ℝ) :
    ∫ b, stieltjesOfMeasurableRat f hf.measurable (a, b) x ∂(ν a)
      = (κ a).real (univ ×ˢ Iic x) := by
rw [← setIntegral_univ, setIntegral_stieltjesOfMeasurableRat hf _ _ MeasurableSet.univ]

end stieltjesOfMeasurableRat

section isRatCondKernelCDFAux

variable {f : α × β → ℚ → ℝ}

/-- This property implies `IsRatCondKernelCDF`. The measurability, integrability and integral
conditions are the same, but the limit properties of `IsRatCondKernelCDF` are replaced by
limits of integrals. -/
structure IsRatCondKernelCDFAux (f : α × β → ℚ → ℝ) (κ : Kernel α (β × ℝ)) (ν : Kernel α β) :
    Prop where
  measurable : Measurable f
  mono' (a : α) {q r : ℚ} (_hqr : q ≤ r) : ∀ᵐ c ∂(ν a), f (a, c) q ≤ f (a, c) r
  nonneg' (a : α) (q : ℚ) : ∀ᵐ c ∂(ν a), 0 ≤ f (a, c) q
  le_one' (a : α) (q : ℚ) : ∀ᵐ c ∂(ν a), f (a, c) q ≤ 1
  /- Same as `Tendsto (fun q : ℚ ↦ ∫ c, f (a, c) q ∂(ν a)) atBot (𝓝 0)` but slightly easier
  to prove in the current applications of this definition (some integral convergence lemmas
  currently apply only to `ℕ`, not `ℚ`) -/
  tendsto_integral_of_antitone (a : α) (seq : ℕ → ℚ) (_hs : Antitone seq)
    (_hs_tendsto : Tendsto seq atTop atBot) :
    Tendsto (fun m ↦ ∫ c, f (a, c) (seq m) ∂(ν a)) atTop (𝓝 0)
  /- Same as `Tendsto (fun q : ℚ ↦ ∫ c, f (a, c) q ∂(ν a)) atTop (𝓝 ((ν a).real univ))` but
  slightly easier to prove in the current applications of this definition (some integral convergence
  lemmas currently apply only to `ℕ`, not `ℚ`) -/
  tendsto_integral_of_monotone (a : α) (seq : ℕ → ℚ) (_hs : Monotone seq)
    (_hs_tendsto : Tendsto seq atTop atTop) :
    Tendsto (fun m ↦ ∫ c, f (a, c) (seq m) ∂(ν a)) atTop (𝓝 ((ν a).real univ))
  integrable (a : α) (q : ℚ) : Integrable (fun c ↦ f (a, c) q) (ν a)
  setIntegral (a : α) {A : Set β} (_hA : MeasurableSet A) (q : ℚ) :
    ∫ c in A, f (a, c) q ∂(ν a) = (κ a).real (A ×ˢ Iic ↑q)
```

-- snippet 13
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.IsRatCondKernelCDFAux (L243-L248)
lemma IsRatCondKernelCDFAux.measurable_right (hf : IsRatCondKernelCDFAux f κ ν) (a : α) (q : ℚ) :
    Measurable (fun t ↦ f (a, t) q) := by
let h := hf.measurable
  rw [measurable_pi_iff] at h
  exact (h q).comp measurable_prodMk_left
```

-- snippet 14
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.IsRatCondKernelCDFAux (L249-L254)
lemma IsRatCondKernelCDFAux.mono (hf : IsRatCondKernelCDFAux f κ ν) (a : α) :
    ∀ᵐ c ∂(ν a), Monotone (f (a, c)) := by
unfold Monotone
  simp_rw [ae_all_iff]
  exact fun _ _ hqr ↦ hf.mono' a hqr
```

-- snippet 15
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.IsRatCondKernelCDFAux (L255-L257)
lemma IsRatCondKernelCDFAux.nonneg (hf : IsRatCondKernelCDFAux f κ ν) (a : α) :
    ∀ᵐ c ∂(ν a), ∀ q, 0 ≤ f (a, c) q := ae_all_iff.mpr <| hf.nonneg' a
```

-- snippet 16
```lean
-- Mathlib.Probability.Kernel.Basic.isSFiniteKernel_const (L216-L219)
lemma isSFiniteKernel_const [Nonempty α] {μβ : Measure β} :
    IsSFiniteKernel (const α μβ) ↔ SFinite μβ :=
⟨fun h ↦ h.sFinite (Classical.arbitrary α), fun _ ↦ inferInstance⟩
```

-- snippet 17
```lean
-- Mathlib.Probability.Kernel.Basic.const (L178-L182)
def const (α : Type*) {β : Type*} [MeasurableSpace α] {_ : MeasurableSpace β} (μβ : Measure β) :
    Kernel α β where
  toFun _ := μβ
measurable' := measurable_const
```

-- snippet 18
```lean
-- Mathlib.Probability.Kernel.Basic.discard_apply (L151-L158)
lemma discard_apply (a : α) : discard α a = Measure.dirac PUnit.unit := deterministic_apply _ _
end Discard

section Swap

/-- The deterministic kernel that maps `(x, y)` to the Dirac measure at `(y, x)`. -/
noncomputable
```

-- snippet 19
```lean
-- Mathlib.Probability.Kernel.Basic.discard (L145-L147)
def discard (α : Type*) [MeasurableSpace α] : Kernel α PUnit :=
Kernel.deterministic (fun _ ↦ PUnit.unit) measurable_const
```

-- snippet 20
```lean
-- Mathlib.Probability.Kernel.Basic.setLIntegral_deterministic (L100-L110)
theorem setLIntegral_deterministic {f : β → ℝ≥0∞} {g : α → β} {a : α} (hg : Measurable g)
    [MeasurableSingletonClass β] (s : Set β) [Decidable (g a ∈ s)] :
    ∫⁻ x in s, f x ∂deterministic g hg a = if g a ∈ s then f (g a) else 0 := by
rw [deterministic_apply, setLIntegral_dirac f s]

end Deterministic

section Id

/-- The identity kernel, that maps `x : α` to the Dirac measure at `x`. -/
protected noncomputable
```

-- snippet 21
```lean
-- Mathlib.Probability.Kernel.Basic.const_add (L191-L193)
lemma const_add (β : Type*) [MeasurableSpace β] (μ ν : Measure α) :
    const β (μ + ν) = const β μ + const β ν := by ext; simp
```

-- snippet 22
```lean
-- Mathlib.Probability.Kernel.Basic.lintegral_deterministic (L90-L93)
theorem lintegral_deterministic {f : β → ℝ≥0∞} {g : α → β} {a : α} (hg : Measurable g)
    [MeasurableSingletonClass β] : ∫⁻ x, f x ∂deterministic g hg a = f (g a) := by
rw [deterministic_apply, lintegral_dirac (g a) f]
```

-- snippet 23
```lean
-- Mathlib.Probability.Kernel.Basic.lintegral_id (L122-L131)
lemma lintegral_id [MeasurableSingletonClass α] {f : α → ℝ≥0∞} (a : α) :
    ∫⁻ a, f a ∂(@Kernel.id α mα a) = f a := by
rw [id_apply, lintegral_dirac]

end Id

section Copy

/-- The deterministic kernel that maps `x : α` to the Dirac measure at `(x, x) : α × α`. -/
noncomputable
```

-- snippet 24
```lean
-- Mathlib.Probability.Kernel.Basic.swap_apply (L165-L168)
lemma swap_apply (ab : α × β) : swap α β ab = Measure.dirac ab.swap := by
rw [swap, deterministic_apply]

/-- See `swap_apply` for a partially applied version of this lemma. -/
```

-- snippet 25
```lean
-- Mathlib.Probability.Kernel.Basic.lintegral_deterministic' (L85-L88)
theorem lintegral_deterministic' {f : β → ℝ≥0∞} {g : α → β} {a : α} (hg : Measurable g)
    (hf : Measurable f) : ∫⁻ x, f x ∂deterministic g hg a = f (g a) := by
rw [deterministic_apply, lintegral_dirac' _ hf]
```

-- snippet 26
```lean
-- Mathlib.Probability.Kernel.Basic.deterministic_apply (L65-L68)
theorem deterministic_apply {f : α → β} (hf : Measurable f) (a : α) :
    deterministic f hf a = Measure.dirac (f a) :=
rfl
```

-- snippet 27
```lean
-- Mathlib.Probability.Kernel.Basic.lintegral_id' (L118-L121)
lemma lintegral_id' {f : α → ℝ≥0∞} (hf : Measurable f) (a : α) :
    ∫⁻ a, f a ∂(@Kernel.id α mα a) = f a := by
rw [id_apply, lintegral_dirac' _ hf]
```

-- snippet 28
```lean
-- Mathlib.Probability.Kernel.Basic.swap (L159-L161)
def swap (α β : Type*) [MeasurableSpace α] [MeasurableSpace β] : Kernel (α × β) (β × α) :=
Kernel.deterministic Prod.swap measurable_swap
```

-- snippet 29
```lean
-- Mathlib.Probability.Kernel.Basic.deterministic_congr (L77-L80)
theorem deterministic_congr {f g : α → β} {hf : Measurable f} (h : f = g) :
    deterministic f hf = deterministic g (h ▸ hf) := by
grind
```

-- snippet 30
```lean
-- Mathlib.Probability.Kernel.Basic.id (L111-L112)
def id : Kernel α α := Kernel.deterministic id measurable_id
```

-- snippet 31
```lean
-- Mathlib.Probability.Kernel.Basic.setLIntegral_deterministic' (L94-L98)
theorem setLIntegral_deterministic' {f : β → ℝ≥0∞} {g : α → β} {a : α} (hg : Measurable g)
    (hf : Measurable f) {s : Set β} (hs : MeasurableSet s) [Decidable (g a ∈ s)] :
    ∫⁻ x in s, f x ∂deterministic g hg a = if g a ∈ s then f (g a) else 0 := by
rw [deterministic_apply, setLIntegral_dirac' hf hs]
```

-- snippet 32
```lean
-- Mathlib.Probability.Kernel.Basic.copy_apply (L137-L144)
lemma copy_apply (a : α) : copy α a = Measure.dirac (a, a) := by simp [copy, deterministic_apply]
end Copy

section Discard

/-- The Markov kernel to the `PUnit` type. -/
noncomputable
```

-- snippet 33
```lean
-- Mathlib.Probability.Kernel.Basic.const_apply (L184-L186)
theorem const_apply (μβ : Measure β) (a : α) : const α μβ a = μβ :=
rfl
```

-- snippet 34
```lean
-- Mathlib.Probability.Kernel.Basic.const_zero (L188-L190)
lemma const_zero : const α (0 : Measure β) = 0 := by
ext x s _; simp [const_apply]
```

-- snippet 35
```lean
-- Mathlib.Probability.Kernel.Basic.deterministic_apply' (L69-L76)
theorem deterministic_apply' {f : α → β} (hf : Measurable f) (a : α) {s : Set β}
    (hs : MeasurableSet s) : deterministic f hf a s = s.indicator (fun _ => 1) (f a) := by
rw [deterministic]
  change Measure.dirac (f a) s = s.indicator 1 (f a)
  simp_rw [Measure.dirac_apply' _ hs]

/-- Because of the measurability field in `Kernel.deterministic`, `rw [h]` will not rewrite
`deterministic f hf` to `deterministic g ⋯`. Instead one can do `rw [deterministic_congr h]`. -/
```

-- snippet 36
```lean
-- Mathlib.Probability.Kernel.Basic.sum_const (L194-L196)
lemma sum_const [Countable ι] (μ : ι → Measure β) :
    Kernel.sum (fun n ↦ const α (μ n)) = const α (Measure.sum μ) := rfl
```

-- snippet 37
```lean
-- Mathlib.Probability.Kernel.Basic.copy (L132-L134)
def copy (α : Type*) [MeasurableSpace α] : Kernel α (α × α) :=
Kernel.deterministic (fun x ↦ (x, x)) (measurable_id.prod measurable_id)
```

-- snippet 38
```lean
-- Mathlib.Probability.Kernel.Basic.id_apply (L115-L117)
lemma id_apply (a : α) : Kernel.id a = Measure.dirac a := by
rw [Kernel.id, deterministic_apply, id_def]
```

-- snippet 39
```lean
-- Mathlib.Probability.Kernel.Basic.isMarkovKernel_deterministic (L81-L84)
instance isMarkovKernel_deterministic {f : α → β} (hf : Measurable f) :
    IsMarkovKernel (deterministic f hf) :=
⟨fun a => by rw [deterministic_apply hf]; infer_instance⟩
```

-- snippet 40
```lean
-- Mathlib.Probability.Kernel.Basic.swap_apply' (L169-L177)
lemma swap_apply' (ab : α × β) {s : Set (β × α)} (hs : MeasurableSet s) :
    swap α β ab s = s.indicator 1 ab.swap := by
rw [swap_apply, Measure.dirac_apply' _ hs]

end Swap

section Const

/-- Constant kernel, which always returns the same measure. -/
```

<s>[SYSTEM_PROMPT]You are Leanstral, a Lean 4 proof assistant. Complete the task below using the provided Lean source context. The target mathlib commit is 5e932f97dd25535344f80f9dd8da3aab83df0fe6 (module Mathlib.Probability.Kernel.Disintegration.CDFToKernel).[/SYSTEM_PROMPT][MODEL_SETTINGS]{"reasoning_effort": "none"}[/MODEL_SETTINGS][INST]Prove the following theorem. The retrieved context below may help.

```lean
lemma IsCondKernelCDF.toKernel_apply {hf : IsCondKernelCDF f κ ν} (p : α × β) :
    hf.toKernel f p = (f p).measure
```

## Retrieved context
-- snippet 1
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.IsRatCondKernelCDF (L78-L81)
lemma IsRatCondKernelCDF.mono (hf : IsRatCondKernelCDF f κ ν) (a : α) :
    ∀ᵐ b ∂(ν a), Monotone (f (a, b)) := by
filter_upwards [hf.isRatStieltjesPoint_ae a] with b hb using hb.mono
```

-- snippet 2
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.IsRatCondKernelCDF (L82-L85)
lemma IsRatCondKernelCDF.tendsto_atTop_one (hf : IsRatCondKernelCDF f κ ν) (a : α) :
    ∀ᵐ b ∂(ν a), Tendsto (f (a, b)) atTop (𝓝 1) := by
filter_upwards [hf.isRatStieltjesPoint_ae a] with b hb using hb.tendsto_atTop_one
```

-- snippet 3
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.IsRatCondKernelCDF (L86-L89)
lemma IsRatCondKernelCDF.tendsto_atBot_zero (hf : IsRatCondKernelCDF f κ ν) (a : α) :
    ∀ᵐ b ∂(ν a), Tendsto (f (a, b)) atBot (𝓝 0) := by
filter_upwards [hf.isRatStieltjesPoint_ae a] with b hb using hb.tendsto_atBot_zero
```

-- snippet 4
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.IsRatCondKernelCDF (L90-L93)
lemma IsRatCondKernelCDF.iInf_rat_gt_eq (hf : IsRatCondKernelCDF f κ ν) (a : α) :
    ∀ᵐ b ∂(ν a), ∀ q, ⨅ r : Ioi q, f (a, b) r = f (a, b) q := by
filter_upwards [hf.isRatStieltjesPoint_ae a] with b hb using hb.iInf_rat_gt_eq
```

-- snippet 5
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.stieltjesOfMeasurableRat_ae_eq (L94-L98)
lemma stieltjesOfMeasurableRat_ae_eq (hf : IsRatCondKernelCDF f κ ν) (a : α) (q : ℚ) :
    (fun b ↦ stieltjesOfMeasurableRat f hf.measurable (a, b) q) =ᵐ[ν a] fun b ↦ f (a, b) q := by
filter_upwards [hf.isRatStieltjesPoint_ae a] with a ha
  rw [stieltjesOfMeasurableRat_eq, toRatCDF_of_isRatStieltjesPoint ha]
```

-- snippet 6
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.setIntegral_stieltjesOfMeasurableRat_rat (L99-L105)
lemma setIntegral_stieltjesOfMeasurableRat_rat (hf : IsRatCondKernelCDF f κ ν) (a : α) (q : ℚ)
    {s : Set β} (hs : MeasurableSet s) :
    ∫ b in s, stieltjesOfMeasurableRat f hf.measurable (a, b) q ∂(ν a)
      = (κ a).real (s ×ˢ Iic (q : ℝ)) := by
rw [setIntegral_congr_ae hs (g := fun b ↦ f (a, b) q) ?_, hf.setIntegral a hs]
  filter_upwards [stieltjesOfMeasurableRat_ae_eq hf a q] with b hb using fun _ ↦ hb
```

-- snippet 7
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.setLIntegral_stieltjesOfMeasurableRat_rat (L106-L116)
lemma setLIntegral_stieltjesOfMeasurableRat_rat [IsFiniteKernel κ] (hf : IsRatCondKernelCDF f κ ν)
    (a : α) (q : ℚ) {s : Set β} (hs : MeasurableSet s) :
    ∫⁻ b in s, ENNReal.ofReal (stieltjesOfMeasurableRat f hf.measurable (a, b) q) ∂(ν a)
      = κ a (s ×ˢ Iic (q : ℝ)) := by
rw [← ofReal_integral_eq_lintegral_ofReal]
  · rw [setIntegral_stieltjesOfMeasurableRat_rat hf a q hs, ofReal_measureReal]
  · refine Integrable.restrict ?_
    rw [integrable_congr (stieltjesOfMeasurableRat_ae_eq hf a q)]
    exact hf.integrable a q
  · exact ae_of_all _ (fun x ↦ stieltjesOfMeasurableRat_nonneg _ _ _)
```

-- snippet 8
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.setLIntegral_stieltjesOfMeasurableRat (L117-L172)
lemma setLIntegral_stieltjesOfMeasurableRat [IsFiniteKernel κ] (hf : IsRatCondKernelCDF f κ ν)
    (a : α) (x : ℝ) {s : Set β} (hs : MeasurableSet s) :
    ∫⁻ b in s, ENNReal.ofReal (stieltjesOfMeasurableRat f hf.measurable (a, b) x) ∂(ν a)
      = κ a (s ×ˢ Iic x) := by
-- We have the result for `x : ℚ` thanks to `setLIntegral_stieltjesOfMeasurableRat_rat`.
  -- We use a monotone convergence argument to extend it to the reals.
  by_cases hρ_zero : (ν a).restrict s = 0
  · rw [hρ_zero, lintegral_zero_measure]
    have ⟨q, hq⟩ := exists_rat_gt x
    suffices κ a (s ×ˢ Iic (q : ℝ)) = 0 by
      symm
      refine measure_mono_null (fun p ↦ ?_) this
      simp only [mem_prod, mem_Iic, and_imp]
      exact fun h1 h2 ↦ ⟨h1, h2.trans hq.le⟩
    suffices (κ a).real (s ×ˢ Iic (q : ℝ)) = 0 by
      rw [measureReal_eq_zero_iff] at this
      simpa [measure_ne_top] using this
    rw [← hf.setIntegral a hs q]
    simp [hρ_zero]
  have h : ∫⁻ b in s, ENNReal.ofReal (stieltjesOfMeasurableRat f hf.measurable (a, b) x) ∂(ν a)
      = ∫⁻ b in s, ⨅ r : { r' : ℚ // x < r' },
        ENNReal.ofReal (stieltjesOfMeasurableRat f hf.measurable (a, b) r) ∂(ν a) := by
    congr with b : 1
    simp_rw [← measure_stieltjesOfMeasurableRat_Iic]
    rw [← Monotone.measure_iInter]
    · congr with y : 1
      simp only [mem_Iic, mem_iInter, Subtype.forall]
      exact le_iff_forall_lt_rat_imp_le
    · exact fun r r' hrr' ↦ Iic_subset_Iic.mpr <| mod_cast hrr'
    · exact fun _ ↦ nullMeasurableSet_Iic
    · obtain ⟨q, hq⟩ := exists_rat_gt x
      exact ⟨⟨q, hq⟩, measure_ne_top _ _⟩
  have h_nonempty : Nonempty { r' : ℚ // x < ↑r' } := by
    obtain ⟨r, hrx⟩ := exists_rat_gt x
    exact ⟨⟨r, hrx⟩⟩
  rw [h, lintegral_iInf_directed_of_measurable hρ_zero fun q : { r' : ℚ // x < ↑r' } ↦ ?_]
  rotate_left
  · intro b
    rw [setLIntegral_stieltjesOfMeasurableRat_rat hf a _ hs]
    exact measure_ne_top _ _
  · refine Monotone.directed_ge fun i j hij b ↦ ?_
    simp_rw [← measure_stieltjesOfMeasurableRat_Iic]
    refine measure_mono (Iic_subset_Iic.mpr ?_)
    exact mod_cast hij
  · refine Measurable.ennreal_ofReal ?_
    exact (measurable_stieltjesOfMeasurableRat hf.measurable _).comp measurable_prodMk_left
  simp_rw [setLIntegral_stieltjesOfMeasurableRat_rat hf _ _ hs]
  rw [← Monotone.measure_iInter]
  · rw [← prod_iInter]
    congr with y
    simp only [mem_iInter, mem_Iic, Subtype.forall]
    exact ⟨le_of_forall_lt_rat_imp_le, fun hyx q hq ↦ hyx.trans hq.le⟩
  · exact fun i j hij ↦ prod_mono_right (by gcongr)
  · exact fun i ↦ (hs.prod measurableSet_Iic).nullMeasurableSet
  · exact ⟨h_nonempty.some, measure_ne_top _ _⟩
```

-- snippet 9
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.lintegral_stieltjesOfMeasurableRat (L173-L178)
lemma lintegral_stieltjesOfMeasurableRat [IsFiniteKernel κ] (hf : IsRatCondKernelCDF f κ ν)
    (a : α) (x : ℝ) :
    ∫⁻ b, ENNReal.ofReal (stieltjesOfMeasurableRat f hf.measurable (a, b) x) ∂(ν a)
      = κ a (univ ×ˢ Iic x) := by
rw [← setLIntegral_univ, setLIntegral_stieltjesOfMeasurableRat hf _ _ MeasurableSet.univ]
```

-- snippet 10
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.integrable_stieltjesOfMeasurableRat (L179-L193)
lemma integrable_stieltjesOfMeasurableRat [IsFiniteKernel κ] (hf : IsRatCondKernelCDF f κ ν)
    (a : α) (x : ℝ) :
    Integrable (fun b ↦ stieltjesOfMeasurableRat f hf.measurable (a, b) x) (ν a) := by
have : (fun b ↦ stieltjesOfMeasurableRat f hf.measurable (a, b) x)
      = fun b ↦ (ENNReal.ofReal (stieltjesOfMeasurableRat f hf.measurable (a, b) x)).toReal := by
    ext t
    rw [ENNReal.toReal_ofReal]
    exact stieltjesOfMeasurableRat_nonneg _ _ _
  rw [this]
  refine integrable_toReal_of_lintegral_ne_top ?_ ?_
  · refine (Measurable.ennreal_ofReal ?_).aemeasurable
    exact (measurable_stieltjesOfMeasurableRat hf.measurable x).comp measurable_prodMk_left
  · rw [lintegral_stieltjesOfMeasurableRat hf]
    exact measure_ne_top _ _
```

-- snippet 11
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.setIntegral_stieltjesOfMeasurableRat (L194-L205)
lemma setIntegral_stieltjesOfMeasurableRat [IsFiniteKernel κ] (hf : IsRatCondKernelCDF f κ ν)
    (a : α) (x : ℝ) {s : Set β} (hs : MeasurableSet s) :
    ∫ b in s, stieltjesOfMeasurableRat f hf.measurable (a, b) x ∂(ν a)
      = (κ a).real (s ×ˢ Iic x) := by
rw [← ENNReal.ofReal_eq_ofReal_iff, ofReal_measureReal]
  rotate_left
  · exact setIntegral_nonneg hs (fun _ _ ↦ stieltjesOfMeasurableRat_nonneg _ _ _)
  · exact ENNReal.toReal_nonneg
  rw [ofReal_integral_eq_lintegral_ofReal, setLIntegral_stieltjesOfMeasurableRat hf _ _ hs]
  · exact (integrable_stieltjesOfMeasurableRat hf _ _).restrict
  · exact ae_of_all _ (fun _ ↦ stieltjesOfMeasurableRat_nonneg _ _ _)
```

-- snippet 12
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.integral_stieltjesOfMeasurableRat (L206-L242)
lemma integral_stieltjesOfMeasurableRat [IsFiniteKernel κ] (hf : IsRatCondKernelCDF f κ ν)
    (a : α) (x : ℝ) :
    ∫ b, stieltjesOfMeasurableRat f hf.measurable (a, b) x ∂(ν a)
      = (κ a).real (univ ×ˢ Iic x) := by
rw [← setIntegral_univ, setIntegral_stieltjesOfMeasurableRat hf _ _ MeasurableSet.univ]

end stieltjesOfMeasurableRat

section isRatCondKernelCDFAux

variable {f : α × β → ℚ → ℝ}

/-- This property implies `IsRatCondKernelCDF`. The measurability, integrability and integral
conditions are the same, but the limit properties of `IsRatCondKernelCDF` are replaced by
limits of integrals. -/
structure IsRatCondKernelCDFAux (f : α × β → ℚ → ℝ) (κ : Kernel α (β × ℝ)) (ν : Kernel α β) :
    Prop where
  measurable : Measurable f
  mono' (a : α) {q r : ℚ} (_hqr : q ≤ r) : ∀ᵐ c ∂(ν a), f (a, c) q ≤ f (a, c) r
  nonneg' (a : α) (q : ℚ) : ∀ᵐ c ∂(ν a), 0 ≤ f (a, c) q
  le_one' (a : α) (q : ℚ) : ∀ᵐ c ∂(ν a), f (a, c) q ≤ 1
  /- Same as `Tendsto (fun q : ℚ ↦ ∫ c, f (a, c) q ∂(ν a)) atBot (𝓝 0)` but slightly easier
  to prove in the current applications of this definition (some integral convergence lemmas
  currently apply only to `ℕ`, not `ℚ`) -/
  tendsto_integral_of_antitone (a : α) (seq : ℕ → ℚ) (_hs : Antitone seq)
    (_hs_tendsto : Tendsto seq atTop atBot) :
    Tendsto (fun m ↦ ∫ c, f (a, c) (seq m) ∂(ν a)) atTop (𝓝 0)
  /- Same as `Tendsto (fun q : ℚ ↦ ∫ c, f (a, c) q ∂(ν a)) atTop (𝓝 ((ν a).real univ))` but
  slightly easier to prove in the current applications of this definition (some integral convergence
  lemmas currently apply only to `ℕ`, not `ℚ`) -/
  tendsto_integral_of_monotone (a : α) (seq : ℕ → ℚ) (_hs : Monotone seq)
    (_hs_tendsto : Tendsto seq atTop atTop) :
    Tendsto (fun m ↦ ∫ c, f (a, c) (seq m) ∂(ν a)) atTop (𝓝 ((ν a).real univ))
  integrable (a : α) (q : ℚ) : Integrable (fun c ↦ f (a, c) q) (ν a)
  setIntegral (a : α) {A : Set β} (_hA : MeasurableSet A) (q : ℚ) :
    ∫ c in A, f (a, c) q ∂(ν a) = (κ a).real (A ×ˢ Iic ↑q)
```

-- snippet 13
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.IsRatCondKernelCDFAux (L243-L248)
lemma IsRatCondKernelCDFAux.measurable_right (hf : IsRatCondKernelCDFAux f κ ν) (a : α) (q : ℚ) :
    Measurable (fun t ↦ f (a, t) q) := by
let h := hf.measurable
  rw [measurable_pi_iff] at h
  exact (h q).comp measurable_prodMk_left
```

-- snippet 14
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.IsRatCondKernelCDFAux (L249-L254)
lemma IsRatCondKernelCDFAux.mono (hf : IsRatCondKernelCDFAux f κ ν) (a : α) :
    ∀ᵐ c ∂(ν a), Monotone (f (a, c)) := by
unfold Monotone
  simp_rw [ae_all_iff]
  exact fun _ _ hqr ↦ hf.mono' a hqr
```

-- snippet 15
```lean
-- Mathlib.Probability.Kernel.Disintegration.CDFToKernel.IsRatCondKernelCDFAux (L255-L257)
lemma IsRatCondKernelCDFAux.nonneg (hf : IsRatCondKernelCDFAux f κ ν) (a : α) :
    ∀ᵐ c ∂(ν a), ∀ q, 0 ≤ f (a, c) q := ae_all_iff.mpr <| hf.nonneg' a
```

-- snippet 16
```lean
-- Mathlib.Probability.Kernel.Basic.isSFiniteKernel_const (L216-L219)
lemma isSFiniteKernel_const [Nonempty α] {μβ : Measure β} :
    IsSFiniteKernel (const α μβ) ↔ SFinite μβ :=
⟨fun h ↦ h.sFinite (Classical.arbitrary α), fun _ ↦ inferInstance⟩
```

-- snippet 17
```lean
-- Mathlib.Probability.Kernel.Basic.const (L178-L182)
def const (α : Type*) {β : Type*} [MeasurableSpace α] {_ : MeasurableSpace β} (μβ : Measure β) :
    Kernel α β where
  toFun _ := μβ
measurable' := measurable_const
```

-- snippet 18
```lean
-- Mathlib.Probability.Kernel.Basic.discard_apply (L151-L158)
lemma discard_apply (a : α) : discard α a = Measure.dirac PUnit.unit := deterministic_apply _ _
end Discard

section Swap

/-- The deterministic kernel that maps `(x, y)` to the Dirac measure at `(y, x)`. -/
noncomputable
```

-- snippet 19
```lean
-- Mathlib.Probability.Kernel.Basic.discard (L145-L147)
def discard (α : Type*) [MeasurableSpace α] : Kernel α PUnit :=
Kernel.deterministic (fun _ ↦ PUnit.unit) measurable_const
```

-- snippet 20
```lean
-- Mathlib.Probability.Kernel.Basic.setLIntegral_deterministic (L100-L110)
theorem setLIntegral_deterministic {f : β → ℝ≥0∞} {g : α → β} {a : α} (hg : Measurable g)
    [MeasurableSingletonClass β] (s : Set β) [Decidable (g a ∈ s)] :
    ∫⁻ x in s, f x ∂deterministic g hg a = if g a ∈ s then f (g a) else 0 := by
rw [deterministic_apply, setLIntegral_dirac f s]

end Deterministic

section Id

/-- The identity kernel, that maps `x : α` to the Dirac measure at `x`. -/
protected noncomputable
```

-- snippet 21
```lean
-- Mathlib.Probability.Kernel.Basic.const_add (L191-L193)
lemma const_add (β : Type*) [MeasurableSpace β] (μ ν : Measure α) :
    const β (μ + ν) = const β μ + const β ν := by ext; simp
```

-- snippet 22
```lean
-- Mathlib.Probability.Kernel.Basic.lintegral_deterministic (L90-L93)
theorem lintegral_deterministic {f : β → ℝ≥0∞} {g : α → β} {a : α} (hg : Measurable g)
    [MeasurableSingletonClass β] : ∫⁻ x, f x ∂deterministic g hg a = f (g a) := by
rw [deterministic_apply, lintegral_dirac (g a) f]
```

-- snippet 23
```lean
-- Mathlib.Probability.Kernel.Basic.lintegral_id (L122-L131)
lemma lintegral_id [MeasurableSingletonClass α] {f : α → ℝ≥0∞} (a : α) :
    ∫⁻ a, f a ∂(@Kernel.id α mα a) = f a := by
rw [id_apply, lintegral_dirac]

end Id

section Copy

/-- The deterministic kernel that maps `x : α` to the Dirac measure at `(x, x) : α × α`. -/
noncomputable
```

-- snippet 24
```lean
-- Mathlib.Probability.Kernel.Basic.swap_apply (L165-L168)
lemma swap_apply (ab : α × β) : swap α β ab = Measure.dirac ab.swap := by
rw [swap, deterministic_apply]

/-- See `swap_apply` for a partially applied version of this lemma. -/
```

-- snippet 25
```lean
-- Mathlib.Probability.Kernel.Basic.lintegral_deterministic' (L85-L88)
theorem lintegral_deterministic' {f : β → ℝ≥0∞} {g : α → β} {a : α} (hg : Measurable g)
    (hf : Measurable f) : ∫⁻ x, f x ∂deterministic g hg a = f (g a) := by
rw [deterministic_apply, lintegral_dirac' _ hf]
```

-- snippet 26
```lean
-- Mathlib.Probability.Kernel.Basic.deterministic_apply (L65-L68)
theorem deterministic_apply {f : α → β} (hf : Measurable f) (a : α) :
    deterministic f hf a = Measure.dirac (f a) :=
rfl
```

-- snippet 27
```lean
-- Mathlib.Probability.Kernel.Basic.lintegral_id' (L118-L121)
lemma lintegral_id' {f : α → ℝ≥0∞} (hf : Measurable f) (a : α) :
    ∫⁻ a, f a ∂(@Kernel.id α mα a) = f a := by
rw [id_apply, lintegral_dirac' _ hf]
```

-- snippet 28
```lean
-- Mathlib.Probability.Kernel.Basic.swap (L159-L161)
def swap (α β : Type*) [MeasurableSpace α] [MeasurableSpace β] : Kernel (α × β) (β × α) :=
Kernel.deterministic Prod.swap measurable_swap
```

-- snippet 29
```lean
-- Mathlib.Probability.Kernel.Basic.deterministic_congr (L77-L80)
theorem deterministic_congr {f g : α → β} {hf : Measurable f} (h : f = g) :
    deterministic f hf = deterministic g (h ▸ hf) := by
grind
```

-- snippet 30
```lean
-- Mathlib.Probability.Kernel.Basic.id (L111-L112)
def id : Kernel α α := Kernel.deterministic id measurable_id
```

-- snippet 31
```lean
-- Mathlib.Probability.Kernel.Basic.setLIntegral_deterministic' (L94-L98)
theorem setLIntegral_deterministic' {f : β → ℝ≥0∞} {g : α → β} {a : α} (hg : Measurable g)
    (hf : Measurable f) {s : Set β} (hs : MeasurableSet s) [Decidable (g a ∈ s)] :
    ∫⁻ x in s, f x ∂deterministic g hg a = if g a ∈ s then f (g a) else 0 := by
rw [deterministic_apply, setLIntegral_dirac' hf hs]
```

-- snippet 32
```lean
-- Mathlib.Probability.Kernel.Basic.copy_apply (L137-L144)
lemma copy_apply (a : α) : copy α a = Measure.dirac (a, a) := by simp [copy, deterministic_apply]
end Copy

section Discard

/-- The Markov kernel to the `PUnit` type. -/
noncomputable
```

-- snippet 33
```lean
-- Mathlib.Probability.Kernel.Basic.const_apply (L184-L186)
theorem const_apply (μβ : Measure β) (a : α) : const α μβ a = μβ :=
rfl
```

-- snippet 34
```lean
-- Mathlib.Probability.Kernel.Basic.const_zero (L188-L190)
lemma const_zero : const α (0 : Measure β) = 0 := by
ext x s _; simp [const_apply]
```

-- snippet 35
```lean
-- Mathlib.Probability.Kernel.Basic.deterministic_apply' (L69-L76)
theorem deterministic_apply' {f : α → β} (hf : Measurable f) (a : α) {s : Set β}
    (hs : MeasurableSet s) : deterministic f hf a s = s.indicator (fun _ => 1) (f a) := by
rw [deterministic]
  change Measure.dirac (f a) s = s.indicator 1 (f a)
  simp_rw [Measure.dirac_apply' _ hs]

/-- Because of the measurability field in `Kernel.deterministic`, `rw [h]` will not rewrite
`deterministic f hf` to `deterministic g ⋯`. Instead one can do `rw [deterministic_congr h]`. -/
```

-- snippet 36
```lean
-- Mathlib.Probability.Kernel.Basic.sum_const (L194-L196)
lemma sum_const [Countable ι] (μ : ι → Measure β) :
    Kernel.sum (fun n ↦ const α (μ n)) = const α (Measure.sum μ) := rfl
```

-- snippet 37
```lean
-- Mathlib.Probability.Kernel.Basic.copy (L132-L134)
def copy (α : Type*) [MeasurableSpace α] : Kernel α (α × α) :=
Kernel.deterministic (fun x ↦ (x, x)) (measurable_id.prod measurable_id)
```

-- snippet 38
```lean
-- Mathlib.Probability.Kernel.Basic.id_apply (L115-L117)
lemma id_apply (a : α) : Kernel.id a = Measure.dirac a := by
rw [Kernel.id, deterministic_apply, id_def]
```

-- snippet 39
```lean
-- Mathlib.Probability.Kernel.Basic.isMarkovKernel_deterministic (L81-L84)
instance isMarkovKernel_deterministic {f : α → β} (hf : Measurable f) :
    IsMarkovKernel (deterministic f hf) :=
⟨fun a => by rw [deterministic_apply hf]; infer_instance⟩
```

-- snippet 40
```lean
-- Mathlib.Probability.Kernel.Basic.swap_apply' (L169-L177)
lemma swap_apply' (ab : α × β) {s : Set (β × α)} (hs : MeasurableSet s) :
    swap α β ab s = s.indicator 1 ab.swap := by
rw [swap_apply, Measure.dirac_apply' _ hs]

end Swap

section Const

/-- Constant kernel, which always returns the same measure. -/
```
[/INST]


[System] You are Leanstral, a Lean 4 proof assistant. Complete the task below using the provided Lean source context. The target mathlib commit is 5e932f97dd25535344f80f9dd8da3aab83df0fe6 (module Mathlib.MeasureTheory.Measure.MeasureSpace).

[User]
Prove the following theorem. The retrieved context below may help.

```lean
theorem Ioc_ae_eq_Icc' (ha : μ {a} = 0) : Ioc a b =ᵐ[μ] Icc a b
```

## Retrieved context
-- snippet 1
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.ae_isMeasurablyGenerated (L102-L107)
instance ae_isMeasurablyGenerated : IsMeasurablyGenerated (ae μ) :=
⟨fun _s hs =>
    let ⟨t, hst, htm, htμ⟩ := exists_measurable_superset_of_null hs
    ⟨tᶜ, compl_mem_ae_iff.2 htμ, htm.compl, compl_subset_comm.1 hst⟩⟩

/-- See also `MeasureTheory.ae_restrict_uIoc_iff`. -/
```

-- snippet 2
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.ae_uIoc_iff (L108-L111)
theorem ae_uIoc_iff [LinearOrder α] {a b : α} {P : α → Prop} :
    (∀ᵐ x ∂μ, x ∈ Ι a b → P x) ↔ (∀ᵐ x ∂μ, x ∈ Ioc a b → P x) ∧ ∀ᵐ x ∂μ, x ∈ Ioc b a → P x := by
simp only [uIoc_eq_union, mem_union, or_imp, eventually_and]
```

-- snippet 3
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_union (L112-L114)
theorem measure_union (hd : Disjoint s₁ s₂) (h : MeasurableSet s₂) : μ (s₁ ∪ s₂) = μ s₁ + μ s₂ :=
measure_union₀ h.nullMeasurableSet hd.aedisjoint
```

-- snippet 4
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_union' (L115-L117)
theorem measure_union' (hd : Disjoint s₁ s₂) (h : MeasurableSet s₁) : μ (s₁ ∪ s₂) = μ s₁ + μ s₂ :=
measure_union₀' h.nullMeasurableSet hd.aedisjoint
```

-- snippet 5
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_inter_add_diff (L118-L120)
theorem measure_inter_add_diff (s : Set α) (ht : MeasurableSet t) : μ (s ∩ t) + μ (s \ t) = μ s :=
measure_inter_add_diff₀ _ ht.nullMeasurableSet
```

-- snippet 6
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_diff_add_inter (L121-L123)
theorem measure_diff_add_inter (s : Set α) (ht : MeasurableSet t) : μ (s \ t) + μ (s ∩ t) = μ s :=
(add_comm _ _).trans (measure_inter_add_diff s ht)
```

-- snippet 7
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_diff_eq_top (L124-L128)
theorem measure_diff_eq_top (hs : μ s = ∞) (ht : μ t ≠ ∞) : μ (s \ t) = ∞ := by
contrapose! hs
  exact ((measure_mono (subset_diff_union s t)).trans_lt
    ((measure_union_le _ _).trans_lt (ENNReal.add_lt_top.2 ⟨hs.lt_top, ht.lt_top⟩))).ne
```

-- snippet 8
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_union_add_inter (L129-L134)
theorem measure_union_add_inter (s : Set α) (ht : MeasurableSet t) :
    μ (s ∪ t) + μ (s ∩ t) = μ s + μ t := by
rw [← measure_inter_add_diff (s ∪ t) ht, Set.union_inter_cancel_right, union_diff_right, ←
    measure_inter_add_diff s ht]
  ac_rfl
```

-- snippet 9
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_union_add_inter' (L135-L138)
theorem measure_union_add_inter' (hs : MeasurableSet s) (t : Set α) :
    μ (s ∪ t) + μ (s ∩ t) = μ s + μ t := by
rw [union_comm, inter_comm, measure_union_add_inter t hs, add_comm]
```

-- snippet 10
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_symmDiff_eq (L139-L143)
lemma measure_symmDiff_eq (hs : NullMeasurableSet s μ) (ht : NullMeasurableSet t μ) :
    μ (s ∆ t) = μ (s \ t) + μ (t \ s) := by
simpa only [symmDiff_def, sup_eq_union]
    using measure_union₀ (ht.diff hs) disjoint_sdiff_sdiff.aedisjoint
```

-- snippet 11
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_symmDiff_le (L144-L147)
lemma measure_symmDiff_le (s t u : Set α) :
    μ (s ∆ u) ≤ μ (s ∆ t) + μ (t ∆ u) :=
le_trans (μ.mono <| symmDiff_triangle s t u) (measure_union_le (s ∆ t) (t ∆ u))
```

-- snippet 12
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_symmDiff_eq_top (L148-L150)
theorem measure_symmDiff_eq_top (hs : μ s ≠ ∞) (ht : μ t = ∞) : μ (s ∆ t) = ∞ :=
measure_mono_top subset_union_right (measure_diff_eq_top ht hs)
```

-- snippet 13
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_add_measure_compl (L151-L153)
theorem measure_add_measure_compl (h : MeasurableSet s) : μ s + μ sᶜ = μ univ :=
measure_add_measure_compl₀ h.nullMeasurableSet
```

-- snippet 14
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_biUnion₀ (L154-L160)
theorem measure_biUnion₀ {s : Set β} {f : β → Set α} (hs : s.Countable)
    (hd : s.Pairwise (AEDisjoint μ on f)) (h : ∀ b ∈ s, NullMeasurableSet (f b) μ) :
    μ (⋃ b ∈ s, f b) = ∑' p : s, μ (f p) := by
haveI := hs.toEncodable
  rw [biUnion_eq_iUnion]
  exact measure_iUnion₀ (hd.on_injective Subtype.coe_injective fun x => x.2) fun x => h x x.2
```

-- snippet 15
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_biUnion (L161-L164)
theorem measure_biUnion {s : Set β} {f : β → Set α} (hs : s.Countable) (hd : s.PairwiseDisjoint f)
    (h : ∀ b ∈ s, MeasurableSet (f b)) : μ (⋃ b ∈ s, f b) = ∑' p : s, μ (f p) :=
measure_biUnion₀ hs hd.aedisjoint fun b hb => (h b hb).nullMeasurableSet
```

-- snippet 16
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.union_left (L101-L103)
theorem union_left (hs : AEDisjoint μ s u) (ht : AEDisjoint μ t u) : AEDisjoint μ (s ∪ t) u :=
union_left_iff.mpr ⟨hs, ht⟩
```

-- snippet 17
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.of_null_left (L130-L134)
theorem of_null_left (h : μ s = 0) : AEDisjoint μ s t :=
AEDisjoint.symm (of_null_right h)

end AEDisjoint
```

-- snippet 18
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.AEDisjoint (L28-L34)
def AEDisjoint (s t : Set α) :=
μ (s ∩ t) = 0

variable {μ} {s t u v : Set α}

/-- If `s : ι → Set α` is a countable family of pairwise a.e. disjoint sets, then there exists a
family of measurable null sets `t i` such that `s i \ t i` are pairwise disjoint. -/
```

-- snippet 19
```lean
-- Mathlib.MeasureTheory.Measure.AEMeasurable.mono_measure (L56-L58)
theorem mono_measure (h : AEMeasurable f μ) (h' : ν ≤ μ) : AEMeasurable f ν :=
mono_ac h h'.absolutelyContinuous
```

-- snippet 20
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.of_null_right (L127-L129)
theorem of_null_right (h : μ t = 0) : AEDisjoint μ s t :=
measure_mono_null inter_subset_right h
```

-- snippet 21
```lean
-- Mathlib.MeasureTheory.Measure.AEMeasurable.Subsingleton (L29-L31)
theorem Subsingleton.aemeasurable [Subsingleton α] : AEMeasurable f μ :=
Subsingleton.measurable.aemeasurable
```

-- snippet 22
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.measure_diff_left (L113-L115)
theorem measure_diff_left (h : AEDisjoint μ s t) : μ (s \ t) = μ s :=
measure_congr <| AEDisjoint.diff_ae_eq_left h
```

-- snippet 23
```lean
-- Mathlib.MeasureTheory.Measure.AEMeasurable.aemeasurable_of_map_neZero (L45-L52)
lemma aemeasurable_of_map_neZero {μ : Measure α}
    {f : α → β} (h : NeZero (μ.map f)) :
    AEMeasurable f μ := by
by_contra h'
  simp [h'] at h

namespace AEMeasurable
```

-- snippet 24
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.union_right_iff (L98-L100)
theorem union_right_iff : AEDisjoint μ s (t ∪ u) ↔ AEDisjoint μ s t ∧ AEDisjoint μ s u := by
simp [union_eq_iUnion, and_comm]
```

-- snippet 25
```lean
-- Mathlib.MeasureTheory.Measure.AEMeasurable.mono_ac (L53-L55)
lemma mono_ac (hf : AEMeasurable f ν) (hμν : μ ≪ ν) : AEMeasurable f μ :=
⟨hf.mk f, hf.measurable_mk, hμν.ae_le hf.ae_eq_mk⟩
```

-- snippet 26
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.exists_disjoint_diff (L121-L126)
theorem exists_disjoint_diff (h : AEDisjoint μ s t) :
    ∃ u, MeasurableSet u ∧ μ u = 0 ∧ Disjoint (s \ u) t :=
⟨toMeasurable μ (s ∩ t), measurableSet_toMeasurable _ _, (measure_toMeasurable _).trans h,
    disjoint_sdiff_self_left.mono_left (b := s \ t) fun x hx => by
      simpa using ⟨hx.1, fun hxt => hx.2 <| subset_toMeasurable _ _ ⟨hx.1, hxt⟩⟩⟩
```

-- snippet 27
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.iUnion_right_iff (L89-L92)
theorem iUnion_right_iff {ι : Sort*} [Countable ι] {t : ι → Set α} :
    AEDisjoint μ s (⋃ i, t i) ↔ ∀ i, AEDisjoint μ s (t i) := by
simp only [AEDisjoint, inter_iUnion, measure_iUnion_null_iff]
```

-- snippet 28
```lean
-- Mathlib.MeasureTheory.Measure.AEMeasurable.aemeasurable_id'' (L41-L44)
theorem aemeasurable_id'' (μ : Measure α) {m : MeasurableSpace α} (hm : m ≤ m0) :
    @AEMeasurable α α m m0 id μ :=
@Measurable.aemeasurable α α m0 m id μ (measurable_id'' hm)
```

-- snippet 29
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.exists_null_pairwise_disjoint_diff (L35-L53)
theorem exists_null_pairwise_disjoint_diff [Countable ι] {s : ι → Set α}
    (hd : Pairwise (AEDisjoint μ on s)) : ∃ t : ι → Set α, (∀ i, MeasurableSet (t i)) ∧
    (∀ i, μ (t i) = 0) ∧ Pairwise (Disjoint on fun i => s i \ t i) := by
refine ⟨fun i => toMeasurable μ (s i ∩ ⋃ j ∈ ({i}ᶜ : Set ι), s j), fun i =>
    measurableSet_toMeasurable _ _, fun i => ?_, ?_⟩
  · simp only [measure_toMeasurable, inter_iUnion]
    exact (measure_biUnion_null_iff <| to_countable _).2 fun j hj => hd (Ne.symm hj)
  · simp only [Pairwise, disjoint_left, onFun, mem_diff, not_and, and_imp, Classical.not_not]
    intro i j hne x hi hU hj
    replace hU : x ∉ s i ∩ iUnion fun j ↦ iUnion fun _ ↦ s j :=
      fun h ↦ hU (subset_toMeasurable _ _ h)
    simp only [mem_inter_iff, mem_iUnion, not_and, not_exists] at hU
    exact (hU hi j hne.symm hj).elim

namespace AEDisjoint

protected theorem eq (h : AEDisjoint μ s t) : μ (s ∩ t) = 0 :=
  h
```

-- snippet 30
```lean
-- Mathlib.MeasureTheory.Measure.AEMeasurable.aemeasurable_of_subsingleton_codomain (L33-L35)
theorem aemeasurable_of_subsingleton_codomain [Subsingleton β] : AEMeasurable f μ :=
(measurable_of_subsingleton_codomain f).aemeasurable
```

-- snippet 31
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.measure_diff_right (L116-L120)
theorem measure_diff_right (h : AEDisjoint μ s t) : μ (t \ s) = μ t :=
measure_congr <| AEDisjoint.diff_ae_eq_right h

/-- If `s` and `t` are `μ`-a.e. disjoint, then `s \ u` and `t` are disjoint for some measurable null
set `u`. -/
```

-- snippet 32
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.diff_ae_eq_right (L110-L112)
theorem diff_ae_eq_right (h : AEDisjoint μ s t) : (t \ s : Set α) =ᵐ[μ] t :=
diff_ae_eq_left <| AEDisjoint.symm h
```

-- snippet 33
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.union_left_iff (L94-L96)
theorem union_left_iff : AEDisjoint μ (s ∪ t) u ↔ AEDisjoint μ s u ∧ AEDisjoint μ t u := by
simp [union_eq_iUnion, and_comm]
```

-- snippet 34
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.iUnion_left_iff (L84-L87)
theorem iUnion_left_iff {ι : Sort*} [Countable ι] {s : ι → Set α} :
    AEDisjoint μ (⋃ i, s i) t ↔ ∀ i, AEDisjoint μ (s i) t := by
simp only [AEDisjoint, iUnion_inter, measure_iUnion_null_iff]
```

-- snippet 35
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.union_right (L104-L106)
theorem union_right (ht : AEDisjoint μ s t) (hu : AEDisjoint μ s u) : AEDisjoint μ s (t ∪ u) :=
union_right_iff.2 ⟨ht, hu⟩
```

-- snippet 36
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.diff_ae_eq_left (L107-L109)
theorem diff_ae_eq_left (h : AEDisjoint μ s t) : (s \ t : Set α) =ᵐ[μ] s :=
@diff_self_inter _ s t ▸ diff_null_ae_eq_self h
```

-- snippet 37
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.aedisjoint_compl_right (L138-L141)
theorem aedisjoint_compl_right : AEDisjoint μ s sᶜ :=
(@disjoint_compl_right _ _ s).aedisjoint

end MeasureTheory
```

-- snippet 38
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.aedisjoint_compl_left (L135-L137)
theorem aedisjoint_compl_left : AEDisjoint μ sᶜ s :=
(@disjoint_compl_left _ _ s).aedisjoint
```

-- snippet 39
```lean
-- Mathlib.MeasureTheory.Measure.AEMeasurable.aemeasurable_zero_measure (L37-L40)
theorem aemeasurable_zero_measure : AEMeasurable f (0 : Measure α) := by
nontriviality α; inhabit α
  exact ⟨fun _ => f default, measurable_const, rfl⟩
```

-- snippet 40
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.mono_ae (L73-L82)
theorem mono_ae (h : AEDisjoint μ s t) (hu : u ≤ᵐ[μ] s) (hv : v ≤ᵐ[μ] t) : AEDisjoint μ u v :=
measure_mono_null_ae (hu.inter hv) h

protected theorem mono (h : AEDisjoint μ s t) (hu : u ⊆ s) (hv : v ⊆ t) : AEDisjoint μ u v :=
  mono_ae h (HasSubset.Subset.eventuallyLE hu) (HasSubset.Subset.eventuallyLE hv)

protected theorem congr (h : AEDisjoint μ s t) (hu : u =ᵐ[μ] s) (hv : v =ᵐ[μ] t) :
    AEDisjoint μ u v :=
  mono_ae h (Filter.EventuallyEq.le hu) (Filter.EventuallyEq.le hv)
```

<s>[SYSTEM_PROMPT]You are Leanstral, a Lean 4 proof assistant. Complete the task below using the provided Lean source context. The target mathlib commit is 5e932f97dd25535344f80f9dd8da3aab83df0fe6 (module Mathlib.MeasureTheory.Measure.MeasureSpace).[/SYSTEM_PROMPT][MODEL_SETTINGS]{"reasoning_effort": "none"}[/MODEL_SETTINGS][INST]Prove the following theorem. The retrieved context below may help.

```lean
theorem Ioc_ae_eq_Icc' (ha : μ {a} = 0) : Ioc a b =ᵐ[μ] Icc a b
```

## Retrieved context
-- snippet 1
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.ae_isMeasurablyGenerated (L102-L107)
instance ae_isMeasurablyGenerated : IsMeasurablyGenerated (ae μ) :=
⟨fun _s hs =>
    let ⟨t, hst, htm, htμ⟩ := exists_measurable_superset_of_null hs
    ⟨tᶜ, compl_mem_ae_iff.2 htμ, htm.compl, compl_subset_comm.1 hst⟩⟩

/-- See also `MeasureTheory.ae_restrict_uIoc_iff`. -/
```

-- snippet 2
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.ae_uIoc_iff (L108-L111)
theorem ae_uIoc_iff [LinearOrder α] {a b : α} {P : α → Prop} :
    (∀ᵐ x ∂μ, x ∈ Ι a b → P x) ↔ (∀ᵐ x ∂μ, x ∈ Ioc a b → P x) ∧ ∀ᵐ x ∂μ, x ∈ Ioc b a → P x := by
simp only [uIoc_eq_union, mem_union, or_imp, eventually_and]
```

-- snippet 3
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_union (L112-L114)
theorem measure_union (hd : Disjoint s₁ s₂) (h : MeasurableSet s₂) : μ (s₁ ∪ s₂) = μ s₁ + μ s₂ :=
measure_union₀ h.nullMeasurableSet hd.aedisjoint
```

-- snippet 4
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_union' (L115-L117)
theorem measure_union' (hd : Disjoint s₁ s₂) (h : MeasurableSet s₁) : μ (s₁ ∪ s₂) = μ s₁ + μ s₂ :=
measure_union₀' h.nullMeasurableSet hd.aedisjoint
```

-- snippet 5
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_inter_add_diff (L118-L120)
theorem measure_inter_add_diff (s : Set α) (ht : MeasurableSet t) : μ (s ∩ t) + μ (s \ t) = μ s :=
measure_inter_add_diff₀ _ ht.nullMeasurableSet
```

-- snippet 6
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_diff_add_inter (L121-L123)
theorem measure_diff_add_inter (s : Set α) (ht : MeasurableSet t) : μ (s \ t) + μ (s ∩ t) = μ s :=
(add_comm _ _).trans (measure_inter_add_diff s ht)
```

-- snippet 7
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_diff_eq_top (L124-L128)
theorem measure_diff_eq_top (hs : μ s = ∞) (ht : μ t ≠ ∞) : μ (s \ t) = ∞ := by
contrapose! hs
  exact ((measure_mono (subset_diff_union s t)).trans_lt
    ((measure_union_le _ _).trans_lt (ENNReal.add_lt_top.2 ⟨hs.lt_top, ht.lt_top⟩))).ne
```

-- snippet 8
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_union_add_inter (L129-L134)
theorem measure_union_add_inter (s : Set α) (ht : MeasurableSet t) :
    μ (s ∪ t) + μ (s ∩ t) = μ s + μ t := by
rw [← measure_inter_add_diff (s ∪ t) ht, Set.union_inter_cancel_right, union_diff_right, ←
    measure_inter_add_diff s ht]
  ac_rfl
```

-- snippet 9
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_union_add_inter' (L135-L138)
theorem measure_union_add_inter' (hs : MeasurableSet s) (t : Set α) :
    μ (s ∪ t) + μ (s ∩ t) = μ s + μ t := by
rw [union_comm, inter_comm, measure_union_add_inter t hs, add_comm]
```

-- snippet 10
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_symmDiff_eq (L139-L143)
lemma measure_symmDiff_eq (hs : NullMeasurableSet s μ) (ht : NullMeasurableSet t μ) :
    μ (s ∆ t) = μ (s \ t) + μ (t \ s) := by
simpa only [symmDiff_def, sup_eq_union]
    using measure_union₀ (ht.diff hs) disjoint_sdiff_sdiff.aedisjoint
```

-- snippet 11
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_symmDiff_le (L144-L147)
lemma measure_symmDiff_le (s t u : Set α) :
    μ (s ∆ u) ≤ μ (s ∆ t) + μ (t ∆ u) :=
le_trans (μ.mono <| symmDiff_triangle s t u) (measure_union_le (s ∆ t) (t ∆ u))
```

-- snippet 12
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_symmDiff_eq_top (L148-L150)
theorem measure_symmDiff_eq_top (hs : μ s ≠ ∞) (ht : μ t = ∞) : μ (s ∆ t) = ∞ :=
measure_mono_top subset_union_right (measure_diff_eq_top ht hs)
```

-- snippet 13
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_add_measure_compl (L151-L153)
theorem measure_add_measure_compl (h : MeasurableSet s) : μ s + μ sᶜ = μ univ :=
measure_add_measure_compl₀ h.nullMeasurableSet
```

-- snippet 14
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_biUnion₀ (L154-L160)
theorem measure_biUnion₀ {s : Set β} {f : β → Set α} (hs : s.Countable)
    (hd : s.Pairwise (AEDisjoint μ on f)) (h : ∀ b ∈ s, NullMeasurableSet (f b) μ) :
    μ (⋃ b ∈ s, f b) = ∑' p : s, μ (f p) := by
haveI := hs.toEncodable
  rw [biUnion_eq_iUnion]
  exact measure_iUnion₀ (hd.on_injective Subtype.coe_injective fun x => x.2) fun x => h x x.2
```

-- snippet 15
```lean
-- Mathlib.MeasureTheory.Measure.MeasureSpace.measure_biUnion (L161-L164)
theorem measure_biUnion {s : Set β} {f : β → Set α} (hs : s.Countable) (hd : s.PairwiseDisjoint f)
    (h : ∀ b ∈ s, MeasurableSet (f b)) : μ (⋃ b ∈ s, f b) = ∑' p : s, μ (f p) :=
measure_biUnion₀ hs hd.aedisjoint fun b hb => (h b hb).nullMeasurableSet
```

-- snippet 16
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.union_left (L101-L103)
theorem union_left (hs : AEDisjoint μ s u) (ht : AEDisjoint μ t u) : AEDisjoint μ (s ∪ t) u :=
union_left_iff.mpr ⟨hs, ht⟩
```

-- snippet 17
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.of_null_left (L130-L134)
theorem of_null_left (h : μ s = 0) : AEDisjoint μ s t :=
AEDisjoint.symm (of_null_right h)

end AEDisjoint
```

-- snippet 18
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.AEDisjoint (L28-L34)
def AEDisjoint (s t : Set α) :=
μ (s ∩ t) = 0

variable {μ} {s t u v : Set α}

/-- If `s : ι → Set α` is a countable family of pairwise a.e. disjoint sets, then there exists a
family of measurable null sets `t i` such that `s i \ t i` are pairwise disjoint. -/
```

-- snippet 19
```lean
-- Mathlib.MeasureTheory.Measure.AEMeasurable.mono_measure (L56-L58)
theorem mono_measure (h : AEMeasurable f μ) (h' : ν ≤ μ) : AEMeasurable f ν :=
mono_ac h h'.absolutelyContinuous
```

-- snippet 20
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.of_null_right (L127-L129)
theorem of_null_right (h : μ t = 0) : AEDisjoint μ s t :=
measure_mono_null inter_subset_right h
```

-- snippet 21
```lean
-- Mathlib.MeasureTheory.Measure.AEMeasurable.Subsingleton (L29-L31)
theorem Subsingleton.aemeasurable [Subsingleton α] : AEMeasurable f μ :=
Subsingleton.measurable.aemeasurable
```

-- snippet 22
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.measure_diff_left (L113-L115)
theorem measure_diff_left (h : AEDisjoint μ s t) : μ (s \ t) = μ s :=
measure_congr <| AEDisjoint.diff_ae_eq_left h
```

-- snippet 23
```lean
-- Mathlib.MeasureTheory.Measure.AEMeasurable.aemeasurable_of_map_neZero (L45-L52)
lemma aemeasurable_of_map_neZero {μ : Measure α}
    {f : α → β} (h : NeZero (μ.map f)) :
    AEMeasurable f μ := by
by_contra h'
  simp [h'] at h

namespace AEMeasurable
```

-- snippet 24
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.union_right_iff (L98-L100)
theorem union_right_iff : AEDisjoint μ s (t ∪ u) ↔ AEDisjoint μ s t ∧ AEDisjoint μ s u := by
simp [union_eq_iUnion, and_comm]
```

-- snippet 25
```lean
-- Mathlib.MeasureTheory.Measure.AEMeasurable.mono_ac (L53-L55)
lemma mono_ac (hf : AEMeasurable f ν) (hμν : μ ≪ ν) : AEMeasurable f μ :=
⟨hf.mk f, hf.measurable_mk, hμν.ae_le hf.ae_eq_mk⟩
```

-- snippet 26
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.exists_disjoint_diff (L121-L126)
theorem exists_disjoint_diff (h : AEDisjoint μ s t) :
    ∃ u, MeasurableSet u ∧ μ u = 0 ∧ Disjoint (s \ u) t :=
⟨toMeasurable μ (s ∩ t), measurableSet_toMeasurable _ _, (measure_toMeasurable _).trans h,
    disjoint_sdiff_self_left.mono_left (b := s \ t) fun x hx => by
      simpa using ⟨hx.1, fun hxt => hx.2 <| subset_toMeasurable _ _ ⟨hx.1, hxt⟩⟩⟩
```

-- snippet 27
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.iUnion_right_iff (L89-L92)
theorem iUnion_right_iff {ι : Sort*} [Countable ι] {t : ι → Set α} :
    AEDisjoint μ s (⋃ i, t i) ↔ ∀ i, AEDisjoint μ s (t i) := by
simp only [AEDisjoint, inter_iUnion, measure_iUnion_null_iff]
```

-- snippet 28
```lean
-- Mathlib.MeasureTheory.Measure.AEMeasurable.aemeasurable_id'' (L41-L44)
theorem aemeasurable_id'' (μ : Measure α) {m : MeasurableSpace α} (hm : m ≤ m0) :
    @AEMeasurable α α m m0 id μ :=
@Measurable.aemeasurable α α m0 m id μ (measurable_id'' hm)
```

-- snippet 29
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.exists_null_pairwise_disjoint_diff (L35-L53)
theorem exists_null_pairwise_disjoint_diff [Countable ι] {s : ι → Set α}
    (hd : Pairwise (AEDisjoint μ on s)) : ∃ t : ι → Set α, (∀ i, MeasurableSet (t i)) ∧
    (∀ i, μ (t i) = 0) ∧ Pairwise (Disjoint on fun i => s i \ t i) := by
refine ⟨fun i => toMeasurable μ (s i ∩ ⋃ j ∈ ({i}ᶜ : Set ι), s j), fun i =>
    measurableSet_toMeasurable _ _, fun i => ?_, ?_⟩
  · simp only [measure_toMeasurable, inter_iUnion]
    exact (measure_biUnion_null_iff <| to_countable _).2 fun j hj => hd (Ne.symm hj)
  · simp only [Pairwise, disjoint_left, onFun, mem_diff, not_and, and_imp, Classical.not_not]
    intro i j hne x hi hU hj
    replace hU : x ∉ s i ∩ iUnion fun j ↦ iUnion fun _ ↦ s j :=
      fun h ↦ hU (subset_toMeasurable _ _ h)
    simp only [mem_inter_iff, mem_iUnion, not_and, not_exists] at hU
    exact (hU hi j hne.symm hj).elim

namespace AEDisjoint

protected theorem eq (h : AEDisjoint μ s t) : μ (s ∩ t) = 0 :=
  h
```

-- snippet 30
```lean
-- Mathlib.MeasureTheory.Measure.AEMeasurable.aemeasurable_of_subsingleton_codomain (L33-L35)
theorem aemeasurable_of_subsingleton_codomain [Subsingleton β] : AEMeasurable f μ :=
(measurable_of_subsingleton_codomain f).aemeasurable
```

-- snippet 31
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.measure_diff_right (L116-L120)
theorem measure_diff_right (h : AEDisjoint μ s t) : μ (t \ s) = μ t :=
measure_congr <| AEDisjoint.diff_ae_eq_right h

/-- If `s` and `t` are `μ`-a.e. disjoint, then `s \ u` and `t` are disjoint for some measurable null
set `u`. -/
```

-- snippet 32
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.diff_ae_eq_right (L110-L112)
theorem diff_ae_eq_right (h : AEDisjoint μ s t) : (t \ s : Set α) =ᵐ[μ] t :=
diff_ae_eq_left <| AEDisjoint.symm h
```

-- snippet 33
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.union_left_iff (L94-L96)
theorem union_left_iff : AEDisjoint μ (s ∪ t) u ↔ AEDisjoint μ s u ∧ AEDisjoint μ t u := by
simp [union_eq_iUnion, and_comm]
```

-- snippet 34
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.iUnion_left_iff (L84-L87)
theorem iUnion_left_iff {ι : Sort*} [Countable ι] {s : ι → Set α} :
    AEDisjoint μ (⋃ i, s i) t ↔ ∀ i, AEDisjoint μ (s i) t := by
simp only [AEDisjoint, iUnion_inter, measure_iUnion_null_iff]
```

-- snippet 35
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.union_right (L104-L106)
theorem union_right (ht : AEDisjoint μ s t) (hu : AEDisjoint μ s u) : AEDisjoint μ s (t ∪ u) :=
union_right_iff.2 ⟨ht, hu⟩
```

-- snippet 36
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.diff_ae_eq_left (L107-L109)
theorem diff_ae_eq_left (h : AEDisjoint μ s t) : (s \ t : Set α) =ᵐ[μ] s :=
@diff_self_inter _ s t ▸ diff_null_ae_eq_self h
```

-- snippet 37
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.aedisjoint_compl_right (L138-L141)
theorem aedisjoint_compl_right : AEDisjoint μ s sᶜ :=
(@disjoint_compl_right _ _ s).aedisjoint

end MeasureTheory
```

-- snippet 38
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.aedisjoint_compl_left (L135-L137)
theorem aedisjoint_compl_left : AEDisjoint μ sᶜ s :=
(@disjoint_compl_left _ _ s).aedisjoint
```

-- snippet 39
```lean
-- Mathlib.MeasureTheory.Measure.AEMeasurable.aemeasurable_zero_measure (L37-L40)
theorem aemeasurable_zero_measure : AEMeasurable f (0 : Measure α) := by
nontriviality α; inhabit α
  exact ⟨fun _ => f default, measurable_const, rfl⟩
```

-- snippet 40
```lean
-- Mathlib.MeasureTheory.Measure.AEDisjoint.mono_ae (L73-L82)
theorem mono_ae (h : AEDisjoint μ s t) (hu : u ≤ᵐ[μ] s) (hv : v ≤ᵐ[μ] t) : AEDisjoint μ u v :=
measure_mono_null_ae (hu.inter hv) h

protected theorem mono (h : AEDisjoint μ s t) (hu : u ⊆ s) (hv : v ⊆ t) : AEDisjoint μ u v :=
  mono_ae h (HasSubset.Subset.eventuallyLE hu) (HasSubset.Subset.eventuallyLE hv)

protected theorem congr (h : AEDisjoint μ s t) (hu : u =ᵐ[μ] s) (hv : v =ᵐ[μ] t) :
    AEDisjoint μ u v :=
  mono_ae h (Filter.EventuallyEq.le hu) (Filter.EventuallyEq.le hv)
```
[/INST]