src/HOL/Probability/Lebesgue_Integration.thy
author hoelzl
Mon Jan 24 22:29:50 2011 +0100 (2011-01-24)
changeset 41661 baf1964bc468
parent 41545 9c869baf1c66
child 41689 3e39b0e730d6
permissions -rw-r--r--
use pre-image measure, instead of image
     1 (* Author: Armin Heller, Johannes Hoelzl, TU Muenchen *)
     2 
     3 header {*Lebesgue Integration*}
     4 
     5 theory Lebesgue_Integration
     6 imports Measure Borel_Space
     7 begin
     8 
     9 lemma sums_If_finite:
    10   assumes finite: "finite {r. P r}"
    11   shows "(\<lambda>r. if P r then f r else 0) sums (\<Sum>r\<in>{r. P r}. f r)" (is "?F sums _")
    12 proof cases
    13   assume "{r. P r} = {}" hence "\<And>r. \<not> P r" by auto
    14   thus ?thesis by (simp add: sums_zero)
    15 next
    16   assume not_empty: "{r. P r} \<noteq> {}"
    17   have "?F sums (\<Sum>r = 0..< Suc (Max {r. P r}). ?F r)"
    18     by (rule series_zero)
    19        (auto simp add: Max_less_iff[OF finite not_empty] less_eq_Suc_le[symmetric])
    20   also have "(\<Sum>r = 0..< Suc (Max {r. P r}). ?F r) = (\<Sum>r\<in>{r. P r}. f r)"
    21     by (subst setsum_cases)
    22        (auto intro!: setsum_cong simp: Max_ge_iff[OF finite not_empty] less_Suc_eq_le)
    23   finally show ?thesis .
    24 qed
    25 
    26 lemma sums_single:
    27   "(\<lambda>r. if r = i then f r else 0) sums f i"
    28   using sums_If_finite[of "\<lambda>r. r = i" f] by simp
    29 
    30 section "Simple function"
    31 
    32 text {*
    33 
    34 Our simple functions are not restricted to positive real numbers. Instead
    35 they are just functions with a finite range and are measurable when singleton
    36 sets are measurable.
    37 
    38 *}
    39 
    40 definition (in sigma_algebra) "simple_function g \<longleftrightarrow>
    41     finite (g ` space M) \<and>
    42     (\<forall>x \<in> g ` space M. g -` {x} \<inter> space M \<in> sets M)"
    43 
    44 lemma (in sigma_algebra) simple_functionD:
    45   assumes "simple_function g"
    46   shows "finite (g ` space M)" and "g -` X \<inter> space M \<in> sets M"
    47 proof -
    48   show "finite (g ` space M)"
    49     using assms unfolding simple_function_def by auto
    50   have "g -` X \<inter> space M = g -` (X \<inter> g`space M) \<inter> space M" by auto
    51   also have "\<dots> = (\<Union>x\<in>X \<inter> g`space M. g-`{x} \<inter> space M)" by auto
    52   finally show "g -` X \<inter> space M \<in> sets M" using assms
    53     by (auto intro!: finite_UN simp del: UN_simps simp: simple_function_def)
    54 qed
    55 
    56 lemma (in sigma_algebra) simple_function_indicator_representation:
    57   fixes f ::"'a \<Rightarrow> pextreal"
    58   assumes f: "simple_function f" and x: "x \<in> space M"
    59   shows "f x = (\<Sum>y \<in> f ` space M. y * indicator (f -` {y} \<inter> space M) x)"
    60   (is "?l = ?r")
    61 proof -
    62   have "?r = (\<Sum>y \<in> f ` space M.
    63     (if y = f x then y * indicator (f -` {y} \<inter> space M) x else 0))"
    64     by (auto intro!: setsum_cong2)
    65   also have "... =  f x *  indicator (f -` {f x} \<inter> space M) x"
    66     using assms by (auto dest: simple_functionD simp: setsum_delta)
    67   also have "... = f x" using x by (auto simp: indicator_def)
    68   finally show ?thesis by auto
    69 qed
    70 
    71 lemma (in measure_space) simple_function_notspace:
    72   "simple_function (\<lambda>x. h x * indicator (- space M) x::pextreal)" (is "simple_function ?h")
    73 proof -
    74   have "?h ` space M \<subseteq> {0}" unfolding indicator_def by auto
    75   hence [simp, intro]: "finite (?h ` space M)" by (auto intro: finite_subset)
    76   have "?h -` {0} \<inter> space M = space M" by auto
    77   thus ?thesis unfolding simple_function_def by auto
    78 qed
    79 
    80 lemma (in sigma_algebra) simple_function_cong:
    81   assumes "\<And>t. t \<in> space M \<Longrightarrow> f t = g t"
    82   shows "simple_function f \<longleftrightarrow> simple_function g"
    83 proof -
    84   have "f ` space M = g ` space M"
    85     "\<And>x. f -` {x} \<inter> space M = g -` {x} \<inter> space M"
    86     using assms by (auto intro!: image_eqI)
    87   thus ?thesis unfolding simple_function_def using assms by simp
    88 qed
    89 
    90 lemma (in sigma_algebra) borel_measurable_simple_function:
    91   assumes "simple_function f"
    92   shows "f \<in> borel_measurable M"
    93 proof (rule borel_measurableI)
    94   fix S
    95   let ?I = "f ` (f -` S \<inter> space M)"
    96   have *: "(\<Union>x\<in>?I. f -` {x} \<inter> space M) = f -` S \<inter> space M" (is "?U = _") by auto
    97   have "finite ?I"
    98     using assms unfolding simple_function_def by (auto intro: finite_subset)
    99   hence "?U \<in> sets M"
   100     apply (rule finite_UN)
   101     using assms unfolding simple_function_def by auto
   102   thus "f -` S \<inter> space M \<in> sets M" unfolding * .
   103 qed
   104 
   105 lemma (in sigma_algebra) simple_function_borel_measurable:
   106   fixes f :: "'a \<Rightarrow> 'x::t2_space"
   107   assumes "f \<in> borel_measurable M" and "finite (f ` space M)"
   108   shows "simple_function f"
   109   using assms unfolding simple_function_def
   110   by (auto intro: borel_measurable_vimage)
   111 
   112 lemma (in sigma_algebra) simple_function_const[intro, simp]:
   113   "simple_function (\<lambda>x. c)"
   114   by (auto intro: finite_subset simp: simple_function_def)
   115 
   116 lemma (in sigma_algebra) simple_function_compose[intro, simp]:
   117   assumes "simple_function f"
   118   shows "simple_function (g \<circ> f)"
   119   unfolding simple_function_def
   120 proof safe
   121   show "finite ((g \<circ> f) ` space M)"
   122     using assms unfolding simple_function_def by (auto simp: image_compose)
   123 next
   124   fix x assume "x \<in> space M"
   125   let ?G = "g -` {g (f x)} \<inter> (f`space M)"
   126   have *: "(g \<circ> f) -` {(g \<circ> f) x} \<inter> space M =
   127     (\<Union>x\<in>?G. f -` {x} \<inter> space M)" by auto
   128   show "(g \<circ> f) -` {(g \<circ> f) x} \<inter> space M \<in> sets M"
   129     using assms unfolding simple_function_def *
   130     by (rule_tac finite_UN) (auto intro!: finite_UN)
   131 qed
   132 
   133 lemma (in sigma_algebra) simple_function_indicator[intro, simp]:
   134   assumes "A \<in> sets M"
   135   shows "simple_function (indicator A)"
   136 proof -
   137   have "indicator A ` space M \<subseteq> {0, 1}" (is "?S \<subseteq> _")
   138     by (auto simp: indicator_def)
   139   hence "finite ?S" by (rule finite_subset) simp
   140   moreover have "- A \<inter> space M = space M - A" by auto
   141   ultimately show ?thesis unfolding simple_function_def
   142     using assms by (auto simp: indicator_def_raw)
   143 qed
   144 
   145 lemma (in sigma_algebra) simple_function_Pair[intro, simp]:
   146   assumes "simple_function f"
   147   assumes "simple_function g"
   148   shows "simple_function (\<lambda>x. (f x, g x))" (is "simple_function ?p")
   149   unfolding simple_function_def
   150 proof safe
   151   show "finite (?p ` space M)"
   152     using assms unfolding simple_function_def
   153     by (rule_tac finite_subset[of _ "f`space M \<times> g`space M"]) auto
   154 next
   155   fix x assume "x \<in> space M"
   156   have "(\<lambda>x. (f x, g x)) -` {(f x, g x)} \<inter> space M =
   157       (f -` {f x} \<inter> space M) \<inter> (g -` {g x} \<inter> space M)"
   158     by auto
   159   with `x \<in> space M` show "(\<lambda>x. (f x, g x)) -` {(f x, g x)} \<inter> space M \<in> sets M"
   160     using assms unfolding simple_function_def by auto
   161 qed
   162 
   163 lemma (in sigma_algebra) simple_function_compose1:
   164   assumes "simple_function f"
   165   shows "simple_function (\<lambda>x. g (f x))"
   166   using simple_function_compose[OF assms, of g]
   167   by (simp add: comp_def)
   168 
   169 lemma (in sigma_algebra) simple_function_compose2:
   170   assumes "simple_function f" and "simple_function g"
   171   shows "simple_function (\<lambda>x. h (f x) (g x))"
   172 proof -
   173   have "simple_function ((\<lambda>(x, y). h x y) \<circ> (\<lambda>x. (f x, g x)))"
   174     using assms by auto
   175   thus ?thesis by (simp_all add: comp_def)
   176 qed
   177 
   178 lemmas (in sigma_algebra) simple_function_add[intro, simp] = simple_function_compose2[where h="op +"]
   179   and simple_function_diff[intro, simp] = simple_function_compose2[where h="op -"]
   180   and simple_function_uminus[intro, simp] = simple_function_compose[where g="uminus"]
   181   and simple_function_mult[intro, simp] = simple_function_compose2[where h="op *"]
   182   and simple_function_div[intro, simp] = simple_function_compose2[where h="op /"]
   183   and simple_function_inverse[intro, simp] = simple_function_compose[where g="inverse"]
   184 
   185 lemma (in sigma_algebra) simple_function_setsum[intro, simp]:
   186   assumes "\<And>i. i \<in> P \<Longrightarrow> simple_function (f i)"
   187   shows "simple_function (\<lambda>x. \<Sum>i\<in>P. f i x)"
   188 proof cases
   189   assume "finite P" from this assms show ?thesis by induct auto
   190 qed auto
   191 
   192 lemma (in sigma_algebra) simple_function_le_measurable:
   193   assumes "simple_function f" "simple_function g"
   194   shows "{x \<in> space M. f x \<le> g x} \<in> sets M"
   195 proof -
   196   have *: "{x \<in> space M. f x \<le> g x} =
   197     (\<Union>(F, G)\<in>f`space M \<times> g`space M.
   198       if F \<le> G then (f -` {F} \<inter> space M) \<inter> (g -` {G} \<inter> space M) else {})"
   199     apply (auto split: split_if_asm)
   200     apply (rule_tac x=x in bexI)
   201     apply (rule_tac x=x in bexI)
   202     by simp_all
   203   have **: "\<And>x y. x \<in> space M \<Longrightarrow> y \<in> space M \<Longrightarrow>
   204     (f -` {f x} \<inter> space M) \<inter> (g -` {g y} \<inter> space M) \<in> sets M"
   205     using assms unfolding simple_function_def by auto
   206   have "finite (f`space M \<times> g`space M)"
   207     using assms unfolding simple_function_def by auto
   208   thus ?thesis unfolding *
   209     apply (rule finite_UN)
   210     using assms unfolding simple_function_def
   211     by (auto intro!: **)
   212 qed
   213 
   214 lemma (in sigma_algebra) borel_measurable_implies_simple_function_sequence:
   215   fixes u :: "'a \<Rightarrow> pextreal"
   216   assumes u: "u \<in> borel_measurable M"
   217   shows "\<exists>f. (\<forall>i. simple_function (f i) \<and> (\<forall>x\<in>space M. f i x \<noteq> \<omega>)) \<and> f \<up> u"
   218 proof -
   219   have "\<exists>f. \<forall>x j. (of_nat j \<le> u x \<longrightarrow> f x j = j*2^j) \<and>
   220     (u x < of_nat j \<longrightarrow> of_nat (f x j) \<le> u x * 2^j \<and> u x * 2^j < of_nat (Suc (f x j)))"
   221     (is "\<exists>f. \<forall>x j. ?P x j (f x j)")
   222   proof(rule choice, rule, rule choice, rule)
   223     fix x j show "\<exists>n. ?P x j n"
   224     proof cases
   225       assume *: "u x < of_nat j"
   226       then obtain r where r: "u x = Real r" "0 \<le> r" by (cases "u x") auto
   227       from reals_Archimedean6a[of "r * 2^j"]
   228       obtain n :: nat where "real n \<le> r * 2 ^ j" "r * 2 ^ j < real (Suc n)"
   229         using `0 \<le> r` by (auto simp: zero_le_power zero_le_mult_iff)
   230       thus ?thesis using r * by (auto intro!: exI[of _ n])
   231     qed auto
   232   qed
   233   then obtain f where top: "\<And>j x. of_nat j \<le> u x \<Longrightarrow> f x j = j*2^j" and
   234     upper: "\<And>j x. u x < of_nat j \<Longrightarrow> of_nat (f x j) \<le> u x * 2^j" and
   235     lower: "\<And>j x. u x < of_nat j \<Longrightarrow> u x * 2^j < of_nat (Suc (f x j))" by blast
   236 
   237   { fix j x P
   238     assume 1: "of_nat j \<le> u x \<Longrightarrow> P (j * 2^j)"
   239     assume 2: "\<And>k. \<lbrakk> u x < of_nat j ; of_nat k \<le> u x * 2^j ; u x * 2^j < of_nat (Suc k) \<rbrakk> \<Longrightarrow> P k"
   240     have "P (f x j)"
   241     proof cases
   242       assume "of_nat j \<le> u x" thus "P (f x j)"
   243         using top[of j x] 1 by auto
   244     next
   245       assume "\<not> of_nat j \<le> u x"
   246       hence "u x < of_nat j" "of_nat (f x j) \<le> u x * 2^j" "u x * 2^j < of_nat (Suc (f x j))"
   247         using upper lower by auto
   248       from 2[OF this] show "P (f x j)" .
   249     qed }
   250   note fI = this
   251 
   252   { fix j x
   253     have "f x j = j * 2 ^ j \<longleftrightarrow> of_nat j \<le> u x"
   254       by (rule fI, simp, cases "u x") (auto split: split_if_asm) }
   255   note f_eq = this
   256 
   257   { fix j x
   258     have "f x j \<le> j * 2 ^ j"
   259     proof (rule fI)
   260       fix k assume *: "u x < of_nat j"
   261       assume "of_nat k \<le> u x * 2 ^ j"
   262       also have "\<dots> \<le> of_nat (j * 2^j)"
   263         using * by (cases "u x") (auto simp: zero_le_mult_iff)
   264       finally show "k \<le> j*2^j" by (auto simp del: real_of_nat_mult)
   265     qed simp }
   266   note f_upper = this
   267 
   268   let "?g j x" = "of_nat (f x j) / 2^j :: pextreal"
   269   show ?thesis unfolding simple_function_def isoton_fun_expand unfolding isoton_def
   270   proof (safe intro!: exI[of _ ?g])
   271     fix j
   272     have *: "?g j ` space M \<subseteq> (\<lambda>x. of_nat x / 2^j) ` {..j * 2^j}"
   273       using f_upper by auto
   274     thus "finite (?g j ` space M)" by (rule finite_subset) auto
   275   next
   276     fix j t assume "t \<in> space M"
   277     have **: "?g j -` {?g j t} \<inter> space M = {x \<in> space M. f t j = f x j}"
   278       by (auto simp: power_le_zero_eq Real_eq_Real mult_le_0_iff)
   279 
   280     show "?g j -` {?g j t} \<inter> space M \<in> sets M"
   281     proof cases
   282       assume "of_nat j \<le> u t"
   283       hence "?g j -` {?g j t} \<inter> space M = {x\<in>space M. of_nat j \<le> u x}"
   284         unfolding ** f_eq[symmetric] by auto
   285       thus "?g j -` {?g j t} \<inter> space M \<in> sets M"
   286         using u by auto
   287     next
   288       assume not_t: "\<not> of_nat j \<le> u t"
   289       hence less: "f t j < j*2^j" using f_eq[symmetric] `f t j \<le> j*2^j` by auto
   290       have split_vimage: "?g j -` {?g j t} \<inter> space M =
   291           {x\<in>space M. of_nat (f t j)/2^j \<le> u x} \<inter> {x\<in>space M. u x < of_nat (Suc (f t j))/2^j}"
   292         unfolding **
   293       proof safe
   294         fix x assume [simp]: "f t j = f x j"
   295         have *: "\<not> of_nat j \<le> u x" using not_t f_eq[symmetric] by simp
   296         hence "of_nat (f t j) \<le> u x * 2^j \<and> u x * 2^j < of_nat (Suc (f t j))"
   297           using upper lower by auto
   298         hence "of_nat (f t j) / 2^j \<le> u x \<and> u x < of_nat (Suc (f t j))/2^j" using *
   299           by (cases "u x") (auto simp: zero_le_mult_iff power_le_zero_eq divide_real_def[symmetric] field_simps)
   300         thus "of_nat (f t j) / 2^j \<le> u x" "u x < of_nat (Suc (f t j))/2^j" by auto
   301       next
   302         fix x
   303         assume "of_nat (f t j) / 2^j \<le> u x" "u x < of_nat (Suc (f t j))/2^j"
   304         hence "of_nat (f t j) \<le> u x * 2 ^ j \<and> u x * 2 ^ j < of_nat (Suc (f t j))"
   305           by (cases "u x") (auto simp: zero_le_mult_iff power_le_zero_eq divide_real_def[symmetric] field_simps)
   306         hence 1: "of_nat (f t j) \<le> u x * 2 ^ j" and 2: "u x * 2 ^ j < of_nat (Suc (f t j))" by auto
   307         note 2
   308         also have "\<dots> \<le> of_nat (j*2^j)"
   309           using less by (auto simp: zero_le_mult_iff simp del: real_of_nat_mult)
   310         finally have bound_ux: "u x < of_nat j"
   311           by (cases "u x") (auto simp: zero_le_mult_iff power_le_zero_eq)
   312         show "f t j = f x j"
   313         proof (rule antisym)
   314           from 1 lower[OF bound_ux]
   315           show "f t j \<le> f x j" by (cases "u x") (auto split: split_if_asm)
   316           from upper[OF bound_ux] 2
   317           show "f x j \<le> f t j" by (cases "u x") (auto split: split_if_asm)
   318         qed
   319       qed
   320       show ?thesis unfolding split_vimage using u by auto
   321     qed
   322   next
   323     fix j t assume "?g j t = \<omega>" thus False by (auto simp: power_le_zero_eq)
   324   next
   325     fix t
   326     { fix i
   327       have "f t i * 2 \<le> f t (Suc i)"
   328       proof (rule fI)
   329         assume "of_nat (Suc i) \<le> u t"
   330         hence "of_nat i \<le> u t" by (cases "u t") auto
   331         thus "f t i * 2 \<le> Suc i * 2 ^ Suc i" unfolding f_eq[symmetric] by simp
   332       next
   333         fix k
   334         assume *: "u t * 2 ^ Suc i < of_nat (Suc k)"
   335         show "f t i * 2 \<le> k"
   336         proof (rule fI)
   337           assume "of_nat i \<le> u t"
   338           hence "of_nat (i * 2^Suc i) \<le> u t * 2^Suc i"
   339             by (cases "u t") (auto simp: zero_le_mult_iff power_le_zero_eq)
   340           also have "\<dots> < of_nat (Suc k)" using * by auto
   341           finally show "i * 2 ^ i * 2 \<le> k"
   342             by (auto simp del: real_of_nat_mult)
   343         next
   344           fix j assume "of_nat j \<le> u t * 2 ^ i"
   345           with * show "j * 2 \<le> k" by (cases "u t") (auto simp: zero_le_mult_iff power_le_zero_eq)
   346         qed
   347       qed
   348       thus "?g i t \<le> ?g (Suc i) t"
   349         by (auto simp: zero_le_mult_iff power_le_zero_eq divide_real_def[symmetric] field_simps) }
   350     hence mono: "mono (\<lambda>i. ?g i t)" unfolding mono_iff_le_Suc by auto
   351 
   352     show "(SUP j. of_nat (f t j) / 2 ^ j) = u t"
   353     proof (rule pextreal_SUPI)
   354       fix j show "of_nat (f t j) / 2 ^ j \<le> u t"
   355       proof (rule fI)
   356         assume "of_nat j \<le> u t" thus "of_nat (j * 2 ^ j) / 2 ^ j \<le> u t"
   357           by (cases "u t") (auto simp: power_le_zero_eq divide_real_def[symmetric] field_simps)
   358       next
   359         fix k assume "of_nat k \<le> u t * 2 ^ j"
   360         thus "of_nat k / 2 ^ j \<le> u t"
   361           by (cases "u t")
   362              (auto simp: power_le_zero_eq divide_real_def[symmetric] field_simps zero_le_mult_iff)
   363       qed
   364     next
   365       fix y :: pextreal assume *: "\<And>j. j \<in> UNIV \<Longrightarrow> of_nat (f t j) / 2 ^ j \<le> y"
   366       show "u t \<le> y"
   367       proof (cases "u t")
   368         case (preal r)
   369         show ?thesis
   370         proof (rule ccontr)
   371           assume "\<not> u t \<le> y"
   372           then obtain p where p: "y = Real p" "0 \<le> p" "p < r" using preal by (cases y) auto
   373           with LIMSEQ_inverse_realpow_zero[of 2, unfolded LIMSEQ_iff, rule_format, of "r - p"]
   374           obtain n where n: "\<And>N. n \<le> N \<Longrightarrow> inverse (2^N) < r - p" by auto
   375           let ?N = "max n (natfloor r + 1)"
   376           have "u t < of_nat ?N" "n \<le> ?N"
   377             using ge_natfloor_plus_one_imp_gt[of r n] preal
   378             using real_natfloor_add_one_gt
   379             by (auto simp: max_def real_of_nat_Suc)
   380           from lower[OF this(1)]
   381           have "r < (real (f t ?N) + 1) / 2 ^ ?N" unfolding less_divide_eq
   382             using preal by (auto simp add: power_le_zero_eq zero_le_mult_iff real_of_nat_Suc split: split_if_asm)
   383           hence "u t < of_nat (f t ?N) / 2 ^ ?N + 1 / 2 ^ ?N"
   384             using preal by (auto simp: field_simps divide_real_def[symmetric])
   385           with n[OF `n \<le> ?N`] p preal *[of ?N]
   386           show False
   387             by (cases "f t ?N") (auto simp: power_le_zero_eq split: split_if_asm)
   388         qed
   389       next
   390         case infinite
   391         { fix j have "f t j = j*2^j" using top[of j t] infinite by simp
   392           hence "of_nat j \<le> y" using *[of j]
   393             by (cases y) (auto simp: power_le_zero_eq zero_le_mult_iff field_simps) }
   394         note all_less_y = this
   395         show ?thesis unfolding infinite
   396         proof (rule ccontr)
   397           assume "\<not> \<omega> \<le> y" then obtain r where r: "y = Real r" "0 \<le> r" by (cases y) auto
   398           moreover obtain n ::nat where "r < real n" using ex_less_of_nat by (auto simp: real_eq_of_nat)
   399           with all_less_y[of n] r show False by auto
   400         qed
   401       qed
   402     qed
   403   qed
   404 qed
   405 
   406 lemma (in sigma_algebra) borel_measurable_implies_simple_function_sequence':
   407   fixes u :: "'a \<Rightarrow> pextreal"
   408   assumes "u \<in> borel_measurable M"
   409   obtains (x) f where "f \<up> u" "\<And>i. simple_function (f i)" "\<And>i. \<omega>\<notin>f i`space M"
   410 proof -
   411   from borel_measurable_implies_simple_function_sequence[OF assms]
   412   obtain f where x: "\<And>i. simple_function (f i)" "f \<up> u"
   413     and fin: "\<And>i. \<And>x. x\<in>space M \<Longrightarrow> f i x \<noteq> \<omega>" by auto
   414   { fix i from fin[of _ i] have "\<omega> \<notin> f i`space M" by fastsimp }
   415   with x show thesis by (auto intro!: that[of f])
   416 qed
   417 
   418 lemma (in sigma_algebra) simple_function_eq_borel_measurable:
   419   fixes f :: "'a \<Rightarrow> pextreal"
   420   shows "simple_function f \<longleftrightarrow>
   421     finite (f`space M) \<and> f \<in> borel_measurable M"
   422   using simple_function_borel_measurable[of f]
   423     borel_measurable_simple_function[of f]
   424   by (fastsimp simp: simple_function_def)
   425 
   426 lemma (in measure_space) simple_function_restricted:
   427   fixes f :: "'a \<Rightarrow> pextreal" assumes "A \<in> sets M"
   428   shows "sigma_algebra.simple_function (restricted_space A) f \<longleftrightarrow> simple_function (\<lambda>x. f x * indicator A x)"
   429     (is "sigma_algebra.simple_function ?R f \<longleftrightarrow> simple_function ?f")
   430 proof -
   431   interpret R: sigma_algebra ?R by (rule restricted_sigma_algebra[OF `A \<in> sets M`])
   432   have "finite (f`A) \<longleftrightarrow> finite (?f`space M)"
   433   proof cases
   434     assume "A = space M"
   435     then have "f`A = ?f`space M" by (fastsimp simp: image_iff)
   436     then show ?thesis by simp
   437   next
   438     assume "A \<noteq> space M"
   439     then obtain x where x: "x \<in> space M" "x \<notin> A"
   440       using sets_into_space `A \<in> sets M` by auto
   441     have *: "?f`space M = f`A \<union> {0}"
   442     proof (auto simp add: image_iff)
   443       show "\<exists>x\<in>space M. f x = 0 \<or> indicator A x = 0"
   444         using x by (auto intro!: bexI[of _ x])
   445     next
   446       fix x assume "x \<in> A"
   447       then show "\<exists>y\<in>space M. f x = f y * indicator A y"
   448         using `A \<in> sets M` sets_into_space by (auto intro!: bexI[of _ x])
   449     next
   450       fix x
   451       assume "indicator A x \<noteq> (0::pextreal)"
   452       then have "x \<in> A" by (auto simp: indicator_def split: split_if_asm)
   453       moreover assume "x \<in> space M" "\<forall>y\<in>A. ?f x \<noteq> f y"
   454       ultimately show "f x = 0" by auto
   455     qed
   456     then show ?thesis by auto
   457   qed
   458   then show ?thesis
   459     unfolding simple_function_eq_borel_measurable
   460       R.simple_function_eq_borel_measurable
   461     unfolding borel_measurable_restricted[OF `A \<in> sets M`]
   462     by auto
   463 qed
   464 
   465 lemma (in sigma_algebra) simple_function_subalgebra:
   466   assumes "sigma_algebra.simple_function N f"
   467   and N_subalgebra: "sets N \<subseteq> sets M" "space N = space M" "sigma_algebra N"
   468   shows "simple_function f"
   469   using assms
   470   unfolding simple_function_def
   471   unfolding sigma_algebra.simple_function_def[OF N_subalgebra(3)]
   472   by auto
   473 
   474 lemma (in measure_space) simple_function_vimage:
   475   assumes T: "sigma_algebra M'" "T \<in> measurable M M'"
   476     and f: "sigma_algebra.simple_function M' f"
   477   shows "simple_function (\<lambda>x. f (T x))"
   478 proof (intro simple_function_def[THEN iffD2] conjI ballI)
   479   interpret T: sigma_algebra M' by fact
   480   have "(\<lambda>x. f (T x)) ` space M \<subseteq> f ` space M'"
   481     using T unfolding measurable_def by auto
   482   then show "finite ((\<lambda>x. f (T x)) ` space M)"
   483     using f unfolding T.simple_function_def by (auto intro: finite_subset)
   484   fix i assume i: "i \<in> (\<lambda>x. f (T x)) ` space M"
   485   then have "i \<in> f ` space M'"
   486     using T unfolding measurable_def by auto
   487   then have "f -` {i} \<inter> space M' \<in> sets M'"
   488     using f unfolding T.simple_function_def by auto
   489   then have "T -` (f -` {i} \<inter> space M') \<inter> space M \<in> sets M"
   490     using T unfolding measurable_def by auto
   491   also have "T -` (f -` {i} \<inter> space M') \<inter> space M = (\<lambda>x. f (T x)) -` {i} \<inter> space M"
   492     using T unfolding measurable_def by auto
   493   finally show "(\<lambda>x. f (T x)) -` {i} \<inter> space M \<in> sets M" .
   494 qed
   495 
   496 section "Simple integral"
   497 
   498 definition (in measure_space) simple_integral (binder "\<integral>\<^isup>S " 10) where
   499   "simple_integral f = (\<Sum>x \<in> f ` space M. x * \<mu> (f -` {x} \<inter> space M))"
   500 
   501 lemma (in measure_space) simple_integral_cong:
   502   assumes "\<And>t. t \<in> space M \<Longrightarrow> f t = g t"
   503   shows "simple_integral f = simple_integral g"
   504 proof -
   505   have "f ` space M = g ` space M"
   506     "\<And>x. f -` {x} \<inter> space M = g -` {x} \<inter> space M"
   507     using assms by (auto intro!: image_eqI)
   508   thus ?thesis unfolding simple_integral_def by simp
   509 qed
   510 
   511 lemma (in measure_space) simple_integral_cong_measure:
   512   assumes "\<And>A. A \<in> sets M \<Longrightarrow> \<nu> A = \<mu> A" and "simple_function f"
   513   shows "measure_space.simple_integral M \<nu> f = simple_integral f"
   514 proof -
   515   interpret v: measure_space M \<nu>
   516     by (rule measure_space_cong) fact
   517   from simple_functionD[OF `simple_function f`] assms show ?thesis
   518     unfolding simple_integral_def v.simple_integral_def
   519     by (auto intro!: setsum_cong)
   520 qed
   521 
   522 lemma (in measure_space) simple_integral_const[simp]:
   523   "(\<integral>\<^isup>Sx. c) = c * \<mu> (space M)"
   524 proof (cases "space M = {}")
   525   case True thus ?thesis unfolding simple_integral_def by simp
   526 next
   527   case False hence "(\<lambda>x. c) ` space M = {c}" by auto
   528   thus ?thesis unfolding simple_integral_def by simp
   529 qed
   530 
   531 lemma (in measure_space) simple_function_partition:
   532   assumes "simple_function f" and "simple_function g"
   533   shows "simple_integral f = (\<Sum>A\<in>(\<lambda>x. f -` {f x} \<inter> g -` {g x} \<inter> space M) ` space M. the_elem (f`A) * \<mu> A)"
   534     (is "_ = setsum _ (?p ` space M)")
   535 proof-
   536   let "?sub x" = "?p ` (f -` {x} \<inter> space M)"
   537   let ?SIGMA = "Sigma (f`space M) ?sub"
   538 
   539   have [intro]:
   540     "finite (f ` space M)"
   541     "finite (g ` space M)"
   542     using assms unfolding simple_function_def by simp_all
   543 
   544   { fix A
   545     have "?p ` (A \<inter> space M) \<subseteq>
   546       (\<lambda>(x,y). f -` {x} \<inter> g -` {y} \<inter> space M) ` (f`space M \<times> g`space M)"
   547       by auto
   548     hence "finite (?p ` (A \<inter> space M))"
   549       by (rule finite_subset) auto }
   550   note this[intro, simp]
   551 
   552   { fix x assume "x \<in> space M"
   553     have "\<Union>(?sub (f x)) = (f -` {f x} \<inter> space M)" by auto
   554     moreover {
   555       fix x y
   556       have *: "f -` {f x} \<inter> g -` {g x} \<inter> space M
   557           = (f -` {f x} \<inter> space M) \<inter> (g -` {g x} \<inter> space M)" by auto
   558       assume "x \<in> space M" "y \<in> space M"
   559       hence "f -` {f x} \<inter> g -` {g x} \<inter> space M \<in> sets M"
   560         using assms unfolding simple_function_def * by auto }
   561     ultimately
   562     have "\<mu> (f -` {f x} \<inter> space M) = setsum (\<mu>) (?sub (f x))"
   563       by (subst measure_finitely_additive) auto }
   564   hence "simple_integral f = (\<Sum>(x,A)\<in>?SIGMA. x * \<mu> A)"
   565     unfolding simple_integral_def
   566     by (subst setsum_Sigma[symmetric],
   567        auto intro!: setsum_cong simp: setsum_right_distrib[symmetric])
   568   also have "\<dots> = (\<Sum>A\<in>?p ` space M. the_elem (f`A) * \<mu> A)"
   569   proof -
   570     have [simp]: "\<And>x. x \<in> space M \<Longrightarrow> f ` ?p x = {f x}" by (auto intro!: imageI)
   571     have "(\<lambda>A. (the_elem (f ` A), A)) ` ?p ` space M
   572       = (\<lambda>x. (f x, ?p x)) ` space M"
   573     proof safe
   574       fix x assume "x \<in> space M"
   575       thus "(f x, ?p x) \<in> (\<lambda>A. (the_elem (f`A), A)) ` ?p ` space M"
   576         by (auto intro!: image_eqI[of _ _ "?p x"])
   577     qed auto
   578     thus ?thesis
   579       apply (auto intro!: setsum_reindex_cong[of "\<lambda>A. (the_elem (f`A), A)"] inj_onI)
   580       apply (rule_tac x="xa" in image_eqI)
   581       by simp_all
   582   qed
   583   finally show ?thesis .
   584 qed
   585 
   586 lemma (in measure_space) simple_integral_add[simp]:
   587   assumes "simple_function f" and "simple_function g"
   588   shows "(\<integral>\<^isup>Sx. f x + g x) = simple_integral f + simple_integral g"
   589 proof -
   590   { fix x let ?S = "g -` {g x} \<inter> f -` {f x} \<inter> space M"
   591     assume "x \<in> space M"
   592     hence "(\<lambda>a. f a + g a) ` ?S = {f x + g x}" "f ` ?S = {f x}" "g ` ?S = {g x}"
   593         "(\<lambda>x. (f x, g x)) -` {(f x, g x)} \<inter> space M = ?S"
   594       by auto }
   595   thus ?thesis
   596     unfolding
   597       simple_function_partition[OF simple_function_add[OF assms] simple_function_Pair[OF assms]]
   598       simple_function_partition[OF `simple_function f` `simple_function g`]
   599       simple_function_partition[OF `simple_function g` `simple_function f`]
   600     apply (subst (3) Int_commute)
   601     by (auto simp add: field_simps setsum_addf[symmetric] intro!: setsum_cong)
   602 qed
   603 
   604 lemma (in measure_space) simple_integral_setsum[simp]:
   605   assumes "\<And>i. i \<in> P \<Longrightarrow> simple_function (f i)"
   606   shows "(\<integral>\<^isup>Sx. \<Sum>i\<in>P. f i x) = (\<Sum>i\<in>P. simple_integral (f i))"
   607 proof cases
   608   assume "finite P"
   609   from this assms show ?thesis
   610     by induct (auto simp: simple_function_setsum simple_integral_add)
   611 qed auto
   612 
   613 lemma (in measure_space) simple_integral_mult[simp]:
   614   assumes "simple_function f"
   615   shows "(\<integral>\<^isup>Sx. c * f x) = c * simple_integral f"
   616 proof -
   617   note mult = simple_function_mult[OF simple_function_const[of c] assms]
   618   { fix x let ?S = "f -` {f x} \<inter> (\<lambda>x. c * f x) -` {c * f x} \<inter> space M"
   619     assume "x \<in> space M"
   620     hence "(\<lambda>x. c * f x) ` ?S = {c * f x}" "f ` ?S = {f x}"
   621       by auto }
   622   thus ?thesis
   623     unfolding simple_function_partition[OF mult assms]
   624       simple_function_partition[OF assms mult]
   625     by (auto simp: setsum_right_distrib field_simps intro!: setsum_cong)
   626 qed
   627 
   628 lemma (in sigma_algebra) simple_function_If:
   629   assumes sf: "simple_function f" "simple_function g" and A: "A \<in> sets M"
   630   shows "simple_function (\<lambda>x. if x \<in> A then f x else g x)" (is "simple_function ?IF")
   631 proof -
   632   def F \<equiv> "\<lambda>x. f -` {x} \<inter> space M" and G \<equiv> "\<lambda>x. g -` {x} \<inter> space M"
   633   show ?thesis unfolding simple_function_def
   634   proof safe
   635     have "?IF ` space M \<subseteq> f ` space M \<union> g ` space M" by auto
   636     from finite_subset[OF this] assms
   637     show "finite (?IF ` space M)" unfolding simple_function_def by auto
   638   next
   639     fix x assume "x \<in> space M"
   640     then have *: "?IF -` {?IF x} \<inter> space M = (if x \<in> A
   641       then ((F (f x) \<inter> A) \<union> (G (f x) - (G (f x) \<inter> A)))
   642       else ((F (g x) \<inter> A) \<union> (G (g x) - (G (g x) \<inter> A))))"
   643       using sets_into_space[OF A] by (auto split: split_if_asm simp: G_def F_def)
   644     have [intro]: "\<And>x. F x \<in> sets M" "\<And>x. G x \<in> sets M"
   645       unfolding F_def G_def using sf[THEN simple_functionD(2)] by auto
   646     show "?IF -` {?IF x} \<inter> space M \<in> sets M" unfolding * using A by auto
   647   qed
   648 qed
   649 
   650 lemma (in measure_space) simple_integral_mono_AE:
   651   assumes "simple_function f" and "simple_function g"
   652   and mono: "AE x. f x \<le> g x"
   653   shows "simple_integral f \<le> simple_integral g"
   654 proof -
   655   let "?S x" = "(g -` {g x} \<inter> space M) \<inter> (f -` {f x} \<inter> space M)"
   656   have *: "\<And>x. g -` {g x} \<inter> f -` {f x} \<inter> space M = ?S x"
   657     "\<And>x. f -` {f x} \<inter> g -` {g x} \<inter> space M = ?S x" by auto
   658   show ?thesis
   659     unfolding *
   660       simple_function_partition[OF `simple_function f` `simple_function g`]
   661       simple_function_partition[OF `simple_function g` `simple_function f`]
   662   proof (safe intro!: setsum_mono)
   663     fix x assume "x \<in> space M"
   664     then have *: "f ` ?S x = {f x}" "g ` ?S x = {g x}" by auto
   665     show "the_elem (f`?S x) * \<mu> (?S x) \<le> the_elem (g`?S x) * \<mu> (?S x)"
   666     proof (cases "f x \<le> g x")
   667       case True then show ?thesis using * by (auto intro!: mult_right_mono)
   668     next
   669       case False
   670       obtain N where N: "{x\<in>space M. \<not> f x \<le> g x} \<subseteq> N" "N \<in> sets M" "\<mu> N = 0"
   671         using mono by (auto elim!: AE_E)
   672       have "?S x \<subseteq> N" using N `x \<in> space M` False by auto
   673       moreover have "?S x \<in> sets M" using assms
   674         by (rule_tac Int) (auto intro!: simple_functionD)
   675       ultimately have "\<mu> (?S x) \<le> \<mu> N"
   676         using `N \<in> sets M` by (auto intro!: measure_mono)
   677       then show ?thesis using `\<mu> N = 0` by auto
   678     qed
   679   qed
   680 qed
   681 
   682 lemma (in measure_space) simple_integral_mono:
   683   assumes "simple_function f" and "simple_function g"
   684   and mono: "\<And> x. x \<in> space M \<Longrightarrow> f x \<le> g x"
   685   shows "simple_integral f \<le> simple_integral g"
   686 proof (rule simple_integral_mono_AE[OF assms(1, 2)])
   687   show "AE x. f x \<le> g x"
   688     using mono by (rule AE_cong) auto
   689 qed
   690 
   691 lemma (in measure_space) simple_integral_cong_AE:
   692   assumes "simple_function f" "simple_function g" and "AE x. f x = g x"
   693   shows "simple_integral f = simple_integral g"
   694   using assms by (auto simp: eq_iff intro!: simple_integral_mono_AE)
   695 
   696 lemma (in measure_space) simple_integral_cong':
   697   assumes sf: "simple_function f" "simple_function g"
   698   and mea: "\<mu> {x\<in>space M. f x \<noteq> g x} = 0"
   699   shows "simple_integral f = simple_integral g"
   700 proof (intro simple_integral_cong_AE sf AE_I)
   701   show "\<mu> {x\<in>space M. f x \<noteq> g x} = 0" by fact
   702   show "{x \<in> space M. f x \<noteq> g x} \<in> sets M"
   703     using sf[THEN borel_measurable_simple_function] by auto
   704 qed simp
   705 
   706 lemma (in measure_space) simple_integral_indicator:
   707   assumes "A \<in> sets M"
   708   assumes "simple_function f"
   709   shows "(\<integral>\<^isup>Sx. f x * indicator A x) =
   710     (\<Sum>x \<in> f ` space M. x * \<mu> (f -` {x} \<inter> space M \<inter> A))"
   711 proof cases
   712   assume "A = space M"
   713   moreover hence "(\<integral>\<^isup>Sx. f x * indicator A x) = simple_integral f"
   714     by (auto intro!: simple_integral_cong)
   715   moreover have "\<And>X. X \<inter> space M \<inter> space M = X \<inter> space M" by auto
   716   ultimately show ?thesis by (simp add: simple_integral_def)
   717 next
   718   assume "A \<noteq> space M"
   719   then obtain x where x: "x \<in> space M" "x \<notin> A" using sets_into_space[OF assms(1)] by auto
   720   have I: "(\<lambda>x. f x * indicator A x) ` space M = f ` A \<union> {0}" (is "?I ` _ = _")
   721   proof safe
   722     fix y assume "?I y \<notin> f ` A" hence "y \<notin> A" by auto thus "?I y = 0" by auto
   723   next
   724     fix y assume "y \<in> A" thus "f y \<in> ?I ` space M"
   725       using sets_into_space[OF assms(1)] by (auto intro!: image_eqI[of _ _ y])
   726   next
   727     show "0 \<in> ?I ` space M" using x by (auto intro!: image_eqI[of _ _ x])
   728   qed
   729   have *: "(\<integral>\<^isup>Sx. f x * indicator A x) =
   730     (\<Sum>x \<in> f ` space M \<union> {0}. x * \<mu> (f -` {x} \<inter> space M \<inter> A))"
   731     unfolding simple_integral_def I
   732   proof (rule setsum_mono_zero_cong_left)
   733     show "finite (f ` space M \<union> {0})"
   734       using assms(2) unfolding simple_function_def by auto
   735     show "f ` A \<union> {0} \<subseteq> f`space M \<union> {0}"
   736       using sets_into_space[OF assms(1)] by auto
   737     have "\<And>x. f x \<notin> f ` A \<Longrightarrow> f -` {f x} \<inter> space M \<inter> A = {}"
   738       by (auto simp: image_iff)
   739     thus "\<forall>i\<in>f ` space M \<union> {0} - (f ` A \<union> {0}).
   740       i * \<mu> (f -` {i} \<inter> space M \<inter> A) = 0" by auto
   741   next
   742     fix x assume "x \<in> f`A \<union> {0}"
   743     hence "x \<noteq> 0 \<Longrightarrow> ?I -` {x} \<inter> space M = f -` {x} \<inter> space M \<inter> A"
   744       by (auto simp: indicator_def split: split_if_asm)
   745     thus "x * \<mu> (?I -` {x} \<inter> space M) =
   746       x * \<mu> (f -` {x} \<inter> space M \<inter> A)" by (cases "x = 0") simp_all
   747   qed
   748   show ?thesis unfolding *
   749     using assms(2) unfolding simple_function_def
   750     by (auto intro!: setsum_mono_zero_cong_right)
   751 qed
   752 
   753 lemma (in measure_space) simple_integral_indicator_only[simp]:
   754   assumes "A \<in> sets M"
   755   shows "simple_integral (indicator A) = \<mu> A"
   756 proof cases
   757   assume "space M = {}" hence "A = {}" using sets_into_space[OF assms] by auto
   758   thus ?thesis unfolding simple_integral_def using `space M = {}` by auto
   759 next
   760   assume "space M \<noteq> {}" hence "(\<lambda>x. 1) ` space M = {1::pextreal}" by auto
   761   thus ?thesis
   762     using simple_integral_indicator[OF assms simple_function_const[of 1]]
   763     using sets_into_space[OF assms]
   764     by (auto intro!: arg_cong[where f="\<mu>"])
   765 qed
   766 
   767 lemma (in measure_space) simple_integral_null_set:
   768   assumes "simple_function u" "N \<in> null_sets"
   769   shows "(\<integral>\<^isup>Sx. u x * indicator N x) = 0"
   770 proof -
   771   have "AE x. indicator N x = (0 :: pextreal)"
   772     using `N \<in> null_sets` by (auto simp: indicator_def intro!: AE_I[of _ N])
   773   then have "(\<integral>\<^isup>Sx. u x * indicator N x) = (\<integral>\<^isup>Sx. 0)"
   774     using assms by (intro simple_integral_cong_AE) (auto intro!: AE_disjI2)
   775   then show ?thesis by simp
   776 qed
   777 
   778 lemma (in measure_space) simple_integral_cong_AE_mult_indicator:
   779   assumes sf: "simple_function f" and eq: "AE x. x \<in> S" and "S \<in> sets M"
   780   shows "simple_integral f = (\<integral>\<^isup>Sx. f x * indicator S x)"
   781 proof (rule simple_integral_cong_AE)
   782   show "simple_function f" by fact
   783   show "simple_function (\<lambda>x. f x * indicator S x)"
   784     using sf `S \<in> sets M` by auto
   785   from eq show "AE x. f x = f x * indicator S x"
   786     by (rule AE_mp) simp
   787 qed
   788 
   789 lemma (in measure_space) simple_integral_restricted:
   790   assumes "A \<in> sets M"
   791   assumes sf: "simple_function (\<lambda>x. f x * indicator A x)"
   792   shows "measure_space.simple_integral (restricted_space A) \<mu> f = (\<integral>\<^isup>Sx. f x * indicator A x)"
   793     (is "_ = simple_integral ?f")
   794   unfolding measure_space.simple_integral_def[OF restricted_measure_space[OF `A \<in> sets M`]]
   795   unfolding simple_integral_def
   796 proof (simp, safe intro!: setsum_mono_zero_cong_left)
   797   from sf show "finite (?f ` space M)"
   798     unfolding simple_function_def by auto
   799 next
   800   fix x assume "x \<in> A"
   801   then show "f x \<in> ?f ` space M"
   802     using sets_into_space `A \<in> sets M` by (auto intro!: image_eqI[of _ _ x])
   803 next
   804   fix x assume "x \<in> space M" "?f x \<notin> f`A"
   805   then have "x \<notin> A" by (auto simp: image_iff)
   806   then show "?f x * \<mu> (?f -` {?f x} \<inter> space M) = 0" by simp
   807 next
   808   fix x assume "x \<in> A"
   809   then have "f x \<noteq> 0 \<Longrightarrow>
   810     f -` {f x} \<inter> A = ?f -` {f x} \<inter> space M"
   811     using `A \<in> sets M` sets_into_space
   812     by (auto simp: indicator_def split: split_if_asm)
   813   then show "f x * \<mu> (f -` {f x} \<inter> A) =
   814     f x * \<mu> (?f -` {f x} \<inter> space M)"
   815     unfolding pextreal_mult_cancel_left by auto
   816 qed
   817 
   818 lemma (in measure_space) simple_integral_subalgebra:
   819   assumes N: "measure_space N \<mu>" and [simp]: "space N = space M"
   820   shows "measure_space.simple_integral N \<mu> = simple_integral"
   821   unfolding simple_integral_def_raw
   822   unfolding measure_space.simple_integral_def_raw[OF N] by simp
   823 
   824 lemma (in measure_space) simple_integral_vimage:
   825   assumes T: "sigma_algebra M'" "T \<in> measurable M M'"
   826     and f: "sigma_algebra.simple_function M' f"
   827   shows "measure_space.simple_integral M' (\<lambda>A. \<mu> (T -` A \<inter> space M)) f = (\<integral>\<^isup>S x. f (T x))"
   828     (is "measure_space.simple_integral M' ?nu f = _")
   829 proof -
   830   interpret T: measure_space M' ?nu using T by (rule measure_space_vimage) auto
   831   show "T.simple_integral f = (\<integral>\<^isup>S x. f (T x))"
   832     unfolding simple_integral_def T.simple_integral_def
   833   proof (intro setsum_mono_zero_cong_right ballI)
   834     show "(\<lambda>x. f (T x)) ` space M \<subseteq> f ` space M'"
   835       using T unfolding measurable_def by auto
   836     show "finite (f ` space M')"
   837       using f unfolding T.simple_function_def by auto
   838   next
   839     fix i assume "i \<in> f ` space M' - (\<lambda>x. f (T x)) ` space M"
   840     then have "T -` (f -` {i} \<inter> space M') \<inter> space M = {}" by (auto simp: image_iff)
   841     then show "i * \<mu> (T -` (f -` {i} \<inter> space M') \<inter> space M) = 0" by simp
   842   next
   843     fix i assume "i \<in> (\<lambda>x. f (T x)) ` space M"
   844     then have "T -` (f -` {i} \<inter> space M') \<inter> space M = (\<lambda>x. f (T x)) -` {i} \<inter> space M"
   845       using T unfolding measurable_def by auto
   846     then show "i * \<mu> (T -` (f -` {i} \<inter> space M') \<inter> space M) = i * \<mu> ((\<lambda>x. f (T x)) -` {i} \<inter> space M)"
   847       by auto
   848   qed
   849 qed
   850 
   851 section "Continuous posititve integration"
   852 
   853 definition (in measure_space) positive_integral (binder "\<integral>\<^isup>+ " 10) where
   854   "positive_integral f = SUPR {g. simple_function g \<and> g \<le> f} simple_integral"
   855 
   856 lemma (in measure_space) positive_integral_alt:
   857   "positive_integral f =
   858     (SUPR {g. simple_function g \<and> g \<le> f \<and> \<omega> \<notin> g`space M} simple_integral)" (is "_ = ?alt")
   859 proof (rule antisym SUP_leI)
   860   show "positive_integral f \<le> ?alt" unfolding positive_integral_def
   861   proof (safe intro!: SUP_leI)
   862     fix g assume g: "simple_function g" "g \<le> f"
   863     let ?G = "g -` {\<omega>} \<inter> space M"
   864     show "simple_integral g \<le>
   865       SUPR {g. simple_function g \<and> g \<le> f \<and> \<omega> \<notin> g ` space M} simple_integral"
   866       (is "simple_integral g \<le> SUPR ?A simple_integral")
   867     proof cases
   868       let ?g = "\<lambda>x. indicator (space M - ?G) x * g x"
   869       have g': "simple_function ?g"
   870         using g by (auto intro: simple_functionD)
   871       moreover
   872       assume "\<mu> ?G = 0"
   873       then have "AE x. g x = ?g x" using g
   874         by (intro AE_I[where N="?G"])
   875            (auto intro: simple_functionD simp: indicator_def)
   876       with g(1) g' have "simple_integral g = simple_integral ?g"
   877         by (rule simple_integral_cong_AE)
   878       moreover have "?g \<le> g" by (auto simp: le_fun_def indicator_def)
   879       from this `g \<le> f` have "?g \<le> f" by (rule order_trans)
   880       moreover have "\<omega> \<notin> ?g ` space M"
   881         by (auto simp: indicator_def split: split_if_asm)
   882       ultimately show ?thesis by (auto intro!: le_SUPI)
   883     next
   884       assume "\<mu> ?G \<noteq> 0"
   885       then have "?G \<noteq> {}" by auto
   886       then have "\<omega> \<in> g`space M" by force
   887       then have "space M \<noteq> {}" by auto
   888       have "SUPR ?A simple_integral = \<omega>"
   889       proof (intro SUP_\<omega>[THEN iffD2] allI impI)
   890         fix x assume "x < \<omega>"
   891         then guess n unfolding less_\<omega>_Ex_of_nat .. note n = this
   892         then have "0 < n" by (intro neq0_conv[THEN iffD1] notI) simp
   893         let ?g = "\<lambda>x. (of_nat n / (if \<mu> ?G = \<omega> then 1 else \<mu> ?G)) * indicator ?G x"
   894         show "\<exists>i\<in>?A. x < simple_integral i"
   895         proof (intro bexI impI CollectI conjI)
   896           show "simple_function ?g" using g
   897             by (auto intro!: simple_functionD simple_function_add)
   898           have "?g \<le> g" by (auto simp: le_fun_def indicator_def)
   899           from this g(2) show "?g \<le> f" by (rule order_trans)
   900           show "\<omega> \<notin> ?g ` space M"
   901             using `\<mu> ?G \<noteq> 0` by (auto simp: indicator_def split: split_if_asm)
   902           have "x < (of_nat n / (if \<mu> ?G = \<omega> then 1 else \<mu> ?G)) * \<mu> ?G"
   903             using n `\<mu> ?G \<noteq> 0` `0 < n`
   904             by (auto simp: pextreal_noteq_omega_Ex field_simps)
   905           also have "\<dots> = simple_integral ?g" using g `space M \<noteq> {}`
   906             by (subst simple_integral_indicator)
   907                (auto simp: image_constant ac_simps dest: simple_functionD)
   908           finally show "x < simple_integral ?g" .
   909         qed
   910       qed
   911       then show ?thesis by simp
   912     qed
   913   qed
   914 qed (auto intro!: SUP_subset simp: positive_integral_def)
   915 
   916 lemma (in measure_space) positive_integral_cong_measure:
   917   assumes "\<And>A. A \<in> sets M \<Longrightarrow> \<nu> A = \<mu> A"
   918   shows "measure_space.positive_integral M \<nu> f = positive_integral f"
   919 proof -
   920   interpret v: measure_space M \<nu>
   921     by (rule measure_space_cong) fact
   922   with assms show ?thesis
   923     unfolding positive_integral_def v.positive_integral_def SUPR_def
   924     by (auto intro!: arg_cong[where f=Sup] image_cong
   925              simp: simple_integral_cong_measure[of \<nu>])
   926 qed
   927 
   928 lemma (in measure_space) positive_integral_alt1:
   929   "positive_integral f =
   930     (SUP g : {g. simple_function g \<and> (\<forall>x\<in>space M. g x \<le> f x \<and> g x \<noteq> \<omega>)}. simple_integral g)"
   931   unfolding positive_integral_alt SUPR_def
   932 proof (safe intro!: arg_cong[where f=Sup])
   933   fix g let ?g = "\<lambda>x. if x \<in> space M then g x else f x"
   934   assume "simple_function g" "\<forall>x\<in>space M. g x \<le> f x \<and> g x \<noteq> \<omega>"
   935   hence "?g \<le> f" "simple_function ?g" "simple_integral g = simple_integral ?g"
   936     "\<omega> \<notin> g`space M"
   937     unfolding le_fun_def by (auto cong: simple_function_cong simple_integral_cong)
   938   thus "simple_integral g \<in> simple_integral ` {g. simple_function g \<and> g \<le> f \<and> \<omega> \<notin> g`space M}"
   939     by auto
   940 next
   941   fix g assume "simple_function g" "g \<le> f" "\<omega> \<notin> g`space M"
   942   hence "simple_function g" "\<forall>x\<in>space M. g x \<le> f x \<and> g x \<noteq> \<omega>"
   943     by (auto simp add: le_fun_def image_iff)
   944   thus "simple_integral g \<in> simple_integral ` {g. simple_function g \<and> (\<forall>x\<in>space M. g x \<le> f x \<and> g x \<noteq> \<omega>)}"
   945     by auto
   946 qed
   947 
   948 lemma (in measure_space) positive_integral_cong:
   949   assumes "\<And>x. x \<in> space M \<Longrightarrow> f x = g x"
   950   shows "positive_integral f = positive_integral g"
   951 proof -
   952   have "\<And>h. (\<forall>x\<in>space M. h x \<le> f x \<and> h x \<noteq> \<omega>) = (\<forall>x\<in>space M. h x \<le> g x \<and> h x \<noteq> \<omega>)"
   953     using assms by auto
   954   thus ?thesis unfolding positive_integral_alt1 by auto
   955 qed
   956 
   957 lemma (in measure_space) positive_integral_eq_simple_integral:
   958   assumes "simple_function f"
   959   shows "positive_integral f = simple_integral f"
   960   unfolding positive_integral_def
   961 proof (safe intro!: pextreal_SUPI)
   962   fix g assume "simple_function g" "g \<le> f"
   963   with assms show "simple_integral g \<le> simple_integral f"
   964     by (auto intro!: simple_integral_mono simp: le_fun_def)
   965 next
   966   fix y assume "\<forall>x. x\<in>{g. simple_function g \<and> g \<le> f} \<longrightarrow> simple_integral x \<le> y"
   967   with assms show "simple_integral f \<le> y" by auto
   968 qed
   969 
   970 lemma (in measure_space) positive_integral_mono_AE:
   971   assumes ae: "AE x. u x \<le> v x"
   972   shows "positive_integral u \<le> positive_integral v"
   973   unfolding positive_integral_alt1
   974 proof (safe intro!: SUPR_mono)
   975   fix a assume a: "simple_function a" and mono: "\<forall>x\<in>space M. a x \<le> u x \<and> a x \<noteq> \<omega>"
   976   from ae obtain N where N: "{x\<in>space M. \<not> u x \<le> v x} \<subseteq> N" "N \<in> sets M" "\<mu> N = 0"
   977     by (auto elim!: AE_E)
   978   have "simple_function (\<lambda>x. a x * indicator (space M - N) x)"
   979     using `N \<in> sets M` a by auto
   980   with a show "\<exists>b\<in>{g. simple_function g \<and> (\<forall>x\<in>space M. g x \<le> v x \<and> g x \<noteq> \<omega>)}.
   981     simple_integral a \<le> simple_integral b"
   982   proof (safe intro!: bexI[of _ "\<lambda>x. a x * indicator (space M - N) x"]
   983                       simple_integral_mono_AE)
   984     show "AE x. a x \<le> a x * indicator (space M - N) x"
   985     proof (rule AE_I, rule subset_refl)
   986       have *: "{x \<in> space M. \<not> a x \<le> a x * indicator (space M - N) x} =
   987         N \<inter> {x \<in> space M. a x \<noteq> 0}" (is "?N = _")
   988         using `N \<in> sets M`[THEN sets_into_space] by (auto simp: indicator_def)
   989       then show "?N \<in> sets M" 
   990         using `N \<in> sets M` `simple_function a`[THEN borel_measurable_simple_function]
   991         by (auto intro!: measure_mono Int)
   992       then have "\<mu> ?N \<le> \<mu> N"
   993         unfolding * using `N \<in> sets M` by (auto intro!: measure_mono)
   994       then show "\<mu> ?N = 0" using `\<mu> N = 0` by auto
   995     qed
   996   next
   997     fix x assume "x \<in> space M"
   998     show "a x * indicator (space M - N) x \<le> v x"
   999     proof (cases "x \<in> N")
  1000       case True then show ?thesis by simp
  1001     next
  1002       case False
  1003       with N mono have "a x \<le> u x" "u x \<le> v x" using `x \<in> space M` by auto
  1004       with False `x \<in> space M` show "a x * indicator (space M - N) x \<le> v x" by auto
  1005     qed
  1006     assume "a x * indicator (space M - N) x = \<omega>"
  1007     with mono `x \<in> space M` show False
  1008       by (simp split: split_if_asm add: indicator_def)
  1009   qed
  1010 qed
  1011 
  1012 lemma (in measure_space) positive_integral_cong_AE:
  1013   "AE x. u x = v x \<Longrightarrow> positive_integral u = positive_integral v"
  1014   by (auto simp: eq_iff intro!: positive_integral_mono_AE)
  1015 
  1016 lemma (in measure_space) positive_integral_mono:
  1017   assumes mono: "\<And>x. x \<in> space M \<Longrightarrow> u x \<le> v x"
  1018   shows "positive_integral u \<le> positive_integral v"
  1019   using mono by (auto intro!: AE_cong positive_integral_mono_AE)
  1020 
  1021 lemma image_set_cong:
  1022   assumes A: "\<And>x. x \<in> A \<Longrightarrow> \<exists>y\<in>B. f x = g y"
  1023   assumes B: "\<And>y. y \<in> B \<Longrightarrow> \<exists>x\<in>A. g y = f x"
  1024   shows "f ` A = g ` B"
  1025   using assms by blast
  1026 
  1027 lemma (in measure_space) positive_integral_SUP_approx:
  1028   assumes "f \<up> s"
  1029   and f: "\<And>i. f i \<in> borel_measurable M"
  1030   and "simple_function u"
  1031   and le: "u \<le> s" and real: "\<omega> \<notin> u`space M"
  1032   shows "simple_integral u \<le> (SUP i. positive_integral (f i))" (is "_ \<le> ?S")
  1033 proof (rule pextreal_le_mult_one_interval)
  1034   fix a :: pextreal assume "0 < a" "a < 1"
  1035   hence "a \<noteq> 0" by auto
  1036   let "?B i" = "{x \<in> space M. a * u x \<le> f i x}"
  1037   have B: "\<And>i. ?B i \<in> sets M"
  1038     using f `simple_function u` by (auto simp: borel_measurable_simple_function)
  1039 
  1040   let "?uB i x" = "u x * indicator (?B i) x"
  1041 
  1042   { fix i have "?B i \<subseteq> ?B (Suc i)"
  1043     proof safe
  1044       fix i x assume "a * u x \<le> f i x"
  1045       also have "\<dots> \<le> f (Suc i) x"
  1046         using `f \<up> s` unfolding isoton_def le_fun_def by auto
  1047       finally show "a * u x \<le> f (Suc i) x" .
  1048     qed }
  1049   note B_mono = this
  1050 
  1051   have u: "\<And>x. x \<in> space M \<Longrightarrow> u -` {u x} \<inter> space M \<in> sets M"
  1052     using `simple_function u` by (auto simp add: simple_function_def)
  1053 
  1054   have "\<And>i. (\<lambda>n. (u -` {i} \<inter> space M) \<inter> ?B n) \<up> (u -` {i} \<inter> space M)" using B_mono unfolding isoton_def
  1055   proof safe
  1056     fix x i assume "x \<in> space M"
  1057     show "x \<in> (\<Union>i. (u -` {u x} \<inter> space M) \<inter> ?B i)"
  1058     proof cases
  1059       assume "u x = 0" thus ?thesis using `x \<in> space M` by simp
  1060     next
  1061       assume "u x \<noteq> 0"
  1062       with `a < 1` real `x \<in> space M`
  1063       have "a * u x < 1 * u x" by (rule_tac pextreal_mult_strict_right_mono) (auto simp: image_iff)
  1064       also have "\<dots> \<le> (SUP i. f i x)" using le `f \<up> s`
  1065         unfolding isoton_fun_expand by (auto simp: isoton_def le_fun_def)
  1066       finally obtain i where "a * u x < f i x" unfolding SUPR_def
  1067         by (auto simp add: less_Sup_iff)
  1068       hence "a * u x \<le> f i x" by auto
  1069       thus ?thesis using `x \<in> space M` by auto
  1070     qed
  1071   qed auto
  1072   note measure_conv = measure_up[OF Int[OF u B] this]
  1073 
  1074   have "simple_integral u = (SUP i. simple_integral (?uB i))"
  1075     unfolding simple_integral_indicator[OF B `simple_function u`]
  1076   proof (subst SUPR_pextreal_setsum, safe)
  1077     fix x n assume "x \<in> space M"
  1078     have "\<mu> (u -` {u x} \<inter> space M \<inter> {x \<in> space M. a * u x \<le> f n x})
  1079       \<le> \<mu> (u -` {u x} \<inter> space M \<inter> {x \<in> space M. a * u x \<le> f (Suc n) x})"
  1080       using B_mono Int[OF u[OF `x \<in> space M`] B] by (auto intro!: measure_mono)
  1081     thus "u x * \<mu> (u -` {u x} \<inter> space M \<inter> ?B n)
  1082             \<le> u x * \<mu> (u -` {u x} \<inter> space M \<inter> ?B (Suc n))"
  1083       by (auto intro: mult_left_mono)
  1084   next
  1085     show "simple_integral u =
  1086       (\<Sum>i\<in>u ` space M. SUP n. i * \<mu> (u -` {i} \<inter> space M \<inter> ?B n))"
  1087       using measure_conv unfolding simple_integral_def isoton_def
  1088       by (auto intro!: setsum_cong simp: pextreal_SUP_cmult)
  1089   qed
  1090   moreover
  1091   have "a * (SUP i. simple_integral (?uB i)) \<le> ?S"
  1092     unfolding pextreal_SUP_cmult[symmetric]
  1093   proof (safe intro!: SUP_mono bexI)
  1094     fix i
  1095     have "a * simple_integral (?uB i) = (\<integral>\<^isup>Sx. a * ?uB i x)"
  1096       using B `simple_function u`
  1097       by (subst simple_integral_mult[symmetric]) (auto simp: field_simps)
  1098     also have "\<dots> \<le> positive_integral (f i)"
  1099     proof -
  1100       have "\<And>x. a * ?uB i x \<le> f i x" unfolding indicator_def by auto
  1101       hence *: "simple_function (\<lambda>x. a * ?uB i x)" using B assms(3)
  1102         by (auto intro!: simple_integral_mono)
  1103       show ?thesis unfolding positive_integral_eq_simple_integral[OF *, symmetric]
  1104         by (auto intro!: positive_integral_mono simp: indicator_def)
  1105     qed
  1106     finally show "a * simple_integral (?uB i) \<le> positive_integral (f i)"
  1107       by auto
  1108   qed simp
  1109   ultimately show "a * simple_integral u \<le> ?S" by simp
  1110 qed
  1111 
  1112 text {* Beppo-Levi monotone convergence theorem *}
  1113 lemma (in measure_space) positive_integral_isoton:
  1114   assumes "f \<up> u" "\<And>i. f i \<in> borel_measurable M"
  1115   shows "(\<lambda>i. positive_integral (f i)) \<up> positive_integral u"
  1116   unfolding isoton_def
  1117 proof safe
  1118   fix i show "positive_integral (f i) \<le> positive_integral (f (Suc i))"
  1119     apply (rule positive_integral_mono)
  1120     using `f \<up> u` unfolding isoton_def le_fun_def by auto
  1121 next
  1122   have u: "u = (SUP i. f i)" using `f \<up> u` unfolding isoton_def by auto
  1123 
  1124   show "(SUP i. positive_integral (f i)) = positive_integral u"
  1125   proof (rule antisym)
  1126     from `f \<up> u`[THEN isoton_Sup, unfolded le_fun_def]
  1127     show "(SUP j. positive_integral (f j)) \<le> positive_integral u"
  1128       by (auto intro!: SUP_leI positive_integral_mono)
  1129   next
  1130     show "positive_integral u \<le> (SUP i. positive_integral (f i))"
  1131       unfolding positive_integral_alt[of u]
  1132       by (auto intro!: SUP_leI positive_integral_SUP_approx[OF assms])
  1133   qed
  1134 qed
  1135 
  1136 lemma (in measure_space) positive_integral_monotone_convergence_SUP:
  1137   assumes "\<And>i x. x \<in> space M \<Longrightarrow> f i x \<le> f (Suc i) x"
  1138   assumes "\<And>i. f i \<in> borel_measurable M"
  1139   shows "(SUP i. positive_integral (f i)) = (\<integral>\<^isup>+ x. SUP i. f i x)"
  1140     (is "_ = positive_integral ?u")
  1141 proof -
  1142   show ?thesis
  1143   proof (rule antisym)
  1144     show "(SUP j. positive_integral (f j)) \<le> positive_integral ?u"
  1145       by (auto intro!: SUP_leI positive_integral_mono le_SUPI)
  1146   next
  1147     def rf \<equiv> "\<lambda>i. \<lambda>x\<in>space M. f i x" and ru \<equiv> "\<lambda>x\<in>space M. ?u x"
  1148     have "\<And>i. rf i \<in> borel_measurable M" unfolding rf_def
  1149       using assms by (simp cong: measurable_cong)
  1150     moreover have iso: "rf \<up> ru" using assms unfolding rf_def ru_def
  1151       unfolding isoton_def le_fun_def fun_eq_iff SUPR_apply
  1152       using SUP_const[OF UNIV_not_empty]
  1153       by (auto simp: restrict_def le_fun_def fun_eq_iff)
  1154     ultimately have "positive_integral ru \<le> (SUP i. positive_integral (rf i))"
  1155       unfolding positive_integral_alt[of ru]
  1156       by (auto simp: le_fun_def intro!: SUP_leI positive_integral_SUP_approx)
  1157     then show "positive_integral ?u \<le> (SUP i. positive_integral (f i))"
  1158       unfolding ru_def rf_def by (simp cong: positive_integral_cong)
  1159   qed
  1160 qed
  1161 
  1162 lemma (in measure_space) SUP_simple_integral_sequences:
  1163   assumes f: "f \<up> u" "\<And>i. simple_function (f i)"
  1164   and g: "g \<up> u" "\<And>i. simple_function (g i)"
  1165   shows "(SUP i. simple_integral (f i)) = (SUP i. simple_integral (g i))"
  1166     (is "SUPR _ ?F = SUPR _ ?G")
  1167 proof -
  1168   have "(SUP i. ?F i) = (SUP i. positive_integral (f i))"
  1169     using assms by (simp add: positive_integral_eq_simple_integral)
  1170   also have "\<dots> = positive_integral u"
  1171     using positive_integral_isoton[OF f(1) borel_measurable_simple_function[OF f(2)]]
  1172     unfolding isoton_def by simp
  1173   also have "\<dots> = (SUP i. positive_integral (g i))"
  1174     using positive_integral_isoton[OF g(1) borel_measurable_simple_function[OF g(2)]]
  1175     unfolding isoton_def by simp
  1176   also have "\<dots> = (SUP i. ?G i)"
  1177     using assms by (simp add: positive_integral_eq_simple_integral)
  1178   finally show ?thesis .
  1179 qed
  1180 
  1181 lemma (in measure_space) positive_integral_const[simp]:
  1182   "(\<integral>\<^isup>+ x. c) = c * \<mu> (space M)"
  1183   by (subst positive_integral_eq_simple_integral) auto
  1184 
  1185 lemma (in measure_space) positive_integral_isoton_simple:
  1186   assumes "f \<up> u" and e: "\<And>i. simple_function (f i)"
  1187   shows "(\<lambda>i. simple_integral (f i)) \<up> positive_integral u"
  1188   using positive_integral_isoton[OF `f \<up> u` e(1)[THEN borel_measurable_simple_function]]
  1189   unfolding positive_integral_eq_simple_integral[OF e] .
  1190 
  1191 lemma (in measure_space) positive_integral_vimage:
  1192   assumes T: "sigma_algebra M'" "T \<in> measurable M M'" and f: "f \<in> borel_measurable M'"
  1193   shows "measure_space.positive_integral M' (\<lambda>A. \<mu> (T -` A \<inter> space M)) f = (\<integral>\<^isup>+ x. f (T x))"
  1194     (is "measure_space.positive_integral M' ?nu f = _")
  1195 proof -
  1196   interpret T: measure_space M' ?nu using T by (rule measure_space_vimage) auto
  1197   obtain f' where f': "f' \<up> f" "\<And>i. T.simple_function (f' i)"
  1198     using T.borel_measurable_implies_simple_function_sequence[OF f] by blast
  1199   then have f: "(\<lambda>i x. f' i (T x)) \<up> (\<lambda>x. f (T x))" "\<And>i. simple_function (\<lambda>x. f' i (T x))"
  1200     using simple_function_vimage[OF T] unfolding isoton_fun_expand by auto
  1201   show "T.positive_integral f = (\<integral>\<^isup>+ x. f (T x))"
  1202     using positive_integral_isoton_simple[OF f]
  1203     using T.positive_integral_isoton_simple[OF f']
  1204     unfolding simple_integral_vimage[OF T f'(2)] isoton_def
  1205     by simp
  1206 qed
  1207 
  1208 lemma (in measure_space) positive_integral_linear:
  1209   assumes f: "f \<in> borel_measurable M"
  1210   and g: "g \<in> borel_measurable M"
  1211   shows "(\<integral>\<^isup>+ x. a * f x + g x) =
  1212       a * positive_integral f + positive_integral g"
  1213     (is "positive_integral ?L = _")
  1214 proof -
  1215   from borel_measurable_implies_simple_function_sequence'[OF f] guess u .
  1216   note u = this positive_integral_isoton_simple[OF this(1-2)]
  1217   from borel_measurable_implies_simple_function_sequence'[OF g] guess v .
  1218   note v = this positive_integral_isoton_simple[OF this(1-2)]
  1219   let "?L' i x" = "a * u i x + v i x"
  1220 
  1221   have "?L \<in> borel_measurable M"
  1222     using assms by simp
  1223   from borel_measurable_implies_simple_function_sequence'[OF this] guess l .
  1224   note positive_integral_isoton_simple[OF this(1-2)] and l = this
  1225   moreover have
  1226       "(SUP i. simple_integral (l i)) = (SUP i. simple_integral (?L' i))"
  1227   proof (rule SUP_simple_integral_sequences[OF l(1-2)])
  1228     show "?L' \<up> ?L" "\<And>i. simple_function (?L' i)"
  1229       using u v by (auto simp: isoton_fun_expand isoton_add isoton_cmult_right)
  1230   qed
  1231   moreover from u v have L'_isoton:
  1232       "(\<lambda>i. simple_integral (?L' i)) \<up> a * positive_integral f + positive_integral g"
  1233     by (simp add: isoton_add isoton_cmult_right)
  1234   ultimately show ?thesis by (simp add: isoton_def)
  1235 qed
  1236 
  1237 lemma (in measure_space) positive_integral_cmult:
  1238   assumes "f \<in> borel_measurable M"
  1239   shows "(\<integral>\<^isup>+ x. c * f x) = c * positive_integral f"
  1240   using positive_integral_linear[OF assms, of "\<lambda>x. 0" c] by auto
  1241 
  1242 lemma (in measure_space) positive_integral_multc:
  1243   assumes "f \<in> borel_measurable M"
  1244   shows "(\<integral>\<^isup>+ x. f x * c) = positive_integral f * c"
  1245   unfolding mult_commute[of _ c] positive_integral_cmult[OF assms] by simp
  1246 
  1247 lemma (in measure_space) positive_integral_indicator[simp]:
  1248   "A \<in> sets M \<Longrightarrow> (\<integral>\<^isup>+ x. indicator A x) = \<mu> A"
  1249   by (subst positive_integral_eq_simple_integral)
  1250      (auto simp: simple_function_indicator simple_integral_indicator)
  1251 
  1252 lemma (in measure_space) positive_integral_cmult_indicator:
  1253   "A \<in> sets M \<Longrightarrow> (\<integral>\<^isup>+ x. c * indicator A x) = c * \<mu> A"
  1254   by (subst positive_integral_eq_simple_integral)
  1255      (auto simp: simple_function_indicator simple_integral_indicator)
  1256 
  1257 lemma (in measure_space) positive_integral_add:
  1258   assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1259   shows "(\<integral>\<^isup>+ x. f x + g x) = positive_integral f + positive_integral g"
  1260   using positive_integral_linear[OF assms, of 1] by simp
  1261 
  1262 lemma (in measure_space) positive_integral_setsum:
  1263   assumes "\<And>i. i\<in>P \<Longrightarrow> f i \<in> borel_measurable M"
  1264   shows "(\<integral>\<^isup>+ x. \<Sum>i\<in>P. f i x) = (\<Sum>i\<in>P. positive_integral (f i))"
  1265 proof cases
  1266   assume "finite P"
  1267   from this assms show ?thesis
  1268   proof induct
  1269     case (insert i P)
  1270     have "f i \<in> borel_measurable M"
  1271       "(\<lambda>x. \<Sum>i\<in>P. f i x) \<in> borel_measurable M"
  1272       using insert by (auto intro!: borel_measurable_pextreal_setsum)
  1273     from positive_integral_add[OF this]
  1274     show ?case using insert by auto
  1275   qed simp
  1276 qed simp
  1277 
  1278 lemma (in measure_space) positive_integral_diff:
  1279   assumes f: "f \<in> borel_measurable M" and g: "g \<in> borel_measurable M"
  1280   and fin: "positive_integral g \<noteq> \<omega>"
  1281   and mono: "\<And>x. x \<in> space M \<Longrightarrow> g x \<le> f x"
  1282   shows "(\<integral>\<^isup>+ x. f x - g x) = positive_integral f - positive_integral g"
  1283 proof -
  1284   have borel: "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  1285     using f g by (rule borel_measurable_pextreal_diff)
  1286   have "(\<integral>\<^isup>+x. f x - g x) + positive_integral g =
  1287     positive_integral f"
  1288     unfolding positive_integral_add[OF borel g, symmetric]
  1289   proof (rule positive_integral_cong)
  1290     fix x assume "x \<in> space M"
  1291     from mono[OF this] show "f x - g x + g x = f x"
  1292       by (cases "f x", cases "g x", simp, simp, cases "g x", auto)
  1293   qed
  1294   with mono show ?thesis
  1295     by (subst minus_pextreal_eq2[OF _ fin]) (auto intro!: positive_integral_mono)
  1296 qed
  1297 
  1298 lemma (in measure_space) positive_integral_psuminf:
  1299   assumes "\<And>i. f i \<in> borel_measurable M"
  1300   shows "(\<integral>\<^isup>+ x. \<Sum>\<^isub>\<infinity> i. f i x) = (\<Sum>\<^isub>\<infinity> i. positive_integral (f i))"
  1301 proof -
  1302   have "(\<lambda>i. (\<integral>\<^isup>+x. \<Sum>i<i. f i x)) \<up> (\<integral>\<^isup>+x. \<Sum>\<^isub>\<infinity>i. f i x)"
  1303     by (rule positive_integral_isoton)
  1304        (auto intro!: borel_measurable_pextreal_setsum assms positive_integral_mono
  1305                      arg_cong[where f=Sup]
  1306              simp: isoton_def le_fun_def psuminf_def fun_eq_iff SUPR_def Sup_fun_def)
  1307   thus ?thesis
  1308     by (auto simp: isoton_def psuminf_def positive_integral_setsum[OF assms])
  1309 qed
  1310 
  1311 text {* Fatou's lemma: convergence theorem on limes inferior *}
  1312 lemma (in measure_space) positive_integral_lim_INF:
  1313   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> pextreal"
  1314   assumes "\<And>i. u i \<in> borel_measurable M"
  1315   shows "(\<integral>\<^isup>+ x. SUP n. INF m. u (m + n) x) \<le>
  1316     (SUP n. INF m. positive_integral (u (m + n)))"
  1317 proof -
  1318   have "(\<integral>\<^isup>+x. SUP n. INF m. u (m + n) x)
  1319       = (SUP n. (\<integral>\<^isup>+x. INF m. u (m + n) x))"
  1320     using assms
  1321     by (intro positive_integral_monotone_convergence_SUP[symmetric] INF_mono)
  1322        (auto simp del: add_Suc simp add: add_Suc[symmetric])
  1323   also have "\<dots> \<le> (SUP n. INF m. positive_integral (u (m + n)))"
  1324     by (auto intro!: SUP_mono bexI le_INFI positive_integral_mono INF_leI)
  1325   finally show ?thesis .
  1326 qed
  1327 
  1328 lemma (in measure_space) measure_space_density:
  1329   assumes borel: "u \<in> borel_measurable M"
  1330   shows "measure_space M (\<lambda>A. (\<integral>\<^isup>+ x. u x * indicator A x))" (is "measure_space M ?v")
  1331 proof
  1332   show "?v {} = 0" by simp
  1333   show "countably_additive M ?v"
  1334     unfolding countably_additive_def
  1335   proof safe
  1336     fix A :: "nat \<Rightarrow> 'a set"
  1337     assume "range A \<subseteq> sets M"
  1338     hence "\<And>i. (\<lambda>x. u x * indicator (A i) x) \<in> borel_measurable M"
  1339       using borel by (auto intro: borel_measurable_indicator)
  1340     moreover assume "disjoint_family A"
  1341     note psuminf_indicator[OF this]
  1342     ultimately show "(\<Sum>\<^isub>\<infinity>n. ?v (A n)) = ?v (\<Union>x. A x)"
  1343       by (simp add: positive_integral_psuminf[symmetric])
  1344   qed
  1345 qed
  1346 
  1347 lemma (in measure_space) positive_integral_translated_density:
  1348   assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1349   shows "measure_space.positive_integral M (\<lambda>A. (\<integral>\<^isup>+ x. f x * indicator A x)) g = 
  1350          (\<integral>\<^isup>+ x. f x * g x)" (is "measure_space.positive_integral M ?T _ = _")
  1351 proof -
  1352   from measure_space_density[OF assms(1)]
  1353   interpret T: measure_space M ?T .
  1354   from borel_measurable_implies_simple_function_sequence[OF assms(2)]
  1355   obtain G where G: "\<And>i. simple_function (G i)" "G \<up> g" by blast
  1356   note G_borel = borel_measurable_simple_function[OF this(1)]
  1357   from T.positive_integral_isoton[OF `G \<up> g` G_borel]
  1358   have *: "(\<lambda>i. T.positive_integral (G i)) \<up> T.positive_integral g" .
  1359   { fix i
  1360     have [simp]: "finite (G i ` space M)"
  1361       using G(1) unfolding simple_function_def by auto
  1362     have "T.positive_integral (G i) = T.simple_integral (G i)"
  1363       using G T.positive_integral_eq_simple_integral by simp
  1364     also have "\<dots> = (\<integral>\<^isup>+x. f x * (\<Sum>y\<in>G i`space M. y * indicator (G i -` {y} \<inter> space M) x))"
  1365       apply (simp add: T.simple_integral_def)
  1366       apply (subst positive_integral_cmult[symmetric])
  1367       using G_borel assms(1) apply (fastsimp intro: borel_measurable_indicator borel_measurable_vimage)
  1368       apply (subst positive_integral_setsum[symmetric])
  1369       using G_borel assms(1) apply (fastsimp intro: borel_measurable_indicator borel_measurable_vimage)
  1370       by (simp add: setsum_right_distrib field_simps)
  1371     also have "\<dots> = (\<integral>\<^isup>+x. f x * G i x)"
  1372       by (auto intro!: positive_integral_cong
  1373                simp: indicator_def if_distrib setsum_cases)
  1374     finally have "T.positive_integral (G i) = (\<integral>\<^isup>+x. f x * G i x)" . }
  1375   with * have eq_Tg: "(\<lambda>i. (\<integral>\<^isup>+x. f x * G i x)) \<up> T.positive_integral g" by simp
  1376   from G(2) have "(\<lambda>i x. f x * G i x) \<up> (\<lambda>x. f x * g x)"
  1377     unfolding isoton_fun_expand by (auto intro!: isoton_cmult_right)
  1378   then have "(\<lambda>i. (\<integral>\<^isup>+x. f x * G i x)) \<up> (\<integral>\<^isup>+x. f x * g x)"
  1379     using assms(1) G_borel by (auto intro!: positive_integral_isoton borel_measurable_pextreal_times)
  1380   with eq_Tg show "T.positive_integral g = (\<integral>\<^isup>+x. f x * g x)"
  1381     unfolding isoton_def by simp
  1382 qed
  1383 
  1384 lemma (in measure_space) positive_integral_null_set:
  1385   assumes "N \<in> null_sets" shows "(\<integral>\<^isup>+ x. u x * indicator N x) = 0"
  1386 proof -
  1387   have "(\<integral>\<^isup>+ x. u x * indicator N x) = (\<integral>\<^isup>+ x. 0)"
  1388   proof (intro positive_integral_cong_AE AE_I)
  1389     show "{x \<in> space M. u x * indicator N x \<noteq> 0} \<subseteq> N"
  1390       by (auto simp: indicator_def)
  1391     show "\<mu> N = 0" "N \<in> sets M"
  1392       using assms by auto
  1393   qed
  1394   then show ?thesis by simp
  1395 qed
  1396 
  1397 lemma (in measure_space) positive_integral_Markov_inequality:
  1398   assumes borel: "u \<in> borel_measurable M" and "A \<in> sets M" and c: "c \<noteq> \<omega>"
  1399   shows "\<mu> ({x\<in>space M. 1 \<le> c * u x} \<inter> A) \<le> c * (\<integral>\<^isup>+ x. u x * indicator A x)"
  1400     (is "\<mu> ?A \<le> _ * ?PI")
  1401 proof -
  1402   have "?A \<in> sets M"
  1403     using `A \<in> sets M` borel by auto
  1404   hence "\<mu> ?A = (\<integral>\<^isup>+ x. indicator ?A x)"
  1405     using positive_integral_indicator by simp
  1406   also have "\<dots> \<le> (\<integral>\<^isup>+ x. c * (u x * indicator A x))"
  1407   proof (rule positive_integral_mono)
  1408     fix x assume "x \<in> space M"
  1409     show "indicator ?A x \<le> c * (u x * indicator A x)"
  1410       by (cases "x \<in> ?A") auto
  1411   qed
  1412   also have "\<dots> = c * (\<integral>\<^isup>+ x. u x * indicator A x)"
  1413     using assms
  1414     by (auto intro!: positive_integral_cmult borel_measurable_indicator)
  1415   finally show ?thesis .
  1416 qed
  1417 
  1418 lemma (in measure_space) positive_integral_0_iff:
  1419   assumes borel: "u \<in> borel_measurable M"
  1420   shows "positive_integral u = 0 \<longleftrightarrow> \<mu> {x\<in>space M. u x \<noteq> 0} = 0"
  1421     (is "_ \<longleftrightarrow> \<mu> ?A = 0")
  1422 proof -
  1423   have A: "?A \<in> sets M" using borel by auto
  1424   have u: "(\<integral>\<^isup>+ x. u x * indicator ?A x) = positive_integral u"
  1425     by (auto intro!: positive_integral_cong simp: indicator_def)
  1426 
  1427   show ?thesis
  1428   proof
  1429     assume "\<mu> ?A = 0"
  1430     hence "?A \<in> null_sets" using `?A \<in> sets M` by auto
  1431     from positive_integral_null_set[OF this]
  1432     have "0 = (\<integral>\<^isup>+ x. u x * indicator ?A x)" by simp
  1433     thus "positive_integral u = 0" unfolding u by simp
  1434   next
  1435     assume *: "positive_integral u = 0"
  1436     let "?M n" = "{x \<in> space M. 1 \<le> of_nat n * u x}"
  1437     have "0 = (SUP n. \<mu> (?M n \<inter> ?A))"
  1438     proof -
  1439       { fix n
  1440         from positive_integral_Markov_inequality[OF borel `?A \<in> sets M`, of "of_nat n"]
  1441         have "\<mu> (?M n \<inter> ?A) = 0" unfolding * u by simp }
  1442       thus ?thesis by simp
  1443     qed
  1444     also have "\<dots> = \<mu> (\<Union>n. ?M n \<inter> ?A)"
  1445     proof (safe intro!: continuity_from_below)
  1446       fix n show "?M n \<inter> ?A \<in> sets M"
  1447         using borel by (auto intro!: Int)
  1448     next
  1449       fix n x assume "1 \<le> of_nat n * u x"
  1450       also have "\<dots> \<le> of_nat (Suc n) * u x"
  1451         by (subst (1 2) mult_commute) (auto intro!: pextreal_mult_cancel)
  1452       finally show "1 \<le> of_nat (Suc n) * u x" .
  1453     qed
  1454     also have "\<dots> = \<mu> ?A"
  1455     proof (safe intro!: arg_cong[where f="\<mu>"])
  1456       fix x assume "u x \<noteq> 0" and [simp, intro]: "x \<in> space M"
  1457       show "x \<in> (\<Union>n. ?M n \<inter> ?A)"
  1458       proof (cases "u x")
  1459         case (preal r)
  1460         obtain j where "1 / r \<le> of_nat j" using ex_le_of_nat ..
  1461         hence "1 / r * r \<le> of_nat j * r" using preal unfolding mult_le_cancel_right by auto
  1462         hence "1 \<le> of_nat j * r" using preal `u x \<noteq> 0` by auto
  1463         thus ?thesis using `u x \<noteq> 0` preal by (auto simp: real_of_nat_def[symmetric])
  1464       qed auto
  1465     qed
  1466     finally show "\<mu> ?A = 0" by simp
  1467   qed
  1468 qed
  1469 
  1470 lemma (in measure_space) positive_integral_restricted:
  1471   assumes "A \<in> sets M"
  1472   shows "measure_space.positive_integral (restricted_space A) \<mu> f = (\<integral>\<^isup>+ x. f x * indicator A x)"
  1473     (is "measure_space.positive_integral ?R \<mu> f = positive_integral ?f")
  1474 proof -
  1475   have msR: "measure_space ?R \<mu>" by (rule restricted_measure_space[OF `A \<in> sets M`])
  1476   then interpret R: measure_space ?R \<mu> .
  1477   have saR: "sigma_algebra ?R" by fact
  1478   have *: "R.positive_integral f = R.positive_integral ?f"
  1479     by (intro R.positive_integral_cong) auto
  1480   show ?thesis
  1481     unfolding * R.positive_integral_def positive_integral_def
  1482     unfolding simple_function_restricted[OF `A \<in> sets M`]
  1483     apply (simp add: SUPR_def)
  1484     apply (rule arg_cong[where f=Sup])
  1485   proof (auto simp add: image_iff simple_integral_restricted[OF `A \<in> sets M`])
  1486     fix g assume "simple_function (\<lambda>x. g x * indicator A x)"
  1487       "g \<le> f"
  1488     then show "\<exists>x. simple_function x \<and> x \<le> (\<lambda>x. f x * indicator A x) \<and>
  1489       (\<integral>\<^isup>Sx. g x * indicator A x) = simple_integral x"
  1490       apply (rule_tac exI[of _ "\<lambda>x. g x * indicator A x"])
  1491       by (auto simp: indicator_def le_fun_def)
  1492   next
  1493     fix g assume g: "simple_function g" "g \<le> (\<lambda>x. f x * indicator A x)"
  1494     then have *: "(\<lambda>x. g x * indicator A x) = g"
  1495       "\<And>x. g x * indicator A x = g x"
  1496       "\<And>x. g x \<le> f x"
  1497       by (auto simp: le_fun_def fun_eq_iff indicator_def split: split_if_asm)
  1498     from g show "\<exists>x. simple_function (\<lambda>xa. x xa * indicator A xa) \<and> x \<le> f \<and>
  1499       simple_integral g = simple_integral (\<lambda>xa. x xa * indicator A xa)"
  1500       using `A \<in> sets M`[THEN sets_into_space]
  1501       apply (rule_tac exI[of _ "\<lambda>x. g x * indicator A x"])
  1502       by (fastsimp simp: le_fun_def *)
  1503   qed
  1504 qed
  1505 
  1506 lemma (in measure_space) positive_integral_subalgebra:
  1507   assumes borel: "f \<in> borel_measurable N"
  1508   and N: "sets N \<subseteq> sets M" "space N = space M" and sa: "sigma_algebra N"
  1509   shows "measure_space.positive_integral N \<mu> f = positive_integral f"
  1510 proof -
  1511   interpret N: measure_space N \<mu> using measure_space_subalgebra[OF sa N] .
  1512   from N.borel_measurable_implies_simple_function_sequence[OF borel]
  1513   obtain fs where Nsf: "\<And>i. N.simple_function (fs i)" and "fs \<up> f" by blast
  1514   then have sf: "\<And>i. simple_function (fs i)"
  1515     using simple_function_subalgebra[OF _ N sa] by blast
  1516   from positive_integral_isoton_simple[OF `fs \<up> f` sf] N.positive_integral_isoton_simple[OF `fs \<up> f` Nsf]
  1517   show ?thesis unfolding isoton_def simple_integral_def N.simple_integral_def `space N = space M` by simp
  1518 qed
  1519 
  1520 section "Lebesgue Integral"
  1521 
  1522 definition (in measure_space) integrable where
  1523   "integrable f \<longleftrightarrow> f \<in> borel_measurable M \<and>
  1524     (\<integral>\<^isup>+ x. Real (f x)) \<noteq> \<omega> \<and>
  1525     (\<integral>\<^isup>+ x. Real (- f x)) \<noteq> \<omega>"
  1526 
  1527 lemma (in measure_space) integrableD[dest]:
  1528   assumes "integrable f"
  1529   shows "f \<in> borel_measurable M"
  1530   "(\<integral>\<^isup>+ x. Real (f x)) \<noteq> \<omega>"
  1531   "(\<integral>\<^isup>+ x. Real (- f x)) \<noteq> \<omega>"
  1532   using assms unfolding integrable_def by auto
  1533 
  1534 definition (in measure_space) integral (binder "\<integral> " 10) where
  1535   "integral f = real ((\<integral>\<^isup>+ x. Real (f x))) - real ((\<integral>\<^isup>+ x. Real (- f x)))"
  1536 
  1537 lemma (in measure_space) integral_cong:
  1538   assumes cong: "\<And>x. x \<in> space M \<Longrightarrow> f x = g x"
  1539   shows "integral f = integral g"
  1540   using assms by (simp cong: positive_integral_cong add: integral_def)
  1541 
  1542 lemma (in measure_space) integral_cong_measure:
  1543   assumes "\<And>A. A \<in> sets M \<Longrightarrow> \<nu> A = \<mu> A"
  1544   shows "measure_space.integral M \<nu> f = integral f"
  1545 proof -
  1546   interpret v: measure_space M \<nu>
  1547     by (rule measure_space_cong) fact
  1548   show ?thesis
  1549     unfolding integral_def v.integral_def
  1550     by (simp add: positive_integral_cong_measure[OF assms])
  1551 qed
  1552 
  1553 lemma (in measure_space) integral_cong_AE:
  1554   assumes cong: "AE x. f x = g x"
  1555   shows "integral f = integral g"
  1556 proof -
  1557   have "AE x. Real (f x) = Real (g x)"
  1558     using cong by (rule AE_mp) simp
  1559   moreover have "AE x. Real (- f x) = Real (- g x)"
  1560     using cong by (rule AE_mp) simp
  1561   ultimately show ?thesis
  1562     by (simp cong: positive_integral_cong_AE add: integral_def)
  1563 qed
  1564 
  1565 lemma (in measure_space) integrable_cong:
  1566   "(\<And>x. x \<in> space M \<Longrightarrow> f x = g x) \<Longrightarrow> integrable f \<longleftrightarrow> integrable g"
  1567   by (simp cong: positive_integral_cong measurable_cong add: integrable_def)
  1568 
  1569 lemma (in measure_space) integral_eq_positive_integral:
  1570   assumes "\<And>x. 0 \<le> f x"
  1571   shows "integral f = real ((\<integral>\<^isup>+ x. Real (f x)))"
  1572 proof -
  1573   have "\<And>x. Real (- f x) = 0" using assms by simp
  1574   thus ?thesis by (simp del: Real_eq_0 add: integral_def)
  1575 qed
  1576 
  1577 lemma (in measure_space) integral_vimage:
  1578   assumes T: "sigma_algebra M'" "T \<in> measurable M M'"
  1579   assumes f: "measure_space.integrable M' (\<lambda>A. \<mu> (T -` A \<inter> space M)) f"
  1580   shows "integrable (\<lambda>x. f (T x))" (is ?P)
  1581     and "measure_space.integral M' (\<lambda>A. \<mu> (T -` A \<inter> space M)) f = (\<integral>x. f (T x))" (is ?I)
  1582 proof -
  1583   interpret T: measure_space M' "\<lambda>A. \<mu> (T -` A \<inter> space M)"
  1584     using T by (rule measure_space_vimage) auto
  1585   from measurable_comp[OF T(2), of f borel]
  1586   have borel: "(\<lambda>x. Real (f x)) \<in> borel_measurable M'" "(\<lambda>x. Real (- f x)) \<in> borel_measurable M'"
  1587     and "(\<lambda>x. f (T x)) \<in> borel_measurable M"
  1588     using f unfolding T.integrable_def by (auto simp: comp_def)
  1589   then show ?P ?I
  1590     using f unfolding T.integral_def integral_def T.integrable_def integrable_def
  1591     unfolding borel[THEN positive_integral_vimage[OF T]] by auto
  1592 qed
  1593 
  1594 lemma (in measure_space) integral_minus[intro, simp]:
  1595   assumes "integrable f"
  1596   shows "integrable (\<lambda>x. - f x)" "(\<integral>x. - f x) = - integral f"
  1597   using assms by (auto simp: integrable_def integral_def)
  1598 
  1599 lemma (in measure_space) integral_of_positive_diff:
  1600   assumes integrable: "integrable u" "integrable v"
  1601   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"
  1602   shows "integrable f" and "integral f = integral u - integral v"
  1603 proof -
  1604   let ?PI = positive_integral
  1605   let "?f x" = "Real (f x)"
  1606   let "?mf x" = "Real (- f x)"
  1607   let "?u x" = "Real (u x)"
  1608   let "?v x" = "Real (v x)"
  1609 
  1610   from borel_measurable_diff[of u v] integrable
  1611   have f_borel: "?f \<in> borel_measurable M" and
  1612     mf_borel: "?mf \<in> borel_measurable M" and
  1613     v_borel: "?v \<in> borel_measurable M" and
  1614     u_borel: "?u \<in> borel_measurable M" and
  1615     "f \<in> borel_measurable M"
  1616     by (auto simp: f_def[symmetric] integrable_def)
  1617 
  1618   have "?PI (\<lambda>x. Real (u x - v x)) \<le> ?PI ?u"
  1619     using pos by (auto intro!: positive_integral_mono)
  1620   moreover have "?PI (\<lambda>x. Real (v x - u x)) \<le> ?PI ?v"
  1621     using pos by (auto intro!: positive_integral_mono)
  1622   ultimately show f: "integrable f"
  1623     using `integrable u` `integrable v` `f \<in> borel_measurable M`
  1624     by (auto simp: integrable_def f_def)
  1625   hence mf: "integrable (\<lambda>x. - f x)" ..
  1626 
  1627   have *: "\<And>x. Real (- v x) = 0" "\<And>x. Real (- u x) = 0"
  1628     using pos by auto
  1629 
  1630   have "\<And>x. ?u x + ?mf x = ?v x + ?f x"
  1631     unfolding f_def using pos by simp
  1632   hence "?PI (\<lambda>x. ?u x + ?mf x) = ?PI (\<lambda>x. ?v x + ?f x)" by simp
  1633   hence "real (?PI ?u + ?PI ?mf) = real (?PI ?v + ?PI ?f)"
  1634     using positive_integral_add[OF u_borel mf_borel]
  1635     using positive_integral_add[OF v_borel f_borel]
  1636     by auto
  1637   then show "integral f = integral u - integral v"
  1638     using f mf `integrable u` `integrable v`
  1639     unfolding integral_def integrable_def *
  1640     by (cases "?PI ?f", cases "?PI ?mf", cases "?PI ?v", cases "?PI ?u")
  1641        (auto simp add: field_simps)
  1642 qed
  1643 
  1644 lemma (in measure_space) integral_linear:
  1645   assumes "integrable f" "integrable g" and "0 \<le> a"
  1646   shows "integrable (\<lambda>t. a * f t + g t)"
  1647   and "(\<integral> t. a * f t + g t) = a * integral f + integral g"
  1648 proof -
  1649   let ?PI = positive_integral
  1650   let "?f x" = "Real (f x)"
  1651   let "?g x" = "Real (g x)"
  1652   let "?mf x" = "Real (- f x)"
  1653   let "?mg x" = "Real (- g x)"
  1654   let "?p t" = "max 0 (a * f t) + max 0 (g t)"
  1655   let "?n t" = "max 0 (- (a * f t)) + max 0 (- g t)"
  1656 
  1657   have pos: "?f \<in> borel_measurable M" "?g \<in> borel_measurable M"
  1658     and neg: "?mf \<in> borel_measurable M" "?mg \<in> borel_measurable M"
  1659     and p: "?p \<in> borel_measurable M"
  1660     and n: "?n \<in> borel_measurable M"
  1661     using assms by (simp_all add: integrable_def)
  1662 
  1663   have *: "\<And>x. Real (?p x) = Real a * ?f x + ?g x"
  1664           "\<And>x. Real (?n x) = Real a * ?mf x + ?mg x"
  1665           "\<And>x. Real (- ?p x) = 0"
  1666           "\<And>x. Real (- ?n x) = 0"
  1667     using `0 \<le> a` by (auto simp: max_def min_def zero_le_mult_iff mult_le_0_iff add_nonpos_nonpos)
  1668 
  1669   note linear =
  1670     positive_integral_linear[OF pos]
  1671     positive_integral_linear[OF neg]
  1672 
  1673   have "integrable ?p" "integrable ?n"
  1674       "\<And>t. a * f t + g t = ?p t - ?n t" "\<And>t. 0 \<le> ?p t" "\<And>t. 0 \<le> ?n t"
  1675     using assms p n unfolding integrable_def * linear by auto
  1676   note diff = integral_of_positive_diff[OF this]
  1677 
  1678   show "integrable (\<lambda>t. a * f t + g t)" by (rule diff)
  1679 
  1680   from assms show "(\<integral> t. a * f t + g t) = a * integral f + integral g"
  1681     unfolding diff(2) unfolding integral_def * linear integrable_def
  1682     by (cases "?PI ?f", cases "?PI ?mf", cases "?PI ?g", cases "?PI ?mg")
  1683        (auto simp add: field_simps zero_le_mult_iff)
  1684 qed
  1685 
  1686 lemma (in measure_space) integral_add[simp, intro]:
  1687   assumes "integrable f" "integrable g"
  1688   shows "integrable (\<lambda>t. f t + g t)"
  1689   and "(\<integral> t. f t + g t) = integral f + integral g"
  1690   using assms integral_linear[where a=1] by auto
  1691 
  1692 lemma (in measure_space) integral_zero[simp, intro]:
  1693   shows "integrable (\<lambda>x. 0)"
  1694   and "(\<integral> x.0) = 0"
  1695   unfolding integrable_def integral_def
  1696   by (auto simp add: borel_measurable_const)
  1697 
  1698 lemma (in measure_space) integral_cmult[simp, intro]:
  1699   assumes "integrable f"
  1700   shows "integrable (\<lambda>t. a * f t)" (is ?P)
  1701   and "(\<integral> t. a * f t) = a * integral f" (is ?I)
  1702 proof -
  1703   have "integrable (\<lambda>t. a * f t) \<and> (\<integral> t. a * f t) = a * integral f"
  1704   proof (cases rule: le_cases)
  1705     assume "0 \<le> a" show ?thesis
  1706       using integral_linear[OF assms integral_zero(1) `0 \<le> a`]
  1707       by (simp add: integral_zero)
  1708   next
  1709     assume "a \<le> 0" hence "0 \<le> - a" by auto
  1710     have *: "\<And>t. - a * t + 0 = (-a) * t" by simp
  1711     show ?thesis using integral_linear[OF assms integral_zero(1) `0 \<le> - a`]
  1712         integral_minus(1)[of "\<lambda>t. - a * f t"]
  1713       unfolding * integral_zero by simp
  1714   qed
  1715   thus ?P ?I by auto
  1716 qed
  1717 
  1718 lemma (in measure_space) integral_multc:
  1719   assumes "integrable f"
  1720   shows "(\<integral> x. f x * c) = integral f * c"
  1721   unfolding mult_commute[of _ c] integral_cmult[OF assms] ..
  1722 
  1723 lemma (in measure_space) integral_mono_AE:
  1724   assumes fg: "integrable f" "integrable g"
  1725   and mono: "AE t. f t \<le> g t"
  1726   shows "integral f \<le> integral g"
  1727 proof -
  1728   have "AE x. Real (f x) \<le> Real (g x)"
  1729     using mono by (rule AE_mp) (auto intro!: AE_cong)
  1730   moreover have "AE x. Real (- g x) \<le> Real (- f x)" 
  1731     using mono by (rule AE_mp) (auto intro!: AE_cong)
  1732   ultimately show ?thesis using fg
  1733     by (auto simp: integral_def integrable_def diff_minus
  1734              intro!: add_mono real_of_pextreal_mono positive_integral_mono_AE)
  1735 qed
  1736 
  1737 lemma (in measure_space) integral_mono:
  1738   assumes fg: "integrable f" "integrable g"
  1739   and mono: "\<And>t. t \<in> space M \<Longrightarrow> f t \<le> g t"
  1740   shows "integral f \<le> integral g"
  1741   apply (rule integral_mono_AE[OF fg])
  1742   using mono by (rule AE_cong) auto
  1743 
  1744 lemma (in measure_space) integral_diff[simp, intro]:
  1745   assumes f: "integrable f" and g: "integrable g"
  1746   shows "integrable (\<lambda>t. f t - g t)"
  1747   and "(\<integral> t. f t - g t) = integral f - integral g"
  1748   using integral_add[OF f integral_minus(1)[OF g]]
  1749   unfolding diff_minus integral_minus(2)[OF g]
  1750   by auto
  1751 
  1752 lemma (in measure_space) integral_indicator[simp, intro]:
  1753   assumes "a \<in> sets M" and "\<mu> a \<noteq> \<omega>"
  1754   shows "integral (indicator a) = real (\<mu> a)" (is ?int)
  1755   and "integrable (indicator a)" (is ?able)
  1756 proof -
  1757   have *:
  1758     "\<And>A x. Real (indicator A x) = indicator A x"
  1759     "\<And>A x. Real (- indicator A x) = 0" unfolding indicator_def by auto
  1760   show ?int ?able
  1761     using assms unfolding integral_def integrable_def
  1762     by (auto simp: * positive_integral_indicator borel_measurable_indicator)
  1763 qed
  1764 
  1765 lemma (in measure_space) integral_cmul_indicator:
  1766   assumes "A \<in> sets M" and "c \<noteq> 0 \<Longrightarrow> \<mu> A \<noteq> \<omega>"
  1767   shows "integrable (\<lambda>x. c * indicator A x)" (is ?P)
  1768   and "(\<integral>x. c * indicator A x) = c * real (\<mu> A)" (is ?I)
  1769 proof -
  1770   show ?P
  1771   proof (cases "c = 0")
  1772     case False with assms show ?thesis by simp
  1773   qed simp
  1774 
  1775   show ?I
  1776   proof (cases "c = 0")
  1777     case False with assms show ?thesis by simp
  1778   qed simp
  1779 qed
  1780 
  1781 lemma (in measure_space) integral_setsum[simp, intro]:
  1782   assumes "\<And>n. n \<in> S \<Longrightarrow> integrable (f n)"
  1783   shows "(\<integral>x. \<Sum> i \<in> S. f i x) = (\<Sum> i \<in> S. integral (f i))" (is "?int S")
  1784     and "integrable (\<lambda>x. \<Sum> i \<in> S. f i x)" (is "?I S")
  1785 proof -
  1786   have "?int S \<and> ?I S"
  1787   proof (cases "finite S")
  1788     assume "finite S"
  1789     from this assms show ?thesis by (induct S) simp_all
  1790   qed simp
  1791   thus "?int S" and "?I S" by auto
  1792 qed
  1793 
  1794 lemma (in measure_space) integrable_abs:
  1795   assumes "integrable f"
  1796   shows "integrable (\<lambda> x. \<bar>f x\<bar>)"
  1797 proof -
  1798   have *:
  1799     "\<And>x. Real \<bar>f x\<bar> = Real (f x) + Real (- f x)"
  1800     "\<And>x. Real (- \<bar>f x\<bar>) = 0" by auto
  1801   have abs: "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M" and
  1802     f: "(\<lambda>x. Real (f x)) \<in> borel_measurable M"
  1803         "(\<lambda>x. Real (- f x)) \<in> borel_measurable M"
  1804     using assms unfolding integrable_def by auto
  1805   from abs assms show ?thesis unfolding integrable_def *
  1806     using positive_integral_linear[OF f, of 1] by simp
  1807 qed
  1808 
  1809 lemma (in measure_space) integral_subalgebra:
  1810   assumes borel: "f \<in> borel_measurable N"
  1811   and N: "sets N \<subseteq> sets M" "space N = space M" and sa: "sigma_algebra N"
  1812   shows "measure_space.integrable N \<mu> f \<longleftrightarrow> integrable f" (is ?P)
  1813     and "measure_space.integral N \<mu> f = integral f" (is ?I)
  1814 proof -
  1815   interpret N: measure_space N \<mu> using measure_space_subalgebra[OF sa N] .
  1816   have "(\<lambda>x. Real (f x)) \<in> borel_measurable N" "(\<lambda>x. Real (- f x)) \<in> borel_measurable N"
  1817     using borel by auto
  1818   note * = this[THEN positive_integral_subalgebra[OF _ N sa]]
  1819   have "f \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable N"
  1820     using assms unfolding measurable_def by auto
  1821   then show ?P ?I unfolding integrable_def N.integrable_def integral_def N.integral_def
  1822     unfolding * by auto
  1823 qed
  1824 
  1825 lemma (in measure_space) integrable_bound:
  1826   assumes "integrable f"
  1827   and f: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
  1828     "\<And>x. x \<in> space M \<Longrightarrow> \<bar>g x\<bar> \<le> f x"
  1829   assumes borel: "g \<in> borel_measurable M"
  1830   shows "integrable g"
  1831 proof -
  1832   have "(\<integral>\<^isup>+ x. Real (g x)) \<le> (\<integral>\<^isup>+ x. Real \<bar>g x\<bar>)"
  1833     by (auto intro!: positive_integral_mono)
  1834   also have "\<dots> \<le> (\<integral>\<^isup>+ x. Real (f x))"
  1835     using f by (auto intro!: positive_integral_mono)
  1836   also have "\<dots> < \<omega>"
  1837     using `integrable f` unfolding integrable_def by (auto simp: pextreal_less_\<omega>)
  1838   finally have pos: "(\<integral>\<^isup>+ x. Real (g x)) < \<omega>" .
  1839 
  1840   have "(\<integral>\<^isup>+ x. Real (- g x)) \<le> (\<integral>\<^isup>+ x. Real (\<bar>g x\<bar>))"
  1841     by (auto intro!: positive_integral_mono)
  1842   also have "\<dots> \<le> (\<integral>\<^isup>+ x. Real (f x))"
  1843     using f by (auto intro!: positive_integral_mono)
  1844   also have "\<dots> < \<omega>"
  1845     using `integrable f` unfolding integrable_def by (auto simp: pextreal_less_\<omega>)
  1846   finally have neg: "(\<integral>\<^isup>+ x. Real (- g x)) < \<omega>" .
  1847 
  1848   from neg pos borel show ?thesis
  1849     unfolding integrable_def by auto
  1850 qed
  1851 
  1852 lemma (in measure_space) integrable_abs_iff:
  1853   "f \<in> borel_measurable M \<Longrightarrow> integrable (\<lambda> x. \<bar>f x\<bar>) \<longleftrightarrow> integrable f"
  1854   by (auto intro!: integrable_bound[where g=f] integrable_abs)
  1855 
  1856 lemma (in measure_space) integrable_max:
  1857   assumes int: "integrable f" "integrable g"
  1858   shows "integrable (\<lambda> x. max (f x) (g x))"
  1859 proof (rule integrable_bound)
  1860   show "integrable (\<lambda>x. \<bar>f x\<bar> + \<bar>g x\<bar>)"
  1861     using int by (simp add: integrable_abs)
  1862   show "(\<lambda>x. max (f x) (g x)) \<in> borel_measurable M"
  1863     using int unfolding integrable_def by auto
  1864 next
  1865   fix x assume "x \<in> space M"
  1866   show "0 \<le> \<bar>f x\<bar> + \<bar>g x\<bar>" "\<bar>max (f x) (g x)\<bar> \<le> \<bar>f x\<bar> + \<bar>g x\<bar>"
  1867     by auto
  1868 qed
  1869 
  1870 lemma (in measure_space) integrable_min:
  1871   assumes int: "integrable f" "integrable g"
  1872   shows "integrable (\<lambda> x. min (f x) (g x))"
  1873 proof (rule integrable_bound)
  1874   show "integrable (\<lambda>x. \<bar>f x\<bar> + \<bar>g x\<bar>)"
  1875     using int by (simp add: integrable_abs)
  1876   show "(\<lambda>x. min (f x) (g x)) \<in> borel_measurable M"
  1877     using int unfolding integrable_def by auto
  1878 next
  1879   fix x assume "x \<in> space M"
  1880   show "0 \<le> \<bar>f x\<bar> + \<bar>g x\<bar>" "\<bar>min (f x) (g x)\<bar> \<le> \<bar>f x\<bar> + \<bar>g x\<bar>"
  1881     by auto
  1882 qed
  1883 
  1884 lemma (in measure_space) integral_triangle_inequality:
  1885   assumes "integrable f"
  1886   shows "\<bar>integral f\<bar> \<le> (\<integral>x. \<bar>f x\<bar>)"
  1887 proof -
  1888   have "\<bar>integral f\<bar> = max (integral f) (- integral f)" by auto
  1889   also have "\<dots> \<le> (\<integral>x. \<bar>f x\<bar>)"
  1890       using assms integral_minus(2)[of f, symmetric]
  1891       by (auto intro!: integral_mono integrable_abs simp del: integral_minus)
  1892   finally show ?thesis .
  1893 qed
  1894 
  1895 lemma (in measure_space) integral_positive:
  1896   assumes "integrable f" "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
  1897   shows "0 \<le> integral f"
  1898 proof -
  1899   have "0 = (\<integral>x. 0)" by (auto simp: integral_zero)
  1900   also have "\<dots> \<le> integral f"
  1901     using assms by (rule integral_mono[OF integral_zero(1)])
  1902   finally show ?thesis .
  1903 qed
  1904 
  1905 lemma (in measure_space) integral_monotone_convergence_pos:
  1906   assumes i: "\<And>i. integrable (f i)" and mono: "\<And>x. mono (\<lambda>n. f n x)"
  1907   and pos: "\<And>x i. 0 \<le> f i x"
  1908   and lim: "\<And>x. (\<lambda>i. f i x) ----> u x"
  1909   and ilim: "(\<lambda>i. integral (f i)) ----> x"
  1910   shows "integrable u"
  1911   and "integral u = x"
  1912 proof -
  1913   { fix x have "0 \<le> u x"
  1914       using mono pos[of 0 x] incseq_le[OF _ lim, of x 0]
  1915       by (simp add: mono_def incseq_def) }
  1916   note pos_u = this
  1917 
  1918   hence [simp]: "\<And>i x. Real (- f i x) = 0" "\<And>x. Real (- u x) = 0"
  1919     using pos by auto
  1920 
  1921   have SUP_F: "\<And>x. (SUP n. Real (f n x)) = Real (u x)"
  1922     using mono pos pos_u lim by (rule SUP_eq_LIMSEQ[THEN iffD2])
  1923 
  1924   have borel_f: "\<And>i. (\<lambda>x. Real (f i x)) \<in> borel_measurable M"
  1925     using i unfolding integrable_def by auto
  1926   hence "(\<lambda>x. SUP i. Real (f i x)) \<in> borel_measurable M"
  1927     by auto
  1928   hence borel_u: "u \<in> borel_measurable M"
  1929     using pos_u by (auto simp: borel_measurable_Real_eq SUP_F)
  1930 
  1931   have integral_eq: "\<And>n. (\<integral>\<^isup>+ x. Real (f n x)) = Real (integral (f n))"
  1932     using i unfolding integral_def integrable_def by (auto simp: Real_real)
  1933 
  1934   have pos_integral: "\<And>n. 0 \<le> integral (f n)"
  1935     using pos i by (auto simp: integral_positive)
  1936   hence "0 \<le> x"
  1937     using LIMSEQ_le_const[OF ilim, of 0] by auto
  1938 
  1939   have "(\<lambda>i. (\<integral>\<^isup>+ x. Real (f i x))) \<up> (\<integral>\<^isup>+ x. Real (u x))"
  1940   proof (rule positive_integral_isoton)
  1941     from SUP_F mono pos
  1942     show "(\<lambda>i x. Real (f i x)) \<up> (\<lambda>x. Real (u x))"
  1943       unfolding isoton_fun_expand by (auto simp: isoton_def mono_def)
  1944   qed (rule borel_f)
  1945   hence pI: "(\<integral>\<^isup>+ x. Real (u x)) =
  1946       (SUP n. (\<integral>\<^isup>+ x. Real (f n x)))"
  1947     unfolding isoton_def by simp
  1948   also have "\<dots> = Real x" unfolding integral_eq
  1949   proof (rule SUP_eq_LIMSEQ[THEN iffD2])
  1950     show "mono (\<lambda>n. integral (f n))"
  1951       using mono i by (auto simp: mono_def intro!: integral_mono)
  1952     show "\<And>n. 0 \<le> integral (f n)" using pos_integral .
  1953     show "0 \<le> x" using `0 \<le> x` .
  1954     show "(\<lambda>n. integral (f n)) ----> x" using ilim .
  1955   qed
  1956   finally show  "integrable u" "integral u = x" using borel_u `0 \<le> x`
  1957     unfolding integrable_def integral_def by auto
  1958 qed
  1959 
  1960 lemma (in measure_space) integral_monotone_convergence:
  1961   assumes f: "\<And>i. integrable (f i)" and "mono f"
  1962   and lim: "\<And>x. (\<lambda>i. f i x) ----> u x"
  1963   and ilim: "(\<lambda>i. integral (f i)) ----> x"
  1964   shows "integrable u"
  1965   and "integral u = x"
  1966 proof -
  1967   have 1: "\<And>i. integrable (\<lambda>x. f i x - f 0 x)"
  1968       using f by (auto intro!: integral_diff)
  1969   have 2: "\<And>x. mono (\<lambda>n. f n x - f 0 x)" using `mono f`
  1970       unfolding mono_def le_fun_def by auto
  1971   have 3: "\<And>x n. 0 \<le> f n x - f 0 x" using `mono f`
  1972       unfolding mono_def le_fun_def by (auto simp: field_simps)
  1973   have 4: "\<And>x. (\<lambda>i. f i x - f 0 x) ----> u x - f 0 x"
  1974     using lim by (auto intro!: LIMSEQ_diff)
  1975   have 5: "(\<lambda>i. (\<integral>x. f i x - f 0 x)) ----> x - integral (f 0)"
  1976     using f ilim by (auto intro!: LIMSEQ_diff simp: integral_diff)
  1977   note diff = integral_monotone_convergence_pos[OF 1 2 3 4 5]
  1978   have "integrable (\<lambda>x. (u x - f 0 x) + f 0 x)"
  1979     using diff(1) f by (rule integral_add(1))
  1980   with diff(2) f show "integrable u" "integral u = x"
  1981     by (auto simp: integral_diff)
  1982 qed
  1983 
  1984 lemma (in measure_space) integral_0_iff:
  1985   assumes "integrable f"
  1986   shows "(\<integral>x. \<bar>f x\<bar>) = 0 \<longleftrightarrow> \<mu> {x\<in>space M. f x \<noteq> 0} = 0"
  1987 proof -
  1988   have *: "\<And>x. Real (- \<bar>f x\<bar>) = 0" by auto
  1989   have "integrable (\<lambda>x. \<bar>f x\<bar>)" using assms by (rule integrable_abs)
  1990   hence "(\<lambda>x. Real (\<bar>f x\<bar>)) \<in> borel_measurable M"
  1991     "(\<integral>\<^isup>+ x. Real \<bar>f x\<bar>) \<noteq> \<omega>" unfolding integrable_def by auto
  1992   from positive_integral_0_iff[OF this(1)] this(2)
  1993   show ?thesis unfolding integral_def *
  1994     by (simp add: real_of_pextreal_eq_0)
  1995 qed
  1996 
  1997 lemma (in measure_space) positive_integral_omega:
  1998   assumes "f \<in> borel_measurable M"
  1999   and "positive_integral f \<noteq> \<omega>"
  2000   shows "\<mu> (f -` {\<omega>} \<inter> space M) = 0"
  2001 proof -
  2002   have "\<omega> * \<mu> (f -` {\<omega>} \<inter> space M) = (\<integral>\<^isup>+ x. \<omega> * indicator (f -` {\<omega>} \<inter> space M) x)"
  2003     using positive_integral_cmult_indicator[OF borel_measurable_vimage, OF assms(1), of \<omega> \<omega>] by simp
  2004   also have "\<dots> \<le> positive_integral f"
  2005     by (auto intro!: positive_integral_mono simp: indicator_def)
  2006   finally show ?thesis
  2007     using assms(2) by (cases ?thesis) auto
  2008 qed
  2009 
  2010 lemma (in measure_space) positive_integral_omega_AE:
  2011   assumes "f \<in> borel_measurable M" "positive_integral f \<noteq> \<omega>" shows "AE x. f x \<noteq> \<omega>"
  2012 proof (rule AE_I)
  2013   show "\<mu> (f -` {\<omega>} \<inter> space M) = 0"
  2014     by (rule positive_integral_omega[OF assms])
  2015   show "f -` {\<omega>} \<inter> space M \<in> sets M"
  2016     using assms by (auto intro: borel_measurable_vimage)
  2017 qed auto
  2018 
  2019 lemma (in measure_space) simple_integral_omega:
  2020   assumes "simple_function f"
  2021   and "simple_integral f \<noteq> \<omega>"
  2022   shows "\<mu> (f -` {\<omega>} \<inter> space M) = 0"
  2023 proof (rule positive_integral_omega)
  2024   show "f \<in> borel_measurable M" using assms by (auto intro: borel_measurable_simple_function)
  2025   show "positive_integral f \<noteq> \<omega>"
  2026     using assms by (simp add: positive_integral_eq_simple_integral)
  2027 qed
  2028 
  2029 lemma (in measure_space) integral_real:
  2030   fixes f :: "'a \<Rightarrow> pextreal"
  2031   assumes "AE x. f x \<noteq> \<omega>"
  2032   shows "(\<integral>x. real (f x)) = real (positive_integral f)" (is ?plus)
  2033     and "(\<integral>x. - real (f x)) = - real (positive_integral f)" (is ?minus)
  2034 proof -
  2035   have "(\<integral>\<^isup>+ x. Real (real (f x))) = positive_integral f"
  2036     apply (rule positive_integral_cong_AE)
  2037     apply (rule AE_mp[OF assms(1)])
  2038     by (auto intro!: AE_cong simp: Real_real)
  2039   moreover
  2040   have "(\<integral>\<^isup>+ x. Real (- real (f x))) = (\<integral>\<^isup>+ x. 0)"
  2041     by (intro positive_integral_cong) auto
  2042   ultimately show ?plus ?minus
  2043     by (auto simp: integral_def integrable_def)
  2044 qed
  2045 
  2046 lemma (in measure_space) integral_dominated_convergence:
  2047   assumes u: "\<And>i. integrable (u i)" and bound: "\<And>x j. x\<in>space M \<Longrightarrow> \<bar>u j x\<bar> \<le> w x"
  2048   and w: "integrable w" "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> w x"
  2049   and u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  2050   shows "integrable u'"
  2051   and "(\<lambda>i. (\<integral>x. \<bar>u i x - u' x\<bar>)) ----> 0" (is "?lim_diff")
  2052   and "(\<lambda>i. integral (u i)) ----> integral u'" (is ?lim)
  2053 proof -
  2054   { fix x j assume x: "x \<in> space M"
  2055     from u'[OF x] have "(\<lambda>i. \<bar>u i x\<bar>) ----> \<bar>u' x\<bar>" by (rule LIMSEQ_imp_rabs)
  2056     from LIMSEQ_le_const2[OF this]
  2057     have "\<bar>u' x\<bar> \<le> w x" using bound[OF x] by auto }
  2058   note u'_bound = this
  2059 
  2060   from u[unfolded integrable_def]
  2061   have u'_borel: "u' \<in> borel_measurable M"
  2062     using u' by (blast intro: borel_measurable_LIMSEQ[of u])
  2063 
  2064   show "integrable u'"
  2065   proof (rule integrable_bound)
  2066     show "integrable w" by fact
  2067     show "u' \<in> borel_measurable M" by fact
  2068   next
  2069     fix x assume x: "x \<in> space M"
  2070     thus "0 \<le> w x" by fact
  2071     show "\<bar>u' x\<bar> \<le> w x" using u'_bound[OF x] .
  2072   qed
  2073 
  2074   let "?diff n x" = "2 * w x - \<bar>u n x - u' x\<bar>"
  2075   have diff: "\<And>n. integrable (\<lambda>x. \<bar>u n x - u' x\<bar>)"
  2076     using w u `integrable u'`
  2077     by (auto intro!: integral_add integral_diff integral_cmult integrable_abs)
  2078 
  2079   { fix j x assume x: "x \<in> space M"
  2080     have "\<bar>u j x - u' x\<bar> \<le> \<bar>u j x\<bar> + \<bar>u' x\<bar>" by auto
  2081     also have "\<dots> \<le> w x + w x"
  2082       by (rule add_mono[OF bound[OF x] u'_bound[OF x]])
  2083     finally have "\<bar>u j x - u' x\<bar> \<le> 2 * w x" by simp }
  2084   note diff_less_2w = this
  2085 
  2086   have PI_diff: "\<And>m n. (\<integral>\<^isup>+ x. Real (?diff (m + n) x)) =
  2087     (\<integral>\<^isup>+ x. Real (2 * w x)) - (\<integral>\<^isup>+ x. Real \<bar>u (m + n) x - u' x\<bar>)"
  2088     using diff w diff_less_2w
  2089     by (subst positive_integral_diff[symmetric])
  2090        (auto simp: integrable_def intro!: positive_integral_cong)
  2091 
  2092   have "integrable (\<lambda>x. 2 * w x)"
  2093     using w by (auto intro: integral_cmult)
  2094   hence I2w_fin: "(\<integral>\<^isup>+ x. Real (2 * w x)) \<noteq> \<omega>" and
  2095     borel_2w: "(\<lambda>x. Real (2 * w x)) \<in> borel_measurable M"
  2096     unfolding integrable_def by auto
  2097 
  2098   have "(INF n. SUP m. (\<integral>\<^isup>+ x. Real \<bar>u (m + n) x - u' x\<bar>)) = 0" (is "?lim_SUP = 0")
  2099   proof cases
  2100     assume eq_0: "(\<integral>\<^isup>+ x. Real (2 * w x)) = 0"
  2101     have "\<And>i. (\<integral>\<^isup>+ x. Real \<bar>u i x - u' x\<bar>) \<le> (\<integral>\<^isup>+ x. Real (2 * w x))"
  2102     proof (rule positive_integral_mono)
  2103       fix i x assume "x \<in> space M" from diff_less_2w[OF this, of i]
  2104       show "Real \<bar>u i x - u' x\<bar> \<le> Real (2 * w x)" by auto
  2105     qed
  2106     hence "\<And>i. (\<integral>\<^isup>+ x. Real \<bar>u i x - u' x\<bar>) = 0" using eq_0 by auto
  2107     thus ?thesis by simp
  2108   next
  2109     assume neq_0: "(\<integral>\<^isup>+ x. Real (2 * w x)) \<noteq> 0"
  2110     have "(\<integral>\<^isup>+ x. Real (2 * w x)) = (\<integral>\<^isup>+ x. SUP n. INF m. Real (?diff (m + n) x))"
  2111     proof (rule positive_integral_cong, subst add_commute)
  2112       fix x assume x: "x \<in> space M"
  2113       show "Real (2 * w x) = (SUP n. INF m. Real (?diff (n + m) x))"
  2114       proof (rule LIMSEQ_imp_lim_INF[symmetric])
  2115         fix j show "0 \<le> ?diff j x" using diff_less_2w[OF x, of j] by simp
  2116       next
  2117         have "(\<lambda>i. ?diff i x) ----> 2 * w x - \<bar>u' x - u' x\<bar>"
  2118           using u'[OF x] by (safe intro!: LIMSEQ_diff LIMSEQ_const LIMSEQ_imp_rabs)
  2119         thus "(\<lambda>i. ?diff i x) ----> 2 * w x" by simp
  2120       qed
  2121     qed
  2122     also have "\<dots> \<le> (SUP n. INF m. (\<integral>\<^isup>+ x. Real (?diff (m + n) x)))"
  2123       using u'_borel w u unfolding integrable_def
  2124       by (auto intro!: positive_integral_lim_INF)
  2125     also have "\<dots> = (\<integral>\<^isup>+ x. Real (2 * w x)) -
  2126         (INF n. SUP m. (\<integral>\<^isup>+ x. Real \<bar>u (m + n) x - u' x\<bar>))"
  2127       unfolding PI_diff pextreal_INF_minus[OF I2w_fin] pextreal_SUP_minus ..
  2128     finally show ?thesis using neq_0 I2w_fin by (rule pextreal_le_minus_imp_0)
  2129   qed
  2130 
  2131   have [simp]: "\<And>n m x. Real (- \<bar>u (m + n) x - u' x\<bar>) = 0" by auto
  2132 
  2133   have [simp]: "\<And>n m. (\<integral>\<^isup>+ x. Real \<bar>u (m + n) x - u' x\<bar>) =
  2134     Real ((\<integral>x. \<bar>u (n + m) x - u' x\<bar>))"
  2135     using diff by (subst add_commute) (simp add: integral_def integrable_def Real_real)
  2136 
  2137   have "(SUP n. INF m. (\<integral>\<^isup>+ x. Real \<bar>u (m + n) x - u' x\<bar>)) \<le> ?lim_SUP"
  2138     (is "?lim_INF \<le> _") by (subst (1 2) add_commute) (rule lim_INF_le_lim_SUP)
  2139   hence "?lim_INF = Real 0" "?lim_SUP = Real 0" using `?lim_SUP = 0` by auto
  2140   thus ?lim_diff using diff by (auto intro!: integral_positive lim_INF_eq_lim_SUP)
  2141 
  2142   show ?lim
  2143   proof (rule LIMSEQ_I)
  2144     fix r :: real assume "0 < r"
  2145     from LIMSEQ_D[OF `?lim_diff` this]
  2146     obtain N where N: "\<And>n. N \<le> n \<Longrightarrow> (\<integral>x. \<bar>u n x - u' x\<bar>) < r"
  2147       using diff by (auto simp: integral_positive)
  2148 
  2149     show "\<exists>N. \<forall>n\<ge>N. norm (integral (u n) - integral u') < r"
  2150     proof (safe intro!: exI[of _ N])
  2151       fix n assume "N \<le> n"
  2152       have "\<bar>integral (u n) - integral u'\<bar> = \<bar>(\<integral>x. u n x - u' x)\<bar>"
  2153         using u `integrable u'` by (auto simp: integral_diff)
  2154       also have "\<dots> \<le> (\<integral>x. \<bar>u n x - u' x\<bar>)" using u `integrable u'`
  2155         by (rule_tac integral_triangle_inequality) (auto intro!: integral_diff)
  2156       also note N[OF `N \<le> n`]
  2157       finally show "norm (integral (u n) - integral u') < r" by simp
  2158     qed
  2159   qed
  2160 qed
  2161 
  2162 lemma (in measure_space) integral_sums:
  2163   assumes borel: "\<And>i. integrable (f i)"
  2164   and summable: "\<And>x. x \<in> space M \<Longrightarrow> summable (\<lambda>i. \<bar>f i x\<bar>)"
  2165   and sums: "summable (\<lambda>i. (\<integral>x. \<bar>f i x\<bar>))"
  2166   shows "integrable (\<lambda>x. (\<Sum>i. f i x))" (is "integrable ?S")
  2167   and "(\<lambda>i. integral (f i)) sums (\<integral>x. (\<Sum>i. f i x))" (is ?integral)
  2168 proof -
  2169   have "\<forall>x\<in>space M. \<exists>w. (\<lambda>i. \<bar>f i x\<bar>) sums w"
  2170     using summable unfolding summable_def by auto
  2171   from bchoice[OF this]
  2172   obtain w where w: "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. \<bar>f i x\<bar>) sums w x" by auto
  2173 
  2174   let "?w y" = "if y \<in> space M then w y else 0"
  2175 
  2176   obtain x where abs_sum: "(\<lambda>i. (\<integral>x. \<bar>f i x\<bar>)) sums x"
  2177     using sums unfolding summable_def ..
  2178 
  2179   have 1: "\<And>n. integrable (\<lambda>x. \<Sum>i = 0..<n. f i x)"
  2180     using borel by (auto intro!: integral_setsum)
  2181 
  2182   { fix j x assume [simp]: "x \<in> space M"
  2183     have "\<bar>\<Sum>i = 0..< j. f i x\<bar> \<le> (\<Sum>i = 0..< j. \<bar>f i x\<bar>)" by (rule setsum_abs)
  2184     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
  2185     finally have "\<bar>\<Sum>i = 0..<j. f i x\<bar> \<le> ?w x" by simp }
  2186   note 2 = this
  2187 
  2188   have 3: "integrable ?w"
  2189   proof (rule integral_monotone_convergence(1))
  2190     let "?F n y" = "(\<Sum>i = 0..<n. \<bar>f i y\<bar>)"
  2191     let "?w' n y" = "if y \<in> space M then ?F n y else 0"
  2192     have "\<And>n. integrable (?F n)"
  2193       using borel by (auto intro!: integral_setsum integrable_abs)
  2194     thus "\<And>n. integrable (?w' n)" by (simp cong: integrable_cong)
  2195     show "mono ?w'"
  2196       by (auto simp: mono_def le_fun_def intro!: setsum_mono2)
  2197     { fix x show "(\<lambda>n. ?w' n x) ----> ?w x"
  2198         using w by (cases "x \<in> space M") (simp_all add: LIMSEQ_const sums_def) }
  2199     have *: "\<And>n. integral (?w' n) = (\<Sum>i = 0..< n. (\<integral>x. \<bar>f i x\<bar>))"
  2200       using borel by (simp add: integral_setsum integrable_abs cong: integral_cong)
  2201     from abs_sum
  2202     show "(\<lambda>i. integral (?w' i)) ----> x" unfolding * sums_def .
  2203   qed
  2204 
  2205   have 4: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> ?w x" using 2[of _ 0] by simp
  2206 
  2207   from summable[THEN summable_rabs_cancel]
  2208   have 5: "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>n. \<Sum>i = 0..<n. f i x) ----> (\<Sum>i. f i x)"
  2209     by (auto intro: summable_sumr_LIMSEQ_suminf)
  2210 
  2211   note int = integral_dominated_convergence(1,3)[OF 1 2 3 4 5]
  2212 
  2213   from int show "integrable ?S" by simp
  2214 
  2215   show ?integral unfolding sums_def integral_setsum(1)[symmetric, OF borel]
  2216     using int(2) by simp
  2217 qed
  2218 
  2219 section "Lebesgue integration on countable spaces"
  2220 
  2221 lemma (in measure_space) integral_on_countable:
  2222   assumes f: "f \<in> borel_measurable M"
  2223   and bij: "bij_betw enum S (f ` space M)"
  2224   and enum_zero: "enum ` (-S) \<subseteq> {0}"
  2225   and fin: "\<And>x. x \<noteq> 0 \<Longrightarrow> \<mu> (f -` {x} \<inter> space M) \<noteq> \<omega>"
  2226   and abs_summable: "summable (\<lambda>r. \<bar>enum r * real (\<mu> (f -` {enum r} \<inter> space M))\<bar>)"
  2227   shows "integrable f"
  2228   and "(\<lambda>r. enum r * real (\<mu> (f -` {enum r} \<inter> space M))) sums integral f" (is ?sums)
  2229 proof -
  2230   let "?A r" = "f -` {enum r} \<inter> space M"
  2231   let "?F r x" = "enum r * indicator (?A r) x"
  2232   have enum_eq: "\<And>r. enum r * real (\<mu> (?A r)) = integral (?F r)"
  2233     using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
  2234 
  2235   { fix x assume "x \<in> space M"
  2236     hence "f x \<in> enum ` S" using bij unfolding bij_betw_def by auto
  2237     then obtain i where "i\<in>S" "enum i = f x" by auto
  2238     have F: "\<And>j. ?F j x = (if j = i then f x else 0)"
  2239     proof cases
  2240       fix j assume "j = i"
  2241       thus "?thesis j" using `x \<in> space M` `enum i = f x` by (simp add: indicator_def)
  2242     next
  2243       fix j assume "j \<noteq> i"
  2244       show "?thesis j" using bij `i \<in> S` `j \<noteq> i` `enum i = f x` enum_zero
  2245         by (cases "j \<in> S") (auto simp add: indicator_def bij_betw_def inj_on_def)
  2246     qed
  2247     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
  2248     have "(\<lambda>i. ?F i x) sums f x"
  2249          "(\<lambda>i. \<bar>?F i x\<bar>) sums \<bar>f x\<bar>"
  2250       by (auto intro!: sums_single simp: F F_abs) }
  2251   note F_sums_f = this(1) and F_abs_sums_f = this(2)
  2252 
  2253   have int_f: "integral f = (\<integral>x. \<Sum>r. ?F r x)" "integrable f = integrable (\<lambda>x. \<Sum>r. ?F r x)"
  2254     using F_sums_f by (auto intro!: integral_cong integrable_cong simp: sums_iff)
  2255 
  2256   { fix r
  2257     have "(\<integral>x. \<bar>?F r x\<bar>) = (\<integral>x. \<bar>enum r\<bar> * indicator (?A r) x)"
  2258       by (auto simp: indicator_def intro!: integral_cong)
  2259     also have "\<dots> = \<bar>enum r\<bar> * real (\<mu> (?A r))"
  2260       using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
  2261     finally have "(\<integral>x. \<bar>?F r x\<bar>) = \<bar>enum r * real (\<mu> (?A r))\<bar>"
  2262       by (simp add: abs_mult_pos real_pextreal_pos) }
  2263   note int_abs_F = this
  2264 
  2265   have 1: "\<And>i. integrable (\<lambda>x. ?F i x)"
  2266     using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
  2267 
  2268   have 2: "\<And>x. x \<in> space M \<Longrightarrow> summable (\<lambda>i. \<bar>?F i x\<bar>)"
  2269     using F_abs_sums_f unfolding sums_iff by auto
  2270 
  2271   from integral_sums(2)[OF 1 2, unfolded int_abs_F, OF _ abs_summable]
  2272   show ?sums unfolding enum_eq int_f by simp
  2273 
  2274   from integral_sums(1)[OF 1 2, unfolded int_abs_F, OF _ abs_summable]
  2275   show "integrable f" unfolding int_f by simp
  2276 qed
  2277 
  2278 section "Lebesgue integration on finite space"
  2279 
  2280 lemma (in measure_space) integral_on_finite:
  2281   assumes f: "f \<in> borel_measurable M" and finite: "finite (f`space M)"
  2282   and fin: "\<And>x. x \<noteq> 0 \<Longrightarrow> \<mu> (f -` {x} \<inter> space M) \<noteq> \<omega>"
  2283   shows "integrable f"
  2284   and "(\<integral>x. f x) =
  2285     (\<Sum> r \<in> f`space M. r * real (\<mu> (f -` {r} \<inter> space M)))" (is "?integral")
  2286 proof -
  2287   let "?A r" = "f -` {r} \<inter> space M"
  2288   let "?S x" = "\<Sum>r\<in>f`space M. r * indicator (?A r) x"
  2289 
  2290   { fix x assume "x \<in> space M"
  2291     have "f x = (\<Sum>r\<in>f`space M. if x \<in> ?A r then r else 0)"
  2292       using finite `x \<in> space M` by (simp add: setsum_cases)
  2293     also have "\<dots> = ?S x"
  2294       by (auto intro!: setsum_cong)
  2295     finally have "f x = ?S x" . }
  2296   note f_eq = this
  2297 
  2298   have f_eq_S: "integrable f \<longleftrightarrow> integrable ?S" "integral f = integral ?S"
  2299     by (auto intro!: integrable_cong integral_cong simp only: f_eq)
  2300 
  2301   show "integrable f" ?integral using fin f f_eq_S
  2302     by (simp_all add: integral_cmul_indicator borel_measurable_vimage)
  2303 qed
  2304 
  2305 lemma (in finite_measure_space) simple_function_finite[simp, intro]: "simple_function f"
  2306   unfolding simple_function_def using finite_space by auto
  2307 
  2308 lemma (in finite_measure_space) borel_measurable_finite[intro, simp]: "f \<in> borel_measurable M"
  2309   by (auto intro: borel_measurable_simple_function)
  2310 
  2311 lemma (in finite_measure_space) positive_integral_finite_eq_setsum:
  2312   "positive_integral f = (\<Sum>x \<in> space M. f x * \<mu> {x})"
  2313 proof -
  2314   have *: "positive_integral f = (\<integral>\<^isup>+ x. \<Sum>y\<in>space M. f y * indicator {y} x)"
  2315     by (auto intro!: positive_integral_cong simp add: indicator_def if_distrib setsum_cases[OF finite_space])
  2316   show ?thesis unfolding * using borel_measurable_finite[of f]
  2317     by (simp add: positive_integral_setsum positive_integral_cmult_indicator)
  2318 qed
  2319 
  2320 lemma (in finite_measure_space) integral_finite_singleton:
  2321   shows "integrable f"
  2322   and "integral f = (\<Sum>x \<in> space M. f x * real (\<mu> {x}))" (is ?I)
  2323 proof -
  2324   have [simp]:
  2325     "(\<integral>\<^isup>+ x. Real (f x)) = (\<Sum>x \<in> space M. Real (f x) * \<mu> {x})"
  2326     "(\<integral>\<^isup>+ x. Real (- f x)) = (\<Sum>x \<in> space M. Real (- f x) * \<mu> {x})"
  2327     unfolding positive_integral_finite_eq_setsum by auto
  2328   show "integrable f" using finite_space finite_measure
  2329     by (simp add: setsum_\<omega> integrable_def)
  2330   show ?I using finite_measure
  2331     apply (simp add: integral_def real_of_pextreal_setsum[symmetric]
  2332       real_of_pextreal_mult[symmetric] setsum_subtractf[symmetric])
  2333     by (rule setsum_cong) (simp_all split: split_if)
  2334 qed
  2335 
  2336 end