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