# Theory Henstock_Kurzweil_Integration

theory Henstock_Kurzweil_Integration
imports Lebesgue_Measure Tagged_Division
```(*  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"]

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 ∀i∈Basis. a∙i ≤ b∙i then prod (λi. b∙i - a∙i) Basis else 0)"

lemma content_cbox: "∀i∈Basis. a∙i ≤ b∙i ⟹ content (cbox a b) = (∏i∈Basis. b∙i - a∙i)"
unfolding content_cbox_cases by simp

lemma content_cbox': "cbox a b ≠ {} ⟹ content (cbox a b) = (∏i∈Basis. b∙i - a∙i)"

lemma content_cbox_if: "content (cbox a b) = (if cbox a b = {} then 0 else ∏i∈Basis. b∙i - a∙i)"

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)"

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 x≤y 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 content_nonneg [simp]: "~ content (cbox a b) < 0"
using not_le by blast

lemma content_pos_lt: "∀i∈Basis. a∙i < b∙i ⟹ 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 ⟷ (∃i∈Basis. b∙i ≤ a∙i)"
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)) ⟷ (∀i∈Basis. a∙i < b∙i)"
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)"

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"

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_split:
fixes a :: "'a::euclidean_space"
assumes "k ∈ Basis"
shows "content (cbox a b) = content(cbox a b ∩ {x. x∙k ≤ c}) + content(cbox a b ∩ {x. x∙k ≥ c})"
― ‹Prove using measure theory›
proof (cases "∀i∈Basis. a ∙ i ≤ b ∙ i")
case True
have 1: "⋀X Y Z. (∏i∈Basis. Z i (if i = k then X else Y i)) = Z k X * (∏i∈Basis-{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: "(∏i∈Basis. b∙i - a∙i) = (∏i∈Basis-{k}. b∙i - a∙i) * (b∙k - a∙k)"
by (metis (no_types, lifting) assms finite_Basis mult.commute prod.remove)
have "⋀x. min (b ∙ k) c = max (a ∙ k) c ⟹
x * (b∙k - a∙k) = x * (max (a ∙ k) c - a ∙ k) + x * (b ∙ k - max (a ∙ k) c)"
moreover
have **: "(∏i∈Basis. ((∑i∈Basis. (if i = k then min (b ∙ k) c else b ∙ i) *⇩R i) ∙ i - a ∙ i)) =
(∏i∈Basis. (if i = k then min (b ∙ k) c else b ∙ i) - a ∙ i)"
"(∏i∈Basis. b ∙ i - ((∑i∈Basis. (if i = k then max (a ∙ k) c else a ∙ i) *⇩R i) ∙ i)) =
(∏i∈Basis. 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. b∙i - x"] 1[of "λi x. x - a∙i"] 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"
qed

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

"d tagged_division_of (cbox a b) ⟹ sum (λ(x,l). content l) d = content (cbox a b)"
by (fact sum_content.tagged_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)"
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))"

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

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"]
also have "… < norm (k1 - k2)/2 + norm (k1 - k2)/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"]
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)"
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"
apply (rule_tac x="h z" in exI)
apply (simp add: * has_integral_linear_cbox[OF z(1) h])
apply (metis B diff le_less_trans pos_less_divide_eq z(2))
done
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)

text‹The case analysis eliminates the condition @{term "f 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"]
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"]

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

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

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"
apply (rule_tac x="w + z" in exI)
using norm_triangle_ineq[of "w - k" "z - l"] w(2) z(2)
done
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"

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

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)"
apply (rule has_integral_unique [where i=S and f = "h ∘ f"])
apply (simp_all add: integrable_integral integrable_linear has_integral_linear )
done

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
qed

lemma integral_sum:
"⟦finite I;  ⋀a. a ∈ I ⟹ f a integrable_on S⟧ ⟹
integral S (λx. ∑a∈I. f a x) = (∑a∈I. 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. ∑a∈I. 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"]
then have "(λx. (of_real (1 / c) * of_real c * f x)) integrable_on s"
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)"

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 ⟷ (∀b∈Basis. ((λ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 "(∀b∈Basis. ((λx. f x ∙ b) has_integral (y ∙ b)) A)"
hence "∀b∈Basis. (((λ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. ∑b∈Basis. (f x ∙ b) *⇩R b) has_integral (∑b∈Basis. (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 ⟷ (∀b∈Basis. (λ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 "(∀b∈Basis. ((λx. f x ∙ b) has_integral (y ∙ b)) A)"
by (subst (asm) has_integral_componentwise_iff)
thus "(∀b∈Basis. (λx. f x ∙ b) integrable_on A)" by (auto simp: integrable_on_def)
next
assume "(∀b∈Basis. (λx. f x ∙ b) integrable_on A)"
then obtain y where "∀b∈Basis. ((λx. f x ∙ b) has_integral y b) A"
unfolding integrable_on_def by (subst (asm) bchoice_iff) blast
hence "∀b∈Basis. (((λ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. ∑b∈Basis. (f x ∙ b) *⇩R b) has_integral (∑b∈Basis. 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 = (∑b∈Basis. integral A (λx. (f x ∙ b) *⇩R b))"
proof -
from assms have integrable: "∀b∈Basis. (λx. x *⇩R b) ∘ (λx. (f x ∙ b)) integrable_on A"
by (subst (asm) integrable_componentwise_iff, intro integrable_linear ballI)
have "integral A f = integral A (λx. ∑b∈Basis. (f x ∙ b) *⇩R b)"
also from integrable have "… = (∑a∈Basis. 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 "⋀n. gauge (λx. ⋂{γ i x |i. i ∈ {0..n}})"
apply (rule gauge_Inter)
using γ by 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. i≤n ⟹ γ 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. ∀m≥M. ∀n≥M. 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. ∀n≥no. 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"
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. x∙k ≤ c} = K2 ∩ {x. x∙k ≤ c}" "k ∈ Basis"
shows "content (K1 ∩ {x. x∙k ≤ 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. x∙k ≥ c} = K2 ∩ {x. x∙k ≥ c}" "k ∈ Basis"
shows "content (K1 ∩ {x. x∙k ≥ 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. x∙k ≤ c})"
and fj: "(f has_integral j) (cbox a b ∩ {x. x∙k ≥ 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])
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])
done
let ?γ = "λx. if x∙k = c then (γ1 x ∩ γ2 x) else ball x ¦x∙k - 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: "x∙k ≤ c" if as: "(x,K) ∈ p" and K: "K ∩ {x. x∙k ≤ 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¦" "y∙k ≤ 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: "x∙k ≥ c" if as: "(x,K) ∈ p" and K: "K ∩ {x. x∙k ≥ 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¦" "y∙k ≥ 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. x∙k ≤ c}) |x K. (x,K) ∈ p ∧ K ∩ {x. x∙k ≤ 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. x∙k ≤ 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. x∙k ≥ c}) |x K. (x,K) ∈ p ∧ K ∩ {x. x∙k ≥ 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. x∙k ≥ 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"
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. x∙k ≤ c}) ∧ d fine p1 ∧
p2 tagged_division_of (cbox a b ∩ {x. x∙k ≥ 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. x∙k = 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. x∙k = c}"
by auto
then obtain ε where "0 < ε" and ε: "ball x ε ⊆ {x. x ∙ k = c}"
using mem_interior by metis
have x: "x∙k = 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 "(∑i∈Basis. ¦(x - (x + (ε/2 ) *⇩R k)) ∙ i¦) =
(∑i∈Basis. (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. x∙k = 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. x∙k ≤ c})"   (is ?thesis1)
and   "f integrable_on (cbox a b ∩ {x. x∙k ≥ c})"   (is ?thesis2)
proof -
obtain y where y: "(f has_integral y) (cbox a b)"
using f by blast
define a' where "a' = (∑i∈Basis. (if i = k then max (a∙k) c else a∙i)*⇩R i)"
define b' where "b' = (∑i∈Basis. (if i = k then min (b∙k) c else b∙i)*⇩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]
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]
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)
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
apply (rule integral_unique [OF has_integral_split[OF _ _ k]])
apply (auto intro: integrable_integral)
done
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 (rule has_integral_split[OF _ _ k])
apply (auto intro: integrable_integral)
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 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: "(∑i∈p. ¦content i¦) = sum content p"
apply (rule sum.cong)
using assms
apply simp
apply (metis abs_of_nonneg assms(1) content_pos_le division_ofD(4))
done
have e: "0 ≤ e"
using assms(2) norm_ge_zero order_trans by blast
have "norm (sum (λl. content l *⇩R c) p) ≤ (∑i∈p. norm (content i *⇩R c))"
using norm_sum by blast
also have "...  ≤ e * (∑i∈p. ¦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)"
apply (rule mult_left_mono [OF _ e])
using additive_content_division assms(1) eq_iff apply blast
done
finally show ?thesis .
qed

lemma rsum_bound:
assumes p: "p tagged_division_of (cbox a b)"
and "∀x∈cbox 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)"
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: "⋀xk. xk ∈ p ⟹ norm (f (fst xk)) ≤ e"
unfolding fst_conv using tagged_division_ofD(2,3)[OF p] assms
by (metis prod.collapse subset_eq)
have "norm (sum (λ(x,k). content k *⇩R f x) p) ≤ (∑i∈p. norm (case i of (x, k) ⇒ content k *⇩R f x))"
by (rule norm_sum)
also have "...  ≤ e * content (cbox a b)"
unfolding split_def norm_scaleR
apply (rule order_trans[OF sum_mono])
apply (rule mult_left_mono[OF _ abs_ge_zero, of _ e])
apply (metis norm)
unfolding sum_distrib_right[symmetric]
using con sum_le
apply (auto simp: mult.commute intro: mult_left_mono [OF _ e])
done
finally show ?thesis .
qed

lemma rsum_diff_bound:
assumes "p tagged_division_of (cbox a b)"
and "∀x∈cbox 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)"
apply (rule order_trans[OF _ rsum_bound[OF assms]])
apply (simp add: split_def scaleR_diff_right sum_subtractf eq_refl)
done

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. x∈cbox 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. x∈cbox 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 "i∙k ≤ j∙k"
proof -
have ik_le_jk: "i∙k ≤ j∙k"
if f_i: "(f has_integral i) (cbox a b)"
and g_j: "(g has_integral j) (cbox a b)"
and le: "∀x∈cbox 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 < (i∙k - j∙k) / 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 "¬ i∙k ≤ j∙k"
then have ij: "(i∙k - j∙k) / 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 "w1∙k ≤ w2∙k"
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"
apply (rule has_integral_component_le)
using integrable_integral assms
apply auto
done

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 ≤ i∙k"
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
apply (rule has_integral_component_nonneg)
using assms True
apply auto
done
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 "i∙k ≤ 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 "∀x∈cbox a b. B ≤ f(x)∙k"
and "k ∈ Basis"
shows "B * content (cbox a b) ≤ i∙k"
using has_integral_component_le[OF assms(3) has_integral_const assms(1),of "(∑i∈Basis. B *⇩R i)::'b"] assms(2-)

lemma has_integral_component_ubound:
fixes f::"'a::euclidean_space => 'b::euclidean_space"
assumes "(f has_integral i) (cbox a b)"
and "∀x∈cbox a b. f x∙k ≤ B"
and "k ∈ Basis"
shows "i∙k ≤ B * content (cbox a b)"
using has_integral_component_le[OF assms(3,1) has_integral_const, of "∑i∈Basis. B *⇩R i"] assms(2-)

lemma integral_component_lbound:
fixes f :: "'a::euclidean_space ⇒ 'b::euclidean_space"
assumes "f integrable_on cbox a b"
and "∀x∈cbox a b. B ≤ f(x)∙k"
and "k ∈ Basis"
shows "B * content (cbox a b) ≤ (integral(cbox a b) f)∙k"
apply (rule has_integral_component_lbound)
using assms
unfolding has_integral_integral
apply auto
done

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 "∀x∈cbox a b. f x∙k ≤ B"
and "k ∈ Basis"
shows "(integral (cbox a b) f)∙k ≤ B * content (cbox a b)"
apply (rule has_integral_component_ubound)
using assms
unfolding has_integral_integral
apply auto
done

lemma integral_component_ubound_real:
fixes f :: "real ⇒ 'a::euclidean_space"
assumes "f integrable_on {a..b}"
and "∀x∈{a..b}. f x∙k ≤ 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. (∀x∈cbox 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. (∀x∈cbox 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. ∀m≥M. ∀n≥M. 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 (auto simp: divide_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: "∀n≥N1. 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: divide_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 :: 'a⇒real) 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. ¦x∙k - c¦ ≤ d}) < e"
proof cases
assume *: "a ∙ k ≤ c ∧ c ≤ b ∙ k ∧ (∀j∈Basis. a ∙ j ≤ b ∙ j)"
define a' where "a' d = (∑j∈Basis. (if j = k then max (a∙j) (c - d) else a∙j) *⇩R j)" for d
define b' where "b' d = (∑j∈Basis. (if j = k then min (b∙j) (c + d) else b∙j) *⇩R j)" for d

have "((λd. ∏j∈Basis. (b' d - a' d) ∙ j) ⤏ (∏j∈Basis. (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 "(∏j∈Basis. (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. ∏j∈Basis. (b' d - a' d) ∙ j) ⤏ 0) (at_right 0) =
((λd. content (cbox a b ∩ {x. ¦x∙k - 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. ¦x∙k - 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 "(∏j∈Basis. (b' d - a' d) ∙ j) = content (cbox a b ∩ {x. ¦x∙k - 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 ∧ (∀j∈Basis. a ∙ j ≤ b ∙ j))"
then have "(∃j∈Basis. b ∙ j < a ∙ j) ∨ (c < a ∙ k ∨ b ∙ k < c)"
by (auto simp: not_le)
show thesis
proof cases
assume "∃j∈Basis. 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 "¬ (∃j∈Basis. 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. x∙k = 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. x∙k = c} :: 'a⇒real"
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: "x∙k = 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. ¦x∙k - 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 "(∑l∈snd ` 𝒟. content (l ∩ {x. ¦x ∙ k - c¦ ≤ d})) =
sum content ((λl. l ∩ {x. ¦x ∙ k - c¦ ≤ d})`{l∈snd ` 𝒟. 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})`{l∈snd ` 𝒟. 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 "(∑K∈snd ` 𝒟. 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 *
apply (subst abs_of_nonneg)
using measure_nonneg
by (force simp add: indicator_def intro: sum_nonneg)+
qed
qed

corollary negligible_standard_hyperplane_cart:
fixes k :: "'a::finite"
shows "negligible {x. x\$k = (0::real)}"

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)⌋)"
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)"
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 "... = (∑i≤N + 1. ∑j∈q i. (real i + 1) * (case j of (x, K) ⇒ content K *⇩R indicator S x))"
apply (rule sum_Sigma_product [symmetric])
using q(1) apply auto
done
also have "... ≤ (∑i≤N + 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 "... ≤ (∑i≤N + 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 * (∑i≤N + 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
show ?thesis
using fint gf
apply (subst has_integral_alt)
apply (subst (asm) has_integral_alt)
apply (simp split: if_split_asm)
apply (blast dest: *)
apply (erule_tac V = "∀a b. T ≠ cbox a b" in thin_rl)
apply (elim all_forward imp_forward ex_forward all_forward conj_forward asm_rl)
apply (auto dest!: *[where f="λx. if x∈T then f x else 0" and g="λx. if x ∈ T then g x else 0"])
done
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)"
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"
apply (subst insert_is_Un)
unfolding negligible_Un_eq
apply auto
done

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))

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]
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. x∙k = a∙k} ∪ {x::'a. x∙k = b∙k}) ` Basis)"
have "negligible ?A"
by (force simp add: negligible_Union[OF finite_imageI])
moreover have "cbox a b - box a b ⊆ ?A"
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)"
apply (rule has_integral_spike[OF negligible_frontier_interval _ f])
using gf by auto

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. (∀x∈i. 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. (∀x∈cbox a b. norm (f x - g x) ≤ e) ∧ g integrable_on cbox a b"
if "box a b = {}" for a b
apply (rule_tac x=f in exI)
using assms that by (auto simp: content_eq_0_interior)
{
fix c g and k :: 'b
assume fg: "∀x∈cbox a b. norm (f x - g x) ≤ e" and g: "g integrable_on cbox a b"
assume k: "k ∈ Basis"
show "∃g. (∀x∈cbox a b ∩ {x. x ∙ k ≤ c}. norm (f x - g x) ≤ e) ∧ g integrable_on cbox a b ∩ {x. x ∙ k ≤ c}"
"∃g. (∀x∈cbox a b ∩ {x. c ≤ x ∙ k}. norm (f x - g x) ≤ e) ∧ g integrable_on cbox a b ∩ {x. c ≤ x ∙ k}"
apply (rule_tac[!] x=g in exI)
using fg integrable_split[OF g k] by auto
}
show "∃g. (∀x∈cbox a b. norm (f x - g x) ≤ e) ∧ g integrable_on cbox a b"
if fg1: "∀x∈cbox 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: "∀x∈cbox 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 x∙k = c then f x else if x∙k ≤ c then g1 x else g2 x"
show "∃g. (∀x∈cbox 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 ⟶ (∀x∈s. f x))"
proof -
interpret bool: comm_monoid_set "(∧)" True
proof qed auto
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: "∀i∈d. ∃g. (∀x∈i. norm (f x - g x) ≤ e) ∧ g integrable_on i"
obtains g where "∀x∈cbox a b. norm (f x - g x) ≤ e" "g integrable_on cbox a b"
proof -
interpret operative conj True "λi. ∃g. (∀x∈i. 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 *: "∀i∈snd ` p. ∃g. (∀x∈i. 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. (∀x∈l. norm (f x - g x) ≤ e) ∧ g integrable_on l"
apply (rule_tac x="λy. f x" in exI)
proof safe
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. (∀x∈cbox 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]
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
apply (subst has_integral)
proof safe
fix e :: real
assume e: "e > 0"
{ assume "∀e>0. ?P e (<)"
then show "?P (e * content (cbox a b)) (≤)"
apply (rule allE [where x="e * content (cbox a b)"])
apply (elim impE ex_forward conj_forward)
using F e apply (auto simp add: algebra_simps)
done }
{ assume "∀e>0. ?P (e * content (cbox a b)) (≤)"
then show "?P e (<)"
apply (rule allE [where x="e/2 / content (cbox a b)"])
apply (elim impE ex_forward conj_forward)
using F e apply (auto simp add: algebra_simps)
done }
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))"
‹a ≤ b› ptag by auto
have "norm (∑(x, K) ∈ p. (content K *⇩R f' x) - (f (Sup K) - f (Inf K)))
≤ (∑n∈p. 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: "∀y∈K. 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))"
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)"
show "norm (f u - f x - (u - x) *⇩R f' x) ≤ e * norm (u - x)"
apply (rule d(2)[of x u])
using ‹x ∈ K› kab ‹u ∈ K› ball dist_real_def by (auto simp add:dist_real_def)
show "norm (f v - f x - (v - x) *⇩R f' x) ≤ e * norm (v - x)"
apply (rule d(2)[of x v])
using ‹x ∈ K› kab ‹v ∈ K› ball dist_real_def by (auto simp add:dist_real_def)
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 (b⇧2 - a⇧2)/2) {a..b}"
proof -
have "((λx. x) has_integral inverse 2 * b⇧2 - inverse 2 * a⇧2) {a..b}"
apply (rule fundamental_theorem_of_calculus [OF assms])
unfolding power2_eq_square
by (rule derivative_eq_intros | simp)+
then show ?thesis
qed

lemma integral_ident [simp]:
fixes a::real
assumes "a ≤ b"
shows "integral {a..b} (λx. x) = (if a ≤ b then (b⇧2 - a⇧2)/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)

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
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)))
= prod (f t) (Dg p t) - (- 1) ^ p *⇩R prod (Df p t) (g t)"
unfolding p'[symmetric]
thus ?thesis
using assms
by (auto intro!: derivative_eq_intros has_vector_derivative)
qed (auto intro!: derivative_eq_intros has_vector_derivative)

lemma
fixes f::"real⇒'a::banach"
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 ivl: "a ≤ b"
defines "i ≡ λx. ((b - x) ^ (p - 1) / fact (p - 1)) *⇩R Df p x"
shows taylor_has_integral:
"(i has_integral f b - (∑i<p. ((b-a) ^ i / fact i) *⇩R Df i a)) {a..b}"
and taylor_integral:
"f b = (∑i<p. ((b-a) ^ i / fact i) *⇩R Df i a) + integral {a..b} i"
and taylor_integrable:
"i integrable_on {a..b}"
proof goal_cases
case 1
interpret bounded_bilinear "scaleR::real⇒'a⇒'a"
by (rule bounded_bilinear_scaleR)
define g where "g s = (b - s)^(p - 1)/fact (p - 1)" for s
define Dg where [abs_def]:
"Dg n s = (if n < p then (-1)^n * (b - s)^(p - 1 - n) / fact (p - 1 - n) else 0)" for n s
have g0: "Dg 0 = g"
using ‹p > 0›
by (auto simp add: Dg_def divide_simps g_def split: if_split_asm)
{
fix m
assume "p > Suc m"
hence "p - Suc m = Suc (p - Suc (Suc m))"
by auto
hence "real (p - Suc m) * fact (p - Suc (Suc m)) = fact (p - Suc m)"
by auto
} note fact_eq = this
have Dg: "⋀m t. m < p ⟹ a ≤ t ⟹ t ≤ b ⟹
(Dg m has_vector_derivative Dg (Suc m) t) (at t within {a..b})"
unfolding Dg_def
by (auto intro!: derivative_eq_intros simp: has_vector_derivative_def fact_eq divide_simps)
let ?sum = "λt. ∑i<p. (- 1) ^ i *⇩R Dg i t *⇩R Df (p - Suc i) t"
from sum_prod_derivatives_has_vector_derivative[of _ Dg _ _ _ Df,
OF ‹p > 0› g0 Dg f0 Df]
have deriv: "⋀t. a ≤ t ⟹ t ≤ b ⟹
(?sum has_vector_derivative
g t *⇩R Df p t - (- 1) ^ p *⇩R Dg p t *⇩R f t) (at t within {a..b})"
by auto
from fundamental_theorem_of_calculus[rule_format, OF ‹a ≤ b› deriv]
have "(i has_integral ?sum b - ?sum a) {a..b}"
using atLeastatMost_empty'[simp del]
by (simp add: i_def g_def Dg_def)
also
have one: "(- 1) ^ p' * (- 1) ^ p' = (1::real)"
and "{..<p} ∩ {i. p = Suc i} = {p - 1}"
for p'
using ‹p > 0›
by (auto simp: power_mult_distrib[symmetric])
then have "?sum b = f b"
using Suc_pred'[OF ‹p > 0›]
by (simp add: diff_eq_eq Dg_def power_0_left le_Suc_eq if_distrib
if_distribR sum.If_cases f0)
also
have "{..<p} = (λx. p - x - 1) ` {..<p}"
proof safe
fix x
assume "x < p"
thus "x ∈ (λx. p - x - 1) ` {..<p}"
by (auto intro!: image_eqI[where x = "p - x - 1"])
qed simp
from _ this
have "?sum a = (∑i<p. ((b-a) ^ i / fact i) *⇩R Df i a)"
by (rule sum.reindex_cong) (auto simp add: inj_on_def Dg_def one)
finally show c: ?case .
case 2 show ?case using c integral_unique
by (metis (lifting) add.commute diff_eq_eq integral_unique)
case 3 show ?case using c by force
qed

subsection ‹Only need trivial subintervals if the interval itself is trivial›

proposition division_of_nontrivial:
fixes 𝒟 :: "'a::euclidean_space set set"
assumes sdiv: "𝒟 division_of (cbox a b)"
and cont0: "content (cbox a b) ≠ 0"
shows "{k. k ∈ 𝒟 ∧ content k ≠ 0} division_of (cbox a b)"
using sdiv
proof (induction "card 𝒟" arbitrary: 𝒟 rule: less_induct)
case less
note 𝒟 = division_ofD[OF less.prems]
{
presume *: "{k ∈ 𝒟. content k ≠ 0} ≠ 𝒟 ⟹ ?case"
then show ?case
using less.prems by fastforce
}
assume noteq: "{k ∈ 𝒟. content k ≠ 0} ≠ 𝒟"
then obtain K c d where "K ∈ 𝒟" and contk: "content K = 0" and keq: "K = cbox c d"
using 𝒟(4) by blast
then have "card 𝒟 > 0"
unfolding card_gt_0_iff using less by auto
then have card: "card (𝒟 - {K}) < card 𝒟"
using less ‹K ∈ 𝒟› by (simp add: 𝒟(1))
have closed: "closed (⋃(𝒟 - {K}))"
using less.prems by auto
have "x islimpt ⋃(𝒟 - {K})" if "x ∈ K" for x
unfolding islimpt_approachable
proof (intro allI impI)
fix e::real
assume "e > 0"
obtain i where i: "c∙i = d∙i" "i∈Basis"
using contk 𝒟(3) [OF ‹K ∈ 𝒟›] unfolding box_ne_empty keq
by (meson content_eq_0 dual_order.antisym)
then have xi: "x∙i = d∙i"
using ‹x ∈ K› unfolding keq mem_box by (metis antisym)
define y where "y = (∑j∈Basis. (if j = i then if c∙i ≤ (a∙i + b∙i)/2 then c∙i +
min e (b∙i - c∙i)/2 else c∙i - min e (c∙i - a∙i)/2 else x∙j) *⇩R j)"
show "∃x'∈⋃(𝒟 - {K}). x' ≠ x ∧ dist x' x < e"
proof (intro bexI conjI)
have "d ∈ cbox c d"
using 𝒟(3)[OF ‹K ∈ 𝒟›] by (simp add: box_ne_empty(1) keq mem_box(2))
then have "d ∈ cbox a b"
using 𝒟(2)[OF ‹K ∈ 𝒟›] by (auto simp: keq)
then have di: "a ∙ i ≤ d ∙ i ∧ d ∙ i ≤ b ∙ i"
using ‹i ∈ Basis› mem_box(2) by blast
then have xyi: "y∙i ≠ x∙i"
unfolding y_def i xi using ‹e > 0› cont0 ‹i ∈ Basis›
by (auto simp: content_eq_0 elim!: ballE[of _ _ i])
then show "y ≠ x"
unfolding euclidean_eq_iff[where 'a='a] using i by auto
have "norm (y-x) ≤ (∑b∈Basis. ¦(y - x) ∙ b¦)"
by (rule norm_le_l1)
also have "... = ¦(y - x) ∙ i¦ + (∑b ∈ Basis - {i}. ¦(y - x) ∙ b¦)"
by (meson finite_Basis i(2) sum.remove)
also have "... <  e + sum (λi. 0) Basis"
show "¦(y-x) ∙ i¦ < e"
using di ‹e > 0› y_def i xi by (auto simp: inner_simps)
show "(∑i∈Basis - {i}. ¦(y-x) ∙ i¦) ≤ (∑i∈Basis. 0)"
unfolding y_def by (auto simp: inner_simps)
qed
finally have "norm (y-x) < e + sum (λi. 0) Basis" .
then show "dist y x < e"
unfolding dist_norm by auto
have "y ∉ K"
unfolding keq mem_box using i(1) i(2) xi xyi by fastforce
moreover have "y ∈ ⋃𝒟"
using subsetD[OF 𝒟(2)[OF ‹K ∈ 𝒟›] ‹x ∈ K›] ‹e > 0› di i
by (auto simp: 𝒟 mem_box y_def field_simps elim!: ballE[of _ _ i])
ultimately show "y ∈ ⋃(𝒟 - {K})" by auto
qed
qed
then have "K ⊆ ⋃(𝒟 - {K})"
using closed closed_limpt by blast
then have "⋃(𝒟 - {K}) = cbox a b"
unfolding 𝒟(6)[symmetric] by auto
then have "𝒟 - {K} division_of cbox a b"
by (metis Diff_subset less.prems division_of_subset 𝒟(6))
then have "{ka ∈ 𝒟 - {K}. content ka ≠ 0} division_of (cbox a b)"
using card less.hyps by blast
moreover have "{ka ∈ 𝒟 - {K}. content ka ≠ 0} = {K ∈ 𝒟. content K ≠ 0}"
using contk by auto
ultimately show ?case by auto
qed

subsection ‹Integrability on subintervals›

lemma operative_integrableI:
fixes f :: "'b::euclidean_space ⇒ 'a::banach"
assumes "0 ≤ e"
shows "operative conj True (λi. f integrable_on i)"
proof -
interpret comm_monoid conj True
by standard auto
have 1: "⋀a b. box a b = {} ⟹ f integrable_on cbox a b"
have 2: "⋀a b c k.
⟦k ∈ Basis;
f integrable_on cbox a b ∩ {x. x ∙ k ≤ c};
f integrable_on cbox a b ∩ {x. c ≤ x ∙ k}⟧
⟹ f integrable_on cbox a b"
unfolding integrable_on_def by (auto intro!: has_integral_split)
show ?thesis
apply standard
using 1 2 by blast+
qed

lemma integrable_subinterval:
fixes f :: "'b::euclidean_space ⇒ 'a::banach"
assumes f: "f integrable_on cbox a b"
and cd: "cbox c d ⊆ cbox a b"
shows "f integrable_on cbox c d"
proof -
interpret operative conj True "λi. f integrable_on i"
using order_refl by (rule operative_integrableI)
show ?thesis
proof (cases "cbox c d = {}")
case True
then show ?thesis
using division [symmetric] f by (auto simp: comm_monoid_set_F_and)
next
case False
then show ?thesis
by (metis cd comm_monoid_set_F_and division division_of_finite f partial_division_extend_1)
qed
qed

lemma integrable_subinterval_real:
fixes f :: "real ⇒ 'a::banach"
assumes "f integrable_on {a..b}"
and "{c..d} ⊆ {a..b}"
shows "f integrable_on {c..d}"
by (metis assms box_real(2) integrable_subinterval)

subsection ‹Combining adjacent intervals in 1 dimension›

lemma has_integral_combine:
fixes a b c :: real and j :: "'a::banach"
assumes "a ≤ c"
and "c ≤ b"
and ac: "(f has_integral i) {a..c}"
and cb: "(f has_integral j) {c..b}"
shows "(f has_integral (i + j)) {a..b}"
proof -
interpret operative_real "lift_option plus" "Some 0"
"λi. if f integrable_on i then Some (integral i f) else None"
using operative_integralI by (rule operative_realI)
from ‹a ≤ c› ‹c ≤ b› ac cb coalesce_less_eq
have *: "lift_option (+)
(if f integrable_on {a..c} then Some (integral {a..c} f) else None)
(if f integrable_on {c..b} then Some (integral {c..b} f) else None) =
(if f integrable_on {a..b} then Some (integral {a..b} f) else None)"
by (auto simp: split: if_split_asm)
then have "f integrable_on cbox a b"
using ac cb by (auto split: if_split_asm)
with *
show ?thesis
using ac cb by (auto simp add: integrable_on_def integral_unique split: if_split_asm)
qed

lemma integral_combine:
fixes f :: "real ⇒ 'a::banach"
assumes "a ≤ c"
and "c ≤ b"
and ab: "f integrable_on {a..b}"
shows "integral {a..c} f + integral {c..b} f = integral {a..b} f"
proof -
have "(f has_integral integral {a..c} f) {a..c}"
using ab ‹c ≤ b› integrable_subinterval_real by fastforce
moreover
have "(f has_integral integral {c..b} f) {c..b}"
using ab ‹a ≤ c› integrable_subinterval_real by fastforce
ultimately have "(f has_integral integral {a..c} f + integral {c..b} f) {a..b}"
using ‹a ≤ c› ‹c ≤ b› has_integral_combine by blast
then show ?thesis
qed

lemma integrable_combine:
fixes f :: "real ⇒ 'a::banach"
assumes "a ≤ c"
and "c ≤ b"
and "f integrable_on {a..c}"
and "f integrable_on {c..b}"
shows "f integrable_on {a..b}"
using assms
unfolding integrable_on_def
by (auto intro!: has_integral_combine)

lemma integral_minus_sets:
fixes f::"real ⇒ 'a::banach"
shows "c ≤ a ⟹ c ≤ b ⟹ f integrable_on {c .. max a b} ⟹
integral {c .. a} f - integral {c .. b} f =
(if a ≤ b then - integral {a .. b} f else integral {b .. a} f)"
using integral_combine[of c a b f]  integral_combine[of c b a f]
by (auto simp: algebra_simps max_def)

lemma integral_minus_sets':
fixes f::"real ⇒ 'a::banach"
shows "c ≥ a ⟹ c ≥ b ⟹ f integrable_on {min a b .. c} ⟹
integral {a .. c} f - integral {b .. c} f =
(if a ≤ b then integral {a .. b} f else - integral {b .. a} f)"
using integral_combine[of b a c f] integral_combine[of a b c f]
by (auto simp: algebra_simps min_def)

subsection ‹Reduce integrability to "local" integrability›

lemma integrable_on_little_subintervals:
fixes f :: "'b::euclidean_space ⇒ 'a::banach"
assumes "∀x∈cbox a b. ∃d>0. ∀u v. x ∈ cbox u v ∧ cbox u v ⊆ ball x d ∧ cbox u v ⊆ cbox a b ⟶
f integrable_on cbox u v"
shows "f integrable_on cbox a b"
proof -
interpret operative conj True "λi. f integrable_on i"
using order_refl by (rule operative_integrableI)
have "∀x. ∃d>0. x∈cbox a b ⟶ (∀u v. x ∈ cbox u v ∧ cbox u v ⊆ ball x d ∧ cbox u v ⊆ cbox a b ⟶
f integrable_on cbox u v)"
using assms by (metis zero_less_one)
then obtain d where d: "⋀x. 0 < d x"
"⋀x u v. ⟦x ∈ cbox a b; x ∈ cbox u v; cbox u v ⊆ ball x (d x); cbox u v ⊆ cbox a b⟧
⟹ f integrable_on cbox u v"
by metis
obtain p where p: "p tagged_division_of cbox a b" "(λx. ball x (d x)) fine p"
using fine_division_exists[OF gauge_ball_dependent,of d a b] d(1) by blast
then have sndp: "snd ` p division_of cbox a b"
by (metis division_of_tagged_division)
have "f integrable_on k" if "(x, k) ∈ p" for x k
using tagged_division_ofD(2-4)[OF p(1) that] fineD[OF p(2) that] d[of x] by auto
then show ?thesis
unfolding division [symmetric, OF sndp] comm_monoid_set_F_and
by auto
qed

subsection ‹Second FTC or existence of antiderivative›

lemma integrable_const[intro]: "(λx. c) integrable_on cbox a b"
unfolding integrable_on_def by blast

lemma integral_has_vector_derivative_continuous_at:
fixes f :: "real ⇒ 'a::banach"
assumes f: "f integrable_on {a..b}"
and x: "x ∈ {a..b} - S"
and "finite S"
and fx: "continuous (at x within ({a..b} - S)) f"
shows "((λu. integral {a..u} f) has_vector_derivative f x) (at x within ({a..b} - S))"
proof -
let ?I = "λa b. integral {a..b} f"
{ fix e::real
assume "e > 0"
obtain d where "d>0" and d: "⋀x'. ⟦x' ∈ {a..b} - S; ¦x' - x¦ < d⟧ ⟹ norm(f x' - f x) ≤ e"
using ‹e>0› fx by (auto simp: continuous_within_eps_delta dist_norm less_imp_le)
have "norm (integral {a..y} f - integral {a..x} f - (y-x) *⇩R f x) ≤ e * ¦y - x¦"
if y: "y ∈ {a..b} - S" and yx: "¦y - x¦ < d" for y
proof (cases "y < x")
case False
have "f integrable_on {a..y}"
using f y by (simp add: integrable_subinterval_real)
then have Idiff: "?I a y - ?I a x = ?I x y"
using False x by (simp add: algebra_simps integral_combine)
have fux_int: "((λu. f u - f x) has_integral integral {x..y} f - (y-x) *⇩R f x) {x..y}"
apply (rule has_integral_diff)
using x y apply (auto intro: integrable_integral [OF integrable_subinterval_real [OF f]])
using has_integral_const_real [of "f x" x y] False
apply simp
done
have "⋀xa. y - x < d ⟹ (⋀x'. a ≤ x' ∧ x' ≤ b ∧ x' ∉ S ⟹ ¦x' - x¦ < d ⟹ norm (f x' - f x) ≤ e) ⟹ 0 < e ⟹ xa ∉ S ⟹ a ≤ x ⟹ x ∉ S ⟹ y ≤ b ⟹ y ∉ S ⟹ x ≤ xa ⟹ xa ≤ y ⟹ norm (f xa - f x) ≤ e"
using assms by auto
show ?thesis
using False
apply (simp add: abs_eq_content del: content_real_if measure_lborel_Icc)
apply (rule has_integral_bound_real[where f="(λu. f u - f x)"])
using yx False d x y ‹e>0› assms by (auto simp: Idiff fux_int)
next
case True
have "f integrable_on {a..x}"
using f x by (simp add: integrable_subinterval_real)
then have Idiff: "?I a x - ?I a y = ?I y x"
using True x y by (simp add: algebra_simps integral_combine)
have fux_int: "((λu. f u - f x) has_integral integral {y..x} f - (x - y) *⇩R f x) {y..x}"
apply (rule has_integral_diff)
using x y apply (auto intro: integrable_integral [OF integrable_subinterval_real [OF f]])
using has_integral_const_real [of "f x" y x] True
apply simp
done
have "norm (integral {a..x} f - integral {a..y} f - (x - y) *⇩R f x) ≤ e * ¦y - x¦"
using True
apply (simp add: abs_eq_content del: content_real_if measure_lborel_Icc)
apply (rule has_integral_bound_real[where f="(λu. f u - f x)"])
using yx True d x y ‹e>0› assms by (auto simp: Idiff fux_int)
then show ?thesis
qed
then have "∃d>0. ∀y∈{a..b} - S. ¦y - x¦ < d ⟶ norm (integral {a..y} f - integral {a..x} f - (y-x) *⇩R f x) ≤ e * ¦y - x¦"
using ‹d>0› by blast
}
then show ?thesis
by (simp add: has_vector_derivative_def has_derivative_within_alt bounded_linear_scaleR_left)
qed

lemma integral_has_vector_derivative:
fixes f :: "real ⇒ 'a::banach"
assumes "continuous_on {a..b} f"
and "x ∈ {a..b}"
shows "((λu. integral {a..u} f) has_vector_derivative f(x)) (at x within {a..b})"
using assms integral_has_vector_derivative_continuous_at [OF integrable_continuous_real]
by (fastforce simp: continuous_on_eq_continuous_within)

lemma integral_has_real_derivative:
assumes "continuous_on {a..b} g"
assumes "t ∈ {a..b}"
shows "((λx. integral {a..x} g) has_real_derivative g t) (at t within {a..b})"
using integral_has_vector_derivative[of a b g t] assms
by (auto simp: has_field_derivative_iff_has_vector_derivative)

lemma antiderivative_continuous:
fixes q b :: real
assumes "continuous_on {a..b} f"
obtains g where "⋀x. x ∈ {a..b} ⟹ (g has_vector_derivative (f x::_::banach)) (at x within {a..b})"
using integral_has_vector_derivative[OF assms] by auto

subsection ‹Combined fundamental theorem of calculus›

lemma antiderivative_integral_continuous:
fixes f :: "real ⇒ 'a::banach"
assumes "continuous_on {a..b} f"
obtains g where "∀u∈{a..b}. ∀v ∈ {a..b}. u ≤ v ⟶ (f has_integral (g v - g u)) {u..v}"
proof -
obtain g
where g: "⋀x. x ∈ {a..b} ⟹ (g has_vector_derivative f x) (at x within {a..b})"
using  antiderivative_continuous[OF assms] by metis
have "(f has_integral g v - g u) {u..v}" if "u ∈ {a..b}" "v ∈ {a..b}" "u ≤ v" for u v
proof -
have "⋀x. x ∈ cbox u v ⟹ (g has_vector_derivative f x) (at x within cbox u v)"
by (metis atLeastAtMost_iff atLeastatMost_subset_iff box_real(2) g
has_vector_derivative_within_subset subsetCE that(1,2))
then show ?thesis
by (metis box_real(2) that(3) fundamental_theorem_of_calculus)
qed
then show ?thesis
using that by blast
qed

subsection ‹General "twiddling" for interval-to-interval function image›

lemma has_integral_twiddle:
assumes "0 < r"
and hg: "⋀x. h(g x) = x"
and gh: "⋀x. g(h x) = x"
and contg: "⋀x. continuous (at x) g"
and g: "⋀u v. ∃w z. g ` cbox u v = cbox w z"
and h: "⋀u v. ∃w z. h ` cbox u v = cbox w z"
and r: "⋀u v. content(g ` cbox u v) = r * content (cbox u v)"
and intfi: "(f has_integral i) (cbox a b)"
shows "((λx. f(g x)) has_integral (1 / r) *⇩R i) (h ` cbox a b)"
proof (cases "cbox a b = {}")
case True
then show ?thesis
using intfi by auto
next
case False
obtain w z where wz: "h ` cbox a b = cbox w z"
using h by blast
have inj: "inj g" "inj h"
using hg gh injI by metis+
from h obtain ha hb where h_eq: "h ` cbox a b = cbox ha hb" by blast
have "∃d. gauge d ∧ (∀p. p tagged_division_of h ` cbox a b ∧ d fine p
⟶ norm ((∑(x, k)∈p. content k *⇩R f (g x)) - (1 / r) *⇩R i) < e)"
if "e > 0" for e
proof -
have "e * r > 0" using that ‹0 < r› by simp
with intfi[unfolded has_integral]
obtain d where "gauge d"
and d: "⋀p. p tagged_division_of cbox a b ∧ d fine p
⟹ norm ((∑(x, k)∈p. content k *⇩R f x) - i) < e * r"
by metis
define d' where "d' x = g -` d (g x)" for x
show ?thesis
proof (rule_tac x=d' in exI, safe)
show "gauge d'"
using ‹gauge d› continuous_open_vimage[OF _ contg] by (auto simp: gauge_def d'_def)
next
fix p
assume ptag: "p tagged_division_of h ` cbox a b" and finep: "d' fine p"
note p = tagged_division_ofD[OF ptag]
have gab: "g y ∈ cbox a b" if "y ∈ K" "(x, K) ∈ p" for x y K
by (metis hg inj(2) inj_image_mem_iff p(3) subsetCE that that)
have gimp: "(λ(x,K). (g x, g ` K)) ` p tagged_division_of (cbox a b) ∧
d fine (λ(x, k). (g x, g ` k)) ` p"
unfolding tagged_division_of
proof safe
show "finite ((λ(x, k). (g x, g ` k)) ` p)"
using ptag by auto
show "d fine (λ(x, k). (g x, g ` k)) ` p"
using finep unfolding fine_def d'_def by auto
next
fix x k
assume xk: "(x, k) ∈ p"
show "g x ∈ g ` k"
using p(2)[OF xk] by auto
show "∃u v. g ` k = cbox u v"
using p(4)[OF xk] using assms(5-6) by auto
fix x' K' u
assume xk': "(x', K') ∈ p" and u: "u ∈ interior (g ` k)" "u ∈ interior (g ` K')"
have "interior k ∩ interior K' ≠ {}"
proof
assume "interior k ∩ interior K' = {}"
moreover have "u ∈ g ` (interior k ∩ interior K')"
using interior_image_subset[OF ‹inj g› contg] u
unfolding image_Int[OF inj(1)] by blast
ultimately show False by blast
qed
then have same: "(x, k) = (x', K')"
using ptag xk' xk by blast
then show "g x = g x'"
by auto
show "g u ∈ g ` K'"if "u ∈ k" for u
using that same by auto
show "g u ∈ g ` k"if "u ∈ K'" for u
using that same by auto
next
fix x
assume "x ∈ cbox a b"
then have "h x ∈  ⋃{k. ∃x. (x, k) ∈ p}"
using p(6) by auto
then obtain X y where "h x ∈ X" "(y, X) ∈ p" by blast
then show "x ∈ ⋃{k. ∃x. (x, k) ∈ (λ(x, k). (g x, g ` k)) ` p}"
apply clarsimp
by (metis (no_types, lifting) assms(3) image_eqI pair_imageI)
qed (use gab in auto)
have *: "inj_on (λ(x, k). (g x, g ` k)) p"
using inj(1) unfolding inj_on_def by fastforce
have "(∑(x, k)∈(λ(x, k). (g x, g ` k)) ` p. content k *⇩R f x) - i = r *⇩R (∑(x, k)∈p. content k *⇩R f (g x)) - i" (is "?l = _")
using r
apply (simp only: algebra_simps add_left_cancel scaleR_right.sum)
apply (subst sum.reindex_bij_betw[symmetric, where h="λ(x, k). (g x, g ` k)" and S=p])
apply (auto intro!: * sum.cong simp: bij_betw_def dest!: p(4))
done
also have "… = r *⇩R ((∑(x, k)∈p. content k *⇩R f (g x)) - (1 / r) *⇩R i)" (is "_ = ?r")
using ‹0 < r› by (auto simp: scaleR_diff_right)
finally have eq: "?l = ?r" .
show "norm ((∑(x,K)∈p. content K *⇩R f (g x)) - (1 / r) *⇩R i) < e"
using d[OF gimp] ‹0 < r› by (auto simp add: eq)
qed
qed
then show ?thesis
by (auto simp: h_eq has_integral)
qed

subsection ‹Special case of a basic affine transformation›

lemma AE_lborel_inner_neq:
assumes k: "k ∈ Basis"
shows "AE x in lborel. x ∙ k ≠ c"
proof -
interpret finite_product_sigma_finite "λ_. lborel" Basis
proof qed simp

have "emeasure lborel {x∈space lborel. x ∙ k = c} = emeasure (Π⇩M j::'a∈Basis. lborel) (Π⇩E j∈Basis. if j = k then {c} else UNIV)"
using k
by (auto simp add: lborel_eq[where 'a='a] emeasure_distr intro!: arg_cong2[where f=emeasure])
(auto simp: space_PiM PiE_iff extensional_def split: if_split_asm)
also have "… = (∏j∈Basis. emeasure lborel (if j = k then {c} else UNIV))"
by (intro measure_times) auto
also have "… = 0"
by (intro prod_zero bexI[OF _ k]) auto
finally show ?thesis
by (subst AE_iff_measurable[OF _ refl]) auto
qed

lemma content_image_stretch_interval:
fixes m :: "'a::euclidean_space ⇒ real"
defines "s f x ≡ (∑k::'a∈Basis. (f k * (x∙k)) *⇩R k)"
shows "content (s m ` cbox a b) = ¦∏k∈Basis. m k¦ * content (cbox a b)"
proof cases
have s[measurable]: "s f ∈ borel →⇩M borel" for f
by (auto simp: s_def[abs_def])
assume m: "∀k∈Basis. m k ≠ 0"
then have s_comp_s: "s (λk. 1 / m k) ∘ s m = id" "s m ∘ s (λk. 1 / m k) = id"
by (auto simp: s_def[abs_def] fun_eq_iff euclidean_representation)
then have "inv (s (λk. 1 / m k)) = s m" "bij (s (λk. 1 / m k))"
by (auto intro: inv_unique_comp o_bij)
then have eq: "s m ` cbox a b = s (λk. 1 / m k) -` cbox a b"
using bij_vimage_eq_inv_image[OF ‹bij (s (λk. 1 / m k))›, of "cbox a b"] by auto
show ?thesis
using m unfolding eq measure_def
by (subst lborel_affine_euclidean[where c=m and t=0])
(simp_all add: emeasure_density measurable_sets_borel[OF s] abs_prod nn_integral_cmult
s_def[symmetric] emeasure_distr vimage_comp s_comp_s enn2real_mult prod_nonneg)
next
assume "¬ (∀k∈Basis. m k ≠ 0)"
then obtain k where k: "k ∈ Basis" "m k = 0" by auto
then have [simp]: "(∏k∈Basis. m k) = 0"
by (intro prod_zero) auto
have "emeasure lborel {x∈space lborel. x ∈ s m ` cbox a b} = 0"
proof (rule emeasure_eq_0_AE)
show "AE x in lborel. x ∉ s m ` cbox a b"
using AE_lborel_inner_neq[OF ‹k∈Basis›]
proof eventually_elim
show "x ∙ k ≠ 0 ⟹ x ∉ s m ` cbox a b " for x
using k by (auto simp: s_def[abs_def] cbox_def)
qed
qed
then show ?thesis
qed

lemma interval_image_affinity_interval:
"∃u v. (λx. m *⇩R (x::'a::euclidean_space) + c) ` cbox a b = cbox u v"
unfolding image_affinity_cbox
by auto

lemma content_image_affinity_cbox:
"content((λx::'a::euclidean_space. m *⇩R x + c) ` cbox a b) =
¦m¦ ^ DIM('a) * content (cbox a b)" (is "?l = ?r")
proof (cases "cbox a b = {}")
case True then show ?thesis by simp
next
case False
show ?thesis
proof (cases "m ≥ 0")
case True
with ‹cbox a b ≠ {}› have "cbox (m *⇩R a + c) (m *⇩R b + c) ≠ {}"
unfolding box_ne_empty
apply (intro ballI)
apply (erule_tac x=i in ballE)
apply (auto simp: inner_simps mult_left_mono)
done
moreover from True have *: "⋀i. (m *⇩R b + c) ∙ i - (m *⇩R a + c) ∙ i = m *⇩R (b-a) ∙ i"
ultimately show ?thesis
by (simp add: image_affinity_cbox True content_cbox'
prod.distrib prod_constant inner_diff_left)
next
case False
with ‹cbox a b ≠ {}› have "cbox (m *⇩R b + c) (m *⇩R a + c) ≠ {}"
unfolding box_ne_empty
apply (intro ballI)
apply (erule_tac x=i in ballE)
apply (auto simp: inner_simps mult_left_mono)
done
moreover from False have *: "⋀i. (m *⇩R a + c) ∙ i - (m *⇩R b + c) ∙ i = (-m) *⇩R (b-a) ∙ i"
ultimately show ?thesis using False
prod.distrib[symmetric] inner_diff_left flip: prod_constant)
qed
qed

lemma has_integral_affinity:
fixes a :: "'a::euclidean_space"
assumes "(f has_integral i) (cbox a b)"
and "m ≠ 0"
shows "((λx. f(m *⇩R x + c)) has_integral ((1 / (¦m¦ ^ DIM('a))) *⇩R i)) ((λx. (1 / m) *⇩R x + -((1 / m) *⇩R c)) ` cbox a b)"
apply (rule has_integral_twiddle)
using assms
apply (safe intro!: interval_image_affinity_interval content_image_affinity_cbox)
apply (rule zero_less_power)
unfolding scaleR_right_distrib
apply auto
done

lemma integrable_affinity:
assumes "f integrable_on cbox a b"
and "m ≠ 0"
shows "(λx. f(m *⇩R x + c)) integrable_on ((λx. (1 / m) *⇩R x + -((1/m) *⇩R c)) ` cbox a b)"
using assms
unfolding integrable_on_def
apply safe
apply (drule has_integral_affinity)
apply auto
done

lemmas has_integral_affinity01 = has_integral_affinity [of _ _ 0 "1::real", simplified]

subsection ‹Special case of stretching coordinate axes separately›

lemma has_integral_stretch:
fixes f :: "'a::euclidean_space ⇒ 'b::real_normed_vector"
assumes "(f has_integral i) (cbox a b)"
and "∀k∈Basis. m k ≠ 0"
shows "((λx. f (∑k∈Basis. (m k * (x∙k))*⇩R k)) has_integral
((1/ ¦prod m Basis¦) *⇩R i)) ((λx. (∑k∈Basis. (1 / m k * (x∙k))*⇩R k)) ` cbox a b)"
apply (rule has_integral_twiddle[where f=f])
unfolding zero_less_abs_iff content_image_stretch_interval
unfolding image_stretch_interval empty_as_interval euclidean_eq_iff[where 'a='a]
using assms
by auto

lemma integrable_stretch:
fixes f :: "'a::euclidean_space ⇒ 'b::real_normed_vector"
assumes "f integrable_on cbox a b"
and "∀k∈Basis. m k ≠ 0"
shows "(λx::'a. f (∑k∈Basis. (m k * (x∙k))*⇩R k)) integrable_on
((λx. ∑k∈Basis. (1 / m k * (x∙k))*⇩R k) ` cbox a b)"
using assms unfolding integrable_on_def
by (force dest: has_integral_stretch)

lemma vec_lambda_eq_sum:
shows "(χ k. f k (x \$ k)) = (∑k∈Basis. (f (axis_index k) (x ∙ k)) *⇩R k)"
apply (simp add: Basis_vec_def cart_eq_inner_axis UNION_singleton_eq_range sum.reindex axis_eq_axis inj_on_def)
apply (simp add: vec_eq_iff axis_def if_distrib cong: if_cong)
done

lemma has_integral_stretch_cart:
fixes m :: "'n::finite ⇒ real"
assumes f: "(f has_integral i) (cbox a b)" and m: "⋀k. m k ≠ 0"
shows "((λx. f(χ k. m k * x\$k)) has_integral i /⇩R ¦prod m UNIV¦)
((λx. χ k. x\$k / m k) ` (cbox a b))"
proof -
have *: "∀k:: real^'n ∈ Basis. m (axis_index k) ≠ 0"
using axis_index by (simp add: m)
have eqp: "(∏k:: real^'n ∈ Basis. m (axis_index k)) = prod m UNIV"
by (simp add: Basis_vec_def UNION_singleton_eq_range prod.reindex axis_eq_axis inj_on_def)
show ?thesis
using has_integral_stretch [OF f *] vec_lambda_eq_sum [where f="λi x. m i * x"] vec_lambda_eq_sum [where f="λi x. x / m i"]
qed

lemma image_stretch_interval_cart:
fixes m :: "'n::finite ⇒ real"
shows "(λx. χ k. m k * x\$k) ` cbox a b =
(if cbox a b = {} then {}
else cbox (χ k. min (m k * a\$k) (m k * b\$k)) (χ k. max (m k * a\$k) (m k * b\$k)))"
proof -
have *: "(∑k∈Basis. min (m (axis_index k) * (a ∙ k)) (m (axis_index k) * (b ∙ k)) *⇩R k)
= (χ k. min (m k * a \$ k) (m k * b \$ k))"
"(∑k∈Basis. max (m (axis_index k) * (a ∙ k)) (m (axis_index k) * (b ∙ k)) *⇩R k)
= (χ k. max (m k * a \$ k) (m k * b \$ k))"
apply (simp_all add: Basis_vec_def cart_eq_inner_axis UNION_singleton_eq_range sum.reindex axis_eq_axis inj_on_def)
apply (simp_all add: vec_eq_iff axis_def if_distrib cong: if_cong)
done
show ?thesis
by (simp add: vec_lambda_eq_sum [where f="λi x. m i * x"] image_stretch_interval eq_cbox *)
qed

subsection ‹even more special cases›

lemma uminus_interval_vector[simp]:
fixes a b :: "'a::euclidean_space"
shows "uminus ` cbox a b = cbox (-b) (-a)"
apply safe
apply (rule_tac x="-x" in image_eqI)
done

lemma has_integral_reflect_lemma[intro]:
assumes "(f has_integral i) (cbox a b)"
shows "((λx. f(-x)) has_integral i) (cbox (-b) (-a))"
using has_integral_affinity[OF assms, of "-1" 0]
by auto

lemma has_integral_reflect_lemma_real[intro]:
assumes "(f has_integral i) {a..b::real}"
shows "((λx. f(-x)) has_integral i) {-b .. -a}"
using assms
unfolding box_real[symmetric]
by (rule has_integral_reflect_lemma)

lemma has_integral_reflect[simp]:
"((λx. f (-x)) has_integral i) (cbox (-b) (-a)) ⟷ (f has_integral i) (cbox a b)"
by (auto dest: has_integral_reflect_lemma)

lemma integrable_reflect[simp]: "(λx. f(-x)) integrable_on cbox (-b) (-a) ⟷ f integrable_on cbox a b"
unfolding integrable_on_def by auto

lemma integrable_reflect_real[simp]: "(λx. f(-x)) integrable_on {-b .. -a} ⟷ f integrable_on {a..b::real}"
unfolding box_real[symmetric]
by (rule integrable_reflect)

lemma integral_reflect[simp]: "integral (cbox (-b) (-a)) (λx. f (-x)) = integral (cbox a b) f"
unfolding integral_def by auto

lemma integral_reflect_real[simp]: "integral {-b .. -a} (λx. f (-x)) = integral {a..b::real} f"
unfolding box_real[symmetric]
by (rule integral_reflect)

subsection ‹Stronger form of FCT; quite a tedious proof›

lemma split_minus[simp]: "(λ(x, k). f x k) x - (λ(x, k). g x k) x = (λ(x, k). f x k - g x k) x"

theorem fundamental_theorem_of_calculus_interior:
fixes f :: "real ⇒ 'a::real_normed_vector"
assumes "a ≤ b"
and contf: "continuous_on {a..b} f"
and derf: "⋀x. x ∈ {a <..< b} ⟹ (f has_vector_derivative f' x) (at x)"
shows "(f' has_integral (f b - f a)) {a..b}"
proof (cases "a = b")
case True
then have *: "cbox a b = {b}" "f b - f a = 0"
with True show ?thesis by auto
next
case False
with ‹a ≤ b› have ab: "a < b" by arith
show ?thesis
unfolding has_integral_factor_content_real
proof (intro allI impI)
fix e :: real
assume e: "e > 0"
then have eba8: "(e * (b-a)) / 8 > 0"
using ab by (auto simp add: field_simps)
note derf_exp = derf[unfolded has_vector_derivative_def has_derivative_at_alt]
have bounded: "⋀x. x ∈ {a<..<b} ⟹ bounded_linear (λu. u *⇩R f' x)"
using derf_exp by simp
have "∀x ∈ box a b. ∃d>0. ∀y. norm (y-x) < d ⟶ norm (f y - f x - (y-x) *⇩R f' x) ≤ e/2 * norm (y-x)"
(is "∀x ∈ box a b. ?Q x")
proof
fix x assume x: "x ∈ box a b"
show "?Q x"
apply (rule allE [where x="e/2", OF derf_exp [THEN conjunct2, of x]])
using x e by auto
qed
from this [unfolded bgauge_existence_lemma]
obtain d where d: "⋀x. 0 < d x"
"⋀x y. ⟦x ∈ box a b; norm (y-x) < d x⟧
⟹ norm (f y - f x - (y-x) *⇩R f' x) ≤ e/2 * norm (y-x)"
by metis
have "bounded (f ` cbox a b)"
using compact_cbox assms by (auto simp: compact_imp_bounded compact_continuous_image)
then obtain B
where "0 < B" and B: "⋀x. x ∈ f ` cbox a b ⟹ norm x ≤ B"
unfolding bounded_pos by metis
obtain da where "0 < da"
and da: "⋀c. ⟦a ≤ c; {a..c} ⊆ {a..b}; {a..c} ⊆ ball a da⟧
⟹ norm (content {a..c} *⇩R f' a - (f c - f a)) ≤ (e * (b-a)) / 4"
proof -
have "continuous (at a within {a..b}) f"
using contf continuous_on_eq_continuous_within by force
with eba8 obtain k where "0 < k"
and k: "⋀x. ⟦x ∈ {a..b}; 0 < norm (x-a); norm (x-a) < k⟧ ⟹ norm (f x - f a) < e * (b-a) / 8"
unfolding continuous_within Lim_within dist_norm by metis
obtain l where l: "0 < l" "norm (l *⇩R f' a) ≤ e * (b-a) / 8"
proof (cases "f' a = 0")
case True with ab e that show ?thesis by auto
next
case False
then show ?thesis
apply (rule_tac l="(e * (b-a)) / 8 / norm (f' a)" in that)
using ab e apply (auto simp add: field_simps)
done
qed
have "norm (content {a..c} *⇩R f' a - (f c - f a)) ≤ e * (b-a) / 4"
if "a ≤ c" "{a..c} ⊆ {a..b}" and bmin: "{a..c} ⊆ ball a (min k l)" for c
proof -
have minkl: "¦a - x¦ < min k l" if "x ∈ {a..c}" for x
using bmin dist_real_def that by auto
then have lel: "¦c - a¦ ≤ ¦l¦"
using that by force
have "norm ((c - a) *⇩R f' a - (f c - f a)) ≤ norm ((c - a) *⇩R f' a) + norm (f c - f a)"
by (rule norm_triangle_ineq4)
also have "… ≤ e * (b-a) / 8 + e * (b-a) / 8"
have "norm ((c - a) *⇩R f' a) ≤ norm (l *⇩R f' a)"
by (auto intro: mult_right_mono [OF lel])
also have "... ≤ e * (b-a) / 8"
by (rule l)
finally show "norm ((c - a) *⇩R f' a) ≤ e * (b-a) / 8" .
next
have "norm (f c - f a) < e * (b-a) / 8"
proof (cases "a = c")
case True then show ?thesis
using eba8 by auto
next
case False show ?thesis
by (rule k) (use minkl ‹a ≤ c› that False in auto)
qed
then show "norm (f c - f a) ≤ e * (b-a) / 8" by simp
qed
finally show "norm (content {a..c} *⇩R f' a - (f c - f a)) ≤ e * (b-a) / 4"
unfolding content_real[OF ‹a ≤ c›] by auto
qed
then show ?thesis
by (rule_tac da="min k l" in that) (auto simp: l ‹0 < k›)
qed
obtain db where "0 < db"
and db: "⋀c. ⟦c ≤ b; {c..b} ⊆ {a..b}; {c..b} ⊆ ball b db⟧
⟹ norm (content {c..b} *⇩R f' b - (f b - f c)) ≤ (e * (b-a)) / 4"
proof -
have "continuous (at b within {a..b}) f"
using contf continuous_on_eq_continuous_within by force
with eba8 obtain k
where "0 < k"
and k: "⋀x. ⟦x ∈ {a..b}; 0 < norm(x-b); norm(x-b) < k⟧
⟹ norm (f b - f x) < e * (b-a) / 8"
unfolding continuous_within Lim_within dist_norm norm_minus_commute by metis
obtain l where l: "0 < l" "norm (l *⇩R f' b) ≤ (e * (b-a)) / 8"
proof (cases "f' b = 0")
case True thus ?thesis
using ab e that by auto
next
case False then show ?thesis
apply (rule_tac l="(e * (b-a)) / 8 / norm (f' b)" in that)
using ab e by (auto simp add: field_simps)
qed
have "norm (content {c..b} *⇩R f' b - (f b - f c)) ≤ e * (b-a) / 4"
if "c ≤ b" "{c..b} ⊆ {a..b}" and bmin: "{c..b} ⊆ ball b (min k l)" for c
proof -
have minkl: "¦b - x¦ < min k l" if "x ∈ {c..b}" for x
using bmin dist_real_def that by auto
then have lel: "¦b - c¦ ≤ ¦l¦"
using that by force
have "norm ((b - c) *⇩R f' b - (f b - f c)) ≤ norm ((b - c) *⇩R f' b) + norm (f b - f c)"
by (rule norm_triangle_ineq4)
also have "… ≤ e * (b-a) / 8 + e * (b-a) / 8"
have "norm ((b - c) *⇩R f' b) ≤ norm (l *⇩R f' b)"
by (auto intro: mult_right_mono [OF lel])
also have "... ≤ e * (b-a) / 8"
by (rule l)
finally show "norm ((b - c) *⇩R f' b) ≤ e * (b-a) / 8" .
next
have "norm (f b - f c) < e * (b-a) / 8"
proof (cases "b = c")
case True with eba8 show ?thesis
by auto
next
case False show ?thesis
by (rule k) (use minkl ‹c ≤ b› that False in auto)
qed
then show "norm (f b - f c) ≤ e * (b-a) / 8" by simp
qed
finally show "norm (content {c..b} *⇩R f' b - (f b - f c)) ≤ e * (b-a) / 4"
unfolding content_real[OF ‹c ≤ b›] by auto
qed
then show ?thesis
by (rule_tac db="min k l" in that) (auto simp: l ‹0 < k›)
qed
let ?d = "(λx. ball x (if x=a then da else if x=b then db else d x))"
show "∃d. gauge d ∧ (∀p. p tagged_division_of {a..b} ∧ d fine p ⟶
norm ((∑(x,K)∈p. content K *⇩R f' x) - (f b - f a)) ≤ e * content {a..b})"
proof (rule exI, safe)
show "gauge ?d"
using ab ‹db > 0› ‹da > 0› d(1) by (auto intro: gauge_ball_dependent)
next
fix p
assume ptag: "p tagged_division_of {a..b}" and fine: "?d fine p"
let ?A = "{t. fst t ∈ {a, b}}"
note p = tagged_division_ofD[OF ptag]
have pA: "p = (p ∩ ?A) ∪ (p - ?A)" "finite (p ∩ ?A)" "finite (p - ?A)" "(p ∩ ?A) ∩ (p - ?A) = {}"
using ptag fine by auto
have le_xz: "⋀w x y z::real. y ≤ z/2 ⟹ w - x ≤ z/2 ⟹ w + y ≤ x + z"
by arith
have non: False if xk: "(x,K) ∈ p" and "x ≠ a" "x ≠ b"
and less: "e * (Sup K - Inf K)/2 < norm (content K *⇩R f' x - (f (Sup K) - f (Inf K)))"
for x K
proof -
obtain u v where k: "K = cbox u v"
using p(4) xk by blast
then have "u ≤ v" and uv: "{u, v} ⊆ cbox u v"
using p(2)[OF xk] by auto
then have result: "e * (v - u)/2 < norm ((v - u) *⇩R f' x - (f v - f u))"
using less[unfolded k box_real interval_bounds_real content_real] by auto
then have "x ∈ box a b"
using p(2) p(3) ‹x ≠ a› ‹x ≠ b› xk by fastforce
with d have *: "⋀y. norm (y-x) < d x
⟹ norm (f y - f x - (y-x) *⇩R f' x) ≤ e/2 * norm (y-x)"
by metis
have xd: "norm (u - x) < d x" "norm (v - x) < d x"
using fineD[OF fine xk] ‹x ≠ a› ‹x ≠ b› uv
by (auto simp add: k subset_eq dist_commute dist_real_def)
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 (rule arg_cong[where f=norm]) (auto simp: scaleR_left.diff)
also have "… ≤ e/2 * norm (u - x) + e/2 * norm (v - x)"
by (metis norm_triangle_le_diff add_mono * xd)
also have "… ≤ e/2 * norm (v - u)"
using p(2)[OF xk] by (auto simp add: field_simps k)
also have "… < norm ((v - u) *⇩R f' x - (f v - f u))"
using result by (simp add: ‹u ≤ v›)
finally have "e * (v - u)/2 < e * (v - u)/2"
using uv by auto
then show False by auto
qed
have "norm (∑(x, K)∈p - ?A. content K *⇩R f' x - (f (Sup K) - f (Inf K)))
≤ (∑(x, K)∈p - ?A. norm (content K *⇩R f' x - (f (Sup K) - f (Inf K))))"
by (auto intro: sum_norm_le)
also have "... ≤ (∑n∈p - ?A. e * (case n of (x, k) ⇒ Sup k - Inf k)/2)"
using non by (fastforce intro: sum_mono)
finally have I: "norm (∑(x, k)∈p - ?A.
content k *⇩R f' x - (f (Sup k) - f (Inf k)))
≤ (∑n∈p - ?A. e * (case n of (x, k) ⇒ Sup k - Inf k))/2"
have II: "norm (∑(x, k)∈p ∩ ?A. content k *⇩R f' x - (f (Sup k) - f (Inf k))) -
(∑n∈p ∩ ?A. e * (case n of (x, k) ⇒ Sup k - Inf k))
≤ (∑n∈p - ?A. e * (case n of (x, k) ⇒ Sup k - Inf k))/2"
proof -
have ge0: "0 ≤ e * (Sup k - Inf k)" if xkp: "(x, k) ∈ p ∩ ?A" for x k
proof -
obtain u v where uv: "k = cbox u v"
by (meson Int_iff xkp p(4))
with p(2) that uv have "cbox u v ≠ {}"
by blast
then show "0 ≤ e * ((Sup k) - (Inf k))"
unfolding uv using e by (auto simp add: field_simps)
qed
let ?B = "λx. {t ∈ p. fst t = x ∧ content (snd t) ≠ 0}"
let ?C = "{t ∈ p. fst t ∈ {a, b} ∧ content (snd t) ≠ 0}"
have "norm (∑(x, k)∈p ∩ {t. fst t ∈ {a, b}}. content k *⇩R f' x - (f (Sup k) - f (Inf k))) ≤ e * (b-a)/2"
proof -
have *: "⋀S f e. sum f S = sum f (p ∩ ?C) ⟹ norm (sum f (p ∩ ?C)) ≤ e ⟹ norm (sum f S) ≤ e"
by auto
have 1: "content K *⇩R (f' x) - (f ((Sup K)) - f ((Inf K))) = 0"
if "(x,K) ∈ p ∩ {t. fst t ∈ {a, b}} - p ∩ ?C" for x K
proof -
have xk: "(x,K) ∈ p" and k0: "content K = 0"
using that by auto
then obtain u v where uv: "K = cbox u v"
using p(4) by blast
then have "u = v"
using xk k0 p(2) by force
then show "content K *⇩R (f' x) - (f ((Sup K)) - f ((Inf K))) = 0"
using xk unfolding uv by auto
qed
have 2: "norm(∑(x,K)∈p ∩ ?C. content K *⇩R f' x - (f (Sup K) - f (Inf K)))  ≤ e * (b-a)/2"
proof -
have norm_le: "norm (sum f S) ≤ e"
if "⋀x y. ⟦x ∈ S; y ∈ S⟧ ⟹ x = y" "⋀x. x ∈ S ⟹ norm (f x) ≤ e" "e > 0"
for S f and e :: real
proof (cases "S = {}")
case True
with that show ?thesis by auto
next
case False then obtain x where "x ∈ S"
by auto
then have "S = {x}"
using that(1) by auto
then show ?thesis
using ‹x ∈ S› that(2) by auto
qed
have *: "p ∩ ?C = ?B a ∪ ?B b"
by blast
then have "norm (∑(x,K)∈p ∩ ?C. content K *⇩R f' x - (f (Sup K) - f (Inf K))) =
norm (∑(x,K) ∈ ?B a ∪ ?B b. content K *⇩R f' x - (f (Sup K) - f (Inf K)))"
by simp
also have "... = norm ((∑(x,K) ∈ ?B a. content K *⇩R f' x - (f (Sup K) - f (Inf K))) +
(∑(x,K) ∈ ?B b. content K *⇩R f' x - (f (Sup K) - f (Inf K))))"
apply (subst sum.union_disjoint)
using p(1) ab e by auto
also have "... ≤ e * (b - a) / 4 + e * (b - a) / 4"
have pa: "∃v. k = cbox a v ∧ a ≤ v" if "(a, k) ∈ p" for k
using p(2) p(3) p(4) that by fastforce
show "norm (∑(x,K) ∈ ?B a. content K *⇩R f' x - (f (Sup K) - f (Inf K))) ≤ e * (b - a) / 4"
proof (intro norm_le; clarsimp)
fix K K'
assume K: "(a, K) ∈ p" "(a, K') ∈ p" and ne0: "content K ≠ 0" "content K' ≠ 0"
with pa obtain v v' where v: "K = cbox a v" "a ≤ v" and v': "K' = cbox a v'" "a ≤ v'"
by blast
let ?v = "min v v'"
have "box a ?v ⊆ K ∩ K'"
unfolding v v' by (auto simp add: mem_box)
then have "interior (box a (min v v')) ⊆ interior K ∩ interior K'"
using interior_Int interior_mono by blast
moreover have "(a + ?v)/2 ∈ box a ?v"
using ne0  unfolding v v' content_eq_0 not_le
ultimately have "(a + ?v)/2 ∈ interior K ∩ interior K'"
unfolding interior_open[OF open_box] by auto
then show "K = K'"
using p(5)[OF K] by auto
next
fix K
assume K: "(a, K) ∈ p" and ne0: "content K ≠ 0"
show "norm (content c *⇩R f' a - (f (Sup c) - f (Inf c))) * 4 ≤ e * (b-a)"
if "(a, c) ∈ p" and ne0: "content c ≠ 0" for c
proof -
obtain v where v: "c = cbox a v" and "a ≤ v"
using pa[OF ‹(a, c) ∈ p›] by metis
then have "a ∈ {a..v}" "v ≤ b"
using p(3)[OF ‹(a, c) ∈ p›] by auto
moreover have "{a..v} ⊆ ball a da"
using fineD[OF ‹?d fine p› ‹(a, c) ∈ p›] by (simp add: v split: if_split_asm)
ultimately show ?thesis
unfolding v interval_bounds_real[OF ‹a ≤ v›] box_real
using da ‹a ≤ v› by auto
qed
qed (use ab e in auto)
next
have pb: "∃v. k = cbox v b ∧ b ≥ v" if "(b, k) ∈ p" for k
using p(2) p(3) p(4) that by fastforce
show "norm (∑(x,K) ∈ ?B b. content K *⇩R f' x - (f (Sup K) - f (Inf K))) ≤ e * (b - a) / 4"
proof (intro norm_le; clarsimp)
fix K K'
assume K: "(b, K) ∈ p" "(b, K') ∈ p" and ne0: "content K ≠ 0" "content K' ≠ 0"
with pb obtain v v' where v: "K = cbox v b" "v ≤ b" and v': "K' = cbox v' b" "v' ≤ b"
by blast
let ?v = "max v v'"
have "box ?v b ⊆ K ∩ K'"
unfolding v v' by (auto simp: mem_box)
then have "interior (box (max v v') b) ⊆ interior K ∩ interior K'"
using interior_Int interior_mono by blast
moreover have " ((b + ?v)/2) ∈ box ?v b"
using ne0 unfolding v v' content_eq_0 not_le by (auto simp: mem_box)
ultimately ```