src/HOL/Analysis/Radon_Nikodym.thy
author nipkow
Sat Dec 29 15:43:53 2018 +0100 (6 months ago)
changeset 69529 4ab9657b3257
parent 69517 dc20f278e8f3
child 69683 8b3458ca0762
permissions -rw-r--r--
capitalize proper names in lemma names
     1 (*  Title:      HOL/Analysis/Radon_Nikodym.thy
     2     Author:     Johannes Hölzl, TU München
     3 *)
     4 
     5 section%important \<open>Radon-Nikod{\'y}m Derivative\<close>
     6 
     7 theory Radon_Nikodym
     8 imports Bochner_Integration
     9 begin
    10 
    11 definition%important diff_measure :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> 'a measure"
    12 where
    13   "diff_measure M N = measure_of (space M) (sets M) (\<lambda>A. emeasure M A - emeasure N A)"
    14 
    15 lemma
    16   shows space_diff_measure[simp]: "space (diff_measure M N) = space M"
    17     and sets_diff_measure[simp]: "sets (diff_measure M N) = sets M"
    18   by (auto simp: diff_measure_def)
    19 
    20 lemma%important emeasure_diff_measure:
    21   assumes fin: "finite_measure M" "finite_measure N" and sets_eq: "sets M = sets N"
    22   assumes pos: "\<And>A. A \<in> sets M \<Longrightarrow> emeasure N A \<le> emeasure M A" and A: "A \<in> sets M"
    23   shows "emeasure (diff_measure M N) A = emeasure M A - emeasure N A" (is "_ = ?\<mu> A")
    24   unfolding%unimportant diff_measure_def
    25 proof%unimportant (rule emeasure_measure_of_sigma)
    26   show "sigma_algebra (space M) (sets M)" ..
    27   show "positive (sets M) ?\<mu>"
    28     using pos by (simp add: positive_def)
    29   show "countably_additive (sets M) ?\<mu>"
    30   proof (rule countably_additiveI)
    31     fix A :: "nat \<Rightarrow> _"  assume A: "range A \<subseteq> sets M" and "disjoint_family A"
    32     then have suminf:
    33       "(\<Sum>i. emeasure M (A i)) = emeasure M (\<Union>i. A i)"
    34       "(\<Sum>i. emeasure N (A i)) = emeasure N (\<Union>i. A i)"
    35       by (simp_all add: suminf_emeasure sets_eq)
    36     with A have "(\<Sum>i. emeasure M (A i) - emeasure N (A i)) =
    37       (\<Sum>i. emeasure M (A i)) - (\<Sum>i. emeasure N (A i))"
    38       using fin pos[of "A _"]
    39       by (intro ennreal_suminf_minus)
    40          (auto simp: sets_eq finite_measure.emeasure_eq_measure suminf_emeasure)
    41     then show "(\<Sum>i. emeasure M (A i) - emeasure N (A i)) =
    42       emeasure M (\<Union>i. A i) - emeasure N (\<Union>i. A i) "
    43       by (simp add: suminf)
    44   qed
    45 qed fact
    46 
    47 text \<open>An equivalent characterization of sigma-finite spaces is the existence of integrable
    48 positive functions (or, still equivalently, the existence of a probability measure which is in
    49 the same measure class as the original measure).\<close>
    50 
    51 lemma (in sigma_finite_measure) obtain_positive_integrable_function:
    52   obtains f::"'a \<Rightarrow> real" where
    53     "f \<in> borel_measurable M"
    54     "\<And>x. f x > 0"
    55     "\<And>x. f x \<le> 1"
    56     "integrable M f"
    57 proof -
    58   obtain A :: "nat \<Rightarrow> 'a set" where "range A \<subseteq> sets M" "(\<Union>i. A i) = space M" "\<And>i. emeasure M (A i) \<noteq> \<infinity>"
    59     using sigma_finite by auto
    60   then have [measurable]:"A n \<in> sets M" for n by auto
    61   define g where "g = (\<lambda>x. (\<Sum>n. (1/2)^(Suc n) * indicator (A n) x / (1+ measure M (A n))))"
    62   have [measurable]: "g \<in> borel_measurable M" unfolding g_def by auto
    63   have *: "summable (\<lambda>n. (1/2)^(Suc n) * indicator (A n) x / (1+ measure M (A n)))" for x
    64     apply (rule summable_comparison_test'[of "\<lambda>n. (1/2)^(Suc n)" 0])
    65     using power_half_series summable_def by (auto simp add: indicator_def divide_simps)
    66   have "g x \<le> (\<Sum>n. (1/2)^(Suc n))" for x unfolding g_def
    67     apply (rule suminf_le) using * power_half_series summable_def by (auto simp add: indicator_def divide_simps)
    68   then have g_le_1: "g x \<le> 1" for x using power_half_series sums_unique by fastforce
    69 
    70   have g_pos: "g x > 0" if "x \<in> space M" for x
    71   unfolding g_def proof (subst suminf_pos_iff[OF *[of x]], auto)
    72     obtain i where "x \<in> A i" using \<open>(\<Union>i. A i) = space M\<close> \<open>x \<in> space M\<close> by auto
    73     then have "0 < (1 / 2) ^ Suc i * indicator (A i) x / (1 + Sigma_Algebra.measure M (A i))"
    74       unfolding indicator_def apply (auto simp add: divide_simps) using measure_nonneg[of M "A i"]
    75       by (auto, meson add_nonneg_nonneg linorder_not_le mult_nonneg_nonneg zero_le_numeral zero_le_one zero_le_power)
    76     then show "\<exists>i. 0 < (1 / 2) ^ i * indicator (A i) x / (2 + 2 * Sigma_Algebra.measure M (A i))"
    77       by auto
    78   qed
    79 
    80   have "integrable M g"
    81   unfolding g_def proof (rule integrable_suminf)
    82     fix n
    83     show "integrable M (\<lambda>x. (1 / 2) ^ Suc n * indicator (A n) x / (1 + Sigma_Algebra.measure M (A n)))"
    84       using \<open>emeasure M (A n) \<noteq> \<infinity>\<close>
    85       by (auto intro!: integrable_mult_right integrable_divide_zero integrable_real_indicator simp add: top.not_eq_extremum)
    86   next
    87     show "AE x in M. summable (\<lambda>n. norm ((1 / 2) ^ Suc n * indicator (A n) x / (1 + Sigma_Algebra.measure M (A n))))"
    88       using * by auto
    89     show "summable (\<lambda>n. (\<integral>x. norm ((1 / 2) ^ Suc n * indicator (A n) x / (1 + Sigma_Algebra.measure M (A n))) \<partial>M))"
    90       apply (rule summable_comparison_test'[of "\<lambda>n. (1/2)^(Suc n)" 0], auto)
    91       using power_half_series summable_def apply auto[1]
    92       apply (auto simp add: divide_simps) using measure_nonneg[of M] not_less by fastforce
    93   qed
    94 
    95   define f where "f = (\<lambda>x. if x \<in> space M then g x else 1)"
    96   have "f x > 0" for x unfolding f_def using g_pos by auto
    97   moreover have "f x \<le> 1" for x unfolding f_def using g_le_1 by auto
    98   moreover have [measurable]: "f \<in> borel_measurable M" unfolding f_def by auto
    99   moreover have "integrable M f"
   100     apply (subst integrable_cong[of _ _ _ g]) unfolding f_def using \<open>integrable M g\<close> by auto
   101   ultimately show "(\<And>f. f \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 < f x) \<Longrightarrow> (\<And>x. f x \<le> 1) \<Longrightarrow> integrable M f \<Longrightarrow> thesis) \<Longrightarrow> thesis"
   102     by (meson that)
   103 qed
   104 
   105 lemma%important (in sigma_finite_measure) Ex_finite_integrable_function:
   106   "\<exists>h\<in>borel_measurable M. integral\<^sup>N M h \<noteq> \<infinity> \<and> (\<forall>x\<in>space M. 0 < h x \<and> h x < \<infinity>)"
   107 proof%unimportant -
   108   obtain A :: "nat \<Rightarrow> 'a set" where
   109     range[measurable]: "range A \<subseteq> sets M" and
   110     space: "(\<Union>i. A i) = space M" and
   111     measure: "\<And>i. emeasure M (A i) \<noteq> \<infinity>" and
   112     disjoint: "disjoint_family A"
   113     using sigma_finite_disjoint by blast
   114   let ?B = "\<lambda>i. 2^Suc i * emeasure M (A i)"
   115   have [measurable]: "\<And>i. A i \<in> sets M"
   116     using range by fastforce+
   117   have "\<forall>i. \<exists>x. 0 < x \<and> x < inverse (?B i)"
   118   proof
   119     fix i show "\<exists>x. 0 < x \<and> x < inverse (?B i)"
   120       using measure[of i]
   121       by (auto intro!: dense simp: ennreal_inverse_positive ennreal_mult_eq_top_iff power_eq_top_ennreal)
   122   qed
   123   from choice[OF this] obtain n where n: "\<And>i. 0 < n i"
   124     "\<And>i. n i < inverse (2^Suc i * emeasure M (A i))" by auto
   125   { fix i have "0 \<le> n i" using n(1)[of i] by auto } note pos = this
   126   let ?h = "\<lambda>x. \<Sum>i. n i * indicator (A i) x"
   127   show ?thesis
   128   proof (safe intro!: bexI[of _ ?h] del: notI)
   129     have "integral\<^sup>N M ?h = (\<Sum>i. n i * emeasure M (A i))" using pos
   130       by (simp add: nn_integral_suminf nn_integral_cmult_indicator)
   131     also have "\<dots> \<le> (\<Sum>i. ennreal ((1/2)^Suc i))"
   132     proof (intro suminf_le allI)
   133       fix N
   134       have "n N * emeasure M (A N) \<le> inverse (2^Suc N * emeasure M (A N)) * emeasure M (A N)"
   135         using n[of N] by (intro mult_right_mono) auto
   136       also have "\<dots> = (1/2)^Suc N * (inverse (emeasure M (A N)) * emeasure M (A N))"
   137         using measure[of N]
   138         by (simp add: ennreal_inverse_power divide_ennreal_def ennreal_inverse_mult
   139                       power_eq_top_ennreal less_top[symmetric] mult_ac
   140                  del: power_Suc)
   141       also have "\<dots> \<le> inverse (ennreal 2) ^ Suc N"
   142         using measure[of N]
   143         by (cases "emeasure M (A N)"; cases "emeasure M (A N) = 0")
   144            (auto simp: inverse_ennreal ennreal_mult[symmetric] divide_ennreal_def simp del: power_Suc)
   145       also have "\<dots> = ennreal (inverse 2 ^ Suc N)"
   146         by (subst ennreal_power[symmetric], simp) (simp add: inverse_ennreal)
   147       finally show "n N * emeasure M (A N) \<le> ennreal ((1/2)^Suc N)"
   148         by simp
   149     qed auto
   150     also have "\<dots> < top"
   151       unfolding less_top[symmetric]
   152       by (rule ennreal_suminf_neq_top)
   153          (auto simp: summable_geometric summable_Suc_iff simp del: power_Suc)
   154     finally show "integral\<^sup>N M ?h \<noteq> \<infinity>"
   155       by (auto simp: top_unique)
   156   next
   157     { fix x assume "x \<in> space M"
   158       then obtain i where "x \<in> A i" using space[symmetric] by auto
   159       with disjoint n have "?h x = n i"
   160         by (auto intro!: suminf_cmult_indicator intro: less_imp_le)
   161       then show "0 < ?h x" and "?h x < \<infinity>" using n[of i] by (auto simp: less_top[symmetric]) }
   162     note pos = this
   163   qed measurable
   164 qed
   165 
   166 subsection%important "Absolutely continuous"
   167 
   168 definition%important absolutely_continuous :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> bool" where
   169   "absolutely_continuous M N \<longleftrightarrow> null_sets M \<subseteq> null_sets N"
   170 
   171 lemma absolutely_continuousI_count_space: "absolutely_continuous (count_space A) M"
   172   unfolding absolutely_continuous_def by (auto simp: null_sets_count_space)
   173 
   174 lemma absolutely_continuousI_density:
   175   "f \<in> borel_measurable M \<Longrightarrow> absolutely_continuous M (density M f)"
   176   by (force simp add: absolutely_continuous_def null_sets_density_iff dest: AE_not_in)
   177 
   178 lemma absolutely_continuousI_point_measure_finite:
   179   "(\<And>x. \<lbrakk> x \<in> A ; f x \<le> 0 \<rbrakk> \<Longrightarrow> g x \<le> 0) \<Longrightarrow> absolutely_continuous (point_measure A f) (point_measure A g)"
   180   unfolding absolutely_continuous_def by (force simp: null_sets_point_measure_iff)
   181 
   182 lemma absolutely_continuousD:
   183   "absolutely_continuous M N \<Longrightarrow> A \<in> sets M \<Longrightarrow> emeasure M A = 0 \<Longrightarrow> emeasure N A = 0"
   184   by (auto simp: absolutely_continuous_def null_sets_def)
   185 
   186 lemma%important absolutely_continuous_AE:
   187   assumes sets_eq: "sets M' = sets M"
   188     and "absolutely_continuous M M'" "AE x in M. P x"
   189    shows "AE x in M'. P x"
   190 proof%unimportant -
   191   from \<open>AE x in M. P x\<close> obtain N where N: "N \<in> null_sets M" "{x\<in>space M. \<not> P x} \<subseteq> N"
   192     unfolding eventually_ae_filter by auto
   193   show "AE x in M'. P x"
   194   proof (rule AE_I')
   195     show "{x\<in>space M'. \<not> P x} \<subseteq> N" using sets_eq_imp_space_eq[OF sets_eq] N(2) by simp
   196     from \<open>absolutely_continuous M M'\<close> show "N \<in> null_sets M'"
   197       using N unfolding absolutely_continuous_def sets_eq null_sets_def by auto
   198   qed
   199 qed
   200 
   201 subsection%important "Existence of the Radon-Nikodym derivative"
   202 
   203 lemma%important
   204  (in finite_measure) Radon_Nikodym_finite_measure:
   205   assumes "finite_measure N" and sets_eq[simp]: "sets N = sets M"
   206   assumes "absolutely_continuous M N"
   207   shows "\<exists>f \<in> borel_measurable M. density M f = N"
   208 proof%unimportant -
   209   interpret N: finite_measure N by fact
   210   define G where "G = {g \<in> borel_measurable M. \<forall>A\<in>sets M. (\<integral>\<^sup>+x. g x * indicator A x \<partial>M) \<le> N A}"
   211   have [measurable_dest]: "f \<in> G \<Longrightarrow> f \<in> borel_measurable M"
   212     and G_D: "\<And>A. f \<in> G \<Longrightarrow> A \<in> sets M \<Longrightarrow> (\<integral>\<^sup>+x. f x * indicator A x \<partial>M) \<le> N A" for f
   213     by (auto simp: G_def)
   214   note this[measurable_dest]
   215   have "(\<lambda>x. 0) \<in> G" unfolding G_def by auto
   216   hence "G \<noteq> {}" by auto
   217   { fix f g assume f[measurable]: "f \<in> G" and g[measurable]: "g \<in> G"
   218     have "(\<lambda>x. max (g x) (f x)) \<in> G" (is "?max \<in> G") unfolding G_def
   219     proof safe
   220       let ?A = "{x \<in> space M. f x \<le> g x}"
   221       have "?A \<in> sets M" using f g unfolding G_def by auto
   222       fix A assume [measurable]: "A \<in> sets M"
   223       have union: "((?A \<inter> A) \<union> ((space M - ?A) \<inter> A)) = A"
   224         using sets.sets_into_space[OF \<open>A \<in> sets M\<close>] by auto
   225       have "\<And>x. x \<in> space M \<Longrightarrow> max (g x) (f x) * indicator A x =
   226         g x * indicator (?A \<inter> A) x + f x * indicator ((space M - ?A) \<inter> A) x"
   227         by (auto simp: indicator_def max_def)
   228       hence "(\<integral>\<^sup>+x. max (g x) (f x) * indicator A x \<partial>M) =
   229         (\<integral>\<^sup>+x. g x * indicator (?A \<inter> A) x \<partial>M) +
   230         (\<integral>\<^sup>+x. f x * indicator ((space M - ?A) \<inter> A) x \<partial>M)"
   231         by (auto cong: nn_integral_cong intro!: nn_integral_add)
   232       also have "\<dots> \<le> N (?A \<inter> A) + N ((space M - ?A) \<inter> A)"
   233         using f g unfolding G_def by (auto intro!: add_mono)
   234       also have "\<dots> = N A"
   235         using union by (subst plus_emeasure) auto
   236       finally show "(\<integral>\<^sup>+x. max (g x) (f x) * indicator A x \<partial>M) \<le> N A" .
   237     qed auto }
   238   note max_in_G = this
   239   { fix f assume  "incseq f" and f: "\<And>i. f i \<in> G"
   240     then have [measurable]: "\<And>i. f i \<in> borel_measurable M" by (auto simp: G_def)
   241     have "(\<lambda>x. SUP i. f i x) \<in> G" unfolding G_def
   242     proof safe
   243       show "(\<lambda>x. SUP i. f i x) \<in> borel_measurable M" by measurable
   244     next
   245       fix A assume "A \<in> sets M"
   246       have "(\<integral>\<^sup>+x. (SUP i. f i x) * indicator A x \<partial>M) =
   247         (\<integral>\<^sup>+x. (SUP i. f i x * indicator A x) \<partial>M)"
   248         by (intro nn_integral_cong) (simp split: split_indicator)
   249       also have "\<dots> = (SUP i. (\<integral>\<^sup>+x. f i x * indicator A x \<partial>M))"
   250         using \<open>incseq f\<close> f \<open>A \<in> sets M\<close>
   251         by (intro nn_integral_monotone_convergence_SUP)
   252            (auto simp: G_def incseq_Suc_iff le_fun_def split: split_indicator)
   253       finally show "(\<integral>\<^sup>+x. (SUP i. f i x) * indicator A x \<partial>M) \<le> N A"
   254         using f \<open>A \<in> sets M\<close> by (auto intro!: SUP_least simp: G_D)
   255     qed }
   256   note SUP_in_G = this
   257   let ?y = "SUP g \<in> G. integral\<^sup>N M g"
   258   have y_le: "?y \<le> N (space M)" unfolding G_def
   259   proof (safe intro!: SUP_least)
   260     fix g assume "\<forall>A\<in>sets M. (\<integral>\<^sup>+x. g x * indicator A x \<partial>M) \<le> N A"
   261     from this[THEN bspec, OF sets.top] show "integral\<^sup>N M g \<le> N (space M)"
   262       by (simp cong: nn_integral_cong)
   263   qed
   264   from ennreal_SUP_countable_SUP [OF \<open>G \<noteq> {}\<close>, of "integral\<^sup>N M"] guess ys .. note ys = this
   265   then have "\<forall>n. \<exists>g. g\<in>G \<and> integral\<^sup>N M g = ys n"
   266   proof safe
   267     fix n assume "range ys \<subseteq> integral\<^sup>N M ` G"
   268     hence "ys n \<in> integral\<^sup>N M ` G" by auto
   269     thus "\<exists>g. g\<in>G \<and> integral\<^sup>N M g = ys n" by auto
   270   qed
   271   from choice[OF this] obtain gs where "\<And>i. gs i \<in> G" "\<And>n. integral\<^sup>N M (gs n) = ys n" by auto
   272   hence y_eq: "?y = (SUP i. integral\<^sup>N M (gs i))" using ys by auto
   273   let ?g = "\<lambda>i x. Max ((\<lambda>n. gs n x) ` {..i})"
   274   define f where [abs_def]: "f x = (SUP i. ?g i x)" for x
   275   let ?F = "\<lambda>A x. f x * indicator A x"
   276   have gs_not_empty: "\<And>i x. (\<lambda>n. gs n x) ` {..i} \<noteq> {}" by auto
   277   { fix i have "?g i \<in> G"
   278     proof (induct i)
   279       case 0 thus ?case by simp fact
   280     next
   281       case (Suc i)
   282       with Suc gs_not_empty \<open>gs (Suc i) \<in> G\<close> show ?case
   283         by (auto simp add: atMost_Suc intro!: max_in_G)
   284     qed }
   285   note g_in_G = this
   286   have "incseq ?g" using gs_not_empty
   287     by (auto intro!: incseq_SucI le_funI simp add: atMost_Suc)
   288 
   289   from SUP_in_G[OF this g_in_G] have [measurable]: "f \<in> G" unfolding f_def .
   290   then have [measurable]: "f \<in> borel_measurable M" unfolding G_def by auto
   291 
   292   have "integral\<^sup>N M f = (SUP i. integral\<^sup>N M (?g i))" unfolding f_def
   293     using g_in_G \<open>incseq ?g\<close> by (auto intro!: nn_integral_monotone_convergence_SUP simp: G_def)
   294   also have "\<dots> = ?y"
   295   proof (rule antisym)
   296     show "(SUP i. integral\<^sup>N M (?g i)) \<le> ?y"
   297       using g_in_G by (auto intro: SUP_mono)
   298     show "?y \<le> (SUP i. integral\<^sup>N M (?g i))" unfolding y_eq
   299       by (auto intro!: SUP_mono nn_integral_mono Max_ge)
   300   qed
   301   finally have int_f_eq_y: "integral\<^sup>N M f = ?y" .
   302 
   303   have upper_bound: "\<forall>A\<in>sets M. N A \<le> density M f A"
   304   proof (rule ccontr)
   305     assume "\<not> ?thesis"
   306     then obtain A where A[measurable]: "A \<in> sets M" and f_less_N: "density M f A < N A"
   307       by (auto simp: not_le)
   308     then have pos_A: "0 < M A"
   309       using \<open>absolutely_continuous M N\<close>[THEN absolutely_continuousD, OF A]
   310       by (auto simp: zero_less_iff_neq_zero)
   311 
   312     define b where "b = (N A - density M f A) / M A / 2"
   313     with f_less_N pos_A have "0 < b" "b \<noteq> top"
   314       by (auto intro!: diff_gr0_ennreal simp: zero_less_iff_neq_zero diff_eq_0_iff_ennreal ennreal_divide_eq_top_iff)
   315 
   316     let ?f = "\<lambda>x. f x + b"
   317     have "nn_integral M f \<noteq> top"
   318       using \<open>f \<in> G\<close>[THEN G_D, of "space M"] by (auto simp: top_unique cong: nn_integral_cong)
   319     with \<open>b \<noteq> top\<close> interpret Mf: finite_measure "density M ?f"
   320       by (intro finite_measureI)
   321          (auto simp: field_simps mult_indicator_subset ennreal_mult_eq_top_iff
   322                      emeasure_density nn_integral_cmult_indicator nn_integral_add
   323                cong: nn_integral_cong)
   324 
   325     from unsigned_Hahn_decomposition[of "density M ?f" N A]
   326     obtain Y where [measurable]: "Y \<in> sets M" and [simp]: "Y \<subseteq> A"
   327        and Y1: "\<And>C. C \<in> sets M \<Longrightarrow> C \<subseteq> Y \<Longrightarrow> density M ?f C \<le> N C"
   328        and Y2: "\<And>C. C \<in> sets M \<Longrightarrow> C \<subseteq> A \<Longrightarrow> C \<inter> Y = {} \<Longrightarrow> N C \<le> density M ?f C"
   329        by auto
   330 
   331     let ?f' = "\<lambda>x. f x + b * indicator Y x"
   332     have "M Y \<noteq> 0"
   333     proof
   334       assume "M Y = 0"
   335       then have "N Y = 0"
   336         using \<open>absolutely_continuous M N\<close>[THEN absolutely_continuousD, of Y] by auto
   337       then have "N A = N (A - Y)"
   338         by (subst emeasure_Diff) auto
   339       also have "\<dots> \<le> density M ?f (A - Y)"
   340         by (rule Y2) auto
   341       also have "\<dots> \<le> density M ?f A - density M ?f Y"
   342         by (subst emeasure_Diff) auto
   343       also have "\<dots> \<le> density M ?f A - 0"
   344         by (intro ennreal_minus_mono) auto
   345       also have "density M ?f A = b * M A + density M f A"
   346         by (simp add: emeasure_density field_simps mult_indicator_subset nn_integral_add nn_integral_cmult_indicator)
   347       also have "\<dots> < N A"
   348         using f_less_N pos_A
   349         by (cases "density M f A"; cases "M A"; cases "N A")
   350            (auto simp: b_def ennreal_less_iff ennreal_minus divide_ennreal ennreal_numeral[symmetric]
   351                        ennreal_plus[symmetric] ennreal_mult[symmetric] field_simps
   352                     simp del: ennreal_numeral ennreal_plus)
   353       finally show False
   354         by simp
   355     qed
   356     then have "nn_integral M f < nn_integral M ?f'"
   357       using \<open>0 < b\<close> \<open>nn_integral M f \<noteq> top\<close>
   358       by (simp add: nn_integral_add nn_integral_cmult_indicator ennreal_zero_less_mult_iff zero_less_iff_neq_zero)
   359     moreover
   360     have "?f' \<in> G"
   361       unfolding G_def
   362     proof safe
   363       fix X assume [measurable]: "X \<in> sets M"
   364       have "(\<integral>\<^sup>+ x. ?f' x * indicator X x \<partial>M) = density M f (X - Y) + density M ?f (X \<inter> Y)"
   365         by (auto simp add: emeasure_density nn_integral_add[symmetric] split: split_indicator intro!: nn_integral_cong)
   366       also have "\<dots> \<le> N (X - Y) + N (X \<inter> Y)"
   367         using G_D[OF \<open>f \<in> G\<close>] by (intro add_mono Y1) (auto simp: emeasure_density)
   368       also have "\<dots> = N X"
   369         by (subst plus_emeasure) (auto intro!: arg_cong2[where f=emeasure])
   370       finally show "(\<integral>\<^sup>+ x. ?f' x * indicator X x \<partial>M) \<le> N X" .
   371     qed simp
   372     then have "nn_integral M ?f' \<le> ?y"
   373       by (rule SUP_upper)
   374     ultimately show False
   375       by (simp add: int_f_eq_y)
   376   qed
   377   show ?thesis
   378   proof (intro bexI[of _ f] measure_eqI conjI antisym)
   379     fix A assume "A \<in> sets (density M f)" then show "emeasure (density M f) A \<le> emeasure N A"
   380       by (auto simp: emeasure_density intro!: G_D[OF \<open>f \<in> G\<close>])
   381   next
   382     fix A assume A: "A \<in> sets (density M f)" then show "emeasure N A \<le> emeasure (density M f) A"
   383       using upper_bound by auto
   384   qed auto
   385 qed
   386 
   387 lemma%important (in finite_measure) split_space_into_finite_sets_and_rest:
   388   assumes ac: "absolutely_continuous M N" and sets_eq[simp]: "sets N = sets M"
   389   shows "\<exists>B::nat\<Rightarrow>'a set. disjoint_family B \<and> range B \<subseteq> sets M \<and> (\<forall>i. N (B i) \<noteq> \<infinity>) \<and>
   390     (\<forall>A\<in>sets M. A \<inter> (\<Union>i. B i) = {} \<longrightarrow> (emeasure M A = 0 \<and> N A = 0) \<or> (emeasure M A > 0 \<and> N A = \<infinity>))"
   391 proof%unimportant -
   392   let ?Q = "{Q\<in>sets M. N Q \<noteq> \<infinity>}"
   393   let ?a = "SUP Q\<in>?Q. emeasure M Q"
   394   have "{} \<in> ?Q" by auto
   395   then have Q_not_empty: "?Q \<noteq> {}" by blast
   396   have "?a \<le> emeasure M (space M)" using sets.sets_into_space
   397     by (auto intro!: SUP_least emeasure_mono)
   398   then have "?a \<noteq> \<infinity>"
   399     using finite_emeasure_space
   400     by (auto simp: less_top[symmetric] top_unique simp del: SUP_eq_top_iff Sup_eq_top_iff)
   401   from ennreal_SUP_countable_SUP [OF Q_not_empty, of "emeasure M"]
   402   obtain Q'' where "range Q'' \<subseteq> emeasure M ` ?Q" and a: "?a = (SUP i::nat. Q'' i)"
   403     by auto
   404   then have "\<forall>i. \<exists>Q'. Q'' i = emeasure M Q' \<and> Q' \<in> ?Q" by auto
   405   from choice[OF this] obtain Q' where Q': "\<And>i. Q'' i = emeasure M (Q' i)" "\<And>i. Q' i \<in> ?Q"
   406     by auto
   407   then have a_Lim: "?a = (SUP i. emeasure M (Q' i))" using a by simp
   408   let ?O = "\<lambda>n. \<Union>i\<le>n. Q' i"
   409   have Union: "(SUP i. emeasure M (?O i)) = emeasure M (\<Union>i. ?O i)"
   410   proof (rule SUP_emeasure_incseq[of ?O])
   411     show "range ?O \<subseteq> sets M" using Q' by auto
   412     show "incseq ?O" by (fastforce intro!: incseq_SucI)
   413   qed
   414   have Q'_sets[measurable]: "\<And>i. Q' i \<in> sets M" using Q' by auto
   415   have O_sets: "\<And>i. ?O i \<in> sets M" using Q' by auto
   416   then have O_in_G: "\<And>i. ?O i \<in> ?Q"
   417   proof (safe del: notI)
   418     fix i have "Q' ` {..i} \<subseteq> sets M" using Q' by auto
   419     then have "N (?O i) \<le> (\<Sum>i\<le>i. N (Q' i))"
   420       by (simp add: emeasure_subadditive_finite)
   421     also have "\<dots> < \<infinity>" using Q' by (simp add: less_top)
   422     finally show "N (?O i) \<noteq> \<infinity>" by simp
   423   qed auto
   424   have O_mono: "\<And>n. ?O n \<subseteq> ?O (Suc n)" by fastforce
   425   have a_eq: "?a = emeasure M (\<Union>i. ?O i)" unfolding Union[symmetric]
   426   proof (rule antisym)
   427     show "?a \<le> (SUP i. emeasure M (?O i))" unfolding a_Lim
   428       using Q' by (auto intro!: SUP_mono emeasure_mono)
   429     show "(SUP i. emeasure M (?O i)) \<le> ?a"
   430     proof (safe intro!: Sup_mono, unfold bex_simps)
   431       fix i
   432       have *: "(\<Union>(Q' ` {..i})) = ?O i" by auto
   433       then show "\<exists>x. (x \<in> sets M \<and> N x \<noteq> \<infinity>) \<and>
   434         emeasure M (\<Union>(Q' ` {..i})) \<le> emeasure M x"
   435         using O_in_G[of i] by (auto intro!: exI[of _ "?O i"])
   436     qed
   437   qed
   438   let ?O_0 = "(\<Union>i. ?O i)"
   439   have "?O_0 \<in> sets M" using Q' by auto
   440   have "disjointed Q' i \<in> sets M" for i
   441     using sets.range_disjointed_sets[of Q' M] using Q'_sets by (auto simp: subset_eq)
   442   note Q_sets = this
   443   show ?thesis
   444   proof (intro bexI exI conjI ballI impI allI)
   445     show "disjoint_family (disjointed Q')"
   446       by (rule disjoint_family_disjointed)
   447     show "range (disjointed Q') \<subseteq> sets M"
   448       using Q'_sets by (intro sets.range_disjointed_sets) auto
   449     { fix A assume A: "A \<in> sets M" "A \<inter> (\<Union>i. disjointed Q' i) = {}"
   450       then have A1: "A \<inter> (\<Union>i. Q' i) = {}"
   451         unfolding UN_disjointed_eq by auto
   452       show "emeasure M A = 0 \<and> N A = 0 \<or> 0 < emeasure M A \<and> N A = \<infinity>"
   453       proof (rule disjCI, simp)
   454         assume *: "emeasure M A = 0 \<or> N A \<noteq> top"
   455         show "emeasure M A = 0 \<and> N A = 0"
   456         proof (cases "emeasure M A = 0")
   457           case True
   458           with ac A have "N A = 0"
   459             unfolding absolutely_continuous_def by auto
   460           with True show ?thesis by simp
   461         next
   462           case False
   463           with * have "N A \<noteq> \<infinity>" by auto
   464           with A have "emeasure M ?O_0 + emeasure M A = emeasure M (?O_0 \<union> A)"
   465             using Q' A1 by (auto intro!: plus_emeasure simp: set_eq_iff)
   466           also have "\<dots> = (SUP i. emeasure M (?O i \<union> A))"
   467           proof (rule SUP_emeasure_incseq[of "\<lambda>i. ?O i \<union> A", symmetric, simplified])
   468             show "range (\<lambda>i. ?O i \<union> A) \<subseteq> sets M"
   469               using \<open>N A \<noteq> \<infinity>\<close> O_sets A by auto
   470           qed (fastforce intro!: incseq_SucI)
   471           also have "\<dots> \<le> ?a"
   472           proof (safe intro!: SUP_least)
   473             fix i have "?O i \<union> A \<in> ?Q"
   474             proof (safe del: notI)
   475               show "?O i \<union> A \<in> sets M" using O_sets A by auto
   476               from O_in_G[of i] have "N (?O i \<union> A) \<le> N (?O i) + N A"
   477                 using emeasure_subadditive[of "?O i" N A] A O_sets by auto
   478               with O_in_G[of i] show "N (?O i \<union> A) \<noteq> \<infinity>"
   479                 using \<open>N A \<noteq> \<infinity>\<close> by (auto simp: top_unique)
   480             qed
   481             then show "emeasure M (?O i \<union> A) \<le> ?a" by (rule SUP_upper)
   482           qed
   483           finally have "emeasure M A = 0"
   484             unfolding a_eq using measure_nonneg[of M A] by (simp add: emeasure_eq_measure)
   485           with \<open>emeasure M A \<noteq> 0\<close> show ?thesis by auto
   486         qed
   487       qed }
   488     { fix i
   489       have "N (disjointed Q' i) \<le> N (Q' i)"
   490         by (auto intro!: emeasure_mono simp: disjointed_def)
   491       then show "N (disjointed Q' i) \<noteq> \<infinity>"
   492         using Q'(2)[of i] by (auto simp: top_unique) }
   493   qed
   494 qed
   495 
   496 lemma%important (in finite_measure) Radon_Nikodym_finite_measure_infinite:
   497   assumes "absolutely_continuous M N" and sets_eq: "sets N = sets M"
   498   shows "\<exists>f\<in>borel_measurable M. density M f = N"
   499 proof%unimportant -
   500   from split_space_into_finite_sets_and_rest[OF assms]
   501   obtain Q :: "nat \<Rightarrow> 'a set"
   502     where Q: "disjoint_family Q" "range Q \<subseteq> sets M"
   503     and in_Q0: "\<And>A. A \<in> sets M \<Longrightarrow> A \<inter> (\<Union>i. Q i) = {} \<Longrightarrow> emeasure M A = 0 \<and> N A = 0 \<or> 0 < emeasure M A \<and> N A = \<infinity>"
   504     and Q_fin: "\<And>i. N (Q i) \<noteq> \<infinity>" by force
   505   from Q have Q_sets: "\<And>i. Q i \<in> sets M" by auto
   506   let ?N = "\<lambda>i. density N (indicator (Q i))" and ?M = "\<lambda>i. density M (indicator (Q i))"
   507   have "\<forall>i. \<exists>f\<in>borel_measurable (?M i). density (?M i) f = ?N i"
   508   proof (intro allI finite_measure.Radon_Nikodym_finite_measure)
   509     fix i
   510     from Q show "finite_measure (?M i)"
   511       by (auto intro!: finite_measureI cong: nn_integral_cong
   512                simp add: emeasure_density subset_eq sets_eq)
   513     from Q have "emeasure (?N i) (space N) = emeasure N (Q i)"
   514       by (simp add: sets_eq[symmetric] emeasure_density subset_eq cong: nn_integral_cong)
   515     with Q_fin show "finite_measure (?N i)"
   516       by (auto intro!: finite_measureI)
   517     show "sets (?N i) = sets (?M i)" by (simp add: sets_eq)
   518     have [measurable]: "\<And>A. A \<in> sets M \<Longrightarrow> A \<in> sets N" by (simp add: sets_eq)
   519     show "absolutely_continuous (?M i) (?N i)"
   520       using \<open>absolutely_continuous M N\<close> \<open>Q i \<in> sets M\<close>
   521       by (auto simp: absolutely_continuous_def null_sets_density_iff sets_eq
   522                intro!: absolutely_continuous_AE[OF sets_eq])
   523   qed
   524   from choice[OF this[unfolded Bex_def]]
   525   obtain f where borel: "\<And>i. f i \<in> borel_measurable M" "\<And>i x. 0 \<le> f i x"
   526     and f_density: "\<And>i. density (?M i) (f i) = ?N i"
   527     by force
   528   { fix A i assume A: "A \<in> sets M"
   529     with Q borel have "(\<integral>\<^sup>+x. f i x * indicator (Q i \<inter> A) x \<partial>M) = emeasure (density (?M i) (f i)) A"
   530       by (auto simp add: emeasure_density nn_integral_density subset_eq
   531                intro!: nn_integral_cong split: split_indicator)
   532     also have "\<dots> = emeasure N (Q i \<inter> A)"
   533       using A Q by (simp add: f_density emeasure_restricted subset_eq sets_eq)
   534     finally have "emeasure N (Q i \<inter> A) = (\<integral>\<^sup>+x. f i x * indicator (Q i \<inter> A) x \<partial>M)" .. }
   535   note integral_eq = this
   536   let ?f = "\<lambda>x. (\<Sum>i. f i x * indicator (Q i) x) + \<infinity> * indicator (space M - (\<Union>i. Q i)) x"
   537   show ?thesis
   538   proof (safe intro!: bexI[of _ ?f])
   539     show "?f \<in> borel_measurable M" using borel Q_sets
   540       by (auto intro!: measurable_If)
   541     show "density M ?f = N"
   542     proof (rule measure_eqI)
   543       fix A assume "A \<in> sets (density M ?f)"
   544       then have "A \<in> sets M" by simp
   545       have Qi: "\<And>i. Q i \<in> sets M" using Q by auto
   546       have [intro,simp]: "\<And>i. (\<lambda>x. f i x * indicator (Q i \<inter> A) x) \<in> borel_measurable M"
   547         "\<And>i. AE x in M. 0 \<le> f i x * indicator (Q i \<inter> A) x"
   548         using borel Qi \<open>A \<in> sets M\<close> by auto
   549       have "(\<integral>\<^sup>+x. ?f x * indicator A x \<partial>M) = (\<integral>\<^sup>+x. (\<Sum>i. f i x * indicator (Q i \<inter> A) x) + \<infinity> * indicator ((space M - (\<Union>i. Q i)) \<inter> A) x \<partial>M)"
   550         using borel by (intro nn_integral_cong) (auto simp: indicator_def)
   551       also have "\<dots> = (\<integral>\<^sup>+x. (\<Sum>i. f i x * indicator (Q i \<inter> A) x) \<partial>M) + \<infinity> * emeasure M ((space M - (\<Union>i. Q i)) \<inter> A)"
   552         using borel Qi \<open>A \<in> sets M\<close>
   553         by (subst nn_integral_add)
   554            (auto simp add: nn_integral_cmult_indicator sets.Int intro!: suminf_0_le)
   555       also have "\<dots> = (\<Sum>i. N (Q i \<inter> A)) + \<infinity> * emeasure M ((space M - (\<Union>i. Q i)) \<inter> A)"
   556         by (subst integral_eq[OF \<open>A \<in> sets M\<close>], subst nn_integral_suminf) auto
   557       finally have "(\<integral>\<^sup>+x. ?f x * indicator A x \<partial>M) = (\<Sum>i. N (Q i \<inter> A)) + \<infinity> * emeasure M ((space M - (\<Union>i. Q i)) \<inter> A)" .
   558       moreover have "(\<Sum>i. N (Q i \<inter> A)) = N ((\<Union>i. Q i) \<inter> A)"
   559         using Q Q_sets \<open>A \<in> sets M\<close>
   560         by (subst suminf_emeasure) (auto simp: disjoint_family_on_def sets_eq)
   561       moreover
   562       have "(space M - (\<Union>x. Q x)) \<inter> A \<inter> (\<Union>x. Q x) = {}"
   563         by auto
   564       then have "\<infinity> * emeasure M ((space M - (\<Union>i. Q i)) \<inter> A) = N ((space M - (\<Union>i. Q i)) \<inter> A)"
   565         using in_Q0[of "(space M - (\<Union>i. Q i)) \<inter> A"] \<open>A \<in> sets M\<close> Q by (auto simp: ennreal_top_mult)
   566       moreover have "(space M - (\<Union>i. Q i)) \<inter> A \<in> sets M" "((\<Union>i. Q i) \<inter> A) \<in> sets M"
   567         using Q_sets \<open>A \<in> sets M\<close> by auto
   568       moreover have "((\<Union>i. Q i) \<inter> A) \<union> ((space M - (\<Union>i. Q i)) \<inter> A) = A" "((\<Union>i. Q i) \<inter> A) \<inter> ((space M - (\<Union>i. Q i)) \<inter> A) = {}"
   569         using \<open>A \<in> sets M\<close> sets.sets_into_space by auto
   570       ultimately have "N A = (\<integral>\<^sup>+x. ?f x * indicator A x \<partial>M)"
   571         using plus_emeasure[of "(\<Union>i. Q i) \<inter> A" N "(space M - (\<Union>i. Q i)) \<inter> A"] by (simp add: sets_eq)
   572       with \<open>A \<in> sets M\<close> borel Q show "emeasure (density M ?f) A = N A"
   573         by (auto simp: subset_eq emeasure_density)
   574     qed (simp add: sets_eq)
   575   qed
   576 qed
   577 
   578 lemma (in sigma_finite_measure) Radon_Nikodym:
   579   assumes ac: "absolutely_continuous M N" assumes sets_eq: "sets N = sets M"
   580   shows "\<exists>f \<in> borel_measurable M. density M f = N"
   581 proof -
   582   from Ex_finite_integrable_function
   583   obtain h where finite: "integral\<^sup>N M h \<noteq> \<infinity>" and
   584     borel: "h \<in> borel_measurable M" and
   585     nn: "\<And>x. 0 \<le> h x" and
   586     pos: "\<And>x. x \<in> space M \<Longrightarrow> 0 < h x" and
   587     "\<And>x. x \<in> space M \<Longrightarrow> h x < \<infinity>" by auto
   588   let ?T = "\<lambda>A. (\<integral>\<^sup>+x. h x * indicator A x \<partial>M)"
   589   let ?MT = "density M h"
   590   from borel finite nn interpret T: finite_measure ?MT
   591     by (auto intro!: finite_measureI cong: nn_integral_cong simp: emeasure_density)
   592   have "absolutely_continuous ?MT N" "sets N = sets ?MT"
   593   proof (unfold absolutely_continuous_def, safe)
   594     fix A assume "A \<in> null_sets ?MT"
   595     with borel have "A \<in> sets M" "AE x in M. x \<in> A \<longrightarrow> h x \<le> 0"
   596       by (auto simp add: null_sets_density_iff)
   597     with pos sets.sets_into_space have "AE x in M. x \<notin> A"
   598       by (elim eventually_mono) (auto simp: not_le[symmetric])
   599     then have "A \<in> null_sets M"
   600       using \<open>A \<in> sets M\<close> by (simp add: AE_iff_null_sets)
   601     with ac show "A \<in> null_sets N"
   602       by (auto simp: absolutely_continuous_def)
   603   qed (auto simp add: sets_eq)
   604   from T.Radon_Nikodym_finite_measure_infinite[OF this]
   605   obtain f where f_borel: "f \<in> borel_measurable M" "density ?MT f = N" by auto
   606   with nn borel show ?thesis
   607     by (auto intro!: bexI[of _ "\<lambda>x. h x * f x"] simp: density_density_eq)
   608 qed
   609 
   610 subsection%important \<open>Uniqueness of densities\<close>
   611 
   612 lemma%important finite_density_unique:
   613   assumes borel: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   614   assumes pos: "AE x in M. 0 \<le> f x" "AE x in M. 0 \<le> g x"
   615   and fin: "integral\<^sup>N M f \<noteq> \<infinity>"
   616   shows "density M f = density M g \<longleftrightarrow> (AE x in M. f x = g x)"
   617 proof%unimportant (intro iffI ballI)
   618   fix A assume eq: "AE x in M. f x = g x"
   619   with borel show "density M f = density M g"
   620     by (auto intro: density_cong)
   621 next
   622   let ?P = "\<lambda>f A. \<integral>\<^sup>+ x. f x * indicator A x \<partial>M"
   623   assume "density M f = density M g"
   624   with borel have eq: "\<forall>A\<in>sets M. ?P f A = ?P g A"
   625     by (simp add: emeasure_density[symmetric])
   626   from this[THEN bspec, OF sets.top] fin
   627   have g_fin: "integral\<^sup>N M g \<noteq> \<infinity>" by (simp cong: nn_integral_cong)
   628   { fix f g assume borel: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   629       and pos: "AE x in M. 0 \<le> f x" "AE x in M. 0 \<le> g x"
   630       and g_fin: "integral\<^sup>N M g \<noteq> \<infinity>" and eq: "\<forall>A\<in>sets M. ?P f A = ?P g A"
   631     let ?N = "{x\<in>space M. g x < f x}"
   632     have N: "?N \<in> sets M" using borel by simp
   633     have "?P g ?N \<le> integral\<^sup>N M g" using pos
   634       by (intro nn_integral_mono_AE) (auto split: split_indicator)
   635     then have Pg_fin: "?P g ?N \<noteq> \<infinity>" using g_fin by (auto simp: top_unique)
   636     have "?P (\<lambda>x. (f x - g x)) ?N = (\<integral>\<^sup>+x. f x * indicator ?N x - g x * indicator ?N x \<partial>M)"
   637       by (auto intro!: nn_integral_cong simp: indicator_def)
   638     also have "\<dots> = ?P f ?N - ?P g ?N"
   639     proof (rule nn_integral_diff)
   640       show "(\<lambda>x. f x * indicator ?N x) \<in> borel_measurable M" "(\<lambda>x. g x * indicator ?N x) \<in> borel_measurable M"
   641         using borel N by auto
   642       show "AE x in M. g x * indicator ?N x \<le> f x * indicator ?N x"
   643         using pos by (auto split: split_indicator)
   644     qed fact
   645     also have "\<dots> = 0"
   646       unfolding eq[THEN bspec, OF N] using Pg_fin by auto
   647     finally have "AE x in M. f x \<le> g x"
   648       using pos borel nn_integral_PInf_AE[OF borel(2) g_fin]
   649       by (subst (asm) nn_integral_0_iff_AE)
   650          (auto split: split_indicator simp: not_less ennreal_minus_eq_0) }
   651   from this[OF borel pos g_fin eq] this[OF borel(2,1) pos(2,1) fin] eq
   652   show "AE x in M. f x = g x" by auto
   653 qed
   654 
   655 lemma%important (in finite_measure) density_unique_finite_measure:
   656   assumes borel: "f \<in> borel_measurable M" "f' \<in> borel_measurable M"
   657   assumes pos: "AE x in M. 0 \<le> f x" "AE x in M. 0 \<le> f' x"
   658   assumes f: "\<And>A. A \<in> sets M \<Longrightarrow> (\<integral>\<^sup>+x. f x * indicator A x \<partial>M) = (\<integral>\<^sup>+x. f' x * indicator A x \<partial>M)"
   659     (is "\<And>A. A \<in> sets M \<Longrightarrow> ?P f A = ?P f' A")
   660   shows "AE x in M. f x = f' x"
   661 proof%unimportant -
   662   let ?D = "\<lambda>f. density M f"
   663   let ?N = "\<lambda>A. ?P f A" and ?N' = "\<lambda>A. ?P f' A"
   664   let ?f = "\<lambda>A x. f x * indicator A x" and ?f' = "\<lambda>A x. f' x * indicator A x"
   665 
   666   have ac: "absolutely_continuous M (density M f)" "sets (density M f) = sets M"
   667     using borel by (auto intro!: absolutely_continuousI_density)
   668   from split_space_into_finite_sets_and_rest[OF this]
   669   obtain Q :: "nat \<Rightarrow> 'a set"
   670     where Q: "disjoint_family Q" "range Q \<subseteq> sets M"
   671     and in_Q0: "\<And>A. A \<in> sets M \<Longrightarrow> A \<inter> (\<Union>i. Q i) = {} \<Longrightarrow> emeasure M A = 0 \<and> ?D f A = 0 \<or> 0 < emeasure M A \<and> ?D f A = \<infinity>"
   672     and Q_fin: "\<And>i. ?D f (Q i) \<noteq> \<infinity>" by force
   673   with borel pos have in_Q0: "\<And>A. A \<in> sets M \<Longrightarrow> A \<inter> (\<Union>i. Q i) = {} \<Longrightarrow> emeasure M A = 0 \<and> ?N A = 0 \<or> 0 < emeasure M A \<and> ?N A = \<infinity>"
   674     and Q_fin: "\<And>i. ?N (Q i) \<noteq> \<infinity>" by (auto simp: emeasure_density subset_eq)
   675 
   676   from Q have Q_sets[measurable]: "\<And>i. Q i \<in> sets M" by auto
   677   let ?D = "{x\<in>space M. f x \<noteq> f' x}"
   678   have "?D \<in> sets M" using borel by auto
   679   have *: "\<And>i x A. \<And>y::ennreal. y * indicator (Q i) x * indicator A x = y * indicator (Q i \<inter> A) x"
   680     unfolding indicator_def by auto
   681   have "\<forall>i. AE x in M. ?f (Q i) x = ?f' (Q i) x" using borel Q_fin Q pos
   682     by (intro finite_density_unique[THEN iffD1] allI)
   683        (auto intro!: f measure_eqI simp: emeasure_density * subset_eq)
   684   moreover have "AE x in M. ?f (space M - (\<Union>i. Q i)) x = ?f' (space M - (\<Union>i. Q i)) x"
   685   proof (rule AE_I')
   686     { fix f :: "'a \<Rightarrow> ennreal" assume borel: "f \<in> borel_measurable M"
   687         and eq: "\<And>A. A \<in> sets M \<Longrightarrow> ?N A = (\<integral>\<^sup>+x. f x * indicator A x \<partial>M)"
   688       let ?A = "\<lambda>i. (space M - (\<Union>i. Q i)) \<inter> {x \<in> space M. f x < (i::nat)}"
   689       have "(\<Union>i. ?A i) \<in> null_sets M"
   690       proof (rule null_sets_UN)
   691         fix i ::nat have "?A i \<in> sets M"
   692           using borel by auto
   693         have "?N (?A i) \<le> (\<integral>\<^sup>+x. (i::ennreal) * indicator (?A i) x \<partial>M)"
   694           unfolding eq[OF \<open>?A i \<in> sets M\<close>]
   695           by (auto intro!: nn_integral_mono simp: indicator_def)
   696         also have "\<dots> = i * emeasure M (?A i)"
   697           using \<open>?A i \<in> sets M\<close> by (auto intro!: nn_integral_cmult_indicator)
   698         also have "\<dots> < \<infinity>" using emeasure_real[of "?A i"] by (auto simp: ennreal_mult_less_top of_nat_less_top)
   699         finally have "?N (?A i) \<noteq> \<infinity>" by simp
   700         then show "?A i \<in> null_sets M" using in_Q0[OF \<open>?A i \<in> sets M\<close>] \<open>?A i \<in> sets M\<close> by auto
   701       qed
   702       also have "(\<Union>i. ?A i) = (space M - (\<Union>i. Q i)) \<inter> {x\<in>space M. f x \<noteq> \<infinity>}"
   703         by (auto simp: ennreal_Ex_less_of_nat less_top[symmetric])
   704       finally have "(space M - (\<Union>i. Q i)) \<inter> {x\<in>space M. f x \<noteq> \<infinity>} \<in> null_sets M" by simp }
   705     from this[OF borel(1) refl] this[OF borel(2) f]
   706     have "(space M - (\<Union>i. Q i)) \<inter> {x\<in>space M. f x \<noteq> \<infinity>} \<in> null_sets M" "(space M - (\<Union>i. Q i)) \<inter> {x\<in>space M. f' x \<noteq> \<infinity>} \<in> null_sets M" by simp_all
   707     then show "((space M - (\<Union>i. Q i)) \<inter> {x\<in>space M. f x \<noteq> \<infinity>}) \<union> ((space M - (\<Union>i. Q i)) \<inter> {x\<in>space M. f' x \<noteq> \<infinity>}) \<in> null_sets M" by (rule null_sets.Un)
   708     show "{x \<in> space M. ?f (space M - (\<Union>i. Q i)) x \<noteq> ?f' (space M - (\<Union>i. Q i)) x} \<subseteq>
   709       ((space M - (\<Union>i. Q i)) \<inter> {x\<in>space M. f x \<noteq> \<infinity>}) \<union> ((space M - (\<Union>i. Q i)) \<inter> {x\<in>space M. f' x \<noteq> \<infinity>})" by (auto simp: indicator_def)
   710   qed
   711   moreover have "AE x in M. (?f (space M - (\<Union>i. Q i)) x = ?f' (space M - (\<Union>i. Q i)) x) \<longrightarrow> (\<forall>i. ?f (Q i) x = ?f' (Q i) x) \<longrightarrow>
   712     ?f (space M) x = ?f' (space M) x"
   713     by (auto simp: indicator_def)
   714   ultimately have "AE x in M. ?f (space M) x = ?f' (space M) x"
   715     unfolding AE_all_countable[symmetric]
   716     by eventually_elim (auto split: if_split_asm simp: indicator_def)
   717   then show "AE x in M. f x = f' x" by auto
   718 qed
   719 
   720 lemma (in sigma_finite_measure) density_unique:
   721   assumes f: "f \<in> borel_measurable M"
   722   assumes f': "f' \<in> borel_measurable M"
   723   assumes density_eq: "density M f = density M f'"
   724   shows "AE x in M. f x = f' x"
   725 proof -
   726   obtain h where h_borel: "h \<in> borel_measurable M"
   727     and fin: "integral\<^sup>N M h \<noteq> \<infinity>" and pos: "\<And>x. x \<in> space M \<Longrightarrow> 0 < h x \<and> h x < \<infinity>" "\<And>x. 0 \<le> h x"
   728     using Ex_finite_integrable_function by auto
   729   then have h_nn: "AE x in M. 0 \<le> h x" by auto
   730   let ?H = "density M h"
   731   interpret h: finite_measure ?H
   732     using fin h_borel pos
   733     by (intro finite_measureI) (simp cong: nn_integral_cong emeasure_density add: fin)
   734   let ?fM = "density M f"
   735   let ?f'M = "density M f'"
   736   { fix A assume "A \<in> sets M"
   737     then have "{x \<in> space M. h x * indicator A x \<noteq> 0} = A"
   738       using pos(1) sets.sets_into_space by (force simp: indicator_def)
   739     then have "(\<integral>\<^sup>+x. h x * indicator A x \<partial>M) = 0 \<longleftrightarrow> A \<in> null_sets M"
   740       using h_borel \<open>A \<in> sets M\<close> h_nn by (subst nn_integral_0_iff) auto }
   741   note h_null_sets = this
   742   { fix A assume "A \<in> sets M"
   743     have "(\<integral>\<^sup>+x. f x * (h x * indicator A x) \<partial>M) = (\<integral>\<^sup>+x. h x * indicator A x \<partial>?fM)"
   744       using \<open>A \<in> sets M\<close> h_borel h_nn f f'
   745       by (intro nn_integral_density[symmetric]) auto
   746     also have "\<dots> = (\<integral>\<^sup>+x. h x * indicator A x \<partial>?f'M)"
   747       by (simp_all add: density_eq)
   748     also have "\<dots> = (\<integral>\<^sup>+x. f' x * (h x * indicator A x) \<partial>M)"
   749       using \<open>A \<in> sets M\<close> h_borel h_nn f f'
   750       by (intro nn_integral_density) auto
   751     finally have "(\<integral>\<^sup>+x. h x * (f x * indicator A x) \<partial>M) = (\<integral>\<^sup>+x. h x * (f' x * indicator A x) \<partial>M)"
   752       by (simp add: ac_simps)
   753     then have "(\<integral>\<^sup>+x. (f x * indicator A x) \<partial>?H) = (\<integral>\<^sup>+x. (f' x * indicator A x) \<partial>?H)"
   754       using \<open>A \<in> sets M\<close> h_borel h_nn f f'
   755       by (subst (asm) (1 2) nn_integral_density[symmetric]) auto }
   756   then have "AE x in ?H. f x = f' x" using h_borel h_nn f f'
   757     by (intro h.density_unique_finite_measure absolutely_continuous_AE[of M]) auto
   758   with AE_space[of M] pos show "AE x in M. f x = f' x"
   759     unfolding AE_density[OF h_borel] by auto
   760 qed
   761 
   762 lemma (in sigma_finite_measure) density_unique_iff:
   763   assumes f: "f \<in> borel_measurable M" and f': "f' \<in> borel_measurable M"
   764   shows "density M f = density M f' \<longleftrightarrow> (AE x in M. f x = f' x)"
   765   using density_unique[OF assms] density_cong[OF f f'] by auto
   766 
   767 lemma%important sigma_finite_density_unique:
   768   assumes borel: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   769   and fin: "sigma_finite_measure (density M f)"
   770   shows "density M f = density M g \<longleftrightarrow> (AE x in M. f x = g x)"
   771 proof%unimportant
   772   assume "AE x in M. f x = g x" with borel show "density M f = density M g"
   773     by (auto intro: density_cong)
   774 next
   775   assume eq: "density M f = density M g"
   776   interpret f: sigma_finite_measure "density M f" by fact
   777   from f.sigma_finite_incseq guess A . note cover = this
   778 
   779   have "AE x in M. \<forall>i. x \<in> A i \<longrightarrow> f x = g x"
   780     unfolding AE_all_countable
   781   proof
   782     fix i
   783     have "density (density M f) (indicator (A i)) = density (density M g) (indicator (A i))"
   784       unfolding eq ..
   785     moreover have "(\<integral>\<^sup>+x. f x * indicator (A i) x \<partial>M) \<noteq> \<infinity>"
   786       using cover(1) cover(3)[of i] borel by (auto simp: emeasure_density subset_eq)
   787     ultimately have "AE x in M. f x * indicator (A i) x = g x * indicator (A i) x"
   788       using borel cover(1)
   789       by (intro finite_density_unique[THEN iffD1]) (auto simp: density_density_eq subset_eq)
   790     then show "AE x in M. x \<in> A i \<longrightarrow> f x = g x"
   791       by auto
   792   qed
   793   with AE_space show "AE x in M. f x = g x"
   794     apply eventually_elim
   795     using cover(2)[symmetric]
   796     apply auto
   797     done
   798 qed
   799 
   800 lemma%important (in sigma_finite_measure) sigma_finite_iff_density_finite':
   801   assumes f: "f \<in> borel_measurable M"
   802   shows "sigma_finite_measure (density M f) \<longleftrightarrow> (AE x in M. f x \<noteq> \<infinity>)"
   803     (is "sigma_finite_measure ?N \<longleftrightarrow> _")
   804 proof%unimportant
   805   assume "sigma_finite_measure ?N"
   806   then interpret N: sigma_finite_measure ?N .
   807   from N.Ex_finite_integrable_function obtain h where
   808     h: "h \<in> borel_measurable M" "integral\<^sup>N ?N h \<noteq> \<infinity>" and
   809     fin: "\<forall>x\<in>space M. 0 < h x \<and> h x < \<infinity>"
   810     by auto
   811   have "AE x in M. f x * h x \<noteq> \<infinity>"
   812   proof (rule AE_I')
   813     have "integral\<^sup>N ?N h = (\<integral>\<^sup>+x. f x * h x \<partial>M)"
   814       using f h by (auto intro!: nn_integral_density)
   815     then have "(\<integral>\<^sup>+x. f x * h x \<partial>M) \<noteq> \<infinity>"
   816       using h(2) by simp
   817     then show "(\<lambda>x. f x * h x) -` {\<infinity>} \<inter> space M \<in> null_sets M"
   818       using f h(1) by (auto intro!: nn_integral_PInf[unfolded infinity_ennreal_def] borel_measurable_vimage)
   819   qed auto
   820   then show "AE x in M. f x \<noteq> \<infinity>"
   821     using fin by (auto elim!: AE_Ball_mp simp: less_top ennreal_mult_less_top)
   822 next
   823   assume AE: "AE x in M. f x \<noteq> \<infinity>"
   824   from sigma_finite guess Q . note Q = this
   825   define A where "A i =
   826     f -` (case i of 0 \<Rightarrow> {\<infinity>} | Suc n \<Rightarrow> {.. ennreal(of_nat (Suc n))}) \<inter> space M" for i
   827   { fix i j have "A i \<inter> Q j \<in> sets M"
   828     unfolding A_def using f Q
   829     apply (rule_tac sets.Int)
   830     by (cases i) (auto intro: measurable_sets[OF f(1)]) }
   831   note A_in_sets = this
   832 
   833   show "sigma_finite_measure ?N"
   834   proof (standard, intro exI conjI ballI)
   835     show "countable (range (\<lambda>(i, j). A i \<inter> Q j))"
   836       by auto
   837     show "range (\<lambda>(i, j). A i \<inter> Q j) \<subseteq> sets (density M f)"
   838       using A_in_sets by auto
   839   next
   840     have "\<Union>range (\<lambda>(i, j). A i \<inter> Q j) = (\<Union>i j. A i \<inter> Q j)"
   841       by auto
   842     also have "\<dots> = (\<Union>i. A i) \<inter> space M" using Q by auto
   843     also have "(\<Union>i. A i) = space M"
   844     proof safe
   845       fix x assume x: "x \<in> space M"
   846       show "x \<in> (\<Union>i. A i)"
   847       proof (cases "f x" rule: ennreal_cases)
   848         case top with x show ?thesis unfolding A_def by (auto intro: exI[of _ 0])
   849       next
   850         case (real r)
   851         with ennreal_Ex_less_of_nat[of "f x"] obtain n :: nat where "f x < n"
   852           by auto
   853         also have "n < (Suc n :: ennreal)"
   854           by simp
   855         finally show ?thesis
   856           using x real by (auto simp: A_def ennreal_of_nat_eq_real_of_nat intro!: exI[of _ "Suc n"])
   857       qed
   858     qed (auto simp: A_def)
   859     finally show "\<Union>range (\<lambda>(i, j). A i \<inter> Q j) = space ?N" by simp
   860   next
   861     fix X assume "X \<in> range (\<lambda>(i, j). A i \<inter> Q j)"
   862     then obtain i j where [simp]:"X = A i \<inter> Q j" by auto
   863     have "(\<integral>\<^sup>+x. f x * indicator (A i \<inter> Q j) x \<partial>M) \<noteq> \<infinity>"
   864     proof (cases i)
   865       case 0
   866       have "AE x in M. f x * indicator (A i \<inter> Q j) x = 0"
   867         using AE by (auto simp: A_def \<open>i = 0\<close>)
   868       from nn_integral_cong_AE[OF this] show ?thesis by simp
   869     next
   870       case (Suc n)
   871       then have "(\<integral>\<^sup>+x. f x * indicator (A i \<inter> Q j) x \<partial>M) \<le>
   872         (\<integral>\<^sup>+x. (Suc n :: ennreal) * indicator (Q j) x \<partial>M)"
   873         by (auto intro!: nn_integral_mono simp: indicator_def A_def ennreal_of_nat_eq_real_of_nat)
   874       also have "\<dots> = Suc n * emeasure M (Q j)"
   875         using Q by (auto intro!: nn_integral_cmult_indicator)
   876       also have "\<dots> < \<infinity>"
   877         using Q by (auto simp: ennreal_mult_less_top less_top of_nat_less_top)
   878       finally show ?thesis by simp
   879     qed
   880     then show "emeasure ?N X \<noteq> \<infinity>"
   881       using A_in_sets Q f by (auto simp: emeasure_density)
   882   qed
   883 qed
   884 
   885 lemma (in sigma_finite_measure) sigma_finite_iff_density_finite:
   886   "f \<in> borel_measurable M \<Longrightarrow> sigma_finite_measure (density M f) \<longleftrightarrow> (AE x in M. f x \<noteq> \<infinity>)"
   887   by (subst sigma_finite_iff_density_finite')
   888      (auto simp: max_def intro!: measurable_If)
   889 
   890 subsection%important \<open>Radon-Nikodym derivative\<close>
   891 
   892 definition%important RN_deriv :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> 'a \<Rightarrow> ennreal" where
   893   "RN_deriv M N =
   894     (if \<exists>f. f \<in> borel_measurable M \<and> density M f = N
   895        then SOME f. f \<in> borel_measurable M \<and> density M f = N
   896        else (\<lambda>_. 0))"
   897 
   898 lemma%important RN_derivI:
   899   assumes "f \<in> borel_measurable M" "density M f = N"
   900   shows "density M (RN_deriv M N) = N"
   901 proof%unimportant -
   902   have *: "\<exists>f. f \<in> borel_measurable M \<and> density M f = N"
   903     using assms by auto
   904   then have "density M (SOME f. f \<in> borel_measurable M \<and> density M f = N) = N"
   905     by (rule someI2_ex) auto
   906   with * show ?thesis
   907     by (auto simp: RN_deriv_def)
   908 qed
   909 
   910 lemma borel_measurable_RN_deriv[measurable]: "RN_deriv M N \<in> borel_measurable M"
   911 proof -
   912   { assume ex: "\<exists>f. f \<in> borel_measurable M \<and> density M f = N"
   913     have 1: "(SOME f. f \<in> borel_measurable M \<and> density M f = N) \<in> borel_measurable M"
   914       using ex by (rule someI2_ex) auto }
   915   from this show ?thesis
   916     by (auto simp: RN_deriv_def)
   917 qed
   918 
   919 lemma density_RN_deriv_density:
   920   assumes f: "f \<in> borel_measurable M"
   921   shows "density M (RN_deriv M (density M f)) = density M f"
   922   by (rule RN_derivI[OF f]) simp
   923 
   924 lemma (in sigma_finite_measure) density_RN_deriv:
   925   "absolutely_continuous M N \<Longrightarrow> sets N = sets M \<Longrightarrow> density M (RN_deriv M N) = N"
   926   by (metis RN_derivI Radon_Nikodym)
   927 
   928 lemma%important (in sigma_finite_measure) RN_deriv_nn_integral:
   929   assumes N: "absolutely_continuous M N" "sets N = sets M"
   930     and f: "f \<in> borel_measurable M"
   931   shows "integral\<^sup>N N f = (\<integral>\<^sup>+x. RN_deriv M N x * f x \<partial>M)"
   932 proof%unimportant -
   933   have "integral\<^sup>N N f = integral\<^sup>N (density M (RN_deriv M N)) f"
   934     using N by (simp add: density_RN_deriv)
   935   also have "\<dots> = (\<integral>\<^sup>+x. RN_deriv M N x * f x \<partial>M)"
   936     using f by (simp add: nn_integral_density)
   937   finally show ?thesis by simp
   938 qed
   939 
   940 lemma (in sigma_finite_measure) RN_deriv_unique:
   941   assumes f: "f \<in> borel_measurable M"
   942   and eq: "density M f = N"
   943   shows "AE x in M. f x = RN_deriv M N x"
   944   unfolding eq[symmetric]
   945   by (intro density_unique_iff[THEN iffD1] f borel_measurable_RN_deriv
   946             density_RN_deriv_density[symmetric])
   947 
   948 lemma RN_deriv_unique_sigma_finite:
   949   assumes f: "f \<in> borel_measurable M"
   950   and eq: "density M f = N" and fin: "sigma_finite_measure N"
   951   shows "AE x in M. f x = RN_deriv M N x"
   952   using fin unfolding eq[symmetric]
   953   by (intro sigma_finite_density_unique[THEN iffD1] f borel_measurable_RN_deriv
   954             density_RN_deriv_density[symmetric])
   955 
   956 lemma%important (in sigma_finite_measure) RN_deriv_distr:
   957   fixes T :: "'a \<Rightarrow> 'b"
   958   assumes T: "T \<in> measurable M M'" and T': "T' \<in> measurable M' M"
   959     and inv: "\<forall>x\<in>space M. T' (T x) = x"
   960   and ac[simp]: "absolutely_continuous (distr M M' T) (distr N M' T)"
   961   and N: "sets N = sets M"
   962   shows "AE x in M. RN_deriv (distr M M' T) (distr N M' T) (T x) = RN_deriv M N x"
   963 proof%unimportant (rule RN_deriv_unique)
   964   have [simp]: "sets N = sets M" by fact
   965   note sets_eq_imp_space_eq[OF N, simp]
   966   have measurable_N[simp]: "\<And>M'. measurable N M' = measurable M M'" by (auto simp: measurable_def)
   967   { fix A assume "A \<in> sets M"
   968     with inv T T' sets.sets_into_space[OF this]
   969     have "T -` T' -` A \<inter> T -` space M' \<inter> space M = A"
   970       by (auto simp: measurable_def) }
   971   note eq = this[simp]
   972   { fix A assume "A \<in> sets M"
   973     with inv T T' sets.sets_into_space[OF this]
   974     have "(T' \<circ> T) -` A \<inter> space M = A"
   975       by (auto simp: measurable_def) }
   976   note eq2 = this[simp]
   977   let ?M' = "distr M M' T" and ?N' = "distr N M' T"
   978   interpret M': sigma_finite_measure ?M'
   979   proof
   980     from sigma_finite_countable guess F .. note F = this
   981     show "\<exists>A. countable A \<and> A \<subseteq> sets (distr M M' T) \<and> \<Union>A = space (distr M M' T) \<and> (\<forall>a\<in>A. emeasure (distr M M' T) a \<noteq> \<infinity>)"
   982     proof (intro exI conjI ballI)
   983       show *: "(\<lambda>A. T' -` A \<inter> space ?M') ` F \<subseteq> sets ?M'"
   984         using F T' by (auto simp: measurable_def)
   985       show "\<Union>((\<lambda>A. T' -` A \<inter> space ?M')`F) = space ?M'"
   986         using F T'[THEN measurable_space] by (auto simp: set_eq_iff)
   987     next
   988       fix X assume "X \<in> (\<lambda>A. T' -` A \<inter> space ?M')`F"
   989       then obtain A where [simp]: "X = T' -` A \<inter> space ?M'" and "A \<in> F" by auto
   990       have "X \<in> sets M'" using F T' \<open>A\<in>F\<close> by auto
   991       moreover
   992       have Fi: "A \<in> sets M" using F \<open>A\<in>F\<close> by auto
   993       ultimately show "emeasure ?M' X \<noteq> \<infinity>"
   994         using F T T' \<open>A\<in>F\<close> by (simp add: emeasure_distr)
   995     qed (insert F, auto)
   996   qed
   997   have "(RN_deriv ?M' ?N') \<circ> T \<in> borel_measurable M"
   998     using T ac by measurable
   999   then show "(\<lambda>x. RN_deriv ?M' ?N' (T x)) \<in> borel_measurable M"
  1000     by (simp add: comp_def)
  1001 
  1002   have "N = distr N M (T' \<circ> T)"
  1003     by (subst measure_of_of_measure[of N, symmetric])
  1004        (auto simp add: distr_def sets.sigma_sets_eq intro!: measure_of_eq sets.space_closed)
  1005   also have "\<dots> = distr (distr N M' T) M T'"
  1006     using T T' by (simp add: distr_distr)
  1007   also have "\<dots> = distr (density (distr M M' T) (RN_deriv (distr M M' T) (distr N M' T))) M T'"
  1008     using ac by (simp add: M'.density_RN_deriv)
  1009   also have "\<dots> = density M (RN_deriv (distr M M' T) (distr N M' T) \<circ> T)"
  1010     by (simp add: distr_density_distr[OF T T', OF inv])
  1011   finally show "density M (\<lambda>x. RN_deriv (distr M M' T) (distr N M' T) (T x)) = N"
  1012     by (simp add: comp_def)
  1013 qed
  1014 
  1015 lemma%important (in sigma_finite_measure) RN_deriv_finite:
  1016   assumes N: "sigma_finite_measure N" and ac: "absolutely_continuous M N" "sets N = sets M"
  1017   shows "AE x in M. RN_deriv M N x \<noteq> \<infinity>"
  1018 proof%unimportant -
  1019   interpret N: sigma_finite_measure N by fact
  1020   from N show ?thesis
  1021     using sigma_finite_iff_density_finite[OF borel_measurable_RN_deriv, of N] density_RN_deriv[OF ac]
  1022     by simp
  1023 qed
  1024 
  1025 lemma%important (in sigma_finite_measure) (* *FIX ME needs name*)
  1026   assumes N: "sigma_finite_measure N" and ac: "absolutely_continuous M N" "sets N = sets M"
  1027     and f: "f \<in> borel_measurable M"
  1028   shows RN_deriv_integrable: "integrable N f \<longleftrightarrow>
  1029       integrable M (\<lambda>x. enn2real (RN_deriv M N x) * f x)" (is ?integrable)
  1030     and RN_deriv_integral: "integral\<^sup>L N f = (\<integral>x. enn2real (RN_deriv M N x) * f x \<partial>M)" (is ?integral)
  1031 proof%unimportant -
  1032   note ac(2)[simp] and sets_eq_imp_space_eq[OF ac(2), simp]
  1033   interpret N: sigma_finite_measure N by fact
  1034 
  1035   have eq: "density M (RN_deriv M N) = density M (\<lambda>x. enn2real (RN_deriv M N x))"
  1036   proof (rule density_cong)
  1037     from RN_deriv_finite[OF assms(1,2,3)]
  1038     show "AE x in M. RN_deriv M N x = ennreal (enn2real (RN_deriv M N x))"
  1039       by eventually_elim (auto simp: less_top)
  1040   qed (insert ac, auto)
  1041 
  1042   show ?integrable
  1043     apply (subst density_RN_deriv[OF ac, symmetric])
  1044     unfolding eq
  1045     apply (intro integrable_real_density f AE_I2 enn2real_nonneg)
  1046     apply (insert ac, auto)
  1047     done
  1048 
  1049   show ?integral
  1050     apply (subst density_RN_deriv[OF ac, symmetric])
  1051     unfolding eq
  1052     apply (intro integral_real_density f AE_I2 enn2real_nonneg)
  1053     apply (insert ac, auto)
  1054     done
  1055 qed
  1056 
  1057 lemma%important (in sigma_finite_measure) real_RN_deriv:
  1058   assumes "finite_measure N"
  1059   assumes ac: "absolutely_continuous M N" "sets N = sets M"
  1060   obtains D where "D \<in> borel_measurable M"
  1061     and "AE x in M. RN_deriv M N x = ennreal (D x)"
  1062     and "AE x in N. 0 < D x"
  1063     and "\<And>x. 0 \<le> D x"
  1064 proof%unimportant
  1065   interpret N: finite_measure N by fact
  1066 
  1067   note RN = borel_measurable_RN_deriv density_RN_deriv[OF ac]
  1068 
  1069   let ?RN = "\<lambda>t. {x \<in> space M. RN_deriv M N x = t}"
  1070 
  1071   show "(\<lambda>x. enn2real (RN_deriv M N x)) \<in> borel_measurable M"
  1072     using RN by auto
  1073 
  1074   have "N (?RN \<infinity>) = (\<integral>\<^sup>+ x. RN_deriv M N x * indicator (?RN \<infinity>) x \<partial>M)"
  1075     using RN(1) by (subst RN(2)[symmetric]) (auto simp: emeasure_density)
  1076   also have "\<dots> = (\<integral>\<^sup>+ x. \<infinity> * indicator (?RN \<infinity>) x \<partial>M)"
  1077     by (intro nn_integral_cong) (auto simp: indicator_def)
  1078   also have "\<dots> = \<infinity> * emeasure M (?RN \<infinity>)"
  1079     using RN by (intro nn_integral_cmult_indicator) auto
  1080   finally have eq: "N (?RN \<infinity>) = \<infinity> * emeasure M (?RN \<infinity>)" .
  1081   moreover
  1082   have "emeasure M (?RN \<infinity>) = 0"
  1083   proof (rule ccontr)
  1084     assume "emeasure M {x \<in> space M. RN_deriv M N x = \<infinity>} \<noteq> 0"
  1085     then have "0 < emeasure M {x \<in> space M. RN_deriv M N x = \<infinity>}"
  1086       by (auto simp: zero_less_iff_neq_zero)
  1087     with eq have "N (?RN \<infinity>) = \<infinity>" by (simp add: ennreal_mult_eq_top_iff)
  1088     with N.emeasure_finite[of "?RN \<infinity>"] RN show False by auto
  1089   qed
  1090   ultimately have "AE x in M. RN_deriv M N x < \<infinity>"
  1091     using RN by (intro AE_iff_measurable[THEN iffD2]) (auto simp: less_top[symmetric])
  1092   then show "AE x in M. RN_deriv M N x = ennreal (enn2real (RN_deriv M N x))"
  1093     by auto
  1094   then have eq: "AE x in N. RN_deriv M N x = ennreal (enn2real (RN_deriv M N x))"
  1095     using ac absolutely_continuous_AE by auto
  1096 
  1097 
  1098   have "N (?RN 0) = (\<integral>\<^sup>+ x. RN_deriv M N x * indicator (?RN 0) x \<partial>M)"
  1099     by (subst RN(2)[symmetric]) (auto simp: emeasure_density)
  1100   also have "\<dots> = (\<integral>\<^sup>+ x. 0 \<partial>M)"
  1101     by (intro nn_integral_cong) (auto simp: indicator_def)
  1102   finally have "AE x in N. RN_deriv M N x \<noteq> 0"
  1103     using RN by (subst AE_iff_measurable[OF _ refl]) (auto simp: ac cong: sets_eq_imp_space_eq)
  1104   with eq show "AE x in N. 0 < enn2real (RN_deriv M N x)"
  1105     by (auto simp: enn2real_positive_iff less_top[symmetric] zero_less_iff_neq_zero)
  1106 qed (rule enn2real_nonneg)
  1107 
  1108 lemma (in sigma_finite_measure) RN_deriv_singleton:
  1109   assumes ac: "absolutely_continuous M N" "sets N = sets M"
  1110   and x: "{x} \<in> sets M"
  1111   shows "N {x} = RN_deriv M N x * emeasure M {x}"
  1112 proof -
  1113   from \<open>{x} \<in> sets M\<close>
  1114   have "density M (RN_deriv M N) {x} = (\<integral>\<^sup>+w. RN_deriv M N x * indicator {x} w \<partial>M)"
  1115     by (auto simp: indicator_def emeasure_density intro!: nn_integral_cong)
  1116   with x density_RN_deriv[OF ac] show ?thesis
  1117     by (auto simp: max_def)
  1118 qed
  1119 
  1120 end