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