src/HOL/Probability/Bochner_Integration.thy
author hoelzl
Tue May 20 19:24:39 2014 +0200 (2014-05-20)
changeset 57025 e7fd64f82876
parent 56996 891e992e510f
child 57036 22568fb89165
permissions -rw-r--r--
add various lemmas
     1 (*  Title:      HOL/Probability/Bochner_Integration.thy
     2     Author:     Johannes Hölzl, TU München
     3 *)
     4 
     5 header {* Bochner Integration for Vector-Valued Functions *}
     6 
     7 theory Bochner_Integration
     8   imports Finite_Product_Measure
     9 begin
    10 
    11 text {*
    12 
    13 In the following development of the Bochner integral we use second countable topologies instead
    14 of separable spaces. A second countable topology is also separable.
    15 
    16 *}
    17 
    18 lemma borel_measurable_implies_sequence_metric:
    19   fixes f :: "'a \<Rightarrow> 'b :: {metric_space, second_countable_topology}"
    20   assumes [measurable]: "f \<in> borel_measurable M"
    21   shows "\<exists>F. (\<forall>i. simple_function M (F i)) \<and> (\<forall>x\<in>space M. (\<lambda>i. F i x) ----> f x) \<and>
    22     (\<forall>i. \<forall>x\<in>space M. dist (F i x) z \<le> 2 * dist (f x) z)"
    23 proof -
    24   obtain D :: "'b set" where "countable D" and D: "\<And>X. open X \<Longrightarrow> X \<noteq> {} \<Longrightarrow> \<exists>d\<in>D. d \<in> X"
    25     by (erule countable_dense_setE)
    26 
    27   def e \<equiv> "from_nat_into D"
    28   { fix n x
    29     obtain d where "d \<in> D" and d: "d \<in> ball x (1 / Suc n)"
    30       using D[of "ball x (1 / Suc n)"] by auto
    31     from `d \<in> D` D[of UNIV] `countable D` obtain i where "d = e i"
    32       unfolding e_def by (auto dest: from_nat_into_surj)
    33     with d have "\<exists>i. dist x (e i) < 1 / Suc n"
    34       by auto }
    35   note e = this
    36 
    37   def A \<equiv> "\<lambda>m n. {x\<in>space M. dist (f x) (e n) < 1 / (Suc m) \<and> 1 / (Suc m) \<le> dist (f x) z}"
    38   def B \<equiv> "\<lambda>m. disjointed (A m)"
    39   
    40   def m \<equiv> "\<lambda>N x. Max {m::nat. m \<le> N \<and> x \<in> (\<Union>n\<le>N. B m n)}"
    41   def F \<equiv> "\<lambda>N::nat. \<lambda>x. if (\<exists>m\<le>N. x \<in> (\<Union>n\<le>N. B m n)) \<and> (\<exists>n\<le>N. x \<in> B (m N x) n) 
    42     then e (LEAST n. x \<in> B (m N x) n) else z"
    43 
    44   have B_imp_A[intro, simp]: "\<And>x m n. x \<in> B m n \<Longrightarrow> x \<in> A m n"
    45     using disjointed_subset[of "A m" for m] unfolding B_def by auto
    46 
    47   { fix m
    48     have "\<And>n. A m n \<in> sets M"
    49       by (auto simp: A_def)
    50     then have "\<And>n. B m n \<in> sets M"
    51       using sets.range_disjointed_sets[of "A m" M] by (auto simp: B_def) }
    52   note this[measurable]
    53 
    54   { fix N i x assume "\<exists>m\<le>N. x \<in> (\<Union>n\<le>N. B m n)"
    55     then have "m N x \<in> {m::nat. m \<le> N \<and> x \<in> (\<Union>n\<le>N. B m n)}"
    56       unfolding m_def by (intro Max_in) auto
    57     then have "m N x \<le> N" "\<exists>n\<le>N. x \<in> B (m N x) n"
    58       by auto }
    59   note m = this
    60 
    61   { fix j N i x assume "j \<le> N" "i \<le> N" "x \<in> B j i"
    62     then have "j \<le> m N x"
    63       unfolding m_def by (intro Max_ge) auto }
    64   note m_upper = this
    65 
    66   show ?thesis
    67     unfolding simple_function_def
    68   proof (safe intro!: exI[of _ F])
    69     have [measurable]: "\<And>i. F i \<in> borel_measurable M"
    70       unfolding F_def m_def by measurable
    71     show "\<And>x i. F i -` {x} \<inter> space M \<in> sets M"
    72       by measurable
    73 
    74     { fix i
    75       { fix n x assume "x \<in> B (m i x) n"
    76         then have "(LEAST n. x \<in> B (m i x) n) \<le> n"
    77           by (intro Least_le)
    78         also assume "n \<le> i" 
    79         finally have "(LEAST n. x \<in> B (m i x) n) \<le> i" . }
    80       then have "F i ` space M \<subseteq> {z} \<union> e ` {.. i}"
    81         by (auto simp: F_def)
    82       then show "finite (F i ` space M)"
    83         by (rule finite_subset) auto }
    84     
    85     { fix N i n x assume "i \<le> N" "n \<le> N" "x \<in> B i n"
    86       then have 1: "\<exists>m\<le>N. x \<in> (\<Union> n\<le>N. B m n)" by auto
    87       from m[OF this] obtain n where n: "m N x \<le> N" "n \<le> N" "x \<in> B (m N x) n" by auto
    88       moreover
    89       def L \<equiv> "LEAST n. x \<in> B (m N x) n"
    90       have "dist (f x) (e L) < 1 / Suc (m N x)"
    91       proof -
    92         have "x \<in> B (m N x) L"
    93           using n(3) unfolding L_def by (rule LeastI)
    94         then have "x \<in> A (m N x) L"
    95           by auto
    96         then show ?thesis
    97           unfolding A_def by simp
    98       qed
    99       ultimately have "dist (f x) (F N x) < 1 / Suc (m N x)"
   100         by (auto simp add: F_def L_def) }
   101     note * = this
   102 
   103     fix x assume "x \<in> space M"
   104     show "(\<lambda>i. F i x) ----> f x"
   105     proof cases
   106       assume "f x = z"
   107       then have "\<And>i n. x \<notin> A i n"
   108         unfolding A_def by auto
   109       then have "\<And>i. F i x = z"
   110         by (auto simp: F_def)
   111       then show ?thesis
   112         using `f x = z` by auto
   113     next
   114       assume "f x \<noteq> z"
   115 
   116       show ?thesis
   117       proof (rule tendstoI)
   118         fix e :: real assume "0 < e"
   119         with `f x \<noteq> z` obtain n where "1 / Suc n < e" "1 / Suc n < dist (f x) z"
   120           by (metis dist_nz order_less_trans neq_iff nat_approx_posE)
   121         with `x\<in>space M` `f x \<noteq> z` have "x \<in> (\<Union>i. B n i)"
   122           unfolding A_def B_def UN_disjointed_eq using e by auto
   123         then obtain i where i: "x \<in> B n i" by auto
   124 
   125         show "eventually (\<lambda>i. dist (F i x) (f x) < e) sequentially"
   126           using eventually_ge_at_top[of "max n i"]
   127         proof eventually_elim
   128           fix j assume j: "max n i \<le> j"
   129           with i have "dist (f x) (F j x) < 1 / Suc (m j x)"
   130             by (intro *[OF _ _ i]) auto
   131           also have "\<dots> \<le> 1 / Suc n"
   132             using j m_upper[OF _ _ i]
   133             by (auto simp: field_simps)
   134           also note `1 / Suc n < e`
   135           finally show "dist (F j x) (f x) < e"
   136             by (simp add: less_imp_le dist_commute)
   137         qed
   138       qed
   139     qed
   140     fix i 
   141     { fix n m assume "x \<in> A n m"
   142       then have "dist (e m) (f x) + dist (f x) z \<le> 2 * dist (f x) z"
   143         unfolding A_def by (auto simp: dist_commute)
   144       also have "dist (e m) z \<le> dist (e m) (f x) + dist (f x) z"
   145         by (rule dist_triangle)
   146       finally (xtrans) have "dist (e m) z \<le> 2 * dist (f x) z" . }
   147     then show "dist (F i x) z \<le> 2 * dist (f x) z"
   148       unfolding F_def
   149       apply auto
   150       apply (rule LeastI2)
   151       apply auto
   152       done
   153   qed
   154 qed
   155 
   156 lemma real_indicator: "real (indicator A x :: ereal) = indicator A x"
   157   unfolding indicator_def by auto
   158 
   159 lemma split_indicator_asm:
   160   "P (indicator S x) \<longleftrightarrow> \<not> ((x \<in> S \<and> \<not> P 1) \<or> (x \<notin> S \<and> \<not> P 0))"
   161   unfolding indicator_def by auto
   162 
   163 lemma
   164   fixes f :: "'a \<Rightarrow> 'b::semiring_1" assumes "finite A"
   165   shows setsum_mult_indicator[simp]: "(\<Sum>x \<in> A. f x * indicator (B x) (g x)) = (\<Sum>x\<in>{x\<in>A. g x \<in> B x}. f x)"
   166   and setsum_indicator_mult[simp]: "(\<Sum>x \<in> A. indicator (B x) (g x) * f x) = (\<Sum>x\<in>{x\<in>A. g x \<in> B x}. f x)"
   167   unfolding indicator_def
   168   using assms by (auto intro!: setsum_mono_zero_cong_right split: split_if_asm)
   169 
   170 lemma borel_measurable_induct_real[consumes 2, case_names set mult add seq]:
   171   fixes P :: "('a \<Rightarrow> real) \<Rightarrow> bool"
   172   assumes u: "u \<in> borel_measurable M" "\<And>x. 0 \<le> u x"
   173   assumes set: "\<And>A. A \<in> sets M \<Longrightarrow> P (indicator A)"
   174   assumes mult: "\<And>u c. 0 \<le> c \<Longrightarrow> u \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> P u \<Longrightarrow> P (\<lambda>x. c * u x)"
   175   assumes add: "\<And>u v. u \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> P u \<Longrightarrow> v \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 \<le> v x) \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> u x = 0 \<or> v x = 0) \<Longrightarrow> P v \<Longrightarrow> P (\<lambda>x. v x + u x)"
   176   assumes seq: "\<And>U. (\<And>i. U i \<in> borel_measurable M) \<Longrightarrow> (\<And>i x. 0 \<le> U i x) \<Longrightarrow> (\<And>i. P (U i)) \<Longrightarrow> incseq U \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. U i x) ----> u x) \<Longrightarrow> P u"
   177   shows "P u"
   178 proof -
   179   have "(\<lambda>x. ereal (u x)) \<in> borel_measurable M" using u by auto
   180   from borel_measurable_implies_simple_function_sequence'[OF this]
   181   obtain U where U: "\<And>i. simple_function M (U i)" "incseq U" "\<And>i. \<infinity> \<notin> range (U i)" and
   182     sup: "\<And>x. (SUP i. U i x) = max 0 (ereal (u x))" and nn: "\<And>i x. 0 \<le> U i x"
   183     by blast
   184 
   185   def U' \<equiv> "\<lambda>i x. indicator (space M) x * real (U i x)"
   186   then have U'_sf[measurable]: "\<And>i. simple_function M (U' i)"
   187     using U by (auto intro!: simple_function_compose1[where g=real])
   188 
   189   show "P u"
   190   proof (rule seq)
   191     fix i show "U' i \<in> borel_measurable M" "\<And>x. 0 \<le> U' i x"
   192       using U nn by (auto
   193           intro: borel_measurable_simple_function 
   194           intro!: borel_measurable_real_of_ereal real_of_ereal_pos borel_measurable_times
   195           simp: U'_def zero_le_mult_iff)
   196     show "incseq U'"
   197       using U(2,3) nn
   198       by (auto simp: incseq_def le_fun_def image_iff eq_commute U'_def indicator_def
   199                intro!: real_of_ereal_positive_mono)
   200   next
   201     fix x assume x: "x \<in> space M"
   202     have "(\<lambda>i. U i x) ----> (SUP i. U i x)"
   203       using U(2) by (intro LIMSEQ_SUP) (auto simp: incseq_def le_fun_def)
   204     moreover have "(\<lambda>i. U i x) = (\<lambda>i. ereal (U' i x))"
   205       using x nn U(3) by (auto simp: fun_eq_iff U'_def ereal_real image_iff eq_commute)
   206     moreover have "(SUP i. U i x) = ereal (u x)"
   207       using sup u(2) by (simp add: max_def)
   208     ultimately show "(\<lambda>i. U' i x) ----> u x" 
   209       by simp
   210   next
   211     fix i
   212     have "U' i ` space M \<subseteq> real ` (U i ` space M)" "finite (U i ` space M)"
   213       unfolding U'_def using U(1) by (auto dest: simple_functionD)
   214     then have fin: "finite (U' i ` space M)"
   215       by (metis finite_subset finite_imageI)
   216     moreover have "\<And>z. {y. U' i z = y \<and> y \<in> U' i ` space M \<and> z \<in> space M} = (if z \<in> space M then {U' i z} else {})"
   217       by auto
   218     ultimately have U': "(\<lambda>z. \<Sum>y\<in>U' i`space M. y * indicator {x\<in>space M. U' i x = y} z) = U' i"
   219       by (simp add: U'_def fun_eq_iff)
   220     have "\<And>x. x \<in> U' i ` space M \<Longrightarrow> 0 \<le> x"
   221       using nn by (auto simp: U'_def real_of_ereal_pos)
   222     with fin have "P (\<lambda>z. \<Sum>y\<in>U' i`space M. y * indicator {x\<in>space M. U' i x = y} z)"
   223     proof induct
   224       case empty from set[of "{}"] show ?case
   225         by (simp add: indicator_def[abs_def])
   226     next
   227       case (insert x F)
   228       then show ?case
   229         by (auto intro!: add mult set setsum_nonneg split: split_indicator split_indicator_asm
   230                  simp del: setsum_mult_indicator simp: setsum_nonneg_eq_0_iff )
   231     qed
   232     with U' show "P (U' i)" by simp
   233   qed
   234 qed
   235 
   236 lemma scaleR_cong_right:
   237   fixes x :: "'a :: real_vector"
   238   shows "(x \<noteq> 0 \<Longrightarrow> r = p) \<Longrightarrow> r *\<^sub>R x = p *\<^sub>R x"
   239   by (cases "x = 0") auto
   240 
   241 inductive simple_bochner_integrable :: "'a measure \<Rightarrow> ('a \<Rightarrow> 'b::real_vector) \<Rightarrow> bool" for M f where
   242   "simple_function M f \<Longrightarrow> emeasure M {y\<in>space M. f y \<noteq> 0} \<noteq> \<infinity> \<Longrightarrow>
   243     simple_bochner_integrable M f"
   244 
   245 lemma simple_bochner_integrable_compose2:
   246   assumes p_0: "p 0 0 = 0"
   247   shows "simple_bochner_integrable M f \<Longrightarrow> simple_bochner_integrable M g \<Longrightarrow>
   248     simple_bochner_integrable M (\<lambda>x. p (f x) (g x))"
   249 proof (safe intro!: simple_bochner_integrable.intros elim!: simple_bochner_integrable.cases del: notI)
   250   assume sf: "simple_function M f" "simple_function M g"
   251   then show "simple_function M (\<lambda>x. p (f x) (g x))"
   252     by (rule simple_function_compose2)
   253 
   254   from sf have [measurable]:
   255       "f \<in> measurable M (count_space UNIV)"
   256       "g \<in> measurable M (count_space UNIV)"
   257     by (auto intro: measurable_simple_function)
   258 
   259   assume fin: "emeasure M {y \<in> space M. f y \<noteq> 0} \<noteq> \<infinity>" "emeasure M {y \<in> space M. g y \<noteq> 0} \<noteq> \<infinity>"
   260    
   261   have "emeasure M {x\<in>space M. p (f x) (g x) \<noteq> 0} \<le>
   262       emeasure M ({x\<in>space M. f x \<noteq> 0} \<union> {x\<in>space M. g x \<noteq> 0})"
   263     by (intro emeasure_mono) (auto simp: p_0)
   264   also have "\<dots> \<le> emeasure M {x\<in>space M. f x \<noteq> 0} + emeasure M {x\<in>space M. g x \<noteq> 0}"
   265     by (intro emeasure_subadditive) auto
   266   finally show "emeasure M {y \<in> space M. p (f y) (g y) \<noteq> 0} \<noteq> \<infinity>"
   267     using fin by auto
   268 qed
   269 
   270 lemma simple_function_finite_support:
   271   assumes f: "simple_function M f" and fin: "(\<integral>\<^sup>+x. f x \<partial>M) < \<infinity>" and nn: "\<And>x. 0 \<le> f x"
   272   shows "emeasure M {x\<in>space M. f x \<noteq> 0} \<noteq> \<infinity>"
   273 proof cases
   274   from f have meas[measurable]: "f \<in> borel_measurable M"
   275     by (rule borel_measurable_simple_function)
   276 
   277   assume non_empty: "\<exists>x\<in>space M. f x \<noteq> 0"
   278 
   279   def m \<equiv> "Min (f`space M - {0})"
   280   have "m \<in> f`space M - {0}"
   281     unfolding m_def using f non_empty by (intro Min_in) (auto simp: simple_function_def)
   282   then have m: "0 < m"
   283     using nn by (auto simp: less_le)
   284 
   285   from m have "m * emeasure M {x\<in>space M. 0 \<noteq> f x} = 
   286     (\<integral>\<^sup>+x. m * indicator {x\<in>space M. 0 \<noteq> f x} x \<partial>M)"
   287     using f by (intro nn_integral_cmult_indicator[symmetric]) auto
   288   also have "\<dots> \<le> (\<integral>\<^sup>+x. f x \<partial>M)"
   289     using AE_space
   290   proof (intro nn_integral_mono_AE, eventually_elim)
   291     fix x assume "x \<in> space M"
   292     with nn show "m * indicator {x \<in> space M. 0 \<noteq> f x} x \<le> f x"
   293       using f by (auto split: split_indicator simp: simple_function_def m_def)
   294   qed
   295   also note `\<dots> < \<infinity>`
   296   finally show ?thesis
   297     using m by auto 
   298 next
   299   assume "\<not> (\<exists>x\<in>space M. f x \<noteq> 0)"
   300   with nn have *: "{x\<in>space M. f x \<noteq> 0} = {}"
   301     by auto
   302   show ?thesis unfolding * by simp
   303 qed
   304 
   305 lemma simple_bochner_integrableI_bounded:
   306   assumes f: "simple_function M f" and fin: "(\<integral>\<^sup>+x. norm (f x) \<partial>M) < \<infinity>"
   307   shows "simple_bochner_integrable M f"
   308 proof
   309   have "emeasure M {y \<in> space M. ereal (norm (f y)) \<noteq> 0} \<noteq> \<infinity>"
   310   proof (rule simple_function_finite_support)
   311     show "simple_function M (\<lambda>x. ereal (norm (f x)))"
   312       using f by (rule simple_function_compose1)
   313     show "(\<integral>\<^sup>+ y. ereal (norm (f y)) \<partial>M) < \<infinity>" by fact
   314   qed simp
   315   then show "emeasure M {y \<in> space M. f y \<noteq> 0} \<noteq> \<infinity>" by simp
   316 qed fact
   317 
   318 definition simple_bochner_integral :: "'a measure \<Rightarrow> ('a \<Rightarrow> 'b::real_vector) \<Rightarrow> 'b" where
   319   "simple_bochner_integral M f = (\<Sum>y\<in>f`space M. measure M {x\<in>space M. f x = y} *\<^sub>R y)"
   320 
   321 lemma simple_bochner_integral_partition:
   322   assumes f: "simple_bochner_integrable M f" and g: "simple_function M g"
   323   assumes sub: "\<And>x y. x \<in> space M \<Longrightarrow> y \<in> space M \<Longrightarrow> g x = g y \<Longrightarrow> f x = f y"
   324   assumes v: "\<And>x. x \<in> space M \<Longrightarrow> f x = v (g x)"
   325   shows "simple_bochner_integral M f = (\<Sum>y\<in>g ` space M. measure M {x\<in>space M. g x = y} *\<^sub>R v y)"
   326     (is "_ = ?r")
   327 proof -
   328   from f g have [simp]: "finite (f`space M)" "finite (g`space M)"
   329     by (auto simp: simple_function_def elim: simple_bochner_integrable.cases)
   330 
   331   from f have [measurable]: "f \<in> measurable M (count_space UNIV)"
   332     by (auto intro: measurable_simple_function elim: simple_bochner_integrable.cases)
   333 
   334   from g have [measurable]: "g \<in> measurable M (count_space UNIV)"
   335     by (auto intro: measurable_simple_function elim: simple_bochner_integrable.cases)
   336 
   337   { fix y assume "y \<in> space M"
   338     then have "f ` space M \<inter> {i. \<exists>x\<in>space M. i = f x \<and> g y = g x} = {v (g y)}"
   339       by (auto cong: sub simp: v[symmetric]) }
   340   note eq = this
   341 
   342   have "simple_bochner_integral M f =
   343     (\<Sum>y\<in>f`space M. (\<Sum>z\<in>g`space M. 
   344       if \<exists>x\<in>space M. y = f x \<and> z = g x then measure M {x\<in>space M. g x = z} else 0) *\<^sub>R y)"
   345     unfolding simple_bochner_integral_def
   346   proof (safe intro!: setsum_cong scaleR_cong_right)
   347     fix y assume y: "y \<in> space M" "f y \<noteq> 0"
   348     have [simp]: "g ` space M \<inter> {z. \<exists>x\<in>space M. f y = f x \<and> z = g x} = 
   349         {z. \<exists>x\<in>space M. f y = f x \<and> z = g x}"
   350       by auto
   351     have eq:"{x \<in> space M. f x = f y} =
   352         (\<Union>i\<in>{z. \<exists>x\<in>space M. f y = f x \<and> z = g x}. {x \<in> space M. g x = i})"
   353       by (auto simp: eq_commute cong: sub rev_conj_cong)
   354     have "finite (g`space M)" by simp
   355     then have "finite {z. \<exists>x\<in>space M. f y = f x \<and> z = g x}"
   356       by (rule rev_finite_subset) auto
   357     moreover
   358     { fix x assume "x \<in> space M" "f x = f y"
   359       then have "x \<in> space M" "f x \<noteq> 0"
   360         using y by auto
   361       then have "emeasure M {y \<in> space M. g y = g x} \<le> emeasure M {y \<in> space M. f y \<noteq> 0}"
   362         by (auto intro!: emeasure_mono cong: sub)
   363       then have "emeasure M {xa \<in> space M. g xa = g x} < \<infinity>"
   364         using f by (auto simp: simple_bochner_integrable.simps) }
   365     ultimately
   366     show "measure M {x \<in> space M. f x = f y} =
   367       (\<Sum>z\<in>g ` space M. if \<exists>x\<in>space M. f y = f x \<and> z = g x then measure M {x \<in> space M. g x = z} else 0)"
   368       apply (simp add: setsum_cases eq)
   369       apply (subst measure_finite_Union[symmetric])
   370       apply (auto simp: disjoint_family_on_def)
   371       done
   372   qed
   373   also have "\<dots> = (\<Sum>y\<in>f`space M. (\<Sum>z\<in>g`space M. 
   374       if \<exists>x\<in>space M. y = f x \<and> z = g x then measure M {x\<in>space M. g x = z} *\<^sub>R y else 0))"
   375     by (auto intro!: setsum_cong simp: scaleR_setsum_left)
   376   also have "\<dots> = ?r"
   377     by (subst setsum_commute)
   378        (auto intro!: setsum_cong simp: setsum_cases scaleR_setsum_right[symmetric] eq)
   379   finally show "simple_bochner_integral M f = ?r" .
   380 qed
   381 
   382 lemma simple_bochner_integral_add:
   383   assumes f: "simple_bochner_integrable M f" and g: "simple_bochner_integrable M g"
   384   shows "simple_bochner_integral M (\<lambda>x. f x + g x) =
   385     simple_bochner_integral M f + simple_bochner_integral M g"
   386 proof -
   387   from f g have "simple_bochner_integral M (\<lambda>x. f x + g x) =
   388     (\<Sum>y\<in>(\<lambda>x. (f x, g x)) ` space M. measure M {x \<in> space M. (f x, g x) = y} *\<^sub>R (fst y + snd y))"
   389     by (intro simple_bochner_integral_partition)
   390        (auto simp: simple_bochner_integrable_compose2 elim: simple_bochner_integrable.cases)
   391   moreover from f g have "simple_bochner_integral M f =
   392     (\<Sum>y\<in>(\<lambda>x. (f x, g x)) ` space M. measure M {x \<in> space M. (f x, g x) = y} *\<^sub>R fst y)"
   393     by (intro simple_bochner_integral_partition)
   394        (auto simp: simple_bochner_integrable_compose2 elim: simple_bochner_integrable.cases)
   395   moreover from f g have "simple_bochner_integral M g =
   396     (\<Sum>y\<in>(\<lambda>x. (f x, g x)) ` space M. measure M {x \<in> space M. (f x, g x) = y} *\<^sub>R snd y)"
   397     by (intro simple_bochner_integral_partition)
   398        (auto simp: simple_bochner_integrable_compose2 elim: simple_bochner_integrable.cases)
   399   ultimately show ?thesis
   400     by (simp add: setsum_addf[symmetric] scaleR_add_right)
   401 qed
   402 
   403 lemma (in linear) simple_bochner_integral_linear:
   404   assumes g: "simple_bochner_integrable M g"
   405   shows "simple_bochner_integral M (\<lambda>x. f (g x)) = f (simple_bochner_integral M g)"
   406 proof -
   407   from g have "simple_bochner_integral M (\<lambda>x. f (g x)) =
   408     (\<Sum>y\<in>g ` space M. measure M {x \<in> space M. g x = y} *\<^sub>R f y)"
   409     by (intro simple_bochner_integral_partition)
   410        (auto simp: simple_bochner_integrable_compose2[where p="\<lambda>x y. f x"] zero
   411              elim: simple_bochner_integrable.cases)
   412   also have "\<dots> = f (simple_bochner_integral M g)"
   413     by (simp add: simple_bochner_integral_def setsum scaleR)
   414   finally show ?thesis .
   415 qed
   416 
   417 lemma simple_bochner_integral_minus:
   418   assumes f: "simple_bochner_integrable M f"
   419   shows "simple_bochner_integral M (\<lambda>x. - f x) = - simple_bochner_integral M f"
   420 proof -
   421   interpret linear uminus by unfold_locales auto
   422   from f show ?thesis
   423     by (rule simple_bochner_integral_linear)
   424 qed
   425 
   426 lemma simple_bochner_integral_diff:
   427   assumes f: "simple_bochner_integrable M f" and g: "simple_bochner_integrable M g"
   428   shows "simple_bochner_integral M (\<lambda>x. f x - g x) =
   429     simple_bochner_integral M f - simple_bochner_integral M g"
   430   unfolding diff_conv_add_uminus using f g
   431   by (subst simple_bochner_integral_add)
   432      (auto simp: simple_bochner_integral_minus simple_bochner_integrable_compose2[where p="\<lambda>x y. - y"])
   433 
   434 lemma simple_bochner_integral_norm_bound:
   435   assumes f: "simple_bochner_integrable M f"
   436   shows "norm (simple_bochner_integral M f) \<le> simple_bochner_integral M (\<lambda>x. norm (f x))"
   437 proof -
   438   have "norm (simple_bochner_integral M f) \<le> 
   439     (\<Sum>y\<in>f ` space M. norm (measure M {x \<in> space M. f x = y} *\<^sub>R y))"
   440     unfolding simple_bochner_integral_def by (rule norm_setsum)
   441   also have "\<dots> = (\<Sum>y\<in>f ` space M. measure M {x \<in> space M. f x = y} *\<^sub>R norm y)"
   442     by (simp add: measure_nonneg)
   443   also have "\<dots> = simple_bochner_integral M (\<lambda>x. norm (f x))"
   444     using f
   445     by (intro simple_bochner_integral_partition[symmetric])
   446        (auto intro: f simple_bochner_integrable_compose2 elim: simple_bochner_integrable.cases)
   447   finally show ?thesis .
   448 qed
   449 
   450 lemma simple_bochner_integral_eq_nn_integral:
   451   assumes f: "simple_bochner_integrable M f" "\<And>x. 0 \<le> f x"
   452   shows "simple_bochner_integral M f = (\<integral>\<^sup>+x. f x \<partial>M)"
   453 proof -
   454   { fix x y z have "(x \<noteq> 0 \<Longrightarrow> y = z) \<Longrightarrow> ereal x * y = ereal x * z"
   455       by (cases "x = 0") (auto simp: zero_ereal_def[symmetric]) }
   456   note ereal_cong_mult = this
   457 
   458   have [measurable]: "f \<in> borel_measurable M"
   459     using f(1) by (auto intro: borel_measurable_simple_function elim: simple_bochner_integrable.cases)
   460 
   461   { fix y assume y: "y \<in> space M" "f y \<noteq> 0"
   462     have "ereal (measure M {x \<in> space M. f x = f y}) = emeasure M {x \<in> space M. f x = f y}"
   463     proof (rule emeasure_eq_ereal_measure[symmetric])
   464       have "emeasure M {x \<in> space M. f x = f y} \<le> emeasure M {x \<in> space M. f x \<noteq> 0}"
   465         using y by (intro emeasure_mono) auto
   466       with f show "emeasure M {x \<in> space M. f x = f y} \<noteq> \<infinity>"
   467         by (auto simp: simple_bochner_integrable.simps)
   468     qed
   469     moreover have "{x \<in> space M. f x = f y} = (\<lambda>x. ereal (f x)) -` {ereal (f y)} \<inter> space M"
   470       by auto
   471     ultimately have "ereal (measure M {x \<in> space M. f x = f y}) =
   472           emeasure M ((\<lambda>x. ereal (f x)) -` {ereal (f y)} \<inter> space M)" by simp }
   473   with f have "simple_bochner_integral M f = (\<integral>\<^sup>Sx. f x \<partial>M)"
   474     unfolding simple_integral_def
   475     by (subst simple_bochner_integral_partition[OF f(1), where g="\<lambda>x. ereal (f x)" and v=real])
   476        (auto intro: f simple_function_compose1 elim: simple_bochner_integrable.cases
   477              intro!: setsum_cong ereal_cong_mult
   478              simp: setsum_ereal[symmetric] times_ereal.simps(1)[symmetric] mult_ac
   479              simp del: setsum_ereal times_ereal.simps(1))
   480   also have "\<dots> = (\<integral>\<^sup>+x. f x \<partial>M)"
   481     using f
   482     by (intro nn_integral_eq_simple_integral[symmetric])
   483        (auto simp: simple_function_compose1 simple_bochner_integrable.simps)
   484   finally show ?thesis .
   485 qed
   486 
   487 lemma simple_bochner_integral_bounded:
   488   fixes f :: "'a \<Rightarrow> 'b::{real_normed_vector, second_countable_topology}"
   489   assumes f[measurable]: "f \<in> borel_measurable M"
   490   assumes s: "simple_bochner_integrable M s" and t: "simple_bochner_integrable M t"
   491   shows "ereal (norm (simple_bochner_integral M s - simple_bochner_integral M t)) \<le>
   492     (\<integral>\<^sup>+ x. norm (f x - s x) \<partial>M) + (\<integral>\<^sup>+ x. norm (f x - t x) \<partial>M)"
   493     (is "ereal (norm (?s - ?t)) \<le> ?S + ?T")
   494 proof -
   495   have [measurable]: "s \<in> borel_measurable M" "t \<in> borel_measurable M"
   496     using s t by (auto intro: borel_measurable_simple_function elim: simple_bochner_integrable.cases)
   497 
   498   have "ereal (norm (?s - ?t)) = norm (simple_bochner_integral M (\<lambda>x. s x - t x))"
   499     using s t by (subst simple_bochner_integral_diff) auto
   500   also have "\<dots> \<le> simple_bochner_integral M (\<lambda>x. norm (s x - t x))"
   501     using simple_bochner_integrable_compose2[of "op -" M "s" "t"] s t
   502     by (auto intro!: simple_bochner_integral_norm_bound)
   503   also have "\<dots> = (\<integral>\<^sup>+x. norm (s x - t x) \<partial>M)"
   504     using simple_bochner_integrable_compose2[of "\<lambda>x y. norm (x - y)" M "s" "t"] s t
   505     by (auto intro!: simple_bochner_integral_eq_nn_integral)
   506   also have "\<dots> \<le> (\<integral>\<^sup>+x. ereal (norm (f x - s x)) + ereal (norm (f x - t x)) \<partial>M)"
   507     by (auto intro!: nn_integral_mono)
   508        (metis (erased, hide_lams) add_diff_cancel_left add_diff_eq diff_add_eq order_trans
   509               norm_minus_commute norm_triangle_ineq4 order_refl)
   510   also have "\<dots> = ?S + ?T"
   511    by (rule nn_integral_add) auto
   512   finally show ?thesis .
   513 qed
   514 
   515 inductive has_bochner_integral :: "'a measure \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'b::{real_normed_vector, second_countable_topology} \<Rightarrow> bool"
   516   for M f x where
   517   "f \<in> borel_measurable M \<Longrightarrow>
   518     (\<And>i. simple_bochner_integrable M (s i)) \<Longrightarrow>
   519     (\<lambda>i. \<integral>\<^sup>+x. norm (f x - s i x) \<partial>M) ----> 0 \<Longrightarrow>
   520     (\<lambda>i. simple_bochner_integral M (s i)) ----> x \<Longrightarrow>
   521     has_bochner_integral M f x"
   522 
   523 lemma has_bochner_integral_cong:
   524   assumes "M = N" "\<And>x. x \<in> space N \<Longrightarrow> f x = g x" "x = y"
   525   shows "has_bochner_integral M f x \<longleftrightarrow> has_bochner_integral N g y"
   526   unfolding has_bochner_integral.simps assms(1,3)
   527   using assms(2) by (simp cong: measurable_cong_strong nn_integral_cong_strong)
   528 
   529 lemma has_bochner_integral_cong_AE:
   530   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (AE x in M. f x = g x) \<Longrightarrow>
   531     has_bochner_integral M f x \<longleftrightarrow> has_bochner_integral M g x"
   532   unfolding has_bochner_integral.simps
   533   by (intro arg_cong[where f=Ex] ext conj_cong rev_conj_cong refl arg_cong[where f="\<lambda>x. x ----> 0"]
   534             nn_integral_cong_AE)
   535      auto
   536 
   537 lemma borel_measurable_has_bochner_integral[measurable_dest]:
   538   "has_bochner_integral M f x \<Longrightarrow> f \<in> borel_measurable M"
   539   by (auto elim: has_bochner_integral.cases)
   540 
   541 lemma has_bochner_integral_simple_bochner_integrable:
   542   "simple_bochner_integrable M f \<Longrightarrow> has_bochner_integral M f (simple_bochner_integral M f)"
   543   by (rule has_bochner_integral.intros[where s="\<lambda>_. f"])
   544      (auto intro: borel_measurable_simple_function 
   545            elim: simple_bochner_integrable.cases
   546            simp: zero_ereal_def[symmetric])
   547 
   548 lemma has_bochner_integral_real_indicator:
   549   assumes [measurable]: "A \<in> sets M" and A: "emeasure M A < \<infinity>"
   550   shows "has_bochner_integral M (indicator A) (measure M A)"
   551 proof -
   552   have sbi: "simple_bochner_integrable M (indicator A::'a \<Rightarrow> real)"
   553   proof
   554     have "{y \<in> space M. (indicator A y::real) \<noteq> 0} = A"
   555       using sets.sets_into_space[OF `A\<in>sets M`] by (auto split: split_indicator)
   556     then show "emeasure M {y \<in> space M. (indicator A y::real) \<noteq> 0} \<noteq> \<infinity>"
   557       using A by auto
   558   qed (rule simple_function_indicator assms)+
   559   moreover have "simple_bochner_integral M (indicator A) = measure M A"
   560     using simple_bochner_integral_eq_nn_integral[OF sbi] A
   561     by (simp add: ereal_indicator emeasure_eq_ereal_measure)
   562   ultimately show ?thesis
   563     by (metis has_bochner_integral_simple_bochner_integrable)
   564 qed
   565 
   566 lemma has_bochner_integral_add:
   567   "has_bochner_integral M f x \<Longrightarrow> has_bochner_integral M g y \<Longrightarrow>
   568     has_bochner_integral M (\<lambda>x. f x + g x) (x + y)"
   569 proof (safe intro!: has_bochner_integral.intros elim!: has_bochner_integral.cases)
   570   fix sf sg
   571   assume f_sf: "(\<lambda>i. \<integral>\<^sup>+ x. norm (f x - sf i x) \<partial>M) ----> 0"
   572   assume g_sg: "(\<lambda>i. \<integral>\<^sup>+ x. norm (g x - sg i x) \<partial>M) ----> 0"
   573 
   574   assume sf: "\<forall>i. simple_bochner_integrable M (sf i)"
   575     and sg: "\<forall>i. simple_bochner_integrable M (sg i)"
   576   then have [measurable]: "\<And>i. sf i \<in> borel_measurable M" "\<And>i. sg i \<in> borel_measurable M"
   577     by (auto intro: borel_measurable_simple_function elim: simple_bochner_integrable.cases)
   578   assume [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   579 
   580   show "\<And>i. simple_bochner_integrable M (\<lambda>x. sf i x + sg i x)"
   581     using sf sg by (simp add: simple_bochner_integrable_compose2)
   582 
   583   show "(\<lambda>i. \<integral>\<^sup>+ x. (norm (f x + g x - (sf i x + sg i x))) \<partial>M) ----> 0"
   584     (is "?f ----> 0")
   585   proof (rule tendsto_sandwich)
   586     show "eventually (\<lambda>n. 0 \<le> ?f n) sequentially" "(\<lambda>_. 0) ----> 0"
   587       by (auto simp: nn_integral_nonneg)
   588     show "eventually (\<lambda>i. ?f i \<le> (\<integral>\<^sup>+ x. (norm (f x - sf i x)) \<partial>M) + \<integral>\<^sup>+ x. (norm (g x - sg i x)) \<partial>M) sequentially"
   589       (is "eventually (\<lambda>i. ?f i \<le> ?g i) sequentially")
   590     proof (intro always_eventually allI)
   591       fix i have "?f i \<le> (\<integral>\<^sup>+ x. (norm (f x - sf i x)) + ereal (norm (g x - sg i x)) \<partial>M)"
   592         by (auto intro!: nn_integral_mono norm_diff_triangle_ineq)
   593       also have "\<dots> = ?g i"
   594         by (intro nn_integral_add) auto
   595       finally show "?f i \<le> ?g i" .
   596     qed
   597     show "?g ----> 0"
   598       using tendsto_add_ereal[OF _ _ f_sf g_sg] by simp
   599   qed
   600 qed (auto simp: simple_bochner_integral_add tendsto_add)
   601 
   602 lemma has_bochner_integral_bounded_linear:
   603   assumes "bounded_linear T"
   604   shows "has_bochner_integral M f x \<Longrightarrow> has_bochner_integral M (\<lambda>x. T (f x)) (T x)"
   605 proof (safe intro!: has_bochner_integral.intros elim!: has_bochner_integral.cases)
   606   interpret T: bounded_linear T by fact
   607   have [measurable]: "T \<in> borel_measurable borel"
   608     by (intro borel_measurable_continuous_on1 T.continuous_on continuous_on_id)
   609   assume [measurable]: "f \<in> borel_measurable M"
   610   then show "(\<lambda>x. T (f x)) \<in> borel_measurable M"
   611     by auto
   612 
   613   fix s assume f_s: "(\<lambda>i. \<integral>\<^sup>+ x. norm (f x - s i x) \<partial>M) ----> 0"
   614   assume s: "\<forall>i. simple_bochner_integrable M (s i)"
   615   then show "\<And>i. simple_bochner_integrable M (\<lambda>x. T (s i x))"
   616     by (auto intro: simple_bochner_integrable_compose2 T.zero)
   617 
   618   have [measurable]: "\<And>i. s i \<in> borel_measurable M"
   619     using s by (auto intro: borel_measurable_simple_function elim: simple_bochner_integrable.cases)
   620 
   621   obtain K where K: "K > 0" "\<And>x i. norm (T (f x) - T (s i x)) \<le> norm (f x - s i x) * K"
   622     using T.pos_bounded by (auto simp: T.diff[symmetric])
   623 
   624   show "(\<lambda>i. \<integral>\<^sup>+ x. norm (T (f x) - T (s i x)) \<partial>M) ----> 0"
   625     (is "?f ----> 0")
   626   proof (rule tendsto_sandwich)
   627     show "eventually (\<lambda>n. 0 \<le> ?f n) sequentially" "(\<lambda>_. 0) ----> 0"
   628       by (auto simp: nn_integral_nonneg)
   629 
   630     show "eventually (\<lambda>i. ?f i \<le> K * (\<integral>\<^sup>+ x. norm (f x - s i x) \<partial>M)) sequentially"
   631       (is "eventually (\<lambda>i. ?f i \<le> ?g i) sequentially")
   632     proof (intro always_eventually allI)
   633       fix i have "?f i \<le> (\<integral>\<^sup>+ x. ereal K * norm (f x - s i x) \<partial>M)"
   634         using K by (intro nn_integral_mono) (auto simp: mult_ac)
   635       also have "\<dots> = ?g i"
   636         using K by (intro nn_integral_cmult) auto
   637       finally show "?f i \<le> ?g i" .
   638     qed
   639     show "?g ----> 0"
   640       using ereal_lim_mult[OF f_s, of "ereal K"] by simp
   641   qed
   642 
   643   assume "(\<lambda>i. simple_bochner_integral M (s i)) ----> x"
   644   with s show "(\<lambda>i. simple_bochner_integral M (\<lambda>x. T (s i x))) ----> T x"
   645     by (auto intro!: T.tendsto simp: T.simple_bochner_integral_linear)
   646 qed
   647 
   648 lemma has_bochner_integral_zero[intro]: "has_bochner_integral M (\<lambda>x. 0) 0"
   649   by (auto intro!: has_bochner_integral.intros[where s="\<lambda>_ _. 0"]
   650            simp: zero_ereal_def[symmetric] simple_bochner_integrable.simps
   651                  simple_bochner_integral_def image_constant_conv)
   652 
   653 lemma has_bochner_integral_scaleR_left[intro]:
   654   "(c \<noteq> 0 \<Longrightarrow> has_bochner_integral M f x) \<Longrightarrow> has_bochner_integral M (\<lambda>x. f x *\<^sub>R c) (x *\<^sub>R c)"
   655   by (cases "c = 0") (auto simp add: has_bochner_integral_bounded_linear[OF bounded_linear_scaleR_left])
   656 
   657 lemma has_bochner_integral_scaleR_right[intro]:
   658   "(c \<noteq> 0 \<Longrightarrow> has_bochner_integral M f x) \<Longrightarrow> has_bochner_integral M (\<lambda>x. c *\<^sub>R f x) (c *\<^sub>R x)"
   659   by (cases "c = 0") (auto simp add: has_bochner_integral_bounded_linear[OF bounded_linear_scaleR_right])
   660 
   661 lemma has_bochner_integral_mult_left[intro]:
   662   fixes c :: "_::{real_normed_algebra,second_countable_topology}"
   663   shows "(c \<noteq> 0 \<Longrightarrow> has_bochner_integral M f x) \<Longrightarrow> has_bochner_integral M (\<lambda>x. f x * c) (x * c)"
   664   by (cases "c = 0") (auto simp add: has_bochner_integral_bounded_linear[OF bounded_linear_mult_left])
   665 
   666 lemma has_bochner_integral_mult_right[intro]:
   667   fixes c :: "_::{real_normed_algebra,second_countable_topology}"
   668   shows "(c \<noteq> 0 \<Longrightarrow> has_bochner_integral M f x) \<Longrightarrow> has_bochner_integral M (\<lambda>x. c * f x) (c * x)"
   669   by (cases "c = 0") (auto simp add: has_bochner_integral_bounded_linear[OF bounded_linear_mult_right])
   670 
   671 lemmas has_bochner_integral_divide = 
   672   has_bochner_integral_bounded_linear[OF bounded_linear_divide]
   673 
   674 lemma has_bochner_integral_divide_zero[intro]:
   675   fixes c :: "_::{real_normed_field, field_inverse_zero, second_countable_topology}"
   676   shows "(c \<noteq> 0 \<Longrightarrow> has_bochner_integral M f x) \<Longrightarrow> has_bochner_integral M (\<lambda>x. f x / c) (x / c)"
   677   using has_bochner_integral_divide by (cases "c = 0") auto
   678 
   679 lemma has_bochner_integral_inner_left[intro]:
   680   "(c \<noteq> 0 \<Longrightarrow> has_bochner_integral M f x) \<Longrightarrow> has_bochner_integral M (\<lambda>x. f x \<bullet> c) (x \<bullet> c)"
   681   by (cases "c = 0") (auto simp add: has_bochner_integral_bounded_linear[OF bounded_linear_inner_left])
   682 
   683 lemma has_bochner_integral_inner_right[intro]:
   684   "(c \<noteq> 0 \<Longrightarrow> has_bochner_integral M f x) \<Longrightarrow> has_bochner_integral M (\<lambda>x. c \<bullet> f x) (c \<bullet> x)"
   685   by (cases "c = 0") (auto simp add: has_bochner_integral_bounded_linear[OF bounded_linear_inner_right])
   686 
   687 lemmas has_bochner_integral_minus =
   688   has_bochner_integral_bounded_linear[OF bounded_linear_minus[OF bounded_linear_ident]]
   689 lemmas has_bochner_integral_Re =
   690   has_bochner_integral_bounded_linear[OF bounded_linear_Re]
   691 lemmas has_bochner_integral_Im =
   692   has_bochner_integral_bounded_linear[OF bounded_linear_Im]
   693 lemmas has_bochner_integral_cnj =
   694   has_bochner_integral_bounded_linear[OF bounded_linear_cnj]
   695 lemmas has_bochner_integral_of_real =
   696   has_bochner_integral_bounded_linear[OF bounded_linear_of_real]
   697 lemmas has_bochner_integral_fst =
   698   has_bochner_integral_bounded_linear[OF bounded_linear_fst]
   699 lemmas has_bochner_integral_snd =
   700   has_bochner_integral_bounded_linear[OF bounded_linear_snd]
   701 
   702 lemma has_bochner_integral_indicator:
   703   "A \<in> sets M \<Longrightarrow> emeasure M A < \<infinity> \<Longrightarrow>
   704     has_bochner_integral M (\<lambda>x. indicator A x *\<^sub>R c) (measure M A *\<^sub>R c)"
   705   by (intro has_bochner_integral_scaleR_left has_bochner_integral_real_indicator)
   706 
   707 lemma has_bochner_integral_diff:
   708   "has_bochner_integral M f x \<Longrightarrow> has_bochner_integral M g y \<Longrightarrow>
   709     has_bochner_integral M (\<lambda>x. f x - g x) (x - y)"
   710   unfolding diff_conv_add_uminus
   711   by (intro has_bochner_integral_add has_bochner_integral_minus)
   712 
   713 lemma has_bochner_integral_setsum:
   714   "(\<And>i. i \<in> I \<Longrightarrow> has_bochner_integral M (f i) (x i)) \<Longrightarrow>
   715     has_bochner_integral M (\<lambda>x. \<Sum>i\<in>I. f i x) (\<Sum>i\<in>I. x i)"
   716   by (induct I rule: infinite_finite_induct)
   717      (auto intro: has_bochner_integral_zero has_bochner_integral_add)
   718 
   719 lemma has_bochner_integral_implies_finite_norm:
   720   "has_bochner_integral M f x \<Longrightarrow> (\<integral>\<^sup>+x. norm (f x) \<partial>M) < \<infinity>"
   721 proof (elim has_bochner_integral.cases)
   722   fix s v
   723   assume [measurable]: "f \<in> borel_measurable M" and s: "\<And>i. simple_bochner_integrable M (s i)" and
   724     lim_0: "(\<lambda>i. \<integral>\<^sup>+ x. ereal (norm (f x - s i x)) \<partial>M) ----> 0"
   725   from order_tendstoD[OF lim_0, of "\<infinity>"]
   726   obtain i where f_s_fin: "(\<integral>\<^sup>+ x. ereal (norm (f x - s i x)) \<partial>M) < \<infinity>"
   727     by (metis (mono_tags, lifting) eventually_False_sequentially eventually_elim1
   728               less_ereal.simps(4) zero_ereal_def)
   729 
   730   have [measurable]: "\<And>i. s i \<in> borel_measurable M"
   731     using s by (auto intro: borel_measurable_simple_function elim: simple_bochner_integrable.cases)
   732 
   733   def m \<equiv> "if space M = {} then 0 else Max ((\<lambda>x. norm (s i x))`space M)"
   734   have "finite (s i ` space M)"
   735     using s by (auto simp: simple_function_def simple_bochner_integrable.simps)
   736   then have "finite (norm ` s i ` space M)"
   737     by (rule finite_imageI)
   738   then have "\<And>x. x \<in> space M \<Longrightarrow> norm (s i x) \<le> m" "0 \<le> m"
   739     by (auto simp: m_def image_comp comp_def Max_ge_iff)
   740   then have "(\<integral>\<^sup>+x. norm (s i x) \<partial>M) \<le> (\<integral>\<^sup>+x. ereal m * indicator {x\<in>space M. s i x \<noteq> 0} x \<partial>M)"
   741     by (auto split: split_indicator intro!: Max_ge nn_integral_mono simp:)
   742   also have "\<dots> < \<infinity>"
   743     using s by (subst nn_integral_cmult_indicator) (auto simp: `0 \<le> m` simple_bochner_integrable.simps)
   744   finally have s_fin: "(\<integral>\<^sup>+x. norm (s i x) \<partial>M) < \<infinity>" .
   745 
   746   have "(\<integral>\<^sup>+ x. norm (f x) \<partial>M) \<le> (\<integral>\<^sup>+ x. ereal (norm (f x - s i x)) + ereal (norm (s i x)) \<partial>M)"
   747     by (auto intro!: nn_integral_mono) (metis add_commute norm_triangle_sub)
   748   also have "\<dots> = (\<integral>\<^sup>+x. norm (f x - s i x) \<partial>M) + (\<integral>\<^sup>+x. norm (s i x) \<partial>M)"
   749     by (rule nn_integral_add) auto
   750   also have "\<dots> < \<infinity>"
   751     using s_fin f_s_fin by auto
   752   finally show "(\<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M) < \<infinity>" .
   753 qed
   754 
   755 lemma has_bochner_integral_norm_bound:
   756   assumes i: "has_bochner_integral M f x"
   757   shows "norm x \<le> (\<integral>\<^sup>+x. norm (f x) \<partial>M)"
   758 using assms proof
   759   fix s assume
   760     x: "(\<lambda>i. simple_bochner_integral M (s i)) ----> x" (is "?s ----> x") and
   761     s[simp]: "\<And>i. simple_bochner_integrable M (s i)" and
   762     lim: "(\<lambda>i. \<integral>\<^sup>+ x. ereal (norm (f x - s i x)) \<partial>M) ----> 0" and
   763     f[measurable]: "f \<in> borel_measurable M"
   764 
   765   have [measurable]: "\<And>i. s i \<in> borel_measurable M"
   766     using s by (auto simp: simple_bochner_integrable.simps intro: borel_measurable_simple_function)
   767 
   768   show "norm x \<le> (\<integral>\<^sup>+x. norm (f x) \<partial>M)"
   769   proof (rule LIMSEQ_le)
   770     show "(\<lambda>i. ereal (norm (?s i))) ----> norm x"
   771       using x by (intro tendsto_intros lim_ereal[THEN iffD2])
   772     show "\<exists>N. \<forall>n\<ge>N. norm (?s n) \<le> (\<integral>\<^sup>+x. norm (f x - s n x) \<partial>M) + (\<integral>\<^sup>+x. norm (f x) \<partial>M)"
   773       (is "\<exists>N. \<forall>n\<ge>N. _ \<le> ?t n")
   774     proof (intro exI allI impI)
   775       fix n
   776       have "ereal (norm (?s n)) \<le> simple_bochner_integral M (\<lambda>x. norm (s n x))"
   777         by (auto intro!: simple_bochner_integral_norm_bound)
   778       also have "\<dots> = (\<integral>\<^sup>+x. norm (s n x) \<partial>M)"
   779         by (intro simple_bochner_integral_eq_nn_integral)
   780            (auto intro: s simple_bochner_integrable_compose2)
   781       also have "\<dots> \<le> (\<integral>\<^sup>+x. ereal (norm (f x - s n x)) + norm (f x) \<partial>M)"
   782         by (auto intro!: nn_integral_mono)
   783            (metis add_commute norm_minus_commute norm_triangle_sub)
   784       also have "\<dots> = ?t n" 
   785         by (rule nn_integral_add) auto
   786       finally show "norm (?s n) \<le> ?t n" .
   787     qed
   788     have "?t ----> 0 + (\<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M)"
   789       using has_bochner_integral_implies_finite_norm[OF i]
   790       by (intro tendsto_add_ereal tendsto_const lim) auto
   791     then show "?t ----> \<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M"
   792       by simp
   793   qed
   794 qed
   795 
   796 lemma has_bochner_integral_eq:
   797   "has_bochner_integral M f x \<Longrightarrow> has_bochner_integral M f y \<Longrightarrow> x = y"
   798 proof (elim has_bochner_integral.cases)
   799   assume f[measurable]: "f \<in> borel_measurable M"
   800 
   801   fix s t
   802   assume "(\<lambda>i. \<integral>\<^sup>+ x. norm (f x - s i x) \<partial>M) ----> 0" (is "?S ----> 0")
   803   assume "(\<lambda>i. \<integral>\<^sup>+ x. norm (f x - t i x) \<partial>M) ----> 0" (is "?T ----> 0")
   804   assume s: "\<And>i. simple_bochner_integrable M (s i)"
   805   assume t: "\<And>i. simple_bochner_integrable M (t i)"
   806 
   807   have [measurable]: "\<And>i. s i \<in> borel_measurable M" "\<And>i. t i \<in> borel_measurable M"
   808     using s t by (auto intro: borel_measurable_simple_function elim: simple_bochner_integrable.cases)
   809 
   810   let ?s = "\<lambda>i. simple_bochner_integral M (s i)"
   811   let ?t = "\<lambda>i. simple_bochner_integral M (t i)"
   812   assume "?s ----> x" "?t ----> y"
   813   then have "(\<lambda>i. norm (?s i - ?t i)) ----> norm (x - y)"
   814     by (intro tendsto_intros)
   815   moreover
   816   have "(\<lambda>i. ereal (norm (?s i - ?t i))) ----> ereal 0"
   817   proof (rule tendsto_sandwich)
   818     show "eventually (\<lambda>i. 0 \<le> ereal (norm (?s i - ?t i))) sequentially" "(\<lambda>_. 0) ----> ereal 0"
   819       by (auto simp: nn_integral_nonneg zero_ereal_def[symmetric])
   820 
   821     show "eventually (\<lambda>i. norm (?s i - ?t i) \<le> ?S i + ?T i) sequentially"
   822       by (intro always_eventually allI simple_bochner_integral_bounded s t f)
   823     show "(\<lambda>i. ?S i + ?T i) ----> ereal 0"
   824       using tendsto_add_ereal[OF _ _ `?S ----> 0` `?T ----> 0`]
   825       by (simp add: zero_ereal_def[symmetric])
   826   qed
   827   then have "(\<lambda>i. norm (?s i - ?t i)) ----> 0"
   828     by simp
   829   ultimately have "norm (x - y) = 0"
   830     by (rule LIMSEQ_unique)
   831   then show "x = y" by simp
   832 qed
   833 
   834 lemma has_bochner_integralI_AE:
   835   assumes f: "has_bochner_integral M f x"
   836     and g: "g \<in> borel_measurable M"
   837     and ae: "AE x in M. f x = g x"
   838   shows "has_bochner_integral M g x"
   839   using f
   840 proof (safe intro!: has_bochner_integral.intros elim!: has_bochner_integral.cases)
   841   fix s assume "(\<lambda>i. \<integral>\<^sup>+ x. ereal (norm (f x - s i x)) \<partial>M) ----> 0"
   842   also have "(\<lambda>i. \<integral>\<^sup>+ x. ereal (norm (f x - s i x)) \<partial>M) = (\<lambda>i. \<integral>\<^sup>+ x. ereal (norm (g x - s i x)) \<partial>M)"
   843     using ae
   844     by (intro ext nn_integral_cong_AE, eventually_elim) simp
   845   finally show "(\<lambda>i. \<integral>\<^sup>+ x. ereal (norm (g x - s i x)) \<partial>M) ----> 0" .
   846 qed (auto intro: g)
   847 
   848 lemma has_bochner_integral_eq_AE:
   849   assumes f: "has_bochner_integral M f x"
   850     and g: "has_bochner_integral M g y"
   851     and ae: "AE x in M. f x = g x"
   852   shows "x = y"
   853 proof -
   854   from assms have "has_bochner_integral M g x"
   855     by (auto intro: has_bochner_integralI_AE)
   856   from this g show "x = y"
   857     by (rule has_bochner_integral_eq)
   858 qed
   859 
   860 inductive integrable for M f where
   861   "has_bochner_integral M f x \<Longrightarrow> integrable M f"
   862 
   863 definition lebesgue_integral ("integral\<^sup>L") where
   864   "integral\<^sup>L M f = (THE x. has_bochner_integral M f x)"
   865 
   866 syntax
   867   "_lebesgue_integral" :: "pttrn \<Rightarrow> real \<Rightarrow> 'a measure \<Rightarrow> real" ("\<integral> _. _ \<partial>_" [60,61] 110)
   868 
   869 translations
   870   "\<integral> x. f \<partial>M" == "CONST lebesgue_integral M (\<lambda>x. f)"
   871 
   872 lemma has_bochner_integral_integral_eq: "has_bochner_integral M f x \<Longrightarrow> integral\<^sup>L M f = x"
   873   by (metis the_equality has_bochner_integral_eq lebesgue_integral_def)
   874 
   875 lemma has_bochner_integral_integrable:
   876   "integrable M f \<Longrightarrow> has_bochner_integral M f (integral\<^sup>L M f)"
   877   by (auto simp: has_bochner_integral_integral_eq integrable.simps)
   878 
   879 lemma has_bochner_integral_iff:
   880   "has_bochner_integral M f x \<longleftrightarrow> integrable M f \<and> integral\<^sup>L M f = x"
   881   by (metis has_bochner_integral_integrable has_bochner_integral_integral_eq integrable.intros)
   882 
   883 lemma simple_bochner_integrable_eq_integral:
   884   "simple_bochner_integrable M f \<Longrightarrow> simple_bochner_integral M f = integral\<^sup>L M f"
   885   using has_bochner_integral_simple_bochner_integrable[of M f]
   886   by (simp add: has_bochner_integral_integral_eq)
   887 
   888 lemma not_integrable_integral_eq: "\<not> integrable M f \<Longrightarrow> integral\<^sup>L M f = (THE x. False)"
   889   unfolding integrable.simps lebesgue_integral_def by (auto intro!: arg_cong[where f=The])
   890 
   891 lemma integral_eq_cases:
   892   "integrable M f \<longleftrightarrow> integrable N g \<Longrightarrow>
   893     (integrable M f \<Longrightarrow> integrable N g \<Longrightarrow> integral\<^sup>L M f = integral\<^sup>L N g) \<Longrightarrow>
   894     integral\<^sup>L M f = integral\<^sup>L N g"
   895   by (metis not_integrable_integral_eq)
   896 
   897 lemma borel_measurable_integrable[measurable_dest]: "integrable M f \<Longrightarrow> f \<in> borel_measurable M"
   898   by (auto elim: integrable.cases has_bochner_integral.cases)
   899 
   900 lemma integrable_cong:
   901   "M = N \<Longrightarrow> (\<And>x. x \<in> space N \<Longrightarrow> f x = g x) \<Longrightarrow> integrable M f \<longleftrightarrow> integrable N g"
   902   using assms by (simp cong: has_bochner_integral_cong add: integrable.simps)
   903 
   904 lemma integrable_cong_AE:
   905   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> AE x in M. f x = g x \<Longrightarrow>
   906     integrable M f \<longleftrightarrow> integrable M g"
   907   unfolding integrable.simps
   908   by (intro has_bochner_integral_cong_AE arg_cong[where f=Ex] ext)
   909 
   910 lemma integral_cong:
   911   "M = N \<Longrightarrow> (\<And>x. x \<in> space N \<Longrightarrow> f x = g x) \<Longrightarrow> integral\<^sup>L M f = integral\<^sup>L N g"
   912   using assms by (simp cong: has_bochner_integral_cong add: lebesgue_integral_def)
   913 
   914 lemma integral_cong_AE:
   915   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> AE x in M. f x = g x \<Longrightarrow>
   916     integral\<^sup>L M f = integral\<^sup>L M g"
   917   unfolding lebesgue_integral_def
   918   by (intro has_bochner_integral_cong_AE arg_cong[where f=The] ext)
   919 
   920 lemma integrable_add[simp, intro]: "integrable M f \<Longrightarrow> integrable M g \<Longrightarrow> integrable M (\<lambda>x. f x + g x)"
   921   by (auto simp: integrable.simps intro: has_bochner_integral_add)
   922 
   923 lemma integrable_zero[simp, intro]: "integrable M (\<lambda>x. 0)"
   924   by (metis has_bochner_integral_zero integrable.simps) 
   925 
   926 lemma integrable_setsum[simp, intro]: "(\<And>i. i \<in> I \<Longrightarrow> integrable M (f i)) \<Longrightarrow> integrable M (\<lambda>x. \<Sum>i\<in>I. f i x)"
   927   by (metis has_bochner_integral_setsum integrable.simps) 
   928 
   929 lemma integrable_indicator[simp, intro]: "A \<in> sets M \<Longrightarrow> emeasure M A < \<infinity> \<Longrightarrow>
   930   integrable M (\<lambda>x. indicator A x *\<^sub>R c)"
   931   by (metis has_bochner_integral_indicator integrable.simps) 
   932 
   933 lemma integrable_real_indicator[simp, intro]: "A \<in> sets M \<Longrightarrow> emeasure M A < \<infinity> \<Longrightarrow>
   934   integrable M (indicator A :: 'a \<Rightarrow> real)"
   935   by (metis has_bochner_integral_real_indicator integrable.simps)
   936 
   937 lemma integrable_diff[simp, intro]: "integrable M f \<Longrightarrow> integrable M g \<Longrightarrow> integrable M (\<lambda>x. f x - g x)"
   938   by (auto simp: integrable.simps intro: has_bochner_integral_diff)
   939   
   940 lemma integrable_bounded_linear: "bounded_linear T \<Longrightarrow> integrable M f \<Longrightarrow> integrable M (\<lambda>x. T (f x))"
   941   by (auto simp: integrable.simps intro: has_bochner_integral_bounded_linear)
   942 
   943 lemma integrable_scaleR_left[simp, intro]: "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integrable M (\<lambda>x. f x *\<^sub>R c)"
   944   unfolding integrable.simps by fastforce
   945 
   946 lemma integrable_scaleR_right[simp, intro]: "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integrable M (\<lambda>x. c *\<^sub>R f x)"
   947   unfolding integrable.simps by fastforce
   948 
   949 lemma integrable_mult_left[simp, intro]:
   950   fixes c :: "_::{real_normed_algebra,second_countable_topology}"
   951   shows "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integrable M (\<lambda>x. f x * c)"
   952   unfolding integrable.simps by fastforce
   953 
   954 lemma integrable_mult_right[simp, intro]:
   955   fixes c :: "_::{real_normed_algebra,second_countable_topology}"
   956   shows "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integrable M (\<lambda>x. c * f x)"
   957   unfolding integrable.simps by fastforce
   958 
   959 lemma integrable_divide_zero[simp, intro]:
   960   fixes c :: "_::{real_normed_field, field_inverse_zero, second_countable_topology}"
   961   shows "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integrable M (\<lambda>x. f x / c)"
   962   unfolding integrable.simps by fastforce
   963 
   964 lemma integrable_inner_left[simp, intro]:
   965   "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integrable M (\<lambda>x. f x \<bullet> c)"
   966   unfolding integrable.simps by fastforce
   967 
   968 lemma integrable_inner_right[simp, intro]:
   969   "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integrable M (\<lambda>x. c \<bullet> f x)"
   970   unfolding integrable.simps by fastforce
   971 
   972 lemmas integrable_minus[simp, intro] =
   973   integrable_bounded_linear[OF bounded_linear_minus[OF bounded_linear_ident]]
   974 lemmas integrable_divide[simp, intro] =
   975   integrable_bounded_linear[OF bounded_linear_divide]
   976 lemmas integrable_Re[simp, intro] =
   977   integrable_bounded_linear[OF bounded_linear_Re]
   978 lemmas integrable_Im[simp, intro] =
   979   integrable_bounded_linear[OF bounded_linear_Im]
   980 lemmas integrable_cnj[simp, intro] =
   981   integrable_bounded_linear[OF bounded_linear_cnj]
   982 lemmas integrable_of_real[simp, intro] =
   983   integrable_bounded_linear[OF bounded_linear_of_real]
   984 lemmas integrable_fst[simp, intro] =
   985   integrable_bounded_linear[OF bounded_linear_fst]
   986 lemmas integrable_snd[simp, intro] =
   987   integrable_bounded_linear[OF bounded_linear_snd]
   988 
   989 lemma integral_zero[simp]: "integral\<^sup>L M (\<lambda>x. 0) = 0"
   990   by (intro has_bochner_integral_integral_eq has_bochner_integral_zero)
   991 
   992 lemma integral_add[simp]: "integrable M f \<Longrightarrow> integrable M g \<Longrightarrow>
   993     integral\<^sup>L M (\<lambda>x. f x + g x) = integral\<^sup>L M f + integral\<^sup>L M g"
   994   by (intro has_bochner_integral_integral_eq has_bochner_integral_add has_bochner_integral_integrable)
   995 
   996 lemma integral_diff[simp]: "integrable M f \<Longrightarrow> integrable M g \<Longrightarrow>
   997     integral\<^sup>L M (\<lambda>x. f x - g x) = integral\<^sup>L M f - integral\<^sup>L M g"
   998   by (intro has_bochner_integral_integral_eq has_bochner_integral_diff has_bochner_integral_integrable)
   999 
  1000 lemma integral_setsum[simp]: "(\<And>i. i \<in> I \<Longrightarrow> integrable M (f i)) \<Longrightarrow>
  1001   integral\<^sup>L M (\<lambda>x. \<Sum>i\<in>I. f i x) = (\<Sum>i\<in>I. integral\<^sup>L M (f i))"
  1002   by (intro has_bochner_integral_integral_eq has_bochner_integral_setsum has_bochner_integral_integrable)
  1003 
  1004 lemma integral_bounded_linear: "bounded_linear T \<Longrightarrow> integrable M f \<Longrightarrow>
  1005     integral\<^sup>L M (\<lambda>x. T (f x)) = T (integral\<^sup>L M f)"
  1006   by (metis has_bochner_integral_bounded_linear has_bochner_integral_integrable has_bochner_integral_integral_eq)
  1007 
  1008 lemma integral_indicator[simp]: "A \<in> sets M \<Longrightarrow> emeasure M A < \<infinity> \<Longrightarrow>
  1009   integral\<^sup>L M (\<lambda>x. indicator A x *\<^sub>R c) = measure M A *\<^sub>R c"
  1010   by (intro has_bochner_integral_integral_eq has_bochner_integral_indicator has_bochner_integral_integrable)
  1011 
  1012 lemma integral_real_indicator[simp]: "A \<in> sets M \<Longrightarrow> emeasure M A < \<infinity> \<Longrightarrow>
  1013   integral\<^sup>L M (indicator A :: 'a \<Rightarrow> real) = measure M A"
  1014   by (intro has_bochner_integral_integral_eq has_bochner_integral_real_indicator has_bochner_integral_integrable)
  1015 
  1016 lemma integral_scaleR_left[simp]: "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> (\<integral> x. f x *\<^sub>R c \<partial>M) = integral\<^sup>L M f *\<^sub>R c"
  1017   by (intro has_bochner_integral_integral_eq has_bochner_integral_integrable has_bochner_integral_scaleR_left)
  1018 
  1019 lemma integral_scaleR_right[simp]: "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> (\<integral> x. c *\<^sub>R f x \<partial>M) = c *\<^sub>R integral\<^sup>L M f"
  1020   by (intro has_bochner_integral_integral_eq has_bochner_integral_integrable has_bochner_integral_scaleR_right)
  1021 
  1022 lemma integral_mult_left[simp]:
  1023   fixes c :: "_::{real_normed_algebra,second_countable_topology}"
  1024   shows "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> (\<integral> x. f x * c \<partial>M) = integral\<^sup>L M f * c"
  1025   by (intro has_bochner_integral_integral_eq has_bochner_integral_integrable has_bochner_integral_mult_left)
  1026 
  1027 lemma integral_mult_right[simp]:
  1028   fixes c :: "_::{real_normed_algebra,second_countable_topology}"
  1029   shows "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> (\<integral> x. c * f x \<partial>M) = c * integral\<^sup>L M f"
  1030   by (intro has_bochner_integral_integral_eq has_bochner_integral_integrable has_bochner_integral_mult_right)
  1031 
  1032 lemma integral_inner_left[simp]: "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> (\<integral> x. f x \<bullet> c \<partial>M) = integral\<^sup>L M f \<bullet> c"
  1033   by (intro has_bochner_integral_integral_eq has_bochner_integral_integrable has_bochner_integral_inner_left)
  1034 
  1035 lemma integral_inner_right[simp]: "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> (\<integral> x. c \<bullet> f x \<partial>M) = c \<bullet> integral\<^sup>L M f"
  1036   by (intro has_bochner_integral_integral_eq has_bochner_integral_integrable has_bochner_integral_inner_right)
  1037 
  1038 lemma integral_divide_zero[simp]:
  1039   fixes c :: "_::{real_normed_field, field_inverse_zero, second_countable_topology}"
  1040   shows "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integral\<^sup>L M (\<lambda>x. f x / c) = integral\<^sup>L M f / c"
  1041   by (intro has_bochner_integral_integral_eq has_bochner_integral_integrable has_bochner_integral_divide_zero)
  1042 
  1043 lemmas integral_minus[simp] =
  1044   integral_bounded_linear[OF bounded_linear_minus[OF bounded_linear_ident]]
  1045 lemmas integral_divide[simp] =
  1046   integral_bounded_linear[OF bounded_linear_divide]
  1047 lemmas integral_Re[simp] =
  1048   integral_bounded_linear[OF bounded_linear_Re]
  1049 lemmas integral_Im[simp] =
  1050   integral_bounded_linear[OF bounded_linear_Im]
  1051 lemmas integral_cnj[simp] =
  1052   integral_bounded_linear[OF bounded_linear_cnj]
  1053 lemmas integral_of_real[simp] =
  1054   integral_bounded_linear[OF bounded_linear_of_real]
  1055 lemmas integral_fst[simp] =
  1056   integral_bounded_linear[OF bounded_linear_fst]
  1057 lemmas integral_snd[simp] =
  1058   integral_bounded_linear[OF bounded_linear_snd]
  1059 
  1060 lemma integral_norm_bound_ereal:
  1061   "integrable M f \<Longrightarrow> norm (integral\<^sup>L M f) \<le> (\<integral>\<^sup>+x. norm (f x) \<partial>M)"
  1062   by (metis has_bochner_integral_integrable has_bochner_integral_norm_bound)
  1063 
  1064 lemma integrableI_sequence:
  1065   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1066   assumes f[measurable]: "f \<in> borel_measurable M"
  1067   assumes s: "\<And>i. simple_bochner_integrable M (s i)"
  1068   assumes lim: "(\<lambda>i. \<integral>\<^sup>+x. norm (f x - s i x) \<partial>M) ----> 0" (is "?S ----> 0")
  1069   shows "integrable M f"
  1070 proof -
  1071   let ?s = "\<lambda>n. simple_bochner_integral M (s n)"
  1072 
  1073   have "\<exists>x. ?s ----> x"
  1074     unfolding convergent_eq_cauchy
  1075   proof (rule metric_CauchyI)
  1076     fix e :: real assume "0 < e"
  1077     then have "0 < ereal (e / 2)" by auto
  1078     from order_tendstoD(2)[OF lim this]
  1079     obtain M where M: "\<And>n. M \<le> n \<Longrightarrow> ?S n < e / 2"
  1080       by (auto simp: eventually_sequentially)
  1081     show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (?s m) (?s n) < e"
  1082     proof (intro exI allI impI)
  1083       fix m n assume m: "M \<le> m" and n: "M \<le> n"
  1084       have "?S n \<noteq> \<infinity>"
  1085         using M[OF n] by auto
  1086       have "norm (?s n - ?s m) \<le> ?S n + ?S m"
  1087         by (intro simple_bochner_integral_bounded s f)
  1088       also have "\<dots> < ereal (e / 2) + e / 2"
  1089         using ereal_add_strict_mono[OF less_imp_le[OF M[OF n]] _ `?S n \<noteq> \<infinity>` M[OF m]]
  1090         by (auto simp: nn_integral_nonneg)
  1091       also have "\<dots> = e" by simp
  1092       finally show "dist (?s n) (?s m) < e"
  1093         by (simp add: dist_norm)
  1094     qed
  1095   qed
  1096   then obtain x where "?s ----> x" ..
  1097   show ?thesis
  1098     by (rule, rule) fact+
  1099 qed
  1100 
  1101 lemma nn_integral_dominated_convergence_norm:
  1102   fixes u' :: "_ \<Rightarrow> _::{real_normed_vector, second_countable_topology}"
  1103   assumes [measurable]:
  1104        "\<And>i. u i \<in> borel_measurable M" "u' \<in> borel_measurable M" "w \<in> borel_measurable M"
  1105     and bound: "\<And>j. AE x in M. norm (u j x) \<le> w x"
  1106     and w: "(\<integral>\<^sup>+x. w x \<partial>M) < \<infinity>"
  1107     and u': "AE x in M. (\<lambda>i. u i x) ----> u' x"
  1108   shows "(\<lambda>i. (\<integral>\<^sup>+x. norm (u' x - u i x) \<partial>M)) ----> 0"
  1109 proof -
  1110   have "AE x in M. \<forall>j. norm (u j x) \<le> w x"
  1111     unfolding AE_all_countable by rule fact
  1112   with u' have bnd: "AE x in M. \<forall>j. norm (u' x - u j x) \<le> 2 * w x"
  1113   proof (eventually_elim, intro allI)
  1114     fix i x assume "(\<lambda>i. u i x) ----> u' x" "\<forall>j. norm (u j x) \<le> w x" "\<forall>j. norm (u j x) \<le> w x"
  1115     then have "norm (u' x) \<le> w x" "norm (u i x) \<le> w x"
  1116       by (auto intro: LIMSEQ_le_const2 tendsto_norm)
  1117     then have "norm (u' x) + norm (u i x) \<le> 2 * w x"
  1118       by simp
  1119     also have "norm (u' x - u i x) \<le> norm (u' x) + norm (u i x)"
  1120       by (rule norm_triangle_ineq4)
  1121     finally (xtrans) show "norm (u' x - u i x) \<le> 2 * w x" .
  1122   qed
  1123   
  1124   have "(\<lambda>i. (\<integral>\<^sup>+x. norm (u' x - u i x) \<partial>M)) ----> (\<integral>\<^sup>+x. 0 \<partial>M)"
  1125   proof (rule nn_integral_dominated_convergence)  
  1126     show "(\<integral>\<^sup>+x. 2 * w x \<partial>M) < \<infinity>"
  1127       by (rule nn_integral_mult_bounded_inf[OF _ w, of 2]) auto
  1128     show "AE x in M. (\<lambda>i. ereal (norm (u' x - u i x))) ----> 0"
  1129       using u' 
  1130     proof eventually_elim
  1131       fix x assume "(\<lambda>i. u i x) ----> u' x"
  1132       from tendsto_diff[OF tendsto_const[of "u' x"] this]
  1133       show "(\<lambda>i. ereal (norm (u' x - u i x))) ----> 0"
  1134         by (simp add: zero_ereal_def tendsto_norm_zero_iff)
  1135     qed
  1136   qed (insert bnd, auto)
  1137   then show ?thesis by simp
  1138 qed
  1139 
  1140 lemma integrableI_bounded:
  1141   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1142   assumes f[measurable]: "f \<in> borel_measurable M" and fin: "(\<integral>\<^sup>+x. norm (f x) \<partial>M) < \<infinity>"
  1143   shows "integrable M f"
  1144 proof -
  1145   from borel_measurable_implies_sequence_metric[OF f, of 0] obtain s where
  1146     s: "\<And>i. simple_function M (s i)" and
  1147     pointwise: "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. s i x) ----> f x" and
  1148     bound: "\<And>i x. x \<in> space M \<Longrightarrow> norm (s i x) \<le> 2 * norm (f x)"
  1149     by (simp add: norm_conv_dist) metis
  1150   
  1151   show ?thesis
  1152   proof (rule integrableI_sequence)
  1153     { fix i
  1154       have "(\<integral>\<^sup>+x. norm (s i x) \<partial>M) \<le> (\<integral>\<^sup>+x. 2 * ereal (norm (f x)) \<partial>M)"
  1155         by (intro nn_integral_mono) (simp add: bound)
  1156       also have "\<dots> = 2 * (\<integral>\<^sup>+x. ereal (norm (f x)) \<partial>M)"
  1157         by (rule nn_integral_cmult) auto
  1158       finally have "(\<integral>\<^sup>+x. norm (s i x) \<partial>M) < \<infinity>"
  1159         using fin by auto }
  1160     note fin_s = this
  1161 
  1162     show "\<And>i. simple_bochner_integrable M (s i)"
  1163       by (rule simple_bochner_integrableI_bounded) fact+
  1164 
  1165     show "(\<lambda>i. \<integral>\<^sup>+ x. ereal (norm (f x - s i x)) \<partial>M) ----> 0"
  1166     proof (rule nn_integral_dominated_convergence_norm)
  1167       show "\<And>j. AE x in M. norm (s j x) \<le> 2 * norm (f x)"
  1168         using bound by auto
  1169       show "\<And>i. s i \<in> borel_measurable M" "(\<lambda>x. 2 * norm (f x)) \<in> borel_measurable M"
  1170         using s by (auto intro: borel_measurable_simple_function)
  1171       show "(\<integral>\<^sup>+ x. ereal (2 * norm (f x)) \<partial>M) < \<infinity>"
  1172         using fin unfolding times_ereal.simps(1)[symmetric] by (subst nn_integral_cmult) auto
  1173       show "AE x in M. (\<lambda>i. s i x) ----> f x"
  1174         using pointwise by auto
  1175     qed fact
  1176   qed fact
  1177 qed
  1178 
  1179 lemma integrableI_nonneg:
  1180   fixes f :: "'a \<Rightarrow> real"
  1181   assumes "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>M) < \<infinity>"
  1182   shows "integrable M f"
  1183 proof -
  1184   have "(\<integral>\<^sup>+x. norm (f x) \<partial>M) = (\<integral>\<^sup>+x. f x \<partial>M)"
  1185     using assms by (intro nn_integral_cong_AE) auto
  1186   then show ?thesis
  1187     using assms by (intro integrableI_bounded) auto
  1188 qed
  1189 
  1190 lemma integrable_iff_bounded:
  1191   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1192   shows "integrable M f \<longleftrightarrow> f \<in> borel_measurable M \<and> (\<integral>\<^sup>+x. norm (f x) \<partial>M) < \<infinity>"
  1193   using integrableI_bounded[of f M] has_bochner_integral_implies_finite_norm[of M f]
  1194   unfolding integrable.simps has_bochner_integral.simps[abs_def] by auto
  1195 
  1196 lemma integrable_bound:
  1197   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1198     and g :: "'a \<Rightarrow> 'c::{banach, second_countable_topology}"
  1199   shows "integrable M f \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (AE x in M. norm (g x) \<le> norm (f x)) \<Longrightarrow>
  1200     integrable M g"
  1201   unfolding integrable_iff_bounded
  1202 proof safe
  1203   assume "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1204   assume "AE x in M. norm (g x) \<le> norm (f x)"
  1205   then have "(\<integral>\<^sup>+ x. ereal (norm (g x)) \<partial>M) \<le> (\<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M)"
  1206     by  (intro nn_integral_mono_AE) auto
  1207   also assume "(\<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M) < \<infinity>"
  1208   finally show "(\<integral>\<^sup>+ x. ereal (norm (g x)) \<partial>M) < \<infinity>" .
  1209 qed 
  1210 
  1211 lemma integrable_abs[simp, intro]:
  1212   fixes f :: "'a \<Rightarrow> real"
  1213   assumes [measurable]: "integrable M f" shows "integrable M (\<lambda>x. \<bar>f x\<bar>)"
  1214   using assms by (rule integrable_bound) auto
  1215 
  1216 lemma integrable_norm[simp, intro]:
  1217   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1218   assumes [measurable]: "integrable M f" shows "integrable M (\<lambda>x. norm (f x))"
  1219   using assms by (rule integrable_bound) auto
  1220   
  1221 lemma integrable_norm_cancel:
  1222   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1223   assumes [measurable]: "integrable M (\<lambda>x. norm (f x))" "f \<in> borel_measurable M" shows "integrable M f"
  1224   using assms by (rule integrable_bound) auto
  1225 
  1226 lemma integrable_abs_cancel:
  1227   fixes f :: "'a \<Rightarrow> real"
  1228   assumes [measurable]: "integrable M (\<lambda>x. \<bar>f x\<bar>)" "f \<in> borel_measurable M" shows "integrable M f"
  1229   using assms by (rule integrable_bound) auto
  1230 
  1231 lemma integrable_max[simp, intro]:
  1232   fixes f :: "'a \<Rightarrow> real"
  1233   assumes fg[measurable]: "integrable M f" "integrable M g"
  1234   shows "integrable M (\<lambda>x. max (f x) (g x))"
  1235   using integrable_add[OF integrable_norm[OF fg(1)] integrable_norm[OF fg(2)]]
  1236   by (rule integrable_bound) auto
  1237 
  1238 lemma integrable_min[simp, intro]:
  1239   fixes f :: "'a \<Rightarrow> real"
  1240   assumes fg[measurable]: "integrable M f" "integrable M g"
  1241   shows "integrable M (\<lambda>x. min (f x) (g x))"
  1242   using integrable_add[OF integrable_norm[OF fg(1)] integrable_norm[OF fg(2)]]
  1243   by (rule integrable_bound) auto
  1244 
  1245 lemma integral_minus_iff[simp]:
  1246   "integrable M (\<lambda>x. - f x ::'a::{banach, second_countable_topology}) \<longleftrightarrow> integrable M f"
  1247   unfolding integrable_iff_bounded
  1248   by (auto intro: borel_measurable_uminus[of "\<lambda>x. - f x" M, simplified])
  1249 
  1250 lemma integrable_indicator_iff:
  1251   "integrable M (indicator A::_ \<Rightarrow> real) \<longleftrightarrow> A \<inter> space M \<in> sets M \<and> emeasure M (A \<inter> space M) < \<infinity>"
  1252   by (simp add: integrable_iff_bounded borel_measurable_indicator_iff ereal_indicator nn_integral_indicator'
  1253            cong: conj_cong)
  1254 
  1255 lemma integral_dominated_convergence:
  1256   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}" and w :: "'a \<Rightarrow> real"
  1257   assumes "f \<in> borel_measurable M" "\<And>i. s i \<in> borel_measurable M" "integrable M w"
  1258   assumes lim: "AE x in M. (\<lambda>i. s i x) ----> f x"
  1259   assumes bound: "\<And>i. AE x in M. norm (s i x) \<le> w x"
  1260   shows "integrable M f"
  1261     and "\<And>i. integrable M (s i)"
  1262     and "(\<lambda>i. integral\<^sup>L M (s i)) ----> integral\<^sup>L M f"
  1263 proof -
  1264   have "AE x in M. 0 \<le> w x"
  1265     using bound[of 0] by eventually_elim (auto intro: norm_ge_zero order_trans)
  1266   then have "(\<integral>\<^sup>+x. w x \<partial>M) = (\<integral>\<^sup>+x. norm (w x) \<partial>M)"
  1267     by (intro nn_integral_cong_AE) auto
  1268   with `integrable M w` have w: "w \<in> borel_measurable M" "(\<integral>\<^sup>+x. w x \<partial>M) < \<infinity>"
  1269     unfolding integrable_iff_bounded by auto
  1270 
  1271   show int_s: "\<And>i. integrable M (s i)"
  1272     unfolding integrable_iff_bounded
  1273   proof
  1274     fix i 
  1275     have "(\<integral>\<^sup>+ x. ereal (norm (s i x)) \<partial>M) \<le> (\<integral>\<^sup>+x. w x \<partial>M)"
  1276       using bound by (intro nn_integral_mono_AE) auto
  1277     with w show "(\<integral>\<^sup>+ x. ereal (norm (s i x)) \<partial>M) < \<infinity>" by auto
  1278   qed fact
  1279 
  1280   have all_bound: "AE x in M. \<forall>i. norm (s i x) \<le> w x"
  1281     using bound unfolding AE_all_countable by auto
  1282 
  1283   show int_f: "integrable M f"
  1284     unfolding integrable_iff_bounded
  1285   proof
  1286     have "(\<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M) \<le> (\<integral>\<^sup>+x. w x \<partial>M)"
  1287       using all_bound lim
  1288     proof (intro nn_integral_mono_AE, eventually_elim)
  1289       fix x assume "\<forall>i. norm (s i x) \<le> w x" "(\<lambda>i. s i x) ----> f x"
  1290       then show "ereal (norm (f x)) \<le> ereal (w x)"
  1291         by (intro LIMSEQ_le_const2[where X="\<lambda>i. ereal (norm (s i x))"] tendsto_intros lim_ereal[THEN iffD2]) auto
  1292     qed
  1293     with w show "(\<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M) < \<infinity>" by auto
  1294   qed fact
  1295 
  1296   have "(\<lambda>n. ereal (norm (integral\<^sup>L M (s n) - integral\<^sup>L M f))) ----> ereal 0" (is "?d ----> ereal 0")
  1297   proof (rule tendsto_sandwich)
  1298     show "eventually (\<lambda>n. ereal 0 \<le> ?d n) sequentially" "(\<lambda>_. ereal 0) ----> ereal 0" by auto
  1299     show "eventually (\<lambda>n. ?d n \<le> (\<integral>\<^sup>+x. norm (s n x - f x) \<partial>M)) sequentially"
  1300     proof (intro always_eventually allI)
  1301       fix n
  1302       have "?d n = norm (integral\<^sup>L M (\<lambda>x. s n x - f x))"
  1303         using int_f int_s by simp
  1304       also have "\<dots> \<le> (\<integral>\<^sup>+x. norm (s n x - f x) \<partial>M)"
  1305         by (intro int_f int_s integrable_diff integral_norm_bound_ereal)
  1306       finally show "?d n \<le> (\<integral>\<^sup>+x. norm (s n x - f x) \<partial>M)" .
  1307     qed
  1308     show "(\<lambda>n. \<integral>\<^sup>+x. norm (s n x - f x) \<partial>M) ----> ereal 0"
  1309       unfolding zero_ereal_def[symmetric]
  1310       apply (subst norm_minus_commute)
  1311     proof (rule nn_integral_dominated_convergence_norm[where w=w])
  1312       show "\<And>n. s n \<in> borel_measurable M"
  1313         using int_s unfolding integrable_iff_bounded by auto
  1314     qed fact+
  1315   qed
  1316   then have "(\<lambda>n. integral\<^sup>L M (s n) - integral\<^sup>L M f) ----> 0"
  1317     unfolding lim_ereal tendsto_norm_zero_iff .
  1318   from tendsto_add[OF this tendsto_const[of "integral\<^sup>L M f"]]
  1319   show "(\<lambda>i. integral\<^sup>L M (s i)) ----> integral\<^sup>L M f"  by simp
  1320 qed
  1321 
  1322 lemma integrable_mult_left_iff:
  1323   fixes f :: "'a \<Rightarrow> real"
  1324   shows "integrable M (\<lambda>x. c * f x) \<longleftrightarrow> c = 0 \<or> integrable M f"
  1325   using integrable_mult_left[of c M f] integrable_mult_left[of "1 / c" M "\<lambda>x. c * f x"]
  1326   by (cases "c = 0") auto
  1327 
  1328 lemma nn_integral_eq_integral:
  1329   assumes f: "integrable M f"
  1330   assumes nonneg: "AE x in M. 0 \<le> f x" 
  1331   shows "(\<integral>\<^sup>+ x. f x \<partial>M) = integral\<^sup>L M f"
  1332 proof -
  1333   { fix f :: "'a \<Rightarrow> real" assume f: "f \<in> borel_measurable M" "\<And>x. 0 \<le> f x" "integrable M f"
  1334     then have "(\<integral>\<^sup>+ x. f x \<partial>M) = integral\<^sup>L M f"
  1335     proof (induct rule: borel_measurable_induct_real)
  1336       case (set A) then show ?case
  1337         by (simp add: integrable_indicator_iff ereal_indicator emeasure_eq_ereal_measure)
  1338     next
  1339       case (mult f c) then show ?case
  1340         unfolding times_ereal.simps(1)[symmetric]
  1341         by (subst nn_integral_cmult)
  1342            (auto simp add: integrable_mult_left_iff zero_ereal_def[symmetric])
  1343     next
  1344       case (add g f)
  1345       then have "integrable M f" "integrable M g"
  1346         by (auto intro!: integrable_bound[OF add(8)])
  1347       with add show ?case
  1348         unfolding plus_ereal.simps(1)[symmetric]
  1349         by (subst nn_integral_add) auto
  1350     next
  1351       case (seq s)
  1352       { fix i x assume "x \<in> space M" with seq(4) have "s i x \<le> f x"
  1353           by (intro LIMSEQ_le_const[OF seq(5)] exI[of _ i]) (auto simp: incseq_def le_fun_def) }
  1354       note s_le_f = this
  1355 
  1356       show ?case
  1357       proof (rule LIMSEQ_unique)
  1358         show "(\<lambda>i. ereal (integral\<^sup>L M (s i))) ----> ereal (integral\<^sup>L M f)"
  1359           unfolding lim_ereal
  1360         proof (rule integral_dominated_convergence[where w=f])
  1361           show "integrable M f" by fact
  1362           from s_le_f seq show "\<And>i. AE x in M. norm (s i x) \<le> f x"
  1363             by auto
  1364         qed (insert seq, auto)
  1365         have int_s: "\<And>i. integrable M (s i)"
  1366           using seq f s_le_f by (intro integrable_bound[OF f(3)]) auto
  1367         have "(\<lambda>i. \<integral>\<^sup>+ x. s i x \<partial>M) ----> \<integral>\<^sup>+ x. f x \<partial>M"
  1368           using seq s_le_f f
  1369           by (intro nn_integral_dominated_convergence[where w=f])
  1370              (auto simp: integrable_iff_bounded)
  1371         also have "(\<lambda>i. \<integral>\<^sup>+x. s i x \<partial>M) = (\<lambda>i. \<integral>x. s i x \<partial>M)"
  1372           using seq int_s by simp
  1373         finally show "(\<lambda>i. \<integral>x. s i x \<partial>M) ----> \<integral>\<^sup>+x. f x \<partial>M"
  1374           by simp
  1375       qed
  1376     qed }
  1377   from this[of "\<lambda>x. max 0 (f x)"] assms have "(\<integral>\<^sup>+ x. max 0 (f x) \<partial>M) = integral\<^sup>L M (\<lambda>x. max 0 (f x))"
  1378     by simp
  1379   also have "\<dots> = integral\<^sup>L M f"
  1380     using assms by (auto intro!: integral_cong_AE)
  1381   also have "(\<integral>\<^sup>+ x. max 0 (f x) \<partial>M) = (\<integral>\<^sup>+ x. f x \<partial>M)"
  1382     using assms by (auto intro!: nn_integral_cong_AE simp: max_def)
  1383   finally show ?thesis .
  1384 qed
  1385 
  1386 lemma integral_norm_bound:
  1387   fixes f :: "_ \<Rightarrow> 'a :: {banach, second_countable_topology}"
  1388   shows "integrable M f \<Longrightarrow> norm (integral\<^sup>L M f) \<le> (\<integral>x. norm (f x) \<partial>M)"
  1389   using nn_integral_eq_integral[of M "\<lambda>x. norm (f x)"]
  1390   using integral_norm_bound_ereal[of M f] by simp
  1391   
  1392 lemma integral_eq_nn_integral:
  1393   "integrable M f \<Longrightarrow> (\<And>x. 0 \<le> f x) \<Longrightarrow> integral\<^sup>L M f = real (\<integral>\<^sup>+ x. ereal (f x) \<partial>M)"
  1394   by (subst nn_integral_eq_integral) auto
  1395   
  1396 lemma integrableI_simple_bochner_integrable:
  1397   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1398   shows "simple_bochner_integrable M f \<Longrightarrow> integrable M f"
  1399   by (intro integrableI_sequence[where s="\<lambda>_. f"] borel_measurable_simple_function)
  1400      (auto simp: zero_ereal_def[symmetric] simple_bochner_integrable.simps)
  1401 
  1402 lemma integrable_induct[consumes 1, case_names base add lim, induct pred: integrable]:
  1403   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1404   assumes "integrable M f"
  1405   assumes base: "\<And>A c. A \<in> sets M \<Longrightarrow> emeasure M A < \<infinity> \<Longrightarrow> P (\<lambda>x. indicator A x *\<^sub>R c)"
  1406   assumes add: "\<And>f g. integrable M f \<Longrightarrow> P f \<Longrightarrow> integrable M g \<Longrightarrow> P g \<Longrightarrow> P (\<lambda>x. f x + g x)"
  1407   assumes lim: "\<And>f s. (\<And>i. integrable M (s i)) \<Longrightarrow> (\<And>i. P (s i)) \<Longrightarrow>
  1408    (\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. s i x) ----> f x) \<Longrightarrow>
  1409    (\<And>i x. x \<in> space M \<Longrightarrow> norm (s i x) \<le> 2 * norm (f x)) \<Longrightarrow> integrable M f \<Longrightarrow> P f"
  1410   shows "P f"
  1411 proof -
  1412   from `integrable M f` have f: "f \<in> borel_measurable M" "(\<integral>\<^sup>+x. norm (f x) \<partial>M) < \<infinity>"
  1413     unfolding integrable_iff_bounded by auto
  1414   from borel_measurable_implies_sequence_metric[OF f(1)]
  1415   obtain s where s: "\<And>i. simple_function M (s i)" "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. s i x) ----> f x"
  1416     "\<And>i x. x \<in> space M \<Longrightarrow> norm (s i x) \<le> 2 * norm (f x)"
  1417     unfolding norm_conv_dist by metis
  1418 
  1419   { fix f A 
  1420     have [simp]: "P (\<lambda>x. 0)"
  1421       using base[of "{}" undefined] by simp
  1422     have "(\<And>i::'b. i \<in> A \<Longrightarrow> integrable M (f i::'a \<Rightarrow> 'b)) \<Longrightarrow>
  1423     (\<And>i. i \<in> A \<Longrightarrow> P (f i)) \<Longrightarrow> P (\<lambda>x. \<Sum>i\<in>A. f i x)"
  1424     by (induct A rule: infinite_finite_induct) (auto intro!: add) }
  1425   note setsum = this
  1426 
  1427   def s' \<equiv> "\<lambda>i z. indicator (space M) z *\<^sub>R s i z"
  1428   then have s'_eq_s: "\<And>i x. x \<in> space M \<Longrightarrow> s' i x = s i x"
  1429     by simp
  1430 
  1431   have sf[measurable]: "\<And>i. simple_function M (s' i)"
  1432     unfolding s'_def using s(1)
  1433     by (intro simple_function_compose2[where h="op *\<^sub>R"] simple_function_indicator) auto
  1434 
  1435   { fix i 
  1436     have "\<And>z. {y. s' i z = y \<and> y \<in> s' i ` space M \<and> y \<noteq> 0 \<and> z \<in> space M} =
  1437         (if z \<in> space M \<and> s' i z \<noteq> 0 then {s' i z} else {})"
  1438       by (auto simp add: s'_def split: split_indicator)
  1439     then have "\<And>z. s' i = (\<lambda>z. \<Sum>y\<in>s' i`space M - {0}. indicator {x\<in>space M. s' i x = y} z *\<^sub>R y)"
  1440       using sf by (auto simp: fun_eq_iff simple_function_def s'_def) }
  1441   note s'_eq = this
  1442 
  1443   show "P f"
  1444   proof (rule lim)
  1445     fix i
  1446 
  1447     have "(\<integral>\<^sup>+x. norm (s' i x) \<partial>M) \<le> (\<integral>\<^sup>+x. 2 * ereal (norm (f x)) \<partial>M)"
  1448       using s by (intro nn_integral_mono) (auto simp: s'_eq_s)
  1449     also have "\<dots> < \<infinity>"
  1450       using f by (subst nn_integral_cmult) auto
  1451     finally have sbi: "simple_bochner_integrable M (s' i)"
  1452       using sf by (intro simple_bochner_integrableI_bounded) auto
  1453     then show "integrable M (s' i)"
  1454       by (rule integrableI_simple_bochner_integrable)
  1455 
  1456     { fix x assume"x \<in> space M" "s' i x \<noteq> 0"
  1457       then have "emeasure M {y \<in> space M. s' i y = s' i x} \<le> emeasure M {y \<in> space M. s' i y \<noteq> 0}"
  1458         by (intro emeasure_mono) auto
  1459       also have "\<dots> < \<infinity>"
  1460         using sbi by (auto elim: simple_bochner_integrable.cases)
  1461       finally have "emeasure M {y \<in> space M. s' i y = s' i x} \<noteq> \<infinity>" by simp }
  1462     then show "P (s' i)"
  1463       by (subst s'_eq) (auto intro!: setsum base)
  1464 
  1465     fix x assume "x \<in> space M" with s show "(\<lambda>i. s' i x) ----> f x"
  1466       by (simp add: s'_eq_s)
  1467     show "norm (s' i x) \<le> 2 * norm (f x)"
  1468       using `x \<in> space M` s by (simp add: s'_eq_s)
  1469   qed fact
  1470 qed
  1471 
  1472 lemma integral_nonneg_AE:
  1473   fixes f :: "'a \<Rightarrow> real"
  1474   assumes [measurable]: "integrable M f" "AE x in M. 0 \<le> f x"
  1475   shows "0 \<le> integral\<^sup>L M f"
  1476 proof -
  1477   have "0 \<le> ereal (integral\<^sup>L M (\<lambda>x. max 0 (f x)))"
  1478     by (subst integral_eq_nn_integral)
  1479        (auto intro: real_of_ereal_pos nn_integral_nonneg integrable_max assms)
  1480   also have "integral\<^sup>L M (\<lambda>x. max 0 (f x)) = integral\<^sup>L M f"
  1481     using assms(2) by (intro integral_cong_AE assms integrable_max) auto
  1482   finally show ?thesis
  1483     by simp
  1484 qed
  1485 
  1486 lemma integral_eq_zero_AE:
  1487   "f \<in> borel_measurable M \<Longrightarrow> (AE x in M. f x = 0) \<Longrightarrow> integral\<^sup>L M f = 0"
  1488   using integral_cong_AE[of f M "\<lambda>_. 0"] by simp
  1489 
  1490 lemma integral_nonneg_eq_0_iff_AE:
  1491   fixes f :: "_ \<Rightarrow> real"
  1492   assumes f[measurable]: "integrable M f" and nonneg: "AE x in M. 0 \<le> f x"
  1493   shows "integral\<^sup>L M f = 0 \<longleftrightarrow> (AE x in M. f x = 0)"
  1494 proof
  1495   assume "integral\<^sup>L M f = 0"
  1496   then have "integral\<^sup>N M f = 0"
  1497     using nn_integral_eq_integral[OF f nonneg] by simp
  1498   then have "AE x in M. ereal (f x) \<le> 0"
  1499     by (simp add: nn_integral_0_iff_AE)
  1500   with nonneg show "AE x in M. f x = 0"
  1501     by auto
  1502 qed (auto simp add: integral_eq_zero_AE)
  1503 
  1504 lemma integral_mono_AE:
  1505   fixes f :: "'a \<Rightarrow> real"
  1506   assumes "integrable M f" "integrable M g" "AE x in M. f x \<le> g x"
  1507   shows "integral\<^sup>L M f \<le> integral\<^sup>L M g"
  1508 proof -
  1509   have "0 \<le> integral\<^sup>L M (\<lambda>x. g x - f x)"
  1510     using assms by (intro integral_nonneg_AE integrable_diff assms) auto
  1511   also have "\<dots> = integral\<^sup>L M g - integral\<^sup>L M f"
  1512     by (intro integral_diff assms)
  1513   finally show ?thesis by simp
  1514 qed
  1515 
  1516 lemma integral_mono:
  1517   fixes f :: "'a \<Rightarrow> real"
  1518   shows "integrable M f \<Longrightarrow> integrable M g \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> f x \<le> g x) \<Longrightarrow> 
  1519     integral\<^sup>L M f \<le> integral\<^sup>L M g"
  1520   by (intro integral_mono_AE) auto
  1521 
  1522 lemma (in finite_measure) integrable_measure: 
  1523   assumes I: "disjoint_family_on X I" "countable I"
  1524   shows "integrable (count_space I) (\<lambda>i. measure M (X i))"
  1525 proof -
  1526   have "(\<integral>\<^sup>+i. measure M (X i) \<partial>count_space I) = (\<integral>\<^sup>+i. measure M (if X i \<in> sets M then X i else {}) \<partial>count_space I)"
  1527     by (auto intro!: nn_integral_cong measure_notin_sets)
  1528   also have "\<dots> = measure M (\<Union>i\<in>I. if X i \<in> sets M then X i else {})"
  1529     using I unfolding emeasure_eq_measure[symmetric]
  1530     by (subst emeasure_UN_countable) (auto simp: disjoint_family_on_def)
  1531   finally show ?thesis
  1532     by (auto intro!: integrableI_bounded simp: measure_nonneg)
  1533 qed
  1534 
  1535 lemma integrableI_real_bounded:
  1536   assumes f: "f \<in> borel_measurable M" and ae: "AE x in M. 0 \<le> f x" and fin: "integral\<^sup>N M f < \<infinity>"
  1537   shows "integrable M f"
  1538 proof (rule integrableI_bounded)
  1539   have "(\<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M) = \<integral>\<^sup>+ x. ereal (f x) \<partial>M"
  1540     using ae by (auto intro: nn_integral_cong_AE)
  1541   also note fin
  1542   finally show "(\<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M) < \<infinity>" .
  1543 qed fact
  1544 
  1545 lemma integral_real_bounded:
  1546   assumes "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x" "integral\<^sup>N M f \<le> ereal r"
  1547   shows "integral\<^sup>L M f \<le> r"
  1548 proof -
  1549   have "integrable M f"
  1550     using assms by (intro integrableI_real_bounded) auto
  1551   from nn_integral_eq_integral[OF this] assms show ?thesis
  1552     by simp
  1553 qed
  1554 
  1555 subsection {* Measure spaces with an associated density *}
  1556 
  1557 lemma integrable_density:
  1558   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}" and g :: "'a \<Rightarrow> real"
  1559   assumes [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1560     and nn: "AE x in M. 0 \<le> g x"
  1561   shows "integrable (density M g) f \<longleftrightarrow> integrable M (\<lambda>x. g x *\<^sub>R f x)"
  1562   unfolding integrable_iff_bounded using nn
  1563   apply (simp add: nn_integral_density )
  1564   apply (intro arg_cong2[where f="op ="] refl nn_integral_cong_AE)
  1565   apply auto
  1566   done
  1567 
  1568 lemma integral_density:
  1569   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}" and g :: "'a \<Rightarrow> real"
  1570   assumes f: "f \<in> borel_measurable M"
  1571     and g[measurable]: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
  1572   shows "integral\<^sup>L (density M g) f = integral\<^sup>L M (\<lambda>x. g x *\<^sub>R f x)"
  1573 proof (rule integral_eq_cases)
  1574   assume "integrable (density M g) f"
  1575   then show ?thesis
  1576   proof induct
  1577     case (base A c)
  1578     then have [measurable]: "A \<in> sets M" by auto
  1579   
  1580     have int: "integrable M (\<lambda>x. g x * indicator A x)"
  1581       using g base integrable_density[of "indicator A :: 'a \<Rightarrow> real" M g] by simp
  1582     then have "integral\<^sup>L M (\<lambda>x. g x * indicator A x) = (\<integral>\<^sup>+ x. ereal (g x * indicator A x) \<partial>M)"
  1583       using g by (subst nn_integral_eq_integral) auto
  1584     also have "\<dots> = (\<integral>\<^sup>+ x. ereal (g x) * indicator A x \<partial>M)"
  1585       by (intro nn_integral_cong) (auto split: split_indicator)
  1586     also have "\<dots> = emeasure (density M g) A"
  1587       by (rule emeasure_density[symmetric]) auto
  1588     also have "\<dots> = ereal (measure (density M g) A)"
  1589       using base by (auto intro: emeasure_eq_ereal_measure)
  1590     also have "\<dots> = integral\<^sup>L (density M g) (indicator A)"
  1591       using base by simp
  1592     finally show ?case
  1593       using base by (simp add: int)
  1594   next
  1595     case (add f h)
  1596     then have [measurable]: "f \<in> borel_measurable M" "h \<in> borel_measurable M"
  1597       by (auto dest!: borel_measurable_integrable)
  1598     from add g show ?case
  1599       by (simp add: scaleR_add_right integrable_density)
  1600   next
  1601     case (lim f s)
  1602     have [measurable]: "f \<in> borel_measurable M" "\<And>i. s i \<in> borel_measurable M"
  1603       using lim(1,5)[THEN borel_measurable_integrable] by auto
  1604   
  1605     show ?case
  1606     proof (rule LIMSEQ_unique)
  1607       show "(\<lambda>i. integral\<^sup>L M (\<lambda>x. g x *\<^sub>R s i x)) ----> integral\<^sup>L M (\<lambda>x. g x *\<^sub>R f x)"
  1608       proof (rule integral_dominated_convergence(3))
  1609         show "integrable M (\<lambda>x. 2 * norm (g x *\<^sub>R f x))"
  1610           by (intro integrable_mult_right integrable_norm integrable_density[THEN iffD1] lim g) auto
  1611         show "AE x in M. (\<lambda>i. g x *\<^sub>R s i x) ----> g x *\<^sub>R f x"
  1612           using lim(3) by (auto intro!: tendsto_scaleR AE_I2[of M])
  1613         show "\<And>i. AE x in M. norm (g x *\<^sub>R s i x) \<le> 2 * norm (g x *\<^sub>R f x)"
  1614           using lim(4) g by (auto intro!: AE_I2[of M] mult_left_mono simp: field_simps)
  1615       qed auto
  1616       show "(\<lambda>i. integral\<^sup>L M (\<lambda>x. g x *\<^sub>R s i x)) ----> integral\<^sup>L (density M g) f"
  1617         unfolding lim(2)[symmetric]
  1618         by (rule integral_dominated_convergence(3)[where w="\<lambda>x. 2 * norm (f x)"])
  1619            (insert lim(3-5), auto intro: integrable_norm)
  1620     qed
  1621   qed
  1622 qed (simp add: f g integrable_density)
  1623 
  1624 lemma
  1625   fixes g :: "'a \<Rightarrow> real"
  1626   assumes "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x" "g \<in> borel_measurable M"
  1627   shows integral_real_density: "integral\<^sup>L (density M f) g = (\<integral> x. f x * g x \<partial>M)"
  1628     and integrable_real_density: "integrable (density M f) g \<longleftrightarrow> integrable M (\<lambda>x. f x * g x)"
  1629   using assms integral_density[of g M f] integrable_density[of g M f] by auto
  1630 
  1631 lemma has_bochner_integral_density:
  1632   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}" and g :: "'a \<Rightarrow> real"
  1633   shows "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (AE x in M. 0 \<le> g x) \<Longrightarrow>
  1634     has_bochner_integral M (\<lambda>x. g x *\<^sub>R f x) x \<Longrightarrow> has_bochner_integral (density M g) f x"
  1635   by (simp add: has_bochner_integral_iff integrable_density integral_density)
  1636 
  1637 subsection {* Distributions *}
  1638 
  1639 lemma integrable_distr_eq:
  1640   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1641   assumes [measurable]: "g \<in> measurable M N" "f \<in> borel_measurable N"
  1642   shows "integrable (distr M N g) f \<longleftrightarrow> integrable M (\<lambda>x. f (g x))"
  1643   unfolding integrable_iff_bounded by (simp_all add: nn_integral_distr)
  1644 
  1645 lemma integrable_distr:
  1646   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1647   shows "T \<in> measurable M M' \<Longrightarrow> integrable (distr M M' T) f \<Longrightarrow> integrable M (\<lambda>x. f (T x))"
  1648   by (subst integrable_distr_eq[symmetric, where g=T])
  1649      (auto dest: borel_measurable_integrable)
  1650 
  1651 lemma integral_distr:
  1652   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1653   assumes g[measurable]: "g \<in> measurable M N" and f: "f \<in> borel_measurable N"
  1654   shows "integral\<^sup>L (distr M N g) f = integral\<^sup>L M (\<lambda>x. f (g x))"
  1655 proof (rule integral_eq_cases)
  1656   assume "integrable (distr M N g) f"
  1657   then show ?thesis
  1658   proof induct
  1659     case (base A c)
  1660     then have [measurable]: "A \<in> sets N" by auto
  1661     from base have int: "integrable (distr M N g) (\<lambda>a. indicator A a *\<^sub>R c)"
  1662       by (intro integrable_indicator)
  1663   
  1664     have "integral\<^sup>L (distr M N g) (\<lambda>a. indicator A a *\<^sub>R c) = measure (distr M N g) A *\<^sub>R c"
  1665       using base by (subst integral_indicator) auto
  1666     also have "\<dots> = measure M (g -` A \<inter> space M) *\<^sub>R c"
  1667       by (subst measure_distr) auto
  1668     also have "\<dots> = integral\<^sup>L M (\<lambda>a. indicator (g -` A \<inter> space M) a *\<^sub>R c)"
  1669       using base by (subst integral_indicator) (auto simp: emeasure_distr)
  1670     also have "\<dots> = integral\<^sup>L M (\<lambda>a. indicator A (g a) *\<^sub>R c)"
  1671       using int base by (intro integral_cong_AE) (auto simp: emeasure_distr split: split_indicator)
  1672     finally show ?case .
  1673   next
  1674     case (add f h)
  1675     then have [measurable]: "f \<in> borel_measurable N" "h \<in> borel_measurable N"
  1676       by (auto dest!: borel_measurable_integrable)
  1677     from add g show ?case
  1678       by (simp add: scaleR_add_right integrable_distr_eq)
  1679   next
  1680     case (lim f s)
  1681     have [measurable]: "f \<in> borel_measurable N" "\<And>i. s i \<in> borel_measurable N"
  1682       using lim(1,5)[THEN borel_measurable_integrable] by auto
  1683   
  1684     show ?case
  1685     proof (rule LIMSEQ_unique)
  1686       show "(\<lambda>i. integral\<^sup>L M (\<lambda>x. s i (g x))) ----> integral\<^sup>L M (\<lambda>x. f (g x))"
  1687       proof (rule integral_dominated_convergence(3))
  1688         show "integrable M (\<lambda>x. 2 * norm (f (g x)))"
  1689           using lim by (auto intro!: integrable_norm simp: integrable_distr_eq) 
  1690         show "AE x in M. (\<lambda>i. s i (g x)) ----> f (g x)"
  1691           using lim(3) g[THEN measurable_space] by auto
  1692         show "\<And>i. AE x in M. norm (s i (g x)) \<le> 2 * norm (f (g x))"
  1693           using lim(4) g[THEN measurable_space] by auto
  1694       qed auto
  1695       show "(\<lambda>i. integral\<^sup>L M (\<lambda>x. s i (g x))) ----> integral\<^sup>L (distr M N g) f"
  1696         unfolding lim(2)[symmetric]
  1697         by (rule integral_dominated_convergence(3)[where w="\<lambda>x. 2 * norm (f x)"])
  1698            (insert lim(3-5), auto intro: integrable_norm)
  1699     qed
  1700   qed
  1701 qed (simp add: f g integrable_distr_eq)
  1702 
  1703 lemma has_bochner_integral_distr:
  1704   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1705   shows "f \<in> borel_measurable N \<Longrightarrow> g \<in> measurable M N \<Longrightarrow>
  1706     has_bochner_integral M (\<lambda>x. f (g x)) x \<Longrightarrow> has_bochner_integral (distr M N g) f x"
  1707   by (simp add: has_bochner_integral_iff integrable_distr_eq integral_distr)
  1708 
  1709 subsection {* Lebesgue integration on @{const count_space} *}
  1710 
  1711 lemma integrable_count_space:
  1712   fixes f :: "'a \<Rightarrow> 'b::{banach,second_countable_topology}"
  1713   shows "finite X \<Longrightarrow> integrable (count_space X) f"
  1714   by (auto simp: nn_integral_count_space integrable_iff_bounded)
  1715 
  1716 lemma measure_count_space[simp]:
  1717   "B \<subseteq> A \<Longrightarrow> finite B \<Longrightarrow> measure (count_space A) B = card B"
  1718   unfolding measure_def by (subst emeasure_count_space ) auto
  1719 
  1720 lemma lebesgue_integral_count_space_finite_support:
  1721   assumes f: "finite {a\<in>A. f a \<noteq> 0}"
  1722   shows "(\<integral>x. f x \<partial>count_space A) = (\<Sum>a | a \<in> A \<and> f a \<noteq> 0. f a)"
  1723 proof -
  1724   have eq: "\<And>x. x \<in> A \<Longrightarrow> (\<Sum>a | x = a \<and> a \<in> A \<and> f a \<noteq> 0. f a) = (\<Sum>x\<in>{x}. f x)"
  1725     by (intro setsum_mono_zero_cong_left) auto
  1726     
  1727   have "(\<integral>x. f x \<partial>count_space A) = (\<integral>x. (\<Sum>a | a \<in> A \<and> f a \<noteq> 0. indicator {a} x *\<^sub>R f a) \<partial>count_space A)"
  1728     by (intro integral_cong refl) (simp add: f eq)
  1729   also have "\<dots> = (\<Sum>a | a \<in> A \<and> f a \<noteq> 0. measure (count_space A) {a} *\<^sub>R f a)"
  1730     by (subst integral_setsum) (auto intro!: setsum_cong)
  1731   finally show ?thesis
  1732     by auto
  1733 qed
  1734 
  1735 lemma lebesgue_integral_count_space_finite: "finite A \<Longrightarrow> (\<integral>x. f x \<partial>count_space A) = (\<Sum>a\<in>A. f a)"
  1736   by (subst lebesgue_integral_count_space_finite_support)
  1737      (auto intro!: setsum_mono_zero_cong_left)
  1738 
  1739 subsection {* Point measure *}
  1740 
  1741 lemma lebesgue_integral_point_measure_finite:
  1742   fixes g :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1743   shows "finite A \<Longrightarrow> (\<And>a. a \<in> A \<Longrightarrow> 0 \<le> f a) \<Longrightarrow>
  1744     integral\<^sup>L (point_measure A f) g = (\<Sum>a\<in>A. f a *\<^sub>R g a)"
  1745   by (simp add: lebesgue_integral_count_space_finite AE_count_space integral_density point_measure_def)
  1746 
  1747 lemma integrable_point_measure_finite:
  1748   fixes g :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}" and f :: "'a \<Rightarrow> real"
  1749   shows "finite A \<Longrightarrow> integrable (point_measure A f) g"
  1750   unfolding point_measure_def
  1751   apply (subst density_ereal_max_0)
  1752   apply (subst integrable_density)
  1753   apply (auto simp: AE_count_space integrable_count_space)
  1754   done
  1755 
  1756 subsection {* Legacy lemmas for the real-valued Lebesgue integral *}
  1757 
  1758 lemma real_lebesgue_integral_def:
  1759   assumes f: "integrable M f"
  1760   shows "integral\<^sup>L M f = real (\<integral>\<^sup>+x. f x \<partial>M) - real (\<integral>\<^sup>+x. - f x \<partial>M)"
  1761 proof -
  1762   have "integral\<^sup>L M f = integral\<^sup>L M (\<lambda>x. max 0 (f x) - max 0 (- f x))"
  1763     by (auto intro!: arg_cong[where f="integral\<^sup>L M"])
  1764   also have "\<dots> = integral\<^sup>L M (\<lambda>x. max 0 (f x)) - integral\<^sup>L M (\<lambda>x. max 0 (- f x))"
  1765     by (intro integral_diff integrable_max integrable_minus integrable_zero f)
  1766   also have "integral\<^sup>L M (\<lambda>x. max 0 (f x)) = real (\<integral>\<^sup>+x. max 0 (f x) \<partial>M)"
  1767     by (subst integral_eq_nn_integral[symmetric]) (auto intro!: integrable_max f)
  1768   also have "integral\<^sup>L M (\<lambda>x. max 0 (- f x)) = real (\<integral>\<^sup>+x. max 0 (- f x) \<partial>M)"
  1769     by (subst integral_eq_nn_integral[symmetric]) (auto intro!: integrable_max f)
  1770   also have "(\<lambda>x. ereal (max 0 (f x))) = (\<lambda>x. max 0 (ereal (f x)))"
  1771     by (auto simp: max_def)
  1772   also have "(\<lambda>x. ereal (max 0 (- f x))) = (\<lambda>x. max 0 (- ereal (f x)))"
  1773     by (auto simp: max_def)
  1774   finally show ?thesis
  1775     unfolding nn_integral_max_0 .
  1776 qed
  1777 
  1778 lemma real_integrable_def:
  1779   "integrable M f \<longleftrightarrow> f \<in> borel_measurable M \<and>
  1780     (\<integral>\<^sup>+ x. ereal (f x) \<partial>M) \<noteq> \<infinity> \<and> (\<integral>\<^sup>+ x. ereal (- f x) \<partial>M) \<noteq> \<infinity>"
  1781   unfolding integrable_iff_bounded
  1782 proof (safe del: notI)
  1783   assume *: "(\<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M) < \<infinity>"
  1784   have "(\<integral>\<^sup>+ x. ereal (f x) \<partial>M) \<le> (\<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M)"
  1785     by (intro nn_integral_mono) auto
  1786   also note *
  1787   finally show "(\<integral>\<^sup>+ x. ereal (f x) \<partial>M) \<noteq> \<infinity>"
  1788     by simp
  1789   have "(\<integral>\<^sup>+ x. ereal (- f x) \<partial>M) \<le> (\<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M)"
  1790     by (intro nn_integral_mono) auto
  1791   also note *
  1792   finally show "(\<integral>\<^sup>+ x. ereal (- f x) \<partial>M) \<noteq> \<infinity>"
  1793     by simp
  1794 next
  1795   assume [measurable]: "f \<in> borel_measurable M"
  1796   assume fin: "(\<integral>\<^sup>+ x. ereal (f x) \<partial>M) \<noteq> \<infinity>" "(\<integral>\<^sup>+ x. ereal (- f x) \<partial>M) \<noteq> \<infinity>"
  1797   have "(\<integral>\<^sup>+ x. norm (f x) \<partial>M) = (\<integral>\<^sup>+ x. max 0 (ereal (f x)) + max 0 (ereal (- f x)) \<partial>M)"
  1798     by (intro nn_integral_cong) (auto simp: max_def)
  1799   also have"\<dots> = (\<integral>\<^sup>+ x. max 0 (ereal (f x)) \<partial>M) + (\<integral>\<^sup>+ x. max 0 (ereal (- f x)) \<partial>M)"
  1800     by (intro nn_integral_add) auto
  1801   also have "\<dots> < \<infinity>"
  1802     using fin by (auto simp: nn_integral_max_0)
  1803   finally show "(\<integral>\<^sup>+ x. norm (f x) \<partial>M) < \<infinity>" .
  1804 qed
  1805 
  1806 lemma integrableD[dest]:
  1807   assumes "integrable M f"
  1808   shows "f \<in> borel_measurable M" "(\<integral>\<^sup>+ x. ereal (f x) \<partial>M) \<noteq> \<infinity>" "(\<integral>\<^sup>+ x. ereal (- f x) \<partial>M) \<noteq> \<infinity>"
  1809   using assms unfolding real_integrable_def by auto
  1810 
  1811 lemma integrableE:
  1812   assumes "integrable M f"
  1813   obtains r q where
  1814     "(\<integral>\<^sup>+x. ereal (f x)\<partial>M) = ereal r"
  1815     "(\<integral>\<^sup>+x. ereal (-f x)\<partial>M) = ereal q"
  1816     "f \<in> borel_measurable M" "integral\<^sup>L M f = r - q"
  1817   using assms unfolding real_integrable_def real_lebesgue_integral_def[OF assms]
  1818   using nn_integral_nonneg[of M "\<lambda>x. ereal (f x)"]
  1819   using nn_integral_nonneg[of M "\<lambda>x. ereal (-f x)"]
  1820   by (cases rule: ereal2_cases[of "(\<integral>\<^sup>+x. ereal (-f x)\<partial>M)" "(\<integral>\<^sup>+x. ereal (f x)\<partial>M)"]) auto
  1821 
  1822 lemma integral_monotone_convergence_nonneg:
  1823   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1824   assumes i: "\<And>i. integrable M (f i)" and mono: "AE x in M. mono (\<lambda>n. f n x)"
  1825     and pos: "\<And>i. AE x in M. 0 \<le> f i x"
  1826     and lim: "AE x in M. (\<lambda>i. f i x) ----> u x"
  1827     and ilim: "(\<lambda>i. integral\<^sup>L M (f i)) ----> x"
  1828     and u: "u \<in> borel_measurable M"
  1829   shows "integrable M u"
  1830   and "integral\<^sup>L M u = x"
  1831 proof -
  1832   have "(\<integral>\<^sup>+ x. ereal (u x) \<partial>M) = (SUP n. (\<integral>\<^sup>+ x. ereal (f n x) \<partial>M))"
  1833   proof (subst nn_integral_monotone_convergence_SUP_AE[symmetric])
  1834     fix i
  1835     from mono pos show "AE x in M. ereal (f i x) \<le> ereal (f (Suc i) x) \<and> 0 \<le> ereal (f i x)"
  1836       by eventually_elim (auto simp: mono_def)
  1837     show "(\<lambda>x. ereal (f i x)) \<in> borel_measurable M"
  1838       using i by auto
  1839   next
  1840     show "(\<integral>\<^sup>+ x. ereal (u x) \<partial>M) = \<integral>\<^sup>+ x. (SUP i. ereal (f i x)) \<partial>M"
  1841       apply (rule nn_integral_cong_AE)
  1842       using lim mono
  1843       by eventually_elim (simp add: SUP_eq_LIMSEQ[THEN iffD2])
  1844   qed
  1845   also have "\<dots> = ereal x"
  1846     using mono i unfolding nn_integral_eq_integral[OF i pos]
  1847     by (subst SUP_eq_LIMSEQ) (auto simp: mono_def intro!: integral_mono_AE ilim)
  1848   finally have "(\<integral>\<^sup>+ x. ereal (u x) \<partial>M) = ereal x" .
  1849   moreover have "(\<integral>\<^sup>+ x. ereal (- u x) \<partial>M) = 0"
  1850   proof (subst nn_integral_0_iff_AE)
  1851     show "(\<lambda>x. ereal (- u x)) \<in> borel_measurable M"
  1852       using u by auto
  1853     from mono pos[of 0] lim show "AE x in M. ereal (- u x) \<le> 0"
  1854     proof eventually_elim
  1855       fix x assume "mono (\<lambda>n. f n x)" "0 \<le> f 0 x" "(\<lambda>i. f i x) ----> u x"
  1856       then show "ereal (- u x) \<le> 0"
  1857         using incseq_le[of "\<lambda>n. f n x" "u x" 0] by (simp add: mono_def incseq_def)
  1858     qed
  1859   qed
  1860   ultimately show "integrable M u" "integral\<^sup>L M u = x"
  1861     by (auto simp: real_integrable_def real_lebesgue_integral_def u)
  1862 qed
  1863 
  1864 lemma
  1865   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1866   assumes f: "\<And>i. integrable M (f i)" and mono: "AE x in M. mono (\<lambda>n. f n x)"
  1867   and lim: "AE x in M. (\<lambda>i. f i x) ----> u x"
  1868   and ilim: "(\<lambda>i. integral\<^sup>L M (f i)) ----> x"
  1869   and u: "u \<in> borel_measurable M"
  1870   shows integrable_monotone_convergence: "integrable M u"
  1871     and integral_monotone_convergence: "integral\<^sup>L M u = x"
  1872     and has_bochner_integral_monotone_convergence: "has_bochner_integral M u x"
  1873 proof -
  1874   have 1: "\<And>i. integrable M (\<lambda>x. f i x - f 0 x)"
  1875     using f by auto
  1876   have 2: "AE x in M. mono (\<lambda>n. f n x - f 0 x)"
  1877     using mono by (auto simp: mono_def le_fun_def)
  1878   have 3: "\<And>n. AE x in M. 0 \<le> f n x - f 0 x"
  1879     using mono by (auto simp: field_simps mono_def le_fun_def)
  1880   have 4: "AE x in M. (\<lambda>i. f i x - f 0 x) ----> u x - f 0 x"
  1881     using lim by (auto intro!: tendsto_diff)
  1882   have 5: "(\<lambda>i. (\<integral>x. f i x - f 0 x \<partial>M)) ----> x - integral\<^sup>L M (f 0)"
  1883     using f ilim by (auto intro!: tendsto_diff)
  1884   have 6: "(\<lambda>x. u x - f 0 x) \<in> borel_measurable M"
  1885     using f[of 0] u by auto
  1886   note diff = integral_monotone_convergence_nonneg[OF 1 2 3 4 5 6]
  1887   have "integrable M (\<lambda>x. (u x - f 0 x) + f 0 x)"
  1888     using diff(1) f by (rule integrable_add)
  1889   with diff(2) f show "integrable M u" "integral\<^sup>L M u = x"
  1890     by auto
  1891   then show "has_bochner_integral M u x"
  1892     by (metis has_bochner_integral_integrable)
  1893 qed
  1894 
  1895 lemma integral_norm_eq_0_iff:
  1896   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1897   assumes f[measurable]: "integrable M f"
  1898   shows "(\<integral>x. norm (f x) \<partial>M) = 0 \<longleftrightarrow> emeasure M {x\<in>space M. f x \<noteq> 0} = 0"
  1899 proof -
  1900   have "(\<integral>\<^sup>+x. norm (f x) \<partial>M) = (\<integral>x. norm (f x) \<partial>M)"
  1901     using f by (intro nn_integral_eq_integral integrable_norm) auto
  1902   then have "(\<integral>x. norm (f x) \<partial>M) = 0 \<longleftrightarrow> (\<integral>\<^sup>+x. norm (f x) \<partial>M) = 0"
  1903     by simp
  1904   also have "\<dots> \<longleftrightarrow> emeasure M {x\<in>space M. ereal (norm (f x)) \<noteq> 0} = 0"
  1905     by (intro nn_integral_0_iff) auto
  1906   finally show ?thesis
  1907     by simp
  1908 qed
  1909 
  1910 lemma integral_0_iff:
  1911   fixes f :: "'a \<Rightarrow> real"
  1912   shows "integrable M f \<Longrightarrow> (\<integral>x. abs (f x) \<partial>M) = 0 \<longleftrightarrow> emeasure M {x\<in>space M. f x \<noteq> 0} = 0"
  1913   using integral_norm_eq_0_iff[of M f] by simp
  1914 
  1915 lemma (in finite_measure) lebesgue_integral_const[simp]:
  1916   "(\<integral>x. a \<partial>M) = measure M (space M) *\<^sub>R a"
  1917   using integral_indicator[of "space M" M a]
  1918   by (simp del: integral_indicator integral_scaleR_left cong: integral_cong)
  1919 
  1920 lemma (in finite_measure) integrable_const[intro!, simp]: "integrable M (\<lambda>x. a)"
  1921   using integrable_indicator[of "space M" M a] by (simp cong: integrable_cong)
  1922 
  1923 lemma (in finite_measure) integrable_const_bound:
  1924   fixes f :: "'a \<Rightarrow> 'b::{banach,second_countable_topology}"
  1925   shows "AE x in M. norm (f x) \<le> B \<Longrightarrow> f \<in> borel_measurable M \<Longrightarrow> integrable M f"
  1926   apply (rule integrable_bound[OF integrable_const[of B], of f])
  1927   apply assumption
  1928   apply (cases "0 \<le> B")
  1929   apply auto
  1930   done
  1931 
  1932 lemma (in finite_measure) integral_less_AE:
  1933   fixes X Y :: "'a \<Rightarrow> real"
  1934   assumes int: "integrable M X" "integrable M Y"
  1935   assumes A: "(emeasure M) A \<noteq> 0" "A \<in> sets M" "AE x in M. x \<in> A \<longrightarrow> X x \<noteq> Y x"
  1936   assumes gt: "AE x in M. X x \<le> Y x"
  1937   shows "integral\<^sup>L M X < integral\<^sup>L M Y"
  1938 proof -
  1939   have "integral\<^sup>L M X \<le> integral\<^sup>L M Y"
  1940     using gt int by (intro integral_mono_AE) auto
  1941   moreover
  1942   have "integral\<^sup>L M X \<noteq> integral\<^sup>L M Y"
  1943   proof
  1944     assume eq: "integral\<^sup>L M X = integral\<^sup>L M Y"
  1945     have "integral\<^sup>L M (\<lambda>x. \<bar>Y x - X x\<bar>) = integral\<^sup>L M (\<lambda>x. Y x - X x)"
  1946       using gt int by (intro integral_cong_AE) auto
  1947     also have "\<dots> = 0"
  1948       using eq int by simp
  1949     finally have "(emeasure M) {x \<in> space M. Y x - X x \<noteq> 0} = 0"
  1950       using int by (simp add: integral_0_iff)
  1951     moreover
  1952     have "(\<integral>\<^sup>+x. indicator A x \<partial>M) \<le> (\<integral>\<^sup>+x. indicator {x \<in> space M. Y x - X x \<noteq> 0} x \<partial>M)"
  1953       using A by (intro nn_integral_mono_AE) auto
  1954     then have "(emeasure M) A \<le> (emeasure M) {x \<in> space M. Y x - X x \<noteq> 0}"
  1955       using int A by (simp add: integrable_def)
  1956     ultimately have "emeasure M A = 0"
  1957       using emeasure_nonneg[of M A] by simp
  1958     with `(emeasure M) A \<noteq> 0` show False by auto
  1959   qed
  1960   ultimately show ?thesis by auto
  1961 qed
  1962 
  1963 lemma (in finite_measure) integral_less_AE_space:
  1964   fixes X Y :: "'a \<Rightarrow> real"
  1965   assumes int: "integrable M X" "integrable M Y"
  1966   assumes gt: "AE x in M. X x < Y x" "emeasure M (space M) \<noteq> 0"
  1967   shows "integral\<^sup>L M X < integral\<^sup>L M Y"
  1968   using gt by (intro integral_less_AE[OF int, where A="space M"]) auto
  1969 
  1970 (* GENERALIZE to banach, sct *)
  1971 lemma integral_sums:
  1972   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1973   assumes integrable[measurable]: "\<And>i. integrable M (f i)"
  1974   and summable: "\<And>x. x \<in> space M \<Longrightarrow> summable (\<lambda>i. \<bar>f i x\<bar>)"
  1975   and sums: "summable (\<lambda>i. (\<integral>x. \<bar>f i x\<bar> \<partial>M))"
  1976   shows "integrable M (\<lambda>x. (\<Sum>i. f i x))" (is "integrable M ?S")
  1977   and "(\<lambda>i. integral\<^sup>L M (f i)) sums (\<integral>x. (\<Sum>i. f i x) \<partial>M)" (is ?integral)
  1978 proof -
  1979   have "\<forall>x\<in>space M. \<exists>w. (\<lambda>i. \<bar>f i x\<bar>) sums w"
  1980     using summable unfolding summable_def by auto
  1981   from bchoice[OF this]
  1982   obtain w where w: "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. \<bar>f i x\<bar>) sums w x" by auto
  1983   then have w_borel: "w \<in> borel_measurable M" unfolding sums_def
  1984     by (rule borel_measurable_LIMSEQ) auto
  1985 
  1986   let ?w = "\<lambda>y. if y \<in> space M then w y else 0"
  1987 
  1988   obtain x where abs_sum: "(\<lambda>i. (\<integral>x. \<bar>f i x\<bar> \<partial>M)) sums x"
  1989     using sums unfolding summable_def ..
  1990 
  1991   have 1: "\<And>n. integrable M (\<lambda>x. \<Sum>i<n. f i x)"
  1992     using integrable by auto
  1993 
  1994   have 2: "\<And>j. AE x in M. norm (\<Sum>i<j. f i x) \<le> ?w x"
  1995     using AE_space
  1996   proof eventually_elim
  1997     fix j x assume [simp]: "x \<in> space M"
  1998     have "\<bar>\<Sum>i<j. f i x\<bar> \<le> (\<Sum>i<j. \<bar>f i x\<bar>)" by (rule setsum_abs)
  1999     also have "\<dots> \<le> w x" using w[of x] setsum_le_suminf[of "\<lambda>i. \<bar>f i x\<bar>"] unfolding sums_iff by auto
  2000     finally show "norm (\<Sum>i<j. f i x) \<le> ?w x" by simp
  2001   qed
  2002 
  2003   have 3: "integrable M ?w"
  2004   proof (rule integrable_monotone_convergence(1))
  2005     let ?F = "\<lambda>n y. (\<Sum>i<n. \<bar>f i y\<bar>)"
  2006     let ?w' = "\<lambda>n y. if y \<in> space M then ?F n y else 0"
  2007     have "\<And>n. integrable M (?F n)"
  2008       using integrable by (auto intro!: integrable_abs)
  2009     thus "\<And>n. integrable M (?w' n)" by (simp cong: integrable_cong)
  2010     show "AE x in M. mono (\<lambda>n. ?w' n x)"
  2011       by (auto simp: mono_def le_fun_def intro!: setsum_mono2)
  2012     show "AE x in M. (\<lambda>n. ?w' n x) ----> ?w x"
  2013         using w by (simp_all add: tendsto_const sums_def)
  2014     have *: "\<And>n. integral\<^sup>L M (?w' n) = (\<Sum>i< n. (\<integral>x. \<bar>f i x\<bar> \<partial>M))"
  2015       using integrable by (simp add: integrable_abs cong: integral_cong)
  2016     from abs_sum
  2017     show "(\<lambda>i. integral\<^sup>L M (?w' i)) ----> x" unfolding * sums_def .
  2018   qed (simp add: w_borel measurable_If_set)
  2019 
  2020   from summable[THEN summable_rabs_cancel]
  2021   have 4: "AE x in M. (\<lambda>n. \<Sum>i<n. f i x) ----> (\<Sum>i. f i x)"
  2022     by (auto intro: summable_LIMSEQ)
  2023 
  2024   note int = integral_dominated_convergence(1,3)[OF _ _ 3 4 2]
  2025 
  2026   from int show "integrable M ?S" by simp
  2027 
  2028   show ?integral unfolding sums_def integral_setsum(1)[symmetric, OF integrable]
  2029     using int(2) by simp
  2030 qed
  2031 
  2032 lemma integrable_mult_indicator:
  2033   fixes f :: "'a \<Rightarrow> real"
  2034   shows "A \<in> sets M \<Longrightarrow> integrable M f \<Longrightarrow> integrable M (\<lambda>x. f x * indicator A x)"
  2035   by (rule integrable_bound[where f="\<lambda>x. \<bar>f x\<bar>"])
  2036      (auto intro: integrable_abs split: split_indicator)
  2037 
  2038 lemma tendsto_integral_at_top:
  2039   fixes f :: "real \<Rightarrow> real"
  2040   assumes M: "sets M = sets borel" and f[measurable]: "integrable M f"
  2041   shows "((\<lambda>y. \<integral> x. f x * indicator {.. y} x \<partial>M) ---> \<integral> x. f x \<partial>M) at_top"
  2042 proof -
  2043   have M_measure[simp]: "borel_measurable M = borel_measurable borel"
  2044     using M by (simp add: sets_eq_imp_space_eq measurable_def)
  2045   { fix f :: "real \<Rightarrow> real" assume f: "integrable M f" "\<And>x. 0 \<le> f x"
  2046     then have [measurable]: "f \<in> borel_measurable borel"
  2047       by (simp add: real_integrable_def)
  2048     have "((\<lambda>y. \<integral> x. f x * indicator {.. y} x \<partial>M) ---> \<integral> x. f x \<partial>M) at_top"
  2049     proof (rule tendsto_at_topI_sequentially)
  2050       have int: "\<And>n. integrable M (\<lambda>x. f x * indicator {.. n} x)"
  2051         by (rule integrable_mult_indicator) (auto simp: M f)
  2052       show "(\<lambda>n. \<integral> x. f x * indicator {..real n} x \<partial>M) ----> integral\<^sup>L M f"
  2053       proof (rule integral_dominated_convergence)
  2054         { fix x have "eventually (\<lambda>n. f x * indicator {..real n} x = f x) sequentially"
  2055             by (rule eventually_sequentiallyI[of "natceiling x"])
  2056                (auto split: split_indicator simp: natceiling_le_eq) }
  2057         from filterlim_cong[OF refl refl this]
  2058         show "AE x in M. (\<lambda>n. f x * indicator {..real n} x) ----> f x"
  2059           by (simp add: tendsto_const)
  2060         show "\<And>j. AE x in M. norm (f x * indicator {.. j} x) \<le> f x"
  2061           using f(2) by (intro AE_I2) (auto split: split_indicator)
  2062       qed (simp | fact)+
  2063       show "mono (\<lambda>y. \<integral> x. f x * indicator {..y} x \<partial>M)"
  2064         by (intro monoI integral_mono int) (auto split: split_indicator intro: f)
  2065     qed }
  2066   note nonneg = this
  2067   let ?P = "\<lambda>y. \<integral> x. max 0 (f x) * indicator {..y} x \<partial>M"
  2068   let ?N = "\<lambda>y. \<integral> x. max 0 (- f x) * indicator {..y} x \<partial>M"
  2069   let ?p = "integral\<^sup>L M (\<lambda>x. max 0 (f x))"
  2070   let ?n = "integral\<^sup>L M (\<lambda>x. max 0 (- f x))"
  2071   have "(?P ---> ?p) at_top" "(?N ---> ?n) at_top"
  2072     by (auto intro!: nonneg integrable_max f)
  2073   note tendsto_diff[OF this]
  2074   also have "(\<lambda>y. ?P y - ?N y) = (\<lambda>y. \<integral> x. f x * indicator {..y} x \<partial>M)"
  2075     by (subst integral_diff[symmetric])
  2076        (auto intro!: integrable_mult_indicator integrable_max f integral_cong
  2077              simp: M split: split_max)
  2078   also have "?p - ?n = integral\<^sup>L M f"
  2079     by (subst integral_diff[symmetric])
  2080        (auto intro!: integrable_max f integral_cong simp: M split: split_max)
  2081   finally show ?thesis .
  2082 qed
  2083 
  2084 lemma
  2085   fixes f :: "real \<Rightarrow> real"
  2086   assumes M: "sets M = sets borel"
  2087   assumes nonneg: "AE x in M. 0 \<le> f x"
  2088   assumes borel: "f \<in> borel_measurable borel"
  2089   assumes int: "\<And>y. integrable M (\<lambda>x. f x * indicator {.. y} x)"
  2090   assumes conv: "((\<lambda>y. \<integral> x. f x * indicator {.. y} x \<partial>M) ---> x) at_top"
  2091   shows has_bochner_integral_monotone_convergence_at_top: "has_bochner_integral M f x"
  2092     and integrable_monotone_convergence_at_top: "integrable M f"
  2093     and integral_monotone_convergence_at_top:"integral\<^sup>L M f = x"
  2094 proof -
  2095   from nonneg have "AE x in M. mono (\<lambda>n::nat. f x * indicator {..real n} x)"
  2096     by (auto split: split_indicator intro!: monoI)
  2097   { fix x have "eventually (\<lambda>n. f x * indicator {..real n} x = f x) sequentially"
  2098       by (rule eventually_sequentiallyI[of "natceiling x"])
  2099          (auto split: split_indicator simp: natceiling_le_eq) }
  2100   from filterlim_cong[OF refl refl this]
  2101   have "AE x in M. (\<lambda>i. f x * indicator {..real i} x) ----> f x"
  2102     by (simp add: tendsto_const)
  2103   have "(\<lambda>i. \<integral> x. f x * indicator {..real i} x \<partial>M) ----> x"
  2104     using conv filterlim_real_sequentially by (rule filterlim_compose)
  2105   have M_measure[simp]: "borel_measurable M = borel_measurable borel"
  2106     using M by (simp add: sets_eq_imp_space_eq measurable_def)
  2107   have "f \<in> borel_measurable M"
  2108     using borel by simp
  2109   show "has_bochner_integral M f x"
  2110     by (rule has_bochner_integral_monotone_convergence) fact+
  2111   then show "integrable M f" "integral\<^sup>L M f = x"
  2112     by (auto simp: _has_bochner_integral_iff)
  2113 qed
  2114 
  2115 
  2116 subsection {* Lebesgue integration on countable spaces *}
  2117 
  2118 lemma integral_on_countable:
  2119   fixes f :: "real \<Rightarrow> real"
  2120   assumes f: "f \<in> borel_measurable M"
  2121   and bij: "bij_betw enum S (f ` space M)"
  2122   and enum_zero: "enum ` (-S) \<subseteq> {0}"
  2123   and fin: "\<And>x. x \<noteq> 0 \<Longrightarrow> (emeasure M) (f -` {x} \<inter> space M) \<noteq> \<infinity>"
  2124   and abs_summable: "summable (\<lambda>r. \<bar>enum r * real ((emeasure M) (f -` {enum r} \<inter> space M))\<bar>)"
  2125   shows "integrable M f"
  2126   and "(\<lambda>r. enum r * real ((emeasure M) (f -` {enum r} \<inter> space M))) sums integral\<^sup>L M f" (is ?sums)
  2127 proof -
  2128   let ?A = "\<lambda>r. f -` {enum r} \<inter> space M"
  2129   let ?F = "\<lambda>r x. enum r * indicator (?A r) x"
  2130   have enum_eq: "\<And>r. enum r * real ((emeasure M) (?A r)) = integral\<^sup>L M (?F r)"
  2131     using f fin by (simp add: measure_def cong: disj_cong)
  2132 
  2133   { fix x assume "x \<in> space M"
  2134     hence "f x \<in> enum ` S" using bij unfolding bij_betw_def by auto
  2135     then obtain i where "i\<in>S" "enum i = f x" by auto
  2136     have F: "\<And>j. ?F j x = (if j = i then f x else 0)"
  2137     proof cases
  2138       fix j assume "j = i"
  2139       thus "?thesis j" using `x \<in> space M` `enum i = f x` by (simp add: indicator_def)
  2140     next
  2141       fix j assume "j \<noteq> i"
  2142       show "?thesis j" using bij `i \<in> S` `j \<noteq> i` `enum i = f x` enum_zero
  2143         by (cases "j \<in> S") (auto simp add: indicator_def bij_betw_def inj_on_def)
  2144     qed
  2145     hence F_abs: "\<And>j. \<bar>if j = i then f x else 0\<bar> = (if j = i then \<bar>f x\<bar> else 0)" by auto
  2146     have "(\<lambda>i. ?F i x) sums f x"
  2147          "(\<lambda>i. \<bar>?F i x\<bar>) sums \<bar>f x\<bar>"
  2148       by (auto intro!: sums_single simp: F F_abs) }
  2149   note F_sums_f = this(1) and F_abs_sums_f = this(2)
  2150 
  2151   have int_f: "integral\<^sup>L M f = (\<integral>x. (\<Sum>r. ?F r x) \<partial>M)" "integrable M f = integrable M (\<lambda>x. \<Sum>r. ?F r x)"
  2152     using F_sums_f by (auto intro!: integral_cong integrable_cong simp: sums_iff)
  2153 
  2154   { fix r
  2155     have "(\<integral>x. \<bar>?F r x\<bar> \<partial>M) = (\<integral>x. \<bar>enum r\<bar> * indicator (?A r) x \<partial>M)"
  2156       by (auto simp: indicator_def intro!: integral_cong)
  2157     also have "\<dots> = \<bar>enum r\<bar> * real ((emeasure M) (?A r))"
  2158       using f fin by (simp add: measure_def cong: disj_cong)
  2159     finally have "(\<integral>x. \<bar>?F r x\<bar> \<partial>M) = \<bar>enum r * real ((emeasure M) (?A r))\<bar>"
  2160       using f by (subst (2) abs_mult_pos[symmetric]) (auto intro!: real_of_ereal_pos measurable_sets) }
  2161   note int_abs_F = this
  2162 
  2163   have 1: "\<And>i. integrable M (\<lambda>x. ?F i x)"
  2164     using f fin by auto
  2165 
  2166   have 2: "\<And>x. x \<in> space M \<Longrightarrow> summable (\<lambda>i. \<bar>?F i x\<bar>)"
  2167     using F_abs_sums_f unfolding sums_iff by auto
  2168 
  2169   from integral_sums(2)[OF 1 2, unfolded int_abs_F, OF _ abs_summable]
  2170   show ?sums unfolding enum_eq int_f by simp
  2171 
  2172   from integral_sums(1)[OF 1 2, unfolded int_abs_F, OF _ abs_summable]
  2173   show "integrable M f" unfolding int_f by simp
  2174 qed
  2175 
  2176 subsection {* Product measure *}
  2177 
  2178 lemma (in sigma_finite_measure) borel_measurable_lebesgue_integrable[measurable (raw)]:
  2179   fixes f :: "_ \<Rightarrow> _ \<Rightarrow> _::{banach, second_countable_topology}"
  2180   assumes [measurable]: "split f \<in> borel_measurable (N \<Otimes>\<^sub>M M)"
  2181   shows "Measurable.pred N (\<lambda>x. integrable M (f x))"
  2182 proof -
  2183   have [simp]: "\<And>x. x \<in> space N \<Longrightarrow> integrable M (f x) \<longleftrightarrow> (\<integral>\<^sup>+y. norm (f x y) \<partial>M) < \<infinity>"
  2184     unfolding integrable_iff_bounded by simp
  2185   show ?thesis
  2186     by (simp cong: measurable_cong)
  2187 qed
  2188 
  2189 lemma (in sigma_finite_measure) measurable_measure[measurable (raw)]:
  2190   "(\<And>x. x \<in> space N \<Longrightarrow> A x \<subseteq> space M) \<Longrightarrow>
  2191     {x \<in> space (N \<Otimes>\<^sub>M M). snd x \<in> A (fst x)} \<in> sets (N \<Otimes>\<^sub>M M) \<Longrightarrow>
  2192     (\<lambda>x. measure M (A x)) \<in> borel_measurable N"
  2193   unfolding measure_def by (intro measurable_emeasure borel_measurable_real_of_ereal)
  2194 
  2195 lemma Collect_subset [simp]: "{x\<in>A. P x} \<subseteq> A" by auto
  2196 
  2197 lemma (in sigma_finite_measure) borel_measurable_lebesgue_integral[measurable (raw)]:
  2198   fixes f :: "_ \<Rightarrow> _ \<Rightarrow> _::{banach, second_countable_topology}"
  2199   assumes f[measurable]: "split f \<in> borel_measurable (N \<Otimes>\<^sub>M M)"
  2200   shows "(\<lambda>x. \<integral>y. f x y \<partial>M) \<in> borel_measurable N"
  2201 proof -
  2202   from borel_measurable_implies_sequence_metric[OF f, of 0] guess s ..
  2203   then have s: "\<And>i. simple_function (N \<Otimes>\<^sub>M M) (s i)"
  2204     "\<And>x y. x \<in> space N \<Longrightarrow> y \<in> space M \<Longrightarrow> (\<lambda>i. s i (x, y)) ----> f x y"
  2205     "\<And>i x y. x \<in> space N \<Longrightarrow> y \<in> space M \<Longrightarrow> norm (s i (x, y)) \<le> 2 * norm (f x y)"
  2206     by (auto simp: space_pair_measure norm_conv_dist)
  2207 
  2208   have [measurable]: "\<And>i. s i \<in> borel_measurable (N \<Otimes>\<^sub>M M)"
  2209     by (rule borel_measurable_simple_function) fact
  2210 
  2211   have "\<And>i. s i \<in> measurable (N \<Otimes>\<^sub>M M) (count_space UNIV)"
  2212     by (rule measurable_simple_function) fact
  2213 
  2214   def f' \<equiv> "\<lambda>i x. if integrable M (f x)
  2215     then simple_bochner_integral M (\<lambda>y. s i (x, y))
  2216     else (THE x. False)"
  2217 
  2218   { fix i x assume "x \<in> space N"
  2219     then have "simple_bochner_integral M (\<lambda>y. s i (x, y)) =
  2220       (\<Sum>z\<in>s i ` (space N \<times> space M). measure M {y \<in> space M. s i (x, y) = z} *\<^sub>R z)"
  2221       using s(1)[THEN simple_functionD(1)]
  2222       unfolding simple_bochner_integral_def
  2223       by (intro setsum_mono_zero_cong_left)
  2224          (auto simp: eq_commute space_pair_measure image_iff cong: conj_cong) }
  2225   note eq = this
  2226 
  2227   show ?thesis
  2228   proof (rule borel_measurable_LIMSEQ_metric)
  2229     fix i show "f' i \<in> borel_measurable N"
  2230       unfolding f'_def by (simp_all add: eq cong: measurable_cong if_cong)
  2231   next
  2232     fix x assume x: "x \<in> space N"
  2233     { assume int_f: "integrable M (f x)"
  2234       have int_2f: "integrable M (\<lambda>y. 2 * norm (f x y))"
  2235         by (intro integrable_norm integrable_mult_right int_f)
  2236       have "(\<lambda>i. integral\<^sup>L M (\<lambda>y. s i (x, y))) ----> integral\<^sup>L M (f x)"
  2237       proof (rule integral_dominated_convergence)
  2238         from int_f show "f x \<in> borel_measurable M" by auto
  2239         show "\<And>i. (\<lambda>y. s i (x, y)) \<in> borel_measurable M"
  2240           using x by simp
  2241         show "AE xa in M. (\<lambda>i. s i (x, xa)) ----> f x xa"
  2242           using x s(2) by auto
  2243         show "\<And>i. AE xa in M. norm (s i (x, xa)) \<le> 2 * norm (f x xa)"
  2244           using x s(3) by auto
  2245       qed fact
  2246       moreover
  2247       { fix i
  2248         have "simple_bochner_integrable M (\<lambda>y. s i (x, y))"
  2249         proof (rule simple_bochner_integrableI_bounded)
  2250           have "(\<lambda>y. s i (x, y)) ` space M \<subseteq> s i ` (space N \<times> space M)"
  2251             using x by auto
  2252           then show "simple_function M (\<lambda>y. s i (x, y))"
  2253             using simple_functionD(1)[OF s(1), of i] x
  2254             by (intro simple_function_borel_measurable)
  2255                (auto simp: space_pair_measure dest: finite_subset)
  2256           have "(\<integral>\<^sup>+ y. ereal (norm (s i (x, y))) \<partial>M) \<le> (\<integral>\<^sup>+ y. 2 * norm (f x y) \<partial>M)"
  2257             using x s by (intro nn_integral_mono) auto
  2258           also have "(\<integral>\<^sup>+ y. 2 * norm (f x y) \<partial>M) < \<infinity>"
  2259             using int_2f by (simp add: integrable_iff_bounded)
  2260           finally show "(\<integral>\<^sup>+ xa. ereal (norm (s i (x, xa))) \<partial>M) < \<infinity>" .
  2261         qed
  2262         then have "integral\<^sup>L M (\<lambda>y. s i (x, y)) = simple_bochner_integral M (\<lambda>y. s i (x, y))"
  2263           by (rule simple_bochner_integrable_eq_integral[symmetric]) }
  2264       ultimately have "(\<lambda>i. simple_bochner_integral M (\<lambda>y. s i (x, y))) ----> integral\<^sup>L M (f x)"
  2265         by simp }
  2266     then 
  2267     show "(\<lambda>i. f' i x) ----> integral\<^sup>L M (f x)"
  2268       unfolding f'_def
  2269       by (cases "integrable M (f x)") (simp_all add: not_integrable_integral_eq tendsto_const)
  2270   qed
  2271 qed
  2272 
  2273 lemma (in pair_sigma_finite) integrable_product_swap:
  2274   fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  2275   assumes "integrable (M1 \<Otimes>\<^sub>M M2) f"
  2276   shows "integrable (M2 \<Otimes>\<^sub>M M1) (\<lambda>(x,y). f (y,x))"
  2277 proof -
  2278   interpret Q: pair_sigma_finite M2 M1 by default
  2279   have *: "(\<lambda>(x,y). f (y,x)) = (\<lambda>x. f (case x of (x,y)\<Rightarrow>(y,x)))" by (auto simp: fun_eq_iff)
  2280   show ?thesis unfolding *
  2281     by (rule integrable_distr[OF measurable_pair_swap'])
  2282        (simp add: distr_pair_swap[symmetric] assms)
  2283 qed
  2284 
  2285 lemma (in pair_sigma_finite) integrable_product_swap_iff:
  2286   fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  2287   shows "integrable (M2 \<Otimes>\<^sub>M M1) (\<lambda>(x,y). f (y,x)) \<longleftrightarrow> integrable (M1 \<Otimes>\<^sub>M M2) f"
  2288 proof -
  2289   interpret Q: pair_sigma_finite M2 M1 by default
  2290   from Q.integrable_product_swap[of "\<lambda>(x,y). f (y,x)"] integrable_product_swap[of f]
  2291   show ?thesis by auto
  2292 qed
  2293 
  2294 lemma (in pair_sigma_finite) integral_product_swap:
  2295   fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  2296   assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
  2297   shows "(\<integral>(x,y). f (y,x) \<partial>(M2 \<Otimes>\<^sub>M M1)) = integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) f"
  2298 proof -
  2299   have *: "(\<lambda>(x,y). f (y,x)) = (\<lambda>x. f (case x of (x,y)\<Rightarrow>(y,x)))" by (auto simp: fun_eq_iff)
  2300   show ?thesis unfolding *
  2301     by (simp add: integral_distr[symmetric, OF measurable_pair_swap' f] distr_pair_swap[symmetric])
  2302 qed
  2303 
  2304 lemma (in pair_sigma_finite) emeasure_pair_measure_finite:
  2305   assumes A: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)" and finite: "emeasure (M1 \<Otimes>\<^sub>M M2) A < \<infinity>"
  2306   shows "AE x in M1. emeasure M2 {y\<in>space M2. (x, y) \<in> A} < \<infinity>"
  2307 proof -
  2308   from M2.emeasure_pair_measure_alt[OF A] finite
  2309   have "(\<integral>\<^sup>+ x. emeasure M2 (Pair x -` A) \<partial>M1) \<noteq> \<infinity>"
  2310     by simp
  2311   then have "AE x in M1. emeasure M2 (Pair x -` A) \<noteq> \<infinity>"
  2312     by (rule nn_integral_PInf_AE[rotated]) (intro M2.measurable_emeasure_Pair A)
  2313   moreover have "\<And>x. x \<in> space M1 \<Longrightarrow> Pair x -` A = {y\<in>space M2. (x, y) \<in> A}"
  2314     using sets.sets_into_space[OF A] by (auto simp: space_pair_measure)
  2315   ultimately show ?thesis by auto
  2316 qed
  2317 
  2318 lemma (in pair_sigma_finite) AE_integrable_fst':
  2319   fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  2320   assumes f[measurable]: "integrable (M1 \<Otimes>\<^sub>M M2) f"
  2321   shows "AE x in M1. integrable M2 (\<lambda>y. f (x, y))"
  2322 proof -
  2323   have "(\<integral>\<^sup>+x. (\<integral>\<^sup>+y. norm (f (x, y)) \<partial>M2) \<partial>M1) = (\<integral>\<^sup>+x. norm (f x) \<partial>(M1 \<Otimes>\<^sub>M M2))"
  2324     by (rule M2.nn_integral_fst) simp
  2325   also have "(\<integral>\<^sup>+x. norm (f x) \<partial>(M1 \<Otimes>\<^sub>M M2)) \<noteq> \<infinity>"
  2326     using f unfolding integrable_iff_bounded by simp
  2327   finally have "AE x in M1. (\<integral>\<^sup>+y. norm (f (x, y)) \<partial>M2) \<noteq> \<infinity>"
  2328     by (intro nn_integral_PInf_AE M2.borel_measurable_nn_integral )
  2329        (auto simp: measurable_split_conv)
  2330   with AE_space show ?thesis
  2331     by eventually_elim
  2332        (auto simp: integrable_iff_bounded measurable_compose[OF _ borel_measurable_integrable[OF f]])
  2333 qed
  2334 
  2335 lemma (in pair_sigma_finite) integrable_fst':
  2336   fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  2337   assumes f[measurable]: "integrable (M1 \<Otimes>\<^sub>M M2) f"
  2338   shows "integrable M1 (\<lambda>x. \<integral>y. f (x, y) \<partial>M2)"
  2339   unfolding integrable_iff_bounded
  2340 proof
  2341   show "(\<lambda>x. \<integral> y. f (x, y) \<partial>M2) \<in> borel_measurable M1"
  2342     by (rule M2.borel_measurable_lebesgue_integral) simp
  2343   have "(\<integral>\<^sup>+ x. ereal (norm (\<integral> y. f (x, y) \<partial>M2)) \<partial>M1) \<le> (\<integral>\<^sup>+x. (\<integral>\<^sup>+y. norm (f (x, y)) \<partial>M2) \<partial>M1)"
  2344     using AE_integrable_fst'[OF f] by (auto intro!: nn_integral_mono_AE integral_norm_bound_ereal)
  2345   also have "(\<integral>\<^sup>+x. (\<integral>\<^sup>+y. norm (f (x, y)) \<partial>M2) \<partial>M1) = (\<integral>\<^sup>+x. norm (f x) \<partial>(M1 \<Otimes>\<^sub>M M2))"
  2346     by (rule M2.nn_integral_fst) simp
  2347   also have "(\<integral>\<^sup>+x. norm (f x) \<partial>(M1 \<Otimes>\<^sub>M M2)) < \<infinity>"
  2348     using f unfolding integrable_iff_bounded by simp
  2349   finally show "(\<integral>\<^sup>+ x. ereal (norm (\<integral> y. f (x, y) \<partial>M2)) \<partial>M1) < \<infinity>" .
  2350 qed
  2351 
  2352 lemma (in pair_sigma_finite) integral_fst':
  2353   fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  2354   assumes f: "integrable (M1 \<Otimes>\<^sub>M M2) f"
  2355   shows "(\<integral>x. (\<integral>y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) f"
  2356 using f proof induct
  2357   case (base A c)
  2358   have A[measurable]: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)" by fact
  2359 
  2360   have eq: "\<And>x y. x \<in> space M1 \<Longrightarrow> indicator A (x, y) = indicator {y\<in>space M2. (x, y) \<in> A} y"
  2361     using sets.sets_into_space[OF A] by (auto split: split_indicator simp: space_pair_measure)
  2362 
  2363   have int_A: "integrable (M1 \<Otimes>\<^sub>M M2) (indicator A :: _ \<Rightarrow> real)"
  2364     using base by (rule integrable_real_indicator)
  2365 
  2366   have "(\<integral> x. \<integral> y. indicator A (x, y) *\<^sub>R c \<partial>M2 \<partial>M1) = (\<integral>x. measure M2 {y\<in>space M2. (x, y) \<in> A} *\<^sub>R c \<partial>M1)"
  2367   proof (intro integral_cong_AE, simp, simp)
  2368     from AE_integrable_fst'[OF int_A] AE_space
  2369     show "AE x in M1. (\<integral>y. indicator A (x, y) *\<^sub>R c \<partial>M2) = measure M2 {y\<in>space M2. (x, y) \<in> A} *\<^sub>R c"
  2370       by eventually_elim (simp add: eq integrable_indicator_iff)
  2371   qed
  2372   also have "\<dots> = measure (M1 \<Otimes>\<^sub>M M2) A *\<^sub>R c"
  2373   proof (subst integral_scaleR_left)
  2374     have "(\<integral>\<^sup>+x. ereal (measure M2 {y \<in> space M2. (x, y) \<in> A}) \<partial>M1) =
  2375       (\<integral>\<^sup>+x. emeasure M2 {y \<in> space M2. (x, y) \<in> A} \<partial>M1)"
  2376       using emeasure_pair_measure_finite[OF base]
  2377       by (intro nn_integral_cong_AE, eventually_elim) (simp add: emeasure_eq_ereal_measure)
  2378     also have "\<dots> = emeasure (M1 \<Otimes>\<^sub>M M2) A"
  2379       using sets.sets_into_space[OF A]
  2380       by (subst M2.emeasure_pair_measure_alt)
  2381          (auto intro!: nn_integral_cong arg_cong[where f="emeasure M2"] simp: space_pair_measure)
  2382     finally have *: "(\<integral>\<^sup>+x. ereal (measure M2 {y \<in> space M2. (x, y) \<in> A}) \<partial>M1) = emeasure (M1 \<Otimes>\<^sub>M M2) A" .
  2383 
  2384     from base * show "integrable M1 (\<lambda>x. measure M2 {y \<in> space M2. (x, y) \<in> A})"
  2385       by (simp add: measure_nonneg integrable_iff_bounded)
  2386     then have "(\<integral>x. measure M2 {y \<in> space M2. (x, y) \<in> A} \<partial>M1) = 
  2387       (\<integral>\<^sup>+x. ereal (measure M2 {y \<in> space M2. (x, y) \<in> A}) \<partial>M1)"
  2388       by (rule nn_integral_eq_integral[symmetric]) (simp add: measure_nonneg)
  2389     also note *
  2390     finally show "(\<integral>x. measure M2 {y \<in> space M2. (x, y) \<in> A} \<partial>M1) *\<^sub>R c = measure (M1 \<Otimes>\<^sub>M M2) A *\<^sub>R c"
  2391       using base by (simp add: emeasure_eq_ereal_measure)
  2392   qed
  2393   also have "\<dots> = (\<integral> a. indicator A a *\<^sub>R c \<partial>(M1 \<Otimes>\<^sub>M M2))"
  2394     using base by simp
  2395   finally show ?case .
  2396 next
  2397   case (add f g)
  2398   then have [measurable]: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)" "g \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
  2399     by auto
  2400   have "(\<integral> x. \<integral> y. f (x, y) + g (x, y) \<partial>M2 \<partial>M1) = 
  2401     (\<integral> x. (\<integral> y. f (x, y) \<partial>M2) + (\<integral> y. g (x, y) \<partial>M2) \<partial>M1)"
  2402     apply (rule integral_cong_AE)
  2403     apply simp_all
  2404     using AE_integrable_fst'[OF add(1)] AE_integrable_fst'[OF add(3)]
  2405     apply eventually_elim
  2406     apply simp
  2407     done 
  2408   also have "\<dots> = (\<integral> x. f x \<partial>(M1 \<Otimes>\<^sub>M M2)) + (\<integral> x. g x \<partial>(M1 \<Otimes>\<^sub>M M2))"
  2409     using integrable_fst'[OF add(1)] integrable_fst'[OF add(3)] add(2,4) by simp
  2410   finally show ?case
  2411     using add by simp
  2412 next
  2413   case (lim f s)
  2414   then have [measurable]: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)" "\<And>i. s i \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
  2415     by auto
  2416   
  2417   show ?case
  2418   proof (rule LIMSEQ_unique)
  2419     show "(\<lambda>i. integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) (s i)) ----> integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) f"
  2420     proof (rule integral_dominated_convergence)
  2421       show "integrable (M1 \<Otimes>\<^sub>M M2) (\<lambda>x. 2 * norm (f x))"
  2422         using lim(5) by (auto intro: integrable_norm)
  2423     qed (insert lim, auto)
  2424     have "(\<lambda>i. \<integral> x. \<integral> y. s i (x, y) \<partial>M2 \<partial>M1) ----> \<integral> x. \<integral> y. f (x, y) \<partial>M2 \<partial>M1"
  2425     proof (rule integral_dominated_convergence)
  2426       have "AE x in M1. \<forall>i. integrable M2 (\<lambda>y. s i (x, y))"
  2427         unfolding AE_all_countable using AE_integrable_fst'[OF lim(1)] ..
  2428       with AE_space AE_integrable_fst'[OF lim(5)]
  2429       show "AE x in M1. (\<lambda>i. \<integral> y. s i (x, y) \<partial>M2) ----> \<integral> y. f (x, y) \<partial>M2"
  2430       proof eventually_elim
  2431         fix x assume x: "x \<in> space M1" and
  2432           s: "\<forall>i. integrable M2 (\<lambda>y. s i (x, y))" and f: "integrable M2 (\<lambda>y. f (x, y))"
  2433         show "(\<lambda>i. \<integral> y. s i (x, y) \<partial>M2) ----> \<integral> y. f (x, y) \<partial>M2"
  2434         proof (rule integral_dominated_convergence)
  2435           show "integrable M2 (\<lambda>y. 2 * norm (f (x, y)))"
  2436              using f by (auto intro: integrable_norm)
  2437           show "AE xa in M2. (\<lambda>i. s i (x, xa)) ----> f (x, xa)"
  2438             using x lim(3) by (auto simp: space_pair_measure)
  2439           show "\<And>i. AE xa in M2. norm (s i (x, xa)) \<le> 2 * norm (f (x, xa))"
  2440             using x lim(4) by (auto simp: space_pair_measure)
  2441         qed (insert x, measurable)
  2442       qed
  2443       show "integrable M1 (\<lambda>x. (\<integral> y. 2 * norm (f (x, y)) \<partial>M2))"
  2444         by (intro integrable_mult_right integrable_norm integrable_fst' lim)
  2445       fix i show "AE x in M1. norm (\<integral> y. s i (x, y) \<partial>M2) \<le> (\<integral> y. 2 * norm (f (x, y)) \<partial>M2)"
  2446         using AE_space AE_integrable_fst'[OF lim(1), of i] AE_integrable_fst'[OF lim(5)]
  2447       proof eventually_elim 
  2448         fix x assume x: "x \<in> space M1"
  2449           and s: "integrable M2 (\<lambda>y. s i (x, y))" and f: "integrable M2 (\<lambda>y. f (x, y))"
  2450         from s have "norm (\<integral> y. s i (x, y) \<partial>M2) \<le> (\<integral>\<^sup>+y. norm (s i (x, y)) \<partial>M2)"
  2451           by (rule integral_norm_bound_ereal)
  2452         also have "\<dots> \<le> (\<integral>\<^sup>+y. 2 * norm (f (x, y)) \<partial>M2)"
  2453           using x lim by (auto intro!: nn_integral_mono simp: space_pair_measure)
  2454         also have "\<dots> = (\<integral>y. 2 * norm (f (x, y)) \<partial>M2)"
  2455           using f by (intro nn_integral_eq_integral) auto
  2456         finally show "norm (\<integral> y. s i (x, y) \<partial>M2) \<le> (\<integral> y. 2 * norm (f (x, y)) \<partial>M2)"
  2457           by simp
  2458       qed
  2459     qed simp_all
  2460     then show "(\<lambda>i. integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) (s i)) ----> \<integral> x. \<integral> y. f (x, y) \<partial>M2 \<partial>M1"
  2461       using lim by simp
  2462   qed
  2463 qed
  2464 
  2465 lemma (in pair_sigma_finite)
  2466   fixes f :: "_ \<Rightarrow> _ \<Rightarrow> _::{banach, second_countable_topology}"
  2467   assumes f: "integrable (M1 \<Otimes>\<^sub>M M2) (split f)"
  2468   shows AE_integrable_fst: "AE x in M1. integrable M2 (\<lambda>y. f x y)" (is "?AE")
  2469     and integrable_fst: "integrable M1 (\<lambda>x. \<integral>y. f x y \<partial>M2)" (is "?INT")
  2470     and integral_fst: "(\<integral>x. (\<integral>y. f x y \<partial>M2) \<partial>M1) = integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) (\<lambda>(x, y). f x y)" (is "?EQ")
  2471   using AE_integrable_fst'[OF f] integrable_fst'[OF f] integral_fst'[OF f] by auto
  2472 
  2473 lemma (in pair_sigma_finite)
  2474   fixes f :: "_ \<Rightarrow> _ \<Rightarrow> _::{banach, second_countable_topology}"
  2475   assumes f[measurable]: "integrable (M1 \<Otimes>\<^sub>M M2) (split f)"
  2476   shows AE_integrable_snd: "AE y in M2. integrable M1 (\<lambda>x. f x y)" (is "?AE")
  2477     and integrable_snd: "integrable M2 (\<lambda>y. \<integral>x. f x y \<partial>M1)" (is "?INT")
  2478     and integral_snd: "(\<integral>y. (\<integral>x. f x y \<partial>M1) \<partial>M2) = integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) (split f)" (is "?EQ")
  2479 proof -
  2480   interpret Q: pair_sigma_finite M2 M1 by default
  2481   have Q_int: "integrable (M2 \<Otimes>\<^sub>M M1) (\<lambda>(x, y). f y x)"
  2482     using f unfolding integrable_product_swap_iff[symmetric] by simp
  2483   show ?AE  using Q.AE_integrable_fst'[OF Q_int] by simp
  2484   show ?INT using Q.integrable_fst'[OF Q_int] by simp
  2485   show ?EQ using Q.integral_fst'[OF Q_int]
  2486     using integral_product_swap[of "split f"] by simp
  2487 qed
  2488 
  2489 lemma (in pair_sigma_finite) Fubini_integral:
  2490   fixes f :: "_ \<Rightarrow> _ \<Rightarrow> _ :: {banach, second_countable_topology}"
  2491   assumes f: "integrable (M1 \<Otimes>\<^sub>M M2) (split f)"
  2492   shows "(\<integral>y. (\<integral>x. f x y \<partial>M1) \<partial>M2) = (\<integral>x. (\<integral>y. f x y \<partial>M2) \<partial>M1)"
  2493   unfolding integral_snd[OF assms] integral_fst[OF assms] ..
  2494 
  2495 lemma (in product_sigma_finite) product_integral_singleton:
  2496   fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  2497   shows "f \<in> borel_measurable (M i) \<Longrightarrow> (\<integral>x. f (x i) \<partial>Pi\<^sub>M {i} M) = integral\<^sup>L (M i) f"
  2498   apply (subst distr_singleton[symmetric])
  2499   apply (subst integral_distr)
  2500   apply simp_all
  2501   done
  2502 
  2503 lemma (in product_sigma_finite) product_integral_fold:
  2504   fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  2505   assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
  2506   and f: "integrable (Pi\<^sub>M (I \<union> J) M) f"
  2507   shows "integral\<^sup>L (Pi\<^sub>M (I \<union> J) M) f = (\<integral>x. (\<integral>y. f (merge I J (x, y)) \<partial>Pi\<^sub>M J M) \<partial>Pi\<^sub>M I M)"
  2508 proof -
  2509   interpret I: finite_product_sigma_finite M I by default fact
  2510   interpret J: finite_product_sigma_finite M J by default fact
  2511   have "finite (I \<union> J)" using fin by auto
  2512   interpret IJ: finite_product_sigma_finite M "I \<union> J" by default fact
  2513   interpret P: pair_sigma_finite "Pi\<^sub>M I M" "Pi\<^sub>M J M" by default
  2514   let ?M = "merge I J"
  2515   let ?f = "\<lambda>x. f (?M x)"
  2516   from f have f_borel: "f \<in> borel_measurable (Pi\<^sub>M (I \<union> J) M)"
  2517     by auto
  2518   have P_borel: "(\<lambda>x. f (merge I J x)) \<in> borel_measurable (Pi\<^sub>M I M \<Otimes>\<^sub>M Pi\<^sub>M J M)"
  2519     using measurable_comp[OF measurable_merge f_borel] by (simp add: comp_def)
  2520   have f_int: "integrable (Pi\<^sub>M I M \<Otimes>\<^sub>M Pi\<^sub>M J M) ?f"
  2521     by (rule integrable_distr[OF measurable_merge]) (simp add: distr_merge[OF IJ fin] f)
  2522   show ?thesis
  2523     apply (subst distr_merge[symmetric, OF IJ fin])
  2524     apply (subst integral_distr[OF measurable_merge f_borel])
  2525     apply (subst P.integral_fst'[symmetric, OF f_int])
  2526     apply simp
  2527     done
  2528 qed
  2529 
  2530 lemma (in product_sigma_finite) product_integral_insert:
  2531   fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  2532   assumes I: "finite I" "i \<notin> I"
  2533     and f: "integrable (Pi\<^sub>M (insert i I) M) f"
  2534   shows "integral\<^sup>L (Pi\<^sub>M (insert i I) M) f = (\<integral>x. (\<integral>y. f (x(i:=y)) \<partial>M i) \<partial>Pi\<^sub>M I M)"
  2535 proof -
  2536   have "integral\<^sup>L (Pi\<^sub>M (insert i I) M) f = integral\<^sup>L (Pi\<^sub>M (I \<union> {i}) M) f"
  2537     by simp
  2538   also have "\<dots> = (\<integral>x. (\<integral>y. f (merge I {i} (x,y)) \<partial>Pi\<^sub>M {i} M) \<partial>Pi\<^sub>M I M)"
  2539     using f I by (intro product_integral_fold) auto
  2540   also have "\<dots> = (\<integral>x. (\<integral>y. f (x(i := y)) \<partial>M i) \<partial>Pi\<^sub>M I M)"
  2541   proof (rule integral_cong[OF refl], subst product_integral_singleton[symmetric])
  2542     fix x assume x: "x \<in> space (Pi\<^sub>M I M)"
  2543     have f_borel: "f \<in> borel_measurable (Pi\<^sub>M (insert i I) M)"
  2544       using f by auto
  2545     show "(\<lambda>y. f (x(i := y))) \<in> borel_measurable (M i)"
  2546       using measurable_comp[OF measurable_component_update f_borel, OF x `i \<notin> I`]
  2547       unfolding comp_def .
  2548     from x I show "(\<integral> y. f (merge I {i} (x,y)) \<partial>Pi\<^sub>M {i} M) = (\<integral> xa. f (x(i := xa i)) \<partial>Pi\<^sub>M {i} M)"
  2549       by (auto intro!: integral_cong arg_cong[where f=f] simp: merge_def space_PiM extensional_def PiE_def)
  2550   qed
  2551   finally show ?thesis .
  2552 qed
  2553 
  2554 lemma (in product_sigma_finite) product_integrable_setprod:
  2555   fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> _::{real_normed_field,banach,second_countable_topology}"
  2556   assumes [simp]: "finite I" and integrable: "\<And>i. i \<in> I \<Longrightarrow> integrable (M i) (f i)"
  2557   shows "integrable (Pi\<^sub>M I M) (\<lambda>x. (\<Prod>i\<in>I. f i (x i)))" (is "integrable _ ?f")
  2558 proof (unfold integrable_iff_bounded, intro conjI)
  2559   interpret finite_product_sigma_finite M I by default fact
  2560   show "?f \<in> borel_measurable (Pi\<^sub>M I M)"
  2561     using assms by simp
  2562   have "(\<integral>\<^sup>+ x. ereal (norm (\<Prod>i\<in>I. f i (x i))) \<partial>Pi\<^sub>M I M) = 
  2563       (\<integral>\<^sup>+ x. (\<Prod>i\<in>I. ereal (norm (f i (x i)))) \<partial>Pi\<^sub>M I M)"
  2564     by (simp add: setprod_norm setprod_ereal)
  2565   also have "\<dots> = (\<Prod>i\<in>I. \<integral>\<^sup>+ x. ereal (norm (f i x)) \<partial>M i)"
  2566     using assms by (intro product_nn_integral_setprod) auto
  2567   also have "\<dots> < \<infinity>"
  2568     using integrable by (simp add: setprod_PInf nn_integral_nonneg integrable_iff_bounded)
  2569   finally show "(\<integral>\<^sup>+ x. ereal (norm (\<Prod>i\<in>I. f i (x i))) \<partial>Pi\<^sub>M I M) < \<infinity>" .
  2570 qed
  2571 
  2572 lemma (in product_sigma_finite) product_integral_setprod:
  2573   fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> _::{real_normed_field,banach,second_countable_topology}"
  2574   assumes "finite I" and integrable: "\<And>i. i \<in> I \<Longrightarrow> integrable (M i) (f i)"
  2575   shows "(\<integral>x. (\<Prod>i\<in>I. f i (x i)) \<partial>Pi\<^sub>M I M) = (\<Prod>i\<in>I. integral\<^sup>L (M i) (f i))"
  2576 using assms proof induct
  2577   case empty
  2578   interpret finite_measure "Pi\<^sub>M {} M"
  2579     by rule (simp add: space_PiM)
  2580   show ?case by (simp add: space_PiM measure_def)
  2581 next
  2582   case (insert i I)
  2583   then have iI: "finite (insert i I)" by auto
  2584   then have prod: "\<And>J. J \<subseteq> insert i I \<Longrightarrow>
  2585     integrable (Pi\<^sub>M J M) (\<lambda>x. (\<Prod>i\<in>J. f i (x i)))"
  2586     by (intro product_integrable_setprod insert(4)) (auto intro: finite_subset)
  2587   interpret I: finite_product_sigma_finite M I by default fact
  2588   have *: "\<And>x y. (\<Prod>j\<in>I. f j (if j = i then y else x j)) = (\<Prod>j\<in>I. f j (x j))"
  2589     using `i \<notin> I` by (auto intro!: setprod_cong)
  2590   show ?case
  2591     unfolding product_integral_insert[OF insert(1,2) prod[OF subset_refl]]
  2592     by (simp add: * insert prod subset_insertI)
  2593 qed
  2594 
  2595 lemma integrable_subalgebra:
  2596   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  2597   assumes borel: "f \<in> borel_measurable N"
  2598   and N: "sets N \<subseteq> sets M" "space N = space M" "\<And>A. A \<in> sets N \<Longrightarrow> emeasure N A = emeasure M A"
  2599   shows "integrable N f \<longleftrightarrow> integrable M f" (is ?P)
  2600 proof -
  2601   have "f \<in> borel_measurable M"
  2602     using assms by (auto simp: measurable_def)
  2603   with assms show ?thesis
  2604     using assms by (auto simp: integrable_iff_bounded nn_integral_subalgebra)
  2605 qed
  2606 
  2607 lemma integral_subalgebra:
  2608   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  2609   assumes borel: "f \<in> borel_measurable N"
  2610   and N: "sets N \<subseteq> sets M" "space N = space M" "\<And>A. A \<in> sets N \<Longrightarrow> emeasure N A = emeasure M A"
  2611   shows "integral\<^sup>L N f = integral\<^sup>L M f"
  2612 proof cases
  2613   assume "integrable N f"
  2614   then show ?thesis
  2615   proof induct
  2616     case base with assms show ?case by (auto simp: subset_eq measure_def)
  2617   next
  2618     case (add f g)
  2619     then have "(\<integral> a. f a + g a \<partial>N) = integral\<^sup>L M f + integral\<^sup>L M g"
  2620       by simp
  2621     also have "\<dots> = (\<integral> a. f a + g a \<partial>M)"
  2622       using add integrable_subalgebra[OF _ N, of f] integrable_subalgebra[OF _ N, of g] by simp
  2623     finally show ?case .
  2624   next
  2625     case (lim f s)
  2626     then have M: "\<And>i. integrable M (s i)" "integrable M f"
  2627       using integrable_subalgebra[OF _ N, of f] integrable_subalgebra[OF _ N, of "s i" for i] by simp_all
  2628     show ?case
  2629     proof (intro LIMSEQ_unique)
  2630       show "(\<lambda>i. integral\<^sup>L N (s i)) ----> integral\<^sup>L N f"
  2631         apply (rule integral_dominated_convergence[where w="\<lambda>x. 2 * norm (f x)"])
  2632         using lim
  2633         apply auto
  2634         done
  2635       show "(\<lambda>i. integral\<^sup>L N (s i)) ----> integral\<^sup>L M f"
  2636         unfolding lim
  2637         apply (rule integral_dominated_convergence[where w="\<lambda>x. 2 * norm (f x)"])
  2638         using lim M N(2)
  2639         apply auto
  2640         done
  2641     qed
  2642   qed
  2643 qed (simp add: not_integrable_integral_eq integrable_subalgebra[OF assms])
  2644 
  2645 hide_const simple_bochner_integral
  2646 hide_const simple_bochner_integrable
  2647 
  2648 end