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