Theory Henstock_Kurzweil_Integration

(*  Author:     John Harrison
    Author:     Robert Himmelmann, TU Muenchen (Translation from HOL light)
                Huge cleanup by LCP
*)

section ‹Henstock-Kurzweil Gauge Integration in Many Dimensions›

theory Henstock_Kurzweil_Integration
imports
  Lebesgue_Measure Tagged_Division
begin

lemma norm_diff2: "y = y1 + y2; x = x1 + x2; e = e1 + e2; norm(y1 - x1)  e1; norm(y2 - x2)  e2
   norm(y-x)  e"
  using norm_triangle_mono [of "y1 - x1" "e1" "y2 - x2" "e2"]
  by (simp add: add_diff_add)

lemma setcomp_dot1: "{z. P (z  (i,0))} = {(x,y). P(x  i)}"
  by auto

lemma setcomp_dot2: "{z. P (z  (0,i))} = {(x,y). P(y  i)}"
  by auto

lemma Sigma_Int_Paircomp1: "(Sigma A B)  {(x, y). P x} = Sigma (A  {x. P x}) B"
  by blast

lemma Sigma_Int_Paircomp2: "(Sigma A B)  {(x, y). P y} = Sigma A (λz. B z  {y. P y})"
  by blast
(* END MOVE *)

subsection ‹Content (length, area, volume...) of an interval›

abbreviation content :: "'a::euclidean_space set  real"
  where "content s  measure lborel s"

lemma content_cbox_cases:
  "content (cbox a b) = (if iBasis. ai  bi then prod (λi. bi - ai) Basis else 0)"
  by (simp add: measure_lborel_cbox_eq inner_diff)

lemma content_cbox: "iBasis. ai  bi  content (cbox a b) = (iBasis. bi - ai)"
  unfolding content_cbox_cases by simp

lemma content_cbox': "cbox a b  {}  content (cbox a b) = (iBasis. bi - ai)"
  by (simp add: box_ne_empty inner_diff)

lemma content_cbox_if: "content (cbox a b) = (if cbox a b = {} then 0 else iBasis. bi - ai)"
  by (simp add: content_cbox')

lemma content_cbox_cart:
   "cbox a b  {}  content(cbox a b) = prod (λi. b$i - a$i) UNIV"
  by (simp add: content_cbox_if Basis_vec_def cart_eq_inner_axis axis_eq_axis prod.UNION_disjoint)

lemma content_cbox_if_cart:
   "content(cbox a b) = (if cbox a b = {} then 0 else prod (λi. b$i - a$i) UNIV)"
  by (simp add: content_cbox_cart)

lemma content_division_of:
  assumes "K  𝒟" "𝒟 division_of S"
  shows "content K = (i  Basis. interval_upperbound K  i - interval_lowerbound K  i)"
proof -
  obtain a b where "K = cbox a b"
    using cbox_division_memE assms by metis
  then show ?thesis
    using assms by (force simp: division_of_def content_cbox')
qed

lemma content_real: "a  b  content {a..b} = b - a"
  by simp

lemma abs_eq_content: "¦y - x¦ = (if xy then content {x..y} else content {y..x})"
  by (auto simp: content_real)

lemma content_singleton: "content {a} = 0"
  by simp

lemma content_unit[iff]: "content (cbox 0 (One::'a::euclidean_space)) = 1"
  by simp

lemma content_pos_le [iff]: "0  content X"
  by simp

corollary‹tag unimportant› content_nonneg [simp]: "¬ content (cbox a b) < 0"
  using not_le by blast

lemma content_pos_lt: "iBasis. ai < bi  0 < content (cbox a b)"
  by (auto simp: less_imp_le inner_diff box_eq_empty intro!: prod_pos)

lemma content_eq_0: "content (cbox a b) = 0  (iBasis. bi  ai)"
  by (auto simp: content_cbox_cases not_le intro: less_imp_le antisym eq_refl)

lemma content_eq_0_interior: "content (cbox a b) = 0  interior(cbox a b) = {}"
  unfolding content_eq_0 interior_cbox box_eq_empty by auto

lemma content_pos_lt_eq: "0 < content (cbox a (b::'a::euclidean_space))  (iBasis. ai < bi)"
  by (auto simp add: content_cbox_cases less_le prod_nonneg)

lemma content_empty [simp]: "content {} = 0"
  by simp

lemma content_real_if [simp]: "content {a..b} = (if a  b then b - a else 0)"
  by (simp add: content_real)

lemma content_subset: "cbox a b  cbox c d  content (cbox a b)  content (cbox c d)"
  unfolding measure_def
  by (intro enn2real_mono emeasure_mono) (auto simp: emeasure_lborel_cbox_eq)

lemma content_lt_nz: "0 < content (cbox a b)  content (cbox a b)  0"
  unfolding content_pos_lt_eq content_eq_0 unfolding not_ex not_le by fastforce

lemma content_Pair: "content (cbox (a,c) (b,d)) = content (cbox a b) * content (cbox c d)"
  unfolding measure_lborel_cbox_eq Basis_prod_def
  apply (subst prod.union_disjoint)
  apply (auto simp: bex_Un ball_Un)
  apply (subst (1 2) prod.reindex_nontrivial)
  apply auto
  done

lemma content_cbox_pair_eq0_D:
   "content (cbox (a,c) (b,d)) = 0  content (cbox a b) = 0  content (cbox c d) = 0"
  by (simp add: content_Pair)

lemma content_cbox_plus:
  fixes x :: "'a::euclidean_space"
  shows "content(cbox x (x + h *R One)) = (if h  0 then h ^ DIM('a) else 0)"
  by (simp add: algebra_simps content_cbox_if box_eq_empty)

lemma content_0_subset: "content(cbox a b) = 0  s  cbox a b  content s = 0"
  using emeasure_mono[of s "cbox a b" lborel]
  by (auto simp: measure_def enn2real_eq_0_iff emeasure_lborel_cbox_eq)

lemma content_ball_pos:
  assumes "r > 0"
  shows   "content (ball c r) > 0"
proof -
  from rational_boxes[OF assms, of c] obtain a b where ab: "c  box a b" "box a b  ball c r"
    by auto
  from ab have "0 < content (box a b)"
    by (subst measure_lborel_box_eq) (auto intro!: prod_pos simp: algebra_simps box_def)
  have "emeasure lborel (box a b)  emeasure lborel (ball c r)"
    using ab by (intro emeasure_mono) auto
  also have "emeasure lborel (box a b) = ennreal (content (box a b))"
    using emeasure_lborel_box_finite[of a b] by (intro emeasure_eq_ennreal_measure) auto
  also have "emeasure lborel (ball c r) = ennreal (content (ball c r))"
    using emeasure_lborel_ball_finite[of c r] by (intro emeasure_eq_ennreal_measure) auto
  finally show ?thesis
    using ‹content (box a b) > 0 by simp
qed

lemma content_cball_pos:
  assumes "r > 0"
  shows   "content (cball c r) > 0"
proof -
  from rational_boxes[OF assms, of c] obtain a b where ab: "c  box a b" "box a b  ball c r"
    by auto
  from ab have "0 < content (box a b)"
    by (subst measure_lborel_box_eq) (auto intro!: prod_pos simp: algebra_simps box_def)
  have "emeasure lborel (box a b)  emeasure lborel (ball c r)"
    using ab by (intro emeasure_mono) auto
  also have "  emeasure lborel (cball c r)"
    by (intro emeasure_mono) auto
  also have "emeasure lborel (box a b) = ennreal (content (box a b))"
    using emeasure_lborel_box_finite[of a b] by (intro emeasure_eq_ennreal_measure) auto
  also have "emeasure lborel (cball c r) = ennreal (content (cball c r))"
    using emeasure_lborel_cball_finite[of c r] by (intro emeasure_eq_ennreal_measure) auto
  finally show ?thesis
    using ‹content (box a b) > 0 by simp
qed

lemma content_split:
  fixes a :: "'a::euclidean_space"
  assumes "k  Basis"
  shows "content (cbox a b) = content(cbox a b  {x. xk  c}) + content(cbox a b  {x. xk  c})"
  ― ‹Prove using measure theory›
proof (cases "iBasis. a  i  b  i")
  case True
  have 1: "X Y Z. (iBasis. Z i (if i = k then X else Y i)) = Z k X * (iBasis-{k}. Z i (Y i))"
    by (simp add: if_distrib prod.delta_remove assms)
  note simps = interval_split[OF assms] content_cbox_cases
  have 2: "(iBasis. bi - ai) = (iBasis-{k}. bi - ai) * (bk - ak)"
    by (metis (no_types, lifting) assms finite_Basis mult.commute prod.remove)
  have "x. min (b  k) c = max (a  k) c 
    x * (bk - ak) = x * (max (a  k) c - a  k) + x * (b  k - max (a  k) c)"
    by  (auto simp add: field_simps)
  moreover
  have **: "(iBasis. ((iBasis. (if i = k then min (b  k) c else b  i) *R i)  i - a  i)) =
      (iBasis. (if i = k then min (b  k) c else b  i) - a  i)"
    "(iBasis. b  i - ((iBasis. (if i = k then max (a  k) c else a  i) *R i)  i)) =
      (iBasis. b  i - (if i = k then max (a  k) c else a  i))"
    by (auto intro!: prod.cong)
  have "¬ a  k  c  ¬ c  b  k  False"
    unfolding not_le using True assms by auto
  ultimately show ?thesis
    using assms unfolding simps ** 1[of "λi x. bi - x"] 1[of "λi x. x - ai"] 2
    by auto
next
  case False
  then have "cbox a b = {}"
    unfolding box_eq_empty by (auto simp: not_le)
  then show ?thesis
    by (auto simp: not_le)
qed

lemma division_of_content_0:
  assumes "content (cbox a b) = 0" "d division_of (cbox a b)" "K  d"
  shows "content K = 0"
  unfolding forall_in_division[OF assms(2)]
  by (meson assms content_0_subset division_of_def)

lemma sum_content_null:
  assumes "content (cbox a b) = 0"
    and "p tagged_division_of (cbox a b)"
  shows "((x,K)p. content K *R f x) = (0::'a::real_normed_vector)"
proof (rule sum.neutral, rule)
  fix y
  assume y: "y  p"
  obtain x K where xk: "y = (x, K)"
    using surj_pair[of y] by blast
  then obtain c d where k: "K = cbox c d" "K  cbox a b"
    by (metis assms(2) tagged_division_ofD(3) tagged_division_ofD(4) y)
  have "(λ(x',K'). content K' *R f x') y = content K *R f x"
    unfolding xk by auto
  also have " = 0"
    using assms(1) content_0_subset k(2) by auto
  finally show "(λ(x, k). content k *R f x) y = 0" .
qed

global_interpretation sum_content: operative plus 0 content
  rewrites "comm_monoid_set.F plus 0 = sum"
proof -
  interpret operative plus 0 content
    by standard (auto simp add: content_split [symmetric] content_eq_0_interior)
  show "operative plus 0 content"
    by standard
  show "comm_monoid_set.F plus 0 = sum"
    by (simp add: sum_def)
qed

lemma additive_content_division: "d division_of (cbox a b)  sum content d = content (cbox a b)"
  by (fact sum_content.division)

lemma additive_content_tagged_division:
  "d tagged_division_of (cbox a b)  sum (λ(x,l). content l) d = content (cbox a b)"
  by (fact sum_content.tagged_division)

lemma subadditive_content_division:
  assumes "𝒟 division_of S" "S  cbox a b"
  shows "sum content 𝒟  content(cbox a b)"
proof -
  have "𝒟 division_of 𝒟" "𝒟  cbox a b"
    using assms by auto
  then obtain 𝒟' where "𝒟  𝒟'" "𝒟' division_of cbox a b"
    using partial_division_extend_interval by metis
  then have "sum content 𝒟  sum content 𝒟'"
    using sum_mono2 by blast
  also have "...  content(cbox a b)"
    by (simp add: 𝒟' division_of cbox a b additive_content_division less_eq_real_def)
  finally show ?thesis .
qed

lemma content_real_eq_0: "content {a..b::real} = 0  a  b"
  by (metis atLeastatMost_empty_iff2 content_empty content_real diff_self eq_iff le_cases le_iff_diff_le_0)

lemma property_empty_interval: "a b. content (cbox a b) = 0  P (cbox a b)  P {}"
  using content_empty unfolding empty_as_interval by auto

lemma interval_bounds_nz_content [simp]:
  assumes "content (cbox a b)  0"
  shows "interval_upperbound (cbox a b) = b"
    and "interval_lowerbound (cbox a b) = a"
  by (metis assms content_empty interval_bounds')+

subsection ‹Gauge integral›

text ‹Case distinction to define it first on compact intervals first, then use a limit. This is only
much later unified. In Fremlin: Measure Theory, Volume 4I this is generalized using residual sets.›

definition has_integral :: "('n::euclidean_space  'b::real_normed_vector)  'b  'n set  bool"
  (infixr "has'_integral" 46)
  where "(f has_integral I) s 
    (if a b. s = cbox a b
      then ((λp. (x,k)p. content k *R f x)  I) (division_filter s)
      else (e>0. B>0. a b. ball 0 B  cbox a b 
        (z. ((λp. (x,k)p. content k *R (if x  s then f x else 0))  z) (division_filter (cbox a b)) 
          norm (z - I) < e)))"

lemma has_integral_cbox:
  "(f has_integral I) (cbox a b)  ((λp. (x,k)p. content k *R f x)  I) (division_filter (cbox a b))"
  by (auto simp add: has_integral_def)

lemma has_integral:
  "(f has_integral y) (cbox a b) 
    (e>0. γ. gauge γ 
      (𝒟. 𝒟 tagged_division_of (cbox a b)  γ fine 𝒟 
        norm (sum (λ(x,k). content(k) *R f x) 𝒟 - y) < e))"
  by (auto simp: dist_norm eventually_division_filter has_integral_def tendsto_iff)

lemma has_integral_real:
  "(f has_integral y) {a..b::real} 
    (e>0. γ. gauge γ 
      (𝒟. 𝒟 tagged_division_of {a..b}  γ fine 𝒟 
        norm (sum (λ(x,k). content(k) *R f x) 𝒟 - y) < e))"
  unfolding box_real[symmetric] by (rule has_integral)

lemma has_integralD[dest]:
  assumes "(f has_integral y) (cbox a b)"
    and "e > 0"
  obtains γ
    where "gauge γ"
      and "𝒟. 𝒟 tagged_division_of (cbox a b)  γ fine 𝒟 
        norm (((x,k)𝒟. content k *R f x) - y) < e"
  using assms unfolding has_integral by auto

lemma has_integral_alt:
  "(f has_integral y) i 
    (if a b. i = cbox a b
     then (f has_integral y) i
     else (e>0. B>0. a b. ball 0 B  cbox a b 
      (z. ((λx. if x  i then f x else 0) has_integral z) (cbox a b)  norm (z - y) < e)))"
  by (subst has_integral_def) (auto simp add: has_integral_cbox)

lemma has_integral_altD:
  assumes "(f has_integral y) i"
    and "¬ (a b. i = cbox a b)"
    and "e>0"
  obtains B where "B > 0"
    and "a b. ball 0 B  cbox a b 
      (z. ((λx. if x  i then f(x) else 0) has_integral z) (cbox a b)  norm(z - y) < e)"
  using assms has_integral_alt[of f y i] by auto

definition integrable_on (infixr "integrable'_on" 46)
  where "f integrable_on i  (y. (f has_integral y) i)"

definition "integral i f = (SOME y. (f has_integral y) i  ¬ f integrable_on i  y=0)"

lemma integrable_integral[intro]: "f integrable_on i  (f has_integral (integral i f)) i"
  unfolding integrable_on_def integral_def by (metis (mono_tags, lifting) someI_ex)

lemma not_integrable_integral: "¬ f integrable_on i  integral i f = 0"
  unfolding integrable_on_def integral_def by blast

lemma has_integral_integrable[dest]: "(f has_integral i) s  f integrable_on s"
  unfolding integrable_on_def by auto

lemma has_integral_integral: "f integrable_on s  (f has_integral (integral s f)) s"
  by auto

subsection ‹Basic theorems about integrals›

lemma has_integral_eq_rhs: "(f has_integral j) S  i = j  (f has_integral i) S"
  by (rule forw_subst)

lemma has_integral_unique_cbox:
  fixes f :: "'n::euclidean_space  'a::real_normed_vector"
  shows "(f has_integral k1) (cbox a b)  (f has_integral k2) (cbox a b)  k1 = k2"
    by (auto simp: has_integral_cbox intro: tendsto_unique[OF division_filter_not_empty])    

lemma has_integral_unique:
  fixes f :: "'n::euclidean_space  'a::real_normed_vector"
  assumes "(f has_integral k1) i" "(f has_integral k2) i"
  shows "k1 = k2"
proof (rule ccontr)
  let ?e = "norm (k1 - k2)/2"
  let ?F = "(λx. if x  i then f x else 0)"
  assume "k1  k2"
  then have e: "?e > 0"
    by auto
  have nonbox: "¬ (a b. i = cbox a b)"
    using k1  k2 assms has_integral_unique_cbox by blast
  obtain B1 where B1:
      "0 < B1"
      "a b. ball 0 B1  cbox a b 
        z. (?F has_integral z) (cbox a b)  norm (z - k1) < norm (k1 - k2)/2"
    by (rule has_integral_altD[OF assms(1) nonbox,OF e]) blast
  obtain B2 where B2:
      "0 < B2"
      "a b. ball 0 B2  cbox a b 
        z. (?F has_integral z) (cbox a b)  norm (z - k2) < norm (k1 - k2)/2"
    by (rule has_integral_altD[OF assms(2) nonbox,OF e]) blast
  obtain a b :: 'n where ab: "ball 0 B1  cbox a b" "ball 0 B2  cbox a b"
    by (metis Un_subset_iff bounded_Un bounded_ball bounded_subset_cbox_symmetric)
  obtain w where w: "(?F has_integral w) (cbox a b)" "norm (w - k1) < norm (k1 - k2)/2"
    using B1(2)[OF ab(1)] by blast
  obtain z where z: "(?F has_integral z) (cbox a b)" "norm (z - k2) < norm (k1 - k2)/2"
    using B2(2)[OF ab(2)] by blast
  have "z = w"
    using has_integral_unique_cbox[OF w(1) z(1)] by auto
  then have "norm (k1 - k2)  norm (z - k2) + norm (w - k1)"
    using norm_triangle_ineq4 [of "k1 - w" "k2 - z"]
    by (auto simp add: norm_minus_commute)
  also have " < norm (k1 - k2)/2 + norm (k1 - k2)/2"
    by (metis add_strict_mono z(2) w(2))
  finally show False by auto
qed

lemma integral_unique [intro]: "(f has_integral y) k  integral k f = y"
  unfolding integral_def
  by (rule some_equality) (auto intro: has_integral_unique)

lemma has_integral_iff: "(f has_integral i) S  (f integrable_on S  integral S f = i)"
  by blast

lemma eq_integralD: "integral k f = y  (f has_integral y) k  ¬ f integrable_on k  y=0"
  unfolding integral_def integrable_on_def
  apply (erule subst)
  apply (rule someI_ex)
  by blast

lemma has_integral_const [intro]:
  fixes a b :: "'a::euclidean_space"
  shows "((λx. c) has_integral (content (cbox a b) *R c)) (cbox a b)"
  using eventually_division_filter_tagged_division[of "cbox a b"]
     additive_content_tagged_division[of _ a b]
  by (auto simp: has_integral_cbox split_beta' scaleR_sum_left[symmetric]
           elim!: eventually_mono intro!: tendsto_cong[THEN iffD1, OF _ tendsto_const])

lemma has_integral_const_real [intro]:
  fixes a b :: real
  shows "((λx. c) has_integral (content {a..b} *R c)) {a..b}"
  by (metis box_real(2) has_integral_const)

lemma has_integral_integrable_integral: "(f has_integral i) s  f integrable_on s  integral s f = i"
  by blast

lemma integral_const [simp]:
  fixes a b :: "'a::euclidean_space"
  shows "integral (cbox a b) (λx. c) = content (cbox a b) *R c"
  by (rule integral_unique) (rule has_integral_const)

lemma integral_const_real [simp]:
  fixes a b :: real
  shows "integral {a..b} (λx. c) = content {a..b} *R c"
  by (metis box_real(2) integral_const)

lemma has_integral_is_0_cbox:
  fixes f :: "'n::euclidean_space  'a::real_normed_vector"
  assumes "x. x  cbox a b  f x = 0"
  shows "(f has_integral 0) (cbox a b)"
    unfolding has_integral_cbox
    using eventually_division_filter_tagged_division[of "cbox a b"] assms
    by (subst tendsto_cong[where g="λ_. 0"])
       (auto elim!: eventually_mono intro!: sum.neutral simp: tag_in_interval)

lemma has_integral_is_0:
  fixes f :: "'n::euclidean_space  'a::real_normed_vector"
  assumes "x. x  S  f x = 0"
  shows "(f has_integral 0) S"
proof (cases "(a b. S = cbox a b)")
  case True with assms has_integral_is_0_cbox show ?thesis
    by blast
next
  case False
  have *: "(λx. if x  S then f x else 0) = (λx. 0)"
    by (auto simp add: assms)
  show ?thesis
    using has_integral_is_0_cbox False
    by (subst has_integral_alt) (force simp add: *)
qed

lemma has_integral_0[simp]: "((λx::'n::euclidean_space. 0) has_integral 0) S"
  by (rule has_integral_is_0) auto

lemma has_integral_0_eq[simp]: "((λx. 0) has_integral i) S  i = 0"
  using has_integral_unique[OF has_integral_0] by auto

lemma has_integral_linear_cbox:
  fixes f :: "'n::euclidean_space  'a::real_normed_vector"
  assumes f: "(f has_integral y) (cbox a b)"
    and h: "bounded_linear h"
  shows "((h  f) has_integral (h y)) (cbox a b)"
proof -
  interpret bounded_linear h using h .
  show ?thesis
    unfolding has_integral_cbox using tendsto [OF f [unfolded has_integral_cbox]]
    by (simp add: sum scaleR split_beta')
qed

lemma has_integral_linear:
  fixes f :: "'n::euclidean_space  'a::real_normed_vector"
  assumes f: "(f has_integral y) S"
    and h: "bounded_linear h"
  shows "((h  f) has_integral (h y)) S"
proof (cases "(a b. S = cbox a b)")
  case True with f h has_integral_linear_cbox show ?thesis 
    by blast
next
  case False
  interpret bounded_linear h using h .
  from pos_bounded obtain B where B: "0 < B" "x. norm (h x)  norm x * B"
    by blast
  let ?S = "λf x. if x  S then f x else 0"
  show ?thesis
  proof (subst has_integral_alt, clarsimp simp: False)
    fix e :: real
    assume e: "e > 0"
    have *: "0 < e/B" using e B(1) by simp
    obtain M where M:
      "M > 0"
      "a b. ball 0 M  cbox a b 
        z. (?S f has_integral z) (cbox a b)  norm (z - y) < e/B"
      using has_integral_altD[OF f False *] by blast
    show "B>0. a b. ball 0 B  cbox a b 
      (z. (?S(h  f) has_integral z) (cbox a b)  norm (z - h y) < e)"
    proof (rule exI, intro allI conjI impI)
      show "M > 0" using M by metis
    next
      fix a b::'n
      assume sb: "ball 0 M  cbox a b"
      obtain z where z: "(?S f has_integral z) (cbox a b)" "norm (z - y) < e/B"
        using M(2)[OF sb] by blast
      have *: "?S(h  f) = h  ?S f"
        using zero by auto
      show "z. (?S(h  f) has_integral z) (cbox a b)  norm (z - h y) < e"
      proof (intro exI conjI)
        show "(?S(h  f) has_integral h z) (cbox a b)"
          by (simp add: * has_integral_linear_cbox[OF z(1) h])
        show "norm (h z - h y) < e"
          by (metis B diff le_less_trans pos_less_divide_eq z(2))
      qed
    qed
  qed
qed

lemma has_integral_scaleR_left:
  "(f has_integral y) S  ((λx. f x *R c) has_integral (y *R c)) S"
  using has_integral_linear[OF _ bounded_linear_scaleR_left] by (simp add: comp_def)

lemma integrable_on_scaleR_left:
  assumes "f integrable_on A"
  shows "(λx. f x *R y) integrable_on A"
  using assms has_integral_scaleR_left unfolding integrable_on_def by blast

lemma has_integral_mult_left:
  fixes c :: "_ :: real_normed_algebra"
  shows "(f has_integral y) S  ((λx. f x * c) has_integral (y * c)) S"
  using has_integral_linear[OF _ bounded_linear_mult_left] by (simp add: comp_def)

lemma has_integral_divide:
  fixes c :: "_ :: real_normed_div_algebra"
  shows "(f has_integral y) S  ((λx. f x / c) has_integral (y / c)) S"
  unfolding divide_inverse by (simp add: has_integral_mult_left)

text‹The case analysis eliminates the condition termf integrable_on S at the cost
     of the type class constraint division_ring›
corollary integral_mult_left [simp]:
  fixes c:: "'a::{real_normed_algebra,division_ring}"
  shows "integral S (λx. f x * c) = integral S f * c"
proof (cases "f integrable_on S  c = 0")
  case True then show ?thesis
    by (force intro: has_integral_mult_left)
next
  case False then have "¬ (λx. f x * c) integrable_on S"
    using has_integral_mult_left [of "(λx. f x * c)" _ S "inverse c"]
    by (auto simp add: mult.assoc)
  with False show ?thesis by (simp add: not_integrable_integral)
qed

corollary integral_mult_right [simp]:
  fixes c:: "'a::{real_normed_field}"
  shows "integral S (λx. c * f x) = c * integral S f"
by (simp add: mult.commute [of c])

corollary integral_divide [simp]:
  fixes z :: "'a::real_normed_field"
  shows "integral S (λx. f x / z) = integral S (λx. f x) / z"
using integral_mult_left [of S f "inverse z"]
  by (simp add: divide_inverse_commute)

lemma has_integral_mult_right:
  fixes c :: "'a :: real_normed_algebra"
  shows "(f has_integral y) i  ((λx. c * f x) has_integral (c * y)) i"
  using has_integral_linear[OF _ bounded_linear_mult_right] by (simp add: comp_def)

lemma has_integral_cmul: "(f has_integral k) S  ((λx. c *R f x) has_integral (c *R k)) S"
  unfolding o_def[symmetric]
  by (metis has_integral_linear bounded_linear_scaleR_right)

lemma has_integral_cmult_real:
  fixes c :: real
  assumes "c  0  (f has_integral x) A"
  shows "((λx. c * f x) has_integral c * x) A"
proof (cases "c = 0")
  case True
  then show ?thesis by simp
next
  case False
  from has_integral_cmul[OF assms[OF this], of c] show ?thesis
    unfolding real_scaleR_def .
qed

lemma has_integral_neg: "(f has_integral k) S  ((λx. -(f x)) has_integral -k) S"
  by (drule_tac c="-1" in has_integral_cmul) auto

lemma has_integral_neg_iff: "((λx. - f x) has_integral k) S  (f has_integral - k) S"
  using has_integral_neg[of f "- k"] has_integral_neg[of "λx. - f x" k] by auto

lemma has_integral_add_cbox:
  fixes f :: "'n::euclidean_space  'a::real_normed_vector"
  assumes "(f has_integral k) (cbox a b)" "(g has_integral l) (cbox a b)"
  shows "((λx. f x + g x) has_integral (k + l)) (cbox a b)"
  using assms
    unfolding has_integral_cbox
    by (simp add: split_beta' scaleR_add_right sum.distrib[abs_def] tendsto_add)

lemma has_integral_add:
  fixes f :: "'n::euclidean_space  'a::real_normed_vector"
  assumes f: "(f has_integral k) S" and g: "(g has_integral l) S"
  shows "((λx. f x + g x) has_integral (k + l)) S"
proof (cases "a b. S = cbox a b")
  case True with has_integral_add_cbox assms show ?thesis
    by blast 
next
  let ?S = "λf x. if x  S then f x else 0"
  case False
  then show ?thesis
  proof (subst has_integral_alt, clarsimp, goal_cases)
    case (1 e)
    then have e2: "e/2 > 0"
      by auto
    obtain Bf where "0 < Bf"
      and Bf: "a b. ball 0 Bf  cbox a b 
                     z. (?S f has_integral z) (cbox a b)  norm (z - k) < e/2"
      using has_integral_altD[OF f False e2] by blast
    obtain Bg where "0 < Bg"
      and Bg: "a b. ball 0 Bg  (cbox a b) 
                     z. (?S g has_integral z) (cbox a b)  norm (z - l) < e/2"
      using has_integral_altD[OF g False e2] by blast
    show ?case
    proof (rule_tac x="max Bf Bg" in exI, clarsimp simp add: max.strict_coboundedI1 0 < Bf)
      fix a b
      assume "ball 0 (max Bf Bg)  cbox a (b::'n)"
      then have fs: "ball 0 Bf  cbox a (b::'n)" and gs: "ball 0 Bg  cbox a (b::'n)"
        by auto
      obtain w where w: "(?S f has_integral w) (cbox a b)" "norm (w - k) < e/2"
        using Bf[OF fs] by blast
      obtain z where z: "(?S g has_integral z) (cbox a b)" "norm (z - l) < e/2"
        using Bg[OF gs] by blast
      have *: "x. (if x  S then f x + g x else 0) = (?S f x) + (?S g x)"
        by auto
      show "z. (?S(λx. f x + g x) has_integral z) (cbox a b)  norm (z - (k + l)) < e"
      proof (intro exI conjI)
        show "(?S(λx. f x + g x) has_integral (w + z)) (cbox a b)"
          by (simp add: has_integral_add_cbox[OF w(1) z(1), unfolded *[symmetric]])
        show "norm (w + z - (k + l)) < e"
          by (metis dist_norm dist_triangle_add_half w(2) z(2))
      qed
    qed
  qed
qed

lemma has_integral_diff:
  "(f has_integral k) S  (g has_integral l) S 
    ((λx. f x - g x) has_integral (k - l)) S"
  using has_integral_add[OF _ has_integral_neg, of f k S g l]
  by (auto simp: algebra_simps)

lemma integral_0 [simp]:
  "integral S (λx::'n::euclidean_space. 0::'m::real_normed_vector) = 0"
  by (rule integral_unique has_integral_0)+

lemma integral_add: "f integrable_on S  g integrable_on S 
    integral S (λx. f x + g x) = integral S f + integral S g"
  by (rule integral_unique) (metis integrable_integral has_integral_add)

lemma integral_cmul [simp]: "integral S (λx. c *R f x) = c *R integral S f"
proof (cases "f integrable_on S  c = 0")
  case True with has_integral_cmul integrable_integral show ?thesis
    by fastforce
next
  case False then have "¬ (λx. c *R f x) integrable_on S"
    using has_integral_cmul [of "(λx. c *R f x)" _ S "inverse c"] by auto
  with False show ?thesis by (simp add: not_integrable_integral)
qed

lemma integral_mult:
  fixes K::real
  shows "f integrable_on X  K * integral X f = integral X (λx. K * f x)"
  unfolding real_scaleR_def[symmetric] integral_cmul ..

lemma integral_neg [simp]: "integral S (λx. - f x) = - integral S f"
proof (cases "f integrable_on S")
  case True then show ?thesis
    by (simp add: has_integral_neg integrable_integral integral_unique)
next
  case False then have "¬ (λx. - f x) integrable_on S"
    using has_integral_neg [of "(λx. - f x)" _ S ] by auto
  with False show ?thesis by (simp add: not_integrable_integral)
qed

lemma integral_diff: "f integrable_on S  g integrable_on S 
    integral S (λx. f x - g x) = integral S f - integral S g"
  by (rule integral_unique) (metis integrable_integral has_integral_diff)

lemma integrable_0: "(λx. 0) integrable_on S"
  unfolding integrable_on_def using has_integral_0 by auto

lemma integrable_add: "f integrable_on S  g integrable_on S  (λx. f x + g x) integrable_on S"
  unfolding integrable_on_def by(auto intro: has_integral_add)

lemma integrable_cmul: "f integrable_on S  (λx. c *R f(x)) integrable_on S"
  unfolding integrable_on_def by(auto intro: has_integral_cmul)

lemma integrable_on_scaleR_iff [simp]:
  fixes c :: real
  assumes "c  0"
  shows "(λx. c *R f x) integrable_on S  f integrable_on S"
  using integrable_cmul[of "λx. c *R f x" S "1 / c"] integrable_cmul[of f S c] c  0
  by auto

lemma integrable_on_cmult_iff [simp]:
  fixes c :: real
  assumes "c  0"
  shows "(λx. c * f x) integrable_on S  f integrable_on S"
  using integrable_on_scaleR_iff [of c f] assms by simp

lemma integrable_on_cmult_left:
  assumes "f integrable_on S"
  shows "(λx. of_real c * f x) integrable_on S"
    using integrable_cmul[of f S "of_real c"] assms
    by (simp add: scaleR_conv_of_real)

lemma integrable_neg: "f integrable_on S  (λx. -f(x)) integrable_on S"
  unfolding integrable_on_def by(auto intro: has_integral_neg)

lemma integrable_neg_iff: "(λx. -f(x)) integrable_on S  f integrable_on S"
  using integrable_neg by fastforce

lemma integrable_diff:
  "f integrable_on S  g integrable_on S  (λx. f x - g x) integrable_on S"
  unfolding integrable_on_def by(auto intro: has_integral_diff)

lemma integrable_linear:
  "f integrable_on S  bounded_linear h  (h  f) integrable_on S"
  unfolding integrable_on_def by(auto intro: has_integral_linear)

lemma integral_linear:
  "f integrable_on S  bounded_linear h  integral S (h  f) = h (integral S f)"
  by (meson has_integral_iff has_integral_linear)

lemma integrable_on_cnj_iff:
  "(λx. cnj (f x)) integrable_on A  f integrable_on A"
  using integrable_linear[OF _ bounded_linear_cnj, of f A]
        integrable_linear[OF _ bounded_linear_cnj, of "cnj  f" A]
  by (auto simp: o_def)

lemma integral_cnj: "cnj (integral A f) = integral A (λx. cnj (f x))"
  by (cases "f integrable_on A")
     (simp_all add: integral_linear[OF _ bounded_linear_cnj, symmetric]
                    o_def integrable_on_cnj_iff not_integrable_integral)

lemma integral_component_eq[simp]:
  fixes f :: "'n::euclidean_space  'm::euclidean_space"
  assumes "f integrable_on S"
  shows "integral S (λx. f x  k) = integral S f  k"
  unfolding integral_linear[OF assms(1) bounded_linear_inner_left,unfolded o_def] ..

lemma has_integral_sum:
  assumes "finite T"
    and "a. a  T  ((f a) has_integral (i a)) S"
  shows "((λx. sum (λa. f a x) T) has_integral (sum i T)) S"
  using assms(1) subset_refl[of T]
proof (induct rule: finite_subset_induct)
  case empty
  then show ?case by auto
next
  case (insert x F)
  with assms show ?case
    by (simp add: has_integral_add)
qed

lemma integral_sum:
  "finite I;  a. a  I  f a integrable_on S 
   integral S (λx. aI. f a x) = (aI. integral S (f a))"
  by (simp add: has_integral_sum integrable_integral integral_unique)

lemma integrable_sum:
  "finite I;  a. a  I  f a integrable_on S  (λx. aI. f a x) integrable_on S"
  unfolding integrable_on_def using has_integral_sum[of I] by metis

lemma has_integral_eq:
  assumes "x. x  s  f x = g x"
    and "(f has_integral k) s"
  shows "(g has_integral k) s"
  using has_integral_diff[OF assms(2), of "λx. f x - g x" 0]
  using has_integral_is_0[of s "λx. f x - g x"]
  using assms(1)
  by auto

lemma integrable_eq: "f integrable_on s; x. x  s  f x = g x  g integrable_on s"
  unfolding integrable_on_def
  using has_integral_eq[of s f g] has_integral_eq by blast

lemma has_integral_cong:
  assumes "x. x  s  f x = g x"
  shows "(f has_integral i) s = (g has_integral i) s"
  using has_integral_eq[of s f g] has_integral_eq[of s g f] assms
  by auto

lemma integral_cong:
  assumes "x. x  s  f x = g x"
  shows "integral s f = integral s g"
  unfolding integral_def
by (metis (full_types, hide_lams) assms has_integral_cong integrable_eq)

lemma integrable_on_cmult_left_iff [simp]:
  assumes "c  0"
  shows "(λx. of_real c * f x) integrable_on s  f integrable_on s"
        (is "?lhs = ?rhs")
proof
  assume ?lhs
  then have "(λx. of_real (1 / c) * (of_real c * f x)) integrable_on s"
    using integrable_cmul[of "λx. of_real c * f x" s "1 / of_real c"]
    by (simp add: scaleR_conv_of_real)
  then have "(λx. (of_real (1 / c) * of_real c * f x)) integrable_on s"
    by (simp add: algebra_simps)
  with c  0 show ?rhs
    by (metis (no_types, lifting) integrable_eq mult.left_neutral nonzero_divide_eq_eq of_real_1 of_real_mult)
qed (blast intro: integrable_on_cmult_left)

lemma integrable_on_cmult_right:
  fixes f :: "_  'b :: {comm_ring,real_algebra_1,real_normed_vector}"
  assumes "f integrable_on s"
  shows "(λx. f x * of_real c) integrable_on s"
using integrable_on_cmult_left [OF assms] by (simp add: mult.commute)

lemma integrable_on_cmult_right_iff [simp]:
  fixes f :: "_  'b :: {comm_ring,real_algebra_1,real_normed_vector}"
  assumes "c  0"
  shows "(λx. f x * of_real c) integrable_on s  f integrable_on s"
using integrable_on_cmult_left_iff [OF assms] by (simp add: mult.commute)

lemma integrable_on_cdivide:
  fixes f :: "_  'b :: real_normed_field"
  assumes "f integrable_on s"
  shows "(λx. f x / of_real c) integrable_on s"
by (simp add: integrable_on_cmult_right divide_inverse assms flip: of_real_inverse)

lemma integrable_on_cdivide_iff [simp]:
  fixes f :: "_  'b :: real_normed_field"
  assumes "c  0"
  shows "(λx. f x / of_real c) integrable_on s  f integrable_on s"
by (simp add: divide_inverse assms flip: of_real_inverse)

lemma has_integral_null [intro]: "content(cbox a b) = 0  (f has_integral 0) (cbox a b)"
  unfolding has_integral_cbox
  using eventually_division_filter_tagged_division[of "cbox a b"]
  by (subst tendsto_cong[where g="λ_. 0"]) (auto elim: eventually_mono intro: sum_content_null)

lemma has_integral_null_real [intro]: "content {a..b::real} = 0  (f has_integral 0) {a..b}"
  by (metis box_real(2) has_integral_null)

lemma has_integral_null_eq[simp]: "content (cbox a b) = 0  (f has_integral i) (cbox a b)  i = 0"
  by (auto simp add: has_integral_null dest!: integral_unique)

lemma integral_null [simp]: "content (cbox a b) = 0  integral (cbox a b) f = 0"
  by (metis has_integral_null integral_unique)

lemma integrable_on_null [intro]: "content (cbox a b) = 0  f integrable_on (cbox a b)"
  by (simp add: has_integral_integrable)

lemma has_integral_empty[intro]: "(f has_integral 0) {}"
  by (meson ex_in_conv has_integral_is_0)

lemma has_integral_empty_eq[simp]: "(f has_integral i) {}  i = 0"
  by (auto simp add: has_integral_empty has_integral_unique)

lemma integrable_on_empty[intro]: "f integrable_on {}"
  unfolding integrable_on_def by auto

lemma integral_empty[simp]: "integral {} f = 0"
  by (rule integral_unique) (rule has_integral_empty)

lemma has_integral_refl[intro]:
  fixes a :: "'a::euclidean_space"
  shows "(f has_integral 0) (cbox a a)"
    and "(f has_integral 0) {a}"
proof -
  show "(f has_integral 0) (cbox a a)"
     by (rule has_integral_null) simp
  then show "(f has_integral 0) {a}"
    by simp
qed

lemma integrable_on_refl[intro]: "f integrable_on cbox a a"
  unfolding integrable_on_def by auto

lemma integral_refl [simp]: "integral (cbox a a) f = 0"
  by (rule integral_unique) auto

lemma integral_singleton [simp]: "integral {a} f = 0"
  by auto

lemma integral_blinfun_apply:
  assumes "f integrable_on s"
  shows "integral s (λx. blinfun_apply h (f x)) = blinfun_apply h (integral s f)"
  by (subst integral_linear[symmetric, OF assms blinfun.bounded_linear_right]) (simp add: o_def)

lemma blinfun_apply_integral:
  assumes "f integrable_on s"
  shows "blinfun_apply (integral s f) x = integral s (λy. blinfun_apply (f y) x)"
  by (metis (no_types, lifting) assms blinfun.prod_left.rep_eq integral_blinfun_apply integral_cong)

lemma has_integral_componentwise_iff:
  fixes f :: "'a :: euclidean_space  'b :: euclidean_space"
  shows "(f has_integral y) A  (bBasis. ((λx. f x  b) has_integral (y  b)) A)"
proof safe
  fix b :: 'b assume "(f has_integral y) A"
  from has_integral_linear[OF this(1) bounded_linear_inner_left, of b]
    show "((λx. f x  b) has_integral (y  b)) A" by (simp add: o_def)
next
  assume "(bBasis. ((λx. f x  b) has_integral (y  b)) A)"
  hence "bBasis. (((λx. x *R b)  (λx. f x  b)) has_integral ((y  b) *R b)) A"
    by (intro ballI has_integral_linear) (simp_all add: bounded_linear_scaleR_left)
  hence "((λx. bBasis. (f x  b) *R b) has_integral (bBasis. (y  b) *R b)) A"
    by (intro has_integral_sum) (simp_all add: o_def)
  thus "(f has_integral y) A" by (simp add: euclidean_representation)
qed

lemma has_integral_componentwise:
  fixes f :: "'a :: euclidean_space  'b :: euclidean_space"
  shows "(b. b  Basis  ((λx. f x  b) has_integral (y  b)) A)  (f has_integral y) A"
  by (subst has_integral_componentwise_iff) blast

lemma integrable_componentwise_iff:
  fixes f :: "'a :: euclidean_space  'b :: euclidean_space"
  shows "f integrable_on A  (bBasis. (λx. f x  b) integrable_on A)"
proof
  assume "f integrable_on A"
  then obtain y where "(f has_integral y) A" by (auto simp: integrable_on_def)
  hence "(bBasis. ((λx. f x  b) has_integral (y  b)) A)"
    by (subst (asm) has_integral_componentwise_iff)
  thus "(bBasis. (λx. f x  b) integrable_on A)" by (auto simp: integrable_on_def)
next
  assume "(bBasis. (λx. f x  b) integrable_on A)"
  then obtain y where "bBasis. ((λx. f x  b) has_integral y b) A"
    unfolding integrable_on_def by (subst (asm) bchoice_iff) blast
  hence "bBasis. (((λx. x *R b)  (λx. f x  b)) has_integral (y b *R b)) A"
    by (intro ballI has_integral_linear) (simp_all add: bounded_linear_scaleR_left)
  hence "((λx. bBasis. (f x  b) *R b) has_integral (bBasis. y b *R b)) A"
    by (intro has_integral_sum) (simp_all add: o_def)
  thus "f integrable_on A" by (auto simp: integrable_on_def o_def euclidean_representation)
qed

lemma integrable_componentwise:
  fixes f :: "'a :: euclidean_space  'b :: euclidean_space"
  shows "(b. b  Basis  (λx. f x  b) integrable_on A)  f integrable_on A"
  by (subst integrable_componentwise_iff) blast

lemma integral_componentwise:
  fixes f :: "'a :: euclidean_space  'b :: euclidean_space"
  assumes "f integrable_on A"
  shows "integral A f = (bBasis. integral A (λx. (f x  b) *R b))"
proof -
  from assms have integrable: "bBasis. (λx. x *R b)  (λx. (f x  b)) integrable_on A"
    by (subst (asm) integrable_componentwise_iff, intro integrable_linear ballI)
       (simp_all add: bounded_linear_scaleR_left)
  have "integral A f = integral A (λx. bBasis. (f x  b) *R b)"
    by (simp add: euclidean_representation)
  also from integrable have " = (aBasis. integral A (λx. (f x  a) *R a))"
    by (subst integral_sum) (simp_all add: o_def)
  finally show ?thesis .
qed

lemma integrable_component:
  "f integrable_on A  (λx. f x  (y :: 'b :: euclidean_space)) integrable_on A"
  by (drule integrable_linear[OF _ bounded_linear_inner_left[of y]]) (simp add: o_def)



subsection ‹Cauchy-type criterion for integrability›

proposition integrable_Cauchy:
  fixes f :: "'n::euclidean_space  'a::{real_normed_vector,complete_space}"
  shows "f integrable_on cbox a b 
        (e>0. γ. gauge γ 
          (𝒟1 𝒟2. 𝒟1 tagged_division_of (cbox a b)  γ fine 𝒟1 
            𝒟2 tagged_division_of (cbox a b)  γ fine 𝒟2 
            norm (((x,K)𝒟1. content K *R f x) - ((x,K)𝒟2. content K *R f x)) < e))"
  (is "?l = (e>0. γ. ?P e γ)")
proof (intro iffI allI impI)
  assume ?l
  then obtain y
    where y: "e. e > 0 
        γ. gauge γ 
            (𝒟. 𝒟 tagged_division_of cbox a b  γ fine 𝒟 
                 norm (((x,K)  𝒟. content K *R f x) - y) < e)"
    by (auto simp: integrable_on_def has_integral)
  show "γ. ?P e γ" if "e > 0" for e
  proof -
    have "e/2 > 0" using that by auto
    with y obtain γ where "gauge γ"
      and γ: "𝒟. 𝒟 tagged_division_of cbox a b  γ fine 𝒟 
                  norm (((x,K)𝒟. content K *R f x) - y) < e/2"
      by meson
    show ?thesis
    apply (rule_tac x=γ in exI, clarsimp simp: ‹gauge γ)
        by (blast intro!: γ dist_triangle_half_l[where y=y,unfolded dist_norm])
    qed
next
  assume "e>0. γ. ?P e γ"
  then have "n::nat. γ. ?P (1 / (n + 1)) γ"
    by auto
  then obtain γ :: "nat  'n  'n set" where γ:
    "m. gauge (γ m)"
    "m 𝒟1 𝒟2. 𝒟1 tagged_division_of cbox a b;
              γ m fine 𝒟1; 𝒟2 tagged_division_of cbox a b; γ m fine 𝒟2
               norm (((x,K)  𝒟1. content K *R f x) - ((x,K)  𝒟2. content K *R f x))
                  < 1 / (m + 1)"
    by metis
  have "gauge (λx. {γ i x |i. i  {0..n}})" for n
    using γ by (intro gauge_Inter) auto
  then have "n. p. p tagged_division_of (cbox a b)  (λx. {γ i x |i. i  {0..n}}) fine p"
    by (meson fine_division_exists)
  then obtain p where p: "z. p z tagged_division_of cbox a b"
                         "z. (λx. {γ i x |i. i  {0..z}}) fine p z"
    by meson
  have dp: "i n. in  γ i fine p n"
    using p unfolding fine_Inter
    using atLeastAtMost_iff by blast
  have "Cauchy (λn. sum (λ(x,K). content K *R (f x)) (p n))"
  proof (rule CauchyI)
    fix e::real
    assume "0 < e"
    then obtain N where "N  0" and N: "inverse (real N) < e"
      using real_arch_inverse[of e] by blast
    show "M. mM. nM. norm (((x,K)  p m. content K *R f x) - ((x,K)  p n. content K *R f x)) < e"
    proof (intro exI allI impI)
      fix m n
      assume mn: "N  m" "N  n"
      have "norm (((x,K)  p m. content K *R f x) - ((x,K)  p n. content K *R f x)) < 1 / (real N + 1)"
        by (simp add: p(1) dp mn γ)
      also have "... < e"
        using  N N  0 0 < e by (auto simp: field_simps)
      finally show "norm (((x,K)  p m. content K *R f x) - ((x,K)  p n. content K *R f x)) < e" .
    qed
  qed
  then obtain y where y: "no. nno. norm (((x,K)  p n. content K *R f x) - y) < r" if "r > 0" for r
    by (auto simp: convergent_eq_Cauchy[symmetric] dest: LIMSEQ_D)
  show ?l
    unfolding integrable_on_def has_integral
  proof (rule_tac x=y in exI, clarify)
    fix e :: real
    assume "e>0"
    then have e2: "e/2 > 0" by auto
    then obtain N1::nat where N1: "N1  0" "inverse (real N1) < e/2"
      using real_arch_inverse by blast
    obtain N2::nat where N2: "n. n  N2  norm (((x,K)  p n. content K *R f x) - y) < e/2"
      using y[OF e2] by metis
    show "γ. gauge γ 
              (𝒟. 𝒟 tagged_division_of (cbox a b)  γ fine 𝒟 
                norm (((x,K)  𝒟. content K *R f x) - y) < e)"
    proof (intro exI conjI allI impI)
      show "gauge (γ (N1+N2))"
        using γ by auto
      show "norm (((x,K)  q. content K *R f x) - y) < e"
           if "q tagged_division_of cbox a b  γ (N1+N2) fine q" for q
      proof (rule norm_triangle_half_r)
        have "norm (((x,K)  p (N1+N2). content K *R f x) - ((x,K)  q. content K *R f x))
               < 1 / (real (N1+N2) + 1)"
          by (rule γ; simp add: dp p that)
        also have "... < e/2"
          using N1 0 < e by (auto simp: field_simps intro: less_le_trans)
        finally show "norm (((x,K)  p (N1+N2). content K *R f x) - ((x,K)  q. content K *R f x)) < e/2" .
        show "norm (((x,K)  p (N1+N2). content K *R f x) - y) < e/2"
          using N2 le_add_same_cancel2 by blast
      qed
    qed
  qed
qed


subsection ‹Additivity of integral on abutting intervals›

lemma tagged_division_split_left_inj_content:
  assumes 𝒟: "𝒟 tagged_division_of S"
    and "(x1, K1)  𝒟" "(x2, K2)  𝒟" "K1  K2" "K1  {x. xk  c} = K2  {x. xk  c}" "k  Basis"
  shows "content (K1  {x. xk  c}) = 0"
proof -
  from tagged_division_ofD(4)[OF 𝒟 (x1, K1)  𝒟] obtain a b where K1: "K1 = cbox a b"
    by auto
  then have "interior (K1  {x. x  k  c}) = {}"
    by (metis tagged_division_split_left_inj assms)
  then show ?thesis
    unfolding K1 interval_split[OF k  Basis›] by (auto simp: content_eq_0_interior)
qed

lemma tagged_division_split_right_inj_content:
  assumes 𝒟: "𝒟 tagged_division_of S"
    and "(x1, K1)  𝒟" "(x2, K2)  𝒟" "K1  K2" "K1  {x. xk  c} = K2  {x. xk  c}" "k  Basis"
  shows "content (K1  {x. xk  c}) = 0"
proof -
  from tagged_division_ofD(4)[OF 𝒟 (x1, K1)  𝒟] obtain a b where K1: "K1 = cbox a b"
    by auto
  then have "interior (K1  {x. c  x  k}) = {}"
    by (metis tagged_division_split_right_inj assms)
  then show ?thesis
    unfolding K1 interval_split[OF k  Basis›]
    by (auto simp: content_eq_0_interior)
qed


proposition has_integral_split:
  fixes f :: "'a::euclidean_space  'b::real_normed_vector"
  assumes fi: "(f has_integral i) (cbox a b  {x. xk  c})"
      and fj: "(f has_integral j) (cbox a b  {x. xk  c})"
      and k: "k  Basis"
shows "(f has_integral (i + j)) (cbox a b)"
  unfolding has_integral
proof clarify
  fix e::real
  assume "0 < e"
  then have e: "e/2 > 0"
    by auto
    obtain γ1 where γ1: "gauge γ1"
      and γ1norm:
        "𝒟. 𝒟 tagged_division_of cbox a b  {x. x  k  c}; γ1 fine 𝒟
              norm (((x,K)  𝒟. content K *R f x) - i) < e/2"
       apply (rule has_integralD[OF fi[unfolded interval_split[OF k]] e])
       apply (simp add: interval_split[symmetric] k)
      done
    obtain γ2 where γ2: "gauge γ2"
      and γ2norm:
        "𝒟. 𝒟 tagged_division_of cbox a b  {x. c  x  k}; γ2 fine 𝒟
              norm (((x, k)  𝒟. content k *R f x) - j) < e/2"
       apply (rule has_integralD[OF fj[unfolded interval_split[OF k]] e])
       apply (simp add: interval_split[symmetric] k)
       done
  let  = "λx. if xk = c then (γ1 x  γ2 x) else ball x ¦xk - c¦  γ1 x  γ2 x"
  have "gauge "
    using γ1 γ2 unfolding gauge_def by auto
  then show "γ. gauge γ 
                 (𝒟. 𝒟 tagged_division_of cbox a b  γ fine 𝒟 
                      norm (((x, k)𝒟. content k *R f x) - (i + j)) < e)"
  proof (rule_tac x="" in exI, safe)
    fix p
    assume p: "p tagged_division_of (cbox a b)" and " fine p"
    have ab_eqp: "cbox a b = {K. x. (x, K)  p}"
      using p by blast
    have xk_le_c: "xk  c" if as: "(x,K)  p" and K: "K  {x. xk  c}  {}" for x K
    proof (rule ccontr)
      assume **: "¬ x  k  c"
      then have "K  ball x ¦x  k - c¦"
        using  fine p as by (fastforce simp: not_le algebra_simps)
      with K obtain y where y: "y  ball x ¦x  k - c¦" "yk  c"
        by blast
      then have "¦x  k - y  k¦ < ¦x  k - c¦"
        using Basis_le_norm[OF k, of "x - y"]
        by (auto simp add: dist_norm inner_diff_left intro: le_less_trans)
      with y show False
        using ** by (auto simp add: field_simps)
    qed
    have xk_ge_c: "xk  c" if as: "(x,K)  p" and K: "K  {x. xk  c}  {}" for x K
    proof (rule ccontr)
      assume **: "¬ x  k  c"
      then have "K  ball x ¦x  k - c¦"
        using  fine p as by (fastforce simp: not_le algebra_simps)
      with K obtain y where y: "y  ball x ¦x  k - c¦" "yk  c"
        by blast
      then have "¦x  k - y  k¦ < ¦x  k - c¦"
        using Basis_le_norm[OF k, of "x - y"]
        by (auto simp add: dist_norm inner_diff_left intro: le_less_trans)
      with y show False
        using ** by (auto simp add: field_simps)
    qed
    have fin_finite: "finite {(x,f K) | x K. (x,K)  s  P x K}"
      if "finite s" for s and f :: "'a set  'a set" and P :: "'a  'a set  bool"
    proof -
      from that have "finite ((λ(x,K). (x, f K)) ` s)"
        by auto
      then show ?thesis
        by (rule rev_finite_subset) auto
    qed
    { fix 𝒢 :: "'a set  'a set"
      fix i :: "'a × 'a set"
      assume "i  (λ(x, k). (x, 𝒢 k)) ` p - {(x, 𝒢 k) |x k. (x, k)  p  𝒢 k  {}}"
      then obtain x K where xk: "i = (x, 𝒢 K)"  "(x,K)  p"
                                 "(x, 𝒢 K)  {(x, 𝒢 K) |x K. (x,K)  p  𝒢 K  {}}"
        by auto
      have "content (𝒢 K) = 0"
        using xk using content_empty by auto
      then have "(λ(x,K). content K *R f x) i = 0"
        unfolding xk split_conv by auto
    } note [simp] = this
    have "finite p"
      using p by blast
    let ?M1 = "{(x, K  {x. xk  c}) |x K. (x,K)  p  K  {x. xk  c}  {}}"
    have γ1_fine: "γ1 fine ?M1"
      using  fine p by (fastforce simp: fine_def split: if_split_asm)
    have "norm (((x, k)?M1. content k *R f x) - i) < e/2"
    proof (rule γ1norm [OF tagged_division_ofI γ1_fine])
      show "finite ?M1"
        by (rule fin_finite) (use p in blast)
      show "{k. x. (x, k)  ?M1} = cbox a b  {x. xk  c}"
        by (auto simp: ab_eqp)

      fix x L
      assume xL: "(x, L)  ?M1"
      then obtain x' L' where xL': "x = x'" "L = L'  {x. x  k  c}"
                                   "(x', L')  p" "L'  {x. x  k  c}  {}"
        by blast
      then obtain a' b' where ab': "L' = cbox a' b'"
        using p by blast
      show "x  L" "L  cbox a b  {x. x  k  c}"
        using p xk_le_c xL' by auto
      show "a b. L = cbox a b"
        using p xL' ab' by (auto simp add: interval_split[OF k,where c=c])

      fix y R
      assume yR: "(y, R)  ?M1"
      then obtain y' R' where yR': "y = y'" "R = R'  {x. x  k  c}"
                                   "(y', R')  p" "R'  {x. x  k  c}  {}"
        by blast
      assume as: "(x, L)  (y, R)"
      show "interior L  interior R = {}"
      proof (cases "L' = R'  x' = y'")
        case False
        have "interior R' = {}"
          by (metis (no_types) False Pair_inject inf.idem tagged_division_ofD(5) [OF p] xL'(3) yR'(3))
        then show ?thesis
          using yR' by simp
      next
        case True
        then have "L'  R'"
          using as unfolding xL' yR' by auto
        have "interior L'  interior R' = {}"
          by (metis (no_types) Pair_inject L'  R' p tagged_division_ofD(5) xL'(3) yR'(3))
        then show ?thesis
          using xL'(2) yR'(2) by auto
      qed
    qed
    moreover
    let ?M2 = "{(x,K  {x. xk  c}) |x K. (x,K)  p  K  {x. xk  c}  {}}"
    have γ2_fine: "γ2 fine ?M2"
      using  fine p by (fastforce simp: fine_def split: if_split_asm)
    have "norm (((x, k)?M2. content k *R f x) - j) < e/2"
    proof (rule γ2norm [OF tagged_division_ofI γ2_fine])
      show "finite ?M2"
        by (rule fin_finite) (use p in blast)
      show "{k. x. (x, k)  ?M2} = cbox a b  {x. xk  c}"
        by (auto simp: ab_eqp)

      fix x L
      assume xL: "(x, L)  ?M2"
      then obtain x' L' where xL': "x = x'" "L = L'  {x. x  k  c}"
                                   "(x', L')  p" "L'  {x. x  k  c}  {}"
        by blast
      then obtain a' b' where ab': "L' = cbox a' b'"
        using p by blast
      show "x  L" "L  cbox a b  {x. x  k  c}"
        using p xk_ge_c xL' by auto
      show "a b. L = cbox a b"
        using p xL' ab' by (auto simp add: interval_split[OF k,where c=c])

      fix y R
      assume yR: "(y, R)  ?M2"
      then obtain y' R' where yR': "y = y'" "R = R'  {x. x  k  c}"
                                   "(y', R')  p" "R'  {x. x  k  c}  {}"
        by blast
      assume as: "(x, L)  (y, R)"
      show "interior L  interior R = {}"
      proof (cases "L' = R'  x' = y'")
        case False
        have "interior R' = {}"
          by (metis (no_types) False Pair_inject inf.idem tagged_division_ofD(5) [OF p] xL'(3) yR'(3))
        then show ?thesis
          using yR' by simp
      next
        case True
        then have "L'  R'"
          using as unfolding xL' yR' by auto
        have "interior L'  interior R' = {}"
          by (metis (no_types) Pair_inject L'  R' p tagged_division_ofD(5) xL'(3) yR'(3))
        then show ?thesis
          using xL'(2) yR'(2) by auto
      qed
    qed
    ultimately
    have "norm ((((x,K)  ?M1. content K *R f x) - i) + (((x,K)  ?M2. content K *R f x) - j)) < e/2 + e/2"
      using norm_add_less by blast
    moreover have "(((x,K)  ?M1. content K *R f x) - i) +
                   (((x,K)  ?M2. content K *R f x) - j) =
                   ((x, ka)p. content ka *R f x) - (i + j)"
    proof -
      have eq0: "x y. x = (0::real)  x *R (y::'b) = 0"
         by auto
      have cont_eq: "g. (λ(x,l). content l *R f x)  (λ(x,l). (x,g l)) = (λ(x,l). content (g l) *R f x)"
        by auto
      have *: "𝒢 :: 'a set  'a set.
                  ((x,K){(x, 𝒢 K) |x K. (x,K)  p  𝒢 K  {}}. content K *R f x) =
                  ((x,K)(λ(x,K). (x, 𝒢 K)) ` p. content K *R f x)"
        by (rule sum.mono_neutral_left) (auto simp: ‹finite p)
      have "(((x, k)?M1. content k *R f x) - i) + (((x, k)?M2. content k *R f x) - j) =
        ((x, k)?M1. content k *R f x) + ((x, k)?M2. content k *R f x) - (i + j)"
        by auto
      moreover have " = ((x,K)  p. content (K  {x. x  k  c}) *R f x) +
        ((x,K)  p. content (K  {x. c  x  k}) *R f x) - (i + j)"
        unfolding *
        apply (subst (1 2) sum.reindex_nontrivial)
           apply (auto intro!: k p eq0 tagged_division_split_left_inj_content tagged_division_split_right_inj_content
                       simp: cont_eq ‹finite p)
        done
      moreover have "x. x  p  (λ(a,B). content (B  {a. a  k  c}) *R f a) x +
                                (λ(a,B). content (B  {a. c  a  k}) *R f a) x =
                                (λ(a,B). content B *R f a) x"
      proof clarify
        fix a B
        assume "(a, B)  p"
        with p obtain u v where uv: "B = cbox u v" by blast
        then show "content (B  {x. x  k  c}) *R f a + content (B  {x. c  x  k}) *R f a = content B *R f a"
          by (auto simp: scaleR_left_distrib uv content_split[OF k,of u v c])
      qed
      ultimately show ?thesis
        by (auto simp: sum.distrib[symmetric])
      qed
    ultimately show "norm (((x, k)p. content k *R f x) - (i + j)) < e"
      by auto
  qed
qed


subsection ‹A sort of converse, integrability on subintervals›

lemma has_integral_separate_sides:
  fixes f :: "'a::euclidean_space  'b::real_normed_vector"
  assumes f: "(f has_integral i) (cbox a b)"
    and "e > 0"
    and k: "k  Basis"
  obtains d where "gauge d"
    "p1 p2. p1 tagged_division_of (cbox a b  {x. xk  c})  d fine p1 
        p2 tagged_division_of (cbox a b  {x. xk  c})  d fine p2 
        norm ((sum (λ(x,k). content k *R f x) p1 + sum (λ(x,k). content k *R f x) p2) - i) < e"
proof -
  obtain γ where d: "gauge γ"
      "p. p tagged_division_of cbox a b; γ fine p
             norm (((x, k)p. content k *R f x) - i) < e"
    using has_integralD[OF f e > 0] by metis
  { fix p1 p2
    assume tdiv1: "p1 tagged_division_of (cbox a b)  {x. x  k  c}" and "γ fine p1"
    note p1=tagged_division_ofD[OF this(1)] 
    assume tdiv2: "p2 tagged_division_of (cbox a b)  {x. c  x  k}" and "γ fine p2"
    note p2=tagged_division_ofD[OF this(1)] 
    note tagged_division_Un_interval[OF tdiv1 tdiv2] 
    note p12 = tagged_division_ofD[OF this] this
    { fix a b
      assume ab: "(a, b)  p1  p2"
      have "(a, b)  p1"
        using ab by auto
      obtain u v where uv: "b = cbox u v"
        using (a, b)  p1 p1(4) by moura
      have "b  {x. xk = c}"
        using ab p1(3)[of a b] p2(3)[of a b] by fastforce
      moreover
      have "interior {x::'a. x  k = c} = {}"
      proof (rule ccontr)
        assume "¬ ?thesis"
        then obtain x where x: "x  interior {x::'a. xk = c}"
          by auto
        then obtain ε where "0 < ε" and ε: "ball x ε  {x. x  k = c}"
          using mem_interior by metis
        have x: "xk = c"
          using x interior_subset by fastforce
        have *: "i. i  Basis  ¦(x - (x + (ε/2) *R k))  i¦ = (if i = k then ε/2 else 0)"
          using 0 < ε k by (auto simp: inner_simps inner_not_same_Basis)
        have "(iBasis. ¦(x - (x + (ε/2 ) *R k))  i¦) =
              (iBasis. (if i = k then ε/2 else 0))"
          using "*" by (blast intro: sum.cong)
        also have " < ε"
          by (subst sum.delta) (use 0 < ε in auto)
        finally have "x + (ε/2) *R k  ball x ε"
          unfolding mem_ball dist_norm by(rule le_less_trans[OF norm_le_l1])
        then have "x + (ε/2) *R k  {x. xk = c}"
          using ε by auto
        then show False
          using 0 < ε x k by (auto simp: inner_simps)
      qed
      ultimately have "content b = 0"
        unfolding uv content_eq_0_interior
        using interior_mono by blast
      then have "content b *R f a = 0"
        by auto
    }
    then have "norm (((x, k)p1. content k *R f x) + ((x, k)p2. content k *R f x) - i) =
               norm (((x, k)p1  p2. content k *R f x) - i)"
      by (subst sum.union_inter_neutral) (auto simp: p1 p2)
    also have " < e"
      using d(2) p12 by (simp add: fine_Un k γ fine p1 γ fine p2)
    finally have "norm (((x, k)p1. content k *R f x) + ((x, k)p2. content k *R f x) - i) < e" .
   }
  then show ?thesis
    using d(1) that by auto
qed

lemma integrable_split [intro]:
  fixes f :: "'a::euclidean_space  'b::{real_normed_vector,complete_space}"
  assumes f: "f integrable_on cbox a b"
      and k: "k  Basis"
    shows "f integrable_on (cbox a b  {x. xk  c})"   (is ?thesis1)
    and   "f integrable_on (cbox a b  {x. xk  c})"   (is ?thesis2)
proof -
  obtain y where y: "(f has_integral y) (cbox a b)"
    using f by blast
  define a' where "a' = (iBasis. (if i = k then max (ak) c else ai)*R i)"
  define b' where "b' = (iBasis. (if i = k then min (bk) c else bi)*R i)"
  have "d. gauge d 
            (p1 p2. p1 tagged_division_of cbox a b  {x. x  k  c}  d fine p1 
                     p2 tagged_division_of cbox a b  {x. x  k  c}  d fine p2 
                     norm (((x,K)  p1. content K *R f x) - ((x,K)  p2. content K *R f x)) < e)"
    if "e > 0" for e
  proof -
    have "e/2 > 0" using that by auto
  with has_integral_separate_sides[OF y this k, of c]
  obtain d
    where "gauge d"
         and d: "p1 p2. p1 tagged_division_of cbox a b  {x. x  k  c}; d fine p1;
                          p2 tagged_division_of cbox a b  {x. c  x  k}; d fine p2
                   norm (((x,K)p1. content K *R f x) + ((x,K)p2. content K *R f x) - y) < e/2"
    by metis
  show ?thesis
    proof (rule_tac x=d in exI, clarsimp simp add: ‹gauge d)
      fix p1 p2
      assume as: "p1 tagged_division_of (cbox a b)  {x. x  k  c}" "d fine p1"
                 "p2 tagged_division_of (cbox a b)  {x. x  k  c}" "d fine p2"
      show "norm (((x, k)p1. content k *R f x) - ((x, k)p2. content k *R f x)) < e"
      proof (rule fine_division_exists[OF ‹gauge d, of a' b])
        fix p
        assume "p tagged_division_of cbox a' b" "d fine p"
        then show ?thesis
          using as norm_triangle_half_l[OF d[of p1 p] d[of p2 p]]
          unfolding interval_split[OF k] b'_def[symmetric] a'_def[symmetric]
          by (auto simp add: algebra_simps)
      qed
    qed
  qed
  with f show ?thesis1
    by (simp add: interval_split[OF k] integrable_Cauchy)
  have "d. gauge d 
            (p1 p2. p1 tagged_division_of cbox a b  {x. x  k  c}  d fine p1 
                     p2 tagged_division_of cbox a b  {x. x  k  c}  d fine p2 
                     norm (((x,K)  p1. content K *R f x) - ((x,K)  p2. content K *R f x)) < e)"
    if "e > 0" for e
  proof -
    have "e/2 > 0" using that by auto
  with has_integral_separate_sides[OF y this k, of c]
  obtain d
    where "gauge d"
         and d: "p1 p2. p1 tagged_division_of cbox a b  {x. x  k  c}; d fine p1;
                          p2 tagged_division_of cbox a b  {x. c  x  k}; d fine p2
                   norm (((x,K)p1. content K *R f x) + ((x,K)p2. content K *R f x) - y) < e/2"
    by metis
  show ?thesis
    proof (rule_tac x=d in exI, clarsimp simp add: ‹gauge d)
      fix p1 p2
      assume as: "p1 tagged_division_of (cbox a b)  {x. x  k  c}" "d fine p1"
                 "p2 tagged_division_of (cbox a b)  {x. x  k  c}" "d fine p2"
      show "norm (((x, k)p1. content k *R f x) - ((x, k)p2. content k *R f x)) < e"
      proof (rule fine_division_exists[OF ‹gauge d, of a b'])
        fix p
        assume "p tagged_division_of cbox a b'" "d fine p"
        then show ?thesis
          using as norm_triangle_half_l[OF d[of p p1] d[of p p2]]
          unfolding interval_split[OF k] b'_def[symmetric] a'_def[symmetric]
          by (auto simp add: algebra_simps)
      qed
    qed
  qed
  with f show ?thesis2
    by (simp add: interval_split[OF k] integrable_Cauchy)
qed

lemma operative_integralI:
  fixes f :: "'a::euclidean_space  'b::banach"
  shows "operative (lift_option (+)) (Some 0)
    (λi. if f integrable_on i then Some (integral i f) else None)"
proof -
  interpret comm_monoid "lift_option plus" "Some (0::'b)"
    by (rule comm_monoid_lift_option)
      (rule add.comm_monoid_axioms)
  show ?thesis
  proof
    fix a b c
    fix k :: 'a
    assume k: "k  Basis"
    show "(if f integrable_on cbox a b then Some (integral (cbox a b) f) else None) =
          lift_option (+) (if f integrable_on cbox a b  {x. x  k  c} then Some (integral (cbox a b  {x. x  k  c}) f) else None)
          (if f integrable_on cbox a b  {x. c  x  k} then Some (integral (cbox a b  {x. c  x  k}) f) else None)"
    proof (cases "f integrable_on cbox a b")
      case True
      with k show ?thesis
        by (auto simp: integrable_split intro: integral_unique [OF has_integral_split[OF _ _ k]])
    next
    case False
      have "¬ (f integrable_on cbox a b  {x. x  k  c})  ¬ ( f integrable_on cbox a b  {x. c  x  k})"
      proof (rule ccontr)
        assume "¬ ?thesis"
        then have "f integrable_on cbox a b"
          unfolding integrable_on_def
          apply (rule_tac x="integral (cbox a b  {x. x  k  c}) f + integral (cbox a b  {x. x  k  c}) f" in exI)
          apply (auto intro: has_integral_split[OF _ _ k])
          done
        then show False
          using False by auto
      qed
      then show ?thesis
        using False by auto
    qed
  next
    fix a b :: 'a
    assume "box a b = {}"
    then show "(if f integrable_on cbox a b then Some (integral (cbox a b) f) else None) = Some 0"
      using has_integral_null_eq
      by (auto simp: integrable_on_null content_eq_0_interior)
  qed
qed

subsection ‹Bounds on the norm of Riemann sums and the integral itself›

lemma dsum_bound:
  assumes p: "p division_of (cbox a b)"
    and "norm c  e"
  shows "norm (sum (λl. content l *R c) p)  e * content(cbox a b)"
proof -
  have sumeq: "(ip. ¦content i¦) = sum content p"
    by simp
  have e: "0  e"
    using assms(2) norm_ge_zero order_trans by blast
  have "norm (sum (λl. content l *R c) p)  (ip. norm (content i *R c))"
    using norm_sum by blast
  also have "...   e * (ip. ¦content i¦)"
    by (simp add: sum_distrib_left[symmetric] mult.commute assms(2) mult_right_mono sum_nonneg)
  also have "...  e * content (cbox a b)"
    by (metis additive_content_division p eq_iff sumeq)
  finally show ?thesis .
qed

lemma rsum_bound:
  assumes p: "p tagged_division_of (cbox a b)"
      and "xcbox a b. norm (f x)  e"
    shows "norm (sum (λ(x,k). content k *R f x) p)  e * content (cbox a b)"
proof (cases "cbox a b = {}")
  case True show ?thesis
    using p unfolding True tagged_division_of_trivial by auto
next
  case False
  then have e: "e  0"
    by (meson ex_in_conv assms(2) norm_ge_zero order_trans)
  have sum_le: "sum (content  snd) p  content (cbox a b)"
    unfolding additive_content_tagged_division[OF p, symmetric] split_def
    by (auto intro: eq_refl)
  have con: "xk. xk  p  0  content (snd xk)"
    using tagged_division_ofD(4) [OF p] content_pos_le
    by force
  have "norm (sum (λ(x,k). content k *R f x) p)  (ip. norm (case i of (x, k)  content k *R f x))"
    by (rule norm_sum)
  also have "...   e * content (cbox a b)"
  proof -
    have "xk. xk  p  norm (f (fst xk))  e"
      using assms(2) p tag_in_interval by force
    moreover have "(ip. ¦content (snd i)¦ * e)  e * content (cbox a b)"
      unfolding sum_distrib_right[symmetric]
      using con sum_le by (auto simp: mult.commute intro: mult_left_mono [OF _ e])
    ultimately show ?thesis
      unfolding split_def norm_scaleR
      by (metis (no_types, lifting) mult_left_mono[OF _ abs_ge_zero]   order_trans[OF sum_mono])
  qed
  finally show ?thesis .
qed

lemma rsum_diff_bound:
  assumes "p tagged_division_of (cbox a b)"
    and "xcbox a b. norm (f x - g x)  e"
  shows "norm (sum (λ(x,k). content k *R f x) p - sum (λ(x,k). content k *R g x) p) 
         e * content (cbox a b)"
  using order_trans[OF _ rsum_bound[OF assms]]
  by (simp add: split_def scaleR_diff_right sum_subtractf eq_refl)

lemma has_integral_bound:
  fixes f :: "'a::euclidean_space  'b::real_normed_vector"
  assumes "0  B"
      and f: "(f has_integral i) (cbox a b)"
      and "x. xcbox a b  norm (f x)  B"
    shows "norm i  B * content (cbox a b)"
proof (rule ccontr)
  assume "¬ ?thesis"
  then have "norm i - B * content (cbox a b) > 0"
    by auto
  with f[unfolded has_integral]
  obtain γ where "gauge γ" and γ:
    "p. p tagged_division_of cbox a b; γ fine p
           norm (((x, K)p. content K *R f x) - i) < norm i - B * content (cbox a b)"
    by metis
  then obtain p where p: "p tagged_division_of cbox a b" and "γ fine p"
    using fine_division_exists by blast
  have "s B. norm s  B  ¬ norm (s - i) < norm i - B"
    unfolding not_less
    by (metis diff_left_mono dist_commute dist_norm norm_triangle_ineq2 order_trans)
  then show False
    using γ [OF p γ fine p] rsum_bound[OF p] assms by metis
qed

corollary integrable_bound:
  fixes f :: "'a::euclidean_space  'b::real_normed_vector"
  assumes "0  B"
      and "f integrable_on (cbox a b)"
      and "x. xcbox a b  norm (f x)  B"
    shows "norm (integral (cbox a b) f)  B * content (cbox a b)"
by (metis integrable_integral has_integral_bound assms)


subsection ‹Similar theorems about relationship among components›

lemma rsum_component_le:
  fixes f :: "'a::euclidean_space  'b::euclidean_space"
  assumes p: "p tagged_division_of (cbox a b)"
      and "x. x  cbox a b  (f x)i  (g x)i"
    shows "((x, K)p. content K *R f x)  i  ((x, K)p. content K *R g x)  i"
unfolding inner_sum_left
proof (rule sum_mono, clarify)
  fix x K
  assume ab: "(x, K)  p"
  with p obtain u v where K: "K = cbox u v"
    by blast
  then show "(content K *R f x)  i  (content K *R g x)  i"
    by (metis ab assms inner_scaleR_left measure_nonneg mult_left_mono tag_in_interval)
qed

lemma has_integral_component_le:
  fixes f g :: "'a::euclidean_space  'b::euclidean_space"
  assumes k: "k  Basis"
  assumes "(f has_integral i) S" "(g has_integral j) S"
    and f_le_g: "x. x  S  (f x)k  (g x)k"
  shows "ik  jk"
proof -
  have ik_le_jk: "ik  jk"
    if f_i: "(f has_integral i) (cbox a b)"
    and g_j: "(g has_integral j) (cbox a b)"
    and le: "xcbox a b. (f x)k  (g x)k"
    for a b i and j :: 'b and f g :: "'a  'b"
  proof (rule ccontr)
    assume "¬ ?thesis"
    then have *: "0 < (ik - jk) / 3"
      by auto
    obtain γ1 where "gauge γ1" 
      and γ1: "p. p tagged_division_of cbox a b; γ1 fine p
                 norm (((x, k)p. content k *R f x) - i) < (i  k - j  k) / 3"
      using f_i[unfolded has_integral,rule_format,OF *] by fastforce 
    obtain γ2 where "gauge γ2" 
      and γ2: "p. p tagged_division_of cbox a b; γ2 fine p
                 norm (((x, k)p. content k *R g x) - j) < (i  k - j  k) / 3"
      using g_j[unfolded has_integral,rule_format,OF *] by fastforce 
    obtain p where p: "p tagged_division_of cbox a b" and "γ1 fine p" "γ2 fine p"
       using fine_division_exists[OF gauge_Int[OF ‹gauge γ1 ‹gauge γ2], of a b] unfolding fine_Int
       by metis
    then have "¦(((x, k)p. content k *R f x) - i)  k¦ < (i  k - j  k) / 3"
         "¦(((x, k)p. content k *R g x) - j)  k¦ < (i  k - j  k) / 3"
      using le_less_trans[OF Basis_le_norm[OF k]] k γ1 γ2 by metis+ 
    then show False
      unfolding inner_simps
      using rsum_component_le[OF p] le
      by (fastforce simp add: abs_real_def split: if_split_asm)
  qed
  show ?thesis
  proof (cases "a b. S = cbox a b")
    case True
    with ik_le_jk assms show ?thesis
      by auto
  next
    case False
    show ?thesis
    proof (rule ccontr)
      assume "¬ ik  jk"
      then have ij: "(ik - jk) / 3 > 0"
        by auto
      obtain B1 where "0 < B1" 
           and B1: "a b. ball 0 B1  cbox a b 
                    z. ((λx. if x  S then f x else 0) has_integral z) (cbox a b) 
                        norm (z - i) < (i  k - j  k) / 3"
        using has_integral_altD[OF _ False ij] assms by blast
      obtain B2 where "0 < B2" 
           and B2: "a b. ball 0 B2  cbox a b 
                    z. ((λx. if x  S then g x else 0) has_integral z) (cbox a b) 
                        norm (z - j) < (i  k - j  k) / 3"
        using has_integral_altD[OF _ False ij] assms by blast
      have "bounded (ball 0 B1  ball (0::'a) B2)"
        unfolding bounded_Un by(rule conjI bounded_ball)+
      from bounded_subset_cbox_symmetric[OF this] 
      obtain a b::'a where ab: "ball 0 B1  cbox a b" "ball 0 B2  cbox a b"
        by (meson Un_subset_iff)
      then obtain w1 w2 where int_w1: "((λx. if x  S then f x else 0) has_integral w1) (cbox a b)"
                        and norm_w1:  "norm (w1 - i) < (i  k - j  k) / 3"
                        and int_w2: "((λx. if x  S then g x else 0) has_integral w2) (cbox a b)"
                        and norm_w2: "norm (w2 - j) < (i  k - j  k) / 3"
        using B1 B2 by blast
      have *: "w1 w2 j i::real .¦w1 - i¦ < (i - j) / 3  ¦w2 - j¦ < (i - j) / 3  w1  w2  False"
        by (simp add: abs_real_def split: if_split_asm)
      have "¦(w1 - i)  k¦ < (i  k - j  k) / 3"
           "¦(w2 - j)  k¦ < (i  k - j  k) / 3"
        using Basis_le_norm k le_less_trans norm_w1 norm_w2 by blast+
      moreover
      have "w1k  w2k"
        using ik_le_jk int_w1 int_w2 f_le_g by auto
      ultimately show False
        unfolding inner_simps by(rule *)
    qed
  qed
qed

lemma integral_component_le:
  fixes g f :: "'a::euclidean_space  'b::euclidean_space"
  assumes "k  Basis"
    and "f integrable_on S" "g integrable_on S"
    and "x. x  S  (f x)k  (g x)k"
  shows "(integral S f)k  (integral S g)k"
  using has_integral_component_le assms by blast

lemma has_integral_component_nonneg:
  fixes f :: "'a::euclidean_space  'b::euclidean_space"
  assumes "k  Basis"
    and "(f has_integral i) S"
    and "x. x  S  0  (f x)k"
  shows "0  ik"
  using has_integral_component_le[OF assms(1) has_integral_0 assms(2)]
  using assms(3-)
  by auto

lemma integral_component_nonneg:
  fixes f :: "'a::euclidean_space  'b::euclidean_space"
  assumes "k  Basis"
    and  "x. x  S  0  (f x)k"
  shows "0  (integral S f)k"
proof (cases "f integrable_on S")
  case True show ?thesis
    using True assms has_integral_component_nonneg by blast
next
  case False then show ?thesis by (simp add: not_integrable_integral)
qed

lemma has_integral_component_neg:
  fixes f :: "'a::euclidean_space  'b::euclidean_space"
  assumes "k  Basis"
    and "(f has_integral i) S"
    and "x. x  S  (f x)k  0"
  shows "ik  0"
  using has_integral_component_le[OF assms(1,2) has_integral_0] assms(2-)
  by auto

lemma has_integral_component_lbound:
  fixes f :: "'a::euclidean_space  'b::euclidean_space"
  assumes "(f has_integral i) (cbox a b)"
    and "xcbox a b. B  f(x)k"
    and "k  Basis"
  shows "B * content (cbox a b)  ik"
  using has_integral_component_le[OF assms(3) has_integral_const assms(1),of "(iBasis. B *R i)::'b"] assms(2-)
  by (auto simp add: field_simps)

lemma has_integral_component_ubound:
  fixes f::"'a::euclidean_space => 'b::euclidean_space"
  assumes "(f has_integral i) (cbox a b)"
    and "xcbox a b. f xk  B"
    and "k  Basis"
  shows "ik  B * content (cbox a b)"
  using has_integral_component_le[OF assms(3,1) has_integral_const, of "iBasis. B *R i"] assms(2-)
  by (auto simp add: field_simps)

lemma integral_component_lbound:
  fixes f :: "'a::euclidean_space  'b::euclidean_space"
  assumes "f integrable_on cbox a b"
    and "xcbox a b. B  f(x)k"
    and "k  Basis"
  shows "B * content (cbox a b)  (integral(cbox a b) f)k"
  using assms has_integral_component_lbound by blast

lemma integral_component_lbound_real:
  assumes "f integrable_on {a ::real..b}"
    and "x{a..b}. B  f(x)k"
    and "k  Basis"
  shows "B * content {a..b}  (integral {a..b} f)k"
  using assms
  by (metis box_real(2) integral_component_lbound)

lemma integral_component_ubound:
  fixes f :: "'a::euclidean_space  'b::euclidean_space"
  assumes "f integrable_on cbox a b"
    and "xcbox a b. f xk  B"
    and "k  Basis"
  shows "(integral (cbox a b) f)k  B * content (cbox a b)"
  using assms has_integral_component_ubound by blast

lemma integral_component_ubound_real:
  fixes f :: "real  'a::euclidean_space"
  assumes "f integrable_on {a..b}"
    and "x{a..b}. f xk  B"
    and "k  Basis"
  shows "(integral {a..b} f)k  B * content {a..b}"
  using assms
  by (metis box_real(2) integral_component_ubound)

subsection ‹Uniform limit of integrable functions is integrable›

lemma real_arch_invD:
  "0 < (e::real)  (n::nat. n  0  0 < inverse (real n)  inverse (real n) < e)"
  by (subst(asm) real_arch_inverse)


lemma integrable_uniform_limit:
  fixes f :: "'a::euclidean_space  'b::banach"
  assumes "e. e > 0  g. (xcbox a b. norm (f x - g x)  e)  g integrable_on cbox a b"
  shows "f integrable_on cbox a b"
proof (cases "content (cbox a b) > 0")
  case False then show ?thesis
    using has_integral_null by (simp add: content_lt_nz integrable_on_def)
next
  case True
  have "1 / (real n + 1) > 0" for n
    by auto
  then have "g. (xcbox a b. norm (f x - g x)  1 / (real n + 1))  g integrable_on cbox a b" for n
    using assms by blast
  then obtain g where g_near_f: "n x. x  cbox a b  norm (f x - g n x)  1 / (real n + 1)"
                  and int_g: "n. g n integrable_on cbox a b"
    by metis
  then obtain h where h: "n. (g n has_integral h n) (cbox a b)"
    unfolding integrable_on_def by metis
  have "Cauchy h"
    unfolding Cauchy_def
  proof clarify
    fix e :: real
    assume "e>0"
    then have "e/4 / content (cbox a b) > 0"
      using True by (auto simp: field_simps)
    then obtain M where "M  0" and M: "1 / (real M) < e/4 / content (cbox a b)"
      by (metis inverse_eq_divide real_arch_inverse)
    show "M. mM. nM. dist (h m) (h n) < e"
    proof (rule exI [where x=M], clarify)
      fix m n
      assume m: "M  m" and n: "M  n"
      have "e/4>0" using e>0 by auto
      then obtain gm gn where "gauge gm" "gauge gn"
              and gm: "𝒟. 𝒟 tagged_division_of cbox a b  gm fine 𝒟 
                             norm (((x,K)  𝒟. content K *R g m x) - h m) < e/4"
              and gn: "𝒟. 𝒟 tagged_division_of cbox a b  gn fine 𝒟 
                      norm (((x,K)  𝒟. content K *R g n x) - h n) < e/4"
        using h[unfolded has_integral] by meson
      then obtain 𝒟 where 𝒟: "𝒟 tagged_division_of cbox a b" "(λx. gm x  gn x) fine 𝒟"
        by (metis (full_types) fine_division_exists gauge_Int)
      have triangle3: "norm (i1 - i2) < e"
        if no: "norm(s2 - s1)  e/2" "norm (s1 - i1) < e/4" "norm (s2 - i2) < e/4" for s1 s2 i1 and i2::'b
      proof -
        have "norm (i1 - i2)  norm (i1 - s1) + norm (s1 - s2) + norm (s2 - i2)"
          using norm_triangle_ineq[of "i1 - s1" "s1 - i2"]
          using norm_triangle_ineq[of "s1 - s2" "s2 - i2"]
          by (auto simp: algebra_simps)
        also have " < e"
          using no by (auto simp: algebra_simps norm_minus_commute)
        finally show ?thesis .
      qed
      have finep: "gm fine 𝒟" "gn fine 𝒟"
        using fine_Int 𝒟  by auto
      have norm_le: "norm (g n x - g m x)  2 / real M" if x: "x  cbox a b" for x
      proof -
        have "norm (f x - g n x) + norm (f x - g m x)  1 / (real n + 1) + 1 / (real m + 1)"          
          using g_near_f[OF x, of n] g_near_f[OF x, of m] by simp
        also have "  1 / (real M) + 1 / (real M)"
          using M  0 m n by (intro add_mono; force simp: field_split_simps)
        also have " = 2 / real M"
          by auto
        finally show "norm (g n x - g m x)  2 / real M"
          using norm_triangle_le[of "g n x - f x" "f x - g m x" "2 / real M"]
          by (auto simp: algebra_simps simp add: norm_minus_commute)
      qed
      have "norm (((x,K)  𝒟. content K *R g n x) - ((x,K)  𝒟. content K *R g m x))  2 / real M * content (cbox a b)"
        by (blast intro: norm_le rsum_diff_bound[OF 𝒟(1), where e="2 / real M"])
      also have "...  e/2"
        using M True
        by (auto simp: field_simps)
      finally have le_e2: "norm (((x,K)  𝒟. content K *R g n x) - ((x,K)  𝒟. content K *R g m x))  e/2" .
      then show "dist (h m) (h n) < e"
        unfolding dist_norm using gm gn 𝒟 finep by (auto intro!: triangle3)
    qed
  qed
  then obtain s where s: "h  s"
    using convergent_eq_Cauchy[symmetric] by blast
  show ?thesis
    unfolding integrable_on_def has_integral
  proof (rule_tac x=s in exI, clarify)
    fix e::real
    assume e: "0 < e"
    then have "e/3 > 0" by auto
    then obtain N1 where N1: "nN1. norm (h n - s) < e/3"
      using LIMSEQ_D [OF s] by metis
    from e True have "e/3 / content (cbox a b) > 0"
      by (auto simp: field_simps)
    then obtain N2 :: nat
         where "N2  0" and N2: "1 / (real N2) < e/3 / content (cbox a b)"
      by (metis inverse_eq_divide real_arch_inverse)
    obtain g' where "gauge g'"
            and g': "𝒟. 𝒟 tagged_division_of cbox a b  g' fine 𝒟 
                    norm (((x,K)  𝒟. content K *R g (N1 + N2) x) - h (N1 + N2)) < e/3"
      by (metis h has_integral e/3 > 0)
    have *: "norm (sf - s) < e" 
        if no: "norm (sf - sg)  e/3" "norm(h - s) < e/3" "norm (sg - h) < e/3" for sf sg h
    proof -
      have "norm (sf - s)  norm (sf - sg) + norm (sg - h) + norm (h - s)"
        using norm_triangle_ineq[of "sf - sg" "sg - s"]
        using norm_triangle_ineq[of "sg -  h" " h - s"]
        by (auto simp: algebra_simps)
      also have " < e"
        using no by (auto simp: algebra_simps norm_minus_commute)
      finally show ?thesis .
    qed
    { fix 𝒟
      assume ptag: "𝒟 tagged_division_of (cbox a b)" and "g' fine 𝒟"
      then have norm_less: "norm (((x,K)  𝒟. content K *R g (N1 + N2) x) - h (N1 + N2)) < e/3"
        using g' by blast
      have "content (cbox a b) < e/3 * (of_nat N2)"
        using N2  0 N2 using True by (auto simp: field_split_simps)
      moreover have "e/3 * of_nat N2  e/3 * (of_nat (N1 + N2) + 1)"
        using e>0 by auto
      ultimately have "content (cbox a b) < e/3 * (of_nat (N1 + N2) + 1)"
        by linarith
      then have le_e3: "1 / (real (N1 + N2) + 1) * content (cbox a b)  e/3"
        unfolding inverse_eq_divide
        by (auto simp: field_simps)
      have ne3: "norm (h (N1 + N2) - s) < e/3"
        using N1 by auto
      have "norm (((x,K)  𝒟. content K *R f x) - ((x,K)  𝒟. content K *R g (N1 + N2) x))
             1 / (real (N1 + N2) + 1) * content (cbox a b)"
        by (blast intro: g_near_f rsum_diff_bound[OF ptag])
      then have "norm (((x,K)  𝒟. content K *R f x) - s) < e"
        by (rule *[OF order_trans [OF _ le_e3] ne3 norm_less])
    }
    then show "d. gauge d 
             (𝒟. 𝒟 tagged_division_of cbox a b  d fine 𝒟  norm (((x,K)  𝒟. content K *R f x) - s) < e)"
      by (blast intro: g' ‹gauge g')
  qed
qed

lemmas integrable_uniform_limit_real = integrable_uniform_limit [where 'a=real, simplified]


subsection ‹Negligible sets›

definition "negligible (s:: 'a::euclidean_space set) 
  (a b. ((indicator s :: 'areal) has_integral 0) (cbox a b))"


subsubsection ‹Negligibility of hyperplane›

lemma content_doublesplit:
  fixes a :: "'a::euclidean_space"
  assumes "0 < e"
    and k: "k  Basis"
  obtains d where "0 < d" and "content (cbox a b  {x. ¦xk - c¦  d}) < e"
proof cases
  assume *: "a  k  c  c  b  k  (jBasis. a  j  b  j)"
  define a' where "a' d = (jBasis. (if j = k then max (aj) (c - d) else aj) *R j)" for d
  define b' where "b' d = (jBasis. (if j = k then min (bj) (c + d) else bj) *R j)" for d

  have "((λd. jBasis. (b' d - a' d)  j)  (jBasis. (b' 0 - a' 0)  j)) (at_right 0)"
    by (auto simp: b'_def a'_def intro!: tendsto_min tendsto_max tendsto_eq_intros)
  also have "(jBasis. (b' 0 - a' 0)  j) = 0"
    using k *
    by (intro prod_zero bexI[OF _ k])
       (auto simp: b'_def a'_def inner_diff inner_sum_left inner_not_same_Basis intro!: sum.cong)
  also have "((λd. jBasis. (b' d - a' d)  j)  0) (at_right 0) =
    ((λd. content (cbox a b  {x. ¦xk - c¦  d}))  0) (at_right 0)"
  proof (intro tendsto_cong eventually_at_rightI)
    fix d :: real assume d: "d  {0<..<1}"
    have "cbox a b  {x. ¦xk - c¦  d} = cbox (a' d) (b' d)" for d
      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)
    moreover have "j  Basis  a' d  j  b' d  j" for j
      using * d k by (auto simp: a'_def b'_def)
    ultimately show "(jBasis. (b' d - a' d)  j) = content (cbox a b  {x. ¦xk - c¦  d})"
      by simp
  qed simp
  finally have "((λd. content (cbox a b  {x. ¦x  k - c¦  d}))  0) (at_right 0)" .
  from order_tendstoD(2)[OF this 0<e]
  obtain d' where "0 < d'" and d': "y. y > 0  y < d'  content (cbox a b  {x. ¦x  k - c¦  y}) < e"
    by (subst (asm) eventually_at_right[of _ 1]) auto
  show ?thesis
    by (rule that[of "d'/2"], insert 0<d' d'[of "d'/2"], auto)
next
  assume *: "¬ (a  k  c  c  b  k  (jBasis. a  j  b  j))"
  then have "(jBasis. b  j < a  j)  (c < a  k  b  k < c)"
    by (auto simp: not_le)
  show thesis
  proof cases
    assume "jBasis. b  j < a  j"
    then have [simp]: "cbox a b = {}"
      using box_ne_empty(1)[of a b] by auto
    show ?thesis
      by (rule that[of 1]) (simp_all add: 0<e)
  next
    assume "¬ (jBasis. b  j < a  j)"
    with * have "c < a  k  b  k < c"
      by auto
    then show thesis
    proof
      assume c: "c < a  k"
      moreover have "x  cbox a b  c  x  k" for x
        using k c by (auto simp: cbox_def)
      ultimately have "cbox a b  {x. ¦x  k - c¦  (a  k - c)/2} = {}"
        using k by (auto simp: cbox_def)
      with 0<e c that[of "(a  k - c)/2"] show ?thesis
        by auto
    next
      assume c: "b  k < c"
      moreover have "x  cbox a b  x  k  c" for x
        using k c by (auto simp: cbox_def)
      ultimately have "cbox a b  {x. ¦x  k - c¦  (c - b  k)/2} = {}"
        using k by (auto simp: cbox_def)
      with 0<e c that[of "(c - b  k)/2"] show ?thesis
        by auto
    qed
  qed
qed


proposition negligible_standard_hyperplane[intro]:
  fixes k :: "'a::euclidean_space"
  assumes k: "k  Basis"
  shows "negligible {x. xk = c}"
  unfolding negligible_def has_integral
proof clarsimp
  fix a b and e::real assume "e > 0"
  with k obtain d where "0 < d" and d: "content (cbox a b  {x. ¦x  k - c¦  d}) < e"
    by (metis content_doublesplit)
  let ?i = "indicator {x::'a. xk = c} :: 'areal"
  show "γ. gauge γ 
           (𝒟. 𝒟 tagged_division_of cbox a b  γ fine 𝒟 
                 ¦(x,K)  𝒟. content K * ?i x¦ < e)"
  proof (intro exI, safe)
    show "gauge (λx. ball x d)"
      using 0 < d by blast
  next
    fix 𝒟
    assume p: "𝒟 tagged_division_of (cbox a b)" "(λx. ball x d) fine 𝒟"
    have "content L = content (L  {x. ¦x  k - c¦  d})" 
      if "(x, L)  𝒟" "?i x  0" for x L
    proof -
      have xk: "xk = c"
        using that by (simp add: indicator_def split: if_split_asm)
      have "L  {x. ¦x  k - c¦  d}"
      proof 
        fix y
        assume y: "y  L"
        have "L  ball x d"
          using p(2) that(1) by auto
        then have "norm (x - y) < d"
          by (simp add: dist_norm subset_iff y)
        then have "¦(x - y)  k¦ < d"
          using k norm_bound_Basis_lt by blast
        then show "y  {x. ¦x  k - c¦  d}"
          unfolding inner_simps xk by auto
      qed 
      then show "content L = content (L  {x. ¦x  k - c¦  d})"
        by (metis inf.orderE)
    qed
    then have *: "((x,K)𝒟. content K * ?i x) = ((x,K)𝒟. content (K  {x. ¦xk - c¦  d}) *R ?i x)"
      by (force simp add: split_paired_all intro!: sum.cong [OF refl])
    note p'= tagged_division_ofD[OF p(1)] and p''=division_of_tagged_division[OF p(1)]
    have "((x,K)𝒟. content (K  {x. ¦x  k - c¦  d}) * indicator {x. x  k = c} x) < e"
    proof -
      have "((x,K)𝒟. content (K  {x. ¦x  k - c¦  d}) * ?i x)  ((x,K)𝒟. content (K  {x. ¦x  k - c¦  d}))"
        by (force simp add: indicator_def intro!: sum_mono)
      also have " < e"
      proof (subst sum.over_tagged_division_lemma[OF p(1)])
        fix u v::'a
        assume "box u v = {}"
        then have *: "content (cbox u v) = 0"
          unfolding content_eq_0_interior by simp
        have "cbox u v  {x. ¦x  k - c¦  d}  cbox u v"
          by auto
        then have "content (cbox u v  {x. ¦x  k - c¦  d})  content (cbox u v)"
          unfolding interval_doublesplit[OF k] by (rule content_subset)
        then show "content (cbox u v  {x. ¦x  k - c¦  d}) = 0"
          unfolding * interval_doublesplit[OF k]
          by (blast intro: antisym)
      next
        have "(lsnd ` 𝒟. content (l  {x. ¦x  k - c¦  d})) =
          sum content ((λl. l  {x. ¦x  k - c¦  d})`{lsnd ` 𝒟. l  {x. ¦x  k - c¦  d}  {}})"
        proof (subst (2) sum.reindex_nontrivial)
          fix x y assume "x  {l  snd ` 𝒟. l  {x. ¦x  k - c¦  d}  {}}" "y  {l  snd ` 𝒟. l  {x. ¦x  k - c¦  d}  {}}"
            "x  y" and eq: "x  {x. ¦x  k - c¦  d} = y  {x. ¦x  k - c¦  d}"
          then obtain x' y' where "(x', x)  𝒟" "x  {x. ¦x  k - c¦  d}  {}" "(y', y)  𝒟" "y  {x. ¦x  k - c¦  d}  {}"
            by (auto)
          from p'(5)[OF (x', x)  𝒟 (y', y)  𝒟] x  y have "interior (x  y) = {}"
            by auto
          moreover have "interior ((x  {x. ¦x  k - c¦  d})  (y  {x. ¦x  k - c¦  d}))  interior (x  y)"
            by (auto intro: interior_mono)
          ultimately have "interior (x  {x. ¦x  k - c¦  d}) = {}"
            by (auto simp: eq)
          then show "content (x  {x. ¦x  k - c¦  d}) = 0"
            using p'(4)[OF (x', x)  𝒟] by (auto simp: interval_doublesplit[OF k] content_eq_0_interior simp del: interior_Int)
        qed (insert p'(1), auto intro!: sum.mono_neutral_right)
        also have "  norm (l(λl. l  {x. ¦x  k - c¦  d})`{lsnd ` 𝒟. l  {x. ¦x  k - c¦  d}  {}}. content l *R 1::real)"
          by simp
        also have "  1 * content (cbox a b  {x. ¦x  k - c¦  d})"
          using division_doublesplit[OF p'' k, unfolded interval_doublesplit[OF k]]
          unfolding interval_doublesplit[OF k] by (intro dsum_bound) auto
        also have " < e"
          using d by simp
        finally show "(Ksnd ` 𝒟. content (K  {x. ¦x  k - c¦  d})) < e" .
      qed
      finally show "((x, K)𝒟. content (K  {x. ¦x  k - c¦  d}) * ?i x) < e" .
    qed
    then show "¦(x, K)𝒟. content K * ?i x¦ < e"
      unfolding *  by (simp add: sum_nonneg split: prod.split)
  qed
qed

corollary negligible_standard_hyperplane_cart:
  fixes k :: "'a::finite"
  shows "negligible {x. x$k = (0::real)}"
  by (simp add: cart_eq_inner_axis negligible_standard_hyperplane)


subsubsection ‹Hence the main theorem about negligible sets›


lemma has_integral_negligible_cbox:
  fixes f :: "'b::euclidean_space  'a::real_normed_vector"
  assumes negs: "negligible S"
    and 0: "x. x  S  f x = 0"
  shows "(f has_integral 0) (cbox a b)"
  unfolding has_integral
proof clarify
  fix e::real
  assume "e > 0"
  then have nn_gt0: "e/2 / ((real n+1) * (2 ^ n)) > 0" for n
    by simp
  then have "γ. gauge γ 
                 (𝒟. 𝒟 tagged_division_of cbox a b  γ fine 𝒟 
                      ¦(x,K)  𝒟. content K *R indicator S x¦
                      < e/2 / ((real n + 1) * 2 ^ n))" for n
    using negs [unfolded negligible_def has_integral] by auto
  then obtain γ where 
    gd: "n. gauge (γ n)"
    and γ: "n 𝒟. 𝒟 tagged_division_of cbox a b; γ n fine 𝒟
                   ¦(x,K)  𝒟. content K *R indicator S x¦ < e/2 / ((real n + 1) * 2 ^ n)"
    by metis
  show "γ. gauge γ 
             (𝒟. 𝒟 tagged_division_of cbox a b  γ fine 𝒟 
                  norm (((x,K)  𝒟. content K *R f x) - 0) < e)"
  proof (intro exI, safe)
    show "gauge (λx. γ (nat norm (f x)) x)"
      using gd by (auto simp: gauge_def)

    show "norm (((x,K)  𝒟. content K *R f x) - 0) < e"
      if "𝒟 tagged_division_of (cbox a b)" "(λx. γ (nat norm (f x)) x) fine 𝒟" for 𝒟
    proof (cases "𝒟 = {}")
      case True with 0 < e show ?thesis by simp
    next
      case False
      obtain N where "Max ((λ(x, K). norm (f x)) ` 𝒟)  real N"
        using real_arch_simple by blast
      then have N: "x. x  (λ(x, K). norm (f x)) ` 𝒟  x  real N"
        by (meson Max_ge that(1) dual_order.trans finite_imageI tagged_division_of_finite)
      have "i. q. q tagged_division_of (cbox a b)  (γ i) fine q  ((x,K)  𝒟. K  (γ i) x  (x, K)  q)"
        by (auto intro: tagged_division_finer[OF that(1) gd])
      from choice[OF this] 
      obtain q where q: "n. q n tagged_division_of cbox a b"
                        "n. γ n fine q n"
                        "n x K. (x, K)  𝒟; K  γ n x  (x, K)  q n"
        by fastforce
      have "finite 𝒟"
        using that(1) by blast
      then have sum_le_inc: "finite T; x y. (x,y)  T  (0::real)  g(x,y);
                      y. y𝒟  x. (x,y)  T  f(y)  g(x,y)  sum f 𝒟  sum g T" for f g T
        by (rule sum_le_included[of 𝒟 T g snd f]; force)
      have "norm ((x,K)  𝒟. content K *R f x)  ((x,K)  𝒟. norm (content K *R f x))"
        unfolding split_def by (rule norm_sum)
      also have "...  ((i, j)  Sigma {..N + 1} q.
                          (real i + 1) * (case j of (x, K)  content K *R indicator S x))"
      proof (rule sum_le_inc, safe)
        show "finite (Sigma {..N+1} q)"
          by (meson finite_SigmaI finite_atMost tagged_division_of_finite q(1)) 
      next
        fix x K
        assume xk: "(x, K)  𝒟"
        define n where "n = nat norm (f x)"
        have *: "norm (f x)  (λ(x, K). norm (f x)) ` 𝒟"
          using xk by auto
        have nfx: "real n  norm (f x)" "norm (f x)  real n + 1"
          unfolding n_def by auto
        then have "n  {0..N + 1}"
          using N[OF *] by auto
        moreover have "K  γ (nat norm (f x)) x"
          using that(2) xk by auto
        moreover then have "(x, K)  q (nat norm (f x))"
          by (simp add: q(3) xk)
        moreover then have "(x, K)  q n"
          using n_def by blast
        moreover
        have "norm (content K *R f x)  (real n + 1) * (content K * indicator S x)"
        proof (cases "x  S")
          case False
          then show ?thesis by (simp add: 0)
        next
          case True
          have *: "content K  0"
            using tagged_division_ofD(4)[OF that(1) xk] by auto
          moreover have "content K * norm (f x)  content K * (real n + 1)"
            by (simp add: mult_left_mono nfx(2))
          ultimately show ?thesis
            using nfx True by (auto simp: field_simps)
        qed
        ultimately show "y. (y, x, K)  (Sigma {..N + 1} q)  norm (content K *R f x) 
          (real y + 1) * (content K *R indicator S x)"
          by force
      qed auto
      also have "... = (iN + 1. jq i. (real i + 1) * (case j of (x, K)  content K *R indicator S x))"
        using q(1) by (intro sum_Sigma_product [symmetric]) auto
      also have "...  (iN + 1. (real i + 1) * ¦(x,K)  q i. content K *R indicator S x¦)"
        by (rule sum_mono) (simp add: sum_distrib_left [symmetric])
      also have "...  (iN + 1. e/2/2 ^ i)"
      proof (rule sum_mono)
        show "(real i + 1) * ¦(x,K)  q i. content K *R indicator S x¦  e/2/2 ^ i"
          if "i  {..N + 1}" for i
          using γ[of "q i" i] q by (simp add: divide_simps mult.left_commute)
      qed
      also have "... = e/2 * (iN + 1. (1/2) ^ i)"
        unfolding sum_distrib_left by (metis divide_inverse inverse_eq_divide power_one_over)
      also have " < e/2 * 2"
      proof (rule mult_strict_left_mono)
        have "sum (power (1/2)) {..N + 1} = sum (power (1/2::real)) {..<N + 2}"
          using lessThan_Suc_atMost by auto
        also have "... < 2"
          by (auto simp: geometric_sum)
        finally show "sum (power (1/2::real)) {..N + 1} < 2" .
      qed (use 0 < e in auto)
      finally  show ?thesis by auto
    qed
  qed
qed


proposition has_integral_negligible:
  fixes f :: "'b::euclidean_space  'a::real_normed_vector"
  assumes negs: "negligible S"
    and "x. x  (T - S)  f x = 0"
  shows "(f has_integral 0) T"
proof (cases "a b. T = cbox a b")
  case True
  then have "((λx. if x  T then f x else 0) has_integral 0) T"
    using assms by (auto intro!: has_integral_negligible_cbox)
  then show ?thesis
    by (rule has_integral_eq [rotated]) auto
next
  case False
  let ?f = "(λx. if x  T then f x else 0)"
  have "((λx. if x  T then f x else 0) has_integral 0) T"
    apply (auto simp: False has_integral_alt [of ?f])
    apply (rule_tac x=1 in exI, auto)
    apply (rule_tac x=0 in exI, simp add: has_integral_negligible_cbox [OF negs] assms)
    done
  then show ?thesis
    by (rule_tac f="?f" in has_integral_eq) auto
qed

lemma
  assumes "negligible S"
  shows integrable_negligible: "f integrable_on S" and integral_negligible: "integral S f = 0"
  using has_integral_negligible [OF assms]
  by (auto simp: has_integral_iff)

lemma has_integral_spike:
  fixes f :: "'b::euclidean_space  'a::real_normed_vector"
  assumes "negligible S"
    and gf: "x. x  T - S  g x = f x"
    and fint: "(f has_integral y) T"
  shows "(g has_integral y) T"
proof -
  have *: "(g has_integral y) (cbox a b)"
       if "(f has_integral y) (cbox a b)" "x  cbox a b - S. g x = f x" for a b f and g:: "'b  'a" and y
  proof -
    have "((λx. f x + (g x - f x)) has_integral (y + 0)) (cbox a b)"
      using that by (intro has_integral_add has_integral_negligible) (auto intro!: ‹negligible S)
    then show ?thesis
      by auto
  qed
  have §: "a b z. x. x  T  x  S  g x = f x;
                     ((λx. if x  T then f x else 0) has_integral z) (cbox a b)
                     ((λx. if x  T then g x else 0) has_integral z) (cbox a b)"
      by (auto dest!: *[where f="λx. if xT then f x else 0" and g="λx. if x  T then g x else 0"])
  show ?thesis
    using fint gf
    apply (subst has_integral_alt)
    apply (subst (asm) has_integral_alt)
    apply (auto split: if_split_asm)
     apply (blast dest: *)
    using § by meson
qed

lemma has_integral_spike_eq:
  assumes "negligible S"
    and gf: "x. x  T - S  g x = f x"
  shows "(f has_integral y) T  (g has_integral y) T"
    using has_integral_spike [OF ‹negligible S] gf
    by metis

lemma integrable_spike:
  assumes "f integrable_on T" "negligible S" "x. x  T - S  g x = f x"
    shows "g integrable_on T"
  using assms unfolding integrable_on_def by (blast intro: has_integral_spike)

lemma integral_spike:
  assumes "negligible S"
    and "x. x  T - S  g x = f x"
  shows "integral T f = integral T g"
  using has_integral_spike_eq[OF assms]
    by (auto simp: integral_def integrable_on_def)


subsection ‹Some other trivialities about negligible sets›

lemma negligible_subset:
  assumes "negligible s" "t  s"
  shows "negligible t"
  unfolding negligible_def
    by (metis (no_types) Diff_iff assms contra_subsetD has_integral_negligible indicator_simps(2))

lemma negligible_diff[intro?]:
  assumes "negligible s"
  shows "negligible (s - t)"
  using assms by (meson Diff_subset negligible_subset)

lemma negligible_Int:
  assumes "negligible s  negligible t"
  shows "negligible (s  t)"
  using assms negligible_subset by force

lemma negligible_Un:
  assumes "negligible S" and T: "negligible T"
  shows "negligible (S  T)"
proof -
  have "(indicat_real (S  T) has_integral 0) (cbox a b)"
    if S0: "(indicat_real S has_integral 0) (cbox a b)" 
      and  "(indicat_real T has_integral 0) (cbox a b)" for a b
  proof (subst has_integral_spike_eq[OF T])
    show "indicat_real S x = indicat_real (S  T) x" if "x  cbox a b - T" for x
      by (metis Diff_iff Un_iff indicator_def that)
    show "(indicat_real S has_integral 0) (cbox a b)"
      by (simp add: S0)
  qed
  with assms show ?thesis
    unfolding negligible_def by blast
qed

lemma negligible_Un_eq[simp]: "negligible (s  t)  negligible s  negligible t"
  using negligible_Un negligible_subset by blast

lemma negligible_sing[intro]: "negligible {a::'a::euclidean_space}"
  using negligible_standard_hyperplane[OF SOME_Basis, of "a  (SOME i. i  Basis)"] negligible_subset by blast

lemma negligible_insert[simp]: "negligible (insert a s)  negligible s"
  by (metis insert_is_Un negligible_Un_eq negligible_sing)

lemma negligible_empty[iff]: "negligible {}"
  using negligible_insert by blast

text‹Useful in this form for backchaining›
lemma empty_imp_negligible: "S = {}  negligible S"
  by simp

lemma negligible_finite[intro]:
  assumes "finite s"
  shows "negligible s"
  using assms by (induct s) auto

lemma negligible_Union[intro]:
  assumes "finite 𝒯"
    and "t. t  𝒯  negligible t"
  shows "negligible(𝒯)"
  using assms by induct auto

lemma negligible: "negligible S  (T. (indicat_real S has_integral 0) T)"
proof (intro iffI allI)
  fix T
  assume "negligible S"
  then show "(indicator S has_integral 0) T"
    by (meson Diff_iff has_integral_negligible indicator_simps(2))
qed (simp add: negligible_def)

subsection ‹Finite case of the spike theorem is quite commonly needed›

lemma has_integral_spike_finite:
  assumes "finite S"
    and "x. x  T - S  g x = f x"
    and "(f has_integral y) T"
  shows "(g has_integral y) T"
  using assms has_integral_spike negligible_finite by blast

lemma has_integral_spike_finite_eq:
  assumes "finite S"
    and "x. x  T - S  g x = f x"
  shows "((f has_integral y) T  (g has_integral y) T)"
  by (metis assms has_integral_spike_finite)

lemma integrable_spike_finite:
  assumes "finite S"
    and "x. x  T - S  g x = f x"
    and "f integrable_on T"
  shows "g integrable_on T"
  using assms has_integral_spike_finite by blast

lemma has_integral_bound_spike_finite:
  fixes f :: "'a::euclidean_space  'b::real_normed_vector"
  assumes "0  B" "finite S"
      and f: "(f has_integral i) (cbox a b)"
      and leB: "x. x  cbox a b - S  norm (f x)  B"
    shows "norm i  B * content (cbox a b)"
proof -
  define g where "g  (λx. if x  S then 0 else f x)"
  then have "x. x  cbox a b - S  norm (g x)  B"
    using leB by simp
  moreover have "(g has_integral i) (cbox a b)"
    using has_integral_spike_finite [OF ‹finite S _ f]
    by (simp add: g_def)
  ultimately show ?thesis
    by (simp add: 0  B g_def has_integral_bound)
qed

corollary has_integral_bound_real:
  fixes f :: "real  'b::real_normed_vector"
  assumes "0  B" "finite S"
      and "(f has_integral i) {a..b}"
      and "x. x  {a..b} - S  norm (f x)  B"
    shows "norm i  B * content {a..b}"
  by (metis assms box_real(2) has_integral_bound_spike_finite)


subsection ‹In particular, the boundary of an interval is negligible›

lemma negligible_frontier_interval: "negligible(cbox (a::'a::euclidean_space) b - box a b)"
proof -
  let ?A = "((λk. {x. xk = ak}  {x::'a. xk = bk}) ` Basis)"
  have "negligible ?A"
    by (force simp add: negligible_Union[OF finite_imageI])
  moreover have "cbox a b - box a b  ?A"
    by (force simp add: mem_box)
  ultimately show ?thesis
    by (rule negligible_subset)
qed

lemma has_integral_spike_interior:
  assumes f: "(f has_integral y) (cbox a b)" and gf: "x. x  box a b  g x = f x"
  shows "(g has_integral y) (cbox a b)"
  by (meson Diff_iff gf has_integral_spike[OF negligible_frontier_interval _ f])
  
lemma has_integral_spike_interior_eq:
  assumes "x. x  box a b  g x = f x"
  shows "(f has_integral y) (cbox a b)  (g has_integral y) (cbox a b)"
  by (metis assms has_integral_spike_interior)

lemma integrable_spike_interior:
  assumes "x. x  box a b  g x = f x"
    and "f integrable_on cbox a b"
  shows "g integrable_on cbox a b"
  using assms has_integral_spike_interior_eq by blast


subsection ‹Integrability of continuous functions›

lemma operative_approximableI:
  fixes f :: "'b::euclidean_space  'a::banach"
  assumes "0  e"
  shows "operative conj True (λi. g. (xi. norm (f x - g (x::'b))  e)  g integrable_on i)"
proof -
  interpret comm_monoid conj True
    by standard auto
  show ?thesis
  proof (standard, safe)
    fix a b :: 'b
    show "g. (xcbox a b. norm (f x - g x)  e)  g integrable_on cbox a b"
      if "box a b = {}" for a b
      using assms that
      by (metis content_eq_0_interior integrable_on_null interior_cbox norm_zero right_minus_eq)
    {
      fix c g and k :: 'b
      assume fg: "xcbox a b. norm (f x - g x)  e" and g: "g integrable_on cbox a b"
      assume k: "k  Basis"
      show "g. (xcbox a b  {x. x  k  c}. norm (f x - g x)  e)  g integrable_on cbox a b  {x. x  k  c}"
           "g. (xcbox a b  {x. c  x  k}. norm (f x - g x)  e)  g integrable_on cbox a b  {x. c  x  k}"
        using fg g k by auto
    }
    show "g. (xcbox a b. norm (f x - g x)  e)  g integrable_on cbox a b"
      if fg1: "xcbox a b  {x. x  k  c}. norm (f x - g1 x)  e" 
        and g1: "g1 integrable_on cbox a b  {x. x  k  c}"
        and fg2: "xcbox a b  {x. c  x  k}. norm (f x - g2 x)  e" 
        and g2: "g2 integrable_on cbox a b  {x. c  x  k}" 
        and k: "k  Basis"
      for c k g1 g2
    proof -
      let ?g = "λx. if xk = c then f x else if xk  c then g1 x else g2 x"
      show "g. (xcbox a b. norm (f x - g x)  e)  g integrable_on cbox a b"
      proof (intro exI conjI ballI)
        show "norm (f x - ?g x)  e" if "x  cbox a b" for x
          by (auto simp: that assms fg1 fg2)
        show "?g integrable_on cbox a b"
        proof -
          have "?g integrable_on cbox a b  {x. x  k  c}" "?g integrable_on cbox a b  {x. x  k  c}"
            by(rule integrable_spike[OF _ negligible_standard_hyperplane[of k c]], use k g1 g2 in auto)+
          with has_integral_split[OF _ _ k] show ?thesis
            unfolding integrable_on_def by blast
        qed
      qed
    qed
  qed
qed

lemma comm_monoid_set_F_and: "comm_monoid_set.F (∧) True f s  (finite s  (xs. f x))"
proof -
  interpret bool: comm_monoid_set (∧) True ..
  show ?thesis
    by (induction s rule: infinite_finite_induct) auto
qed

lemma approximable_on_division:
  fixes f :: "'b::euclidean_space  'a::banach"
  assumes "0  e"
    and d: "d division_of (cbox a b)"
    and f: "id. g. (xi. norm (f x - g x)  e)  g integrable_on i"
  obtains g where "xcbox a b. norm (f x - g x)  e" "g integrable_on cbox a b"
proof -
  interpret operative conj True "λi. g. (xi. norm (f x - g (x::'b))  e)  g integrable_on i"
    using 0  e by (rule operative_approximableI)
  from f local.division [OF d] that show thesis
    by auto
qed

lemma integrable_continuous:
  fixes f :: "'b::euclidean_space  'a::banach"
  assumes "continuous_on (cbox a b) f"
  shows "f integrable_on cbox a b"
proof (rule integrable_uniform_limit)
  fix e :: real
  assume e: "e > 0"
  then obtain d where "0 < d" and d: "x x'. x  cbox a b; x'  cbox a b; dist x' x < d  dist (f x') (f x) < e"
    using compact_uniformly_continuous[OF assms compact_cbox] unfolding uniformly_continuous_on_def by metis
  obtain p where ptag: "p tagged_division_of cbox a b" and finep: "(λx. ball x d) fine p"
    using fine_division_exists[OF gauge_ball[OF 0 < d], of a b] .
  have *: "isnd ` p. g. (xi. norm (f x - g x)  e)  g integrable_on i"
  proof (safe, unfold snd_conv)
    fix x l
    assume as: "(x, l)  p"
    obtain a b where l: "l = cbox a b"
      using as ptag by blast
    then have x: "x  cbox a b"
      using as ptag by auto
    show "g. (xl. norm (f x - g x)  e)  g integrable_on l"
    proof (intro exI conjI strip)
      show "(λy. f x) integrable_on l"
        unfolding integrable_on_def l by blast
    next
      fix y
      assume y: "y  l"
      then have "y  ball x d"
        using as finep by fastforce
      then show "norm (f y - f x)  e"
        using d x y as l
        by (metis dist_commute dist_norm less_imp_le mem_ball ptag subsetCE tagged_division_ofD(3))
    qed
  qed
  from e have "e  0"
    by auto
  from approximable_on_division[OF this division_of_tagged_division[OF ptag] *]
  show "g. (xcbox a b. norm (f x - g x)  e)  g integrable_on cbox a b"
    by metis
qed

lemma integrable_continuous_interval:
  fixes f :: "'b::ordered_euclidean_space  'a::banach"
  assumes "continuous_on {a..b} f"
  shows "f integrable_on {a..b}"
  by (metis assms integrable_continuous interval_cbox)

lemmas integrable_continuous_real = integrable_continuous_interval[where 'b=real]

lemma integrable_continuous_closed_segment:
  fixes f :: "real  'a::banach"
  assumes "continuous_on (closed_segment a b) f"
  shows "f integrable_on (closed_segment a b)"
  using assms
  by (auto intro!: integrable_continuous_interval simp: closed_segment_eq_real_ivl)


subsection ‹Specialization of additivity to one dimension›


subsection ‹A useful lemma allowing us to factor out the content size›

lemma has_integral_factor_content:
  "(f has_integral i) (cbox a b) 
    (e>0. d. gauge d  (p. p tagged_division_of (cbox a b)  d fine p 
      norm (sum (λ(x,k). content k *R f x) p - i)  e * content (cbox a b)))"
proof (cases "content (cbox a b) = 0")
  case True
  have "e p. p tagged_division_of cbox a b  norm (((x, k)p. content k *R f x))  e * content (cbox a b)"
    unfolding sum_content_null[OF True] True by force
  moreover have "i = 0" 
    if "e. e > 0  d. gauge d 
              (p. p tagged_division_of cbox a b 
                   d fine p 
                   norm (((x, k)p. content k *R f x) - i)  e * content (cbox a b))"
    using that [of 1]
    by (force simp add: True sum_content_null[OF True] intro: fine_division_exists[of _ a b])
  ultimately show ?thesis
    unfolding has_integral_null_eq[OF True]
    by (force simp add: )
next
  case False
  then have F: "0 < content (cbox a b)"
    using zero_less_measure_iff by blast
  let ?P = "λe opp. d. gauge d 
    (p. p tagged_division_of (cbox a b)  d fine p  opp (norm (((x, k)p. content k *R f x) - i)) e)"
  show ?thesis
  proof (subst has_integral, safe)
    fix e :: real
    assume e: "e > 0"
    show "?P (e * content (cbox a b)) (≤)" if §[rule_format]: "ε>0. ?P ε (<)"
      using § [of "e * content (cbox a b)"]
      by (meson F e less_imp_le mult_pos_pos)
    show "?P e (<)" if §[rule_format]:  "ε>0. ?P (ε * content (cbox a b)) (≤)"
      using § [of "e/2 / content (cbox a b)"]
        using F e by (force simp add: algebra_simps)
  qed
qed

lemma has_integral_factor_content_real:
  "(f has_integral i) {a..b::real} 
    (e>0. d. gauge d  (p. p tagged_division_of {a..b}   d fine p 
      norm (sum (λ(x,k). content k *R f x) p - i)  e * content {a..b} ))"
  unfolding box_real[symmetric]
  by (rule has_integral_factor_content)


subsection ‹Fundamental theorem of calculus›

lemma interval_bounds_real:
  fixes q b :: real
  assumes "a  b"
  shows "Sup {a..b} = b"
    and "Inf {a..b} = a"
  using assms by auto

theorem fundamental_theorem_of_calculus:
  fixes f :: "real  'a::banach"
  assumes "a  b" 
      and vecd: "x. x  {a..b}  (f has_vector_derivative f' x) (at x within {a..b})"
  shows "(f' has_integral (f b - f a)) {a..b}"
  unfolding has_integral_factor_content box_real[symmetric]
proof safe
  fix e :: real
  assume "e > 0"
  then have "x. d>0. x  {a..b} 
         (y{a..b}. norm (y-x) < d  norm (f y - f x - (y-x) *R f' x)  e * norm (y-x))"
    using vecd[unfolded has_vector_derivative_def has_derivative_within_alt] by blast
  then obtain d where d: "x. 0 < d x"
                 "x y. x  {a..b}; y  {a..b}; norm (y-x) < d x
                         norm (f y - f x - (y-x) *R f' x)  e * norm (y-x)"
    by metis  
  show "d. gauge d  (p. p tagged_division_of (cbox a b)  d fine p 
    norm (((x, k)p. content k *R f' x) - (f b - f a))  e * content (cbox a b))"
  proof (rule exI, safe)
    show "gauge (λx. ball x (d x))"
      using d(1) gauge_ball_dependent by blast
  next
    fix p
    assume ptag: "p tagged_division_of cbox a b" and finep: "(λx. ball x (d x)) fine p"
    have ba: "b - a = ((x,K)p. Sup K - Inf K)" "f b - f a = ((x,K)p. f(Sup K) - f(Inf K))"
      using additive_tagged_division_1[where f= "λx. x"] additive_tagged_division_1[where f= f]
            a  b ptag by auto
    have "norm ((x, K)  p. (content K *R f' x) - (f (Sup K) - f (Inf K)))
           (np. e * (case n of (x, k)  Sup k - Inf k))"
    proof (rule sum_norm_le,safe)
      fix x K
      assume "(x, K)  p"
      then have "x  K" and kab: "K  cbox a b"
        using ptag by blast+
      then obtain u v where k: "K = cbox u v" and "x  K" and kab: "K  cbox a b"
        using ptag (x, K)  p by auto 
      have "u  v"
        using x  K unfolding k by auto
      have ball: "yK. y  ball x (d x)"
        using finep (x, K)  p by blast
      have "u  K" "v  K"
        by (simp_all add: u  v k)
      have "norm ((v - u) *R f' x - (f v - f u)) = norm (f u - f x - (u - x) *R f' x - (f v - f x - (v - x) *R f' x))"
        by (auto simp add: algebra_simps)
      also have "...  norm (f u - f x - (u - x) *R f' x) + norm (f v - f x - (v - x) *R f' x)"
        by (rule norm_triangle_ineq4)
      finally have "norm ((v - u) *R f' x - (f v - f u)) 
        norm (f u - f x - (u - x) *R f' x) + norm (f v - f x - (v - x) *R f' x)" .
      also have "  e * norm (u - x) + e * norm (v - x)"
      proof (rule add_mono)
        show "norm (f u - f x - (u - x) *R f' x)  e * norm (u - x)"
        proof (rule d)
          show "norm (u - x) < d x"
            using u  K ball by (auto simp add: dist_real_def)
        qed (use x  K u  K kab in auto)
        show "norm (f v - f x - (v - x) *R f' x)  e * norm (v - x)"
        proof (rule d)
          show "norm (v - x) < d x"
            using v  K ball by (auto simp add: dist_real_def)
        qed (use x  K v  K kab in auto)
      qed
      also have "  e * (Sup K - Inf K)"
        using x  K by (auto simp: k interval_bounds_real[OF u  v] field_simps)
      finally show "norm (content K *R f' x - (f (Sup K) - f (Inf K)))  e * (Sup K - Inf K)"
        using u  v by (simp add: k)
    qed
    with a  b show "norm (((x, K)p. content K *R f' x) - (f b - f a))  e * content (cbox a b)"
      by (auto simp: ba split_def sum_subtractf [symmetric] sum_distrib_left)
  qed
qed

lemma ident_has_integral:
  fixes a::real
  assumes "a  b"
  shows "((λx. x) has_integral (b2 - a2)/2) {a..b}"
proof -
  have "((λx. x) has_integral inverse 2 * b2 - inverse 2 * a2) {a..b}"
    unfolding power2_eq_square
    by (rule fundamental_theorem_of_calculus [OF assms] derivative_eq_intros | simp)+
  then show ?thesis
    by (simp add: field_simps)
qed

lemma integral_ident [simp]:
  fixes a::real
  assumes "a  b"
  shows "integral {a..b} (λx. x) = (if a  b then (b2 - a2)/2 else 0)"
  by (metis assms ident_has_integral integral_unique)

lemma ident_integrable_on:
  fixes a::real
  shows "(λx. x) integrable_on {a..b}"
by (metis atLeastatMost_empty_iff integrable_on_def has_integral_empty ident_has_integral)

lemma integral_sin [simp]:
  fixes a::real
  assumes "a  b" shows "integral {a..b} sin = cos a - cos b"
proof -
  have "(sin has_integral (- cos b - - cos a)) {a..b}"
  proof (rule fundamental_theorem_of_calculus)
    show "((λa. - cos a) has_vector_derivative sin x) (at x within {a..b})" for x
      unfolding has_field_derivative_iff_has_vector_derivative [symmetric]
      by (rule derivative_eq_intros | force)+
  qed (use assms in auto)
  then show ?thesis
    by (simp add: integral_unique)
qed

lemma integral_cos [simp]:
  fixes a::real
  assumes "a  b" shows "integral {a..b} cos = sin b - sin a"
proof -
  have "(cos has_integral (sin b - sin a)) {a..b}"
  proof (rule fundamental_theorem_of_calculus)
    show "(sin has_vector_derivative cos x) (at x within {a..b})" for x
      unfolding has_field_derivative_iff_has_vector_derivative [symmetric]
      by (rule derivative_eq_intros | force)+
  qed (use assms in auto)
  then show ?thesis
    by (simp add: integral_unique)
qed

lemma has_integral_sin_nx: "((λx. sin(real_of_int n * x)) has_integral 0) {-pi..pi}"
proof (cases "n = 0")
  case False
  have "((λx. sin (n * x)) has_integral (- cos (n * pi)/n - - cos (n * - pi)/n)) {-pi..pi}"
  proof (rule fundamental_theorem_of_calculus)
    show "((λx. - cos (n * x) / n) has_vector_derivative sin (n * a)) (at a within {-pi..pi})"
      if "a  {-pi..pi}" for a :: real
      using that False
      unfolding has_vector_derivative_def
      by (intro derivative_eq_intros | force)+
  qed auto
  then show ?thesis
    by simp
qed auto

lemma integral_sin_nx:
   "integral {-pi..pi} (λx. sin(x * real_of_int n)) = 0"
  using has_integral_sin_nx [of n] by (force simp: mult.commute)

lemma has_integral_cos_nx:
  "((λx. cos(real_of_int n * x)) has_integral (if n = 0 then 2 * pi else 0)) {-pi..pi}"
proof (cases "n = 0")
  case True
  then show ?thesis
    using has_integral_const_real [of "1::real" "-pi" pi] by auto
next
  case False
  have "((λx. cos (n * x)) has_integral (sin (n * pi)/n - sin (n * - pi)/n)) {-pi..pi}"
  proof (rule fundamental_theorem_of_calculus)
    show "((λx. sin (n * x) / n) has_vector_derivative cos (n * x)) (at x within {-pi..pi})"
      if "x  {-pi..pi}"
      for x :: real
      using that False
      unfolding has_vector_derivative_def
      by (intro derivative_eq_intros | force)+
  qed auto
  with False show ?thesis
    by (simp add: mult.commute)
qed

lemma integral_cos_nx:
   "integral {-pi..pi} (λx. cos(x * real_of_int n)) = (if n = 0 then 2 * pi else 0)"
  using has_integral_cos_nx [of n] by (force simp: mult.commute)


subsection ‹Taylor series expansion›

lemma mvt_integral:
  fixes f::"'a::real_normed_vector'b::banach"
  assumes f'[derivative_intros]:
    "x. x  S  (f has_derivative f' x) (at x within S)"
  assumes line_in: "t. t  {0..1}  x + t *R y  S"
  shows "f (x + y) - f x = integral {0..1} (λt. f' (x + t *R y) y)" (is ?th1)
proof -
  from assms have subset: "(λxa. x + xa *R y) ` {0..1}  S" by auto
  note [derivative_intros] =
    has_derivative_subset[OF _ subset]
    has_derivative_in_compose[where f="(λxa. x + xa *R y)" and g = f]
  note [continuous_intros] =
    continuous_on_compose2[where f="(λxa. x + xa *R y)"]
    continuous_on_subset[OF _ subset]
  have "t. t  {0..1} 
    ((λt. f (x + t *R y)) has_vector_derivative f' (x + t *R y) y)
    (at t within {0..1})"
    using assms
    by (auto simp: has_vector_derivative_def
        linear_cmul[OF has_derivative_linear[OF f'], symmetric]
      intro!: derivative_eq_intros)
  from fundamental_theorem_of_calculus[rule_format, OF _ this]
  show ?th1
    by (auto intro!: integral_unique[symmetric])
qed

lemma (in bounded_bilinear) sum_prod_derivatives_has_vector_derivative:
  assumes "p>0"
  and f0: "Df 0 = f"
  and Df: "m t. m < p  a  t  t  b 
    (Df m has_vector_derivative Df (Suc m) t) (at t within {a..b})"
  and g0: "Dg 0 = g"
  and Dg: "m t. m < p  a  t  t  b 
    (Dg m has_vector_derivative Dg (Suc m) t) (at t within {a..b})"
  and ivl: "a  t" "t  b"
  shows "((λt. i<p. (-1)^i *R prod (Df i t) (Dg (p - Suc i) t))
    has_vector_derivative
      prod (f t) (Dg p t) - (-1)^p *R prod (Df p t) (g t))
    (at t within {a..b})"
  using assms
proof cases
  assume p: "p  1"
  define p' where "p' = p - 2"
  from assms p have p': "{..<p} = {..Suc p'}" "p = Suc (Suc p')"
    by (auto simp: p'_def)
  have *: "i. i  p'  Suc (Suc p' - i) = (Suc (Suc p') - i)"
    by auto
  let ?f = "λi. (-1) ^ i *R (prod (Df i t) (Dg ((p - i)) t))"
  have "(i<p. (-1) ^ i *R (prod (Df i t) (Dg (Suc (p - Suc i)) t) +
    prod (Df (Suc i) t) (Dg (p - Suc i) t))) =
    (i(Suc p'). ?f i - ?f (Suc i))"
    by (auto simp: algebra_simps p'(2) numeral_2_eq_2 * lessThan_Suc_atMost)
  also note sum_telescope
  finally