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