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