src/HOL/Probability/Lebesgue_Measure.thy
author haftmann
Fri Jun 19 07:53:35 2015 +0200 (2015-06-19)
changeset 60517 f16e4fb20652
parent 59741 5b762cd73a8e
child 60615 e5fa1d5d3952
permissions -rw-r--r--
separate class for notions specific for integral (semi)domains, in contrast to fields where these are trivial
     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 section {* Lebesgue 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 unfolding mult.commute [of 2] by force
   195     then obtain a' where a'lea [arith]: "a' > a" and 
   196       a_prop: "F a' - F a < epsilon / 2"
   197       by auto
   198     def S' \<equiv> "{i. l i < r i}"
   199     obtain S :: "nat set" where 
   200       "S \<subseteq> S'" and finS: "finite S" and 
   201       Sprop: "{a'..b} \<subseteq> (\<Union>i \<in> S. {l i<..<r i + delta i})"
   202     proof (rule compactE_image)
   203       show "compact {a'..b}"
   204         by (rule compact_Icc)
   205       show "\<forall>i \<in> S'. open ({l i<..<r i + delta i})" by auto
   206       have "{a'..b} \<subseteq> {a <.. b}"
   207         by auto
   208       also have "{a <.. b} = (\<Union>i\<in>S'. {l i<..r i})"
   209         unfolding lr_eq_ab[symmetric] by (fastforce simp add: S'_def intro: less_le_trans)
   210       also have "\<dots> \<subseteq> (\<Union>i \<in> S'. {l i<..<r i + delta i})"
   211         apply (intro UN_mono)
   212         apply (auto simp: S'_def)
   213         apply (cut_tac i=i in deltai_gt0)
   214         apply simp
   215         done
   216       finally show "{a'..b} \<subseteq> (\<Union>i \<in> S'. {l i<..<r i + delta i})" .
   217     qed
   218     with S'_def have Sprop2: "\<And>i. i \<in> S \<Longrightarrow> l i < r i" by auto
   219     from finS have "\<exists>n. \<forall>i \<in> S. i \<le> n" 
   220       by (subst finite_nat_set_iff_bounded_le [symmetric])
   221     then obtain n where Sbound [rule_format]: "\<forall>i \<in> S. i \<le> n" ..
   222     have "F b - F a' \<le> (\<Sum>i\<in>S. F (r i + delta i) - F (l i))"
   223       apply (rule claim2 [rule_format])
   224       using finS Sprop apply auto
   225       apply (frule Sprop2)
   226       apply (subgoal_tac "delta i > 0")
   227       apply arith
   228       by (rule deltai_gt0)
   229     also have "... \<le> (SUM i : S. F(r i) - F(l i) + epsilon / 2^(i+2))"
   230       apply (rule setsum_mono)
   231       apply simp
   232       apply (rule order_trans)
   233       apply (rule less_imp_le)
   234       apply (rule deltai_prop)
   235       by auto
   236     also have "... = (SUM i : S. F(r i) - F(l i)) + 
   237         (epsilon / 4) * (SUM i : S. (1 / 2)^i)" (is "_ = ?t + _")
   238       by (subst setsum.distrib) (simp add: field_simps setsum_right_distrib)
   239     also have "... \<le> ?t + (epsilon / 4) * (\<Sum> i < Suc n. (1 / 2)^i)"
   240       apply (rule add_left_mono)
   241       apply (rule mult_left_mono)
   242       apply (rule setsum_mono2)
   243       using egt0 apply auto 
   244       by (frule Sbound, auto)
   245     also have "... \<le> ?t + (epsilon / 2)"
   246       apply (rule add_left_mono)
   247       apply (subst geometric_sum)
   248       apply auto
   249       apply (rule mult_left_mono)
   250       using egt0 apply auto
   251       done
   252     finally have aux2: "F b - F a' \<le> (\<Sum>i\<in>S. F (r i) - F (l i)) + epsilon / 2"
   253       by simp
   254 
   255     have "F b - F a = (F b - F a') + (F a' - F a)"
   256       by auto
   257     also have "... \<le> (F b - F a') + epsilon / 2"
   258       using a_prop by (intro add_left_mono) simp
   259     also have "... \<le> (\<Sum>i\<in>S. F (r i) - F (l i)) + epsilon / 2 + epsilon / 2"
   260       apply (intro add_right_mono)
   261       apply (rule aux2)
   262       done
   263     also have "... = (\<Sum>i\<in>S. F (r i) - F (l i)) + epsilon"
   264       by auto
   265     also have "... \<le> (\<Sum>i\<le>n. F (r i) - F (l i)) + epsilon"
   266       using finS Sbound Sprop by (auto intro!: add_right_mono setsum_mono3)
   267     finally have "ereal (F b - F a) \<le> (\<Sum>i\<le>n. ereal (F (r i) - F (l i))) + epsilon"
   268       by simp
   269     then have "ereal (F b - F a) \<le> (\<Sum>i. ereal (F (r i) - F (l i))) + (epsilon :: real)"
   270       apply (rule_tac order_trans)
   271       prefer 2
   272       apply (rule add_mono[where c="ereal epsilon"])
   273       apply (rule suminf_upper[of _ "Suc n"])
   274       apply (auto simp add: lessThan_Suc_atMost)
   275       done }
   276   hence "ereal (F b - F a) \<le> (\<Sum>i. ereal (F (r i) - F (l i)))"
   277     by (auto intro: ereal_le_epsilon2)
   278   moreover
   279   have "(\<Sum>i. ereal (F (r i) - F (l i))) \<le> ereal (F b - F a)"
   280     by (auto simp add: claim1 intro!: suminf_bound)
   281   ultimately show "(\<Sum>n. ereal (F (r n) - F (l n))) = ereal (F b - F a)"
   282     by simp
   283 qed (auto simp: Ioc_inj diff_le_iff mono_F)
   284 
   285 lemma measure_interval_measure_Ioc:
   286   assumes "a \<le> b"
   287   assumes mono_F: "\<And>x y. x \<le> y \<Longrightarrow> F x \<le> F y"
   288   assumes right_cont_F : "\<And>a. continuous (at_right a) F" 
   289   shows "measure (interval_measure F) {a <.. b} = F b - F a"
   290   unfolding measure_def
   291   apply (subst emeasure_interval_measure_Ioc)
   292   apply fact+
   293   apply simp
   294   done
   295 
   296 lemma emeasure_interval_measure_Ioc_eq:
   297   "(\<And>x y. x \<le> y \<Longrightarrow> F x \<le> F y) \<Longrightarrow> (\<And>a. continuous (at_right a) F) \<Longrightarrow>
   298     emeasure (interval_measure F) {a <.. b} = (if a \<le> b then F b - F a else 0)"
   299   using emeasure_interval_measure_Ioc[of a b F] by auto
   300 
   301 lemma sets_interval_measure [simp, measurable_cong]: "sets (interval_measure F) = sets borel"
   302   apply (simp add: sets_extend_measure interval_measure_def borel_sigma_sets_Ioc)
   303   apply (rule sigma_sets_eqI)
   304   apply auto
   305   apply (case_tac "a \<le> ba")
   306   apply (auto intro: sigma_sets.Empty)
   307   done
   308 
   309 lemma space_interval_measure [simp]: "space (interval_measure F) = UNIV"
   310   by (simp add: interval_measure_def space_extend_measure)
   311 
   312 lemma emeasure_interval_measure_Icc:
   313   assumes "a \<le> b"
   314   assumes mono_F: "\<And>x y. x \<le> y \<Longrightarrow> F x \<le> F y"
   315   assumes cont_F : "continuous_on UNIV F" 
   316   shows "emeasure (interval_measure F) {a .. b} = F b - F a"
   317 proof (rule tendsto_unique)
   318   { fix a b :: real assume "a \<le> b" then have "emeasure (interval_measure F) {a <.. b} = F b - F a"
   319       using cont_F
   320       by (subst emeasure_interval_measure_Ioc)
   321          (auto intro: mono_F continuous_within_subset simp: continuous_on_eq_continuous_within) }
   322   note * = this
   323 
   324   let ?F = "interval_measure F"
   325   show "((\<lambda>a. F b - F a) ---> emeasure ?F {a..b}) (at_left a)"
   326   proof (rule tendsto_at_left_sequentially)
   327     show "a - 1 < a" by simp
   328     fix X assume "\<And>n. X n < a" "incseq X" "X ----> a"
   329     with `a \<le> b` have "(\<lambda>n. emeasure ?F {X n<..b}) ----> emeasure ?F (\<Inter>n. {X n <..b})"
   330       apply (intro Lim_emeasure_decseq)
   331       apply (auto simp: decseq_def incseq_def emeasure_interval_measure_Ioc *)
   332       apply force
   333       apply (subst (asm ) *)
   334       apply (auto intro: less_le_trans less_imp_le)
   335       done
   336     also have "(\<Inter>n. {X n <..b}) = {a..b}"
   337       using `\<And>n. X n < a`
   338       apply auto
   339       apply (rule LIMSEQ_le_const2[OF `X ----> a`])
   340       apply (auto intro: less_imp_le)
   341       apply (auto intro: less_le_trans)
   342       done
   343     also have "(\<lambda>n. emeasure ?F {X n<..b}) = (\<lambda>n. F b - F (X n))"
   344       using `\<And>n. X n < a` `a \<le> b` by (subst *) (auto intro: less_imp_le less_le_trans)
   345     finally show "(\<lambda>n. F b - F (X n)) ----> emeasure ?F {a..b}" .
   346   qed
   347   show "((\<lambda>a. ereal (F b - F a)) ---> F b - F a) (at_left a)"
   348     using cont_F
   349     by (intro lim_ereal[THEN iffD2] tendsto_intros )
   350        (auto simp: continuous_on_def intro: tendsto_within_subset)
   351 qed (rule trivial_limit_at_left_real)
   352   
   353 lemma sigma_finite_interval_measure:
   354   assumes mono_F: "\<And>x y. x \<le> y \<Longrightarrow> F x \<le> F y"
   355   assumes right_cont_F : "\<And>a. continuous (at_right a) F" 
   356   shows "sigma_finite_measure (interval_measure F)"
   357   apply unfold_locales
   358   apply (intro exI[of _ "(\<lambda>(a, b). {a <.. b}) ` (\<rat> \<times> \<rat>)"])
   359   apply (auto intro!: Rats_no_top_le Rats_no_bot_less countable_rat simp: emeasure_interval_measure_Ioc_eq[OF assms])
   360   done
   361 
   362 subsection {* Lebesgue-Borel measure *}
   363 
   364 definition lborel :: "('a :: euclidean_space) measure" where
   365   "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)"
   366 
   367 lemma 
   368   shows sets_lborel[simp, measurable_cong]: "sets lborel = sets borel"
   369     and space_lborel[simp]: "space lborel = space borel"
   370     and measurable_lborel1[simp]: "measurable M lborel = measurable M borel"
   371     and measurable_lborel2[simp]: "measurable lborel M = measurable borel M"
   372   by (simp_all add: lborel_def)
   373 
   374 context
   375 begin
   376 
   377 interpretation sigma_finite_measure "interval_measure (\<lambda>x. x)"
   378   by (rule sigma_finite_interval_measure) auto
   379 interpretation finite_product_sigma_finite "\<lambda>_. interval_measure (\<lambda>x. x)" Basis
   380   proof qed simp
   381 
   382 lemma lborel_eq_real: "lborel = interval_measure (\<lambda>x. x)"
   383   unfolding lborel_def Basis_real_def
   384   using distr_id[of "interval_measure (\<lambda>x. x)"]
   385   by (subst distr_component[symmetric])
   386      (simp_all add: distr_distr comp_def del: distr_id cong: distr_cong)
   387 
   388 lemma lborel_eq: "lborel = distr (\<Pi>\<^sub>M b\<in>Basis. lborel) borel (\<lambda>f. \<Sum>b\<in>Basis. f b *\<^sub>R b)"
   389   by (subst lborel_def) (simp add: lborel_eq_real)
   390 
   391 lemma nn_integral_lborel_setprod:
   392   assumes [measurable]: "\<And>b. b \<in> Basis \<Longrightarrow> f b \<in> borel_measurable borel"
   393   assumes nn[simp]: "\<And>b x. b \<in> Basis \<Longrightarrow> 0 \<le> f b x"
   394   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))"
   395   by (simp add: lborel_def nn_integral_distr product_nn_integral_setprod
   396                 product_nn_integral_singleton)
   397 
   398 lemma emeasure_lborel_Icc[simp]: 
   399   fixes l u :: real
   400   assumes [simp]: "l \<le> u"
   401   shows "emeasure lborel {l .. u} = u - l"
   402 proof -
   403   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}"
   404     by (auto simp: space_PiM)
   405   then show ?thesis
   406     by (simp add: lborel_def emeasure_distr emeasure_PiM emeasure_interval_measure_Icc continuous_on_id)
   407 qed
   408 
   409 lemma emeasure_lborel_Icc_eq: "emeasure lborel {l .. u} = ereal (if l \<le> u then u - l else 0)"
   410   by simp
   411 
   412 lemma emeasure_lborel_cbox[simp]:
   413   assumes [simp]: "\<And>b. b \<in> Basis \<Longrightarrow> l \<bullet> b \<le> u \<bullet> b"
   414   shows "emeasure lborel (cbox l u) = (\<Prod>b\<in>Basis. (u - l) \<bullet> b)"
   415 proof -
   416   have "(\<lambda>x. \<Prod>b\<in>Basis. indicator {l\<bullet>b .. u\<bullet>b} (x \<bullet> b) :: ereal) = indicator (cbox l u)"
   417     by (auto simp: fun_eq_iff cbox_def setprod_ereal_0 split: split_indicator)
   418   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)"
   419     by simp
   420   also have "\<dots> = (\<Prod>b\<in>Basis. (u - l) \<bullet> b)"
   421     by (subst nn_integral_lborel_setprod) (simp_all add: setprod_ereal inner_diff_left)
   422   finally show ?thesis .
   423 qed
   424 
   425 lemma AE_lborel_singleton: "AE x in lborel::'a::euclidean_space measure. x \<noteq> c"
   426   using SOME_Basis AE_discrete_difference [of "{c}" lborel]
   427     emeasure_lborel_cbox [of c c] by (auto simp add: cbox_sing)
   428 
   429 lemma emeasure_lborel_Ioo[simp]:
   430   assumes [simp]: "l \<le> u"
   431   shows "emeasure lborel {l <..< u} = ereal (u - l)"
   432 proof -
   433   have "emeasure lborel {l <..< u} = emeasure lborel {l .. u}"
   434     using AE_lborel_singleton[of u] AE_lborel_singleton[of l] by (intro emeasure_eq_AE) auto
   435   then show ?thesis
   436     by simp
   437 qed
   438 
   439 lemma emeasure_lborel_Ioc[simp]:
   440   assumes [simp]: "l \<le> u"
   441   shows "emeasure lborel {l <.. u} = ereal (u - l)"
   442 proof -
   443   have "emeasure lborel {l <.. u} = emeasure lborel {l .. u}"
   444     using AE_lborel_singleton[of u] AE_lborel_singleton[of l] by (intro emeasure_eq_AE) auto
   445   then show ?thesis
   446     by simp
   447 qed
   448 
   449 lemma emeasure_lborel_Ico[simp]:
   450   assumes [simp]: "l \<le> u"
   451   shows "emeasure lborel {l ..< u} = ereal (u - l)"
   452 proof -
   453   have "emeasure lborel {l ..< u} = emeasure lborel {l .. u}"
   454     using AE_lborel_singleton[of u] AE_lborel_singleton[of l] by (intro emeasure_eq_AE) auto
   455   then show ?thesis
   456     by simp
   457 qed
   458 
   459 lemma emeasure_lborel_box[simp]:
   460   assumes [simp]: "\<And>b. b \<in> Basis \<Longrightarrow> l \<bullet> b \<le> u \<bullet> b"
   461   shows "emeasure lborel (box l u) = (\<Prod>b\<in>Basis. (u - l) \<bullet> b)"
   462 proof -
   463   have "(\<lambda>x. \<Prod>b\<in>Basis. indicator {l\<bullet>b <..< u\<bullet>b} (x \<bullet> b) :: ereal) = indicator (box l u)"
   464     by (auto simp: fun_eq_iff box_def setprod_ereal_0 split: split_indicator)
   465   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)"
   466     by simp
   467   also have "\<dots> = (\<Prod>b\<in>Basis. (u - l) \<bullet> b)"
   468     by (subst nn_integral_lborel_setprod) (simp_all add: setprod_ereal inner_diff_left)
   469   finally show ?thesis .
   470 qed
   471 
   472 lemma emeasure_lborel_cbox_eq:
   473   "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)"
   474   using box_eq_empty(2)[THEN iffD2, of u l] by (auto simp: not_le)
   475 
   476 lemma emeasure_lborel_box_eq:
   477   "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)"
   478   using box_eq_empty(1)[THEN iffD2, of u l] by (auto simp: not_le dest!: less_imp_le) force
   479 
   480 lemma
   481   fixes l u :: real
   482   assumes [simp]: "l \<le> u"
   483   shows measure_lborel_Icc[simp]: "measure lborel {l .. u} = u - l"
   484     and measure_lborel_Ico[simp]: "measure lborel {l ..< u} = u - l"
   485     and measure_lborel_Ioc[simp]: "measure lborel {l <.. u} = u - l"
   486     and measure_lborel_Ioo[simp]: "measure lborel {l <..< u} = u - l"
   487   by (simp_all add: measure_def)
   488 
   489 lemma 
   490   assumes [simp]: "\<And>b. b \<in> Basis \<Longrightarrow> l \<bullet> b \<le> u \<bullet> b"
   491   shows measure_lborel_box[simp]: "measure lborel (box l u) = (\<Prod>b\<in>Basis. (u - l) \<bullet> b)"
   492     and measure_lborel_cbox[simp]: "measure lborel (cbox l u) = (\<Prod>b\<in>Basis. (u - l) \<bullet> b)"
   493   by (simp_all add: measure_def)
   494 
   495 lemma sigma_finite_lborel: "sigma_finite_measure lborel"
   496 proof
   497   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>)"
   498     by (intro exI[of _ "range (\<lambda>n::nat. box (- real n *\<^sub>R One) (real n *\<^sub>R One))"])
   499        (auto simp: emeasure_lborel_cbox_eq UN_box_eq_UNIV)
   500 qed
   501 
   502 end
   503 
   504 lemma emeasure_lborel_UNIV: "emeasure lborel (UNIV::'a::euclidean_space set) = \<infinity>"
   505 proof -
   506   { fix n::nat
   507     let ?Ba = "Basis :: 'a set"
   508     have "real n \<le> (2::real) ^ card ?Ba * real n"
   509       by (simp add: mult_le_cancel_right1)
   510     also 
   511     have "... \<le> (2::real) ^ card ?Ba * real (Suc n) ^ card ?Ba"
   512       apply (rule mult_left_mono)
   513       apply (metis DIM_positive One_nat_def less_eq_Suc_le less_imp_le real_of_nat_le_iff real_of_nat_power self_le_power zero_less_Suc)
   514       apply (simp add: DIM_positive)
   515       done
   516     finally have "real n \<le> (2::real) ^ card ?Ba * real (Suc n) ^ card ?Ba" .
   517   } note [intro!] = this
   518   show ?thesis
   519     unfolding UN_box_eq_UNIV[symmetric]
   520     apply (subst SUP_emeasure_incseq[symmetric])
   521     apply (auto simp: incseq_def subset_box inner_add_left setprod_constant 
   522                intro!: SUP_PInfty)
   523     done 
   524 qed
   525 
   526 lemma emeasure_lborel_singleton[simp]: "emeasure lborel {x} = 0"
   527   using emeasure_lborel_cbox[of x x] nonempty_Basis
   528   by (auto simp del: emeasure_lborel_cbox nonempty_Basis simp add: cbox_sing)
   529 
   530 lemma emeasure_lborel_countable:
   531   fixes A :: "'a::euclidean_space set"
   532   assumes "countable A"
   533   shows "emeasure lborel A = 0"
   534 proof -
   535   have "A \<subseteq> (\<Union>i. {from_nat_into A i})" using from_nat_into_surj assms by force
   536   moreover have "emeasure lborel (\<Union>i. {from_nat_into A i}) = 0"
   537     by (rule emeasure_UN_eq_0) auto
   538   ultimately have "emeasure lborel A \<le> 0" using emeasure_mono
   539     by (metis assms bot.extremum_unique emeasure_empty image_eq_UN range_from_nat_into sets.empty_sets)
   540   thus ?thesis by (auto simp add: emeasure_le_0_iff)
   541 qed
   542 
   543 lemma countable_imp_null_set_lborel: "countable A \<Longrightarrow> A \<in> null_sets lborel"
   544   by (simp add: null_sets_def emeasure_lborel_countable sets.countable)
   545 
   546 lemma finite_imp_null_set_lborel: "finite A \<Longrightarrow> A \<in> null_sets lborel"
   547   by (intro countable_imp_null_set_lborel countable_finite)
   548 
   549 lemma lborel_neq_count_space[simp]: "lborel \<noteq> count_space (A::('a::ordered_euclidean_space) set)"
   550 proof
   551   assume asm: "lborel = count_space A"
   552   have "space lborel = UNIV" by simp
   553   hence [simp]: "A = UNIV" by (subst (asm) asm) (simp only: space_count_space)
   554   have "emeasure lborel {undefined::'a} = 1" 
   555       by (subst asm, subst emeasure_count_space_finite) auto
   556   moreover have "emeasure lborel {undefined} \<noteq> 1" by simp
   557   ultimately show False by contradiction
   558 qed
   559 
   560 subsection {* Affine transformation on the Lebesgue-Borel *}
   561 
   562 lemma lborel_eqI:
   563   fixes M :: "'a::euclidean_space measure"
   564   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)"
   565   assumes sets_eq: "sets M = sets borel"
   566   shows "lborel = M"
   567 proof (rule measure_eqI_generator_eq)
   568   let ?E = "range (\<lambda>(a, b). box a b::'a set)"
   569   show "Int_stable ?E"
   570     by (auto simp: Int_stable_def box_Int_box)
   571 
   572   show "?E \<subseteq> Pow UNIV" "sets lborel = sigma_sets UNIV ?E" "sets M = sigma_sets UNIV ?E"
   573     by (simp_all add: borel_eq_box sets_eq)
   574 
   575   let ?A = "\<lambda>n::nat. box (- (real n *\<^sub>R One)) (real n *\<^sub>R One) :: 'a set"
   576   show "range ?A \<subseteq> ?E" "(\<Union>i. ?A i) = UNIV"
   577     unfolding UN_box_eq_UNIV by auto
   578 
   579   { fix i show "emeasure lborel (?A i) \<noteq> \<infinity>" by auto }
   580   { fix X assume "X \<in> ?E" then show "emeasure lborel X = emeasure M X"
   581       apply (auto simp: emeasure_eq emeasure_lborel_box_eq )
   582       apply (subst box_eq_empty(1)[THEN iffD2])
   583       apply (auto intro: less_imp_le simp: not_le)
   584       done }
   585 qed
   586 
   587 lemma lborel_affine:
   588   fixes t :: "'a::euclidean_space" assumes "c \<noteq> 0"
   589   shows "lborel = density (distr lborel borel (\<lambda>x. t + c *\<^sub>R x)) (\<lambda>_. \<bar>c\<bar>^DIM('a))" (is "_ = ?D")
   590 proof (rule lborel_eqI)
   591   let ?B = "Basis :: 'a set"
   592   fix l u assume le: "\<And>b. b \<in> ?B \<Longrightarrow> l \<bullet> b \<le> u \<bullet> b"
   593   show "emeasure ?D (box l u) = (\<Prod>b\<in>?B. (u - l) \<bullet> b)"
   594   proof cases
   595     assume "0 < c"
   596     then have "(\<lambda>x. t + c *\<^sub>R x) -` box l u = box ((l - t) /\<^sub>R c) ((u - t) /\<^sub>R c)"
   597       by (auto simp: field_simps box_def inner_simps)
   598     with `0 < c` show ?thesis
   599       using le
   600       by (auto simp: field_simps emeasure_density nn_integral_distr nn_integral_cmult
   601                      emeasure_lborel_box_eq inner_simps setprod_dividef setprod_constant
   602                      borel_measurable_indicator' emeasure_distr)
   603   next
   604     assume "\<not> 0 < c" with `c \<noteq> 0` have "c < 0" by auto
   605     then have "box ((u - t) /\<^sub>R c) ((l - t) /\<^sub>R c) = (\<lambda>x. t + c *\<^sub>R x) -` box l u"
   606       by (auto simp: field_simps box_def inner_simps)
   607     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)"
   608       by (auto split: split_indicator)
   609     moreover
   610     { have "(- c) ^ card ?B * (\<Prod>x\<in>?B. l \<bullet> x - u \<bullet> x) = 
   611          (-1 * c) ^ card ?B * (\<Prod>x\<in>?B. -1 * (u \<bullet> x - l \<bullet> x))"
   612          by simp
   613       also have "\<dots> = (-1 * -1)^card ?B * c ^ card ?B * (\<Prod>x\<in>?B. u \<bullet> x - l \<bullet> x)"
   614         unfolding setprod.distrib power_mult_distrib by (simp add: setprod_constant)
   615       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)"
   616         by simp }
   617     ultimately show ?thesis
   618       using `c < 0` le
   619       by (auto simp: field_simps emeasure_density nn_integral_distr nn_integral_cmult
   620                      emeasure_lborel_box_eq inner_simps setprod_dividef setprod_constant
   621                      borel_measurable_indicator' emeasure_distr)
   622   qed
   623 qed simp
   624 
   625 lemma lborel_real_affine:
   626   "c \<noteq> 0 \<Longrightarrow> lborel = density (distr lborel borel (\<lambda>x. t + c * x)) (\<lambda>_. \<bar>c\<bar>)"
   627   using lborel_affine[of c t] by simp
   628 
   629 lemma AE_borel_affine: 
   630   fixes P :: "real \<Rightarrow> bool"
   631   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)"
   632   by (subst lborel_real_affine[where t="- t / c" and c="1 / c"])
   633      (simp_all add: AE_density AE_distr_iff field_simps)
   634 
   635 lemma nn_integral_real_affine:
   636   fixes c :: real assumes [measurable]: "f \<in> borel_measurable borel" and c: "c \<noteq> 0"
   637   shows "(\<integral>\<^sup>+x. f x \<partial>lborel) = \<bar>c\<bar> * (\<integral>\<^sup>+x. f (t + c * x) \<partial>lborel)"
   638   by (subst lborel_real_affine[OF c, of t])
   639      (simp add: nn_integral_density nn_integral_distr nn_integral_cmult)
   640 
   641 lemma lborel_integrable_real_affine:
   642   fixes f :: "real \<Rightarrow> 'a :: {banach, second_countable_topology}"
   643   assumes f: "integrable lborel f"
   644   shows "c \<noteq> 0 \<Longrightarrow> integrable lborel (\<lambda>x. f (t + c * x))"
   645   using f f[THEN borel_measurable_integrable] unfolding integrable_iff_bounded
   646   by (subst (asm) nn_integral_real_affine[where c=c and t=t]) auto
   647 
   648 lemma lborel_integrable_real_affine_iff:
   649   fixes f :: "real \<Rightarrow> 'a :: {banach, second_countable_topology}"
   650   shows "c \<noteq> 0 \<Longrightarrow> integrable lborel (\<lambda>x. f (t + c * x)) \<longleftrightarrow> integrable lborel f"
   651   using
   652     lborel_integrable_real_affine[of f c t]
   653     lborel_integrable_real_affine[of "\<lambda>x. f (t + c * x)" "1/c" "-t/c"]
   654   by (auto simp add: field_simps)
   655 
   656 lemma lborel_integral_real_affine:
   657   fixes f :: "real \<Rightarrow> 'a :: {banach, second_countable_topology}" and c :: real
   658   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)"
   659 proof cases
   660   assume f[measurable]: "integrable lborel f" then show ?thesis
   661     using c f f[THEN borel_measurable_integrable] f[THEN lborel_integrable_real_affine, of c t]
   662     by (subst lborel_real_affine[OF c, of t])
   663        (simp add: integral_density integral_distr)
   664 next
   665   assume "\<not> integrable lborel f" with c show ?thesis
   666     by (simp add: lborel_integrable_real_affine_iff not_integrable_integral_eq)
   667 qed
   668 
   669 lemma divideR_right: 
   670   fixes x y :: "'a::real_normed_vector"
   671   shows "r \<noteq> 0 \<Longrightarrow> y = x /\<^sub>R r \<longleftrightarrow> r *\<^sub>R y = x"
   672   using scaleR_cancel_left[of r y "x /\<^sub>R r"] by simp
   673 
   674 lemma lborel_has_bochner_integral_real_affine_iff:
   675   fixes x :: "'a :: {banach, second_countable_topology}"
   676   shows "c \<noteq> 0 \<Longrightarrow>
   677     has_bochner_integral lborel f x \<longleftrightarrow>
   678     has_bochner_integral lborel (\<lambda>x. f (t + c * x)) (x /\<^sub>R \<bar>c\<bar>)"
   679   unfolding has_bochner_integral_iff lborel_integrable_real_affine_iff
   680   by (simp_all add: lborel_integral_real_affine[symmetric] divideR_right cong: conj_cong)
   681 
   682 lemma lborel_distr_uminus: "distr lborel borel uminus = (lborel :: real measure)"
   683   by (subst lborel_real_affine[of "-1" 0]) 
   684      (auto simp: density_1 one_ereal_def[symmetric])
   685 
   686 lemma lborel_distr_mult: 
   687   assumes "(c::real) \<noteq> 0"
   688   shows "distr lborel borel (op * c) = density lborel (\<lambda>_. inverse \<bar>c\<bar>)"
   689 proof-
   690   have "distr lborel borel (op * c) = distr lborel lborel (op * c)" by (simp cong: distr_cong)
   691   also from assms have "... = density lborel (\<lambda>_. inverse \<bar>c\<bar>)"
   692     by (subst lborel_real_affine[of "inverse c" 0]) (auto simp: o_def distr_density_distr)
   693   finally show ?thesis .
   694 qed
   695 
   696 lemma lborel_distr_mult': 
   697   assumes "(c::real) \<noteq> 0"
   698   shows "lborel = density (distr lborel borel (op * c)) (\<lambda>_. abs c)"
   699 proof-
   700   have "lborel = density lborel (\<lambda>_. 1)" by (rule density_1[symmetric])
   701   also from assms have "(\<lambda>_. 1 :: ereal) = (\<lambda>_. inverse (abs c) * abs c)" by (intro ext) simp
   702   also have "density lborel ... = density (density lborel (\<lambda>_. inverse (abs c))) (\<lambda>_. abs c)"
   703     by (subst density_density_eq) auto
   704   also from assms have "density lborel (\<lambda>_. inverse (abs c)) = distr lborel borel (op * c)"
   705     by (rule lborel_distr_mult[symmetric])
   706   finally show ?thesis .
   707 qed
   708 
   709 lemma lborel_distr_plus: "distr lborel borel (op + c) = (lborel :: real measure)"
   710   by (subst lborel_real_affine[of 1 c]) (auto simp: density_1 one_ereal_def[symmetric])
   711 
   712 interpretation lborel!: sigma_finite_measure lborel
   713   by (rule sigma_finite_lborel)
   714 
   715 interpretation lborel_pair: pair_sigma_finite lborel lborel ..
   716 
   717 lemma lborel_prod:
   718   "lborel \<Otimes>\<^sub>M lborel = (lborel :: ('a::euclidean_space \<times> 'b::euclidean_space) measure)"
   719 proof (rule lborel_eqI[symmetric], clarify)
   720   fix la ua :: 'a and lb ub :: 'b
   721   assume lu: "\<And>a b. (a, b) \<in> Basis \<Longrightarrow> (la, lb) \<bullet> (a, b) \<le> (ua, ub) \<bullet> (a, b)"
   722   have [simp]:
   723     "\<And>b. b \<in> Basis \<Longrightarrow> la \<bullet> b \<le> ua \<bullet> b"
   724     "\<And>b. b \<in> Basis \<Longrightarrow> lb \<bullet> b \<le> ub \<bullet> b"
   725     "inj_on (\<lambda>u. (u, 0)) Basis" "inj_on (\<lambda>u. (0, u)) Basis"
   726     "(\<lambda>u. (u, 0)) ` Basis \<inter> (\<lambda>u. (0, u)) ` Basis = {}"
   727     "box (la, lb) (ua, ub) = box la ua \<times> box lb ub"
   728     using lu[of _ 0] lu[of 0] by (auto intro!: inj_onI simp add: Basis_prod_def ball_Un box_def)
   729   show "emeasure (lborel \<Otimes>\<^sub>M lborel) (box (la, lb) (ua, ub)) =
   730       ereal (setprod (op \<bullet> ((ua, ub) - (la, lb))) Basis)"
   731     by (simp add: lborel.emeasure_pair_measure_Times Basis_prod_def setprod.union_disjoint
   732                   setprod.reindex)
   733 qed (simp add: borel_prod[symmetric])
   734 
   735 (* FIXME: conversion in measurable prover *)
   736 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
   737 lemma lborelD[measurable (raw)]: "A \<in> sets borel \<Longrightarrow> A \<in> sets lborel" by simp
   738 
   739 subsection {* Equivalence Lebesgue integral on @{const lborel} and HK-integral *}
   740 
   741 lemma has_integral_measure_lborel:
   742   fixes A :: "'a::euclidean_space set"
   743   assumes A[measurable]: "A \<in> sets borel" and finite: "emeasure lborel A < \<infinity>"
   744   shows "((\<lambda>x. 1) has_integral measure lborel A) A"
   745 proof -
   746   { fix l u :: 'a
   747     have "((\<lambda>x. 1) has_integral measure lborel (box l u)) (box l u)"
   748     proof cases
   749       assume "\<forall>b\<in>Basis. l \<bullet> b \<le> u \<bullet> b"
   750       then show ?thesis
   751         apply simp
   752         apply (subst has_integral_restrict[symmetric, OF box_subset_cbox])
   753         apply (subst has_integral_spike_interior_eq[where g="\<lambda>_. 1"])
   754         using has_integral_const[of "1::real" l u]
   755         apply (simp_all add: inner_diff_left[symmetric] content_cbox_cases)
   756         done
   757     next
   758       assume "\<not> (\<forall>b\<in>Basis. l \<bullet> b \<le> u \<bullet> b)"
   759       then have "box l u = {}"
   760         unfolding box_eq_empty by (auto simp: not_le intro: less_imp_le)
   761       then show ?thesis
   762         by simp
   763     qed }
   764   note has_integral_box = this
   765 
   766   { fix a b :: 'a let ?M = "\<lambda>A. measure lborel (A \<inter> box a b)"
   767     have "Int_stable  (range (\<lambda>(a, b). box a b))"
   768       by (auto simp: Int_stable_def box_Int_box)
   769     moreover have "(range (\<lambda>(a, b). box a b)) \<subseteq> Pow UNIV"
   770       by auto
   771     moreover have "A \<in> sigma_sets UNIV (range (\<lambda>(a, b). box a b))"
   772        using A unfolding borel_eq_box by simp
   773     ultimately have "((\<lambda>x. 1) has_integral ?M A) (A \<inter> box a b)"
   774     proof (induction rule: sigma_sets_induct_disjoint)
   775       case (basic A) then show ?case
   776         by (auto simp: box_Int_box has_integral_box)
   777     next
   778       case empty then show ?case
   779         by simp
   780     next
   781       case (compl A)
   782       then have [measurable]: "A \<in> sets borel"
   783         by (simp add: borel_eq_box)
   784 
   785       have "((\<lambda>x. 1) has_integral ?M (box a b)) (box a b)"
   786         by (simp add: has_integral_box)
   787       moreover have "((\<lambda>x. if x \<in> A \<inter> box a b then 1 else 0) has_integral ?M A) (box a b)"
   788         by (subst has_integral_restrict) (auto intro: compl)
   789       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)"
   790         by (rule has_integral_sub)
   791       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)"
   792         by (rule has_integral_eq_eq[THEN iffD1, rotated 1]) auto
   793       then have "((\<lambda>x. 1) has_integral ?M (box a b) - ?M A) ((UNIV - A) \<inter> box a b)"
   794         by (subst (asm) has_integral_restrict) auto
   795       also have "?M (box a b) - ?M A = ?M (UNIV - A)"
   796         by (subst measure_Diff[symmetric]) (auto simp: emeasure_lborel_box_eq Diff_Int_distrib2)
   797       finally show ?case .
   798     next
   799       case (union F)
   800       then have [measurable]: "\<And>i. F i \<in> sets borel"
   801         by (simp add: borel_eq_box subset_eq)
   802       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)"
   803       proof (rule has_integral_monotone_convergence_increasing)
   804         let ?f = "\<lambda>k x. \<Sum>i<k. if x \<in> F i \<inter> box a b then 1 else 0 :: real"
   805         show "\<And>k. (?f k has_integral (\<Sum>i<k. ?M (F i))) (box a b)"
   806           using union.IH by (auto intro!: has_integral_setsum simp del: Int_iff)
   807         show "\<And>k x. ?f k x \<le> ?f (Suc k) x"
   808           by (intro setsum_mono2) auto
   809         from union(1) have *: "\<And>x i j. x \<in> F i \<Longrightarrow> x \<in> F j \<longleftrightarrow> j = i"
   810           by (auto simp add: disjoint_family_on_def)
   811         show "\<And>x. (\<lambda>k. ?f k x) ----> (if x \<in> UNION UNIV F \<inter> box a b then 1 else 0)"
   812           apply (auto simp: * setsum.If_cases Iio_Int_singleton)
   813           apply (rule_tac k="Suc xa" in LIMSEQ_offset)
   814           apply (simp add: tendsto_const)
   815           done
   816         have *: "emeasure lborel ((\<Union>x. F x) \<inter> box a b) \<le> emeasure lborel (box a b)"
   817           by (intro emeasure_mono) auto
   818 
   819         with union(1) show "(\<lambda>k. \<Sum>i<k. ?M (F i)) ----> ?M (\<Union>i. F i)"
   820           unfolding sums_def[symmetric] UN_extend_simps
   821           by (intro measure_UNION) (auto simp: disjoint_family_on_def emeasure_lborel_box_eq)
   822       qed
   823       then show ?case
   824         by (subst (asm) has_integral_restrict) auto
   825     qed }
   826   note * = this
   827 
   828   show ?thesis
   829   proof (rule has_integral_monotone_convergence_increasing)
   830     let ?B = "\<lambda>n::nat. box (- real n *\<^sub>R One) (real n *\<^sub>R One) :: 'a set"
   831     let ?f = "\<lambda>n::nat. \<lambda>x. if x \<in> A \<inter> ?B n then 1 else 0 :: real"
   832     let ?M = "\<lambda>n. measure lborel (A \<inter> ?B n)"
   833 
   834     show "\<And>n::nat. (?f n has_integral ?M n) A"
   835       using * by (subst has_integral_restrict) simp_all
   836     show "\<And>k x. ?f k x \<le> ?f (Suc k) x"
   837       by (auto simp: box_def)
   838     { fix x assume "x \<in> A"
   839       moreover have "(\<lambda>k. indicator (A \<inter> ?B k) x :: real) ----> indicator (\<Union>k::nat. A \<inter> ?B k) x"
   840         by (intro LIMSEQ_indicator_incseq) (auto simp: incseq_def box_def)
   841       ultimately show "(\<lambda>k. if x \<in> A \<inter> ?B k then 1 else 0::real) ----> 1"
   842         by (simp add: indicator_def UN_box_eq_UNIV) }
   843 
   844     have "(\<lambda>n. emeasure lborel (A \<inter> ?B n)) ----> emeasure lborel (\<Union>n::nat. A \<inter> ?B n)"
   845       by (intro Lim_emeasure_incseq) (auto simp: incseq_def box_def)
   846     also have "(\<lambda>n. emeasure lborel (A \<inter> ?B n)) = (\<lambda>n. measure lborel (A \<inter> ?B n))"
   847     proof (intro ext emeasure_eq_ereal_measure)
   848       fix n have "emeasure lborel (A \<inter> ?B n) \<le> emeasure lborel (?B n)"
   849         by (intro emeasure_mono) auto
   850       then show "emeasure lborel (A \<inter> ?B n) \<noteq> \<infinity>"
   851         by auto
   852     qed
   853     finally show "(\<lambda>n. measure lborel (A \<inter> ?B n)) ----> measure lborel A"
   854       using emeasure_eq_ereal_measure[of lborel A] finite
   855       by (simp add: UN_box_eq_UNIV)
   856   qed
   857 qed
   858 
   859 lemma nn_integral_has_integral:
   860   fixes f::"'a::euclidean_space \<Rightarrow> real"
   861   assumes f: "f \<in> borel_measurable borel" "\<And>x. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>lborel) = ereal r"
   862   shows "(f has_integral r) UNIV"
   863 using f proof (induct arbitrary: r rule: borel_measurable_induct_real)
   864   case (set A)
   865   moreover then have "((\<lambda>x. 1) has_integral measure lborel A) A"
   866     by (intro has_integral_measure_lborel) (auto simp: ereal_indicator)
   867   ultimately show ?case
   868     by (simp add: ereal_indicator measure_def) (simp add: indicator_def)
   869 next
   870   case (mult g c)
   871   then have "ereal c * (\<integral>\<^sup>+ x. g x \<partial>lborel) = ereal r"
   872     by (subst nn_integral_cmult[symmetric]) auto
   873   then obtain r' where "(c = 0 \<and> r = 0) \<or> ((\<integral>\<^sup>+ x. ereal (g x) \<partial>lborel) = ereal r' \<and> r = c * r')"
   874     by (cases "\<integral>\<^sup>+ x. ereal (g x) \<partial>lborel") (auto split: split_if_asm)
   875   with mult show ?case
   876     by (auto intro!: has_integral_cmult_real)
   877 next
   878   case (add g h)
   879   moreover
   880   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)"
   881     unfolding plus_ereal.simps[symmetric] by (subst nn_integral_add) auto
   882   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"
   883     by (cases "\<integral>\<^sup>+ x. h x \<partial>lborel" "\<integral>\<^sup>+ x. g x \<partial>lborel" rule: ereal2_cases) auto
   884   ultimately show ?case
   885     by (auto intro!: has_integral_add)
   886 next
   887   case (seq U)
   888   note seq(1)[measurable] and f[measurable]
   889 
   890   { fix i x 
   891     have "U i x \<le> f x"
   892       using seq(5)
   893       apply (rule LIMSEQ_le_const)
   894       using seq(4)
   895       apply (auto intro!: exI[of _ i] simp: incseq_def le_fun_def)
   896       done }
   897   note U_le_f = this
   898   
   899   { fix i
   900     have "(\<integral>\<^sup>+x. ereal (U i x) \<partial>lborel) \<le> (\<integral>\<^sup>+x. ereal (f x) \<partial>lborel)"
   901       using U_le_f by (intro nn_integral_mono) simp
   902     then obtain p where "(\<integral>\<^sup>+x. U i x \<partial>lborel) = ereal p" "p \<le> r"
   903       using seq(6) by (cases "\<integral>\<^sup>+x. U i x \<partial>lborel") auto
   904     moreover then have "0 \<le> p"
   905       by (metis ereal_less_eq(5) nn_integral_nonneg)
   906     moreover note seq
   907     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"
   908       by auto }
   909   then obtain p where p: "\<And>i. (\<integral>\<^sup>+x. ereal (U i x) \<partial>lborel) = ereal (p i)"
   910     and bnd: "\<And>i. p i \<le> r" "\<And>i. 0 \<le> p i"
   911     and U_int: "\<And>i.(U i has_integral (p i)) UNIV" by metis
   912 
   913   have int_eq: "\<And>i. integral UNIV (U i) = p i" using U_int by (rule integral_unique)
   914 
   915   have *: "f integrable_on UNIV \<and> (\<lambda>k. integral UNIV (U k)) ----> integral UNIV f"
   916   proof (rule monotone_convergence_increasing)
   917     show "\<forall>k. U k integrable_on UNIV" using U_int by auto
   918     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)
   919     then show "bounded {integral UNIV (U k) |k. True}"
   920       using bnd int_eq by (auto simp: bounded_real intro!: exI[of _ r])
   921     show "\<forall>x\<in>UNIV. (\<lambda>k. U k x) ----> f x"
   922       using seq by auto
   923   qed
   924   moreover have "(\<lambda>i. (\<integral>\<^sup>+x. U i x \<partial>lborel)) ----> (\<integral>\<^sup>+x. f x \<partial>lborel)"
   925     using seq U_le_f by (intro nn_integral_dominated_convergence[where w=f]) auto
   926   ultimately have "integral UNIV f = r"
   927     by (auto simp add: int_eq p seq intro: LIMSEQ_unique)
   928   with * show ?case
   929     by (simp add: has_integral_integral)
   930 qed
   931 
   932 lemma nn_integral_lborel_eq_integral:
   933   fixes f::"'a::euclidean_space \<Rightarrow> real"
   934   assumes f: "f \<in> borel_measurable borel" "\<And>x. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>lborel) < \<infinity>"
   935   shows "(\<integral>\<^sup>+x. f x \<partial>lborel) = integral UNIV f"
   936 proof -
   937   from f(3) obtain r where r: "(\<integral>\<^sup>+x. f x \<partial>lborel) = ereal r"
   938     by (cases "\<integral>\<^sup>+x. f x \<partial>lborel") auto
   939   then show ?thesis
   940     using nn_integral_has_integral[OF f(1,2) r] by (simp add: integral_unique)
   941 qed
   942 
   943 lemma nn_integral_integrable_on:
   944   fixes f::"'a::euclidean_space \<Rightarrow> real"
   945   assumes f: "f \<in> borel_measurable borel" "\<And>x. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>lborel) < \<infinity>"
   946   shows "f integrable_on UNIV"
   947 proof -
   948   from f(3) obtain r where r: "(\<integral>\<^sup>+x. f x \<partial>lborel) = ereal r"
   949     by (cases "\<integral>\<^sup>+x. f x \<partial>lborel") auto
   950   then show ?thesis
   951     by (intro has_integral_integrable[where i=r] nn_integral_has_integral[where r=r] f)
   952 qed
   953 
   954 lemma nn_integral_has_integral_lborel: 
   955   fixes f :: "'a::euclidean_space \<Rightarrow> real"
   956   assumes f_borel: "f \<in> borel_measurable borel" and nonneg: "\<And>x. 0 \<le> f x"
   957   assumes I: "(f has_integral I) UNIV"
   958   shows "integral\<^sup>N lborel f = I"
   959 proof -
   960   from f_borel have "(\<lambda>x. ereal (f x)) \<in> borel_measurable lborel" by auto
   961   from borel_measurable_implies_simple_function_sequence'[OF this] guess F . note F = this
   962   let ?B = "\<lambda>i::nat. box (- (real i *\<^sub>R One)) (real i *\<^sub>R One) :: 'a set"
   963 
   964   note F(1)[THEN borel_measurable_simple_function, measurable]
   965 
   966   { fix i x have "real (F i x) \<le> f x"
   967       using F(3,5) F(4)[of x, symmetric] nonneg
   968       unfolding real_le_ereal_iff
   969       by (auto simp: image_iff eq_commute[of \<infinity>] max_def intro: SUP_upper split: split_if_asm) }
   970   note F_le_f = this
   971   let ?F = "\<lambda>i x. F i x * indicator (?B i) x"
   972   have "(\<integral>\<^sup>+ x. ereal (f x) \<partial>lborel) = (SUP i. integral\<^sup>N lborel (\<lambda>x. ?F i x))"
   973   proof (subst nn_integral_monotone_convergence_SUP[symmetric])
   974     { fix x
   975       obtain j where j: "x \<in> ?B j"
   976         using UN_box_eq_UNIV by auto
   977 
   978       have "ereal (f x) = (SUP i. F i x)"
   979         using F(4)[of x] nonneg[of x] by (simp add: max_def)
   980       also have "\<dots> = (SUP i. ?F i x)"
   981       proof (rule SUP_eq)
   982         fix i show "\<exists>j\<in>UNIV. F i x \<le> ?F j x"
   983           using j F(2)
   984           by (intro bexI[of _ "max i j"])
   985              (auto split: split_max split_indicator simp: incseq_def le_fun_def box_def)
   986       qed (auto intro!: F split: split_indicator)
   987       finally have "ereal (f x) =  (SUP i. ?F i x)" . }
   988     then show "(\<integral>\<^sup>+ x. ereal (f x) \<partial>lborel) = (\<integral>\<^sup>+ x. (SUP i. ?F i x) \<partial>lborel)"
   989       by simp
   990   qed (insert F, auto simp: incseq_def le_fun_def box_def split: split_indicator)
   991   also have "\<dots> \<le> ereal I"
   992   proof (rule SUP_least)
   993     fix i :: nat
   994     have finite_F: "(\<integral>\<^sup>+ x. ereal (real (F i x) * indicator (?B i) x) \<partial>lborel) < \<infinity>"
   995     proof (rule nn_integral_bound_simple_function)
   996       have "emeasure lborel {x \<in> space lborel. ereal (real (F i x) * indicator (?B i) x) \<noteq> 0} \<le>
   997         emeasure lborel (?B i)"
   998         by (intro emeasure_mono)  (auto split: split_indicator)
   999       then show "emeasure lborel {x \<in> space lborel. ereal (real (F i x) * indicator (?B i) x) \<noteq> 0} < \<infinity>"  
  1000         by auto
  1001     qed (auto split: split_indicator
  1002               intro!: real_of_ereal_pos F simple_function_compose1[where g="real"] simple_function_ereal)
  1003 
  1004     have int_F: "(\<lambda>x. real (F i x) * indicator (?B i) x) integrable_on UNIV"
  1005       using F(5) finite_F
  1006       by (intro nn_integral_integrable_on) (auto split: split_indicator intro: real_of_ereal_pos)
  1007     
  1008     have "(\<integral>\<^sup>+ x. F i x * indicator (?B i) x \<partial>lborel) = 
  1009       (\<integral>\<^sup>+ x. ereal (real (F i x) * indicator (?B i) x) \<partial>lborel)"
  1010       using F(3,5)
  1011       by (intro nn_integral_cong) (auto simp: image_iff ereal_real eq_commute split: split_indicator)
  1012     also have "\<dots> = ereal (integral UNIV (\<lambda>x. real (F i x) * indicator (?B i) x))"
  1013       using F
  1014       by (intro nn_integral_lborel_eq_integral[OF _ _ finite_F])
  1015          (auto split: split_indicator intro: real_of_ereal_pos)
  1016     also have "\<dots> \<le> ereal I"
  1017       by (auto intro!: has_integral_le[OF integrable_integral[OF int_F] I] nonneg F_le_f
  1018           split: split_indicator )
  1019     finally show "(\<integral>\<^sup>+ x. F i x * indicator (?B i) x \<partial>lborel) \<le> ereal I" .
  1020   qed
  1021   finally have "(\<integral>\<^sup>+ x. ereal (f x) \<partial>lborel) < \<infinity>"
  1022     by auto
  1023   from nn_integral_lborel_eq_integral[OF assms(1,2) this] I show ?thesis
  1024     by (simp add: integral_unique)
  1025 qed
  1026 
  1027 lemma has_integral_iff_emeasure_lborel:
  1028   fixes A :: "'a::euclidean_space set"
  1029   assumes A[measurable]: "A \<in> sets borel"
  1030   shows "((\<lambda>x. 1) has_integral r) A \<longleftrightarrow> emeasure lborel A = ereal r"
  1031 proof cases
  1032   assume emeasure_A: "emeasure lborel A = \<infinity>"
  1033   have "\<not> (\<lambda>x. 1::real) integrable_on A"
  1034   proof
  1035     assume int: "(\<lambda>x. 1::real) integrable_on A"
  1036     then have "(indicator A::'a \<Rightarrow> real) integrable_on UNIV"
  1037       unfolding indicator_def[abs_def] integrable_restrict_univ .
  1038     then obtain r where "((indicator A::'a\<Rightarrow>real) has_integral r) UNIV"
  1039       by auto
  1040     from nn_integral_has_integral_lborel[OF _ _ this] emeasure_A show False
  1041       by (simp add: ereal_indicator)
  1042   qed
  1043   with emeasure_A show ?thesis
  1044     by auto
  1045 next
  1046   assume "emeasure lborel A \<noteq> \<infinity>"
  1047   moreover then have "((\<lambda>x. 1) has_integral measure lborel A) A"
  1048     by (simp add: has_integral_measure_lborel)
  1049   ultimately show ?thesis
  1050     by (auto simp: emeasure_eq_ereal_measure has_integral_unique)
  1051 qed
  1052 
  1053 lemma has_integral_integral_real:
  1054   fixes f::"'a::euclidean_space \<Rightarrow> real"
  1055   assumes f: "integrable lborel f"
  1056   shows "(f has_integral (integral\<^sup>L lborel f)) UNIV"
  1057 using f proof induct
  1058   case (base A c) then show ?case
  1059     by (auto intro!: has_integral_mult_left simp: )
  1060        (simp add: emeasure_eq_ereal_measure indicator_def has_integral_measure_lborel)
  1061 next
  1062   case (add f g) then show ?case
  1063     by (auto intro!: has_integral_add)
  1064 next
  1065   case (lim f s)
  1066   show ?case
  1067   proof (rule has_integral_dominated_convergence)
  1068     show "\<And>i. (s i has_integral integral\<^sup>L lborel (s i)) UNIV" by fact
  1069     show "(\<lambda>x. norm (2 * f x)) integrable_on UNIV"
  1070       using `integrable lborel f`
  1071       by (intro nn_integral_integrable_on)
  1072          (auto simp: integrable_iff_bounded abs_mult times_ereal.simps(1)[symmetric] nn_integral_cmult
  1073                simp del: times_ereal.simps)
  1074     show "\<And>k. \<forall>x\<in>UNIV. norm (s k x) \<le> norm (2 * f x)"
  1075       using lim by (auto simp add: abs_mult)
  1076     show "\<forall>x\<in>UNIV. (\<lambda>k. s k x) ----> f x"
  1077       using lim by auto
  1078     show "(\<lambda>k. integral\<^sup>L lborel (s k)) ----> integral\<^sup>L lborel f"
  1079       using lim lim(1)[THEN borel_measurable_integrable]
  1080       by (intro integral_dominated_convergence[where w="\<lambda>x. 2 * norm (f x)"]) auto
  1081   qed
  1082 qed
  1083 
  1084 context
  1085   fixes f::"'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1086 begin
  1087 
  1088 lemma has_integral_integral_lborel:
  1089   assumes f: "integrable lborel f"
  1090   shows "(f has_integral (integral\<^sup>L lborel f)) UNIV"
  1091 proof -
  1092   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"
  1093     using f by (intro has_integral_setsum finite_Basis ballI has_integral_scaleR_left has_integral_integral_real) auto
  1094   also have eq_f: "(\<lambda>x. \<Sum>b\<in>Basis. (f x \<bullet> b) *\<^sub>R b) = f"
  1095     by (simp add: fun_eq_iff euclidean_representation)
  1096   also have "(\<Sum>b\<in>Basis. integral\<^sup>L lborel (\<lambda>x. f x \<bullet> b) *\<^sub>R b) = integral\<^sup>L lborel f"
  1097     using f by (subst (2) eq_f[symmetric]) simp
  1098   finally show ?thesis .
  1099 qed
  1100 
  1101 lemma integrable_on_lborel: "integrable lborel f \<Longrightarrow> f integrable_on UNIV"
  1102   using has_integral_integral_lborel by (auto intro: has_integral_integrable)
  1103   
  1104 lemma integral_lborel: "integrable lborel f \<Longrightarrow> integral UNIV f = (\<integral>x. f x \<partial>lborel)"
  1105   using has_integral_integral_lborel by auto
  1106 
  1107 end
  1108 
  1109 subsection {* Fundamental Theorem of Calculus for the Lebesgue integral *}
  1110 
  1111 lemma emeasure_bounded_finite:
  1112   assumes "bounded A" shows "emeasure lborel A < \<infinity>"
  1113 proof -
  1114   from bounded_subset_cbox[OF `bounded A`] obtain a b where "A \<subseteq> cbox a b"
  1115     by auto
  1116   then have "emeasure lborel A \<le> emeasure lborel (cbox a b)"
  1117     by (intro emeasure_mono) auto
  1118   then show ?thesis
  1119     by (auto simp: emeasure_lborel_cbox_eq)
  1120 qed
  1121 
  1122 lemma emeasure_compact_finite: "compact A \<Longrightarrow> emeasure lborel A < \<infinity>"
  1123   using emeasure_bounded_finite[of A] by (auto intro: compact_imp_bounded)
  1124 
  1125 lemma borel_integrable_compact:
  1126   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::{banach, second_countable_topology}"
  1127   assumes "compact S" "continuous_on S f"
  1128   shows "integrable lborel (\<lambda>x. indicator S x *\<^sub>R f x)"
  1129 proof cases
  1130   assume "S \<noteq> {}"
  1131   have "continuous_on S (\<lambda>x. norm (f x))"
  1132     using assms by (intro continuous_intros)
  1133   from continuous_attains_sup[OF `compact S` `S \<noteq> {}` this]
  1134   obtain M where M: "\<And>x. x \<in> S \<Longrightarrow> norm (f x) \<le> M"
  1135     by auto
  1136 
  1137   show ?thesis
  1138   proof (rule integrable_bound)
  1139     show "integrable lborel (\<lambda>x. indicator S x * M)"
  1140       using assms by (auto intro!: emeasure_compact_finite borel_compact integrable_mult_left)
  1141     show "(\<lambda>x. indicator S x *\<^sub>R f x) \<in> borel_measurable lborel"
  1142       using assms by (auto intro!: borel_measurable_continuous_on_indicator borel_compact)
  1143     show "AE x in lborel. norm (indicator S x *\<^sub>R f x) \<le> norm (indicator S x * M)"
  1144       by (auto split: split_indicator simp: abs_real_def dest!: M)
  1145   qed
  1146 qed simp
  1147 
  1148 lemma borel_integrable_atLeastAtMost:
  1149   fixes f :: "real \<Rightarrow> real"
  1150   assumes f: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> isCont f x"
  1151   shows "integrable lborel (\<lambda>x. f x * indicator {a .. b} x)" (is "integrable _ ?f")
  1152 proof -
  1153   have "integrable lborel (\<lambda>x. indicator {a .. b} x *\<^sub>R f x)"
  1154   proof (rule borel_integrable_compact)
  1155     from f show "continuous_on {a..b} f"
  1156       by (auto intro: continuous_at_imp_continuous_on)
  1157   qed simp
  1158   then show ?thesis
  1159     by (auto simp: mult.commute)
  1160 qed
  1161 
  1162 text {*
  1163 
  1164 For the positive integral we replace continuity with Borel-measurability. 
  1165 
  1166 *}
  1167 
  1168 lemma
  1169   fixes f :: "real \<Rightarrow> real"
  1170   assumes [measurable]: "f \<in> borel_measurable borel"
  1171   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"
  1172   shows nn_integral_FTC_Icc: "(\<integral>\<^sup>+x. ereal (f x) * indicator {a .. b} x \<partial>lborel) = F b - F a" (is ?nn)
  1173     and has_bochner_integral_FTC_Icc_nonneg:
  1174       "has_bochner_integral lborel (\<lambda>x. f x * indicator {a .. b} x) (F b - F a)" (is ?has)
  1175     and integral_FTC_Icc_nonneg: "(\<integral>x. f x * indicator {a .. b} x \<partial>lborel) = F b - F a" (is ?eq)
  1176     and integrable_FTC_Icc_nonneg: "integrable lborel (\<lambda>x. f x * indicator {a .. b} x)" (is ?int)
  1177 proof -
  1178   have *: "(\<lambda>x. f x * indicator {a..b} x) \<in> borel_measurable borel" "\<And>x. 0 \<le> f x * indicator {a..b} x"
  1179     using f(2) by (auto split: split_indicator)
  1180     
  1181   have "(f has_integral F b - F a) {a..b}"
  1182     by (intro fundamental_theorem_of_calculus)
  1183        (auto simp: has_field_derivative_iff_has_vector_derivative[symmetric]
  1184              intro: has_field_derivative_subset[OF f(1)] `a \<le> b`)
  1185   then have i: "((\<lambda>x. f x * indicator {a .. b} x) has_integral F b - F a) UNIV"
  1186     unfolding indicator_def if_distrib[where f="\<lambda>x. a * x" for a]
  1187     by (simp cong del: if_cong del: atLeastAtMost_iff)
  1188   then have nn: "(\<integral>\<^sup>+x. f x * indicator {a .. b} x \<partial>lborel) = F b - F a"
  1189     by (rule nn_integral_has_integral_lborel[OF *])
  1190   then show ?has
  1191     by (rule has_bochner_integral_nn_integral[rotated 2]) (simp_all add: *)
  1192   then show ?eq ?int
  1193     unfolding has_bochner_integral_iff by auto
  1194   from nn show ?nn
  1195     by (simp add: ereal_mult_indicator)
  1196 qed
  1197 
  1198 lemma
  1199   fixes f :: "real \<Rightarrow> 'a :: euclidean_space"
  1200   assumes "a \<le> b"
  1201   assumes "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> (F has_vector_derivative f x) (at x within {a .. b})"
  1202   assumes cont: "continuous_on {a .. b} f"
  1203   shows has_bochner_integral_FTC_Icc:
  1204       "has_bochner_integral lborel (\<lambda>x. indicator {a .. b} x *\<^sub>R f x) (F b - F a)" (is ?has)
  1205     and integral_FTC_Icc: "(\<integral>x. indicator {a .. b} x *\<^sub>R f x \<partial>lborel) = F b - F a" (is ?eq)
  1206 proof -
  1207   let ?f = "\<lambda>x. indicator {a .. b} x *\<^sub>R f x"
  1208   have int: "integrable lborel ?f"
  1209     using borel_integrable_compact[OF _ cont] by auto
  1210   have "(f has_integral F b - F a) {a..b}"
  1211     using assms(1,2) by (intro fundamental_theorem_of_calculus) auto
  1212   moreover 
  1213   have "(f has_integral integral\<^sup>L lborel ?f) {a..b}"
  1214     using has_integral_integral_lborel[OF int]
  1215     unfolding indicator_def if_distrib[where f="\<lambda>x. x *\<^sub>R a" for a]
  1216     by (simp cong del: if_cong del: atLeastAtMost_iff)
  1217   ultimately show ?eq
  1218     by (auto dest: has_integral_unique)
  1219   then show ?has
  1220     using int by (auto simp: has_bochner_integral_iff)
  1221 qed
  1222 
  1223 lemma
  1224   fixes f :: "real \<Rightarrow> real"
  1225   assumes "a \<le> b"
  1226   assumes deriv: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> DERIV F x :> f x"
  1227   assumes cont: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> isCont f x"
  1228   shows has_bochner_integral_FTC_Icc_real:
  1229       "has_bochner_integral lborel (\<lambda>x. f x * indicator {a .. b} x) (F b - F a)" (is ?has)
  1230     and integral_FTC_Icc_real: "(\<integral>x. f x * indicator {a .. b} x \<partial>lborel) = F b - F a" (is ?eq)
  1231 proof -
  1232   have 1: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> (F has_vector_derivative f x) (at x within {a .. b})"
  1233     unfolding has_field_derivative_iff_has_vector_derivative[symmetric]
  1234     using deriv by (auto intro: DERIV_subset)
  1235   have 2: "continuous_on {a .. b} f"
  1236     using cont by (intro continuous_at_imp_continuous_on) auto
  1237   show ?has ?eq
  1238     using has_bochner_integral_FTC_Icc[OF `a \<le> b` 1 2] integral_FTC_Icc[OF `a \<le> b` 1 2]
  1239     by (auto simp: mult.commute)
  1240 qed
  1241 
  1242 lemma nn_integral_FTC_atLeast:
  1243   fixes f :: "real \<Rightarrow> real"
  1244   assumes f_borel: "f \<in> borel_measurable borel"
  1245   assumes f: "\<And>x. a \<le> x \<Longrightarrow> DERIV F x :> f x" 
  1246   assumes nonneg: "\<And>x. a \<le> x \<Longrightarrow> 0 \<le> f x"
  1247   assumes lim: "(F ---> T) at_top"
  1248   shows "(\<integral>\<^sup>+x. ereal (f x) * indicator {a ..} x \<partial>lborel) = T - F a"
  1249 proof -
  1250   let ?f = "\<lambda>(i::nat) (x::real). ereal (f x) * indicator {a..a + real i} x"
  1251   let ?fR = "\<lambda>x. ereal (f x) * indicator {a ..} x"
  1252   have "\<And>x. (SUP i::nat. ?f i x) = ?fR x"
  1253   proof (rule SUP_Lim_ereal)
  1254     show "\<And>x. incseq (\<lambda>i. ?f i x)"
  1255       using nonneg by (auto simp: incseq_def le_fun_def split: split_indicator)
  1256 
  1257     fix x
  1258     from reals_Archimedean2[of "x - a"] guess n ..
  1259     then have "eventually (\<lambda>n. ?f n x = ?fR x) sequentially"
  1260       by (auto intro!: eventually_sequentiallyI[where c=n] split: split_indicator)
  1261     then show "(\<lambda>n. ?f n x) ----> ?fR x"
  1262       by (rule Lim_eventually)
  1263   qed
  1264   then have "integral\<^sup>N lborel ?fR = (\<integral>\<^sup>+ x. (SUP i::nat. ?f i x) \<partial>lborel)"
  1265     by simp
  1266   also have "\<dots> = (SUP i::nat. (\<integral>\<^sup>+ x. ?f i x \<partial>lborel))"
  1267   proof (rule nn_integral_monotone_convergence_SUP)
  1268     show "incseq ?f"
  1269       using nonneg by (auto simp: incseq_def le_fun_def split: split_indicator)
  1270     show "\<And>i. (?f i) \<in> borel_measurable lborel"
  1271       using f_borel by auto
  1272   qed
  1273   also have "\<dots> = (SUP i::nat. ereal (F (a + real i) - F a))"
  1274     by (subst nn_integral_FTC_Icc[OF f_borel f nonneg]) auto
  1275   also have "\<dots> = T - F a"
  1276   proof (rule SUP_Lim_ereal)
  1277     show "incseq (\<lambda>n. ereal (F (a + real n) - F a))"
  1278     proof (simp add: incseq_def, safe)
  1279       fix m n :: nat assume "m \<le> n"
  1280       with f nonneg show "F (a + real m) \<le> F (a + real n)"
  1281         by (intro DERIV_nonneg_imp_nondecreasing[where f=F])
  1282            (simp, metis add_increasing2 order_refl order_trans real_of_nat_ge_zero)
  1283     qed 
  1284     have "(\<lambda>x. F (a + real x)) ----> T"
  1285       apply (rule filterlim_compose[OF lim filterlim_tendsto_add_at_top])
  1286       apply (rule LIMSEQ_const_iff[THEN iffD2, OF refl])
  1287       apply (rule filterlim_real_sequentially)
  1288       done
  1289     then show "(\<lambda>n. ereal (F (a + real n) - F a)) ----> ereal (T - F a)"
  1290       unfolding lim_ereal
  1291       by (intro tendsto_diff) auto
  1292   qed
  1293   finally show ?thesis .
  1294 qed
  1295 
  1296 lemma integral_power:
  1297   "a \<le> b \<Longrightarrow> (\<integral>x. x^k * indicator {a..b} x \<partial>lborel) = (b^Suc k - a^Suc k) / Suc k"
  1298 proof (subst integral_FTC_Icc_real)
  1299   fix x show "DERIV (\<lambda>x. x^Suc k / Suc k) x :> x^k"
  1300     by (intro derivative_eq_intros) auto
  1301 qed (auto simp: field_simps)
  1302 
  1303 subsection {* Integration by parts *}
  1304 
  1305 lemma integral_by_parts_integrable:
  1306   fixes f g F G::"real \<Rightarrow> real"
  1307   assumes "a \<le> b"
  1308   assumes cont_f[intro]: "!!x. a \<le>x \<Longrightarrow> x\<le>b \<Longrightarrow> isCont f x"
  1309   assumes cont_g[intro]: "!!x. a \<le>x \<Longrightarrow> x\<le>b \<Longrightarrow> isCont g x"
  1310   assumes [intro]: "!!x. DERIV F x :> f x"
  1311   assumes [intro]: "!!x. DERIV G x :> g x"
  1312   shows  "integrable lborel (\<lambda>x.((F x) * (g x) + (f x) * (G x)) * indicator {a .. b} x)"
  1313   by (auto intro!: borel_integrable_atLeastAtMost continuous_intros) (auto intro!: DERIV_isCont)
  1314 
  1315 lemma integral_by_parts:
  1316   fixes f g F G::"real \<Rightarrow> real"
  1317   assumes [arith]: "a \<le> b"
  1318   assumes cont_f[intro]: "!!x. a \<le>x \<Longrightarrow> x\<le>b \<Longrightarrow> isCont f x"
  1319   assumes cont_g[intro]: "!!x. a \<le>x \<Longrightarrow> x\<le>b \<Longrightarrow> isCont g x"
  1320   assumes [intro]: "!!x. DERIV F x :> f x"
  1321   assumes [intro]: "!!x. DERIV G x :> g x"
  1322   shows "(\<integral>x. (F x * g x) * indicator {a .. b} x \<partial>lborel)
  1323             =  F b * G b - F a * G a - \<integral>x. (f x * G x) * indicator {a .. b} x \<partial>lborel" 
  1324 proof-
  1325   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"
  1326     by (rule integral_FTC_Icc_real, auto intro!: derivative_eq_intros continuous_intros) 
  1327       (auto intro!: DERIV_isCont)
  1328 
  1329   have "(\<integral>x. (F x * g x + f x * G x) * indicator {a .. b} x \<partial>lborel) =
  1330     (\<integral>x. (F x * g x) * indicator {a .. b} x \<partial>lborel) + \<integral>x. (f x * G x) * indicator {a .. b} x \<partial>lborel"
  1331     apply (subst integral_add[symmetric])
  1332     apply (auto intro!: borel_integrable_atLeastAtMost continuous_intros)
  1333     by (auto intro!: DERIV_isCont integral_cong split:split_indicator)
  1334 
  1335   thus ?thesis using 0 by auto
  1336 qed
  1337 
  1338 lemma integral_by_parts':
  1339   fixes f g F G::"real \<Rightarrow> real"
  1340   assumes "a \<le> b"
  1341   assumes "!!x. a \<le>x \<Longrightarrow> x\<le>b \<Longrightarrow> isCont f x"
  1342   assumes "!!x. a \<le>x \<Longrightarrow> x\<le>b \<Longrightarrow> isCont g x"
  1343   assumes "!!x. DERIV F x :> f x"
  1344   assumes "!!x. DERIV G x :> g x"
  1345   shows "(\<integral>x. indicator {a .. b} x *\<^sub>R (F x * g x) \<partial>lborel)
  1346             =  F b * G b - F a * G a - \<integral>x. indicator {a .. b} x *\<^sub>R (f x * G x) \<partial>lborel" 
  1347   using integral_by_parts[OF assms] by (simp add: ac_simps)
  1348 
  1349 lemma has_bochner_integral_even_function:
  1350   fixes f :: "real \<Rightarrow> 'a :: {banach, second_countable_topology}"
  1351   assumes f: "has_bochner_integral lborel (\<lambda>x. indicator {0..} x *\<^sub>R f x) x"
  1352   assumes even: "\<And>x. f (- x) = f x"
  1353   shows "has_bochner_integral lborel f (2 *\<^sub>R x)"
  1354 proof -
  1355   have indicator: "\<And>x::real. indicator {..0} (- x) = indicator {0..} x"
  1356     by (auto split: split_indicator)
  1357   have "has_bochner_integral lborel (\<lambda>x. indicator {.. 0} x *\<^sub>R f x) x"
  1358     by (subst lborel_has_bochner_integral_real_affine_iff[where c="-1" and t=0])
  1359        (auto simp: indicator even f)
  1360   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)"
  1361     by (rule has_bochner_integral_add)
  1362   then have "has_bochner_integral lborel f (x + x)"
  1363     by (rule has_bochner_integral_discrete_difference[where X="{0}", THEN iffD1, rotated 4])
  1364        (auto split: split_indicator)
  1365   then show ?thesis
  1366     by (simp add: scaleR_2)
  1367 qed
  1368 
  1369 lemma has_bochner_integral_odd_function:
  1370   fixes f :: "real \<Rightarrow> 'a :: {banach, second_countable_topology}"
  1371   assumes f: "has_bochner_integral lborel (\<lambda>x. indicator {0..} x *\<^sub>R f x) x"
  1372   assumes odd: "\<And>x. f (- x) = - f x"
  1373   shows "has_bochner_integral lborel f 0"
  1374 proof -
  1375   have indicator: "\<And>x::real. indicator {..0} (- x) = indicator {0..} x"
  1376     by (auto split: split_indicator)
  1377   have "has_bochner_integral lborel (\<lambda>x. - indicator {.. 0} x *\<^sub>R f x) x"
  1378     by (subst lborel_has_bochner_integral_real_affine_iff[where c="-1" and t=0])
  1379        (auto simp: indicator odd f)
  1380   from has_bochner_integral_minus[OF this]
  1381   have "has_bochner_integral lborel (\<lambda>x. indicator {.. 0} x *\<^sub>R f x) (- x)"
  1382     by simp 
  1383   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)"
  1384     by (rule has_bochner_integral_add)
  1385   then have "has_bochner_integral lborel f (x + - x)"
  1386     by (rule has_bochner_integral_discrete_difference[where X="{0}", THEN iffD1, rotated 4])
  1387        (auto split: split_indicator)
  1388   then show ?thesis
  1389     by simp
  1390 qed
  1391 
  1392 end
  1393