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