src/HOL/Analysis/Henstock_Kurzweil_Integration.thy
author wenzelm
Mon Mar 25 17:21:26 2019 +0100 (3 months ago)
changeset 69981 3dced198b9ec
parent 69922 4a9167f377b0
child 70196 b7ef9090feed
permissions -rw-r--r--
more strict AFP properties;
     1 (*  Author:     John Harrison
     2     Author:     Robert Himmelmann, TU Muenchen (Translation from HOL light)
     3                 Huge cleanup by LCP
     4 *)
     5 
     6 section \<open>Henstock-Kurzweil Gauge Integration in Many Dimensions\<close>
     7 
     8 theory Henstock_Kurzweil_Integration
     9 imports
    10   Lebesgue_Measure
    11   Tagged_Division
    12 begin
    13 
    14 lemma norm_diff2: "\<lbrakk>y = y1 + y2; x = x1 + x2; e = e1 + e2; norm(y1 - x1) \<le> e1; norm(y2 - x2) \<le> e2\<rbrakk>
    15   \<Longrightarrow> norm(y-x) \<le> e"
    16   using norm_triangle_mono [of "y1 - x1" "e1" "y2 - x2" "e2"]
    17   by (simp add: add_diff_add)
    18 
    19 lemma setcomp_dot1: "{z. P (z \<bullet> (i,0))} = {(x,y). P(x \<bullet> i)}"
    20   by auto
    21 
    22 lemma setcomp_dot2: "{z. P (z \<bullet> (0,i))} = {(x,y). P(y \<bullet> i)}"
    23   by auto
    24 
    25 lemma Sigma_Int_Paircomp1: "(Sigma A B) \<inter> {(x, y). P x} = Sigma (A \<inter> {x. P x}) B"
    26   by blast
    27 
    28 lemma Sigma_Int_Paircomp2: "(Sigma A B) \<inter> {(x, y). P y} = Sigma A (\<lambda>z. B z \<inter> {y. P y})"
    29   by blast
    30 (* END MOVE *)
    31 
    32 subsection \<open>Content (length, area, volume...) of an interval\<close>
    33 
    34 abbreviation content :: "'a::euclidean_space set \<Rightarrow> real"
    35   where "content s \<equiv> measure lborel s"
    36 
    37 lemma content_cbox_cases:
    38   "content (cbox a b) = (if \<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i then prod (\<lambda>i. b\<bullet>i - a\<bullet>i) Basis else 0)"
    39   by (simp add: measure_lborel_cbox_eq inner_diff)
    40 
    41 lemma content_cbox: "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i \<Longrightarrow> content (cbox a b) = (\<Prod>i\<in>Basis. b\<bullet>i - a\<bullet>i)"
    42   unfolding content_cbox_cases by simp
    43 
    44 lemma content_cbox': "cbox a b \<noteq> {} \<Longrightarrow> content (cbox a b) = (\<Prod>i\<in>Basis. b\<bullet>i - a\<bullet>i)"
    45   by (simp add: box_ne_empty inner_diff)
    46 
    47 lemma content_cbox_if: "content (cbox a b) = (if cbox a b = {} then 0 else \<Prod>i\<in>Basis. b\<bullet>i - a\<bullet>i)"
    48   by (simp add: content_cbox')
    49 
    50 lemma content_cbox_cart:
    51    "cbox a b \<noteq> {} \<Longrightarrow> content(cbox a b) = prod (\<lambda>i. b$i - a$i) UNIV"
    52   by (simp add: content_cbox_if Basis_vec_def cart_eq_inner_axis axis_eq_axis prod.UNION_disjoint)
    53 
    54 lemma content_cbox_if_cart:
    55    "content(cbox a b) = (if cbox a b = {} then 0 else prod (\<lambda>i. b$i - a$i) UNIV)"
    56   by (simp add: content_cbox_cart)
    57 
    58 lemma content_division_of:
    59   assumes "K \<in> \<D>" "\<D> division_of S"
    60   shows "content K = (\<Prod>i \<in> Basis. interval_upperbound K \<bullet> i - interval_lowerbound K \<bullet> i)"
    61 proof -
    62   obtain a b where "K = cbox a b"
    63     using cbox_division_memE assms by metis
    64   then show ?thesis
    65     using assms by (force simp: division_of_def content_cbox')
    66 qed
    67 
    68 lemma content_real: "a \<le> b \<Longrightarrow> content {a..b} = b - a"
    69   by simp
    70 
    71 lemma abs_eq_content: "\<bar>y - x\<bar> = (if x\<le>y then content {x..y} else content {y..x})"
    72   by (auto simp: content_real)
    73 
    74 lemma content_singleton: "content {a} = 0"
    75   by simp
    76 
    77 lemma content_unit[iff]: "content (cbox 0 (One::'a::euclidean_space)) = 1"
    78   by simp
    79 
    80 lemma content_pos_le [iff]: "0 \<le> content X"
    81   by simp
    82 
    83 corollary content_nonneg [simp]: "\<not> content (cbox a b) < 0"
    84   using not_le by blast
    85 
    86 lemma content_pos_lt: "\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i \<Longrightarrow> 0 < content (cbox a b)"
    87   by (auto simp: less_imp_le inner_diff box_eq_empty intro!: prod_pos)
    88 
    89 lemma content_eq_0: "content (cbox a b) = 0 \<longleftrightarrow> (\<exists>i\<in>Basis. b\<bullet>i \<le> a\<bullet>i)"
    90   by (auto simp: content_cbox_cases not_le intro: less_imp_le antisym eq_refl)
    91 
    92 lemma content_eq_0_interior: "content (cbox a b) = 0 \<longleftrightarrow> interior(cbox a b) = {}"
    93   unfolding content_eq_0 interior_cbox box_eq_empty by auto
    94 
    95 lemma content_pos_lt_eq: "0 < content (cbox a (b::'a::euclidean_space)) \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i)"
    96   by (auto simp add: content_cbox_cases less_le prod_nonneg)
    97 
    98 lemma content_empty [simp]: "content {} = 0"
    99   by simp
   100 
   101 lemma content_real_if [simp]: "content {a..b} = (if a \<le> b then b - a else 0)"
   102   by (simp add: content_real)
   103 
   104 lemma content_subset: "cbox a b \<subseteq> cbox c d \<Longrightarrow> content (cbox a b) \<le> content (cbox c d)"
   105   unfolding measure_def
   106   by (intro enn2real_mono emeasure_mono) (auto simp: emeasure_lborel_cbox_eq)
   107 
   108 lemma content_lt_nz: "0 < content (cbox a b) \<longleftrightarrow> content (cbox a b) \<noteq> 0"
   109   unfolding content_pos_lt_eq content_eq_0 unfolding not_ex not_le by fastforce
   110 
   111 lemma content_Pair: "content (cbox (a,c) (b,d)) = content (cbox a b) * content (cbox c d)"
   112   unfolding measure_lborel_cbox_eq Basis_prod_def
   113   apply (subst prod.union_disjoint)
   114   apply (auto simp: bex_Un ball_Un)
   115   apply (subst (1 2) prod.reindex_nontrivial)
   116   apply auto
   117   done
   118 
   119 lemma content_cbox_pair_eq0_D:
   120    "content (cbox (a,c) (b,d)) = 0 \<Longrightarrow> content (cbox a b) = 0 \<or> content (cbox c d) = 0"
   121   by (simp add: content_Pair)
   122 
   123 lemma content_0_subset: "content(cbox a b) = 0 \<Longrightarrow> s \<subseteq> cbox a b \<Longrightarrow> content s = 0"
   124   using emeasure_mono[of s "cbox a b" lborel]
   125   by (auto simp: measure_def enn2real_eq_0_iff emeasure_lborel_cbox_eq)
   126 
   127 lemma content_split:
   128   fixes a :: "'a::euclidean_space"
   129   assumes "k \<in> Basis"
   130   shows "content (cbox a b) = content(cbox a b \<inter> {x. x\<bullet>k \<le> c}) + content(cbox a b \<inter> {x. x\<bullet>k \<ge> c})"
   131   \<comment> \<open>Prove using measure theory\<close>
   132 proof (cases "\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i")
   133   case True
   134   have 1: "\<And>X Y Z. (\<Prod>i\<in>Basis. Z i (if i = k then X else Y i)) = Z k X * (\<Prod>i\<in>Basis-{k}. Z i (Y i))"
   135     by (simp add: if_distrib prod.delta_remove assms)
   136   note simps = interval_split[OF assms] content_cbox_cases
   137   have 2: "(\<Prod>i\<in>Basis. b\<bullet>i - a\<bullet>i) = (\<Prod>i\<in>Basis-{k}. b\<bullet>i - a\<bullet>i) * (b\<bullet>k - a\<bullet>k)"
   138     by (metis (no_types, lifting) assms finite_Basis mult.commute prod.remove)
   139   have "\<And>x. min (b \<bullet> k) c = max (a \<bullet> k) c \<Longrightarrow>
   140     x * (b\<bullet>k - a\<bullet>k) = x * (max (a \<bullet> k) c - a \<bullet> k) + x * (b \<bullet> k - max (a \<bullet> k) c)"
   141     by  (auto simp add: field_simps)
   142   moreover
   143   have **: "(\<Prod>i\<in>Basis. ((\<Sum>i\<in>Basis. (if i = k then min (b \<bullet> k) c else b \<bullet> i) *\<^sub>R i) \<bullet> i - a \<bullet> i)) =
   144       (\<Prod>i\<in>Basis. (if i = k then min (b \<bullet> k) c else b \<bullet> i) - a \<bullet> i)"
   145     "(\<Prod>i\<in>Basis. b \<bullet> i - ((\<Sum>i\<in>Basis. (if i = k then max (a \<bullet> k) c else a \<bullet> i) *\<^sub>R i) \<bullet> i)) =
   146       (\<Prod>i\<in>Basis. b \<bullet> i - (if i = k then max (a \<bullet> k) c else a \<bullet> i))"
   147     by (auto intro!: prod.cong)
   148   have "\<not> a \<bullet> k \<le> c \<Longrightarrow> \<not> c \<le> b \<bullet> k \<Longrightarrow> False"
   149     unfolding not_le using True assms by auto
   150   ultimately show ?thesis
   151     using assms unfolding simps ** 1[of "\<lambda>i x. b\<bullet>i - x"] 1[of "\<lambda>i x. x - a\<bullet>i"] 2
   152     by auto
   153 next
   154   case False
   155   then have "cbox a b = {}"
   156     unfolding box_eq_empty by (auto simp: not_le)
   157   then show ?thesis
   158     by (auto simp: not_le)
   159 qed
   160 
   161 lemma division_of_content_0:
   162   assumes "content (cbox a b) = 0" "d division_of (cbox a b)" "K \<in> d"
   163   shows "content K = 0"
   164   unfolding forall_in_division[OF assms(2)]
   165   by (meson assms content_0_subset division_of_def)
   166 
   167 lemma sum_content_null:
   168   assumes "content (cbox a b) = 0"
   169     and "p tagged_division_of (cbox a b)"
   170   shows "(\<Sum>(x,K)\<in>p. content K *\<^sub>R f x) = (0::'a::real_normed_vector)"
   171 proof (rule sum.neutral, rule)
   172   fix y
   173   assume y: "y \<in> p"
   174   obtain x K where xk: "y = (x, K)"
   175     using surj_pair[of y] by blast
   176   then obtain c d where k: "K = cbox c d" "K \<subseteq> cbox a b"
   177     by (metis assms(2) tagged_division_ofD(3) tagged_division_ofD(4) y)
   178   have "(\<lambda>(x',K'). content K' *\<^sub>R f x') y = content K *\<^sub>R f x"
   179     unfolding xk by auto
   180   also have "\<dots> = 0"
   181     using assms(1) content_0_subset k(2) by auto
   182   finally show "(\<lambda>(x, k). content k *\<^sub>R f x) y = 0" .
   183 qed
   184 
   185 global_interpretation sum_content: operative plus 0 content
   186   rewrites "comm_monoid_set.F plus 0 = sum"
   187 proof -
   188   interpret operative plus 0 content
   189     by standard (auto simp add: content_split [symmetric] content_eq_0_interior)
   190   show "operative plus 0 content"
   191     by standard
   192   show "comm_monoid_set.F plus 0 = sum"
   193     by (simp add: sum_def)
   194 qed
   195 
   196 lemma additive_content_division: "d division_of (cbox a b) \<Longrightarrow> sum content d = content (cbox a b)"
   197   by (fact sum_content.division)
   198 
   199 lemma additive_content_tagged_division:
   200   "d tagged_division_of (cbox a b) \<Longrightarrow> sum (\<lambda>(x,l). content l) d = content (cbox a b)"
   201   by (fact sum_content.tagged_division)
   202 
   203 lemma subadditive_content_division:
   204   assumes "\<D> division_of S" "S \<subseteq> cbox a b"
   205   shows "sum content \<D> \<le> content(cbox a b)"
   206 proof -
   207   have "\<D> division_of \<Union>\<D>" "\<Union>\<D> \<subseteq> cbox a b"
   208     using assms by auto
   209   then obtain \<D>' where "\<D> \<subseteq> \<D>'" "\<D>' division_of cbox a b"
   210     using partial_division_extend_interval by metis
   211   then have "sum content \<D> \<le> sum content \<D>'"
   212     using sum_mono2 by blast
   213   also have "... \<le> content(cbox a b)"
   214     by (simp add: \<open>\<D>' division_of cbox a b\<close> additive_content_division less_eq_real_def)
   215   finally show ?thesis .
   216 qed
   217 
   218 lemma content_real_eq_0: "content {a..b::real} = 0 \<longleftrightarrow> a \<ge> b"
   219   by (metis atLeastatMost_empty_iff2 content_empty content_real diff_self eq_iff le_cases le_iff_diff_le_0)
   220 
   221 lemma property_empty_interval: "\<forall>a b. content (cbox a b) = 0 \<longrightarrow> P (cbox a b) \<Longrightarrow> P {}"
   222   using content_empty unfolding empty_as_interval by auto
   223 
   224 lemma interval_bounds_nz_content [simp]:
   225   assumes "content (cbox a b) \<noteq> 0"
   226   shows "interval_upperbound (cbox a b) = b"
   227     and "interval_lowerbound (cbox a b) = a"
   228   by (metis assms content_empty interval_bounds')+
   229 
   230 subsection \<open>Gauge integral\<close>
   231 
   232 text \<open>Case distinction to define it first on compact intervals first, then use a limit. This is only
   233 much later unified. In Fremlin: Measure Theory, Volume 4I this is generalized using residual sets.\<close>
   234 
   235 definition has_integral :: "('n::euclidean_space \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'b \<Rightarrow> 'n set \<Rightarrow> bool"
   236   (infixr "has'_integral" 46)
   237   where "(f has_integral I) s \<longleftrightarrow>
   238     (if \<exists>a b. s = cbox a b
   239       then ((\<lambda>p. \<Sum>(x,k)\<in>p. content k *\<^sub>R f x) \<longlongrightarrow> I) (division_filter s)
   240       else (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
   241         (\<exists>z. ((\<lambda>p. \<Sum>(x,k)\<in>p. content k *\<^sub>R (if x \<in> s then f x else 0)) \<longlongrightarrow> z) (division_filter (cbox a b)) \<and>
   242           norm (z - I) < e)))"
   243 
   244 lemma has_integral_cbox:
   245   "(f has_integral I) (cbox a b) \<longleftrightarrow> ((\<lambda>p. \<Sum>(x,k)\<in>p. content k *\<^sub>R f x) \<longlongrightarrow> I) (division_filter (cbox a b))"
   246   by (auto simp add: has_integral_def)
   247 
   248 lemma has_integral:
   249   "(f has_integral y) (cbox a b) \<longleftrightarrow>
   250     (\<forall>e>0. \<exists>\<gamma>. gauge \<gamma> \<and>
   251       (\<forall>\<D>. \<D> tagged_division_of (cbox a b) \<and> \<gamma> fine \<D> \<longrightarrow>
   252         norm (sum (\<lambda>(x,k). content(k) *\<^sub>R f x) \<D> - y) < e))"
   253   by (auto simp: dist_norm eventually_division_filter has_integral_def tendsto_iff)
   254 
   255 lemma has_integral_real:
   256   "(f has_integral y) {a..b::real} \<longleftrightarrow>
   257     (\<forall>e>0. \<exists>\<gamma>. gauge \<gamma> \<and>
   258       (\<forall>\<D>. \<D> tagged_division_of {a..b} \<and> \<gamma> fine \<D> \<longrightarrow>
   259         norm (sum (\<lambda>(x,k). content(k) *\<^sub>R f x) \<D> - y) < e))"
   260   unfolding box_real[symmetric] by (rule has_integral)
   261 
   262 lemma has_integralD[dest]:
   263   assumes "(f has_integral y) (cbox a b)"
   264     and "e > 0"
   265   obtains \<gamma>
   266     where "gauge \<gamma>"
   267       and "\<And>\<D>. \<D> tagged_division_of (cbox a b) \<Longrightarrow> \<gamma> fine \<D> \<Longrightarrow>
   268         norm ((\<Sum>(x,k)\<in>\<D>. content k *\<^sub>R f x) - y) < e"
   269   using assms unfolding has_integral by auto
   270 
   271 lemma has_integral_alt:
   272   "(f has_integral y) i \<longleftrightarrow>
   273     (if \<exists>a b. i = cbox a b
   274      then (f has_integral y) i
   275      else (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
   276       (\<exists>z. ((\<lambda>x. if x \<in> i then f x else 0) has_integral z) (cbox a b) \<and> norm (z - y) < e)))"
   277   by (subst has_integral_def) (auto simp add: has_integral_cbox)
   278 
   279 lemma has_integral_altD:
   280   assumes "(f has_integral y) i"
   281     and "\<not> (\<exists>a b. i = cbox a b)"
   282     and "e>0"
   283   obtains B where "B > 0"
   284     and "\<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
   285       (\<exists>z. ((\<lambda>x. if x \<in> i then f(x) else 0) has_integral z) (cbox a b) \<and> norm(z - y) < e)"
   286   using assms has_integral_alt[of f y i] by auto
   287 
   288 definition integrable_on (infixr "integrable'_on" 46)
   289   where "f integrable_on i \<longleftrightarrow> (\<exists>y. (f has_integral y) i)"
   290 
   291 definition "integral i f = (SOME y. (f has_integral y) i \<or> \<not> f integrable_on i \<and> y=0)"
   292 
   293 lemma integrable_integral[intro]: "f integrable_on i \<Longrightarrow> (f has_integral (integral i f)) i"
   294   unfolding integrable_on_def integral_def by (metis (mono_tags, lifting) someI_ex)
   295 
   296 lemma not_integrable_integral: "\<not> f integrable_on i \<Longrightarrow> integral i f = 0"
   297   unfolding integrable_on_def integral_def by blast
   298 
   299 lemma has_integral_integrable[dest]: "(f has_integral i) s \<Longrightarrow> f integrable_on s"
   300   unfolding integrable_on_def by auto
   301 
   302 lemma has_integral_integral: "f integrable_on s \<longleftrightarrow> (f has_integral (integral s f)) s"
   303   by auto
   304 
   305 subsection \<open>Basic theorems about integrals\<close>
   306 
   307 lemma has_integral_eq_rhs: "(f has_integral j) S \<Longrightarrow> i = j \<Longrightarrow> (f has_integral i) S"
   308   by (rule forw_subst)
   309 
   310 lemma has_integral_unique_cbox:
   311   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::real_normed_vector"
   312   shows "(f has_integral k1) (cbox a b) \<Longrightarrow> (f has_integral k2) (cbox a b) \<Longrightarrow> k1 = k2"
   313     by (auto simp: has_integral_cbox intro: tendsto_unique[OF division_filter_not_empty])    
   314 
   315 lemma has_integral_unique:
   316   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::real_normed_vector"
   317   assumes "(f has_integral k1) i" "(f has_integral k2) i"
   318   shows "k1 = k2"
   319 proof (rule ccontr)
   320   let ?e = "norm (k1 - k2)/2"
   321   let ?F = "(\<lambda>x. if x \<in> i then f x else 0)"
   322   assume "k1 \<noteq> k2"
   323   then have e: "?e > 0"
   324     by auto
   325   have nonbox: "\<not> (\<exists>a b. i = cbox a b)"
   326     using \<open>k1 \<noteq> k2\<close> assms has_integral_unique_cbox by blast
   327   obtain B1 where B1:
   328       "0 < B1"
   329       "\<And>a b. ball 0 B1 \<subseteq> cbox a b \<Longrightarrow>
   330         \<exists>z. (?F has_integral z) (cbox a b) \<and> norm (z - k1) < norm (k1 - k2)/2"
   331     by (rule has_integral_altD[OF assms(1) nonbox,OF e]) blast
   332   obtain B2 where B2:
   333       "0 < B2"
   334       "\<And>a b. ball 0 B2 \<subseteq> cbox a b \<Longrightarrow>
   335         \<exists>z. (?F has_integral z) (cbox a b) \<and> norm (z - k2) < norm (k1 - k2)/2"
   336     by (rule has_integral_altD[OF assms(2) nonbox,OF e]) blast
   337   obtain a b :: 'n where ab: "ball 0 B1 \<subseteq> cbox a b" "ball 0 B2 \<subseteq> cbox a b"
   338     by (metis Un_subset_iff bounded_Un bounded_ball bounded_subset_cbox_symmetric)
   339   obtain w where w: "(?F has_integral w) (cbox a b)" "norm (w - k1) < norm (k1 - k2)/2"
   340     using B1(2)[OF ab(1)] by blast
   341   obtain z where z: "(?F has_integral z) (cbox a b)" "norm (z - k2) < norm (k1 - k2)/2"
   342     using B2(2)[OF ab(2)] by blast
   343   have "z = w"
   344     using has_integral_unique_cbox[OF w(1) z(1)] by auto
   345   then have "norm (k1 - k2) \<le> norm (z - k2) + norm (w - k1)"
   346     using norm_triangle_ineq4 [of "k1 - w" "k2 - z"]
   347     by (auto simp add: norm_minus_commute)
   348   also have "\<dots> < norm (k1 - k2)/2 + norm (k1 - k2)/2"
   349     by (metis add_strict_mono z(2) w(2))
   350   finally show False by auto
   351 qed
   352 
   353 lemma integral_unique [intro]: "(f has_integral y) k \<Longrightarrow> integral k f = y"
   354   unfolding integral_def
   355   by (rule some_equality) (auto intro: has_integral_unique)
   356 
   357 lemma has_integral_iff: "(f has_integral i) S \<longleftrightarrow> (f integrable_on S \<and> integral S f = i)"
   358   by blast
   359 
   360 lemma eq_integralD: "integral k f = y \<Longrightarrow> (f has_integral y) k \<or> \<not> f integrable_on k \<and> y=0"
   361   unfolding integral_def integrable_on_def
   362   apply (erule subst)
   363   apply (rule someI_ex)
   364   by blast
   365 
   366 lemma has_integral_const [intro]:
   367   fixes a b :: "'a::euclidean_space"
   368   shows "((\<lambda>x. c) has_integral (content (cbox a b) *\<^sub>R c)) (cbox a b)"
   369   using eventually_division_filter_tagged_division[of "cbox a b"]
   370      additive_content_tagged_division[of _ a b]
   371   by (auto simp: has_integral_cbox split_beta' scaleR_sum_left[symmetric]
   372            elim!: eventually_mono intro!: tendsto_cong[THEN iffD1, OF _ tendsto_const])
   373 
   374 lemma has_integral_const_real [intro]:
   375   fixes a b :: real
   376   shows "((\<lambda>x. c) has_integral (content {a..b} *\<^sub>R c)) {a..b}"
   377   by (metis box_real(2) has_integral_const)
   378 
   379 lemma has_integral_integrable_integral: "(f has_integral i) s \<longleftrightarrow> f integrable_on s \<and> integral s f = i"
   380   by blast
   381 
   382 lemma integral_const [simp]:
   383   fixes a b :: "'a::euclidean_space"
   384   shows "integral (cbox a b) (\<lambda>x. c) = content (cbox a b) *\<^sub>R c"
   385   by (rule integral_unique) (rule has_integral_const)
   386 
   387 lemma integral_const_real [simp]:
   388   fixes a b :: real
   389   shows "integral {a..b} (\<lambda>x. c) = content {a..b} *\<^sub>R c"
   390   by (metis box_real(2) integral_const)
   391 
   392 lemma has_integral_is_0_cbox:
   393   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::real_normed_vector"
   394   assumes "\<And>x. x \<in> cbox a b \<Longrightarrow> f x = 0"
   395   shows "(f has_integral 0) (cbox a b)"
   396     unfolding has_integral_cbox
   397     using eventually_division_filter_tagged_division[of "cbox a b"] assms
   398     by (subst tendsto_cong[where g="\<lambda>_. 0"])
   399        (auto elim!: eventually_mono intro!: sum.neutral simp: tag_in_interval)
   400 
   401 lemma has_integral_is_0:
   402   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::real_normed_vector"
   403   assumes "\<And>x. x \<in> S \<Longrightarrow> f x = 0"
   404   shows "(f has_integral 0) S"
   405 proof (cases "(\<exists>a b. S = cbox a b)")
   406   case True with assms has_integral_is_0_cbox show ?thesis
   407     by blast
   408 next
   409   case False
   410   have *: "(\<lambda>x. if x \<in> S then f x else 0) = (\<lambda>x. 0)"
   411     by (auto simp add: assms)
   412   show ?thesis
   413     using has_integral_is_0_cbox False
   414     by (subst has_integral_alt) (force simp add: *)
   415 qed
   416 
   417 lemma has_integral_0[simp]: "((\<lambda>x::'n::euclidean_space. 0) has_integral 0) S"
   418   by (rule has_integral_is_0) auto
   419 
   420 lemma has_integral_0_eq[simp]: "((\<lambda>x. 0) has_integral i) S \<longleftrightarrow> i = 0"
   421   using has_integral_unique[OF has_integral_0] by auto
   422 
   423 lemma has_integral_linear_cbox:
   424   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::real_normed_vector"
   425   assumes f: "(f has_integral y) (cbox a b)"
   426     and h: "bounded_linear h"
   427   shows "((h \<circ> f) has_integral (h y)) (cbox a b)"
   428 proof -
   429   interpret bounded_linear h using h .
   430   show ?thesis
   431     unfolding has_integral_cbox using tendsto [OF f [unfolded has_integral_cbox]]
   432     by (simp add: sum scaleR split_beta')
   433 qed
   434 
   435 lemma has_integral_linear:
   436   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::real_normed_vector"
   437   assumes f: "(f has_integral y) S"
   438     and h: "bounded_linear h"
   439   shows "((h \<circ> f) has_integral (h y)) S"
   440 proof (cases "(\<exists>a b. S = cbox a b)")
   441   case True with f h has_integral_linear_cbox show ?thesis 
   442     by blast
   443 next
   444   case False
   445   interpret bounded_linear h using h .
   446   from pos_bounded obtain B where B: "0 < B" "\<And>x. norm (h x) \<le> norm x * B"
   447     by blast
   448   let ?S = "\<lambda>f x. if x \<in> S then f x else 0"
   449   show ?thesis
   450   proof (subst has_integral_alt, clarsimp simp: False)
   451     fix e :: real
   452     assume e: "e > 0"
   453     have *: "0 < e/B" using e B(1) by simp
   454     obtain M where M:
   455       "M > 0"
   456       "\<And>a b. ball 0 M \<subseteq> cbox a b \<Longrightarrow>
   457         \<exists>z. (?S f has_integral z) (cbox a b) \<and> norm (z - y) < e/B"
   458       using has_integral_altD[OF f False *] by blast
   459     show "\<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
   460       (\<exists>z. (?S(h \<circ> f) has_integral z) (cbox a b) \<and> norm (z - h y) < e)"
   461     proof (rule exI, intro allI conjI impI)
   462       show "M > 0" using M by metis
   463     next
   464       fix a b::'n
   465       assume sb: "ball 0 M \<subseteq> cbox a b"
   466       obtain z where z: "(?S f has_integral z) (cbox a b)" "norm (z - y) < e/B"
   467         using M(2)[OF sb] by blast
   468       have *: "?S(h \<circ> f) = h \<circ> ?S f"
   469         using zero by auto
   470       show "\<exists>z. (?S(h \<circ> f) has_integral z) (cbox a b) \<and> norm (z - h y) < e"
   471         apply (rule_tac x="h z" in exI)
   472         apply (simp add: * has_integral_linear_cbox[OF z(1) h])
   473         apply (metis B diff le_less_trans pos_less_divide_eq z(2))
   474         done
   475     qed
   476   qed
   477 qed
   478 
   479 lemma has_integral_scaleR_left:
   480   "(f has_integral y) S \<Longrightarrow> ((\<lambda>x. f x *\<^sub>R c) has_integral (y *\<^sub>R c)) S"
   481   using has_integral_linear[OF _ bounded_linear_scaleR_left] by (simp add: comp_def)
   482 
   483 lemma integrable_on_scaleR_left:
   484   assumes "f integrable_on A"
   485   shows "(\<lambda>x. f x *\<^sub>R y) integrable_on A"
   486   using assms has_integral_scaleR_left unfolding integrable_on_def by blast
   487 
   488 lemma has_integral_mult_left:
   489   fixes c :: "_ :: real_normed_algebra"
   490   shows "(f has_integral y) S \<Longrightarrow> ((\<lambda>x. f x * c) has_integral (y * c)) S"
   491   using has_integral_linear[OF _ bounded_linear_mult_left] by (simp add: comp_def)
   492 
   493 text\<open>The case analysis eliminates the condition \<^term>\<open>f integrable_on S\<close> at the cost
   494      of the type class constraint \<open>division_ring\<close>\<close>
   495 corollary integral_mult_left [simp]:
   496   fixes c:: "'a::{real_normed_algebra,division_ring}"
   497   shows "integral S (\<lambda>x. f x * c) = integral S f * c"
   498 proof (cases "f integrable_on S \<or> c = 0")
   499   case True then show ?thesis
   500     by (force intro: has_integral_mult_left)
   501 next
   502   case False then have "\<not> (\<lambda>x. f x * c) integrable_on S"
   503     using has_integral_mult_left [of "(\<lambda>x. f x * c)" _ S "inverse c"]
   504     by (auto simp add: mult.assoc)
   505   with False show ?thesis by (simp add: not_integrable_integral)
   506 qed
   507 
   508 corollary integral_mult_right [simp]:
   509   fixes c:: "'a::{real_normed_field}"
   510   shows "integral S (\<lambda>x. c * f x) = c * integral S f"
   511 by (simp add: mult.commute [of c])
   512 
   513 corollary integral_divide [simp]:
   514   fixes z :: "'a::real_normed_field"
   515   shows "integral S (\<lambda>x. f x / z) = integral S (\<lambda>x. f x) / z"
   516 using integral_mult_left [of S f "inverse z"]
   517   by (simp add: divide_inverse_commute)
   518 
   519 lemma has_integral_mult_right:
   520   fixes c :: "'a :: real_normed_algebra"
   521   shows "(f has_integral y) i \<Longrightarrow> ((\<lambda>x. c * f x) has_integral (c * y)) i"
   522   using has_integral_linear[OF _ bounded_linear_mult_right] by (simp add: comp_def)
   523 
   524 lemma has_integral_cmul: "(f has_integral k) S \<Longrightarrow> ((\<lambda>x. c *\<^sub>R f x) has_integral (c *\<^sub>R k)) S"
   525   unfolding o_def[symmetric]
   526   by (metis has_integral_linear bounded_linear_scaleR_right)
   527 
   528 lemma has_integral_cmult_real:
   529   fixes c :: real
   530   assumes "c \<noteq> 0 \<Longrightarrow> (f has_integral x) A"
   531   shows "((\<lambda>x. c * f x) has_integral c * x) A"
   532 proof (cases "c = 0")
   533   case True
   534   then show ?thesis by simp
   535 next
   536   case False
   537   from has_integral_cmul[OF assms[OF this], of c] show ?thesis
   538     unfolding real_scaleR_def .
   539 qed
   540 
   541 lemma has_integral_neg: "(f has_integral k) S \<Longrightarrow> ((\<lambda>x. -(f x)) has_integral -k) S"
   542   by (drule_tac c="-1" in has_integral_cmul) auto
   543 
   544 lemma has_integral_neg_iff: "((\<lambda>x. - f x) has_integral k) S \<longleftrightarrow> (f has_integral - k) S"
   545   using has_integral_neg[of f "- k"] has_integral_neg[of "\<lambda>x. - f x" k] by auto
   546 
   547 lemma has_integral_add_cbox:
   548   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::real_normed_vector"
   549   assumes "(f has_integral k) (cbox a b)" "(g has_integral l) (cbox a b)"
   550   shows "((\<lambda>x. f x + g x) has_integral (k + l)) (cbox a b)"
   551   using assms
   552     unfolding has_integral_cbox
   553     by (simp add: split_beta' scaleR_add_right sum.distrib[abs_def] tendsto_add)
   554 
   555 lemma has_integral_add:
   556   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::real_normed_vector"
   557   assumes f: "(f has_integral k) S" and g: "(g has_integral l) S"
   558   shows "((\<lambda>x. f x + g x) has_integral (k + l)) S"
   559 proof (cases "\<exists>a b. S = cbox a b")
   560   case True with has_integral_add_cbox assms show ?thesis
   561     by blast 
   562 next
   563   let ?S = "\<lambda>f x. if x \<in> S then f x else 0"
   564   case False
   565   then show ?thesis
   566   proof (subst has_integral_alt, clarsimp, goal_cases)
   567     case (1 e)
   568     then have e2: "e/2 > 0"
   569       by auto
   570     obtain Bf where "0 < Bf"
   571       and Bf: "\<And>a b. ball 0 Bf \<subseteq> cbox a b \<Longrightarrow>
   572                      \<exists>z. (?S f has_integral z) (cbox a b) \<and> norm (z - k) < e/2"
   573       using has_integral_altD[OF f False e2] by blast
   574     obtain Bg where "0 < Bg"
   575       and Bg: "\<And>a b. ball 0 Bg \<subseteq> (cbox a b) \<Longrightarrow>
   576                      \<exists>z. (?S g has_integral z) (cbox a b) \<and> norm (z - l) < e/2"
   577       using has_integral_altD[OF g False e2] by blast
   578     show ?case
   579     proof (rule_tac x="max Bf Bg" in exI, clarsimp simp add: max.strict_coboundedI1 \<open>0 < Bf\<close>)
   580       fix a b
   581       assume "ball 0 (max Bf Bg) \<subseteq> cbox a (b::'n)"
   582       then have fs: "ball 0 Bf \<subseteq> cbox a (b::'n)" and gs: "ball 0 Bg \<subseteq> cbox a (b::'n)"
   583         by auto
   584       obtain w where w: "(?S f has_integral w) (cbox a b)" "norm (w - k) < e/2"
   585         using Bf[OF fs] by blast
   586       obtain z where z: "(?S g has_integral z) (cbox a b)" "norm (z - l) < e/2"
   587         using Bg[OF gs] by blast
   588       have *: "\<And>x. (if x \<in> S then f x + g x else 0) = (?S f x) + (?S g x)"
   589         by auto
   590       show "\<exists>z. (?S(\<lambda>x. f x + g x) has_integral z) (cbox a b) \<and> norm (z - (k + l)) < e"
   591         apply (rule_tac x="w + z" in exI)
   592         apply (simp add: has_integral_add_cbox[OF w(1) z(1), unfolded *[symmetric]])
   593         using norm_triangle_ineq[of "w - k" "z - l"] w(2) z(2)
   594         apply (auto simp add: field_simps)
   595         done
   596     qed
   597   qed
   598 qed
   599 
   600 lemma has_integral_diff:
   601   "(f has_integral k) S \<Longrightarrow> (g has_integral l) S \<Longrightarrow>
   602     ((\<lambda>x. f x - g x) has_integral (k - l)) S"
   603   using has_integral_add[OF _ has_integral_neg, of f k S g l]
   604   by (auto simp: algebra_simps)
   605 
   606 lemma integral_0 [simp]:
   607   "integral S (\<lambda>x::'n::euclidean_space. 0::'m::real_normed_vector) = 0"
   608   by (rule integral_unique has_integral_0)+
   609 
   610 lemma integral_add: "f integrable_on S \<Longrightarrow> g integrable_on S \<Longrightarrow>
   611     integral S (\<lambda>x. f x + g x) = integral S f + integral S g"
   612   by (rule integral_unique) (metis integrable_integral has_integral_add)
   613 
   614 lemma integral_cmul [simp]: "integral S (\<lambda>x. c *\<^sub>R f x) = c *\<^sub>R integral S f"
   615 proof (cases "f integrable_on S \<or> c = 0")
   616   case True with has_integral_cmul integrable_integral show ?thesis
   617     by fastforce
   618 next
   619   case False then have "\<not> (\<lambda>x. c *\<^sub>R f x) integrable_on S"
   620     using has_integral_cmul [of "(\<lambda>x. c *\<^sub>R f x)" _ S "inverse c"] by auto
   621   with False show ?thesis by (simp add: not_integrable_integral)
   622 qed
   623 
   624 lemma integral_mult:
   625   fixes K::real
   626   shows "f integrable_on X \<Longrightarrow> K * integral X f = integral X (\<lambda>x. K * f x)"
   627   unfolding real_scaleR_def[symmetric] integral_cmul ..
   628 
   629 lemma integral_neg [simp]: "integral S (\<lambda>x. - f x) = - integral S f"
   630 proof (cases "f integrable_on S")
   631   case True then show ?thesis
   632     by (simp add: has_integral_neg integrable_integral integral_unique)
   633 next
   634   case False then have "\<not> (\<lambda>x. - f x) integrable_on S"
   635     using has_integral_neg [of "(\<lambda>x. - f x)" _ S ] by auto
   636   with False show ?thesis by (simp add: not_integrable_integral)
   637 qed
   638 
   639 lemma integral_diff: "f integrable_on S \<Longrightarrow> g integrable_on S \<Longrightarrow>
   640     integral S (\<lambda>x. f x - g x) = integral S f - integral S g"
   641   by (rule integral_unique) (metis integrable_integral has_integral_diff)
   642 
   643 lemma integrable_0: "(\<lambda>x. 0) integrable_on S"
   644   unfolding integrable_on_def using has_integral_0 by auto
   645 
   646 lemma integrable_add: "f integrable_on S \<Longrightarrow> g integrable_on S \<Longrightarrow> (\<lambda>x. f x + g x) integrable_on S"
   647   unfolding integrable_on_def by(auto intro: has_integral_add)
   648 
   649 lemma integrable_cmul: "f integrable_on S \<Longrightarrow> (\<lambda>x. c *\<^sub>R f(x)) integrable_on S"
   650   unfolding integrable_on_def by(auto intro: has_integral_cmul)
   651 
   652 lemma integrable_on_scaleR_iff [simp]:
   653   fixes c :: real
   654   assumes "c \<noteq> 0"
   655   shows "(\<lambda>x. c *\<^sub>R f x) integrable_on S \<longleftrightarrow> f integrable_on S"
   656   using integrable_cmul[of "\<lambda>x. c *\<^sub>R f x" S "1 / c"] integrable_cmul[of f S c] \<open>c \<noteq> 0\<close>
   657   by auto
   658 
   659 lemma integrable_on_cmult_iff [simp]:
   660   fixes c :: real
   661   assumes "c \<noteq> 0"
   662   shows "(\<lambda>x. c * f x) integrable_on S \<longleftrightarrow> f integrable_on S"
   663   using integrable_on_scaleR_iff [of c f] assms by simp
   664 
   665 lemma integrable_on_cmult_left:
   666   assumes "f integrable_on S"
   667   shows "(\<lambda>x. of_real c * f x) integrable_on S"
   668     using integrable_cmul[of f S "of_real c"] assms
   669     by (simp add: scaleR_conv_of_real)
   670 
   671 lemma integrable_neg: "f integrable_on S \<Longrightarrow> (\<lambda>x. -f(x)) integrable_on S"
   672   unfolding integrable_on_def by(auto intro: has_integral_neg)
   673 
   674 lemma integrable_neg_iff: "(\<lambda>x. -f(x)) integrable_on S \<longleftrightarrow> f integrable_on S"
   675   using integrable_neg by fastforce
   676 
   677 lemma integrable_diff:
   678   "f integrable_on S \<Longrightarrow> g integrable_on S \<Longrightarrow> (\<lambda>x. f x - g x) integrable_on S"
   679   unfolding integrable_on_def by(auto intro: has_integral_diff)
   680 
   681 lemma integrable_linear:
   682   "f integrable_on S \<Longrightarrow> bounded_linear h \<Longrightarrow> (h \<circ> f) integrable_on S"
   683   unfolding integrable_on_def by(auto intro: has_integral_linear)
   684 
   685 lemma integral_linear:
   686   "f integrable_on S \<Longrightarrow> bounded_linear h \<Longrightarrow> integral S (h \<circ> f) = h (integral S f)"
   687   apply (rule has_integral_unique [where i=S and f = "h \<circ> f"])
   688   apply (simp_all add: integrable_integral integrable_linear has_integral_linear )
   689   done
   690 
   691 lemma integrable_on_cnj_iff:
   692   "(\<lambda>x. cnj (f x)) integrable_on A \<longleftrightarrow> f integrable_on A"
   693   using integrable_linear[OF _ bounded_linear_cnj, of f A]
   694         integrable_linear[OF _ bounded_linear_cnj, of "cnj \<circ> f" A]
   695   by (auto simp: o_def)
   696 
   697 lemma integral_cnj: "cnj (integral A f) = integral A (\<lambda>x. cnj (f x))"
   698   by (cases "f integrable_on A")
   699      (simp_all add: integral_linear[OF _ bounded_linear_cnj, symmetric]
   700                     o_def integrable_on_cnj_iff not_integrable_integral)
   701 
   702 lemma integral_component_eq[simp]:
   703   fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
   704   assumes "f integrable_on S"
   705   shows "integral S (\<lambda>x. f x \<bullet> k) = integral S f \<bullet> k"
   706   unfolding integral_linear[OF assms(1) bounded_linear_inner_left,unfolded o_def] ..
   707 
   708 lemma has_integral_sum:
   709   assumes "finite T"
   710     and "\<And>a. a \<in> T \<Longrightarrow> ((f a) has_integral (i a)) S"
   711   shows "((\<lambda>x. sum (\<lambda>a. f a x) T) has_integral (sum i T)) S"
   712   using assms(1) subset_refl[of T]
   713 proof (induct rule: finite_subset_induct)
   714   case empty
   715   then show ?case by auto
   716 next
   717   case (insert x F)
   718   with assms show ?case
   719     by (simp add: has_integral_add)
   720 qed
   721 
   722 lemma integral_sum:
   723   "\<lbrakk>finite I;  \<And>a. a \<in> I \<Longrightarrow> f a integrable_on S\<rbrakk> \<Longrightarrow>
   724    integral S (\<lambda>x. \<Sum>a\<in>I. f a x) = (\<Sum>a\<in>I. integral S (f a))"
   725   by (simp add: has_integral_sum integrable_integral integral_unique)
   726 
   727 lemma integrable_sum:
   728   "\<lbrakk>finite I;  \<And>a. a \<in> I \<Longrightarrow> f a integrable_on S\<rbrakk> \<Longrightarrow> (\<lambda>x. \<Sum>a\<in>I. f a x) integrable_on S"
   729   unfolding integrable_on_def using has_integral_sum[of I] by metis
   730 
   731 lemma has_integral_eq:
   732   assumes "\<And>x. x \<in> s \<Longrightarrow> f x = g x"
   733     and "(f has_integral k) s"
   734   shows "(g has_integral k) s"
   735   using has_integral_diff[OF assms(2), of "\<lambda>x. f x - g x" 0]
   736   using has_integral_is_0[of s "\<lambda>x. f x - g x"]
   737   using assms(1)
   738   by auto
   739 
   740 lemma integrable_eq: "\<lbrakk>f integrable_on s; \<And>x. x \<in> s \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> g integrable_on s"
   741   unfolding integrable_on_def
   742   using has_integral_eq[of s f g] has_integral_eq by blast
   743 
   744 lemma has_integral_cong:
   745   assumes "\<And>x. x \<in> s \<Longrightarrow> f x = g x"
   746   shows "(f has_integral i) s = (g has_integral i) s"
   747   using has_integral_eq[of s f g] has_integral_eq[of s g f] assms
   748   by auto
   749 
   750 lemma integral_cong:
   751   assumes "\<And>x. x \<in> s \<Longrightarrow> f x = g x"
   752   shows "integral s f = integral s g"
   753   unfolding integral_def
   754 by (metis (full_types, hide_lams) assms has_integral_cong integrable_eq)
   755 
   756 lemma integrable_on_cmult_left_iff [simp]:
   757   assumes "c \<noteq> 0"
   758   shows "(\<lambda>x. of_real c * f x) integrable_on s \<longleftrightarrow> f integrable_on s"
   759         (is "?lhs = ?rhs")
   760 proof
   761   assume ?lhs
   762   then have "(\<lambda>x. of_real (1 / c) * (of_real c * f x)) integrable_on s"
   763     using integrable_cmul[of "\<lambda>x. of_real c * f x" s "1 / of_real c"]
   764     by (simp add: scaleR_conv_of_real)
   765   then have "(\<lambda>x. (of_real (1 / c) * of_real c * f x)) integrable_on s"
   766     by (simp add: algebra_simps)
   767   with \<open>c \<noteq> 0\<close> show ?rhs
   768     by (metis (no_types, lifting) integrable_eq mult.left_neutral nonzero_divide_eq_eq of_real_1 of_real_mult)
   769 qed (blast intro: integrable_on_cmult_left)
   770 
   771 lemma integrable_on_cmult_right:
   772   fixes f :: "_ \<Rightarrow> 'b :: {comm_ring,real_algebra_1,real_normed_vector}"
   773   assumes "f integrable_on s"
   774   shows "(\<lambda>x. f x * of_real c) integrable_on s"
   775 using integrable_on_cmult_left [OF assms] by (simp add: mult.commute)
   776 
   777 lemma integrable_on_cmult_right_iff [simp]:
   778   fixes f :: "_ \<Rightarrow> 'b :: {comm_ring,real_algebra_1,real_normed_vector}"
   779   assumes "c \<noteq> 0"
   780   shows "(\<lambda>x. f x * of_real c) integrable_on s \<longleftrightarrow> f integrable_on s"
   781 using integrable_on_cmult_left_iff [OF assms] by (simp add: mult.commute)
   782 
   783 lemma integrable_on_cdivide:
   784   fixes f :: "_ \<Rightarrow> 'b :: real_normed_field"
   785   assumes "f integrable_on s"
   786   shows "(\<lambda>x. f x / of_real c) integrable_on s"
   787 by (simp add: integrable_on_cmult_right divide_inverse assms flip: of_real_inverse)
   788 
   789 lemma integrable_on_cdivide_iff [simp]:
   790   fixes f :: "_ \<Rightarrow> 'b :: real_normed_field"
   791   assumes "c \<noteq> 0"
   792   shows "(\<lambda>x. f x / of_real c) integrable_on s \<longleftrightarrow> f integrable_on s"
   793 by (simp add: divide_inverse assms flip: of_real_inverse)
   794 
   795 lemma has_integral_null [intro]: "content(cbox a b) = 0 \<Longrightarrow> (f has_integral 0) (cbox a b)"
   796   unfolding has_integral_cbox
   797   using eventually_division_filter_tagged_division[of "cbox a b"]
   798   by (subst tendsto_cong[where g="\<lambda>_. 0"]) (auto elim: eventually_mono intro: sum_content_null)
   799 
   800 lemma has_integral_null_real [intro]: "content {a..b::real} = 0 \<Longrightarrow> (f has_integral 0) {a..b}"
   801   by (metis box_real(2) has_integral_null)
   802 
   803 lemma has_integral_null_eq[simp]: "content (cbox a b) = 0 \<Longrightarrow> (f has_integral i) (cbox a b) \<longleftrightarrow> i = 0"
   804   by (auto simp add: has_integral_null dest!: integral_unique)
   805 
   806 lemma integral_null [simp]: "content (cbox a b) = 0 \<Longrightarrow> integral (cbox a b) f = 0"
   807   by (metis has_integral_null integral_unique)
   808 
   809 lemma integrable_on_null [intro]: "content (cbox a b) = 0 \<Longrightarrow> f integrable_on (cbox a b)"
   810   by (simp add: has_integral_integrable)
   811 
   812 lemma has_integral_empty[intro]: "(f has_integral 0) {}"
   813   by (meson ex_in_conv has_integral_is_0)
   814 
   815 lemma has_integral_empty_eq[simp]: "(f has_integral i) {} \<longleftrightarrow> i = 0"
   816   by (auto simp add: has_integral_empty has_integral_unique)
   817 
   818 lemma integrable_on_empty[intro]: "f integrable_on {}"
   819   unfolding integrable_on_def by auto
   820 
   821 lemma integral_empty[simp]: "integral {} f = 0"
   822   by (rule integral_unique) (rule has_integral_empty)
   823 
   824 lemma has_integral_refl[intro]:
   825   fixes a :: "'a::euclidean_space"
   826   shows "(f has_integral 0) (cbox a a)"
   827     and "(f has_integral 0) {a}"
   828 proof -
   829   show "(f has_integral 0) (cbox a a)"
   830      by (rule has_integral_null) simp
   831   then show "(f has_integral 0) {a}"
   832     by simp
   833 qed
   834 
   835 lemma integrable_on_refl[intro]: "f integrable_on cbox a a"
   836   unfolding integrable_on_def by auto
   837 
   838 lemma integral_refl [simp]: "integral (cbox a a) f = 0"
   839   by (rule integral_unique) auto
   840 
   841 lemma integral_singleton [simp]: "integral {a} f = 0"
   842   by auto
   843 
   844 lemma integral_blinfun_apply:
   845   assumes "f integrable_on s"
   846   shows "integral s (\<lambda>x. blinfun_apply h (f x)) = blinfun_apply h (integral s f)"
   847   by (subst integral_linear[symmetric, OF assms blinfun.bounded_linear_right]) (simp add: o_def)
   848 
   849 lemma blinfun_apply_integral:
   850   assumes "f integrable_on s"
   851   shows "blinfun_apply (integral s f) x = integral s (\<lambda>y. blinfun_apply (f y) x)"
   852   by (metis (no_types, lifting) assms blinfun.prod_left.rep_eq integral_blinfun_apply integral_cong)
   853 
   854 lemma has_integral_componentwise_iff:
   855   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
   856   shows "(f has_integral y) A \<longleftrightarrow> (\<forall>b\<in>Basis. ((\<lambda>x. f x \<bullet> b) has_integral (y \<bullet> b)) A)"
   857 proof safe
   858   fix b :: 'b assume "(f has_integral y) A"
   859   from has_integral_linear[OF this(1) bounded_linear_inner_left, of b]
   860     show "((\<lambda>x. f x \<bullet> b) has_integral (y \<bullet> b)) A" by (simp add: o_def)
   861 next
   862   assume "(\<forall>b\<in>Basis. ((\<lambda>x. f x \<bullet> b) has_integral (y \<bullet> b)) A)"
   863   hence "\<forall>b\<in>Basis. (((\<lambda>x. x *\<^sub>R b) \<circ> (\<lambda>x. f x \<bullet> b)) has_integral ((y \<bullet> b) *\<^sub>R b)) A"
   864     by (intro ballI has_integral_linear) (simp_all add: bounded_linear_scaleR_left)
   865   hence "((\<lambda>x. \<Sum>b\<in>Basis. (f x \<bullet> b) *\<^sub>R b) has_integral (\<Sum>b\<in>Basis. (y \<bullet> b) *\<^sub>R b)) A"
   866     by (intro has_integral_sum) (simp_all add: o_def)
   867   thus "(f has_integral y) A" by (simp add: euclidean_representation)
   868 qed
   869 
   870 lemma has_integral_componentwise:
   871   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
   872   shows "(\<And>b. b \<in> Basis \<Longrightarrow> ((\<lambda>x. f x \<bullet> b) has_integral (y \<bullet> b)) A) \<Longrightarrow> (f has_integral y) A"
   873   by (subst has_integral_componentwise_iff) blast
   874 
   875 lemma integrable_componentwise_iff:
   876   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
   877   shows "f integrable_on A \<longleftrightarrow> (\<forall>b\<in>Basis. (\<lambda>x. f x \<bullet> b) integrable_on A)"
   878 proof
   879   assume "f integrable_on A"
   880   then obtain y where "(f has_integral y) A" by (auto simp: integrable_on_def)
   881   hence "(\<forall>b\<in>Basis. ((\<lambda>x. f x \<bullet> b) has_integral (y \<bullet> b)) A)"
   882     by (subst (asm) has_integral_componentwise_iff)
   883   thus "(\<forall>b\<in>Basis. (\<lambda>x. f x \<bullet> b) integrable_on A)" by (auto simp: integrable_on_def)
   884 next
   885   assume "(\<forall>b\<in>Basis. (\<lambda>x. f x \<bullet> b) integrable_on A)"
   886   then obtain y where "\<forall>b\<in>Basis. ((\<lambda>x. f x \<bullet> b) has_integral y b) A"
   887     unfolding integrable_on_def by (subst (asm) bchoice_iff) blast
   888   hence "\<forall>b\<in>Basis. (((\<lambda>x. x *\<^sub>R b) \<circ> (\<lambda>x. f x \<bullet> b)) has_integral (y b *\<^sub>R b)) A"
   889     by (intro ballI has_integral_linear) (simp_all add: bounded_linear_scaleR_left)
   890   hence "((\<lambda>x. \<Sum>b\<in>Basis. (f x \<bullet> b) *\<^sub>R b) has_integral (\<Sum>b\<in>Basis. y b *\<^sub>R b)) A"
   891     by (intro has_integral_sum) (simp_all add: o_def)
   892   thus "f integrable_on A" by (auto simp: integrable_on_def o_def euclidean_representation)
   893 qed
   894 
   895 lemma integrable_componentwise:
   896   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
   897   shows "(\<And>b. b \<in> Basis \<Longrightarrow> (\<lambda>x. f x \<bullet> b) integrable_on A) \<Longrightarrow> f integrable_on A"
   898   by (subst integrable_componentwise_iff) blast
   899 
   900 lemma integral_componentwise:
   901   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
   902   assumes "f integrable_on A"
   903   shows "integral A f = (\<Sum>b\<in>Basis. integral A (\<lambda>x. (f x \<bullet> b) *\<^sub>R b))"
   904 proof -
   905   from assms have integrable: "\<forall>b\<in>Basis. (\<lambda>x. x *\<^sub>R b) \<circ> (\<lambda>x. (f x \<bullet> b)) integrable_on A"
   906     by (subst (asm) integrable_componentwise_iff, intro integrable_linear ballI)
   907        (simp_all add: bounded_linear_scaleR_left)
   908   have "integral A f = integral A (\<lambda>x. \<Sum>b\<in>Basis. (f x \<bullet> b) *\<^sub>R b)"
   909     by (simp add: euclidean_representation)
   910   also from integrable have "\<dots> = (\<Sum>a\<in>Basis. integral A (\<lambda>x. (f x \<bullet> a) *\<^sub>R a))"
   911     by (subst integral_sum) (simp_all add: o_def)
   912   finally show ?thesis .
   913 qed
   914 
   915 lemma integrable_component:
   916   "f integrable_on A \<Longrightarrow> (\<lambda>x. f x \<bullet> (y :: 'b :: euclidean_space)) integrable_on A"
   917   by (drule integrable_linear[OF _ bounded_linear_inner_left[of y]]) (simp add: o_def)
   918 
   919 
   920 
   921 subsection \<open>Cauchy-type criterion for integrability\<close>
   922 
   923 proposition integrable_Cauchy:
   924   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::{real_normed_vector,complete_space}"
   925   shows "f integrable_on cbox a b \<longleftrightarrow>
   926         (\<forall>e>0. \<exists>\<gamma>. gauge \<gamma> \<and>
   927           (\<forall>\<D>1 \<D>2. \<D>1 tagged_division_of (cbox a b) \<and> \<gamma> fine \<D>1 \<and>
   928             \<D>2 tagged_division_of (cbox a b) \<and> \<gamma> fine \<D>2 \<longrightarrow>
   929             norm ((\<Sum>(x,K)\<in>\<D>1. content K *\<^sub>R f x) - (\<Sum>(x,K)\<in>\<D>2. content K *\<^sub>R f x)) < e))"
   930   (is "?l = (\<forall>e>0. \<exists>\<gamma>. ?P e \<gamma>)")
   931 proof (intro iffI allI impI)
   932   assume ?l
   933   then obtain y
   934     where y: "\<And>e. e > 0 \<Longrightarrow>
   935         \<exists>\<gamma>. gauge \<gamma> \<and>
   936             (\<forall>\<D>. \<D> tagged_division_of cbox a b \<and> \<gamma> fine \<D> \<longrightarrow>
   937                  norm ((\<Sum>(x,K) \<in> \<D>. content K *\<^sub>R f x) - y) < e)"
   938     by (auto simp: integrable_on_def has_integral)
   939   show "\<exists>\<gamma>. ?P e \<gamma>" if "e > 0" for e
   940   proof -
   941     have "e/2 > 0" using that by auto
   942     with y obtain \<gamma> where "gauge \<gamma>"
   943       and \<gamma>: "\<And>\<D>. \<D> tagged_division_of cbox a b \<and> \<gamma> fine \<D> \<Longrightarrow>
   944                   norm ((\<Sum>(x,K)\<in>\<D>. content K *\<^sub>R f x) - y) < e/2"
   945       by meson
   946     show ?thesis
   947     apply (rule_tac x=\<gamma> in exI, clarsimp simp: \<open>gauge \<gamma>\<close>)
   948         by (blast intro!: \<gamma> dist_triangle_half_l[where y=y,unfolded dist_norm])
   949     qed
   950 next
   951   assume "\<forall>e>0. \<exists>\<gamma>. ?P e \<gamma>"
   952   then have "\<forall>n::nat. \<exists>\<gamma>. ?P (1 / (n + 1)) \<gamma>"
   953     by auto
   954   then obtain \<gamma> :: "nat \<Rightarrow> 'n \<Rightarrow> 'n set" where \<gamma>:
   955     "\<And>m. gauge (\<gamma> m)"
   956     "\<And>m \<D>1 \<D>2. \<lbrakk>\<D>1 tagged_division_of cbox a b;
   957               \<gamma> m fine \<D>1; \<D>2 tagged_division_of cbox a b; \<gamma> m fine \<D>2\<rbrakk>
   958               \<Longrightarrow> norm ((\<Sum>(x,K) \<in> \<D>1. content K *\<^sub>R f x) - (\<Sum>(x,K) \<in> \<D>2. content K *\<^sub>R f x))
   959                   < 1 / (m + 1)"
   960     by metis
   961   have "\<And>n. gauge (\<lambda>x. \<Inter>{\<gamma> i x |i. i \<in> {0..n}})"
   962     apply (rule gauge_Inter)
   963     using \<gamma> by auto
   964   then have "\<forall>n. \<exists>p. p tagged_division_of (cbox a b) \<and> (\<lambda>x. \<Inter>{\<gamma> i x |i. i \<in> {0..n}}) fine p"
   965     by (meson fine_division_exists)
   966   then obtain p where p: "\<And>z. p z tagged_division_of cbox a b"
   967                          "\<And>z. (\<lambda>x. \<Inter>{\<gamma> i x |i. i \<in> {0..z}}) fine p z"
   968     by meson
   969   have dp: "\<And>i n. i\<le>n \<Longrightarrow> \<gamma> i fine p n"
   970     using p unfolding fine_Inter
   971     using atLeastAtMost_iff by blast
   972   have "Cauchy (\<lambda>n. sum (\<lambda>(x,K). content K *\<^sub>R (f x)) (p n))"
   973   proof (rule CauchyI)
   974     fix e::real
   975     assume "0 < e"
   976     then obtain N where "N \<noteq> 0" and N: "inverse (real N) < e"
   977       using real_arch_inverse[of e] by blast
   978     show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. norm ((\<Sum>(x,K) \<in> p m. content K *\<^sub>R f x) - (\<Sum>(x,K) \<in> p n. content K *\<^sub>R f x)) < e"
   979     proof (intro exI allI impI)
   980       fix m n
   981       assume mn: "N \<le> m" "N \<le> n"
   982       have "norm ((\<Sum>(x,K) \<in> p m. content K *\<^sub>R f x) - (\<Sum>(x,K) \<in> p n. content K *\<^sub>R f x)) < 1 / (real N + 1)"
   983         by (simp add: p(1) dp mn \<gamma>)
   984       also have "... < e"
   985         using  N \<open>N \<noteq> 0\<close> \<open>0 < e\<close> by (auto simp: field_simps)
   986       finally show "norm ((\<Sum>(x,K) \<in> p m. content K *\<^sub>R f x) - (\<Sum>(x,K) \<in> p n. content K *\<^sub>R f x)) < e" .
   987     qed
   988   qed
   989   then obtain y where y: "\<exists>no. \<forall>n\<ge>no. norm ((\<Sum>(x,K) \<in> p n. content K *\<^sub>R f x) - y) < r" if "r > 0" for r
   990     by (auto simp: convergent_eq_Cauchy[symmetric] dest: LIMSEQ_D)
   991   show ?l
   992     unfolding integrable_on_def has_integral
   993   proof (rule_tac x=y in exI, clarify)
   994     fix e :: real
   995     assume "e>0"
   996     then have e2: "e/2 > 0" by auto
   997     then obtain N1::nat where N1: "N1 \<noteq> 0" "inverse (real N1) < e/2"
   998       using real_arch_inverse by blast
   999     obtain N2::nat where N2: "\<And>n. n \<ge> N2 \<Longrightarrow> norm ((\<Sum>(x,K) \<in> p n. content K *\<^sub>R f x) - y) < e/2"
  1000       using y[OF e2] by metis
  1001     show "\<exists>\<gamma>. gauge \<gamma> \<and>
  1002               (\<forall>\<D>. \<D> tagged_division_of (cbox a b) \<and> \<gamma> fine \<D> \<longrightarrow>
  1003                 norm ((\<Sum>(x,K) \<in> \<D>. content K *\<^sub>R f x) - y) < e)"
  1004     proof (intro exI conjI allI impI)
  1005       show "gauge (\<gamma> (N1+N2))"
  1006         using \<gamma> by auto
  1007       show "norm ((\<Sum>(x,K) \<in> q. content K *\<^sub>R f x) - y) < e"
  1008            if "q tagged_division_of cbox a b \<and> \<gamma> (N1+N2) fine q" for q
  1009       proof (rule norm_triangle_half_r)
  1010         have "norm ((\<Sum>(x,K) \<in> p (N1+N2). content K *\<^sub>R f x) - (\<Sum>(x,K) \<in> q. content K *\<^sub>R f x))
  1011                < 1 / (real (N1+N2) + 1)"
  1012           by (rule \<gamma>; simp add: dp p that)
  1013         also have "... < e/2"
  1014           using N1 \<open>0 < e\<close> by (auto simp: field_simps intro: less_le_trans)
  1015         finally show "norm ((\<Sum>(x,K) \<in> p (N1+N2). content K *\<^sub>R f x) - (\<Sum>(x,K) \<in> q. content K *\<^sub>R f x)) < e/2" .
  1016         show "norm ((\<Sum>(x,K) \<in> p (N1+N2). content K *\<^sub>R f x) - y) < e/2"
  1017           using N2 le_add_same_cancel2 by blast
  1018       qed
  1019     qed
  1020   qed
  1021 qed
  1022 
  1023 
  1024 subsection \<open>Additivity of integral on abutting intervals\<close>
  1025 
  1026 lemma tagged_division_split_left_inj_content:
  1027   assumes \<D>: "\<D> tagged_division_of S"
  1028     and "(x1, K1) \<in> \<D>" "(x2, K2) \<in> \<D>" "K1 \<noteq> K2" "K1 \<inter> {x. x\<bullet>k \<le> c} = K2 \<inter> {x. x\<bullet>k \<le> c}" "k \<in> Basis"
  1029   shows "content (K1 \<inter> {x. x\<bullet>k \<le> c}) = 0"
  1030 proof -
  1031   from tagged_division_ofD(4)[OF \<D> \<open>(x1, K1) \<in> \<D>\<close>] obtain a b where K1: "K1 = cbox a b"
  1032     by auto
  1033   then have "interior (K1 \<inter> {x. x \<bullet> k \<le> c}) = {}"
  1034     by (metis tagged_division_split_left_inj assms)
  1035   then show ?thesis
  1036     unfolding K1 interval_split[OF \<open>k \<in> Basis\<close>] by (auto simp: content_eq_0_interior)
  1037 qed
  1038 
  1039 lemma tagged_division_split_right_inj_content:
  1040   assumes \<D>: "\<D> tagged_division_of S"
  1041     and "(x1, K1) \<in> \<D>" "(x2, K2) \<in> \<D>" "K1 \<noteq> K2" "K1 \<inter> {x. x\<bullet>k \<ge> c} = K2 \<inter> {x. x\<bullet>k \<ge> c}" "k \<in> Basis"
  1042   shows "content (K1 \<inter> {x. x\<bullet>k \<ge> c}) = 0"
  1043 proof -
  1044   from tagged_division_ofD(4)[OF \<D> \<open>(x1, K1) \<in> \<D>\<close>] obtain a b where K1: "K1 = cbox a b"
  1045     by auto
  1046   then have "interior (K1 \<inter> {x. c \<le> x \<bullet> k}) = {}"
  1047     by (metis tagged_division_split_right_inj assms)
  1048   then show ?thesis
  1049     unfolding K1 interval_split[OF \<open>k \<in> Basis\<close>]
  1050     by (auto simp: content_eq_0_interior)
  1051 qed
  1052 
  1053 
  1054 proposition has_integral_split:
  1055   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  1056   assumes fi: "(f has_integral i) (cbox a b \<inter> {x. x\<bullet>k \<le> c})"
  1057       and fj: "(f has_integral j) (cbox a b \<inter> {x. x\<bullet>k \<ge> c})"
  1058       and k: "k \<in> Basis"
  1059 shows "(f has_integral (i + j)) (cbox a b)"
  1060   unfolding has_integral
  1061 proof clarify
  1062   fix e::real
  1063   assume "0 < e"
  1064   then have e: "e/2 > 0"
  1065     by auto
  1066     obtain \<gamma>1 where \<gamma>1: "gauge \<gamma>1"
  1067       and \<gamma>1norm:
  1068         "\<And>\<D>. \<lbrakk>\<D> tagged_division_of cbox a b \<inter> {x. x \<bullet> k \<le> c}; \<gamma>1 fine \<D>\<rbrakk>
  1069              \<Longrightarrow> norm ((\<Sum>(x,K) \<in> \<D>. content K *\<^sub>R f x) - i) < e/2"
  1070        apply (rule has_integralD[OF fi[unfolded interval_split[OF k]] e])
  1071        apply (simp add: interval_split[symmetric] k)
  1072       done
  1073     obtain \<gamma>2 where \<gamma>2: "gauge \<gamma>2"
  1074       and \<gamma>2norm:
  1075         "\<And>\<D>. \<lbrakk>\<D> tagged_division_of cbox a b \<inter> {x. c \<le> x \<bullet> k}; \<gamma>2 fine \<D>\<rbrakk>
  1076              \<Longrightarrow> norm ((\<Sum>(x, k) \<in> \<D>. content k *\<^sub>R f x) - j) < e/2"
  1077        apply (rule has_integralD[OF fj[unfolded interval_split[OF k]] e])
  1078        apply (simp add: interval_split[symmetric] k)
  1079        done
  1080   let ?\<gamma> = "\<lambda>x. if x\<bullet>k = c then (\<gamma>1 x \<inter> \<gamma>2 x) else ball x \<bar>x\<bullet>k - c\<bar> \<inter> \<gamma>1 x \<inter> \<gamma>2 x"
  1081   have "gauge ?\<gamma>"
  1082     using \<gamma>1 \<gamma>2 unfolding gauge_def by auto
  1083   then show "\<exists>\<gamma>. gauge \<gamma> \<and>
  1084                  (\<forall>\<D>. \<D> tagged_division_of cbox a b \<and> \<gamma> fine \<D> \<longrightarrow>
  1085                       norm ((\<Sum>(x, k)\<in>\<D>. content k *\<^sub>R f x) - (i + j)) < e)"
  1086   proof (rule_tac x="?\<gamma>" in exI, safe)
  1087     fix p
  1088     assume p: "p tagged_division_of (cbox a b)" and "?\<gamma> fine p"
  1089     have ab_eqp: "cbox a b = \<Union>{K. \<exists>x. (x, K) \<in> p}"
  1090       using p by blast
  1091     have xk_le_c: "x\<bullet>k \<le> c" if as: "(x,K) \<in> p" and K: "K \<inter> {x. x\<bullet>k \<le> c} \<noteq> {}" for x K
  1092     proof (rule ccontr)
  1093       assume **: "\<not> x \<bullet> k \<le> c"
  1094       then have "K \<subseteq> ball x \<bar>x \<bullet> k - c\<bar>"
  1095         using \<open>?\<gamma> fine p\<close> as by (fastforce simp: not_le algebra_simps)
  1096       with K obtain y where y: "y \<in> ball x \<bar>x \<bullet> k - c\<bar>" "y\<bullet>k \<le> c"
  1097         by blast
  1098       then have "\<bar>x \<bullet> k - y \<bullet> k\<bar> < \<bar>x \<bullet> k - c\<bar>"
  1099         using Basis_le_norm[OF k, of "x - y"]
  1100         by (auto simp add: dist_norm inner_diff_left intro: le_less_trans)
  1101       with y show False
  1102         using ** by (auto simp add: field_simps)
  1103     qed
  1104     have xk_ge_c: "x\<bullet>k \<ge> c" if as: "(x,K) \<in> p" and K: "K \<inter> {x. x\<bullet>k \<ge> c} \<noteq> {}" for x K
  1105     proof (rule ccontr)
  1106       assume **: "\<not> x \<bullet> k \<ge> c"
  1107       then have "K \<subseteq> ball x \<bar>x \<bullet> k - c\<bar>"
  1108         using \<open>?\<gamma> fine p\<close> as by (fastforce simp: not_le algebra_simps)
  1109       with K obtain y where y: "y \<in> ball x \<bar>x \<bullet> k - c\<bar>" "y\<bullet>k \<ge> c"
  1110         by blast
  1111       then have "\<bar>x \<bullet> k - y \<bullet> k\<bar> < \<bar>x \<bullet> k - c\<bar>"
  1112         using Basis_le_norm[OF k, of "x - y"]
  1113         by (auto simp add: dist_norm inner_diff_left intro: le_less_trans)
  1114       with y show False
  1115         using ** by (auto simp add: field_simps)
  1116     qed
  1117     have fin_finite: "finite {(x,f K) | x K. (x,K) \<in> s \<and> P x K}"
  1118       if "finite s" for s and f :: "'a set \<Rightarrow> 'a set" and P :: "'a \<Rightarrow> 'a set \<Rightarrow> bool"
  1119     proof -
  1120       from that have "finite ((\<lambda>(x,K). (x, f K)) ` s)"
  1121         by auto
  1122       then show ?thesis
  1123         by (rule rev_finite_subset) auto
  1124     qed
  1125     { fix \<G> :: "'a set \<Rightarrow> 'a set"
  1126       fix i :: "'a \<times> 'a set"
  1127       assume "i \<in> (\<lambda>(x, k). (x, \<G> k)) ` p - {(x, \<G> k) |x k. (x, k) \<in> p \<and> \<G> k \<noteq> {}}"
  1128       then obtain x K where xk: "i = (x, \<G> K)"  "(x,K) \<in> p"
  1129                                  "(x, \<G> K) \<notin> {(x, \<G> K) |x K. (x,K) \<in> p \<and> \<G> K \<noteq> {}}"
  1130         by auto
  1131       have "content (\<G> K) = 0"
  1132         using xk using content_empty by auto
  1133       then have "(\<lambda>(x,K). content K *\<^sub>R f x) i = 0"
  1134         unfolding xk split_conv by auto
  1135     } note [simp] = this
  1136     have "finite p"
  1137       using p by blast
  1138     let ?M1 = "{(x, K \<inter> {x. x\<bullet>k \<le> c}) |x K. (x,K) \<in> p \<and> K \<inter> {x. x\<bullet>k \<le> c} \<noteq> {}}"
  1139     have \<gamma>1_fine: "\<gamma>1 fine ?M1"
  1140       using \<open>?\<gamma> fine p\<close> by (fastforce simp: fine_def split: if_split_asm)
  1141     have "norm ((\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) - i) < e/2"
  1142     proof (rule \<gamma>1norm [OF tagged_division_ofI \<gamma>1_fine])
  1143       show "finite ?M1"
  1144         by (rule fin_finite) (use p in blast)
  1145       show "\<Union>{k. \<exists>x. (x, k) \<in> ?M1} = cbox a b \<inter> {x. x\<bullet>k \<le> c}"
  1146         by (auto simp: ab_eqp)
  1147 
  1148       fix x L
  1149       assume xL: "(x, L) \<in> ?M1"
  1150       then obtain x' L' where xL': "x = x'" "L = L' \<inter> {x. x \<bullet> k \<le> c}"
  1151                                    "(x', L') \<in> p" "L' \<inter> {x. x \<bullet> k \<le> c} \<noteq> {}"
  1152         by blast
  1153       then obtain a' b' where ab': "L' = cbox a' b'"
  1154         using p by blast
  1155       show "x \<in> L" "L \<subseteq> cbox a b \<inter> {x. x \<bullet> k \<le> c}"
  1156         using p xk_le_c xL' by auto
  1157       show "\<exists>a b. L = cbox a b"
  1158         using p xL' ab' by (auto simp add: interval_split[OF k,where c=c])
  1159 
  1160       fix y R
  1161       assume yR: "(y, R) \<in> ?M1"
  1162       then obtain y' R' where yR': "y = y'" "R = R' \<inter> {x. x \<bullet> k \<le> c}"
  1163                                    "(y', R') \<in> p" "R' \<inter> {x. x \<bullet> k \<le> c} \<noteq> {}"
  1164         by blast
  1165       assume as: "(x, L) \<noteq> (y, R)"
  1166       show "interior L \<inter> interior R = {}"
  1167       proof (cases "L' = R' \<longrightarrow> x' = y'")
  1168         case False
  1169         have "interior R' = {}"
  1170           by (metis (no_types) False Pair_inject inf.idem tagged_division_ofD(5) [OF p] xL'(3) yR'(3))
  1171         then show ?thesis
  1172           using yR' by simp
  1173       next
  1174         case True
  1175         then have "L' \<noteq> R'"
  1176           using as unfolding xL' yR' by auto
  1177         have "interior L' \<inter> interior R' = {}"
  1178           by (metis (no_types) Pair_inject \<open>L' \<noteq> R'\<close> p tagged_division_ofD(5) xL'(3) yR'(3))
  1179         then show ?thesis
  1180           using xL'(2) yR'(2) by auto
  1181       qed
  1182     qed
  1183     moreover
  1184     let ?M2 = "{(x,K \<inter> {x. x\<bullet>k \<ge> c}) |x K. (x,K) \<in> p \<and> K \<inter> {x. x\<bullet>k \<ge> c} \<noteq> {}}"
  1185     have \<gamma>2_fine: "\<gamma>2 fine ?M2"
  1186       using \<open>?\<gamma> fine p\<close> by (fastforce simp: fine_def split: if_split_asm)
  1187     have "norm ((\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - j) < e/2"
  1188     proof (rule \<gamma>2norm [OF tagged_division_ofI \<gamma>2_fine])
  1189       show "finite ?M2"
  1190         by (rule fin_finite) (use p in blast)
  1191       show "\<Union>{k. \<exists>x. (x, k) \<in> ?M2} = cbox a b \<inter> {x. x\<bullet>k \<ge> c}"
  1192         by (auto simp: ab_eqp)
  1193 
  1194       fix x L
  1195       assume xL: "(x, L) \<in> ?M2"
  1196       then obtain x' L' where xL': "x = x'" "L = L' \<inter> {x. x \<bullet> k \<ge> c}"
  1197                                    "(x', L') \<in> p" "L' \<inter> {x. x \<bullet> k \<ge> c} \<noteq> {}"
  1198         by blast
  1199       then obtain a' b' where ab': "L' = cbox a' b'"
  1200         using p by blast
  1201       show "x \<in> L" "L \<subseteq> cbox a b \<inter> {x. x \<bullet> k \<ge> c}"
  1202         using p xk_ge_c xL' by auto
  1203       show "\<exists>a b. L = cbox a b"
  1204         using p xL' ab' by (auto simp add: interval_split[OF k,where c=c])
  1205 
  1206       fix y R
  1207       assume yR: "(y, R) \<in> ?M2"
  1208       then obtain y' R' where yR': "y = y'" "R = R' \<inter> {x. x \<bullet> k \<ge> c}"
  1209                                    "(y', R') \<in> p" "R' \<inter> {x. x \<bullet> k \<ge> c} \<noteq> {}"
  1210         by blast
  1211       assume as: "(x, L) \<noteq> (y, R)"
  1212       show "interior L \<inter> interior R = {}"
  1213       proof (cases "L' = R' \<longrightarrow> x' = y'")
  1214         case False
  1215         have "interior R' = {}"
  1216           by (metis (no_types) False Pair_inject inf.idem tagged_division_ofD(5) [OF p] xL'(3) yR'(3))
  1217         then show ?thesis
  1218           using yR' by simp
  1219       next
  1220         case True
  1221         then have "L' \<noteq> R'"
  1222           using as unfolding xL' yR' by auto
  1223         have "interior L' \<inter> interior R' = {}"
  1224           by (metis (no_types) Pair_inject \<open>L' \<noteq> R'\<close> p tagged_division_ofD(5) xL'(3) yR'(3))
  1225         then show ?thesis
  1226           using xL'(2) yR'(2) by auto
  1227       qed
  1228     qed
  1229     ultimately
  1230     have "norm (((\<Sum>(x,K) \<in> ?M1. content K *\<^sub>R f x) - i) + ((\<Sum>(x,K) \<in> ?M2. content K *\<^sub>R f x) - j)) < e/2 + e/2"
  1231       using norm_add_less by blast
  1232     moreover have "((\<Sum>(x,K) \<in> ?M1. content K *\<^sub>R f x) - i) +
  1233                    ((\<Sum>(x,K) \<in> ?M2. content K *\<^sub>R f x) - j) =
  1234                    (\<Sum>(x, ka)\<in>p. content ka *\<^sub>R f x) - (i + j)"
  1235     proof -
  1236       have eq0: "\<And>x y. x = (0::real) \<Longrightarrow> x *\<^sub>R (y::'b) = 0"
  1237          by auto
  1238       have cont_eq: "\<And>g. (\<lambda>(x,l). content l *\<^sub>R f x) \<circ> (\<lambda>(x,l). (x,g l)) = (\<lambda>(x,l). content (g l) *\<^sub>R f x)"
  1239         by auto
  1240       have *: "\<And>\<G> :: 'a set \<Rightarrow> 'a set.
  1241                   (\<Sum>(x,K)\<in>{(x, \<G> K) |x K. (x,K) \<in> p \<and> \<G> K \<noteq> {}}. content K *\<^sub>R f x) =
  1242                   (\<Sum>(x,K)\<in>(\<lambda>(x,K). (x, \<G> K)) ` p. content K *\<^sub>R f x)"
  1243         by (rule sum.mono_neutral_left) (auto simp: \<open>finite p\<close>)
  1244       have "((\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) - i) + ((\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - j) =
  1245         (\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - (i + j)"
  1246         by auto
  1247       moreover have "\<dots> = (\<Sum>(x,K) \<in> p. content (K \<inter> {x. x \<bullet> k \<le> c}) *\<^sub>R f x) +
  1248         (\<Sum>(x,K) \<in> p. content (K \<inter> {x. c \<le> x \<bullet> k}) *\<^sub>R f x) - (i + j)"
  1249         unfolding *
  1250         apply (subst (1 2) sum.reindex_nontrivial)
  1251            apply (auto intro!: k p eq0 tagged_division_split_left_inj_content tagged_division_split_right_inj_content
  1252                        simp: cont_eq \<open>finite p\<close>)
  1253         done
  1254       moreover have "\<And>x. x \<in> p \<Longrightarrow> (\<lambda>(a,B). content (B \<inter> {a. a \<bullet> k \<le> c}) *\<^sub>R f a) x +
  1255                                 (\<lambda>(a,B). content (B \<inter> {a. c \<le> a \<bullet> k}) *\<^sub>R f a) x =
  1256                                 (\<lambda>(a,B). content B *\<^sub>R f a) x"
  1257       proof clarify
  1258         fix a B
  1259         assume "(a, B) \<in> p"
  1260         with p obtain u v where uv: "B = cbox u v" by blast
  1261         then show "content (B \<inter> {x. x \<bullet> k \<le> c}) *\<^sub>R f a + content (B \<inter> {x. c \<le> x \<bullet> k}) *\<^sub>R f a = content B *\<^sub>R f a"
  1262           by (auto simp: scaleR_left_distrib uv content_split[OF k,of u v c])
  1263       qed
  1264       ultimately show ?thesis
  1265         by (auto simp: sum.distrib[symmetric])
  1266       qed
  1267     ultimately show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - (i + j)) < e"
  1268       by auto
  1269   qed
  1270 qed
  1271 
  1272 
  1273 subsection \<open>A sort of converse, integrability on subintervals\<close>
  1274 
  1275 lemma has_integral_separate_sides:
  1276   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  1277   assumes f: "(f has_integral i) (cbox a b)"
  1278     and "e > 0"
  1279     and k: "k \<in> Basis"
  1280   obtains d where "gauge d"
  1281     "\<forall>p1 p2. p1 tagged_division_of (cbox a b \<inter> {x. x\<bullet>k \<le> c}) \<and> d fine p1 \<and>
  1282         p2 tagged_division_of (cbox a b \<inter> {x. x\<bullet>k \<ge> c}) \<and> d fine p2 \<longrightarrow>
  1283         norm ((sum (\<lambda>(x,k). content k *\<^sub>R f x) p1 + sum (\<lambda>(x,k). content k *\<^sub>R f x) p2) - i) < e"
  1284 proof -
  1285   obtain \<gamma> where d: "gauge \<gamma>"
  1286       "\<And>p. \<lbrakk>p tagged_division_of cbox a b; \<gamma> fine p\<rbrakk>
  1287             \<Longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - i) < e"
  1288     using has_integralD[OF f \<open>e > 0\<close>] by metis
  1289   { fix p1 p2
  1290     assume tdiv1: "p1 tagged_division_of (cbox a b) \<inter> {x. x \<bullet> k \<le> c}" and "\<gamma> fine p1"
  1291     note p1=tagged_division_ofD[OF this(1)] 
  1292     assume tdiv2: "p2 tagged_division_of (cbox a b) \<inter> {x. c \<le> x \<bullet> k}" and "\<gamma> fine p2"
  1293     note p2=tagged_division_ofD[OF this(1)] 
  1294     note tagged_division_Un_interval[OF tdiv1 tdiv2] 
  1295     note p12 = tagged_division_ofD[OF this] this
  1296     { fix a b
  1297       assume ab: "(a, b) \<in> p1 \<inter> p2"
  1298       have "(a, b) \<in> p1"
  1299         using ab by auto
  1300       obtain u v where uv: "b = cbox u v"
  1301         using \<open>(a, b) \<in> p1\<close> p1(4) by moura
  1302       have "b \<subseteq> {x. x\<bullet>k = c}"
  1303         using ab p1(3)[of a b] p2(3)[of a b] by fastforce
  1304       moreover
  1305       have "interior {x::'a. x \<bullet> k = c} = {}"
  1306       proof (rule ccontr)
  1307         assume "\<not> ?thesis"
  1308         then obtain x where x: "x \<in> interior {x::'a. x\<bullet>k = c}"
  1309           by auto
  1310         then obtain \<epsilon> where "0 < \<epsilon>" and \<epsilon>: "ball x \<epsilon> \<subseteq> {x. x \<bullet> k = c}"
  1311           using mem_interior by metis
  1312         have x: "x\<bullet>k = c"
  1313           using x interior_subset by fastforce
  1314         have *: "\<And>i. i \<in> Basis \<Longrightarrow> \<bar>(x - (x + (\<epsilon>/2) *\<^sub>R k)) \<bullet> i\<bar> = (if i = k then \<epsilon>/2 else 0)"
  1315           using \<open>0 < \<epsilon>\<close> k by (auto simp: inner_simps inner_not_same_Basis)
  1316         have "(\<Sum>i\<in>Basis. \<bar>(x - (x + (\<epsilon>/2 ) *\<^sub>R k)) \<bullet> i\<bar>) =
  1317               (\<Sum>i\<in>Basis. (if i = k then \<epsilon>/2 else 0))"
  1318           using "*" by (blast intro: sum.cong)
  1319         also have "\<dots> < \<epsilon>"
  1320           by (subst sum.delta) (use \<open>0 < \<epsilon>\<close> in auto)
  1321         finally have "x + (\<epsilon>/2) *\<^sub>R k \<in> ball x \<epsilon>"
  1322           unfolding mem_ball dist_norm by(rule le_less_trans[OF norm_le_l1])
  1323         then have "x + (\<epsilon>/2) *\<^sub>R k \<in> {x. x\<bullet>k = c}"
  1324           using \<epsilon> by auto
  1325         then show False
  1326           using \<open>0 < \<epsilon>\<close> x k by (auto simp: inner_simps)
  1327       qed
  1328       ultimately have "content b = 0"
  1329         unfolding uv content_eq_0_interior
  1330         using interior_mono by blast
  1331       then have "content b *\<^sub>R f a = 0"
  1332         by auto
  1333     }
  1334     then have "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x) - i) =
  1335                norm ((\<Sum>(x, k)\<in>p1 \<union> p2. content k *\<^sub>R f x) - i)"
  1336       by (subst sum.union_inter_neutral) (auto simp: p1 p2)
  1337     also have "\<dots> < e"
  1338       using d(2) p12 by (simp add: fine_Un k \<open>\<gamma> fine p1\<close> \<open>\<gamma> fine p2\<close>)
  1339     finally have "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x) - i) < e" .
  1340    }
  1341   then show ?thesis
  1342     using d(1) that by auto
  1343 qed
  1344 
  1345 lemma integrable_split [intro]:
  1346   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::{real_normed_vector,complete_space}"
  1347   assumes f: "f integrable_on cbox a b"
  1348       and k: "k \<in> Basis"
  1349     shows "f integrable_on (cbox a b \<inter> {x. x\<bullet>k \<le> c})"   (is ?thesis1)
  1350     and   "f integrable_on (cbox a b \<inter> {x. x\<bullet>k \<ge> c})"   (is ?thesis2)
  1351 proof -
  1352   obtain y where y: "(f has_integral y) (cbox a b)"
  1353     using f by blast
  1354   define a' where "a' = (\<Sum>i\<in>Basis. (if i = k then max (a\<bullet>k) c else a\<bullet>i)*\<^sub>R i)"
  1355   define b' where "b' = (\<Sum>i\<in>Basis. (if i = k then min (b\<bullet>k) c else b\<bullet>i)*\<^sub>R i)"
  1356   have "\<exists>d. gauge d \<and>
  1357             (\<forall>p1 p2. p1 tagged_division_of cbox a b \<inter> {x. x \<bullet> k \<le> c} \<and> d fine p1 \<and>
  1358                      p2 tagged_division_of cbox a b \<inter> {x. x \<bullet> k \<le> c} \<and> d fine p2 \<longrightarrow>
  1359                      norm ((\<Sum>(x,K) \<in> p1. content K *\<^sub>R f x) - (\<Sum>(x,K) \<in> p2. content K *\<^sub>R f x)) < e)"
  1360     if "e > 0" for e
  1361   proof -
  1362     have "e/2 > 0" using that by auto
  1363   with has_integral_separate_sides[OF y this k, of c]
  1364   obtain d
  1365     where "gauge d"
  1366          and d: "\<And>p1 p2. \<lbrakk>p1 tagged_division_of cbox a b \<inter> {x. x \<bullet> k \<le> c}; d fine p1;
  1367                           p2 tagged_division_of cbox a b \<inter> {x. c \<le> x \<bullet> k}; d fine p2\<rbrakk>
  1368                   \<Longrightarrow> norm ((\<Sum>(x,K)\<in>p1. content K *\<^sub>R f x) + (\<Sum>(x,K)\<in>p2. content K *\<^sub>R f x) - y) < e/2"
  1369     by metis
  1370   show ?thesis
  1371     proof (rule_tac x=d in exI, clarsimp simp add: \<open>gauge d\<close>)
  1372       fix p1 p2
  1373       assume as: "p1 tagged_division_of (cbox a b) \<inter> {x. x \<bullet> k \<le> c}" "d fine p1"
  1374                  "p2 tagged_division_of (cbox a b) \<inter> {x. x \<bullet> k \<le> c}" "d fine p2"
  1375       show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e"
  1376       proof (rule fine_division_exists[OF \<open>gauge d\<close>, of a' b])
  1377         fix p
  1378         assume "p tagged_division_of cbox a' b" "d fine p"
  1379         then show ?thesis
  1380           using as norm_triangle_half_l[OF d[of p1 p] d[of p2 p]]
  1381           unfolding interval_split[OF k] b'_def[symmetric] a'_def[symmetric]
  1382           by (auto simp add: algebra_simps)
  1383       qed
  1384     qed
  1385   qed
  1386   with f show ?thesis1
  1387     by (simp add: interval_split[OF k] integrable_Cauchy)
  1388   have "\<exists>d. gauge d \<and>
  1389             (\<forall>p1 p2. p1 tagged_division_of cbox a b \<inter> {x. x \<bullet> k \<ge> c} \<and> d fine p1 \<and>
  1390                      p2 tagged_division_of cbox a b \<inter> {x. x \<bullet> k \<ge> c} \<and> d fine p2 \<longrightarrow>
  1391                      norm ((\<Sum>(x,K) \<in> p1. content K *\<^sub>R f x) - (\<Sum>(x,K) \<in> p2. content K *\<^sub>R f x)) < e)"
  1392     if "e > 0" for e
  1393   proof -
  1394     have "e/2 > 0" using that by auto
  1395   with has_integral_separate_sides[OF y this k, of c]
  1396   obtain d
  1397     where "gauge d"
  1398          and d: "\<And>p1 p2. \<lbrakk>p1 tagged_division_of cbox a b \<inter> {x. x \<bullet> k \<le> c}; d fine p1;
  1399                           p2 tagged_division_of cbox a b \<inter> {x. c \<le> x \<bullet> k}; d fine p2\<rbrakk>
  1400                   \<Longrightarrow> norm ((\<Sum>(x,K)\<in>p1. content K *\<^sub>R f x) + (\<Sum>(x,K)\<in>p2. content K *\<^sub>R f x) - y) < e/2"
  1401     by metis
  1402   show ?thesis
  1403     proof (rule_tac x=d in exI, clarsimp simp add: \<open>gauge d\<close>)
  1404       fix p1 p2
  1405       assume as: "p1 tagged_division_of (cbox a b) \<inter> {x. x \<bullet> k \<ge> c}" "d fine p1"
  1406                  "p2 tagged_division_of (cbox a b) \<inter> {x. x \<bullet> k \<ge> c}" "d fine p2"
  1407       show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e"
  1408       proof (rule fine_division_exists[OF \<open>gauge d\<close>, of a b'])
  1409         fix p
  1410         assume "p tagged_division_of cbox a b'" "d fine p"
  1411         then show ?thesis
  1412           using as norm_triangle_half_l[OF d[of p p1] d[of p p2]]
  1413           unfolding interval_split[OF k] b'_def[symmetric] a'_def[symmetric]
  1414           by (auto simp add: algebra_simps)
  1415       qed
  1416     qed
  1417   qed
  1418   with f show ?thesis2
  1419     by (simp add: interval_split[OF k] integrable_Cauchy)
  1420 qed
  1421 
  1422 lemma operative_integralI:
  1423   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
  1424   shows "operative (lift_option (+)) (Some 0)
  1425     (\<lambda>i. if f integrable_on i then Some (integral i f) else None)"
  1426 proof -
  1427   interpret comm_monoid "lift_option plus" "Some (0::'b)"
  1428     by (rule comm_monoid_lift_option)
  1429       (rule add.comm_monoid_axioms)
  1430   show ?thesis
  1431   proof
  1432     fix a b c
  1433     fix k :: 'a
  1434     assume k: "k \<in> Basis"
  1435     show "(if f integrable_on cbox a b then Some (integral (cbox a b) f) else None) =
  1436           lift_option (+) (if f integrable_on cbox a b \<inter> {x. x \<bullet> k \<le> c} then Some (integral (cbox a b \<inter> {x. x \<bullet> k \<le> c}) f) else None)
  1437           (if f integrable_on cbox a b \<inter> {x. c \<le> x \<bullet> k} then Some (integral (cbox a b \<inter> {x. c \<le> x \<bullet> k}) f) else None)"
  1438     proof (cases "f integrable_on cbox a b")
  1439       case True
  1440       with k show ?thesis
  1441         apply (simp add: integrable_split)
  1442         apply (rule integral_unique [OF has_integral_split[OF _ _ k]])
  1443         apply (auto intro: integrable_integral)
  1444         done
  1445     next
  1446     case False
  1447       have "\<not> (f integrable_on cbox a b \<inter> {x. x \<bullet> k \<le> c}) \<or> \<not> ( f integrable_on cbox a b \<inter> {x. c \<le> x \<bullet> k})"
  1448       proof (rule ccontr)
  1449         assume "\<not> ?thesis"
  1450         then have "f integrable_on cbox a b"
  1451           unfolding integrable_on_def
  1452           apply (rule_tac x="integral (cbox a b \<inter> {x. x \<bullet> k \<le> c}) f + integral (cbox a b \<inter> {x. x \<bullet> k \<ge> c}) f" in exI)
  1453           apply (rule has_integral_split[OF _ _ k])
  1454           apply (auto intro: integrable_integral)
  1455           done
  1456         then show False
  1457           using False by auto
  1458       qed
  1459       then show ?thesis
  1460         using False by auto
  1461     qed
  1462   next
  1463     fix a b :: 'a
  1464     assume "box a b = {}"
  1465     then show "(if f integrable_on cbox a b then Some (integral (cbox a b) f) else None) = Some 0"
  1466       using has_integral_null_eq
  1467       by (auto simp: integrable_on_null content_eq_0_interior)
  1468   qed
  1469 qed
  1470 
  1471 subsection \<open>Bounds on the norm of Riemann sums and the integral itself\<close>
  1472 
  1473 lemma dsum_bound:
  1474   assumes "p division_of (cbox a b)"
  1475     and "norm c \<le> e"
  1476   shows "norm (sum (\<lambda>l. content l *\<^sub>R c) p) \<le> e * content(cbox a b)"
  1477 proof -
  1478   have sumeq: "(\<Sum>i\<in>p. \<bar>content i\<bar>) = sum content p"
  1479     apply (rule sum.cong)
  1480     using assms
  1481     apply simp
  1482     apply (metis abs_of_nonneg assms(1) content_pos_le division_ofD(4))
  1483     done
  1484   have e: "0 \<le> e"
  1485     using assms(2) norm_ge_zero order_trans by blast
  1486   have "norm (sum (\<lambda>l. content l *\<^sub>R c) p) \<le> (\<Sum>i\<in>p. norm (content i *\<^sub>R c))"
  1487     using norm_sum by blast
  1488   also have "...  \<le> e * (\<Sum>i\<in>p. \<bar>content i\<bar>)"
  1489     by (simp add: sum_distrib_left[symmetric] mult.commute assms(2) mult_right_mono sum_nonneg)
  1490   also have "... \<le> e * content (cbox a b)"
  1491     apply (rule mult_left_mono [OF _ e])
  1492     apply (simp add: sumeq)
  1493     using additive_content_division assms(1) eq_iff apply blast
  1494     done
  1495   finally show ?thesis .
  1496 qed
  1497 
  1498 lemma rsum_bound:
  1499   assumes p: "p tagged_division_of (cbox a b)"
  1500       and "\<forall>x\<in>cbox a b. norm (f x) \<le> e"
  1501     shows "norm (sum (\<lambda>(x,k). content k *\<^sub>R f x) p) \<le> e * content (cbox a b)"
  1502 proof (cases "cbox a b = {}")
  1503   case True show ?thesis
  1504     using p unfolding True tagged_division_of_trivial by auto
  1505 next
  1506   case False
  1507   then have e: "e \<ge> 0"
  1508     by (meson ex_in_conv assms(2) norm_ge_zero order_trans)
  1509   have sum_le: "sum (content \<circ> snd) p \<le> content (cbox a b)"
  1510     unfolding additive_content_tagged_division[OF p, symmetric] split_def
  1511     by (auto intro: eq_refl)
  1512   have con: "\<And>xk. xk \<in> p \<Longrightarrow> 0 \<le> content (snd xk)"
  1513     using tagged_division_ofD(4) [OF p] content_pos_le
  1514     by force
  1515   have norm: "\<And>xk. xk \<in> p \<Longrightarrow> norm (f (fst xk)) \<le> e"
  1516     unfolding fst_conv using tagged_division_ofD(2,3)[OF p] assms
  1517     by (metis prod.collapse subset_eq)
  1518   have "norm (sum (\<lambda>(x,k). content k *\<^sub>R f x) p) \<le> (\<Sum>i\<in>p. norm (case i of (x, k) \<Rightarrow> content k *\<^sub>R f x))"
  1519     by (rule norm_sum)
  1520   also have "...  \<le> e * content (cbox a b)"
  1521     unfolding split_def norm_scaleR
  1522     apply (rule order_trans[OF sum_mono])
  1523     apply (rule mult_left_mono[OF _ abs_ge_zero, of _ e])
  1524     apply (metis norm)
  1525     unfolding sum_distrib_right[symmetric]
  1526     using con sum_le
  1527     apply (auto simp: mult.commute intro: mult_left_mono [OF _ e])
  1528     done
  1529   finally show ?thesis .
  1530 qed
  1531 
  1532 lemma rsum_diff_bound:
  1533   assumes "p tagged_division_of (cbox a b)"
  1534     and "\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e"
  1535   shows "norm (sum (\<lambda>(x,k). content k *\<^sub>R f x) p - sum (\<lambda>(x,k). content k *\<^sub>R g x) p) \<le>
  1536          e * content (cbox a b)"
  1537   apply (rule order_trans[OF _ rsum_bound[OF assms]])
  1538   apply (simp add: split_def scaleR_diff_right sum_subtractf eq_refl)
  1539   done
  1540 
  1541 lemma has_integral_bound:
  1542   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  1543   assumes "0 \<le> B"
  1544       and f: "(f has_integral i) (cbox a b)"
  1545       and "\<And>x. x\<in>cbox a b \<Longrightarrow> norm (f x) \<le> B"
  1546     shows "norm i \<le> B * content (cbox a b)"
  1547 proof (rule ccontr)
  1548   assume "\<not> ?thesis"
  1549   then have "norm i - B * content (cbox a b) > 0"
  1550     by auto
  1551   with f[unfolded has_integral]
  1552   obtain \<gamma> where "gauge \<gamma>" and \<gamma>:
  1553     "\<And>p. \<lbrakk>p tagged_division_of cbox a b; \<gamma> fine p\<rbrakk>
  1554           \<Longrightarrow> norm ((\<Sum>(x, K)\<in>p. content K *\<^sub>R f x) - i) < norm i - B * content (cbox a b)"
  1555     by metis
  1556   then obtain p where p: "p tagged_division_of cbox a b" and "\<gamma> fine p"
  1557     using fine_division_exists by blast
  1558   have "\<And>s B. norm s \<le> B \<Longrightarrow> \<not> norm (s - i) < norm i - B"
  1559     unfolding not_less
  1560     by (metis diff_left_mono dist_commute dist_norm norm_triangle_ineq2 order_trans)
  1561   then show False
  1562     using \<gamma> [OF p \<open>\<gamma> fine p\<close>] rsum_bound[OF p] assms by metis
  1563 qed
  1564 
  1565 corollary integrable_bound:
  1566   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  1567   assumes "0 \<le> B"
  1568       and "f integrable_on (cbox a b)"
  1569       and "\<And>x. x\<in>cbox a b \<Longrightarrow> norm (f x) \<le> B"
  1570     shows "norm (integral (cbox a b) f) \<le> B * content (cbox a b)"
  1571 by (metis integrable_integral has_integral_bound assms)
  1572 
  1573 
  1574 subsection \<open>Similar theorems about relationship among components\<close>
  1575 
  1576 lemma rsum_component_le:
  1577   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1578   assumes p: "p tagged_division_of (cbox a b)"
  1579       and "\<And>x. x \<in> cbox a b \<Longrightarrow> (f x)\<bullet>i \<le> (g x)\<bullet>i"
  1580     shows "(\<Sum>(x, K)\<in>p. content K *\<^sub>R f x) \<bullet> i \<le> (\<Sum>(x, K)\<in>p. content K *\<^sub>R g x) \<bullet> i"
  1581 unfolding inner_sum_left
  1582 proof (rule sum_mono, clarify)
  1583   fix x K
  1584   assume ab: "(x, K) \<in> p"
  1585   with p obtain u v where K: "K = cbox u v"
  1586     by blast
  1587   then show "(content K *\<^sub>R f x) \<bullet> i \<le> (content K *\<^sub>R g x) \<bullet> i"
  1588     by (metis ab assms inner_scaleR_left measure_nonneg mult_left_mono tag_in_interval)
  1589 qed
  1590 
  1591 lemma has_integral_component_le:
  1592   fixes f g :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1593   assumes k: "k \<in> Basis"
  1594   assumes "(f has_integral i) S" "(g has_integral j) S"
  1595     and f_le_g: "\<And>x. x \<in> S \<Longrightarrow> (f x)\<bullet>k \<le> (g x)\<bullet>k"
  1596   shows "i\<bullet>k \<le> j\<bullet>k"
  1597 proof -
  1598   have ik_le_jk: "i\<bullet>k \<le> j\<bullet>k"
  1599     if f_i: "(f has_integral i) (cbox a b)"
  1600     and g_j: "(g has_integral j) (cbox a b)"
  1601     and le: "\<forall>x\<in>cbox a b. (f x)\<bullet>k \<le> (g x)\<bullet>k"
  1602     for a b i and j :: 'b and f g :: "'a \<Rightarrow> 'b"
  1603   proof (rule ccontr)
  1604     assume "\<not> ?thesis"
  1605     then have *: "0 < (i\<bullet>k - j\<bullet>k) / 3"
  1606       by auto
  1607     obtain \<gamma>1 where "gauge \<gamma>1" 
  1608       and \<gamma>1: "\<And>p. \<lbrakk>p tagged_division_of cbox a b; \<gamma>1 fine p\<rbrakk>
  1609                 \<Longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - i) < (i \<bullet> k - j \<bullet> k) / 3"
  1610       using f_i[unfolded has_integral,rule_format,OF *] by fastforce 
  1611     obtain \<gamma>2 where "gauge \<gamma>2" 
  1612       and \<gamma>2: "\<And>p. \<lbrakk>p tagged_division_of cbox a b; \<gamma>2 fine p\<rbrakk>
  1613                 \<Longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R g x) - j) < (i \<bullet> k - j \<bullet> k) / 3"
  1614       using g_j[unfolded has_integral,rule_format,OF *] by fastforce 
  1615     obtain p where p: "p tagged_division_of cbox a b" and "\<gamma>1 fine p" "\<gamma>2 fine p"
  1616        using fine_division_exists[OF gauge_Int[OF \<open>gauge \<gamma>1\<close> \<open>gauge \<gamma>2\<close>], of a b] unfolding fine_Int
  1617        by metis
  1618     then have "\<bar>((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - i) \<bullet> k\<bar> < (i \<bullet> k - j \<bullet> k) / 3"
  1619          "\<bar>((\<Sum>(x, k)\<in>p. content k *\<^sub>R g x) - j) \<bullet> k\<bar> < (i \<bullet> k - j \<bullet> k) / 3"
  1620       using le_less_trans[OF Basis_le_norm[OF k]] k \<gamma>1 \<gamma>2 by metis+ 
  1621     then show False
  1622       unfolding inner_simps
  1623       using rsum_component_le[OF p] le
  1624       by (fastforce simp add: abs_real_def split: if_split_asm)
  1625   qed
  1626   show ?thesis
  1627   proof (cases "\<exists>a b. S = cbox a b")
  1628     case True
  1629     with ik_le_jk assms show ?thesis
  1630       by auto
  1631   next
  1632     case False
  1633     show ?thesis
  1634     proof (rule ccontr)
  1635       assume "\<not> i\<bullet>k \<le> j\<bullet>k"
  1636       then have ij: "(i\<bullet>k - j\<bullet>k) / 3 > 0"
  1637         by auto
  1638       obtain B1 where "0 < B1" 
  1639            and B1: "\<And>a b. ball 0 B1 \<subseteq> cbox a b \<Longrightarrow>
  1640                     \<exists>z. ((\<lambda>x. if x \<in> S then f x else 0) has_integral z) (cbox a b) \<and>
  1641                         norm (z - i) < (i \<bullet> k - j \<bullet> k) / 3"
  1642         using has_integral_altD[OF _ False ij] assms by blast
  1643       obtain B2 where "0 < B2" 
  1644            and B2: "\<And>a b. ball 0 B2 \<subseteq> cbox a b \<Longrightarrow>
  1645                     \<exists>z. ((\<lambda>x. if x \<in> S then g x else 0) has_integral z) (cbox a b) \<and>
  1646                         norm (z - j) < (i \<bullet> k - j \<bullet> k) / 3"
  1647         using has_integral_altD[OF _ False ij] assms by blast
  1648       have "bounded (ball 0 B1 \<union> ball (0::'a) B2)"
  1649         unfolding bounded_Un by(rule conjI bounded_ball)+
  1650       from bounded_subset_cbox_symmetric[OF this] 
  1651       obtain a b::'a where ab: "ball 0 B1 \<subseteq> cbox a b" "ball 0 B2 \<subseteq> cbox a b"
  1652         by (meson Un_subset_iff)
  1653       then obtain w1 w2 where int_w1: "((\<lambda>x. if x \<in> S then f x else 0) has_integral w1) (cbox a b)"
  1654                         and norm_w1:  "norm (w1 - i) < (i \<bullet> k - j \<bullet> k) / 3"
  1655                         and int_w2: "((\<lambda>x. if x \<in> S then g x else 0) has_integral w2) (cbox a b)"
  1656                         and norm_w2: "norm (w2 - j) < (i \<bullet> k - j \<bullet> k) / 3"
  1657         using B1 B2 by blast
  1658       have *: "\<And>w1 w2 j i::real .\<bar>w1 - i\<bar> < (i - j) / 3 \<Longrightarrow> \<bar>w2 - j\<bar> < (i - j) / 3 \<Longrightarrow> w1 \<le> w2 \<Longrightarrow> False"
  1659         by (simp add: abs_real_def split: if_split_asm)
  1660       have "\<bar>(w1 - i) \<bullet> k\<bar> < (i \<bullet> k - j \<bullet> k) / 3"
  1661            "\<bar>(w2 - j) \<bullet> k\<bar> < (i \<bullet> k - j \<bullet> k) / 3"
  1662         using Basis_le_norm k le_less_trans norm_w1 norm_w2 by blast+
  1663       moreover
  1664       have "w1\<bullet>k \<le> w2\<bullet>k"
  1665         using ik_le_jk int_w1 int_w2 f_le_g by auto
  1666       ultimately show False
  1667         unfolding inner_simps by(rule *)
  1668     qed
  1669   qed
  1670 qed
  1671 
  1672 lemma integral_component_le:
  1673   fixes g f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1674   assumes "k \<in> Basis"
  1675     and "f integrable_on S" "g integrable_on S"
  1676     and "\<And>x. x \<in> S \<Longrightarrow> (f x)\<bullet>k \<le> (g x)\<bullet>k"
  1677   shows "(integral S f)\<bullet>k \<le> (integral S g)\<bullet>k"
  1678   apply (rule has_integral_component_le)
  1679   using integrable_integral assms
  1680   apply auto
  1681   done
  1682 
  1683 lemma has_integral_component_nonneg:
  1684   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1685   assumes "k \<in> Basis"
  1686     and "(f has_integral i) S"
  1687     and "\<And>x. x \<in> S \<Longrightarrow> 0 \<le> (f x)\<bullet>k"
  1688   shows "0 \<le> i\<bullet>k"
  1689   using has_integral_component_le[OF assms(1) has_integral_0 assms(2)]
  1690   using assms(3-)
  1691   by auto
  1692 
  1693 lemma integral_component_nonneg:
  1694   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1695   assumes "k \<in> Basis"
  1696     and  "\<And>x. x \<in> S \<Longrightarrow> 0 \<le> (f x)\<bullet>k"
  1697   shows "0 \<le> (integral S f)\<bullet>k"
  1698 proof (cases "f integrable_on S")
  1699   case True show ?thesis
  1700     apply (rule has_integral_component_nonneg)
  1701     using assms True
  1702     apply auto
  1703     done
  1704 next
  1705   case False then show ?thesis by (simp add: not_integrable_integral)
  1706 qed
  1707 
  1708 lemma has_integral_component_neg:
  1709   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1710   assumes "k \<in> Basis"
  1711     and "(f has_integral i) S"
  1712     and "\<And>x. x \<in> S \<Longrightarrow> (f x)\<bullet>k \<le> 0"
  1713   shows "i\<bullet>k \<le> 0"
  1714   using has_integral_component_le[OF assms(1,2) has_integral_0] assms(2-)
  1715   by auto
  1716 
  1717 lemma has_integral_component_lbound:
  1718   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1719   assumes "(f has_integral i) (cbox a b)"
  1720     and "\<forall>x\<in>cbox a b. B \<le> f(x)\<bullet>k"
  1721     and "k \<in> Basis"
  1722   shows "B * content (cbox a b) \<le> i\<bullet>k"
  1723   using has_integral_component_le[OF assms(3) has_integral_const assms(1),of "(\<Sum>i\<in>Basis. B *\<^sub>R i)::'b"] assms(2-)
  1724   by (auto simp add: field_simps)
  1725 
  1726 lemma has_integral_component_ubound:
  1727   fixes f::"'a::euclidean_space => 'b::euclidean_space"
  1728   assumes "(f has_integral i) (cbox a b)"
  1729     and "\<forall>x\<in>cbox a b. f x\<bullet>k \<le> B"
  1730     and "k \<in> Basis"
  1731   shows "i\<bullet>k \<le> B * content (cbox a b)"
  1732   using has_integral_component_le[OF assms(3,1) has_integral_const, of "\<Sum>i\<in>Basis. B *\<^sub>R i"] assms(2-)
  1733   by (auto simp add: field_simps)
  1734 
  1735 lemma integral_component_lbound:
  1736   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1737   assumes "f integrable_on cbox a b"
  1738     and "\<forall>x\<in>cbox a b. B \<le> f(x)\<bullet>k"
  1739     and "k \<in> Basis"
  1740   shows "B * content (cbox a b) \<le> (integral(cbox a b) f)\<bullet>k"
  1741   apply (rule has_integral_component_lbound)
  1742   using assms
  1743   unfolding has_integral_integral
  1744   apply auto
  1745   done
  1746 
  1747 lemma integral_component_lbound_real:
  1748   assumes "f integrable_on {a ::real..b}"
  1749     and "\<forall>x\<in>{a..b}. B \<le> f(x)\<bullet>k"
  1750     and "k \<in> Basis"
  1751   shows "B * content {a..b} \<le> (integral {a..b} f)\<bullet>k"
  1752   using assms
  1753   by (metis box_real(2) integral_component_lbound)
  1754 
  1755 lemma integral_component_ubound:
  1756   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1757   assumes "f integrable_on cbox a b"
  1758     and "\<forall>x\<in>cbox a b. f x\<bullet>k \<le> B"
  1759     and "k \<in> Basis"
  1760   shows "(integral (cbox a b) f)\<bullet>k \<le> B * content (cbox a b)"
  1761   apply (rule has_integral_component_ubound)
  1762   using assms
  1763   unfolding has_integral_integral
  1764   apply auto
  1765   done
  1766 
  1767 lemma integral_component_ubound_real:
  1768   fixes f :: "real \<Rightarrow> 'a::euclidean_space"
  1769   assumes "f integrable_on {a..b}"
  1770     and "\<forall>x\<in>{a..b}. f x\<bullet>k \<le> B"
  1771     and "k \<in> Basis"
  1772   shows "(integral {a..b} f)\<bullet>k \<le> B * content {a..b}"
  1773   using assms
  1774   by (metis box_real(2) integral_component_ubound)
  1775 
  1776 subsection \<open>Uniform limit of integrable functions is integrable\<close>
  1777 
  1778 lemma real_arch_invD:
  1779   "0 < (e::real) \<Longrightarrow> (\<exists>n::nat. n \<noteq> 0 \<and> 0 < inverse (real n) \<and> inverse (real n) < e)"
  1780   by (subst(asm) real_arch_inverse)
  1781 
  1782 
  1783 lemma integrable_uniform_limit:
  1784   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
  1785   assumes "\<And>e. e > 0 \<Longrightarrow> \<exists>g. (\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b"
  1786   shows "f integrable_on cbox a b"
  1787 proof (cases "content (cbox a b) > 0")
  1788   case False then show ?thesis
  1789     using has_integral_null by (simp add: content_lt_nz integrable_on_def)
  1790 next
  1791   case True
  1792   have "1 / (real n + 1) > 0" for n
  1793     by auto
  1794   then have "\<exists>g. (\<forall>x\<in>cbox a b. norm (f x - g x) \<le> 1 / (real n + 1)) \<and> g integrable_on cbox a b" for n
  1795     using assms by blast
  1796   then obtain g where g_near_f: "\<And>n x. x \<in> cbox a b \<Longrightarrow> norm (f x - g n x) \<le> 1 / (real n + 1)"
  1797                   and int_g: "\<And>n. g n integrable_on cbox a b"
  1798     by metis
  1799   then obtain h where h: "\<And>n. (g n has_integral h n) (cbox a b)"
  1800     unfolding integrable_on_def by metis
  1801   have "Cauchy h"
  1802     unfolding Cauchy_def
  1803   proof clarify
  1804     fix e :: real
  1805     assume "e>0"
  1806     then have "e/4 / content (cbox a b) > 0"
  1807       using True by (auto simp: field_simps)
  1808     then obtain M where "M \<noteq> 0" and M: "1 / (real M) < e/4 / content (cbox a b)"
  1809       by (metis inverse_eq_divide real_arch_inverse)
  1810     show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (h m) (h n) < e"
  1811     proof (rule exI [where x=M], clarify)
  1812       fix m n
  1813       assume m: "M \<le> m" and n: "M \<le> n"
  1814       have "e/4>0" using \<open>e>0\<close> by auto
  1815       then obtain gm gn where "gauge gm" "gauge gn"
  1816               and gm: "\<And>\<D>. \<D> tagged_division_of cbox a b \<and> gm fine \<D> 
  1817                             \<Longrightarrow> norm ((\<Sum>(x,K) \<in> \<D>. content K *\<^sub>R g m x) - h m) < e/4"
  1818               and gn: "\<And>\<D>. \<D> tagged_division_of cbox a b \<and> gn fine \<D> \<Longrightarrow>
  1819                       norm ((\<Sum>(x,K) \<in> \<D>. content K *\<^sub>R g n x) - h n) < e/4"
  1820         using h[unfolded has_integral] by meson
  1821       then obtain \<D> where \<D>: "\<D> tagged_division_of cbox a b" "(\<lambda>x. gm x \<inter> gn x) fine \<D>"
  1822         by (metis (full_types) fine_division_exists gauge_Int)
  1823       have triangle3: "norm (i1 - i2) < e"
  1824         if no: "norm(s2 - s1) \<le> e/2" "norm (s1 - i1) < e/4" "norm (s2 - i2) < e/4" for s1 s2 i1 and i2::'b
  1825       proof -
  1826         have "norm (i1 - i2) \<le> norm (i1 - s1) + norm (s1 - s2) + norm (s2 - i2)"
  1827           using norm_triangle_ineq[of "i1 - s1" "s1 - i2"]
  1828           using norm_triangle_ineq[of "s1 - s2" "s2 - i2"]
  1829           by (auto simp: algebra_simps)
  1830         also have "\<dots> < e"
  1831           using no by (auto simp: algebra_simps norm_minus_commute)
  1832         finally show ?thesis .
  1833       qed
  1834       have finep: "gm fine \<D>" "gn fine \<D>"
  1835         using fine_Int \<D>  by auto
  1836       have norm_le: "norm (g n x - g m x) \<le> 2 / real M" if x: "x \<in> cbox a b" for x
  1837       proof -
  1838         have "norm (f x - g n x) + norm (f x - g m x) \<le> 1 / (real n + 1) + 1 / (real m + 1)"          
  1839           using g_near_f[OF x, of n] g_near_f[OF x, of m] by simp
  1840         also have "\<dots> \<le> 1 / (real M) + 1 / (real M)"
  1841           apply (rule add_mono)
  1842           using \<open>M \<noteq> 0\<close> m n by (auto simp: divide_simps)
  1843         also have "\<dots> = 2 / real M"
  1844           by auto
  1845         finally show "norm (g n x - g m x) \<le> 2 / real M"
  1846           using norm_triangle_le[of "g n x - f x" "f x - g m x" "2 / real M"]
  1847           by (auto simp: algebra_simps simp add: norm_minus_commute)
  1848       qed
  1849       have "norm ((\<Sum>(x,K) \<in> \<D>. content K *\<^sub>R g n x) - (\<Sum>(x,K) \<in> \<D>. content K *\<^sub>R g m x)) \<le> 2 / real M * content (cbox a b)"
  1850         by (blast intro: norm_le rsum_diff_bound[OF \<D>(1), where e="2 / real M"])
  1851       also have "... \<le> e/2"
  1852         using M True
  1853         by (auto simp: field_simps)
  1854       finally have le_e2: "norm ((\<Sum>(x,K) \<in> \<D>. content K *\<^sub>R g n x) - (\<Sum>(x,K) \<in> \<D>. content K *\<^sub>R g m x)) \<le> e/2" .
  1855       then show "dist (h m) (h n) < e"
  1856         unfolding dist_norm using gm gn \<D> finep by (auto intro!: triangle3)
  1857     qed
  1858   qed
  1859   then obtain s where s: "h \<longlonglongrightarrow> s"
  1860     using convergent_eq_Cauchy[symmetric] by blast
  1861   show ?thesis
  1862     unfolding integrable_on_def has_integral
  1863   proof (rule_tac x=s in exI, clarify)
  1864     fix e::real
  1865     assume e: "0 < e"
  1866     then have "e/3 > 0" by auto
  1867     then obtain N1 where N1: "\<forall>n\<ge>N1. norm (h n - s) < e/3"
  1868       using LIMSEQ_D [OF s] by metis
  1869     from e True have "e/3 / content (cbox a b) > 0"
  1870       by (auto simp: field_simps)
  1871     then obtain N2 :: nat
  1872          where "N2 \<noteq> 0" and N2: "1 / (real N2) < e/3 / content (cbox a b)"
  1873       by (metis inverse_eq_divide real_arch_inverse)
  1874     obtain g' where "gauge g'"
  1875             and g': "\<And>\<D>. \<D> tagged_division_of cbox a b \<and> g' fine \<D> \<Longrightarrow>
  1876                     norm ((\<Sum>(x,K) \<in> \<D>. content K *\<^sub>R g (N1 + N2) x) - h (N1 + N2)) < e/3"
  1877       by (metis h has_integral \<open>e/3 > 0\<close>)
  1878     have *: "norm (sf - s) < e" 
  1879         if no: "norm (sf - sg) \<le> e/3" "norm(h - s) < e/3" "norm (sg - h) < e/3" for sf sg h
  1880     proof -
  1881       have "norm (sf - s) \<le> norm (sf - sg) + norm (sg - h) + norm (h - s)"
  1882         using norm_triangle_ineq[of "sf - sg" "sg - s"]
  1883         using norm_triangle_ineq[of "sg -  h" " h - s"]
  1884         by (auto simp: algebra_simps)
  1885       also have "\<dots> < e"
  1886         using no by (auto simp: algebra_simps norm_minus_commute)
  1887       finally show ?thesis .
  1888     qed
  1889     { fix \<D>
  1890       assume ptag: "\<D> tagged_division_of (cbox a b)" and "g' fine \<D>"
  1891       then have norm_less: "norm ((\<Sum>(x,K) \<in> \<D>. content K *\<^sub>R g (N1 + N2) x) - h (N1 + N2)) < e/3"
  1892         using g' by blast
  1893       have "content (cbox a b) < e/3 * (of_nat N2)"
  1894         using \<open>N2 \<noteq> 0\<close> N2 using True by (auto simp: divide_simps)
  1895       moreover have "e/3 * of_nat N2 \<le> e/3 * (of_nat (N1 + N2) + 1)"
  1896         using \<open>e>0\<close> by auto
  1897       ultimately have "content (cbox a b) < e/3 * (of_nat (N1 + N2) + 1)"
  1898         by linarith
  1899       then have le_e3: "1 / (real (N1 + N2) + 1) * content (cbox a b) \<le> e/3"
  1900         unfolding inverse_eq_divide
  1901         by (auto simp: field_simps)
  1902       have ne3: "norm (h (N1 + N2) - s) < e/3"
  1903         using N1 by auto
  1904       have "norm ((\<Sum>(x,K) \<in> \<D>. content K *\<^sub>R f x) - (\<Sum>(x,K) \<in> \<D>. content K *\<^sub>R g (N1 + N2) x))
  1905             \<le> 1 / (real (N1 + N2) + 1) * content (cbox a b)"
  1906         by (blast intro: g_near_f rsum_diff_bound[OF ptag])
  1907       then have "norm ((\<Sum>(x,K) \<in> \<D>. content K *\<^sub>R f x) - s) < e"
  1908         by (rule *[OF order_trans [OF _ le_e3] ne3 norm_less])
  1909     }
  1910     then show "\<exists>d. gauge d \<and>
  1911              (\<forall>\<D>. \<D> tagged_division_of cbox a b \<and> d fine \<D> \<longrightarrow> norm ((\<Sum>(x,K) \<in> \<D>. content K *\<^sub>R f x) - s) < e)"
  1912       by (blast intro: g' \<open>gauge g'\<close>)
  1913   qed
  1914 qed
  1915 
  1916 lemmas integrable_uniform_limit_real = integrable_uniform_limit [where 'a=real, simplified]
  1917 
  1918 
  1919 subsection \<open>Negligible sets\<close>
  1920 
  1921 definition "negligible (s:: 'a::euclidean_space set) \<longleftrightarrow>
  1922   (\<forall>a b. ((indicator s :: 'a\<Rightarrow>real) has_integral 0) (cbox a b))"
  1923 
  1924 
  1925 subsubsection \<open>Negligibility of hyperplane\<close>
  1926 
  1927 lemma content_doublesplit:
  1928   fixes a :: "'a::euclidean_space"
  1929   assumes "0 < e"
  1930     and k: "k \<in> Basis"
  1931   obtains d where "0 < d" and "content (cbox a b \<inter> {x. \<bar>x\<bullet>k - c\<bar> \<le> d}) < e"
  1932 proof cases
  1933   assume *: "a \<bullet> k \<le> c \<and> c \<le> b \<bullet> k \<and> (\<forall>j\<in>Basis. a \<bullet> j \<le> b \<bullet> j)"
  1934   define a' where "a' d = (\<Sum>j\<in>Basis. (if j = k then max (a\<bullet>j) (c - d) else a\<bullet>j) *\<^sub>R j)" for d
  1935   define b' where "b' d = (\<Sum>j\<in>Basis. (if j = k then min (b\<bullet>j) (c + d) else b\<bullet>j) *\<^sub>R j)" for d
  1936 
  1937   have "((\<lambda>d. \<Prod>j\<in>Basis. (b' d - a' d) \<bullet> j) \<longlongrightarrow> (\<Prod>j\<in>Basis. (b' 0 - a' 0) \<bullet> j)) (at_right 0)"
  1938     by (auto simp: b'_def a'_def intro!: tendsto_min tendsto_max tendsto_eq_intros)
  1939   also have "(\<Prod>j\<in>Basis. (b' 0 - a' 0) \<bullet> j) = 0"
  1940     using k *
  1941     by (intro prod_zero bexI[OF _ k])
  1942        (auto simp: b'_def a'_def inner_diff inner_sum_left inner_not_same_Basis intro!: sum.cong)
  1943   also have "((\<lambda>d. \<Prod>j\<in>Basis. (b' d - a' d) \<bullet> j) \<longlongrightarrow> 0) (at_right 0) =
  1944     ((\<lambda>d. content (cbox a b \<inter> {x. \<bar>x\<bullet>k - c\<bar> \<le> d})) \<longlongrightarrow> 0) (at_right 0)"
  1945   proof (intro tendsto_cong eventually_at_rightI)
  1946     fix d :: real assume d: "d \<in> {0<..<1}"
  1947     have "cbox a b \<inter> {x. \<bar>x\<bullet>k - c\<bar> \<le> d} = cbox (a' d) (b' d)" for d
  1948       using * d k by (auto simp add: cbox_def set_eq_iff Int_def ball_conj_distrib abs_diff_le_iff a'_def b'_def)
  1949     moreover have "j \<in> Basis \<Longrightarrow> a' d \<bullet> j \<le> b' d \<bullet> j" for j
  1950       using * d k by (auto simp: a'_def b'_def)
  1951     ultimately show "(\<Prod>j\<in>Basis. (b' d - a' d) \<bullet> j) = content (cbox a b \<inter> {x. \<bar>x\<bullet>k - c\<bar> \<le> d})"
  1952       by simp
  1953   qed simp
  1954   finally have "((\<lambda>d. content (cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})) \<longlongrightarrow> 0) (at_right 0)" .
  1955   from order_tendstoD(2)[OF this \<open>0<e\<close>]
  1956   obtain d' where "0 < d'" and d': "\<And>y. y > 0 \<Longrightarrow> y < d' \<Longrightarrow> content (cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> y}) < e"
  1957     by (subst (asm) eventually_at_right[of _ 1]) auto
  1958   show ?thesis
  1959     by (rule that[of "d'/2"], insert \<open>0<d'\<close> d'[of "d'/2"], auto)
  1960 next
  1961   assume *: "\<not> (a \<bullet> k \<le> c \<and> c \<le> b \<bullet> k \<and> (\<forall>j\<in>Basis. a \<bullet> j \<le> b \<bullet> j))"
  1962   then have "(\<exists>j\<in>Basis. b \<bullet> j < a \<bullet> j) \<or> (c < a \<bullet> k \<or> b \<bullet> k < c)"
  1963     by (auto simp: not_le)
  1964   show thesis
  1965   proof cases
  1966     assume "\<exists>j\<in>Basis. b \<bullet> j < a \<bullet> j"
  1967     then have [simp]: "cbox a b = {}"
  1968       using box_ne_empty(1)[of a b] by auto
  1969     show ?thesis
  1970       by (rule that[of 1]) (simp_all add: \<open>0<e\<close>)
  1971   next
  1972     assume "\<not> (\<exists>j\<in>Basis. b \<bullet> j < a \<bullet> j)"
  1973     with * have "c < a \<bullet> k \<or> b \<bullet> k < c"
  1974       by auto
  1975     then show thesis
  1976     proof
  1977       assume c: "c < a \<bullet> k"
  1978       moreover have "x \<in> cbox a b \<Longrightarrow> c \<le> x \<bullet> k" for x
  1979         using k c by (auto simp: cbox_def)
  1980       ultimately have "cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> (a \<bullet> k - c)/2} = {}"
  1981         using k by (auto simp: cbox_def)
  1982       with \<open>0<e\<close> c that[of "(a \<bullet> k - c)/2"] show ?thesis
  1983         by auto
  1984     next
  1985       assume c: "b \<bullet> k < c"
  1986       moreover have "x \<in> cbox a b \<Longrightarrow> x \<bullet> k \<le> c" for x
  1987         using k c by (auto simp: cbox_def)
  1988       ultimately have "cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> (c - b \<bullet> k)/2} = {}"
  1989         using k by (auto simp: cbox_def)
  1990       with \<open>0<e\<close> c that[of "(c - b \<bullet> k)/2"] show ?thesis
  1991         by auto
  1992     qed
  1993   qed
  1994 qed
  1995 
  1996 
  1997 proposition negligible_standard_hyperplane[intro]:
  1998   fixes k :: "'a::euclidean_space"
  1999   assumes k: "k \<in> Basis"
  2000   shows "negligible {x. x\<bullet>k = c}"
  2001   unfolding negligible_def has_integral
  2002 proof clarsimp
  2003   fix a b and e::real assume "e > 0"
  2004   with k obtain d where "0 < d" and d: "content (cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) < e"
  2005     by (metis content_doublesplit)
  2006   let ?i = "indicator {x::'a. x\<bullet>k = c} :: 'a\<Rightarrow>real"
  2007   show "\<exists>\<gamma>. gauge \<gamma> \<and>
  2008            (\<forall>\<D>. \<D> tagged_division_of cbox a b \<and> \<gamma> fine \<D> \<longrightarrow>
  2009                  \<bar>\<Sum>(x,K) \<in> \<D>. content K * ?i x\<bar> < e)"
  2010   proof (intro exI, safe)
  2011     show "gauge (\<lambda>x. ball x d)"
  2012       using \<open>0 < d\<close> by blast
  2013   next
  2014     fix \<D>
  2015     assume p: "\<D> tagged_division_of (cbox a b)" "(\<lambda>x. ball x d) fine \<D>"
  2016     have "content L = content (L \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})" 
  2017       if "(x, L) \<in> \<D>" "?i x \<noteq> 0" for x L
  2018     proof -
  2019       have xk: "x\<bullet>k = c"
  2020         using that by (simp add: indicator_def split: if_split_asm)
  2021       have "L \<subseteq> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}"
  2022       proof 
  2023         fix y
  2024         assume y: "y \<in> L"
  2025         have "L \<subseteq> ball x d"
  2026           using p(2) that(1) by auto
  2027         then have "norm (x - y) < d"
  2028           by (simp add: dist_norm subset_iff y)
  2029         then have "\<bar>(x - y) \<bullet> k\<bar> < d"
  2030           using k norm_bound_Basis_lt by blast
  2031         then show "y \<in> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}"
  2032           unfolding inner_simps xk by auto
  2033       qed 
  2034       then show "content L = content (L \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})"
  2035         by (metis inf.orderE)
  2036     qed
  2037     then have *: "(\<Sum>(x,K)\<in>\<D>. content K * ?i x) = (\<Sum>(x,K)\<in>\<D>. content (K \<inter> {x. \<bar>x\<bullet>k - c\<bar> \<le> d}) *\<^sub>R ?i x)"
  2038       by (force simp add: split_paired_all intro!: sum.cong [OF refl])
  2039     note p'= tagged_division_ofD[OF p(1)] and p''=division_of_tagged_division[OF p(1)]
  2040     have "(\<Sum>(x,K)\<in>\<D>. content (K \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) * indicator {x. x \<bullet> k = c} x) < e"
  2041     proof -
  2042       have "(\<Sum>(x,K)\<in>\<D>. content (K \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) * ?i x) \<le> (\<Sum>(x,K)\<in>\<D>. content (K \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}))"
  2043         by (force simp add: indicator_def intro!: sum_mono)
  2044       also have "\<dots> < e"
  2045       proof (subst sum.over_tagged_division_lemma[OF p(1)])
  2046         fix u v::'a
  2047         assume "box u v = {}"
  2048         then have *: "content (cbox u v) = 0"
  2049           unfolding content_eq_0_interior by simp
  2050         have "cbox u v \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} \<subseteq> cbox u v"
  2051           by auto
  2052         then have "content (cbox u v \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) \<le> content (cbox u v)"
  2053           unfolding interval_doublesplit[OF k] by (rule content_subset)
  2054         then show "content (cbox u v \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) = 0"
  2055           unfolding * interval_doublesplit[OF k]
  2056           by (blast intro: antisym)
  2057       next
  2058         have "(\<Sum>l\<in>snd ` \<D>. content (l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})) =
  2059           sum content ((\<lambda>l. l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})`{l\<in>snd ` \<D>. l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} \<noteq> {}})"
  2060         proof (subst (2) sum.reindex_nontrivial)
  2061           fix x y assume "x \<in> {l \<in> snd ` \<D>. l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} \<noteq> {}}" "y \<in> {l \<in> snd ` \<D>. l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} \<noteq> {}}"
  2062             "x \<noteq> y" and eq: "x \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} = y \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}"
  2063           then obtain x' y' where "(x', x) \<in> \<D>" "x \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} \<noteq> {}" "(y', y) \<in> \<D>" "y \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} \<noteq> {}"
  2064             by (auto)
  2065           from p'(5)[OF \<open>(x', x) \<in> \<D>\<close> \<open>(y', y) \<in> \<D>\<close>] \<open>x \<noteq> y\<close> have "interior (x \<inter> y) = {}"
  2066             by auto
  2067           moreover have "interior ((x \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) \<inter> (y \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})) \<subseteq> interior (x \<inter> y)"
  2068             by (auto intro: interior_mono)
  2069           ultimately have "interior (x \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) = {}"
  2070             by (auto simp: eq)
  2071           then show "content (x \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) = 0"
  2072             using p'(4)[OF \<open>(x', x) \<in> \<D>\<close>] by (auto simp: interval_doublesplit[OF k] content_eq_0_interior simp del: interior_Int)
  2073         qed (insert p'(1), auto intro!: sum.mono_neutral_right)
  2074         also have "\<dots> \<le> norm (\<Sum>l\<in>(\<lambda>l. l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})`{l\<in>snd ` \<D>. l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} \<noteq> {}}. content l *\<^sub>R 1::real)"
  2075           by simp
  2076         also have "\<dots> \<le> 1 * content (cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})"
  2077           using division_doublesplit[OF p'' k, unfolded interval_doublesplit[OF k]]
  2078           unfolding interval_doublesplit[OF k] by (intro dsum_bound) auto
  2079         also have "\<dots> < e"
  2080           using d by simp
  2081         finally show "(\<Sum>K\<in>snd ` \<D>. content (K \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})) < e" .
  2082       qed
  2083       finally show "(\<Sum>(x, K)\<in>\<D>. content (K \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) * ?i x) < e" .
  2084     qed
  2085     then show "\<bar>\<Sum>(x, K)\<in>\<D>. content K * ?i x\<bar> < e"
  2086       unfolding * 
  2087       apply (subst abs_of_nonneg)
  2088       using measure_nonneg       
  2089       by (force simp add: indicator_def intro: sum_nonneg)+
  2090   qed
  2091 qed
  2092 
  2093 corollary negligible_standard_hyperplane_cart:
  2094   fixes k :: "'a::finite"
  2095   shows "negligible {x. x$k = (0::real)}"
  2096   by (simp add: cart_eq_inner_axis negligible_standard_hyperplane)
  2097 
  2098 
  2099 subsubsection \<open>Hence the main theorem about negligible sets\<close>
  2100 
  2101 
  2102 lemma has_integral_negligible_cbox:
  2103   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  2104   assumes negs: "negligible S"
  2105     and 0: "\<And>x. x \<notin> S \<Longrightarrow> f x = 0"
  2106   shows "(f has_integral 0) (cbox a b)"
  2107   unfolding has_integral
  2108 proof clarify
  2109   fix e::real
  2110   assume "e > 0"
  2111   then have nn_gt0: "e/2 / ((real n+1) * (2 ^ n)) > 0" for n
  2112     by simp
  2113   then have "\<exists>\<gamma>. gauge \<gamma> \<and>
  2114                    (\<forall>\<D>. \<D> tagged_division_of cbox a b \<and> \<gamma> fine \<D> \<longrightarrow>
  2115                         \<bar>\<Sum>(x,K) \<in> \<D>. content K *\<^sub>R indicator S x\<bar>
  2116                         < e/2 / ((real n + 1) * 2 ^ n))" for n
  2117     using negs [unfolded negligible_def has_integral] by auto
  2118   then obtain \<gamma> where 
  2119     gd: "\<And>n. gauge (\<gamma> n)"
  2120     and \<gamma>: "\<And>n \<D>. \<lbrakk>\<D> tagged_division_of cbox a b; \<gamma> n fine \<D>\<rbrakk>
  2121                   \<Longrightarrow> \<bar>\<Sum>(x,K) \<in> \<D>. content K *\<^sub>R indicator S x\<bar> < e/2 / ((real n + 1) * 2 ^ n)"
  2122     by metis
  2123   show "\<exists>\<gamma>. gauge \<gamma> \<and>
  2124              (\<forall>\<D>. \<D> tagged_division_of cbox a b \<and> \<gamma> fine \<D> \<longrightarrow>
  2125                   norm ((\<Sum>(x,K) \<in> \<D>. content K *\<^sub>R f x) - 0) < e)"
  2126   proof (intro exI, safe)
  2127     show "gauge (\<lambda>x. \<gamma> (nat \<lfloor>norm (f x)\<rfloor>) x)"
  2128       using gd by (auto simp: gauge_def)
  2129 
  2130     show "norm ((\<Sum>(x,K) \<in> \<D>. content K *\<^sub>R f x) - 0) < e"
  2131       if "\<D> tagged_division_of (cbox a b)" "(\<lambda>x. \<gamma> (nat \<lfloor>norm (f x)\<rfloor>) x) fine \<D>" for \<D>
  2132     proof (cases "\<D> = {}")
  2133       case True with \<open>0 < e\<close> show ?thesis by simp
  2134     next
  2135       case False
  2136       obtain N where "Max ((\<lambda>(x, K). norm (f x)) ` \<D>) \<le> real N"
  2137         using real_arch_simple by blast
  2138       then have N: "\<And>x. x \<in> (\<lambda>(x, K). norm (f x)) ` \<D> \<Longrightarrow> x \<le> real N"
  2139         by (meson Max_ge that(1) dual_order.trans finite_imageI tagged_division_of_finite)
  2140       have "\<forall>i. \<exists>q. q tagged_division_of (cbox a b) \<and> (\<gamma> i) fine q \<and> (\<forall>(x,K) \<in> \<D>. K \<subseteq> (\<gamma> i) x \<longrightarrow> (x, K) \<in> q)"
  2141         by (auto intro: tagged_division_finer[OF that(1) gd])
  2142       from choice[OF this] 
  2143       obtain q where q: "\<And>n. q n tagged_division_of cbox a b"
  2144                         "\<And>n. \<gamma> n fine q n"
  2145                         "\<And>n x K. \<lbrakk>(x, K) \<in> \<D>; K \<subseteq> \<gamma> n x\<rbrakk> \<Longrightarrow> (x, K) \<in> q n"
  2146         by fastforce
  2147       have "finite \<D>"
  2148         using that(1) by blast
  2149       then have sum_le_inc: "\<lbrakk>finite T; \<And>x y. (x,y) \<in> T \<Longrightarrow> (0::real) \<le> g(x,y);
  2150                       \<And>y. y\<in>\<D> \<Longrightarrow> \<exists>x. (x,y) \<in> T \<and> f(y) \<le> g(x,y)\<rbrakk> \<Longrightarrow> sum f \<D> \<le> sum g T" for f g T
  2151         by (rule sum_le_included[of \<D> T g snd f]; force)
  2152       have "norm (\<Sum>(x,K) \<in> \<D>. content K *\<^sub>R f x) \<le> (\<Sum>(x,K) \<in> \<D>. norm (content K *\<^sub>R f x))"
  2153         unfolding split_def by (rule norm_sum)
  2154       also have "... \<le> (\<Sum>(i, j) \<in> Sigma {..N + 1} q.
  2155                           (real i + 1) * (case j of (x, K) \<Rightarrow> content K *\<^sub>R indicator S x))"
  2156       proof (rule sum_le_inc, safe)
  2157         show "finite (Sigma {..N+1} q)"
  2158           by (meson finite_SigmaI finite_atMost tagged_division_of_finite q(1)) 
  2159       next
  2160         fix x K
  2161         assume xk: "(x, K) \<in> \<D>"
  2162         define n where "n = nat \<lfloor>norm (f x)\<rfloor>"
  2163         have *: "norm (f x) \<in> (\<lambda>(x, K). norm (f x)) ` \<D>"
  2164           using xk by auto
  2165         have nfx: "real n \<le> norm (f x)" "norm (f x) \<le> real n + 1"
  2166           unfolding n_def by auto
  2167         then have "n \<in> {0..N + 1}"
  2168           using N[OF *] by auto
  2169         moreover have "K \<subseteq> \<gamma> (nat \<lfloor>norm (f x)\<rfloor>) x"
  2170           using that(2) xk by auto
  2171         moreover then have "(x, K) \<in> q (nat \<lfloor>norm (f x)\<rfloor>)"
  2172           by (simp add: q(3) xk)
  2173         moreover then have "(x, K) \<in> q n"
  2174           using n_def by blast
  2175         moreover
  2176         have "norm (content K *\<^sub>R f x) \<le> (real n + 1) * (content K * indicator S x)"
  2177         proof (cases "x \<in> S")
  2178           case False
  2179           then show ?thesis by (simp add: 0)
  2180         next
  2181           case True
  2182           have *: "content K \<ge> 0"
  2183             using tagged_division_ofD(4)[OF that(1) xk] by auto
  2184           moreover have "content K * norm (f x) \<le> content K * (real n + 1)"
  2185             by (simp add: mult_left_mono nfx(2))
  2186           ultimately show ?thesis
  2187             using nfx True by (auto simp: field_simps)
  2188         qed
  2189         ultimately show "\<exists>y. (y, x, K) \<in> (Sigma {..N + 1} q) \<and> norm (content K *\<^sub>R f x) \<le>
  2190           (real y + 1) * (content K *\<^sub>R indicator S x)"
  2191           by force
  2192       qed auto
  2193       also have "... = (\<Sum>i\<le>N + 1. \<Sum>j\<in>q i. (real i + 1) * (case j of (x, K) \<Rightarrow> content K *\<^sub>R indicator S x))"
  2194         apply (rule sum_Sigma_product [symmetric])
  2195         using q(1) apply auto
  2196         done
  2197       also have "... \<le> (\<Sum>i\<le>N + 1. (real i + 1) * \<bar>\<Sum>(x,K) \<in> q i. content K *\<^sub>R indicator S x\<bar>)"
  2198         by (rule sum_mono) (simp add: sum_distrib_left [symmetric])
  2199       also have "... \<le> (\<Sum>i\<le>N + 1. e/2/2 ^ i)"
  2200       proof (rule sum_mono)
  2201         show "(real i + 1) * \<bar>\<Sum>(x,K) \<in> q i. content K *\<^sub>R indicator S x\<bar> \<le> e/2/2 ^ i"
  2202           if "i \<in> {..N + 1}" for i
  2203           using \<gamma>[of "q i" i] q by (simp add: divide_simps mult.left_commute)
  2204       qed
  2205       also have "... = e/2 * (\<Sum>i\<le>N + 1. (1/2) ^ i)"
  2206         unfolding sum_distrib_left by (metis divide_inverse inverse_eq_divide power_one_over)
  2207       also have "\<dots> < e/2 * 2"
  2208       proof (rule mult_strict_left_mono)
  2209         have "sum (power (1/2)) {..N + 1} = sum (power (1/2::real)) {..<N + 2}"
  2210           using lessThan_Suc_atMost by auto
  2211         also have "... < 2"
  2212           by (auto simp: geometric_sum)
  2213         finally show "sum (power (1/2::real)) {..N + 1} < 2" .
  2214       qed (use \<open>0 < e\<close> in auto)
  2215       finally  show ?thesis by auto
  2216     qed
  2217   qed
  2218 qed
  2219 
  2220 
  2221 proposition has_integral_negligible:
  2222   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  2223   assumes negs: "negligible S"
  2224     and "\<And>x. x \<in> (T - S) \<Longrightarrow> f x = 0"
  2225   shows "(f has_integral 0) T"
  2226 proof (cases "\<exists>a b. T = cbox a b")
  2227   case True
  2228   then have "((\<lambda>x. if x \<in> T then f x else 0) has_integral 0) T"
  2229     using assms by (auto intro!: has_integral_negligible_cbox)
  2230   then show ?thesis
  2231     by (rule has_integral_eq [rotated]) auto
  2232 next
  2233   case False
  2234   let ?f = "(\<lambda>x. if x \<in> T then f x else 0)"
  2235   have "((\<lambda>x. if x \<in> T then f x else 0) has_integral 0) T"
  2236     apply (auto simp: False has_integral_alt [of ?f])
  2237     apply (rule_tac x=1 in exI, auto)
  2238     apply (rule_tac x=0 in exI, simp add: has_integral_negligible_cbox [OF negs] assms)
  2239     done
  2240   then show ?thesis
  2241     by (rule_tac f="?f" in has_integral_eq) auto
  2242 qed
  2243 
  2244 lemma
  2245   assumes "negligible S"
  2246   shows integrable_negligible: "f integrable_on S" and integral_negligible: "integral S f = 0"
  2247   using has_integral_negligible [OF assms]
  2248   by (auto simp: has_integral_iff)
  2249 
  2250 lemma has_integral_spike:
  2251   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  2252   assumes "negligible S"
  2253     and gf: "\<And>x. x \<in> T - S \<Longrightarrow> g x = f x"
  2254     and fint: "(f has_integral y) T"
  2255   shows "(g has_integral y) T"
  2256 proof -
  2257   have *: "(g has_integral y) (cbox a b)"
  2258        if "(f has_integral y) (cbox a b)" "\<forall>x \<in> cbox a b - S. g x = f x" for a b f and g:: "'b \<Rightarrow> 'a" and y
  2259   proof -
  2260     have "((\<lambda>x. f x + (g x - f x)) has_integral (y + 0)) (cbox a b)"
  2261       using that by (intro has_integral_add has_integral_negligible) (auto intro!: \<open>negligible S\<close>)
  2262     then show ?thesis
  2263       by auto
  2264   qed
  2265   show ?thesis
  2266     using fint gf
  2267     apply (subst has_integral_alt)
  2268     apply (subst (asm) has_integral_alt)
  2269     apply (simp split: if_split_asm)
  2270      apply (blast dest: *)
  2271       apply (erule_tac V = "\<forall>a b. T \<noteq> cbox a b" in thin_rl)
  2272     apply (elim all_forward imp_forward ex_forward all_forward conj_forward asm_rl)
  2273      apply (auto dest!: *[where f="\<lambda>x. if x\<in>T then f x else 0" and g="\<lambda>x. if x \<in> T then g x else 0"])
  2274     done
  2275 qed
  2276 
  2277 lemma has_integral_spike_eq:
  2278   assumes "negligible S"
  2279     and gf: "\<And>x. x \<in> T - S \<Longrightarrow> g x = f x"
  2280   shows "(f has_integral y) T \<longleftrightarrow> (g has_integral y) T"
  2281     using has_integral_spike [OF \<open>negligible S\<close>] gf
  2282     by metis
  2283 
  2284 lemma integrable_spike:
  2285   assumes "f integrable_on T" "negligible S" "\<And>x. x \<in> T - S \<Longrightarrow> g x = f x"
  2286     shows "g integrable_on T"
  2287   using assms unfolding integrable_on_def by (blast intro: has_integral_spike)
  2288 
  2289 lemma integral_spike:
  2290   assumes "negligible S"
  2291     and "\<And>x. x \<in> T - S \<Longrightarrow> g x = f x"
  2292   shows "integral T f = integral T g"
  2293   using has_integral_spike_eq[OF assms]
  2294     by (auto simp: integral_def integrable_on_def)
  2295 
  2296 
  2297 subsection \<open>Some other trivialities about negligible sets\<close>
  2298 
  2299 lemma negligible_subset:
  2300   assumes "negligible s" "t \<subseteq> s"
  2301   shows "negligible t"
  2302   unfolding negligible_def
  2303     by (metis (no_types) Diff_iff assms contra_subsetD has_integral_negligible indicator_simps(2))
  2304 
  2305 lemma negligible_diff[intro?]:
  2306   assumes "negligible s"
  2307   shows "negligible (s - t)"
  2308   using assms by (meson Diff_subset negligible_subset)
  2309 
  2310 lemma negligible_Int:
  2311   assumes "negligible s \<or> negligible t"
  2312   shows "negligible (s \<inter> t)"
  2313   using assms negligible_subset by force
  2314 
  2315 lemma negligible_Un:
  2316   assumes "negligible S" and T: "negligible T"
  2317   shows "negligible (S \<union> T)"
  2318 proof -
  2319   have "(indicat_real (S \<union> T) has_integral 0) (cbox a b)"
  2320     if S0: "(indicat_real S has_integral 0) (cbox a b)" 
  2321       and  "(indicat_real T has_integral 0) (cbox a b)" for a b
  2322   proof (subst has_integral_spike_eq[OF T])
  2323     show "indicat_real S x = indicat_real (S \<union> T) x" if "x \<in> cbox a b - T" for x
  2324       by (metis Diff_iff Un_iff indicator_def that)
  2325     show "(indicat_real S has_integral 0) (cbox a b)"
  2326       by (simp add: S0)
  2327   qed
  2328   with assms show ?thesis
  2329     unfolding negligible_def by blast
  2330 qed
  2331 
  2332 lemma negligible_Un_eq[simp]: "negligible (s \<union> t) \<longleftrightarrow> negligible s \<and> negligible t"
  2333   using negligible_Un negligible_subset by blast
  2334 
  2335 lemma negligible_sing[intro]: "negligible {a::'a::euclidean_space}"
  2336   using negligible_standard_hyperplane[OF SOME_Basis, of "a \<bullet> (SOME i. i \<in> Basis)"] negligible_subset by blast
  2337 
  2338 lemma negligible_insert[simp]: "negligible (insert a s) \<longleftrightarrow> negligible s"
  2339   apply (subst insert_is_Un)
  2340   unfolding negligible_Un_eq
  2341   apply auto
  2342   done
  2343 
  2344 lemma negligible_empty[iff]: "negligible {}"
  2345   using negligible_insert by blast
  2346 
  2347 text\<open>Useful in this form for backchaining\<close>
  2348 lemma empty_imp_negligible: "S = {} \<Longrightarrow> negligible S"
  2349   by simp
  2350 
  2351 lemma negligible_finite[intro]:
  2352   assumes "finite s"
  2353   shows "negligible s"
  2354   using assms by (induct s) auto
  2355 
  2356 lemma negligible_Union[intro]:
  2357   assumes "finite \<T>"
  2358     and "\<And>t. t \<in> \<T> \<Longrightarrow> negligible t"
  2359   shows "negligible(\<Union>\<T>)"
  2360   using assms by induct auto
  2361 
  2362 lemma negligible: "negligible S \<longleftrightarrow> (\<forall>T. (indicat_real S has_integral 0) T)"
  2363 proof (intro iffI allI)
  2364   fix T
  2365   assume "negligible S"
  2366   then show "(indicator S has_integral 0) T"
  2367     by (meson Diff_iff has_integral_negligible indicator_simps(2))
  2368 qed (simp add: negligible_def)
  2369 
  2370 subsection \<open>Finite case of the spike theorem is quite commonly needed\<close>
  2371 
  2372 lemma has_integral_spike_finite:
  2373   assumes "finite S"
  2374     and "\<And>x. x \<in> T - S \<Longrightarrow> g x = f x"
  2375     and "(f has_integral y) T"
  2376   shows "(g has_integral y) T"
  2377   using assms has_integral_spike negligible_finite by blast
  2378 
  2379 lemma has_integral_spike_finite_eq:
  2380   assumes "finite S"
  2381     and "\<And>x. x \<in> T - S \<Longrightarrow> g x = f x"
  2382   shows "((f has_integral y) T \<longleftrightarrow> (g has_integral y) T)"
  2383   by (metis assms has_integral_spike_finite)
  2384 
  2385 lemma integrable_spike_finite:
  2386   assumes "finite S"
  2387     and "\<And>x. x \<in> T - S \<Longrightarrow> g x = f x"
  2388     and "f integrable_on T"
  2389   shows "g integrable_on T"
  2390   using assms has_integral_spike_finite by blast
  2391 
  2392 lemma has_integral_bound_spike_finite:
  2393   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  2394   assumes "0 \<le> B" "finite S"
  2395       and f: "(f has_integral i) (cbox a b)"
  2396       and leB: "\<And>x. x \<in> cbox a b - S \<Longrightarrow> norm (f x) \<le> B"
  2397     shows "norm i \<le> B * content (cbox a b)"
  2398 proof -
  2399   define g where "g \<equiv> (\<lambda>x. if x \<in> S then 0 else f x)"
  2400   then have "\<And>x. x \<in> cbox a b - S \<Longrightarrow> norm (g x) \<le> B"
  2401     using leB by simp
  2402   moreover have "(g has_integral i) (cbox a b)"
  2403     using has_integral_spike_finite [OF \<open>finite S\<close> _ f]
  2404     by (simp add: g_def)
  2405   ultimately show ?thesis
  2406     by (simp add: \<open>0 \<le> B\<close> g_def has_integral_bound)
  2407 qed
  2408 
  2409 corollary has_integral_bound_real:
  2410   fixes f :: "real \<Rightarrow> 'b::real_normed_vector"
  2411   assumes "0 \<le> B" "finite S"
  2412       and "(f has_integral i) {a..b}"
  2413       and "\<And>x. x \<in> {a..b} - S \<Longrightarrow> norm (f x) \<le> B"
  2414     shows "norm i \<le> B * content {a..b}"
  2415   by (metis assms box_real(2) has_integral_bound_spike_finite)
  2416 
  2417 
  2418 subsection \<open>In particular, the boundary of an interval is negligible\<close>
  2419 
  2420 lemma negligible_frontier_interval: "negligible(cbox (a::'a::euclidean_space) b - box a b)"
  2421 proof -
  2422   let ?A = "\<Union>((\<lambda>k. {x. x\<bullet>k = a\<bullet>k} \<union> {x::'a. x\<bullet>k = b\<bullet>k}) ` Basis)"
  2423   have "negligible ?A"
  2424     by (force simp add: negligible_Union[OF finite_imageI])
  2425   moreover have "cbox a b - box a b \<subseteq> ?A"
  2426     by (force simp add: mem_box)
  2427   ultimately show ?thesis
  2428     by (rule negligible_subset)
  2429 qed
  2430 
  2431 lemma has_integral_spike_interior:
  2432   assumes f: "(f has_integral y) (cbox a b)" and gf: "\<And>x. x \<in> box a b \<Longrightarrow> g x = f x"
  2433   shows "(g has_integral y) (cbox a b)"
  2434   apply (rule has_integral_spike[OF negligible_frontier_interval _ f])
  2435   using gf by auto
  2436 
  2437 lemma has_integral_spike_interior_eq:
  2438   assumes "\<And>x. x \<in> box a b \<Longrightarrow> g x = f x"
  2439   shows "(f has_integral y) (cbox a b) \<longleftrightarrow> (g has_integral y) (cbox a b)"
  2440   by (metis assms has_integral_spike_interior)
  2441 
  2442 lemma integrable_spike_interior:
  2443   assumes "\<And>x. x \<in> box a b \<Longrightarrow> g x = f x"
  2444     and "f integrable_on cbox a b"
  2445   shows "g integrable_on cbox a b"
  2446   using assms has_integral_spike_interior_eq by blast
  2447 
  2448 
  2449 subsection \<open>Integrability of continuous functions\<close>
  2450 
  2451 lemma operative_approximableI:
  2452   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  2453   assumes "0 \<le> e"
  2454   shows "operative conj True (\<lambda>i. \<exists>g. (\<forall>x\<in>i. norm (f x - g (x::'b)) \<le> e) \<and> g integrable_on i)"
  2455 proof -
  2456   interpret comm_monoid conj True
  2457     by standard auto
  2458   show ?thesis
  2459   proof (standard, safe)
  2460     fix a b :: 'b
  2461     show "\<exists>g. (\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b"
  2462       if "box a b = {}" for a b
  2463       apply (rule_tac x=f in exI)
  2464       using assms that by (auto simp: content_eq_0_interior)
  2465     {
  2466       fix c g and k :: 'b
  2467       assume fg: "\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e" and g: "g integrable_on cbox a b"
  2468       assume k: "k \<in> Basis"
  2469       show "\<exists>g. (\<forall>x\<in>cbox a b \<inter> {x. x \<bullet> k \<le> c}. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b \<inter> {x. x \<bullet> k \<le> c}"
  2470            "\<exists>g. (\<forall>x\<in>cbox a b \<inter> {x. c \<le> x \<bullet> k}. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b \<inter> {x. c \<le> x \<bullet> k}"
  2471          apply (rule_tac[!] x=g in exI)
  2472         using fg integrable_split[OF g k] by auto
  2473     }
  2474     show "\<exists>g. (\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b"
  2475       if fg1: "\<forall>x\<in>cbox a b \<inter> {x. x \<bullet> k \<le> c}. norm (f x - g1 x) \<le> e" 
  2476         and g1: "g1 integrable_on cbox a b \<inter> {x. x \<bullet> k \<le> c}"
  2477         and fg2: "\<forall>x\<in>cbox a b \<inter> {x. c \<le> x \<bullet> k}. norm (f x - g2 x) \<le> e" 
  2478         and g2: "g2 integrable_on cbox a b \<inter> {x. c \<le> x \<bullet> k}" 
  2479         and k: "k \<in> Basis"
  2480       for c k g1 g2
  2481     proof -
  2482       let ?g = "\<lambda>x. if x\<bullet>k = c then f x else if x\<bullet>k \<le> c then g1 x else g2 x"
  2483       show "\<exists>g. (\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b"
  2484       proof (intro exI conjI ballI)
  2485         show "norm (f x - ?g x) \<le> e" if "x \<in> cbox a b" for x
  2486           by (auto simp: that assms fg1 fg2)
  2487         show "?g integrable_on cbox a b"
  2488         proof -
  2489           have "?g integrable_on cbox a b \<inter> {x. x \<bullet> k \<le> c}" "?g integrable_on cbox a b \<inter> {x. x \<bullet> k \<ge> c}"
  2490             by(rule integrable_spike[OF _ negligible_standard_hyperplane[of k c]], use k g1 g2 in auto)+
  2491           with has_integral_split[OF _ _ k] show ?thesis
  2492             unfolding integrable_on_def by blast
  2493         qed
  2494       qed
  2495     qed
  2496   qed
  2497 qed
  2498 
  2499 lemma comm_monoid_set_F_and: "comm_monoid_set.F (\<and>) True f s \<longleftrightarrow> (finite s \<longrightarrow> (\<forall>x\<in>s. f x))"
  2500 proof -
  2501   interpret bool: comm_monoid_set "(\<and>)" True
  2502     proof qed auto
  2503   show ?thesis
  2504     by (induction s rule: infinite_finite_induct) auto
  2505 qed
  2506 
  2507 lemma approximable_on_division:
  2508   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  2509   assumes "0 \<le> e"
  2510     and d: "d division_of (cbox a b)"
  2511     and f: "\<forall>i\<in>d. \<exists>g. (\<forall>x\<in>i. norm (f x - g x) \<le> e) \<and> g integrable_on i"
  2512   obtains g where "\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e" "g integrable_on cbox a b"
  2513 proof -
  2514   interpret operative conj True "\<lambda>i. \<exists>g. (\<forall>x\<in>i. norm (f x - g (x::'b)) \<le> e) \<and> g integrable_on i"
  2515     using \<open>0 \<le> e\<close> by (rule operative_approximableI)
  2516   from f local.division [OF d] that show thesis
  2517     by auto
  2518 qed
  2519 
  2520 lemma integrable_continuous:
  2521   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  2522   assumes "continuous_on (cbox a b) f"
  2523   shows "f integrable_on cbox a b"
  2524 proof (rule integrable_uniform_limit)
  2525   fix e :: real
  2526   assume e: "e > 0"
  2527   then obtain d where "0 < d" and d: "\<And>x x'. \<lbrakk>x \<in> cbox a b; x' \<in> cbox a b; dist x' x < d\<rbrakk> \<Longrightarrow> dist (f x') (f x) < e"
  2528     using compact_uniformly_continuous[OF assms compact_cbox] unfolding uniformly_continuous_on_def by metis
  2529   obtain p where ptag: "p tagged_division_of cbox a b" and finep: "(\<lambda>x. ball x d) fine p"
  2530     using fine_division_exists[OF gauge_ball[OF \<open>0 < d\<close>], of a b] .
  2531   have *: "\<forall>i\<in>snd ` p. \<exists>g. (\<forall>x\<in>i. norm (f x - g x) \<le> e) \<and> g integrable_on i"
  2532   proof (safe, unfold snd_conv)
  2533     fix x l
  2534     assume as: "(x, l) \<in> p"
  2535     obtain a b where l: "l = cbox a b"
  2536       using as ptag by blast
  2537     then have x: "x \<in> cbox a b"
  2538       using as ptag by auto
  2539     show "\<exists>g. (\<forall>x\<in>l. norm (f x - g x) \<le> e) \<and> g integrable_on l"
  2540       apply (rule_tac x="\<lambda>y. f x" in exI)
  2541     proof safe
  2542       show "(\<lambda>y. f x) integrable_on l"
  2543         unfolding integrable_on_def l by blast
  2544     next
  2545       fix y
  2546       assume y: "y \<in> l"
  2547       then have "y \<in> ball x d"
  2548         using as finep by fastforce
  2549       then show "norm (f y - f x) \<le> e"
  2550         using d x y as l
  2551         by (metis dist_commute dist_norm less_imp_le mem_ball ptag subsetCE tagged_division_ofD(3))
  2552     qed
  2553   qed
  2554   from e have "e \<ge> 0"
  2555     by auto
  2556   from approximable_on_division[OF this division_of_tagged_division[OF ptag] *]
  2557   show "\<exists>g. (\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b"
  2558     by metis
  2559 qed
  2560 
  2561 lemma integrable_continuous_interval:
  2562   fixes f :: "'b::ordered_euclidean_space \<Rightarrow> 'a::banach"
  2563   assumes "continuous_on {a..b} f"
  2564   shows "f integrable_on {a..b}"
  2565   by (metis assms integrable_continuous interval_cbox)
  2566 
  2567 lemmas integrable_continuous_real = integrable_continuous_interval[where 'b=real]
  2568 
  2569 lemma integrable_continuous_closed_segment:
  2570   fixes f :: "real \<Rightarrow> 'a::banach"
  2571   assumes "continuous_on (closed_segment a b) f"
  2572   shows "f integrable_on (closed_segment a b)"
  2573   using assms
  2574   by (auto intro!: integrable_continuous_interval simp: closed_segment_eq_real_ivl)
  2575 
  2576 
  2577 subsection \<open>Specialization of additivity to one dimension\<close>
  2578 
  2579 
  2580 subsection \<open>A useful lemma allowing us to factor out the content size\<close>
  2581 
  2582 lemma has_integral_factor_content:
  2583   "(f has_integral i) (cbox a b) \<longleftrightarrow>
  2584     (\<forall>e>0. \<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow>
  2585       norm (sum (\<lambda>(x,k). content k *\<^sub>R f x) p - i) \<le> e * content (cbox a b)))"
  2586 proof (cases "content (cbox a b) = 0")
  2587   case True
  2588   have "\<And>e p. p tagged_division_of cbox a b \<Longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)) \<le> e * content (cbox a b)"
  2589     unfolding sum_content_null[OF True] True by force
  2590   moreover have "i = 0" 
  2591     if "\<And>e. e > 0 \<Longrightarrow> \<exists>d. gauge d \<and>
  2592               (\<forall>p. p tagged_division_of cbox a b \<and>
  2593                    d fine p \<longrightarrow>
  2594                    norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - i) \<le> e * content (cbox a b))"
  2595     using that [of 1]
  2596     by (force simp add: True sum_content_null[OF True] intro: fine_division_exists[of _ a b])
  2597   ultimately show ?thesis
  2598     unfolding has_integral_null_eq[OF True]
  2599     by (force simp add: )
  2600 next
  2601   case False
  2602   then have F: "0 < content (cbox a b)"
  2603     using zero_less_measure_iff by blast
  2604   let ?P = "\<lambda>e opp. \<exists>d. gauge d \<and>
  2605     (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow> opp (norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - i)) e)"
  2606   show ?thesis
  2607     apply (subst has_integral)
  2608   proof safe
  2609     fix e :: real
  2610     assume e: "e > 0"
  2611     { assume "\<forall>e>0. ?P e (<)"
  2612       then show "?P (e * content (cbox a b)) (\<le>)"
  2613         apply (rule allE [where x="e * content (cbox a b)"])
  2614         apply (elim impE ex_forward conj_forward)
  2615         using F e apply (auto simp add: algebra_simps)
  2616         done }
  2617     { assume "\<forall>e>0. ?P (e * content (cbox a b)) (\<le>)"
  2618       then show "?P e (<)"
  2619         apply (rule allE [where x="e/2 / content (cbox a b)"])
  2620         apply (elim impE ex_forward conj_forward)
  2621         using F e apply (auto simp add: algebra_simps)
  2622         done }
  2623   qed
  2624 qed
  2625 
  2626 lemma has_integral_factor_content_real:
  2627   "(f has_integral i) {a..b::real} \<longleftrightarrow>
  2628     (\<forall>e>0. \<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b}  \<and> d fine p \<longrightarrow>
  2629       norm (sum (\<lambda>(x,k). content k *\<^sub>R f x) p - i) \<le> e * content {a..b} ))"
  2630   unfolding box_real[symmetric]
  2631   by (rule has_integral_factor_content)
  2632 
  2633 
  2634 subsection \<open>Fundamental theorem of calculus\<close>
  2635 
  2636 lemma interval_bounds_real:
  2637   fixes q b :: real
  2638   assumes "a \<le> b"
  2639   shows "Sup {a..b} = b"
  2640     and "Inf {a..b} = a"
  2641   using assms by auto
  2642 
  2643 theorem fundamental_theorem_of_calculus:
  2644   fixes f :: "real \<Rightarrow> 'a::banach"
  2645   assumes "a \<le> b" 
  2646       and vecd: "\<And>x. x \<in> {a..b} \<Longrightarrow> (f has_vector_derivative f' x) (at x within {a..b})"
  2647   shows "(f' has_integral (f b - f a)) {a..b}"
  2648   unfolding has_integral_factor_content box_real[symmetric]
  2649 proof safe
  2650   fix e :: real
  2651   assume "e > 0"
  2652   then have "\<forall>x. \<exists>d>0. x \<in> {a..b} \<longrightarrow>
  2653          (\<forall>y\<in>{a..b}. norm (y-x) < d \<longrightarrow> norm (f y - f x - (y-x) *\<^sub>R f' x) \<le> e * norm (y-x))"
  2654     using vecd[unfolded has_vector_derivative_def has_derivative_within_alt] by blast
  2655   then obtain d where d: "\<And>x. 0 < d x"
  2656                  "\<And>x y. \<lbrakk>x \<in> {a..b}; y \<in> {a..b}; norm (y-x) < d x\<rbrakk>
  2657                         \<Longrightarrow> norm (f y - f x - (y-x) *\<^sub>R f' x) \<le> e * norm (y-x)"
  2658     by metis  
  2659   show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow>
  2660     norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f' x) - (f b - f a)) \<le> e * content (cbox a b))"
  2661   proof (rule exI, safe)
  2662     show "gauge (\<lambda>x. ball x (d x))"
  2663       using d(1) gauge_ball_dependent by blast
  2664   next
  2665     fix p
  2666     assume ptag: "p tagged_division_of cbox a b" and finep: "(\<lambda>x. ball x (d x)) fine p"
  2667     have ba: "b - a = (\<Sum>(x,K)\<in>p. Sup K - Inf K)" "f b - f a = (\<Sum>(x,K)\<in>p. f(Sup K) - f(Inf K))"
  2668       using additive_tagged_division_1[where f= "\<lambda>x. x"] additive_tagged_division_1[where f= f]
  2669             \<open>a \<le> b\<close> ptag by auto
  2670     have "norm (\<Sum>(x, K) \<in> p. (content K *\<^sub>R f' x) - (f (Sup K) - f (Inf K)))
  2671           \<le> (\<Sum>n\<in>p. e * (case n of (x, k) \<Rightarrow> Sup k - Inf k))"
  2672     proof (rule sum_norm_le,safe)
  2673       fix x K
  2674       assume "(x, K) \<in> p"
  2675       then have "x \<in> K" and kab: "K \<subseteq> cbox a b"
  2676         using ptag by blast+
  2677       then obtain u v where k: "K = cbox u v" and "x \<in> K" and kab: "K \<subseteq> cbox a b"
  2678         using ptag \<open>(x, K) \<in> p\<close> by auto 
  2679       have "u \<le> v"
  2680         using \<open>x \<in> K\<close> unfolding k by auto
  2681       have ball: "\<forall>y\<in>K. y \<in> ball x (d x)"
  2682         using finep \<open>(x, K) \<in> p\<close> by blast
  2683       have "u \<in> K" "v \<in> K"
  2684         by (simp_all add: \<open>u \<le> v\<close> k)
  2685       have "norm ((v - u) *\<^sub>R f' x - (f v - f u)) = norm (f u - f x - (u - x) *\<^sub>R f' x - (f v - f x - (v - x) *\<^sub>R f' x))"
  2686         by (auto simp add: algebra_simps)
  2687       also have "... \<le> norm (f u - f x - (u - x) *\<^sub>R f' x) + norm (f v - f x - (v - x) *\<^sub>R f' x)"
  2688         by (rule norm_triangle_ineq4)
  2689       finally have "norm ((v - u) *\<^sub>R f' x - (f v - f u)) \<le>
  2690         norm (f u - f x - (u - x) *\<^sub>R f' x) + norm (f v - f x - (v - x) *\<^sub>R f' x)" .
  2691       also have "\<dots> \<le> e * norm (u - x) + e * norm (v - x)"
  2692       proof (rule add_mono)
  2693         show "norm (f u - f x - (u - x) *\<^sub>R f' x) \<le> e * norm (u - x)"
  2694           apply (rule d(2)[of x u])
  2695           using \<open>x \<in> K\<close> kab \<open>u \<in> K\<close> ball dist_real_def by (auto simp add:dist_real_def)
  2696         show "norm (f v - f x - (v - x) *\<^sub>R f' x) \<le> e * norm (v - x)"
  2697           apply (rule d(2)[of x v])
  2698           using \<open>x \<in> K\<close> kab \<open>v \<in> K\<close> ball dist_real_def by (auto simp add:dist_real_def)
  2699       qed
  2700       also have "\<dots> \<le> e * (Sup K - Inf K)"
  2701         using \<open>x \<in> K\<close> by (auto simp: k interval_bounds_real[OF \<open>u \<le> v\<close>] field_simps)
  2702       finally show "norm (content K *\<^sub>R f' x - (f (Sup K) - f (Inf K))) \<le> e * (Sup K - Inf K)"
  2703         using \<open>u \<le> v\<close> by (simp add: k)
  2704     qed
  2705     with \<open>a \<le> b\<close> show "norm ((\<Sum>(x, K)\<in>p. content K *\<^sub>R f' x) - (f b - f a)) \<le> e * content (cbox a b)"
  2706       by (auto simp: ba split_def sum_subtractf [symmetric] sum_distrib_left)
  2707   qed
  2708 qed
  2709 
  2710 lemma ident_has_integral:
  2711   fixes a::real
  2712   assumes "a \<le> b"
  2713   shows "((\<lambda>x. x) has_integral (b\<^sup>2 - a\<^sup>2)/2) {a..b}"
  2714 proof -
  2715   have "((\<lambda>x. x) has_integral inverse 2 * b\<^sup>2 - inverse 2 * a\<^sup>2) {a..b}"
  2716     apply (rule fundamental_theorem_of_calculus [OF assms])
  2717     unfolding power2_eq_square
  2718     by (rule derivative_eq_intros | simp)+
  2719   then show ?thesis
  2720     by (simp add: field_simps)
  2721 qed
  2722 
  2723 lemma integral_ident [simp]:
  2724   fixes a::real
  2725   assumes "a \<le> b"
  2726   shows "integral {a..b} (\<lambda>x. x) = (if a \<le> b then (b\<^sup>2 - a\<^sup>2)/2 else 0)"
  2727   by (metis assms ident_has_integral integral_unique)
  2728 
  2729 lemma ident_integrable_on:
  2730   fixes a::real
  2731   shows "(\<lambda>x. x) integrable_on {a..b}"
  2732 by (metis atLeastatMost_empty_iff integrable_on_def has_integral_empty ident_has_integral)
  2733 
  2734 
  2735 subsection \<open>Taylor series expansion\<close>
  2736 
  2737 lemma mvt_integral:
  2738   fixes f::"'a::real_normed_vector\<Rightarrow>'b::banach"
  2739   assumes f'[derivative_intros]:
  2740     "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
  2741   assumes line_in: "\<And>t. t \<in> {0..1} \<Longrightarrow> x + t *\<^sub>R y \<in> S"
  2742   shows "f (x + y) - f x = integral {0..1} (\<lambda>t. f' (x + t *\<^sub>R y) y)" (is ?th1)
  2743 proof -
  2744   from assms have subset: "(\<lambda>xa. x + xa *\<^sub>R y) ` {0..1} \<subseteq> S" by auto
  2745   note [derivative_intros] =
  2746     has_derivative_subset[OF _ subset]
  2747     has_derivative_in_compose[where f="(\<lambda>xa. x + xa *\<^sub>R y)" and g = f]
  2748   note [continuous_intros] =
  2749     continuous_on_compose2[where f="(\<lambda>xa. x + xa *\<^sub>R y)"]
  2750     continuous_on_subset[OF _ subset]
  2751   have "\<And>t. t \<in> {0..1} \<Longrightarrow>
  2752     ((\<lambda>t. f (x + t *\<^sub>R y)) has_vector_derivative f' (x + t *\<^sub>R y) y)
  2753     (at t within {0..1})"
  2754     using assms
  2755     by (auto simp: has_vector_derivative_def
  2756         linear_cmul[OF has_derivative_linear[OF f'], symmetric]
  2757       intro!: derivative_eq_intros)
  2758   from fundamental_theorem_of_calculus[rule_format, OF _ this]
  2759   show ?th1
  2760     by (auto intro!: integral_unique[symmetric])
  2761 qed
  2762 
  2763 lemma (in bounded_bilinear) sum_prod_derivatives_has_vector_derivative:
  2764   assumes "p>0"
  2765   and f0: "Df 0 = f"
  2766   and Df: "\<And>m t. m < p \<Longrightarrow> a \<le> t \<Longrightarrow> t \<le> b \<Longrightarrow>
  2767     (Df m has_vector_derivative Df (Suc m) t) (at t within {a..b})"
  2768   and g0: "Dg 0 = g"
  2769   and Dg: "\<And>m t. m < p \<Longrightarrow> a \<le> t \<Longrightarrow> t \<le> b \<Longrightarrow>
  2770     (Dg m has_vector_derivative Dg (Suc m) t) (at t within {a..b})"
  2771   and ivl: "a \<le> t" "t \<le> b"
  2772   shows "((\<lambda>t. \<Sum>i<p. (-1)^i *\<^sub>R prod (Df i t) (Dg (p - Suc i) t))
  2773     has_vector_derivative
  2774       prod (f t) (Dg p t) - (-1)^p *\<^sub>R prod (Df p t) (g t))
  2775     (at t within {a..b})"
  2776   using assms
  2777 proof cases
  2778   assume p: "p \<noteq> 1"
  2779   define p' where "p' = p - 2"
  2780   from assms p have p': "{..<p} = {..Suc p'}" "p = Suc (Suc p')"
  2781     by (auto simp: p'_def)
  2782   have *: "\<And>i. i \<le> p' \<Longrightarrow> Suc (Suc p' - i) = (Suc (Suc p') - i)"
  2783     by auto
  2784   let ?f = "\<lambda>i. (-1) ^ i *\<^sub>R (prod (Df i t) (Dg ((p - i)) t))"
  2785   have "(\<Sum>i<p. (-1) ^ i *\<^sub>R (prod (Df i t) (Dg (Suc (p - Suc i)) t) +
  2786     prod (Df (Suc i) t) (Dg (p - Suc i) t))) =
  2787     (\<Sum>i\<le>(Suc p'). ?f i - ?f (Suc i))"
  2788     by (auto simp: algebra_simps p'(2) numeral_2_eq_2 * lessThan_Suc_atMost)
  2789   also note sum_telescope
  2790   finally
  2791   have "(\<Sum>i<p. (-1) ^ i *\<^sub>R (prod (Df i t) (Dg (Suc (p - Suc i)) t) +
  2792     prod (Df (Suc i) t) (Dg (p - Suc i) t)))
  2793     = prod (f t) (Dg p t) - (- 1) ^ p *\<^sub>R prod (Df p t) (g t)"
  2794     unfolding p'[symmetric]
  2795     by (simp add: assms)
  2796   thus ?thesis
  2797     using assms
  2798     by (auto intro!: derivative_eq_intros has_vector_derivative)
  2799 qed (auto intro!: derivative_eq_intros has_vector_derivative)
  2800 
  2801 lemma
  2802   fixes f::"real\<Rightarrow>'a::banach"
  2803   assumes "p>0"
  2804   and f0: "Df 0 = f"
  2805   and Df: "\<And>m t. m < p \<Longrightarrow> a \<le> t \<Longrightarrow> t \<le> b \<Longrightarrow>
  2806     (Df m has_vector_derivative Df (Suc m) t) (at t within {a..b})"
  2807   and ivl: "a \<le> b"
  2808   defines "i \<equiv> \<lambda>x. ((b - x) ^ (p - 1) / fact (p - 1)) *\<^sub>R Df p x"
  2809   shows Taylor_has_integral:
  2810     "(i has_integral f b - (\<Sum>i<p. ((b-a) ^ i / fact i) *\<^sub>R Df i a)) {a..b}"
  2811   and Taylor_integral:
  2812     "f b = (\<Sum>i<p. ((b-a) ^ i / fact i) *\<^sub>R Df i a) + integral {a..b} i"
  2813   and Taylor_integrable:
  2814     "i integrable_on {a..b}"
  2815 proof goal_cases
  2816   case 1
  2817   interpret bounded_bilinear "scaleR::real\<Rightarrow>'a\<Rightarrow>'a"
  2818     by (rule bounded_bilinear_scaleR)
  2819   define g where "g s = (b - s)^(p - 1)/fact (p - 1)" for s
  2820   define Dg where [abs_def]:
  2821     "Dg n s = (if n < p then (-1)^n * (b - s)^(p - 1 - n) / fact (p - 1 - n) else 0)" for n s
  2822   have g0: "Dg 0 = g"
  2823     using \<open>p > 0\<close>
  2824     by (auto simp add: Dg_def divide_simps g_def split: if_split_asm)
  2825   {
  2826     fix m
  2827     assume "p > Suc m"
  2828     hence "p - Suc m = Suc (p - Suc (Suc m))"
  2829       by auto
  2830     hence "real (p - Suc m) * fact (p - Suc (Suc m)) = fact (p - Suc m)"
  2831       by auto
  2832   } note fact_eq = this
  2833   have Dg: "\<And>m t. m < p \<Longrightarrow> a \<le> t \<Longrightarrow> t \<le> b \<Longrightarrow>
  2834     (Dg m has_vector_derivative Dg (Suc m) t) (at t within {a..b})"
  2835     unfolding Dg_def
  2836     by (auto intro!: derivative_eq_intros simp: has_vector_derivative_def fact_eq divide_simps)
  2837   let ?sum = "\<lambda>t. \<Sum>i<p. (- 1) ^ i *\<^sub>R Dg i t *\<^sub>R Df (p - Suc i) t"
  2838   from sum_prod_derivatives_has_vector_derivative[of _ Dg _ _ _ Df,
  2839       OF \<open>p > 0\<close> g0 Dg f0 Df]
  2840   have deriv: "\<And>t. a \<le> t \<Longrightarrow> t \<le> b \<Longrightarrow>
  2841     (?sum has_vector_derivative
  2842       g t *\<^sub>R Df p t - (- 1) ^ p *\<^sub>R Dg p t *\<^sub>R f t) (at t within {a..b})"
  2843     by auto
  2844   from fundamental_theorem_of_calculus[rule_format, OF \<open>a \<le> b\<close> deriv]
  2845   have "(i has_integral ?sum b - ?sum a) {a..b}"
  2846     using atLeastatMost_empty'[simp del]
  2847     by (simp add: i_def g_def Dg_def)
  2848   also
  2849   have one: "(- 1) ^ p' * (- 1) ^ p' = (1::real)"
  2850     and "{..<p} \<inter> {i. p = Suc i} = {p - 1}"
  2851     for p'
  2852     using \<open>p > 0\<close>
  2853     by (auto simp: power_mult_distrib[symmetric])
  2854   then have "?sum b = f b"
  2855     using Suc_pred'[OF \<open>p > 0\<close>]
  2856     by (simp add: diff_eq_eq Dg_def power_0_left le_Suc_eq if_distrib
  2857         if_distribR sum.If_cases f0)
  2858   also
  2859   have "{..<p} = (\<lambda>x. p - x - 1) ` {..<p}"
  2860   proof safe
  2861     fix x
  2862     assume "x < p"
  2863     thus "x \<in> (\<lambda>x. p - x - 1) ` {..<p}"
  2864       by (auto intro!: image_eqI[where x = "p - x - 1"])
  2865   qed simp
  2866   from _ this
  2867   have "?sum a = (\<Sum>i<p. ((b-a) ^ i / fact i) *\<^sub>R Df i a)"
  2868     by (rule sum.reindex_cong) (auto simp add: inj_on_def Dg_def one)
  2869   finally show c: ?case .
  2870   case 2 show ?case using c integral_unique
  2871     by (metis (lifting) add.commute diff_eq_eq integral_unique)
  2872   case 3 show ?case using c by force
  2873 qed
  2874 
  2875 
  2876 subsection \<open>Only need trivial subintervals if the interval itself is trivial\<close>
  2877 
  2878 proposition division_of_nontrivial:
  2879   fixes \<D> :: "'a::euclidean_space set set"
  2880   assumes sdiv: "\<D> division_of (cbox a b)"
  2881      and cont0: "content (cbox a b) \<noteq> 0"
  2882   shows "{k. k \<in> \<D> \<and> content k \<noteq> 0} division_of (cbox a b)"
  2883   using sdiv
  2884 proof (induction "card \<D>" arbitrary: \<D> rule: less_induct)
  2885   case less
  2886   note \<D> = division_ofD[OF less.prems]
  2887   {
  2888     presume *: "{k \<in> \<D>. content k \<noteq> 0} \<noteq> \<D> \<Longrightarrow> ?case"
  2889     then show ?case
  2890       using less.prems by fastforce
  2891   }
  2892   assume noteq: "{k \<in> \<D>. content k \<noteq> 0} \<noteq> \<D>"
  2893   then obtain K c d where "K \<in> \<D>" and contk: "content K = 0" and keq: "K = cbox c d"
  2894     using \<D>(4) by blast 
  2895   then have "card \<D> > 0"
  2896     unfolding card_gt_0_iff using less by auto
  2897   then have card: "card (\<D> - {K}) < card \<D>"
  2898     using less \<open>K \<in> \<D>\<close> by (simp add: \<D>(1))
  2899   have closed: "closed (\<Union>(\<D> - {K}))"
  2900     using less.prems by auto
  2901   have "x islimpt \<Union>(\<D> - {K})" if "x \<in> K" for x 
  2902     unfolding islimpt_approachable
  2903   proof (intro allI impI)
  2904     fix e::real
  2905     assume "e > 0"
  2906     obtain i where i: "c\<bullet>i = d\<bullet>i" "i\<in>Basis"
  2907       using contk \<D>(3) [OF \<open>K \<in> \<D>\<close>] unfolding box_ne_empty keq
  2908       by (meson content_eq_0 dual_order.antisym)
  2909     then have xi: "x\<bullet>i = d\<bullet>i"
  2910       using \<open>x \<in> K\<close> unfolding keq mem_box by (metis antisym)
  2911     define y where "y = (\<Sum>j\<in>Basis. (if j = i then if c\<bullet>i \<le> (a\<bullet>i + b\<bullet>i)/2 then c\<bullet>i +
  2912       min e (b\<bullet>i - c\<bullet>i)/2 else c\<bullet>i - min e (c\<bullet>i - a\<bullet>i)/2 else x\<bullet>j) *\<^sub>R j)"
  2913     show "\<exists>x'\<in>\<Union>(\<D> - {K}). x' \<noteq> x \<and> dist x' x < e"
  2914     proof (intro bexI conjI)
  2915       have "d \<in> cbox c d"
  2916         using \<D>(3)[OF \<open>K \<in> \<D>\<close>] by (simp add: box_ne_empty(1) keq mem_box(2))
  2917       then have "d \<in> cbox a b"
  2918         using \<D>(2)[OF \<open>K \<in> \<D>\<close>] by (auto simp: keq)
  2919       then have di: "a \<bullet> i \<le> d \<bullet> i \<and> d \<bullet> i \<le> b \<bullet> i"
  2920         using \<open>i \<in> Basis\<close> mem_box(2) by blast
  2921       then have xyi: "y\<bullet>i \<noteq> x\<bullet>i"
  2922         unfolding y_def i xi using \<open>e > 0\<close> cont0 \<open>i \<in> Basis\<close>
  2923         by (auto simp: content_eq_0 elim!: ballE[of _ _ i])
  2924       then show "y \<noteq> x"
  2925         unfolding euclidean_eq_iff[where 'a='a] using i by auto
  2926       have "norm (y-x) \<le> (\<Sum>b\<in>Basis. \<bar>(y - x) \<bullet> b\<bar>)"
  2927         by (rule norm_le_l1)
  2928       also have "... = \<bar>(y - x) \<bullet> i\<bar> + (\<Sum>b \<in> Basis - {i}. \<bar>(y - x) \<bullet> b\<bar>)"
  2929         by (meson finite_Basis i(2) sum.remove)
  2930       also have "... <  e + sum (\<lambda>i. 0) Basis"
  2931       proof (rule add_less_le_mono)
  2932         show "\<bar>(y-x) \<bullet> i\<bar> < e"
  2933           using di \<open>e > 0\<close> y_def i xi by (auto simp: inner_simps)
  2934         show "(\<Sum>i\<in>Basis - {i}. \<bar>(y-x) \<bullet> i\<bar>) \<le> (\<Sum>i\<in>Basis. 0)"
  2935           unfolding y_def by (auto simp: inner_simps)
  2936       qed 
  2937       finally have "norm (y-x) < e + sum (\<lambda>i. 0) Basis" .
  2938       then show "dist y x < e"
  2939         unfolding dist_norm by auto
  2940       have "y \<notin> K"
  2941         unfolding keq mem_box using i(1) i(2) xi xyi by fastforce
  2942       moreover have "y \<in> \<Union>\<D>"
  2943         using subsetD[OF \<D>(2)[OF \<open>K \<in> \<D>\<close>] \<open>x \<in> K\<close>] \<open>e > 0\<close> di i
  2944         by (auto simp: \<D> mem_box y_def field_simps elim!: ballE[of _ _ i])
  2945       ultimately show "y \<in> \<Union>(\<D> - {K})" by auto
  2946     qed
  2947   qed
  2948   then have "K \<subseteq> \<Union>(\<D> - {K})"
  2949     using closed closed_limpt by blast
  2950   then have "\<Union>(\<D> - {K}) = cbox a b"
  2951     unfolding \<D>(6)[symmetric] by auto
  2952   then have "\<D> - {K} division_of cbox a b"
  2953     by (metis Diff_subset less.prems division_of_subset \<D>(6))
  2954   then have "{ka \<in> \<D> - {K}. content ka \<noteq> 0} division_of (cbox a b)"
  2955     using card less.hyps by blast
  2956   moreover have "{ka \<in> \<D> - {K}. content ka \<noteq> 0} = {K \<in> \<D>. content K \<noteq> 0}"
  2957     using contk by auto
  2958   ultimately show ?case by auto
  2959 qed
  2960 
  2961 
  2962 subsection \<open>Integrability on subintervals\<close>
  2963 
  2964 lemma operative_integrableI:
  2965   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  2966   assumes "0 \<le> e"
  2967   shows "operative conj True (\<lambda>i. f integrable_on i)"
  2968 proof -
  2969   interpret comm_monoid conj True
  2970     by standard auto
  2971   have 1: "\<And>a b. box a b = {} \<Longrightarrow> f integrable_on cbox a b"
  2972     by (simp add: content_eq_0_interior integrable_on_null)
  2973   have 2: "\<And>a b c k.
  2974        \<lbrakk>k \<in> Basis;
  2975         f integrable_on cbox a b \<inter> {x. x \<bullet> k \<le> c};
  2976         f integrable_on cbox a b \<inter> {x. c \<le> x \<bullet> k}\<rbrakk>
  2977        \<Longrightarrow> f integrable_on cbox a b"
  2978     unfolding integrable_on_def by (auto intro!: has_integral_split)
  2979   show ?thesis
  2980     apply standard
  2981     using 1 2 by blast+
  2982 qed
  2983 
  2984 lemma integrable_subinterval:
  2985   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  2986   assumes f: "f integrable_on cbox a b"
  2987     and cd: "cbox c d \<subseteq> cbox a b"
  2988   shows "f integrable_on cbox c d"
  2989 proof -
  2990   interpret operative conj True "\<lambda>i. f integrable_on i"
  2991     using order_refl by (rule operative_integrableI)
  2992   show ?thesis
  2993   proof (cases "cbox c d = {}")
  2994     case True
  2995     then show ?thesis
  2996       using division [symmetric] f by (auto simp: comm_monoid_set_F_and)
  2997   next
  2998     case False
  2999     then show ?thesis
  3000       by (metis cd comm_monoid_set_F_and division division_of_finite f partial_division_extend_1)
  3001   qed
  3002 qed
  3003 
  3004 lemma integrable_subinterval_real:
  3005   fixes f :: "real \<Rightarrow> 'a::banach"
  3006   assumes "f integrable_on {a..b}"
  3007     and "{c..d} \<subseteq> {a..b}"
  3008   shows "f integrable_on {c..d}"
  3009   by (metis assms box_real(2) integrable_subinterval)
  3010 
  3011 subsection \<open>Combining adjacent intervals in 1 dimension\<close>
  3012 
  3013 lemma has_integral_combine:
  3014   fixes a b c :: real and j :: "'a::banach"
  3015   assumes "a \<le> c"
  3016       and "c \<le> b"
  3017       and ac: "(f has_integral i) {a..c}"
  3018       and cb: "(f has_integral j) {c..b}"
  3019   shows "(f has_integral (i + j)) {a..b}"
  3020 proof -
  3021   interpret operative_real "lift_option plus" "Some 0"
  3022     "\<lambda>i. if f integrable_on i then Some (integral i f) else None"
  3023     using operative_integralI by (rule operative_realI)
  3024   from \<open>a \<le> c\<close> \<open>c \<le> b\<close> ac cb coalesce_less_eq
  3025   have *: "lift_option (+)
  3026              (if f integrable_on {a..c} then Some (integral {a..c} f) else None)
  3027              (if f integrable_on {c..b} then Some (integral {c..b} f) else None) =
  3028             (if f integrable_on {a..b} then Some (integral {a..b} f) else None)"
  3029     by (auto simp: split: if_split_asm)
  3030   then have "f integrable_on cbox a b"
  3031     using ac cb by (auto split: if_split_asm)
  3032   with *
  3033   show ?thesis
  3034     using ac cb by (auto simp add: integrable_on_def integral_unique split: if_split_asm)
  3035 qed
  3036 
  3037 lemma integral_combine:
  3038   fixes f :: "real \<Rightarrow> 'a::banach"
  3039   assumes "a \<le> c"
  3040     and "c \<le> b"
  3041     and ab: "f integrable_on {a..b}"
  3042   shows "integral {a..c} f + integral {c..b} f = integral {a..b} f"
  3043 proof -
  3044   have "(f has_integral integral {a..c} f) {a..c}"
  3045     using ab \<open>c \<le> b\<close> integrable_subinterval_real by fastforce
  3046   moreover
  3047   have "(f has_integral integral {c..b} f) {c..b}"
  3048     using ab \<open>a \<le> c\<close> integrable_subinterval_real by fastforce
  3049   ultimately have "(f has_integral integral {a..c} f + integral {c..b} f) {a..b}"
  3050     using \<open>a \<le> c\<close> \<open>c \<le> b\<close> has_integral_combine by blast
  3051   then show ?thesis
  3052     by (simp add: has_integral_integrable_integral)
  3053 qed
  3054 
  3055 lemma integrable_combine:
  3056   fixes f :: "real \<Rightarrow> 'a::banach"
  3057   assumes "a \<le> c"
  3058     and "c \<le> b"
  3059     and "f integrable_on {a..c}"
  3060     and "f integrable_on {c..b}"
  3061   shows "f integrable_on {a..b}"
  3062   using assms
  3063   unfolding integrable_on_def
  3064   by (auto intro!: has_integral_combine)
  3065 
  3066 lemma integral_minus_sets:
  3067   fixes f::"real \<Rightarrow> 'a::banach"
  3068   shows "c \<le> a \<Longrightarrow> c \<le> b \<Longrightarrow> f integrable_on {c .. max a b} \<Longrightarrow>
  3069     integral {c .. a} f - integral {c .. b} f =
  3070     (if a \<le> b then - integral {a .. b} f else integral {b .. a} f)"
  3071   using integral_combine[of c a b f]  integral_combine[of c b a f]
  3072   by (auto simp: algebra_simps max_def)
  3073 
  3074 lemma integral_minus_sets':
  3075   fixes f::"real \<Rightarrow> 'a::banach"
  3076   shows "c \<ge> a \<Longrightarrow> c \<ge> b \<Longrightarrow> f integrable_on {min a b .. c} \<Longrightarrow>
  3077     integral {a .. c} f - integral {b .. c} f =
  3078     (if a \<le> b then integral {a .. b} f else - integral {b .. a} f)"
  3079   using integral_combine[of b a c f] integral_combine[of a b c f]
  3080   by (auto simp: algebra_simps min_def)
  3081 
  3082 subsection \<open>Reduce integrability to "local" integrability\<close>
  3083 
  3084 lemma integrable_on_little_subintervals:
  3085   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  3086   assumes "\<forall>x\<in>cbox a b. \<exists>d>0. \<forall>u v. x \<in> cbox u v \<and> cbox u v \<subseteq> ball x d \<and> cbox u v \<subseteq> cbox a b \<longrightarrow>
  3087     f integrable_on cbox u v"
  3088   shows "f integrable_on cbox a b"
  3089 proof -
  3090   interpret operative conj True "\<lambda>i. f integrable_on i"
  3091     using order_refl by (rule operative_integrableI)
  3092   have "\<forall>x. \<exists>d>0. x\<in>cbox a b \<longrightarrow> (\<forall>u v. x \<in> cbox u v \<and> cbox u v \<subseteq> ball x d \<and> cbox u v \<subseteq> cbox a b \<longrightarrow>
  3093     f integrable_on cbox u v)"
  3094     using assms by (metis zero_less_one)
  3095   then obtain d where d: "\<And>x. 0 < d x"
  3096      "\<And>x u v. \<lbrakk>x \<in> cbox a b; x \<in> cbox u v; cbox u v \<subseteq> ball x (d x); cbox u v \<subseteq> cbox a b\<rbrakk> 
  3097                \<Longrightarrow> f integrable_on cbox u v"
  3098     by metis
  3099   obtain p where p: "p tagged_division_of cbox a b" "(\<lambda>x. ball x (d x)) fine p"
  3100     using fine_division_exists[OF gauge_ball_dependent,of d a b] d(1) by blast 
  3101   then have sndp: "snd ` p division_of cbox a b"
  3102     by (metis division_of_tagged_division)
  3103   have "f integrable_on k" if "(x, k) \<in> p" for x k
  3104     using tagged_division_ofD(2-4)[OF p(1) that] fineD[OF p(2) that] d[of x] by auto
  3105   then show ?thesis
  3106     unfolding division [symmetric, OF sndp] comm_monoid_set_F_and
  3107     by auto
  3108 qed
  3109 
  3110 
  3111 subsection \<open>Second FTC or existence of antiderivative\<close>
  3112 
  3113 lemma integrable_const[intro]: "(\<lambda>x. c) integrable_on cbox a b"
  3114   unfolding integrable_on_def by blast
  3115 
  3116 lemma integral_has_vector_derivative_continuous_at:
  3117   fixes f :: "real \<Rightarrow> 'a::banach"
  3118   assumes f: "f integrable_on {a..b}"
  3119      and x: "x \<in> {a..b} - S"
  3120      and "finite S"
  3121      and fx: "continuous (at x within ({a..b} - S)) f"
  3122  shows "((\<lambda>u. integral {a..u} f) has_vector_derivative f x) (at x within ({a..b} - S))"
  3123 proof -
  3124   let ?I = "\<lambda>a b. integral {a..b} f"
  3125   { fix e::real
  3126     assume "e > 0"
  3127     obtain d where "d>0" and d: "\<And>x'. \<lbrakk>x' \<in> {a..b} - S; \<bar>x' - x\<bar> < d\<rbrakk> \<Longrightarrow> norm(f x' - f x) \<le> e"
  3128       using \<open>e>0\<close> fx by (auto simp: continuous_within_eps_delta dist_norm less_imp_le)
  3129     have "norm (integral {a..y} f - integral {a..x} f - (y-x) *\<^sub>R f x) \<le> e * \<bar>y - x\<bar>"
  3130            if y: "y \<in> {a..b} - S" and yx: "\<bar>y - x\<bar> < d" for y
  3131     proof (cases "y < x")
  3132       case False
  3133       have "f integrable_on {a..y}"
  3134         using f y by (simp add: integrable_subinterval_real)
  3135       then have Idiff: "?I a y - ?I a x = ?I x y"
  3136         using False x by (simp add: algebra_simps integral_combine)
  3137       have fux_int: "((\<lambda>u. f u - f x) has_integral integral {x..y} f - (y-x) *\<^sub>R f x) {x..y}"
  3138         apply (rule has_integral_diff)
  3139         using x y apply (auto intro: integrable_integral [OF integrable_subinterval_real [OF f]])
  3140         using has_integral_const_real [of "f x" x y] False
  3141         apply simp
  3142         done
  3143       have "\<And>xa. y - x < d \<Longrightarrow> (\<And>x'. a \<le> x' \<and> x' \<le> b \<and> x' \<notin> S \<Longrightarrow> \<bar>x' - x\<bar> < d \<Longrightarrow> norm (f x' - f x) \<le> e) \<Longrightarrow> 0 < e \<Longrightarrow> xa \<notin> S \<Longrightarrow> a \<le> x \<Longrightarrow> x \<notin> S \<Longrightarrow> y \<le> b \<Longrightarrow> y \<notin> S \<Longrightarrow> x \<le> xa \<Longrightarrow> xa \<le> y \<Longrightarrow> norm (f xa - f x) \<le> e"
  3144         using assms by auto
  3145       show ?thesis
  3146         using False
  3147         apply (simp add: abs_eq_content del: content_real_if measure_lborel_Icc)
  3148         apply (rule has_integral_bound_real[where f="(\<lambda>u. f u - f x)"])
  3149         using yx False d x y \<open>e>0\<close> assms by (auto simp: Idiff fux_int)
  3150     next
  3151       case True
  3152       have "f integrable_on {a..x}"
  3153         using f x by (simp add: integrable_subinterval_real)
  3154       then have Idiff: "?I a x - ?I a y = ?I y x"
  3155         using True x y by (simp add: algebra_simps integral_combine)
  3156       have fux_int: "((\<lambda>u. f u - f x) has_integral integral {y..x} f - (x - y) *\<^sub>R f x) {y..x}"
  3157         apply (rule has_integral_diff)
  3158         using x y apply (auto intro: integrable_integral [OF integrable_subinterval_real [OF f]])
  3159         using has_integral_const_real [of "f x" y x] True
  3160         apply simp
  3161         done
  3162       have "norm (integral {a..x} f - integral {a..y} f - (x - y) *\<^sub>R f x) \<le> e * \<bar>y - x\<bar>"
  3163         using True
  3164         apply (simp add: abs_eq_content del: content_real_if measure_lborel_Icc)
  3165         apply (rule has_integral_bound_real[where f="(\<lambda>u. f u - f x)"])
  3166         using yx True d x y \<open>e>0\<close> assms by (auto simp: Idiff fux_int)
  3167       then show ?thesis
  3168         by (simp add: algebra_simps norm_minus_commute)
  3169     qed
  3170     then have "\<exists>d>0. \<forall>y\<in>{a..b} - S. \<bar>y - x\<bar> < d \<longrightarrow> norm (integral {a..y} f - integral {a..x} f - (y-x) *\<^sub>R f x) \<le> e * \<bar>y - x\<bar>"
  3171       using \<open>d>0\<close> by blast
  3172   }
  3173   then show ?thesis
  3174     by (simp add: has_vector_derivative_def has_derivative_within_alt bounded_linear_scaleR_left)
  3175 qed
  3176 
  3177 
  3178 lemma integral_has_vector_derivative:
  3179   fixes f :: "real \<Rightarrow> 'a::banach"
  3180   assumes "continuous_on {a..b} f"
  3181     and "x \<in> {a..b}"
  3182   shows "((\<lambda>u. integral {a..u} f) has_vector_derivative f(x)) (at x within {a..b})"
  3183 using assms integral_has_vector_derivative_continuous_at [OF integrable_continuous_real]
  3184   by (fastforce simp: continuous_on_eq_continuous_within)
  3185 
  3186 lemma integral_has_real_derivative:
  3187   assumes "continuous_on {a..b} g"
  3188   assumes "t \<in> {a..b}"
  3189   shows "((\<lambda>x. integral {a..x} g) has_real_derivative g t) (at t within {a..b})"
  3190   using integral_has_vector_derivative[of a b g t] assms
  3191   by (auto simp: has_field_derivative_iff_has_vector_derivative)
  3192 
  3193 lemma antiderivative_continuous:
  3194   fixes q b :: real
  3195   assumes "continuous_on {a..b} f"
  3196   obtains g where "\<And>x. x \<in> {a..b} \<Longrightarrow> (g has_vector_derivative (f x::_::banach)) (at x within {a..b})"
  3197   using integral_has_vector_derivative[OF assms] by auto
  3198 
  3199 subsection \<open>Combined fundamental theorem of calculus\<close>
  3200 
  3201 lemma antiderivative_integral_continuous:
  3202   fixes f :: "real \<Rightarrow> 'a::banach"
  3203   assumes "continuous_on {a..b} f"
  3204   obtains g where "\<forall>u\<in>{a..b}. \<forall>v \<in> {a..b}. u \<le> v \<longrightarrow> (f has_integral (g v - g u)) {u..v}"
  3205 proof -
  3206   obtain g 
  3207     where g: "\<And>x. x \<in> {a..b} \<Longrightarrow> (g has_vector_derivative f x) (at x within {a..b})" 
  3208     using  antiderivative_continuous[OF assms] by metis
  3209   have "(f has_integral g v - g u) {u..v}" if "u \<in> {a..b}" "v \<in> {a..b}" "u \<le> v" for u v
  3210   proof -
  3211     have "\<And>x. x \<in> cbox u v \<Longrightarrow> (g has_vector_derivative f x) (at x within cbox u v)"
  3212       by (metis atLeastAtMost_iff atLeastatMost_subset_iff box_real(2) g
  3213           has_vector_derivative_within_subset subsetCE that(1,2))
  3214     then show ?thesis
  3215       by (metis box_real(2) that(3) fundamental_theorem_of_calculus)
  3216   qed
  3217   then show ?thesis
  3218     using that by blast
  3219 qed
  3220 
  3221 
  3222 subsection \<open>General "twiddling" for interval-to-interval function image\<close>
  3223 
  3224 lemma has_integral_twiddle:
  3225   assumes "0 < r"
  3226     and hg: "\<And>x. h(g x) = x"
  3227     and gh: "\<And>x. g(h x) = x"
  3228     and contg: "\<And>x. continuous (at x) g"
  3229     and g: "\<And>u v. \<exists>w z. g ` cbox u v = cbox w z"
  3230     and h: "\<And>u v. \<exists>w z. h ` cbox u v = cbox w z"
  3231     and r: "\<And>u v. content(g ` cbox u v) = r * content (cbox u v)"
  3232     and intfi: "(f has_integral i) (cbox a b)"
  3233   shows "((\<lambda>x. f(g x)) has_integral (1 / r) *\<^sub>R i) (h ` cbox a b)"
  3234 proof (cases "cbox a b = {}")
  3235   case True
  3236   then show ?thesis 
  3237     using intfi by auto
  3238 next
  3239   case False
  3240   obtain w z where wz: "h ` cbox a b = cbox w z"
  3241     using h by blast
  3242   have inj: "inj g" "inj h"
  3243     using hg gh injI by metis+
  3244   from h obtain ha hb where h_eq: "h ` cbox a b = cbox ha hb" by blast
  3245   have "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of h ` cbox a b \<and> d fine p 
  3246               \<longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f (g x)) - (1 / r) *\<^sub>R i) < e)"
  3247     if "e > 0" for e
  3248   proof -
  3249     have "e * r > 0" using that \<open>0 < r\<close> by simp
  3250     with intfi[unfolded has_integral]
  3251     obtain d where "gauge d"
  3252                and d: "\<And>p. p tagged_division_of cbox a b \<and> d fine p 
  3253                         \<Longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - i) < e * r" 
  3254       by metis
  3255     define d' where "d' x = g -` d (g x)" for x
  3256     show ?thesis
  3257     proof (rule_tac x=d' in exI, safe)
  3258       show "gauge d'"
  3259         using \<open>gauge d\<close> continuous_open_vimage[OF _ contg] by (auto simp: gauge_def d'_def)
  3260     next
  3261       fix p
  3262       assume ptag: "p tagged_division_of h ` cbox a b" and finep: "d' fine p"
  3263       note p = tagged_division_ofD[OF ptag]
  3264       have gab: "g y \<in> cbox a b" if "y \<in> K" "(x, K) \<in> p" for x y K
  3265         by (metis hg inj(2) inj_image_mem_iff p(3) subsetCE that that)
  3266       have gimp: "(\<lambda>(x,K). (g x, g ` K)) ` p tagged_division_of (cbox a b) \<and> 
  3267                   d fine (\<lambda>(x, k). (g x, g ` k)) ` p"
  3268         unfolding tagged_division_of
  3269       proof safe
  3270         show "finite ((\<lambda>(x, k). (g x, g ` k)) ` p)"
  3271           using ptag by auto
  3272         show "d fine (\<lambda>(x, k). (g x, g ` k)) ` p"
  3273           using finep unfolding fine_def d'_def by auto
  3274       next
  3275         fix x k
  3276         assume xk: "(x, k) \<in> p"
  3277         show "g x \<in> g ` k"
  3278           using p(2)[OF xk] by auto
  3279         show "\<exists>u v. g ` k = cbox u v"
  3280           using p(4)[OF xk] using assms(5-6) by auto
  3281         fix x' K' u
  3282         assume xk': "(x', K') \<in> p" and u: "u \<in> interior (g ` k)" "u \<in> interior (g ` K')"
  3283         have "interior k \<inter> interior K' \<noteq> {}"
  3284         proof 
  3285           assume "interior k \<inter> interior K' = {}"
  3286           moreover have "u \<in> g ` (interior k \<inter> interior K')"
  3287             using interior_image_subset[OF \<open>inj g\<close> contg] u
  3288             unfolding image_Int[OF inj(1)] by blast
  3289           ultimately show False by blast
  3290         qed
  3291         then have same: "(x, k) = (x', K')"
  3292           using ptag xk' xk by blast
  3293         then show "g x = g x'"
  3294           by auto
  3295         show "g u \<in> g ` K'"if "u \<in> k" for u
  3296           using that same by auto
  3297         show "g u \<in> g ` k"if "u \<in> K'" for u
  3298           using that same by auto
  3299       next
  3300         fix x
  3301         assume "x \<in> cbox a b"
  3302         then have "h x \<in>  \<Union>{k. \<exists>x. (x, k) \<in> p}"
  3303           using p(6) by auto
  3304         then obtain X y where "h x \<in> X" "(y, X) \<in> p" by blast
  3305         then show "x \<in> \<Union>{k. \<exists>x. (x, k) \<in> (\<lambda>(x, k). (g x, g ` k)) ` p}"
  3306           apply clarsimp
  3307           by (metis (no_types, lifting) assms(3) image_eqI pair_imageI)
  3308       qed (use gab in auto)
  3309       have *: "inj_on (\<lambda>(x, k). (g x, g ` k)) p"
  3310         using inj(1) unfolding inj_on_def by fastforce
  3311       have "(\<Sum>(x, k)\<in>(\<lambda>(x, k). (g x, g ` k)) ` p. content k *\<^sub>R f x) - i = r *\<^sub>R (\<Sum>(x, k)\<in>p. content k *\<^sub>R f (g x)) - i" (is "?l = _")
  3312         using r
  3313         apply (simp only: algebra_simps add_left_cancel scaleR_right.sum)
  3314         apply (subst sum.reindex_bij_betw[symmetric, where h="\<lambda>(x, k). (g x, g ` k)" and S=p])
  3315          apply (auto intro!: * sum.cong simp: bij_betw_def dest!: p(4))
  3316         done
  3317       also have "\<dots> = r *\<^sub>R ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f (g x)) - (1 / r) *\<^sub>R i)" (is "_ = ?r")
  3318         using \<open>0 < r\<close> by (auto simp: scaleR_diff_right)
  3319       finally have eq: "?l = ?r" .
  3320       show "norm ((\<Sum>(x,K)\<in>p. content K *\<^sub>R f (g x)) - (1 / r) *\<^sub>R i) < e"
  3321         using d[OF gimp] \<open>0 < r\<close> by (auto simp add: eq)
  3322     qed
  3323   qed
  3324   then show ?thesis
  3325     by (auto simp: h_eq has_integral)
  3326 qed
  3327 
  3328 
  3329 subsection \<open>Special case of a basic affine transformation\<close>
  3330 
  3331 lemma AE_lborel_inner_neq:
  3332   assumes k: "k \<in> Basis"
  3333   shows "AE x in lborel. x \<bullet> k \<noteq> c"
  3334 proof -
  3335   interpret finite_product_sigma_finite "\<lambda>_. lborel" Basis
  3336     proof qed simp
  3337 
  3338   have "emeasure lborel {x\<in>space lborel. x \<bullet> k = c} = emeasure (\<Pi>\<^sub>M j::'a\<in>Basis. lborel) (\<Pi>\<^sub>E j\<in>Basis. if j = k then {c} else UNIV)"
  3339     using k
  3340     by (auto simp add: lborel_eq[where 'a='a] emeasure_distr intro!: arg_cong2[where f=emeasure])
  3341        (auto simp: space_PiM PiE_iff extensional_def split: if_split_asm)
  3342   also have "\<dots> = (\<Prod>j\<in>Basis. emeasure lborel (if j = k then {c} else UNIV))"
  3343     by (intro measure_times) auto
  3344   also have "\<dots> = 0"
  3345     by (intro prod_zero bexI[OF _ k]) auto
  3346   finally show ?thesis
  3347     by (subst AE_iff_measurable[OF _ refl]) auto
  3348 qed
  3349 
  3350 lemma content_image_stretch_interval:
  3351   fixes m :: "'a::euclidean_space \<Rightarrow> real"
  3352   defines "s f x \<equiv> (\<Sum>k::'a\<in>Basis. (f k * (x\<bullet>k)) *\<^sub>R k)"
  3353   shows "content (s m ` cbox a b) = \<bar>\<Prod>k\<in>Basis. m k\<bar> * content (cbox a b)"
  3354 proof cases
  3355   have s[measurable]: "s f \<in> borel \<rightarrow>\<^sub>M borel" for f
  3356     by (auto simp: s_def[abs_def])
  3357   assume m: "\<forall>k\<in>Basis. m k \<noteq> 0"
  3358   then have s_comp_s: "s (\<lambda>k. 1 / m k) \<circ> s m = id" "s m \<circ> s (\<lambda>k. 1 / m k) = id"
  3359     by (auto simp: s_def[abs_def] fun_eq_iff euclidean_representation)
  3360   then have "inv (s (\<lambda>k. 1 / m k)) = s m" "bij (s (\<lambda>k. 1 / m k))"
  3361     by (auto intro: inv_unique_comp o_bij)
  3362   then have eq: "s m ` cbox a b = s (\<lambda>k. 1 / m k) -` cbox a b"
  3363     using bij_vimage_eq_inv_image[OF \<open>bij (s (\<lambda>k. 1 / m k))\<close>, of "cbox a b"] by auto
  3364   show ?thesis
  3365     using m unfolding eq measure_def
  3366     by (subst lborel_affine_euclidean[where c=m and t=0])
  3367        (simp_all add: emeasure_density measurable_sets_borel[OF s] abs_prod nn_integral_cmult
  3368                       s_def[symmetric] emeasure_distr vimage_comp s_comp_s enn2real_mult prod_nonneg)
  3369 next
  3370   assume "\<not> (\<forall>k\<in>Basis. m k \<noteq> 0)"
  3371   then obtain k where k: "k \<in> Basis" "m k = 0" by auto
  3372   then have [simp]: "(\<Prod>k\<in>Basis. m k) = 0"
  3373     by (intro prod_zero) auto
  3374   have "emeasure lborel {x\<in>space lborel. x \<in> s m ` cbox a b} = 0"
  3375   proof (rule emeasure_eq_0_AE)
  3376     show "AE x in lborel. x \<notin> s m ` cbox a b"
  3377       using AE_lborel_inner_neq[OF \<open>k\<in>Basis\<close>]
  3378     proof eventually_elim
  3379       show "x \<bullet> k \<noteq> 0 \<Longrightarrow> x \<notin> s m ` cbox a b " for x
  3380         using k by (auto simp: s_def[abs_def] cbox_def)
  3381     qed
  3382   qed
  3383   then show ?thesis
  3384     by (simp add: measure_def)
  3385 qed
  3386 
  3387 lemma interval_image_affinity_interval:
  3388   "\<exists>u v. (\<lambda>x. m *\<^sub>R (x::'a::euclidean_space) + c) ` cbox a b = cbox u v"
  3389   unfolding image_affinity_cbox
  3390   by auto
  3391 
  3392 lemma content_image_affinity_cbox:
  3393   "content((\<lambda>x::'a::euclidean_space. m *\<^sub>R x + c) ` cbox a b) =
  3394     \<bar>m\<bar> ^ DIM('a) * content (cbox a b)" (is "?l = ?r")
  3395 proof (cases "cbox a b = {}")
  3396   case True then show ?thesis by simp
  3397 next
  3398   case False
  3399   show ?thesis
  3400   proof (cases "m \<ge> 0")
  3401     case True
  3402     with \<open>cbox a b \<noteq> {}\<close> have "cbox (m *\<^sub>R a + c) (m *\<^sub>R b + c) \<noteq> {}"
  3403       unfolding box_ne_empty
  3404       apply (intro ballI)
  3405       apply (erule_tac x=i in ballE)
  3406       apply (auto simp: inner_simps mult_left_mono)
  3407       done
  3408     moreover from True have *: "\<And>i. (m *\<^sub>R b + c) \<bullet> i - (m *\<^sub>R a + c) \<bullet> i = m *\<^sub>R (b-a) \<bullet> i"
  3409       by (simp add: inner_simps field_simps)
  3410     ultimately show ?thesis
  3411       by (simp add: image_affinity_cbox True content_cbox'
  3412         prod.distrib prod_constant inner_diff_left)
  3413   next
  3414     case False
  3415     with \<open>cbox a b \<noteq> {}\<close> have "cbox (m *\<^sub>R b + c) (m *\<^sub>R a + c) \<noteq> {}"
  3416       unfolding box_ne_empty
  3417       apply (intro ballI)
  3418       apply (erule_tac x=i in ballE)
  3419       apply (auto simp: inner_simps mult_left_mono)
  3420       done
  3421     moreover from False have *: "\<And>i. (m *\<^sub>R a + c) \<bullet> i - (m *\<^sub>R b + c) \<bullet> i = (-m) *\<^sub>R (b-a) \<bullet> i"
  3422       by (simp add: inner_simps field_simps)
  3423     ultimately show ?thesis using False
  3424       by (simp add: image_affinity_cbox content_cbox'
  3425         prod.distrib[symmetric] inner_diff_left flip: prod_constant)
  3426   qed
  3427 qed
  3428 
  3429 lemma has_integral_affinity:
  3430   fixes a :: "'a::euclidean_space"
  3431   assumes "(f has_integral i) (cbox a b)"
  3432       and "m \<noteq> 0"
  3433   shows "((\<lambda>x. f(m *\<^sub>R x + c)) has_integral ((1 / (\<bar>m\<bar> ^ DIM('a))) *\<^sub>R i)) ((\<lambda>x. (1 / m) *\<^sub>R x + -((1 / m) *\<^sub>R c)) ` cbox a b)"
  3434   apply (rule has_integral_twiddle)
  3435   using assms
  3436   apply (safe intro!: interval_image_affinity_interval content_image_affinity_cbox)
  3437   apply (rule zero_less_power)
  3438   unfolding scaleR_right_distrib
  3439   apply auto
  3440   done
  3441 
  3442 lemma integrable_affinity:
  3443   assumes "f integrable_on cbox a b"
  3444     and "m \<noteq> 0"
  3445   shows "(\<lambda>x. f(m *\<^sub>R x + c)) integrable_on ((\<lambda>x. (1 / m) *\<^sub>R x + -((1/m) *\<^sub>R c)) ` cbox a b)"
  3446   using assms
  3447   unfolding integrable_on_def
  3448   apply safe
  3449   apply (drule has_integral_affinity)
  3450   apply auto
  3451   done
  3452 
  3453 lemmas has_integral_affinity01 = has_integral_affinity [of _ _ 0 "1::real", simplified]
  3454 
  3455 lemma integrable_on_affinity:
  3456   assumes "m \<noteq> 0" "f integrable_on (cbox a b)"
  3457   shows   "(\<lambda>x. f (m *\<^sub>R x + c)) integrable_on ((\<lambda>x. (1 / m) *\<^sub>R x - ((1 / m) *\<^sub>R c)) ` cbox a b)"
  3458 proof -
  3459   from assms obtain I where "(f has_integral I) (cbox a b)"
  3460     by (auto simp: integrable_on_def)
  3461   from has_integral_affinity[OF this assms(1), of c] show ?thesis
  3462     by (auto simp: integrable_on_def)
  3463 qed
  3464 
  3465 lemma has_integral_cmul_iff:
  3466   assumes "c \<noteq> 0"
  3467   shows   "((\<lambda>x. c *\<^sub>R f x) has_integral (c *\<^sub>R I)) A \<longleftrightarrow> (f has_integral I) A"
  3468   using assms has_integral_cmul[of f I A c]
  3469         has_integral_cmul[of "\<lambda>x. c *\<^sub>R f x" "c *\<^sub>R I" A "inverse c"] by (auto simp: field_simps)
  3470 
  3471 lemma has_integral_affinity':
  3472   fixes a :: "'a::euclidean_space"
  3473   assumes "(f has_integral i) (cbox a b)" and "m > 0"
  3474   shows "((\<lambda>x. f(m *\<^sub>R x + c)) has_integral (i /\<^sub>R m ^ DIM('a)))
  3475            (cbox ((a - c) /\<^sub>R m) ((b - c) /\<^sub>R m))"
  3476 proof (cases "cbox a b = {}")
  3477   case True
  3478   hence "(cbox ((a - c) /\<^sub>R m) ((b - c) /\<^sub>R m)) = {}"
  3479     using \<open>m > 0\<close> unfolding box_eq_empty by (auto simp: algebra_simps)
  3480   with True and assms show ?thesis by simp
  3481 next
  3482   case False
  3483   have "((\<lambda>x. f (m *\<^sub>R x + c)) has_integral (1 / \<bar>m\<bar> ^ DIM('a)) *\<^sub>R i)
  3484           ((\<lambda>x. (1 / m) *\<^sub>R x + - ((1 / m) *\<^sub>R c)) ` cbox a b)"
  3485     using assms by (intro has_integral_affinity) auto
  3486   also have "((\<lambda>x. (1 / m) *\<^sub>R x + - ((1 / m) *\<^sub>R c)) ` cbox a b) =
  3487                ((\<lambda>x.  - ((1 / m) *\<^sub>R c) + x) ` (\<lambda>x. (1 / m) *\<^sub>R x) ` cbox a b)"
  3488     by (simp add: image_image algebra_simps)
  3489   also have "(\<lambda>x. (1 / m) *\<^sub>R x) ` cbox a b = cbox ((1 / m) *\<^sub>R a) ((1 / m) *\<^sub>R b)" using \<open>m > 0\<close> False
  3490     by (subst image_smult_cbox) simp_all
  3491   also have "(\<lambda>x. - ((1 / m) *\<^sub>R c) + x) ` \<dots> = cbox ((a - c) /\<^sub>R m) ((b - c) /\<^sub>R m)"
  3492     by (subst cbox_translation [symmetric]) (simp add: field_simps vector_add_divide_simps)
  3493   finally show ?thesis using \<open>m > 0\<close> by (simp add: field_simps)
  3494 qed
  3495 
  3496 lemma has_integral_affinity_iff:
  3497   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: real_normed_vector"
  3498   assumes "m > 0"
  3499   shows   "((\<lambda>x. f (m *\<^sub>R x + c)) has_integral (I /\<^sub>R m ^ DIM('a)))
  3500                (cbox ((a - c) /\<^sub>R m) ((b - c) /\<^sub>R m)) \<longleftrightarrow>
  3501            (f has_integral I) (cbox a b)" (is "?lhs = ?rhs")
  3502 proof
  3503   assume ?lhs
  3504   from has_integral_affinity'[OF this, of "1 / m" "-c /\<^sub>R m"] and \<open>m > 0\<close>
  3505     show ?rhs by (simp add: field_simps vector_add_divide_simps)
  3506 next
  3507   assume ?rhs
  3508   from has_integral_affinity'[OF this, of m c] and \<open>m > 0\<close>
  3509   show ?lhs by simp
  3510 qed
  3511 
  3512 
  3513 subsection \<open>Special case of stretching coordinate axes separately\<close>
  3514 
  3515 lemma has_integral_stretch:
  3516   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  3517   assumes "(f has_integral i) (cbox a b)"
  3518     and "\<forall>k\<in>Basis. m k \<noteq> 0"
  3519   shows "((\<lambda>x. f (\<Sum>k\<in>Basis. (m k * (x\<bullet>k))*\<^sub>R k)) has_integral
  3520          ((1/ \<bar>prod m Basis\<bar>) *\<^sub>R i)) ((\<lambda>x. (\<Sum>k\<in>Basis. (1 / m k * (x\<bullet>k))*\<^sub>R k)) ` cbox a b)"
  3521 apply (rule has_integral_twiddle[where f=f])
  3522 unfolding zero_less_abs_iff content_image_stretch_interval
  3523 unfolding image_stretch_interval empty_as_interval euclidean_eq_iff[where 'a='a]
  3524 using assms
  3525 by auto
  3526 
  3527 
  3528 lemma integrable_stretch:
  3529   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  3530   assumes "f integrable_on cbox a b"
  3531     and "\<forall>k\<in>Basis. m k \<noteq> 0"
  3532   shows "(\<lambda>x::'a. f (\<Sum>k\<in>Basis. (m k * (x\<bullet>k))*\<^sub>R k)) integrable_on
  3533     ((\<lambda>x. \<Sum>k\<in>Basis. (1 / m k * (x\<bullet>k))*\<^sub>R k) ` cbox a b)"
  3534   using assms unfolding integrable_on_def
  3535   by (force dest: has_integral_stretch)
  3536 
  3537 lemma vec_lambda_eq_sum:
  3538   shows "(\<chi> k. f k (x $ k)) = (\<Sum>k\<in>Basis. (f (axis_index k) (x \<bullet> k)) *\<^sub>R k)"
  3539     apply (simp add: Basis_vec_def cart_eq_inner_axis UNION_singleton_eq_range sum.reindex axis_eq_axis inj_on_def)
  3540     apply (simp add: vec_eq_iff axis_def if_distrib cong: if_cong)
  3541     done
  3542 
  3543 lemma has_integral_stretch_cart:
  3544   fixes m :: "'n::finite \<Rightarrow> real"
  3545   assumes f: "(f has_integral i) (cbox a b)" and m: "\<And>k. m k \<noteq> 0"
  3546   shows "((\<lambda>x. f(\<chi> k. m k * x$k)) has_integral i /\<^sub>R \<bar>prod m UNIV\<bar>)
  3547             ((\<lambda>x. \<chi> k. x$k / m k) ` (cbox a b))"
  3548 proof -
  3549   have *: "\<forall>k:: real^'n \<in> Basis. m (axis_index k) \<noteq> 0"
  3550     using axis_index by (simp add: m)
  3551   have eqp: "(\<Prod>k:: real^'n \<in> Basis. m (axis_index k)) = prod m UNIV"
  3552     by (simp add: Basis_vec_def UNION_singleton_eq_range prod.reindex axis_eq_axis inj_on_def)
  3553   show ?thesis
  3554     using has_integral_stretch [OF f *] vec_lambda_eq_sum [where f="\<lambda>i x. m i * x"] vec_lambda_eq_sum [where f="\<lambda>i x. x / m i"]
  3555     by (simp add: field_simps eqp)
  3556 qed
  3557 
  3558 lemma image_stretch_interval_cart:
  3559   fixes m :: "'n::finite \<Rightarrow> real"
  3560   shows "(\<lambda>x. \<chi> k. m k * x$k) ` cbox a b =
  3561             (if cbox a b = {} then {}
  3562             else cbox (\<chi> k. min (m k * a$k) (m k * b$k)) (\<chi> k. max (m k * a$k) (m k * b$k)))"
  3563 proof -
  3564   have *: "(\<Sum>k\<in>Basis. min (m (axis_index k) * (a \<bullet> k)) (m (axis_index k) * (b \<bullet> k)) *\<^sub>R k)
  3565            = (\<chi> k. min (m k * a $ k) (m k * b $ k))"
  3566           "(\<Sum>k\<in>Basis. max (m (axis_index k) * (a \<bullet> k)) (m (axis_index k) * (b \<bullet> k)) *\<^sub>R k)
  3567            = (\<chi> k. max (m k * a $ k) (m k * b $ k))"
  3568     apply (simp_all add: Basis_vec_def cart_eq_inner_axis UNION_singleton_eq_range sum.reindex axis_eq_axis inj_on_def)
  3569     apply (simp_all add: vec_eq_iff axis_def if_distrib cong: if_cong)
  3570     done
  3571   show ?thesis
  3572     by (simp add: vec_lambda_eq_sum [where f="\<lambda>i x. m i * x"] image_stretch_interval eq_cbox *)
  3573 qed
  3574 
  3575 
  3576 subsection \<open>even more special cases\<close>
  3577 
  3578 lemma uminus_interval_vector[simp]:
  3579   fixes a b :: "'a::euclidean_space"
  3580   shows "uminus ` cbox a b = cbox (-b) (-a)"
  3581   apply safe
  3582    apply (simp add: mem_box(2))
  3583   apply (rule_tac x="-x" in image_eqI)
  3584    apply (auto simp add: mem_box)
  3585   done
  3586 
  3587 lemma has_integral_reflect_lemma[intro]:
  3588   assumes "(f has_integral i) (cbox a b)"
  3589   shows "((\<lambda>x. f(-x)) has_integral i) (cbox (-b) (-a))"
  3590   using has_integral_affinity[OF assms, of "-1" 0]
  3591   by auto
  3592 
  3593 lemma has_integral_reflect_lemma_real[intro]:
  3594   assumes "(f has_integral i) {a..b::real}"
  3595   shows "((\<lambda>x. f(-x)) has_integral i) {-b .. -a}"
  3596   using assms
  3597   unfolding box_real[symmetric]
  3598   by (rule has_integral_reflect_lemma)
  3599 
  3600 lemma has_integral_reflect[simp]:
  3601   "((\<lambda>x. f (-x)) has_integral i) (cbox (-b) (-a)) \<longleftrightarrow> (f has_integral i) (cbox a b)"
  3602   by (auto dest: has_integral_reflect_lemma)
  3603 
  3604 lemma integrable_reflect[simp]: "(\<lambda>x. f(-x)) integrable_on cbox (-b) (-a) \<longleftrightarrow> f integrable_on cbox a b"
  3605   unfolding integrable_on_def by auto
  3606 
  3607 lemma integrable_reflect_real[simp]: "(\<lambda>x. f(-x)) integrable_on {-b .. -a} \<longleftrightarrow> f integrable_on {a..b::real}"
  3608   unfolding box_real[symmetric]
  3609   by (rule integrable_reflect)
  3610 
  3611 lemma integral_reflect[simp]: "integral (cbox (-b) (-a)) (\<lambda>x. f (-x)) = integral (cbox a b) f"
  3612   unfolding integral_def by auto
  3613 
  3614 lemma integral_reflect_real[simp]: "integral {-b .. -a} (\<lambda>x. f (-x)) = integral {a..b::real} f"
  3615   unfolding box_real[symmetric]
  3616   by (rule integral_reflect)
  3617 
  3618 
  3619 subsection \<open>Stronger form of FCT; quite a tedious proof\<close>
  3620 
  3621 lemma split_minus[simp]: "(\<lambda>(x, k). f x k) x - (\<lambda>(x, k). g x k) x = (\<lambda>(x, k). f x k - g x k) x"
  3622   by (simp add: split_def)
  3623 
  3624 theorem fundamental_theorem_of_calculus_interior:
  3625   fixes f :: "real \<Rightarrow> 'a::real_normed_vector"
  3626   assumes "a \<le> b"
  3627     and contf: "continuous_on {a..b} f"
  3628     and derf: "\<And>x. x \<in> {a <..< b} \<Longrightarrow> (f has_vector_derivative f' x) (at x)"
  3629   shows "(f' has_integral (f b - f a)) {a..b}"
  3630 proof (cases "a = b")
  3631   case True
  3632   then have *: "cbox a b = {b}" "f b - f a = 0"
  3633     by (auto simp add:  order_antisym)
  3634   with True show ?thesis by auto
  3635 next
  3636   case False
  3637   with \<open>a \<le> b\<close> have ab: "a < b" by arith
  3638   show ?thesis
  3639     unfolding has_integral_factor_content_real
  3640   proof (intro allI impI)
  3641     fix e :: real
  3642     assume e: "e > 0"
  3643     then have eba8: "(e * (b-a)) / 8 > 0"
  3644       using ab by (auto simp add: field_simps)
  3645     note derf_exp = derf[unfolded has_vector_derivative_def has_derivative_at_alt]
  3646     have bounded: "\<And>x. x \<in> {a<..<b} \<Longrightarrow> bounded_linear (\<lambda>u. u *\<^sub>R f' x)"
  3647       using derf_exp by simp
  3648     have "\<forall>x \<in> box a b. \<exists>d>0. \<forall>y. norm (y-x) < d \<longrightarrow> norm (f y - f x - (y-x) *\<^sub>R f' x) \<le> e/2 * norm (y-x)"
  3649       (is "\<forall>x \<in> box a b. ?Q x")
  3650     proof
  3651       fix x assume x: "x \<in> box a b"
  3652       show "?Q x"
  3653         apply (rule allE [where x="e/2", OF derf_exp [THEN conjunct2, of x]])
  3654         using x e by auto
  3655     qed
  3656     from this [unfolded bgauge_existence_lemma]
  3657     obtain d where d: "\<And>x. 0 < d x"
  3658       "\<And>x y. \<lbrakk>x \<in> box a b; norm (y-x) < d x\<rbrakk>
  3659                \<Longrightarrow> norm (f y - f x - (y-x) *\<^sub>R f' x) \<le> e/2 * norm (y-x)"
  3660       by metis
  3661     have "bounded (f ` cbox a b)"
  3662       using compact_cbox assms by (auto simp: compact_imp_bounded compact_continuous_image)
  3663     then obtain B 
  3664       where "0 < B" and B: "\<And>x. x \<in> f ` cbox a b \<Longrightarrow> norm x \<le> B"
  3665       unfolding bounded_pos by metis
  3666     obtain da where "0 < da"
  3667       and da: "\<And>c. \<lbrakk>a \<le> c; {a..c} \<subseteq> {a..b}; {a..c} \<subseteq> ball a da\<rbrakk>
  3668                           \<Longrightarrow> norm (content {a..c} *\<^sub>R f' a - (f c - f a)) \<le> (e * (b-a)) / 4"
  3669     proof -
  3670       have "continuous (at a within {a..b}) f"
  3671         using contf continuous_on_eq_continuous_within by force
  3672       with eba8 obtain k where "0 < k"
  3673         and k: "\<And>x. \<lbrakk>x \<in> {a..b}; 0 < norm (x-a); norm (x-a) < k\<rbrakk> \<Longrightarrow> norm (f x - f a) < e * (b-a) / 8"
  3674         unfolding continuous_within Lim_within dist_norm by metis
  3675       obtain l where l: "0 < l" "norm (l *\<^sub>R f' a) \<le> e * (b-a) / 8" 
  3676       proof (cases "f' a = 0")
  3677         case True with ab e that show ?thesis by auto
  3678       next
  3679         case False
  3680         then show ?thesis
  3681           apply (rule_tac l="(e * (b-a)) / 8 / norm (f' a)" in that)
  3682           using ab e apply (auto simp add: field_simps)
  3683           done
  3684       qed
  3685       have "norm (content {a..c} *\<^sub>R f' a - (f c - f a)) \<le> e * (b-a) / 4"
  3686         if "a \<le> c" "{a..c} \<subseteq> {a..b}" and bmin: "{a..c} \<subseteq> ball a (min k l)" for c
  3687       proof -
  3688         have minkl: "\<bar>a - x\<bar> < min k l" if "x \<in> {a..c}" for x
  3689           using bmin dist_real_def that by auto
  3690         then have lel: "\<bar>c - a\<bar> \<le> \<bar>l\<bar>"
  3691           using that by force
  3692         have "norm ((c - a) *\<^sub>R f' a - (f c - f a)) \<le> norm ((c - a) *\<^sub>R f' a) + norm (f c - f a)"
  3693           by (rule norm_triangle_ineq4)
  3694         also have "\<dots> \<le> e * (b-a) / 8 + e * (b-a) / 8"
  3695         proof (rule add_mono)
  3696           have "norm ((c - a) *\<^sub>R f' a) \<le> norm (l *\<^sub>R f' a)"
  3697             by (auto intro: mult_right_mono [OF lel])
  3698           also have "... \<le> e * (b-a) / 8"
  3699             by (rule l)
  3700           finally show "norm ((c - a) *\<^sub>R f' a) \<le> e * (b-a) / 8" .
  3701         next
  3702           have "norm (f c - f a) < e * (b-a) / 8"
  3703           proof (cases "a = c")
  3704             case True then show ?thesis
  3705               using eba8 by auto
  3706           next
  3707             case False show ?thesis
  3708               by (rule k) (use minkl \<open>a \<le> c\<close> that False in auto)
  3709           qed
  3710           then show "norm (f c - f a) \<le> e * (b-a) / 8" by simp
  3711         qed
  3712         finally show "norm (content {a..c} *\<^sub>R f' a - (f c - f a)) \<le> e * (b-a) / 4"
  3713           unfolding content_real[OF \<open>a \<le> c\<close>] by auto
  3714       qed
  3715       then show ?thesis
  3716         by (rule_tac da="min k l" in that) (auto simp: l \<open>0 < k\<close>)
  3717     qed
  3718     obtain db where "0 < db"
  3719       and db: "\<And>c. \<lbrakk>c \<le> b; {c..b} \<subseteq> {a..b}; {c..b} \<subseteq> ball b db\<rbrakk>
  3720                           \<Longrightarrow> norm (content {c..b} *\<^sub>R f' b - (f b - f c)) \<le> (e * (b-a)) / 4"
  3721     proof -
  3722       have "continuous (at b within {a..b}) f"
  3723         using contf continuous_on_eq_continuous_within by force
  3724       with eba8 obtain k
  3725         where "0 < k"
  3726           and k: "\<And>x. \<lbrakk>x \<in> {a..b}; 0 < norm(x-b); norm(x-b) < k\<rbrakk>
  3727                      \<Longrightarrow> norm (f b - f x) < e * (b-a) / 8"
  3728         unfolding continuous_within Lim_within dist_norm norm_minus_commute by metis
  3729       obtain l where l: "0 < l" "norm (l *\<^sub>R f' b) \<le> (e * (b-a)) / 8"
  3730       proof (cases "f' b = 0")
  3731         case True thus ?thesis 
  3732           using ab e that by auto
  3733       next
  3734         case False then show ?thesis
  3735           apply (rule_tac l="(e * (b-a)) / 8 / norm (f' b)" in that)
  3736           using ab e by (auto simp add: field_simps)
  3737       qed
  3738       have "norm (content {c..b} *\<^sub>R f' b - (f b - f c)) \<le> e * (b-a) / 4" 
  3739         if "c \<le> b" "{c..b} \<subseteq> {a..b}" and bmin: "{c..b} \<subseteq> ball b (min k l)" for c
  3740       proof -
  3741         have minkl: "\<bar>b - x\<bar> < min k l" if "x \<in> {c..b}" for x
  3742           using bmin dist_real_def that by auto
  3743         then have lel: "\<bar>b - c\<bar> \<le> \<bar>l\<bar>"
  3744           using that by force
  3745         have "norm ((b - c) *\<^sub>R f' b - (f b - f c)) \<le> norm ((b - c) *\<^sub>R f' b) + norm (f b - f c)"
  3746           by (rule norm_triangle_ineq4)
  3747         also have "\<dots> \<le> e * (b-a) / 8 + e * (b-a) / 8"
  3748         proof (rule add_mono)
  3749           have "norm ((b - c) *\<^sub>R f' b) \<le> norm (l *\<^sub>R f' b)"
  3750             by (auto intro: mult_right_mono [OF lel])
  3751           also have "... \<le> e * (b-a) / 8"
  3752             by (rule l)
  3753           finally show "norm ((b - c) *\<^sub>R f' b) \<le> e * (b-a) / 8" .
  3754         next
  3755           have "norm (f b - f c) < e * (b-a) / 8"
  3756           proof (cases "b = c")
  3757             case True with eba8 show ?thesis
  3758               by auto
  3759           next
  3760             case False show ?thesis
  3761               by (rule k) (use minkl \<open>c \<le> b\<close> that False in auto)
  3762           qed
  3763           then show "norm (f b - f c) \<le> e * (b-a) / 8" by simp
  3764         qed
  3765         finally show "norm (content {c..b} *\<^sub>R f' b - (f b - f c)) \<le> e * (b-a) / 4"
  3766           unfolding content_real[OF \<open>c \<le> b\<close>] by auto
  3767       qed
  3768       then show ?thesis
  3769         by (rule_tac db="min k l" in that) (auto simp: l \<open>0 < k\<close>)
  3770     qed
  3771     let ?d = "(\<lambda>x. ball x (if x=a then da else if x=b then db else d x))"
  3772     show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow>
  3773               norm ((\<Sum>(x,K)\<in>p. content K *\<^sub>R f' x) - (f b - f a)) \<le> e * content {a..b})"
  3774     proof (rule exI, safe)
  3775       show "gauge ?d"
  3776         using ab \<open>db > 0\<close> \<open>da > 0\<close> d(1) by (auto intro: gauge_ball_dependent)
  3777     next
  3778       fix p
  3779       assume ptag: "p tagged_division_of {a..b}" and fine: "?d fine p"
  3780       let ?A = "{t. fst t \<in> {a, b}}"
  3781       note p = tagged_division_ofD[OF ptag]
  3782       have pA: "p = (p \<inter> ?A) \<union> (p - ?A)" "finite (p \<inter> ?A)" "finite (p - ?A)" "(p \<inter> ?A) \<inter> (p - ?A) = {}"
  3783         using ptag fine by auto
  3784       have le_xz: "\<And>w x y z::real. y \<le> z/2 \<Longrightarrow> w - x \<le> z/2 \<Longrightarrow> w + y \<le> x + z"
  3785         by arith
  3786       have non: False if xk: "(x,K) \<in> p" and "x \<noteq> a" "x \<noteq> b"
  3787         and less: "e * (Sup K - Inf K)/2 < norm (content K *\<^sub>R f' x - (f (Sup K) - f (Inf K)))"
  3788       for x K
  3789       proof -
  3790         obtain u v where k: "K = cbox u v"
  3791           using p(4) xk by blast
  3792         then have "u \<le> v" and uv: "{u, v} \<subseteq> cbox u v"
  3793           using p(2)[OF xk] by auto
  3794         then have result: "e * (v - u)/2 < norm ((v - u) *\<^sub>R f' x - (f v - f u))"
  3795           using less[unfolded k box_real interval_bounds_real content_real] by auto
  3796         then have "x \<in> box a b"
  3797           using p(2) p(3) \<open>x \<noteq> a\<close> \<open>x \<noteq> b\<close> xk by fastforce
  3798         with d have *: "\<And>y. norm (y-x) < d x 
  3799                 \<Longrightarrow> norm (f y - f x - (y-x) *\<^sub>R f' x) \<le> e/2 * norm (y-x)"
  3800           by metis
  3801         have xd: "norm (u - x) < d x" "norm (v - x) < d x"
  3802           using fineD[OF fine xk] \<open>x \<noteq> a\<close> \<open>x \<noteq> b\<close> uv 
  3803           by (auto simp add: k subset_eq dist_commute dist_real_def)
  3804         have "norm ((v - u) *\<^sub>R f' x - (f v - f u)) =
  3805               norm ((f u - f x - (u - x) *\<^sub>R f' x) - (f v - f x - (v - x) *\<^sub>R f' x))"
  3806           by (rule arg_cong[where f=norm]) (auto simp: scaleR_left.diff)
  3807         also have "\<dots> \<le> e/2 * norm (u - x) + e/2 * norm (v - x)"
  3808           by (metis norm_triangle_le_diff add_mono * xd)
  3809         also have "\<dots> \<le> e/2 * norm (v - u)"
  3810           using p(2)[OF xk] by (auto simp add: field_simps k)
  3811         also have "\<dots> < norm ((v - u) *\<^sub>R f' x - (f v - f u))"
  3812           using result by (simp add: \<open>u \<le> v\<close>)
  3813         finally have "e * (v - u)/2 < e * (v - u)/2"
  3814           using uv by auto
  3815         then show False by auto
  3816       qed
  3817       have "norm (\<Sum>(x, K)\<in>p - ?A. content K *\<^sub>R f' x - (f (Sup K) - f (Inf K)))
  3818           \<le> (\<Sum>(x, K)\<in>p - ?A. norm (content K *\<^sub>R f' x - (f (Sup K) - f (Inf K))))"
  3819         by (auto intro: sum_norm_le)
  3820       also have "... \<le> (\<Sum>n\<in>p - ?A. e * (case n of (x, k) \<Rightarrow> Sup k - Inf k)/2)"
  3821         using non by (fastforce intro: sum_mono)
  3822       finally have I: "norm (\<Sum>(x, k)\<in>p - ?A.
  3823                   content k *\<^sub>R f' x - (f (Sup k) - f (Inf k)))
  3824              \<le> (\<Sum>n\<in>p - ?A. e * (case n of (x, k) \<Rightarrow> Sup k - Inf k))/2"
  3825         by (simp add: sum_divide_distrib)
  3826       have II: "norm (\<Sum>(x, k)\<in>p \<inter> ?A. content k *\<^sub>R f' x - (f (Sup k) - f (Inf k))) -
  3827              (\<Sum>n\<in>p \<inter> ?A. e * (case n of (x, k) \<Rightarrow> Sup k - Inf k))
  3828              \<le> (\<Sum>n\<in>p - ?A. e * (case n of (x, k) \<Rightarrow> Sup k - Inf k))/2"
  3829       proof -
  3830         have ge0: "0 \<le> e * (Sup k - Inf k)" if xkp: "(x, k) \<in> p \<inter> ?A" for x k
  3831         proof -
  3832           obtain u v where uv: "k = cbox u v"
  3833             by (meson Int_iff xkp p(4))
  3834           with p(2) that uv have "cbox u v \<noteq> {}"
  3835             by blast
  3836           then show "0 \<le> e * ((Sup k) - (Inf k))"
  3837             unfolding uv using e by (auto simp add: field_simps)
  3838         qed
  3839         let ?B = "\<lambda>x. {t \<in> p. fst t = x \<and> content (snd t) \<noteq> 0}"
  3840         let ?C = "{t \<in> p. fst t \<in> {a, b} \<and> content (snd t) \<noteq> 0}"
  3841         have "norm (\<Sum>(x, k)\<in>p \<inter> {t. fst t \<in> {a, b}}. content k *\<^sub>R f' x - (f (Sup k) - f (Inf k))) \<le> e * (b-a)/2"
  3842         proof -
  3843           have *: "\<And>S f e. sum f S = sum f (p \<inter> ?C) \<Longrightarrow> norm (sum f (p \<inter> ?C)) \<le> e \<Longrightarrow> norm (sum f S) \<le> e"
  3844             by auto
  3845           have 1: "content K *\<^sub>R (f' x) - (f ((Sup K)) - f ((Inf K))) = 0"
  3846             if "(x,K) \<in> p \<inter> {t. fst t \<in> {a, b}} - p \<inter> ?C" for x K
  3847           proof -
  3848             have xk: "(x,K) \<in> p" and k0: "content K = 0"
  3849               using that by auto
  3850             then obtain u v where uv: "K = cbox u v"
  3851               using p(4) by blast
  3852             then have "u = v"
  3853               using xk k0 p(2) by force
  3854             then show "content K *\<^sub>R (f' x) - (f ((Sup K)) - f ((Inf K))) = 0"
  3855               using xk unfolding uv by auto
  3856           qed
  3857           have 2: "norm(\<Sum>(x,K)\<in>p \<inter> ?C. content K *\<^sub>R f' x - (f (Sup K) - f (Inf K)))  \<le> e * (b-a)/2"
  3858           proof -
  3859             have norm_le: "norm (sum f S) \<le> e"
  3860               if "\<And>x y. \<lbrakk>x \<in> S; y \<in> S\<rbrakk> \<Longrightarrow> x = y" "\<And>x. x \<in> S \<Longrightarrow> norm (f x) \<le> e" "e > 0"
  3861               for S f and e :: real
  3862             proof (cases "S = {}")
  3863               case True
  3864               with that show ?thesis by auto
  3865             next
  3866               case False then obtain x where "x \<in> S"
  3867                 by auto
  3868               then have "S = {x}"
  3869                 using that(1) by auto
  3870               then show ?thesis
  3871                 using \<open>x \<in> S\<close> that(2) by auto
  3872             qed
  3873             have *: "p \<inter> ?C = ?B a \<union> ?B b"
  3874               by blast
  3875             then have "norm (\<Sum>(x,K)\<in>p \<inter> ?C. content K *\<^sub>R f' x - (f (Sup K) - f (Inf K))) =
  3876                        norm (\<Sum>(x,K) \<in> ?B a \<union> ?B b. content K *\<^sub>R f' x - (f (Sup K) - f (Inf K)))"
  3877               by simp
  3878             also have "... = norm ((\<Sum>(x,K) \<in> ?B a. content K *\<^sub>R f' x - (f (Sup K) - f (Inf K))) + 
  3879                                    (\<Sum>(x,K) \<in> ?B b. content K *\<^sub>R f' x - (f (Sup K) - f (Inf K))))"
  3880               apply (subst sum.union_disjoint)
  3881               using p(1) ab e by auto
  3882             also have "... \<le> e * (b - a) / 4 + e * (b - a) / 4"
  3883             proof (rule norm_triangle_le [OF add_mono])
  3884               have pa: "\<exists>v. k = cbox a v \<and> a \<le> v" if "(a, k) \<in> p" for k
  3885                 using p(2) p(3) p(4) that by fastforce
  3886               show "norm (\<Sum>(x,K) \<in> ?B a. content K *\<^sub>R f' x - (f (Sup K) - f (Inf K))) \<le> e * (b - a) / 4"
  3887               proof (intro norm_le; clarsimp)
  3888                 fix K K'
  3889                 assume K: "(a, K) \<in> p" "(a, K') \<in> p" and ne0: "content K \<noteq> 0" "content K' \<noteq> 0"
  3890                 with pa obtain v v' where v: "K = cbox a v" "a \<le> v" and v': "K' = cbox a v'" "a \<le> v'"
  3891                   by blast
  3892                 let ?v = "min v v'"
  3893                 have "box a ?v \<subseteq> K \<inter> K'"
  3894                   unfolding v v' by (auto simp add: mem_box)
  3895                 then have "interior (box a (min v v')) \<subseteq> interior K \<inter> interior K'"
  3896                   using interior_Int interior_mono by blast
  3897                 moreover have "(a + ?v)/2 \<in> box a ?v"
  3898                   using ne0  unfolding v v' content_eq_0 not_le
  3899                   by (auto simp add: mem_box)
  3900                 ultimately have "(a + ?v)/2 \<in> interior K \<inter> interior K'"
  3901                   unfolding interior_open[OF open_box] by auto
  3902                 then show "K = K'"
  3903                   using p(5)[OF K] by auto
  3904               next
  3905                 fix K 
  3906                 assume K: "(a, K) \<in> p" and ne0: "content K \<noteq> 0"
  3907                 show "norm (content c *\<^sub>R f' a - (f (Sup c) - f (Inf c))) * 4 \<le> e * (b-a)"
  3908                   if "(a, c) \<in> p" and ne0: "content c \<noteq> 0" for c
  3909                 proof -
  3910                   obtain v where v: "c = cbox a v" and "a \<le> v"
  3911                     using pa[OF \<open>(a, c) \<in> p\<close>] by metis 
  3912                   then have "a \<in> {a..v}" "v \<le> b"
  3913                     using p(3)[OF \<open>(a, c) \<in> p\<close>] by auto
  3914                   moreover have "{a..v} \<subseteq> ball a da"
  3915                     using fineD[OF \<open>?d fine p\<close> \<open>(a, c) \<in> p\<close>] by (simp add: v split: if_split_asm)
  3916                   ultimately show ?thesis
  3917                     unfolding v interval_bounds_real[OF \<open>a \<le> v\<close>] box_real
  3918                     using da \<open>a \<le> v\<close> by auto
  3919                 qed
  3920               qed (use ab e in auto)
  3921             next
  3922               have pb: "\<exists>v. k = cbox v b \<and> b \<ge> v" if "(b, k) \<in> p" for k
  3923                 using p(2) p(3) p(4) that by fastforce
  3924               show "norm (\<Sum>(x,K) \<in> ?B b. content K *\<^sub>R f' x - (f (Sup K) - f (Inf K))) \<le> e * (b - a) / 4"
  3925               proof (intro norm_le; clarsimp)
  3926                 fix K K'
  3927                 assume K: "(b, K) \<in> p" "(b, K') \<in> p" and ne0: "content K \<noteq> 0" "content K' \<noteq> 0"
  3928                 with pb obtain v v' where v: "K = cbox v b" "v \<le> b" and v': "K' = cbox v' b" "v' \<le> b"
  3929                   by blast
  3930                 let ?v = "max v v'"
  3931                 have "box ?v b \<subseteq> K \<inter> K'"
  3932                   unfolding v v' by (auto simp: mem_box)
  3933                 then have "interior (box (max v v') b) \<subseteq> interior K \<inter> interior K'"
  3934                   using interior_Int interior_mono by blast
  3935                 moreover have " ((b + ?v)/2) \<in> box ?v b"
  3936                   using ne0 unfolding v v' content_eq_0 not_le by (auto simp: mem_box)
  3937                 ultimately have "((b + ?v)/2) \<in> interior K \<inter> interior K'"
  3938                   unfolding interior_open[OF open_box] by auto
  3939                 then show "K = K'"
  3940                   using p(5)[OF K] by auto
  3941               next
  3942                 fix K 
  3943                 assume K: "(b, K) \<in> p" and ne0: "content K \<noteq> 0"
  3944                 show "norm (content c *\<^sub>R f' b - (f (Sup c) - f (Inf c))) * 4 \<le> e * (b-a)"
  3945                   if "(b, c) \<in> p" and ne0: "content c \<noteq> 0" for c
  3946                 proof -
  3947                 obtain v where v: "c = cbox v b" and "v \<le> b"
  3948                   using \<open>(b, c) \<in> p\<close> pb by blast
  3949                 then have "v \<ge> a""b \<in> {v.. b}"  
  3950                   using p(3)[OF \<open>(b, c) \<in> p\<close>] by auto
  3951                 moreover have "{v..b} \<subseteq> ball b db"
  3952                   using fineD[OF \<open>?d fine p\<close> \<open>(b, c) \<in> p\<close>] box_real(2) v False by force
  3953                 ultimately show ?thesis
  3954                   using db v by auto
  3955                 qed
  3956               qed (use ab e in auto)
  3957             qed
  3958             also have "... = e * (b-a)/2"
  3959               by simp
  3960             finally show "norm (\<Sum>(x,k)\<in>p \<inter> ?C.
  3961                         content k *\<^sub>R f' x - (f (Sup k) - f (Inf k))) \<le> e * (b-a)/2" .
  3962           qed
  3963           show "norm (\<Sum>(x, k)\<in>p \<inter> ?A. content k *\<^sub>R f' x - (f ((Sup k)) - f ((Inf k)))) \<le> e * (b-a)/2"
  3964             apply (rule * [OF sum.mono_neutral_right[OF pA(2)]])
  3965             using 1 2 by (auto simp: split_paired_all)
  3966         qed
  3967         also have "... = (\<Sum>n\<in>p. e * (case n of (x, k) \<Rightarrow> Sup k - Inf k))/2"
  3968           unfolding sum_distrib_left[symmetric]
  3969           apply (subst additive_tagged_division_1[OF \<open>a \<le> b\<close> ptag])
  3970           by auto
  3971         finally have norm_le: "norm (\<Sum>(x,K)\<in>p \<inter> {t. fst t \<in> {a, b}}. content K *\<^sub>R f' x - (f (Sup K) - f (Inf K)))
  3972                 \<le> (\<Sum>n\<in>p. e * (case n of (x, K) \<Rightarrow> Sup K - Inf K))/2" .
  3973         have le2: "\<And>x s1 s2::real. 0 \<le> s1 \<Longrightarrow> x \<le> (s1 + s2)/2 \<Longrightarrow> x - s1 \<le> s2/2"
  3974           by auto
  3975         show ?thesis
  3976           apply (rule le2 [OF sum_nonneg])
  3977           using ge0 apply force
  3978           unfolding sum.union_disjoint[OF pA(2-), symmetric] pA(1)[symmetric]
  3979           by (metis norm_le)
  3980       qed
  3981       note * = additive_tagged_division_1[OF assms(1) ptag, symmetric]
  3982       have "norm (\<Sum>(x,K)\<in>p \<inter> ?A \<union> (p - ?A). content K *\<^sub>R f' x - (f (Sup K) - f (Inf K)))
  3983                \<le> e * (\<Sum>(x,K)\<in>p \<inter> ?A \<union> (p - ?A). Sup K - Inf K)"
  3984         unfolding sum_distrib_left
  3985         unfolding sum.union_disjoint[OF pA(2-)]
  3986         using le_xz norm_triangle_le I II by blast
  3987       then
  3988       show "norm ((\<Sum>(x,K)\<in>p. content K *\<^sub>R f' x) - (f b - f a)) \<le> e * content {a..b}"
  3989         by (simp only: content_real[OF \<open>a \<le> b\<close>] *[of "\<lambda>x. x"] *[of f] sum_subtractf[symmetric] split_minus pA(1) [symmetric])
  3990     qed
  3991   qed
  3992 qed
  3993 
  3994 
  3995 subsection \<open>Stronger form with finite number of exceptional points\<close>
  3996 
  3997 lemma fundamental_theorem_of_calculus_interior_strong:
  3998  fixes f :: "real \<Rightarrow> 'a::banach"
  3999  assumes "finite S"
  4000    and "a \<le> b" "\<And>x. x \<in> {a <..< b} - S \<Longrightarrow> (f has_vector_derivative f'(x)) (at x)"
  4001    and "continuous_on {a .. b} f"
  4002  shows "(f' has_integral (f b - f a)) {a .. b}"
  4003   using assms
  4004 proof (induction arbitrary: a b)
  4005 case empty
  4006   then show ?case
  4007     using fundamental_theorem_of_calculus_interior by force
  4008 next
  4009 case (insert x S)
  4010   show ?case
  4011   proof (cases "x \<in> {a<..<b}")
  4012     case False then show ?thesis
  4013       using insert by blast
  4014   next
  4015     case True then have "a < x" "x < b"
  4016       by auto
  4017     have "(f' has_integral f x - f a) {a..x}" "(f' has_integral f b - f x) {x..b}"
  4018       using \<open>continuous_on {a..b} f\<close> \<open>a < x\<close> \<open>x < b\<close> continuous_on_subset by (force simp: intro!: insert)+
  4019     then have "(f' has_integral f x - f a + (f b - f x)) {a..b}"
  4020       using \<open>a < x\<close> \<open>x < b\<close> has_integral_combine less_imp_le by blast
  4021     then show ?thesis
  4022       by simp
  4023   qed
  4024 qed
  4025 
  4026 corollary fundamental_theorem_of_calculus_strong:
  4027   fixes f :: "real \<Rightarrow> 'a::banach"
  4028   assumes "finite S"
  4029     and "a \<le> b"
  4030     and vec: "\<And>x. x \<in> {a..b} - S \<Longrightarrow> (f has_vector_derivative f'(x)) (at x)"
  4031     and "continuous_on {a..b} f"
  4032   shows "(f' has_integral (f b - f a)) {a..b}"
  4033   by (rule fundamental_theorem_of_calculus_interior_strong [OF \<open>finite S\<close>]) (force simp: assms)+
  4034 
  4035 proposition indefinite_integral_continuous_left:
  4036   fixes f:: "real \<Rightarrow> 'a::banach"
  4037   assumes intf: "f integrable_on {a..b}" and "a < c" "c \<le> b" "e > 0"
  4038   obtains d where "d > 0"
  4039     and "\<forall>t. c - d < t \<and> t \<le> c \<longrightarrow> norm (integral {a..c} f - integral {a..t} f) < e"
  4040 proof -
  4041   obtain w where "w > 0" and w: "\<And>t. \<lbrakk>c - w < t; t < c\<rbrakk> \<Longrightarrow> norm (f c) * norm(c - t) < e/3"
  4042   proof (cases "f c = 0")
  4043     case False
  4044     hence e3: "0 < e/3 / norm (f c)" using \<open>e>0\<close> by simp
  4045     moreover have "norm (f c) * norm (c - t) < e/3" 
  4046       if "t < c" and "c - e/3 / norm (f c) < t" for t
  4047     proof -
  4048       have "norm (c - t) < e/3 / norm (f c)"
  4049         using that by auto
  4050       then show "norm (f c) * norm (c - t) < e/3"
  4051         by (metis e3 mult.commute norm_not_less_zero pos_less_divide_eq zero_less_divide_iff)
  4052     qed
  4053     ultimately show ?thesis
  4054       using that by auto
  4055   next
  4056     case True then show ?thesis
  4057       using \<open>e > 0\<close> that by auto
  4058   qed
  4059 
  4060   let ?SUM = "\<lambda>p. (\<Sum>(x,K) \<in> p. content K *\<^sub>R f x)"
  4061   have e3: "e/3 > 0"
  4062     using \<open>e > 0\<close> by auto
  4063   have "f integrable_on {a..c}"
  4064     apply (rule integrable_subinterval_real[OF intf])
  4065     using \<open>a < c\<close> \<open>c \<le> b\<close> by auto
  4066   then obtain d1 where "gauge d1" and d1:
  4067     "\<And>p. \<lbrakk>p tagged_division_of {a..c}; d1 fine p\<rbrakk> \<Longrightarrow> norm (?SUM p - integral {a..c} f) < e/3"
  4068     using integrable_integral has_integral_real e3 by metis
  4069   define d where [abs_def]: "d x = ball x w \<inter> d1 x" for x
  4070   have "gauge d"
  4071     unfolding d_def using \<open>w > 0\<close> \<open>gauge d1\<close> by auto
  4072   then obtain k where "0 < k" and k: "ball c k \<subseteq> d c"
  4073     by (meson gauge_def open_contains_ball)
  4074 
  4075   let ?d = "min k (c - a)/2"
  4076   show thesis
  4077   proof (intro that[of ?d] allI impI, safe)
  4078     show "?d > 0"
  4079       using \<open>0 < k\<close> \<open>a < c\<close> by auto
  4080   next
  4081     fix t
  4082     assume t: "c - ?d < t" "t \<le> c"
  4083     show "norm (integral ({a..c}) f - integral ({a..t}) f) < e"
  4084     proof (cases "t < c")
  4085       case False with \<open>t \<le> c\<close> show ?thesis
  4086         by (simp add: \<open>e > 0\<close>)
  4087     next
  4088       case True
  4089       have "f integrable_on {a..t}"
  4090         apply (rule integrable_subinterval_real[OF intf])
  4091         using \<open>t < c\<close> \<open>c \<le> b\<close> by auto
  4092       then obtain d2 where d2: "gauge d2"
  4093         "\<And>p. p tagged_division_of {a..t} \<and> d2 fine p \<Longrightarrow> norm (?SUM p - integral {a..t} f) < e/3"
  4094         using integrable_integral has_integral_real e3 by metis
  4095       define d3 where "d3 x = (if x \<le> t then d1 x \<inter> d2 x else d1 x)" for x
  4096       have "gauge d3"
  4097         using \<open>gauge d1\<close> \<open>gauge d2\<close> unfolding d3_def gauge_def by auto
  4098       then obtain p where ptag: "p tagged_division_of {a..t}" and pfine: "d3 fine p"
  4099         by (metis box_real(2) fine_division_exists)
  4100       note p' = tagged_division_ofD[OF ptag]
  4101       have pt: "(x,K)\<in>p \<Longrightarrow> x \<le> t" for x K
  4102         by (meson atLeastAtMost_iff p'(2) p'(3) subsetCE)
  4103       with pfine have "d2 fine p"
  4104         unfolding fine_def d3_def by fastforce
  4105       then have d2_fin: "norm (?SUM p - integral {a..t} f) < e/3"
  4106         using d2(2) ptag by auto
  4107       have eqs: "{a..c} \<inter> {x. x \<le> t} = {a..t}" "{a..c} \<inter> {x. x \<ge> t} = {t..c}"
  4108         using t by (auto simp add: field_simps)
  4109       have "p \<union> {(c, {t..c})} tagged_division_of {a..c}"
  4110         apply (rule tagged_division_Un_interval_real[of _ _ _ 1 "t"])
  4111         using  \<open>t \<le> c\<close> by (auto simp: eqs ptag tagged_division_of_self_real)
  4112       moreover
  4113       have "d1 fine p \<union> {(c, {t..c})}"
  4114         unfolding fine_def
  4115       proof safe
  4116         fix x K y
  4117         assume "(x,K) \<in> p" and "y \<in> K" then show "y \<in> d1 x"
  4118           by (metis Int_iff d3_def subsetD fineD pfine)
  4119       next
  4120         fix x assume "x \<in> {t..c}"
  4121         then have "dist c x < k"
  4122           using t(1) by (auto simp add: field_simps dist_real_def)
  4123         with k show "x \<in> d1 c"
  4124           unfolding d_def by auto
  4125       qed  
  4126       ultimately have d1_fin: "norm (?SUM(p \<union> {(c, {t..c})}) - integral {a..c} f) < e/3"
  4127         using d1 by metis
  4128       have SUMEQ: "?SUM(p \<union> {(c, {t..c})}) = (c - t) *\<^sub>R f c + ?SUM p"
  4129       proof -
  4130         have "?SUM(p \<union> {(c, {t..c})}) = (content{t..c} *\<^sub>R f c) + ?SUM p"
  4131         proof (subst sum.union_disjoint)
  4132           show "p \<inter> {(c, {t..c})} = {}"
  4133             using \<open>t < c\<close> pt by force
  4134         qed (use p'(1) in auto)
  4135         also have "... = (c - t) *\<^sub>R f c + ?SUM p"
  4136           using \<open>t \<le> c\<close> by auto
  4137         finally show ?thesis .
  4138       qed
  4139       have "c - k < t"
  4140         using \<open>k>0\<close> t(1) by (auto simp add: field_simps)
  4141       moreover have "k \<le> w"
  4142       proof (rule ccontr)
  4143         assume "\<not> k \<le> w"
  4144         then have "c + (k + w) / 2 \<notin> d c"
  4145           by (auto simp add: field_simps not_le not_less dist_real_def d_def)
  4146         then have "c + (k + w) / 2 \<notin> ball c k"
  4147           using k by blast
  4148         then show False
  4149           using \<open>0 < w\<close> \<open>\<not> k \<le> w\<close> dist_real_def by auto
  4150       qed
  4151       ultimately have cwt: "c - w < t"
  4152         by (auto simp add: field_simps)
  4153       have eq: "integral {a..c} f - integral {a..t} f = -(((c - t) *\<^sub>R f c + ?SUM p) -
  4154              integral {a..c} f) + (?SUM p - integral {a..t} f) + (c - t) *\<^sub>R f c"
  4155         by auto
  4156       have "norm (integral {a..c} f - integral {a..t} f) < e/3 + e/3 + e/3"
  4157         unfolding eq
  4158       proof (intro norm_triangle_lt add_strict_mono)
  4159         show "norm (- ((c - t) *\<^sub>R f c + ?SUM p - integral {a..c} f)) < e/3"
  4160           by (metis SUMEQ d1_fin norm_minus_cancel)
  4161         show "norm (?SUM p - integral {a..t} f) < e/3"
  4162           using d2_fin by blast
  4163         show "norm ((c - t) *\<^sub>R f c) < e/3"
  4164           using w cwt \<open>t < c\<close> by (auto simp add: field_simps)
  4165       qed
  4166       then show ?thesis by simp
  4167     qed
  4168   qed
  4169 qed
  4170 
  4171 lemma indefinite_integral_continuous_right:
  4172   fixes f :: "real \<Rightarrow> 'a::banach"
  4173   assumes "f integrable_on {a..b}"
  4174     and "a \<le> c"
  4175     and "c < b"
  4176     and "e > 0"
  4177   obtains d where "0 < d"
  4178     and "\<forall>t. c \<le> t \<and> t < c + d \<longrightarrow> norm (integral {a..c} f - integral {a..t} f) < e"
  4179 proof -
  4180   have intm: "(\<lambda>x. f (- x)) integrable_on {-b .. -a}" "- b < - c" "- c \<le> - a"
  4181     using assms by auto
  4182   from indefinite_integral_continuous_left[OF intm \<open>e>0\<close>]
  4183   obtain d where "0 < d"
  4184     and d: "\<And>t. \<lbrakk>- c - d < t; t \<le> -c\<rbrakk> 
  4185              \<Longrightarrow> norm (integral {- b..- c} (\<lambda>x. f (-x)) - integral {- b..t} (\<lambda>x. f (-x))) < e"
  4186     by metis
  4187   let ?d = "min d (b - c)" 
  4188   show ?thesis
  4189   proof (intro that[of "?d"] allI impI)
  4190     show "0 < ?d"
  4191       using \<open>0 < d\<close> \<open>c < b\<close> by auto
  4192     fix t :: real
  4193     assume t: "c \<le> t \<and> t < c + ?d"
  4194     have *: "integral {a..c} f = integral {a..b} f - integral {c..b} f"
  4195             "integral {a..t} f = integral {a..b} f - integral {t..b} f"
  4196       apply (simp_all only: algebra_simps)
  4197       using assms t by (auto simp: integral_combine)
  4198     have "(- c) - d < (- t)" "- t \<le> - c"
  4199       using t by auto 
  4200     from d[OF this] show "norm (integral {a..c} f - integral {a..t} f) < e"
  4201       by (auto simp add: algebra_simps norm_minus_commute *)
  4202   qed
  4203 qed
  4204 
  4205 lemma indefinite_integral_continuous_1:
  4206   fixes f :: "real \<Rightarrow> 'a::banach"
  4207   assumes "f integrable_on {a..b}"
  4208   shows "continuous_on {a..b} (\<lambda>x. integral {a..x} f)"
  4209 proof -
  4210   have "\<exists>d>0. \<forall>x'\<in>{a..b}. dist x' x < d \<longrightarrow> dist (integral {a..x'} f) (integral {a..x} f) < e" 
  4211        if x: "x \<in> {a..b}" and "e > 0" for x e :: real
  4212   proof (cases "a = b")
  4213     case True
  4214     with that show ?thesis by force
  4215   next
  4216     case False
  4217     with x have "a < b" by force
  4218     with x consider "x = a" | "x = b" | "a < x" "x < b"
  4219       by force
  4220     then show ?thesis 
  4221     proof cases
  4222       case 1 show ?thesis
  4223         apply (rule indefinite_integral_continuous_right [OF assms _ \<open>a < b\<close> \<open>e > 0\<close>], force)
  4224         using \<open>x = a\<close> apply (force simp: dist_norm algebra_simps)
  4225         done
  4226     next
  4227       case 2 show ?thesis 
  4228         apply (rule indefinite_integral_continuous_left [OF assms \<open>a < b\<close> _ \<open>e > 0\<close>], force)
  4229         using \<open>x = b\<close> apply (force simp: dist_norm norm_minus_commute algebra_simps)
  4230         done
  4231     next
  4232       case 3
  4233       obtain d1 where "0 < d1" 
  4234         and d1: "\<And>t. \<lbrakk>x - d1 < t; t \<le> x\<rbrakk> \<Longrightarrow> norm (integral {a..x} f - integral {a..t} f) < e"
  4235         using 3 by (auto intro: indefinite_integral_continuous_left [OF assms \<open>a < x\<close> _ \<open>e > 0\<close>])
  4236       obtain d2 where "0 < d2" 
  4237         and d2: "\<And>t. \<lbrakk>x \<le> t; t < x + d2\<rbrakk> \<Longrightarrow> norm (integral {a..x} f - integral {a..t} f) < e"
  4238         using 3 by (auto intro: indefinite_integral_continuous_right [OF assms _ \<open>x < b\<close> \<open>e > 0\<close>])
  4239       show ?thesis
  4240       proof (intro exI ballI conjI impI)
  4241         show "0 < min d1 d2"
  4242           using \<open>0 < d1\<close> \<open>0 < d2\<close> by simp
  4243         show "dist (integral {a..y} f) (integral {a..x} f) < e"
  4244              if "y \<in> {a..b}" "dist y x < min d1 d2" for y
  4245         proof (cases "y < x")
  4246           case True
  4247           with that d1 show ?thesis by (auto simp: dist_commute dist_norm)
  4248         next
  4249           case False
  4250           with that d2 show ?thesis
  4251             by (auto simp: dist_commute dist_norm)
  4252         qed
  4253       qed
  4254     qed
  4255   qed
  4256   then show ?thesis
  4257     by (auto simp: continuous_on_iff)
  4258 qed
  4259 
  4260 lemma indefinite_integral_continuous_1':
  4261   fixes f::"real \<Rightarrow> 'a::banach"
  4262   assumes "f integrable_on {a..b}"
  4263   shows "continuous_on {a..b} (\<lambda>x. integral {x..b} f)"
  4264 proof -
  4265   have "integral {a..b} f - integral {a..x} f = integral {x..b} f" if "x \<in> {a..b}" for x
  4266     using integral_combine[OF _ _ assms, of x] that
  4267     by (auto simp: algebra_simps)
  4268   with _ show ?thesis
  4269     by (rule continuous_on_eq) (auto intro!: continuous_intros indefinite_integral_continuous_1 assms)
  4270 qed
  4271 
  4272 theorem integral_has_vector_derivative':
  4273   fixes f :: "real \<Rightarrow> 'b::banach"
  4274   assumes "continuous_on {a..b} f"
  4275     and "x \<in> {a..b}"
  4276   shows "((\<lambda>u. integral {u..b} f) has_vector_derivative - f x) (at x within {a..b})"
  4277 proof -
  4278   have *: "integral {x..b} f = integral {a .. b} f - integral {a .. x} f" if "a \<le> x" "x \<le> b" for x
  4279     using integral_combine[of a x b for x, OF that integrable_continuous_real[OF assms(1)]]
  4280     by (simp add: algebra_simps)
  4281   show ?thesis
  4282     using \<open>x \<in> _\<close> *
  4283     by (rule has_vector_derivative_transform)
  4284       (auto intro!: derivative_eq_intros assms integral_has_vector_derivative)
  4285 qed
  4286 
  4287 lemma integral_has_real_derivative':
  4288   assumes "continuous_on {a..b} g"
  4289   assumes "t \<in> {a..b}"
  4290   shows "((\<lambda>x. integral {x..b} g) has_real_derivative -g t) (at t within {a..b})"
  4291   using integral_has_vector_derivative'[OF assms]
  4292   by (auto simp: has_field_derivative_iff_has_vector_derivative)
  4293 
  4294 
  4295 subsection \<open>This doesn't directly involve integration, but that gives an easy proof\<close>
  4296 
  4297 lemma has_derivative_zero_unique_strong_interval:
  4298   fixes f :: "real \<Rightarrow> 'a::banach"
  4299   assumes "finite k"
  4300     and contf: "continuous_on {a..b} f"
  4301     and "f a = y"
  4302     and fder: "\<And>x. x \<in> {a..b} - k \<Longrightarrow> (f has_derivative (\<lambda>h. 0)) (at x within {a..b})"
  4303     and x: "x \<in> {a..b}"
  4304   shows "f x = y"
  4305 proof -
  4306   have "a \<le> b" "a \<le> x"
  4307     using assms by auto
  4308   have "((\<lambda>x. 0::'a) has_integral f x - f a) {a..x}"
  4309   proof (rule fundamental_theorem_of_calculus_interior_strong[OF \<open>finite k\<close> \<open>a \<le> x\<close>]; clarify?)
  4310     have "{a..x} \<subseteq> {a..b}"
  4311       using x by auto
  4312     then show "continuous_on {a..x} f"
  4313       by (rule continuous_on_subset[OF contf])
  4314     show "(f has_vector_derivative 0) (at z)" if z: "z \<in> {a<..<x}" and notin: "z \<notin> k" for z
  4315       unfolding has_vector_derivative_def
  4316     proof (simp add: has_derivative_within_open[OF z, symmetric])
  4317       show "(f has_derivative (\<lambda>x. 0)) (at z within {a<..<x})"
  4318         by (rule has_derivative_within_subset [OF fder]) (use x z notin in auto)
  4319     qed
  4320   qed
  4321   from has_integral_unique[OF has_integral_0 this]
  4322   show ?thesis
  4323     unfolding assms by auto
  4324 qed
  4325 
  4326 
  4327 subsection \<open>Generalize a bit to any convex set\<close>
  4328 
  4329 lemma has_derivative_zero_unique_strong_convex:
  4330   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
  4331   assumes "convex S" "finite K"
  4332     and contf: "continuous_on S f"
  4333     and "c \<in> S" "f c = y"
  4334     and derf: "\<And>x. x \<in> S - K \<Longrightarrow> (f has_derivative (\<lambda>h. 0)) (at x within S)"
  4335     and "x \<in> S"
  4336   shows "f x = y"
  4337 proof (cases "x = c")
  4338   case True with \<open>f c = y\<close> show ?thesis
  4339     by blast
  4340 next
  4341   case False
  4342   let ?\<phi> = "\<lambda>u. (1 - u) *\<^sub>R c + u *\<^sub>R x"
  4343   have contf': "continuous_on {0 ..1} (f \<circ> ?\<phi>)"
  4344   proof (rule continuous_intros continuous_on_subset[OF contf])+
  4345     show "(\<lambda>u. (1 - u) *\<^sub>R c + u *\<^sub>R x) ` {0..1} \<subseteq> S"
  4346       using \<open>convex S\<close> \<open>x \<in> S\<close> \<open>c \<in> S\<close> by (auto simp add: convex_alt algebra_simps)
  4347   qed
  4348   have "t = u" if "?\<phi> t = ?\<phi> u" for t u
  4349   proof -
  4350     from that have "(t - u) *\<^sub>R x = (t - u) *\<^sub>R c"
  4351       by (auto simp add: algebra_simps)
  4352     then show ?thesis
  4353       using \<open>x \<noteq> c\<close> by auto
  4354   qed
  4355   then have eq: "(SOME t. ?\<phi> t = ?\<phi> u) = u" for u
  4356     by blast
  4357   then have "(?\<phi> -` K) \<subseteq> (\<lambda>z. SOME t. ?\<phi> t = z) ` K"
  4358     by (clarsimp simp: image_iff) (metis (no_types) eq)
  4359   then have fin: "finite (?\<phi> -` K)"
  4360     by (rule finite_surj[OF \<open>finite K\<close>])
  4361 
  4362   have derf': "((\<lambda>u. f (?\<phi> u)) has_derivative (\<lambda>h. 0)) (at t within {0..1})"
  4363                if "t \<in> {0..1} - {t. ?\<phi> t \<in> K}" for t
  4364   proof -
  4365     have df: "(f has_derivative (\<lambda>h. 0)) (at (?\<phi> t) within ?\<phi> ` {0..1})"
  4366       apply (rule has_derivative_within_subset [OF derf])
  4367       using \<open>convex S\<close> \<open>x \<in> S\<close> \<open>c \<in> S\<close> that by (auto simp add: convex_alt algebra_simps)
  4368     have "(f \<circ> ?\<phi> has_derivative (\<lambda>x. 0) \<circ> (\<lambda>z. (0 - z *\<^sub>R c) + z *\<^sub>R x)) (at t within {0..1})"
  4369       by (rule derivative_eq_intros df | simp)+
  4370     then show ?thesis
  4371       unfolding o_def .
  4372   qed
  4373   have "(f \<circ> ?\<phi>) 1 = y"
  4374     apply (rule has_derivative_zero_unique_strong_interval[OF fin contf'])
  4375     unfolding o_def using \<open>f c = y\<close> derf' by auto
  4376   then show ?thesis
  4377     by auto
  4378 qed
  4379 
  4380 
  4381 text \<open>Also to any open connected set with finite set of exceptions. Could
  4382  generalize to locally convex set with limpt-free set of exceptions.\<close>
  4383 
  4384 lemma has_derivative_zero_unique_strong_connected:
  4385   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
  4386   assumes "connected S"
  4387     and "open S"
  4388     and "finite K"
  4389     and contf: "continuous_on S f"
  4390     and "c \<in> S"
  4391     and "f c = y"
  4392     and derf: "\<And>x. x \<in> S - K \<Longrightarrow> (f has_derivative (\<lambda>h. 0)) (at x within S)"
  4393     and "x \<in> S"
  4394   shows "f x = y"
  4395 proof -
  4396   have "\<exists>e>0. ball x e \<subseteq> (S \<inter> f -` {f x})" if "x \<in> S" for x
  4397   proof -
  4398     obtain e where "0 < e" and e: "ball x e \<subseteq> S"
  4399       using \<open>x \<in> S\<close> \<open>open S\<close> open_contains_ball by blast
  4400     have "ball x e \<subseteq> {u \<in> S. f u \<in> {f x}}"
  4401     proof safe
  4402       fix y
  4403       assume y: "y \<in> ball x e"
  4404       then show "y \<in> S"
  4405         using e by auto
  4406       show "f y = f x"
  4407       proof (rule has_derivative_zero_unique_strong_convex[OF convex_ball \<open>finite K\<close>])
  4408         show "continuous_on (ball x e) f"
  4409           using contf continuous_on_subset e by blast
  4410         show "(f has_derivative (\<lambda>h. 0)) (at u within ball x e)"
  4411              if "u \<in> ball x e - K" for u
  4412           by (metis Diff_iff contra_subsetD derf e has_derivative_within_subset that)
  4413       qed (use y e \<open>0 < e\<close> in auto)
  4414     qed
  4415     then show "\<exists>e>0. ball x e \<subseteq> (S \<inter> f -` {f x})"
  4416       using \<open>0 < e\<close> by blast
  4417   qed
  4418   then have "openin (top_of_set S) (S \<inter> f -` {y})"
  4419     by (auto intro!: open_openin_trans[OF \<open>open S\<close>] simp: open_contains_ball)
  4420   moreover have "closedin (top_of_set S) (S \<inter> f -` {y})"
  4421     by (force intro!: continuous_closedin_preimage [OF contf])
  4422   ultimately have "(S \<inter> f -` {y}) = {} \<or> (S \<inter> f -` {y}) = S"
  4423     using \<open>connected S\<close> by (simp add: connected_clopen)
  4424   then show ?thesis
  4425     using \<open>x \<in> S\<close> \<open>f c = y\<close> \<open>c \<in> S\<close> by auto
  4426 qed
  4427 
  4428 lemma has_derivative_zero_connected_constant:
  4429   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
  4430   assumes "connected S"
  4431       and "open S"
  4432       and "finite k"
  4433       and "continuous_on S f"
  4434       and "\<forall>x\<in>(S - k). (f has_derivative (\<lambda>h. 0)) (at x within S)"
  4435     obtains c where "\<And>x. x \<in> S \<Longrightarrow> f(x) = c"
  4436 proof (cases "S = {}")
  4437   case True
  4438   then show ?thesis
  4439 by (metis empty_iff that)
  4440 next
  4441   case False
  4442   then obtain c where "c \<in> S"
  4443     by (metis equals0I)
  4444   then show ?thesis
  4445     by (metis has_derivative_zero_unique_strong_connected assms that)
  4446 qed
  4447 
  4448 
  4449 subsection \<open>Integrating characteristic function of an interval\<close>
  4450 
  4451 lemma has_integral_restrict_open_subinterval:
  4452   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
  4453   assumes intf: "(f has_integral i) (cbox c d)"
  4454     and cb: "cbox c d \<subseteq> cbox a b"
  4455   shows "((\<lambda>x. if x \<in> box c d then f x else 0) has_integral i) (cbox a b)"
  4456 proof (cases "cbox c d = {}")
  4457   case True
  4458   then have "box c d = {}"
  4459     by (metis bot.extremum_uniqueI box_subset_cbox)
  4460   then show ?thesis
  4461     using True intf by auto
  4462 next
  4463   case False
  4464   then obtain p where pdiv: "p division_of cbox a b" and inp: "cbox c d \<in> p"
  4465     using cb partial_division_extend_1 by blast
  4466   define g where [abs_def]: "g x = (if x \<in>box c d then f x else 0)" for x
  4467   interpret operative "lift_option plus" "Some (0 :: 'b)"
  4468     "\<lambda>i. if g integrable_on i then Some (integral i g) else None"
  4469     by (fact operative_integralI)
  4470   note operat = division [OF pdiv, symmetric]
  4471   show ?thesis
  4472   proof (cases "(g has_integral i) (cbox a b)")
  4473     case True then show ?thesis
  4474       by (simp add: g_def)
  4475   next
  4476     case False
  4477     have iterate:"F (\<lambda>i. if g integrable_on i then Some (integral i g) else None) (p - {cbox c d}) = Some 0"
  4478     proof (intro neutral ballI)
  4479       fix x
  4480       assume x: "x \<in> p - {cbox c d}"
  4481       then have "x \<in> p"
  4482         by auto
  4483       then obtain u v where uv: "x = cbox u v"
  4484         using pdiv by blast
  4485       have "interior x \<inter> interior (cbox c d) = {}"
  4486         using pdiv inp x by blast 
  4487       then have "(g has_integral 0) x"
  4488         unfolding uv using has_integral_spike_interior[where f="\<lambda>x. 0"]
  4489         by (metis (no_types, lifting) disjoint_iff_not_equal g_def has_integral_0_eq interior_cbox)
  4490       then show "(if g integrable_on x then Some (integral x g) else None) = Some 0"
  4491         by auto
  4492     qed
  4493     interpret comm_monoid_set "lift_option plus" "Some (0 :: 'b)"
  4494       by (intro comm_monoid_set.intro comm_monoid_lift_option add.comm_monoid_axioms)
  4495     have intg: "g integrable_on cbox c d"
  4496       using integrable_spike_interior[where f=f]
  4497       by (meson g_def has_integral_integrable intf)
  4498     moreover have "integral (cbox c d) g = i"
  4499     proof (rule has_integral_unique[OF has_integral_spike_interior intf])
  4500       show "\<And>x. x \<in> box c d \<Longrightarrow> f x = g x"
  4501         by (auto simp: g_def)
  4502       show "(g has_integral integral (cbox c d) g) (cbox c d)"
  4503         by (rule integrable_integral[OF intg])
  4504     qed
  4505     ultimately have "F (\<lambda>A. if g integrable_on A then Some (integral A g) else None) p = Some i"
  4506       by (metis (full_types, lifting) division_of_finite inp iterate pdiv remove right_neutral)
  4507     then
  4508     have "(g has_integral i) (cbox a b)"
  4509       by (metis integrable_on_def integral_unique operat option.inject option.simps(3))
  4510     with False show ?thesis
  4511       by blast
  4512   qed
  4513 qed
  4514 
  4515 
  4516 lemma has_integral_restrict_closed_subinterval:
  4517   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
  4518   assumes "(f has_integral i) (cbox c d)"
  4519     and "cbox c d \<subseteq> cbox a b"
  4520   shows "((\<lambda>x. if x \<in> cbox c d then f x else 0) has_integral i) (cbox a b)"
  4521 proof -
  4522   note has_integral_restrict_open_subinterval[OF assms]
  4523   note * = has_integral_spike[OF negligible_frontier_interval _ this]
  4524   show ?thesis
  4525     by (rule *[of c d]) (use box_subset_cbox[of c d] in auto)
  4526 qed
  4527 
  4528 lemma has_integral_restrict_closed_subintervals_eq:
  4529   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
  4530   assumes "cbox c d \<subseteq> cbox a b"
  4531   shows "((\<lambda>x. if x \<in> cbox c d then f x else 0) has_integral i) (cbox a b) \<longleftrightarrow> (f has_integral i) (cbox c d)"
  4532   (is "?l = ?r")
  4533 proof (cases "cbox c d = {}")
  4534   case False
  4535   let ?g = "\<lambda>x. if x \<in> cbox c d then f x else 0"
  4536   show ?thesis
  4537   proof 
  4538     assume ?l
  4539     then have "?g integrable_on cbox c d"
  4540       using assms has_integral_integrable integrable_subinterval by blast
  4541     then have "f integrable_on cbox c d"
  4542       by (rule integrable_eq) auto
  4543     moreover then have "i = integral (cbox c d) f"
  4544       by (meson \<open>((\<lambda>x. if x \<in> cbox c d then f x else 0) has_integral i) (cbox a b)\<close> assms has_integral_restrict_closed_subinterval has_integral_unique integrable_integral)
  4545     ultimately show ?r by auto
  4546   next
  4547     assume ?r then show ?l
  4548       by (rule has_integral_restrict_closed_subinterval[OF _ assms])
  4549   qed
  4550 qed auto
  4551 
  4552 
  4553 text \<open>Hence we can apply the limit process uniformly to all integrals.\<close>
  4554 
  4555 lemma has_integral':
  4556   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  4557   shows "(f has_integral i) S \<longleftrightarrow>
  4558     (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
  4559       (\<exists>z. ((\<lambda>x. if x \<in> S then f(x) else 0) has_integral z) (cbox a b) \<and> norm(z - i) < e))"
  4560   (is "?l \<longleftrightarrow> (\<forall>e>0. ?r e)")
  4561 proof (cases "\<exists>a b. S = cbox a b")
  4562   case False then show ?thesis 
  4563     by (simp add: has_integral_alt)
  4564 next
  4565   case True
  4566   then obtain a b where S: "S = cbox a b"
  4567     by blast 
  4568   obtain B where " 0 < B" and B: "\<And>x. x \<in> cbox a b \<Longrightarrow> norm x \<le> B"
  4569     using bounded_cbox[unfolded bounded_pos] by blast
  4570   show ?thesis
  4571   proof safe
  4572     fix e :: real
  4573     assume ?l and "e > 0"
  4574     have "((\<lambda>x. if x \<in> S then f x else 0) has_integral i) (cbox c d)"
  4575       if "ball 0 (B+1) \<subseteq> cbox c d" for c d
  4576         unfolding S using B that
  4577         by (force intro: \<open>?l\<close>[unfolded S] has_integral_restrict_closed_subinterval)
  4578     then show "?r e"
  4579       apply (rule_tac x="B+1" in exI)
  4580       using \<open>B>0\<close> \<open>e>0\<close> by force
  4581   next
  4582     assume as: "\<forall>e>0. ?r e"
  4583     then obtain C 
  4584       where C: "\<And>a b. ball 0 C \<subseteq> cbox a b \<Longrightarrow>
  4585                 \<exists>z. ((\<lambda>x. if x \<in> S then f x else 0) has_integral z) (cbox a b)"
  4586       by (meson zero_less_one)
  4587     define c :: 'n where "c = (\<Sum>i\<in>Basis. (- max B C) *\<^sub>R i)"
  4588     define d :: 'n where "d = (\<Sum>i\<in>Basis. max B C *\<^sub>R i)"
  4589     have "c \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> d \<bullet> i" if "norm x \<le> B" "i \<in> Basis" for x i
  4590       using that and Basis_le_norm[OF \<open>i\<in>Basis\<close>, of x]
  4591       by (auto simp add: field_simps sum_negf c_def d_def)
  4592     then have c_d: "cbox a b \<subseteq> cbox c d"
  4593       by (meson B mem_box(2) subsetI)
  4594     have "c \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> d \<bullet> i"
  4595       if x: "norm (0 - x) < C" and i: "i \<in> Basis" for x i
  4596         using Basis_le_norm[OF i, of x] x i by (auto simp: sum_negf c_def d_def)
  4597       then have "ball 0 C \<subseteq> cbox c d"
  4598         by (auto simp: mem_box dist_norm)
  4599     with C obtain y where y: "(f has_integral y) (cbox a b)"
  4600       using c_d has_integral_restrict_closed_subintervals_eq S by blast
  4601     have "y = i"
  4602     proof (rule ccontr)
  4603       assume "y \<noteq> i"
  4604       then have "0 < norm (y - i)"
  4605         by auto
  4606       from as[rule_format,OF this]
  4607       obtain C where C: "\<And>a b. ball 0 C \<subseteq> cbox a b \<Longrightarrow> 
  4608            \<exists>z. ((\<lambda>x. if x \<in> S then f x else 0) has_integral z) (cbox a b) \<and> norm (z-i) < norm (y-i)"
  4609         by auto
  4610       define c :: 'n where "c = (\<Sum>i\<in>Basis. (- max B C) *\<^sub>R i)"
  4611       define d :: 'n where "d = (\<Sum>i\<in>Basis. max B C *\<^sub>R i)"
  4612       have "c \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> d \<bullet> i"
  4613         if "norm x \<le> B" and "i \<in> Basis" for x i
  4614           using that Basis_le_norm[of i x] by (auto simp add: field_simps sum_negf c_def d_def)
  4615         then have c_d: "cbox a b \<subseteq> cbox c d"
  4616         by (simp add: B mem_box(2) subset_eq)
  4617       have "c \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> d \<bullet> i" if "norm (0 - x) < C" and "i \<in> Basis" for x i
  4618         using Basis_le_norm[of i x] that by (auto simp: sum_negf c_def d_def)
  4619       then have "ball 0 C \<subseteq> cbox c d"
  4620         by (auto simp: mem_box dist_norm)
  4621       with C obtain z where z: "(f has_integral z) (cbox a b)" "norm (z-i) < norm (y-i)"
  4622         using has_integral_restrict_closed_subintervals_eq[OF c_d] S by blast
  4623       moreover then have "z = y" 
  4624         by (blast intro: has_integral_unique[OF _ y])
  4625       ultimately show False
  4626         by auto
  4627     qed
  4628     then show ?l
  4629       using y by (auto simp: S)
  4630   qed
  4631 qed
  4632 
  4633 lemma has_integral_le:
  4634   fixes f :: "'n::euclidean_space \<Rightarrow> real"
  4635   assumes fg: "(f has_integral i) S" "(g has_integral j) S"
  4636     and le: "\<And>x. x \<in> S \<Longrightarrow> f x \<le> g x"
  4637   shows "i \<le> j"
  4638   using has_integral_component_le[OF _ fg, of 1] le  by auto
  4639 
  4640 lemma integral_le:
  4641   fixes f :: "'n::euclidean_space \<Rightarrow> real"
  4642   assumes "f integrable_on S"
  4643     and "g integrable_on S"
  4644     and "\<And>x. x \<in> S \<Longrightarrow> f x \<le> g x"
  4645   shows "integral S f \<le> integral S g"
  4646   by (rule has_integral_le[OF assms(1,2)[unfolded has_integral_integral] assms(3)])
  4647 
  4648 lemma has_integral_nonneg:
  4649   fixes f :: "'n::euclidean_space \<Rightarrow> real"
  4650   assumes "(f has_integral i) S"
  4651     and "\<And>x. x \<in> S \<Longrightarrow> 0 \<le> f x"
  4652   shows "0 \<le> i"
  4653   using has_integral_component_nonneg[of 1 f i S]
  4654   unfolding o_def
  4655   using assms
  4656   by auto
  4657 
  4658 lemma integral_nonneg:
  4659   fixes f :: "'n::euclidean_space \<Rightarrow> real"
  4660   assumes f: "f integrable_on S" and 0: "\<And>x. x \<in> S \<Longrightarrow> 0 \<le> f x"
  4661   shows "0 \<le> integral S f"
  4662   by (rule has_integral_nonneg[OF f[unfolded has_integral_integral] 0])
  4663 
  4664 
  4665 text \<open>Hence a general restriction property.\<close>
  4666 
  4667 lemma has_integral_restrict [simp]:
  4668   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: banach"
  4669   assumes "S \<subseteq> T"
  4670   shows "((\<lambda>x. if x \<in> S then f x else 0) has_integral i) T \<longleftrightarrow> (f has_integral i) S"
  4671 proof -
  4672   have *: "\<And>x. (if x \<in> T then if x \<in> S then f x else 0 else 0) =  (if x\<in>S then f x else 0)"
  4673     using assms by auto
  4674   show ?thesis
  4675     apply (subst(2) has_integral')
  4676     apply (subst has_integral')
  4677       apply (simp add: *)
  4678     done
  4679 qed
  4680 
  4681 corollary has_integral_restrict_UNIV:
  4682   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  4683   shows "((\<lambda>x. if x \<in> s then f x else 0) has_integral i) UNIV \<longleftrightarrow> (f has_integral i) s"
  4684   by auto
  4685 
  4686 lemma has_integral_restrict_Int:
  4687   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: banach"
  4688   shows "((\<lambda>x. if x \<in> S then f x else 0) has_integral i) T \<longleftrightarrow> (f has_integral i) (S \<inter> T)"
  4689 proof -
  4690   have "((\<lambda>x. if x \<in> T then if x \<in> S then f x else 0 else 0) has_integral i) UNIV =
  4691         ((\<lambda>x. if x \<in> S \<inter> T then f x else 0) has_integral i) UNIV"
  4692     by (rule has_integral_cong) auto
  4693   then show ?thesis
  4694     using has_integral_restrict_UNIV by fastforce
  4695 qed
  4696 
  4697 lemma integral_restrict_Int:
  4698   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: banach"
  4699   shows "integral T (\<lambda>x. if x \<in> S then f x else 0) = integral (S \<inter> T) f"
  4700   by (metis (no_types, lifting) has_integral_cong has_integral_restrict_Int integrable_integral integral_unique not_integrable_integral)
  4701 
  4702 lemma integrable_restrict_Int:
  4703   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: banach"
  4704   shows "(\<lambda>x. if x \<in> S then f x else 0) integrable_on T \<longleftrightarrow> f integrable_on (S \<inter> T)"
  4705   using has_integral_restrict_Int by fastforce
  4706 
  4707 lemma has_integral_on_superset:
  4708   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  4709   assumes f: "(f has_integral i) S"
  4710       and "\<And>x. x \<notin> S \<Longrightarrow> f x = 0"
  4711       and "S \<subseteq> T"
  4712     shows "(f has_integral i) T"
  4713 proof -
  4714   have "(\<lambda>x. if x \<in> S then f x else 0) = (\<lambda>x. if x \<in> T then f x else 0)"
  4715     using assms by fastforce
  4716   with f show ?thesis
  4717     by (simp only: has_integral_restrict_UNIV [symmetric, of f])
  4718 qed
  4719 
  4720 lemma integrable_on_superset:
  4721   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  4722   assumes "f integrable_on S"
  4723     and "\<And>x. x \<notin> S \<Longrightarrow> f x = 0"
  4724     and "S \<subseteq> t"
  4725   shows "f integrable_on t"
  4726   using assms
  4727   unfolding integrable_on_def
  4728   by (auto intro:has_integral_on_superset)
  4729 
  4730 lemma integral_restrict_UNIV:
  4731   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  4732   shows "integral UNIV (\<lambda>x. if x \<in> S then f x else 0) = integral S f"
  4733   by (simp add: integral_restrict_Int)
  4734 
  4735 lemma integrable_restrict_UNIV:
  4736   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  4737   shows "(\<lambda>x. if x \<in> s then f x else 0) integrable_on UNIV \<longleftrightarrow> f integrable_on s"
  4738   unfolding integrable_on_def
  4739   by auto
  4740 
  4741 lemma has_integral_subset_component_le:
  4742   fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
  4743   assumes k: "k \<in> Basis"
  4744     and as: "S \<subseteq> T" "(f has_integral i) S" "(f has_integral j) T" "\<And>x. x\<in>T \<Longrightarrow> 0 \<le> f(x)\<bullet>k"
  4745   shows "i\<bullet>k \<le> j\<bullet>k"
  4746 proof -
  4747   have "((\<lambda>x. if x \<in> S then f x else 0) has_integral i) UNIV"
  4748         "((\<lambda>x. if x \<in> T then f x else 0) has_integral j) UNIV"
  4749     by (simp_all add: assms)
  4750   then show ?thesis
  4751     apply (rule has_integral_component_le[OF k])
  4752     using as by auto
  4753 qed
  4754 
  4755 subsection\<open>Integrals on set differences\<close>
  4756 
  4757 lemma has_integral_setdiff:
  4758   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
  4759   assumes S: "(f has_integral i) S" and T: "(f has_integral j) T"
  4760     and neg: "negligible (T - S)"
  4761   shows "(f has_integral (i - j)) (S - T)"
  4762 proof -
  4763   show ?thesis
  4764     unfolding has_integral_restrict_UNIV [symmetric, of f]
  4765   proof (rule has_integral_spike [OF neg])
  4766     have eq: "(\<lambda>x. (if x \<in> S then f x else 0) - (if x \<in> T then f x else 0)) =
  4767             (\<lambda>x. if x \<in> T - S then - f x else if x \<in> S - T then f x else 0)"
  4768       by (force simp add: )
  4769     have "((\<lambda>x. if x \<in> S then f x else 0) has_integral i) UNIV"
  4770       "((\<lambda>x. if x \<in> T then f x else 0) has_integral j) UNIV"
  4771       using S T has_integral_restrict_UNIV by auto
  4772     from has_integral_diff [OF this]
  4773     show "((\<lambda>x. if x \<in> T - S then - f x else if x \<in> S - T then f x else 0)
  4774                    has_integral i-j) UNIV"
  4775       by (simp add: eq)
  4776   qed force
  4777 qed
  4778 
  4779 lemma integral_setdiff:
  4780   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
  4781   assumes "f integrable_on S" "f integrable_on T" "negligible(T - S)"
  4782  shows "integral (S - T) f = integral S f - integral T f"
  4783   by (rule integral_unique) (simp add: assms has_integral_setdiff integrable_integral)
  4784 
  4785 lemma integrable_setdiff:
  4786   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
  4787   assumes "(f has_integral i) S" "(f has_integral j) T" "negligible (T - S)"
  4788   shows "f integrable_on (S - T)"
  4789   using has_integral_setdiff [OF assms]
  4790   by (simp add: has_integral_iff)
  4791 
  4792 lemma negligible_setdiff [simp]: "T \<subseteq> S \<Longrightarrow> negligible (T - S)"
  4793   by (metis Diff_eq_empty_iff negligible_empty)
  4794 
  4795 lemma negligible_on_intervals: "negligible s \<longleftrightarrow> (\<forall>a b. negligible(s \<inter> cbox a b))" (is "?l \<longleftrightarrow> ?r")
  4796 proof
  4797   assume ?r
  4798   show ?l
  4799     unfolding negligible_def
  4800   proof safe
  4801     fix a b
  4802     show "(indicator s has_integral 0) (cbox a b)"
  4803       apply (rule has_integral_negligible[OF \<open>?r\<close>[rule_format,of a b]])
  4804       unfolding indicator_def
  4805       apply auto
  4806       done
  4807   qed
  4808 qed (simp add: negligible_Int)
  4809 
  4810 lemma negligible_translation:
  4811   assumes "negligible S"
  4812     shows "negligible ((+) c ` S)"
  4813 proof -
  4814   have inj: "inj ((+) c)"
  4815     by simp
  4816   show ?thesis
  4817   using assms
  4818   proof (clarsimp simp: negligible_def)
  4819     fix a b
  4820     assume "\<forall>x y. (indicator S has_integral 0) (cbox x y)"
  4821     then have *: "(indicator S has_integral 0) (cbox (a-c) (b-c))"
  4822       by (meson Diff_iff assms has_integral_negligible indicator_simps(2))
  4823     have eq: "indicator ((+) c ` S) = (\<lambda>x. indicator S (x - c))"
  4824       by (force simp add: indicator_def)
  4825     show "(indicator ((+) c ` S) has_integral 0) (cbox a b)"
  4826       using has_integral_affinity [OF *, of 1 "-c"]
  4827             cbox_translation [of "c" "-c+a" "-c+b"]
  4828       by (simp add: eq) (simp add: ac_simps)
  4829   qed
  4830 qed
  4831 
  4832 lemma negligible_translation_rev:
  4833   assumes "negligible ((+) c ` S)"
  4834     shows "negligible S"
  4835 by (metis negligible_translation [OF assms, of "-c"] translation_galois)
  4836 
  4837 lemma has_integral_spike_set_eq:
  4838   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  4839   assumes "negligible {x \<in> S - T. f x \<noteq> 0}" "negligible {x \<in> T - S. f x \<noteq> 0}"
  4840   shows "(f has_integral y) S \<longleftrightarrow> (f has_integral y) T"
  4841 proof -
  4842   have "((\<lambda>x. if x \<in> S then f x else 0) has_integral y) UNIV =
  4843         ((\<lambda>x. if x \<in> T then f x else 0) has_integral y) UNIV"
  4844   proof (rule has_integral_spike_eq)
  4845     show "negligible ({x \<in> S - T. f x \<noteq> 0} \<union> {x \<in> T - S. f x \<noteq> 0})"
  4846       by (rule negligible_Un [OF assms])
  4847   qed auto
  4848   then show ?thesis
  4849     by (simp add: has_integral_restrict_UNIV)
  4850 qed
  4851 
  4852 corollary integral_spike_set:
  4853   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  4854   assumes "negligible {x \<in> S - T. f x \<noteq> 0}" "negligible {x \<in> T - S. f x \<noteq> 0}"
  4855   shows "integral S f = integral T f"
  4856   using has_integral_spike_set_eq [OF assms]
  4857   by (metis eq_integralD integral_unique)
  4858 
  4859 lemma integrable_spike_set:
  4860   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  4861   assumes f: "f integrable_on S" and neg: "negligible {x \<in> S - T. f x \<noteq> 0}" "negligible {x \<in> T - S. f x \<noteq> 0}"
  4862   shows "f integrable_on T"
  4863   using has_integral_spike_set_eq [OF neg] f by blast
  4864 
  4865 lemma integrable_spike_set_eq:
  4866   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  4867   assumes "negligible ((S - T) \<union> (T - S))"
  4868   shows "f integrable_on S \<longleftrightarrow> f integrable_on T"
  4869   by (blast intro: integrable_spike_set assms negligible_subset)
  4870 
  4871 lemma integrable_on_insert_iff: "f integrable_on (insert x X) \<longleftrightarrow> f integrable_on X"
  4872   for f::"_ \<Rightarrow> 'a::banach"
  4873   by (rule integrable_spike_set_eq) (auto simp: insert_Diff_if)
  4874 
  4875 lemma has_integral_interior:
  4876   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: banach"
  4877   shows "negligible(frontier S) \<Longrightarrow> (f has_integral y) (interior S) \<longleftrightarrow> (f has_integral y) S"
  4878   by (rule has_integral_spike_set_eq [OF empty_imp_negligible negligible_subset])
  4879      (use interior_subset in \<open>auto simp: frontier_def closure_def\<close>)
  4880 
  4881 lemma has_integral_closure:
  4882   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: banach"
  4883   shows "negligible(frontier S) \<Longrightarrow> (f has_integral y) (closure S) \<longleftrightarrow> (f has_integral y) S"
  4884   by (rule has_integral_spike_set_eq [OF negligible_subset empty_imp_negligible]) (auto simp: closure_Un_frontier )
  4885 
  4886 lemma has_integral_open_interval:
  4887   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: banach"
  4888   shows "(f has_integral y) (box a b) \<longleftrightarrow> (f has_integral y) (cbox a b)"
  4889   unfolding interior_cbox [symmetric]
  4890   by (metis frontier_cbox has_integral_interior negligible_frontier_interval)
  4891 
  4892 lemma integrable_on_open_interval:
  4893   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: banach"
  4894   shows "f integrable_on box a b \<longleftrightarrow> f integrable_on cbox a b"
  4895   by (simp add: has_integral_open_interval integrable_on_def)
  4896 
  4897 lemma integral_open_interval:
  4898   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: banach"
  4899   shows "integral(box a b) f = integral(cbox a b) f"
  4900   by (metis has_integral_integrable_integral has_integral_open_interval not_integrable_integral)
  4901 
  4902 
  4903 subsection \<open>More lemmas that are useful later\<close>
  4904 
  4905 lemma has_integral_subset_le:
  4906   fixes f :: "'n::euclidean_space \<Rightarrow> real"
  4907   assumes "s \<subseteq> t"
  4908     and "(f has_integral i) s"
  4909     and "(f has_integral j) t"
  4910     and "\<forall>x\<in>t. 0 \<le> f x"
  4911   shows "i \<le> j"
  4912   using has_integral_subset_component_le[OF _ assms(1), of 1 f i j]
  4913   using assms
  4914   by auto
  4915 
  4916 lemma integral_subset_component_le:
  4917   fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
  4918   assumes "k \<in> Basis"
  4919     and "s \<subseteq> t"
  4920     and "f integrable_on s"
  4921     and "f integrable_on t"
  4922     and "\<forall>x \<in> t. 0 \<le> f x \<bullet> k"
  4923   shows "(integral s f)\<bullet>k \<le> (integral t f)\<bullet>k"
  4924   apply (rule has_integral_subset_component_le)
  4925   using assms
  4926   apply auto
  4927   done
  4928 
  4929 lemma integral_subset_le:
  4930   fixes f :: "'n::euclidean_space \<Rightarrow> real"
  4931   assumes "s \<subseteq> t"
  4932     and "f integrable_on s"
  4933     and "f integrable_on t"
  4934     and "\<forall>x \<in> t. 0 \<le> f x"
  4935   shows "integral s f \<le> integral t f"
  4936   apply (rule has_integral_subset_le)
  4937   using assms
  4938   apply auto
  4939   done
  4940 
  4941 lemma has_integral_alt':
  4942   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  4943   shows "(f has_integral i) s \<longleftrightarrow> 
  4944           (\<forall>a b. (\<lambda>x. if x \<in> s then f x else 0) integrable_on cbox a b) \<and>
  4945           (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
  4946             norm (integral (cbox a b) (\<lambda>x. if x \<in> s then f x else 0) - i) < e)"
  4947   (is "?l = ?r")
  4948 proof
  4949   assume rhs: ?r
  4950   show ?l
  4951   proof (subst has_integral', intro allI impI)
  4952     fix e::real
  4953     assume "e > 0"
  4954     from rhs[THEN conjunct2,rule_format,OF this] 
  4955     show "\<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
  4956                    (\<exists>z. ((\<lambda>x. if x \<in> s then f x else 0) has_integral z)
  4957                          (cbox a b) \<and> norm (z - i) < e)"
  4958       apply (rule ex_forward)
  4959       using rhs by blast
  4960   qed
  4961 next
  4962   let ?\<Phi> = "\<lambda>e a b. \<exists>z. ((\<lambda>x. if x \<in> s then f x else 0) has_integral z) (cbox a b) \<and> norm (z - i) < e"
  4963   assume ?l 
  4964   then have lhs: "\<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow> ?\<Phi> e a b" if "e > 0" for e
  4965     using that has_integral'[of f] by auto
  4966   let ?f = "\<lambda>x. if x \<in> s then f x else 0"
  4967   show ?r
  4968   proof (intro conjI allI impI)
  4969     fix a b :: 'n
  4970     from lhs[OF zero_less_one]
  4971     obtain B where "0 < B" and B: "\<And>a b. ball 0 B \<subseteq> cbox a b \<Longrightarrow> ?\<Phi> 1 a b"
  4972       by blast
  4973     let ?a = "\<Sum>i\<in>Basis. min (a\<bullet>i) (-B) *\<^sub>R i::'n"
  4974     let ?b = "\<Sum>i\<in>Basis. max (b\<bullet>i) B *\<^sub>R i::'n"
  4975     show "?f integrable_on cbox a b"
  4976     proof (rule integrable_subinterval[of _ ?a ?b])
  4977       have "?a \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> ?b \<bullet> i" if "norm (0 - x) < B" "i \<in> Basis" for x i
  4978         using Basis_le_norm[of i x] that by (auto simp add:field_simps)
  4979       then have "ball 0 B \<subseteq> cbox ?a ?b"
  4980         by (auto simp: mem_box dist_norm)
  4981       then show "?f integrable_on cbox ?a ?b"
  4982         unfolding integrable_on_def using B by blast
  4983       show "cbox a b \<subseteq> cbox ?a ?b"
  4984         by (force simp: mem_box)
  4985     qed
  4986   
  4987     fix e :: real
  4988     assume "e > 0"
  4989     with lhs show "\<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
  4990       norm (integral (cbox a b) (\<lambda>x. if x \<in> s then f x else 0) - i) < e"
  4991       by (metis (no_types, lifting) has_integral_integrable_integral)
  4992   qed
  4993 qed
  4994 
  4995 
  4996 subsection \<open>Continuity of the integral (for a 1-dimensional interval)\<close>
  4997 
  4998 lemma integrable_alt:
  4999   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  5000   shows "f integrable_on s \<longleftrightarrow>
  5001     (\<forall>a b. (\<lambda>x. if x \<in> s then f x else 0) integrable_on cbox a b) \<and>
  5002     (\<forall>e>0. \<exists>B>0. \<forall>a b c d. ball 0 B \<subseteq> cbox a b \<and> ball 0 B \<subseteq> cbox c d \<longrightarrow>
  5003     norm (integral (cbox a b) (\<lambda>x. if x \<in> s then f x else 0) -
  5004       integral (cbox c d)  (\<lambda>x. if x \<in> s then f x else 0)) < e)"
  5005   (is "?l = ?r")
  5006 proof
  5007   let ?F = "\<lambda>x. if x \<in> s then f x else 0"
  5008   assume ?l
  5009   then obtain y where intF: "\<And>a b. ?F integrable_on cbox a b"
  5010           and y: "\<And>e. 0 < e \<Longrightarrow>
  5011               \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow> norm (integral (cbox a b) ?F - y) < e"
  5012     unfolding integrable_on_def has_integral_alt'[of f] by auto
  5013   show ?r
  5014   proof (intro conjI allI impI intF)
  5015     fix e::real
  5016     assume "e > 0"
  5017     then have "e/2 > 0"
  5018       by auto
  5019     obtain B where "0 < B" 
  5020        and B: "\<And>a b. ball 0 B \<subseteq> cbox a b \<Longrightarrow> norm (integral (cbox a b) ?F - y) < e/2"
  5021       using \<open>0 < e/2\<close> y by blast
  5022     show "\<exists>B>0. \<forall>a b c d. ball 0 B \<subseteq> cbox a b \<and> ball 0 B \<subseteq> cbox c d \<longrightarrow>
  5023                   norm (integral (cbox a b) ?F - integral (cbox c d) ?F) < e"
  5024     proof (intro conjI exI impI allI, rule \<open>0 < B\<close>)
  5025       fix a b c d::'n
  5026       assume sub: "ball 0 B \<subseteq> cbox a b \<and> ball 0 B \<subseteq> cbox c d"
  5027       show "norm (integral (cbox a b) ?F - integral (cbox c d) ?F) < e"
  5028         using sub by (auto intro: norm_triangle_half_l dest: B)
  5029     qed
  5030   qed
  5031 next
  5032   let ?F = "\<lambda>x. if x \<in> s then f x else 0"
  5033   assume rhs: ?r
  5034   let ?cube = "\<lambda>n. cbox (\<Sum>i\<in>Basis. - real n *\<^sub>R i::'n) (\<Sum>i\<in>Basis. real n *\<^sub>R i)"
  5035   have "Cauchy (\<lambda>n. integral (?cube n) ?F)"
  5036     unfolding Cauchy_def
  5037   proof (intro allI impI)
  5038     fix e::real
  5039     assume "e > 0"
  5040     with rhs obtain B where "0 < B" 
  5041       and B: "\<And>a b c d. ball 0 B \<subseteq> cbox a b \<and> ball 0 B \<subseteq> cbox c d 
  5042                         \<Longrightarrow> norm (integral (cbox a b) ?F - integral (cbox c d) ?F) < e"
  5043       by blast
  5044     obtain N where N: "B \<le> real N"
  5045       using real_arch_simple by blast
  5046     have "ball 0 B \<subseteq> ?cube n" if n: "n \<ge> N" for n
  5047     proof -
  5048       have "sum ((*\<^sub>R) (- real n)) Basis \<bullet> i \<le> x \<bullet> i \<and>
  5049             x \<bullet> i \<le> sum ((*\<^sub>R) (real n)) Basis \<bullet> i"
  5050         if "norm x < B" "i \<in> Basis" for x i::'n
  5051           using Basis_le_norm[of i x] n N that by (auto simp add: field_simps sum_negf)
  5052       then show ?thesis
  5053         by (auto simp: mem_box dist_norm)
  5054     qed
  5055     then show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (integral (?cube m) ?F) (integral (?cube n) ?F) < e"
  5056       by (fastforce simp add: dist_norm intro!: B)
  5057   qed
  5058   then obtain i where i: "(\<lambda>n. integral (?cube n) ?F) \<longlonglongrightarrow> i"
  5059     using convergent_eq_Cauchy by blast
  5060   have "\<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow> norm (integral (cbox a b) ?F - i) < e"
  5061     if "e > 0" for e
  5062   proof -
  5063     have *: "e/2 > 0" using that by auto
  5064     then obtain N where N: "\<And>n. N \<le> n \<Longrightarrow> norm (i - integral (?cube n) ?F) < e/2"
  5065       using i[THEN LIMSEQ_D, simplified norm_minus_commute] by meson
  5066     obtain B where "0 < B" 
  5067       and B: "\<And>a b c d. \<lbrakk>ball 0 B \<subseteq> cbox a b; ball 0 B \<subseteq> cbox c d\<rbrakk> \<Longrightarrow>
  5068                   norm (integral (cbox a b) ?F - integral (cbox c d) ?F) < e/2"
  5069       using rhs * by meson
  5070     let ?B = "max (real N) B"
  5071     show ?thesis
  5072     proof (intro exI conjI allI impI)
  5073       show "0 < ?B"
  5074         using \<open>B > 0\<close> by auto
  5075       fix a b :: 'n
  5076       assume "ball 0 ?B \<subseteq> cbox a b"
  5077       moreover obtain n where n: "max (real N) B \<le> real n"
  5078         using real_arch_simple by blast
  5079       moreover have "ball 0 B \<subseteq> ?cube n"
  5080       proof 
  5081         fix x :: 'n
  5082         assume x: "x \<in> ball 0 B"
  5083         have "\<lbrakk>norm (0 - x) < B; i \<in> Basis\<rbrakk>
  5084               \<Longrightarrow> sum ((*\<^sub>R) (-n)) Basis \<bullet> i\<le> x \<bullet> i \<and> x \<bullet> i \<le> sum ((*\<^sub>R) n) Basis \<bullet> i" for i
  5085           using Basis_le_norm[of i x] n by (auto simp add: field_simps sum_negf)
  5086         then show "x \<in> ?cube n"
  5087           using x by (auto simp: mem_box dist_norm)
  5088       qed 
  5089       ultimately show "norm (integral (cbox a b) ?F - i) < e"
  5090         using norm_triangle_half_l [OF B N] by force
  5091     qed
  5092   qed
  5093   then show ?l unfolding integrable_on_def has_integral_alt'[of f]
  5094     using rhs by blast
  5095 qed
  5096 
  5097 lemma integrable_altD:
  5098   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  5099   assumes "f integrable_on s"
  5100   shows "\<And>a b. (\<lambda>x. if x \<in> s then f x else 0) integrable_on cbox a b"
  5101     and "\<And>e. e > 0 \<Longrightarrow> \<exists>B>0. \<forall>a b c d. ball 0 B \<subseteq> cbox a b \<and> ball 0 B \<subseteq> cbox c d \<longrightarrow>
  5102       norm (integral (cbox a b) (\<lambda>x. if x \<in> s then f x else 0) - integral (cbox c d)  (\<lambda>x. if x \<in> s then f x else 0)) < e"
  5103   using assms[unfolded integrable_alt[of f]] by auto
  5104 
  5105 lemma integrable_alt_subset:
  5106   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
  5107   shows
  5108      "f integrable_on S \<longleftrightarrow>
  5109       (\<forall>a b. (\<lambda>x. if x \<in> S then f x else 0) integrable_on cbox a b) \<and>
  5110       (\<forall>e>0. \<exists>B>0. \<forall>a b c d.
  5111                       ball 0 B \<subseteq> cbox a b \<and> cbox a b \<subseteq> cbox c d
  5112                       \<longrightarrow> norm(integral (cbox a b) (\<lambda>x. if x \<in> S then f x else 0) -
  5113                                integral (cbox c d) (\<lambda>x. if x \<in> S then f x else 0)) < e)"
  5114       (is "_ = ?rhs")
  5115 proof -
  5116   let ?g = "\<lambda>x. if x \<in> S then f x else 0"
  5117   have "f integrable_on S \<longleftrightarrow>
  5118         (\<forall>a b. ?g integrable_on cbox a b) \<and>
  5119         (\<forall>e>0. \<exists>B>0. \<forall>a b c d. ball 0 B \<subseteq> cbox a b \<and> ball 0 B \<subseteq> cbox c d \<longrightarrow>
  5120            norm (integral (cbox a b) ?g - integral (cbox c d)  ?g) < e)"
  5121     by (rule integrable_alt)
  5122   also have "\<dots> = ?rhs"
  5123   proof -
  5124     { fix e :: "real"
  5125       assume e: "\<And>e. e>0 \<Longrightarrow> \<exists>B>0. \<forall>a b c d. ball 0 B \<subseteq> cbox a b \<and> cbox a b \<subseteq> cbox c d \<longrightarrow>
  5126                                    norm (integral (cbox a b) ?g - integral (cbox c d) ?g) < e"
  5127         and "e > 0"
  5128       obtain B where "B > 0"
  5129         and B: "\<And>a b c d. \<lbrakk>ball 0 B \<subseteq> cbox a b; cbox a b \<subseteq> cbox c d\<rbrakk> \<Longrightarrow>
  5130                            norm (integral (cbox a b) ?g - integral (cbox c d) ?g) < e/2"
  5131         using \<open>e > 0\<close> e [of "e/2"] by force
  5132       have "\<exists>B>0. \<forall>a b c d.
  5133                ball 0 B \<subseteq> cbox a b \<and> ball 0 B \<subseteq> cbox c d \<longrightarrow>
  5134                norm (integral (cbox a b) ?g - integral (cbox c d) ?g) < e"
  5135       proof (intro exI allI conjI impI)
  5136         fix a b c d :: "'a"
  5137         let ?\<alpha> = "\<Sum>i\<in>Basis. max (a \<bullet> i) (c \<bullet> i) *\<^sub>R i"
  5138         let ?\<beta> = "\<Sum>i\<in>Basis. min (b \<bullet> i) (d \<bullet> i) *\<^sub>R i"
  5139         show "norm (integral (cbox a b) ?g - integral (cbox c d) ?g) < e"
  5140           if ball: "ball 0 B \<subseteq> cbox a b \<and> ball 0 B \<subseteq> cbox c d"
  5141         proof -
  5142           have B': "norm (integral (cbox a b \<inter> cbox c d) ?g - integral (cbox x y) ?g) < e/2"
  5143             if "cbox a b \<inter> cbox c d \<subseteq> cbox x y" for x y
  5144             using B [of ?\<alpha> ?\<beta> x y] ball that by (simp add: Int_interval [symmetric])
  5145           show ?thesis
  5146             using B' [of a b] B' [of c d] norm_triangle_half_r by blast
  5147         qed
  5148       qed (use \<open>B > 0\<close> in auto)}
  5149     then show ?thesis
  5150       by force
  5151   qed
  5152   finally show ?thesis .
  5153 qed
  5154 
  5155 lemma integrable_on_subcbox:
  5156   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  5157   assumes intf: "f integrable_on S"
  5158     and sub: "cbox a b \<subseteq> S"
  5159   shows "f integrable_on cbox a b"
  5160 proof -
  5161   have "(\<lambda>x. if x \<in> S then f x else 0) integrable_on cbox a b"
  5162     by (simp add: intf integrable_altD(1))
  5163 then show ?thesis
  5164   by (metis (mono_tags) sub integrable_restrict_Int le_inf_iff order_refl subset_antisym)
  5165 qed
  5166 
  5167 
  5168 subsection \<open>A straddling criterion for integrability\<close>
  5169 
  5170 lemma integrable_straddle_interval:
  5171   fixes f :: "'n::euclidean_space \<Rightarrow> real"
  5172   assumes "\<And>e. e>0 \<Longrightarrow> \<exists>g h i j. (g has_integral i) (cbox a b) \<and> (h has_integral j) (cbox a b) \<and>
  5173                             \<bar>i - j\<bar> < e \<and> (\<forall>x\<in>cbox a b. (g x) \<le> f x \<and> f x \<le> h x)"
  5174   shows "f integrable_on cbox a b"
  5175 proof -
  5176   have "\<exists>d. gauge d \<and>
  5177             (\<forall>p1 p2. p1 tagged_division_of cbox a b \<and> d fine p1 \<and>
  5178                      p2 tagged_division_of cbox a b \<and> d fine p2 \<longrightarrow>
  5179                      \<bar>(\<Sum>(x,K)\<in>p1. content K *\<^sub>R f x) - (\<Sum>(x,K)\<in>p2. content K *\<^sub>R f x)\<bar> < e)"
  5180     if "e > 0" for e
  5181   proof -
  5182     have e: "e/3 > 0"
  5183       using that by auto
  5184     then obtain g h i j where ij: "\<bar>i - j\<bar> < e/3"
  5185             and "(g has_integral i) (cbox a b)"
  5186             and "(h has_integral j) (cbox a b)"
  5187             and fgh: "\<And>x. x \<in> cbox a b \<Longrightarrow> g x \<le> f x \<and> f x \<le> h x"
  5188       using assms real_norm_def by metis
  5189     then obtain d1 d2 where "gauge d1" "gauge d2"
  5190             and d1: "\<And>p. \<lbrakk>p tagged_division_of cbox a b; d1 fine p\<rbrakk> \<Longrightarrow>
  5191                           \<bar>(\<Sum>(x,K)\<in>p. content K *\<^sub>R g x) - i\<bar> < e/3"
  5192             and d2: "\<And>p. \<lbrakk>p tagged_division_of cbox a b; d2 fine p\<rbrakk> \<Longrightarrow>
  5193                           \<bar>(\<Sum>(x,K) \<in> p. content K *\<^sub>R h x) - j\<bar> < e/3"
  5194       by (metis e has_integral real_norm_def)
  5195     have "\<bar>(\<Sum>(x,K) \<in> p1. content K *\<^sub>R f x) - (\<Sum>(x,K) \<in> p2. content K *\<^sub>R f x)\<bar> < e"
  5196       if p1: "p1 tagged_division_of cbox a b" and 11: "d1 fine p1" and 21: "d2 fine p1"
  5197        and p2: "p2 tagged_division_of cbox a b" and 12: "d1 fine p2" and 22: "d2 fine p2" for p1 p2
  5198     proof -
  5199       have *: "\<And>g1 g2 h1 h2 f1 f2.
  5200                   \<lbrakk>\<bar>g2 - i\<bar> < e/3; \<bar>g1 - i\<bar> < e/3; \<bar>h2 - j\<bar> < e/3; \<bar>h1 - j\<bar> < e/3;
  5201                    g1 - h2 \<le> f1 - f2; f1 - f2 \<le> h1 - g2\<rbrakk>
  5202                   \<Longrightarrow> \<bar>f1 - f2\<bar> < e"
  5203         using \<open>e > 0\<close> ij by arith
  5204       have 0: "(\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p1. content k *\<^sub>R g x) \<ge> 0"
  5205               "0 \<le> (\<Sum>(x, k)\<in>p2. content k *\<^sub>R h x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)"
  5206               "(\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R g x) \<ge> 0"
  5207               "0 \<le> (\<Sum>(x, k)\<in>p1. content k *\<^sub>R h x) - (\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x)"
  5208         unfolding sum_subtractf[symmetric]
  5209            apply (auto intro!: sum_nonneg)
  5210            apply (meson fgh measure_nonneg mult_left_mono tag_in_interval that sum_nonneg)+
  5211         done
  5212       show ?thesis
  5213       proof (rule *)
  5214         show "\<bar>(\<Sum>(x,K) \<in> p2. content K *\<^sub>R g x) - i\<bar> < e/3"
  5215           by (rule d1[OF p2 12])
  5216         show "\<bar>(\<Sum>(x,K) \<in> p1. content K *\<^sub>R g x) - i\<bar> < e/3"
  5217           by (rule d1[OF p1 11])
  5218         show "\<bar>(\<Sum>(x,K) \<in> p2. content K *\<^sub>R h x) - j\<bar> < e/3"
  5219           by (rule d2[OF p2 22])
  5220         show "\<bar>(\<Sum>(x,K) \<in> p1. content K *\<^sub>R h x) - j\<bar> < e/3"
  5221           by (rule d2[OF p1 21])
  5222       qed (use 0 in auto)
  5223     qed
  5224     then show ?thesis
  5225       by (rule_tac x="\<lambda>x. d1 x \<inter> d2 x" in exI)
  5226         (auto simp: fine_Int intro: \<open>gauge d1\<close> \<open>gauge d2\<close> d1 d2)
  5227   qed
  5228   then show ?thesis
  5229     by (simp add: integrable_Cauchy)
  5230 qed
  5231 
  5232 lemma integrable_straddle:
  5233   fixes f :: "'n::euclidean_space \<Rightarrow> real"
  5234   assumes "\<And>e. e>0 \<Longrightarrow> \<exists>g h i j. (g has_integral i) s \<and> (h has_integral j) s \<and>
  5235                      \<bar>i - j\<bar> < e \<and> (\<forall>x\<in>s. g x \<le> f x \<and> f x \<le> h x)"
  5236   shows "f integrable_on s"
  5237 proof -
  5238   let ?fs = "(\<lambda>x. if x \<in> s then f x else 0)"
  5239   have "?fs integrable_on cbox a b" for a b
  5240   proof (rule integrable_straddle_interval)
  5241     fix e::real
  5242     assume "e > 0"
  5243     then have *: "e/4 > 0"
  5244       by auto
  5245     with assms obtain g h i j where g: "(g has_integral i) s" and h: "(h has_integral j) s"
  5246                  and ij: "\<bar>i - j\<bar> < e/4"
  5247                  and fgh: "\<And>x. x \<in> s \<Longrightarrow> g x \<le> f x \<and> f x \<le> h x"
  5248       by metis
  5249     let ?gs = "(\<lambda>x. if x \<in> s then g x else 0)"
  5250     let ?hs = "(\<lambda>x. if x \<in> s then h x else 0)"
  5251     obtain Bg where Bg: "\<And>a b. ball 0 Bg \<subseteq> cbox a b \<Longrightarrow> \<bar>integral (cbox a b) ?gs - i\<bar> < e/4"
  5252               and int_g: "\<And>a b. ?gs integrable_on cbox a b"
  5253       using g * unfolding has_integral_alt' real_norm_def by meson
  5254     obtain Bh where
  5255           Bh: "\<And>a b. ball 0 Bh \<subseteq> cbox a b \<Longrightarrow> \<bar>integral (cbox a b) ?hs - j\<bar> < e/4"
  5256          and int_h: "\<And>a b. ?hs integrable_on cbox a b"
  5257       using h * unfolding has_integral_alt' real_norm_def by meson
  5258     define c where "c = (\<Sum>i\<in>Basis. min (a\<bullet>i) (- (max Bg Bh)) *\<^sub>R i)"
  5259     define d where "d = (\<Sum>i\<in>Basis. max (b\<bullet>i) (max Bg Bh) *\<^sub>R i)"
  5260     have "\<lbrakk>norm (0 - x) < Bg; i \<in> Basis\<rbrakk> \<Longrightarrow> c \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> d \<bullet> i" for x i
  5261       using Basis_le_norm[of i x] unfolding c_def d_def by auto
  5262     then have ballBg: "ball 0 Bg \<subseteq> cbox c d"
  5263       by (auto simp: mem_box dist_norm)
  5264     have "\<lbrakk>norm (0 - x) < Bh; i \<in> Basis\<rbrakk> \<Longrightarrow> c \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> d \<bullet> i" for x i
  5265       using Basis_le_norm[of i x] unfolding c_def d_def by auto
  5266     then have ballBh: "ball 0 Bh \<subseteq> cbox c d"
  5267       by (auto simp: mem_box dist_norm)
  5268     have ab_cd: "cbox a b \<subseteq> cbox c d"
  5269       by (auto simp: c_def d_def subset_box_imp)
  5270     have **: "\<And>ch cg ag ah::real. \<lbrakk>\<bar>ah - ag\<bar> \<le> \<bar>ch - cg\<bar>; \<bar>cg - i\<bar> < e/4; \<bar>ch - j\<bar> < e/4\<rbrakk>
  5271        \<Longrightarrow> \<bar>ag - ah\<bar> < e"
  5272       using ij by arith
  5273     show "\<exists>g h i j. (g has_integral i) (cbox a b) \<and> (h has_integral j) (cbox a b) \<and> \<bar>i - j\<bar> < e \<and>
  5274           (\<forall>x\<in>cbox a b. g x \<le> (if x \<in> s then f x else 0) \<and>
  5275                         (if x \<in> s then f x else 0) \<le> h x)"
  5276     proof (intro exI ballI conjI)
  5277       have eq: "\<And>x f g. (if x \<in> s then f x else 0) - (if x \<in> s then g x else 0) =
  5278                        (if x \<in> s then f x - g x else (0::real))"
  5279         by auto
  5280       have int_hg: "(\<lambda>x. if x \<in> s then h x - g x else 0) integrable_on cbox a b"
  5281                    "(\<lambda>x. if x \<in> s then h x - g x else 0) integrable_on cbox c d"
  5282         by (metis (no_types) integrable_diff g h has_integral_integrable integrable_altD(1))+
  5283       show "(?gs has_integral integral (cbox a b) ?gs) (cbox a b)"
  5284            "(?hs has_integral integral (cbox a b) ?hs) (cbox a b)"
  5285         by (intro integrable_integral int_g int_h)+
  5286       then have "integral (cbox a b) ?gs \<le> integral (cbox a b) ?hs"
  5287         apply (rule has_integral_le)
  5288         using fgh by force
  5289       then have "0 \<le> integral (cbox a b) ?hs - integral (cbox a b) ?gs"
  5290         by simp
  5291       then have "\<bar>integral (cbox a b) ?hs - integral (cbox a b) ?gs\<bar>
  5292               \<le> \<bar>integral (cbox c d) ?hs - integral (cbox c d) ?gs\<bar>"
  5293         apply (simp add: integral_diff [symmetric] int_g int_h)
  5294         apply (subst abs_of_nonneg[OF integral_nonneg[OF integrable_diff, OF int_h int_g]])
  5295         using fgh apply (force simp: eq intro!: integral_subset_le [OF ab_cd int_hg])+
  5296         done
  5297       then show "\<bar>integral (cbox a b) ?gs - integral (cbox a b) ?hs\<bar> < e"
  5298         apply (rule **)
  5299          apply (rule Bg ballBg Bh ballBh)+
  5300         done
  5301       show "\<And>x. x \<in> cbox a b \<Longrightarrow> ?gs x \<le> ?fs x" "\<And>x. x \<in> cbox a b \<Longrightarrow> ?fs x \<le> ?hs x"
  5302         using fgh by auto
  5303     qed
  5304   qed
  5305   then have int_f: "?fs integrable_on cbox a b" for a b
  5306     by simp
  5307   have "\<exists>B>0. \<forall>a b c d.
  5308                   ball 0 B \<subseteq> cbox a b \<and> ball 0 B \<subseteq> cbox c d \<longrightarrow>
  5309                   abs (integral (cbox a b) ?fs - integral (cbox c d) ?fs) < e"
  5310       if "0 < e" for e
  5311   proof -
  5312     have *: "e/3 > 0"
  5313       using that by auto
  5314     with assms obtain g h i j where g: "(g has_integral i) s" and h: "(h has_integral j) s"
  5315                  and ij: "\<bar>i - j\<bar> < e/3"
  5316                  and fgh: "\<And>x. x \<in> s \<Longrightarrow> g x \<le> f x \<and> f x \<le> h x"
  5317       by metis
  5318     let ?gs = "(\<lambda>x. if x \<in> s then g x else 0)"
  5319     let ?hs = "(\<lambda>x. if x \<in> s then h x else 0)"
  5320     obtain Bg where "Bg > 0"
  5321               and Bg: "\<And>a b. ball 0 Bg \<subseteq> cbox a b \<Longrightarrow> \<bar>integral (cbox a b) ?gs - i\<bar> < e/3"
  5322               and int_g: "\<And>a b. ?gs integrable_on cbox a b"
  5323       using g * unfolding has_integral_alt' real_norm_def by meson
  5324     obtain Bh where "Bh > 0"
  5325               and Bh: "\<And>a b. ball 0 Bh \<subseteq> cbox a b \<Longrightarrow> \<bar>integral (cbox a b) ?hs - j\<bar> < e/3"
  5326               and int_h: "\<And>a b. ?hs integrable_on cbox a b"
  5327       using h * unfolding has_integral_alt' real_norm_def by meson
  5328     { fix a b c d :: 'n
  5329       assume as: "ball 0 (max Bg Bh) \<subseteq> cbox a b" "ball 0 (max Bg Bh) \<subseteq> cbox c d"
  5330       have **: "ball 0 Bg \<subseteq> ball (0::'n) (max Bg Bh)" "ball 0 Bh \<subseteq> ball (0::'n) (max Bg Bh)"
  5331         by auto
  5332       have *: "\<And>ga gc ha hc fa fc. \<lbrakk>\<bar>ga - i\<bar> < e/3; \<bar>gc - i\<bar> < e/3; \<bar>ha - j\<bar> < e/3;
  5333                      \<bar>hc - j\<bar> < e/3; ga \<le> fa; fa \<le> ha; gc \<le> fc; fc \<le> hc\<rbrakk> \<Longrightarrow>
  5334         \<bar>fa - fc\<bar> < e"
  5335         using ij by arith
  5336       have "abs (integral (cbox a b) (\<lambda>x. if x \<in> s then f x else 0) - integral (cbox c d)
  5337         (\<lambda>x. if x \<in> s then f x else 0)) < e"
  5338       proof (rule *)
  5339         show "\<bar>integral (cbox a b) ?gs - i\<bar> < e/3"
  5340           using "**" Bg as by blast
  5341         show "\<bar>integral (cbox c d) ?gs - i\<bar> < e/3"
  5342           using "**" Bg as by blast
  5343         show "\<bar>integral (cbox a b) ?hs - j\<bar> < e/3"
  5344           using "**" Bh as by blast
  5345         show "\<bar>integral (cbox c d) ?hs - j\<bar> < e/3"
  5346           using "**" Bh as by blast
  5347       qed (use int_f int_g int_h fgh in \<open>simp_all add: integral_le\<close>)
  5348     }
  5349     then show ?thesis
  5350       apply (rule_tac x="max Bg Bh" in exI)
  5351         using \<open>Bg > 0\<close> by auto
  5352   qed
  5353   then show ?thesis
  5354     unfolding integrable_alt[of f] real_norm_def by (blast intro: int_f)
  5355 qed
  5356 
  5357 
  5358 subsection \<open>Adding integrals over several sets\<close>
  5359 
  5360 lemma has_integral_Un:
  5361   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  5362   assumes f: "(f has_integral i) S" "(f has_integral j) T"
  5363     and neg: "negligible (S \<inter> T)"
  5364   shows "(f has_integral (i + j)) (S \<union> T)"
  5365   unfolding has_integral_restrict_UNIV[symmetric, of f]
  5366 proof (rule has_integral_spike[OF neg])
  5367   let ?f = "\<lambda>x. (if x \<in> S then f x else 0) + (if x \<in> T then f x else 0)"
  5368   show "(?f has_integral i + j) UNIV"
  5369     by (simp add: f has_integral_add)
  5370 qed auto
  5371 
  5372 lemma integral_Un [simp]:
  5373   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  5374   assumes "f integrable_on S" "f integrable_on T" "negligible (S \<inter> T)"
  5375   shows "integral (S \<union> T) f = integral S f + integral T f"
  5376   by (simp add: has_integral_Un assms integrable_integral integral_unique)
  5377 
  5378 lemma integrable_Un:
  5379   fixes f :: "'a::euclidean_space \<Rightarrow> 'b :: banach"
  5380   assumes "negligible (A \<inter> B)" "f integrable_on A" "f integrable_on B"
  5381   shows   "f integrable_on (A \<union> B)"
  5382 proof -
  5383   from assms obtain y z where "(f has_integral y) A" "(f has_integral z) B"
  5384      by (auto simp: integrable_on_def)
  5385   from has_integral_Un[OF this assms(1)] show ?thesis by (auto simp: integrable_on_def)
  5386 qed
  5387 
  5388 lemma integrable_Un':
  5389   fixes f :: "'a::euclidean_space \<Rightarrow> 'b :: banach"
  5390   assumes "f integrable_on A" "f integrable_on B" "negligible (A \<inter> B)" "C = A \<union> B"
  5391   shows   "f integrable_on C"
  5392   using integrable_Un[of A B f] assms by simp
  5393 
  5394 lemma has_integral_Union:
  5395   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  5396   assumes \<T>: "finite \<T>"
  5397     and int: "\<And>S. S \<in> \<T> \<Longrightarrow> (f has_integral (i S)) S"
  5398     and neg: "pairwise (\<lambda>S S'. negligible (S \<inter> S')) \<T>"
  5399   shows "(f has_integral (sum i \<T>)) (\<Union>\<T>)"
  5400 proof -
  5401   let ?\<U> = "((\<lambda>(a,b). a \<inter> b) ` {(a,b). a \<in> \<T> \<and> b \<in> {y. y \<in> \<T> \<and> a \<noteq> y}})"
  5402   have "((\<lambda>x. if x \<in> \<Union>\<T> then f x else 0) has_integral sum i \<T>) UNIV"
  5403   proof (rule has_integral_spike)
  5404     show "negligible (\<Union>?\<U>)"
  5405     proof (rule negligible_Union)
  5406       have "finite (\<T> \<times> \<T>)"
  5407         by (simp add: \<T>)
  5408       moreover have "{(a, b). a \<in> \<T> \<and> b \<in> {y \<in> \<T>. a \<noteq> y}} \<subseteq> \<T> \<times> \<T>"
  5409         by auto
  5410       ultimately show "finite ?\<U>"
  5411         by (blast intro: finite_subset[of _ "\<T> \<times> \<T>"])
  5412       show "\<And>t. t \<in> ?\<U> \<Longrightarrow> negligible t"
  5413         using neg unfolding pairwise_def by auto
  5414     qed
  5415   next
  5416     show "(if x \<in> \<Union>\<T> then f x else 0) = (\<Sum>A\<in>\<T>. if x \<in> A then f x else 0)"
  5417       if "x \<in> UNIV - (\<Union>?\<U>)" for x
  5418     proof clarsimp
  5419       fix S assume "S \<in> \<T>" "x \<in> S"
  5420       moreover then have "\<forall>b\<in>\<T>. x \<in> b \<longleftrightarrow> b = S"
  5421         using that by blast
  5422       ultimately show "f x = (\<Sum>A\<in>\<T>. if x \<in> A then f x else 0)"
  5423         by (simp add: sum.delta[OF \<T>])
  5424     qed
  5425   next
  5426     show "((\<lambda>x. \<Sum>A\<in>\<T>. if x \<in> A then f x else 0) has_integral (\<Sum>A\<in>\<T>. i A)) UNIV"
  5427       apply (rule has_integral_sum [OF \<T>])
  5428       using int by (simp add: has_integral_restrict_UNIV)
  5429   qed
  5430   then show ?thesis
  5431     using has_integral_restrict_UNIV by blast
  5432 qed
  5433 
  5434 
  5435 text \<open>In particular adding integrals over a division, maybe not of an interval.\<close>
  5436 
  5437 lemma has_integral_combine_division:
  5438   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  5439   assumes "\<D> division_of S"
  5440     and "\<And>k. k \<in> \<D> \<Longrightarrow> (f has_integral (i k)) k"
  5441   shows "(f has_integral (sum i \<D>)) S"
  5442 proof -
  5443   note \<D> = division_ofD[OF assms(1)]
  5444   have neg: "negligible (S \<inter> s')" if "S \<in> \<D>" "s' \<in> \<D>" "S \<noteq> s'" for S s'
  5445   proof -
  5446     obtain a c b \<D> where obt: "S = cbox a b" "s' = cbox c \<D>"
  5447       by (meson \<open>S \<in> \<D>\<close> \<open>s' \<in> \<D>\<close> \<D>(4))
  5448     from \<D>(5)[OF that] show ?thesis
  5449       unfolding obt interior_cbox
  5450       by (metis (no_types, lifting) Diff_empty Int_interval box_Int_box negligible_frontier_interval)
  5451   qed
  5452   show ?thesis
  5453     unfolding \<D>(6)[symmetric]
  5454     by (auto intro: \<D> neg assms has_integral_Union pairwiseI)
  5455 qed
  5456 
  5457 lemma integral_combine_division_bottomup:
  5458   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  5459   assumes "\<D> division_of S" "\<And>k. k \<in> \<D> \<Longrightarrow> f integrable_on k"
  5460   shows "integral S f = sum (\<lambda>i. integral i f) \<D>"
  5461   apply (rule integral_unique)
  5462   by (meson assms has_integral_combine_division has_integral_integrable_integral)
  5463 
  5464 lemma has_integral_combine_division_topdown:
  5465   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  5466   assumes f: "f integrable_on S"
  5467     and \<D>: "\<D> division_of K"
  5468     and "K \<subseteq> S"
  5469   shows "(f has_integral (sum (\<lambda>i. integral i f) \<D>)) K"
  5470 proof -
  5471   have "f integrable_on L" if "L \<in> \<D>" for L
  5472   proof -
  5473     have "L \<subseteq> S"
  5474       using \<open>K \<subseteq> S\<close> \<D> that by blast
  5475     then show "f integrable_on L"
  5476       using that by (metis (no_types) f \<D> division_ofD(4) integrable_on_subcbox)
  5477   qed
  5478   then show ?thesis
  5479     by (meson \<D> has_integral_combine_division has_integral_integrable_integral)
  5480 qed
  5481 
  5482 lemma integral_combine_division_topdown:
  5483   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  5484   assumes "f integrable_on S"
  5485     and "\<D> division_of S"
  5486   shows "integral S f = sum (\<lambda>i. integral i f) \<D>"
  5487   apply (rule integral_unique)
  5488   apply (rule has_integral_combine_division_topdown)
  5489   using assms
  5490   apply auto
  5491   done
  5492 
  5493 lemma integrable_combine_division:
  5494   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  5495   assumes \<D>: "\<D> division_of S"
  5496     and f: "\<And>i. i \<in> \<D> \<Longrightarrow> f integrable_on i"
  5497   shows "f integrable_on S"
  5498   using f unfolding integrable_on_def by (metis has_integral_combine_division[OF \<D>])
  5499 
  5500 lemma integrable_on_subdivision:
  5501   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  5502   assumes \<D>: "\<D> division_of i"
  5503     and f: "f integrable_on S"
  5504     and "i \<subseteq> S"
  5505   shows "f integrable_on i"
  5506 proof -
  5507   have "f integrable_on i" if "i \<in> \<D>" for i
  5508 proof -
  5509   have "i \<subseteq> S"
  5510     using assms that by auto
  5511   then show "f integrable_on i"
  5512     using that by (metis (no_types) \<D> f division_ofD(4) integrable_on_subcbox)
  5513 qed
  5514   then show ?thesis
  5515     using \<D> integrable_combine_division by blast
  5516 qed
  5517 
  5518 
  5519 subsection \<open>Also tagged divisions\<close>
  5520 
  5521 lemma has_integral_combine_tagged_division:
  5522   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  5523   assumes "p tagged_division_of S"
  5524     and "\<forall>(x,k) \<in> p. (f has_integral (i k)) k"
  5525   shows "(f has_integral (\<Sum>(x,k)\<in>p. i k)) S"
  5526 proof -
  5527   have *: "(f has_integral (\<Sum>k\<in>snd`p. integral k f)) S"
  5528     using assms(2)
  5529     apply (intro has_integral_combine_division)
  5530     apply (auto simp: has_integral_integral[symmetric] intro: division_of_tagged_division[OF assms(1)])
  5531     apply auto
  5532     done
  5533   also have "(\<Sum>k\<in>snd`p. integral k f) = (\<Sum>(x, k)\<in>p. integral k f)"
  5534     by (intro sum.over_tagged_division_lemma[OF assms(1), symmetric] integral_null)
  5535        (simp add: content_eq_0_interior)
  5536   finally show ?thesis
  5537     using assms by (auto simp add: has_integral_iff intro!: sum.cong)
  5538 qed
  5539 
  5540 lemma integral_combine_tagged_division_bottomup:
  5541   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  5542   assumes "p tagged_division_of (cbox a b)"
  5543     and "\<forall>(x,k)\<in>p. f integrable_on k"
  5544   shows "integral (cbox a b) f = sum (\<lambda>(x,k). integral k f) p"
  5545   apply (rule integral_unique)
  5546   apply (rule has_integral_combine_tagged_division[OF assms(1)])
  5547   using assms(2)
  5548   apply auto
  5549   done
  5550 
  5551 lemma has_integral_combine_tagged_division_topdown:
  5552   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  5553   assumes f: "f integrable_on cbox a b"
  5554     and p: "p tagged_division_of (cbox a b)"
  5555   shows "(f has_integral (sum (\<lambda>(x,K). integral K f) p)) (cbox a b)"
  5556 proof -
  5557   have "(f has_integral integral K f) K" if "(x,K) \<in> p" for x K
  5558     by (metis assms integrable_integral integrable_on_subcbox tagged_division_ofD(3,4) that)
  5559   then show ?thesis
  5560     by (metis assms case_prodI2 has_integral_integrable_integral integral_combine_tagged_division_bottomup)
  5561 qed
  5562 
  5563 lemma integral_combine_tagged_division_topdown:
  5564   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  5565   assumes "f integrable_on cbox a b"
  5566     and "p tagged_division_of (cbox a b)"
  5567   shows "integral (cbox a b) f = sum (\<lambda>(x,k). integral k f) p"
  5568   apply (rule integral_unique [OF has_integral_combine_tagged_division_topdown])
  5569   using assms apply auto
  5570   done
  5571 
  5572 
  5573 subsection \<open>Henstock's lemma\<close>
  5574 
  5575 lemma Henstock_lemma_part1:
  5576   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  5577   assumes intf: "f integrable_on cbox a b"
  5578     and "e > 0"
  5579     and "gauge d"
  5580     and less_e: "\<And>p. \<lbrakk>p tagged_division_of (cbox a b); d fine p\<rbrakk> \<Longrightarrow>
  5581                      norm (sum (\<lambda>(x,K). content K *\<^sub>R f x) p - integral(cbox a b) f) < e"
  5582     and p: "p tagged_partial_division_of (cbox a b)" "d fine p"
  5583   shows "norm (sum (\<lambda>(x,K). content K *\<^sub>R f x - integral K f) p) \<le> e" (is "?lhs \<le> e")
  5584 proof (rule field_le_epsilon)
  5585   fix k :: real
  5586   assume "k > 0"
  5587   let ?SUM = "\<lambda>p. (\<Sum>(x,K) \<in> p. content K *\<^sub>R f x)"
  5588   note p' = tagged_partial_division_ofD[OF p(1)]
  5589   have "\<Union>(snd ` p) \<subseteq> cbox a b"
  5590     using p'(3) by fastforce
  5591   then obtain q where q: "snd ` p \<subseteq> q" and qdiv: "q division_of cbox a b"
  5592     by (meson p(1) partial_division_extend_interval partial_division_of_tagged_division)
  5593   note q' = division_ofD[OF qdiv]
  5594   define r where "r = q - snd ` p"
  5595   have "snd ` p \<inter> r = {}"
  5596     unfolding r_def by auto
  5597   have "finite r"
  5598     using q' unfolding r_def by auto
  5599   have "\<exists>p. p tagged_division_of i \<and> d fine p \<and>
  5600         norm (?SUM p - integral i f) < k / (real (card r) + 1)"
  5601     if "i\<in>r" for i
  5602   proof -
  5603     have gt0: "k / (real (card r) + 1) > 0" using \<open>k > 0\<close> by simp
  5604     have i: "i \<in> q"
  5605       using that unfolding r_def by auto
  5606     then obtain u v where uv: "i = cbox u v"
  5607       using q'(4) by blast
  5608     then have "cbox u v \<subseteq> cbox a b"
  5609       using i q'(2) by auto  
  5610     then have "f integrable_on cbox u v"
  5611       by (rule integrable_subinterval[OF intf])
  5612     with integrable_integral[OF this, unfolded has_integral[of f]]
  5613     obtain dd where "gauge dd" and dd:
  5614       "\<And>\<D>. \<lbrakk>\<D> tagged_division_of cbox u v; dd fine \<D>\<rbrakk> \<Longrightarrow>
  5615     norm (?SUM \<D> - integral (cbox u v) f) < k / (real (card r) + 1)"
  5616       using gt0 by auto
  5617     with gauge_Int[OF \<open>gauge d\<close> \<open>gauge dd\<close>]
  5618     obtain qq where qq: "qq tagged_division_of cbox u v" "(\<lambda>x. d x \<inter> dd x) fine qq"
  5619       using fine_division_exists by blast
  5620     with dd[of qq]  show ?thesis
  5621       by (auto simp: fine_Int uv)
  5622   qed
  5623   then obtain qq where qq: "\<And>i. i \<in> r \<Longrightarrow> qq i tagged_division_of i \<and>
  5624       d fine qq i \<and> norm (?SUM (qq i) - integral i f) < k / (real (card r) + 1)"
  5625     by metis
  5626 
  5627   let ?p = "p \<union> \<Union>(qq ` r)"
  5628   have "norm (?SUM ?p - integral (cbox a b) f) < e"
  5629   proof (rule less_e)
  5630     show "d fine ?p"
  5631       by (metis (mono_tags, hide_lams) qq fine_Un fine_Union imageE p(2))
  5632     note ptag = tagged_partial_division_of_Union_self[OF p(1)]
  5633     have "p \<union> \<Union>(qq ` r) tagged_division_of \<Union>(snd ` p) \<union> \<Union>r"
  5634     proof (rule tagged_division_Un[OF ptag tagged_division_Union [OF \<open>finite r\<close>]])
  5635       show "\<And>i. i \<in> r \<Longrightarrow> qq i tagged_division_of i"
  5636         using qq by auto
  5637       show "\<And>i1 i2. \<lbrakk>i1 \<in> r; i2 \<in> r; i1 \<noteq> i2\<rbrakk> \<Longrightarrow> interior i1 \<inter> interior i2 = {}"
  5638         by (simp add: q'(5) r_def)
  5639       show "interior (\<Union>(snd ` p)) \<inter> interior (\<Union>r) = {}"
  5640       proof (rule Int_interior_Union_intervals [OF \<open>finite r\<close>])
  5641         show "open (interior (\<Union>(snd ` p)))"
  5642           by blast
  5643         show "\<And>T. T \<in> r \<Longrightarrow> \<exists>a b. T = cbox a b"
  5644           by (simp add: q'(4) r_def)
  5645         have "finite (snd ` p)"
  5646           by (simp add: p'(1))
  5647         then show "\<And>T. T \<in> r \<Longrightarrow> interior (\<Union>(snd ` p)) \<inter> interior T = {}"
  5648           apply (subst Int_commute)
  5649           apply (rule Int_interior_Union_intervals)
  5650           using r_def q'(5) q(1) apply auto
  5651           by (simp add: p'(4))
  5652       qed
  5653     qed
  5654     moreover have "\<Union>(snd ` p) \<union> \<Union>r = cbox a b" and "{qq i |i. i \<in> r} = qq ` r"
  5655       using qdiv q unfolding Union_Un_distrib[symmetric] r_def by auto
  5656     ultimately show "?p tagged_division_of (cbox a b)"
  5657       by fastforce
  5658   qed
  5659   then have "norm (?SUM p + (?SUM (\<Union>(qq ` r))) - integral (cbox a b) f) < e"
  5660   proof (subst sum.union_inter_neutral[symmetric, OF \<open>finite p\<close>], safe)
  5661     show "content L *\<^sub>R f x = 0" if "(x, L) \<in> p" "(x, L) \<in> qq K" "K \<in> r" for x K L 
  5662     proof -
  5663       obtain u v where uv: "L = cbox u v"
  5664         using \<open>(x,L) \<in> p\<close> p'(4) by blast
  5665       have "L \<subseteq> K"
  5666         using  qq[OF that(3)] tagged_division_ofD(3) \<open>(x,L) \<in> qq K\<close> by metis
  5667       have "L \<in> snd ` p" 
  5668         using \<open>(x,L) \<in> p\<close> image_iff by fastforce 
  5669       then have "L \<in> q" "K \<in> q" "L \<noteq> K"
  5670         using that(1,3) q(1) unfolding r_def by auto
  5671       with q'(5) have "interior L = {}"
  5672         using interior_mono[OF \<open>L \<subseteq> K\<close>] by blast
  5673       then show "content L *\<^sub>R f x = 0"
  5674         unfolding uv content_eq_0_interior[symmetric] by auto
  5675     qed
  5676     show "finite (\<Union>(qq ` r))"
  5677       by (meson finite_UN qq \<open>finite r\<close> tagged_division_of_finite)
  5678   qed
  5679   moreover have "content M *\<^sub>R f x = 0" 
  5680       if x: "(x,M) \<in> qq K" "(x,M) \<in> qq L" and KL: "qq K \<noteq> qq L" and r: "K \<in> r" "L \<in> r"
  5681     for x M K L
  5682   proof -
  5683     note kl = tagged_division_ofD(3,4)[OF qq[THEN conjunct1]]
  5684     obtain u v where uv: "M = cbox u v"
  5685       using \<open>(x, M) \<in> qq L\<close> \<open>L \<in> r\<close> kl(2) by blast
  5686     have empty: "interior (K \<inter> L) = {}"
  5687       by (metis DiffD1 interior_Int q'(5) r_def KL r)
  5688     have "interior M = {}"
  5689       by (metis (no_types, lifting) Int_assoc empty inf.absorb_iff2 interior_Int kl(1) subset_empty x r)
  5690     then show "content M *\<^sub>R f x = 0"
  5691       unfolding uv content_eq_0_interior[symmetric]
  5692       by auto
  5693   qed 
  5694   ultimately have "norm (?SUM p + sum ?SUM (qq ` r) - integral (cbox a b) f) < e"
  5695     apply (subst (asm) sum.Union_comp)
  5696     using qq by (force simp: split_paired_all)+
  5697   moreover have "content M *\<^sub>R f x = 0" 
  5698        if "K \<in