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