src/HOL/Probability/Lebesgue_Measure.thy
author haftmann
Fri Jul 04 20:18:47 2014 +0200 (2014-07-04)
changeset 57512 cc97b347b301
parent 57447 87429bdecad5
child 57514 bdc2c6b40bf2
permissions -rw-r--r--
reduced name variants for assoc and commute on plus and mult
     1 (*  Title:      HOL/Probability/Lebesgue_Measure.thy
     2     Author:     Johannes Hölzl, TU München
     3     Author:     Robert Himmelmann, TU München
     4     Author:     Jeremy Avigad
     5     Author:     Luke Serafin
     6 *)
     7 
     8 header {* Lebsegue measure *}
     9 
    10 theory Lebesgue_Measure
    11   imports Finite_Product_Measure Bochner_Integration Caratheodory
    12 begin
    13 
    14 subsection {* Every right continuous and nondecreasing function gives rise to a measure *}
    15 
    16 definition interval_measure :: "(real \<Rightarrow> real) \<Rightarrow> real measure" where
    17   "interval_measure F = extend_measure UNIV {(a, b). a \<le> b} (\<lambda>(a, b). {a <.. b}) (\<lambda>(a, b). ereal (F b - F a))"
    18 
    19 lemma emeasure_interval_measure_Ioc:
    20   assumes "a \<le> b"
    21   assumes mono_F: "\<And>x y. x \<le> y \<Longrightarrow> F x \<le> F y"
    22   assumes right_cont_F : "\<And>a. continuous (at_right a) F" 
    23   shows "emeasure (interval_measure F) {a <.. b} = F b - F a"
    24 proof (rule extend_measure_caratheodory_pair[OF interval_measure_def `a \<le> b`])
    25   show "semiring_of_sets UNIV {{a<..b} |a b :: real. a \<le> b}"
    26   proof (unfold_locales, safe)
    27     fix a b c d :: real assume *: "a \<le> b" "c \<le> d"
    28     then show "\<exists>C\<subseteq>{{a<..b} |a b. a \<le> b}. finite C \<and> disjoint C \<and> {a<..b} - {c<..d} = \<Union>C"
    29     proof cases
    30       let ?C = "{{a<..b}}"
    31       assume "b < c \<or> d \<le> a \<or> d \<le> c"
    32       with * have "?C \<subseteq> {{a<..b} |a b. a \<le> b} \<and> finite ?C \<and> disjoint ?C \<and> {a<..b} - {c<..d} = \<Union>?C"
    33         by (auto simp add: disjoint_def)
    34       thus ?thesis ..
    35     next
    36       let ?C = "{{a<..c}, {d<..b}}"
    37       assume "\<not> (b < c \<or> d \<le> a \<or> d \<le> c)"
    38       with * have "?C \<subseteq> {{a<..b} |a b. a \<le> b} \<and> finite ?C \<and> disjoint ?C \<and> {a<..b} - {c<..d} = \<Union>?C"
    39         by (auto simp add: disjoint_def Ioc_inj) (metis linear)+
    40       thus ?thesis ..
    41     qed
    42   qed (auto simp: Ioc_inj, metis linear)
    43   
    44 next
    45   fix l r :: "nat \<Rightarrow> real" and a b :: real
    46   assume l_r[simp]: "\<And>n. l n \<le> r n" and "a \<le> b" and disj: "disjoint_family (\<lambda>n. {l n<..r n})" 
    47   assume lr_eq_ab: "(\<Union>i. {l i<..r i}) = {a<..b}"
    48 
    49   have [intro, simp]: "\<And>a b. a \<le> b \<Longrightarrow> 0 \<le> F b - F a"
    50     by (auto intro!: l_r mono_F simp: diff_le_iff)
    51 
    52   { fix S :: "nat set" assume "finite S"
    53     moreover note `a \<le> b`
    54     moreover have "\<And>i. i \<in> S \<Longrightarrow> {l i <.. r i} \<subseteq> {a <.. b}"
    55       unfolding lr_eq_ab[symmetric] by auto
    56     ultimately have "(\<Sum>i\<in>S. F (r i) - F (l i)) \<le> F b - F a"
    57     proof (induction S arbitrary: a rule: finite_psubset_induct)
    58       case (psubset S)
    59       show ?case
    60       proof cases
    61         assume "\<exists>i\<in>S. l i < r i"
    62         with `finite S` have "Min (l ` {i\<in>S. l i < r i}) \<in> l ` {i\<in>S. l i < r i}"
    63           by (intro Min_in) auto
    64         then obtain m where m: "m \<in> S" "l m < r m" "l m = Min (l ` {i\<in>S. l i < r i})"
    65           by fastforce
    66 
    67         have "(\<Sum>i\<in>S. F (r i) - F (l i)) = (F (r m) - F (l m)) + (\<Sum>i\<in>S - {m}. F (r i) - F (l i))"
    68           using m psubset by (intro setsum.remove) auto
    69         also have "(\<Sum>i\<in>S - {m}. F (r i) - F (l i)) \<le> F b - F (r m)"
    70         proof (intro psubset.IH)
    71           show "S - {m} \<subset> S"
    72             using `m\<in>S` by auto
    73           show "r m \<le> b"
    74             using psubset.prems(2)[OF `m\<in>S`] `l m < r m` by auto
    75         next
    76           fix i assume "i \<in> S - {m}"
    77           then have i: "i \<in> S" "i \<noteq> m" by auto
    78           { assume i': "l i < r i" "l i < r m"
    79             moreover with `finite S` i m have "l m \<le> l i"
    80               by auto
    81             ultimately have "{l i <.. r i} \<inter> {l m <.. r m} \<noteq> {}"
    82               by auto
    83             then have False
    84               using disjoint_family_onD[OF disj, of i m] i by auto }
    85           then have "l i \<noteq> r i \<Longrightarrow> r m \<le> l i"
    86             unfolding not_less[symmetric] using l_r[of i] by auto
    87           then show "{l i <.. r i} \<subseteq> {r m <.. b}"
    88             using psubset.prems(2)[OF `i\<in>S`] by auto
    89         qed
    90         also have "F (r m) - F (l m) \<le> F (r m) - F a"
    91           using psubset.prems(2)[OF `m \<in> S`] `l m < r m`
    92           by (auto simp add: Ioc_subset_iff intro!: mono_F)
    93         finally show ?case
    94           by (auto intro: add_mono)
    95       qed (simp add: `a \<le> b` less_le)
    96     qed }
    97   note claim1 = this
    98 
    99   (* second key induction: a lower bound on the measures of any finite collection of Ai's
   100      that cover an interval {u..v} *)
   101 
   102   { fix S u v and l r :: "nat \<Rightarrow> real"
   103     assume "finite S" "\<And>i. i\<in>S \<Longrightarrow> l i < r i" "{u..v} \<subseteq> (\<Union>i\<in>S. {l i<..< r i})"
   104     then have "F v - F u \<le> (\<Sum>i\<in>S. F (r i) - F (l i))"
   105     proof (induction arbitrary: v u rule: finite_psubset_induct)
   106       case (psubset S)
   107       show ?case
   108       proof cases
   109         assume "S = {}" then show ?case
   110           using psubset by (simp add: mono_F)
   111       next
   112         assume "S \<noteq> {}"
   113         then obtain j where "j \<in> S"
   114           by auto
   115 
   116         let ?R = "r j < u \<or> l j > v \<or> (\<exists>i\<in>S-{j}. l i \<le> l j \<and> r j \<le> r i)"
   117         show ?case
   118         proof cases
   119           assume "?R"
   120           with `j \<in> S` psubset.prems have "{u..v} \<subseteq> (\<Union>i\<in>S-{j}. {l i<..< r i})"
   121             apply (auto simp: subset_eq Ball_def)
   122             apply (metis Diff_iff less_le_trans leD linear singletonD)
   123             apply (metis Diff_iff less_le_trans leD linear singletonD)
   124             apply (metis order_trans less_le_not_le linear)
   125             done
   126           with `j \<in> S` have "F v - F u \<le> (\<Sum>i\<in>S - {j}. F (r i) - F (l i))"
   127             by (intro psubset) auto
   128           also have "\<dots> \<le> (\<Sum>i\<in>S. F (r i) - F (l i))"
   129             using psubset.prems
   130             by (intro setsum_mono2 psubset) (auto intro: less_imp_le)
   131           finally show ?thesis .
   132         next
   133           assume "\<not> ?R"
   134           then have j: "u \<le> r j" "l j \<le> v" "\<And>i. i \<in> S - {j} \<Longrightarrow> r i < r j \<or> l i > l j"
   135             by (auto simp: not_less)
   136           let ?S1 = "{i \<in> S. l i < l j}"
   137           let ?S2 = "{i \<in> S. r i > r j}"
   138 
   139           have "(\<Sum>i\<in>S. F (r i) - F (l i)) \<ge> (\<Sum>i\<in>?S1 \<union> ?S2 \<union> {j}. F (r i) - F (l i))"
   140             using `j \<in> S` `finite S` psubset.prems j
   141             by (intro setsum_mono2) (auto intro: less_imp_le)
   142           also have "(\<Sum>i\<in>?S1 \<union> ?S2 \<union> {j}. F (r i) - F (l i)) =
   143             (\<Sum>i\<in>?S1. F (r i) - F (l i)) + (\<Sum>i\<in>?S2 . F (r i) - F (l i)) + (F (r j) - F (l j))"
   144             using psubset(1) psubset.prems(1) j
   145             apply (subst setsum.union_disjoint)
   146             apply simp_all
   147             apply (subst setsum.union_disjoint)
   148             apply auto
   149             apply (metis less_le_not_le)
   150             done
   151           also (xtrans) have "(\<Sum>i\<in>?S1. F (r i) - F (l i)) \<ge> F (l j) - F u"
   152             using `j \<in> S` `finite S` psubset.prems j
   153             apply (intro psubset.IH psubset)
   154             apply (auto simp: subset_eq Ball_def)
   155             apply (metis less_le_trans not_le)
   156             done
   157           also (xtrans) have "(\<Sum>i\<in>?S2. F (r i) - F (l i)) \<ge> F v - F (r j)"
   158             using `j \<in> S` `finite S` psubset.prems j
   159             apply (intro psubset.IH psubset)
   160             apply (auto simp: subset_eq Ball_def)
   161             apply (metis le_less_trans not_le)
   162             done
   163           finally (xtrans) show ?case
   164             by (auto simp: add_mono)
   165         qed
   166       qed
   167     qed }
   168   note claim2 = this
   169 
   170   (* now prove the inequality going the other way *)
   171 
   172   { fix epsilon :: real assume egt0: "epsilon > 0"
   173     have "\<forall>i. \<exists>d. d > 0 &  F (r i + d) < F (r i) + epsilon / 2^(i+2)"
   174     proof 
   175       fix i
   176       note right_cont_F [of "r i"]
   177       thus "\<exists>d. d > 0 \<and> F (r i + d) < F (r i) + epsilon / 2^(i+2)"
   178         apply -
   179         apply (subst (asm) continuous_at_right_real_increasing)
   180         apply (rule mono_F, assumption)
   181         apply (drule_tac x = "epsilon / 2 ^ (i + 2)" in spec)
   182         apply (erule impE)
   183         using egt0 by (auto simp add: field_simps)
   184     qed
   185     then obtain delta where 
   186         deltai_gt0: "\<And>i. delta i > 0" and
   187         deltai_prop: "\<And>i. F (r i + delta i) < F (r i) + epsilon / 2^(i+2)"
   188       by metis
   189     have "\<exists>a' > a. F a' - F a < epsilon / 2"
   190       apply (insert right_cont_F [of a])
   191       apply (subst (asm) continuous_at_right_real_increasing)
   192       using mono_F apply force
   193       apply (drule_tac x = "epsilon / 2" in spec)
   194       using egt0 apply (auto simp add: field_simps)
   195       by (metis add_less_cancel_left comm_monoid_add_class.add.right_neutral 
   196         comm_semiring_1_class.normalizing_semiring_rules(24) mult_2 mult_2_right)
   197     then obtain a' where a'lea [arith]: "a' > a" and 
   198       a_prop: "F a' - F a < epsilon / 2"
   199       by auto
   200     def S' \<equiv> "{i. l i < r i}"
   201     obtain S :: "nat set" where 
   202       "S \<subseteq> S'" and finS: "finite S" and 
   203       Sprop: "{a'..b} \<subseteq> (\<Union>i \<in> S. {l i<..<r i + delta i})"
   204     proof (rule compactE_image)
   205       show "compact {a'..b}"
   206         by (rule compact_Icc)
   207       show "\<forall>i \<in> S'. open ({l i<..<r i + delta i})" by auto
   208       have "{a'..b} \<subseteq> {a <.. b}"
   209         by auto
   210       also have "{a <.. b} = (\<Union>i\<in>S'. {l i<..r i})"
   211         unfolding lr_eq_ab[symmetric] by (fastforce simp add: S'_def intro: less_le_trans)
   212       also have "\<dots> \<subseteq> (\<Union>i \<in> S'. {l i<..<r i + delta i})"
   213         apply (intro UN_mono)
   214         apply (auto simp: S'_def)
   215         apply (cut_tac i=i in deltai_gt0)
   216         apply simp
   217         done
   218       finally show "{a'..b} \<subseteq> (\<Union>i \<in> S'. {l i<..<r i + delta i})" .
   219     qed
   220     with S'_def have Sprop2: "\<And>i. i \<in> S \<Longrightarrow> l i < r i" by auto
   221     from finS have "\<exists>n. \<forall>i \<in> S. i \<le> n" 
   222       by (subst finite_nat_set_iff_bounded_le [symmetric])
   223     then obtain n where Sbound [rule_format]: "\<forall>i \<in> S. i \<le> n" ..
   224     have "F b - F a' \<le> (\<Sum>i\<in>S. F (r i + delta i) - F (l i))"
   225       apply (rule claim2 [rule_format])
   226       using finS Sprop apply auto
   227       apply (frule Sprop2)
   228       apply (subgoal_tac "delta i > 0")
   229       apply arith
   230       by (rule deltai_gt0)
   231     also have "... \<le> (SUM i : S. F(r i) - F(l i) + epsilon / 2^(i+2))"
   232       apply (rule setsum_mono)
   233       apply simp
   234       apply (rule order_trans)
   235       apply (rule less_imp_le)
   236       apply (rule deltai_prop)
   237       by auto
   238     also have "... = (SUM i : S. F(r i) - F(l i)) + 
   239         (epsilon / 4) * (SUM i : S. (1 / 2)^i)" (is "_ = ?t + _")
   240       by (subst setsum.distrib) (simp add: field_simps setsum_right_distrib)
   241     also have "... \<le> ?t + (epsilon / 4) * (\<Sum> i < Suc n. (1 / 2)^i)"
   242       apply (rule add_left_mono)
   243       apply (rule mult_left_mono)
   244       apply (rule setsum_mono2)
   245       using egt0 apply auto 
   246       by (frule Sbound, auto)
   247     also have "... \<le> ?t + (epsilon / 2)"
   248       apply (rule add_left_mono)
   249       apply (subst geometric_sum)
   250       apply auto
   251       apply (rule mult_left_mono)
   252       using egt0 apply auto
   253       done
   254     finally have aux2: "F b - F a' \<le> (\<Sum>i\<in>S. F (r i) - F (l i)) + epsilon / 2"
   255       by simp
   256 
   257     have "F b - F a = (F b - F a') + (F a' - F a)"
   258       by auto
   259     also have "... \<le> (F b - F a') + epsilon / 2"
   260       using a_prop by (intro add_left_mono) simp
   261     also have "... \<le> (\<Sum>i\<in>S. F (r i) - F (l i)) + epsilon / 2 + epsilon / 2"
   262       apply (intro add_right_mono)
   263       apply (rule aux2)
   264       done
   265     also have "... = (\<Sum>i\<in>S. F (r i) - F (l i)) + epsilon"
   266       by auto
   267     also have "... \<le> (\<Sum>i\<le>n. F (r i) - F (l i)) + epsilon"
   268       using finS Sbound Sprop by (auto intro!: add_right_mono setsum_mono3)
   269     finally have "ereal (F b - F a) \<le> (\<Sum>i\<le>n. ereal (F (r i) - F (l i))) + epsilon"
   270       by simp
   271     then have "ereal (F b - F a) \<le> (\<Sum>i. ereal (F (r i) - F (l i))) + (epsilon :: real)"
   272       apply (rule_tac order_trans)
   273       prefer 2
   274       apply (rule add_mono[where c="ereal epsilon"])
   275       apply (rule suminf_upper[of _ "Suc n"])
   276       apply (auto simp add: lessThan_Suc_atMost)
   277       done }
   278   hence "ereal (F b - F a) \<le> (\<Sum>i. ereal (F (r i) - F (l i)))"
   279     by (auto intro: ereal_le_epsilon2)
   280   moreover
   281   have "(\<Sum>i. ereal (F (r i) - F (l i))) \<le> ereal (F b - F a)"
   282     by (auto simp add: claim1 intro!: suminf_bound)
   283   ultimately show "(\<Sum>n. ereal (F (r n) - F (l n))) = ereal (F b - F a)"
   284     by simp
   285 qed (auto simp: Ioc_inj diff_le_iff mono_F)
   286 
   287 lemma measure_interval_measure_Ioc:
   288   assumes "a \<le> b"
   289   assumes mono_F: "\<And>x y. x \<le> y \<Longrightarrow> F x \<le> F y"
   290   assumes right_cont_F : "\<And>a. continuous (at_right a) F" 
   291   shows "measure (interval_measure F) {a <.. b} = F b - F a"
   292   unfolding measure_def
   293   apply (subst emeasure_interval_measure_Ioc)
   294   apply fact+
   295   apply simp
   296   done
   297 
   298 lemma emeasure_interval_measure_Ioc_eq:
   299   "(\<And>x y. x \<le> y \<Longrightarrow> F x \<le> F y) \<Longrightarrow> (\<And>a. continuous (at_right a) F) \<Longrightarrow>
   300     emeasure (interval_measure F) {a <.. b} = (if a \<le> b then F b - F a else 0)"
   301   using emeasure_interval_measure_Ioc[of a b F] by auto
   302 
   303 lemma sets_interval_measure [simp]: "sets (interval_measure F) = sets borel"
   304   apply (simp add: sets_extend_measure interval_measure_def borel_sigma_sets_Ioc)
   305   apply (rule sigma_sets_eqI)
   306   apply auto
   307   apply (case_tac "a \<le> ba")
   308   apply (auto intro: sigma_sets.Empty)
   309   done
   310 
   311 lemma space_interval_measure [simp]: "space (interval_measure F) = UNIV"
   312   by (simp add: interval_measure_def space_extend_measure)
   313 
   314 lemma emeasure_interval_measure_Icc:
   315   assumes "a \<le> b"
   316   assumes mono_F: "\<And>x y. x \<le> y \<Longrightarrow> F x \<le> F y"
   317   assumes cont_F : "continuous_on UNIV F" 
   318   shows "emeasure (interval_measure F) {a .. b} = F b - F a"
   319 proof (rule tendsto_unique)
   320   { fix a b :: real assume "a \<le> b" then have "emeasure (interval_measure F) {a <.. b} = F b - F a"
   321       using cont_F
   322       by (subst emeasure_interval_measure_Ioc)
   323          (auto intro: mono_F continuous_within_subset simp: continuous_on_eq_continuous_within) }
   324   note * = this
   325 
   326   let ?F = "interval_measure F"
   327   show "((\<lambda>a. F b - F a) ---> emeasure ?F {a..b}) (at_left a)"
   328   proof (rule tendsto_at_left_sequentially)
   329     show "a - 1 < a" by simp
   330     fix X assume "\<And>n. X n < a" "incseq X" "X ----> a"
   331     with `a \<le> b` have "(\<lambda>n. emeasure ?F {X n<..b}) ----> emeasure ?F (\<Inter>n. {X n <..b})"
   332       apply (intro Lim_emeasure_decseq)
   333       apply (auto simp: decseq_def incseq_def emeasure_interval_measure_Ioc *)
   334       apply force
   335       apply (subst (asm ) *)
   336       apply (auto intro: less_le_trans less_imp_le)
   337       done
   338     also have "(\<Inter>n. {X n <..b}) = {a..b}"
   339       using `\<And>n. X n < a`
   340       apply auto
   341       apply (rule LIMSEQ_le_const2[OF `X ----> a`])
   342       apply (auto intro: less_imp_le)
   343       apply (auto intro: less_le_trans)
   344       done
   345     also have "(\<lambda>n. emeasure ?F {X n<..b}) = (\<lambda>n. F b - F (X n))"
   346       using `\<And>n. X n < a` `a \<le> b` by (subst *) (auto intro: less_imp_le less_le_trans)
   347     finally show "(\<lambda>n. F b - F (X n)) ----> emeasure ?F {a..b}" .
   348   qed
   349   show "((\<lambda>a. ereal (F b - F a)) ---> F b - F a) (at_left a)"
   350     using cont_F
   351     by (intro lim_ereal[THEN iffD2] tendsto_intros )
   352        (auto simp: continuous_on_def intro: tendsto_within_subset)
   353 qed (rule trivial_limit_at_left_real)
   354   
   355 lemma sigma_finite_interval_measure:
   356   assumes mono_F: "\<And>x y. x \<le> y \<Longrightarrow> F x \<le> F y"
   357   assumes right_cont_F : "\<And>a. continuous (at_right a) F" 
   358   shows "sigma_finite_measure (interval_measure F)"
   359   apply unfold_locales
   360   apply (intro exI[of _ "(\<lambda>(a, b). {a <.. b}) ` (\<rat> \<times> \<rat>)"])
   361   apply (auto intro!: Rats_no_top_le Rats_no_bot_less countable_rat simp: emeasure_interval_measure_Ioc_eq[OF assms])
   362   done
   363 
   364 subsection {* Lebesgue-Borel measure *}
   365 
   366 definition lborel :: "('a :: euclidean_space) measure" where
   367   "lborel = distr (\<Pi>\<^sub>M b\<in>Basis. interval_measure (\<lambda>x. x)) borel (\<lambda>f. \<Sum>b\<in>Basis. f b *\<^sub>R b)"
   368 
   369 lemma 
   370   shows sets_lborel[simp]: "sets lborel = sets borel"
   371     and space_lborel[simp]: "space lborel = space borel"
   372     and measurable_lborel1[simp]: "measurable M lborel = measurable M borel"
   373     and measurable_lborel2[simp]: "measurable lborel M = measurable borel M"
   374   by (simp_all add: lborel_def)
   375 
   376 context
   377 begin
   378 
   379 interpretation sigma_finite_measure "interval_measure (\<lambda>x. x)"
   380   by (rule sigma_finite_interval_measure) auto
   381 interpretation finite_product_sigma_finite "\<lambda>_. interval_measure (\<lambda>x. x)" Basis
   382   proof qed simp
   383 
   384 lemma lborel_eq_real: "lborel = interval_measure (\<lambda>x. x)"
   385   unfolding lborel_def Basis_real_def
   386   using distr_id[of "interval_measure (\<lambda>x. x)"]
   387   by (subst distr_component[symmetric])
   388      (simp_all add: distr_distr comp_def del: distr_id cong: distr_cong)
   389 
   390 lemma lborel_eq: "lborel = distr (\<Pi>\<^sub>M b\<in>Basis. lborel) borel (\<lambda>f. \<Sum>b\<in>Basis. f b *\<^sub>R b)"
   391   by (subst lborel_def) (simp add: lborel_eq_real)
   392 
   393 lemma nn_integral_lborel_setprod:
   394   assumes [measurable]: "\<And>b. b \<in> Basis \<Longrightarrow> f b \<in> borel_measurable borel"
   395   assumes nn[simp]: "\<And>b x. b \<in> Basis \<Longrightarrow> 0 \<le> f b x"
   396   shows "(\<integral>\<^sup>+x. (\<Prod>b\<in>Basis. f b (x \<bullet> b)) \<partial>lborel) = (\<Prod>b\<in>Basis. (\<integral>\<^sup>+x. f b x \<partial>lborel))"
   397   by (simp add: lborel_def nn_integral_distr product_nn_integral_setprod
   398                 product_nn_integral_singleton)
   399 
   400 lemma emeasure_lborel_Icc[simp]: 
   401   fixes l u :: real
   402   assumes [simp]: "l \<le> u"
   403   shows "emeasure lborel {l .. u} = u - l"
   404 proof -
   405   have "((\<lambda>f. f 1) -` {l..u} \<inter> space (Pi\<^sub>M {1} (\<lambda>b. interval_measure (\<lambda>x. x)))) = {1::real} \<rightarrow>\<^sub>E {l..u}"
   406     by (auto simp: space_PiM)
   407   then show ?thesis
   408     by (simp add: lborel_def emeasure_distr emeasure_PiM emeasure_interval_measure_Icc continuous_on_id)
   409 qed
   410 
   411 lemma emeasure_lborel_Icc_eq: "emeasure lborel {l .. u} = ereal (if l \<le> u then u - l else 0)"
   412   by simp
   413 
   414 lemma emeasure_lborel_cbox[simp]:
   415   assumes [simp]: "\<And>b. b \<in> Basis \<Longrightarrow> l \<bullet> b \<le> u \<bullet> b"
   416   shows "emeasure lborel (cbox l u) = (\<Prod>b\<in>Basis. (u - l) \<bullet> b)"
   417 proof -
   418   have "(\<lambda>x. \<Prod>b\<in>Basis. indicator {l\<bullet>b .. u\<bullet>b} (x \<bullet> b) :: ereal) = indicator (cbox l u)"
   419     by (auto simp: fun_eq_iff cbox_def setprod_ereal_0 split: split_indicator)
   420   then have "emeasure lborel (cbox l u) = (\<integral>\<^sup>+x. (\<Prod>b\<in>Basis. indicator {l\<bullet>b .. u\<bullet>b} (x \<bullet> b)) \<partial>lborel)"
   421     by simp
   422   also have "\<dots> = (\<Prod>b\<in>Basis. (u - l) \<bullet> b)"
   423     by (subst nn_integral_lborel_setprod) (simp_all add: setprod_ereal inner_diff_left)
   424   finally show ?thesis .
   425 qed
   426 
   427 lemma AE_lborel_singleton: "AE x in lborel::'a::euclidean_space measure. x \<noteq> c"
   428   using AE_discrete_difference[of "{c::'a}" lborel] emeasure_lborel_cbox[of c c]
   429   by (auto simp del: emeasure_lborel_cbox simp add: cbox_sing setprod_constant)
   430 
   431 lemma emeasure_lborel_Ioo[simp]:
   432   assumes [simp]: "l \<le> u"
   433   shows "emeasure lborel {l <..< u} = ereal (u - l)"
   434 proof -
   435   have "emeasure lborel {l <..< u} = emeasure lborel {l .. u}"
   436     using AE_lborel_singleton[of u] AE_lborel_singleton[of l] by (intro emeasure_eq_AE) auto
   437   then show ?thesis
   438     by simp
   439 qed
   440 
   441 lemma emeasure_lborel_Ioc[simp]:
   442   assumes [simp]: "l \<le> u"
   443   shows "emeasure lborel {l <.. u} = ereal (u - l)"
   444 proof -
   445   have "emeasure lborel {l <.. u} = emeasure lborel {l .. u}"
   446     using AE_lborel_singleton[of u] AE_lborel_singleton[of l] by (intro emeasure_eq_AE) auto
   447   then show ?thesis
   448     by simp
   449 qed
   450 
   451 lemma emeasure_lborel_Ico[simp]:
   452   assumes [simp]: "l \<le> u"
   453   shows "emeasure lborel {l ..< u} = ereal (u - l)"
   454 proof -
   455   have "emeasure lborel {l ..< u} = emeasure lborel {l .. u}"
   456     using AE_lborel_singleton[of u] AE_lborel_singleton[of l] by (intro emeasure_eq_AE) auto
   457   then show ?thesis
   458     by simp
   459 qed
   460 
   461 lemma emeasure_lborel_box[simp]:
   462   assumes [simp]: "\<And>b. b \<in> Basis \<Longrightarrow> l \<bullet> b \<le> u \<bullet> b"
   463   shows "emeasure lborel (box l u) = (\<Prod>b\<in>Basis. (u - l) \<bullet> b)"
   464 proof -
   465   have "(\<lambda>x. \<Prod>b\<in>Basis. indicator {l\<bullet>b <..< u\<bullet>b} (x \<bullet> b) :: ereal) = indicator (box l u)"
   466     by (auto simp: fun_eq_iff box_def setprod_ereal_0 split: split_indicator)
   467   then have "emeasure lborel (box l u) = (\<integral>\<^sup>+x. (\<Prod>b\<in>Basis. indicator {l\<bullet>b <..< u\<bullet>b} (x \<bullet> b)) \<partial>lborel)"
   468     by simp
   469   also have "\<dots> = (\<Prod>b\<in>Basis. (u - l) \<bullet> b)"
   470     by (subst nn_integral_lborel_setprod) (simp_all add: setprod_ereal inner_diff_left)
   471   finally show ?thesis .
   472 qed
   473 
   474 lemma emeasure_lborel_cbox_eq:
   475   "emeasure lborel (cbox l u) = (if \<forall>b\<in>Basis. l \<bullet> b \<le> u \<bullet> b then \<Prod>b\<in>Basis. (u - l) \<bullet> b else 0)"
   476   using box_eq_empty(2)[THEN iffD2, of u l] by (auto simp: not_le)
   477 
   478 lemma emeasure_lborel_box_eq:
   479   "emeasure lborel (box l u) = (if \<forall>b\<in>Basis. l \<bullet> b \<le> u \<bullet> b then \<Prod>b\<in>Basis. (u - l) \<bullet> b else 0)"
   480   using box_eq_empty(1)[THEN iffD2, of u l] by (auto simp: not_le dest!: less_imp_le) force
   481 
   482 lemma
   483   fixes l u :: real
   484   assumes [simp]: "l \<le> u"
   485   shows measure_lborel_Icc[simp]: "measure lborel {l .. u} = u - l"
   486     and measure_lborel_Ico[simp]: "measure lborel {l ..< u} = u - l"
   487     and measure_lborel_Ioc[simp]: "measure lborel {l <.. u} = u - l"
   488     and measure_lborel_Ioo[simp]: "measure lborel {l <..< u} = u - l"
   489   by (simp_all add: measure_def)
   490 
   491 lemma 
   492   assumes [simp]: "\<And>b. b \<in> Basis \<Longrightarrow> l \<bullet> b \<le> u \<bullet> b"
   493   shows measure_lborel_box[simp]: "measure lborel (box l u) = (\<Prod>b\<in>Basis. (u - l) \<bullet> b)"
   494     and measure_lborel_cbox[simp]: "measure lborel (cbox l u) = (\<Prod>b\<in>Basis. (u - l) \<bullet> b)"
   495   by (simp_all add: measure_def)
   496 
   497 lemma sigma_finite_lborel: "sigma_finite_measure lborel"
   498 proof
   499   show "\<exists>A::'a set set. countable A \<and> A \<subseteq> sets lborel \<and> \<Union>A = space lborel \<and> (\<forall>a\<in>A. emeasure lborel a \<noteq> \<infinity>)"
   500     by (intro exI[of _ "range (\<lambda>n::nat. box (- real n *\<^sub>R One) (real n *\<^sub>R One))"])
   501        (auto simp: emeasure_lborel_cbox_eq UN_box_eq_UNIV)
   502 qed
   503 
   504 end
   505 
   506 lemma emeasure_lborel_UNIV: "emeasure lborel (UNIV::'a::euclidean_space set) = \<infinity>"
   507   unfolding UN_box_eq_UNIV[symmetric]
   508   apply (subst SUP_emeasure_incseq[symmetric])
   509   apply (auto simp: incseq_def subset_box inner_add_left setprod_constant intro!: SUP_PInfty)
   510   apply (rule_tac x="Suc n" in exI)
   511   apply (rule order_trans[OF _ self_le_power])
   512   apply (auto simp: card_gt_0_iff real_of_nat_Suc)
   513   done
   514 
   515 lemma emeasure_lborel_singleton[simp]: "emeasure lborel {x} = 0"
   516   using emeasure_lborel_cbox[of x x] nonempty_Basis
   517   by (auto simp del: emeasure_lborel_cbox nonempty_Basis simp add: cbox_sing)
   518 
   519 lemma emeasure_lborel_countable:
   520   fixes A :: "'a::euclidean_space set"
   521   assumes "countable A"
   522   shows "emeasure lborel A = 0"
   523 proof -
   524   have "A \<subseteq> (\<Union>i. {from_nat_into A i})" using from_nat_into_surj assms by force
   525   moreover have "emeasure lborel (\<Union>i. {from_nat_into A i}) = 0"
   526     by (rule emeasure_UN_eq_0) auto
   527   ultimately have "emeasure lborel A \<le> 0" using emeasure_mono
   528     by (metis assms bot.extremum_unique emeasure_empty image_eq_UN range_from_nat_into sets.empty_sets)
   529   thus ?thesis by (auto simp add: emeasure_le_0_iff)
   530 qed
   531 
   532 subsection {* Affine transformation on the Lebesgue-Borel *}
   533 
   534 lemma lborel_eqI:
   535   fixes M :: "'a::euclidean_space measure"
   536   assumes emeasure_eq: "\<And>l u. (\<And>b. b \<in> Basis \<Longrightarrow> l \<bullet> b \<le> u \<bullet> b) \<Longrightarrow> emeasure M (box l u) = (\<Prod>b\<in>Basis. (u - l) \<bullet> b)"
   537   assumes sets_eq: "sets M = sets borel"
   538   shows "lborel = M"
   539 proof (rule measure_eqI_generator_eq)
   540   let ?E = "range (\<lambda>(a, b). box a b::'a set)"
   541   show "Int_stable ?E"
   542     by (auto simp: Int_stable_def box_Int_box)
   543 
   544   show "?E \<subseteq> Pow UNIV" "sets lborel = sigma_sets UNIV ?E" "sets M = sigma_sets UNIV ?E"
   545     by (simp_all add: borel_eq_box sets_eq)
   546 
   547   let ?A = "\<lambda>n::nat. box (- (real n *\<^sub>R One)) (real n *\<^sub>R One) :: 'a set"
   548   show "range ?A \<subseteq> ?E" "(\<Union>i. ?A i) = UNIV"
   549     unfolding UN_box_eq_UNIV by auto
   550 
   551   { fix i show "emeasure lborel (?A i) \<noteq> \<infinity>" by auto }
   552   { fix X assume "X \<in> ?E" then show "emeasure lborel X = emeasure M X"
   553       apply (auto simp: emeasure_eq emeasure_lborel_box_eq )
   554       apply (subst box_eq_empty(1)[THEN iffD2])
   555       apply (auto intro: less_imp_le simp: not_le)
   556       done }
   557 qed
   558 
   559 lemma lborel_affine:
   560   fixes t :: "'a::euclidean_space" assumes "c \<noteq> 0"
   561   shows "lborel = density (distr lborel borel (\<lambda>x. t + c *\<^sub>R x)) (\<lambda>_. \<bar>c\<bar>^DIM('a))" (is "_ = ?D")
   562 proof (rule lborel_eqI)
   563   let ?B = "Basis :: 'a set"
   564   fix l u assume le: "\<And>b. b \<in> ?B \<Longrightarrow> l \<bullet> b \<le> u \<bullet> b"
   565   show "emeasure ?D (box l u) = (\<Prod>b\<in>?B. (u - l) \<bullet> b)"
   566   proof cases
   567     assume "0 < c"
   568     then have "(\<lambda>x. t + c *\<^sub>R x) -` box l u = box ((l - t) /\<^sub>R c) ((u - t) /\<^sub>R c)"
   569       by (auto simp: field_simps box_def inner_simps)
   570     with `0 < c` show ?thesis
   571       using le
   572       by (auto simp: field_simps emeasure_density nn_integral_distr nn_integral_cmult
   573                      emeasure_lborel_box_eq inner_simps setprod_dividef setprod_constant
   574                      borel_measurable_indicator' emeasure_distr)
   575   next
   576     assume "\<not> 0 < c" with `c \<noteq> 0` have "c < 0" by auto
   577     then have "box ((u - t) /\<^sub>R c) ((l - t) /\<^sub>R c) = (\<lambda>x. t + c *\<^sub>R x) -` box l u"
   578       by (auto simp: field_simps box_def inner_simps)
   579     then have "\<And>x. indicator (box l u) (t + c *\<^sub>R x) = (indicator (box ((u - t) /\<^sub>R c) ((l - t) /\<^sub>R c)) x :: ereal)"
   580       by (auto split: split_indicator)
   581     moreover
   582     { have "(- c) ^ card ?B * (\<Prod>x\<in>?B. l \<bullet> x - u \<bullet> x) = 
   583          (-1 * c) ^ card ?B * (\<Prod>x\<in>?B. -1 * (u \<bullet> x - l \<bullet> x))"
   584          by simp
   585       also have "\<dots> = (-1 * -1)^card ?B * c ^ card ?B * (\<Prod>x\<in>?B. u \<bullet> x - l \<bullet> x)"
   586         unfolding setprod.distrib power_mult_distrib by (simp add: setprod_constant)
   587       finally have "(- c) ^ card ?B * (\<Prod>x\<in>?B. l \<bullet> x - u \<bullet> x) = c ^ card ?B * (\<Prod>b\<in>?B. u \<bullet> b - l \<bullet> b)"
   588         by simp }
   589     ultimately show ?thesis
   590       using `c < 0` le
   591       by (auto simp: field_simps emeasure_density nn_integral_distr nn_integral_cmult
   592                      emeasure_lborel_box_eq inner_simps setprod_dividef setprod_constant
   593                      borel_measurable_indicator' emeasure_distr)
   594   qed
   595 qed simp
   596 
   597 lemma lborel_real_affine:
   598   "c \<noteq> 0 \<Longrightarrow> lborel = density (distr lborel borel (\<lambda>x. t + c * x)) (\<lambda>_. \<bar>c\<bar>)"
   599   using lborel_affine[of c t] by simp
   600 
   601 lemma AE_borel_affine: 
   602   fixes P :: "real \<Rightarrow> bool"
   603   shows "c \<noteq> 0 \<Longrightarrow> Measurable.pred borel P \<Longrightarrow> AE x in lborel. P x \<Longrightarrow> AE x in lborel. P (t + c * x)"
   604   by (subst lborel_real_affine[where t="- t / c" and c="1 / c"])
   605      (simp_all add: AE_density AE_distr_iff field_simps)
   606 
   607 lemma nn_integral_real_affine:
   608   fixes c :: real assumes [measurable]: "f \<in> borel_measurable borel" and c: "c \<noteq> 0"
   609   shows "(\<integral>\<^sup>+x. f x \<partial>lborel) = \<bar>c\<bar> * (\<integral>\<^sup>+x. f (t + c * x) \<partial>lborel)"
   610   by (subst lborel_real_affine[OF c, of t])
   611      (simp add: nn_integral_density nn_integral_distr nn_integral_cmult)
   612 
   613 lemma lborel_integrable_real_affine:
   614   fixes f :: "real \<Rightarrow> 'a :: {banach, second_countable_topology}"
   615   assumes f: "integrable lborel f"
   616   shows "c \<noteq> 0 \<Longrightarrow> integrable lborel (\<lambda>x. f (t + c * x))"
   617   using f f[THEN borel_measurable_integrable] unfolding integrable_iff_bounded
   618   by (subst (asm) nn_integral_real_affine[where c=c and t=t]) auto
   619 
   620 lemma lborel_integrable_real_affine_iff:
   621   fixes f :: "real \<Rightarrow> 'a :: {banach, second_countable_topology}"
   622   shows "c \<noteq> 0 \<Longrightarrow> integrable lborel (\<lambda>x. f (t + c * x)) \<longleftrightarrow> integrable lborel f"
   623   using
   624     lborel_integrable_real_affine[of f c t]
   625     lborel_integrable_real_affine[of "\<lambda>x. f (t + c * x)" "1/c" "-t/c"]
   626   by (auto simp add: field_simps)
   627 
   628 lemma lborel_integral_real_affine:
   629   fixes f :: "real \<Rightarrow> 'a :: {banach, second_countable_topology}" and c :: real
   630   assumes c: "c \<noteq> 0" shows "(\<integral>x. f x \<partial> lborel) = \<bar>c\<bar> *\<^sub>R (\<integral>x. f (t + c * x) \<partial>lborel)"
   631 proof cases
   632   assume f[measurable]: "integrable lborel f" then show ?thesis
   633     using c f f[THEN borel_measurable_integrable] f[THEN lborel_integrable_real_affine, of c t]
   634     by (subst lborel_real_affine[OF c, of t])
   635        (simp add: integral_density integral_distr)
   636 next
   637   assume "\<not> integrable lborel f" with c show ?thesis
   638     by (simp add: lborel_integrable_real_affine_iff not_integrable_integral_eq)
   639 qed
   640 
   641 lemma divideR_right: 
   642   fixes x y :: "'a::real_normed_vector"
   643   shows "r \<noteq> 0 \<Longrightarrow> y = x /\<^sub>R r \<longleftrightarrow> r *\<^sub>R y = x"
   644   using scaleR_cancel_left[of r y "x /\<^sub>R r"] by simp
   645 
   646 lemma lborel_has_bochner_integral_real_affine_iff:
   647   fixes x :: "'a :: {banach, second_countable_topology}"
   648   shows "c \<noteq> 0 \<Longrightarrow>
   649     has_bochner_integral lborel f x \<longleftrightarrow>
   650     has_bochner_integral lborel (\<lambda>x. f (t + c * x)) (x /\<^sub>R \<bar>c\<bar>)"
   651   unfolding has_bochner_integral_iff lborel_integrable_real_affine_iff
   652   by (simp_all add: lborel_integral_real_affine[symmetric] divideR_right cong: conj_cong)
   653 
   654 interpretation lborel!: sigma_finite_measure lborel
   655   by (rule sigma_finite_lborel)
   656 
   657 interpretation lborel_pair: pair_sigma_finite lborel lborel ..
   658 
   659 (* FIXME: conversion in measurable prover *)
   660 lemma lborelD_Collect[measurable (raw)]: "{x\<in>space borel. P x} \<in> sets borel \<Longrightarrow> {x\<in>space lborel. P x} \<in> sets lborel" by simp
   661 lemma lborelD[measurable (raw)]: "A \<in> sets borel \<Longrightarrow> A \<in> sets lborel" by simp
   662 
   663 subsection {* Equivalence Lebesgue integral on @{const lborel} and HK-integral *}
   664 
   665 lemma has_integral_measure_lborel:
   666   fixes A :: "'a::euclidean_space set"
   667   assumes A[measurable]: "A \<in> sets borel" and finite: "emeasure lborel A < \<infinity>"
   668   shows "((\<lambda>x. 1) has_integral measure lborel A) A"
   669 proof -
   670   { fix l u :: 'a
   671     have "((\<lambda>x. 1) has_integral measure lborel (box l u)) (box l u)"
   672     proof cases
   673       assume "\<forall>b\<in>Basis. l \<bullet> b \<le> u \<bullet> b"
   674       then show ?thesis
   675         apply simp
   676         apply (subst has_integral_restrict[symmetric, OF box_subset_cbox])
   677         apply (subst has_integral_spike_interior_eq[where g="\<lambda>_. 1"])
   678         using has_integral_const[of "1::real" l u]
   679         apply (simp_all add: inner_diff_left[symmetric] content_cbox_cases)
   680         done
   681     next
   682       assume "\<not> (\<forall>b\<in>Basis. l \<bullet> b \<le> u \<bullet> b)"
   683       then have "box l u = {}"
   684         unfolding box_eq_empty by (auto simp: not_le intro: less_imp_le)
   685       then show ?thesis
   686         by simp
   687     qed }
   688   note has_integral_box = this
   689 
   690   { fix a b :: 'a let ?M = "\<lambda>A. measure lborel (A \<inter> box a b)"
   691     have "Int_stable  (range (\<lambda>(a, b). box a b))"
   692       by (auto simp: Int_stable_def box_Int_box)
   693     moreover have "(range (\<lambda>(a, b). box a b)) \<subseteq> Pow UNIV"
   694       by auto
   695     moreover have "A \<in> sigma_sets UNIV (range (\<lambda>(a, b). box a b))"
   696        using A unfolding borel_eq_box by simp
   697     ultimately have "((\<lambda>x. 1) has_integral ?M A) (A \<inter> box a b)"
   698     proof (induction rule: sigma_sets_induct_disjoint)
   699       case (basic A) then show ?case
   700         by (auto simp: box_Int_box has_integral_box)
   701     next
   702       case empty then show ?case
   703         by simp
   704     next
   705       case (compl A)
   706       then have [measurable]: "A \<in> sets borel"
   707         by (simp add: borel_eq_box)
   708 
   709       have "((\<lambda>x. 1) has_integral ?M (box a b)) (box a b)"
   710         by (simp add: has_integral_box)
   711       moreover have "((\<lambda>x. if x \<in> A \<inter> box a b then 1 else 0) has_integral ?M A) (box a b)"
   712         by (subst has_integral_restrict) (auto intro: compl)
   713       ultimately have "((\<lambda>x. 1 - (if x \<in> A \<inter> box a b then 1 else 0)) has_integral ?M (box a b) - ?M A) (box a b)"
   714         by (rule has_integral_sub)
   715       then have "((\<lambda>x. (if x \<in> (UNIV - A) \<inter> box a b then 1 else 0)) has_integral ?M (box a b) - ?M A) (box a b)"
   716         by (rule has_integral_eq_eq[THEN iffD1, rotated 1]) auto
   717       then have "((\<lambda>x. 1) has_integral ?M (box a b) - ?M A) ((UNIV - A) \<inter> box a b)"
   718         by (subst (asm) has_integral_restrict) auto
   719       also have "?M (box a b) - ?M A = ?M (UNIV - A)"
   720         by (subst measure_Diff[symmetric]) (auto simp: emeasure_lborel_box_eq Diff_Int_distrib2)
   721       finally show ?case .
   722     next
   723       case (union F)
   724       then have [measurable]: "\<And>i. F i \<in> sets borel"
   725         by (simp add: borel_eq_box subset_eq)
   726       have "((\<lambda>x. if x \<in> UNION UNIV F \<inter> box a b then 1 else 0) has_integral ?M (\<Union>i. F i)) (box a b)"
   727       proof (rule has_integral_monotone_convergence_increasing)
   728         let ?f = "\<lambda>k x. \<Sum>i<k. if x \<in> F i \<inter> box a b then 1 else 0 :: real"
   729         show "\<And>k. (?f k has_integral (\<Sum>i<k. ?M (F i))) (box a b)"
   730           using union.IH by (auto intro!: has_integral_setsum simp del: Int_iff)
   731         show "\<And>k x. ?f k x \<le> ?f (Suc k) x"
   732           by (intro setsum_mono2) auto
   733         from union(1) have *: "\<And>x i j. x \<in> F i \<Longrightarrow> x \<in> F j \<longleftrightarrow> j = i"
   734           by (auto simp add: disjoint_family_on_def)
   735         show "\<And>x. (\<lambda>k. ?f k x) ----> (if x \<in> UNION UNIV F \<inter> box a b then 1 else 0)"
   736           apply (auto simp: * setsum.If_cases Iio_Int_singleton)
   737           apply (rule_tac k="Suc xa" in LIMSEQ_offset)
   738           apply (simp add: tendsto_const)
   739           done
   740         have *: "emeasure lborel ((\<Union>x. F x) \<inter> box a b) \<le> emeasure lborel (box a b)"
   741           by (intro emeasure_mono) auto
   742 
   743         with union(1) show "(\<lambda>k. \<Sum>i<k. ?M (F i)) ----> ?M (\<Union>i. F i)"
   744           unfolding sums_def[symmetric] UN_extend_simps
   745           by (intro measure_UNION) (auto simp: disjoint_family_on_def emeasure_lborel_box_eq)
   746       qed
   747       then show ?case
   748         by (subst (asm) has_integral_restrict) auto
   749     qed }
   750   note * = this
   751 
   752   show ?thesis
   753   proof (rule has_integral_monotone_convergence_increasing)
   754     let ?B = "\<lambda>n::nat. box (- real n *\<^sub>R One) (real n *\<^sub>R One) :: 'a set"
   755     let ?f = "\<lambda>n::nat. \<lambda>x. if x \<in> A \<inter> ?B n then 1 else 0 :: real"
   756     let ?M = "\<lambda>n. measure lborel (A \<inter> ?B n)"
   757 
   758     show "\<And>n::nat. (?f n has_integral ?M n) A"
   759       using * by (subst has_integral_restrict) simp_all
   760     show "\<And>k x. ?f k x \<le> ?f (Suc k) x"
   761       by (auto simp: box_def)
   762     { fix x assume "x \<in> A"
   763       moreover have "(\<lambda>k. indicator (A \<inter> ?B k) x :: real) ----> indicator (\<Union>k::nat. A \<inter> ?B k) x"
   764         by (intro LIMSEQ_indicator_incseq) (auto simp: incseq_def box_def)
   765       ultimately show "(\<lambda>k. if x \<in> A \<inter> ?B k then 1 else 0::real) ----> 1"
   766         by (simp add: indicator_def UN_box_eq_UNIV) }
   767 
   768     have "(\<lambda>n. emeasure lborel (A \<inter> ?B n)) ----> emeasure lborel (\<Union>n::nat. A \<inter> ?B n)"
   769       by (intro Lim_emeasure_incseq) (auto simp: incseq_def box_def)
   770     also have "(\<lambda>n. emeasure lborel (A \<inter> ?B n)) = (\<lambda>n. measure lborel (A \<inter> ?B n))"
   771     proof (intro ext emeasure_eq_ereal_measure)
   772       fix n have "emeasure lborel (A \<inter> ?B n) \<le> emeasure lborel (?B n)"
   773         by (intro emeasure_mono) auto
   774       then show "emeasure lborel (A \<inter> ?B n) \<noteq> \<infinity>"
   775         by auto
   776     qed
   777     finally show "(\<lambda>n. measure lborel (A \<inter> ?B n)) ----> measure lborel A"
   778       using emeasure_eq_ereal_measure[of lborel A] finite
   779       by (simp add: UN_box_eq_UNIV)
   780   qed
   781 qed
   782 
   783 lemma nn_integral_has_integral:
   784   fixes f::"'a::euclidean_space \<Rightarrow> real"
   785   assumes f: "f \<in> borel_measurable borel" "\<And>x. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>lborel) = ereal r"
   786   shows "(f has_integral r) UNIV"
   787 using f proof (induct arbitrary: r rule: borel_measurable_induct_real)
   788   case (set A)
   789   moreover then have "((\<lambda>x. 1) has_integral measure lborel A) A"
   790     by (intro has_integral_measure_lborel) (auto simp: ereal_indicator)
   791   ultimately show ?case
   792     by (simp add: ereal_indicator measure_def) (simp add: indicator_def)
   793 next
   794   case (mult g c)
   795   then have "ereal c * (\<integral>\<^sup>+ x. g x \<partial>lborel) = ereal r"
   796     by (subst nn_integral_cmult[symmetric]) auto
   797   then obtain r' where "(c = 0 \<and> r = 0) \<or> ((\<integral>\<^sup>+ x. ereal (g x) \<partial>lborel) = ereal r' \<and> r = c * r')"
   798     by (cases "\<integral>\<^sup>+ x. ereal (g x) \<partial>lborel") (auto split: split_if_asm)
   799   with mult show ?case
   800     by (auto intro!: has_integral_cmult_real)
   801 next
   802   case (add g h)
   803   moreover
   804   then have "(\<integral>\<^sup>+ x. h x + g x \<partial>lborel) = (\<integral>\<^sup>+ x. h x \<partial>lborel) + (\<integral>\<^sup>+ x. g x \<partial>lborel)"
   805     unfolding plus_ereal.simps[symmetric] by (subst nn_integral_add) auto
   806   with add obtain a b where "(\<integral>\<^sup>+ x. h x \<partial>lborel) = ereal a" "(\<integral>\<^sup>+ x. g x \<partial>lborel) = ereal b" "r = a + b"
   807     by (cases "\<integral>\<^sup>+ x. h x \<partial>lborel" "\<integral>\<^sup>+ x. g x \<partial>lborel" rule: ereal2_cases) auto
   808   ultimately show ?case
   809     by (auto intro!: has_integral_add)
   810 next
   811   case (seq U)
   812   note seq(1)[measurable] and f[measurable]
   813 
   814   { fix i x 
   815     have "U i x \<le> f x"
   816       using seq(5)
   817       apply (rule LIMSEQ_le_const)
   818       using seq(4)
   819       apply (auto intro!: exI[of _ i] simp: incseq_def le_fun_def)
   820       done }
   821   note U_le_f = this
   822   
   823   { fix i
   824     have "(\<integral>\<^sup>+x. ereal (U i x) \<partial>lborel) \<le> (\<integral>\<^sup>+x. ereal (f x) \<partial>lborel)"
   825       using U_le_f by (intro nn_integral_mono) simp
   826     then obtain p where "(\<integral>\<^sup>+x. U i x \<partial>lborel) = ereal p" "p \<le> r"
   827       using seq(6) by (cases "\<integral>\<^sup>+x. U i x \<partial>lborel") auto
   828     moreover then have "0 \<le> p"
   829       by (metis ereal_less_eq(5) nn_integral_nonneg)
   830     moreover note seq
   831     ultimately have "\<exists>p. (\<integral>\<^sup>+x. U i x \<partial>lborel) = ereal p \<and> 0 \<le> p \<and> p \<le> r \<and> (U i has_integral p) UNIV"
   832       by auto }
   833   then obtain p where p: "\<And>i. (\<integral>\<^sup>+x. ereal (U i x) \<partial>lborel) = ereal (p i)"
   834     and bnd: "\<And>i. p i \<le> r" "\<And>i. 0 \<le> p i"
   835     and U_int: "\<And>i.(U i has_integral (p i)) UNIV" by metis
   836 
   837   have int_eq: "\<And>i. integral UNIV (U i) = p i" using U_int by (rule integral_unique)
   838 
   839   have *: "f integrable_on UNIV \<and> (\<lambda>k. integral UNIV (U k)) ----> integral UNIV f"
   840   proof (rule monotone_convergence_increasing)
   841     show "\<forall>k. U k integrable_on UNIV" using U_int by auto
   842     show "\<forall>k. \<forall>x\<in>UNIV. U k x \<le> U (Suc k) x" using `incseq U` by (auto simp: incseq_def le_fun_def)
   843     then show "bounded {integral UNIV (U k) |k. True}"
   844       using bnd int_eq by (auto simp: bounded_real intro!: exI[of _ r])
   845     show "\<forall>x\<in>UNIV. (\<lambda>k. U k x) ----> f x"
   846       using seq by auto
   847   qed
   848   moreover have "(\<lambda>i. (\<integral>\<^sup>+x. U i x \<partial>lborel)) ----> (\<integral>\<^sup>+x. f x \<partial>lborel)"
   849     using seq U_le_f by (intro nn_integral_dominated_convergence[where w=f]) auto
   850   ultimately have "integral UNIV f = r"
   851     by (auto simp add: int_eq p seq intro: LIMSEQ_unique)
   852   with * show ?case
   853     by (simp add: has_integral_integral)
   854 qed
   855 
   856 lemma nn_integral_lborel_eq_integral:
   857   fixes f::"'a::euclidean_space \<Rightarrow> real"
   858   assumes f: "f \<in> borel_measurable borel" "\<And>x. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>lborel) < \<infinity>"
   859   shows "(\<integral>\<^sup>+x. f x \<partial>lborel) = integral UNIV f"
   860 proof -
   861   from f(3) obtain r where r: "(\<integral>\<^sup>+x. f x \<partial>lborel) = ereal r"
   862     by (cases "\<integral>\<^sup>+x. f x \<partial>lborel") auto
   863   then show ?thesis
   864     using nn_integral_has_integral[OF f(1,2) r] by (simp add: integral_unique)
   865 qed
   866 
   867 lemma nn_integral_integrable_on:
   868   fixes f::"'a::euclidean_space \<Rightarrow> real"
   869   assumes f: "f \<in> borel_measurable borel" "\<And>x. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>lborel) < \<infinity>"
   870   shows "f integrable_on UNIV"
   871 proof -
   872   from f(3) obtain r where r: "(\<integral>\<^sup>+x. f x \<partial>lborel) = ereal r"
   873     by (cases "\<integral>\<^sup>+x. f x \<partial>lborel") auto
   874   then show ?thesis
   875     by (intro has_integral_integrable[where i=r] nn_integral_has_integral[where r=r] f)
   876 qed
   877 
   878 lemma nn_integral_has_integral_lborel: 
   879   fixes f :: "'a::euclidean_space \<Rightarrow> real"
   880   assumes f_borel: "f \<in> borel_measurable borel" and nonneg: "\<And>x. 0 \<le> f x"
   881   assumes I: "(f has_integral I) UNIV"
   882   shows "integral\<^sup>N lborel f = I"
   883 proof -
   884   from f_borel have "(\<lambda>x. ereal (f x)) \<in> borel_measurable lborel" by auto
   885   from borel_measurable_implies_simple_function_sequence'[OF this] guess F . note F = this
   886   let ?B = "\<lambda>i::nat. box (- (real i *\<^sub>R One)) (real i *\<^sub>R One) :: 'a set"
   887 
   888   note F(1)[THEN borel_measurable_simple_function, measurable]
   889 
   890   { fix i x have "real (F i x) \<le> f x"
   891       using F(3,5) F(4)[of x, symmetric] nonneg
   892       unfolding real_le_ereal_iff
   893       by (auto simp: image_iff eq_commute[of \<infinity>] max_def intro: SUP_upper split: split_if_asm) }
   894   note F_le_f = this
   895   let ?F = "\<lambda>i x. F i x * indicator (?B i) x"
   896   have "(\<integral>\<^sup>+ x. ereal (f x) \<partial>lborel) = (SUP i. integral\<^sup>N lborel (\<lambda>x. ?F i x))"
   897   proof (subst nn_integral_monotone_convergence_SUP[symmetric])
   898     { fix x
   899       obtain j where j: "x \<in> ?B j"
   900         using UN_box_eq_UNIV by auto
   901 
   902       have "ereal (f x) = (SUP i. F i x)"
   903         using F(4)[of x] nonneg[of x] by (simp add: max_def)
   904       also have "\<dots> = (SUP i. ?F i x)"
   905       proof (rule SUP_eq)
   906         fix i show "\<exists>j\<in>UNIV. F i x \<le> ?F j x"
   907           using j F(2)
   908           by (intro bexI[of _ "max i j"])
   909              (auto split: split_max split_indicator simp: incseq_def le_fun_def box_def)
   910       qed (auto intro!: F split: split_indicator)
   911       finally have "ereal (f x) =  (SUP i. ?F i x)" . }
   912     then show "(\<integral>\<^sup>+ x. ereal (f x) \<partial>lborel) = (\<integral>\<^sup>+ x. (SUP i. ?F i x) \<partial>lborel)"
   913       by simp
   914   qed (insert F, auto simp: incseq_def le_fun_def box_def split: split_indicator)
   915   also have "\<dots> \<le> ereal I"
   916   proof (rule SUP_least)
   917     fix i :: nat
   918     have finite_F: "(\<integral>\<^sup>+ x. ereal (real (F i x) * indicator (?B i) x) \<partial>lborel) < \<infinity>"
   919     proof (rule nn_integral_bound_simple_function)
   920       have "emeasure lborel {x \<in> space lborel. ereal (real (F i x) * indicator (?B i) x) \<noteq> 0} \<le>
   921         emeasure lborel (?B i)"
   922         by (intro emeasure_mono)  (auto split: split_indicator)
   923       then show "emeasure lborel {x \<in> space lborel. ereal (real (F i x) * indicator (?B i) x) \<noteq> 0} < \<infinity>"  
   924         by auto
   925     qed (auto split: split_indicator
   926               intro!: real_of_ereal_pos F simple_function_compose1[where g="real"] simple_function_ereal)
   927 
   928     have int_F: "(\<lambda>x. real (F i x) * indicator (?B i) x) integrable_on UNIV"
   929       using F(5) finite_F
   930       by (intro nn_integral_integrable_on) (auto split: split_indicator intro: real_of_ereal_pos)
   931     
   932     have "(\<integral>\<^sup>+ x. F i x * indicator (?B i) x \<partial>lborel) = 
   933       (\<integral>\<^sup>+ x. ereal (real (F i x) * indicator (?B i) x) \<partial>lborel)"
   934       using F(3,5)
   935       by (intro nn_integral_cong) (auto simp: image_iff ereal_real eq_commute split: split_indicator)
   936     also have "\<dots> = ereal (integral UNIV (\<lambda>x. real (F i x) * indicator (?B i) x))"
   937       using F
   938       by (intro nn_integral_lborel_eq_integral[OF _ _ finite_F])
   939          (auto split: split_indicator intro: real_of_ereal_pos)
   940     also have "\<dots> \<le> ereal I"
   941       by (auto intro!: has_integral_le[OF integrable_integral[OF int_F] I] nonneg F_le_f
   942           split: split_indicator )
   943     finally show "(\<integral>\<^sup>+ x. F i x * indicator (?B i) x \<partial>lborel) \<le> ereal I" .
   944   qed
   945   finally have "(\<integral>\<^sup>+ x. ereal (f x) \<partial>lborel) < \<infinity>"
   946     by auto
   947   from nn_integral_lborel_eq_integral[OF assms(1,2) this] I show ?thesis
   948     by (simp add: integral_unique)
   949 qed
   950 
   951 lemma has_integral_iff_emeasure_lborel:
   952   fixes A :: "'a::euclidean_space set"
   953   assumes A[measurable]: "A \<in> sets borel"
   954   shows "((\<lambda>x. 1) has_integral r) A \<longleftrightarrow> emeasure lborel A = ereal r"
   955 proof cases
   956   assume emeasure_A: "emeasure lborel A = \<infinity>"
   957   have "\<not> (\<lambda>x. 1::real) integrable_on A"
   958   proof
   959     assume int: "(\<lambda>x. 1::real) integrable_on A"
   960     then have "(indicator A::'a \<Rightarrow> real) integrable_on UNIV"
   961       unfolding indicator_def[abs_def] integrable_restrict_univ .
   962     then obtain r where "((indicator A::'a\<Rightarrow>real) has_integral r) UNIV"
   963       by auto
   964     from nn_integral_has_integral_lborel[OF _ _ this] emeasure_A show False
   965       by (simp add: ereal_indicator)
   966   qed
   967   with emeasure_A show ?thesis
   968     by auto
   969 next
   970   assume "emeasure lborel A \<noteq> \<infinity>"
   971   moreover then have "((\<lambda>x. 1) has_integral measure lborel A) A"
   972     by (simp add: has_integral_measure_lborel)
   973   ultimately show ?thesis
   974     by (auto simp: emeasure_eq_ereal_measure has_integral_unique)
   975 qed
   976 
   977 lemma has_integral_integral_real:
   978   fixes f::"'a::euclidean_space \<Rightarrow> real"
   979   assumes f: "integrable lborel f"
   980   shows "(f has_integral (integral\<^sup>L lborel f)) UNIV"
   981 using f proof induct
   982   case (base A c) then show ?case
   983     by (auto intro!: has_integral_mult_left simp: )
   984        (simp add: emeasure_eq_ereal_measure indicator_def has_integral_measure_lborel)
   985 next
   986   case (add f g) then show ?case
   987     by (auto intro!: has_integral_add)
   988 next
   989   case (lim f s)
   990   show ?case
   991   proof (rule has_integral_dominated_convergence)
   992     show "\<And>i. (s i has_integral integral\<^sup>L lborel (s i)) UNIV" by fact
   993     show "(\<lambda>x. norm (2 * f x)) integrable_on UNIV"
   994       using `integrable lborel f`
   995       by (intro nn_integral_integrable_on)
   996          (auto simp: integrable_iff_bounded abs_mult times_ereal.simps(1)[symmetric] nn_integral_cmult
   997                simp del: times_ereal.simps)
   998     show "\<And>k. \<forall>x\<in>UNIV. norm (s k x) \<le> norm (2 * f x)"
   999       using lim by (auto simp add: abs_mult)
  1000     show "\<forall>x\<in>UNIV. (\<lambda>k. s k x) ----> f x"
  1001       using lim by auto
  1002     show "(\<lambda>k. integral\<^sup>L lborel (s k)) ----> integral\<^sup>L lborel f"
  1003       using lim lim(1)[THEN borel_measurable_integrable]
  1004       by (intro integral_dominated_convergence[where w="\<lambda>x. 2 * norm (f x)"]) auto
  1005   qed
  1006 qed
  1007 
  1008 context
  1009   fixes f::"'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1010 begin
  1011 
  1012 lemma has_integral_integral_lborel:
  1013   assumes f: "integrable lborel f"
  1014   shows "(f has_integral (integral\<^sup>L lborel f)) UNIV"
  1015 proof -
  1016   have "((\<lambda>x. \<Sum>b\<in>Basis. (f x \<bullet> b) *\<^sub>R b) has_integral (\<Sum>b\<in>Basis. integral\<^sup>L lborel (\<lambda>x. f x \<bullet> b) *\<^sub>R b)) UNIV"
  1017     using f by (intro has_integral_setsum finite_Basis ballI has_integral_scaleR_left has_integral_integral_real) auto
  1018   also have eq_f: "(\<lambda>x. \<Sum>b\<in>Basis. (f x \<bullet> b) *\<^sub>R b) = f"
  1019     by (simp add: fun_eq_iff euclidean_representation)
  1020   also have "(\<Sum>b\<in>Basis. integral\<^sup>L lborel (\<lambda>x. f x \<bullet> b) *\<^sub>R b) = integral\<^sup>L lborel f"
  1021     using f by (subst (2) eq_f[symmetric]) simp
  1022   finally show ?thesis .
  1023 qed
  1024 
  1025 lemma integrable_on_lborel: "integrable lborel f \<Longrightarrow> f integrable_on UNIV"
  1026   using has_integral_integral_lborel by (auto intro: has_integral_integrable)
  1027   
  1028 lemma integral_lborel: "integrable lborel f \<Longrightarrow> integral UNIV f = (\<integral>x. f x \<partial>lborel)"
  1029   using has_integral_integral_lborel by auto
  1030 
  1031 end
  1032 
  1033 subsection {* Fundamental Theorem of Calculus for the Lebesgue integral *}
  1034 
  1035 lemma emeasure_bounded_finite:
  1036   assumes "bounded A" shows "emeasure lborel A < \<infinity>"
  1037 proof -
  1038   from bounded_subset_cbox[OF `bounded A`] obtain a b where "A \<subseteq> cbox a b"
  1039     by auto
  1040   then have "emeasure lborel A \<le> emeasure lborel (cbox a b)"
  1041     by (intro emeasure_mono) auto
  1042   then show ?thesis
  1043     by (auto simp: emeasure_lborel_cbox_eq)
  1044 qed
  1045 
  1046 lemma emeasure_compact_finite: "compact A \<Longrightarrow> emeasure lborel A < \<infinity>"
  1047   using emeasure_bounded_finite[of A] by (auto intro: compact_imp_bounded)
  1048 
  1049 lemma borel_integrable_compact:
  1050   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::{banach, second_countable_topology}"
  1051   assumes "compact S" "continuous_on S f"
  1052   shows "integrable lborel (\<lambda>x. indicator S x *\<^sub>R f x)"
  1053 proof cases
  1054   assume "S \<noteq> {}"
  1055   have "continuous_on S (\<lambda>x. norm (f x))"
  1056     using assms by (intro continuous_intros)
  1057   from continuous_attains_sup[OF `compact S` `S \<noteq> {}` this]
  1058   obtain M where M: "\<And>x. x \<in> S \<Longrightarrow> norm (f x) \<le> M"
  1059     by auto
  1060 
  1061   show ?thesis
  1062   proof (rule integrable_bound)
  1063     show "integrable lborel (\<lambda>x. indicator S x * M)"
  1064       using assms by (auto intro!: emeasure_compact_finite borel_compact integrable_mult_left)
  1065     show "(\<lambda>x. indicator S x *\<^sub>R f x) \<in> borel_measurable lborel"
  1066       using assms by (auto intro!: borel_measurable_continuous_on_indicator borel_compact)
  1067     show "AE x in lborel. norm (indicator S x *\<^sub>R f x) \<le> norm (indicator S x * M)"
  1068       by (auto split: split_indicator simp: abs_real_def dest!: M)
  1069   qed
  1070 qed simp
  1071 
  1072 lemma borel_integrable_atLeastAtMost:
  1073   fixes f :: "real \<Rightarrow> real"
  1074   assumes f: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> isCont f x"
  1075   shows "integrable lborel (\<lambda>x. f x * indicator {a .. b} x)" (is "integrable _ ?f")
  1076 proof -
  1077   have "integrable lborel (\<lambda>x. indicator {a .. b} x *\<^sub>R f x)"
  1078   proof (rule borel_integrable_compact)
  1079     from f show "continuous_on {a..b} f"
  1080       by (auto intro: continuous_at_imp_continuous_on)
  1081   qed simp
  1082   then show ?thesis
  1083     by (auto simp: mult.commute)
  1084 qed
  1085 
  1086 text {*
  1087 
  1088 For the positive integral we replace continuity with Borel-measurability. 
  1089 
  1090 *}
  1091 
  1092 lemma
  1093   fixes f :: "real \<Rightarrow> real"
  1094   assumes [measurable]: "f \<in> borel_measurable borel"
  1095   assumes f: "\<And>x. x \<in> {a..b} \<Longrightarrow> DERIV F x :> f x" "\<And>x. x \<in> {a..b} \<Longrightarrow> 0 \<le> f x" and "a \<le> b"
  1096   shows nn_integral_FTC_Icc: "(\<integral>\<^sup>+x. ereal (f x) * indicator {a .. b} x \<partial>lborel) = F b - F a" (is ?nn)
  1097     and has_bochner_integral_FTC_Icc_nonneg:
  1098       "has_bochner_integral lborel (\<lambda>x. f x * indicator {a .. b} x) (F b - F a)" (is ?has)
  1099     and integral_FTC_Icc_nonneg: "(\<integral>x. f x * indicator {a .. b} x \<partial>lborel) = F b - F a" (is ?eq)
  1100     and integrable_FTC_Icc_nonneg: "integrable lborel (\<lambda>x. f x * indicator {a .. b} x)" (is ?int)
  1101 proof -
  1102   have *: "(\<lambda>x. f x * indicator {a..b} x) \<in> borel_measurable borel" "\<And>x. 0 \<le> f x * indicator {a..b} x"
  1103     using f(2) by (auto split: split_indicator)
  1104     
  1105   have "(f has_integral F b - F a) {a..b}"
  1106     by (intro fundamental_theorem_of_calculus)
  1107        (auto simp: has_field_derivative_iff_has_vector_derivative[symmetric]
  1108              intro: has_field_derivative_subset[OF f(1)] `a \<le> b`)
  1109   then have i: "((\<lambda>x. f x * indicator {a .. b} x) has_integral F b - F a) UNIV"
  1110     unfolding indicator_def if_distrib[where f="\<lambda>x. a * x" for a]
  1111     by (simp cong del: if_cong del: atLeastAtMost_iff)
  1112   then have nn: "(\<integral>\<^sup>+x. f x * indicator {a .. b} x \<partial>lborel) = F b - F a"
  1113     by (rule nn_integral_has_integral_lborel[OF *])
  1114   then show ?has
  1115     by (rule has_bochner_integral_nn_integral[rotated 2]) (simp_all add: *)
  1116   then show ?eq ?int
  1117     unfolding has_bochner_integral_iff by auto
  1118   from nn show ?nn
  1119     by (simp add: ereal_mult_indicator)
  1120 qed
  1121 
  1122 lemma
  1123   fixes f :: "real \<Rightarrow> 'a :: euclidean_space"
  1124   assumes "a \<le> b"
  1125   assumes "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> (F has_vector_derivative f x) (at x within {a .. b})"
  1126   assumes cont: "continuous_on {a .. b} f"
  1127   shows has_bochner_integral_FTC_Icc:
  1128       "has_bochner_integral lborel (\<lambda>x. indicator {a .. b} x *\<^sub>R f x) (F b - F a)" (is ?has)
  1129     and integral_FTC_Icc: "(\<integral>x. indicator {a .. b} x *\<^sub>R f x \<partial>lborel) = F b - F a" (is ?eq)
  1130 proof -
  1131   let ?f = "\<lambda>x. indicator {a .. b} x *\<^sub>R f x"
  1132   have int: "integrable lborel ?f"
  1133     using borel_integrable_compact[OF _ cont] by auto
  1134   have "(f has_integral F b - F a) {a..b}"
  1135     using assms(1,2) by (intro fundamental_theorem_of_calculus) auto
  1136   moreover 
  1137   have "(f has_integral integral\<^sup>L lborel ?f) {a..b}"
  1138     using has_integral_integral_lborel[OF int]
  1139     unfolding indicator_def if_distrib[where f="\<lambda>x. x *\<^sub>R a" for a]
  1140     by (simp cong del: if_cong del: atLeastAtMost_iff)
  1141   ultimately show ?eq
  1142     by (auto dest: has_integral_unique)
  1143   then show ?has
  1144     using int by (auto simp: has_bochner_integral_iff)
  1145 qed
  1146 
  1147 lemma
  1148   fixes f :: "real \<Rightarrow> real"
  1149   assumes "a \<le> b"
  1150   assumes deriv: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> DERIV F x :> f x"
  1151   assumes cont: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> isCont f x"
  1152   shows has_bochner_integral_FTC_Icc_real:
  1153       "has_bochner_integral lborel (\<lambda>x. f x * indicator {a .. b} x) (F b - F a)" (is ?has)
  1154     and integral_FTC_Icc_real: "(\<integral>x. f x * indicator {a .. b} x \<partial>lborel) = F b - F a" (is ?eq)
  1155 proof -
  1156   have 1: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> (F has_vector_derivative f x) (at x within {a .. b})"
  1157     unfolding has_field_derivative_iff_has_vector_derivative[symmetric]
  1158     using deriv by (auto intro: DERIV_subset)
  1159   have 2: "continuous_on {a .. b} f"
  1160     using cont by (intro continuous_at_imp_continuous_on) auto
  1161   show ?has ?eq
  1162     using has_bochner_integral_FTC_Icc[OF `a \<le> b` 1 2] integral_FTC_Icc[OF `a \<le> b` 1 2]
  1163     by (auto simp: mult.commute)
  1164 qed
  1165 
  1166 lemma nn_integral_FTC_atLeast:
  1167   fixes f :: "real \<Rightarrow> real"
  1168   assumes f_borel: "f \<in> borel_measurable borel"
  1169   assumes f: "\<And>x. a \<le> x \<Longrightarrow> DERIV F x :> f x" 
  1170   assumes nonneg: "\<And>x. a \<le> x \<Longrightarrow> 0 \<le> f x"
  1171   assumes lim: "(F ---> T) at_top"
  1172   shows "(\<integral>\<^sup>+x. ereal (f x) * indicator {a ..} x \<partial>lborel) = T - F a"
  1173 proof -
  1174   let ?f = "\<lambda>(i::nat) (x::real). ereal (f x) * indicator {a..a + real i} x"
  1175   let ?fR = "\<lambda>x. ereal (f x) * indicator {a ..} x"
  1176   have "\<And>x. (SUP i::nat. ?f i x) = ?fR x"
  1177   proof (rule SUP_Lim_ereal)
  1178     show "\<And>x. incseq (\<lambda>i. ?f i x)"
  1179       using nonneg by (auto simp: incseq_def le_fun_def split: split_indicator)
  1180 
  1181     fix x
  1182     from reals_Archimedean2[of "x - a"] guess n ..
  1183     then have "eventually (\<lambda>n. ?f n x = ?fR x) sequentially"
  1184       by (auto intro!: eventually_sequentiallyI[where c=n] split: split_indicator)
  1185     then show "(\<lambda>n. ?f n x) ----> ?fR x"
  1186       by (rule Lim_eventually)
  1187   qed
  1188   then have "integral\<^sup>N lborel ?fR = (\<integral>\<^sup>+ x. (SUP i::nat. ?f i x) \<partial>lborel)"
  1189     by simp
  1190   also have "\<dots> = (SUP i::nat. (\<integral>\<^sup>+ x. ?f i x \<partial>lborel))"
  1191   proof (rule nn_integral_monotone_convergence_SUP)
  1192     show "incseq ?f"
  1193       using nonneg by (auto simp: incseq_def le_fun_def split: split_indicator)
  1194     show "\<And>i. (?f i) \<in> borel_measurable lborel"
  1195       using f_borel by auto
  1196     show "\<And>i x. 0 \<le> ?f i x"
  1197       using nonneg by (auto split: split_indicator)
  1198   qed
  1199   also have "\<dots> = (SUP i::nat. ereal (F (a + real i) - F a))"
  1200     by (subst nn_integral_FTC_Icc[OF f_borel f nonneg]) auto
  1201   also have "\<dots> = T - F a"
  1202   proof (rule SUP_Lim_ereal)
  1203     show "incseq (\<lambda>n. ereal (F (a + real n) - F a))"
  1204     proof (simp add: incseq_def, safe)
  1205       fix m n :: nat assume "m \<le> n"
  1206       with f nonneg show "F (a + real m) \<le> F (a + real n)"
  1207         by (intro DERIV_nonneg_imp_nondecreasing[where f=F])
  1208            (simp, metis add_increasing2 order_refl order_trans real_of_nat_ge_zero)
  1209     qed 
  1210     have "(\<lambda>x. F (a + real x)) ----> T"
  1211       apply (rule filterlim_compose[OF lim filterlim_tendsto_add_at_top])
  1212       apply (rule LIMSEQ_const_iff[THEN iffD2, OF refl])
  1213       apply (rule filterlim_real_sequentially)
  1214       done
  1215     then show "(\<lambda>n. ereal (F (a + real n) - F a)) ----> ereal (T - F a)"
  1216       unfolding lim_ereal
  1217       by (intro tendsto_diff) auto
  1218   qed
  1219   finally show ?thesis .
  1220 qed
  1221 
  1222 lemma integral_power:
  1223   "a \<le> b \<Longrightarrow> (\<integral>x. x^k * indicator {a..b} x \<partial>lborel) = (b^Suc k - a^Suc k) / Suc k"
  1224 proof (subst integral_FTC_Icc_real)
  1225   fix x show "DERIV (\<lambda>x. x^Suc k / Suc k) x :> x^k"
  1226     by (intro derivative_eq_intros) auto
  1227 qed (auto simp: field_simps)
  1228 
  1229 subsection {* Integration by parts *}
  1230 
  1231 lemma integral_by_parts_integrable:
  1232   fixes f g F G::"real \<Rightarrow> real"
  1233   assumes "a \<le> b"
  1234   assumes cont_f[intro]: "!!x. a \<le>x \<Longrightarrow> x\<le>b \<Longrightarrow> isCont f x"
  1235   assumes cont_g[intro]: "!!x. a \<le>x \<Longrightarrow> x\<le>b \<Longrightarrow> isCont g x"
  1236   assumes [intro]: "!!x. DERIV F x :> f x"
  1237   assumes [intro]: "!!x. DERIV G x :> g x"
  1238   shows  "integrable lborel (\<lambda>x.((F x) * (g x) + (f x) * (G x)) * indicator {a .. b} x)"
  1239   by (auto intro!: borel_integrable_atLeastAtMost continuous_intros) (auto intro!: DERIV_isCont)
  1240 
  1241 lemma integral_by_parts:
  1242   fixes f g F G::"real \<Rightarrow> real"
  1243   assumes [arith]: "a \<le> b"
  1244   assumes cont_f[intro]: "!!x. a \<le>x \<Longrightarrow> x\<le>b \<Longrightarrow> isCont f x"
  1245   assumes cont_g[intro]: "!!x. a \<le>x \<Longrightarrow> x\<le>b \<Longrightarrow> isCont g x"
  1246   assumes [intro]: "!!x. DERIV F x :> f x"
  1247   assumes [intro]: "!!x. DERIV G x :> g x"
  1248   shows "(\<integral>x. (F x * g x) * indicator {a .. b} x \<partial>lborel)
  1249             =  F b * G b - F a * G a - \<integral>x. (f x * G x) * indicator {a .. b} x \<partial>lborel" 
  1250 proof-
  1251   have 0: "(\<integral>x. (F x * g x + f x * G x) * indicator {a .. b} x \<partial>lborel) = F b * G b - F a * G a"
  1252     by (rule integral_FTC_Icc_real, auto intro!: derivative_eq_intros continuous_intros) 
  1253       (auto intro!: DERIV_isCont)
  1254 
  1255   have "(\<integral>x. (F x * g x + f x * G x) * indicator {a .. b} x \<partial>lborel) =
  1256     (\<integral>x. (F x * g x) * indicator {a .. b} x \<partial>lborel) + \<integral>x. (f x * G x) * indicator {a .. b} x \<partial>lborel"
  1257     apply (subst integral_add[symmetric])
  1258     apply (auto intro!: borel_integrable_atLeastAtMost continuous_intros)
  1259     by (auto intro!: DERIV_isCont integral_cong split:split_indicator)
  1260 
  1261   thus ?thesis using 0 by auto
  1262 qed
  1263 
  1264 lemma integral_by_parts':
  1265   fixes f g F G::"real \<Rightarrow> real"
  1266   assumes "a \<le> b"
  1267   assumes "!!x. a \<le>x \<Longrightarrow> x\<le>b \<Longrightarrow> isCont f x"
  1268   assumes "!!x. a \<le>x \<Longrightarrow> x\<le>b \<Longrightarrow> isCont g x"
  1269   assumes "!!x. DERIV F x :> f x"
  1270   assumes "!!x. DERIV G x :> g x"
  1271   shows "(\<integral>x. indicator {a .. b} x *\<^sub>R (F x * g x) \<partial>lborel)
  1272             =  F b * G b - F a * G a - \<integral>x. indicator {a .. b} x *\<^sub>R (f x * G x) \<partial>lborel" 
  1273   using integral_by_parts[OF assms] by (simp add: mult_ac)
  1274 
  1275 lemma has_bochner_integral_even_function:
  1276   fixes f :: "real \<Rightarrow> 'a :: {banach, second_countable_topology}"
  1277   assumes f: "has_bochner_integral lborel (\<lambda>x. indicator {0..} x *\<^sub>R f x) x"
  1278   assumes even: "\<And>x. f (- x) = f x"
  1279   shows "has_bochner_integral lborel f (2 *\<^sub>R x)"
  1280 proof -
  1281   have indicator: "\<And>x::real. indicator {..0} (- x) = indicator {0..} x"
  1282     by (auto split: split_indicator)
  1283   have "has_bochner_integral lborel (\<lambda>x. indicator {.. 0} x *\<^sub>R f x) x"
  1284     by (subst lborel_has_bochner_integral_real_affine_iff[where c="-1" and t=0])
  1285        (auto simp: indicator even f)
  1286   with f have "has_bochner_integral lborel (\<lambda>x. indicator {0..} x *\<^sub>R f x + indicator {.. 0} x *\<^sub>R f x) (x + x)"
  1287     by (rule has_bochner_integral_add)
  1288   then have "has_bochner_integral lborel f (x + x)"
  1289     by (rule has_bochner_integral_discrete_difference[where X="{0}", THEN iffD1, rotated 4])
  1290        (auto split: split_indicator)
  1291   then show ?thesis
  1292     by (simp add: scaleR_2)
  1293 qed
  1294 
  1295 lemma has_bochner_integral_odd_function:
  1296   fixes f :: "real \<Rightarrow> 'a :: {banach, second_countable_topology}"
  1297   assumes f: "has_bochner_integral lborel (\<lambda>x. indicator {0..} x *\<^sub>R f x) x"
  1298   assumes odd: "\<And>x. f (- x) = - f x"
  1299   shows "has_bochner_integral lborel f 0"
  1300 proof -
  1301   have indicator: "\<And>x::real. indicator {..0} (- x) = indicator {0..} x"
  1302     by (auto split: split_indicator)
  1303   have "has_bochner_integral lborel (\<lambda>x. - indicator {.. 0} x *\<^sub>R f x) x"
  1304     by (subst lborel_has_bochner_integral_real_affine_iff[where c="-1" and t=0])
  1305        (auto simp: indicator odd f)
  1306   from has_bochner_integral_minus[OF this]
  1307   have "has_bochner_integral lborel (\<lambda>x. indicator {.. 0} x *\<^sub>R f x) (- x)"
  1308     by simp 
  1309   with f have "has_bochner_integral lborel (\<lambda>x. indicator {0..} x *\<^sub>R f x + indicator {.. 0} x *\<^sub>R f x) (x + - x)"
  1310     by (rule has_bochner_integral_add)
  1311   then have "has_bochner_integral lborel f (x + - x)"
  1312     by (rule has_bochner_integral_discrete_difference[where X="{0}", THEN iffD1, rotated 4])
  1313        (auto split: split_indicator)
  1314   then show ?thesis
  1315     by simp
  1316 qed
  1317 
  1318 end
  1319