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