src/HOL/Probability/Lebesgue_Integration.thy
author hoelzl
Tue Jan 18 21:37:23 2011 +0100 (2011-01-18)
changeset 41654 32fe42892983
parent 41545 9c869baf1c66
child 41661 baf1964bc468
permissions -rw-r--r--
Gauge measure removed
     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 sigma_algebra) simple_function_vimage:
   475   fixes g :: "'a \<Rightarrow> pextreal" and f :: "'d \<Rightarrow> 'a"
   476   assumes g: "simple_function g" and f: "f \<in> S \<rightarrow> space M"
   477   shows "sigma_algebra.simple_function (vimage_algebra S f) (\<lambda>x. g (f x))"
   478 proof -
   479   have subset: "(\<lambda>x. g (f x)) ` S \<subseteq> g ` space M"
   480     using f by auto
   481   interpret V: sigma_algebra "vimage_algebra S f"
   482     using f by (rule sigma_algebra_vimage)
   483   show ?thesis using g
   484     unfolding simple_function_eq_borel_measurable
   485     unfolding V.simple_function_eq_borel_measurable
   486     using measurable_vimage[OF _ f, of g borel]
   487     using finite_subset[OF subset] by auto
   488 qed
   489 
   490 section "Simple integral"
   491 
   492 definition (in measure_space) simple_integral (binder "\<integral>\<^isup>S " 10) where
   493   "simple_integral f = (\<Sum>x \<in> f ` space M. x * \<mu> (f -` {x} \<inter> space M))"
   494 
   495 lemma (in measure_space) simple_integral_cong:
   496   assumes "\<And>t. t \<in> space M \<Longrightarrow> f t = g t"
   497   shows "simple_integral f = simple_integral g"
   498 proof -
   499   have "f ` space M = g ` space M"
   500     "\<And>x. f -` {x} \<inter> space M = g -` {x} \<inter> space M"
   501     using assms by (auto intro!: image_eqI)
   502   thus ?thesis unfolding simple_integral_def by simp
   503 qed
   504 
   505 lemma (in measure_space) simple_integral_cong_measure:
   506   assumes "\<And>A. A \<in> sets M \<Longrightarrow> \<nu> A = \<mu> A" and "simple_function f"
   507   shows "measure_space.simple_integral M \<nu> f = simple_integral f"
   508 proof -
   509   interpret v: measure_space M \<nu>
   510     by (rule measure_space_cong) fact
   511   from simple_functionD[OF `simple_function f`] assms show ?thesis
   512     unfolding simple_integral_def v.simple_integral_def
   513     by (auto intro!: setsum_cong)
   514 qed
   515 
   516 lemma (in measure_space) simple_integral_const[simp]:
   517   "(\<integral>\<^isup>Sx. c) = c * \<mu> (space M)"
   518 proof (cases "space M = {}")
   519   case True thus ?thesis unfolding simple_integral_def by simp
   520 next
   521   case False hence "(\<lambda>x. c) ` space M = {c}" by auto
   522   thus ?thesis unfolding simple_integral_def by simp
   523 qed
   524 
   525 lemma (in measure_space) simple_function_partition:
   526   assumes "simple_function f" and "simple_function g"
   527   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)"
   528     (is "_ = setsum _ (?p ` space M)")
   529 proof-
   530   let "?sub x" = "?p ` (f -` {x} \<inter> space M)"
   531   let ?SIGMA = "Sigma (f`space M) ?sub"
   532 
   533   have [intro]:
   534     "finite (f ` space M)"
   535     "finite (g ` space M)"
   536     using assms unfolding simple_function_def by simp_all
   537 
   538   { fix A
   539     have "?p ` (A \<inter> space M) \<subseteq>
   540       (\<lambda>(x,y). f -` {x} \<inter> g -` {y} \<inter> space M) ` (f`space M \<times> g`space M)"
   541       by auto
   542     hence "finite (?p ` (A \<inter> space M))"
   543       by (rule finite_subset) auto }
   544   note this[intro, simp]
   545 
   546   { fix x assume "x \<in> space M"
   547     have "\<Union>(?sub (f x)) = (f -` {f x} \<inter> space M)" by auto
   548     moreover {
   549       fix x y
   550       have *: "f -` {f x} \<inter> g -` {g x} \<inter> space M
   551           = (f -` {f x} \<inter> space M) \<inter> (g -` {g x} \<inter> space M)" by auto
   552       assume "x \<in> space M" "y \<in> space M"
   553       hence "f -` {f x} \<inter> g -` {g x} \<inter> space M \<in> sets M"
   554         using assms unfolding simple_function_def * by auto }
   555     ultimately
   556     have "\<mu> (f -` {f x} \<inter> space M) = setsum (\<mu>) (?sub (f x))"
   557       by (subst measure_finitely_additive) auto }
   558   hence "simple_integral f = (\<Sum>(x,A)\<in>?SIGMA. x * \<mu> A)"
   559     unfolding simple_integral_def
   560     by (subst setsum_Sigma[symmetric],
   561        auto intro!: setsum_cong simp: setsum_right_distrib[symmetric])
   562   also have "\<dots> = (\<Sum>A\<in>?p ` space M. the_elem (f`A) * \<mu> A)"
   563   proof -
   564     have [simp]: "\<And>x. x \<in> space M \<Longrightarrow> f ` ?p x = {f x}" by (auto intro!: imageI)
   565     have "(\<lambda>A. (the_elem (f ` A), A)) ` ?p ` space M
   566       = (\<lambda>x. (f x, ?p x)) ` space M"
   567     proof safe
   568       fix x assume "x \<in> space M"
   569       thus "(f x, ?p x) \<in> (\<lambda>A. (the_elem (f`A), A)) ` ?p ` space M"
   570         by (auto intro!: image_eqI[of _ _ "?p x"])
   571     qed auto
   572     thus ?thesis
   573       apply (auto intro!: setsum_reindex_cong[of "\<lambda>A. (the_elem (f`A), A)"] inj_onI)
   574       apply (rule_tac x="xa" in image_eqI)
   575       by simp_all
   576   qed
   577   finally show ?thesis .
   578 qed
   579 
   580 lemma (in measure_space) simple_integral_add[simp]:
   581   assumes "simple_function f" and "simple_function g"
   582   shows "(\<integral>\<^isup>Sx. f x + g x) = simple_integral f + simple_integral g"
   583 proof -
   584   { fix x let ?S = "g -` {g x} \<inter> f -` {f x} \<inter> space M"
   585     assume "x \<in> space M"
   586     hence "(\<lambda>a. f a + g a) ` ?S = {f x + g x}" "f ` ?S = {f x}" "g ` ?S = {g x}"
   587         "(\<lambda>x. (f x, g x)) -` {(f x, g x)} \<inter> space M = ?S"
   588       by auto }
   589   thus ?thesis
   590     unfolding
   591       simple_function_partition[OF simple_function_add[OF assms] simple_function_Pair[OF assms]]
   592       simple_function_partition[OF `simple_function f` `simple_function g`]
   593       simple_function_partition[OF `simple_function g` `simple_function f`]
   594     apply (subst (3) Int_commute)
   595     by (auto simp add: field_simps setsum_addf[symmetric] intro!: setsum_cong)
   596 qed
   597 
   598 lemma (in measure_space) simple_integral_setsum[simp]:
   599   assumes "\<And>i. i \<in> P \<Longrightarrow> simple_function (f i)"
   600   shows "(\<integral>\<^isup>Sx. \<Sum>i\<in>P. f i x) = (\<Sum>i\<in>P. simple_integral (f i))"
   601 proof cases
   602   assume "finite P"
   603   from this assms show ?thesis
   604     by induct (auto simp: simple_function_setsum simple_integral_add)
   605 qed auto
   606 
   607 lemma (in measure_space) simple_integral_mult[simp]:
   608   assumes "simple_function f"
   609   shows "(\<integral>\<^isup>Sx. c * f x) = c * simple_integral f"
   610 proof -
   611   note mult = simple_function_mult[OF simple_function_const[of c] assms]
   612   { fix x let ?S = "f -` {f x} \<inter> (\<lambda>x. c * f x) -` {c * f x} \<inter> space M"
   613     assume "x \<in> space M"
   614     hence "(\<lambda>x. c * f x) ` ?S = {c * f x}" "f ` ?S = {f x}"
   615       by auto }
   616   thus ?thesis
   617     unfolding simple_function_partition[OF mult assms]
   618       simple_function_partition[OF assms mult]
   619     by (auto simp: setsum_right_distrib field_simps intro!: setsum_cong)
   620 qed
   621 
   622 lemma (in sigma_algebra) simple_function_If:
   623   assumes sf: "simple_function f" "simple_function g" and A: "A \<in> sets M"
   624   shows "simple_function (\<lambda>x. if x \<in> A then f x else g x)" (is "simple_function ?IF")
   625 proof -
   626   def F \<equiv> "\<lambda>x. f -` {x} \<inter> space M" and G \<equiv> "\<lambda>x. g -` {x} \<inter> space M"
   627   show ?thesis unfolding simple_function_def
   628   proof safe
   629     have "?IF ` space M \<subseteq> f ` space M \<union> g ` space M" by auto
   630     from finite_subset[OF this] assms
   631     show "finite (?IF ` space M)" unfolding simple_function_def by auto
   632   next
   633     fix x assume "x \<in> space M"
   634     then have *: "?IF -` {?IF x} \<inter> space M = (if x \<in> A
   635       then ((F (f x) \<inter> A) \<union> (G (f x) - (G (f x) \<inter> A)))
   636       else ((F (g x) \<inter> A) \<union> (G (g x) - (G (g x) \<inter> A))))"
   637       using sets_into_space[OF A] by (auto split: split_if_asm simp: G_def F_def)
   638     have [intro]: "\<And>x. F x \<in> sets M" "\<And>x. G x \<in> sets M"
   639       unfolding F_def G_def using sf[THEN simple_functionD(2)] by auto
   640     show "?IF -` {?IF x} \<inter> space M \<in> sets M" unfolding * using A by auto
   641   qed
   642 qed
   643 
   644 lemma (in measure_space) simple_integral_mono_AE:
   645   assumes "simple_function f" and "simple_function g"
   646   and mono: "AE x. f x \<le> g x"
   647   shows "simple_integral f \<le> simple_integral g"
   648 proof -
   649   let "?S x" = "(g -` {g x} \<inter> space M) \<inter> (f -` {f x} \<inter> space M)"
   650   have *: "\<And>x. g -` {g x} \<inter> f -` {f x} \<inter> space M = ?S x"
   651     "\<And>x. f -` {f x} \<inter> g -` {g x} \<inter> space M = ?S x" by auto
   652   show ?thesis
   653     unfolding *
   654       simple_function_partition[OF `simple_function f` `simple_function g`]
   655       simple_function_partition[OF `simple_function g` `simple_function f`]
   656   proof (safe intro!: setsum_mono)
   657     fix x assume "x \<in> space M"
   658     then have *: "f ` ?S x = {f x}" "g ` ?S x = {g x}" by auto
   659     show "the_elem (f`?S x) * \<mu> (?S x) \<le> the_elem (g`?S x) * \<mu> (?S x)"
   660     proof (cases "f x \<le> g x")
   661       case True then show ?thesis using * by (auto intro!: mult_right_mono)
   662     next
   663       case False
   664       obtain N where N: "{x\<in>space M. \<not> f x \<le> g x} \<subseteq> N" "N \<in> sets M" "\<mu> N = 0"
   665         using mono by (auto elim!: AE_E)
   666       have "?S x \<subseteq> N" using N `x \<in> space M` False by auto
   667       moreover have "?S x \<in> sets M" using assms
   668         by (rule_tac Int) (auto intro!: simple_functionD)
   669       ultimately have "\<mu> (?S x) \<le> \<mu> N"
   670         using `N \<in> sets M` by (auto intro!: measure_mono)
   671       then show ?thesis using `\<mu> N = 0` by auto
   672     qed
   673   qed
   674 qed
   675 
   676 lemma (in measure_space) simple_integral_mono:
   677   assumes "simple_function f" and "simple_function g"
   678   and mono: "\<And> x. x \<in> space M \<Longrightarrow> f x \<le> g x"
   679   shows "simple_integral f \<le> simple_integral g"
   680 proof (rule simple_integral_mono_AE[OF assms(1, 2)])
   681   show "AE x. f x \<le> g x"
   682     using mono by (rule AE_cong) auto
   683 qed
   684 
   685 lemma (in measure_space) simple_integral_cong_AE:
   686   assumes "simple_function f" "simple_function g" and "AE x. f x = g x"
   687   shows "simple_integral f = simple_integral g"
   688   using assms by (auto simp: eq_iff intro!: simple_integral_mono_AE)
   689 
   690 lemma (in measure_space) simple_integral_cong':
   691   assumes sf: "simple_function f" "simple_function g"
   692   and mea: "\<mu> {x\<in>space M. f x \<noteq> g x} = 0"
   693   shows "simple_integral f = simple_integral g"
   694 proof (intro simple_integral_cong_AE sf AE_I)
   695   show "\<mu> {x\<in>space M. f x \<noteq> g x} = 0" by fact
   696   show "{x \<in> space M. f x \<noteq> g x} \<in> sets M"
   697     using sf[THEN borel_measurable_simple_function] by auto
   698 qed simp
   699 
   700 lemma (in measure_space) simple_integral_indicator:
   701   assumes "A \<in> sets M"
   702   assumes "simple_function f"
   703   shows "(\<integral>\<^isup>Sx. f x * indicator A x) =
   704     (\<Sum>x \<in> f ` space M. x * \<mu> (f -` {x} \<inter> space M \<inter> A))"
   705 proof cases
   706   assume "A = space M"
   707   moreover hence "(\<integral>\<^isup>Sx. f x * indicator A x) = simple_integral f"
   708     by (auto intro!: simple_integral_cong)
   709   moreover have "\<And>X. X \<inter> space M \<inter> space M = X \<inter> space M" by auto
   710   ultimately show ?thesis by (simp add: simple_integral_def)
   711 next
   712   assume "A \<noteq> space M"
   713   then obtain x where x: "x \<in> space M" "x \<notin> A" using sets_into_space[OF assms(1)] by auto
   714   have I: "(\<lambda>x. f x * indicator A x) ` space M = f ` A \<union> {0}" (is "?I ` _ = _")
   715   proof safe
   716     fix y assume "?I y \<notin> f ` A" hence "y \<notin> A" by auto thus "?I y = 0" by auto
   717   next
   718     fix y assume "y \<in> A" thus "f y \<in> ?I ` space M"
   719       using sets_into_space[OF assms(1)] by (auto intro!: image_eqI[of _ _ y])
   720   next
   721     show "0 \<in> ?I ` space M" using x by (auto intro!: image_eqI[of _ _ x])
   722   qed
   723   have *: "(\<integral>\<^isup>Sx. f x * indicator A x) =
   724     (\<Sum>x \<in> f ` space M \<union> {0}. x * \<mu> (f -` {x} \<inter> space M \<inter> A))"
   725     unfolding simple_integral_def I
   726   proof (rule setsum_mono_zero_cong_left)
   727     show "finite (f ` space M \<union> {0})"
   728       using assms(2) unfolding simple_function_def by auto
   729     show "f ` A \<union> {0} \<subseteq> f`space M \<union> {0}"
   730       using sets_into_space[OF assms(1)] by auto
   731     have "\<And>x. f x \<notin> f ` A \<Longrightarrow> f -` {f x} \<inter> space M \<inter> A = {}"
   732       by (auto simp: image_iff)
   733     thus "\<forall>i\<in>f ` space M \<union> {0} - (f ` A \<union> {0}).
   734       i * \<mu> (f -` {i} \<inter> space M \<inter> A) = 0" by auto
   735   next
   736     fix x assume "x \<in> f`A \<union> {0}"
   737     hence "x \<noteq> 0 \<Longrightarrow> ?I -` {x} \<inter> space M = f -` {x} \<inter> space M \<inter> A"
   738       by (auto simp: indicator_def split: split_if_asm)
   739     thus "x * \<mu> (?I -` {x} \<inter> space M) =
   740       x * \<mu> (f -` {x} \<inter> space M \<inter> A)" by (cases "x = 0") simp_all
   741   qed
   742   show ?thesis unfolding *
   743     using assms(2) unfolding simple_function_def
   744     by (auto intro!: setsum_mono_zero_cong_right)
   745 qed
   746 
   747 lemma (in measure_space) simple_integral_indicator_only[simp]:
   748   assumes "A \<in> sets M"
   749   shows "simple_integral (indicator A) = \<mu> A"
   750 proof cases
   751   assume "space M = {}" hence "A = {}" using sets_into_space[OF assms] by auto
   752   thus ?thesis unfolding simple_integral_def using `space M = {}` by auto
   753 next
   754   assume "space M \<noteq> {}" hence "(\<lambda>x. 1) ` space M = {1::pextreal}" by auto
   755   thus ?thesis
   756     using simple_integral_indicator[OF assms simple_function_const[of 1]]
   757     using sets_into_space[OF assms]
   758     by (auto intro!: arg_cong[where f="\<mu>"])
   759 qed
   760 
   761 lemma (in measure_space) simple_integral_null_set:
   762   assumes "simple_function u" "N \<in> null_sets"
   763   shows "(\<integral>\<^isup>Sx. u x * indicator N x) = 0"
   764 proof -
   765   have "AE x. indicator N x = (0 :: pextreal)"
   766     using `N \<in> null_sets` by (auto simp: indicator_def intro!: AE_I[of _ N])
   767   then have "(\<integral>\<^isup>Sx. u x * indicator N x) = (\<integral>\<^isup>Sx. 0)"
   768     using assms by (intro simple_integral_cong_AE) (auto intro!: AE_disjI2)
   769   then show ?thesis by simp
   770 qed
   771 
   772 lemma (in measure_space) simple_integral_cong_AE_mult_indicator:
   773   assumes sf: "simple_function f" and eq: "AE x. x \<in> S" and "S \<in> sets M"
   774   shows "simple_integral f = (\<integral>\<^isup>Sx. f x * indicator S x)"
   775 proof (rule simple_integral_cong_AE)
   776   show "simple_function f" by fact
   777   show "simple_function (\<lambda>x. f x * indicator S x)"
   778     using sf `S \<in> sets M` by auto
   779   from eq show "AE x. f x = f x * indicator S x"
   780     by (rule AE_mp) simp
   781 qed
   782 
   783 lemma (in measure_space) simple_integral_restricted:
   784   assumes "A \<in> sets M"
   785   assumes sf: "simple_function (\<lambda>x. f x * indicator A x)"
   786   shows "measure_space.simple_integral (restricted_space A) \<mu> f = (\<integral>\<^isup>Sx. f x * indicator A x)"
   787     (is "_ = simple_integral ?f")
   788   unfolding measure_space.simple_integral_def[OF restricted_measure_space[OF `A \<in> sets M`]]
   789   unfolding simple_integral_def
   790 proof (simp, safe intro!: setsum_mono_zero_cong_left)
   791   from sf show "finite (?f ` space M)"
   792     unfolding simple_function_def by auto
   793 next
   794   fix x assume "x \<in> A"
   795   then show "f x \<in> ?f ` space M"
   796     using sets_into_space `A \<in> sets M` by (auto intro!: image_eqI[of _ _ x])
   797 next
   798   fix x assume "x \<in> space M" "?f x \<notin> f`A"
   799   then have "x \<notin> A" by (auto simp: image_iff)
   800   then show "?f x * \<mu> (?f -` {?f x} \<inter> space M) = 0" by simp
   801 next
   802   fix x assume "x \<in> A"
   803   then have "f x \<noteq> 0 \<Longrightarrow>
   804     f -` {f x} \<inter> A = ?f -` {f x} \<inter> space M"
   805     using `A \<in> sets M` sets_into_space
   806     by (auto simp: indicator_def split: split_if_asm)
   807   then show "f x * \<mu> (f -` {f x} \<inter> A) =
   808     f x * \<mu> (?f -` {f x} \<inter> space M)"
   809     unfolding pextreal_mult_cancel_left by auto
   810 qed
   811 
   812 lemma (in measure_space) simple_integral_subalgebra:
   813   assumes N: "measure_space N \<mu>" and [simp]: "space N = space M"
   814   shows "measure_space.simple_integral N \<mu> = simple_integral"
   815   unfolding simple_integral_def_raw
   816   unfolding measure_space.simple_integral_def_raw[OF N] by simp
   817 
   818 lemma (in measure_space) simple_integral_vimage:
   819   fixes g :: "'a \<Rightarrow> pextreal" and f :: "'d \<Rightarrow> 'a"
   820   assumes f: "bij_betw f S (space M)"
   821   shows "simple_integral g =
   822          measure_space.simple_integral (vimage_algebra S f) (\<lambda>A. \<mu> (f ` A)) (\<lambda>x. g (f x))"
   823     (is "_ = measure_space.simple_integral ?T ?\<mu> _")
   824 proof -
   825   from f interpret T: measure_space ?T ?\<mu> by (rule measure_space_isomorphic)
   826   have surj: "f`S = space M"
   827     using f unfolding bij_betw_def by simp
   828   have *: "(\<lambda>x. g (f x)) ` S = g ` f ` S" by auto
   829   have **: "f`S = space M" using f unfolding bij_betw_def by auto
   830   { fix x assume "x \<in> space M"
   831     have "(f ` ((\<lambda>x. g (f x)) -` {g x} \<inter> S)) =
   832       (f ` (f -` (g -` {g x}) \<inter> S))" by auto
   833     also have "f -` (g -` {g x}) \<inter> S = f -` (g -` {g x} \<inter> space M) \<inter> S"
   834       using f unfolding bij_betw_def by auto
   835     also have "(f ` (f -` (g -` {g x} \<inter> space M) \<inter> S)) = g -` {g x} \<inter> space M"
   836       using ** by (intro image_vimage_inter_eq) auto
   837     finally have "(f ` ((\<lambda>x. g (f x)) -` {g x} \<inter> S)) = g -` {g x} \<inter> space M" by auto }
   838   then show ?thesis using assms
   839     unfolding simple_integral_def T.simple_integral_def bij_betw_def
   840     by (auto simp add: * intro!: setsum_cong)
   841 qed
   842 
   843 section "Continuous posititve integration"
   844 
   845 definition (in measure_space) positive_integral (binder "\<integral>\<^isup>+ " 10) where
   846   "positive_integral f = SUPR {g. simple_function g \<and> g \<le> f} simple_integral"
   847 
   848 lemma (in measure_space) positive_integral_alt:
   849   "positive_integral f =
   850     (SUPR {g. simple_function g \<and> g \<le> f \<and> \<omega> \<notin> g`space M} simple_integral)" (is "_ = ?alt")
   851 proof (rule antisym SUP_leI)
   852   show "positive_integral f \<le> ?alt" unfolding positive_integral_def
   853   proof (safe intro!: SUP_leI)
   854     fix g assume g: "simple_function g" "g \<le> f"
   855     let ?G = "g -` {\<omega>} \<inter> space M"
   856     show "simple_integral g \<le>
   857       SUPR {g. simple_function g \<and> g \<le> f \<and> \<omega> \<notin> g ` space M} simple_integral"
   858       (is "simple_integral g \<le> SUPR ?A simple_integral")
   859     proof cases
   860       let ?g = "\<lambda>x. indicator (space M - ?G) x * g x"
   861       have g': "simple_function ?g"
   862         using g by (auto intro: simple_functionD)
   863       moreover
   864       assume "\<mu> ?G = 0"
   865       then have "AE x. g x = ?g x" using g
   866         by (intro AE_I[where N="?G"])
   867            (auto intro: simple_functionD simp: indicator_def)
   868       with g(1) g' have "simple_integral g = simple_integral ?g"
   869         by (rule simple_integral_cong_AE)
   870       moreover have "?g \<le> g" by (auto simp: le_fun_def indicator_def)
   871       from this `g \<le> f` have "?g \<le> f" by (rule order_trans)
   872       moreover have "\<omega> \<notin> ?g ` space M"
   873         by (auto simp: indicator_def split: split_if_asm)
   874       ultimately show ?thesis by (auto intro!: le_SUPI)
   875     next
   876       assume "\<mu> ?G \<noteq> 0"
   877       then have "?G \<noteq> {}" by auto
   878       then have "\<omega> \<in> g`space M" by force
   879       then have "space M \<noteq> {}" by auto
   880       have "SUPR ?A simple_integral = \<omega>"
   881       proof (intro SUP_\<omega>[THEN iffD2] allI impI)
   882         fix x assume "x < \<omega>"
   883         then guess n unfolding less_\<omega>_Ex_of_nat .. note n = this
   884         then have "0 < n" by (intro neq0_conv[THEN iffD1] notI) simp
   885         let ?g = "\<lambda>x. (of_nat n / (if \<mu> ?G = \<omega> then 1 else \<mu> ?G)) * indicator ?G x"
   886         show "\<exists>i\<in>?A. x < simple_integral i"
   887         proof (intro bexI impI CollectI conjI)
   888           show "simple_function ?g" using g
   889             by (auto intro!: simple_functionD simple_function_add)
   890           have "?g \<le> g" by (auto simp: le_fun_def indicator_def)
   891           from this g(2) show "?g \<le> f" by (rule order_trans)
   892           show "\<omega> \<notin> ?g ` space M"
   893             using `\<mu> ?G \<noteq> 0` by (auto simp: indicator_def split: split_if_asm)
   894           have "x < (of_nat n / (if \<mu> ?G = \<omega> then 1 else \<mu> ?G)) * \<mu> ?G"
   895             using n `\<mu> ?G \<noteq> 0` `0 < n`
   896             by (auto simp: pextreal_noteq_omega_Ex field_simps)
   897           also have "\<dots> = simple_integral ?g" using g `space M \<noteq> {}`
   898             by (subst simple_integral_indicator)
   899                (auto simp: image_constant ac_simps dest: simple_functionD)
   900           finally show "x < simple_integral ?g" .
   901         qed
   902       qed
   903       then show ?thesis by simp
   904     qed
   905   qed
   906 qed (auto intro!: SUP_subset simp: positive_integral_def)
   907 
   908 lemma (in measure_space) positive_integral_cong_measure:
   909   assumes "\<And>A. A \<in> sets M \<Longrightarrow> \<nu> A = \<mu> A"
   910   shows "measure_space.positive_integral M \<nu> f = positive_integral f"
   911 proof -
   912   interpret v: measure_space M \<nu>
   913     by (rule measure_space_cong) fact
   914   with assms show ?thesis
   915     unfolding positive_integral_def v.positive_integral_def SUPR_def
   916     by (auto intro!: arg_cong[where f=Sup] image_cong
   917              simp: simple_integral_cong_measure[of \<nu>])
   918 qed
   919 
   920 lemma (in measure_space) positive_integral_alt1:
   921   "positive_integral f =
   922     (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)"
   923   unfolding positive_integral_alt SUPR_def
   924 proof (safe intro!: arg_cong[where f=Sup])
   925   fix g let ?g = "\<lambda>x. if x \<in> space M then g x else f x"
   926   assume "simple_function g" "\<forall>x\<in>space M. g x \<le> f x \<and> g x \<noteq> \<omega>"
   927   hence "?g \<le> f" "simple_function ?g" "simple_integral g = simple_integral ?g"
   928     "\<omega> \<notin> g`space M"
   929     unfolding le_fun_def by (auto cong: simple_function_cong simple_integral_cong)
   930   thus "simple_integral g \<in> simple_integral ` {g. simple_function g \<and> g \<le> f \<and> \<omega> \<notin> g`space M}"
   931     by auto
   932 next
   933   fix g assume "simple_function g" "g \<le> f" "\<omega> \<notin> g`space M"
   934   hence "simple_function g" "\<forall>x\<in>space M. g x \<le> f x \<and> g x \<noteq> \<omega>"
   935     by (auto simp add: le_fun_def image_iff)
   936   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>)}"
   937     by auto
   938 qed
   939 
   940 lemma (in measure_space) positive_integral_cong:
   941   assumes "\<And>x. x \<in> space M \<Longrightarrow> f x = g x"
   942   shows "positive_integral f = positive_integral g"
   943 proof -
   944   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>)"
   945     using assms by auto
   946   thus ?thesis unfolding positive_integral_alt1 by auto
   947 qed
   948 
   949 lemma (in measure_space) positive_integral_eq_simple_integral:
   950   assumes "simple_function f"
   951   shows "positive_integral f = simple_integral f"
   952   unfolding positive_integral_def
   953 proof (safe intro!: pextreal_SUPI)
   954   fix g assume "simple_function g" "g \<le> f"
   955   with assms show "simple_integral g \<le> simple_integral f"
   956     by (auto intro!: simple_integral_mono simp: le_fun_def)
   957 next
   958   fix y assume "\<forall>x. x\<in>{g. simple_function g \<and> g \<le> f} \<longrightarrow> simple_integral x \<le> y"
   959   with assms show "simple_integral f \<le> y" by auto
   960 qed
   961 
   962 lemma (in measure_space) positive_integral_mono_AE:
   963   assumes ae: "AE x. u x \<le> v x"
   964   shows "positive_integral u \<le> positive_integral v"
   965   unfolding positive_integral_alt1
   966 proof (safe intro!: SUPR_mono)
   967   fix a assume a: "simple_function a" and mono: "\<forall>x\<in>space M. a x \<le> u x \<and> a x \<noteq> \<omega>"
   968   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"
   969     by (auto elim!: AE_E)
   970   have "simple_function (\<lambda>x. a x * indicator (space M - N) x)"
   971     using `N \<in> sets M` a by auto
   972   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>)}.
   973     simple_integral a \<le> simple_integral b"
   974   proof (safe intro!: bexI[of _ "\<lambda>x. a x * indicator (space M - N) x"]
   975                       simple_integral_mono_AE)
   976     show "AE x. a x \<le> a x * indicator (space M - N) x"
   977     proof (rule AE_I, rule subset_refl)
   978       have *: "{x \<in> space M. \<not> a x \<le> a x * indicator (space M - N) x} =
   979         N \<inter> {x \<in> space M. a x \<noteq> 0}" (is "?N = _")
   980         using `N \<in> sets M`[THEN sets_into_space] by (auto simp: indicator_def)
   981       then show "?N \<in> sets M" 
   982         using `N \<in> sets M` `simple_function a`[THEN borel_measurable_simple_function]
   983         by (auto intro!: measure_mono Int)
   984       then have "\<mu> ?N \<le> \<mu> N"
   985         unfolding * using `N \<in> sets M` by (auto intro!: measure_mono)
   986       then show "\<mu> ?N = 0" using `\<mu> N = 0` by auto
   987     qed
   988   next
   989     fix x assume "x \<in> space M"
   990     show "a x * indicator (space M - N) x \<le> v x"
   991     proof (cases "x \<in> N")
   992       case True then show ?thesis by simp
   993     next
   994       case False
   995       with N mono have "a x \<le> u x" "u x \<le> v x" using `x \<in> space M` by auto
   996       with False `x \<in> space M` show "a x * indicator (space M - N) x \<le> v x" by auto
   997     qed
   998     assume "a x * indicator (space M - N) x = \<omega>"
   999     with mono `x \<in> space M` show False
  1000       by (simp split: split_if_asm add: indicator_def)
  1001   qed
  1002 qed
  1003 
  1004 lemma (in measure_space) positive_integral_cong_AE:
  1005   "AE x. u x = v x \<Longrightarrow> positive_integral u = positive_integral v"
  1006   by (auto simp: eq_iff intro!: positive_integral_mono_AE)
  1007 
  1008 lemma (in measure_space) positive_integral_mono:
  1009   assumes mono: "\<And>x. x \<in> space M \<Longrightarrow> u x \<le> v x"
  1010   shows "positive_integral u \<le> positive_integral v"
  1011   using mono by (auto intro!: AE_cong positive_integral_mono_AE)
  1012 
  1013 lemma image_set_cong:
  1014   assumes A: "\<And>x. x \<in> A \<Longrightarrow> \<exists>y\<in>B. f x = g y"
  1015   assumes B: "\<And>y. y \<in> B \<Longrightarrow> \<exists>x\<in>A. g y = f x"
  1016   shows "f ` A = g ` B"
  1017   using assms by blast
  1018 
  1019 lemma (in measure_space) positive_integral_vimage:
  1020   fixes g :: "'a \<Rightarrow> pextreal" and f :: "'d \<Rightarrow> 'a"
  1021   assumes f: "bij_betw f S (space M)"
  1022   shows "positive_integral g =
  1023          measure_space.positive_integral (vimage_algebra S f) (\<lambda>A. \<mu> (f ` A)) (\<lambda>x. g (f x))"
  1024     (is "_ = measure_space.positive_integral ?T ?\<mu> _")
  1025 proof -
  1026   from f interpret T: measure_space ?T ?\<mu> by (rule measure_space_isomorphic)
  1027   have f_fun: "f \<in> S \<rightarrow> space M" using assms unfolding bij_betw_def by auto
  1028   from assms have inv: "bij_betw (the_inv_into S f) (space M) S"
  1029     by (rule bij_betw_the_inv_into)
  1030   then have inv_fun: "the_inv_into S f \<in> space M \<rightarrow> S" unfolding bij_betw_def by auto
  1031   have surj: "f`S = space M"
  1032     using f unfolding bij_betw_def by simp
  1033   have inj: "inj_on f S"
  1034     using f unfolding bij_betw_def by simp
  1035   have inv_f: "\<And>x. x \<in> space M \<Longrightarrow> f (the_inv_into S f x) = x"
  1036     using f_the_inv_into_f[of f S] f unfolding bij_betw_def by auto
  1037   from simple_integral_vimage[OF assms, symmetric]
  1038   have *: "simple_integral = T.simple_integral \<circ> (\<lambda>g. g \<circ> f)" by (simp add: comp_def)
  1039   show ?thesis
  1040     unfolding positive_integral_alt1 T.positive_integral_alt1 SUPR_def * image_compose
  1041   proof (safe intro!: arg_cong[where f=Sup] image_set_cong, simp_all add: comp_def)
  1042     fix g' :: "'a \<Rightarrow> pextreal" assume "simple_function g'" "\<forall>x\<in>space M. g' x \<le> g x \<and> g' x \<noteq> \<omega>"
  1043     then show "\<exists>h. T.simple_function h \<and> (\<forall>x\<in>S. h x \<le> g (f x) \<and> h x \<noteq> \<omega>) \<and>
  1044                    T.simple_integral (\<lambda>x. g' (f x)) = T.simple_integral h"
  1045       using f unfolding bij_betw_def
  1046       by (auto intro!: exI[of _ "\<lambda>x. g' (f x)"]
  1047                simp add: le_fun_def simple_function_vimage[OF _ f_fun])
  1048   next
  1049     fix g' :: "'d \<Rightarrow> pextreal" assume g': "T.simple_function g'" "\<forall>x\<in>S. g' x \<le> g (f x) \<and> g' x \<noteq> \<omega>"
  1050     let ?g = "\<lambda>x. g' (the_inv_into S f x)"
  1051     show "\<exists>h. simple_function h \<and> (\<forall>x\<in>space M. h x \<le> g x \<and> h x \<noteq> \<omega>) \<and>
  1052               T.simple_integral g' = T.simple_integral (\<lambda>x. h (f x))"
  1053     proof (intro exI[of _ ?g] conjI ballI)
  1054       { fix x assume x: "x \<in> space M"
  1055         then have "the_inv_into S f x \<in> S" using inv_fun by auto
  1056         with g' have "g' (the_inv_into S f x) \<le> g (f (the_inv_into S f x)) \<and> g' (the_inv_into S f x) \<noteq> \<omega>"
  1057           by auto
  1058         then show "g' (the_inv_into S f x) \<le> g x" "g' (the_inv_into S f x) \<noteq> \<omega>"
  1059           using f_the_inv_into_f[of f S x] x f unfolding bij_betw_def by auto }
  1060       note vimage_vimage_inv[OF f inv_f inv_fun, simp]
  1061       from T.simple_function_vimage[OF g'(1), unfolded space_vimage_algebra, OF inv_fun]
  1062       show "simple_function (\<lambda>x. g' (the_inv_into S f x))"
  1063         unfolding simple_function_def by (simp add: simple_function_def)
  1064       show "T.simple_integral g' = T.simple_integral (\<lambda>x. ?g (f x))"
  1065         using the_inv_into_f_f[OF inj] by (auto intro!: T.simple_integral_cong)
  1066     qed
  1067   qed
  1068 qed
  1069 
  1070 lemma (in measure_space) positive_integral_vimage_inv:
  1071   fixes g :: "'d \<Rightarrow> pextreal" and f :: "'d \<Rightarrow> 'a"
  1072   assumes f: "bij_inv S (space M) f h"
  1073   shows "measure_space.positive_integral (vimage_algebra S f) (\<lambda>A. \<mu> (f ` A)) g =
  1074       (\<integral>\<^isup>+x. g (h x))"
  1075 proof -
  1076   interpret v: measure_space "vimage_algebra S f" "\<lambda>A. \<mu> (f ` A)"
  1077     using f by (rule measure_space_isomorphic[OF bij_inv_bij_betw(1)])
  1078   show ?thesis
  1079     unfolding positive_integral_vimage[OF f[THEN bij_inv_bij_betw(1)], of "\<lambda>x. g (h x)"]
  1080     using f[unfolded bij_inv_def]
  1081     by (auto intro!: v.positive_integral_cong)
  1082 qed
  1083 
  1084 lemma (in measure_space) positive_integral_SUP_approx:
  1085   assumes "f \<up> s"
  1086   and f: "\<And>i. f i \<in> borel_measurable M"
  1087   and "simple_function u"
  1088   and le: "u \<le> s" and real: "\<omega> \<notin> u`space M"
  1089   shows "simple_integral u \<le> (SUP i. positive_integral (f i))" (is "_ \<le> ?S")
  1090 proof (rule pextreal_le_mult_one_interval)
  1091   fix a :: pextreal assume "0 < a" "a < 1"
  1092   hence "a \<noteq> 0" by auto
  1093   let "?B i" = "{x \<in> space M. a * u x \<le> f i x}"
  1094   have B: "\<And>i. ?B i \<in> sets M"
  1095     using f `simple_function u` by (auto simp: borel_measurable_simple_function)
  1096 
  1097   let "?uB i x" = "u x * indicator (?B i) x"
  1098 
  1099   { fix i have "?B i \<subseteq> ?B (Suc i)"
  1100     proof safe
  1101       fix i x assume "a * u x \<le> f i x"
  1102       also have "\<dots> \<le> f (Suc i) x"
  1103         using `f \<up> s` unfolding isoton_def le_fun_def by auto
  1104       finally show "a * u x \<le> f (Suc i) x" .
  1105     qed }
  1106   note B_mono = this
  1107 
  1108   have u: "\<And>x. x \<in> space M \<Longrightarrow> u -` {u x} \<inter> space M \<in> sets M"
  1109     using `simple_function u` by (auto simp add: simple_function_def)
  1110 
  1111   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
  1112   proof safe
  1113     fix x i assume "x \<in> space M"
  1114     show "x \<in> (\<Union>i. (u -` {u x} \<inter> space M) \<inter> ?B i)"
  1115     proof cases
  1116       assume "u x = 0" thus ?thesis using `x \<in> space M` by simp
  1117     next
  1118       assume "u x \<noteq> 0"
  1119       with `a < 1` real `x \<in> space M`
  1120       have "a * u x < 1 * u x" by (rule_tac pextreal_mult_strict_right_mono) (auto simp: image_iff)
  1121       also have "\<dots> \<le> (SUP i. f i x)" using le `f \<up> s`
  1122         unfolding isoton_fun_expand by (auto simp: isoton_def le_fun_def)
  1123       finally obtain i where "a * u x < f i x" unfolding SUPR_def
  1124         by (auto simp add: less_Sup_iff)
  1125       hence "a * u x \<le> f i x" by auto
  1126       thus ?thesis using `x \<in> space M` by auto
  1127     qed
  1128   qed auto
  1129   note measure_conv = measure_up[OF Int[OF u B] this]
  1130 
  1131   have "simple_integral u = (SUP i. simple_integral (?uB i))"
  1132     unfolding simple_integral_indicator[OF B `simple_function u`]
  1133   proof (subst SUPR_pextreal_setsum, safe)
  1134     fix x n assume "x \<in> space M"
  1135     have "\<mu> (u -` {u x} \<inter> space M \<inter> {x \<in> space M. a * u x \<le> f n x})
  1136       \<le> \<mu> (u -` {u x} \<inter> space M \<inter> {x \<in> space M. a * u x \<le> f (Suc n) x})"
  1137       using B_mono Int[OF u[OF `x \<in> space M`] B] by (auto intro!: measure_mono)
  1138     thus "u x * \<mu> (u -` {u x} \<inter> space M \<inter> ?B n)
  1139             \<le> u x * \<mu> (u -` {u x} \<inter> space M \<inter> ?B (Suc n))"
  1140       by (auto intro: mult_left_mono)
  1141   next
  1142     show "simple_integral u =
  1143       (\<Sum>i\<in>u ` space M. SUP n. i * \<mu> (u -` {i} \<inter> space M \<inter> ?B n))"
  1144       using measure_conv unfolding simple_integral_def isoton_def
  1145       by (auto intro!: setsum_cong simp: pextreal_SUP_cmult)
  1146   qed
  1147   moreover
  1148   have "a * (SUP i. simple_integral (?uB i)) \<le> ?S"
  1149     unfolding pextreal_SUP_cmult[symmetric]
  1150   proof (safe intro!: SUP_mono bexI)
  1151     fix i
  1152     have "a * simple_integral (?uB i) = (\<integral>\<^isup>Sx. a * ?uB i x)"
  1153       using B `simple_function u`
  1154       by (subst simple_integral_mult[symmetric]) (auto simp: field_simps)
  1155     also have "\<dots> \<le> positive_integral (f i)"
  1156     proof -
  1157       have "\<And>x. a * ?uB i x \<le> f i x" unfolding indicator_def by auto
  1158       hence *: "simple_function (\<lambda>x. a * ?uB i x)" using B assms(3)
  1159         by (auto intro!: simple_integral_mono)
  1160       show ?thesis unfolding positive_integral_eq_simple_integral[OF *, symmetric]
  1161         by (auto intro!: positive_integral_mono simp: indicator_def)
  1162     qed
  1163     finally show "a * simple_integral (?uB i) \<le> positive_integral (f i)"
  1164       by auto
  1165   qed simp
  1166   ultimately show "a * simple_integral u \<le> ?S" by simp
  1167 qed
  1168 
  1169 text {* Beppo-Levi monotone convergence theorem *}
  1170 lemma (in measure_space) positive_integral_isoton:
  1171   assumes "f \<up> u" "\<And>i. f i \<in> borel_measurable M"
  1172   shows "(\<lambda>i. positive_integral (f i)) \<up> positive_integral u"
  1173   unfolding isoton_def
  1174 proof safe
  1175   fix i show "positive_integral (f i) \<le> positive_integral (f (Suc i))"
  1176     apply (rule positive_integral_mono)
  1177     using `f \<up> u` unfolding isoton_def le_fun_def by auto
  1178 next
  1179   have u: "u = (SUP i. f i)" using `f \<up> u` unfolding isoton_def by auto
  1180 
  1181   show "(SUP i. positive_integral (f i)) = positive_integral u"
  1182   proof (rule antisym)
  1183     from `f \<up> u`[THEN isoton_Sup, unfolded le_fun_def]
  1184     show "(SUP j. positive_integral (f j)) \<le> positive_integral u"
  1185       by (auto intro!: SUP_leI positive_integral_mono)
  1186   next
  1187     show "positive_integral u \<le> (SUP i. positive_integral (f i))"
  1188       unfolding positive_integral_alt[of u]
  1189       by (auto intro!: SUP_leI positive_integral_SUP_approx[OF assms])
  1190   qed
  1191 qed
  1192 
  1193 lemma (in measure_space) positive_integral_monotone_convergence_SUP:
  1194   assumes "\<And>i x. x \<in> space M \<Longrightarrow> f i x \<le> f (Suc i) x"
  1195   assumes "\<And>i. f i \<in> borel_measurable M"
  1196   shows "(SUP i. positive_integral (f i)) = (\<integral>\<^isup>+ x. SUP i. f i x)"
  1197     (is "_ = positive_integral ?u")
  1198 proof -
  1199   show ?thesis
  1200   proof (rule antisym)
  1201     show "(SUP j. positive_integral (f j)) \<le> positive_integral ?u"
  1202       by (auto intro!: SUP_leI positive_integral_mono le_SUPI)
  1203   next
  1204     def rf \<equiv> "\<lambda>i. \<lambda>x\<in>space M. f i x" and ru \<equiv> "\<lambda>x\<in>space M. ?u x"
  1205     have "\<And>i. rf i \<in> borel_measurable M" unfolding rf_def
  1206       using assms by (simp cong: measurable_cong)
  1207     moreover have iso: "rf \<up> ru" using assms unfolding rf_def ru_def
  1208       unfolding isoton_def le_fun_def fun_eq_iff SUPR_apply
  1209       using SUP_const[OF UNIV_not_empty]
  1210       by (auto simp: restrict_def le_fun_def fun_eq_iff)
  1211     ultimately have "positive_integral ru \<le> (SUP i. positive_integral (rf i))"
  1212       unfolding positive_integral_alt[of ru]
  1213       by (auto simp: le_fun_def intro!: SUP_leI positive_integral_SUP_approx)
  1214     then show "positive_integral ?u \<le> (SUP i. positive_integral (f i))"
  1215       unfolding ru_def rf_def by (simp cong: positive_integral_cong)
  1216   qed
  1217 qed
  1218 
  1219 lemma (in measure_space) SUP_simple_integral_sequences:
  1220   assumes f: "f \<up> u" "\<And>i. simple_function (f i)"
  1221   and g: "g \<up> u" "\<And>i. simple_function (g i)"
  1222   shows "(SUP i. simple_integral (f i)) = (SUP i. simple_integral (g i))"
  1223     (is "SUPR _ ?F = SUPR _ ?G")
  1224 proof -
  1225   have "(SUP i. ?F i) = (SUP i. positive_integral (f i))"
  1226     using assms by (simp add: positive_integral_eq_simple_integral)
  1227   also have "\<dots> = positive_integral u"
  1228     using positive_integral_isoton[OF f(1) borel_measurable_simple_function[OF f(2)]]
  1229     unfolding isoton_def by simp
  1230   also have "\<dots> = (SUP i. positive_integral (g i))"
  1231     using positive_integral_isoton[OF g(1) borel_measurable_simple_function[OF g(2)]]
  1232     unfolding isoton_def by simp
  1233   also have "\<dots> = (SUP i. ?G i)"
  1234     using assms by (simp add: positive_integral_eq_simple_integral)
  1235   finally show ?thesis .
  1236 qed
  1237 
  1238 lemma (in measure_space) positive_integral_const[simp]:
  1239   "(\<integral>\<^isup>+ x. c) = c * \<mu> (space M)"
  1240   by (subst positive_integral_eq_simple_integral) auto
  1241 
  1242 lemma (in measure_space) positive_integral_isoton_simple:
  1243   assumes "f \<up> u" and e: "\<And>i. simple_function (f i)"
  1244   shows "(\<lambda>i. simple_integral (f i)) \<up> positive_integral u"
  1245   using positive_integral_isoton[OF `f \<up> u` e(1)[THEN borel_measurable_simple_function]]
  1246   unfolding positive_integral_eq_simple_integral[OF e] .
  1247 
  1248 lemma (in measure_space) positive_integral_linear:
  1249   assumes f: "f \<in> borel_measurable M"
  1250   and g: "g \<in> borel_measurable M"
  1251   shows "(\<integral>\<^isup>+ x. a * f x + g x) =
  1252       a * positive_integral f + positive_integral g"
  1253     (is "positive_integral ?L = _")
  1254 proof -
  1255   from borel_measurable_implies_simple_function_sequence'[OF f] guess u .
  1256   note u = this positive_integral_isoton_simple[OF this(1-2)]
  1257   from borel_measurable_implies_simple_function_sequence'[OF g] guess v .
  1258   note v = this positive_integral_isoton_simple[OF this(1-2)]
  1259   let "?L' i x" = "a * u i x + v i x"
  1260 
  1261   have "?L \<in> borel_measurable M"
  1262     using assms by simp
  1263   from borel_measurable_implies_simple_function_sequence'[OF this] guess l .
  1264   note positive_integral_isoton_simple[OF this(1-2)] and l = this
  1265   moreover have
  1266       "(SUP i. simple_integral (l i)) = (SUP i. simple_integral (?L' i))"
  1267   proof (rule SUP_simple_integral_sequences[OF l(1-2)])
  1268     show "?L' \<up> ?L" "\<And>i. simple_function (?L' i)"
  1269       using u v by (auto simp: isoton_fun_expand isoton_add isoton_cmult_right)
  1270   qed
  1271   moreover from u v have L'_isoton:
  1272       "(\<lambda>i. simple_integral (?L' i)) \<up> a * positive_integral f + positive_integral g"
  1273     by (simp add: isoton_add isoton_cmult_right)
  1274   ultimately show ?thesis by (simp add: isoton_def)
  1275 qed
  1276 
  1277 lemma (in measure_space) positive_integral_cmult:
  1278   assumes "f \<in> borel_measurable M"
  1279   shows "(\<integral>\<^isup>+ x. c * f x) = c * positive_integral f"
  1280   using positive_integral_linear[OF assms, of "\<lambda>x. 0" c] by auto
  1281 
  1282 lemma (in measure_space) positive_integral_multc:
  1283   assumes "f \<in> borel_measurable M"
  1284   shows "(\<integral>\<^isup>+ x. f x * c) = positive_integral f * c"
  1285   unfolding mult_commute[of _ c] positive_integral_cmult[OF assms] by simp
  1286 
  1287 lemma (in measure_space) positive_integral_indicator[simp]:
  1288   "A \<in> sets M \<Longrightarrow> (\<integral>\<^isup>+ x. indicator A x) = \<mu> A"
  1289   by (subst positive_integral_eq_simple_integral)
  1290      (auto simp: simple_function_indicator simple_integral_indicator)
  1291 
  1292 lemma (in measure_space) positive_integral_cmult_indicator:
  1293   "A \<in> sets M \<Longrightarrow> (\<integral>\<^isup>+ x. c * indicator A x) = c * \<mu> A"
  1294   by (subst positive_integral_eq_simple_integral)
  1295      (auto simp: simple_function_indicator simple_integral_indicator)
  1296 
  1297 lemma (in measure_space) positive_integral_add:
  1298   assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1299   shows "(\<integral>\<^isup>+ x. f x + g x) = positive_integral f + positive_integral g"
  1300   using positive_integral_linear[OF assms, of 1] by simp
  1301 
  1302 lemma (in measure_space) positive_integral_setsum:
  1303   assumes "\<And>i. i\<in>P \<Longrightarrow> f i \<in> borel_measurable M"
  1304   shows "(\<integral>\<^isup>+ x. \<Sum>i\<in>P. f i x) = (\<Sum>i\<in>P. positive_integral (f i))"
  1305 proof cases
  1306   assume "finite P"
  1307   from this assms show ?thesis
  1308   proof induct
  1309     case (insert i P)
  1310     have "f i \<in> borel_measurable M"
  1311       "(\<lambda>x. \<Sum>i\<in>P. f i x) \<in> borel_measurable M"
  1312       using insert by (auto intro!: borel_measurable_pextreal_setsum)
  1313     from positive_integral_add[OF this]
  1314     show ?case using insert by auto
  1315   qed simp
  1316 qed simp
  1317 
  1318 lemma (in measure_space) positive_integral_diff:
  1319   assumes f: "f \<in> borel_measurable M" and g: "g \<in> borel_measurable M"
  1320   and fin: "positive_integral g \<noteq> \<omega>"
  1321   and mono: "\<And>x. x \<in> space M \<Longrightarrow> g x \<le> f x"
  1322   shows "(\<integral>\<^isup>+ x. f x - g x) = positive_integral f - positive_integral g"
  1323 proof -
  1324   have borel: "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  1325     using f g by (rule borel_measurable_pextreal_diff)
  1326   have "(\<integral>\<^isup>+x. f x - g x) + positive_integral g =
  1327     positive_integral f"
  1328     unfolding positive_integral_add[OF borel g, symmetric]
  1329   proof (rule positive_integral_cong)
  1330     fix x assume "x \<in> space M"
  1331     from mono[OF this] show "f x - g x + g x = f x"
  1332       by (cases "f x", cases "g x", simp, simp, cases "g x", auto)
  1333   qed
  1334   with mono show ?thesis
  1335     by (subst minus_pextreal_eq2[OF _ fin]) (auto intro!: positive_integral_mono)
  1336 qed
  1337 
  1338 lemma (in measure_space) positive_integral_psuminf:
  1339   assumes "\<And>i. f i \<in> borel_measurable M"
  1340   shows "(\<integral>\<^isup>+ x. \<Sum>\<^isub>\<infinity> i. f i x) = (\<Sum>\<^isub>\<infinity> i. positive_integral (f i))"
  1341 proof -
  1342   have "(\<lambda>i. (\<integral>\<^isup>+x. \<Sum>i<i. f i x)) \<up> (\<integral>\<^isup>+x. \<Sum>\<^isub>\<infinity>i. f i x)"
  1343     by (rule positive_integral_isoton)
  1344        (auto intro!: borel_measurable_pextreal_setsum assms positive_integral_mono
  1345                      arg_cong[where f=Sup]
  1346              simp: isoton_def le_fun_def psuminf_def fun_eq_iff SUPR_def Sup_fun_def)
  1347   thus ?thesis
  1348     by (auto simp: isoton_def psuminf_def positive_integral_setsum[OF assms])
  1349 qed
  1350 
  1351 text {* Fatou's lemma: convergence theorem on limes inferior *}
  1352 lemma (in measure_space) positive_integral_lim_INF:
  1353   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> pextreal"
  1354   assumes "\<And>i. u i \<in> borel_measurable M"
  1355   shows "(\<integral>\<^isup>+ x. SUP n. INF m. u (m + n) x) \<le>
  1356     (SUP n. INF m. positive_integral (u (m + n)))"
  1357 proof -
  1358   have "(\<integral>\<^isup>+x. SUP n. INF m. u (m + n) x)
  1359       = (SUP n. (\<integral>\<^isup>+x. INF m. u (m + n) x))"
  1360     using assms
  1361     by (intro positive_integral_monotone_convergence_SUP[symmetric] INF_mono)
  1362        (auto simp del: add_Suc simp add: add_Suc[symmetric])
  1363   also have "\<dots> \<le> (SUP n. INF m. positive_integral (u (m + n)))"
  1364     by (auto intro!: SUP_mono bexI le_INFI positive_integral_mono INF_leI)
  1365   finally show ?thesis .
  1366 qed
  1367 
  1368 lemma (in measure_space) measure_space_density:
  1369   assumes borel: "u \<in> borel_measurable M"
  1370   shows "measure_space M (\<lambda>A. (\<integral>\<^isup>+ x. u x * indicator A x))" (is "measure_space M ?v")
  1371 proof
  1372   show "?v {} = 0" by simp
  1373   show "countably_additive M ?v"
  1374     unfolding countably_additive_def
  1375   proof safe
  1376     fix A :: "nat \<Rightarrow> 'a set"
  1377     assume "range A \<subseteq> sets M"
  1378     hence "\<And>i. (\<lambda>x. u x * indicator (A i) x) \<in> borel_measurable M"
  1379       using borel by (auto intro: borel_measurable_indicator)
  1380     moreover assume "disjoint_family A"
  1381     note psuminf_indicator[OF this]
  1382     ultimately show "(\<Sum>\<^isub>\<infinity>n. ?v (A n)) = ?v (\<Union>x. A x)"
  1383       by (simp add: positive_integral_psuminf[symmetric])
  1384   qed
  1385 qed
  1386 
  1387 lemma (in measure_space) positive_integral_translated_density:
  1388   assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1389   shows "measure_space.positive_integral M (\<lambda>A. (\<integral>\<^isup>+ x. f x * indicator A x)) g = 
  1390          (\<integral>\<^isup>+ x. f x * g x)" (is "measure_space.positive_integral M ?T _ = _")
  1391 proof -
  1392   from measure_space_density[OF assms(1)]
  1393   interpret T: measure_space M ?T .
  1394   from borel_measurable_implies_simple_function_sequence[OF assms(2)]
  1395   obtain G where G: "\<And>i. simple_function (G i)" "G \<up> g" by blast
  1396   note G_borel = borel_measurable_simple_function[OF this(1)]
  1397   from T.positive_integral_isoton[OF `G \<up> g` G_borel]
  1398   have *: "(\<lambda>i. T.positive_integral (G i)) \<up> T.positive_integral g" .
  1399   { fix i
  1400     have [simp]: "finite (G i ` space M)"
  1401       using G(1) unfolding simple_function_def by auto
  1402     have "T.positive_integral (G i) = T.simple_integral (G i)"
  1403       using G T.positive_integral_eq_simple_integral by simp
  1404     also have "\<dots> = (\<integral>\<^isup>+x. f x * (\<Sum>y\<in>G i`space M. y * indicator (G i -` {y} \<inter> space M) x))"
  1405       apply (simp add: T.simple_integral_def)
  1406       apply (subst positive_integral_cmult[symmetric])
  1407       using G_borel assms(1) apply (fastsimp intro: borel_measurable_indicator borel_measurable_vimage)
  1408       apply (subst positive_integral_setsum[symmetric])
  1409       using G_borel assms(1) apply (fastsimp intro: borel_measurable_indicator borel_measurable_vimage)
  1410       by (simp add: setsum_right_distrib field_simps)
  1411     also have "\<dots> = (\<integral>\<^isup>+x. f x * G i x)"
  1412       by (auto intro!: positive_integral_cong
  1413                simp: indicator_def if_distrib setsum_cases)
  1414     finally have "T.positive_integral (G i) = (\<integral>\<^isup>+x. f x * G i x)" . }
  1415   with * have eq_Tg: "(\<lambda>i. (\<integral>\<^isup>+x. f x * G i x)) \<up> T.positive_integral g" by simp
  1416   from G(2) have "(\<lambda>i x. f x * G i x) \<up> (\<lambda>x. f x * g x)"
  1417     unfolding isoton_fun_expand by (auto intro!: isoton_cmult_right)
  1418   then have "(\<lambda>i. (\<integral>\<^isup>+x. f x * G i x)) \<up> (\<integral>\<^isup>+x. f x * g x)"
  1419     using assms(1) G_borel by (auto intro!: positive_integral_isoton borel_measurable_pextreal_times)
  1420   with eq_Tg show "T.positive_integral g = (\<integral>\<^isup>+x. f x * g x)"
  1421     unfolding isoton_def by simp
  1422 qed
  1423 
  1424 lemma (in measure_space) positive_integral_null_set:
  1425   assumes "N \<in> null_sets" shows "(\<integral>\<^isup>+ x. u x * indicator N x) = 0"
  1426 proof -
  1427   have "(\<integral>\<^isup>+ x. u x * indicator N x) = (\<integral>\<^isup>+ x. 0)"
  1428   proof (intro positive_integral_cong_AE AE_I)
  1429     show "{x \<in> space M. u x * indicator N x \<noteq> 0} \<subseteq> N"
  1430       by (auto simp: indicator_def)
  1431     show "\<mu> N = 0" "N \<in> sets M"
  1432       using assms by auto
  1433   qed
  1434   then show ?thesis by simp
  1435 qed
  1436 
  1437 lemma (in measure_space) positive_integral_Markov_inequality:
  1438   assumes borel: "u \<in> borel_measurable M" and "A \<in> sets M" and c: "c \<noteq> \<omega>"
  1439   shows "\<mu> ({x\<in>space M. 1 \<le> c * u x} \<inter> A) \<le> c * (\<integral>\<^isup>+ x. u x * indicator A x)"
  1440     (is "\<mu> ?A \<le> _ * ?PI")
  1441 proof -
  1442   have "?A \<in> sets M"
  1443     using `A \<in> sets M` borel by auto
  1444   hence "\<mu> ?A = (\<integral>\<^isup>+ x. indicator ?A x)"
  1445     using positive_integral_indicator by simp
  1446   also have "\<dots> \<le> (\<integral>\<^isup>+ x. c * (u x * indicator A x))"
  1447   proof (rule positive_integral_mono)
  1448     fix x assume "x \<in> space M"
  1449     show "indicator ?A x \<le> c * (u x * indicator A x)"
  1450       by (cases "x \<in> ?A") auto
  1451   qed
  1452   also have "\<dots> = c * (\<integral>\<^isup>+ x. u x * indicator A x)"
  1453     using assms
  1454     by (auto intro!: positive_integral_cmult borel_measurable_indicator)
  1455   finally show ?thesis .
  1456 qed
  1457 
  1458 lemma (in measure_space) positive_integral_0_iff:
  1459   assumes borel: "u \<in> borel_measurable M"
  1460   shows "positive_integral u = 0 \<longleftrightarrow> \<mu> {x\<in>space M. u x \<noteq> 0} = 0"
  1461     (is "_ \<longleftrightarrow> \<mu> ?A = 0")
  1462 proof -
  1463   have A: "?A \<in> sets M" using borel by auto
  1464   have u: "(\<integral>\<^isup>+ x. u x * indicator ?A x) = positive_integral u"
  1465     by (auto intro!: positive_integral_cong simp: indicator_def)
  1466 
  1467   show ?thesis
  1468   proof
  1469     assume "\<mu> ?A = 0"
  1470     hence "?A \<in> null_sets" using `?A \<in> sets M` by auto
  1471     from positive_integral_null_set[OF this]
  1472     have "0 = (\<integral>\<^isup>+ x. u x * indicator ?A x)" by simp
  1473     thus "positive_integral u = 0" unfolding u by simp
  1474   next
  1475     assume *: "positive_integral u = 0"
  1476     let "?M n" = "{x \<in> space M. 1 \<le> of_nat n * u x}"
  1477     have "0 = (SUP n. \<mu> (?M n \<inter> ?A))"
  1478     proof -
  1479       { fix n
  1480         from positive_integral_Markov_inequality[OF borel `?A \<in> sets M`, of "of_nat n"]
  1481         have "\<mu> (?M n \<inter> ?A) = 0" unfolding * u by simp }
  1482       thus ?thesis by simp
  1483     qed
  1484     also have "\<dots> = \<mu> (\<Union>n. ?M n \<inter> ?A)"
  1485     proof (safe intro!: continuity_from_below)
  1486       fix n show "?M n \<inter> ?A \<in> sets M"
  1487         using borel by (auto intro!: Int)
  1488     next
  1489       fix n x assume "1 \<le> of_nat n * u x"
  1490       also have "\<dots> \<le> of_nat (Suc n) * u x"
  1491         by (subst (1 2) mult_commute) (auto intro!: pextreal_mult_cancel)
  1492       finally show "1 \<le> of_nat (Suc n) * u x" .
  1493     qed
  1494     also have "\<dots> = \<mu> ?A"
  1495     proof (safe intro!: arg_cong[where f="\<mu>"])
  1496       fix x assume "u x \<noteq> 0" and [simp, intro]: "x \<in> space M"
  1497       show "x \<in> (\<Union>n. ?M n \<inter> ?A)"
  1498       proof (cases "u x")
  1499         case (preal r)
  1500         obtain j where "1 / r \<le> of_nat j" using ex_le_of_nat ..
  1501         hence "1 / r * r \<le> of_nat j * r" using preal unfolding mult_le_cancel_right by auto
  1502         hence "1 \<le> of_nat j * r" using preal `u x \<noteq> 0` by auto
  1503         thus ?thesis using `u x \<noteq> 0` preal by (auto simp: real_of_nat_def[symmetric])
  1504       qed auto
  1505     qed
  1506     finally show "\<mu> ?A = 0" by simp
  1507   qed
  1508 qed
  1509 
  1510 lemma (in measure_space) positive_integral_restricted:
  1511   assumes "A \<in> sets M"
  1512   shows "measure_space.positive_integral (restricted_space A) \<mu> f = (\<integral>\<^isup>+ x. f x * indicator A x)"
  1513     (is "measure_space.positive_integral ?R \<mu> f = positive_integral ?f")
  1514 proof -
  1515   have msR: "measure_space ?R \<mu>" by (rule restricted_measure_space[OF `A \<in> sets M`])
  1516   then interpret R: measure_space ?R \<mu> .
  1517   have saR: "sigma_algebra ?R" by fact
  1518   have *: "R.positive_integral f = R.positive_integral ?f"
  1519     by (intro R.positive_integral_cong) auto
  1520   show ?thesis
  1521     unfolding * R.positive_integral_def positive_integral_def
  1522     unfolding simple_function_restricted[OF `A \<in> sets M`]
  1523     apply (simp add: SUPR_def)
  1524     apply (rule arg_cong[where f=Sup])
  1525   proof (auto simp add: image_iff simple_integral_restricted[OF `A \<in> sets M`])
  1526     fix g assume "simple_function (\<lambda>x. g x * indicator A x)"
  1527       "g \<le> f"
  1528     then show "\<exists>x. simple_function x \<and> x \<le> (\<lambda>x. f x * indicator A x) \<and>
  1529       (\<integral>\<^isup>Sx. g x * indicator A x) = simple_integral x"
  1530       apply (rule_tac exI[of _ "\<lambda>x. g x * indicator A x"])
  1531       by (auto simp: indicator_def le_fun_def)
  1532   next
  1533     fix g assume g: "simple_function g" "g \<le> (\<lambda>x. f x * indicator A x)"
  1534     then have *: "(\<lambda>x. g x * indicator A x) = g"
  1535       "\<And>x. g x * indicator A x = g x"
  1536       "\<And>x. g x \<le> f x"
  1537       by (auto simp: le_fun_def fun_eq_iff indicator_def split: split_if_asm)
  1538     from g show "\<exists>x. simple_function (\<lambda>xa. x xa * indicator A xa) \<and> x \<le> f \<and>
  1539       simple_integral g = simple_integral (\<lambda>xa. x xa * indicator A xa)"
  1540       using `A \<in> sets M`[THEN sets_into_space]
  1541       apply (rule_tac exI[of _ "\<lambda>x. g x * indicator A x"])
  1542       by (fastsimp simp: le_fun_def *)
  1543   qed
  1544 qed
  1545 
  1546 lemma (in measure_space) positive_integral_subalgebra:
  1547   assumes borel: "f \<in> borel_measurable N"
  1548   and N: "sets N \<subseteq> sets M" "space N = space M" and sa: "sigma_algebra N"
  1549   shows "measure_space.positive_integral N \<mu> f = positive_integral f"
  1550 proof -
  1551   interpret N: measure_space N \<mu> using measure_space_subalgebra[OF sa N] .
  1552   from N.borel_measurable_implies_simple_function_sequence[OF borel]
  1553   obtain fs where Nsf: "\<And>i. N.simple_function (fs i)" and "fs \<up> f" by blast
  1554   then have sf: "\<And>i. simple_function (fs i)"
  1555     using simple_function_subalgebra[OF _ N sa] by blast
  1556   from positive_integral_isoton_simple[OF `fs \<up> f` sf] N.positive_integral_isoton_simple[OF `fs \<up> f` Nsf]
  1557   show ?thesis unfolding isoton_def simple_integral_def N.simple_integral_def `space N = space M` by simp
  1558 qed
  1559 
  1560 section "Lebesgue Integral"
  1561 
  1562 definition (in measure_space) integrable where
  1563   "integrable f \<longleftrightarrow> f \<in> borel_measurable M \<and>
  1564     (\<integral>\<^isup>+ x. Real (f x)) \<noteq> \<omega> \<and>
  1565     (\<integral>\<^isup>+ x. Real (- f x)) \<noteq> \<omega>"
  1566 
  1567 lemma (in measure_space) integrableD[dest]:
  1568   assumes "integrable f"
  1569   shows "f \<in> borel_measurable M"
  1570   "(\<integral>\<^isup>+ x. Real (f x)) \<noteq> \<omega>"
  1571   "(\<integral>\<^isup>+ x. Real (- f x)) \<noteq> \<omega>"
  1572   using assms unfolding integrable_def by auto
  1573 
  1574 definition (in measure_space) integral (binder "\<integral> " 10) where
  1575   "integral f = real ((\<integral>\<^isup>+ x. Real (f x))) - real ((\<integral>\<^isup>+ x. Real (- f x)))"
  1576 
  1577 lemma (in measure_space) integral_cong:
  1578   assumes cong: "\<And>x. x \<in> space M \<Longrightarrow> f x = g x"
  1579   shows "integral f = integral g"
  1580   using assms by (simp cong: positive_integral_cong add: integral_def)
  1581 
  1582 lemma (in measure_space) integral_cong_measure:
  1583   assumes "\<And>A. A \<in> sets M \<Longrightarrow> \<nu> A = \<mu> A"
  1584   shows "measure_space.integral M \<nu> f = integral f"
  1585 proof -
  1586   interpret v: measure_space M \<nu>
  1587     by (rule measure_space_cong) fact
  1588   show ?thesis
  1589     unfolding integral_def v.integral_def
  1590     by (simp add: positive_integral_cong_measure[OF assms])
  1591 qed
  1592 
  1593 lemma (in measure_space) integral_cong_AE:
  1594   assumes cong: "AE x. f x = g x"
  1595   shows "integral f = integral g"
  1596 proof -
  1597   have "AE x. Real (f x) = Real (g x)"
  1598     using cong by (rule AE_mp) simp
  1599   moreover have "AE x. Real (- f x) = Real (- g x)"
  1600     using cong by (rule AE_mp) simp
  1601   ultimately show ?thesis
  1602     by (simp cong: positive_integral_cong_AE add: integral_def)
  1603 qed
  1604 
  1605 lemma (in measure_space) integrable_cong:
  1606   "(\<And>x. x \<in> space M \<Longrightarrow> f x = g x) \<Longrightarrow> integrable f \<longleftrightarrow> integrable g"
  1607   by (simp cong: positive_integral_cong measurable_cong add: integrable_def)
  1608 
  1609 lemma (in measure_space) integral_eq_positive_integral:
  1610   assumes "\<And>x. 0 \<le> f x"
  1611   shows "integral f = real ((\<integral>\<^isup>+ x. Real (f x)))"
  1612 proof -
  1613   have "\<And>x. Real (- f x) = 0" using assms by simp
  1614   thus ?thesis by (simp del: Real_eq_0 add: integral_def)
  1615 qed
  1616 
  1617 lemma (in measure_space) integral_vimage_inv:
  1618   assumes f: "bij_betw f S (space M)"
  1619   shows "measure_space.integral (vimage_algebra S f) (\<lambda>A. \<mu> (f ` A)) (\<lambda>x. g x) = (\<integral>x. g (the_inv_into S f x))"
  1620 proof -
  1621   interpret v: measure_space "vimage_algebra S f" "\<lambda>A. \<mu> (f ` A)"
  1622     using f by (rule measure_space_isomorphic)
  1623   have "\<And>x. x \<in> space (vimage_algebra S f) \<Longrightarrow> the_inv_into S f (f x) = x"
  1624     using f[unfolded bij_betw_def] by (simp add: the_inv_into_f_f)
  1625   then have *: "v.positive_integral (\<lambda>x. Real (g (the_inv_into S f (f x)))) = v.positive_integral (\<lambda>x. Real (g x))"
  1626      "v.positive_integral (\<lambda>x. Real (- g (the_inv_into S f (f x)))) = v.positive_integral (\<lambda>x. Real (- g x))"
  1627     by (auto intro!: v.positive_integral_cong)
  1628   show ?thesis
  1629     unfolding integral_def v.integral_def
  1630     unfolding positive_integral_vimage[OF f]
  1631     by (simp add: *)
  1632 qed
  1633 
  1634 lemma (in measure_space) integral_minus[intro, simp]:
  1635   assumes "integrable f"
  1636   shows "integrable (\<lambda>x. - f x)" "(\<integral>x. - f x) = - integral f"
  1637   using assms by (auto simp: integrable_def integral_def)
  1638 
  1639 lemma (in measure_space) integral_of_positive_diff:
  1640   assumes integrable: "integrable u" "integrable v"
  1641   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"
  1642   shows "integrable f" and "integral f = integral u - integral v"
  1643 proof -
  1644   let ?PI = positive_integral
  1645   let "?f x" = "Real (f x)"
  1646   let "?mf x" = "Real (- f x)"
  1647   let "?u x" = "Real (u x)"
  1648   let "?v x" = "Real (v x)"
  1649 
  1650   from borel_measurable_diff[of u v] integrable
  1651   have f_borel: "?f \<in> borel_measurable M" and
  1652     mf_borel: "?mf \<in> borel_measurable M" and
  1653     v_borel: "?v \<in> borel_measurable M" and
  1654     u_borel: "?u \<in> borel_measurable M" and
  1655     "f \<in> borel_measurable M"
  1656     by (auto simp: f_def[symmetric] integrable_def)
  1657 
  1658   have "?PI (\<lambda>x. Real (u x - v x)) \<le> ?PI ?u"
  1659     using pos by (auto intro!: positive_integral_mono)
  1660   moreover have "?PI (\<lambda>x. Real (v x - u x)) \<le> ?PI ?v"
  1661     using pos by (auto intro!: positive_integral_mono)
  1662   ultimately show f: "integrable f"
  1663     using `integrable u` `integrable v` `f \<in> borel_measurable M`
  1664     by (auto simp: integrable_def f_def)
  1665   hence mf: "integrable (\<lambda>x. - f x)" ..
  1666 
  1667   have *: "\<And>x. Real (- v x) = 0" "\<And>x. Real (- u x) = 0"
  1668     using pos by auto
  1669 
  1670   have "\<And>x. ?u x + ?mf x = ?v x + ?f x"
  1671     unfolding f_def using pos by simp
  1672   hence "?PI (\<lambda>x. ?u x + ?mf x) = ?PI (\<lambda>x. ?v x + ?f x)" by simp
  1673   hence "real (?PI ?u + ?PI ?mf) = real (?PI ?v + ?PI ?f)"
  1674     using positive_integral_add[OF u_borel mf_borel]
  1675     using positive_integral_add[OF v_borel f_borel]
  1676     by auto
  1677   then show "integral f = integral u - integral v"
  1678     using f mf `integrable u` `integrable v`
  1679     unfolding integral_def integrable_def *
  1680     by (cases "?PI ?f", cases "?PI ?mf", cases "?PI ?v", cases "?PI ?u")
  1681        (auto simp add: field_simps)
  1682 qed
  1683 
  1684 lemma (in measure_space) integral_linear:
  1685   assumes "integrable f" "integrable g" and "0 \<le> a"
  1686   shows "integrable (\<lambda>t. a * f t + g t)"
  1687   and "(\<integral> t. a * f t + g t) = a * integral f + integral g"
  1688 proof -
  1689   let ?PI = positive_integral
  1690   let "?f x" = "Real (f x)"
  1691   let "?g x" = "Real (g x)"
  1692   let "?mf x" = "Real (- f x)"
  1693   let "?mg x" = "Real (- g x)"
  1694   let "?p t" = "max 0 (a * f t) + max 0 (g t)"
  1695   let "?n t" = "max 0 (- (a * f t)) + max 0 (- g t)"
  1696 
  1697   have pos: "?f \<in> borel_measurable M" "?g \<in> borel_measurable M"
  1698     and neg: "?mf \<in> borel_measurable M" "?mg \<in> borel_measurable M"
  1699     and p: "?p \<in> borel_measurable M"
  1700     and n: "?n \<in> borel_measurable M"
  1701     using assms by (simp_all add: integrable_def)
  1702 
  1703   have *: "\<And>x. Real (?p x) = Real a * ?f x + ?g x"
  1704           "\<And>x. Real (?n x) = Real a * ?mf x + ?mg x"
  1705           "\<And>x. Real (- ?p x) = 0"
  1706           "\<And>x. Real (- ?n x) = 0"
  1707     using `0 \<le> a` by (auto simp: max_def min_def zero_le_mult_iff mult_le_0_iff add_nonpos_nonpos)
  1708 
  1709   note linear =
  1710     positive_integral_linear[OF pos]
  1711     positive_integral_linear[OF neg]
  1712 
  1713   have "integrable ?p" "integrable ?n"
  1714       "\<And>t. a * f t + g t = ?p t - ?n t" "\<And>t. 0 \<le> ?p t" "\<And>t. 0 \<le> ?n t"
  1715     using assms p n unfolding integrable_def * linear by auto
  1716   note diff = integral_of_positive_diff[OF this]
  1717 
  1718   show "integrable (\<lambda>t. a * f t + g t)" by (rule diff)
  1719 
  1720   from assms show "(\<integral> t. a * f t + g t) = a * integral f + integral g"
  1721     unfolding diff(2) unfolding integral_def * linear integrable_def
  1722     by (cases "?PI ?f", cases "?PI ?mf", cases "?PI ?g", cases "?PI ?mg")
  1723        (auto simp add: field_simps zero_le_mult_iff)
  1724 qed
  1725 
  1726 lemma (in measure_space) integral_add[simp, intro]:
  1727   assumes "integrable f" "integrable g"
  1728   shows "integrable (\<lambda>t. f t + g t)"
  1729   and "(\<integral> t. f t + g t) = integral f + integral g"
  1730   using assms integral_linear[where a=1] by auto
  1731 
  1732 lemma (in measure_space) integral_zero[simp, intro]:
  1733   shows "integrable (\<lambda>x. 0)"
  1734   and "(\<integral> x.0) = 0"
  1735   unfolding integrable_def integral_def
  1736   by (auto simp add: borel_measurable_const)
  1737 
  1738 lemma (in measure_space) integral_cmult[simp, intro]:
  1739   assumes "integrable f"
  1740   shows "integrable (\<lambda>t. a * f t)" (is ?P)
  1741   and "(\<integral> t. a * f t) = a * integral f" (is ?I)
  1742 proof -
  1743   have "integrable (\<lambda>t. a * f t) \<and> (\<integral> t. a * f t) = a * integral f"
  1744   proof (cases rule: le_cases)
  1745     assume "0 \<le> a" show ?thesis
  1746       using integral_linear[OF assms integral_zero(1) `0 \<le> a`]
  1747       by (simp add: integral_zero)
  1748   next
  1749     assume "a \<le> 0" hence "0 \<le> - a" by auto
  1750     have *: "\<And>t. - a * t + 0 = (-a) * t" by simp
  1751     show ?thesis using integral_linear[OF assms integral_zero(1) `0 \<le> - a`]
  1752         integral_minus(1)[of "\<lambda>t. - a * f t"]
  1753       unfolding * integral_zero by simp
  1754   qed
  1755   thus ?P ?I by auto
  1756 qed
  1757 
  1758 lemma (in measure_space) integral_multc:
  1759   assumes "integrable f"
  1760   shows "(\<integral> x. f x * c) = integral f * c"
  1761   unfolding mult_commute[of _ c] integral_cmult[OF assms] ..
  1762 
  1763 lemma (in measure_space) integral_mono_AE:
  1764   assumes fg: "integrable f" "integrable g"
  1765   and mono: "AE t. f t \<le> g t"
  1766   shows "integral f \<le> integral g"
  1767 proof -
  1768   have "AE x. Real (f x) \<le> Real (g x)"
  1769     using mono by (rule AE_mp) (auto intro!: AE_cong)
  1770   moreover have "AE x. Real (- g x) \<le> Real (- f x)" 
  1771     using mono by (rule AE_mp) (auto intro!: AE_cong)
  1772   ultimately show ?thesis using fg
  1773     by (auto simp: integral_def integrable_def diff_minus
  1774              intro!: add_mono real_of_pextreal_mono positive_integral_mono_AE)
  1775 qed
  1776 
  1777 lemma (in measure_space) integral_mono:
  1778   assumes fg: "integrable f" "integrable g"
  1779   and mono: "\<And>t. t \<in> space M \<Longrightarrow> f t \<le> g t"
  1780   shows "integral f \<le> integral g"
  1781   apply (rule integral_mono_AE[OF fg])
  1782   using mono by (rule AE_cong) auto
  1783 
  1784 lemma (in measure_space) integral_diff[simp, intro]:
  1785   assumes f: "integrable f" and g: "integrable g"
  1786   shows "integrable (\<lambda>t. f t - g t)"
  1787   and "(\<integral> t. f t - g t) = integral f - integral g"
  1788   using integral_add[OF f integral_minus(1)[OF g]]
  1789   unfolding diff_minus integral_minus(2)[OF g]
  1790   by auto
  1791 
  1792 lemma (in measure_space) integral_indicator[simp, intro]:
  1793   assumes "a \<in> sets M" and "\<mu> a \<noteq> \<omega>"
  1794   shows "integral (indicator a) = real (\<mu> a)" (is ?int)
  1795   and "integrable (indicator a)" (is ?able)
  1796 proof -
  1797   have *:
  1798     "\<And>A x. Real (indicator A x) = indicator A x"
  1799     "\<And>A x. Real (- indicator A x) = 0" unfolding indicator_def by auto
  1800   show ?int ?able
  1801     using assms unfolding integral_def integrable_def
  1802     by (auto simp: * positive_integral_indicator borel_measurable_indicator)
  1803 qed
  1804 
  1805 lemma (in measure_space) integral_cmul_indicator:
  1806   assumes "A \<in> sets M" and "c \<noteq> 0 \<Longrightarrow> \<mu> A \<noteq> \<omega>"
  1807   shows "integrable (\<lambda>x. c * indicator A x)" (is ?P)
  1808   and "(\<integral>x. c * indicator A x) = c * real (\<mu> A)" (is ?I)
  1809 proof -
  1810   show ?P
  1811   proof (cases "c = 0")
  1812     case False with assms show ?thesis by simp
  1813   qed simp
  1814 
  1815   show ?I
  1816   proof (cases "c = 0")
  1817     case False with assms show ?thesis by simp
  1818   qed simp
  1819 qed
  1820 
  1821 lemma (in measure_space) integral_setsum[simp, intro]:
  1822   assumes "\<And>n. n \<in> S \<Longrightarrow> integrable (f n)"
  1823   shows "(\<integral>x. \<Sum> i \<in> S. f i x) = (\<Sum> i \<in> S. integral (f i))" (is "?int S")
  1824     and "integrable (\<lambda>x. \<Sum> i \<in> S. f i x)" (is "?I S")
  1825 proof -
  1826   have "?int S \<and> ?I S"
  1827   proof (cases "finite S")
  1828     assume "finite S"
  1829     from this assms show ?thesis by (induct S) simp_all
  1830   qed simp
  1831   thus "?int S" and "?I S" by auto
  1832 qed
  1833 
  1834 lemma (in measure_space) integrable_abs:
  1835   assumes "integrable f"
  1836   shows "integrable (\<lambda> x. \<bar>f x\<bar>)"
  1837 proof -
  1838   have *:
  1839     "\<And>x. Real \<bar>f x\<bar> = Real (f x) + Real (- f x)"
  1840     "\<And>x. Real (- \<bar>f x\<bar>) = 0" by auto
  1841   have abs: "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M" and
  1842     f: "(\<lambda>x. Real (f x)) \<in> borel_measurable M"
  1843         "(\<lambda>x. Real (- f x)) \<in> borel_measurable M"
  1844     using assms unfolding integrable_def by auto
  1845   from abs assms show ?thesis unfolding integrable_def *
  1846     using positive_integral_linear[OF f, of 1] by simp
  1847 qed
  1848 
  1849 lemma (in measure_space) integral_subalgebra:
  1850   assumes borel: "f \<in> borel_measurable N"
  1851   and N: "sets N \<subseteq> sets M" "space N = space M" and sa: "sigma_algebra N"
  1852   shows "measure_space.integrable N \<mu> f \<longleftrightarrow> integrable f" (is ?P)
  1853     and "measure_space.integral N \<mu> f = integral f" (is ?I)
  1854 proof -
  1855   interpret N: measure_space N \<mu> using measure_space_subalgebra[OF sa N] .
  1856   have "(\<lambda>x. Real (f x)) \<in> borel_measurable N" "(\<lambda>x. Real (- f x)) \<in> borel_measurable N"
  1857     using borel by auto
  1858   note * = this[THEN positive_integral_subalgebra[OF _ N sa]]
  1859   have "f \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable N"
  1860     using assms unfolding measurable_def by auto
  1861   then show ?P ?I unfolding integrable_def N.integrable_def integral_def N.integral_def
  1862     unfolding * by auto
  1863 qed
  1864 
  1865 lemma (in measure_space) integrable_bound:
  1866   assumes "integrable f"
  1867   and f: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
  1868     "\<And>x. x \<in> space M \<Longrightarrow> \<bar>g x\<bar> \<le> f x"
  1869   assumes borel: "g \<in> borel_measurable M"
  1870   shows "integrable g"
  1871 proof -
  1872   have "(\<integral>\<^isup>+ x. Real (g x)) \<le> (\<integral>\<^isup>+ x. Real \<bar>g x\<bar>)"
  1873     by (auto intro!: positive_integral_mono)
  1874   also have "\<dots> \<le> (\<integral>\<^isup>+ x. Real (f x))"
  1875     using f by (auto intro!: positive_integral_mono)
  1876   also have "\<dots> < \<omega>"
  1877     using `integrable f` unfolding integrable_def by (auto simp: pextreal_less_\<omega>)
  1878   finally have pos: "(\<integral>\<^isup>+ x. Real (g x)) < \<omega>" .
  1879 
  1880   have "(\<integral>\<^isup>+ x. Real (- g x)) \<le> (\<integral>\<^isup>+ x. Real (\<bar>g x\<bar>))"
  1881     by (auto intro!: positive_integral_mono)
  1882   also have "\<dots> \<le> (\<integral>\<^isup>+ x. Real (f x))"
  1883     using f by (auto intro!: positive_integral_mono)
  1884   also have "\<dots> < \<omega>"
  1885     using `integrable f` unfolding integrable_def by (auto simp: pextreal_less_\<omega>)
  1886   finally have neg: "(\<integral>\<^isup>+ x. Real (- g x)) < \<omega>" .
  1887 
  1888   from neg pos borel show ?thesis
  1889     unfolding integrable_def by auto
  1890 qed
  1891 
  1892 lemma (in measure_space) integrable_abs_iff:
  1893   "f \<in> borel_measurable M \<Longrightarrow> integrable (\<lambda> x. \<bar>f x\<bar>) \<longleftrightarrow> integrable f"
  1894   by (auto intro!: integrable_bound[where g=f] integrable_abs)
  1895 
  1896 lemma (in measure_space) integrable_max:
  1897   assumes int: "integrable f" "integrable g"
  1898   shows "integrable (\<lambda> x. max (f x) (g x))"
  1899 proof (rule integrable_bound)
  1900   show "integrable (\<lambda>x. \<bar>f x\<bar> + \<bar>g x\<bar>)"
  1901     using int by (simp add: integrable_abs)
  1902   show "(\<lambda>x. max (f x) (g x)) \<in> borel_measurable M"
  1903     using int unfolding integrable_def by auto
  1904 next
  1905   fix x assume "x \<in> space M"
  1906   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>"
  1907     by auto
  1908 qed
  1909 
  1910 lemma (in measure_space) integrable_min:
  1911   assumes int: "integrable f" "integrable g"
  1912   shows "integrable (\<lambda> x. min (f x) (g x))"
  1913 proof (rule integrable_bound)
  1914   show "integrable (\<lambda>x. \<bar>f x\<bar> + \<bar>g x\<bar>)"
  1915     using int by (simp add: integrable_abs)
  1916   show "(\<lambda>x. min (f x) (g x)) \<in> borel_measurable M"
  1917     using int unfolding integrable_def by auto
  1918 next
  1919   fix x assume "x \<in> space M"
  1920   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>"
  1921     by auto
  1922 qed
  1923 
  1924 lemma (in measure_space) integral_triangle_inequality:
  1925   assumes "integrable f"
  1926   shows "\<bar>integral f\<bar> \<le> (\<integral>x. \<bar>f x\<bar>)"
  1927 proof -
  1928   have "\<bar>integral f\<bar> = max (integral f) (- integral f)" by auto
  1929   also have "\<dots> \<le> (\<integral>x. \<bar>f x\<bar>)"
  1930       using assms integral_minus(2)[of f, symmetric]
  1931       by (auto intro!: integral_mono integrable_abs simp del: integral_minus)
  1932   finally show ?thesis .
  1933 qed
  1934 
  1935 lemma (in measure_space) integral_positive:
  1936   assumes "integrable f" "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
  1937   shows "0 \<le> integral f"
  1938 proof -
  1939   have "0 = (\<integral>x. 0)" by (auto simp: integral_zero)
  1940   also have "\<dots> \<le> integral f"
  1941     using assms by (rule integral_mono[OF integral_zero(1)])
  1942   finally show ?thesis .
  1943 qed
  1944 
  1945 lemma (in measure_space) integral_monotone_convergence_pos:
  1946   assumes i: "\<And>i. integrable (f i)" and mono: "\<And>x. mono (\<lambda>n. f n x)"
  1947   and pos: "\<And>x i. 0 \<le> f i x"
  1948   and lim: "\<And>x. (\<lambda>i. f i x) ----> u x"
  1949   and ilim: "(\<lambda>i. integral (f i)) ----> x"
  1950   shows "integrable u"
  1951   and "integral u = x"
  1952 proof -
  1953   { fix x have "0 \<le> u x"
  1954       using mono pos[of 0 x] incseq_le[OF _ lim, of x 0]
  1955       by (simp add: mono_def incseq_def) }
  1956   note pos_u = this
  1957 
  1958   hence [simp]: "\<And>i x. Real (- f i x) = 0" "\<And>x. Real (- u x) = 0"
  1959     using pos by auto
  1960 
  1961   have SUP_F: "\<And>x. (SUP n. Real (f n x)) = Real (u x)"
  1962     using mono pos pos_u lim by (rule SUP_eq_LIMSEQ[THEN iffD2])
  1963 
  1964   have borel_f: "\<And>i. (\<lambda>x. Real (f i x)) \<in> borel_measurable M"
  1965     using i unfolding integrable_def by auto
  1966   hence "(\<lambda>x. SUP i. Real (f i x)) \<in> borel_measurable M"
  1967     by auto
  1968   hence borel_u: "u \<in> borel_measurable M"
  1969     using pos_u by (auto simp: borel_measurable_Real_eq SUP_F)
  1970 
  1971   have integral_eq: "\<And>n. (\<integral>\<^isup>+ x. Real (f n x)) = Real (integral (f n))"
  1972     using i unfolding integral_def integrable_def by (auto simp: Real_real)
  1973 
  1974   have pos_integral: "\<And>n. 0 \<le> integral (f n)"
  1975     using pos i by (auto simp: integral_positive)
  1976   hence "0 \<le> x"
  1977     using LIMSEQ_le_const[OF ilim, of 0] by auto
  1978 
  1979   have "(\<lambda>i. (\<integral>\<^isup>+ x. Real (f i x))) \<up> (\<integral>\<^isup>+ x. Real (u x))"
  1980   proof (rule positive_integral_isoton)
  1981     from SUP_F mono pos
  1982     show "(\<lambda>i x. Real (f i x)) \<up> (\<lambda>x. Real (u x))"
  1983       unfolding isoton_fun_expand by (auto simp: isoton_def mono_def)
  1984   qed (rule borel_f)
  1985   hence pI: "(\<integral>\<^isup>+ x. Real (u x)) =
  1986       (SUP n. (\<integral>\<^isup>+ x. Real (f n x)))"
  1987     unfolding isoton_def by simp
  1988   also have "\<dots> = Real x" unfolding integral_eq
  1989   proof (rule SUP_eq_LIMSEQ[THEN iffD2])
  1990     show "mono (\<lambda>n. integral (f n))"
  1991       using mono i by (auto simp: mono_def intro!: integral_mono)
  1992     show "\<And>n. 0 \<le> integral (f n)" using pos_integral .
  1993     show "0 \<le> x" using `0 \<le> x` .
  1994     show "(\<lambda>n. integral (f n)) ----> x" using ilim .
  1995   qed
  1996   finally show  "integrable u" "integral u = x" using borel_u `0 \<le> x`
  1997     unfolding integrable_def integral_def by auto
  1998 qed
  1999 
  2000 lemma (in measure_space) integral_monotone_convergence:
  2001   assumes f: "\<And>i. integrable (f i)" and "mono f"
  2002   and lim: "\<And>x. (\<lambda>i. f i x) ----> u x"
  2003   and ilim: "(\<lambda>i. integral (f i)) ----> x"
  2004   shows "integrable u"
  2005   and "integral u = x"
  2006 proof -
  2007   have 1: "\<And>i. integrable (\<lambda>x. f i x - f 0 x)"
  2008       using f by (auto intro!: integral_diff)
  2009   have 2: "\<And>x. mono (\<lambda>n. f n x - f 0 x)" using `mono f`
  2010       unfolding mono_def le_fun_def by auto
  2011   have 3: "\<And>x n. 0 \<le> f n x - f 0 x" using `mono f`
  2012       unfolding mono_def le_fun_def by (auto simp: field_simps)
  2013   have 4: "\<And>x. (\<lambda>i. f i x - f 0 x) ----> u x - f 0 x"
  2014     using lim by (auto intro!: LIMSEQ_diff)
  2015   have 5: "(\<lambda>i. (\<integral>x. f i x - f 0 x)) ----> x - integral (f 0)"
  2016     using f ilim by (auto intro!: LIMSEQ_diff simp: integral_diff)
  2017   note diff = integral_monotone_convergence_pos[OF 1 2 3 4 5]
  2018   have "integrable (\<lambda>x. (u x - f 0 x) + f 0 x)"
  2019     using diff(1) f by (rule integral_add(1))
  2020   with diff(2) f show "integrable u" "integral u = x"
  2021     by (auto simp: integral_diff)
  2022 qed
  2023 
  2024 lemma (in measure_space) integral_0_iff:
  2025   assumes "integrable f"
  2026   shows "(\<integral>x. \<bar>f x\<bar>) = 0 \<longleftrightarrow> \<mu> {x\<in>space M. f x \<noteq> 0} = 0"
  2027 proof -
  2028   have *: "\<And>x. Real (- \<bar>f x\<bar>) = 0" by auto
  2029   have "integrable (\<lambda>x. \<bar>f x\<bar>)" using assms by (rule integrable_abs)
  2030   hence "(\<lambda>x. Real (\<bar>f x\<bar>)) \<in> borel_measurable M"
  2031     "(\<integral>\<^isup>+ x. Real \<bar>f x\<bar>) \<noteq> \<omega>" unfolding integrable_def by auto
  2032   from positive_integral_0_iff[OF this(1)] this(2)
  2033   show ?thesis unfolding integral_def *
  2034     by (simp add: real_of_pextreal_eq_0)
  2035 qed
  2036 
  2037 lemma (in measure_space) positive_integral_omega:
  2038   assumes "f \<in> borel_measurable M"
  2039   and "positive_integral f \<noteq> \<omega>"
  2040   shows "\<mu> (f -` {\<omega>} \<inter> space M) = 0"
  2041 proof -
  2042   have "\<omega> * \<mu> (f -` {\<omega>} \<inter> space M) = (\<integral>\<^isup>+ x. \<omega> * indicator (f -` {\<omega>} \<inter> space M) x)"
  2043     using positive_integral_cmult_indicator[OF borel_measurable_vimage, OF assms(1), of \<omega> \<omega>] by simp
  2044   also have "\<dots> \<le> positive_integral f"
  2045     by (auto intro!: positive_integral_mono simp: indicator_def)
  2046   finally show ?thesis
  2047     using assms(2) by (cases ?thesis) auto
  2048 qed
  2049 
  2050 lemma (in measure_space) positive_integral_omega_AE:
  2051   assumes "f \<in> borel_measurable M" "positive_integral f \<noteq> \<omega>" shows "AE x. f x \<noteq> \<omega>"
  2052 proof (rule AE_I)
  2053   show "\<mu> (f -` {\<omega>} \<inter> space M) = 0"
  2054     by (rule positive_integral_omega[OF assms])
  2055   show "f -` {\<omega>} \<inter> space M \<in> sets M"
  2056     using assms by (auto intro: borel_measurable_vimage)
  2057 qed auto
  2058 
  2059 lemma (in measure_space) simple_integral_omega:
  2060   assumes "simple_function f"
  2061   and "simple_integral f \<noteq> \<omega>"
  2062   shows "\<mu> (f -` {\<omega>} \<inter> space M) = 0"
  2063 proof (rule positive_integral_omega)
  2064   show "f \<in> borel_measurable M" using assms by (auto intro: borel_measurable_simple_function)
  2065   show "positive_integral f \<noteq> \<omega>"
  2066     using assms by (simp add: positive_integral_eq_simple_integral)
  2067 qed
  2068 
  2069 lemma (in measure_space) integral_real:
  2070   fixes f :: "'a \<Rightarrow> pextreal"
  2071   assumes "AE x. f x \<noteq> \<omega>"
  2072   shows "(\<integral>x. real (f x)) = real (positive_integral f)" (is ?plus)
  2073     and "(\<integral>x. - real (f x)) = - real (positive_integral f)" (is ?minus)
  2074 proof -
  2075   have "(\<integral>\<^isup>+ x. Real (real (f x))) = positive_integral f"
  2076     apply (rule positive_integral_cong_AE)
  2077     apply (rule AE_mp[OF assms(1)])
  2078     by (auto intro!: AE_cong simp: Real_real)
  2079   moreover
  2080   have "(\<integral>\<^isup>+ x. Real (- real (f x))) = (\<integral>\<^isup>+ x. 0)"
  2081     by (intro positive_integral_cong) auto
  2082   ultimately show ?plus ?minus
  2083     by (auto simp: integral_def integrable_def)
  2084 qed
  2085 
  2086 lemma (in measure_space) integral_dominated_convergence:
  2087   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"
  2088   and w: "integrable w" "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> w x"
  2089   and u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  2090   shows "integrable u'"
  2091   and "(\<lambda>i. (\<integral>x. \<bar>u i x - u' x\<bar>)) ----> 0" (is "?lim_diff")
  2092   and "(\<lambda>i. integral (u i)) ----> integral u'" (is ?lim)
  2093 proof -
  2094   { fix x j assume x: "x \<in> space M"
  2095     from u'[OF x] have "(\<lambda>i. \<bar>u i x\<bar>) ----> \<bar>u' x\<bar>" by (rule LIMSEQ_imp_rabs)
  2096     from LIMSEQ_le_const2[OF this]
  2097     have "\<bar>u' x\<bar> \<le> w x" using bound[OF x] by auto }
  2098   note u'_bound = this
  2099 
  2100   from u[unfolded integrable_def]
  2101   have u'_borel: "u' \<in> borel_measurable M"
  2102     using u' by (blast intro: borel_measurable_LIMSEQ[of u])
  2103 
  2104   show "integrable u'"
  2105   proof (rule integrable_bound)
  2106     show "integrable w" by fact
  2107     show "u' \<in> borel_measurable M" by fact
  2108   next
  2109     fix x assume x: "x \<in> space M"
  2110     thus "0 \<le> w x" by fact
  2111     show "\<bar>u' x\<bar> \<le> w x" using u'_bound[OF x] .
  2112   qed
  2113 
  2114   let "?diff n x" = "2 * w x - \<bar>u n x - u' x\<bar>"
  2115   have diff: "\<And>n. integrable (\<lambda>x. \<bar>u n x - u' x\<bar>)"
  2116     using w u `integrable u'`
  2117     by (auto intro!: integral_add integral_diff integral_cmult integrable_abs)
  2118 
  2119   { fix j x assume x: "x \<in> space M"
  2120     have "\<bar>u j x - u' x\<bar> \<le> \<bar>u j x\<bar> + \<bar>u' x\<bar>" by auto
  2121     also have "\<dots> \<le> w x + w x"
  2122       by (rule add_mono[OF bound[OF x] u'_bound[OF x]])
  2123     finally have "\<bar>u j x - u' x\<bar> \<le> 2 * w x" by simp }
  2124   note diff_less_2w = this
  2125 
  2126   have PI_diff: "\<And>m n. (\<integral>\<^isup>+ x. Real (?diff (m + n) x)) =
  2127     (\<integral>\<^isup>+ x. Real (2 * w x)) - (\<integral>\<^isup>+ x. Real \<bar>u (m + n) x - u' x\<bar>)"
  2128     using diff w diff_less_2w
  2129     by (subst positive_integral_diff[symmetric])
  2130        (auto simp: integrable_def intro!: positive_integral_cong)
  2131 
  2132   have "integrable (\<lambda>x. 2 * w x)"
  2133     using w by (auto intro: integral_cmult)
  2134   hence I2w_fin: "(\<integral>\<^isup>+ x. Real (2 * w x)) \<noteq> \<omega>" and
  2135     borel_2w: "(\<lambda>x. Real (2 * w x)) \<in> borel_measurable M"
  2136     unfolding integrable_def by auto
  2137 
  2138   have "(INF n. SUP m. (\<integral>\<^isup>+ x. Real \<bar>u (m + n) x - u' x\<bar>)) = 0" (is "?lim_SUP = 0")
  2139   proof cases
  2140     assume eq_0: "(\<integral>\<^isup>+ x. Real (2 * w x)) = 0"
  2141     have "\<And>i. (\<integral>\<^isup>+ x. Real \<bar>u i x - u' x\<bar>) \<le> (\<integral>\<^isup>+ x. Real (2 * w x))"
  2142     proof (rule positive_integral_mono)
  2143       fix i x assume "x \<in> space M" from diff_less_2w[OF this, of i]
  2144       show "Real \<bar>u i x - u' x\<bar> \<le> Real (2 * w x)" by auto
  2145     qed
  2146     hence "\<And>i. (\<integral>\<^isup>+ x. Real \<bar>u i x - u' x\<bar>) = 0" using eq_0 by auto
  2147     thus ?thesis by simp
  2148   next
  2149     assume neq_0: "(\<integral>\<^isup>+ x. Real (2 * w x)) \<noteq> 0"
  2150     have "(\<integral>\<^isup>+ x. Real (2 * w x)) = (\<integral>\<^isup>+ x. SUP n. INF m. Real (?diff (m + n) x))"
  2151     proof (rule positive_integral_cong, subst add_commute)
  2152       fix x assume x: "x \<in> space M"
  2153       show "Real (2 * w x) = (SUP n. INF m. Real (?diff (n + m) x))"
  2154       proof (rule LIMSEQ_imp_lim_INF[symmetric])
  2155         fix j show "0 \<le> ?diff j x" using diff_less_2w[OF x, of j] by simp
  2156       next
  2157         have "(\<lambda>i. ?diff i x) ----> 2 * w x - \<bar>u' x - u' x\<bar>"
  2158           using u'[OF x] by (safe intro!: LIMSEQ_diff LIMSEQ_const LIMSEQ_imp_rabs)
  2159         thus "(\<lambda>i. ?diff i x) ----> 2 * w x" by simp
  2160       qed
  2161     qed
  2162     also have "\<dots> \<le> (SUP n. INF m. (\<integral>\<^isup>+ x. Real (?diff (m + n) x)))"
  2163       using u'_borel w u unfolding integrable_def
  2164       by (auto intro!: positive_integral_lim_INF)
  2165     also have "\<dots> = (\<integral>\<^isup>+ x. Real (2 * w x)) -
  2166         (INF n. SUP m. (\<integral>\<^isup>+ x. Real \<bar>u (m + n) x - u' x\<bar>))"
  2167       unfolding PI_diff pextreal_INF_minus[OF I2w_fin] pextreal_SUP_minus ..
  2168     finally show ?thesis using neq_0 I2w_fin by (rule pextreal_le_minus_imp_0)
  2169   qed
  2170 
  2171   have [simp]: "\<And>n m x. Real (- \<bar>u (m + n) x - u' x\<bar>) = 0" by auto
  2172 
  2173   have [simp]: "\<And>n m. (\<integral>\<^isup>+ x. Real \<bar>u (m + n) x - u' x\<bar>) =
  2174     Real ((\<integral>x. \<bar>u (n + m) x - u' x\<bar>))"
  2175     using diff by (subst add_commute) (simp add: integral_def integrable_def Real_real)
  2176 
  2177   have "(SUP n. INF m. (\<integral>\<^isup>+ x. Real \<bar>u (m + n) x - u' x\<bar>)) \<le> ?lim_SUP"
  2178     (is "?lim_INF \<le> _") by (subst (1 2) add_commute) (rule lim_INF_le_lim_SUP)
  2179   hence "?lim_INF = Real 0" "?lim_SUP = Real 0" using `?lim_SUP = 0` by auto
  2180   thus ?lim_diff using diff by (auto intro!: integral_positive lim_INF_eq_lim_SUP)
  2181 
  2182   show ?lim
  2183   proof (rule LIMSEQ_I)
  2184     fix r :: real assume "0 < r"
  2185     from LIMSEQ_D[OF `?lim_diff` this]
  2186     obtain N where N: "\<And>n. N \<le> n \<Longrightarrow> (\<integral>x. \<bar>u n x - u' x\<bar>) < r"
  2187       using diff by (auto simp: integral_positive)
  2188 
  2189     show "\<exists>N. \<forall>n\<ge>N. norm (integral (u n) - integral u') < r"
  2190     proof (safe intro!: exI[of _ N])
  2191       fix n assume "N \<le> n"
  2192       have "\<bar>integral (u n) - integral u'\<bar> = \<bar>(\<integral>x. u n x - u' x)\<bar>"
  2193         using u `integrable u'` by (auto simp: integral_diff)
  2194       also have "\<dots> \<le> (\<integral>x. \<bar>u n x - u' x\<bar>)" using u `integrable u'`
  2195         by (rule_tac integral_triangle_inequality) (auto intro!: integral_diff)
  2196       also note N[OF `N \<le> n`]
  2197       finally show "norm (integral (u n) - integral u') < r" by simp
  2198     qed
  2199   qed
  2200 qed
  2201 
  2202 lemma (in measure_space) integral_sums:
  2203   assumes borel: "\<And>i. integrable (f i)"
  2204   and summable: "\<And>x. x \<in> space M \<Longrightarrow> summable (\<lambda>i. \<bar>f i x\<bar>)"
  2205   and sums: "summable (\<lambda>i. (\<integral>x. \<bar>f i x\<bar>))"
  2206   shows "integrable (\<lambda>x. (\<Sum>i. f i x))" (is "integrable ?S")
  2207   and "(\<lambda>i. integral (f i)) sums (\<integral>x. (\<Sum>i. f i x))" (is ?integral)
  2208 proof -
  2209   have "\<forall>x\<in>space M. \<exists>w. (\<lambda>i. \<bar>f i x\<bar>) sums w"
  2210     using summable unfolding summable_def by auto
  2211   from bchoice[OF this]
  2212   obtain w where w: "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. \<bar>f i x\<bar>) sums w x" by auto
  2213 
  2214   let "?w y" = "if y \<in> space M then w y else 0"
  2215 
  2216   obtain x where abs_sum: "(\<lambda>i. (\<integral>x. \<bar>f i x\<bar>)) sums x"
  2217     using sums unfolding summable_def ..
  2218 
  2219   have 1: "\<And>n. integrable (\<lambda>x. \<Sum>i = 0..<n. f i x)"
  2220     using borel by (auto intro!: integral_setsum)
  2221 
  2222   { fix j x assume [simp]: "x \<in> space M"
  2223     have "\<bar>\<Sum>i = 0..< j. f i x\<bar> \<le> (\<Sum>i = 0..< j. \<bar>f i x\<bar>)" by (rule setsum_abs)
  2224     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
  2225     finally have "\<bar>\<Sum>i = 0..<j. f i x\<bar> \<le> ?w x" by simp }
  2226   note 2 = this
  2227 
  2228   have 3: "integrable ?w"
  2229   proof (rule integral_monotone_convergence(1))
  2230     let "?F n y" = "(\<Sum>i = 0..<n. \<bar>f i y\<bar>)"
  2231     let "?w' n y" = "if y \<in> space M then ?F n y else 0"
  2232     have "\<And>n. integrable (?F n)"
  2233       using borel by (auto intro!: integral_setsum integrable_abs)
  2234     thus "\<And>n. integrable (?w' n)" by (simp cong: integrable_cong)
  2235     show "mono ?w'"
  2236       by (auto simp: mono_def le_fun_def intro!: setsum_mono2)
  2237     { fix x show "(\<lambda>n. ?w' n x) ----> ?w x"
  2238         using w by (cases "x \<in> space M") (simp_all add: LIMSEQ_const sums_def) }
  2239     have *: "\<And>n. integral (?w' n) = (\<Sum>i = 0..< n. (\<integral>x. \<bar>f i x\<bar>))"
  2240       using borel by (simp add: integral_setsum integrable_abs cong: integral_cong)
  2241     from abs_sum
  2242     show "(\<lambda>i. integral (?w' i)) ----> x" unfolding * sums_def .
  2243   qed
  2244 
  2245   have 4: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> ?w x" using 2[of _ 0] by simp
  2246 
  2247   from summable[THEN summable_rabs_cancel]
  2248   have 5: "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>n. \<Sum>i = 0..<n. f i x) ----> (\<Sum>i. f i x)"
  2249     by (auto intro: summable_sumr_LIMSEQ_suminf)
  2250 
  2251   note int = integral_dominated_convergence(1,3)[OF 1 2 3 4 5]
  2252 
  2253   from int show "integrable ?S" by simp
  2254 
  2255   show ?integral unfolding sums_def integral_setsum(1)[symmetric, OF borel]
  2256     using int(2) by simp
  2257 qed
  2258 
  2259 section "Lebesgue integration on countable spaces"
  2260 
  2261 lemma (in measure_space) integral_on_countable:
  2262   assumes f: "f \<in> borel_measurable M"
  2263   and bij: "bij_betw enum S (f ` space M)"
  2264   and enum_zero: "enum ` (-S) \<subseteq> {0}"
  2265   and fin: "\<And>x. x \<noteq> 0 \<Longrightarrow> \<mu> (f -` {x} \<inter> space M) \<noteq> \<omega>"
  2266   and abs_summable: "summable (\<lambda>r. \<bar>enum r * real (\<mu> (f -` {enum r} \<inter> space M))\<bar>)"
  2267   shows "integrable f"
  2268   and "(\<lambda>r. enum r * real (\<mu> (f -` {enum r} \<inter> space M))) sums integral f" (is ?sums)
  2269 proof -
  2270   let "?A r" = "f -` {enum r} \<inter> space M"
  2271   let "?F r x" = "enum r * indicator (?A r) x"
  2272   have enum_eq: "\<And>r. enum r * real (\<mu> (?A r)) = integral (?F r)"
  2273     using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
  2274 
  2275   { fix x assume "x \<in> space M"
  2276     hence "f x \<in> enum ` S" using bij unfolding bij_betw_def by auto
  2277     then obtain i where "i\<in>S" "enum i = f x" by auto
  2278     have F: "\<And>j. ?F j x = (if j = i then f x else 0)"
  2279     proof cases
  2280       fix j assume "j = i"
  2281       thus "?thesis j" using `x \<in> space M` `enum i = f x` by (simp add: indicator_def)
  2282     next
  2283       fix j assume "j \<noteq> i"
  2284       show "?thesis j" using bij `i \<in> S` `j \<noteq> i` `enum i = f x` enum_zero
  2285         by (cases "j \<in> S") (auto simp add: indicator_def bij_betw_def inj_on_def)
  2286     qed
  2287     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
  2288     have "(\<lambda>i. ?F i x) sums f x"
  2289          "(\<lambda>i. \<bar>?F i x\<bar>) sums \<bar>f x\<bar>"
  2290       by (auto intro!: sums_single simp: F F_abs) }
  2291   note F_sums_f = this(1) and F_abs_sums_f = this(2)
  2292 
  2293   have int_f: "integral f = (\<integral>x. \<Sum>r. ?F r x)" "integrable f = integrable (\<lambda>x. \<Sum>r. ?F r x)"
  2294     using F_sums_f by (auto intro!: integral_cong integrable_cong simp: sums_iff)
  2295 
  2296   { fix r
  2297     have "(\<integral>x. \<bar>?F r x\<bar>) = (\<integral>x. \<bar>enum r\<bar> * indicator (?A r) x)"
  2298       by (auto simp: indicator_def intro!: integral_cong)
  2299     also have "\<dots> = \<bar>enum r\<bar> * real (\<mu> (?A r))"
  2300       using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
  2301     finally have "(\<integral>x. \<bar>?F r x\<bar>) = \<bar>enum r * real (\<mu> (?A r))\<bar>"
  2302       by (simp add: abs_mult_pos real_pextreal_pos) }
  2303   note int_abs_F = this
  2304 
  2305   have 1: "\<And>i. integrable (\<lambda>x. ?F i x)"
  2306     using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
  2307 
  2308   have 2: "\<And>x. x \<in> space M \<Longrightarrow> summable (\<lambda>i. \<bar>?F i x\<bar>)"
  2309     using F_abs_sums_f unfolding sums_iff by auto
  2310 
  2311   from integral_sums(2)[OF 1 2, unfolded int_abs_F, OF _ abs_summable]
  2312   show ?sums unfolding enum_eq int_f by simp
  2313 
  2314   from integral_sums(1)[OF 1 2, unfolded int_abs_F, OF _ abs_summable]
  2315   show "integrable f" unfolding int_f by simp
  2316 qed
  2317 
  2318 section "Lebesgue integration on finite space"
  2319 
  2320 lemma (in measure_space) integral_on_finite:
  2321   assumes f: "f \<in> borel_measurable M" and finite: "finite (f`space M)"
  2322   and fin: "\<And>x. x \<noteq> 0 \<Longrightarrow> \<mu> (f -` {x} \<inter> space M) \<noteq> \<omega>"
  2323   shows "integrable f"
  2324   and "(\<integral>x. f x) =
  2325     (\<Sum> r \<in> f`space M. r * real (\<mu> (f -` {r} \<inter> space M)))" (is "?integral")
  2326 proof -
  2327   let "?A r" = "f -` {r} \<inter> space M"
  2328   let "?S x" = "\<Sum>r\<in>f`space M. r * indicator (?A r) x"
  2329 
  2330   { fix x assume "x \<in> space M"
  2331     have "f x = (\<Sum>r\<in>f`space M. if x \<in> ?A r then r else 0)"
  2332       using finite `x \<in> space M` by (simp add: setsum_cases)
  2333     also have "\<dots> = ?S x"
  2334       by (auto intro!: setsum_cong)
  2335     finally have "f x = ?S x" . }
  2336   note f_eq = this
  2337 
  2338   have f_eq_S: "integrable f \<longleftrightarrow> integrable ?S" "integral f = integral ?S"
  2339     by (auto intro!: integrable_cong integral_cong simp only: f_eq)
  2340 
  2341   show "integrable f" ?integral using fin f f_eq_S
  2342     by (simp_all add: integral_cmul_indicator borel_measurable_vimage)
  2343 qed
  2344 
  2345 lemma (in finite_measure_space) simple_function_finite[simp, intro]: "simple_function f"
  2346   unfolding simple_function_def using finite_space by auto
  2347 
  2348 lemma (in finite_measure_space) borel_measurable_finite[intro, simp]: "f \<in> borel_measurable M"
  2349   by (auto intro: borel_measurable_simple_function)
  2350 
  2351 lemma (in finite_measure_space) positive_integral_finite_eq_setsum:
  2352   "positive_integral f = (\<Sum>x \<in> space M. f x * \<mu> {x})"
  2353 proof -
  2354   have *: "positive_integral f = (\<integral>\<^isup>+ x. \<Sum>y\<in>space M. f y * indicator {y} x)"
  2355     by (auto intro!: positive_integral_cong simp add: indicator_def if_distrib setsum_cases[OF finite_space])
  2356   show ?thesis unfolding * using borel_measurable_finite[of f]
  2357     by (simp add: positive_integral_setsum positive_integral_cmult_indicator)
  2358 qed
  2359 
  2360 lemma (in finite_measure_space) integral_finite_singleton:
  2361   shows "integrable f"
  2362   and "integral f = (\<Sum>x \<in> space M. f x * real (\<mu> {x}))" (is ?I)
  2363 proof -
  2364   have [simp]:
  2365     "(\<integral>\<^isup>+ x. Real (f x)) = (\<Sum>x \<in> space M. Real (f x) * \<mu> {x})"
  2366     "(\<integral>\<^isup>+ x. Real (- f x)) = (\<Sum>x \<in> space M. Real (- f x) * \<mu> {x})"
  2367     unfolding positive_integral_finite_eq_setsum by auto
  2368   show "integrable f" using finite_space finite_measure
  2369     by (simp add: setsum_\<omega> integrable_def)
  2370   show ?I using finite_measure
  2371     apply (simp add: integral_def real_of_pextreal_setsum[symmetric]
  2372       real_of_pextreal_mult[symmetric] setsum_subtractf[symmetric])
  2373     by (rule setsum_cong) (simp_all split: split_if)
  2374 qed
  2375 
  2376 end