src/HOL/Probability/Lebesgue_Integration.thy
changeset 43920 cedb5cb948fd
parent 43339 9ba256ad6781
child 43941 481566bc20e4
equal deleted inserted replaced
43919:a7e4fb1a0502 43920:cedb5cb948fd
     7 
     7 
     8 theory Lebesgue_Integration
     8 theory Lebesgue_Integration
     9 imports Measure Borel_Space
     9 imports Measure Borel_Space
    10 begin
    10 begin
    11 
    11 
    12 lemma real_extreal_1[simp]: "real (1::extreal) = 1"
    12 lemma real_ereal_1[simp]: "real (1::ereal) = 1"
    13   unfolding one_extreal_def by simp
    13   unfolding one_ereal_def by simp
    14 
    14 
    15 lemma extreal_indicator_pos[simp,intro]: "0 \<le> (indicator A x ::extreal)"
    15 lemma ereal_indicator_pos[simp,intro]: "0 \<le> (indicator A x ::ereal)"
    16   unfolding indicator_def by auto
    16   unfolding indicator_def by auto
    17 
    17 
    18 lemma tendsto_real_max:
    18 lemma tendsto_real_max:
    19   fixes x y :: real
    19   fixes x y :: real
    20   assumes "(X ---> x) net"
    20   assumes "(X ---> x) net"
   148     by auto
   148     by auto
   149   then show ?thesis using assms[THEN simple_functionD(2)] by auto
   149   then show ?thesis using assms[THEN simple_functionD(2)] by auto
   150 qed
   150 qed
   151 
   151 
   152 lemma (in sigma_algebra) simple_function_indicator_representation:
   152 lemma (in sigma_algebra) simple_function_indicator_representation:
   153   fixes f ::"'a \<Rightarrow> extreal"
   153   fixes f ::"'a \<Rightarrow> ereal"
   154   assumes f: "simple_function M f" and x: "x \<in> space M"
   154   assumes f: "simple_function M f" and x: "x \<in> space M"
   155   shows "f x = (\<Sum>y \<in> f ` space M. y * indicator (f -` {y} \<inter> space M) x)"
   155   shows "f x = (\<Sum>y \<in> f ` space M. y * indicator (f -` {y} \<inter> space M) x)"
   156   (is "?l = ?r")
   156   (is "?l = ?r")
   157 proof -
   157 proof -
   158   have "?r = (\<Sum>y \<in> f ` space M.
   158   have "?r = (\<Sum>y \<in> f ` space M.
   163   also have "... = f x" using x by (auto simp: indicator_def)
   163   also have "... = f x" using x by (auto simp: indicator_def)
   164   finally show ?thesis by auto
   164   finally show ?thesis by auto
   165 qed
   165 qed
   166 
   166 
   167 lemma (in measure_space) simple_function_notspace:
   167 lemma (in measure_space) simple_function_notspace:
   168   "simple_function M (\<lambda>x. h x * indicator (- space M) x::extreal)" (is "simple_function M ?h")
   168   "simple_function M (\<lambda>x. h x * indicator (- space M) x::ereal)" (is "simple_function M ?h")
   169 proof -
   169 proof -
   170   have "?h ` space M \<subseteq> {0}" unfolding indicator_def by auto
   170   have "?h ` space M \<subseteq> {0}" unfolding indicator_def by auto
   171   hence [simp, intro]: "finite (?h ` space M)" by (auto intro: finite_subset)
   171   hence [simp, intro]: "finite (?h ` space M)" by (auto intro: finite_subset)
   172   have "?h -` {0} \<inter> space M = space M" by auto
   172   have "?h -` {0} \<inter> space M = space M" by auto
   173   thus ?thesis unfolding simple_function_def by auto
   173   thus ?thesis unfolding simple_function_def by auto
   210   shows "simple_function M f"
   210   shows "simple_function M f"
   211   using assms unfolding simple_function_def
   211   using assms unfolding simple_function_def
   212   by (auto intro: borel_measurable_vimage)
   212   by (auto intro: borel_measurable_vimage)
   213 
   213 
   214 lemma (in sigma_algebra) simple_function_eq_borel_measurable:
   214 lemma (in sigma_algebra) simple_function_eq_borel_measurable:
   215   fixes f :: "'a \<Rightarrow> extreal"
   215   fixes f :: "'a \<Rightarrow> ereal"
   216   shows "simple_function M f \<longleftrightarrow> finite (f`space M) \<and> f \<in> borel_measurable M"
   216   shows "simple_function M f \<longleftrightarrow> finite (f`space M) \<and> f \<in> borel_measurable M"
   217   using simple_function_borel_measurable[of f]
   217   using simple_function_borel_measurable[of f]
   218     borel_measurable_simple_function[of f]
   218     borel_measurable_simple_function[of f]
   219   by (fastsimp simp: simple_function_def)
   219   by (fastsimp simp: simple_function_def)
   220 
   220 
   298   assume "finite P" from this assms show ?thesis by induct auto
   298   assume "finite P" from this assms show ?thesis by induct auto
   299 qed auto
   299 qed auto
   300 
   300 
   301 lemma (in sigma_algebra)
   301 lemma (in sigma_algebra)
   302   fixes f g :: "'a \<Rightarrow> real" assumes sf: "simple_function M f"
   302   fixes f g :: "'a \<Rightarrow> real" assumes sf: "simple_function M f"
   303   shows simple_function_extreal[intro, simp]: "simple_function M (\<lambda>x. extreal (f x))"
   303   shows simple_function_ereal[intro, simp]: "simple_function M (\<lambda>x. ereal (f x))"
   304   by (auto intro!: simple_function_compose1[OF sf])
   304   by (auto intro!: simple_function_compose1[OF sf])
   305 
   305 
   306 lemma (in sigma_algebra)
   306 lemma (in sigma_algebra)
   307   fixes f g :: "'a \<Rightarrow> nat" assumes sf: "simple_function M f"
   307   fixes f g :: "'a \<Rightarrow> nat" assumes sf: "simple_function M f"
   308   shows simple_function_real_of_nat[intro, simp]: "simple_function M (\<lambda>x. real (f x))"
   308   shows simple_function_real_of_nat[intro, simp]: "simple_function M (\<lambda>x. real (f x))"
   309   by (auto intro!: simple_function_compose1[OF sf])
   309   by (auto intro!: simple_function_compose1[OF sf])
   310 
   310 
   311 lemma (in sigma_algebra) borel_measurable_implies_simple_function_sequence:
   311 lemma (in sigma_algebra) borel_measurable_implies_simple_function_sequence:
   312   fixes u :: "'a \<Rightarrow> extreal"
   312   fixes u :: "'a \<Rightarrow> ereal"
   313   assumes u: "u \<in> borel_measurable M"
   313   assumes u: "u \<in> borel_measurable M"
   314   shows "\<exists>f. incseq f \<and> (\<forall>i. \<infinity> \<notin> range (f i) \<and> simple_function M (f i)) \<and>
   314   shows "\<exists>f. incseq f \<and> (\<forall>i. \<infinity> \<notin> range (f i) \<and> simple_function M (f i)) \<and>
   315              (\<forall>x. (SUP i. f i x) = max 0 (u x)) \<and> (\<forall>i x. 0 \<le> f i x)"
   315              (\<forall>x. (SUP i. f i x) = max 0 (u x)) \<and> (\<forall>i x. 0 \<le> f i x)"
   316 proof -
   316 proof -
   317   def f \<equiv> "\<lambda>x i. if real i \<le> u x then i * 2 ^ i else natfloor (real (u x) * 2 ^ i)"
   317   def f \<equiv> "\<lambda>x i. if real i \<le> u x then i * 2 ^ i else natfloor (real (u x) * 2 ^ i)"
   329 
   329 
   330   have real_f:
   330   have real_f:
   331     "\<And>i x. real (f x i) = (if real i \<le> u x then i * 2 ^ i else real (natfloor (real (u x) * 2 ^ i)))"
   331     "\<And>i x. real (f x i) = (if real i \<le> u x then i * 2 ^ i else real (natfloor (real (u x) * 2 ^ i)))"
   332     unfolding f_def by auto
   332     unfolding f_def by auto
   333 
   333 
   334   let "?g j x" = "real (f x j) / 2^j :: extreal"
   334   let "?g j x" = "real (f x j) / 2^j :: ereal"
   335   show ?thesis
   335   show ?thesis
   336   proof (intro exI[of _ ?g] conjI allI ballI)
   336   proof (intro exI[of _ ?g] conjI allI ballI)
   337     fix i
   337     fix i
   338     have "simple_function M (\<lambda>x. real (f x i))"
   338     have "simple_function M (\<lambda>x. real (f x i))"
   339     proof (intro simple_function_borel_measurable)
   339     proof (intro simple_function_borel_measurable)
   343         using f_upper[of _ i] by auto
   343         using f_upper[of _ i] by auto
   344       then show "finite ((\<lambda>x. real (f x i))`space M)"
   344       then show "finite ((\<lambda>x. real (f x i))`space M)"
   345         by (rule finite_subset) auto
   345         by (rule finite_subset) auto
   346     qed
   346     qed
   347     then show "simple_function M (?g i)"
   347     then show "simple_function M (?g i)"
   348       by (auto intro: simple_function_extreal simple_function_div)
   348       by (auto intro: simple_function_ereal simple_function_div)
   349   next
   349   next
   350     show "incseq ?g"
   350     show "incseq ?g"
   351     proof (intro incseq_extreal incseq_SucI le_funI)
   351     proof (intro incseq_ereal incseq_SucI le_funI)
   352       fix x and i :: nat
   352       fix x and i :: nat
   353       have "f x i * 2 \<le> f x (Suc i)" unfolding f_def
   353       have "f x i * 2 \<le> f x (Suc i)" unfolding f_def
   354       proof ((split split_if)+, intro conjI impI)
   354       proof ((split split_if)+, intro conjI impI)
   355         assume "extreal (real i) \<le> u x" "\<not> extreal (real (Suc i)) \<le> u x"
   355         assume "ereal (real i) \<le> u x" "\<not> ereal (real (Suc i)) \<le> u x"
   356         then show "i * 2 ^ i * 2 \<le> natfloor (real (u x) * 2 ^ Suc i)"
   356         then show "i * 2 ^ i * 2 \<le> natfloor (real (u x) * 2 ^ Suc i)"
   357           by (cases "u x") (auto intro!: le_natfloor)
   357           by (cases "u x") (auto intro!: le_natfloor)
   358       next
   358       next
   359         assume "\<not> extreal (real i) \<le> u x" "extreal (real (Suc i)) \<le> u x"
   359         assume "\<not> ereal (real i) \<le> u x" "ereal (real (Suc i)) \<le> u x"
   360         then show "natfloor (real (u x) * 2 ^ i) * 2 \<le> Suc i * 2 ^ Suc i"
   360         then show "natfloor (real (u x) * 2 ^ i) * 2 \<le> Suc i * 2 ^ Suc i"
   361           by (cases "u x") auto
   361           by (cases "u x") auto
   362       next
   362       next
   363         assume "\<not> extreal (real i) \<le> u x" "\<not> extreal (real (Suc i)) \<le> u x"
   363         assume "\<not> ereal (real i) \<le> u x" "\<not> ereal (real (Suc i)) \<le> u x"
   364         have "natfloor (real (u x) * 2 ^ i) * 2 = natfloor (real (u x) * 2 ^ i) * natfloor 2"
   364         have "natfloor (real (u x) * 2 ^ i) * 2 = natfloor (real (u x) * 2 ^ i) * natfloor 2"
   365           by simp
   365           by simp
   366         also have "\<dots> \<le> natfloor (real (u x) * 2 ^ i * 2)"
   366         also have "\<dots> \<le> natfloor (real (u x) * 2 ^ i * 2)"
   367         proof cases
   367         proof cases
   368           assume "0 \<le> u x" then show ?thesis
   368           assume "0 \<le> u x" then show ?thesis
   378       then show "?g i x \<le> ?g (Suc i) x"
   378       then show "?g i x \<le> ?g (Suc i) x"
   379         by (auto simp: field_simps)
   379         by (auto simp: field_simps)
   380     qed
   380     qed
   381   next
   381   next
   382     fix x show "(SUP i. ?g i x) = max 0 (u x)"
   382     fix x show "(SUP i. ?g i x) = max 0 (u x)"
   383     proof (rule extreal_SUPI)
   383     proof (rule ereal_SUPI)
   384       fix i show "?g i x \<le> max 0 (u x)" unfolding max_def f_def
   384       fix i show "?g i x \<le> max 0 (u x)" unfolding max_def f_def
   385         by (cases "u x") (auto simp: field_simps real_natfloor_le natfloor_neg
   385         by (cases "u x") (auto simp: field_simps real_natfloor_le natfloor_neg
   386                                      mult_nonpos_nonneg mult_nonneg_nonneg)
   386                                      mult_nonpos_nonneg mult_nonneg_nonneg)
   387     next
   387     next
   388       fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> ?g i x \<le> y"
   388       fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> ?g i x \<le> y"
   391       show "max 0 (u x) \<le> y"
   391       show "max 0 (u x) \<le> y"
   392       proof (cases y)
   392       proof (cases y)
   393         case (real r)
   393         case (real r)
   394         with * have *: "\<And>i. f x i \<le> r * 2^i" by (auto simp: divide_le_eq)
   394         with * have *: "\<And>i. f x i \<le> r * 2^i" by (auto simp: divide_le_eq)
   395         from real_arch_lt[of r] * have "u x \<noteq> \<infinity>" by (auto simp: f_def) (metis less_le_not_le)
   395         from real_arch_lt[of r] * have "u x \<noteq> \<infinity>" by (auto simp: f_def) (metis less_le_not_le)
   396         then have "\<exists>p. max 0 (u x) = extreal p \<and> 0 \<le> p" by (cases "u x") (auto simp: max_def)
   396         then have "\<exists>p. max 0 (u x) = ereal p \<and> 0 \<le> p" by (cases "u x") (auto simp: max_def)
   397         then guess p .. note ux = this
   397         then guess p .. note ux = this
   398         obtain m :: nat where m: "p < real m" using real_arch_lt ..
   398         obtain m :: nat where m: "p < real m" using real_arch_lt ..
   399         have "p \<le> r"
   399         have "p \<le> r"
   400         proof (rule ccontr)
   400         proof (rule ccontr)
   401           assume "\<not> p \<le> r"
   401           assume "\<not> p \<le> r"
   415     qed
   415     qed
   416   qed (auto simp: divide_nonneg_pos)
   416   qed (auto simp: divide_nonneg_pos)
   417 qed
   417 qed
   418 
   418 
   419 lemma (in sigma_algebra) borel_measurable_implies_simple_function_sequence':
   419 lemma (in sigma_algebra) borel_measurable_implies_simple_function_sequence':
   420   fixes u :: "'a \<Rightarrow> extreal"
   420   fixes u :: "'a \<Rightarrow> ereal"
   421   assumes u: "u \<in> borel_measurable M"
   421   assumes u: "u \<in> borel_measurable M"
   422   obtains f where "\<And>i. simple_function M (f i)" "incseq f" "\<And>i. \<infinity> \<notin> range (f i)"
   422   obtains f where "\<And>i. simple_function M (f i)" "incseq f" "\<And>i. \<infinity> \<notin> range (f i)"
   423     "\<And>x. (SUP i. f i x) = max 0 (u x)" "\<And>i x. 0 \<le> f i x"
   423     "\<And>x. (SUP i. f i x) = max 0 (u x)" "\<And>i x. 0 \<le> f i x"
   424   using borel_measurable_implies_simple_function_sequence[OF u] by auto
   424   using borel_measurable_implies_simple_function_sequence[OF u] by auto
   425 
   425 
   452   have "{x\<in>space M. P x} = {x. P x} \<inter> space M" by auto
   452   have "{x\<in>space M. P x} = {x. P x} \<inter> space M" by auto
   453   with simple_function_If_set[OF sf, of "{x. P x}"] P show ?thesis by simp
   453   with simple_function_If_set[OF sf, of "{x. P x}"] P show ?thesis by simp
   454 qed
   454 qed
   455 
   455 
   456 lemma (in measure_space) simple_function_restricted:
   456 lemma (in measure_space) simple_function_restricted:
   457   fixes f :: "'a \<Rightarrow> extreal" assumes "A \<in> sets M"
   457   fixes f :: "'a \<Rightarrow> ereal" assumes "A \<in> sets M"
   458   shows "simple_function (restricted_space A) f \<longleftrightarrow> simple_function M (\<lambda>x. f x * indicator A x)"
   458   shows "simple_function (restricted_space A) f \<longleftrightarrow> simple_function M (\<lambda>x. f x * indicator A x)"
   459     (is "simple_function ?R f \<longleftrightarrow> simple_function M ?f")
   459     (is "simple_function ?R f \<longleftrightarrow> simple_function M ?f")
   460 proof -
   460 proof -
   461   interpret R: sigma_algebra ?R by (rule restricted_sigma_algebra[OF `A \<in> sets M`])
   461   interpret R: sigma_algebra ?R by (rule restricted_sigma_algebra[OF `A \<in> sets M`])
   462   have f: "finite (f`A) \<longleftrightarrow> finite (?f`space M)"
   462   have f: "finite (f`A) \<longleftrightarrow> finite (?f`space M)"
   476       fix x assume "x \<in> A"
   476       fix x assume "x \<in> A"
   477       then show "\<exists>y\<in>space M. f x = f y * indicator A y"
   477       then show "\<exists>y\<in>space M. f x = f y * indicator A y"
   478         using `A \<in> sets M` sets_into_space by (auto intro!: bexI[of _ x])
   478         using `A \<in> sets M` sets_into_space by (auto intro!: bexI[of _ x])
   479     next
   479     next
   480       fix x
   480       fix x
   481       assume "indicator A x \<noteq> (0::extreal)"
   481       assume "indicator A x \<noteq> (0::ereal)"
   482       then have "x \<in> A" by (auto simp: indicator_def split: split_if_asm)
   482       then have "x \<in> A" by (auto simp: indicator_def split: split_if_asm)
   483       moreover assume "x \<in> space M" "\<forall>y\<in>A. ?f x \<noteq> f y"
   483       moreover assume "x \<in> space M" "\<forall>y\<in>A. ?f x \<noteq> f y"
   484       ultimately show "f x = 0" by auto
   484       ultimately show "f x = 0" by auto
   485     qed
   485     qed
   486     then show ?thesis by auto
   486     then show ?thesis by auto
   525 
   525 
   526 definition simple_integral_def:
   526 definition simple_integral_def:
   527   "integral\<^isup>S M f = (\<Sum>x \<in> f ` space M. x * measure M (f -` {x} \<inter> space M))"
   527   "integral\<^isup>S M f = (\<Sum>x \<in> f ` space M. x * measure M (f -` {x} \<inter> space M))"
   528 
   528 
   529 syntax
   529 syntax
   530   "_simple_integral" :: "'a \<Rightarrow> ('a \<Rightarrow> extreal) \<Rightarrow> ('a, 'b) measure_space_scheme \<Rightarrow> extreal" ("\<integral>\<^isup>S _. _ \<partial>_" [60,61] 110)
   530   "_simple_integral" :: "'a \<Rightarrow> ('a \<Rightarrow> ereal) \<Rightarrow> ('a, 'b) measure_space_scheme \<Rightarrow> ereal" ("\<integral>\<^isup>S _. _ \<partial>_" [60,61] 110)
   531 
   531 
   532 translations
   532 translations
   533   "\<integral>\<^isup>S x. f \<partial>M" == "CONST integral\<^isup>S M (%x. f)"
   533   "\<integral>\<^isup>S x. f \<partial>M" == "CONST integral\<^isup>S M (%x. f)"
   534 
   534 
   535 lemma (in measure_space) simple_integral_cong:
   535 lemma (in measure_space) simple_integral_cong:
   589     with sets have "\<mu> (f -` {f x} \<inter> space M) = setsum \<mu> (?sub (f x))"
   589     with sets have "\<mu> (f -` {f x} \<inter> space M) = setsum \<mu> (?sub (f x))"
   590       by (subst measure_Union) auto }
   590       by (subst measure_Union) auto }
   591   hence "integral\<^isup>S M f = (\<Sum>(x,A)\<in>?SIGMA. x * \<mu> A)"
   591   hence "integral\<^isup>S M f = (\<Sum>(x,A)\<in>?SIGMA. x * \<mu> A)"
   592     unfolding simple_integral_def using f sets
   592     unfolding simple_integral_def using f sets
   593     by (subst setsum_Sigma[symmetric])
   593     by (subst setsum_Sigma[symmetric])
   594        (auto intro!: setsum_cong setsum_extreal_right_distrib)
   594        (auto intro!: setsum_cong setsum_ereal_right_distrib)
   595   also have "\<dots> = (\<Sum>A\<in>?p ` space M. the_elem (f`A) * \<mu> A)"
   595   also have "\<dots> = (\<Sum>A\<in>?p ` space M. the_elem (f`A) * \<mu> A)"
   596   proof -
   596   proof -
   597     have [simp]: "\<And>x. x \<in> space M \<Longrightarrow> f ` ?p x = {f x}" by (auto intro!: imageI)
   597     have [simp]: "\<And>x. x \<in> space M \<Longrightarrow> f ` ?p x = {f x}" by (auto intro!: imageI)
   598     have "(\<lambda>A. (the_elem (f ` A), A)) ` ?p ` space M
   598     have "(\<lambda>A. (the_elem (f ` A), A)) ` ?p ` space M
   599       = (\<lambda>x. (f x, ?p x)) ` space M"
   599       = (\<lambda>x. (f x, ?p x)) ` space M"
   623     unfolding
   623     unfolding
   624       simple_function_partition[OF simple_function_add[OF f g] simple_function_Pair[OF f g]]
   624       simple_function_partition[OF simple_function_add[OF f g] simple_function_Pair[OF f g]]
   625       simple_function_partition[OF f g]
   625       simple_function_partition[OF f g]
   626       simple_function_partition[OF g f]
   626       simple_function_partition[OF g f]
   627     by (subst (3) Int_commute)
   627     by (subst (3) Int_commute)
   628        (auto simp add: extreal_left_distrib setsum_addf[symmetric] intro!: setsum_cong)
   628        (auto simp add: ereal_left_distrib setsum_addf[symmetric] intro!: setsum_cong)
   629 qed
   629 qed
   630 
   630 
   631 lemma (in measure_space) simple_integral_setsum[simp]:
   631 lemma (in measure_space) simple_integral_setsum[simp]:
   632   assumes "\<And>i x. i \<in> P \<Longrightarrow> 0 \<le> f i x"
   632   assumes "\<And>i x. i \<in> P \<Longrightarrow> 0 \<le> f i x"
   633   assumes "\<And>i. i \<in> P \<Longrightarrow> simple_function M (f i)"
   633   assumes "\<And>i. i \<in> P \<Longrightarrow> simple_function M (f i)"
   648     hence "(\<lambda>x. c * f x) ` ?S = {c * f x}" "f ` ?S = {f x}"
   648     hence "(\<lambda>x. c * f x) ` ?S = {c * f x}" "f ` ?S = {f x}"
   649       by auto }
   649       by auto }
   650   with assms show ?thesis
   650   with assms show ?thesis
   651     unfolding simple_function_partition[OF mult f(1)]
   651     unfolding simple_function_partition[OF mult f(1)]
   652               simple_function_partition[OF f(1) mult]
   652               simple_function_partition[OF f(1) mult]
   653     by (subst setsum_extreal_right_distrib)
   653     by (subst setsum_ereal_right_distrib)
   654        (auto intro!: extreal_0_le_mult setsum_cong simp: mult_assoc)
   654        (auto intro!: ereal_0_le_mult setsum_cong simp: mult_assoc)
   655 qed
   655 qed
   656 
   656 
   657 lemma (in measure_space) simple_integral_mono_AE:
   657 lemma (in measure_space) simple_integral_mono_AE:
   658   assumes f: "simple_function M f" and g: "simple_function M g"
   658   assumes f: "simple_function M f" and g: "simple_function M g"
   659   and mono: "AE x. f x \<le> g x"
   659   and mono: "AE x. f x \<le> g x"
   671     then have *: "f ` ?S x = {f x}" "g ` ?S x = {g x}" by auto
   671     then have *: "f ` ?S x = {f x}" "g ` ?S x = {g x}" by auto
   672     show "the_elem (f`?S x) * \<mu> (?S x) \<le> the_elem (g`?S x) * \<mu> (?S x)"
   672     show "the_elem (f`?S x) * \<mu> (?S x) \<le> the_elem (g`?S x) * \<mu> (?S x)"
   673     proof (cases "f x \<le> g x")
   673     proof (cases "f x \<le> g x")
   674       case True then show ?thesis
   674       case True then show ?thesis
   675         using * assms(1,2)[THEN simple_functionD(2)]
   675         using * assms(1,2)[THEN simple_functionD(2)]
   676         by (auto intro!: extreal_mult_right_mono)
   676         by (auto intro!: ereal_mult_right_mono)
   677     next
   677     next
   678       case False
   678       case False
   679       obtain N where N: "{x\<in>space M. \<not> f x \<le> g x} \<subseteq> N" "N \<in> sets M" "\<mu> N = 0"
   679       obtain N where N: "{x\<in>space M. \<not> f x \<le> g x} \<subseteq> N" "N \<in> sets M" "\<mu> N = 0"
   680         using mono by (auto elim!: AE_E)
   680         using mono by (auto elim!: AE_E)
   681       have "?S x \<subseteq> N" using N `x \<in> space M` False by auto
   681       have "?S x \<subseteq> N" using N `x \<in> space M` False by auto
   765   shows "integral\<^isup>S M (indicator A) = \<mu> A"
   765   shows "integral\<^isup>S M (indicator A) = \<mu> A"
   766 proof cases
   766 proof cases
   767   assume "space M = {}" hence "A = {}" using sets_into_space[OF assms] by auto
   767   assume "space M = {}" hence "A = {}" using sets_into_space[OF assms] by auto
   768   thus ?thesis unfolding simple_integral_def using `space M = {}` by auto
   768   thus ?thesis unfolding simple_integral_def using `space M = {}` by auto
   769 next
   769 next
   770   assume "space M \<noteq> {}" hence "(\<lambda>x. 1) ` space M = {1::extreal}" by auto
   770   assume "space M \<noteq> {}" hence "(\<lambda>x. 1) ` space M = {1::ereal}" by auto
   771   thus ?thesis
   771   thus ?thesis
   772     using simple_integral_indicator[OF assms simple_function_const[of 1]]
   772     using simple_integral_indicator[OF assms simple_function_const[of 1]]
   773     using sets_into_space[OF assms]
   773     using sets_into_space[OF assms]
   774     by (auto intro!: arg_cong[where f="\<mu>"])
   774     by (auto intro!: arg_cong[where f="\<mu>"])
   775 qed
   775 qed
   776 
   776 
   777 lemma (in measure_space) simple_integral_null_set:
   777 lemma (in measure_space) simple_integral_null_set:
   778   assumes "simple_function M u" "\<And>x. 0 \<le> u x" and "N \<in> null_sets"
   778   assumes "simple_function M u" "\<And>x. 0 \<le> u x" and "N \<in> null_sets"
   779   shows "(\<integral>\<^isup>Sx. u x * indicator N x \<partial>M) = 0"
   779   shows "(\<integral>\<^isup>Sx. u x * indicator N x \<partial>M) = 0"
   780 proof -
   780 proof -
   781   have "AE x. indicator N x = (0 :: extreal)"
   781   have "AE x. indicator N x = (0 :: ereal)"
   782     using `N \<in> null_sets` by (auto simp: indicator_def intro!: AE_I[of _ N])
   782     using `N \<in> null_sets` by (auto simp: indicator_def intro!: AE_I[of _ N])
   783   then have "(\<integral>\<^isup>Sx. u x * indicator N x \<partial>M) = (\<integral>\<^isup>Sx. 0 \<partial>M)"
   783   then have "(\<integral>\<^isup>Sx. u x * indicator N x \<partial>M) = (\<integral>\<^isup>Sx. 0 \<partial>M)"
   784     using assms apply (intro simple_integral_cong_AE) by auto
   784     using assms apply (intro simple_integral_cong_AE) by auto
   785   then show ?thesis by simp
   785   then show ?thesis by simp
   786 qed
   786 qed
   813     f -` {f x} \<inter> A = ?f -` {f x} \<inter> space M"
   813     f -` {f x} \<inter> A = ?f -` {f x} \<inter> space M"
   814     using `A \<in> sets M` sets_into_space
   814     using `A \<in> sets M` sets_into_space
   815     by (auto simp: indicator_def split: split_if_asm)
   815     by (auto simp: indicator_def split: split_if_asm)
   816   then show "f x * \<mu> (f -` {f x} \<inter> A) =
   816   then show "f x * \<mu> (f -` {f x} \<inter> A) =
   817     f x * \<mu> (?f -` {f x} \<inter> space M)"
   817     f x * \<mu> (?f -` {f x} \<inter> space M)"
   818     unfolding extreal_mult_cancel_left by auto
   818     unfolding ereal_mult_cancel_left by auto
   819 qed
   819 qed
   820 
   820 
   821 lemma (in measure_space) simple_integral_subalgebra:
   821 lemma (in measure_space) simple_integral_subalgebra:
   822   assumes N: "measure_space N" and [simp]: "space N = space M" "measure N = measure M"
   822   assumes N: "measure_space N" and [simp]: "space N = space M" "measure N = measure M"
   823   shows "integral\<^isup>S N = integral\<^isup>S M"
   823   shows "integral\<^isup>S N = integral\<^isup>S M"
   870 
   870 
   871 definition positive_integral_def:
   871 definition positive_integral_def:
   872   "integral\<^isup>P M f = (SUP g : {g. simple_function M g \<and> g \<le> max 0 \<circ> f}. integral\<^isup>S M g)"
   872   "integral\<^isup>P M f = (SUP g : {g. simple_function M g \<and> g \<le> max 0 \<circ> f}. integral\<^isup>S M g)"
   873 
   873 
   874 syntax
   874 syntax
   875   "_positive_integral" :: "'a \<Rightarrow> ('a \<Rightarrow> extreal) \<Rightarrow> ('a, 'b) measure_space_scheme \<Rightarrow> extreal" ("\<integral>\<^isup>+ _. _ \<partial>_" [60,61] 110)
   875   "_positive_integral" :: "'a \<Rightarrow> ('a \<Rightarrow> ereal) \<Rightarrow> ('a, 'b) measure_space_scheme \<Rightarrow> ereal" ("\<integral>\<^isup>+ _. _ \<partial>_" [60,61] 110)
   876 
   876 
   877 translations
   877 translations
   878   "\<integral>\<^isup>+ x. f \<partial>M" == "CONST integral\<^isup>P M (%x. f)"
   878   "\<integral>\<^isup>+ x. f \<partial>M" == "CONST integral\<^isup>P M (%x. f)"
   879 
   879 
   880 lemma (in measure_space) positive_integral_cong_measure:
   880 lemma (in measure_space) positive_integral_cong_measure:
   915   next
   915   next
   916     assume \<mu>G: "\<mu> ?G \<noteq> 0"
   916     assume \<mu>G: "\<mu> ?G \<noteq> 0"
   917     have "SUPR ?A (integral\<^isup>S M) = \<infinity>"
   917     have "SUPR ?A (integral\<^isup>S M) = \<infinity>"
   918     proof (intro SUP_PInfty)
   918     proof (intro SUP_PInfty)
   919       fix n :: nat
   919       fix n :: nat
   920       let ?y = "extreal (real n) / (if \<mu> ?G = \<infinity> then 1 else \<mu> ?G)"
   920       let ?y = "ereal (real n) / (if \<mu> ?G = \<infinity> then 1 else \<mu> ?G)"
   921       have "0 \<le> ?y" "?y \<noteq> \<infinity>" using \<mu>G \<mu>G_pos by (auto simp: extreal_divide_eq)
   921       have "0 \<le> ?y" "?y \<noteq> \<infinity>" using \<mu>G \<mu>G_pos by (auto simp: ereal_divide_eq)
   922       then have "?g ?y \<in> ?A" by (rule g_in_A)
   922       then have "?g ?y \<in> ?A" by (rule g_in_A)
   923       have "real n \<le> ?y * \<mu> ?G"
   923       have "real n \<le> ?y * \<mu> ?G"
   924         using \<mu>G \<mu>G_pos by (cases "\<mu> ?G") (auto simp: field_simps)
   924         using \<mu>G \<mu>G_pos by (cases "\<mu> ?G") (auto simp: field_simps)
   925       also have "\<dots> = (\<integral>\<^isup>Sx. ?y * indicator ?G x \<partial>M)"
   925       also have "\<dots> = (\<integral>\<^isup>Sx. ?y * indicator ?G x \<partial>M)"
   926         using `0 \<le> ?y` `?g ?y \<in> ?A` gM
   926         using `0 \<le> ?y` `?g ?y \<in> ?A` gM
  1000 
  1000 
  1001 lemma (in measure_space) positive_integral_SUP_approx:
  1001 lemma (in measure_space) positive_integral_SUP_approx:
  1002   assumes f: "incseq f" "\<And>i. f i \<in> borel_measurable M" "\<And>i x. 0 \<le> f i x"
  1002   assumes f: "incseq f" "\<And>i. f i \<in> borel_measurable M" "\<And>i x. 0 \<le> f i x"
  1003   and u: "simple_function M u" "u \<le> (SUP i. f i)" "u`space M \<subseteq> {0..<\<infinity>}"
  1003   and u: "simple_function M u" "u \<le> (SUP i. f i)" "u`space M \<subseteq> {0..<\<infinity>}"
  1004   shows "integral\<^isup>S M u \<le> (SUP i. integral\<^isup>P M (f i))" (is "_ \<le> ?S")
  1004   shows "integral\<^isup>S M u \<le> (SUP i. integral\<^isup>P M (f i))" (is "_ \<le> ?S")
  1005 proof (rule extreal_le_mult_one_interval)
  1005 proof (rule ereal_le_mult_one_interval)
  1006   have "0 \<le> (SUP i. integral\<^isup>P M (f i))"
  1006   have "0 \<le> (SUP i. integral\<^isup>P M (f i))"
  1007     using f(3) by (auto intro!: le_SUPI2 positive_integral_positive)
  1007     using f(3) by (auto intro!: le_SUPI2 positive_integral_positive)
  1008   then show "(SUP i. integral\<^isup>P M (f i)) \<noteq> -\<infinity>" by auto
  1008   then show "(SUP i. integral\<^isup>P M (f i)) \<noteq> -\<infinity>" by auto
  1009   have u_range: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> u x \<and> u x \<noteq> \<infinity>"
  1009   have u_range: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> u x \<and> u x \<noteq> \<infinity>"
  1010     using u(3) by auto
  1010     using u(3) by auto
  1011   fix a :: extreal assume "0 < a" "a < 1"
  1011   fix a :: ereal assume "0 < a" "a < 1"
  1012   hence "a \<noteq> 0" by auto
  1012   hence "a \<noteq> 0" by auto
  1013   let "?B i" = "{x \<in> space M. a * u x \<le> f i x}"
  1013   let "?B i" = "{x \<in> space M. a * u x \<le> f i x}"
  1014   have B: "\<And>i. ?B i \<in> sets M"
  1014   have B: "\<And>i. ?B i \<in> sets M"
  1015     using f `simple_function M u` by (auto simp: borel_measurable_simple_function)
  1015     using f `simple_function M u` by (auto simp: borel_measurable_simple_function)
  1016 
  1016 
  1041         assume "u x = 0" thus ?thesis using `x \<in> space M` f(3) by simp
  1041         assume "u x = 0" thus ?thesis using `x \<in> space M` f(3) by simp
  1042       next
  1042       next
  1043         assume "u x \<noteq> 0"
  1043         assume "u x \<noteq> 0"
  1044         with `a < 1` u_range[OF `x \<in> space M`]
  1044         with `a < 1` u_range[OF `x \<in> space M`]
  1045         have "a * u x < 1 * u x"
  1045         have "a * u x < 1 * u x"
  1046           by (intro extreal_mult_strict_right_mono) (auto simp: image_iff)
  1046           by (intro ereal_mult_strict_right_mono) (auto simp: image_iff)
  1047         also have "\<dots> \<le> (SUP i. f i x)" using u(2) by (auto simp: le_fun_def SUPR_apply)
  1047         also have "\<dots> \<le> (SUP i. f i x)" using u(2) by (auto simp: le_fun_def SUPR_apply)
  1048         finally obtain i where "a * u x < f i x" unfolding SUPR_def
  1048         finally obtain i where "a * u x < f i x" unfolding SUPR_def
  1049           by (auto simp add: less_Sup_iff)
  1049           by (auto simp add: less_Sup_iff)
  1050         hence "a * u x \<le> f i x" by auto
  1050         hence "a * u x \<le> f i x" by auto
  1051         thus ?thesis using `x \<in> space M` by auto
  1051         thus ?thesis using `x \<in> space M` by auto
  1054     then show "?thesis i" using continuity_from_below[OF 1 2] by simp
  1054     then show "?thesis i" using continuity_from_below[OF 1 2] by simp
  1055   qed
  1055   qed
  1056 
  1056 
  1057   have "integral\<^isup>S M u = (SUP i. integral\<^isup>S M (?uB i))"
  1057   have "integral\<^isup>S M u = (SUP i. integral\<^isup>S M (?uB i))"
  1058     unfolding simple_integral_indicator[OF B `simple_function M u`]
  1058     unfolding simple_integral_indicator[OF B `simple_function M u`]
  1059   proof (subst SUPR_extreal_setsum, safe)
  1059   proof (subst SUPR_ereal_setsum, safe)
  1060     fix x n assume "x \<in> space M"
  1060     fix x n assume "x \<in> space M"
  1061     with u_range show "incseq (\<lambda>i. u x * \<mu> (?B' (u x) i))" "\<And>i. 0 \<le> u x * \<mu> (?B' (u x) i)"
  1061     with u_range show "incseq (\<lambda>i. u x * \<mu> (?B' (u x) i))" "\<And>i. 0 \<le> u x * \<mu> (?B' (u x) i)"
  1062       using B_mono B_u by (auto intro!: measure_mono extreal_mult_left_mono incseq_SucI simp: extreal_zero_le_0_iff)
  1062       using B_mono B_u by (auto intro!: measure_mono ereal_mult_left_mono incseq_SucI simp: ereal_zero_le_0_iff)
  1063   next
  1063   next
  1064     show "integral\<^isup>S M u = (\<Sum>i\<in>u ` space M. SUP n. i * \<mu> (?B' i n))"
  1064     show "integral\<^isup>S M u = (\<Sum>i\<in>u ` space M. SUP n. i * \<mu> (?B' i n))"
  1065       using measure_conv u_range B_u unfolding simple_integral_def
  1065       using measure_conv u_range B_u unfolding simple_integral_def
  1066       by (auto intro!: setsum_cong SUPR_extreal_cmult[symmetric])
  1066       by (auto intro!: setsum_cong SUPR_ereal_cmult[symmetric])
  1067   qed
  1067   qed
  1068   moreover
  1068   moreover
  1069   have "a * (SUP i. integral\<^isup>S M (?uB i)) \<le> ?S"
  1069   have "a * (SUP i. integral\<^isup>S M (?uB i)) \<le> ?S"
  1070     apply (subst SUPR_extreal_cmult[symmetric])
  1070     apply (subst SUPR_ereal_cmult[symmetric])
  1071   proof (safe intro!: SUP_mono bexI)
  1071   proof (safe intro!: SUP_mono bexI)
  1072     fix i
  1072     fix i
  1073     have "a * integral\<^isup>S M (?uB i) = (\<integral>\<^isup>Sx. a * ?uB i x \<partial>M)"
  1073     have "a * integral\<^isup>S M (?uB i) = (\<integral>\<^isup>Sx. a * ?uB i x \<partial>M)"
  1074       using B `simple_function M u` u_range
  1074       using B `simple_function M u` u_range
  1075       by (subst simple_integral_mult) (auto split: split_indicator)
  1075       by (subst simple_integral_mult) (auto split: split_indicator)
  1232   note l = positive_integral_monotone_convergence_simple[OF this(2,5,1)] this
  1232   note l = positive_integral_monotone_convergence_simple[OF this(2,5,1)] this
  1233 
  1233 
  1234   have inc: "incseq (\<lambda>i. a * integral\<^isup>S M (u i))" "incseq (\<lambda>i. integral\<^isup>S M (v i))"
  1234   have inc: "incseq (\<lambda>i. a * integral\<^isup>S M (u i))" "incseq (\<lambda>i. integral\<^isup>S M (v i))"
  1235     using u v `0 \<le> a`
  1235     using u v `0 \<le> a`
  1236     by (auto simp: incseq_Suc_iff le_fun_def
  1236     by (auto simp: incseq_Suc_iff le_fun_def
  1237              intro!: add_mono extreal_mult_left_mono simple_integral_mono)
  1237              intro!: add_mono ereal_mult_left_mono simple_integral_mono)
  1238   have pos: "\<And>i. 0 \<le> integral\<^isup>S M (u i)" "\<And>i. 0 \<le> integral\<^isup>S M (v i)" "\<And>i. 0 \<le> a * integral\<^isup>S M (u i)"
  1238   have pos: "\<And>i. 0 \<le> integral\<^isup>S M (u i)" "\<And>i. 0 \<le> integral\<^isup>S M (v i)" "\<And>i. 0 \<le> a * integral\<^isup>S M (u i)"
  1239     using u v `0 \<le> a` by (auto simp: simple_integral_positive)
  1239     using u v `0 \<le> a` by (auto simp: simple_integral_positive)
  1240   { fix i from pos[of i] have "a * integral\<^isup>S M (u i) \<noteq> -\<infinity>" "integral\<^isup>S M (v i) \<noteq> -\<infinity>"
  1240   { fix i from pos[of i] have "a * integral\<^isup>S M (u i) \<noteq> -\<infinity>" "integral\<^isup>S M (v i) \<noteq> -\<infinity>"
  1241       by (auto split: split_if_asm) }
  1241       by (auto split: split_if_asm) }
  1242   note not_MInf = this
  1242   note not_MInf = this
  1243 
  1243 
  1244   have l': "(SUP i. integral\<^isup>S M (l i)) = (SUP i. integral\<^isup>S M (?L' i))"
  1244   have l': "(SUP i. integral\<^isup>S M (l i)) = (SUP i. integral\<^isup>S M (?L' i))"
  1245   proof (rule SUP_simple_integral_sequences[OF l(3,6,2)])
  1245   proof (rule SUP_simple_integral_sequences[OF l(3,6,2)])
  1246     show "incseq ?L'" "\<And>i x. 0 \<le> ?L' i x" "\<And>i. simple_function M (?L' i)"
  1246     show "incseq ?L'" "\<And>i x. 0 \<le> ?L' i x" "\<And>i. simple_function M (?L' i)"
  1247       using u v  `0 \<le> a` unfolding incseq_Suc_iff le_fun_def
  1247       using u v  `0 \<le> a` unfolding incseq_Suc_iff le_fun_def
  1248       by (auto intro!: add_mono extreal_mult_left_mono extreal_add_nonneg_nonneg)
  1248       by (auto intro!: add_mono ereal_mult_left_mono ereal_add_nonneg_nonneg)
  1249     { fix x
  1249     { fix x
  1250       { fix i have "a * u i x \<noteq> -\<infinity>" "v i x \<noteq> -\<infinity>" "u i x \<noteq> -\<infinity>" using `0 \<le> a` u(6)[of i x] v(6)[of i x]
  1250       { fix i have "a * u i x \<noteq> -\<infinity>" "v i x \<noteq> -\<infinity>" "u i x \<noteq> -\<infinity>" using `0 \<le> a` u(6)[of i x] v(6)[of i x]
  1251           by auto }
  1251           by auto }
  1252       then have "(SUP i. a * u i x + v i x) = a * (SUP i. u i x) + (SUP i. v i x)"
  1252       then have "(SUP i. a * u i x + v i x) = a * (SUP i. u i x) + (SUP i. v i x)"
  1253         using `0 \<le> a` u(3) v(3) u(6)[of _ x] v(6)[of _ x]
  1253         using `0 \<le> a` u(3) v(3) u(6)[of _ x] v(6)[of _ x]
  1254         by (subst SUPR_extreal_cmult[symmetric, OF u(6) `0 \<le> a`])
  1254         by (subst SUPR_ereal_cmult[symmetric, OF u(6) `0 \<le> a`])
  1255            (auto intro!: SUPR_extreal_add
  1255            (auto intro!: SUPR_ereal_add
  1256                  simp: incseq_Suc_iff le_fun_def add_mono extreal_mult_left_mono extreal_add_nonneg_nonneg) }
  1256                  simp: incseq_Suc_iff le_fun_def add_mono ereal_mult_left_mono ereal_add_nonneg_nonneg) }
  1257     then show "AE x. (SUP i. l i x) = (SUP i. ?L' i x)"
  1257     then show "AE x. (SUP i. l i x) = (SUP i. ?L' i x)"
  1258       unfolding l(5) using `0 \<le> a` u(5) v(5) l(5) f(2) g(2)
  1258       unfolding l(5) using `0 \<le> a` u(5) v(5) l(5) f(2) g(2)
  1259       by (intro AE_I2) (auto split: split_max simp add: extreal_add_nonneg_nonneg)
  1259       by (intro AE_I2) (auto split: split_max simp add: ereal_add_nonneg_nonneg)
  1260   qed
  1260   qed
  1261   also have "\<dots> = (SUP i. a * integral\<^isup>S M (u i) + integral\<^isup>S M (v i))"
  1261   also have "\<dots> = (SUP i. a * integral\<^isup>S M (u i) + integral\<^isup>S M (v i))"
  1262     using u(2, 6) v(2, 6) `0 \<le> a` by (auto intro!: arg_cong[where f="SUPR UNIV"] ext)
  1262     using u(2, 6) v(2, 6) `0 \<le> a` by (auto intro!: arg_cong[where f="SUPR UNIV"] ext)
  1263   finally have "(\<integral>\<^isup>+ x. max 0 (a * f x + g x) \<partial>M) = a * (\<integral>\<^isup>+x. max 0 (f x) \<partial>M) + (\<integral>\<^isup>+x. max 0 (g x) \<partial>M)"
  1263   finally have "(\<integral>\<^isup>+ x. max 0 (a * f x + g x) \<partial>M) = a * (\<integral>\<^isup>+x. max 0 (f x) \<partial>M) + (\<integral>\<^isup>+x. max 0 (g x) \<partial>M)"
  1264     unfolding l(5)[symmetric] u(5)[symmetric] v(5)[symmetric]
  1264     unfolding l(5)[symmetric] u(5)[symmetric] v(5)[symmetric]
  1265     unfolding l(1)[symmetric] u(1)[symmetric] v(1)[symmetric]
  1265     unfolding l(1)[symmetric] u(1)[symmetric] v(1)[symmetric]
  1266     apply (subst SUPR_extreal_cmult[symmetric, OF pos(1) `0 \<le> a`])
  1266     apply (subst SUPR_ereal_cmult[symmetric, OF pos(1) `0 \<le> a`])
  1267     apply (subst SUPR_extreal_add[symmetric, OF inc not_MInf]) .
  1267     apply (subst SUPR_ereal_add[symmetric, OF inc not_MInf]) .
  1268   then show ?thesis by (simp add: positive_integral_max_0)
  1268   then show ?thesis by (simp add: positive_integral_max_0)
  1269 qed
  1269 qed
  1270 
  1270 
  1271 lemma (in measure_space) positive_integral_cmult:
  1271 lemma (in measure_space) positive_integral_cmult:
  1272   assumes f: "f \<in> borel_measurable M" "AE x. 0 \<le> f x" "0 \<le> c"
  1272   assumes f: "f \<in> borel_measurable M" "AE x. 0 \<le> f x" "0 \<le> c"
  1273   shows "(\<integral>\<^isup>+ x. c * f x \<partial>M) = c * integral\<^isup>P M f"
  1273   shows "(\<integral>\<^isup>+ x. c * f x \<partial>M) = c * integral\<^isup>P M f"
  1274 proof -
  1274 proof -
  1275   have [simp]: "\<And>x. c * max 0 (f x) = max 0 (c * f x)" using `0 \<le> c`
  1275   have [simp]: "\<And>x. c * max 0 (f x) = max 0 (c * f x)" using `0 \<le> c`
  1276     by (auto split: split_max simp: extreal_zero_le_0_iff)
  1276     by (auto split: split_max simp: ereal_zero_le_0_iff)
  1277   have "(\<integral>\<^isup>+ x. c * f x \<partial>M) = (\<integral>\<^isup>+ x. c * max 0 (f x) \<partial>M)"
  1277   have "(\<integral>\<^isup>+ x. c * f x \<partial>M) = (\<integral>\<^isup>+ x. c * max 0 (f x) \<partial>M)"
  1278     by (simp add: positive_integral_max_0)
  1278     by (simp add: positive_integral_max_0)
  1279   then show ?thesis
  1279   then show ?thesis
  1280     using positive_integral_linear[OF _ _ `0 \<le> c`, of "\<lambda>x. max 0 (f x)" "\<lambda>x. 0"] f
  1280     using positive_integral_linear[OF _ _ `0 \<le> c`, of "\<lambda>x. max 0 (f x)" "\<lambda>x. 0"] f
  1281     by (auto simp: positive_integral_max_0)
  1281     by (auto simp: positive_integral_max_0)
  1300   assumes f: "f \<in> borel_measurable M" "AE x. 0 \<le> f x"
  1300   assumes f: "f \<in> borel_measurable M" "AE x. 0 \<le> f x"
  1301   and g: "g \<in> borel_measurable M" "AE x. 0 \<le> g x"
  1301   and g: "g \<in> borel_measurable M" "AE x. 0 \<le> g x"
  1302   shows "(\<integral>\<^isup>+ x. f x + g x \<partial>M) = integral\<^isup>P M f + integral\<^isup>P M g"
  1302   shows "(\<integral>\<^isup>+ x. f x + g x \<partial>M) = integral\<^isup>P M f + integral\<^isup>P M g"
  1303 proof -
  1303 proof -
  1304   have ae: "AE x. max 0 (f x) + max 0 (g x) = max 0 (f x + g x)"
  1304   have ae: "AE x. max 0 (f x) + max 0 (g x) = max 0 (f x + g x)"
  1305     using assms by (auto split: split_max simp: extreal_add_nonneg_nonneg)
  1305     using assms by (auto split: split_max simp: ereal_add_nonneg_nonneg)
  1306   have "(\<integral>\<^isup>+ x. f x + g x \<partial>M) = (\<integral>\<^isup>+ x. max 0 (f x + g x) \<partial>M)"
  1306   have "(\<integral>\<^isup>+ x. f x + g x \<partial>M) = (\<integral>\<^isup>+ x. max 0 (f x + g x) \<partial>M)"
  1307     by (simp add: positive_integral_max_0)
  1307     by (simp add: positive_integral_max_0)
  1308   also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (f x) + max 0 (g x) \<partial>M)"
  1308   also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (f x) + max 0 (g x) \<partial>M)"
  1309     unfolding ae[THEN positive_integral_cong_AE] ..
  1309     unfolding ae[THEN positive_integral_cong_AE] ..
  1310   also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (f x) \<partial>M) + (\<integral>\<^isup>+ x. max 0 (g x) \<partial>M)"
  1310   also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (f x) \<partial>M) + (\<integral>\<^isup>+ x. max 0 (g x) \<partial>M)"
  1323   from f this assms(1) show ?thesis
  1323   from f this assms(1) show ?thesis
  1324   proof induct
  1324   proof induct
  1325     case (insert i P)
  1325     case (insert i P)
  1326     then have "f i \<in> borel_measurable M" "AE x. 0 \<le> f i x"
  1326     then have "f i \<in> borel_measurable M" "AE x. 0 \<le> f i x"
  1327       "(\<lambda>x. \<Sum>i\<in>P. f i x) \<in> borel_measurable M" "AE x. 0 \<le> (\<Sum>i\<in>P. f i x)"
  1327       "(\<lambda>x. \<Sum>i\<in>P. f i x) \<in> borel_measurable M" "AE x. 0 \<le> (\<Sum>i\<in>P. f i x)"
  1328       by (auto intro!: borel_measurable_extreal_setsum setsum_nonneg)
  1328       by (auto intro!: borel_measurable_ereal_setsum setsum_nonneg)
  1329     from positive_integral_add[OF this]
  1329     from positive_integral_add[OF this]
  1330     show ?case using insert by auto
  1330     show ?case using insert by auto
  1331   qed simp
  1331   qed simp
  1332 qed simp
  1332 qed simp
  1333 
  1333 
  1340     using `A \<in> sets M` u by auto
  1340     using `A \<in> sets M` u by auto
  1341   hence "\<mu> ?A = (\<integral>\<^isup>+ x. indicator ?A x \<partial>M)"
  1341   hence "\<mu> ?A = (\<integral>\<^isup>+ x. indicator ?A x \<partial>M)"
  1342     using positive_integral_indicator by simp
  1342     using positive_integral_indicator by simp
  1343   also have "\<dots> \<le> (\<integral>\<^isup>+ x. c * (u x * indicator A x) \<partial>M)" using u c
  1343   also have "\<dots> \<le> (\<integral>\<^isup>+ x. c * (u x * indicator A x) \<partial>M)" using u c
  1344     by (auto intro!: positive_integral_mono_AE
  1344     by (auto intro!: positive_integral_mono_AE
  1345       simp: indicator_def extreal_zero_le_0_iff)
  1345       simp: indicator_def ereal_zero_le_0_iff)
  1346   also have "\<dots> = c * (\<integral>\<^isup>+ x. u x * indicator A x \<partial>M)"
  1346   also have "\<dots> = c * (\<integral>\<^isup>+ x. u x * indicator A x \<partial>M)"
  1347     using assms
  1347     using assms
  1348     by (auto intro!: positive_integral_cmult borel_measurable_indicator simp: extreal_zero_le_0_iff)
  1348     by (auto intro!: positive_integral_cmult borel_measurable_indicator simp: ereal_zero_le_0_iff)
  1349   finally show ?thesis .
  1349   finally show ?thesis .
  1350 qed
  1350 qed
  1351 
  1351 
  1352 lemma (in measure_space) positive_integral_noteq_infinite:
  1352 lemma (in measure_space) positive_integral_noteq_infinite:
  1353   assumes g: "g \<in> borel_measurable M" "AE x. 0 \<le> g x"
  1353   assumes g: "g \<in> borel_measurable M" "AE x. 0 \<le> g x"
  1373   and fin: "integral\<^isup>P M g \<noteq> \<infinity>"
  1373   and fin: "integral\<^isup>P M g \<noteq> \<infinity>"
  1374   and mono: "AE x. g x \<le> f x"
  1374   and mono: "AE x. g x \<le> f x"
  1375   shows "(\<integral>\<^isup>+ x. f x - g x \<partial>M) = integral\<^isup>P M f - integral\<^isup>P M g"
  1375   shows "(\<integral>\<^isup>+ x. f x - g x \<partial>M) = integral\<^isup>P M f - integral\<^isup>P M g"
  1376 proof -
  1376 proof -
  1377   have diff: "(\<lambda>x. f x - g x) \<in> borel_measurable M" "AE x. 0 \<le> f x - g x"
  1377   have diff: "(\<lambda>x. f x - g x) \<in> borel_measurable M" "AE x. 0 \<le> f x - g x"
  1378     using assms by (auto intro: extreal_diff_positive)
  1378     using assms by (auto intro: ereal_diff_positive)
  1379   have pos_f: "AE x. 0 \<le> f x" using mono g by auto
  1379   have pos_f: "AE x. 0 \<le> f x" using mono g by auto
  1380   { fix a b :: extreal assume "0 \<le> a" "a \<noteq> \<infinity>" "0 \<le> b" "a \<le> b" then have "b - a + a = b"
  1380   { fix a b :: ereal assume "0 \<le> a" "a \<noteq> \<infinity>" "0 \<le> b" "a \<le> b" then have "b - a + a = b"
  1381       by (cases rule: extreal2_cases[of a b]) auto }
  1381       by (cases rule: ereal2_cases[of a b]) auto }
  1382   note * = this
  1382   note * = this
  1383   then have "AE x. f x = f x - g x + g x"
  1383   then have "AE x. f x = f x - g x + g x"
  1384     using mono positive_integral_noteq_infinite[OF g fin] assms by auto
  1384     using mono positive_integral_noteq_infinite[OF g fin] assms by auto
  1385   then have **: "integral\<^isup>P M f = (\<integral>\<^isup>+x. f x - g x \<partial>M) + integral\<^isup>P M g"
  1385   then have **: "integral\<^isup>P M f = (\<integral>\<^isup>+x. f x - g x \<partial>M) + integral\<^isup>P M g"
  1386     unfolding positive_integral_add[OF diff g, symmetric]
  1386     unfolding positive_integral_add[OF diff g, symmetric]
  1387     by (rule positive_integral_cong_AE)
  1387     by (rule positive_integral_cong_AE)
  1388   show ?thesis unfolding **
  1388   show ?thesis unfolding **
  1389     using fin positive_integral_positive[of g]
  1389     using fin positive_integral_positive[of g]
  1390     by (cases rule: extreal2_cases[of "\<integral>\<^isup>+ x. f x - g x \<partial>M" "integral\<^isup>P M g"]) auto
  1390     by (cases rule: ereal2_cases[of "\<integral>\<^isup>+ x. f x - g x \<partial>M" "integral\<^isup>P M g"]) auto
  1391 qed
  1391 qed
  1392 
  1392 
  1393 lemma (in measure_space) positive_integral_suminf:
  1393 lemma (in measure_space) positive_integral_suminf:
  1394   assumes f: "\<And>i. f i \<in> borel_measurable M" "\<And>i. AE x. 0 \<le> f i x"
  1394   assumes f: "\<And>i. f i \<in> borel_measurable M" "\<And>i. AE x. 0 \<le> f i x"
  1395   shows "(\<integral>\<^isup>+ x. (\<Sum>i. f i x) \<partial>M) = (\<Sum>i. integral\<^isup>P M (f i))"
  1395   shows "(\<integral>\<^isup>+ x. (\<Sum>i. f i x) \<partial>M) = (\<Sum>i. integral\<^isup>P M (f i))"
  1396 proof -
  1396 proof -
  1397   have all_pos: "AE x. \<forall>i. 0 \<le> f i x"
  1397   have all_pos: "AE x. \<forall>i. 0 \<le> f i x"
  1398     using assms by (auto simp: AE_all_countable)
  1398     using assms by (auto simp: AE_all_countable)
  1399   have "(\<Sum>i. integral\<^isup>P M (f i)) = (SUP n. \<Sum>i<n. integral\<^isup>P M (f i))"
  1399   have "(\<Sum>i. integral\<^isup>P M (f i)) = (SUP n. \<Sum>i<n. integral\<^isup>P M (f i))"
  1400     using positive_integral_positive by (rule suminf_extreal_eq_SUPR)
  1400     using positive_integral_positive by (rule suminf_ereal_eq_SUPR)
  1401   also have "\<dots> = (SUP n. \<integral>\<^isup>+x. (\<Sum>i<n. f i x) \<partial>M)"
  1401   also have "\<dots> = (SUP n. \<integral>\<^isup>+x. (\<Sum>i<n. f i x) \<partial>M)"
  1402     unfolding positive_integral_setsum[OF f] ..
  1402     unfolding positive_integral_setsum[OF f] ..
  1403   also have "\<dots> = \<integral>\<^isup>+x. (SUP n. \<Sum>i<n. f i x) \<partial>M" using f all_pos
  1403   also have "\<dots> = \<integral>\<^isup>+x. (SUP n. \<Sum>i<n. f i x) \<partial>M" using f all_pos
  1404     by (intro positive_integral_monotone_convergence_SUP_AE[symmetric])
  1404     by (intro positive_integral_monotone_convergence_SUP_AE[symmetric])
  1405        (elim AE_mp, auto simp: setsum_nonneg simp del: setsum_lessThan_Suc intro!: AE_I2 setsum_mono3)
  1405        (elim AE_mp, auto simp: setsum_nonneg simp del: setsum_lessThan_Suc intro!: AE_I2 setsum_mono3)
  1406   also have "\<dots> = \<integral>\<^isup>+x. (\<Sum>i. f i x) \<partial>M" using all_pos
  1406   also have "\<dots> = \<integral>\<^isup>+x. (\<Sum>i. f i x) \<partial>M" using all_pos
  1407     by (intro positive_integral_cong_AE) (auto simp: suminf_extreal_eq_SUPR)
  1407     by (intro positive_integral_cong_AE) (auto simp: suminf_ereal_eq_SUPR)
  1408   finally show ?thesis by simp
  1408   finally show ?thesis by simp
  1409 qed
  1409 qed
  1410 
  1410 
  1411 text {* Fatou's lemma: convergence theorem on limes inferior *}
  1411 text {* Fatou's lemma: convergence theorem on limes inferior *}
  1412 lemma (in measure_space) positive_integral_lim_INF:
  1412 lemma (in measure_space) positive_integral_lim_INF:
  1413   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> extreal"
  1413   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1414   assumes u: "\<And>i. u i \<in> borel_measurable M" "\<And>i. AE x. 0 \<le> u i x"
  1414   assumes u: "\<And>i. u i \<in> borel_measurable M" "\<And>i. AE x. 0 \<le> u i x"
  1415   shows "(\<integral>\<^isup>+ x. liminf (\<lambda>n. u n x) \<partial>M) \<le> liminf (\<lambda>n. integral\<^isup>P M (u n))"
  1415   shows "(\<integral>\<^isup>+ x. liminf (\<lambda>n. u n x) \<partial>M) \<le> liminf (\<lambda>n. integral\<^isup>P M (u n))"
  1416 proof -
  1416 proof -
  1417   have pos: "AE x. \<forall>i. 0 \<le> u i x" using u by (auto simp: AE_all_countable)
  1417   have pos: "AE x. \<forall>i. 0 \<le> u i x" using u by (auto simp: AE_all_countable)
  1418   have "(\<integral>\<^isup>+ x. liminf (\<lambda>n. u n x) \<partial>M) =
  1418   have "(\<integral>\<^isup>+ x. liminf (\<lambda>n. u n x) \<partial>M) =
  1433 proof -
  1433 proof -
  1434   interpret M': sigma_algebra M' by (intro sigma_algebra_cong) auto
  1434   interpret M': sigma_algebra M' by (intro sigma_algebra_cong) auto
  1435   show ?thesis
  1435   show ?thesis
  1436   proof
  1436   proof
  1437     have pos: "\<And>A. AE x. 0 \<le> u x * indicator A x"
  1437     have pos: "\<And>A. AE x. 0 \<le> u x * indicator A x"
  1438       using u by (auto simp: extreal_zero_le_0_iff)
  1438       using u by (auto simp: ereal_zero_le_0_iff)
  1439     then show "positive M' (measure M')" unfolding M'
  1439     then show "positive M' (measure M')" unfolding M'
  1440       using u(1) by (auto simp: positive_def intro!: positive_integral_positive)
  1440       using u(1) by (auto simp: positive_def intro!: positive_integral_positive)
  1441     show "countably_additive M' (measure M')"
  1441     show "countably_additive M' (measure M')"
  1442     proof (intro countably_additiveI)
  1442     proof (intro countably_additiveI)
  1443       fix A :: "nat \<Rightarrow> 'a set" assume "range A \<subseteq> sets M'"
  1443       fix A :: "nat \<Rightarrow> 'a set" assume "range A \<subseteq> sets M'"
  1447       have "(\<Sum>n. measure M' (A n)) = (\<integral>\<^isup>+ x. (\<Sum>n. u x * indicator (A n) x) \<partial>M)"
  1447       have "(\<Sum>n. measure M' (A n)) = (\<integral>\<^isup>+ x. (\<Sum>n. u x * indicator (A n) x) \<partial>M)"
  1448         unfolding M' using u(1) *
  1448         unfolding M' using u(1) *
  1449         by (simp add: positive_integral_suminf[OF _ pos, symmetric])
  1449         by (simp add: positive_integral_suminf[OF _ pos, symmetric])
  1450       also have "\<dots> = (\<integral>\<^isup>+ x. u x * (\<Sum>n. indicator (A n) x) \<partial>M)" using u
  1450       also have "\<dots> = (\<integral>\<^isup>+ x. u x * (\<Sum>n. indicator (A n) x) \<partial>M)" using u
  1451         by (intro positive_integral_cong_AE)
  1451         by (intro positive_integral_cong_AE)
  1452            (elim AE_mp, auto intro!: AE_I2 suminf_cmult_extreal)
  1452            (elim AE_mp, auto intro!: AE_I2 suminf_cmult_ereal)
  1453       also have "\<dots> = (\<integral>\<^isup>+ x. u x * indicator (\<Union>n. A n) x \<partial>M)"
  1453       also have "\<dots> = (\<integral>\<^isup>+ x. u x * indicator (\<Union>n. A n) x \<partial>M)"
  1454         unfolding suminf_indicator[OF disj] ..
  1454         unfolding suminf_indicator[OF disj] ..
  1455       finally show "(\<Sum>n. measure M' (A n)) = measure M' (\<Union>x. A x)"
  1455       finally show "(\<Sum>n. measure M' (A n)) = measure M' (\<Union>x. A x)"
  1456         unfolding M' by simp
  1456         unfolding M' by simp
  1457     qed
  1457     qed
  1496   { fix i
  1496   { fix i
  1497     let "?I y x" = "indicator (G i -` {y} \<inter> space M) x"
  1497     let "?I y x" = "indicator (G i -` {y} \<inter> space M) x"
  1498     { fix x assume *: "x \<in> space M" "0 \<le> f x" "0 \<le> g x"
  1498     { fix x assume *: "x \<in> space M" "0 \<le> f x" "0 \<le> g x"
  1499       then have [simp]: "G i ` space M \<inter> {y. G i x = y \<and> x \<in> space M} = {G i x}" by auto
  1499       then have [simp]: "G i ` space M \<inter> {y. G i x = y \<and> x \<in> space M} = {G i x}" by auto
  1500       from * G' G have "(\<Sum>y\<in>G i`space M. y * (f x * ?I y x)) = f x * (\<Sum>y\<in>G i`space M. (y * ?I y x))"
  1500       from * G' G have "(\<Sum>y\<in>G i`space M. y * (f x * ?I y x)) = f x * (\<Sum>y\<in>G i`space M. (y * ?I y x))"
  1501         by (subst setsum_extreal_right_distrib) (auto simp: ac_simps)
  1501         by (subst setsum_ereal_right_distrib) (auto simp: ac_simps)
  1502       also have "\<dots> = f x * G i x"
  1502       also have "\<dots> = f x * G i x"
  1503         by (simp add: indicator_def if_distrib setsum_cases)
  1503         by (simp add: indicator_def if_distrib setsum_cases)
  1504       finally have "(\<Sum>y\<in>G i`space M. y * (f x * ?I y x)) = f x * G i x" . }
  1504       finally have "(\<Sum>y\<in>G i`space M. y * (f x * ?I y x)) = f x * G i x" . }
  1505     note to_singleton = this
  1505     note to_singleton = this
  1506     have "integral\<^isup>P M' (G i) = integral\<^isup>S M' (G i)"
  1506     have "integral\<^isup>P M' (G i) = integral\<^isup>S M' (G i)"
  1519     by (simp cong: T.positive_integral_cong_AE)
  1519     by (simp cong: T.positive_integral_cong_AE)
  1520   also have "\<dots> = (SUP i. (\<integral>\<^isup>+x. f x * G i x \<partial>M))" by simp
  1520   also have "\<dots> = (SUP i. (\<integral>\<^isup>+x. f x * G i x \<partial>M))" by simp
  1521   also have "\<dots> = (\<integral>\<^isup>+x. (SUP i. f x * G i x) \<partial>M)"
  1521   also have "\<dots> = (\<integral>\<^isup>+x. (SUP i. f x * G i x) \<partial>M)"
  1522     using f G' G(2)[THEN incseq_SucD] G
  1522     using f G' G(2)[THEN incseq_SucD] G
  1523     by (intro positive_integral_monotone_convergence_SUP_AE[symmetric])
  1523     by (intro positive_integral_monotone_convergence_SUP_AE[symmetric])
  1524        (auto simp: extreal_mult_left_mono le_fun_def extreal_zero_le_0_iff)
  1524        (auto simp: ereal_mult_left_mono le_fun_def ereal_zero_le_0_iff)
  1525   also have "\<dots> = (\<integral>\<^isup>+x. f x * g x \<partial>M)" using f G' G g
  1525   also have "\<dots> = (\<integral>\<^isup>+x. f x * g x \<partial>M)" using f G' G g
  1526     by (intro positive_integral_cong_AE)
  1526     by (intro positive_integral_cong_AE)
  1527        (auto simp add: SUPR_extreal_cmult split: split_max)
  1527        (auto simp add: SUPR_ereal_cmult split: split_max)
  1528   finally show "integral\<^isup>P M' g = (\<integral>\<^isup>+x. f x * g x \<partial>M)" .
  1528   finally show "integral\<^isup>P M' g = (\<integral>\<^isup>+x. f x * g x \<partial>M)" .
  1529 qed
  1529 qed
  1530 
  1530 
  1531 lemma (in measure_space) positive_integral_0_iff:
  1531 lemma (in measure_space) positive_integral_0_iff:
  1532   assumes u: "u \<in> borel_measurable M" and pos: "AE x. 0 \<le> u x"
  1532   assumes u: "u \<in> borel_measurable M" and pos: "AE x. 0 \<le> u x"
  1539   proof
  1539   proof
  1540     assume "\<mu> ?A = 0"
  1540     assume "\<mu> ?A = 0"
  1541     with positive_integral_null_set[of ?A u] u
  1541     with positive_integral_null_set[of ?A u] u
  1542     show "integral\<^isup>P M u = 0" by (simp add: u_eq)
  1542     show "integral\<^isup>P M u = 0" by (simp add: u_eq)
  1543   next
  1543   next
  1544     { fix r :: extreal and n :: nat assume gt_1: "1 \<le> real n * r"
  1544     { fix r :: ereal and n :: nat assume gt_1: "1 \<le> real n * r"
  1545       then have "0 < real n * r" by (cases r) (auto split: split_if_asm simp: one_extreal_def)
  1545       then have "0 < real n * r" by (cases r) (auto split: split_if_asm simp: one_ereal_def)
  1546       then have "0 \<le> r" by (auto simp add: extreal_zero_less_0_iff) }
  1546       then have "0 \<le> r" by (auto simp add: ereal_zero_less_0_iff) }
  1547     note gt_1 = this
  1547     note gt_1 = this
  1548     assume *: "integral\<^isup>P M u = 0"
  1548     assume *: "integral\<^isup>P M u = 0"
  1549     let "?M n" = "{x \<in> space M. 1 \<le> real (n::nat) * u x}"
  1549     let "?M n" = "{x \<in> space M. 1 \<le> real (n::nat) * u x}"
  1550     have "0 = (SUP n. \<mu> (?M n \<inter> ?A))"
  1550     have "0 = (SUP n. \<mu> (?M n \<inter> ?A))"
  1551     proof -
  1551     proof -
  1552       { fix n :: nat
  1552       { fix n :: nat
  1553         from positive_integral_Markov_inequality[OF u pos, of ?A "extreal (real n)"]
  1553         from positive_integral_Markov_inequality[OF u pos, of ?A "ereal (real n)"]
  1554         have "\<mu> (?M n \<inter> ?A) \<le> 0" unfolding u_eq * using u by simp
  1554         have "\<mu> (?M n \<inter> ?A) \<le> 0" unfolding u_eq * using u by simp
  1555         moreover have "0 \<le> \<mu> (?M n \<inter> ?A)" using u by auto
  1555         moreover have "0 \<le> \<mu> (?M n \<inter> ?A)" using u by auto
  1556         ultimately have "\<mu> (?M n \<inter> ?A) = 0" by auto }
  1556         ultimately have "\<mu> (?M n \<inter> ?A) = 0" by auto }
  1557       thus ?thesis by simp
  1557       thus ?thesis by simp
  1558     qed
  1558     qed
  1564       show "incseq (\<lambda>n. {x \<in> space M. 1 \<le> real n * u x} \<inter> {x \<in> space M. u x \<noteq> 0})"
  1564       show "incseq (\<lambda>n. {x \<in> space M. 1 \<le> real n * u x} \<inter> {x \<in> space M. u x \<noteq> 0})"
  1565       proof (safe intro!: incseq_SucI)
  1565       proof (safe intro!: incseq_SucI)
  1566         fix n :: nat and x
  1566         fix n :: nat and x
  1567         assume *: "1 \<le> real n * u x"
  1567         assume *: "1 \<le> real n * u x"
  1568         also from gt_1[OF this] have "real n * u x \<le> real (Suc n) * u x"
  1568         also from gt_1[OF this] have "real n * u x \<le> real (Suc n) * u x"
  1569           using `0 \<le> u x` by (auto intro!: extreal_mult_right_mono)
  1569           using `0 \<le> u x` by (auto intro!: ereal_mult_right_mono)
  1570         finally show "1 \<le> real (Suc n) * u x" by auto
  1570         finally show "1 \<le> real (Suc n) * u x" by auto
  1571       qed
  1571       qed
  1572     qed
  1572     qed
  1573     also have "\<dots> = \<mu> {x\<in>space M. 0 < u x}"
  1573     also have "\<dots> = \<mu> {x\<in>space M. 0 < u x}"
  1574     proof (safe intro!: arg_cong[where f="\<mu>"] dest!: gt_1)
  1574     proof (safe intro!: arg_cong[where f="\<mu>"] dest!: gt_1)
  1577       proof (cases "u x")
  1577       proof (cases "u x")
  1578         case (real r) with `0 < u x` have "0 < r" by auto
  1578         case (real r) with `0 < u x` have "0 < r" by auto
  1579         obtain j :: nat where "1 / r \<le> real j" using real_arch_simple ..
  1579         obtain j :: nat where "1 / r \<le> real j" using real_arch_simple ..
  1580         hence "1 / r * r \<le> real j * r" unfolding mult_le_cancel_right using `0 < r` by auto
  1580         hence "1 / r * r \<le> real j * r" unfolding mult_le_cancel_right using `0 < r` by auto
  1581         hence "1 \<le> real j * r" using real `0 < r` by auto
  1581         hence "1 \<le> real j * r" using real `0 < r` by auto
  1582         thus ?thesis using `0 < r` real by (auto simp: one_extreal_def)
  1582         thus ?thesis using `0 < r` real by (auto simp: one_ereal_def)
  1583       qed (insert `0 < u x`, auto)
  1583       qed (insert `0 < u x`, auto)
  1584     qed auto
  1584     qed auto
  1585     finally have "\<mu> {x\<in>space M. 0 < u x} = 0" by simp
  1585     finally have "\<mu> {x\<in>space M. 0 < u x} = 0" by simp
  1586     moreover
  1586     moreover
  1587     from pos have "AE x. \<not> (u x < 0)" by auto
  1587     from pos have "AE x. \<not> (u x < 0)" by auto
  1616   shows "integral\<^isup>P (restricted_space A) f = (\<integral>\<^isup>+ x. f x * indicator A x \<partial>M)"
  1616   shows "integral\<^isup>P (restricted_space A) f = (\<integral>\<^isup>+ x. f x * indicator A x \<partial>M)"
  1617     (is "integral\<^isup>P ?R f = integral\<^isup>P M ?f")
  1617     (is "integral\<^isup>P ?R f = integral\<^isup>P M ?f")
  1618 proof -
  1618 proof -
  1619   interpret R: measure_space ?R
  1619   interpret R: measure_space ?R
  1620     by (rule restricted_measure_space) fact
  1620     by (rule restricted_measure_space) fact
  1621   let "?I g x" = "g x * indicator A x :: extreal"
  1621   let "?I g x" = "g x * indicator A x :: ereal"
  1622   show ?thesis
  1622   show ?thesis
  1623     unfolding positive_integral_def
  1623     unfolding positive_integral_def
  1624     unfolding simple_function_restricted[OF A]
  1624     unfolding simple_function_restricted[OF A]
  1625     unfolding AE_restricted[OF A]
  1625     unfolding AE_restricted[OF A]
  1626   proof (safe intro!: SUPR_eq)
  1626   proof (safe intro!: SUPR_eq)
  1673 
  1673 
  1674 section "Lebesgue Integral"
  1674 section "Lebesgue Integral"
  1675 
  1675 
  1676 definition integrable where
  1676 definition integrable where
  1677   "integrable M f \<longleftrightarrow> f \<in> borel_measurable M \<and>
  1677   "integrable M f \<longleftrightarrow> f \<in> borel_measurable M \<and>
  1678     (\<integral>\<^isup>+ x. extreal (f x) \<partial>M) \<noteq> \<infinity> \<and> (\<integral>\<^isup>+ x. extreal (- f x) \<partial>M) \<noteq> \<infinity>"
  1678     (\<integral>\<^isup>+ x. ereal (f x) \<partial>M) \<noteq> \<infinity> \<and> (\<integral>\<^isup>+ x. ereal (- f x) \<partial>M) \<noteq> \<infinity>"
  1679 
  1679 
  1680 lemma integrableD[dest]:
  1680 lemma integrableD[dest]:
  1681   assumes "integrable M f"
  1681   assumes "integrable M f"
  1682   shows "f \<in> borel_measurable M" "(\<integral>\<^isup>+ x. extreal (f x) \<partial>M) \<noteq> \<infinity>" "(\<integral>\<^isup>+ x. extreal (- f x) \<partial>M) \<noteq> \<infinity>"
  1682   shows "f \<in> borel_measurable M" "(\<integral>\<^isup>+ x. ereal (f x) \<partial>M) \<noteq> \<infinity>" "(\<integral>\<^isup>+ x. ereal (- f x) \<partial>M) \<noteq> \<infinity>"
  1683   using assms unfolding integrable_def by auto
  1683   using assms unfolding integrable_def by auto
  1684 
  1684 
  1685 definition lebesgue_integral_def:
  1685 definition lebesgue_integral_def:
  1686   "integral\<^isup>L M f = real ((\<integral>\<^isup>+ x. extreal (f x) \<partial>M)) - real ((\<integral>\<^isup>+ x. extreal (- f x) \<partial>M))"
  1686   "integral\<^isup>L M f = real ((\<integral>\<^isup>+ x. ereal (f x) \<partial>M)) - real ((\<integral>\<^isup>+ x. ereal (- f x) \<partial>M))"
  1687 
  1687 
  1688 syntax
  1688 syntax
  1689   "_lebesgue_integral" :: "'a \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> ('a, 'b) measure_space_scheme \<Rightarrow> real" ("\<integral> _. _ \<partial>_" [60,61] 110)
  1689   "_lebesgue_integral" :: "'a \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> ('a, 'b) measure_space_scheme \<Rightarrow> real" ("\<integral> _. _ \<partial>_" [60,61] 110)
  1690 
  1690 
  1691 translations
  1691 translations
  1692   "\<integral> x. f \<partial>M" == "CONST integral\<^isup>L M (%x. f)"
  1692   "\<integral> x. f \<partial>M" == "CONST integral\<^isup>L M (%x. f)"
  1693 
  1693 
  1694 lemma (in measure_space) integrableE:
  1694 lemma (in measure_space) integrableE:
  1695   assumes "integrable M f"
  1695   assumes "integrable M f"
  1696   obtains r q where
  1696   obtains r q where
  1697     "(\<integral>\<^isup>+x. extreal (f x)\<partial>M) = extreal r"
  1697     "(\<integral>\<^isup>+x. ereal (f x)\<partial>M) = ereal r"
  1698     "(\<integral>\<^isup>+x. extreal (-f x)\<partial>M) = extreal q"
  1698     "(\<integral>\<^isup>+x. ereal (-f x)\<partial>M) = ereal q"
  1699     "f \<in> borel_measurable M" "integral\<^isup>L M f = r - q"
  1699     "f \<in> borel_measurable M" "integral\<^isup>L M f = r - q"
  1700   using assms unfolding integrable_def lebesgue_integral_def
  1700   using assms unfolding integrable_def lebesgue_integral_def
  1701   using positive_integral_positive[of "\<lambda>x. extreal (f x)"]
  1701   using positive_integral_positive[of "\<lambda>x. ereal (f x)"]
  1702   using positive_integral_positive[of "\<lambda>x. extreal (-f x)"]
  1702   using positive_integral_positive[of "\<lambda>x. ereal (-f x)"]
  1703   by (cases rule: extreal2_cases[of "(\<integral>\<^isup>+x. extreal (-f x)\<partial>M)" "(\<integral>\<^isup>+x. extreal (f x)\<partial>M)"]) auto
  1703   by (cases rule: ereal2_cases[of "(\<integral>\<^isup>+x. ereal (-f x)\<partial>M)" "(\<integral>\<^isup>+x. ereal (f x)\<partial>M)"]) auto
  1704 
  1704 
  1705 lemma (in measure_space) integral_cong:
  1705 lemma (in measure_space) integral_cong:
  1706   assumes "\<And>x. x \<in> space M \<Longrightarrow> f x = g x"
  1706   assumes "\<And>x. x \<in> space M \<Longrightarrow> f x = g x"
  1707   shows "integral\<^isup>L M f = integral\<^isup>L M g"
  1707   shows "integral\<^isup>L M f = integral\<^isup>L M g"
  1708   using assms by (simp cong: positive_integral_cong add: lebesgue_integral_def)
  1708   using assms by (simp cong: positive_integral_cong add: lebesgue_integral_def)
  1720 
  1720 
  1721 lemma (in measure_space) integral_cong_AE:
  1721 lemma (in measure_space) integral_cong_AE:
  1722   assumes cong: "AE x. f x = g x"
  1722   assumes cong: "AE x. f x = g x"
  1723   shows "integral\<^isup>L M f = integral\<^isup>L M g"
  1723   shows "integral\<^isup>L M f = integral\<^isup>L M g"
  1724 proof -
  1724 proof -
  1725   have *: "AE x. extreal (f x) = extreal (g x)"
  1725   have *: "AE x. ereal (f x) = ereal (g x)"
  1726     "AE x. extreal (- f x) = extreal (- g x)" using cong by auto
  1726     "AE x. ereal (- f x) = ereal (- g x)" using cong by auto
  1727   show ?thesis
  1727   show ?thesis
  1728     unfolding *[THEN positive_integral_cong_AE] lebesgue_integral_def ..
  1728     unfolding *[THEN positive_integral_cong_AE] lebesgue_integral_def ..
  1729 qed
  1729 qed
  1730 
  1730 
  1731 lemma (in measure_space) integrable_cong_AE:
  1731 lemma (in measure_space) integrable_cong_AE:
  1732   assumes borel: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1732   assumes borel: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1733   assumes "AE x. f x = g x"
  1733   assumes "AE x. f x = g x"
  1734   shows "integrable M f = integrable M g"
  1734   shows "integrable M f = integrable M g"
  1735 proof -
  1735 proof -
  1736   have "(\<integral>\<^isup>+ x. extreal (f x) \<partial>M) = (\<integral>\<^isup>+ x. extreal (g x) \<partial>M)"
  1736   have "(\<integral>\<^isup>+ x. ereal (f x) \<partial>M) = (\<integral>\<^isup>+ x. ereal (g x) \<partial>M)"
  1737     "(\<integral>\<^isup>+ x. extreal (- f x) \<partial>M) = (\<integral>\<^isup>+ x. extreal (- g x) \<partial>M)"
  1737     "(\<integral>\<^isup>+ x. ereal (- f x) \<partial>M) = (\<integral>\<^isup>+ x. ereal (- g x) \<partial>M)"
  1738     using assms by (auto intro!: positive_integral_cong_AE)
  1738     using assms by (auto intro!: positive_integral_cong_AE)
  1739   with assms show ?thesis
  1739   with assms show ?thesis
  1740     by (auto simp: integrable_def)
  1740     by (auto simp: integrable_def)
  1741 qed
  1741 qed
  1742 
  1742 
  1744   "(\<And>x. x \<in> space M \<Longrightarrow> f x = g x) \<Longrightarrow> integrable M f \<longleftrightarrow> integrable M g"
  1744   "(\<And>x. x \<in> space M \<Longrightarrow> f x = g x) \<Longrightarrow> integrable M f \<longleftrightarrow> integrable M g"
  1745   by (simp cong: positive_integral_cong measurable_cong add: integrable_def)
  1745   by (simp cong: positive_integral_cong measurable_cong add: integrable_def)
  1746 
  1746 
  1747 lemma (in measure_space) integral_eq_positive_integral:
  1747 lemma (in measure_space) integral_eq_positive_integral:
  1748   assumes f: "\<And>x. 0 \<le> f x"
  1748   assumes f: "\<And>x. 0 \<le> f x"
  1749   shows "integral\<^isup>L M f = real (\<integral>\<^isup>+ x. extreal (f x) \<partial>M)"
  1749   shows "integral\<^isup>L M f = real (\<integral>\<^isup>+ x. ereal (f x) \<partial>M)"
  1750 proof -
  1750 proof -
  1751   { fix x have "max 0 (extreal (- f x)) = 0" using f[of x] by (simp split: split_max) }
  1751   { fix x have "max 0 (ereal (- f x)) = 0" using f[of x] by (simp split: split_max) }
  1752   then have "0 = (\<integral>\<^isup>+ x. max 0 (extreal (- f x)) \<partial>M)" by simp
  1752   then have "0 = (\<integral>\<^isup>+ x. max 0 (ereal (- f x)) \<partial>M)" by simp
  1753   also have "\<dots> = (\<integral>\<^isup>+ x. extreal (- f x) \<partial>M)" unfolding positive_integral_max_0 ..
  1753   also have "\<dots> = (\<integral>\<^isup>+ x. ereal (- f x) \<partial>M)" unfolding positive_integral_max_0 ..
  1754   finally show ?thesis
  1754   finally show ?thesis
  1755     unfolding lebesgue_integral_def by simp
  1755     unfolding lebesgue_integral_def by simp
  1756 qed
  1756 qed
  1757 
  1757 
  1758 lemma (in measure_space) integral_vimage:
  1758 lemma (in measure_space) integral_vimage:
  1760   assumes f: "f \<in> borel_measurable M'"
  1760   assumes f: "f \<in> borel_measurable M'"
  1761   shows "integral\<^isup>L M' f = (\<integral>x. f (T x) \<partial>M)"
  1761   shows "integral\<^isup>L M' f = (\<integral>x. f (T x) \<partial>M)"
  1762 proof -
  1762 proof -
  1763   interpret T: measure_space M' by (rule measure_space_vimage[OF T])
  1763   interpret T: measure_space M' by (rule measure_space_vimage[OF T])
  1764   from measurable_comp[OF measure_preservingD2[OF T(2)], of f borel]
  1764   from measurable_comp[OF measure_preservingD2[OF T(2)], of f borel]
  1765   have borel: "(\<lambda>x. extreal (f x)) \<in> borel_measurable M'" "(\<lambda>x. extreal (- f x)) \<in> borel_measurable M'"
  1765   have borel: "(\<lambda>x. ereal (f x)) \<in> borel_measurable M'" "(\<lambda>x. ereal (- f x)) \<in> borel_measurable M'"
  1766     and "(\<lambda>x. f (T x)) \<in> borel_measurable M"
  1766     and "(\<lambda>x. f (T x)) \<in> borel_measurable M"
  1767     using f by (auto simp: comp_def)
  1767     using f by (auto simp: comp_def)
  1768   then show ?thesis
  1768   then show ?thesis
  1769     using f unfolding lebesgue_integral_def integrable_def
  1769     using f unfolding lebesgue_integral_def integrable_def
  1770     by (auto simp: borel[THEN positive_integral_vimage[OF T]])
  1770     by (auto simp: borel[THEN positive_integral_vimage[OF T]])
  1775   assumes f: "integrable M' f"
  1775   assumes f: "integrable M' f"
  1776   shows "integrable M (\<lambda>x. f (T x))"
  1776   shows "integrable M (\<lambda>x. f (T x))"
  1777 proof -
  1777 proof -
  1778   interpret T: measure_space M' by (rule measure_space_vimage[OF T])
  1778   interpret T: measure_space M' by (rule measure_space_vimage[OF T])
  1779   from measurable_comp[OF measure_preservingD2[OF T(2)], of f borel]
  1779   from measurable_comp[OF measure_preservingD2[OF T(2)], of f borel]
  1780   have borel: "(\<lambda>x. extreal (f x)) \<in> borel_measurable M'" "(\<lambda>x. extreal (- f x)) \<in> borel_measurable M'"
  1780   have borel: "(\<lambda>x. ereal (f x)) \<in> borel_measurable M'" "(\<lambda>x. ereal (- f x)) \<in> borel_measurable M'"
  1781     and "(\<lambda>x. f (T x)) \<in> borel_measurable M"
  1781     and "(\<lambda>x. f (T x)) \<in> borel_measurable M"
  1782     using f by (auto simp: comp_def)
  1782     using f by (auto simp: comp_def)
  1783   then show ?thesis
  1783   then show ?thesis
  1784     using f unfolding lebesgue_integral_def integrable_def
  1784     using f unfolding lebesgue_integral_def integrable_def
  1785     by (auto simp: borel[THEN positive_integral_vimage[OF T]])
  1785     by (auto simp: borel[THEN positive_integral_vimage[OF T]])
  1793       (is "\<And>A. _ \<Longrightarrow> _ = ?d A")
  1793       (is "\<And>A. _ \<Longrightarrow> _ = ?d A")
  1794   shows "integral\<^isup>L N g = (\<integral> x. f x * g x \<partial>M)" (is ?integral)
  1794   shows "integral\<^isup>L N g = (\<integral> x. f x * g x \<partial>M)" (is ?integral)
  1795     and "integrable N g = integrable M (\<lambda>x. f x * g x)" (is ?integrable)
  1795     and "integrable N g = integrable M (\<lambda>x. f x * g x)" (is ?integrable)
  1796 proof -
  1796 proof -
  1797   from f have ms: "measure_space (M\<lparr>measure := ?d\<rparr>)"
  1797   from f have ms: "measure_space (M\<lparr>measure := ?d\<rparr>)"
  1798     by (intro measure_space_density[where u="\<lambda>x. extreal (f x)"]) auto
  1798     by (intro measure_space_density[where u="\<lambda>x. ereal (f x)"]) auto
  1799 
  1799 
  1800   from ms density N have "(\<integral>\<^isup>+ x. g x \<partial>N) =  (\<integral>\<^isup>+ x. max 0 (extreal (g x)) \<partial>M\<lparr>measure := ?d\<rparr>)"
  1800   from ms density N have "(\<integral>\<^isup>+ x. g x \<partial>N) =  (\<integral>\<^isup>+ x. max 0 (ereal (g x)) \<partial>M\<lparr>measure := ?d\<rparr>)"
  1801     unfolding positive_integral_max_0
  1801     unfolding positive_integral_max_0
  1802     by (intro measure_space.positive_integral_cong_measure) auto
  1802     by (intro measure_space.positive_integral_cong_measure) auto
  1803   also have "\<dots> = (\<integral>\<^isup>+ x. extreal (f x) * max 0 (extreal (g x)) \<partial>M)"
  1803   also have "\<dots> = (\<integral>\<^isup>+ x. ereal (f x) * max 0 (ereal (g x)) \<partial>M)"
  1804     using f g by (intro positive_integral_translated_density) auto
  1804     using f g by (intro positive_integral_translated_density) auto
  1805   also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (extreal (f x * g x)) \<partial>M)"
  1805   also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (ereal (f x * g x)) \<partial>M)"
  1806     using f by (intro positive_integral_cong_AE)
  1806     using f by (intro positive_integral_cong_AE)
  1807                (auto simp: extreal_max_0 zero_le_mult_iff split: split_max)
  1807                (auto simp: ereal_max_0 zero_le_mult_iff split: split_max)
  1808   finally have pos: "(\<integral>\<^isup>+ x. g x \<partial>N) = (\<integral>\<^isup>+ x. f x * g x \<partial>M)"
  1808   finally have pos: "(\<integral>\<^isup>+ x. g x \<partial>N) = (\<integral>\<^isup>+ x. f x * g x \<partial>M)"
  1809     by (simp add: positive_integral_max_0)
  1809     by (simp add: positive_integral_max_0)
  1810   
  1810   
  1811   from ms density N have "(\<integral>\<^isup>+ x. - (g x) \<partial>N) =  (\<integral>\<^isup>+ x. max 0 (extreal (- g x)) \<partial>M\<lparr>measure := ?d\<rparr>)"
  1811   from ms density N have "(\<integral>\<^isup>+ x. - (g x) \<partial>N) =  (\<integral>\<^isup>+ x. max 0 (ereal (- g x)) \<partial>M\<lparr>measure := ?d\<rparr>)"
  1812     unfolding positive_integral_max_0
  1812     unfolding positive_integral_max_0
  1813     by (intro measure_space.positive_integral_cong_measure) auto
  1813     by (intro measure_space.positive_integral_cong_measure) auto
  1814   also have "\<dots> = (\<integral>\<^isup>+ x. extreal (f x) * max 0 (extreal (- g x)) \<partial>M)"
  1814   also have "\<dots> = (\<integral>\<^isup>+ x. ereal (f x) * max 0 (ereal (- g x)) \<partial>M)"
  1815     using f g by (intro positive_integral_translated_density) auto
  1815     using f g by (intro positive_integral_translated_density) auto
  1816   also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (extreal (- f x * g x)) \<partial>M)"
  1816   also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (ereal (- f x * g x)) \<partial>M)"
  1817     using f by (intro positive_integral_cong_AE)
  1817     using f by (intro positive_integral_cong_AE)
  1818                (auto simp: extreal_max_0 mult_le_0_iff split: split_max)
  1818                (auto simp: ereal_max_0 mult_le_0_iff split: split_max)
  1819   finally have neg: "(\<integral>\<^isup>+ x. - g x \<partial>N) = (\<integral>\<^isup>+ x. - (f x * g x) \<partial>M)"
  1819   finally have neg: "(\<integral>\<^isup>+ x. - g x \<partial>N) = (\<integral>\<^isup>+ x. - (f x * g x) \<partial>M)"
  1820     by (simp add: positive_integral_max_0)
  1820     by (simp add: positive_integral_max_0)
  1821 
  1821 
  1822   have g_N: "g \<in> borel_measurable N"
  1822   have g_N: "g \<in> borel_measurable N"
  1823     using g N unfolding measurable_def by simp
  1823     using g N unfolding measurable_def by simp
  1844 lemma (in measure_space) integral_of_positive_diff:
  1844 lemma (in measure_space) integral_of_positive_diff:
  1845   assumes integrable: "integrable M u" "integrable M v"
  1845   assumes integrable: "integrable M u" "integrable M v"
  1846   and f_def: "\<And>x. f x = u x - v x" and pos: "\<And>x. 0 \<le> u x" "\<And>x. 0 \<le> v x"
  1846   and f_def: "\<And>x. f x = u x - v x" and pos: "\<And>x. 0 \<le> u x" "\<And>x. 0 \<le> v x"
  1847   shows "integrable M f" and "integral\<^isup>L M f = integral\<^isup>L M u - integral\<^isup>L M v"
  1847   shows "integrable M f" and "integral\<^isup>L M f = integral\<^isup>L M u - integral\<^isup>L M v"
  1848 proof -
  1848 proof -
  1849   let "?f x" = "max 0 (extreal (f x))"
  1849   let "?f x" = "max 0 (ereal (f x))"
  1850   let "?mf x" = "max 0 (extreal (- f x))"
  1850   let "?mf x" = "max 0 (ereal (- f x))"
  1851   let "?u x" = "max 0 (extreal (u x))"
  1851   let "?u x" = "max 0 (ereal (u x))"
  1852   let "?v x" = "max 0 (extreal (v x))"
  1852   let "?v x" = "max 0 (ereal (v x))"
  1853 
  1853 
  1854   from borel_measurable_diff[of u v] integrable
  1854   from borel_measurable_diff[of u v] integrable
  1855   have f_borel: "?f \<in> borel_measurable M" and
  1855   have f_borel: "?f \<in> borel_measurable M" and
  1856     mf_borel: "?mf \<in> borel_measurable M" and
  1856     mf_borel: "?mf \<in> borel_measurable M" and
  1857     v_borel: "?v \<in> borel_measurable M" and
  1857     v_borel: "?v \<in> borel_measurable M" and
  1858     u_borel: "?u \<in> borel_measurable M" and
  1858     u_borel: "?u \<in> borel_measurable M" and
  1859     "f \<in> borel_measurable M"
  1859     "f \<in> borel_measurable M"
  1860     by (auto simp: f_def[symmetric] integrable_def)
  1860     by (auto simp: f_def[symmetric] integrable_def)
  1861 
  1861 
  1862   have "(\<integral>\<^isup>+ x. extreal (u x - v x) \<partial>M) \<le> integral\<^isup>P M ?u"
  1862   have "(\<integral>\<^isup>+ x. ereal (u x - v x) \<partial>M) \<le> integral\<^isup>P M ?u"
  1863     using pos by (auto intro!: positive_integral_mono simp: positive_integral_max_0)
  1863     using pos by (auto intro!: positive_integral_mono simp: positive_integral_max_0)
  1864   moreover have "(\<integral>\<^isup>+ x. extreal (v x - u x) \<partial>M) \<le> integral\<^isup>P M ?v"
  1864   moreover have "(\<integral>\<^isup>+ x. ereal (v x - u x) \<partial>M) \<le> integral\<^isup>P M ?v"
  1865     using pos by (auto intro!: positive_integral_mono simp: positive_integral_max_0)
  1865     using pos by (auto intro!: positive_integral_mono simp: positive_integral_max_0)
  1866   ultimately show f: "integrable M f"
  1866   ultimately show f: "integrable M f"
  1867     using `integrable M u` `integrable M v` `f \<in> borel_measurable M`
  1867     using `integrable M u` `integrable M v` `f \<in> borel_measurable M`
  1868     by (auto simp: integrable_def f_def positive_integral_max_0)
  1868     by (auto simp: integrable_def f_def positive_integral_max_0)
  1869 
  1869 
  1884 lemma (in measure_space) integral_linear:
  1884 lemma (in measure_space) integral_linear:
  1885   assumes "integrable M f" "integrable M g" and "0 \<le> a"
  1885   assumes "integrable M f" "integrable M g" and "0 \<le> a"
  1886   shows "integrable M (\<lambda>t. a * f t + g t)"
  1886   shows "integrable M (\<lambda>t. a * f t + g t)"
  1887   and "(\<integral> t. a * f t + g t \<partial>M) = a * integral\<^isup>L M f + integral\<^isup>L M g" (is ?EQ)
  1887   and "(\<integral> t. a * f t + g t \<partial>M) = a * integral\<^isup>L M f + integral\<^isup>L M g" (is ?EQ)
  1888 proof -
  1888 proof -
  1889   let "?f x" = "max 0 (extreal (f x))"
  1889   let "?f x" = "max 0 (ereal (f x))"
  1890   let "?g x" = "max 0 (extreal (g x))"
  1890   let "?g x" = "max 0 (ereal (g x))"
  1891   let "?mf x" = "max 0 (extreal (- f x))"
  1891   let "?mf x" = "max 0 (ereal (- f x))"
  1892   let "?mg x" = "max 0 (extreal (- g x))"
  1892   let "?mg x" = "max 0 (ereal (- g x))"
  1893   let "?p t" = "max 0 (a * f t) + max 0 (g t)"
  1893   let "?p t" = "max 0 (a * f t) + max 0 (g t)"
  1894   let "?n t" = "max 0 (- (a * f t)) + max 0 (- g t)"
  1894   let "?n t" = "max 0 (- (a * f t)) + max 0 (- g t)"
  1895 
  1895 
  1896   from assms have linear:
  1896   from assms have linear:
  1897     "(\<integral>\<^isup>+ x. extreal a * ?f x + ?g x \<partial>M) = extreal a * integral\<^isup>P M ?f + integral\<^isup>P M ?g"
  1897     "(\<integral>\<^isup>+ x. ereal a * ?f x + ?g x \<partial>M) = ereal a * integral\<^isup>P M ?f + integral\<^isup>P M ?g"
  1898     "(\<integral>\<^isup>+ x. extreal a * ?mf x + ?mg x \<partial>M) = extreal a * integral\<^isup>P M ?mf + integral\<^isup>P M ?mg"
  1898     "(\<integral>\<^isup>+ x. ereal a * ?mf x + ?mg x \<partial>M) = ereal a * integral\<^isup>P M ?mf + integral\<^isup>P M ?mg"
  1899     by (auto intro!: positive_integral_linear simp: integrable_def)
  1899     by (auto intro!: positive_integral_linear simp: integrable_def)
  1900 
  1900 
  1901   have *: "(\<integral>\<^isup>+x. extreal (- ?p x) \<partial>M) = 0" "(\<integral>\<^isup>+x. extreal (- ?n x) \<partial>M) = 0"
  1901   have *: "(\<integral>\<^isup>+x. ereal (- ?p x) \<partial>M) = 0" "(\<integral>\<^isup>+x. ereal (- ?n x) \<partial>M) = 0"
  1902     using `0 \<le> a` assms by (auto simp: positive_integral_0_iff_AE integrable_def)
  1902     using `0 \<le> a` assms by (auto simp: positive_integral_0_iff_AE integrable_def)
  1903   have **: "\<And>x. extreal a * ?f x + ?g x = max 0 (extreal (?p x))"
  1903   have **: "\<And>x. ereal a * ?f x + ?g x = max 0 (ereal (?p x))"
  1904            "\<And>x. extreal a * ?mf x + ?mg x = max 0 (extreal (?n x))"
  1904            "\<And>x. ereal a * ?mf x + ?mg x = max 0 (ereal (?n x))"
  1905     using `0 \<le> a` by (auto split: split_max simp: zero_le_mult_iff mult_le_0_iff)
  1905     using `0 \<le> a` by (auto split: split_max simp: zero_le_mult_iff mult_le_0_iff)
  1906 
  1906 
  1907   have "integrable M ?p" "integrable M ?n"
  1907   have "integrable M ?p" "integrable M ?n"
  1908       "\<And>t. a * f t + g t = ?p t - ?n t" "\<And>t. 0 \<le> ?p t" "\<And>t. 0 \<le> ?n t"
  1908       "\<And>t. a * f t + g t = ?p t - ?n t" "\<And>t. 0 \<le> ?p t" "\<And>t. 0 \<le> ?n t"
  1909     using linear assms unfolding integrable_def ** *
  1909     using linear assms unfolding integrable_def ** *
  1956 lemma (in measure_space) integral_mono_AE:
  1956 lemma (in measure_space) integral_mono_AE:
  1957   assumes fg: "integrable M f" "integrable M g"
  1957   assumes fg: "integrable M f" "integrable M g"
  1958   and mono: "AE t. f t \<le> g t"
  1958   and mono: "AE t. f t \<le> g t"
  1959   shows "integral\<^isup>L M f \<le> integral\<^isup>L M g"
  1959   shows "integral\<^isup>L M f \<le> integral\<^isup>L M g"
  1960 proof -
  1960 proof -
  1961   have "AE x. extreal (f x) \<le> extreal (g x)"
  1961   have "AE x. ereal (f x) \<le> ereal (g x)"
  1962     using mono by auto
  1962     using mono by auto
  1963   moreover have "AE x. extreal (- g x) \<le> extreal (- f x)"
  1963   moreover have "AE x. ereal (- g x) \<le> ereal (- f x)"
  1964     using mono by auto
  1964     using mono by auto
  1965   ultimately show ?thesis using fg
  1965   ultimately show ?thesis using fg
  1966     by (auto intro!: add_mono positive_integral_mono_AE real_of_extreal_positive_mono
  1966     by (auto intro!: add_mono positive_integral_mono_AE real_of_ereal_positive_mono
  1967              simp: positive_integral_positive lebesgue_integral_def diff_minus)
  1967              simp: positive_integral_positive lebesgue_integral_def diff_minus)
  1968 qed
  1968 qed
  1969 
  1969 
  1970 lemma (in measure_space) integral_mono:
  1970 lemma (in measure_space) integral_mono:
  1971   assumes "integrable M f" "integrable M g" "\<And>t. t \<in> space M \<Longrightarrow> f t \<le> g t"
  1971   assumes "integrable M f" "integrable M g" "\<And>t. t \<in> space M \<Longrightarrow> f t \<le> g t"
  1984   assumes "A \<in> sets M" and "\<mu> A \<noteq> \<infinity>"
  1984   assumes "A \<in> sets M" and "\<mu> A \<noteq> \<infinity>"
  1985   shows "integral\<^isup>L M (indicator A) = real (\<mu> A)" (is ?int)
  1985   shows "integral\<^isup>L M (indicator A) = real (\<mu> A)" (is ?int)
  1986   and "integrable M (indicator A)" (is ?able)
  1986   and "integrable M (indicator A)" (is ?able)
  1987 proof -
  1987 proof -
  1988   from `A \<in> sets M` have *:
  1988   from `A \<in> sets M` have *:
  1989     "\<And>x. extreal (indicator A x) = indicator A x"
  1989     "\<And>x. ereal (indicator A x) = indicator A x"
  1990     "(\<integral>\<^isup>+x. extreal (- indicator A x) \<partial>M) = 0"
  1990     "(\<integral>\<^isup>+x. ereal (- indicator A x) \<partial>M) = 0"
  1991     by (auto split: split_indicator simp: positive_integral_0_iff_AE one_extreal_def)
  1991     by (auto split: split_indicator simp: positive_integral_0_iff_AE one_ereal_def)
  1992   show ?int ?able
  1992   show ?int ?able
  1993     using assms unfolding lebesgue_integral_def integrable_def
  1993     using assms unfolding lebesgue_integral_def integrable_def
  1994     by (auto simp: * positive_integral_indicator borel_measurable_indicator)
  1994     by (auto simp: * positive_integral_indicator borel_measurable_indicator)
  1995 qed
  1995 qed
  1996 
  1996 
  2025 
  2025 
  2026 lemma (in measure_space) integrable_abs:
  2026 lemma (in measure_space) integrable_abs:
  2027   assumes "integrable M f"
  2027   assumes "integrable M f"
  2028   shows "integrable M (\<lambda> x. \<bar>f x\<bar>)"
  2028   shows "integrable M (\<lambda> x. \<bar>f x\<bar>)"
  2029 proof -
  2029 proof -
  2030   from assms have *: "(\<integral>\<^isup>+x. extreal (- \<bar>f x\<bar>)\<partial>M) = 0"
  2030   from assms have *: "(\<integral>\<^isup>+x. ereal (- \<bar>f x\<bar>)\<partial>M) = 0"
  2031     "\<And>x. extreal \<bar>f x\<bar> = max 0 (extreal (f x)) + max 0 (extreal (- f x))"
  2031     "\<And>x. ereal \<bar>f x\<bar> = max 0 (ereal (f x)) + max 0 (ereal (- f x))"
  2032     by (auto simp: integrable_def positive_integral_0_iff_AE split: split_max)
  2032     by (auto simp: integrable_def positive_integral_0_iff_AE split: split_max)
  2033   with assms show ?thesis
  2033   with assms show ?thesis
  2034     by (simp add: positive_integral_add positive_integral_max_0 integrable_def)
  2034     by (simp add: positive_integral_add positive_integral_max_0 integrable_def)
  2035 qed
  2035 qed
  2036 
  2036 
  2039   and N: "sets N \<subseteq> sets M" "space N = space M" "\<And>A. A \<in> sets N \<Longrightarrow> measure N A = \<mu> A" and sa: "sigma_algebra N"
  2039   and N: "sets N \<subseteq> sets M" "space N = space M" "\<And>A. A \<in> sets N \<Longrightarrow> measure N A = \<mu> A" and sa: "sigma_algebra N"
  2040   shows "integrable N f \<longleftrightarrow> integrable M f" (is ?P)
  2040   shows "integrable N f \<longleftrightarrow> integrable M f" (is ?P)
  2041     and "integral\<^isup>L N f = integral\<^isup>L M f" (is ?I)
  2041     and "integral\<^isup>L N f = integral\<^isup>L M f" (is ?I)
  2042 proof -
  2042 proof -
  2043   interpret N: measure_space N using measure_space_subalgebra[OF sa N] .
  2043   interpret N: measure_space N using measure_space_subalgebra[OF sa N] .
  2044   have "(\<integral>\<^isup>+ x. max 0 (extreal (f x)) \<partial>N) = (\<integral>\<^isup>+ x. max 0 (extreal (f x)) \<partial>M)"
  2044   have "(\<integral>\<^isup>+ x. max 0 (ereal (f x)) \<partial>N) = (\<integral>\<^isup>+ x. max 0 (ereal (f x)) \<partial>M)"
  2045        "(\<integral>\<^isup>+ x. max 0 (extreal (- f x)) \<partial>N) = (\<integral>\<^isup>+ x. max 0 (extreal (- f x)) \<partial>M)"
  2045        "(\<integral>\<^isup>+ x. max 0 (ereal (- f x)) \<partial>N) = (\<integral>\<^isup>+ x. max 0 (ereal (- f x)) \<partial>M)"
  2046     using borel by (auto intro!: positive_integral_subalgebra N sa)
  2046     using borel by (auto intro!: positive_integral_subalgebra N sa)
  2047   moreover have "f \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable N"
  2047   moreover have "f \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable N"
  2048     using assms unfolding measurable_def by auto
  2048     using assms unfolding measurable_def by auto
  2049   ultimately show ?P ?I
  2049   ultimately show ?P ?I
  2050     by (auto simp: integrable_def lebesgue_integral_def positive_integral_max_0)
  2050     by (auto simp: integrable_def lebesgue_integral_def positive_integral_max_0)
  2055   and f: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
  2055   and f: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
  2056     "\<And>x. x \<in> space M \<Longrightarrow> \<bar>g x\<bar> \<le> f x"
  2056     "\<And>x. x \<in> space M \<Longrightarrow> \<bar>g x\<bar> \<le> f x"
  2057   assumes borel: "g \<in> borel_measurable M"
  2057   assumes borel: "g \<in> borel_measurable M"
  2058   shows "integrable M g"
  2058   shows "integrable M g"
  2059 proof -
  2059 proof -
  2060   have "(\<integral>\<^isup>+ x. extreal (g x) \<partial>M) \<le> (\<integral>\<^isup>+ x. extreal \<bar>g x\<bar> \<partial>M)"
  2060   have "(\<integral>\<^isup>+ x. ereal (g x) \<partial>M) \<le> (\<integral>\<^isup>+ x. ereal \<bar>g x\<bar> \<partial>M)"
  2061     by (auto intro!: positive_integral_mono)
  2061     by (auto intro!: positive_integral_mono)
  2062   also have "\<dots> \<le> (\<integral>\<^isup>+ x. extreal (f x) \<partial>M)"
  2062   also have "\<dots> \<le> (\<integral>\<^isup>+ x. ereal (f x) \<partial>M)"
  2063     using f by (auto intro!: positive_integral_mono)
  2063     using f by (auto intro!: positive_integral_mono)
  2064   also have "\<dots> < \<infinity>"
  2064   also have "\<dots> < \<infinity>"
  2065     using `integrable M f` unfolding integrable_def by auto
  2065     using `integrable M f` unfolding integrable_def by auto
  2066   finally have pos: "(\<integral>\<^isup>+ x. extreal (g x) \<partial>M) < \<infinity>" .
  2066   finally have pos: "(\<integral>\<^isup>+ x. ereal (g x) \<partial>M) < \<infinity>" .
  2067 
  2067 
  2068   have "(\<integral>\<^isup>+ x. extreal (- g x) \<partial>M) \<le> (\<integral>\<^isup>+ x. extreal (\<bar>g x\<bar>) \<partial>M)"
  2068   have "(\<integral>\<^isup>+ x. ereal (- g x) \<partial>M) \<le> (\<integral>\<^isup>+ x. ereal (\<bar>g x\<bar>) \<partial>M)"
  2069     by (auto intro!: positive_integral_mono)
  2069     by (auto intro!: positive_integral_mono)
  2070   also have "\<dots> \<le> (\<integral>\<^isup>+ x. extreal (f x) \<partial>M)"
  2070   also have "\<dots> \<le> (\<integral>\<^isup>+ x. ereal (f x) \<partial>M)"
  2071     using f by (auto intro!: positive_integral_mono)
  2071     using f by (auto intro!: positive_integral_mono)
  2072   also have "\<dots> < \<infinity>"
  2072   also have "\<dots> < \<infinity>"
  2073     using `integrable M f` unfolding integrable_def by auto
  2073     using `integrable M f` unfolding integrable_def by auto
  2074   finally have neg: "(\<integral>\<^isup>+ x. extreal (- g x) \<partial>M) < \<infinity>" .
  2074   finally have neg: "(\<integral>\<^isup>+ x. ereal (- g x) \<partial>M) < \<infinity>" .
  2075 
  2075 
  2076   from neg pos borel show ?thesis
  2076   from neg pos borel show ?thesis
  2077     unfolding integrable_def by auto
  2077     unfolding integrable_def by auto
  2078 qed
  2078 qed
  2079 
  2079 
  2141   { fix x have "0 \<le> u x"
  2141   { fix x have "0 \<le> u x"
  2142       using mono pos[of 0 x] incseq_le[OF _ lim, of x 0]
  2142       using mono pos[of 0 x] incseq_le[OF _ lim, of x 0]
  2143       by (simp add: mono_def incseq_def) }
  2143       by (simp add: mono_def incseq_def) }
  2144   note pos_u = this
  2144   note pos_u = this
  2145 
  2145 
  2146   have SUP_F: "\<And>x. (SUP n. extreal (f n x)) = extreal (u x)"
  2146   have SUP_F: "\<And>x. (SUP n. ereal (f n x)) = ereal (u x)"
  2147     unfolding SUP_eq_LIMSEQ[OF mono] by (rule lim)
  2147     unfolding SUP_eq_LIMSEQ[OF mono] by (rule lim)
  2148 
  2148 
  2149   have borel_f: "\<And>i. (\<lambda>x. extreal (f i x)) \<in> borel_measurable M"
  2149   have borel_f: "\<And>i. (\<lambda>x. ereal (f i x)) \<in> borel_measurable M"
  2150     using i unfolding integrable_def by auto
  2150     using i unfolding integrable_def by auto
  2151   hence "(\<lambda>x. SUP i. extreal (f i x)) \<in> borel_measurable M"
  2151   hence "(\<lambda>x. SUP i. ereal (f i x)) \<in> borel_measurable M"
  2152     by auto
  2152     by auto
  2153   hence borel_u: "u \<in> borel_measurable M"
  2153   hence borel_u: "u \<in> borel_measurable M"
  2154     by (auto simp: borel_measurable_extreal_iff SUP_F)
  2154     by (auto simp: borel_measurable_ereal_iff SUP_F)
  2155 
  2155 
  2156   hence [simp]: "\<And>i. (\<integral>\<^isup>+x. extreal (- f i x) \<partial>M) = 0" "(\<integral>\<^isup>+x. extreal (- u x) \<partial>M) = 0"
  2156   hence [simp]: "\<And>i. (\<integral>\<^isup>+x. ereal (- f i x) \<partial>M) = 0" "(\<integral>\<^isup>+x. ereal (- u x) \<partial>M) = 0"
  2157     using i borel_u pos pos_u by (auto simp: positive_integral_0_iff_AE integrable_def)
  2157     using i borel_u pos pos_u by (auto simp: positive_integral_0_iff_AE integrable_def)
  2158 
  2158 
  2159   have integral_eq: "\<And>n. (\<integral>\<^isup>+ x. extreal (f n x) \<partial>M) = extreal (integral\<^isup>L M (f n))"
  2159   have integral_eq: "\<And>n. (\<integral>\<^isup>+ x. ereal (f n x) \<partial>M) = ereal (integral\<^isup>L M (f n))"
  2160     using i positive_integral_positive by (auto simp: extreal_real lebesgue_integral_def integrable_def)
  2160     using i positive_integral_positive by (auto simp: ereal_real lebesgue_integral_def integrable_def)
  2161 
  2161 
  2162   have pos_integral: "\<And>n. 0 \<le> integral\<^isup>L M (f n)"
  2162   have pos_integral: "\<And>n. 0 \<le> integral\<^isup>L M (f n)"
  2163     using pos i by (auto simp: integral_positive)
  2163     using pos i by (auto simp: integral_positive)
  2164   hence "0 \<le> x"
  2164   hence "0 \<le> x"
  2165     using LIMSEQ_le_const[OF ilim, of 0] by auto
  2165     using LIMSEQ_le_const[OF ilim, of 0] by auto
  2166 
  2166 
  2167   from mono pos i have pI: "(\<integral>\<^isup>+ x. extreal (u x) \<partial>M) = (SUP n. (\<integral>\<^isup>+ x. extreal (f n x) \<partial>M))"
  2167   from mono pos i have pI: "(\<integral>\<^isup>+ x. ereal (u x) \<partial>M) = (SUP n. (\<integral>\<^isup>+ x. ereal (f n x) \<partial>M))"
  2168     by (auto intro!: positive_integral_monotone_convergence_SUP
  2168     by (auto intro!: positive_integral_monotone_convergence_SUP
  2169       simp: integrable_def incseq_mono incseq_Suc_iff le_fun_def SUP_F[symmetric])
  2169       simp: integrable_def incseq_mono incseq_Suc_iff le_fun_def SUP_F[symmetric])
  2170   also have "\<dots> = extreal x" unfolding integral_eq
  2170   also have "\<dots> = ereal x" unfolding integral_eq
  2171   proof (rule SUP_eq_LIMSEQ[THEN iffD2])
  2171   proof (rule SUP_eq_LIMSEQ[THEN iffD2])
  2172     show "mono (\<lambda>n. integral\<^isup>L M (f n))"
  2172     show "mono (\<lambda>n. integral\<^isup>L M (f n))"
  2173       using mono i by (auto simp: mono_def intro!: integral_mono)
  2173       using mono i by (auto simp: mono_def intro!: integral_mono)
  2174     show "(\<lambda>n. integral\<^isup>L M (f n)) ----> x" using ilim .
  2174     show "(\<lambda>n. integral\<^isup>L M (f n)) ----> x" using ilim .
  2175   qed
  2175   qed
  2203 
  2203 
  2204 lemma (in measure_space) integral_0_iff:
  2204 lemma (in measure_space) integral_0_iff:
  2205   assumes "integrable M f"
  2205   assumes "integrable M f"
  2206   shows "(\<integral>x. \<bar>f x\<bar> \<partial>M) = 0 \<longleftrightarrow> \<mu> {x\<in>space M. f x \<noteq> 0} = 0"
  2206   shows "(\<integral>x. \<bar>f x\<bar> \<partial>M) = 0 \<longleftrightarrow> \<mu> {x\<in>space M. f x \<noteq> 0} = 0"
  2207 proof -
  2207 proof -
  2208   have *: "(\<integral>\<^isup>+x. extreal (- \<bar>f x\<bar>) \<partial>M) = 0"
  2208   have *: "(\<integral>\<^isup>+x. ereal (- \<bar>f x\<bar>) \<partial>M) = 0"
  2209     using assms by (auto simp: positive_integral_0_iff_AE integrable_def)
  2209     using assms by (auto simp: positive_integral_0_iff_AE integrable_def)
  2210   have "integrable M (\<lambda>x. \<bar>f x\<bar>)" using assms by (rule integrable_abs)
  2210   have "integrable M (\<lambda>x. \<bar>f x\<bar>)" using assms by (rule integrable_abs)
  2211   hence "(\<lambda>x. extreal (\<bar>f x\<bar>)) \<in> borel_measurable M"
  2211   hence "(\<lambda>x. ereal (\<bar>f x\<bar>)) \<in> borel_measurable M"
  2212     "(\<integral>\<^isup>+ x. extreal \<bar>f x\<bar> \<partial>M) \<noteq> \<infinity>" unfolding integrable_def by auto
  2212     "(\<integral>\<^isup>+ x. ereal \<bar>f x\<bar> \<partial>M) \<noteq> \<infinity>" unfolding integrable_def by auto
  2213   from positive_integral_0_iff[OF this(1)] this(2)
  2213   from positive_integral_0_iff[OF this(1)] this(2)
  2214   show ?thesis unfolding lebesgue_integral_def *
  2214   show ?thesis unfolding lebesgue_integral_def *
  2215     using positive_integral_positive[of "\<lambda>x. extreal \<bar>f x\<bar>"]
  2215     using positive_integral_positive[of "\<lambda>x. ereal \<bar>f x\<bar>"]
  2216     by (auto simp add: real_of_extreal_eq_0)
  2216     by (auto simp add: real_of_ereal_eq_0)
  2217 qed
  2217 qed
  2218 
  2218 
  2219 lemma (in measure_space) positive_integral_PInf:
  2219 lemma (in measure_space) positive_integral_PInf:
  2220   assumes f: "f \<in> borel_measurable M"
  2220   assumes f: "f \<in> borel_measurable M"
  2221   and not_Inf: "integral\<^isup>P M f \<noteq> \<infinity>"
  2221   and not_Inf: "integral\<^isup>P M f \<noteq> \<infinity>"
  2253 qed
  2253 qed
  2254 
  2254 
  2255 lemma (in measure_space) integral_real:
  2255 lemma (in measure_space) integral_real:
  2256   "AE x. \<bar>f x\<bar> \<noteq> \<infinity> \<Longrightarrow> (\<integral>x. real (f x) \<partial>M) = real (integral\<^isup>P M f) - real (integral\<^isup>P M (\<lambda>x. - f x))"
  2256   "AE x. \<bar>f x\<bar> \<noteq> \<infinity> \<Longrightarrow> (\<integral>x. real (f x) \<partial>M) = real (integral\<^isup>P M f) - real (integral\<^isup>P M (\<lambda>x. - f x))"
  2257   using assms unfolding lebesgue_integral_def
  2257   using assms unfolding lebesgue_integral_def
  2258   by (subst (1 2) positive_integral_cong_AE) (auto simp add: extreal_real)
  2258   by (subst (1 2) positive_integral_cong_AE) (auto simp add: ereal_real)
  2259 
  2259 
  2260 lemma (in finite_measure) lebesgue_integral_const[simp]:
  2260 lemma (in finite_measure) lebesgue_integral_const[simp]:
  2261   shows "integrable M (\<lambda>x. a)"
  2261   shows "integrable M (\<lambda>x. a)"
  2262   and  "(\<integral>x. a \<partial>M) = a * \<mu>' (space M)"
  2262   and  "(\<integral>x. a \<partial>M) = a * \<mu>' (space M)"
  2263 proof -
  2263 proof -
  2264   { fix a :: real assume "0 \<le> a"
  2264   { fix a :: real assume "0 \<le> a"
  2265     then have "(\<integral>\<^isup>+ x. extreal a \<partial>M) = extreal a * \<mu> (space M)"
  2265     then have "(\<integral>\<^isup>+ x. ereal a \<partial>M) = ereal a * \<mu> (space M)"
  2266       by (subst positive_integral_const) auto
  2266       by (subst positive_integral_const) auto
  2267     moreover
  2267     moreover
  2268     from `0 \<le> a` have "(\<integral>\<^isup>+ x. extreal (-a) \<partial>M) = 0"
  2268     from `0 \<le> a` have "(\<integral>\<^isup>+ x. ereal (-a) \<partial>M) = 0"
  2269       by (subst positive_integral_0_iff_AE) auto
  2269       by (subst positive_integral_0_iff_AE) auto
  2270     ultimately have "integrable M (\<lambda>x. a)" by (auto simp: integrable_def) }
  2270     ultimately have "integrable M (\<lambda>x. a)" by (auto simp: integrable_def) }
  2271   note * = this
  2271   note * = this
  2272   show "integrable M (\<lambda>x. a)"
  2272   show "integrable M (\<lambda>x. a)"
  2273   proof cases
  2273   proof cases
  2280   show "(\<integral>x. a \<partial>M) = a * \<mu>' (space M)"
  2280   show "(\<integral>x. a \<partial>M) = a * \<mu>' (space M)"
  2281     by (simp add: \<mu>'_def lebesgue_integral_def positive_integral_const_If)
  2281     by (simp add: \<mu>'_def lebesgue_integral_def positive_integral_const_If)
  2282 qed
  2282 qed
  2283 
  2283 
  2284 lemma indicator_less[simp]:
  2284 lemma indicator_less[simp]:
  2285   "indicator A x \<le> (indicator B x::extreal) \<longleftrightarrow> (x \<in> A \<longrightarrow> x \<in> B)"
  2285   "indicator A x \<le> (indicator B x::ereal) \<longleftrightarrow> (x \<in> A \<longrightarrow> x \<in> B)"
  2286   by (simp add: indicator_def not_le)
  2286   by (simp add: indicator_def not_le)
  2287 
  2287 
  2288 lemma (in finite_measure) integral_less_AE:
  2288 lemma (in finite_measure) integral_less_AE:
  2289   assumes int: "integrable M X" "integrable M Y"
  2289   assumes int: "integrable M X" "integrable M Y"
  2290   assumes A: "\<mu> A \<noteq> 0" "A \<in> sets M" "AE x. x \<in> A \<longrightarrow> X x \<noteq> Y x"
  2290   assumes A: "\<mu> A \<noteq> 0" "A \<in> sets M" "AE x. x \<in> A \<longrightarrow> X x \<noteq> Y x"
  2363     also have "\<dots> \<le> w x + w x"
  2363     also have "\<dots> \<le> w x + w x"
  2364       by (rule add_mono[OF bound[OF x] u'_bound[OF x]])
  2364       by (rule add_mono[OF bound[OF x] u'_bound[OF x]])
  2365     finally have "\<bar>u j x - u' x\<bar> \<le> 2 * w x" by simp }
  2365     finally have "\<bar>u j x - u' x\<bar> \<le> 2 * w x" by simp }
  2366   note diff_less_2w = this
  2366   note diff_less_2w = this
  2367 
  2367 
  2368   have PI_diff: "\<And>n. (\<integral>\<^isup>+ x. extreal (?diff n x) \<partial>M) =
  2368   have PI_diff: "\<And>n. (\<integral>\<^isup>+ x. ereal (?diff n x) \<partial>M) =
  2369     (\<integral>\<^isup>+ x. extreal (2 * w x) \<partial>M) - (\<integral>\<^isup>+ x. extreal \<bar>u n x - u' x\<bar> \<partial>M)"
  2369     (\<integral>\<^isup>+ x. ereal (2 * w x) \<partial>M) - (\<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)"
  2370     using diff w diff_less_2w w_pos
  2370     using diff w diff_less_2w w_pos
  2371     by (subst positive_integral_diff[symmetric])
  2371     by (subst positive_integral_diff[symmetric])
  2372        (auto simp: integrable_def intro!: positive_integral_cong)
  2372        (auto simp: integrable_def intro!: positive_integral_cong)
  2373 
  2373 
  2374   have "integrable M (\<lambda>x. 2 * w x)"
  2374   have "integrable M (\<lambda>x. 2 * w x)"
  2375     using w by (auto intro: integral_cmult)
  2375     using w by (auto intro: integral_cmult)
  2376   hence I2w_fin: "(\<integral>\<^isup>+ x. extreal (2 * w x) \<partial>M) \<noteq> \<infinity>" and
  2376   hence I2w_fin: "(\<integral>\<^isup>+ x. ereal (2 * w x) \<partial>M) \<noteq> \<infinity>" and
  2377     borel_2w: "(\<lambda>x. extreal (2 * w x)) \<in> borel_measurable M"
  2377     borel_2w: "(\<lambda>x. ereal (2 * w x)) \<in> borel_measurable M"
  2378     unfolding integrable_def by auto
  2378     unfolding integrable_def by auto
  2379 
  2379 
  2380   have "limsup (\<lambda>n. \<integral>\<^isup>+ x. extreal \<bar>u n x - u' x\<bar> \<partial>M) = 0" (is "limsup ?f = 0")
  2380   have "limsup (\<lambda>n. \<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M) = 0" (is "limsup ?f = 0")
  2381   proof cases
  2381   proof cases
  2382     assume eq_0: "(\<integral>\<^isup>+ x. max 0 (extreal (2 * w x)) \<partial>M) = 0" (is "?wx = 0")
  2382     assume eq_0: "(\<integral>\<^isup>+ x. max 0 (ereal (2 * w x)) \<partial>M) = 0" (is "?wx = 0")
  2383     { fix n
  2383     { fix n
  2384       have "?f n \<le> ?wx" (is "integral\<^isup>P M ?f' \<le> _")
  2384       have "?f n \<le> ?wx" (is "integral\<^isup>P M ?f' \<le> _")
  2385         using diff_less_2w[of _ n] unfolding positive_integral_max_0
  2385         using diff_less_2w[of _ n] unfolding positive_integral_max_0
  2386         by (intro positive_integral_mono) auto
  2386         by (intro positive_integral_mono) auto
  2387       then have "?f n = 0"
  2387       then have "?f n = 0"
  2388         using positive_integral_positive[of ?f'] eq_0 by auto }
  2388         using positive_integral_positive[of ?f'] eq_0 by auto }
  2389     then show ?thesis by (simp add: Limsup_const)
  2389     then show ?thesis by (simp add: Limsup_const)
  2390   next
  2390   next
  2391     assume neq_0: "(\<integral>\<^isup>+ x. max 0 (extreal (2 * w x)) \<partial>M) \<noteq> 0" (is "?wx \<noteq> 0")
  2391     assume neq_0: "(\<integral>\<^isup>+ x. max 0 (ereal (2 * w x)) \<partial>M) \<noteq> 0" (is "?wx \<noteq> 0")
  2392     have "0 = limsup (\<lambda>n. 0 :: extreal)" by (simp add: Limsup_const)
  2392     have "0 = limsup (\<lambda>n. 0 :: ereal)" by (simp add: Limsup_const)
  2393     also have "\<dots> \<le> limsup (\<lambda>n. \<integral>\<^isup>+ x. extreal \<bar>u n x - u' x\<bar> \<partial>M)"
  2393     also have "\<dots> \<le> limsup (\<lambda>n. \<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)"
  2394       by (intro limsup_mono positive_integral_positive)
  2394       by (intro limsup_mono positive_integral_positive)
  2395     finally have pos: "0 \<le> limsup (\<lambda>n. \<integral>\<^isup>+ x. extreal \<bar>u n x - u' x\<bar> \<partial>M)" .
  2395     finally have pos: "0 \<le> limsup (\<lambda>n. \<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)" .
  2396     have "?wx = (\<integral>\<^isup>+ x. liminf (\<lambda>n. max 0 (extreal (?diff n x))) \<partial>M)"
  2396     have "?wx = (\<integral>\<^isup>+ x. liminf (\<lambda>n. max 0 (ereal (?diff n x))) \<partial>M)"
  2397     proof (rule positive_integral_cong)
  2397     proof (rule positive_integral_cong)
  2398       fix x assume x: "x \<in> space M"
  2398       fix x assume x: "x \<in> space M"
  2399       show "max 0 (extreal (2 * w x)) = liminf (\<lambda>n. max 0 (extreal (?diff n x)))"
  2399       show "max 0 (ereal (2 * w x)) = liminf (\<lambda>n. max 0 (ereal (?diff n x)))"
  2400         unfolding extreal_max_0
  2400         unfolding ereal_max_0
  2401       proof (rule lim_imp_Liminf[symmetric], unfold lim_extreal)
  2401       proof (rule lim_imp_Liminf[symmetric], unfold lim_ereal)
  2402         have "(\<lambda>i. ?diff i x) ----> 2 * w x - \<bar>u' x - u' x\<bar>"
  2402         have "(\<lambda>i. ?diff i x) ----> 2 * w x - \<bar>u' x - u' x\<bar>"
  2403           using u'[OF x] by (safe intro!: LIMSEQ_diff LIMSEQ_const LIMSEQ_imp_rabs)
  2403           using u'[OF x] by (safe intro!: LIMSEQ_diff LIMSEQ_const LIMSEQ_imp_rabs)
  2404         then show "(\<lambda>i. max 0 (?diff i x)) ----> max 0 (2 * w x)"
  2404         then show "(\<lambda>i. max 0 (?diff i x)) ----> max 0 (2 * w x)"
  2405           by (auto intro!: tendsto_real_max simp add: lim_extreal)
  2405           by (auto intro!: tendsto_real_max simp add: lim_ereal)
  2406       qed (rule trivial_limit_sequentially)
  2406       qed (rule trivial_limit_sequentially)
  2407     qed
  2407     qed
  2408     also have "\<dots> \<le> liminf (\<lambda>n. \<integral>\<^isup>+ x. max 0 (extreal (?diff n x)) \<partial>M)"
  2408     also have "\<dots> \<le> liminf (\<lambda>n. \<integral>\<^isup>+ x. max 0 (ereal (?diff n x)) \<partial>M)"
  2409       using u'_borel w u unfolding integrable_def
  2409       using u'_borel w u unfolding integrable_def
  2410       by (intro positive_integral_lim_INF) (auto intro!: positive_integral_lim_INF)
  2410       by (intro positive_integral_lim_INF) (auto intro!: positive_integral_lim_INF)
  2411     also have "\<dots> = (\<integral>\<^isup>+ x. extreal (2 * w x) \<partial>M) -
  2411     also have "\<dots> = (\<integral>\<^isup>+ x. ereal (2 * w x) \<partial>M) -
  2412         limsup (\<lambda>n. \<integral>\<^isup>+ x. extreal \<bar>u n x - u' x\<bar> \<partial>M)"
  2412         limsup (\<lambda>n. \<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)"
  2413       unfolding PI_diff positive_integral_max_0
  2413       unfolding PI_diff positive_integral_max_0
  2414       using positive_integral_positive[of "\<lambda>x. extreal (2 * w x)"]
  2414       using positive_integral_positive[of "\<lambda>x. ereal (2 * w x)"]
  2415       by (subst liminf_extreal_cminus) auto
  2415       by (subst liminf_ereal_cminus) auto
  2416     finally show ?thesis
  2416     finally show ?thesis
  2417       using neq_0 I2w_fin positive_integral_positive[of "\<lambda>x. extreal (2 * w x)"] pos
  2417       using neq_0 I2w_fin positive_integral_positive[of "\<lambda>x. ereal (2 * w x)"] pos
  2418       unfolding positive_integral_max_0
  2418       unfolding positive_integral_max_0
  2419       by (cases rule: extreal2_cases[of "\<integral>\<^isup>+ x. extreal (2 * w x) \<partial>M" "limsup (\<lambda>n. \<integral>\<^isup>+ x. extreal \<bar>u n x - u' x\<bar> \<partial>M)"])
  2419       by (cases rule: ereal2_cases[of "\<integral>\<^isup>+ x. ereal (2 * w x) \<partial>M" "limsup (\<lambda>n. \<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)"])
  2420          auto
  2420          auto
  2421   qed
  2421   qed
  2422 
  2422 
  2423   have "liminf ?f \<le> limsup ?f"
  2423   have "liminf ?f \<le> limsup ?f"
  2424     by (intro extreal_Liminf_le_Limsup trivial_limit_sequentially)
  2424     by (intro ereal_Liminf_le_Limsup trivial_limit_sequentially)
  2425   moreover
  2425   moreover
  2426   { have "0 = liminf (\<lambda>n. 0 :: extreal)" by (simp add: Liminf_const)
  2426   { have "0 = liminf (\<lambda>n. 0 :: ereal)" by (simp add: Liminf_const)
  2427     also have "\<dots> \<le> liminf ?f"
  2427     also have "\<dots> \<le> liminf ?f"
  2428       by (intro liminf_mono positive_integral_positive)
  2428       by (intro liminf_mono positive_integral_positive)
  2429     finally have "0 \<le> liminf ?f" . }
  2429     finally have "0 \<le> liminf ?f" . }
  2430   ultimately have liminf_limsup_eq: "liminf ?f = extreal 0" "limsup ?f = extreal 0"
  2430   ultimately have liminf_limsup_eq: "liminf ?f = ereal 0" "limsup ?f = ereal 0"
  2431     using `limsup ?f = 0` by auto
  2431     using `limsup ?f = 0` by auto
  2432   have "\<And>n. (\<integral>\<^isup>+ x. extreal \<bar>u n x - u' x\<bar> \<partial>M) = extreal (\<integral>x. \<bar>u n x - u' x\<bar> \<partial>M)"
  2432   have "\<And>n. (\<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M) = ereal (\<integral>x. \<bar>u n x - u' x\<bar> \<partial>M)"
  2433     using diff positive_integral_positive
  2433     using diff positive_integral_positive
  2434     by (subst integral_eq_positive_integral) (auto simp: extreal_real integrable_def)
  2434     by (subst integral_eq_positive_integral) (auto simp: ereal_real integrable_def)
  2435   then show ?lim_diff
  2435   then show ?lim_diff
  2436     using extreal_Liminf_eq_Limsup[OF trivial_limit_sequentially liminf_limsup_eq]
  2436     using ereal_Liminf_eq_Limsup[OF trivial_limit_sequentially liminf_limsup_eq]
  2437     by (simp add: lim_extreal)
  2437     by (simp add: lim_ereal)
  2438 
  2438 
  2439   show ?lim
  2439   show ?lim
  2440   proof (rule LIMSEQ_I)
  2440   proof (rule LIMSEQ_I)
  2441     fix r :: real assume "0 < r"
  2441     fix r :: real assume "0 < r"
  2442     from LIMSEQ_D[OF `?lim_diff` this]
  2442     from LIMSEQ_D[OF `?lim_diff` this]
  2552     have "(\<integral>x. \<bar>?F r x\<bar> \<partial>M) = (\<integral>x. \<bar>enum r\<bar> * indicator (?A r) x \<partial>M)"
  2552     have "(\<integral>x. \<bar>?F r x\<bar> \<partial>M) = (\<integral>x. \<bar>enum r\<bar> * indicator (?A r) x \<partial>M)"
  2553       by (auto simp: indicator_def intro!: integral_cong)
  2553       by (auto simp: indicator_def intro!: integral_cong)
  2554     also have "\<dots> = \<bar>enum r\<bar> * real (\<mu> (?A r))"
  2554     also have "\<dots> = \<bar>enum r\<bar> * real (\<mu> (?A r))"
  2555       using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
  2555       using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
  2556     finally have "(\<integral>x. \<bar>?F r x\<bar> \<partial>M) = \<bar>enum r * real (\<mu> (?A r))\<bar>"
  2556     finally have "(\<integral>x. \<bar>?F r x\<bar> \<partial>M) = \<bar>enum r * real (\<mu> (?A r))\<bar>"
  2557       using f by (subst (2) abs_mult_pos[symmetric]) (auto intro!: real_of_extreal_pos measurable_sets) }
  2557       using f by (subst (2) abs_mult_pos[symmetric]) (auto intro!: real_of_ereal_pos measurable_sets) }
  2558   note int_abs_F = this
  2558   note int_abs_F = this
  2559 
  2559 
  2560   have 1: "\<And>i. integrable M (\<lambda>x. ?F i x)"
  2560   have 1: "\<And>i. integrable M (\<lambda>x. ?F i x)"
  2561     using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
  2561     using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
  2562 
  2562 
  2616 lemma (in finite_measure_space) integral_finite_singleton:
  2616 lemma (in finite_measure_space) integral_finite_singleton:
  2617   shows "integrable M f"
  2617   shows "integrable M f"
  2618   and "integral\<^isup>L M f = (\<Sum>x \<in> space M. f x * real (\<mu> {x}))" (is ?I)
  2618   and "integral\<^isup>L M f = (\<Sum>x \<in> space M. f x * real (\<mu> {x}))" (is ?I)
  2619 proof -
  2619 proof -
  2620   have *:
  2620   have *:
  2621     "(\<integral>\<^isup>+ x. max 0 (extreal (f x)) \<partial>M) = (\<Sum>x \<in> space M. max 0 (extreal (f x)) * \<mu> {x})"
  2621     "(\<integral>\<^isup>+ x. max 0 (ereal (f x)) \<partial>M) = (\<Sum>x \<in> space M. max 0 (ereal (f x)) * \<mu> {x})"
  2622     "(\<integral>\<^isup>+ x. max 0 (extreal (- f x)) \<partial>M) = (\<Sum>x \<in> space M. max 0 (extreal (- f x)) * \<mu> {x})"
  2622     "(\<integral>\<^isup>+ x. max 0 (ereal (- f x)) \<partial>M) = (\<Sum>x \<in> space M. max 0 (ereal (- f x)) * \<mu> {x})"
  2623     by (simp_all add: positive_integral_finite_eq_setsum)
  2623     by (simp_all add: positive_integral_finite_eq_setsum)
  2624   then show "integrable M f" using finite_space finite_measure
  2624   then show "integrable M f" using finite_space finite_measure
  2625     by (simp add: setsum_Pinfty integrable_def positive_integral_max_0
  2625     by (simp add: setsum_Pinfty integrable_def positive_integral_max_0
  2626              split: split_max)
  2626              split: split_max)
  2627   show ?I using finite_measure *
  2627   show ?I using finite_measure *
  2628     apply (simp add: positive_integral_max_0 lebesgue_integral_def)
  2628     apply (simp add: positive_integral_max_0 lebesgue_integral_def)
  2629     apply (subst (1 2) setsum_real_of_extreal[symmetric])
  2629     apply (subst (1 2) setsum_real_of_ereal[symmetric])
  2630     apply (simp_all split: split_max add: setsum_subtractf[symmetric])
  2630     apply (simp_all split: split_max add: setsum_subtractf[symmetric])
  2631     apply (intro setsum_cong[OF refl])
  2631     apply (intro setsum_cong[OF refl])
  2632     apply (simp split: split_max)
  2632     apply (simp split: split_max)
  2633     done
  2633     done
  2634 qed
  2634 qed