src/HOL/Analysis/Equivalence_Lebesgue_Henstock_Integration.thy
author nipkow
Sat Dec 29 15:43:53 2018 +0100 (6 months ago)
changeset 69529 4ab9657b3257
parent 69508 2a4c8a2a3f8e
child 69597 ff784d5a5bfb
permissions -rw-r--r--
capitalize proper names in lemma names
     1 (*  Title:      HOL/Analysis/Equivalence_Lebesgue_Henstock_Integration.thy
     2     Author:     Johannes Hölzl, TU München
     3     Author:     Robert Himmelmann, TU München
     4     Huge cleanup by LCP
     5 *)
     6 
     7 theory Equivalence_Lebesgue_Henstock_Integration
     8   imports Lebesgue_Measure Henstock_Kurzweil_Integration Complete_Measure Set_Integral
     9 begin
    10 
    11 lemma le_left_mono: "x \<le> y \<Longrightarrow> y \<le> a \<longrightarrow> x \<le> (a::'a::preorder)"
    12   by (auto intro: order_trans)
    13 
    14 lemma ball_trans:
    15   assumes "y \<in> ball z q" "r + q \<le> s" shows "ball y r \<subseteq> ball z s"
    16 proof safe
    17   fix x assume x: "x \<in> ball y r"
    18   have "dist z x \<le> dist z y + dist y x"
    19     by (rule dist_triangle)
    20   also have "\<dots> < s"
    21     using assms x by auto
    22   finally show "x \<in> ball z s"
    23     by simp
    24 qed
    25 
    26 lemma has_integral_implies_lebesgue_measurable_cbox:
    27   fixes f :: "'a :: euclidean_space \<Rightarrow> real"
    28   assumes f: "(f has_integral I) (cbox x y)"
    29   shows "f \<in> lebesgue_on (cbox x y) \<rightarrow>\<^sub>M borel"
    30 proof (rule cld_measure.borel_measurable_cld)
    31   let ?L = "lebesgue_on (cbox x y)"
    32   let ?\<mu> = "emeasure ?L"
    33   let ?\<mu>' = "outer_measure_of ?L"
    34   interpret L: finite_measure ?L
    35   proof
    36     show "?\<mu> (space ?L) \<noteq> \<infinity>"
    37       by (simp add: emeasure_restrict_space space_restrict_space emeasure_lborel_cbox_eq)
    38   qed
    39 
    40   show "cld_measure ?L"
    41   proof
    42     fix B A assume "B \<subseteq> A" "A \<in> null_sets ?L"
    43     then show "B \<in> sets ?L"
    44       using null_sets_completion_subset[OF \<open>B \<subseteq> A\<close>, of lborel]
    45       by (auto simp add: null_sets_restrict_space sets_restrict_space_iff intro: )
    46   next
    47     fix A assume "A \<subseteq> space ?L" "\<And>B. B \<in> sets ?L \<Longrightarrow> ?\<mu> B < \<infinity> \<Longrightarrow> A \<inter> B \<in> sets ?L"
    48     from this(1) this(2)[of "space ?L"] show "A \<in> sets ?L"
    49       by (auto simp: Int_absorb2 less_top[symmetric])
    50   qed auto
    51   then interpret cld_measure ?L
    52     .
    53 
    54   have content_eq_L: "A \<in> sets borel \<Longrightarrow> A \<subseteq> cbox x y \<Longrightarrow> content A = measure ?L A" for A
    55     by (subst measure_restrict_space) (auto simp: measure_def)
    56 
    57   fix E and a b :: real assume "E \<in> sets ?L" "a < b" "0 < ?\<mu> E" "?\<mu> E < \<infinity>"
    58   then obtain M :: real where "?\<mu> E = M" "0 < M"
    59     by (cases "?\<mu> E") auto
    60   define e where "e = M / (4 + 2 / (b - a))"
    61   from \<open>a < b\<close> \<open>0<M\<close> have "0 < e"
    62     by (auto intro!: divide_pos_pos simp: field_simps e_def)
    63 
    64   have "e < M / (3 + 2 / (b - a))"
    65     using \<open>a < b\<close> \<open>0 < M\<close>
    66     unfolding e_def by (intro divide_strict_left_mono add_strict_right_mono mult_pos_pos) (auto simp: field_simps)
    67   then have "2 * e < (b - a) * (M - e * 3)"
    68     using \<open>0<M\<close> \<open>0 < e\<close> \<open>a < b\<close> by (simp add: field_simps)
    69 
    70   have e_less_M: "e < M / 1"
    71     unfolding e_def using \<open>a < b\<close> \<open>0<M\<close> by (intro divide_strict_left_mono) (auto simp: field_simps)
    72 
    73   obtain d
    74     where "gauge d"
    75       and integral_f: "\<forall>p. p tagged_division_of cbox x y \<and> d fine p \<longrightarrow>
    76         norm ((\<Sum>(x,k) \<in> p. content k *\<^sub>R f x) - I) < e"
    77     using \<open>0<e\<close> f unfolding has_integral by auto
    78 
    79   define C where "C X m = X \<inter> {x. ball x (1/Suc m) \<subseteq> d x}" for X m
    80   have "incseq (C X)" for X
    81     unfolding C_def [abs_def]
    82     by (intro monoI Collect_mono conj_mono imp_refl le_left_mono subset_ball divide_left_mono Int_mono) auto
    83 
    84   { fix X assume "X \<subseteq> space ?L" and eq: "?\<mu>' X = ?\<mu> E"
    85     have "(SUP m. outer_measure_of ?L (C X m)) = outer_measure_of ?L (\<Union>m. C X m)"
    86       using \<open>X \<subseteq> space ?L\<close> by (intro SUP_outer_measure_of_incseq \<open>incseq (C X)\<close>) (auto simp: C_def)
    87     also have "(\<Union>m. C X m) = X"
    88     proof -
    89       { fix x
    90         obtain e where "0 < e" "ball x e \<subseteq> d x"
    91           using gaugeD[OF \<open>gauge d\<close>, of x] unfolding open_contains_ball by auto
    92         moreover
    93         obtain n where "1 / (1 + real n) < e"
    94           using reals_Archimedean[OF \<open>0<e\<close>] by (auto simp: inverse_eq_divide)
    95         then have "ball x (1 / (1 + real n)) \<subseteq> ball x e"
    96           by (intro subset_ball) auto
    97         ultimately have "\<exists>n. ball x (1 / (1 + real n)) \<subseteq> d x"
    98           by blast }
    99       then show ?thesis
   100         by (auto simp: C_def)
   101     qed
   102     finally have "(SUP m. outer_measure_of ?L (C X m)) = ?\<mu> E"
   103       using eq by auto
   104     also have "\<dots> > M - e"
   105       using \<open>0 < M\<close> \<open>?\<mu> E = M\<close> \<open>0<e\<close> by (auto intro!: ennreal_lessI)
   106     finally have "\<exists>m. M - e < outer_measure_of ?L (C X m)"
   107       unfolding less_SUP_iff by auto }
   108   note C = this
   109 
   110   let ?E = "{x\<in>E. f x \<le> a}" and ?F = "{x\<in>E. b \<le> f x}"
   111 
   112   have "\<not> (?\<mu>' ?E = ?\<mu> E \<and> ?\<mu>' ?F = ?\<mu> E)"
   113   proof
   114     assume eq: "?\<mu>' ?E = ?\<mu> E \<and> ?\<mu>' ?F = ?\<mu> E"
   115     with C[of ?E] C[of ?F] \<open>E \<in> sets ?L\<close>[THEN sets.sets_into_space] obtain ma mb
   116       where "M - e < outer_measure_of ?L (C ?E ma)" "M - e < outer_measure_of ?L (C ?F mb)"
   117       by auto
   118     moreover define m where "m = max ma mb"
   119     ultimately have M_minus_e: "M - e < outer_measure_of ?L (C ?E m)" "M - e < outer_measure_of ?L (C ?F m)"
   120       using
   121         incseqD[OF \<open>incseq (C ?E)\<close>, of ma m, THEN outer_measure_of_mono]
   122         incseqD[OF \<open>incseq (C ?F)\<close>, of mb m, THEN outer_measure_of_mono]
   123       by (auto intro: less_le_trans)
   124     define d' where "d' x = d x \<inter> ball x (1 / (3 * Suc m))" for x
   125     have "gauge d'"
   126       unfolding d'_def by (intro gauge_Int \<open>gauge d\<close> gauge_ball) auto
   127     then obtain p where p: "p tagged_division_of cbox x y" "d' fine p"
   128       by (rule fine_division_exists)
   129     then have "d fine p"
   130       unfolding d'_def[abs_def] fine_def by auto
   131 
   132     define s where "s = {(x::'a, k). k \<inter> (C ?E m) \<noteq> {} \<and> k \<inter> (C ?F m) \<noteq> {}}"
   133     define T where "T E k = (SOME x. x \<in> k \<inter> C E m)" for E k
   134     let ?A = "(\<lambda>(x, k). (T ?E k, k)) ` (p \<inter> s) \<union> (p - s)"
   135     let ?B = "(\<lambda>(x, k). (T ?F k, k)) ` (p \<inter> s) \<union> (p - s)"
   136 
   137     { fix X assume X_eq: "X = ?E \<or> X = ?F"
   138       let ?T = "(\<lambda>(x, k). (T X k, k))"
   139       let ?p = "?T ` (p \<inter> s) \<union> (p - s)"
   140 
   141       have in_s: "(x, k) \<in> s \<Longrightarrow> T X k \<in> k \<inter> C X m" for x k
   142         using someI_ex[of "\<lambda>x. x \<in> k \<inter> C X m"] X_eq unfolding ex_in_conv by (auto simp: T_def s_def)
   143 
   144       { fix x k assume "(x, k) \<in> p" "(x, k) \<in> s"
   145         have k: "k \<subseteq> ball x (1 / (3 * Suc m))"
   146           using \<open>d' fine p\<close>[THEN fineD, OF \<open>(x, k) \<in> p\<close>] by (auto simp: d'_def)
   147         then have "x \<in> ball (T X k) (1 / (3 * Suc m))"
   148           using in_s[OF \<open>(x, k) \<in> s\<close>] by (auto simp: C_def subset_eq dist_commute)
   149         then have "ball x (1 / (3 * Suc m)) \<subseteq> ball (T X k) (1 / Suc m)"
   150           by (rule ball_trans) (auto simp: divide_simps)
   151         with k in_s[OF \<open>(x, k) \<in> s\<close>] have "k \<subseteq> d (T X k)"
   152           by (auto simp: C_def) }
   153       then have "d fine ?p"
   154         using \<open>d fine p\<close> by (auto intro!: fineI)
   155       moreover
   156       have "?p tagged_division_of cbox x y"
   157       proof (rule tagged_division_ofI)
   158         show "finite ?p"
   159           using p(1) by auto
   160       next
   161         fix z k assume *: "(z, k) \<in> ?p"
   162         then consider "(z, k) \<in> p" "(z, k) \<notin> s"
   163           | x' where "(x', k) \<in> p" "(x', k) \<in> s" "z = T X k"
   164           by (auto simp: T_def)
   165         then have "z \<in> k \<and> k \<subseteq> cbox x y \<and> (\<exists>a b. k = cbox a b)"
   166           using p(1) by cases (auto dest: in_s)
   167         then show "z \<in> k" "k \<subseteq> cbox x y" "\<exists>a b. k = cbox a b"
   168           by auto
   169       next
   170         fix z k z' k' assume "(z, k) \<in> ?p" "(z', k') \<in> ?p" "(z, k) \<noteq> (z', k')"
   171         with tagged_division_ofD(5)[OF p(1), of _ k _ k']
   172         show "interior k \<inter> interior k' = {}"
   173           by (auto simp: T_def dest: in_s)
   174       next
   175         have "{k. \<exists>x. (x, k) \<in> ?p} = {k. \<exists>x. (x, k) \<in> p}"
   176           by (auto simp: T_def image_iff Bex_def)
   177         then show "\<Union>{k. \<exists>x. (x, k) \<in> ?p} = cbox x y"
   178           using p(1) by auto
   179       qed
   180       ultimately have I: "norm ((\<Sum>(x,k) \<in> ?p. content k *\<^sub>R f x) - I) < e"
   181         using integral_f by auto
   182 
   183       have "(\<Sum>(x,k) \<in> ?p. content k *\<^sub>R f x) =
   184         (\<Sum>(x,k) \<in> ?T ` (p \<inter> s). content k *\<^sub>R f x) + (\<Sum>(x,k) \<in> p - s. content k *\<^sub>R f x)"
   185         using p(1)[THEN tagged_division_ofD(1)]
   186         by (safe intro!: sum.union_inter_neutral) (auto simp: s_def T_def)
   187       also have "(\<Sum>(x,k) \<in> ?T ` (p \<inter> s). content k *\<^sub>R f x) = (\<Sum>(x,k) \<in> p \<inter> s. content k *\<^sub>R f (T X k))"
   188       proof (subst sum.reindex_nontrivial, safe)
   189         fix x1 x2 k assume 1: "(x1, k) \<in> p" "(x1, k) \<in> s" and 2: "(x2, k) \<in> p" "(x2, k) \<in> s"
   190           and eq: "content k *\<^sub>R f (T X k) \<noteq> 0"
   191         with tagged_division_ofD(5)[OF p(1), of x1 k x2 k] tagged_division_ofD(4)[OF p(1), of x1 k]
   192         show "x1 = x2"
   193           by (auto simp: content_eq_0_interior)
   194       qed (use p in \<open>auto intro!: sum.cong\<close>)
   195       finally have eq: "(\<Sum>(x,k) \<in> ?p. content k *\<^sub>R f x) =
   196         (\<Sum>(x,k) \<in> p \<inter> s. content k *\<^sub>R f (T X k)) + (\<Sum>(x,k) \<in> p - s. content k *\<^sub>R f x)" .
   197 
   198       have in_T: "(x, k) \<in> s \<Longrightarrow> T X k \<in> X" for x k
   199         using in_s[of x k] by (auto simp: C_def)
   200 
   201       note I eq in_T }
   202     note parts = this
   203 
   204     have p_in_L: "(x, k) \<in> p \<Longrightarrow> k \<in> sets ?L" for x k
   205       using tagged_division_ofD(3, 4)[OF p(1), of x k] by (auto simp: sets_restrict_space)
   206 
   207     have [simp]: "finite p"
   208       using tagged_division_ofD(1)[OF p(1)] .
   209 
   210     have "(M - 3*e) * (b - a) \<le> (\<Sum>(x,k) \<in> p \<inter> s. content k) * (b - a)"
   211     proof (intro mult_right_mono)
   212       have fin: "?\<mu> (E \<inter> \<Union>{k\<in>snd`p. k \<inter> C X m = {}}) < \<infinity>" for X
   213         using \<open>?\<mu> E < \<infinity>\<close> by (rule le_less_trans[rotated]) (auto intro!: emeasure_mono \<open>E \<in> sets ?L\<close>)
   214       have sets: "(E \<inter> \<Union>{k\<in>snd`p. k \<inter> C X m = {}}) \<in> sets ?L" for X
   215         using tagged_division_ofD(1)[OF p(1)] by (intro sets.Diff \<open>E \<in> sets ?L\<close> sets.finite_Union sets.Int) (auto intro: p_in_L)
   216       { fix X assume "X \<subseteq> E" "M - e < ?\<mu>' (C X m)"
   217         have "M - e \<le> ?\<mu>' (C X m)"
   218           by (rule less_imp_le) fact
   219         also have "\<dots> \<le> ?\<mu>' (E - (E \<inter> \<Union>{k\<in>snd`p. k \<inter> C X m = {}}))"
   220         proof (intro outer_measure_of_mono subsetI)
   221           fix v assume "v \<in> C X m"
   222           then have "v \<in> cbox x y" "v \<in> E"
   223             using \<open>E \<subseteq> space ?L\<close> \<open>X \<subseteq> E\<close> by (auto simp: space_restrict_space C_def)
   224           then obtain z k where "(z, k) \<in> p" "v \<in> k"
   225             using tagged_division_ofD(6)[OF p(1), symmetric] by auto
   226           then show "v \<in> E - E \<inter> (\<Union>{k\<in>snd`p. k \<inter> C X m = {}})"
   227             using \<open>v \<in> C X m\<close> \<open>v \<in> E\<close> by auto
   228         qed
   229         also have "\<dots> = ?\<mu> E - ?\<mu> (E \<inter> \<Union>{k\<in>snd`p. k \<inter> C X m = {}})"
   230           using \<open>E \<in> sets ?L\<close> fin[of X] sets[of X] by (auto intro!: emeasure_Diff)
   231         finally have "?\<mu> (E \<inter> \<Union>{k\<in>snd`p. k \<inter> C X m = {}}) \<le> e"
   232           using \<open>0 < e\<close> e_less_M apply (cases "?\<mu> (E \<inter> \<Union>{k\<in>snd`p. k \<inter> C X m = {}})")
   233           by (auto simp add: \<open>?\<mu> E = M\<close> ennreal_minus ennreal_le_iff2)
   234         note this }
   235       note upper_bound = this
   236 
   237       have "?\<mu> (E \<inter> \<Union>(snd`(p - s))) =
   238         ?\<mu> ((E \<inter> \<Union>{k\<in>snd`p. k \<inter> C ?E m = {}}) \<union> (E \<inter> \<Union>{k\<in>snd`p. k \<inter> C ?F m = {}}))"
   239         by (intro arg_cong[where f="?\<mu>"]) (auto simp: s_def image_def Bex_def)
   240       also have "\<dots> \<le> ?\<mu> (E \<inter> \<Union>{k\<in>snd`p. k \<inter> C ?E m = {}}) + ?\<mu> (E \<inter> \<Union>{k\<in>snd`p. k \<inter> C ?F m = {}})"
   241         using sets[of ?E] sets[of ?F] M_minus_e by (intro emeasure_subadditive) auto
   242       also have "\<dots> \<le> e + ennreal e"
   243         using upper_bound[of ?E] upper_bound[of ?F] M_minus_e by (intro add_mono) auto
   244       finally have "?\<mu> E - 2*e \<le> ?\<mu> (E - (E \<inter> \<Union>(snd`(p - s))))"
   245         using \<open>0 < e\<close> \<open>E \<in> sets ?L\<close> tagged_division_ofD(1)[OF p(1)]
   246         by (subst emeasure_Diff)
   247            (auto simp: top_unique simp flip: ennreal_plus
   248                  intro!: sets.Int sets.finite_UN ennreal_mono_minus intro: p_in_L)
   249       also have "\<dots> \<le> ?\<mu> (\<Union>x\<in>p \<inter> s. snd x)"
   250       proof (safe intro!: emeasure_mono subsetI)
   251         fix v assume "v \<in> E" and not: "v \<notin> (\<Union>x\<in>p \<inter> s. snd x)"
   252         then have "v \<in> cbox x y"
   253           using \<open>E \<subseteq> space ?L\<close> by (auto simp: space_restrict_space)
   254         then obtain z k where "(z, k) \<in> p" "v \<in> k"
   255           using tagged_division_ofD(6)[OF p(1), symmetric] by auto
   256         with not show "v \<in> \<Union>(snd ` (p - s))"
   257           by (auto intro!: bexI[of _ "(z, k)"] elim: ballE[of _ _ "(z, k)"])
   258       qed (auto intro!: sets.Int sets.finite_UN ennreal_mono_minus intro: p_in_L)
   259       also have "\<dots> = measure ?L (\<Union>x\<in>p \<inter> s. snd x)"
   260         by (auto intro!: emeasure_eq_ennreal_measure)
   261       finally have "M - 2 * e \<le> measure ?L (\<Union>x\<in>p \<inter> s. snd x)"
   262         unfolding \<open>?\<mu> E = M\<close> using \<open>0 < e\<close> by (simp add: ennreal_minus)
   263       also have "measure ?L (\<Union>x\<in>p \<inter> s. snd x) = content (\<Union>x\<in>p \<inter> s. snd x)"
   264         using tagged_division_ofD(1,3,4) [OF p(1)]
   265         by (intro content_eq_L[symmetric])
   266            (fastforce intro!: sets.finite_UN UN_least del: subsetI)+
   267       also have "content (\<Union>x\<in>p \<inter> s. snd x) \<le> (\<Sum>k\<in>p \<inter> s. content (snd k))"
   268         using p(1) by (auto simp: emeasure_lborel_cbox_eq intro!: measure_subadditive_finite
   269                             dest!: p(1)[THEN tagged_division_ofD(4)])
   270       finally show "M - 3 * e \<le> (\<Sum>(x, y)\<in>p \<inter> s. content y)"
   271         using \<open>0 < e\<close> by (simp add: split_beta)
   272     qed (use \<open>a < b\<close> in auto)
   273     also have "\<dots> = (\<Sum>(x,k) \<in> p \<inter> s. content k * (b - a))"
   274       by (simp add: sum_distrib_right split_beta')
   275     also have "\<dots> \<le> (\<Sum>(x,k) \<in> p \<inter> s. content k * (f (T ?F k) - f (T ?E k)))"
   276       using parts(3) by (auto intro!: sum_mono mult_left_mono diff_mono)
   277     also have "\<dots> = (\<Sum>(x,k) \<in> p \<inter> s. content k * f (T ?F k)) - (\<Sum>(x,k) \<in> p \<inter> s. content k * f (T ?E k))"
   278       by (auto intro!: sum.cong simp: field_simps sum_subtractf[symmetric])
   279     also have "\<dots> = (\<Sum>(x,k) \<in> ?B. content k *\<^sub>R f x) - (\<Sum>(x,k) \<in> ?A. content k *\<^sub>R f x)"
   280       by (subst (1 2) parts) auto
   281     also have "\<dots> \<le> norm ((\<Sum>(x,k) \<in> ?B. content k *\<^sub>R f x) - (\<Sum>(x,k) \<in> ?A. content k *\<^sub>R f x))"
   282       by auto
   283     also have "\<dots> \<le> e + e"
   284       using parts(1)[of ?E] parts(1)[of ?F] by (intro norm_diff_triangle_le[of _ I]) auto
   285     finally show False
   286       using \<open>2 * e < (b - a) * (M - e * 3)\<close> by (auto simp: field_simps)
   287   qed
   288   moreover have "?\<mu>' ?E \<le> ?\<mu> E" "?\<mu>' ?F \<le> ?\<mu> E"
   289     unfolding outer_measure_of_eq[OF \<open>E \<in> sets ?L\<close>, symmetric] by (auto intro!: outer_measure_of_mono)
   290   ultimately show "min (?\<mu>' ?E) (?\<mu>' ?F) < ?\<mu> E"
   291     unfolding min_less_iff_disj by (auto simp: less_le)
   292 qed
   293 
   294 lemma has_integral_implies_lebesgue_measurable_real:
   295   fixes f :: "'a :: euclidean_space \<Rightarrow> real"
   296   assumes f: "(f has_integral I) \<Omega>"
   297   shows "(\<lambda>x. f x * indicator \<Omega> x) \<in> lebesgue \<rightarrow>\<^sub>M borel"
   298 proof -
   299   define B :: "nat \<Rightarrow> 'a set" where "B n = cbox (- real n *\<^sub>R One) (real n *\<^sub>R One)" for n
   300   show "(\<lambda>x. f x * indicator \<Omega> x) \<in> lebesgue \<rightarrow>\<^sub>M borel"
   301   proof (rule measurable_piecewise_restrict)
   302     have "(\<Union>n. box (- real n *\<^sub>R One) (real n *\<^sub>R One)) \<subseteq> \<Union>(B ` UNIV)"
   303       unfolding B_def by (intro UN_mono box_subset_cbox order_refl)
   304     then show "countable (range B)" "space lebesgue \<subseteq> \<Union>(B ` UNIV)"
   305       by (auto simp: B_def UN_box_eq_UNIV)
   306   next
   307     fix \<Omega>' assume "\<Omega>' \<in> range B"
   308     then obtain n where \<Omega>': "\<Omega>' = B n" by auto
   309     then show "\<Omega>' \<inter> space lebesgue \<in> sets lebesgue"
   310       by (auto simp: B_def)
   311 
   312     have "f integrable_on \<Omega>"
   313       using f by auto
   314     then have "(\<lambda>x. f x * indicator \<Omega> x) integrable_on \<Omega>"
   315       by (auto simp: integrable_on_def cong: has_integral_cong)
   316     then have "(\<lambda>x. f x * indicator \<Omega> x) integrable_on (\<Omega> \<union> B n)"
   317       by (rule integrable_on_superset) auto
   318     then have "(\<lambda>x. f x * indicator \<Omega> x) integrable_on B n"
   319       unfolding B_def by (rule integrable_on_subcbox) auto
   320     then show "(\<lambda>x. f x * indicator \<Omega> x) \<in> lebesgue_on \<Omega>' \<rightarrow>\<^sub>M borel"
   321       unfolding B_def \<Omega>' by (auto intro: has_integral_implies_lebesgue_measurable_cbox simp: integrable_on_def)
   322   qed
   323 qed
   324 
   325 lemma has_integral_implies_lebesgue_measurable:
   326   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
   327   assumes f: "(f has_integral I) \<Omega>"
   328   shows "(\<lambda>x. indicator \<Omega> x *\<^sub>R f x) \<in> lebesgue \<rightarrow>\<^sub>M borel"
   329 proof (intro borel_measurable_euclidean_space[where 'c='b, THEN iffD2] ballI)
   330   fix i :: "'b" assume "i \<in> Basis"
   331   have "(\<lambda>x. (f x \<bullet> i) * indicator \<Omega> x) \<in> borel_measurable (completion lborel)"
   332     using has_integral_linear[OF f bounded_linear_inner_left, of i]
   333     by (intro has_integral_implies_lebesgue_measurable_real) (auto simp: comp_def)
   334   then show "(\<lambda>x. indicator \<Omega> x *\<^sub>R f x \<bullet> i) \<in> borel_measurable (completion lborel)"
   335     by (simp add: ac_simps)
   336 qed
   337 
   338 subsection \<open>Equivalence Lebesgue integral on @{const lborel} and HK-integral\<close>
   339 
   340 lemma has_integral_measure_lborel:
   341   fixes A :: "'a::euclidean_space set"
   342   assumes A[measurable]: "A \<in> sets borel" and finite: "emeasure lborel A < \<infinity>"
   343   shows "((\<lambda>x. 1) has_integral measure lborel A) A"
   344 proof -
   345   { fix l u :: 'a
   346     have "((\<lambda>x. 1) has_integral measure lborel (box l u)) (box l u)"
   347     proof cases
   348       assume "\<forall>b\<in>Basis. l \<bullet> b \<le> u \<bullet> b"
   349       then show ?thesis
   350         apply simp
   351         apply (subst has_integral_restrict[symmetric, OF box_subset_cbox])
   352         apply (subst has_integral_spike_interior_eq[where g="\<lambda>_. 1"])
   353         using has_integral_const[of "1::real" l u]
   354         apply (simp_all add: inner_diff_left[symmetric] content_cbox_cases)
   355         done
   356     next
   357       assume "\<not> (\<forall>b\<in>Basis. l \<bullet> b \<le> u \<bullet> b)"
   358       then have "box l u = {}"
   359         unfolding box_eq_empty by (auto simp: not_le intro: less_imp_le)
   360       then show ?thesis
   361         by simp
   362     qed }
   363   note has_integral_box = this
   364 
   365   { fix a b :: 'a let ?M = "\<lambda>A. measure lborel (A \<inter> box a b)"
   366     have "Int_stable  (range (\<lambda>(a, b). box a b))"
   367       by (auto simp: Int_stable_def box_Int_box)
   368     moreover have "(range (\<lambda>(a, b). box a b)) \<subseteq> Pow UNIV"
   369       by auto
   370     moreover have "A \<in> sigma_sets UNIV (range (\<lambda>(a, b). box a b))"
   371        using A unfolding borel_eq_box by simp
   372     ultimately have "((\<lambda>x. 1) has_integral ?M A) (A \<inter> box a b)"
   373     proof (induction rule: sigma_sets_induct_disjoint)
   374       case (basic A) then show ?case
   375         by (auto simp: box_Int_box has_integral_box)
   376     next
   377       case empty then show ?case
   378         by simp
   379     next
   380       case (compl A)
   381       then have [measurable]: "A \<in> sets borel"
   382         by (simp add: borel_eq_box)
   383 
   384       have "((\<lambda>x. 1) has_integral ?M (box a b)) (box a b)"
   385         by (simp add: has_integral_box)
   386       moreover have "((\<lambda>x. if x \<in> A \<inter> box a b then 1 else 0) has_integral ?M A) (box a b)"
   387         by (subst has_integral_restrict) (auto intro: compl)
   388       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)"
   389         by (rule has_integral_diff)
   390       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)"
   391         by (rule has_integral_cong[THEN iffD1, rotated 1]) auto
   392       then have "((\<lambda>x. 1) has_integral ?M (box a b) - ?M A) ((UNIV - A) \<inter> box a b)"
   393         by (subst (asm) has_integral_restrict) auto
   394       also have "?M (box a b) - ?M A = ?M (UNIV - A)"
   395         by (subst measure_Diff[symmetric]) (auto simp: emeasure_lborel_box_eq Diff_Int_distrib2)
   396       finally show ?case .
   397     next
   398       case (union F)
   399       then have [measurable]: "\<And>i. F i \<in> sets borel"
   400         by (simp add: borel_eq_box subset_eq)
   401       have "((\<lambda>x. if x \<in> \<Union>(F ` UNIV) \<inter> box a b then 1 else 0) has_integral ?M (\<Union>i. F i)) (box a b)"
   402       proof (rule has_integral_monotone_convergence_increasing)
   403         let ?f = "\<lambda>k x. \<Sum>i<k. if x \<in> F i \<inter> box a b then 1 else 0 :: real"
   404         show "\<And>k. (?f k has_integral (\<Sum>i<k. ?M (F i))) (box a b)"
   405           using union.IH by (auto intro!: has_integral_sum simp del: Int_iff)
   406         show "\<And>k x. ?f k x \<le> ?f (Suc k) x"
   407           by (intro sum_mono2) auto
   408         from union(1) have *: "\<And>x i j. x \<in> F i \<Longrightarrow> x \<in> F j \<longleftrightarrow> j = i"
   409           by (auto simp add: disjoint_family_on_def)
   410         show "\<And>x. (\<lambda>k. ?f k x) \<longlonglongrightarrow> (if x \<in> \<Union>(F ` UNIV) \<inter> box a b then 1 else 0)"
   411           apply (auto simp: * sum.If_cases Iio_Int_singleton)
   412           apply (rule_tac k="Suc xa" in LIMSEQ_offset)
   413           apply simp
   414           done
   415         have *: "emeasure lborel ((\<Union>x. F x) \<inter> box a b) \<le> emeasure lborel (box a b)"
   416           by (intro emeasure_mono) auto
   417 
   418         with union(1) show "(\<lambda>k. \<Sum>i<k. ?M (F i)) \<longlonglongrightarrow> ?M (\<Union>i. F i)"
   419           unfolding sums_def[symmetric] UN_extend_simps
   420           by (intro measure_UNION) (auto simp: disjoint_family_on_def emeasure_lborel_box_eq top_unique)
   421       qed
   422       then show ?case
   423         by (subst (asm) has_integral_restrict) auto
   424     qed }
   425   note * = this
   426 
   427   show ?thesis
   428   proof (rule has_integral_monotone_convergence_increasing)
   429     let ?B = "\<lambda>n::nat. box (- real n *\<^sub>R One) (real n *\<^sub>R One) :: 'a set"
   430     let ?f = "\<lambda>n::nat. \<lambda>x. if x \<in> A \<inter> ?B n then 1 else 0 :: real"
   431     let ?M = "\<lambda>n. measure lborel (A \<inter> ?B n)"
   432 
   433     show "\<And>n::nat. (?f n has_integral ?M n) A"
   434       using * by (subst has_integral_restrict) simp_all
   435     show "\<And>k x. ?f k x \<le> ?f (Suc k) x"
   436       by (auto simp: box_def)
   437     { fix x assume "x \<in> A"
   438       moreover have "(\<lambda>k. indicator (A \<inter> ?B k) x :: real) \<longlonglongrightarrow> indicator (\<Union>k::nat. A \<inter> ?B k) x"
   439         by (intro LIMSEQ_indicator_incseq) (auto simp: incseq_def box_def)
   440       ultimately show "(\<lambda>k. if x \<in> A \<inter> ?B k then 1 else 0::real) \<longlonglongrightarrow> 1"
   441         by (simp add: indicator_def UN_box_eq_UNIV) }
   442 
   443     have "(\<lambda>n. emeasure lborel (A \<inter> ?B n)) \<longlonglongrightarrow> emeasure lborel (\<Union>n::nat. A \<inter> ?B n)"
   444       by (intro Lim_emeasure_incseq) (auto simp: incseq_def box_def)
   445     also have "(\<lambda>n. emeasure lborel (A \<inter> ?B n)) = (\<lambda>n. measure lborel (A \<inter> ?B n))"
   446     proof (intro ext emeasure_eq_ennreal_measure)
   447       fix n have "emeasure lborel (A \<inter> ?B n) \<le> emeasure lborel (?B n)"
   448         by (intro emeasure_mono) auto
   449       then show "emeasure lborel (A \<inter> ?B n) \<noteq> top"
   450         by (auto simp: top_unique)
   451     qed
   452     finally show "(\<lambda>n. measure lborel (A \<inter> ?B n)) \<longlonglongrightarrow> measure lborel A"
   453       using emeasure_eq_ennreal_measure[of lborel A] finite
   454       by (simp add: UN_box_eq_UNIV less_top)
   455   qed
   456 qed
   457 
   458 lemma nn_integral_has_integral:
   459   fixes f::"'a::euclidean_space \<Rightarrow> real"
   460   assumes f: "f \<in> borel_measurable borel" "\<And>x. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>lborel) = ennreal r" "0 \<le> r"
   461   shows "(f has_integral r) UNIV"
   462 using f proof (induct f arbitrary: r rule: borel_measurable_induct_real)
   463   case (set A)
   464   then have "((\<lambda>x. 1) has_integral measure lborel A) A"
   465     by (intro has_integral_measure_lborel) (auto simp: ennreal_indicator)
   466   with set show ?case
   467     by (simp add: ennreal_indicator measure_def) (simp add: indicator_def)
   468 next
   469   case (mult g c)
   470   then have "ennreal c * (\<integral>\<^sup>+ x. g x \<partial>lborel) = ennreal r"
   471     by (subst nn_integral_cmult[symmetric]) (auto simp: ennreal_mult)
   472   with \<open>0 \<le> r\<close> \<open>0 \<le> c\<close>
   473   obtain r' where "(c = 0 \<and> r = 0) \<or> (0 \<le> r' \<and> (\<integral>\<^sup>+ x. ennreal (g x) \<partial>lborel) = ennreal r' \<and> r = c * r')"
   474     by (cases "\<integral>\<^sup>+ x. ennreal (g x) \<partial>lborel" rule: ennreal_cases)
   475        (auto split: if_split_asm simp: ennreal_mult_top ennreal_mult[symmetric])
   476   with mult show ?case
   477     by (auto intro!: has_integral_cmult_real)
   478 next
   479   case (add g h)
   480   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)"
   481     by (simp add: nn_integral_add)
   482   with add obtain a b where "0 \<le> a" "0 \<le> b" "(\<integral>\<^sup>+ x. h x \<partial>lborel) = ennreal a" "(\<integral>\<^sup>+ x. g x \<partial>lborel) = ennreal b" "r = a + b"
   483     by (cases "\<integral>\<^sup>+ x. h x \<partial>lborel" "\<integral>\<^sup>+ x. g x \<partial>lborel" rule: ennreal2_cases)
   484        (auto simp: add_top nn_integral_add top_add simp flip: ennreal_plus)
   485   with add show ?case
   486     by (auto intro!: has_integral_add)
   487 next
   488   case (seq U)
   489   note seq(1)[measurable] and f[measurable]
   490 
   491   { fix i x
   492     have "U i x \<le> f x"
   493       using seq(5)
   494       apply (rule LIMSEQ_le_const)
   495       using seq(4)
   496       apply (auto intro!: exI[of _ i] simp: incseq_def le_fun_def)
   497       done }
   498   note U_le_f = this
   499 
   500   { fix i
   501     have "(\<integral>\<^sup>+x. U i x \<partial>lborel) \<le> (\<integral>\<^sup>+x. f x \<partial>lborel)"
   502       using seq(2) f(2) U_le_f by (intro nn_integral_mono) simp
   503     then obtain p where "(\<integral>\<^sup>+x. U i x \<partial>lborel) = ennreal p" "p \<le> r" "0 \<le> p"
   504       using seq(6) \<open>0\<le>r\<close> by (cases "\<integral>\<^sup>+x. U i x \<partial>lborel" rule: ennreal_cases) (auto simp: top_unique)
   505     moreover note seq
   506     ultimately have "\<exists>p. (\<integral>\<^sup>+x. U i x \<partial>lborel) = ennreal p \<and> 0 \<le> p \<and> p \<le> r \<and> (U i has_integral p) UNIV"
   507       by auto }
   508   then obtain p where p: "\<And>i. (\<integral>\<^sup>+x. ennreal (U i x) \<partial>lborel) = ennreal (p i)"
   509     and bnd: "\<And>i. p i \<le> r" "\<And>i. 0 \<le> p i"
   510     and U_int: "\<And>i.(U i has_integral (p i)) UNIV" by metis
   511 
   512   have int_eq: "\<And>i. integral UNIV (U i) = p i" using U_int by (rule integral_unique)
   513 
   514   have *: "f integrable_on UNIV \<and> (\<lambda>k. integral UNIV (U k)) \<longlonglongrightarrow> integral UNIV f"
   515   proof (rule monotone_convergence_increasing)
   516     show "\<And>k. U k integrable_on UNIV" using U_int by auto
   517     show "\<And>k x. x\<in>UNIV \<Longrightarrow> U k x \<le> U (Suc k) x" using \<open>incseq U\<close> by (auto simp: incseq_def le_fun_def)
   518     then show "bounded (range (\<lambda>k. integral UNIV (U k)))"
   519       using bnd int_eq by (auto simp: bounded_real intro!: exI[of _ r])
   520     show "\<And>x. x\<in>UNIV \<Longrightarrow> (\<lambda>k. U k x) \<longlonglongrightarrow> f x"
   521       using seq by auto
   522   qed
   523   moreover have "(\<lambda>i. (\<integral>\<^sup>+x. U i x \<partial>lborel)) \<longlonglongrightarrow> (\<integral>\<^sup>+x. f x \<partial>lborel)"
   524     using seq f(2) U_le_f by (intro nn_integral_dominated_convergence[where w=f]) auto
   525   ultimately have "integral UNIV f = r"
   526     by (auto simp add: bnd int_eq p seq intro: LIMSEQ_unique)
   527   with * show ?case
   528     by (simp add: has_integral_integral)
   529 qed
   530 
   531 lemma nn_integral_lborel_eq_integral:
   532   fixes f::"'a::euclidean_space \<Rightarrow> real"
   533   assumes f: "f \<in> borel_measurable borel" "\<And>x. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>lborel) < \<infinity>"
   534   shows "(\<integral>\<^sup>+x. f x \<partial>lborel) = integral UNIV f"
   535 proof -
   536   from f(3) obtain r where r: "(\<integral>\<^sup>+x. f x \<partial>lborel) = ennreal r" "0 \<le> r"
   537     by (cases "\<integral>\<^sup>+x. f x \<partial>lborel" rule: ennreal_cases) auto
   538   then show ?thesis
   539     using nn_integral_has_integral[OF f(1,2) r] by (simp add: integral_unique)
   540 qed
   541 
   542 lemma nn_integral_integrable_on:
   543   fixes f::"'a::euclidean_space \<Rightarrow> real"
   544   assumes f: "f \<in> borel_measurable borel" "\<And>x. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>lborel) < \<infinity>"
   545   shows "f integrable_on UNIV"
   546 proof -
   547   from f(3) obtain r where r: "(\<integral>\<^sup>+x. f x \<partial>lborel) = ennreal r" "0 \<le> r"
   548     by (cases "\<integral>\<^sup>+x. f x \<partial>lborel" rule: ennreal_cases) auto
   549   then show ?thesis
   550     by (intro has_integral_integrable[where i=r] nn_integral_has_integral[where r=r] f)
   551 qed
   552 
   553 lemma nn_integral_has_integral_lborel:
   554   fixes f :: "'a::euclidean_space \<Rightarrow> real"
   555   assumes f_borel: "f \<in> borel_measurable borel" and nonneg: "\<And>x. 0 \<le> f x"
   556   assumes I: "(f has_integral I) UNIV"
   557   shows "integral\<^sup>N lborel f = I"
   558 proof -
   559   from f_borel have "(\<lambda>x. ennreal (f x)) \<in> borel_measurable lborel" by auto
   560   from borel_measurable_implies_simple_function_sequence'[OF this] 
   561   obtain F where F: "\<And>i. simple_function lborel (F i)" "incseq F" 
   562                  "\<And>i x. F i x < top" "\<And>x. (SUP i. F i x) = ennreal (f x)"
   563     by blast
   564   then have [measurable]: "\<And>i. F i \<in> borel_measurable lborel"
   565     by (metis borel_measurable_simple_function)
   566   let ?B = "\<lambda>i::nat. box (- (real i *\<^sub>R One)) (real i *\<^sub>R One) :: 'a set"
   567 
   568   have "0 \<le> I"
   569     using I by (rule has_integral_nonneg) (simp add: nonneg)
   570 
   571   have F_le_f: "enn2real (F i x) \<le> f x" for i x
   572     using F(3,4)[where x=x] nonneg SUP_upper[of i UNIV "\<lambda>i. F i x"]
   573     by (cases "F i x" rule: ennreal_cases) auto
   574   let ?F = "\<lambda>i x. F i x * indicator (?B i) x"
   575   have "(\<integral>\<^sup>+ x. ennreal (f x) \<partial>lborel) = (SUP i. integral\<^sup>N lborel (\<lambda>x. ?F i x))"
   576   proof (subst nn_integral_monotone_convergence_SUP[symmetric])
   577     { fix x
   578       obtain j where j: "x \<in> ?B j"
   579         using UN_box_eq_UNIV by auto
   580 
   581       have "ennreal (f x) = (SUP i. F i x)"
   582         using F(4)[of x] nonneg[of x] by (simp add: max_def)
   583       also have "\<dots> = (SUP i. ?F i x)"
   584       proof (rule SUP_eq)
   585         fix i show "\<exists>j\<in>UNIV. F i x \<le> ?F j x"
   586           using j F(2)
   587           by (intro bexI[of _ "max i j"])
   588              (auto split: split_max split_indicator simp: incseq_def le_fun_def box_def)
   589       qed (auto intro!: F split: split_indicator)
   590       finally have "ennreal (f x) =  (SUP i. ?F i x)" . }
   591     then show "(\<integral>\<^sup>+ x. ennreal (f x) \<partial>lborel) = (\<integral>\<^sup>+ x. (SUP i. ?F i x) \<partial>lborel)"
   592       by simp
   593   qed (insert F, auto simp: incseq_def le_fun_def box_def split: split_indicator)
   594   also have "\<dots> \<le> ennreal I"
   595   proof (rule SUP_least)
   596     fix i :: nat
   597     have finite_F: "(\<integral>\<^sup>+ x. ennreal (enn2real (F i x) * indicator (?B i) x) \<partial>lborel) < \<infinity>"
   598     proof (rule nn_integral_bound_simple_function)
   599       have "emeasure lborel {x \<in> space lborel. ennreal (enn2real (F i x) * indicator (?B i) x) \<noteq> 0} \<le>
   600         emeasure lborel (?B i)"
   601         by (intro emeasure_mono)  (auto split: split_indicator)
   602       then show "emeasure lborel {x \<in> space lborel. ennreal (enn2real (F i x) * indicator (?B i) x) \<noteq> 0} < \<infinity>"
   603         by (auto simp: less_top[symmetric] top_unique)
   604     qed (auto split: split_indicator
   605               intro!: F simple_function_compose1[where g="enn2real"] simple_function_ennreal)
   606 
   607     have int_F: "(\<lambda>x. enn2real (F i x) * indicator (?B i) x) integrable_on UNIV"
   608       using F(4) finite_F
   609       by (intro nn_integral_integrable_on) (auto split: split_indicator simp: enn2real_nonneg)
   610 
   611     have "(\<integral>\<^sup>+ x. F i x * indicator (?B i) x \<partial>lborel) =
   612       (\<integral>\<^sup>+ x. ennreal (enn2real (F i x) * indicator (?B i) x) \<partial>lborel)"
   613       using F(3,4)
   614       by (intro nn_integral_cong) (auto simp: image_iff eq_commute split: split_indicator)
   615     also have "\<dots> = ennreal (integral UNIV (\<lambda>x. enn2real (F i x) * indicator (?B i) x))"
   616       using F
   617       by (intro nn_integral_lborel_eq_integral[OF _ _ finite_F])
   618          (auto split: split_indicator intro: enn2real_nonneg)
   619     also have "\<dots> \<le> ennreal I"
   620       by (auto intro!: has_integral_le[OF integrable_integral[OF int_F] I] nonneg F_le_f
   621                simp: \<open>0 \<le> I\<close> split: split_indicator )
   622     finally show "(\<integral>\<^sup>+ x. F i x * indicator (?B i) x \<partial>lborel) \<le> ennreal I" .
   623   qed
   624   finally have "(\<integral>\<^sup>+ x. ennreal (f x) \<partial>lborel) < \<infinity>"
   625     by (auto simp: less_top[symmetric] top_unique)
   626   from nn_integral_lborel_eq_integral[OF assms(1,2) this] I show ?thesis
   627     by (simp add: integral_unique)
   628 qed
   629 
   630 lemma has_integral_iff_emeasure_lborel:
   631   fixes A :: "'a::euclidean_space set"
   632   assumes A[measurable]: "A \<in> sets borel" and [simp]: "0 \<le> r"
   633   shows "((\<lambda>x. 1) has_integral r) A \<longleftrightarrow> emeasure lborel A = ennreal r"
   634 proof (cases "emeasure lborel A = \<infinity>")
   635   case emeasure_A: True
   636   have "\<not> (\<lambda>x. 1::real) integrable_on A"
   637   proof
   638     assume int: "(\<lambda>x. 1::real) integrable_on A"
   639     then have "(indicator A::'a \<Rightarrow> real) integrable_on UNIV"
   640       unfolding indicator_def[abs_def] integrable_restrict_UNIV .
   641     then obtain r where "((indicator A::'a\<Rightarrow>real) has_integral r) UNIV"
   642       by auto
   643     from nn_integral_has_integral_lborel[OF _ _ this] emeasure_A show False
   644       by (simp add: ennreal_indicator)
   645   qed
   646   with emeasure_A show ?thesis
   647     by auto
   648 next
   649   case False
   650   then have "((\<lambda>x. 1) has_integral measure lborel A) A"
   651     by (simp add: has_integral_measure_lborel less_top)
   652   with False show ?thesis
   653     by (auto simp: emeasure_eq_ennreal_measure has_integral_unique)
   654 qed
   655 
   656 lemma ennreal_max_0: "ennreal (max 0 x) = ennreal x"
   657   by (auto simp: max_def ennreal_neg)
   658 
   659 lemma has_integral_integral_real:
   660   fixes f::"'a::euclidean_space \<Rightarrow> real"
   661   assumes f: "integrable lborel f"
   662   shows "(f has_integral (integral\<^sup>L lborel f)) UNIV"
   663 proof -
   664   from integrableE[OF f] obtain r q
   665     where "0 \<le> r" "0 \<le> q"
   666       and r: "(\<integral>\<^sup>+ x. ennreal (max 0 (f x)) \<partial>lborel) = ennreal r"
   667       and q: "(\<integral>\<^sup>+ x. ennreal (max 0 (- f x)) \<partial>lborel) = ennreal q"
   668       and f: "f \<in> borel_measurable lborel" and eq: "integral\<^sup>L lborel f = r - q"
   669     unfolding ennreal_max_0 by auto
   670   then have "((\<lambda>x. max 0 (f x)) has_integral r) UNIV" "((\<lambda>x. max 0 (- f x)) has_integral q) UNIV"
   671     using nn_integral_has_integral[OF _ _ r] nn_integral_has_integral[OF _ _ q] by auto
   672   note has_integral_diff[OF this]
   673   moreover have "(\<lambda>x. max 0 (f x) - max 0 (- f x)) = f"
   674     by auto
   675   ultimately show ?thesis
   676     by (simp add: eq)
   677 qed
   678 
   679 lemma has_integral_AE:
   680   assumes ae: "AE x in lborel. x \<in> \<Omega> \<longrightarrow> f x = g x"
   681   shows "(f has_integral x) \<Omega> = (g has_integral x) \<Omega>"
   682 proof -
   683   from ae obtain N
   684     where N: "N \<in> sets borel" "emeasure lborel N = 0" "{x. \<not> (x \<in> \<Omega> \<longrightarrow> f x = g x)} \<subseteq> N"
   685     by (auto elim!: AE_E)
   686   then have not_N: "AE x in lborel. x \<notin> N"
   687     by (simp add: AE_iff_measurable)
   688   show ?thesis
   689   proof (rule has_integral_spike_eq[symmetric])
   690     show "\<And>x. x\<in>\<Omega> - N \<Longrightarrow> f x = g x" using N(3) by auto
   691     show "negligible N"
   692       unfolding negligible_def
   693     proof (intro allI)
   694       fix a b :: "'a"
   695       let ?F = "\<lambda>x::'a. if x \<in> cbox a b then indicator N x else 0 :: real"
   696       have "integrable lborel ?F = integrable lborel (\<lambda>x::'a. 0::real)"
   697         using not_N N(1) by (intro integrable_cong_AE) auto
   698       moreover have "(LINT x|lborel. ?F x) = (LINT x::'a|lborel. 0::real)"
   699         using not_N N(1) by (intro integral_cong_AE) auto
   700       ultimately have "(?F has_integral 0) UNIV"
   701         using has_integral_integral_real[of ?F] by simp
   702       then show "(indicator N has_integral (0::real)) (cbox a b)"
   703         unfolding has_integral_restrict_UNIV .
   704     qed
   705   qed
   706 qed
   707 
   708 lemma nn_integral_has_integral_lebesgue:
   709   fixes f :: "'a::euclidean_space \<Rightarrow> real"
   710   assumes nonneg: "\<And>x. 0 \<le> f x" and I: "(f has_integral I) \<Omega>"
   711   shows "integral\<^sup>N lborel (\<lambda>x. indicator \<Omega> x * f x) = I"
   712 proof -
   713   from I have "(\<lambda>x. indicator \<Omega> x *\<^sub>R f x) \<in> lebesgue \<rightarrow>\<^sub>M borel"
   714     by (rule has_integral_implies_lebesgue_measurable)
   715   then obtain f' :: "'a \<Rightarrow> real"
   716     where [measurable]: "f' \<in> borel \<rightarrow>\<^sub>M borel" and eq: "AE x in lborel. indicator \<Omega> x * f x = f' x"
   717     by (auto dest: completion_ex_borel_measurable_real)
   718 
   719   from I have "((\<lambda>x. abs (indicator \<Omega> x * f x)) has_integral I) UNIV"
   720     using nonneg by (simp add: indicator_def if_distrib[of "\<lambda>x. x * f y" for y] cong: if_cong)
   721   also have "((\<lambda>x. abs (indicator \<Omega> x * f x)) has_integral I) UNIV \<longleftrightarrow> ((\<lambda>x. abs (f' x)) has_integral I) UNIV"
   722     using eq by (intro has_integral_AE) auto
   723   finally have "integral\<^sup>N lborel (\<lambda>x. abs (f' x)) = I"
   724     by (rule nn_integral_has_integral_lborel[rotated 2]) auto
   725   also have "integral\<^sup>N lborel (\<lambda>x. abs (f' x)) = integral\<^sup>N lborel (\<lambda>x. abs (indicator \<Omega> x * f x))"
   726     using eq by (intro nn_integral_cong_AE) auto
   727   finally show ?thesis
   728     using nonneg by auto
   729 qed
   730 
   731 lemma has_integral_iff_nn_integral_lebesgue:
   732   assumes f: "\<And>x. 0 \<le> f x"
   733   shows "(f has_integral r) UNIV \<longleftrightarrow> (f \<in> lebesgue \<rightarrow>\<^sub>M borel \<and> integral\<^sup>N lebesgue f = r \<and> 0 \<le> r)" (is "?I = ?N")
   734 proof
   735   assume ?I
   736   have "0 \<le> r"
   737     using has_integral_nonneg[OF \<open>?I\<close>] f by auto
   738   then show ?N
   739     using nn_integral_has_integral_lebesgue[OF f \<open>?I\<close>]
   740       has_integral_implies_lebesgue_measurable[OF \<open>?I\<close>]
   741     by (auto simp: nn_integral_completion)
   742 next
   743   assume ?N
   744   then obtain f' where f': "f' \<in> borel \<rightarrow>\<^sub>M borel" "AE x in lborel. f x = f' x"
   745     by (auto dest: completion_ex_borel_measurable_real)
   746   moreover have "(\<integral>\<^sup>+ x. ennreal \<bar>f' x\<bar> \<partial>lborel) = (\<integral>\<^sup>+ x. ennreal \<bar>f x\<bar> \<partial>lborel)"
   747     using f' by (intro nn_integral_cong_AE) auto
   748   moreover have "((\<lambda>x. \<bar>f' x\<bar>) has_integral r) UNIV \<longleftrightarrow> ((\<lambda>x. \<bar>f x\<bar>) has_integral r) UNIV"
   749     using f' by (intro has_integral_AE) auto
   750   moreover note nn_integral_has_integral[of "\<lambda>x. \<bar>f' x\<bar>" r] \<open>?N\<close>
   751   ultimately show ?I
   752     using f by (auto simp: nn_integral_completion)
   753 qed
   754 
   755 context
   756   fixes f::"'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
   757 begin
   758 
   759 lemma has_integral_integral_lborel:
   760   assumes f: "integrable lborel f"
   761   shows "(f has_integral (integral\<^sup>L lborel f)) UNIV"
   762 proof -
   763   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"
   764     using f by (intro has_integral_sum finite_Basis ballI has_integral_scaleR_left has_integral_integral_real) auto
   765   also have eq_f: "(\<lambda>x. \<Sum>b\<in>Basis. (f x \<bullet> b) *\<^sub>R b) = f"
   766     by (simp add: fun_eq_iff euclidean_representation)
   767   also have "(\<Sum>b\<in>Basis. integral\<^sup>L lborel (\<lambda>x. f x \<bullet> b) *\<^sub>R b) = integral\<^sup>L lborel f"
   768     using f by (subst (2) eq_f[symmetric]) simp
   769   finally show ?thesis .
   770 qed
   771 
   772 lemma integrable_on_lborel: "integrable lborel f \<Longrightarrow> f integrable_on UNIV"
   773   using has_integral_integral_lborel by auto
   774 
   775 lemma integral_lborel: "integrable lborel f \<Longrightarrow> integral UNIV f = (\<integral>x. f x \<partial>lborel)"
   776   using has_integral_integral_lborel by auto
   777 
   778 end
   779 
   780 context
   781 begin
   782 
   783 private lemma has_integral_integral_lebesgue_real:
   784   fixes f :: "'a::euclidean_space \<Rightarrow> real"
   785   assumes f: "integrable lebesgue f"
   786   shows "(f has_integral (integral\<^sup>L lebesgue f)) UNIV"
   787 proof -
   788   obtain f' where f': "f' \<in> borel \<rightarrow>\<^sub>M borel" "AE x in lborel. f x = f' x"
   789     using completion_ex_borel_measurable_real[OF borel_measurable_integrable[OF f]] by auto
   790   moreover have "(\<integral>\<^sup>+ x. ennreal (norm (f x)) \<partial>lborel) = (\<integral>\<^sup>+ x. ennreal (norm (f' x)) \<partial>lborel)"
   791     using f' by (intro nn_integral_cong_AE) auto
   792   ultimately have "integrable lborel f'"
   793     using f by (auto simp: integrable_iff_bounded nn_integral_completion cong: nn_integral_cong_AE)
   794   note has_integral_integral_real[OF this]
   795   moreover have "integral\<^sup>L lebesgue f = integral\<^sup>L lebesgue f'"
   796     using f' f by (intro integral_cong_AE) (auto intro: AE_completion measurable_completion)
   797   moreover have "integral\<^sup>L lebesgue f' = integral\<^sup>L lborel f'"
   798     using f' by (simp add: integral_completion)
   799   moreover have "(f' has_integral integral\<^sup>L lborel f') UNIV \<longleftrightarrow> (f has_integral integral\<^sup>L lborel f') UNIV"
   800     using f' by (intro has_integral_AE) auto
   801   ultimately show ?thesis
   802     by auto
   803 qed
   804 
   805 lemma has_integral_integral_lebesgue:
   806   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
   807   assumes f: "integrable lebesgue f"
   808   shows "(f has_integral (integral\<^sup>L lebesgue f)) UNIV"
   809 proof -
   810   have "((\<lambda>x. \<Sum>b\<in>Basis. (f x \<bullet> b) *\<^sub>R b) has_integral (\<Sum>b\<in>Basis. integral\<^sup>L lebesgue (\<lambda>x. f x \<bullet> b) *\<^sub>R b)) UNIV"
   811     using f by (intro has_integral_sum finite_Basis ballI has_integral_scaleR_left has_integral_integral_lebesgue_real) auto
   812   also have eq_f: "(\<lambda>x. \<Sum>b\<in>Basis. (f x \<bullet> b) *\<^sub>R b) = f"
   813     by (simp add: fun_eq_iff euclidean_representation)
   814   also have "(\<Sum>b\<in>Basis. integral\<^sup>L lebesgue (\<lambda>x. f x \<bullet> b) *\<^sub>R b) = integral\<^sup>L lebesgue f"
   815     using f by (subst (2) eq_f[symmetric]) simp
   816   finally show ?thesis .
   817 qed
   818 
   819 lemma integrable_on_lebesgue:
   820   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
   821   shows "integrable lebesgue f \<Longrightarrow> f integrable_on UNIV"
   822   using has_integral_integral_lebesgue by auto
   823 
   824 lemma integral_lebesgue:
   825   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
   826   shows "integrable lebesgue f \<Longrightarrow> integral UNIV f = (\<integral>x. f x \<partial>lebesgue)"
   827   using has_integral_integral_lebesgue by auto
   828 
   829 end
   830 
   831 subsection \<open>Absolute integrability (this is the same as Lebesgue integrability)\<close>
   832 
   833 translations
   834 "LBINT x. f" == "CONST lebesgue_integral CONST lborel (\<lambda>x. f)"
   835 
   836 translations
   837 "LBINT x:A. f" == "CONST set_lebesgue_integral CONST lborel A (\<lambda>x. f)"
   838 
   839 lemma set_integral_reflect:
   840   fixes S and f :: "real \<Rightarrow> 'a :: {banach, second_countable_topology}"
   841   shows "(LBINT x : S. f x) = (LBINT x : {x. - x \<in> S}. f (- x))"
   842   unfolding set_lebesgue_integral_def
   843   by (subst lborel_integral_real_affine[where c="-1" and t=0])
   844      (auto intro!: Bochner_Integration.integral_cong split: split_indicator)
   845 
   846 lemma borel_integrable_atLeastAtMost':
   847   fixes f :: "real \<Rightarrow> 'a::{banach, second_countable_topology}"
   848   assumes f: "continuous_on {a..b} f"
   849   shows "set_integrable lborel {a..b} f" 
   850   unfolding set_integrable_def
   851   by (intro borel_integrable_compact compact_Icc f)
   852 
   853 lemma integral_FTC_atLeastAtMost:
   854   fixes f :: "real \<Rightarrow> 'a :: euclidean_space"
   855   assumes "a \<le> b"
   856     and F: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> (F has_vector_derivative f x) (at x within {a .. b})"
   857     and f: "continuous_on {a .. b} f"
   858   shows "integral\<^sup>L lborel (\<lambda>x. indicator {a .. b} x *\<^sub>R f x) = F b - F a"
   859 proof -
   860   let ?f = "\<lambda>x. indicator {a .. b} x *\<^sub>R f x"
   861   have "(?f has_integral (\<integral>x. ?f x \<partial>lborel)) UNIV"
   862     using borel_integrable_atLeastAtMost'[OF f]
   863     unfolding set_integrable_def by (rule has_integral_integral_lborel)
   864   moreover
   865   have "(f has_integral F b - F a) {a .. b}"
   866     by (intro fundamental_theorem_of_calculus ballI assms) auto
   867   then have "(?f has_integral F b - F a) {a .. b}"
   868     by (subst has_integral_cong[where g=f]) auto
   869   then have "(?f has_integral F b - F a) UNIV"
   870     by (intro has_integral_on_superset[where T=UNIV and S="{a..b}"]) auto
   871   ultimately show "integral\<^sup>L lborel ?f = F b - F a"
   872     by (rule has_integral_unique)
   873 qed
   874 
   875 lemma set_borel_integral_eq_integral:
   876   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
   877   assumes "set_integrable lborel S f"
   878   shows "f integrable_on S" "LINT x : S | lborel. f x = integral S f"
   879 proof -
   880   let ?f = "\<lambda>x. indicator S x *\<^sub>R f x"
   881   have "(?f has_integral LINT x : S | lborel. f x) UNIV"
   882     using assms has_integral_integral_lborel 
   883     unfolding set_integrable_def set_lebesgue_integral_def by blast
   884   hence 1: "(f has_integral (set_lebesgue_integral lborel S f)) S"
   885     apply (subst has_integral_restrict_UNIV [symmetric])
   886     apply (rule has_integral_eq)
   887     by auto
   888   thus "f integrable_on S"
   889     by (auto simp add: integrable_on_def)
   890   with 1 have "(f has_integral (integral S f)) S"
   891     by (intro integrable_integral, auto simp add: integrable_on_def)
   892   thus "LINT x : S | lborel. f x = integral S f"
   893     by (intro has_integral_unique [OF 1])
   894 qed
   895 
   896 lemma has_integral_set_lebesgue:
   897   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
   898   assumes f: "set_integrable lebesgue S f"
   899   shows "(f has_integral (LINT x:S|lebesgue. f x)) S"
   900   using has_integral_integral_lebesgue f 
   901   by (fastforce simp add: set_integrable_def set_lebesgue_integral_def indicator_def if_distrib[of "\<lambda>x. x *\<^sub>R f _"] cong: if_cong)
   902 
   903 lemma set_lebesgue_integral_eq_integral:
   904   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
   905   assumes f: "set_integrable lebesgue S f"
   906   shows "f integrable_on S" "LINT x:S | lebesgue. f x = integral S f"
   907   using has_integral_set_lebesgue[OF f] by (auto simp: integral_unique integrable_on_def)
   908 
   909 lemma lmeasurable_iff_has_integral:
   910   "S \<in> lmeasurable \<longleftrightarrow> ((indicator S) has_integral measure lebesgue S) UNIV"
   911   by (subst has_integral_iff_nn_integral_lebesgue)
   912      (auto simp: ennreal_indicator emeasure_eq_measure2 borel_measurable_indicator_iff intro!: fmeasurableI)
   913 
   914 abbreviation
   915   absolutely_integrable_on :: "('a::euclidean_space \<Rightarrow> 'b::{banach, second_countable_topology}) \<Rightarrow> 'a set \<Rightarrow> bool"
   916   (infixr "absolutely'_integrable'_on" 46)
   917   where "f absolutely_integrable_on s \<equiv> set_integrable lebesgue s f"
   918 
   919 
   920 lemma absolutely_integrable_zero [simp]: "(\<lambda>x. 0) absolutely_integrable_on S"
   921     by (simp add: set_integrable_def)
   922 
   923 lemma absolutely_integrable_on_def:
   924   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
   925   shows "f absolutely_integrable_on S \<longleftrightarrow> f integrable_on S \<and> (\<lambda>x. norm (f x)) integrable_on S"
   926 proof safe
   927   assume f: "f absolutely_integrable_on S"
   928   then have nf: "integrable lebesgue (\<lambda>x. norm (indicator S x *\<^sub>R f x))"
   929     using integrable_norm set_integrable_def by blast
   930   show "f integrable_on S"
   931     by (rule set_lebesgue_integral_eq_integral[OF f])
   932   have "(\<lambda>x. norm (indicator S x *\<^sub>R f x)) = (\<lambda>x. if x \<in> S then norm (f x) else 0)"
   933     by auto
   934   with integrable_on_lebesgue[OF nf] show "(\<lambda>x. norm (f x)) integrable_on S"
   935     by (simp add: integrable_restrict_UNIV)
   936 next
   937   assume f: "f integrable_on S" and nf: "(\<lambda>x. norm (f x)) integrable_on S"
   938   show "f absolutely_integrable_on S"
   939     unfolding set_integrable_def
   940   proof (rule integrableI_bounded)
   941     show "(\<lambda>x. indicator S x *\<^sub>R f x) \<in> borel_measurable lebesgue"
   942       using f has_integral_implies_lebesgue_measurable[of f _ S] by (auto simp: integrable_on_def)
   943     show "(\<integral>\<^sup>+ x. ennreal (norm (indicator S x *\<^sub>R f x)) \<partial>lebesgue) < \<infinity>"
   944       using nf nn_integral_has_integral_lebesgue[of "\<lambda>x. norm (f x)" _ S]
   945       by (auto simp: integrable_on_def nn_integral_completion)
   946   qed
   947 qed
   948 
   949 lemma integrable_on_lebesgue_on:
   950   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
   951   assumes f: "integrable (lebesgue_on S) f" and S: "S \<in> sets lebesgue"
   952   shows "f integrable_on S"
   953 proof -
   954   have "integrable lebesgue (\<lambda>x. indicator S x *\<^sub>R f x)"
   955     using S f inf_top.comm_neutral integrable_restrict_space by blast
   956   then show ?thesis
   957     using absolutely_integrable_on_def set_integrable_def by blast
   958 qed
   959 
   960 lemma absolutely_integrable_on_null [intro]:
   961   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
   962   shows "content (cbox a b) = 0 \<Longrightarrow> f absolutely_integrable_on (cbox a b)"
   963   by (auto simp: absolutely_integrable_on_def)
   964 
   965 lemma absolutely_integrable_on_open_interval:
   966   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
   967   shows "f absolutely_integrable_on box a b \<longleftrightarrow>
   968          f absolutely_integrable_on cbox a b"
   969   by (auto simp: integrable_on_open_interval absolutely_integrable_on_def)
   970 
   971 lemma absolutely_integrable_restrict_UNIV:
   972   "(\<lambda>x. if x \<in> S then f x else 0) absolutely_integrable_on UNIV \<longleftrightarrow> f absolutely_integrable_on S"
   973     unfolding set_integrable_def
   974   by (intro arg_cong2[where f=integrable]) auto
   975 
   976 lemma absolutely_integrable_onI:
   977   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
   978   shows "f integrable_on S \<Longrightarrow> (\<lambda>x. norm (f x)) integrable_on S \<Longrightarrow> f absolutely_integrable_on S"
   979   unfolding absolutely_integrable_on_def by auto
   980 
   981 lemma nonnegative_absolutely_integrable_1:
   982   fixes f :: "'a :: euclidean_space \<Rightarrow> real"
   983   assumes f: "f integrable_on A" and "\<And>x. x \<in> A \<Longrightarrow> 0 \<le> f x"
   984   shows "f absolutely_integrable_on A"
   985   by (rule absolutely_integrable_onI [OF f]) (use assms in \<open>simp add: integrable_eq\<close>)
   986 
   987 lemma absolutely_integrable_on_iff_nonneg:
   988   fixes f :: "'a :: euclidean_space \<Rightarrow> real"
   989   assumes "\<And>x. x \<in> S \<Longrightarrow> 0 \<le> f x" shows "f absolutely_integrable_on S \<longleftrightarrow> f integrable_on S"
   990 proof -
   991   { assume "f integrable_on S"
   992     then have "(\<lambda>x. if x \<in> S then f x else 0) integrable_on UNIV"
   993       by (simp add: integrable_restrict_UNIV)
   994     then have "(\<lambda>x. if x \<in> S then f x else 0) absolutely_integrable_on UNIV"
   995       using \<open>f integrable_on S\<close> absolutely_integrable_restrict_UNIV assms nonnegative_absolutely_integrable_1 by blast
   996     then have "f absolutely_integrable_on S"
   997       using absolutely_integrable_restrict_UNIV by blast
   998   }
   999   then show ?thesis        
  1000     unfolding absolutely_integrable_on_def by auto
  1001 qed
  1002 
  1003 lemma absolutely_integrable_on_scaleR_iff:
  1004   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1005   shows
  1006    "(\<lambda>x. c *\<^sub>R f x) absolutely_integrable_on S \<longleftrightarrow>
  1007       c = 0 \<or> f absolutely_integrable_on S"
  1008 proof (cases "c=0")
  1009   case False
  1010   then show ?thesis
  1011   unfolding absolutely_integrable_on_def 
  1012   by (simp add: norm_mult)
  1013 qed auto
  1014 
  1015 lemma absolutely_integrable_spike:
  1016   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1017   assumes "f absolutely_integrable_on T" and S: "negligible S" "\<And>x. x \<in> T - S \<Longrightarrow> g x = f x"
  1018   shows "g absolutely_integrable_on T"
  1019   using assms integrable_spike
  1020   unfolding absolutely_integrable_on_def by metis
  1021 
  1022 lemma absolutely_integrable_negligible:
  1023   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1024   assumes "negligible S"
  1025   shows "f absolutely_integrable_on S"
  1026   using assms by (simp add: absolutely_integrable_on_def integrable_negligible)
  1027 
  1028 lemma absolutely_integrable_spike_eq:
  1029   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1030   assumes "negligible S" "\<And>x. x \<in> T - S \<Longrightarrow> g x = f x"
  1031   shows "(f absolutely_integrable_on T \<longleftrightarrow> g absolutely_integrable_on T)"
  1032   using assms by (blast intro: absolutely_integrable_spike sym)
  1033 
  1034 lemma absolutely_integrable_spike_set_eq:
  1035   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1036   assumes "negligible {x \<in> S - T. f x \<noteq> 0}" "negligible {x \<in> T - S. f x \<noteq> 0}"
  1037   shows "(f absolutely_integrable_on S \<longleftrightarrow> f absolutely_integrable_on T)"
  1038 proof -
  1039   have "(\<lambda>x. if x \<in> S then f x else 0) absolutely_integrable_on UNIV \<longleftrightarrow>
  1040         (\<lambda>x. if x \<in> T then f x else 0) absolutely_integrable_on UNIV"
  1041   proof (rule absolutely_integrable_spike_eq)
  1042     show "negligible ({x \<in> S - T. f x \<noteq> 0} \<union> {x \<in> T - S. f x \<noteq> 0})"
  1043       by (rule negligible_Un [OF assms])
  1044   qed auto
  1045   with absolutely_integrable_restrict_UNIV show ?thesis
  1046     by blast
  1047 qed
  1048 
  1049 lemma absolutely_integrable_spike_set:
  1050   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1051   assumes f: "f absolutely_integrable_on S" and neg: "negligible {x \<in> S - T. f x \<noteq> 0}" "negligible {x \<in> T - S. f x \<noteq> 0}"
  1052   shows "f absolutely_integrable_on T"
  1053   using absolutely_integrable_spike_set_eq f neg by blast
  1054 
  1055 lemma lmeasurable_iff_integrable_on: "S \<in> lmeasurable \<longleftrightarrow> (\<lambda>x. 1::real) integrable_on S"
  1056   by (subst absolutely_integrable_on_iff_nonneg[symmetric])
  1057      (simp_all add: lmeasurable_iff_integrable set_integrable_def)
  1058 
  1059 lemma lmeasure_integral_UNIV: "S \<in> lmeasurable \<Longrightarrow> measure lebesgue S = integral UNIV (indicator S)"
  1060   by (simp add: lmeasurable_iff_has_integral integral_unique)
  1061 
  1062 lemma lmeasure_integral: "S \<in> lmeasurable \<Longrightarrow> measure lebesgue S = integral S (\<lambda>x. 1::real)"
  1063   by (fastforce simp add: lmeasure_integral_UNIV indicator_def[abs_def] lmeasurable_iff_integrable_on)
  1064 
  1065 lemma integrable_on_const: "S \<in> lmeasurable \<Longrightarrow> (\<lambda>x. c) integrable_on S"
  1066   unfolding lmeasurable_iff_integrable
  1067   by (metis (mono_tags, lifting) integrable_eq integrable_on_scaleR_left lmeasurable_iff_integrable lmeasurable_iff_integrable_on scaleR_one)
  1068 
  1069 lemma integral_indicator:
  1070   assumes "(S \<inter> T) \<in> lmeasurable"
  1071   shows "integral T (indicator S) = measure lebesgue (S \<inter> T)"
  1072 proof -
  1073   have "integral UNIV (indicator (S \<inter> T)) = integral UNIV (\<lambda>a. if a \<in> S \<inter> T then 1::real else 0)"
  1074     by (meson indicator_def)
  1075   moreover
  1076   have "(indicator (S \<inter> T) has_integral measure lebesgue (S \<inter> T)) UNIV"
  1077     using assms by (simp add: lmeasurable_iff_has_integral)
  1078   ultimately have "integral UNIV (\<lambda>x. if x \<in> S \<inter> T then 1 else 0) = measure lebesgue (S \<inter> T)"
  1079     by (metis (no_types) integral_unique)
  1080   then show ?thesis
  1081     using integral_restrict_Int [of UNIV "S \<inter> T" "\<lambda>x. 1::real"]
  1082     apply (simp add: integral_restrict_Int [symmetric])
  1083     by (meson indicator_def)
  1084 qed
  1085 
  1086 lemma measurable_integrable:
  1087   fixes S :: "'a::euclidean_space set"
  1088   shows "S \<in> lmeasurable \<longleftrightarrow> (indicat_real S) integrable_on UNIV"
  1089   by (auto simp: lmeasurable_iff_integrable absolutely_integrable_on_iff_nonneg [symmetric] set_integrable_def)
  1090 
  1091 lemma integrable_on_indicator:
  1092   fixes S :: "'a::euclidean_space set"
  1093   shows "indicat_real S integrable_on T \<longleftrightarrow> (S \<inter> T) \<in> lmeasurable"
  1094   unfolding measurable_integrable
  1095   unfolding integrable_restrict_UNIV [of T, symmetric]
  1096   by (fastforce simp add: indicator_def elim: integrable_eq)
  1097 
  1098 lemma
  1099   assumes \<D>: "\<D> division_of S"
  1100   shows lmeasurable_division: "S \<in> lmeasurable" (is ?l)
  1101     and content_division: "(\<Sum>k\<in>\<D>. measure lebesgue k) = measure lebesgue S" (is ?m)
  1102 proof -
  1103   { fix d1 d2 assume *: "d1 \<in> \<D>" "d2 \<in> \<D>" "d1 \<noteq> d2"
  1104     then obtain a b c d where "d1 = cbox a b" "d2 = cbox c d"
  1105       using division_ofD(4)[OF \<D>] by blast
  1106     with division_ofD(5)[OF \<D> *]
  1107     have "d1 \<in> sets lborel" "d2 \<in> sets lborel" "d1 \<inter> d2 \<subseteq> (cbox a b - box a b) \<union> (cbox c d - box c d)"
  1108       by auto
  1109     moreover have "(cbox a b - box a b) \<union> (cbox c d - box c d) \<in> null_sets lborel"
  1110       by (intro null_sets.Un null_sets_cbox_Diff_box)
  1111     ultimately have "d1 \<inter> d2 \<in> null_sets lborel"
  1112       by (blast intro: null_sets_subset) }
  1113   then show ?l ?m
  1114     unfolding division_ofD(6)[OF \<D>, symmetric]
  1115     using division_ofD(1,4)[OF \<D>]
  1116     by (auto intro!: measure_Union_AE[symmetric] simp: completion.AE_iff_null_sets Int_def[symmetric] pairwise_def null_sets_def)
  1117 qed
  1118 
  1119 lemma has_measure_limit:
  1120   assumes "S \<in> lmeasurable" "e > 0"
  1121   obtains B where "B > 0"
  1122     "\<And>a b. ball 0 B \<subseteq> cbox a b \<Longrightarrow> \<bar>measure lebesgue (S \<inter> cbox a b) - measure lebesgue S\<bar> < e"
  1123   using assms unfolding lmeasurable_iff_has_integral has_integral_alt'
  1124   by (force simp: integral_indicator integrable_on_indicator)
  1125 
  1126 lemma lmeasurable_iff_indicator_has_integral:
  1127   fixes S :: "'a::euclidean_space set"
  1128   shows "S \<in> lmeasurable \<and> m = measure lebesgue S \<longleftrightarrow> (indicat_real S has_integral m) UNIV"
  1129   by (metis has_integral_iff lmeasurable_iff_has_integral measurable_integrable)
  1130 
  1131 lemma has_measure_limit_iff:
  1132   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  1133   shows "S \<in> lmeasurable \<and> m = measure lebesgue S \<longleftrightarrow>
  1134           (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
  1135             (S \<inter> cbox a b) \<in> lmeasurable \<and> \<bar>measure lebesgue (S \<inter> cbox a b) - m\<bar> < e)" (is "?lhs = ?rhs")
  1136 proof
  1137   assume ?lhs then show ?rhs
  1138     by (meson has_measure_limit fmeasurable.Int lmeasurable_cbox)
  1139 next
  1140   assume RHS [rule_format]: ?rhs
  1141   show ?lhs
  1142     apply (simp add: lmeasurable_iff_indicator_has_integral has_integral' [where i=m])
  1143     using RHS
  1144     by (metis (full_types) integral_indicator integrable_integral integrable_on_indicator)
  1145 qed
  1146 
  1147 subsection\<open>Applications to Negligibility\<close>
  1148 
  1149 lemma negligible_iff_null_sets: "negligible S \<longleftrightarrow> S \<in> null_sets lebesgue"
  1150 proof
  1151   assume "negligible S"
  1152   then have "(indicator S has_integral (0::real)) UNIV"
  1153     by (auto simp: negligible)
  1154   then show "S \<in> null_sets lebesgue"
  1155     by (subst (asm) has_integral_iff_nn_integral_lebesgue)
  1156         (auto simp: borel_measurable_indicator_iff nn_integral_0_iff_AE AE_iff_null_sets indicator_eq_0_iff)
  1157 next
  1158   assume S: "S \<in> null_sets lebesgue"
  1159   show "negligible S"
  1160     unfolding negligible_def
  1161   proof (safe intro!: has_integral_iff_nn_integral_lebesgue[THEN iffD2]
  1162                       has_integral_restrict_UNIV[where s="cbox _ _", THEN iffD1])
  1163     fix a b
  1164     show "(\<lambda>x. if x \<in> cbox a b then indicator S x else 0) \<in> lebesgue \<rightarrow>\<^sub>M borel"
  1165       using S by (auto intro!: measurable_If)
  1166     then show "(\<integral>\<^sup>+ x. ennreal (if x \<in> cbox a b then indicator S x else 0) \<partial>lebesgue) = ennreal 0"
  1167       using S[THEN AE_not_in] by (auto intro!: nn_integral_0_iff_AE[THEN iffD2])
  1168   qed auto
  1169 qed
  1170 
  1171 lemma starlike_negligible:
  1172   assumes "closed S"
  1173       and eq1: "\<And>c x. \<lbrakk>(a + c *\<^sub>R x) \<in> S; 0 \<le> c; a + x \<in> S\<rbrakk> \<Longrightarrow> c = 1"
  1174     shows "negligible S"
  1175 proof -
  1176   have "negligible ((+) (-a) ` S)"
  1177   proof (subst negligible_on_intervals, intro allI)
  1178     fix u v
  1179     show "negligible ((+) (- a) ` S \<inter> cbox u v)"
  1180       unfolding negligible_iff_null_sets
  1181       apply (rule starlike_negligible_compact)
  1182        apply (simp add: assms closed_translation closed_Int_compact, clarify)
  1183       by (metis eq1 minus_add_cancel)
  1184   qed
  1185   then show ?thesis
  1186     by (rule negligible_translation_rev)
  1187 qed
  1188 
  1189 lemma starlike_negligible_strong:
  1190   assumes "closed S"
  1191       and star: "\<And>c x. \<lbrakk>0 \<le> c; c < 1; a+x \<in> S\<rbrakk> \<Longrightarrow> a + c *\<^sub>R x \<notin> S"
  1192     shows "negligible S"
  1193 proof -
  1194   show ?thesis
  1195   proof (rule starlike_negligible [OF \<open>closed S\<close>, of a])
  1196     fix c x
  1197     assume cx: "a + c *\<^sub>R x \<in> S" "0 \<le> c" "a + x \<in> S"
  1198     with star have "\<not> (c < 1)" by auto
  1199     moreover have "\<not> (c > 1)"
  1200       using star [of "1/c" "c *\<^sub>R x"] cx by force
  1201     ultimately show "c = 1" by arith
  1202   qed
  1203 qed
  1204 
  1205 lemma negligible_hyperplane:
  1206   assumes "a \<noteq> 0 \<or> b \<noteq> 0" shows "negligible {x. a \<bullet> x = b}"
  1207 proof -
  1208   obtain x where x: "a \<bullet> x \<noteq> b"
  1209     using assms
  1210     apply auto
  1211      apply (metis inner_eq_zero_iff inner_zero_right)
  1212     using inner_zero_right by fastforce
  1213   show ?thesis
  1214     apply (rule starlike_negligible [OF closed_hyperplane, of x])
  1215     using x apply (auto simp: algebra_simps)
  1216     done
  1217 qed
  1218 
  1219 lemma negligible_lowdim:
  1220   fixes S :: "'N :: euclidean_space set"
  1221   assumes "dim S < DIM('N)"
  1222     shows "negligible S"
  1223 proof -
  1224   obtain a where "a \<noteq> 0" and a: "span S \<subseteq> {x. a \<bullet> x = 0}"
  1225     using lowdim_subset_hyperplane [OF assms] by blast
  1226   have "negligible (span S)"
  1227     using \<open>a \<noteq> 0\<close> a negligible_hyperplane by (blast intro: negligible_subset)
  1228   then show ?thesis
  1229     using span_base by (blast intro: negligible_subset)
  1230 qed
  1231 
  1232 proposition negligible_convex_frontier:
  1233   fixes S :: "'N :: euclidean_space set"
  1234   assumes "convex S"
  1235     shows "negligible(frontier S)"
  1236 proof -
  1237   have nf: "negligible(frontier S)" if "convex S" "0 \<in> S" for S :: "'N set"
  1238   proof -
  1239     obtain B where "B \<subseteq> S" and indB: "independent B"
  1240                and spanB: "S \<subseteq> span B" and cardB: "card B = dim S"
  1241       by (metis basis_exists)
  1242     consider "dim S < DIM('N)" | "dim S = DIM('N)"
  1243       using dim_subset_UNIV le_eq_less_or_eq by auto
  1244     then show ?thesis
  1245     proof cases
  1246       case 1
  1247       show ?thesis
  1248         by (rule negligible_subset [of "closure S"])
  1249            (simp_all add: frontier_def negligible_lowdim 1)
  1250     next
  1251       case 2
  1252       obtain a where a: "a \<in> interior S"
  1253         apply (rule interior_simplex_nonempty [OF indB])
  1254           apply (simp add: indB independent_finite)
  1255          apply (simp add: cardB 2)
  1256         apply (metis \<open>B \<subseteq> S\<close> \<open>0 \<in> S\<close> \<open>convex S\<close> insert_absorb insert_subset interior_mono subset_hull)
  1257         done
  1258       show ?thesis
  1259       proof (rule starlike_negligible_strong [where a=a])
  1260         fix c::real and x
  1261         have eq: "a + c *\<^sub>R x = (a + x) - (1 - c) *\<^sub>R ((a + x) - a)"
  1262           by (simp add: algebra_simps)
  1263         assume "0 \<le> c" "c < 1" "a + x \<in> frontier S"
  1264         then show "a + c *\<^sub>R x \<notin> frontier S"
  1265           apply (clarsimp simp: frontier_def)
  1266           apply (subst eq)
  1267           apply (rule mem_interior_closure_convex_shrink [OF \<open>convex S\<close> a, of _ "1-c"], auto)
  1268           done
  1269       qed auto
  1270     qed
  1271   qed
  1272   show ?thesis
  1273   proof (cases "S = {}")
  1274     case True then show ?thesis by auto
  1275   next
  1276     case False
  1277     then obtain a where "a \<in> S" by auto
  1278     show ?thesis
  1279       using nf [of "(\<lambda>x. -a + x) ` S"]
  1280       by (metis \<open>a \<in> S\<close> add.left_inverse assms convex_translation_eq frontier_translation
  1281                 image_eqI negligible_translation_rev)
  1282   qed
  1283 qed
  1284 
  1285 corollary negligible_sphere: "negligible (sphere a e)"
  1286   using frontier_cball negligible_convex_frontier convex_cball
  1287   by (blast intro: negligible_subset)
  1288 
  1289 lemma non_negligible_UNIV [simp]: "\<not> negligible UNIV"
  1290   unfolding negligible_iff_null_sets by (auto simp: null_sets_def)
  1291 
  1292 lemma negligible_interval:
  1293   "negligible (cbox a b) \<longleftrightarrow> box a b = {}" "negligible (box a b) \<longleftrightarrow> box a b = {}"
  1294    by (auto simp: negligible_iff_null_sets null_sets_def prod_nonneg inner_diff_left box_eq_empty
  1295                   not_le emeasure_lborel_cbox_eq emeasure_lborel_box_eq
  1296             intro: eq_refl antisym less_imp_le)
  1297 
  1298 proposition open_not_negligible:
  1299   assumes "open S" "S \<noteq> {}"
  1300   shows "\<not> negligible S"
  1301 proof
  1302   assume neg: "negligible S"
  1303   obtain a where "a \<in> S"
  1304     using \<open>S \<noteq> {}\<close> by blast
  1305   then obtain e where "e > 0" "cball a e \<subseteq> S"
  1306     using \<open>open S\<close> open_contains_cball_eq by blast
  1307   let ?p = "a - (e / DIM('a)) *\<^sub>R One" let ?q = "a + (e / DIM('a)) *\<^sub>R One"
  1308   have "cbox ?p ?q \<subseteq> cball a e"
  1309   proof (clarsimp simp: mem_box dist_norm)
  1310     fix x
  1311     assume "\<forall>i\<in>Basis. ?p \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> ?q \<bullet> i"
  1312     then have ax: "\<bar>(a - x) \<bullet> i\<bar> \<le> e / real DIM('a)" if "i \<in> Basis" for i
  1313       using that by (auto simp: algebra_simps)
  1314     have "norm (a - x) \<le> (\<Sum>i\<in>Basis. \<bar>(a - x) \<bullet> i\<bar>)"
  1315       by (rule norm_le_l1)
  1316     also have "\<dots> \<le> DIM('a) * (e / real DIM('a))"
  1317       by (intro sum_bounded_above ax)
  1318     also have "\<dots> = e"
  1319       by simp
  1320     finally show "norm (a - x) \<le> e" .
  1321   qed
  1322   then have "negligible (cbox ?p ?q)"
  1323     by (meson \<open>cball a e \<subseteq> S\<close> neg negligible_subset)
  1324   with \<open>e > 0\<close> show False
  1325     by (simp add: negligible_interval box_eq_empty algebra_simps divide_simps mult_le_0_iff)
  1326 qed
  1327 
  1328 lemma negligible_convex_interior:
  1329    "convex S \<Longrightarrow> (negligible S \<longleftrightarrow> interior S = {})"
  1330   apply safe
  1331   apply (metis interior_subset negligible_subset open_interior open_not_negligible)
  1332    apply (metis Diff_empty closure_subset frontier_def negligible_convex_frontier negligible_subset)
  1333   done
  1334 
  1335 lemma measure_eq_0_null_sets: "S \<in> null_sets M \<Longrightarrow> measure M S = 0"
  1336   by (auto simp: measure_def null_sets_def)
  1337 
  1338 lemma negligible_imp_measure0: "negligible S \<Longrightarrow> measure lebesgue S = 0"
  1339   by (simp add: measure_eq_0_null_sets negligible_iff_null_sets)
  1340 
  1341 lemma negligible_iff_emeasure0: "S \<in> sets lebesgue \<Longrightarrow> (negligible S \<longleftrightarrow> emeasure lebesgue S = 0)"
  1342   by (auto simp: measure_eq_0_null_sets negligible_iff_null_sets)
  1343 
  1344 lemma negligible_iff_measure0: "S \<in> lmeasurable \<Longrightarrow> (negligible S \<longleftrightarrow> measure lebesgue S = 0)"
  1345   apply (auto simp: measure_eq_0_null_sets negligible_iff_null_sets)
  1346   by (metis completion.null_sets_outer subsetI)
  1347 
  1348 lemma negligible_imp_sets: "negligible S \<Longrightarrow> S \<in> sets lebesgue"
  1349   by (simp add: negligible_iff_null_sets null_setsD2)
  1350 
  1351 lemma negligible_imp_measurable: "negligible S \<Longrightarrow> S \<in> lmeasurable"
  1352   by (simp add: fmeasurableI_null_sets negligible_iff_null_sets)
  1353 
  1354 lemma negligible_iff_measure: "negligible S \<longleftrightarrow> S \<in> lmeasurable \<and> measure lebesgue S = 0"
  1355   by (fastforce simp: negligible_iff_measure0 negligible_imp_measurable dest: negligible_imp_measure0)
  1356 
  1357 lemma negligible_outer:
  1358   "negligible S \<longleftrightarrow> (\<forall>e>0. \<exists>T. S \<subseteq> T \<and> T \<in> lmeasurable \<and> measure lebesgue T < e)" (is "_ = ?rhs")
  1359 proof
  1360   assume "negligible S" then show ?rhs
  1361     by (metis negligible_iff_measure order_refl)
  1362 next
  1363   assume ?rhs then show "negligible S"
  1364   by (meson completion.null_sets_outer negligible_iff_null_sets)
  1365 qed
  1366 
  1367 lemma negligible_outer_le:
  1368      "negligible S \<longleftrightarrow> (\<forall>e>0. \<exists>T. S \<subseteq> T \<and> T \<in> lmeasurable \<and> measure lebesgue T \<le> e)" (is "_ = ?rhs")
  1369 proof
  1370   assume "negligible S" then show ?rhs
  1371     by (metis dual_order.strict_implies_order negligible_imp_measurable negligible_imp_measure0 order_refl)
  1372 next
  1373   assume ?rhs then show "negligible S"
  1374     by (metis le_less_trans negligible_outer field_lbound_gt_zero)
  1375 qed
  1376 
  1377 lemma negligible_UNIV: "negligible S \<longleftrightarrow> (indicat_real S has_integral 0) UNIV" (is "_=?rhs")
  1378 proof
  1379   assume ?rhs
  1380   then show "negligible S"
  1381     apply (auto simp: negligible_def has_integral_iff integrable_on_indicator)
  1382     by (metis negligible integral_unique lmeasure_integral_UNIV negligible_iff_measure0)
  1383 qed (simp add: negligible)
  1384 
  1385 lemma sets_negligible_symdiff:
  1386    "\<lbrakk>S \<in> sets lebesgue; negligible((S - T) \<union> (T - S))\<rbrakk> \<Longrightarrow> T \<in> sets lebesgue"
  1387   by (metis Diff_Diff_Int Int_Diff_Un inf_commute negligible_Un_eq negligible_imp_sets sets.Diff sets.Un)
  1388 
  1389 lemma lmeasurable_negligible_symdiff:
  1390    "\<lbrakk>S \<in> lmeasurable; negligible((S - T) \<union> (T - S))\<rbrakk> \<Longrightarrow> T \<in> lmeasurable"
  1391   using integrable_spike_set_eq lmeasurable_iff_integrable_on by blast
  1392 
  1393 
  1394 lemma measure_Un3_negligible:
  1395   assumes meas: "S \<in> lmeasurable" "T \<in> lmeasurable" "U \<in> lmeasurable"
  1396   and neg: "negligible(S \<inter> T)" "negligible(S \<inter> U)" "negligible(T \<inter> U)" and V: "S \<union> T \<union> U = V"
  1397 shows "measure lebesgue V = measure lebesgue S + measure lebesgue T + measure lebesgue U"
  1398 proof -
  1399   have [simp]: "measure lebesgue (S \<inter> T) = 0"
  1400     using neg(1) negligible_imp_measure0 by blast
  1401   have [simp]: "measure lebesgue (S \<inter> U \<union> T \<inter> U) = 0"
  1402     using neg(2) neg(3) negligible_Un negligible_imp_measure0 by blast
  1403   have "measure lebesgue V = measure lebesgue (S \<union> T \<union> U)"
  1404     using V by simp
  1405   also have "\<dots> = measure lebesgue S + measure lebesgue T + measure lebesgue U"
  1406     by (simp add: measure_Un3 meas fmeasurable.Un Int_Un_distrib2)
  1407   finally show ?thesis .
  1408 qed
  1409 
  1410 lemma measure_translate_add:
  1411   assumes meas: "S \<in> lmeasurable" "T \<in> lmeasurable"
  1412     and U: "S \<union> ((+)a ` T) = U" and neg: "negligible(S \<inter> ((+)a ` T))"
  1413   shows "measure lebesgue S + measure lebesgue T = measure lebesgue U"
  1414 proof -
  1415   have [simp]: "measure lebesgue (S \<inter> (+) a ` T) = 0"
  1416     using neg negligible_imp_measure0 by blast
  1417   have "measure lebesgue (S \<union> ((+)a ` T)) = measure lebesgue S + measure lebesgue T"
  1418     by (simp add: measure_Un3 meas measurable_translation measure_translation fmeasurable.Un)
  1419   then show ?thesis
  1420     using U by auto
  1421 qed
  1422 
  1423 lemma measure_negligible_symdiff:
  1424   assumes S: "S \<in> lmeasurable"
  1425     and neg: "negligible (S - T \<union> (T - S))"
  1426   shows "measure lebesgue T = measure lebesgue S"
  1427 proof -
  1428   have "measure lebesgue (S - T) = 0"
  1429     using neg negligible_Un_eq negligible_imp_measure0 by blast
  1430   then show ?thesis
  1431     by (metis S Un_commute add.right_neutral lmeasurable_negligible_symdiff measure_Un2 neg negligible_Un_eq negligible_imp_measure0)
  1432 qed
  1433 
  1434 lemma measure_closure:
  1435   assumes "bounded S" and neg: "negligible (frontier S)"
  1436   shows "measure lebesgue (closure S) = measure lebesgue S"
  1437 proof -
  1438   have "measure lebesgue (frontier S) = 0"
  1439     by (metis neg negligible_imp_measure0)
  1440   then show ?thesis
  1441     by (metis assms lmeasurable_iff_integrable_on eq_iff_diff_eq_0 has_integral_interior integrable_on_def integral_unique lmeasurable_interior lmeasure_integral measure_frontier)
  1442 qed
  1443 
  1444 lemma measure_interior:
  1445    "\<lbrakk>bounded S; negligible(frontier S)\<rbrakk> \<Longrightarrow> measure lebesgue (interior S) = measure lebesgue S"
  1446   using measure_closure measure_frontier negligible_imp_measure0 by fastforce
  1447 
  1448 lemma measurable_Jordan:
  1449   assumes "bounded S" and neg: "negligible (frontier S)"
  1450   shows "S \<in> lmeasurable"
  1451 proof -
  1452   have "closure S \<in> lmeasurable"
  1453     by (metis lmeasurable_closure \<open>bounded S\<close>)
  1454   moreover have "interior S \<in> lmeasurable"
  1455     by (simp add: lmeasurable_interior \<open>bounded S\<close>)
  1456   moreover have "interior S \<subseteq> S"
  1457     by (simp add: interior_subset)
  1458   ultimately show ?thesis
  1459     using assms by (metis (full_types) closure_subset completion.complete_sets_sandwich_fmeasurable measure_closure measure_interior)
  1460 qed
  1461 
  1462 lemma measurable_convex: "\<lbrakk>convex S; bounded S\<rbrakk> \<Longrightarrow> S \<in> lmeasurable"
  1463   by (simp add: measurable_Jordan negligible_convex_frontier)
  1464 
  1465 subsection\<open>Negligibility of image under non-injective linear map\<close>
  1466 
  1467 lemma negligible_Union_nat:
  1468   assumes "\<And>n::nat. negligible(S n)"
  1469   shows "negligible(\<Union>n. S n)"
  1470 proof -
  1471   have "negligible (\<Union>m\<le>k. S m)" for k
  1472     using assms by blast
  1473   then have 0:  "integral UNIV (indicat_real (\<Union>m\<le>k. S m)) = 0"
  1474     and 1: "(indicat_real (\<Union>m\<le>k. S m)) integrable_on UNIV" for k
  1475     by (auto simp: negligible has_integral_iff)
  1476   have 2: "\<And>k x. indicat_real (\<Union>m\<le>k. S m) x \<le> (indicat_real (\<Union>m\<le>Suc k. S m) x)"
  1477     by (simp add: indicator_def)
  1478   have 3: "\<And>x. (\<lambda>k. indicat_real (\<Union>m\<le>k. S m) x) \<longlonglongrightarrow> (indicat_real (\<Union>n. S n) x)"
  1479     by (force simp: indicator_def eventually_sequentially intro: Lim_eventually)
  1480   have 4: "bounded (range (\<lambda>k. integral UNIV (indicat_real (\<Union>m\<le>k. S m))))"
  1481     by (simp add: 0 image_def)
  1482   have *: "indicat_real (\<Union>n. S n) integrable_on UNIV \<and>
  1483         (\<lambda>k. integral UNIV (indicat_real (\<Union>m\<le>k. S m))) \<longlonglongrightarrow> (integral UNIV (indicat_real (\<Union>n. S n)))"
  1484     by (intro monotone_convergence_increasing 1 2 3 4)
  1485   then have "integral UNIV (indicat_real (\<Union>n. S n)) = (0::real)"
  1486     using LIMSEQ_unique by (auto simp: 0)
  1487   then show ?thesis
  1488     using * by (simp add: negligible_UNIV has_integral_iff)
  1489 qed
  1490 
  1491 
  1492 lemma negligible_linear_singular_image:
  1493   fixes f :: "'n::euclidean_space \<Rightarrow> 'n"
  1494   assumes "linear f" "\<not> inj f"
  1495   shows "negligible (f ` S)"
  1496 proof -
  1497   obtain a where "a \<noteq> 0" "\<And>S. f ` S \<subseteq> {x. a \<bullet> x = 0}"
  1498     using assms linear_singular_image_hyperplane by blast
  1499   then show "negligible (f ` S)"
  1500     by (metis negligible_hyperplane negligible_subset)
  1501 qed
  1502 
  1503 lemma measure_negligible_finite_Union:
  1504   assumes "finite \<F>"
  1505     and meas: "\<And>S. S \<in> \<F> \<Longrightarrow> S \<in> lmeasurable"
  1506     and djointish: "pairwise (\<lambda>S T. negligible (S \<inter> T)) \<F>"
  1507   shows "measure lebesgue (\<Union>\<F>) = (\<Sum>S\<in>\<F>. measure lebesgue S)"
  1508   using assms
  1509 proof (induction)
  1510   case empty
  1511   then show ?case
  1512     by auto
  1513 next
  1514   case (insert S \<F>)
  1515   then have "S \<in> lmeasurable" "\<Union>\<F> \<in> lmeasurable" "pairwise (\<lambda>S T. negligible (S \<inter> T)) \<F>"
  1516     by (simp_all add: fmeasurable.finite_Union insert.hyps(1) insert.prems(1) pairwise_insert subsetI)
  1517   then show ?case
  1518   proof (simp add: measure_Un3 insert)
  1519     have *: "\<And>T. T \<in> (\<inter>) S ` \<F> \<Longrightarrow> negligible T"
  1520       using insert by (force simp: pairwise_def)
  1521     have "negligible(S \<inter> \<Union>\<F>)"
  1522       unfolding Int_Union
  1523       by (rule negligible_Union) (simp_all add: * insert.hyps(1))
  1524     then show "measure lebesgue (S \<inter> \<Union>\<F>) = 0"
  1525       using negligible_imp_measure0 by blast
  1526   qed
  1527 qed
  1528 
  1529 lemma measure_negligible_finite_Union_image:
  1530   assumes "finite S"
  1531     and meas: "\<And>x. x \<in> S \<Longrightarrow> f x \<in> lmeasurable"
  1532     and djointish: "pairwise (\<lambda>x y. negligible (f x \<inter> f y)) S"
  1533   shows "measure lebesgue (\<Union>(f ` S)) = (\<Sum>x\<in>S. measure lebesgue (f x))"
  1534 proof -
  1535   have "measure lebesgue (\<Union>(f ` S)) = sum (measure lebesgue) (f ` S)"
  1536     using assms by (auto simp: pairwise_mono pairwise_image intro: measure_negligible_finite_Union)
  1537   also have "\<dots> = sum (measure lebesgue \<circ> f) S"
  1538     using djointish [unfolded pairwise_def] by (metis inf.idem negligible_imp_measure0 sum.reindex_nontrivial [OF \<open>finite S\<close>])
  1539   also have "\<dots> = (\<Sum>x\<in>S. measure lebesgue (f x))"
  1540     by simp
  1541   finally show ?thesis .
  1542 qed
  1543 
  1544 subsection \<open>Negligibility of a Lipschitz image of a negligible set\<close>
  1545 
  1546 text\<open>The bound will be eliminated by a sort of onion argument\<close>
  1547 lemma locally_Lipschitz_negl_bounded:
  1548   fixes f :: "'M::euclidean_space \<Rightarrow> 'N::euclidean_space"
  1549   assumes MleN: "DIM('M) \<le> DIM('N)" "0 < B" "bounded S" "negligible S"
  1550       and lips: "\<And>x. x \<in> S
  1551                       \<Longrightarrow> \<exists>T. open T \<and> x \<in> T \<and>
  1552                               (\<forall>y \<in> S \<inter> T. norm(f y - f x) \<le> B * norm(y - x))"
  1553   shows "negligible (f ` S)"
  1554   unfolding negligible_iff_null_sets
  1555 proof (clarsimp simp: completion.null_sets_outer)
  1556   fix e::real
  1557   assume "0 < e"
  1558   have "S \<in> lmeasurable"
  1559     using \<open>negligible S\<close> by (simp add: negligible_iff_null_sets fmeasurableI_null_sets)
  1560   then have "S \<in> sets lebesgue"
  1561     by blast
  1562   have e22: "0 < e/2 / (2 * B * real DIM('M)) ^ DIM('N)"
  1563     using \<open>0 < e\<close> \<open>0 < B\<close> by (simp add: divide_simps)
  1564   obtain T where "open T" "S \<subseteq> T" "(T - S) \<in> lmeasurable" 
  1565                  "measure lebesgue (T - S) < e/2 / (2 * B * DIM('M)) ^ DIM('N)"
  1566     by (rule lmeasurable_outer_open [OF \<open>S \<in> sets lebesgue\<close> e22])
  1567   then have T: "measure lebesgue T \<le> e/2 / (2 * B * DIM('M)) ^ DIM('N)"
  1568     using \<open>negligible S\<close> by (simp add: measure_Diff_null_set negligible_iff_null_sets)
  1569   have "\<exists>r. 0 < r \<and> r \<le> 1/2 \<and>
  1570             (x \<in> S \<longrightarrow> (\<forall>y. norm(y - x) < r
  1571                        \<longrightarrow> y \<in> T \<and> (y \<in> S \<longrightarrow> norm(f y - f x) \<le> B * norm(y - x))))"
  1572         for x
  1573   proof (cases "x \<in> S")
  1574     case True
  1575     obtain U where "open U" "x \<in> U" and U: "\<And>y. y \<in> S \<inter> U \<Longrightarrow> norm(f y - f x) \<le> B * norm(y - x)"
  1576       using lips [OF \<open>x \<in> S\<close>] by auto
  1577     have "x \<in> T \<inter> U"
  1578       using \<open>S \<subseteq> T\<close> \<open>x \<in> U\<close> \<open>x \<in> S\<close> by auto
  1579     then obtain \<epsilon> where "0 < \<epsilon>" "ball x \<epsilon> \<subseteq> T \<inter> U"
  1580       by (metis \<open>open T\<close> \<open>open U\<close> openE open_Int)
  1581     then show ?thesis
  1582       apply (rule_tac x="min (1/2) \<epsilon>" in exI)
  1583       apply (simp del: divide_const_simps)
  1584       apply (intro allI impI conjI)
  1585        apply (metis dist_commute dist_norm mem_ball subsetCE)
  1586       by (metis Int_iff subsetCE U dist_norm mem_ball norm_minus_commute)
  1587   next
  1588     case False
  1589     then show ?thesis
  1590       by (rule_tac x="1/4" in exI) auto
  1591   qed
  1592   then obtain R where R12: "\<And>x. 0 < R x \<and> R x \<le> 1/2"
  1593                 and RT: "\<And>x y. \<lbrakk>x \<in> S; norm(y - x) < R x\<rbrakk> \<Longrightarrow> y \<in> T"
  1594                 and RB: "\<And>x y. \<lbrakk>x \<in> S; y \<in> S; norm(y - x) < R x\<rbrakk> \<Longrightarrow> norm(f y - f x) \<le> B * norm(y - x)"
  1595     by metis+
  1596   then have gaugeR: "gauge (\<lambda>x. ball x (R x))"
  1597     by (simp add: gauge_def)
  1598   obtain c where c: "S \<subseteq> cbox (-c *\<^sub>R One) (c *\<^sub>R One)" "box (-c *\<^sub>R One:: 'M) (c *\<^sub>R One) \<noteq> {}"
  1599   proof -
  1600     obtain B where B: "\<And>x. x \<in> S \<Longrightarrow> norm x \<le> B"
  1601       using \<open>bounded S\<close> bounded_iff by blast
  1602     show ?thesis
  1603       apply (rule_tac c = "abs B + 1" in that)
  1604       using norm_bound_Basis_le Basis_le_norm
  1605        apply (fastforce simp: box_eq_empty mem_box dest!: B intro: order_trans)+
  1606       done
  1607   qed
  1608   obtain \<D> where "countable \<D>"
  1609      and Dsub: "\<Union>\<D> \<subseteq> cbox (-c *\<^sub>R One) (c *\<^sub>R One)"
  1610      and cbox: "\<And>K. K \<in> \<D> \<Longrightarrow> interior K \<noteq> {} \<and> (\<exists>c d. K = cbox c d)"
  1611      and pw:   "pairwise (\<lambda>A B. interior A \<inter> interior B = {}) \<D>"
  1612      and Ksub: "\<And>K. K \<in> \<D> \<Longrightarrow> \<exists>x \<in> S \<inter> K. K \<subseteq> (\<lambda>x. ball x (R x)) x"
  1613      and exN:  "\<And>u v. cbox u v \<in> \<D> \<Longrightarrow> \<exists>n. \<forall>i \<in> Basis. v \<bullet> i - u \<bullet> i = (2*c) / 2^n"
  1614      and "S \<subseteq> \<Union>\<D>"
  1615     using covering_lemma [OF c gaugeR]  by force
  1616   have "\<exists>u v z. K = cbox u v \<and> box u v \<noteq> {} \<and> z \<in> S \<and> z \<in> cbox u v \<and>
  1617                 cbox u v \<subseteq> ball z (R z)" if "K \<in> \<D>" for K
  1618   proof -
  1619     obtain u v where "K = cbox u v"
  1620       using \<open>K \<in> \<D>\<close> cbox by blast
  1621     with that show ?thesis
  1622       apply (rule_tac x=u in exI)
  1623       apply (rule_tac x=v in exI)
  1624       apply (metis Int_iff interior_cbox cbox Ksub)
  1625       done
  1626   qed
  1627   then obtain uf vf zf
  1628     where uvz: "\<And>K. K \<in> \<D> \<Longrightarrow>
  1629                 K = cbox (uf K) (vf K) \<and> box (uf K) (vf K) \<noteq> {} \<and> zf K \<in> S \<and>
  1630                 zf K \<in> cbox (uf K) (vf K) \<and> cbox (uf K) (vf K) \<subseteq> ball (zf K) (R (zf K))"
  1631     by metis
  1632   define prj1 where "prj1 \<equiv> \<lambda>x::'M. x \<bullet> (SOME i. i \<in> Basis)"
  1633   define fbx where "fbx \<equiv> \<lambda>D. cbox (f(zf D) - (B * DIM('M) * (prj1(vf D - uf D))) *\<^sub>R One::'N)
  1634                                     (f(zf D) + (B * DIM('M) * prj1(vf D - uf D)) *\<^sub>R One)"
  1635   have vu_pos: "0 < prj1 (vf X - uf X)" if "X \<in> \<D>" for X
  1636     using uvz [OF that] by (simp add: prj1_def box_ne_empty SOME_Basis inner_diff_left)
  1637   have prj1_idem: "prj1 (vf X - uf X) = (vf X - uf X) \<bullet> i" if  "X \<in> \<D>" "i \<in> Basis" for X i
  1638   proof -
  1639     have "cbox (uf X) (vf X) \<in> \<D>"
  1640       using uvz \<open>X \<in> \<D>\<close> by auto
  1641     with exN obtain n where "\<And>i. i \<in> Basis \<Longrightarrow> vf X \<bullet> i - uf X \<bullet> i = (2*c) / 2^n"
  1642       by blast
  1643     then show ?thesis
  1644       by (simp add: \<open>i \<in> Basis\<close> SOME_Basis inner_diff prj1_def)
  1645   qed
  1646   have countbl: "countable (fbx ` \<D>)"
  1647     using \<open>countable \<D>\<close> by blast
  1648   have "(\<Sum>k\<in>fbx`\<D>'. measure lebesgue k) \<le> e/2" if "\<D>' \<subseteq> \<D>" "finite \<D>'" for \<D>'
  1649   proof -
  1650     have BM_ge0: "0 \<le> B * (DIM('M) * prj1 (vf X - uf X))" if "X \<in> \<D>'" for X
  1651       using \<open>0 < B\<close> \<open>\<D>' \<subseteq> \<D>\<close> that vu_pos by fastforce
  1652     have "{} \<notin> \<D>'"
  1653       using cbox \<open>\<D>' \<subseteq> \<D>\<close> interior_empty by blast
  1654     have "(\<Sum>k\<in>fbx`\<D>'. measure lebesgue k) \<le> sum (measure lebesgue o fbx) \<D>'"
  1655       by (rule sum_image_le [OF \<open>finite \<D>'\<close>]) (force simp: fbx_def)
  1656     also have "\<dots> \<le> (\<Sum>X\<in>\<D>'. (2 * B * DIM('M)) ^ DIM('N) * measure lebesgue X)"
  1657     proof (rule sum_mono)
  1658       fix X assume "X \<in> \<D>'"
  1659       then have "X \<in> \<D>" using \<open>\<D>' \<subseteq> \<D>\<close> by blast
  1660       then have ufvf: "cbox (uf X) (vf X) = X"
  1661         using uvz by blast
  1662       have "prj1 (vf X - uf X) ^ DIM('M) = (\<Prod>i::'M \<in> Basis. prj1 (vf X - uf X))"
  1663         by (rule prod_constant [symmetric])
  1664       also have "\<dots> = (\<Prod>i\<in>Basis. vf X \<bullet> i - uf X \<bullet> i)"
  1665         apply (rule prod.cong [OF refl])
  1666         by (simp add: \<open>X \<in> \<D>\<close> inner_diff_left prj1_idem)
  1667       finally have prj1_eq: "prj1 (vf X - uf X) ^ DIM('M) = (\<Prod>i\<in>Basis. vf X \<bullet> i - uf X \<bullet> i)" .
  1668       have "uf X \<in> cbox (uf X) (vf X)" "vf X \<in> cbox (uf X) (vf X)"
  1669         using uvz [OF \<open>X \<in> \<D>\<close>] by (force simp: mem_box)+
  1670       moreover have "cbox (uf X) (vf X) \<subseteq> ball (zf X) (1/2)"
  1671         by (meson R12 order_trans subset_ball uvz [OF \<open>X \<in> \<D>\<close>])
  1672       ultimately have "uf X \<in> ball (zf X) (1/2)"  "vf X \<in> ball (zf X) (1/2)"
  1673         by auto
  1674       then have "dist (vf X) (uf X) \<le> 1"
  1675         unfolding mem_ball
  1676         by (metis dist_commute dist_triangle_half_l dual_order.order_iff_strict)
  1677       then have 1: "prj1 (vf X - uf X) \<le> 1"
  1678         unfolding prj1_def dist_norm using Basis_le_norm SOME_Basis order_trans by fastforce
  1679       have 0: "0 \<le> prj1 (vf X - uf X)"
  1680         using \<open>X \<in> \<D>\<close> prj1_def vu_pos by fastforce
  1681       have "(measure lebesgue \<circ> fbx) X \<le> (2 * B * DIM('M)) ^ DIM('N) * content (cbox (uf X) (vf X))"
  1682         apply (simp add: fbx_def content_cbox_cases algebra_simps BM_ge0 \<open>X \<in> \<D>'\<close> prod_constant)
  1683         apply (simp add: power_mult_distrib \<open>0 < B\<close> prj1_eq [symmetric])
  1684         using MleN 0 1 uvz \<open>X \<in> \<D>\<close>
  1685         apply (fastforce simp add: box_ne_empty power_decreasing)
  1686         done
  1687       also have "\<dots> = (2 * B * DIM('M)) ^ DIM('N) * measure lebesgue X"
  1688         by (subst (3) ufvf[symmetric]) simp
  1689       finally show "(measure lebesgue \<circ> fbx) X \<le> (2 * B * DIM('M)) ^ DIM('N) * measure lebesgue X" .
  1690     qed
  1691     also have "\<dots> = (2 * B * DIM('M)) ^ DIM('N) * sum (measure lebesgue) \<D>'"
  1692       by (simp add: sum_distrib_left)
  1693     also have "\<dots> \<le> e/2"
  1694     proof -
  1695       have div: "\<D>' division_of \<Union>\<D>'"
  1696         apply (auto simp: \<open>finite \<D>'\<close> \<open>{} \<notin> \<D>'\<close> division_of_def)
  1697         using cbox that apply blast
  1698         using pairwise_subset [OF pw \<open>\<D>' \<subseteq> \<D>\<close>] unfolding pairwise_def apply force+
  1699         done
  1700       have le_meaT: "measure lebesgue (\<Union>\<D>') \<le> measure lebesgue T"
  1701       proof (rule measure_mono_fmeasurable)
  1702         show "(\<Union>\<D>') \<in> sets lebesgue"
  1703           using div lmeasurable_division by auto
  1704         have "\<Union>\<D>' \<subseteq> \<Union>\<D>"
  1705           using \<open>\<D>' \<subseteq> \<D>\<close> by blast
  1706         also have "... \<subseteq> T"
  1707         proof (clarify)
  1708           fix x D
  1709           assume "x \<in> D" "D \<in> \<D>"
  1710           show "x \<in> T"
  1711             using Ksub [OF \<open>D \<in> \<D>\<close>]
  1712             by (metis \<open>x \<in> D\<close> Int_iff dist_norm mem_ball norm_minus_commute subsetD RT)
  1713         qed
  1714         finally show "\<Union>\<D>' \<subseteq> T" .
  1715         show "T \<in> lmeasurable"
  1716           using \<open>S \<in> lmeasurable\<close> \<open>S \<subseteq> T\<close> \<open>T - S \<in> lmeasurable\<close> fmeasurable_Diff_D by blast
  1717       qed 
  1718       have "sum (measure lebesgue) \<D>' = sum content \<D>'"
  1719         using  \<open>\<D>' \<subseteq> \<D>\<close> cbox by (force intro: sum.cong)
  1720       then have "(2 * B * DIM('M)) ^ DIM('N) * sum (measure lebesgue) \<D>' =
  1721                  (2 * B * real DIM('M)) ^ DIM('N) * measure lebesgue (\<Union>\<D>')"
  1722         using content_division [OF div] by auto
  1723       also have "\<dots> \<le> (2 * B * real DIM('M)) ^ DIM('N) * measure lebesgue T"
  1724         apply (rule mult_left_mono [OF le_meaT])
  1725         using \<open>0 < B\<close>
  1726         apply (simp add: algebra_simps)
  1727         done
  1728       also have "\<dots> \<le> e/2"
  1729         using T \<open>0 < B\<close> by (simp add: field_simps)
  1730       finally show ?thesis .
  1731     qed
  1732     finally show ?thesis .
  1733   qed
  1734   then have e2: "sum (measure lebesgue) \<G> \<le> e/2" if "\<G> \<subseteq> fbx ` \<D>" "finite \<G>" for \<G>
  1735     by (metis finite_subset_image that)
  1736   show "\<exists>W\<in>lmeasurable. f ` S \<subseteq> W \<and> measure lebesgue W < e"
  1737   proof (intro bexI conjI)
  1738     have "\<exists>X\<in>\<D>. f y \<in> fbx X" if "y \<in> S" for y
  1739     proof -
  1740       obtain X where "y \<in> X" "X \<in> \<D>"
  1741         using \<open>S \<subseteq> \<Union>\<D>\<close> \<open>y \<in> S\<close> by auto
  1742       then have y: "y \<in> ball(zf X) (R(zf X))"
  1743         using uvz by fastforce
  1744       have conj_le_eq: "z - b \<le> y \<and> y \<le> z + b \<longleftrightarrow> abs(y - z) \<le> b" for z y b::real
  1745         by auto
  1746       have yin: "y \<in> cbox (uf X) (vf X)" and zin: "(zf X) \<in> cbox (uf X) (vf X)"
  1747         using uvz \<open>X \<in> \<D>\<close> \<open>y \<in> X\<close> by auto
  1748       have "norm (y - zf X) \<le> (\<Sum>i\<in>Basis. \<bar>(y - zf X) \<bullet> i\<bar>)"
  1749         by (rule norm_le_l1)
  1750       also have "\<dots> \<le> real DIM('M) * prj1 (vf X - uf X)"
  1751       proof (rule sum_bounded_above)
  1752         fix j::'M assume j: "j \<in> Basis"
  1753         show "\<bar>(y - zf X) \<bullet> j\<bar> \<le> prj1 (vf X - uf X)"
  1754           using yin zin j
  1755           by (fastforce simp add: mem_box prj1_idem [OF \<open>X \<in> \<D>\<close> j] inner_diff_left)
  1756       qed
  1757       finally have nole: "norm (y - zf X) \<le> DIM('M) * prj1 (vf X - uf X)"
  1758         by simp
  1759       have fle: "\<bar>f y \<bullet> i - f(zf X) \<bullet> i\<bar> \<le> B * DIM('M) * prj1 (vf X - uf X)" if "i \<in> Basis" for i
  1760       proof -
  1761         have "\<bar>f y \<bullet> i - f (zf X) \<bullet> i\<bar> = \<bar>(f y - f (zf X)) \<bullet> i\<bar>"
  1762           by (simp add: algebra_simps)
  1763         also have "\<dots> \<le> norm (f y - f (zf X))"
  1764           by (simp add: Basis_le_norm that)
  1765         also have "\<dots> \<le> B * norm(y - zf X)"
  1766           by (metis uvz RB \<open>X \<in> \<D>\<close> dist_commute dist_norm mem_ball \<open>y \<in> S\<close> y)
  1767         also have "\<dots> \<le> B * real DIM('M) * prj1 (vf X - uf X)"
  1768           using \<open>0 < B\<close> by (simp add: nole)
  1769         finally show ?thesis .
  1770       qed
  1771       show ?thesis
  1772         by (rule_tac x=X in bexI)
  1773            (auto simp: fbx_def prj1_idem mem_box conj_le_eq inner_add inner_diff fle \<open>X \<in> \<D>\<close>)
  1774     qed
  1775     then show "f ` S \<subseteq> (\<Union>D\<in>\<D>. fbx D)" by auto
  1776   next
  1777     have 1: "\<And>D. D \<in> \<D> \<Longrightarrow> fbx D \<in> lmeasurable"
  1778       by (auto simp: fbx_def)
  1779     have 2: "I' \<subseteq> \<D> \<Longrightarrow> finite I' \<Longrightarrow> measure lebesgue (\<Union>D\<in>I'. fbx D) \<le> e/2" for I'
  1780       by (rule order_trans[OF measure_Union_le e2]) (auto simp: fbx_def)
  1781     show "(\<Union>D\<in>\<D>. fbx D) \<in> lmeasurable"
  1782       by (intro fmeasurable_UN_bound[OF \<open>countable \<D>\<close> 1 2])
  1783     have "measure lebesgue (\<Union>D\<in>\<D>. fbx D) \<le> e/2"
  1784       by (intro measure_UN_bound[OF \<open>countable \<D>\<close> 1 2])
  1785     then show "measure lebesgue (\<Union>D\<in>\<D>. fbx D) < e"
  1786       using \<open>0 < e\<close> by linarith
  1787   qed
  1788 qed
  1789 
  1790 proposition negligible_locally_Lipschitz_image:
  1791   fixes f :: "'M::euclidean_space \<Rightarrow> 'N::euclidean_space"
  1792   assumes MleN: "DIM('M) \<le> DIM('N)" "negligible S"
  1793       and lips: "\<And>x. x \<in> S
  1794                       \<Longrightarrow> \<exists>T B. open T \<and> x \<in> T \<and>
  1795                               (\<forall>y \<in> S \<inter> T. norm(f y - f x) \<le> B * norm(y - x))"
  1796     shows "negligible (f ` S)"
  1797 proof -
  1798   let ?S = "\<lambda>n. ({x \<in> S. norm x \<le> n \<and>
  1799                           (\<exists>T. open T \<and> x \<in> T \<and>
  1800                                (\<forall>y\<in>S \<inter> T. norm (f y - f x) \<le> (real n + 1) * norm (y - x)))})"
  1801   have negfn: "f ` ?S n \<in> null_sets lebesgue" for n::nat
  1802     unfolding negligible_iff_null_sets[symmetric]
  1803     apply (rule_tac B = "real n + 1" in locally_Lipschitz_negl_bounded)
  1804     by (auto simp: MleN bounded_iff intro: negligible_subset [OF \<open>negligible S\<close>])
  1805   have "S = (\<Union>n. ?S n)"
  1806   proof (intro set_eqI iffI)
  1807     fix x assume "x \<in> S"
  1808     with lips obtain T B where T: "open T" "x \<in> T"
  1809                            and B: "\<And>y. y \<in> S \<inter> T \<Longrightarrow> norm(f y - f x) \<le> B * norm(y - x)"
  1810       by metis+
  1811     have no: "norm (f y - f x) \<le> (nat \<lceil>max B (norm x)\<rceil> + 1) * norm (y - x)" if "y \<in> S \<inter> T" for y
  1812     proof -
  1813       have "B * norm(y - x) \<le> (nat \<lceil>max B (norm x)\<rceil> + 1) * norm (y - x)"
  1814         by (meson max.cobounded1 mult_right_mono nat_ceiling_le_eq nat_le_iff_add norm_ge_zero order_trans)
  1815       then show ?thesis
  1816         using B order_trans that by blast
  1817     qed
  1818     have "x \<in> ?S (nat (ceiling (max B (norm x))))"
  1819       apply (simp add: \<open>x \<in> S \<close>, rule)
  1820       using real_nat_ceiling_ge max.bounded_iff apply blast
  1821       using T no
  1822       apply (force simp: algebra_simps)
  1823       done
  1824     then show "x \<in> (\<Union>n. ?S n)" by force
  1825   qed auto
  1826   then show ?thesis
  1827     by (rule ssubst) (auto simp: image_Union negligible_iff_null_sets intro: negfn)
  1828 qed
  1829 
  1830 corollary negligible_differentiable_image_negligible:
  1831   fixes f :: "'M::euclidean_space \<Rightarrow> 'N::euclidean_space"
  1832   assumes MleN: "DIM('M) \<le> DIM('N)" "negligible S"
  1833       and diff_f: "f differentiable_on S"
  1834     shows "negligible (f ` S)"
  1835 proof -
  1836   have "\<exists>T B. open T \<and> x \<in> T \<and> (\<forall>y \<in> S \<inter> T. norm(f y - f x) \<le> B * norm(y - x))"
  1837         if "x \<in> S" for x
  1838   proof -
  1839     obtain f' where "linear f'"
  1840       and f': "\<And>e. e>0 \<Longrightarrow>
  1841                   \<exists>d>0. \<forall>y\<in>S. norm (y - x) < d \<longrightarrow>
  1842                               norm (f y - f x - f' (y - x)) \<le> e * norm (y - x)"
  1843       using diff_f \<open>x \<in> S\<close>
  1844       by (auto simp: linear_linear differentiable_on_def differentiable_def has_derivative_within_alt)
  1845     obtain B where "B > 0" and B: "\<forall>x. norm (f' x) \<le> B * norm x"
  1846       using linear_bounded_pos \<open>linear f'\<close> by blast
  1847     obtain d where "d>0"
  1848               and d: "\<And>y. \<lbrakk>y \<in> S; norm (y - x) < d\<rbrakk> \<Longrightarrow>
  1849                           norm (f y - f x - f' (y - x)) \<le> norm (y - x)"
  1850       using f' [of 1] by (force simp:)
  1851     have *: "norm (f y - f x) \<le> (B + 1) * norm (y - x)"
  1852               if "y \<in> S" "norm (y - x) < d" for y
  1853     proof -
  1854       have "norm (f y - f x) -B *  norm (y - x) \<le> norm (f y - f x) - norm (f' (y - x))"
  1855         by (simp add: B)
  1856       also have "\<dots> \<le> norm (f y - f x - f' (y - x))"
  1857         by (rule norm_triangle_ineq2)
  1858       also have "... \<le> norm (y - x)"
  1859         by (rule d [OF that])
  1860       finally show ?thesis
  1861         by (simp add: algebra_simps)
  1862     qed
  1863     show ?thesis
  1864       apply (rule_tac x="ball x d" in exI)
  1865       apply (rule_tac x="B+1" in exI)
  1866       using \<open>d>0\<close>
  1867       apply (auto simp: dist_norm norm_minus_commute intro!: *)
  1868       done
  1869   qed
  1870   with negligible_locally_Lipschitz_image assms show ?thesis by metis
  1871 qed
  1872 
  1873 corollary negligible_differentiable_image_lowdim:
  1874   fixes f :: "'M::euclidean_space \<Rightarrow> 'N::euclidean_space"
  1875   assumes MlessN: "DIM('M) < DIM('N)" and diff_f: "f differentiable_on S"
  1876     shows "negligible (f ` S)"
  1877 proof -
  1878   have "x \<le> DIM('M) \<Longrightarrow> x \<le> DIM('N)" for x
  1879     using MlessN by linarith
  1880   obtain lift :: "'M * real \<Rightarrow> 'N" and drop :: "'N \<Rightarrow> 'M * real" and j :: 'N
  1881     where "linear lift" "linear drop" and dropl [simp]: "\<And>z. drop (lift z) = z"
  1882       and "j \<in> Basis" and j: "\<And>x. lift(x,0) \<bullet> j = 0"
  1883     using lowerdim_embeddings [OF MlessN] by metis
  1884   have "negligible {x. x\<bullet>j = 0}"
  1885     by (metis \<open>j \<in> Basis\<close> negligible_standard_hyperplane)
  1886   then have neg0S: "negligible ((\<lambda>x. lift (x, 0)) ` S)"
  1887     apply (rule negligible_subset)
  1888     by (simp add: image_subsetI j)
  1889   have diff_f': "f \<circ> fst \<circ> drop differentiable_on (\<lambda>x. lift (x, 0)) ` S"
  1890     using diff_f
  1891     apply (clarsimp simp add: differentiable_on_def)
  1892     apply (intro differentiable_chain_within linear_imp_differentiable [OF \<open>linear drop\<close>]
  1893              linear_imp_differentiable [OF fst_linear])
  1894     apply (force simp: image_comp o_def)
  1895     done
  1896   have "f = (f o fst o drop o (\<lambda>x. lift (x, 0)))"
  1897     by (simp add: o_def)
  1898   then show ?thesis
  1899     apply (rule ssubst)
  1900     apply (subst image_comp [symmetric])
  1901     apply (metis negligible_differentiable_image_negligible order_refl diff_f' neg0S)
  1902     done
  1903 qed
  1904 
  1905 subsection\<open>Measurability of countable unions and intersections of various kinds.\<close>
  1906 
  1907 lemma
  1908   assumes S: "\<And>n. S n \<in> lmeasurable"
  1909     and leB: "\<And>n. measure lebesgue (S n) \<le> B"
  1910     and nest: "\<And>n. S n \<subseteq> S(Suc n)"
  1911   shows measurable_nested_Union: "(\<Union>n. S n) \<in> lmeasurable"
  1912   and measure_nested_Union:  "(\<lambda>n. measure lebesgue (S n)) \<longlonglongrightarrow> measure lebesgue (\<Union>n. S n)" (is ?Lim)
  1913 proof -
  1914   have 1: "\<And>n. (indicat_real (S n)) integrable_on UNIV"
  1915     using S measurable_integrable by blast
  1916   have 2: "\<And>n x::'a. indicat_real (S n) x \<le> (indicat_real (S (Suc n)) x)"
  1917     by (simp add: indicator_leI nest rev_subsetD)
  1918   have 3: "\<And>x. (\<lambda>n. indicat_real (S n) x) \<longlonglongrightarrow> (indicat_real (\<Union>(S ` UNIV)) x)"
  1919     apply (rule Lim_eventually)
  1920     apply (simp add: indicator_def)
  1921     by (metis eventually_sequentiallyI lift_Suc_mono_le nest subsetCE)
  1922   have 4: "bounded (range (\<lambda>n. integral UNIV (indicat_real (S n))))"
  1923     using leB by (auto simp: lmeasure_integral_UNIV [symmetric] S bounded_iff)
  1924   have "(\<Union>n. S n) \<in> lmeasurable \<and> ?Lim"
  1925     apply (simp add: lmeasure_integral_UNIV S cong: conj_cong)
  1926     apply (simp add: measurable_integrable)
  1927     apply (rule monotone_convergence_increasing [OF 1 2 3 4])
  1928     done
  1929   then show "(\<Union>n. S n) \<in> lmeasurable" "?Lim"
  1930     by auto
  1931 qed
  1932 
  1933 lemma
  1934   assumes S: "\<And>n. S n \<in> lmeasurable"
  1935     and djointish: "pairwise (\<lambda>m n. negligible (S m \<inter> S n)) UNIV"
  1936     and leB: "\<And>n. (\<Sum>k\<le>n. measure lebesgue (S k)) \<le> B"
  1937   shows measurable_countable_negligible_Union: "(\<Union>n. S n) \<in> lmeasurable"
  1938   and   measure_countable_negligible_Union:    "(\<lambda>n. (measure lebesgue (S n))) sums measure lebesgue (\<Union>n. S n)" (is ?Sums)
  1939 proof -
  1940   have 1: "\<Union> (S ` {..n}) \<in> lmeasurable" for n
  1941     using S by blast
  1942   have 2: "measure lebesgue (\<Union> (S ` {..n})) \<le> B" for n
  1943   proof -
  1944     have "measure lebesgue (\<Union> (S ` {..n})) \<le> (\<Sum>k\<le>n. measure lebesgue (S k))"
  1945       by (simp add: S fmeasurableD measure_UNION_le)
  1946     with leB show ?thesis
  1947       using order_trans by blast
  1948   qed
  1949   have 3: "\<And>n. \<Union> (S ` {..n}) \<subseteq> \<Union> (S ` {..Suc n})"
  1950     by (simp add: SUP_subset_mono)
  1951   have eqS: "(\<Union>n. S n) = (\<Union>n. \<Union> (S ` {..n}))"
  1952     using atLeastAtMost_iff by blast
  1953   also have "(\<Union>n. \<Union> (S ` {..n})) \<in> lmeasurable"
  1954     by (intro measurable_nested_Union [OF 1 2] 3)
  1955   finally show "(\<Union>n. S n) \<in> lmeasurable" .
  1956   have eqm: "(\<Sum>i\<le>n. measure lebesgue (S i)) = measure lebesgue (\<Union> (S ` {..n}))" for n
  1957     using assms by (simp add: measure_negligible_finite_Union_image pairwise_mono)
  1958   have "(\<lambda>n. (measure lebesgue (S n))) sums measure lebesgue (\<Union>n. \<Union> (S ` {..n}))"
  1959     by (simp add: sums_def' eqm atLeast0AtMost) (intro measure_nested_Union [OF 1 2] 3)
  1960   then show ?Sums
  1961     by (simp add: eqS)
  1962 qed
  1963 
  1964 lemma negligible_countable_Union [intro]:
  1965   assumes "countable \<F>" and meas: "\<And>S. S \<in> \<F> \<Longrightarrow> negligible S"
  1966   shows "negligible (\<Union>\<F>)"
  1967 proof (cases "\<F> = {}")
  1968   case False
  1969   then show ?thesis
  1970     by (metis from_nat_into range_from_nat_into assms negligible_Union_nat)
  1971 qed simp
  1972 
  1973 lemma
  1974   assumes S: "\<And>n. (S n) \<in> lmeasurable"
  1975     and djointish: "pairwise (\<lambda>m n. negligible (S m \<inter> S n)) UNIV"
  1976     and bo: "bounded (\<Union>n. S n)"
  1977   shows measurable_countable_negligible_Union_bounded: "(\<Union>n. S n) \<in> lmeasurable"
  1978   and   measure_countable_negligible_Union_bounded:    "(\<lambda>n. (measure lebesgue (S n))) sums measure lebesgue (\<Union>n. S n)" (is ?Sums)
  1979 proof -
  1980   obtain a b where ab: "(\<Union>n. S n) \<subseteq> cbox a b"
  1981     using bo bounded_subset_cbox_symmetric by metis
  1982   then have B: "(\<Sum>k\<le>n. measure lebesgue (S k)) \<le> measure lebesgue (cbox a b)" for n
  1983   proof -
  1984     have "(\<Sum>k\<le>n. measure lebesgue (S k)) = measure lebesgue (\<Union> (S ` {..n}))"
  1985       using measure_negligible_finite_Union_image [OF _ _ pairwise_subset] djointish
  1986       by (metis S finite_atMost subset_UNIV)
  1987     also have "\<dots> \<le> measure lebesgue (cbox a b)"
  1988       apply (rule measure_mono_fmeasurable)
  1989       using ab S by force+
  1990     finally show ?thesis .
  1991   qed
  1992   show "(\<Union>n. S n) \<in> lmeasurable"
  1993     by (rule measurable_countable_negligible_Union [OF S djointish B])
  1994   show ?Sums
  1995     by (rule measure_countable_negligible_Union [OF S djointish B])
  1996 qed
  1997 
  1998 lemma measure_countable_Union_approachable:
  1999   assumes "countable \<D>" "e > 0" and measD: "\<And>d. d \<in> \<D> \<Longrightarrow> d \<in> lmeasurable"
  2000       and B: "\<And>D'. \<lbrakk>D' \<subseteq> \<D>; finite D'\<rbrakk> \<Longrightarrow> measure lebesgue (\<Union>D') \<le> B"
  2001     obtains D' where "D' \<subseteq> \<D>" "finite D'" "measure lebesgue (\<Union>\<D>) - e < measure lebesgue (\<Union>D')"
  2002 proof (cases "\<D> = {}")
  2003   case True
  2004   then show ?thesis
  2005     by (simp add: \<open>e > 0\<close> that)
  2006 next
  2007   case False
  2008   let ?S = "\<lambda>n. \<Union>k \<le> n. from_nat_into \<D> k"
  2009   have "(\<lambda>n. measure lebesgue (?S n)) \<longlonglongrightarrow> measure lebesgue (\<Union>n. ?S n)"
  2010   proof (rule measure_nested_Union)
  2011     show "?S n \<in> lmeasurable" for n
  2012       by (simp add: False fmeasurable.finite_UN from_nat_into measD)
  2013     show "measure lebesgue (?S n) \<le> B" for n
  2014       by (metis (mono_tags, lifting) B False finite_atMost finite_imageI from_nat_into image_iff subsetI)
  2015     show "?S n \<subseteq> ?S (Suc n)" for n
  2016       by force
  2017   qed
  2018   then obtain N where N: "\<And>n. n \<ge> N \<Longrightarrow> dist (measure lebesgue (?S n)) (measure lebesgue (\<Union>n. ?S n)) < e"
  2019     using metric_LIMSEQ_D \<open>e > 0\<close> by blast
  2020   show ?thesis
  2021   proof
  2022     show "from_nat_into \<D> ` {..N} \<subseteq> \<D>"
  2023       by (auto simp: False from_nat_into)
  2024     have eq: "(\<Union>n. \<Union>k\<le>n. from_nat_into \<D> k) = (\<Union>\<D>)"
  2025       using \<open>countable \<D>\<close> False
  2026       by (auto intro: from_nat_into dest: from_nat_into_surj [OF \<open>countable \<D>\<close>])
  2027     show "measure lebesgue (\<Union>\<D>) - e < measure lebesgue (\<Union> (from_nat_into \<D> ` {..N}))"
  2028       using N [OF order_refl]
  2029       by (auto simp: eq algebra_simps dist_norm)
  2030   qed auto
  2031 qed
  2032 
  2033 
  2034 subsection\<open>Negligibility is a local property\<close>
  2035 
  2036 lemma locally_negligible_alt:
  2037     "negligible S \<longleftrightarrow> (\<forall>x \<in> S. \<exists>U. openin (subtopology euclidean S) U \<and> x \<in> U \<and> negligible U)"
  2038      (is "_ = ?rhs")
  2039 proof
  2040   assume "negligible S"
  2041   then show ?rhs
  2042     using openin_subtopology_self by blast
  2043 next
  2044   assume ?rhs
  2045   then obtain U where ope: "\<And>x. x \<in> S \<Longrightarrow> openin (subtopology euclidean S) (U x)"
  2046     and cov: "\<And>x. x \<in> S \<Longrightarrow> x \<in> U x"
  2047     and neg: "\<And>x. x \<in> S \<Longrightarrow> negligible (U x)"
  2048     by metis
  2049   obtain \<F> where "\<F> \<subseteq> U ` S" "countable \<F>" and eq: "\<Union>\<F> = \<Union>(U ` S)"
  2050     using ope by (force intro: Lindelof_openin [of "U ` S" S])
  2051   then have "negligible (\<Union>\<F>)"
  2052     by (metis imageE neg negligible_countable_Union subset_eq)
  2053   with eq have "negligible (\<Union>(U ` S))"
  2054     by metis
  2055   moreover have "S \<subseteq> \<Union>(U ` S)"
  2056     using cov by blast
  2057   ultimately show "negligible S"
  2058     using negligible_subset by blast
  2059 qed
  2060 
  2061 lemma locally_negligible:
  2062      "locally negligible S \<longleftrightarrow> negligible S"
  2063   unfolding locally_def
  2064   apply safe
  2065    apply (meson negligible_subset openin_subtopology_self locally_negligible_alt)
  2066   by (meson negligible_subset openin_imp_subset order_refl)
  2067 
  2068 
  2069 subsection\<open>Integral bounds\<close>
  2070 
  2071 lemma set_integral_norm_bound:
  2072   fixes f :: "_ \<Rightarrow> 'a :: {banach, second_countable_topology}"
  2073   shows "set_integrable M k f \<Longrightarrow> norm (LINT x:k|M. f x) \<le> LINT x:k|M. norm (f x)"
  2074   using integral_norm_bound[of M "\<lambda>x. indicator k x *\<^sub>R f x"] by (simp add: set_lebesgue_integral_def)
  2075 
  2076 lemma set_integral_finite_UN_AE:
  2077   fixes f :: "_ \<Rightarrow> _ :: {banach, second_countable_topology}"
  2078   assumes "finite I"
  2079     and ae: "\<And>i j. i \<in> I \<Longrightarrow> j \<in> I \<Longrightarrow> AE x in M. (x \<in> A i \<and> x \<in> A j) \<longrightarrow> i = j"
  2080     and [measurable]: "\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets M"
  2081     and f: "\<And>i. i \<in> I \<Longrightarrow> set_integrable M (A i) f"
  2082   shows "LINT x:(\<Union>i\<in>I. A i)|M. f x = (\<Sum>i\<in>I. LINT x:A i|M. f x)"
  2083   using \<open>finite I\<close> order_refl[of I]
  2084 proof (induction I rule: finite_subset_induct')
  2085   case (insert i I')
  2086   have "AE x in M. (\<forall>j\<in>I'. x \<in> A i \<longrightarrow> x \<notin> A j)"
  2087   proof (intro AE_ball_countable[THEN iffD2] ballI)
  2088     fix j assume "j \<in> I'"
  2089     with \<open>I' \<subseteq> I\<close> \<open>i \<notin> I'\<close> have "i \<noteq> j" "j \<in> I"
  2090       by auto
  2091     then show "AE x in M. x \<in> A i \<longrightarrow> x \<notin> A j"
  2092       using ae[of i j] \<open>i \<in> I\<close> by auto
  2093   qed (use \<open>finite I'\<close> in \<open>rule countable_finite\<close>)
  2094   then have "AE x\<in>A i in M. \<forall>xa\<in>I'. x \<notin> A xa "
  2095     by auto
  2096   with insert.hyps insert.IH[symmetric]
  2097   show ?case
  2098     by (auto intro!: set_integral_Un_AE sets.finite_UN f set_integrable_UN)
  2099 qed (simp add: set_lebesgue_integral_def)
  2100 
  2101 lemma set_integrable_norm:
  2102   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  2103   assumes f: "set_integrable M k f" shows "set_integrable M k (\<lambda>x. norm (f x))"
  2104   using integrable_norm f by (force simp add: set_integrable_def)
  2105  
  2106 lemma absolutely_integrable_bounded_variation:
  2107   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  2108   assumes f: "f absolutely_integrable_on UNIV"
  2109   obtains B where "\<forall>d. d division_of (\<Union>d) \<longrightarrow> sum (\<lambda>k. norm (integral k f)) d \<le> B"
  2110 proof (rule that[of "integral UNIV (\<lambda>x. norm (f x))"]; safe)
  2111   fix d :: "'a set set" assume d: "d division_of \<Union>d"
  2112   have *: "k \<in> d \<Longrightarrow> f absolutely_integrable_on k" for k
  2113     using f[THEN set_integrable_subset, of k] division_ofD(2,4)[OF d, of k] by auto
  2114   note d' = division_ofD[OF d]
  2115   have "(\<Sum>k\<in>d. norm (integral k f)) = (\<Sum>k\<in>d. norm (LINT x:k|lebesgue. f x))"
  2116     by (intro sum.cong refl arg_cong[where f=norm] set_lebesgue_integral_eq_integral(2)[symmetric] *)
  2117   also have "\<dots> \<le> (\<Sum>k\<in>d. LINT x:k|lebesgue. norm (f x))"
  2118     by (intro sum_mono set_integral_norm_bound *)
  2119   also have "\<dots> = (\<Sum>k\<in>d. integral k (\<lambda>x. norm (f x)))"
  2120     by (intro sum.cong refl set_lebesgue_integral_eq_integral(2) set_integrable_norm *)
  2121   also have "\<dots> \<le> integral (\<Union>d) (\<lambda>x. norm (f x))"
  2122     using integrable_on_subdivision[OF d] assms f unfolding absolutely_integrable_on_def
  2123     by (subst integral_combine_division_topdown[OF _ d]) auto
  2124   also have "\<dots> \<le> integral UNIV (\<lambda>x. norm (f x))"
  2125     using integrable_on_subdivision[OF d] assms unfolding absolutely_integrable_on_def
  2126     by (intro integral_subset_le) auto
  2127   finally show "(\<Sum>k\<in>d. norm (integral k f)) \<le> integral UNIV (\<lambda>x. norm (f x))" .
  2128 qed
  2129 
  2130 lemma absdiff_norm_less:
  2131   assumes "sum (\<lambda>x. norm (f x - g x)) s < e"
  2132     and "finite s"
  2133   shows "\<bar>sum (\<lambda>x. norm(f x)) s - sum (\<lambda>x. norm(g x)) s\<bar> < e"
  2134   unfolding sum_subtractf[symmetric]
  2135   apply (rule le_less_trans[OF sum_abs])
  2136   apply (rule le_less_trans[OF _ assms(1)])
  2137   apply (rule sum_mono)
  2138   apply (rule norm_triangle_ineq3)
  2139   done
  2140 
  2141 proposition bounded_variation_absolutely_integrable_interval:
  2142   fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
  2143   assumes f: "f integrable_on cbox a b"
  2144     and *: "\<And>d. d division_of (cbox a b) \<Longrightarrow> sum (\<lambda>K. norm(integral K f)) d \<le> B"
  2145   shows "f absolutely_integrable_on cbox a b"
  2146 proof -
  2147   let ?f = "\<lambda>d. \<Sum>K\<in>d. norm (integral K f)" and ?D = "{d. d division_of (cbox a b)}"
  2148   have D_1: "?D \<noteq> {}"
  2149     by (rule elementary_interval[of a b]) auto
  2150   have D_2: "bdd_above (?f`?D)"
  2151     by (metis * mem_Collect_eq bdd_aboveI2)
  2152   note D = D_1 D_2
  2153   let ?S = "SUP x\<in>?D. ?f x"
  2154   have *: "\<exists>\<gamma>. gauge \<gamma> \<and>
  2155              (\<forall>p. p tagged_division_of cbox a b \<and>
  2156                   \<gamma> fine p \<longrightarrow>
  2157                   norm ((\<Sum>(x,k) \<in> p. content k *\<^sub>R norm (f x)) - ?S) < e)"
  2158     if e: "e > 0" for e
  2159   proof -
  2160     have "?S - e/2 < ?S" using \<open>e > 0\<close> by simp
  2161     then obtain d where d: "d division_of (cbox a b)" "?S - e/2 < (\<Sum>k\<in>d. norm (integral k f))"
  2162       unfolding less_cSUP_iff[OF D] by auto
  2163     note d' = division_ofD[OF this(1)]
  2164 
  2165     have "\<exists>e>0. \<forall>i\<in>d. x \<notin> i \<longrightarrow> ball x e \<inter> i = {}" for x
  2166     proof -
  2167       have "\<exists>d'>0. \<forall>x'\<in>\<Union>{i \<in> d. x \<notin> i}. d' \<le> dist x x'"
  2168       proof (rule separate_point_closed)
  2169         show "closed (\<Union>{i \<in> d. x \<notin> i})"
  2170           using d' by force
  2171         show "x \<notin> \<Union>{i \<in> d. x \<notin> i}"
  2172           by auto
  2173       qed 
  2174       then show ?thesis
  2175         by force
  2176     qed
  2177     then obtain k where k: "\<And>x. 0 < k x" "\<And>i x. \<lbrakk>i \<in> d; x \<notin> i\<rbrakk> \<Longrightarrow> ball x (k x) \<inter> i = {}"
  2178       by metis
  2179     have "e/2 > 0"
  2180       using e by auto
  2181     with Henstock_lemma[OF f] 
  2182     obtain \<gamma> where g: "gauge \<gamma>"
  2183       "\<And>p. \<lbrakk>p tagged_partial_division_of cbox a b; \<gamma> fine p\<rbrakk> 
  2184                 \<Longrightarrow> (\<Sum>(x,k) \<in> p. norm (content k *\<^sub>R f x - integral k f)) < e/2"
  2185       by (metis (no_types, lifting))      
  2186     let ?g = "\<lambda>x. \<gamma> x \<inter> ball x (k x)"
  2187     show ?thesis 
  2188     proof (intro exI conjI allI impI)
  2189       show "gauge ?g"
  2190         using g(1) k(1) by (auto simp: gauge_def)
  2191     next
  2192       fix p
  2193       assume "p tagged_division_of (cbox a b) \<and> ?g fine p"
  2194       then have p: "p tagged_division_of cbox a b" "\<gamma> fine p" "(\<lambda>x. ball x (k x)) fine p"
  2195         by (auto simp: fine_Int)
  2196       note p' = tagged_division_ofD[OF p(1)]
  2197       define p' where "p' = {(x,k) | x k. \<exists>i l. x \<in> i \<and> i \<in> d \<and> (x,l) \<in> p \<and> k = i \<inter> l}"
  2198       have gp': "\<gamma> fine p'"
  2199         using p(2) by (auto simp: p'_def fine_def)
  2200       have p'': "p' tagged_division_of (cbox a b)"
  2201       proof (rule tagged_division_ofI)
  2202         show "finite p'"
  2203         proof (rule finite_subset)
  2204           show "p' \<subseteq> (\<lambda>(k, x, l). (x, k \<inter> l)) ` (d \<times> p)"
  2205             by (force simp: p'_def image_iff)
  2206           show "finite ((\<lambda>(k, x, l). (x, k \<inter> l)) ` (d \<times> p))"
  2207             by (simp add: d'(1) p'(1))
  2208         qed
  2209       next
  2210         fix x K
  2211         assume "(x, K) \<in> p'"
  2212         then have "\<exists>i l. x \<in> i \<and> i \<in> d \<and> (x, l) \<in> p \<and> K = i \<inter> l"
  2213           unfolding p'_def by auto
  2214         then obtain i l where il: "x \<in> i" "i \<in> d" "(x, l) \<in> p" "K = i \<inter> l" by blast
  2215         show "x \<in> K" and "K \<subseteq> cbox a b"
  2216           using p'(2-3)[OF il(3)] il by auto
  2217         show "\<exists>a b. K = cbox a b"
  2218           unfolding il using p'(4)[OF il(3)] d'(4)[OF il(2)] by (meson Int_interval)
  2219       next
  2220         fix x1 K1
  2221         assume "(x1, K1) \<in> p'"
  2222         then have "\<exists>i l. x1 \<in> i \<and> i \<in> d \<and> (x1, l) \<in> p \<and> K1 = i \<inter> l"
  2223           unfolding p'_def by auto
  2224         then obtain i1 l1 where il1: "x1 \<in> i1" "i1 \<in> d" "(x1, l1) \<in> p" "K1 = i1 \<inter> l1" by blast
  2225         fix x2 K2
  2226         assume "(x2,K2) \<in> p'"
  2227         then have "\<exists>i l. x2 \<in> i \<and> i \<in> d \<and> (x2, l) \<in> p \<and> K2 = i \<inter> l"
  2228           unfolding p'_def by auto
  2229         then obtain i2 l2 where il2: "x2 \<in> i2" "i2 \<in> d" "(x2, l2) \<in> p" "K2 = i2 \<inter> l2" by blast
  2230         assume "(x1, K1) \<noteq> (x2, K2)"
  2231         then have "interior i1 \<inter> interior i2 = {} \<or> interior l1 \<inter> interior l2 = {}"
  2232           using d'(5)[OF il1(2) il2(2)] p'(5)[OF il1(3) il2(3)]  by (auto simp: il1 il2)
  2233         then show "interior K1 \<inter> interior K2 = {}"
  2234           unfolding il1 il2 by auto
  2235       next
  2236         have *: "\<forall>(x, X) \<in> p'. X \<subseteq> cbox a b"
  2237           unfolding p'_def using d' by blast
  2238         have "y \<in> \<Union>{K. \<exists>x. (x, K) \<in> p'}" if y: "y \<in> cbox a b" for y
  2239         proof -
  2240           obtain x l where xl: "(x, l) \<in> p" "y \<in> l" 
  2241             using y unfolding p'(6)[symmetric] by auto
  2242           obtain i where i: "i \<in> d" "y \<in> i" 
  2243             using y unfolding d'(6)[symmetric] by auto
  2244           have "x \<in> i"
  2245             using fineD[OF p(3) xl(1)] using k(2) i xl by auto
  2246           then show ?thesis
  2247             unfolding p'_def by (rule_tac X="i \<inter> l" in UnionI) (use i xl in auto)
  2248         qed
  2249         show "\<Union>{K. \<exists>x. (x, K) \<in> p'} = cbox a b"
  2250         proof
  2251           show "\<Union>{k. \<exists>x. (x, k) \<in> p'} \<subseteq> cbox a b"
  2252             using * by auto
  2253         next
  2254           show "cbox a b \<subseteq> \<Union>{k. \<exists>x. (x, k) \<in> p'}"
  2255           proof 
  2256             fix y
  2257             assume y: "y \<in> cbox a b"
  2258             obtain x L where xl: "(x, L) \<in> p" "y \<in> L" 
  2259               using y unfolding p'(6)[symmetric] by auto
  2260             obtain I where i: "I \<in> d" "y \<in> I" 
  2261               using y unfolding d'(6)[symmetric] by auto
  2262             have "x \<in> I"
  2263               using fineD[OF p(3) xl(1)] using k(2) i xl by auto
  2264             then show "y \<in> \<Union>{k. \<exists>x. (x, k) \<in> p'}"
  2265               apply (rule_tac X="I \<inter> L" in UnionI)
  2266               using i xl by (auto simp: p'_def)
  2267           qed
  2268         qed
  2269       qed
  2270 
  2271       then have sum_less_e2: "(\<Sum>(x,K) \<in> p'. norm (content K *\<^sub>R f x - integral K f)) < e/2"
  2272         using g(2) gp' tagged_division_of_def by blast
  2273 
  2274       have "(x, I \<inter> L) \<in> p'" if x: "(x, L) \<in> p" "I \<in> d" and y: "y \<in> I" "y \<in> L"
  2275         for x I L y
  2276       proof -
  2277         have "x \<in> I"
  2278           using fineD[OF p(3) that(1)] k(2)[OF \<open>I \<in> d\<close>] y by auto
  2279         with x have "(\<exists>i l. x \<in> i \<and> i \<in> d \<and> (x, l) \<in> p \<and> I \<inter> L = i \<inter> l)"
  2280           by blast
  2281         then have "(x, I \<inter> L) \<in> p'"
  2282           by (simp add: p'_def)
  2283         with y show ?thesis by auto
  2284       qed
  2285       moreover have "\<exists>y i l. (x, K) = (y, i \<inter> l) \<and> (y, l) \<in> p \<and> i \<in> d \<and> i \<inter> l \<noteq> {}"
  2286         if xK: "(x,K) \<in> p'" for x K
  2287       proof -
  2288         obtain i l where il: "x \<in> i" "i \<in> d" "(x, l) \<in> p" "K = i \<inter> l" 
  2289           using xK unfolding p'_def by auto
  2290         then show ?thesis
  2291           using p'(2) by fastforce
  2292       qed
  2293       ultimately have p'alt: "p' = {(x, I \<inter> L) | x I L. (x,L) \<in> p \<and> I \<in> d \<and> I \<inter> L \<noteq> {}}"
  2294         by auto
  2295       have sum_p': "(\<Sum>(x,K) \<in> p'. norm (integral K f)) = (\<Sum>k\<in>snd ` p'. norm (integral k f))"
  2296         apply (subst sum.over_tagged_division_lemma[OF p'',of "\<lambda>k. norm (integral k f)"])
  2297          apply (auto intro: integral_null simp: content_eq_0_interior)
  2298         done
  2299       have snd_p_div: "snd ` p division_of cbox a b"
  2300         by (rule division_of_tagged_division[OF p(1)])
  2301       note snd_p = division_ofD[OF snd_p_div]
  2302       have fin_d_sndp: "finite (d \<times> snd ` p)"
  2303         by (simp add: d'(1) snd_p(1))
  2304 
  2305       have *: "\<And>sni sni' sf sf'. \<lbrakk>\<bar>sf' - sni'\<bar> < e/2; ?S - e/2 < sni; sni' \<le> ?S;
  2306                        sni \<le> sni'; sf' = sf\<rbrakk> \<Longrightarrow> \<bar>sf - ?S\<bar> < e"
  2307         by arith
  2308       show "norm ((\<Sum>(x,k) \<in> p. content k *\<^sub>R norm (f x)) - ?S) < e"
  2309         unfolding real_norm_def
  2310       proof (rule *)
  2311         show "\<bar>(\<Sum>(x,K)\<in>p'. norm (content K *\<^sub>R f x)) - (\<Sum>(x,k)\<in>p'. norm (integral k f))\<bar> < e/2"
  2312           using p'' sum_less_e2 unfolding split_def by (force intro!: absdiff_norm_less)
  2313         show "(\<Sum>(x,k) \<in> p'. norm (integral k f)) \<le>?S"
  2314           by (auto simp: sum_p' division_of_tagged_division[OF p''] D intro!: cSUP_upper)
  2315         show "(\<Sum>k\<in>d. norm (integral k f)) \<le> (\<Sum>(x,k) \<in> p'. norm (integral k f))"
  2316         proof -
  2317           have *: "{k \<inter> l | k l. k \<in> d \<and> l \<in> snd ` p} = (\<lambda>(k,l). k \<inter> l) ` (d \<times> snd ` p)"
  2318             by auto
  2319           have "(\<Sum>K\<in>d. norm (integral K f)) \<le> (\<Sum>i\<in>d. \<Sum>l\<in>snd ` p. norm (integral (i \<inter> l) f))"
  2320           proof (rule sum_mono)
  2321             fix K assume k: "K \<in> d"
  2322             from d'(4)[OF this] obtain u v where uv: "K = cbox u v" by metis
  2323             define d' where "d' = {cbox u v \<inter> l |l. l \<in> snd ` p \<and>  cbox u v \<inter> l \<noteq> {}}"
  2324             have uvab: "cbox u v \<subseteq> cbox a b"
  2325               using d(1) k uv by blast
  2326             have "d' division_of cbox u v"
  2327               unfolding d'_def by (rule division_inter_1 [OF snd_p_div uvab])
  2328             moreover then have "norm (\<Sum>i\<in>d'. integral i f) \<le> (\<Sum>k\<in>d'. norm (integral k f))"
  2329               by (simp add: sum_norm_le)
  2330             ultimately have "norm (integral K f) \<le> sum (\<lambda>k. norm (integral k f)) d'"
  2331               apply (subst integral_combine_division_topdown[of _ _ d'])
  2332                 apply (auto simp: uv intro: integrable_on_subcbox[OF assms(1) uvab])
  2333               done
  2334             also have "\<dots> = (\<Sum>I\<in>{K \<inter> L |L. L \<in> snd ` p}. norm (integral I f))"
  2335             proof -
  2336               have *: "norm (integral I f) = 0"
  2337                 if "I \<in> {cbox u v \<inter> l |l. l \<in> snd ` p}"
  2338                   "I \<notin> {cbox u v \<inter> l |l. l \<in> snd ` p \<and> cbox u v \<inter> l \<noteq> {}}" for I
  2339                 using that by auto
  2340               show ?thesis
  2341                 apply (rule sum.mono_neutral_left)
  2342                   apply (simp add: snd_p(1))
  2343                 unfolding d'_def uv using * by auto 
  2344             qed
  2345             also have "\<dots> = (\<Sum>l\<in>snd ` p. norm (integral (K \<inter> l) f))"
  2346             proof -
  2347               have *: "norm (integral (K \<inter> l) f) = 0"
  2348                 if "l \<in> snd ` p" "y \<in> snd ` p" "l \<noteq> y" "K \<inter> l = K \<inter> y" for l y
  2349               proof -
  2350                 have "interior (K \<inter> l) \<subseteq> interior (l \<inter> y)"
  2351                   by (metis Int_lower2 interior_mono le_inf_iff that(4))
  2352                 then have "interior (K \<inter> l) = {}"
  2353                   by (simp add: snd_p(5) that) 
  2354                 moreover from d'(4)[OF k] snd_p(4)[OF that(1)] 
  2355                 obtain u1 v1 u2 v2
  2356                   where uv: "K = cbox u1 u2" "l = cbox v1 v2" by metis
  2357                 ultimately show ?thesis
  2358                   using that integral_null
  2359                   unfolding uv Int_interval content_eq_0_interior
  2360                   by (metis (mono_tags, lifting) norm_eq_zero)
  2361               qed
  2362               show ?thesis
  2363                 unfolding Setcompr_eq_image
  2364                 apply (rule sum.reindex_nontrivial [unfolded o_def])
  2365                  apply (rule finite_imageI)
  2366                  apply (rule p')
  2367                 using * by auto
  2368             qed
  2369             finally show "norm (integral K f) \<le> (\<Sum>l\<in>snd ` p. norm (integral (K \<inter> l) f))" .
  2370           qed
  2371           also have "\<dots> = (\<Sum>(i,l) \<in> d \<times> snd ` p. norm (integral (i\<inter>l) f))"
  2372             by (simp add: sum.cartesian_product)
  2373           also have "\<dots> = (\<Sum>x \<in> d \<times> snd ` p. norm (integral (case_prod (\<inter>) x) f))"
  2374             by (force simp: split_def intro!: sum.cong)
  2375           also have "\<dots> = (\<Sum>k\<in>{i \<inter> l |i l. i \<in> d \<and> l \<in> snd ` p}. norm (integral k f))"
  2376           proof -
  2377             have eq0: " (integral (l1 \<inter> k1) f) = 0"
  2378               if "l1 \<inter> k1 = l2 \<inter> k2" "(l1, k1) \<noteq> (l2, k2)"
  2379                 "l1 \<in> d" "(j1,k1) \<in> p" "l2 \<in> d" "(j2,k2) \<in> p"
  2380               for l1 l2 k1 k2 j1 j2
  2381             proof -
  2382               obtain u1 v1 u2 v2 where uv: "l1 = cbox u1 u2" "k1 = cbox v1 v2"
  2383                 using \<open>(j1, k1) \<in> p\<close> \<open>l1 \<in> d\<close> d'(4) p'(4) by blast
  2384               have "l1 \<noteq> l2 \<or> k1 \<noteq> k2"
  2385                 using that by auto
  2386               then have "interior k1 \<inter> interior k2 = {} \<or> interior l1 \<inter> interior l2 = {}"
  2387                 by (meson d'(5) old.prod.inject p'(5) that(3) that(4) that(5) that(6))
  2388               moreover have "interior (l1 \<inter> k1) = interior (l2 \<inter> k2)"
  2389                 by (simp add: that(1))
  2390               ultimately have "interior(l1 \<inter> k1) = {}"
  2391                 by auto
  2392               then show ?thesis
  2393                 unfolding uv Int_interval content_eq_0_interior[symmetric] by auto
  2394             qed
  2395             show ?thesis
  2396               unfolding *
  2397               apply (rule sum.reindex_nontrivial [OF fin_d_sndp, symmetric, unfolded o_def])
  2398               apply clarsimp
  2399               by (metis eq0 fst_conv snd_conv)
  2400           qed
  2401           also have "\<dots> = (\<Sum>(x,k) \<in> p'. norm (integral k f))"
  2402           proof -
  2403             have 0: "integral (ia \<inter> snd (a, b)) f = 0"
  2404               if "ia \<inter> snd (a, b) \<notin> snd ` p'" "ia \<in> d" "(a, b) \<in> p" for ia a b
  2405             proof -
  2406               have "ia \<inter> b = {}"
  2407                 using that unfolding p'alt image_iff Bex_def not_ex
  2408                 apply (erule_tac x="(a, ia \<inter> b)" in allE)
  2409                 apply auto
  2410                 done
  2411               then show ?thesis by auto
  2412             qed
  2413             have 1: "\<exists>i l. snd (a, b) = i \<inter> l \<and> i \<in> d \<and> l \<in> snd ` p" if "(a, b) \<in> p'" for a b
  2414               using that 
  2415               apply (clarsimp simp: p'_def image_iff)
  2416               by (metis (no_types, hide_lams) snd_conv)
  2417             show ?thesis
  2418               unfolding sum_p'
  2419               apply (rule sum.mono_neutral_right)
  2420                 apply (metis * finite_imageI[OF fin_d_sndp])
  2421               using 0 1 by auto
  2422           qed
  2423           finally show ?thesis .
  2424         qed
  2425         show "(\<Sum>(x,k) \<in> p'. norm (content k *\<^sub>R f x)) = (\<Sum>(x,k) \<in> p. content k *\<^sub>R norm (f x))"
  2426         proof -
  2427           let ?S = "{(x, i \<inter> l) |x i l. (x, l) \<in> p \<and> i \<in> d}"
  2428           have *: "?S = (\<lambda>(xl,i). (fst xl, snd xl \<inter> i)) ` (p \<times> d)"
  2429             by force
  2430           have fin_pd: "finite (p \<times> d)"
  2431             using finite_cartesian_product[OF p'(1) d'(1)] by metis
  2432           have "(\<Sum>(x,k) \<in> p'. norm (content k *\<^sub>R f x)) = (\<Sum>(x,k) \<in> ?S. \<bar>content k\<bar> * norm (f x))"
  2433             unfolding norm_scaleR
  2434             apply (rule sum.mono_neutral_left)
  2435               apply (subst *)
  2436               apply (rule finite_imageI [OF fin_pd])
  2437             unfolding p'alt apply auto
  2438             by fastforce
  2439           also have "\<dots> = (\<Sum>((x,l),i)\<in>p \<times> d. \<bar>content (l \<inter> i)\<bar> * norm (f x))"
  2440           proof -
  2441             have "\<bar>content (l1 \<inter> k1)\<bar> * norm (f x1) = 0"
  2442               if "(x1, l1) \<in> p" "(x2, l2) \<in> p" "k1 \<in> d" "k2 \<in> d"
  2443                 "x1 = x2" "l1 \<inter> k1 = l2 \<inter> k2" "x1 \<noteq> x2 \<or> l1 \<noteq> l2 \<or> k1 \<noteq> k2"
  2444               for x1 l1 k1 x2 l2 k2
  2445             proof -
  2446               obtain u1 v1 u2 v2 where uv: "k1 = cbox u1 u2" "l1 = cbox v1 v2"
  2447                 by (meson \<open>(x1, l1) \<in> p\<close> \<open>k1 \<in> d\<close> d(1) division_ofD(4) p'(4))
  2448               have "l1 \<noteq> l2 \<or> k1 \<noteq> k2"
  2449                 using that by auto
  2450               then have "interior k1 \<inter> interior k2 = {} \<or> interior l1 \<inter> interior l2 = {}"
  2451                 apply (rule disjE)
  2452                 using that p'(5) d'(5) by auto
  2453               moreover have "interior (l1 \<inter> k1) = interior (l2 \<inter> k2)"
  2454                 unfolding that ..
  2455               ultimately have "interior (l1 \<inter> k1) = {}"
  2456                 by auto
  2457               then show "\<bar>content (l1 \<inter> k1)\<bar> * norm (f x1) = 0"
  2458                 unfolding uv Int_interval content_eq_0_interior[symmetric] by auto
  2459             qed 
  2460             then show ?thesis
  2461               unfolding *
  2462               apply (subst sum.reindex_nontrivial [OF fin_pd])
  2463               unfolding split_paired_all o_def split_def prod.inject
  2464                apply force+
  2465               done
  2466           qed
  2467           also have "\<dots> = (\<Sum>(x,k) \<in> p. content k *\<^sub>R norm (f x))"
  2468           proof -
  2469             have sumeq: "(\<Sum>i\<in>d. content (l \<inter> i) * norm (f x)) = content l * norm (f x)"
  2470               if "(x, l) \<in> p" for x l
  2471             proof -
  2472               note xl = p'(2-4)[OF that]
  2473               then obtain u v where uv: "l = cbox u v" by blast
  2474               have "(\<Sum>i\<in>d. \<bar>content (l \<inter> i)\<bar>) = (\<Sum>k\<in>d. content (k \<inter> cbox u v))"
  2475                 by (simp add: Int_commute uv)
  2476               also have "\<dots> = sum content {k \<inter> cbox u v| k. k \<in> d}"
  2477               proof -
  2478                 have eq0: "content (k \<inter> cbox u v) = 0"
  2479                   if "k \<in> d" "y \<in> d" "k \<noteq> y" and eq: "k \<inter> cbox u v = y \<inter> cbox u v" for k y
  2480                 proof -
  2481                   from d'(4)[OF that(1)] d'(4)[OF that(2)]
  2482                   obtain \<alpha> \<beta> where \<alpha>: "k \<inter> cbox u v = cbox \<alpha> \<beta>"
  2483                     by (meson Int_interval)
  2484                   have "{} = interior ((k \<inter> y) \<inter> cbox u v)"
  2485                     by (simp add: d'(5) that)
  2486                   also have "\<dots> = interior (y \<inter> (k \<inter> cbox u v))"
  2487                     by auto
  2488                   also have "\<dots> = interior (k \<inter> cbox u v)"
  2489                     unfolding eq by auto
  2490                   finally show ?thesis
  2491                     unfolding \<alpha> content_eq_0_interior ..
  2492                 qed
  2493                 then show ?thesis
  2494                   unfolding Setcompr_eq_image
  2495                   apply (rule sum.reindex_nontrivial [OF \<open>finite d\<close>, unfolded o_def, symmetric])
  2496                   by auto
  2497               qed
  2498               also have "\<dots> = sum content {cbox u v \<inter> k |k. k \<in> d \<and> cbox u v \<inter> k \<noteq> {}}"
  2499                 apply (rule sum.mono_neutral_right)
  2500                 unfolding Setcompr_eq_image
  2501                   apply (rule finite_imageI [OF \<open>finite d\<close>])
  2502                  apply (fastforce simp: inf.commute)+
  2503                 done
  2504               finally show "(\<Sum>i\<in>d. content (l \<inter> i) * norm (f x)) = content l * norm (f x)"
  2505                 unfolding sum_distrib_right[symmetric] real_scaleR_def
  2506                 apply (subst(asm) additive_content_division[OF division_inter_1[OF d(1)]])
  2507                 using xl(2)[unfolded uv] unfolding uv apply auto
  2508                 done
  2509             qed
  2510             show ?thesis
  2511               by (subst sum_Sigma_product[symmetric]) (auto intro!: sumeq sum.cong p' d')
  2512           qed
  2513           finally show ?thesis .
  2514         qed
  2515       qed (rule d)
  2516     qed 
  2517   qed
  2518   then show ?thesis
  2519     using absolutely_integrable_onI [OF f has_integral_integrable] has_integral[of _ ?S]
  2520     by blast
  2521 qed
  2522 
  2523 
  2524 lemma bounded_variation_absolutely_integrable:
  2525   fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
  2526   assumes "f integrable_on UNIV"
  2527     and "\<forall>d. d division_of (\<Union>d) \<longrightarrow> sum (\<lambda>k. norm (integral k f)) d \<le> B"
  2528   shows "f absolutely_integrable_on UNIV"
  2529 proof (rule absolutely_integrable_onI, fact)
  2530   let ?f = "\<lambda>d. \<Sum>k\<in>d. norm (integral k f)" and ?D = "{d. d division_of  (\<Union>d)}"
  2531   have D_1: "?D \<noteq> {}"
  2532     by (rule elementary_interval) auto
  2533   have D_2: "bdd_above (?f`?D)"
  2534     by (intro bdd_aboveI2[where M=B] assms(2)[rule_format]) simp
  2535   note D = D_1 D_2
  2536   let ?S = "SUP d\<in>?D. ?f d"
  2537   have "\<And>a b. f integrable_on cbox a b"
  2538     using assms(1) integrable_on_subcbox by blast
  2539   then have f_int: "\<And>a b. f absolutely_integrable_on cbox a b"
  2540     apply (rule bounded_variation_absolutely_integrable_interval[where B=B])
  2541     using assms(2) apply blast
  2542     done
  2543   have "((\<lambda>x. norm (f x)) has_integral ?S) UNIV"
  2544     apply (subst has_integral_alt')
  2545     apply safe
  2546   proof goal_cases
  2547     case (1 a b)
  2548     show ?case
  2549       using f_int[of a b] unfolding absolutely_integrable_on_def by auto
  2550   next
  2551     case prems: (2 e)
  2552     have "\<exists>y\<in>sum (\<lambda>k. norm (integral k f)) ` {d. d division_of \<Union>d}. \<not> y \<le> ?S - e"
  2553     proof (rule ccontr)
  2554       assume "\<not> ?thesis"
  2555       then have "?S \<le> ?S - e"
  2556         by (intro cSUP_least[OF D(1)]) auto
  2557       then show False
  2558         using prems by auto
  2559     qed
  2560     then obtain d K where ddiv: "d division_of \<Union>d" and "K = (\<Sum>k\<in>d. norm (integral k f))"
  2561       "Sup (sum (\<lambda>k. norm (integral k f)) ` {d. d division_of \<Union> d}) - e < K"
  2562       by (auto simp add: image_iff not_le)
  2563     then have d: "Sup (sum (\<lambda>k. norm (integral k f)) ` {d. d division_of \<Union> d}) - e 
  2564                   < (\<Sum>k\<in>d. norm (integral k f))"
  2565       by auto
  2566     note d'=division_ofD[OF ddiv]
  2567     have "bounded (\<Union>d)"
  2568       by (rule elementary_bounded,fact)
  2569     from this[unfolded bounded_pos] obtain K where
  2570        K: "0 < K" "\<forall>x\<in>\<Union>d. norm x \<le> K" by auto
  2571     show ?case
  2572     proof (intro conjI impI allI exI)
  2573       fix a b :: 'n
  2574       assume ab: "ball 0 (K + 1) \<subseteq> cbox a b"
  2575       have *: "\<And>s s1. \<lbrakk>?S - e < s1; s1 \<le> s; s < ?S + e\<rbrakk> \<Longrightarrow> \<bar>s - ?S\<bar> < e"
  2576         by arith
  2577       show "norm (integral (cbox a b) (\<lambda>x. if x \<in> UNIV then norm (f x) else 0) - ?S) < e"
  2578         unfolding real_norm_def
  2579       proof (rule * [OF d])
  2580         have "(\<Sum>k\<in>d. norm (integral k f)) \<le> sum (\<lambda>k. integral k (\<lambda>x. norm (f x))) d"
  2581         proof (intro sum_mono)
  2582           fix k assume "k \<in> d"
  2583           with d'(4) f_int show "norm (integral k f) \<le> integral k (\<lambda>x. norm (f x))"
  2584             by (force simp: absolutely_integrable_on_def integral_norm_bound_integral)
  2585         qed
  2586         also have "\<dots> = integral (\<Union>d) (\<lambda>x. norm (f x))"
  2587           apply (rule integral_combine_division_bottomup[OF ddiv, symmetric])
  2588           using absolutely_integrable_on_def d'(4) f_int by blast
  2589         also have "\<dots> \<le> integral (cbox a b) (\<lambda>x. if x \<in> UNIV then norm (f x) else 0)"
  2590         proof -
  2591           have "\<Union>d \<subseteq> cbox a b"
  2592             using K(2) ab by fastforce
  2593           then show ?thesis
  2594             using integrable_on_subdivision[OF ddiv] f_int[of a b] unfolding absolutely_integrable_on_def
  2595             by (auto intro!: integral_subset_le)
  2596         qed
  2597         finally show "(\<Sum>k\<in>d. norm (integral k f))
  2598                       \<le> integral (cbox a b) (\<lambda>x. if x \<in> UNIV then norm (f x) else 0)" .
  2599       next
  2600         have "e/2>0"
  2601           using \<open>e > 0\<close> by auto
  2602         moreover
  2603         have f: "f integrable_on cbox a b" "(\<lambda>x. norm (f x)) integrable_on cbox a b"
  2604           using f_int by (auto simp: absolutely_integrable_on_def)
  2605         ultimately obtain d1 where "gauge d1"
  2606            and d1: "\<And>p. \<lbrakk>p tagged_division_of (cbox a b); d1 fine p\<rbrakk> \<Longrightarrow>
  2607             norm ((\<Sum>(x,k) \<in> p. content k *\<^sub>R norm (f x)) - integral (cbox a b) (\<lambda>x. norm (f x))) < e/2"
  2608           unfolding has_integral_integral has_integral by meson
  2609         obtain d2 where "gauge d2" 
  2610           and d2: "\<And>p. \<lbrakk>p tagged_partial_division_of (cbox a b); d2 fine p\<rbrakk> \<Longrightarrow>
  2611             (\<Sum>(x,k) \<in> p. norm (content k *\<^sub>R f x - integral k f)) < e/2"
  2612           by (blast intro: Henstock_lemma [OF f(1) \<open>e/2>0\<close>])
  2613         obtain p where
  2614           p: "p tagged_division_of (cbox a b)" "d1 fine p" "d2 fine p"
  2615           by (rule fine_division_exists [OF gauge_Int [OF \<open>gauge d1\<close> \<open>gauge d2\<close>], of a b])
  2616             (auto simp add: fine_Int)
  2617         have *: "\<And>sf sf' si di. \<lbrakk>sf' = sf; si \<le> ?S; \<bar>sf - si\<bar> < e/2;
  2618                       \<bar>sf' - di\<bar> < e/2\<rbrakk> \<Longrightarrow> di < ?S + e"
  2619           by arith
  2620         have "integral (cbox a b) (\<lambda>x. norm (f x)) < ?S + e"
  2621         proof (rule *)
  2622           show "\<bar>(\<Sum>(x,k)\<in>p. norm (content k *\<^sub>R f x)) - (\<Sum>(x,k)\<in>p. norm (integral k f))\<bar> < e/2"
  2623             unfolding split_def
  2624             apply (rule absdiff_norm_less)
  2625             using d2[of p] p(1,3) apply (auto simp: tagged_division_of_def split_def)
  2626             done
  2627           show "\<bar>(\<Sum>(x,k) \<in> p. content k *\<^sub>R norm (f x)) - integral (cbox a b) (\<lambda>x. norm(f x))\<bar> < e/2"
  2628             using d1[OF p(1,2)] by (simp only: real_norm_def)
  2629           show "(\<Sum>(x,k) \<in> p. content k *\<^sub>R norm (f x)) = (\<Sum>(x,k) \<in> p. norm (content k *\<^sub>R f x))"
  2630             by (auto simp: split_paired_all sum.cong [OF refl])
  2631           show "(\<Sum>(x,k) \<in> p. norm (integral k f)) \<le> ?S"
  2632             using partial_division_of_tagged_division[of p "cbox a b"] p(1)
  2633             apply (subst sum.over_tagged_division_lemma[OF p(1)])
  2634             apply (auto simp: content_eq_0_interior tagged_partial_division_of_def intro!: cSUP_upper2 D)
  2635             done
  2636         qed
  2637         then show "integral (cbox a b) (\<lambda>x. if x \<in> UNIV then norm (f x) else 0) < ?S + e"
  2638           by simp
  2639       qed
  2640     qed (insert K, auto)
  2641   qed
  2642   then show "(\<lambda>x. norm (f x)) integrable_on UNIV"
  2643     by blast
  2644 qed
  2645 
  2646 
  2647 subsection\<open>Outer and inner approximation of measurable sets by well-behaved sets.\<close>
  2648 
  2649 proposition measurable_outer_intervals_bounded:
  2650   assumes "S \<in> lmeasurable" "S \<subseteq> cbox a b" "e > 0"
  2651   obtains \<D>
  2652   where "countable \<D>"
  2653         "\<And>K. K \<in> \<D> \<Longrightarrow> K \<subseteq> cbox a b \<and> K \<noteq> {} \<and> (\<exists>c d. K = cbox c d)"
  2654         "pairwise (\<lambda>A B. interior A \<inter> interior B = {}) \<D>"
  2655         "\<And>u v. cbox u v \<in> \<D> \<Longrightarrow> \<exists>n. \<forall>i \<in> Basis. v \<bullet> i - u \<bullet> i = (b \<bullet> i - a \<bullet> i)/2^n"
  2656         "\<And>K. \<lbrakk>K \<in> \<D>; box a b \<noteq> {}\<rbrakk> \<Longrightarrow> interior K \<noteq> {}"
  2657         "S \<subseteq> \<Union>\<D>" "\<Union>\<D> \<in> lmeasurable" "measure lebesgue (\<Union>\<D>) \<le> measure lebesgue S + e"
  2658 proof (cases "box a b = {}")
  2659   case True
  2660   show ?thesis
  2661   proof (cases "cbox a b = {}")
  2662     case True
  2663     with assms have [simp]: "S = {}"
  2664       by auto
  2665     show ?thesis
  2666     proof
  2667       show "countable {}"
  2668         by simp
  2669     qed (use \<open>e > 0\<close> in auto)
  2670   next
  2671     case False
  2672     show ?thesis
  2673     proof
  2674       show "countable {cbox a b}"
  2675         by simp
  2676       show "\<And>u v. cbox u v \<in> {cbox a b} \<Longrightarrow> \<exists>n. \<forall>i\<in>Basis. v \<bullet> i - u \<bullet> i = (b \<bullet> i - a \<bullet> i)/2 ^ n"
  2677         using False by (force simp: eq_cbox intro: exI [where x=0])
  2678       show "measure lebesgue (\<Union>{cbox a b}) \<le> measure lebesgue S + e"
  2679         using assms by (simp add: sum_content.box_empty_imp [OF True])
  2680     qed (use assms \<open>cbox a b \<noteq> {}\<close> in auto)
  2681   qed
  2682 next
  2683   case False
  2684   let ?\<mu> = "measure lebesgue"
  2685   have "S \<inter> cbox a b \<in> lmeasurable"
  2686     using \<open>S \<in> lmeasurable\<close> by blast
  2687   then have indS_int: "(indicator S has_integral (?\<mu> S)) (cbox a b)"
  2688     by (metis integral_indicator \<open>S \<subseteq> cbox a b\<close> has_integral_integrable_integral inf.orderE integrable_on_indicator)
  2689   with \<open>e > 0\<close> obtain \<gamma> where "gauge \<gamma>" and \<gamma>:
  2690     "\<And>\<D>. \<lbrakk>\<D> tagged_division_of (cbox a b); \<gamma> fine \<D>\<rbrakk> \<Longrightarrow> norm ((\<Sum>(x,K)\<in>\<D>. content(K) *\<^sub>R indicator S x) - ?\<mu> S) < e"
  2691     by (force simp: has_integral)
  2692   have inteq: "integral (cbox a b) (indicat_real S) = integral UNIV (indicator S)"
  2693     using assms by (metis has_integral_iff indS_int lmeasure_integral_UNIV)
  2694   obtain \<D> where \<D>: "countable \<D>"  "\<Union>\<D> \<subseteq> cbox a b"
  2695             and cbox: "\<And>K. K \<in> \<D> \<Longrightarrow> interior K \<noteq> {} \<and> (\<exists>c d. K = cbox c d)"
  2696             and djointish: "pairwise (\<lambda>A B. interior A \<inter> interior B = {}) \<D>"
  2697             and covered: "\<And>K. K \<in> \<D> \<Longrightarrow> \<exists>x \<in> S \<inter> K. K \<subseteq> \<gamma> x"
  2698             and close: "\<And>u v. cbox u v \<in> \<D> \<Longrightarrow> \<exists>n. \<forall>i \<in> Basis. v\<bullet>i - u\<bullet>i = (b\<bullet>i - a\<bullet>i)/2^n"
  2699             and covers: "S \<subseteq> \<Union>\<D>"
  2700     using covering_lemma [of S a b \<gamma>] \<open>gauge \<gamma>\<close> \<open>box a b \<noteq> {}\<close> assms by force
  2701   show ?thesis
  2702   proof
  2703     show "\<And>K. K \<in> \<D> \<Longrightarrow> K \<subseteq> cbox a b \<and> K \<noteq> {} \<and> (\<exists>c d. K = cbox c d)"
  2704       by (meson Sup_le_iff \<D>(2) cbox interior_empty)
  2705     have negl_int: "negligible(K \<inter> L)" if "K \<in> \<D>" "L \<in> \<D>" "K \<noteq> L" for K L
  2706     proof -
  2707       have "interior K \<inter> interior L = {}"
  2708         using djointish pairwiseD that by fastforce
  2709       moreover obtain u v x y where "K = cbox u v" "L = cbox x y"
  2710         using cbox \<open>K \<in> \<D>\<close> \<open>L \<in> \<D>\<close> by blast
  2711       ultimately show ?thesis
  2712         by (simp add: Int_interval box_Int_box negligible_interval(1))
  2713     qed
  2714     have fincase: "\<Union>\<F> \<in> lmeasurable \<and> ?\<mu> (\<Union>\<F>) \<le> ?\<mu> S + e" if "finite \<F>" "\<F> \<subseteq> \<D>" for \<F>
  2715     proof -
  2716       obtain t where t: "\<And>K. K \<in> \<F> \<Longrightarrow> t K \<in> S \<inter> K \<and> K \<subseteq> \<gamma>(t K)"
  2717         using covered \<open>\<F> \<subseteq> \<D>\<close> subsetD by metis
  2718       have "\<forall>K \<in> \<F>. \<forall>L \<in> \<F>. K \<noteq> L \<longrightarrow> interior K \<inter> interior L = {}"
  2719         using that djointish by (simp add: pairwise_def) (metis subsetD)
  2720       with cbox that \<D> have \<F>div: "\<F> division_of (\<Union>\<F>)"
  2721         by (fastforce simp: division_of_def dest: cbox)
  2722       then have 1: "\<Union>\<F> \<in> lmeasurable"
  2723         by blast
  2724       have norme: "\<And>p. \<lbrakk>p tagged_division_of cbox a b; \<gamma> fine p\<rbrakk>
  2725           \<Longrightarrow> norm ((\<Sum>(x,K)\<in>p. content K * indicator S x) - integral (cbox a b) (indicator S)) < e"
  2726         by (auto simp: lmeasure_integral_UNIV assms inteq dest: \<gamma>)
  2727       have "\<forall>x K y L. (x,K) \<in> (\<lambda>K. (t K,K)) ` \<F> \<and> (y,L) \<in> (\<lambda>K. (t K,K)) ` \<F> \<and> (x,K) \<noteq> (y,L) \<longrightarrow>             interior K \<inter> interior L = {}"
  2728         using that djointish  by (clarsimp simp: pairwise_def) (metis subsetD)
  2729       with that \<D> have tagged: "(\<lambda>K. (t K, K)) ` \<F> tagged_partial_division_of cbox a b"
  2730         by (auto simp: tagged_partial_division_of_def dest: t cbox)
  2731       have fine: "\<gamma> fine (\<lambda>K. (t K, K)) ` \<F>"
  2732         using t by (auto simp: fine_def)
  2733       have *: "y \<le> ?\<mu> S \<Longrightarrow> \<bar>x - y\<bar> \<le> e \<Longrightarrow> x \<le> ?\<mu> S + e" for x y
  2734         by arith
  2735       have "?\<mu> (\<Union>\<F>) \<le> ?\<mu> S + e"
  2736       proof (rule *)
  2737         have "(\<Sum>K\<in>\<F>. ?\<mu> (K \<inter> S)) = ?\<mu> (\<Union>C\<in>\<F>. C \<inter> S)"
  2738           apply (rule measure_negligible_finite_Union_image [OF \<open>finite \<F>\<close>, symmetric])
  2739           using \<F>div \<open>S \<in> lmeasurable\<close> apply blast
  2740           unfolding pairwise_def
  2741           by (metis inf.commute inf_sup_aci(3) negligible_Int subsetCE negl_int \<open>\<F> \<subseteq> \<D>\<close>)
  2742         also have "\<dots> = ?\<mu> (\<Union>\<F> \<inter> S)"
  2743           by simp
  2744         also have "\<dots> \<le> ?\<mu> S"
  2745           by (simp add: "1" \<open>S \<in> lmeasurable\<close> fmeasurableD measure_mono_fmeasurable sets.Int)
  2746         finally show "(\<Sum>K\<in>\<F>. ?\<mu> (K \<inter> S)) \<le> ?\<mu> S" .
  2747       next
  2748         have "?\<mu> (\<Union>\<F>) = sum ?\<mu> \<F>"
  2749           by (metis \<F>div content_division)
  2750         also have "\<dots> = (\<Sum>K\<in>\<F>. content K)"
  2751           using \<F>div by (force intro: sum.cong)
  2752         also have "\<dots> = (\<Sum>x\<in>\<F>. content x * indicator S (t x))"
  2753           using t by auto
  2754         finally have eq1: "?\<mu> (\<Union>\<F>) = (\<Sum>x\<in>\<F>. content x * indicator S (t x))" .
  2755         have eq2: "(\<Sum>K\<in>\<F>. ?\<mu> (K \<inter> S)) = (\<Sum>K\<in>\<F>. integral K (indicator S))"
  2756           apply (rule sum.cong [OF refl])
  2757           by (metis integral_indicator \<F>div \<open>S \<in> lmeasurable\<close> division_ofD(4) fmeasurable.Int inf.commute lmeasurable_cbox)
  2758         have "\<bar>\<Sum>(x,K)\<in>(\<lambda>K. (t K, K)) ` \<F>. content K * indicator S x - integral K (indicator S)\<bar> \<le> e"
  2759           using Henstock_lemma_part1 [of "indicator S::'a\<Rightarrow>real", OF _ \<open>e > 0\<close> \<open>gauge \<gamma>\<close> _ tagged fine]
  2760             indS_int norme by auto
  2761         then show "\<bar>?\<mu> (\<Union>\<F>) - (\<Sum>K\<in>\<F>. ?\<mu> (K \<inter> S))\<bar> \<le> e"
  2762           by (simp add: eq1 eq2 comm_monoid_add_class.sum.reindex inj_on_def sum_subtractf)
  2763       qed
  2764       with 1 show ?thesis by blast
  2765     qed
  2766     have "\<Union>\<D> \<in> lmeasurable \<and> ?\<mu> (\<Union>\<D>) \<le> ?\<mu> S + e"
  2767     proof (cases "finite \<D>")
  2768       case True
  2769       with fincase show ?thesis
  2770         by blast
  2771     next
  2772       case False
  2773       let ?T = "from_nat_into \<D>"
  2774       have T: "bij_betw ?T UNIV \<D>"
  2775         by (simp add: False \<D>(1) bij_betw_from_nat_into)
  2776       have TM: "\<And>n. ?T n \<in> lmeasurable"
  2777         by (metis False cbox finite.emptyI from_nat_into lmeasurable_cbox)
  2778       have TN: "\<And>m n. m \<noteq> n \<Longrightarrow> negligible (?T m \<inter> ?T n)"
  2779         by (simp add: False \<D>(1) from_nat_into infinite_imp_nonempty negl_int)
  2780       have TB: "(\<Sum>k\<le>n. ?\<mu> (?T k)) \<le> ?\<mu> S + e" for n
  2781       proof -
  2782         have "(\<Sum>k\<le>n. ?\<mu> (?T k)) = ?\<mu> (\<Union> (?T ` {..n}))"
  2783           by (simp add: pairwise_def TM TN measure_negligible_finite_Union_image)
  2784         also have "?\<mu> (\<Union> (?T ` {..n})) \<le> ?\<mu> S + e"
  2785           using fincase [of "?T ` {..n}"] T by (auto simp: bij_betw_def)
  2786         finally show ?thesis .
  2787       qed
  2788       have "\<Union>\<D> \<in> lmeasurable"
  2789         by (metis lmeasurable_compact T \<D>(2) bij_betw_def cbox compact_cbox countable_Un_Int(1) fmeasurableD fmeasurableI2 rangeI)
  2790       moreover
  2791       have "?\<mu> (\<Union>x. from_nat_into \<D> x) \<le> ?\<mu> S + e"
  2792       proof (rule measure_countable_Union_le [OF TM])
  2793         show "?\<mu> (\<Union>x\<le>n. from_nat_into \<D> x) \<le> ?\<mu> S + e" for n
  2794           by (metis (mono_tags, lifting) False fincase finite.emptyI finite_atMost finite_imageI from_nat_into imageE subsetI)
  2795       qed
  2796       ultimately show ?thesis by (metis T bij_betw_def)
  2797     qed
  2798     then show "\<Union>\<D> \<in> lmeasurable" "measure lebesgue (\<Union>\<D>) \<le> ?\<mu> S + e" by blast+
  2799   qed (use \<D> cbox djointish close covers in auto)
  2800 qed
  2801 
  2802 
  2803 subsection\<open>Transformation of measure by linear maps\<close>
  2804 
  2805 lemma measurable_linear_image_interval:
  2806    "linear f \<Longrightarrow> f ` (cbox a b) \<in> lmeasurable"
  2807   by (metis bounded_linear_image linear_linear bounded_cbox closure_bounded_linear_image closure_cbox compact_closure lmeasurable_compact)
  2808 
  2809 proposition measure_linear_sufficient:
  2810   fixes f :: "'n::euclidean_space \<Rightarrow> 'n"
  2811   assumes "linear f" and S: "S \<in> lmeasurable"
  2812     and im: "\<And>a b. measure lebesgue (f ` (cbox a b)) = m * measure lebesgue (cbox a b)"
  2813   shows "f ` S \<in> lmeasurable \<and> m * measure lebesgue S = measure lebesgue (f ` S)"
  2814   using le_less_linear [of 0 m]
  2815 proof
  2816   assume "m < 0"
  2817   then show ?thesis
  2818     using im [of 0 One] by auto
  2819 next
  2820   assume "m \<ge> 0"
  2821   let ?\<mu> = "measure lebesgue"
  2822   show ?thesis
  2823   proof (cases "inj f")
  2824     case False
  2825     then have "?\<mu> (f ` S) = 0"
  2826       using \<open>linear f\<close> negligible_imp_measure0 negligible_linear_singular_image by blast
  2827     then have "m * ?\<mu> (cbox 0 (One)) = 0"
  2828       by (metis False \<open>linear f\<close> cbox_borel content_unit im measure_completion negligible_imp_measure0 negligible_linear_singular_image sets_lborel)
  2829     then show ?thesis
  2830       using \<open>linear f\<close> negligible_linear_singular_image negligible_imp_measure0 False
  2831       by (auto simp: lmeasurable_iff_has_integral negligible_UNIV)
  2832   next
  2833     case True
  2834     then obtain h where "linear h" and hf: "\<And>x. h (f x) = x" and fh: "\<And>x. f (h x) = x"
  2835       using \<open>linear f\<close> linear_injective_isomorphism by blast
  2836     have fBS: "(f ` S) \<in> lmeasurable \<and> m * ?\<mu> S = ?\<mu> (f ` S)"
  2837       if "bounded S" "S \<in> lmeasurable" for S
  2838     proof -
  2839       obtain a b where "S \<subseteq> cbox a b"
  2840         using \<open>bounded S\<close> bounded_subset_cbox_symmetric by metis
  2841       have fUD: "(f ` \<Union>\<D>) \<in> lmeasurable \<and> ?\<mu> (f ` \<Union>\<D>) = (m * ?\<mu> (\<Union>\<D>))"
  2842         if "countable \<D>"
  2843           and cbox: "\<And>K. K \<in> \<D> \<Longrightarrow> K \<subseteq> cbox a b \<and> K \<noteq> {} \<and> (\<exists>c d. K = cbox c d)"
  2844           and intint: "pairwise (\<lambda>A B. interior A \<inter> interior B = {}) \<D>"
  2845         for \<D>
  2846       proof -
  2847         have conv: "\<And>K. K \<in> \<D> \<Longrightarrow> convex K"
  2848           using cbox convex_box(1) by blast
  2849         have neg: "negligible (g ` K \<inter> g ` L)" if "linear g" "K \<in> \<D>" "L \<in> \<D>" "K \<noteq> L"
  2850           for K L and g :: "'n\<Rightarrow>'n"
  2851         proof (cases "inj g")
  2852           case True
  2853           have "negligible (frontier(g ` K \<inter> g ` L) \<union> interior(g ` K \<inter> g ` L))"
  2854           proof (rule negligible_Un)
  2855             show "negligible (frontier (g ` K \<inter> g ` L))"
  2856               by (simp add: negligible_convex_frontier convex_Int conv convex_linear_image that)
  2857           next
  2858             have "\<forall>p N. pairwise p N = (\<forall>Na. (Na::'n set) \<in> N \<longrightarrow> (\<forall>Nb. Nb \<in> N \<and> Na \<noteq> Nb \<longrightarrow> p Na Nb))"
  2859               by (metis pairwise_def)
  2860             then have "interior K \<inter> interior L = {}"
  2861               using intint that(2) that(3) that(4) by presburger
  2862             then show "negligible (interior (g ` K \<inter> g ` L))"
  2863               by (metis True empty_imp_negligible image_Int image_empty interior_Int interior_injective_linear_image that(1))
  2864           qed
  2865           moreover have "g ` K \<inter> g ` L \<subseteq> frontier (g ` K \<inter> g ` L) \<union> interior (g ` K \<inter> g ` L)"
  2866             apply (auto simp: frontier_def)
  2867             using closure_subset contra_subsetD by fastforce+
  2868           ultimately show ?thesis
  2869             by (rule negligible_subset)
  2870         next
  2871           case False
  2872           then show ?thesis
  2873             by (simp add: negligible_Int negligible_linear_singular_image \<open>linear g\<close>)
  2874         qed
  2875         have negf: "negligible ((f ` K) \<inter> (f ` L))"
  2876         and negid: "negligible (K \<inter> L)" if "K \<in> \<D>" "L \<in> \<D>" "K \<noteq> L" for K L
  2877           using neg [OF \<open>linear f\<close>] neg [OF linear_id] that by auto
  2878         show ?thesis
  2879         proof (cases "finite \<D>")
  2880           case True
  2881           then have "?\<mu> (\<Union>x\<in>\<D>. f ` x) = (\<Sum>x\<in>\<D>. ?\<mu> (f ` x))"
  2882             using \<open>linear f\<close> cbox measurable_linear_image_interval negf
  2883             by (blast intro: measure_negligible_finite_Union_image [unfolded pairwise_def])
  2884           also have "\<dots> = (\<Sum>k\<in>\<D>. m * ?\<mu> k)"
  2885             by (metis (no_types, lifting) cbox im sum.cong)
  2886           also have "\<dots> = m * ?\<mu> (\<Union>\<D>)"
  2887             unfolding sum_distrib_left [symmetric]
  2888             by (metis True cbox lmeasurable_cbox measure_negligible_finite_Union [unfolded pairwise_def] negid)
  2889           finally show ?thesis
  2890             by (metis True \<open>linear f\<close> cbox image_Union fmeasurable.finite_UN measurable_linear_image_interval)
  2891         next
  2892           case False
  2893           with \<open>countable \<D>\<close> obtain X :: "nat \<Rightarrow> 'n set" where S: "bij_betw X UNIV \<D>"
  2894             using bij_betw_from_nat_into by blast
  2895           then have eq: "(\<Union>\<D>) = (\<Union>n. X n)" "(f ` \<Union>\<D>) = (\<Union>n. f ` X n)"
  2896             by (auto simp: bij_betw_def)
  2897           have meas: "\<And>K. K \<in> \<D> \<Longrightarrow> K \<in> lmeasurable"
  2898             using cbox by blast
  2899           with S have 1: "\<And>n. X n \<in> lmeasurable"
  2900             by (auto simp: bij_betw_def)
  2901           have 2: "pairwise (\<lambda>m n. negligible (X m \<inter> X n)) UNIV"
  2902             using S unfolding bij_betw_def pairwise_def by (metis injD negid range_eqI)
  2903           have "bounded (\<Union>\<D>)"
  2904             by (meson Sup_least bounded_cbox bounded_subset cbox)
  2905           then have 3: "bounded (\<Union>n. X n)"
  2906             using S unfolding bij_betw_def by blast
  2907           have "(\<Union>n. X n) \<in> lmeasurable"
  2908             by (rule measurable_countable_negligible_Union_bounded [OF 1 2 3])
  2909           with S have f1: "\<And>n. f ` (X n) \<in> lmeasurable"
  2910             unfolding bij_betw_def by (metis assms(1) cbox measurable_linear_image_interval rangeI)
  2911           have f2: "pairwise (\<lambda>m n. negligible (f ` (X m) \<inter> f ` (X n))) UNIV"
  2912             using S unfolding bij_betw_def pairwise_def by (metis injD negf rangeI)
  2913           have "bounded (\<Union>\<D>)"
  2914             by (meson Sup_least bounded_cbox bounded_subset cbox)
  2915           then have f3: "bounded (\<Union>n. f ` X n)"
  2916             using S unfolding bij_betw_def
  2917             by (metis bounded_linear_image linear_linear assms(1) image_Union range_composition)
  2918           have "(\<lambda>n. ?\<mu> (X n)) sums ?\<mu> (\<Union>n. X n)"
  2919             by (rule measure_countable_negligible_Union_bounded [OF 1 2 3])
  2920           have meq: "?\<mu> (\<Union>n. f ` X n) = m * ?\<mu> (\<Union>(X ` UNIV))"
  2921           proof (rule sums_unique2 [OF measure_countable_negligible_Union_bounded [OF f1 f2 f3]])
  2922             have m: "\<And>n. ?\<mu> (f ` X n) = (m * ?\<mu> (X n))"
  2923               using S unfolding bij_betw_def by (metis cbox im rangeI)
  2924             show "(\<lambda>n. ?\<mu> (f ` X n)) sums (m * ?\<mu> (\<Union>(X ` UNIV)))"
  2925               unfolding m
  2926               using measure_countable_negligible_Union_bounded [OF 1 2 3] sums_mult by blast
  2927           qed
  2928           show ?thesis
  2929             using measurable_countable_negligible_Union_bounded [OF f1 f2 f3] meq
  2930             by (auto simp: eq [symmetric])
  2931         qed
  2932       qed
  2933       show ?thesis
  2934         unfolding completion.fmeasurable_measure_inner_outer_le
  2935       proof (intro conjI allI impI)
  2936         fix e :: real
  2937         assume "e > 0"
  2938         have 1: "cbox a b - S \<in> lmeasurable"
  2939           by (simp add: fmeasurable.Diff that)
  2940         have 2: "0 < e / (1 + \<bar>m\<bar>)"
  2941           using \<open>e > 0\<close> by (simp add: divide_simps abs_add_one_gt_zero)
  2942         obtain \<D>
  2943           where "countable \<D>"
  2944             and cbox: "\<And>K. K \<in> \<D> \<Longrightarrow> K \<subseteq> cbox a b \<and> K \<noteq> {} \<and> (\<exists>c d. K = cbox c d)"
  2945             and intdisj: "pairwise (\<lambda>A B. interior A \<inter> interior B = {}) \<D>"
  2946             and DD: "cbox a b - S \<subseteq> \<Union>\<D>" "\<Union>\<D> \<in> lmeasurable"
  2947             and le: "?\<mu> (\<Union>\<D>) \<le> ?\<mu> (cbox a b - S) + e/(1 + \<bar>m\<bar>)"
  2948           by (rule measurable_outer_intervals_bounded [of "cbox a b - S" a b "e/(1 + \<bar>m\<bar>)"]; use 1 2 pairwise_def in force)
  2949         have meq: "?\<mu> (cbox a b - S) = ?\<mu> (cbox a b) - ?\<mu> S"
  2950           by (simp add: measurable_measure_Diff \<open>S \<subseteq> cbox a b\<close> fmeasurableD that(2))
  2951         show "\<exists>T \<in> lmeasurable. T \<subseteq> f ` S \<and> m * ?\<mu> S - e \<le> ?\<mu> T"
  2952         proof (intro bexI conjI)
  2953           show "f ` (cbox a b) - f ` (\<Union>\<D>) \<subseteq> f ` S"
  2954             using \<open>cbox a b - S \<subseteq> \<Union>\<D>\<close> by force
  2955           have "m * ?\<mu> S - e \<le> m * (?\<mu> S - e / (1 + \<bar>m\<bar>))"
  2956             using \<open>m \<ge> 0\<close> \<open>e > 0\<close> by (simp add: field_simps)
  2957           also have "\<dots> \<le> ?\<mu> (f ` cbox a b) - ?\<mu> (f ` (\<Union>\<D>))"
  2958             using le \<open>m \<ge> 0\<close> \<open>e > 0\<close>
  2959             apply (simp add: im fUD [OF \<open>countable \<D>\<close> cbox intdisj] right_diff_distrib [symmetric])
  2960             apply (rule mult_left_mono; simp add: algebra_simps meq)
  2961             done
  2962           also have "\<dots> = ?\<mu> (f ` cbox a b - f ` \<Union>\<D>)"
  2963             apply (rule measurable_measure_Diff [symmetric])
  2964             apply (simp add: assms(1) measurable_linear_image_interval)
  2965             apply (simp add: \<open>countable \<D>\<close> cbox fUD fmeasurableD intdisj)
  2966              apply (simp add: Sup_le_iff cbox image_mono)
  2967             done
  2968           finally show "m * ?\<mu> S - e \<le> ?\<mu> (f ` cbox a b - f ` \<Union>\<D>)" .
  2969           show "f ` cbox a b - f ` \<Union>\<D> \<in> lmeasurable"
  2970             by (simp add: fUD \<open>countable \<D>\<close> \<open>linear f\<close> cbox fmeasurable.Diff intdisj measurable_linear_image_interval)
  2971         qed
  2972       next
  2973         fix e :: real
  2974         assume "e > 0"
  2975         have em: "0 < e / (1 + \<bar>m\<bar>)"
  2976           using \<open>e > 0\<close> by (simp add: divide_simps abs_add_one_gt_zero)
  2977         obtain \<D>
  2978           where "countable \<D>"
  2979             and cbox: "\<And>K. K \<in> \<D> \<Longrightarrow> K \<subseteq> cbox a b \<and> K \<noteq> {} \<and> (\<exists>c d. K = cbox c d)"
  2980             and intdisj: "pairwise (\<lambda>A B. interior A \<inter> interior B = {}) \<D>"
  2981             and DD: "S \<subseteq> \<Union>\<D>" "\<Union>\<D> \<in> lmeasurable"
  2982             and le: "?\<mu> (\<Union>\<D>) \<le> ?\<mu> S + e/(1 + \<bar>m\<bar>)"
  2983           by (rule measurable_outer_intervals_bounded [of S a b "e/(1 + \<bar>m\<bar>)"]; use \<open>S \<in> lmeasurable\<close> \<open>S \<subseteq> cbox a b\<close> em in force)
  2984         show "\<exists>U \<in> lmeasurable. f ` S \<subseteq> U \<and> ?\<mu> U \<le> m * ?\<mu> S + e"
  2985         proof (intro bexI conjI)
  2986           show "f ` S \<subseteq> f ` (\<Union>\<D>)"
  2987             by (simp add: DD(1) image_mono)
  2988           have "?\<mu> (f ` \<Union>\<D>) \<le> m * (?\<mu> S + e / (1 + \<bar>m\<bar>))"
  2989             using \<open>m \<ge> 0\<close> le mult_left_mono
  2990             by (auto simp: fUD \<open>countable \<D>\<close> \<open>linear f\<close> cbox fmeasurable.Diff intdisj measurable_linear_image_interval)
  2991           also have "\<dots> \<le> m * ?\<mu> S + e"
  2992             using \<open>m \<ge> 0\<close> \<open>e > 0\<close> by (simp add: fUD [OF \<open>countable \<D>\<close> cbox intdisj] field_simps)
  2993           finally show "?\<mu> (f ` \<Union>\<D>) \<le> m * ?\<mu> S + e" .
  2994           show "f ` \<Union>\<D> \<in> lmeasurable"
  2995             by (simp add: \<open>countable \<D>\<close> cbox fUD intdisj)
  2996         qed
  2997       qed
  2998     qed
  2999     show ?thesis
  3000       unfolding has_measure_limit_iff
  3001     proof (intro allI impI)
  3002       fix e :: real
  3003       assume "e > 0"
  3004       obtain B where "B > 0" and B:
  3005         "\<And>a b. ball 0 B \<subseteq> cbox a b \<Longrightarrow> \<bar>?\<mu> (S \<inter> cbox a b) - ?\<mu> S\<bar> < e / (1 + \<bar>m\<bar>)"
  3006         using has_measure_limit [OF S] \<open>e > 0\<close> by (metis abs_add_one_gt_zero zero_less_divide_iff)
  3007       obtain c d::'n where cd: "ball 0 B \<subseteq> cbox c d"
  3008         by (metis bounded_subset_cbox_symmetric bounded_ball)
  3009       with B have less: "\<bar>?\<mu> (S \<inter> cbox c d) - ?\<mu> S\<bar> < e / (1 + \<bar>m\<bar>)" .
  3010       obtain D where "D > 0" and D: "cbox c d \<subseteq> ball 0 D"
  3011         by (metis bounded_cbox bounded_subset_ballD)
  3012       obtain C where "C > 0" and C: "\<And>x. norm (f x) \<le> C * norm x"
  3013         using linear_bounded_pos \<open>linear f\<close> by blast
  3014       have "f ` S \<inter> cbox a b \<in> lmeasurable \<and>
  3015             \<bar>?\<mu> (f ` S \<inter> cbox a b) - m * ?\<mu> S\<bar> < e"
  3016         if "ball 0 (D*C) \<subseteq> cbox a b" for a b
  3017       proof -
  3018         have "bounded (S \<inter> h ` cbox a b)"
  3019           by (simp add: bounded_linear_image linear_linear \<open>linear h\<close> bounded_Int)
  3020         moreover have Shab: "S \<inter> h ` cbox a b \<in> lmeasurable"
  3021           by (simp add: S \<open>linear h\<close> fmeasurable.Int measurable_linear_image_interval)
  3022         moreover have fim: "f ` (S \<inter> h ` (cbox a b)) = (f ` S) \<inter> cbox a b"
  3023           by (auto simp: hf rev_image_eqI fh)
  3024         ultimately have 1: "(f ` S) \<inter> cbox a b \<in> lmeasurable"
  3025               and 2: "m * ?\<mu> (S \<inter> h ` cbox a b) = ?\<mu> ((f ` S) \<inter> cbox a b)"
  3026           using fBS [of "S \<inter> (h ` (cbox a b))"] by auto
  3027         have *: "\<lbrakk>\<bar>z - m\<bar> < e; z \<le> w; w \<le> m\<rbrakk> \<Longrightarrow> \<bar>w - m\<bar> \<le> e"
  3028           for w z m and e::real by auto
  3029         have meas_adiff: "\<bar>?\<mu> (S \<inter> h ` cbox a b) - ?\<mu> S\<bar> \<le> e / (1 + \<bar>m\<bar>)"
  3030         proof (rule * [OF less])
  3031           show "?\<mu> (S \<inter> cbox c d) \<le> ?\<mu> (S \<inter> h ` cbox a b)"
  3032           proof (rule measure_mono_fmeasurable [OF _ _ Shab])
  3033             have "f ` ball 0 D \<subseteq> ball 0 (C * D)"
  3034               using C \<open>C > 0\<close>
  3035               apply (clarsimp simp: algebra_simps)
  3036               by (meson le_less_trans linordered_comm_semiring_strict_class.comm_mult_strict_left_mono)
  3037             then have "f ` ball 0 D \<subseteq> cbox a b"
  3038               by (metis mult.commute order_trans that)
  3039             have "ball 0 D \<subseteq> h ` cbox a b"
  3040               by (metis \<open>f ` ball 0 D \<subseteq> cbox a b\<close> hf image_subset_iff subsetI)
  3041             then show "S \<inter> cbox c d \<subseteq> S \<inter> h ` cbox a b"
  3042               using D by blast
  3043           next
  3044             show "S \<inter> cbox c d \<in> sets lebesgue"
  3045               using S fmeasurable_cbox by blast
  3046           qed
  3047         next
  3048           show "?\<mu> (S \<inter> h ` cbox a b) \<le> ?\<mu> S"
  3049             by (simp add: S Shab fmeasurableD measure_mono_fmeasurable)
  3050         qed
  3051         have "\<bar>?\<mu> (f ` S \<inter> cbox a b) - m * ?\<mu> S\<bar> \<le> m * e / (1 + \<bar>m\<bar>)"
  3052         proof -
  3053           have mm: "\<bar>m\<bar> = m"
  3054             by (simp add: \<open>0 \<le> m\<close>)
  3055           then have "\<bar>?\<mu> S - ?\<mu> (S \<inter> h ` cbox a b)\<bar> * m \<le> e / (1 + m) * m"
  3056             by (metis (no_types) \<open>0 \<le> m\<close> meas_adiff abs_minus_commute mult_right_mono)
  3057           moreover have "\<forall>r. \<bar>r * m\<bar> = m * \<bar>r\<bar>"
  3058             by (metis \<open>0 \<le> m\<close> abs_mult_pos mult.commute)
  3059           ultimately show ?thesis
  3060             apply (simp add: 2 [symmetric])
  3061             by (metis (no_types) abs_minus_commute mult.commute right_diff_distrib' mm)
  3062         qed
  3063         also have "\<dots> < e"
  3064           using \<open>e > 0\<close> by (auto simp: divide_simps)
  3065         finally have "\<bar>?\<mu> (f ` S \<inter> cbox a b) - m * ?\<mu> S\<bar> < e" .
  3066         with 1 show ?thesis by auto
  3067       qed
  3068       then show "\<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
  3069                          f ` S \<inter> cbox a b \<in> lmeasurable \<and>
  3070                          \<bar>?\<mu> (f ` S \<inter> cbox a b) - m * ?\<mu> S\<bar> < e"
  3071         using \<open>C>0\<close> \<open>D>0\<close> by (metis mult_zero_left real_mult_less_iff1)
  3072     qed
  3073   qed
  3074 qed
  3075 
  3076 
  3077 subsection\<open>Lemmas about absolute integrability\<close>
  3078 
  3079 text\<open>FIXME Redundant!\<close>
  3080 lemma absolutely_integrable_add[intro]:
  3081   fixes f g :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
  3082   shows "f absolutely_integrable_on s \<Longrightarrow> g absolutely_integrable_on s \<Longrightarrow> (\<lambda>x. f x + g x) absolutely_integrable_on s"
  3083   by (rule set_integral_add)
  3084 
  3085 text\<open>FIXME Redundant!\<close>
  3086 lemma absolutely_integrable_diff[intro]:
  3087   fixes f g :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
  3088   shows "f absolutely_integrable_on s \<Longrightarrow> g absolutely_integrable_on s \<Longrightarrow> (\<lambda>x. f x - g x) absolutely_integrable_on s"
  3089   by (rule set_integral_diff)
  3090 
  3091 lemma absolutely_integrable_linear:
  3092   fixes f :: "'m::euclidean_space \<Rightarrow> 'n::euclidean_space"
  3093     and h :: "'n::euclidean_space \<Rightarrow> 'p::euclidean_space"
  3094   shows "f absolutely_integrable_on s \<Longrightarrow> bounded_linear h \<Longrightarrow> (h \<circ> f) absolutely_integrable_on s"
  3095   using integrable_bounded_linear[of h lebesgue "\<lambda>x. indicator s x *\<^sub>R f x"]
  3096   by (simp add: linear_simps[of h] set_integrable_def)
  3097 
  3098 lemma absolutely_integrable_sum:
  3099   fixes f :: "'a \<Rightarrow> 'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
  3100   assumes "finite T" and "\<And>a. a \<in> T \<Longrightarrow> (f a) absolutely_integrable_on S"
  3101   shows "(\<lambda>x. sum (\<lambda>a. f a x) T) absolutely_integrable_on S"
  3102   using assms by induction auto
  3103 
  3104 lemma absolutely_integrable_integrable_bound:
  3105   fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
  3106   assumes le: "\<And>x. x\<in>S \<Longrightarrow> norm (f x) \<le> g x" and f: "f integrable_on S" and g: "g integrable_on S"
  3107   shows "f absolutely_integrable_on S"
  3108     unfolding set_integrable_def
  3109 proof (rule Bochner_Integration.integrable_bound)
  3110   have "g absolutely_integrable_on S"
  3111     unfolding absolutely_integrable_on_def
  3112   proof
  3113     show "(\<lambda>x. norm (g x)) integrable_on S"
  3114       using le norm_ge_zero[of "f _"]
  3115       by (intro integrable_spike_finite[OF _ _ g, of "{}"])
  3116          (auto intro!: abs_of_nonneg intro: order_trans simp del: norm_ge_zero)
  3117   qed fact
  3118   then show "integrable lebesgue (\<lambda>x. indicat_real S x *\<^sub>R g x)"
  3119     by (simp add: set_integrable_def)
  3120   show "(\<lambda>x. indicat_real S x *\<^sub>R f x) \<in> borel_measurable lebesgue"
  3121     using f by (auto intro: has_integral_implies_lebesgue_measurable simp: integrable_on_def)
  3122 qed (use le in \<open>force intro!: always_eventually split: split_indicator\<close>)
  3123 
  3124 lemma absolutely_integrable_continuous:
  3125   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  3126   shows "continuous_on (cbox a b) f \<Longrightarrow> f absolutely_integrable_on cbox a b"
  3127   using absolutely_integrable_integrable_bound
  3128   by (simp add: absolutely_integrable_on_def continuous_on_norm integrable_continuous)
  3129 
  3130 
  3131 subsection \<open>Componentwise\<close>
  3132 
  3133 proposition absolutely_integrable_componentwise_iff:
  3134   shows "f absolutely_integrable_on A \<longleftrightarrow> (\<forall>b\<in>Basis. (\<lambda>x. f x \<bullet> b) absolutely_integrable_on A)"
  3135 proof -
  3136   have *: "(\<lambda>x. norm (f x)) integrable_on A \<longleftrightarrow> (\<forall>b\<in>Basis. (\<lambda>x. norm (f x \<bullet> b)) integrable_on A)"
  3137           if "f integrable_on A"
  3138   proof -
  3139     have 1: "\<And>i. \<lbrakk>(\<lambda>x. norm (f x)) integrable_on A; i \<in> Basis\<rbrakk>
  3140                  \<Longrightarrow> (\<lambda>x. f x \<bullet> i) absolutely_integrable_on A"
  3141       apply (rule absolutely_integrable_integrable_bound [where g = "\<lambda>x. norm(f x)"])
  3142       using Basis_le_norm integrable_component that apply fastforce+
  3143       done
  3144     have 2: "\<forall>i\<in>Basis. (\<lambda>x. \<bar>f x \<bullet> i\<bar>) integrable_on A \<Longrightarrow> f absolutely_integrable_on A"
  3145       apply (rule absolutely_integrable_integrable_bound [where g = "\<lambda>x. \<Sum>i\<in>Basis. norm (f x \<bullet> i)"])
  3146       using norm_le_l1 that apply (force intro: integrable_sum)+
  3147       done
  3148     show ?thesis
  3149       apply auto
  3150        apply (metis (full_types) absolutely_integrable_on_def set_integrable_abs 1)
  3151       apply (metis (full_types) absolutely_integrable_on_def 2)
  3152       done
  3153   qed
  3154   show ?thesis
  3155     unfolding absolutely_integrable_on_def
  3156     by (simp add:  integrable_componentwise_iff [symmetric] ball_conj_distrib * cong: conj_cong)
  3157 qed
  3158 
  3159 lemma absolutely_integrable_componentwise:
  3160   shows "(\<And>b. b \<in> Basis \<Longrightarrow> (\<lambda>x. f x \<bullet> b) absolutely_integrable_on A) \<Longrightarrow> f absolutely_integrable_on A"
  3161   using absolutely_integrable_componentwise_iff by blast
  3162 
  3163 lemma absolutely_integrable_component:
  3164   "f absolutely_integrable_on A \<Longrightarrow> (\<lambda>x. f x \<bullet> (b :: 'b :: euclidean_space)) absolutely_integrable_on A"
  3165   by (drule absolutely_integrable_linear[OF _ bounded_linear_inner_left[of b]]) (simp add: o_def)
  3166 
  3167 
  3168 lemma absolutely_integrable_scaleR_left:
  3169   fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
  3170     assumes "f absolutely_integrable_on S"
  3171   shows "(\<lambda>x. c *\<^sub>R f x) absolutely_integrable_on S"
  3172 proof -
  3173   have "(\<lambda>x. c *\<^sub>R x) o f absolutely_integrable_on S"
  3174     apply (rule absolutely_integrable_linear [OF assms])
  3175     by (simp add: bounded_linear_scaleR_right)
  3176   then show ?thesis
  3177     using assms by blast
  3178 qed
  3179 
  3180 lemma absolutely_integrable_scaleR_right:
  3181   assumes "f absolutely_integrable_on S"
  3182   shows "(\<lambda>x. f x *\<^sub>R c) absolutely_integrable_on S"
  3183   using assms by blast
  3184 
  3185 lemma absolutely_integrable_norm:
  3186   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  3187   assumes "f absolutely_integrable_on S"
  3188   shows "(norm o f) absolutely_integrable_on S"
  3189   using assms by (simp add: absolutely_integrable_on_def o_def)
  3190 
  3191 lemma absolutely_integrable_abs:
  3192   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  3193   assumes "f absolutely_integrable_on S"
  3194   shows "(\<lambda>x. \<Sum>i\<in>Basis. \<bar>f x \<bullet> i\<bar> *\<^sub>R i) absolutely_integrable_on S"
  3195         (is "?g absolutely_integrable_on S")
  3196 proof -
  3197   have eq: "?g =
  3198         (\<lambda>x. \<Sum>i\<in>Basis. ((\<lambda>y. \<Sum>j\<in>Basis. if j = i then y *\<^sub>R j else 0) \<circ>
  3199                (\<lambda>x. norm(\<Sum>j\<in>Basis. if j = i then (x \<bullet> i) *\<^sub>R j else 0)) \<circ> f) x)"
  3200     by (simp add: sum.delta)
  3201   have *: "(\<lambda>y. \<Sum>j\<in>Basis. if j = i then y *\<^sub>R j else 0) \<circ>
  3202            (\<lambda>x. norm (\<Sum>j\<in>Basis. if j = i then (x \<bullet> i) *\<^sub>R j else 0)) \<circ> f 
  3203            absolutely_integrable_on S" 
  3204         if "i \<in> Basis" for i
  3205   proof -
  3206     have "bounded_linear (\<lambda>y. \<Sum>j\<in>Basis. if j = i then y *\<^sub>R j else 0)"
  3207       by (simp add: linear_linear algebra_simps linearI)
  3208     moreover have "(\<lambda>x. norm (\<Sum>j\<in>Basis. if j = i then (x \<bullet> i) *\<^sub>R j else 0)) \<circ> f 
  3209                    absolutely_integrable_on S"
  3210       unfolding o_def
  3211       apply (rule absolutely_integrable_norm [unfolded o_def])
  3212       using assms \<open>i \<in> Basis\<close>
  3213       apply (auto simp: algebra_simps dest: absolutely_integrable_component[where b=i])
  3214       done
  3215     ultimately show ?thesis
  3216       by (subst comp_assoc) (blast intro: absolutely_integrable_linear)
  3217   qed
  3218   show ?thesis
  3219     apply (rule ssubst [OF eq])
  3220     apply (rule absolutely_integrable_sum)
  3221      apply (force simp: intro!: *)+
  3222     done
  3223 qed
  3224 
  3225 lemma abs_absolutely_integrableI_1:
  3226   fixes f :: "'a :: euclidean_space \<Rightarrow> real"
  3227   assumes f: "f integrable_on A" and "(\<lambda>x. \<bar>f x\<bar>) integrable_on A"
  3228   shows "f absolutely_integrable_on A"
  3229   by (rule absolutely_integrable_integrable_bound [OF _ assms]) auto
  3230 
  3231   
  3232 lemma abs_absolutely_integrableI:
  3233   assumes f: "f integrable_on S" and fcomp: "(\<lambda>x. \<Sum>i\<in>Basis. \<bar>f x \<bullet> i\<bar> *\<^sub>R i) integrable_on S"
  3234   shows "f absolutely_integrable_on S"
  3235 proof -
  3236   have "(\<lambda>x. (f x \<bullet> i) *\<^sub>R i) absolutely_integrable_on S" if "i \<in> Basis" for i
  3237   proof -
  3238     have "(\<lambda>x. \<bar>f x \<bullet> i\<bar>) integrable_on S" 
  3239       using assms integrable_component [OF fcomp, where y=i] that by simp
  3240     then have "(\<lambda>x. f x \<bullet> i) absolutely_integrable_on S"
  3241       using abs_absolutely_integrableI_1 f integrable_component by blast
  3242     then show ?thesis
  3243       by (rule absolutely_integrable_scaleR_right)
  3244   qed
  3245   then have "(\<lambda>x. \<Sum>i\<in>Basis. (f x \<bullet> i) *\<^sub>R i) absolutely_integrable_on S"
  3246     by (simp add: absolutely_integrable_sum)
  3247   then show ?thesis
  3248     by (simp add: euclidean_representation)
  3249 qed
  3250 
  3251     
  3252 lemma absolutely_integrable_abs_iff:
  3253    "f absolutely_integrable_on S \<longleftrightarrow>
  3254     f integrable_on S \<and> (\<lambda>x. \<Sum>i\<in>Basis. \<bar>f x \<bullet> i\<bar> *\<^sub>R i) integrable_on S"
  3255     (is "?lhs = ?rhs")
  3256 proof
  3257   assume ?lhs then show ?rhs
  3258     using absolutely_integrable_abs absolutely_integrable_on_def by blast
  3259 next
  3260   assume ?rhs 
  3261   moreover
  3262   have "(\<lambda>x. if x \<in> S then \<Sum>i\<in>Basis. \<bar>f x \<bullet> i\<bar> *\<^sub>R i else 0) = (\<lambda>x. \<Sum>i\<in>Basis. \<bar>(if x \<in> S then f x else 0) \<bullet> i\<bar> *\<^sub>R i)"
  3263     by force
  3264   ultimately show ?lhs
  3265     by (simp only: absolutely_integrable_restrict_UNIV [of S, symmetric] integrable_restrict_UNIV [of S, symmetric] abs_absolutely_integrableI)
  3266 qed
  3267 
  3268 lemma absolutely_integrable_max:
  3269   fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
  3270   assumes "f absolutely_integrable_on S" "g absolutely_integrable_on S"
  3271    shows "(\<lambda>x. \<Sum>i\<in>Basis. max (f x \<bullet> i) (g x \<bullet> i) *\<^sub>R i)
  3272             absolutely_integrable_on S"
  3273 proof -
  3274   have "(\<lambda>x. \<Sum>i\<in>Basis. max (f x \<bullet> i) (g x \<bullet> i) *\<^sub>R i) = 
  3275         (\<lambda>x. (1/2) *\<^sub>R (f x + g x + (\<Sum>i\<in>Basis. \<bar>f x \<bullet> i - g x \<bullet> i\<bar> *\<^sub>R i)))"
  3276   proof (rule ext)
  3277     fix x
  3278     have "(\<Sum>i\<in>Basis. max (f x \<bullet> i) (g x \<bullet> i) *\<^sub>R i) = (\<Sum>i\<in>Basis. ((f x \<bullet> i + g x \<bullet> i + \<bar>f x \<bullet> i - g x \<bullet> i\<bar>) / 2) *\<^sub>R i)"
  3279       by (force intro: sum.cong)
  3280     also have "... = (1 / 2) *\<^sub>R (\<Sum>i\<in>Basis. (f x \<bullet> i + g x \<bullet> i + \<bar>f x \<bullet> i - g x \<bullet> i\<bar>) *\<^sub>R i)"
  3281       by (simp add: scaleR_right.sum)
  3282     also have "... = (1 / 2) *\<^sub>R (f x + g x + (\<Sum>i\<in>Basis. \<bar>f x \<bullet> i - g x \<bullet> i\<bar> *\<^sub>R i))"
  3283       by (simp add: sum.distrib algebra_simps euclidean_representation)
  3284     finally
  3285     show "(\<Sum>i\<in>Basis. max (f x \<bullet> i) (g x \<bullet> i) *\<^sub>R i) =
  3286          (1 / 2) *\<^sub>R (f x + g x + (\<Sum>i\<in>Basis. \<bar>f x \<bullet> i - g x \<bullet> i\<bar> *\<^sub>R i))" .
  3287   qed
  3288   moreover have "(\<lambda>x. (1 / 2) *\<^sub>R (f x + g x + (\<Sum>i\<in>Basis. \<bar>f x \<bullet> i - g x \<bullet> i\<bar> *\<^sub>R i))) 
  3289                  absolutely_integrable_on S"
  3290     apply (intro absolutely_integrable_add absolutely_integrable_scaleR_left assms)
  3291     using absolutely_integrable_abs [OF absolutely_integrable_diff [OF assms]]
  3292     apply (simp add: algebra_simps)
  3293     done
  3294   ultimately show ?thesis by metis
  3295 qed
  3296   
  3297 corollary absolutely_integrable_max_1:
  3298   fixes f :: "'n::euclidean_space \<Rightarrow> real"
  3299   assumes "f absolutely_integrable_on S" "g absolutely_integrable_on S"
  3300    shows "(\<lambda>x. max (f x) (g x)) absolutely_integrable_on S"
  3301   using absolutely_integrable_max [OF assms] by simp
  3302 
  3303 lemma absolutely_integrable_min:
  3304   fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
  3305   assumes "f absolutely_integrable_on S" "g absolutely_integrable_on S"
  3306    shows "(\<lambda>x. \<Sum>i\<in>Basis. min (f x \<bullet> i) (g x \<bullet> i) *\<^sub>R i)
  3307             absolutely_integrable_on S"
  3308 proof -
  3309   have "(\<lambda>x. \<Sum>i\<in>Basis. min (f x \<bullet> i) (g x \<bullet> i) *\<^sub>R i) = 
  3310         (\<lambda>x. (1/2) *\<^sub>R (f x + g x - (\<Sum>i\<in>Basis. \<bar>f x \<bullet> i - g x \<bullet> i\<bar> *\<^sub>R i)))"
  3311   proof (rule ext)
  3312     fix x
  3313     have "(\<Sum>i\<in>Basis. min (f x \<bullet> i) (g x \<bullet> i) *\<^sub>R i) = (\<Sum>i\<in>Basis. ((f x \<bullet> i + g x \<bullet> i - \<bar>f x \<bullet> i - g x \<bullet> i\<bar>) / 2) *\<^sub>R i)"
  3314       by (force intro: sum.cong)
  3315     also have "... = (1 / 2) *\<^sub>R (\<Sum>i\<in>Basis. (f x \<bullet> i + g x \<bullet> i - \<bar>f x \<bullet> i - g x \<bullet> i\<bar>) *\<^sub>R i)"
  3316       by (simp add: scaleR_right.sum)
  3317     also have "... = (1 / 2) *\<^sub>R (f x + g x - (\<Sum>i\<in>Basis. \<bar>f x \<bullet> i - g x \<bullet> i\<bar> *\<^sub>R i))"
  3318       by (simp add: sum.distrib sum_subtractf algebra_simps euclidean_representation)
  3319     finally
  3320     show "(\<Sum>i\<in>Basis. min (f x \<bullet> i) (g x \<bullet> i) *\<^sub>R i) =
  3321          (1 / 2) *\<^sub>R (f x + g x - (\<Sum>i\<in>Basis. \<bar>f x \<bullet> i - g x \<bullet> i\<bar> *\<^sub>R i))" .
  3322   qed
  3323   moreover have "(\<lambda>x. (1 / 2) *\<^sub>R (f x + g x - (\<Sum>i\<in>Basis. \<bar>f x \<bullet> i - g x \<bullet> i\<bar> *\<^sub>R i))) 
  3324                  absolutely_integrable_on S"
  3325     apply (intro absolutely_integrable_add absolutely_integrable_diff absolutely_integrable_scaleR_left assms)
  3326     using absolutely_integrable_abs [OF absolutely_integrable_diff [OF assms]]
  3327     apply (simp add: algebra_simps)
  3328     done
  3329   ultimately show ?thesis by metis
  3330 qed
  3331   
  3332 corollary absolutely_integrable_min_1:
  3333   fixes f :: "'n::euclidean_space \<Rightarrow> real"
  3334   assumes "f absolutely_integrable_on S" "g absolutely_integrable_on S"
  3335    shows "(\<lambda>x. min (f x) (g x)) absolutely_integrable_on S"
  3336   using absolutely_integrable_min [OF assms] by simp
  3337 
  3338 lemma nonnegative_absolutely_integrable:
  3339   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  3340   assumes "f integrable_on A" and comp: "\<And>x b. \<lbrakk>x \<in> A; b \<in> Basis\<rbrakk> \<Longrightarrow> 0 \<le> f x \<bullet> b"
  3341   shows "f absolutely_integrable_on A"
  3342 proof -
  3343   have "(\<lambda>x. (f x \<bullet> i) *\<^sub>R i) absolutely_integrable_on A" if "i \<in> Basis" for i
  3344   proof -
  3345     have "(\<lambda>x. f x \<bullet> i) integrable_on A" 
  3346       by (simp add: assms(1) integrable_component)
  3347     then have "(\<lambda>x. f x \<bullet> i) absolutely_integrable_on A"
  3348       by (metis that comp nonnegative_absolutely_integrable_1)
  3349     then show ?thesis
  3350       by (rule absolutely_integrable_scaleR_right)
  3351   qed
  3352   then have "(\<lambda>x. \<Sum>i\<in>Basis. (f x \<bullet> i) *\<^sub>R i) absolutely_integrable_on A"
  3353     by (simp add: absolutely_integrable_sum)
  3354   then show ?thesis
  3355     by (simp add: euclidean_representation)
  3356 qed
  3357   
  3358 lemma absolutely_integrable_component_ubound:
  3359   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  3360   assumes f: "f integrable_on A" and g: "g absolutely_integrable_on A"
  3361       and comp: "\<And>x b. \<lbrakk>x \<in> A; b \<in> Basis\<rbrakk> \<Longrightarrow> f x \<bullet> b \<le> g x \<bullet> b"
  3362   shows "f absolutely_integrable_on A"
  3363 proof -
  3364   have "(\<lambda>x. g x - (g x - f x)) absolutely_integrable_on A"
  3365     apply (rule absolutely_integrable_diff [OF g nonnegative_absolutely_integrable])
  3366     using Henstock_Kurzweil_Integration.integrable_diff absolutely_integrable_on_def f g apply blast
  3367     by (simp add: comp inner_diff_left)
  3368   then show ?thesis
  3369     by simp
  3370 qed
  3371 
  3372 lemma absolutely_integrable_component_lbound:
  3373   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  3374   assumes f: "f absolutely_integrable_on A" and g: "g integrable_on A"
  3375       and comp: "\<And>x b. \<lbrakk>x \<in> A; b \<in> Basis\<rbrakk> \<Longrightarrow> f x \<bullet> b \<le> g x \<bullet> b"
  3376   shows "g absolutely_integrable_on A"
  3377 proof -
  3378   have "(\<lambda>x. f x + (g x - f x)) absolutely_integrable_on A"
  3379     apply (rule absolutely_integrable_add [OF f nonnegative_absolutely_integrable])
  3380     using Henstock_Kurzweil_Integration.integrable_diff absolutely_integrable_on_def f g apply blast
  3381     by (simp add: comp inner_diff_left)
  3382   then show ?thesis
  3383     by simp
  3384 qed
  3385 
  3386 lemma integrable_on_1_iff:
  3387   fixes f :: "'a::euclidean_space \<Rightarrow> real^1"
  3388   shows "f integrable_on S \<longleftrightarrow> (\<lambda>x. f x $ 1) integrable_on S"
  3389   by (auto simp: integrable_componentwise_iff [of f] Basis_vec_def cart_eq_inner_axis)
  3390 
  3391 lemma integral_on_1_eq:
  3392   fixes f :: "'a::euclidean_space \<Rightarrow> real^1"
  3393   shows "integral S f = vec (integral S (\<lambda>x. f x $ 1))"
  3394 by (cases "f integrable_on S") (simp_all add: integrable_on_1_iff vec_eq_iff not_integrable_integral)
  3395 
  3396 lemma absolutely_integrable_on_1_iff:
  3397   fixes f :: "'a::euclidean_space \<Rightarrow> real^1"
  3398   shows "f absolutely_integrable_on S \<longleftrightarrow> (\<lambda>x. f x $ 1) absolutely_integrable_on S"
  3399   unfolding absolutely_integrable_on_def
  3400   by (auto simp: integrable_on_1_iff norm_real)
  3401 
  3402 lemma absolutely_integrable_absolutely_integrable_lbound:
  3403   fixes f :: "'m::euclidean_space \<Rightarrow> real"
  3404   assumes f: "f integrable_on S" and g: "g absolutely_integrable_on S"
  3405     and *: "\<And>x. x \<in> S \<Longrightarrow> g x \<le> f x"
  3406   shows "f absolutely_integrable_on S"
  3407   by (rule absolutely_integrable_component_lbound [OF g f]) (simp add: *)
  3408 
  3409 lemma absolutely_integrable_absolutely_integrable_ubound:
  3410   fixes f :: "'m::euclidean_space \<Rightarrow> real"
  3411   assumes fg: "f integrable_on S" "g absolutely_integrable_on S"
  3412     and *: "\<And>x. x \<in> S \<Longrightarrow> f x \<le> g x"
  3413   shows "f absolutely_integrable_on S"
  3414   by (rule absolutely_integrable_component_ubound [OF fg]) (simp add: *)
  3415 
  3416 lemma has_integral_vec1_I_cbox:
  3417   fixes f :: "real^1 \<Rightarrow> 'a::real_normed_vector"
  3418   assumes "(f has_integral y) (cbox a b)"
  3419   shows "((f \<circ> vec) has_integral y) {a$1..b$1}"
  3420 proof -
  3421   have "((\<lambda>x. f(vec x)) has_integral (1 / 1) *\<^sub>R y) ((\<lambda>x. x $ 1) ` cbox a b)"
  3422   proof (rule has_integral_twiddle)
  3423     show "\<exists>w z::real^1. vec ` cbox u v = cbox w z"
  3424          "content (vec ` cbox u v :: (real^1) set) = 1 * content (cbox u v)" for u v
  3425       unfolding vec_cbox_1_eq
  3426       by (auto simp: content_cbox_if_cart interval_eq_empty_cart)
  3427     show "\<exists>w z. (\<lambda>x. x $ 1) ` cbox u v = cbox w z" for u v :: "real^1"
  3428       using vec_nth_cbox_1_eq by blast
  3429   qed (auto simp: continuous_vec assms)
  3430   then show ?thesis
  3431     by (simp add: o_def)
  3432 qed
  3433 
  3434 lemma has_integral_vec1_I:
  3435   fixes f :: "real^1 \<Rightarrow> 'a::real_normed_vector"
  3436   assumes "(f has_integral y) S"
  3437   shows "(f \<circ> vec has_integral y) ((\<lambda>x. x $ 1) ` S)"
  3438 proof -
  3439   have *: "\<exists>z. ((\<lambda>x. if x \<in> (\<lambda>x. x $ 1) ` S then (f \<circ> vec) x else 0) has_integral z) {a..b} \<and> norm (z - y) < e"
  3440     if int: "\<And>a b. ball 0 B \<subseteq> cbox a b \<Longrightarrow>
  3441                     (\<exists>z. ((\<lambda>x. if x \<in> S then f x else 0) has_integral z) (cbox a b) \<and> norm (z - y) < e)"
  3442       and B: "ball 0 B \<subseteq> {a..b}" for e B a b
  3443   proof -
  3444     have [simp]: "(\<exists>y\<in>S. x = y $ 1) \<longleftrightarrow> vec x \<in> S" for x
  3445       by force
  3446     have B': "ball (0::real^1) B \<subseteq> cbox (vec a) (vec b)"
  3447       using B by (simp add: Basis_vec_def cart_eq_inner_axis [symmetric] mem_box norm_real subset_iff)
  3448     show ?thesis
  3449       using int [OF B'] by (auto simp: image_iff o_def cong: if_cong dest!: has_integral_vec1_I_cbox)
  3450   qed
  3451   show ?thesis
  3452     using assms 
  3453     apply (subst has_integral_alt)
  3454     apply (subst (asm) has_integral_alt)
  3455     apply (simp add: has_integral_vec1_I_cbox split: if_split_asm)
  3456     apply (metis vector_one_nth)
  3457     apply (erule all_forward imp_forward asm_rl ex_forward conj_forward)+
  3458     apply (blast intro!: *)
  3459     done
  3460 qed
  3461 
  3462 lemma has_integral_vec1_nth_cbox:
  3463   fixes f :: "real \<Rightarrow> 'a::real_normed_vector"
  3464   assumes "(f has_integral y) {a..b}"
  3465   shows "((\<lambda>x::real^1. f(x$1)) has_integral y) (cbox (vec a) (vec b))"
  3466 proof -
  3467   have "((\<lambda>x::real^1. f(x$1)) has_integral (1 / 1) *\<^sub>R y) (vec ` cbox a b)"
  3468   proof (rule has_integral_twiddle)
  3469     show "\<exists>w z::real. (\<lambda>x. x $ 1) ` cbox u v = cbox w z"
  3470          "content ((\<lambda>x. x $ 1) ` cbox u v) = 1 * content (cbox u v)" for u v::"real^1"
  3471       unfolding vec_cbox_1_eq by (auto simp: content_cbox_if_cart interval_eq_empty_cart)
  3472     show "\<exists>w z::real^1. vec ` cbox u v = cbox w z" for u v :: "real"
  3473       using vec_cbox_1_eq by auto
  3474   qed (auto simp: continuous_vec assms)
  3475   then show ?thesis
  3476     using vec_cbox_1_eq by auto
  3477 qed
  3478 
  3479 lemma has_integral_vec1_D_cbox:
  3480   fixes f :: "real^1 \<Rightarrow> 'a::real_normed_vector"
  3481   assumes "((f \<circ> vec) has_integral y) {a$1..b$1}"
  3482   shows "(f has_integral y) (cbox a b)"
  3483   by (metis (mono_tags, lifting) assms comp_apply has_integral_eq has_integral_vec1_nth_cbox vector_one_nth)
  3484 
  3485 lemma has_integral_vec1_D:
  3486   fixes f :: "real^1 \<Rightarrow> 'a::real_normed_vector"
  3487   assumes "((f \<circ> vec) has_integral y) ((\<lambda>x. x $ 1) ` S)"
  3488   shows "(f has_integral y) S"
  3489 proof -
  3490   have *: "\<exists>z. ((\<lambda>x. if x \<in> S then f x else 0) has_integral z) (cbox a b) \<and> norm (z - y) < e"
  3491     if int: "\<And>a b. ball 0 B \<subseteq> {a..b} \<Longrightarrow>
  3492                              (\<exists>z. ((\<lambda>x. if x \<in> (\<lambda>x. x $ 1) ` S then (f \<circ> vec) x else 0) has_integral z) {a..b} \<and> norm (z - y) < e)"
  3493       and B: "ball 0 B \<subseteq> cbox a b" for e B and a b::"real^1"
  3494   proof -
  3495     have B': "ball 0 B \<subseteq> {a$1..b$1}"
  3496       using B
  3497       apply (simp add: subset_iff Basis_vec_def cart_eq_inner_axis [symmetric] mem_box)
  3498       apply (metis (full_types) norm_real vec_component)
  3499       done
  3500     have eq: "(\<lambda>x. if vec x \<in> S then f (vec x) else 0) = (\<lambda>x. if x \<in> S then f x else 0) \<circ> vec"
  3501       by force
  3502     have [simp]: "(\<exists>y\<in>S. x = y $ 1) \<longleftrightarrow> vec x \<in> S" for x
  3503       by force
  3504     show ?thesis
  3505       using int [OF B'] by (auto simp: image_iff eq cong: if_cong dest!: has_integral_vec1_D_cbox)
  3506 qed
  3507   show ?thesis
  3508     using assms
  3509     apply (subst has_integral_alt)
  3510     apply (subst (asm) has_integral_alt)
  3511     apply (simp add: has_integral_vec1_D_cbox eq_cbox split: if_split_asm, blast)
  3512     apply (intro conjI impI)
  3513      apply (metis vector_one_nth)
  3514     apply (erule thin_rl)
  3515     apply (erule all_forward imp_forward asm_rl ex_forward conj_forward)+
  3516     using * apply blast
  3517     done
  3518 qed
  3519 
  3520 
  3521 lemma integral_vec1_eq:
  3522   fixes f :: "real^1 \<Rightarrow> 'a::real_normed_vector"
  3523   shows "integral S f = integral ((\<lambda>x. x $ 1) ` S) (f \<circ> vec)"
  3524   using has_integral_vec1_I [of f] has_integral_vec1_D [of f]
  3525   by (metis has_integral_iff not_integrable_integral)
  3526 
  3527 lemma absolutely_integrable_drop:
  3528   fixes f :: "real^1 \<Rightarrow> 'b::euclidean_space"
  3529   shows "f absolutely_integrable_on S \<longleftrightarrow> (f \<circ> vec) absolutely_integrable_on (\<lambda>x. x $ 1) ` S"
  3530   unfolding absolutely_integrable_on_def integrable_on_def
  3531 proof safe
  3532   fix y r
  3533   assume "(f has_integral y) S" "((\<lambda>x. norm (f x)) has_integral r) S"
  3534   then show "\<exists>y. (f \<circ> vec has_integral y) ((\<lambda>x. x $ 1) ` S)"
  3535             "\<exists>y. ((\<lambda>x. norm ((f \<circ> vec) x)) has_integral y) ((\<lambda>x. x $ 1) ` S)"
  3536     by (force simp: o_def dest!: has_integral_vec1_I)+
  3537 next
  3538   fix y :: "'b" and r :: "real"
  3539   assume "(f \<circ> vec has_integral y) ((\<lambda>x. x $ 1) ` S)"
  3540          "((\<lambda>x. norm ((f \<circ> vec) x)) has_integral r) ((\<lambda>x. x $ 1) ` S)"
  3541   then show "\<exists>y. (f has_integral y) S"  "\<exists>y. ((\<lambda>x. norm (f x)) has_integral y) S"
  3542     by (force simp: o_def intro: has_integral_vec1_D)+
  3543 qed
  3544 
  3545 subsection \<open>Dominated convergence\<close>
  3546 
  3547 lemma dominated_convergence:
  3548   fixes f :: "nat \<Rightarrow> 'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
  3549   assumes f: "\<And>k. (f k) integrable_on S" and h: "h integrable_on S"
  3550     and le: "\<And>k x. x \<in> S \<Longrightarrow> norm (f k x) \<le> h x"
  3551     and conv: "\<forall>x \<in> S. (\<lambda>k. f k x) \<longlonglongrightarrow> g x"
  3552   shows "g integrable_on S" "(\<lambda>k. integral S (f k)) \<longlonglongrightarrow> integral S g"
  3553 proof -
  3554   have 3: "h absolutely_integrable_on S"
  3555     unfolding absolutely_integrable_on_def
  3556   proof
  3557     show "(\<lambda>x. norm (h x)) integrable_on S"
  3558     proof (intro integrable_spike_finite[OF _ _ h, of "{}"] ballI)
  3559       fix x assume "x \<in> S - {}" then show "norm (h x) = h x"
  3560         by (metis Diff_empty abs_of_nonneg bot_set_def le norm_ge_zero order_trans real_norm_def)
  3561     qed auto
  3562   qed fact
  3563   have 2: "set_borel_measurable lebesgue S (f k)" for k
  3564     unfolding set_borel_measurable_def
  3565     using f by (auto intro: has_integral_implies_lebesgue_measurable simp: integrable_on_def)
  3566   then have 1: "set_borel_measurable lebesgue S g"
  3567     unfolding set_borel_measurable_def
  3568     by (rule borel_measurable_LIMSEQ_metric) (use conv in \<open>auto split: split_indicator\<close>)
  3569   have 4: "AE x in lebesgue. (\<lambda>i. indicator S x *\<^sub>R f i x) \<longlonglongrightarrow> indicator S x *\<^sub>R g x"
  3570     "AE x in lebesgue. norm (indicator S x *\<^sub>R f k x) \<le> indicator S x *\<^sub>R h x" for k
  3571     using conv le by (auto intro!: always_eventually split: split_indicator)
  3572   have g: "g absolutely_integrable_on S"
  3573     using 1 2 3 4 unfolding set_borel_measurable_def set_integrable_def    
  3574     by (rule integrable_dominated_convergence)
  3575   then show "g integrable_on S"
  3576     by (auto simp: absolutely_integrable_on_def)
  3577   have "(\<lambda>k. (LINT x:S|lebesgue. f k x)) \<longlonglongrightarrow> (LINT x:S|lebesgue. g x)"
  3578     unfolding set_borel_measurable_def set_lebesgue_integral_def
  3579     using 1 2 3 4 unfolding set_borel_measurable_def set_lebesgue_integral_def set_integrable_def
  3580     by (rule integral_dominated_convergence)
  3581   then show "(\<lambda>k. integral S (f k)) \<longlonglongrightarrow> integral S g"
  3582     using g absolutely_integrable_integrable_bound[OF le f h]
  3583     by (subst (asm) (1 2) set_lebesgue_integral_eq_integral) auto
  3584 qed
  3585 
  3586 lemma has_integral_dominated_convergence:
  3587   fixes f :: "nat \<Rightarrow> 'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
  3588   assumes "\<And>k. (f k has_integral y k) S" "h integrable_on S"
  3589     "\<And>k. \<forall>x\<in>S. norm (f k x) \<le> h x" "\<forall>x\<in>S. (\<lambda>k. f k x) \<longlonglongrightarrow> g x"
  3590     and x: "y \<longlonglongrightarrow> x"
  3591   shows "(g has_integral x) S"
  3592 proof -
  3593   have int_f: "\<And>k. (f k) integrable_on S"
  3594     using assms by (auto simp: integrable_on_def)
  3595   have "(g has_integral (integral S g)) S"
  3596     by (metis assms(2-4) dominated_convergence(1) has_integral_integral int_f)
  3597   moreover have "integral S g = x"
  3598   proof (rule LIMSEQ_unique)
  3599     show "(\<lambda>i. integral S (f i)) \<longlonglongrightarrow> x"
  3600       using integral_unique[OF assms(1)] x by simp
  3601     show "(\<lambda>i. integral S (f i)) \<longlonglongrightarrow> integral S g"
  3602       by (metis assms(2) assms(3) assms(4) dominated_convergence(2) int_f)
  3603   qed
  3604   ultimately show ?thesis
  3605     by simp
  3606 qed
  3607 
  3608 lemma dominated_convergence_integrable_1:
  3609   fixes f :: "nat \<Rightarrow> 'n::euclidean_space \<Rightarrow> real"
  3610   assumes f: "\<And>k. f k absolutely_integrable_on S"
  3611     and h: "h integrable_on S"
  3612     and normg: "\<And>x. x \<in> S \<Longrightarrow> norm(g x) \<le> (h x)"
  3613     and lim: "\<And>x. x \<in> S \<Longrightarrow> (\<lambda>k. f k x) \<longlonglongrightarrow> g x"
  3614  shows "g integrable_on S"
  3615 proof -
  3616   have habs: "h absolutely_integrable_on S"
  3617     using h normg nonnegative_absolutely_integrable_1 norm_ge_zero order_trans by blast
  3618   let ?f = "\<lambda>n x. (min (max (- h x) (f n x)) (h x))"
  3619   have h0: "h x \<ge> 0" if "x \<in> S" for x
  3620     using normg that by force
  3621   have leh: "norm (?f k x) \<le> h x" if "x \<in> S" for k x
  3622     using h0 that by force
  3623   have limf: "(\<lambda>k. ?f k x) \<longlonglongrightarrow> g x" if "x \<in> S" for x
  3624   proof -
  3625     have "\<And>e y. \<bar>f y x - g x\<bar> < e \<Longrightarrow> \<bar>min (max (- h x) (f y x)) (h x) - g x\<bar> < e"
  3626       using h0 [OF that] normg [OF that] by simp
  3627     then show ?thesis
  3628       using lim [OF that] by (auto simp add: tendsto_iff dist_norm elim!: eventually_mono)
  3629   qed
  3630   show ?thesis
  3631   proof (rule dominated_convergence [of ?f S h g])
  3632     have "(\<lambda>x. - h x) absolutely_integrable_on S"
  3633       using habs unfolding set_integrable_def by auto
  3634     then show "?f k integrable_on S" for k
  3635       by (intro set_lebesgue_integral_eq_integral absolutely_integrable_min_1 absolutely_integrable_max_1 f habs)
  3636   qed (use assms leh limf in auto)
  3637 qed
  3638 
  3639 lemma dominated_convergence_integrable:
  3640   fixes f :: "nat \<Rightarrow> 'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
  3641   assumes f: "\<And>k. f k absolutely_integrable_on S"
  3642     and h: "h integrable_on S"
  3643     and normg: "\<And>x. x \<in> S \<Longrightarrow> norm(g x) \<le> (h x)"
  3644     and lim: "\<And>x. x \<in> S \<Longrightarrow> (\<lambda>k. f k x) \<longlonglongrightarrow> g x"
  3645   shows "g integrable_on S"
  3646   using f
  3647   unfolding integrable_componentwise_iff [of g] absolutely_integrable_componentwise_iff [where f = "f k" for k]
  3648 proof clarify
  3649   fix b :: "'m"
  3650   assume fb [rule_format]: "\<And>k. \<forall>b\<in>Basis. (\<lambda>x. f k x \<bullet> b) absolutely_integrable_on S" and b: "b \<in> Basis"
  3651   show "(\<lambda>x. g x \<bullet> b) integrable_on S"
  3652   proof (rule dominated_convergence_integrable_1 [OF fb h])
  3653     fix x 
  3654     assume "x \<in> S"
  3655     show "norm (g x \<bullet> b) \<le> h x"
  3656       using norm_nth_le \<open>x \<in> S\<close> b normg order.trans by blast
  3657     show "(\<lambda>k. f k x \<bullet> b) \<longlonglongrightarrow> g x \<bullet> b"
  3658       using \<open>x \<in> S\<close> b lim tendsto_componentwise_iff by fastforce
  3659   qed (use b in auto)
  3660 qed
  3661 
  3662 lemma dominated_convergence_absolutely_integrable:
  3663   fixes f :: "nat \<Rightarrow> 'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
  3664   assumes f: "\<And>k. f k absolutely_integrable_on S"
  3665     and h: "h integrable_on S"
  3666     and normg: "\<And>x. x \<in> S \<Longrightarrow> norm(g x) \<le> (h x)"
  3667     and lim: "\<And>x. x \<in> S \<Longrightarrow> (\<lambda>k. f k x) \<longlonglongrightarrow> g x"
  3668   shows "g absolutely_integrable_on S"
  3669 proof -
  3670   have "g integrable_on S"
  3671     by (rule dominated_convergence_integrable [OF assms])
  3672   with assms show ?thesis
  3673     by (blast intro:  absolutely_integrable_integrable_bound [where g=h])
  3674 qed
  3675 
  3676 
  3677 proposition integral_countable_UN:
  3678   fixes f :: "real^'m \<Rightarrow> real^'n"
  3679   assumes f: "f absolutely_integrable_on (\<Union>(range s))"
  3680     and s: "\<And>m. s m \<in> sets lebesgue"
  3681   shows "\<And>n. f absolutely_integrable_on (\<Union>m\<le>n. s m)"
  3682     and "(\<lambda>n. integral (\<Union>m\<le>n. s m) f) \<longlonglongrightarrow> integral (\<Union>(s ` UNIV)) f" (is "?F \<longlonglongrightarrow> ?I")
  3683 proof -
  3684   show fU: "f absolutely_integrable_on (\<Union>m\<le>n. s m)" for n
  3685     using assms by (blast intro: set_integrable_subset [OF f])
  3686   have fint: "f integrable_on (\<Union> (range s))"
  3687     using absolutely_integrable_on_def f by blast
  3688   let ?h = "\<lambda>x. if x \<in> \<Union>(s ` UNIV) then norm(f x) else 0"
  3689   have "(\<lambda>n. integral UNIV (\<lambda>x. if x \<in> (\<Union>m\<le>n. s m) then f x else 0))
  3690         \<longlonglongrightarrow> integral UNIV (\<lambda>x. if x \<in> \<Union>(s ` UNIV) then f x else 0)"
  3691   proof (rule dominated_convergence)
  3692     show "(\<lambda>x. if x \<in> (\<Union>m\<le>n. s m) then f x else 0) integrable_on UNIV" for n
  3693       unfolding integrable_restrict_UNIV
  3694       using fU absolutely_integrable_on_def by blast
  3695     show "(\<lambda>x. if x \<in> \<Union>(s ` UNIV) then norm(f x) else 0) integrable_on UNIV"
  3696       by (metis (no_types) absolutely_integrable_on_def f integrable_restrict_UNIV)
  3697     show "\<forall>x\<in>UNIV.
  3698          (\<lambda>n. if x \<in> (\<Union>m\<le>n. s m) then f x else 0)
  3699          \<longlonglongrightarrow> (if x \<in> \<Union>(s ` UNIV) then f x else 0)"
  3700       by (force intro: Lim_eventually eventually_sequentiallyI)
  3701   qed auto
  3702   then show "?F \<longlonglongrightarrow> ?I"
  3703     by (simp only: integral_restrict_UNIV)
  3704 qed
  3705 
  3706 
  3707 subsection \<open>Fundamental Theorem of Calculus for the Lebesgue integral\<close>
  3708 
  3709 text \<open>
  3710 
  3711 For the positive integral we replace continuity with Borel-measurability.
  3712 
  3713 \<close>
  3714 
  3715 lemma                                                                                          
  3716   fixes f :: "real \<Rightarrow> real"
  3717   assumes [measurable]: "f \<in> borel_measurable borel"
  3718   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"
  3719   shows nn_integral_FTC_Icc: "(\<integral>\<^sup>+x. ennreal (f x) * indicator {a .. b} x \<partial>lborel) = F b - F a" (is ?nn)
  3720     and has_bochner_integral_FTC_Icc_nonneg:
  3721       "has_bochner_integral lborel (\<lambda>x. f x * indicator {a .. b} x) (F b - F a)" (is ?has)
  3722     and integral_FTC_Icc_nonneg: "(\<integral>x. f x * indicator {a .. b} x \<partial>lborel) = F b - F a" (is ?eq)
  3723     and integrable_FTC_Icc_nonneg: "integrable lborel (\<lambda>x. f x * indicator {a .. b} x)" (is ?int)
  3724 proof -
  3725   have *: "(\<lambda>x. f x * indicator {a..b} x) \<in> borel_measurable borel" "\<And>x. 0 \<le> f x * indicator {a..b} x"
  3726     using f(2) by (auto split: split_indicator)
  3727 
  3728   have F_mono: "a \<le> x \<Longrightarrow> x \<le> y \<Longrightarrow> y \<le> b\<Longrightarrow> F x \<le> F y" for x y
  3729     using f by (intro DERIV_nonneg_imp_nondecreasing[of x y F]) (auto intro: order_trans)
  3730 
  3731   have "(f has_integral F b - F a) {a..b}"
  3732     by (intro fundamental_theorem_of_calculus)
  3733        (auto simp: has_field_derivative_iff_has_vector_derivative[symmetric]
  3734              intro: has_field_derivative_subset[OF f(1)] \<open>a \<le> b\<close>)
  3735   then have i: "((\<lambda>x. f x * indicator {a .. b} x) has_integral F b - F a) UNIV"
  3736     unfolding indicator_def if_distrib[where f="\<lambda>x. a * x" for a]
  3737     by (simp cong del: if_weak_cong del: atLeastAtMost_iff)
  3738   then have nn: "(\<integral>\<^sup>+x. f x * indicator {a .. b} x \<partial>lborel) = F b - F a"
  3739     by (rule nn_integral_has_integral_lborel[OF *])
  3740   then show ?has
  3741     by (rule has_bochner_integral_nn_integral[rotated 3]) (simp_all add: * F_mono \<open>a \<le> b\<close>)
  3742   then show ?eq ?int
  3743     unfolding has_bochner_integral_iff by auto
  3744   show ?nn
  3745     by (subst nn[symmetric])
  3746        (auto intro!: nn_integral_cong simp add: ennreal_mult f split: split_indicator)
  3747 qed
  3748 
  3749 lemma
  3750   fixes f :: "real \<Rightarrow> 'a :: euclidean_space"
  3751   assumes "a \<le> b"
  3752   assumes "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> (F has_vector_derivative f x) (at x within {a .. b})"
  3753   assumes cont: "continuous_on {a .. b} f"
  3754   shows has_bochner_integral_FTC_Icc:
  3755       "has_bochner_integral lborel (\<lambda>x. indicator {a .. b} x *\<^sub>R f x) (F b - F a)" (is ?has)
  3756     and integral_FTC_Icc: "(\<integral>x. indicator {a .. b} x *\<^sub>R f x \<partial>lborel) = F b - F a" (is ?eq)
  3757 proof -
  3758   let ?f = "\<lambda>x. indicator {a .. b} x *\<^sub>R f x"
  3759   have int: "integrable lborel ?f"
  3760     using borel_integrable_compact[OF _ cont] by auto
  3761   have "(f has_integral F b - F a) {a..b}"
  3762     using assms(1,2) by (intro fundamental_theorem_of_calculus) auto
  3763   moreover
  3764   have "(f has_integral integral\<^sup>L lborel ?f) {a..b}"
  3765     using has_integral_integral_lborel[OF int]
  3766     unfolding indicator_def if_distrib[where f="\<lambda>x. x *\<^sub>R a" for a]
  3767     by (simp cong del: if_weak_cong del: atLeastAtMost_iff)
  3768   ultimately show ?eq
  3769     by (auto dest: has_integral_unique)
  3770   then show ?has
  3771     using int by (auto simp: has_bochner_integral_iff)
  3772 qed
  3773 
  3774 lemma
  3775   fixes f :: "real \<Rightarrow> real"
  3776   assumes "a \<le> b"
  3777   assumes deriv: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> DERIV F x :> f x"
  3778   assumes cont: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> isCont f x"
  3779   shows has_bochner_integral_FTC_Icc_real:
  3780       "has_bochner_integral lborel (\<lambda>x. f x * indicator {a .. b} x) (F b - F a)" (is ?has)
  3781     and integral_FTC_Icc_real: "(\<integral>x. f x * indicator {a .. b} x \<partial>lborel) = F b - F a" (is ?eq)
  3782 proof -
  3783   have 1: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> (F has_vector_derivative f x) (at x within {a .. b})"
  3784     unfolding has_field_derivative_iff_has_vector_derivative[symmetric]
  3785     using deriv by (auto intro: DERIV_subset)
  3786   have 2: "continuous_on {a .. b} f"
  3787     using cont by (intro continuous_at_imp_continuous_on) auto
  3788   show ?has ?eq
  3789     using has_bochner_integral_FTC_Icc[OF \<open>a \<le> b\<close> 1 2] integral_FTC_Icc[OF \<open>a \<le> b\<close> 1 2]
  3790     by (auto simp: mult.commute)
  3791 qed
  3792 
  3793 lemma nn_integral_FTC_atLeast:
  3794   fixes f :: "real \<Rightarrow> real"
  3795   assumes f_borel: "f \<in> borel_measurable borel"
  3796   assumes f: "\<And>x. a \<le> x \<Longrightarrow> DERIV F x :> f x"
  3797   assumes nonneg: "\<And>x. a \<le> x \<Longrightarrow> 0 \<le> f x"
  3798   assumes lim: "(F \<longlongrightarrow> T) at_top"
  3799   shows "(\<integral>\<^sup>+x. ennreal (f x) * indicator {a ..} x \<partial>lborel) = T - F a"
  3800 proof -
  3801   let ?f = "\<lambda>(i::nat) (x::real). ennreal (f x) * indicator {a..a + real i} x"
  3802   let ?fR = "\<lambda>x. ennreal (f x) * indicator {a ..} x"
  3803 
  3804   have F_mono: "a \<le> x \<Longrightarrow> x \<le> y \<Longrightarrow> F x \<le> F y" for x y
  3805     using f nonneg by (intro DERIV_nonneg_imp_nondecreasing[of x y F]) (auto intro: order_trans)
  3806   then have F_le_T: "a \<le> x \<Longrightarrow> F x \<le> T" for x
  3807     by (intro tendsto_lowerbound[OF lim])
  3808        (auto simp: eventually_at_top_linorder)
  3809 
  3810   have "(SUP i. ?f i x) = ?fR x" for x
  3811   proof (rule LIMSEQ_unique[OF LIMSEQ_SUP])
  3812     obtain n where "x - a < real n"
  3813       using reals_Archimedean2[of "x - a"] ..
  3814     then have "eventually (\<lambda>n. ?f n x = ?fR x) sequentially"
  3815       by (auto intro!: eventually_sequentiallyI[where c=n] split: split_indicator)
  3816     then show "(\<lambda>n. ?f n x) \<longlonglongrightarrow> ?fR x"
  3817       by (rule Lim_eventually)
  3818   qed (auto simp: nonneg incseq_def le_fun_def split: split_indicator)
  3819   then have "integral\<^sup>N lborel ?fR = (\<integral>\<^sup>+ x. (SUP i. ?f i x) \<partial>lborel)"
  3820     by simp
  3821   also have "\<dots> = (SUP i. (\<integral>\<^sup>+ x. ?f i x \<partial>lborel))"
  3822   proof (rule nn_integral_monotone_convergence_SUP)
  3823     show "incseq ?f"
  3824       using nonneg by (auto simp: incseq_def le_fun_def split: split_indicator)
  3825     show "\<And>i. (?f i) \<in> borel_measurable lborel"
  3826       using f_borel by auto
  3827   qed
  3828   also have "\<dots> = (SUP i. ennreal (F (a + real i) - F a))"
  3829     by (subst nn_integral_FTC_Icc[OF f_borel f nonneg]) auto
  3830   also have "\<dots> = T - F a"
  3831   proof (rule LIMSEQ_unique[OF LIMSEQ_SUP])
  3832     have "(\<lambda>x. F (a + real x)) \<longlonglongrightarrow> T"
  3833       apply (rule filterlim_compose[OF lim filterlim_tendsto_add_at_top])
  3834       apply (rule LIMSEQ_const_iff[THEN iffD2, OF refl])
  3835       apply (rule filterlim_real_sequentially)
  3836       done
  3837     then show "(\<lambda>n. ennreal (F (a + real n) - F a)) \<longlonglongrightarrow> ennreal (T - F a)"
  3838       by (simp add: F_mono F_le_T tendsto_diff)
  3839   qed (auto simp: incseq_def intro!: ennreal_le_iff[THEN iffD2] F_mono)
  3840   finally show ?thesis .
  3841 qed
  3842 
  3843 lemma integral_power:
  3844   "a \<le> b \<Longrightarrow> (\<integral>x. x^k * indicator {a..b} x \<partial>lborel) = (b^Suc k - a^Suc k) / Suc k"
  3845 proof (subst integral_FTC_Icc_real)
  3846   fix x show "DERIV (\<lambda>x. x^Suc k / Suc k) x :> x^k"
  3847     by (intro derivative_eq_intros) auto
  3848 qed (auto simp: field_simps simp del: of_nat_Suc)
  3849 
  3850 subsection \<open>Integration by parts\<close>
  3851 
  3852 lemma integral_by_parts_integrable:
  3853   fixes f g F G::"real \<Rightarrow> real"
  3854   assumes "a \<le> b"
  3855   assumes cont_f[intro]: "!!x. a \<le>x \<Longrightarrow> x\<le>b \<Longrightarrow> isCont f x"
  3856   assumes cont_g[intro]: "!!x. a \<le>x \<Longrightarrow> x\<le>b \<Longrightarrow> isCont g x"
  3857   assumes [intro]: "!!x. DERIV F x :> f x"
  3858   assumes [intro]: "!!x. DERIV G x :> g x"
  3859   shows  "integrable lborel (\<lambda>x.((F x) * (g x) + (f x) * (G x)) * indicator {a .. b} x)"
  3860   by (auto intro!: borel_integrable_atLeastAtMost continuous_intros) (auto intro!: DERIV_isCont)
  3861 
  3862 lemma integral_by_parts:
  3863   fixes f g F G::"real \<Rightarrow> real"
  3864   assumes [arith]: "a \<le> b"
  3865   assumes cont_f[intro]: "!!x. a \<le>x \<Longrightarrow> x\<le>b \<Longrightarrow> isCont f x"
  3866   assumes cont_g[intro]: "!!x. a \<le>x \<Longrightarrow> x\<le>b \<Longrightarrow> isCont g x"
  3867   assumes [intro]: "!!x. DERIV F x :> f x"
  3868   assumes [intro]: "!!x. DERIV G x :> g x"
  3869   shows "(\<integral>x. (F x * g x) * indicator {a .. b} x \<partial>lborel)
  3870             =  F b * G b - F a * G a - \<integral>x. (f x * G x) * indicator {a .. b} x \<partial>lborel"
  3871 proof-
  3872   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"
  3873     by (rule integral_FTC_Icc_real, auto intro!: derivative_eq_intros continuous_intros)
  3874       (auto intro!: DERIV_isCont)
  3875 
  3876   have "(\<integral>x. (F x * g x + f x * G x) * indicator {a .. b} x \<partial>lborel) =
  3877     (\<integral>x. (F x * g x) * indicator {a .. b} x \<partial>lborel) + \<integral>x. (f x * G x) * indicator {a .. b} x \<partial>lborel"
  3878     apply (subst Bochner_Integration.integral_add[symmetric])
  3879     apply (auto intro!: borel_integrable_atLeastAtMost continuous_intros)
  3880     by (auto intro!: DERIV_isCont Bochner_Integration.integral_cong split: split_indicator)
  3881 
  3882   thus ?thesis using 0 by auto
  3883 qed
  3884 
  3885 lemma integral_by_parts':
  3886   fixes f g F G::"real \<Rightarrow> real"
  3887   assumes "a \<le> b"
  3888   assumes "!!x. a \<le>x \<Longrightarrow> x\<le>b \<Longrightarrow> isCont f x"
  3889   assumes "!!x. a \<le>x \<Longrightarrow> x\<le>b \<Longrightarrow> isCont g x"
  3890   assumes "!!x. DERIV F x :> f x"
  3891   assumes "!!x. DERIV G x :> g x"
  3892   shows "(\<integral>x. indicator {a .. b} x *\<^sub>R (F x * g x) \<partial>lborel)
  3893             =  F b * G b - F a * G a - \<integral>x. indicator {a .. b} x *\<^sub>R (f x * G x) \<partial>lborel"
  3894   using integral_by_parts[OF assms] by (simp add: ac_simps)
  3895 
  3896 lemma has_bochner_integral_even_function:
  3897   fixes f :: "real \<Rightarrow> 'a :: {banach, second_countable_topology}"
  3898   assumes f: "has_bochner_integral lborel (\<lambda>x. indicator {0..} x *\<^sub>R f x) x"
  3899   assumes even: "\<And>x. f (- x) = f x"
  3900   shows "has_bochner_integral lborel f (2 *\<^sub>R x)"
  3901 proof -
  3902   have indicator: "\<And>x::real. indicator {..0} (- x) = indicator {0..} x"
  3903     by (auto split: split_indicator)
  3904   have "has_bochner_integral lborel (\<lambda>x. indicator {.. 0} x *\<^sub>R f x) x"
  3905     by (subst lborel_has_bochner_integral_real_affine_iff[where c="-1" and t=0])
  3906        (auto simp: indicator even f)
  3907   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)"
  3908     by (rule has_bochner_integral_add)
  3909   then have "has_bochner_integral lborel f (x + x)"
  3910     by (rule has_bochner_integral_discrete_difference[where X="{0}", THEN iffD1, rotated 4])
  3911        (auto split: split_indicator)
  3912   then show ?thesis
  3913     by (simp add: scaleR_2)
  3914 qed
  3915 
  3916 lemma has_bochner_integral_odd_function:
  3917   fixes f :: "real \<Rightarrow> 'a :: {banach, second_countable_topology}"
  3918   assumes f: "has_bochner_integral lborel (\<lambda>x. indicator {0..} x *\<^sub>R f x) x"
  3919   assumes odd: "\<And>x. f (- x) = - f x"
  3920   shows "has_bochner_integral lborel f 0"
  3921 proof -
  3922   have indicator: "\<And>x::real. indicator {..0} (- x) = indicator {0..} x"
  3923     by (auto split: split_indicator)
  3924   have "has_bochner_integral lborel (\<lambda>x. - indicator {.. 0} x *\<^sub>R f x) x"
  3925     by (subst lborel_has_bochner_integral_real_affine_iff[where c="-1" and t=0])
  3926        (auto simp: indicator odd f)
  3927   from has_bochner_integral_minus[OF this]
  3928   have "has_bochner_integral lborel (\<lambda>x. indicator {.. 0} x *\<^sub>R f x) (- x)"
  3929     by simp
  3930   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)"
  3931     by (rule has_bochner_integral_add)
  3932   then have "has_bochner_integral lborel f (x + - x)"
  3933     by (rule has_bochner_integral_discrete_difference[where X="{0}", THEN iffD1, rotated 4])
  3934        (auto split: split_indicator)
  3935   then show ?thesis
  3936     by simp
  3937 qed
  3938 
  3939 lemma has_integral_0_closure_imp_0:
  3940   fixes f :: "'a::euclidean_space \<Rightarrow> real"
  3941   assumes f: "continuous_on (closure S) f"
  3942     and nonneg_interior: "\<And>x. x \<in> S \<Longrightarrow> 0 \<le> f x"
  3943     and pos: "0 < emeasure lborel S"
  3944     and finite: "emeasure lborel S < \<infinity>"
  3945     and regular: "emeasure lborel (closure S) = emeasure lborel S"
  3946     and opn: "open S"
  3947   assumes int: "(f has_integral 0) (closure S)"
  3948   assumes x: "x \<in> closure S"
  3949   shows "f x = 0"
  3950 proof -
  3951   have zero: "emeasure lborel (frontier S) = 0"
  3952     using finite closure_subset regular
  3953     unfolding frontier_def
  3954     by (subst emeasure_Diff) (auto simp: frontier_def interior_open \<open>open S\<close> )
  3955   have nonneg: "0 \<le> f x" if "x \<in> closure S" for x
  3956     using continuous_ge_on_closure[OF f that nonneg_interior] by simp
  3957   have "0 = integral (closure S) f"
  3958     by (blast intro: int sym)
  3959   also
  3960   note intl = has_integral_integrable[OF int]
  3961   have af: "f absolutely_integrable_on (closure S)"
  3962     using nonneg
  3963     by (intro absolutely_integrable_onI intl integrable_eq[OF intl]) simp
  3964   then have "integral (closure S) f = set_lebesgue_integral lebesgue (closure S) f"
  3965     by (intro set_lebesgue_integral_eq_integral(2)[symmetric])
  3966   also have "\<dots> = 0 \<longleftrightarrow> (AE x in lebesgue. indicator (closure S) x *\<^sub>R f x = 0)"
  3967     unfolding set_lebesgue_integral_def
  3968   proof (rule integral_nonneg_eq_0_iff_AE)
  3969     show "integrable lebesgue (\<lambda>x. indicat_real (closure S) x *\<^sub>R f x)"
  3970       by (metis af set_integrable_def)
  3971   qed (use nonneg in \<open>auto simp: indicator_def\<close>)
  3972   also have "\<dots> \<longleftrightarrow> (AE x in lebesgue. x \<in> {x. x \<in> closure S \<longrightarrow> f x = 0})"
  3973     by (auto simp: indicator_def)
  3974   finally have "(AE x in lebesgue. x \<in> {x. x \<in> closure S \<longrightarrow> f x = 0})" by simp
  3975   moreover have "(AE x in lebesgue. x \<in> - frontier S)"
  3976     using zero
  3977     by (auto simp: eventually_ae_filter null_sets_def intro!: exI[where x="frontier S"])
  3978   ultimately have ae: "AE x \<in> S in lebesgue. x \<in> {x \<in> closure S. f x = 0}" (is ?th)
  3979     by eventually_elim (use closure_subset in \<open>auto simp: \<close>)
  3980   have "closed {0::real}" by simp
  3981   with continuous_on_closed_vimage[OF closed_closure, of S f] f
  3982   have "closed (f -` {0} \<inter> closure S)" by blast
  3983   then have "closed {x \<in> closure S. f x = 0}" by (auto simp: vimage_def Int_def conj_commute)
  3984   with \<open>open S\<close> have "x \<in> {x \<in> closure S. f x = 0}" if "x \<in> S" for x using ae that
  3985     by (rule mem_closed_if_AE_lebesgue_open)
  3986   then have "f x = 0" if "x \<in> S" for x using that by auto
  3987   from continuous_constant_on_closure[OF f this \<open>x \<in> closure S\<close>]
  3988   show "f x = 0" .
  3989 qed
  3990 
  3991 lemma has_integral_0_cbox_imp_0:
  3992   fixes f :: "'a::euclidean_space \<Rightarrow> real"
  3993   assumes f: "continuous_on (cbox a b) f"
  3994     and nonneg_interior: "\<And>x. x \<in> box a b \<Longrightarrow> 0 \<le> f x"
  3995   assumes int: "(f has_integral 0) (cbox a b)"
  3996   assumes ne: "box a b \<noteq> {}"
  3997   assumes x: "x \<in> cbox a b"
  3998   shows "f x = 0"
  3999 proof -
  4000   have "0 < emeasure lborel (box a b)"
  4001     using ne x unfolding emeasure_lborel_box_eq
  4002     by (force intro!: prod_pos simp: mem_box algebra_simps)
  4003   then show ?thesis using assms
  4004     by (intro has_integral_0_closure_imp_0[of "box a b" f x])
  4005       (auto simp: emeasure_lborel_box_eq emeasure_lborel_cbox_eq algebra_simps mem_box)
  4006 qed
  4007 
  4008 subsection\<open>Various common equivalent forms of function measurability\<close>
  4009 
  4010 lemma indicator_sum_eq:
  4011   fixes m::real and f :: "'a \<Rightarrow> real"
  4012   assumes "\<bar>m\<bar> \<le> 2 ^ (2*n)" "m/2^n \<le> f x" "f x < (m+1)/2^n" "m \<in> \<int>"
  4013   shows   "(\<Sum>k::real | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n).
  4014             k/2^n * indicator {y. k/2^n \<le> f y \<and> f y < (k+1)/2^n} x)  = m/2^n"
  4015           (is "sum ?f ?S = _")
  4016 proof -
  4017   have "sum ?f ?S = sum (\<lambda>k. k/2^n * indicator {y. k/2^n \<le> f y \<and> f y < (k+1)/2^n} x) {m}"
  4018   proof (rule comm_monoid_add_class.sum.mono_neutral_right)
  4019     show "finite ?S"
  4020       by (rule finite_abs_int_segment)
  4021     show "{m} \<subseteq> {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}"
  4022       using assms by auto
  4023     show "\<forall>i\<in>{k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)} - {m}. ?f i = 0"
  4024       using assms by (auto simp: indicator_def Ints_def abs_le_iff divide_simps)
  4025   qed
  4026   also have "\<dots> = m/2^n"
  4027     using assms by (auto simp: indicator_def not_less)
  4028   finally show ?thesis .
  4029 qed
  4030 
  4031 lemma measurable_on_sf_limit_lemma1:
  4032   fixes f :: "'a::euclidean_space \<Rightarrow> real"
  4033   assumes "\<And>a b. {x \<in> S. a \<le> f x \<and> f x < b} \<in> sets (lebesgue_on S)"
  4034   obtains g where "\<And>n. g n \<in> borel_measurable (lebesgue_on S)"
  4035                   "\<And>n. finite(range (g n))"
  4036                   "\<And>x. (\<lambda>n. g n x) \<longlonglongrightarrow> f x"
  4037 proof
  4038   show "(\<lambda>x. sum (\<lambda>k::real. k/2^n * indicator {y. k/2^n \<le> f y \<and> f y < (k+1)/2^n} x)
  4039                  {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}) \<in> borel_measurable (lebesgue_on S)"
  4040         (is "?g \<in> _")  for n
  4041   proof -
  4042     have "\<And>k. \<lbrakk>k \<in> \<int>; \<bar>k\<bar> \<le> 2 ^ (2*n)\<rbrakk>
  4043          \<Longrightarrow> Measurable.pred (lebesgue_on S) (\<lambda>x. k / (2^n) \<le> f x \<and> f x < (k+1) / (2^n))"
  4044       using assms by (force simp: pred_def space_restrict_space)
  4045     then show ?thesis
  4046       by (simp add: field_class.field_divide_inverse)
  4047   qed
  4048   show "finite (range (?g n))" for n
  4049   proof -
  4050     have "range (?g n) \<subseteq> (\<lambda>k. k/2^n) ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}"
  4051     proof clarify
  4052       fix x
  4053       show "?g n x  \<in> (\<lambda>k. k/2^n) ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}"
  4054       proof (cases "\<exists>k::real. k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n) \<and> k/2^n \<le> (f x) \<and> (f x) < (k+1)/2^n")
  4055         case True
  4056         then show ?thesis
  4057           apply clarify
  4058           by (subst indicator_sum_eq) auto
  4059       next
  4060         case False
  4061         then have "?g n x = 0" by auto
  4062         then show ?thesis by force
  4063       qed
  4064     qed
  4065     moreover have "finite ((\<lambda>k::real. (k/2^n)) ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)})"
  4066       by (simp add: finite_abs_int_segment)
  4067     ultimately show ?thesis
  4068       using finite_subset by blast
  4069   qed
  4070   show "(\<lambda>n. ?g n x) \<longlonglongrightarrow> f x" for x
  4071   proof (rule LIMSEQ_I)
  4072     fix e::real
  4073     assume "e > 0"
  4074     obtain N1 where N1: "\<bar>f x\<bar> < 2 ^ N1"
  4075       using real_arch_pow by fastforce
  4076     obtain N2 where N2: "(1/2) ^ N2 < e"
  4077       using real_arch_pow_inv \<open>e > 0\<close> by force
  4078     have "norm (?g n x - f x) < e" if n: "n \<ge> max N1 N2" for n
  4079     proof -
  4080       define m where "m \<equiv> floor(2^n * (f x))"
  4081       have "1 \<le> \<bar>2^n\<bar> * e"
  4082         using n N2 \<open>e > 0\<close> less_eq_real_def less_le_trans by (fastforce simp add: divide_simps)
  4083       then have *: "\<lbrakk>x \<le> y; y < x + 1\<rbrakk> \<Longrightarrow> abs(x - y) < \<bar>2^n\<bar> * e" for x y::real
  4084         by linarith
  4085       have "\<bar>2^n\<bar> * \<bar>m/2^n - f x\<bar> = \<bar>2^n * (m/2^n - f x)\<bar>"
  4086         by (simp add: abs_mult)
  4087       also have "\<dots> = \<bar>real_of_int \<lfloor>2^n * f x\<rfloor> - f x * 2^n\<bar>"
  4088         by (simp add: algebra_simps m_def)
  4089       also have "\<dots> < \<bar>2^n\<bar> * e"
  4090         by (rule *; simp add: mult.commute)
  4091       finally have "\<bar>2^n\<bar> * \<bar>m/2^n - f x\<bar> < \<bar>2^n\<bar> * e" .
  4092       then have me: "\<bar>m/2^n - f x\<bar> < e"
  4093         by simp
  4094       have "\<bar>real_of_int m\<bar> \<le> 2 ^ (2*n)"
  4095       proof (cases "f x < 0")
  4096         case True
  4097         then have "-\<lfloor>f x\<rfloor> \<le> \<lfloor>(2::real) ^ N1\<rfloor>"
  4098           using N1 le_floor_iff minus_le_iff by fastforce
  4099         with n True have "\<bar>real_of_int\<lfloor>f x\<rfloor>\<bar> \<le> 2 ^ N1"
  4100           by linarith
  4101         also have "\<dots> \<le> 2^n"
  4102           using n by (simp add: m_def)
  4103         finally have "\<bar>real_of_int \<lfloor>f x\<rfloor>\<bar> * 2^n \<le> 2^n * 2^n"
  4104           by simp
  4105         moreover
  4106         have "\<bar>real_of_int \<lfloor>2^n * f x\<rfloor>\<bar> \<le> \<bar>real_of_int \<lfloor>f x\<rfloor>\<bar> * 2^n"
  4107         proof -
  4108           have "\<bar>real_of_int \<lfloor>2^n * f x\<rfloor>\<bar> = - (real_of_int \<lfloor>2^n * f x\<rfloor>)"
  4109             using True by (simp add: abs_if mult_less_0_iff)
  4110           also have "\<dots> \<le> - (real_of_int (\<lfloor>(2::real) ^ n\<rfloor> * \<lfloor>f x\<rfloor>))"
  4111             using le_mult_floor_Ints [of "(2::real)^n"] by simp
  4112           also have "\<dots> \<le> \<bar>real_of_int \<lfloor>f x\<rfloor>\<bar> * 2^n"
  4113             using True
  4114             by simp
  4115           finally show ?thesis .
  4116         qed
  4117         ultimately show ?thesis
  4118           by (metis (no_types, hide_lams) m_def order_trans power2_eq_square power_even_eq)
  4119       next
  4120         case False
  4121         with n N1 have "f x \<le> 2^n"
  4122           by (simp add: not_less) (meson less_eq_real_def one_le_numeral order_trans power_increasing)
  4123         moreover have "0 \<le> m"
  4124           using False m_def by force
  4125         ultimately show ?thesis
  4126           by (metis abs_of_nonneg floor_mono le_floor_iff m_def of_int_0_le_iff power2_eq_square power_mult real_mult_le_cancel_iff1 zero_less_numeral mult.commute zero_less_power)
  4127       qed
  4128       then have "?g n x = m/2^n"
  4129         by (rule indicator_sum_eq) (auto simp: m_def mult.commute divide_simps)
  4130       then have "norm (?g n x - f x) = norm (m/2^n - f x)"
  4131         by simp
  4132       also have "\<dots> < e"
  4133         by (simp add: me)
  4134       finally show ?thesis .
  4135     qed
  4136     then show "\<exists>no. \<forall>n\<ge>no. norm (?g n x - f x) < e"
  4137       by blast
  4138   qed
  4139 qed
  4140 
  4141 
  4142 lemma borel_measurable_vimage_halfspace_component_lt:
  4143      "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
  4144       (\<forall>a i. i \<in> Basis \<longrightarrow> {x \<in> S. f x \<bullet> i < a} \<in> sets (lebesgue_on S))"
  4145   apply (rule trans [OF borel_measurable_iff_halfspace_less])
  4146   apply (fastforce simp add: space_restrict_space)
  4147   done
  4148 
  4149 lemma borel_measurable_simple_function_limit:
  4150   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4151   shows "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
  4152          (\<exists>g. (\<forall>n. (g n) \<in> borel_measurable (lebesgue_on S)) \<and>
  4153               (\<forall>n. finite (range (g n))) \<and> (\<forall>x. (\<lambda>n. g n x) \<longlonglongrightarrow> f x))"
  4154 proof -
  4155   have "\<exists>g. (\<forall>n. (g n) \<in> borel_measurable (lebesgue_on S)) \<and>
  4156             (\<forall>n. finite (range (g n))) \<and> (\<forall>x. (\<lambda>n. g n x) \<longlonglongrightarrow> f x)"
  4157        if f: "\<And>a i. i \<in> Basis \<Longrightarrow> {x \<in> S. f x \<bullet> i < a} \<in> sets (lebesgue_on S)"
  4158   proof -
  4159     have "\<exists>g. (\<forall>n. (g n) \<in> borel_measurable (lebesgue_on S)) \<and>
  4160                   (\<forall>n. finite(image (g n) UNIV)) \<and>
  4161                   (\<forall>x. ((\<lambda>n. g n x) \<longlonglongrightarrow> f x \<bullet> i))" if "i \<in> Basis" for i
  4162     proof (rule measurable_on_sf_limit_lemma1 [of S "\<lambda>x. f x \<bullet> i"])
  4163       show "{x \<in> S. a \<le> f x \<bullet> i \<and> f x \<bullet> i < b} \<in> sets (lebesgue_on S)" for a b
  4164       proof -
  4165         have "{x \<in> S. a \<le> f x \<bullet> i \<and> f x \<bullet> i < b} = {x \<in> S. f x \<bullet> i < b} - {x \<in> S. a > f x \<bullet> i}"
  4166           by auto
  4167         also have "\<dots> \<in> sets (lebesgue_on S)"
  4168           using f that by blast
  4169         finally show ?thesis .
  4170       qed
  4171     qed blast
  4172     then obtain g where g:
  4173           "\<And>i n. i \<in> Basis \<Longrightarrow> g i n \<in> borel_measurable (lebesgue_on S)"
  4174           "\<And>i n. i \<in> Basis \<Longrightarrow> finite(range (g i n))"
  4175           "\<And>i x. i \<in> Basis \<Longrightarrow> ((\<lambda>n. g i n x) \<longlonglongrightarrow> f x \<bullet> i)"
  4176       by metis
  4177     show ?thesis
  4178     proof (intro conjI allI exI)
  4179       show "(\<lambda>x. \<Sum>i\<in>Basis. g i n x *\<^sub>R i) \<in> borel_measurable (lebesgue_on S)" for n
  4180         by (intro borel_measurable_sum borel_measurable_scaleR) (auto intro: g)
  4181       show "finite (range (\<lambda>x. \<Sum>i\<in>Basis. g i n x *\<^sub>R i))" for n
  4182       proof -
  4183         have "range (\<lambda>x. \<Sum>i\<in>Basis. g i n x *\<^sub>R i) \<subseteq> (\<lambda>h. \<Sum>i\<in>Basis. h i *\<^sub>R i) ` PiE Basis (\<lambda>i. range (g i n))"
  4184         proof clarify
  4185           fix x
  4186           show "(\<Sum>i\<in>Basis. g i n x *\<^sub>R i) \<in> (\<lambda>h. \<Sum>i\<in>Basis. h i *\<^sub>R i) ` (\<Pi>\<^sub>E i\<in>Basis. range (g i n))"
  4187             by (rule_tac x="\<lambda>i\<in>Basis. g i n x" in image_eqI) auto
  4188         qed
  4189         moreover have "finite(PiE Basis (\<lambda>i. range (g i n)))"
  4190           by (simp add: g finite_PiE)
  4191         ultimately show ?thesis
  4192           by (metis (mono_tags, lifting) finite_surj)
  4193       qed
  4194       show "(\<lambda>n. \<Sum>i\<in>Basis. g i n x *\<^sub>R i) \<longlonglongrightarrow> f x" for x
  4195       proof -
  4196         have "(\<lambda>n. \<Sum>i\<in>Basis. g i n x *\<^sub>R i) \<longlonglongrightarrow> (\<Sum>i\<in>Basis. (f x \<bullet> i) *\<^sub>R i)"
  4197           by (auto intro!: tendsto_sum tendsto_scaleR g)
  4198         moreover have "(\<Sum>i\<in>Basis. (f x \<bullet> i) *\<^sub>R i) = f x"
  4199           using euclidean_representation by blast
  4200         ultimately show ?thesis
  4201           by metis
  4202       qed
  4203     qed
  4204   qed
  4205   moreover have "f \<in> borel_measurable (lebesgue_on S)"
  4206               if meas_g: "\<And>n. g n \<in> borel_measurable (lebesgue_on S)"
  4207                  and fin: "\<And>n. finite (range (g n))"
  4208                  and to_f: "\<And>x. (\<lambda>n. g n x) \<longlonglongrightarrow> f x" for  g
  4209     by (rule borel_measurable_LIMSEQ_metric [OF meas_g to_f])
  4210   ultimately show ?thesis
  4211     using borel_measurable_vimage_halfspace_component_lt by blast
  4212 qed
  4213 
  4214 lemma borel_measurable_vimage_halfspace_component_ge:
  4215   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4216   shows "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
  4217          (\<forall>a i. i \<in> Basis \<longrightarrow> {x \<in> S. f x \<bullet> i \<ge> a} \<in> sets (lebesgue_on S))"
  4218   apply (rule trans [OF borel_measurable_iff_halfspace_ge])
  4219   apply (fastforce simp add: space_restrict_space)
  4220   done
  4221 
  4222 lemma borel_measurable_vimage_halfspace_component_gt:
  4223   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4224   shows "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
  4225          (\<forall>a i. i \<in> Basis \<longrightarrow> {x \<in> S. f x \<bullet> i > a} \<in> sets (lebesgue_on S))"
  4226   apply (rule trans [OF borel_measurable_iff_halfspace_greater])
  4227   apply (fastforce simp add: space_restrict_space)
  4228   done
  4229 
  4230 lemma borel_measurable_vimage_halfspace_component_le:
  4231   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4232   shows "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
  4233          (\<forall>a i. i \<in> Basis \<longrightarrow> {x \<in> S. f x \<bullet> i \<le> a} \<in> sets (lebesgue_on S))"
  4234   apply (rule trans [OF borel_measurable_iff_halfspace_le])
  4235   apply (fastforce simp add: space_restrict_space)
  4236   done
  4237 
  4238 lemma
  4239   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4240   shows borel_measurable_vimage_open_interval:
  4241          "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
  4242          (\<forall>a b. {x \<in> S. f x \<in> box a b} \<in> sets (lebesgue_on S))" (is ?thesis1)
  4243    and borel_measurable_vimage_open:
  4244          "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
  4245          (\<forall>T. open T \<longrightarrow> {x \<in> S. f x \<in> T} \<in> sets (lebesgue_on S))" (is ?thesis2)
  4246 proof -
  4247   have "{x \<in> S. f x \<in> box a b} \<in> sets (lebesgue_on S)" if "f \<in> borel_measurable (lebesgue_on S)" for a b
  4248   proof -
  4249     have "S = S \<inter> space lebesgue"
  4250       by simp
  4251     then have "S \<inter> (f -` box a b) \<in> sets (lebesgue_on S)"
  4252       by (metis (no_types) box_borel in_borel_measurable_borel inf_sup_aci(1) space_restrict_space that)
  4253     then show ?thesis
  4254       by (simp add: Collect_conj_eq vimage_def)
  4255   qed
  4256   moreover
  4257   have "{x \<in> S. f x \<in> T} \<in> sets (lebesgue_on S)"
  4258        if T: "\<And>a b. {x \<in> S. f x \<in> box a b} \<in> sets (lebesgue_on S)" "open T" for T
  4259   proof -
  4260     obtain \<D> where "countable \<D>" and \<D>: "\<And>X. X \<in> \<D> \<Longrightarrow> \<exists>a b. X = box a b" "\<Union>\<D> = T"
  4261       using open_countable_Union_open_box that \<open>open T\<close> by metis
  4262     then have eq: "{x \<in> S. f x \<in> T} = (\<Union>U \<in> \<D>. {x \<in> S. f x \<in> U})"
  4263       by blast
  4264     have "{x \<in> S. f x \<in> U} \<in> sets (lebesgue_on S)" if "U \<in> \<D>" for U
  4265       using that T \<D> by blast
  4266     then show ?thesis
  4267       by (auto simp: eq intro: Sigma_Algebra.sets.countable_UN' [OF \<open>countable \<D>\<close>])
  4268   qed
  4269   moreover
  4270   have eq: "{x \<in> S. f x \<bullet> i < a} = {x \<in> S. f x \<in> {y. y \<bullet> i < a}}" for i a
  4271     by auto
  4272   have "f \<in> borel_measurable (lebesgue_on S)"
  4273     if "\<And>T. open T \<Longrightarrow> {x \<in> S. f x \<in> T} \<in> sets (lebesgue_on S)"
  4274     by (metis (no_types) eq borel_measurable_vimage_halfspace_component_lt open_halfspace_component_lt that)
  4275   ultimately show "?thesis1" "?thesis2"
  4276     by blast+
  4277 qed
  4278 
  4279 
  4280 lemma borel_measurable_vimage_closed:
  4281   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4282   shows "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
  4283          (\<forall>T. closed T \<longrightarrow> {x \<in> S. f x \<in> T} \<in> sets (lebesgue_on S))"
  4284         (is "?lhs = ?rhs")
  4285 proof -
  4286   have eq: "{x \<in> S. f x \<in> T} = S - {x \<in> S. f x \<in> (- T)}" for T
  4287     by auto
  4288   show ?thesis
  4289     apply (simp add: borel_measurable_vimage_open, safe)
  4290      apply (simp_all (no_asm) add: eq)
  4291      apply (intro sets.Diff sets_lebesgue_on_refl, force simp: closed_open)
  4292     apply (intro sets.Diff sets_lebesgue_on_refl, force simp: open_closed)
  4293     done
  4294 qed
  4295 
  4296 lemma borel_measurable_vimage_closed_interval:
  4297   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4298   shows "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
  4299          (\<forall>a b. {x \<in> S. f x \<in> cbox a b} \<in> sets (lebesgue_on S))"
  4300         (is "?lhs = ?rhs")
  4301 proof
  4302   assume ?lhs then show ?rhs
  4303     using borel_measurable_vimage_closed by blast
  4304 next
  4305   assume RHS: ?rhs
  4306   have "{x \<in> S. f x \<in> T} \<in> sets (lebesgue_on S)" if "open T" for T
  4307   proof -
  4308     obtain \<D> where "countable \<D>" and \<D>: "\<D> \<subseteq> Pow T" "\<And>X. X \<in> \<D> \<Longrightarrow> \<exists>a b. X = cbox a b" "\<Union>\<D> = T"
  4309       using open_countable_Union_open_cbox that \<open>open T\<close> by metis
  4310     then have eq: "{x \<in> S. f x \<in> T} = (\<Union>U \<in> \<D>. {x \<in> S. f x \<in> U})"
  4311       by blast
  4312     have "{x \<in> S. f x \<in> U} \<in> sets (lebesgue_on S)" if "U \<in> \<D>" for U
  4313       using that \<D> by (metis RHS)
  4314     then show ?thesis
  4315       by (auto simp: eq intro: Sigma_Algebra.sets.countable_UN' [OF \<open>countable \<D>\<close>])
  4316   qed
  4317   then show ?lhs
  4318     by (simp add: borel_measurable_vimage_open)
  4319 qed
  4320 
  4321 lemma borel_measurable_UNIV_eq: "borel_measurable (lebesgue_on UNIV) = borel_measurable lebesgue"
  4322   by auto
  4323 
  4324 lemma borel_measurable_vimage_borel:
  4325   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4326   shows "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
  4327          (\<forall>T. T \<in> sets borel \<longrightarrow> {x \<in> S. f x \<in> T} \<in> sets (lebesgue_on S))"
  4328         (is "?lhs = ?rhs")
  4329 proof
  4330   assume f: ?lhs
  4331   then show ?rhs
  4332     using measurable_sets [OF f]
  4333       by (simp add: Collect_conj_eq inf_sup_aci(1) space_restrict_space vimage_def)
  4334 qed (simp add: borel_measurable_vimage_open_interval)
  4335 
  4336 lemma lebesgue_measurable_vimage_borel:
  4337   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4338   assumes "f \<in> borel_measurable lebesgue" "T \<in> sets borel"
  4339   shows "{x. f x \<in> T} \<in> sets lebesgue"
  4340   using assms borel_measurable_vimage_borel [of f UNIV] by auto
  4341 
  4342 lemma borel_measurable_If_I:
  4343   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4344   assumes f: "f \<in> borel_measurable (lebesgue_on S)" and S: "S \<in> sets lebesgue"
  4345   shows "(\<lambda>x. if x \<in> S then f x else 0) \<in> borel_measurable lebesgue"
  4346 proof -
  4347   have eq: "{x. x \<notin> S} \<union> {x. f x \<in> Y} = {x. x \<notin> S} \<union> {x. f x \<in> Y} \<inter> S" for Y
  4348     by blast
  4349   show ?thesis
  4350   using f S
  4351   apply (simp add: vimage_def in_borel_measurable_borel Ball_def)
  4352   apply (elim all_forward imp_forward asm_rl)
  4353   apply (simp only: Collect_conj_eq Collect_disj_eq imp_conv_disj eq)
  4354   apply (auto simp: Compl_eq [symmetric] Compl_in_sets_lebesgue sets_restrict_space_iff)
  4355   done
  4356 qed
  4357 
  4358 lemma borel_measurable_If_D:
  4359   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4360   assumes "(\<lambda>x. if x \<in> S then f x else 0) \<in> borel_measurable lebesgue"
  4361   shows "f \<in> borel_measurable (lebesgue_on S)"
  4362   using assms
  4363   apply (simp add: in_borel_measurable_borel Ball_def)
  4364   apply (elim all_forward imp_forward asm_rl)
  4365   apply (force simp: space_restrict_space sets_restrict_space image_iff intro: rev_bexI)
  4366   done
  4367 
  4368 lemma borel_measurable_UNIV:
  4369   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4370   assumes "S \<in> sets lebesgue"
  4371   shows "(\<lambda>x. if x \<in> S then f x else 0) \<in> borel_measurable lebesgue \<longleftrightarrow> f \<in> borel_measurable (lebesgue_on S)"
  4372   using assms borel_measurable_If_D borel_measurable_If_I by blast
  4373 
  4374 lemma borel_measurable_lebesgue_preimage_borel:
  4375   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4376   shows "f \<in> borel_measurable lebesgue \<longleftrightarrow>
  4377          (\<forall>T. T \<in> sets borel \<longrightarrow> {x. f x \<in> T} \<in> sets lebesgue)"
  4378   apply (intro iffI allI impI lebesgue_measurable_vimage_borel)
  4379     apply (auto simp: in_borel_measurable_borel vimage_def)
  4380   done
  4381 
  4382 subsection\<open>More results on integrability\<close>
  4383 
  4384 lemma integrable_on_all_intervals_UNIV:
  4385   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
  4386   assumes intf: "\<And>a b. f integrable_on cbox a b"
  4387     and normf: "\<And>x. norm(f x) \<le> g x" and g: "g integrable_on UNIV"
  4388   shows "f integrable_on UNIV"
  4389 proof -
  4390 have intg: "(\<forall>a b. g integrable_on cbox a b)"
  4391     and gle_e: "\<forall>e>0. \<exists>B>0. \<forall>a b c d.
  4392                     ball 0 B \<subseteq> cbox a b \<and> cbox a b \<subseteq> cbox c d \<longrightarrow>
  4393                     \<bar>integral (cbox a b) g - integral (cbox c d) g\<bar>
  4394                     < e"
  4395     using g
  4396     by (auto simp: integrable_alt_subset [of _ UNIV] intf)
  4397   have le: "norm (integral (cbox a b) f - integral (cbox c d) f) \<le> \<bar>integral (cbox a b) g - integral (cbox c d) g\<bar>"
  4398     if "cbox a b \<subseteq> cbox c d" for a b c d
  4399   proof -
  4400     have "norm (integral (cbox a b) f - integral (cbox c d) f) = norm (integral (cbox c d - cbox a b) f)"
  4401       using intf that by (simp add: norm_minus_commute integral_setdiff)
  4402     also have "\<dots> \<le> integral (cbox c d - cbox a b) g"
  4403     proof (rule integral_norm_bound_integral [OF _ _ normf])
  4404       show "f integrable_on cbox c d - cbox a b" "g integrable_on cbox c d - cbox a b"
  4405         by (meson integrable_integral integrable_setdiff intf intg negligible_setdiff that)+
  4406     qed
  4407     also have "\<dots> = integral (cbox c d) g - integral (cbox a b) g"
  4408       using intg that by (simp add: integral_setdiff)
  4409     also have "\<dots> \<le> \<bar>integral (cbox a b) g - integral (cbox c d) g\<bar>"
  4410       by simp
  4411     finally show ?thesis .
  4412   qed
  4413   show ?thesis
  4414     using gle_e
  4415     apply (simp add: integrable_alt_subset [of _ UNIV] intf)
  4416     apply (erule imp_forward all_forward ex_forward asm_rl)+
  4417     by (meson not_less order_trans le)
  4418 qed
  4419 
  4420 lemma integrable_on_all_intervals_integrable_bound:
  4421   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
  4422   assumes intf: "\<And>a b. (\<lambda>x. if x \<in> S then f x else 0) integrable_on cbox a b"
  4423     and normf: "\<And>x. x \<in> S \<Longrightarrow> norm(f x) \<le> g x" and g: "g integrable_on S"
  4424   shows "f integrable_on S"
  4425   using integrable_on_all_intervals_UNIV [OF intf, of "(\<lambda>x. if x \<in> S then g x else 0)"]
  4426   by (simp add: g integrable_restrict_UNIV normf)
  4427 
  4428 lemma measurable_bounded_lemma:
  4429   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4430   assumes f: "f \<in> borel_measurable lebesgue" and g: "g integrable_on cbox a b"
  4431     and normf: "\<And>x. x \<in> cbox a b \<Longrightarrow> norm(f x) \<le> g x"
  4432   shows "f integrable_on cbox a b"
  4433 proof -
  4434   have "g absolutely_integrable_on cbox a b"
  4435     by (metis (full_types) add_increasing g le_add_same_cancel1 nonnegative_absolutely_integrable_1 norm_ge_zero normf)
  4436   then have "integrable (lebesgue_on (cbox a b)) g"
  4437     by (simp add: integrable_restrict_space set_integrable_def)
  4438   then have "integrable (lebesgue_on (cbox a b)) f"
  4439   proof (rule Bochner_Integration.integrable_bound)
  4440     show "AE x in lebesgue_on (cbox a b). norm (f x) \<le> norm (g x)"
  4441       by (rule AE_I2) (auto intro: normf order_trans)
  4442   qed (simp add: f measurable_restrict_space1)
  4443   then show ?thesis
  4444     by (simp add: integrable_on_lebesgue_on)
  4445 qed
  4446 
  4447 proposition measurable_bounded_by_integrable_imp_integrable:
  4448   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4449   assumes f: "f \<in> borel_measurable (lebesgue_on S)" and g: "g integrable_on S"
  4450     and normf: "\<And>x. x \<in> S \<Longrightarrow> norm(f x) \<le> g x" and S: "S \<in> sets lebesgue"
  4451   shows "f integrable_on S"
  4452 proof (rule integrable_on_all_intervals_integrable_bound [OF _ normf g])
  4453   show "(\<lambda>x. if x \<in> S then f x else 0) integrable_on cbox a b" for a b
  4454   proof (rule measurable_bounded_lemma)
  4455     show "(\<lambda>x. if x \<in> S then f x else 0) \<in> borel_measurable lebesgue"
  4456       by (simp add: S borel_measurable_UNIV f)
  4457     show "(\<lambda>x. if x \<in> S then g x else 0) integrable_on cbox a b"
  4458       by (simp add: g integrable_altD(1))
  4459     show "norm (if x \<in> S then f x else 0) \<le> (if x \<in> S then g x else 0)" for x
  4460       using normf by simp
  4461   qed
  4462 qed
  4463 
  4464 subsection\<open> Relation between Borel measurability and integrability.\<close>
  4465 
  4466 lemma integrable_imp_measurable_weak:
  4467   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4468   assumes "S \<in> sets lebesgue" "f integrable_on S"
  4469   shows "f \<in> borel_measurable (lebesgue_on S)"
  4470   by (metis (mono_tags, lifting) assms has_integral_implies_lebesgue_measurable borel_measurable_restrict_space_iff integrable_on_def sets.Int_space_eq2)
  4471 
  4472 lemma integrable_imp_measurable:
  4473   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4474   assumes "f integrable_on S"
  4475   shows "f \<in> borel_measurable (lebesgue_on S)"
  4476 proof -
  4477   have "(UNIV::'a set) \<in> sets lborel"
  4478     by simp
  4479   then show ?thesis
  4480     using assms borel_measurable_If_D borel_measurable_UNIV_eq integrable_imp_measurable_weak integrable_restrict_UNIV by blast
  4481 qed
  4482 
  4483 proposition negligible_differentiable_vimage:
  4484   fixes f :: "'a \<Rightarrow> 'a::euclidean_space"
  4485   assumes "negligible T"
  4486     and f': "\<And>x. x \<in> S \<Longrightarrow> inj(f' x)"
  4487     and derf: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
  4488   shows "negligible {x \<in> S. f x \<in> T}"
  4489 proof -
  4490   define U where
  4491     "U \<equiv> \<lambda>n::nat. {x \<in> S. \<forall>y. y \<in> S \<and> norm(y - x) < 1/n
  4492                      \<longrightarrow> norm(y - x) \<le> n * norm(f y - f x)}"
  4493   have "negligible {x \<in> U n. f x \<in> T}" if "n > 0" for n
  4494   proof (subst locally_negligible_alt, clarify)
  4495     fix a
  4496     assume a: "a \<in> U n" and fa: "f a \<in> T"
  4497     define V where "V \<equiv> {x. x \<in> U n \<and> f x \<in> T} \<inter> ball a (1 / n / 2)"
  4498     show "\<exists>V. openin (subtopology euclidean {x \<in> U n. f x \<in> T}) V \<and> a \<in> V \<and> negligible V"
  4499     proof (intro exI conjI)
  4500       have noxy: "norm(x - y) \<le> n * norm(f x - f y)" if "x \<in> V" "y \<in> V" for x y
  4501         using that unfolding U_def V_def mem_Collect_eq Int_iff mem_ball dist_norm
  4502         by (meson norm_triangle_half_r)
  4503       then have "inj_on f V"
  4504         by (force simp: inj_on_def)
  4505       then obtain g where g: "\<And>x. x \<in> V \<Longrightarrow> g(f x) = x"
  4506         by (metis inv_into_f_f)
  4507       have "\<exists>T' B. open T' \<and> f x \<in> T' \<and>
  4508                    (\<forall>y\<in>f ` V \<inter> T \<inter> T'. norm (g y - g (f x)) \<le> B * norm (y - f x))"
  4509         if "f x \<in> T" "x \<in> V" for x
  4510         apply (rule_tac x = "ball (f x) 1" in exI)
  4511         using that noxy by (force simp: g)
  4512       then have "negligible (g ` (f ` V \<inter> T))"
  4513         by (force simp: \<open>negligible T\<close> negligible_Int intro!: negligible_locally_Lipschitz_image)
  4514       moreover have "V \<subseteq> g ` (f ` V \<inter> T)"
  4515         by (force simp: g image_iff V_def)
  4516       ultimately show "negligible V"
  4517         by (rule negligible_subset)
  4518     qed (use a fa V_def that in auto)
  4519   qed
  4520   with negligible_countable_Union have "negligible (\<Union>n \<in> {0<..}. {x. x \<in> U n \<and> f x \<in> T})"
  4521     by auto
  4522   moreover have "{x \<in> S. f x \<in> T} \<subseteq> (\<Union>n \<in> {0<..}. {x. x \<in> U n \<and> f x \<in> T})"
  4523   proof clarsimp
  4524     fix x
  4525     assume "x \<in> S" and "f x \<in> T"
  4526     then obtain inj: "inj(f' x)" and der: "(f has_derivative f' x) (at x within S)"
  4527       using assms by metis
  4528     moreover have "linear(f' x)"
  4529       and eps: "\<And>\<epsilon>. \<epsilon> > 0 \<Longrightarrow> \<exists>\<delta>>0. \<forall>y\<in>S. norm (y - x) < \<delta> \<longrightarrow>
  4530                       norm (f y - f x - f' x (y - x)) \<le> \<epsilon> * norm (y - x)"
  4531       using der by (auto simp: has_derivative_within_alt linear_linear)
  4532     ultimately obtain g where "linear g" and g: "g \<circ> f' x = id"
  4533       using linear_injective_left_inverse by metis
  4534     then obtain B where "B > 0" and B: "\<And>z. B * norm z \<le> norm(f' x z)"
  4535       using linear_invertible_bounded_below_pos \<open>linear (f' x)\<close> by blast
  4536     then obtain i where "i \<noteq> 0" and i: "1 / real i < B"
  4537       by (metis (full_types) inverse_eq_divide real_arch_invD)
  4538     then obtain \<delta> where "\<delta> > 0"
  4539          and \<delta>: "\<And>y. \<lbrakk>y\<in>S; norm (y - x) < \<delta>\<rbrakk> \<Longrightarrow>
  4540                   norm (f y - f x - f' x (y - x)) \<le> (B - 1 / real i) * norm (y - x)"
  4541       using eps [of "B - 1/i"] by auto
  4542     then obtain j where "j \<noteq> 0" and j: "inverse (real j) < \<delta>"
  4543       using real_arch_inverse by blast
  4544     have "norm (y - x)/(max i j) \<le> norm (f y - f x)"
  4545       if "y \<in> S" and less: "norm (y - x) < 1 / (max i j)" for y
  4546     proof -
  4547       have "1 / real (max i j) < \<delta>"
  4548         using j \<open>j \<noteq> 0\<close> \<open>0 < \<delta>\<close>
  4549         by (auto simp: divide_simps max_mult_distrib_left of_nat_max)
  4550     then have "norm (y - x) < \<delta>"
  4551       using less by linarith
  4552     with \<delta> \<open>y \<in> S\<close> have le: "norm (f y - f x - f' x (y - x)) \<le> B * norm (y - x) - norm (y - x)/i"
  4553       by (auto simp: algebra_simps)
  4554     have *: "\<lbrakk>norm(f - f' - y) \<le> b - c; b \<le> norm y; d \<le> c\<rbrakk> \<Longrightarrow> d \<le> norm(f - f')"
  4555       for b c d and y f f'::'a
  4556       using norm_triangle_ineq3 [of "f - f'" y] by simp
  4557     show ?thesis
  4558       apply (rule * [OF le B])
  4559       using \<open>i \<noteq> 0\<close> \<open>j \<noteq> 0\<close> by (simp add: divide_simps max_mult_distrib_left of_nat_max less_max_iff_disj)
  4560   qed
  4561   with \<open>x \<in> S\<close> \<open>i \<noteq> 0\<close> \<open>j \<noteq> 0\<close> show "\<exists>n\<in>{0<..}. x \<in> U n"
  4562     by (rule_tac x="max i j" in bexI) (auto simp: field_simps U_def less_max_iff_disj)
  4563 qed
  4564   ultimately show ?thesis
  4565     by (rule negligible_subset)
  4566 qed
  4567 
  4568 lemma absolutely_integrable_Un:
  4569   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4570   assumes S: "f absolutely_integrable_on S" and T: "f absolutely_integrable_on T"
  4571   shows "f absolutely_integrable_on (S \<union> T)"
  4572 proof -
  4573   have [simp]: "{x. (if x \<in> A then f x else 0) \<noteq> 0} = {x \<in> A. f x \<noteq> 0}" for A
  4574     by auto
  4575   let ?ST = "{x \<in> S. f x \<noteq> 0} \<inter> {x \<in> T. f x \<noteq> 0}"
  4576   have "?ST \<in> sets lebesgue"
  4577   proof (rule Sigma_Algebra.sets.Int)
  4578     have "f integrable_on S"
  4579       using S absolutely_integrable_on_def by blast
  4580     then have "(\<lambda>x. if x \<in> S then f x else 0) integrable_on UNIV"
  4581       by (simp add: integrable_restrict_UNIV)
  4582     then have borel: "(\<lambda>x. if x \<in> S then f x else 0) \<in> borel_measurable (lebesgue_on UNIV)"
  4583       using integrable_imp_measurable borel_measurable_UNIV_eq by blast
  4584     then show "{x \<in> S. f x \<noteq> 0} \<in> sets lebesgue"
  4585       unfolding borel_measurable_vimage_open
  4586       by (rule allE [where x = "-{0}"]) auto
  4587   next
  4588     have "f integrable_on T"
  4589       using T absolutely_integrable_on_def by blast
  4590     then have "(\<lambda>x. if x \<in> T then f x else 0) integrable_on UNIV"
  4591       by (simp add: integrable_restrict_UNIV)
  4592     then have borel: "(\<lambda>x. if x \<in> T then f x else 0) \<in> borel_measurable (lebesgue_on UNIV)"
  4593       using integrable_imp_measurable borel_measurable_UNIV_eq by blast
  4594     then show "{x \<in> T. f x \<noteq> 0} \<in> sets lebesgue"
  4595       unfolding borel_measurable_vimage_open
  4596       by (rule allE [where x = "-{0}"]) auto
  4597   qed
  4598   then have "f absolutely_integrable_on ?ST"
  4599     by (rule set_integrable_subset [OF S]) auto
  4600   then have Int: "(\<lambda>x. if x \<in> ?ST then f x else 0) absolutely_integrable_on UNIV"
  4601     using absolutely_integrable_restrict_UNIV by blast
  4602   have "(\<lambda>x. if x \<in> S then f x else 0) absolutely_integrable_on UNIV"
  4603        "(\<lambda>x. if x \<in> T then f x else 0) absolutely_integrable_on UNIV"
  4604     using S T absolutely_integrable_restrict_UNIV by blast+
  4605   then have "(\<lambda>x. (if x \<in> S then f x else 0) + (if x \<in> T then f x else 0)) absolutely_integrable_on UNIV"
  4606     by (rule absolutely_integrable_add)
  4607   then have "(\<lambda>x. ((if x \<in> S then f x else 0) + (if x \<in> T then f x else 0)) - (if x \<in> ?ST then f x else 0)) absolutely_integrable_on UNIV"
  4608     using Int by (rule absolutely_integrable_diff)
  4609   then have "(\<lambda>x. if x \<in> S \<union> T then f x else 0) absolutely_integrable_on UNIV"
  4610     by (rule absolutely_integrable_spike) (auto intro: empty_imp_negligible)
  4611   then show ?thesis
  4612     unfolding absolutely_integrable_restrict_UNIV .
  4613 qed
  4614 
  4615 lemma uniform_limit_set_lebesgue_integral_at_top:
  4616   fixes f :: "'a \<Rightarrow> real \<Rightarrow> 'b::{banach, second_countable_topology}"
  4617     and g :: "real \<Rightarrow> real"
  4618   assumes bound: "\<And>x y. x \<in> A \<Longrightarrow> y \<ge> a \<Longrightarrow> norm (f x y) \<le> g y"
  4619   assumes integrable: "set_integrable M {a..} g"
  4620   assumes measurable: "\<And>x. x \<in> A \<Longrightarrow> set_borel_measurable M {a..} (f x)"
  4621   assumes "sets borel \<subseteq> sets M"
  4622   shows   "uniform_limit A (\<lambda>b x. LINT y:{a..b}|M. f x y) (\<lambda>x. LINT y:{a..}|M. f x y) at_top"
  4623 proof (cases "A = {}")
  4624   case False
  4625   then obtain x where x: "x \<in> A" by auto
  4626   have g_nonneg: "g y \<ge> 0" if "y \<ge> a" for y
  4627   proof -
  4628     have "0 \<le> norm (f x y)" by simp
  4629     also have "\<dots> \<le> g y" using bound[OF x that] by simp
  4630     finally show ?thesis .
  4631   qed
  4632 
  4633   have integrable': "set_integrable M {a..} (\<lambda>y. f x y)" if "x \<in> A" for x
  4634     unfolding set_integrable_def
  4635   proof (rule Bochner_Integration.integrable_bound)
  4636     show "integrable M (\<lambda>x. indicator {a..} x * g x)"
  4637       using integrable by (simp add: set_integrable_def)
  4638     show "(\<lambda>y. indicat_real {a..} y *\<^sub>R f x y) \<in> borel_measurable M" using measurable[OF that]
  4639       by (simp add: set_borel_measurable_def)
  4640     show "AE y in M. norm (indicat_real {a..} y *\<^sub>R f x y) \<le> norm (indicat_real {a..} y * g y)"
  4641       using bound[OF that] by (intro AE_I2) (auto simp: indicator_def g_nonneg)
  4642   qed
  4643 
  4644   show ?thesis
  4645   proof (rule uniform_limitI)
  4646     fix e :: real assume e: "e > 0"
  4647     have sets [intro]: "A \<in> sets M" if "A \<in> sets borel" for A
  4648       using that assms by blast
  4649   
  4650     have "((\<lambda>b. LINT y:{a..b}|M. g y) \<longlongrightarrow> (LINT y:{a..}|M. g y)) at_top"
  4651       by (intro tendsto_set_lebesgue_integral_at_top assms sets) auto
  4652     with e obtain b0 :: real where b0: "\<forall>b\<ge>b0. \<bar>(LINT y:{a..}|M. g y) - (LINT y:{a..b}|M. g y)\<bar> < e"
  4653       by (auto simp: tendsto_iff eventually_at_top_linorder dist_real_def abs_minus_commute)
  4654     define b where "b = max a b0"
  4655     have "a \<le> b" by (simp add: b_def)
  4656     from b0 have "\<bar>(LINT y:{a..}|M. g y) - (LINT y:{a..b}|M. g y)\<bar> < e"
  4657       by (auto simp: b_def)
  4658     also have "{a..} = {a..b} \<union> {b<..}" by (auto simp: b_def)
  4659     also have "\<bar>(LINT y:\<dots>|M. g y) - (LINT y:{a..b}|M. g y)\<bar> = \<bar>(LINT y:{b<..}|M. g y)\<bar>"
  4660       using \<open>a \<le> b\<close> by (subst set_integral_Un) (auto intro!: set_integrable_subset[OF integrable])
  4661     also have "(LINT y:{b<..}|M. g y) \<ge> 0"
  4662       using g_nonneg \<open>a \<le> b\<close> unfolding set_lebesgue_integral_def
  4663       by (intro Bochner_Integration.integral_nonneg) (auto simp: indicator_def)
  4664     hence "\<bar>(LINT y:{b<..}|M. g y)\<bar> = (LINT y:{b<..}|M. g y)" by simp
  4665     finally have less: "(LINT y:{b<..}|M. g y) < e" .
  4666 
  4667     have "eventually (\<lambda>b. b \<ge> b0) at_top" by (rule eventually_ge_at_top)
  4668     moreover have "eventually (\<lambda>b. b \<ge> a) at_top" by (rule eventually_ge_at_top)
  4669     ultimately show "eventually (\<lambda>b. \<forall>x\<in>A. 
  4670                        dist (LINT y:{a..b}|M. f x y) (LINT y:{a..}|M. f x y) < e) at_top"
  4671     proof eventually_elim
  4672       case (elim b)
  4673       show ?case
  4674       proof
  4675         fix x assume x: "x \<in> A"
  4676         have "dist (LINT y:{a..b}|M. f x y) (LINT y:{a..}|M. f x y) =
  4677                 norm ((LINT y:{a..}|M. f x y) - (LINT y:{a..b}|M. f x y))"
  4678           by (simp add: dist_norm norm_minus_commute)
  4679         also have "{a..} = {a..b} \<union> {b<..}" using elim by auto
  4680         also have "(LINT y:\<dots>|M. f x y) - (LINT y:{a..b}|M. f x y) = (LINT y:{b<..}|M. f x y)"
  4681           using elim x
  4682           by (subst set_integral_Un) (auto intro!: set_integrable_subset[OF integrable'])
  4683         also have "norm \<dots> \<le> (LINT y:{b<..}|M. norm (f x y))" using elim x
  4684           by (intro set_integral_norm_bound set_integrable_subset[OF integrable']) auto
  4685         also have "\<dots> \<le> (LINT y:{b<..}|M. g y)" using elim x bound g_nonneg
  4686           by (intro set_integral_mono set_integrable_norm set_integrable_subset[OF integrable']
  4687                     set_integrable_subset[OF integrable]) auto
  4688         also have "(LINT y:{b<..}|M. g y) \<ge> 0"
  4689           using g_nonneg \<open>a \<le> b\<close> unfolding set_lebesgue_integral_def
  4690           by (intro Bochner_Integration.integral_nonneg) (auto simp: indicator_def)
  4691         hence "(LINT y:{b<..}|M. g y) = \<bar>(LINT y:{b<..}|M. g y)\<bar>" by simp
  4692         also have "\<dots> = \<bar>(LINT y:{a..b} \<union> {b<..}|M. g y) - (LINT y:{a..b}|M. g y)\<bar>"
  4693           using elim by (subst set_integral_Un) (auto intro!: set_integrable_subset[OF integrable])
  4694         also have "{a..b} \<union> {b<..} = {a..}" using elim by auto
  4695         also have "\<bar>(LINT y:{a..}|M. g y) - (LINT y:{a..b}|M. g y)\<bar> < e"
  4696           using b0 elim by blast
  4697         finally show "dist (LINT y:{a..b}|M. f x y) (LINT y:{a..}|M. f x y) < e" .
  4698       qed
  4699     qed
  4700   qed
  4701 qed auto
  4702 
  4703 
  4704 
  4705 subsubsection\<open>Differentiability of inverse function (most basic form)\<close>
  4706 
  4707 proposition has_derivative_inverse_within:
  4708   fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::euclidean_space"
  4709   assumes der_f: "(f has_derivative f') (at a within S)"
  4710     and cont_g: "continuous (at (f a) within f ` S) g"
  4711     and "a \<in> S" "linear g'" and id: "g' \<circ> f' = id"
  4712     and gf: "\<And>x. x \<in> S \<Longrightarrow> g(f x) = x"
  4713   shows "(g has_derivative g') (at (f a) within f ` S)"
  4714 proof -
  4715   have [simp]: "g' (f' x) = x" for x
  4716     by (simp add: local.id pointfree_idE)
  4717   have "bounded_linear f'"
  4718     and f': "\<And>e. e>0 \<Longrightarrow> \<exists>d>0. \<forall>y\<in>S. norm (y - a) < d \<longrightarrow>
  4719                         norm (f y - f a - f' (y - a)) \<le> e * norm (y - a)"
  4720     using der_f by (auto simp: has_derivative_within_alt)
  4721   obtain C where "C > 0" and C: "\<And>x. norm (g' x) \<le> C * norm x"
  4722     using linear_bounded_pos [OF \<open>linear g'\<close>] by metis
  4723   obtain B k where "B > 0" "k > 0"
  4724     and Bk: "\<And>x. \<lbrakk>x \<in> S; norm(f x - f a) < k\<rbrakk> \<Longrightarrow> norm(x - a) \<le> B * norm(f x - f a)"
  4725   proof -
  4726     obtain B where "B > 0" and B: "\<And>x. B * norm x \<le> norm (f' x)"
  4727       using linear_inj_bounded_below_pos [of f'] \<open>linear g'\<close> id der_f has_derivative_linear
  4728         linear_invertible_bounded_below_pos by blast
  4729     then obtain d where "d>0"
  4730       and d: "\<And>y. \<lbrakk>y \<in> S; norm (y - a) < d\<rbrakk> \<Longrightarrow>
  4731                     norm (f y - f a - f' (y - a)) \<le> B / 2 * norm (y - a)"
  4732       using f' [of "B/2"] by auto
  4733     then obtain e where "e > 0"
  4734       and e: "\<And>x. \<lbrakk>x \<in> S; norm (f x - f a) < e\<rbrakk> \<Longrightarrow> norm (g (f x) - g (f a)) < d"
  4735       using cont_g by (auto simp: continuous_within_eps_delta dist_norm)
  4736     show thesis
  4737     proof
  4738       show "2/B > 0"
  4739         using \<open>B > 0\<close> by simp
  4740       show "norm (x - a) \<le> 2 / B * norm (f x - f a)"
  4741         if "x \<in> S" "norm (f x - f a) < e" for x
  4742       proof -
  4743         have xa: "norm (x - a) < d"
  4744           using e [OF that] gf by (simp add: \<open>a \<in> S\<close> that)
  4745         have *: "\<lbrakk>norm(y - f') \<le> B / 2 * norm x; B * norm x \<le> norm f'\<rbrakk>
  4746                  \<Longrightarrow> norm y \<ge> B / 2 * norm x" for y f'::'b and x::'a
  4747           using norm_triangle_ineq3 [of y f'] by linarith
  4748         show ?thesis
  4749           using * [OF d [OF \<open>x \<in> S\<close> xa] B] \<open>B > 0\<close> by (simp add: field_simps)
  4750       qed
  4751     qed (use \<open>e > 0\<close> in auto)
  4752   qed
  4753   show ?thesis
  4754     unfolding has_derivative_within_alt
  4755   proof (intro conjI impI allI)
  4756     show "bounded_linear g'"
  4757       using \<open>linear g'\<close> by (simp add: linear_linear)
  4758   next
  4759     fix e :: "real"
  4760     assume "e > 0"
  4761     then obtain d where "d>0"
  4762       and d: "\<And>y. \<lbrakk>y \<in> S; norm (y - a) < d\<rbrakk> \<Longrightarrow>
  4763                     norm (f y - f a - f' (y - a)) \<le> e / (B * C) * norm (y - a)"
  4764       using f' [of "e / (B * C)"] \<open>B > 0\<close> \<open>C > 0\<close> by auto
  4765     have "norm (x - a - g' (f x - f a)) \<le> e * norm (f x - f a)"
  4766       if "x \<in> S" and lt_k: "norm (f x - f a) < k" and lt_dB: "norm (f x - f a) < d/B" for x
  4767     proof -
  4768       have "norm (x - a) \<le> B * norm(f x - f a)"
  4769         using Bk lt_k \<open>x \<in> S\<close> by blast
  4770       also have "\<dots> < d"
  4771         by (metis \<open>0 < B\<close> lt_dB mult.commute pos_less_divide_eq)
  4772       finally have lt_d: "norm (x - a) < d" .
  4773       have "norm (x - a - g' (f x - f a)) \<le> norm(g'(f x - f a - (f' (x - a))))"
  4774         by (simp add: linear_diff [OF \<open>linear g'\<close>] norm_minus_commute)
  4775       also have "\<dots> \<le> C * norm (f x - f a - f' (x - a))"
  4776         using C by blast
  4777       also have "\<dots> \<le> e * norm (f x - f a)"
  4778       proof -
  4779         have "norm (f x - f a - f' (x - a)) \<le> e / (B * C) * norm (x - a)"
  4780           using d [OF \<open>x \<in> S\<close> lt_d] .
  4781         also have "\<dots> \<le> (norm (f x - f a) * e) / C"
  4782           using \<open>B > 0\<close> \<open>C > 0\<close> \<open>e > 0\<close> by (simp add: field_simps Bk lt_k \<open>x \<in> S\<close>)
  4783         finally show ?thesis
  4784           using \<open>C > 0\<close> by (simp add: field_simps)
  4785       qed
  4786     finally show ?thesis .
  4787     qed
  4788     then show "\<exists>d>0. \<forall>y\<in>f ` S.
  4789                norm (y - f a) < d \<longrightarrow>
  4790                norm (g y - g (f a) - g' (y - f a)) \<le> e * norm (y - f a)"
  4791       apply (rule_tac x="min k (d / B)" in exI)
  4792       using \<open>k > 0\<close> \<open>B > 0\<close> \<open>d > 0\<close> \<open>a \<in> S\<close> by (auto simp: gf)
  4793   qed
  4794 qed
  4795 
  4796 end