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