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