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