src/HOL/Multivariate_Analysis/Integration.thy
author hoelzl
Mon Feb 22 20:41:49 2010 +0100 (2010-02-22)
changeset 35292 e4a431b6d9b7
parent 35291 src/HOL/Multivariate_Analysis/Integration_MV.thy@ead7bfc30b26
child 35540 3d073a3e1c61
permissions -rw-r--r--
Replaced Integration by Multivariate-Analysis/Real_Integration
himmelma@35172
     1
himmelma@35172
     2
header {* Kurzweil-Henstock gauge integration in many dimensions. *}
himmelma@35172
     3
(*  Author:                     John Harrison
himmelma@35172
     4
    Translation from HOL light: Robert Himmelmann, TU Muenchen *)
himmelma@35172
     5
hoelzl@35292
     6
theory Integration
himmelma@35172
     7
  imports Derivative SMT
himmelma@35172
     8
begin
himmelma@35172
     9
hoelzl@35292
    10
declare [[smt_certificates="~~/src/HOL/Multivariate_Analysis/Integration.cert"]]
himmelma@35172
    11
declare [[smt_record=true]]
himmelma@35172
    12
declare [[z3_proofs=true]]
himmelma@35172
    13
himmelma@35172
    14
lemma conjunctD2: assumes "a \<and> b" shows a b using assms by auto
himmelma@35172
    15
lemma conjunctD3: assumes "a \<and> b \<and> c" shows a b c using assms by auto
himmelma@35172
    16
lemma conjunctD4: assumes "a \<and> b \<and> c \<and> d" shows a b c d using assms by auto
himmelma@35172
    17
lemma conjunctD5: assumes "a \<and> b \<and> c \<and> d \<and> e" shows a b c d e using assms by auto
himmelma@35172
    18
himmelma@35172
    19
declare smult_conv_scaleR[simp]
himmelma@35172
    20
himmelma@35172
    21
subsection {* Some useful lemmas about intervals. *}
himmelma@35172
    22
himmelma@35172
    23
lemma empty_as_interval: "{} = {1..0::real^'n}"
himmelma@35172
    24
  apply(rule set_ext,rule) defer unfolding vector_le_def mem_interval
himmelma@35172
    25
  using UNIV_witness[where 'a='n] apply(erule_tac exE,rule_tac x=x in allE) by auto
himmelma@35172
    26
himmelma@35172
    27
lemma interior_subset_union_intervals: 
himmelma@35172
    28
  assumes "i = {a..b::real^'n}" "j = {c..d}" "interior j \<noteq> {}" "i \<subseteq> j \<union> s" "interior(i) \<inter> interior(j) = {}"
himmelma@35172
    29
  shows "interior i \<subseteq> interior s" proof-
himmelma@35172
    30
  have "{a<..<b} \<inter> {c..d} = {}" using inter_interval_mixed_eq_empty[of c d a b] and assms(3,5)
himmelma@35172
    31
    unfolding assms(1,2) interior_closed_interval by auto
himmelma@35172
    32
  moreover have "{a<..<b} \<subseteq> {c..d} \<union> s" apply(rule order_trans,rule interval_open_subset_closed)
himmelma@35172
    33
    using assms(4) unfolding assms(1,2) by auto
himmelma@35172
    34
  ultimately show ?thesis apply-apply(rule interior_maximal) defer apply(rule open_interior)
himmelma@35172
    35
    unfolding assms(1,2) interior_closed_interval by auto qed
himmelma@35172
    36
himmelma@35172
    37
lemma inter_interior_unions_intervals: fixes f::"(real^'n) set set"
himmelma@35172
    38
  assumes "finite f" "open s" "\<forall>t\<in>f. \<exists>a b. t = {a..b}" "\<forall>t\<in>f. s \<inter> (interior t) = {}"
himmelma@35172
    39
  shows "s \<inter> interior(\<Union>f) = {}" proof(rule ccontr,unfold ex_in_conv[THEN sym]) case goal1
himmelma@35172
    40
  have lem1:"\<And>x e s U. ball x e \<subseteq> s \<inter> interior U \<longleftrightarrow> ball x e \<subseteq> s \<inter> U" apply rule  defer apply(rule_tac Int_greatest)
himmelma@35172
    41
    unfolding open_subset_interior[OF open_ball]  using interior_subset by auto
himmelma@35172
    42
  have lem2:"\<And>x s P. \<exists>x\<in>s. P x \<Longrightarrow> \<exists>x\<in>insert x s. P x" by auto
himmelma@35172
    43
  have "\<And>f. finite f \<Longrightarrow> (\<forall>t\<in>f. \<exists>a b. t = {a..b}) \<Longrightarrow> (\<exists>x. x \<in> s \<inter> interior (\<Union>f)) \<Longrightarrow> (\<exists>t\<in>f. \<exists>x. \<exists>e>0. ball x e \<subseteq> s \<inter> t)" proof- case goal1
himmelma@35172
    44
  thus ?case proof(induct rule:finite_induct) 
himmelma@35172
    45
    case empty from this(2) guess x .. hence False unfolding Union_empty interior_empty by auto thus ?case by auto next
himmelma@35172
    46
    case (insert i f) guess x using insert(5) .. note x = this
himmelma@35172
    47
    then guess e unfolding open_contains_ball_eq[OF open_Int[OF assms(2) open_interior],rule_format] .. note e=this
himmelma@35172
    48
    guess a using insert(4)[rule_format,OF insertI1] .. then guess b .. note ab = this
himmelma@35172
    49
    show ?case proof(cases "x\<in>i") case False hence "x \<in> UNIV - {a..b}" unfolding ab by auto
himmelma@35172
    50
      then guess d unfolding open_contains_ball_eq[OF open_Diff[OF open_UNIV closed_interval],rule_format] ..
himmelma@35172
    51
      hence "0 < d" "ball x (min d e) \<subseteq> UNIV - i" using e unfolding ab by auto
himmelma@35172
    52
      hence "ball x (min d e) \<subseteq> s \<inter> interior (\<Union>f)" using e unfolding lem1 by auto hence "x \<in> s \<inter> interior (\<Union>f)" using `d>0` e by auto
himmelma@35172
    53
      hence "\<exists>t\<in>f. \<exists>x e. 0 < e \<and> ball x e \<subseteq> s \<inter> t" apply-apply(rule insert(3)) using insert(4) by auto thus ?thesis by auto next
himmelma@35172
    54
    case True show ?thesis proof(cases "x\<in>{a<..<b}")
himmelma@35172
    55
      case True then guess d unfolding open_contains_ball_eq[OF open_interval,rule_format] ..
himmelma@35172
    56
      thus ?thesis apply(rule_tac x=i in bexI,rule_tac x=x in exI,rule_tac x="min d e" in exI)
himmelma@35172
    57
	unfolding ab using interval_open_subset_closed[of a b] and e by fastsimp+ next
himmelma@35172
    58
    case False then obtain k where "x$k \<le> a$k \<or> x$k \<ge> b$k" unfolding mem_interval by(auto simp add:not_less) 
himmelma@35172
    59
    hence "x$k = a$k \<or> x$k = b$k" using True unfolding ab and mem_interval apply(erule_tac x=k in allE) by auto
himmelma@35172
    60
    hence "\<exists>x. ball x (e/2) \<subseteq> s \<inter> (\<Union>f)" proof(erule_tac disjE)
himmelma@35172
    61
      let ?z = "x - (e/2) *\<^sub>R basis k" assume as:"x$k = a$k" have "ball ?z (e / 2) \<inter> i = {}" apply(rule ccontr) unfolding ex_in_conv[THEN sym] proof(erule exE)
himmelma@35172
    62
	fix y assume "y \<in> ball ?z (e / 2) \<inter> i" hence "dist ?z y < e/2" and yi:"y\<in>i" by auto
himmelma@35172
    63
	hence "\<bar>(?z - y) $ k\<bar> < e/2" using component_le_norm[of "?z - y" k] unfolding vector_dist_norm by auto
himmelma@35172
    64
	hence "y$k < a$k" unfolding vector_component_simps vector_scaleR_component as using e[THEN conjunct1] by(auto simp add:field_simps)
himmelma@35172
    65
	hence "y \<notin> i" unfolding ab mem_interval not_all by(rule_tac x=k in exI,auto) thus False using yi by auto qed
himmelma@35172
    66
      moreover have "ball ?z (e/2) \<subseteq> s \<inter> (\<Union>insert i f)" apply(rule order_trans[OF _ e[THEN conjunct2, unfolded lem1]]) proof
himmelma@35172
    67
	fix y assume as:"y\<in> ball ?z (e/2)" have "norm (x - y) \<le> \<bar>e\<bar> / 2 + norm (x - y - (e / 2) *\<^sub>R basis k)"
himmelma@35172
    68
	   apply-apply(rule order_trans,rule norm_triangle_sub[of "x - y" "(e/2) *\<^sub>R basis k"])
himmelma@35172
    69
	  unfolding norm_scaleR norm_basis by auto
himmelma@35172
    70
	also have "\<dots> < \<bar>e\<bar> / 2 + \<bar>e\<bar> / 2" apply(rule add_strict_left_mono) using as unfolding mem_ball vector_dist_norm using e by(auto simp add:field_simps)
himmelma@35172
    71
	finally show "y\<in>ball x e" unfolding mem_ball vector_dist_norm using e by(auto simp add:field_simps) qed
himmelma@35172
    72
      ultimately show ?thesis apply(rule_tac x="?z" in exI) unfolding Union_insert by auto
himmelma@35172
    73
    next let ?z = "x + (e/2) *\<^sub>R basis k" assume as:"x$k = b$k" have "ball ?z (e / 2) \<inter> i = {}" apply(rule ccontr) unfolding ex_in_conv[THEN sym] proof(erule exE)
himmelma@35172
    74
	fix y assume "y \<in> ball ?z (e / 2) \<inter> i" hence "dist ?z y < e/2" and yi:"y\<in>i" by auto
himmelma@35172
    75
	hence "\<bar>(?z - y) $ k\<bar> < e/2" using component_le_norm[of "?z - y" k] unfolding vector_dist_norm by auto
himmelma@35172
    76
	hence "y$k > b$k" unfolding vector_component_simps vector_scaleR_component as using e[THEN conjunct1] by(auto simp add:field_simps)
himmelma@35172
    77
	hence "y \<notin> i" unfolding ab mem_interval not_all by(rule_tac x=k in exI,auto) thus False using yi by auto qed
himmelma@35172
    78
      moreover have "ball ?z (e/2) \<subseteq> s \<inter> (\<Union>insert i f)" apply(rule order_trans[OF _ e[THEN conjunct2, unfolded lem1]]) proof
himmelma@35172
    79
	fix y assume as:"y\<in> ball ?z (e/2)" have "norm (x - y) \<le> \<bar>e\<bar> / 2 + norm (x - y + (e / 2) *\<^sub>R basis k)"
himmelma@35172
    80
	   apply-apply(rule order_trans,rule norm_triangle_sub[of "x - y" "- (e/2) *\<^sub>R basis k"])
himmelma@35172
    81
	  unfolding norm_scaleR norm_basis by auto
himmelma@35172
    82
	also have "\<dots> < \<bar>e\<bar> / 2 + \<bar>e\<bar> / 2" apply(rule add_strict_left_mono) using as unfolding mem_ball vector_dist_norm using e by(auto simp add:field_simps)
himmelma@35172
    83
	finally show "y\<in>ball x e" unfolding mem_ball vector_dist_norm using e by(auto simp add:field_simps) qed
himmelma@35172
    84
      ultimately show ?thesis apply(rule_tac x="?z" in exI) unfolding Union_insert by auto qed 
himmelma@35172
    85
    then guess x .. hence "x \<in> s \<inter> interior (\<Union>f)" unfolding lem1[where U="\<Union>f",THEN sym] using centre_in_ball e[THEN conjunct1] by auto
himmelma@35172
    86
    thus ?thesis apply-apply(rule lem2,rule insert(3)) using insert(4) by auto qed qed qed qed note * = this
himmelma@35172
    87
  guess t using *[OF assms(1,3) goal1]  .. from this(2) guess x .. then guess e ..
himmelma@35172
    88
  hence "x \<in> s" "x\<in>interior t" defer using open_subset_interior[OF open_ball, of x e t] by auto
himmelma@35172
    89
  thus False using `t\<in>f` assms(4) by auto qed
himmelma@35172
    90
subsection {* Bounds on intervals where they exist. *}
himmelma@35172
    91
himmelma@35172
    92
definition "interval_upperbound (s::(real^'n) set) = (\<chi> i. Sup {a. \<exists>x\<in>s. x$i = a})"
himmelma@35172
    93
himmelma@35172
    94
definition "interval_lowerbound (s::(real^'n) set) = (\<chi> i. Inf {a. \<exists>x\<in>s. x$i = a})"
himmelma@35172
    95
himmelma@35172
    96
lemma interval_upperbound[simp]: assumes "\<forall>i. a$i \<le> b$i" shows "interval_upperbound {a..b} = b"
himmelma@35172
    97
  using assms unfolding interval_upperbound_def Cart_eq Cart_lambda_beta apply-apply(rule,erule_tac x=i in allE)
himmelma@35172
    98
  apply(rule Sup_unique) unfolding setle_def apply rule unfolding mem_Collect_eq apply(erule bexE) unfolding mem_interval defer
himmelma@35172
    99
  apply(rule,rule) apply(rule_tac x="b$i" in bexI) defer unfolding mem_Collect_eq apply(rule_tac x=b in bexI)
himmelma@35172
   100
  unfolding mem_interval using assms by auto
himmelma@35172
   101
himmelma@35172
   102
lemma interval_lowerbound[simp]: assumes "\<forall>i. a$i \<le> b$i" shows "interval_lowerbound {a..b} = a"
himmelma@35172
   103
  using assms unfolding interval_lowerbound_def Cart_eq Cart_lambda_beta apply-apply(rule,erule_tac x=i in allE)
himmelma@35172
   104
  apply(rule Inf_unique) unfolding setge_def apply rule unfolding mem_Collect_eq apply(erule bexE) unfolding mem_interval defer
himmelma@35172
   105
  apply(rule,rule) apply(rule_tac x="a$i" in bexI) defer unfolding mem_Collect_eq apply(rule_tac x=a in bexI)
himmelma@35172
   106
  unfolding mem_interval using assms by auto
himmelma@35172
   107
himmelma@35172
   108
lemmas interval_bounds = interval_upperbound interval_lowerbound
himmelma@35172
   109
himmelma@35172
   110
lemma interval_bounds'[simp]: assumes "{a..b}\<noteq>{}" shows "interval_upperbound {a..b} = b" "interval_lowerbound {a..b} = a"
himmelma@35172
   111
  using assms unfolding interval_ne_empty by auto
himmelma@35172
   112
himmelma@35172
   113
lemma interval_upperbound_1[simp]: "dest_vec1 a \<le> dest_vec1 b \<Longrightarrow> interval_upperbound {a..b} = (b::real^1)"
himmelma@35172
   114
  apply(rule interval_upperbound) by auto
himmelma@35172
   115
himmelma@35172
   116
lemma interval_lowerbound_1[simp]: "dest_vec1 a \<le> dest_vec1 b \<Longrightarrow> interval_lowerbound {a..b} = (a::real^1)"
himmelma@35172
   117
  apply(rule interval_lowerbound) by auto
himmelma@35172
   118
himmelma@35172
   119
lemmas interval_bound_1 = interval_upperbound_1 interval_lowerbound_1
himmelma@35172
   120
himmelma@35172
   121
subsection {* Content (length, area, volume...) of an interval. *}
himmelma@35172
   122
himmelma@35172
   123
definition "content (s::(real^'n) set) =
himmelma@35172
   124
       (if s = {} then 0 else (\<Prod>i\<in>UNIV. (interval_upperbound s)$i - (interval_lowerbound s)$i))"
himmelma@35172
   125
himmelma@35172
   126
lemma interval_not_empty:"\<forall>i. a$i \<le> b$i \<Longrightarrow> {a..b::real^'n} \<noteq> {}"
himmelma@35172
   127
  unfolding interval_eq_empty unfolding not_ex not_less by assumption
himmelma@35172
   128
himmelma@35172
   129
lemma content_closed_interval: assumes "\<forall>i. a$i \<le> b$i"
himmelma@35172
   130
  shows "content {a..b} = (\<Prod>i\<in>UNIV. b$i - a$i)"
himmelma@35172
   131
  using interval_not_empty[OF assms] unfolding content_def interval_upperbound[OF assms] interval_lowerbound[OF assms] by auto
himmelma@35172
   132
himmelma@35172
   133
lemma content_closed_interval': assumes "{a..b}\<noteq>{}" shows "content {a..b} = (\<Prod>i\<in>UNIV. b$i - a$i)"
himmelma@35172
   134
  apply(rule content_closed_interval) using assms unfolding interval_ne_empty .
himmelma@35172
   135
himmelma@35172
   136
lemma content_1:"dest_vec1 a \<le> dest_vec1 b \<Longrightarrow> content {a..b} = dest_vec1 b - dest_vec1 a"
himmelma@35172
   137
  using content_closed_interval[of a b] by auto
himmelma@35172
   138
himmelma@35172
   139
lemma content_1':"a \<le> b \<Longrightarrow> content {vec1 a..vec1 b} = b - a" using content_1[of "vec a" "vec b"] by auto
himmelma@35172
   140
himmelma@35172
   141
lemma content_unit[intro]: "content{0..1::real^'n} = 1" proof-
himmelma@35172
   142
  have *:"\<forall>i. 0$i \<le> (1::real^'n::finite)$i" by auto
himmelma@35172
   143
  have "0 \<in> {0..1::real^'n::finite}" unfolding mem_interval by auto
himmelma@35172
   144
  thus ?thesis unfolding content_def interval_bounds[OF *] using setprod_1 by auto qed
himmelma@35172
   145
himmelma@35172
   146
lemma content_pos_le[intro]: "0 \<le> content {a..b}" proof(cases "{a..b}={}")
himmelma@35172
   147
  case False hence *:"\<forall>i. a $ i \<le> b $ i" unfolding interval_ne_empty by assumption
himmelma@35172
   148
  have "(\<Prod>i\<in>UNIV. interval_upperbound {a..b} $ i - interval_lowerbound {a..b} $ i) \<ge> 0"
himmelma@35172
   149
    apply(rule setprod_nonneg) unfolding interval_bounds[OF *] using * apply(erule_tac x=x in allE) by auto
himmelma@35172
   150
  thus ?thesis unfolding content_def by(auto simp del:interval_bounds') qed(unfold content_def, auto)
himmelma@35172
   151
himmelma@35172
   152
lemma content_pos_lt: assumes "\<forall>i. a$i < b$i" shows "0 < content {a..b}"
himmelma@35172
   153
proof- have help_lemma1: "\<forall>i. a$i < b$i \<Longrightarrow> \<forall>i. a$i \<le> ((b$i)::real)" apply(rule,erule_tac x=i in allE) by auto
himmelma@35172
   154
  show ?thesis unfolding content_closed_interval[OF help_lemma1[OF assms]] apply(rule setprod_pos)
himmelma@35172
   155
    using assms apply(erule_tac x=x in allE) by auto qed
himmelma@35172
   156
himmelma@35172
   157
lemma content_pos_lt_1: "dest_vec1 a < dest_vec1 b \<Longrightarrow> 0 < content({a..b})"
himmelma@35172
   158
  apply(rule content_pos_lt) by auto
himmelma@35172
   159
himmelma@35172
   160
lemma content_eq_0: "content({a..b::real^'n}) = 0 \<longleftrightarrow> (\<exists>i. b$i \<le> a$i)" proof(cases "{a..b} = {}")
himmelma@35172
   161
  case True thus ?thesis unfolding content_def if_P[OF True] unfolding interval_eq_empty apply-
himmelma@35172
   162
    apply(rule,erule exE) apply(rule_tac x=i in exI) by auto next
himmelma@35172
   163
  guess a using UNIV_witness[where 'a='n] .. case False note as=this[unfolded interval_eq_empty not_ex not_less]
himmelma@35172
   164
  show ?thesis unfolding content_def if_not_P[OF False] setprod_zero_iff[OF finite_UNIV]
himmelma@35172
   165
    apply(rule) apply(erule_tac[!] exE bexE) unfolding interval_bounds[OF as] apply(rule_tac x=x in exI) defer
himmelma@35172
   166
    apply(rule_tac x=i in bexI) using as apply(erule_tac x=i in allE) by auto qed
himmelma@35172
   167
himmelma@35172
   168
lemma cond_cases:"(P \<Longrightarrow> Q x) \<Longrightarrow> (\<not> P \<Longrightarrow> Q y) \<Longrightarrow> Q (if P then x else y)" by auto
himmelma@35172
   169
himmelma@35172
   170
lemma content_closed_interval_cases:
himmelma@35172
   171
  "content {a..b} = (if \<forall>i. a$i \<le> b$i then setprod (\<lambda>i. b$i - a$i) UNIV else 0)" apply(rule cond_cases) 
himmelma@35172
   172
  apply(rule content_closed_interval) unfolding content_eq_0 not_all not_le defer apply(erule exE,rule_tac x=x in exI) by auto
himmelma@35172
   173
himmelma@35172
   174
lemma content_eq_0_interior: "content {a..b} = 0 \<longleftrightarrow> interior({a..b}) = {}"
himmelma@35172
   175
  unfolding content_eq_0 interior_closed_interval interval_eq_empty by auto
himmelma@35172
   176
himmelma@35172
   177
lemma content_eq_0_1: "content {a..b::real^1} = 0 \<longleftrightarrow> dest_vec1 b \<le> dest_vec1 a"
himmelma@35172
   178
  unfolding content_eq_0 by auto
himmelma@35172
   179
himmelma@35172
   180
lemma content_pos_lt_eq: "0 < content {a..b} \<longleftrightarrow> (\<forall>i. a$i < b$i)"
himmelma@35172
   181
  apply(rule) defer apply(rule content_pos_lt,assumption) proof- assume "0 < content {a..b}"
himmelma@35172
   182
  hence "content {a..b} \<noteq> 0" by auto thus "\<forall>i. a$i < b$i" unfolding content_eq_0 not_ex not_le by auto qed
himmelma@35172
   183
himmelma@35172
   184
lemma content_empty[simp]: "content {} = 0" unfolding content_def by auto
himmelma@35172
   185
himmelma@35172
   186
lemma content_subset: assumes "{a..b} \<subseteq> {c..d}" shows "content {a..b::real^'n} \<le> content {c..d}" proof(cases "{a..b}={}")
himmelma@35172
   187
  case True thus ?thesis using content_pos_le[of c d] by auto next
himmelma@35172
   188
  case False hence ab_ne:"\<forall>i. a $ i \<le> b $ i" unfolding interval_ne_empty by auto
himmelma@35172
   189
  hence ab_ab:"a\<in>{a..b}" "b\<in>{a..b}" unfolding mem_interval by auto
himmelma@35172
   190
  have "{c..d} \<noteq> {}" using assms False by auto
himmelma@35172
   191
  hence cd_ne:"\<forall>i. c $ i \<le> d $ i" using assms unfolding interval_ne_empty by auto
himmelma@35172
   192
  show ?thesis unfolding content_def unfolding interval_bounds[OF ab_ne] interval_bounds[OF cd_ne]
himmelma@35172
   193
    unfolding if_not_P[OF False] if_not_P[OF `{c..d} \<noteq> {}`] apply(rule setprod_mono,rule) proof fix i::'n
himmelma@35172
   194
    show "0 \<le> b $ i - a $ i" using ab_ne[THEN spec[where x=i]] by auto
himmelma@35172
   195
    show "b $ i - a $ i \<le> d $ i - c $ i"
himmelma@35172
   196
      using assms[unfolded subset_eq mem_interval,rule_format,OF ab_ab(2),of i]
himmelma@35172
   197
      using assms[unfolded subset_eq mem_interval,rule_format,OF ab_ab(1),of i] by auto qed qed
himmelma@35172
   198
himmelma@35172
   199
lemma content_lt_nz: "0 < content {a..b} \<longleftrightarrow> content {a..b} \<noteq> 0"
himmelma@35172
   200
  unfolding content_pos_lt_eq content_eq_0 unfolding not_ex not_le by auto
himmelma@35172
   201
himmelma@35172
   202
subsection {* The notion of a gauge --- simply an open set containing the point. *}
himmelma@35172
   203
himmelma@35172
   204
definition gauge where "gauge d \<longleftrightarrow> (\<forall>x. x\<in>(d x) \<and> open(d x))"
himmelma@35172
   205
himmelma@35172
   206
lemma gaugeI:assumes "\<And>x. x\<in>g x" "\<And>x. open (g x)" shows "gauge g"
himmelma@35172
   207
  using assms unfolding gauge_def by auto
himmelma@35172
   208
himmelma@35172
   209
lemma gaugeD[dest]: assumes "gauge d" shows "x\<in>d x" "open (d x)" using assms unfolding gauge_def by auto
himmelma@35172
   210
himmelma@35172
   211
lemma gauge_ball_dependent: "\<forall>x. 0 < e x \<Longrightarrow> gauge (\<lambda>x. ball x (e x))"
himmelma@35172
   212
  unfolding gauge_def by auto 
himmelma@35172
   213
himmelma@35172
   214
lemma gauge_ball[intro?]: "0 < e \<Longrightarrow> gauge (\<lambda>x. ball x e)" unfolding gauge_def by auto 
himmelma@35172
   215
himmelma@35172
   216
lemma gauge_trivial[intro]: "gauge (\<lambda>x. ball x 1)" apply(rule gauge_ball) by auto
himmelma@35172
   217
himmelma@35172
   218
lemma gauge_inter: "gauge d1 \<Longrightarrow> gauge d2 \<Longrightarrow> gauge (\<lambda>x. (d1 x) \<inter> (d2 x))"
himmelma@35172
   219
  unfolding gauge_def by auto 
himmelma@35172
   220
himmelma@35172
   221
lemma gauge_inters: assumes "finite s" "\<forall>d\<in>s. gauge (f d)" shows "gauge(\<lambda>x. \<Inter> {f d x | d. d \<in> s})" proof-
himmelma@35172
   222
  have *:"\<And>x. {f d x |d. d \<in> s} = (\<lambda>d. f d x) ` s" by auto show ?thesis
himmelma@35172
   223
  unfolding gauge_def unfolding * 
himmelma@35172
   224
  using assms unfolding Ball_def Inter_iff mem_Collect_eq gauge_def by auto qed
himmelma@35172
   225
himmelma@35172
   226
lemma gauge_existence_lemma: "(\<forall>x. \<exists>d::real. p x \<longrightarrow> 0 < d \<and> q d x) \<longleftrightarrow> (\<forall>x. \<exists>d>0. p x \<longrightarrow> q d x)" by(meson zero_less_one)
himmelma@35172
   227
himmelma@35172
   228
subsection {* Divisions. *}
himmelma@35172
   229
himmelma@35172
   230
definition division_of (infixl "division'_of" 40) where
himmelma@35172
   231
  "s division_of i \<equiv>
himmelma@35172
   232
        finite s \<and>
himmelma@35172
   233
        (\<forall>k\<in>s. k \<subseteq> i \<and> k \<noteq> {} \<and> (\<exists>a b. k = {a..b})) \<and>
himmelma@35172
   234
        (\<forall>k1\<in>s. \<forall>k2\<in>s. k1 \<noteq> k2 \<longrightarrow> interior(k1) \<inter> interior(k2) = {}) \<and>
himmelma@35172
   235
        (\<Union>s = i)"
himmelma@35172
   236
himmelma@35172
   237
lemma division_ofD[dest]: assumes  "s division_of i"
himmelma@35172
   238
  shows"finite s" "\<And>k. k\<in>s \<Longrightarrow> k \<subseteq> i" "\<And>k. k\<in>s \<Longrightarrow>  k \<noteq> {}" "\<And>k. k\<in>s \<Longrightarrow> (\<exists>a b. k = {a..b})"
himmelma@35172
   239
  "\<And>k1 k2. \<lbrakk>k1\<in>s; k2\<in>s; k1 \<noteq> k2\<rbrakk> \<Longrightarrow> interior(k1) \<inter> interior(k2) = {}" "\<Union>s = i" using assms unfolding division_of_def by auto
himmelma@35172
   240
himmelma@35172
   241
lemma division_ofI:
himmelma@35172
   242
  assumes "finite s" "\<And>k. k\<in>s \<Longrightarrow> k \<subseteq> i" "\<And>k. k\<in>s \<Longrightarrow>  k \<noteq> {}" "\<And>k. k\<in>s \<Longrightarrow> (\<exists>a b. k = {a..b})"
himmelma@35172
   243
  "\<And>k1 k2. \<lbrakk>k1\<in>s; k2\<in>s; k1 \<noteq> k2\<rbrakk> \<Longrightarrow> interior(k1) \<inter> interior(k2) = {}" "\<Union>s = i"
himmelma@35172
   244
  shows "s division_of i" using assms unfolding division_of_def by auto
himmelma@35172
   245
himmelma@35172
   246
lemma division_of_finite: "s division_of i \<Longrightarrow> finite s"
himmelma@35172
   247
  unfolding division_of_def by auto
himmelma@35172
   248
himmelma@35172
   249
lemma division_of_self[intro]: "{a..b} \<noteq> {} \<Longrightarrow> {{a..b}} division_of {a..b}"
himmelma@35172
   250
  unfolding division_of_def by auto
himmelma@35172
   251
himmelma@35172
   252
lemma division_of_trivial[simp]: "s division_of {} \<longleftrightarrow> s = {}" unfolding division_of_def by auto 
himmelma@35172
   253
himmelma@35172
   254
lemma division_of_sing[simp]: "s division_of {a..a::real^'n} \<longleftrightarrow> s = {{a..a}}" (is "?l = ?r") proof
himmelma@35172
   255
  assume ?r moreover { assume "s = {{a}}" moreover fix k assume "k\<in>s" 
himmelma@35172
   256
    ultimately have"\<exists>x y. k = {x..y}" apply(rule_tac x=a in exI)+ unfolding interval_sing[THEN conjunct1] by auto }
himmelma@35172
   257
  ultimately show ?l unfolding division_of_def interval_sing[THEN conjunct1] by auto next
himmelma@35172
   258
  assume ?l note as=conjunctD4[OF this[unfolded division_of_def interval_sing[THEN conjunct1]]]
himmelma@35172
   259
  { fix x assume x:"x\<in>s" have "x={a}" using as(2)[rule_format,OF x] by auto }
himmelma@35172
   260
  moreover have "s \<noteq> {}" using as(4) by auto ultimately show ?r unfolding interval_sing[THEN conjunct1] by auto qed
himmelma@35172
   261
himmelma@35172
   262
lemma elementary_empty: obtains p where "p division_of {}"
himmelma@35172
   263
  unfolding division_of_trivial by auto
himmelma@35172
   264
himmelma@35172
   265
lemma elementary_interval: obtains p where  "p division_of {a..b}"
himmelma@35172
   266
  by(metis division_of_trivial division_of_self)
himmelma@35172
   267
himmelma@35172
   268
lemma division_contains: "s division_of i \<Longrightarrow> \<forall>x\<in>i. \<exists>k\<in>s. x \<in> k"
himmelma@35172
   269
  unfolding division_of_def by auto
himmelma@35172
   270
himmelma@35172
   271
lemma forall_in_division:
himmelma@35172
   272
 "d division_of i \<Longrightarrow> ((\<forall>x\<in>d. P x) \<longleftrightarrow> (\<forall>a b. {a..b} \<in> d \<longrightarrow> P {a..b}))"
himmelma@35172
   273
  unfolding division_of_def by fastsimp
himmelma@35172
   274
himmelma@35172
   275
lemma division_of_subset: assumes "p division_of (\<Union>p)" "q \<subseteq> p" shows "q division_of (\<Union>q)"
himmelma@35172
   276
  apply(rule division_ofI) proof- note as=division_ofD[OF assms(1)]
himmelma@35172
   277
  show "finite q" apply(rule finite_subset) using as(1) assms(2) by auto
himmelma@35172
   278
  { fix k assume "k \<in> q" hence kp:"k\<in>p" using assms(2) by auto show "k\<subseteq>\<Union>q" using `k \<in> q` by auto
himmelma@35172
   279
  show "\<exists>a b. k = {a..b}" using as(4)[OF kp] by auto show "k \<noteq> {}" using as(3)[OF kp] by auto }
himmelma@35172
   280
  fix k1 k2 assume "k1 \<in> q" "k2 \<in> q" "k1 \<noteq> k2" hence *:"k1\<in>p" "k2\<in>p" "k1\<noteq>k2" using assms(2) by auto
himmelma@35172
   281
  show "interior k1 \<inter> interior k2 = {}" using as(5)[OF *] by auto qed auto
himmelma@35172
   282
himmelma@35172
   283
lemma division_of_union_self[intro]: "p division_of s \<Longrightarrow> p division_of (\<Union>p)" unfolding division_of_def by auto
himmelma@35172
   284
himmelma@35172
   285
lemma division_of_content_0: assumes "content {a..b} = 0" "d division_of {a..b}" shows "\<forall>k\<in>d. content k = 0"
himmelma@35172
   286
  unfolding forall_in_division[OF assms(2)] apply(rule,rule,rule) apply(drule division_ofD(2)[OF assms(2)])
himmelma@35172
   287
  apply(drule content_subset) unfolding assms(1) proof- case goal1 thus ?case using content_pos_le[of a b] by auto qed
himmelma@35172
   288
himmelma@35172
   289
lemma division_inter: assumes "p1 division_of s1" "p2 division_of (s2::(real^'a) set)"
himmelma@35172
   290
  shows "{k1 \<inter> k2 | k1 k2 .k1 \<in> p1 \<and> k2 \<in> p2 \<and> k1 \<inter> k2 \<noteq> {}} division_of (s1 \<inter> s2)" (is "?A' division_of _") proof-
himmelma@35172
   291
let ?A = "{s. s \<in>  (\<lambda>(k1,k2). k1 \<inter> k2) ` (p1 \<times> p2) \<and> s \<noteq> {}}" have *:"?A' = ?A" by auto
himmelma@35172
   292
show ?thesis unfolding * proof(rule division_ofI) have "?A \<subseteq> (\<lambda>(x, y). x \<inter> y) ` (p1 \<times> p2)" by auto
himmelma@35172
   293
  moreover have "finite (p1 \<times> p2)" using assms unfolding division_of_def by auto ultimately show "finite ?A" by auto
himmelma@35172
   294
  have *:"\<And>s. \<Union>{x\<in>s. x \<noteq> {}} = \<Union>s" by auto show "\<Union>?A = s1 \<inter> s2" apply(rule set_ext) unfolding * and Union_image_eq UN_iff
himmelma@35172
   295
    using division_ofD(6)[OF assms(1)] and division_ofD(6)[OF assms(2)] by auto
himmelma@35172
   296
  { fix k assume "k\<in>?A" then obtain k1 k2 where k:"k = k1 \<inter> k2" "k1\<in>p1" "k2\<in>p2" "k\<noteq>{}" by auto thus "k \<noteq> {}" by auto
himmelma@35172
   297
  show "k \<subseteq> s1 \<inter> s2" using division_ofD(2)[OF assms(1) k(2)] and division_ofD(2)[OF assms(2) k(3)] unfolding k by auto
himmelma@35172
   298
  guess a1 using division_ofD(4)[OF assms(1) k(2)] .. then guess b1 .. note ab1=this
himmelma@35172
   299
  guess a2 using division_ofD(4)[OF assms(2) k(3)] .. then guess b2 .. note ab2=this
himmelma@35172
   300
  show "\<exists>a b. k = {a..b}" unfolding k ab1 ab2 unfolding inter_interval by auto } fix k1 k2
himmelma@35172
   301
  assume "k1\<in>?A" then obtain x1 y1 where k1:"k1 = x1 \<inter> y1" "x1\<in>p1" "y1\<in>p2" "k1\<noteq>{}" by auto
himmelma@35172
   302
  assume "k2\<in>?A" then obtain x2 y2 where k2:"k2 = x2 \<inter> y2" "x2\<in>p1" "y2\<in>p2" "k2\<noteq>{}" by auto
himmelma@35172
   303
  assume "k1 \<noteq> k2" hence th:"x1\<noteq>x2 \<or> y1\<noteq>y2" unfolding k1 k2 by auto
himmelma@35172
   304
  have *:"(interior x1 \<inter> interior x2 = {} \<or> interior y1 \<inter> interior y2 = {}) \<Longrightarrow>
himmelma@35172
   305
      interior(x1 \<inter> y1) \<subseteq> interior(x1) \<Longrightarrow> interior(x1 \<inter> y1) \<subseteq> interior(y1) \<Longrightarrow>
himmelma@35172
   306
      interior(x2 \<inter> y2) \<subseteq> interior(x2) \<Longrightarrow> interior(x2 \<inter> y2) \<subseteq> interior(y2)
himmelma@35172
   307
      \<Longrightarrow> interior(x1 \<inter> y1) \<inter> interior(x2 \<inter> y2) = {}" by auto
himmelma@35172
   308
  show "interior k1 \<inter> interior k2 = {}" unfolding k1 k2 apply(rule *) defer apply(rule_tac[1-4] subset_interior)
himmelma@35172
   309
    using division_ofD(5)[OF assms(1) k1(2) k2(2)]
himmelma@35172
   310
    using division_ofD(5)[OF assms(2) k1(3) k2(3)] using th by auto qed qed
himmelma@35172
   311
himmelma@35172
   312
lemma division_inter_1: assumes "d division_of i" "{a..b::real^'n} \<subseteq> i"
himmelma@35172
   313
  shows "{ {a..b} \<inter> k |k. k \<in> d \<and> {a..b} \<inter> k \<noteq> {} } division_of {a..b}" proof(cases "{a..b} = {}")
himmelma@35172
   314
  case True show ?thesis unfolding True and division_of_trivial by auto next
himmelma@35172
   315
  have *:"{a..b} \<inter> i = {a..b}" using assms(2) by auto 
himmelma@35172
   316
  case False show ?thesis using division_inter[OF division_of_self[OF False] assms(1)] unfolding * by auto qed
himmelma@35172
   317
himmelma@35172
   318
lemma elementary_inter: assumes "p1 division_of s" "p2 division_of (t::(real^'n) set)"
himmelma@35172
   319
  shows "\<exists>p. p division_of (s \<inter> t)"
himmelma@35172
   320
  by(rule,rule division_inter[OF assms])
himmelma@35172
   321
himmelma@35172
   322
lemma elementary_inters: assumes "finite f" "f\<noteq>{}" "\<forall>s\<in>f. \<exists>p. p division_of (s::(real^'n) set)"
himmelma@35172
   323
  shows "\<exists>p. p division_of (\<Inter> f)" using assms apply-proof(induct f rule:finite_induct)
himmelma@35172
   324
case (insert x f) show ?case proof(cases "f={}")
himmelma@35172
   325
  case True thus ?thesis unfolding True using insert by auto next
himmelma@35172
   326
  case False guess p using insert(3)[OF False insert(5)[unfolded ball_simps,THEN conjunct2]] ..
himmelma@35172
   327
  moreover guess px using insert(5)[rule_format,OF insertI1] .. ultimately
himmelma@35172
   328
  show ?thesis unfolding Inter_insert apply(rule_tac elementary_inter) by assumption+ qed qed auto
himmelma@35172
   329
himmelma@35172
   330
lemma division_disjoint_union:
himmelma@35172
   331
  assumes "p1 division_of s1" "p2 division_of s2" "interior s1 \<inter> interior s2 = {}"
himmelma@35172
   332
  shows "(p1 \<union> p2) division_of (s1 \<union> s2)" proof(rule division_ofI) 
himmelma@35172
   333
  note d1 = division_ofD[OF assms(1)] and d2 = division_ofD[OF assms(2)]
himmelma@35172
   334
  show "finite (p1 \<union> p2)" using d1(1) d2(1) by auto
himmelma@35172
   335
  show "\<Union>(p1 \<union> p2) = s1 \<union> s2" using d1(6) d2(6) by auto
himmelma@35172
   336
  { fix k1 k2 assume as:"k1 \<in> p1 \<union> p2" "k2 \<in> p1 \<union> p2" "k1 \<noteq> k2" moreover let ?g="interior k1 \<inter> interior k2 = {}"
himmelma@35172
   337
  { assume as:"k1\<in>p1" "k2\<in>p2" have ?g using subset_interior[OF d1(2)[OF as(1)]] subset_interior[OF d2(2)[OF as(2)]]
himmelma@35172
   338
      using assms(3) by blast } moreover
himmelma@35172
   339
  { assume as:"k1\<in>p2" "k2\<in>p1" have ?g using subset_interior[OF d1(2)[OF as(2)]] subset_interior[OF d2(2)[OF as(1)]]
himmelma@35172
   340
      using assms(3) by blast} ultimately
himmelma@35172
   341
  show ?g using d1(5)[OF _ _ as(3)] and d2(5)[OF _ _ as(3)] by auto }
himmelma@35172
   342
  fix k assume k:"k \<in> p1 \<union> p2"  show "k \<subseteq> s1 \<union> s2" using k d1(2) d2(2) by auto
himmelma@35172
   343
  show "k \<noteq> {}" using k d1(3) d2(3) by auto show "\<exists>a b. k = {a..b}" using k d1(4) d2(4) by auto qed
himmelma@35172
   344
himmelma@35172
   345
lemma partial_division_extend_1:
himmelma@35172
   346
  assumes "{c..d} \<subseteq> {a..b::real^'n}" "{c..d} \<noteq> {}"
himmelma@35172
   347
  obtains p where "p division_of {a..b}" "{c..d} \<in> p"
himmelma@35172
   348
proof- def n \<equiv> "CARD('n)" have n:"1 \<le> n" "0 < n" "n \<noteq> 0" unfolding n_def by auto
himmelma@35172
   349
  guess \<pi> using ex_bij_betw_nat_finite_1[OF finite_UNIV[where 'a='n]] .. note \<pi>=this
himmelma@35172
   350
  def \<pi>' \<equiv> "inv_into {1..n} \<pi>"
himmelma@35172
   351
  have \<pi>':"bij_betw \<pi>' UNIV {1..n}" using bij_betw_inv_into[OF \<pi>] unfolding \<pi>'_def n_def by auto
himmelma@35172
   352
  hence \<pi>'i:"\<And>i. \<pi>' i \<in> {1..n}" unfolding bij_betw_def by auto 
himmelma@35172
   353
  have \<pi>\<pi>'[simp]:"\<And>i. \<pi> (\<pi>' i) = i" unfolding \<pi>'_def apply(rule f_inv_into_f) unfolding n_def using \<pi> unfolding bij_betw_def by auto
himmelma@35172
   354
  have \<pi>'\<pi>[simp]:"\<And>i. i\<in>{1..n} \<Longrightarrow> \<pi>' (\<pi> i) = i" unfolding \<pi>'_def apply(rule inv_into_f_eq) using \<pi> unfolding n_def bij_betw_def by auto
himmelma@35172
   355
  have "{c..d} \<noteq> {}" using assms by auto
himmelma@35172
   356
  let ?p1 = "\<lambda>l. {(\<chi> i. if \<pi>' i < l then c$i else a$i) .. (\<chi> i. if \<pi>' i < l then d$i else if \<pi>' i = l then c$\<pi> l else b$i)}"
himmelma@35172
   357
  let ?p2 = "\<lambda>l. {(\<chi> i. if \<pi>' i < l then c$i else if \<pi>' i = l then d$\<pi> l else a$i) .. (\<chi> i. if \<pi>' i < l then d$i else b$i)}"
himmelma@35172
   358
  let ?p =  "{?p1 l |l. l \<in> {1..n+1}} \<union> {?p2 l |l. l \<in> {1..n+1}}"
himmelma@35172
   359
  have abcd:"\<And>i. a $ i \<le> c $ i \<and> c$i \<le> d$i \<and> d $ i \<le> b $ i" using assms unfolding subset_interval interval_eq_empty by(auto simp add:not_le not_less)
himmelma@35172
   360
  show ?thesis apply(rule that[of ?p]) apply(rule division_ofI)
himmelma@35172
   361
  proof- have "\<And>i. \<pi>' i < Suc n"
himmelma@35172
   362
    proof(rule ccontr,unfold not_less) fix i assume "Suc n \<le> \<pi>' i"
himmelma@35172
   363
      hence "\<pi>' i \<notin> {1..n}" by auto thus False using \<pi>' unfolding bij_betw_def by auto
himmelma@35172
   364
    qed hence "c = (\<chi> i. if \<pi>' i < Suc n then c $ i else a $ i)"
himmelma@35172
   365
        "d = (\<chi> i. if \<pi>' i < Suc n then d $ i else if \<pi>' i = n + 1 then c $ \<pi> (n + 1) else b $ i)"
himmelma@35172
   366
      unfolding Cart_eq Cart_lambda_beta using \<pi>' unfolding bij_betw_def by auto
himmelma@35172
   367
    thus cdp:"{c..d} \<in> ?p" apply-apply(rule UnI1) unfolding mem_Collect_eq apply(rule_tac x="n + 1" in exI) by auto
himmelma@35172
   368
    have "\<And>l. l\<in>{1..n+1} \<Longrightarrow> ?p1 l \<subseteq> {a..b}"  "\<And>l. l\<in>{1..n+1} \<Longrightarrow> ?p2 l \<subseteq> {a..b}"
himmelma@35172
   369
      unfolding subset_eq apply(rule_tac[!] ballI,rule_tac[!] ccontr)
himmelma@35172
   370
    proof- fix l assume l:"l\<in>{1..n+1}" fix x assume "x\<notin>{a..b}"
himmelma@35172
   371
      then guess i unfolding mem_interval not_all .. note i=this
himmelma@35172
   372
      show "x \<in> ?p1 l \<Longrightarrow> False" "x \<in> ?p2 l \<Longrightarrow> False" unfolding mem_interval apply(erule_tac[!] x=i in allE)
himmelma@35172
   373
        apply(case_tac[!] "\<pi>' i < l", case_tac[!] "\<pi>' i = l") using abcd[of i] i by auto 
himmelma@35172
   374
    qed moreover have "\<And>x. x \<in> {a..b} \<Longrightarrow> x \<in> \<Union>?p"
himmelma@35172
   375
    proof- fix x assume x:"x\<in>{a..b}"
himmelma@35172
   376
      { presume "x\<notin>{c..d} \<Longrightarrow> x \<in> \<Union>?p" thus "x \<in> \<Union>?p" using cdp by blast }
himmelma@35172
   377
      let ?M = "{i. i\<in>{1..n+1} \<and> \<not> (c $ \<pi> i \<le> x $ \<pi> i \<and> x $ \<pi> i \<le> d $ \<pi> i)}"
himmelma@35172
   378
      assume "x\<notin>{c..d}" then guess i0 unfolding mem_interval not_all ..
himmelma@35172
   379
      hence "\<pi>' i0 \<in> ?M" using \<pi>' unfolding bij_betw_def by(auto intro!:le_SucI)
himmelma@35172
   380
      hence M:"finite ?M" "?M \<noteq> {}" by auto
himmelma@35172
   381
      def l \<equiv> "Min ?M" note l = Min_less_iff[OF M,unfolded l_def[symmetric]] Min_in[OF M,unfolded mem_Collect_eq l_def[symmetric]]
himmelma@35172
   382
        Min_gr_iff[OF M,unfolded l_def[symmetric]]
himmelma@35172
   383
      have "x\<in>?p1 l \<or> x\<in>?p2 l" using l(2)[THEN conjunct2] unfolding de_Morgan_conj not_le
himmelma@35172
   384
        apply- apply(erule disjE) apply(rule disjI1) defer apply(rule disjI2)
himmelma@35172
   385
      proof- assume as:"x $ \<pi> l < c $ \<pi> l"
himmelma@35172
   386
        show "x \<in> ?p1 l" unfolding mem_interval Cart_lambda_beta
himmelma@35172
   387
        proof case goal1 have "\<pi>' i \<in> {1..n}" using \<pi>' unfolding bij_betw_def not_le by auto
himmelma@35172
   388
          thus ?case using as x[unfolded mem_interval,rule_format,of i]
himmelma@35172
   389
            apply auto using l(3)[of "\<pi>' i"] by(auto elim!:ballE[where x="\<pi>' i"])
himmelma@35172
   390
        qed
himmelma@35172
   391
      next assume as:"x $ \<pi> l > d $ \<pi> l"
himmelma@35172
   392
        show "x \<in> ?p2 l" unfolding mem_interval Cart_lambda_beta
himmelma@35172
   393
        proof case goal1 have "\<pi>' i \<in> {1..n}" using \<pi>' unfolding bij_betw_def not_le by auto
himmelma@35172
   394
          thus ?case using as x[unfolded mem_interval,rule_format,of i]
himmelma@35172
   395
            apply auto using l(3)[of "\<pi>' i"] by(auto elim!:ballE[where x="\<pi>' i"])
himmelma@35172
   396
        qed qed
himmelma@35172
   397
      thus "x \<in> \<Union>?p" using l(2) by blast 
himmelma@35172
   398
    qed ultimately show "\<Union>?p = {a..b}" apply-apply(rule) defer apply(rule) by(assumption,blast)
himmelma@35172
   399
    
himmelma@35172
   400
    show "finite ?p" by auto
himmelma@35172
   401
    fix k assume k:"k\<in>?p" then obtain l where l:"k = ?p1 l \<or> k = ?p2 l" "l \<in> {1..n + 1}" by auto
himmelma@35172
   402
    show "k\<subseteq>{a..b}" apply(rule,unfold mem_interval,rule,rule) 
himmelma@35172
   403
    proof- fix i::'n and x assume "x \<in> k" moreover have "\<pi>' i < l \<or> \<pi>' i = l \<or> \<pi>' i > l" by auto
himmelma@35172
   404
      ultimately show "a$i \<le> x$i" "x$i \<le> b$i" using abcd[of i] using l by(auto elim:disjE elim!:allE[where x=i] simp add:vector_le_def)
himmelma@35172
   405
    qed have "\<And>l. ?p1 l \<noteq> {}" "\<And>l. ?p2 l \<noteq> {}" unfolding interval_eq_empty not_ex apply(rule_tac[!] allI)
himmelma@35172
   406
    proof- case goal1 thus ?case using abcd[of x] by auto
himmelma@35172
   407
    next   case goal2 thus ?case using abcd[of x] by auto
himmelma@35172
   408
    qed thus "k \<noteq> {}" using k by auto
himmelma@35172
   409
    show "\<exists>a b. k = {a..b}" using k by auto
himmelma@35172
   410
    fix k' assume k':"k' \<in> ?p" "k \<noteq> k'" then obtain l' where l':"k' = ?p1 l' \<or> k' = ?p2 l'" "l' \<in> {1..n + 1}" by auto
himmelma@35172
   411
    { fix k k' l l'
himmelma@35172
   412
      assume k:"k\<in>?p" and l:"k = ?p1 l \<or> k = ?p2 l" "l \<in> {1..n + 1}" 
himmelma@35172
   413
      assume k':"k' \<in> ?p" "k \<noteq> k'" and  l':"k' = ?p1 l' \<or> k' = ?p2 l'" "l' \<in> {1..n + 1}" 
himmelma@35172
   414
      assume "l \<le> l'" fix x
himmelma@35172
   415
      have "x \<notin> interior k \<inter> interior k'" 
himmelma@35172
   416
      proof(rule,cases "l' = n+1") assume x:"x \<in> interior k \<inter> interior k'"
himmelma@35172
   417
        case True hence "\<And>i. \<pi>' i < l'" using \<pi>'i by(auto simp add:less_Suc_eq_le)
himmelma@35172
   418
        hence k':"k' = {c..d}" using l'(1) \<pi>'i by(auto simp add:Cart_nth_inverse)
himmelma@35172
   419
        have ln:"l < n + 1" 
himmelma@35172
   420
        proof(rule ccontr) case goal1 hence l2:"l = n+1" using l by auto
himmelma@35172
   421
          hence "\<And>i. \<pi>' i < l" using \<pi>'i by(auto simp add:less_Suc_eq_le)
himmelma@35172
   422
          hence "k = {c..d}" using l(1) \<pi>'i by(auto simp add:Cart_nth_inverse)
himmelma@35172
   423
          thus False using `k\<noteq>k'` k' by auto
himmelma@35172
   424
        qed have **:"\<pi>' (\<pi> l) = l" using \<pi>'\<pi>[of l] using l ln by auto
himmelma@35172
   425
        have "x $ \<pi> l < c $ \<pi> l \<or> d $ \<pi> l < x $ \<pi> l" using l(1) apply-
himmelma@35172
   426
        proof(erule disjE)
himmelma@35172
   427
          assume as:"k = ?p1 l" note * = conjunct1[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
himmelma@35172
   428
          show ?thesis using *[of "\<pi> l"] using ln unfolding Cart_lambda_beta ** by auto
himmelma@35172
   429
        next assume as:"k = ?p2 l" note * = conjunct1[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
himmelma@35172
   430
          show ?thesis using *[of "\<pi> l"] using ln unfolding Cart_lambda_beta ** by auto
himmelma@35172
   431
        qed thus False using x unfolding k' unfolding Int_iff interior_closed_interval mem_interval
himmelma@35172
   432
          by(auto elim!:allE[where x="\<pi> l"])
himmelma@35172
   433
      next case False hence "l < n + 1" using l'(2) using `l\<le>l'` by auto
himmelma@35172
   434
        hence ln:"l \<in> {1..n}" "l' \<in> {1..n}" using l l' False by auto
himmelma@35172
   435
        note \<pi>l = \<pi>'\<pi>[OF ln(1)] \<pi>'\<pi>[OF ln(2)]
himmelma@35172
   436
        assume x:"x \<in> interior k \<inter> interior k'"
himmelma@35172
   437
        show False using l(1) l'(1) apply-
himmelma@35172
   438
        proof(erule_tac[!] disjE)+
himmelma@35172
   439
          assume as:"k = ?p1 l" "k' = ?p1 l'"
himmelma@35172
   440
          note * = x[unfolded as Int_iff interior_closed_interval mem_interval]
himmelma@35172
   441
          have "l \<noteq> l'" using k'(2)[unfolded as] by auto
himmelma@35172
   442
          thus False using * by(smt Cart_lambda_beta \<pi>l)
himmelma@35172
   443
        next assume as:"k = ?p2 l" "k' = ?p2 l'"
himmelma@35172
   444
          note * = conjunctD2[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
himmelma@35172
   445
          have "l \<noteq> l'" apply(rule) using k'(2)[unfolded as] by auto
himmelma@35172
   446
          thus False using *[of "\<pi> l"] *[of "\<pi> l'"]
himmelma@35172
   447
            unfolding Cart_lambda_beta \<pi>l using `l \<le> l'` by auto
himmelma@35172
   448
        next assume as:"k = ?p1 l" "k' = ?p2 l'"
himmelma@35172
   449
          note * = conjunctD2[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
himmelma@35172
   450
          show False using *[of "\<pi> l"] *[of "\<pi> l'"]
himmelma@35172
   451
            unfolding Cart_lambda_beta \<pi>l using `l \<le> l'` using abcd[of "\<pi> l'"] by smt 
himmelma@35172
   452
        next assume as:"k = ?p2 l" "k' = ?p1 l'"
himmelma@35172
   453
          note * = conjunctD2[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
himmelma@35172
   454
          show False using *[of "\<pi> l"] *[of "\<pi> l'"]
himmelma@35172
   455
            unfolding Cart_lambda_beta \<pi>l using `l \<le> l'` using abcd[of "\<pi> l'"] by smt
himmelma@35172
   456
        qed qed } 
himmelma@35172
   457
    from this[OF k l k' l'] this[OF k'(1) l' k _ l] have "\<And>x. x \<notin> interior k \<inter> interior k'"
himmelma@35172
   458
      apply - apply(cases "l' \<le> l") using k'(2) by auto            
himmelma@35172
   459
    thus "interior k \<inter> interior k' = {}" by auto        
himmelma@35172
   460
qed qed
himmelma@35172
   461
himmelma@35172
   462
lemma partial_division_extend_interval: assumes "p division_of (\<Union>p)" "(\<Union>p) \<subseteq> {a..b}"
himmelma@35172
   463
  obtains q where "p \<subseteq> q" "q division_of {a..b::real^'n}" proof(cases "p = {}")
himmelma@35172
   464
  case True guess q apply(rule elementary_interval[of a b]) .
himmelma@35172
   465
  thus ?thesis apply- apply(rule that[of q]) unfolding True by auto next
himmelma@35172
   466
  case False note p = division_ofD[OF assms(1)]
himmelma@35172
   467
  have *:"\<forall>k\<in>p. \<exists>q. q division_of {a..b} \<and> k\<in>q" proof case goal1
himmelma@35172
   468
    guess c using p(4)[OF goal1] .. then guess d .. note cd_ = this
himmelma@35172
   469
    have *:"{c..d} \<subseteq> {a..b}" "{c..d} \<noteq> {}" using p(2,3)[OF goal1, unfolded cd_] using assms(2) by auto
himmelma@35172
   470
    guess q apply(rule partial_division_extend_1[OF *]) . thus ?case unfolding cd_ by auto qed
himmelma@35172
   471
  guess q using bchoice[OF *] .. note q = conjunctD2[OF this[rule_format]]
himmelma@35172
   472
  have "\<And>x. x\<in>p \<Longrightarrow> \<exists>d. d division_of \<Union>(q x - {x})" apply(rule,rule_tac p="q x" in division_of_subset) proof-
himmelma@35172
   473
    fix x assume x:"x\<in>p" show "q x division_of \<Union>q x" apply-apply(rule division_ofI)
himmelma@35172
   474
      using division_ofD[OF q(1)[OF x]] by auto show "q x - {x} \<subseteq> q x" by auto qed
himmelma@35172
   475
  hence "\<exists>d. d division_of \<Inter> ((\<lambda>i. \<Union>(q i - {i})) ` p)" apply- apply(rule elementary_inters)
himmelma@35172
   476
    apply(rule finite_imageI[OF p(1)]) unfolding image_is_empty apply(rule False) by auto
himmelma@35172
   477
  then guess d .. note d = this
himmelma@35172
   478
  show ?thesis apply(rule that[of "d \<union> p"]) proof-
himmelma@35172
   479
    have *:"\<And>s f t. s \<noteq> {} \<Longrightarrow> (\<forall>i\<in>s. f i \<union> i = t) \<Longrightarrow> t = \<Inter> (f ` s) \<union> (\<Union>s)" by auto
himmelma@35172
   480
    have *:"{a..b} = \<Inter> (\<lambda>i. \<Union>(q i - {i})) ` p \<union> \<Union>p" apply(rule *[OF False]) proof fix i assume i:"i\<in>p"
himmelma@35172
   481
      show "\<Union>(q i - {i}) \<union> i = {a..b}" using division_ofD(6)[OF q(1)[OF i]] using q(2)[OF i] by auto qed
himmelma@35172
   482
    show "d \<union> p division_of {a..b}" unfolding * apply(rule division_disjoint_union[OF d assms(1)])
himmelma@35172
   483
      apply(rule inter_interior_unions_intervals) apply(rule p open_interior ballI)+ proof(assumption,rule)
himmelma@35172
   484
      fix k assume k:"k\<in>p" have *:"\<And>u t s. u \<subseteq> s \<Longrightarrow> s \<inter> t = {} \<Longrightarrow> u \<inter> t = {}" by auto
himmelma@35172
   485
      show "interior (\<Inter>(\<lambda>i. \<Union>(q i - {i})) ` p) \<inter> interior k = {}" apply(rule *[of _ "interior (\<Union>(q k - {k}))"])
himmelma@35172
   486
	defer apply(subst Int_commute) apply(rule inter_interior_unions_intervals) proof- note qk=division_ofD[OF q(1)[OF k]]
himmelma@35172
   487
	show "finite (q k - {k})" "open (interior k)"  "\<forall>t\<in>q k - {k}. \<exists>a b. t = {a..b}" using qk by auto
himmelma@35172
   488
	show "\<forall>t\<in>q k - {k}. interior k \<inter> interior t = {}" using qk(5) using q(2)[OF k] by auto
himmelma@35172
   489
	have *:"\<And>x s. x \<in> s \<Longrightarrow> \<Inter>s \<subseteq> x" by auto show "interior (\<Inter>(\<lambda>i. \<Union>(q i - {i})) ` p) \<subseteq> interior (\<Union>(q k - {k}))"
himmelma@35172
   490
	  apply(rule subset_interior *)+ using k by auto qed qed qed auto qed
himmelma@35172
   491
himmelma@35172
   492
lemma elementary_bounded[dest]: "p division_of s \<Longrightarrow> bounded (s::(real^'n) set)"
himmelma@35172
   493
  unfolding division_of_def by(metis bounded_Union bounded_interval) 
himmelma@35172
   494
himmelma@35172
   495
lemma elementary_subset_interval: "p division_of s \<Longrightarrow> \<exists>a b. s \<subseteq> {a..b::real^'n}"
himmelma@35172
   496
  by(meson elementary_bounded bounded_subset_closed_interval)
himmelma@35172
   497
himmelma@35172
   498
lemma division_union_intervals_exists: assumes "{a..b::real^'n} \<noteq> {}"
himmelma@35172
   499
  obtains p where "(insert {a..b} p) division_of ({a..b} \<union> {c..d})" proof(cases "{c..d} = {}")
himmelma@35172
   500
  case True show ?thesis apply(rule that[of "{}"]) unfolding True using assms by auto next
himmelma@35172
   501
  case False note false=this show ?thesis proof(cases "{a..b} \<inter> {c..d} = {}")
himmelma@35172
   502
  have *:"\<And>a b. {a,b} = {a} \<union> {b}" by auto
himmelma@35172
   503
  case True show ?thesis apply(rule that[of "{{c..d}}"]) unfolding * apply(rule division_disjoint_union)
himmelma@35172
   504
    using false True assms using interior_subset by auto next
himmelma@35172
   505
  case False obtain u v where uv:"{a..b} \<inter> {c..d} = {u..v}" unfolding inter_interval by auto
himmelma@35172
   506
  have *:"{u..v} \<subseteq> {c..d}" using uv by auto
himmelma@35172
   507
  guess p apply(rule partial_division_extend_1[OF * False[unfolded uv]]) . note p=this division_ofD[OF this(1)]
himmelma@35172
   508
  have *:"{a..b} \<union> {c..d} = {a..b} \<union> \<Union>(p - {{u..v}})" "\<And>x s. insert x s = {x} \<union> s" using p(8) unfolding uv[THEN sym] by auto
himmelma@35172
   509
  show thesis apply(rule that[of "p - {{u..v}}"]) unfolding *(1) apply(subst *(2)) apply(rule division_disjoint_union)
himmelma@35172
   510
    apply(rule,rule assms) apply(rule division_of_subset[of p]) apply(rule division_of_union_self[OF p(1)]) defer
himmelma@35172
   511
    unfolding interior_inter[THEN sym] proof-
himmelma@35172
   512
    have *:"\<And>cd p uv ab. p \<subseteq> cd \<Longrightarrow> ab \<inter> cd = uv \<Longrightarrow> ab \<inter> p = uv \<inter> p" by auto
himmelma@35172
   513
    have "interior ({a..b} \<inter> \<Union>(p - {{u..v}})) = interior({u..v} \<inter> \<Union>(p - {{u..v}}))" 
himmelma@35172
   514
      apply(rule arg_cong[of _ _ interior]) apply(rule *[OF _ uv]) using p(8) by auto
himmelma@35172
   515
    also have "\<dots> = {}" unfolding interior_inter apply(rule inter_interior_unions_intervals) using p(6) p(7)[OF p(2)] p(3) by auto
himmelma@35172
   516
    finally show "interior ({a..b} \<inter> \<Union>(p - {{u..v}})) = {}" by assumption qed auto qed qed
himmelma@35172
   517
himmelma@35172
   518
lemma division_of_unions: assumes "finite f"  "\<And>p. p\<in>f \<Longrightarrow> p division_of (\<Union>p)"
himmelma@35172
   519
  "\<And>k1 k2. \<lbrakk>k1 \<in> \<Union>f; k2 \<in> \<Union>f; k1 \<noteq> k2\<rbrakk> \<Longrightarrow> interior k1 \<inter> interior k2 = {}"
himmelma@35172
   520
  shows "\<Union>f division_of \<Union>\<Union>f" apply(rule division_ofI) prefer 5 apply(rule assms(3)|assumption)+
himmelma@35172
   521
  apply(rule finite_Union assms(1))+ prefer 3 apply(erule UnionE) apply(rule_tac s=X in division_ofD(3)[OF assms(2)])
himmelma@35172
   522
  using division_ofD[OF assms(2)] by auto
himmelma@35172
   523
  
himmelma@35172
   524
lemma elementary_union_interval: assumes "p division_of \<Union>p"
himmelma@35172
   525
  obtains q where "q division_of ({a..b::real^'n} \<union> \<Union>p)" proof-
himmelma@35172
   526
  note assm=division_ofD[OF assms]
himmelma@35172
   527
  have lem1:"\<And>f s. \<Union>\<Union> (f ` s) = \<Union>(\<lambda>x.\<Union>(f x)) ` s" by auto
himmelma@35172
   528
  have lem2:"\<And>f s. f \<noteq> {} \<Longrightarrow> \<Union>{s \<union> t |t. t \<in> f} = s \<union> \<Union>f" by auto
himmelma@35172
   529
{ presume "p={} \<Longrightarrow> thesis" "{a..b} = {} \<Longrightarrow> thesis" "{a..b} \<noteq> {} \<Longrightarrow> interior {a..b} = {} \<Longrightarrow> thesis"
himmelma@35172
   530
    "p\<noteq>{} \<Longrightarrow> interior {a..b}\<noteq>{} \<Longrightarrow> {a..b} \<noteq> {} \<Longrightarrow> thesis"
himmelma@35172
   531
  thus thesis by auto
himmelma@35172
   532
next assume as:"p={}" guess p apply(rule elementary_interval[of a b]) .
himmelma@35172
   533
  thus thesis apply(rule_tac that[of p]) unfolding as by auto 
himmelma@35172
   534
next assume as:"{a..b}={}" show thesis apply(rule that) unfolding as using assms by auto
himmelma@35172
   535
next assume as:"interior {a..b} = {}" "{a..b} \<noteq> {}"
himmelma@35172
   536
  show thesis apply(rule that[of "insert {a..b} p"],rule division_ofI)
himmelma@35172
   537
    unfolding finite_insert apply(rule assm(1)) unfolding Union_insert  
himmelma@35172
   538
    using assm(2-4) as apply- by(fastsimp dest: assm(5))+
himmelma@35172
   539
next assume as:"p \<noteq> {}" "interior {a..b} \<noteq> {}" "{a..b}\<noteq>{}"
himmelma@35172
   540
  have "\<forall>k\<in>p. \<exists>q. (insert {a..b} q) division_of ({a..b} \<union> k)" proof case goal1
himmelma@35172
   541
    from assm(4)[OF this] guess c .. then guess d ..
himmelma@35172
   542
    thus ?case apply-apply(rule division_union_intervals_exists[OF as(3),of c d]) by auto
himmelma@35172
   543
  qed from bchoice[OF this] guess q .. note q=division_ofD[OF this[rule_format]]
himmelma@35172
   544
  let ?D = "\<Union>{insert {a..b} (q k) | k. k \<in> p}"
himmelma@35172
   545
  show thesis apply(rule that[of "?D"]) proof(rule division_ofI)
himmelma@35172
   546
    have *:"{insert {a..b} (q k) |k. k \<in> p} = (\<lambda>k. insert {a..b} (q k)) ` p" by auto
himmelma@35172
   547
    show "finite ?D" apply(rule finite_Union) unfolding * apply(rule finite_imageI) using assm(1) q(1) by auto
himmelma@35172
   548
    show "\<Union>?D = {a..b} \<union> \<Union>p" unfolding * lem1 unfolding lem2[OF as(1), of "{a..b}",THEN sym]
himmelma@35172
   549
      using q(6) by auto
himmelma@35172
   550
    fix k assume k:"k\<in>?D" thus " k \<subseteq> {a..b} \<union> \<Union>p" using q(2) by auto
himmelma@35172
   551
    show "k \<noteq> {}" using q(3) k by auto show "\<exists>a b. k = {a..b}" using q(4) k by auto
himmelma@35172
   552
    fix k' assume k':"k'\<in>?D" "k\<noteq>k'"
himmelma@35172
   553
    obtain x  where x: "k \<in>insert {a..b} (q x)"  "x\<in>p"  using k  by auto
himmelma@35172
   554
    obtain x' where x':"k'\<in>insert {a..b} (q x')" "x'\<in>p" using k' by auto
himmelma@35172
   555
    show "interior k \<inter> interior k' = {}" proof(cases "x=x'")
himmelma@35172
   556
      case True show ?thesis apply(rule q(5)) using x x' k' unfolding True by auto
himmelma@35172
   557
    next case False 
himmelma@35172
   558
      { presume "k = {a..b} \<Longrightarrow> ?thesis" "k' = {a..b} \<Longrightarrow> ?thesis" 
himmelma@35172
   559
        "k \<noteq> {a..b} \<Longrightarrow> k' \<noteq> {a..b} \<Longrightarrow> ?thesis"
himmelma@35172
   560
        thus ?thesis by auto }
himmelma@35172
   561
      { assume as':"k  = {a..b}" show ?thesis apply(rule q(5)) using x' k'(2) unfolding as' by auto }
himmelma@35172
   562
      { assume as':"k' = {a..b}" show ?thesis apply(rule q(5)) using x  k'(2) unfolding as' by auto }
himmelma@35172
   563
      assume as':"k \<noteq> {a..b}" "k' \<noteq> {a..b}"
himmelma@35172
   564
      guess c using q(4)[OF x(2,1)] .. then guess d .. note c_d=this
himmelma@35172
   565
      have "interior k  \<inter> interior {a..b} = {}" apply(rule q(5)) using x  k'(2) using as' by auto
himmelma@35172
   566
      hence "interior k \<subseteq> interior x" apply-
himmelma@35172
   567
        apply(rule interior_subset_union_intervals[OF c_d _ as(2) q(2)[OF x(2,1)]]) by auto moreover
himmelma@35172
   568
      guess c using q(4)[OF x'(2,1)] .. then guess d .. note c_d=this
himmelma@35172
   569
      have "interior k' \<inter> interior {a..b} = {}" apply(rule q(5)) using x' k'(2) using as' by auto
himmelma@35172
   570
      hence "interior k' \<subseteq> interior x'" apply-
himmelma@35172
   571
        apply(rule interior_subset_union_intervals[OF c_d _ as(2) q(2)[OF x'(2,1)]]) by auto
himmelma@35172
   572
      ultimately show ?thesis using assm(5)[OF x(2) x'(2) False] by auto
himmelma@35172
   573
    qed qed } qed
himmelma@35172
   574
himmelma@35172
   575
lemma elementary_unions_intervals:
himmelma@35172
   576
  assumes "finite f" "\<And>s. s \<in> f \<Longrightarrow> \<exists>a b. s = {a..b::real^'n}"
himmelma@35172
   577
  obtains p where "p division_of (\<Union>f)" proof-
himmelma@35172
   578
  have "\<exists>p. p division_of (\<Union>f)" proof(induct_tac f rule:finite_subset_induct) 
himmelma@35172
   579
    show "\<exists>p. p division_of \<Union>{}" using elementary_empty by auto
himmelma@35172
   580
    fix x F assume as:"finite F" "x \<notin> F" "\<exists>p. p division_of \<Union>F" "x\<in>f"
himmelma@35172
   581
    from this(3) guess p .. note p=this
himmelma@35172
   582
    from assms(2)[OF as(4)] guess a .. then guess b .. note ab=this
himmelma@35172
   583
    have *:"\<Union>F = \<Union>p" using division_ofD[OF p] by auto
himmelma@35172
   584
    show "\<exists>p. p division_of \<Union>insert x F" using elementary_union_interval[OF p[unfolded *], of a b]
himmelma@35172
   585
      unfolding Union_insert ab * by auto
himmelma@35172
   586
  qed(insert assms,auto) thus ?thesis apply-apply(erule exE,rule that) by auto qed
himmelma@35172
   587
himmelma@35172
   588
lemma elementary_union: assumes "ps division_of s" "pt division_of (t::(real^'n) set)"
himmelma@35172
   589
  obtains p where "p division_of (s \<union> t)"
himmelma@35172
   590
proof- have "s \<union> t = \<Union>ps \<union> \<Union>pt" using assms unfolding division_of_def by auto
himmelma@35172
   591
  hence *:"\<Union>(ps \<union> pt) = s \<union> t" by auto
himmelma@35172
   592
  show ?thesis apply-apply(rule elementary_unions_intervals[of "ps\<union>pt"])
himmelma@35172
   593
    unfolding * prefer 3 apply(rule_tac p=p in that)
himmelma@35172
   594
    using assms[unfolded division_of_def] by auto qed
himmelma@35172
   595
himmelma@35172
   596
lemma partial_division_extend: fixes t::"(real^'n) set"
himmelma@35172
   597
  assumes "p division_of s" "q division_of t" "s \<subseteq> t"
himmelma@35172
   598
  obtains r where "p \<subseteq> r" "r division_of t" proof-
himmelma@35172
   599
  note divp = division_ofD[OF assms(1)] and divq = division_ofD[OF assms(2)]
himmelma@35172
   600
  obtain a b where ab:"t\<subseteq>{a..b}" using elementary_subset_interval[OF assms(2)] by auto
himmelma@35172
   601
  guess r1 apply(rule partial_division_extend_interval) apply(rule assms(1)[unfolded divp(6)[THEN sym]])
himmelma@35172
   602
    apply(rule subset_trans) by(rule ab assms[unfolded divp(6)[THEN sym]])+  note r1 = this division_ofD[OF this(2)]
himmelma@35172
   603
  guess p' apply(rule elementary_unions_intervals[of "r1 - p"]) using r1(3,6) by auto 
himmelma@35172
   604
  then obtain r2 where r2:"r2 division_of (\<Union>(r1 - p)) \<inter> (\<Union>q)" 
himmelma@35172
   605
    apply- apply(drule elementary_inter[OF _ assms(2)[unfolded divq(6)[THEN sym]]]) by auto
himmelma@35172
   606
  { fix x assume x:"x\<in>t" "x\<notin>s"
himmelma@35172
   607
    hence "x\<in>\<Union>r1" unfolding r1 using ab by auto
himmelma@35172
   608
    then guess r unfolding Union_iff .. note r=this moreover
himmelma@35172
   609
    have "r \<notin> p" proof assume "r\<in>p" hence "x\<in>s" using divp(2) r by auto
himmelma@35172
   610
      thus False using x by auto qed
himmelma@35172
   611
    ultimately have "x\<in>\<Union>(r1 - p)" by auto }
himmelma@35172
   612
  hence *:"t = \<Union>p \<union> (\<Union>(r1 - p) \<inter> \<Union>q)" unfolding divp divq using assms(3) by auto
himmelma@35172
   613
  show ?thesis apply(rule that[of "p \<union> r2"]) unfolding * defer apply(rule division_disjoint_union)
himmelma@35172
   614
    unfolding divp(6) apply(rule assms r2)+
himmelma@35172
   615
  proof- have "interior s \<inter> interior (\<Union>(r1-p)) = {}"
himmelma@35172
   616
    proof(rule inter_interior_unions_intervals)
himmelma@35172
   617
      show "finite (r1 - p)" "open (interior s)" "\<forall>t\<in>r1-p. \<exists>a b. t = {a..b}" using r1 by auto
himmelma@35172
   618
      have *:"\<And>s. (\<And>x. x \<in> s \<Longrightarrow> False) \<Longrightarrow> s = {}" by auto
himmelma@35172
   619
      show "\<forall>t\<in>r1-p. interior s \<inter> interior t = {}" proof(rule)
himmelma@35172
   620
        fix m x assume as:"m\<in>r1-p"
himmelma@35172
   621
        have "interior m \<inter> interior (\<Union>p) = {}" proof(rule inter_interior_unions_intervals)
himmelma@35172
   622
          show "finite p" "open (interior m)" "\<forall>t\<in>p. \<exists>a b. t = {a..b}" using divp by auto
himmelma@35172
   623
          show "\<forall>t\<in>p. interior m \<inter> interior t = {}" apply(rule, rule r1(7)) using as using r1 by auto
himmelma@35172
   624
        qed thus "interior s \<inter> interior m = {}" unfolding divp by auto
himmelma@35172
   625
      qed qed        
himmelma@35172
   626
    thus "interior s \<inter> interior (\<Union>(r1-p) \<inter> (\<Union>q)) = {}" using interior_subset by auto
himmelma@35172
   627
  qed auto qed
himmelma@35172
   628
himmelma@35172
   629
subsection {* Tagged (partial) divisions. *}
himmelma@35172
   630
himmelma@35172
   631
definition tagged_partial_division_of (infixr "tagged'_partial'_division'_of" 40) where
himmelma@35172
   632
  "(s tagged_partial_division_of i) \<equiv>
himmelma@35172
   633
        finite s \<and>
himmelma@35172
   634
        (\<forall>x k. (x,k) \<in> s \<longrightarrow> x \<in> k \<and> k \<subseteq> i \<and> (\<exists>a b. k = {a..b})) \<and>
himmelma@35172
   635
        (\<forall>x1 k1 x2 k2. (x1,k1) \<in> s \<and> (x2,k2) \<in> s \<and> ((x1,k1) \<noteq> (x2,k2))
himmelma@35172
   636
                       \<longrightarrow> (interior(k1) \<inter> interior(k2) = {}))"
himmelma@35172
   637
himmelma@35172
   638
lemma tagged_partial_division_ofD[dest]: assumes "s tagged_partial_division_of i"
himmelma@35172
   639
  shows "finite s" "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k" "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"
himmelma@35172
   640
  "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = {a..b}"
himmelma@35172
   641
  "\<And>x1 k1 x2 k2. (x1,k1) \<in> s \<Longrightarrow> (x2,k2) \<in> s \<Longrightarrow> (x1,k1) \<noteq> (x2,k2) \<Longrightarrow> interior(k1) \<inter> interior(k2) = {}"
himmelma@35172
   642
  using assms unfolding tagged_partial_division_of_def  apply- by blast+ 
himmelma@35172
   643
himmelma@35172
   644
definition tagged_division_of (infixr "tagged'_division'_of" 40) where
himmelma@35172
   645
  "(s tagged_division_of i) \<equiv>
himmelma@35172
   646
        (s tagged_partial_division_of i) \<and> (\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
himmelma@35172
   647
himmelma@35172
   648
lemma tagged_division_of_finite[dest]: "s tagged_division_of i \<Longrightarrow> finite s"
himmelma@35172
   649
  unfolding tagged_division_of_def tagged_partial_division_of_def by auto
himmelma@35172
   650
himmelma@35172
   651
lemma tagged_division_of:
himmelma@35172
   652
 "(s tagged_division_of i) \<longleftrightarrow>
himmelma@35172
   653
        finite s \<and>
himmelma@35172
   654
        (\<forall>x k. (x,k) \<in> s
himmelma@35172
   655
               \<longrightarrow> x \<in> k \<and> k \<subseteq> i \<and> (\<exists>a b. k = {a..b})) \<and>
himmelma@35172
   656
        (\<forall>x1 k1 x2 k2. (x1,k1) \<in> s \<and> (x2,k2) \<in> s \<and> ~((x1,k1) = (x2,k2))
himmelma@35172
   657
                       \<longrightarrow> (interior(k1) \<inter> interior(k2) = {})) \<and>
himmelma@35172
   658
        (\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
himmelma@35172
   659
  unfolding tagged_division_of_def tagged_partial_division_of_def by auto
himmelma@35172
   660
himmelma@35172
   661
lemma tagged_division_ofI: assumes
himmelma@35172
   662
  "finite s" "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k" "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"  "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = {a..b}"
himmelma@35172
   663
  "\<And>x1 k1 x2 k2. (x1,k1) \<in> s \<Longrightarrow> (x2,k2) \<in> s \<Longrightarrow> ~((x1,k1) = (x2,k2)) \<Longrightarrow> (interior(k1) \<inter> interior(k2) = {})"
himmelma@35172
   664
  "(\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
himmelma@35172
   665
  shows "s tagged_division_of i"
himmelma@35172
   666
  unfolding tagged_division_of apply(rule) defer apply rule
himmelma@35172
   667
  apply(rule allI impI conjI assms)+ apply assumption
himmelma@35172
   668
  apply(rule, rule assms, assumption) apply(rule assms, assumption)
himmelma@35172
   669
  using assms(1,5-) apply- by blast+
himmelma@35172
   670
himmelma@35172
   671
lemma tagged_division_ofD[dest]: assumes "s tagged_division_of i"
himmelma@35172
   672
  shows "finite s" "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k" "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"  "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = {a..b}"
himmelma@35172
   673
  "\<And>x1 k1 x2 k2. (x1,k1) \<in> s \<Longrightarrow> (x2,k2) \<in> s \<Longrightarrow> ~((x1,k1) = (x2,k2)) \<Longrightarrow> (interior(k1) \<inter> interior(k2) = {})"
himmelma@35172
   674
  "(\<Union>{k. \<exists>x. (x,k) \<in> s} = i)" using assms unfolding tagged_division_of apply- by blast+
himmelma@35172
   675
himmelma@35172
   676
lemma division_of_tagged_division: assumes"s tagged_division_of i"  shows "(snd ` s) division_of i"
himmelma@35172
   677
proof(rule division_ofI) note assm=tagged_division_ofD[OF assms]
himmelma@35172
   678
  show "\<Union>snd ` s = i" "finite (snd ` s)" using assm by auto
himmelma@35172
   679
  fix k assume k:"k \<in> snd ` s" then obtain xk where xk:"(xk, k) \<in> s" by auto
himmelma@35172
   680
  thus  "k \<subseteq> i" "k \<noteq> {}" "\<exists>a b. k = {a..b}" using assm apply- by fastsimp+
himmelma@35172
   681
  fix k' assume k':"k' \<in> snd ` s" "k \<noteq> k'" from this(1) obtain xk' where xk':"(xk', k') \<in> s" by auto
himmelma@35172
   682
  thus "interior k \<inter> interior k' = {}" apply-apply(rule assm(5)) apply(rule xk xk')+ using k' by auto
himmelma@35172
   683
qed
himmelma@35172
   684
himmelma@35172
   685
lemma partial_division_of_tagged_division: assumes "s tagged_partial_division_of i"
himmelma@35172
   686
  shows "(snd ` s) division_of \<Union>(snd ` s)"
himmelma@35172
   687
proof(rule division_ofI) note assm=tagged_partial_division_ofD[OF assms]
himmelma@35172
   688
  show "finite (snd ` s)" "\<Union>snd ` s = \<Union>snd ` s" using assm by auto
himmelma@35172
   689
  fix k assume k:"k \<in> snd ` s" then obtain xk where xk:"(xk, k) \<in> s" by auto
himmelma@35172
   690
  thus "k\<noteq>{}" "\<exists>a b. k = {a..b}" "k \<subseteq> \<Union>snd ` s" using assm by auto
himmelma@35172
   691
  fix k' assume k':"k' \<in> snd ` s" "k \<noteq> k'" from this(1) obtain xk' where xk':"(xk', k') \<in> s" by auto
himmelma@35172
   692
  thus "interior k \<inter> interior k' = {}" apply-apply(rule assm(5)) apply(rule xk xk')+ using k' by auto
himmelma@35172
   693
qed
himmelma@35172
   694
himmelma@35172
   695
lemma tagged_partial_division_subset: assumes "s tagged_partial_division_of i" "t \<subseteq> s"
himmelma@35172
   696
  shows "t tagged_partial_division_of i"
himmelma@35172
   697
  using assms unfolding tagged_partial_division_of_def using finite_subset[OF assms(2)] by blast
himmelma@35172
   698
himmelma@35172
   699
lemma setsum_over_tagged_division_lemma: fixes d::"(real^'m) set \<Rightarrow> 'a::real_normed_vector"
himmelma@35172
   700
  assumes "p tagged_division_of i" "\<And>u v. {u..v} \<noteq> {} \<Longrightarrow> content {u..v} = 0 \<Longrightarrow> d {u..v} = 0"
himmelma@35172
   701
  shows "setsum (\<lambda>(x,k). d k) p = setsum d (snd ` p)"
himmelma@35172
   702
proof- note assm=tagged_division_ofD[OF assms(1)]
himmelma@35172
   703
  have *:"(\<lambda>(x,k). d k) = d \<circ> snd" unfolding o_def apply(rule ext) by auto
himmelma@35172
   704
  show ?thesis unfolding * apply(subst eq_commute) proof(rule setsum_reindex_nonzero)
himmelma@35172
   705
    show "finite p" using assm by auto
himmelma@35172
   706
    fix x y assume as:"x\<in>p" "y\<in>p" "x\<noteq>y" "snd x = snd y" 
himmelma@35172
   707
    obtain a b where ab:"snd x = {a..b}" using assm(4)[of "fst x" "snd x"] as(1) by auto
himmelma@35172
   708
    have "(fst x, snd y) \<in> p" "(fst x, snd y) \<noteq> y" unfolding as(4)[THEN sym] using as(1-3) by auto
himmelma@35172
   709
    hence "interior (snd x) \<inter> interior (snd y) = {}" apply-apply(rule assm(5)[of "fst x" _ "fst y"]) using as by auto 
himmelma@35172
   710
    hence "content {a..b} = 0" unfolding as(4)[THEN sym] ab content_eq_0_interior by auto
himmelma@35172
   711
    hence "d {a..b} = 0" apply-apply(rule assms(2)) using assm(2)[of "fst x" "snd x"] as(1) unfolding ab[THEN sym] by auto
himmelma@35172
   712
    thus "d (snd x) = 0" unfolding ab by auto qed qed
himmelma@35172
   713
himmelma@35172
   714
lemma tag_in_interval: "p tagged_division_of i \<Longrightarrow> (x,k) \<in> p \<Longrightarrow> x \<in> i" by auto
himmelma@35172
   715
himmelma@35172
   716
lemma tagged_division_of_empty: "{} tagged_division_of {}"
himmelma@35172
   717
  unfolding tagged_division_of by auto
himmelma@35172
   718
himmelma@35172
   719
lemma tagged_partial_division_of_trivial[simp]:
himmelma@35172
   720
 "p tagged_partial_division_of {} \<longleftrightarrow> p = {}"
himmelma@35172
   721
  unfolding tagged_partial_division_of_def by auto
himmelma@35172
   722
himmelma@35172
   723
lemma tagged_division_of_trivial[simp]:
himmelma@35172
   724
 "p tagged_division_of {} \<longleftrightarrow> p = {}"
himmelma@35172
   725
  unfolding tagged_division_of by auto
himmelma@35172
   726
himmelma@35172
   727
lemma tagged_division_of_self:
himmelma@35172
   728
 "x \<in> {a..b} \<Longrightarrow> {(x,{a..b})} tagged_division_of {a..b}"
himmelma@35172
   729
  apply(rule tagged_division_ofI) by auto
himmelma@35172
   730
himmelma@35172
   731
lemma tagged_division_union:
himmelma@35172
   732
  assumes "p1 tagged_division_of s1"  "p2 tagged_division_of s2" "interior s1 \<inter> interior s2 = {}"
himmelma@35172
   733
  shows "(p1 \<union> p2) tagged_division_of (s1 \<union> s2)"
himmelma@35172
   734
proof(rule tagged_division_ofI) note p1=tagged_division_ofD[OF assms(1)] and p2=tagged_division_ofD[OF assms(2)]
himmelma@35172
   735
  show "finite (p1 \<union> p2)" using p1(1) p2(1) by auto
himmelma@35172
   736
  show "\<Union>{k. \<exists>x. (x, k) \<in> p1 \<union> p2} = s1 \<union> s2" using p1(6) p2(6) by blast
himmelma@35172
   737
  fix x k assume xk:"(x,k)\<in>p1\<union>p2" show "x\<in>k" "\<exists>a b. k = {a..b}" using xk p1(2,4) p2(2,4) by auto
himmelma@35172
   738
  show "k\<subseteq>s1\<union>s2" using xk p1(3) p2(3) by blast
himmelma@35172
   739
  fix x' k' assume xk':"(x',k')\<in>p1\<union>p2" "(x,k) \<noteq> (x',k')"
himmelma@35172
   740
  have *:"\<And>a b. a\<subseteq> s1 \<Longrightarrow> b\<subseteq> s2 \<Longrightarrow> interior a \<inter> interior b = {}" using assms(3) subset_interior by blast
himmelma@35172
   741
  show "interior k \<inter> interior k' = {}" apply(cases "(x,k)\<in>p1", case_tac[!] "(x',k')\<in>p1")
himmelma@35172
   742
    apply(rule p1(5)) prefer 4 apply(rule *) prefer 6 apply(subst Int_commute,rule *) prefer 8 apply(rule p2(5))
himmelma@35172
   743
    using p1(3) p2(3) using xk xk' by auto qed 
himmelma@35172
   744
himmelma@35172
   745
lemma tagged_division_unions:
himmelma@35172
   746
  assumes "finite iset" "\<forall>i\<in>iset. (pfn(i) tagged_division_of i)"
himmelma@35172
   747
  "\<forall>i1 \<in> iset. \<forall>i2 \<in> iset. ~(i1 = i2) \<longrightarrow> (interior(i1) \<inter> interior(i2) = {})"
himmelma@35172
   748
  shows "\<Union>(pfn ` iset) tagged_division_of (\<Union>iset)"
himmelma@35172
   749
proof(rule tagged_division_ofI)
himmelma@35172
   750
  note assm = tagged_division_ofD[OF assms(2)[rule_format]]
himmelma@35172
   751
  show "finite (\<Union>pfn ` iset)" apply(rule finite_Union) using assms by auto
himmelma@35172
   752
  have "\<Union>{k. \<exists>x. (x, k) \<in> \<Union>pfn ` iset} = \<Union>(\<lambda>i. \<Union>{k. \<exists>x. (x, k) \<in> pfn i}) ` iset" by blast 
himmelma@35172
   753
  also have "\<dots> = \<Union>iset" using assm(6) by auto
himmelma@35172
   754
  finally show "\<Union>{k. \<exists>x. (x, k) \<in> \<Union>pfn ` iset} = \<Union>iset" . 
himmelma@35172
   755
  fix x k assume xk:"(x,k)\<in>\<Union>pfn ` iset" then obtain i where i:"i \<in> iset" "(x, k) \<in> pfn i" by auto
himmelma@35172
   756
  show "x\<in>k" "\<exists>a b. k = {a..b}" "k \<subseteq> \<Union>iset" using assm(2-4)[OF i] using i(1) by auto
himmelma@35172
   757
  fix x' k' assume xk':"(x',k')\<in>\<Union>pfn ` iset" "(x, k) \<noteq> (x', k')" then obtain i' where i':"i' \<in> iset" "(x', k') \<in> pfn i'" by auto
himmelma@35172
   758
  have *:"\<And>a b. i\<noteq>i' \<Longrightarrow> a\<subseteq> i \<Longrightarrow> b\<subseteq> i' \<Longrightarrow> interior a \<inter> interior b = {}" using i(1) i'(1)
himmelma@35172
   759
    using assms(3)[rule_format] subset_interior by blast
himmelma@35172
   760
  show "interior k \<inter> interior k' = {}" apply(cases "i=i'")
himmelma@35172
   761
    using assm(5)[OF i _ xk'(2)]  i'(2) using assm(3)[OF i] assm(3)[OF i'] defer apply-apply(rule *) by auto
himmelma@35172
   762
qed
himmelma@35172
   763
himmelma@35172
   764
lemma tagged_partial_division_of_union_self:
himmelma@35172
   765
  assumes "p tagged_partial_division_of s" shows "p tagged_division_of (\<Union>(snd ` p))"
himmelma@35172
   766
  apply(rule tagged_division_ofI) using tagged_partial_division_ofD[OF assms] by auto
himmelma@35172
   767
himmelma@35172
   768
lemma tagged_division_of_union_self: assumes "p tagged_division_of s"
himmelma@35172
   769
  shows "p tagged_division_of (\<Union>(snd ` p))"
himmelma@35172
   770
  apply(rule tagged_division_ofI) using tagged_division_ofD[OF assms] by auto
himmelma@35172
   771
himmelma@35172
   772
subsection {* Fine-ness of a partition w.r.t. a gauge. *}
himmelma@35172
   773
himmelma@35172
   774
definition fine (infixr "fine" 46) where
himmelma@35172
   775
  "d fine s \<longleftrightarrow> (\<forall>(x,k) \<in> s. k \<subseteq> d(x))"
himmelma@35172
   776
himmelma@35172
   777
lemma fineI: assumes "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> d x"
himmelma@35172
   778
  shows "d fine s" using assms unfolding fine_def by auto
himmelma@35172
   779
himmelma@35172
   780
lemma fineD[dest]: assumes "d fine s"
himmelma@35172
   781
  shows "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> d x" using assms unfolding fine_def by auto
himmelma@35172
   782
himmelma@35172
   783
lemma fine_inter: "(\<lambda>x. d1 x \<inter> d2 x) fine p \<longleftrightarrow> d1 fine p \<and> d2 fine p"
himmelma@35172
   784
  unfolding fine_def by auto
himmelma@35172
   785
himmelma@35172
   786
lemma fine_inters:
himmelma@35172
   787
 "(\<lambda>x. \<Inter> {f d x | d.  d \<in> s}) fine p \<longleftrightarrow> (\<forall>d\<in>s. (f d) fine p)"
himmelma@35172
   788
  unfolding fine_def by blast
himmelma@35172
   789
himmelma@35172
   790
lemma fine_union:
himmelma@35172
   791
  "d fine p1 \<Longrightarrow> d fine p2 \<Longrightarrow> d fine (p1 \<union> p2)"
himmelma@35172
   792
  unfolding fine_def by blast
himmelma@35172
   793
himmelma@35172
   794
lemma fine_unions:"(\<And>p. p \<in> ps \<Longrightarrow> d fine p) \<Longrightarrow> d fine (\<Union>ps)"
himmelma@35172
   795
  unfolding fine_def by auto
himmelma@35172
   796
himmelma@35172
   797
lemma fine_subset:  "p \<subseteq> q \<Longrightarrow> d fine q \<Longrightarrow> d fine p"
himmelma@35172
   798
  unfolding fine_def by blast
himmelma@35172
   799
himmelma@35172
   800
subsection {* Gauge integral. Define on compact intervals first, then use a limit. *}
himmelma@35172
   801
himmelma@35172
   802
definition has_integral_compact_interval (infixr "has'_integral'_compact'_interval" 46) where
himmelma@35172
   803
  "(f has_integral_compact_interval y) i \<equiv>
himmelma@35172
   804
        (\<forall>e>0. \<exists>d. gauge d \<and>
himmelma@35172
   805
          (\<forall>p. p tagged_division_of i \<and> d fine p
himmelma@35172
   806
                        \<longrightarrow> norm(setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - y) < e))"
himmelma@35172
   807
himmelma@35172
   808
definition has_integral (infixr "has'_integral" 46) where 
himmelma@35172
   809
"((f::(real^'n \<Rightarrow> 'b::real_normed_vector)) has_integral y) i \<equiv>
himmelma@35172
   810
        if (\<exists>a b. i = {a..b}) then (f has_integral_compact_interval y) i
himmelma@35172
   811
        else (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> {a..b}
himmelma@35172
   812
              \<longrightarrow> (\<exists>z. ((\<lambda>x. if x \<in> i then f x else 0) has_integral_compact_interval z) {a..b} \<and>
himmelma@35172
   813
                                       norm(z - y) < e))"
himmelma@35172
   814
himmelma@35172
   815
lemma has_integral:
himmelma@35172
   816
 "(f has_integral y) ({a..b}) \<longleftrightarrow>
himmelma@35172
   817
        (\<forall>e>0. \<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p
himmelma@35172
   818
                        \<longrightarrow> norm(setsum (\<lambda>(x,k). content(k) *\<^sub>R f x) p - y) < e))"
himmelma@35172
   819
  unfolding has_integral_def has_integral_compact_interval_def by auto
himmelma@35172
   820
himmelma@35172
   821
lemma has_integralD[dest]: assumes
himmelma@35172
   822
 "(f has_integral y) ({a..b})" "e>0"
himmelma@35172
   823
  obtains d where "gauge d" "\<And>p. p tagged_division_of {a..b} \<Longrightarrow> d fine p
himmelma@35172
   824
                        \<Longrightarrow> norm(setsum (\<lambda>(x,k). content(k) *\<^sub>R f(x)) p - y) < e"
himmelma@35172
   825
  using assms unfolding has_integral by auto
himmelma@35172
   826
himmelma@35172
   827
lemma has_integral_alt:
himmelma@35172
   828
 "(f has_integral y) i \<longleftrightarrow>
himmelma@35172
   829
      (if (\<exists>a b. i = {a..b}) then (f has_integral y) i
himmelma@35172
   830
       else (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> {a..b}
himmelma@35172
   831
                               \<longrightarrow> (\<exists>z. ((\<lambda>x. if x \<in> i then f(x) else 0)
himmelma@35172
   832
                                        has_integral z) ({a..b}) \<and>
himmelma@35172
   833
                                       norm(z - y) < e)))"
himmelma@35172
   834
  unfolding has_integral unfolding has_integral_compact_interval_def has_integral_def by auto
himmelma@35172
   835
himmelma@35172
   836
lemma has_integral_altD:
himmelma@35172
   837
  assumes "(f has_integral y) i" "\<not> (\<exists>a b. i = {a..b})" "e>0"
himmelma@35172
   838
  obtains B where "B>0" "\<forall>a b. ball 0 B \<subseteq> {a..b}\<longrightarrow> (\<exists>z. ((\<lambda>x. if x \<in> i then f(x) else 0) has_integral z) ({a..b}) \<and> norm(z - y) < e)"
himmelma@35172
   839
  using assms unfolding has_integral unfolding has_integral_compact_interval_def has_integral_def by auto
himmelma@35172
   840
himmelma@35172
   841
definition integrable_on (infixr "integrable'_on" 46) where
himmelma@35172
   842
  "(f integrable_on i) \<equiv> \<exists>y. (f has_integral y) i"
himmelma@35172
   843
himmelma@35172
   844
definition "integral i f \<equiv> SOME y. (f has_integral y) i"
himmelma@35172
   845
himmelma@35172
   846
lemma integrable_integral[dest]:
himmelma@35172
   847
 "f integrable_on i \<Longrightarrow> (f has_integral (integral i f)) i"
himmelma@35172
   848
  unfolding integrable_on_def integral_def by(rule someI_ex)
himmelma@35172
   849
himmelma@35172
   850
lemma has_integral_integrable[intro]: "(f has_integral i) s \<Longrightarrow> f integrable_on s"
himmelma@35172
   851
  unfolding integrable_on_def by auto
himmelma@35172
   852
himmelma@35172
   853
lemma has_integral_integral:"f integrable_on s \<longleftrightarrow> (f has_integral (integral s f)) s"
himmelma@35172
   854
  by auto
himmelma@35172
   855
himmelma@35291
   856
lemma has_integral_vec1: assumes "(f has_integral k) {a..b}"
himmelma@35291
   857
  shows "((\<lambda>x. vec1 (f x)) has_integral (vec1 k)) {a..b}"
himmelma@35291
   858
proof- have *:"\<And>p. (\<Sum>(x, k)\<in>p. content k *\<^sub>R vec1 (f x)) - vec1 k = vec1 ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - k)"
himmelma@35291
   859
    unfolding vec_sub Cart_eq by(auto simp add:vec1_dest_vec1_simps split_beta)
himmelma@35291
   860
  show ?thesis using assms unfolding has_integral apply safe
himmelma@35291
   861
    apply(erule_tac x=e in allE,safe) apply(rule_tac x=d in exI,safe)
himmelma@35291
   862
    apply(erule_tac x=p in allE,safe) unfolding * norm_vector_1 by auto qed
himmelma@35291
   863
himmelma@35172
   864
lemma setsum_content_null:
himmelma@35172
   865
  assumes "content({a..b}) = 0" "p tagged_division_of {a..b}"
himmelma@35172
   866
  shows "setsum (\<lambda>(x,k). content k *\<^sub>R f x) p = (0::'a::real_normed_vector)"
himmelma@35172
   867
proof(rule setsum_0',rule) fix y assume y:"y\<in>p"
himmelma@35172
   868
  obtain x k where xk:"y = (x,k)" using surj_pair[of y] by blast
himmelma@35172
   869
  note assm = tagged_division_ofD(3-4)[OF assms(2) y[unfolded xk]]
himmelma@35172
   870
  from this(2) guess c .. then guess d .. note c_d=this
himmelma@35172
   871
  have "(\<lambda>(x, k). content k *\<^sub>R f x) y = content k *\<^sub>R f x" unfolding xk by auto
himmelma@35172
   872
  also have "\<dots> = 0" using content_subset[OF assm(1)[unfolded c_d]] content_pos_le[of c d]
himmelma@35172
   873
    unfolding assms(1) c_d by auto
himmelma@35172
   874
  finally show "(\<lambda>(x, k). content k *\<^sub>R f x) y = 0" .
himmelma@35172
   875
qed
himmelma@35172
   876
himmelma@35172
   877
subsection {* Some basic combining lemmas. *}
himmelma@35172
   878
himmelma@35172
   879
lemma tagged_division_unions_exists:
himmelma@35172
   880
  assumes "finite iset" "\<forall>i \<in> iset. \<exists>p. p tagged_division_of i \<and> d fine p"
himmelma@35172
   881
  "\<forall>i1\<in>iset. \<forall>i2\<in>iset. ~(i1 = i2) \<longrightarrow> (interior(i1) \<inter> interior(i2) = {})" "(\<Union>iset = i)"
himmelma@35172
   882
   obtains p where "p tagged_division_of i" "d fine p"
himmelma@35172
   883
proof- guess pfn using bchoice[OF assms(2)] .. note pfn = conjunctD2[OF this[rule_format]]
himmelma@35172
   884
  show thesis apply(rule_tac p="\<Union>(pfn ` iset)" in that) unfolding assms(4)[THEN sym]
himmelma@35172
   885
    apply(rule tagged_division_unions[OF assms(1) _ assms(3)]) defer 
himmelma@35172
   886
    apply(rule fine_unions) using pfn by auto
himmelma@35172
   887
qed
himmelma@35172
   888
himmelma@35172
   889
subsection {* The set we're concerned with must be closed. *}
himmelma@35172
   890
himmelma@35172
   891
lemma division_of_closed: "s division_of i \<Longrightarrow> closed (i::(real^'n) set)"
himmelma@35172
   892
  unfolding division_of_def by(fastsimp intro!: closed_Union closed_interval)
himmelma@35172
   893
himmelma@35172
   894
subsection {* General bisection principle for intervals; might be useful elsewhere. *}
himmelma@35172
   895
himmelma@35172
   896
lemma interval_bisection_step:
himmelma@35172
   897
  assumes "P {}" "(\<forall>s t. P s \<and> P t \<and> interior(s) \<inter> interior(t) = {} \<longrightarrow> P(s \<union> t))" "~(P {a..b::real^'n})"
himmelma@35172
   898
  obtains c d where "~(P{c..d})"
himmelma@35172
   899
  "\<forall>i. a$i \<le> c$i \<and> c$i \<le> d$i \<and> d$i \<le> b$i \<and> 2 * (d$i - c$i) \<le> b$i - a$i"
himmelma@35172
   900
proof- have "{a..b} \<noteq> {}" using assms(1,3) by auto
himmelma@35172
   901
  note ab=this[unfolded interval_eq_empty not_ex not_less]
himmelma@35172
   902
  { fix f have "finite f \<Longrightarrow>
himmelma@35172
   903
        (\<forall>s\<in>f. P s) \<Longrightarrow>
himmelma@35172
   904
        (\<forall>s\<in>f. \<exists>a b. s = {a..b}) \<Longrightarrow>
himmelma@35172
   905
        (\<forall>s\<in>f.\<forall>t\<in>f. ~(s = t) \<longrightarrow> interior(s) \<inter> interior(t) = {}) \<Longrightarrow> P(\<Union>f)"
himmelma@35172
   906
    proof(induct f rule:finite_induct)
himmelma@35172
   907
      case empty show ?case using assms(1) by auto
himmelma@35172
   908
    next case (insert x f) show ?case unfolding Union_insert apply(rule assms(2)[rule_format])
himmelma@35172
   909
        apply rule defer apply rule defer apply(rule inter_interior_unions_intervals)
himmelma@35172
   910
        using insert by auto
himmelma@35172
   911
    qed } note * = this
himmelma@35172
   912
  let ?A = "{{c..d} | c d. \<forall>i. (c$i = a$i) \<and> (d$i = (a$i + b$i) / 2) \<or> (c$i = (a$i + b$i) / 2) \<and> (d$i = b$i)}"
himmelma@35172
   913
  let ?PP = "\<lambda>c d. \<forall>i. a$i \<le> c$i \<and> c$i \<le> d$i \<and> d$i \<le> b$i \<and> 2 * (d$i - c$i) \<le> b$i - a$i"
himmelma@35172
   914
  { presume "\<forall>c d. ?PP c d \<longrightarrow> P {c..d} \<Longrightarrow> False"
himmelma@35172
   915
    thus thesis unfolding atomize_not not_all apply-apply(erule exE)+ apply(rule_tac c=x and d=xa in that) by auto }
himmelma@35172
   916
  assume as:"\<forall>c d. ?PP c d \<longrightarrow> P {c..d}"
himmelma@35172
   917
  have "P (\<Union> ?A)" proof(rule *, rule_tac[2-] ballI, rule_tac[4] ballI, rule_tac[4] impI) 
himmelma@35172
   918
    let ?B = "(\<lambda>s.{(\<chi> i. if i \<in> s then a$i else (a$i + b$i) / 2) ..
himmelma@35172
   919
      (\<chi> i. if i \<in> s then (a$i + b$i) / 2 else b$i)}) ` {s. s \<subseteq> UNIV}"
himmelma@35172
   920
    have "?A \<subseteq> ?B" proof case goal1
himmelma@35172
   921
      then guess c unfolding mem_Collect_eq .. then guess d apply- by(erule exE,(erule conjE)+) note c_d=this[rule_format]
himmelma@35172
   922
      have *:"\<And>a b c d. a = c \<Longrightarrow> b = d \<Longrightarrow> {a..b} = {c..d}" by auto
himmelma@35172
   923
      show "x\<in>?B" unfolding image_iff apply(rule_tac x="{i. c$i = a$i}" in bexI)
himmelma@35172
   924
        unfolding c_d apply(rule * ) unfolding Cart_eq cond_component Cart_lambda_beta
himmelma@35172
   925
      proof(rule_tac[1-2] allI) fix i show "c $ i = (if i \<in> {i. c $ i = a $ i} then a $ i else (a $ i + b $ i) / 2)"
himmelma@35172
   926
          "d $ i = (if i \<in> {i. c $ i = a $ i} then (a $ i + b $ i) / 2 else b $ i)"
himmelma@35172
   927
          using c_d(2)[of i] ab[THEN spec[where x=i]] by(auto simp add:field_simps)
himmelma@35172
   928
      qed auto qed
himmelma@35172
   929
    thus "finite ?A" apply(rule finite_subset[of _ ?B]) by auto
himmelma@35172
   930
    fix s assume "s\<in>?A" then guess c unfolding mem_Collect_eq .. then guess d apply- by(erule exE,(erule conjE)+)
himmelma@35172
   931
    note c_d=this[rule_format]
himmelma@35172
   932
    show "P s" unfolding c_d apply(rule as[rule_format]) proof- case goal1 show ?case 
himmelma@35172
   933
        using c_d(2)[of i] using ab[THEN spec[where x=i]] by auto qed
himmelma@35172
   934
    show "\<exists>a b. s = {a..b}" unfolding c_d by auto
himmelma@35172
   935
    fix t assume "t\<in>?A" then guess e unfolding mem_Collect_eq .. then guess f apply- by(erule exE,(erule conjE)+)
himmelma@35172
   936
    note e_f=this[rule_format]
himmelma@35172
   937
    assume "s \<noteq> t" hence "\<not> (c = e \<and> d = f)" unfolding c_d e_f by auto
himmelma@35172
   938
    then obtain i where "c$i \<noteq> e$i \<or> d$i \<noteq> f$i" unfolding de_Morgan_conj Cart_eq by auto
himmelma@35172
   939
    hence i:"c$i \<noteq> e$i" "d$i \<noteq> f$i" apply- apply(erule_tac[!] disjE)
himmelma@35172
   940
    proof- assume "c$i \<noteq> e$i" thus "d$i \<noteq> f$i" using c_d(2)[of i] e_f(2)[of i] by fastsimp
himmelma@35172
   941
    next   assume "d$i \<noteq> f$i" thus "c$i \<noteq> e$i" using c_d(2)[of i] e_f(2)[of i] by fastsimp
himmelma@35172
   942
    qed have *:"\<And>s t. (\<And>a. a\<in>s \<Longrightarrow> a\<in>t \<Longrightarrow> False) \<Longrightarrow> s \<inter> t = {}" by auto
himmelma@35172
   943
    show "interior s \<inter> interior t = {}" unfolding e_f c_d interior_closed_interval proof(rule *)
himmelma@35172
   944
      fix x assume "x\<in>{c<..<d}" "x\<in>{e<..<f}"
himmelma@35172
   945
      hence x:"c$i < d$i" "e$i < f$i" "c$i < f$i" "e$i < d$i" unfolding mem_interval apply-apply(erule_tac[!] x=i in allE)+ by auto
himmelma@35172
   946
      show False using c_d(2)[of i] apply- apply(erule_tac disjE)
himmelma@35172
   947
      proof(erule_tac[!] conjE) assume as:"c $ i = a $ i" "d $ i = (a $ i + b $ i) / 2"
himmelma@35172
   948
        show False using e_f(2)[of i] and i x unfolding as by(fastsimp simp add:field_simps)
himmelma@35172
   949
      next assume as:"c $ i = (a $ i + b $ i) / 2" "d $ i = b $ i"
himmelma@35172
   950
        show False using e_f(2)[of i] and i x unfolding as by(fastsimp simp add:field_simps)
himmelma@35172
   951
      qed qed qed
himmelma@35172
   952
  also have "\<Union> ?A = {a..b}" proof(rule set_ext,rule)
himmelma@35172
   953
    fix x assume "x\<in>\<Union>?A" then guess Y unfolding Union_iff ..
himmelma@35172
   954
    from this(1) guess c unfolding mem_Collect_eq .. then guess d ..
himmelma@35172
   955
    note c_d = this[THEN conjunct2,rule_format] `x\<in>Y`[unfolded this[THEN conjunct1]]
himmelma@35172
   956
    show "x\<in>{a..b}" unfolding mem_interval proof 
himmelma@35172
   957
      fix i show "a $ i \<le> x $ i \<and> x $ i \<le> b $ i"
himmelma@35172
   958
        using c_d(1)[of i] c_d(2)[unfolded mem_interval,THEN spec[where x=i]] by auto qed
himmelma@35172
   959
  next fix x assume x:"x\<in>{a..b}"
himmelma@35172
   960
    have "\<forall>i. \<exists>c d. (c = a$i \<and> d = (a$i + b$i) / 2 \<or> c = (a$i + b$i) / 2 \<and> d = b$i) \<and> c\<le>x$i \<and> x$i \<le> d"
himmelma@35172
   961
      (is "\<forall>i. \<exists>c d. ?P i c d") unfolding mem_interval proof fix i
himmelma@35172
   962
      have "?P i (a$i) ((a $ i + b $ i) / 2) \<or> ?P i ((a $ i + b $ i) / 2) (b$i)"
himmelma@35172
   963
        using x[unfolded mem_interval,THEN spec[where x=i]] by auto thus "\<exists>c d. ?P i c d" by blast
himmelma@35172
   964
    qed thus "x\<in>\<Union>?A" unfolding Union_iff lambda_skolem unfolding Bex_def mem_Collect_eq
himmelma@35172
   965
      apply-apply(erule exE)+ apply(rule_tac x="{xa..xaa}" in exI) unfolding mem_interval by auto
himmelma@35172
   966
  qed finally show False using assms by auto qed
himmelma@35172
   967
himmelma@35172
   968
lemma interval_bisection:
himmelma@35172
   969
  assumes "P {}" "(\<forall>s t. P s \<and> P t \<and> interior(s) \<inter> interior(t) = {} \<longrightarrow> P(s \<union> t))" "\<not> P {a..b::real^'n}"
himmelma@35172
   970
  obtains x where "x \<in> {a..b}" "\<forall>e>0. \<exists>c d. x \<in> {c..d} \<and> {c..d} \<subseteq> ball x e \<and> {c..d} \<subseteq> {a..b} \<and> ~P({c..d})"
himmelma@35172
   971
proof-
himmelma@35172
   972
  have "\<forall>x. \<exists>y. \<not> P {fst x..snd x} \<longrightarrow> (\<not> P {fst y..snd y} \<and> (\<forall>i. fst x$i \<le> fst y$i \<and> fst y$i \<le> snd y$i \<and> snd y$i \<le> snd x$i \<and>
himmelma@35172
   973
                           2 * (snd y$i - fst y$i) \<le> snd x$i - fst x$i))" proof case goal1 thus ?case proof-
himmelma@35172
   974
      presume "\<not> P {fst x..snd x} \<Longrightarrow> ?thesis"
himmelma@35172
   975
      thus ?thesis apply(cases "P {fst x..snd x}") by auto
himmelma@35172
   976
    next assume as:"\<not> P {fst x..snd x}" from interval_bisection_step[of P, OF assms(1-2) as] guess c d . 
himmelma@35172
   977
      thus ?thesis apply- apply(rule_tac x="(c,d)" in exI) by auto
himmelma@35172
   978
    qed qed then guess f apply-apply(drule choice) by(erule exE) note f=this
himmelma@35172
   979
  def AB \<equiv> "\<lambda>n. (f ^^ n) (a,b)" def A \<equiv> "\<lambda>n. fst(AB n)" and B \<equiv> "\<lambda>n. snd(AB n)" note ab_def = this AB_def
himmelma@35172
   980
  have "A 0 = a" "B 0 = b" "\<And>n. \<not> P {A(Suc n)..B(Suc n)} \<and>
himmelma@35172
   981
    (\<forall>i. A(n)$i \<le> A(Suc n)$i \<and> A(Suc n)$i \<le> B(Suc n)$i \<and> B(Suc n)$i \<le> B(n)$i \<and> 
himmelma@35172
   982
    2 * (B(Suc n)$i - A(Suc n)$i) \<le> B(n)$i - A(n)$i)" (is "\<And>n. ?P n")
himmelma@35172
   983
  proof- show "A 0 = a" "B 0 = b" unfolding ab_def by auto
himmelma@35172
   984
    case goal3 note S = ab_def funpow.simps o_def id_apply show ?case
himmelma@35172
   985
    proof(induct n) case 0 thus ?case unfolding S apply(rule f[rule_format]) using assms(3) by auto
himmelma@35172
   986
    next case (Suc n) show ?case unfolding S apply(rule f[rule_format]) using Suc unfolding S by auto
himmelma@35172
   987
    qed qed note AB = this(1-2) conjunctD2[OF this(3),rule_format]
himmelma@35172
   988
himmelma@35172
   989
  have interv:"\<And>e. 0 < e \<Longrightarrow> \<exists>n. \<forall>x\<in>{A n..B n}. \<forall>y\<in>{A n..B n}. dist x y < e"
himmelma@35172
   990
  proof- case goal1 guess n using real_arch_pow2[of "(setsum (\<lambda>i. b$i - a$i) UNIV) / e"] .. note n=this
himmelma@35172
   991
    show ?case apply(rule_tac x=n in exI) proof(rule,rule)
himmelma@35172
   992
      fix x y assume xy:"x\<in>{A n..B n}" "y\<in>{A n..B n}"
himmelma@35172
   993
      have "dist x y \<le> setsum (\<lambda>i. abs((x - y)$i)) UNIV" unfolding vector_dist_norm by(rule norm_le_l1)
himmelma@35172
   994
      also have "\<dots> \<le> setsum (\<lambda>i. B n$i - A n$i) UNIV"
himmelma@35172
   995
      proof(rule setsum_mono) fix i show "\<bar>(x - y) $ i\<bar> \<le> B n $ i - A n $ i"
himmelma@35172
   996
          using xy[unfolded mem_interval,THEN spec[where x=i]]
himmelma@35172
   997
          unfolding vector_minus_component by auto qed
himmelma@35172
   998
      also have "\<dots> \<le> setsum (\<lambda>i. b$i - a$i) UNIV / 2^n" unfolding setsum_divide_distrib
himmelma@35172
   999
      proof(rule setsum_mono) case goal1 thus ?case
himmelma@35172
  1000
        proof(induct n) case 0 thus ?case unfolding AB by auto
himmelma@35172
  1001
        next case (Suc n) have "B (Suc n) $ i - A (Suc n) $ i \<le> (B n $ i - A n $ i) / 2" using AB(4)[of n i] by auto
himmelma@35172
  1002
          also have "\<dots> \<le> (b $ i - a $ i) / 2 ^ Suc n" using Suc by(auto simp add:field_simps) finally show ?case .
himmelma@35172
  1003
        qed qed
himmelma@35172
  1004
      also have "\<dots> < e" using n using goal1 by(auto simp add:field_simps) finally show "dist x y < e" .
himmelma@35172
  1005
    qed qed
himmelma@35172
  1006
  { fix n m ::nat assume "m \<le> n" then guess d unfolding le_Suc_ex_iff .. note d=this
himmelma@35172
  1007
    have "{A n..B n} \<subseteq> {A m..B m}" unfolding d 
himmelma@35172
  1008
    proof(induct d) case 0 thus ?case by auto
himmelma@35172
  1009
    next case (Suc d) show ?case apply(rule subset_trans[OF _ Suc])
himmelma@35172
  1010
        apply(rule) unfolding mem_interval apply(rule,erule_tac x=i in allE)
himmelma@35172
  1011
      proof- case goal1 thus ?case using AB(4)[of "m + d" i] by(auto simp add:field_simps)
himmelma@35172
  1012
      qed qed } note ABsubset = this 
himmelma@35172
  1013
  have "\<exists>a. \<forall>n. a\<in>{A n..B n}" apply(rule decreasing_closed_nest[rule_format,OF closed_interval _ ABsubset interv])
himmelma@35172
  1014
  proof- fix n show "{A n..B n} \<noteq> {}" apply(cases "0<n") using AB(3)[of "n - 1"] assms(1,3) AB(1-2) by auto qed auto
himmelma@35172
  1015
  then guess x0 .. note x0=this[rule_format]
himmelma@35172
  1016
  show thesis proof(rule that[rule_format,of x0])
himmelma@35172
  1017
    show "x0\<in>{a..b}" using x0[of 0] unfolding AB .
himmelma@35172
  1018
    fix e assume "0 < (e::real)" from interv[OF this] guess n .. note n=this
himmelma@35172
  1019
    show "\<exists>c d. x0 \<in> {c..d} \<and> {c..d} \<subseteq> ball x0 e \<and> {c..d} \<subseteq> {a..b} \<and> \<not> P {c..d}"
himmelma@35172
  1020
      apply(rule_tac x="A n" in exI,rule_tac x="B n" in exI) apply(rule,rule x0) apply rule defer 
himmelma@35172
  1021
    proof show "\<not> P {A n..B n}" apply(cases "0<n") using AB(3)[of "n - 1"] assms(3) AB(1-2) by auto
himmelma@35172
  1022
      show "{A n..B n} \<subseteq> ball x0 e" using n using x0[of n] by auto
himmelma@35172
  1023
      show "{A n..B n} \<subseteq> {a..b}" unfolding AB(1-2)[symmetric] apply(rule ABsubset) by auto
himmelma@35172
  1024
    qed qed qed 
himmelma@35172
  1025
himmelma@35172
  1026
subsection {* Cousin's lemma. *}
himmelma@35172
  1027
himmelma@35172
  1028
lemma fine_division_exists: assumes "gauge g" 
himmelma@35172
  1029
  obtains p where "p tagged_division_of {a..b::real^'n}" "g fine p"
himmelma@35172
  1030
proof- presume "\<not> (\<exists>p. p tagged_division_of {a..b} \<and> g fine p) \<Longrightarrow> False"
himmelma@35172
  1031
  then guess p unfolding atomize_not not_not .. thus thesis apply-apply(rule that[of p]) by auto
himmelma@35172
  1032
next assume as:"\<not> (\<exists>p. p tagged_division_of {a..b} \<and> g fine p)"
himmelma@35172
  1033
  guess x apply(rule interval_bisection[of "\<lambda>s. \<exists>p. p tagged_division_of s \<and> g fine p",rule_format,OF _ _ as])
himmelma@35172
  1034
    apply(rule_tac x="{}" in exI) defer apply(erule conjE exE)+
himmelma@35172
  1035
  proof- show "{} tagged_division_of {} \<and> g fine {}" unfolding fine_def by auto
himmelma@35172
  1036
    fix s t p p' assume "p tagged_division_of s" "g fine p" "p' tagged_division_of t" "g fine p'" "interior s \<inter> interior t = {}"
himmelma@35172
  1037
    thus "\<exists>p. p tagged_division_of s \<union> t \<and> g fine p" apply-apply(rule_tac x="p \<union> p'" in exI) apply rule
himmelma@35172
  1038
      apply(rule tagged_division_union) prefer 4 apply(rule fine_union) by auto
himmelma@35172
  1039
  qed note x=this
himmelma@35172
  1040
  obtain e where e:"e>0" "ball x e \<subseteq> g x" using gaugeD[OF assms, of x] unfolding open_contains_ball by auto
himmelma@35172
  1041
  from x(2)[OF e(1)] guess c d apply-apply(erule exE conjE)+ . note c_d = this
himmelma@35172
  1042
  have "g fine {(x, {c..d})}" unfolding fine_def using e using c_d(2) by auto
himmelma@35172
  1043
  thus False using tagged_division_of_self[OF c_d(1)] using c_d by auto qed
himmelma@35172
  1044
himmelma@35172
  1045
subsection {* Basic theorems about integrals. *}
himmelma@35172
  1046
himmelma@35172
  1047
lemma has_integral_unique: fixes f::"real^'n \<Rightarrow> 'a::real_normed_vector"
himmelma@35172
  1048
  assumes "(f has_integral k1) i" "(f has_integral k2) i" shows "k1 = k2"
himmelma@35172
  1049
proof(rule ccontr) let ?e = "norm(k1 - k2) / 2" assume as:"k1 \<noteq> k2" hence e:"?e > 0" by auto
himmelma@35172
  1050
  have lem:"\<And>f::real^'n \<Rightarrow> 'a.  \<And> a b k1 k2.
himmelma@35172
  1051
    (f has_integral k1) ({a..b}) \<Longrightarrow> (f has_integral k2) ({a..b}) \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow> False"
himmelma@35172
  1052
  proof- case goal1 let ?e = "norm(k1 - k2) / 2" from goal1(3) have e:"?e > 0" by auto
himmelma@35172
  1053
    guess d1 by(rule has_integralD[OF goal1(1) e]) note d1=this
himmelma@35172
  1054
    guess d2 by(rule has_integralD[OF goal1(2) e]) note d2=this
himmelma@35172
  1055
    guess p by(rule fine_division_exists[OF gauge_inter[OF d1(1) d2(1)],of a b]) note p=this
himmelma@35172
  1056
    let ?c = "(\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)" have "norm (k1 - k2) \<le> norm (?c - k2) + norm (?c - k1)"
himmelma@35172
  1057
      using norm_triangle_ineq4[of "k1 - ?c" "k2 - ?c"] by(auto simp add:group_simps norm_minus_commute)
himmelma@35172
  1058
    also have "\<dots> < norm (k1 - k2) / 2 + norm (k1 - k2) / 2"
himmelma@35172
  1059
      apply(rule add_strict_mono) apply(rule_tac[!] d2(2) d1(2)) using p unfolding fine_def by auto
himmelma@35172
  1060
    finally show False by auto
himmelma@35172
  1061
  qed { presume "\<not> (\<exists>a b. i = {a..b}) \<Longrightarrow> False"
himmelma@35172
  1062
    thus False apply-apply(cases "\<exists>a b. i = {a..b}")
himmelma@35172
  1063
      using assms by(auto simp add:has_integral intro:lem[OF _ _ as]) }
himmelma@35172
  1064
  assume as:"\<not> (\<exists>a b. i = {a..b})"
himmelma@35172
  1065
  guess B1 by(rule has_integral_altD[OF assms(1) as,OF e]) note B1=this[rule_format]
himmelma@35172
  1066
  guess B2 by(rule has_integral_altD[OF assms(2) as,OF e]) note B2=this[rule_format]
himmelma@35172
  1067
  have "\<exists>a b::real^'n. ball 0 B1 \<union> ball 0 B2 \<subseteq> {a..b}" apply(rule bounded_subset_closed_interval)
himmelma@35172
  1068
    using bounded_Un bounded_ball by auto then guess a b apply-by(erule exE)+
himmelma@35172
  1069
  note ab=conjunctD2[OF this[unfolded Un_subset_iff]]
himmelma@35172
  1070
  guess w using B1(2)[OF ab(1)] .. note w=conjunctD2[OF this]
himmelma@35172
  1071
  guess z using B2(2)[OF ab(2)] .. note z=conjunctD2[OF this]
himmelma@35172
  1072
  have "z = w" using lem[OF w(1) z(1)] by auto
himmelma@35172
  1073
  hence "norm (k1 - k2) \<le> norm (z - k2) + norm (w - k1)"
himmelma@35172
  1074
    using norm_triangle_ineq4[of "k1 - w" "k2 - z"] by(auto simp add: norm_minus_commute) 
himmelma@35172
  1075
  also have "\<dots> < norm (k1 - k2) / 2 + norm (k1 - k2) / 2" apply(rule add_strict_mono) by(rule_tac[!] z(2) w(2))
himmelma@35172
  1076
  finally show False by auto qed
himmelma@35172
  1077
himmelma@35172
  1078
lemma integral_unique[intro]:
himmelma@35172
  1079
  "(f has_integral y) k \<Longrightarrow> integral k f = y"
himmelma@35172
  1080
  unfolding integral_def apply(rule some_equality) by(auto intro: has_integral_unique) 
himmelma@35172
  1081
himmelma@35172
  1082
lemma has_integral_is_0: fixes f::"real^'n \<Rightarrow> 'a::real_normed_vector" 
himmelma@35172
  1083
  assumes "\<forall>x\<in>s. f x = 0" shows "(f has_integral 0) s"
himmelma@35172
  1084
proof- have lem:"\<And>a b. \<And>f::real^'n \<Rightarrow> 'a.
himmelma@35172
  1085
    (\<forall>x\<in>{a..b}. f(x) = 0) \<Longrightarrow> (f has_integral 0) ({a..b})" unfolding has_integral
himmelma@35172
  1086
  proof(rule,rule) fix a b e and f::"real^'n \<Rightarrow> 'a"
himmelma@35172
  1087
    assume as:"\<forall>x\<in>{a..b}. f x = 0" "0 < (e::real)"
himmelma@35172
  1088
    show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e)"
himmelma@35172
  1089
      apply(rule_tac x="\<lambda>x. ball x 1" in exI)  apply(rule,rule gaugeI) unfolding centre_in_ball defer apply(rule open_ball)
himmelma@35172
  1090
    proof(rule,rule,erule conjE) case goal1
himmelma@35172
  1091
      have "(\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) = 0" proof(rule setsum_0',rule)
himmelma@35172
  1092
        fix x assume x:"x\<in>p" have "f (fst x) = 0" using tagged_division_ofD(2-3)[OF goal1(1), of "fst x" "snd x"] using as x by auto
himmelma@35172
  1093
        thus "(\<lambda>(x, k). content k *\<^sub>R f x) x = 0" apply(subst surjective_pairing[of x]) unfolding split_conv by auto
himmelma@35172
  1094
      qed thus ?case using as by auto
himmelma@35172
  1095
    qed auto qed  { presume "\<not> (\<exists>a b. s = {a..b}) \<Longrightarrow> ?thesis"
himmelma@35172
  1096
    thus ?thesis apply-apply(cases "\<exists>a b. s = {a..b}")
himmelma@35172
  1097
      using assms by(auto simp add:has_integral intro:lem) }
himmelma@35172
  1098
  have *:"(\<lambda>x. if x \<in> s then f x else 0) = (\<lambda>x. 0)" apply(rule ext) using assms by auto
himmelma@35172
  1099
  assume "\<not> (\<exists>a b. s = {a..b})" thus ?thesis apply(subst has_integral_alt) unfolding if_not_P *
himmelma@35172
  1100
  apply(rule,rule,rule_tac x=1 in exI,rule) defer apply(rule,rule,rule)
himmelma@35172
  1101
  proof- fix e::real and a b assume "e>0"
himmelma@35172
  1102
    thus "\<exists>z. ((\<lambda>x::real^'n. 0::'a) has_integral z) {a..b} \<and> norm (z - 0) < e"
himmelma@35172
  1103
      apply(rule_tac x=0 in exI) apply(rule,rule lem) by auto
himmelma@35172
  1104
  qed auto qed
himmelma@35172
  1105
himmelma@35172
  1106
lemma has_integral_0[simp]: "((\<lambda>x::real^'n. 0) has_integral 0) s"
himmelma@35172
  1107
  apply(rule has_integral_is_0) by auto 
himmelma@35172
  1108
himmelma@35172
  1109
lemma has_integral_0_eq[simp]: "((\<lambda>x. 0) has_integral i) s \<longleftrightarrow> i = 0"
himmelma@35172
  1110
  using has_integral_unique[OF has_integral_0] by auto
himmelma@35172
  1111
himmelma@35172
  1112
lemma has_integral_linear: fixes f::"real^'n \<Rightarrow> 'a::real_normed_vector"
himmelma@35172
  1113
  assumes "(f has_integral y) s" "bounded_linear h" shows "((h o f) has_integral ((h y))) s"
himmelma@35172
  1114
proof- interpret bounded_linear h using assms(2) . from pos_bounded guess B .. note B=conjunctD2[OF this,rule_format]
himmelma@35172
  1115
  have lem:"\<And>f::real^'n \<Rightarrow> 'a. \<And> y a b.
himmelma@35172
  1116
    (f has_integral y) ({a..b}) \<Longrightarrow> ((h o f) has_integral h(y)) ({a..b})"
himmelma@35172
  1117
  proof(subst has_integral,rule,rule) case goal1
himmelma@35172
  1118
    from pos_bounded guess B .. note B=conjunctD2[OF this,rule_format]
himmelma@35172
  1119
    have *:"e / B > 0" apply(rule divide_pos_pos) using goal1(2) B by auto
himmelma@35172
  1120
    guess g using has_integralD[OF goal1(1) *] . note g=this
himmelma@35172
  1121
    show ?case apply(rule_tac x=g in exI) apply(rule,rule g(1))
himmelma@35172
  1122
    proof(rule,rule,erule conjE) fix p assume as:"p tagged_division_of {a..b}" "g fine p" 
himmelma@35172
  1123
      have *:"\<And>x k. h ((\<lambda>(x, k). content k *\<^sub>R f x) x) = (\<lambda>(x, k). h (content k *\<^sub>R f x)) x" by auto
himmelma@35172
  1124
      have "(\<Sum>(x, k)\<in>p. content k *\<^sub>R (h \<circ> f) x) = setsum (h \<circ> (\<lambda>(x, k). content k *\<^sub>R f x)) p"
himmelma@35172
  1125
        unfolding o_def unfolding scaleR[THEN sym] * by simp
himmelma@35172
  1126
      also have "\<dots> = h (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)" using setsum[of "\<lambda>(x,k). content k *\<^sub>R f x" p] using as by auto
himmelma@35172
  1127
      finally have *:"(\<Sum>(x, k)\<in>p. content k *\<^sub>R (h \<circ> f) x) = h (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)" .
himmelma@35172
  1128
      show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (h \<circ> f) x) - h y) < e" unfolding * diff[THEN sym]
himmelma@35172
  1129
        apply(rule le_less_trans[OF B(2)]) using g(2)[OF as] B(1) by(auto simp add:field_simps)
himmelma@35172
  1130
    qed qed { presume "\<not> (\<exists>a b. s = {a..b}) \<Longrightarrow> ?thesis"
himmelma@35172
  1131
    thus ?thesis apply-apply(cases "\<exists>a b. s = {a..b}") using assms by(auto simp add:has_integral intro!:lem) }
himmelma@35172
  1132
  assume as:"\<not> (\<exists>a b. s = {a..b})" thus ?thesis apply(subst has_integral_alt) unfolding if_not_P
himmelma@35172
  1133
  proof(rule,rule) fix e::real  assume e:"0<e"
himmelma@35172
  1134
    have *:"0 < e/B" by(rule divide_pos_pos,rule e,rule B(1))
himmelma@35172
  1135
    guess M using has_integral_altD[OF assms(1) as *,rule_format] . note M=this
himmelma@35172
  1136
    show "\<exists>B>0. \<forall>a b. ball 0 B \<subseteq> {a..b} \<longrightarrow> (\<exists>z. ((\<lambda>x. if x \<in> s then (h \<circ> f) x else 0) has_integral z) {a..b} \<and> norm (z - h y) < e)"
himmelma@35172
  1137
      apply(rule_tac x=M in exI) apply(rule,rule M(1))
himmelma@35172
  1138
    proof(rule,rule,rule) case goal1 guess z using M(2)[OF goal1(1)] .. note z=conjunctD2[OF this]
himmelma@35172
  1139
      have *:"(\<lambda>x. if x \<in> s then (h \<circ> f) x else 0) = h \<circ> (\<lambda>x. if x \<in> s then f x else 0)"
himmelma@35172
  1140
        unfolding o_def apply(rule ext) using zero by auto
himmelma@35172
  1141
      show ?case apply(rule_tac x="h z" in exI,rule) unfolding * apply(rule lem[OF z(1)]) unfolding diff[THEN sym]
himmelma@35172
  1142
        apply(rule le_less_trans[OF B(2)]) using B(1) z(2) by(auto simp add:field_simps)
himmelma@35172
  1143
    qed qed qed
himmelma@35172
  1144
himmelma@35172
  1145
lemma has_integral_cmul:
himmelma@35172
  1146
  shows "(f has_integral k) s \<Longrightarrow> ((\<lambda>x. c *\<^sub>R f x) has_integral (c *\<^sub>R k)) s"
himmelma@35172
  1147
  unfolding o_def[THEN sym] apply(rule has_integral_linear,assumption)
himmelma@35172
  1148
  by(rule scaleR.bounded_linear_right)
himmelma@35172
  1149
himmelma@35172
  1150
lemma has_integral_neg:
himmelma@35172
  1151
  shows "(f has_integral k) s \<Longrightarrow> ((\<lambda>x. -(f x)) has_integral (-k)) s"
himmelma@35172
  1152
  apply(drule_tac c="-1" in has_integral_cmul) by auto
himmelma@35172
  1153
himmelma@35172
  1154
lemma has_integral_add: fixes f::"real^'n \<Rightarrow> 'a::real_normed_vector" 
himmelma@35172
  1155
  assumes "(f has_integral k) s" "(g has_integral l) s"
himmelma@35172
  1156
  shows "((\<lambda>x. f x + g x) has_integral (k + l)) s"
himmelma@35172
  1157
proof- have lem:"\<And>f g::real^'n \<Rightarrow> 'a. \<And>a b k l.
himmelma@35172
  1158
    (f has_integral k) ({a..b}) \<Longrightarrow> (g has_integral l) ({a..b}) \<Longrightarrow>
himmelma@35172
  1159
     ((\<lambda>x. f(x) + g(x)) has_integral (k + l)) ({a..b})" proof- case goal1
himmelma@35172
  1160
    show ?case unfolding has_integral proof(rule,rule) fix e::real assume e:"e>0" hence *:"e/2>0" by auto
himmelma@35172
  1161
      guess d1 using has_integralD[OF goal1(1) *] . note d1=this
himmelma@35172
  1162
      guess d2 using has_integralD[OF goal1(2) *] . note d2=this
himmelma@35172
  1163
      show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) - (k + l)) < e)"
himmelma@35172
  1164
        apply(rule_tac x="\<lambda>x. (d1 x) \<inter> (d2 x)" in exI) apply(rule,rule gauge_inter[OF d1(1) d2(1)])
himmelma@35172
  1165
      proof(rule,rule,erule conjE) fix p assume as:"p tagged_division_of {a..b}" "(\<lambda>x. d1 x \<inter> d2 x) fine p"
himmelma@35172
  1166
        have *:"(\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) = (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>p. content k *\<^sub>R g x)"
himmelma@35172
  1167
          unfolding scaleR_right_distrib setsum_addf[of "\<lambda>(x,k). content k *\<^sub>R f x" "\<lambda>(x,k). content k *\<^sub>R g x" p,THEN sym]
himmelma@35172
  1168
          by(rule setsum_cong2,auto)
himmelma@35172
  1169
        have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) - (k + l)) = norm (((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - k) + ((\<Sum>(x, k)\<in>p. content k *\<^sub>R g x) - l))"
himmelma@35172
  1170
          unfolding * by(auto simp add:group_simps) also let ?res = "\<dots>"
himmelma@35172
  1171
        from as have *:"d1 fine p" "d2 fine p" unfolding fine_inter by auto
himmelma@35172
  1172
        have "?res < e/2 + e/2" apply(rule le_less_trans[OF norm_triangle_ineq])
himmelma@35172
  1173
          apply(rule add_strict_mono) using d1(2)[OF as(1) *(1)] and d2(2)[OF as(1) *(2)] by auto
himmelma@35172
  1174
        finally show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) - (k + l)) < e" by auto
himmelma@35172
  1175
      qed qed qed { presume "\<not> (\<exists>a b. s = {a..b}) \<Longrightarrow> ?thesis"
himmelma@35172
  1176
    thus ?thesis apply-apply(cases "\<exists>a b. s = {a..b}") using assms by(auto simp add:has_integral intro!:lem) }
himmelma@35172
  1177
  assume as:"\<not> (\<exists>a b. s = {a..b})" thus ?thesis apply(subst has_integral_alt) unfolding if_not_P
himmelma@35172
  1178
  proof(rule,rule) case goal1 hence *:"e/2 > 0" by auto
himmelma@35172
  1179
    from has_integral_altD[OF assms(1) as *] guess B1 . note B1=this[rule_format]
himmelma@35172
  1180
    from has_integral_altD[OF assms(2) as *] guess B2 . note B2=this[rule_format]
himmelma@35172
  1181
    show ?case apply(rule_tac x="max B1 B2" in exI) apply(rule,rule min_max.less_supI1,rule B1)
himmelma@35172
  1182
    proof(rule,rule,rule) fix a b assume "ball 0 (max B1 B2) \<subseteq> {a..b::real^'n}"
himmelma@35172
  1183
      hence *:"ball 0 B1 \<subseteq> {a..b::real^'n}" "ball 0 B2 \<subseteq> {a..b::real^'n}" by auto
himmelma@35172
  1184
      guess w using B1(2)[OF *(1)] .. note w=conjunctD2[OF this]
himmelma@35172
  1185
      guess z using B2(2)[OF *(2)] .. note z=conjunctD2[OF this]
himmelma@35172
  1186
      have *:"\<And>x. (if x \<in> s then f x + g x else 0) = (if x \<in> s then f x else 0) + (if x \<in> s then g x else 0)" by auto
himmelma@35172
  1187
      show "\<exists>z. ((\<lambda>x. if x \<in> s then f x + g x else 0) has_integral z) {a..b} \<and> norm (z - (k + l)) < e"
himmelma@35172
  1188
        apply(rule_tac x="w + z" in exI) apply(rule,rule lem[OF w(1) z(1), unfolded *[THEN sym]])
himmelma@35172
  1189
        using norm_triangle_ineq[of "w - k" "z - l"] w(2) z(2) by(auto simp add:field_simps)
himmelma@35172
  1190
    qed qed qed
himmelma@35172
  1191
himmelma@35172
  1192
lemma has_integral_sub:
himmelma@35172
  1193
  shows "(f has_integral k) s \<Longrightarrow> (g has_integral l) s \<Longrightarrow> ((\<lambda>x. f(x) - g(x)) has_integral (k - l)) s"
himmelma@35172
  1194
  using has_integral_add[OF _ has_integral_neg,of f k s g l] unfolding group_simps by auto
himmelma@35172
  1195
himmelma@35172
  1196
lemma integral_0: "integral s (\<lambda>x::real^'n. 0::real^'m) = 0"
himmelma@35172
  1197
  by(rule integral_unique has_integral_0)+
himmelma@35172
  1198
himmelma@35172
  1199
lemma integral_add:
himmelma@35172
  1200
  shows "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow>
himmelma@35172
  1201
   integral s (\<lambda>x. f x + g x) = integral s f + integral s g"
himmelma@35172
  1202
  apply(rule integral_unique) apply(drule integrable_integral)+
himmelma@35172
  1203
  apply(rule has_integral_add) by assumption+
himmelma@35172
  1204
himmelma@35172
  1205
lemma integral_cmul:
himmelma@35172
  1206
  shows "f integrable_on s \<Longrightarrow> integral s (\<lambda>x. c *\<^sub>R f x) = c *\<^sub>R integral s f"
himmelma@35172
  1207
  apply(rule integral_unique) apply(drule integrable_integral)+
himmelma@35172
  1208
  apply(rule has_integral_cmul) by assumption+
himmelma@35172
  1209
himmelma@35172
  1210
lemma integral_neg:
himmelma@35172
  1211
  shows "f integrable_on s \<Longrightarrow> integral s (\<lambda>x. - f x) = - integral s f"
himmelma@35172
  1212
  apply(rule integral_unique) apply(drule integrable_integral)+
himmelma@35172
  1213
  apply(rule has_integral_neg) by assumption+
himmelma@35172
  1214
himmelma@35172
  1215
lemma integral_sub:
himmelma@35172
  1216
  shows "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow> integral s (\<lambda>x. f x - g x) = integral s f - integral s g"
himmelma@35172
  1217
  apply(rule integral_unique) apply(drule integrable_integral)+
himmelma@35172
  1218
  apply(rule has_integral_sub) by assumption+
himmelma@35172
  1219
himmelma@35172
  1220
lemma integrable_0: "(\<lambda>x. 0) integrable_on s"
himmelma@35172
  1221
  unfolding integrable_on_def using has_integral_0 by auto
himmelma@35172
  1222
himmelma@35172
  1223
lemma integrable_add:
himmelma@35172
  1224
  shows "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow> (\<lambda>x. f x + g x) integrable_on s"
himmelma@35172
  1225
  unfolding integrable_on_def by(auto intro: has_integral_add)
himmelma@35172
  1226
himmelma@35172
  1227
lemma integrable_cmul:
himmelma@35172
  1228
  shows "f integrable_on s \<Longrightarrow> (\<lambda>x. c *\<^sub>R f(x)) integrable_on s"
himmelma@35172
  1229
  unfolding integrable_on_def by(auto intro: has_integral_cmul)
himmelma@35172
  1230
himmelma@35172
  1231
lemma integrable_neg:
himmelma@35172
  1232
  shows "f integrable_on s \<Longrightarrow> (\<lambda>x. -f(x)) integrable_on s"
himmelma@35172
  1233
  unfolding integrable_on_def by(auto intro: has_integral_neg)
himmelma@35172
  1234
himmelma@35172
  1235
lemma integrable_sub:
himmelma@35172
  1236
  shows "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow> (\<lambda>x. f x - g x) integrable_on s"
himmelma@35172
  1237
  unfolding integrable_on_def by(auto intro: has_integral_sub)
himmelma@35172
  1238
himmelma@35172
  1239
lemma integrable_linear:
himmelma@35172
  1240
  shows "f integrable_on s \<Longrightarrow> bounded_linear h \<Longrightarrow> (h o f) integrable_on s"
himmelma@35172
  1241
  unfolding integrable_on_def by(auto intro: has_integral_linear)
himmelma@35172
  1242
himmelma@35172
  1243
lemma integral_linear:
himmelma@35172
  1244
  shows "f integrable_on s \<Longrightarrow> bounded_linear h \<Longrightarrow> integral s (h o f) = h(integral s f)"
himmelma@35172
  1245
  apply(rule has_integral_unique) defer unfolding has_integral_integral 
himmelma@35172
  1246
  apply(drule has_integral_linear,assumption,assumption) unfolding has_integral_integral[THEN sym]
himmelma@35172
  1247
  apply(rule integrable_linear) by assumption+
himmelma@35172
  1248
himmelma@35172
  1249
lemma has_integral_setsum:
himmelma@35172
  1250
  assumes "finite t" "\<forall>a\<in>t. ((f a) has_integral (i a)) s"
himmelma@35172
  1251
  shows "((\<lambda>x. setsum (\<lambda>a. f a x) t) has_integral (setsum i t)) s"
himmelma@35172
  1252
proof(insert assms(1) subset_refl[of t],induct rule:finite_subset_induct)
himmelma@35172
  1253
  case (insert x F) show ?case unfolding setsum_insert[OF insert(1,3)]
himmelma@35172
  1254
    apply(rule has_integral_add) using insert assms by auto
himmelma@35172
  1255
qed auto
himmelma@35172
  1256
himmelma@35172
  1257
lemma integral_setsum:
himmelma@35172
  1258
  shows "finite t \<Longrightarrow> \<forall>a\<in>t. (f a) integrable_on s \<Longrightarrow>
himmelma@35172
  1259
  integral s (\<lambda>x. setsum (\<lambda>a. f a x) t) = setsum (\<lambda>a. integral s (f a)) t"
himmelma@35172
  1260
  apply(rule integral_unique) apply(rule has_integral_setsum)
himmelma@35172
  1261
  using integrable_integral by auto
himmelma@35172
  1262
himmelma@35172
  1263
lemma integrable_setsum:
himmelma@35172
  1264
  shows "finite t \<Longrightarrow> \<forall>a \<in> t.(f a) integrable_on s \<Longrightarrow> (\<lambda>x. setsum (\<lambda>a. f a x) t) integrable_on s"
himmelma@35172
  1265
  unfolding integrable_on_def apply(drule bchoice) using has_integral_setsum[of t] by auto
himmelma@35172
  1266
himmelma@35172
  1267
lemma has_integral_eq:
himmelma@35172
  1268
  assumes "\<forall>x\<in>s. f x = g x" "(f has_integral k) s" shows "(g has_integral k) s"
himmelma@35172
  1269
  using has_integral_sub[OF assms(2), of "\<lambda>x. f x - g x" 0]
himmelma@35172
  1270
  using has_integral_is_0[of s "\<lambda>x. f x - g x"] using assms(1) by auto
himmelma@35172
  1271
himmelma@35172
  1272
lemma integrable_eq:
himmelma@35172
  1273
  shows "\<forall>x\<in>s. f x = g x \<Longrightarrow> f integrable_on s \<Longrightarrow> g integrable_on s"
himmelma@35172
  1274
  unfolding integrable_on_def using has_integral_eq[of s f g] by auto
himmelma@35172
  1275
himmelma@35172
  1276
lemma has_integral_eq_eq:
himmelma@35172
  1277
  shows "\<forall>x\<in>s. f x = g x \<Longrightarrow> ((f has_integral k) s \<longleftrightarrow> (g has_integral k) s)"
himmelma@35172
  1278
  using has_integral_eq[of s f g] has_integral_eq[of s g f] by auto 
himmelma@35172
  1279
himmelma@35172
  1280
lemma has_integral_null[dest]:
himmelma@35172
  1281
  assumes "content({a..b}) = 0" shows  "(f has_integral 0) ({a..b})"
himmelma@35172
  1282
  unfolding has_integral apply(rule,rule,rule_tac x="\<lambda>x. ball x 1" in exI,rule) defer
himmelma@35172
  1283
proof(rule,rule,erule conjE) fix e::real assume e:"e>0" thus "gauge (\<lambda>x. ball x 1)" by auto
himmelma@35172
  1284
  fix p assume p:"p tagged_division_of {a..b}" (*"(\<lambda>x. ball x 1) fine p"*)
himmelma@35172
  1285
  have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) = 0" unfolding norm_eq_zero diff_0_right
himmelma@35172
  1286
    using setsum_content_null[OF assms(1) p, of f] . 
himmelma@35172
  1287
  thus "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e" using e by auto qed
himmelma@35172
  1288
himmelma@35172
  1289
lemma has_integral_null_eq[simp]:
himmelma@35172
  1290
  shows "content({a..b}) = 0 \<Longrightarrow> ((f has_integral i) ({a..b}) \<longleftrightarrow> i = 0)"
himmelma@35172
  1291
  apply rule apply(rule has_integral_unique,assumption) 
himmelma@35172
  1292
  apply(drule has_integral_null,assumption)
himmelma@35172
  1293
  apply(drule has_integral_null) by auto
himmelma@35172
  1294
himmelma@35172
  1295
lemma integral_null[dest]: shows "content({a..b}) = 0 \<Longrightarrow> integral({a..b}) f = 0"
himmelma@35172
  1296
  by(rule integral_unique,drule has_integral_null)
himmelma@35172
  1297
himmelma@35172
  1298
lemma integrable_on_null[dest]: shows "content({a..b}) = 0 \<Longrightarrow> f integrable_on {a..b}"
himmelma@35172
  1299
  unfolding integrable_on_def apply(drule has_integral_null) by auto
himmelma@35172
  1300
himmelma@35172
  1301
lemma has_integral_empty[intro]: shows "(f has_integral 0) {}"
himmelma@35172
  1302
  unfolding empty_as_interval apply(rule has_integral_null) 
himmelma@35172
  1303
  using content_empty unfolding empty_as_interval .
himmelma@35172
  1304
himmelma@35172
  1305
lemma has_integral_empty_eq[simp]: shows "(f has_integral i) {} \<longleftrightarrow> i = 0"
himmelma@35172
  1306
  apply(rule,rule has_integral_unique,assumption) by auto
himmelma@35172
  1307
himmelma@35172
  1308
lemma integrable_on_empty[intro]: shows "f integrable_on {}" unfolding integrable_on_def by auto
himmelma@35172
  1309
himmelma@35172
  1310
lemma integral_empty[simp]: shows "integral {} f = 0"
himmelma@35172
  1311
  apply(rule integral_unique) using has_integral_empty .
himmelma@35172
  1312
himmelma@35172
  1313
lemma has_integral_refl[intro]: shows "(f has_integral 0) {a..a}"
himmelma@35172
  1314
  apply(rule has_integral_null) unfolding content_eq_0_interior
himmelma@35172
  1315
  unfolding interior_closed_interval using interval_sing by auto
himmelma@35172
  1316
himmelma@35172
  1317
lemma integrable_on_refl[intro]: shows "f integrable_on {a..a}" unfolding integrable_on_def by auto
himmelma@35172
  1318
himmelma@35172
  1319
lemma integral_refl: shows "integral {a..a} f = 0" apply(rule integral_unique) by auto
himmelma@35172
  1320
himmelma@35172
  1321
subsection {* Cauchy-type criterion for integrability. *}
himmelma@35172
  1322
himmelma@35172
  1323
lemma integrable_cauchy: fixes f::"real^'n \<Rightarrow> 'a::{real_normed_vector,complete_space}" 
himmelma@35172
  1324
  shows "f integrable_on {a..b} \<longleftrightarrow>
himmelma@35172
  1325
  (\<forall>e>0.\<exists>d. gauge d \<and> (\<forall>p1 p2. p1 tagged_division_of {a..b} \<and> d fine p1 \<and>
himmelma@35172
  1326
                            p2 tagged_division_of {a..b} \<and> d fine p2
himmelma@35172
  1327
                            \<longrightarrow> norm(setsum (\<lambda>(x,k). content k *\<^sub>R f x) p1 -
himmelma@35172
  1328
                                     setsum (\<lambda>(x,k). content k *\<^sub>R f x) p2) < e))" (is "?l = (\<forall>e>0. \<exists>d. ?P e d)")
himmelma@35172
  1329
proof assume ?l
himmelma@35172
  1330
  then guess y unfolding integrable_on_def has_integral .. note y=this
himmelma@35172
  1331
  show "\<forall>e>0. \<exists>d. ?P e d" proof(rule,rule) case goal1 hence "e/2 > 0" by auto
himmelma@35172
  1332
    then guess d apply- apply(drule y[rule_format]) by(erule exE,erule conjE) note d=this[rule_format]
himmelma@35172
  1333
    show ?case apply(rule_tac x=d in exI,rule,rule d) apply(rule,rule,rule,(erule conjE)+)
himmelma@35172
  1334
    proof- fix p1 p2 assume as:"p1 tagged_division_of {a..b}" "d fine p1" "p2 tagged_division_of {a..b}" "d fine p2"
himmelma@35172
  1335
      show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e"
himmelma@35172
  1336
        apply(rule dist_triangle_half_l[where y=y,unfolded vector_dist_norm])
himmelma@35172
  1337
        using d(2)[OF conjI[OF as(1-2)]] d(2)[OF conjI[OF as(3-4)]] .
himmelma@35172
  1338
    qed qed
himmelma@35172
  1339
next assume "\<forall>e>0. \<exists>d. ?P e d" hence "\<forall>n::nat. \<exists>d. ?P (inverse(real (n + 1))) d" by auto
himmelma@35172
  1340
  from choice[OF this] guess d .. note d=conjunctD2[OF this[rule_format],rule_format]
himmelma@35172
  1341
  have "\<And>n. gauge (\<lambda>x. \<Inter>{d i x |i. i \<in> {0..n}})" apply(rule gauge_inters) using d(1) by auto
himmelma@35172
  1342
  hence "\<forall>n. \<exists>p. p tagged_division_of {a..b} \<and> (\<lambda>x. \<Inter>{d i x |i. i \<in> {0..n}}) fine p" apply-
himmelma@35172
  1343
  proof case goal1 from this[of n] show ?case apply(drule_tac fine_division_exists) by auto qed
himmelma@35172
  1344
  from choice[OF this] guess p .. note p = conjunctD2[OF this[rule_format]]
himmelma@35172
  1345
  have dp:"\<And>i n. i\<le>n \<Longrightarrow> d i fine p n" using p(2) unfolding fine_inters by auto
himmelma@35172
  1346
  have "Cauchy (\<lambda>n. setsum (\<lambda>(x,k). content k *\<^sub>R (f x)) (p n))"
himmelma@35172
  1347
  proof(rule CauchyI) case goal1 then guess N unfolding real_arch_inv[of e] .. note N=this
himmelma@35172
  1348
    show ?case apply(rule_tac x=N in exI)
himmelma@35172
  1349
    proof(rule,rule,rule,rule) fix m n assume mn:"N \<le> m" "N \<le> n" have *:"N = (N - 1) + 1" using N by auto
himmelma@35172
  1350
      show "norm ((\<Sum>(x, k)\<in>p m. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p n. content k *\<^sub>R f x)) < e"
himmelma@35172
  1351
        apply(rule less_trans[OF _ N[THEN conjunct2,THEN conjunct2]]) apply(subst *) apply(rule d(2))
himmelma@35172
  1352
        using dp p(1) using mn by auto 
himmelma@35172
  1353
    qed qed
himmelma@35172
  1354
  then guess y unfolding convergent_eq_cauchy[THEN sym] .. note y=this[unfolded Lim_sequentially,rule_format]
himmelma@35172
  1355
  show ?l unfolding integrable_on_def has_integral apply(rule_tac x=y in exI)
himmelma@35172
  1356
  proof(rule,rule) fix e::real assume "e>0" hence *:"e/2 > 0" by auto
himmelma@35172
  1357
    then guess N1 unfolding real_arch_inv[of "e/2"] .. note N1=this hence N1':"N1 = N1 - 1 + 1" by auto
himmelma@35172
  1358
    guess N2 using y[OF *] .. note N2=this
himmelma@35172
  1359
    show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - y) < e)"
himmelma@35172
  1360
      apply(rule_tac x="d (N1 + N2)" in exI) apply rule defer 
himmelma@35172
  1361
    proof(rule,rule,erule conjE) show "gauge (d (N1 + N2))" using d by auto
himmelma@35172
  1362
      fix q assume as:"q tagged_division_of {a..b}" "d (N1 + N2) fine q"
himmelma@35172
  1363
      have *:"inverse (real (N1 + N2 + 1)) < e / 2" apply(rule less_trans) using N1 by auto
himmelma@35172
  1364
      show "norm ((\<Sum>(x, k)\<in>q. content k *\<^sub>R f x) - y) < e" apply(rule norm_triangle_half_r)
himmelma@35172
  1365
        apply(rule less_trans[OF _ *]) apply(subst N1', rule d(2)[of "p (N1+N2)"]) defer
himmelma@35172
  1366
        using N2[rule_format,unfolded vector_dist_norm,of "N1+N2"]
himmelma@35172
  1367
        using as dp[of "N1 - 1 + 1 + N2" "N1 + N2"] using p(1)[of "N1 + N2"] using N1 by auto qed qed qed
himmelma@35172
  1368
himmelma@35172
  1369
subsection {* Additivity of integral on abutting intervals. *}
himmelma@35172
  1370
himmelma@35172
  1371
lemma interval_split:
himmelma@35172
  1372
  "{a..b::real^'n} \<inter> {x. x$k \<le> c} = {a .. (\<chi> i. if i = k then min (b$k) c else b$i)}"
himmelma@35172
  1373
  "{a..b} \<inter> {x. x$k \<ge> c} = {(\<chi> i. if i = k then max (a$k) c else a$i) .. b}"
himmelma@35172
  1374
  apply(rule_tac[!] set_ext) unfolding Int_iff mem_interval mem_Collect_eq
himmelma@35172
  1375
  unfolding Cart_lambda_beta by auto
himmelma@35172
  1376
himmelma@35172
  1377
lemma content_split:
himmelma@35172
  1378
  "content {a..b::real^'n} = content({a..b} \<inter> {x. x$k \<le> c}) + content({a..b} \<inter> {x. x$k >= c})"
himmelma@35172
  1379
proof- note simps = interval_split content_closed_interval_cases Cart_lambda_beta vector_le_def
himmelma@35172
  1380
  { presume "a\<le>b \<Longrightarrow> ?thesis" thus ?thesis apply(cases "a\<le>b") unfolding simps by auto }
himmelma@35172
  1381
  have *:"UNIV = insert k (UNIV - {k})" "\<And>x. finite (UNIV-{x::'n})" "\<And>x. x\<notin>UNIV-{x}" by auto
himmelma@35172
  1382
  have *:"\<And>X Y Z. (\<Prod>i\<in>UNIV. Z i (if i = k then X else Y i)) = Z k X * (\<Prod>i\<in>UNIV-{k}. Z i (Y i))"
himmelma@35172
  1383
    "(\<Prod>i\<in>UNIV. b$i - a$i) = (\<Prod>i\<in>UNIV-{k}. b$i - a$i) * (b$k - a$k)" 
himmelma@35172
  1384
    apply(subst *(1)) defer apply(subst *(1)) unfolding setprod_insert[OF *(2-)] by auto
himmelma@35172
  1385
  assume as:"a\<le>b" moreover have "\<And>x. min (b $ k) c = max (a $ k) c
himmelma@35172
  1386
    \<Longrightarrow> x* (b$k - a$k) = x*(max (a $ k) c - a $ k) + x*(b $ k - max (a $ k) c)"
himmelma@35172
  1387
    by  (auto simp add:field_simps)
himmelma@35172
  1388
  moreover have "\<not> a $ k \<le> c \<Longrightarrow> \<not> c \<le> b $ k \<Longrightarrow> False"
himmelma@35172
  1389
    unfolding not_le using as[unfolded vector_le_def,rule_format,of k] by auto
himmelma@35172
  1390
  ultimately show ?thesis 
himmelma@35172
  1391
    unfolding simps unfolding *(1)[of "\<lambda>i x. b$i - x"] *(1)[of "\<lambda>i x. x - a$i"] *(2) by(auto)
himmelma@35172
  1392
qed
himmelma@35172
  1393
himmelma@35172
  1394
lemma division_split_left_inj:
himmelma@35172
  1395
  assumes "d division_of i" "k1 \<in> d" "k2 \<in> d"  "k1 \<noteq> k2"
himmelma@35172
  1396
  "k1 \<inter> {x::real^'n. x$k \<le> c} = k2 \<inter> {x. x$k \<le> c}"
himmelma@35172
  1397
  shows "content(k1 \<inter> {x. x$k \<le> c}) = 0"
himmelma@35172
  1398
proof- note d=division_ofD[OF assms(1)]
himmelma@35172
  1399
  have *:"\<And>a b::real^'n. \<And> c k. (content({a..b} \<inter> {x. x$k \<le> c}) = 0 \<longleftrightarrow> interior({a..b} \<inter> {x. x$k \<le> c}) = {})"
himmelma@35172
  1400
    unfolding interval_split content_eq_0_interior by auto
himmelma@35172
  1401
  guess u1 v1 using d(4)[OF assms(2)] apply-by(erule exE)+ note uv1=this
himmelma@35172
  1402
  guess u2 v2 using d(4)[OF assms(3)] apply-by(erule exE)+ note uv2=this
himmelma@35172
  1403
  have **:"\<And>s t u. s \<inter> t = {} \<Longrightarrow> u \<subseteq> s \<Longrightarrow> u \<subseteq> t \<Longrightarrow> u = {}" by auto
himmelma@35172
  1404
  show ?thesis unfolding uv1 uv2 * apply(rule **[OF d(5)[OF assms(2-4)]])
himmelma@35172
  1405
    defer apply(subst assms(5)[unfolded uv1 uv2]) unfolding uv1 uv2 by auto qed
himmelma@35172
  1406
himmelma@35172
  1407
lemma division_split_right_inj:
himmelma@35172
  1408
  assumes "d division_of i" "k1 \<in> d" "k2 \<in> d"  "k1 \<noteq> k2"
himmelma@35172
  1409
  "k1 \<inter> {x::real^'n. x$k \<ge> c} = k2 \<inter> {x. x$k \<ge> c}"
himmelma@35172
  1410
  shows "content(k1 \<inter> {x. x$k \<ge> c}) = 0"
himmelma@35172
  1411
proof- note d=division_ofD[OF assms(1)]
himmelma@35172
  1412
  have *:"\<And>a b::real^'n. \<And> c k. (content({a..b} \<inter> {x. x$k >= c}) = 0 \<longleftrightarrow> interior({a..b} \<inter> {x. x$k >= c}) = {})"
himmelma@35172
  1413
    unfolding interval_split content_eq_0_interior by auto
himmelma@35172
  1414
  guess u1 v1 using d(4)[OF assms(2)] apply-by(erule exE)+ note uv1=this
himmelma@35172
  1415
  guess u2 v2 using d(4)[OF assms(3)] apply-by(erule exE)+ note uv2=this
himmelma@35172
  1416
  have **:"\<And>s t u. s \<inter> t = {} \<Longrightarrow> u \<subseteq> s \<Longrightarrow> u \<subseteq> t \<Longrightarrow> u = {}" by auto
himmelma@35172
  1417
  show ?thesis unfolding uv1 uv2 * apply(rule **[OF d(5)[OF assms(2-4)]])
himmelma@35172
  1418
    defer apply(subst assms(5)[unfolded uv1 uv2]) unfolding uv1 uv2 by auto qed
himmelma@35172
  1419
himmelma@35172
  1420
lemma tagged_division_split_left_inj:
himmelma@35172
  1421
  assumes "d tagged_division_of i" "(x1,k1) \<in> d" "(x2,k2) \<in> d" "k1 \<noteq> k2"  "k1 \<inter> {x. x$k \<le> c} = k2 \<inter> {x. x$k \<le> c}" 
himmelma@35172
  1422
  shows "content(k1 \<inter> {x. x$k \<le> c}) = 0"
himmelma@35172
  1423
proof- have *:"\<And>a b c. (a,b) \<in> c \<Longrightarrow> b \<in> snd ` c" unfolding image_iff apply(rule_tac x="(a,b)" in bexI) by auto
himmelma@35172
  1424
  show ?thesis apply(rule division_split_left_inj[OF division_of_tagged_division[OF assms(1)]])
himmelma@35172
  1425
    apply(rule_tac[1-2] *) using assms(2-) by auto qed
himmelma@35172
  1426
himmelma@35172
  1427
lemma tagged_division_split_right_inj:
himmelma@35172
  1428
  assumes "d tagged_division_of i" "(x1,k1) \<in> d" "(x2,k2) \<in> d" "k1 \<noteq> k2"  "k1 \<inter> {x. x$k \<ge> c} = k2 \<inter> {x. x$k \<ge> c}" 
himmelma@35172
  1429
  shows "content(k1 \<inter> {x. x$k \<ge> c}) = 0"
himmelma@35172
  1430
proof- have *:"\<And>a b c. (a,b) \<in> c \<Longrightarrow> b \<in> snd ` c" unfolding image_iff apply(rule_tac x="(a,b)" in bexI) by auto
himmelma@35172
  1431
  show ?thesis apply(rule division_split_right_inj[OF division_of_tagged_division[OF assms(1)]])
himmelma@35172
  1432
    apply(rule_tac[1-2] *) using assms(2-) by auto qed
himmelma@35172
  1433
himmelma@35172
  1434
lemma division_split:
himmelma@35172
  1435
  assumes "p division_of {a..b::real^'n}"
himmelma@35172
  1436
  shows "{l \<inter> {x. x$k \<le> c} | l. l \<in> p \<and> ~(l \<inter> {x. x$k \<le> c} = {})} division_of ({a..b} \<inter> {x. x$k \<le> c})" (is "?p1 division_of ?I1") and 
himmelma@35172
  1437
        "{l \<inter> {x. x$k \<ge> c} | l. l \<in> p \<and> ~(l \<inter> {x. x$k \<ge> c} = {})} division_of ({a..b} \<inter> {x. x$k \<ge> c})" (is "?p2 division_of ?I2")
himmelma@35172
  1438
proof(rule_tac[!] division_ofI) note p=division_ofD[OF assms]
himmelma@35172
  1439
  show "finite ?p1" "finite ?p2" using p(1) by auto show "\<Union>?p1 = ?I1" "\<Union>?p2 = ?I2" unfolding p(6)[THEN sym] by auto
himmelma@35172
  1440
  { fix k assume "k\<in>?p1" then guess l unfolding mem_Collect_eq apply-by(erule exE,(erule conjE)+) note l=this
himmelma@35172
  1441
    guess u v using p(4)[OF l(2)] apply-by(erule exE)+ note uv=this
himmelma@35172
  1442
    show "k\<subseteq>?I1" "k \<noteq> {}" "\<exists>a b. k = {a..b}" unfolding l
himmelma@35172
  1443
      using p(2-3)[OF l(2)] l(3) unfolding uv apply- prefer 3 apply(subst interval_split) by auto
himmelma@35172
  1444
    fix k' assume "k'\<in>?p1" then guess l' unfolding mem_Collect_eq apply-by(erule exE,(erule conjE)+) note l'=this
himmelma@35172
  1445
    assume "k\<noteq>k'" thus "interior k \<inter> interior k' = {}" unfolding l l' using p(5)[OF l(2) l'(2)] by auto }
himmelma@35172
  1446
  { fix k assume "k\<in>?p2" then guess l unfolding mem_Collect_eq apply-by(erule exE,(erule conjE)+) note l=this
himmelma@35172
  1447
    guess u v using p(4)[OF l(2)] apply-by(erule exE)+ note uv=this
himmelma@35172
  1448
    show "k\<subseteq>?I2" "k \<noteq> {}" "\<exists>a b. k = {a..b}" unfolding l
himmelma@35172
  1449
      using p(2-3)[OF l(2)] l(3) unfolding uv apply- prefer 3 apply(subst interval_split) by auto
himmelma@35172
  1450
    fix k' assume "k'\<in>?p2" then guess l' unfolding mem_Collect_eq apply-by(erule exE,(erule conjE)+) note l'=this
himmelma@35172
  1451
    assume "k\<noteq>k'" thus "interior k \<inter> interior k' = {}" unfolding l l' using p(5)[OF l(2) l'(2)] by auto }
himmelma@35172
  1452
qed
himmelma@35172
  1453
himmelma@35172
  1454
lemma has_integral_split: fixes f::"real^'n \<Rightarrow> 'a::real_normed_vector"
himmelma@35172
  1455
  assumes "(f has_integral i) ({a..b} \<inter> {x. x$k \<le> c})"  "(f has_integral j) ({a..b} \<inter> {x. x$k \<ge> c})"
himmelma@35172
  1456
  shows "(f has_integral (i + j)) ({a..b})"
himmelma@35172
  1457
proof(unfold has_integral,rule,rule) case goal1 hence e:"e/2>0" by auto
himmelma@35172
  1458
  guess d1 using has_integralD[OF assms(1)[unfolded interval_split] e] . note d1=this[unfolded interval_split[THEN sym]]
himmelma@35172
  1459
  guess d2 using has_integralD[OF assms(2)[unfolded interval_split] e] . note d2=this[unfolded interval_split[THEN sym]]
himmelma@35172
  1460
  let ?d = "\<lambda>x. if x$k = c then (d1 x \<inter> d2 x) else ball x (abs(x$k - c)) \<inter> d1 x \<inter> d2 x"
himmelma@35172
  1461
  show ?case apply(rule_tac x="?d" in exI,rule) defer apply(rule,rule,(erule conjE)+)
himmelma@35172
  1462
  proof- show "gauge ?d" using d1(1) d2(1) unfolding gauge_def by auto
himmelma@35172
  1463
    fix p assume "p tagged_division_of {a..b}" "?d fine p" note p = this tagged_division_ofD[OF this(1)]
himmelma@35172
  1464
    have lem0:"\<And>x kk. (x,kk) \<in> p \<Longrightarrow> ~(kk \<inter> {x. x$k \<le> c} = {}) \<Longrightarrow> x$k \<le> c"
himmelma@35172
  1465
         "\<And>x kk. (x,kk) \<in> p \<Longrightarrow> ~(kk \<inter> {x. x$k \<ge> c} = {}) \<Longrightarrow> x$k \<ge> c"
himmelma@35172
  1466
    proof- fix x kk assume as:"(x,kk)\<in>p"
himmelma@35172
  1467
      show "~(kk \<inter> {x. x$k \<le> c} = {}) \<Longrightarrow> x$k \<le> c"
himmelma@35172
  1468
      proof(rule ccontr) case goal1
himmelma@35172
  1469
        from this(2)[unfolded not_le] have "kk \<subseteq> ball x \<bar>x $ k - c\<bar>"
himmelma@35172
  1470
          using p(2)[unfolded fine_def,rule_format,OF as,unfolded split_conv] by auto
himmelma@35172
  1471
        hence "\<exists>y. y \<in> ball x \<bar>x $ k - c\<bar> \<inter> {x. x $ k \<le> c}" using goal1(1) by blast 
himmelma@35172
  1472
        then guess y .. hence "\<bar>x $ k - y $ k\<bar> < \<bar>x $ k - c\<bar>" "y$k \<le> c" apply-apply(rule le_less_trans)
himmelma@35172
  1473
          using component_le_norm[of "x - y" k,unfolded vector_minus_component] by(auto simp add:vector_dist_norm)
himmelma@35172
  1474
        thus False using goal1(2)[unfolded not_le] by(auto simp add:field_simps)
himmelma@35172
  1475
      qed
himmelma@35172
  1476
      show "~(kk \<inter> {x. x$k \<ge> c} = {}) \<Longrightarrow> x$k \<ge> c"
himmelma@35172
  1477
      proof(rule ccontr) case goal1
himmelma@35172
  1478
        from this(2)[unfolded not_le] have "kk \<subseteq> ball x \<bar>x $ k - c\<bar>"
himmelma@35172
  1479
          using p(2)[unfolded fine_def,rule_format,OF as,unfolded split_conv] by auto
himmelma@35172
  1480
        hence "\<exists>y. y \<in> ball x \<bar>x $ k - c\<bar> \<inter> {x. x $ k \<ge> c}" using goal1(1) by blast 
himmelma@35172
  1481
        then guess y .. hence "\<bar>x $ k - y $ k\<bar> < \<bar>x $ k - c\<bar>" "y$k \<ge> c" apply-apply(rule le_less_trans)
himmelma@35172
  1482
          using component_le_norm[of "x - y" k,unfolded vector_minus_component] by(auto simp add:vector_dist_norm)
himmelma@35172
  1483
        thus False using goal1(2)[unfolded not_le] by(auto simp add:field_simps)
himmelma@35172
  1484
      qed
himmelma@35172
  1485
    qed
himmelma@35172
  1486
himmelma@35172
  1487
    have lem1: "\<And>f P Q. (\<forall>x k. (x,k) \<in> {(x,f k) | x k. P x k} \<longrightarrow> Q x k) \<longleftrightarrow> (\<forall>x k. P x k \<longrightarrow> Q x (f k))" by auto
himmelma@35172
  1488
    have lem2: "\<And>f s P f. finite s \<Longrightarrow> finite {(x,f k) | x k. (x,k) \<in> s \<and> P x k}"
himmelma@35172
  1489
    proof- case goal1 thus ?case apply-apply(rule finite_subset[of _ "(\<lambda>(x,k). (x,f k)) ` s"]) by auto qed
himmelma@35172
  1490
    have lem3: "\<And>g::(real ^ 'n \<Rightarrow> bool) \<Rightarrow> real ^ 'n \<Rightarrow> bool. finite p \<Longrightarrow>
himmelma@35172
  1491
      setsum (\<lambda>(x,k). content k *\<^sub>R f x) {(x,g k) |x k. (x,k) \<in> p \<and> ~(g k = {})}
himmelma@35172
  1492
               = setsum (\<lambda>(x,k). content k *\<^sub>R f x) ((\<lambda>(x,k). (x,g k)) ` p)"
himmelma@35172
  1493
      apply(rule setsum_mono_zero_left) prefer 3
himmelma@35172
  1494
    proof fix g::"(real ^ 'n \<Rightarrow> bool) \<Rightarrow> real ^ 'n \<Rightarrow> bool" and i::"(real^'n) \<times> ((real^'n) set)"
himmelma@35172
  1495
      assume "i \<in> (\<lambda>(x, k). (x, g k)) ` p - {(x, g k) |x k. (x, k) \<in> p \<and> g k \<noteq> {}}"
himmelma@35172
  1496
      then obtain x k where xk:"i=(x,g k)" "(x,k)\<in>p" "(x,g k) \<notin> {(x, g k) |x k. (x, k) \<in> p \<and> g k \<noteq> {}}" by auto
himmelma@35172
  1497
      have "content (g k) = 0" using xk using content_empty by auto
himmelma@35172
  1498
      thus "(\<lambda>(x, k). content k *\<^sub>R f x) i = 0" unfolding xk split_conv by auto
himmelma@35172
  1499
    qed auto
himmelma@35172
  1500
    have lem4:"\<And>g. (\<lambda>(x,l). content (g l) *\<^sub>R f x) = (\<lambda>(x,l). content l *\<^sub>R f x) o (\<lambda>(x,l). (x,g l))" apply(rule ext) by auto
himmelma@35172
  1501
himmelma@35172
  1502
    let ?M1 = "{(x,kk \<inter> {x. x$k \<le> c}) |x kk. (x,kk) \<in> p \<and> kk \<inter> {x. x$k \<le> c} \<noteq> {}}"
himmelma@35172
  1503
    have "norm ((\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) - i) < e/2" apply(rule d1(2),rule tagged_division_ofI)
himmelma@35172
  1504
      apply(rule lem2 p(3))+ prefer 6 apply(rule fineI)
himmelma@35172
  1505
    proof- show "\<Union>{k. \<exists>x. (x, k) \<in> ?M1} = {a..b} \<inter> {x. x$k \<le> c}" unfolding p(8)[THEN sym] by auto
himmelma@35172
  1506
      fix x l assume xl:"(x,l)\<in>?M1"
himmelma@35172
  1507
      then guess x' l' unfolding mem_Collect_eq apply- unfolding Pair_eq apply((erule exE)+,(erule conjE)+) .  note xl'=this
himmelma@35172
  1508
      have "l' \<subseteq> d1 x'" apply(rule order_trans[OF fineD[OF p(2) xl'(3)]]) by auto
himmelma@35172
  1509
      thus "l \<subseteq> d1 x" unfolding xl' by auto
himmelma@35172
  1510
      show "x\<in>l" "l \<subseteq> {a..b} \<inter> {x. x $ k \<le> c}" unfolding xl' using p(4-6)[OF xl'(3)] using xl'(4)
himmelma@35172
  1511
        using lem0(1)[OF xl'(3-4)] by auto
himmelma@35172
  1512
      show  "\<exists>a b. l = {a..b}" unfolding xl' using p(6)[OF xl'(3)] by(fastsimp simp add: interval_split[where c=c and k=k])
himmelma@35172
  1513
      fix y r let ?goal = "interior l \<inter> interior r = {}" assume yr:"(y,r)\<in>?M1"
himmelma@35172
  1514
      then guess y' r' unfolding mem_Collect_eq apply- unfolding Pair_eq apply((erule exE)+,(erule conjE)+) .  note yr'=this
himmelma@35172
  1515
      assume as:"(x,l) \<noteq> (y,r)" show "interior l \<inter> interior r = {}"
himmelma@35172
  1516
      proof(cases "l' = r' \<longrightarrow> x' = y'")
himmelma@35172
  1517
        case False thus ?thesis using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
himmelma@35172
  1518
      next case True hence "l' \<noteq> r'" using as unfolding xl' yr' by auto
himmelma@35172
  1519
        thus ?thesis using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
himmelma@35172
  1520
      qed qed moreover
himmelma@35172
  1521
himmelma@35172
  1522
    let ?M2 = "{(x,kk \<inter> {x. x$k \<ge> c}) |x kk. (x,kk) \<in> p \<and> kk \<inter> {x. x$k \<ge> c} \<noteq> {}}" 
himmelma@35172
  1523
    have "norm ((\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - j) < e/2" apply(rule d2(2),rule tagged_division_ofI)
himmelma@35172
  1524
      apply(rule lem2 p(3))+ prefer 6 apply(rule fineI)
himmelma@35172
  1525
    proof- show "\<Union>{k. \<exists>x. (x, k) \<in> ?M2} = {a..b} \<inter> {x. x$k \<ge> c}" unfolding p(8)[THEN sym] by auto
himmelma@35172
  1526
      fix x l assume xl:"(x,l)\<in>?M2"
himmelma@35172
  1527
      then guess x' l' unfolding mem_Collect_eq apply- unfolding Pair_eq apply((erule exE)+,(erule conjE)+) .  note xl'=this
himmelma@35172
  1528
      have "l' \<subseteq> d2 x'" apply(rule order_trans[OF fineD[OF p(2) xl'(3)]]) by auto
himmelma@35172
  1529
      thus "l \<subseteq> d2 x" unfolding xl' by auto
himmelma@35172
  1530
      show "x\<in>l" "l \<subseteq> {a..b} \<inter> {x. x $ k \<ge> c}" unfolding xl' using p(4-6)[OF xl'(3)] using xl'(4)
himmelma@35172
  1531
        using lem0(2)[OF xl'(3-4)] by auto
himmelma@35172
  1532
      show  "\<exists>a b. l = {a..b}" unfolding xl' using p(6)[OF xl'(3)] by(fastsimp simp add: interval_split[where c=c and k=k])
himmelma@35172
  1533
      fix y r let ?goal = "interior l \<inter> interior r = {}" assume yr:"(y,r)\<in>?M2"
himmelma@35172
  1534
      then guess y' r' unfolding mem_Collect_eq apply- unfolding Pair_eq apply((erule exE)+,(erule conjE)+) .  note yr'=this
himmelma@35172
  1535
      assume as:"(x,l) \<noteq> (y,r)" show "interior l \<inter> interior r = {}"
himmelma@35172
  1536
      proof(cases "l' = r' \<longrightarrow> x' = y'")
himmelma@35172
  1537
        case False thus ?thesis using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
himmelma@35172
  1538
      next case True hence "l' \<noteq> r'" using as unfolding xl' yr' by auto
himmelma@35172
  1539
        thus ?thesis using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
himmelma@35172
  1540
      qed qed ultimately
himmelma@35172
  1541
himmelma@35172
  1542
    have "norm (((\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) - i) + ((\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - j)) < e/2 + e/2"
himmelma@35172
  1543
      apply- apply(rule norm_triangle_lt) by auto
himmelma@35172
  1544
    also { have *:"\<And>x y. x = (0::real) \<Longrightarrow> x *\<^sub>R (y::'a) = 0" using scaleR_zero_left by auto
himmelma@35172
  1545
      have "((\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) - i) + ((\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - j)
himmelma@35172
  1546
       = (\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - (i + j)" by auto
himmelma@35172
  1547
      also have "\<dots> = (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. x $ k \<le> c}) *\<^sub>R f x) + (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. c \<le> x $ k}) *\<^sub>R f x) - (i + j)"
himmelma@35172
  1548
        unfolding lem3[OF p(3)] apply(subst setsum_reindex_nonzero[OF p(3)]) defer apply(subst setsum_reindex_nonzero[OF p(3)])
himmelma@35172
  1549
        defer unfolding lem4[THEN sym] apply(rule refl) unfolding split_paired_all split_conv apply(rule_tac[!] *)
himmelma@35172
  1550
      proof- case goal1 thus ?case apply- apply(rule tagged_division_split_left_inj [OF p(1), of a b aa ba]) by auto
himmelma@35172
  1551
      next case   goal2 thus ?case apply- apply(rule tagged_division_split_right_inj[OF p(1), of a b aa ba]) by auto
himmelma@35172
  1552
      qed also note setsum_addf[THEN sym]
himmelma@35172
  1553
      also have *:"\<And>x. x\<in>p \<Longrightarrow> (\<lambda>(x, ka). content (ka \<inter> {x. x $ k \<le> c}) *\<^sub>R f x) x + (\<lambda>(x, ka). content (ka \<inter> {x. c \<le> x $ k}) *\<^sub>R f x) x
himmelma@35172
  1554
        = (\<lambda>(x,ka). content ka *\<^sub>R f x) x" unfolding split_paired_all split_conv
himmelma@35172
  1555
      proof- fix a b assume "(a,b) \<in> p" from p(6)[OF this] guess u v apply-by(erule exE)+ note uv=this
himmelma@35172
  1556
        thus "content (b \<inter> {x. x $ k \<le> c}) *\<^sub>R f a + content (b \<inter> {x. c \<le> x $ k}) *\<^sub>R f a = content b *\<^sub>R f a"
himmelma@35172
  1557
          unfolding scaleR_left_distrib[THEN sym] unfolding uv content_split[of u v k c] by auto
himmelma@35172
  1558
      qed note setsum_cong2[OF this]
himmelma@35172
  1559
      finally have "(\<Sum>(x, k)\<in>{(x, kk \<inter> {x. x $ k \<le> c}) |x kk. (x, kk) \<in> p \<and> kk \<inter> {x. x $ k \<le> c} \<noteq> {}}. content k *\<^sub>R f x) - i +
himmelma@35172
  1560
        ((\<Sum>(x, k)\<in>{(x, kk \<inter> {x. c \<le> x $ k}) |x kk. (x, kk) \<in> p \<and> kk \<inter> {x. c \<le> x $ k} \<noteq> {}}. content k *\<^sub>R f x) - j) =
himmelma@35172
  1561
        (\<Sum>(x, ka)\<in>p. content ka *\<^sub>R f x) - (i + j)" by auto }
himmelma@35172
  1562
    finally show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - (i + j)) < e" by auto qed qed
himmelma@35172
  1563
himmelma@35172
  1564
subsection {* A sort of converse, integrability on subintervals. *}
himmelma@35172
  1565
himmelma@35172
  1566
lemma tagged_division_union_interval:
himmelma@35172
  1567
  assumes "p1 tagged_division_of ({a..b} \<inter> {x::real^'n. x$k \<le> (c::real)})"  "p2 tagged_division_of ({a..b} \<inter> {x. x$k \<ge> c})"
himmelma@35172
  1568
  shows "(p1 \<union> p2) tagged_division_of ({a..b})"
himmelma@35172
  1569
proof- have *:"{a..b} = ({a..b} \<inter> {x. x$k \<le> c}) \<union> ({a..b} \<inter> {x. x$k \<ge> c})" by auto
himmelma@35172
  1570
  show ?thesis apply(subst *) apply(rule tagged_division_union[OF assms])
himmelma@35172
  1571
    unfolding interval_split interior_closed_interval
himmelma@35172
  1572
    by(auto simp add: vector_less_def Cart_lambda_beta elim!:allE[where x=k]) qed
himmelma@35172
  1573
himmelma@35172
  1574
lemma has_integral_separate_sides: fixes f::"real^'m \<Rightarrow> 'a::real_normed_vector"
himmelma@35172
  1575
  assumes "(f has_integral i) ({a..b})" "e>0"
himmelma@35172
  1576
  obtains d where "gauge d" "(\<forall>p1 p2. p1 tagged_division_of ({a..b} \<inter> {x. x$k \<le> c}) \<and> d fine p1 \<and>
himmelma@35172
  1577
                                p2 tagged_division_of ({a..b} \<inter> {x. x$k \<ge> c}) \<and> d fine p2
himmelma@35172
  1578
                                \<longrightarrow> norm((setsum (\<lambda>(x,k). content k *\<^sub>R f x) p1 +
himmelma@35172
  1579
                                          setsum (\<lambda>(x,k). content k *\<^sub>R f x) p2) - i) < e)"
himmelma@35172
  1580
proof- guess d using has_integralD[OF assms] . note d=this
himmelma@35172
  1581
  show ?thesis apply(rule that[of d]) apply(rule d) apply(rule,rule,rule,(erule conjE)+)
himmelma@35172
  1582
  proof- fix p1 p2 assume "p1 tagged_division_of {a..b} \<inter> {x. x $ k \<le> c}" "d fine p1" note p1=tagged_division_ofD[OF this(1)] this
himmelma@35172
  1583
                   assume "p2 tagged_division_of {a..b} \<inter> {x. c \<le> x $ k}" "d fine p2" note p2=tagged_division_ofD[OF this(1)] this
himmelma@35172
  1584
    note tagged_division_union_interval[OF p1(7) p2(7)] note p12 = tagged_division_ofD[OF this] this
himmelma@35172
  1585
    have "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x) - i) = norm ((\<Sum>(x, k)\<in>p1 \<union> p2. content k *\<^sub>R f x) - i)"
himmelma@35172
  1586
      apply(subst setsum_Un_zero) apply(rule p1 p2)+ apply(rule) unfolding split_paired_all split_conv
himmelma@35172
  1587
    proof- fix a b assume ab:"(a,b) \<in> p1 \<inter> p2"
himmelma@35172
  1588
      have "(a,b) \<in> p1" using ab by auto from p1(4)[OF this] guess u v apply-by(erule exE)+ note uv =this
himmelma@35172
  1589
      have "b \<subseteq> {x. x$k = c}" using ab p1(3)[of a b] p2(3)[of a b] by fastsimp
himmelma@35172
  1590
      moreover have "interior {x. x $ k = c} = {}" 
himmelma@35172
  1591
      proof(rule ccontr) case goal1 then obtain x where x:"x\<in>interior {x. x$k = c}" by auto
himmelma@35172
  1592
        then guess e unfolding mem_interior .. note e=this
himmelma@35172
  1593
        have x:"x$k = c" using x interior_subset by fastsimp
himmelma@35172
  1594
        have *:"\<And>i. \<bar>(x - (x + (\<chi> i. if i = k then e / 2 else 0))) $ i\<bar> = (if i = k then e/2 else 0)" using e by auto
himmelma@35172
  1595
        have "x + (\<chi> i. if i = k then e/2 else 0) \<in> ball x e" unfolding mem_ball vector_dist_norm 
himmelma@35172
  1596
          apply(rule le_less_trans[OF norm_le_l1]) unfolding * 
himmelma@35172
  1597
          unfolding setsum_delta[OF finite_UNIV] using e by auto 
himmelma@35172
  1598
        hence "x + (\<chi> i. if i = k then e/2 else 0) \<in> {x. x$k = c}" using e by auto
himmelma@35172
  1599
        thus False unfolding mem_Collect_eq using e x by auto
himmelma@35172
  1600
      qed ultimately have "content b = 0" unfolding uv content_eq_0_interior apply-apply(drule subset_interior) by auto
himmelma@35172
  1601
      thus "content b *\<^sub>R f a = 0" by auto
himmelma@35172
  1602
    qed auto
himmelma@35172
  1603
    also have "\<dots> < e" by(rule d(2) p12 fine_union p1 p2)+
himmelma@35172
  1604
    finally show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x) - i) < e" . qed qed
himmelma@35172
  1605
himmelma@35172
  1606
lemma integrable_split[intro]: fixes f::"real^'n \<Rightarrow> 'a::{real_normed_vector,complete_space}" assumes "f integrable_on {a..b}"
himmelma@35172
  1607
  shows "f integrable_on ({a..b} \<inter> {x. x$k \<le> c})" (is ?t1) and "f integrable_on ({a..b} \<inter> {x. x$k \<ge> c})" (is ?t2) 
himmelma@35172
  1608
proof- guess y using assms unfolding integrable_on_def .. note y=this
himmelma@35172
  1609
  def b' \<equiv> "(\<chi> i. if i = k then min (b$k) c else b$i)::real^'n"
himmelma@35172
  1610
  and a' \<equiv> "(\<chi> i. if i = k then max (a$k) c else a$i)::real^'n"
himmelma@35172
  1611
  show ?t1 ?t2 unfolding interval_split integrable_cauchy unfolding interval_split[THEN sym]
himmelma@35172
  1612
  proof(rule_tac[!] allI impI)+ fix e::real assume "e>0" hence "e/2>0" by auto
himmelma@35172
  1613
    from has_integral_separate_sides[OF y this,of k c] guess d . note d=this[rule_format]
himmelma@35172
  1614
    let ?P = "\<lambda>A. \<exists>d. gauge d \<and> (\<forall>p1 p2. p1 tagged_division_of {a..b} \<inter> A \<and> d fine p1 \<and> p2 tagged_division_of {a..b} \<inter> A \<and> d fine p2 \<longrightarrow>
himmelma@35172
  1615
                              norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e)"
himmelma@35172
  1616
    show "?P {x. x $ k \<le> c}" apply(rule_tac x=d in exI) apply(rule,rule d) apply(rule,rule,rule)
himmelma@35172
  1617
    proof- fix p1 p2 assume as:"p1 tagged_division_of {a..b} \<inter> {x. x $ k \<le> c} \<and> d fine p1 \<and> p2 tagged_division_of {a..b} \<inter> {x. x $ k \<le> c} \<and> d fine p2"
himmelma@35172
  1618
      show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e"
himmelma@35172
  1619
      proof- guess p using fine_division_exists[OF d(1), of a' b] . note p=this
himmelma@35172
  1620
        show ?thesis using norm_triangle_half_l[OF d(2)[of p1 p] d(2)[of p2 p]]
himmelma@35172
  1621
          using as unfolding interval_split b'_def[symmetric] a'_def[symmetric]
himmelma@35172
  1622
          using p using assms by(auto simp add:group_simps)
himmelma@35172
  1623
      qed qed  
himmelma@35172
  1624
    show "?P {x. x $ k \<ge> c}" apply(rule_tac x=d in exI) apply(rule,rule d) apply(rule,rule,rule)
himmelma@35172
  1625
    proof- fix p1 p2 assume as:"p1 tagged_division_of {a..b} \<inter> {x. x $ k \<ge> c} \<and> d fine p1 \<and> p2 tagged_division_of {a..b} \<inter> {x. x $ k \<ge> c} \<and> d fine p2"
himmelma@35172
  1626
      show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e"
himmelma@35172
  1627
      proof- guess p using fine_division_exists[OF d(1), of a b'] . note p=this
himmelma@35172
  1628
        show ?thesis using norm_triangle_half_l[OF d(2)[of p p1] d(2)[of p p2]]
himmelma@35172
  1629
          using as unfolding interval_split b'_def[symmetric] a'_def[symmetric]
himmelma@35172
  1630
          using p using assms by(auto simp add:group_simps) qed qed qed qed
himmelma@35172
  1631
himmelma@35172
  1632
subsection {* Generalized notion of additivity. *}
himmelma@35172
  1633
himmelma@35172
  1634
definition "neutral opp = (SOME x. \<forall>y. opp x y = y \<and> opp y x = y)"
himmelma@35172
  1635
himmelma@35172
  1636
definition operative :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ((real^'n) set \<Rightarrow> 'a) \<Rightarrow> bool" where
himmelma@35172
  1637
  "operative opp f \<equiv> 
himmelma@35172
  1638
    (\<forall>a b. content {a..b} = 0 \<longrightarrow> f {a..b} = neutral(opp)) \<and>
himmelma@35172
  1639
    (\<forall>a b c k. f({a..b}) =
himmelma@35172
  1640
                   opp (f({a..b} \<inter> {x. x$k \<le> c}))
himmelma@35172
  1641
                       (f({a..b} \<inter> {x. x$k \<ge> c})))"
himmelma@35172
  1642
himmelma@35172
  1643
lemma operativeD[dest]: assumes "operative opp f"
himmelma@35172
  1644
  shows "\<And>a b. content {a..b} = 0 \<Longrightarrow> f {a..b} = neutral(opp)"
himmelma@35172
  1645
  "\<And>a b c k. f({a..b}) = opp (f({a..b} \<inter> {x. x$k \<le> c})) (f({a..b} \<inter> {x. x$k \<ge> c}))"
himmelma@35172
  1646
  using assms unfolding operative_def by auto
himmelma@35172
  1647
himmelma@35172
  1648
lemma operative_trivial:
himmelma@35172
  1649
 "operative opp f \<Longrightarrow> content({a..b}) = 0 \<Longrightarrow> f({a..b}) = neutral opp"
himmelma@35172
  1650
  unfolding operative_def by auto
himmelma@35172
  1651
himmelma@35172
  1652
lemma property_empty_interval:
himmelma@35172
  1653
 "(\<forall>a b. content({a..b}) = 0 \<longrightarrow> P({a..b})) \<Longrightarrow> P {}" 
himmelma@35172
  1654
  using content_empty unfolding empty_as_interval by auto
himmelma@35172
  1655
himmelma@35172
  1656
lemma operative_empty: "operative opp f \<Longrightarrow> f {} = neutral opp"
himmelma@35172
  1657
  unfolding operative_def apply(rule property_empty_interval) by auto
himmelma@35172
  1658
himmelma@35172
  1659
subsection {* Using additivity of lifted function to encode definedness. *}
himmelma@35172
  1660
himmelma@35172
  1661
lemma forall_option: "(\<forall>x. P x) \<longleftrightarrow> P None \<and> (\<forall>x. P(Some x))"
himmelma@35172
  1662
  by (metis map_of.simps option.nchotomy)
himmelma@35172
  1663
himmelma@35172
  1664
lemma exists_option:
himmelma@35172
  1665
 "(\<exists>x. P x) \<longleftrightarrow> P None \<or> (\<exists>x. P(Some x))" 
himmelma@35172
  1666
  by (metis map_of.simps option.nchotomy)
himmelma@35172
  1667
himmelma@35172
  1668
fun lifted where 
himmelma@35172
  1669
  "lifted (opp::'a\<Rightarrow>'a\<Rightarrow>'b) (Some x) (Some y) = Some(opp x y)" |
himmelma@35172
  1670
  "lifted opp None _ = (None::'b option)" |
himmelma@35172
  1671
  "lifted opp _ None = None"
himmelma@35172
  1672
himmelma@35172
  1673
lemma lifted_simp_1[simp]: "lifted opp v None = None"
himmelma@35172
  1674
  apply(induct v) by auto
himmelma@35172
  1675
himmelma@35172
  1676
definition "monoidal opp \<equiv>  (\<forall>x y. opp x y = opp y x) \<and>
himmelma@35172
  1677
                   (\<forall>x y z. opp x (opp y z) = opp (opp x y) z) \<and>
himmelma@35172
  1678
                   (\<forall>x. opp (neutral opp) x = x)"
himmelma@35172
  1679
himmelma@35172
  1680
lemma monoidalI: assumes "\<And>x y. opp x y = opp y x"
himmelma@35172
  1681
  "\<And>x y z. opp x (opp y z) = opp (opp x y) z"
himmelma@35172
  1682
  "\<And>x. opp (neutral opp) x = x" shows "monoidal opp"
himmelma@35172
  1683
  unfolding monoidal_def using assms by fastsimp
himmelma@35172
  1684
himmelma@35172
  1685
lemma monoidal_ac: assumes "monoidal opp"
himmelma@35172
  1686
  shows "opp (neutral opp) a = a" "opp a (neutral opp) = a" "opp a b = opp b a"
himmelma@35172
  1687
  "opp (opp a b) c = opp a (opp b c)"  "opp a (opp b c) = opp b (opp a c)"
himmelma@35172
  1688
  using assms unfolding monoidal_def apply- by metis+
himmelma@35172
  1689
himmelma@35172
  1690
lemma monoidal_simps[simp]: assumes "monoidal opp"
himmelma@35172
  1691
  shows "opp (neutral opp) a = a" "opp a (neutral opp) = a"
himmelma@35172
  1692
  using monoidal_ac[OF assms] by auto
himmelma@35172
  1693
himmelma@35172
  1694
lemma neutral_lifted[cong]: assumes "monoidal opp"
himmelma@35172
  1695
  shows "neutral (lifted opp) = Some(neutral opp)"
himmelma@35172
  1696
  apply(subst neutral_def) apply(rule some_equality) apply(rule,induct_tac y) prefer 3
himmelma@35172
  1697
proof- fix x assume "\<forall>y. lifted opp x y = y \<and> lifted opp y x = y"
himmelma@35172
  1698
  thus "x = Some (neutral opp)" apply(induct x) defer
himmelma@35172
  1699
    apply rule apply(subst neutral_def) apply(subst eq_commute,rule some_equality)
himmelma@35172
  1700
    apply(rule,erule_tac x="Some y" in allE) defer apply(erule_tac x="Some x" in allE) by auto
himmelma@35172
  1701
qed(auto simp add:monoidal_ac[OF assms])
himmelma@35172
  1702
himmelma@35172
  1703
lemma monoidal_lifted[intro]: assumes "monoidal opp" shows "monoidal(lifted opp)"
himmelma@35172
  1704
  unfolding monoidal_def forall_option neutral_lifted[OF assms] using monoidal_ac[OF assms] by auto
himmelma@35172
  1705
himmelma@35172
  1706
definition "support opp f s = {x. x\<in>s \<and> f x \<noteq> neutral opp}"
himmelma@35172
  1707
definition "fold' opp e s \<equiv> (if finite s then fold opp e s else e)"
himmelma@35172
  1708
definition "iterate opp s f \<equiv> fold' (\<lambda>x a. opp (f x) a) (neutral opp) (support opp f s)"
himmelma@35172
  1709
himmelma@35172
  1710
lemma support_subset[intro]:"support opp f s \<subseteq> s" unfolding support_def by auto
himmelma@35172
  1711
lemma support_empty[simp]:"support opp f {} = {}" using support_subset[of opp f "{}"] by auto
himmelma@35172
  1712
himmelma@35172
  1713
lemma fun_left_comm_monoidal[intro]: assumes "monoidal opp" shows "fun_left_comm opp"
himmelma@35172
  1714
  unfolding fun_left_comm_def using monoidal_ac[OF assms] by auto
himmelma@35172
  1715
himmelma@35172
  1716
lemma support_clauses:
himmelma@35172
  1717
  "\<And>f g s. support opp f {} = {}"
himmelma@35172
  1718
  "\<And>f g s. support opp f (insert x s) = (if f(x) = neutral opp then support opp f s else insert x (support opp f s))"
himmelma@35172
  1719
  "\<And>f g s. support opp f (s - {x}) = (support opp f s) - {x}"
himmelma@35172
  1720
  "\<And>f g s. support opp f (s \<union> t) = (support opp f s) \<union> (support opp f t)"
himmelma@35172
  1721
  "\<And>f g s. support opp f (s \<inter> t) = (support opp f s) \<inter> (support opp f t)"
himmelma@35172
  1722
  "\<And>f g s. support opp f (s - t) = (support opp f s) - (support opp f t)"
himmelma@35172
  1723
  "\<And>f g s. support opp g (f ` s) = f ` (support opp (g o f) s)"
himmelma@35172
  1724
unfolding support_def by auto
himmelma@35172
  1725
himmelma@35172
  1726
lemma finite_support[intro]:"finite s \<Longrightarrow> finite (support opp f s)"
himmelma@35172
  1727
  unfolding support_def by auto
himmelma@35172
  1728
himmelma@35172
  1729
lemma iterate_empty[simp]:"iterate opp {} f = neutral opp"
himmelma@35172
  1730
  unfolding iterate_def fold'_def by auto 
himmelma@35172
  1731
himmelma@35172
  1732
lemma iterate_insert[simp]: assumes "monoidal opp" "finite s"
himmelma@35172
  1733
  shows "iterate opp (insert x s) f = (if x \<in> s then iterate opp s f else opp (f x) (iterate opp s f))" 
himmelma@35172
  1734
proof(cases "x\<in>s") case True hence *:"insert x s = s" by auto
himmelma@35172
  1735
  show ?thesis unfolding iterate_def if_P[OF True] * by auto
himmelma@35172
  1736
next case False note x=this
himmelma@35172
  1737
  note * = fun_left_comm.fun_left_comm_apply[OF fun_left_comm_monoidal[OF assms(1)]]
himmelma@35172
  1738
  show ?thesis proof(cases "f x = neutral opp")
himmelma@35172
  1739
    case True show ?thesis unfolding iterate_def if_not_P[OF x] support_clauses if_P[OF True]
himmelma@35172
  1740
      unfolding True monoidal_simps[OF assms(1)] by auto
himmelma@35172
  1741
  next case False show ?thesis unfolding iterate_def fold'_def  if_not_P[OF x] support_clauses if_not_P[OF False]
himmelma@35172
  1742
      apply(subst fun_left_comm.fold_insert[OF * finite_support])
himmelma@35172
  1743
      using `finite s` unfolding support_def using False x by auto qed qed 
himmelma@35172
  1744
himmelma@35172
  1745
lemma iterate_some:
himmelma@35172
  1746
  assumes "monoidal opp"  "finite s"
himmelma@35172
  1747
  shows "iterate (lifted opp) s (\<lambda>x. Some(f x)) = Some (iterate opp s f)" using assms(2)
himmelma@35172
  1748
proof(induct s) case empty thus ?case using assms by auto
himmelma@35172
  1749
next case (insert x F) show ?case apply(subst iterate_insert) prefer 3 apply(subst if_not_P)
himmelma@35172
  1750
    defer unfolding insert(3) lifted.simps apply rule using assms insert by auto qed
himmelma@35172
  1751
himmelma@35172
  1752
subsection {* Two key instances of additivity. *}
himmelma@35172
  1753
himmelma@35172
  1754
lemma neutral_add[simp]:
himmelma@35172
  1755
  "neutral op + = (0::_::comm_monoid_add)" unfolding neutral_def 
himmelma@35172
  1756
  apply(rule some_equality) defer apply(erule_tac x=0 in allE) by auto
himmelma@35172
  1757
himmelma@35172
  1758
lemma operative_content[intro]: "operative (op +) content"
himmelma@35172
  1759
  unfolding operative_def content_split[THEN sym] neutral_add by auto
himmelma@35172
  1760
himmelma@35172
  1761
lemma neutral_monoid[simp]: "neutral ((op +)::('a::comm_monoid_add) \<Rightarrow> 'a \<Rightarrow> 'a) = 0"
himmelma@35172
  1762
  unfolding neutral_def apply(rule some_equality) defer
himmelma@35172
  1763
  apply(erule_tac x=0 in allE) by auto
himmelma@35172
  1764
himmelma@35172
  1765
lemma monoidal_monoid[intro]:
himmelma@35172
  1766
  shows "monoidal ((op +)::('a::comm_monoid_add) \<Rightarrow> 'a \<Rightarrow> 'a)"
himmelma@35172
  1767
  unfolding monoidal_def neutral_monoid by(auto simp add: group_simps) 
himmelma@35172
  1768
himmelma@35172
  1769
lemma operative_integral: fixes f::"real^'n \<Rightarrow> 'a::banach"
himmelma@35172
  1770
  shows "operative (lifted(op +)) (\<lambda>i. if f integrable_on i then Some(integral i f) else None)"
himmelma@35172
  1771
  unfolding operative_def unfolding neutral_lifted[OF monoidal_monoid] neutral_add
himmelma@35172
  1772
  apply(rule,rule,rule,rule) defer apply(rule allI)+
himmelma@35172
  1773
proof- fix a b c k show "(if f integrable_on {a..b} then Some (integral {a..b} f) else None) =
himmelma@35172
  1774
              lifted op + (if f integrable_on {a..b} \<inter> {x. x $ k \<le> c} then Some (integral ({a..b} \<inter> {x. x $ k \<le> c}) f) else None)
himmelma@35172
  1775
               (if f integrable_on {a..b} \<inter> {x. c \<le> x $ k} then Some (integral ({a..b} \<inter> {x. c \<le> x $ k}) f) else None)"
himmelma@35172
  1776
  proof(cases "f integrable_on {a..b}") 
himmelma@35172
  1777
    case True show ?thesis unfolding if_P[OF True]
himmelma@35172
  1778
      unfolding if_P[OF integrable_split(1)[OF True]] if_P[OF integrable_split(2)[OF True]]
himmelma@35172
  1779
      unfolding lifted.simps option.inject apply(rule integral_unique) apply(rule has_integral_split) 
himmelma@35172
  1780
      apply(rule_tac[!] integrable_integral integrable_split)+ using True by assumption+
himmelma@35172
  1781
  next case False have "(\<not> (f integrable_on {a..b} \<inter> {x. x $ k \<le> c})) \<or> (\<not> ( f integrable_on {a..b} \<inter> {x. c \<le> x $ k}))"
himmelma@35172
  1782
    proof(rule ccontr) case goal1 hence "f integrable_on {a..b}" apply- unfolding integrable_on_def
himmelma@35172
  1783
        apply(rule_tac x="integral ({a..b} \<inter> {x. x $ k \<le> c}) f + integral ({a..b} \<inter> {x. x $ k \<ge> c}) f" in exI)
himmelma@35172
  1784
        apply(rule has_integral_split) apply(rule_tac[!] integrable_integral) by auto
himmelma@35172
  1785
      thus False using False by auto
himmelma@35172
  1786
    qed thus ?thesis using False by auto 
himmelma@35172
  1787
  qed next 
himmelma@35172
  1788
  fix a b assume as:"content {a..b::real^'n} = 0"
himmelma@35172
  1789
  thus "(if f integrable_on {a..b} then Some (integral {a..b} f) else None) = Some 0"
himmelma@35172
  1790
    unfolding if_P[OF integrable_on_null[OF as]] using has_integral_null_eq[OF as] by auto qed
himmelma@35172
  1791
himmelma@35172
  1792
subsection {* Points of division of a partition. *}
himmelma@35172
  1793
himmelma@35172
  1794
definition "division_points (k::(real^'n) set) d = 
himmelma@35172
  1795
    {(j,x). (interval_lowerbound k)$j < x \<and> x < (interval_upperbound k)$j \<and>
himmelma@35172
  1796
           (\<exists>i\<in>d. (interval_lowerbound i)$j = x \<or> (interval_upperbound i)$j = x)}"
himmelma@35172
  1797
himmelma@35172
  1798
lemma division_points_finite: assumes "d division_of i"
himmelma@35172
  1799
  shows "finite (division_points i d)"
himmelma@35172
  1800
proof- note assm = division_ofD[OF assms]
himmelma@35172
  1801
  let ?M = "\<lambda>j. {(j,x)|x. (interval_lowerbound i)$j < x \<and> x < (interval_upperbound i)$j \<and>
himmelma@35172
  1802
           (\<exists>i\<in>d. (interval_lowerbound i)$j = x \<or> (interval_upperbound i)$j = x)}"
himmelma@35172
  1803
  have *:"division_points i d = \<Union>(?M ` UNIV)"
himmelma@35172
  1804
    unfolding division_points_def by auto
himmelma@35172
  1805
  show ?thesis unfolding * using assm by auto qed
himmelma@35172
  1806
himmelma@35172
  1807
lemma division_points_subset:
himmelma@35172
  1808
  assumes "d division_of {a..b}" "\<forall>i. a$i < b$i"  "a$k < c" "c < b$k"
himmelma@35172
  1809
  shows "division_points ({a..b} \<inter> {x. x$k \<le> c}) {l \<inter> {x. x$k \<le> c} | l . l \<in> d \<and> ~(l \<inter> {x. x$k \<le> c} = {})}
himmelma@35172
  1810
                  \<subseteq> division_points ({a..b}) d" (is ?t1) and
himmelma@35172
  1811
        "division_points ({a..b} \<inter> {x. x$k \<ge> c}) {l \<inter> {x. x$k \<ge> c} | l . l \<in> d \<and> ~(l \<inter> {x. x$k \<ge> c} = {})}
himmelma@35172
  1812
                  \<subseteq> division_points ({a..b}) d" (is ?t2)
himmelma@35172
  1813
proof- note assm = division_ofD[OF assms(1)]
himmelma@35172
  1814
  have *:"\<forall>i. a$i \<le> b$i"   "\<forall>i. a$i \<le> (\<chi> i. if i = k then min (b $ k) c else b $ i) $ i"
himmelma@35172
  1815
    "\<forall>i. (\<chi> i. if i = k then max (a $ k) c else a $ i) $ i \<le> b$i"  "min (b $ k) c = c" "max (a $ k) c = c"
himmelma@35172
  1816
    using assms using less_imp_le by auto
himmelma@35172
  1817
  show ?t1 unfolding division_points_def interval_split[of a b]
himmelma@35172
  1818
    unfolding interval_bounds[OF *(1)] interval_bounds[OF *(2)] interval_bounds[OF *(3)] Cart_lambda_beta unfolding *
himmelma@35172
  1819
    unfolding subset_eq apply(rule) unfolding mem_Collect_eq split_beta apply(erule bexE conjE)+ unfolding mem_Collect_eq apply(erule exE conjE)+
himmelma@35172
  1820
  proof- fix i l x assume as:"a $ fst x < snd x" "snd x < (if fst x = k then c else b $ fst x)"
himmelma@35172
  1821
      "interval_lowerbound i $ fst x = snd x \<or> interval_upperbound i $ fst x = snd x"  "i = l \<inter> {x. x $ k \<le> c}" "l \<in> d" "l \<inter> {x. x $ k \<le> c} \<noteq> {}"
himmelma@35172
  1822
    from assm(4)[OF this(5)] guess u v apply-by(erule exE)+ note l=this
himmelma@35172
  1823
    have *:"\<forall>i. u $ i \<le> (\<chi> i. if i = k then min (v $ k) c else v $ i) $ i" using as(6) unfolding l interval_split interval_ne_empty as .
himmelma@35172
  1824
    have **:"\<forall>i. u$i \<le> v$i" using l using as(6) unfolding interval_ne_empty[THEN sym] by auto
himmelma@35172
  1825
    show "a $ fst x < snd x \<and> snd x < b $ fst x \<and> (\<exists>i\<in>d. interval_lowerbound i $ fst x = snd x \<or> interval_upperbound i $ fst x = snd x)"
himmelma@35172
  1826
      using as(1-3,5) unfolding l interval_split interval_ne_empty as interval_bounds[OF *] Cart_lambda_beta apply-
himmelma@35172
  1827
      apply(rule,assumption,rule) defer apply(rule_tac x="{u..v}" in bexI) unfolding interval_bounds[OF **]
himmelma@35172
  1828
      apply(case_tac[!] "fst x = k") using assms by auto
himmelma@35172
  1829
  qed
himmelma@35172
  1830
  show ?t2 unfolding division_points_def interval_split[of a b]
himmelma@35172
  1831
    unfolding interval_bounds[OF *(1)] interval_bounds[OF *(2)] interval_bounds[OF *(3)] Cart_lambda_beta unfolding *
himmelma@35172
  1832
    unfolding subset_eq apply(rule) unfolding mem_Collect_eq split_beta apply(erule bexE conjE)+ unfolding mem_Collect_eq apply(erule exE conjE)+
himmelma@35172
  1833
  proof- fix i l x assume as:"(if fst x = k then c else a $ fst x) < snd x" "snd x < b $ fst x" "interval_lowerbound i $ fst x = snd x \<or> interval_upperbound i $ fst x = snd x"
himmelma@35172
  1834
      "i = l \<inter> {x. c \<le> x $ k}" "l \<in> d" "l \<inter> {x. c \<le> x $ k} \<noteq> {}"
himmelma@35172
  1835
    from assm(4)[OF this(5)] guess u v apply-by(erule exE)+ note l=this
himmelma@35172
  1836
    have *:"\<forall>i. (\<chi> i. if i = k then max (u $ k) c else u $ i) $ i \<le> v $ i" using as(6) unfolding l interval_split interval_ne_empty as .
himmelma@35172
  1837
    have **:"\<forall>i. u$i \<le> v$i" using l using as(6) unfolding interval_ne_empty[THEN sym] by auto
himmelma@35172
  1838
    show "a $ fst x < snd x \<and> snd x < b $ fst x \<and> (\<exists>i\<in>d. interval_lowerbound i $ fst x = snd x \<or> interval_upperbound i $ fst x = snd x)"
himmelma@35172
  1839
      using as(1-3,5) unfolding l interval_split interval_ne_empty as interval_bounds[OF *] Cart_lambda_beta apply-
himmelma@35172
  1840
      apply rule defer apply(rule,assumption) apply(rule_tac x="{u..v}" in bexI) unfolding interval_bounds[OF **]
himmelma@35172
  1841
      apply(case_tac[!] "fst x = k") using assms by auto qed qed
himmelma@35172
  1842
himmelma@35172
  1843
lemma division_points_psubset:
himmelma@35172
  1844
  assumes "d division_of {a..b}"  "\<forall>i. a$i < b$i"  "a$k < c" "c < b$k"
himmelma@35172
  1845
  "l \<in> d" "interval_lowerbound l$k = c \<or> interval_upperbound l$k = c"
himmelma@35172
  1846
  shows "division_points ({a..b} \<inter> {x. x$k \<le> c}) {l \<inter> {x. x$k \<le> c} | l. l\<in>d \<and> l \<inter> {x. x$k \<le> c} \<noteq> {}} \<subset> division_points ({a..b}) d" (is "?D1 \<subset> ?D") 
himmelma@35172
  1847
        "division_points ({a..b} \<inter> {x. x$k \<ge> c}) {l \<inter> {x. x$k \<ge> c} | l. l\<in>d \<and> l \<inter> {x. x$k \<ge> c} \<noteq> {}} \<subset> division_points ({a..b}) d" (is "?D2 \<subset> ?D") 
himmelma@35172
  1848
proof- have ab:"\<forall>i. a$i \<le> b$i" using assms(2) by(auto intro!:less_imp_le)
himmelma@35172
  1849
  guess u v using division_ofD(4)[OF assms(1,5)] apply-by(erule exE)+ note l=this
himmelma@35172
  1850
  have uv:"\<forall>i. u$i \<le> v$i" "\<forall>i. a$i \<le> u$i \<and> v$i \<le> b$i" using division_ofD(2,2,3)[OF assms(1,5)] unfolding l interval_ne_empty
himmelma@35172
  1851
    unfolding subset_eq apply- defer apply(erule_tac x=u in ballE, erule_tac x=v in ballE) unfolding mem_interval by auto
himmelma@35172
  1852
  have *:"interval_upperbound ({a..b} \<inter> {x. x $ k \<le> interval_upperbound l $ k}) $ k = interval_upperbound l $ k"
himmelma@35172
  1853
         "interval_upperbound ({a..b} \<inter> {x. x $ k \<le> interval_lowerbound l $ k}) $ k = interval_lowerbound l $ k"
himmelma@35172
  1854
    unfolding interval_split apply(subst interval_bounds) prefer 3 apply(subst interval_bounds)
himmelma@35172
  1855
    unfolding l interval_bounds[OF uv(1)] using uv[rule_format,of k] ab by auto
himmelma@35172
  1856
  have "\<exists>x. x \<in> ?D - ?D1" using assms(2-) apply-apply(erule disjE)
himmelma@35172
  1857
    apply(rule_tac x="(k,(interval_lowerbound l)$k)" in exI) defer
himmelma@35172
  1858
    apply(rule_tac x="(k,(interval_upperbound l)$k)" in exI)
himmelma@35172
  1859
    unfolding division_points_def unfolding interval_bounds[OF ab]
himmelma@35172
  1860
    apply (auto simp add:interval_bounds) unfolding * by auto
himmelma@35172
  1861
  thus "?D1 \<subset> ?D" apply-apply(rule,rule division_points_subset[OF assms(1-4)]) by auto
himmelma@35172
  1862
himmelma@35172
  1863
  have *:"interval_lowerbound ({a..b} \<inter> {x. x $ k \<ge> interval_lowerbound l $ k}) $ k = interval_lowerbound l $ k"
himmelma@35172
  1864
         "interval_lowerbound ({a..b} \<inter> {x. x $ k \<ge> interval_upperbound l $ k}) $ k = interval_upperbound l $ k"
himmelma@35172
  1865
    unfolding interval_split apply(subst interval_bounds) prefer 3 apply(subst interval_bounds)
himmelma@35172
  1866
    unfolding l interval_bounds[OF uv(1)] using uv[rule_format,of k] ab by auto
himmelma@35172
  1867
  have "\<exists>x. x \<in> ?D - ?D2" using assms(2-) apply-apply(erule disjE)
himmelma@35172
  1868
    apply(rule_tac x="(k,(interval_lowerbound l)$k)" in exI) defer
himmelma@35172
  1869
    apply(rule_tac x="(k,(interval_upperbound l)$k)" in exI)
himmelma@35172
  1870
    unfolding division_points_def unfolding interval_bounds[OF ab]
himmelma@35172
  1871
    apply (auto simp add:interval_bounds) unfolding * by auto
himmelma@35172
  1872
  thus "?D2 \<subset> ?D" apply-apply(rule,rule division_points_subset[OF assms(1-4)]) by auto qed
himmelma@35172
  1873
himmelma@35172
  1874
subsection {* Preservation by divisions and tagged divisions. *}
himmelma@35172
  1875
himmelma@35172
  1876
lemma support_support[simp]:"support opp f (support opp f s) = support opp f s"
himmelma@35172
  1877
  unfolding support_def by auto
himmelma@35172
  1878
himmelma@35172
  1879
lemma iterate_support[simp]: "iterate opp (support opp f s) f = iterate opp s f"
himmelma@35172
  1880
  unfolding iterate_def support_support by auto
himmelma@35172
  1881
himmelma@35172
  1882
lemma iterate_expand_cases:
himmelma@35172
  1883
  "iterate opp s f = (if finite(support opp f s) then iterate opp (support opp f s) f else neutral opp)"
himmelma@35172
  1884
  apply(cases) apply(subst if_P,assumption) unfolding iterate_def support_support fold'_def by auto 
himmelma@35172
  1885
himmelma@35172
  1886
lemma iterate_image: assumes "monoidal opp"  "inj_on f s"
himmelma@35172
  1887
  shows "iterate opp (f ` s) g = iterate opp s (g \<circ> f)"
himmelma@35172
  1888
proof- have *:"\<And>s. finite s \<Longrightarrow>  \<forall>x\<in>s. \<forall>y\<in>s. f x = f y \<longrightarrow> x = y \<Longrightarrow>
himmelma@35172
  1889
     iterate opp (f ` s) g = iterate opp s (g \<circ> f)"
himmelma@35172
  1890
  proof- case goal1 show ?case using goal1
himmelma@35172
  1891
    proof(induct s) case empty thus ?case using assms(1) by auto
himmelma@35172
  1892
    next case (insert x s) show ?case unfolding iterate_insert[OF assms(1) insert(1)]
himmelma@35172
  1893
        unfolding if_not_P[OF insert(2)] apply(subst insert(3)[THEN sym])
himmelma@35172
  1894
        unfolding image_insert defer apply(subst iterate_insert[OF assms(1)])
himmelma@35172
  1895
        apply(rule finite_imageI insert)+ apply(subst if_not_P)
himmelma@35172
  1896
        unfolding image_iff o_def using insert(2,4) by auto
himmelma@35172
  1897
    qed qed
himmelma@35172
  1898
  show ?thesis 
himmelma@35172
  1899
    apply(cases "finite (support opp g (f ` s))")
himmelma@35172
  1900
    apply(subst (1) iterate_support[THEN sym],subst (2) iterate_support[THEN sym])
himmelma@35172
  1901
    unfolding support_clauses apply(rule *)apply(rule finite_imageD,assumption) unfolding inj_on_def[symmetric]
himmelma@35172
  1902
    apply(rule subset_inj_on[OF assms(2) support_subset])+
himmelma@35172
  1903
    apply(subst iterate_expand_cases) unfolding support_clauses apply(simp only: if_False)
himmelma@35172
  1904
    apply(subst iterate_expand_cases) apply(subst if_not_P) by auto qed
himmelma@35172
  1905
himmelma@35172
  1906
himmelma@35172
  1907
(* This lemma about iterations comes up in a few places.                     *)
himmelma@35172
  1908
lemma iterate_nonzero_image_lemma:
himmelma@35172
  1909
  assumes "monoidal opp" "finite s" "g(a) = neutral opp"
himmelma@35172
  1910
  "\<forall>x\<in>s. \<forall>y\<in>s. f x = f y \<and> x \<noteq> y \<longrightarrow> g(f x) = neutral opp"
himmelma@35172
  1911
  shows "iterate opp {f x | x. x \<in> s \<and> f x \<noteq> a} g = iterate opp s (g \<circ> f)"
himmelma@35172
  1912
proof- have *:"{f x |x. x \<in> s \<and> ~(f x = a)} = f ` {x. x \<in> s \<and> ~(f x = a)}" by auto
himmelma@35172
  1913
  have **:"support opp (g \<circ> f) {x \<in> s. f x \<noteq> a} = support opp (g \<circ> f) s"
himmelma@35172
  1914
    unfolding support_def using assms(3) by auto
himmelma@35172
  1915
  show ?thesis unfolding *
himmelma@35172
  1916
    apply(subst iterate_support[THEN sym]) unfolding support_clauses
himmelma@35172
  1917
    apply(subst iterate_image[OF assms(1)]) defer
himmelma@35172
  1918
    apply(subst(2) iterate_support[THEN sym]) apply(subst **)
himmelma@35172
  1919
    unfolding inj_on_def using assms(3,4) unfolding support_def by auto qed
himmelma@35172
  1920
himmelma@35172
  1921
lemma iterate_eq_neutral:
himmelma@35172
  1922
  assumes "monoidal opp"  "\<forall>x \<in> s. (f(x) = neutral opp)"
himmelma@35172
  1923
  shows "(iterate opp s f = neutral opp)"
himmelma@35172
  1924
proof- have *:"support opp f s = {}" unfolding support_def using assms(2) by auto
himmelma@35172
  1925
  show ?thesis apply(subst iterate_support[THEN sym]) 
himmelma@35172
  1926
    unfolding * using assms(1) by auto qed
himmelma@35172
  1927
himmelma@35172
  1928
lemma iterate_op: assumes "monoidal opp" "finite s"
himmelma@35172
  1929
  shows "iterate opp s (\<lambda>x. opp (f x) (g x)) = opp (iterate opp s f) (iterate opp s g)" using assms(2)
himmelma@35172
  1930
proof(induct s) case empty thus ?case unfolding iterate_insert[OF assms(1)] using assms(1) by auto
himmelma@35172
  1931
next case (insert x F) show ?case unfolding iterate_insert[OF assms(1) insert(1)] if_not_P[OF insert(2)] insert(3)
himmelma@35172
  1932
    unfolding monoidal_ac[OF assms(1)] by(rule refl) qed
himmelma@35172
  1933
himmelma@35172
  1934
lemma iterate_eq: assumes "monoidal opp" "\<And>x. x \<in> s \<Longrightarrow> f x = g x"
himmelma@35172
  1935
  shows "iterate opp s f = iterate opp s g"
himmelma@35172
  1936
proof- have *:"support opp g s = support opp f s"
himmelma@35172
  1937
    unfolding support_def using assms(2) by auto
himmelma@35172
  1938
  show ?thesis
himmelma@35172
  1939
  proof(cases "finite (support opp f s)")
himmelma@35172
  1940
    case False thus ?thesis apply(subst iterate_expand_cases,subst(2) iterate_expand_cases)
himmelma@35172
  1941
      unfolding * by auto
himmelma@35172
  1942
  next def su \<equiv> "support opp f s"
himmelma@35172
  1943
    case True note support_subset[of opp f s] 
himmelma@35172
  1944
    thus ?thesis apply- apply(subst iterate_support[THEN sym],subst(2) iterate_support[THEN sym]) unfolding * using True
himmelma@35172
  1945
      unfolding su_def[symmetric]
himmelma@35172
  1946
    proof(induct su) case empty show ?case by auto
himmelma@35172
  1947
    next case (insert x s) show ?case unfolding iterate_insert[OF assms(1) insert(1)] 
himmelma@35172
  1948
        unfolding if_not_P[OF insert(2)] apply(subst insert(3))
himmelma@35172
  1949
        defer apply(subst assms(2)[of x]) using insert by auto qed qed qed
himmelma@35172
  1950
himmelma@35172
  1951
lemma nonempty_witness: assumes "s \<noteq> {}" obtains x where "x \<in> s" using assms by auto
himmelma@35172
  1952
himmelma@35172
  1953
lemma operative_division: fixes f::"(real^'n) set \<Rightarrow> 'a"
himmelma@35172
  1954
  assumes "monoidal opp" "operative opp f" "d division_of {a..b}"
himmelma@35172
  1955
  shows "iterate opp d f = f {a..b}"
himmelma@35172
  1956
proof- def C \<equiv> "card (division_points {a..b} d)" thus ?thesis using assms
himmelma@35172
  1957
  proof(induct C arbitrary:a b d rule:full_nat_induct)
himmelma@35172
  1958
    case goal1
himmelma@35172
  1959
    { presume *:"content {a..b} \<noteq> 0 \<Longrightarrow> ?case"
himmelma@35172
  1960
      thus ?case apply-apply(cases) defer apply assumption
himmelma@35172
  1961
      proof- assume as:"content {a..b} = 0"
himmelma@35172
  1962
        show ?case unfolding operativeD(1)[OF assms(2) as] apply(rule iterate_eq_neutral[OF goal1(2)])
himmelma@35172
  1963
        proof fix x assume x:"x\<in>d"
himmelma@35172
  1964
          then guess u v apply(drule_tac division_ofD(4)[OF goal1(4)]) by(erule exE)+
himmelma@35172
  1965
          thus "f x = neutral opp" using division_of_content_0[OF as goal1(4)] 
himmelma@35172
  1966
            using operativeD(1)[OF assms(2)] x by auto
himmelma@35172
  1967
        qed qed }
himmelma@35172
  1968
    assume "content {a..b} \<noteq> 0" note ab = this[unfolded content_lt_nz[THEN sym] content_pos_lt_eq]
himmelma@35172
  1969
    hence ab':"\<forall>i. a$i \<le> b$i" by (auto intro!: less_imp_le) show ?case 
himmelma@35172
  1970
    proof(cases "division_points {a..b} d = {}")
himmelma@35172
  1971
      case True have d':"\<forall>i\<in>d. \<exists>u v. i = {u..v} \<and>
himmelma@35172
  1972
        (\<forall>j. u$j = a$j \<and> v$j = a$j \<or> u$j = b$j \<and> v$j = b$j \<or> u$j = a$j \<and> v$j = b$j)"
himmelma@35172
  1973
        unfolding forall_in_division[OF goal1(4)] apply(rule,rule,rule)
himmelma@35172
  1974
        apply(rule_tac x=a in exI,rule_tac x=b in exI) apply(rule,rule refl) apply(rule)
himmelma@35172
  1975
      proof- fix u v j assume as:"{u..v} \<in> d" note division_ofD(3)[OF goal1(4) this]
himmelma@35172
  1976
        hence uv:"\<forall>i. u$i \<le> v$i" "u$j \<le> v$j" unfolding interval_ne_empty by auto
himmelma@35172
  1977
        have *:"\<And>p r Q. p \<or> r \<or> (\<forall>x\<in>d. Q x) \<Longrightarrow> p \<or> r \<or> (Q {u..v})" using as by auto
himmelma@35172
  1978
        have "(j, u$j) \<notin> division_points {a..b} d"
himmelma@35172
  1979
          "(j, v$j) \<notin> division_points {a..b} d" using True by auto
himmelma@35172
  1980
        note this[unfolded de_Morgan_conj division_points_def mem_Collect_eq split_conv interval_bounds[OF ab'] bex_simps]
himmelma@35172
  1981
        note *[OF this(1)] *[OF this(2)] note this[unfolded interval_bounds[OF uv(1)]]
himmelma@35172
  1982
        moreover have "a$j \<le> u$j" "v$j \<le> b$j" using division_ofD(2,2,3)[OF goal1(4) as] 
himmelma@35172
  1983
          unfolding subset_eq apply- apply(erule_tac x=u in ballE,erule_tac[3] x=v in ballE)
himmelma@35172
  1984
          unfolding interval_ne_empty mem_interval by auto
himmelma@35172
  1985
        ultimately show "u$j = a$j \<and> v$j = a$j \<or> u$j = b$j \<and> v$j = b$j \<or> u$j = a$j \<and> v$j = b$j"
himmelma@35172
  1986
          unfolding not_less de_Morgan_disj using ab[rule_format,of j] uv(2) by auto
himmelma@35172
  1987
      qed have "(1/2) *\<^sub>R (a+b) \<in> {a..b}" unfolding mem_interval using ab by(auto intro!:less_imp_le)
himmelma@35172
  1988
      note this[unfolded division_ofD(6)[OF goal1(4),THEN sym] Union_iff]
himmelma@35172
  1989
      then guess i .. note i=this guess u v using d'[rule_format,OF i(1)] apply-by(erule exE conjE)+ note uv=this
himmelma@35172
  1990
      have "{a..b} \<in> d"
himmelma@35172
  1991
      proof- { presume "i = {a..b}" thus ?thesis using i by auto }
himmelma@35172
  1992
        { presume "u = a" "v = b" thus "i = {a..b}" using uv by auto }
himmelma@35172
  1993
        show "u = a" "v = b" unfolding Cart_eq
himmelma@35172
  1994
        proof(rule_tac[!] allI) fix j note i(2)[unfolded uv mem_interval,rule_format,of j]
himmelma@35172
  1995
          thus "u $ j = a $ j" "v $ j = b $ j" using uv(2)[rule_format,of j] by auto
himmelma@35172
  1996
        qed qed
himmelma@35172
  1997
      hence *:"d = insert {a..b} (d - {{a..b}})" by auto
himmelma@35172
  1998
      have "iterate opp (d - {{a..b}}) f = neutral opp" apply(rule iterate_eq_neutral[OF goal1(2)])
himmelma@35172
  1999
      proof fix x assume x:"x \<in> d - {{a..b}}" hence "x\<in>d" by auto note d'[rule_format,OF this]
himmelma@35172
  2000
        then guess u v apply-by(erule exE conjE)+ note uv=this
himmelma@35172
  2001
        have "u\<noteq>a \<or> v\<noteq>b" using x[unfolded uv] by auto  
himmelma@35172
  2002
        then obtain j where "u$j \<noteq> a$j \<or> v$j \<noteq> b$j" unfolding Cart_eq by auto
himmelma@35172
  2003
        hence "u$j = v$j" using uv(2)[rule_format,of j] by auto
himmelma@35172
  2004
        hence "content {u..v} = 0"  unfolding content_eq_0 apply(rule_tac x=j in exI) by auto
himmelma@35172
  2005
        thus "f x = neutral opp" unfolding uv(1) by(rule operativeD(1)[OF goal1(3)])
himmelma@35172
  2006
      qed thus "iterate opp d f = f {a..b}" apply-apply(subst *) 
himmelma@35172
  2007
        apply(subst iterate_insert[OF goal1(2)]) using goal1(2,4) by auto
himmelma@35172
  2008
    next case False hence "\<exists>x. x\<in>division_points {a..b} d" by auto
himmelma@35172
  2009
      then guess k c unfolding split_paired_Ex apply- unfolding division_points_def mem_Collect_eq split_conv
himmelma@35172
  2010
        by(erule exE conjE)+ note kc=this[unfolded interval_bounds[OF ab']]
himmelma@35172
  2011
      from this(3) guess j .. note j=this
himmelma@35172
  2012
      def d1 \<equiv> "{l \<inter> {x. x$k \<le> c} | l. l \<in> d \<and> l \<inter> {x. x$k \<le> c} \<noteq> {}}"
himmelma@35172
  2013
      def d2 \<equiv> "{l \<inter> {x. x$k \<ge> c} | l. l \<in> d \<and> l \<inter> {x. x$k \<ge> c} \<noteq> {}}"
himmelma@35172
  2014
      def cb \<equiv> "(\<chi> i. if i = k then c else b$i)" and ca \<equiv> "(\<chi> i. if i = k then c else a$i)"
himmelma@35172
  2015
      note division_points_psubset[OF goal1(4) ab kc(1-2) j]
himmelma@35172
  2016
      note psubset_card_mono[OF _ this(1)] psubset_card_mono[OF _ this(2)]
himmelma@35172
  2017
      hence *:"(iterate opp d1 f) = f ({a..b} \<inter> {x. x$k \<le> c})" "(iterate opp d2 f) = f ({a..b} \<inter> {x. x$k \<ge> c})"
himmelma@35172
  2018
        apply- unfolding interval_split apply(rule_tac[!] goal1(1)[rule_format])
himmelma@35172
  2019
        using division_split[OF goal1(4), where k=k and c=c]
himmelma@35172
  2020
        unfolding interval_split d1_def[symmetric] d2_def[symmetric] unfolding goal1(2) Suc_le_mono
himmelma@35172
  2021
        using goal1(2-3) using division_points_finite[OF goal1(4)] by auto
himmelma@35172
  2022
      have "f {a..b} = opp (iterate opp d1 f) (iterate opp d2 f)" (is "_ = ?prev")
himmelma@35172
  2023
        unfolding * apply(rule operativeD(2)) using goal1(3) .
himmelma@35172
  2024
      also have "iterate opp d1 f = iterate opp d (\<lambda>l. f(l \<inter> {x. x$k \<le> c}))"
himmelma@35172
  2025
        unfolding d1_def apply(rule iterate_nonzero_image_lemma[unfolded o_def])
himmelma@35172
  2026
        unfolding empty_as_interval apply(rule goal1 division_of_finite operativeD[OF goal1(3)])+
himmelma@35172
  2027
        unfolding empty_as_interval[THEN sym] apply(rule content_empty)
himmelma@35172
  2028
      proof(rule,rule,rule,erule conjE) fix l y assume as:"l \<in> d" "y \<in> d" "l \<inter> {x. x $ k \<le> c} = y \<inter> {x. x $ k \<le> c}" "l \<noteq> y" 
himmelma@35172
  2029
        from division_ofD(4)[OF goal1(4) this(1)] guess u v apply-by(erule exE)+ note l=this
himmelma@35172
  2030
        show "f (l \<inter> {x. x $ k \<le> c}) = neutral opp" unfolding l interval_split
himmelma@35172
  2031
          apply(rule operativeD(1) goal1)+ unfolding interval_split[THEN sym] apply(rule division_split_left_inj)
himmelma@35172
  2032
          apply(rule goal1) unfolding l[THEN sym] apply(rule as(1),rule as(2)) by(rule as)+
himmelma@35172
  2033
      qed also have "iterate opp d2 f = iterate opp d (\<lambda>l. f(l \<inter> {x. x$k \<ge> c}))"
himmelma@35172
  2034
        unfolding d2_def apply(rule iterate_nonzero_image_lemma[unfolded o_def])
himmelma@35172
  2035
        unfolding empty_as_interval apply(rule goal1 division_of_finite operativeD[OF goal1(3)])+
himmelma@35172
  2036
        unfolding empty_as_interval[THEN sym] apply(rule content_empty)
himmelma@35172
  2037
      proof(rule,rule,rule,erule conjE) fix l y assume as:"l \<in> d" "y \<in> d" "l \<inter> {x. c \<le> x $ k} = y \<inter> {x. c \<le> x $ k}" "l \<noteq> y" 
himmelma@35172
  2038
        from division_ofD(4)[OF goal1(4) this(1)] guess u v apply-by(erule exE)+ note l=this
himmelma@35172
  2039
        show "f (l \<inter> {x. x $ k \<ge> c}) = neutral opp" unfolding l interval_split
himmelma@35172
  2040
          apply(rule operativeD(1) goal1)+ unfolding interval_split[THEN sym] apply(rule division_split_right_inj)
himmelma@35172
  2041
          apply(rule goal1) unfolding l[THEN sym] apply(rule as(1),rule as(2)) by(rule as)+
himmelma@35172
  2042
      qed also have *:"\<forall>x\<in>d. f x = opp (f (x \<inter> {x. x $ k \<le> c})) (f (x \<inter> {x. c \<le> x $ k}))"
himmelma@35172
  2043
        unfolding forall_in_division[OF goal1(4)] apply(rule,rule,rule,rule operativeD(2)) using goal1(3) .
himmelma@35172
  2044
      have "opp (iterate opp d (\<lambda>l. f (l \<inter> {x. x $ k \<le> c}))) (iterate opp d (\<lambda>l. f (l \<inter> {x. c \<le> x $ k})))
himmelma@35172
  2045
        = iterate opp d f" apply(subst(3) iterate_eq[OF _ *[rule_format]]) prefer 3
himmelma@35172
  2046
        apply(rule iterate_op[THEN sym]) using goal1 by auto
himmelma@35172
  2047
      finally show ?thesis by auto
himmelma@35172
  2048
    qed qed qed 
himmelma@35172
  2049
himmelma@35172
  2050
lemma iterate_image_nonzero: assumes "monoidal opp"
himmelma@35172
  2051
  "finite s" "\<forall>x\<in>s. \<forall>y\<in>s. ~(x = y) \<and> f x = f y \<longrightarrow> g(f x) = neutral opp"
himmelma@35172
  2052
  shows "iterate opp (f ` s) g = iterate opp s (g \<circ> f)" using assms
himmelma@35172
  2053
proof(induct rule:finite_subset_induct[OF assms(2) subset_refl])
himmelma@35172
  2054
  case goal1 show ?case using assms(1) by auto
himmelma@35172
  2055
next case goal2 have *:"\<And>x y. y = neutral opp \<Longrightarrow> x = opp y x" using assms(1) by auto
himmelma@35172
  2056
  show ?case unfolding image_insert apply(subst iterate_insert[OF assms(1)])
himmelma@35172
  2057
    apply(rule finite_imageI goal2)+
himmelma@35172
  2058
    apply(cases "f a \<in> f ` F") unfolding if_P if_not_P apply(subst goal2(4)[OF assms(1) goal2(1)]) defer
himmelma@35172
  2059
    apply(subst iterate_insert[OF assms(1) goal2(1)]) defer
himmelma@35172
  2060
    apply(subst iterate_insert[OF assms(1) goal2(1)])
himmelma@35172
  2061
    unfolding if_not_P[OF goal2(3)] defer unfolding image_iff defer apply(erule bexE)
himmelma@35172
  2062
    apply(rule *) unfolding o_def apply(rule_tac y=x in goal2(7)[rule_format])
himmelma@35172
  2063
    using goal2 unfolding o_def by auto qed 
himmelma@35172
  2064
himmelma@35172
  2065
lemma operative_tagged_division: assumes "monoidal opp" "operative opp f" "d tagged_division_of {a..b}"
himmelma@35172
  2066
  shows "iterate(opp) d (\<lambda>(x,l). f l) = f {a..b}"
himmelma@35172
  2067
proof- have *:"(\<lambda>(x,l). f l) = (f o snd)" unfolding o_def by(rule,auto) note assm = tagged_division_ofD[OF assms(3)]
himmelma@35172
  2068
  have "iterate(opp) d (\<lambda>(x,l). f l) = iterate opp (snd ` d) f" unfolding *
himmelma@35172
  2069
    apply(rule iterate_image_nonzero[THEN sym,OF assms(1)]) apply(rule tagged_division_of_finite assms)+ 
himmelma@35172
  2070
    unfolding Ball_def split_paired_All snd_conv apply(rule,rule,rule,rule,rule,rule,rule,erule conjE)
himmelma@35172
  2071
  proof- fix a b aa ba assume as:"(a, b) \<in> d" "(aa, ba) \<in> d" "(a, b) \<noteq> (aa, ba)" "b = ba"
himmelma@35172
  2072
    guess u v using assm(4)[OF as(1)] apply-by(erule exE)+ note uv=this
himmelma@35172
  2073
    show "f b = neutral opp" unfolding uv apply(rule operativeD(1)[OF assms(2)])
himmelma@35172
  2074
      unfolding content_eq_0_interior using tagged_division_ofD(5)[OF assms(3) as(1-3)]
himmelma@35172
  2075
      unfolding as(4)[THEN sym] uv by auto
himmelma@35172
  2076
  qed also have "\<dots> = f {a..b}" 
himmelma@35172
  2077
    using operative_division[OF assms(1-2) division_of_tagged_division[OF assms(3)]] .
himmelma@35172
  2078
  finally show ?thesis . qed
himmelma@35172
  2079
himmelma@35172
  2080
subsection {* Additivity of content. *}
himmelma@35172
  2081
himmelma@35172
  2082
lemma setsum_iterate:assumes "finite s" shows "setsum f s = iterate op + s f"
himmelma@35172
  2083
proof- have *:"setsum f s = setsum f (support op + f s)"
himmelma@35172
  2084
    apply(rule setsum_mono_zero_right)
himmelma@35172
  2085
    unfolding support_def neutral_monoid using assms by auto
himmelma@35172
  2086
  thus ?thesis unfolding * setsum_def iterate_def fold_image_def fold'_def
himmelma@35172
  2087
    unfolding neutral_monoid . qed
himmelma@35172
  2088
himmelma@35172
  2089
lemma additive_content_division: assumes "d division_of {a..b}"
himmelma@35172
  2090
  shows "setsum content d = content({a..b})"
himmelma@35172
  2091
  unfolding operative_division[OF monoidal_monoid operative_content assms,THEN sym]
himmelma@35172
  2092
  apply(subst setsum_iterate) using assms by auto
himmelma@35172
  2093
himmelma@35172
  2094
lemma additive_content_tagged_division:
himmelma@35172
  2095
  assumes "d tagged_division_of {a..b}"
himmelma@35172
  2096
  shows "setsum (\<lambda>(x,l). content l) d = content({a..b})"
himmelma@35172
  2097
  unfolding operative_tagged_division[OF monoidal_monoid operative_content assms,THEN sym]
himmelma@35172
  2098
  apply(subst setsum_iterate) using assms by auto
himmelma@35172
  2099
himmelma@35172
  2100
subsection {* Finally, the integral of a constant\<forall> *}
himmelma@35172
  2101
himmelma@35172
  2102
lemma has_integral_const[intro]:
himmelma@35172
  2103
  "((\<lambda>x. c) has_integral (content({a..b::real^'n}) *\<^sub>R c)) ({a..b})"
himmelma@35172
  2104
  unfolding has_integral apply(rule,rule,rule_tac x="\<lambda>x. ball x 1" in exI)
himmelma@35172
  2105
  apply(rule,rule gauge_trivial)apply(rule,rule,erule conjE)
himmelma@35172
  2106
  unfolding split_def apply(subst scaleR_left.setsum[THEN sym, unfolded o_def])
himmelma@35172
  2107
  defer apply(subst additive_content_tagged_division[unfolded split_def]) apply assumption by auto
himmelma@35172
  2108
himmelma@35172
  2109
subsection {* Bounds on the norm of Riemann sums and the integral itself. *}
himmelma@35172
  2110
himmelma@35172
  2111
lemma dsum_bound: assumes "p division_of {a..b}" "norm(c) \<le> e"
himmelma@35172
  2112
  shows "norm(setsum (\<lambda>l. content l *\<^sub>R c) p) \<le> e * content({a..b})" (is "?l \<le> ?r")
himmelma@35172
  2113
  apply(rule order_trans,rule setsum_norm) defer unfolding norm_scaleR setsum_left_distrib[THEN sym]
himmelma@35172
  2114
  apply(rule order_trans[OF mult_left_mono],rule assms,rule setsum_abs_ge_zero)
himmelma@35172
  2115
  apply(subst real_mult_commute) apply(rule mult_left_mono)
himmelma@35172
  2116
  apply(rule order_trans[of _ "setsum content p"]) apply(rule eq_refl,rule setsum_cong2)
himmelma@35172
  2117
  apply(subst abs_of_nonneg) unfolding additive_content_division[OF assms(1)]
himmelma@35172
  2118
proof- from order_trans[OF norm_ge_zero[of c] assms(2)] show "0 \<le> e" .
himmelma@35172
  2119
  fix x assume "x\<in>p" from division_ofD(4)[OF assms(1) this] guess u v apply-by(erule exE)+
himmelma@35172
  2120
  thus "0 \<le> content x" using content_pos_le by auto
himmelma@35172
  2121
qed(insert assms,auto)
himmelma@35172
  2122
himmelma@35172
  2123
lemma rsum_bound: assumes "p tagged_division_of {a..b}" "\<forall>x\<in>{a..b}. norm(f x) \<le> e"
himmelma@35172
  2124
  shows "norm(setsum (\<lambda>(x,k). content k *\<^sub>R f x) p) \<le> e * content({a..b})"
himmelma@35172
  2125
proof(cases "{a..b} = {}") case True
himmelma@35172
  2126
  show ?thesis using assms(1) unfolding True tagged_division_of_trivial by auto
himmelma@35172
  2127
next case False show ?thesis
himmelma@35172
  2128
    apply(rule order_trans,rule setsum_norm) defer unfolding split_def norm_scaleR
himmelma@35172
  2129
    apply(rule order_trans[OF setsum_mono]) apply(rule mult_left_mono[OF _ abs_ge_zero, of _ e]) defer
himmelma@35172
  2130
    unfolding setsum_left_distrib[THEN sym] apply(subst real_mult_commute) apply(rule mult_left_mono)
himmelma@35172
  2131
    apply(rule order_trans[of _ "setsum (content \<circ> snd) p"]) apply(rule eq_refl,rule setsum_cong2)
himmelma@35172
  2132
    apply(subst o_def, rule abs_of_nonneg)
himmelma@35172
  2133
  proof- show "setsum (content \<circ> snd) p \<le> content {a..b}" apply(rule eq_refl)
himmelma@35172
  2134
      unfolding additive_content_tagged_division[OF assms(1),THEN sym] split_def by auto
himmelma@35172
  2135
    guess w using nonempty_witness[OF False] .
himmelma@35172
  2136
    thus "e\<ge>0" apply-apply(rule order_trans) defer apply(rule assms(2)[rule_format],assumption) by auto
himmelma@35172
  2137
    fix xk assume *:"xk\<in>p" guess x k  using surj_pair[of xk] apply-by(erule exE)+ note xk = this *[unfolded this]
himmelma@35172
  2138
    from tagged_division_ofD(4)[OF assms(1) xk(2)] guess u v apply-by(erule exE)+ note uv=this
himmelma@35172
  2139
    show "0\<le> content (snd xk)" unfolding xk snd_conv uv by(rule content_pos_le)
himmelma@35172
  2140
    show "norm (f (fst xk)) \<le> e" unfolding xk fst_conv using tagged_division_ofD(2,3)[OF assms(1) xk(2)] assms(2) by auto
himmelma@35172
  2141
  qed(insert assms,auto) qed
himmelma@35172
  2142
himmelma@35172
  2143
lemma rsum_diff_bound:
himmelma@35172
  2144
  assumes "p tagged_division_of {a..b}"  "\<forall>x\<in>{a..b}. norm(f x - g x) \<le> e"
himmelma@35172
  2145
  shows "norm(setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - setsum (\<lambda>(x,k). content k *\<^sub>R g x) p) \<le> e * content({a..b})"
himmelma@35172
  2146
  apply(rule order_trans[OF _ rsum_bound[OF assms]]) apply(rule eq_refl) apply(rule arg_cong[where f=norm])
himmelma@35172
  2147
  unfolding setsum_subtractf[THEN sym] apply(rule setsum_cong2) unfolding scaleR.diff_right by auto
himmelma@35172
  2148
himmelma@35172
  2149
lemma has_integral_bound: fixes f::"real^'n \<Rightarrow> 'a::real_normed_vector"
himmelma@35172
  2150
  assumes "0 \<le> B" "(f has_integral i) ({a..b})" "\<forall>x\<in>{a..b}. norm(f x) \<le> B"