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