Theory Radon_Nikodym

theory Radon_Nikodym
imports Bochner_Integration
(*  Title:      HOL/Probability/Radon_Nikodym.thy
    Author:     Johannes Hölzl, TU München
*)

section ‹Radon-Nikod{\'y}m derivative›

theory Radon_Nikodym
imports Bochner_Integration
begin

definition "diff_measure M N =
  measure_of (space M) (sets M) (λA. emeasure M A - emeasure N A)"

lemma
  shows space_diff_measure[simp]: "space (diff_measure M N) = space M"
    and sets_diff_measure[simp]: "sets (diff_measure M N) = sets M"
  by (auto simp: diff_measure_def)

lemma emeasure_diff_measure:
  assumes fin: "finite_measure M" "finite_measure N" and sets_eq: "sets M = sets N"
  assumes pos: "⋀A. A ∈ sets M ⟹ emeasure N A ≤ emeasure M A" and A: "A ∈ sets M"
  shows "emeasure (diff_measure M N) A = emeasure M A - emeasure N A" (is "_ = ?μ A")
  unfolding diff_measure_def
proof (rule emeasure_measure_of_sigma)
  show "sigma_algebra (space M) (sets M)" ..
  show "positive (sets M) ?μ"
    using pos by (simp add: positive_def)
  show "countably_additive (sets M) ?μ"
  proof (rule countably_additiveI)
    fix A :: "nat ⇒ _"  assume A: "range A ⊆ sets M" and "disjoint_family A"
    then have suminf:
      "(∑i. emeasure M (A i)) = emeasure M (⋃i. A i)"
      "(∑i. emeasure N (A i)) = emeasure N (⋃i. A i)"
      by (simp_all add: suminf_emeasure sets_eq)
    with A have "(∑i. emeasure M (A i) - emeasure N (A i)) =
      (∑i. emeasure M (A i)) - (∑i. emeasure N (A i))"
      using fin pos[of "A _"]
      by (intro ennreal_suminf_minus)
         (auto simp: sets_eq finite_measure.emeasure_eq_measure suminf_emeasure measure_nonneg)
    then show "(∑i. emeasure M (A i) - emeasure N (A i)) =
      emeasure M (⋃i. A i) - emeasure N (⋃i. A i) "
      by (simp add: suminf)
  qed
qed fact

lemma (in sigma_finite_measure) Ex_finite_integrable_function:
  "∃h∈borel_measurable M. integralN M h ≠ ∞ ∧ (∀x∈space M. 0 < h x ∧ h x < ∞)"
proof -
  obtain A :: "nat ⇒ 'a set" where
    range[measurable]: "range A ⊆ sets M" and
    space: "(⋃i. A i) = space M" and
    measure: "⋀i. emeasure M (A i) ≠ ∞" and
    disjoint: "disjoint_family A"
    using sigma_finite_disjoint by blast
  let ?B = "λi. 2^Suc i * emeasure M (A i)"
  have "∀i. ∃x. 0 < x ∧ x < inverse (?B i)"
  proof
    fix i show "∃x. 0 < x ∧ x < inverse (?B i)"
      using measure[of i]
      by (auto intro!: dense simp: ennreal_inverse_positive ennreal_mult_eq_top_iff power_eq_top_ennreal)
  qed
  from choice[OF this] obtain n where n: "⋀i. 0 < n i"
    "⋀i. n i < inverse (2^Suc i * emeasure M (A i))" by auto
  { fix i have "0 ≤ n i" using n(1)[of i] by auto } note pos = this
  let ?h = "λx. ∑i. n i * indicator (A i) x"
  show ?thesis
  proof (safe intro!: bexI[of _ ?h] del: notI)
    have "⋀i. A i ∈ sets M"
      using range by fastforce+
    then have "integralN M ?h = (∑i. n i * emeasure M (A i))" using pos
      by (simp add: nn_integral_suminf nn_integral_cmult_indicator)
    also have "… ≤ (∑i. ennreal ((1/2)^Suc i))"
    proof (intro suminf_le allI)
      fix N
      have "n N * emeasure M (A N) ≤ inverse (2^Suc N * emeasure M (A N)) * emeasure M (A N)"
        using n[of N] by (intro mult_right_mono) auto
      also have "… = (1/2)^Suc N * (inverse (emeasure M (A N)) * emeasure M (A N))"
        using measure[of N]
        by (simp add: ennreal_inverse_power divide_ennreal_def ennreal_inverse_mult
                      power_eq_top_ennreal less_top[symmetric] mult_ac
                 del: power_Suc)
      also have "… ≤ inverse (ennreal 2) ^ Suc N"
        using measure[of N]
        apply (cases "emeasure M (A N)" rule: ennreal_cases)
        apply (cases "emeasure M (A N) = 0")
        apply (auto simp: inverse_ennreal ennreal_mult[symmetric] divide_ennreal_def simp del: power_Suc)
        done
      also have "… = ennreal (inverse 2 ^ Suc N)"
        by (subst ennreal_power[symmetric], simp) (simp add: inverse_ennreal)
      finally show "n N * emeasure M (A N) ≤ ennreal ((1/2)^Suc N)"
        by simp
    qed auto
    also have "… < top"
      unfolding less_top[symmetric]
      apply (rule ennreal_suminf_neq_top)
      apply (subst summable_Suc_iff)
      apply (subst summable_geometric)
      apply auto
      done
    finally show "integralN M ?h ≠ ∞"
      by (auto simp: top_unique)
  next
    { fix x assume "x ∈ space M"
      then obtain i where "x ∈ A i" using space[symmetric] by auto
      with disjoint n have "?h x = n i"
        by (auto intro!: suminf_cmult_indicator intro: less_imp_le)
      then show "0 < ?h x" and "?h x < ∞" using n[of i] by (auto simp: less_top[symmetric]) }
    note pos = this
  qed measurable
qed

subsection "Absolutely continuous"

definition absolutely_continuous :: "'a measure ⇒ 'a measure ⇒ bool" where
  "absolutely_continuous M N ⟷ null_sets M ⊆ null_sets N"

lemma absolutely_continuousI_count_space: "absolutely_continuous (count_space A) M"
  unfolding absolutely_continuous_def by (auto simp: null_sets_count_space)

lemma absolutely_continuousI_density:
  "f ∈ borel_measurable M ⟹ absolutely_continuous M (density M f)"
  by (force simp add: absolutely_continuous_def null_sets_density_iff dest: AE_not_in)

lemma absolutely_continuousI_point_measure_finite:
  "(⋀x. ⟦ x ∈ A ; f x ≤ 0 ⟧ ⟹ g x ≤ 0) ⟹ absolutely_continuous (point_measure A f) (point_measure A g)"
  unfolding absolutely_continuous_def by (force simp: null_sets_point_measure_iff)

lemma absolutely_continuous_AE:
  assumes sets_eq: "sets M' = sets M"
    and "absolutely_continuous M M'" "AE x in M. P x"
   shows "AE x in M'. P x"
proof -
  from ‹AE x in M. P x› obtain N where N: "N ∈ null_sets M" "{x∈space M. ¬ P x} ⊆ N"
    unfolding eventually_ae_filter by auto
  show "AE x in M'. P x"
  proof (rule AE_I')
    show "{x∈space M'. ¬ P x} ⊆ N" using sets_eq_imp_space_eq[OF sets_eq] N(2) by simp
    from ‹absolutely_continuous M M'› show "N ∈ null_sets M'"
      using N unfolding absolutely_continuous_def sets_eq null_sets_def by auto
  qed
qed

subsection "Existence of the Radon-Nikodym derivative"

lemma (in finite_measure) Radon_Nikodym_aux_epsilon:
  fixes e :: real assumes "0 < e"
  assumes "finite_measure N" and sets_eq: "sets N = sets M"
  shows "∃A∈sets M. measure M (space M) - measure N (space M) ≤ measure M A - measure N A ∧
                    (∀B∈sets M. B ⊆ A ⟶ - e < measure M B - measure N B)"
proof -
  interpret M': finite_measure N by fact
  let ?d = "λA. measure M A - measure N A"
  let ?A = "λA. if (∀B∈sets M. B ⊆ space M - A ⟶ -e < ?d B)
    then {}
    else (SOME B. B ∈ sets M ∧ B ⊆ space M - A ∧ ?d B ≤ -e)"
  def A  "λn. ((λB. B ∪ ?A B) ^^ n) {}"
  have A_simps[simp]:
    "A 0 = {}"
    "⋀n. A (Suc n) = (A n ∪ ?A (A n))" unfolding A_def by simp_all
  { fix A assume "A ∈ sets M"
    have "?A A ∈ sets M"
      by (auto intro!: someI2[of _ _ "λA. A ∈ sets M"] simp: not_less) }
  note A'_in_sets = this
  { fix n have "A n ∈ sets M"
    proof (induct n)
      case (Suc n) thus "A (Suc n) ∈ sets M"
        using A'_in_sets[of "A n"] by (auto split: if_split_asm)
    qed (simp add: A_def) }
  note A_in_sets = this
  hence "range A ⊆ sets M" by auto
  { fix n B
    assume Ex: "∃B. B ∈ sets M ∧ B ⊆ space M - A n ∧ ?d B ≤ -e"
    hence False: "¬ (∀B∈sets M. B ⊆ space M - A n ⟶ -e < ?d B)" by (auto simp: not_less)
    have "?d (A (Suc n)) ≤ ?d (A n) - e" unfolding A_simps if_not_P[OF False]
    proof (rule someI2_ex[OF Ex])
      fix B assume "B ∈ sets M ∧ B ⊆ space M - A n ∧ ?d B ≤ - e"
      hence "A n ∩ B = {}" "B ∈ sets M" and dB: "?d B ≤ -e" by auto
      hence "?d (A n ∪ B) = ?d (A n) + ?d B"
        using ‹A n ∈ sets M› finite_measure_Union M'.finite_measure_Union by (simp add: sets_eq)
      also have "… ≤ ?d (A n) - e" using dB by simp
      finally show "?d (A n ∪ B) ≤ ?d (A n) - e" .
    qed }
  note dA_epsilon = this
  { fix n have "?d (A (Suc n)) ≤ ?d (A n)"
    proof (cases "∃B. B∈sets M ∧ B ⊆ space M - A n ∧ ?d B ≤ - e")
      case True from dA_epsilon[OF this] show ?thesis using ‹0 < e› by simp
    next
      case False
      hence "∀B∈sets M. B ⊆ space M - A n ⟶ -e < ?d B" by (auto simp: not_le)
      thus ?thesis by simp
    qed }
  note dA_mono = this
  show ?thesis
  proof (cases "∃n. ∀B∈sets M. B ⊆ space M - A n ⟶ -e < ?d B")
    case True then obtain n where B: "⋀B. ⟦ B ∈ sets M; B ⊆ space M - A n⟧ ⟹ -e < ?d B" by blast
    show ?thesis
    proof (safe intro!: bexI[of _ "space M - A n"])
      fix B assume "B ∈ sets M" "B ⊆ space M - A n"
      from B[OF this] show "-e < ?d B" .
    next
      show "space M - A n ∈ sets M" by (rule sets.compl_sets) fact
    next
      show "?d (space M) ≤ ?d (space M - A n)"
      proof (induct n)
        fix n assume "?d (space M) ≤ ?d (space M - A n)"
        also have "… ≤ ?d (space M - A (Suc n))"
          using A_in_sets sets.sets_into_space dA_mono[of n] finite_measure_compl M'.finite_measure_compl
          by (simp del: A_simps add: sets_eq sets_eq_imp_space_eq[OF sets_eq])
        finally show "?d (space M) ≤ ?d (space M - A (Suc n))" .
      qed simp
    qed
  next
    case False hence B: "⋀n. ∃B. B∈sets M ∧ B ⊆ space M - A n ∧ ?d B ≤ - e"
      by (auto simp add: not_less)
    { fix n have "?d (A n) ≤ - real n * e"
      proof (induct n)
        case (Suc n) with dA_epsilon[of n, OF B] show ?case by (simp del: A_simps add: of_nat_Suc field_simps)
      next
        case 0 with measure_empty show ?case by (simp add: zero_ennreal_def)
      qed } note dA_less = this
    have decseq: "decseq (λn. ?d (A n))" unfolding decseq_eq_incseq
    proof (rule incseq_SucI)
      fix n show "- ?d (A n) ≤ - ?d (A (Suc n))" using dA_mono[of n] by auto
    qed
    have A: "incseq A" by (auto intro!: incseq_SucI)
    from finite_Lim_measure_incseq[OF _ A] ‹range A ⊆ sets M›
      M'.finite_Lim_measure_incseq[OF _ A]
    have convergent: "(λi. ?d (A i)) ⇢ ?d (⋃i. A i)"
      by (auto intro!: tendsto_diff simp: sets_eq)
    obtain n :: nat where "- ?d (⋃i. A i) / e < real n" using reals_Archimedean2 by auto
    moreover from order_trans[OF decseq_le[OF decseq convergent] dA_less]
    have "real n ≤ - ?d (⋃i. A i) / e" using ‹0<e› by (simp add: field_simps)
    ultimately show ?thesis by auto
  qed
qed

lemma (in finite_measure) Radon_Nikodym_aux:
  assumes "finite_measure N" and sets_eq: "sets N = sets M"
  shows "∃A∈sets M. measure M (space M) - measure N (space M) ≤
                    measure M A - measure N A ∧
                    (∀B∈sets M. B ⊆ A ⟶ 0 ≤ measure M B - measure N B)"
proof -
  interpret N: finite_measure N by fact
  let ?d = "λA. measure M A - measure N A"
  let ?P = "λA n. if n = 0 then A = space M else (∀C∈sets M. C ⊆ A ⟶ - 1 / real (Suc n) < ?d C)"
  let ?Q = "λA B. A ⊆ B ∧ ?d B ≤ ?d A"

  have "∃A. ∀n. (A n ∈ sets M ∧ ?P (A n) n) ∧ ?Q (A (Suc n)) (A n)"
  proof (rule dependent_nat_choice)
    show "∃A. A ∈ sets M ∧ ?P A 0"
      by auto
  next
    fix A n assume "A ∈ sets M ∧ ?P A n"
    then have A: "A ∈ sets M" by auto
    then have "finite_measure (density M (indicator A))" "0 < 1 / real (Suc (Suc n))"
         "finite_measure (density N (indicator A))" "sets (density N (indicator A)) = sets (density M (indicator A))"
      by (auto simp: finite_measure_restricted N.finite_measure_restricted sets_eq)
    from finite_measure.Radon_Nikodym_aux_epsilon[OF this] guess X .. note X = this
    with A have "A ∩ X ∈ sets M ∧ ?P (A ∩ X) (Suc n) ∧ ?Q (A ∩ X) A"
      by (simp add: measure_restricted sets_eq sets.Int) (metis inf_absorb2)
    then show "∃B. (B ∈ sets M ∧ ?P B (Suc n)) ∧ ?Q B A"
      by blast
  qed
  then obtain A where A: "⋀n. A n ∈ sets M" "⋀n. ?P (A n) n" "⋀n. ?Q (A (Suc n)) (A n)"
    by metis
  then have mono_dA: "mono (λi. ?d (A i))" and A_0[simp]: "A 0 = space M"
    by (auto simp add: mono_iff_le_Suc)
  show ?thesis
  proof (safe intro!: bexI[of _ "⋂i. A i"])
    show "(⋂i. A i) ∈ sets M" using ‹⋀n. A n ∈ sets M› by auto
    have "decseq A" using A by (auto intro!: decseq_SucI)
    from A(1) finite_Lim_measure_decseq[OF _ this] N.finite_Lim_measure_decseq[OF _ this]
    have "(λi. ?d (A i)) ⇢ ?d (⋂i. A i)" by (auto intro!: tendsto_diff simp: sets_eq)
    thus "?d (space M) ≤ ?d (⋂i. A i)" using mono_dA[THEN monoD, of 0 _]
      by (rule_tac LIMSEQ_le_const) auto
  next
    fix B assume B: "B ∈ sets M" "B ⊆ (⋂i. A i)"
    show "0 ≤ ?d B"
    proof (rule ccontr)
      assume "¬ 0 ≤ ?d B"
      hence "0 < - ?d B" by auto
      from reals_Archimedean[OF this]
      obtain n where *: "?d B < - 1 / real (Suc n)"
        by (auto simp: field_simps)
      also have "… ≤ - 1 / real (Suc (Suc n))"
        by (auto simp: field_simps)
      finally show False
        using * A(2)[of "Suc n"] B by (auto elim!: ballE[of _ _ B])
    qed
  qed
qed

lemma (in finite_measure) Radon_Nikodym_finite_measure:
  assumes "finite_measure N" and sets_eq: "sets N = sets M"
  assumes "absolutely_continuous M N"
  shows "∃f ∈ borel_measurable M. (∀x. 0 ≤ f x) ∧ density M f = N"
proof -
  interpret N: finite_measure N by fact
  def G  "{g ∈ borel_measurable M. (∀x. 0 ≤ g x) ∧ (∀A∈sets M. (∫+x. g x * indicator A x ∂M) ≤ N A)}"
  { fix f have "f ∈ G ⟹ f ∈ borel_measurable M" by (auto simp: G_def) }
  note this[measurable_dest]
  have "(λx. 0) ∈ G" unfolding G_def by auto
  hence "G ≠ {}" by auto
  { fix f g assume f: "f ∈ G" and g: "g ∈ G"
    have "(λx. max (g x) (f x)) ∈ G" (is "?max ∈ G") unfolding G_def
    proof safe
      show "?max ∈ borel_measurable M" using f g unfolding G_def by auto
      let ?A = "{x ∈ space M. f x ≤ g x}"
      have "?A ∈ sets M" using f g unfolding G_def by auto
      fix A assume "A ∈ sets M"
      hence sets: "?A ∩ A ∈ sets M" "(space M - ?A) ∩ A ∈ sets M" using ‹?A ∈ sets M› by auto
      hence sets': "?A ∩ A ∈ sets N" "(space M - ?A) ∩ A ∈ sets N" by (auto simp: sets_eq)
      have union: "((?A ∩ A) ∪ ((space M - ?A) ∩ A)) = A"
        using sets.sets_into_space[OF ‹A ∈ sets M›] by auto
      have "⋀x. x ∈ space M ⟹ max (g x) (f x) * indicator A x =
        g x * indicator (?A ∩ A) x + f x * indicator ((space M - ?A) ∩ A) x"
        by (auto simp: indicator_def max_def)
      hence "(∫+x. max (g x) (f x) * indicator A x ∂M) =
        (∫+x. g x * indicator (?A ∩ A) x ∂M) +
        (∫+x. f x * indicator ((space M - ?A) ∩ A) x ∂M)"
        using f g sets unfolding G_def
        by (auto cong: nn_integral_cong intro!: nn_integral_add)
      also have "… ≤ N (?A ∩ A) + N ((space M - ?A) ∩ A)"
        using f g sets unfolding G_def by (auto intro!: add_mono)
      also have "… = N A"
        using plus_emeasure[OF sets'] union by auto
      finally show "(∫+x. max (g x) (f x) * indicator A x ∂M) ≤ N A" .
    next
      fix x show "0 ≤ max (g x) (f x)" using f g by (auto simp: G_def split: split_max)
    qed }
  note max_in_G = this
  { fix f assume  "incseq f" and f: "⋀i. f i ∈ G"
    then have [measurable]: "⋀i. f i ∈ borel_measurable M" by (auto simp: G_def)
    have "(λx. SUP i. f i x) ∈ G" unfolding G_def
    proof safe
      show "(λx. SUP i. f i x) ∈ borel_measurable M" by measurable
      { fix x show "0 ≤ (SUP i. f i x)"
          using f by (auto simp: G_def intro: SUP_upper2) }
    next
      fix A assume "A ∈ sets M"
      have "(∫+x. (SUP i. f i x) * indicator A x ∂M) =
        (∫+x. (SUP i. f i x * indicator A x) ∂M)"
        by (intro nn_integral_cong) (simp split: split_indicator)
      also have "… = (SUP i. (∫+x. f i x * indicator A x ∂M))"
        using ‹incseq f› f ‹A ∈ sets M›
        by (intro nn_integral_monotone_convergence_SUP)
           (auto simp: G_def incseq_Suc_iff le_fun_def split: split_indicator)
      finally show "(∫+x. (SUP i. f i x) * indicator A x ∂M) ≤ N A"
        using f ‹A ∈ sets M› by (auto intro!: SUP_least simp: G_def)
    qed }
  note SUP_in_G = this
  let ?y = "SUP g : G. integralN M g"
  have y_le: "?y ≤ N (space M)" unfolding G_def
  proof (safe intro!: SUP_least)
    fix g assume "∀A∈sets M. (∫+x. g x * indicator A x ∂M) ≤ N A"
    from this[THEN bspec, OF sets.top] show "integralN M g ≤ N (space M)"
      by (simp cong: nn_integral_cong)
  qed
  from ennreal_SUP_countable_SUP [OF ‹G ≠ {}›, of "integralN M"] guess ys .. note ys = this
  then have "∀n. ∃g. g∈G ∧ integralN M g = ys n"
  proof safe
    fix n assume "range ys ⊆ integralN M ` G"
    hence "ys n ∈ integralN M ` G" by auto
    thus "∃g. g∈G ∧ integralN M g = ys n" by auto
  qed
  from choice[OF this] obtain gs where "⋀i. gs i ∈ G" "⋀n. integralN M (gs n) = ys n" by auto
  hence y_eq: "?y = (SUP i. integralN M (gs i))" using ys by auto
  let ?g = "λi x. Max ((λn. gs n x) ` {..i})"
  def f  "λx. SUP i. ?g i x"
  let ?F = "λA x. f x * indicator A x"
  have gs_not_empty: "⋀i x. (λn. gs n x) ` {..i} ≠ {}" by auto
  { fix i have "?g i ∈ G"
    proof (induct i)
      case 0 thus ?case by simp fact
    next
      case (Suc i)
      with Suc gs_not_empty ‹gs (Suc i) ∈ G› show ?case
        by (auto simp add: atMost_Suc intro!: max_in_G)
    qed }
  note g_in_G = this
  have "incseq ?g" using gs_not_empty
    by (auto intro!: incseq_SucI le_funI simp add: atMost_Suc)
  from SUP_in_G[OF this g_in_G] have [measurable]: "f ∈ G" unfolding f_def .
  then have [simp, intro]: "f ∈ borel_measurable M" unfolding G_def by auto
  have "integralN M f = (SUP i. integralN M (?g i))" unfolding f_def
    using g_in_G ‹incseq ?g›
    by (auto intro!: nn_integral_monotone_convergence_SUP simp: G_def)
  also have "… = ?y"
  proof (rule antisym)
    show "(SUP i. integralN M (?g i)) ≤ ?y"
      using g_in_G by (auto intro: SUP_mono)
    show "?y ≤ (SUP i. integralN M (?g i))" unfolding y_eq
      by (auto intro!: SUP_mono nn_integral_mono Max_ge)
  qed
  finally have int_f_eq_y: "integralN M f = ?y" .
  have "⋀x. 0 ≤ f x"
    unfolding f_def using ‹⋀i. gs i ∈ G›
    by (auto intro!: SUP_upper2 Max_ge_iff[THEN iffD2] simp: G_def)
  let ?t = "λA. N A - (∫+x. ?F A x ∂M)"
  let ?M = "diff_measure N (density M f)"
  have f_le_N: "⋀A. A ∈ sets M ⟹ (∫+x. ?F A x ∂M) ≤ N A"
    using ‹f ∈ G› unfolding G_def by auto
  have emeasure_M: "⋀A. A ∈ sets M ⟹ emeasure ?M A = ?t A"
  proof (subst emeasure_diff_measure)
    from f_le_N[of "space M"] show "finite_measure N" "finite_measure (density M f)"
      by (auto intro!: finite_measureI simp: emeasure_density top_unique cong: nn_integral_cong)
  next
    fix B assume "B ∈ sets N" with f_le_N[of B] show "emeasure (density M f) B ≤ emeasure N B"
      by (auto simp: sets_eq emeasure_density cong: nn_integral_cong)
  qed (auto simp: sets_eq emeasure_density)
  from emeasure_M[of "space M"] N.finite_emeasure_space
  interpret M': finite_measure ?M
    by (auto intro!: finite_measureI simp: sets_eq_imp_space_eq[OF sets_eq] N.emeasure_eq_measure )

  have ac: "absolutely_continuous M ?M" unfolding absolutely_continuous_def
  proof
    fix A assume A_M: "A ∈ null_sets M"
    with ‹absolutely_continuous M N› have A_N: "A ∈ null_sets N"
      unfolding absolutely_continuous_def by auto
    moreover from A_M A_N have "(∫+ x. ?F A x ∂M) ≤ N A" using ‹f ∈ G› by (auto simp: G_def)
    ultimately have "N A - (∫+ x. ?F A x ∂M) = 0"
      by (auto intro!: antisym)
    then show "A ∈ null_sets ?M"
      using A_M by (simp add: emeasure_M null_sets_def sets_eq)
  qed
  have upper_bound: "∀A∈sets M. ?M A ≤ 0"
  proof (rule ccontr)
    assume "¬ ?thesis"
    then obtain A where A: "A ∈ sets M" and pos: "0 < ?M A"
      by (auto simp: zero_less_iff_neq_zero)
    note pos
    also have "?M A ≤ ?M (space M)"
      using emeasure_space[of ?M A] by (simp add: sets_eq[THEN sets_eq_imp_space_eq])
    finally have pos_t: "0 < ?M (space M)" by simp
    moreover
    from pos_t have "emeasure M (space M) ≠ 0"
      using ac unfolding absolutely_continuous_def by (auto simp: null_sets_def)
    then have pos_M: "0 < emeasure M (space M)"
      by (simp add: zero_less_iff_neq_zero)
    moreover
    have "(∫+x. f x * indicator (space M) x ∂M) ≤ N (space M)"
      using ‹f ∈ G› unfolding G_def by auto
    hence "(∫+x. f x * indicator (space M) x ∂M) ≠ ∞"
      using M'.finite_emeasure_space by (auto simp: top_unique)
    moreover
    def b  "?M (space M) / emeasure M (space M) / 2"
    ultimately have b: "b ≠ 0 ∧ 0 ≤ b ∧ b ≠ ∞"
      by (auto simp: ennreal_divide_eq_top_iff)
    then have b: "b ≠ 0" "0 ≤ b" "0 < b"  "b ≠ ∞"
      by (auto simp: less_le)
    let ?Mb = "density M (λ_. b)"
    have Mb: "finite_measure ?Mb" "sets ?Mb = sets ?M"
        using b by (auto simp: emeasure_density_const sets_eq ennreal_mult_eq_top_iff intro!: finite_measureI)
    from M'.Radon_Nikodym_aux[OF this] guess A0 ..
    then have "A0 ∈ sets M"
      and space_le_A0: "measure ?M (space M) - enn2real b * measure M (space M) ≤ measure ?M A0 - enn2real b * measure M A0"
      and *: "⋀B. B ∈ sets M ⟹ B ⊆ A0 ⟹ 0 ≤ measure ?M B - enn2real b * measure M B"
      using b by (simp_all add: measure_density_const sets_eq_imp_space_eq[OF sets_eq] sets_eq)
    { fix B assume B: "B ∈ sets M" "B ⊆ A0"
      with *[OF this] have "b * emeasure M B ≤ ?M B"
        using b unfolding M'.emeasure_eq_measure emeasure_eq_measure
        by (cases b rule: ennreal_cases) (auto simp: ennreal_mult[symmetric] measure_nonneg) }
    note bM_le_t = this
    let ?f0 = "λx. f x + b * indicator A0 x"
    { fix A assume A: "A ∈ sets M"
      hence "A ∩ A0 ∈ sets M" using ‹A0 ∈ sets M› by auto
      have "(∫+x. ?f0 x  * indicator A x ∂M) =
        (∫+x. f x * indicator A x + b * indicator (A ∩ A0) x ∂M)"
        by (auto intro!: nn_integral_cong split: split_indicator)
      hence "(∫+x. ?f0 x * indicator A x ∂M) =
          (∫+x. f x * indicator A x ∂M) + b * emeasure M (A ∩ A0)"
        using ‹A0 ∈ sets M› ‹A ∩ A0 ∈ sets M› A b ‹f ∈ G›
        by (simp add: nn_integral_add nn_integral_cmult_indicator G_def) }
    note f0_eq = this
    { fix A assume A: "A ∈ sets M"
      hence "A ∩ A0 ∈ sets M" using ‹A0 ∈ sets M› by auto
      have f_le_v: "(∫+x. ?F A x ∂M) ≤ N A" using ‹f ∈ G› A unfolding G_def by auto
      note f0_eq[OF A]
      also have "(∫+x. ?F A x ∂M) + b * emeasure M (A ∩ A0) ≤ (∫+x. ?F A x ∂M) + ?M (A ∩ A0)"
        using bM_le_t[OF ‹A ∩ A0 ∈ sets M›] ‹A ∈ sets M› ‹A0 ∈ sets M›
        by (auto intro!: add_left_mono)
      also have "… ≤ (∫+x. f x * indicator A x ∂M) + ?M A"
        using emeasure_mono[of "A ∩ A0" A ?M] ‹A ∈ sets M› ‹A0 ∈ sets M›
        by (auto intro!: add_left_mono simp: sets_eq)
      also have "… ≤ N A"
        unfolding emeasure_M[OF ‹A ∈ sets M›]
        using f_le_v N.emeasure_eq_measure[of A]
        by (cases "∫+x. ?F A x ∂M" "N A" rule: ennreal2_cases)
           (auto simp: top_unique measure_nonneg ennreal_minus ennreal_plus[symmetric] simp del: ennreal_plus)
      finally have "(∫+x. ?f0 x * indicator A x ∂M) ≤ N A" . }
    hence "?f0 ∈ G" using ‹A0 ∈ sets M› b ‹f ∈ G› by (auto simp: G_def)
    have int_f_finite: "integralN M f ≠ ∞"
      by (metis top_unique infinity_ennreal_def int_f_eq_y y_le N.emeasure_finite)
    have pos: "0 < measure ?M (space M) - enn2real b * measure M (space M)"
      using pos_t pos_M
      by (simp add: M'.emeasure_eq_measure emeasure_eq_measure b_def divide_ennreal ennreal_divide_numeral)
    also have "… ≤ measure ?M A0 - enn2real b * measure M A0"
      by (rule space_le_A0)
    finally have "enn2real b * measure M A0 < measure ?M A0"
      by simp
    with b have "?M A0 ≠ 0"
      by (cases b rule: ennreal_cases)
         (auto simp: M'.emeasure_eq_measure measure_nonneg mult_less_0_iff not_le[symmetric])
    then have "emeasure M A0 ≠ 0"
      using ac ‹A0 ∈ sets M› by (auto simp: absolutely_continuous_def null_sets_def)
    then have "0 < emeasure M A0"
      by (auto simp: zero_less_iff_neq_zero)
    hence "0 < b * emeasure M A0"
      using b by (auto simp: ennreal_zero_less_mult_iff)
    with int_f_finite have "?y < integralN M f + b * emeasure M A0"
      unfolding int_f_eq_y by auto
    also have "… = integralN M ?f0"
      using f0_eq[OF sets.top] ‹A0 ∈ sets M› sets.sets_into_space by (simp cong: nn_integral_cong)
    finally have "?y < integralN M ?f0"
      by simp
    moreover have "integralN M ?f0 ≤ ?y"
      using ‹?f0 ∈ G› by (auto intro!: SUP_upper)
    ultimately show False by auto
  qed
  show ?thesis
  proof (intro bexI[of _ f] measure_eqI conjI)
    show "sets (density M f) = sets N"
      by (simp add: sets_eq)
    fix A assume A: "A∈sets (density M f)"
    then show "emeasure (density M f) A = emeasure N A"
      using ‹f ∈ G› A upper_bound[THEN bspec, of A] N.emeasure_eq_measure[of A]
      by (cases "integralN M (?F A)")
         (auto intro!: antisym simp: emeasure_density G_def emeasure_M ennreal_minus_eq_0 top_unique
                               simp del: measure_nonneg)
  qed auto
qed

lemma (in finite_measure) split_space_into_finite_sets_and_rest:
  assumes ac: "absolutely_continuous M N" and sets_eq: "sets N = sets M"
  shows "∃A0∈sets M. ∃B::nat⇒'a set. disjoint_family B ∧ range B ⊆ sets M ∧ A0 = space M - (⋃i. B i) ∧
    (∀A∈sets M. A ⊆ A0 ⟶ (emeasure M A = 0 ∧ N A = 0) ∨ (emeasure M A > 0 ∧ N A = ∞)) ∧
    (∀i. N (B i) ≠ ∞)"
proof -
  let ?Q = "{Q∈sets M. N Q ≠ ∞}"
  let ?a = "SUP Q:?Q. emeasure M Q"
  have "{} ∈ ?Q" by auto
  then have Q_not_empty: "?Q ≠ {}" by blast
  have "?a ≤ emeasure M (space M)" using sets.sets_into_space
    by (auto intro!: SUP_least emeasure_mono)
  then have "?a ≠ ∞"
    using finite_emeasure_space
    by (auto simp: less_top[symmetric] top_unique simp del: SUP_eq_top_iff Sup_eq_top_iff)
  from ennreal_SUP_countable_SUP [OF Q_not_empty, of "emeasure M"]
  obtain Q'' where "range Q'' ⊆ emeasure M ` ?Q" and a: "?a = (SUP i::nat. Q'' i)"
    by auto
  then have "∀i. ∃Q'. Q'' i = emeasure M Q' ∧ Q' ∈ ?Q" by auto
  from choice[OF this] obtain Q' where Q': "⋀i. Q'' i = emeasure M (Q' i)" "⋀i. Q' i ∈ ?Q"
    by auto
  then have a_Lim: "?a = (SUP i::nat. emeasure M (Q' i))" using a by simp
  let ?O = "λn. ⋃i≤n. Q' i"
  have Union: "(SUP i. emeasure M (?O i)) = emeasure M (⋃i. ?O i)"
  proof (rule SUP_emeasure_incseq[of ?O])
    show "range ?O ⊆ sets M" using Q' by auto
    show "incseq ?O" by (fastforce intro!: incseq_SucI)
  qed
  have Q'_sets: "⋀i. Q' i ∈ sets M" using Q' by auto
  have O_sets: "⋀i. ?O i ∈ sets M" using Q' by auto
  then have O_in_G: "⋀i. ?O i ∈ ?Q"
  proof (safe del: notI)
    fix i have "Q' ` {..i} ⊆ sets M" using Q' by auto
    then have "N (?O i) ≤ (∑i≤i. N (Q' i))"
      by (simp add: sets_eq emeasure_subadditive_finite)
    also have "… < ∞" using Q' by (simp add: less_top)
    finally show "N (?O i) ≠ ∞" by simp
  qed auto
  have O_mono: "⋀n. ?O n ⊆ ?O (Suc n)" by fastforce
  have a_eq: "?a = emeasure M (⋃i. ?O i)" unfolding Union[symmetric]
  proof (rule antisym)
    show "?a ≤ (SUP i. emeasure M (?O i))" unfolding a_Lim
      using Q' by (auto intro!: SUP_mono emeasure_mono)
    show "(SUP i. emeasure M (?O i)) ≤ ?a"
    proof (safe intro!: Sup_mono, unfold bex_simps)
      fix i
      have *: "(⋃(Q' ` {..i})) = ?O i" by auto
      then show "∃x. (x ∈ sets M ∧ N x ≠ ∞) ∧
        emeasure M (⋃(Q' ` {..i})) ≤ emeasure M x"
        using O_in_G[of i] by (auto intro!: exI[of _ "?O i"])
    qed
  qed
  let ?O_0 = "(⋃i. ?O i)"
  have "?O_0 ∈ sets M" using Q' by auto
  def Q  "λi. case i of 0 ⇒ Q' 0 | Suc n ⇒ ?O (Suc n) - ?O n"
  { fix i have "Q i ∈ sets M" unfolding Q_def using Q'[of 0] by (cases i) (auto intro: O_sets) }
  note Q_sets = this
  show ?thesis
  proof (intro bexI exI conjI ballI impI allI)
    show "disjoint_family Q"
      by (fastforce simp: disjoint_family_on_def Q_def
        split: nat.split_asm)
    show "range Q ⊆ sets M"
      using Q_sets by auto
    { fix A assume A: "A ∈ sets M" "A ⊆ space M - ?O_0"
      show "emeasure M A = 0 ∧ N A = 0 ∨ 0 < emeasure M A ∧ N A = ∞"
      proof (rule disjCI, simp)
        assume *: "emeasure M A = 0 ∨ N A ≠ top"
        show "emeasure M A = 0 ∧ N A = 0"
        proof (cases "emeasure M A = 0")
          case True
          with ac A have "N A = 0"
            unfolding absolutely_continuous_def by auto
          with True show ?thesis by simp
        next
          case False
          with * have "N A ≠ ∞" by auto
          with A have "emeasure M ?O_0 + emeasure M A = emeasure M (?O_0 ∪ A)"
            using Q' by (auto intro!: plus_emeasure sets.countable_UN)
          also have "… = (SUP i. emeasure M (?O i ∪ A))"
          proof (rule SUP_emeasure_incseq[of "λi. ?O i ∪ A", symmetric, simplified])
            show "range (λi. ?O i ∪ A) ⊆ sets M"
              using ‹N A ≠ ∞› O_sets A by auto
          qed (fastforce intro!: incseq_SucI)
          also have "… ≤ ?a"
          proof (safe intro!: SUP_least)
            fix i have "?O i ∪ A ∈ ?Q"
            proof (safe del: notI)
              show "?O i ∪ A ∈ sets M" using O_sets A by auto
              from O_in_G[of i] have "N (?O i ∪ A) ≤ N (?O i) + N A"
                using emeasure_subadditive[of "?O i" N A] A O_sets by (auto simp: sets_eq)
              with O_in_G[of i] show "N (?O i ∪ A) ≠ ∞"
                using ‹N A ≠ ∞› by (auto simp: top_unique)
            qed
            then show "emeasure M (?O i ∪ A) ≤ ?a" by (rule SUP_upper)
          qed
          finally have "emeasure M A = 0"
            unfolding a_eq using measure_nonneg[of M A] by (simp add: emeasure_eq_measure)
          with ‹emeasure M A ≠ 0› show ?thesis by auto
        qed
      qed }
    { fix i show "N (Q i) ≠ ∞"
      proof (cases i)
        case 0 then show ?thesis
          unfolding Q_def using Q'[of 0] by simp
      next
        case (Suc n)
        with ‹?O n ∈ ?Q› ‹?O (Suc n) ∈ ?Q› emeasure_Diff[OF _ _ _ O_mono, of N n]
        show ?thesis
          by (auto simp: sets_eq Q_def)
      qed }
    show "space M - ?O_0 ∈ sets M" using Q'_sets by auto
    { fix j have "(⋃i≤j. ?O i) = (⋃i≤j. Q i)"
      proof (induct j)
        case 0 then show ?case by (simp add: Q_def)
      next
        case (Suc j)
        have eq: "⋀j. (⋃i≤j. ?O i) = (⋃i≤j. Q' i)" by fastforce
        have "{..j} ∪ {..Suc j} = {..Suc j}" by auto
        then have "(⋃i≤Suc j. Q' i) = (⋃i≤j. Q' i) ∪ Q (Suc j)"
          by (simp add: UN_Un[symmetric] Q_def del: UN_Un)
        then show ?case using Suc by (auto simp add: eq atMost_Suc)
      qed }
    then have "(⋃j. (⋃i≤j. ?O i)) = (⋃j. (⋃i≤j. Q i))" by simp
    then show "space M - ?O_0 = space M - (⋃i. Q i)" by fastforce
  qed
qed

lemma (in finite_measure) Radon_Nikodym_finite_measure_infinite:
  assumes "absolutely_continuous M N" and sets_eq: "sets N = sets M"
  shows "∃f∈borel_measurable M. (∀x. 0 ≤ f x) ∧ density M f = N"
proof -
  from split_space_into_finite_sets_and_rest[OF assms]
  obtain Q0 and Q :: "nat ⇒ 'a set"
    where Q: "disjoint_family Q" "range Q ⊆ sets M"
    and Q0: "Q0 ∈ sets M" "Q0 = space M - (⋃i. Q i)"
    and in_Q0: "⋀A. A ∈ sets M ⟹ A ⊆ Q0 ⟹ emeasure M A = 0 ∧ N A = 0 ∨ 0 < emeasure M A ∧ N A = ∞"
    and Q_fin: "⋀i. N (Q i) ≠ ∞" by force
  from Q have Q_sets: "⋀i. Q i ∈ sets M" by auto
  let ?N = "λi. density N (indicator (Q i))" and ?M = "λi. density M (indicator (Q i))"
  have "∀i. ∃f∈borel_measurable (?M i). (∀x. 0 ≤ f x) ∧ density (?M i) f = ?N i"
  proof (intro allI finite_measure.Radon_Nikodym_finite_measure)
    fix i
    from Q show "finite_measure (?M i)"
      by (auto intro!: finite_measureI cong: nn_integral_cong
               simp add: emeasure_density subset_eq sets_eq)
    from Q have "emeasure (?N i) (space N) = emeasure N (Q i)"
      by (simp add: sets_eq[symmetric] emeasure_density subset_eq cong: nn_integral_cong)
    with Q_fin show "finite_measure (?N i)"
      by (auto intro!: finite_measureI)
    show "sets (?N i) = sets (?M i)" by (simp add: sets_eq)
    have [measurable]: "⋀A. A ∈ sets M ⟹ A ∈ sets N" by (simp add: sets_eq)
    show "absolutely_continuous (?M i) (?N i)"
      using ‹absolutely_continuous M N› ‹Q i ∈ sets M›
      by (auto simp: absolutely_continuous_def null_sets_density_iff sets_eq
               intro!: absolutely_continuous_AE[OF sets_eq])
  qed
  from choice[OF this[unfolded Bex_def]]
  obtain f where borel: "⋀i. f i ∈ borel_measurable M" "⋀i x. 0 ≤ f i x"
    and f_density: "⋀i. density (?M i) (f i) = ?N i"
    by force
  { fix A i assume A: "A ∈ sets M"
    with Q borel have "(∫+x. f i x * indicator (Q i ∩ A) x ∂M) = emeasure (density (?M i) (f i)) A"
      by (auto simp add: emeasure_density nn_integral_density subset_eq
               intro!: nn_integral_cong split: split_indicator)
    also have "… = emeasure N (Q i ∩ A)"
      using A Q by (simp add: f_density emeasure_restricted subset_eq sets_eq)
    finally have "emeasure N (Q i ∩ A) = (∫+x. f i x * indicator (Q i ∩ A) x ∂M)" .. }
  note integral_eq = this
  let ?f = "λx. (∑i. f i x * indicator (Q i) x) + ∞ * indicator Q0 x"
  show ?thesis
  proof (safe intro!: bexI[of _ ?f])
    show "?f ∈ borel_measurable M" using Q0 borel Q_sets
      by (auto intro!: measurable_If)
    show "⋀x. 0 ≤ ?f x" using borel by (auto intro!: suminf_0_le simp: indicator_def)
    show "density M ?f = N"
    proof (rule measure_eqI)
      fix A assume "A ∈ sets (density M ?f)"
      then have "A ∈ sets M" by simp
      have Qi: "⋀i. Q i ∈ sets M" using Q by auto
      have [intro,simp]: "⋀i. (λx. f i x * indicator (Q i ∩ A) x) ∈ borel_measurable M"
        "⋀i. AE x in M. 0 ≤ f i x * indicator (Q i ∩ A) x"
        using borel Qi Q0(1) ‹A ∈ sets M› by auto
      have "(∫+x. ?f x * indicator A x ∂M) = (∫+x. (∑i. f i x * indicator (Q i ∩ A) x) + ∞ * indicator (Q0 ∩ A) x ∂M)"
        using borel by (intro nn_integral_cong) (auto simp: indicator_def)
      also have "… = (∫+x. (∑i. f i x * indicator (Q i ∩ A) x) ∂M) + ∞ * emeasure M (Q0 ∩ A)"
        using borel Qi Q0(1) ‹A ∈ sets M›
        by (subst nn_integral_add)
           (auto simp add: nn_integral_cmult_indicator sets.Int intro!: suminf_0_le)
      also have "… = (∑i. N (Q i ∩ A)) + ∞ * emeasure M (Q0 ∩ A)"
        by (subst integral_eq[OF ‹A ∈ sets M›], subst nn_integral_suminf) auto
      finally have "(∫+x. ?f x * indicator A x ∂M) = (∑i. N (Q i ∩ A)) + ∞ * emeasure M (Q0 ∩ A)" .
      moreover have "(∑i. N (Q i ∩ A)) = N ((⋃i. Q i) ∩ A)"
        using Q Q_sets ‹A ∈ sets M›
        by (subst suminf_emeasure) (auto simp: disjoint_family_on_def sets_eq)
      moreover have "∞ * emeasure M (Q0 ∩ A) = N (Q0 ∩ A)"
      proof -
        have "Q0 ∩ A ∈ sets M" using Q0(1) ‹A ∈ sets M› by blast
        from in_Q0[OF this] show ?thesis by (auto simp: ennreal_top_mult)
      qed
      moreover have "Q0 ∩ A ∈ sets M" "((⋃i. Q i) ∩ A) ∈ sets M"
        using Q_sets ‹A ∈ sets M› Q0(1) by auto
      moreover have "((⋃i. Q i) ∩ A) ∪ (Q0 ∩ A) = A" "((⋃i. Q i) ∩ A) ∩ (Q0 ∩ A) = {}"
        using ‹A ∈ sets M› sets.sets_into_space Q0 by auto
      ultimately have "N A = (∫+x. ?f x * indicator A x ∂M)"
        using plus_emeasure[of "(⋃i. Q i) ∩ A" N "Q0 ∩ A"] by (simp add: sets_eq)
      with ‹A ∈ sets M› borel Q Q0(1) show "emeasure (density M ?f) A = N A"
        by (auto simp: subset_eq emeasure_density)
    qed (simp add: sets_eq)
  qed
qed

lemma (in sigma_finite_measure) Radon_Nikodym:
  assumes ac: "absolutely_continuous M N" assumes sets_eq: "sets N = sets M"
  shows "∃f ∈ borel_measurable M. (∀x. 0 ≤ f x) ∧ density M f = N"
proof -
  from Ex_finite_integrable_function
  obtain h where finite: "integralN M h ≠ ∞" and
    borel: "h ∈ borel_measurable M" and
    nn: "⋀x. 0 ≤ h x" and
    pos: "⋀x. x ∈ space M ⟹ 0 < h x" and
    "⋀x. x ∈ space M ⟹ h x < ∞" by auto
  let ?T = "λA. (∫+x. h x * indicator A x ∂M)"
  let ?MT = "density M h"
  from borel finite nn interpret T: finite_measure ?MT
    by (auto intro!: finite_measureI cong: nn_integral_cong simp: emeasure_density)
  have "absolutely_continuous ?MT N" "sets N = sets ?MT"
  proof (unfold absolutely_continuous_def, safe)
    fix A assume "A ∈ null_sets ?MT"
    with borel have "A ∈ sets M" "AE x in M. x ∈ A ⟶ h x ≤ 0"
      by (auto simp add: null_sets_density_iff)
    with pos sets.sets_into_space have "AE x in M. x ∉ A"
      by (elim eventually_mono) (auto simp: not_le[symmetric])
    then have "A ∈ null_sets M"
      using ‹A ∈ sets M› by (simp add: AE_iff_null_sets)
    with ac show "A ∈ null_sets N"
      by (auto simp: absolutely_continuous_def)
  qed (auto simp add: sets_eq)
  from T.Radon_Nikodym_finite_measure_infinite[OF this]
  obtain f where f_borel: "f ∈ borel_measurable M" "⋀x. 0 ≤ f x" "density ?MT f = N" by auto
  with nn borel show ?thesis
    by (auto intro!: bexI[of _ "λx. h x * f x"] simp: density_density_eq)
qed

subsection ‹Uniqueness of densities›

lemma finite_density_unique:
  assumes borel: "f ∈ borel_measurable M" "g ∈ borel_measurable M"
  assumes pos: "AE x in M. 0 ≤ f x" "AE x in M. 0 ≤ g x"
  and fin: "integralN M f ≠ ∞"
  shows "density M f = density M g ⟷ (AE x in M. f x = g x)"
proof (intro iffI ballI)
  fix A assume eq: "AE x in M. f x = g x"
  with borel show "density M f = density M g"
    by (auto intro: density_cong)
next
  let ?P = "λf A. ∫+ x. f x * indicator A x ∂M"
  assume "density M f = density M g"
  with borel have eq: "∀A∈sets M. ?P f A = ?P g A"
    by (simp add: emeasure_density[symmetric])
  from this[THEN bspec, OF sets.top] fin
  have g_fin: "integralN M g ≠ ∞" by (simp cong: nn_integral_cong)
  { fix f g assume borel: "f ∈ borel_measurable M" "g ∈ borel_measurable M"
      and pos: "AE x in M. 0 ≤ f x" "AE x in M. 0 ≤ g x"
      and g_fin: "integralN M g ≠ ∞" and eq: "∀A∈sets M. ?P f A = ?P g A"
    let ?N = "{x∈space M. g x < f x}"
    have N: "?N ∈ sets M" using borel by simp
    have "?P g ?N ≤ integralN M g" using pos
      by (intro nn_integral_mono_AE) (auto split: split_indicator)
    then have Pg_fin: "?P g ?N ≠ ∞" using g_fin by (auto simp: top_unique)
    have "?P (λx. (f x - g x)) ?N = (∫+x. f x * indicator ?N x - g x * indicator ?N x ∂M)"
      by (auto intro!: nn_integral_cong simp: indicator_def)
    also have "… = ?P f ?N - ?P g ?N"
    proof (rule nn_integral_diff)
      show "(λx. f x * indicator ?N x) ∈ borel_measurable M" "(λx. g x * indicator ?N x) ∈ borel_measurable M"
        using borel N by auto
      show "AE x in M. g x * indicator ?N x ≤ f x * indicator ?N x"
        using pos by (auto split: split_indicator)
    qed fact
    also have "… = 0"
      unfolding eq[THEN bspec, OF N] using Pg_fin by auto
    finally have "AE x in M. f x ≤ g x"
      using pos borel nn_integral_PInf_AE[OF borel(2) g_fin]
      by (subst (asm) nn_integral_0_iff_AE)
         (auto split: split_indicator simp: not_less ennreal_minus_eq_0) }
  from this[OF borel pos g_fin eq] this[OF borel(2,1) pos(2,1) fin] eq
  show "AE x in M. f x = g x" by auto
qed

lemma (in finite_measure) density_unique_finite_measure:
  assumes borel: "f ∈ borel_measurable M" "f' ∈ borel_measurable M"
  assumes pos: "AE x in M. 0 ≤ f x" "AE x in M. 0 ≤ f' x"
  assumes f: "⋀A. A ∈ sets M ⟹ (∫+x. f x * indicator A x ∂M) = (∫+x. f' x * indicator A x ∂M)"
    (is "⋀A. A ∈ sets M ⟹ ?P f A = ?P f' A")
  shows "AE x in M. f x = f' x"
proof -
  let ?D = "λf. density M f"
  let ?N = "λA. ?P f A" and ?N' = "λA. ?P f' A"
  let ?f = "λA x. f x * indicator A x" and ?f' = "λA x. f' x * indicator A x"

  have ac: "absolutely_continuous M (density M f)" "sets (density M f) = sets M"
    using borel by (auto intro!: absolutely_continuousI_density)
  from split_space_into_finite_sets_and_rest[OF this]
  obtain Q0 and Q :: "nat ⇒ 'a set"
    where Q: "disjoint_family Q" "range Q ⊆ sets M"
    and Q0: "Q0 ∈ sets M" "Q0 = space M - (⋃i. Q i)"
    and in_Q0: "⋀A. A ∈ sets M ⟹ A ⊆ Q0 ⟹ emeasure M A = 0 ∧ ?D f A = 0 ∨ 0 < emeasure M A ∧ ?D f A = ∞"
    and Q_fin: "⋀i. ?D f (Q i) ≠ ∞" by force
  with borel pos have in_Q0: "⋀A. A ∈ sets M ⟹ A ⊆ Q0 ⟹ emeasure M A = 0 ∧ ?N A = 0 ∨ 0 < emeasure M A ∧ ?N A = ∞"
    and Q_fin: "⋀i. ?N (Q i) ≠ ∞" by (auto simp: emeasure_density subset_eq)

  from Q have Q_sets: "⋀i. Q i ∈ sets M" by auto
  let ?D = "{x∈space M. f x ≠ f' x}"
  have "?D ∈ sets M" using borel by auto
  have *: "⋀i x A. ⋀y::ennreal. y * indicator (Q i) x * indicator A x = y * indicator (Q i ∩ A) x"
    unfolding indicator_def by auto
  have "∀i. AE x in M. ?f (Q i) x = ?f' (Q i) x" using borel Q_fin Q pos
    by (intro finite_density_unique[THEN iffD1] allI)
       (auto intro!: f measure_eqI simp: emeasure_density * subset_eq)
  moreover have "AE x in M. ?f Q0 x = ?f' Q0 x"
  proof (rule AE_I')
    { fix f :: "'a ⇒ ennreal" assume borel: "f ∈ borel_measurable M"
        and eq: "⋀A. A ∈ sets M ⟹ ?N A = (∫+x. f x * indicator A x ∂M)"
      let ?A = "λi. Q0 ∩ {x ∈ space M. f x < (i::nat)}"
      have "(⋃i. ?A i) ∈ null_sets M"
      proof (rule null_sets_UN)
        fix i ::nat have "?A i ∈ sets M"
          using borel Q0(1) by auto
        have "?N (?A i) ≤ (∫+x. (i::ennreal) * indicator (?A i) x ∂M)"
          unfolding eq[OF ‹?A i ∈ sets M›]
          by (auto intro!: nn_integral_mono simp: indicator_def)
        also have "… = i * emeasure M (?A i)"
          using ‹?A i ∈ sets M› by (auto intro!: nn_integral_cmult_indicator)
        also have "… < ∞" using emeasure_real[of "?A i"] by (auto simp: ennreal_mult_less_top of_nat_less_top)
        finally have "?N (?A i) ≠ ∞" by simp
        then show "?A i ∈ null_sets M" using in_Q0[OF ‹?A i ∈ sets M›] ‹?A i ∈ sets M› by auto
      qed
      also have "(⋃i. ?A i) = Q0 ∩ {x∈space M. f x ≠ ∞}"
        by (auto simp: ennreal_Ex_less_of_nat less_top[symmetric])
      finally have "Q0 ∩ {x∈space M. f x ≠ ∞} ∈ null_sets M" by simp }
    from this[OF borel(1) refl] this[OF borel(2) f]
    have "Q0 ∩ {x∈space M. f x ≠ ∞} ∈ null_sets M" "Q0 ∩ {x∈space M. f' x ≠ ∞} ∈ null_sets M" by simp_all
    then show "(Q0 ∩ {x∈space M. f x ≠ ∞}) ∪ (Q0 ∩ {x∈space M. f' x ≠ ∞}) ∈ null_sets M" by (rule null_sets.Un)
    show "{x ∈ space M. ?f Q0 x ≠ ?f' Q0 x} ⊆
      (Q0 ∩ {x∈space M. f x ≠ ∞}) ∪ (Q0 ∩ {x∈space M. f' x ≠ ∞})" by (auto simp: indicator_def)
  qed
  moreover have "AE x in M. (?f Q0 x = ?f' Q0 x) ⟶ (∀i. ?f (Q i) x = ?f' (Q i) x) ⟶
    ?f (space M) x = ?f' (space M) x"
    by (auto simp: indicator_def Q0)
  ultimately have "AE x in M. ?f (space M) x = ?f' (space M) x"
    unfolding AE_all_countable[symmetric]
    by eventually_elim (auto intro!: AE_I2 split: if_split_asm simp: indicator_def)
  then show "AE x in M. f x = f' x" by auto
qed

lemma (in sigma_finite_measure) density_unique:
  assumes f: "f ∈ borel_measurable M"
  assumes f': "f' ∈ borel_measurable M"
  assumes density_eq: "density M f = density M f'"
  shows "AE x in M. f x = f' x"
proof -
  obtain h where h_borel: "h ∈ borel_measurable M"
    and fin: "integralN M h ≠ ∞" and pos: "⋀x. x ∈ space M ⟹ 0 < h x ∧ h x < ∞" "⋀x. 0 ≤ h x"
    using Ex_finite_integrable_function by auto
  then have h_nn: "AE x in M. 0 ≤ h x" by auto
  let ?H = "density M h"
  interpret h: finite_measure ?H
    using fin h_borel pos
    by (intro finite_measureI) (simp cong: nn_integral_cong emeasure_density add: fin)
  let ?fM = "density M f"
  let ?f'M = "density M f'"
  { fix A assume "A ∈ sets M"
    then have "{x ∈ space M. h x * indicator A x ≠ 0} = A"
      using pos(1) sets.sets_into_space by (force simp: indicator_def)
    then have "(∫+x. h x * indicator A x ∂M) = 0 ⟷ A ∈ null_sets M"
      using h_borel ‹A ∈ sets M› h_nn by (subst nn_integral_0_iff) auto }
  note h_null_sets = this
  { fix A assume "A ∈ sets M"
    have "(∫+x. f x * (h x * indicator A x) ∂M) = (∫+x. h x * indicator A x ∂?fM)"
      using ‹A ∈ sets M› h_borel h_nn f f'
      by (intro nn_integral_density[symmetric]) auto
    also have "… = (∫+x. h x * indicator A x ∂?f'M)"
      by (simp_all add: density_eq)
    also have "… = (∫+x. f' x * (h x * indicator A x) ∂M)"
      using ‹A ∈ sets M› h_borel h_nn f f'
      by (intro nn_integral_density) auto
    finally have "(∫+x. h x * (f x * indicator A x) ∂M) = (∫+x. h x * (f' x * indicator A x) ∂M)"
      by (simp add: ac_simps)
    then have "(∫+x. (f x * indicator A x) ∂?H) = (∫+x. (f' x * indicator A x) ∂?H)"
      using ‹A ∈ sets M› h_borel h_nn f f'
      by (subst (asm) (1 2) nn_integral_density[symmetric]) auto }
  then have "AE x in ?H. f x = f' x" using h_borel h_nn f f'
    by (intro h.density_unique_finite_measure absolutely_continuous_AE[of M]) auto
  with AE_space[of M] pos show "AE x in M. f x = f' x"
    unfolding AE_density[OF h_borel] by auto
qed

lemma (in sigma_finite_measure) density_unique_iff:
  assumes f: "f ∈ borel_measurable M" and f': "f' ∈ borel_measurable M"
  shows "density M f = density M f' ⟷ (AE x in M. f x = f' x)"
  using density_unique[OF assms] density_cong[OF f f'] by auto

lemma sigma_finite_density_unique:
  assumes borel: "f ∈ borel_measurable M" "g ∈ borel_measurable M"
  and fin: "sigma_finite_measure (density M f)"
  shows "density M f = density M g ⟷ (AE x in M. f x = g x)"
proof
  assume "AE x in M. f x = g x" with borel show "density M f = density M g"
    by (auto intro: density_cong)
next
  assume eq: "density M f = density M g"
  interpret f: sigma_finite_measure "density M f" by fact
  from f.sigma_finite_incseq guess A . note cover = this

  have "AE x in M. ∀i. x ∈ A i ⟶ f x = g x"
    unfolding AE_all_countable
  proof
    fix i
    have "density (density M f) (indicator (A i)) = density (density M g) (indicator (A i))"
      unfolding eq ..
    moreover have "(∫+x. f x * indicator (A i) x ∂M) ≠ ∞"
      using cover(1) cover(3)[of i] borel by (auto simp: emeasure_density subset_eq)
    ultimately have "AE x in M. f x * indicator (A i) x = g x * indicator (A i) x"
      using borel cover(1)
      by (intro finite_density_unique[THEN iffD1]) (auto simp: density_density_eq subset_eq)
    then show "AE x in M. x ∈ A i ⟶ f x = g x"
      by auto
  qed
  with AE_space show "AE x in M. f x = g x"
    apply eventually_elim
    using cover(2)[symmetric]
    apply auto
    done
qed

lemma (in sigma_finite_measure) sigma_finite_iff_density_finite':
  assumes f: "f ∈ borel_measurable M"
  shows "sigma_finite_measure (density M f) ⟷ (AE x in M. f x ≠ ∞)"
    (is "sigma_finite_measure ?N ⟷ _")
proof
  assume "sigma_finite_measure ?N"
  then interpret N: sigma_finite_measure ?N .
  from N.Ex_finite_integrable_function obtain h where
    h: "h ∈ borel_measurable M" "integralN ?N h ≠ ∞" and
    fin: "∀x∈space M. 0 < h x ∧ h x < ∞"
    by auto
  have "AE x in M. f x * h x ≠ ∞"
  proof (rule AE_I')
    have "integralN ?N h = (∫+x. f x * h x ∂M)"
      using f h by (auto intro!: nn_integral_density)
    then have "(∫+x. f x * h x ∂M) ≠ ∞"
      using h(2) by simp
    then show "(λx. f x * h x) -` {∞} ∩ space M ∈ null_sets M"
      using f h(1) by (auto intro!: nn_integral_PInf[unfolded infinity_ennreal_def] borel_measurable_vimage)
  qed auto
  then show "AE x in M. f x ≠ ∞"
    using fin by (auto elim!: AE_Ball_mp simp: less_top ennreal_mult_less_top)
next
  assume AE: "AE x in M. f x ≠ ∞"
  from sigma_finite guess Q . note Q = this
  def A  "λi. f -` (case i of 0 ⇒ {∞} | Suc n ⇒ {.. ennreal(of_nat (Suc n))}) ∩ space M"
  { fix i j have "A i ∩ Q j ∈ sets M"
    unfolding A_def using f Q
    apply (rule_tac sets.Int)
    by (cases i) (auto intro: measurable_sets[OF f(1)]) }
  note A_in_sets = this

  show "sigma_finite_measure ?N"
  proof (standard, intro exI conjI ballI)
    show "countable (range (λ(i, j). A i ∩ Q j))"
      by auto
    show "range (λ(i, j). A i ∩ Q j) ⊆ sets (density M f)"
      using A_in_sets by auto
  next
    have "⋃range (λ(i, j). A i ∩ Q j) = (⋃i j. A i ∩ Q j)"
      by auto
    also have "… = (⋃i. A i) ∩ space M" using Q by auto
    also have "(⋃i. A i) = space M"
    proof safe
      fix x assume x: "x ∈ space M"
      show "x ∈ (⋃i. A i)"
      proof (cases "f x" rule: ennreal_cases)
        case top with x show ?thesis unfolding A_def by (auto intro: exI[of _ 0])
      next
        case (real r)
        with ennreal_Ex_less_of_nat[of "f x"] obtain n :: nat where "f x < n"
          by auto
        also have "n < (Suc n :: ennreal)"
          by simp
        finally show ?thesis
          using x real by (auto simp: A_def ennreal_of_nat_eq_real_of_nat intro!: exI[of _ "Suc n"])
      qed
    qed (auto simp: A_def)
    finally show "⋃range (λ(i, j). A i ∩ Q j) = space ?N" by simp
  next
    fix X assume "X ∈ range (λ(i, j). A i ∩ Q j)"
    then obtain i j where [simp]:"X = A i ∩ Q j" by auto
    have "(∫+x. f x * indicator (A i ∩ Q j) x ∂M) ≠ ∞"
    proof (cases i)
      case 0
      have "AE x in M. f x * indicator (A i ∩ Q j) x = 0"
        using AE by (auto simp: A_def ‹i = 0›)
      from nn_integral_cong_AE[OF this] show ?thesis by simp
    next
      case (Suc n)
      then have "(∫+x. f x * indicator (A i ∩ Q j) x ∂M) ≤
        (∫+x. (Suc n :: ennreal) * indicator (Q j) x ∂M)"
        by (auto intro!: nn_integral_mono simp: indicator_def A_def ennreal_of_nat_eq_real_of_nat)
      also have "… = Suc n * emeasure M (Q j)"
        using Q by (auto intro!: nn_integral_cmult_indicator)
      also have "… < ∞"
        using Q by (auto simp: ennreal_mult_less_top less_top of_nat_less_top)
      finally show ?thesis by simp
    qed
    then show "emeasure ?N X ≠ ∞"
      using A_in_sets Q f by (auto simp: emeasure_density)
  qed
qed

lemma (in sigma_finite_measure) sigma_finite_iff_density_finite:
  "f ∈ borel_measurable M ⟹ sigma_finite_measure (density M f) ⟷ (AE x in M. f x ≠ ∞)"
  by (subst sigma_finite_iff_density_finite')
     (auto simp: max_def intro!: measurable_If)

subsection ‹Radon-Nikodym derivative›

definition RN_deriv :: "'a measure ⇒ 'a measure ⇒ 'a ⇒ ennreal" where
  "RN_deriv M N =
    (if ∃f. f ∈ borel_measurable M ∧ density M f = N
       then SOME f. f ∈ borel_measurable M ∧ density M f = N
       else (λ_. 0))"

lemma RN_derivI:
  assumes "f ∈ borel_measurable M" "density M f = N"
  shows "density M (RN_deriv M N) = N"
proof -
  have "∃f. f ∈ borel_measurable M ∧ density M f = N"
    using assms by auto
  moreover then have "density M (SOME f. f ∈ borel_measurable M ∧ density M f = N) = N"
    by (rule someI2_ex) auto
  ultimately show ?thesis
    by (auto simp: RN_deriv_def)
qed

lemma borel_measurable_RN_deriv[measurable]: "RN_deriv M N ∈ borel_measurable M"
proof -
  { assume ex: "∃f. f ∈ borel_measurable M ∧ density M f = N"
    have 1: "(SOME f. f ∈ borel_measurable M ∧ density M f = N) ∈ borel_measurable M"
      using ex by (rule someI2_ex) auto }
  from this show ?thesis
    by (auto simp: RN_deriv_def)
qed

lemma density_RN_deriv_density:
  assumes f: "f ∈ borel_measurable M"
  shows "density M (RN_deriv M (density M f)) = density M f"
  by (rule RN_derivI[OF f]) simp

lemma (in sigma_finite_measure) density_RN_deriv:
  "absolutely_continuous M N ⟹ sets N = sets M ⟹ density M (RN_deriv M N) = N"
  by (metis RN_derivI Radon_Nikodym)

lemma (in sigma_finite_measure) RN_deriv_nn_integral:
  assumes N: "absolutely_continuous M N" "sets N = sets M"
    and f: "f ∈ borel_measurable M"
  shows "integralN N f = (∫+x. RN_deriv M N x * f x ∂M)"
proof -
  have "integralN N f = integralN (density M (RN_deriv M N)) f"
    using N by (simp add: density_RN_deriv)
  also have "… = (∫+x. RN_deriv M N x * f x ∂M)"
    using f by (simp add: nn_integral_density)
  finally show ?thesis by simp
qed

lemma null_setsD_AE: "N ∈ null_sets M ⟹ AE x in M. x ∉ N"
  using AE_iff_null_sets[of N M] by auto

lemma (in sigma_finite_measure) RN_deriv_unique:
  assumes f: "f ∈ borel_measurable M"
  and eq: "density M f = N"
  shows "AE x in M. f x = RN_deriv M N x"
  unfolding eq[symmetric]
  by (intro density_unique_iff[THEN iffD1] f borel_measurable_RN_deriv
            density_RN_deriv_density[symmetric])

lemma RN_deriv_unique_sigma_finite:
  assumes f: "f ∈ borel_measurable M"
  and eq: "density M f = N" and fin: "sigma_finite_measure N"
  shows "AE x in M. f x = RN_deriv M N x"
  using fin unfolding eq[symmetric]
  by (intro sigma_finite_density_unique[THEN iffD1] f borel_measurable_RN_deriv
            density_RN_deriv_density[symmetric])

lemma (in sigma_finite_measure) RN_deriv_distr:
  fixes T :: "'a ⇒ 'b"
  assumes T: "T ∈ measurable M M'" and T': "T' ∈ measurable M' M"
    and inv: "∀x∈space M. T' (T x) = x"
  and ac[simp]: "absolutely_continuous (distr M M' T) (distr N M' T)"
  and N: "sets N = sets M"
  shows "AE x in M. RN_deriv (distr M M' T) (distr N M' T) (T x) = RN_deriv M N x"
proof (rule RN_deriv_unique)
  have [simp]: "sets N = sets M" by fact
  note sets_eq_imp_space_eq[OF N, simp]
  have measurable_N[simp]: "⋀M'. measurable N M' = measurable M M'" by (auto simp: measurable_def)
  { fix A assume "A ∈ sets M"
    with inv T T' sets.sets_into_space[OF this]
    have "T -` T' -` A ∩ T -` space M' ∩ space M = A"
      by (auto simp: measurable_def) }
  note eq = this[simp]
  { fix A assume "A ∈ sets M"
    with inv T T' sets.sets_into_space[OF this]
    have "(T' ∘ T) -` A ∩ space M = A"
      by (auto simp: measurable_def) }
  note eq2 = this[simp]
  let ?M' = "distr M M' T" and ?N' = "distr N M' T"
  interpret M': sigma_finite_measure ?M'
  proof
    from sigma_finite_countable guess F .. note F = this
    show "∃A. countable A ∧ A ⊆ sets (distr M M' T) ∧ ⋃A = space (distr M M' T) ∧ (∀a∈A. emeasure (distr M M' T) a ≠ ∞)"
    proof (intro exI conjI ballI)
      show *: "(λA. T' -` A ∩ space ?M') ` F ⊆ sets ?M'"
        using F T' by (auto simp: measurable_def)
      show "⋃((λA. T' -` A ∩ space ?M')`F) = space ?M'"
        using F T'[THEN measurable_space] by (auto simp: set_eq_iff)
    next
      fix X assume "X ∈ (λA. T' -` A ∩ space ?M')`F"
      then obtain A where [simp]: "X = T' -` A ∩ space ?M'" and "A ∈ F" by auto
      have "X ∈ sets M'" using F T' ‹A∈F› by auto
      moreover
      have Fi: "A ∈ sets M" using F ‹A∈F› by auto
      ultimately show "emeasure ?M' X ≠ ∞"
        using F T T' ‹A∈F› by (simp add: emeasure_distr)
    qed (insert F, auto)
  qed
  have "(RN_deriv ?M' ?N') ∘ T ∈ borel_measurable M"
    using T ac by measurable
  then show "(λx. RN_deriv ?M' ?N' (T x)) ∈ borel_measurable M"
    by (simp add: comp_def)

  have "N = distr N M (T' ∘ T)"
    by (subst measure_of_of_measure[of N, symmetric])
       (auto simp add: distr_def sets.sigma_sets_eq intro!: measure_of_eq sets.space_closed)
  also have "… = distr (distr N M' T) M T'"
    using T T' by (simp add: distr_distr)
  also have "… = distr (density (distr M M' T) (RN_deriv (distr M M' T) (distr N M' T))) M T'"
    using ac by (simp add: M'.density_RN_deriv)
  also have "… = density M (RN_deriv (distr M M' T) (distr N M' T) ∘ T)"
    by (simp add: distr_density_distr[OF T T', OF inv])
  finally show "density M (λx. RN_deriv (distr M M' T) (distr N M' T) (T x)) = N"
    by (simp add: comp_def)
qed

lemma (in sigma_finite_measure) RN_deriv_finite:
  assumes N: "sigma_finite_measure N" and ac: "absolutely_continuous M N" "sets N = sets M"
  shows "AE x in M. RN_deriv M N x ≠ ∞"
proof -
  interpret N: sigma_finite_measure N by fact
  from N show ?thesis
    using sigma_finite_iff_density_finite[OF borel_measurable_RN_deriv, of N] density_RN_deriv[OF ac]
    by simp
qed

lemma (in sigma_finite_measure)
  assumes N: "sigma_finite_measure N" and ac: "absolutely_continuous M N" "sets N = sets M"
    and f: "f ∈ borel_measurable M"
  shows RN_deriv_integrable: "integrable N f ⟷
      integrable M (λx. enn2real (RN_deriv M N x) * f x)" (is ?integrable)
    and RN_deriv_integral: "integralL N f = (∫x. enn2real (RN_deriv M N x) * f x ∂M)" (is ?integral)
proof -
  note ac(2)[simp] and sets_eq_imp_space_eq[OF ac(2), simp]
  interpret N: sigma_finite_measure N by fact

  have eq: "density M (RN_deriv M N) = density M (λx. enn2real (RN_deriv M N x))"
  proof (rule density_cong)
    from RN_deriv_finite[OF assms(1,2,3)]
    show "AE x in M. RN_deriv M N x = ennreal (enn2real (RN_deriv M N x))"
      by eventually_elim (auto simp: less_top)
  qed (insert ac, auto)

  show ?integrable
    apply (subst density_RN_deriv[OF ac, symmetric])
    unfolding eq
    apply (intro integrable_real_density f AE_I2 enn2real_nonneg)
    apply (insert ac, auto)
    done

  show ?integral
    apply (subst density_RN_deriv[OF ac, symmetric])
    unfolding eq
    apply (intro integral_real_density f AE_I2 enn2real_nonneg)
    apply (insert ac, auto)
    done
qed

lemma (in sigma_finite_measure) real_RN_deriv:
  assumes "finite_measure N"
  assumes ac: "absolutely_continuous M N" "sets N = sets M"
  obtains D where "D ∈ borel_measurable M"
    and "AE x in M. RN_deriv M N x = ennreal (D x)"
    and "AE x in N. 0 < D x"
    and "⋀x. 0 ≤ D x"
proof
  interpret N: finite_measure N by fact

  note RN = borel_measurable_RN_deriv density_RN_deriv[OF ac]

  let ?RN = "λt. {x ∈ space M. RN_deriv M N x = t}"

  show "(λx. enn2real (RN_deriv M N x)) ∈ borel_measurable M"
    using RN by auto

  have "N (?RN ∞) = (∫+ x. RN_deriv M N x * indicator (?RN ∞) x ∂M)"
    using RN(1) by (subst RN(2)[symmetric]) (auto simp: emeasure_density)
  also have "… = (∫+ x. ∞ * indicator (?RN ∞) x ∂M)"
    by (intro nn_integral_cong) (auto simp: indicator_def)
  also have "… = ∞ * emeasure M (?RN ∞)"
    using RN by (intro nn_integral_cmult_indicator) auto
  finally have eq: "N (?RN ∞) = ∞ * emeasure M (?RN ∞)" .
  moreover
  have "emeasure M (?RN ∞) = 0"
  proof (rule ccontr)
    assume "emeasure M {x ∈ space M. RN_deriv M N x = ∞} ≠ 0"
    then have "0 < emeasure M {x ∈ space M. RN_deriv M N x = ∞}"
      by (auto simp: zero_less_iff_neq_zero)
    with eq have "N (?RN ∞) = ∞" by (simp add: ennreal_mult_eq_top_iff)
    with N.emeasure_finite[of "?RN ∞"] RN show False by auto
  qed
  ultimately have "AE x in M. RN_deriv M N x < ∞"
    using RN by (intro AE_iff_measurable[THEN iffD2]) (auto simp: less_top[symmetric])
  then show "AE x in M. RN_deriv M N x = ennreal (enn2real (RN_deriv M N x))"
    by auto
  then have eq: "AE x in N. RN_deriv M N x = ennreal (enn2real (RN_deriv M N x))"
    using ac absolutely_continuous_AE by auto


  have "N (?RN 0) = (∫+ x. RN_deriv M N x * indicator (?RN 0) x ∂M)"
    by (subst RN(2)[symmetric]) (auto simp: emeasure_density)
  also have "… = (∫+ x. 0 ∂M)"
    by (intro nn_integral_cong) (auto simp: indicator_def)
  finally have "AE x in N. RN_deriv M N x ≠ 0"
    using RN by (subst AE_iff_measurable[OF _ refl]) (auto simp: ac cong: sets_eq_imp_space_eq)
  with eq show "AE x in N. 0 < enn2real (RN_deriv M N x)"
    by (auto simp: enn2real_positive_iff less_top[symmetric] zero_less_iff_neq_zero)
qed (rule enn2real_nonneg)

lemma (in sigma_finite_measure) RN_deriv_singleton:
  assumes ac: "absolutely_continuous M N" "sets N = sets M"
  and x: "{x} ∈ sets M"
  shows "N {x} = RN_deriv M N x * emeasure M {x}"
proof -
  from ‹{x} ∈ sets M›
  have "density M (RN_deriv M N) {x} = (∫+w. RN_deriv M N x * indicator {x} w ∂M)"
    by (auto simp: indicator_def emeasure_density intro!: nn_integral_cong)
  with x density_RN_deriv[OF ac] show ?thesis
    by (auto simp: max_def)
qed

end