src/HOL/Probability/Lebesgue_Integration.thy
changeset 47694 05663f75964c
parent 46905 6b1c0a80a57a
child 47761 dfe747e72fa8
equal deleted inserted replaced
47693:64023cf4d148 47694:05663f75964c
     4 *)
     4 *)
     5 
     5 
     6 header {*Lebesgue Integration*}
     6 header {*Lebesgue Integration*}
     7 
     7 
     8 theory Lebesgue_Integration
     8 theory Lebesgue_Integration
     9   imports Measure Borel_Space
     9   imports Measure_Space Borel_Space
    10 begin
    10 begin
       
    11 
       
    12 lemma ereal_minus_eq_PInfty_iff:
       
    13   fixes x y :: ereal shows "x - y = \<infinity> \<longleftrightarrow> y = -\<infinity> \<or> x = \<infinity>"
       
    14   by (cases x y rule: ereal2_cases) simp_all
    11 
    15 
    12 lemma real_ereal_1[simp]: "real (1::ereal) = 1"
    16 lemma real_ereal_1[simp]: "real (1::ereal) = 1"
    13   unfolding one_ereal_def by simp
    17   unfolding one_ereal_def by simp
    14 
    18 
    15 lemma ereal_indicator_pos[simp,intro]: "0 \<le> (indicator A x ::ereal)"
    19 lemma ereal_indicator_pos[simp,intro]: "0 \<le> (indicator A x ::ereal)"
    26   show ?thesis
    30   show ?thesis
    27     unfolding *
    31     unfolding *
    28     by (intro tendsto_add assms tendsto_divide tendsto_norm tendsto_diff) auto
    32     by (intro tendsto_add assms tendsto_divide tendsto_norm tendsto_diff) auto
    29 qed
    33 qed
    30 
    34 
    31 lemma (in measure_space) measure_Union:
    35 lemma measure_Union:
    32   assumes "finite S" "S \<subseteq> sets M" "\<And>A B. A \<in> S \<Longrightarrow> B \<in> S \<Longrightarrow> A \<noteq> B \<Longrightarrow> A \<inter> B = {}"
    36   assumes "finite S" "S \<subseteq> sets M" "\<And>A B. A \<in> S \<Longrightarrow> B \<in> S \<Longrightarrow> A \<noteq> B \<Longrightarrow> A \<inter> B = {}"
    33   shows "setsum \<mu> S = \<mu> (\<Union>S)"
    37   shows "setsum (emeasure M) S = (emeasure M) (\<Union>S)"
    34 proof -
    38 proof -
    35   have "setsum \<mu> S = \<mu> (\<Union>i\<in>S. i)"
    39   have "setsum (emeasure M) S = (emeasure M) (\<Union>i\<in>S. i)"
    36     using assms by (intro measure_setsum[OF `finite S`]) (auto simp: disjoint_family_on_def)
    40     using assms by (intro setsum_emeasure[OF _ _ `finite S`]) (auto simp: disjoint_family_on_def)
    37   also have "\<dots> = \<mu> (\<Union>S)" by (auto intro!: arg_cong[where f=\<mu>])
    41   also have "\<dots> = (emeasure M) (\<Union>S)" by (auto intro!: arg_cong[where f="emeasure M"])
    38   finally show ?thesis .
    42   finally show ?thesis .
    39 qed
    43 qed
    40 
    44 
    41 lemma (in sigma_algebra) measurable_sets2[intro]:
    45 lemma measurable_sets2[intro]:
    42   assumes "f \<in> measurable M M'" "g \<in> measurable M M''"
    46   assumes "f \<in> measurable M M'" "g \<in> measurable M M''"
    43   and "A \<in> sets M'" "B \<in> sets M''"
    47   and "A \<in> sets M'" "B \<in> sets M''"
    44   shows "f -` A \<inter> g -` B \<inter> space M \<in> sets M"
    48   shows "f -` A \<inter> g -` B \<inter> space M \<in> sets M"
    45 proof -
    49 proof -
    46   have "f -` A \<inter> g -` B \<inter> space M = (f -` A \<inter> space M) \<inter> (g -` B \<inter> space M)"
    50   have "f -` A \<inter> g -` B \<inter> space M = (f -` A \<inter> space M) \<inter> (g -` B \<inter> space M)"
    53   assume "\<forall>n. f n \<le> f (Suc n)" then show "incseq f" by (auto intro!: incseq_SucI)
    57   assume "\<forall>n. f n \<le> f (Suc n)" then show "incseq f" by (auto intro!: incseq_SucI)
    54 qed (auto simp: incseq_def)
    58 qed (auto simp: incseq_def)
    55 
    59 
    56 lemma borel_measurable_real_floor:
    60 lemma borel_measurable_real_floor:
    57   "(\<lambda>x::real. real \<lfloor>x\<rfloor>) \<in> borel_measurable borel"
    61   "(\<lambda>x::real. real \<lfloor>x\<rfloor>) \<in> borel_measurable borel"
    58   unfolding borel.borel_measurable_iff_ge
    62   unfolding borel_measurable_iff_ge
    59 proof (intro allI)
    63 proof (intro allI)
    60   fix a :: real
    64   fix a :: real
    61   { fix x have "a \<le> real \<lfloor>x\<rfloor> \<longleftrightarrow> real \<lceil>a\<rceil> \<le> x"
    65   { fix x have "a \<le> real \<lfloor>x\<rfloor> \<longleftrightarrow> real \<lceil>a\<rceil> \<le> x"
    62       using le_floor_eq[of "\<lceil>a\<rceil>" x] ceiling_le_iff[of a "\<lfloor>x\<rfloor>"]
    66       using le_floor_eq[of "\<lceil>a\<rceil>" x] ceiling_le_iff[of a "\<lfloor>x\<rfloor>"]
    63       unfolding real_eq_of_int by simp }
    67       unfolding real_eq_of_int by simp }
    64   then have "{w::real \<in> space borel. a \<le> real \<lfloor>w\<rfloor>} = {real \<lceil>a\<rceil>..}" by auto
    68   then have "{w::real \<in> space borel. a \<le> real \<lfloor>w\<rfloor>} = {real \<lceil>a\<rceil>..}" by auto
    65   then show "{w::real \<in> space borel. a \<le> real \<lfloor>w\<rfloor>} \<in> sets borel" by auto
    69   then show "{w::real \<in> space borel. a \<le> real \<lfloor>w\<rfloor>} \<in> sets borel" by auto
    66 qed
    70 qed
    67 
    71 
    68 lemma measure_preservingD2:
    72 lemma borel_measurable_real_natfloor[intro, simp]:
    69   "f \<in> measure_preserving A B \<Longrightarrow> f \<in> measurable A B"
       
    70   unfolding measure_preserving_def by auto
       
    71 
       
    72 lemma measure_preservingD3:
       
    73   "f \<in> measure_preserving A B \<Longrightarrow> f \<in> space A \<rightarrow> space B"
       
    74   unfolding measure_preserving_def measurable_def by auto
       
    75 
       
    76 lemma measure_preservingD:
       
    77   "T \<in> measure_preserving A B \<Longrightarrow> X \<in> sets B \<Longrightarrow> measure A (T -` X \<inter> space A) = measure B X"
       
    78   unfolding measure_preserving_def by auto
       
    79 
       
    80 lemma (in sigma_algebra) borel_measurable_real_natfloor[intro, simp]:
       
    81   assumes "f \<in> borel_measurable M"
    73   assumes "f \<in> borel_measurable M"
    82   shows "(\<lambda>x. real (natfloor (f x))) \<in> borel_measurable M"
    74   shows "(\<lambda>x. real (natfloor (f x))) \<in> borel_measurable M"
    83 proof -
    75 proof -
    84   have "\<And>x. real (natfloor (f x)) = max 0 (real \<lfloor>f x\<rfloor>)"
    76   have "\<And>x. real (natfloor (f x)) = max 0 (real \<lfloor>f x\<rfloor>)"
    85     by (auto simp: max_def natfloor_def)
    77     by (auto simp: max_def natfloor_def)
    86   with borel_measurable_max[OF measurable_comp[OF assms borel_measurable_real_floor] borel_measurable_const]
    78   with borel_measurable_max[OF measurable_comp[OF assms borel_measurable_real_floor] borel_measurable_const]
    87   show ?thesis by (simp add: comp_def)
    79   show ?thesis by (simp add: comp_def)
    88 qed
    80 qed
    89 
    81 
    90 lemma (in measure_space) AE_not_in:
    82 lemma AE_not_in:
    91   assumes N: "N \<in> null_sets" shows "AE x. x \<notin> N"
    83   assumes N: "N \<in> null_sets M" shows "AE x in M. x \<notin> N"
    92   using N by (rule AE_I') auto
    84   using N by (rule AE_I') auto
    93 
    85 
    94 lemma sums_If_finite:
    86 lemma sums_If_finite:
    95   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
    87   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
    96   assumes finite: "finite {r. P r}"
    88   assumes finite: "finite {r. P r}"
   126 
   118 
   127 definition "simple_function M g \<longleftrightarrow>
   119 definition "simple_function M g \<longleftrightarrow>
   128     finite (g ` space M) \<and>
   120     finite (g ` space M) \<and>
   129     (\<forall>x \<in> g ` space M. g -` {x} \<inter> space M \<in> sets M)"
   121     (\<forall>x \<in> g ` space M. g -` {x} \<inter> space M \<in> sets M)"
   130 
   122 
   131 lemma (in sigma_algebra) simple_functionD:
   123 lemma simple_functionD:
   132   assumes "simple_function M g"
   124   assumes "simple_function M g"
   133   shows "finite (g ` space M)" and "g -` X \<inter> space M \<in> sets M"
   125   shows "finite (g ` space M)" and "g -` X \<inter> space M \<in> sets M"
   134 proof -
   126 proof -
   135   show "finite (g ` space M)"
   127   show "finite (g ` space M)"
   136     using assms unfolding simple_function_def by auto
   128     using assms unfolding simple_function_def by auto
   138   also have "\<dots> = (\<Union>x\<in>X \<inter> g`space M. g-`{x} \<inter> space M)" by auto
   130   also have "\<dots> = (\<Union>x\<in>X \<inter> g`space M. g-`{x} \<inter> space M)" by auto
   139   finally show "g -` X \<inter> space M \<in> sets M" using assms
   131   finally show "g -` X \<inter> space M \<in> sets M" using assms
   140     by (auto intro!: finite_UN simp del: UN_simps simp: simple_function_def)
   132     by (auto intro!: finite_UN simp del: UN_simps simp: simple_function_def)
   141 qed
   133 qed
   142 
   134 
   143 lemma (in sigma_algebra) simple_function_measurable2[intro]:
   135 lemma simple_function_measurable2[intro]:
   144   assumes "simple_function M f" "simple_function M g"
   136   assumes "simple_function M f" "simple_function M g"
   145   shows "f -` A \<inter> g -` B \<inter> space M \<in> sets M"
   137   shows "f -` A \<inter> g -` B \<inter> space M \<in> sets M"
   146 proof -
   138 proof -
   147   have "f -` A \<inter> g -` B \<inter> space M = (f -` A \<inter> space M) \<inter> (g -` B \<inter> space M)"
   139   have "f -` A \<inter> g -` B \<inter> space M = (f -` A \<inter> space M) \<inter> (g -` B \<inter> space M)"
   148     by auto
   140     by auto
   149   then show ?thesis using assms[THEN simple_functionD(2)] by auto
   141   then show ?thesis using assms[THEN simple_functionD(2)] by auto
   150 qed
   142 qed
   151 
   143 
   152 lemma (in sigma_algebra) simple_function_indicator_representation:
   144 lemma simple_function_indicator_representation:
   153   fixes f ::"'a \<Rightarrow> ereal"
   145   fixes f ::"'a \<Rightarrow> ereal"
   154   assumes f: "simple_function M f" and x: "x \<in> space M"
   146   assumes f: "simple_function M f" and x: "x \<in> space M"
   155   shows "f x = (\<Sum>y \<in> f ` space M. y * indicator (f -` {y} \<inter> space M) x)"
   147   shows "f x = (\<Sum>y \<in> f ` space M. y * indicator (f -` {y} \<inter> space M) x)"
   156   (is "?l = ?r")
   148   (is "?l = ?r")
   157 proof -
   149 proof -
   162     using assms by (auto dest: simple_functionD simp: setsum_delta)
   154     using assms by (auto dest: simple_functionD simp: setsum_delta)
   163   also have "... = f x" using x by (auto simp: indicator_def)
   155   also have "... = f x" using x by (auto simp: indicator_def)
   164   finally show ?thesis by auto
   156   finally show ?thesis by auto
   165 qed
   157 qed
   166 
   158 
   167 lemma (in measure_space) simple_function_notspace:
   159 lemma simple_function_notspace:
   168   "simple_function M (\<lambda>x. h x * indicator (- space M) x::ereal)" (is "simple_function M ?h")
   160   "simple_function M (\<lambda>x. h x * indicator (- space M) x::ereal)" (is "simple_function M ?h")
   169 proof -
   161 proof -
   170   have "?h ` space M \<subseteq> {0}" unfolding indicator_def by auto
   162   have "?h ` space M \<subseteq> {0}" unfolding indicator_def by auto
   171   hence [simp, intro]: "finite (?h ` space M)" by (auto intro: finite_subset)
   163   hence [simp, intro]: "finite (?h ` space M)" by (auto intro: finite_subset)
   172   have "?h -` {0} \<inter> space M = space M" by auto
   164   have "?h -` {0} \<inter> space M = space M" by auto
   173   thus ?thesis unfolding simple_function_def by auto
   165   thus ?thesis unfolding simple_function_def by auto
   174 qed
   166 qed
   175 
   167 
   176 lemma (in sigma_algebra) simple_function_cong:
   168 lemma simple_function_cong:
   177   assumes "\<And>t. t \<in> space M \<Longrightarrow> f t = g t"
   169   assumes "\<And>t. t \<in> space M \<Longrightarrow> f t = g t"
   178   shows "simple_function M f \<longleftrightarrow> simple_function M g"
   170   shows "simple_function M f \<longleftrightarrow> simple_function M g"
   179 proof -
   171 proof -
   180   have "f ` space M = g ` space M"
   172   have "f ` space M = g ` space M"
   181     "\<And>x. f -` {x} \<inter> space M = g -` {x} \<inter> space M"
   173     "\<And>x. f -` {x} \<inter> space M = g -` {x} \<inter> space M"
   182     using assms by (auto intro!: image_eqI)
   174     using assms by (auto intro!: image_eqI)
   183   thus ?thesis unfolding simple_function_def using assms by simp
   175   thus ?thesis unfolding simple_function_def using assms by simp
   184 qed
   176 qed
   185 
   177 
   186 lemma (in sigma_algebra) simple_function_cong_algebra:
   178 lemma simple_function_cong_algebra:
   187   assumes "sets N = sets M" "space N = space M"
   179   assumes "sets N = sets M" "space N = space M"
   188   shows "simple_function M f \<longleftrightarrow> simple_function N f"
   180   shows "simple_function M f \<longleftrightarrow> simple_function N f"
   189   unfolding simple_function_def assms ..
   181   unfolding simple_function_def assms ..
   190 
   182 
   191 lemma (in sigma_algebra) borel_measurable_simple_function:
   183 lemma borel_measurable_simple_function:
   192   assumes "simple_function M f"
   184   assumes "simple_function M f"
   193   shows "f \<in> borel_measurable M"
   185   shows "f \<in> borel_measurable M"
   194 proof (rule borel_measurableI)
   186 proof (rule borel_measurableI)
   195   fix S
   187   fix S
   196   let ?I = "f ` (f -` S \<inter> space M)"
   188   let ?I = "f ` (f -` S \<inter> space M)"
   202     apply (rule finite_UN)
   194     apply (rule finite_UN)
   203     using assms unfolding simple_function_def by auto
   195     using assms unfolding simple_function_def by auto
   204   thus "f -` S \<inter> space M \<in> sets M" unfolding * .
   196   thus "f -` S \<inter> space M \<in> sets M" unfolding * .
   205 qed
   197 qed
   206 
   198 
   207 lemma (in sigma_algebra) simple_function_borel_measurable:
   199 lemma simple_function_borel_measurable:
   208   fixes f :: "'a \<Rightarrow> 'x::{t2_space}"
   200   fixes f :: "'a \<Rightarrow> 'x::{t2_space}"
   209   assumes "f \<in> borel_measurable M" and "finite (f ` space M)"
   201   assumes "f \<in> borel_measurable M" and "finite (f ` space M)"
   210   shows "simple_function M f"
   202   shows "simple_function M f"
   211   using assms unfolding simple_function_def
   203   using assms unfolding simple_function_def
   212   by (auto intro: borel_measurable_vimage)
   204   by (auto intro: borel_measurable_vimage)
   213 
   205 
   214 lemma (in sigma_algebra) simple_function_eq_borel_measurable:
   206 lemma simple_function_eq_borel_measurable:
   215   fixes f :: "'a \<Rightarrow> ereal"
   207   fixes f :: "'a \<Rightarrow> ereal"
   216   shows "simple_function M f \<longleftrightarrow> finite (f`space M) \<and> f \<in> borel_measurable M"
   208   shows "simple_function M f \<longleftrightarrow> finite (f`space M) \<and> f \<in> borel_measurable M"
   217   using simple_function_borel_measurable[of f]
   209   using simple_function_borel_measurable[of f] borel_measurable_simple_function[of M f]
   218     borel_measurable_simple_function[of f]
       
   219   by (fastforce simp: simple_function_def)
   210   by (fastforce simp: simple_function_def)
   220 
   211 
   221 lemma (in sigma_algebra) simple_function_const[intro, simp]:
   212 lemma simple_function_const[intro, simp]:
   222   "simple_function M (\<lambda>x. c)"
   213   "simple_function M (\<lambda>x. c)"
   223   by (auto intro: finite_subset simp: simple_function_def)
   214   by (auto intro: finite_subset simp: simple_function_def)
   224 lemma (in sigma_algebra) simple_function_compose[intro, simp]:
   215 lemma simple_function_compose[intro, simp]:
   225   assumes "simple_function M f"
   216   assumes "simple_function M f"
   226   shows "simple_function M (g \<circ> f)"
   217   shows "simple_function M (g \<circ> f)"
   227   unfolding simple_function_def
   218   unfolding simple_function_def
   228 proof safe
   219 proof safe
   229   show "finite ((g \<circ> f) ` space M)"
   220   show "finite ((g \<circ> f) ` space M)"
   236   show "(g \<circ> f) -` {(g \<circ> f) x} \<inter> space M \<in> sets M"
   227   show "(g \<circ> f) -` {(g \<circ> f) x} \<inter> space M \<in> sets M"
   237     using assms unfolding simple_function_def *
   228     using assms unfolding simple_function_def *
   238     by (rule_tac finite_UN) (auto intro!: finite_UN)
   229     by (rule_tac finite_UN) (auto intro!: finite_UN)
   239 qed
   230 qed
   240 
   231 
   241 lemma (in sigma_algebra) simple_function_indicator[intro, simp]:
   232 lemma simple_function_indicator[intro, simp]:
   242   assumes "A \<in> sets M"
   233   assumes "A \<in> sets M"
   243   shows "simple_function M (indicator A)"
   234   shows "simple_function M (indicator A)"
   244 proof -
   235 proof -
   245   have "indicator A ` space M \<subseteq> {0, 1}" (is "?S \<subseteq> _")
   236   have "indicator A ` space M \<subseteq> {0, 1}" (is "?S \<subseteq> _")
   246     by (auto simp: indicator_def)
   237     by (auto simp: indicator_def)
   248   moreover have "- A \<inter> space M = space M - A" by auto
   239   moreover have "- A \<inter> space M = space M - A" by auto
   249   ultimately show ?thesis unfolding simple_function_def
   240   ultimately show ?thesis unfolding simple_function_def
   250     using assms by (auto simp: indicator_def [abs_def])
   241     using assms by (auto simp: indicator_def [abs_def])
   251 qed
   242 qed
   252 
   243 
   253 lemma (in sigma_algebra) simple_function_Pair[intro, simp]:
   244 lemma simple_function_Pair[intro, simp]:
   254   assumes "simple_function M f"
   245   assumes "simple_function M f"
   255   assumes "simple_function M g"
   246   assumes "simple_function M g"
   256   shows "simple_function M (\<lambda>x. (f x, g x))" (is "simple_function M ?p")
   247   shows "simple_function M (\<lambda>x. (f x, g x))" (is "simple_function M ?p")
   257   unfolding simple_function_def
   248   unfolding simple_function_def
   258 proof safe
   249 proof safe
   266     by auto
   257     by auto
   267   with `x \<in> space M` show "(\<lambda>x. (f x, g x)) -` {(f x, g x)} \<inter> space M \<in> sets M"
   258   with `x \<in> space M` show "(\<lambda>x. (f x, g x)) -` {(f x, g x)} \<inter> space M \<in> sets M"
   268     using assms unfolding simple_function_def by auto
   259     using assms unfolding simple_function_def by auto
   269 qed
   260 qed
   270 
   261 
   271 lemma (in sigma_algebra) simple_function_compose1:
   262 lemma simple_function_compose1:
   272   assumes "simple_function M f"
   263   assumes "simple_function M f"
   273   shows "simple_function M (\<lambda>x. g (f x))"
   264   shows "simple_function M (\<lambda>x. g (f x))"
   274   using simple_function_compose[OF assms, of g]
   265   using simple_function_compose[OF assms, of g]
   275   by (simp add: comp_def)
   266   by (simp add: comp_def)
   276 
   267 
   277 lemma (in sigma_algebra) simple_function_compose2:
   268 lemma simple_function_compose2:
   278   assumes "simple_function M f" and "simple_function M g"
   269   assumes "simple_function M f" and "simple_function M g"
   279   shows "simple_function M (\<lambda>x. h (f x) (g x))"
   270   shows "simple_function M (\<lambda>x. h (f x) (g x))"
   280 proof -
   271 proof -
   281   have "simple_function M ((\<lambda>(x, y). h x y) \<circ> (\<lambda>x. (f x, g x)))"
   272   have "simple_function M ((\<lambda>(x, y). h x y) \<circ> (\<lambda>x. (f x, g x)))"
   282     using assms by auto
   273     using assms by auto
   283   thus ?thesis by (simp_all add: comp_def)
   274   thus ?thesis by (simp_all add: comp_def)
   284 qed
   275 qed
   285 
   276 
   286 lemmas (in sigma_algebra) simple_function_add[intro, simp] = simple_function_compose2[where h="op +"]
   277 lemmas simple_function_add[intro, simp] = simple_function_compose2[where h="op +"]
   287   and simple_function_diff[intro, simp] = simple_function_compose2[where h="op -"]
   278   and simple_function_diff[intro, simp] = simple_function_compose2[where h="op -"]
   288   and simple_function_uminus[intro, simp] = simple_function_compose[where g="uminus"]
   279   and simple_function_uminus[intro, simp] = simple_function_compose[where g="uminus"]
   289   and simple_function_mult[intro, simp] = simple_function_compose2[where h="op *"]
   280   and simple_function_mult[intro, simp] = simple_function_compose2[where h="op *"]
   290   and simple_function_div[intro, simp] = simple_function_compose2[where h="op /"]
   281   and simple_function_div[intro, simp] = simple_function_compose2[where h="op /"]
   291   and simple_function_inverse[intro, simp] = simple_function_compose[where g="inverse"]
   282   and simple_function_inverse[intro, simp] = simple_function_compose[where g="inverse"]
   292   and simple_function_max[intro, simp] = simple_function_compose2[where h=max]
   283   and simple_function_max[intro, simp] = simple_function_compose2[where h=max]
   293 
   284 
   294 lemma (in sigma_algebra) simple_function_setsum[intro, simp]:
   285 lemma simple_function_setsum[intro, simp]:
   295   assumes "\<And>i. i \<in> P \<Longrightarrow> simple_function M (f i)"
   286   assumes "\<And>i. i \<in> P \<Longrightarrow> simple_function M (f i)"
   296   shows "simple_function M (\<lambda>x. \<Sum>i\<in>P. f i x)"
   287   shows "simple_function M (\<lambda>x. \<Sum>i\<in>P. f i x)"
   297 proof cases
   288 proof cases
   298   assume "finite P" from this assms show ?thesis by induct auto
   289   assume "finite P" from this assms show ?thesis by induct auto
   299 qed auto
   290 qed auto
   300 
   291 
   301 lemma (in sigma_algebra)
   292 lemma
   302   fixes f g :: "'a \<Rightarrow> real" assumes sf: "simple_function M f"
   293   fixes f g :: "'a \<Rightarrow> real" assumes sf: "simple_function M f"
   303   shows simple_function_ereal[intro, simp]: "simple_function M (\<lambda>x. ereal (f x))"
   294   shows simple_function_ereal[intro, simp]: "simple_function M (\<lambda>x. ereal (f x))"
   304   by (auto intro!: simple_function_compose1[OF sf])
   295   by (auto intro!: simple_function_compose1[OF sf])
   305 
   296 
   306 lemma (in sigma_algebra)
   297 lemma
   307   fixes f g :: "'a \<Rightarrow> nat" assumes sf: "simple_function M f"
   298   fixes f g :: "'a \<Rightarrow> nat" assumes sf: "simple_function M f"
   308   shows simple_function_real_of_nat[intro, simp]: "simple_function M (\<lambda>x. real (f x))"
   299   shows simple_function_real_of_nat[intro, simp]: "simple_function M (\<lambda>x. real (f x))"
   309   by (auto intro!: simple_function_compose1[OF sf])
   300   by (auto intro!: simple_function_compose1[OF sf])
   310 
   301 
   311 lemma (in sigma_algebra) borel_measurable_implies_simple_function_sequence:
   302 lemma borel_measurable_implies_simple_function_sequence:
   312   fixes u :: "'a \<Rightarrow> ereal"
   303   fixes u :: "'a \<Rightarrow> ereal"
   313   assumes u: "u \<in> borel_measurable M"
   304   assumes u: "u \<in> borel_measurable M"
   314   shows "\<exists>f. incseq f \<and> (\<forall>i. \<infinity> \<notin> range (f i) \<and> simple_function M (f i)) \<and>
   305   shows "\<exists>f. incseq f \<and> (\<forall>i. \<infinity> \<notin> range (f i) \<and> simple_function M (f i)) \<and>
   315              (\<forall>x. (SUP i. f i x) = max 0 (u x)) \<and> (\<forall>i x. 0 \<le> f i x)"
   306              (\<forall>x. (SUP i. f i x) = max 0 (u x)) \<and> (\<forall>i x. 0 \<le> f i x)"
   316 proof -
   307 proof -
   414       qed (insert `0 \<le> y`, auto)
   405       qed (insert `0 \<le> y`, auto)
   415     qed
   406     qed
   416   qed (auto simp: divide_nonneg_pos)
   407   qed (auto simp: divide_nonneg_pos)
   417 qed
   408 qed
   418 
   409 
   419 lemma (in sigma_algebra) borel_measurable_implies_simple_function_sequence':
   410 lemma borel_measurable_implies_simple_function_sequence':
   420   fixes u :: "'a \<Rightarrow> ereal"
   411   fixes u :: "'a \<Rightarrow> ereal"
   421   assumes u: "u \<in> borel_measurable M"
   412   assumes u: "u \<in> borel_measurable M"
   422   obtains f where "\<And>i. simple_function M (f i)" "incseq f" "\<And>i. \<infinity> \<notin> range (f i)"
   413   obtains f where "\<And>i. simple_function M (f i)" "incseq f" "\<And>i. \<infinity> \<notin> range (f i)"
   423     "\<And>x. (SUP i. f i x) = max 0 (u x)" "\<And>i x. 0 \<le> f i x"
   414     "\<And>x. (SUP i. f i x) = max 0 (u x)" "\<And>i x. 0 \<le> f i x"
   424   using borel_measurable_implies_simple_function_sequence[OF u] by auto
   415   using borel_measurable_implies_simple_function_sequence[OF u] by auto
   425 
   416 
   426 lemma (in sigma_algebra) simple_function_If_set:
   417 lemma simple_function_If_set:
   427   assumes sf: "simple_function M f" "simple_function M g" and A: "A \<inter> space M \<in> sets M"
   418   assumes sf: "simple_function M f" "simple_function M g" and A: "A \<inter> space M \<in> sets M"
   428   shows "simple_function M (\<lambda>x. if x \<in> A then f x else g x)" (is "simple_function M ?IF")
   419   shows "simple_function M (\<lambda>x. if x \<in> A then f x else g x)" (is "simple_function M ?IF")
   429 proof -
   420 proof -
   430   def F \<equiv> "\<lambda>x. f -` {x} \<inter> space M" and G \<equiv> "\<lambda>x. g -` {x} \<inter> space M"
   421   def F \<equiv> "\<lambda>x. f -` {x} \<inter> space M" and G \<equiv> "\<lambda>x. g -` {x} \<inter> space M"
   431   show ?thesis unfolding simple_function_def
   422   show ?thesis unfolding simple_function_def
   443       unfolding F_def G_def using sf[THEN simple_functionD(2)] by auto
   434       unfolding F_def G_def using sf[THEN simple_functionD(2)] by auto
   444     show "?IF -` {?IF x} \<inter> space M \<in> sets M" unfolding * using A by auto
   435     show "?IF -` {?IF x} \<inter> space M \<in> sets M" unfolding * using A by auto
   445   qed
   436   qed
   446 qed
   437 qed
   447 
   438 
   448 lemma (in sigma_algebra) simple_function_If:
   439 lemma simple_function_If:
   449   assumes sf: "simple_function M f" "simple_function M g" and P: "{x\<in>space M. P x} \<in> sets M"
   440   assumes sf: "simple_function M f" "simple_function M g" and P: "{x\<in>space M. P x} \<in> sets M"
   450   shows "simple_function M (\<lambda>x. if P x then f x else g x)"
   441   shows "simple_function M (\<lambda>x. if P x then f x else g x)"
   451 proof -
   442 proof -
   452   have "{x\<in>space M. P x} = {x. P x} \<inter> space M" by auto
   443   have "{x\<in>space M. P x} = {x. P x} \<inter> space M" by auto
   453   with simple_function_If_set[OF sf, of "{x. P x}"] P show ?thesis by simp
   444   with simple_function_If_set[OF sf, of "{x. P x}"] P show ?thesis by simp
   454 qed
   445 qed
   455 
   446 
   456 lemma (in measure_space) simple_function_restricted:
   447 lemma simple_function_subalgebra:
   457   fixes f :: "'a \<Rightarrow> ereal" assumes "A \<in> sets M"
       
   458   shows "simple_function (restricted_space A) f \<longleftrightarrow> simple_function M (\<lambda>x. f x * indicator A x)"
       
   459     (is "simple_function ?R f \<longleftrightarrow> simple_function M ?f")
       
   460 proof -
       
   461   interpret R: sigma_algebra ?R by (rule restricted_sigma_algebra[OF `A \<in> sets M`])
       
   462   have f: "finite (f`A) \<longleftrightarrow> finite (?f`space M)"
       
   463   proof cases
       
   464     assume "A = space M"
       
   465     then have "f`A = ?f`space M" by (fastforce simp: image_iff)
       
   466     then show ?thesis by simp
       
   467   next
       
   468     assume "A \<noteq> space M"
       
   469     then obtain x where x: "x \<in> space M" "x \<notin> A"
       
   470       using sets_into_space `A \<in> sets M` by auto
       
   471     have *: "?f`space M = f`A \<union> {0}"
       
   472     proof (auto simp add: image_iff)
       
   473       show "\<exists>x\<in>space M. f x = 0 \<or> indicator A x = 0"
       
   474         using x by (auto intro!: bexI[of _ x])
       
   475     next
       
   476       fix x assume "x \<in> A"
       
   477       then show "\<exists>y\<in>space M. f x = f y * indicator A y"
       
   478         using `A \<in> sets M` sets_into_space by (auto intro!: bexI[of _ x])
       
   479     next
       
   480       fix x
       
   481       assume "indicator A x \<noteq> (0::ereal)"
       
   482       then have "x \<in> A" by (auto simp: indicator_def split: split_if_asm)
       
   483       moreover assume "x \<in> space M" "\<forall>y\<in>A. ?f x \<noteq> f y"
       
   484       ultimately show "f x = 0" by auto
       
   485     qed
       
   486     then show ?thesis by auto
       
   487   qed
       
   488   then show ?thesis
       
   489     unfolding simple_function_eq_borel_measurable
       
   490       R.simple_function_eq_borel_measurable
       
   491     unfolding borel_measurable_restricted[OF `A \<in> sets M`]
       
   492     using assms(1)[THEN sets_into_space]
       
   493     by (auto simp: indicator_def)
       
   494 qed
       
   495 
       
   496 lemma (in sigma_algebra) simple_function_subalgebra:
       
   497   assumes "simple_function N f"
   448   assumes "simple_function N f"
   498   and N_subalgebra: "sets N \<subseteq> sets M" "space N = space M"
   449   and N_subalgebra: "sets N \<subseteq> sets M" "space N = space M"
   499   shows "simple_function M f"
   450   shows "simple_function M f"
   500   using assms unfolding simple_function_def by auto
   451   using assms unfolding simple_function_def by auto
   501 
   452 
   502 lemma (in measure_space) simple_function_vimage:
   453 lemma simple_function_comp:
   503   assumes T: "sigma_algebra M'" "T \<in> measurable M M'"
   454   assumes T: "T \<in> measurable M M'"
   504     and f: "simple_function M' f"
   455     and f: "simple_function M' f"
   505   shows "simple_function M (\<lambda>x. f (T x))"
   456   shows "simple_function M (\<lambda>x. f (T x))"
   506 proof (intro simple_function_def[THEN iffD2] conjI ballI)
   457 proof (intro simple_function_def[THEN iffD2] conjI ballI)
   507   interpret T: sigma_algebra M' by fact
       
   508   have "(\<lambda>x. f (T x)) ` space M \<subseteq> f ` space M'"
   458   have "(\<lambda>x. f (T x)) ` space M \<subseteq> f ` space M'"
   509     using T unfolding measurable_def by auto
   459     using T unfolding measurable_def by auto
   510   then show "finite ((\<lambda>x. f (T x)) ` space M)"
   460   then show "finite ((\<lambda>x. f (T x)) ` space M)"
   511     using f unfolding simple_function_def by (auto intro: finite_subset)
   461     using f unfolding simple_function_def by (auto intro: finite_subset)
   512   fix i assume i: "i \<in> (\<lambda>x. f (T x)) ` space M"
   462   fix i assume i: "i \<in> (\<lambda>x. f (T x)) ` space M"
   521   finally show "(\<lambda>x. f (T x)) -` {i} \<inter> space M \<in> sets M" .
   471   finally show "(\<lambda>x. f (T x)) -` {i} \<inter> space M \<in> sets M" .
   522 qed
   472 qed
   523 
   473 
   524 section "Simple integral"
   474 section "Simple integral"
   525 
   475 
   526 definition simple_integral_def:
   476 definition simple_integral :: "'a measure \<Rightarrow> ('a \<Rightarrow> ereal) \<Rightarrow> ereal" ("integral\<^isup>S") where
   527   "integral\<^isup>S M f = (\<Sum>x \<in> f ` space M. x * measure M (f -` {x} \<inter> space M))"
   477   "integral\<^isup>S M f = (\<Sum>x \<in> f ` space M. x * emeasure M (f -` {x} \<inter> space M))"
   528 
   478 
   529 syntax
   479 syntax
   530   "_simple_integral" :: "pttrn \<Rightarrow> ereal \<Rightarrow> ('a, 'b) measure_space_scheme \<Rightarrow> ereal" ("\<integral>\<^isup>S _. _ \<partial>_" [60,61] 110)
   480   "_simple_integral" :: "pttrn \<Rightarrow> ereal \<Rightarrow> 'a measure \<Rightarrow> ereal" ("\<integral>\<^isup>S _. _ \<partial>_" [60,61] 110)
   531 
   481 
   532 translations
   482 translations
   533   "\<integral>\<^isup>S x. f \<partial>M" == "CONST integral\<^isup>S M (%x. f)"
   483   "\<integral>\<^isup>S x. f \<partial>M" == "CONST simple_integral M (%x. f)"
   534 
   484 
   535 lemma (in measure_space) simple_integral_cong:
   485 lemma simple_integral_cong:
   536   assumes "\<And>t. t \<in> space M \<Longrightarrow> f t = g t"
   486   assumes "\<And>t. t \<in> space M \<Longrightarrow> f t = g t"
   537   shows "integral\<^isup>S M f = integral\<^isup>S M g"
   487   shows "integral\<^isup>S M f = integral\<^isup>S M g"
   538 proof -
   488 proof -
   539   have "f ` space M = g ` space M"
   489   have "f ` space M = g ` space M"
   540     "\<And>x. f -` {x} \<inter> space M = g -` {x} \<inter> space M"
   490     "\<And>x. f -` {x} \<inter> space M = g -` {x} \<inter> space M"
   541     using assms by (auto intro!: image_eqI)
   491     using assms by (auto intro!: image_eqI)
   542   thus ?thesis unfolding simple_integral_def by simp
   492   thus ?thesis unfolding simple_integral_def by simp
   543 qed
   493 qed
   544 
   494 
   545 lemma (in measure_space) simple_integral_cong_measure:
   495 lemma simple_integral_const[simp]:
   546   assumes "\<And>A. A \<in> sets M \<Longrightarrow> measure N A = \<mu> A" "sets N = sets M" "space N = space M"
   496   "(\<integral>\<^isup>Sx. c \<partial>M) = c * (emeasure M) (space M)"
   547     and "simple_function M f"
       
   548   shows "integral\<^isup>S N f = integral\<^isup>S M f"
       
   549 proof -
       
   550   interpret v: measure_space N
       
   551     by (rule measure_space_cong) fact+
       
   552   from simple_functionD[OF `simple_function M f`] assms show ?thesis
       
   553     by (auto intro!: setsum_cong simp: simple_integral_def)
       
   554 qed
       
   555 
       
   556 lemma (in measure_space) simple_integral_const[simp]:
       
   557   "(\<integral>\<^isup>Sx. c \<partial>M) = c * \<mu> (space M)"
       
   558 proof (cases "space M = {}")
   497 proof (cases "space M = {}")
   559   case True thus ?thesis unfolding simple_integral_def by simp
   498   case True thus ?thesis unfolding simple_integral_def by simp
   560 next
   499 next
   561   case False hence "(\<lambda>x. c) ` space M = {c}" by auto
   500   case False hence "(\<lambda>x. c) ` space M = {c}" by auto
   562   thus ?thesis unfolding simple_integral_def by simp
   501   thus ?thesis unfolding simple_integral_def by simp
   563 qed
   502 qed
   564 
   503 
   565 lemma (in measure_space) simple_function_partition:
   504 lemma simple_function_partition:
   566   assumes f: "simple_function M f" and g: "simple_function M g"
   505   assumes f: "simple_function M f" and g: "simple_function M g"
   567   shows "integral\<^isup>S M f = (\<Sum>A\<in>(\<lambda>x. f -` {f x} \<inter> g -` {g x} \<inter> space M) ` space M. the_elem (f`A) * \<mu> A)"
   506   shows "integral\<^isup>S M f = (\<Sum>A\<in>(\<lambda>x. f -` {f x} \<inter> g -` {g x} \<inter> space M) ` space M. the_elem (f`A) * (emeasure M) A)"
   568     (is "_ = setsum _ (?p ` space M)")
   507     (is "_ = setsum _ (?p ` space M)")
   569 proof-
   508 proof-
   570   let ?sub = "\<lambda>x. ?p ` (f -` {x} \<inter> space M)"
   509   let ?sub = "\<lambda>x. ?p ` (f -` {x} \<inter> space M)"
   571   let ?SIGMA = "Sigma (f`space M) ?sub"
   510   let ?SIGMA = "Sigma (f`space M) ?sub"
   572 
   511 
   584   note this[intro, simp]
   523   note this[intro, simp]
   585   note sets = simple_function_measurable2[OF f g]
   524   note sets = simple_function_measurable2[OF f g]
   586 
   525 
   587   { fix x assume "x \<in> space M"
   526   { fix x assume "x \<in> space M"
   588     have "\<Union>(?sub (f x)) = (f -` {f x} \<inter> space M)" by auto
   527     have "\<Union>(?sub (f x)) = (f -` {f x} \<inter> space M)" by auto
   589     with sets have "\<mu> (f -` {f x} \<inter> space M) = setsum \<mu> (?sub (f x))"
   528     with sets have "(emeasure M) (f -` {f x} \<inter> space M) = setsum (emeasure M) (?sub (f x))"
   590       by (subst measure_Union) auto }
   529       by (subst measure_Union) auto }
   591   hence "integral\<^isup>S M f = (\<Sum>(x,A)\<in>?SIGMA. x * \<mu> A)"
   530   hence "integral\<^isup>S M f = (\<Sum>(x,A)\<in>?SIGMA. x * (emeasure M) A)"
   592     unfolding simple_integral_def using f sets
   531     unfolding simple_integral_def using f sets
   593     by (subst setsum_Sigma[symmetric])
   532     by (subst setsum_Sigma[symmetric])
   594        (auto intro!: setsum_cong setsum_ereal_right_distrib)
   533        (auto intro!: setsum_cong setsum_ereal_right_distrib)
   595   also have "\<dots> = (\<Sum>A\<in>?p ` space M. the_elem (f`A) * \<mu> A)"
   534   also have "\<dots> = (\<Sum>A\<in>?p ` space M. the_elem (f`A) * (emeasure M) A)"
   596   proof -
   535   proof -
   597     have [simp]: "\<And>x. x \<in> space M \<Longrightarrow> f ` ?p x = {f x}" by (auto intro!: imageI)
   536     have [simp]: "\<And>x. x \<in> space M \<Longrightarrow> f ` ?p x = {f x}" by (auto intro!: imageI)
   598     have "(\<lambda>A. (the_elem (f ` A), A)) ` ?p ` space M
   537     have "(\<lambda>A. (the_elem (f ` A), A)) ` ?p ` space M
   599       = (\<lambda>x. (f x, ?p x)) ` space M"
   538       = (\<lambda>x. (f x, ?p x)) ` space M"
   600     proof safe
   539     proof safe
   608       by simp_all
   547       by simp_all
   609   qed
   548   qed
   610   finally show ?thesis .
   549   finally show ?thesis .
   611 qed
   550 qed
   612 
   551 
   613 lemma (in measure_space) simple_integral_add[simp]:
   552 lemma simple_integral_add[simp]:
   614   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"
   553   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"
   615   shows "(\<integral>\<^isup>Sx. f x + g x \<partial>M) = integral\<^isup>S M f + integral\<^isup>S M g"
   554   shows "(\<integral>\<^isup>Sx. f x + g x \<partial>M) = integral\<^isup>S M f + integral\<^isup>S M g"
   616 proof -
   555 proof -
   617   { fix x let ?S = "g -` {g x} \<inter> f -` {f x} \<inter> space M"
   556   { fix x let ?S = "g -` {g x} \<inter> f -` {f x} \<inter> space M"
   618     assume "x \<in> space M"
   557     assume "x \<in> space M"
   626       simple_function_partition[OF g f]
   565       simple_function_partition[OF g f]
   627     by (subst (3) Int_commute)
   566     by (subst (3) Int_commute)
   628        (auto simp add: ereal_left_distrib setsum_addf[symmetric] intro!: setsum_cong)
   567        (auto simp add: ereal_left_distrib setsum_addf[symmetric] intro!: setsum_cong)
   629 qed
   568 qed
   630 
   569 
   631 lemma (in measure_space) simple_integral_setsum[simp]:
   570 lemma simple_integral_setsum[simp]:
   632   assumes "\<And>i x. i \<in> P \<Longrightarrow> 0 \<le> f i x"
   571   assumes "\<And>i x. i \<in> P \<Longrightarrow> 0 \<le> f i x"
   633   assumes "\<And>i. i \<in> P \<Longrightarrow> simple_function M (f i)"
   572   assumes "\<And>i. i \<in> P \<Longrightarrow> simple_function M (f i)"
   634   shows "(\<integral>\<^isup>Sx. (\<Sum>i\<in>P. f i x) \<partial>M) = (\<Sum>i\<in>P. integral\<^isup>S M (f i))"
   573   shows "(\<integral>\<^isup>Sx. (\<Sum>i\<in>P. f i x) \<partial>M) = (\<Sum>i\<in>P. integral\<^isup>S M (f i))"
   635 proof cases
   574 proof cases
   636   assume "finite P"
   575   assume "finite P"
   637   from this assms show ?thesis
   576   from this assms show ?thesis
   638     by induct (auto simp: simple_function_setsum simple_integral_add setsum_nonneg)
   577     by induct (auto simp: simple_function_setsum simple_integral_add setsum_nonneg)
   639 qed auto
   578 qed auto
   640 
   579 
   641 lemma (in measure_space) simple_integral_mult[simp]:
   580 lemma simple_integral_mult[simp]:
   642   assumes f: "simple_function M f" "\<And>x. 0 \<le> f x"
   581   assumes f: "simple_function M f" "\<And>x. 0 \<le> f x"
   643   shows "(\<integral>\<^isup>Sx. c * f x \<partial>M) = c * integral\<^isup>S M f"
   582   shows "(\<integral>\<^isup>Sx. c * f x \<partial>M) = c * integral\<^isup>S M f"
   644 proof -
   583 proof -
   645   note mult = simple_function_mult[OF simple_function_const[of c] f(1)]
   584   note mult = simple_function_mult[OF simple_function_const[of _ c] f(1)]
   646   { fix x let ?S = "f -` {f x} \<inter> (\<lambda>x. c * f x) -` {c * f x} \<inter> space M"
   585   { fix x let ?S = "f -` {f x} \<inter> (\<lambda>x. c * f x) -` {c * f x} \<inter> space M"
   647     assume "x \<in> space M"
   586     assume "x \<in> space M"
   648     hence "(\<lambda>x. c * f x) ` ?S = {c * f x}" "f ` ?S = {f x}"
   587     hence "(\<lambda>x. c * f x) ` ?S = {c * f x}" "f ` ?S = {f x}"
   649       by auto }
   588       by auto }
   650   with assms show ?thesis
   589   with assms show ?thesis
   652               simple_function_partition[OF f(1) mult]
   591               simple_function_partition[OF f(1) mult]
   653     by (subst setsum_ereal_right_distrib)
   592     by (subst setsum_ereal_right_distrib)
   654        (auto intro!: ereal_0_le_mult setsum_cong simp: mult_assoc)
   593        (auto intro!: ereal_0_le_mult setsum_cong simp: mult_assoc)
   655 qed
   594 qed
   656 
   595 
   657 lemma (in measure_space) simple_integral_mono_AE:
   596 lemma simple_integral_mono_AE:
   658   assumes f: "simple_function M f" and g: "simple_function M g"
   597   assumes f: "simple_function M f" and g: "simple_function M g"
   659   and mono: "AE x. f x \<le> g x"
   598   and mono: "AE x in M. f x \<le> g x"
   660   shows "integral\<^isup>S M f \<le> integral\<^isup>S M g"
   599   shows "integral\<^isup>S M f \<le> integral\<^isup>S M g"
   661 proof -
   600 proof -
   662   let ?S = "\<lambda>x. (g -` {g x} \<inter> space M) \<inter> (f -` {f x} \<inter> space M)"
   601   let ?S = "\<lambda>x. (g -` {g x} \<inter> space M) \<inter> (f -` {f x} \<inter> space M)"
   663   have *: "\<And>x. g -` {g x} \<inter> f -` {f x} \<inter> space M = ?S x"
   602   have *: "\<And>x. g -` {g x} \<inter> f -` {f x} \<inter> space M = ?S x"
   664     "\<And>x. f -` {f x} \<inter> g -` {g x} \<inter> space M = ?S x" by auto
   603     "\<And>x. f -` {f x} \<inter> g -` {g x} \<inter> space M = ?S x" by auto
   667       simple_function_partition[OF f g]
   606       simple_function_partition[OF f g]
   668       simple_function_partition[OF g f]
   607       simple_function_partition[OF g f]
   669   proof (safe intro!: setsum_mono)
   608   proof (safe intro!: setsum_mono)
   670     fix x assume "x \<in> space M"
   609     fix x assume "x \<in> space M"
   671     then have *: "f ` ?S x = {f x}" "g ` ?S x = {g x}" by auto
   610     then have *: "f ` ?S x = {f x}" "g ` ?S x = {g x}" by auto
   672     show "the_elem (f`?S x) * \<mu> (?S x) \<le> the_elem (g`?S x) * \<mu> (?S x)"
   611     show "the_elem (f`?S x) * (emeasure M) (?S x) \<le> the_elem (g`?S x) * (emeasure M) (?S x)"
   673     proof (cases "f x \<le> g x")
   612     proof (cases "f x \<le> g x")
   674       case True then show ?thesis
   613       case True then show ?thesis
   675         using * assms(1,2)[THEN simple_functionD(2)]
   614         using * assms(1,2)[THEN simple_functionD(2)]
   676         by (auto intro!: ereal_mult_right_mono)
   615         by (auto intro!: ereal_mult_right_mono)
   677     next
   616     next
   678       case False
   617       case False
   679       obtain N where N: "{x\<in>space M. \<not> f x \<le> g x} \<subseteq> N" "N \<in> sets M" "\<mu> N = 0"
   618       obtain N where N: "{x\<in>space M. \<not> f x \<le> g x} \<subseteq> N" "N \<in> sets M" "(emeasure M) N = 0"
   680         using mono by (auto elim!: AE_E)
   619         using mono by (auto elim!: AE_E)
   681       have "?S x \<subseteq> N" using N `x \<in> space M` False by auto
   620       have "?S x \<subseteq> N" using N `x \<in> space M` False by auto
   682       moreover have "?S x \<in> sets M" using assms
   621       moreover have "?S x \<in> sets M" using assms
   683         by (rule_tac Int) (auto intro!: simple_functionD)
   622         by (rule_tac Int) (auto intro!: simple_functionD)
   684       ultimately have "\<mu> (?S x) \<le> \<mu> N"
   623       ultimately have "(emeasure M) (?S x) \<le> (emeasure M) N"
   685         using `N \<in> sets M` by (auto intro!: measure_mono)
   624         using `N \<in> sets M` by (auto intro!: emeasure_mono)
   686       moreover have "0 \<le> \<mu> (?S x)"
   625       moreover have "0 \<le> (emeasure M) (?S x)"
   687         using assms(1,2)[THEN simple_functionD(2)] by auto
   626         using assms(1,2)[THEN simple_functionD(2)] by auto
   688       ultimately have "\<mu> (?S x) = 0" using `\<mu> N = 0` by auto
   627       ultimately have "(emeasure M) (?S x) = 0" using `(emeasure M) N = 0` by auto
   689       then show ?thesis by simp
   628       then show ?thesis by simp
   690     qed
   629     qed
   691   qed
   630   qed
   692 qed
   631 qed
   693 
   632 
   694 lemma (in measure_space) simple_integral_mono:
   633 lemma simple_integral_mono:
   695   assumes "simple_function M f" and "simple_function M g"
   634   assumes "simple_function M f" and "simple_function M g"
   696   and mono: "\<And> x. x \<in> space M \<Longrightarrow> f x \<le> g x"
   635   and mono: "\<And> x. x \<in> space M \<Longrightarrow> f x \<le> g x"
   697   shows "integral\<^isup>S M f \<le> integral\<^isup>S M g"
   636   shows "integral\<^isup>S M f \<le> integral\<^isup>S M g"
   698   using assms by (intro simple_integral_mono_AE) auto
   637   using assms by (intro simple_integral_mono_AE) auto
   699 
   638 
   700 lemma (in measure_space) simple_integral_cong_AE:
   639 lemma simple_integral_cong_AE:
   701   assumes "simple_function M f" and "simple_function M g"
   640   assumes "simple_function M f" and "simple_function M g"
   702   and "AE x. f x = g x"
   641   and "AE x in M. f x = g x"
   703   shows "integral\<^isup>S M f = integral\<^isup>S M g"
   642   shows "integral\<^isup>S M f = integral\<^isup>S M g"
   704   using assms by (auto simp: eq_iff intro!: simple_integral_mono_AE)
   643   using assms by (auto simp: eq_iff intro!: simple_integral_mono_AE)
   705 
   644 
   706 lemma (in measure_space) simple_integral_cong':
   645 lemma simple_integral_cong':
   707   assumes sf: "simple_function M f" "simple_function M g"
   646   assumes sf: "simple_function M f" "simple_function M g"
   708   and mea: "\<mu> {x\<in>space M. f x \<noteq> g x} = 0"
   647   and mea: "(emeasure M) {x\<in>space M. f x \<noteq> g x} = 0"
   709   shows "integral\<^isup>S M f = integral\<^isup>S M g"
   648   shows "integral\<^isup>S M f = integral\<^isup>S M g"
   710 proof (intro simple_integral_cong_AE sf AE_I)
   649 proof (intro simple_integral_cong_AE sf AE_I)
   711   show "\<mu> {x\<in>space M. f x \<noteq> g x} = 0" by fact
   650   show "(emeasure M) {x\<in>space M. f x \<noteq> g x} = 0" by fact
   712   show "{x \<in> space M. f x \<noteq> g x} \<in> sets M"
   651   show "{x \<in> space M. f x \<noteq> g x} \<in> sets M"
   713     using sf[THEN borel_measurable_simple_function] by auto
   652     using sf[THEN borel_measurable_simple_function] by auto
   714 qed simp
   653 qed simp
   715 
   654 
   716 lemma (in measure_space) simple_integral_indicator:
   655 lemma simple_integral_indicator:
   717   assumes "A \<in> sets M"
   656   assumes "A \<in> sets M"
   718   assumes "simple_function M f"
   657   assumes "simple_function M f"
   719   shows "(\<integral>\<^isup>Sx. f x * indicator A x \<partial>M) =
   658   shows "(\<integral>\<^isup>Sx. f x * indicator A x \<partial>M) =
   720     (\<Sum>x \<in> f ` space M. x * \<mu> (f -` {x} \<inter> space M \<inter> A))"
   659     (\<Sum>x \<in> f ` space M. x * (emeasure M) (f -` {x} \<inter> space M \<inter> A))"
   721 proof cases
   660 proof cases
   722   assume "A = space M"
   661   assume "A = space M"
   723   moreover hence "(\<integral>\<^isup>Sx. f x * indicator A x \<partial>M) = integral\<^isup>S M f"
   662   moreover hence "(\<integral>\<^isup>Sx. f x * indicator A x \<partial>M) = integral\<^isup>S M f"
   724     by (auto intro!: simple_integral_cong)
   663     by (auto intro!: simple_integral_cong)
   725   moreover have "\<And>X. X \<inter> space M \<inter> space M = X \<inter> space M" by auto
   664   moreover have "\<And>X. X \<inter> space M \<inter> space M = X \<inter> space M" by auto
   735       using sets_into_space[OF assms(1)] by (auto intro!: image_eqI[of _ _ y])
   674       using sets_into_space[OF assms(1)] by (auto intro!: image_eqI[of _ _ y])
   736   next
   675   next
   737     show "0 \<in> ?I ` space M" using x by (auto intro!: image_eqI[of _ _ x])
   676     show "0 \<in> ?I ` space M" using x by (auto intro!: image_eqI[of _ _ x])
   738   qed
   677   qed
   739   have *: "(\<integral>\<^isup>Sx. f x * indicator A x \<partial>M) =
   678   have *: "(\<integral>\<^isup>Sx. f x * indicator A x \<partial>M) =
   740     (\<Sum>x \<in> f ` space M \<union> {0}. x * \<mu> (f -` {x} \<inter> space M \<inter> A))"
   679     (\<Sum>x \<in> f ` space M \<union> {0}. x * (emeasure M) (f -` {x} \<inter> space M \<inter> A))"
   741     unfolding simple_integral_def I
   680     unfolding simple_integral_def I
   742   proof (rule setsum_mono_zero_cong_left)
   681   proof (rule setsum_mono_zero_cong_left)
   743     show "finite (f ` space M \<union> {0})"
   682     show "finite (f ` space M \<union> {0})"
   744       using assms(2) unfolding simple_function_def by auto
   683       using assms(2) unfolding simple_function_def by auto
   745     show "f ` A \<union> {0} \<subseteq> f`space M \<union> {0}"
   684     show "f ` A \<union> {0} \<subseteq> f`space M \<union> {0}"
   746       using sets_into_space[OF assms(1)] by auto
   685       using sets_into_space[OF assms(1)] by auto
   747     have "\<And>x. f x \<notin> f ` A \<Longrightarrow> f -` {f x} \<inter> space M \<inter> A = {}"
   686     have "\<And>x. f x \<notin> f ` A \<Longrightarrow> f -` {f x} \<inter> space M \<inter> A = {}"
   748       by (auto simp: image_iff)
   687       by (auto simp: image_iff)
   749     thus "\<forall>i\<in>f ` space M \<union> {0} - (f ` A \<union> {0}).
   688     thus "\<forall>i\<in>f ` space M \<union> {0} - (f ` A \<union> {0}).
   750       i * \<mu> (f -` {i} \<inter> space M \<inter> A) = 0" by auto
   689       i * (emeasure M) (f -` {i} \<inter> space M \<inter> A) = 0" by auto
   751   next
   690   next
   752     fix x assume "x \<in> f`A \<union> {0}"
   691     fix x assume "x \<in> f`A \<union> {0}"
   753     hence "x \<noteq> 0 \<Longrightarrow> ?I -` {x} \<inter> space M = f -` {x} \<inter> space M \<inter> A"
   692     hence "x \<noteq> 0 \<Longrightarrow> ?I -` {x} \<inter> space M = f -` {x} \<inter> space M \<inter> A"
   754       by (auto simp: indicator_def split: split_if_asm)
   693       by (auto simp: indicator_def split: split_if_asm)
   755     thus "x * \<mu> (?I -` {x} \<inter> space M) =
   694     thus "x * (emeasure M) (?I -` {x} \<inter> space M) =
   756       x * \<mu> (f -` {x} \<inter> space M \<inter> A)" by (cases "x = 0") simp_all
   695       x * (emeasure M) (f -` {x} \<inter> space M \<inter> A)" by (cases "x = 0") simp_all
   757   qed
   696   qed
   758   show ?thesis unfolding *
   697   show ?thesis unfolding *
   759     using assms(2) unfolding simple_function_def
   698     using assms(2) unfolding simple_function_def
   760     by (auto intro!: setsum_mono_zero_cong_right)
   699     by (auto intro!: setsum_mono_zero_cong_right)
   761 qed
   700 qed
   762 
   701 
   763 lemma (in measure_space) simple_integral_indicator_only[simp]:
   702 lemma simple_integral_indicator_only[simp]:
   764   assumes "A \<in> sets M"
   703   assumes "A \<in> sets M"
   765   shows "integral\<^isup>S M (indicator A) = \<mu> A"
   704   shows "integral\<^isup>S M (indicator A) = emeasure M A"
   766 proof cases
   705 proof cases
   767   assume "space M = {}" hence "A = {}" using sets_into_space[OF assms] by auto
   706   assume "space M = {}" hence "A = {}" using sets_into_space[OF assms] by auto
   768   thus ?thesis unfolding simple_integral_def using `space M = {}` by auto
   707   thus ?thesis unfolding simple_integral_def using `space M = {}` by auto
   769 next
   708 next
   770   assume "space M \<noteq> {}" hence "(\<lambda>x. 1) ` space M = {1::ereal}" by auto
   709   assume "space M \<noteq> {}" hence "(\<lambda>x. 1) ` space M = {1::ereal}" by auto
   771   thus ?thesis
   710   thus ?thesis
   772     using simple_integral_indicator[OF assms simple_function_const[of 1]]
   711     using simple_integral_indicator[OF assms simple_function_const[of _ 1]]
   773     using sets_into_space[OF assms]
   712     using sets_into_space[OF assms]
   774     by (auto intro!: arg_cong[where f="\<mu>"])
   713     by (auto intro!: arg_cong[where f="(emeasure M)"])
   775 qed
   714 qed
   776 
   715 
   777 lemma (in measure_space) simple_integral_null_set:
   716 lemma simple_integral_null_set:
   778   assumes "simple_function M u" "\<And>x. 0 \<le> u x" and "N \<in> null_sets"
   717   assumes "simple_function M u" "\<And>x. 0 \<le> u x" and "N \<in> null_sets M"
   779   shows "(\<integral>\<^isup>Sx. u x * indicator N x \<partial>M) = 0"
   718   shows "(\<integral>\<^isup>Sx. u x * indicator N x \<partial>M) = 0"
   780 proof -
   719 proof -
   781   have "AE x. indicator N x = (0 :: ereal)"
   720   have "AE x in M. indicator N x = (0 :: ereal)"
   782     using `N \<in> null_sets` by (auto simp: indicator_def intro!: AE_I[of _ N])
   721     using `N \<in> null_sets M` by (auto simp: indicator_def intro!: AE_I[of _ _ N])
   783   then have "(\<integral>\<^isup>Sx. u x * indicator N x \<partial>M) = (\<integral>\<^isup>Sx. 0 \<partial>M)"
   722   then have "(\<integral>\<^isup>Sx. u x * indicator N x \<partial>M) = (\<integral>\<^isup>Sx. 0 \<partial>M)"
   784     using assms apply (intro simple_integral_cong_AE) by auto
   723     using assms apply (intro simple_integral_cong_AE) by auto
   785   then show ?thesis by simp
   724   then show ?thesis by simp
   786 qed
   725 qed
   787 
   726 
   788 lemma (in measure_space) simple_integral_cong_AE_mult_indicator:
   727 lemma simple_integral_cong_AE_mult_indicator:
   789   assumes sf: "simple_function M f" and eq: "AE x. x \<in> S" and "S \<in> sets M"
   728   assumes sf: "simple_function M f" and eq: "AE x in M. x \<in> S" and "S \<in> sets M"
   790   shows "integral\<^isup>S M f = (\<integral>\<^isup>Sx. f x * indicator S x \<partial>M)"
   729   shows "integral\<^isup>S M f = (\<integral>\<^isup>Sx. f x * indicator S x \<partial>M)"
   791   using assms by (intro simple_integral_cong_AE) auto
   730   using assms by (intro simple_integral_cong_AE) auto
   792 
   731 
   793 lemma (in measure_space) simple_integral_restricted:
   732 lemma simple_integral_distr:
   794   assumes "A \<in> sets M"
   733   assumes T: "T \<in> measurable M M'"
   795   assumes sf: "simple_function M (\<lambda>x. f x * indicator A x)"
   734     and f: "simple_function M' f"
   796   shows "integral\<^isup>S (restricted_space A) f = (\<integral>\<^isup>Sx. f x * indicator A x \<partial>M)"
   735   shows "integral\<^isup>S (distr M M' T) f = (\<integral>\<^isup>S x. f (T x) \<partial>M)"
   797     (is "_ = integral\<^isup>S M ?f")
       
   798   unfolding simple_integral_def
   736   unfolding simple_integral_def
   799 proof (simp, safe intro!: setsum_mono_zero_cong_left)
   737 proof (intro setsum_mono_zero_cong_right ballI)
   800   from sf show "finite (?f ` space M)"
   738   show "(\<lambda>x. f (T x)) ` space M \<subseteq> f ` space (distr M M' T)"
   801     unfolding simple_function_def by auto
   739     using T unfolding measurable_def by auto
       
   740   show "finite (f ` space (distr M M' T))"
       
   741     using f unfolding simple_function_def by auto
   802 next
   742 next
   803   fix x assume "x \<in> A"
   743   fix i assume "i \<in> f ` space (distr M M' T) - (\<lambda>x. f (T x)) ` space M"
   804   then show "f x \<in> ?f ` space M"
   744   then have "T -` (f -` {i} \<inter> space (distr M M' T)) \<inter> space M = {}" by (auto simp: image_iff)
   805     using sets_into_space `A \<in> sets M` by (auto intro!: image_eqI[of _ _ x])
   745   with f[THEN simple_functionD(2), of "{i}"]
       
   746   show "i * emeasure (distr M M' T) (f -` {i} \<inter> space (distr M M' T)) = 0"
       
   747     using T by (simp add: emeasure_distr)
   806 next
   748 next
   807   fix x assume "x \<in> space M" "?f x \<notin> f`A"
   749   fix i assume "i \<in> (\<lambda>x. f (T x)) ` space M"
   808   then have "x \<notin> A" by (auto simp: image_iff)
   750   then have "T -` (f -` {i} \<inter> space M') \<inter> space M = (\<lambda>x. f (T x)) -` {i} \<inter> space M"
   809   then show "?f x * \<mu> (?f -` {?f x} \<inter> space M) = 0" by simp
   751     using T unfolding measurable_def by auto
   810 next
   752   with f[THEN simple_functionD(2), of "{i}"] T
   811   fix x assume "x \<in> A"
   753   show "i * emeasure (distr M M' T) (f -` {i} \<inter> space (distr M M' T)) =
   812   then have "f x \<noteq> 0 \<Longrightarrow>
   754       i * (emeasure M) ((\<lambda>x. f (T x)) -` {i} \<inter> space M)"
   813     f -` {f x} \<inter> A = ?f -` {f x} \<inter> space M"
   755     by (auto simp add: emeasure_distr)
   814     using `A \<in> sets M` sets_into_space
   756 qed
   815     by (auto simp: indicator_def split: split_if_asm)
   757 
   816   then show "f x * \<mu> (f -` {f x} \<inter> A) =
   758 lemma simple_integral_cmult_indicator:
   817     f x * \<mu> (?f -` {f x} \<inter> space M)"
       
   818     unfolding ereal_mult_cancel_left by auto
       
   819 qed
       
   820 
       
   821 lemma (in measure_space) simple_integral_subalgebra:
       
   822   assumes N: "measure_space N" and [simp]: "space N = space M" "measure N = measure M"
       
   823   shows "integral\<^isup>S N = integral\<^isup>S M"
       
   824   unfolding simple_integral_def [abs_def] by simp
       
   825 
       
   826 lemma (in measure_space) simple_integral_vimage:
       
   827   assumes T: "sigma_algebra M'" "T \<in> measure_preserving M M'"
       
   828     and f: "simple_function M' f"
       
   829   shows "integral\<^isup>S M' f = (\<integral>\<^isup>S x. f (T x) \<partial>M)"
       
   830 proof -
       
   831   interpret T: measure_space M' by (rule measure_space_vimage[OF T])
       
   832   show "integral\<^isup>S M' f = (\<integral>\<^isup>S x. f (T x) \<partial>M)"
       
   833     unfolding simple_integral_def
       
   834   proof (intro setsum_mono_zero_cong_right ballI)
       
   835     show "(\<lambda>x. f (T x)) ` space M \<subseteq> f ` space M'"
       
   836       using T unfolding measurable_def measure_preserving_def by auto
       
   837     show "finite (f ` space M')"
       
   838       using f unfolding simple_function_def by auto
       
   839   next
       
   840     fix i assume "i \<in> f ` space M' - (\<lambda>x. f (T x)) ` space M"
       
   841     then have "T -` (f -` {i} \<inter> space M') \<inter> space M = {}" by (auto simp: image_iff)
       
   842     with f[THEN T.simple_functionD(2), THEN measure_preservingD[OF T(2)], of "{i}"]
       
   843     show "i * T.\<mu> (f -` {i} \<inter> space M') = 0" by simp
       
   844   next
       
   845     fix i assume "i \<in> (\<lambda>x. f (T x)) ` space M"
       
   846     then have "T -` (f -` {i} \<inter> space M') \<inter> space M = (\<lambda>x. f (T x)) -` {i} \<inter> space M"
       
   847       using T unfolding measurable_def measure_preserving_def by auto
       
   848     with f[THEN T.simple_functionD(2), THEN measure_preservingD[OF T(2)], of "{i}"]
       
   849     show "i * T.\<mu> (f -` {i} \<inter> space M') = i * \<mu> ((\<lambda>x. f (T x)) -` {i} \<inter> space M)"
       
   850       by auto
       
   851   qed
       
   852 qed
       
   853 
       
   854 lemma (in measure_space) simple_integral_cmult_indicator:
       
   855   assumes A: "A \<in> sets M"
   759   assumes A: "A \<in> sets M"
   856   shows "(\<integral>\<^isup>Sx. c * indicator A x \<partial>M) = c * \<mu> A"
   760   shows "(\<integral>\<^isup>Sx. c * indicator A x \<partial>M) = c * (emeasure M) A"
   857   using simple_integral_mult[OF simple_function_indicator[OF A]]
   761   using simple_integral_mult[OF simple_function_indicator[OF A]]
   858   unfolding simple_integral_indicator_only[OF A] by simp
   762   unfolding simple_integral_indicator_only[OF A] by simp
   859 
   763 
   860 lemma (in measure_space) simple_integral_positive:
   764 lemma simple_integral_positive:
   861   assumes f: "simple_function M f" and ae: "AE x. 0 \<le> f x"
   765   assumes f: "simple_function M f" and ae: "AE x in M. 0 \<le> f x"
   862   shows "0 \<le> integral\<^isup>S M f"
   766   shows "0 \<le> integral\<^isup>S M f"
   863 proof -
   767 proof -
   864   have "integral\<^isup>S M (\<lambda>x. 0) \<le> integral\<^isup>S M f"
   768   have "integral\<^isup>S M (\<lambda>x. 0) \<le> integral\<^isup>S M f"
   865     using simple_integral_mono_AE[OF _ f ae] by auto
   769     using simple_integral_mono_AE[OF _ f ae] by auto
   866   then show ?thesis by simp
   770   then show ?thesis by simp
   867 qed
   771 qed
   868 
   772 
   869 section "Continuous positive integration"
   773 section "Continuous positive integration"
   870 
   774 
   871 definition positive_integral_def:
   775 definition positive_integral :: "'a measure \<Rightarrow> ('a \<Rightarrow> ereal) \<Rightarrow> ereal" ("integral\<^isup>P") where
   872   "integral\<^isup>P M f = (SUP g : {g. simple_function M g \<and> g \<le> max 0 \<circ> f}. integral\<^isup>S M g)"
   776   "integral\<^isup>P M f = (SUP g : {g. simple_function M g \<and> g \<le> max 0 \<circ> f}. integral\<^isup>S M g)"
   873 
   777 
   874 syntax
   778 syntax
   875   "_positive_integral" :: "pttrn \<Rightarrow> ereal \<Rightarrow> ('a, 'b) measure_space_scheme \<Rightarrow> ereal" ("\<integral>\<^isup>+ _. _ \<partial>_" [60,61] 110)
   779   "_positive_integral" :: "pttrn \<Rightarrow> ereal \<Rightarrow> 'a measure \<Rightarrow> ereal" ("\<integral>\<^isup>+ _. _ \<partial>_" [60,61] 110)
   876 
   780 
   877 translations
   781 translations
   878   "\<integral>\<^isup>+ x. f \<partial>M" == "CONST integral\<^isup>P M (%x. f)"
   782   "\<integral>\<^isup>+ x. f \<partial>M" == "CONST positive_integral M (%x. f)"
   879 
   783 
   880 lemma (in measure_space) positive_integral_cong_measure:
   784 lemma positive_integral_positive:
   881   assumes "\<And>A. A \<in> sets M \<Longrightarrow> measure N A = \<mu> A" "sets N = sets M" "space N = space M"
       
   882   shows "integral\<^isup>P N f = integral\<^isup>P M f"
       
   883   unfolding positive_integral_def
       
   884   unfolding simple_function_cong_algebra[OF assms(2,3), symmetric]
       
   885   using AE_cong_measure[OF assms]
       
   886   using simple_integral_cong_measure[OF assms]
       
   887   by (auto intro!: SUP_cong)
       
   888 
       
   889 lemma (in measure_space) positive_integral_positive:
       
   890   "0 \<le> integral\<^isup>P M f"
   785   "0 \<le> integral\<^isup>P M f"
   891   by (auto intro!: SUP_upper2[of "\<lambda>x. 0"] simp: positive_integral_def le_fun_def)
   786   by (auto intro!: SUP_upper2[of "\<lambda>x. 0"] simp: positive_integral_def le_fun_def)
   892 
   787 
   893 lemma (in measure_space) positive_integral_def_finite:
   788 lemma positive_integral_not_MInfty[simp]: "integral\<^isup>P M f \<noteq> -\<infinity>"
       
   789   using positive_integral_positive[of M f] by auto
       
   790 
       
   791 lemma positive_integral_def_finite:
   894   "integral\<^isup>P M f = (SUP g : {g. simple_function M g \<and> g \<le> max 0 \<circ> f \<and> range g \<subseteq> {0 ..< \<infinity>}}. integral\<^isup>S M g)"
   792   "integral\<^isup>P M f = (SUP g : {g. simple_function M g \<and> g \<le> max 0 \<circ> f \<and> range g \<subseteq> {0 ..< \<infinity>}}. integral\<^isup>S M g)"
   895     (is "_ = SUPR ?A ?f")
   793     (is "_ = SUPR ?A ?f")
   896   unfolding positive_integral_def
   794   unfolding positive_integral_def
   897 proof (safe intro!: antisym SUP_least)
   795 proof (safe intro!: antisym SUP_least)
   898   fix g assume g: "simple_function M g" "g \<le> max 0 \<circ> f"
   796   fix g assume g: "simple_function M g" "g \<le> max 0 \<circ> f"
   899   let ?G = "{x \<in> space M. \<not> g x \<noteq> \<infinity>}"
   797   let ?G = "{x \<in> space M. \<not> g x \<noteq> \<infinity>}"
   900   note gM = g(1)[THEN borel_measurable_simple_function]
   798   note gM = g(1)[THEN borel_measurable_simple_function]
   901   have \<mu>G_pos: "0 \<le> \<mu> ?G" using gM by auto
   799   have \<mu>G_pos: "0 \<le> (emeasure M) ?G" using gM by auto
   902   let ?g = "\<lambda>y x. if g x = \<infinity> then y else max 0 (g x)"
   800   let ?g = "\<lambda>y x. if g x = \<infinity> then y else max 0 (g x)"
   903   from g gM have g_in_A: "\<And>y. 0 \<le> y \<Longrightarrow> y \<noteq> \<infinity> \<Longrightarrow> ?g y \<in> ?A"
   801   from g gM have g_in_A: "\<And>y. 0 \<le> y \<Longrightarrow> y \<noteq> \<infinity> \<Longrightarrow> ?g y \<in> ?A"
   904     apply (safe intro!: simple_function_max simple_function_If)
   802     apply (safe intro!: simple_function_max simple_function_If)
   905     apply (force simp: max_def le_fun_def split: split_if_asm)+
   803     apply (force simp: max_def le_fun_def split: split_if_asm)+
   906     done
   804     done
   907   show "integral\<^isup>S M g \<le> SUPR ?A ?f"
   805   show "integral\<^isup>S M g \<le> SUPR ?A ?f"
   908   proof cases
   806   proof cases
   909     have g0: "?g 0 \<in> ?A" by (intro g_in_A) auto
   807     have g0: "?g 0 \<in> ?A" by (intro g_in_A) auto
   910     assume "\<mu> ?G = 0"
   808     assume "(emeasure M) ?G = 0"
   911     with gM have "AE x. x \<notin> ?G" by (simp add: AE_iff_null_set)
   809     with gM have "AE x in M. x \<notin> ?G"
       
   810       by (auto simp add: AE_iff_null intro!: null_setsI)
   912     with gM g show ?thesis
   811     with gM g show ?thesis
   913       by (intro SUP_upper2[OF g0] simple_integral_mono_AE)
   812       by (intro SUP_upper2[OF g0] simple_integral_mono_AE)
   914          (auto simp: max_def intro!: simple_function_If)
   813          (auto simp: max_def intro!: simple_function_If)
   915   next
   814   next
   916     assume \<mu>G: "\<mu> ?G \<noteq> 0"
   815     assume \<mu>G: "(emeasure M) ?G \<noteq> 0"
   917     have "SUPR ?A (integral\<^isup>S M) = \<infinity>"
   816     have "SUPR ?A (integral\<^isup>S M) = \<infinity>"
   918     proof (intro SUP_PInfty)
   817     proof (intro SUP_PInfty)
   919       fix n :: nat
   818       fix n :: nat
   920       let ?y = "ereal (real n) / (if \<mu> ?G = \<infinity> then 1 else \<mu> ?G)"
   819       let ?y = "ereal (real n) / (if (emeasure M) ?G = \<infinity> then 1 else (emeasure M) ?G)"
   921       have "0 \<le> ?y" "?y \<noteq> \<infinity>" using \<mu>G \<mu>G_pos by (auto simp: ereal_divide_eq)
   820       have "0 \<le> ?y" "?y \<noteq> \<infinity>" using \<mu>G \<mu>G_pos by (auto simp: ereal_divide_eq)
   922       then have "?g ?y \<in> ?A" by (rule g_in_A)
   821       then have "?g ?y \<in> ?A" by (rule g_in_A)
   923       have "real n \<le> ?y * \<mu> ?G"
   822       have "real n \<le> ?y * (emeasure M) ?G"
   924         using \<mu>G \<mu>G_pos by (cases "\<mu> ?G") (auto simp: field_simps)
   823         using \<mu>G \<mu>G_pos by (cases "(emeasure M) ?G") (auto simp: field_simps)
   925       also have "\<dots> = (\<integral>\<^isup>Sx. ?y * indicator ?G x \<partial>M)"
   824       also have "\<dots> = (\<integral>\<^isup>Sx. ?y * indicator ?G x \<partial>M)"
   926         using `0 \<le> ?y` `?g ?y \<in> ?A` gM
   825         using `0 \<le> ?y` `?g ?y \<in> ?A` gM
   927         by (subst simple_integral_cmult_indicator) auto
   826         by (subst simple_integral_cmult_indicator) auto
   928       also have "\<dots> \<le> integral\<^isup>S M (?g ?y)" using `?g ?y \<in> ?A` gM
   827       also have "\<dots> \<le> integral\<^isup>S M (?g ?y)" using `?g ?y \<in> ?A` gM
   929         by (intro simple_integral_mono) auto
   828         by (intro simple_integral_mono) auto
   932     qed
   831     qed
   933     then show ?thesis by simp
   832     then show ?thesis by simp
   934   qed
   833   qed
   935 qed (auto intro: SUP_upper)
   834 qed (auto intro: SUP_upper)
   936 
   835 
   937 lemma (in measure_space) positive_integral_mono_AE:
   836 lemma positive_integral_mono_AE:
   938   assumes ae: "AE x. u x \<le> v x" shows "integral\<^isup>P M u \<le> integral\<^isup>P M v"
   837   assumes ae: "AE x in M. u x \<le> v x" shows "integral\<^isup>P M u \<le> integral\<^isup>P M v"
   939   unfolding positive_integral_def
   838   unfolding positive_integral_def
   940 proof (safe intro!: SUP_mono)
   839 proof (safe intro!: SUP_mono)
   941   fix n assume n: "simple_function M n" "n \<le> max 0 \<circ> u"
   840   fix n assume n: "simple_function M n" "n \<le> max 0 \<circ> u"
   942   from ae[THEN AE_E] guess N . note N = this
   841   from ae[THEN AE_E] guess N . note N = this
   943   then have ae_N: "AE x. x \<notin> N" by (auto intro: AE_not_in)
   842   then have ae_N: "AE x in M. x \<notin> N" by (auto intro: AE_not_in)
   944   let ?n = "\<lambda>x. n x * indicator (space M - N) x"
   843   let ?n = "\<lambda>x. n x * indicator (space M - N) x"
   945   have "AE x. n x \<le> ?n x" "simple_function M ?n"
   844   have "AE x in M. n x \<le> ?n x" "simple_function M ?n"
   946     using n N ae_N by auto
   845     using n N ae_N by auto
   947   moreover
   846   moreover
   948   { fix x have "?n x \<le> max 0 (v x)"
   847   { fix x have "?n x \<le> max 0 (v x)"
   949     proof cases
   848     proof cases
   950       assume x: "x \<in> space M - N"
   849       assume x: "x \<in> space M - N"
   957     using ae_N N n by (auto intro!: simple_integral_mono_AE)
   856     using ae_N N n by (auto intro!: simple_integral_mono_AE)
   958   ultimately show "\<exists>m\<in>{g. simple_function M g \<and> g \<le> max 0 \<circ> v}. integral\<^isup>S M n \<le> integral\<^isup>S M m"
   857   ultimately show "\<exists>m\<in>{g. simple_function M g \<and> g \<le> max 0 \<circ> v}. integral\<^isup>S M n \<le> integral\<^isup>S M m"
   959     by force
   858     by force
   960 qed
   859 qed
   961 
   860 
   962 lemma (in measure_space) positive_integral_mono:
   861 lemma positive_integral_mono:
   963   "(\<And>x. x \<in> space M \<Longrightarrow> u x \<le> v x) \<Longrightarrow> integral\<^isup>P M u \<le> integral\<^isup>P M v"
   862   "(\<And>x. x \<in> space M \<Longrightarrow> u x \<le> v x) \<Longrightarrow> integral\<^isup>P M u \<le> integral\<^isup>P M v"
   964   by (auto intro: positive_integral_mono_AE)
   863   by (auto intro: positive_integral_mono_AE)
   965 
   864 
   966 lemma (in measure_space) positive_integral_cong_AE:
   865 lemma positive_integral_cong_AE:
   967   "AE x. u x = v x \<Longrightarrow> integral\<^isup>P M u = integral\<^isup>P M v"
   866   "AE x in M. u x = v x \<Longrightarrow> integral\<^isup>P M u = integral\<^isup>P M v"
   968   by (auto simp: eq_iff intro!: positive_integral_mono_AE)
   867   by (auto simp: eq_iff intro!: positive_integral_mono_AE)
   969 
   868 
   970 lemma (in measure_space) positive_integral_cong:
   869 lemma positive_integral_cong:
   971   "(\<And>x. x \<in> space M \<Longrightarrow> u x = v x) \<Longrightarrow> integral\<^isup>P M u = integral\<^isup>P M v"
   870   "(\<And>x. x \<in> space M \<Longrightarrow> u x = v x) \<Longrightarrow> integral\<^isup>P M u = integral\<^isup>P M v"
   972   by (auto intro: positive_integral_cong_AE)
   871   by (auto intro: positive_integral_cong_AE)
   973 
   872 
   974 lemma (in measure_space) positive_integral_eq_simple_integral:
   873 lemma positive_integral_eq_simple_integral:
   975   assumes f: "simple_function M f" "\<And>x. 0 \<le> f x" shows "integral\<^isup>P M f = integral\<^isup>S M f"
   874   assumes f: "simple_function M f" "\<And>x. 0 \<le> f x" shows "integral\<^isup>P M f = integral\<^isup>S M f"
   976 proof -
   875 proof -
   977   let ?f = "\<lambda>x. f x * indicator (space M) x"
   876   let ?f = "\<lambda>x. f x * indicator (space M) x"
   978   have f': "simple_function M ?f" using f by auto
   877   have f': "simple_function M ?f" using f by auto
   979   with f(2) have [simp]: "max 0 \<circ> ?f = ?f"
   878   with f(2) have [simp]: "max 0 \<circ> ?f = ?f"
   985     using f' by (auto intro!: SUP_upper)
   884     using f' by (auto intro!: SUP_upper)
   986   ultimately show ?thesis
   885   ultimately show ?thesis
   987     by (simp cong: positive_integral_cong simple_integral_cong)
   886     by (simp cong: positive_integral_cong simple_integral_cong)
   988 qed
   887 qed
   989 
   888 
   990 lemma (in measure_space) positive_integral_eq_simple_integral_AE:
   889 lemma positive_integral_eq_simple_integral_AE:
   991   assumes f: "simple_function M f" "AE x. 0 \<le> f x" shows "integral\<^isup>P M f = integral\<^isup>S M f"
   890   assumes f: "simple_function M f" "AE x in M. 0 \<le> f x" shows "integral\<^isup>P M f = integral\<^isup>S M f"
   992 proof -
   891 proof -
   993   have "AE x. f x = max 0 (f x)" using f by (auto split: split_max)
   892   have "AE x in M. f x = max 0 (f x)" using f by (auto split: split_max)
   994   with f have "integral\<^isup>P M f = integral\<^isup>S M (\<lambda>x. max 0 (f x))"
   893   with f have "integral\<^isup>P M f = integral\<^isup>S M (\<lambda>x. max 0 (f x))"
   995     by (simp cong: positive_integral_cong_AE simple_integral_cong_AE
   894     by (simp cong: positive_integral_cong_AE simple_integral_cong_AE
   996              add: positive_integral_eq_simple_integral)
   895              add: positive_integral_eq_simple_integral)
   997   with assms show ?thesis
   896   with assms show ?thesis
   998     by (auto intro!: simple_integral_cong_AE split: split_max)
   897     by (auto intro!: simple_integral_cong_AE split: split_max)
   999 qed
   898 qed
  1000 
   899 
  1001 lemma (in measure_space) positive_integral_SUP_approx:
   900 lemma positive_integral_SUP_approx:
  1002   assumes f: "incseq f" "\<And>i. f i \<in> borel_measurable M" "\<And>i x. 0 \<le> f i x"
   901   assumes f: "incseq f" "\<And>i. f i \<in> borel_measurable M" "\<And>i x. 0 \<le> f i x"
  1003   and u: "simple_function M u" "u \<le> (SUP i. f i)" "u`space M \<subseteq> {0..<\<infinity>}"
   902   and u: "simple_function M u" "u \<le> (SUP i. f i)" "u`space M \<subseteq> {0..<\<infinity>}"
  1004   shows "integral\<^isup>S M u \<le> (SUP i. integral\<^isup>P M (f i))" (is "_ \<le> ?S")
   903   shows "integral\<^isup>S M u \<le> (SUP i. integral\<^isup>P M (f i))" (is "_ \<le> ?S")
  1005 proof (rule ereal_le_mult_one_interval)
   904 proof (rule ereal_le_mult_one_interval)
  1006   have "0 \<le> (SUP i. integral\<^isup>P M (f i))"
   905   have "0 \<le> (SUP i. integral\<^isup>P M (f i))"
  1026   note B_mono = this
   925   note B_mono = this
  1027 
   926 
  1028   note B_u = Int[OF u(1)[THEN simple_functionD(2)] B]
   927   note B_u = Int[OF u(1)[THEN simple_functionD(2)] B]
  1029 
   928 
  1030   let ?B' = "\<lambda>i n. (u -` {i} \<inter> space M) \<inter> ?B n"
   929   let ?B' = "\<lambda>i n. (u -` {i} \<inter> space M) \<inter> ?B n"
  1031   have measure_conv: "\<And>i. \<mu> (u -` {i} \<inter> space M) = (SUP n. \<mu> (?B' i n))"
   930   have measure_conv: "\<And>i. (emeasure M) (u -` {i} \<inter> space M) = (SUP n. (emeasure M) (?B' i n))"
  1032   proof -
   931   proof -
  1033     fix i
   932     fix i
  1034     have 1: "range (?B' i) \<subseteq> sets M" using B_u by auto
   933     have 1: "range (?B' i) \<subseteq> sets M" using B_u by auto
  1035     have 2: "incseq (?B' i)" using B_mono by (auto intro!: incseq_SucI)
   934     have 2: "incseq (?B' i)" using B_mono by (auto intro!: incseq_SucI)
  1036     have "(\<Union>n. ?B' i n) = u -` {i} \<inter> space M"
   935     have "(\<Union>n. ?B' i n) = u -` {i} \<inter> space M"
  1049           by (auto simp add: less_Sup_iff)
   948           by (auto simp add: less_Sup_iff)
  1050         hence "a * u x \<le> f i x" by auto
   949         hence "a * u x \<le> f i x" by auto
  1051         thus ?thesis using `x \<in> space M` by auto
   950         thus ?thesis using `x \<in> space M` by auto
  1052       qed
   951       qed
  1053     qed
   952     qed
  1054     then show "?thesis i" using continuity_from_below[OF 1 2] by simp
   953     then show "?thesis i" using SUP_emeasure_incseq[OF 1 2] by simp
  1055   qed
   954   qed
  1056 
   955 
  1057   have "integral\<^isup>S M u = (SUP i. integral\<^isup>S M (?uB i))"
   956   have "integral\<^isup>S M u = (SUP i. integral\<^isup>S M (?uB i))"
  1058     unfolding simple_integral_indicator[OF B `simple_function M u`]
   957     unfolding simple_integral_indicator[OF B `simple_function M u`]
  1059   proof (subst SUPR_ereal_setsum, safe)
   958   proof (subst SUPR_ereal_setsum, safe)
  1060     fix x n assume "x \<in> space M"
   959     fix x n assume "x \<in> space M"
  1061     with u_range show "incseq (\<lambda>i. u x * \<mu> (?B' (u x) i))" "\<And>i. 0 \<le> u x * \<mu> (?B' (u x) i)"
   960     with u_range show "incseq (\<lambda>i. u x * (emeasure M) (?B' (u x) i))" "\<And>i. 0 \<le> u x * (emeasure M) (?B' (u x) i)"
  1062       using B_mono B_u by (auto intro!: measure_mono ereal_mult_left_mono incseq_SucI simp: ereal_zero_le_0_iff)
   961       using B_mono B_u by (auto intro!: emeasure_mono ereal_mult_left_mono incseq_SucI simp: ereal_zero_le_0_iff)
  1063   next
   962   next
  1064     show "integral\<^isup>S M u = (\<Sum>i\<in>u ` space M. SUP n. i * \<mu> (?B' i n))"
   963     show "integral\<^isup>S M u = (\<Sum>i\<in>u ` space M. SUP n. i * (emeasure M) (?B' i n))"
  1065       using measure_conv u_range B_u unfolding simple_integral_def
   964       using measure_conv u_range B_u unfolding simple_integral_def
  1066       by (auto intro!: setsum_cong SUPR_ereal_cmult[symmetric])
   965       by (auto intro!: setsum_cong SUPR_ereal_cmult[symmetric])
  1067   qed
   966   qed
  1068   moreover
   967   moreover
  1069   have "a * (SUP i. integral\<^isup>S M (?uB i)) \<le> ?S"
   968   have "a * (SUP i. integral\<^isup>S M (?uB i)) \<le> ?S"
  1087       by (intro simple_integral_positive) (auto split: split_indicator)
   986       by (intro simple_integral_positive) (auto split: split_indicator)
  1088   qed (insert `0 < a`, auto)
   987   qed (insert `0 < a`, auto)
  1089   ultimately show "a * integral\<^isup>S M u \<le> ?S" by simp
   988   ultimately show "a * integral\<^isup>S M u \<le> ?S" by simp
  1090 qed
   989 qed
  1091 
   990 
  1092 lemma (in measure_space) incseq_positive_integral:
   991 lemma incseq_positive_integral:
  1093   assumes "incseq f" shows "incseq (\<lambda>i. integral\<^isup>P M (f i))"
   992   assumes "incseq f" shows "incseq (\<lambda>i. integral\<^isup>P M (f i))"
  1094 proof -
   993 proof -
  1095   have "\<And>i x. f i x \<le> f (Suc i) x"
   994   have "\<And>i x. f i x \<le> f (Suc i) x"
  1096     using assms by (auto dest!: incseq_SucD simp: le_fun_def)
   995     using assms by (auto dest!: incseq_SucD simp: le_fun_def)
  1097   then show ?thesis
   996   then show ?thesis
  1098     by (auto intro!: incseq_SucI positive_integral_mono)
   997     by (auto intro!: incseq_SucI positive_integral_mono)
  1099 qed
   998 qed
  1100 
   999 
  1101 text {* Beppo-Levi monotone convergence theorem *}
  1000 text {* Beppo-Levi monotone convergence theorem *}
  1102 lemma (in measure_space) positive_integral_monotone_convergence_SUP:
  1001 lemma positive_integral_monotone_convergence_SUP:
  1103   assumes f: "incseq f" "\<And>i. f i \<in> borel_measurable M" "\<And>i x. 0 \<le> f i x"
  1002   assumes f: "incseq f" "\<And>i. f i \<in> borel_measurable M" "\<And>i x. 0 \<le> f i x"
  1104   shows "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) = (SUP i. integral\<^isup>P M (f i))"
  1003   shows "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) = (SUP i. integral\<^isup>P M (f i))"
  1105 proof (rule antisym)
  1004 proof (rule antisym)
  1106   show "(SUP j. integral\<^isup>P M (f j)) \<le> (\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M)"
  1005   show "(SUP j. integral\<^isup>P M (f j)) \<le> (\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M)"
  1107     by (auto intro!: SUP_least SUP_upper positive_integral_mono)
  1006     by (auto intro!: SUP_least SUP_upper positive_integral_mono)
  1108 next
  1007 next
  1109   show "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) \<le> (SUP j. integral\<^isup>P M (f j))"
  1008   show "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) \<le> (SUP j. integral\<^isup>P M (f j))"
  1110     unfolding positive_integral_def_finite[of "\<lambda>x. SUP i. f i x"]
  1009     unfolding positive_integral_def_finite[of _ "\<lambda>x. SUP i. f i x"]
  1111   proof (safe intro!: SUP_least)
  1010   proof (safe intro!: SUP_least)
  1112     fix g assume g: "simple_function M g"
  1011     fix g assume g: "simple_function M g"
  1113       and "g \<le> max 0 \<circ> (\<lambda>x. SUP i. f i x)" "range g \<subseteq> {0..<\<infinity>}"
  1012       and "g \<le> max 0 \<circ> (\<lambda>x. SUP i. f i x)" "range g \<subseteq> {0..<\<infinity>}"
  1114     moreover then have "\<And>x. 0 \<le> (SUP i. f i x)" and g': "g`space M \<subseteq> {0..<\<infinity>}"
  1013     moreover then have "\<And>x. 0 \<le> (SUP i. f i x)" and g': "g`space M \<subseteq> {0..<\<infinity>}"
  1115       using f by (auto intro!: SUP_upper2)
  1014       using f by (auto intro!: SUP_upper2)
  1117       by (intro  positive_integral_SUP_approx[OF f g _ g'])
  1016       by (intro  positive_integral_SUP_approx[OF f g _ g'])
  1118          (auto simp: le_fun_def max_def)
  1017          (auto simp: le_fun_def max_def)
  1119   qed
  1018   qed
  1120 qed
  1019 qed
  1121 
  1020 
  1122 lemma (in measure_space) positive_integral_monotone_convergence_SUP_AE:
  1021 lemma positive_integral_monotone_convergence_SUP_AE:
  1123   assumes f: "\<And>i. AE x. f i x \<le> f (Suc i) x \<and> 0 \<le> f i x" "\<And>i. f i \<in> borel_measurable M"
  1022   assumes f: "\<And>i. AE x in M. f i x \<le> f (Suc i) x \<and> 0 \<le> f i x" "\<And>i. f i \<in> borel_measurable M"
  1124   shows "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) = (SUP i. integral\<^isup>P M (f i))"
  1023   shows "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) = (SUP i. integral\<^isup>P M (f i))"
  1125 proof -
  1024 proof -
  1126   from f have "AE x. \<forall>i. f i x \<le> f (Suc i) x \<and> 0 \<le> f i x"
  1025   from f have "AE x in M. \<forall>i. f i x \<le> f (Suc i) x \<and> 0 \<le> f i x"
  1127     by (simp add: AE_all_countable)
  1026     by (simp add: AE_all_countable)
  1128   from this[THEN AE_E] guess N . note N = this
  1027   from this[THEN AE_E] guess N . note N = this
  1129   let ?f = "\<lambda>i x. if x \<in> space M - N then f i x else 0"
  1028   let ?f = "\<lambda>i x. if x \<in> space M - N then f i x else 0"
  1130   have f_eq: "AE x. \<forall>i. ?f i x = f i x" using N by (auto intro!: AE_I[of _ N])
  1029   have f_eq: "AE x in M. \<forall>i. ?f i x = f i x" using N by (auto intro!: AE_I[of _ _ N])
  1131   then have "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) = (\<integral>\<^isup>+ x. (SUP i. ?f i x) \<partial>M)"
  1030   then have "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) = (\<integral>\<^isup>+ x. (SUP i. ?f i x) \<partial>M)"
  1132     by (auto intro!: positive_integral_cong_AE)
  1031     by (auto intro!: positive_integral_cong_AE)
  1133   also have "\<dots> = (SUP i. (\<integral>\<^isup>+ x. ?f i x \<partial>M))"
  1032   also have "\<dots> = (SUP i. (\<integral>\<^isup>+ x. ?f i x \<partial>M))"
  1134   proof (rule positive_integral_monotone_convergence_SUP)
  1033   proof (rule positive_integral_monotone_convergence_SUP)
  1135     show "incseq ?f" using N(1) by (force intro!: incseq_SucI le_funI)
  1034     show "incseq ?f" using N(1) by (force intro!: incseq_SucI le_funI)
  1141   also have "\<dots> = (SUP i. (\<integral>\<^isup>+ x. f i x \<partial>M))"
  1040   also have "\<dots> = (SUP i. (\<integral>\<^isup>+ x. f i x \<partial>M))"
  1142     using f_eq by (force intro!: arg_cong[where f="SUPR UNIV"] positive_integral_cong_AE ext)
  1041     using f_eq by (force intro!: arg_cong[where f="SUPR UNIV"] positive_integral_cong_AE ext)
  1143   finally show ?thesis .
  1042   finally show ?thesis .
  1144 qed
  1043 qed
  1145 
  1044 
  1146 lemma (in measure_space) positive_integral_monotone_convergence_SUP_AE_incseq:
  1045 lemma positive_integral_monotone_convergence_SUP_AE_incseq:
  1147   assumes f: "incseq f" "\<And>i. AE x. 0 \<le> f i x" and borel: "\<And>i. f i \<in> borel_measurable M"
  1046   assumes f: "incseq f" "\<And>i. AE x in M. 0 \<le> f i x" and borel: "\<And>i. f i \<in> borel_measurable M"
  1148   shows "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) = (SUP i. integral\<^isup>P M (f i))"
  1047   shows "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) = (SUP i. integral\<^isup>P M (f i))"
  1149   using f[unfolded incseq_Suc_iff le_fun_def]
  1048   using f[unfolded incseq_Suc_iff le_fun_def]
  1150   by (intro positive_integral_monotone_convergence_SUP_AE[OF _ borel])
  1049   by (intro positive_integral_monotone_convergence_SUP_AE[OF _ borel])
  1151      auto
  1050      auto
  1152 
  1051 
  1153 lemma (in measure_space) positive_integral_monotone_convergence_simple:
  1052 lemma positive_integral_monotone_convergence_simple:
  1154   assumes f: "incseq f" "\<And>i x. 0 \<le> f i x" "\<And>i. simple_function M (f i)"
  1053   assumes f: "incseq f" "\<And>i x. 0 \<le> f i x" "\<And>i. simple_function M (f i)"
  1155   shows "(SUP i. integral\<^isup>S M (f i)) = (\<integral>\<^isup>+x. (SUP i. f i x) \<partial>M)"
  1054   shows "(SUP i. integral\<^isup>S M (f i)) = (\<integral>\<^isup>+x. (SUP i. f i x) \<partial>M)"
  1156   using assms unfolding positive_integral_monotone_convergence_SUP[OF f(1)
  1055   using assms unfolding positive_integral_monotone_convergence_SUP[OF f(1)
  1157     f(3)[THEN borel_measurable_simple_function] f(2)]
  1056     f(3)[THEN borel_measurable_simple_function] f(2)]
  1158   by (auto intro!: positive_integral_eq_simple_integral[symmetric] arg_cong[where f="SUPR UNIV"] ext)
  1057   by (auto intro!: positive_integral_eq_simple_integral[symmetric] arg_cong[where f="SUPR UNIV"] ext)
  1159 
  1058 
  1160 lemma positive_integral_max_0:
  1059 lemma positive_integral_max_0:
  1161   "(\<integral>\<^isup>+x. max 0 (f x) \<partial>M) = integral\<^isup>P M f"
  1060   "(\<integral>\<^isup>+x. max 0 (f x) \<partial>M) = integral\<^isup>P M f"
  1162   by (simp add: le_fun_def positive_integral_def)
  1061   by (simp add: le_fun_def positive_integral_def)
  1163 
  1062 
  1164 lemma (in measure_space) positive_integral_cong_pos:
  1063 lemma positive_integral_cong_pos:
  1165   assumes "\<And>x. x \<in> space M \<Longrightarrow> f x \<le> 0 \<and> g x \<le> 0 \<or> f x = g x"
  1064   assumes "\<And>x. x \<in> space M \<Longrightarrow> f x \<le> 0 \<and> g x \<le> 0 \<or> f x = g x"
  1166   shows "integral\<^isup>P M f = integral\<^isup>P M g"
  1065   shows "integral\<^isup>P M f = integral\<^isup>P M g"
  1167 proof -
  1066 proof -
  1168   have "integral\<^isup>P M (\<lambda>x. max 0 (f x)) = integral\<^isup>P M (\<lambda>x. max 0 (g x))"
  1067   have "integral\<^isup>P M (\<lambda>x. max 0 (f x)) = integral\<^isup>P M (\<lambda>x. max 0 (g x))"
  1169   proof (intro positive_integral_cong)
  1068   proof (intro positive_integral_cong)
  1172       by (auto split: split_max)
  1071       by (auto split: split_max)
  1173   qed
  1072   qed
  1174   then show ?thesis by (simp add: positive_integral_max_0)
  1073   then show ?thesis by (simp add: positive_integral_max_0)
  1175 qed
  1074 qed
  1176 
  1075 
  1177 lemma (in measure_space) SUP_simple_integral_sequences:
  1076 lemma SUP_simple_integral_sequences:
  1178   assumes f: "incseq f" "\<And>i x. 0 \<le> f i x" "\<And>i. simple_function M (f i)"
  1077   assumes f: "incseq f" "\<And>i x. 0 \<le> f i x" "\<And>i. simple_function M (f i)"
  1179   and g: "incseq g" "\<And>i x. 0 \<le> g i x" "\<And>i. simple_function M (g i)"
  1078   and g: "incseq g" "\<And>i x. 0 \<le> g i x" "\<And>i. simple_function M (g i)"
  1180   and eq: "AE x. (SUP i. f i x) = (SUP i. g i x)"
  1079   and eq: "AE x in M. (SUP i. f i x) = (SUP i. g i x)"
  1181   shows "(SUP i. integral\<^isup>S M (f i)) = (SUP i. integral\<^isup>S M (g i))"
  1080   shows "(SUP i. integral\<^isup>S M (f i)) = (SUP i. integral\<^isup>S M (g i))"
  1182     (is "SUPR _ ?F = SUPR _ ?G")
  1081     (is "SUPR _ ?F = SUPR _ ?G")
  1183 proof -
  1082 proof -
  1184   have "(SUP i. integral\<^isup>S M (f i)) = (\<integral>\<^isup>+x. (SUP i. f i x) \<partial>M)"
  1083   have "(SUP i. integral\<^isup>S M (f i)) = (\<integral>\<^isup>+x. (SUP i. f i x) \<partial>M)"
  1185     using f by (rule positive_integral_monotone_convergence_simple)
  1084     using f by (rule positive_integral_monotone_convergence_simple)
  1188   also have "\<dots> = (SUP i. ?G i)"
  1087   also have "\<dots> = (SUP i. ?G i)"
  1189     using g by (rule positive_integral_monotone_convergence_simple[symmetric])
  1088     using g by (rule positive_integral_monotone_convergence_simple[symmetric])
  1190   finally show ?thesis by simp
  1089   finally show ?thesis by simp
  1191 qed
  1090 qed
  1192 
  1091 
  1193 lemma (in measure_space) positive_integral_const[simp]:
  1092 lemma positive_integral_const[simp]:
  1194   "0 \<le> c \<Longrightarrow> (\<integral>\<^isup>+ x. c \<partial>M) = c * \<mu> (space M)"
  1093   "0 \<le> c \<Longrightarrow> (\<integral>\<^isup>+ x. c \<partial>M) = c * (emeasure M) (space M)"
  1195   by (subst positive_integral_eq_simple_integral) auto
  1094   by (subst positive_integral_eq_simple_integral) auto
  1196 
  1095 
  1197 lemma (in measure_space) positive_integral_vimage:
  1096 lemma positive_integral_linear:
  1198   assumes T: "sigma_algebra M'" "T \<in> measure_preserving M M'"
       
  1199   and f: "f \<in> borel_measurable M'"
       
  1200   shows "integral\<^isup>P M' f = (\<integral>\<^isup>+ x. f (T x) \<partial>M)"
       
  1201 proof -
       
  1202   interpret T: measure_space M' by (rule measure_space_vimage[OF T])
       
  1203   from T.borel_measurable_implies_simple_function_sequence'[OF f]
       
  1204   guess f' . note f' = this
       
  1205   let ?f = "\<lambda>i x. f' i (T x)"
       
  1206   have inc: "incseq ?f" using f' by (force simp: le_fun_def incseq_def)
       
  1207   have sup: "\<And>x. (SUP i. ?f i x) = max 0 (f (T x))"
       
  1208     using f'(4) .
       
  1209   have sf: "\<And>i. simple_function M (\<lambda>x. f' i (T x))"
       
  1210     using simple_function_vimage[OF T(1) measure_preservingD2[OF T(2)] f'(1)] .
       
  1211   show "integral\<^isup>P M' f = (\<integral>\<^isup>+ x. f (T x) \<partial>M)"
       
  1212     using
       
  1213       T.positive_integral_monotone_convergence_simple[OF f'(2,5,1)]
       
  1214       positive_integral_monotone_convergence_simple[OF inc f'(5) sf]
       
  1215     by (simp add: positive_integral_max_0 simple_integral_vimage[OF T f'(1)] f')
       
  1216 qed
       
  1217 
       
  1218 lemma (in measure_space) positive_integral_linear:
       
  1219   assumes f: "f \<in> borel_measurable M" "\<And>x. 0 \<le> f x" and "0 \<le> a"
  1097   assumes f: "f \<in> borel_measurable M" "\<And>x. 0 \<le> f x" and "0 \<le> a"
  1220   and g: "g \<in> borel_measurable M" "\<And>x. 0 \<le> g x"
  1098   and g: "g \<in> borel_measurable M" "\<And>x. 0 \<le> g x"
  1221   shows "(\<integral>\<^isup>+ x. a * f x + g x \<partial>M) = a * integral\<^isup>P M f + integral\<^isup>P M g"
  1099   shows "(\<integral>\<^isup>+ x. a * f x + g x \<partial>M) = a * integral\<^isup>P M f + integral\<^isup>P M g"
  1222     (is "integral\<^isup>P M ?L = _")
  1100     (is "integral\<^isup>P M ?L = _")
  1223 proof -
  1101 proof -
  1252       then have "(SUP i. a * u i x + v i x) = a * (SUP i. u i x) + (SUP i. v i x)"
  1130       then have "(SUP i. a * u i x + v i x) = a * (SUP i. u i x) + (SUP i. v i x)"
  1253         using `0 \<le> a` u(3) v(3) u(6)[of _ x] v(6)[of _ x]
  1131         using `0 \<le> a` u(3) v(3) u(6)[of _ x] v(6)[of _ x]
  1254         by (subst SUPR_ereal_cmult[symmetric, OF u(6) `0 \<le> a`])
  1132         by (subst SUPR_ereal_cmult[symmetric, OF u(6) `0 \<le> a`])
  1255            (auto intro!: SUPR_ereal_add
  1133            (auto intro!: SUPR_ereal_add
  1256                  simp: incseq_Suc_iff le_fun_def add_mono ereal_mult_left_mono ereal_add_nonneg_nonneg) }
  1134                  simp: incseq_Suc_iff le_fun_def add_mono ereal_mult_left_mono ereal_add_nonneg_nonneg) }
  1257     then show "AE x. (SUP i. l i x) = (SUP i. ?L' i x)"
  1135     then show "AE x in M. (SUP i. l i x) = (SUP i. ?L' i x)"
  1258       unfolding l(5) using `0 \<le> a` u(5) v(5) l(5) f(2) g(2)
  1136       unfolding l(5) using `0 \<le> a` u(5) v(5) l(5) f(2) g(2)
  1259       by (intro AE_I2) (auto split: split_max simp add: ereal_add_nonneg_nonneg)
  1137       by (intro AE_I2) (auto split: split_max simp add: ereal_add_nonneg_nonneg)
  1260   qed
  1138   qed
  1261   also have "\<dots> = (SUP i. a * integral\<^isup>S M (u i) + integral\<^isup>S M (v i))"
  1139   also have "\<dots> = (SUP i. a * integral\<^isup>S M (u i) + integral\<^isup>S M (v i))"
  1262     using u(2, 6) v(2, 6) `0 \<le> a` by (auto intro!: arg_cong[where f="SUPR UNIV"] ext)
  1140     using u(2, 6) v(2, 6) `0 \<le> a` by (auto intro!: arg_cong[where f="SUPR UNIV"] ext)
  1266     apply (subst SUPR_ereal_cmult[symmetric, OF pos(1) `0 \<le> a`])
  1144     apply (subst SUPR_ereal_cmult[symmetric, OF pos(1) `0 \<le> a`])
  1267     apply (subst SUPR_ereal_add[symmetric, OF inc not_MInf]) .
  1145     apply (subst SUPR_ereal_add[symmetric, OF inc not_MInf]) .
  1268   then show ?thesis by (simp add: positive_integral_max_0)
  1146   then show ?thesis by (simp add: positive_integral_max_0)
  1269 qed
  1147 qed
  1270 
  1148 
  1271 lemma (in measure_space) positive_integral_cmult:
  1149 lemma positive_integral_cmult:
  1272   assumes f: "f \<in> borel_measurable M" "AE x. 0 \<le> f x" "0 \<le> c"
  1150   assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x" "0 \<le> c"
  1273   shows "(\<integral>\<^isup>+ x. c * f x \<partial>M) = c * integral\<^isup>P M f"
  1151   shows "(\<integral>\<^isup>+ x. c * f x \<partial>M) = c * integral\<^isup>P M f"
  1274 proof -
  1152 proof -
  1275   have [simp]: "\<And>x. c * max 0 (f x) = max 0 (c * f x)" using `0 \<le> c`
  1153   have [simp]: "\<And>x. c * max 0 (f x) = max 0 (c * f x)" using `0 \<le> c`
  1276     by (auto split: split_max simp: ereal_zero_le_0_iff)
  1154     by (auto split: split_max simp: ereal_zero_le_0_iff)
  1277   have "(\<integral>\<^isup>+ x. c * f x \<partial>M) = (\<integral>\<^isup>+ x. c * max 0 (f x) \<partial>M)"
  1155   have "(\<integral>\<^isup>+ x. c * f x \<partial>M) = (\<integral>\<^isup>+ x. c * max 0 (f x) \<partial>M)"
  1278     by (simp add: positive_integral_max_0)
  1156     by (simp add: positive_integral_max_0)
  1279   then show ?thesis
  1157   then show ?thesis
  1280     using positive_integral_linear[OF _ _ `0 \<le> c`, of "\<lambda>x. max 0 (f x)" "\<lambda>x. 0"] f
  1158     using positive_integral_linear[OF _ _ `0 \<le> c`, of "\<lambda>x. max 0 (f x)" _ "\<lambda>x. 0"] f
  1281     by (auto simp: positive_integral_max_0)
  1159     by (auto simp: positive_integral_max_0)
  1282 qed
  1160 qed
  1283 
  1161 
  1284 lemma (in measure_space) positive_integral_multc:
  1162 lemma positive_integral_multc:
  1285   assumes "f \<in> borel_measurable M" "AE x. 0 \<le> f x" "0 \<le> c"
  1163   assumes "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x" "0 \<le> c"
  1286   shows "(\<integral>\<^isup>+ x. f x * c \<partial>M) = integral\<^isup>P M f * c"
  1164   shows "(\<integral>\<^isup>+ x. f x * c \<partial>M) = integral\<^isup>P M f * c"
  1287   unfolding mult_commute[of _ c] positive_integral_cmult[OF assms] by simp
  1165   unfolding mult_commute[of _ c] positive_integral_cmult[OF assms] by simp
  1288 
  1166 
  1289 lemma (in measure_space) positive_integral_indicator[simp]:
  1167 lemma positive_integral_indicator[simp]:
  1290   "A \<in> sets M \<Longrightarrow> (\<integral>\<^isup>+ x. indicator A x\<partial>M) = \<mu> A"
  1168   "A \<in> sets M \<Longrightarrow> (\<integral>\<^isup>+ x. indicator A x\<partial>M) = (emeasure M) A"
  1291   by (subst positive_integral_eq_simple_integral)
  1169   by (subst positive_integral_eq_simple_integral)
  1292      (auto simp: simple_function_indicator simple_integral_indicator)
  1170      (auto simp: simple_function_indicator simple_integral_indicator)
  1293 
  1171 
  1294 lemma (in measure_space) positive_integral_cmult_indicator:
  1172 lemma positive_integral_cmult_indicator:
  1295   "0 \<le> c \<Longrightarrow> A \<in> sets M \<Longrightarrow> (\<integral>\<^isup>+ x. c * indicator A x \<partial>M) = c * \<mu> A"
  1173   "0 \<le> c \<Longrightarrow> A \<in> sets M \<Longrightarrow> (\<integral>\<^isup>+ x. c * indicator A x \<partial>M) = c * (emeasure M) A"
  1296   by (subst positive_integral_eq_simple_integral)
  1174   by (subst positive_integral_eq_simple_integral)
  1297      (auto simp: simple_function_indicator simple_integral_indicator)
  1175      (auto simp: simple_function_indicator simple_integral_indicator)
  1298 
  1176 
  1299 lemma (in measure_space) positive_integral_add:
  1177 lemma positive_integral_add:
  1300   assumes f: "f \<in> borel_measurable M" "AE x. 0 \<le> f x"
  1178   assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x"
  1301   and g: "g \<in> borel_measurable M" "AE x. 0 \<le> g x"
  1179   and g: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
  1302   shows "(\<integral>\<^isup>+ x. f x + g x \<partial>M) = integral\<^isup>P M f + integral\<^isup>P M g"
  1180   shows "(\<integral>\<^isup>+ x. f x + g x \<partial>M) = integral\<^isup>P M f + integral\<^isup>P M g"
  1303 proof -
  1181 proof -
  1304   have ae: "AE x. max 0 (f x) + max 0 (g x) = max 0 (f x + g x)"
  1182   have ae: "AE x in M. max 0 (f x) + max 0 (g x) = max 0 (f x + g x)"
  1305     using assms by (auto split: split_max simp: ereal_add_nonneg_nonneg)
  1183     using assms by (auto split: split_max simp: ereal_add_nonneg_nonneg)
  1306   have "(\<integral>\<^isup>+ x. f x + g x \<partial>M) = (\<integral>\<^isup>+ x. max 0 (f x + g x) \<partial>M)"
  1184   have "(\<integral>\<^isup>+ x. f x + g x \<partial>M) = (\<integral>\<^isup>+ x. max 0 (f x + g x) \<partial>M)"
  1307     by (simp add: positive_integral_max_0)
  1185     by (simp add: positive_integral_max_0)
  1308   also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (f x) + max 0 (g x) \<partial>M)"
  1186   also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (f x) + max 0 (g x) \<partial>M)"
  1309     unfolding ae[THEN positive_integral_cong_AE] ..
  1187     unfolding ae[THEN positive_integral_cong_AE] ..
  1310   also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (f x) \<partial>M) + (\<integral>\<^isup>+ x. max 0 (g x) \<partial>M)"
  1188   also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (f x) \<partial>M) + (\<integral>\<^isup>+ x. max 0 (g x) \<partial>M)"
  1311     using positive_integral_linear[of "\<lambda>x. max 0 (f x)" 1 "\<lambda>x. max 0 (g x)"] f g
  1189     using positive_integral_linear[of "\<lambda>x. max 0 (f x)" _ 1 "\<lambda>x. max 0 (g x)"] f g
  1312     by auto
  1190     by auto
  1313   finally show ?thesis
  1191   finally show ?thesis
  1314     by (simp add: positive_integral_max_0)
  1192     by (simp add: positive_integral_max_0)
  1315 qed
  1193 qed
  1316 
  1194 
  1317 lemma (in measure_space) positive_integral_setsum:
  1195 lemma positive_integral_setsum:
  1318   assumes "\<And>i. i\<in>P \<Longrightarrow> f i \<in> borel_measurable M" "\<And>i. i\<in>P \<Longrightarrow> AE x. 0 \<le> f i x"
  1196   assumes "\<And>i. i\<in>P \<Longrightarrow> f i \<in> borel_measurable M" "\<And>i. i\<in>P \<Longrightarrow> AE x in M. 0 \<le> f i x"
  1319   shows "(\<integral>\<^isup>+ x. (\<Sum>i\<in>P. f i x) \<partial>M) = (\<Sum>i\<in>P. integral\<^isup>P M (f i))"
  1197   shows "(\<integral>\<^isup>+ x. (\<Sum>i\<in>P. f i x) \<partial>M) = (\<Sum>i\<in>P. integral\<^isup>P M (f i))"
  1320 proof cases
  1198 proof cases
  1321   assume f: "finite P"
  1199   assume f: "finite P"
  1322   from assms have "AE x. \<forall>i\<in>P. 0 \<le> f i x" unfolding AE_finite_all[OF f] by auto
  1200   from assms have "AE x in M. \<forall>i\<in>P. 0 \<le> f i x" unfolding AE_finite_all[OF f] by auto
  1323   from f this assms(1) show ?thesis
  1201   from f this assms(1) show ?thesis
  1324   proof induct
  1202   proof induct
  1325     case (insert i P)
  1203     case (insert i P)
  1326     then have "f i \<in> borel_measurable M" "AE x. 0 \<le> f i x"
  1204     then have "f i \<in> borel_measurable M" "AE x in M. 0 \<le> f i x"
  1327       "(\<lambda>x. \<Sum>i\<in>P. f i x) \<in> borel_measurable M" "AE x. 0 \<le> (\<Sum>i\<in>P. f i x)"
  1205       "(\<lambda>x. \<Sum>i\<in>P. f i x) \<in> borel_measurable M" "AE x in M. 0 \<le> (\<Sum>i\<in>P. f i x)"
  1328       by (auto intro!: borel_measurable_ereal_setsum setsum_nonneg)
  1206       by (auto intro!: borel_measurable_ereal_setsum setsum_nonneg)
  1329     from positive_integral_add[OF this]
  1207     from positive_integral_add[OF this]
  1330     show ?case using insert by auto
  1208     show ?case using insert by auto
  1331   qed simp
  1209   qed simp
  1332 qed simp
  1210 qed simp
  1333 
  1211 
  1334 lemma (in measure_space) positive_integral_Markov_inequality:
  1212 lemma positive_integral_Markov_inequality:
  1335   assumes u: "u \<in> borel_measurable M" "AE x. 0 \<le> u x" and "A \<in> sets M" and c: "0 \<le> c" "c \<noteq> \<infinity>"
  1213   assumes u: "u \<in> borel_measurable M" "AE x in M. 0 \<le> u x" and "A \<in> sets M" and c: "0 \<le> c" "c \<noteq> \<infinity>"
  1336   shows "\<mu> ({x\<in>space M. 1 \<le> c * u x} \<inter> A) \<le> c * (\<integral>\<^isup>+ x. u x * indicator A x \<partial>M)"
  1214   shows "(emeasure M) ({x\<in>space M. 1 \<le> c * u x} \<inter> A) \<le> c * (\<integral>\<^isup>+ x. u x * indicator A x \<partial>M)"
  1337     (is "\<mu> ?A \<le> _ * ?PI")
  1215     (is "(emeasure M) ?A \<le> _ * ?PI")
  1338 proof -
  1216 proof -
  1339   have "?A \<in> sets M"
  1217   have "?A \<in> sets M"
  1340     using `A \<in> sets M` u by auto
  1218     using `A \<in> sets M` u by auto
  1341   hence "\<mu> ?A = (\<integral>\<^isup>+ x. indicator ?A x \<partial>M)"
  1219   hence "(emeasure M) ?A = (\<integral>\<^isup>+ x. indicator ?A x \<partial>M)"
  1342     using positive_integral_indicator by simp
  1220     using positive_integral_indicator by simp
  1343   also have "\<dots> \<le> (\<integral>\<^isup>+ x. c * (u x * indicator A x) \<partial>M)" using u c
  1221   also have "\<dots> \<le> (\<integral>\<^isup>+ x. c * (u x * indicator A x) \<partial>M)" using u c
  1344     by (auto intro!: positive_integral_mono_AE
  1222     by (auto intro!: positive_integral_mono_AE
  1345       simp: indicator_def ereal_zero_le_0_iff)
  1223       simp: indicator_def ereal_zero_le_0_iff)
  1346   also have "\<dots> = c * (\<integral>\<^isup>+ x. u x * indicator A x \<partial>M)"
  1224   also have "\<dots> = c * (\<integral>\<^isup>+ x. u x * indicator A x \<partial>M)"
  1347     using assms
  1225     using assms
  1348     by (auto intro!: positive_integral_cmult borel_measurable_indicator simp: ereal_zero_le_0_iff)
  1226     by (auto intro!: positive_integral_cmult borel_measurable_indicator simp: ereal_zero_le_0_iff)
  1349   finally show ?thesis .
  1227   finally show ?thesis .
  1350 qed
  1228 qed
  1351 
  1229 
  1352 lemma (in measure_space) positive_integral_noteq_infinite:
  1230 lemma positive_integral_noteq_infinite:
  1353   assumes g: "g \<in> borel_measurable M" "AE x. 0 \<le> g x"
  1231   assumes g: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
  1354   and "integral\<^isup>P M g \<noteq> \<infinity>"
  1232   and "integral\<^isup>P M g \<noteq> \<infinity>"
  1355   shows "AE x. g x \<noteq> \<infinity>"
  1233   shows "AE x in M. g x \<noteq> \<infinity>"
  1356 proof (rule ccontr)
  1234 proof (rule ccontr)
  1357   assume c: "\<not> (AE x. g x \<noteq> \<infinity>)"
  1235   assume c: "\<not> (AE x in M. g x \<noteq> \<infinity>)"
  1358   have "\<mu> {x\<in>space M. g x = \<infinity>} \<noteq> 0"
  1236   have "(emeasure M) {x\<in>space M. g x = \<infinity>} \<noteq> 0"
  1359     using c g by (simp add: AE_iff_null_set)
  1237     using c g by (auto simp add: AE_iff_null)
  1360   moreover have "0 \<le> \<mu> {x\<in>space M. g x = \<infinity>}" using g by (auto intro: measurable_sets)
  1238   moreover have "0 \<le> (emeasure M) {x\<in>space M. g x = \<infinity>}" using g by (auto intro: measurable_sets)
  1361   ultimately have "0 < \<mu> {x\<in>space M. g x = \<infinity>}" by auto
  1239   ultimately have "0 < (emeasure M) {x\<in>space M. g x = \<infinity>}" by auto
  1362   then have "\<infinity> = \<infinity> * \<mu> {x\<in>space M. g x = \<infinity>}" by auto
  1240   then have "\<infinity> = \<infinity> * (emeasure M) {x\<in>space M. g x = \<infinity>}" by auto
  1363   also have "\<dots> \<le> (\<integral>\<^isup>+x. \<infinity> * indicator {x\<in>space M. g x = \<infinity>} x \<partial>M)"
  1241   also have "\<dots> \<le> (\<integral>\<^isup>+x. \<infinity> * indicator {x\<in>space M. g x = \<infinity>} x \<partial>M)"
  1364     using g by (subst positive_integral_cmult_indicator) auto
  1242     using g by (subst positive_integral_cmult_indicator) auto
  1365   also have "\<dots> \<le> integral\<^isup>P M g"
  1243   also have "\<dots> \<le> integral\<^isup>P M g"
  1366     using assms by (auto intro!: positive_integral_mono_AE simp: indicator_def)
  1244     using assms by (auto intro!: positive_integral_mono_AE simp: indicator_def)
  1367   finally show False using `integral\<^isup>P M g \<noteq> \<infinity>` by auto
  1245   finally show False using `integral\<^isup>P M g \<noteq> \<infinity>` by auto
  1368 qed
  1246 qed
  1369 
  1247 
  1370 lemma (in measure_space) positive_integral_diff:
  1248 lemma positive_integral_diff:
  1371   assumes f: "f \<in> borel_measurable M"
  1249   assumes f: "f \<in> borel_measurable M"
  1372   and g: "g \<in> borel_measurable M" "AE x. 0 \<le> g x"
  1250   and g: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
  1373   and fin: "integral\<^isup>P M g \<noteq> \<infinity>"
  1251   and fin: "integral\<^isup>P M g \<noteq> \<infinity>"
  1374   and mono: "AE x. g x \<le> f x"
  1252   and mono: "AE x in M. g x \<le> f x"
  1375   shows "(\<integral>\<^isup>+ x. f x - g x \<partial>M) = integral\<^isup>P M f - integral\<^isup>P M g"
  1253   shows "(\<integral>\<^isup>+ x. f x - g x \<partial>M) = integral\<^isup>P M f - integral\<^isup>P M g"
  1376 proof -
  1254 proof -
  1377   have diff: "(\<lambda>x. f x - g x) \<in> borel_measurable M" "AE x. 0 \<le> f x - g x"
  1255   have diff: "(\<lambda>x. f x - g x) \<in> borel_measurable M" "AE x in M. 0 \<le> f x - g x"
  1378     using assms by (auto intro: ereal_diff_positive)
  1256     using assms by (auto intro: ereal_diff_positive)
  1379   have pos_f: "AE x. 0 \<le> f x" using mono g by auto
  1257   have pos_f: "AE x in M. 0 \<le> f x" using mono g by auto
  1380   { fix a b :: ereal assume "0 \<le> a" "a \<noteq> \<infinity>" "0 \<le> b" "a \<le> b" then have "b - a + a = b"
  1258   { fix a b :: ereal assume "0 \<le> a" "a \<noteq> \<infinity>" "0 \<le> b" "a \<le> b" then have "b - a + a = b"
  1381       by (cases rule: ereal2_cases[of a b]) auto }
  1259       by (cases rule: ereal2_cases[of a b]) auto }
  1382   note * = this
  1260   note * = this
  1383   then have "AE x. f x = f x - g x + g x"
  1261   then have "AE x in M. f x = f x - g x + g x"
  1384     using mono positive_integral_noteq_infinite[OF g fin] assms by auto
  1262     using mono positive_integral_noteq_infinite[OF g fin] assms by auto
  1385   then have **: "integral\<^isup>P M f = (\<integral>\<^isup>+x. f x - g x \<partial>M) + integral\<^isup>P M g"
  1263   then have **: "integral\<^isup>P M f = (\<integral>\<^isup>+x. f x - g x \<partial>M) + integral\<^isup>P M g"
  1386     unfolding positive_integral_add[OF diff g, symmetric]
  1264     unfolding positive_integral_add[OF diff g, symmetric]
  1387     by (rule positive_integral_cong_AE)
  1265     by (rule positive_integral_cong_AE)
  1388   show ?thesis unfolding **
  1266   show ?thesis unfolding **
  1389     using fin positive_integral_positive[of g]
  1267     using fin positive_integral_positive[of M g]
  1390     by (cases rule: ereal2_cases[of "\<integral>\<^isup>+ x. f x - g x \<partial>M" "integral\<^isup>P M g"]) auto
  1268     by (cases rule: ereal2_cases[of "\<integral>\<^isup>+ x. f x - g x \<partial>M" "integral\<^isup>P M g"]) auto
  1391 qed
  1269 qed
  1392 
  1270 
  1393 lemma (in measure_space) positive_integral_suminf:
  1271 lemma positive_integral_suminf:
  1394   assumes f: "\<And>i. f i \<in> borel_measurable M" "\<And>i. AE x. 0 \<le> f i x"
  1272   assumes f: "\<And>i. f i \<in> borel_measurable M" "\<And>i. AE x in M. 0 \<le> f i x"
  1395   shows "(\<integral>\<^isup>+ x. (\<Sum>i. f i x) \<partial>M) = (\<Sum>i. integral\<^isup>P M (f i))"
  1273   shows "(\<integral>\<^isup>+ x. (\<Sum>i. f i x) \<partial>M) = (\<Sum>i. integral\<^isup>P M (f i))"
  1396 proof -
  1274 proof -
  1397   have all_pos: "AE x. \<forall>i. 0 \<le> f i x"
  1275   have all_pos: "AE x in M. \<forall>i. 0 \<le> f i x"
  1398     using assms by (auto simp: AE_all_countable)
  1276     using assms by (auto simp: AE_all_countable)
  1399   have "(\<Sum>i. integral\<^isup>P M (f i)) = (SUP n. \<Sum>i<n. integral\<^isup>P M (f i))"
  1277   have "(\<Sum>i. integral\<^isup>P M (f i)) = (SUP n. \<Sum>i<n. integral\<^isup>P M (f i))"
  1400     using positive_integral_positive by (rule suminf_ereal_eq_SUPR)
  1278     using positive_integral_positive by (rule suminf_ereal_eq_SUPR)
  1401   also have "\<dots> = (SUP n. \<integral>\<^isup>+x. (\<Sum>i<n. f i x) \<partial>M)"
  1279   also have "\<dots> = (SUP n. \<integral>\<^isup>+x. (\<Sum>i<n. f i x) \<partial>M)"
  1402     unfolding positive_integral_setsum[OF f] ..
  1280     unfolding positive_integral_setsum[OF f] ..
  1407     by (intro positive_integral_cong_AE) (auto simp: suminf_ereal_eq_SUPR)
  1285     by (intro positive_integral_cong_AE) (auto simp: suminf_ereal_eq_SUPR)
  1408   finally show ?thesis by simp
  1286   finally show ?thesis by simp
  1409 qed
  1287 qed
  1410 
  1288 
  1411 text {* Fatou's lemma: convergence theorem on limes inferior *}
  1289 text {* Fatou's lemma: convergence theorem on limes inferior *}
  1412 lemma (in measure_space) positive_integral_lim_INF:
  1290 lemma positive_integral_lim_INF:
  1413   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1291   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1414   assumes u: "\<And>i. u i \<in> borel_measurable M" "\<And>i. AE x. 0 \<le> u i x"
  1292   assumes u: "\<And>i. u i \<in> borel_measurable M" "\<And>i. AE x in M. 0 \<le> u i x"
  1415   shows "(\<integral>\<^isup>+ x. liminf (\<lambda>n. u n x) \<partial>M) \<le> liminf (\<lambda>n. integral\<^isup>P M (u n))"
  1293   shows "(\<integral>\<^isup>+ x. liminf (\<lambda>n. u n x) \<partial>M) \<le> liminf (\<lambda>n. integral\<^isup>P M (u n))"
  1416 proof -
  1294 proof -
  1417   have pos: "AE x. \<forall>i. 0 \<le> u i x" using u by (auto simp: AE_all_countable)
  1295   have pos: "AE x in M. \<forall>i. 0 \<le> u i x" using u by (auto simp: AE_all_countable)
  1418   have "(\<integral>\<^isup>+ x. liminf (\<lambda>n. u n x) \<partial>M) =
  1296   have "(\<integral>\<^isup>+ x. liminf (\<lambda>n. u n x) \<partial>M) =
  1419     (SUP n. \<integral>\<^isup>+ x. (INF i:{n..}. u i x) \<partial>M)"
  1297     (SUP n. \<integral>\<^isup>+ x. (INF i:{n..}. u i x) \<partial>M)"
  1420     unfolding liminf_SUPR_INFI using pos u
  1298     unfolding liminf_SUPR_INFI using pos u
  1421     by (intro positive_integral_monotone_convergence_SUP_AE)
  1299     by (intro positive_integral_monotone_convergence_SUP_AE)
  1422        (elim AE_mp, auto intro!: AE_I2 intro: INF_greatest INF_superset_mono)
  1300        (elim AE_mp, auto intro!: AE_I2 intro: INF_greatest INF_superset_mono)
  1424     unfolding liminf_SUPR_INFI
  1302     unfolding liminf_SUPR_INFI
  1425     by (auto intro!: SUP_mono exI INF_greatest positive_integral_mono INF_lower)
  1303     by (auto intro!: SUP_mono exI INF_greatest positive_integral_mono INF_lower)
  1426   finally show ?thesis .
  1304   finally show ?thesis .
  1427 qed
  1305 qed
  1428 
  1306 
  1429 lemma (in measure_space) measure_space_density:
  1307 lemma positive_integral_null_set:
  1430   assumes u: "u \<in> borel_measurable M" "AE x. 0 \<le> u x"
  1308   assumes "N \<in> null_sets M" shows "(\<integral>\<^isup>+ x. u x * indicator N x \<partial>M) = 0"
  1431     and M'[simp]: "M' = (M\<lparr>measure := \<lambda>A. (\<integral>\<^isup>+ x. u x * indicator A x \<partial>M)\<rparr>)"
       
  1432   shows "measure_space M'"
       
  1433 proof -
       
  1434   interpret M': sigma_algebra M' by (intro sigma_algebra_cong) auto
       
  1435   show ?thesis
       
  1436   proof
       
  1437     have pos: "\<And>A. AE x. 0 \<le> u x * indicator A x"
       
  1438       using u by (auto simp: ereal_zero_le_0_iff)
       
  1439     then show "positive M' (measure M')" unfolding M'
       
  1440       using u(1) by (auto simp: positive_def intro!: positive_integral_positive)
       
  1441     show "countably_additive M' (measure M')"
       
  1442     proof (intro countably_additiveI)
       
  1443       fix A :: "nat \<Rightarrow> 'a set" assume "range A \<subseteq> sets M'"
       
  1444       then have *: "\<And>i. (\<lambda>x. u x * indicator (A i) x) \<in> borel_measurable M"
       
  1445         using u by (auto intro: borel_measurable_indicator)
       
  1446       assume disj: "disjoint_family A"
       
  1447       have "(\<Sum>n. measure M' (A n)) = (\<integral>\<^isup>+ x. (\<Sum>n. u x * indicator (A n) x) \<partial>M)"
       
  1448         unfolding M' using u(1) *
       
  1449         by (simp add: positive_integral_suminf[OF _ pos, symmetric])
       
  1450       also have "\<dots> = (\<integral>\<^isup>+ x. u x * (\<Sum>n. indicator (A n) x) \<partial>M)" using u
       
  1451         by (intro positive_integral_cong_AE)
       
  1452            (elim AE_mp, auto intro!: AE_I2 suminf_cmult_ereal)
       
  1453       also have "\<dots> = (\<integral>\<^isup>+ x. u x * indicator (\<Union>n. A n) x \<partial>M)"
       
  1454         unfolding suminf_indicator[OF disj] ..
       
  1455       finally show "(\<Sum>n. measure M' (A n)) = measure M' (\<Union>x. A x)"
       
  1456         unfolding M' by simp
       
  1457     qed
       
  1458   qed
       
  1459 qed
       
  1460 
       
  1461 lemma (in measure_space) positive_integral_null_set:
       
  1462   assumes "N \<in> null_sets" shows "(\<integral>\<^isup>+ x. u x * indicator N x \<partial>M) = 0"
       
  1463 proof -
  1309 proof -
  1464   have "(\<integral>\<^isup>+ x. u x * indicator N x \<partial>M) = (\<integral>\<^isup>+ x. 0 \<partial>M)"
  1310   have "(\<integral>\<^isup>+ x. u x * indicator N x \<partial>M) = (\<integral>\<^isup>+ x. 0 \<partial>M)"
  1465   proof (intro positive_integral_cong_AE AE_I)
  1311   proof (intro positive_integral_cong_AE AE_I)
  1466     show "{x \<in> space M. u x * indicator N x \<noteq> 0} \<subseteq> N"
  1312     show "{x \<in> space M. u x * indicator N x \<noteq> 0} \<subseteq> N"
  1467       by (auto simp: indicator_def)
  1313       by (auto simp: indicator_def)
  1468     show "\<mu> N = 0" "N \<in> sets M"
  1314     show "(emeasure M) N = 0" "N \<in> sets M"
  1469       using assms by auto
  1315       using assms by auto
  1470   qed
  1316   qed
  1471   then show ?thesis by simp
  1317   then show ?thesis by simp
  1472 qed
  1318 qed
  1473 
  1319 
  1474 lemma (in measure_space) positive_integral_translated_density:
  1320 lemma positive_integral_0_iff:
  1475   assumes f: "f \<in> borel_measurable M" "AE x. 0 \<le> f x"
  1321   assumes u: "u \<in> borel_measurable M" and pos: "AE x in M. 0 \<le> u x"
  1476   assumes g: "g \<in> borel_measurable M" "AE x. 0 \<le> g x"
  1322   shows "integral\<^isup>P M u = 0 \<longleftrightarrow> emeasure M {x\<in>space M. u x \<noteq> 0} = 0"
  1477     and M': "M' = (M\<lparr> measure := \<lambda>A. (\<integral>\<^isup>+ x. f x * indicator A x \<partial>M)\<rparr>)"
  1323     (is "_ \<longleftrightarrow> (emeasure M) ?A = 0")
  1478   shows "integral\<^isup>P M' g = (\<integral>\<^isup>+ x. f x * g x \<partial>M)"
       
  1479 proof -
       
  1480   from measure_space_density[OF f M']
       
  1481   interpret T: measure_space M' .
       
  1482   have borel[simp]:
       
  1483     "borel_measurable M' = borel_measurable M"
       
  1484     "simple_function M' = simple_function M"
       
  1485     unfolding measurable_def simple_function_def [abs_def] by (auto simp: M')
       
  1486   from borel_measurable_implies_simple_function_sequence'[OF g(1)] guess G . note G = this
       
  1487   note G' = borel_measurable_simple_function[OF this(1)] simple_functionD[OF G(1)]
       
  1488   note G'(2)[simp]
       
  1489   { fix P have "AE x. P x \<Longrightarrow> AE x in M'. P x"
       
  1490       using positive_integral_null_set[of _ f]
       
  1491       unfolding T.almost_everywhere_def almost_everywhere_def
       
  1492       by (auto simp: M') }
       
  1493   note ac = this
       
  1494   from G(4) g(2) have G_M': "AE x in M'. (SUP i. G i x) = g x"
       
  1495     by (auto intro!: ac split: split_max)
       
  1496   { fix i
       
  1497     let ?I = "\<lambda>y x. indicator (G i -` {y} \<inter> space M) x"
       
  1498     { fix x assume *: "x \<in> space M" "0 \<le> f x" "0 \<le> g x"
       
  1499       then have [simp]: "G i ` space M \<inter> {y. G i x = y \<and> x \<in> space M} = {G i x}" by auto
       
  1500       from * G' G have "(\<Sum>y\<in>G i`space M. y * (f x * ?I y x)) = f x * (\<Sum>y\<in>G i`space M. (y * ?I y x))"
       
  1501         by (subst setsum_ereal_right_distrib) (auto simp: ac_simps)
       
  1502       also have "\<dots> = f x * G i x"
       
  1503         by (simp add: indicator_def if_distrib setsum_cases)
       
  1504       finally have "(\<Sum>y\<in>G i`space M. y * (f x * ?I y x)) = f x * G i x" . }
       
  1505     note to_singleton = this
       
  1506     have "integral\<^isup>P M' (G i) = integral\<^isup>S M' (G i)"
       
  1507       using G T.positive_integral_eq_simple_integral by simp
       
  1508     also have "\<dots> = (\<Sum>y\<in>G i`space M. y * (\<integral>\<^isup>+x. f x * ?I y x \<partial>M))"
       
  1509       unfolding simple_integral_def M' by simp
       
  1510     also have "\<dots> = (\<Sum>y\<in>G i`space M. (\<integral>\<^isup>+x. y * (f x * ?I y x) \<partial>M))"
       
  1511       using f G' G by (auto intro!: setsum_cong positive_integral_cmult[symmetric])
       
  1512     also have "\<dots> = (\<integral>\<^isup>+x. (\<Sum>y\<in>G i`space M. y * (f x * ?I y x)) \<partial>M)"
       
  1513       using f G' G by (auto intro!: positive_integral_setsum[symmetric])
       
  1514     finally have "integral\<^isup>P M' (G i) = (\<integral>\<^isup>+x. f x * G i x \<partial>M)"
       
  1515       using f g G' to_singleton by (auto intro!: positive_integral_cong_AE) }
       
  1516   note [simp] = this
       
  1517   have "integral\<^isup>P M' g = (SUP i. integral\<^isup>P M' (G i))" using G'(1) G_M'(1) G
       
  1518     using T.positive_integral_monotone_convergence_SUP[symmetric, OF `incseq G`]
       
  1519     by (simp cong: T.positive_integral_cong_AE)
       
  1520   also have "\<dots> = (SUP i. (\<integral>\<^isup>+x. f x * G i x \<partial>M))" by simp
       
  1521   also have "\<dots> = (\<integral>\<^isup>+x. (SUP i. f x * G i x) \<partial>M)"
       
  1522     using f G' G(2)[THEN incseq_SucD] G
       
  1523     by (intro positive_integral_monotone_convergence_SUP_AE[symmetric])
       
  1524        (auto simp: ereal_mult_left_mono le_fun_def ereal_zero_le_0_iff)
       
  1525   also have "\<dots> = (\<integral>\<^isup>+x. f x * g x \<partial>M)" using f G' G g
       
  1526     by (intro positive_integral_cong_AE)
       
  1527        (auto simp add: SUPR_ereal_cmult split: split_max)
       
  1528   finally show "integral\<^isup>P M' g = (\<integral>\<^isup>+x. f x * g x \<partial>M)" .
       
  1529 qed
       
  1530 
       
  1531 lemma (in measure_space) positive_integral_0_iff:
       
  1532   assumes u: "u \<in> borel_measurable M" and pos: "AE x. 0 \<le> u x"
       
  1533   shows "integral\<^isup>P M u = 0 \<longleftrightarrow> \<mu> {x\<in>space M. u x \<noteq> 0} = 0"
       
  1534     (is "_ \<longleftrightarrow> \<mu> ?A = 0")
       
  1535 proof -
  1324 proof -
  1536   have u_eq: "(\<integral>\<^isup>+ x. u x * indicator ?A x \<partial>M) = integral\<^isup>P M u"
  1325   have u_eq: "(\<integral>\<^isup>+ x. u x * indicator ?A x \<partial>M) = integral\<^isup>P M u"
  1537     by (auto intro!: positive_integral_cong simp: indicator_def)
  1326     by (auto intro!: positive_integral_cong simp: indicator_def)
  1538   show ?thesis
  1327   show ?thesis
  1539   proof
  1328   proof
  1540     assume "\<mu> ?A = 0"
  1329     assume "(emeasure M) ?A = 0"
  1541     with positive_integral_null_set[of ?A u] u
  1330     with positive_integral_null_set[of ?A M u] u
  1542     show "integral\<^isup>P M u = 0" by (simp add: u_eq)
  1331     show "integral\<^isup>P M u = 0" by (simp add: u_eq null_sets_def)
  1543   next
  1332   next
  1544     { fix r :: ereal and n :: nat assume gt_1: "1 \<le> real n * r"
  1333     { fix r :: ereal and n :: nat assume gt_1: "1 \<le> real n * r"
  1545       then have "0 < real n * r" by (cases r) (auto split: split_if_asm simp: one_ereal_def)
  1334       then have "0 < real n * r" by (cases r) (auto split: split_if_asm simp: one_ereal_def)
  1546       then have "0 \<le> r" by (auto simp add: ereal_zero_less_0_iff) }
  1335       then have "0 \<le> r" by (auto simp add: ereal_zero_less_0_iff) }
  1547     note gt_1 = this
  1336     note gt_1 = this
  1548     assume *: "integral\<^isup>P M u = 0"
  1337     assume *: "integral\<^isup>P M u = 0"
  1549     let ?M = "\<lambda>n. {x \<in> space M. 1 \<le> real (n::nat) * u x}"
  1338     let ?M = "\<lambda>n. {x \<in> space M. 1 \<le> real (n::nat) * u x}"
  1550     have "0 = (SUP n. \<mu> (?M n \<inter> ?A))"
  1339     have "0 = (SUP n. (emeasure M) (?M n \<inter> ?A))"
  1551     proof -
  1340     proof -
  1552       { fix n :: nat
  1341       { fix n :: nat
  1553         from positive_integral_Markov_inequality[OF u pos, of ?A "ereal (real n)"]
  1342         from positive_integral_Markov_inequality[OF u pos, of ?A "ereal (real n)"]
  1554         have "\<mu> (?M n \<inter> ?A) \<le> 0" unfolding u_eq * using u by simp
  1343         have "(emeasure M) (?M n \<inter> ?A) \<le> 0" unfolding u_eq * using u by simp
  1555         moreover have "0 \<le> \<mu> (?M n \<inter> ?A)" using u by auto
  1344         moreover have "0 \<le> (emeasure M) (?M n \<inter> ?A)" using u by auto
  1556         ultimately have "\<mu> (?M n \<inter> ?A) = 0" by auto }
  1345         ultimately have "(emeasure M) (?M n \<inter> ?A) = 0" by auto }
  1557       thus ?thesis by simp
  1346       thus ?thesis by simp
  1558     qed
  1347     qed
  1559     also have "\<dots> = \<mu> (\<Union>n. ?M n \<inter> ?A)"
  1348     also have "\<dots> = (emeasure M) (\<Union>n. ?M n \<inter> ?A)"
  1560     proof (safe intro!: continuity_from_below)
  1349     proof (safe intro!: SUP_emeasure_incseq)
  1561       fix n show "?M n \<inter> ?A \<in> sets M"
  1350       fix n show "?M n \<inter> ?A \<in> sets M"
  1562         using u by (auto intro!: Int)
  1351         using u by (auto intro!: Int)
  1563     next
  1352     next
  1564       show "incseq (\<lambda>n. {x \<in> space M. 1 \<le> real n * u x} \<inter> {x \<in> space M. u x \<noteq> 0})"
  1353       show "incseq (\<lambda>n. {x \<in> space M. 1 \<le> real n * u x} \<inter> {x \<in> space M. u x \<noteq> 0})"
  1565       proof (safe intro!: incseq_SucI)
  1354       proof (safe intro!: incseq_SucI)
  1568         also from gt_1[OF this] have "real n * u x \<le> real (Suc n) * u x"
  1357         also from gt_1[OF this] have "real n * u x \<le> real (Suc n) * u x"
  1569           using `0 \<le> u x` by (auto intro!: ereal_mult_right_mono)
  1358           using `0 \<le> u x` by (auto intro!: ereal_mult_right_mono)
  1570         finally show "1 \<le> real (Suc n) * u x" by auto
  1359         finally show "1 \<le> real (Suc n) * u x" by auto
  1571       qed
  1360       qed
  1572     qed
  1361     qed
  1573     also have "\<dots> = \<mu> {x\<in>space M. 0 < u x}"
  1362     also have "\<dots> = (emeasure M) {x\<in>space M. 0 < u x}"
  1574     proof (safe intro!: arg_cong[where f="\<mu>"] dest!: gt_1)
  1363     proof (safe intro!: arg_cong[where f="(emeasure M)"] dest!: gt_1)
  1575       fix x assume "0 < u x" and [simp, intro]: "x \<in> space M"
  1364       fix x assume "0 < u x" and [simp, intro]: "x \<in> space M"
  1576       show "x \<in> (\<Union>n. ?M n \<inter> ?A)"
  1365       show "x \<in> (\<Union>n. ?M n \<inter> ?A)"
  1577       proof (cases "u x")
  1366       proof (cases "u x")
  1578         case (real r) with `0 < u x` have "0 < r" by auto
  1367         case (real r) with `0 < u x` have "0 < r" by auto
  1579         obtain j :: nat where "1 / r \<le> real j" using real_arch_simple ..
  1368         obtain j :: nat where "1 / r \<le> real j" using real_arch_simple ..
  1580         hence "1 / r * r \<le> real j * r" unfolding mult_le_cancel_right using `0 < r` by auto
  1369         hence "1 / r * r \<le> real j * r" unfolding mult_le_cancel_right using `0 < r` by auto
  1581         hence "1 \<le> real j * r" using real `0 < r` by auto
  1370         hence "1 \<le> real j * r" using real `0 < r` by auto
  1582         thus ?thesis using `0 < r` real by (auto simp: one_ereal_def)
  1371         thus ?thesis using `0 < r` real by (auto simp: one_ereal_def)
  1583       qed (insert `0 < u x`, auto)
  1372       qed (insert `0 < u x`, auto)
  1584     qed auto
  1373     qed auto
  1585     finally have "\<mu> {x\<in>space M. 0 < u x} = 0" by simp
  1374     finally have "(emeasure M) {x\<in>space M. 0 < u x} = 0" by simp
  1586     moreover
  1375     moreover
  1587     from pos have "AE x. \<not> (u x < 0)" by auto
  1376     from pos have "AE x in M. \<not> (u x < 0)" by auto
  1588     then have "\<mu> {x\<in>space M. u x < 0} = 0"
  1377     then have "(emeasure M) {x\<in>space M. u x < 0} = 0"
  1589       using AE_iff_null_set u by auto
  1378       using AE_iff_null[of M] u by auto
  1590     moreover have "\<mu> {x\<in>space M. u x \<noteq> 0} = \<mu> {x\<in>space M. u x < 0} + \<mu> {x\<in>space M. 0 < u x}"
  1379     moreover have "(emeasure M) {x\<in>space M. u x \<noteq> 0} = (emeasure M) {x\<in>space M. u x < 0} + (emeasure M) {x\<in>space M. 0 < u x}"
  1591       using u by (subst measure_additive) (auto intro!: arg_cong[where f=\<mu>])
  1380       using u by (subst plus_emeasure) (auto intro!: arg_cong[where f="emeasure M"])
  1592     ultimately show "\<mu> ?A = 0" by simp
  1381     ultimately show "(emeasure M) ?A = 0" by simp
  1593   qed
  1382   qed
  1594 qed
  1383 qed
  1595 
  1384 
  1596 lemma (in measure_space) positive_integral_0_iff_AE:
  1385 lemma positive_integral_0_iff_AE:
  1597   assumes u: "u \<in> borel_measurable M"
  1386   assumes u: "u \<in> borel_measurable M"
  1598   shows "integral\<^isup>P M u = 0 \<longleftrightarrow> (AE x. u x \<le> 0)"
  1387   shows "integral\<^isup>P M u = 0 \<longleftrightarrow> (AE x in M. u x \<le> 0)"
  1599 proof -
  1388 proof -
  1600   have sets: "{x\<in>space M. max 0 (u x) \<noteq> 0} \<in> sets M"
  1389   have sets: "{x\<in>space M. max 0 (u x) \<noteq> 0} \<in> sets M"
  1601     using u by auto
  1390     using u by auto
  1602   from positive_integral_0_iff[of "\<lambda>x. max 0 (u x)"]
  1391   from positive_integral_0_iff[of "\<lambda>x. max 0 (u x)"]
  1603   have "integral\<^isup>P M u = 0 \<longleftrightarrow> (AE x. max 0 (u x) = 0)"
  1392   have "integral\<^isup>P M u = 0 \<longleftrightarrow> (AE x in M. max 0 (u x) = 0)"
  1604     unfolding positive_integral_max_0
  1393     unfolding positive_integral_max_0
  1605     using AE_iff_null_set[OF sets] u by auto
  1394     using AE_iff_null[OF sets] u by auto
  1606   also have "\<dots> \<longleftrightarrow> (AE x. u x \<le> 0)" by (auto split: split_max)
  1395   also have "\<dots> \<longleftrightarrow> (AE x in M. u x \<le> 0)" by (auto split: split_max)
  1607   finally show ?thesis .
  1396   finally show ?thesis .
  1608 qed
  1397 qed
  1609 
  1398 
  1610 lemma (in measure_space) positive_integral_const_If:
  1399 lemma positive_integral_const_If:
  1611   "(\<integral>\<^isup>+x. a \<partial>M) = (if 0 \<le> a then a * \<mu> (space M) else 0)"
  1400   "(\<integral>\<^isup>+x. a \<partial>M) = (if 0 \<le> a then a * (emeasure M) (space M) else 0)"
  1612   by (auto intro!: positive_integral_0_iff_AE[THEN iffD2])
  1401   by (auto intro!: positive_integral_0_iff_AE[THEN iffD2])
  1613 
  1402 
  1614 lemma (in measure_space) positive_integral_restricted:
  1403 lemma positive_integral_subalgebra:
  1615   assumes A: "A \<in> sets M"
       
  1616   shows "integral\<^isup>P (restricted_space A) f = (\<integral>\<^isup>+ x. f x * indicator A x \<partial>M)"
       
  1617     (is "integral\<^isup>P ?R f = integral\<^isup>P M ?f")
       
  1618 proof -
       
  1619   interpret R: measure_space ?R
       
  1620     by (rule restricted_measure_space) fact
       
  1621   let ?I = "\<lambda>g x. g x * indicator A x :: ereal"
       
  1622   show ?thesis
       
  1623     unfolding positive_integral_def
       
  1624     unfolding simple_function_restricted[OF A]
       
  1625     unfolding AE_restricted[OF A]
       
  1626   proof (safe intro!: SUPR_eq)
       
  1627     fix g assume g: "simple_function M (?I g)" and le: "g \<le> max 0 \<circ> f"
       
  1628     show "\<exists>j\<in>{g. simple_function M g \<and> g \<le> max 0 \<circ> ?I f}.
       
  1629       integral\<^isup>S (restricted_space A) g \<le> integral\<^isup>S M j"
       
  1630     proof (safe intro!: bexI[of _ "?I g"])
       
  1631       show "integral\<^isup>S (restricted_space A) g \<le> integral\<^isup>S M (?I g)"
       
  1632         using g A by (simp add: simple_integral_restricted)
       
  1633       show "?I g \<le> max 0 \<circ> ?I f"
       
  1634         using le by (auto simp: le_fun_def max_def indicator_def split: split_if_asm)
       
  1635     qed fact
       
  1636   next
       
  1637     fix g assume g: "simple_function M g" and le: "g \<le> max 0 \<circ> ?I f"
       
  1638     show "\<exists>i\<in>{g. simple_function M (?I g) \<and> g \<le> max 0 \<circ> f}.
       
  1639       integral\<^isup>S M g \<le> integral\<^isup>S (restricted_space A) i"
       
  1640     proof (safe intro!: bexI[of _ "?I g"])
       
  1641       show "?I g \<le> max 0 \<circ> f"
       
  1642         using le by (auto simp: le_fun_def max_def indicator_def split: split_if_asm)
       
  1643       from le have "\<And>x. g x \<le> ?I (?I g) x"
       
  1644         by (auto simp: le_fun_def max_def indicator_def split: split_if_asm)
       
  1645       then show "integral\<^isup>S M g \<le> integral\<^isup>S (restricted_space A) (?I g)"
       
  1646         using A g by (auto intro!: simple_integral_mono simp: simple_integral_restricted)
       
  1647       show "simple_function M (?I (?I g))" using g A by auto
       
  1648     qed
       
  1649   qed
       
  1650 qed
       
  1651 
       
  1652 lemma (in measure_space) positive_integral_subalgebra:
       
  1653   assumes f: "f \<in> borel_measurable N" "AE x in N. 0 \<le> f x"
  1404   assumes f: "f \<in> borel_measurable N" "AE x in N. 0 \<le> f x"
  1654   and N: "sets N \<subseteq> sets M" "space N = space M" "\<And>A. A \<in> sets N \<Longrightarrow> measure N A = \<mu> A"
  1405   and N: "sets N \<subseteq> sets M" "space N = space M" "\<And>A. A \<in> sets N \<Longrightarrow> emeasure N A = emeasure M A"
  1655   and sa: "sigma_algebra N"
       
  1656   shows "integral\<^isup>P N f = integral\<^isup>P M f"
  1406   shows "integral\<^isup>P N f = integral\<^isup>P M f"
  1657 proof -
  1407 proof -
  1658   interpret N: measure_space N using measure_space_subalgebra[OF sa N] .
  1408   from borel_measurable_implies_simple_function_sequence'[OF f(1)] guess fs . note fs = this
  1659   from N.borel_measurable_implies_simple_function_sequence'[OF f(1)] guess fs . note fs = this
       
  1660   note sf = simple_function_subalgebra[OF fs(1) N(1,2)]
  1409   note sf = simple_function_subalgebra[OF fs(1) N(1,2)]
  1661   from N.positive_integral_monotone_convergence_simple[OF fs(2,5,1), symmetric]
  1410   from positive_integral_monotone_convergence_simple[OF fs(2,5,1), symmetric]
  1662   have "integral\<^isup>P N f = (SUP i. \<Sum>x\<in>fs i ` space M. x * N.\<mu> (fs i -` {x} \<inter> space M))"
  1411   have "integral\<^isup>P N f = (SUP i. \<Sum>x\<in>fs i ` space M. x * emeasure N (fs i -` {x} \<inter> space M))"
  1663     unfolding fs(4) positive_integral_max_0
  1412     unfolding fs(4) positive_integral_max_0
  1664     unfolding simple_integral_def `space N = space M` by simp
  1413     unfolding simple_integral_def `space N = space M` by simp
  1665   also have "\<dots> = (SUP i. \<Sum>x\<in>fs i ` space M. x * \<mu> (fs i -` {x} \<inter> space M))"
  1414   also have "\<dots> = (SUP i. \<Sum>x\<in>fs i ` space M. x * (emeasure M) (fs i -` {x} \<inter> space M))"
  1666     using N N.simple_functionD(2)[OF fs(1)] unfolding `space N = space M` by auto
  1415     using N simple_functionD(2)[OF fs(1)] unfolding `space N = space M` by auto
  1667   also have "\<dots> = integral\<^isup>P M f"
  1416   also have "\<dots> = integral\<^isup>P M f"
  1668     using positive_integral_monotone_convergence_simple[OF fs(2,5) sf, symmetric]
  1417     using positive_integral_monotone_convergence_simple[OF fs(2,5) sf, symmetric]
  1669     unfolding fs(4) positive_integral_max_0
  1418     unfolding fs(4) positive_integral_max_0
  1670     unfolding simple_integral_def `space N = space M` by simp
  1419     unfolding simple_integral_def `space N = space M` by simp
  1671   finally show ?thesis .
  1420   finally show ?thesis .
  1672 qed
  1421 qed
  1673 
  1422 
  1674 section "Lebesgue Integral"
  1423 section "Lebesgue Integral"
  1675 
  1424 
  1676 definition integrable where
  1425 definition integrable :: "'a measure \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> bool" where
  1677   "integrable M f \<longleftrightarrow> f \<in> borel_measurable M \<and>
  1426   "integrable M f \<longleftrightarrow> f \<in> borel_measurable M \<and>
  1678     (\<integral>\<^isup>+ x. ereal (f x) \<partial>M) \<noteq> \<infinity> \<and> (\<integral>\<^isup>+ x. ereal (- f x) \<partial>M) \<noteq> \<infinity>"
  1427     (\<integral>\<^isup>+ x. ereal (f x) \<partial>M) \<noteq> \<infinity> \<and> (\<integral>\<^isup>+ x. ereal (- f x) \<partial>M) \<noteq> \<infinity>"
  1679 
  1428 
  1680 lemma integrableD[dest]:
  1429 lemma integrableD[dest]:
  1681   assumes "integrable M f"
  1430   assumes "integrable M f"
  1682   shows "f \<in> borel_measurable M" "(\<integral>\<^isup>+ x. ereal (f x) \<partial>M) \<noteq> \<infinity>" "(\<integral>\<^isup>+ x. ereal (- f x) \<partial>M) \<noteq> \<infinity>"
  1431   shows "f \<in> borel_measurable M" "(\<integral>\<^isup>+ x. ereal (f x) \<partial>M) \<noteq> \<infinity>" "(\<integral>\<^isup>+ x. ereal (- f x) \<partial>M) \<noteq> \<infinity>"
  1683   using assms unfolding integrable_def by auto
  1432   using assms unfolding integrable_def by auto
  1684 
  1433 
  1685 definition lebesgue_integral_def:
  1434 definition lebesgue_integral :: "'a measure \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> real" ("integral\<^isup>L") where
  1686   "integral\<^isup>L M f = real ((\<integral>\<^isup>+ x. ereal (f x) \<partial>M)) - real ((\<integral>\<^isup>+ x. ereal (- f x) \<partial>M))"
  1435   "integral\<^isup>L M f = real ((\<integral>\<^isup>+ x. ereal (f x) \<partial>M)) - real ((\<integral>\<^isup>+ x. ereal (- f x) \<partial>M))"
  1687 
  1436 
  1688 syntax
  1437 syntax
  1689   "_lebesgue_integral" :: "pttrn \<Rightarrow> real \<Rightarrow> ('a, 'b) measure_space_scheme \<Rightarrow> real" ("\<integral> _. _ \<partial>_" [60,61] 110)
  1438   "_lebesgue_integral" :: "pttrn \<Rightarrow> real \<Rightarrow> 'a measure \<Rightarrow> real" ("\<integral> _. _ \<partial>_" [60,61] 110)
  1690 
  1439 
  1691 translations
  1440 translations
  1692   "\<integral> x. f \<partial>M" == "CONST integral\<^isup>L M (%x. f)"
  1441   "\<integral> x. f \<partial>M" == "CONST lebesgue_integral M (%x. f)"
  1693 
  1442 
  1694 lemma (in measure_space) integrableE:
  1443 lemma integrableE:
  1695   assumes "integrable M f"
  1444   assumes "integrable M f"
  1696   obtains r q where
  1445   obtains r q where
  1697     "(\<integral>\<^isup>+x. ereal (f x)\<partial>M) = ereal r"
  1446     "(\<integral>\<^isup>+x. ereal (f x)\<partial>M) = ereal r"
  1698     "(\<integral>\<^isup>+x. ereal (-f x)\<partial>M) = ereal q"
  1447     "(\<integral>\<^isup>+x. ereal (-f x)\<partial>M) = ereal q"
  1699     "f \<in> borel_measurable M" "integral\<^isup>L M f = r - q"
  1448     "f \<in> borel_measurable M" "integral\<^isup>L M f = r - q"
  1700   using assms unfolding integrable_def lebesgue_integral_def
  1449   using assms unfolding integrable_def lebesgue_integral_def
  1701   using positive_integral_positive[of "\<lambda>x. ereal (f x)"]
  1450   using positive_integral_positive[of M "\<lambda>x. ereal (f x)"]
  1702   using positive_integral_positive[of "\<lambda>x. ereal (-f x)"]
  1451   using positive_integral_positive[of M "\<lambda>x. ereal (-f x)"]
  1703   by (cases rule: ereal2_cases[of "(\<integral>\<^isup>+x. ereal (-f x)\<partial>M)" "(\<integral>\<^isup>+x. ereal (f x)\<partial>M)"]) auto
  1452   by (cases rule: ereal2_cases[of "(\<integral>\<^isup>+x. ereal (-f x)\<partial>M)" "(\<integral>\<^isup>+x. ereal (f x)\<partial>M)"]) auto
  1704 
  1453 
  1705 lemma (in measure_space) integral_cong:
  1454 lemma integral_cong:
  1706   assumes "\<And>x. x \<in> space M \<Longrightarrow> f x = g x"
  1455   assumes "\<And>x. x \<in> space M \<Longrightarrow> f x = g x"
  1707   shows "integral\<^isup>L M f = integral\<^isup>L M g"
  1456   shows "integral\<^isup>L M f = integral\<^isup>L M g"
  1708   using assms by (simp cong: positive_integral_cong add: lebesgue_integral_def)
  1457   using assms by (simp cong: positive_integral_cong add: lebesgue_integral_def)
  1709 
  1458 
  1710 lemma (in measure_space) integral_cong_measure:
  1459 lemma integral_cong_AE:
  1711   assumes "\<And>A. A \<in> sets M \<Longrightarrow> measure N A = \<mu> A" "sets N = sets M" "space N = space M"
  1460   assumes cong: "AE x in M. f x = g x"
  1712   shows "integral\<^isup>L N f = integral\<^isup>L M f"
       
  1713   by (simp add: positive_integral_cong_measure[OF assms] lebesgue_integral_def)
       
  1714 
       
  1715 lemma (in measure_space) integrable_cong_measure:
       
  1716   assumes "\<And>A. A \<in> sets M \<Longrightarrow> measure N A = \<mu> A" "sets N = sets M" "space N = space M"
       
  1717   shows "integrable N f \<longleftrightarrow> integrable M f"
       
  1718   using assms
       
  1719   by (simp add: positive_integral_cong_measure[OF assms] integrable_def measurable_def)
       
  1720 
       
  1721 lemma (in measure_space) integral_cong_AE:
       
  1722   assumes cong: "AE x. f x = g x"
       
  1723   shows "integral\<^isup>L M f = integral\<^isup>L M g"
  1461   shows "integral\<^isup>L M f = integral\<^isup>L M g"
  1724 proof -
  1462 proof -
  1725   have *: "AE x. ereal (f x) = ereal (g x)"
  1463   have *: "AE x in M. ereal (f x) = ereal (g x)"
  1726     "AE x. ereal (- f x) = ereal (- g x)" using cong by auto
  1464     "AE x in M. ereal (- f x) = ereal (- g x)" using cong by auto
  1727   show ?thesis
  1465   show ?thesis
  1728     unfolding *[THEN positive_integral_cong_AE] lebesgue_integral_def ..
  1466     unfolding *[THEN positive_integral_cong_AE] lebesgue_integral_def ..
  1729 qed
  1467 qed
  1730 
  1468 
  1731 lemma (in measure_space) integrable_cong_AE:
  1469 lemma integrable_cong_AE:
  1732   assumes borel: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1470   assumes borel: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1733   assumes "AE x. f x = g x"
  1471   assumes "AE x in M. f x = g x"
  1734   shows "integrable M f = integrable M g"
  1472   shows "integrable M f = integrable M g"
  1735 proof -
  1473 proof -
  1736   have "(\<integral>\<^isup>+ x. ereal (f x) \<partial>M) = (\<integral>\<^isup>+ x. ereal (g x) \<partial>M)"
  1474   have "(\<integral>\<^isup>+ x. ereal (f x) \<partial>M) = (\<integral>\<^isup>+ x. ereal (g x) \<partial>M)"
  1737     "(\<integral>\<^isup>+ x. ereal (- f x) \<partial>M) = (\<integral>\<^isup>+ x. ereal (- g x) \<partial>M)"
  1475     "(\<integral>\<^isup>+ x. ereal (- f x) \<partial>M) = (\<integral>\<^isup>+ x. ereal (- g x) \<partial>M)"
  1738     using assms by (auto intro!: positive_integral_cong_AE)
  1476     using assms by (auto intro!: positive_integral_cong_AE)
  1739   with assms show ?thesis
  1477   with assms show ?thesis
  1740     by (auto simp: integrable_def)
  1478     by (auto simp: integrable_def)
  1741 qed
  1479 qed
  1742 
  1480 
  1743 lemma (in measure_space) integrable_cong:
  1481 lemma integrable_cong:
  1744   "(\<And>x. x \<in> space M \<Longrightarrow> f x = g x) \<Longrightarrow> integrable M f \<longleftrightarrow> integrable M g"
  1482   "(\<And>x. x \<in> space M \<Longrightarrow> f x = g x) \<Longrightarrow> integrable M f \<longleftrightarrow> integrable M g"
  1745   by (simp cong: positive_integral_cong measurable_cong add: integrable_def)
  1483   by (simp cong: positive_integral_cong measurable_cong add: integrable_def)
  1746 
  1484 
  1747 lemma (in measure_space) integral_eq_positive_integral:
  1485 lemma positive_integral_eq_integral:
       
  1486   assumes f: "integrable M f"
       
  1487   assumes nonneg: "AE x in M. 0 \<le> f x" 
       
  1488   shows "(\<integral>\<^isup>+ x. ereal (f x) \<partial>M) = integral\<^isup>L M f"
       
  1489 proof -
       
  1490   have "(\<integral>\<^isup>+ x. max 0 (ereal (- f x)) \<partial>M) = (\<integral>\<^isup>+ x. 0 \<partial>M)"
       
  1491     using nonneg by (intro positive_integral_cong_AE) (auto split: split_max)
       
  1492   with f positive_integral_positive show ?thesis
       
  1493     by (cases "\<integral>\<^isup>+ x. ereal (f x) \<partial>M")
       
  1494        (auto simp add: lebesgue_integral_def positive_integral_max_0 integrable_def)
       
  1495 qed
       
  1496   
       
  1497 lemma integral_eq_positive_integral:
  1748   assumes f: "\<And>x. 0 \<le> f x"
  1498   assumes f: "\<And>x. 0 \<le> f x"
  1749   shows "integral\<^isup>L M f = real (\<integral>\<^isup>+ x. ereal (f x) \<partial>M)"
  1499   shows "integral\<^isup>L M f = real (\<integral>\<^isup>+ x. ereal (f x) \<partial>M)"
  1750 proof -
  1500 proof -
  1751   { fix x have "max 0 (ereal (- f x)) = 0" using f[of x] by (simp split: split_max) }
  1501   { fix x have "max 0 (ereal (- f x)) = 0" using f[of x] by (simp split: split_max) }
  1752   then have "0 = (\<integral>\<^isup>+ x. max 0 (ereal (- f x)) \<partial>M)" by simp
  1502   then have "0 = (\<integral>\<^isup>+ x. max 0 (ereal (- f x)) \<partial>M)" by simp
  1753   also have "\<dots> = (\<integral>\<^isup>+ x. ereal (- f x) \<partial>M)" unfolding positive_integral_max_0 ..
  1503   also have "\<dots> = (\<integral>\<^isup>+ x. ereal (- f x) \<partial>M)" unfolding positive_integral_max_0 ..
  1754   finally show ?thesis
  1504   finally show ?thesis
  1755     unfolding lebesgue_integral_def by simp
  1505     unfolding lebesgue_integral_def by simp
  1756 qed
  1506 qed
  1757 
  1507 
  1758 lemma (in measure_space) integral_vimage:
  1508 lemma integral_minus[intro, simp]:
  1759   assumes T: "sigma_algebra M'" "T \<in> measure_preserving M M'"
       
  1760   assumes f: "f \<in> borel_measurable M'"
       
  1761   shows "integral\<^isup>L M' f = (\<integral>x. f (T x) \<partial>M)"
       
  1762 proof -
       
  1763   interpret T: measure_space M' by (rule measure_space_vimage[OF T])
       
  1764   from measurable_comp[OF measure_preservingD2[OF T(2)], of f borel]
       
  1765   have borel: "(\<lambda>x. ereal (f x)) \<in> borel_measurable M'" "(\<lambda>x. ereal (- f x)) \<in> borel_measurable M'"
       
  1766     and "(\<lambda>x. f (T x)) \<in> borel_measurable M"
       
  1767     using f by (auto simp: comp_def)
       
  1768   then show ?thesis
       
  1769     using f unfolding lebesgue_integral_def integrable_def
       
  1770     by (auto simp: borel[THEN positive_integral_vimage[OF T]])
       
  1771 qed
       
  1772 
       
  1773 lemma (in measure_space) integrable_vimage:
       
  1774   assumes T: "sigma_algebra M'" "T \<in> measure_preserving M M'"
       
  1775   assumes f: "integrable M' f"
       
  1776   shows "integrable M (\<lambda>x. f (T x))"
       
  1777 proof -
       
  1778   interpret T: measure_space M' by (rule measure_space_vimage[OF T])
       
  1779   from measurable_comp[OF measure_preservingD2[OF T(2)], of f borel]
       
  1780   have borel: "(\<lambda>x. ereal (f x)) \<in> borel_measurable M'" "(\<lambda>x. ereal (- f x)) \<in> borel_measurable M'"
       
  1781     and "(\<lambda>x. f (T x)) \<in> borel_measurable M"
       
  1782     using f by (auto simp: comp_def)
       
  1783   then show ?thesis
       
  1784     using f unfolding lebesgue_integral_def integrable_def
       
  1785     by (auto simp: borel[THEN positive_integral_vimage[OF T]])
       
  1786 qed
       
  1787 
       
  1788 lemma (in measure_space) integral_translated_density:
       
  1789   assumes f: "f \<in> borel_measurable M" "AE x. 0 \<le> f x"
       
  1790     and g: "g \<in> borel_measurable M"
       
  1791     and N: "space N = space M" "sets N = sets M"
       
  1792     and density: "\<And>A. A \<in> sets M \<Longrightarrow> measure N A = (\<integral>\<^isup>+ x. f x * indicator A x \<partial>M)"
       
  1793       (is "\<And>A. _ \<Longrightarrow> _ = ?d A")
       
  1794   shows "integral\<^isup>L N g = (\<integral> x. f x * g x \<partial>M)" (is ?integral)
       
  1795     and "integrable N g = integrable M (\<lambda>x. f x * g x)" (is ?integrable)
       
  1796 proof -
       
  1797   from f have ms: "measure_space (M\<lparr>measure := ?d\<rparr>)"
       
  1798     by (intro measure_space_density[where u="\<lambda>x. ereal (f x)"]) auto
       
  1799 
       
  1800   from ms density N have "(\<integral>\<^isup>+ x. g x \<partial>N) =  (\<integral>\<^isup>+ x. max 0 (ereal (g x)) \<partial>M\<lparr>measure := ?d\<rparr>)"
       
  1801     unfolding positive_integral_max_0
       
  1802     by (intro measure_space.positive_integral_cong_measure) auto
       
  1803   also have "\<dots> = (\<integral>\<^isup>+ x. ereal (f x) * max 0 (ereal (g x)) \<partial>M)"
       
  1804     using f g by (intro positive_integral_translated_density) auto
       
  1805   also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (ereal (f x * g x)) \<partial>M)"
       
  1806     using f by (intro positive_integral_cong_AE)
       
  1807                (auto simp: ereal_max_0 zero_le_mult_iff split: split_max)
       
  1808   finally have pos: "(\<integral>\<^isup>+ x. g x \<partial>N) = (\<integral>\<^isup>+ x. f x * g x \<partial>M)"
       
  1809     by (simp add: positive_integral_max_0)
       
  1810   
       
  1811   from ms density N have "(\<integral>\<^isup>+ x. - (g x) \<partial>N) =  (\<integral>\<^isup>+ x. max 0 (ereal (- g x)) \<partial>M\<lparr>measure := ?d\<rparr>)"
       
  1812     unfolding positive_integral_max_0
       
  1813     by (intro measure_space.positive_integral_cong_measure) auto
       
  1814   also have "\<dots> = (\<integral>\<^isup>+ x. ereal (f x) * max 0 (ereal (- g x)) \<partial>M)"
       
  1815     using f g by (intro positive_integral_translated_density) auto
       
  1816   also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (ereal (- f x * g x)) \<partial>M)"
       
  1817     using f by (intro positive_integral_cong_AE)
       
  1818                (auto simp: ereal_max_0 mult_le_0_iff split: split_max)
       
  1819   finally have neg: "(\<integral>\<^isup>+ x. - g x \<partial>N) = (\<integral>\<^isup>+ x. - (f x * g x) \<partial>M)"
       
  1820     by (simp add: positive_integral_max_0)
       
  1821 
       
  1822   have g_N: "g \<in> borel_measurable N"
       
  1823     using g N unfolding measurable_def by simp
       
  1824 
       
  1825   show ?integral ?integrable
       
  1826     unfolding lebesgue_integral_def integrable_def
       
  1827     using pos neg f g g_N by auto
       
  1828 qed
       
  1829 
       
  1830 lemma (in measure_space) integral_minus[intro, simp]:
       
  1831   assumes "integrable M f"
  1509   assumes "integrable M f"
  1832   shows "integrable M (\<lambda>x. - f x)" "(\<integral>x. - f x \<partial>M) = - integral\<^isup>L M f"
  1510   shows "integrable M (\<lambda>x. - f x)" "(\<integral>x. - f x \<partial>M) = - integral\<^isup>L M f"
  1833   using assms by (auto simp: integrable_def lebesgue_integral_def)
  1511   using assms by (auto simp: integrable_def lebesgue_integral_def)
  1834 
  1512 
  1835 lemma (in measure_space) integral_minus_iff[simp]:
  1513 lemma integral_minus_iff[simp]:
  1836   "integrable M (\<lambda>x. - f x) \<longleftrightarrow> integrable M f"
  1514   "integrable M (\<lambda>x. - f x) \<longleftrightarrow> integrable M f"
  1837 proof
  1515 proof
  1838   assume "integrable M (\<lambda>x. - f x)"
  1516   assume "integrable M (\<lambda>x. - f x)"
  1839   then have "integrable M (\<lambda>x. - (- f x))"
  1517   then have "integrable M (\<lambda>x. - (- f x))"
  1840     by (rule integral_minus)
  1518     by (rule integral_minus)
  1841   then show "integrable M f" by simp
  1519   then show "integrable M f" by simp
  1842 qed (rule integral_minus)
  1520 qed (rule integral_minus)
  1843 
  1521 
  1844 lemma (in measure_space) integral_of_positive_diff:
  1522 lemma integral_of_positive_diff:
  1845   assumes integrable: "integrable M u" "integrable M v"
  1523   assumes integrable: "integrable M u" "integrable M v"
  1846   and f_def: "\<And>x. f x = u x - v x" and pos: "\<And>x. 0 \<le> u x" "\<And>x. 0 \<le> v x"
  1524   and f_def: "\<And>x. f x = u x - v x" and pos: "\<And>x. 0 \<le> u x" "\<And>x. 0 \<le> v x"
  1847   shows "integrable M f" and "integral\<^isup>L M f = integral\<^isup>L M u - integral\<^isup>L M v"
  1525   shows "integrable M f" and "integral\<^isup>L M f = integral\<^isup>L M u - integral\<^isup>L M v"
  1848 proof -
  1526 proof -
  1849   let ?f = "\<lambda>x. max 0 (ereal (f x))"
  1527   let ?f = "\<lambda>x. max 0 (ereal (f x))"
  1850   let ?mf = "\<lambda>x. max 0 (ereal (- f x))"
  1528   let ?mf = "\<lambda>x. max 0 (ereal (- f x))"
  1851   let ?u = "\<lambda>x. max 0 (ereal (u x))"
  1529   let ?u = "\<lambda>x. max 0 (ereal (u x))"
  1852   let ?v = "\<lambda>x. max 0 (ereal (v x))"
  1530   let ?v = "\<lambda>x. max 0 (ereal (v x))"
  1853 
  1531 
  1854   from borel_measurable_diff[of u v] integrable
  1532   from borel_measurable_diff[of u M v] integrable
  1855   have f_borel: "?f \<in> borel_measurable M" and
  1533   have f_borel: "?f \<in> borel_measurable M" and
  1856     mf_borel: "?mf \<in> borel_measurable M" and
  1534     mf_borel: "?mf \<in> borel_measurable M" and
  1857     v_borel: "?v \<in> borel_measurable M" and
  1535     v_borel: "?v \<in> borel_measurable M" and
  1858     u_borel: "?u \<in> borel_measurable M" and
  1536     u_borel: "?u \<in> borel_measurable M" and
  1859     "f \<in> borel_measurable M"
  1537     "f \<in> borel_measurable M"
  1879     unfolding positive_integral_max_0
  1557     unfolding positive_integral_max_0
  1880     unfolding pos[THEN integral_eq_positive_integral]
  1558     unfolding pos[THEN integral_eq_positive_integral]
  1881     using integrable f by (auto elim!: integrableE)
  1559     using integrable f by (auto elim!: integrableE)
  1882 qed
  1560 qed
  1883 
  1561 
  1884 lemma (in measure_space) integral_linear:
  1562 lemma integral_linear:
  1885   assumes "integrable M f" "integrable M g" and "0 \<le> a"
  1563   assumes "integrable M f" "integrable M g" and "0 \<le> a"
  1886   shows "integrable M (\<lambda>t. a * f t + g t)"
  1564   shows "integrable M (\<lambda>t. a * f t + g t)"
  1887   and "(\<integral> t. a * f t + g t \<partial>M) = a * integral\<^isup>L M f + integral\<^isup>L M g" (is ?EQ)
  1565   and "(\<integral> t. a * f t + g t \<partial>M) = a * integral\<^isup>L M f + integral\<^isup>L M g" (is ?EQ)
  1888 proof -
  1566 proof -
  1889   let ?f = "\<lambda>x. max 0 (ereal (f x))"
  1567   let ?f = "\<lambda>x. max 0 (ereal (f x))"
  1915     unfolding diff(2) ** positive_integral_max_0
  1593     unfolding diff(2) ** positive_integral_max_0
  1916     unfolding lebesgue_integral_def *
  1594     unfolding lebesgue_integral_def *
  1917     by (auto elim!: integrableE simp: field_simps)
  1595     by (auto elim!: integrableE simp: field_simps)
  1918 qed
  1596 qed
  1919 
  1597 
  1920 lemma (in measure_space) integral_add[simp, intro]:
  1598 lemma integral_add[simp, intro]:
  1921   assumes "integrable M f" "integrable M g"
  1599   assumes "integrable M f" "integrable M g"
  1922   shows "integrable M (\<lambda>t. f t + g t)"
  1600   shows "integrable M (\<lambda>t. f t + g t)"
  1923   and "(\<integral> t. f t + g t \<partial>M) = integral\<^isup>L M f + integral\<^isup>L M g"
  1601   and "(\<integral> t. f t + g t \<partial>M) = integral\<^isup>L M f + integral\<^isup>L M g"
  1924   using assms integral_linear[where a=1] by auto
  1602   using assms integral_linear[where a=1] by auto
  1925 
  1603 
  1926 lemma (in measure_space) integral_zero[simp, intro]:
  1604 lemma integral_zero[simp, intro]:
  1927   shows "integrable M (\<lambda>x. 0)" "(\<integral> x.0 \<partial>M) = 0"
  1605   shows "integrable M (\<lambda>x. 0)" "(\<integral> x.0 \<partial>M) = 0"
  1928   unfolding integrable_def lebesgue_integral_def
  1606   unfolding integrable_def lebesgue_integral_def
  1929   by (auto simp add: borel_measurable_const)
  1607   by (auto simp add: borel_measurable_const)
  1930 
  1608 
  1931 lemma (in measure_space) integral_cmult[simp, intro]:
  1609 lemma integral_cmult[simp, intro]:
  1932   assumes "integrable M f"
  1610   assumes "integrable M f"
  1933   shows "integrable M (\<lambda>t. a * f t)" (is ?P)
  1611   shows "integrable M (\<lambda>t. a * f t)" (is ?P)
  1934   and "(\<integral> t. a * f t \<partial>M) = a * integral\<^isup>L M f" (is ?I)
  1612   and "(\<integral> t. a * f t \<partial>M) = a * integral\<^isup>L M f" (is ?I)
  1935 proof -
  1613 proof -
  1936   have "integrable M (\<lambda>t. a * f t) \<and> (\<integral> t. a * f t \<partial>M) = a * integral\<^isup>L M f"
  1614   have "integrable M (\<lambda>t. a * f t) \<and> (\<integral> t. a * f t \<partial>M) = a * integral\<^isup>L M f"
  1940       by (simp add: integral_zero)
  1618       by (simp add: integral_zero)
  1941   next
  1619   next
  1942     assume "a \<le> 0" hence "0 \<le> - a" by auto
  1620     assume "a \<le> 0" hence "0 \<le> - a" by auto
  1943     have *: "\<And>t. - a * t + 0 = (-a) * t" by simp
  1621     have *: "\<And>t. - a * t + 0 = (-a) * t" by simp
  1944     show ?thesis using integral_linear[OF assms integral_zero(1) `0 \<le> - a`]
  1622     show ?thesis using integral_linear[OF assms integral_zero(1) `0 \<le> - a`]
  1945         integral_minus(1)[of "\<lambda>t. - a * f t"]
  1623         integral_minus(1)[of M "\<lambda>t. - a * f t"]
  1946       unfolding * integral_zero by simp
  1624       unfolding * integral_zero by simp
  1947   qed
  1625   qed
  1948   thus ?P ?I by auto
  1626   thus ?P ?I by auto
  1949 qed
  1627 qed
  1950 
  1628 
  1951 lemma (in measure_space) integral_multc:
  1629 lemma lebesgue_integral_cmult_nonneg:
       
  1630   assumes f: "f \<in> borel_measurable M" and "0 \<le> c"
       
  1631   shows "(\<integral>x. c * f x \<partial>M) = c * integral\<^isup>L M f"
       
  1632 proof -
       
  1633   { have "c * real (integral\<^isup>P M (\<lambda>x. max 0 (ereal (f x)))) =
       
  1634       real (ereal c * integral\<^isup>P M (\<lambda>x. max 0 (ereal (f x))))"
       
  1635       by simp
       
  1636     also have "\<dots> = real (integral\<^isup>P M (\<lambda>x. ereal c * max 0 (ereal (f x))))"
       
  1637       using f `0 \<le> c` by (subst positive_integral_cmult) auto
       
  1638     also have "\<dots> = real (integral\<^isup>P M (\<lambda>x. max 0 (ereal (c * f x))))"
       
  1639       using `0 \<le> c` by (auto intro!: arg_cong[where f=real] positive_integral_cong simp: max_def zero_le_mult_iff)
       
  1640     finally have "real (integral\<^isup>P M (\<lambda>x. ereal (c * f x))) = c * real (integral\<^isup>P M (\<lambda>x. ereal (f x)))"
       
  1641       by (simp add: positive_integral_max_0) }
       
  1642   moreover
       
  1643   { have "c * real (integral\<^isup>P M (\<lambda>x. max 0 (ereal (- f x)))) =
       
  1644       real (ereal c * integral\<^isup>P M (\<lambda>x. max 0 (ereal (- f x))))"
       
  1645       by simp
       
  1646     also have "\<dots> = real (integral\<^isup>P M (\<lambda>x. ereal c * max 0 (ereal (- f x))))"
       
  1647       using f `0 \<le> c` by (subst positive_integral_cmult) auto
       
  1648     also have "\<dots> = real (integral\<^isup>P M (\<lambda>x. max 0 (ereal (- c * f x))))"
       
  1649       using `0 \<le> c` by (auto intro!: arg_cong[where f=real] positive_integral_cong simp: max_def mult_le_0_iff)
       
  1650     finally have "real (integral\<^isup>P M (\<lambda>x. ereal (- c * f x))) = c * real (integral\<^isup>P M (\<lambda>x. ereal (- f x)))"
       
  1651       by (simp add: positive_integral_max_0) }
       
  1652   ultimately show ?thesis
       
  1653     by (simp add: lebesgue_integral_def field_simps)
       
  1654 qed
       
  1655 
       
  1656 lemma lebesgue_integral_uminus:
       
  1657   "(\<integral>x. - f x \<partial>M) = - integral\<^isup>L M f"
       
  1658     unfolding lebesgue_integral_def by simp
       
  1659 
       
  1660 lemma lebesgue_integral_cmult:
       
  1661   assumes f: "f \<in> borel_measurable M"
       
  1662   shows "(\<integral>x. c * f x \<partial>M) = c * integral\<^isup>L M f"
       
  1663 proof (cases rule: linorder_le_cases)
       
  1664   assume "0 \<le> c" with f show ?thesis by (rule lebesgue_integral_cmult_nonneg)
       
  1665 next
       
  1666   assume "c \<le> 0"
       
  1667   with lebesgue_integral_cmult_nonneg[OF f, of "-c"]
       
  1668   show ?thesis
       
  1669     by (simp add: lebesgue_integral_def)
       
  1670 qed
       
  1671 
       
  1672 lemma integral_multc:
  1952   assumes "integrable M f"
  1673   assumes "integrable M f"
  1953   shows "(\<integral> x. f x * c \<partial>M) = integral\<^isup>L M f * c"
  1674   shows "(\<integral> x. f x * c \<partial>M) = integral\<^isup>L M f * c"
  1954   unfolding mult_commute[of _ c] integral_cmult[OF assms] ..
  1675   unfolding mult_commute[of _ c] integral_cmult[OF assms] ..
  1955 
  1676 
  1956 lemma (in measure_space) integral_mono_AE:
  1677 lemma integral_mono_AE:
  1957   assumes fg: "integrable M f" "integrable M g"
  1678   assumes fg: "integrable M f" "integrable M g"
  1958   and mono: "AE t. f t \<le> g t"
  1679   and mono: "AE t in M. f t \<le> g t"
  1959   shows "integral\<^isup>L M f \<le> integral\<^isup>L M g"
  1680   shows "integral\<^isup>L M f \<le> integral\<^isup>L M g"
  1960 proof -
  1681 proof -
  1961   have "AE x. ereal (f x) \<le> ereal (g x)"
  1682   have "AE x in M. ereal (f x) \<le> ereal (g x)"
  1962     using mono by auto
  1683     using mono by auto
  1963   moreover have "AE x. ereal (- g x) \<le> ereal (- f x)"
  1684   moreover have "AE x in M. ereal (- g x) \<le> ereal (- f x)"
  1964     using mono by auto
  1685     using mono by auto
  1965   ultimately show ?thesis using fg
  1686   ultimately show ?thesis using fg
  1966     by (auto intro!: add_mono positive_integral_mono_AE real_of_ereal_positive_mono
  1687     by (auto intro!: add_mono positive_integral_mono_AE real_of_ereal_positive_mono
  1967              simp: positive_integral_positive lebesgue_integral_def diff_minus)
  1688              simp: positive_integral_positive lebesgue_integral_def diff_minus)
  1968 qed
  1689 qed
  1969 
  1690 
  1970 lemma (in measure_space) integral_mono:
  1691 lemma integral_mono:
  1971   assumes "integrable M f" "integrable M g" "\<And>t. t \<in> space M \<Longrightarrow> f t \<le> g t"
  1692   assumes "integrable M f" "integrable M g" "\<And>t. t \<in> space M \<Longrightarrow> f t \<le> g t"
  1972   shows "integral\<^isup>L M f \<le> integral\<^isup>L M g"
  1693   shows "integral\<^isup>L M f \<le> integral\<^isup>L M g"
  1973   using assms by (auto intro: integral_mono_AE)
  1694   using assms by (auto intro: integral_mono_AE)
  1974 
  1695 
  1975 lemma (in measure_space) integral_diff[simp, intro]:
  1696 lemma integral_diff[simp, intro]:
  1976   assumes f: "integrable M f" and g: "integrable M g"
  1697   assumes f: "integrable M f" and g: "integrable M g"
  1977   shows "integrable M (\<lambda>t. f t - g t)"
  1698   shows "integrable M (\<lambda>t. f t - g t)"
  1978   and "(\<integral> t. f t - g t \<partial>M) = integral\<^isup>L M f - integral\<^isup>L M g"
  1699   and "(\<integral> t. f t - g t \<partial>M) = integral\<^isup>L M f - integral\<^isup>L M g"
  1979   using integral_add[OF f integral_minus(1)[OF g]]
  1700   using integral_add[OF f integral_minus(1)[OF g]]
  1980   unfolding diff_minus integral_minus(2)[OF g]
  1701   unfolding diff_minus integral_minus(2)[OF g]
  1981   by auto
  1702   by auto
  1982 
  1703 
  1983 lemma (in measure_space) integral_indicator[simp, intro]:
  1704 lemma integral_indicator[simp, intro]:
  1984   assumes "A \<in> sets M" and "\<mu> A \<noteq> \<infinity>"
  1705   assumes "A \<in> sets M" and "(emeasure M) A \<noteq> \<infinity>"
  1985   shows "integral\<^isup>L M (indicator A) = real (\<mu> A)" (is ?int)
  1706   shows "integral\<^isup>L M (indicator A) = real ((emeasure M) A)" (is ?int)
  1986   and "integrable M (indicator A)" (is ?able)
  1707   and "integrable M (indicator A)" (is ?able)
  1987 proof -
  1708 proof -
  1988   from `A \<in> sets M` have *:
  1709   from `A \<in> sets M` have *:
  1989     "\<And>x. ereal (indicator A x) = indicator A x"
  1710     "\<And>x. ereal (indicator A x) = indicator A x"
  1990     "(\<integral>\<^isup>+x. ereal (- indicator A x) \<partial>M) = 0"
  1711     "(\<integral>\<^isup>+x. ereal (- indicator A x) \<partial>M) = 0"
  1992   show ?int ?able
  1713   show ?int ?able
  1993     using assms unfolding lebesgue_integral_def integrable_def
  1714     using assms unfolding lebesgue_integral_def integrable_def
  1994     by (auto simp: * positive_integral_indicator borel_measurable_indicator)
  1715     by (auto simp: * positive_integral_indicator borel_measurable_indicator)
  1995 qed
  1716 qed
  1996 
  1717 
  1997 lemma (in measure_space) integral_cmul_indicator:
  1718 lemma integral_cmul_indicator:
  1998   assumes "A \<in> sets M" and "c \<noteq> 0 \<Longrightarrow> \<mu> A \<noteq> \<infinity>"
  1719   assumes "A \<in> sets M" and "c \<noteq> 0 \<Longrightarrow> (emeasure M) A \<noteq> \<infinity>"
  1999   shows "integrable M (\<lambda>x. c * indicator A x)" (is ?P)
  1720   shows "integrable M (\<lambda>x. c * indicator A x)" (is ?P)
  2000   and "(\<integral>x. c * indicator A x \<partial>M) = c * real (\<mu> A)" (is ?I)
  1721   and "(\<integral>x. c * indicator A x \<partial>M) = c * real ((emeasure M) A)" (is ?I)
  2001 proof -
  1722 proof -
  2002   show ?P
  1723   show ?P
  2003   proof (cases "c = 0")
  1724   proof (cases "c = 0")
  2004     case False with assms show ?thesis by simp
  1725     case False with assms show ?thesis by simp
  2005   qed simp
  1726   qed simp
  2008   proof (cases "c = 0")
  1729   proof (cases "c = 0")
  2009     case False with assms show ?thesis by simp
  1730     case False with assms show ?thesis by simp
  2010   qed simp
  1731   qed simp
  2011 qed
  1732 qed
  2012 
  1733 
  2013 lemma (in measure_space) integral_setsum[simp, intro]:
  1734 lemma integral_setsum[simp, intro]:
  2014   assumes "\<And>n. n \<in> S \<Longrightarrow> integrable M (f n)"
  1735   assumes "\<And>n. n \<in> S \<Longrightarrow> integrable M (f n)"
  2015   shows "(\<integral>x. (\<Sum> i \<in> S. f i x) \<partial>M) = (\<Sum> i \<in> S. integral\<^isup>L M (f i))" (is "?int S")
  1736   shows "(\<integral>x. (\<Sum> i \<in> S. f i x) \<partial>M) = (\<Sum> i \<in> S. integral\<^isup>L M (f i))" (is "?int S")
  2016     and "integrable M (\<lambda>x. \<Sum> i \<in> S. f i x)" (is "?I S")
  1737     and "integrable M (\<lambda>x. \<Sum> i \<in> S. f i x)" (is "?I S")
  2017 proof -
  1738 proof -
  2018   have "?int S \<and> ?I S"
  1739   have "?int S \<and> ?I S"
  2021     from this assms show ?thesis by (induct S) simp_all
  1742     from this assms show ?thesis by (induct S) simp_all
  2022   qed simp
  1743   qed simp
  2023   thus "?int S" and "?I S" by auto
  1744   thus "?int S" and "?I S" by auto
  2024 qed
  1745 qed
  2025 
  1746 
  2026 lemma (in measure_space) integrable_abs:
  1747 lemma integrable_abs:
  2027   assumes "integrable M f"
  1748   assumes "integrable M f"
  2028   shows "integrable M (\<lambda> x. \<bar>f x\<bar>)"
  1749   shows "integrable M (\<lambda> x. \<bar>f x\<bar>)"
  2029 proof -
  1750 proof -
  2030   from assms have *: "(\<integral>\<^isup>+x. ereal (- \<bar>f x\<bar>)\<partial>M) = 0"
  1751   from assms have *: "(\<integral>\<^isup>+x. ereal (- \<bar>f x\<bar>)\<partial>M) = 0"
  2031     "\<And>x. ereal \<bar>f x\<bar> = max 0 (ereal (f x)) + max 0 (ereal (- f x))"
  1752     "\<And>x. ereal \<bar>f x\<bar> = max 0 (ereal (f x)) + max 0 (ereal (- f x))"
  2032     by (auto simp: integrable_def positive_integral_0_iff_AE split: split_max)
  1753     by (auto simp: integrable_def positive_integral_0_iff_AE split: split_max)
  2033   with assms show ?thesis
  1754   with assms show ?thesis
  2034     by (simp add: positive_integral_add positive_integral_max_0 integrable_def)
  1755     by (simp add: positive_integral_add positive_integral_max_0 integrable_def)
  2035 qed
  1756 qed
  2036 
  1757 
  2037 lemma (in measure_space) integral_subalgebra:
  1758 lemma integral_subalgebra:
  2038   assumes borel: "f \<in> borel_measurable N"
  1759   assumes borel: "f \<in> borel_measurable N"
  2039   and N: "sets N \<subseteq> sets M" "space N = space M" "\<And>A. A \<in> sets N \<Longrightarrow> measure N A = \<mu> A" and sa: "sigma_algebra N"
  1760   and N: "sets N \<subseteq> sets M" "space N = space M" "\<And>A. A \<in> sets N \<Longrightarrow> emeasure N A = emeasure M A"
  2040   shows "integrable N f \<longleftrightarrow> integrable M f" (is ?P)
  1761   shows "integrable N f \<longleftrightarrow> integrable M f" (is ?P)
  2041     and "integral\<^isup>L N f = integral\<^isup>L M f" (is ?I)
  1762     and "integral\<^isup>L N f = integral\<^isup>L M f" (is ?I)
  2042 proof -
  1763 proof -
  2043   interpret N: measure_space N using measure_space_subalgebra[OF sa N] .
       
  2044   have "(\<integral>\<^isup>+ x. max 0 (ereal (f x)) \<partial>N) = (\<integral>\<^isup>+ x. max 0 (ereal (f x)) \<partial>M)"
  1764   have "(\<integral>\<^isup>+ x. max 0 (ereal (f x)) \<partial>N) = (\<integral>\<^isup>+ x. max 0 (ereal (f x)) \<partial>M)"
  2045        "(\<integral>\<^isup>+ x. max 0 (ereal (- f x)) \<partial>N) = (\<integral>\<^isup>+ x. max 0 (ereal (- f x)) \<partial>M)"
  1765        "(\<integral>\<^isup>+ x. max 0 (ereal (- f x)) \<partial>N) = (\<integral>\<^isup>+ x. max 0 (ereal (- f x)) \<partial>M)"
  2046     using borel by (auto intro!: positive_integral_subalgebra N sa)
  1766     using borel by (auto intro!: positive_integral_subalgebra N)
  2047   moreover have "f \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable N"
  1767   moreover have "f \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable N"
  2048     using assms unfolding measurable_def by auto
  1768     using assms unfolding measurable_def by auto
  2049   ultimately show ?P ?I
  1769   ultimately show ?P ?I
  2050     by (auto simp: integrable_def lebesgue_integral_def positive_integral_max_0)
  1770     by (auto simp: integrable_def lebesgue_integral_def positive_integral_max_0)
  2051 qed
  1771 qed
  2052 
  1772 
  2053 lemma (in measure_space) integrable_bound:
  1773 lemma integrable_bound:
  2054   assumes "integrable M f"
  1774   assumes "integrable M f"
  2055   and f: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
  1775   and f: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
  2056     "\<And>x. x \<in> space M \<Longrightarrow> \<bar>g x\<bar> \<le> f x"
  1776     "\<And>x. x \<in> space M \<Longrightarrow> \<bar>g x\<bar> \<le> f x"
  2057   assumes borel: "g \<in> borel_measurable M"
  1777   assumes borel: "g \<in> borel_measurable M"
  2058   shows "integrable M g"
  1778   shows "integrable M g"
  2075 
  1795 
  2076   from neg pos borel show ?thesis
  1796   from neg pos borel show ?thesis
  2077     unfolding integrable_def by auto
  1797     unfolding integrable_def by auto
  2078 qed
  1798 qed
  2079 
  1799 
  2080 lemma (in measure_space) integrable_abs_iff:
  1800 lemma lebesgue_integral_nonneg:
       
  1801   assumes ae: "(AE x in M. 0 \<le> f x)" shows "0 \<le> integral\<^isup>L M f"
       
  1802 proof -
       
  1803   have "(\<integral>\<^isup>+x. max 0 (ereal (- f x)) \<partial>M) = (\<integral>\<^isup>+x. 0 \<partial>M)"
       
  1804     using ae by (intro positive_integral_cong_AE) (auto simp: max_def)
       
  1805   then show ?thesis
       
  1806     by (auto simp: lebesgue_integral_def positive_integral_max_0
       
  1807              intro!: real_of_ereal_pos positive_integral_positive)
       
  1808 qed
       
  1809 
       
  1810 lemma integrable_abs_iff:
  2081   "f \<in> borel_measurable M \<Longrightarrow> integrable M (\<lambda> x. \<bar>f x\<bar>) \<longleftrightarrow> integrable M f"
  1811   "f \<in> borel_measurable M \<Longrightarrow> integrable M (\<lambda> x. \<bar>f x\<bar>) \<longleftrightarrow> integrable M f"
  2082   by (auto intro!: integrable_bound[where g=f] integrable_abs)
  1812   by (auto intro!: integrable_bound[where g=f] integrable_abs)
  2083 
  1813 
  2084 lemma (in measure_space) integrable_max:
  1814 lemma integrable_max:
  2085   assumes int: "integrable M f" "integrable M g"
  1815   assumes int: "integrable M f" "integrable M g"
  2086   shows "integrable M (\<lambda> x. max (f x) (g x))"
  1816   shows "integrable M (\<lambda> x. max (f x) (g x))"
  2087 proof (rule integrable_bound)
  1817 proof (rule integrable_bound)
  2088   show "integrable M (\<lambda>x. \<bar>f x\<bar> + \<bar>g x\<bar>)"
  1818   show "integrable M (\<lambda>x. \<bar>f x\<bar> + \<bar>g x\<bar>)"
  2089     using int by (simp add: integrable_abs)
  1819     using int by (simp add: integrable_abs)
  2093   fix x assume "x \<in> space M"
  1823   fix x assume "x \<in> space M"
  2094   show "0 \<le> \<bar>f x\<bar> + \<bar>g x\<bar>" "\<bar>max (f x) (g x)\<bar> \<le> \<bar>f x\<bar> + \<bar>g x\<bar>"
  1824   show "0 \<le> \<bar>f x\<bar> + \<bar>g x\<bar>" "\<bar>max (f x) (g x)\<bar> \<le> \<bar>f x\<bar> + \<bar>g x\<bar>"
  2095     by auto
  1825     by auto
  2096 qed
  1826 qed
  2097 
  1827 
  2098 lemma (in measure_space) integrable_min:
  1828 lemma integrable_min:
  2099   assumes int: "integrable M f" "integrable M g"
  1829   assumes int: "integrable M f" "integrable M g"
  2100   shows "integrable M (\<lambda> x. min (f x) (g x))"
  1830   shows "integrable M (\<lambda> x. min (f x) (g x))"
  2101 proof (rule integrable_bound)
  1831 proof (rule integrable_bound)
  2102   show "integrable M (\<lambda>x. \<bar>f x\<bar> + \<bar>g x\<bar>)"
  1832   show "integrable M (\<lambda>x. \<bar>f x\<bar> + \<bar>g x\<bar>)"
  2103     using int by (simp add: integrable_abs)
  1833     using int by (simp add: integrable_abs)
  2107   fix x assume "x \<in> space M"
  1837   fix x assume "x \<in> space M"
  2108   show "0 \<le> \<bar>f x\<bar> + \<bar>g x\<bar>" "\<bar>min (f x) (g x)\<bar> \<le> \<bar>f x\<bar> + \<bar>g x\<bar>"
  1838   show "0 \<le> \<bar>f x\<bar> + \<bar>g x\<bar>" "\<bar>min (f x) (g x)\<bar> \<le> \<bar>f x\<bar> + \<bar>g x\<bar>"
  2109     by auto
  1839     by auto
  2110 qed
  1840 qed
  2111 
  1841 
  2112 lemma (in measure_space) integral_triangle_inequality:
  1842 lemma integral_triangle_inequality:
  2113   assumes "integrable M f"
  1843   assumes "integrable M f"
  2114   shows "\<bar>integral\<^isup>L M f\<bar> \<le> (\<integral>x. \<bar>f x\<bar> \<partial>M)"
  1844   shows "\<bar>integral\<^isup>L M f\<bar> \<le> (\<integral>x. \<bar>f x\<bar> \<partial>M)"
  2115 proof -
  1845 proof -
  2116   have "\<bar>integral\<^isup>L M f\<bar> = max (integral\<^isup>L M f) (- integral\<^isup>L M f)" by auto
  1846   have "\<bar>integral\<^isup>L M f\<bar> = max (integral\<^isup>L M f) (- integral\<^isup>L M f)" by auto
  2117   also have "\<dots> \<le> (\<integral>x. \<bar>f x\<bar> \<partial>M)"
  1847   also have "\<dots> \<le> (\<integral>x. \<bar>f x\<bar> \<partial>M)"
  2118       using assms integral_minus(2)[of f, symmetric]
  1848       using assms integral_minus(2)[of M f, symmetric]
  2119       by (auto intro!: integral_mono integrable_abs simp del: integral_minus)
  1849       by (auto intro!: integral_mono integrable_abs simp del: integral_minus)
  2120   finally show ?thesis .
  1850   finally show ?thesis .
  2121 qed
  1851 qed
  2122 
  1852 
  2123 lemma (in measure_space) integral_positive:
  1853 lemma integral_positive:
  2124   assumes "integrable M f" "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
  1854   assumes "integrable M f" "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
  2125   shows "0 \<le> integral\<^isup>L M f"
  1855   shows "0 \<le> integral\<^isup>L M f"
  2126 proof -
  1856 proof -
  2127   have "0 = (\<integral>x. 0 \<partial>M)" by (auto simp: integral_zero)
  1857   have "0 = (\<integral>x. 0 \<partial>M)" by (auto simp: integral_zero)
  2128   also have "\<dots> \<le> integral\<^isup>L M f"
  1858   also have "\<dots> \<le> integral\<^isup>L M f"
  2129     using assms by (rule integral_mono[OF integral_zero(1)])
  1859     using assms by (rule integral_mono[OF integral_zero(1)])
  2130   finally show ?thesis .
  1860   finally show ?thesis .
  2131 qed
  1861 qed
  2132 
  1862 
  2133 lemma (in measure_space) integral_monotone_convergence_pos:
  1863 lemma integral_monotone_convergence_pos:
  2134   assumes i: "\<And>i. integrable M (f i)" and mono: "\<And>x. mono (\<lambda>n. f n x)"
  1864   assumes i: "\<And>i. integrable M (f i)" and mono: "\<And>x. mono (\<lambda>n. f n x)"
  2135   and pos: "\<And>x i. 0 \<le> f i x"
  1865   and pos: "\<And>x i. 0 \<le> f i x"
  2136   and lim: "\<And>x. (\<lambda>i. f i x) ----> u x"
  1866   and lim: "\<And>x. (\<lambda>i. f i x) ----> u x"
  2137   and ilim: "(\<lambda>i. integral\<^isup>L M (f i)) ----> x"
  1867   and ilim: "(\<lambda>i. integral\<^isup>L M (f i)) ----> x"
  2138   shows "integrable M u"
  1868   shows "integrable M u"
  2155 
  1885 
  2156   hence [simp]: "\<And>i. (\<integral>\<^isup>+x. ereal (- f i x) \<partial>M) = 0" "(\<integral>\<^isup>+x. ereal (- u x) \<partial>M) = 0"
  1886   hence [simp]: "\<And>i. (\<integral>\<^isup>+x. ereal (- f i x) \<partial>M) = 0" "(\<integral>\<^isup>+x. ereal (- u x) \<partial>M) = 0"
  2157     using i borel_u pos pos_u by (auto simp: positive_integral_0_iff_AE integrable_def)
  1887     using i borel_u pos pos_u by (auto simp: positive_integral_0_iff_AE integrable_def)
  2158 
  1888 
  2159   have integral_eq: "\<And>n. (\<integral>\<^isup>+ x. ereal (f n x) \<partial>M) = ereal (integral\<^isup>L M (f n))"
  1889   have integral_eq: "\<And>n. (\<integral>\<^isup>+ x. ereal (f n x) \<partial>M) = ereal (integral\<^isup>L M (f n))"
  2160     using i positive_integral_positive by (auto simp: ereal_real lebesgue_integral_def integrable_def)
  1890     using i positive_integral_positive[of M] by (auto simp: ereal_real lebesgue_integral_def integrable_def)
  2161 
  1891 
  2162   have pos_integral: "\<And>n. 0 \<le> integral\<^isup>L M (f n)"
  1892   have pos_integral: "\<And>n. 0 \<le> integral\<^isup>L M (f n)"
  2163     using pos i by (auto simp: integral_positive)
  1893     using pos i by (auto simp: integral_positive)
  2164   hence "0 \<le> x"
  1894   hence "0 \<le> x"
  2165     using LIMSEQ_le_const[OF ilim, of 0] by auto
  1895     using LIMSEQ_le_const[OF ilim, of 0] by auto
  2175   qed
  1905   qed
  2176   finally show  "integrable M u" "integral\<^isup>L M u = x" using borel_u `0 \<le> x`
  1906   finally show  "integrable M u" "integral\<^isup>L M u = x" using borel_u `0 \<le> x`
  2177     unfolding integrable_def lebesgue_integral_def by auto
  1907     unfolding integrable_def lebesgue_integral_def by auto
  2178 qed
  1908 qed
  2179 
  1909 
  2180 lemma (in measure_space) integral_monotone_convergence:
  1910 lemma integral_monotone_convergence:
  2181   assumes f: "\<And>i. integrable M (f i)" and "mono f"
  1911   assumes f: "\<And>i. integrable M (f i)" and "mono f"
  2182   and lim: "\<And>x. (\<lambda>i. f i x) ----> u x"
  1912   and lim: "\<And>x. (\<lambda>i. f i x) ----> u x"
  2183   and ilim: "(\<lambda>i. integral\<^isup>L M (f i)) ----> x"
  1913   and ilim: "(\<lambda>i. integral\<^isup>L M (f i)) ----> x"
  2184   shows "integrable M u"
  1914   shows "integrable M u"
  2185   and "integral\<^isup>L M u = x"
  1915   and "integral\<^isup>L M u = x"
  2199     using diff(1) f by (rule integral_add(1))
  1929     using diff(1) f by (rule integral_add(1))
  2200   with diff(2) f show "integrable M u" "integral\<^isup>L M u = x"
  1930   with diff(2) f show "integrable M u" "integral\<^isup>L M u = x"
  2201     by (auto simp: integral_diff)
  1931     by (auto simp: integral_diff)
  2202 qed
  1932 qed
  2203 
  1933 
  2204 lemma (in measure_space) integral_0_iff:
  1934 lemma integral_0_iff:
  2205   assumes "integrable M f"
  1935   assumes "integrable M f"
  2206   shows "(\<integral>x. \<bar>f x\<bar> \<partial>M) = 0 \<longleftrightarrow> \<mu> {x\<in>space M. f x \<noteq> 0} = 0"
  1936   shows "(\<integral>x. \<bar>f x\<bar> \<partial>M) = 0 \<longleftrightarrow> (emeasure M) {x\<in>space M. f x \<noteq> 0} = 0"
  2207 proof -
  1937 proof -
  2208   have *: "(\<integral>\<^isup>+x. ereal (- \<bar>f x\<bar>) \<partial>M) = 0"
  1938   have *: "(\<integral>\<^isup>+x. ereal (- \<bar>f x\<bar>) \<partial>M) = 0"
  2209     using assms by (auto simp: positive_integral_0_iff_AE integrable_def)
  1939     using assms by (auto simp: positive_integral_0_iff_AE integrable_def)
  2210   have "integrable M (\<lambda>x. \<bar>f x\<bar>)" using assms by (rule integrable_abs)
  1940   have "integrable M (\<lambda>x. \<bar>f x\<bar>)" using assms by (rule integrable_abs)
  2211   hence "(\<lambda>x. ereal (\<bar>f x\<bar>)) \<in> borel_measurable M"
  1941   hence "(\<lambda>x. ereal (\<bar>f x\<bar>)) \<in> borel_measurable M"
  2212     "(\<integral>\<^isup>+ x. ereal \<bar>f x\<bar> \<partial>M) \<noteq> \<infinity>" unfolding integrable_def by auto
  1942     "(\<integral>\<^isup>+ x. ereal \<bar>f x\<bar> \<partial>M) \<noteq> \<infinity>" unfolding integrable_def by auto
  2213   from positive_integral_0_iff[OF this(1)] this(2)
  1943   from positive_integral_0_iff[OF this(1)] this(2)
  2214   show ?thesis unfolding lebesgue_integral_def *
  1944   show ?thesis unfolding lebesgue_integral_def *
  2215     using positive_integral_positive[of "\<lambda>x. ereal \<bar>f x\<bar>"]
  1945     using positive_integral_positive[of M "\<lambda>x. ereal \<bar>f x\<bar>"]
  2216     by (auto simp add: real_of_ereal_eq_0)
  1946     by (auto simp add: real_of_ereal_eq_0)
  2217 qed
  1947 qed
  2218 
  1948 
  2219 lemma (in measure_space) positive_integral_PInf:
  1949 lemma positive_integral_PInf:
  2220   assumes f: "f \<in> borel_measurable M"
  1950   assumes f: "f \<in> borel_measurable M"
  2221   and not_Inf: "integral\<^isup>P M f \<noteq> \<infinity>"
  1951   and not_Inf: "integral\<^isup>P M f \<noteq> \<infinity>"
  2222   shows "\<mu> (f -` {\<infinity>} \<inter> space M) = 0"
  1952   shows "(emeasure M) (f -` {\<infinity>} \<inter> space M) = 0"
  2223 proof -
  1953 proof -
  2224   have "\<infinity> * \<mu> (f -` {\<infinity>} \<inter> space M) = (\<integral>\<^isup>+ x. \<infinity> * indicator (f -` {\<infinity>} \<inter> space M) x \<partial>M)"
  1954   have "\<infinity> * (emeasure M) (f -` {\<infinity>} \<inter> space M) = (\<integral>\<^isup>+ x. \<infinity> * indicator (f -` {\<infinity>} \<inter> space M) x \<partial>M)"
  2225     using f by (subst positive_integral_cmult_indicator) (auto simp: measurable_sets)
  1955     using f by (subst positive_integral_cmult_indicator) (auto simp: measurable_sets)
  2226   also have "\<dots> \<le> integral\<^isup>P M (\<lambda>x. max 0 (f x))"
  1956   also have "\<dots> \<le> integral\<^isup>P M (\<lambda>x. max 0 (f x))"
  2227     by (auto intro!: positive_integral_mono simp: indicator_def max_def)
  1957     by (auto intro!: positive_integral_mono simp: indicator_def max_def)
  2228   finally have "\<infinity> * \<mu> (f -` {\<infinity>} \<inter> space M) \<le> integral\<^isup>P M f"
  1958   finally have "\<infinity> * (emeasure M) (f -` {\<infinity>} \<inter> space M) \<le> integral\<^isup>P M f"
  2229     by (simp add: positive_integral_max_0)
  1959     by (simp add: positive_integral_max_0)
  2230   moreover have "0 \<le> \<mu> (f -` {\<infinity>} \<inter> space M)"
  1960   moreover have "0 \<le> (emeasure M) (f -` {\<infinity>} \<inter> space M)"
  2231     using f by (simp add: measurable_sets)
  1961     by (rule emeasure_nonneg)
  2232   ultimately show ?thesis
  1962   ultimately show ?thesis
  2233     using assms by (auto split: split_if_asm)
  1963     using assms by (auto split: split_if_asm)
  2234 qed
  1964 qed
  2235 
  1965 
  2236 lemma (in measure_space) positive_integral_PInf_AE:
  1966 lemma positive_integral_PInf_AE:
  2237   assumes "f \<in> borel_measurable M" "integral\<^isup>P M f \<noteq> \<infinity>" shows "AE x. f x \<noteq> \<infinity>"
  1967   assumes "f \<in> borel_measurable M" "integral\<^isup>P M f \<noteq> \<infinity>" shows "AE x in M. f x \<noteq> \<infinity>"
  2238 proof (rule AE_I)
  1968 proof (rule AE_I)
  2239   show "\<mu> (f -` {\<infinity>} \<inter> space M) = 0"
  1969   show "(emeasure M) (f -` {\<infinity>} \<inter> space M) = 0"
  2240     by (rule positive_integral_PInf[OF assms])
  1970     by (rule positive_integral_PInf[OF assms])
  2241   show "f -` {\<infinity>} \<inter> space M \<in> sets M"
  1971   show "f -` {\<infinity>} \<inter> space M \<in> sets M"
  2242     using assms by (auto intro: borel_measurable_vimage)
  1972     using assms by (auto intro: borel_measurable_vimage)
  2243 qed auto
  1973 qed auto
  2244 
  1974 
  2245 lemma (in measure_space) simple_integral_PInf:
  1975 lemma simple_integral_PInf:
  2246   assumes "simple_function M f" "\<And>x. 0 \<le> f x"
  1976   assumes "simple_function M f" "\<And>x. 0 \<le> f x"
  2247   and "integral\<^isup>S M f \<noteq> \<infinity>"
  1977   and "integral\<^isup>S M f \<noteq> \<infinity>"
  2248   shows "\<mu> (f -` {\<infinity>} \<inter> space M) = 0"
  1978   shows "(emeasure M) (f -` {\<infinity>} \<inter> space M) = 0"
  2249 proof (rule positive_integral_PInf)
  1979 proof (rule positive_integral_PInf)
  2250   show "f \<in> borel_measurable M" using assms by (auto intro: borel_measurable_simple_function)
  1980   show "f \<in> borel_measurable M" using assms by (auto intro: borel_measurable_simple_function)
  2251   show "integral\<^isup>P M f \<noteq> \<infinity>"
  1981   show "integral\<^isup>P M f \<noteq> \<infinity>"
  2252     using assms by (simp add: positive_integral_eq_simple_integral)
  1982     using assms by (simp add: positive_integral_eq_simple_integral)
  2253 qed
  1983 qed
  2254 
  1984 
  2255 lemma (in measure_space) integral_real:
  1985 lemma integral_real:
  2256   "AE x. \<bar>f x\<bar> \<noteq> \<infinity> \<Longrightarrow> (\<integral>x. real (f x) \<partial>M) = real (integral\<^isup>P M f) - real (integral\<^isup>P M (\<lambda>x. - f x))"
  1986   "AE x in M. \<bar>f x\<bar> \<noteq> \<infinity> \<Longrightarrow> (\<integral>x. real (f x) \<partial>M) = real (integral\<^isup>P M f) - real (integral\<^isup>P M (\<lambda>x. - f x))"
  2257   using assms unfolding lebesgue_integral_def
  1987   using assms unfolding lebesgue_integral_def
  2258   by (subst (1 2) positive_integral_cong_AE) (auto simp add: ereal_real)
  1988   by (subst (1 2) positive_integral_cong_AE) (auto simp add: ereal_real)
  2259 
  1989 
  2260 lemma (in finite_measure) lebesgue_integral_const[simp]:
  1990 lemma (in finite_measure) lebesgue_integral_const[simp]:
  2261   shows "integrable M (\<lambda>x. a)"
  1991   shows "integrable M (\<lambda>x. a)"
  2262   and  "(\<integral>x. a \<partial>M) = a * \<mu>' (space M)"
  1992   and  "(\<integral>x. a \<partial>M) = a * (measure M) (space M)"
  2263 proof -
  1993 proof -
  2264   { fix a :: real assume "0 \<le> a"
  1994   { fix a :: real assume "0 \<le> a"
  2265     then have "(\<integral>\<^isup>+ x. ereal a \<partial>M) = ereal a * \<mu> (space M)"
  1995     then have "(\<integral>\<^isup>+ x. ereal a \<partial>M) = ereal a * (emeasure M) (space M)"
  2266       by (subst positive_integral_const) auto
  1996       by (subst positive_integral_const) auto
  2267     moreover
  1997     moreover
  2268     from `0 \<le> a` have "(\<integral>\<^isup>+ x. ereal (-a) \<partial>M) = 0"
  1998     from `0 \<le> a` have "(\<integral>\<^isup>+ x. ereal (-a) \<partial>M) = 0"
  2269       by (subst positive_integral_0_iff_AE) auto
  1999       by (subst positive_integral_0_iff_AE) auto
  2270     ultimately have "integrable M (\<lambda>x. a)" by (auto simp: integrable_def) }
  2000     ultimately have "integrable M (\<lambda>x. a)" by (auto simp: integrable_def) }
  2275   next
  2005   next
  2276     assume "\<not> 0 \<le> a"
  2006     assume "\<not> 0 \<le> a"
  2277     then have "0 \<le> -a" by auto
  2007     then have "0 \<le> -a" by auto
  2278     from *[OF this] show ?thesis by simp
  2008     from *[OF this] show ?thesis by simp
  2279   qed
  2009   qed
  2280   show "(\<integral>x. a \<partial>M) = a * \<mu>' (space M)"
  2010   show "(\<integral>x. a \<partial>M) = a * measure M (space M)"
  2281     by (simp add: \<mu>'_def lebesgue_integral_def positive_integral_const_If)
  2011     by (simp add: lebesgue_integral_def positive_integral_const_If emeasure_eq_measure)
  2282 qed
  2012 qed
  2283 
  2013 
  2284 lemma indicator_less[simp]:
  2014 lemma indicator_less[simp]:
  2285   "indicator A x \<le> (indicator B x::ereal) \<longleftrightarrow> (x \<in> A \<longrightarrow> x \<in> B)"
  2015   "indicator A x \<le> (indicator B x::ereal) \<longleftrightarrow> (x \<in> A \<longrightarrow> x \<in> B)"
  2286   by (simp add: indicator_def not_le)
  2016   by (simp add: indicator_def not_le)
  2287 
  2017 
  2288 lemma (in finite_measure) integral_less_AE:
  2018 lemma (in finite_measure) integral_less_AE:
  2289   assumes int: "integrable M X" "integrable M Y"
  2019   assumes int: "integrable M X" "integrable M Y"
  2290   assumes A: "\<mu> A \<noteq> 0" "A \<in> sets M" "AE x. x \<in> A \<longrightarrow> X x \<noteq> Y x"
  2020   assumes A: "(emeasure M) A \<noteq> 0" "A \<in> sets M" "AE x in M. x \<in> A \<longrightarrow> X x \<noteq> Y x"
  2291   assumes gt: "AE x. X x \<le> Y x"
  2021   assumes gt: "AE x in M. X x \<le> Y x"
  2292   shows "integral\<^isup>L M X < integral\<^isup>L M Y"
  2022   shows "integral\<^isup>L M X < integral\<^isup>L M Y"
  2293 proof -
  2023 proof -
  2294   have "integral\<^isup>L M X \<le> integral\<^isup>L M Y"
  2024   have "integral\<^isup>L M X \<le> integral\<^isup>L M Y"
  2295     using gt int by (intro integral_mono_AE) auto
  2025     using gt int by (intro integral_mono_AE) auto
  2296   moreover
  2026   moreover
  2299     assume eq: "integral\<^isup>L M X = integral\<^isup>L M Y"
  2029     assume eq: "integral\<^isup>L M X = integral\<^isup>L M Y"
  2300     have "integral\<^isup>L M (\<lambda>x. \<bar>Y x - X x\<bar>) = integral\<^isup>L M (\<lambda>x. Y x - X x)"
  2030     have "integral\<^isup>L M (\<lambda>x. \<bar>Y x - X x\<bar>) = integral\<^isup>L M (\<lambda>x. Y x - X x)"
  2301       using gt by (intro integral_cong_AE) auto
  2031       using gt by (intro integral_cong_AE) auto
  2302     also have "\<dots> = 0"
  2032     also have "\<dots> = 0"
  2303       using eq int by simp
  2033       using eq int by simp
  2304     finally have "\<mu> {x \<in> space M. Y x - X x \<noteq> 0} = 0"
  2034     finally have "(emeasure M) {x \<in> space M. Y x - X x \<noteq> 0} = 0"
  2305       using int by (simp add: integral_0_iff)
  2035       using int by (simp add: integral_0_iff)
  2306     moreover
  2036     moreover
  2307     have "(\<integral>\<^isup>+x. indicator A x \<partial>M) \<le> (\<integral>\<^isup>+x. indicator {x \<in> space M. Y x - X x \<noteq> 0} x \<partial>M)"
  2037     have "(\<integral>\<^isup>+x. indicator A x \<partial>M) \<le> (\<integral>\<^isup>+x. indicator {x \<in> space M. Y x - X x \<noteq> 0} x \<partial>M)"
  2308       using A by (intro positive_integral_mono_AE) auto
  2038       using A by (intro positive_integral_mono_AE) auto
  2309     then have "\<mu> A \<le> \<mu> {x \<in> space M. Y x - X x \<noteq> 0}"
  2039     then have "(emeasure M) A \<le> (emeasure M) {x \<in> space M. Y x - X x \<noteq> 0}"
  2310       using int A by (simp add: integrable_def)
  2040       using int A by (simp add: integrable_def)
  2311     moreover note `\<mu> A \<noteq> 0` positive_measure[OF `A \<in> sets M`]
  2041     ultimately have "emeasure M A = 0"
  2312     ultimately show False by auto
  2042       using emeasure_nonneg[of M A] by simp
       
  2043     with `(emeasure M) A \<noteq> 0` show False by auto
  2313   qed
  2044   qed
  2314   ultimately show ?thesis by auto
  2045   ultimately show ?thesis by auto
  2315 qed
  2046 qed
  2316 
  2047 
  2317 lemma (in finite_measure) integral_less_AE_space:
  2048 lemma (in finite_measure) integral_less_AE_space:
  2318   assumes int: "integrable M X" "integrable M Y"
  2049   assumes int: "integrable M X" "integrable M Y"
  2319   assumes gt: "AE x. X x < Y x" "\<mu> (space M) \<noteq> 0"
  2050   assumes gt: "AE x in M. X x < Y x" "(emeasure M) (space M) \<noteq> 0"
  2320   shows "integral\<^isup>L M X < integral\<^isup>L M Y"
  2051   shows "integral\<^isup>L M X < integral\<^isup>L M Y"
  2321   using gt by (intro integral_less_AE[OF int, where A="space M"]) auto
  2052   using gt by (intro integral_less_AE[OF int, where A="space M"]) auto
  2322 
  2053 
  2323 lemma (in measure_space) integral_dominated_convergence:
  2054 lemma integral_dominated_convergence:
  2324   assumes u: "\<And>i. integrable M (u i)" and bound: "\<And>x j. x\<in>space M \<Longrightarrow> \<bar>u j x\<bar> \<le> w x"
  2055   assumes u: "\<And>i. integrable M (u i)" and bound: "\<And>x j. x\<in>space M \<Longrightarrow> \<bar>u j x\<bar> \<le> w x"
  2325   and w: "integrable M w"
  2056   and w: "integrable M w"
  2326   and u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  2057   and u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  2327   shows "integrable M u'"
  2058   shows "integrable M u'"
  2328   and "(\<lambda>i. (\<integral>x. \<bar>u i x - u' x\<bar> \<partial>M)) ----> 0" (is "?lim_diff")
  2059   and "(\<lambda>i. (\<integral>x. \<bar>u i x - u' x\<bar> \<partial>M)) ----> 0" (is "?lim_diff")
  2334     have "\<bar>u' x\<bar> \<le> w x" using bound[OF x] by auto }
  2065     have "\<bar>u' x\<bar> \<le> w x" using bound[OF x] by auto }
  2335   note u'_bound = this
  2066   note u'_bound = this
  2336 
  2067 
  2337   from u[unfolded integrable_def]
  2068   from u[unfolded integrable_def]
  2338   have u'_borel: "u' \<in> borel_measurable M"
  2069   have u'_borel: "u' \<in> borel_measurable M"
  2339     using u' by (blast intro: borel_measurable_LIMSEQ[of u])
  2070     using u' by (blast intro: borel_measurable_LIMSEQ[of M u])
  2340 
  2071 
  2341   { fix x assume x: "x \<in> space M"
  2072   { fix x assume x: "x \<in> space M"
  2342     then have "0 \<le> \<bar>u 0 x\<bar>" by auto
  2073     then have "0 \<le> \<bar>u 0 x\<bar>" by auto
  2343     also have "\<dots> \<le> w x" using bound[OF x] by auto
  2074     also have "\<dots> \<le> w x" using bound[OF x] by auto
  2344     finally have "0 \<le> w x" . }
  2075     finally have "0 \<le> w x" . }
  2383     { fix n
  2114     { fix n
  2384       have "?f n \<le> ?wx" (is "integral\<^isup>P M ?f' \<le> _")
  2115       have "?f n \<le> ?wx" (is "integral\<^isup>P M ?f' \<le> _")
  2385         using diff_less_2w[of _ n] unfolding positive_integral_max_0
  2116         using diff_less_2w[of _ n] unfolding positive_integral_max_0
  2386         by (intro positive_integral_mono) auto
  2117         by (intro positive_integral_mono) auto
  2387       then have "?f n = 0"
  2118       then have "?f n = 0"
  2388         using positive_integral_positive[of ?f'] eq_0 by auto }
  2119         using positive_integral_positive[of M ?f'] eq_0 by auto }
  2389     then show ?thesis by (simp add: Limsup_const)
  2120     then show ?thesis by (simp add: Limsup_const)
  2390   next
  2121   next
  2391     assume neq_0: "(\<integral>\<^isup>+ x. max 0 (ereal (2 * w x)) \<partial>M) \<noteq> 0" (is "?wx \<noteq> 0")
  2122     assume neq_0: "(\<integral>\<^isup>+ x. max 0 (ereal (2 * w x)) \<partial>M) \<noteq> 0" (is "?wx \<noteq> 0")
  2392     have "0 = limsup (\<lambda>n. 0 :: ereal)" by (simp add: Limsup_const)
  2123     have "0 = limsup (\<lambda>n. 0 :: ereal)" by (simp add: Limsup_const)
  2393     also have "\<dots> \<le> limsup (\<lambda>n. \<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)"
  2124     also have "\<dots> \<le> limsup (\<lambda>n. \<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)"
  2409       using u'_borel w u unfolding integrable_def
  2140       using u'_borel w u unfolding integrable_def
  2410       by (intro positive_integral_lim_INF) (auto intro!: positive_integral_lim_INF)
  2141       by (intro positive_integral_lim_INF) (auto intro!: positive_integral_lim_INF)
  2411     also have "\<dots> = (\<integral>\<^isup>+ x. ereal (2 * w x) \<partial>M) -
  2142     also have "\<dots> = (\<integral>\<^isup>+ x. ereal (2 * w x) \<partial>M) -
  2412         limsup (\<lambda>n. \<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)"
  2143         limsup (\<lambda>n. \<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)"
  2413       unfolding PI_diff positive_integral_max_0
  2144       unfolding PI_diff positive_integral_max_0
  2414       using positive_integral_positive[of "\<lambda>x. ereal (2 * w x)"]
  2145       using positive_integral_positive[of M "\<lambda>x. ereal (2 * w x)"]
  2415       by (subst liminf_ereal_cminus) auto
  2146       by (subst liminf_ereal_cminus) auto
  2416     finally show ?thesis
  2147     finally show ?thesis
  2417       using neq_0 I2w_fin positive_integral_positive[of "\<lambda>x. ereal (2 * w x)"] pos
  2148       using neq_0 I2w_fin positive_integral_positive[of M "\<lambda>x. ereal (2 * w x)"] pos
  2418       unfolding positive_integral_max_0
  2149       unfolding positive_integral_max_0
  2419       by (cases rule: ereal2_cases[of "\<integral>\<^isup>+ x. ereal (2 * w x) \<partial>M" "limsup (\<lambda>n. \<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)"])
  2150       by (cases rule: ereal2_cases[of "\<integral>\<^isup>+ x. ereal (2 * w x) \<partial>M" "limsup (\<lambda>n. \<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)"])
  2420          auto
  2151          auto
  2421   qed
  2152   qed
  2422 
  2153 
  2428       by (intro liminf_mono positive_integral_positive)
  2159       by (intro liminf_mono positive_integral_positive)
  2429     finally have "0 \<le> liminf ?f" . }
  2160     finally have "0 \<le> liminf ?f" . }
  2430   ultimately have liminf_limsup_eq: "liminf ?f = ereal 0" "limsup ?f = ereal 0"
  2161   ultimately have liminf_limsup_eq: "liminf ?f = ereal 0" "limsup ?f = ereal 0"
  2431     using `limsup ?f = 0` by auto
  2162     using `limsup ?f = 0` by auto
  2432   have "\<And>n. (\<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M) = ereal (\<integral>x. \<bar>u n x - u' x\<bar> \<partial>M)"
  2163   have "\<And>n. (\<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M) = ereal (\<integral>x. \<bar>u n x - u' x\<bar> \<partial>M)"
  2433     using diff positive_integral_positive
  2164     using diff positive_integral_positive[of M]
  2434     by (subst integral_eq_positive_integral) (auto simp: ereal_real integrable_def)
  2165     by (subst integral_eq_positive_integral[of _ M]) (auto simp: ereal_real integrable_def)
  2435   then show ?lim_diff
  2166   then show ?lim_diff
  2436     using ereal_Liminf_eq_Limsup[OF trivial_limit_sequentially liminf_limsup_eq]
  2167     using ereal_Liminf_eq_Limsup[OF trivial_limit_sequentially liminf_limsup_eq]
  2437     by (simp add: lim_ereal)
  2168     by (simp add: lim_ereal)
  2438 
  2169 
  2439   show ?lim
  2170   show ?lim
  2454       finally show "norm (integral\<^isup>L M (u n) - integral\<^isup>L M u') < r" by simp
  2185       finally show "norm (integral\<^isup>L M (u n) - integral\<^isup>L M u') < r" by simp
  2455     qed
  2186     qed
  2456   qed
  2187   qed
  2457 qed
  2188 qed
  2458 
  2189 
  2459 lemma (in measure_space) integral_sums:
  2190 lemma integral_sums:
  2460   assumes borel: "\<And>i. integrable M (f i)"
  2191   assumes borel: "\<And>i. integrable M (f i)"
  2461   and summable: "\<And>x. x \<in> space M \<Longrightarrow> summable (\<lambda>i. \<bar>f i x\<bar>)"
  2192   and summable: "\<And>x. x \<in> space M \<Longrightarrow> summable (\<lambda>i. \<bar>f i x\<bar>)"
  2462   and sums: "summable (\<lambda>i. (\<integral>x. \<bar>f i x\<bar> \<partial>M))"
  2193   and sums: "summable (\<lambda>i. (\<integral>x. \<bar>f i x\<bar> \<partial>M))"
  2463   shows "integrable M (\<lambda>x. (\<Sum>i. f i x))" (is "integrable M ?S")
  2194   shows "integrable M (\<lambda>x. (\<Sum>i. f i x))" (is "integrable M ?S")
  2464   and "(\<lambda>i. integral\<^isup>L M (f i)) sums (\<integral>x. (\<Sum>i. f i x) \<partial>M)" (is ?integral)
  2195   and "(\<lambda>i. integral\<^isup>L M (f i)) sums (\<integral>x. (\<Sum>i. f i x) \<partial>M)" (is ?integral)
  2511     using int(2) by simp
  2242     using int(2) by simp
  2512 qed
  2243 qed
  2513 
  2244 
  2514 section "Lebesgue integration on countable spaces"
  2245 section "Lebesgue integration on countable spaces"
  2515 
  2246 
  2516 lemma (in measure_space) integral_on_countable:
  2247 lemma integral_on_countable:
  2517   assumes f: "f \<in> borel_measurable M"
  2248   assumes f: "f \<in> borel_measurable M"
  2518   and bij: "bij_betw enum S (f ` space M)"
  2249   and bij: "bij_betw enum S (f ` space M)"
  2519   and enum_zero: "enum ` (-S) \<subseteq> {0}"
  2250   and enum_zero: "enum ` (-S) \<subseteq> {0}"
  2520   and fin: "\<And>x. x \<noteq> 0 \<Longrightarrow> \<mu> (f -` {x} \<inter> space M) \<noteq> \<infinity>"
  2251   and fin: "\<And>x. x \<noteq> 0 \<Longrightarrow> (emeasure M) (f -` {x} \<inter> space M) \<noteq> \<infinity>"
  2521   and abs_summable: "summable (\<lambda>r. \<bar>enum r * real (\<mu> (f -` {enum r} \<inter> space M))\<bar>)"
  2252   and abs_summable: "summable (\<lambda>r. \<bar>enum r * real ((emeasure M) (f -` {enum r} \<inter> space M))\<bar>)"
  2522   shows "integrable M f"
  2253   shows "integrable M f"
  2523   and "(\<lambda>r. enum r * real (\<mu> (f -` {enum r} \<inter> space M))) sums integral\<^isup>L M f" (is ?sums)
  2254   and "(\<lambda>r. enum r * real ((emeasure M) (f -` {enum r} \<inter> space M))) sums integral\<^isup>L M f" (is ?sums)
  2524 proof -
  2255 proof -
  2525   let ?A = "\<lambda>r. f -` {enum r} \<inter> space M"
  2256   let ?A = "\<lambda>r. f -` {enum r} \<inter> space M"
  2526   let ?F = "\<lambda>r x. enum r * indicator (?A r) x"
  2257   let ?F = "\<lambda>r x. enum r * indicator (?A r) x"
  2527   have enum_eq: "\<And>r. enum r * real (\<mu> (?A r)) = integral\<^isup>L M (?F r)"
  2258   have enum_eq: "\<And>r. enum r * real ((emeasure M) (?A r)) = integral\<^isup>L M (?F r)"
  2528     using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
  2259     using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
  2529 
  2260 
  2530   { fix x assume "x \<in> space M"
  2261   { fix x assume "x \<in> space M"
  2531     hence "f x \<in> enum ` S" using bij unfolding bij_betw_def by auto
  2262     hence "f x \<in> enum ` S" using bij unfolding bij_betw_def by auto
  2532     then obtain i where "i\<in>S" "enum i = f x" by auto
  2263     then obtain i where "i\<in>S" "enum i = f x" by auto
  2549     using F_sums_f by (auto intro!: integral_cong integrable_cong simp: sums_iff)
  2280     using F_sums_f by (auto intro!: integral_cong integrable_cong simp: sums_iff)
  2550 
  2281 
  2551   { fix r
  2282   { fix r
  2552     have "(\<integral>x. \<bar>?F r x\<bar> \<partial>M) = (\<integral>x. \<bar>enum r\<bar> * indicator (?A r) x \<partial>M)"
  2283     have "(\<integral>x. \<bar>?F r x\<bar> \<partial>M) = (\<integral>x. \<bar>enum r\<bar> * indicator (?A r) x \<partial>M)"
  2553       by (auto simp: indicator_def intro!: integral_cong)
  2284       by (auto simp: indicator_def intro!: integral_cong)
  2554     also have "\<dots> = \<bar>enum r\<bar> * real (\<mu> (?A r))"
  2285     also have "\<dots> = \<bar>enum r\<bar> * real ((emeasure M) (?A r))"
  2555       using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
  2286       using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
  2556     finally have "(\<integral>x. \<bar>?F r x\<bar> \<partial>M) = \<bar>enum r * real (\<mu> (?A r))\<bar>"
  2287     finally have "(\<integral>x. \<bar>?F r x\<bar> \<partial>M) = \<bar>enum r * real ((emeasure M) (?A r))\<bar>"
  2557       using f by (subst (2) abs_mult_pos[symmetric]) (auto intro!: real_of_ereal_pos measurable_sets) }
  2288       using f by (subst (2) abs_mult_pos[symmetric]) (auto intro!: real_of_ereal_pos measurable_sets) }
  2558   note int_abs_F = this
  2289   note int_abs_F = this
  2559 
  2290 
  2560   have 1: "\<And>i. integrable M (\<lambda>x. ?F i x)"
  2291   have 1: "\<And>i. integrable M (\<lambda>x. ?F i x)"
  2561     using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
  2292     using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
  2568 
  2299 
  2569   from integral_sums(1)[OF 1 2, unfolded int_abs_F, OF _ abs_summable]
  2300   from integral_sums(1)[OF 1 2, unfolded int_abs_F, OF _ abs_summable]
  2570   show "integrable M f" unfolding int_f by simp
  2301   show "integrable M f" unfolding int_f by simp
  2571 qed
  2302 qed
  2572 
  2303 
  2573 section "Lebesgue integration on finite space"
  2304 section {* Distributions *}
  2574 
  2305 
  2575 lemma (in measure_space) integral_on_finite:
  2306 lemma simple_function_distr[simp]:
  2576   assumes f: "f \<in> borel_measurable M" and finite: "finite (f`space M)"
  2307   "simple_function (distr M M' T) f \<longleftrightarrow> simple_function M' (\<lambda>x. f x)"
  2577   and fin: "\<And>x. x \<noteq> 0 \<Longrightarrow> \<mu> (f -` {x} \<inter> space M) \<noteq> \<infinity>"
  2308  unfolding simple_function_def by simp
  2578   shows "integrable M f"
  2309 
  2579   and "(\<integral>x. f x \<partial>M) =
  2310 lemma positive_integral_distr:
  2580     (\<Sum> r \<in> f`space M. r * real (\<mu> (f -` {r} \<inter> space M)))" (is "?integral")
  2311   assumes T: "T \<in> measurable M M'"
  2581 proof -
  2312   and f: "f \<in> borel_measurable M'"
  2582   let ?A = "\<lambda>r. f -` {r} \<inter> space M"
  2313   shows "integral\<^isup>P (distr M M' T) f = (\<integral>\<^isup>+ x. f (T x) \<partial>M)"
  2583   let ?S = "\<lambda>x. \<Sum>r\<in>f`space M. r * indicator (?A r) x"
  2314 proof -
  2584 
  2315   from borel_measurable_implies_simple_function_sequence'[OF f]
       
  2316   guess f' . note f' = this
       
  2317   then have f_distr: "\<And>i. simple_function (distr M M' T) (f' i)"
       
  2318     by simp
       
  2319   let ?f = "\<lambda>i x. f' i (T x)"
       
  2320   have inc: "incseq ?f" using f' by (force simp: le_fun_def incseq_def)
       
  2321   have sup: "\<And>x. (SUP i. ?f i x) = max 0 (f (T x))"
       
  2322     using f'(4) .
       
  2323   have sf: "\<And>i. simple_function M (\<lambda>x. f' i (T x))"
       
  2324     using simple_function_comp[OF T(1) f'(1)] .
       
  2325   show "integral\<^isup>P (distr M M' T) f = (\<integral>\<^isup>+ x. f (T x) \<partial>M)"
       
  2326     using
       
  2327       positive_integral_monotone_convergence_simple[OF f'(2,5) f_distr]
       
  2328       positive_integral_monotone_convergence_simple[OF inc f'(5) sf]
       
  2329     by (simp add: positive_integral_max_0 simple_integral_distr[OF T f'(1)] f')
       
  2330 qed
       
  2331 
       
  2332 lemma integral_distr:
       
  2333   assumes T: "T \<in> measurable M M'"
       
  2334   assumes f: "f \<in> borel_measurable M'"
       
  2335   shows "integral\<^isup>L (distr M M' T) f = (\<integral>x. f (T x) \<partial>M)"
       
  2336 proof -
       
  2337   from measurable_comp[OF T, of f borel]
       
  2338   have borel: "(\<lambda>x. ereal (f x)) \<in> borel_measurable M'" "(\<lambda>x. ereal (- f x)) \<in> borel_measurable M'"
       
  2339     and "(\<lambda>x. f (T x)) \<in> borel_measurable M"
       
  2340     using f by (auto simp: comp_def)
       
  2341   then show ?thesis
       
  2342     using f unfolding lebesgue_integral_def integrable_def
       
  2343     by (auto simp: borel[THEN positive_integral_distr[OF T]])
       
  2344 qed
       
  2345 
       
  2346 lemma integrable_distr:
       
  2347   assumes T: "T \<in> measurable M M'" and f: "integrable (distr M M' T) f"
       
  2348   shows "integrable M (\<lambda>x. f (T x))"
       
  2349 proof -
       
  2350   from measurable_comp[OF T, of f borel]
       
  2351   have borel: "(\<lambda>x. ereal (f x)) \<in> borel_measurable M'" "(\<lambda>x. ereal (- f x)) \<in> borel_measurable M'"
       
  2352     and "(\<lambda>x. f (T x)) \<in> borel_measurable M"
       
  2353     using f by (auto simp: comp_def)
       
  2354   then show ?thesis
       
  2355     using f unfolding lebesgue_integral_def integrable_def
       
  2356     using borel[THEN positive_integral_distr[OF T]]
       
  2357     by (auto simp: borel[THEN positive_integral_distr[OF T]])
       
  2358 qed
       
  2359 
       
  2360 section {* Lebesgue integration on @{const count_space} *}
       
  2361 
       
  2362 lemma simple_function_count_space[simp]:
       
  2363   "simple_function (count_space A) f \<longleftrightarrow> finite (f ` A)"
       
  2364   unfolding simple_function_def by simp
       
  2365 
       
  2366 lemma positive_integral_count_space:
       
  2367   assumes A: "finite {a\<in>A. 0 < f a}"
       
  2368   shows "integral\<^isup>P (count_space A) f = (\<Sum>a|a\<in>A \<and> 0 < f a. f a)"
       
  2369 proof -
       
  2370   have *: "(\<integral>\<^isup>+x. max 0 (f x) \<partial>count_space A) =
       
  2371     (\<integral>\<^isup>+ x. (\<Sum>a|a\<in>A \<and> 0 < f a. f a * indicator {a} x) \<partial>count_space A)"
       
  2372     by (auto intro!: positive_integral_cong
       
  2373              simp add: indicator_def if_distrib setsum_cases[OF A] max_def le_less)
       
  2374   also have "\<dots> = (\<Sum>a|a\<in>A \<and> 0 < f a. \<integral>\<^isup>+ x. f a * indicator {a} x \<partial>count_space A)"
       
  2375     by (subst positive_integral_setsum)
       
  2376        (simp_all add: AE_count_space ereal_zero_le_0_iff less_imp_le)
       
  2377   also have "\<dots> = (\<Sum>a|a\<in>A \<and> 0 < f a. f a)"
       
  2378     by (auto intro!: setsum_cong simp: positive_integral_cmult_indicator one_ereal_def[symmetric])
       
  2379   finally show ?thesis by (simp add: positive_integral_max_0)
       
  2380 qed
       
  2381 
       
  2382 lemma integrable_count_space:
       
  2383   "finite X \<Longrightarrow> integrable (count_space X) f"
       
  2384   by (auto simp: positive_integral_count_space integrable_def)
       
  2385 
       
  2386 lemma positive_integral_count_space_finite:
       
  2387     "finite A \<Longrightarrow> (\<integral>\<^isup>+x. f x \<partial>count_space A) = (\<Sum>a\<in>A. max 0 (f a))"
       
  2388   by (subst positive_integral_max_0[symmetric])
       
  2389      (auto intro!: setsum_mono_zero_left simp: positive_integral_count_space less_le)
       
  2390 
       
  2391 lemma lebesgue_integral_count_space_finite:
       
  2392     "finite A \<Longrightarrow> (\<integral>x. f x \<partial>count_space A) = (\<Sum>a\<in>A. f a)"
       
  2393   apply (auto intro!: setsum_mono_zero_left
       
  2394               simp: positive_integral_count_space_finite lebesgue_integral_def)
       
  2395   apply (subst (1 2)  setsum_real_of_ereal[symmetric])
       
  2396   apply (auto simp: max_def setsum_subtractf[symmetric] intro!: setsum_cong)
       
  2397   done
       
  2398 
       
  2399 section {* Measure spaces with an associated density *}
       
  2400 
       
  2401 definition density :: "'a measure \<Rightarrow> ('a \<Rightarrow> ereal) \<Rightarrow> 'a measure" where
       
  2402   "density M f = measure_of (space M) (sets M) (\<lambda>A. \<integral>\<^isup>+ x. f x * indicator A x \<partial>M)"
       
  2403 
       
  2404 lemma 
       
  2405   shows sets_density[simp]: "sets (density M f) = sets M"
       
  2406     and space_density[simp]: "space (density M f) = space M"
       
  2407   by (auto simp: density_def)
       
  2408 
       
  2409 lemma 
       
  2410   shows measurable_density_eq1[simp]: "g \<in> measurable (density Mg f) Mg' \<longleftrightarrow> g \<in> measurable Mg Mg'"
       
  2411     and measurable_density_eq2[simp]: "h \<in> measurable Mh (density Mh' f) \<longleftrightarrow> h \<in> measurable Mh Mh'"
       
  2412     and simple_function_density_eq[simp]: "simple_function (density Mu f) u \<longleftrightarrow> simple_function Mu u"
       
  2413   unfolding measurable_def simple_function_def by simp_all
       
  2414 
       
  2415 lemma density_cong: "f \<in> borel_measurable M \<Longrightarrow> f' \<in> borel_measurable M \<Longrightarrow>
       
  2416   (AE x in M. f x = f' x) \<Longrightarrow> density M f = density M f'"
       
  2417   unfolding density_def by (auto intro!: measure_of_eq positive_integral_cong_AE space_closed)
       
  2418 
       
  2419 lemma density_max_0: "density M f = density M (\<lambda>x. max 0 (f x))"
       
  2420 proof -
       
  2421   have "\<And>x A. max 0 (f x) * indicator A x = max 0 (f x * indicator A x)"
       
  2422     by (auto simp: indicator_def)
       
  2423   then show ?thesis
       
  2424     unfolding density_def by (simp add: positive_integral_max_0)
       
  2425 qed
       
  2426 
       
  2427 lemma density_ereal_max_0: "density M (\<lambda>x. ereal (f x)) = density M (\<lambda>x. ereal (max 0 (f x)))"
       
  2428   by (subst density_max_0) (auto intro!: arg_cong[where f="density M"] split: split_max)
       
  2429 
       
  2430 lemma emeasure_density:
       
  2431   assumes f: "f \<in> borel_measurable M" and A: "A \<in> sets M"
       
  2432   shows "emeasure (density M f) A = (\<integral>\<^isup>+ x. f x * indicator A x \<partial>M)"
       
  2433     (is "_ = ?\<mu> A")
       
  2434   unfolding density_def
       
  2435 proof (rule emeasure_measure_of_sigma)
       
  2436   show "sigma_algebra (space M) (sets M)" ..
       
  2437   show "positive (sets M) ?\<mu>"
       
  2438     using f by (auto simp: positive_def intro!: positive_integral_positive)
       
  2439   have \<mu>_eq: "?\<mu> = (\<lambda>A. \<integral>\<^isup>+ x. max 0 (f x) * indicator A x \<partial>M)" (is "?\<mu> = ?\<mu>'")
       
  2440     apply (subst positive_integral_max_0[symmetric])
       
  2441     apply (intro ext positive_integral_cong_AE AE_I2)
       
  2442     apply (auto simp: indicator_def)
       
  2443     done
       
  2444   show "countably_additive (sets M) ?\<mu>"
       
  2445     unfolding \<mu>_eq
       
  2446   proof (intro countably_additiveI)
       
  2447     fix A :: "nat \<Rightarrow> 'a set" assume "range A \<subseteq> sets M"
       
  2448     then have *: "\<And>i. (\<lambda>x. max 0 (f x) * indicator (A i) x) \<in> borel_measurable M"
       
  2449       using f by (auto intro!: borel_measurable_ereal_times)
       
  2450     assume disj: "disjoint_family A"
       
  2451     have "(\<Sum>n. ?\<mu>' (A n)) = (\<integral>\<^isup>+ x. (\<Sum>n. max 0 (f x) * indicator (A n) x) \<partial>M)"
       
  2452       using f * by (simp add: positive_integral_suminf)
       
  2453     also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (f x) * (\<Sum>n. indicator (A n) x) \<partial>M)" using f
       
  2454       by (auto intro!: suminf_cmult_ereal positive_integral_cong_AE)
       
  2455     also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (f x) * indicator (\<Union>n. A n) x \<partial>M)"
       
  2456       unfolding suminf_indicator[OF disj] ..
       
  2457     finally show "(\<Sum>n. ?\<mu>' (A n)) = ?\<mu>' (\<Union>x. A x)" by simp
       
  2458   qed
       
  2459 qed fact
       
  2460 
       
  2461 lemma null_sets_density_iff:
       
  2462   assumes f: "f \<in> borel_measurable M"
       
  2463   shows "A \<in> null_sets (density M f) \<longleftrightarrow> A \<in> sets M \<and> (AE x in M. x \<in> A \<longrightarrow> f x \<le> 0)"
       
  2464 proof -
       
  2465   { assume "A \<in> sets M"
       
  2466     have eq: "(\<integral>\<^isup>+x. f x * indicator A x \<partial>M) = (\<integral>\<^isup>+x. max 0 (f x) * indicator A x \<partial>M)"
       
  2467       apply (subst positive_integral_max_0[symmetric])
       
  2468       apply (intro positive_integral_cong)
       
  2469       apply (auto simp: indicator_def)
       
  2470       done
       
  2471     have "(\<integral>\<^isup>+x. f x * indicator A x \<partial>M) = 0 \<longleftrightarrow> 
       
  2472       emeasure M {x \<in> space M. max 0 (f x) * indicator A x \<noteq> 0} = 0"
       
  2473       unfolding eq
       
  2474       using f `A \<in> sets M`
       
  2475       by (intro positive_integral_0_iff) auto
       
  2476     also have "\<dots> \<longleftrightarrow> (AE x in M. max 0 (f x) * indicator A x = 0)"
       
  2477       using f `A \<in> sets M`
       
  2478       by (intro AE_iff_measurable[OF _ refl, symmetric])
       
  2479          (auto intro!: sets_Collect borel_measurable_ereal_eq)
       
  2480     also have "(AE x in M. max 0 (f x) * indicator A x = 0) \<longleftrightarrow> (AE x in M. x \<in> A \<longrightarrow> f x \<le> 0)"
       
  2481       by (auto simp add: indicator_def max_def split: split_if_asm)
       
  2482     finally have "(\<integral>\<^isup>+x. f x * indicator A x \<partial>M) = 0 \<longleftrightarrow> (AE x in M. x \<in> A \<longrightarrow> f x \<le> 0)" . }
       
  2483   with f show ?thesis
       
  2484     by (simp add: null_sets_def emeasure_density cong: conj_cong)
       
  2485 qed
       
  2486 
       
  2487 lemma AE_density:
       
  2488   assumes f: "f \<in> borel_measurable M"
       
  2489   shows "(AE x in density M f. P x) \<longleftrightarrow> (AE x in M. 0 < f x \<longrightarrow> P x)"
       
  2490 proof
       
  2491   assume "AE x in density M f. P x"
       
  2492   with f obtain N where "{x \<in> space M. \<not> P x} \<subseteq> N" "N \<in> sets M" and ae: "AE x in M. x \<in> N \<longrightarrow> f x \<le> 0"
       
  2493     by (auto simp: eventually_ae_filter null_sets_density_iff)
       
  2494   then have "AE x in M. x \<notin> N \<longrightarrow> P x" by auto
       
  2495   with ae show "AE x in M. 0 < f x \<longrightarrow> P x"
       
  2496     by (rule eventually_elim2) auto
       
  2497 next
       
  2498   fix N assume ae: "AE x in M. 0 < f x \<longrightarrow> P x"
       
  2499   then obtain N where "{x \<in> space M. \<not> (0 < f x \<longrightarrow> P x)} \<subseteq> N" "N \<in> null_sets M"
       
  2500     by (auto simp: eventually_ae_filter)
       
  2501   then have *: "{x \<in> space (density M f). \<not> P x} \<subseteq> N \<union> {x\<in>space M. \<not> 0 < f x}"
       
  2502     "N \<union> {x\<in>space M. \<not> 0 < f x} \<in> sets M" and ae2: "AE x in M. x \<notin> N"
       
  2503     using f by (auto simp: subset_eq intro!: sets_Collect_neg AE_not_in)
       
  2504   show "AE x in density M f. P x"
       
  2505     using ae2
       
  2506     unfolding eventually_ae_filter[of _ "density M f"] Bex_def null_sets_density_iff[OF f]
       
  2507     by (intro exI[of _ "N \<union> {x\<in>space M. \<not> 0 < f x}"] conjI *)
       
  2508        (auto elim: eventually_elim2)
       
  2509 qed
       
  2510 
       
  2511 lemma positive_integral_density:
       
  2512   assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x"
       
  2513   assumes g': "g' \<in> borel_measurable M"
       
  2514   shows "integral\<^isup>P (density M f) g' = (\<integral>\<^isup>+ x. f x * g' x \<partial>M)"
       
  2515 proof -
       
  2516   def g \<equiv> "\<lambda>x. max 0 (g' x)"
       
  2517   then have g: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
       
  2518     using g' by auto
       
  2519   from borel_measurable_implies_simple_function_sequence'[OF g(1)] guess G . note G = this
       
  2520   note G' = borel_measurable_simple_function[OF this(1)] simple_functionD[OF G(1)]
       
  2521   note G'(2)[simp]
       
  2522   { fix P have "AE x in M. P x \<Longrightarrow> AE x in M. P x"
       
  2523       using positive_integral_null_set[of _ _ f]
       
  2524       by (auto simp: eventually_ae_filter ) }
       
  2525   note ac = this
       
  2526   with G(4) f g have G_M': "AE x in density M f. (SUP i. G i x) = g x"
       
  2527     by (auto simp add: AE_density[OF f(1)] max_def)
       
  2528   { fix i
       
  2529     let ?I = "\<lambda>y x. indicator (G i -` {y} \<inter> space M) x"
       
  2530     { fix x assume *: "x \<in> space M" "0 \<le> f x" "0 \<le> g x"
       
  2531       then have [simp]: "G i ` space M \<inter> {y. G i x = y \<and> x \<in> space M} = {G i x}" by auto
       
  2532       from * G' G have "(\<Sum>y\<in>G i`space M. y * (f x * ?I y x)) = f x * (\<Sum>y\<in>G i`space M. (y * ?I y x))"
       
  2533         by (subst setsum_ereal_right_distrib) (auto simp: ac_simps)
       
  2534       also have "\<dots> = f x * G i x"
       
  2535         by (simp add: indicator_def if_distrib setsum_cases)
       
  2536       finally have "(\<Sum>y\<in>G i`space M. y * (f x * ?I y x)) = f x * G i x" . }
       
  2537     note to_singleton = this
       
  2538     have "integral\<^isup>P (density M f) (G i) = integral\<^isup>S (density M f) (G i)"
       
  2539       using G by (intro positive_integral_eq_simple_integral) simp_all
       
  2540     also have "\<dots> = (\<Sum>y\<in>G i`space M. y * (\<integral>\<^isup>+x. f x * ?I y x \<partial>M))"
       
  2541       using f G(1)
       
  2542       by (auto intro!: setsum_cong arg_cong2[where f="op *"] emeasure_density
       
  2543                simp: simple_function_def simple_integral_def)
       
  2544     also have "\<dots> = (\<Sum>y\<in>G i`space M. (\<integral>\<^isup>+x. y * (f x * ?I y x) \<partial>M))"
       
  2545       using f G' G by (auto intro!: setsum_cong positive_integral_cmult[symmetric])
       
  2546     also have "\<dots> = (\<integral>\<^isup>+x. (\<Sum>y\<in>G i`space M. y * (f x * ?I y x)) \<partial>M)"
       
  2547       using f G' G by (auto intro!: positive_integral_setsum[symmetric])
       
  2548     finally have "integral\<^isup>P (density M f) (G i) = (\<integral>\<^isup>+x. f x * G i x \<partial>M)"
       
  2549       using f g G' to_singleton by (auto intro!: positive_integral_cong_AE) }
       
  2550   note [simp] = this
       
  2551   have "integral\<^isup>P (density M f) g = (SUP i. integral\<^isup>P (density M f) (G i))" using G'(1) G_M'(1) G
       
  2552     using positive_integral_monotone_convergence_SUP[symmetric, OF `incseq G`, of "density M f"]
       
  2553     by (simp cong: positive_integral_cong_AE)
       
  2554   also have "\<dots> = (SUP i. (\<integral>\<^isup>+x. f x * G i x \<partial>M))" by simp
       
  2555   also have "\<dots> = (\<integral>\<^isup>+x. (SUP i. f x * G i x) \<partial>M)"
       
  2556     using f G' G(2)[THEN incseq_SucD] G
       
  2557     by (intro positive_integral_monotone_convergence_SUP_AE[symmetric])
       
  2558        (auto simp: ereal_mult_left_mono le_fun_def ereal_zero_le_0_iff)
       
  2559   also have "\<dots> = (\<integral>\<^isup>+x. f x * g x \<partial>M)" using f G' G g
       
  2560     by (intro positive_integral_cong_AE)
       
  2561        (auto simp add: SUPR_ereal_cmult split: split_max)
       
  2562   also have "\<dots> = (\<integral>\<^isup>+x. f x * g' x \<partial>M)"
       
  2563     using f(2)
       
  2564     by (subst (2) positive_integral_max_0[symmetric])
       
  2565        (auto simp: g_def max_def ereal_zero_le_0_iff intro!: positive_integral_cong_AE)
       
  2566   finally show "integral\<^isup>P (density M f) g' = (\<integral>\<^isup>+x. f x * g' x \<partial>M)"
       
  2567     unfolding g_def positive_integral_max_0 .
       
  2568 qed
       
  2569 
       
  2570 lemma integral_density:
       
  2571   assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x"
       
  2572     and g: "g \<in> borel_measurable M"
       
  2573   shows "integral\<^isup>L (density M f) g = (\<integral> x. f x * g x \<partial>M)"
       
  2574     and "integrable (density M f) g \<longleftrightarrow> integrable M (\<lambda>x. f x * g x)"
       
  2575   unfolding lebesgue_integral_def integrable_def using f g
       
  2576   by (auto simp: positive_integral_density)
       
  2577 
       
  2578 lemma emeasure_restricted:
       
  2579   assumes S: "S \<in> sets M" and X: "X \<in> sets M"
       
  2580   shows "emeasure (density M (indicator S)) X = emeasure M (S \<inter> X)"
       
  2581 proof -
       
  2582   have "emeasure (density M (indicator S)) X = (\<integral>\<^isup>+x. indicator S x * indicator X x \<partial>M)"
       
  2583     using S X by (simp add: emeasure_density)
       
  2584   also have "\<dots> = (\<integral>\<^isup>+x. indicator (S \<inter> X) x \<partial>M)"
       
  2585     by (auto intro!: positive_integral_cong simp: indicator_def)
       
  2586   also have "\<dots> = emeasure M (S \<inter> X)"
       
  2587     using S X by (simp add: Int)
       
  2588   finally show ?thesis .
       
  2589 qed
       
  2590 
       
  2591 lemma measure_restricted:
       
  2592   "S \<in> sets M \<Longrightarrow> X \<in> sets M \<Longrightarrow> measure (density M (indicator S)) X = measure M (S \<inter> X)"
       
  2593   by (simp add: emeasure_restricted measure_def)
       
  2594 
       
  2595 lemma (in finite_measure) finite_measure_restricted:
       
  2596   "S \<in> sets M \<Longrightarrow> finite_measure (density M (indicator S))"
       
  2597   by default (simp add: emeasure_restricted)
       
  2598 
       
  2599 lemma emeasure_density_const:
       
  2600   "A \<in> sets M \<Longrightarrow> 0 \<le> c \<Longrightarrow> emeasure (density M (\<lambda>_. c)) A = c * emeasure M A"
       
  2601   by (auto simp: positive_integral_cmult_indicator emeasure_density)
       
  2602 
       
  2603 lemma measure_density_const:
       
  2604   "A \<in> sets M \<Longrightarrow> 0 < c \<Longrightarrow> c \<noteq> \<infinity> \<Longrightarrow> measure (density M (\<lambda>_. c)) A = real c * measure M A"
       
  2605   by (auto simp: emeasure_density_const measure_def)
       
  2606 
       
  2607 lemma density_density_eq:
       
  2608    "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> AE x in M. 0 \<le> f x \<Longrightarrow>
       
  2609    density (density M f) g = density M (\<lambda>x. f x * g x)"
       
  2610   by (auto intro!: measure_eqI simp: emeasure_density positive_integral_density ac_simps)
       
  2611 
       
  2612 lemma distr_density_distr:
       
  2613   assumes T: "T \<in> measurable M M'" and T': "T' \<in> measurable M' M"
       
  2614     and inv: "\<forall>x\<in>space M. T' (T x) = x"
       
  2615   assumes f: "f \<in> borel_measurable M'"
       
  2616   shows "distr (density (distr M M' T) f) M T' = density M (f \<circ> T)" (is "?R = ?L")
       
  2617 proof (rule measure_eqI)
       
  2618   fix A assume A: "A \<in> sets ?R"
  2585   { fix x assume "x \<in> space M"
  2619   { fix x assume "x \<in> space M"
  2586     have "f x = (\<Sum>r\<in>f`space M. if x \<in> ?A r then r else 0)"
  2620     with sets_into_space[OF A]
  2587       using finite `x \<in> space M` by (simp add: setsum_cases)
  2621     have "indicator (T' -` A \<inter> space M') (T x) = (indicator A x :: ereal)"
  2588     also have "\<dots> = ?S x"
  2622       using T inv by (auto simp: indicator_def measurable_space) }
  2589       by (auto intro!: setsum_cong)
  2623   with A T T' f show "emeasure ?R A = emeasure ?L A"
  2590     finally have "f x = ?S x" . }
  2624     by (simp add: measurable_comp emeasure_density emeasure_distr
  2591   note f_eq = this
  2625                   positive_integral_distr measurable_sets cong: positive_integral_cong)
  2592 
  2626 qed simp
  2593   have f_eq_S: "integrable M f \<longleftrightarrow> integrable M ?S" "integral\<^isup>L M f = integral\<^isup>L M ?S"
  2627 
  2594     by (auto intro!: integrable_cong integral_cong simp only: f_eq)
  2628 lemma density_density_divide:
  2595 
  2629   fixes f g :: "'a \<Rightarrow> real"
  2596   show "integrable M f" ?integral using fin f f_eq_S
  2630   assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x"
  2597     by (simp_all add: integral_cmul_indicator borel_measurable_vimage)
  2631   assumes g: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
  2598 qed
  2632   assumes ac: "AE x in M. f x = 0 \<longrightarrow> g x = 0"
  2599 
  2633   shows "density (density M f) (\<lambda>x. g x / f x) = density M g"
  2600 lemma (in finite_measure_space) simple_function_finite[simp, intro]: "simple_function M f"
  2634 proof -
  2601   unfolding simple_function_def using finite_space by auto
  2635   have "density M g = density M (\<lambda>x. f x * (g x / f x))"
  2602 
  2636     using f g ac by (auto intro!: density_cong measurable_If)
  2603 lemma (in finite_measure_space) borel_measurable_finite[intro, simp]: "f \<in> borel_measurable M"
  2637   then show ?thesis
  2604   by (auto intro: borel_measurable_simple_function)
  2638     using f g by (subst density_density_eq) auto
  2605 
  2639 qed
  2606 lemma (in finite_measure_space) positive_integral_finite_eq_setsum:
  2640 
  2607   assumes pos: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
  2641 section {* Point measure *}
  2608   shows "integral\<^isup>P M f = (\<Sum>x \<in> space M. f x * \<mu> {x})"
  2642 
  2609 proof -
  2643 definition point_measure :: "'a set \<Rightarrow> ('a \<Rightarrow> ereal) \<Rightarrow> 'a measure" where
  2610   have *: "integral\<^isup>P M f = (\<integral>\<^isup>+ x. (\<Sum>y\<in>space M. f y * indicator {y} x) \<partial>M)"
  2644   "point_measure A f = density (count_space A) f"
  2611     by (auto intro!: positive_integral_cong simp add: indicator_def if_distrib setsum_cases[OF finite_space])
  2645 
  2612   show ?thesis unfolding * using borel_measurable_finite[of f] pos
  2646 lemma
  2613     by (simp add: positive_integral_setsum positive_integral_cmult_indicator)
  2647   shows space_point_measure: "space (point_measure A f) = A"
  2614 qed
  2648     and sets_point_measure: "sets (point_measure A f) = Pow A"
  2615 
  2649   by (auto simp: point_measure_def)
  2616 lemma (in finite_measure_space) integral_finite_singleton:
  2650 
  2617   shows "integrable M f"
  2651 lemma measurable_point_measure_eq1[simp]:
  2618   and "integral\<^isup>L M f = (\<Sum>x \<in> space M. f x * real (\<mu> {x}))" (is ?I)
  2652   "g \<in> measurable (point_measure A f) M \<longleftrightarrow> g \<in> A \<rightarrow> space M"
  2619 proof -
  2653   unfolding point_measure_def by simp
  2620   have *:
  2654 
  2621     "(\<integral>\<^isup>+ x. max 0 (ereal (f x)) \<partial>M) = (\<Sum>x \<in> space M. max 0 (ereal (f x)) * \<mu> {x})"
  2655 lemma measurable_point_measure_eq2_finite[simp]:
  2622     "(\<integral>\<^isup>+ x. max 0 (ereal (- f x)) \<partial>M) = (\<Sum>x \<in> space M. max 0 (ereal (- f x)) * \<mu> {x})"
  2656   "finite A \<Longrightarrow>
  2623     by (simp_all add: positive_integral_finite_eq_setsum)
  2657    g \<in> measurable M (point_measure A f) \<longleftrightarrow>
  2624   then show "integrable M f" using finite_space finite_measure
  2658     (g \<in> space M \<rightarrow> A \<and> (\<forall>a\<in>A. g -` {a} \<inter> space M \<in> sets M))"
  2625     by (simp add: setsum_Pinfty integrable_def positive_integral_max_0
  2659   unfolding point_measure_def by simp
  2626              split: split_max)
  2660 
  2627   show ?I using finite_measure *
  2661 lemma simple_function_point_measure[simp]:
  2628     apply (simp add: positive_integral_max_0 lebesgue_integral_def)
  2662   "simple_function (point_measure A f) g \<longleftrightarrow> finite (g ` A)"
  2629     apply (subst (1 2) setsum_real_of_ereal[symmetric])
  2663   by (simp add: point_measure_def)
  2630     apply (simp_all split: split_max add: setsum_subtractf[symmetric])
  2664 
  2631     apply (intro setsum_cong[OF refl])
  2665 lemma emeasure_point_measure:
  2632     apply (simp split: split_max)
  2666   assumes A: "finite {a\<in>X. 0 < f a}" "X \<subseteq> A"
  2633     done
  2667   shows "emeasure (point_measure A f) X = (\<Sum>a|a\<in>X \<and> 0 < f a. f a)"
  2634 qed
  2668 proof -
       
  2669   have "{a. (a \<in> X \<longrightarrow> a \<in> A \<and> 0 < f a) \<and> a \<in> X} = {a\<in>X. 0 < f a}"
       
  2670     using `X \<subseteq> A` by auto
       
  2671   with A show ?thesis
       
  2672     by (simp add: emeasure_density positive_integral_count_space ereal_zero_le_0_iff
       
  2673                   point_measure_def indicator_def)
       
  2674 qed
       
  2675 
       
  2676 lemma emeasure_point_measure_finite:
       
  2677   "finite A \<Longrightarrow> (\<And>i. i \<in> A \<Longrightarrow> 0 \<le> f i) \<Longrightarrow> X \<subseteq> A \<Longrightarrow> emeasure (point_measure A f) X = (\<Sum>a|a\<in>X. f a)"
       
  2678   by (subst emeasure_point_measure) (auto dest: finite_subset intro!: setsum_mono_zero_left simp: less_le)
       
  2679 
       
  2680 lemma null_sets_point_measure_iff:
       
  2681   "X \<in> null_sets (point_measure A f) \<longleftrightarrow> X \<subseteq> A \<and> (\<forall>x\<in>X. f x \<le> 0)"
       
  2682  by (auto simp: AE_count_space null_sets_density_iff point_measure_def)
       
  2683 
       
  2684 lemma AE_point_measure:
       
  2685   "(AE x in point_measure A f. P x) \<longleftrightarrow> (\<forall>x\<in>A. 0 < f x \<longrightarrow> P x)"
       
  2686   unfolding point_measure_def
       
  2687   by (subst AE_density) (auto simp: AE_density AE_count_space point_measure_def)
       
  2688 
       
  2689 lemma positive_integral_point_measure:
       
  2690   "finite {a\<in>A. 0 < f a \<and> 0 < g a} \<Longrightarrow>
       
  2691     integral\<^isup>P (point_measure A f) g = (\<Sum>a|a\<in>A \<and> 0 < f a \<and> 0 < g a. f a * g a)"
       
  2692   unfolding point_measure_def
       
  2693   apply (subst density_max_0)
       
  2694   apply (subst positive_integral_density)
       
  2695   apply (simp_all add: AE_count_space positive_integral_density)
       
  2696   apply (subst positive_integral_count_space )
       
  2697   apply (auto intro!: setsum_cong simp: max_def ereal_zero_less_0_iff)
       
  2698   apply (rule finite_subset)
       
  2699   prefer 2
       
  2700   apply assumption
       
  2701   apply auto
       
  2702   done
       
  2703 
       
  2704 lemma positive_integral_point_measure_finite:
       
  2705   "finite A \<Longrightarrow> (\<And>a. a \<in> A \<Longrightarrow> 0 \<le> f a) \<Longrightarrow> (\<And>a. a \<in> A \<Longrightarrow> 0 \<le> g a) \<Longrightarrow>
       
  2706     integral\<^isup>P (point_measure A f) g = (\<Sum>a\<in>A. f a * g a)"
       
  2707   by (subst positive_integral_point_measure) (auto intro!: setsum_mono_zero_left simp: less_le)
       
  2708 
       
  2709 lemma lebesgue_integral_point_measure_finite:
       
  2710   "finite A \<Longrightarrow> (\<And>a. a \<in> A \<Longrightarrow> 0 \<le> f a) \<Longrightarrow> integral\<^isup>L (point_measure A f) g = (\<Sum>a\<in>A. f a * g a)"
       
  2711   by (simp add: lebesgue_integral_count_space_finite AE_count_space integral_density point_measure_def)
       
  2712 
       
  2713 lemma integrable_point_measure_finite:
       
  2714   "finite A \<Longrightarrow> integrable (point_measure A (\<lambda>x. ereal (f x))) g"
       
  2715   unfolding point_measure_def
       
  2716   apply (subst density_ereal_max_0)
       
  2717   apply (subst integral_density)
       
  2718   apply (auto simp: AE_count_space integrable_count_space)
       
  2719   done
       
  2720 
       
  2721 section {* Uniform measure *}
       
  2722 
       
  2723 definition "uniform_measure M A = density M (\<lambda>x. indicator A x / emeasure M A)"
       
  2724 
       
  2725 lemma
       
  2726   shows sets_uniform_measure[simp]: "sets (uniform_measure M A) = sets M"
       
  2727     and space_uniform_measure[simp]: "space (uniform_measure M A) = space M"
       
  2728   by (auto simp: uniform_measure_def)
       
  2729 
       
  2730 lemma emeasure_uniform_measure[simp]:
       
  2731   assumes A: "A \<in> sets M" and B: "B \<in> sets M"
       
  2732   shows "emeasure (uniform_measure M A) B = emeasure M (A \<inter> B) / emeasure M A"
       
  2733 proof -
       
  2734   from A B have "emeasure (uniform_measure M A) B = (\<integral>\<^isup>+x. (1 / emeasure M A) * indicator (A \<inter> B) x \<partial>M)"
       
  2735     by (auto simp add: uniform_measure_def emeasure_density split: split_indicator
       
  2736              intro!: positive_integral_cong)
       
  2737   also have "\<dots> = emeasure M (A \<inter> B) / emeasure M A"
       
  2738     using A B
       
  2739     by (subst positive_integral_cmult_indicator) (simp_all add: Int emeasure_nonneg)
       
  2740   finally show ?thesis .
       
  2741 qed
       
  2742 
       
  2743 lemma emeasure_neq_0_sets: "emeasure M A \<noteq> 0 \<Longrightarrow> A \<in> sets M"
       
  2744   using emeasure_notin_sets[of A M] by blast
       
  2745 
       
  2746 lemma measure_uniform_measure[simp]:
       
  2747   assumes A: "emeasure M A \<noteq> 0" "emeasure M A \<noteq> \<infinity>" and B: "B \<in> sets M"
       
  2748   shows "measure (uniform_measure M A) B = measure M (A \<inter> B) / measure M A"
       
  2749   using emeasure_uniform_measure[OF emeasure_neq_0_sets[OF A(1)] B] A
       
  2750   by (cases "emeasure M A" "emeasure M (A \<inter> B)" rule: ereal2_cases) (simp_all add: measure_def)
       
  2751 
       
  2752 section {* Uniform count measure *}
       
  2753 
       
  2754 definition "uniform_count_measure A = point_measure A (\<lambda>x. 1 / card A)"
       
  2755  
       
  2756 lemma 
       
  2757   shows space_uniform_count_measure: "space (uniform_count_measure A) = A"
       
  2758     and sets_uniform_count_measure: "sets (uniform_count_measure A) = Pow A"
       
  2759     unfolding uniform_count_measure_def by (auto simp: space_point_measure sets_point_measure)
       
  2760  
       
  2761 lemma emeasure_uniform_count_measure:
       
  2762   "finite A \<Longrightarrow> X \<subseteq> A \<Longrightarrow> emeasure (uniform_count_measure A) X = card X / card A"
       
  2763   by (simp add: real_eq_of_nat emeasure_point_measure_finite uniform_count_measure_def)
       
  2764  
       
  2765 lemma measure_uniform_count_measure:
       
  2766   "finite A \<Longrightarrow> X \<subseteq> A \<Longrightarrow> measure (uniform_count_measure A) X = card X / card A"
       
  2767   by (simp add: real_eq_of_nat emeasure_point_measure_finite uniform_count_measure_def measure_def)
  2635 
  2768 
  2636 end
  2769 end