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