src/HOL/Multivariate_Analysis/Integration.thy
author immler
Tue Mar 18 10:12:57 2014 +0100 (2014-03-18)
changeset 56188 0268784f60da
parent 56181 2aa0b19e74f3
child 56189 c4daa97ac57a
permissions -rw-r--r--
use cbox to relax class constraints
wenzelm@53399
     1
(*  Author:     John Harrison
wenzelm@53399
     2
    Author:     Robert Himmelmann, TU Muenchen (Translation from HOL light)
wenzelm@53399
     3
*)
wenzelm@53399
     4
hoelzl@37489
     5
header {* Kurzweil-Henstock Gauge Integration in many dimensions. *}
himmelma@35172
     6
hoelzl@35292
     7
theory Integration
wenzelm@41413
     8
imports
wenzelm@41413
     9
  Derivative
wenzelm@41413
    10
  "~~/src/HOL/Library/Indicator_Function"
himmelma@35172
    11
begin
himmelma@35172
    12
hoelzl@51518
    13
lemma cSup_abs_le: (* TODO: is this really needed? *)
hoelzl@51518
    14
  fixes S :: "real set"
hoelzl@51518
    15
  shows "S \<noteq> {} \<Longrightarrow> (\<forall>x\<in>S. \<bar>x\<bar> \<le> a) \<Longrightarrow> \<bar>Sup S\<bar> \<le> a"
hoelzl@54258
    16
  by (auto simp add: abs_le_interval_iff intro: cSup_least) (metis cSup_upper2 bdd_aboveI)
hoelzl@51518
    17
hoelzl@51518
    18
lemma cInf_abs_ge: (* TODO: is this really needed? *)
hoelzl@51518
    19
  fixes S :: "real set"
hoelzl@51518
    20
  shows "S \<noteq> {} \<Longrightarrow> (\<forall>x\<in>S. \<bar>x\<bar> \<le> a) \<Longrightarrow> \<bar>Inf S\<bar> \<le> a"
haftmann@56166
    21
  by (simp add: Inf_real_def) (insert cSup_abs_le [of "uminus ` S"], auto)
hoelzl@51518
    22
hoelzl@51518
    23
lemma cSup_asclose: (* TODO: is this really needed? *)
hoelzl@51475
    24
  fixes S :: "real set"
wenzelm@53399
    25
  assumes S: "S \<noteq> {}"
wenzelm@53399
    26
    and b: "\<forall>x\<in>S. \<bar>x - l\<bar> \<le> e"
wenzelm@53399
    27
  shows "\<bar>Sup S - l\<bar> \<le> e"
wenzelm@53399
    28
proof -
wenzelm@53399
    29
  have th: "\<And>(x::real) l e. \<bar>x - l\<bar> \<le> e \<longleftrightarrow> l - e \<le> x \<and> x \<le> l + e"
wenzelm@53399
    30
    by arith
hoelzl@54263
    31
  have "bdd_above S"
hoelzl@54263
    32
    using b by (auto intro!: bdd_aboveI[of _ "l + e"])
hoelzl@54263
    33
  with S b show ?thesis
hoelzl@54263
    34
    unfolding th by (auto intro!: cSup_upper2 cSup_least)
hoelzl@51518
    35
qed
hoelzl@51518
    36
hoelzl@51518
    37
lemma cInf_asclose: (* TODO: is this really needed? *)
hoelzl@51518
    38
  fixes S :: "real set"
wenzelm@53399
    39
  assumes S: "S \<noteq> {}"
wenzelm@53399
    40
    and b: "\<forall>x\<in>S. \<bar>x - l\<bar> \<le> e"
wenzelm@53399
    41
  shows "\<bar>Inf S - l\<bar> \<le> e"
hoelzl@51518
    42
proof -
hoelzl@51518
    43
  have "\<bar>- Sup (uminus ` S) - l\<bar> =  \<bar>Sup (uminus ` S) - (-l)\<bar>"
hoelzl@51518
    44
    by auto
wenzelm@53399
    45
  also have "\<dots> \<le> e"
wenzelm@53399
    46
    apply (rule cSup_asclose)
haftmann@54230
    47
    using abs_minus_add_cancel b by (auto simp add: S)
hoelzl@51518
    48
  finally have "\<bar>- Sup (uminus ` S) - l\<bar> \<le> e" .
wenzelm@53399
    49
  then show ?thesis
hoelzl@51518
    50
    by (simp add: Inf_real_def)
hoelzl@51518
    51
qed
hoelzl@51518
    52
wenzelm@53399
    53
lemma cSup_finite_ge_iff:
wenzelm@53399
    54
  fixes S :: "real set"
wenzelm@53399
    55
  shows "finite S \<Longrightarrow> S \<noteq> {} \<Longrightarrow> a \<le> Sup S \<longleftrightarrow> (\<exists>x\<in>S. a \<le> x)"
hoelzl@51518
    56
  by (metis cSup_eq_Max Max_ge_iff)
hoelzl@51475
    57
wenzelm@53399
    58
lemma cSup_finite_le_iff:
wenzelm@53399
    59
  fixes S :: "real set"
wenzelm@53399
    60
  shows "finite S \<Longrightarrow> S \<noteq> {} \<Longrightarrow> a \<ge> Sup S \<longleftrightarrow> (\<forall>x\<in>S. a \<ge> x)"
hoelzl@51518
    61
  by (metis cSup_eq_Max Max_le_iff)
hoelzl@51518
    62
wenzelm@53399
    63
lemma cInf_finite_ge_iff:
wenzelm@53399
    64
  fixes S :: "real set"
wenzelm@53399
    65
  shows "finite S \<Longrightarrow> S \<noteq> {} \<Longrightarrow> a \<le> Inf S \<longleftrightarrow> (\<forall>x\<in>S. a \<le> x)"
hoelzl@51518
    66
  by (metis cInf_eq_Min Min_ge_iff)
hoelzl@51518
    67
wenzelm@53399
    68
lemma cInf_finite_le_iff:
wenzelm@53399
    69
  fixes S :: "real set"
wenzelm@53399
    70
  shows "finite S \<Longrightarrow> S \<noteq> {} \<Longrightarrow> a \<ge> Inf S \<longleftrightarrow> (\<exists>x\<in>S. a \<ge> x)"
hoelzl@51518
    71
  by (metis cInf_eq_Min Min_le_iff)
hoelzl@51475
    72
hoelzl@37489
    73
(*declare not_less[simp] not_le[simp]*)
hoelzl@37489
    74
hoelzl@37489
    75
lemmas scaleR_simps = scaleR_zero_left scaleR_minus_left scaleR_left_diff_distrib
hoelzl@37489
    76
  scaleR_zero_right scaleR_minus_right scaleR_right_diff_distrib scaleR_eq_0_iff
huffman@44282
    77
  scaleR_cancel_left scaleR_cancel_right scaleR_add_right scaleR_add_left real_vector_class.scaleR_one
hoelzl@37489
    78
hoelzl@37489
    79
lemma real_arch_invD:
hoelzl@37489
    80
  "0 < (e::real) \<Longrightarrow> (\<exists>n::nat. n \<noteq> 0 \<and> 0 < inverse (real n) \<and> inverse (real n) < e)"
wenzelm@49675
    81
  by (subst(asm) real_arch_inv)
wenzelm@49675
    82
wenzelm@49675
    83
himmelma@36243
    84
subsection {* Sundries *}
himmelma@36243
    85
himmelma@35172
    86
lemma conjunctD2: assumes "a \<and> b" shows a b using assms by auto
himmelma@35172
    87
lemma conjunctD3: assumes "a \<and> b \<and> c" shows a b c using assms by auto
himmelma@35172
    88
lemma conjunctD4: assumes "a \<and> b \<and> c \<and> d" shows a b c d using assms by auto
himmelma@35172
    89
lemma conjunctD5: assumes "a \<and> b \<and> c \<and> d \<and> e" shows a b c d e using assms by auto
himmelma@35172
    90
wenzelm@53399
    91
declare norm_triangle_ineq4[intro]
wenzelm@53399
    92
wenzelm@53399
    93
lemma simple_image: "{f x |x . x \<in> s} = f ` s"
wenzelm@53399
    94
  by blast
himmelma@36243
    95
wenzelm@49970
    96
lemma linear_simps:
wenzelm@49970
    97
  assumes "bounded_linear f"
wenzelm@49970
    98
  shows
wenzelm@49970
    99
    "f (a + b) = f a + f b"
wenzelm@49970
   100
    "f (a - b) = f a - f b"
wenzelm@49970
   101
    "f 0 = 0"
wenzelm@49970
   102
    "f (- a) = - f a"
wenzelm@49970
   103
    "f (s *\<^sub>R v) = s *\<^sub>R (f v)"
huffman@53600
   104
proof -
huffman@53600
   105
  interpret f: bounded_linear f by fact
huffman@53600
   106
  show "f (a + b) = f a + f b" by (rule f.add)
huffman@53600
   107
  show "f (a - b) = f a - f b" by (rule f.diff)
huffman@53600
   108
  show "f 0 = 0" by (rule f.zero)
huffman@53600
   109
  show "f (- a) = - f a" by (rule f.minus)
huffman@53600
   110
  show "f (s *\<^sub>R v) = s *\<^sub>R (f v)" by (rule f.scaleR)
huffman@53600
   111
qed
wenzelm@49675
   112
wenzelm@49675
   113
lemma bounded_linearI:
wenzelm@49675
   114
  assumes "\<And>x y. f (x + y) = f x + f y"
wenzelm@53399
   115
    and "\<And>r x. f (r *\<^sub>R x) = r *\<^sub>R f x"
wenzelm@53399
   116
    and "\<And>x. norm (f x) \<le> norm x * K"
himmelma@36243
   117
  shows "bounded_linear f"
huffman@53600
   118
  using assms by (rule bounded_linear_intro) (* FIXME: duplicate *)
hoelzl@51348
   119
hoelzl@50526
   120
lemma bounded_linear_component [intro]: "bounded_linear (\<lambda>x::'a::euclidean_space. x \<bullet> k)"
hoelzl@50526
   121
  by (rule bounded_linear_inner_left)
himmelma@36243
   122
himmelma@36243
   123
lemma transitive_stepwise_lt_eq:
himmelma@36243
   124
  assumes "(\<And>x y z::nat. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z)"
wenzelm@53399
   125
  shows "((\<forall>m. \<forall>n>m. R m n) \<longleftrightarrow> (\<forall>n. R n (Suc n)))"
wenzelm@53399
   126
  (is "?l = ?r")
wenzelm@53408
   127
proof safe
wenzelm@49675
   128
  assume ?r
wenzelm@49675
   129
  fix n m :: nat
wenzelm@49675
   130
  assume "m < n"
wenzelm@49675
   131
  then show "R m n"
wenzelm@49675
   132
  proof (induct n arbitrary: m)
wenzelm@53399
   133
    case 0
wenzelm@53399
   134
    then show ?case by auto
wenzelm@53399
   135
  next
wenzelm@49675
   136
    case (Suc n)
wenzelm@53399
   137
    show ?case
wenzelm@49675
   138
    proof (cases "m < n")
wenzelm@49675
   139
      case True
wenzelm@49675
   140
      show ?thesis
wenzelm@49675
   141
        apply (rule assms[OF Suc(1)[OF True]])
wenzelm@50945
   142
        using `?r`
wenzelm@50945
   143
        apply auto
wenzelm@49675
   144
        done
wenzelm@49675
   145
    next
wenzelm@49675
   146
      case False
wenzelm@53408
   147
      then have "m = n"
wenzelm@53408
   148
        using Suc(2) by auto
wenzelm@53408
   149
      then show ?thesis
wenzelm@53408
   150
        using `?r` by auto
wenzelm@49675
   151
    qed
wenzelm@53399
   152
  qed
wenzelm@49675
   153
qed auto
himmelma@36243
   154
himmelma@36243
   155
lemma transitive_stepwise_gt:
wenzelm@53408
   156
  assumes "\<And>x y z. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z" "\<And>n. R n (Suc n)"
himmelma@36243
   157
  shows "\<forall>n>m. R m n"
wenzelm@49675
   158
proof -
wenzelm@49675
   159
  have "\<forall>m. \<forall>n>m. R m n"
wenzelm@49675
   160
    apply (subst transitive_stepwise_lt_eq)
wenzelm@49675
   161
    apply (rule assms)
wenzelm@49675
   162
    apply assumption
wenzelm@49675
   163
    apply assumption
wenzelm@49675
   164
    using assms(2) apply auto
wenzelm@49675
   165
    done
wenzelm@49970
   166
  then show ?thesis by auto
wenzelm@49675
   167
qed
himmelma@36243
   168
himmelma@36243
   169
lemma transitive_stepwise_le_eq:
himmelma@36243
   170
  assumes "\<And>x. R x x" "\<And>x y z. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z"
wenzelm@53399
   171
  shows "(\<forall>m. \<forall>n\<ge>m. R m n) \<longleftrightarrow> (\<forall>n. R n (Suc n))"
wenzelm@53399
   172
  (is "?l = ?r")
wenzelm@49675
   173
proof safe
wenzelm@49675
   174
  assume ?r
wenzelm@49675
   175
  fix m n :: nat
wenzelm@49675
   176
  assume "m \<le> n"
wenzelm@53408
   177
  then show "R m n"
wenzelm@49675
   178
  proof (induct n arbitrary: m)
wenzelm@49970
   179
    case 0
wenzelm@49970
   180
    with assms show ?case by auto
wenzelm@49970
   181
  next
wenzelm@49675
   182
    case (Suc n)
wenzelm@49675
   183
    show ?case
wenzelm@49675
   184
    proof (cases "m \<le> n")
wenzelm@49675
   185
      case True
wenzelm@49675
   186
      show ?thesis
wenzelm@49675
   187
        apply (rule assms(2))
wenzelm@49675
   188
        apply (rule Suc(1)[OF True])
wenzelm@49675
   189
        using `?r` apply auto
wenzelm@49675
   190
        done
wenzelm@49675
   191
    next
wenzelm@49675
   192
      case False
wenzelm@53408
   193
      then have "m = Suc n"
wenzelm@53408
   194
        using Suc(2) by auto
wenzelm@53408
   195
      then show ?thesis
wenzelm@53408
   196
        using assms(1) by auto
wenzelm@49675
   197
    qed
wenzelm@49970
   198
  qed
wenzelm@49675
   199
qed auto
himmelma@36243
   200
himmelma@36243
   201
lemma transitive_stepwise_le:
wenzelm@53408
   202
  assumes "\<And>x. R x x" "\<And>x y z. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z"
wenzelm@53408
   203
    and "\<And>n. R n (Suc n)"
himmelma@36243
   204
  shows "\<forall>n\<ge>m. R m n"
wenzelm@49675
   205
proof -
wenzelm@49675
   206
  have "\<forall>m. \<forall>n\<ge>m. R m n"
wenzelm@49675
   207
    apply (subst transitive_stepwise_le_eq)
wenzelm@49675
   208
    apply (rule assms)
wenzelm@49675
   209
    apply (rule assms,assumption,assumption)
wenzelm@53408
   210
    using assms(3)
wenzelm@53408
   211
    apply auto
wenzelm@49675
   212
    done
wenzelm@49970
   213
  then show ?thesis by auto
wenzelm@49675
   214
qed
wenzelm@49675
   215
himmelma@36243
   216
himmelma@35172
   217
subsection {* Some useful lemmas about intervals. *}
himmelma@35172
   218
wenzelm@53408
   219
abbreviation One :: "'a::euclidean_space"
wenzelm@53408
   220
  where "One \<equiv> \<Sum>Basis"
hoelzl@50526
   221
immler@56188
   222
lemma empty_as_interval: "{} = cbox One (0::'a::euclidean_space)"
immler@56188
   223
  using nonempty_Basis
immler@56188
   224
  by (fastforce simp add: set_eq_iff mem_box)
himmelma@35172
   225
immler@54781
   226
lemma One_nonneg: "0 \<le> (One::'a::ordered_euclidean_space)"
immler@54781
   227
  by (auto intro: setsum_nonneg)
immler@54781
   228
wenzelm@53399
   229
lemma interior_subset_union_intervals:
immler@56188
   230
  assumes "i = cbox a b"
immler@56188
   231
    and "j = cbox c d"
wenzelm@53399
   232
    and "interior j \<noteq> {}"
wenzelm@53399
   233
    and "i \<subseteq> j \<union> s"
wenzelm@53399
   234
    and "interior i \<inter> interior j = {}"
wenzelm@49675
   235
  shows "interior i \<subseteq> interior s"
wenzelm@49675
   236
proof -
immler@56188
   237
  have "box a b \<inter> cbox c d = {}"
immler@56188
   238
     using inter_interval_mixed_eq_empty[of c d a b] and assms(3,5)
immler@56188
   239
     unfolding assms(1,2) interior_cbox by auto
wenzelm@49675
   240
  moreover
immler@56188
   241
  have "box a b \<subseteq> cbox c d \<union> s"
immler@56188
   242
    apply (rule order_trans,rule box_subset_cbox)
wenzelm@49970
   243
    using assms(4) unfolding assms(1,2)
wenzelm@49970
   244
    apply auto
wenzelm@49970
   245
    done
wenzelm@49675
   246
  ultimately
wenzelm@49675
   247
  show ?thesis
wenzelm@49675
   248
    apply -
wenzelm@49970
   249
    apply (rule interior_maximal)
wenzelm@49970
   250
    defer
wenzelm@49675
   251
    apply (rule open_interior)
immler@56188
   252
    unfolding assms(1,2) interior_cbox
wenzelm@49970
   253
    apply auto
wenzelm@49675
   254
    done
wenzelm@49675
   255
qed
wenzelm@49675
   256
wenzelm@49675
   257
lemma inter_interior_unions_intervals:
immler@56188
   258
  fixes f::"('a::euclidean_space) set set"
wenzelm@53399
   259
  assumes "finite f"
wenzelm@53399
   260
    and "open s"
immler@56188
   261
    and "\<forall>t\<in>f. \<exists>a b. t = cbox a b"
wenzelm@53399
   262
    and "\<forall>t\<in>f. s \<inter> (interior t) = {}"
wenzelm@53399
   263
  shows "s \<inter> interior (\<Union>f) = {}"
wenzelm@53399
   264
proof (rule ccontr, unfold ex_in_conv[symmetric])
wenzelm@49970
   265
  case goal1
wenzelm@49970
   266
  have lem1: "\<And>x e s U. ball x e \<subseteq> s \<inter> interior U \<longleftrightarrow> ball x e \<subseteq> s \<inter> U"
wenzelm@49970
   267
    apply rule
wenzelm@49970
   268
    defer
wenzelm@49970
   269
    apply (rule_tac Int_greatest)
wenzelm@49970
   270
    unfolding open_subset_interior[OF open_ball]
wenzelm@49970
   271
    using interior_subset
wenzelm@49970
   272
    apply auto
wenzelm@49970
   273
    done
wenzelm@49970
   274
  have lem2: "\<And>x s P. \<exists>x\<in>s. P x \<Longrightarrow> \<exists>x\<in>insert x s. P x" by auto
immler@56188
   275
  have "\<And>f. finite f \<Longrightarrow> \<forall>t\<in>f. \<exists>a b. t = cbox a b \<Longrightarrow>
wenzelm@53408
   276
    \<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"
wenzelm@49970
   277
  proof -
wenzelm@49970
   278
    case goal1
wenzelm@49970
   279
    then show ?case
wenzelm@49970
   280
    proof (induct rule: finite_induct)
wenzelm@53399
   281
      case empty
wenzelm@53408
   282
      obtain x where "x \<in> s \<inter> interior (\<Union>{})"
wenzelm@53408
   283
        using empty(2) ..
wenzelm@53399
   284
      then have False
wenzelm@53399
   285
        unfolding Union_empty interior_empty by auto
wenzelm@53399
   286
      then show ?case by auto
wenzelm@49970
   287
    next
wenzelm@53399
   288
      case (insert i f)
wenzelm@53408
   289
      obtain x where x: "x \<in> s \<inter> interior (\<Union>insert i f)"
wenzelm@53408
   290
        using insert(5) ..
wenzelm@53408
   291
      then obtain e where e: "0 < e \<and> ball x e \<subseteq> s \<inter> interior (\<Union>insert i f)"
wenzelm@53408
   292
        unfolding open_contains_ball_eq[OF open_Int[OF assms(2) open_interior], rule_format] ..
immler@56188
   293
      obtain a where "\<exists>b. i = cbox a b"
wenzelm@53399
   294
        using insert(4)[rule_format,OF insertI1] ..
immler@56188
   295
      then obtain b where ab: "i = cbox a b" ..
wenzelm@49970
   296
      show ?case
wenzelm@53408
   297
      proof (cases "x \<in> i")
wenzelm@49970
   298
        case False
immler@56188
   299
        then have "x \<in> UNIV - cbox a b"
wenzelm@53399
   300
          unfolding ab by auto
immler@56188
   301
        then obtain d where "0 < d \<and> ball x d \<subseteq> UNIV - cbox a b"
immler@56188
   302
          unfolding open_contains_ball_eq[OF open_Diff[OF open_UNIV closed_cbox],rule_format] ..
wenzelm@53399
   303
        then have "0 < d" "ball x (min d e) \<subseteq> UNIV - i"
wenzelm@53399
   304
          unfolding ab ball_min_Int by auto
wenzelm@53399
   305
        then have "ball x (min d e) \<subseteq> s \<inter> interior (\<Union>f)"
wenzelm@49970
   306
          using e unfolding lem1 unfolding  ball_min_Int by auto
wenzelm@53399
   307
        then have "x \<in> s \<inter> interior (\<Union>f)" using `d>0` e by auto
wenzelm@53399
   308
        then have "\<exists>t\<in>f. \<exists>x e. 0 < e \<and> ball x e \<subseteq> s \<inter> t"
wenzelm@49970
   309
          apply -
wenzelm@49970
   310
          apply (rule insert(3))
wenzelm@49970
   311
          using insert(4)
wenzelm@49970
   312
          apply auto
wenzelm@49970
   313
          done
wenzelm@53399
   314
        then show ?thesis by auto
wenzelm@49970
   315
      next
wenzelm@49970
   316
        case True show ?thesis
immler@54775
   317
        proof (cases "x\<in>box a b")
wenzelm@49970
   318
          case True
immler@54775
   319
          then obtain d where "0 < d \<and> ball x d \<subseteq> box a b"
immler@56188
   320
            unfolding open_contains_ball_eq[OF open_box,rule_format] ..
wenzelm@53399
   321
          then show ?thesis
wenzelm@49970
   322
            apply (rule_tac x=i in bexI, rule_tac x=x in exI, rule_tac x="min d e" in exI)
wenzelm@49970
   323
            unfolding ab
immler@56188
   324
            using box_subset_cbox[of a b] and e
wenzelm@50945
   325
            apply fastforce+
wenzelm@49970
   326
            done
wenzelm@49970
   327
        next
wenzelm@49970
   328
          case False
wenzelm@53399
   329
          then obtain k where "x\<bullet>k \<le> a\<bullet>k \<or> x\<bullet>k \<ge> b\<bullet>k" and k: "k \<in> Basis"
immler@56188
   330
            unfolding mem_box by (auto simp add: not_less)
wenzelm@53399
   331
          then have "x\<bullet>k = a\<bullet>k \<or> x\<bullet>k = b\<bullet>k"
immler@56188
   332
            using True unfolding ab and mem_box
hoelzl@50526
   333
              apply (erule_tac x = k in ballE)
wenzelm@49970
   334
              apply auto
wenzelm@49970
   335
              done
wenzelm@53399
   336
          then have "\<exists>x. ball x (e/2) \<subseteq> s \<inter> (\<Union>f)"
wenzelm@53399
   337
          proof (rule disjE)
hoelzl@50526
   338
            let ?z = "x - (e/2) *\<^sub>R k"
hoelzl@50526
   339
            assume as: "x\<bullet>k = a\<bullet>k"
wenzelm@49970
   340
            have "ball ?z (e / 2) \<inter> i = {}"
wenzelm@49970
   341
              apply (rule ccontr)
wenzelm@53399
   342
              unfolding ex_in_conv[symmetric]
wenzelm@53399
   343
              apply (erule exE)
wenzelm@53399
   344
            proof -
wenzelm@49970
   345
              fix y
wenzelm@49970
   346
              assume "y \<in> ball ?z (e / 2) \<inter> i"
wenzelm@53399
   347
              then have "dist ?z y < e/2" and yi:"y\<in>i" by auto
wenzelm@53399
   348
              then have "\<bar>(?z - y) \<bullet> k\<bar> < e/2"
hoelzl@50526
   349
                using Basis_le_norm[OF k, of "?z - y"] unfolding dist_norm by auto
wenzelm@53399
   350
              then have "y\<bullet>k < a\<bullet>k"
wenzelm@53399
   351
                using e[THEN conjunct1] k
haftmann@54230
   352
                by (auto simp add: field_simps abs_less_iff as inner_Basis inner_simps)
wenzelm@53399
   353
              then have "y \<notin> i"
immler@56188
   354
                unfolding ab mem_box by (auto intro!: bexI[OF _ k])
wenzelm@53399
   355
              then show False using yi by auto
wenzelm@49970
   356
            qed
wenzelm@49970
   357
            moreover
wenzelm@49970
   358
            have "ball ?z (e/2) \<subseteq> s \<inter> (\<Union>insert i f)"
wenzelm@53399
   359
              apply (rule order_trans[OF _ e[THEN conjunct2, unfolded lem1]])
wenzelm@49970
   360
            proof
wenzelm@49970
   361
              fix y
wenzelm@53399
   362
              assume as: "y \<in> ball ?z (e/2)"
hoelzl@50526
   363
              have "norm (x - y) \<le> \<bar>e\<bar> / 2 + norm (x - y - (e / 2) *\<^sub>R k)"
wenzelm@49970
   364
                apply -
hoelzl@50526
   365
                apply (rule order_trans,rule norm_triangle_sub[of "x - y" "(e/2) *\<^sub>R k"])
hoelzl@50526
   366
                unfolding norm_scaleR norm_Basis[OF k]
wenzelm@49970
   367
                apply auto
wenzelm@49970
   368
                done
wenzelm@49970
   369
              also have "\<dots> < \<bar>e\<bar> / 2 + \<bar>e\<bar> / 2"
wenzelm@49970
   370
                apply (rule add_strict_left_mono)
wenzelm@50945
   371
                using as
wenzelm@50945
   372
                unfolding mem_ball dist_norm
wenzelm@50945
   373
                using e
wenzelm@50945
   374
                apply (auto simp add: field_simps)
wenzelm@49970
   375
                done
wenzelm@53399
   376
              finally show "y \<in> ball x e"
wenzelm@49970
   377
                unfolding mem_ball dist_norm using e by (auto simp add:field_simps)
wenzelm@49970
   378
            qed
wenzelm@49970
   379
            ultimately show ?thesis
wenzelm@49970
   380
              apply (rule_tac x="?z" in exI)
wenzelm@49970
   381
              unfolding Union_insert
wenzelm@49970
   382
              apply auto
wenzelm@49970
   383
              done
wenzelm@49970
   384
          next
hoelzl@50526
   385
            let ?z = "x + (e/2) *\<^sub>R k"
hoelzl@50526
   386
            assume as: "x\<bullet>k = b\<bullet>k"
wenzelm@49970
   387
            have "ball ?z (e / 2) \<inter> i = {}"
wenzelm@49970
   388
              apply (rule ccontr)
wenzelm@53399
   389
              unfolding ex_in_conv[symmetric]
wenzelm@53408
   390
              apply (erule exE)
wenzelm@53408
   391
            proof -
wenzelm@49970
   392
              fix y
wenzelm@49970
   393
              assume "y \<in> ball ?z (e / 2) \<inter> i"
wenzelm@53408
   394
              then have "dist ?z y < e/2" and yi: "y \<in> i"
wenzelm@53408
   395
                by auto
wenzelm@53399
   396
              then have "\<bar>(?z - y) \<bullet> k\<bar> < e/2"
wenzelm@53399
   397
                using Basis_le_norm[OF k, of "?z - y"]
wenzelm@53399
   398
                unfolding dist_norm by auto
wenzelm@53399
   399
              then have "y\<bullet>k > b\<bullet>k"
wenzelm@53399
   400
                using e[THEN conjunct1] k
wenzelm@53399
   401
                by (auto simp add:field_simps inner_simps inner_Basis as)
wenzelm@53399
   402
              then have "y \<notin> i"
immler@56188
   403
                unfolding ab mem_box by (auto intro!: bexI[OF _ k])
wenzelm@53399
   404
              then show False using yi by auto
wenzelm@49970
   405
            qed
wenzelm@49970
   406
            moreover
wenzelm@49970
   407
            have "ball ?z (e/2) \<subseteq> s \<inter> (\<Union>insert i f)"
wenzelm@49970
   408
              apply (rule order_trans[OF _ e[THEN conjunct2, unfolded lem1]])
wenzelm@49970
   409
            proof
wenzelm@49970
   410
              fix y
wenzelm@49970
   411
              assume as: "y\<in> ball ?z (e/2)"
hoelzl@50526
   412
              have "norm (x - y) \<le> \<bar>e\<bar> / 2 + norm (x - y + (e / 2) *\<^sub>R k)"
wenzelm@49970
   413
                apply -
wenzelm@53399
   414
                apply (rule order_trans,rule norm_triangle_sub[of "x - y" "- (e/2) *\<^sub>R k"])
wenzelm@49970
   415
                unfolding norm_scaleR
hoelzl@50526
   416
                apply (auto simp: k)
wenzelm@49970
   417
                done
wenzelm@49970
   418
              also have "\<dots> < \<bar>e\<bar> / 2 + \<bar>e\<bar> / 2"
wenzelm@49970
   419
                apply (rule add_strict_left_mono)
wenzelm@49970
   420
                using as unfolding mem_ball dist_norm
wenzelm@49970
   421
                using e apply (auto simp add: field_simps)
wenzelm@49970
   422
                done
wenzelm@53399
   423
              finally show "y \<in> ball x e"
wenzelm@53399
   424
                unfolding mem_ball dist_norm using e by (auto simp add:field_simps)
wenzelm@49970
   425
            qed
wenzelm@49970
   426
            ultimately show ?thesis
wenzelm@49970
   427
              apply (rule_tac x="?z" in exI)
wenzelm@49970
   428
              unfolding Union_insert
wenzelm@49970
   429
              apply auto
wenzelm@49970
   430
              done
wenzelm@53399
   431
          qed
wenzelm@53408
   432
          then obtain x where "ball x (e / 2) \<subseteq> s \<inter> \<Union>f" ..
wenzelm@53399
   433
          then have "x \<in> s \<inter> interior (\<Union>f)"
wenzelm@53408
   434
            unfolding lem1[where U="\<Union>f", symmetric]
wenzelm@49970
   435
            using centre_in_ball e[THEN conjunct1] by auto
wenzelm@53399
   436
          then show ?thesis
wenzelm@49970
   437
            apply -
wenzelm@49970
   438
            apply (rule lem2, rule insert(3))
wenzelm@53399
   439
            using insert(4)
wenzelm@53399
   440
            apply auto
wenzelm@49970
   441
            done
wenzelm@49970
   442
        qed
wenzelm@49970
   443
      qed
wenzelm@49970
   444
    qed
wenzelm@49970
   445
  qed
wenzelm@53408
   446
  from this[OF assms(1,3) goal1]
wenzelm@53408
   447
  obtain t x e where "t \<in> f" "0 < e" "ball x e \<subseteq> s \<inter> t"
wenzelm@53408
   448
    by blast
wenzelm@53408
   449
  then have "x \<in> s" "x \<in> interior t"
wenzelm@53399
   450
    using open_subset_interior[OF open_ball, of x e t]
wenzelm@53408
   451
    by auto
wenzelm@53399
   452
  then show False
wenzelm@53399
   453
    using `t \<in> f` assms(4) by auto
wenzelm@49970
   454
qed
wenzelm@49970
   455
immler@56188
   456
subsection {* Bounds on intervals where they exist. *}
immler@56188
   457
immler@56188
   458
definition interval_upperbound :: "('a::euclidean_space) set \<Rightarrow> 'a"
immler@56188
   459
  where "interval_upperbound s = (\<Sum>i\<in>Basis. (SUP x:s. x\<bullet>i) *\<^sub>R i)"
immler@56188
   460
immler@56188
   461
definition interval_lowerbound :: "('a::euclidean_space) set \<Rightarrow> 'a"
immler@56188
   462
   where "interval_lowerbound s = (\<Sum>i\<in>Basis. (INF x:s. x\<bullet>i) *\<^sub>R i)"
immler@56188
   463
immler@56188
   464
lemma interval_upperbound[simp]:
immler@56188
   465
  "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i \<Longrightarrow>
immler@56188
   466
    interval_upperbound (cbox a b) = (b::'a::euclidean_space)"
immler@56188
   467
  unfolding interval_upperbound_def euclidean_representation_setsum cbox_def SUP_def
immler@56188
   468
  by (safe intro!: cSup_eq) auto
immler@56188
   469
immler@56188
   470
lemma interval_lowerbound[simp]:
immler@56188
   471
  "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i \<Longrightarrow>
immler@56188
   472
    interval_lowerbound (cbox a b) = (a::'a::euclidean_space)"
immler@56188
   473
  unfolding interval_lowerbound_def euclidean_representation_setsum cbox_def INF_def
immler@56188
   474
  by (safe intro!: cInf_eq) auto
immler@56188
   475
immler@56188
   476
lemmas interval_bounds = interval_upperbound interval_lowerbound
immler@56188
   477
immler@56188
   478
lemma
immler@56188
   479
  fixes X::"real set"
immler@56188
   480
  shows interval_upperbound_real[simp]: "interval_upperbound X = Sup X"
immler@56188
   481
    and interval_lowerbound_real[simp]: "interval_lowerbound X = Inf X"
immler@56188
   482
  by (auto simp: interval_upperbound_def interval_lowerbound_def SUP_def INF_def)
immler@56188
   483
immler@56188
   484
lemma interval_bounds'[simp]:
immler@56188
   485
  assumes "cbox a b \<noteq> {}"
immler@56188
   486
  shows "interval_upperbound (cbox a b) = b"
immler@56188
   487
    and "interval_lowerbound (cbox a b) = a"
immler@56188
   488
  using assms unfolding box_ne_empty by auto
wenzelm@53399
   489
himmelma@35172
   490
subsection {* Content (length, area, volume...) of an interval. *}
himmelma@35172
   491
immler@56188
   492
definition "content (s::('a::euclidean_space) set) =
immler@56188
   493
  (if s = {} then 0 else (\<Prod>i\<in>Basis. (interval_upperbound s)\<bullet>i - (interval_lowerbound s)\<bullet>i))"
immler@56188
   494
immler@56188
   495
lemma interval_not_empty: "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i \<Longrightarrow> cbox a b \<noteq> {}"
immler@56188
   496
  unfolding box_eq_empty unfolding not_ex not_less by auto
immler@56188
   497
immler@56188
   498
lemma content_cbox:
immler@56188
   499
  fixes a :: "'a::euclidean_space"
hoelzl@50526
   500
  assumes "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i"
immler@56188
   501
  shows "content (cbox a b) = (\<Prod>i\<in>Basis. b\<bullet>i - a\<bullet>i)"
wenzelm@49970
   502
  using interval_not_empty[OF assms]
immler@54777
   503
  unfolding content_def
immler@56188
   504
  by (auto simp: )
immler@56188
   505
immler@56188
   506
lemma content_cbox':
immler@56188
   507
  fixes a :: "'a::euclidean_space"
immler@56188
   508
  assumes "cbox a b \<noteq> {}"
immler@56188
   509
  shows "content (cbox a b) = (\<Prod>i\<in>Basis. b\<bullet>i - a\<bullet>i)"
immler@56188
   510
  apply (rule content_cbox)
wenzelm@50945
   511
  using assms
immler@56188
   512
  unfolding box_ne_empty
wenzelm@49970
   513
  apply assumption
wenzelm@49970
   514
  done
wenzelm@49970
   515
wenzelm@53408
   516
lemma content_real: "a \<le> b \<Longrightarrow> content {a..b} = b - a"
immler@56188
   517
  by (auto simp: interval_upperbound_def interval_lowerbound_def SUP_def INF_def content_def)
immler@56188
   518
immler@56188
   519
lemma content_closed_interval:
immler@56188
   520
  fixes a :: "'a::ordered_euclidean_space"
immler@56188
   521
  assumes "a \<le> b"
immler@56188
   522
  shows "content {a .. b} = (\<Prod>i\<in>Basis. b\<bullet>i - a\<bullet>i)"
immler@56188
   523
  using content_cbox[of a b] assms
immler@56188
   524
  by (simp add: cbox_interval eucl_le[where 'a='a])
hoelzl@37489
   525
hoelzl@50104
   526
lemma content_singleton[simp]: "content {a} = 0"
hoelzl@50104
   527
proof -
immler@56188
   528
  have "content (cbox a a) = 0"
immler@56188
   529
    by (subst content_cbox) (auto simp: ex_in_conv)
immler@56188
   530
  then show ?thesis by (simp add: cbox_sing)
immler@56188
   531
qed
immler@56188
   532
immler@56188
   533
lemma content_unit[intro]: "content(cbox 0 (One::'a::euclidean_space)) = 1"
immler@56188
   534
 proof -
immler@56188
   535
   have *: "\<forall>i\<in>Basis. (0::'a)\<bullet>i \<le> (One::'a)\<bullet>i"
immler@56188
   536
    by auto
immler@56188
   537
  have "0 \<in> cbox 0 (One::'a)"
immler@56188
   538
    unfolding mem_box by auto
immler@56188
   539
  then show ?thesis
immler@56188
   540
     unfolding content_def interval_bounds[OF *] using setprod_1 by auto
immler@56188
   541
 qed
wenzelm@49970
   542
wenzelm@49970
   543
lemma content_pos_le[intro]:
immler@56188
   544
  fixes a::"'a::euclidean_space"
immler@56188
   545
  shows "0 \<le> content (cbox a b)"
immler@56188
   546
proof (cases "cbox a b = {}")
immler@56188
   547
  case False
immler@56188
   548
  then have *: "\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i"
immler@56188
   549
    unfolding box_ne_empty .
immler@56188
   550
  have "0 \<le> (\<Prod>i\<in>Basis. interval_upperbound (cbox a b) \<bullet> i - interval_lowerbound (cbox a b) \<bullet> i)"
immler@56188
   551
    apply (rule setprod_nonneg)
immler@56188
   552
    unfolding interval_bounds[OF *]
immler@56188
   553
    using *
immler@56188
   554
    apply auto
immler@56188
   555
    done
immler@56188
   556
  also have "\<dots> = content (cbox a b)" using False by (simp add: content_def)
immler@56188
   557
  finally show ?thesis .
immler@56188
   558
qed (simp add: content_def)
wenzelm@49970
   559
wenzelm@49970
   560
lemma content_pos_lt:
immler@56188
   561
  fixes a :: "'a::euclidean_space"
hoelzl@50526
   562
  assumes "\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i"
immler@56188
   563
  shows "0 < content (cbox a b)"
immler@54777
   564
  using assms
immler@56188
   565
  by (auto simp: content_def box_eq_empty intro!: setprod_pos)
wenzelm@49970
   566
wenzelm@53408
   567
lemma content_eq_0:
immler@56188
   568
  "content (cbox a b) = 0 \<longleftrightarrow> (\<exists>i\<in>Basis. b\<bullet>i \<le> a\<bullet>i)"
immler@56188
   569
  by (auto simp: content_def box_eq_empty intro!: setprod_pos bexI)
himmelma@35172
   570
wenzelm@53408
   571
lemma cond_cases: "(P \<Longrightarrow> Q x) \<Longrightarrow> (\<not> P \<Longrightarrow> Q y) \<Longrightarrow> Q (if P then x else y)"
wenzelm@53399
   572
  by auto
himmelma@35172
   573
immler@56188
   574
lemma content_cbox_cases:
immler@56188
   575
  "content (cbox a (b::'a::euclidean_space)) =
hoelzl@50526
   576
    (if \<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i then setprod (\<lambda>i. b\<bullet>i - a\<bullet>i) Basis else 0)"
immler@56188
   577
  by (auto simp: not_le content_eq_0 intro: less_imp_le content_cbox)
immler@56188
   578
immler@56188
   579
lemma content_eq_0_interior: "content (cbox a b) = 0 \<longleftrightarrow> interior(cbox a b) = {}"
immler@56188
   580
  unfolding content_eq_0 interior_cbox box_eq_empty
wenzelm@53408
   581
  by auto
himmelma@35172
   582
wenzelm@53399
   583
lemma content_pos_lt_eq:
immler@56188
   584
  "0 < content (cbox a (b::'a::euclidean_space)) \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i)"
wenzelm@49970
   585
  apply rule
wenzelm@49970
   586
  defer
wenzelm@49970
   587
  apply (rule content_pos_lt, assumption)
wenzelm@49970
   588
proof -
immler@56188
   589
  assume "0 < content (cbox a b)"
immler@56188
   590
  then have "content (cbox a b) \<noteq> 0" by auto
wenzelm@53399
   591
  then show "\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i"
wenzelm@49970
   592
    unfolding content_eq_0 not_ex not_le by fastforce
wenzelm@49970
   593
qed
wenzelm@49970
   594
wenzelm@53399
   595
lemma content_empty [simp]: "content {} = 0"
wenzelm@53399
   596
  unfolding content_def by auto
himmelma@35172
   597
wenzelm@49698
   598
lemma content_subset:
immler@56188
   599
  assumes "cbox a b \<subseteq> cbox c d"
immler@56188
   600
  shows "content (cbox a b) \<le> content (cbox c d)"
immler@56188
   601
proof (cases "cbox a b = {}")
immler@56188
   602
  case True
immler@56188
   603
  then show ?thesis
immler@56188
   604
    using content_pos_le[of c d] by auto
immler@56188
   605
next
immler@56188
   606
  case False
immler@56188
   607
  then have ab_ne: "\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i"
immler@56188
   608
    unfolding box_ne_empty by auto
immler@56188
   609
  then have ab_ab: "a\<in>cbox a b" "b\<in>cbox a b"
immler@56188
   610
    unfolding mem_box by auto
immler@56188
   611
  have "cbox c d \<noteq> {}" using assms False by auto
immler@56188
   612
  then have cd_ne: "\<forall>i\<in>Basis. c \<bullet> i \<le> d \<bullet> i"
immler@56188
   613
    using assms unfolding box_ne_empty by auto
immler@56188
   614
  show ?thesis
immler@56188
   615
    unfolding content_def
immler@56188
   616
    unfolding interval_bounds[OF ab_ne] interval_bounds[OF cd_ne]
immler@56188
   617
    unfolding if_not_P[OF False] if_not_P[OF `cbox c d \<noteq> {}`]
immler@56188
   618
    apply (rule setprod_mono)
immler@56188
   619
    apply rule
immler@56188
   620
  proof
immler@56188
   621
    fix i :: 'a
immler@56188
   622
    assume i: "i \<in> Basis"
immler@56188
   623
    show "0 \<le> b \<bullet> i - a \<bullet> i"
immler@56188
   624
      using ab_ne[THEN bspec, OF i] i by auto
immler@56188
   625
    show "b \<bullet> i - a \<bullet> i \<le> d \<bullet> i - c \<bullet> i"
immler@56188
   626
      using assms[unfolded subset_eq mem_box,rule_format,OF ab_ab(2),of i]
immler@56188
   627
      using assms[unfolded subset_eq mem_box,rule_format,OF ab_ab(1),of i]
immler@56188
   628
      using i by auto
immler@56188
   629
  qed
immler@56188
   630
qed
immler@56188
   631
immler@56188
   632
lemma content_lt_nz: "0 < content (cbox a b) \<longleftrightarrow> content (cbox a b) \<noteq> 0"
nipkow@44890
   633
  unfolding content_pos_lt_eq content_eq_0 unfolding not_ex not_le by fastforce
himmelma@35172
   634
wenzelm@49698
   635
himmelma@35172
   636
subsection {* The notion of a gauge --- simply an open set containing the point. *}
himmelma@35172
   637
wenzelm@53408
   638
definition "gauge d \<longleftrightarrow> (\<forall>x. x \<in> d x \<and> open (d x))"
wenzelm@53399
   639
wenzelm@53399
   640
lemma gaugeI:
wenzelm@53399
   641
  assumes "\<And>x. x \<in> g x"
wenzelm@53399
   642
    and "\<And>x. open (g x)"
wenzelm@53399
   643
  shows "gauge g"
himmelma@35172
   644
  using assms unfolding gauge_def by auto
himmelma@35172
   645
wenzelm@53399
   646
lemma gaugeD[dest]:
wenzelm@53399
   647
  assumes "gauge d"
wenzelm@53399
   648
  shows "x \<in> d x"
wenzelm@53399
   649
    and "open (d x)"
wenzelm@49698
   650
  using assms unfolding gauge_def by auto
himmelma@35172
   651
himmelma@35172
   652
lemma gauge_ball_dependent: "\<forall>x. 0 < e x \<Longrightarrow> gauge (\<lambda>x. ball x (e x))"
wenzelm@53399
   653
  unfolding gauge_def by auto
wenzelm@53399
   654
wenzelm@53399
   655
lemma gauge_ball[intro]: "0 < e \<Longrightarrow> gauge (\<lambda>x. ball x e)"
wenzelm@53399
   656
  unfolding gauge_def by auto
himmelma@35172
   657
wenzelm@49698
   658
lemma gauge_trivial[intro]: "gauge (\<lambda>x. ball x 1)"
wenzelm@49698
   659
  by (rule gauge_ball) auto
himmelma@35172
   660
wenzelm@53408
   661
lemma gauge_inter[intro]: "gauge d1 \<Longrightarrow> gauge d2 \<Longrightarrow> gauge (\<lambda>x. d1 x \<inter> d2 x)"
wenzelm@53399
   662
  unfolding gauge_def by auto
himmelma@35172
   663
wenzelm@49698
   664
lemma gauge_inters:
wenzelm@53399
   665
  assumes "finite s"
wenzelm@53399
   666
    and "\<forall>d\<in>s. gauge (f d)"
wenzelm@53408
   667
  shows "gauge (\<lambda>x. \<Inter> {f d x | d. d \<in> s})"
wenzelm@49698
   668
proof -
wenzelm@53399
   669
  have *: "\<And>x. {f d x |d. d \<in> s} = (\<lambda>d. f d x) ` s"
wenzelm@53399
   670
    by auto
wenzelm@49698
   671
  show ?thesis
wenzelm@53399
   672
    unfolding gauge_def unfolding *
wenzelm@49698
   673
    using assms unfolding Ball_def Inter_iff mem_Collect_eq gauge_def by auto
wenzelm@49698
   674
qed
wenzelm@49698
   675
wenzelm@53399
   676
lemma gauge_existence_lemma:
wenzelm@53408
   677
  "(\<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)"
wenzelm@53399
   678
  by (metis zero_less_one)
wenzelm@49698
   679
himmelma@35172
   680
himmelma@35172
   681
subsection {* Divisions. *}
himmelma@35172
   682
wenzelm@53408
   683
definition division_of (infixl "division'_of" 40)
wenzelm@53408
   684
where
wenzelm@53399
   685
  "s division_of i \<longleftrightarrow>
wenzelm@53399
   686
    finite s \<and>
immler@56188
   687
    (\<forall>k\<in>s. k \<subseteq> i \<and> k \<noteq> {} \<and> (\<exists>a b. k = cbox a b)) \<and>
wenzelm@53399
   688
    (\<forall>k1\<in>s. \<forall>k2\<in>s. k1 \<noteq> k2 \<longrightarrow> interior(k1) \<inter> interior(k2) = {}) \<and>
wenzelm@53399
   689
    (\<Union>s = i)"
himmelma@35172
   690
wenzelm@49698
   691
lemma division_ofD[dest]:
wenzelm@49698
   692
  assumes "s division_of i"
wenzelm@53408
   693
  shows "finite s"
wenzelm@53408
   694
    and "\<And>k. k \<in> s \<Longrightarrow> k \<subseteq> i"
wenzelm@53408
   695
    and "\<And>k. k \<in> s \<Longrightarrow> k \<noteq> {}"
immler@56188
   696
    and "\<And>k. k \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
wenzelm@53408
   697
    and "\<And>k1 k2. k1 \<in> s \<Longrightarrow> k2 \<in> s \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow> interior(k1) \<inter> interior(k2) = {}"
wenzelm@53408
   698
    and "\<Union>s = i"
wenzelm@49698
   699
  using assms unfolding division_of_def by auto
himmelma@35172
   700
himmelma@35172
   701
lemma division_ofI:
wenzelm@53408
   702
  assumes "finite s"
wenzelm@53408
   703
    and "\<And>k. k \<in> s \<Longrightarrow> k \<subseteq> i"
wenzelm@53408
   704
    and "\<And>k. k \<in> s \<Longrightarrow> k \<noteq> {}"
immler@56188
   705
    and "\<And>k. k \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
wenzelm@53408
   706
    and "\<And>k1 k2. k1 \<in> s \<Longrightarrow> k2 \<in> s \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow> interior k1 \<inter> interior k2 = {}"
wenzelm@53399
   707
    and "\<Union>s = i"
wenzelm@53399
   708
  shows "s division_of i"
wenzelm@53399
   709
  using assms unfolding division_of_def by auto
himmelma@35172
   710
himmelma@35172
   711
lemma division_of_finite: "s division_of i \<Longrightarrow> finite s"
himmelma@35172
   712
  unfolding division_of_def by auto
himmelma@35172
   713
immler@56188
   714
lemma division_of_self[intro]: "cbox a b \<noteq> {} \<Longrightarrow> {cbox a b} division_of (cbox a b)"
himmelma@35172
   715
  unfolding division_of_def by auto
himmelma@35172
   716
wenzelm@53399
   717
lemma division_of_trivial[simp]: "s division_of {} \<longleftrightarrow> s = {}"
wenzelm@53399
   718
  unfolding division_of_def by auto
himmelma@35172
   719
wenzelm@49698
   720
lemma division_of_sing[simp]:
immler@56188
   721
  "s division_of cbox a (a::'a::euclidean_space) \<longleftrightarrow> s = {cbox a a}"
wenzelm@53399
   722
  (is "?l = ?r")
wenzelm@49698
   723
proof
wenzelm@49698
   724
  assume ?r
wenzelm@53399
   725
  moreover
wenzelm@53399
   726
  {
wenzelm@49698
   727
    assume "s = {{a}}"
wenzelm@53399
   728
    moreover fix k assume "k\<in>s"
immler@56188
   729
    ultimately have"\<exists>x y. k = cbox x y"
wenzelm@50945
   730
      apply (rule_tac x=a in exI)+
immler@56188
   731
      unfolding cbox_sing
wenzelm@50945
   732
      apply auto
wenzelm@50945
   733
      done
wenzelm@49698
   734
  }
wenzelm@53399
   735
  ultimately show ?l
immler@56188
   736
    unfolding division_of_def cbox_sing by auto
wenzelm@49698
   737
next
wenzelm@49698
   738
  assume ?l
immler@56188
   739
  note * = conjunctD4[OF this[unfolded division_of_def cbox_sing]]
wenzelm@53399
   740
  {
wenzelm@53399
   741
    fix x
wenzelm@53399
   742
    assume x: "x \<in> s" have "x = {a}"
wenzelm@53408
   743
      using *(2)[rule_format,OF x] by auto
wenzelm@53399
   744
  }
wenzelm@53408
   745
  moreover have "s \<noteq> {}"
wenzelm@53408
   746
    using *(4) by auto
wenzelm@53408
   747
  ultimately show ?r
immler@56188
   748
    unfolding cbox_sing by auto
wenzelm@49698
   749
qed
himmelma@35172
   750
himmelma@35172
   751
lemma elementary_empty: obtains p where "p division_of {}"
himmelma@35172
   752
  unfolding division_of_trivial by auto
himmelma@35172
   753
immler@56188
   754
lemma elementary_interval: obtains p where "p division_of (cbox a b)"
wenzelm@49698
   755
  by (metis division_of_trivial division_of_self)
himmelma@35172
   756
himmelma@35172
   757
lemma division_contains: "s division_of i \<Longrightarrow> \<forall>x\<in>i. \<exists>k\<in>s. x \<in> k"
himmelma@35172
   758
  unfolding division_of_def by auto
himmelma@35172
   759
himmelma@35172
   760
lemma forall_in_division:
immler@56188
   761
  "d division_of i \<Longrightarrow> (\<forall>x\<in>d. P x) \<longleftrightarrow> (\<forall>a b. cbox a b \<in> d \<longrightarrow> P (cbox a b))"
nipkow@44890
   762
  unfolding division_of_def by fastforce
himmelma@35172
   763
wenzelm@53399
   764
lemma division_of_subset:
wenzelm@53399
   765
  assumes "p division_of (\<Union>p)"
wenzelm@53399
   766
    and "q \<subseteq> p"
wenzelm@53399
   767
  shows "q division_of (\<Union>q)"
wenzelm@53408
   768
proof (rule division_ofI)
wenzelm@53408
   769
  note * = division_ofD[OF assms(1)]
wenzelm@49698
   770
  show "finite q"
wenzelm@49698
   771
    apply (rule finite_subset)
wenzelm@53408
   772
    using *(1) assms(2)
wenzelm@53408
   773
    apply auto
wenzelm@49698
   774
    done
wenzelm@53399
   775
  {
wenzelm@53399
   776
    fix k
wenzelm@49698
   777
    assume "k \<in> q"
wenzelm@53408
   778
    then have kp: "k \<in> p"
wenzelm@53408
   779
      using assms(2) by auto
wenzelm@53408
   780
    show "k \<subseteq> \<Union>q"
wenzelm@53408
   781
      using `k \<in> q` by auto
immler@56188
   782
    show "\<exists>a b. k = cbox a b"
wenzelm@53408
   783
      using *(4)[OF kp] by auto
wenzelm@53408
   784
    show "k \<noteq> {}"
wenzelm@53408
   785
      using *(3)[OF kp] by auto
wenzelm@53399
   786
  }
wenzelm@49698
   787
  fix k1 k2
wenzelm@49698
   788
  assume "k1 \<in> q" "k2 \<in> q" "k1 \<noteq> k2"
wenzelm@53408
   789
  then have **: "k1 \<in> p" "k2 \<in> p" "k1 \<noteq> k2"
wenzelm@53399
   790
    using assms(2) by auto
wenzelm@53399
   791
  show "interior k1 \<inter> interior k2 = {}"
wenzelm@53408
   792
    using *(5)[OF **] by auto
wenzelm@49698
   793
qed auto
wenzelm@49698
   794
wenzelm@49698
   795
lemma division_of_union_self[intro]: "p division_of s \<Longrightarrow> p division_of (\<Union>p)"
wenzelm@49698
   796
  unfolding division_of_def by auto
himmelma@35172
   797
wenzelm@49970
   798
lemma division_of_content_0:
immler@56188
   799
  assumes "content (cbox a b) = 0" "d division_of (cbox a b)"
wenzelm@49970
   800
  shows "\<forall>k\<in>d. content k = 0"
wenzelm@49970
   801
  unfolding forall_in_division[OF assms(2)]
wenzelm@50945
   802
  apply (rule,rule,rule)
wenzelm@50945
   803
  apply (drule division_ofD(2)[OF assms(2)])
wenzelm@50945
   804
  apply (drule content_subset) unfolding assms(1)
wenzelm@49970
   805
proof -
wenzelm@49970
   806
  case goal1
wenzelm@53399
   807
  then show ?case using content_pos_le[of a b] by auto
wenzelm@49970
   808
qed
wenzelm@49970
   809
wenzelm@49970
   810
lemma division_inter:
immler@56188
   811
  fixes s1 s2 :: "'a::euclidean_space set"
wenzelm@53408
   812
  assumes "p1 division_of s1"
wenzelm@53408
   813
    and "p2 division_of s2"
wenzelm@49970
   814
  shows "{k1 \<inter> k2 | k1 k2 .k1 \<in> p1 \<and> k2 \<in> p2 \<and> k1 \<inter> k2 \<noteq> {}} division_of (s1 \<inter> s2)"
wenzelm@49970
   815
  (is "?A' division_of _")
wenzelm@49970
   816
proof -
wenzelm@49970
   817
  let ?A = "{s. s \<in>  (\<lambda>(k1,k2). k1 \<inter> k2) ` (p1 \<times> p2) \<and> s \<noteq> {}}"
wenzelm@53408
   818
  have *: "?A' = ?A" by auto
wenzelm@53399
   819
  show ?thesis
wenzelm@53399
   820
    unfolding *
wenzelm@49970
   821
  proof (rule division_ofI)
wenzelm@53399
   822
    have "?A \<subseteq> (\<lambda>(x, y). x \<inter> y) ` (p1 \<times> p2)"
wenzelm@53399
   823
      by auto
wenzelm@53399
   824
    moreover have "finite (p1 \<times> p2)"
wenzelm@53399
   825
      using assms unfolding division_of_def by auto
wenzelm@49970
   826
    ultimately show "finite ?A" by auto
wenzelm@53399
   827
    have *: "\<And>s. \<Union>{x\<in>s. x \<noteq> {}} = \<Union>s"
wenzelm@53399
   828
      by auto
wenzelm@49970
   829
    show "\<Union>?A = s1 \<inter> s2"
wenzelm@49970
   830
      apply (rule set_eqI)
wenzelm@49970
   831
      unfolding * and Union_image_eq UN_iff
wenzelm@49970
   832
      using division_ofD(6)[OF assms(1)] and division_ofD(6)[OF assms(2)]
wenzelm@49970
   833
      apply auto
wenzelm@49970
   834
      done
wenzelm@53399
   835
    {
wenzelm@53399
   836
      fix k
wenzelm@53399
   837
      assume "k \<in> ?A"
wenzelm@53408
   838
      then obtain k1 k2 where k: "k = k1 \<inter> k2" "k1 \<in> p1" "k2 \<in> p2" "k \<noteq> {}"
wenzelm@53399
   839
        by auto
wenzelm@53408
   840
      then show "k \<noteq> {}"
wenzelm@53408
   841
        by auto
wenzelm@49970
   842
      show "k \<subseteq> s1 \<inter> s2"
wenzelm@49970
   843
        using division_ofD(2)[OF assms(1) k(2)] and division_ofD(2)[OF assms(2) k(3)]
wenzelm@49970
   844
        unfolding k by auto
immler@56188
   845
      obtain a1 b1 where k1: "k1 = cbox a1 b1"
wenzelm@53408
   846
        using division_ofD(4)[OF assms(1) k(2)] by blast
immler@56188
   847
      obtain a2 b2 where k2: "k2 = cbox a2 b2"
wenzelm@53408
   848
        using division_ofD(4)[OF assms(2) k(3)] by blast
immler@56188
   849
      show "\<exists>a b. k = cbox a b"
wenzelm@53408
   850
        unfolding k k1 k2 unfolding inter_interval by auto
wenzelm@53408
   851
    }
wenzelm@49970
   852
    fix k1 k2
wenzelm@53408
   853
    assume "k1 \<in> ?A"
wenzelm@53408
   854
    then obtain x1 y1 where k1: "k1 = x1 \<inter> y1" "x1 \<in> p1" "y1 \<in> p2" "k1 \<noteq> {}"
wenzelm@53408
   855
      by auto
wenzelm@53408
   856
    assume "k2 \<in> ?A"
wenzelm@53408
   857
    then obtain x2 y2 where k2: "k2 = x2 \<inter> y2" "x2 \<in> p1" "y2 \<in> p2" "k2 \<noteq> {}"
wenzelm@53408
   858
      by auto
wenzelm@49970
   859
    assume "k1 \<noteq> k2"
wenzelm@53399
   860
    then have th: "x1 \<noteq> x2 \<or> y1 \<noteq> y2"
wenzelm@53399
   861
      unfolding k1 k2 by auto
wenzelm@53408
   862
    have *: "interior x1 \<inter> interior x2 = {} \<or> interior y1 \<inter> interior y2 = {} \<Longrightarrow>
wenzelm@53408
   863
      interior (x1 \<inter> y1) \<subseteq> interior x1 \<Longrightarrow> interior (x1 \<inter> y1) \<subseteq> interior y1 \<Longrightarrow>
wenzelm@53408
   864
      interior (x2 \<inter> y2) \<subseteq> interior x2 \<Longrightarrow> interior (x2 \<inter> y2) \<subseteq> interior y2 \<Longrightarrow>
wenzelm@53408
   865
      interior (x1 \<inter> y1) \<inter> interior (x2 \<inter> y2) = {}" by auto
wenzelm@49970
   866
    show "interior k1 \<inter> interior k2 = {}"
wenzelm@49970
   867
      unfolding k1 k2
wenzelm@49970
   868
      apply (rule *)
wenzelm@49970
   869
      defer
wenzelm@49970
   870
      apply (rule_tac[1-4] interior_mono)
wenzelm@49970
   871
      using division_ofD(5)[OF assms(1) k1(2) k2(2)]
wenzelm@49970
   872
      using division_ofD(5)[OF assms(2) k1(3) k2(3)]
wenzelm@53408
   873
      using th
wenzelm@53408
   874
      apply auto
wenzelm@53399
   875
      done
wenzelm@49970
   876
  qed
wenzelm@49970
   877
qed
wenzelm@49970
   878
wenzelm@49970
   879
lemma division_inter_1:
wenzelm@53408
   880
  assumes "d division_of i"
immler@56188
   881
    and "cbox a (b::'a::euclidean_space) \<subseteq> i"
immler@56188
   882
  shows "{cbox a b \<inter> k | k. k \<in> d \<and> cbox a b \<inter> k \<noteq> {}} division_of (cbox a b)"
immler@56188
   883
proof (cases "cbox a b = {}")
wenzelm@49970
   884
  case True
wenzelm@53399
   885
  show ?thesis
wenzelm@53399
   886
    unfolding True and division_of_trivial by auto
wenzelm@49970
   887
next
wenzelm@49970
   888
  case False
immler@56188
   889
  have *: "cbox a b \<inter> i = cbox a b" using assms(2) by auto
wenzelm@53399
   890
  show ?thesis
wenzelm@53399
   891
    using division_inter[OF division_of_self[OF False] assms(1)]
wenzelm@53399
   892
    unfolding * by auto
wenzelm@49970
   893
qed
wenzelm@49970
   894
wenzelm@49970
   895
lemma elementary_inter:
immler@56188
   896
  fixes s t :: "'a::euclidean_space set"
wenzelm@53408
   897
  assumes "p1 division_of s"
wenzelm@53408
   898
    and "p2 division_of t"
himmelma@35172
   899
  shows "\<exists>p. p division_of (s \<inter> t)"
wenzelm@50945
   900
  apply rule
wenzelm@50945
   901
  apply (rule division_inter[OF assms])
wenzelm@50945
   902
  done
wenzelm@49970
   903
wenzelm@49970
   904
lemma elementary_inters:
wenzelm@53408
   905
  assumes "finite f"
wenzelm@53408
   906
    and "f \<noteq> {}"
immler@56188
   907
    and "\<forall>s\<in>f. \<exists>p. p division_of (s::('a::euclidean_space) set)"
wenzelm@49970
   908
  shows "\<exists>p. p division_of (\<Inter> f)"
wenzelm@49970
   909
  using assms
wenzelm@49970
   910
proof (induct f rule: finite_induct)
wenzelm@49970
   911
  case (insert x f)
wenzelm@49970
   912
  show ?case
wenzelm@49970
   913
  proof (cases "f = {}")
wenzelm@49970
   914
    case True
wenzelm@53399
   915
    then show ?thesis
wenzelm@53399
   916
      unfolding True using insert by auto
wenzelm@49970
   917
  next
wenzelm@49970
   918
    case False
wenzelm@53408
   919
    obtain p where "p division_of \<Inter>f"
wenzelm@53408
   920
      using insert(3)[OF False insert(5)[unfolded ball_simps,THEN conjunct2]] ..
wenzelm@53408
   921
    moreover obtain px where "px division_of x"
wenzelm@53408
   922
      using insert(5)[rule_format,OF insertI1] ..
wenzelm@49970
   923
    ultimately show ?thesis
wenzelm@53408
   924
      apply -
wenzelm@49970
   925
      unfolding Inter_insert
wenzelm@53408
   926
      apply (rule elementary_inter)
wenzelm@49970
   927
      apply assumption
wenzelm@49970
   928
      apply assumption
wenzelm@49970
   929
      done
wenzelm@49970
   930
  qed
wenzelm@49970
   931
qed auto
himmelma@35172
   932
himmelma@35172
   933
lemma division_disjoint_union:
wenzelm@53408
   934
  assumes "p1 division_of s1"
wenzelm@53408
   935
    and "p2 division_of s2"
wenzelm@53408
   936
    and "interior s1 \<inter> interior s2 = {}"
wenzelm@50945
   937
  shows "(p1 \<union> p2) division_of (s1 \<union> s2)"
wenzelm@50945
   938
proof (rule division_ofI)
wenzelm@53408
   939
  note d1 = division_ofD[OF assms(1)]
wenzelm@53408
   940
  note d2 = division_ofD[OF assms(2)]
wenzelm@53408
   941
  show "finite (p1 \<union> p2)"
wenzelm@53408
   942
    using d1(1) d2(1) by auto
wenzelm@53408
   943
  show "\<Union>(p1 \<union> p2) = s1 \<union> s2"
wenzelm@53408
   944
    using d1(6) d2(6) by auto
wenzelm@50945
   945
  {
wenzelm@50945
   946
    fix k1 k2
wenzelm@50945
   947
    assume as: "k1 \<in> p1 \<union> p2" "k2 \<in> p1 \<union> p2" "k1 \<noteq> k2"
wenzelm@50945
   948
    moreover
wenzelm@50945
   949
    let ?g="interior k1 \<inter> interior k2 = {}"
wenzelm@50945
   950
    {
wenzelm@50945
   951
      assume as: "k1\<in>p1" "k2\<in>p2"
wenzelm@50945
   952
      have ?g
wenzelm@50945
   953
        using interior_mono[OF d1(2)[OF as(1)]] interior_mono[OF d2(2)[OF as(2)]]
wenzelm@50945
   954
        using assms(3) by blast
wenzelm@50945
   955
    }
wenzelm@50945
   956
    moreover
wenzelm@50945
   957
    {
wenzelm@50945
   958
      assume as: "k1\<in>p2" "k2\<in>p1"
wenzelm@50945
   959
      have ?g
wenzelm@50945
   960
        using interior_mono[OF d1(2)[OF as(2)]] interior_mono[OF d2(2)[OF as(1)]]
wenzelm@50945
   961
        using assms(3) by blast
wenzelm@50945
   962
    }
wenzelm@53399
   963
    ultimately show ?g
wenzelm@53399
   964
      using d1(5)[OF _ _ as(3)] and d2(5)[OF _ _ as(3)] by auto
wenzelm@50945
   965
  }
wenzelm@50945
   966
  fix k
wenzelm@50945
   967
  assume k: "k \<in> p1 \<union> p2"
wenzelm@53408
   968
  show "k \<subseteq> s1 \<union> s2"
wenzelm@53408
   969
    using k d1(2) d2(2) by auto
wenzelm@53408
   970
  show "k \<noteq> {}"
wenzelm@53408
   971
    using k d1(3) d2(3) by auto
immler@56188
   972
  show "\<exists>a b. k = cbox a b"
wenzelm@53408
   973
    using k d1(4) d2(4) by auto
wenzelm@50945
   974
qed
himmelma@35172
   975
himmelma@35172
   976
lemma partial_division_extend_1:
immler@56188
   977
  fixes a b c d :: "'a::euclidean_space"
immler@56188
   978
  assumes incl: "cbox c d \<subseteq> cbox a b"
immler@56188
   979
    and nonempty: "cbox c d \<noteq> {}"
immler@56188
   980
  obtains p where "p division_of (cbox a b)" "cbox c d \<in> p"
hoelzl@50526
   981
proof
wenzelm@53408
   982
  let ?B = "\<lambda>f::'a\<Rightarrow>'a \<times> 'a.
immler@56188
   983
    cbox (\<Sum>i\<in>Basis. (fst (f i) \<bullet> i) *\<^sub>R i) (\<Sum>i\<in>Basis. (snd (f i) \<bullet> i) *\<^sub>R i)"
wenzelm@53015
   984
  def p \<equiv> "?B ` (Basis \<rightarrow>\<^sub>E {(a, c), (c, d), (d, b)})"
hoelzl@50526
   985
immler@56188
   986
  show "cbox c d \<in> p"
hoelzl@50526
   987
    unfolding p_def
immler@56188
   988
    by (auto simp add: box_eq_empty cbox_def intro!: image_eqI[where x="\<lambda>(i::'a)\<in>Basis. (c, d)"])
wenzelm@50945
   989
  {
wenzelm@50945
   990
    fix i :: 'a
wenzelm@50945
   991
    assume "i \<in> Basis"
hoelzl@50526
   992
    with incl nonempty have "a \<bullet> i \<le> c \<bullet> i" "c \<bullet> i \<le> d \<bullet> i" "d \<bullet> i \<le> b \<bullet> i"
immler@56188
   993
      unfolding box_eq_empty subset_box by (auto simp: not_le)
wenzelm@50945
   994
  }
hoelzl@50526
   995
  note ord = this
hoelzl@50526
   996
immler@56188
   997
  show "p division_of (cbox a b)"
hoelzl@50526
   998
  proof (rule division_ofI)
wenzelm@53399
   999
    show "finite p"
wenzelm@53399
  1000
      unfolding p_def by (auto intro!: finite_PiE)
wenzelm@50945
  1001
    {
wenzelm@50945
  1002
      fix k
wenzelm@50945
  1003
      assume "k \<in> p"
wenzelm@53015
  1004
      then obtain f where f: "f \<in> Basis \<rightarrow>\<^sub>E {(a, c), (c, d), (d, b)}" and k: "k = ?B f"
hoelzl@50526
  1005
        by (auto simp: p_def)
immler@56188
  1006
      then show "\<exists>a b. k = cbox a b"
wenzelm@53408
  1007
        by auto
immler@56188
  1008
      have "k \<subseteq> cbox a b \<and> k \<noteq> {}"
immler@56188
  1009
      proof (simp add: k box_eq_empty subset_box not_less, safe)
wenzelm@53374
  1010
        fix i :: 'a
wenzelm@53374
  1011
        assume i: "i \<in> Basis"
wenzelm@50945
  1012
        with f have "f i = (a, c) \<or> f i = (c, d) \<or> f i = (d, b)"
hoelzl@50526
  1013
          by (auto simp: PiE_iff)
wenzelm@53374
  1014
        with i ord[of i]
wenzelm@50945
  1015
        show "a \<bullet> i \<le> fst (f i) \<bullet> i" "snd (f i) \<bullet> i \<le> b \<bullet> i" "fst (f i) \<bullet> i \<le> snd (f i) \<bullet> i"
immler@54776
  1016
          by auto
hoelzl@50526
  1017
      qed
immler@56188
  1018
      then show "k \<noteq> {}" "k \<subseteq> cbox a b"
wenzelm@53408
  1019
        by auto
wenzelm@50945
  1020
      {
wenzelm@53408
  1021
        fix l
wenzelm@53408
  1022
        assume "l \<in> p"
wenzelm@53015
  1023
        then obtain g where g: "g \<in> Basis \<rightarrow>\<^sub>E {(a, c), (c, d), (d, b)}" and l: "l = ?B g"
wenzelm@50945
  1024
          by (auto simp: p_def)
wenzelm@50945
  1025
        assume "l \<noteq> k"
wenzelm@50945
  1026
        have "\<exists>i\<in>Basis. f i \<noteq> g i"
wenzelm@50945
  1027
        proof (rule ccontr)
wenzelm@53408
  1028
          assume "\<not> ?thesis"
wenzelm@50945
  1029
          with f g have "f = g"
wenzelm@50945
  1030
            by (auto simp: PiE_iff extensional_def intro!: ext)
wenzelm@50945
  1031
          with `l \<noteq> k` show False
wenzelm@50945
  1032
            by (simp add: l k)
wenzelm@50945
  1033
        qed
wenzelm@53408
  1034
        then obtain i where *: "i \<in> Basis" "f i \<noteq> g i" ..
wenzelm@53408
  1035
        then have "f i = (a, c) \<or> f i = (c, d) \<or> f i = (d, b)"
wenzelm@50945
  1036
            "g i = (a, c) \<or> g i = (c, d) \<or> g i = (d, b)"
wenzelm@50945
  1037
          using f g by (auto simp: PiE_iff)
wenzelm@53408
  1038
        with * ord[of i] show "interior l \<inter> interior k = {}"
immler@56188
  1039
          by (auto simp add: l k interior_cbox disjoint_interval intro!: bexI[of _ i])
wenzelm@50945
  1040
      }
immler@56188
  1041
      note `k \<subseteq> cbox a b`
wenzelm@50945
  1042
    }
hoelzl@50526
  1043
    moreover
wenzelm@50945
  1044
    {
immler@56188
  1045
      fix x assume x: "x \<in> cbox a b"
hoelzl@50526
  1046
      have "\<forall>i\<in>Basis. \<exists>l. x \<bullet> i \<in> {fst l \<bullet> i .. snd l \<bullet> i} \<and> l \<in> {(a, c), (c, d), (d, b)}"
hoelzl@50526
  1047
      proof
wenzelm@53408
  1048
        fix i :: 'a
wenzelm@53408
  1049
        assume "i \<in> Basis"
wenzelm@53399
  1050
        with x ord[of i]
hoelzl@50526
  1051
        have "(a \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> c \<bullet> i) \<or> (c \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> d \<bullet> i) \<or>
hoelzl@50526
  1052
            (d \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> b \<bullet> i)"
immler@56188
  1053
          by (auto simp: cbox_def)
hoelzl@50526
  1054
        then show "\<exists>l. x \<bullet> i \<in> {fst l \<bullet> i .. snd l \<bullet> i} \<and> l \<in> {(a, c), (c, d), (d, b)}"
hoelzl@50526
  1055
          by auto
hoelzl@50526
  1056
      qed
wenzelm@53408
  1057
      then obtain f where
wenzelm@53408
  1058
        f: "\<forall>i\<in>Basis. x \<bullet> i \<in> {fst (f i) \<bullet> i..snd (f i) \<bullet> i} \<and> f i \<in> {(a, c), (c, d), (d, b)}"
wenzelm@53408
  1059
        unfolding bchoice_iff ..
wenzelm@53374
  1060
      moreover from f have "restrict f Basis \<in> Basis \<rightarrow>\<^sub>E {(a, c), (c, d), (d, b)}"
hoelzl@50526
  1061
        by auto
hoelzl@50526
  1062
      moreover from f have "x \<in> ?B (restrict f Basis)"
immler@56188
  1063
        by (auto simp: mem_box)
hoelzl@50526
  1064
      ultimately have "\<exists>k\<in>p. x \<in> k"
wenzelm@53408
  1065
        unfolding p_def by blast
wenzelm@53408
  1066
    }
immler@56188
  1067
    ultimately show "\<Union>p = cbox a b"
hoelzl@50526
  1068
      by auto
hoelzl@50526
  1069
  qed
hoelzl@50526
  1070
qed
himmelma@35172
  1071
wenzelm@50945
  1072
lemma partial_division_extend_interval:
immler@56188
  1073
  assumes "p division_of (\<Union>p)" "(\<Union>p) \<subseteq> cbox a b"
immler@56188
  1074
  obtains q where "p \<subseteq> q" "q division_of cbox a (b::'a::euclidean_space)"
wenzelm@50945
  1075
proof (cases "p = {}")
wenzelm@50945
  1076
  case True
immler@56188
  1077
  obtain q where "q division_of (cbox a b)"
wenzelm@53408
  1078
    by (rule elementary_interval)
wenzelm@53399
  1079
  then show ?thesis
wenzelm@50945
  1080
    apply -
wenzelm@50945
  1081
    apply (rule that[of q])
wenzelm@50945
  1082
    unfolding True
wenzelm@50945
  1083
    apply auto
wenzelm@50945
  1084
    done
wenzelm@50945
  1085
next
wenzelm@50945
  1086
  case False
wenzelm@50945
  1087
  note p = division_ofD[OF assms(1)]
immler@56188
  1088
  have *: "\<forall>k\<in>p. \<exists>q. q division_of cbox a b \<and> k \<in> q"
wenzelm@50945
  1089
  proof
wenzelm@50945
  1090
    case goal1
immler@56188
  1091
    obtain c d where k: "k = cbox c d"
wenzelm@53408
  1092
      using p(4)[OF goal1] by blast
immler@56188
  1093
    have *: "cbox c d \<subseteq> cbox a b" "cbox c d \<noteq> {}"
immler@54775
  1094
      using p(2,3)[OF goal1, unfolded k] using assms(2)
immler@54776
  1095
      by (blast intro: order.trans)+
immler@56188
  1096
    obtain q where "q division_of cbox a b" "cbox c d \<in> q"
wenzelm@53408
  1097
      by (rule partial_division_extend_1[OF *])
wenzelm@53408
  1098
    then show ?case
wenzelm@53408
  1099
      unfolding k by auto
wenzelm@50945
  1100
  qed
immler@56188
  1101
  obtain q where q: "\<And>x. x \<in> p \<Longrightarrow> q x division_of cbox a b" "\<And>x. x \<in> p \<Longrightarrow> x \<in> q x"
wenzelm@53408
  1102
    using bchoice[OF *] by blast
wenzelm@53408
  1103
  have "\<And>x. x \<in> p \<Longrightarrow> \<exists>d. d division_of \<Union>(q x - {x})"
wenzelm@53408
  1104
    apply rule
wenzelm@53408
  1105
    apply (rule_tac p="q x" in division_of_subset)
wenzelm@50945
  1106
  proof -
wenzelm@50945
  1107
    fix x
wenzelm@53408
  1108
    assume x: "x \<in> p"
wenzelm@50945
  1109
    show "q x division_of \<Union>q x"
wenzelm@50945
  1110
      apply -
wenzelm@50945
  1111
      apply (rule division_ofI)
wenzelm@50945
  1112
      using division_ofD[OF q(1)[OF x]]
wenzelm@50945
  1113
      apply auto
wenzelm@50945
  1114
      done
wenzelm@53408
  1115
    show "q x - {x} \<subseteq> q x"
wenzelm@53408
  1116
      by auto
wenzelm@50945
  1117
  qed
wenzelm@53399
  1118
  then have "\<exists>d. d division_of \<Inter> ((\<lambda>i. \<Union>(q i - {i})) ` p)"
wenzelm@50945
  1119
    apply -
wenzelm@50945
  1120
    apply (rule elementary_inters)
wenzelm@50945
  1121
    apply (rule finite_imageI[OF p(1)])
wenzelm@50945
  1122
    unfolding image_is_empty
wenzelm@50945
  1123
    apply (rule False)
wenzelm@50945
  1124
    apply auto
wenzelm@50945
  1125
    done
wenzelm@53408
  1126
  then obtain d where d: "d division_of \<Inter>((\<lambda>i. \<Union>(q i - {i})) ` p)" ..
wenzelm@50945
  1127
  show ?thesis
wenzelm@50945
  1128
    apply (rule that[of "d \<union> p"])
wenzelm@50945
  1129
  proof -
wenzelm@53408
  1130
    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
immler@56188
  1131
    have *: "cbox a b = \<Inter>((\<lambda>i. \<Union>(q i - {i})) ` p) \<union> \<Union>p"
wenzelm@50945
  1132
      apply (rule *[OF False])
wenzelm@50945
  1133
    proof
wenzelm@50945
  1134
      fix i
wenzelm@53408
  1135
      assume i: "i \<in> p"
immler@56188
  1136
      show "\<Union>(q i - {i}) \<union> i = cbox a b"
wenzelm@50945
  1137
        using division_ofD(6)[OF q(1)[OF i]] using q(2)[OF i] by auto
wenzelm@50945
  1138
    qed
immler@56188
  1139
    show "d \<union> p division_of (cbox a b)"
wenzelm@50945
  1140
      unfolding *
wenzelm@50945
  1141
      apply (rule division_disjoint_union[OF d assms(1)])
wenzelm@50945
  1142
      apply (rule inter_interior_unions_intervals)
wenzelm@50945
  1143
      apply (rule p open_interior ballI)+
wenzelm@53408
  1144
      apply assumption
wenzelm@53408
  1145
    proof
wenzelm@50945
  1146
      fix k
wenzelm@53408
  1147
      assume k: "k \<in> p"
wenzelm@53408
  1148
      have *: "\<And>u t s. u \<subseteq> s \<Longrightarrow> s \<inter> t = {} \<Longrightarrow> u \<inter> t = {}"
wenzelm@53408
  1149
        by auto
haftmann@52141
  1150
      show "interior (\<Inter> ((\<lambda>i. \<Union>(q i - {i})) ` p)) \<inter> interior k = {}"
wenzelm@50945
  1151
        apply (rule *[of _ "interior (\<Union>(q k - {k}))"])
wenzelm@50945
  1152
        defer
wenzelm@50945
  1153
        apply (subst Int_commute)
wenzelm@50945
  1154
        apply (rule inter_interior_unions_intervals)
wenzelm@50945
  1155
      proof -
wenzelm@50945
  1156
        note qk=division_ofD[OF q(1)[OF k]]
immler@56188
  1157
        show "finite (q k - {k})" "open (interior k)" "\<forall>t\<in>q k - {k}. \<exists>a b. t = cbox a b"
wenzelm@53408
  1158
          using qk by auto
wenzelm@50945
  1159
        show "\<forall>t\<in>q k - {k}. interior k \<inter> interior t = {}"
wenzelm@50945
  1160
          using qk(5) using q(2)[OF k] by auto
wenzelm@53408
  1161
        have *: "\<And>x s. x \<in> s \<Longrightarrow> \<Inter>s \<subseteq> x"
wenzelm@53408
  1162
          by auto
haftmann@52141
  1163
        show "interior (\<Inter> ((\<lambda>i. \<Union>(q i - {i})) ` p)) \<subseteq> interior (\<Union>(q k - {k}))"
wenzelm@50945
  1164
          apply (rule interior_mono *)+
wenzelm@53408
  1165
          using k
wenzelm@53408
  1166
          apply auto
wenzelm@53408
  1167
          done
wenzelm@50945
  1168
      qed
wenzelm@50945
  1169
    qed
wenzelm@50945
  1170
  qed auto
wenzelm@50945
  1171
qed
himmelma@35172
  1172
wenzelm@53399
  1173
lemma elementary_bounded[dest]:
immler@56188
  1174
  fixes s :: "'a::euclidean_space set"
wenzelm@53408
  1175
  shows "p division_of s \<Longrightarrow> bounded s"
wenzelm@53408
  1176
  unfolding division_of_def by (metis bounded_Union bounded_interval)
wenzelm@53399
  1177
immler@56188
  1178
lemma elementary_subset_cbox:
immler@56188
  1179
  "p division_of s \<Longrightarrow> \<exists>a b. s \<subseteq> cbox a (b::'a::euclidean_space)"
immler@56188
  1180
  by (meson elementary_bounded bounded_subset_cbox)
wenzelm@50945
  1181
wenzelm@50945
  1182
lemma division_union_intervals_exists:
immler@56188
  1183
  fixes a b :: "'a::euclidean_space"
immler@56188
  1184
  assumes "cbox a b \<noteq> {}"
immler@56188
  1185
  obtains p where "(insert (cbox a b) p) division_of (cbox a b \<union> cbox c d)"
immler@56188
  1186
proof (cases "cbox c d = {}")
wenzelm@50945
  1187
  case True
wenzelm@50945
  1188
  show ?thesis
wenzelm@50945
  1189
    apply (rule that[of "{}"])
wenzelm@50945
  1190
    unfolding True
wenzelm@50945
  1191
    using assms
wenzelm@50945
  1192
    apply auto
wenzelm@50945
  1193
    done
wenzelm@50945
  1194
next
wenzelm@50945
  1195
  case False
wenzelm@50945
  1196
  show ?thesis
immler@56188
  1197
  proof (cases "cbox a b \<inter> cbox c d = {}")
wenzelm@50945
  1198
    case True
wenzelm@53408
  1199
    have *: "\<And>a b. {a, b} = {a} \<union> {b}" by auto
wenzelm@50945
  1200
    show ?thesis
immler@56188
  1201
      apply (rule that[of "{cbox c d}"])
wenzelm@50945
  1202
      unfolding *
wenzelm@50945
  1203
      apply (rule division_disjoint_union)
immler@56188
  1204
      using `cbox c d \<noteq> {}` True assms
wenzelm@50945
  1205
      using interior_subset
wenzelm@50945
  1206
      apply auto
wenzelm@50945
  1207
      done
wenzelm@50945
  1208
  next
wenzelm@50945
  1209
    case False
immler@56188
  1210
    obtain u v where uv: "cbox a b \<inter> cbox c d = cbox u v"
wenzelm@50945
  1211
      unfolding inter_interval by auto
immler@56188
  1212
    have *: "cbox u v \<subseteq> cbox c d" using uv by auto
immler@56188
  1213
    obtain p where "p division_of cbox c d" "cbox u v \<in> p"
wenzelm@53408
  1214
      by (rule partial_division_extend_1[OF * False[unfolded uv]])
wenzelm@53408
  1215
    note p = this division_ofD[OF this(1)]
immler@56188
  1216
    have *: "cbox a b \<union> cbox c d = cbox a b \<union> \<Union>(p - {cbox u v})" "\<And>x s. insert x s = {x} \<union> s"
wenzelm@53399
  1217
      using p(8) unfolding uv[symmetric] by auto
wenzelm@50945
  1218
    show ?thesis
immler@56188
  1219
      apply (rule that[of "p - {cbox u v}"])
wenzelm@50945
  1220
      unfolding *(1)
wenzelm@50945
  1221
      apply (subst *(2))
wenzelm@50945
  1222
      apply (rule division_disjoint_union)
wenzelm@50945
  1223
      apply (rule, rule assms)
wenzelm@50945
  1224
      apply (rule division_of_subset[of p])
wenzelm@50945
  1225
      apply (rule division_of_union_self[OF p(1)])
wenzelm@50945
  1226
      defer
wenzelm@53399
  1227
      unfolding interior_inter[symmetric]
wenzelm@50945
  1228
    proof -
wenzelm@50945
  1229
      have *: "\<And>cd p uv ab. p \<subseteq> cd \<Longrightarrow> ab \<inter> cd = uv \<Longrightarrow> ab \<inter> p = uv \<inter> p" by auto
immler@56188
  1230
      have "interior (cbox a b \<inter> \<Union>(p - {cbox u v})) = interior(cbox u v \<inter> \<Union>(p - {cbox u v}))"
wenzelm@50945
  1231
        apply (rule arg_cong[of _ _ interior])
wenzelm@50945
  1232
        apply (rule *[OF _ uv])
wenzelm@50945
  1233
        using p(8)
wenzelm@50945
  1234
        apply auto
wenzelm@50945
  1235
        done
wenzelm@50945
  1236
      also have "\<dots> = {}"
wenzelm@50945
  1237
        unfolding interior_inter
wenzelm@50945
  1238
        apply (rule inter_interior_unions_intervals)
wenzelm@50945
  1239
        using p(6) p(7)[OF p(2)] p(3)
wenzelm@50945
  1240
        apply auto
wenzelm@50945
  1241
        done
immler@56188
  1242
      finally show "interior (cbox a b \<inter> \<Union>(p - {cbox u v})) = {}" .
wenzelm@50945
  1243
    qed auto
wenzelm@50945
  1244
  qed
wenzelm@50945
  1245
qed
himmelma@35172
  1246
wenzelm@53399
  1247
lemma division_of_unions:
wenzelm@53399
  1248
  assumes "finite f"
wenzelm@53408
  1249
    and "\<And>p. p \<in> f \<Longrightarrow> p division_of (\<Union>p)"
wenzelm@53399
  1250
    and "\<And>k1 k2. k1 \<in> \<Union>f \<Longrightarrow> k2 \<in> \<Union>f \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow> interior k1 \<inter> interior k2 = {}"
wenzelm@53399
  1251
  shows "\<Union>f division_of \<Union>\<Union>f"
wenzelm@53399
  1252
  apply (rule division_ofI)
wenzelm@53399
  1253
  prefer 5
wenzelm@53399
  1254
  apply (rule assms(3)|assumption)+
wenzelm@53399
  1255
  apply (rule finite_Union assms(1))+
wenzelm@53399
  1256
  prefer 3
wenzelm@53399
  1257
  apply (erule UnionE)
wenzelm@53399
  1258
  apply (rule_tac s=X in division_ofD(3)[OF assms(2)])
wenzelm@53399
  1259
  using division_ofD[OF assms(2)]
wenzelm@53399
  1260
  apply auto
wenzelm@53399
  1261
  done
wenzelm@53399
  1262
wenzelm@53399
  1263
lemma elementary_union_interval:
immler@56188
  1264
  fixes a b :: "'a::euclidean_space"
wenzelm@53399
  1265
  assumes "p division_of \<Union>p"
immler@56188
  1266
  obtains q where "q division_of (cbox a b \<union> \<Union>p)"
wenzelm@53399
  1267
proof -
wenzelm@53399
  1268
  note assm = division_ofD[OF assms]
wenzelm@53408
  1269
  have lem1: "\<And>f s. \<Union>\<Union>(f ` s) = \<Union>((\<lambda>x. \<Union>(f x)) ` s)"
wenzelm@53399
  1270
    by auto
wenzelm@53399
  1271
  have lem2: "\<And>f s. f \<noteq> {} \<Longrightarrow> \<Union>{s \<union> t |t. t \<in> f} = s \<union> \<Union>f"
wenzelm@53399
  1272
    by auto
wenzelm@53399
  1273
  {
wenzelm@53399
  1274
    presume "p = {} \<Longrightarrow> thesis"
immler@56188
  1275
      "cbox a b = {} \<Longrightarrow> thesis"
immler@56188
  1276
      "cbox a b \<noteq> {} \<Longrightarrow> interior (cbox a b) = {} \<Longrightarrow> thesis"
immler@56188
  1277
      "p \<noteq> {} \<Longrightarrow> interior (cbox a b)\<noteq>{} \<Longrightarrow> cbox a b \<noteq> {} \<Longrightarrow> thesis"
wenzelm@53399
  1278
    then show thesis by auto
wenzelm@53399
  1279
  next
wenzelm@53399
  1280
    assume as: "p = {}"
immler@56188
  1281
    obtain p where "p division_of (cbox a b)"
wenzelm@53408
  1282
      by (rule elementary_interval)
wenzelm@53399
  1283
    then show thesis
wenzelm@53408
  1284
      apply -
wenzelm@53408
  1285
      apply (rule that[of p])
wenzelm@53399
  1286
      unfolding as
wenzelm@53399
  1287
      apply auto
wenzelm@53399
  1288
      done
wenzelm@53399
  1289
  next
immler@56188
  1290
    assume as: "cbox a b = {}"
wenzelm@53399
  1291
    show thesis
wenzelm@53399
  1292
      apply (rule that)
wenzelm@53399
  1293
      unfolding as
wenzelm@53399
  1294
      using assms
wenzelm@53399
  1295
      apply auto
wenzelm@53399
  1296
      done
wenzelm@53399
  1297
  next
immler@56188
  1298
    assume as: "interior (cbox a b) = {}" "cbox a b \<noteq> {}"
wenzelm@53399
  1299
    show thesis
immler@56188
  1300
      apply (rule that[of "insert (cbox a b) p"],rule division_ofI)
wenzelm@53399
  1301
      unfolding finite_insert
wenzelm@53399
  1302
      apply (rule assm(1)) unfolding Union_insert
wenzelm@53399
  1303
      using assm(2-4) as
wenzelm@53399
  1304
      apply -
immler@54775
  1305
      apply (fast dest: assm(5))+
wenzelm@53399
  1306
      done
wenzelm@53399
  1307
  next
immler@56188
  1308
    assume as: "p \<noteq> {}" "interior (cbox a b) \<noteq> {}" "cbox a b \<noteq> {}"
immler@56188
  1309
    have "\<forall>k\<in>p. \<exists>q. (insert (cbox a b) q) division_of (cbox a b \<union> k)"
wenzelm@53399
  1310
    proof
wenzelm@53399
  1311
      case goal1
immler@56188
  1312
      from assm(4)[OF this] obtain c d where "k = cbox c d" by blast
wenzelm@53399
  1313
      then show ?case
wenzelm@53399
  1314
        apply -
wenzelm@53408
  1315
        apply (rule division_union_intervals_exists[OF as(3), of c d])
wenzelm@53399
  1316
        apply auto
wenzelm@53399
  1317
        done
wenzelm@53399
  1318
    qed
immler@56188
  1319
    from bchoice[OF this] obtain q where "\<forall>x\<in>p. insert (cbox a b) (q x) division_of (cbox a b) \<union> x" ..
wenzelm@53408
  1320
    note q = division_ofD[OF this[rule_format]]
immler@56188
  1321
    let ?D = "\<Union>{insert (cbox a b) (q k) | k. k \<in> p}"
wenzelm@53399
  1322
    show thesis
wenzelm@53399
  1323
      apply (rule that[of "?D"])
wenzelm@53408
  1324
      apply (rule division_ofI)
wenzelm@53408
  1325
    proof -
immler@56188
  1326
      have *: "{insert (cbox a b) (q k) |k. k \<in> p} = (\<lambda>k. insert (cbox a b) (q k)) ` p"
wenzelm@53399
  1327
        by auto
wenzelm@53399
  1328
      show "finite ?D"
wenzelm@53399
  1329
        apply (rule finite_Union)
wenzelm@53399
  1330
        unfolding *
wenzelm@53399
  1331
        apply (rule finite_imageI)
wenzelm@53399
  1332
        using assm(1) q(1)
wenzelm@53399
  1333
        apply auto
wenzelm@53399
  1334
        done
immler@56188
  1335
      show "\<Union>?D = cbox a b \<union> \<Union>p"
wenzelm@53399
  1336
        unfolding * lem1
immler@56188
  1337
        unfolding lem2[OF as(1), of "cbox a b", symmetric]
wenzelm@53399
  1338
        using q(6)
wenzelm@53399
  1339
        by auto
wenzelm@53399
  1340
      fix k
wenzelm@53408
  1341
      assume k: "k \<in> ?D"
immler@56188
  1342
      then show "k \<subseteq> cbox a b \<union> \<Union>p"
wenzelm@53408
  1343
        using q(2) by auto
wenzelm@53399
  1344
      show "k \<noteq> {}"
wenzelm@53408
  1345
        using q(3) k by auto
immler@56188
  1346
      show "\<exists>a b. k = cbox a b"
wenzelm@53408
  1347
        using q(4) k by auto
wenzelm@53399
  1348
      fix k'
wenzelm@53408
  1349
      assume k': "k' \<in> ?D" "k \<noteq> k'"
immler@56188
  1350
      obtain x where x: "k \<in> insert (cbox a b) (q x)" "x\<in>p"
wenzelm@53408
  1351
        using k by auto
immler@56188
  1352
      obtain x' where x': "k'\<in>insert (cbox a b) (q x')" "x'\<in>p"
wenzelm@53399
  1353
        using k' by auto
wenzelm@53399
  1354
      show "interior k \<inter> interior k' = {}"
wenzelm@53399
  1355
      proof (cases "x = x'")
wenzelm@53399
  1356
        case True
wenzelm@53399
  1357
        show ?thesis
wenzelm@53399
  1358
          apply(rule q(5))
wenzelm@53399
  1359
          using x x' k'
wenzelm@53399
  1360
          unfolding True
wenzelm@53399
  1361
          apply auto
wenzelm@53399
  1362
          done
wenzelm@53399
  1363
      next
wenzelm@53399
  1364
        case False
wenzelm@53399
  1365
        {
immler@56188
  1366
          presume "k = cbox a b \<Longrightarrow> ?thesis"
immler@56188
  1367
            and "k' = cbox a b \<Longrightarrow> ?thesis"
immler@56188
  1368
            and "k \<noteq> cbox a b \<Longrightarrow> k' \<noteq> cbox a b \<Longrightarrow> ?thesis"
wenzelm@53399
  1369
          then show ?thesis by auto
wenzelm@53399
  1370
        next
immler@56188
  1371
          assume as': "k  = cbox a b"
wenzelm@53399
  1372
          show ?thesis
wenzelm@53408
  1373
            apply (rule q(5))
wenzelm@53408
  1374
            using x' k'(2)
wenzelm@53408
  1375
            unfolding as'
wenzelm@53408
  1376
            apply auto
wenzelm@53408
  1377
            done
wenzelm@53399
  1378
        next
immler@56188
  1379
          assume as': "k' = cbox a b"
wenzelm@53399
  1380
          show ?thesis
wenzelm@53399
  1381
            apply (rule q(5))
wenzelm@53399
  1382
            using x  k'(2)
wenzelm@53399
  1383
            unfolding as'
wenzelm@53399
  1384
            apply auto
wenzelm@53399
  1385
            done
wenzelm@53399
  1386
        }
immler@56188
  1387
        assume as': "k \<noteq> cbox a b" "k' \<noteq> cbox a b"
immler@56188
  1388
        obtain c d where k: "k = cbox c d"
wenzelm@53408
  1389
          using q(4)[OF x(2,1)] by blast
immler@56188
  1390
        have "interior k \<inter> interior (cbox a b) = {}"
wenzelm@53408
  1391
          apply (rule q(5))
wenzelm@53408
  1392
          using x k'(2)
wenzelm@53399
  1393
          using as'
wenzelm@53399
  1394
          apply auto
wenzelm@53399
  1395
          done
wenzelm@53399
  1396
        then have "interior k \<subseteq> interior x"
wenzelm@53399
  1397
          apply -
wenzelm@53408
  1398
          apply (rule interior_subset_union_intervals[OF k _ as(2) q(2)[OF x(2,1)]])
wenzelm@53399
  1399
          apply auto
wenzelm@53399
  1400
          done
wenzelm@53399
  1401
        moreover
immler@56188
  1402
        obtain c d where c_d: "k' = cbox c d"
wenzelm@53408
  1403
          using q(4)[OF x'(2,1)] by blast
immler@56188
  1404
        have "interior k' \<inter> interior (cbox a b) = {}"
wenzelm@53399
  1405
          apply (rule q(5))
wenzelm@53399
  1406
          using x' k'(2)
wenzelm@53399
  1407
          using as'
wenzelm@53399
  1408
          apply auto
wenzelm@53399
  1409
          done
wenzelm@53399
  1410
        then have "interior k' \<subseteq> interior x'"
wenzelm@53399
  1411
          apply -
wenzelm@53399
  1412
          apply (rule interior_subset_union_intervals[OF c_d _ as(2) q(2)[OF x'(2,1)]])
wenzelm@53399
  1413
          apply auto
wenzelm@53399
  1414
          done
wenzelm@53399
  1415
        ultimately show ?thesis
wenzelm@53399
  1416
          using assm(5)[OF x(2) x'(2) False] by auto
wenzelm@53399
  1417
      qed
wenzelm@53399
  1418
    qed
wenzelm@53399
  1419
  }
wenzelm@53399
  1420
qed
himmelma@35172
  1421
himmelma@35172
  1422
lemma elementary_unions_intervals:
wenzelm@53399
  1423
  assumes fin: "finite f"
immler@56188
  1424
    and "\<And>s. s \<in> f \<Longrightarrow> \<exists>a b. s = cbox a (b::'a::euclidean_space)"
wenzelm@53399
  1425
  obtains p where "p division_of (\<Union>f)"
wenzelm@53399
  1426
proof -
wenzelm@53399
  1427
  have "\<exists>p. p division_of (\<Union>f)"
wenzelm@53399
  1428
  proof (induct_tac f rule:finite_subset_induct)
himmelma@35172
  1429
    show "\<exists>p. p division_of \<Union>{}" using elementary_empty by auto
wenzelm@53399
  1430
  next
wenzelm@53399
  1431
    fix x F
wenzelm@53399
  1432
    assume as: "finite F" "x \<notin> F" "\<exists>p. p division_of \<Union>F" "x\<in>f"
wenzelm@53408
  1433
    from this(3) obtain p where p: "p division_of \<Union>F" ..
immler@56188
  1434
    from assms(2)[OF as(4)] obtain a b where x: "x = cbox a b" by blast
wenzelm@53399
  1435
    have *: "\<Union>F = \<Union>p"
wenzelm@53399
  1436
      using division_ofD[OF p] by auto
wenzelm@53399
  1437
    show "\<exists>p. p division_of \<Union>insert x F"
wenzelm@53399
  1438
      using elementary_union_interval[OF p[unfolded *], of a b]
wenzelm@53408
  1439
      unfolding Union_insert x * by auto
wenzelm@53408
  1440
  qed (insert assms, auto)
wenzelm@53399
  1441
  then show ?thesis
wenzelm@53399
  1442
    apply -
wenzelm@53399
  1443
    apply (erule exE)
wenzelm@53399
  1444
    apply (rule that)
wenzelm@53399
  1445
    apply auto
wenzelm@53399
  1446
    done
wenzelm@53399
  1447
qed
wenzelm@53399
  1448
wenzelm@53399
  1449
lemma elementary_union:
immler@56188
  1450
  fixes s t :: "'a::euclidean_space set"
wenzelm@53399
  1451
  assumes "ps division_of s"
wenzelm@53408
  1452
    and "pt division_of t"
himmelma@35172
  1453
  obtains p where "p division_of (s \<union> t)"
wenzelm@53399
  1454
proof -
wenzelm@53399
  1455
  have "s \<union> t = \<Union>ps \<union> \<Union>pt"
wenzelm@53399
  1456
    using assms unfolding division_of_def by auto
wenzelm@53399
  1457
  then have *: "\<Union>(ps \<union> pt) = s \<union> t" by auto
wenzelm@53399
  1458
  show ?thesis
wenzelm@53399
  1459
    apply -
wenzelm@53408
  1460
    apply (rule elementary_unions_intervals[of "ps \<union> pt"])
wenzelm@53399
  1461
    unfolding *
wenzelm@53399
  1462
    prefer 3
wenzelm@53399
  1463
    apply (rule_tac p=p in that)
wenzelm@53399
  1464
    using assms[unfolded division_of_def]
wenzelm@53399
  1465
    apply auto
wenzelm@53399
  1466
    done
wenzelm@53399
  1467
qed
wenzelm@53399
  1468
wenzelm@53399
  1469
lemma partial_division_extend:
immler@56188
  1470
  fixes t :: "'a::euclidean_space set"
wenzelm@53399
  1471
  assumes "p division_of s"
wenzelm@53399
  1472
    and "q division_of t"
wenzelm@53399
  1473
    and "s \<subseteq> t"
wenzelm@53399
  1474
  obtains r where "p \<subseteq> r" and "r division_of t"
wenzelm@53399
  1475
proof -
himmelma@35172
  1476
  note divp = division_ofD[OF assms(1)] and divq = division_ofD[OF assms(2)]
immler@56188
  1477
  obtain a b where ab: "t \<subseteq> cbox a b"
immler@56188
  1478
    using elementary_subset_cbox[OF assms(2)] by auto
immler@56188
  1479
  obtain r1 where "p \<subseteq> r1" "r1 division_of (cbox a b)"
wenzelm@53399
  1480
    apply (rule partial_division_extend_interval)
wenzelm@53399
  1481
    apply (rule assms(1)[unfolded divp(6)[symmetric]])
wenzelm@53399
  1482
    apply (rule subset_trans)
wenzelm@53399
  1483
    apply (rule ab assms[unfolded divp(6)[symmetric]])+
wenzelm@53408
  1484
    apply assumption
wenzelm@53399
  1485
    done
wenzelm@53399
  1486
  note r1 = this division_ofD[OF this(2)]
wenzelm@53408
  1487
  obtain p' where "p' division_of \<Union>(r1 - p)"
wenzelm@53399
  1488
    apply (rule elementary_unions_intervals[of "r1 - p"])
wenzelm@53399
  1489
    using r1(3,6)
wenzelm@53399
  1490
    apply auto
wenzelm@53399
  1491
    done
wenzelm@53399
  1492
  then obtain r2 where r2: "r2 division_of (\<Union>(r1 - p)) \<inter> (\<Union>q)"
wenzelm@53399
  1493
    apply -
wenzelm@53399
  1494
    apply (drule elementary_inter[OF _ assms(2)[unfolded divq(6)[symmetric]]])
wenzelm@53399
  1495
    apply auto
wenzelm@53399
  1496
    done
wenzelm@53399
  1497
  {
wenzelm@53399
  1498
    fix x
wenzelm@53399
  1499
    assume x: "x \<in> t" "x \<notin> s"
wenzelm@53399
  1500
    then have "x\<in>\<Union>r1"
wenzelm@53399
  1501
      unfolding r1 using ab by auto
wenzelm@53408
  1502
    then obtain r where r: "r \<in> r1" "x \<in> r"
wenzelm@53408
  1503
      unfolding Union_iff ..
wenzelm@53399
  1504
    moreover
wenzelm@53399
  1505
    have "r \<notin> p"
wenzelm@53399
  1506
    proof
wenzelm@53399
  1507
      assume "r \<in> p"
wenzelm@53399
  1508
      then have "x \<in> s" using divp(2) r by auto
wenzelm@53399
  1509
      then show False using x by auto
wenzelm@53399
  1510
    qed
wenzelm@53399
  1511
    ultimately have "x\<in>\<Union>(r1 - p)" by auto
wenzelm@53399
  1512
  }
wenzelm@53399
  1513
  then have *: "t = \<Union>p \<union> (\<Union>(r1 - p) \<inter> \<Union>q)"
wenzelm@53399
  1514
    unfolding divp divq using assms(3) by auto
wenzelm@53399
  1515
  show ?thesis
wenzelm@53399
  1516
    apply (rule that[of "p \<union> r2"])
wenzelm@53399
  1517
    unfolding *
wenzelm@53399
  1518
    defer
wenzelm@53399
  1519
    apply (rule division_disjoint_union)
wenzelm@53399
  1520
    unfolding divp(6)
wenzelm@53399
  1521
    apply(rule assms r2)+
wenzelm@53399
  1522
  proof -
wenzelm@53399
  1523
    have "interior s \<inter> interior (\<Union>(r1-p)) = {}"
wenzelm@53399
  1524
    proof (rule inter_interior_unions_intervals)
immler@56188
  1525
      show "finite (r1 - p)" and "open (interior s)" and "\<forall>t\<in>r1-p. \<exists>a b. t = cbox a b"
wenzelm@53399
  1526
        using r1 by auto
wenzelm@53399
  1527
      have *: "\<And>s. (\<And>x. x \<in> s \<Longrightarrow> False) \<Longrightarrow> s = {}"
wenzelm@53399
  1528
        by auto
wenzelm@53399
  1529
      show "\<forall>t\<in>r1-p. interior s \<inter> interior t = {}"
wenzelm@53399
  1530
      proof
wenzelm@53399
  1531
        fix m x
wenzelm@53399
  1532
        assume as: "m \<in> r1 - p"
wenzelm@53399
  1533
        have "interior m \<inter> interior (\<Union>p) = {}"
wenzelm@53399
  1534
        proof (rule inter_interior_unions_intervals)
immler@56188
  1535
          show "finite p" and "open (interior m)" and "\<forall>t\<in>p. \<exists>a b. t = cbox a b"
wenzelm@53399
  1536
            using divp by auto
wenzelm@53399
  1537
          show "\<forall>t\<in>p. interior m \<inter> interior t = {}"
wenzelm@53399
  1538
            apply (rule, rule r1(7))
wenzelm@53399
  1539
            using as
wenzelm@53399
  1540
            using r1 
wenzelm@53399
  1541
            apply auto
wenzelm@53399
  1542
            done
wenzelm@53399
  1543
        qed
wenzelm@53399
  1544
        then show "interior s \<inter> interior m = {}"
wenzelm@53399
  1545
          unfolding divp by auto
wenzelm@53399
  1546
      qed
wenzelm@53399
  1547
    qed
wenzelm@53399
  1548
    then show "interior s \<inter> interior (\<Union>(r1-p) \<inter> (\<Union>q)) = {}"
wenzelm@53399
  1549
      using interior_subset by auto
wenzelm@53399
  1550
  qed auto
wenzelm@53399
  1551
qed
wenzelm@53399
  1552
himmelma@35172
  1553
himmelma@35172
  1554
subsection {* Tagged (partial) divisions. *}
himmelma@35172
  1555
wenzelm@53408
  1556
definition tagged_partial_division_of (infixr "tagged'_partial'_division'_of" 40)
wenzelm@53408
  1557
  where "s tagged_partial_division_of i \<longleftrightarrow>
wenzelm@53408
  1558
    finite s \<and>
immler@56188
  1559
    (\<forall>x k. (x, k) \<in> s \<longrightarrow> x \<in> k \<and> k \<subseteq> i \<and> (\<exists>a b. k = cbox a b)) \<and>
wenzelm@53408
  1560
    (\<forall>x1 k1 x2 k2. (x1, k1) \<in> s \<and> (x2, k2) \<in> s \<and> (x1, k1) \<noteq> (x2, k2) \<longrightarrow>
wenzelm@53408
  1561
      interior k1 \<inter> interior k2 = {})"
wenzelm@53408
  1562
wenzelm@53408
  1563
lemma tagged_partial_division_ofD[dest]:
wenzelm@53408
  1564
  assumes "s tagged_partial_division_of i"
wenzelm@53408
  1565
  shows "finite s"
wenzelm@53408
  1566
    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k"
wenzelm@53408
  1567
    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"
immler@56188
  1568
    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
wenzelm@53408
  1569
    and "\<And>x1 k1 x2 k2. (x1,k1) \<in> s \<Longrightarrow>
wenzelm@53408
  1570
      (x2, k2) \<in> s \<Longrightarrow> (x1, k1) \<noteq> (x2, k2) \<Longrightarrow> interior k1 \<inter> interior k2 = {}"
wenzelm@53408
  1571
  using assms unfolding tagged_partial_division_of_def by blast+
wenzelm@53408
  1572
wenzelm@53408
  1573
definition tagged_division_of (infixr "tagged'_division'_of" 40)
wenzelm@53408
  1574
  where "s tagged_division_of i \<longleftrightarrow> s tagged_partial_division_of i \<and> (\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
himmelma@35172
  1575
huffman@44167
  1576
lemma tagged_division_of_finite: "s tagged_division_of i \<Longrightarrow> finite s"
himmelma@35172
  1577
  unfolding tagged_division_of_def tagged_partial_division_of_def by auto
himmelma@35172
  1578
himmelma@35172
  1579
lemma tagged_division_of:
wenzelm@53408
  1580
  "s tagged_division_of i \<longleftrightarrow>
wenzelm@53408
  1581
    finite s \<and>
immler@56188
  1582
    (\<forall>x k. (x, k) \<in> s \<longrightarrow> x \<in> k \<and> k \<subseteq> i \<and> (\<exists>a b. k = cbox a b)) \<and>
wenzelm@53408
  1583
    (\<forall>x1 k1 x2 k2. (x1, k1) \<in> s \<and> (x2, k2) \<in> s \<and> (x1, k1) \<noteq> (x2, k2) \<longrightarrow>
wenzelm@53408
  1584
      interior k1 \<inter> interior k2 = {}) \<and>
wenzelm@53408
  1585
    (\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
himmelma@35172
  1586
  unfolding tagged_division_of_def tagged_partial_division_of_def by auto
himmelma@35172
  1587
wenzelm@53408
  1588
lemma tagged_division_ofI:
wenzelm@53408
  1589
  assumes "finite s"
wenzelm@53408
  1590
    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k"
wenzelm@53408
  1591
    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"
immler@56188
  1592
    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
wenzelm@53408
  1593
    and "\<And>x1 k1 x2 k2. (x1,k1) \<in> s \<Longrightarrow> (x2, k2) \<in> s \<Longrightarrow> (x1, k1) \<noteq> (x2, k2) \<Longrightarrow>
wenzelm@53408
  1594
      interior k1 \<inter> interior k2 = {}"
wenzelm@53408
  1595
    and "(\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
himmelma@35172
  1596
  shows "s tagged_division_of i"
wenzelm@53408
  1597
  unfolding tagged_division_of
wenzelm@53408
  1598
  apply rule
wenzelm@53408
  1599
  defer
wenzelm@53408
  1600
  apply rule
wenzelm@53408
  1601
  apply (rule allI impI conjI assms)+
wenzelm@53408
  1602
  apply assumption
wenzelm@53408
  1603
  apply rule
wenzelm@53408
  1604
  apply (rule assms)
wenzelm@53408
  1605
  apply assumption
wenzelm@53408
  1606
  apply (rule assms)
wenzelm@53408
  1607
  apply assumption
wenzelm@53408
  1608
  using assms(1,5-)
wenzelm@53408
  1609
  apply blast+
wenzelm@53408
  1610
  done
wenzelm@53408
  1611
wenzelm@53408
  1612
lemma tagged_division_ofD[dest]:
wenzelm@53408
  1613
  assumes "s tagged_division_of i"
wenzelm@53408
  1614
  shows "finite s"
wenzelm@53408
  1615
    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k"
wenzelm@53408
  1616
    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"
immler@56188
  1617
    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
wenzelm@53408
  1618
    and "\<And>x1 k1 x2 k2. (x1, k1) \<in> s \<Longrightarrow> (x2, k2) \<in> s \<Longrightarrow> (x1, k1) \<noteq> (x2, k2) \<Longrightarrow>
wenzelm@53408
  1619
      interior k1 \<inter> interior k2 = {}"
wenzelm@53408
  1620
    and "(\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
wenzelm@53408
  1621
  using assms unfolding tagged_division_of by blast+
wenzelm@53408
  1622
wenzelm@53408
  1623
lemma division_of_tagged_division:
wenzelm@53408
  1624
  assumes "s tagged_division_of i"
wenzelm@53408
  1625
  shows "(snd ` s) division_of i"
wenzelm@53408
  1626
proof (rule division_ofI)
wenzelm@53408
  1627
  note assm = tagged_division_ofD[OF assms]
wenzelm@53408
  1628
  show "\<Union>(snd ` s) = i" "finite (snd ` s)"
wenzelm@53408
  1629
    using assm by auto
wenzelm@53408
  1630
  fix k
wenzelm@53408
  1631
  assume k: "k \<in> snd ` s"
wenzelm@53408
  1632
  then obtain xk where xk: "(xk, k) \<in> s"
wenzelm@53408
  1633
    by auto
immler@56188
  1634
  then show "k \<subseteq> i" "k \<noteq> {}" "\<exists>a b. k = cbox a b"
wenzelm@53408
  1635
    using assm by fastforce+
wenzelm@53408
  1636
  fix k'
wenzelm@53408
  1637
  assume k': "k' \<in> snd ` s" "k \<noteq> k'"
wenzelm@53408
  1638
  from this(1) obtain xk' where xk': "(xk', k') \<in> s"
wenzelm@53408
  1639
    by auto
wenzelm@53408
  1640
  then show "interior k \<inter> interior k' = {}"
wenzelm@53408
  1641
    apply -
wenzelm@53408
  1642
    apply (rule assm(5))
wenzelm@53408
  1643
    apply (rule xk xk')+
wenzelm@53408
  1644
    using k'
wenzelm@53408
  1645
    apply auto
wenzelm@53408
  1646
    done
himmelma@35172
  1647
qed
himmelma@35172
  1648
wenzelm@53408
  1649
lemma partial_division_of_tagged_division:
wenzelm@53408
  1650
  assumes "s tagged_partial_division_of i"
himmelma@35172
  1651
  shows "(snd ` s) division_of \<Union>(snd ` s)"
wenzelm@53408
  1652
proof (rule division_ofI)
wenzelm@53408
  1653
  note assm = tagged_partial_division_ofD[OF assms]
wenzelm@53408
  1654
  show "finite (snd ` s)" "\<Union>(snd ` s) = \<Union>(snd ` s)"
wenzelm@53408
  1655
    using assm by auto
wenzelm@53408
  1656
  fix k
wenzelm@53408
  1657
  assume k: "k \<in> snd ` s"
wenzelm@53408
  1658
  then obtain xk where xk: "(xk, k) \<in> s"
wenzelm@53408
  1659
    by auto
immler@56188
  1660
  then show "k \<noteq> {}" "\<exists>a b. k = cbox a b" "k \<subseteq> \<Union>(snd ` s)"
wenzelm@53408
  1661
    using assm by auto
wenzelm@53408
  1662
  fix k'
wenzelm@53408
  1663
  assume k': "k' \<in> snd ` s" "k \<noteq> k'"
wenzelm@53408
  1664
  from this(1) obtain xk' where xk': "(xk', k') \<in> s"
wenzelm@53408
  1665
    by auto
wenzelm@53408
  1666
  then show "interior k \<inter> interior k' = {}"
wenzelm@53408
  1667
    apply -
wenzelm@53408
  1668
    apply (rule assm(5))
wenzelm@53408
  1669
    apply(rule xk xk')+
wenzelm@53408
  1670
    using k'
wenzelm@53408
  1671
    apply auto
wenzelm@53408
  1672
    done
himmelma@35172
  1673
qed
himmelma@35172
  1674
wenzelm@53408
  1675
lemma tagged_partial_division_subset:
wenzelm@53408
  1676
  assumes "s tagged_partial_division_of i"
wenzelm@53408
  1677
    and "t \<subseteq> s"
himmelma@35172
  1678
  shows "t tagged_partial_division_of i"
wenzelm@53408
  1679
  using assms
wenzelm@53408
  1680
  unfolding tagged_partial_division_of_def
wenzelm@53408
  1681
  using finite_subset[OF assms(2)]
wenzelm@53408
  1682
  by blast
wenzelm@53408
  1683
wenzelm@53408
  1684
lemma setsum_over_tagged_division_lemma:
immler@56188
  1685
  fixes d :: "'m::euclidean_space set \<Rightarrow> 'a::real_normed_vector"
wenzelm@53408
  1686
  assumes "p tagged_division_of i"
immler@56188
  1687
    and "\<And>u v. cbox u v \<noteq> {} \<Longrightarrow> content (cbox u v) = 0 \<Longrightarrow> d (cbox u v) = 0"
himmelma@35172
  1688
  shows "setsum (\<lambda>(x,k). d k) p = setsum d (snd ` p)"
wenzelm@53408
  1689
proof -
wenzelm@53408
  1690
  note assm = tagged_division_ofD[OF assms(1)]
wenzelm@53408
  1691
  have *: "(\<lambda>(x,k). d k) = d \<circ> snd"
wenzelm@53408
  1692
    unfolding o_def by (rule ext) auto
wenzelm@53408
  1693
  show ?thesis
wenzelm@53408
  1694
    unfolding *
wenzelm@53408
  1695
    apply (subst eq_commute)
wenzelm@53408
  1696
  proof (rule setsum_reindex_nonzero)
wenzelm@53408
  1697
    show "finite p"
wenzelm@53408
  1698
      using assm by auto
wenzelm@53408
  1699
    fix x y
wenzelm@53408
  1700
    assume as: "x\<in>p" "y\<in>p" "x\<noteq>y" "snd x = snd y"
immler@56188
  1701
    obtain a b where ab: "snd x = cbox a b"
wenzelm@53408
  1702
      using assm(4)[of "fst x" "snd x"] as(1) by auto
wenzelm@53408
  1703
    have "(fst x, snd y) \<in> p" "(fst x, snd y) \<noteq> y"
wenzelm@53408
  1704
      unfolding as(4)[symmetric] using as(1-3) by auto
wenzelm@53408
  1705
    then have "interior (snd x) \<inter> interior (snd y) = {}"
wenzelm@53408
  1706
      apply -
wenzelm@53408
  1707
      apply (rule assm(5)[of "fst x" _ "fst y"])
wenzelm@53408
  1708
      using as
wenzelm@53408
  1709
      apply auto
wenzelm@53408
  1710
      done
immler@56188
  1711
    then have "content (cbox a b) = 0"
wenzelm@53408
  1712
      unfolding as(4)[symmetric] ab content_eq_0_interior by auto
immler@56188
  1713
    then have "d (cbox a b) = 0"
wenzelm@53408
  1714
      apply -
wenzelm@53408
  1715
      apply (rule assms(2))
wenzelm@53408
  1716
      using assm(2)[of "fst x" "snd x"] as(1)
wenzelm@53408
  1717
      unfolding ab[symmetric]
wenzelm@53408
  1718
      apply auto
wenzelm@53408
  1719
      done
wenzelm@53408
  1720
    then show "d (snd x) = 0"
wenzelm@53408
  1721
      unfolding ab by auto
wenzelm@53408
  1722
  qed
wenzelm@53408
  1723
qed
wenzelm@53408
  1724
wenzelm@53408
  1725
lemma tag_in_interval: "p tagged_division_of i \<Longrightarrow> (x, k) \<in> p \<Longrightarrow> x \<in> i"
wenzelm@53408
  1726
  by auto
himmelma@35172
  1727
himmelma@35172
  1728
lemma tagged_division_of_empty: "{} tagged_division_of {}"
himmelma@35172
  1729
  unfolding tagged_division_of by auto
himmelma@35172
  1730
wenzelm@53408
  1731
lemma tagged_partial_division_of_trivial[simp]: "p tagged_partial_division_of {} \<longleftrightarrow> p = {}"
himmelma@35172
  1732
  unfolding tagged_partial_division_of_def by auto
himmelma@35172
  1733
wenzelm@53408
  1734
lemma tagged_division_of_trivial[simp]: "p tagged_division_of {} \<longleftrightarrow> p = {}"
himmelma@35172
  1735
  unfolding tagged_division_of by auto
himmelma@35172
  1736
immler@56188
  1737
lemma tagged_division_of_self: "x \<in> cbox a b \<Longrightarrow> {(x,cbox a b)} tagged_division_of (cbox a b)"
wenzelm@53408
  1738
  by (rule tagged_division_ofI) auto
himmelma@35172
  1739
immler@56188
  1740
lemma tagged_division_of_self_real: "x \<in> {a .. b::real} \<Longrightarrow> {(x,{a .. b})} tagged_division_of {a .. b}"
immler@56188
  1741
  unfolding box_real[symmetric]
immler@56188
  1742
  by (rule tagged_division_of_self)
immler@56188
  1743
himmelma@35172
  1744
lemma tagged_division_union:
wenzelm@53408
  1745
  assumes "p1 tagged_division_of s1"
wenzelm@53408
  1746
    and "p2 tagged_division_of s2"
wenzelm@53408
  1747
    and "interior s1 \<inter> interior s2 = {}"
himmelma@35172
  1748
  shows "(p1 \<union> p2) tagged_division_of (s1 \<union> s2)"
wenzelm@53408
  1749
proof (rule tagged_division_ofI)
wenzelm@53408
  1750
  note p1 = tagged_division_ofD[OF assms(1)]
wenzelm@53408
  1751
  note p2 = tagged_division_ofD[OF assms(2)]
wenzelm@53408
  1752
  show "finite (p1 \<union> p2)"
wenzelm@53408
  1753
    using p1(1) p2(1) by auto
wenzelm@53408
  1754
  show "\<Union>{k. \<exists>x. (x, k) \<in> p1 \<union> p2} = s1 \<union> s2"
wenzelm@53408
  1755
    using p1(6) p2(6) by blast
wenzelm@53408
  1756
  fix x k
wenzelm@53408
  1757
  assume xk: "(x, k) \<in> p1 \<union> p2"
immler@56188
  1758
  show "x \<in> k" "\<exists>a b. k = cbox a b"
wenzelm@53408
  1759
    using xk p1(2,4) p2(2,4) by auto
wenzelm@53408
  1760
  show "k \<subseteq> s1 \<union> s2"
wenzelm@53408
  1761
    using xk p1(3) p2(3) by blast
wenzelm@53408
  1762
  fix x' k'
wenzelm@53408
  1763
  assume xk': "(x', k') \<in> p1 \<union> p2" "(x, k) \<noteq> (x', k')"
wenzelm@53408
  1764
  have *: "\<And>a b. a \<subseteq> s1 \<Longrightarrow> b \<subseteq> s2 \<Longrightarrow> interior a \<inter> interior b = {}"
wenzelm@53408
  1765
    using assms(3) interior_mono by blast
wenzelm@53408
  1766
  show "interior k \<inter> interior k' = {}"
wenzelm@53408
  1767
    apply (cases "(x, k) \<in> p1")
wenzelm@53408
  1768
    apply (case_tac[!] "(x',k') \<in> p1")
wenzelm@53408
  1769
    apply (rule p1(5))
wenzelm@53408
  1770
    prefer 4
wenzelm@53408
  1771
    apply (rule *)
wenzelm@53408
  1772
    prefer 6
wenzelm@53408
  1773
    apply (subst Int_commute)
wenzelm@53408
  1774
    apply (rule *)
wenzelm@53408
  1775
    prefer 8
wenzelm@53408
  1776
    apply (rule p2(5))
wenzelm@53408
  1777
    using p1(3) p2(3)
wenzelm@53408
  1778
    using xk xk'
wenzelm@53408
  1779
    apply auto
wenzelm@53408
  1780
    done
wenzelm@53408
  1781
qed
himmelma@35172
  1782
himmelma@35172
  1783
lemma tagged_division_unions:
wenzelm@53408
  1784
  assumes "finite iset"
wenzelm@53408
  1785
    and "\<forall>i\<in>iset. pfn i tagged_division_of i"
wenzelm@53408
  1786
    and "\<forall>i1\<in>iset. \<forall>i2\<in>iset. i1 \<noteq> i2 \<longrightarrow> interior(i1) \<inter> interior(i2) = {}"
himmelma@35172
  1787
  shows "\<Union>(pfn ` iset) tagged_division_of (\<Union>iset)"
wenzelm@53408
  1788
proof (rule tagged_division_ofI)
himmelma@35172
  1789
  note assm = tagged_division_ofD[OF assms(2)[rule_format]]
wenzelm@53408
  1790
  show "finite (\<Union>(pfn ` iset))"
wenzelm@53408
  1791
    apply (rule finite_Union)
wenzelm@53408
  1792
    using assms
wenzelm@53408
  1793
    apply auto
wenzelm@53408
  1794
    done
wenzelm@53408
  1795
  have "\<Union>{k. \<exists>x. (x, k) \<in> \<Union>(pfn ` iset)} = \<Union>((\<lambda>i. \<Union>{k. \<exists>x. (x, k) \<in> pfn i}) ` iset)"
wenzelm@53408
  1796
    by blast
wenzelm@53408
  1797
  also have "\<dots> = \<Union>iset"
wenzelm@53408
  1798
    using assm(6) by auto
wenzelm@53399
  1799
  finally show "\<Union>{k. \<exists>x. (x, k) \<in> \<Union>(pfn ` iset)} = \<Union>iset" .
wenzelm@53408
  1800
  fix x k
wenzelm@53408
  1801
  assume xk: "(x, k) \<in> \<Union>(pfn ` iset)"
wenzelm@53408
  1802
  then obtain i where i: "i \<in> iset" "(x, k) \<in> pfn i"
wenzelm@53408
  1803
    by auto
immler@56188
  1804
  show "x \<in> k" "\<exists>a b. k = cbox a b" "k \<subseteq> \<Union>iset"
wenzelm@53408
  1805
    using assm(2-4)[OF i] using i(1) by auto
wenzelm@53408
  1806
  fix x' k'
wenzelm@53408
  1807
  assume xk': "(x', k') \<in> \<Union>(pfn ` iset)" "(x, k) \<noteq> (x', k')"
wenzelm@53408
  1808
  then obtain i' where i': "i' \<in> iset" "(x', k') \<in> pfn i'"
wenzelm@53408
  1809
    by auto
wenzelm@53408
  1810
  have *: "\<And>a b. i \<noteq> i' \<Longrightarrow> a \<subseteq> i \<Longrightarrow> b \<subseteq> i' \<Longrightarrow> interior a \<inter> interior b = {}"
wenzelm@53408
  1811
    using i(1) i'(1)
wenzelm@53408
  1812
    using assms(3)[rule_format] interior_mono
wenzelm@53408
  1813
    by blast
wenzelm@53408
  1814
  show "interior k \<inter> interior k' = {}"
wenzelm@53408
  1815
    apply (cases "i = i'")
wenzelm@53408
  1816
    using assm(5)[OF i _ xk'(2)] i'(2)
wenzelm@53408
  1817
    using assm(3)[OF i] assm(3)[OF i']
wenzelm@53408
  1818
    defer
wenzelm@53408
  1819
    apply -
wenzelm@53408
  1820
    apply (rule *)
wenzelm@53408
  1821
    apply auto
wenzelm@53408
  1822
    done
himmelma@35172
  1823
qed
himmelma@35172
  1824
himmelma@35172
  1825
lemma tagged_partial_division_of_union_self:
wenzelm@53408
  1826
  assumes "p tagged_partial_division_of s"
himmelma@35172
  1827
  shows "p tagged_division_of (\<Union>(snd ` p))"
wenzelm@53408
  1828
  apply (rule tagged_division_ofI)
wenzelm@53408
  1829
  using tagged_partial_division_ofD[OF assms]
wenzelm@53408
  1830
  apply auto
wenzelm@53408
  1831
  done
wenzelm@53408
  1832
wenzelm@53408
  1833
lemma tagged_division_of_union_self:
wenzelm@53408
  1834
  assumes "p tagged_division_of s"
wenzelm@53408
  1835
  shows "p tagged_division_of (\<Union>(snd ` p))"
wenzelm@53408
  1836
  apply (rule tagged_division_ofI)
wenzelm@53408
  1837
  using tagged_division_ofD[OF assms]
wenzelm@53408
  1838
  apply auto
wenzelm@53408
  1839
  done
wenzelm@53408
  1840
himmelma@35172
  1841
himmelma@35172
  1842
subsection {* Fine-ness of a partition w.r.t. a gauge. *}
himmelma@35172
  1843
wenzelm@53408
  1844
definition fine  (infixr "fine" 46)
wenzelm@53408
  1845
  where "d fine s \<longleftrightarrow> (\<forall>(x,k) \<in> s. k \<subseteq> d x)"
wenzelm@53408
  1846
wenzelm@53408
  1847
lemma fineI:
wenzelm@53408
  1848
  assumes "\<And>x k. (x, k) \<in> s \<Longrightarrow> k \<subseteq> d x"
wenzelm@53408
  1849
  shows "d fine s"
wenzelm@53408
  1850
  using assms unfolding fine_def by auto
wenzelm@53408
  1851
wenzelm@53408
  1852
lemma fineD[dest]:
wenzelm@53408
  1853
  assumes "d fine s"
wenzelm@53408
  1854
  shows "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> d x"
wenzelm@53408
  1855
  using assms unfolding fine_def by auto
himmelma@35172
  1856
himmelma@35172
  1857
lemma fine_inter: "(\<lambda>x. d1 x \<inter> d2 x) fine p \<longleftrightarrow> d1 fine p \<and> d2 fine p"
himmelma@35172
  1858
  unfolding fine_def by auto
himmelma@35172
  1859
himmelma@35172
  1860
lemma fine_inters:
himmelma@35172
  1861
 "(\<lambda>x. \<Inter> {f d x | d.  d \<in> s}) fine p \<longleftrightarrow> (\<forall>d\<in>s. (f d) fine p)"
himmelma@35172
  1862
  unfolding fine_def by blast
himmelma@35172
  1863
wenzelm@53408
  1864
lemma fine_union: "d fine p1 \<Longrightarrow> d fine p2 \<Longrightarrow> d fine (p1 \<union> p2)"
himmelma@35172
  1865
  unfolding fine_def by blast
himmelma@35172
  1866
wenzelm@53408
  1867
lemma fine_unions: "(\<And>p. p \<in> ps \<Longrightarrow> d fine p) \<Longrightarrow> d fine (\<Union>ps)"
himmelma@35172
  1868
  unfolding fine_def by auto
himmelma@35172
  1869
wenzelm@53408
  1870
lemma fine_subset: "p \<subseteq> q \<Longrightarrow> d fine q \<Longrightarrow> d fine p"
himmelma@35172
  1871
  unfolding fine_def by blast
himmelma@35172
  1872
wenzelm@53408
  1873
himmelma@35172
  1874
subsection {* Gauge integral. Define on compact intervals first, then use a limit. *}
himmelma@35172
  1875
wenzelm@53408
  1876
definition has_integral_compact_interval (infixr "has'_integral'_compact'_interval" 46)
wenzelm@53408
  1877
  where "(f has_integral_compact_interval y) i \<longleftrightarrow>
wenzelm@53408
  1878
    (\<forall>e>0. \<exists>d. gauge d \<and>
wenzelm@53408
  1879
      (\<forall>p. p tagged_division_of i \<and> d fine p \<longrightarrow>
wenzelm@53408
  1880
        norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - y) < e))"
wenzelm@53408
  1881
wenzelm@53408
  1882
definition has_integral ::
immler@56188
  1883
    "('n::euclidean_space \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'b \<Rightarrow> 'n set \<Rightarrow> bool"
wenzelm@53408
  1884
  (infixr "has'_integral" 46)
wenzelm@53408
  1885
  where "(f has_integral y) i \<longleftrightarrow>
immler@56188
  1886
    (if \<exists>a b. i = cbox a b
wenzelm@53408
  1887
     then (f has_integral_compact_interval y) i
immler@56188
  1888
     else (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
immler@56188
  1889
      (\<exists>z. ((\<lambda>x. if x \<in> i then f x else 0) has_integral_compact_interval z) (cbox a b) \<and>
wenzelm@53408
  1890
        norm (z - y) < e)))"
himmelma@35172
  1891
himmelma@35172
  1892
lemma has_integral:
immler@56188
  1893
  "(f has_integral y) (cbox a b) \<longleftrightarrow>
wenzelm@53408
  1894
    (\<forall>e>0. \<exists>d. gauge d \<and>
immler@56188
  1895
      (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow>
wenzelm@53408
  1896
        norm (setsum (\<lambda>(x,k). content(k) *\<^sub>R f x) p - y) < e))"
wenzelm@53408
  1897
  unfolding has_integral_def has_integral_compact_interval_def
wenzelm@53408
  1898
  by auto
wenzelm@53408
  1899
immler@56188
  1900
lemma has_integral_real:
immler@56188
  1901
  "(f has_integral y) {a .. b::real} \<longleftrightarrow>
immler@56188
  1902
    (\<forall>e>0. \<exists>d. gauge d \<and>
immler@56188
  1903
      (\<forall>p. p tagged_division_of {a .. b} \<and> d fine p \<longrightarrow>
immler@56188
  1904
        norm (setsum (\<lambda>(x,k). content(k) *\<^sub>R f x) p - y) < e))"
immler@56188
  1905
  unfolding box_real[symmetric]
immler@56188
  1906
  by (rule has_integral)
immler@56188
  1907
wenzelm@53408
  1908
lemma has_integralD[dest]:
immler@56188
  1909
  assumes "(f has_integral y) (cbox a b)"
wenzelm@53408
  1910
    and "e > 0"
wenzelm@53408
  1911
  obtains d where "gauge d"
immler@56188
  1912
    and "\<And>p. p tagged_division_of (cbox a b) \<Longrightarrow> d fine p \<Longrightarrow>
wenzelm@53408
  1913
      norm (setsum (\<lambda>(x,k). content(k) *\<^sub>R f(x)) p - y) < e"
himmelma@35172
  1914
  using assms unfolding has_integral by auto
himmelma@35172
  1915
himmelma@35172
  1916
lemma has_integral_alt:
wenzelm@53408
  1917
  "(f has_integral y) i \<longleftrightarrow>
immler@56188
  1918
    (if \<exists>a b. i = cbox a b
wenzelm@53408
  1919
     then (f has_integral y) i
immler@56188
  1920
     else (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
immler@56188
  1921
      (\<exists>z. ((\<lambda>x. if x \<in> i then f(x) else 0) has_integral z) (cbox a b) \<and> norm (z - y) < e)))"
wenzelm@53408
  1922
  unfolding has_integral
wenzelm@53408
  1923
  unfolding has_integral_compact_interval_def has_integral_def
wenzelm@53408
  1924
  by auto
himmelma@35172
  1925
himmelma@35172
  1926
lemma has_integral_altD:
wenzelm@53408
  1927
  assumes "(f has_integral y) i"
immler@56188
  1928
    and "\<not> (\<exists>a b. i = cbox a b)"
wenzelm@53408
  1929
    and "e>0"
wenzelm@53408
  1930
  obtains B where "B > 0"
immler@56188
  1931
    and "\<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
immler@56188
  1932
      (\<exists>z. ((\<lambda>x. if x \<in> i then f(x) else 0) has_integral z) (cbox a b) \<and> norm(z - y) < e)"
wenzelm@53408
  1933
  using assms
wenzelm@53408
  1934
  unfolding has_integral
wenzelm@53408
  1935
  unfolding has_integral_compact_interval_def has_integral_def
wenzelm@53408
  1936
  by auto
wenzelm@53408
  1937
wenzelm@53408
  1938
definition integrable_on (infixr "integrable'_on" 46)
wenzelm@53408
  1939
  where "f integrable_on i \<longleftrightarrow> (\<exists>y. (f has_integral y) i)"
wenzelm@53408
  1940
wenzelm@53408
  1941
definition "integral i f = (SOME y. (f has_integral y) i)"
himmelma@35172
  1942
wenzelm@53409
  1943
lemma integrable_integral[dest]: "f integrable_on i \<Longrightarrow> (f has_integral (integral i f)) i"
wenzelm@53409
  1944
  unfolding integrable_on_def integral_def by (rule someI_ex)
himmelma@35172
  1945
himmelma@35172
  1946
lemma has_integral_integrable[intro]: "(f has_integral i) s \<Longrightarrow> f integrable_on s"
himmelma@35172
  1947
  unfolding integrable_on_def by auto
himmelma@35172
  1948
wenzelm@53409
  1949
lemma has_integral_integral: "f integrable_on s \<longleftrightarrow> (f has_integral (integral s f)) s"
himmelma@35172
  1950
  by auto
himmelma@35172
  1951
himmelma@35172
  1952
lemma setsum_content_null:
immler@56188
  1953
  assumes "content (cbox a b) = 0"
immler@56188
  1954
    and "p tagged_division_of (cbox a b)"
himmelma@35172
  1955
  shows "setsum (\<lambda>(x,k). content k *\<^sub>R f x) p = (0::'a::real_normed_vector)"
wenzelm@53409
  1956
proof (rule setsum_0', rule)
wenzelm@53409
  1957
  fix y
wenzelm@53409
  1958
  assume y: "y \<in> p"
wenzelm@53409
  1959
  obtain x k where xk: "y = (x, k)"
wenzelm@53409
  1960
    using surj_pair[of y] by blast
himmelma@35172
  1961
  note assm = tagged_division_ofD(3-4)[OF assms(2) y[unfolded xk]]
immler@56188
  1962
  from this(2) obtain c d where k: "k = cbox c d" by blast
wenzelm@53409
  1963
  have "(\<lambda>(x, k). content k *\<^sub>R f x) y = content k *\<^sub>R f x"
wenzelm@53409
  1964
    unfolding xk by auto
wenzelm@53409
  1965
  also have "\<dots> = 0"
wenzelm@53409
  1966
    using content_subset[OF assm(1)[unfolded k]] content_pos_le[of c d]
wenzelm@53409
  1967
    unfolding assms(1) k
wenzelm@53409
  1968
    by auto
himmelma@35172
  1969
  finally show "(\<lambda>(x, k). content k *\<^sub>R f x) y = 0" .
himmelma@35172
  1970
qed
himmelma@35172
  1971
wenzelm@53409
  1972
himmelma@35172
  1973
subsection {* Some basic combining lemmas. *}
himmelma@35172
  1974
himmelma@35172
  1975
lemma tagged_division_unions_exists:
wenzelm@53409
  1976
  assumes "finite iset"
wenzelm@53409
  1977
    and "\<forall>i\<in>iset. \<exists>p. p tagged_division_of i \<and> d fine p"
wenzelm@53409
  1978
    and "\<forall>i1\<in>iset. \<forall>i2\<in>iset. i1 \<noteq> i2 \<longrightarrow> interior i1 \<inter> interior i2 = {}"
wenzelm@53409
  1979
    and "\<Union>iset = i"
wenzelm@53409
  1980
   obtains p where "p tagged_division_of i" and "d fine p"
wenzelm@53409
  1981
proof -
wenzelm@53409
  1982
  obtain pfn where pfn:
wenzelm@53409
  1983
    "\<And>x. x \<in> iset \<Longrightarrow> pfn x tagged_division_of x"
wenzelm@53409
  1984
    "\<And>x. x \<in> iset \<Longrightarrow> d fine pfn x"
wenzelm@53409
  1985
    using bchoice[OF assms(2)] by auto
wenzelm@53409
  1986
  show thesis
wenzelm@53409
  1987
    apply (rule_tac p="\<Union>(pfn ` iset)" in that)
wenzelm@53409
  1988
    unfolding assms(4)[symmetric]
wenzelm@53409
  1989
    apply (rule tagged_division_unions[OF assms(1) _ assms(3)])
wenzelm@53409
  1990
    defer
wenzelm@53409
  1991
    apply (rule fine_unions)
wenzelm@53409
  1992
    using pfn
wenzelm@53409
  1993
    apply auto
wenzelm@53409
  1994
    done
himmelma@35172
  1995
qed
himmelma@35172
  1996
wenzelm@53409
  1997
himmelma@35172
  1998
subsection {* The set we're concerned with must be closed. *}
himmelma@35172
  1999
wenzelm@53409
  2000
lemma division_of_closed:
wenzelm@53409
  2001
  fixes i :: "'n::ordered_euclidean_space set"
wenzelm@53409
  2002
  shows "s division_of i \<Longrightarrow> closed i"
nipkow@44890
  2003
  unfolding division_of_def by fastforce
himmelma@35172
  2004
himmelma@35172
  2005
subsection {* General bisection principle for intervals; might be useful elsewhere. *}
himmelma@35172
  2006
wenzelm@53409
  2007
lemma interval_bisection_step:
immler@56188
  2008
  fixes type :: "'a::euclidean_space"
wenzelm@53409
  2009
  assumes "P {}"
wenzelm@53409
  2010
    and "\<forall>s t. P s \<and> P t \<and> interior(s) \<inter> interior(t) = {} \<longrightarrow> P (s \<union> t)"
immler@56188
  2011
    and "\<not> P (cbox a (b::'a))"
immler@56188
  2012
  obtains c d where "\<not> P (cbox c d)"
wenzelm@53409
  2013
    and "\<forall>i\<in>Basis. a\<bullet>i \<le> c\<bullet>i \<and> c\<bullet>i \<le> d\<bullet>i \<and> d\<bullet>i \<le> b\<bullet>i \<and> 2 * (d\<bullet>i - c\<bullet>i) \<le> b\<bullet>i - a\<bullet>i"
wenzelm@53409
  2014
proof -
immler@56188
  2015
  have "cbox a b \<noteq> {}"
immler@54776
  2016
    using assms(1,3) by metis
wenzelm@53409
  2017
  then have ab: "\<And>i. i\<in>Basis \<Longrightarrow> a \<bullet> i \<le> b \<bullet> i"
immler@56188
  2018
    by (force simp: mem_box)
wenzelm@53409
  2019
  {
wenzelm@53409
  2020
    fix f
wenzelm@53409
  2021
    have "finite f \<Longrightarrow>
wenzelm@53409
  2022
      \<forall>s\<in>f. P s \<Longrightarrow>
immler@56188
  2023
      \<forall>s\<in>f. \<exists>a b. s = cbox a b \<Longrightarrow>
wenzelm@53409
  2024
      \<forall>s\<in>f.\<forall>t\<in>f. s \<noteq> t \<longrightarrow> interior s \<inter> interior t = {} \<Longrightarrow> P (\<Union>f)"
wenzelm@53409
  2025
    proof (induct f rule: finite_induct)
wenzelm@53409
  2026
      case empty
wenzelm@53409
  2027
      show ?case
wenzelm@53409
  2028
        using assms(1) by auto
wenzelm@53409
  2029
    next
wenzelm@53409
  2030
      case (insert x f)
wenzelm@53409
  2031
      show ?case
wenzelm@53409
  2032
        unfolding Union_insert
wenzelm@53409
  2033
        apply (rule assms(2)[rule_format])
wenzelm@53409
  2034
        apply rule
wenzelm@53409
  2035
        defer
wenzelm@53409
  2036
        apply rule
wenzelm@53409
  2037
        defer
wenzelm@53409
  2038
        apply (rule inter_interior_unions_intervals)
wenzelm@53409
  2039
        using insert
wenzelm@53409
  2040
        apply auto
wenzelm@53409
  2041
        done
wenzelm@53409
  2042
    qed
wenzelm@53409
  2043
  } note * = this
immler@56188
  2044
  let ?A = "{cbox c d | c d::'a. \<forall>i\<in>Basis. (c\<bullet>i = a\<bullet>i) \<and> (d\<bullet>i = (a\<bullet>i + b\<bullet>i) / 2) \<or>
wenzelm@53409
  2045
    (c\<bullet>i = (a\<bullet>i + b\<bullet>i) / 2) \<and> (d\<bullet>i = b\<bullet>i)}"
hoelzl@50526
  2046
  let ?PP = "\<lambda>c d. \<forall>i\<in>Basis. a\<bullet>i \<le> c\<bullet>i \<and> c\<bullet>i \<le> d\<bullet>i \<and> d\<bullet>i \<le> b\<bullet>i \<and> 2 * (d\<bullet>i - c\<bullet>i) \<le> b\<bullet>i - a\<bullet>i"
wenzelm@53409
  2047
  {
immler@56188
  2048
    presume "\<forall>c d. ?PP c d \<longrightarrow> P (cbox c d) \<Longrightarrow> False"
wenzelm@53409
  2049
    then show thesis
wenzelm@53409
  2050
      unfolding atomize_not not_all
wenzelm@53409
  2051
      apply -
wenzelm@53409
  2052
      apply (erule exE)+
wenzelm@53409
  2053
      apply (rule_tac c=x and d=xa in that)
wenzelm@53409
  2054
      apply auto
wenzelm@53409
  2055
      done
wenzelm@53409
  2056
  }
immler@56188
  2057
  assume as: "\<forall>c d. ?PP c d \<longrightarrow> P (cbox c d)"
wenzelm@53409
  2058
  have "P (\<Union> ?A)"
wenzelm@53409
  2059
    apply (rule *)
wenzelm@53409
  2060
    apply (rule_tac[2-] ballI)
wenzelm@53409
  2061
    apply (rule_tac[4] ballI)
wenzelm@53409
  2062
    apply (rule_tac[4] impI)
wenzelm@53409
  2063
  proof -
immler@56188
  2064
    let ?B = "(\<lambda>s. cbox (\<Sum>i\<in>Basis. (if i \<in> s then a\<bullet>i else (a\<bullet>i + b\<bullet>i) / 2) *\<^sub>R i::'a)
immler@56188
  2065
      (\<Sum>i\<in>Basis. (if i \<in> s then (a\<bullet>i + b\<bullet>i) / 2 else b\<bullet>i) *\<^sub>R i)) ` {s. s \<subseteq> Basis}"
wenzelm@53409
  2066
    have "?A \<subseteq> ?B"
wenzelm@53409
  2067
    proof
wenzelm@53409
  2068
      case goal1
immler@56188
  2069
      then obtain c d where x: "x = cbox c d"
wenzelm@53409
  2070
        "\<And>i. i \<in> Basis \<Longrightarrow>
wenzelm@53409
  2071
          c \<bullet> i = a \<bullet> i \<and> d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
wenzelm@53409
  2072
          c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> d \<bullet> i = b \<bullet> i" by blast
immler@56188
  2073
      have *: "\<And>a b c d. a = c \<Longrightarrow> b = d \<Longrightarrow> cbox a b = cbox c d"
wenzelm@53409
  2074
        by auto
wenzelm@53409
  2075
      show "x \<in> ?B"
wenzelm@53409
  2076
        unfolding image_iff
wenzelm@53409
  2077
        apply (rule_tac x="{i. i\<in>Basis \<and> c\<bullet>i = a\<bullet>i}" in bexI)
wenzelm@53409
  2078
        unfolding x
wenzelm@53409
  2079
        apply (rule *)
hoelzl@50526
  2080
        apply (simp_all only: euclidean_eq_iff[where 'a='a] inner_setsum_left_Basis mem_Collect_eq simp_thms
wenzelm@53409
  2081
          cong: ball_cong)
hoelzl@50526
  2082
        apply safe
wenzelm@53409
  2083
      proof -
wenzelm@53409
  2084
        fix i :: 'a
wenzelm@53409
  2085
        assume i: "i \<in> Basis"
wenzelm@53409
  2086
        then show "c \<bullet> i = (if c \<bullet> i = a \<bullet> i then a \<bullet> i else (a \<bullet> i + b \<bullet> i) / 2)"
wenzelm@53409
  2087
          and "d \<bullet> i = (if c \<bullet> i = a \<bullet> i then (a \<bullet> i + b \<bullet> i) / 2 else b \<bullet> i)"
wenzelm@53409
  2088
          using x(2)[of i] ab[OF i] by (auto simp add:field_simps)
wenzelm@53409
  2089
      qed
wenzelm@53409
  2090
    qed
wenzelm@53409
  2091
    then show "finite ?A"
wenzelm@53409
  2092
      by (rule finite_subset) auto
wenzelm@53409
  2093
    fix s
wenzelm@53409
  2094
    assume "s \<in> ?A"
wenzelm@53409
  2095
    then obtain c d where s:
immler@56188
  2096
      "s = cbox c d"
wenzelm@53409
  2097
      "\<And>i. i \<in> Basis \<Longrightarrow>
wenzelm@53409
  2098
         c \<bullet> i = a \<bullet> i \<and> d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
wenzelm@53409
  2099
         c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> d \<bullet> i = b \<bullet> i"
wenzelm@53409
  2100
      by blast
wenzelm@53409
  2101
    show "P s"
wenzelm@53409
  2102
      unfolding s
wenzelm@53409
  2103
      apply (rule as[rule_format])
wenzelm@53409
  2104
    proof -
wenzelm@53409
  2105
      case goal1
wenzelm@53409
  2106
      then show ?case
wenzelm@53409
  2107
        using s(2)[of i] using ab[OF `i \<in> Basis`] by auto
wenzelm@53409
  2108
    qed
immler@56188
  2109
    show "\<exists>a b. s = cbox a b"
wenzelm@53409
  2110
      unfolding s by auto
wenzelm@53409
  2111
    fix t
wenzelm@53409
  2112
    assume "t \<in> ?A"
wenzelm@53409
  2113
    then obtain e f where t:
immler@56188
  2114
      "t = cbox e f"
wenzelm@53409
  2115
      "\<And>i. i \<in> Basis \<Longrightarrow>
wenzelm@53409
  2116
        e \<bullet> i = a \<bullet> i \<and> f \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
wenzelm@53409
  2117
        e \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> f \<bullet> i = b \<bullet> i"
wenzelm@53409
  2118
      by blast
wenzelm@53409
  2119
    assume "s \<noteq> t"
wenzelm@53409
  2120
    then have "\<not> (c = e \<and> d = f)"
wenzelm@53409
  2121
      unfolding s t by auto
wenzelm@53409
  2122
    then obtain i where "c\<bullet>i \<noteq> e\<bullet>i \<or> d\<bullet>i \<noteq> f\<bullet>i" and i': "i \<in> Basis"
hoelzl@50526
  2123
      unfolding euclidean_eq_iff[where 'a='a] by auto
wenzelm@53409
  2124
    then have i: "c\<bullet>i \<noteq> e\<bullet>i" "d\<bullet>i \<noteq> f\<bullet>i"
wenzelm@53409
  2125
      apply -
wenzelm@53409
  2126
      apply(erule_tac[!] disjE)
wenzelm@53409
  2127
    proof -
wenzelm@53409
  2128
      assume "c\<bullet>i \<noteq> e\<bullet>i"
wenzelm@53409
  2129
      then show "d\<bullet>i \<noteq> f\<bullet>i"
wenzelm@53409
  2130
        using s(2)[OF i'] t(2)[OF i'] by fastforce
wenzelm@53409
  2131
    next
wenzelm@53409
  2132
      assume "d\<bullet>i \<noteq> f\<bullet>i"
wenzelm@53409
  2133
      then show "c\<bullet>i \<noteq> e\<bullet>i"
wenzelm@53409
  2134
        using s(2)[OF i'] t(2)[OF i'] by fastforce
wenzelm@53409
  2135
    qed
wenzelm@53409
  2136
    have *: "\<And>s t. (\<And>a. a \<in> s \<Longrightarrow> a \<in> t \<Longrightarrow> False) \<Longrightarrow> s \<inter> t = {}"
wenzelm@53409
  2137
      by auto
wenzelm@53409
  2138
    show "interior s \<inter> interior t = {}"
immler@56188
  2139
      unfolding s t interior_cbox
wenzelm@53409
  2140
    proof (rule *)
wenzelm@53409
  2141
      fix x
immler@54775
  2142
      assume "x \<in> box c d" "x \<in> box e f"
wenzelm@53409
  2143
      then have x: "c\<bullet>i < d\<bullet>i" "e\<bullet>i < f\<bullet>i" "c\<bullet>i < f\<bullet>i" "e\<bullet>i < d\<bullet>i"
immler@56188
  2144
        unfolding mem_box using i'
wenzelm@53409
  2145
        apply -
wenzelm@53409
  2146
        apply (erule_tac[!] x=i in ballE)+
wenzelm@53409
  2147
        apply auto
wenzelm@53409
  2148
        done
wenzelm@53409
  2149
      show False
wenzelm@53409
  2150
        using s(2)[OF i']
wenzelm@53409
  2151
        apply -
wenzelm@53409
  2152
        apply (erule_tac disjE)
wenzelm@53409
  2153
        apply (erule_tac[!] conjE)
wenzelm@53409
  2154
      proof -
wenzelm@53409
  2155
        assume as: "c \<bullet> i = a \<bullet> i" "d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2"
wenzelm@53409
  2156
        show False
wenzelm@53409
  2157
          using t(2)[OF i'] and i x unfolding as by (fastforce simp add:field_simps)
wenzelm@53409
  2158
      next
wenzelm@53409
  2159
        assume as: "c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2" "d \<bullet> i = b \<bullet> i"
wenzelm@53409
  2160
        show False
wenzelm@53409
  2161
          using t(2)[OF i'] and i x unfolding as by(fastforce simp add:field_simps)
wenzelm@53409
  2162
      qed
wenzelm@53409
  2163
    qed
wenzelm@53409
  2164
  qed
immler@56188
  2165
  also have "\<Union> ?A = cbox a b"
wenzelm@53409
  2166
  proof (rule set_eqI,rule)
wenzelm@53409
  2167
    fix x
wenzelm@53409
  2168
    assume "x \<in> \<Union>?A"
wenzelm@53409
  2169
    then obtain c d where x:
immler@56188
  2170
      "x \<in> cbox c d"
wenzelm@53409
  2171
      "\<And>i. i \<in> Basis \<Longrightarrow>
wenzelm@53409
  2172
        c \<bullet> i = a \<bullet> i \<and> d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
wenzelm@53409
  2173
        c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> d \<bullet> i = b \<bullet> i" by blast
immler@56188
  2174
    show "x\<in>cbox a b"
immler@56188
  2175
      unfolding mem_box
wenzelm@53409
  2176
    proof safe
wenzelm@53409
  2177
      fix i :: 'a
wenzelm@53409
  2178
      assume i: "i \<in> Basis"
wenzelm@53409
  2179
      then show "a \<bullet> i \<le> x \<bullet> i" "x \<bullet> i \<le> b \<bullet> i"
immler@56188
  2180
        using x(2)[OF i] x(1)[unfolded mem_box,THEN bspec, OF i] by auto
wenzelm@53409
  2181
    qed
wenzelm@53409
  2182
  next
wenzelm@53409
  2183
    fix x
immler@56188
  2184
    assume x: "x \<in> cbox a b"
wenzelm@53409
  2185
    have "\<forall>i\<in>Basis.
wenzelm@53409
  2186
      \<exists>c d. (c = a\<bullet>i \<and> d = (a\<bullet>i + b\<bullet>i) / 2 \<or> c = (a\<bullet>i + b\<bullet>i) / 2 \<and> d = b\<bullet>i) \<and> c\<le>x\<bullet>i \<and> x\<bullet>i \<le> d"
wenzelm@53409
  2187
      (is "\<forall>i\<in>Basis. \<exists>c d. ?P i c d")
immler@56188
  2188
      unfolding mem_box
hoelzl@50526
  2189
    proof
wenzelm@53409
  2190
      fix i :: 'a
wenzelm@53409
  2191
      assume i: "i \<in> Basis"
hoelzl@50526
  2192
      have "?P i (a\<bullet>i) ((a \<bullet> i + b \<bullet> i) / 2) \<or> ?P i ((a \<bullet> i + b \<bullet> i) / 2) (b\<bullet>i)"
immler@56188
  2193
        using x[unfolded mem_box,THEN bspec, OF i] by auto
wenzelm@53409
  2194
      then show "\<exists>c d. ?P i c d"
wenzelm@53409
  2195
        by blast
hoelzl@50526
  2196
    qed
wenzelm@53409
  2197
    then show "x\<in>\<Union>?A"
hoelzl@50526
  2198
      unfolding Union_iff Bex_def mem_Collect_eq choice_Basis_iff
wenzelm@53409
  2199
      apply -
wenzelm@53409
  2200
      apply (erule exE)+
immler@56188
  2201
      apply (rule_tac x="cbox xa xaa" in exI)
immler@56188
  2202
      unfolding mem_box
wenzelm@53409
  2203
      apply auto
wenzelm@53409
  2204
      done
wenzelm@53409
  2205
  qed
wenzelm@53409
  2206
  finally show False
wenzelm@53409
  2207
    using assms by auto
wenzelm@53409
  2208
qed
wenzelm@53409
  2209
wenzelm@53409
  2210
lemma interval_bisection:
immler@56188
  2211
  fixes type :: "'a::euclidean_space"
wenzelm@53409
  2212
  assumes "P {}"
wenzelm@53409
  2213
    and "(\<forall>s t. P s \<and> P t \<and> interior(s) \<inter> interior(t) = {} \<longrightarrow> P(s \<union> t))"
immler@56188
  2214
    and "\<not> P (cbox a (b::'a))"
immler@56188
  2215
  obtains x where "x \<in> cbox a b"
immler@56188
  2216
    and "\<forall>e>0. \<exists>c d. x \<in> cbox c d \<and> cbox c d \<subseteq> ball x e \<and> cbox c d \<subseteq> cbox a b \<and> \<not> P (cbox c d)"
immler@56188
  2217
proof -
immler@56188
  2218
  have "\<forall>x. \<exists>y. \<not> P (cbox (fst x) (snd x)) \<longrightarrow> (\<not> P (cbox (fst y) (snd y)) \<and>
hoelzl@50526
  2219
    (\<forall>i\<in>Basis. fst x\<bullet>i \<le> fst y\<bullet>i \<and> fst y\<bullet>i \<le> snd y\<bullet>i \<and> snd y\<bullet>i \<le> snd x\<bullet>i \<and>
wenzelm@53409
  2220
       2 * (snd y\<bullet>i - fst y\<bullet>i) \<le> snd x\<bullet>i - fst x\<bullet>i))"
wenzelm@53409
  2221
  proof
wenzelm@53409
  2222
    case goal1
wenzelm@53409
  2223
    then show ?case
wenzelm@53409
  2224
    proof -
immler@56188
  2225
      presume "\<not> P (cbox (fst x) (snd x)) \<Longrightarrow> ?thesis"
immler@56188
  2226
      then show ?thesis by (cases "P (cbox (fst x) (snd x))") auto
wenzelm@53409
  2227
    next
immler@56188
  2228
      assume as: "\<not> P (cbox (fst x) (snd x))"
immler@56188
  2229
      obtain c d where "\<not> P (cbox c d)"
wenzelm@53409
  2230
        "\<forall>i\<in>Basis.
wenzelm@53409
  2231
           fst x \<bullet> i \<le> c \<bullet> i \<and>
wenzelm@53409
  2232
           c \<bullet> i \<le> d \<bullet> i \<and>
wenzelm@53409
  2233
           d \<bullet> i \<le> snd x \<bullet> i \<and>
wenzelm@53409
  2234
           2 * (d \<bullet> i - c \<bullet> i) \<le> snd x \<bullet> i - fst x \<bullet> i"
wenzelm@53409
  2235
        by (rule interval_bisection_step[of P, OF assms(1-2) as])
wenzelm@53409
  2236
      then show ?thesis
wenzelm@53409
  2237
        apply -
wenzelm@53409
  2238
        apply (rule_tac x="(c,d)" in exI)
wenzelm@53409
  2239
        apply auto
wenzelm@53409
  2240
        done
wenzelm@53409
  2241
    qed
wenzelm@53409
  2242
  qed
wenzelm@55751
  2243
  then obtain f where f:
wenzelm@55751
  2244
    "\<forall>x.
immler@56188
  2245
      \<not> P (cbox (fst x) (snd x)) \<longrightarrow>
immler@56188
  2246
      \<not> P (cbox (fst (f x)) (snd (f x))) \<and>
wenzelm@55751
  2247
        (\<forall>i\<in>Basis.
wenzelm@55751
  2248
            fst x \<bullet> i \<le> fst (f x) \<bullet> i \<and>
wenzelm@55751
  2249
            fst (f x) \<bullet> i \<le> snd (f x) \<bullet> i \<and>
wenzelm@55751
  2250
            snd (f x) \<bullet> i \<le> snd x \<bullet> i \<and>
wenzelm@55751
  2251
            2 * (snd (f x) \<bullet> i - fst (f x) \<bullet> i) \<le> snd x \<bullet> i - fst x \<bullet> i)"
wenzelm@53409
  2252
    apply -
wenzelm@53409
  2253
    apply (drule choice)
wenzelm@55751
  2254
    apply blast
wenzelm@55751
  2255
    done
wenzelm@53409
  2256
  def AB \<equiv> "\<lambda>n. (f ^^ n) (a,b)"
wenzelm@53409
  2257
  def A \<equiv> "\<lambda>n. fst(AB n)"
wenzelm@53409
  2258
  def B \<equiv> "\<lambda>n. snd(AB n)"
wenzelm@53409
  2259
  note ab_def = A_def B_def AB_def
immler@56188
  2260
  have "A 0 = a" "B 0 = b" "\<And>n. \<not> P (cbox (A(Suc n)) (B(Suc n))) \<and>
wenzelm@53399
  2261
    (\<forall>i\<in>Basis. A(n)\<bullet>i \<le> A(Suc n)\<bullet>i \<and> A(Suc n)\<bullet>i \<le> B(Suc n)\<bullet>i \<and> B(Suc n)\<bullet>i \<le> B(n)\<bullet>i \<and>
hoelzl@50526
  2262
    2 * (B(Suc n)\<bullet>i - A(Suc n)\<bullet>i) \<le> B(n)\<bullet>i - A(n)\<bullet>i)" (is "\<And>n. ?P n")
wenzelm@53409
  2263
  proof -
wenzelm@53409
  2264
    show "A 0 = a" "B 0 = b"
wenzelm@53409
  2265
      unfolding ab_def by auto
wenzelm@53409
  2266
    case goal3
wenzelm@53409
  2267
    note S = ab_def funpow.simps o_def id_apply
wenzelm@53409
  2268
    show ?case
wenzelm@53409
  2269
    proof (induct n)
wenzelm@53409
  2270
      case 0
wenzelm@53409
  2271
      then show ?case
wenzelm@53409
  2272
        unfolding S
wenzelm@53409
  2273
        apply (rule f[rule_format]) using assms(3)
wenzelm@53409
  2274
        apply auto
wenzelm@53409
  2275
        done
wenzelm@53409
  2276
    next
wenzelm@53409
  2277
      case (Suc n)
wenzelm@53409
  2278
      show ?case
wenzelm@53409
  2279
        unfolding S
wenzelm@53409
  2280
        apply (rule f[rule_format])
wenzelm@53409
  2281
        using Suc
wenzelm@53409
  2282
        unfolding S
wenzelm@53409
  2283
        apply auto
wenzelm@53409
  2284
        done
wenzelm@53409
  2285
    qed
wenzelm@53409
  2286
  qed
wenzelm@53409
  2287
  note AB = this(1-2) conjunctD2[OF this(3),rule_format]
wenzelm@53409
  2288
immler@56188
  2289
  have interv: "\<And>e. 0 < e \<Longrightarrow> \<exists>n. \<forall>x\<in>cbox (A n) (B n). \<forall>y\<in>cbox (A n) (B n). dist x y < e"
wenzelm@53409
  2290
  proof -
wenzelm@53409
  2291
    case goal1
wenzelm@53409
  2292
    obtain n where n: "(\<Sum>i\<in>Basis. b \<bullet> i - a \<bullet> i) / e < 2 ^ n"
wenzelm@53409
  2293
      using real_arch_pow2[of "(setsum (\<lambda>i. b\<bullet>i - a\<bullet>i) Basis) / e"] ..
wenzelm@53409
  2294
    show ?case
wenzelm@53409
  2295
      apply (rule_tac x=n in exI)
wenzelm@53409
  2296
      apply rule
wenzelm@53409
  2297
      apply rule
wenzelm@53409
  2298
    proof -
wenzelm@53409
  2299
      fix x y
immler@56188
  2300
      assume xy: "x\<in>cbox (A n) (B n)" "y\<in>cbox (A n) (B n)"
wenzelm@53409
  2301
      have "dist x y \<le> setsum (\<lambda>i. abs((x - y)\<bullet>i)) Basis"
wenzelm@53409
  2302
        unfolding dist_norm by(rule norm_le_l1)
hoelzl@50526
  2303
      also have "\<dots> \<le> setsum (\<lambda>i. B n\<bullet>i - A n\<bullet>i) Basis"
wenzelm@53409
  2304
      proof (rule setsum_mono)
wenzelm@53409
  2305
        fix i :: 'a
wenzelm@53409
  2306
        assume i: "i \<in> Basis"
wenzelm@53409
  2307
        show "\<bar>(x - y) \<bullet> i\<bar> \<le> B n \<bullet> i - A n \<bullet> i"
immler@56188
  2308
          using xy[unfolded mem_box,THEN bspec, OF i]
wenzelm@53409
  2309
          by (auto simp: inner_diff_left)
wenzelm@53409
  2310
      qed
wenzelm@53409
  2311
      also have "\<dots> \<le> setsum (\<lambda>i. b\<bullet>i - a\<bullet>i) Basis / 2^n"
wenzelm@53409
  2312
        unfolding setsum_divide_distrib
wenzelm@53409
  2313
      proof (rule setsum_mono)
wenzelm@53409
  2314
        case goal1
wenzelm@53409
  2315
        then show ?case
wenzelm@53409
  2316
        proof (induct n)
wenzelm@53409
  2317
          case 0
wenzelm@53409
  2318
          then show ?case
wenzelm@53409
  2319
            unfolding AB by auto
wenzelm@53409
  2320
        next
wenzelm@53409
  2321
          case (Suc n)
wenzelm@53409
  2322
          have "B (Suc n) \<bullet> i - A (Suc n) \<bullet> i \<le> (B n \<bullet> i - A n \<bullet> i) / 2"
hoelzl@37489
  2323
            using AB(4)[of i n] using goal1 by auto
wenzelm@53409
  2324
          also have "\<dots> \<le> (b \<bullet> i - a \<bullet> i) / 2 ^ Suc n"
wenzelm@53409
  2325
            using Suc by (auto simp add:field_simps)
wenzelm@53409
  2326
          finally show ?case .
wenzelm@53409
  2327
        qed
wenzelm@53409
  2328
      qed
wenzelm@53409
  2329
      also have "\<dots> < e"
wenzelm@53409
  2330
        using n using goal1 by (auto simp add:field_simps)
wenzelm@53409
  2331
      finally show "dist x y < e" .
wenzelm@53409
  2332
    qed
wenzelm@53409
  2333
  qed
wenzelm@53409
  2334
  {
wenzelm@53409
  2335
    fix n m :: nat
immler@56188
  2336
    assume "m \<le> n" then have "cbox (A n) (B n) \<subseteq> cbox (A m) (B m)"
hoelzl@54411
  2337
    proof (induction rule: inc_induct)
wenzelm@53409
  2338
      case (step i)
wenzelm@53409
  2339
      show ?case
immler@56188
  2340
        using AB(4) by (intro order_trans[OF step.IH] subset_box_imp) auto
wenzelm@53409
  2341
    qed simp
wenzelm@53409
  2342
  } note ABsubset = this
immler@56188
  2343
  have "\<exists>a. \<forall>n. a\<in> cbox (A n) (B n)"
immler@56188
  2344
    by (rule decreasing_closed_nest[rule_format,OF closed_cbox _ ABsubset interv])
immler@54776
  2345
      (metis nat.exhaust AB(1-3) assms(1,3))
immler@56188
  2346
  then obtain x0 where x0: "\<And>n. x0 \<in> cbox (A n) (B n)"
wenzelm@53409
  2347
    by blast
wenzelm@53409
  2348
  show thesis
wenzelm@53409
  2349
  proof (rule that[rule_format, of x0])
immler@56188
  2350
    show "x0\<in>cbox a b"
wenzelm@53409
  2351
      using x0[of 0] unfolding AB .
wenzelm@53409
  2352
    fix e :: real
wenzelm@53409
  2353
    assume "e > 0"
wenzelm@53409
  2354
    from interv[OF this] obtain n
immler@56188
  2355
      where n: "\<forall>x\<in>cbox (A n) (B n). \<forall>y\<in>cbox (A n) (B n). dist x y < e" ..
immler@56188
  2356
    show "\<exists>c d. x0 \<in> cbox c d \<and> cbox c d \<subseteq> ball x0 e \<and> cbox c d \<subseteq> cbox a b \<and> \<not> P (cbox c d)"
wenzelm@53409
  2357
      apply (rule_tac x="A n" in exI)
wenzelm@53409
  2358
      apply (rule_tac x="B n" in exI)
wenzelm@53409
  2359
      apply rule
wenzelm@53409
  2360
      apply (rule x0)
wenzelm@53409
  2361
      apply rule
wenzelm@53409
  2362
      defer
wenzelm@53409
  2363
      apply rule
wenzelm@53409
  2364
    proof -
immler@56188
  2365
      show "\<not> P (cbox (A n) (B n))"
wenzelm@53409
  2366
        apply (cases "0 < n")
wenzelm@53409
  2367
        using AB(3)[of "n - 1"] assms(3) AB(1-2)
wenzelm@53409
  2368
        apply auto
wenzelm@53409
  2369
        done
immler@56188
  2370
      show "cbox (A n) (B n) \<subseteq> ball x0 e"
wenzelm@53409
  2371
        using n using x0[of n] by auto
immler@56188
  2372
      show "cbox (A n) (B n) \<subseteq> cbox a b"
wenzelm@53409
  2373
        unfolding AB(1-2)[symmetric] by (rule ABsubset) auto
wenzelm@53409
  2374
    qed
wenzelm@53409
  2375
  qed
wenzelm@53409
  2376
qed
wenzelm@53409
  2377
himmelma@35172
  2378
himmelma@35172
  2379
subsection {* Cousin's lemma. *}
himmelma@35172
  2380
wenzelm@53409
  2381
lemma fine_division_exists:
immler@56188
  2382
  fixes a b :: "'a::euclidean_space"
wenzelm@53409
  2383
  assumes "gauge g"
immler@56188
  2384
  obtains p where "p tagged_division_of (cbox a b)" "g fine p"
immler@56188
  2385
proof -
immler@56188
  2386
  presume "\<not> (\<exists>p. p tagged_division_of (cbox a b) \<and> g fine p) \<Longrightarrow> False"
immler@56188
  2387
  then obtain p where "p tagged_division_of (cbox a b)" "g fine p"
wenzelm@53410
  2388
    by blast
wenzelm@53409
  2389
  then show thesis ..
wenzelm@53409
  2390
next
immler@56188
  2391
  assume as: "\<not> (\<exists>p. p tagged_division_of (cbox a b) \<and> g fine p)"
wenzelm@55751
  2392
  obtain x where x:
immler@56188
  2393
    "x \<in> (cbox a b)"
wenzelm@55751
  2394
    "\<And>e. 0 < e \<Longrightarrow>
wenzelm@55751
  2395
      \<exists>c d.
immler@56188
  2396
        x \<in> cbox c d \<and>
immler@56188
  2397
        cbox c d \<subseteq> ball x e \<and>
immler@56188
  2398
        cbox c d \<subseteq> (cbox a b) \<and>
immler@56188
  2399
        \<not> (\<exists>p. p tagged_division_of cbox c d \<and> g fine p)"
wenzelm@53410
  2400
    apply (rule interval_bisection[of "\<lambda>s. \<exists>p. p tagged_division_of s \<and> g fine p",rule_format,OF _ _ as])
wenzelm@53410
  2401
    apply (rule_tac x="{}" in exI)
wenzelm@53410
  2402
    defer
wenzelm@53410
  2403
    apply (erule conjE exE)+
wenzelm@53409
  2404
  proof -
wenzelm@53410
  2405
    show "{} tagged_division_of {} \<and> g fine {}"
wenzelm@53410
  2406
      unfolding fine_def by auto
wenzelm@53410
  2407
    fix s t p p'
wenzelm@53410
  2408
    assume "p tagged_division_of s" "g fine p" "p' tagged_division_of t" "g fine p'"
wenzelm@53410
  2409
      "interior s \<inter> interior t = {}"
wenzelm@53409
  2410
    then show "\<exists>p. p tagged_division_of s \<union> t \<and> g fine p"
wenzelm@53409
  2411
      apply -
wenzelm@53409
  2412
      apply (rule_tac x="p \<union> p'" in exI)
wenzelm@53409
  2413
      apply rule
wenzelm@53409
  2414
      apply (rule tagged_division_union)
wenzelm@53409
  2415
      prefer 4
wenzelm@53409
  2416
      apply (rule fine_union)
wenzelm@53409
  2417
      apply auto
wenzelm@53409
  2418
      done
wenzelm@55751
  2419
  qed blast
wenzelm@53410
  2420
  obtain e where e: "e > 0" "ball x e \<subseteq> g x"
wenzelm@53409
  2421
    using gaugeD[OF assms, of x] unfolding open_contains_ball by auto
wenzelm@53410
  2422
  from x(2)[OF e(1)] obtain c d where c_d:
immler@56188
  2423
    "x \<in> cbox c d"
immler@56188
  2424
    "cbox c d \<subseteq> ball x e"
immler@56188
  2425
    "cbox c d \<subseteq> cbox a b"
immler@56188
  2426
    "\<not> (\<exists>p. p tagged_division_of cbox c d \<and> g fine p)"
wenzelm@53410
  2427
    by blast
immler@56188
  2428
  have "g fine {(x, cbox c d)}"
wenzelm@53409
  2429
    unfolding fine_def using e using c_d(2) by auto
wenzelm@53410
  2430
  then show False
wenzelm@53410
  2431
    using tagged_division_of_self[OF c_d(1)] using c_d by auto
wenzelm@53409
  2432
qed
wenzelm@53409
  2433
immler@56188
  2434
lemma fine_division_exists_real:
immler@56188
  2435
  fixes a b :: real
immler@56188
  2436
  assumes "gauge g"
immler@56188
  2437
  obtains p where "p tagged_division_of {a .. b}" "g fine p"
immler@56188
  2438
  by (metis assms box_real(2) fine_division_exists)
himmelma@35172
  2439
himmelma@35172
  2440
subsection {* Basic theorems about integrals. *}
himmelma@35172
  2441
wenzelm@53409
  2442
lemma has_integral_unique:
immler@56188
  2443
  fixes f :: "'n::euclidean_space \<Rightarrow> 'a::real_normed_vector"
wenzelm@53410
  2444
  assumes "(f has_integral k1) i"
wenzelm@53410
  2445
    and "(f has_integral k2) i"
wenzelm@53409
  2446
  shows "k1 = k2"
wenzelm@53410
  2447
proof (rule ccontr)
wenzelm@53842
  2448
  let ?e = "norm (k1 - k2) / 2"
wenzelm@53410
  2449
  assume as:"k1 \<noteq> k2"
wenzelm@53410
  2450
  then have e: "?e > 0"
wenzelm@53410
  2451
    by auto
wenzelm@53410
  2452
  have lem: "\<And>f::'n \<Rightarrow> 'a.  \<And>a b k1 k2.
immler@56188
  2453
    (f has_integral k1) (cbox a b) \<Longrightarrow> (f has_integral k2) (cbox a b) \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow> False"
wenzelm@53410
  2454
  proof -
wenzelm@53410
  2455
    case goal1
wenzelm@53410
  2456
    let ?e = "norm (k1 - k2) / 2"
wenzelm@53410
  2457
    from goal1(3) have e: "?e > 0" by auto
wenzelm@55751
  2458
    obtain d1 where d1:
wenzelm@55751
  2459
        "gauge d1"
immler@56188
  2460
        "\<And>p. p tagged_division_of cbox a b \<Longrightarrow>
wenzelm@55751
  2461
          d1 fine p \<Longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - k1) < norm (k1 - k2) / 2"
wenzelm@55751
  2462
      by (rule has_integralD[OF goal1(1) e]) blast
wenzelm@55751
  2463
    obtain d2 where d2:
wenzelm@55751
  2464
        "gauge d2"
immler@56188
  2465
        "\<And>p. p tagged_division_of cbox a b \<Longrightarrow>
wenzelm@55751
  2466
          d2 fine p \<Longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - k2) < norm (k1 - k2) / 2"
wenzelm@55751
  2467
      by (rule has_integralD[OF goal1(2) e]) blast
wenzelm@55751
  2468
    obtain p where p:
immler@56188
  2469
        "p tagged_division_of cbox a b"
wenzelm@55751
  2470
        "(\<lambda>x. d1 x \<inter> d2 x) fine p"
wenzelm@55751
  2471
      by (rule fine_division_exists[OF gauge_inter[OF d1(1) d2(1)]])
wenzelm@53410
  2472
    let ?c = "(\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)"
wenzelm@53410
  2473
    have "norm (k1 - k2) \<le> norm (?c - k2) + norm (?c - k1)"
wenzelm@53410
  2474
      using norm_triangle_ineq4[of "k1 - ?c" "k2 - ?c"]
wenzelm@53410
  2475
      by (auto simp add:algebra_simps norm_minus_commute)
himmelma@35172
  2476
    also have "\<dots> < norm (k1 - k2) / 2 + norm (k1 - k2) / 2"
wenzelm@53410
  2477
      apply (rule add_strict_mono)
wenzelm@53410
  2478
      apply (rule_tac[!] d2(2) d1(2))
wenzelm@53410
  2479
      using p unfolding fine_def
wenzelm@53410
  2480
      apply auto
wenzelm@53410
  2481
      done
himmelma@35172
  2482
    finally show False by auto
wenzelm@53410
  2483
  qed
wenzelm@53410
  2484
  {
immler@56188
  2485
    presume "\<not> (\<exists>a b. i = cbox a b) \<Longrightarrow> False"
wenzelm@53410
  2486
    then show False
wenzelm@53410
  2487
      apply -
immler@56188
  2488
      apply (cases "\<exists>a b. i = cbox a b")
wenzelm@53410
  2489
      using assms
wenzelm@53410
  2490
      apply (auto simp add:has_integral intro:lem[OF _ _ as])
wenzelm@53410
  2491
      done
wenzelm@53410
  2492
  }
immler@56188
  2493
  assume as: "\<not> (\<exists>a b. i = cbox a b)"
wenzelm@55751
  2494
  obtain B1 where B1:
wenzelm@55751
  2495
      "0 < B1"
immler@56188
  2496
      "\<And>a b. ball 0 B1 \<subseteq> cbox a b \<Longrightarrow>
immler@56188
  2497
        \<exists>z. ((\<lambda>x. if x \<in> i then f x else 0) has_integral z) (cbox a b) \<and>
wenzelm@55751
  2498
          norm (z - k1) < norm (k1 - k2) / 2"
wenzelm@55751
  2499
    by (rule has_integral_altD[OF assms(1) as,OF e]) blast
wenzelm@55751
  2500
  obtain B2 where B2:
wenzelm@55751
  2501
      "0 < B2"
immler@56188
  2502
      "\<And>a b. ball 0 B2 \<subseteq> cbox a b \<Longrightarrow>
immler@56188
  2503
        \<exists>z. ((\<lambda>x. if x \<in> i then f x else 0) has_integral z) (cbox a b) \<and>
wenzelm@55751
  2504
          norm (z - k2) < norm (k1 - k2) / 2"
wenzelm@55751
  2505
    by (rule has_integral_altD[OF assms(2) as,OF e]) blast
immler@56188
  2506
  have "\<exists>a b::'n. ball 0 B1 \<union> ball 0 B2 \<subseteq> cbox a b"
immler@56188
  2507
    apply (rule bounded_subset_cbox)
wenzelm@53410
  2508
    using bounded_Un bounded_ball
wenzelm@53410
  2509
    apply auto
wenzelm@53410
  2510
    done
immler@56188
  2511
  then obtain a b :: 'n where ab: "ball 0 B1 \<subseteq> cbox a b" "ball 0 B2 \<subseteq> cbox a b"
wenzelm@53410
  2512
    by blast
wenzelm@53410
  2513
  obtain w where w:
immler@56188
  2514
    "((\<lambda>x. if x \<in> i then f x else 0) has_integral w) (cbox a b)"
wenzelm@53410
  2515
    "norm (w - k1) < norm (k1 - k2) / 2"
wenzelm@53410
  2516
    using B1(2)[OF ab(1)] by blast
wenzelm@53410
  2517
  obtain z where z:
immler@56188
  2518
    "((\<lambda>x. if x \<in> i then f x else 0) has_integral z) (cbox a b)"
wenzelm@53410
  2519
    "norm (z - k2) < norm (k1 - k2) / 2"
wenzelm@53410
  2520
    using B2(2)[OF ab(2)] by blast
wenzelm@53410
  2521
  have "z = w"
wenzelm@53410
  2522
    using lem[OF w(1) z(1)] by auto
wenzelm@53410
  2523
  then have "norm (k1 - k2) \<le> norm (z - k2) + norm (w - k1)"
wenzelm@53410