src/HOL/Probability/Lebesgue_Integration.thy
author hoelzl
Fri Nov 02 14:23:40 2012 +0100 (2012-11-02)
changeset 50002 ce0d316b5b44
parent 50001 382bd3173584
child 50003 8c213922ed49
permissions -rw-r--r--
add measurability prover; add support for Borel sets
     1 (*  Title:      HOL/Probability/Lebesgue_Integration.thy
     2     Author:     Johannes Hölzl, TU München
     3     Author:     Armin Heller, TU München
     4 *)
     5 
     6 header {*Lebesgue Integration*}
     7 
     8 theory Lebesgue_Integration
     9   imports Measure_Space Borel_Space
    10 begin
    11 
    12 lemma ereal_minus_eq_PInfty_iff:
    13   fixes x y :: ereal shows "x - y = \<infinity> \<longleftrightarrow> y = -\<infinity> \<or> x = \<infinity>"
    14   by (cases x y rule: ereal2_cases) simp_all
    15 
    16 lemma real_ereal_1[simp]: "real (1::ereal) = 1"
    17   unfolding one_ereal_def by simp
    18 
    19 lemma ereal_indicator_pos[simp,intro]: "0 \<le> (indicator A x ::ereal)"
    20   unfolding indicator_def by auto
    21 
    22 lemma tendsto_real_max:
    23   fixes x y :: real
    24   assumes "(X ---> x) net"
    25   assumes "(Y ---> y) net"
    26   shows "((\<lambda>x. max (X x) (Y x)) ---> max x y) net"
    27 proof -
    28   have *: "\<And>x y :: real. max x y = y + ((x - y) + norm (x - y)) / 2"
    29     by (auto split: split_max simp: field_simps)
    30   show ?thesis
    31     unfolding *
    32     by (intro tendsto_add assms tendsto_divide tendsto_norm tendsto_diff) auto
    33 qed
    34 
    35 lemma measurable_sets2[intro]:
    36   assumes "f \<in> measurable M M'" "g \<in> measurable M M''"
    37   and "A \<in> sets M'" "B \<in> sets M''"
    38   shows "f -` A \<inter> g -` B \<inter> space M \<in> sets M"
    39 proof -
    40   have "f -` A \<inter> g -` B \<inter> space M = (f -` A \<inter> space M) \<inter> (g -` B \<inter> space M)"
    41     by auto
    42   then show ?thesis using assms by (auto intro: measurable_sets)
    43 qed
    44 
    45 lemma incseq_Suc_iff: "incseq f \<longleftrightarrow> (\<forall>n. f n \<le> f (Suc n))"
    46 proof
    47   assume "\<forall>n. f n \<le> f (Suc n)" then show "incseq f" by (auto intro!: incseq_SucI)
    48 qed (auto simp: incseq_def)
    49 
    50 section "Simple function"
    51 
    52 text {*
    53 
    54 Our simple functions are not restricted to positive real numbers. Instead
    55 they are just functions with a finite range and are measurable when singleton
    56 sets are measurable.
    57 
    58 *}
    59 
    60 definition "simple_function M g \<longleftrightarrow>
    61     finite (g ` space M) \<and>
    62     (\<forall>x \<in> g ` space M. g -` {x} \<inter> space M \<in> sets M)"
    63 
    64 lemma simple_functionD:
    65   assumes "simple_function M g"
    66   shows "finite (g ` space M)" and "g -` X \<inter> space M \<in> sets M"
    67 proof -
    68   show "finite (g ` space M)"
    69     using assms unfolding simple_function_def by auto
    70   have "g -` X \<inter> space M = g -` (X \<inter> g`space M) \<inter> space M" by auto
    71   also have "\<dots> = (\<Union>x\<in>X \<inter> g`space M. g-`{x} \<inter> space M)" by auto
    72   finally show "g -` X \<inter> space M \<in> sets M" using assms
    73     by (auto simp del: UN_simps simp: simple_function_def)
    74 qed
    75 
    76 lemma simple_function_measurable2[intro]:
    77   assumes "simple_function M f" "simple_function M g"
    78   shows "f -` A \<inter> g -` B \<inter> space M \<in> sets M"
    79 proof -
    80   have "f -` A \<inter> g -` B \<inter> space M = (f -` A \<inter> space M) \<inter> (g -` B \<inter> space M)"
    81     by auto
    82   then show ?thesis using assms[THEN simple_functionD(2)] by auto
    83 qed
    84 
    85 lemma simple_function_indicator_representation:
    86   fixes f ::"'a \<Rightarrow> ereal"
    87   assumes f: "simple_function M f" and x: "x \<in> space M"
    88   shows "f x = (\<Sum>y \<in> f ` space M. y * indicator (f -` {y} \<inter> space M) x)"
    89   (is "?l = ?r")
    90 proof -
    91   have "?r = (\<Sum>y \<in> f ` space M.
    92     (if y = f x then y * indicator (f -` {y} \<inter> space M) x else 0))"
    93     by (auto intro!: setsum_cong2)
    94   also have "... =  f x *  indicator (f -` {f x} \<inter> space M) x"
    95     using assms by (auto dest: simple_functionD simp: setsum_delta)
    96   also have "... = f x" using x by (auto simp: indicator_def)
    97   finally show ?thesis by auto
    98 qed
    99 
   100 lemma simple_function_notspace:
   101   "simple_function M (\<lambda>x. h x * indicator (- space M) x::ereal)" (is "simple_function M ?h")
   102 proof -
   103   have "?h ` space M \<subseteq> {0}" unfolding indicator_def by auto
   104   hence [simp, intro]: "finite (?h ` space M)" by (auto intro: finite_subset)
   105   have "?h -` {0} \<inter> space M = space M" by auto
   106   thus ?thesis unfolding simple_function_def by auto
   107 qed
   108 
   109 lemma simple_function_cong:
   110   assumes "\<And>t. t \<in> space M \<Longrightarrow> f t = g t"
   111   shows "simple_function M f \<longleftrightarrow> simple_function M g"
   112 proof -
   113   have "f ` space M = g ` space M"
   114     "\<And>x. f -` {x} \<inter> space M = g -` {x} \<inter> space M"
   115     using assms by (auto intro!: image_eqI)
   116   thus ?thesis unfolding simple_function_def using assms by simp
   117 qed
   118 
   119 lemma simple_function_cong_algebra:
   120   assumes "sets N = sets M" "space N = space M"
   121   shows "simple_function M f \<longleftrightarrow> simple_function N f"
   122   unfolding simple_function_def assms ..
   123 
   124 lemma borel_measurable_simple_function:
   125   assumes "simple_function M f"
   126   shows "f \<in> borel_measurable M"
   127 proof (rule borel_measurableI)
   128   fix S
   129   let ?I = "f ` (f -` S \<inter> space M)"
   130   have *: "(\<Union>x\<in>?I. f -` {x} \<inter> space M) = f -` S \<inter> space M" (is "?U = _") by auto
   131   have "finite ?I"
   132     using assms unfolding simple_function_def
   133     using finite_subset[of "f ` (f -` S \<inter> space M)" "f ` space M"] by auto
   134   hence "?U \<in> sets M"
   135     apply (rule finite_UN)
   136     using assms unfolding simple_function_def by auto
   137   thus "f -` S \<inter> space M \<in> sets M" unfolding * .
   138 qed
   139 
   140 lemma simple_function_borel_measurable:
   141   fixes f :: "'a \<Rightarrow> 'x::{t2_space}"
   142   assumes "f \<in> borel_measurable M" and "finite (f ` space M)"
   143   shows "simple_function M f"
   144   using assms unfolding simple_function_def
   145   by (auto intro: borel_measurable_vimage)
   146 
   147 lemma simple_function_eq_borel_measurable:
   148   fixes f :: "'a \<Rightarrow> ereal"
   149   shows "simple_function M f \<longleftrightarrow> finite (f`space M) \<and> f \<in> borel_measurable M"
   150   using simple_function_borel_measurable[of f] borel_measurable_simple_function[of M f]
   151   by (fastforce simp: simple_function_def)
   152 
   153 lemma simple_function_const[intro, simp]:
   154   "simple_function M (\<lambda>x. c)"
   155   by (auto intro: finite_subset simp: simple_function_def)
   156 lemma simple_function_compose[intro, simp]:
   157   assumes "simple_function M f"
   158   shows "simple_function M (g \<circ> f)"
   159   unfolding simple_function_def
   160 proof safe
   161   show "finite ((g \<circ> f) ` space M)"
   162     using assms unfolding simple_function_def by (auto simp: image_compose)
   163 next
   164   fix x assume "x \<in> space M"
   165   let ?G = "g -` {g (f x)} \<inter> (f`space M)"
   166   have *: "(g \<circ> f) -` {(g \<circ> f) x} \<inter> space M =
   167     (\<Union>x\<in>?G. f -` {x} \<inter> space M)" by auto
   168   show "(g \<circ> f) -` {(g \<circ> f) x} \<inter> space M \<in> sets M"
   169     using assms unfolding simple_function_def *
   170     by (rule_tac finite_UN) auto
   171 qed
   172 
   173 lemma simple_function_indicator[intro, simp]:
   174   assumes "A \<in> sets M"
   175   shows "simple_function M (indicator A)"
   176 proof -
   177   have "indicator A ` space M \<subseteq> {0, 1}" (is "?S \<subseteq> _")
   178     by (auto simp: indicator_def)
   179   hence "finite ?S" by (rule finite_subset) simp
   180   moreover have "- A \<inter> space M = space M - A" by auto
   181   ultimately show ?thesis unfolding simple_function_def
   182     using assms by (auto simp: indicator_def [abs_def])
   183 qed
   184 
   185 lemma simple_function_Pair[intro, simp]:
   186   assumes "simple_function M f"
   187   assumes "simple_function M g"
   188   shows "simple_function M (\<lambda>x. (f x, g x))" (is "simple_function M ?p")
   189   unfolding simple_function_def
   190 proof safe
   191   show "finite (?p ` space M)"
   192     using assms unfolding simple_function_def
   193     by (rule_tac finite_subset[of _ "f`space M \<times> g`space M"]) auto
   194 next
   195   fix x assume "x \<in> space M"
   196   have "(\<lambda>x. (f x, g x)) -` {(f x, g x)} \<inter> space M =
   197       (f -` {f x} \<inter> space M) \<inter> (g -` {g x} \<inter> space M)"
   198     by auto
   199   with `x \<in> space M` show "(\<lambda>x. (f x, g x)) -` {(f x, g x)} \<inter> space M \<in> sets M"
   200     using assms unfolding simple_function_def by auto
   201 qed
   202 
   203 lemma simple_function_compose1:
   204   assumes "simple_function M f"
   205   shows "simple_function M (\<lambda>x. g (f x))"
   206   using simple_function_compose[OF assms, of g]
   207   by (simp add: comp_def)
   208 
   209 lemma simple_function_compose2:
   210   assumes "simple_function M f" and "simple_function M g"
   211   shows "simple_function M (\<lambda>x. h (f x) (g x))"
   212 proof -
   213   have "simple_function M ((\<lambda>(x, y). h x y) \<circ> (\<lambda>x. (f x, g x)))"
   214     using assms by auto
   215   thus ?thesis by (simp_all add: comp_def)
   216 qed
   217 
   218 lemmas simple_function_add[intro, simp] = simple_function_compose2[where h="op +"]
   219   and simple_function_diff[intro, simp] = simple_function_compose2[where h="op -"]
   220   and simple_function_uminus[intro, simp] = simple_function_compose[where g="uminus"]
   221   and simple_function_mult[intro, simp] = simple_function_compose2[where h="op *"]
   222   and simple_function_div[intro, simp] = simple_function_compose2[where h="op /"]
   223   and simple_function_inverse[intro, simp] = simple_function_compose[where g="inverse"]
   224   and simple_function_max[intro, simp] = simple_function_compose2[where h=max]
   225 
   226 lemma simple_function_setsum[intro, simp]:
   227   assumes "\<And>i. i \<in> P \<Longrightarrow> simple_function M (f i)"
   228   shows "simple_function M (\<lambda>x. \<Sum>i\<in>P. f i x)"
   229 proof cases
   230   assume "finite P" from this assms show ?thesis by induct auto
   231 qed auto
   232 
   233 lemma
   234   fixes f g :: "'a \<Rightarrow> real" assumes sf: "simple_function M f"
   235   shows simple_function_ereal[intro, simp]: "simple_function M (\<lambda>x. ereal (f x))"
   236   by (auto intro!: simple_function_compose1[OF sf])
   237 
   238 lemma
   239   fixes f g :: "'a \<Rightarrow> nat" assumes sf: "simple_function M f"
   240   shows simple_function_real_of_nat[intro, simp]: "simple_function M (\<lambda>x. real (f x))"
   241   by (auto intro!: simple_function_compose1[OF sf])
   242 
   243 lemma borel_measurable_implies_simple_function_sequence:
   244   fixes u :: "'a \<Rightarrow> ereal"
   245   assumes u: "u \<in> borel_measurable M"
   246   shows "\<exists>f. incseq f \<and> (\<forall>i. \<infinity> \<notin> range (f i) \<and> simple_function M (f i)) \<and>
   247              (\<forall>x. (SUP i. f i x) = max 0 (u x)) \<and> (\<forall>i x. 0 \<le> f i x)"
   248 proof -
   249   def f \<equiv> "\<lambda>x i. if real i \<le> u x then i * 2 ^ i else natfloor (real (u x) * 2 ^ i)"
   250   { fix x j have "f x j \<le> j * 2 ^ j" unfolding f_def
   251     proof (split split_if, intro conjI impI)
   252       assume "\<not> real j \<le> u x"
   253       then have "natfloor (real (u x) * 2 ^ j) \<le> natfloor (j * 2 ^ j)"
   254          by (cases "u x") (auto intro!: natfloor_mono simp: mult_nonneg_nonneg)
   255       moreover have "real (natfloor (j * 2 ^ j)) \<le> j * 2^j"
   256         by (intro real_natfloor_le) (auto simp: mult_nonneg_nonneg)
   257       ultimately show "natfloor (real (u x) * 2 ^ j) \<le> j * 2 ^ j"
   258         unfolding real_of_nat_le_iff by auto
   259     qed auto }
   260   note f_upper = this
   261 
   262   have real_f:
   263     "\<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)))"
   264     unfolding f_def by auto
   265 
   266   let ?g = "\<lambda>j x. real (f x j) / 2^j :: ereal"
   267   show ?thesis
   268   proof (intro exI[of _ ?g] conjI allI ballI)
   269     fix i
   270     have "simple_function M (\<lambda>x. real (f x i))"
   271     proof (intro simple_function_borel_measurable)
   272       show "(\<lambda>x. real (f x i)) \<in> borel_measurable M"
   273         using u by (auto intro!: measurable_If simp: real_f)
   274       have "(\<lambda>x. real (f x i))`space M \<subseteq> real`{..i*2^i}"
   275         using f_upper[of _ i] by auto
   276       then show "finite ((\<lambda>x. real (f x i))`space M)"
   277         by (rule finite_subset) auto
   278     qed
   279     then show "simple_function M (?g i)"
   280       by (auto intro: simple_function_ereal simple_function_div)
   281   next
   282     show "incseq ?g"
   283     proof (intro incseq_ereal incseq_SucI le_funI)
   284       fix x and i :: nat
   285       have "f x i * 2 \<le> f x (Suc i)" unfolding f_def
   286       proof ((split split_if)+, intro conjI impI)
   287         assume "ereal (real i) \<le> u x" "\<not> ereal (real (Suc i)) \<le> u x"
   288         then show "i * 2 ^ i * 2 \<le> natfloor (real (u x) * 2 ^ Suc i)"
   289           by (cases "u x") (auto intro!: le_natfloor)
   290       next
   291         assume "\<not> ereal (real i) \<le> u x" "ereal (real (Suc i)) \<le> u x"
   292         then show "natfloor (real (u x) * 2 ^ i) * 2 \<le> Suc i * 2 ^ Suc i"
   293           by (cases "u x") auto
   294       next
   295         assume "\<not> ereal (real i) \<le> u x" "\<not> ereal (real (Suc i)) \<le> u x"
   296         have "natfloor (real (u x) * 2 ^ i) * 2 = natfloor (real (u x) * 2 ^ i) * natfloor 2"
   297           by simp
   298         also have "\<dots> \<le> natfloor (real (u x) * 2 ^ i * 2)"
   299         proof cases
   300           assume "0 \<le> u x" then show ?thesis
   301             by (intro le_mult_natfloor) 
   302         next
   303           assume "\<not> 0 \<le> u x" then show ?thesis
   304             by (cases "u x") (auto simp: natfloor_neg mult_nonpos_nonneg)
   305         qed
   306         also have "\<dots> = natfloor (real (u x) * 2 ^ Suc i)"
   307           by (simp add: ac_simps)
   308         finally show "natfloor (real (u x) * 2 ^ i) * 2 \<le> natfloor (real (u x) * 2 ^ Suc i)" .
   309       qed simp
   310       then show "?g i x \<le> ?g (Suc i) x"
   311         by (auto simp: field_simps)
   312     qed
   313   next
   314     fix x show "(SUP i. ?g i x) = max 0 (u x)"
   315     proof (rule ereal_SUPI)
   316       fix i show "?g i x \<le> max 0 (u x)" unfolding max_def f_def
   317         by (cases "u x") (auto simp: field_simps real_natfloor_le natfloor_neg
   318                                      mult_nonpos_nonneg mult_nonneg_nonneg)
   319     next
   320       fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> ?g i x \<le> y"
   321       have "\<And>i. 0 \<le> ?g i x" by (auto simp: divide_nonneg_pos)
   322       from order_trans[OF this *] have "0 \<le> y" by simp
   323       show "max 0 (u x) \<le> y"
   324       proof (cases y)
   325         case (real r)
   326         with * have *: "\<And>i. f x i \<le> r * 2^i" by (auto simp: divide_le_eq)
   327         from reals_Archimedean2[of r] * have "u x \<noteq> \<infinity>" by (auto simp: f_def) (metis less_le_not_le)
   328         then have "\<exists>p. max 0 (u x) = ereal p \<and> 0 \<le> p" by (cases "u x") (auto simp: max_def)
   329         then guess p .. note ux = this
   330         obtain m :: nat where m: "p < real m" using reals_Archimedean2 ..
   331         have "p \<le> r"
   332         proof (rule ccontr)
   333           assume "\<not> p \<le> r"
   334           with LIMSEQ_inverse_realpow_zero[unfolded LIMSEQ_iff, rule_format, of 2 "p - r"]
   335           obtain N where "\<forall>n\<ge>N. r * 2^n < p * 2^n - 1" by (auto simp: inverse_eq_divide field_simps)
   336           then have "r * 2^max N m < p * 2^max N m - 1" by simp
   337           moreover
   338           have "real (natfloor (p * 2 ^ max N m)) \<le> r * 2 ^ max N m"
   339             using *[of "max N m"] m unfolding real_f using ux
   340             by (cases "0 \<le> u x") (simp_all add: max_def mult_nonneg_nonneg split: split_if_asm)
   341           then have "p * 2 ^ max N m - 1 < r * 2 ^ max N m"
   342             by (metis real_natfloor_gt_diff_one less_le_trans)
   343           ultimately show False by auto
   344         qed
   345         then show "max 0 (u x) \<le> y" using real ux by simp
   346       qed (insert `0 \<le> y`, auto)
   347     qed
   348   qed (auto simp: divide_nonneg_pos)
   349 qed
   350 
   351 lemma borel_measurable_implies_simple_function_sequence':
   352   fixes u :: "'a \<Rightarrow> ereal"
   353   assumes u: "u \<in> borel_measurable M"
   354   obtains f where "\<And>i. simple_function M (f i)" "incseq f" "\<And>i. \<infinity> \<notin> range (f i)"
   355     "\<And>x. (SUP i. f i x) = max 0 (u x)" "\<And>i x. 0 \<le> f i x"
   356   using borel_measurable_implies_simple_function_sequence[OF u] by auto
   357 
   358 lemma simple_function_induct[consumes 1, case_names cong set mult add, induct set: simple_function]:
   359   fixes u :: "'a \<Rightarrow> ereal"
   360   assumes u: "simple_function M u"
   361   assumes cong: "\<And>f g. simple_function M f \<Longrightarrow> simple_function M g \<Longrightarrow> (AE x in M. f x = g x) \<Longrightarrow> P f \<Longrightarrow> P g"
   362   assumes set: "\<And>A. A \<in> sets M \<Longrightarrow> P (indicator A)"
   363   assumes mult: "\<And>u c. P u \<Longrightarrow> P (\<lambda>x. c * u x)"
   364   assumes add: "\<And>u v. P u \<Longrightarrow> P v \<Longrightarrow> P (\<lambda>x. v x + u x)"
   365   shows "P u"
   366 proof (rule cong)
   367   from AE_space show "AE x in M. (\<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x) = u x"
   368   proof eventually_elim
   369     fix x assume x: "x \<in> space M"
   370     from simple_function_indicator_representation[OF u x]
   371     show "(\<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x) = u x" ..
   372   qed
   373 next
   374   from u have "finite (u ` space M)"
   375     unfolding simple_function_def by auto
   376   then show "P (\<lambda>x. \<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x)"
   377   proof induct
   378     case empty show ?case
   379       using set[of "{}"] by (simp add: indicator_def[abs_def])
   380   qed (auto intro!: add mult set simple_functionD u)
   381 next
   382   show "simple_function M (\<lambda>x. (\<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x))"
   383     apply (subst simple_function_cong)
   384     apply (rule simple_function_indicator_representation[symmetric])
   385     apply (auto intro: u)
   386     done
   387 qed fact
   388 
   389 lemma simple_function_induct_nn[consumes 2, case_names cong set mult add]:
   390   fixes u :: "'a \<Rightarrow> ereal"
   391   assumes u: "simple_function M u" and nn: "\<And>x. 0 \<le> u x"
   392   assumes cong: "\<And>f g. simple_function M f \<Longrightarrow> simple_function M g \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> f x = g x) \<Longrightarrow> P f \<Longrightarrow> P g"
   393   assumes set: "\<And>A. A \<in> sets M \<Longrightarrow> P (indicator A)"
   394   assumes mult: "\<And>u c. 0 \<le> c \<Longrightarrow> simple_function M u \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> P u \<Longrightarrow> P (\<lambda>x. c * u x)"
   395   assumes add: "\<And>u v. simple_function M u \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> P u \<Longrightarrow> simple_function M v \<Longrightarrow> (\<And>x. 0 \<le> v x) \<Longrightarrow> P v \<Longrightarrow> P (\<lambda>x. v x + u x)"
   396   shows "P u"
   397 proof -
   398   show ?thesis
   399   proof (rule cong)
   400     fix x assume x: "x \<in> space M"
   401     from simple_function_indicator_representation[OF u x]
   402     show "(\<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x) = u x" ..
   403   next
   404     show "simple_function M (\<lambda>x. (\<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x))"
   405       apply (subst simple_function_cong)
   406       apply (rule simple_function_indicator_representation[symmetric])
   407       apply (auto intro: u)
   408       done
   409   next
   410     from u nn have "finite (u ` space M)" "\<And>x. x \<in> u ` space M \<Longrightarrow> 0 \<le> x"
   411       unfolding simple_function_def by auto
   412     then show "P (\<lambda>x. \<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x)"
   413     proof induct
   414       case empty show ?case
   415         using set[of "{}"] by (simp add: indicator_def[abs_def])
   416     qed (auto intro!: add mult set simple_functionD u setsum_nonneg
   417        simple_function_setsum)
   418   qed fact
   419 qed
   420 
   421 lemma borel_measurable_induct[consumes 2, case_names cong set mult add seq, induct set: borel_measurable]:
   422   fixes u :: "'a \<Rightarrow> ereal"
   423   assumes u: "u \<in> borel_measurable M" "\<And>x. 0 \<le> u x"
   424   assumes cong: "\<And>f g. f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> f x = g x) \<Longrightarrow> P g \<Longrightarrow> P f"
   425   assumes set: "\<And>A. A \<in> sets M \<Longrightarrow> P (indicator A)"
   426   assumes mult: "\<And>u c. 0 \<le> c \<Longrightarrow> u \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> P u \<Longrightarrow> P (\<lambda>x. c * u x)"
   427   assumes add: "\<And>u v. u \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> P u \<Longrightarrow> v \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 \<le> v x) \<Longrightarrow> P v \<Longrightarrow> P (\<lambda>x. v x + u x)"
   428   assumes seq: "\<And>U. (\<And>i. U i \<in> borel_measurable M) \<Longrightarrow>  (\<And>i x. 0 \<le> U i x) \<Longrightarrow>  (\<And>i. P (U i)) \<Longrightarrow> incseq U \<Longrightarrow> P (SUP i. U i)"
   429   shows "P u"
   430   using u
   431 proof (induct rule: borel_measurable_implies_simple_function_sequence')
   432   fix U assume U: "\<And>i. simple_function M (U i)" "incseq U" "\<And>i. \<infinity> \<notin> range (U i)" and
   433     sup: "\<And>x. (SUP i. U i x) = max 0 (u x)" and nn: "\<And>i x. 0 \<le> U i x"
   434   have u_eq: "u = (SUP i. U i)"
   435     using nn u sup by (auto simp: max_def)
   436   
   437   from U have "\<And>i. U i \<in> borel_measurable M"
   438     by (simp add: borel_measurable_simple_function)
   439 
   440   show "P u"
   441     unfolding u_eq
   442   proof (rule seq)
   443     fix i show "P (U i)"
   444       using `simple_function M (U i)` nn
   445       by (induct rule: simple_function_induct_nn)
   446          (auto intro: set mult add cong dest!: borel_measurable_simple_function)
   447   qed fact+
   448 qed
   449 
   450 lemma simple_function_If_set:
   451   assumes sf: "simple_function M f" "simple_function M g" and A: "A \<inter> space M \<in> sets M"
   452   shows "simple_function M (\<lambda>x. if x \<in> A then f x else g x)" (is "simple_function M ?IF")
   453 proof -
   454   def F \<equiv> "\<lambda>x. f -` {x} \<inter> space M" and G \<equiv> "\<lambda>x. g -` {x} \<inter> space M"
   455   show ?thesis unfolding simple_function_def
   456   proof safe
   457     have "?IF ` space M \<subseteq> f ` space M \<union> g ` space M" by auto
   458     from finite_subset[OF this] assms
   459     show "finite (?IF ` space M)" unfolding simple_function_def by auto
   460   next
   461     fix x assume "x \<in> space M"
   462     then have *: "?IF -` {?IF x} \<inter> space M = (if x \<in> A
   463       then ((F (f x) \<inter> (A \<inter> space M)) \<union> (G (f x) - (G (f x) \<inter> (A \<inter> space M))))
   464       else ((F (g x) \<inter> (A \<inter> space M)) \<union> (G (g x) - (G (g x) \<inter> (A \<inter> space M)))))"
   465       using sets_into_space[OF A] by (auto split: split_if_asm simp: G_def F_def)
   466     have [intro]: "\<And>x. F x \<in> sets M" "\<And>x. G x \<in> sets M"
   467       unfolding F_def G_def using sf[THEN simple_functionD(2)] by auto
   468     show "?IF -` {?IF x} \<inter> space M \<in> sets M" unfolding * using A by auto
   469   qed
   470 qed
   471 
   472 lemma simple_function_If:
   473   assumes sf: "simple_function M f" "simple_function M g" and P: "{x\<in>space M. P x} \<in> sets M"
   474   shows "simple_function M (\<lambda>x. if P x then f x else g x)"
   475 proof -
   476   have "{x\<in>space M. P x} = {x. P x} \<inter> space M" by auto
   477   with simple_function_If_set[OF sf, of "{x. P x}"] P show ?thesis by simp
   478 qed
   479 
   480 lemma simple_function_subalgebra:
   481   assumes "simple_function N f"
   482   and N_subalgebra: "sets N \<subseteq> sets M" "space N = space M"
   483   shows "simple_function M f"
   484   using assms unfolding simple_function_def by auto
   485 
   486 lemma simple_function_comp:
   487   assumes T: "T \<in> measurable M M'"
   488     and f: "simple_function M' f"
   489   shows "simple_function M (\<lambda>x. f (T x))"
   490 proof (intro simple_function_def[THEN iffD2] conjI ballI)
   491   have "(\<lambda>x. f (T x)) ` space M \<subseteq> f ` space M'"
   492     using T unfolding measurable_def by auto
   493   then show "finite ((\<lambda>x. f (T x)) ` space M)"
   494     using f unfolding simple_function_def by (auto intro: finite_subset)
   495   fix i assume i: "i \<in> (\<lambda>x. f (T x)) ` space M"
   496   then have "i \<in> f ` space M'"
   497     using T unfolding measurable_def by auto
   498   then have "f -` {i} \<inter> space M' \<in> sets M'"
   499     using f unfolding simple_function_def by auto
   500   then have "T -` (f -` {i} \<inter> space M') \<inter> space M \<in> sets M"
   501     using T unfolding measurable_def by auto
   502   also have "T -` (f -` {i} \<inter> space M') \<inter> space M = (\<lambda>x. f (T x)) -` {i} \<inter> space M"
   503     using T unfolding measurable_def by auto
   504   finally show "(\<lambda>x. f (T x)) -` {i} \<inter> space M \<in> sets M" .
   505 qed
   506 
   507 section "Simple integral"
   508 
   509 definition simple_integral :: "'a measure \<Rightarrow> ('a \<Rightarrow> ereal) \<Rightarrow> ereal" ("integral\<^isup>S") where
   510   "integral\<^isup>S M f = (\<Sum>x \<in> f ` space M. x * emeasure M (f -` {x} \<inter> space M))"
   511 
   512 syntax
   513   "_simple_integral" :: "pttrn \<Rightarrow> ereal \<Rightarrow> 'a measure \<Rightarrow> ereal" ("\<integral>\<^isup>S _. _ \<partial>_" [60,61] 110)
   514 
   515 translations
   516   "\<integral>\<^isup>S x. f \<partial>M" == "CONST simple_integral M (%x. f)"
   517 
   518 lemma simple_integral_cong:
   519   assumes "\<And>t. t \<in> space M \<Longrightarrow> f t = g t"
   520   shows "integral\<^isup>S M f = integral\<^isup>S M g"
   521 proof -
   522   have "f ` space M = g ` space M"
   523     "\<And>x. f -` {x} \<inter> space M = g -` {x} \<inter> space M"
   524     using assms by (auto intro!: image_eqI)
   525   thus ?thesis unfolding simple_integral_def by simp
   526 qed
   527 
   528 lemma simple_integral_const[simp]:
   529   "(\<integral>\<^isup>Sx. c \<partial>M) = c * (emeasure M) (space M)"
   530 proof (cases "space M = {}")
   531   case True thus ?thesis unfolding simple_integral_def by simp
   532 next
   533   case False hence "(\<lambda>x. c) ` space M = {c}" by auto
   534   thus ?thesis unfolding simple_integral_def by simp
   535 qed
   536 
   537 lemma simple_function_partition:
   538   assumes f: "simple_function M f" and g: "simple_function M g"
   539   shows "integral\<^isup>S M f = (\<Sum>A\<in>(\<lambda>x. f -` {f x} \<inter> g -` {g x} \<inter> space M) ` space M. the_elem (f`A) * (emeasure M) A)"
   540     (is "_ = setsum _ (?p ` space M)")
   541 proof-
   542   let ?sub = "\<lambda>x. ?p ` (f -` {x} \<inter> space M)"
   543   let ?SIGMA = "Sigma (f`space M) ?sub"
   544 
   545   have [intro]:
   546     "finite (f ` space M)"
   547     "finite (g ` space M)"
   548     using assms unfolding simple_function_def by simp_all
   549 
   550   { fix A
   551     have "?p ` (A \<inter> space M) \<subseteq>
   552       (\<lambda>(x,y). f -` {x} \<inter> g -` {y} \<inter> space M) ` (f`space M \<times> g`space M)"
   553       by auto
   554     hence "finite (?p ` (A \<inter> space M))"
   555       by (rule finite_subset) auto }
   556   note this[intro, simp]
   557   note sets = simple_function_measurable2[OF f g]
   558 
   559   { fix x assume "x \<in> space M"
   560     have "\<Union>(?sub (f x)) = (f -` {f x} \<inter> space M)" by auto
   561     with sets have "(emeasure M) (f -` {f x} \<inter> space M) = setsum (emeasure M) (?sub (f x))"
   562       by (subst setsum_emeasure) (auto simp: disjoint_family_on_def) }
   563   hence "integral\<^isup>S M f = (\<Sum>(x,A)\<in>?SIGMA. x * (emeasure M) A)"
   564     unfolding simple_integral_def using f sets
   565     by (subst setsum_Sigma[symmetric])
   566        (auto intro!: setsum_cong setsum_ereal_right_distrib)
   567   also have "\<dots> = (\<Sum>A\<in>?p ` space M. the_elem (f`A) * (emeasure M) A)"
   568   proof -
   569     have [simp]: "\<And>x. x \<in> space M \<Longrightarrow> f ` ?p x = {f x}" by (auto intro!: imageI)
   570     have "(\<lambda>A. (the_elem (f ` A), A)) ` ?p ` space M
   571       = (\<lambda>x. (f x, ?p x)) ` space M"
   572     proof safe
   573       fix x assume "x \<in> space M"
   574       thus "(f x, ?p x) \<in> (\<lambda>A. (the_elem (f`A), A)) ` ?p ` space M"
   575         by (auto intro!: image_eqI[of _ _ "?p x"])
   576     qed auto
   577     thus ?thesis
   578       apply (auto intro!: setsum_reindex_cong[of "\<lambda>A. (the_elem (f`A), A)"] inj_onI)
   579       apply (rule_tac x="xa" in image_eqI)
   580       by simp_all
   581   qed
   582   finally show ?thesis .
   583 qed
   584 
   585 lemma simple_integral_add[simp]:
   586   assumes f: "simple_function M f" and "\<And>x. 0 \<le> f x" and g: "simple_function M g" and "\<And>x. 0 \<le> g x"
   587   shows "(\<integral>\<^isup>Sx. f x + g x \<partial>M) = integral\<^isup>S M f + integral\<^isup>S M g"
   588 proof -
   589   { fix x let ?S = "g -` {g x} \<inter> f -` {f x} \<inter> space M"
   590     assume "x \<in> space M"
   591     hence "(\<lambda>a. f a + g a) ` ?S = {f x + g x}" "f ` ?S = {f x}" "g ` ?S = {g x}"
   592         "(\<lambda>x. (f x, g x)) -` {(f x, g x)} \<inter> space M = ?S"
   593       by auto }
   594   with assms show ?thesis
   595     unfolding
   596       simple_function_partition[OF simple_function_add[OF f g] simple_function_Pair[OF f g]]
   597       simple_function_partition[OF f g]
   598       simple_function_partition[OF g f]
   599     by (subst (3) Int_commute)
   600        (auto simp add: ereal_left_distrib setsum_addf[symmetric] intro!: setsum_cong)
   601 qed
   602 
   603 lemma simple_integral_setsum[simp]:
   604   assumes "\<And>i x. i \<in> P \<Longrightarrow> 0 \<le> f i x"
   605   assumes "\<And>i. i \<in> P \<Longrightarrow> simple_function M (f i)"
   606   shows "(\<integral>\<^isup>Sx. (\<Sum>i\<in>P. f i x) \<partial>M) = (\<Sum>i\<in>P. integral\<^isup>S M (f i))"
   607 proof cases
   608   assume "finite P"
   609   from this assms show ?thesis
   610     by induct (auto simp: simple_function_setsum simple_integral_add setsum_nonneg)
   611 qed auto
   612 
   613 lemma simple_integral_mult[simp]:
   614   assumes f: "simple_function M f" "\<And>x. 0 \<le> f x"
   615   shows "(\<integral>\<^isup>Sx. c * f x \<partial>M) = c * integral\<^isup>S M f"
   616 proof -
   617   note mult = simple_function_mult[OF simple_function_const[of _ c] f(1)]
   618   { fix x let ?S = "f -` {f x} \<inter> (\<lambda>x. c * f x) -` {c * f x} \<inter> space M"
   619     assume "x \<in> space M"
   620     hence "(\<lambda>x. c * f x) ` ?S = {c * f x}" "f ` ?S = {f x}"
   621       by auto }
   622   with assms show ?thesis
   623     unfolding simple_function_partition[OF mult f(1)]
   624               simple_function_partition[OF f(1) mult]
   625     by (subst setsum_ereal_right_distrib)
   626        (auto intro!: ereal_0_le_mult setsum_cong simp: mult_assoc)
   627 qed
   628 
   629 lemma simple_integral_mono_AE:
   630   assumes f: "simple_function M f" and g: "simple_function M g"
   631   and mono: "AE x in M. f x \<le> g x"
   632   shows "integral\<^isup>S M f \<le> integral\<^isup>S M g"
   633 proof -
   634   let ?S = "\<lambda>x. (g -` {g x} \<inter> space M) \<inter> (f -` {f x} \<inter> space M)"
   635   have *: "\<And>x. g -` {g x} \<inter> f -` {f x} \<inter> space M = ?S x"
   636     "\<And>x. f -` {f x} \<inter> g -` {g x} \<inter> space M = ?S x" by auto
   637   show ?thesis
   638     unfolding *
   639       simple_function_partition[OF f g]
   640       simple_function_partition[OF g f]
   641   proof (safe intro!: setsum_mono)
   642     fix x assume "x \<in> space M"
   643     then have *: "f ` ?S x = {f x}" "g ` ?S x = {g x}" by auto
   644     show "the_elem (f`?S x) * (emeasure M) (?S x) \<le> the_elem (g`?S x) * (emeasure M) (?S x)"
   645     proof (cases "f x \<le> g x")
   646       case True then show ?thesis
   647         using * assms(1,2)[THEN simple_functionD(2)]
   648         by (auto intro!: ereal_mult_right_mono)
   649     next
   650       case False
   651       obtain N where N: "{x\<in>space M. \<not> f x \<le> g x} \<subseteq> N" "N \<in> sets M" "(emeasure M) N = 0"
   652         using mono by (auto elim!: AE_E)
   653       have "?S x \<subseteq> N" using N `x \<in> space M` False by auto
   654       moreover have "?S x \<in> sets M" using assms
   655         by (rule_tac Int) (auto intro!: simple_functionD)
   656       ultimately have "(emeasure M) (?S x) \<le> (emeasure M) N"
   657         using `N \<in> sets M` by (auto intro!: emeasure_mono)
   658       moreover have "0 \<le> (emeasure M) (?S x)"
   659         using assms(1,2)[THEN simple_functionD(2)] by auto
   660       ultimately have "(emeasure M) (?S x) = 0" using `(emeasure M) N = 0` by auto
   661       then show ?thesis by simp
   662     qed
   663   qed
   664 qed
   665 
   666 lemma simple_integral_mono:
   667   assumes "simple_function M f" and "simple_function M g"
   668   and mono: "\<And> x. x \<in> space M \<Longrightarrow> f x \<le> g x"
   669   shows "integral\<^isup>S M f \<le> integral\<^isup>S M g"
   670   using assms by (intro simple_integral_mono_AE) auto
   671 
   672 lemma simple_integral_cong_AE:
   673   assumes "simple_function M f" and "simple_function M g"
   674   and "AE x in M. f x = g x"
   675   shows "integral\<^isup>S M f = integral\<^isup>S M g"
   676   using assms by (auto simp: eq_iff intro!: simple_integral_mono_AE)
   677 
   678 lemma simple_integral_cong':
   679   assumes sf: "simple_function M f" "simple_function M g"
   680   and mea: "(emeasure M) {x\<in>space M. f x \<noteq> g x} = 0"
   681   shows "integral\<^isup>S M f = integral\<^isup>S M g"
   682 proof (intro simple_integral_cong_AE sf AE_I)
   683   show "(emeasure M) {x\<in>space M. f x \<noteq> g x} = 0" by fact
   684   show "{x \<in> space M. f x \<noteq> g x} \<in> sets M"
   685     using sf[THEN borel_measurable_simple_function] by auto
   686 qed simp
   687 
   688 lemma simple_integral_indicator:
   689   assumes "A \<in> sets M"
   690   assumes f: "simple_function M f"
   691   shows "(\<integral>\<^isup>Sx. f x * indicator A x \<partial>M) =
   692     (\<Sum>x \<in> f ` space M. x * (emeasure M) (f -` {x} \<inter> space M \<inter> A))"
   693 proof cases
   694   assume "A = space M"
   695   moreover hence "(\<integral>\<^isup>Sx. f x * indicator A x \<partial>M) = integral\<^isup>S M f"
   696     by (auto intro!: simple_integral_cong)
   697   moreover have "\<And>X. X \<inter> space M \<inter> space M = X \<inter> space M" by auto
   698   ultimately show ?thesis by (simp add: simple_integral_def)
   699 next
   700   assume "A \<noteq> space M"
   701   then obtain x where x: "x \<in> space M" "x \<notin> A" using sets_into_space[OF assms(1)] by auto
   702   have I: "(\<lambda>x. f x * indicator A x) ` space M = f ` A \<union> {0}" (is "?I ` _ = _")
   703   proof safe
   704     fix y assume "?I y \<notin> f ` A" hence "y \<notin> A" by auto thus "?I y = 0" by auto
   705   next
   706     fix y assume "y \<in> A" thus "f y \<in> ?I ` space M"
   707       using sets_into_space[OF assms(1)] by (auto intro!: image_eqI[of _ _ y])
   708   next
   709     show "0 \<in> ?I ` space M" using x by (auto intro!: image_eqI[of _ _ x])
   710   qed
   711   have *: "(\<integral>\<^isup>Sx. f x * indicator A x \<partial>M) =
   712     (\<Sum>x \<in> f ` space M \<union> {0}. x * (emeasure M) (f -` {x} \<inter> space M \<inter> A))"
   713     unfolding simple_integral_def I
   714   proof (rule setsum_mono_zero_cong_left)
   715     show "finite (f ` space M \<union> {0})"
   716       using assms(2) unfolding simple_function_def by auto
   717     show "f ` A \<union> {0} \<subseteq> f`space M \<union> {0}"
   718       using sets_into_space[OF assms(1)] by auto
   719     have "\<And>x. f x \<notin> f ` A \<Longrightarrow> f -` {f x} \<inter> space M \<inter> A = {}"
   720       by (auto simp: image_iff)
   721     thus "\<forall>i\<in>f ` space M \<union> {0} - (f ` A \<union> {0}).
   722       i * (emeasure M) (f -` {i} \<inter> space M \<inter> A) = 0" by auto
   723   next
   724     fix x assume "x \<in> f`A \<union> {0}"
   725     hence "x \<noteq> 0 \<Longrightarrow> ?I -` {x} \<inter> space M = f -` {x} \<inter> space M \<inter> A"
   726       by (auto simp: indicator_def split: split_if_asm)
   727     thus "x * (emeasure M) (?I -` {x} \<inter> space M) =
   728       x * (emeasure M) (f -` {x} \<inter> space M \<inter> A)" by (cases "x = 0") simp_all
   729   qed
   730   show ?thesis unfolding *
   731     using assms(2) unfolding simple_function_def
   732     by (auto intro!: setsum_mono_zero_cong_right)
   733 qed
   734 
   735 lemma simple_integral_indicator_only[simp]:
   736   assumes "A \<in> sets M"
   737   shows "integral\<^isup>S M (indicator A) = emeasure M A"
   738 proof cases
   739   assume "space M = {}" hence "A = {}" using sets_into_space[OF assms] by auto
   740   thus ?thesis unfolding simple_integral_def using `space M = {}` by auto
   741 next
   742   assume "space M \<noteq> {}" hence "(\<lambda>x. 1) ` space M = {1::ereal}" by auto
   743   thus ?thesis
   744     using simple_integral_indicator[OF assms simple_function_const[of _ 1]]
   745     using sets_into_space[OF assms]
   746     by (auto intro!: arg_cong[where f="(emeasure M)"])
   747 qed
   748 
   749 lemma simple_integral_null_set:
   750   assumes "simple_function M u" "\<And>x. 0 \<le> u x" and "N \<in> null_sets M"
   751   shows "(\<integral>\<^isup>Sx. u x * indicator N x \<partial>M) = 0"
   752 proof -
   753   have "AE x in M. indicator N x = (0 :: ereal)"
   754     using `N \<in> null_sets M` by (auto simp: indicator_def intro!: AE_I[of _ _ N])
   755   then have "(\<integral>\<^isup>Sx. u x * indicator N x \<partial>M) = (\<integral>\<^isup>Sx. 0 \<partial>M)"
   756     using assms apply (intro simple_integral_cong_AE) by auto
   757   then show ?thesis by simp
   758 qed
   759 
   760 lemma simple_integral_cong_AE_mult_indicator:
   761   assumes sf: "simple_function M f" and eq: "AE x in M. x \<in> S" and "S \<in> sets M"
   762   shows "integral\<^isup>S M f = (\<integral>\<^isup>Sx. f x * indicator S x \<partial>M)"
   763   using assms by (intro simple_integral_cong_AE) auto
   764 
   765 lemma simple_integral_cmult_indicator:
   766   assumes A: "A \<in> sets M"
   767   shows "(\<integral>\<^isup>Sx. c * indicator A x \<partial>M) = c * (emeasure M) A"
   768   using simple_integral_mult[OF simple_function_indicator[OF A]]
   769   unfolding simple_integral_indicator_only[OF A] by simp
   770 
   771 lemma simple_integral_positive:
   772   assumes f: "simple_function M f" and ae: "AE x in M. 0 \<le> f x"
   773   shows "0 \<le> integral\<^isup>S M f"
   774 proof -
   775   have "integral\<^isup>S M (\<lambda>x. 0) \<le> integral\<^isup>S M f"
   776     using simple_integral_mono_AE[OF _ f ae] by auto
   777   then show ?thesis by simp
   778 qed
   779 
   780 section "Continuous positive integration"
   781 
   782 definition positive_integral :: "'a measure \<Rightarrow> ('a \<Rightarrow> ereal) \<Rightarrow> ereal" ("integral\<^isup>P") where
   783   "integral\<^isup>P M f = (SUP g : {g. simple_function M g \<and> g \<le> max 0 \<circ> f}. integral\<^isup>S M g)"
   784 
   785 syntax
   786   "_positive_integral" :: "pttrn \<Rightarrow> ereal \<Rightarrow> 'a measure \<Rightarrow> ereal" ("\<integral>\<^isup>+ _. _ \<partial>_" [60,61] 110)
   787 
   788 translations
   789   "\<integral>\<^isup>+ x. f \<partial>M" == "CONST positive_integral M (%x. f)"
   790 
   791 lemma positive_integral_positive:
   792   "0 \<le> integral\<^isup>P M f"
   793   by (auto intro!: SUP_upper2[of "\<lambda>x. 0"] simp: positive_integral_def le_fun_def)
   794 
   795 lemma positive_integral_not_MInfty[simp]: "integral\<^isup>P M f \<noteq> -\<infinity>"
   796   using positive_integral_positive[of M f] by auto
   797 
   798 lemma positive_integral_def_finite:
   799   "integral\<^isup>P M f = (SUP g : {g. simple_function M g \<and> g \<le> max 0 \<circ> f \<and> range g \<subseteq> {0 ..< \<infinity>}}. integral\<^isup>S M g)"
   800     (is "_ = SUPR ?A ?f")
   801   unfolding positive_integral_def
   802 proof (safe intro!: antisym SUP_least)
   803   fix g assume g: "simple_function M g" "g \<le> max 0 \<circ> f"
   804   let ?G = "{x \<in> space M. \<not> g x \<noteq> \<infinity>}"
   805   note gM = g(1)[THEN borel_measurable_simple_function]
   806   have \<mu>G_pos: "0 \<le> (emeasure M) ?G" using gM by auto
   807   let ?g = "\<lambda>y x. if g x = \<infinity> then y else max 0 (g x)"
   808   from g gM have g_in_A: "\<And>y. 0 \<le> y \<Longrightarrow> y \<noteq> \<infinity> \<Longrightarrow> ?g y \<in> ?A"
   809     apply (safe intro!: simple_function_max simple_function_If)
   810     apply (force simp: max_def le_fun_def split: split_if_asm)+
   811     done
   812   show "integral\<^isup>S M g \<le> SUPR ?A ?f"
   813   proof cases
   814     have g0: "?g 0 \<in> ?A" by (intro g_in_A) auto
   815     assume "(emeasure M) ?G = 0"
   816     with gM have "AE x in M. x \<notin> ?G"
   817       by (auto simp add: AE_iff_null intro!: null_setsI)
   818     with gM g show ?thesis
   819       by (intro SUP_upper2[OF g0] simple_integral_mono_AE)
   820          (auto simp: max_def intro!: simple_function_If)
   821   next
   822     assume \<mu>G: "(emeasure M) ?G \<noteq> 0"
   823     have "SUPR ?A (integral\<^isup>S M) = \<infinity>"
   824     proof (intro SUP_PInfty)
   825       fix n :: nat
   826       let ?y = "ereal (real n) / (if (emeasure M) ?G = \<infinity> then 1 else (emeasure M) ?G)"
   827       have "0 \<le> ?y" "?y \<noteq> \<infinity>" using \<mu>G \<mu>G_pos by (auto simp: ereal_divide_eq)
   828       then have "?g ?y \<in> ?A" by (rule g_in_A)
   829       have "real n \<le> ?y * (emeasure M) ?G"
   830         using \<mu>G \<mu>G_pos by (cases "(emeasure M) ?G") (auto simp: field_simps)
   831       also have "\<dots> = (\<integral>\<^isup>Sx. ?y * indicator ?G x \<partial>M)"
   832         using `0 \<le> ?y` `?g ?y \<in> ?A` gM
   833         by (subst simple_integral_cmult_indicator) auto
   834       also have "\<dots> \<le> integral\<^isup>S M (?g ?y)" using `?g ?y \<in> ?A` gM
   835         by (intro simple_integral_mono) auto
   836       finally show "\<exists>i\<in>?A. real n \<le> integral\<^isup>S M i"
   837         using `?g ?y \<in> ?A` by blast
   838     qed
   839     then show ?thesis by simp
   840   qed
   841 qed (auto intro: SUP_upper)
   842 
   843 lemma positive_integral_mono_AE:
   844   assumes ae: "AE x in M. u x \<le> v x" shows "integral\<^isup>P M u \<le> integral\<^isup>P M v"
   845   unfolding positive_integral_def
   846 proof (safe intro!: SUP_mono)
   847   fix n assume n: "simple_function M n" "n \<le> max 0 \<circ> u"
   848   from ae[THEN AE_E] guess N . note N = this
   849   then have ae_N: "AE x in M. x \<notin> N" by (auto intro: AE_not_in)
   850   let ?n = "\<lambda>x. n x * indicator (space M - N) x"
   851   have "AE x in M. n x \<le> ?n x" "simple_function M ?n"
   852     using n N ae_N by auto
   853   moreover
   854   { fix x have "?n x \<le> max 0 (v x)"
   855     proof cases
   856       assume x: "x \<in> space M - N"
   857       with N have "u x \<le> v x" by auto
   858       with n(2)[THEN le_funD, of x] x show ?thesis
   859         by (auto simp: max_def split: split_if_asm)
   860     qed simp }
   861   then have "?n \<le> max 0 \<circ> v" by (auto simp: le_funI)
   862   moreover have "integral\<^isup>S M n \<le> integral\<^isup>S M ?n"
   863     using ae_N N n by (auto intro!: simple_integral_mono_AE)
   864   ultimately show "\<exists>m\<in>{g. simple_function M g \<and> g \<le> max 0 \<circ> v}. integral\<^isup>S M n \<le> integral\<^isup>S M m"
   865     by force
   866 qed
   867 
   868 lemma positive_integral_mono:
   869   "(\<And>x. x \<in> space M \<Longrightarrow> u x \<le> v x) \<Longrightarrow> integral\<^isup>P M u \<le> integral\<^isup>P M v"
   870   by (auto intro: positive_integral_mono_AE)
   871 
   872 lemma positive_integral_cong_AE:
   873   "AE x in M. u x = v x \<Longrightarrow> integral\<^isup>P M u = integral\<^isup>P M v"
   874   by (auto simp: eq_iff intro!: positive_integral_mono_AE)
   875 
   876 lemma positive_integral_cong:
   877   "(\<And>x. x \<in> space M \<Longrightarrow> u x = v x) \<Longrightarrow> integral\<^isup>P M u = integral\<^isup>P M v"
   878   by (auto intro: positive_integral_cong_AE)
   879 
   880 lemma positive_integral_eq_simple_integral:
   881   assumes f: "simple_function M f" "\<And>x. 0 \<le> f x" shows "integral\<^isup>P M f = integral\<^isup>S M f"
   882 proof -
   883   let ?f = "\<lambda>x. f x * indicator (space M) x"
   884   have f': "simple_function M ?f" using f by auto
   885   with f(2) have [simp]: "max 0 \<circ> ?f = ?f"
   886     by (auto simp: fun_eq_iff max_def split: split_indicator)
   887   have "integral\<^isup>P M ?f \<le> integral\<^isup>S M ?f" using f'
   888     by (force intro!: SUP_least simple_integral_mono simp: le_fun_def positive_integral_def)
   889   moreover have "integral\<^isup>S M ?f \<le> integral\<^isup>P M ?f"
   890     unfolding positive_integral_def
   891     using f' by (auto intro!: SUP_upper)
   892   ultimately show ?thesis
   893     by (simp cong: positive_integral_cong simple_integral_cong)
   894 qed
   895 
   896 lemma positive_integral_eq_simple_integral_AE:
   897   assumes f: "simple_function M f" "AE x in M. 0 \<le> f x" shows "integral\<^isup>P M f = integral\<^isup>S M f"
   898 proof -
   899   have "AE x in M. f x = max 0 (f x)" using f by (auto split: split_max)
   900   with f have "integral\<^isup>P M f = integral\<^isup>S M (\<lambda>x. max 0 (f x))"
   901     by (simp cong: positive_integral_cong_AE simple_integral_cong_AE
   902              add: positive_integral_eq_simple_integral)
   903   with assms show ?thesis
   904     by (auto intro!: simple_integral_cong_AE split: split_max)
   905 qed
   906 
   907 lemma positive_integral_SUP_approx:
   908   assumes f: "incseq f" "\<And>i. f i \<in> borel_measurable M" "\<And>i x. 0 \<le> f i x"
   909   and u: "simple_function M u" "u \<le> (SUP i. f i)" "u`space M \<subseteq> {0..<\<infinity>}"
   910   shows "integral\<^isup>S M u \<le> (SUP i. integral\<^isup>P M (f i))" (is "_ \<le> ?S")
   911 proof (rule ereal_le_mult_one_interval)
   912   have "0 \<le> (SUP i. integral\<^isup>P M (f i))"
   913     using f(3) by (auto intro!: SUP_upper2 positive_integral_positive)
   914   then show "(SUP i. integral\<^isup>P M (f i)) \<noteq> -\<infinity>" by auto
   915   have u_range: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> u x \<and> u x \<noteq> \<infinity>"
   916     using u(3) by auto
   917   fix a :: ereal assume "0 < a" "a < 1"
   918   hence "a \<noteq> 0" by auto
   919   let ?B = "\<lambda>i. {x \<in> space M. a * u x \<le> f i x}"
   920   have B: "\<And>i. ?B i \<in> sets M"
   921     using f `simple_function M u` by (auto simp: borel_measurable_simple_function)
   922 
   923   let ?uB = "\<lambda>i x. u x * indicator (?B i) x"
   924 
   925   { fix i have "?B i \<subseteq> ?B (Suc i)"
   926     proof safe
   927       fix i x assume "a * u x \<le> f i x"
   928       also have "\<dots> \<le> f (Suc i) x"
   929         using `incseq f`[THEN incseq_SucD] unfolding le_fun_def by auto
   930       finally show "a * u x \<le> f (Suc i) x" .
   931     qed }
   932   note B_mono = this
   933 
   934   note B_u = Int[OF u(1)[THEN simple_functionD(2)] B]
   935 
   936   let ?B' = "\<lambda>i n. (u -` {i} \<inter> space M) \<inter> ?B n"
   937   have measure_conv: "\<And>i. (emeasure M) (u -` {i} \<inter> space M) = (SUP n. (emeasure M) (?B' i n))"
   938   proof -
   939     fix i
   940     have 1: "range (?B' i) \<subseteq> sets M" using B_u by auto
   941     have 2: "incseq (?B' i)" using B_mono by (auto intro!: incseq_SucI)
   942     have "(\<Union>n. ?B' i n) = u -` {i} \<inter> space M"
   943     proof safe
   944       fix x i assume x: "x \<in> space M"
   945       show "x \<in> (\<Union>i. ?B' (u x) i)"
   946       proof cases
   947         assume "u x = 0" thus ?thesis using `x \<in> space M` f(3) by simp
   948       next
   949         assume "u x \<noteq> 0"
   950         with `a < 1` u_range[OF `x \<in> space M`]
   951         have "a * u x < 1 * u x"
   952           by (intro ereal_mult_strict_right_mono) (auto simp: image_iff)
   953         also have "\<dots> \<le> (SUP i. f i x)" using u(2) by (auto simp: le_fun_def)
   954         finally obtain i where "a * u x < f i x" unfolding SUP_def
   955           by (auto simp add: less_Sup_iff)
   956         hence "a * u x \<le> f i x" by auto
   957         thus ?thesis using `x \<in> space M` by auto
   958       qed
   959     qed
   960     then show "?thesis i" using SUP_emeasure_incseq[OF 1 2] by simp
   961   qed
   962 
   963   have "integral\<^isup>S M u = (SUP i. integral\<^isup>S M (?uB i))"
   964     unfolding simple_integral_indicator[OF B `simple_function M u`]
   965   proof (subst SUPR_ereal_setsum, safe)
   966     fix x n assume "x \<in> space M"
   967     with u_range show "incseq (\<lambda>i. u x * (emeasure M) (?B' (u x) i))" "\<And>i. 0 \<le> u x * (emeasure M) (?B' (u x) i)"
   968       using B_mono B_u by (auto intro!: emeasure_mono ereal_mult_left_mono incseq_SucI simp: ereal_zero_le_0_iff)
   969   next
   970     show "integral\<^isup>S M u = (\<Sum>i\<in>u ` space M. SUP n. i * (emeasure M) (?B' i n))"
   971       using measure_conv u_range B_u unfolding simple_integral_def
   972       by (auto intro!: setsum_cong SUPR_ereal_cmult[symmetric])
   973   qed
   974   moreover
   975   have "a * (SUP i. integral\<^isup>S M (?uB i)) \<le> ?S"
   976     apply (subst SUPR_ereal_cmult[symmetric])
   977   proof (safe intro!: SUP_mono bexI)
   978     fix i
   979     have "a * integral\<^isup>S M (?uB i) = (\<integral>\<^isup>Sx. a * ?uB i x \<partial>M)"
   980       using B `simple_function M u` u_range
   981       by (subst simple_integral_mult) (auto split: split_indicator)
   982     also have "\<dots> \<le> integral\<^isup>P M (f i)"
   983     proof -
   984       have *: "simple_function M (\<lambda>x. a * ?uB i x)" using B `0 < a` u(1) by auto
   985       show ?thesis using f(3) * u_range `0 < a`
   986         by (subst positive_integral_eq_simple_integral[symmetric])
   987            (auto intro!: positive_integral_mono split: split_indicator)
   988     qed
   989     finally show "a * integral\<^isup>S M (?uB i) \<le> integral\<^isup>P M (f i)"
   990       by auto
   991   next
   992     fix i show "0 \<le> \<integral>\<^isup>S x. ?uB i x \<partial>M" using B `0 < a` u(1) u_range
   993       by (intro simple_integral_positive) (auto split: split_indicator)
   994   qed (insert `0 < a`, auto)
   995   ultimately show "a * integral\<^isup>S M u \<le> ?S" by simp
   996 qed
   997 
   998 lemma incseq_positive_integral:
   999   assumes "incseq f" shows "incseq (\<lambda>i. integral\<^isup>P M (f i))"
  1000 proof -
  1001   have "\<And>i x. f i x \<le> f (Suc i) x"
  1002     using assms by (auto dest!: incseq_SucD simp: le_fun_def)
  1003   then show ?thesis
  1004     by (auto intro!: incseq_SucI positive_integral_mono)
  1005 qed
  1006 
  1007 text {* Beppo-Levi monotone convergence theorem *}
  1008 lemma positive_integral_monotone_convergence_SUP:
  1009   assumes f: "incseq f" "\<And>i. f i \<in> borel_measurable M" "\<And>i x. 0 \<le> f i x"
  1010   shows "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) = (SUP i. integral\<^isup>P M (f i))"
  1011 proof (rule antisym)
  1012   show "(SUP j. integral\<^isup>P M (f j)) \<le> (\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M)"
  1013     by (auto intro!: SUP_least SUP_upper positive_integral_mono)
  1014 next
  1015   show "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) \<le> (SUP j. integral\<^isup>P M (f j))"
  1016     unfolding positive_integral_def_finite[of _ "\<lambda>x. SUP i. f i x"]
  1017   proof (safe intro!: SUP_least)
  1018     fix g assume g: "simple_function M g"
  1019       and "g \<le> max 0 \<circ> (\<lambda>x. SUP i. f i x)" "range g \<subseteq> {0..<\<infinity>}"
  1020     moreover then have "\<And>x. 0 \<le> (SUP i. f i x)" and g': "g`space M \<subseteq> {0..<\<infinity>}"
  1021       using f by (auto intro!: SUP_upper2)
  1022     ultimately show "integral\<^isup>S M g \<le> (SUP j. integral\<^isup>P M (f j))"
  1023       by (intro  positive_integral_SUP_approx[OF f g _ g'])
  1024          (auto simp: le_fun_def max_def)
  1025   qed
  1026 qed
  1027 
  1028 lemma positive_integral_monotone_convergence_SUP_AE:
  1029   assumes f: "\<And>i. AE x in M. f i x \<le> f (Suc i) x \<and> 0 \<le> f i x" "\<And>i. f i \<in> borel_measurable M"
  1030   shows "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) = (SUP i. integral\<^isup>P M (f i))"
  1031 proof -
  1032   from f have "AE x in M. \<forall>i. f i x \<le> f (Suc i) x \<and> 0 \<le> f i x"
  1033     by (simp add: AE_all_countable)
  1034   from this[THEN AE_E] guess N . note N = this
  1035   let ?f = "\<lambda>i x. if x \<in> space M - N then f i x else 0"
  1036   have f_eq: "AE x in M. \<forall>i. ?f i x = f i x" using N by (auto intro!: AE_I[of _ _ N])
  1037   then have "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) = (\<integral>\<^isup>+ x. (SUP i. ?f i x) \<partial>M)"
  1038     by (auto intro!: positive_integral_cong_AE)
  1039   also have "\<dots> = (SUP i. (\<integral>\<^isup>+ x. ?f i x \<partial>M))"
  1040   proof (rule positive_integral_monotone_convergence_SUP)
  1041     show "incseq ?f" using N(1) by (force intro!: incseq_SucI le_funI)
  1042     { fix i show "(\<lambda>x. if x \<in> space M - N then f i x else 0) \<in> borel_measurable M"
  1043         using f N(3) by (intro measurable_If_set) auto
  1044       fix x show "0 \<le> ?f i x"
  1045         using N(1) by auto }
  1046   qed
  1047   also have "\<dots> = (SUP i. (\<integral>\<^isup>+ x. f i x \<partial>M))"
  1048     using f_eq by (force intro!: arg_cong[where f="SUPR UNIV"] positive_integral_cong_AE ext)
  1049   finally show ?thesis .
  1050 qed
  1051 
  1052 lemma positive_integral_monotone_convergence_SUP_AE_incseq:
  1053   assumes f: "incseq f" "\<And>i. AE x in M. 0 \<le> f i x" and borel: "\<And>i. f i \<in> borel_measurable M"
  1054   shows "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) = (SUP i. integral\<^isup>P M (f i))"
  1055   using f[unfolded incseq_Suc_iff le_fun_def]
  1056   by (intro positive_integral_monotone_convergence_SUP_AE[OF _ borel])
  1057      auto
  1058 
  1059 lemma positive_integral_monotone_convergence_simple:
  1060   assumes f: "incseq f" "\<And>i x. 0 \<le> f i x" "\<And>i. simple_function M (f i)"
  1061   shows "(SUP i. integral\<^isup>S M (f i)) = (\<integral>\<^isup>+x. (SUP i. f i x) \<partial>M)"
  1062   using assms unfolding positive_integral_monotone_convergence_SUP[OF f(1)
  1063     f(3)[THEN borel_measurable_simple_function] f(2)]
  1064   by (auto intro!: positive_integral_eq_simple_integral[symmetric] arg_cong[where f="SUPR UNIV"] ext)
  1065 
  1066 lemma positive_integral_max_0:
  1067   "(\<integral>\<^isup>+x. max 0 (f x) \<partial>M) = integral\<^isup>P M f"
  1068   by (simp add: le_fun_def positive_integral_def)
  1069 
  1070 lemma positive_integral_cong_pos:
  1071   assumes "\<And>x. x \<in> space M \<Longrightarrow> f x \<le> 0 \<and> g x \<le> 0 \<or> f x = g x"
  1072   shows "integral\<^isup>P M f = integral\<^isup>P M g"
  1073 proof -
  1074   have "integral\<^isup>P M (\<lambda>x. max 0 (f x)) = integral\<^isup>P M (\<lambda>x. max 0 (g x))"
  1075   proof (intro positive_integral_cong)
  1076     fix x assume "x \<in> space M"
  1077     from assms[OF this] show "max 0 (f x) = max 0 (g x)"
  1078       by (auto split: split_max)
  1079   qed
  1080   then show ?thesis by (simp add: positive_integral_max_0)
  1081 qed
  1082 
  1083 lemma SUP_simple_integral_sequences:
  1084   assumes f: "incseq f" "\<And>i x. 0 \<le> f i x" "\<And>i. simple_function M (f i)"
  1085   and g: "incseq g" "\<And>i x. 0 \<le> g i x" "\<And>i. simple_function M (g i)"
  1086   and eq: "AE x in M. (SUP i. f i x) = (SUP i. g i x)"
  1087   shows "(SUP i. integral\<^isup>S M (f i)) = (SUP i. integral\<^isup>S M (g i))"
  1088     (is "SUPR _ ?F = SUPR _ ?G")
  1089 proof -
  1090   have "(SUP i. integral\<^isup>S M (f i)) = (\<integral>\<^isup>+x. (SUP i. f i x) \<partial>M)"
  1091     using f by (rule positive_integral_monotone_convergence_simple)
  1092   also have "\<dots> = (\<integral>\<^isup>+x. (SUP i. g i x) \<partial>M)"
  1093     unfolding eq[THEN positive_integral_cong_AE] ..
  1094   also have "\<dots> = (SUP i. ?G i)"
  1095     using g by (rule positive_integral_monotone_convergence_simple[symmetric])
  1096   finally show ?thesis by simp
  1097 qed
  1098 
  1099 lemma positive_integral_const[simp]:
  1100   "0 \<le> c \<Longrightarrow> (\<integral>\<^isup>+ x. c \<partial>M) = c * (emeasure M) (space M)"
  1101   by (subst positive_integral_eq_simple_integral) auto
  1102 
  1103 lemma positive_integral_linear:
  1104   assumes f: "f \<in> borel_measurable M" "\<And>x. 0 \<le> f x" and "0 \<le> a"
  1105   and g: "g \<in> borel_measurable M" "\<And>x. 0 \<le> g x"
  1106   shows "(\<integral>\<^isup>+ x. a * f x + g x \<partial>M) = a * integral\<^isup>P M f + integral\<^isup>P M g"
  1107     (is "integral\<^isup>P M ?L = _")
  1108 proof -
  1109   from borel_measurable_implies_simple_function_sequence'[OF f(1)] guess u .
  1110   note u = positive_integral_monotone_convergence_simple[OF this(2,5,1)] this
  1111   from borel_measurable_implies_simple_function_sequence'[OF g(1)] guess v .
  1112   note v = positive_integral_monotone_convergence_simple[OF this(2,5,1)] this
  1113   let ?L' = "\<lambda>i x. a * u i x + v i x"
  1114 
  1115   have "?L \<in> borel_measurable M" using assms by auto
  1116   from borel_measurable_implies_simple_function_sequence'[OF this] guess l .
  1117   note l = positive_integral_monotone_convergence_simple[OF this(2,5,1)] this
  1118 
  1119   have inc: "incseq (\<lambda>i. a * integral\<^isup>S M (u i))" "incseq (\<lambda>i. integral\<^isup>S M (v i))"
  1120     using u v `0 \<le> a`
  1121     by (auto simp: incseq_Suc_iff le_fun_def
  1122              intro!: add_mono ereal_mult_left_mono simple_integral_mono)
  1123   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)"
  1124     using u v `0 \<le> a` by (auto simp: simple_integral_positive)
  1125   { fix i from pos[of i] have "a * integral\<^isup>S M (u i) \<noteq> -\<infinity>" "integral\<^isup>S M (v i) \<noteq> -\<infinity>"
  1126       by (auto split: split_if_asm) }
  1127   note not_MInf = this
  1128 
  1129   have l': "(SUP i. integral\<^isup>S M (l i)) = (SUP i. integral\<^isup>S M (?L' i))"
  1130   proof (rule SUP_simple_integral_sequences[OF l(3,6,2)])
  1131     show "incseq ?L'" "\<And>i x. 0 \<le> ?L' i x" "\<And>i. simple_function M (?L' i)"
  1132       using u v  `0 \<le> a` unfolding incseq_Suc_iff le_fun_def
  1133       by (auto intro!: add_mono ereal_mult_left_mono ereal_add_nonneg_nonneg)
  1134     { fix x
  1135       { 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]
  1136           by auto }
  1137       then have "(SUP i. a * u i x + v i x) = a * (SUP i. u i x) + (SUP i. v i x)"
  1138         using `0 \<le> a` u(3) v(3) u(6)[of _ x] v(6)[of _ x]
  1139         by (subst SUPR_ereal_cmult[symmetric, OF u(6) `0 \<le> a`])
  1140            (auto intro!: SUPR_ereal_add
  1141                  simp: incseq_Suc_iff le_fun_def add_mono ereal_mult_left_mono ereal_add_nonneg_nonneg) }
  1142     then show "AE x in M. (SUP i. l i x) = (SUP i. ?L' i x)"
  1143       unfolding l(5) using `0 \<le> a` u(5) v(5) l(5) f(2) g(2)
  1144       by (intro AE_I2) (auto split: split_max simp add: ereal_add_nonneg_nonneg)
  1145   qed
  1146   also have "\<dots> = (SUP i. a * integral\<^isup>S M (u i) + integral\<^isup>S M (v i))"
  1147     using u(2, 6) v(2, 6) `0 \<le> a` by (auto intro!: arg_cong[where f="SUPR UNIV"] ext)
  1148   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)"
  1149     unfolding l(5)[symmetric] u(5)[symmetric] v(5)[symmetric]
  1150     unfolding l(1)[symmetric] u(1)[symmetric] v(1)[symmetric]
  1151     apply (subst SUPR_ereal_cmult[symmetric, OF pos(1) `0 \<le> a`])
  1152     apply (subst SUPR_ereal_add[symmetric, OF inc not_MInf]) .
  1153   then show ?thesis by (simp add: positive_integral_max_0)
  1154 qed
  1155 
  1156 lemma positive_integral_cmult:
  1157   assumes f: "f \<in> borel_measurable M" "0 \<le> c"
  1158   shows "(\<integral>\<^isup>+ x. c * f x \<partial>M) = c * integral\<^isup>P M f"
  1159 proof -
  1160   have [simp]: "\<And>x. c * max 0 (f x) = max 0 (c * f x)" using `0 \<le> c`
  1161     by (auto split: split_max simp: ereal_zero_le_0_iff)
  1162   have "(\<integral>\<^isup>+ x. c * f x \<partial>M) = (\<integral>\<^isup>+ x. c * max 0 (f x) \<partial>M)"
  1163     by (simp add: positive_integral_max_0)
  1164   then show ?thesis
  1165     using positive_integral_linear[OF _ _ `0 \<le> c`, of "\<lambda>x. max 0 (f x)" _ "\<lambda>x. 0"] f
  1166     by (auto simp: positive_integral_max_0)
  1167 qed
  1168 
  1169 lemma positive_integral_multc:
  1170   assumes "f \<in> borel_measurable M" "0 \<le> c"
  1171   shows "(\<integral>\<^isup>+ x. f x * c \<partial>M) = integral\<^isup>P M f * c"
  1172   unfolding mult_commute[of _ c] positive_integral_cmult[OF assms] by simp
  1173 
  1174 lemma positive_integral_indicator[simp]:
  1175   "A \<in> sets M \<Longrightarrow> (\<integral>\<^isup>+ x. indicator A x\<partial>M) = (emeasure M) A"
  1176   by (subst positive_integral_eq_simple_integral)
  1177      (auto simp: simple_integral_indicator)
  1178 
  1179 lemma positive_integral_cmult_indicator:
  1180   "0 \<le> c \<Longrightarrow> A \<in> sets M \<Longrightarrow> (\<integral>\<^isup>+ x. c * indicator A x \<partial>M) = c * (emeasure M) A"
  1181   by (subst positive_integral_eq_simple_integral)
  1182      (auto simp: simple_function_indicator simple_integral_indicator)
  1183 
  1184 lemma positive_integral_add:
  1185   assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x"
  1186   and g: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
  1187   shows "(\<integral>\<^isup>+ x. f x + g x \<partial>M) = integral\<^isup>P M f + integral\<^isup>P M g"
  1188 proof -
  1189   have ae: "AE x in M. max 0 (f x) + max 0 (g x) = max 0 (f x + g x)"
  1190     using assms by (auto split: split_max simp: ereal_add_nonneg_nonneg)
  1191   have "(\<integral>\<^isup>+ x. f x + g x \<partial>M) = (\<integral>\<^isup>+ x. max 0 (f x + g x) \<partial>M)"
  1192     by (simp add: positive_integral_max_0)
  1193   also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (f x) + max 0 (g x) \<partial>M)"
  1194     unfolding ae[THEN positive_integral_cong_AE] ..
  1195   also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (f x) \<partial>M) + (\<integral>\<^isup>+ x. max 0 (g x) \<partial>M)"
  1196     using positive_integral_linear[of "\<lambda>x. max 0 (f x)" _ 1 "\<lambda>x. max 0 (g x)"] f g
  1197     by auto
  1198   finally show ?thesis
  1199     by (simp add: positive_integral_max_0)
  1200 qed
  1201 
  1202 lemma positive_integral_setsum:
  1203   assumes "\<And>i. i\<in>P \<Longrightarrow> f i \<in> borel_measurable M" "\<And>i. i\<in>P \<Longrightarrow> AE x in M. 0 \<le> f i x"
  1204   shows "(\<integral>\<^isup>+ x. (\<Sum>i\<in>P. f i x) \<partial>M) = (\<Sum>i\<in>P. integral\<^isup>P M (f i))"
  1205 proof cases
  1206   assume f: "finite P"
  1207   from assms have "AE x in M. \<forall>i\<in>P. 0 \<le> f i x" unfolding AE_finite_all[OF f] by auto
  1208   from f this assms(1) show ?thesis
  1209   proof induct
  1210     case (insert i P)
  1211     then have "f i \<in> borel_measurable M" "AE x in M. 0 \<le> f i x"
  1212       "(\<lambda>x. \<Sum>i\<in>P. f i x) \<in> borel_measurable M" "AE x in M. 0 \<le> (\<Sum>i\<in>P. f i x)"
  1213       by (auto intro!: setsum_nonneg)
  1214     from positive_integral_add[OF this]
  1215     show ?case using insert by auto
  1216   qed simp
  1217 qed simp
  1218 
  1219 lemma positive_integral_Markov_inequality:
  1220   assumes u: "u \<in> borel_measurable M" "AE x in M. 0 \<le> u x" and "A \<in> sets M" and c: "0 \<le> c"
  1221   shows "(emeasure M) ({x\<in>space M. 1 \<le> c * u x} \<inter> A) \<le> c * (\<integral>\<^isup>+ x. u x * indicator A x \<partial>M)"
  1222     (is "(emeasure M) ?A \<le> _ * ?PI")
  1223 proof -
  1224   have "?A \<in> sets M"
  1225     using `A \<in> sets M` u by auto
  1226   hence "(emeasure M) ?A = (\<integral>\<^isup>+ x. indicator ?A x \<partial>M)"
  1227     using positive_integral_indicator by simp
  1228   also have "\<dots> \<le> (\<integral>\<^isup>+ x. c * (u x * indicator A x) \<partial>M)" using u c
  1229     by (auto intro!: positive_integral_mono_AE
  1230       simp: indicator_def ereal_zero_le_0_iff)
  1231   also have "\<dots> = c * (\<integral>\<^isup>+ x. u x * indicator A x \<partial>M)"
  1232     using assms
  1233     by (auto intro!: positive_integral_cmult simp: ereal_zero_le_0_iff)
  1234   finally show ?thesis .
  1235 qed
  1236 
  1237 lemma positive_integral_noteq_infinite:
  1238   assumes g: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
  1239   and "integral\<^isup>P M g \<noteq> \<infinity>"
  1240   shows "AE x in M. g x \<noteq> \<infinity>"
  1241 proof (rule ccontr)
  1242   assume c: "\<not> (AE x in M. g x \<noteq> \<infinity>)"
  1243   have "(emeasure M) {x\<in>space M. g x = \<infinity>} \<noteq> 0"
  1244     using c g by (auto simp add: AE_iff_null)
  1245   moreover have "0 \<le> (emeasure M) {x\<in>space M. g x = \<infinity>}" using g by (auto intro: measurable_sets)
  1246   ultimately have "0 < (emeasure M) {x\<in>space M. g x = \<infinity>}" by auto
  1247   then have "\<infinity> = \<infinity> * (emeasure M) {x\<in>space M. g x = \<infinity>}" by auto
  1248   also have "\<dots> \<le> (\<integral>\<^isup>+x. \<infinity> * indicator {x\<in>space M. g x = \<infinity>} x \<partial>M)"
  1249     using g by (subst positive_integral_cmult_indicator) auto
  1250   also have "\<dots> \<le> integral\<^isup>P M g"
  1251     using assms by (auto intro!: positive_integral_mono_AE simp: indicator_def)
  1252   finally show False using `integral\<^isup>P M g \<noteq> \<infinity>` by auto
  1253 qed
  1254 
  1255 lemma positive_integral_diff:
  1256   assumes f: "f \<in> borel_measurable M"
  1257   and g: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
  1258   and fin: "integral\<^isup>P M g \<noteq> \<infinity>"
  1259   and mono: "AE x in M. g x \<le> f x"
  1260   shows "(\<integral>\<^isup>+ x. f x - g x \<partial>M) = integral\<^isup>P M f - integral\<^isup>P M g"
  1261 proof -
  1262   have diff: "(\<lambda>x. f x - g x) \<in> borel_measurable M" "AE x in M. 0 \<le> f x - g x"
  1263     using assms by (auto intro: ereal_diff_positive)
  1264   have pos_f: "AE x in M. 0 \<le> f x" using mono g by auto
  1265   { fix a b :: ereal assume "0 \<le> a" "a \<noteq> \<infinity>" "0 \<le> b" "a \<le> b" then have "b - a + a = b"
  1266       by (cases rule: ereal2_cases[of a b]) auto }
  1267   note * = this
  1268   then have "AE x in M. f x = f x - g x + g x"
  1269     using mono positive_integral_noteq_infinite[OF g fin] assms by auto
  1270   then have **: "integral\<^isup>P M f = (\<integral>\<^isup>+x. f x - g x \<partial>M) + integral\<^isup>P M g"
  1271     unfolding positive_integral_add[OF diff g, symmetric]
  1272     by (rule positive_integral_cong_AE)
  1273   show ?thesis unfolding **
  1274     using fin positive_integral_positive[of M g]
  1275     by (cases rule: ereal2_cases[of "\<integral>\<^isup>+ x. f x - g x \<partial>M" "integral\<^isup>P M g"]) auto
  1276 qed
  1277 
  1278 lemma positive_integral_suminf:
  1279   assumes f: "\<And>i. f i \<in> borel_measurable M" "\<And>i. AE x in M. 0 \<le> f i x"
  1280   shows "(\<integral>\<^isup>+ x. (\<Sum>i. f i x) \<partial>M) = (\<Sum>i. integral\<^isup>P M (f i))"
  1281 proof -
  1282   have all_pos: "AE x in M. \<forall>i. 0 \<le> f i x"
  1283     using assms by (auto simp: AE_all_countable)
  1284   have "(\<Sum>i. integral\<^isup>P M (f i)) = (SUP n. \<Sum>i<n. integral\<^isup>P M (f i))"
  1285     using positive_integral_positive by (rule suminf_ereal_eq_SUPR)
  1286   also have "\<dots> = (SUP n. \<integral>\<^isup>+x. (\<Sum>i<n. f i x) \<partial>M)"
  1287     unfolding positive_integral_setsum[OF f] ..
  1288   also have "\<dots> = \<integral>\<^isup>+x. (SUP n. \<Sum>i<n. f i x) \<partial>M" using f all_pos
  1289     by (intro positive_integral_monotone_convergence_SUP_AE[symmetric])
  1290        (elim AE_mp, auto simp: setsum_nonneg simp del: setsum_lessThan_Suc intro!: AE_I2 setsum_mono3)
  1291   also have "\<dots> = \<integral>\<^isup>+x. (\<Sum>i. f i x) \<partial>M" using all_pos
  1292     by (intro positive_integral_cong_AE) (auto simp: suminf_ereal_eq_SUPR)
  1293   finally show ?thesis by simp
  1294 qed
  1295 
  1296 text {* Fatou's lemma: convergence theorem on limes inferior *}
  1297 lemma positive_integral_lim_INF:
  1298   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1299   assumes u: "\<And>i. u i \<in> borel_measurable M" "\<And>i. AE x in M. 0 \<le> u i x"
  1300   shows "(\<integral>\<^isup>+ x. liminf (\<lambda>n. u n x) \<partial>M) \<le> liminf (\<lambda>n. integral\<^isup>P M (u n))"
  1301 proof -
  1302   have pos: "AE x in M. \<forall>i. 0 \<le> u i x" using u by (auto simp: AE_all_countable)
  1303   have "(\<integral>\<^isup>+ x. liminf (\<lambda>n. u n x) \<partial>M) =
  1304     (SUP n. \<integral>\<^isup>+ x. (INF i:{n..}. u i x) \<partial>M)"
  1305     unfolding liminf_SUPR_INFI using pos u
  1306     by (intro positive_integral_monotone_convergence_SUP_AE)
  1307        (elim AE_mp, auto intro!: AE_I2 intro: INF_greatest INF_superset_mono)
  1308   also have "\<dots> \<le> liminf (\<lambda>n. integral\<^isup>P M (u n))"
  1309     unfolding liminf_SUPR_INFI
  1310     by (auto intro!: SUP_mono exI INF_greatest positive_integral_mono INF_lower)
  1311   finally show ?thesis .
  1312 qed
  1313 
  1314 lemma positive_integral_null_set:
  1315   assumes "N \<in> null_sets M" shows "(\<integral>\<^isup>+ x. u x * indicator N x \<partial>M) = 0"
  1316 proof -
  1317   have "(\<integral>\<^isup>+ x. u x * indicator N x \<partial>M) = (\<integral>\<^isup>+ x. 0 \<partial>M)"
  1318   proof (intro positive_integral_cong_AE AE_I)
  1319     show "{x \<in> space M. u x * indicator N x \<noteq> 0} \<subseteq> N"
  1320       by (auto simp: indicator_def)
  1321     show "(emeasure M) N = 0" "N \<in> sets M"
  1322       using assms by auto
  1323   qed
  1324   then show ?thesis by simp
  1325 qed
  1326 
  1327 lemma positive_integral_0_iff:
  1328   assumes u: "u \<in> borel_measurable M" and pos: "AE x in M. 0 \<le> u x"
  1329   shows "integral\<^isup>P M u = 0 \<longleftrightarrow> emeasure M {x\<in>space M. u x \<noteq> 0} = 0"
  1330     (is "_ \<longleftrightarrow> (emeasure M) ?A = 0")
  1331 proof -
  1332   have u_eq: "(\<integral>\<^isup>+ x. u x * indicator ?A x \<partial>M) = integral\<^isup>P M u"
  1333     by (auto intro!: positive_integral_cong simp: indicator_def)
  1334   show ?thesis
  1335   proof
  1336     assume "(emeasure M) ?A = 0"
  1337     with positive_integral_null_set[of ?A M u] u
  1338     show "integral\<^isup>P M u = 0" by (simp add: u_eq null_sets_def)
  1339   next
  1340     { fix r :: ereal and n :: nat assume gt_1: "1 \<le> real n * r"
  1341       then have "0 < real n * r" by (cases r) (auto split: split_if_asm simp: one_ereal_def)
  1342       then have "0 \<le> r" by (auto simp add: ereal_zero_less_0_iff) }
  1343     note gt_1 = this
  1344     assume *: "integral\<^isup>P M u = 0"
  1345     let ?M = "\<lambda>n. {x \<in> space M. 1 \<le> real (n::nat) * u x}"
  1346     have "0 = (SUP n. (emeasure M) (?M n \<inter> ?A))"
  1347     proof -
  1348       { fix n :: nat
  1349         from positive_integral_Markov_inequality[OF u pos, of ?A "ereal (real n)"]
  1350         have "(emeasure M) (?M n \<inter> ?A) \<le> 0" unfolding u_eq * using u by simp
  1351         moreover have "0 \<le> (emeasure M) (?M n \<inter> ?A)" using u by auto
  1352         ultimately have "(emeasure M) (?M n \<inter> ?A) = 0" by auto }
  1353       thus ?thesis by simp
  1354     qed
  1355     also have "\<dots> = (emeasure M) (\<Union>n. ?M n \<inter> ?A)"
  1356     proof (safe intro!: SUP_emeasure_incseq)
  1357       fix n show "?M n \<inter> ?A \<in> sets M"
  1358         using u by (auto intro!: Int)
  1359     next
  1360       show "incseq (\<lambda>n. {x \<in> space M. 1 \<le> real n * u x} \<inter> {x \<in> space M. u x \<noteq> 0})"
  1361       proof (safe intro!: incseq_SucI)
  1362         fix n :: nat and x
  1363         assume *: "1 \<le> real n * u x"
  1364         also from gt_1[OF this] have "real n * u x \<le> real (Suc n) * u x"
  1365           using `0 \<le> u x` by (auto intro!: ereal_mult_right_mono)
  1366         finally show "1 \<le> real (Suc n) * u x" by auto
  1367       qed
  1368     qed
  1369     also have "\<dots> = (emeasure M) {x\<in>space M. 0 < u x}"
  1370     proof (safe intro!: arg_cong[where f="(emeasure M)"] dest!: gt_1)
  1371       fix x assume "0 < u x" and [simp, intro]: "x \<in> space M"
  1372       show "x \<in> (\<Union>n. ?M n \<inter> ?A)"
  1373       proof (cases "u x")
  1374         case (real r) with `0 < u x` have "0 < r" by auto
  1375         obtain j :: nat where "1 / r \<le> real j" using real_arch_simple ..
  1376         hence "1 / r * r \<le> real j * r" unfolding mult_le_cancel_right using `0 < r` by auto
  1377         hence "1 \<le> real j * r" using real `0 < r` by auto
  1378         thus ?thesis using `0 < r` real by (auto simp: one_ereal_def)
  1379       qed (insert `0 < u x`, auto)
  1380     qed auto
  1381     finally have "(emeasure M) {x\<in>space M. 0 < u x} = 0" by simp
  1382     moreover
  1383     from pos have "AE x in M. \<not> (u x < 0)" by auto
  1384     then have "(emeasure M) {x\<in>space M. u x < 0} = 0"
  1385       using AE_iff_null[of M] u by auto
  1386     moreover have "(emeasure M) {x\<in>space M. u x \<noteq> 0} = (emeasure M) {x\<in>space M. u x < 0} + (emeasure M) {x\<in>space M. 0 < u x}"
  1387       using u by (subst plus_emeasure) (auto intro!: arg_cong[where f="emeasure M"])
  1388     ultimately show "(emeasure M) ?A = 0" by simp
  1389   qed
  1390 qed
  1391 
  1392 lemma positive_integral_0_iff_AE:
  1393   assumes u: "u \<in> borel_measurable M"
  1394   shows "integral\<^isup>P M u = 0 \<longleftrightarrow> (AE x in M. u x \<le> 0)"
  1395 proof -
  1396   have sets: "{x\<in>space M. max 0 (u x) \<noteq> 0} \<in> sets M"
  1397     using u by auto
  1398   from positive_integral_0_iff[of "\<lambda>x. max 0 (u x)"]
  1399   have "integral\<^isup>P M u = 0 \<longleftrightarrow> (AE x in M. max 0 (u x) = 0)"
  1400     unfolding positive_integral_max_0
  1401     using AE_iff_null[OF sets] u by auto
  1402   also have "\<dots> \<longleftrightarrow> (AE x in M. u x \<le> 0)" by (auto split: split_max)
  1403   finally show ?thesis .
  1404 qed
  1405 
  1406 lemma AE_iff_positive_integral: 
  1407   "{x\<in>space M. P x} \<in> sets M \<Longrightarrow> (AE x in M. P x) \<longleftrightarrow> integral\<^isup>P M (indicator {x. \<not> P x}) = 0"
  1408   by (subst positive_integral_0_iff_AE)
  1409      (auto simp: one_ereal_def zero_ereal_def sets_Collect_neg indicator_def[abs_def] measurable_If)
  1410 
  1411 lemma positive_integral_const_If:
  1412   "(\<integral>\<^isup>+x. a \<partial>M) = (if 0 \<le> a then a * (emeasure M) (space M) else 0)"
  1413   by (auto intro!: positive_integral_0_iff_AE[THEN iffD2])
  1414 
  1415 lemma positive_integral_subalgebra:
  1416   assumes f: "f \<in> borel_measurable N" "\<And>x. 0 \<le> f x"
  1417   and N: "sets N \<subseteq> sets M" "space N = space M" "\<And>A. A \<in> sets N \<Longrightarrow> emeasure N A = emeasure M A"
  1418   shows "integral\<^isup>P N f = integral\<^isup>P M f"
  1419 proof -
  1420   have [simp]: "\<And>f :: 'a \<Rightarrow> ereal. f \<in> borel_measurable N \<Longrightarrow> f \<in> borel_measurable M"
  1421     using N by (auto simp: measurable_def)
  1422   have [simp]: "\<And>P. (AE x in N. P x) \<Longrightarrow> (AE x in M. P x)"
  1423     using N by (auto simp add: eventually_ae_filter null_sets_def)
  1424   have [simp]: "\<And>A. A \<in> sets N \<Longrightarrow> A \<in> sets M"
  1425     using N by auto
  1426   from f show ?thesis
  1427     apply induct
  1428     apply (simp_all add: positive_integral_add positive_integral_cmult positive_integral_monotone_convergence_SUP N)
  1429     apply (auto intro!: positive_integral_cong cong: positive_integral_cong simp: N(2)[symmetric])
  1430     done
  1431 qed
  1432 
  1433 section "Lebesgue Integral"
  1434 
  1435 definition integrable :: "'a measure \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> bool" where
  1436   "integrable M f \<longleftrightarrow> f \<in> borel_measurable M \<and>
  1437     (\<integral>\<^isup>+ x. ereal (f x) \<partial>M) \<noteq> \<infinity> \<and> (\<integral>\<^isup>+ x. ereal (- f x) \<partial>M) \<noteq> \<infinity>"
  1438 
  1439 lemma integrableD[dest]:
  1440   assumes "integrable M f"
  1441   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>"
  1442   using assms unfolding integrable_def by auto
  1443 
  1444 definition lebesgue_integral :: "'a measure \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> real" ("integral\<^isup>L") where
  1445   "integral\<^isup>L M f = real ((\<integral>\<^isup>+ x. ereal (f x) \<partial>M)) - real ((\<integral>\<^isup>+ x. ereal (- f x) \<partial>M))"
  1446 
  1447 syntax
  1448   "_lebesgue_integral" :: "pttrn \<Rightarrow> real \<Rightarrow> 'a measure \<Rightarrow> real" ("\<integral> _. _ \<partial>_" [60,61] 110)
  1449 
  1450 translations
  1451   "\<integral> x. f \<partial>M" == "CONST lebesgue_integral M (%x. f)"
  1452 
  1453 lemma integrableE:
  1454   assumes "integrable M f"
  1455   obtains r q where
  1456     "(\<integral>\<^isup>+x. ereal (f x)\<partial>M) = ereal r"
  1457     "(\<integral>\<^isup>+x. ereal (-f x)\<partial>M) = ereal q"
  1458     "f \<in> borel_measurable M" "integral\<^isup>L M f = r - q"
  1459   using assms unfolding integrable_def lebesgue_integral_def
  1460   using positive_integral_positive[of M "\<lambda>x. ereal (f x)"]
  1461   using positive_integral_positive[of M "\<lambda>x. ereal (-f x)"]
  1462   by (cases rule: ereal2_cases[of "(\<integral>\<^isup>+x. ereal (-f x)\<partial>M)" "(\<integral>\<^isup>+x. ereal (f x)\<partial>M)"]) auto
  1463 
  1464 lemma integral_cong:
  1465   assumes "\<And>x. x \<in> space M \<Longrightarrow> f x = g x"
  1466   shows "integral\<^isup>L M f = integral\<^isup>L M g"
  1467   using assms by (simp cong: positive_integral_cong add: lebesgue_integral_def)
  1468 
  1469 lemma integral_cong_AE:
  1470   assumes cong: "AE x in M. f x = g x"
  1471   shows "integral\<^isup>L M f = integral\<^isup>L M g"
  1472 proof -
  1473   have *: "AE x in M. ereal (f x) = ereal (g x)"
  1474     "AE x in M. ereal (- f x) = ereal (- g x)" using cong by auto
  1475   show ?thesis
  1476     unfolding *[THEN positive_integral_cong_AE] lebesgue_integral_def ..
  1477 qed
  1478 
  1479 lemma integrable_cong_AE:
  1480   assumes borel: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1481   assumes "AE x in M. f x = g x"
  1482   shows "integrable M f = integrable M g"
  1483 proof -
  1484   have "(\<integral>\<^isup>+ x. ereal (f x) \<partial>M) = (\<integral>\<^isup>+ x. ereal (g x) \<partial>M)"
  1485     "(\<integral>\<^isup>+ x. ereal (- f x) \<partial>M) = (\<integral>\<^isup>+ x. ereal (- g x) \<partial>M)"
  1486     using assms by (auto intro!: positive_integral_cong_AE)
  1487   with assms show ?thesis
  1488     by (auto simp: integrable_def)
  1489 qed
  1490 
  1491 lemma integrable_cong:
  1492   "(\<And>x. x \<in> space M \<Longrightarrow> f x = g x) \<Longrightarrow> integrable M f \<longleftrightarrow> integrable M g"
  1493   by (simp cong: positive_integral_cong measurable_cong add: integrable_def)
  1494 
  1495 lemma integral_mono_AE:
  1496   assumes fg: "integrable M f" "integrable M g"
  1497   and mono: "AE t in M. f t \<le> g t"
  1498   shows "integral\<^isup>L M f \<le> integral\<^isup>L M g"
  1499 proof -
  1500   have "AE x in M. ereal (f x) \<le> ereal (g x)"
  1501     using mono by auto
  1502   moreover have "AE x in M. ereal (- g x) \<le> ereal (- f x)"
  1503     using mono by auto
  1504   ultimately show ?thesis using fg
  1505     by (auto intro!: add_mono positive_integral_mono_AE real_of_ereal_positive_mono
  1506              simp: positive_integral_positive lebesgue_integral_def diff_minus)
  1507 qed
  1508 
  1509 lemma integral_mono:
  1510   assumes "integrable M f" "integrable M g" "\<And>t. t \<in> space M \<Longrightarrow> f t \<le> g t"
  1511   shows "integral\<^isup>L M f \<le> integral\<^isup>L M g"
  1512   using assms by (auto intro: integral_mono_AE)
  1513 
  1514 lemma positive_integral_eq_integral:
  1515   assumes f: "integrable M f"
  1516   assumes nonneg: "AE x in M. 0 \<le> f x" 
  1517   shows "(\<integral>\<^isup>+ x. ereal (f x) \<partial>M) = integral\<^isup>L M f"
  1518 proof -
  1519   have "(\<integral>\<^isup>+ x. max 0 (ereal (- f x)) \<partial>M) = (\<integral>\<^isup>+ x. 0 \<partial>M)"
  1520     using nonneg by (intro positive_integral_cong_AE) (auto split: split_max)
  1521   with f positive_integral_positive show ?thesis
  1522     by (cases "\<integral>\<^isup>+ x. ereal (f x) \<partial>M")
  1523        (auto simp add: lebesgue_integral_def positive_integral_max_0 integrable_def)
  1524 qed
  1525   
  1526 lemma integral_eq_positive_integral:
  1527   assumes f: "\<And>x. 0 \<le> f x"
  1528   shows "integral\<^isup>L M f = real (\<integral>\<^isup>+ x. ereal (f x) \<partial>M)"
  1529 proof -
  1530   { fix x have "max 0 (ereal (- f x)) = 0" using f[of x] by (simp split: split_max) }
  1531   then have "0 = (\<integral>\<^isup>+ x. max 0 (ereal (- f x)) \<partial>M)" by simp
  1532   also have "\<dots> = (\<integral>\<^isup>+ x. ereal (- f x) \<partial>M)" unfolding positive_integral_max_0 ..
  1533   finally show ?thesis
  1534     unfolding lebesgue_integral_def by simp
  1535 qed
  1536 
  1537 lemma integral_minus[intro, simp]:
  1538   assumes "integrable M f"
  1539   shows "integrable M (\<lambda>x. - f x)" "(\<integral>x. - f x \<partial>M) = - integral\<^isup>L M f"
  1540   using assms by (auto simp: integrable_def lebesgue_integral_def)
  1541 
  1542 lemma integral_minus_iff[simp]:
  1543   "integrable M (\<lambda>x. - f x) \<longleftrightarrow> integrable M f"
  1544 proof
  1545   assume "integrable M (\<lambda>x. - f x)"
  1546   then have "integrable M (\<lambda>x. - (- f x))"
  1547     by (rule integral_minus)
  1548   then show "integrable M f" by simp
  1549 qed (rule integral_minus)
  1550 
  1551 lemma integral_of_positive_diff:
  1552   assumes integrable: "integrable M u" "integrable M v"
  1553   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"
  1554   shows "integrable M f" and "integral\<^isup>L M f = integral\<^isup>L M u - integral\<^isup>L M v"
  1555 proof -
  1556   let ?f = "\<lambda>x. max 0 (ereal (f x))"
  1557   let ?mf = "\<lambda>x. max 0 (ereal (- f x))"
  1558   let ?u = "\<lambda>x. max 0 (ereal (u x))"
  1559   let ?v = "\<lambda>x. max 0 (ereal (v x))"
  1560 
  1561   from borel_measurable_diff[of u M v] integrable
  1562   have f_borel: "?f \<in> borel_measurable M" and
  1563     mf_borel: "?mf \<in> borel_measurable M" and
  1564     v_borel: "?v \<in> borel_measurable M" and
  1565     u_borel: "?u \<in> borel_measurable M" and
  1566     "f \<in> borel_measurable M"
  1567     by (auto simp: f_def[symmetric] integrable_def)
  1568 
  1569   have "(\<integral>\<^isup>+ x. ereal (u x - v x) \<partial>M) \<le> integral\<^isup>P M ?u"
  1570     using pos by (auto intro!: positive_integral_mono simp: positive_integral_max_0)
  1571   moreover have "(\<integral>\<^isup>+ x. ereal (v x - u x) \<partial>M) \<le> integral\<^isup>P M ?v"
  1572     using pos by (auto intro!: positive_integral_mono simp: positive_integral_max_0)
  1573   ultimately show f: "integrable M f"
  1574     using `integrable M u` `integrable M v` `f \<in> borel_measurable M`
  1575     by (auto simp: integrable_def f_def positive_integral_max_0)
  1576 
  1577   have "\<And>x. ?u x + ?mf x = ?v x + ?f x"
  1578     unfolding f_def using pos by (simp split: split_max)
  1579   then have "(\<integral>\<^isup>+ x. ?u x + ?mf x \<partial>M) = (\<integral>\<^isup>+ x. ?v x + ?f x \<partial>M)" by simp
  1580   then have "real (integral\<^isup>P M ?u + integral\<^isup>P M ?mf) =
  1581       real (integral\<^isup>P M ?v + integral\<^isup>P M ?f)"
  1582     using positive_integral_add[OF u_borel _ mf_borel]
  1583     using positive_integral_add[OF v_borel _ f_borel]
  1584     by auto
  1585   then show "integral\<^isup>L M f = integral\<^isup>L M u - integral\<^isup>L M v"
  1586     unfolding positive_integral_max_0
  1587     unfolding pos[THEN integral_eq_positive_integral]
  1588     using integrable f by (auto elim!: integrableE)
  1589 qed
  1590 
  1591 lemma integral_linear:
  1592   assumes "integrable M f" "integrable M g" and "0 \<le> a"
  1593   shows "integrable M (\<lambda>t. a * f t + g t)"
  1594   and "(\<integral> t. a * f t + g t \<partial>M) = a * integral\<^isup>L M f + integral\<^isup>L M g" (is ?EQ)
  1595 proof -
  1596   let ?f = "\<lambda>x. max 0 (ereal (f x))"
  1597   let ?g = "\<lambda>x. max 0 (ereal (g x))"
  1598   let ?mf = "\<lambda>x. max 0 (ereal (- f x))"
  1599   let ?mg = "\<lambda>x. max 0 (ereal (- g x))"
  1600   let ?p = "\<lambda>t. max 0 (a * f t) + max 0 (g t)"
  1601   let ?n = "\<lambda>t. max 0 (- (a * f t)) + max 0 (- g t)"
  1602 
  1603   from assms have linear:
  1604     "(\<integral>\<^isup>+ x. ereal a * ?f x + ?g x \<partial>M) = ereal a * integral\<^isup>P M ?f + integral\<^isup>P M ?g"
  1605     "(\<integral>\<^isup>+ x. ereal a * ?mf x + ?mg x \<partial>M) = ereal a * integral\<^isup>P M ?mf + integral\<^isup>P M ?mg"
  1606     by (auto intro!: positive_integral_linear simp: integrable_def)
  1607 
  1608   have *: "(\<integral>\<^isup>+x. ereal (- ?p x) \<partial>M) = 0" "(\<integral>\<^isup>+x. ereal (- ?n x) \<partial>M) = 0"
  1609     using `0 \<le> a` assms by (auto simp: positive_integral_0_iff_AE integrable_def)
  1610   have **: "\<And>x. ereal a * ?f x + ?g x = max 0 (ereal (?p x))"
  1611            "\<And>x. ereal a * ?mf x + ?mg x = max 0 (ereal (?n x))"
  1612     using `0 \<le> a` by (auto split: split_max simp: zero_le_mult_iff mult_le_0_iff)
  1613 
  1614   have "integrable M ?p" "integrable M ?n"
  1615       "\<And>t. a * f t + g t = ?p t - ?n t" "\<And>t. 0 \<le> ?p t" "\<And>t. 0 \<le> ?n t"
  1616     using linear assms unfolding integrable_def ** *
  1617     by (auto simp: positive_integral_max_0)
  1618   note diff = integral_of_positive_diff[OF this]
  1619 
  1620   show "integrable M (\<lambda>t. a * f t + g t)" by (rule diff)
  1621   from assms linear show ?EQ
  1622     unfolding diff(2) ** positive_integral_max_0
  1623     unfolding lebesgue_integral_def *
  1624     by (auto elim!: integrableE simp: field_simps)
  1625 qed
  1626 
  1627 lemma integral_add[simp, intro]:
  1628   assumes "integrable M f" "integrable M g"
  1629   shows "integrable M (\<lambda>t. f t + g t)"
  1630   and "(\<integral> t. f t + g t \<partial>M) = integral\<^isup>L M f + integral\<^isup>L M g"
  1631   using assms integral_linear[where a=1] by auto
  1632 
  1633 lemma integral_zero[simp, intro]:
  1634   shows "integrable M (\<lambda>x. 0)" "(\<integral> x.0 \<partial>M) = 0"
  1635   unfolding integrable_def lebesgue_integral_def
  1636   by auto
  1637 
  1638 lemma integral_cmult[simp, intro]:
  1639   assumes "integrable M f"
  1640   shows "integrable M (\<lambda>t. a * f t)" (is ?P)
  1641   and "(\<integral> t. a * f t \<partial>M) = a * integral\<^isup>L M f" (is ?I)
  1642 proof -
  1643   have "integrable M (\<lambda>t. a * f t) \<and> (\<integral> t. a * f t \<partial>M) = a * integral\<^isup>L M f"
  1644   proof (cases rule: le_cases)
  1645     assume "0 \<le> a" show ?thesis
  1646       using integral_linear[OF assms integral_zero(1) `0 \<le> a`]
  1647       by simp
  1648   next
  1649     assume "a \<le> 0" hence "0 \<le> - a" by auto
  1650     have *: "\<And>t. - a * t + 0 = (-a) * t" by simp
  1651     show ?thesis using integral_linear[OF assms integral_zero(1) `0 \<le> - a`]
  1652         integral_minus(1)[of M "\<lambda>t. - a * f t"]
  1653       unfolding * integral_zero by simp
  1654   qed
  1655   thus ?P ?I by auto
  1656 qed
  1657 
  1658 lemma lebesgue_integral_cmult_nonneg:
  1659   assumes f: "f \<in> borel_measurable M" and "0 \<le> c"
  1660   shows "(\<integral>x. c * f x \<partial>M) = c * integral\<^isup>L M f"
  1661 proof -
  1662   { have "real (ereal c * integral\<^isup>P M (\<lambda>x. max 0 (ereal (f x)))) =
  1663       real (integral\<^isup>P M (\<lambda>x. ereal c * max 0 (ereal (f x))))"
  1664       using f `0 \<le> c` by (subst positive_integral_cmult) auto
  1665     also have "\<dots> = real (integral\<^isup>P M (\<lambda>x. max 0 (ereal (c * f x))))"
  1666       using `0 \<le> c` by (auto intro!: arg_cong[where f=real] positive_integral_cong simp: max_def zero_le_mult_iff)
  1667     finally have "real (integral\<^isup>P M (\<lambda>x. ereal (c * f x))) = c * real (integral\<^isup>P M (\<lambda>x. ereal (f x)))"
  1668       by (simp add: positive_integral_max_0) }
  1669   moreover
  1670   { have "real (ereal c * integral\<^isup>P M (\<lambda>x. max 0 (ereal (- f x)))) =
  1671       real (integral\<^isup>P M (\<lambda>x. ereal c * max 0 (ereal (- f x))))"
  1672       using f `0 \<le> c` by (subst positive_integral_cmult) auto
  1673     also have "\<dots> = real (integral\<^isup>P M (\<lambda>x. max 0 (ereal (- c * f x))))"
  1674       using `0 \<le> c` by (auto intro!: arg_cong[where f=real] positive_integral_cong simp: max_def mult_le_0_iff)
  1675     finally have "real (integral\<^isup>P M (\<lambda>x. ereal (- c * f x))) = c * real (integral\<^isup>P M (\<lambda>x. ereal (- f x)))"
  1676       by (simp add: positive_integral_max_0) }
  1677   ultimately show ?thesis
  1678     by (simp add: lebesgue_integral_def field_simps)
  1679 qed
  1680 
  1681 lemma lebesgue_integral_uminus:
  1682   "(\<integral>x. - f x \<partial>M) = - integral\<^isup>L M f"
  1683     unfolding lebesgue_integral_def by simp
  1684 
  1685 lemma lebesgue_integral_cmult:
  1686   assumes f: "f \<in> borel_measurable M"
  1687   shows "(\<integral>x. c * f x \<partial>M) = c * integral\<^isup>L M f"
  1688 proof (cases rule: linorder_le_cases)
  1689   assume "0 \<le> c" with f show ?thesis by (rule lebesgue_integral_cmult_nonneg)
  1690 next
  1691   assume "c \<le> 0"
  1692   with lebesgue_integral_cmult_nonneg[OF f, of "-c"]
  1693   show ?thesis
  1694     by (simp add: lebesgue_integral_def)
  1695 qed
  1696 
  1697 lemma integral_multc:
  1698   assumes "integrable M f"
  1699   shows "(\<integral> x. f x * c \<partial>M) = integral\<^isup>L M f * c"
  1700   unfolding mult_commute[of _ c] integral_cmult[OF assms] ..
  1701 
  1702 lemma integral_diff[simp, intro]:
  1703   assumes f: "integrable M f" and g: "integrable M g"
  1704   shows "integrable M (\<lambda>t. f t - g t)"
  1705   and "(\<integral> t. f t - g t \<partial>M) = integral\<^isup>L M f - integral\<^isup>L M g"
  1706   using integral_add[OF f integral_minus(1)[OF g]]
  1707   unfolding diff_minus integral_minus(2)[OF g]
  1708   by auto
  1709 
  1710 lemma integral_indicator[simp, intro]:
  1711   assumes "A \<in> sets M" and "(emeasure M) A \<noteq> \<infinity>"
  1712   shows "integral\<^isup>L M (indicator A) = real ((emeasure M) A)" (is ?int)
  1713   and "integrable M (indicator A)" (is ?able)
  1714 proof -
  1715   from `A \<in> sets M` have *:
  1716     "\<And>x. ereal (indicator A x) = indicator A x"
  1717     "(\<integral>\<^isup>+x. ereal (- indicator A x) \<partial>M) = 0"
  1718     by (auto split: split_indicator simp: positive_integral_0_iff_AE one_ereal_def)
  1719   show ?int ?able
  1720     using assms unfolding lebesgue_integral_def integrable_def
  1721     by (auto simp: *)
  1722 qed
  1723 
  1724 lemma integral_cmul_indicator:
  1725   assumes "A \<in> sets M" and "c \<noteq> 0 \<Longrightarrow> (emeasure M) A \<noteq> \<infinity>"
  1726   shows "integrable M (\<lambda>x. c * indicator A x)" (is ?P)
  1727   and "(\<integral>x. c * indicator A x \<partial>M) = c * real ((emeasure M) A)" (is ?I)
  1728 proof -
  1729   show ?P
  1730   proof (cases "c = 0")
  1731     case False with assms show ?thesis by simp
  1732   qed simp
  1733 
  1734   show ?I
  1735   proof (cases "c = 0")
  1736     case False with assms show ?thesis by simp
  1737   qed simp
  1738 qed
  1739 
  1740 lemma integral_setsum[simp, intro]:
  1741   assumes "\<And>n. n \<in> S \<Longrightarrow> integrable M (f n)"
  1742   shows "(\<integral>x. (\<Sum> i \<in> S. f i x) \<partial>M) = (\<Sum> i \<in> S. integral\<^isup>L M (f i))" (is "?int S")
  1743     and "integrable M (\<lambda>x. \<Sum> i \<in> S. f i x)" (is "?I S")
  1744 proof -
  1745   have "?int S \<and> ?I S"
  1746   proof (cases "finite S")
  1747     assume "finite S"
  1748     from this assms show ?thesis by (induct S) simp_all
  1749   qed simp
  1750   thus "?int S" and "?I S" by auto
  1751 qed
  1752 
  1753 lemma integrable_bound:
  1754   assumes "integrable M f" and f: "AE x in M. \<bar>g x\<bar> \<le> f x"
  1755   assumes borel: "g \<in> borel_measurable M"
  1756   shows "integrable M g"
  1757 proof -
  1758   have "(\<integral>\<^isup>+ x. ereal (g x) \<partial>M) \<le> (\<integral>\<^isup>+ x. ereal \<bar>g x\<bar> \<partial>M)"
  1759     by (auto intro!: positive_integral_mono)
  1760   also have "\<dots> \<le> (\<integral>\<^isup>+ x. ereal (f x) \<partial>M)"
  1761     using f by (auto intro!: positive_integral_mono_AE)
  1762   also have "\<dots> < \<infinity>"
  1763     using `integrable M f` unfolding integrable_def by auto
  1764   finally have pos: "(\<integral>\<^isup>+ x. ereal (g x) \<partial>M) < \<infinity>" .
  1765 
  1766   have "(\<integral>\<^isup>+ x. ereal (- g x) \<partial>M) \<le> (\<integral>\<^isup>+ x. ereal (\<bar>g x\<bar>) \<partial>M)"
  1767     by (auto intro!: positive_integral_mono)
  1768   also have "\<dots> \<le> (\<integral>\<^isup>+ x. ereal (f x) \<partial>M)"
  1769     using f by (auto intro!: positive_integral_mono_AE)
  1770   also have "\<dots> < \<infinity>"
  1771     using `integrable M f` unfolding integrable_def by auto
  1772   finally have neg: "(\<integral>\<^isup>+ x. ereal (- g x) \<partial>M) < \<infinity>" .
  1773 
  1774   from neg pos borel show ?thesis
  1775     unfolding integrable_def by auto
  1776 qed
  1777 
  1778 lemma integrable_abs:
  1779   assumes f: "integrable M f"
  1780   shows "integrable M (\<lambda> x. \<bar>f x\<bar>)"
  1781 proof -
  1782   from assms have *: "(\<integral>\<^isup>+x. ereal (- \<bar>f x\<bar>)\<partial>M) = 0"
  1783     "\<And>x. ereal \<bar>f x\<bar> = max 0 (ereal (f x)) + max 0 (ereal (- f x))"
  1784     by (auto simp: integrable_def positive_integral_0_iff_AE split: split_max)
  1785   with assms show ?thesis
  1786     by (simp add: positive_integral_add positive_integral_max_0 integrable_def)
  1787 qed
  1788 
  1789 lemma integral_subalgebra:
  1790   assumes borel: "f \<in> borel_measurable N"
  1791   and N: "sets N \<subseteq> sets M" "space N = space M" "\<And>A. A \<in> sets N \<Longrightarrow> emeasure N A = emeasure M A"
  1792   shows "integrable N f \<longleftrightarrow> integrable M f" (is ?P)
  1793     and "integral\<^isup>L N f = integral\<^isup>L M f" (is ?I)
  1794 proof -
  1795   have "(\<integral>\<^isup>+ x. max 0 (ereal (f x)) \<partial>N) = (\<integral>\<^isup>+ x. max 0 (ereal (f x)) \<partial>M)"
  1796        "(\<integral>\<^isup>+ x. max 0 (ereal (- f x)) \<partial>N) = (\<integral>\<^isup>+ x. max 0 (ereal (- f x)) \<partial>M)"
  1797     using borel by (auto intro!: positive_integral_subalgebra N)
  1798   moreover have "f \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable N"
  1799     using assms unfolding measurable_def by auto
  1800   ultimately show ?P ?I
  1801     by (auto simp: integrable_def lebesgue_integral_def positive_integral_max_0)
  1802 qed
  1803 
  1804 lemma lebesgue_integral_nonneg:
  1805   assumes ae: "(AE x in M. 0 \<le> f x)" shows "0 \<le> integral\<^isup>L M f"
  1806 proof -
  1807   have "(\<integral>\<^isup>+x. max 0 (ereal (- f x)) \<partial>M) = (\<integral>\<^isup>+x. 0 \<partial>M)"
  1808     using ae by (intro positive_integral_cong_AE) (auto simp: max_def)
  1809   then show ?thesis
  1810     by (auto simp: lebesgue_integral_def positive_integral_max_0
  1811              intro!: real_of_ereal_pos positive_integral_positive)
  1812 qed
  1813 
  1814 lemma integrable_abs_iff:
  1815   "f \<in> borel_measurable M \<Longrightarrow> integrable M (\<lambda> x. \<bar>f x\<bar>) \<longleftrightarrow> integrable M f"
  1816   by (auto intro!: integrable_bound[where g=f] integrable_abs)
  1817 
  1818 lemma integrable_max:
  1819   assumes int: "integrable M f" "integrable M g"
  1820   shows "integrable M (\<lambda> x. max (f x) (g x))"
  1821 proof (rule integrable_bound)
  1822   show "integrable M (\<lambda>x. \<bar>f x\<bar> + \<bar>g x\<bar>)"
  1823     using int by (simp add: integrable_abs)
  1824   show "(\<lambda>x. max (f x) (g x)) \<in> borel_measurable M"
  1825     using int unfolding integrable_def by auto
  1826 qed auto
  1827 
  1828 lemma integrable_min:
  1829   assumes int: "integrable M f" "integrable M g"
  1830   shows "integrable M (\<lambda> x. min (f x) (g x))"
  1831 proof (rule integrable_bound)
  1832   show "integrable M (\<lambda>x. \<bar>f x\<bar> + \<bar>g x\<bar>)"
  1833     using int by (simp add: integrable_abs)
  1834   show "(\<lambda>x. min (f x) (g x)) \<in> borel_measurable M"
  1835     using int unfolding integrable_def by auto
  1836 qed auto
  1837 
  1838 lemma integral_triangle_inequality:
  1839   assumes "integrable M f"
  1840   shows "\<bar>integral\<^isup>L M f\<bar> \<le> (\<integral>x. \<bar>f x\<bar> \<partial>M)"
  1841 proof -
  1842   have "\<bar>integral\<^isup>L M f\<bar> = max (integral\<^isup>L M f) (- integral\<^isup>L M f)" by auto
  1843   also have "\<dots> \<le> (\<integral>x. \<bar>f x\<bar> \<partial>M)"
  1844       using assms integral_minus(2)[of M f, symmetric]
  1845       by (auto intro!: integral_mono integrable_abs simp del: integral_minus)
  1846   finally show ?thesis .
  1847 qed
  1848 
  1849 lemma integral_positive:
  1850   assumes "integrable M f" "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
  1851   shows "0 \<le> integral\<^isup>L M f"
  1852 proof -
  1853   have "0 = (\<integral>x. 0 \<partial>M)" by auto
  1854   also have "\<dots> \<le> integral\<^isup>L M f"
  1855     using assms by (rule integral_mono[OF integral_zero(1)])
  1856   finally show ?thesis .
  1857 qed
  1858 
  1859 lemma integral_monotone_convergence_pos:
  1860   assumes i: "\<And>i. integrable M (f i)" and mono: "AE x in M. mono (\<lambda>n. f n x)"
  1861     and pos: "\<And>i. AE x in M. 0 \<le> f i x"
  1862     and lim: "AE x in M. (\<lambda>i. f i x) ----> u x"
  1863     and ilim: "(\<lambda>i. integral\<^isup>L M (f i)) ----> x"
  1864     and u: "u \<in> borel_measurable M"
  1865   shows "integrable M u"
  1866   and "integral\<^isup>L M u = x"
  1867 proof -
  1868   have "(\<integral>\<^isup>+ x. ereal (u x) \<partial>M) = (SUP n. (\<integral>\<^isup>+ x. ereal (f n x) \<partial>M))"
  1869   proof (subst positive_integral_monotone_convergence_SUP_AE[symmetric])
  1870     fix i
  1871     from mono pos show "AE x in M. ereal (f i x) \<le> ereal (f (Suc i) x) \<and> 0 \<le> ereal (f i x)"
  1872       by eventually_elim (auto simp: mono_def)
  1873     show "(\<lambda>x. ereal (f i x)) \<in> borel_measurable M"
  1874       using i by (auto simp: integrable_def)
  1875   next
  1876     show "(\<integral>\<^isup>+ x. ereal (u x) \<partial>M) = \<integral>\<^isup>+ x. (SUP i. ereal (f i x)) \<partial>M"
  1877       apply (rule positive_integral_cong_AE)
  1878       using lim mono
  1879       by eventually_elim (simp add: SUP_eq_LIMSEQ[THEN iffD2])
  1880   qed
  1881   also have "\<dots> = ereal x"
  1882     using mono i unfolding positive_integral_eq_integral[OF i pos]
  1883     by (subst SUP_eq_LIMSEQ) (auto simp: mono_def intro!: integral_mono_AE ilim)
  1884   finally have "(\<integral>\<^isup>+ x. ereal (u x) \<partial>M) = ereal x" .
  1885   moreover have "(\<integral>\<^isup>+ x. ereal (- u x) \<partial>M) = 0"
  1886   proof (subst positive_integral_0_iff_AE)
  1887     show "(\<lambda>x. ereal (- u x)) \<in> borel_measurable M"
  1888       using u by auto
  1889     from mono pos[of 0] lim show "AE x in M. ereal (- u x) \<le> 0"
  1890     proof eventually_elim
  1891       fix x assume "mono (\<lambda>n. f n x)" "0 \<le> f 0 x" "(\<lambda>i. f i x) ----> u x"
  1892       then show "ereal (- u x) \<le> 0"
  1893         using incseq_le[of "\<lambda>n. f n x" "u x" 0] by (simp add: mono_def incseq_def)
  1894     qed
  1895   qed
  1896   ultimately show "integrable M u" "integral\<^isup>L M u = x"
  1897     by (auto simp: integrable_def lebesgue_integral_def u)
  1898 qed
  1899 
  1900 lemma integral_monotone_convergence:
  1901   assumes f: "\<And>i. integrable M (f i)" and mono: "AE x in M. mono (\<lambda>n. f n x)"
  1902   and lim: "AE x in M. (\<lambda>i. f i x) ----> u x"
  1903   and ilim: "(\<lambda>i. integral\<^isup>L M (f i)) ----> x"
  1904   and u: "u \<in> borel_measurable M"
  1905   shows "integrable M u"
  1906   and "integral\<^isup>L M u = x"
  1907 proof -
  1908   have 1: "\<And>i. integrable M (\<lambda>x. f i x - f 0 x)"
  1909     using f by auto
  1910   have 2: "AE x in M. mono (\<lambda>n. f n x - f 0 x)"
  1911     using mono by (auto simp: mono_def le_fun_def)
  1912   have 3: "\<And>n. AE x in M. 0 \<le> f n x - f 0 x"
  1913     using mono by (auto simp: field_simps mono_def le_fun_def)
  1914   have 4: "AE x in M. (\<lambda>i. f i x - f 0 x) ----> u x - f 0 x"
  1915     using lim by (auto intro!: tendsto_diff)
  1916   have 5: "(\<lambda>i. (\<integral>x. f i x - f 0 x \<partial>M)) ----> x - integral\<^isup>L M (f 0)"
  1917     using f ilim by (auto intro!: tendsto_diff)
  1918   have 6: "(\<lambda>x. u x - f 0 x) \<in> borel_measurable M"
  1919     using f[of 0] u by auto
  1920   note diff = integral_monotone_convergence_pos[OF 1 2 3 4 5 6]
  1921   have "integrable M (\<lambda>x. (u x - f 0 x) + f 0 x)"
  1922     using diff(1) f by (rule integral_add(1))
  1923   with diff(2) f show "integrable M u" "integral\<^isup>L M u = x"
  1924     by auto
  1925 qed
  1926 
  1927 lemma integral_0_iff:
  1928   assumes "integrable M f"
  1929   shows "(\<integral>x. \<bar>f x\<bar> \<partial>M) = 0 \<longleftrightarrow> (emeasure M) {x\<in>space M. f x \<noteq> 0} = 0"
  1930 proof -
  1931   have *: "(\<integral>\<^isup>+x. ereal (- \<bar>f x\<bar>) \<partial>M) = 0"
  1932     using assms by (auto simp: positive_integral_0_iff_AE integrable_def)
  1933   have "integrable M (\<lambda>x. \<bar>f x\<bar>)" using assms by (rule integrable_abs)
  1934   hence "(\<lambda>x. ereal (\<bar>f x\<bar>)) \<in> borel_measurable M"
  1935     "(\<integral>\<^isup>+ x. ereal \<bar>f x\<bar> \<partial>M) \<noteq> \<infinity>" unfolding integrable_def by auto
  1936   from positive_integral_0_iff[OF this(1)] this(2)
  1937   show ?thesis unfolding lebesgue_integral_def *
  1938     using positive_integral_positive[of M "\<lambda>x. ereal \<bar>f x\<bar>"]
  1939     by (auto simp add: real_of_ereal_eq_0)
  1940 qed
  1941 
  1942 lemma positive_integral_PInf:
  1943   assumes f: "f \<in> borel_measurable M"
  1944   and not_Inf: "integral\<^isup>P M f \<noteq> \<infinity>"
  1945   shows "(emeasure M) (f -` {\<infinity>} \<inter> space M) = 0"
  1946 proof -
  1947   have "\<infinity> * (emeasure M) (f -` {\<infinity>} \<inter> space M) = (\<integral>\<^isup>+ x. \<infinity> * indicator (f -` {\<infinity>} \<inter> space M) x \<partial>M)"
  1948     using f by (subst positive_integral_cmult_indicator) (auto simp: measurable_sets)
  1949   also have "\<dots> \<le> integral\<^isup>P M (\<lambda>x. max 0 (f x))"
  1950     by (auto intro!: positive_integral_mono simp: indicator_def max_def)
  1951   finally have "\<infinity> * (emeasure M) (f -` {\<infinity>} \<inter> space M) \<le> integral\<^isup>P M f"
  1952     by (simp add: positive_integral_max_0)
  1953   moreover have "0 \<le> (emeasure M) (f -` {\<infinity>} \<inter> space M)"
  1954     by (rule emeasure_nonneg)
  1955   ultimately show ?thesis
  1956     using assms by (auto split: split_if_asm)
  1957 qed
  1958 
  1959 lemma positive_integral_PInf_AE:
  1960   assumes "f \<in> borel_measurable M" "integral\<^isup>P M f \<noteq> \<infinity>" shows "AE x in M. f x \<noteq> \<infinity>"
  1961 proof (rule AE_I)
  1962   show "(emeasure M) (f -` {\<infinity>} \<inter> space M) = 0"
  1963     by (rule positive_integral_PInf[OF assms])
  1964   show "f -` {\<infinity>} \<inter> space M \<in> sets M"
  1965     using assms by (auto intro: borel_measurable_vimage)
  1966 qed auto
  1967 
  1968 lemma simple_integral_PInf:
  1969   assumes "simple_function M f" "\<And>x. 0 \<le> f x"
  1970   and "integral\<^isup>S M f \<noteq> \<infinity>"
  1971   shows "(emeasure M) (f -` {\<infinity>} \<inter> space M) = 0"
  1972 proof (rule positive_integral_PInf)
  1973   show "f \<in> borel_measurable M" using assms by (auto intro: borel_measurable_simple_function)
  1974   show "integral\<^isup>P M f \<noteq> \<infinity>"
  1975     using assms by (simp add: positive_integral_eq_simple_integral)
  1976 qed
  1977 
  1978 lemma integral_real:
  1979   "AE x in M. \<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))"
  1980   using assms unfolding lebesgue_integral_def
  1981   by (subst (1 2) positive_integral_cong_AE) (auto simp add: ereal_real)
  1982 
  1983 lemma (in finite_measure) lebesgue_integral_const[simp]:
  1984   shows "integrable M (\<lambda>x. a)"
  1985   and  "(\<integral>x. a \<partial>M) = a * (measure M) (space M)"
  1986 proof -
  1987   { fix a :: real assume "0 \<le> a"
  1988     then have "(\<integral>\<^isup>+ x. ereal a \<partial>M) = ereal a * (emeasure M) (space M)"
  1989       by (subst positive_integral_const) auto
  1990     moreover
  1991     from `0 \<le> a` have "(\<integral>\<^isup>+ x. ereal (-a) \<partial>M) = 0"
  1992       by (subst positive_integral_0_iff_AE) auto
  1993     ultimately have "integrable M (\<lambda>x. a)" by (auto simp: integrable_def) }
  1994   note * = this
  1995   show "integrable M (\<lambda>x. a)"
  1996   proof cases
  1997     assume "0 \<le> a" with * show ?thesis .
  1998   next
  1999     assume "\<not> 0 \<le> a"
  2000     then have "0 \<le> -a" by auto
  2001     from *[OF this] show ?thesis by simp
  2002   qed
  2003   show "(\<integral>x. a \<partial>M) = a * measure M (space M)"
  2004     by (simp add: lebesgue_integral_def positive_integral_const_If emeasure_eq_measure)
  2005 qed
  2006 
  2007 lemma indicator_less[simp]:
  2008   "indicator A x \<le> (indicator B x::ereal) \<longleftrightarrow> (x \<in> A \<longrightarrow> x \<in> B)"
  2009   by (simp add: indicator_def not_le)
  2010 
  2011 lemma (in finite_measure) integral_less_AE:
  2012   assumes int: "integrable M X" "integrable M Y"
  2013   assumes A: "(emeasure M) A \<noteq> 0" "A \<in> sets M" "AE x in M. x \<in> A \<longrightarrow> X x \<noteq> Y x"
  2014   assumes gt: "AE x in M. X x \<le> Y x"
  2015   shows "integral\<^isup>L M X < integral\<^isup>L M Y"
  2016 proof -
  2017   have "integral\<^isup>L M X \<le> integral\<^isup>L M Y"
  2018     using gt int by (intro integral_mono_AE) auto
  2019   moreover
  2020   have "integral\<^isup>L M X \<noteq> integral\<^isup>L M Y"
  2021   proof
  2022     assume eq: "integral\<^isup>L M X = integral\<^isup>L M Y"
  2023     have "integral\<^isup>L M (\<lambda>x. \<bar>Y x - X x\<bar>) = integral\<^isup>L M (\<lambda>x. Y x - X x)"
  2024       using gt by (intro integral_cong_AE) auto
  2025     also have "\<dots> = 0"
  2026       using eq int by simp
  2027     finally have "(emeasure M) {x \<in> space M. Y x - X x \<noteq> 0} = 0"
  2028       using int by (simp add: integral_0_iff)
  2029     moreover
  2030     have "(\<integral>\<^isup>+x. indicator A x \<partial>M) \<le> (\<integral>\<^isup>+x. indicator {x \<in> space M. Y x - X x \<noteq> 0} x \<partial>M)"
  2031       using A by (intro positive_integral_mono_AE) auto
  2032     then have "(emeasure M) A \<le> (emeasure M) {x \<in> space M. Y x - X x \<noteq> 0}"
  2033       using int A by (simp add: integrable_def)
  2034     ultimately have "emeasure M A = 0"
  2035       using emeasure_nonneg[of M A] by simp
  2036     with `(emeasure M) A \<noteq> 0` show False by auto
  2037   qed
  2038   ultimately show ?thesis by auto
  2039 qed
  2040 
  2041 lemma (in finite_measure) integral_less_AE_space:
  2042   assumes int: "integrable M X" "integrable M Y"
  2043   assumes gt: "AE x in M. X x < Y x" "(emeasure M) (space M) \<noteq> 0"
  2044   shows "integral\<^isup>L M X < integral\<^isup>L M Y"
  2045   using gt by (intro integral_less_AE[OF int, where A="space M"]) auto
  2046 
  2047 lemma integral_dominated_convergence:
  2048   assumes u: "\<And>i. integrable M (u i)" and bound: "\<And>j. AE x in M. \<bar>u j x\<bar> \<le> w x"
  2049   and w: "integrable M w"
  2050   and u': "AE x in M. (\<lambda>i. u i x) ----> u' x"
  2051   and borel: "u' \<in> borel_measurable M"
  2052   shows "integrable M u'"
  2053   and "(\<lambda>i. (\<integral>x. \<bar>u i x - u' x\<bar> \<partial>M)) ----> 0" (is "?lim_diff")
  2054   and "(\<lambda>i. integral\<^isup>L M (u i)) ----> integral\<^isup>L M u'" (is ?lim)
  2055 proof -
  2056   have all_bound: "AE x in M. \<forall>j. \<bar>u j x\<bar> \<le> w x"
  2057     using bound by (auto simp: AE_all_countable)
  2058   with u' have u'_bound: "AE x in M. \<bar>u' x\<bar> \<le> w x"
  2059     by eventually_elim (auto intro: LIMSEQ_le_const2 tendsto_rabs)
  2060 
  2061   from bound[of 0] have w_pos: "AE x in M. 0 \<le> w x"
  2062     by eventually_elim auto
  2063 
  2064   show "integrable M u'"
  2065     by (rule integrable_bound) fact+
  2066 
  2067   let ?diff = "\<lambda>n x. 2 * w x - \<bar>u n x - u' x\<bar>"
  2068   have diff: "\<And>n. integrable M (\<lambda>x. \<bar>u n x - u' x\<bar>)"
  2069     using w u `integrable M u'` by (auto intro!: integrable_abs)
  2070 
  2071   from u'_bound all_bound
  2072   have diff_less_2w: "AE x in M. \<forall>j. \<bar>u j x - u' x\<bar> \<le> 2 * w x"
  2073   proof (eventually_elim, intro allI)
  2074     fix x j assume *: "\<bar>u' x\<bar> \<le> w x" "\<forall>j. \<bar>u j x\<bar> \<le> w x"
  2075     then have "\<bar>u j x - u' x\<bar> \<le> \<bar>u j x\<bar> + \<bar>u' x\<bar>" by auto
  2076     also have "\<dots> \<le> w x + w x"
  2077       using * by (intro add_mono) auto
  2078     finally show "\<bar>u j x - u' x\<bar> \<le> 2 * w x" by simp
  2079   qed
  2080 
  2081   have PI_diff: "\<And>n. (\<integral>\<^isup>+ x. ereal (?diff n x) \<partial>M) =
  2082     (\<integral>\<^isup>+ x. ereal (2 * w x) \<partial>M) - (\<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)"
  2083     using diff w diff_less_2w w_pos
  2084     by (subst positive_integral_diff[symmetric])
  2085        (auto simp: integrable_def intro!: positive_integral_cong_AE)
  2086 
  2087   have "integrable M (\<lambda>x. 2 * w x)"
  2088     using w by auto
  2089   hence I2w_fin: "(\<integral>\<^isup>+ x. ereal (2 * w x) \<partial>M) \<noteq> \<infinity>" and
  2090     borel_2w: "(\<lambda>x. ereal (2 * w x)) \<in> borel_measurable M"
  2091     unfolding integrable_def by auto
  2092 
  2093   have "limsup (\<lambda>n. \<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M) = 0" (is "limsup ?f = 0")
  2094   proof cases
  2095     assume eq_0: "(\<integral>\<^isup>+ x. max 0 (ereal (2 * w x)) \<partial>M) = 0" (is "?wx = 0")
  2096     { fix n
  2097       have "?f n \<le> ?wx" (is "integral\<^isup>P M ?f' \<le> _")
  2098         using diff_less_2w unfolding positive_integral_max_0
  2099         by (intro positive_integral_mono_AE) auto
  2100       then have "?f n = 0"
  2101         using positive_integral_positive[of M ?f'] eq_0 by auto }
  2102     then show ?thesis by (simp add: Limsup_const)
  2103   next
  2104     assume neq_0: "(\<integral>\<^isup>+ x. max 0 (ereal (2 * w x)) \<partial>M) \<noteq> 0" (is "?wx \<noteq> 0")
  2105     have "0 = limsup (\<lambda>n. 0 :: ereal)" by (simp add: Limsup_const)
  2106     also have "\<dots> \<le> limsup (\<lambda>n. \<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)"
  2107       by (intro limsup_mono positive_integral_positive)
  2108     finally have pos: "0 \<le> limsup (\<lambda>n. \<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)" .
  2109     have "?wx = (\<integral>\<^isup>+ x. liminf (\<lambda>n. max 0 (ereal (?diff n x))) \<partial>M)"
  2110       using u'
  2111     proof (intro positive_integral_cong_AE, eventually_elim)
  2112       fix x assume u': "(\<lambda>i. u i x) ----> u' x"
  2113       show "max 0 (ereal (2 * w x)) = liminf (\<lambda>n. max 0 (ereal (?diff n x)))"
  2114         unfolding ereal_max_0
  2115       proof (rule lim_imp_Liminf[symmetric], unfold lim_ereal)
  2116         have "(\<lambda>i. ?diff i x) ----> 2 * w x - \<bar>u' x - u' x\<bar>"
  2117           using u' by (safe intro!: tendsto_intros)
  2118         then show "(\<lambda>i. max 0 (?diff i x)) ----> max 0 (2 * w x)"
  2119           by (auto intro!: tendsto_real_max)
  2120       qed (rule trivial_limit_sequentially)
  2121     qed
  2122     also have "\<dots> \<le> liminf (\<lambda>n. \<integral>\<^isup>+ x. max 0 (ereal (?diff n x)) \<partial>M)"
  2123       using borel w u unfolding integrable_def
  2124       by (intro positive_integral_lim_INF) (auto intro!: positive_integral_lim_INF)
  2125     also have "\<dots> = (\<integral>\<^isup>+ x. ereal (2 * w x) \<partial>M) -
  2126         limsup (\<lambda>n. \<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)"
  2127       unfolding PI_diff positive_integral_max_0
  2128       using positive_integral_positive[of M "\<lambda>x. ereal (2 * w x)"]
  2129       by (subst liminf_ereal_cminus) auto
  2130     finally show ?thesis
  2131       using neq_0 I2w_fin positive_integral_positive[of M "\<lambda>x. ereal (2 * w x)"] pos
  2132       unfolding positive_integral_max_0
  2133       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)"])
  2134          auto
  2135   qed
  2136 
  2137   have "liminf ?f \<le> limsup ?f"
  2138     by (intro ereal_Liminf_le_Limsup trivial_limit_sequentially)
  2139   moreover
  2140   { have "0 = liminf (\<lambda>n. 0 :: ereal)" by (simp add: Liminf_const)
  2141     also have "\<dots> \<le> liminf ?f"
  2142       by (intro liminf_mono positive_integral_positive)
  2143     finally have "0 \<le> liminf ?f" . }
  2144   ultimately have liminf_limsup_eq: "liminf ?f = ereal 0" "limsup ?f = ereal 0"
  2145     using `limsup ?f = 0` by auto
  2146   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)"
  2147     using diff positive_integral_positive[of M]
  2148     by (subst integral_eq_positive_integral[of _ M]) (auto simp: ereal_real integrable_def)
  2149   then show ?lim_diff
  2150     using ereal_Liminf_eq_Limsup[OF trivial_limit_sequentially liminf_limsup_eq]
  2151     by simp
  2152 
  2153   show ?lim
  2154   proof (rule LIMSEQ_I)
  2155     fix r :: real assume "0 < r"
  2156     from LIMSEQ_D[OF `?lim_diff` this]
  2157     obtain N where N: "\<And>n. N \<le> n \<Longrightarrow> (\<integral>x. \<bar>u n x - u' x\<bar> \<partial>M) < r"
  2158       using diff by (auto simp: integral_positive)
  2159 
  2160     show "\<exists>N. \<forall>n\<ge>N. norm (integral\<^isup>L M (u n) - integral\<^isup>L M u') < r"
  2161     proof (safe intro!: exI[of _ N])
  2162       fix n assume "N \<le> n"
  2163       have "\<bar>integral\<^isup>L M (u n) - integral\<^isup>L M u'\<bar> = \<bar>(\<integral>x. u n x - u' x \<partial>M)\<bar>"
  2164         using u `integrable M u'` by auto
  2165       also have "\<dots> \<le> (\<integral>x. \<bar>u n x - u' x\<bar> \<partial>M)" using u `integrable M u'`
  2166         by (rule_tac integral_triangle_inequality) auto
  2167       also note N[OF `N \<le> n`]
  2168       finally show "norm (integral\<^isup>L M (u n) - integral\<^isup>L M u') < r" by simp
  2169     qed
  2170   qed
  2171 qed
  2172 
  2173 lemma integral_sums:
  2174   assumes borel: "\<And>i. integrable M (f i)"
  2175   and summable: "\<And>x. x \<in> space M \<Longrightarrow> summable (\<lambda>i. \<bar>f i x\<bar>)"
  2176   and sums: "summable (\<lambda>i. (\<integral>x. \<bar>f i x\<bar> \<partial>M))"
  2177   shows "integrable M (\<lambda>x. (\<Sum>i. f i x))" (is "integrable M ?S")
  2178   and "(\<lambda>i. integral\<^isup>L M (f i)) sums (\<integral>x. (\<Sum>i. f i x) \<partial>M)" (is ?integral)
  2179 proof -
  2180   have "\<forall>x\<in>space M. \<exists>w. (\<lambda>i. \<bar>f i x\<bar>) sums w"
  2181     using summable unfolding summable_def by auto
  2182   from bchoice[OF this]
  2183   obtain w where w: "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. \<bar>f i x\<bar>) sums w x" by auto
  2184   then have w_borel: "w \<in> borel_measurable M" unfolding sums_def
  2185     by (rule borel_measurable_LIMSEQ) (auto simp: borel[THEN integrableD(1)])
  2186 
  2187   let ?w = "\<lambda>y. if y \<in> space M then w y else 0"
  2188 
  2189   obtain x where abs_sum: "(\<lambda>i. (\<integral>x. \<bar>f i x\<bar> \<partial>M)) sums x"
  2190     using sums unfolding summable_def ..
  2191 
  2192   have 1: "\<And>n. integrable M (\<lambda>x. \<Sum>i = 0..<n. f i x)"
  2193     using borel by auto
  2194 
  2195   have 2: "\<And>j. AE x in M. \<bar>\<Sum>i = 0..<j. f i x\<bar> \<le> ?w x"
  2196     using AE_space
  2197   proof eventually_elim
  2198     fix j x assume [simp]: "x \<in> space M"
  2199     have "\<bar>\<Sum>i = 0..< j. f i x\<bar> \<le> (\<Sum>i = 0..< j. \<bar>f i x\<bar>)" by (rule setsum_abs)
  2200     also have "\<dots> \<le> w x" using w[of x] series_pos_le[of "\<lambda>i. \<bar>f i x\<bar>"] unfolding sums_iff by auto
  2201     finally show "\<bar>\<Sum>i = 0..<j. f i x\<bar> \<le> ?w x" by simp
  2202   qed
  2203 
  2204   have 3: "integrable M ?w"
  2205   proof (rule integral_monotone_convergence(1))
  2206     let ?F = "\<lambda>n y. (\<Sum>i = 0..<n. \<bar>f i y\<bar>)"
  2207     let ?w' = "\<lambda>n y. if y \<in> space M then ?F n y else 0"
  2208     have "\<And>n. integrable M (?F n)"
  2209       using borel by (auto intro!: integrable_abs)
  2210     thus "\<And>n. integrable M (?w' n)" by (simp cong: integrable_cong)
  2211     show "AE x in M. mono (\<lambda>n. ?w' n x)"
  2212       by (auto simp: mono_def le_fun_def intro!: setsum_mono2)
  2213     show "AE x in M. (\<lambda>n. ?w' n x) ----> ?w x"
  2214         using w by (simp_all add: tendsto_const sums_def)
  2215     have *: "\<And>n. integral\<^isup>L M (?w' n) = (\<Sum>i = 0..< n. (\<integral>x. \<bar>f i x\<bar> \<partial>M))"
  2216       using borel by (simp add: integrable_abs cong: integral_cong)
  2217     from abs_sum
  2218     show "(\<lambda>i. integral\<^isup>L M (?w' i)) ----> x" unfolding * sums_def .
  2219   qed (simp add: w_borel measurable_If_set)
  2220 
  2221   from summable[THEN summable_rabs_cancel]
  2222   have 4: "AE x in M. (\<lambda>n. \<Sum>i = 0..<n. f i x) ----> (\<Sum>i. f i x)"
  2223     by (auto intro: summable_sumr_LIMSEQ_suminf)
  2224 
  2225   note int = integral_dominated_convergence(1,3)[OF 1 2 3 4
  2226     borel_measurable_suminf[OF integrableD(1)[OF borel]]]
  2227 
  2228   from int show "integrable M ?S" by simp
  2229 
  2230   show ?integral unfolding sums_def integral_setsum(1)[symmetric, OF borel]
  2231     using int(2) by simp
  2232 qed
  2233 
  2234 section "Lebesgue integration on countable spaces"
  2235 
  2236 lemma integral_on_countable:
  2237   assumes f: "f \<in> borel_measurable M"
  2238   and bij: "bij_betw enum S (f ` space M)"
  2239   and enum_zero: "enum ` (-S) \<subseteq> {0}"
  2240   and fin: "\<And>x. x \<noteq> 0 \<Longrightarrow> (emeasure M) (f -` {x} \<inter> space M) \<noteq> \<infinity>"
  2241   and abs_summable: "summable (\<lambda>r. \<bar>enum r * real ((emeasure M) (f -` {enum r} \<inter> space M))\<bar>)"
  2242   shows "integrable M f"
  2243   and "(\<lambda>r. enum r * real ((emeasure M) (f -` {enum r} \<inter> space M))) sums integral\<^isup>L M f" (is ?sums)
  2244 proof -
  2245   let ?A = "\<lambda>r. f -` {enum r} \<inter> space M"
  2246   let ?F = "\<lambda>r x. enum r * indicator (?A r) x"
  2247   have enum_eq: "\<And>r. enum r * real ((emeasure M) (?A r)) = integral\<^isup>L M (?F r)"
  2248     using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
  2249 
  2250   { fix x assume "x \<in> space M"
  2251     hence "f x \<in> enum ` S" using bij unfolding bij_betw_def by auto
  2252     then obtain i where "i\<in>S" "enum i = f x" by auto
  2253     have F: "\<And>j. ?F j x = (if j = i then f x else 0)"
  2254     proof cases
  2255       fix j assume "j = i"
  2256       thus "?thesis j" using `x \<in> space M` `enum i = f x` by (simp add: indicator_def)
  2257     next
  2258       fix j assume "j \<noteq> i"
  2259       show "?thesis j" using bij `i \<in> S` `j \<noteq> i` `enum i = f x` enum_zero
  2260         by (cases "j \<in> S") (auto simp add: indicator_def bij_betw_def inj_on_def)
  2261     qed
  2262     hence F_abs: "\<And>j. \<bar>if j = i then f x else 0\<bar> = (if j = i then \<bar>f x\<bar> else 0)" by auto
  2263     have "(\<lambda>i. ?F i x) sums f x"
  2264          "(\<lambda>i. \<bar>?F i x\<bar>) sums \<bar>f x\<bar>"
  2265       by (auto intro!: sums_single simp: F F_abs) }
  2266   note F_sums_f = this(1) and F_abs_sums_f = this(2)
  2267 
  2268   have int_f: "integral\<^isup>L M f = (\<integral>x. (\<Sum>r. ?F r x) \<partial>M)" "integrable M f = integrable M (\<lambda>x. \<Sum>r. ?F r x)"
  2269     using F_sums_f by (auto intro!: integral_cong integrable_cong simp: sums_iff)
  2270 
  2271   { fix r
  2272     have "(\<integral>x. \<bar>?F r x\<bar> \<partial>M) = (\<integral>x. \<bar>enum r\<bar> * indicator (?A r) x \<partial>M)"
  2273       by (auto simp: indicator_def intro!: integral_cong)
  2274     also have "\<dots> = \<bar>enum r\<bar> * real ((emeasure M) (?A r))"
  2275       using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
  2276     finally have "(\<integral>x. \<bar>?F r x\<bar> \<partial>M) = \<bar>enum r * real ((emeasure M) (?A r))\<bar>"
  2277       using f by (subst (2) abs_mult_pos[symmetric]) (auto intro!: real_of_ereal_pos measurable_sets) }
  2278   note int_abs_F = this
  2279 
  2280   have 1: "\<And>i. integrable M (\<lambda>x. ?F i x)"
  2281     using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
  2282 
  2283   have 2: "\<And>x. x \<in> space M \<Longrightarrow> summable (\<lambda>i. \<bar>?F i x\<bar>)"
  2284     using F_abs_sums_f unfolding sums_iff by auto
  2285 
  2286   from integral_sums(2)[OF 1 2, unfolded int_abs_F, OF _ abs_summable]
  2287   show ?sums unfolding enum_eq int_f by simp
  2288 
  2289   from integral_sums(1)[OF 1 2, unfolded int_abs_F, OF _ abs_summable]
  2290   show "integrable M f" unfolding int_f by simp
  2291 qed
  2292 
  2293 section {* Distributions *}
  2294 
  2295 lemma positive_integral_distr':
  2296   assumes T: "T \<in> measurable M M'"
  2297   and f: "f \<in> borel_measurable (distr M M' T)" "\<And>x. 0 \<le> f x"
  2298   shows "integral\<^isup>P (distr M M' T) f = (\<integral>\<^isup>+ x. f (T x) \<partial>M)"
  2299   using f 
  2300 proof induct
  2301   case (cong f g)
  2302   with T show ?case
  2303     apply (subst positive_integral_cong[of _ f g])
  2304     apply simp
  2305     apply (subst positive_integral_cong[of _ "\<lambda>x. f (T x)" "\<lambda>x. g (T x)"])
  2306     apply (simp add: measurable_def Pi_iff)
  2307     apply simp
  2308     done
  2309 next
  2310   case (set A)
  2311   then have eq: "\<And>x. x \<in> space M \<Longrightarrow> indicator A (T x) = indicator (T -` A \<inter> space M) x"
  2312     by (auto simp: indicator_def)
  2313   from set T show ?case
  2314     by (subst positive_integral_cong[OF eq])
  2315        (auto simp add: emeasure_distr intro!: positive_integral_indicator[symmetric] measurable_sets)
  2316 qed (simp_all add: measurable_compose[OF T] T positive_integral_cmult positive_integral_add
  2317                    positive_integral_monotone_convergence_SUP le_fun_def incseq_def)
  2318 
  2319 lemma positive_integral_distr:
  2320   assumes T: "T \<in> measurable M M'"
  2321   and f: "f \<in> borel_measurable M'"
  2322   shows "integral\<^isup>P (distr M M' T) f = (\<integral>\<^isup>+ x. f (T x) \<partial>M)"
  2323   apply (subst (1 2) positive_integral_max_0[symmetric])
  2324   apply (rule positive_integral_distr')
  2325   apply (auto simp: f T)
  2326   done
  2327 
  2328 lemma integral_distr:
  2329   "T \<in> measurable M M' \<Longrightarrow> f \<in> borel_measurable M' \<Longrightarrow> integral\<^isup>L (distr M M' T) f = (\<integral> x. f (T x) \<partial>M)"
  2330   unfolding lebesgue_integral_def
  2331   by (subst (1 2) positive_integral_distr) auto
  2332 
  2333 lemma integrable_distr_eq:
  2334   assumes T: "T \<in> measurable M M'" "f \<in> borel_measurable M'"
  2335   shows "integrable (distr M M' T) f \<longleftrightarrow> integrable M (\<lambda>x. f (T x))"
  2336   using T measurable_comp[OF T]
  2337   unfolding integrable_def 
  2338   by (subst (1 2) positive_integral_distr) (auto simp: comp_def)
  2339 
  2340 lemma integrable_distr:
  2341   assumes T: "T \<in> measurable M M'" shows "integrable (distr M M' T) f \<Longrightarrow> integrable M (\<lambda>x. f (T x))"
  2342   by (subst integrable_distr_eq[symmetric, OF T]) auto
  2343 
  2344 section {* Lebesgue integration on @{const count_space} *}
  2345 
  2346 lemma simple_function_count_space[simp]:
  2347   "simple_function (count_space A) f \<longleftrightarrow> finite (f ` A)"
  2348   unfolding simple_function_def by simp
  2349 
  2350 lemma positive_integral_count_space:
  2351   assumes A: "finite {a\<in>A. 0 < f a}"
  2352   shows "integral\<^isup>P (count_space A) f = (\<Sum>a|a\<in>A \<and> 0 < f a. f a)"
  2353 proof -
  2354   have *: "(\<integral>\<^isup>+x. max 0 (f x) \<partial>count_space A) =
  2355     (\<integral>\<^isup>+ x. (\<Sum>a|a\<in>A \<and> 0 < f a. f a * indicator {a} x) \<partial>count_space A)"
  2356     by (auto intro!: positive_integral_cong
  2357              simp add: indicator_def if_distrib setsum_cases[OF A] max_def le_less)
  2358   also have "\<dots> = (\<Sum>a|a\<in>A \<and> 0 < f a. \<integral>\<^isup>+ x. f a * indicator {a} x \<partial>count_space A)"
  2359     by (subst positive_integral_setsum)
  2360        (simp_all add: AE_count_space ereal_zero_le_0_iff less_imp_le)
  2361   also have "\<dots> = (\<Sum>a|a\<in>A \<and> 0 < f a. f a)"
  2362     by (auto intro!: setsum_cong simp: positive_integral_cmult_indicator one_ereal_def[symmetric])
  2363   finally show ?thesis by (simp add: positive_integral_max_0)
  2364 qed
  2365 
  2366 lemma integrable_count_space:
  2367   "finite X \<Longrightarrow> integrable (count_space X) f"
  2368   by (auto simp: positive_integral_count_space integrable_def)
  2369 
  2370 lemma positive_integral_count_space_finite:
  2371     "finite A \<Longrightarrow> (\<integral>\<^isup>+x. f x \<partial>count_space A) = (\<Sum>a\<in>A. max 0 (f a))"
  2372   by (subst positive_integral_max_0[symmetric])
  2373      (auto intro!: setsum_mono_zero_left simp: positive_integral_count_space less_le)
  2374 
  2375 lemma lebesgue_integral_count_space_finite_support:
  2376   assumes f: "finite {a\<in>A. f a \<noteq> 0}" shows "(\<integral>x. f x \<partial>count_space A) = (\<Sum>a | a \<in> A \<and> f a \<noteq> 0. f a)"
  2377 proof -
  2378   have *: "\<And>r::real. 0 < max 0 r \<longleftrightarrow> 0 < r" "\<And>x. max 0 (ereal x) = ereal (max 0 x)"
  2379     "\<And>a. a \<in> A \<and> 0 < f a \<Longrightarrow> max 0 (f a) = f a"
  2380     "\<And>a. a \<in> A \<and> f a < 0 \<Longrightarrow> max 0 (- f a) = - f a"
  2381     "{a \<in> A. f a \<noteq> 0} = {a \<in> A. 0 < f a} \<union> {a \<in> A. f a < 0}"
  2382     "({a \<in> A. 0 < f a} \<inter> {a \<in> A. f a < 0}) = {}"
  2383     by (auto split: split_max)
  2384   have "finite {a \<in> A. 0 < f a}" "finite {a \<in> A. f a < 0}"
  2385     by (auto intro: finite_subset[OF _ f])
  2386   then show ?thesis
  2387     unfolding lebesgue_integral_def
  2388     apply (subst (1 2) positive_integral_max_0[symmetric])
  2389     apply (subst (1 2) positive_integral_count_space)
  2390     apply (auto simp add: * setsum_negf setsum_Un
  2391                 simp del: ereal_max)
  2392     done
  2393 qed
  2394 
  2395 lemma lebesgue_integral_count_space_finite:
  2396     "finite A \<Longrightarrow> (\<integral>x. f x \<partial>count_space A) = (\<Sum>a\<in>A. f a)"
  2397   apply (auto intro!: setsum_mono_zero_left
  2398               simp: positive_integral_count_space_finite lebesgue_integral_def)
  2399   apply (subst (1 2)  setsum_real_of_ereal[symmetric])
  2400   apply (auto simp: max_def setsum_subtractf[symmetric] intro!: setsum_cong)
  2401   done
  2402 
  2403 lemma borel_measurable_count_space[simp, intro!]:
  2404   "f \<in> borel_measurable (count_space A)"
  2405   by simp
  2406 
  2407 section {* Measure spaces with an associated density *}
  2408 
  2409 definition density :: "'a measure \<Rightarrow> ('a \<Rightarrow> ereal) \<Rightarrow> 'a measure" where
  2410   "density M f = measure_of (space M) (sets M) (\<lambda>A. \<integral>\<^isup>+ x. f x * indicator A x \<partial>M)"
  2411 
  2412 lemma 
  2413   shows sets_density[simp]: "sets (density M f) = sets M"
  2414     and space_density[simp]: "space (density M f) = space M"
  2415   by (auto simp: density_def)
  2416 
  2417 lemma 
  2418   shows measurable_density_eq1[simp]: "g \<in> measurable (density Mg f) Mg' \<longleftrightarrow> g \<in> measurable Mg Mg'"
  2419     and measurable_density_eq2[simp]: "h \<in> measurable Mh (density Mh' f) \<longleftrightarrow> h \<in> measurable Mh Mh'"
  2420     and simple_function_density_eq[simp]: "simple_function (density Mu f) u \<longleftrightarrow> simple_function Mu u"
  2421   unfolding measurable_def simple_function_def by simp_all
  2422 
  2423 lemma density_cong: "f \<in> borel_measurable M \<Longrightarrow> f' \<in> borel_measurable M \<Longrightarrow>
  2424   (AE x in M. f x = f' x) \<Longrightarrow> density M f = density M f'"
  2425   unfolding density_def by (auto intro!: measure_of_eq positive_integral_cong_AE space_closed)
  2426 
  2427 lemma density_max_0: "density M f = density M (\<lambda>x. max 0 (f x))"
  2428 proof -
  2429   have "\<And>x A. max 0 (f x) * indicator A x = max 0 (f x * indicator A x)"
  2430     by (auto simp: indicator_def)
  2431   then show ?thesis
  2432     unfolding density_def by (simp add: positive_integral_max_0)
  2433 qed
  2434 
  2435 lemma density_ereal_max_0: "density M (\<lambda>x. ereal (f x)) = density M (\<lambda>x. ereal (max 0 (f x)))"
  2436   by (subst density_max_0) (auto intro!: arg_cong[where f="density M"] split: split_max)
  2437 
  2438 lemma emeasure_density:
  2439   assumes f[measurable]: "f \<in> borel_measurable M" and A[measurable]: "A \<in> sets M"
  2440   shows "emeasure (density M f) A = (\<integral>\<^isup>+ x. f x * indicator A x \<partial>M)"
  2441     (is "_ = ?\<mu> A")
  2442   unfolding density_def
  2443 proof (rule emeasure_measure_of_sigma)
  2444   show "sigma_algebra (space M) (sets M)" ..
  2445   show "positive (sets M) ?\<mu>"
  2446     using f by (auto simp: positive_def intro!: positive_integral_positive)
  2447   have \<mu>_eq: "?\<mu> = (\<lambda>A. \<integral>\<^isup>+ x. max 0 (f x) * indicator A x \<partial>M)" (is "?\<mu> = ?\<mu>'")
  2448     apply (subst positive_integral_max_0[symmetric])
  2449     apply (intro ext positive_integral_cong_AE AE_I2)
  2450     apply (auto simp: indicator_def)
  2451     done
  2452   show "countably_additive (sets M) ?\<mu>"
  2453     unfolding \<mu>_eq
  2454   proof (intro countably_additiveI)
  2455     fix A :: "nat \<Rightarrow> 'a set" assume "range A \<subseteq> sets M"
  2456     then have "\<And>i. A i \<in> sets M" by auto
  2457     then have *: "\<And>i. (\<lambda>x. max 0 (f x) * indicator (A i) x) \<in> borel_measurable M"
  2458       by (auto simp: set_eq_iff)
  2459     assume disj: "disjoint_family A"
  2460     have "(\<Sum>n. ?\<mu>' (A n)) = (\<integral>\<^isup>+ x. (\<Sum>n. max 0 (f x) * indicator (A n) x) \<partial>M)"
  2461       using f * by (simp add: positive_integral_suminf)
  2462     also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (f x) * (\<Sum>n. indicator (A n) x) \<partial>M)" using f
  2463       by (auto intro!: suminf_cmult_ereal positive_integral_cong_AE)
  2464     also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (f x) * indicator (\<Union>n. A n) x \<partial>M)"
  2465       unfolding suminf_indicator[OF disj] ..
  2466     finally show "(\<Sum>n. ?\<mu>' (A n)) = ?\<mu>' (\<Union>x. A x)" by simp
  2467   qed
  2468 qed fact
  2469 
  2470 lemma null_sets_density_iff:
  2471   assumes f: "f \<in> borel_measurable M"
  2472   shows "A \<in> null_sets (density M f) \<longleftrightarrow> A \<in> sets M \<and> (AE x in M. x \<in> A \<longrightarrow> f x \<le> 0)"
  2473 proof -
  2474   { assume "A \<in> sets M"
  2475     have eq: "(\<integral>\<^isup>+x. f x * indicator A x \<partial>M) = (\<integral>\<^isup>+x. max 0 (f x) * indicator A x \<partial>M)"
  2476       apply (subst positive_integral_max_0[symmetric])
  2477       apply (intro positive_integral_cong)
  2478       apply (auto simp: indicator_def)
  2479       done
  2480     have "(\<integral>\<^isup>+x. f x * indicator A x \<partial>M) = 0 \<longleftrightarrow> 
  2481       emeasure M {x \<in> space M. max 0 (f x) * indicator A x \<noteq> 0} = 0"
  2482       unfolding eq
  2483       using f `A \<in> sets M`
  2484       by (intro positive_integral_0_iff) auto
  2485     also have "\<dots> \<longleftrightarrow> (AE x in M. max 0 (f x) * indicator A x = 0)"
  2486       using f `A \<in> sets M`
  2487       by (intro AE_iff_measurable[OF _ refl, symmetric]) auto
  2488     also have "(AE x in M. max 0 (f x) * indicator A x = 0) \<longleftrightarrow> (AE x in M. x \<in> A \<longrightarrow> f x \<le> 0)"
  2489       by (auto simp add: indicator_def max_def split: split_if_asm)
  2490     finally have "(\<integral>\<^isup>+x. f x * indicator A x \<partial>M) = 0 \<longleftrightarrow> (AE x in M. x \<in> A \<longrightarrow> f x \<le> 0)" . }
  2491   with f show ?thesis
  2492     by (simp add: null_sets_def emeasure_density cong: conj_cong)
  2493 qed
  2494 
  2495 lemma AE_density:
  2496   assumes f: "f \<in> borel_measurable M"
  2497   shows "(AE x in density M f. P x) \<longleftrightarrow> (AE x in M. 0 < f x \<longrightarrow> P x)"
  2498 proof
  2499   assume "AE x in density M f. P x"
  2500   with f obtain N where "{x \<in> space M. \<not> P x} \<subseteq> N" "N \<in> sets M" and ae: "AE x in M. x \<in> N \<longrightarrow> f x \<le> 0"
  2501     by (auto simp: eventually_ae_filter null_sets_density_iff)
  2502   then have "AE x in M. x \<notin> N \<longrightarrow> P x" by auto
  2503   with ae show "AE x in M. 0 < f x \<longrightarrow> P x"
  2504     by (rule eventually_elim2) auto
  2505 next
  2506   fix N assume ae: "AE x in M. 0 < f x \<longrightarrow> P x"
  2507   then obtain N where "{x \<in> space M. \<not> (0 < f x \<longrightarrow> P x)} \<subseteq> N" "N \<in> null_sets M"
  2508     by (auto simp: eventually_ae_filter)
  2509   then have *: "{x \<in> space (density M f). \<not> P x} \<subseteq> N \<union> {x\<in>space M. \<not> 0 < f x}"
  2510     "N \<union> {x\<in>space M. \<not> 0 < f x} \<in> sets M" and ae2: "AE x in M. x \<notin> N"
  2511     using f by (auto simp: subset_eq intro!: sets_Collect_neg AE_not_in)
  2512   show "AE x in density M f. P x"
  2513     using ae2
  2514     unfolding eventually_ae_filter[of _ "density M f"] Bex_def null_sets_density_iff[OF f]
  2515     by (intro exI[of _ "N \<union> {x\<in>space M. \<not> 0 < f x}"] conjI *)
  2516        (auto elim: eventually_elim2)
  2517 qed
  2518 
  2519 lemma positive_integral_density':
  2520   assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x"
  2521   assumes g: "g \<in> borel_measurable M" "\<And>x. 0 \<le> g x"
  2522   shows "integral\<^isup>P (density M f) g = (\<integral>\<^isup>+ x. f x * g x \<partial>M)"
  2523 using g proof induct
  2524   case (cong u v)
  2525   then show ?case
  2526     apply (subst positive_integral_cong[OF cong(3)])
  2527     apply (simp_all cong: positive_integral_cong)
  2528     done
  2529 next
  2530   case (set A) then show ?case
  2531     by (simp add: emeasure_density f)
  2532 next
  2533   case (mult u c)
  2534   moreover have "\<And>x. f x * (c * u x) = c * (f x * u x)" by (simp add: field_simps)
  2535   ultimately show ?case
  2536     by (simp add: f positive_integral_cmult)
  2537 next
  2538   case (add u v)
  2539   moreover then have "\<And>x. f x * (v x + u x) = f x * v x + f x * u x"
  2540     by (simp add: ereal_right_distrib)
  2541   moreover note f
  2542   ultimately show ?case
  2543     by (auto simp add: positive_integral_add ereal_zero_le_0_iff intro!: positive_integral_add[symmetric])
  2544 next
  2545   case (seq U)
  2546   from f(2) have eq: "AE x in M. f x * (SUP i. U i x) = (SUP i. f x * U i x)"
  2547     by eventually_elim (simp add: SUPR_ereal_cmult seq)
  2548   from seq f show ?case
  2549     apply (simp add: positive_integral_monotone_convergence_SUP)
  2550     apply (subst positive_integral_cong_AE[OF eq])
  2551     apply (subst positive_integral_monotone_convergence_SUP_AE)
  2552     apply (auto simp: incseq_def le_fun_def intro!: ereal_mult_left_mono)
  2553     done
  2554 qed
  2555 
  2556 lemma positive_integral_density:
  2557   "f \<in> borel_measurable M \<Longrightarrow> AE x in M. 0 \<le> f x \<Longrightarrow> g' \<in> borel_measurable M \<Longrightarrow> 
  2558     integral\<^isup>P (density M f) g' = (\<integral>\<^isup>+ x. f x * g' x \<partial>M)"
  2559   by (subst (1 2) positive_integral_max_0[symmetric])
  2560      (auto intro!: positive_integral_cong_AE
  2561            simp: measurable_If max_def ereal_zero_le_0_iff positive_integral_density')
  2562 
  2563 lemma integral_density:
  2564   assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x"
  2565     and g: "g \<in> borel_measurable M"
  2566   shows "integral\<^isup>L (density M f) g = (\<integral> x. f x * g x \<partial>M)"
  2567     and "integrable (density M f) g \<longleftrightarrow> integrable M (\<lambda>x. f x * g x)"
  2568   unfolding lebesgue_integral_def integrable_def using f g
  2569   by (auto simp: positive_integral_density)
  2570 
  2571 lemma emeasure_restricted:
  2572   assumes S: "S \<in> sets M" and X: "X \<in> sets M"
  2573   shows "emeasure (density M (indicator S)) X = emeasure M (S \<inter> X)"
  2574 proof -
  2575   have "emeasure (density M (indicator S)) X = (\<integral>\<^isup>+x. indicator S x * indicator X x \<partial>M)"
  2576     using S X by (simp add: emeasure_density)
  2577   also have "\<dots> = (\<integral>\<^isup>+x. indicator (S \<inter> X) x \<partial>M)"
  2578     by (auto intro!: positive_integral_cong simp: indicator_def)
  2579   also have "\<dots> = emeasure M (S \<inter> X)"
  2580     using S X by (simp add: Int)
  2581   finally show ?thesis .
  2582 qed
  2583 
  2584 lemma measure_restricted:
  2585   "S \<in> sets M \<Longrightarrow> X \<in> sets M \<Longrightarrow> measure (density M (indicator S)) X = measure M (S \<inter> X)"
  2586   by (simp add: emeasure_restricted measure_def)
  2587 
  2588 lemma (in finite_measure) finite_measure_restricted:
  2589   "S \<in> sets M \<Longrightarrow> finite_measure (density M (indicator S))"
  2590   by default (simp add: emeasure_restricted)
  2591 
  2592 lemma emeasure_density_const:
  2593   "A \<in> sets M \<Longrightarrow> 0 \<le> c \<Longrightarrow> emeasure (density M (\<lambda>_. c)) A = c * emeasure M A"
  2594   by (auto simp: positive_integral_cmult_indicator emeasure_density)
  2595 
  2596 lemma measure_density_const:
  2597   "A \<in> sets M \<Longrightarrow> 0 < c \<Longrightarrow> c \<noteq> \<infinity> \<Longrightarrow> measure (density M (\<lambda>_. c)) A = real c * measure M A"
  2598   by (auto simp: emeasure_density_const measure_def)
  2599 
  2600 lemma density_density_eq:
  2601    "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> AE x in M. 0 \<le> f x \<Longrightarrow>
  2602    density (density M f) g = density M (\<lambda>x. f x * g x)"
  2603   by (auto intro!: measure_eqI simp: emeasure_density positive_integral_density ac_simps)
  2604 
  2605 lemma distr_density_distr:
  2606   assumes T: "T \<in> measurable M M'" and T': "T' \<in> measurable M' M"
  2607     and inv: "\<forall>x\<in>space M. T' (T x) = x"
  2608   assumes f: "f \<in> borel_measurable M'"
  2609   shows "distr (density (distr M M' T) f) M T' = density M (f \<circ> T)" (is "?R = ?L")
  2610 proof (rule measure_eqI)
  2611   fix A assume A: "A \<in> sets ?R"
  2612   { fix x assume "x \<in> space M"
  2613     with sets_into_space[OF A]
  2614     have "indicator (T' -` A \<inter> space M') (T x) = (indicator A x :: ereal)"
  2615       using T inv by (auto simp: indicator_def measurable_space) }
  2616   with A T T' f show "emeasure ?R A = emeasure ?L A"
  2617     by (simp add: measurable_comp emeasure_density emeasure_distr
  2618                   positive_integral_distr measurable_sets cong: positive_integral_cong)
  2619 qed simp
  2620 
  2621 lemma density_density_divide:
  2622   fixes f g :: "'a \<Rightarrow> real"
  2623   assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x"
  2624   assumes g: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
  2625   assumes ac: "AE x in M. f x = 0 \<longrightarrow> g x = 0"
  2626   shows "density (density M f) (\<lambda>x. g x / f x) = density M g"
  2627 proof -
  2628   have "density M g = density M (\<lambda>x. f x * (g x / f x))"
  2629     using f g ac by (auto intro!: density_cong measurable_If)
  2630   then show ?thesis
  2631     using f g by (subst density_density_eq) auto
  2632 qed
  2633 
  2634 section {* Point measure *}
  2635 
  2636 definition point_measure :: "'a set \<Rightarrow> ('a \<Rightarrow> ereal) \<Rightarrow> 'a measure" where
  2637   "point_measure A f = density (count_space A) f"
  2638 
  2639 lemma
  2640   shows space_point_measure: "space (point_measure A f) = A"
  2641     and sets_point_measure: "sets (point_measure A f) = Pow A"
  2642   by (auto simp: point_measure_def)
  2643 
  2644 lemma measurable_point_measure_eq1[simp]:
  2645   "g \<in> measurable (point_measure A f) M \<longleftrightarrow> g \<in> A \<rightarrow> space M"
  2646   unfolding point_measure_def by simp
  2647 
  2648 lemma measurable_point_measure_eq2_finite[simp]:
  2649   "finite A \<Longrightarrow>
  2650    g \<in> measurable M (point_measure A f) \<longleftrightarrow>
  2651     (g \<in> space M \<rightarrow> A \<and> (\<forall>a\<in>A. g -` {a} \<inter> space M \<in> sets M))"
  2652   unfolding point_measure_def by (simp add: measurable_count_space_eq2)
  2653 
  2654 lemma simple_function_point_measure[simp]:
  2655   "simple_function (point_measure A f) g \<longleftrightarrow> finite (g ` A)"
  2656   by (simp add: point_measure_def)
  2657 
  2658 lemma emeasure_point_measure:
  2659   assumes A: "finite {a\<in>X. 0 < f a}" "X \<subseteq> A"
  2660   shows "emeasure (point_measure A f) X = (\<Sum>a|a\<in>X \<and> 0 < f a. f a)"
  2661 proof -
  2662   have "{a. (a \<in> X \<longrightarrow> a \<in> A \<and> 0 < f a) \<and> a \<in> X} = {a\<in>X. 0 < f a}"
  2663     using `X \<subseteq> A` by auto
  2664   with A show ?thesis
  2665     by (simp add: emeasure_density positive_integral_count_space ereal_zero_le_0_iff
  2666                   point_measure_def indicator_def)
  2667 qed
  2668 
  2669 lemma emeasure_point_measure_finite:
  2670   "finite A \<Longrightarrow> (\<And>i. i \<in> A \<Longrightarrow> 0 \<le> f i) \<Longrightarrow> X \<subseteq> A \<Longrightarrow> emeasure (point_measure A f) X = (\<Sum>a\<in>X. f a)"
  2671   by (subst emeasure_point_measure) (auto dest: finite_subset intro!: setsum_mono_zero_left simp: less_le)
  2672 
  2673 lemma emeasure_point_measure_finite2:
  2674   "X \<subseteq> A \<Longrightarrow> finite X \<Longrightarrow> (\<And>i. i \<in> X \<Longrightarrow> 0 \<le> f i) \<Longrightarrow> emeasure (point_measure A f) X = (\<Sum>a\<in>X. f a)"
  2675   by (subst emeasure_point_measure)
  2676      (auto dest: finite_subset intro!: setsum_mono_zero_left simp: less_le)
  2677 
  2678 lemma null_sets_point_measure_iff:
  2679   "X \<in> null_sets (point_measure A f) \<longleftrightarrow> X \<subseteq> A \<and> (\<forall>x\<in>X. f x \<le> 0)"
  2680  by (auto simp: AE_count_space null_sets_density_iff point_measure_def)
  2681 
  2682 lemma AE_point_measure:
  2683   "(AE x in point_measure A f. P x) \<longleftrightarrow> (\<forall>x\<in>A. 0 < f x \<longrightarrow> P x)"
  2684   unfolding point_measure_def
  2685   by (subst AE_density) (auto simp: AE_density AE_count_space point_measure_def)
  2686 
  2687 lemma positive_integral_point_measure:
  2688   "finite {a\<in>A. 0 < f a \<and> 0 < g a} \<Longrightarrow>
  2689     integral\<^isup>P (point_measure A f) g = (\<Sum>a|a\<in>A \<and> 0 < f a \<and> 0 < g a. f a * g a)"
  2690   unfolding point_measure_def
  2691   apply (subst density_max_0)
  2692   apply (subst positive_integral_density)
  2693   apply (simp_all add: AE_count_space positive_integral_density)
  2694   apply (subst positive_integral_count_space )
  2695   apply (auto intro!: setsum_cong simp: max_def ereal_zero_less_0_iff)
  2696   apply (rule finite_subset)
  2697   prefer 2
  2698   apply assumption
  2699   apply auto
  2700   done
  2701 
  2702 lemma positive_integral_point_measure_finite:
  2703   "finite A \<Longrightarrow> (\<And>a. a \<in> A \<Longrightarrow> 0 \<le> f a) \<Longrightarrow> (\<And>a. a \<in> A \<Longrightarrow> 0 \<le> g a) \<Longrightarrow>
  2704     integral\<^isup>P (point_measure A f) g = (\<Sum>a\<in>A. f a * g a)"
  2705   by (subst positive_integral_point_measure) (auto intro!: setsum_mono_zero_left simp: less_le)
  2706 
  2707 lemma lebesgue_integral_point_measure_finite:
  2708   "finite A \<Longrightarrow> (\<And>a. a \<in> A \<Longrightarrow> 0 \<le> f a) \<Longrightarrow> integral\<^isup>L (point_measure A f) g = (\<Sum>a\<in>A. f a * g a)"
  2709   by (simp add: lebesgue_integral_count_space_finite AE_count_space integral_density point_measure_def)
  2710 
  2711 lemma integrable_point_measure_finite:
  2712   "finite A \<Longrightarrow> integrable (point_measure A (\<lambda>x. ereal (f x))) g"
  2713   unfolding point_measure_def
  2714   apply (subst density_ereal_max_0)
  2715   apply (subst integral_density)
  2716   apply (auto simp: AE_count_space integrable_count_space)
  2717   done
  2718 
  2719 section {* Uniform measure *}
  2720 
  2721 definition "uniform_measure M A = density M (\<lambda>x. indicator A x / emeasure M A)"
  2722 
  2723 lemma
  2724   shows sets_uniform_measure[simp]: "sets (uniform_measure M A) = sets M"
  2725     and space_uniform_measure[simp]: "space (uniform_measure M A) = space M"
  2726   by (auto simp: uniform_measure_def)
  2727 
  2728 lemma emeasure_uniform_measure[simp]:
  2729   assumes A: "A \<in> sets M" and B: "B \<in> sets M"
  2730   shows "emeasure (uniform_measure M A) B = emeasure M (A \<inter> B) / emeasure M A"
  2731 proof -
  2732   from A B have "emeasure (uniform_measure M A) B = (\<integral>\<^isup>+x. (1 / emeasure M A) * indicator (A \<inter> B) x \<partial>M)"
  2733     by (auto simp add: uniform_measure_def emeasure_density split: split_indicator
  2734              intro!: positive_integral_cong)
  2735   also have "\<dots> = emeasure M (A \<inter> B) / emeasure M A"
  2736     using A B
  2737     by (subst positive_integral_cmult_indicator) (simp_all add: Int emeasure_nonneg)
  2738   finally show ?thesis .
  2739 qed
  2740 
  2741 lemma emeasure_neq_0_sets: "emeasure M A \<noteq> 0 \<Longrightarrow> A \<in> sets M"
  2742   using emeasure_notin_sets[of A M] by blast
  2743 
  2744 lemma measure_uniform_measure[simp]:
  2745   assumes A: "emeasure M A \<noteq> 0" "emeasure M A \<noteq> \<infinity>" and B: "B \<in> sets M"
  2746   shows "measure (uniform_measure M A) B = measure M (A \<inter> B) / measure M A"
  2747   using emeasure_uniform_measure[OF emeasure_neq_0_sets[OF A(1)] B] A
  2748   by (cases "emeasure M A" "emeasure M (A \<inter> B)" rule: ereal2_cases) (simp_all add: measure_def)
  2749 
  2750 section {* Uniform count measure *}
  2751 
  2752 definition "uniform_count_measure A = point_measure A (\<lambda>x. 1 / card A)"
  2753  
  2754 lemma 
  2755   shows space_uniform_count_measure: "space (uniform_count_measure A) = A"
  2756     and sets_uniform_count_measure: "sets (uniform_count_measure A) = Pow A"
  2757     unfolding uniform_count_measure_def by (auto simp: space_point_measure sets_point_measure)
  2758  
  2759 lemma emeasure_uniform_count_measure:
  2760   "finite A \<Longrightarrow> X \<subseteq> A \<Longrightarrow> emeasure (uniform_count_measure A) X = card X / card A"
  2761   by (simp add: real_eq_of_nat emeasure_point_measure_finite uniform_count_measure_def)
  2762  
  2763 lemma measure_uniform_count_measure:
  2764   "finite A \<Longrightarrow> X \<subseteq> A \<Longrightarrow> measure (uniform_count_measure A) X = card X / card A"
  2765   by (simp add: real_eq_of_nat emeasure_point_measure_finite uniform_count_measure_def measure_def)
  2766 
  2767 end