author noschinl
Tue Sep 13 16:21:48 2011 +0200 (2011-09-13)
changeset 44918 6a80fbc4e72c
parent 44890 22f665a2e91c
child 44928 7ef6505bde7f
permissions -rw-r--r--
tune simpset for Complete_Lattices
     1 (*  Title:      HOL/Probability/Radon_Nikodym.thy
     2     Author:     Johannes Hölzl, TU München
     3 *)
     5 header {*Radon-Nikod{\'y}m derivative*}
     7 theory Radon_Nikodym
     8 imports Lebesgue_Integration
     9 begin
    11 lemma (in sigma_finite_measure) Ex_finite_integrable_function:
    12   shows "\<exists>h\<in>borel_measurable M. integral\<^isup>P M h \<noteq> \<infinity> \<and> (\<forall>x\<in>space M. 0 < h x \<and> h x < \<infinity>) \<and> (\<forall>x. 0 \<le> h x)"
    13 proof -
    14   obtain A :: "nat \<Rightarrow> 'a set" where
    15     range: "range A \<subseteq> sets M" and
    16     space: "(\<Union>i. A i) = space M" and
    17     measure: "\<And>i. \<mu> (A i) \<noteq> \<infinity>" and
    18     disjoint: "disjoint_family A"
    19     using disjoint_sigma_finite by auto
    20   let "?B i" = "2^Suc i * \<mu> (A i)"
    21   have "\<forall>i. \<exists>x. 0 < x \<and> x < inverse (?B i)"
    22   proof
    23     fix i have Ai: "A i \<in> sets M" using range by auto
    24     from measure positive_measure[OF this]
    25     show "\<exists>x. 0 < x \<and> x < inverse (?B i)"
    26       by (auto intro!: ereal_dense simp: ereal_0_gt_inverse)
    27   qed
    28   from choice[OF this] obtain n where n: "\<And>i. 0 < n i"
    29     "\<And>i. n i < inverse (2^Suc i * \<mu> (A i))" by auto
    30   { fix i have "0 \<le> n i" using n(1)[of i] by auto } note pos = this
    31   let "?h x" = "\<Sum>i. n i * indicator (A i) x"
    32   show ?thesis
    33   proof (safe intro!: bexI[of _ ?h] del: notI)
    34     have "\<And>i. A i \<in> sets M"
    35       using range by fastforce+
    36     then have "integral\<^isup>P M ?h = (\<Sum>i. n i * \<mu> (A i))" using pos
    37       by (simp add: positive_integral_suminf positive_integral_cmult_indicator)
    38     also have "\<dots> \<le> (\<Sum>i. (1 / 2)^Suc i)"
    39     proof (rule suminf_le_pos)
    40       fix N
    41       have "n N * \<mu> (A N) \<le> inverse (2^Suc N * \<mu> (A N)) * \<mu> (A N)"
    42         using positive_measure[OF `A N \<in> sets M`] n[of N]
    43         by (intro ereal_mult_right_mono) auto
    44       also have "\<dots> \<le> (1 / 2) ^ Suc N"
    45         using measure[of N] n[of N]
    46         by (cases rule: ereal2_cases[of "n N" "\<mu> (A N)"])
    47            (simp_all add: inverse_eq_divide power_divide one_ereal_def ereal_power_divide)
    48       finally show "n N * \<mu> (A N) \<le> (1 / 2) ^ Suc N" .
    49       show "0 \<le> n N * \<mu> (A N)" using n[of N] `A N \<in> sets M` by simp
    50     qed
    51     finally show "integral\<^isup>P M ?h \<noteq> \<infinity>" unfolding suminf_half_series_ereal by auto
    52   next
    53     { fix x assume "x \<in> space M"
    54       then obtain i where "x \<in> A i" using space[symmetric] by auto
    55       with disjoint n have "?h x = n i"
    56         by (auto intro!: suminf_cmult_indicator intro: less_imp_le)
    57       then show "0 < ?h x" and "?h x < \<infinity>" using n[of i] by auto }
    58     note pos = this
    59     fix x show "0 \<le> ?h x"
    60     proof cases
    61       assume "x \<in> space M" then show "0 \<le> ?h x" using pos by (auto intro: less_imp_le)
    62     next
    63       assume "x \<notin> space M" then have "\<And>i. x \<notin> A i" using space by auto
    64       then show "0 \<le> ?h x" by auto
    65     qed
    66   next
    67     show "?h \<in> borel_measurable M" using range n
    68       by (auto intro!: borel_measurable_psuminf borel_measurable_ereal_times ereal_0_le_mult intro: less_imp_le)
    69   qed
    70 qed
    72 subsection "Absolutely continuous"
    74 definition (in measure_space)
    75   "absolutely_continuous \<nu> = (\<forall>N\<in>null_sets. \<nu> N = (0 :: ereal))"
    77 lemma (in measure_space) absolutely_continuous_AE:
    78   assumes "measure_space M'" and [simp]: "sets M' = sets M" "space M' = space M"
    79     and "absolutely_continuous (measure M')" "AE x. P x"
    80    shows "AE x in M'. P x"
    81 proof -
    82   interpret \<nu>: measure_space M' by fact
    83   from `AE x. P x` obtain N where N: "N \<in> null_sets" and "{x\<in>space M. \<not> P x} \<subseteq> N"
    84     unfolding almost_everywhere_def by auto
    85   show "AE x in M'. P x"
    86   proof (rule \<nu>.AE_I')
    87     show "{x\<in>space M'. \<not> P x} \<subseteq> N" by simp fact
    88     from `absolutely_continuous (measure M')` show "N \<in> \<nu>.null_sets"
    89       using N unfolding absolutely_continuous_def by auto
    90   qed
    91 qed
    93 lemma (in finite_measure_space) absolutely_continuousI:
    94   assumes "finite_measure_space (M\<lparr> measure := \<nu>\<rparr>)" (is "finite_measure_space ?\<nu>")
    95   assumes v: "\<And>x. \<lbrakk> x \<in> space M ; \<mu> {x} = 0 \<rbrakk> \<Longrightarrow> \<nu> {x} = 0"
    96   shows "absolutely_continuous \<nu>"
    97 proof (unfold absolutely_continuous_def sets_eq_Pow, safe)
    98   fix N assume "\<mu> N = 0" "N \<subseteq> space M"
    99   interpret v: finite_measure_space ?\<nu> by fact
   100   have "\<nu> N = measure ?\<nu> (\<Union>x\<in>N. {x})" by simp
   101   also have "\<dots> = (\<Sum>x\<in>N. measure ?\<nu> {x})"
   102   proof (rule v.measure_setsum[symmetric])
   103     show "finite N" using `N \<subseteq> space M` finite_space by (auto intro: finite_subset)
   104     show "disjoint_family_on (\<lambda>i. {i}) N" unfolding disjoint_family_on_def by auto
   105     fix x assume "x \<in> N" thus "{x} \<in> sets ?\<nu>" using `N \<subseteq> space M` sets_eq_Pow by auto
   106   qed
   107   also have "\<dots> = 0"
   108   proof (safe intro!: setsum_0')
   109     fix x assume "x \<in> N"
   110     hence "\<mu> {x} \<le> \<mu> N" "0 \<le> \<mu> {x}"
   111       using sets_eq_Pow `N \<subseteq> space M` positive_measure[of "{x}"]
   112       by (auto intro!: measure_mono)
   113     then have "\<mu> {x} = 0" using `\<mu> N = 0` by simp
   114     thus "measure ?\<nu> {x} = 0" using v[of x] `x \<in> N` `N \<subseteq> space M` by auto
   115   qed
   116   finally show "\<nu> N = 0" by simp
   117 qed
   119 lemma (in measure_space) density_is_absolutely_continuous:
   120   assumes "\<And>A. A \<in> sets M \<Longrightarrow> \<nu> A = (\<integral>\<^isup>+x. f x * indicator A x \<partial>M)"
   121   shows "absolutely_continuous \<nu>"
   122   using assms unfolding absolutely_continuous_def
   123   by (simp add: positive_integral_null_set)
   125 subsection "Existence of the Radon-Nikodym derivative"
   127 lemma (in finite_measure) Radon_Nikodym_aux_epsilon:
   128   fixes e :: real assumes "0 < e"
   129   assumes "finite_measure (M\<lparr>measure := \<nu>\<rparr>)"
   130   shows "\<exists>A\<in>sets M. \<mu>' (space M) - finite_measure.\<mu>' (M\<lparr>measure := \<nu>\<rparr>) (space M) \<le>
   131                     \<mu>' A - finite_measure.\<mu>' (M\<lparr>measure := \<nu>\<rparr>) A \<and>
   132                     (\<forall>B\<in>sets M. B \<subseteq> A \<longrightarrow> - e < \<mu>' B - finite_measure.\<mu>' (M\<lparr>measure := \<nu>\<rparr>) B)"
   133 proof -
   134   interpret M': finite_measure "M\<lparr>measure := \<nu>\<rparr>" by fact
   135   let "?d A" = "\<mu>' A - M'.\<mu>' A"
   136   let "?A A" = "if (\<forall>B\<in>sets M. B \<subseteq> space M - A \<longrightarrow> -e < ?d B)
   137     then {}
   138     else (SOME B. B \<in> sets M \<and> B \<subseteq> space M - A \<and> ?d B \<le> -e)"
   139   def A \<equiv> "\<lambda>n. ((\<lambda>B. B \<union> ?A B) ^^ n) {}"
   140   have A_simps[simp]:
   141     "A 0 = {}"
   142     "\<And>n. A (Suc n) = (A n \<union> ?A (A n))" unfolding A_def by simp_all
   143   { fix A assume "A \<in> sets M"
   144     have "?A A \<in> sets M"
   145       by (auto intro!: someI2[of _ _ "\<lambda>A. A \<in> sets M"] simp: not_less) }
   146   note A'_in_sets = this
   147   { fix n have "A n \<in> sets M"
   148     proof (induct n)
   149       case (Suc n) thus "A (Suc n) \<in> sets M"
   150         using A'_in_sets[of "A n"] by (auto split: split_if_asm)
   151     qed (simp add: A_def) }
   152   note A_in_sets = this
   153   hence "range A \<subseteq> sets M" by auto
   154   { fix n B
   155     assume Ex: "\<exists>B. B \<in> sets M \<and> B \<subseteq> space M - A n \<and> ?d B \<le> -e"
   156     hence False: "\<not> (\<forall>B\<in>sets M. B \<subseteq> space M - A n \<longrightarrow> -e < ?d B)" by (auto simp: not_less)
   157     have "?d (A (Suc n)) \<le> ?d (A n) - e" unfolding A_simps if_not_P[OF False]
   158     proof (rule someI2_ex[OF Ex])
   159       fix B assume "B \<in> sets M \<and> B \<subseteq> space M - A n \<and> ?d B \<le> - e"
   160       hence "A n \<inter> B = {}" "B \<in> sets M" and dB: "?d B \<le> -e" by auto
   161       hence "?d (A n \<union> B) = ?d (A n) + ?d B"
   162         using `A n \<in> sets M` finite_measure_Union M'.finite_measure_Union by simp
   163       also have "\<dots> \<le> ?d (A n) - e" using dB by simp
   164       finally show "?d (A n \<union> B) \<le> ?d (A n) - e" .
   165     qed }
   166   note dA_epsilon = this
   167   { fix n have "?d (A (Suc n)) \<le> ?d (A n)"
   168     proof (cases "\<exists>B. B\<in>sets M \<and> B \<subseteq> space M - A n \<and> ?d B \<le> - e")
   169       case True from dA_epsilon[OF this] show ?thesis using `0 < e` by simp
   170     next
   171       case False
   172       hence "\<forall>B\<in>sets M. B \<subseteq> space M - A n \<longrightarrow> -e < ?d B" by (auto simp: not_le)
   173       thus ?thesis by simp
   174     qed }
   175   note dA_mono = this
   176   show ?thesis
   177   proof (cases "\<exists>n. \<forall>B\<in>sets M. B \<subseteq> space M - A n \<longrightarrow> -e < ?d B")
   178     case True then obtain n where B: "\<And>B. \<lbrakk> B \<in> sets M; B \<subseteq> space M - A n\<rbrakk> \<Longrightarrow> -e < ?d B" by blast
   179     show ?thesis
   180     proof (safe intro!: bexI[of _ "space M - A n"])
   181       fix B assume "B \<in> sets M" "B \<subseteq> space M - A n"
   182       from B[OF this] show "-e < ?d B" .
   183     next
   184       show "space M - A n \<in> sets M" by (rule compl_sets) fact
   185     next
   186       show "?d (space M) \<le> ?d (space M - A n)"
   187       proof (induct n)
   188         fix n assume "?d (space M) \<le> ?d (space M - A n)"
   189         also have "\<dots> \<le> ?d (space M - A (Suc n))"
   190           using A_in_sets sets_into_space dA_mono[of n]
   191           by (simp del: A_simps add: finite_measure_Diff M'.finite_measure_Diff)
   192         finally show "?d (space M) \<le> ?d (space M - A (Suc n))" .
   193       qed simp
   194     qed
   195   next
   196     case False hence B: "\<And>n. \<exists>B. B\<in>sets M \<and> B \<subseteq> space M - A n \<and> ?d B \<le> - e"
   197       by (auto simp add: not_less)
   198     { fix n have "?d (A n) \<le> - real n * e"
   199       proof (induct n)
   200         case (Suc n) with dA_epsilon[of n, OF B] show ?case by (simp del: A_simps add: real_of_nat_Suc field_simps)
   201       next
   202         case 0 with M'.empty_measure show ?case by (simp add: zero_ereal_def)
   203       qed } note dA_less = this
   204     have decseq: "decseq (\<lambda>n. ?d (A n))" unfolding decseq_eq_incseq
   205     proof (rule incseq_SucI)
   206       fix n show "- ?d (A n) \<le> - ?d (A (Suc n))" using dA_mono[of n] by auto
   207     qed
   208     have A: "incseq A" by (auto intro!: incseq_SucI)
   209     from finite_continuity_from_below[OF _ A] `range A \<subseteq> sets M`
   210       M'.finite_continuity_from_below[OF _ A]
   211     have convergent: "(\<lambda>i. ?d (A i)) ----> ?d (\<Union>i. A i)"
   212       by (auto intro!: tendsto_diff)
   213     obtain n :: nat where "- ?d (\<Union>i. A i) / e < real n" using reals_Archimedean2 by auto
   214     moreover from order_trans[OF decseq_le[OF decseq convergent] dA_less]
   215     have "real n \<le> - ?d (\<Union>i. A i) / e" using `0<e` by (simp add: field_simps)
   216     ultimately show ?thesis by auto
   217   qed
   218 qed
   220 lemma (in finite_measure) restricted_measure_subset:
   221   assumes S: "S \<in> sets M" and X: "X \<subseteq> S"
   222   shows "finite_measure.\<mu>' (restricted_space S) X = \<mu>' X"
   223 proof cases
   224   note r = restricted_finite_measure[OF S]
   225   { assume "X \<in> sets M" with S X show ?thesis
   226       unfolding finite_measure.\<mu>'_def[OF r] \<mu>'_def by auto }
   227   { assume "X \<notin> sets M"
   228     moreover then have "S \<inter> X \<notin> sets M"
   229       using X by (simp add: Int_absorb1)
   230     ultimately show ?thesis
   231       unfolding finite_measure.\<mu>'_def[OF r] \<mu>'_def using S by auto }
   232 qed
   234 lemma (in finite_measure) restricted_measure:
   235   assumes X: "S \<in> sets M" "X \<in> sets (restricted_space S)"
   236   shows "finite_measure.\<mu>' (restricted_space S) X = \<mu>' X"
   237 proof -
   238   from X have "S \<in> sets M" "X \<subseteq> S" by auto
   239   from restricted_measure_subset[OF this] show ?thesis .
   240 qed
   242 lemma (in finite_measure) Radon_Nikodym_aux:
   243   assumes "finite_measure (M\<lparr>measure := \<nu>\<rparr>)" (is "finite_measure ?M'")
   244   shows "\<exists>A\<in>sets M. \<mu>' (space M) - finite_measure.\<mu>' (M\<lparr>measure := \<nu>\<rparr>) (space M) \<le>
   245                     \<mu>' A - finite_measure.\<mu>' (M\<lparr>measure := \<nu>\<rparr>) A \<and>
   246                     (\<forall>B\<in>sets M. B \<subseteq> A \<longrightarrow> 0 \<le> \<mu>' B - finite_measure.\<mu>' (M\<lparr>measure := \<nu>\<rparr>) B)"
   247 proof -
   248   interpret M': finite_measure ?M' where
   249     "space ?M' = space M" and "sets ?M' = sets M" and "measure ?M' = \<nu>" by fact auto
   250   let "?d A" = "\<mu>' A - M'.\<mu>' A"
   251   let "?P A B n" = "A \<in> sets M \<and> A \<subseteq> B \<and> ?d B \<le> ?d A \<and> (\<forall>C\<in>sets M. C \<subseteq> A \<longrightarrow> - 1 / real (Suc n) < ?d C)"
   252   let "?r S" = "restricted_space S"
   253   { fix S n assume S: "S \<in> sets M"
   254     note r = M'.restricted_finite_measure[of S] restricted_finite_measure[OF S] S
   255     then have "finite_measure (?r S)" "0 < 1 / real (Suc n)"
   256       "finite_measure (?r S\<lparr>measure := \<nu>\<rparr>)" by auto
   257     from finite_measure.Radon_Nikodym_aux_epsilon[OF this] guess X .. note X = this
   258     have "?P X S n"
   259     proof (intro conjI ballI impI)
   260       show "X \<in> sets M" "X \<subseteq> S" using X(1) `S \<in> sets M` by auto
   261       have "S \<in> op \<inter> S ` sets M" using `S \<in> sets M` by auto
   262       then show "?d S \<le> ?d X"
   263         using S X restricted_measure[OF S] M'.restricted_measure[OF S] by simp
   264       fix C assume "C \<in> sets M" "C \<subseteq> X"
   265       then have "C \<in> sets (restricted_space S)" "C \<subseteq> X"
   266         using `S \<in> sets M` `X \<subseteq> S` by auto
   267       with X(2) show "- 1 / real (Suc n) < ?d C"
   268         using S X restricted_measure[OF S] M'.restricted_measure[OF S] by auto
   269     qed
   270     hence "\<exists>A. ?P A S n" by auto }
   271   note Ex_P = this
   272   def A \<equiv> "nat_rec (space M) (\<lambda>n A. SOME B. ?P B A n)"
   273   have A_Suc: "\<And>n. A (Suc n) = (SOME B. ?P B (A n) n)" by (simp add: A_def)
   274   have A_0[simp]: "A 0 = space M" unfolding A_def by simp
   275   { fix i have "A i \<in> sets M" unfolding A_def
   276     proof (induct i)
   277       case (Suc i)
   278       from Ex_P[OF this, of i] show ?case unfolding nat_rec_Suc
   279         by (rule someI2_ex) simp
   280     qed simp }
   281   note A_in_sets = this
   282   { fix n have "?P (A (Suc n)) (A n) n"
   283       using Ex_P[OF A_in_sets] unfolding A_Suc
   284       by (rule someI2_ex) simp }
   285   note P_A = this
   286   have "range A \<subseteq> sets M" using A_in_sets by auto
   287   have A_mono: "\<And>i. A (Suc i) \<subseteq> A i" using P_A by simp
   288   have mono_dA: "mono (\<lambda>i. ?d (A i))" using P_A by (simp add: mono_iff_le_Suc)
   289   have epsilon: "\<And>C i. \<lbrakk> C \<in> sets M; C \<subseteq> A (Suc i) \<rbrakk> \<Longrightarrow> - 1 / real (Suc i) < ?d C"
   290       using P_A by auto
   291   show ?thesis
   292   proof (safe intro!: bexI[of _ "\<Inter>i. A i"])
   293     show "(\<Inter>i. A i) \<in> sets M" using A_in_sets by auto
   294     have A: "decseq A" using A_mono by (auto intro!: decseq_SucI)
   295     from
   296       finite_continuity_from_above[OF `range A \<subseteq> sets M` A]
   297       M'.finite_continuity_from_above[OF `range A \<subseteq> sets M` A]
   298     have "(\<lambda>i. ?d (A i)) ----> ?d (\<Inter>i. A i)" by (intro tendsto_diff)
   299     thus "?d (space M) \<le> ?d (\<Inter>i. A i)" using mono_dA[THEN monoD, of 0 _]
   300       by (rule_tac LIMSEQ_le_const) (auto intro!: exI)
   301   next
   302     fix B assume B: "B \<in> sets M" "B \<subseteq> (\<Inter>i. A i)"
   303     show "0 \<le> ?d B"
   304     proof (rule ccontr)
   305       assume "\<not> 0 \<le> ?d B"
   306       hence "0 < - ?d B" by auto
   307       from ex_inverse_of_nat_Suc_less[OF this]
   308       obtain n where *: "?d B < - 1 / real (Suc n)"
   309         by (auto simp: real_eq_of_nat inverse_eq_divide field_simps)
   310       have "B \<subseteq> A (Suc n)" using B by (auto simp del: nat_rec_Suc)
   311       from epsilon[OF B(1) this] *
   312       show False by auto
   313     qed
   314   qed
   315 qed
   317 lemma (in finite_measure) Radon_Nikodym_finite_measure:
   318   assumes "finite_measure (M\<lparr> measure := \<nu>\<rparr>)" (is "finite_measure ?M'")
   319   assumes "absolutely_continuous \<nu>"
   320   shows "\<exists>f \<in> borel_measurable M. \<forall>A\<in>sets M. \<nu> A = (\<integral>\<^isup>+x. f x * indicator A x \<partial>M)"
   321 proof -
   322   interpret M': finite_measure ?M'
   323     where "space ?M' = space M" and "sets ?M' = sets M" and "measure ?M' = \<nu>"
   324     using assms(1) by auto
   325   def G \<equiv> "{g \<in> borel_measurable M. (\<forall>x. 0 \<le> g x) \<and> (\<forall>A\<in>sets M. (\<integral>\<^isup>+x. g x * indicator A x \<partial>M) \<le> \<nu> A)}"
   326   have "(\<lambda>x. 0) \<in> G" unfolding G_def by auto
   327   hence "G \<noteq> {}" by auto
   328   { fix f g assume f: "f \<in> G" and g: "g \<in> G"
   329     have "(\<lambda>x. max (g x) (f x)) \<in> G" (is "?max \<in> G") unfolding G_def
   330     proof safe
   331       show "?max \<in> borel_measurable M" using f g unfolding G_def by auto
   332       let ?A = "{x \<in> space M. f x \<le> g x}"
   333       have "?A \<in> sets M" using f g unfolding G_def by auto
   334       fix A assume "A \<in> sets M"
   335       hence sets: "?A \<inter> A \<in> sets M" "(space M - ?A) \<inter> A \<in> sets M" using `?A \<in> sets M` by auto
   336       have union: "((?A \<inter> A) \<union> ((space M - ?A) \<inter> A)) = A"
   337         using sets_into_space[OF `A \<in> sets M`] by auto
   338       have "\<And>x. x \<in> space M \<Longrightarrow> max (g x) (f x) * indicator A x =
   339         g x * indicator (?A \<inter> A) x + f x * indicator ((space M - ?A) \<inter> A) x"
   340         by (auto simp: indicator_def max_def)
   341       hence "(\<integral>\<^isup>+x. max (g x) (f x) * indicator A x \<partial>M) =
   342         (\<integral>\<^isup>+x. g x * indicator (?A \<inter> A) x \<partial>M) +
   343         (\<integral>\<^isup>+x. f x * indicator ((space M - ?A) \<inter> A) x \<partial>M)"
   344         using f g sets unfolding G_def
   345         by (auto cong: positive_integral_cong intro!: positive_integral_add borel_measurable_indicator)
   346       also have "\<dots> \<le> \<nu> (?A \<inter> A) + \<nu> ((space M - ?A) \<inter> A)"
   347         using f g sets unfolding G_def by (auto intro!: add_mono)
   348       also have "\<dots> = \<nu> A"
   349         using M'.measure_additive[OF sets] union by auto
   350       finally show "(\<integral>\<^isup>+x. max (g x) (f x) * indicator A x \<partial>M) \<le> \<nu> A" .
   351     next
   352       fix x show "0 \<le> max (g x) (f x)" using f g by (auto simp: G_def split: split_max)
   353     qed }
   354   note max_in_G = this
   355   { fix f assume  "incseq f" and f: "\<And>i. f i \<in> G"
   356     have "(\<lambda>x. SUP i. f i x) \<in> G" unfolding G_def
   357     proof safe
   358       show "(\<lambda>x. SUP i. f i x) \<in> borel_measurable M"
   359         using f by (auto simp: G_def)
   360       { fix x show "0 \<le> (SUP i. f i x)"
   361           using f by (auto simp: G_def intro: le_SUPI2) }
   362     next
   363       fix A assume "A \<in> sets M"
   364       have "(\<integral>\<^isup>+x. (SUP i. f i x) * indicator A x \<partial>M) =
   365         (\<integral>\<^isup>+x. (SUP i. f i x * indicator A x) \<partial>M)"
   366         by (intro positive_integral_cong) (simp split: split_indicator)
   367       also have "\<dots> = (SUP i. (\<integral>\<^isup>+x. f i x * indicator A x \<partial>M))"
   368         using `incseq f` f `A \<in> sets M`
   369         by (intro positive_integral_monotone_convergence_SUP)
   370            (auto simp: G_def incseq_Suc_iff le_fun_def split: split_indicator)
   371       finally show "(\<integral>\<^isup>+x. (SUP i. f i x) * indicator A x \<partial>M) \<le> \<nu> A"
   372         using f `A \<in> sets M` by (auto intro!: SUP_leI simp: G_def)
   373     qed }
   374   note SUP_in_G = this
   375   let ?y = "SUP g : G. integral\<^isup>P M g"
   376   have "?y \<le> \<nu> (space M)" unfolding G_def
   377   proof (safe intro!: SUP_leI)
   378     fix g assume "\<forall>A\<in>sets M. (\<integral>\<^isup>+x. g x * indicator A x \<partial>M) \<le> \<nu> A"
   379     from this[THEN bspec, OF top] show "integral\<^isup>P M g \<le> \<nu> (space M)"
   380       by (simp cong: positive_integral_cong)
   381   qed
   382   from SUPR_countable_SUPR[OF `G \<noteq> {}`, of "integral\<^isup>P M"] guess ys .. note ys = this
   383   then have "\<forall>n. \<exists>g. g\<in>G \<and> integral\<^isup>P M g = ys n"
   384   proof safe
   385     fix n assume "range ys \<subseteq> integral\<^isup>P M ` G"
   386     hence "ys n \<in> integral\<^isup>P M ` G" by auto
   387     thus "\<exists>g. g\<in>G \<and> integral\<^isup>P M g = ys n" by auto
   388   qed
   389   from choice[OF this] obtain gs where "\<And>i. gs i \<in> G" "\<And>n. integral\<^isup>P M (gs n) = ys n" by auto
   390   hence y_eq: "?y = (SUP i. integral\<^isup>P M (gs i))" using ys by auto
   391   let "?g i x" = "Max ((\<lambda>n. gs n x) ` {..i})"
   392   def f \<equiv> "\<lambda>x. SUP i. ?g i x"
   393   let "?F A x" = "f x * indicator A x"
   394   have gs_not_empty: "\<And>i x. (\<lambda>n. gs n x) ` {..i} \<noteq> {}" by auto
   395   { fix i have "?g i \<in> G"
   396     proof (induct i)
   397       case 0 thus ?case by simp fact
   398     next
   399       case (Suc i)
   400       with Suc gs_not_empty `gs (Suc i) \<in> G` show ?case
   401         by (auto simp add: atMost_Suc intro!: max_in_G)
   402     qed }
   403   note g_in_G = this
   404   have "incseq ?g" using gs_not_empty
   405     by (auto intro!: incseq_SucI le_funI simp add: atMost_Suc)
   406   from SUP_in_G[OF this g_in_G] have "f \<in> G" unfolding f_def .
   407   then have [simp, intro]: "f \<in> borel_measurable M" unfolding G_def by auto
   408   have "integral\<^isup>P M f = (SUP i. integral\<^isup>P M (?g i))" unfolding f_def
   409     using g_in_G `incseq ?g`
   410     by (auto intro!: positive_integral_monotone_convergence_SUP simp: G_def)
   411   also have "\<dots> = ?y"
   412   proof (rule antisym)
   413     show "(SUP i. integral\<^isup>P M (?g i)) \<le> ?y"
   414       using g_in_G
   415       using [[simp_trace]]
   416       by (auto intro!: exI Sup_mono simp: SUPR_def)
   417     show "?y \<le> (SUP i. integral\<^isup>P M (?g i))" unfolding y_eq
   418       by (auto intro!: SUP_mono positive_integral_mono Max_ge)
   419   qed
   420   finally have int_f_eq_y: "integral\<^isup>P M f = ?y" .
   421   have "\<And>x. 0 \<le> f x"
   422     unfolding f_def using `\<And>i. gs i \<in> G`
   423     by (auto intro!: le_SUPI2 Max_ge_iff[THEN iffD2] simp: G_def)
   424   let "?t A" = "\<nu> A - (\<integral>\<^isup>+x. ?F A x \<partial>M)"
   425   let ?M = "M\<lparr> measure := ?t\<rparr>"
   426   interpret M: sigma_algebra ?M
   427     by (intro sigma_algebra_cong) auto
   428   have f_le_\<nu>: "\<And>A. A \<in> sets M \<Longrightarrow> (\<integral>\<^isup>+x. ?F A x \<partial>M) \<le> \<nu> A"
   429     using `f \<in> G` unfolding G_def by auto
   430   have fmM: "finite_measure ?M"
   431   proof (default, simp_all add: countably_additive_def positive_def, safe del: notI)
   432     fix A :: "nat \<Rightarrow> 'a set"  assume A: "range A \<subseteq> sets M" "disjoint_family A"
   433     have "(\<Sum>n. (\<integral>\<^isup>+x. ?F (A n) x \<partial>M)) = (\<integral>\<^isup>+x. (\<Sum>n. ?F (A n) x) \<partial>M)"
   434       using `range A \<subseteq> sets M` `\<And>x. 0 \<le> f x`
   435       by (intro positive_integral_suminf[symmetric]) auto
   436     also have "\<dots> = (\<integral>\<^isup>+x. ?F (\<Union>n. A n) x \<partial>M)"
   437       using `\<And>x. 0 \<le> f x`
   438       by (intro positive_integral_cong) (simp add: suminf_cmult_ereal suminf_indicator[OF `disjoint_family A`])
   439     finally have "(\<Sum>n. (\<integral>\<^isup>+x. ?F (A n) x \<partial>M)) = (\<integral>\<^isup>+x. ?F (\<Union>n. A n) x \<partial>M)" .
   440     moreover have "(\<Sum>n. \<nu> (A n)) = \<nu> (\<Union>n. A n)"
   441       using M'.measure_countably_additive A by (simp add: comp_def)
   442     moreover have v_fin: "\<nu> (\<Union>i. A i) \<noteq> \<infinity>" using M'.finite_measure A by (simp add: countable_UN)
   443     moreover {
   444       have "(\<integral>\<^isup>+x. ?F (\<Union>i. A i) x \<partial>M) \<le> \<nu> (\<Union>i. A i)"
   445         using A `f \<in> G` unfolding G_def by (auto simp: countable_UN)
   446       also have "\<nu> (\<Union>i. A i) < \<infinity>" using v_fin by simp
   447       finally have "(\<integral>\<^isup>+x. ?F (\<Union>i. A i) x \<partial>M) \<noteq> \<infinity>" by simp }
   448     moreover have "\<And>i. (\<integral>\<^isup>+x. ?F (A i) x \<partial>M) \<le> \<nu> (A i)"
   449       using A by (intro f_le_\<nu>) auto
   450     ultimately
   451     show "(\<Sum>n. ?t (A n)) = ?t (\<Union>i. A i)"
   452       by (subst suminf_ereal_minus) (simp_all add: positive_integral_positive)
   453   next
   454     fix A assume A: "A \<in> sets M" show "0 \<le> \<nu> A - \<integral>\<^isup>+ x. ?F A x \<partial>M"
   455       using f_le_\<nu>[OF A] `f \<in> G` M'.finite_measure[OF A] by (auto simp: G_def ereal_le_minus_iff)
   456   next
   457     show "\<nu> (space M) - (\<integral>\<^isup>+ x. ?F (space M) x \<partial>M) \<noteq> \<infinity>" (is "?a - ?b \<noteq> _")
   458       using positive_integral_positive[of "?F (space M)"]
   459       by (cases rule: ereal2_cases[of ?a ?b]) auto
   460   qed
   461   then interpret M: finite_measure ?M
   462     where "space ?M = space M" and "sets ?M = sets M" and "measure ?M = ?t"
   463     by (simp_all add: fmM)
   464   have ac: "absolutely_continuous ?t" unfolding absolutely_continuous_def
   465   proof
   466     fix N assume N: "N \<in> null_sets"
   467     with `absolutely_continuous \<nu>` have "\<nu> N = 0" unfolding absolutely_continuous_def by auto
   468     moreover with N have "(\<integral>\<^isup>+ x. ?F N x \<partial>M) \<le> \<nu> N" using `f \<in> G` by (auto simp: G_def)
   469     ultimately show "\<nu> N - (\<integral>\<^isup>+ x. ?F N x \<partial>M) = 0"
   470       using positive_integral_positive by (auto intro!: antisym)
   471   qed
   472   have upper_bound: "\<forall>A\<in>sets M. ?t A \<le> 0"
   473   proof (rule ccontr)
   474     assume "\<not> ?thesis"
   475     then obtain A where A: "A \<in> sets M" and pos: "0 < ?t A"
   476       by (auto simp: not_le)
   477     note pos
   478     also have "?t A \<le> ?t (space M)"
   479       using M.measure_mono[of A "space M"] A sets_into_space by simp
   480     finally have pos_t: "0 < ?t (space M)" by simp
   481     moreover
   482     then have "\<mu> (space M) \<noteq> 0"
   483       using ac unfolding absolutely_continuous_def by auto
   484     then have pos_M: "0 < \<mu> (space M)"
   485       using positive_measure[OF top] by (simp add: le_less)
   486     moreover
   487     have "(\<integral>\<^isup>+x. f x * indicator (space M) x \<partial>M) \<le> \<nu> (space M)"
   488       using `f \<in> G` unfolding G_def by auto
   489     hence "(\<integral>\<^isup>+x. f x * indicator (space M) x \<partial>M) \<noteq> \<infinity>"
   490       using M'.finite_measure_of_space by auto
   491     moreover
   492     def b \<equiv> "?t (space M) / \<mu> (space M) / 2"
   493     ultimately have b: "b \<noteq> 0 \<and> 0 \<le> b \<and> b \<noteq> \<infinity>"
   494       using M'.finite_measure_of_space positive_integral_positive[of "?F (space M)"]
   495       by (cases rule: ereal3_cases[of "integral\<^isup>P M (?F (space M))" "\<nu> (space M)" "\<mu> (space M)"])
   496          (simp_all add: field_simps)
   497     then have b: "b \<noteq> 0" "0 \<le> b" "0 < b"  "b \<noteq> \<infinity>" by auto
   498     let ?Mb = "?M\<lparr>measure := \<lambda>A. b * \<mu> A\<rparr>"
   499     interpret b: sigma_algebra ?Mb by (intro sigma_algebra_cong) auto
   500     have Mb: "finite_measure ?Mb"
   501     proof
   502       show "positive ?Mb (measure ?Mb)"
   503         using `0 \<le> b` by (auto simp: positive_def)
   504       show "countably_additive ?Mb (measure ?Mb)"
   505         using `0 \<le> b` measure_countably_additive
   506         by (auto simp: countably_additive_def suminf_cmult_ereal subset_eq)
   507       show "measure ?Mb (space ?Mb) \<noteq> \<infinity>"
   508         using b by auto
   509     qed
   510     from M.Radon_Nikodym_aux[OF this]
   511     obtain A0 where "A0 \<in> sets M" and
   512       space_less_A0: "real (?t (space M)) - real (b * \<mu> (space M)) \<le> real (?t A0) - real (b * \<mu> A0)" and
   513       *: "\<And>B. \<lbrakk> B \<in> sets M ; B \<subseteq> A0 \<rbrakk> \<Longrightarrow> 0 \<le> real (?t B) - real (b * \<mu> B)"
   514       unfolding M.\<mu>'_def finite_measure.\<mu>'_def[OF Mb] by auto
   515     { fix B assume B: "B \<in> sets M" "B \<subseteq> A0"
   516       with *[OF this] have "b * \<mu> B \<le> ?t B"
   517         using M'.finite_measure b finite_measure M.positive_measure[OF B(1)]
   518         by (cases rule: ereal2_cases[of "?t B" "b * \<mu> B"]) auto }
   519     note bM_le_t = this
   520     let "?f0 x" = "f x + b * indicator A0 x"
   521     { fix A assume A: "A \<in> sets M"
   522       hence "A \<inter> A0 \<in> sets M" using `A0 \<in> sets M` by auto
   523       have "(\<integral>\<^isup>+x. ?f0 x  * indicator A x \<partial>M) =
   524         (\<integral>\<^isup>+x. f x * indicator A x + b * indicator (A \<inter> A0) x \<partial>M)"
   525         by (auto intro!: positive_integral_cong split: split_indicator)
   526       hence "(\<integral>\<^isup>+x. ?f0 x * indicator A x \<partial>M) =
   527           (\<integral>\<^isup>+x. f x * indicator A x \<partial>M) + b * \<mu> (A \<inter> A0)"
   528         using `A0 \<in> sets M` `A \<inter> A0 \<in> sets M` A b `f \<in> G`
   529         by (simp add: G_def positive_integral_add positive_integral_cmult_indicator) }
   530     note f0_eq = this
   531     { fix A assume A: "A \<in> sets M"
   532       hence "A \<inter> A0 \<in> sets M" using `A0 \<in> sets M` by auto
   533       have f_le_v: "(\<integral>\<^isup>+x. f x * indicator A x \<partial>M) \<le> \<nu> A"
   534         using `f \<in> G` A unfolding G_def by auto
   535       note f0_eq[OF A]
   536       also have "(\<integral>\<^isup>+x. f x * indicator A x \<partial>M) + b * \<mu> (A \<inter> A0) \<le>
   537           (\<integral>\<^isup>+x. f x * indicator A x \<partial>M) + ?t (A \<inter> A0)"
   538         using bM_le_t[OF `A \<inter> A0 \<in> sets M`] `A \<in> sets M` `A0 \<in> sets M`
   539         by (auto intro!: add_left_mono)
   540       also have "\<dots> \<le> (\<integral>\<^isup>+x. f x * indicator A x \<partial>M) + ?t A"
   541         using M.measure_mono[simplified, OF _ `A \<inter> A0 \<in> sets M` `A \<in> sets M`]
   542         by (auto intro!: add_left_mono)
   543       also have "\<dots> \<le> \<nu> A"
   544         using f_le_v M'.finite_measure[simplified, OF `A \<in> sets M`] positive_integral_positive[of "?F A"]
   545         by (cases "\<integral>\<^isup>+x. ?F A x \<partial>M", cases "\<nu> A") auto
   546       finally have "(\<integral>\<^isup>+x. ?f0 x * indicator A x \<partial>M) \<le> \<nu> A" . }
   547     hence "?f0 \<in> G" using `A0 \<in> sets M` b `f \<in> G` unfolding G_def
   548       by (auto intro!: borel_measurable_indicator borel_measurable_ereal_add
   549                        borel_measurable_ereal_times ereal_add_nonneg_nonneg)
   550     have real: "?t (space M) \<noteq> \<infinity>" "?t A0 \<noteq> \<infinity>"
   551       "b * \<mu> (space M) \<noteq> \<infinity>" "b * \<mu> A0 \<noteq> \<infinity>"
   552       using `A0 \<in> sets M` b
   553         finite_measure[of A0] M.finite_measure[of A0]
   554         finite_measure_of_space M.finite_measure_of_space
   555       by auto
   556     have int_f_finite: "integral\<^isup>P M f \<noteq> \<infinity>"
   557       using M'.finite_measure_of_space pos_t unfolding ereal_less_minus_iff
   558       by (auto cong: positive_integral_cong)
   559     have  "0 < ?t (space M) - b * \<mu> (space M)" unfolding b_def
   560       using finite_measure_of_space M'.finite_measure_of_space pos_t pos_M
   561       using positive_integral_positive[of "?F (space M)"]
   562       by (cases rule: ereal3_cases[of "\<mu> (space M)" "\<nu> (space M)" "integral\<^isup>P M (?F (space M))"])
   563          (auto simp: field_simps mult_less_cancel_left)
   564     also have "\<dots> \<le> ?t A0 - b * \<mu> A0"
   565       using space_less_A0 b
   566       using
   567         `A0 \<in> sets M`[THEN M.real_measure]
   568         top[THEN M.real_measure]
   569       apply safe
   570       apply simp
   571       using
   572         `A0 \<in> sets M`[THEN real_measure]
   573         `A0 \<in> sets M`[THEN M'.real_measure]
   574         top[THEN real_measure]
   575         top[THEN M'.real_measure]
   576       by (cases b) auto
   577     finally have 1: "b * \<mu> A0 < ?t A0"
   578       using
   579         `A0 \<in> sets M`[THEN M.real_measure]
   580       apply safe
   581       apply simp
   582       using
   583         `A0 \<in> sets M`[THEN real_measure]
   584         `A0 \<in> sets M`[THEN M'.real_measure]
   585       by (cases b) auto
   586     have "0 < ?t A0"
   587       using b `A0 \<in> sets M` by (auto intro!: le_less_trans[OF _ 1])
   588     then have "\<mu> A0 \<noteq> 0" using ac unfolding absolutely_continuous_def
   589       using `A0 \<in> sets M` by auto
   590     then have "0 < \<mu> A0" using positive_measure[OF `A0 \<in> sets M`] by auto
   591     hence "0 < b * \<mu> A0" using b by (auto simp: ereal_zero_less_0_iff)
   592     with int_f_finite have "?y + 0 < integral\<^isup>P M f + b * \<mu> A0" unfolding int_f_eq_y
   593       using `f \<in> G`
   594       by (intro ereal_add_strict_mono) (auto intro!: le_SUPI2 positive_integral_positive)
   595     also have "\<dots> = integral\<^isup>P M ?f0" using f0_eq[OF top] `A0 \<in> sets M` sets_into_space
   596       by (simp cong: positive_integral_cong)
   597     finally have "?y < integral\<^isup>P M ?f0" by simp
   598     moreover from `?f0 \<in> G` have "integral\<^isup>P M ?f0 \<le> ?y" by (auto intro!: le_SUPI)
   599     ultimately show False by auto
   600   qed
   601   show ?thesis
   602   proof (safe intro!: bexI[of _ f])
   603     fix A assume A: "A\<in>sets M"
   604     show "\<nu> A = (\<integral>\<^isup>+x. f x * indicator A x \<partial>M)"
   605     proof (rule antisym)
   606       show "(\<integral>\<^isup>+x. f x * indicator A x \<partial>M) \<le> \<nu> A"
   607         using `f \<in> G` `A \<in> sets M` unfolding G_def by auto
   608       show "\<nu> A \<le> (\<integral>\<^isup>+x. f x * indicator A x \<partial>M)"
   609         using upper_bound[THEN bspec, OF `A \<in> sets M`]
   610         using M'.real_measure[OF A]
   611         by (cases "integral\<^isup>P M (?F A)") auto
   612     qed
   613   qed simp
   614 qed
   616 lemma (in finite_measure) split_space_into_finite_sets_and_rest:
   617   assumes "measure_space (M\<lparr>measure := \<nu>\<rparr>)" (is "measure_space ?N")
   618   assumes ac: "absolutely_continuous \<nu>"
   619   shows "\<exists>A0\<in>sets M. \<exists>B::nat\<Rightarrow>'a set. disjoint_family B \<and> range B \<subseteq> sets M \<and> A0 = space M - (\<Union>i. B i) \<and>
   620     (\<forall>A\<in>sets M. A \<subseteq> A0 \<longrightarrow> (\<mu> A = 0 \<and> \<nu> A = 0) \<or> (\<mu> A > 0 \<and> \<nu> A = \<infinity>)) \<and>
   621     (\<forall>i. \<nu> (B i) \<noteq> \<infinity>)"
   622 proof -
   623   interpret v: measure_space ?N
   624     where "space ?N = space M" and "sets ?N = sets M" and "measure ?N = \<nu>"
   625     by fact auto
   626   let ?Q = "{Q\<in>sets M. \<nu> Q \<noteq> \<infinity>}"
   627   let ?a = "SUP Q:?Q. \<mu> Q"
   628   have "{} \<in> ?Q" using v.empty_measure by auto
   629   then have Q_not_empty: "?Q \<noteq> {}" by blast
   630   have "?a \<le> \<mu> (space M)" using sets_into_space
   631     by (auto intro!: SUP_leI measure_mono top)
   632   then have "?a \<noteq> \<infinity>" using finite_measure_of_space
   633     by auto
   634   from SUPR_countable_SUPR[OF Q_not_empty, of \<mu>]
   635   obtain Q'' where "range Q'' \<subseteq> \<mu> ` ?Q" and a: "?a = (SUP i::nat. Q'' i)"
   636     by auto
   637   then have "\<forall>i. \<exists>Q'. Q'' i = \<mu> Q' \<and> Q' \<in> ?Q" by auto
   638   from choice[OF this] obtain Q' where Q': "\<And>i. Q'' i = \<mu> (Q' i)" "\<And>i. Q' i \<in> ?Q"
   639     by auto
   640   then have a_Lim: "?a = (SUP i::nat. \<mu> (Q' i))" using a by simp
   641   let "?O n" = "\<Union>i\<le>n. Q' i"
   642   have Union: "(SUP i. \<mu> (?O i)) = \<mu> (\<Union>i. ?O i)"
   643   proof (rule continuity_from_below[of ?O])
   644     show "range ?O \<subseteq> sets M" using Q' by (auto intro!: finite_UN)
   645     show "incseq ?O" by (fastforce intro!: incseq_SucI)
   646   qed
   647   have Q'_sets: "\<And>i. Q' i \<in> sets M" using Q' by auto
   648   have O_sets: "\<And>i. ?O i \<in> sets M"
   649      using Q' by (auto intro!: finite_UN Un)
   650   then have O_in_G: "\<And>i. ?O i \<in> ?Q"
   651   proof (safe del: notI)
   652     fix i have "Q' ` {..i} \<subseteq> sets M"
   653       using Q' by (auto intro: finite_UN)
   654     with v.measure_finitely_subadditive[of "{.. i}" Q']
   655     have "\<nu> (?O i) \<le> (\<Sum>i\<le>i. \<nu> (Q' i))" by auto
   656     also have "\<dots> < \<infinity>" using Q' by (simp add: setsum_Pinfty)
   657     finally show "\<nu> (?O i) \<noteq> \<infinity>" by simp
   658   qed auto
   659   have O_mono: "\<And>n. ?O n \<subseteq> ?O (Suc n)" by fastforce
   660   have a_eq: "?a = \<mu> (\<Union>i. ?O i)" unfolding Union[symmetric]
   661   proof (rule antisym)
   662     show "?a \<le> (SUP i. \<mu> (?O i))" unfolding a_Lim
   663       using Q' by (auto intro!: SUP_mono measure_mono finite_UN)
   664     show "(SUP i. \<mu> (?O i)) \<le> ?a" unfolding SUPR_def
   665     proof (safe intro!: Sup_mono, unfold bex_simps)
   666       fix i
   667       have *: "(\<Union>Q' ` {..i}) = ?O i" by auto
   668       then show "\<exists>x. (x \<in> sets M \<and> \<nu> x \<noteq> \<infinity>) \<and>
   669         \<mu> (\<Union>Q' ` {..i}) \<le> \<mu> x"
   670         using O_in_G[of i] by (auto intro!: exI[of _ "?O i"])
   671     qed
   672   qed
   673   let "?O_0" = "(\<Union>i. ?O i)"
   674   have "?O_0 \<in> sets M" using Q' by auto
   675   def Q \<equiv> "\<lambda>i. case i of 0 \<Rightarrow> Q' 0 | Suc n \<Rightarrow> ?O (Suc n) - ?O n"
   676   { fix i have "Q i \<in> sets M" unfolding Q_def using Q'[of 0] by (cases i) (auto intro: O_sets) }
   677   note Q_sets = this
   678   show ?thesis
   679   proof (intro bexI exI conjI ballI impI allI)
   680     show "disjoint_family Q"
   681       by (fastforce simp: disjoint_family_on_def Q_def
   682         split: nat.split_asm)
   683     show "range Q \<subseteq> sets M"
   684       using Q_sets by auto
   685     { fix A assume A: "A \<in> sets M" "A \<subseteq> space M - ?O_0"
   686       show "\<mu> A = 0 \<and> \<nu> A = 0 \<or> 0 < \<mu> A \<and> \<nu> A = \<infinity>"
   687       proof (rule disjCI, simp)
   688         assume *: "0 < \<mu> A \<longrightarrow> \<nu> A \<noteq> \<infinity>"
   689         show "\<mu> A = 0 \<and> \<nu> A = 0"
   690         proof cases
   691           assume "\<mu> A = 0" moreover with ac A have "\<nu> A = 0"
   692             unfolding absolutely_continuous_def by auto
   693           ultimately show ?thesis by simp
   694         next
   695           assume "\<mu> A \<noteq> 0" with * have "\<nu> A \<noteq> \<infinity>" using positive_measure[OF A(1)] by auto
   696           with A have "\<mu> ?O_0 + \<mu> A = \<mu> (?O_0 \<union> A)"
   697             using Q' by (auto intro!: measure_additive countable_UN)
   698           also have "\<dots> = (SUP i. \<mu> (?O i \<union> A))"
   699           proof (rule continuity_from_below[of "\<lambda>i. ?O i \<union> A", symmetric, simplified])
   700             show "range (\<lambda>i. ?O i \<union> A) \<subseteq> sets M"
   701               using `\<nu> A \<noteq> \<infinity>` O_sets A by auto
   702           qed (fastforce intro!: incseq_SucI)
   703           also have "\<dots> \<le> ?a"
   704           proof (safe intro!: SUP_leI)
   705             fix i have "?O i \<union> A \<in> ?Q"
   706             proof (safe del: notI)
   707               show "?O i \<union> A \<in> sets M" using O_sets A by auto
   708               from O_in_G[of i]
   709               moreover have "\<nu> (?O i \<union> A) \<le> \<nu> (?O i) + \<nu> A"
   710                 using v.measure_subadditive[of "?O i" A] A O_sets by auto
   711               ultimately show "\<nu> (?O i \<union> A) \<noteq> \<infinity>"
   712                 using `\<nu> A \<noteq> \<infinity>` by auto
   713             qed
   714             then show "\<mu> (?O i \<union> A) \<le> ?a" by (rule le_SUPI)
   715           qed
   716           finally have "\<mu> A = 0"
   717             unfolding a_eq using real_measure[OF `?O_0 \<in> sets M`] real_measure[OF A(1)] by auto
   718           with `\<mu> A \<noteq> 0` show ?thesis by auto
   719         qed
   720       qed }
   721     { fix i show "\<nu> (Q i) \<noteq> \<infinity>"
   722       proof (cases i)
   723         case 0 then show ?thesis
   724           unfolding Q_def using Q'[of 0] by simp
   725       next
   726         case (Suc n)
   727         then show ?thesis unfolding Q_def
   728           using `?O n \<in> ?Q` `?O (Suc n) \<in> ?Q`
   729           using v.measure_mono[OF O_mono, of n] v.positive_measure[of "?O n"] v.positive_measure[of "?O (Suc n)"]
   730           using v.measure_Diff[of "?O n" "?O (Suc n)", OF _ _ _ O_mono]
   731           by (cases rule: ereal2_cases[of "\<nu> (\<Union> x\<le>Suc n. Q' x)" "\<nu> (\<Union> i\<le>n. Q' i)"]) auto
   732       qed }
   733     show "space M - ?O_0 \<in> sets M" using Q'_sets by auto
   734     { fix j have "(\<Union>i\<le>j. ?O i) = (\<Union>i\<le>j. Q i)"
   735       proof (induct j)
   736         case 0 then show ?case by (simp add: Q_def)
   737       next
   738         case (Suc j)
   739         have eq: "\<And>j. (\<Union>i\<le>j. ?O i) = (\<Union>i\<le>j. Q' i)" by fastforce
   740         have "{..j} \<union> {..Suc j} = {..Suc j}" by auto
   741         then have "(\<Union>i\<le>Suc j. Q' i) = (\<Union>i\<le>j. Q' i) \<union> Q (Suc j)"
   742           by (simp add: UN_Un[symmetric] Q_def del: UN_Un)
   743         then show ?case using Suc by (auto simp add: eq atMost_Suc)
   744       qed }
   745     then have "(\<Union>j. (\<Union>i\<le>j. ?O i)) = (\<Union>j. (\<Union>i\<le>j. Q i))" by simp
   746     then show "space M - ?O_0 = space M - (\<Union>i. Q i)" by fastforce
   747   qed
   748 qed
   750 lemma (in finite_measure) Radon_Nikodym_finite_measure_infinite:
   751   assumes "measure_space (M\<lparr>measure := \<nu>\<rparr>)" (is "measure_space ?N")
   752   assumes "absolutely_continuous \<nu>"
   753   shows "\<exists>f \<in> borel_measurable M. (\<forall>x. 0 \<le> f x) \<and> (\<forall>A\<in>sets M. \<nu> A = (\<integral>\<^isup>+x. f x * indicator A x \<partial>M))"
   754 proof -
   755   interpret v: measure_space ?N
   756     where "space ?N = space M" and "sets ?N = sets M" and "measure ?N = \<nu>"
   757     by fact auto
   758   from split_space_into_finite_sets_and_rest[OF assms]
   759   obtain Q0 and Q :: "nat \<Rightarrow> 'a set"
   760     where Q: "disjoint_family Q" "range Q \<subseteq> sets M"
   761     and Q0: "Q0 \<in> sets M" "Q0 = space M - (\<Union>i. Q i)"
   762     and in_Q0: "\<And>A. A \<in> sets M \<Longrightarrow> A \<subseteq> Q0 \<Longrightarrow> \<mu> A = 0 \<and> \<nu> A = 0 \<or> 0 < \<mu> A \<and> \<nu> A = \<infinity>"
   763     and Q_fin: "\<And>i. \<nu> (Q i) \<noteq> \<infinity>" by force
   764   from Q have Q_sets: "\<And>i. Q i \<in> sets M" by auto
   765   have "\<forall>i. \<exists>f. f\<in>borel_measurable M \<and> (\<forall>x. 0 \<le> f x) \<and> (\<forall>A\<in>sets M.
   766     \<nu> (Q i \<inter> A) = (\<integral>\<^isup>+x. f x * indicator (Q i \<inter> A) x \<partial>M))"
   767   proof
   768     fix i
   769     have indicator_eq: "\<And>f x A. (f x :: ereal) * indicator (Q i \<inter> A) x * indicator (Q i) x
   770       = (f x * indicator (Q i) x) * indicator A x"
   771       unfolding indicator_def by auto
   772     have fm: "finite_measure (restricted_space (Q i))"
   773       (is "finite_measure ?R") by (rule restricted_finite_measure[OF Q_sets[of i]])
   774     then interpret R: finite_measure ?R .
   775     have fmv: "finite_measure (restricted_space (Q i) \<lparr> measure := \<nu>\<rparr>)" (is "finite_measure ?Q")
   776       unfolding finite_measure_def finite_measure_axioms_def
   777     proof
   778       show "measure_space ?Q"
   779         using v.restricted_measure_space Q_sets[of i] by auto
   780       show "measure ?Q (space ?Q) \<noteq> \<infinity>" using Q_fin by simp
   781     qed
   782     have "R.absolutely_continuous \<nu>"
   783       using `absolutely_continuous \<nu>` `Q i \<in> sets M`
   784       by (auto simp: R.absolutely_continuous_def absolutely_continuous_def)
   785     from R.Radon_Nikodym_finite_measure[OF fmv this]
   786     obtain f where f: "(\<lambda>x. f x * indicator (Q i) x) \<in> borel_measurable M"
   787       and f_int: "\<And>A. A\<in>sets M \<Longrightarrow> \<nu> (Q i \<inter> A) = (\<integral>\<^isup>+x. (f x * indicator (Q i) x) * indicator A x \<partial>M)"
   788       unfolding Bex_def borel_measurable_restricted[OF `Q i \<in> sets M`]
   789         positive_integral_restricted[OF `Q i \<in> sets M`] by (auto simp: indicator_eq)
   790     then show "\<exists>f. f\<in>borel_measurable M \<and> (\<forall>x. 0 \<le> f x) \<and> (\<forall>A\<in>sets M.
   791       \<nu> (Q i \<inter> A) = (\<integral>\<^isup>+x. f x * indicator (Q i \<inter> A) x \<partial>M))"
   792       by (auto intro!: exI[of _ "\<lambda>x. max 0 (f x * indicator (Q i) x)"] positive_integral_cong_pos
   793         split: split_indicator split_if_asm simp: max_def)
   794   qed
   795   from choice[OF this] obtain f where borel: "\<And>i. f i \<in> borel_measurable M" "\<And>i x. 0 \<le> f i x"
   796     and f: "\<And>A i. A \<in> sets M \<Longrightarrow>
   797       \<nu> (Q i \<inter> A) = (\<integral>\<^isup>+x. f i x * indicator (Q i \<inter> A) x \<partial>M)"
   798     by auto
   799   let "?f x" = "(\<Sum>i. f i x * indicator (Q i) x) + \<infinity> * indicator Q0 x"
   800   show ?thesis
   801   proof (safe intro!: bexI[of _ ?f])
   802     show "?f \<in> borel_measurable M" using Q0 borel Q_sets
   803       by (auto intro!: measurable_If)
   804     show "\<And>x. 0 \<le> ?f x" using borel by (auto intro!: suminf_0_le simp: indicator_def)
   805     fix A assume "A \<in> sets M"
   806     have Qi: "\<And>i. Q i \<in> sets M" using Q by auto
   807     have [intro,simp]: "\<And>i. (\<lambda>x. f i x * indicator (Q i \<inter> A) x) \<in> borel_measurable M"
   808       "\<And>i. AE x. 0 \<le> f i x * indicator (Q i \<inter> A) x"
   809       using borel Qi Q0(1) `A \<in> sets M` by (auto intro!: borel_measurable_ereal_times)
   810     have "(\<integral>\<^isup>+x. ?f x * indicator A x \<partial>M) = (\<integral>\<^isup>+x. (\<Sum>i. f i x * indicator (Q i \<inter> A) x) + \<infinity> * indicator (Q0 \<inter> A) x \<partial>M)"
   811       using borel by (intro positive_integral_cong) (auto simp: indicator_def)
   812     also have "\<dots> = (\<integral>\<^isup>+x. (\<Sum>i. f i x * indicator (Q i \<inter> A) x) \<partial>M) + \<infinity> * \<mu> (Q0 \<inter> A)"
   813       using borel Qi Q0(1) `A \<in> sets M`
   814       by (subst positive_integral_add) (auto simp del: ereal_infty_mult
   815           simp add: positive_integral_cmult_indicator Int intro!: suminf_0_le)
   816     also have "\<dots> = (\<Sum>i. \<nu> (Q i \<inter> A)) + \<infinity> * \<mu> (Q0 \<inter> A)"
   817       by (subst f[OF `A \<in> sets M`], subst positive_integral_suminf) auto
   818     finally have "(\<integral>\<^isup>+x. ?f x * indicator A x \<partial>M) = (\<Sum>i. \<nu> (Q i \<inter> A)) + \<infinity> * \<mu> (Q0 \<inter> A)" .
   819     moreover have "(\<Sum>i. \<nu> (Q i \<inter> A)) = \<nu> ((\<Union>i. Q i) \<inter> A)"
   820       using Q Q_sets `A \<in> sets M`
   821       by (intro v.measure_countably_additive[of "\<lambda>i. Q i \<inter> A", unfolded comp_def, simplified])
   822          (auto simp: disjoint_family_on_def)
   823     moreover have "\<infinity> * \<mu> (Q0 \<inter> A) = \<nu> (Q0 \<inter> A)"
   824     proof -
   825       have "Q0 \<inter> A \<in> sets M" using Q0(1) `A \<in> sets M` by blast
   826       from in_Q0[OF this] show ?thesis by auto
   827     qed
   828     moreover have "Q0 \<inter> A \<in> sets M" "((\<Union>i. Q i) \<inter> A) \<in> sets M"
   829       using Q_sets `A \<in> sets M` Q0(1) by (auto intro!: countable_UN)
   830     moreover have "((\<Union>i. Q i) \<inter> A) \<union> (Q0 \<inter> A) = A" "((\<Union>i. Q i) \<inter> A) \<inter> (Q0 \<inter> A) = {}"
   831       using `A \<in> sets M` sets_into_space Q0 by auto
   832     ultimately show "\<nu> A = (\<integral>\<^isup>+x. ?f x * indicator A x \<partial>M)"
   833       using v.measure_additive[simplified, of "(\<Union>i. Q i) \<inter> A" "Q0 \<inter> A"]
   834       by simp
   835   qed
   836 qed
   838 lemma (in sigma_finite_measure) Radon_Nikodym:
   839   assumes "measure_space (M\<lparr>measure := \<nu>\<rparr>)" (is "measure_space ?N")
   840   assumes ac: "absolutely_continuous \<nu>"
   841   shows "\<exists>f \<in> borel_measurable M. (\<forall>x. 0 \<le> f x) \<and> (\<forall>A\<in>sets M. \<nu> A = (\<integral>\<^isup>+x. f x * indicator A x \<partial>M))"
   842 proof -
   843   from Ex_finite_integrable_function
   844   obtain h where finite: "integral\<^isup>P M h \<noteq> \<infinity>" and
   845     borel: "h \<in> borel_measurable M" and
   846     nn: "\<And>x. 0 \<le> h x" and
   847     pos: "\<And>x. x \<in> space M \<Longrightarrow> 0 < h x" and
   848     "\<And>x. x \<in> space M \<Longrightarrow> h x < \<infinity>" by auto
   849   let "?T A" = "(\<integral>\<^isup>+x. h x * indicator A x \<partial>M)"
   850   let ?MT = "M\<lparr> measure := ?T \<rparr>"
   851   interpret T: finite_measure ?MT
   852     where "space ?MT = space M" and "sets ?MT = sets M" and "measure ?MT = ?T"
   853     unfolding finite_measure_def finite_measure_axioms_def using borel finite nn
   854     by (auto intro!: measure_space_density cong: positive_integral_cong)
   855   have "T.absolutely_continuous \<nu>"
   856   proof (unfold T.absolutely_continuous_def, safe)
   857     fix N assume "N \<in> sets M" "(\<integral>\<^isup>+x. h x * indicator N x \<partial>M) = 0"
   858     with borel ac pos have "AE x. x \<notin> N"
   859       by (subst (asm) positive_integral_0_iff_AE) (auto split: split_indicator simp: not_le)
   860     then have "N \<in> null_sets" using `N \<in> sets M` sets_into_space
   861       by (subst (asm) AE_iff_measurable[OF `N \<in> sets M`]) auto
   862     then show "\<nu> N = 0" using ac by (auto simp: absolutely_continuous_def)
   863   qed
   864   from T.Radon_Nikodym_finite_measure_infinite[simplified, OF assms(1) this]
   865   obtain f where f_borel: "f \<in> borel_measurable M" "\<And>x. 0 \<le> f x" and
   866     fT: "\<And>A. A \<in> sets M \<Longrightarrow> \<nu> A = (\<integral>\<^isup>+ x. f x * indicator A x \<partial>?MT)"
   867     by (auto simp: measurable_def)
   868   show ?thesis
   869   proof (safe intro!: bexI[of _ "\<lambda>x. h x * f x"])
   870     show "(\<lambda>x. h x * f x) \<in> borel_measurable M"
   871       using borel f_borel by (auto intro: borel_measurable_ereal_times)
   872     show "\<And>x. 0 \<le> h x * f x" using nn f_borel by auto
   873     fix A assume "A \<in> sets M"
   874     then show "\<nu> A = (\<integral>\<^isup>+x. h x * f x * indicator A x \<partial>M)"
   875       unfolding fT[OF `A \<in> sets M`] mult_assoc using nn borel f_borel
   876       by (intro positive_integral_translated_density) auto
   877   qed
   878 qed
   880 section "Uniqueness of densities"
   882 lemma (in measure_space) finite_density_unique:
   883   assumes borel: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   884   assumes pos: "AE x. 0 \<le> f x" "AE x. 0 \<le> g x"
   885   and fin: "integral\<^isup>P M f \<noteq> \<infinity>"
   886   shows "(\<forall>A\<in>sets M. (\<integral>\<^isup>+x. f x * indicator A x \<partial>M) = (\<integral>\<^isup>+x. g x * indicator A x \<partial>M))
   887     \<longleftrightarrow> (AE x. f x = g x)"
   888     (is "(\<forall>A\<in>sets M. ?P f A = ?P g A) \<longleftrightarrow> _")
   889 proof (intro iffI ballI)
   890   fix A assume eq: "AE x. f x = g x"
   891   then show "?P f A = ?P g A"
   892     by (auto intro: positive_integral_cong_AE)
   893 next
   894   assume eq: "\<forall>A\<in>sets M. ?P f A = ?P g A"
   895   from this[THEN bspec, OF top] fin
   896   have g_fin: "integral\<^isup>P M g \<noteq> \<infinity>" by (simp cong: positive_integral_cong)
   897   { fix f g assume borel: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   898       and pos: "AE x. 0 \<le> f x" "AE x. 0 \<le> g x"
   899       and g_fin: "integral\<^isup>P M g \<noteq> \<infinity>" and eq: "\<forall>A\<in>sets M. ?P f A = ?P g A"
   900     let ?N = "{x\<in>space M. g x < f x}"
   901     have N: "?N \<in> sets M" using borel by simp
   902     have "?P g ?N \<le> integral\<^isup>P M g" using pos
   903       by (intro positive_integral_mono_AE) (auto split: split_indicator)
   904     then have Pg_fin: "?P g ?N \<noteq> \<infinity>" using g_fin by auto
   905     have "?P (\<lambda>x. (f x - g x)) ?N = (\<integral>\<^isup>+x. f x * indicator ?N x - g x * indicator ?N x \<partial>M)"
   906       by (auto intro!: positive_integral_cong simp: indicator_def)
   907     also have "\<dots> = ?P f ?N - ?P g ?N"
   908     proof (rule positive_integral_diff)
   909       show "(\<lambda>x. f x * indicator ?N x) \<in> borel_measurable M" "(\<lambda>x. g x * indicator ?N x) \<in> borel_measurable M"
   910         using borel N by auto
   911       show "AE x. g x * indicator ?N x \<le> f x * indicator ?N x"
   912            "AE x. 0 \<le> g x * indicator ?N x"
   913         using pos by (auto split: split_indicator)
   914     qed fact
   915     also have "\<dots> = 0"
   916       unfolding eq[THEN bspec, OF N] using positive_integral_positive Pg_fin by auto
   917     finally have "AE x. f x \<le> g x"
   918       using pos borel positive_integral_PInf_AE[OF borel(2) g_fin]
   919       by (subst (asm) positive_integral_0_iff_AE)
   920          (auto split: split_indicator simp: not_less ereal_minus_le_iff) }
   921   from this[OF borel pos g_fin eq] this[OF borel(2,1) pos(2,1) fin] eq
   922   show "AE x. f x = g x" by auto
   923 qed
   925 lemma (in finite_measure) density_unique_finite_measure:
   926   assumes borel: "f \<in> borel_measurable M" "f' \<in> borel_measurable M"
   927   assumes pos: "AE x. 0 \<le> f x" "AE x. 0 \<le> f' x"
   928   assumes f: "\<And>A. A \<in> sets M \<Longrightarrow> (\<integral>\<^isup>+x. f x * indicator A x \<partial>M) = (\<integral>\<^isup>+x. f' x * indicator A x \<partial>M)"
   929     (is "\<And>A. A \<in> sets M \<Longrightarrow> ?P f A = ?P f' A")
   930   shows "AE x. f x = f' x"
   931 proof -
   932   let "?\<nu> A" = "?P f A" and "?\<nu>' A" = "?P f' A"
   933   let "?f A x" = "f x * indicator A x" and "?f' A x" = "f' x * indicator A x"
   934   interpret M: measure_space "M\<lparr> measure := ?\<nu>\<rparr>"
   935     using borel(1) pos(1) by (rule measure_space_density) simp
   936   have ac: "absolutely_continuous ?\<nu>"
   937     using f by (rule density_is_absolutely_continuous)
   938   from split_space_into_finite_sets_and_rest[OF `measure_space (M\<lparr> measure := ?\<nu>\<rparr>)` ac]
   939   obtain Q0 and Q :: "nat \<Rightarrow> 'a set"
   940     where Q: "disjoint_family Q" "range Q \<subseteq> sets M"
   941     and Q0: "Q0 \<in> sets M" "Q0 = space M - (\<Union>i. Q i)"
   942     and in_Q0: "\<And>A. A \<in> sets M \<Longrightarrow> A \<subseteq> Q0 \<Longrightarrow> \<mu> A = 0 \<and> ?\<nu> A = 0 \<or> 0 < \<mu> A \<and> ?\<nu> A = \<infinity>"
   943     and Q_fin: "\<And>i. ?\<nu> (Q i) \<noteq> \<infinity>" by force
   944   from Q have Q_sets: "\<And>i. Q i \<in> sets M" by auto
   945   let ?N = "{x\<in>space M. f x \<noteq> f' x}"
   946   have "?N \<in> sets M" using borel by auto
   947   have *: "\<And>i x A. \<And>y::ereal. y * indicator (Q i) x * indicator A x = y * indicator (Q i \<inter> A) x"
   948     unfolding indicator_def by auto
   949   have "\<forall>i. AE x. ?f (Q i) x = ?f' (Q i) x" using borel Q_fin Q pos
   950     by (intro finite_density_unique[THEN iffD1] allI)
   951        (auto intro!: borel_measurable_ereal_times f Int simp: *)
   952   moreover have "AE x. ?f Q0 x = ?f' Q0 x"
   953   proof (rule AE_I')
   954     { fix f :: "'a \<Rightarrow> ereal" assume borel: "f \<in> borel_measurable M"
   955         and eq: "\<And>A. A \<in> sets M \<Longrightarrow> ?\<nu> A = (\<integral>\<^isup>+x. f x * indicator A x \<partial>M)"
   956       let "?A i" = "Q0 \<inter> {x \<in> space M. f x < (i::nat)}"
   957       have "(\<Union>i. ?A i) \<in> null_sets"
   958       proof (rule null_sets_UN)
   959         fix i ::nat have "?A i \<in> sets M"
   960           using borel Q0(1) by auto
   961         have "?\<nu> (?A i) \<le> (\<integral>\<^isup>+x. (i::ereal) * indicator (?A i) x \<partial>M)"
   962           unfolding eq[OF `?A i \<in> sets M`]
   963           by (auto intro!: positive_integral_mono simp: indicator_def)
   964         also have "\<dots> = i * \<mu> (?A i)"
   965           using `?A i \<in> sets M` by (auto intro!: positive_integral_cmult_indicator)
   966         also have "\<dots> < \<infinity>"
   967           using `?A i \<in> sets M`[THEN finite_measure] by auto
   968         finally have "?\<nu> (?A i) \<noteq> \<infinity>" by simp
   969         then show "?A i \<in> null_sets" using in_Q0[OF `?A i \<in> sets M`] `?A i \<in> sets M` by auto
   970       qed
   971       also have "(\<Union>i. ?A i) = Q0 \<inter> {x\<in>space M. f x \<noteq> \<infinity>}"
   972         by (auto simp: less_PInf_Ex_of_nat real_eq_of_nat)
   973       finally have "Q0 \<inter> {x\<in>space M. f x \<noteq> \<infinity>} \<in> null_sets" by simp }
   974     from this[OF borel(1) refl] this[OF borel(2) f]
   975     have "Q0 \<inter> {x\<in>space M. f x \<noteq> \<infinity>} \<in> null_sets" "Q0 \<inter> {x\<in>space M. f' x \<noteq> \<infinity>} \<in> null_sets" by simp_all
   976     then show "(Q0 \<inter> {x\<in>space M. f x \<noteq> \<infinity>}) \<union> (Q0 \<inter> {x\<in>space M. f' x \<noteq> \<infinity>}) \<in> null_sets" by (rule nullsets.Un)
   977     show "{x \<in> space M. ?f Q0 x \<noteq> ?f' Q0 x} \<subseteq>
   978       (Q0 \<inter> {x\<in>space M. f x \<noteq> \<infinity>}) \<union> (Q0 \<inter> {x\<in>space M. f' x \<noteq> \<infinity>})" by (auto simp: indicator_def)
   979   qed
   980   moreover have "\<And>x. (?f Q0 x = ?f' Q0 x) \<longrightarrow> (\<forall>i. ?f (Q i) x = ?f' (Q i) x) \<longrightarrow>
   981     ?f (space M) x = ?f' (space M) x"
   982     by (auto simp: indicator_def Q0)
   983   ultimately have "AE x. ?f (space M) x = ?f' (space M) x"
   984     by (auto simp: AE_all_countable[symmetric])
   985   then show "AE x. f x = f' x" by auto
   986 qed
   988 lemma (in sigma_finite_measure) density_unique:
   989   assumes f: "f \<in> borel_measurable M" "AE x. 0 \<le> f x"
   990   assumes f': "f' \<in> borel_measurable M" "AE x. 0 \<le> f' x"
   991   assumes eq: "\<And>A. A \<in> sets M \<Longrightarrow> (\<integral>\<^isup>+x. f x * indicator A x \<partial>M) = (\<integral>\<^isup>+x. f' x * indicator A x \<partial>M)"
   992     (is "\<And>A. A \<in> sets M \<Longrightarrow> ?P f A = ?P f' A")
   993   shows "AE x. f x = f' x"
   994 proof -
   995   obtain h where h_borel: "h \<in> borel_measurable M"
   996     and fin: "integral\<^isup>P 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"
   997     using Ex_finite_integrable_function by auto
   998   then have h_nn: "AE x. 0 \<le> h x" by auto
   999   let ?H = "M\<lparr> measure := \<lambda>A. (\<integral>\<^isup>+x. h x * indicator A x \<partial>M) \<rparr>"
  1000   have H: "measure_space ?H"
  1001     using h_borel h_nn by (rule measure_space_density) simp
  1002   then interpret h: measure_space ?H .
  1003   interpret h: finite_measure "M\<lparr> measure := \<lambda>A. (\<integral>\<^isup>+x. h x * indicator A x \<partial>M) \<rparr>"
  1004     by default (simp cong: positive_integral_cong add: fin)
  1005   let ?fM = "M\<lparr>measure := \<lambda>A. (\<integral>\<^isup>+x. f x * indicator A x \<partial>M)\<rparr>"
  1006   interpret f: measure_space ?fM
  1007     using f by (rule measure_space_density) simp
  1008   let ?f'M = "M\<lparr>measure := \<lambda>A. (\<integral>\<^isup>+x. f' x * indicator A x \<partial>M)\<rparr>"
  1009   interpret f': measure_space ?f'M
  1010     using f' by (rule measure_space_density) simp
  1011   { fix A assume "A \<in> sets M"
  1012     then have "{x \<in> space M. h x * indicator A x \<noteq> 0} = A"
  1013       using pos(1) sets_into_space by (force simp: indicator_def)
  1014     then have "(\<integral>\<^isup>+x. h x * indicator A x \<partial>M) = 0 \<longleftrightarrow> A \<in> null_sets"
  1015       using h_borel `A \<in> sets M` h_nn by (subst positive_integral_0_iff) auto }
  1016   note h_null_sets = this
  1017   { fix A assume "A \<in> sets M"
  1018     have "(\<integral>\<^isup>+x. f x * (h x * indicator A x) \<partial>M) = (\<integral>\<^isup>+x. h x * indicator A x \<partial>?fM)"
  1019       using `A \<in> sets M` h_borel h_nn f f'
  1020       by (intro positive_integral_translated_density[symmetric]) auto
  1021     also have "\<dots> = (\<integral>\<^isup>+x. h x * indicator A x \<partial>?f'M)"
  1022       by (rule f'.positive_integral_cong_measure) (simp_all add: eq)
  1023     also have "\<dots> = (\<integral>\<^isup>+x. f' x * (h x * indicator A x) \<partial>M)"
  1024       using `A \<in> sets M` h_borel h_nn f f'
  1025       by (intro positive_integral_translated_density) auto
  1026     finally have "(\<integral>\<^isup>+x. h x * (f x * indicator A x) \<partial>M) = (\<integral>\<^isup>+x. h x * (f' x * indicator A x) \<partial>M)"
  1027       by (simp add: ac_simps)
  1028     then have "(\<integral>\<^isup>+x. (f x * indicator A x) \<partial>?H) = (\<integral>\<^isup>+x. (f' x * indicator A x) \<partial>?H)"
  1029       using `A \<in> sets M` h_borel h_nn f f'
  1030       by (subst (asm) (1 2) positive_integral_translated_density[symmetric]) auto }
  1031   then have "AE x in ?H. f x = f' x" using h_borel h_nn f f'
  1032     by (intro h.density_unique_finite_measure absolutely_continuous_AE[OF H] density_is_absolutely_continuous)
  1033        simp_all
  1034   then show "AE x. f x = f' x"
  1035     unfolding h.almost_everywhere_def almost_everywhere_def
  1036     by (auto simp add: h_null_sets)
  1037 qed
  1039 lemma (in sigma_finite_measure) sigma_finite_iff_density_finite:
  1040   assumes \<nu>: "measure_space (M\<lparr>measure := \<nu>\<rparr>)" (is "measure_space ?N")
  1041     and f: "f \<in> borel_measurable M" "AE x. 0 \<le> f x"
  1042     and eq: "\<And>A. A \<in> sets M \<Longrightarrow> \<nu> A = (\<integral>\<^isup>+x. f x * indicator A x \<partial>M)"
  1043   shows "sigma_finite_measure (M\<lparr>measure := \<nu>\<rparr>) \<longleftrightarrow> (AE x. f x \<noteq> \<infinity>)"
  1044 proof
  1045   assume "sigma_finite_measure ?N"
  1046   then interpret \<nu>: sigma_finite_measure ?N
  1047     where "borel_measurable ?N = borel_measurable M" and "measure ?N = \<nu>"
  1048     and "sets ?N = sets M" and "space ?N = space M" by (auto simp: measurable_def)
  1049   from \<nu>.Ex_finite_integrable_function obtain h where
  1050     h: "h \<in> borel_measurable M" "integral\<^isup>P ?N h \<noteq> \<infinity>" and
  1051     h_nn: "\<And>x. 0 \<le> h x" and
  1052     fin: "\<forall>x\<in>space M. 0 < h x \<and> h x < \<infinity>" by auto
  1053   have "AE x. f x * h x \<noteq> \<infinity>"
  1054   proof (rule AE_I')
  1055     have "integral\<^isup>P ?N h = (\<integral>\<^isup>+x. f x * h x \<partial>M)" using f h h_nn
  1056       by (subst \<nu>.positive_integral_cong_measure[symmetric,
  1057           of "M\<lparr> measure := \<lambda> A. \<integral>\<^isup>+x. f x * indicator A x \<partial>M \<rparr>"])
  1058          (auto intro!: positive_integral_translated_density simp: eq)
  1059     then have "(\<integral>\<^isup>+x. f x * h x \<partial>M) \<noteq> \<infinity>"
  1060       using h(2) by simp
  1061     then show "(\<lambda>x. f x * h x) -` {\<infinity>} \<inter> space M \<in> null_sets"
  1062       using f h(1) by (auto intro!: positive_integral_PInf borel_measurable_vimage)
  1063   qed auto
  1064   then show "AE x. f x \<noteq> \<infinity>"
  1065     using fin by (auto elim!: AE_Ball_mp)
  1066 next
  1067   assume AE: "AE x. f x \<noteq> \<infinity>"
  1068   from sigma_finite guess Q .. note Q = this
  1069   interpret \<nu>: measure_space ?N
  1070     where "borel_measurable ?N = borel_measurable M" and "measure ?N = \<nu>"
  1071     and "sets ?N = sets M" and "space ?N = space M" using \<nu> by (auto simp: measurable_def)
  1072   def A \<equiv> "\<lambda>i. f -` (case i of 0 \<Rightarrow> {\<infinity>} | Suc n \<Rightarrow> {.. ereal(of_nat (Suc n))}) \<inter> space M"
  1073   { fix i j have "A i \<inter> Q j \<in> sets M"
  1074     unfolding A_def using f Q
  1075     apply (rule_tac Int)
  1076     by (cases i) (auto intro: measurable_sets[OF f(1)]) }
  1077   note A_in_sets = this
  1078   let "?A n" = "case prod_decode n of (i,j) \<Rightarrow> A i \<inter> Q j"
  1079   show "sigma_finite_measure ?N"
  1080   proof (default, intro exI conjI subsetI allI)
  1081     fix x assume "x \<in> range ?A"
  1082     then obtain n where n: "x = ?A n" by auto
  1083     then show "x \<in> sets ?N" using A_in_sets by (cases "prod_decode n") auto
  1084   next
  1085     have "(\<Union>i. ?A i) = (\<Union>i j. A i \<inter> Q j)"
  1086     proof safe
  1087       fix x i j assume "x \<in> A i" "x \<in> Q j"
  1088       then show "x \<in> (\<Union>i. case prod_decode i of (i, j) \<Rightarrow> A i \<inter> Q j)"
  1089         by (intro UN_I[of "prod_encode (i,j)"]) auto
  1090     qed auto
  1091     also have "\<dots> = (\<Union>i. A i) \<inter> space M" using Q by auto
  1092     also have "(\<Union>i. A i) = space M"
  1093     proof safe
  1094       fix x assume x: "x \<in> space M"
  1095       show "x \<in> (\<Union>i. A i)"
  1096       proof (cases "f x")
  1097         case PInf with x show ?thesis unfolding A_def by (auto intro: exI[of _ 0])
  1098       next
  1099         case (real r)
  1100         with less_PInf_Ex_of_nat[of "f x"] obtain n :: nat where "f x < n" by (auto simp: real_eq_of_nat)
  1101         then show ?thesis using x real unfolding A_def by (auto intro!: exI[of _ "Suc n"])
  1102       next
  1103         case MInf with x show ?thesis
  1104           unfolding A_def by (auto intro!: exI[of _ "Suc 0"])
  1105       qed
  1106     qed (auto simp: A_def)
  1107     finally show "(\<Union>i. ?A i) = space ?N" by simp
  1108   next
  1109     fix n obtain i j where
  1110       [simp]: "prod_decode n = (i, j)" by (cases "prod_decode n") auto
  1111     have "(\<integral>\<^isup>+x. f x * indicator (A i \<inter> Q j) x \<partial>M) \<noteq> \<infinity>"
  1112     proof (cases i)
  1113       case 0
  1114       have "AE x. f x * indicator (A i \<inter> Q j) x = 0"
  1115         using AE by (auto simp: A_def `i = 0`)
  1116       from positive_integral_cong_AE[OF this] show ?thesis by simp
  1117     next
  1118       case (Suc n)
  1119       then have "(\<integral>\<^isup>+x. f x * indicator (A i \<inter> Q j) x \<partial>M) \<le>
  1120         (\<integral>\<^isup>+x. (Suc n :: ereal) * indicator (Q j) x \<partial>M)"
  1121         by (auto intro!: positive_integral_mono simp: indicator_def A_def)
  1122       also have "\<dots> = Suc n * \<mu> (Q j)"
  1123         using Q by (auto intro!: positive_integral_cmult_indicator)
  1124       also have "\<dots> < \<infinity>"
  1125         using Q by (auto simp: real_eq_of_nat[symmetric])
  1126       finally show ?thesis by simp
  1127     qed
  1128     then show "measure ?N (?A n) \<noteq> \<infinity>"
  1129       using A_in_sets Q eq by auto
  1130   qed
  1131 qed
  1133 section "Radon-Nikodym derivative"
  1135 definition
  1136   "RN_deriv M \<nu> \<equiv> SOME f. f \<in> borel_measurable M \<and> (\<forall>x. 0 \<le> f x) \<and>
  1137     (\<forall>A \<in> sets M. \<nu> A = (\<integral>\<^isup>+x. f x * indicator A x \<partial>M))"
  1139 lemma (in sigma_finite_measure) RN_deriv_cong:
  1140   assumes cong: "\<And>A. A \<in> sets M \<Longrightarrow> measure M' A = \<mu> A" "sets M' = sets M" "space M' = space M"
  1141     and \<nu>: "\<And>A. A \<in> sets M \<Longrightarrow> \<nu>' A = \<nu> A"
  1142   shows "RN_deriv M' \<nu>' x = RN_deriv M \<nu> x"
  1143 proof -
  1144   interpret \<mu>': sigma_finite_measure M'
  1145     using cong by (rule sigma_finite_measure_cong)
  1146   show ?thesis
  1147     unfolding RN_deriv_def
  1148     by (simp add: cong \<nu> positive_integral_cong_measure[OF cong] measurable_def)
  1149 qed
  1151 lemma (in sigma_finite_measure) RN_deriv:
  1152   assumes "measure_space (M\<lparr>measure := \<nu>\<rparr>)"
  1153   assumes "absolutely_continuous \<nu>"
  1154   shows "RN_deriv M \<nu> \<in> borel_measurable M" (is ?borel)
  1155   and "\<And>A. A \<in> sets M \<Longrightarrow> \<nu> A = (\<integral>\<^isup>+x. RN_deriv M \<nu> x * indicator A x \<partial>M)"
  1156     (is "\<And>A. _ \<Longrightarrow> ?int A")
  1157   and "0 \<le> RN_deriv M \<nu> x"
  1158 proof -
  1159   note Ex = Radon_Nikodym[OF assms, unfolded Bex_def]
  1160   then show ?borel unfolding RN_deriv_def by (rule someI2_ex) auto
  1161   from Ex show "0 \<le> RN_deriv M \<nu> x" unfolding RN_deriv_def
  1162     by (rule someI2_ex) simp
  1163   fix A assume "A \<in> sets M"
  1164   from Ex show "?int A" unfolding RN_deriv_def
  1165     by (rule someI2_ex) (simp add: `A \<in> sets M`)
  1166 qed
  1168 lemma (in sigma_finite_measure) RN_deriv_positive_integral:
  1169   assumes \<nu>: "measure_space (M\<lparr>measure := \<nu>\<rparr>)" "absolutely_continuous \<nu>"
  1170     and f: "f \<in> borel_measurable M"
  1171   shows "integral\<^isup>P (M\<lparr>measure := \<nu>\<rparr>) f = (\<integral>\<^isup>+x. RN_deriv M \<nu> x * f x \<partial>M)"
  1172 proof -
  1173   interpret \<nu>: measure_space "M\<lparr>measure := \<nu>\<rparr>" by fact
  1174   note RN = RN_deriv[OF \<nu>]
  1175   have "integral\<^isup>P (M\<lparr>measure := \<nu>\<rparr>) f = (\<integral>\<^isup>+x. max 0 (f x) \<partial>M\<lparr>measure := \<nu>\<rparr>)"
  1176     unfolding positive_integral_max_0 ..
  1177   also have "(\<integral>\<^isup>+x. max 0 (f x) \<partial>M\<lparr>measure := \<nu>\<rparr>) =
  1178     (\<integral>\<^isup>+x. max 0 (f x) \<partial>M\<lparr>measure := \<lambda>A. (\<integral>\<^isup>+x. RN_deriv M \<nu> x * indicator A x \<partial>M)\<rparr>)"
  1179     by (intro \<nu>.positive_integral_cong_measure[symmetric]) (simp_all add: RN(2))
  1180   also have "\<dots> = (\<integral>\<^isup>+x. RN_deriv M \<nu> x * max 0 (f x) \<partial>M)"
  1181     by (intro positive_integral_translated_density) (auto simp add: RN f)
  1182   also have "\<dots> = (\<integral>\<^isup>+x. RN_deriv M \<nu> x * f x \<partial>M)"
  1183     using RN_deriv(3)[OF \<nu>]
  1184     by (auto intro!: positive_integral_cong_pos split: split_if_asm
  1185              simp: max_def ereal_mult_le_0_iff)
  1186   finally show ?thesis .
  1187 qed
  1189 lemma (in sigma_finite_measure) RN_deriv_unique:
  1190   assumes \<nu>: "measure_space (M\<lparr>measure := \<nu>\<rparr>)" "absolutely_continuous \<nu>"
  1191   and f: "f \<in> borel_measurable M" "AE x. 0 \<le> f x"
  1192   and eq: "\<And>A. A \<in> sets M \<Longrightarrow> \<nu> A = (\<integral>\<^isup>+x. f x * indicator A x \<partial>M)"
  1193   shows "AE x. f x = RN_deriv M \<nu> x"
  1194 proof (rule density_unique[OF f RN_deriv(1)[OF \<nu>]])
  1195   show "AE x. 0 \<le> RN_deriv M \<nu> x" using RN_deriv[OF \<nu>] by auto
  1196   fix A assume A: "A \<in> sets M"
  1197   show "(\<integral>\<^isup>+x. f x * indicator A x \<partial>M) = (\<integral>\<^isup>+x. RN_deriv M \<nu> x * indicator A x \<partial>M)"
  1198     unfolding eq[OF A, symmetric] RN_deriv(2)[OF \<nu> A, symmetric] ..
  1199 qed
  1201 lemma (in sigma_finite_measure) RN_deriv_vimage:
  1202   assumes T: "T \<in> measure_preserving M M'"
  1203     and T': "T' \<in> measure_preserving (M'\<lparr> measure := \<nu>' \<rparr>) (M\<lparr> measure := \<nu> \<rparr>)"
  1204     and inv: "\<And>x. x \<in> space M \<Longrightarrow> T' (T x) = x"
  1205   and \<nu>': "measure_space (M'\<lparr>measure := \<nu>'\<rparr>)" "measure_space.absolutely_continuous M' \<nu>'"
  1206   shows "AE x. RN_deriv M' \<nu>' (T x) = RN_deriv M \<nu> x"
  1207 proof (rule RN_deriv_unique)
  1208   interpret \<nu>': measure_space "M'\<lparr>measure := \<nu>'\<rparr>" by fact
  1209   show "measure_space (M\<lparr> measure := \<nu>\<rparr>)"
  1210     by (rule \<nu>'.measure_space_vimage[OF _ T'], simp) default
  1211   interpret M': measure_space M'
  1212   proof (rule measure_space_vimage)
  1213     have "sigma_algebra (M'\<lparr> measure := \<nu>'\<rparr>)" by default
  1214     then show "sigma_algebra M'" by simp
  1215   qed fact
  1216   show "absolutely_continuous \<nu>" unfolding absolutely_continuous_def
  1217   proof safe
  1218     fix N assume N: "N \<in> sets M" and N_0: "\<mu> N = 0"
  1219     then have N': "T' -` N \<inter> space M' \<in> sets M'"
  1220       using T' by (auto simp: measurable_def measure_preserving_def)
  1221     have "T -` (T' -` N \<inter> space M') \<inter> space M = N"
  1222       using inv T N sets_into_space[OF N] by (auto simp: measurable_def measure_preserving_def)
  1223     then have "measure M' (T' -` N \<inter> space M') = 0"
  1224       using measure_preservingD[OF T N'] N_0 by auto
  1225     with \<nu>'(2) N' show "\<nu> N = 0" using measure_preservingD[OF T', of N] N
  1226       unfolding M'.absolutely_continuous_def measurable_def by auto
  1227   qed
  1228   interpret M': sigma_finite_measure M'
  1229   proof
  1230     from sigma_finite guess F .. note F = this
  1231     show "\<exists>A::nat \<Rightarrow> 'c set. range A \<subseteq> sets M' \<and> (\<Union>i. A i) = space M' \<and> (\<forall>i. M'.\<mu> (A i) \<noteq> \<infinity>)"
  1232     proof (intro exI conjI allI)
  1233       show *: "range (\<lambda>i. T' -` F i \<inter> space M') \<subseteq> sets M'"
  1234         using F T' by (auto simp: measurable_def measure_preserving_def)
  1235       show "(\<Union>i. T' -` F i \<inter> space M') = space M'"
  1236         using F T' by (force simp: measurable_def measure_preserving_def)
  1237       fix i
  1238       have "T' -` F i \<inter> space M' \<in> sets M'" using * by auto
  1239       note measure_preservingD[OF T this, symmetric]
  1240       moreover
  1241       have Fi: "F i \<in> sets M" using F by auto
  1242       then have "T -` (T' -` F i \<inter> space M') \<inter> space M = F i"
  1243         using T inv sets_into_space[OF Fi]
  1244         by (auto simp: measurable_def measure_preserving_def)
  1245       ultimately show "measure M' (T' -` F i \<inter> space M') \<noteq> \<infinity>"
  1246         using F by simp
  1247     qed
  1248   qed
  1249   have "(RN_deriv M' \<nu>') \<circ> T \<in> borel_measurable M"
  1250     by (intro measurable_comp[where b=M'] M'.RN_deriv(1) measure_preservingD2[OF T]) fact+
  1251   then show "(\<lambda>x. RN_deriv M' \<nu>' (T x)) \<in> borel_measurable M"
  1252     by (simp add: comp_def)
  1253   show "AE x. 0 \<le> RN_deriv M' \<nu>' (T x)" using M'.RN_deriv(3)[OF \<nu>'] by auto
  1254   fix A let ?A = "T' -` A \<inter> space M'"
  1255   assume A: "A \<in> sets M"
  1256   then have A': "?A \<in> sets M'" using T' unfolding measurable_def measure_preserving_def
  1257     by auto
  1258   from A have "\<nu> A = \<nu>' ?A" using T'[THEN measure_preservingD] by simp
  1259   also have "\<dots> = \<integral>\<^isup>+ x. RN_deriv M' \<nu>' x * indicator ?A x \<partial>M'"
  1260     using A' by (rule M'.RN_deriv(2)[OF \<nu>'])
  1261   also have "\<dots> = \<integral>\<^isup>+ x. RN_deriv M' \<nu>' (T x) * indicator ?A (T x) \<partial>M"
  1262   proof (rule positive_integral_vimage)
  1263     show "sigma_algebra M'" by default
  1264     show "(\<lambda>x. RN_deriv M' \<nu>' x * indicator (T' -` A \<inter> space M') x) \<in> borel_measurable M'"
  1265       by (auto intro!: A' M'.RN_deriv(1)[OF \<nu>'])
  1266   qed fact
  1267   also have "\<dots> = \<integral>\<^isup>+ x. RN_deriv M' \<nu>' (T x) * indicator A x \<partial>M"
  1268     using T inv by (auto intro!: positive_integral_cong simp: measure_preserving_def measurable_def indicator_def)
  1269   finally show "\<nu> A = \<integral>\<^isup>+ x. RN_deriv M' \<nu>' (T x) * indicator A x \<partial>M" .
  1270 qed
  1272 lemma (in sigma_finite_measure) RN_deriv_finite:
  1273   assumes sfm: "sigma_finite_measure (M\<lparr>measure := \<nu>\<rparr>)" and ac: "absolutely_continuous \<nu>"
  1274   shows "AE x. RN_deriv M \<nu> x \<noteq> \<infinity>"
  1275 proof -
  1276   interpret \<nu>: sigma_finite_measure "M\<lparr>measure := \<nu>\<rparr>" by fact
  1277   have \<nu>: "measure_space (M\<lparr>measure := \<nu>\<rparr>)" by default
  1278   from sfm show ?thesis
  1279     using sigma_finite_iff_density_finite[OF \<nu> RN_deriv(1)[OF \<nu> ac]] RN_deriv(2,3)[OF \<nu> ac] by simp
  1280 qed
  1282 lemma (in sigma_finite_measure)
  1283   assumes \<nu>: "sigma_finite_measure (M\<lparr>measure := \<nu>\<rparr>)" "absolutely_continuous \<nu>"
  1284     and f: "f \<in> borel_measurable M"
  1285   shows RN_deriv_integrable: "integrable (M\<lparr>measure := \<nu>\<rparr>) f \<longleftrightarrow>
  1286       integrable M (\<lambda>x. real (RN_deriv M \<nu> x) * f x)" (is ?integrable)
  1287     and RN_deriv_integral: "integral\<^isup>L (M\<lparr>measure := \<nu>\<rparr>) f =
  1288       (\<integral>x. real (RN_deriv M \<nu> x) * f x \<partial>M)" (is ?integral)
  1289 proof -
  1290   interpret \<nu>: sigma_finite_measure "M\<lparr>measure := \<nu>\<rparr>" by fact
  1291   have ms: "measure_space (M\<lparr>measure := \<nu>\<rparr>)" by default
  1292   have minus_cong: "\<And>A B A' B'::ereal. A = A' \<Longrightarrow> B = B' \<Longrightarrow> real A - real B = real A' - real B'" by simp
  1293   have f': "(\<lambda>x. - f x) \<in> borel_measurable M" using f by auto
  1294   have Nf: "f \<in> borel_measurable (M\<lparr>measure := \<nu>\<rparr>)" using f by simp
  1295   { fix f :: "'a \<Rightarrow> real"
  1296     { fix x assume *: "RN_deriv M \<nu> x \<noteq> \<infinity>"
  1297       have "ereal (real (RN_deriv M \<nu> x)) * ereal (f x) = ereal (real (RN_deriv M \<nu> x) * f x)"
  1298         by (simp add: mult_le_0_iff)
  1299       then have "RN_deriv M \<nu> x * ereal (f x) = ereal (real (RN_deriv M \<nu> x) * f x)"
  1300         using RN_deriv(3)[OF ms \<nu>(2)] * by (auto simp add: ereal_real split: split_if_asm) }
  1301     then have "(\<integral>\<^isup>+x. ereal (real (RN_deriv M \<nu> x) * f x) \<partial>M) = (\<integral>\<^isup>+x. RN_deriv M \<nu> x * ereal (f x) \<partial>M)"
  1302               "(\<integral>\<^isup>+x. ereal (- (real (RN_deriv M \<nu> x) * f x)) \<partial>M) = (\<integral>\<^isup>+x. RN_deriv M \<nu> x * ereal (- f x) \<partial>M)"
  1303       using RN_deriv_finite[OF \<nu>] unfolding ereal_mult_minus_right uminus_ereal.simps(1)[symmetric]
  1304       by (auto intro!: positive_integral_cong_AE) }
  1305   note * = this
  1306   show ?integral ?integrable
  1307     unfolding lebesgue_integral_def integrable_def *
  1308     using f RN_deriv(1)[OF ms \<nu>(2)]
  1309     by (auto simp: RN_deriv_positive_integral[OF ms \<nu>(2)])
  1310 qed
  1312 lemma (in sigma_finite_measure) real_RN_deriv:
  1313   assumes "finite_measure (M\<lparr>measure := \<nu>\<rparr>)" (is "finite_measure ?\<nu>")
  1314   assumes ac: "absolutely_continuous \<nu>"
  1315   obtains D where "D \<in> borel_measurable M"
  1316     and "AE x. RN_deriv M \<nu> x = ereal (D x)"
  1317     and "AE x in M\<lparr>measure := \<nu>\<rparr>. 0 < D x"
  1318     and "\<And>x. 0 \<le> D x"
  1319 proof
  1320   interpret \<nu>: finite_measure ?\<nu> by fact
  1321   have ms: "measure_space ?\<nu>" by default
  1322   note RN = RN_deriv[OF ms ac]
  1324   let ?RN = "\<lambda>t. {x \<in> space M. RN_deriv M \<nu> x = t}"
  1326   show "(\<lambda>x. real (RN_deriv M \<nu> x)) \<in> borel_measurable M"
  1327     using RN by auto
  1329   have "\<nu> (?RN \<infinity>) = (\<integral>\<^isup>+ x. RN_deriv M \<nu> x * indicator (?RN \<infinity>) x \<partial>M)"
  1330     using RN by auto
  1331   also have "\<dots> = (\<integral>\<^isup>+ x. \<infinity> * indicator (?RN \<infinity>) x \<partial>M)"
  1332     by (intro positive_integral_cong) (auto simp: indicator_def)
  1333   also have "\<dots> = \<infinity> * \<mu> (?RN \<infinity>)"
  1334     using RN by (intro positive_integral_cmult_indicator) auto
  1335   finally have eq: "\<nu> (?RN \<infinity>) = \<infinity> * \<mu> (?RN \<infinity>)" .
  1336   moreover
  1337   have "\<mu> (?RN \<infinity>) = 0"
  1338   proof (rule ccontr)
  1339     assume "\<mu> {x \<in> space M. RN_deriv M \<nu> x = \<infinity>} \<noteq> 0"
  1340     moreover from RN have "0 \<le> \<mu> {x \<in> space M. RN_deriv M \<nu> x = \<infinity>}" by auto
  1341     ultimately have "0 < \<mu> {x \<in> space M. RN_deriv M \<nu> x = \<infinity>}" by auto
  1342     with eq have "\<nu> (?RN \<infinity>) = \<infinity>" by simp
  1343     with \<nu>.finite_measure[of "?RN \<infinity>"] RN show False by auto
  1344   qed
  1345   ultimately have "AE x. RN_deriv M \<nu> x < \<infinity>"
  1346     using RN by (intro AE_iff_measurable[THEN iffD2]) auto
  1347   then show "AE x. RN_deriv M \<nu> x = ereal (real (RN_deriv M \<nu> x))"
  1348     using RN(3) by (auto simp: ereal_real)
  1349   then have eq: "AE x in (M\<lparr>measure := \<nu>\<rparr>) . RN_deriv M \<nu> x = ereal (real (RN_deriv M \<nu> x))"
  1350     using ac absolutely_continuous_AE[OF ms] by auto
  1352   show "\<And>x. 0 \<le> real (RN_deriv M \<nu> x)"
  1353     using RN by (auto intro: real_of_ereal_pos)
  1355   have "\<nu> (?RN 0) = (\<integral>\<^isup>+ x. RN_deriv M \<nu> x * indicator (?RN 0) x \<partial>M)"
  1356     using RN by auto
  1357   also have "\<dots> = (\<integral>\<^isup>+ x. 0 \<partial>M)"
  1358     by (intro positive_integral_cong) (auto simp: indicator_def)
  1359   finally have "AE x in (M\<lparr>measure := \<nu>\<rparr>). RN_deriv M \<nu> x \<noteq> 0"
  1360     using RN by (intro \<nu>.AE_iff_measurable[THEN iffD2]) auto
  1361   with RN(3) eq show "AE x in (M\<lparr>measure := \<nu>\<rparr>). 0 < real (RN_deriv M \<nu> x)"
  1362     by (auto simp: zero_less_real_of_ereal le_less)
  1363 qed
  1365 lemma (in sigma_finite_measure) RN_deriv_singleton:
  1366   assumes "measure_space (M\<lparr>measure := \<nu>\<rparr>)"
  1367   and ac: "absolutely_continuous \<nu>"
  1368   and "{x} \<in> sets M"
  1369   shows "\<nu> {x} = RN_deriv M \<nu> x * \<mu> {x}"
  1370 proof -
  1371   note deriv = RN_deriv[OF assms(1, 2)]
  1372   from deriv(2)[OF `{x} \<in> sets M`]
  1373   have "\<nu> {x} = (\<integral>\<^isup>+w. RN_deriv M \<nu> x * indicator {x} w \<partial>M)"
  1374     by (auto simp: indicator_def intro!: positive_integral_cong)
  1375   thus ?thesis using positive_integral_cmult_indicator[OF _ `{x} \<in> sets M`] deriv(3)
  1376     by auto
  1377 qed
  1379 theorem (in finite_measure_space) RN_deriv_finite_measure:
  1380   assumes "measure_space (M\<lparr>measure := \<nu>\<rparr>)"
  1381   and ac: "absolutely_continuous \<nu>"
  1382   and "x \<in> space M"
  1383   shows "\<nu> {x} = RN_deriv M \<nu> x * \<mu> {x}"
  1384 proof -
  1385   have "{x} \<in> sets M" using sets_eq_Pow `x \<in> space M` by auto
  1386   from RN_deriv_singleton[OF assms(1,2) this] show ?thesis .
  1387 qed
  1389 end