src/HOL/Analysis/Topology_Euclidean_Space.thy
author immler
Fri Mar 10 23:16:40 2017 +0100 (2017-03-10)
changeset 65204 d23eded35a33
parent 65038 9391ea7daa17
child 65585 a043de9ad41e
permissions -rw-r--r--
modernized construction of type bcontfun; base explicit theorems on Uniform_Limit.thy; added some lemmas
     1 (*  Author:     L C Paulson, University of Cambridge
     2     Author:     Amine Chaieb, University of Cambridge
     3     Author:     Robert Himmelmann, TU Muenchen
     4     Author:     Brian Huffman, Portland State University
     5 *)
     6 
     7 section \<open>Elementary topology in Euclidean space.\<close>
     8 
     9 theory Topology_Euclidean_Space
    10 imports
    11   "~~/src/HOL/Library/Indicator_Function"
    12   "~~/src/HOL/Library/Countable_Set"
    13   "~~/src/HOL/Library/FuncSet"
    14   Linear_Algebra
    15   Norm_Arith
    16 begin
    17 
    18 (* FIXME: move elsewhere *)
    19 
    20 lemma Times_eq_image_sum:
    21   fixes S :: "'a :: comm_monoid_add set" and T :: "'b :: comm_monoid_add set"
    22   shows "S \<times> T = {u + v |u v. u \<in> (\<lambda>x. (x, 0)) ` S \<and> v \<in> Pair 0 ` T}"
    23   by force
    24 
    25 lemma halfspace_Int_eq:
    26      "{x. a \<bullet> x \<le> b} \<inter> {x. b \<le> a \<bullet> x} = {x. a \<bullet> x = b}"
    27      "{x. b \<le> a \<bullet> x} \<inter> {x. a \<bullet> x \<le> b} = {x. a \<bullet> x = b}"
    28   by auto
    29 
    30 definition (in monoid_add) support_on :: "'b set \<Rightarrow> ('b \<Rightarrow> 'a) \<Rightarrow> 'b set"
    31   where "support_on s f = {x\<in>s. f x \<noteq> 0}"
    32 
    33 lemma in_support_on: "x \<in> support_on s f \<longleftrightarrow> x \<in> s \<and> f x \<noteq> 0"
    34   by (simp add: support_on_def)
    35 
    36 lemma support_on_simps[simp]:
    37   "support_on {} f = {}"
    38   "support_on (insert x s) f =
    39     (if f x = 0 then support_on s f else insert x (support_on s f))"
    40   "support_on (s \<union> t) f = support_on s f \<union> support_on t f"
    41   "support_on (s \<inter> t) f = support_on s f \<inter> support_on t f"
    42   "support_on (s - t) f = support_on s f - support_on t f"
    43   "support_on (f ` s) g = f ` (support_on s (g \<circ> f))"
    44   unfolding support_on_def by auto
    45 
    46 lemma support_on_cong:
    47   "(\<And>x. x \<in> s \<Longrightarrow> f x = 0 \<longleftrightarrow> g x = 0) \<Longrightarrow> support_on s f = support_on s g"
    48   by (auto simp: support_on_def)
    49 
    50 lemma support_on_if: "a \<noteq> 0 \<Longrightarrow> support_on A (\<lambda>x. if P x then a else 0) = {x\<in>A. P x}"
    51   by (auto simp: support_on_def)
    52 
    53 lemma support_on_if_subset: "support_on A (\<lambda>x. if P x then a else 0) \<subseteq> {x \<in> A. P x}"
    54   by (auto simp: support_on_def)
    55 
    56 lemma finite_support[intro]: "finite s \<Longrightarrow> finite (support_on s f)"
    57   unfolding support_on_def by auto
    58 
    59 (* TODO: is supp_sum really needed? TODO: Generalize to Finite_Set.fold *)
    60 definition (in comm_monoid_add) supp_sum :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'a"
    61   where "supp_sum f s = (\<Sum>x\<in>support_on s f. f x)"
    62 
    63 lemma supp_sum_empty[simp]: "supp_sum f {} = 0"
    64   unfolding supp_sum_def by auto
    65 
    66 lemma supp_sum_insert[simp]:
    67   "finite (support_on s f) \<Longrightarrow>
    68     supp_sum f (insert x s) = (if x \<in> s then supp_sum f s else f x + supp_sum f s)"
    69   by (simp add: supp_sum_def in_support_on insert_absorb)
    70 
    71 lemma supp_sum_divide_distrib: "supp_sum f A / (r::'a::field) = supp_sum (\<lambda>n. f n / r) A"
    72   by (cases "r = 0")
    73      (auto simp: supp_sum_def sum_divide_distrib intro!: sum.cong support_on_cong)
    74 
    75 (*END OF SUPPORT, ETC.*)
    76 
    77 lemma image_affinity_interval:
    78   fixes c :: "'a::ordered_real_vector"
    79   shows "((\<lambda>x. m *\<^sub>R x + c) ` {a..b}) = (if {a..b}={} then {}
    80             else if 0 <= m then {m *\<^sub>R a + c .. m  *\<^sub>R b + c}
    81             else {m *\<^sub>R b + c .. m *\<^sub>R a + c})"
    82   apply (case_tac "m=0", force)
    83   apply (auto simp: scaleR_left_mono)
    84   apply (rule_tac x="inverse m *\<^sub>R (x-c)" in rev_image_eqI, auto simp: pos_le_divideR_eq le_diff_eq scaleR_left_mono_neg)
    85   apply (metis diff_le_eq inverse_inverse_eq order.not_eq_order_implies_strict pos_le_divideR_eq positive_imp_inverse_positive)
    86   apply (rule_tac x="inverse m *\<^sub>R (x-c)" in rev_image_eqI, auto simp: not_le neg_le_divideR_eq diff_le_eq)
    87   using le_diff_eq scaleR_le_cancel_left_neg
    88   apply fastforce
    89   done
    90 
    91 lemma countable_PiE:
    92   "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> countable (F i)) \<Longrightarrow> countable (Pi\<^sub>E I F)"
    93   by (induct I arbitrary: F rule: finite_induct) (auto simp: PiE_insert_eq)
    94 
    95 lemma open_sums:
    96   fixes T :: "('b::real_normed_vector) set"
    97   assumes "open S \<or> open T"
    98   shows "open (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"
    99   using assms
   100 proof
   101   assume S: "open S"
   102   show ?thesis
   103   proof (clarsimp simp: open_dist)
   104     fix x y
   105     assume "x \<in> S" "y \<in> T"
   106     with S obtain e where "e > 0" and e: "\<And>x'. dist x' x < e \<Longrightarrow> x' \<in> S"
   107       by (auto simp: open_dist)
   108     then have "\<And>z. dist z (x + y) < e \<Longrightarrow> \<exists>x\<in>S. \<exists>y\<in>T. z = x + y"
   109       by (metis \<open>y \<in> T\<close> diff_add_cancel dist_add_cancel2)
   110     then show "\<exists>e>0. \<forall>z. dist z (x + y) < e \<longrightarrow> (\<exists>x\<in>S. \<exists>y\<in>T. z = x + y)"
   111       using \<open>0 < e\<close> \<open>x \<in> S\<close> by blast
   112   qed
   113 next
   114   assume T: "open T"
   115   show ?thesis
   116   proof (clarsimp simp: open_dist)
   117     fix x y
   118     assume "x \<in> S" "y \<in> T"
   119     with T obtain e where "e > 0" and e: "\<And>x'. dist x' y < e \<Longrightarrow> x' \<in> T"
   120       by (auto simp: open_dist)
   121     then have "\<And>z. dist z (x + y) < e \<Longrightarrow> \<exists>x\<in>S. \<exists>y\<in>T. z = x + y"
   122       by (metis \<open>x \<in> S\<close> add_diff_cancel_left' add_diff_eq diff_diff_add dist_norm)
   123     then show "\<exists>e>0. \<forall>z. dist z (x + y) < e \<longrightarrow> (\<exists>x\<in>S. \<exists>y\<in>T. z = x + y)"
   124       using \<open>0 < e\<close> \<open>y \<in> T\<close> by blast
   125   qed
   126 qed
   127 
   128 
   129 subsection \<open>Topological Basis\<close>
   130 
   131 context topological_space
   132 begin
   133 
   134 definition "topological_basis B \<longleftrightarrow>
   135   (\<forall>b\<in>B. open b) \<and> (\<forall>x. open x \<longrightarrow> (\<exists>B'. B' \<subseteq> B \<and> \<Union>B' = x))"
   136 
   137 lemma topological_basis:
   138   "topological_basis B \<longleftrightarrow> (\<forall>x. open x \<longleftrightarrow> (\<exists>B'. B' \<subseteq> B \<and> \<Union>B' = x))"
   139   unfolding topological_basis_def
   140   apply safe
   141      apply fastforce
   142     apply fastforce
   143    apply (erule_tac x="x" in allE)
   144    apply simp
   145    apply (rule_tac x="{x}" in exI)
   146   apply auto
   147   done
   148 
   149 lemma topological_basis_iff:
   150   assumes "\<And>B'. B' \<in> B \<Longrightarrow> open B'"
   151   shows "topological_basis B \<longleftrightarrow> (\<forall>O'. open O' \<longrightarrow> (\<forall>x\<in>O'. \<exists>B'\<in>B. x \<in> B' \<and> B' \<subseteq> O'))"
   152     (is "_ \<longleftrightarrow> ?rhs")
   153 proof safe
   154   fix O' and x::'a
   155   assume H: "topological_basis B" "open O'" "x \<in> O'"
   156   then have "(\<exists>B'\<subseteq>B. \<Union>B' = O')" by (simp add: topological_basis_def)
   157   then obtain B' where "B' \<subseteq> B" "O' = \<Union>B'" by auto
   158   then show "\<exists>B'\<in>B. x \<in> B' \<and> B' \<subseteq> O'" using H by auto
   159 next
   160   assume H: ?rhs
   161   show "topological_basis B"
   162     using assms unfolding topological_basis_def
   163   proof safe
   164     fix O' :: "'a set"
   165     assume "open O'"
   166     with H obtain f where "\<forall>x\<in>O'. f x \<in> B \<and> x \<in> f x \<and> f x \<subseteq> O'"
   167       by (force intro: bchoice simp: Bex_def)
   168     then show "\<exists>B'\<subseteq>B. \<Union>B' = O'"
   169       by (auto intro: exI[where x="{f x |x. x \<in> O'}"])
   170   qed
   171 qed
   172 
   173 lemma topological_basisI:
   174   assumes "\<And>B'. B' \<in> B \<Longrightarrow> open B'"
   175     and "\<And>O' x. open O' \<Longrightarrow> x \<in> O' \<Longrightarrow> \<exists>B'\<in>B. x \<in> B' \<and> B' \<subseteq> O'"
   176   shows "topological_basis B"
   177   using assms by (subst topological_basis_iff) auto
   178 
   179 lemma topological_basisE:
   180   fixes O'
   181   assumes "topological_basis B"
   182     and "open O'"
   183     and "x \<in> O'"
   184   obtains B' where "B' \<in> B" "x \<in> B'" "B' \<subseteq> O'"
   185 proof atomize_elim
   186   from assms have "\<And>B'. B'\<in>B \<Longrightarrow> open B'"
   187     by (simp add: topological_basis_def)
   188   with topological_basis_iff assms
   189   show  "\<exists>B'. B' \<in> B \<and> x \<in> B' \<and> B' \<subseteq> O'"
   190     using assms by (simp add: Bex_def)
   191 qed
   192 
   193 lemma topological_basis_open:
   194   assumes "topological_basis B"
   195     and "X \<in> B"
   196   shows "open X"
   197   using assms by (simp add: topological_basis_def)
   198 
   199 lemma topological_basis_imp_subbasis:
   200   assumes B: "topological_basis B"
   201   shows "open = generate_topology B"
   202 proof (intro ext iffI)
   203   fix S :: "'a set"
   204   assume "open S"
   205   with B obtain B' where "B' \<subseteq> B" "S = \<Union>B'"
   206     unfolding topological_basis_def by blast
   207   then show "generate_topology B S"
   208     by (auto intro: generate_topology.intros dest: topological_basis_open)
   209 next
   210   fix S :: "'a set"
   211   assume "generate_topology B S"
   212   then show "open S"
   213     by induct (auto dest: topological_basis_open[OF B])
   214 qed
   215 
   216 lemma basis_dense:
   217   fixes B :: "'a set set"
   218     and f :: "'a set \<Rightarrow> 'a"
   219   assumes "topological_basis B"
   220     and choosefrom_basis: "\<And>B'. B' \<noteq> {} \<Longrightarrow> f B' \<in> B'"
   221   shows "\<forall>X. open X \<longrightarrow> X \<noteq> {} \<longrightarrow> (\<exists>B' \<in> B. f B' \<in> X)"
   222 proof (intro allI impI)
   223   fix X :: "'a set"
   224   assume "open X" and "X \<noteq> {}"
   225   from topological_basisE[OF \<open>topological_basis B\<close> \<open>open X\<close> choosefrom_basis[OF \<open>X \<noteq> {}\<close>]]
   226   obtain B' where "B' \<in> B" "f X \<in> B'" "B' \<subseteq> X" .
   227   then show "\<exists>B'\<in>B. f B' \<in> X"
   228     by (auto intro!: choosefrom_basis)
   229 qed
   230 
   231 end
   232 
   233 lemma topological_basis_prod:
   234   assumes A: "topological_basis A"
   235     and B: "topological_basis B"
   236   shows "topological_basis ((\<lambda>(a, b). a \<times> b) ` (A \<times> B))"
   237   unfolding topological_basis_def
   238 proof (safe, simp_all del: ex_simps add: subset_image_iff ex_simps(1)[symmetric])
   239   fix S :: "('a \<times> 'b) set"
   240   assume "open S"
   241   then show "\<exists>X\<subseteq>A \<times> B. (\<Union>(a,b)\<in>X. a \<times> b) = S"
   242   proof (safe intro!: exI[of _ "{x\<in>A \<times> B. fst x \<times> snd x \<subseteq> S}"])
   243     fix x y
   244     assume "(x, y) \<in> S"
   245     from open_prod_elim[OF \<open>open S\<close> this]
   246     obtain a b where a: "open a""x \<in> a" and b: "open b" "y \<in> b" and "a \<times> b \<subseteq> S"
   247       by (metis mem_Sigma_iff)
   248     moreover
   249     from A a obtain A0 where "A0 \<in> A" "x \<in> A0" "A0 \<subseteq> a"
   250       by (rule topological_basisE)
   251     moreover
   252     from B b obtain B0 where "B0 \<in> B" "y \<in> B0" "B0 \<subseteq> b"
   253       by (rule topological_basisE)
   254     ultimately show "(x, y) \<in> (\<Union>(a, b)\<in>{X \<in> A \<times> B. fst X \<times> snd X \<subseteq> S}. a \<times> b)"
   255       by (intro UN_I[of "(A0, B0)"]) auto
   256   qed auto
   257 qed (metis A B topological_basis_open open_Times)
   258 
   259 
   260 subsection \<open>Countable Basis\<close>
   261 
   262 locale countable_basis =
   263   fixes B :: "'a::topological_space set set"
   264   assumes is_basis: "topological_basis B"
   265     and countable_basis: "countable B"
   266 begin
   267 
   268 lemma open_countable_basis_ex:
   269   assumes "open X"
   270   shows "\<exists>B' \<subseteq> B. X = \<Union>B'"
   271   using assms countable_basis is_basis
   272   unfolding topological_basis_def by blast
   273 
   274 lemma open_countable_basisE:
   275   assumes "open X"
   276   obtains B' where "B' \<subseteq> B" "X = \<Union>B'"
   277   using assms open_countable_basis_ex
   278   by (atomize_elim) simp
   279 
   280 lemma countable_dense_exists:
   281   "\<exists>D::'a set. countable D \<and> (\<forall>X. open X \<longrightarrow> X \<noteq> {} \<longrightarrow> (\<exists>d \<in> D. d \<in> X))"
   282 proof -
   283   let ?f = "(\<lambda>B'. SOME x. x \<in> B')"
   284   have "countable (?f ` B)" using countable_basis by simp
   285   with basis_dense[OF is_basis, of ?f] show ?thesis
   286     by (intro exI[where x="?f ` B"]) (metis (mono_tags) all_not_in_conv imageI someI)
   287 qed
   288 
   289 lemma countable_dense_setE:
   290   obtains D :: "'a set"
   291   where "countable D" "\<And>X. open X \<Longrightarrow> X \<noteq> {} \<Longrightarrow> \<exists>d \<in> D. d \<in> X"
   292   using countable_dense_exists by blast
   293 
   294 end
   295 
   296 lemma (in first_countable_topology) first_countable_basisE:
   297   obtains A where "countable A" "\<And>a. a \<in> A \<Longrightarrow> x \<in> a" "\<And>a. a \<in> A \<Longrightarrow> open a"
   298     "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> (\<exists>a\<in>A. a \<subseteq> S)"
   299   using first_countable_basis[of x]
   300   apply atomize_elim
   301   apply (elim exE)
   302   apply (rule_tac x="range A" in exI)
   303   apply auto
   304   done
   305 
   306 lemma (in first_countable_topology) first_countable_basis_Int_stableE:
   307   obtains A where "countable A" "\<And>a. a \<in> A \<Longrightarrow> x \<in> a" "\<And>a. a \<in> A \<Longrightarrow> open a"
   308     "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> (\<exists>a\<in>A. a \<subseteq> S)"
   309     "\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<inter> b \<in> A"
   310 proof atomize_elim
   311   obtain A' where A':
   312     "countable A'"
   313     "\<And>a. a \<in> A' \<Longrightarrow> x \<in> a"
   314     "\<And>a. a \<in> A' \<Longrightarrow> open a"
   315     "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>a\<in>A'. a \<subseteq> S"
   316     by (rule first_countable_basisE) blast
   317   define A where [abs_def]:
   318     "A = (\<lambda>N. \<Inter>((\<lambda>n. from_nat_into A' n) ` N)) ` (Collect finite::nat set set)"
   319   then show "\<exists>A. countable A \<and> (\<forall>a. a \<in> A \<longrightarrow> x \<in> a) \<and> (\<forall>a. a \<in> A \<longrightarrow> open a) \<and>
   320         (\<forall>S. open S \<longrightarrow> x \<in> S \<longrightarrow> (\<exists>a\<in>A. a \<subseteq> S)) \<and> (\<forall>a b. a \<in> A \<longrightarrow> b \<in> A \<longrightarrow> a \<inter> b \<in> A)"
   321   proof (safe intro!: exI[where x=A])
   322     show "countable A"
   323       unfolding A_def by (intro countable_image countable_Collect_finite)
   324     fix a
   325     assume "a \<in> A"
   326     then show "x \<in> a" "open a"
   327       using A'(4)[OF open_UNIV] by (auto simp: A_def intro: A' from_nat_into)
   328   next
   329     let ?int = "\<lambda>N. \<Inter>(from_nat_into A' ` N)"
   330     fix a b
   331     assume "a \<in> A" "b \<in> A"
   332     then obtain N M where "a = ?int N" "b = ?int M" "finite (N \<union> M)"
   333       by (auto simp: A_def)
   334     then show "a \<inter> b \<in> A"
   335       by (auto simp: A_def intro!: image_eqI[where x="N \<union> M"])
   336   next
   337     fix S
   338     assume "open S" "x \<in> S"
   339     then obtain a where a: "a\<in>A'" "a \<subseteq> S" using A' by blast
   340     then show "\<exists>a\<in>A. a \<subseteq> S" using a A'
   341       by (intro bexI[where x=a]) (auto simp: A_def intro: image_eqI[where x="{to_nat_on A' a}"])
   342   qed
   343 qed
   344 
   345 lemma (in topological_space) first_countableI:
   346   assumes "countable A"
   347     and 1: "\<And>a. a \<in> A \<Longrightarrow> x \<in> a" "\<And>a. a \<in> A \<Longrightarrow> open a"
   348     and 2: "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>a\<in>A. a \<subseteq> S"
   349   shows "\<exists>A::nat \<Rightarrow> 'a set. (\<forall>i. x \<in> A i \<and> open (A i)) \<and> (\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>i. A i \<subseteq> S))"
   350 proof (safe intro!: exI[of _ "from_nat_into A"])
   351   fix i
   352   have "A \<noteq> {}" using 2[of UNIV] by auto
   353   show "x \<in> from_nat_into A i" "open (from_nat_into A i)"
   354     using range_from_nat_into_subset[OF \<open>A \<noteq> {}\<close>] 1 by auto
   355 next
   356   fix S
   357   assume "open S" "x\<in>S" from 2[OF this]
   358   show "\<exists>i. from_nat_into A i \<subseteq> S"
   359     using subset_range_from_nat_into[OF \<open>countable A\<close>] by auto
   360 qed
   361 
   362 instance prod :: (first_countable_topology, first_countable_topology) first_countable_topology
   363 proof
   364   fix x :: "'a \<times> 'b"
   365   obtain A where A:
   366       "countable A"
   367       "\<And>a. a \<in> A \<Longrightarrow> fst x \<in> a"
   368       "\<And>a. a \<in> A \<Longrightarrow> open a"
   369       "\<And>S. open S \<Longrightarrow> fst x \<in> S \<Longrightarrow> \<exists>a\<in>A. a \<subseteq> S"
   370     by (rule first_countable_basisE[of "fst x"]) blast
   371   obtain B where B:
   372       "countable B"
   373       "\<And>a. a \<in> B \<Longrightarrow> snd x \<in> a"
   374       "\<And>a. a \<in> B \<Longrightarrow> open a"
   375       "\<And>S. open S \<Longrightarrow> snd x \<in> S \<Longrightarrow> \<exists>a\<in>B. a \<subseteq> S"
   376     by (rule first_countable_basisE[of "snd x"]) blast
   377   show "\<exists>A::nat \<Rightarrow> ('a \<times> 'b) set.
   378     (\<forall>i. x \<in> A i \<and> open (A i)) \<and> (\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>i. A i \<subseteq> S))"
   379   proof (rule first_countableI[of "(\<lambda>(a, b). a \<times> b) ` (A \<times> B)"], safe)
   380     fix a b
   381     assume x: "a \<in> A" "b \<in> B"
   382     with A(2, 3)[of a] B(2, 3)[of b] show "x \<in> a \<times> b" and "open (a \<times> b)"
   383       unfolding mem_Times_iff
   384       by (auto intro: open_Times)
   385   next
   386     fix S
   387     assume "open S" "x \<in> S"
   388     then obtain a' b' where a'b': "open a'" "open b'" "x \<in> a' \<times> b'" "a' \<times> b' \<subseteq> S"
   389       by (rule open_prod_elim)
   390     moreover
   391     from a'b' A(4)[of a'] B(4)[of b']
   392     obtain a b where "a \<in> A" "a \<subseteq> a'" "b \<in> B" "b \<subseteq> b'"
   393       by auto
   394     ultimately
   395     show "\<exists>a\<in>(\<lambda>(a, b). a \<times> b) ` (A \<times> B). a \<subseteq> S"
   396       by (auto intro!: bexI[of _ "a \<times> b"] bexI[of _ a] bexI[of _ b])
   397   qed (simp add: A B)
   398 qed
   399 
   400 class second_countable_topology = topological_space +
   401   assumes ex_countable_subbasis:
   402     "\<exists>B::'a::topological_space set set. countable B \<and> open = generate_topology B"
   403 begin
   404 
   405 lemma ex_countable_basis: "\<exists>B::'a set set. countable B \<and> topological_basis B"
   406 proof -
   407   from ex_countable_subbasis obtain B where B: "countable B" "open = generate_topology B"
   408     by blast
   409   let ?B = "Inter ` {b. finite b \<and> b \<subseteq> B }"
   410 
   411   show ?thesis
   412   proof (intro exI conjI)
   413     show "countable ?B"
   414       by (intro countable_image countable_Collect_finite_subset B)
   415     {
   416       fix S
   417       assume "open S"
   418       then have "\<exists>B'\<subseteq>{b. finite b \<and> b \<subseteq> B}. (\<Union>b\<in>B'. \<Inter>b) = S"
   419         unfolding B
   420       proof induct
   421         case UNIV
   422         show ?case by (intro exI[of _ "{{}}"]) simp
   423       next
   424         case (Int a b)
   425         then obtain x y where x: "a = UNION x Inter" "\<And>i. i \<in> x \<Longrightarrow> finite i \<and> i \<subseteq> B"
   426           and y: "b = UNION y Inter" "\<And>i. i \<in> y \<Longrightarrow> finite i \<and> i \<subseteq> B"
   427           by blast
   428         show ?case
   429           unfolding x y Int_UN_distrib2
   430           by (intro exI[of _ "{i \<union> j| i j.  i \<in> x \<and> j \<in> y}"]) (auto dest: x(2) y(2))
   431       next
   432         case (UN K)
   433         then have "\<forall>k\<in>K. \<exists>B'\<subseteq>{b. finite b \<and> b \<subseteq> B}. UNION B' Inter = k" by auto
   434         then obtain k where
   435             "\<forall>ka\<in>K. k ka \<subseteq> {b. finite b \<and> b \<subseteq> B} \<and> UNION (k ka) Inter = ka"
   436           unfolding bchoice_iff ..
   437         then show "\<exists>B'\<subseteq>{b. finite b \<and> b \<subseteq> B}. UNION B' Inter = \<Union>K"
   438           by (intro exI[of _ "UNION K k"]) auto
   439       next
   440         case (Basis S)
   441         then show ?case
   442           by (intro exI[of _ "{{S}}"]) auto
   443       qed
   444       then have "(\<exists>B'\<subseteq>Inter ` {b. finite b \<and> b \<subseteq> B}. \<Union>B' = S)"
   445         unfolding subset_image_iff by blast }
   446     then show "topological_basis ?B"
   447       unfolding topological_space_class.topological_basis_def
   448       by (safe intro!: topological_space_class.open_Inter)
   449          (simp_all add: B generate_topology.Basis subset_eq)
   450   qed
   451 qed
   452 
   453 end
   454 
   455 sublocale second_countable_topology <
   456   countable_basis "SOME B. countable B \<and> topological_basis B"
   457   using someI_ex[OF ex_countable_basis]
   458   by unfold_locales safe
   459 
   460 instance prod :: (second_countable_topology, second_countable_topology) second_countable_topology
   461 proof
   462   obtain A :: "'a set set" where "countable A" "topological_basis A"
   463     using ex_countable_basis by auto
   464   moreover
   465   obtain B :: "'b set set" where "countable B" "topological_basis B"
   466     using ex_countable_basis by auto
   467   ultimately show "\<exists>B::('a \<times> 'b) set set. countable B \<and> open = generate_topology B"
   468     by (auto intro!: exI[of _ "(\<lambda>(a, b). a \<times> b) ` (A \<times> B)"] topological_basis_prod
   469       topological_basis_imp_subbasis)
   470 qed
   471 
   472 instance second_countable_topology \<subseteq> first_countable_topology
   473 proof
   474   fix x :: 'a
   475   define B :: "'a set set" where "B = (SOME B. countable B \<and> topological_basis B)"
   476   then have B: "countable B" "topological_basis B"
   477     using countable_basis is_basis
   478     by (auto simp: countable_basis is_basis)
   479   then show "\<exists>A::nat \<Rightarrow> 'a set.
   480     (\<forall>i. x \<in> A i \<and> open (A i)) \<and> (\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>i. A i \<subseteq> S))"
   481     by (intro first_countableI[of "{b\<in>B. x \<in> b}"])
   482        (fastforce simp: topological_space_class.topological_basis_def)+
   483 qed
   484 
   485 instance nat :: second_countable_topology
   486 proof
   487   show "\<exists>B::nat set set. countable B \<and> open = generate_topology B"
   488     by (intro exI[of _ "range lessThan \<union> range greaterThan"]) (auto simp: open_nat_def)
   489 qed
   490 
   491 lemma countable_separating_set_linorder1:
   492   shows "\<exists>B::('a::{linorder_topology, second_countable_topology} set). countable B \<and> (\<forall>x y. x < y \<longrightarrow> (\<exists>b \<in> B. x < b \<and> b \<le> y))"
   493 proof -
   494   obtain A::"'a set set" where "countable A" "topological_basis A" using ex_countable_basis by auto
   495   define B1 where "B1 = {(LEAST x. x \<in> U)| U. U \<in> A}"
   496   then have "countable B1" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
   497   define B2 where "B2 = {(SOME x. x \<in> U)| U. U \<in> A}"
   498   then have "countable B2" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
   499   have "\<exists>b \<in> B1 \<union> B2. x < b \<and> b \<le> y" if "x < y" for x y
   500   proof (cases)
   501     assume "\<exists>z. x < z \<and> z < y"
   502     then obtain z where z: "x < z \<and> z < y" by auto
   503     define U where "U = {x<..<y}"
   504     then have "open U" by simp
   505     moreover have "z \<in> U" using z U_def by simp
   506     ultimately obtain V where "V \<in> A" "z \<in> V" "V \<subseteq> U" using topological_basisE[OF \<open>topological_basis A\<close>] by auto
   507     define w where "w = (SOME x. x \<in> V)"
   508     then have "w \<in> V" using \<open>z \<in> V\<close> by (metis someI2)
   509     then have "x < w \<and> w \<le> y" using \<open>w \<in> V\<close> \<open>V \<subseteq> U\<close> U_def by fastforce
   510     moreover have "w \<in> B1 \<union> B2" using w_def B2_def \<open>V \<in> A\<close> by auto
   511     ultimately show ?thesis by auto
   512   next
   513     assume "\<not>(\<exists>z. x < z \<and> z < y)"
   514     then have *: "\<And>z. z > x \<Longrightarrow> z \<ge> y" by auto
   515     define U where "U = {x<..}"
   516     then have "open U" by simp
   517     moreover have "y \<in> U" using \<open>x < y\<close> U_def by simp
   518     ultimately obtain "V" where "V \<in> A" "y \<in> V" "V \<subseteq> U" using topological_basisE[OF \<open>topological_basis A\<close>] by auto
   519     have "U = {y..}" unfolding U_def using * \<open>x < y\<close> by auto
   520     then have "V \<subseteq> {y..}" using \<open>V \<subseteq> U\<close> by simp
   521     then have "(LEAST w. w \<in> V) = y" using \<open>y \<in> V\<close> by (meson Least_equality atLeast_iff subsetCE)
   522     then have "y \<in> B1 \<union> B2" using \<open>V \<in> A\<close> B1_def by auto
   523     moreover have "x < y \<and> y \<le> y" using \<open>x < y\<close> by simp
   524     ultimately show ?thesis by auto
   525   qed
   526   moreover have "countable (B1 \<union> B2)" using \<open>countable B1\<close> \<open>countable B2\<close> by simp
   527   ultimately show ?thesis by auto
   528 qed
   529 
   530 lemma countable_separating_set_linorder2:
   531   shows "\<exists>B::('a::{linorder_topology, second_countable_topology} set). countable B \<and> (\<forall>x y. x < y \<longrightarrow> (\<exists>b \<in> B. x \<le> b \<and> b < y))"
   532 proof -
   533   obtain A::"'a set set" where "countable A" "topological_basis A" using ex_countable_basis by auto
   534   define B1 where "B1 = {(GREATEST x. x \<in> U) | U. U \<in> A}"
   535   then have "countable B1" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
   536   define B2 where "B2 = {(SOME x. x \<in> U)| U. U \<in> A}"
   537   then have "countable B2" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
   538   have "\<exists>b \<in> B1 \<union> B2. x \<le> b \<and> b < y" if "x < y" for x y
   539   proof (cases)
   540     assume "\<exists>z. x < z \<and> z < y"
   541     then obtain z where z: "x < z \<and> z < y" by auto
   542     define U where "U = {x<..<y}"
   543     then have "open U" by simp
   544     moreover have "z \<in> U" using z U_def by simp
   545     ultimately obtain "V" where "V \<in> A" "z \<in> V" "V \<subseteq> U" using topological_basisE[OF \<open>topological_basis A\<close>] by auto
   546     define w where "w = (SOME x. x \<in> V)"
   547     then have "w \<in> V" using \<open>z \<in> V\<close> by (metis someI2)
   548     then have "x \<le> w \<and> w < y" using \<open>w \<in> V\<close> \<open>V \<subseteq> U\<close> U_def by fastforce
   549     moreover have "w \<in> B1 \<union> B2" using w_def B2_def \<open>V \<in> A\<close> by auto
   550     ultimately show ?thesis by auto
   551   next
   552     assume "\<not>(\<exists>z. x < z \<and> z < y)"
   553     then have *: "\<And>z. z < y \<Longrightarrow> z \<le> x" using leI by blast
   554     define U where "U = {..<y}"
   555     then have "open U" by simp
   556     moreover have "x \<in> U" using \<open>x < y\<close> U_def by simp
   557     ultimately obtain "V" where "V \<in> A" "x \<in> V" "V \<subseteq> U" using topological_basisE[OF \<open>topological_basis A\<close>] by auto
   558     have "U = {..x}" unfolding U_def using * \<open>x < y\<close> by auto
   559     then have "V \<subseteq> {..x}" using \<open>V \<subseteq> U\<close> by simp
   560     then have "(GREATEST x. x \<in> V) = x" using \<open>x \<in> V\<close> by (meson Greatest_equality atMost_iff subsetCE)
   561     then have "x \<in> B1 \<union> B2" using \<open>V \<in> A\<close> B1_def by auto
   562     moreover have "x \<le> x \<and> x < y" using \<open>x < y\<close> by simp
   563     ultimately show ?thesis by auto
   564   qed
   565   moreover have "countable (B1 \<union> B2)" using \<open>countable B1\<close> \<open>countable B2\<close> by simp
   566   ultimately show ?thesis by auto
   567 qed
   568 
   569 lemma countable_separating_set_dense_linorder:
   570   shows "\<exists>B::('a::{linorder_topology, dense_linorder, second_countable_topology} set). countable B \<and> (\<forall>x y. x < y \<longrightarrow> (\<exists>b \<in> B. x < b \<and> b < y))"
   571 proof -
   572   obtain B::"'a set" where B: "countable B" "\<And>x y. x < y \<Longrightarrow> (\<exists>b \<in> B. x < b \<and> b \<le> y)"
   573     using countable_separating_set_linorder1 by auto
   574   have "\<exists>b \<in> B. x < b \<and> b < y" if "x < y" for x y
   575   proof -
   576     obtain z where "x < z" "z < y" using \<open>x < y\<close> dense by blast
   577     then obtain b where "b \<in> B" "x < b \<and> b \<le> z" using B(2) by auto
   578     then have "x < b \<and> b < y" using \<open>z < y\<close> by auto
   579     then show ?thesis using \<open>b \<in> B\<close> by auto
   580   qed
   581   then show ?thesis using B(1) by auto
   582 qed
   583 
   584 subsection \<open>Polish spaces\<close>
   585 
   586 text \<open>Textbooks define Polish spaces as completely metrizable.
   587   We assume the topology to be complete for a given metric.\<close>
   588 
   589 class polish_space = complete_space + second_countable_topology
   590 
   591 subsection \<open>General notion of a topology as a value\<close>
   592 
   593 definition "istopology L \<longleftrightarrow>
   594   L {} \<and> (\<forall>S T. L S \<longrightarrow> L T \<longrightarrow> L (S \<inter> T)) \<and> (\<forall>K. Ball K L \<longrightarrow> L (\<Union>K))"
   595 
   596 typedef 'a topology = "{L::('a set) \<Rightarrow> bool. istopology L}"
   597   morphisms "openin" "topology"
   598   unfolding istopology_def by blast
   599 
   600 lemma istopology_openin[intro]: "istopology(openin U)"
   601   using openin[of U] by blast
   602 
   603 lemma topology_inverse': "istopology U \<Longrightarrow> openin (topology U) = U"
   604   using topology_inverse[unfolded mem_Collect_eq] .
   605 
   606 lemma topology_inverse_iff: "istopology U \<longleftrightarrow> openin (topology U) = U"
   607   using topology_inverse[of U] istopology_openin[of "topology U"] by auto
   608 
   609 lemma topology_eq: "T1 = T2 \<longleftrightarrow> (\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S)"
   610 proof
   611   assume "T1 = T2"
   612   then show "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S" by simp
   613 next
   614   assume H: "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S"
   615   then have "openin T1 = openin T2" by (simp add: fun_eq_iff)
   616   then have "topology (openin T1) = topology (openin T2)" by simp
   617   then show "T1 = T2" unfolding openin_inverse .
   618 qed
   619 
   620 text\<open>Infer the "universe" from union of all sets in the topology.\<close>
   621 
   622 definition "topspace T = \<Union>{S. openin T S}"
   623 
   624 subsubsection \<open>Main properties of open sets\<close>
   625 
   626 lemma openin_clauses:
   627   fixes U :: "'a topology"
   628   shows
   629     "openin U {}"
   630     "\<And>S T. openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S\<inter>T)"
   631     "\<And>K. (\<forall>S \<in> K. openin U S) \<Longrightarrow> openin U (\<Union>K)"
   632   using openin[of U] unfolding istopology_def mem_Collect_eq by fast+
   633 
   634 lemma openin_subset[intro]: "openin U S \<Longrightarrow> S \<subseteq> topspace U"
   635   unfolding topspace_def by blast
   636 
   637 lemma openin_empty[simp]: "openin U {}"
   638   by (rule openin_clauses)
   639 
   640 lemma openin_Int[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<inter> T)"
   641   by (rule openin_clauses)
   642 
   643 lemma openin_Union[intro]: "(\<And>S. S \<in> K \<Longrightarrow> openin U S) \<Longrightarrow> openin U (\<Union>K)"
   644   using openin_clauses by blast
   645 
   646 lemma openin_Un[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<union> T)"
   647   using openin_Union[of "{S,T}" U] by auto
   648 
   649 lemma openin_topspace[intro, simp]: "openin U (topspace U)"
   650   by (force simp add: openin_Union topspace_def)
   651 
   652 lemma openin_subopen: "openin U S \<longleftrightarrow> (\<forall>x \<in> S. \<exists>T. openin U T \<and> x \<in> T \<and> T \<subseteq> S)"
   653   (is "?lhs \<longleftrightarrow> ?rhs")
   654 proof
   655   assume ?lhs
   656   then show ?rhs by auto
   657 next
   658   assume H: ?rhs
   659   let ?t = "\<Union>{T. openin U T \<and> T \<subseteq> S}"
   660   have "openin U ?t" by (force simp add: openin_Union)
   661   also have "?t = S" using H by auto
   662   finally show "openin U S" .
   663 qed
   664 
   665 lemma openin_INT [intro]:
   666   assumes "finite I"
   667           "\<And>i. i \<in> I \<Longrightarrow> openin T (U i)"
   668   shows "openin T ((\<Inter>i \<in> I. U i) \<inter> topspace T)"
   669 using assms by (induct, auto simp add: inf_sup_aci(2) openin_Int)
   670 
   671 lemma openin_INT2 [intro]:
   672   assumes "finite I" "I \<noteq> {}"
   673           "\<And>i. i \<in> I \<Longrightarrow> openin T (U i)"
   674   shows "openin T (\<Inter>i \<in> I. U i)"
   675 proof -
   676   have "(\<Inter>i \<in> I. U i) \<subseteq> topspace T"
   677     using \<open>I \<noteq> {}\<close> openin_subset[OF assms(3)] by auto
   678   then show ?thesis
   679     using openin_INT[of _ _ U, OF assms(1) assms(3)] by (simp add: inf.absorb2 inf_commute)
   680 qed
   681 
   682 
   683 subsubsection \<open>Closed sets\<close>
   684 
   685 definition "closedin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> openin U (topspace U - S)"
   686 
   687 lemma closedin_subset: "closedin U S \<Longrightarrow> S \<subseteq> topspace U"
   688   by (metis closedin_def)
   689 
   690 lemma closedin_empty[simp]: "closedin U {}"
   691   by (simp add: closedin_def)
   692 
   693 lemma closedin_topspace[intro, simp]: "closedin U (topspace U)"
   694   by (simp add: closedin_def)
   695 
   696 lemma closedin_Un[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<union> T)"
   697   by (auto simp add: Diff_Un closedin_def)
   698 
   699 lemma Diff_Inter[intro]: "A - \<Inter>S = \<Union>{A - s|s. s\<in>S}"
   700   by auto
   701 
   702 lemma closedin_Union:
   703   assumes "finite S" "\<And>T. T \<in> S \<Longrightarrow> closedin U T"
   704     shows "closedin U (\<Union>S)"
   705   using assms by induction auto
   706 
   707 lemma closedin_Inter[intro]:
   708   assumes Ke: "K \<noteq> {}"
   709     and Kc: "\<And>S. S \<in>K \<Longrightarrow> closedin U S"
   710   shows "closedin U (\<Inter>K)"
   711   using Ke Kc unfolding closedin_def Diff_Inter by auto
   712 
   713 lemma closedin_INT[intro]:
   714   assumes "A \<noteq> {}" "\<And>x. x \<in> A \<Longrightarrow> closedin U (B x)"
   715   shows "closedin U (\<Inter>x\<in>A. B x)"
   716   apply (rule closedin_Inter)
   717   using assms
   718   apply auto
   719   done
   720 
   721 lemma closedin_Int[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<inter> T)"
   722   using closedin_Inter[of "{S,T}" U] by auto
   723 
   724 lemma openin_closedin_eq: "openin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> closedin U (topspace U - S)"
   725   apply (auto simp add: closedin_def Diff_Diff_Int inf_absorb2)
   726   apply (metis openin_subset subset_eq)
   727   done
   728 
   729 lemma openin_closedin: "S \<subseteq> topspace U \<Longrightarrow> (openin U S \<longleftrightarrow> closedin U (topspace U - S))"
   730   by (simp add: openin_closedin_eq)
   731 
   732 lemma openin_diff[intro]:
   733   assumes oS: "openin U S"
   734     and cT: "closedin U T"
   735   shows "openin U (S - T)"
   736 proof -
   737   have "S - T = S \<inter> (topspace U - T)" using openin_subset[of U S]  oS cT
   738     by (auto simp add: topspace_def openin_subset)
   739   then show ?thesis using oS cT
   740     by (auto simp add: closedin_def)
   741 qed
   742 
   743 lemma closedin_diff[intro]:
   744   assumes oS: "closedin U S"
   745     and cT: "openin U T"
   746   shows "closedin U (S - T)"
   747 proof -
   748   have "S - T = S \<inter> (topspace U - T)"
   749     using closedin_subset[of U S] oS cT by (auto simp add: topspace_def)
   750   then show ?thesis
   751     using oS cT by (auto simp add: openin_closedin_eq)
   752 qed
   753 
   754 
   755 subsubsection \<open>Subspace topology\<close>
   756 
   757 definition "subtopology U V = topology (\<lambda>T. \<exists>S. T = S \<inter> V \<and> openin U S)"
   758 
   759 lemma istopology_subtopology: "istopology (\<lambda>T. \<exists>S. T = S \<inter> V \<and> openin U S)"
   760   (is "istopology ?L")
   761 proof -
   762   have "?L {}" by blast
   763   {
   764     fix A B
   765     assume A: "?L A" and B: "?L B"
   766     from A B obtain Sa and Sb where Sa: "openin U Sa" "A = Sa \<inter> V" and Sb: "openin U Sb" "B = Sb \<inter> V"
   767       by blast
   768     have "A \<inter> B = (Sa \<inter> Sb) \<inter> V" "openin U (Sa \<inter> Sb)"
   769       using Sa Sb by blast+
   770     then have "?L (A \<inter> B)" by blast
   771   }
   772   moreover
   773   {
   774     fix K
   775     assume K: "K \<subseteq> Collect ?L"
   776     have th0: "Collect ?L = (\<lambda>S. S \<inter> V) ` Collect (openin U)"
   777       by blast
   778     from K[unfolded th0 subset_image_iff]
   779     obtain Sk where Sk: "Sk \<subseteq> Collect (openin U)" "K = (\<lambda>S. S \<inter> V) ` Sk"
   780       by blast
   781     have "\<Union>K = (\<Union>Sk) \<inter> V"
   782       using Sk by auto
   783     moreover have "openin U (\<Union>Sk)"
   784       using Sk by (auto simp add: subset_eq)
   785     ultimately have "?L (\<Union>K)" by blast
   786   }
   787   ultimately show ?thesis
   788     unfolding subset_eq mem_Collect_eq istopology_def by auto
   789 qed
   790 
   791 lemma openin_subtopology: "openin (subtopology U V) S \<longleftrightarrow> (\<exists>T. openin U T \<and> S = T \<inter> V)"
   792   unfolding subtopology_def topology_inverse'[OF istopology_subtopology]
   793   by auto
   794 
   795 lemma topspace_subtopology: "topspace (subtopology U V) = topspace U \<inter> V"
   796   by (auto simp add: topspace_def openin_subtopology)
   797 
   798 lemma closedin_subtopology: "closedin (subtopology U V) S \<longleftrightarrow> (\<exists>T. closedin U T \<and> S = T \<inter> V)"
   799   unfolding closedin_def topspace_subtopology
   800   by (auto simp add: openin_subtopology)
   801 
   802 lemma openin_subtopology_refl: "openin (subtopology U V) V \<longleftrightarrow> V \<subseteq> topspace U"
   803   unfolding openin_subtopology
   804   by auto (metis IntD1 in_mono openin_subset)
   805 
   806 lemma subtopology_superset:
   807   assumes UV: "topspace U \<subseteq> V"
   808   shows "subtopology U V = U"
   809 proof -
   810   {
   811     fix S
   812     {
   813       fix T
   814       assume T: "openin U T" "S = T \<inter> V"
   815       from T openin_subset[OF T(1)] UV have eq: "S = T"
   816         by blast
   817       have "openin U S"
   818         unfolding eq using T by blast
   819     }
   820     moreover
   821     {
   822       assume S: "openin U S"
   823       then have "\<exists>T. openin U T \<and> S = T \<inter> V"
   824         using openin_subset[OF S] UV by auto
   825     }
   826     ultimately have "(\<exists>T. openin U T \<and> S = T \<inter> V) \<longleftrightarrow> openin U S"
   827       by blast
   828   }
   829   then show ?thesis
   830     unfolding topology_eq openin_subtopology by blast
   831 qed
   832 
   833 lemma subtopology_topspace[simp]: "subtopology U (topspace U) = U"
   834   by (simp add: subtopology_superset)
   835 
   836 lemma subtopology_UNIV[simp]: "subtopology U UNIV = U"
   837   by (simp add: subtopology_superset)
   838 
   839 lemma openin_subtopology_empty:
   840    "openin (subtopology U {}) S \<longleftrightarrow> S = {}"
   841 by (metis Int_empty_right openin_empty openin_subtopology)
   842 
   843 lemma closedin_subtopology_empty:
   844    "closedin (subtopology U {}) S \<longleftrightarrow> S = {}"
   845 by (metis Int_empty_right closedin_empty closedin_subtopology)
   846 
   847 lemma closedin_subtopology_refl [simp]:
   848    "closedin (subtopology U X) X \<longleftrightarrow> X \<subseteq> topspace U"
   849 by (metis closedin_def closedin_topspace inf.absorb_iff2 le_inf_iff topspace_subtopology)
   850 
   851 lemma openin_imp_subset:
   852    "openin (subtopology U S) T \<Longrightarrow> T \<subseteq> S"
   853 by (metis Int_iff openin_subtopology subsetI)
   854 
   855 lemma closedin_imp_subset:
   856    "closedin (subtopology U S) T \<Longrightarrow> T \<subseteq> S"
   857 by (simp add: closedin_def topspace_subtopology)
   858 
   859 lemma openin_subtopology_Un:
   860     "openin (subtopology U T) S \<and> openin (subtopology U u) S
   861      \<Longrightarrow> openin (subtopology U (T \<union> u)) S"
   862 by (simp add: openin_subtopology) blast
   863 
   864 
   865 subsubsection \<open>The standard Euclidean topology\<close>
   866 
   867 definition euclidean :: "'a::topological_space topology"
   868   where "euclidean = topology open"
   869 
   870 lemma open_openin: "open S \<longleftrightarrow> openin euclidean S"
   871   unfolding euclidean_def
   872   apply (rule cong[where x=S and y=S])
   873   apply (rule topology_inverse[symmetric])
   874   apply (auto simp add: istopology_def)
   875   done
   876 
   877 declare open_openin [symmetric, simp]
   878 
   879 lemma topspace_euclidean [simp]: "topspace euclidean = UNIV"
   880   by (force simp add: topspace_def)
   881 
   882 lemma topspace_euclidean_subtopology[simp]: "topspace (subtopology euclidean S) = S"
   883   by (simp add: topspace_subtopology)
   884 
   885 lemma closed_closedin: "closed S \<longleftrightarrow> closedin euclidean S"
   886   by (simp add: closed_def closedin_def Compl_eq_Diff_UNIV)
   887 
   888 lemma open_subopen: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S)"
   889   using openI by auto
   890 
   891 lemma openin_subtopology_self [simp]: "openin (subtopology euclidean S) S"
   892   by (metis openin_topspace topspace_euclidean_subtopology)
   893 
   894 text \<open>Basic "localization" results are handy for connectedness.\<close>
   895 
   896 lemma openin_open: "openin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. open T \<and> (S = U \<inter> T))"
   897   by (auto simp add: openin_subtopology)
   898 
   899 lemma openin_Int_open:
   900    "\<lbrakk>openin (subtopology euclidean U) S; open T\<rbrakk>
   901         \<Longrightarrow> openin (subtopology euclidean U) (S \<inter> T)"
   902 by (metis open_Int Int_assoc openin_open)
   903 
   904 lemma openin_open_Int[intro]: "open S \<Longrightarrow> openin (subtopology euclidean U) (U \<inter> S)"
   905   by (auto simp add: openin_open)
   906 
   907 lemma open_openin_trans[trans]:
   908   "open S \<Longrightarrow> open T \<Longrightarrow> T \<subseteq> S \<Longrightarrow> openin (subtopology euclidean S) T"
   909   by (metis Int_absorb1  openin_open_Int)
   910 
   911 lemma open_subset: "S \<subseteq> T \<Longrightarrow> open S \<Longrightarrow> openin (subtopology euclidean T) S"
   912   by (auto simp add: openin_open)
   913 
   914 lemma closedin_closed: "closedin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. closed T \<and> S = U \<inter> T)"
   915   by (simp add: closedin_subtopology closed_closedin Int_ac)
   916 
   917 lemma closedin_closed_Int: "closed S \<Longrightarrow> closedin (subtopology euclidean U) (U \<inter> S)"
   918   by (metis closedin_closed)
   919 
   920 lemma closed_subset: "S \<subseteq> T \<Longrightarrow> closed S \<Longrightarrow> closedin (subtopology euclidean T) S"
   921   by (auto simp add: closedin_closed)
   922 
   923 lemma closedin_closed_subset:
   924  "\<lbrakk>closedin (subtopology euclidean U) V; T \<subseteq> U; S = V \<inter> T\<rbrakk>
   925              \<Longrightarrow> closedin (subtopology euclidean T) S"
   926   by (metis (no_types, lifting) Int_assoc Int_commute closedin_closed inf.orderE)
   927 
   928 lemma finite_imp_closedin:
   929   fixes S :: "'a::t1_space set"
   930   shows "\<lbrakk>finite S; S \<subseteq> T\<rbrakk> \<Longrightarrow> closedin (subtopology euclidean T) S"
   931     by (simp add: finite_imp_closed closed_subset)
   932 
   933 lemma closedin_singleton [simp]:
   934   fixes a :: "'a::t1_space"
   935   shows "closedin (subtopology euclidean U) {a} \<longleftrightarrow> a \<in> U"
   936 using closedin_subset  by (force intro: closed_subset)
   937 
   938 lemma openin_euclidean_subtopology_iff:
   939   fixes S U :: "'a::metric_space set"
   940   shows "openin (subtopology euclidean U) S \<longleftrightarrow>
   941     S \<subseteq> U \<and> (\<forall>x\<in>S. \<exists>e>0. \<forall>x'\<in>U. dist x' x < e \<longrightarrow> x'\<in> S)"
   942   (is "?lhs \<longleftrightarrow> ?rhs")
   943 proof
   944   assume ?lhs
   945   then show ?rhs
   946     unfolding openin_open open_dist by blast
   947 next
   948   define T where "T = {x. \<exists>a\<in>S. \<exists>d>0. (\<forall>y\<in>U. dist y a < d \<longrightarrow> y \<in> S) \<and> dist x a < d}"
   949   have 1: "\<forall>x\<in>T. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> T"
   950     unfolding T_def
   951     apply clarsimp
   952     apply (rule_tac x="d - dist x a" in exI)
   953     apply (clarsimp simp add: less_diff_eq)
   954     by (metis dist_commute dist_triangle_lt)
   955   assume ?rhs then have 2: "S = U \<inter> T"
   956     unfolding T_def
   957     by auto (metis dist_self)
   958   from 1 2 show ?lhs
   959     unfolding openin_open open_dist by fast
   960 qed
   961 
   962 lemma connected_openin:
   963       "connected s \<longleftrightarrow>
   964        ~(\<exists>e1 e2. openin (subtopology euclidean s) e1 \<and>
   965                  openin (subtopology euclidean s) e2 \<and>
   966                  s \<subseteq> e1 \<union> e2 \<and> e1 \<inter> e2 = {} \<and> e1 \<noteq> {} \<and> e2 \<noteq> {})"
   967   apply (simp add: connected_def openin_open, safe)
   968   apply (simp_all, blast+)  (* SLOW *)
   969   done
   970 
   971 lemma connected_openin_eq:
   972       "connected s \<longleftrightarrow>
   973        ~(\<exists>e1 e2. openin (subtopology euclidean s) e1 \<and>
   974                  openin (subtopology euclidean s) e2 \<and>
   975                  e1 \<union> e2 = s \<and> e1 \<inter> e2 = {} \<and>
   976                  e1 \<noteq> {} \<and> e2 \<noteq> {})"
   977   apply (simp add: connected_openin, safe)
   978   apply blast
   979   by (metis Int_lower1 Un_subset_iff openin_open subset_antisym)
   980 
   981 lemma connected_closedin:
   982       "connected s \<longleftrightarrow>
   983        ~(\<exists>e1 e2.
   984              closedin (subtopology euclidean s) e1 \<and>
   985              closedin (subtopology euclidean s) e2 \<and>
   986              s \<subseteq> e1 \<union> e2 \<and> e1 \<inter> e2 = {} \<and>
   987              e1 \<noteq> {} \<and> e2 \<noteq> {})"
   988 proof -
   989   { fix A B x x'
   990     assume s_sub: "s \<subseteq> A \<union> B"
   991        and disj: "A \<inter> B \<inter> s = {}"
   992        and x: "x \<in> s" "x \<in> B" and x': "x' \<in> s" "x' \<in> A"
   993        and cl: "closed A" "closed B"
   994     assume "\<forall>e1. (\<forall>T. closed T \<longrightarrow> e1 \<noteq> s \<inter> T) \<or> (\<forall>e2. e1 \<inter> e2 = {} \<longrightarrow> s \<subseteq> e1 \<union> e2 \<longrightarrow> (\<forall>T. closed T \<longrightarrow> e2 \<noteq> s \<inter> T) \<or> e1 = {} \<or> e2 = {})"
   995     then have "\<And>C D. s \<inter> C = {} \<or> s \<inter> D = {} \<or> s \<inter> (C \<inter> (s \<inter> D)) \<noteq> {} \<or> \<not> s \<subseteq> s \<inter> (C \<union> D) \<or> \<not> closed C \<or> \<not> closed D"
   996       by (metis (no_types) Int_Un_distrib Int_assoc)
   997     moreover have "s \<inter> (A \<inter> B) = {}" "s \<inter> (A \<union> B) = s" "s \<inter> B \<noteq> {}"
   998       using disj s_sub x by blast+
   999     ultimately have "s \<inter> A = {}"
  1000       using cl by (metis inf.left_commute inf_bot_right order_refl)
  1001     then have False
  1002       using x' by blast
  1003   } note * = this
  1004   show ?thesis
  1005     apply (simp add: connected_closed closedin_closed)
  1006     apply (safe; simp)
  1007     apply blast
  1008     apply (blast intro: *)
  1009     done
  1010 qed
  1011 
  1012 lemma connected_closedin_eq:
  1013       "connected s \<longleftrightarrow>
  1014            ~(\<exists>e1 e2.
  1015                  closedin (subtopology euclidean s) e1 \<and>
  1016                  closedin (subtopology euclidean s) e2 \<and>
  1017                  e1 \<union> e2 = s \<and> e1 \<inter> e2 = {} \<and>
  1018                  e1 \<noteq> {} \<and> e2 \<noteq> {})"
  1019   apply (simp add: connected_closedin, safe)
  1020   apply blast
  1021   by (metis Int_lower1 Un_subset_iff closedin_closed subset_antisym)
  1022 
  1023 text \<open>These "transitivity" results are handy too\<close>
  1024 
  1025 lemma openin_trans[trans]:
  1026   "openin (subtopology euclidean T) S \<Longrightarrow> openin (subtopology euclidean U) T \<Longrightarrow>
  1027     openin (subtopology euclidean U) S"
  1028   unfolding open_openin openin_open by blast
  1029 
  1030 lemma openin_open_trans: "openin (subtopology euclidean T) S \<Longrightarrow> open T \<Longrightarrow> open S"
  1031   by (auto simp add: openin_open intro: openin_trans)
  1032 
  1033 lemma closedin_trans[trans]:
  1034   "closedin (subtopology euclidean T) S \<Longrightarrow> closedin (subtopology euclidean U) T \<Longrightarrow>
  1035     closedin (subtopology euclidean U) S"
  1036   by (auto simp add: closedin_closed closed_closedin closed_Inter Int_assoc)
  1037 
  1038 lemma closedin_closed_trans: "closedin (subtopology euclidean T) S \<Longrightarrow> closed T \<Longrightarrow> closed S"
  1039   by (auto simp add: closedin_closed intro: closedin_trans)
  1040 
  1041 lemma openin_subtopology_Int_subset:
  1042    "\<lbrakk>openin (subtopology euclidean u) (u \<inter> S); v \<subseteq> u\<rbrakk> \<Longrightarrow> openin (subtopology euclidean v) (v \<inter> S)"
  1043   by (auto simp: openin_subtopology)
  1044 
  1045 lemma openin_open_eq: "open s \<Longrightarrow> (openin (subtopology euclidean s) t \<longleftrightarrow> open t \<and> t \<subseteq> s)"
  1046   using open_subset openin_open_trans openin_subset by fastforce
  1047 
  1048 
  1049 subsection \<open>Open and closed balls\<close>
  1050 
  1051 definition ball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set"
  1052   where "ball x e = {y. dist x y < e}"
  1053 
  1054 definition cball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set"
  1055   where "cball x e = {y. dist x y \<le> e}"
  1056 
  1057 definition sphere :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set"
  1058   where "sphere x e = {y. dist x y = e}"
  1059 
  1060 lemma mem_ball [simp]: "y \<in> ball x e \<longleftrightarrow> dist x y < e"
  1061   by (simp add: ball_def)
  1062 
  1063 lemma mem_cball [simp]: "y \<in> cball x e \<longleftrightarrow> dist x y \<le> e"
  1064   by (simp add: cball_def)
  1065 
  1066 lemma mem_sphere [simp]: "y \<in> sphere x e \<longleftrightarrow> dist x y = e"
  1067   by (simp add: sphere_def)
  1068 
  1069 lemma ball_trivial [simp]: "ball x 0 = {}"
  1070   by (simp add: ball_def)
  1071 
  1072 lemma cball_trivial [simp]: "cball x 0 = {x}"
  1073   by (simp add: cball_def)
  1074 
  1075 lemma sphere_trivial [simp]: "sphere x 0 = {x}"
  1076   by (simp add: sphere_def)
  1077 
  1078 lemma mem_ball_0 [simp]: "x \<in> ball 0 e \<longleftrightarrow> norm x < e"
  1079   for x :: "'a::real_normed_vector"
  1080   by (simp add: dist_norm)
  1081 
  1082 lemma mem_cball_0 [simp]: "x \<in> cball 0 e \<longleftrightarrow> norm x \<le> e"
  1083   for x :: "'a::real_normed_vector"
  1084   by (simp add: dist_norm)
  1085 
  1086 lemma disjoint_ballI: "dist x y \<ge> r+s \<Longrightarrow> ball x r \<inter> ball y s = {}"
  1087   using dist_triangle_less_add not_le by fastforce
  1088 
  1089 lemma disjoint_cballI: "dist x y > r + s \<Longrightarrow> cball x r \<inter> cball y s = {}"
  1090   by (metis add_mono disjoint_iff_not_equal dist_triangle2 dual_order.trans leD mem_cball)
  1091 
  1092 lemma mem_sphere_0 [simp]: "x \<in> sphere 0 e \<longleftrightarrow> norm x = e"
  1093   for x :: "'a::real_normed_vector"
  1094   by (simp add: dist_norm)
  1095 
  1096 lemma sphere_empty [simp]: "r < 0 \<Longrightarrow> sphere a r = {}"
  1097   for a :: "'a::metric_space"
  1098   by auto
  1099 
  1100 lemma centre_in_ball [simp]: "x \<in> ball x e \<longleftrightarrow> 0 < e"
  1101   by simp
  1102 
  1103 lemma centre_in_cball [simp]: "x \<in> cball x e \<longleftrightarrow> 0 \<le> e"
  1104   by simp
  1105 
  1106 lemma ball_subset_cball [simp, intro]: "ball x e \<subseteq> cball x e"
  1107   by (simp add: subset_eq)
  1108 
  1109 lemma sphere_cball [simp,intro]: "sphere z r \<subseteq> cball z r"
  1110   by force
  1111 
  1112 lemma cball_diff_sphere: "cball a r - sphere a r = ball a r"
  1113   by auto
  1114 
  1115 lemma subset_ball[intro]: "d \<le> e \<Longrightarrow> ball x d \<subseteq> ball x e"
  1116   by (simp add: subset_eq)
  1117 
  1118 lemma subset_cball[intro]: "d \<le> e \<Longrightarrow> cball x d \<subseteq> cball x e"
  1119   by (simp add: subset_eq)
  1120 
  1121 lemma ball_max_Un: "ball a (max r s) = ball a r \<union> ball a s"
  1122   by (simp add: set_eq_iff) arith
  1123 
  1124 lemma ball_min_Int: "ball a (min r s) = ball a r \<inter> ball a s"
  1125   by (simp add: set_eq_iff)
  1126 
  1127 lemma cball_max_Un: "cball a (max r s) = cball a r \<union> cball a s"
  1128   by (simp add: set_eq_iff) arith
  1129 
  1130 lemma cball_min_Int: "cball a (min r s) = cball a r \<inter> cball a s"
  1131   by (simp add: set_eq_iff)
  1132 
  1133 lemma cball_diff_eq_sphere: "cball a r - ball a r =  sphere a r"
  1134   by (auto simp: cball_def ball_def dist_commute)
  1135 
  1136 lemma image_add_ball [simp]:
  1137   fixes a :: "'a::real_normed_vector"
  1138   shows "op + b ` ball a r = ball (a+b) r"
  1139 apply (intro equalityI subsetI)
  1140 apply (force simp: dist_norm)
  1141 apply (rule_tac x="x-b" in image_eqI)
  1142 apply (auto simp: dist_norm algebra_simps)
  1143 done
  1144 
  1145 lemma image_add_cball [simp]:
  1146   fixes a :: "'a::real_normed_vector"
  1147   shows "op + b ` cball a r = cball (a+b) r"
  1148 apply (intro equalityI subsetI)
  1149 apply (force simp: dist_norm)
  1150 apply (rule_tac x="x-b" in image_eqI)
  1151 apply (auto simp: dist_norm algebra_simps)
  1152 done
  1153 
  1154 lemma open_ball [intro, simp]: "open (ball x e)"
  1155 proof -
  1156   have "open (dist x -` {..<e})"
  1157     by (intro open_vimage open_lessThan continuous_intros)
  1158   also have "dist x -` {..<e} = ball x e"
  1159     by auto
  1160   finally show ?thesis .
  1161 qed
  1162 
  1163 lemma open_contains_ball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. ball x e \<subseteq> S)"
  1164   by (simp add: open_dist subset_eq mem_ball Ball_def dist_commute)
  1165 
  1166 lemma openI [intro?]: "(\<And>x. x\<in>S \<Longrightarrow> \<exists>e>0. ball x e \<subseteq> S) \<Longrightarrow> open S"
  1167   by (auto simp: open_contains_ball)
  1168 
  1169 lemma openE[elim?]:
  1170   assumes "open S" "x\<in>S"
  1171   obtains e where "e>0" "ball x e \<subseteq> S"
  1172   using assms unfolding open_contains_ball by auto
  1173 
  1174 lemma open_contains_ball_eq: "open S \<Longrightarrow> x\<in>S \<longleftrightarrow> (\<exists>e>0. ball x e \<subseteq> S)"
  1175   by (metis open_contains_ball subset_eq centre_in_ball)
  1176 
  1177 lemma openin_contains_ball:
  1178     "openin (subtopology euclidean t) s \<longleftrightarrow>
  1179      s \<subseteq> t \<and> (\<forall>x \<in> s. \<exists>e. 0 < e \<and> ball x e \<inter> t \<subseteq> s)"
  1180     (is "?lhs = ?rhs")
  1181 proof
  1182   assume ?lhs
  1183   then show ?rhs
  1184     apply (simp add: openin_open)
  1185     apply (metis Int_commute Int_mono inf.cobounded2 open_contains_ball order_refl subsetCE)
  1186     done
  1187 next
  1188   assume ?rhs
  1189   then show ?lhs
  1190     apply (simp add: openin_euclidean_subtopology_iff)
  1191     by (metis (no_types) Int_iff dist_commute inf.absorb_iff2 mem_ball)
  1192 qed
  1193 
  1194 lemma openin_contains_cball:
  1195    "openin (subtopology euclidean t) s \<longleftrightarrow>
  1196         s \<subseteq> t \<and>
  1197         (\<forall>x \<in> s. \<exists>e. 0 < e \<and> cball x e \<inter> t \<subseteq> s)"
  1198 apply (simp add: openin_contains_ball)
  1199 apply (rule iffI)
  1200 apply (auto dest!: bspec)
  1201 apply (rule_tac x="e/2" in exI)
  1202 apply force+
  1203 done
  1204 
  1205 lemma ball_eq_empty[simp]: "ball x e = {} \<longleftrightarrow> e \<le> 0"
  1206   unfolding mem_ball set_eq_iff
  1207   apply (simp add: not_less)
  1208   apply (metis zero_le_dist order_trans dist_self)
  1209   done
  1210 
  1211 lemma ball_empty: "e \<le> 0 \<Longrightarrow> ball x e = {}" by simp
  1212 
  1213 lemma euclidean_dist_l2:
  1214   fixes x y :: "'a :: euclidean_space"
  1215   shows "dist x y = setL2 (\<lambda>i. dist (x \<bullet> i) (y \<bullet> i)) Basis"
  1216   unfolding dist_norm norm_eq_sqrt_inner setL2_def
  1217   by (subst euclidean_inner) (simp add: power2_eq_square inner_diff_left)
  1218 
  1219 lemma eventually_nhds_ball: "d > 0 \<Longrightarrow> eventually (\<lambda>x. x \<in> ball z d) (nhds z)"
  1220   by (rule eventually_nhds_in_open) simp_all
  1221 
  1222 lemma eventually_at_ball: "d > 0 \<Longrightarrow> eventually (\<lambda>t. t \<in> ball z d \<and> t \<in> A) (at z within A)"
  1223   unfolding eventually_at by (intro exI[of _ d]) (simp_all add: dist_commute)
  1224 
  1225 lemma eventually_at_ball': "d > 0 \<Longrightarrow> eventually (\<lambda>t. t \<in> ball z d \<and> t \<noteq> z \<and> t \<in> A) (at z within A)"
  1226   unfolding eventually_at by (intro exI[of _ d]) (simp_all add: dist_commute)
  1227 
  1228 
  1229 subsection \<open>Boxes\<close>
  1230 
  1231 abbreviation One :: "'a::euclidean_space"
  1232   where "One \<equiv> \<Sum>Basis"
  1233 
  1234 lemma One_non_0: assumes "One = (0::'a::euclidean_space)" shows False
  1235 proof -
  1236   have "dependent (Basis :: 'a set)"
  1237     apply (simp add: dependent_finite)
  1238     apply (rule_tac x="\<lambda>i. 1" in exI)
  1239     using SOME_Basis apply (auto simp: assms)
  1240     done
  1241   with independent_Basis show False by force
  1242 qed
  1243 
  1244 corollary One_neq_0[iff]: "One \<noteq> 0"
  1245   by (metis One_non_0)
  1246 
  1247 corollary Zero_neq_One[iff]: "0 \<noteq> One"
  1248   by (metis One_non_0)
  1249 
  1250 definition (in euclidean_space) eucl_less (infix "<e" 50)
  1251   where "eucl_less a b \<longleftrightarrow> (\<forall>i\<in>Basis. a \<bullet> i < b \<bullet> i)"
  1252 
  1253 definition box_eucl_less: "box a b = {x. a <e x \<and> x <e b}"
  1254 definition "cbox a b = {x. \<forall>i\<in>Basis. a \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> b \<bullet> i}"
  1255 
  1256 lemma box_def: "box a b = {x. \<forall>i\<in>Basis. a \<bullet> i < x \<bullet> i \<and> x \<bullet> i < b \<bullet> i}"
  1257   and in_box_eucl_less: "x \<in> box a b \<longleftrightarrow> a <e x \<and> x <e b"
  1258   and mem_box: "x \<in> box a b \<longleftrightarrow> (\<forall>i\<in>Basis. a \<bullet> i < x \<bullet> i \<and> x \<bullet> i < b \<bullet> i)"
  1259     "x \<in> cbox a b \<longleftrightarrow> (\<forall>i\<in>Basis. a \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> b \<bullet> i)"
  1260   by (auto simp: box_eucl_less eucl_less_def cbox_def)
  1261 
  1262 lemma cbox_Pair_eq: "cbox (a, c) (b, d) = cbox a b \<times> cbox c d"
  1263   by (force simp: cbox_def Basis_prod_def)
  1264 
  1265 lemma cbox_Pair_iff [iff]: "(x, y) \<in> cbox (a, c) (b, d) \<longleftrightarrow> x \<in> cbox a b \<and> y \<in> cbox c d"
  1266   by (force simp: cbox_Pair_eq)
  1267 
  1268 lemma cbox_Pair_eq_0: "cbox (a, c) (b, d) = {} \<longleftrightarrow> cbox a b = {} \<or> cbox c d = {}"
  1269   by (force simp: cbox_Pair_eq)
  1270 
  1271 lemma swap_cbox_Pair [simp]: "prod.swap ` cbox (c, a) (d, b) = cbox (a,c) (b,d)"
  1272   by auto
  1273 
  1274 lemma mem_box_real[simp]:
  1275   "(x::real) \<in> box a b \<longleftrightarrow> a < x \<and> x < b"
  1276   "(x::real) \<in> cbox a b \<longleftrightarrow> a \<le> x \<and> x \<le> b"
  1277   by (auto simp: mem_box)
  1278 
  1279 lemma box_real[simp]:
  1280   fixes a b:: real
  1281   shows "box a b = {a <..< b}" "cbox a b = {a .. b}"
  1282   by auto
  1283 
  1284 lemma box_Int_box:
  1285   fixes a :: "'a::euclidean_space"
  1286   shows "box a b \<inter> box c d =
  1287     box (\<Sum>i\<in>Basis. max (a\<bullet>i) (c\<bullet>i) *\<^sub>R i) (\<Sum>i\<in>Basis. min (b\<bullet>i) (d\<bullet>i) *\<^sub>R i)"
  1288   unfolding set_eq_iff and Int_iff and mem_box by auto
  1289 
  1290 lemma rational_boxes:
  1291   fixes x :: "'a::euclidean_space"
  1292   assumes "e > 0"
  1293   shows "\<exists>a b. (\<forall>i\<in>Basis. a \<bullet> i \<in> \<rat> \<and> b \<bullet> i \<in> \<rat> ) \<and> x \<in> box a b \<and> box a b \<subseteq> ball x e"
  1294 proof -
  1295   define e' where "e' = e / (2 * sqrt (real (DIM ('a))))"
  1296   then have e: "e' > 0"
  1297     using assms by (auto simp: DIM_positive)
  1298   have "\<forall>i. \<exists>y. y \<in> \<rat> \<and> y < x \<bullet> i \<and> x \<bullet> i - y < e'" (is "\<forall>i. ?th i")
  1299   proof
  1300     fix i
  1301     from Rats_dense_in_real[of "x \<bullet> i - e'" "x \<bullet> i"] e
  1302     show "?th i" by auto
  1303   qed
  1304   from choice[OF this] obtain a where
  1305     a: "\<forall>xa. a xa \<in> \<rat> \<and> a xa < x \<bullet> xa \<and> x \<bullet> xa - a xa < e'" ..
  1306   have "\<forall>i. \<exists>y. y \<in> \<rat> \<and> x \<bullet> i < y \<and> y - x \<bullet> i < e'" (is "\<forall>i. ?th i")
  1307   proof
  1308     fix i
  1309     from Rats_dense_in_real[of "x \<bullet> i" "x \<bullet> i + e'"] e
  1310     show "?th i" by auto
  1311   qed
  1312   from choice[OF this] obtain b where
  1313     b: "\<forall>xa. b xa \<in> \<rat> \<and> x \<bullet> xa < b xa \<and> b xa - x \<bullet> xa < e'" ..
  1314   let ?a = "\<Sum>i\<in>Basis. a i *\<^sub>R i" and ?b = "\<Sum>i\<in>Basis. b i *\<^sub>R i"
  1315   show ?thesis
  1316   proof (rule exI[of _ ?a], rule exI[of _ ?b], safe)
  1317     fix y :: 'a
  1318     assume *: "y \<in> box ?a ?b"
  1319     have "dist x y = sqrt (\<Sum>i\<in>Basis. (dist (x \<bullet> i) (y \<bullet> i))\<^sup>2)"
  1320       unfolding setL2_def[symmetric] by (rule euclidean_dist_l2)
  1321     also have "\<dots> < sqrt (\<Sum>(i::'a)\<in>Basis. e^2 / real (DIM('a)))"
  1322     proof (rule real_sqrt_less_mono, rule sum_strict_mono)
  1323       fix i :: "'a"
  1324       assume i: "i \<in> Basis"
  1325       have "a i < y\<bullet>i \<and> y\<bullet>i < b i"
  1326         using * i by (auto simp: box_def)
  1327       moreover have "a i < x\<bullet>i" "x\<bullet>i - a i < e'"
  1328         using a by auto
  1329       moreover have "x\<bullet>i < b i" "b i - x\<bullet>i < e'"
  1330         using b by auto
  1331       ultimately have "\<bar>x\<bullet>i - y\<bullet>i\<bar> < 2 * e'"
  1332         by auto
  1333       then have "dist (x \<bullet> i) (y \<bullet> i) < e/sqrt (real (DIM('a)))"
  1334         unfolding e'_def by (auto simp: dist_real_def)
  1335       then have "(dist (x \<bullet> i) (y \<bullet> i))\<^sup>2 < (e/sqrt (real (DIM('a))))\<^sup>2"
  1336         by (rule power_strict_mono) auto
  1337       then show "(dist (x \<bullet> i) (y \<bullet> i))\<^sup>2 < e\<^sup>2 / real DIM('a)"
  1338         by (simp add: power_divide)
  1339     qed auto
  1340     also have "\<dots> = e"
  1341       using \<open>0 < e\<close> by simp
  1342     finally show "y \<in> ball x e"
  1343       by (auto simp: ball_def)
  1344   qed (insert a b, auto simp: box_def)
  1345 qed
  1346 
  1347 lemma open_UNION_box:
  1348   fixes M :: "'a::euclidean_space set"
  1349   assumes "open M"
  1350   defines "a' \<equiv> \<lambda>f :: 'a \<Rightarrow> real \<times> real. (\<Sum>(i::'a)\<in>Basis. fst (f i) *\<^sub>R i)"
  1351   defines "b' \<equiv> \<lambda>f :: 'a \<Rightarrow> real \<times> real. (\<Sum>(i::'a)\<in>Basis. snd (f i) *\<^sub>R i)"
  1352   defines "I \<equiv> {f\<in>Basis \<rightarrow>\<^sub>E \<rat> \<times> \<rat>. box (a' f) (b' f) \<subseteq> M}"
  1353   shows "M = (\<Union>f\<in>I. box (a' f) (b' f))"
  1354 proof -
  1355   have "x \<in> (\<Union>f\<in>I. box (a' f) (b' f))" if "x \<in> M" for x
  1356   proof -
  1357     obtain e where e: "e > 0" "ball x e \<subseteq> M"
  1358       using openE[OF \<open>open M\<close> \<open>x \<in> M\<close>] by auto
  1359     moreover obtain a b where ab:
  1360       "x \<in> box a b"
  1361       "\<forall>i \<in> Basis. a \<bullet> i \<in> \<rat>"
  1362       "\<forall>i\<in>Basis. b \<bullet> i \<in> \<rat>"
  1363       "box a b \<subseteq> ball x e"
  1364       using rational_boxes[OF e(1)] by metis
  1365     ultimately show ?thesis
  1366        by (intro UN_I[of "\<lambda>i\<in>Basis. (a \<bullet> i, b \<bullet> i)"])
  1367           (auto simp: euclidean_representation I_def a'_def b'_def)
  1368   qed
  1369   then show ?thesis by (auto simp: I_def)
  1370 qed
  1371 
  1372 lemma box_eq_empty:
  1373   fixes a :: "'a::euclidean_space"
  1374   shows "(box a b = {} \<longleftrightarrow> (\<exists>i\<in>Basis. b\<bullet>i \<le> a\<bullet>i))" (is ?th1)
  1375     and "(cbox a b = {} \<longleftrightarrow> (\<exists>i\<in>Basis. b\<bullet>i < a\<bullet>i))" (is ?th2)
  1376 proof -
  1377   {
  1378     fix i x
  1379     assume i: "i\<in>Basis" and as:"b\<bullet>i \<le> a\<bullet>i" and x:"x\<in>box a b"
  1380     then have "a \<bullet> i < x \<bullet> i \<and> x \<bullet> i < b \<bullet> i"
  1381       unfolding mem_box by (auto simp: box_def)
  1382     then have "a\<bullet>i < b\<bullet>i" by auto
  1383     then have False using as by auto
  1384   }
  1385   moreover
  1386   {
  1387     assume as: "\<forall>i\<in>Basis. \<not> (b\<bullet>i \<le> a\<bullet>i)"
  1388     let ?x = "(1/2) *\<^sub>R (a + b)"
  1389     {
  1390       fix i :: 'a
  1391       assume i: "i \<in> Basis"
  1392       have "a\<bullet>i < b\<bullet>i"
  1393         using as[THEN bspec[where x=i]] i by auto
  1394       then have "a\<bullet>i < ((1/2) *\<^sub>R (a+b)) \<bullet> i" "((1/2) *\<^sub>R (a+b)) \<bullet> i < b\<bullet>i"
  1395         by (auto simp: inner_add_left)
  1396     }
  1397     then have "box a b \<noteq> {}"
  1398       using mem_box(1)[of "?x" a b] by auto
  1399   }
  1400   ultimately show ?th1 by blast
  1401 
  1402   {
  1403     fix i x
  1404     assume i: "i \<in> Basis" and as:"b\<bullet>i < a\<bullet>i" and x:"x\<in>cbox a b"
  1405     then have "a \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> b \<bullet> i"
  1406       unfolding mem_box by auto
  1407     then have "a\<bullet>i \<le> b\<bullet>i" by auto
  1408     then have False using as by auto
  1409   }
  1410   moreover
  1411   {
  1412     assume as:"\<forall>i\<in>Basis. \<not> (b\<bullet>i < a\<bullet>i)"
  1413     let ?x = "(1/2) *\<^sub>R (a + b)"
  1414     {
  1415       fix i :: 'a
  1416       assume i:"i \<in> Basis"
  1417       have "a\<bullet>i \<le> b\<bullet>i"
  1418         using as[THEN bspec[where x=i]] i by auto
  1419       then have "a\<bullet>i \<le> ((1/2) *\<^sub>R (a+b)) \<bullet> i" "((1/2) *\<^sub>R (a+b)) \<bullet> i \<le> b\<bullet>i"
  1420         by (auto simp: inner_add_left)
  1421     }
  1422     then have "cbox a b \<noteq> {}"
  1423       using mem_box(2)[of "?x" a b] by auto
  1424   }
  1425   ultimately show ?th2 by blast
  1426 qed
  1427 
  1428 lemma box_ne_empty:
  1429   fixes a :: "'a::euclidean_space"
  1430   shows "cbox a b \<noteq> {} \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i)"
  1431   and "box a b \<noteq> {} \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i)"
  1432   unfolding box_eq_empty[of a b] by fastforce+
  1433 
  1434 lemma
  1435   fixes a :: "'a::euclidean_space"
  1436   shows cbox_sing: "cbox a a = {a}"
  1437     and box_sing: "box a a = {}"
  1438   unfolding set_eq_iff mem_box eq_iff [symmetric]
  1439   by (auto intro!: euclidean_eqI[where 'a='a])
  1440      (metis all_not_in_conv nonempty_Basis)
  1441 
  1442 lemma subset_box_imp:
  1443   fixes a :: "'a::euclidean_space"
  1444   shows "(\<forall>i\<in>Basis. a\<bullet>i \<le> c\<bullet>i \<and> d\<bullet>i \<le> b\<bullet>i) \<Longrightarrow> cbox c d \<subseteq> cbox a b"
  1445     and "(\<forall>i\<in>Basis. a\<bullet>i < c\<bullet>i \<and> d\<bullet>i < b\<bullet>i) \<Longrightarrow> cbox c d \<subseteq> box a b"
  1446     and "(\<forall>i\<in>Basis. a\<bullet>i \<le> c\<bullet>i \<and> d\<bullet>i \<le> b\<bullet>i) \<Longrightarrow> box c d \<subseteq> cbox a b"
  1447      and "(\<forall>i\<in>Basis. a\<bullet>i \<le> c\<bullet>i \<and> d\<bullet>i \<le> b\<bullet>i) \<Longrightarrow> box c d \<subseteq> box a b"
  1448   unfolding subset_eq[unfolded Ball_def] unfolding mem_box
  1449   by (best intro: order_trans less_le_trans le_less_trans less_imp_le)+
  1450 
  1451 lemma box_subset_cbox:
  1452   fixes a :: "'a::euclidean_space"
  1453   shows "box a b \<subseteq> cbox a b"
  1454   unfolding subset_eq [unfolded Ball_def] mem_box
  1455   by (fast intro: less_imp_le)
  1456 
  1457 lemma subset_box:
  1458   fixes a :: "'a::euclidean_space"
  1459   shows "cbox c d \<subseteq> cbox a b \<longleftrightarrow> (\<forall>i\<in>Basis. c\<bullet>i \<le> d\<bullet>i) \<longrightarrow> (\<forall>i\<in>Basis. a\<bullet>i \<le> c\<bullet>i \<and> d\<bullet>i \<le> b\<bullet>i)" (is ?th1)
  1460     and "cbox c d \<subseteq> box a b \<longleftrightarrow> (\<forall>i\<in>Basis. c\<bullet>i \<le> d\<bullet>i) \<longrightarrow> (\<forall>i\<in>Basis. a\<bullet>i < c\<bullet>i \<and> d\<bullet>i < b\<bullet>i)" (is ?th2)
  1461     and "box c d \<subseteq> cbox a b \<longleftrightarrow> (\<forall>i\<in>Basis. c\<bullet>i < d\<bullet>i) \<longrightarrow> (\<forall>i\<in>Basis. a\<bullet>i \<le> c\<bullet>i \<and> d\<bullet>i \<le> b\<bullet>i)" (is ?th3)
  1462     and "box c d \<subseteq> box a b \<longleftrightarrow> (\<forall>i\<in>Basis. c\<bullet>i < d\<bullet>i) \<longrightarrow> (\<forall>i\<in>Basis. a\<bullet>i \<le> c\<bullet>i \<and> d\<bullet>i \<le> b\<bullet>i)" (is ?th4)
  1463 proof -
  1464   show ?th1
  1465     unfolding subset_eq and Ball_def and mem_box
  1466     by (auto intro: order_trans)
  1467   show ?th2
  1468     unfolding subset_eq and Ball_def and mem_box
  1469     by (auto intro: le_less_trans less_le_trans order_trans less_imp_le)
  1470   {
  1471     assume as: "box c d \<subseteq> cbox a b" "\<forall>i\<in>Basis. c\<bullet>i < d\<bullet>i"
  1472     then have "box c d \<noteq> {}"
  1473       unfolding box_eq_empty by auto
  1474     fix i :: 'a
  1475     assume i: "i \<in> Basis"
  1476     (** TODO combine the following two parts as done in the HOL_light version. **)
  1477     {
  1478       let ?x = "(\<Sum>j\<in>Basis. (if j=i then ((min (a\<bullet>j) (d\<bullet>j))+c\<bullet>j)/2 else (c\<bullet>j+d\<bullet>j)/2) *\<^sub>R j)::'a"
  1479       assume as2: "a\<bullet>i > c\<bullet>i"
  1480       {
  1481         fix j :: 'a
  1482         assume j: "j \<in> Basis"
  1483         then have "c \<bullet> j < ?x \<bullet> j \<and> ?x \<bullet> j < d \<bullet> j"
  1484           apply (cases "j = i")
  1485           using as(2)[THEN bspec[where x=j]] i
  1486           apply (auto simp add: as2)
  1487           done
  1488       }
  1489       then have "?x\<in>box c d"
  1490         using i unfolding mem_box by auto
  1491       moreover
  1492       have "?x \<notin> cbox a b"
  1493         unfolding mem_box
  1494         apply auto
  1495         apply (rule_tac x=i in bexI)
  1496         using as(2)[THEN bspec[where x=i]] and as2 i
  1497         apply auto
  1498         done
  1499       ultimately have False using as by auto
  1500     }
  1501     then have "a\<bullet>i \<le> c\<bullet>i" by (rule ccontr) auto
  1502     moreover
  1503     {
  1504       let ?x = "(\<Sum>j\<in>Basis. (if j=i then ((max (b\<bullet>j) (c\<bullet>j))+d\<bullet>j)/2 else (c\<bullet>j+d\<bullet>j)/2) *\<^sub>R j)::'a"
  1505       assume as2: "b\<bullet>i < d\<bullet>i"
  1506       {
  1507         fix j :: 'a
  1508         assume "j\<in>Basis"
  1509         then have "d \<bullet> j > ?x \<bullet> j \<and> ?x \<bullet> j > c \<bullet> j"
  1510           apply (cases "j = i")
  1511           using as(2)[THEN bspec[where x=j]]
  1512           apply (auto simp add: as2)
  1513           done
  1514       }
  1515       then have "?x\<in>box c d"
  1516         unfolding mem_box by auto
  1517       moreover
  1518       have "?x\<notin>cbox a b"
  1519         unfolding mem_box
  1520         apply auto
  1521         apply (rule_tac x=i in bexI)
  1522         using as(2)[THEN bspec[where x=i]] and as2 using i
  1523         apply auto
  1524         done
  1525       ultimately have False using as by auto
  1526     }
  1527     then have "b\<bullet>i \<ge> d\<bullet>i" by (rule ccontr) auto
  1528     ultimately
  1529     have "a\<bullet>i \<le> c\<bullet>i \<and> d\<bullet>i \<le> b\<bullet>i" by auto
  1530   } note part1 = this
  1531   show ?th3
  1532     unfolding subset_eq and Ball_def and mem_box
  1533     apply (rule, rule, rule, rule)
  1534     apply (rule part1)
  1535     unfolding subset_eq and Ball_def and mem_box
  1536     prefer 4
  1537     apply auto
  1538     apply (erule_tac x=xa in allE, erule_tac x=xa in allE, fastforce)+
  1539     done
  1540   {
  1541     assume as: "box c d \<subseteq> box a b" "\<forall>i\<in>Basis. c\<bullet>i < d\<bullet>i"
  1542     fix i :: 'a
  1543     assume i:"i\<in>Basis"
  1544     from as(1) have "box c d \<subseteq> cbox a b"
  1545       using box_subset_cbox[of a b] by auto
  1546     then have "a\<bullet>i \<le> c\<bullet>i \<and> d\<bullet>i \<le> b\<bullet>i"
  1547       using part1 and as(2) using i by auto
  1548   } note * = this
  1549   show ?th4
  1550     unfolding subset_eq and Ball_def and mem_box
  1551     apply (rule, rule, rule, rule)
  1552     apply (rule *)
  1553     unfolding subset_eq and Ball_def and mem_box
  1554     prefer 4
  1555     apply auto
  1556     apply (erule_tac x=xa in allE, simp)+
  1557     done
  1558 qed
  1559 
  1560 lemma eq_cbox: "cbox a b = cbox c d \<longleftrightarrow> cbox a b = {} \<and> cbox c d = {} \<or> a = c \<and> b = d"
  1561       (is "?lhs = ?rhs")
  1562 proof
  1563   assume ?lhs
  1564   then have "cbox a b \<subseteq> cbox c d" "cbox c d \<subseteq> cbox a b"
  1565     by auto
  1566   then show ?rhs
  1567     by (force simp add: subset_box box_eq_empty intro: antisym euclidean_eqI)
  1568 next
  1569   assume ?rhs
  1570   then show ?lhs
  1571     by force
  1572 qed
  1573 
  1574 lemma eq_cbox_box [simp]: "cbox a b = box c d \<longleftrightarrow> cbox a b = {} \<and> box c d = {}"
  1575   (is "?lhs \<longleftrightarrow> ?rhs")
  1576 proof
  1577   assume ?lhs
  1578   then have "cbox a b \<subseteq> box c d" "box c d \<subseteq>cbox a b"
  1579     by auto
  1580   then show ?rhs
  1581     apply (simp add: subset_box)
  1582     using \<open>cbox a b = box c d\<close> box_ne_empty box_sing
  1583     apply (fastforce simp add:)
  1584     done
  1585 next
  1586   assume ?rhs
  1587   then show ?lhs
  1588     by force
  1589 qed
  1590 
  1591 lemma eq_box_cbox [simp]: "box a b = cbox c d \<longleftrightarrow> box a b = {} \<and> cbox c d = {}"
  1592   by (metis eq_cbox_box)
  1593 
  1594 lemma eq_box: "box a b = box c d \<longleftrightarrow> box a b = {} \<and> box c d = {} \<or> a = c \<and> b = d"
  1595   (is "?lhs \<longleftrightarrow> ?rhs")
  1596 proof
  1597   assume ?lhs
  1598   then have "box a b \<subseteq> box c d" "box c d \<subseteq> box a b"
  1599     by auto
  1600   then show ?rhs
  1601     apply (simp add: subset_box)
  1602     using box_ne_empty(2) \<open>box a b = box c d\<close>
  1603     apply auto
  1604      apply (meson euclidean_eqI less_eq_real_def not_less)+
  1605     done
  1606 next
  1607   assume ?rhs
  1608   then show ?lhs
  1609     by force
  1610 qed
  1611 
  1612 lemma Int_interval:
  1613   fixes a :: "'a::euclidean_space"
  1614   shows "cbox a b \<inter> cbox c d =
  1615     cbox (\<Sum>i\<in>Basis. max (a\<bullet>i) (c\<bullet>i) *\<^sub>R i) (\<Sum>i\<in>Basis. min (b\<bullet>i) (d\<bullet>i) *\<^sub>R i)"
  1616   unfolding set_eq_iff and Int_iff and mem_box
  1617   by auto
  1618 
  1619 lemma disjoint_interval:
  1620   fixes a::"'a::euclidean_space"
  1621   shows "cbox a b \<inter> cbox c d = {} \<longleftrightarrow> (\<exists>i\<in>Basis. (b\<bullet>i < a\<bullet>i \<or> d\<bullet>i < c\<bullet>i \<or> b\<bullet>i < c\<bullet>i \<or> d\<bullet>i < a\<bullet>i))" (is ?th1)
  1622     and "cbox a b \<inter> box c d = {} \<longleftrightarrow> (\<exists>i\<in>Basis. (b\<bullet>i < a\<bullet>i \<or> d\<bullet>i \<le> c\<bullet>i \<or> b\<bullet>i \<le> c\<bullet>i \<or> d\<bullet>i \<le> a\<bullet>i))" (is ?th2)
  1623     and "box a b \<inter> cbox c d = {} \<longleftrightarrow> (\<exists>i\<in>Basis. (b\<bullet>i \<le> a\<bullet>i \<or> d\<bullet>i < c\<bullet>i \<or> b\<bullet>i \<le> c\<bullet>i \<or> d\<bullet>i \<le> a\<bullet>i))" (is ?th3)
  1624     and "box a b \<inter> box c d = {} \<longleftrightarrow> (\<exists>i\<in>Basis. (b\<bullet>i \<le> a\<bullet>i \<or> d\<bullet>i \<le> c\<bullet>i \<or> b\<bullet>i \<le> c\<bullet>i \<or> d\<bullet>i \<le> a\<bullet>i))" (is ?th4)
  1625 proof -
  1626   let ?z = "(\<Sum>i\<in>Basis. (((max (a\<bullet>i) (c\<bullet>i)) + (min (b\<bullet>i) (d\<bullet>i))) / 2) *\<^sub>R i)::'a"
  1627   have **: "\<And>P Q. (\<And>i :: 'a. i \<in> Basis \<Longrightarrow> Q ?z i \<Longrightarrow> P i) \<Longrightarrow>
  1628       (\<And>i x :: 'a. i \<in> Basis \<Longrightarrow> P i \<Longrightarrow> Q x i) \<Longrightarrow> (\<forall>x. \<exists>i\<in>Basis. Q x i) \<longleftrightarrow> (\<exists>i\<in>Basis. P i)"
  1629     by blast
  1630   note * = set_eq_iff Int_iff empty_iff mem_box ball_conj_distrib[symmetric] eq_False ball_simps(10)
  1631   show ?th1 unfolding * by (intro **) auto
  1632   show ?th2 unfolding * by (intro **) auto
  1633   show ?th3 unfolding * by (intro **) auto
  1634   show ?th4 unfolding * by (intro **) auto
  1635 qed
  1636 
  1637 lemma UN_box_eq_UNIV: "(\<Union>i::nat. box (- (real i *\<^sub>R One)) (real i *\<^sub>R One)) = UNIV"
  1638 proof -
  1639   have "\<bar>x \<bullet> b\<bar> < real_of_int (\<lceil>Max ((\<lambda>b. \<bar>x \<bullet> b\<bar>)`Basis)\<rceil> + 1)"
  1640     if [simp]: "b \<in> Basis" for x b :: 'a
  1641   proof -
  1642     have "\<bar>x \<bullet> b\<bar> \<le> real_of_int \<lceil>\<bar>x \<bullet> b\<bar>\<rceil>"
  1643       by (rule le_of_int_ceiling)
  1644     also have "\<dots> \<le> real_of_int \<lceil>Max ((\<lambda>b. \<bar>x \<bullet> b\<bar>)`Basis)\<rceil>"
  1645       by (auto intro!: ceiling_mono)
  1646     also have "\<dots> < real_of_int (\<lceil>Max ((\<lambda>b. \<bar>x \<bullet> b\<bar>)`Basis)\<rceil> + 1)"
  1647       by simp
  1648     finally show ?thesis .
  1649   qed
  1650   then have "\<exists>n::nat. \<forall>b\<in>Basis. \<bar>x \<bullet> b\<bar> < real n" for x :: 'a
  1651     by (metis order.strict_trans reals_Archimedean2)
  1652   moreover have "\<And>x b::'a. \<And>n::nat.  \<bar>x \<bullet> b\<bar> < real n \<longleftrightarrow> - real n < x \<bullet> b \<and> x \<bullet> b < real n"
  1653     by auto
  1654   ultimately show ?thesis
  1655     by (auto simp: box_def inner_sum_left inner_Basis sum.If_cases)
  1656 qed
  1657 
  1658 text \<open>Intervals in general, including infinite and mixtures of open and closed.\<close>
  1659 
  1660 definition "is_interval (s::('a::euclidean_space) set) \<longleftrightarrow>
  1661   (\<forall>a\<in>s. \<forall>b\<in>s. \<forall>x. (\<forall>i\<in>Basis. ((a\<bullet>i \<le> x\<bullet>i \<and> x\<bullet>i \<le> b\<bullet>i) \<or> (b\<bullet>i \<le> x\<bullet>i \<and> x\<bullet>i \<le> a\<bullet>i))) \<longrightarrow> x \<in> s)"
  1662 
  1663 lemma is_interval_cbox: "is_interval (cbox a (b::'a::euclidean_space))" (is ?th1)
  1664   and is_interval_box: "is_interval (box a b)" (is ?th2)
  1665   unfolding is_interval_def mem_box Ball_def atLeastAtMost_iff
  1666   by (meson order_trans le_less_trans less_le_trans less_trans)+
  1667 
  1668 lemma is_interval_empty [iff]: "is_interval {}"
  1669   unfolding is_interval_def  by simp
  1670 
  1671 lemma is_interval_univ [iff]: "is_interval UNIV"
  1672   unfolding is_interval_def  by simp
  1673 
  1674 lemma mem_is_intervalI:
  1675   assumes "is_interval s"
  1676     and "a \<in> s" "b \<in> s"
  1677     and "\<And>i. i \<in> Basis \<Longrightarrow> a \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> b \<bullet> i \<or> b \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> a \<bullet> i"
  1678   shows "x \<in> s"
  1679   by (rule assms(1)[simplified is_interval_def, rule_format, OF assms(2,3,4)])
  1680 
  1681 lemma interval_subst:
  1682   fixes S::"'a::euclidean_space set"
  1683   assumes "is_interval S"
  1684     and "x \<in> S" "y j \<in> S"
  1685     and "j \<in> Basis"
  1686   shows "(\<Sum>i\<in>Basis. (if i = j then y i \<bullet> i else x \<bullet> i) *\<^sub>R i) \<in> S"
  1687   by (rule mem_is_intervalI[OF assms(1,2)]) (auto simp: assms)
  1688 
  1689 lemma mem_box_componentwiseI:
  1690   fixes S::"'a::euclidean_space set"
  1691   assumes "is_interval S"
  1692   assumes "\<And>i. i \<in> Basis \<Longrightarrow> x \<bullet> i \<in> ((\<lambda>x. x \<bullet> i) ` S)"
  1693   shows "x \<in> S"
  1694 proof -
  1695   from assms have "\<forall>i \<in> Basis. \<exists>s \<in> S. x \<bullet> i = s \<bullet> i"
  1696     by auto
  1697   with finite_Basis obtain s and bs::"'a list"
  1698     where s: "\<And>i. i \<in> Basis \<Longrightarrow> x \<bullet> i = s i \<bullet> i" "\<And>i. i \<in> Basis \<Longrightarrow> s i \<in> S"
  1699       and bs: "set bs = Basis" "distinct bs"
  1700     by (metis finite_distinct_list)
  1701   from nonempty_Basis s obtain j where j: "j \<in> Basis" "s j \<in> S"
  1702     by blast
  1703   define y where
  1704     "y = rec_list (s j) (\<lambda>j _ Y. (\<Sum>i\<in>Basis. (if i = j then s i \<bullet> i else Y \<bullet> i) *\<^sub>R i))"
  1705   have "x = (\<Sum>i\<in>Basis. (if i \<in> set bs then s i \<bullet> i else s j \<bullet> i) *\<^sub>R i)"
  1706     using bs by (auto simp add: s(1)[symmetric] euclidean_representation)
  1707   also have [symmetric]: "y bs = \<dots>"
  1708     using bs(2) bs(1)[THEN equalityD1]
  1709     by (induct bs) (auto simp: y_def euclidean_representation intro!: euclidean_eqI[where 'a='a])
  1710   also have "y bs \<in> S"
  1711     using bs(1)[THEN equalityD1]
  1712     apply (induct bs)
  1713      apply (auto simp: y_def j)
  1714     apply (rule interval_subst[OF assms(1)])
  1715       apply (auto simp: s)
  1716     done
  1717   finally show ?thesis .
  1718 qed
  1719 
  1720 lemma cbox01_nonempty [simp]: "cbox 0 One \<noteq> {}"
  1721   by (simp add: box_ne_empty inner_Basis inner_sum_left sum_nonneg)
  1722 
  1723 lemma box01_nonempty [simp]: "box 0 One \<noteq> {}"
  1724   by (simp add: box_ne_empty inner_Basis inner_sum_left) (simp add: sum.remove)
  1725 
  1726 lemma empty_as_interval: "{} = cbox One (0::'a::euclidean_space)"
  1727   using nonempty_Basis box01_nonempty box_eq_empty(1) box_ne_empty(1) by blast
  1728 
  1729 
  1730 subsection \<open>Connectedness\<close>
  1731 
  1732 lemma connected_local:
  1733  "connected S \<longleftrightarrow>
  1734   \<not> (\<exists>e1 e2.
  1735       openin (subtopology euclidean S) e1 \<and>
  1736       openin (subtopology euclidean S) e2 \<and>
  1737       S \<subseteq> e1 \<union> e2 \<and>
  1738       e1 \<inter> e2 = {} \<and>
  1739       e1 \<noteq> {} \<and>
  1740       e2 \<noteq> {})"
  1741   unfolding connected_def openin_open
  1742   by safe blast+
  1743 
  1744 lemma exists_diff:
  1745   fixes P :: "'a set \<Rightarrow> bool"
  1746   shows "(\<exists>S. P (- S)) \<longleftrightarrow> (\<exists>S. P S)"
  1747     (is "?lhs \<longleftrightarrow> ?rhs")
  1748 proof -
  1749   have ?rhs if ?lhs
  1750     using that by blast
  1751   moreover have "P (- (- S))" if "P S" for S
  1752   proof -
  1753     have "S = - (- S)" by simp
  1754     with that show ?thesis by metis
  1755   qed
  1756   ultimately show ?thesis by metis
  1757 qed
  1758 
  1759 lemma connected_clopen: "connected S \<longleftrightarrow>
  1760   (\<forall>T. openin (subtopology euclidean S) T \<and>
  1761      closedin (subtopology euclidean S) T \<longrightarrow> T = {} \<or> T = S)" (is "?lhs \<longleftrightarrow> ?rhs")
  1762 proof -
  1763   have "\<not> connected S \<longleftrightarrow>
  1764     (\<exists>e1 e2. open e1 \<and> open (- e2) \<and> S \<subseteq> e1 \<union> (- e2) \<and> e1 \<inter> (- e2) \<inter> S = {} \<and> e1 \<inter> S \<noteq> {} \<and> (- e2) \<inter> S \<noteq> {})"
  1765     unfolding connected_def openin_open closedin_closed
  1766     by (metis double_complement)
  1767   then have th0: "connected S \<longleftrightarrow>
  1768     \<not> (\<exists>e2 e1. closed e2 \<and> open e1 \<and> S \<subseteq> e1 \<union> (- e2) \<and> e1 \<inter> (- e2) \<inter> S = {} \<and> e1 \<inter> S \<noteq> {} \<and> (- e2) \<inter> S \<noteq> {})"
  1769     (is " _ \<longleftrightarrow> \<not> (\<exists>e2 e1. ?P e2 e1)")
  1770     by (simp add: closed_def) metis
  1771   have th1: "?rhs \<longleftrightarrow> \<not> (\<exists>t' t. closed t'\<and>t = S\<inter>t' \<and> t\<noteq>{} \<and> t\<noteq>S \<and> (\<exists>t'. open t' \<and> t = S \<inter> t'))"
  1772     (is "_ \<longleftrightarrow> \<not> (\<exists>t' t. ?Q t' t)")
  1773     unfolding connected_def openin_open closedin_closed by auto
  1774   have "(\<exists>e1. ?P e2 e1) \<longleftrightarrow> (\<exists>t. ?Q e2 t)" for e2
  1775   proof -
  1776     have "?P e2 e1 \<longleftrightarrow> (\<exists>t. closed e2 \<and> t = S\<inter>e2 \<and> open e1 \<and> t = S\<inter>e1 \<and> t\<noteq>{} \<and> t \<noteq> S)" for e1
  1777       by auto
  1778     then show ?thesis
  1779       by metis
  1780   qed
  1781   then have "\<forall>e2. (\<exists>e1. ?P e2 e1) \<longleftrightarrow> (\<exists>t. ?Q e2 t)"
  1782     by blast
  1783   then show ?thesis
  1784     by (simp add: th0 th1)
  1785 qed
  1786 
  1787 
  1788 subsection \<open>Limit points\<close>
  1789 
  1790 definition (in topological_space) islimpt:: "'a \<Rightarrow> 'a set \<Rightarrow> bool"  (infixr "islimpt" 60)
  1791   where "x islimpt S \<longleftrightarrow> (\<forall>T. x\<in>T \<longrightarrow> open T \<longrightarrow> (\<exists>y\<in>S. y\<in>T \<and> y\<noteq>x))"
  1792 
  1793 lemma islimptI:
  1794   assumes "\<And>T. x \<in> T \<Longrightarrow> open T \<Longrightarrow> \<exists>y\<in>S. y \<in> T \<and> y \<noteq> x"
  1795   shows "x islimpt S"
  1796   using assms unfolding islimpt_def by auto
  1797 
  1798 lemma islimptE:
  1799   assumes "x islimpt S" and "x \<in> T" and "open T"
  1800   obtains y where "y \<in> S" and "y \<in> T" and "y \<noteq> x"
  1801   using assms unfolding islimpt_def by auto
  1802 
  1803 lemma islimpt_iff_eventually: "x islimpt S \<longleftrightarrow> \<not> eventually (\<lambda>y. y \<notin> S) (at x)"
  1804   unfolding islimpt_def eventually_at_topological by auto
  1805 
  1806 lemma islimpt_subset: "x islimpt S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> x islimpt T"
  1807   unfolding islimpt_def by fast
  1808 
  1809 lemma islimpt_approachable:
  1810   fixes x :: "'a::metric_space"
  1811   shows "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e)"
  1812   unfolding islimpt_iff_eventually eventually_at by fast
  1813 
  1814 lemma islimpt_approachable_le: "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in> S. x' \<noteq> x \<and> dist x' x \<le> e)"
  1815   for x :: "'a::metric_space"
  1816   unfolding islimpt_approachable
  1817   using approachable_lt_le [where f="\<lambda>y. dist y x" and P="\<lambda>y. y \<notin> S \<or> y = x",
  1818     THEN arg_cong [where f=Not]]
  1819   by (simp add: Bex_def conj_commute conj_left_commute)
  1820 
  1821 lemma islimpt_UNIV_iff: "x islimpt UNIV \<longleftrightarrow> \<not> open {x}"
  1822   unfolding islimpt_def by (safe, fast, case_tac "T = {x}", fast, fast)
  1823 
  1824 lemma islimpt_punctured: "x islimpt S = x islimpt (S-{x})"
  1825   unfolding islimpt_def by blast
  1826 
  1827 text \<open>A perfect space has no isolated points.\<close>
  1828 
  1829 lemma islimpt_UNIV [simp, intro]: "x islimpt UNIV"
  1830   for x :: "'a::perfect_space"
  1831   unfolding islimpt_UNIV_iff by (rule not_open_singleton)
  1832 
  1833 lemma perfect_choose_dist: "0 < r \<Longrightarrow> \<exists>a. a \<noteq> x \<and> dist a x < r"
  1834   for x :: "'a::{perfect_space,metric_space}"
  1835   using islimpt_UNIV [of x] by (simp add: islimpt_approachable)
  1836 
  1837 lemma closed_limpt: "closed S \<longleftrightarrow> (\<forall>x. x islimpt S \<longrightarrow> x \<in> S)"
  1838   unfolding closed_def
  1839   apply (subst open_subopen)
  1840   apply (simp add: islimpt_def subset_eq)
  1841   apply (metis ComplE ComplI)
  1842   done
  1843 
  1844 lemma islimpt_EMPTY[simp]: "\<not> x islimpt {}"
  1845   by (auto simp add: islimpt_def)
  1846 
  1847 lemma finite_set_avoid:
  1848   fixes a :: "'a::metric_space"
  1849   assumes fS: "finite S"
  1850   shows "\<exists>d>0. \<forall>x\<in>S. x \<noteq> a \<longrightarrow> d \<le> dist a x"
  1851 proof (induct rule: finite_induct[OF fS])
  1852   case 1
  1853   then show ?case by (auto intro: zero_less_one)
  1854 next
  1855   case (2 x F)
  1856   from 2 obtain d where d: "d > 0" "\<forall>x\<in>F. x \<noteq> a \<longrightarrow> d \<le> dist a x"
  1857     by blast
  1858   show ?case
  1859   proof (cases "x = a")
  1860     case True
  1861     with d show ?thesis by auto
  1862   next
  1863     case False
  1864     let ?d = "min d (dist a x)"
  1865     from False d(1) have dp: "?d > 0"
  1866       by auto
  1867     from d have d': "\<forall>x\<in>F. x \<noteq> a \<longrightarrow> ?d \<le> dist a x"
  1868       by auto
  1869     with dp False show ?thesis
  1870       by (auto intro!: exI[where x="?d"])
  1871   qed
  1872 qed
  1873 
  1874 lemma islimpt_Un: "x islimpt (S \<union> T) \<longleftrightarrow> x islimpt S \<or> x islimpt T"
  1875   by (simp add: islimpt_iff_eventually eventually_conj_iff)
  1876 
  1877 lemma discrete_imp_closed:
  1878   fixes S :: "'a::metric_space set"
  1879   assumes e: "0 < e"
  1880     and d: "\<forall>x \<in> S. \<forall>y \<in> S. dist y x < e \<longrightarrow> y = x"
  1881   shows "closed S"
  1882 proof -
  1883   have False if C: "\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e" for x
  1884   proof -
  1885     from e have e2: "e/2 > 0" by arith
  1886     from C[rule_format, OF e2] obtain y where y: "y \<in> S" "y \<noteq> x" "dist y x < e/2"
  1887       by blast
  1888     let ?m = "min (e/2) (dist x y) "
  1889     from e2 y(2) have mp: "?m > 0"
  1890       by simp
  1891     from C[rule_format, OF mp] obtain z where z: "z \<in> S" "z \<noteq> x" "dist z x < ?m"
  1892       by blast
  1893     from z y have "dist z y < e"
  1894       by (intro dist_triangle_lt [where z=x]) simp
  1895     from d[rule_format, OF y(1) z(1) this] y z show ?thesis
  1896       by (auto simp add: dist_commute)
  1897   qed
  1898   then show ?thesis
  1899     by (metis islimpt_approachable closed_limpt [where 'a='a])
  1900 qed
  1901 
  1902 lemma closed_of_nat_image: "closed (of_nat ` A :: 'a::real_normed_algebra_1 set)"
  1903   by (rule discrete_imp_closed[of 1]) (auto simp: dist_of_nat)
  1904 
  1905 lemma closed_of_int_image: "closed (of_int ` A :: 'a::real_normed_algebra_1 set)"
  1906   by (rule discrete_imp_closed[of 1]) (auto simp: dist_of_int)
  1907 
  1908 lemma closed_Nats [simp]: "closed (\<nat> :: 'a :: real_normed_algebra_1 set)"
  1909   unfolding Nats_def by (rule closed_of_nat_image)
  1910 
  1911 lemma closed_Ints [simp]: "closed (\<int> :: 'a :: real_normed_algebra_1 set)"
  1912   unfolding Ints_def by (rule closed_of_int_image)
  1913 
  1914 
  1915 subsection \<open>Interior of a Set\<close>
  1916 
  1917 definition "interior S = \<Union>{T. open T \<and> T \<subseteq> S}"
  1918 
  1919 lemma interiorI [intro?]:
  1920   assumes "open T" and "x \<in> T" and "T \<subseteq> S"
  1921   shows "x \<in> interior S"
  1922   using assms unfolding interior_def by fast
  1923 
  1924 lemma interiorE [elim?]:
  1925   assumes "x \<in> interior S"
  1926   obtains T where "open T" and "x \<in> T" and "T \<subseteq> S"
  1927   using assms unfolding interior_def by fast
  1928 
  1929 lemma open_interior [simp, intro]: "open (interior S)"
  1930   by (simp add: interior_def open_Union)
  1931 
  1932 lemma interior_subset: "interior S \<subseteq> S"
  1933   by (auto simp add: interior_def)
  1934 
  1935 lemma interior_maximal: "T \<subseteq> S \<Longrightarrow> open T \<Longrightarrow> T \<subseteq> interior S"
  1936   by (auto simp add: interior_def)
  1937 
  1938 lemma interior_open: "open S \<Longrightarrow> interior S = S"
  1939   by (intro equalityI interior_subset interior_maximal subset_refl)
  1940 
  1941 lemma interior_eq: "interior S = S \<longleftrightarrow> open S"
  1942   by (metis open_interior interior_open)
  1943 
  1944 lemma open_subset_interior: "open S \<Longrightarrow> S \<subseteq> interior T \<longleftrightarrow> S \<subseteq> T"
  1945   by (metis interior_maximal interior_subset subset_trans)
  1946 
  1947 lemma interior_empty [simp]: "interior {} = {}"
  1948   using open_empty by (rule interior_open)
  1949 
  1950 lemma interior_UNIV [simp]: "interior UNIV = UNIV"
  1951   using open_UNIV by (rule interior_open)
  1952 
  1953 lemma interior_interior [simp]: "interior (interior S) = interior S"
  1954   using open_interior by (rule interior_open)
  1955 
  1956 lemma interior_mono: "S \<subseteq> T \<Longrightarrow> interior S \<subseteq> interior T"
  1957   by (auto simp add: interior_def)
  1958 
  1959 lemma interior_unique:
  1960   assumes "T \<subseteq> S" and "open T"
  1961   assumes "\<And>T'. T' \<subseteq> S \<Longrightarrow> open T' \<Longrightarrow> T' \<subseteq> T"
  1962   shows "interior S = T"
  1963   by (intro equalityI assms interior_subset open_interior interior_maximal)
  1964 
  1965 lemma interior_singleton [simp]: "interior {a} = {}"
  1966   for a :: "'a::perfect_space"
  1967   apply (rule interior_unique)
  1968     apply simp_all
  1969   using not_open_singleton subset_singletonD
  1970   apply fastforce
  1971   done
  1972 
  1973 lemma interior_Int [simp]: "interior (S \<inter> T) = interior S \<inter> interior T"
  1974   by (intro equalityI Int_mono Int_greatest interior_mono Int_lower1
  1975     Int_lower2 interior_maximal interior_subset open_Int open_interior)
  1976 
  1977 lemma mem_interior: "x \<in> interior S \<longleftrightarrow> (\<exists>e>0. ball x e \<subseteq> S)"
  1978   using open_contains_ball_eq [where S="interior S"]
  1979   by (simp add: open_subset_interior)
  1980 
  1981 lemma eventually_nhds_in_nhd: "x \<in> interior s \<Longrightarrow> eventually (\<lambda>y. y \<in> s) (nhds x)"
  1982   using interior_subset[of s] by (subst eventually_nhds) blast
  1983 
  1984 lemma interior_limit_point [intro]:
  1985   fixes x :: "'a::perfect_space"
  1986   assumes x: "x \<in> interior S"
  1987   shows "x islimpt S"
  1988   using x islimpt_UNIV [of x]
  1989   unfolding interior_def islimpt_def
  1990   apply (clarsimp, rename_tac T T')
  1991   apply (drule_tac x="T \<inter> T'" in spec)
  1992   apply (auto simp add: open_Int)
  1993   done
  1994 
  1995 lemma interior_closed_Un_empty_interior:
  1996   assumes cS: "closed S"
  1997     and iT: "interior T = {}"
  1998   shows "interior (S \<union> T) = interior S"
  1999 proof
  2000   show "interior S \<subseteq> interior (S \<union> T)"
  2001     by (rule interior_mono) (rule Un_upper1)
  2002   show "interior (S \<union> T) \<subseteq> interior S"
  2003   proof
  2004     fix x
  2005     assume "x \<in> interior (S \<union> T)"
  2006     then obtain R where "open R" "x \<in> R" "R \<subseteq> S \<union> T" ..
  2007     show "x \<in> interior S"
  2008     proof (rule ccontr)
  2009       assume "x \<notin> interior S"
  2010       with \<open>x \<in> R\<close> \<open>open R\<close> obtain y where "y \<in> R - S"
  2011         unfolding interior_def by fast
  2012       from \<open>open R\<close> \<open>closed S\<close> have "open (R - S)"
  2013         by (rule open_Diff)
  2014       from \<open>R \<subseteq> S \<union> T\<close> have "R - S \<subseteq> T"
  2015         by fast
  2016       from \<open>y \<in> R - S\<close> \<open>open (R - S)\<close> \<open>R - S \<subseteq> T\<close> \<open>interior T = {}\<close> show False
  2017         unfolding interior_def by fast
  2018     qed
  2019   qed
  2020 qed
  2021 
  2022 lemma interior_Times: "interior (A \<times> B) = interior A \<times> interior B"
  2023 proof (rule interior_unique)
  2024   show "interior A \<times> interior B \<subseteq> A \<times> B"
  2025     by (intro Sigma_mono interior_subset)
  2026   show "open (interior A \<times> interior B)"
  2027     by (intro open_Times open_interior)
  2028   fix T
  2029   assume "T \<subseteq> A \<times> B" and "open T"
  2030   then show "T \<subseteq> interior A \<times> interior B"
  2031   proof safe
  2032     fix x y
  2033     assume "(x, y) \<in> T"
  2034     then obtain C D where "open C" "open D" "C \<times> D \<subseteq> T" "x \<in> C" "y \<in> D"
  2035       using \<open>open T\<close> unfolding open_prod_def by fast
  2036     then have "open C" "open D" "C \<subseteq> A" "D \<subseteq> B" "x \<in> C" "y \<in> D"
  2037       using \<open>T \<subseteq> A \<times> B\<close> by auto
  2038     then show "x \<in> interior A" and "y \<in> interior B"
  2039       by (auto intro: interiorI)
  2040   qed
  2041 qed
  2042 
  2043 lemma interior_Ici:
  2044   fixes x :: "'a :: {dense_linorder,linorder_topology}"
  2045   assumes "b < x"
  2046   shows "interior {x ..} = {x <..}"
  2047 proof (rule interior_unique)
  2048   fix T
  2049   assume "T \<subseteq> {x ..}" "open T"
  2050   moreover have "x \<notin> T"
  2051   proof
  2052     assume "x \<in> T"
  2053     obtain y where "y < x" "{y <.. x} \<subseteq> T"
  2054       using open_left[OF \<open>open T\<close> \<open>x \<in> T\<close> \<open>b < x\<close>] by auto
  2055     with dense[OF \<open>y < x\<close>] obtain z where "z \<in> T" "z < x"
  2056       by (auto simp: subset_eq Ball_def)
  2057     with \<open>T \<subseteq> {x ..}\<close> show False by auto
  2058   qed
  2059   ultimately show "T \<subseteq> {x <..}"
  2060     by (auto simp: subset_eq less_le)
  2061 qed auto
  2062 
  2063 lemma interior_Iic:
  2064   fixes x :: "'a ::{dense_linorder,linorder_topology}"
  2065   assumes "x < b"
  2066   shows "interior {.. x} = {..< x}"
  2067 proof (rule interior_unique)
  2068   fix T
  2069   assume "T \<subseteq> {.. x}" "open T"
  2070   moreover have "x \<notin> T"
  2071   proof
  2072     assume "x \<in> T"
  2073     obtain y where "x < y" "{x ..< y} \<subseteq> T"
  2074       using open_right[OF \<open>open T\<close> \<open>x \<in> T\<close> \<open>x < b\<close>] by auto
  2075     with dense[OF \<open>x < y\<close>] obtain z where "z \<in> T" "x < z"
  2076       by (auto simp: subset_eq Ball_def less_le)
  2077     with \<open>T \<subseteq> {.. x}\<close> show False by auto
  2078   qed
  2079   ultimately show "T \<subseteq> {..< x}"
  2080     by (auto simp: subset_eq less_le)
  2081 qed auto
  2082 
  2083 
  2084 subsection \<open>Closure of a Set\<close>
  2085 
  2086 definition "closure S = S \<union> {x | x. x islimpt S}"
  2087 
  2088 lemma interior_closure: "interior S = - (closure (- S))"
  2089   by (auto simp add: interior_def closure_def islimpt_def)
  2090 
  2091 lemma closure_interior: "closure S = - interior (- S)"
  2092   by (simp add: interior_closure)
  2093 
  2094 lemma closed_closure[simp, intro]: "closed (closure S)"
  2095   by (simp add: closure_interior closed_Compl)
  2096 
  2097 lemma closure_subset: "S \<subseteq> closure S"
  2098   by (simp add: closure_def)
  2099 
  2100 lemma closure_hull: "closure S = closed hull S"
  2101   by (auto simp add: hull_def closure_interior interior_def)
  2102 
  2103 lemma closure_eq: "closure S = S \<longleftrightarrow> closed S"
  2104   unfolding closure_hull using closed_Inter by (rule hull_eq)
  2105 
  2106 lemma closure_closed [simp]: "closed S \<Longrightarrow> closure S = S"
  2107   by (simp only: closure_eq)
  2108 
  2109 lemma closure_closure [simp]: "closure (closure S) = closure S"
  2110   unfolding closure_hull by (rule hull_hull)
  2111 
  2112 lemma closure_mono: "S \<subseteq> T \<Longrightarrow> closure S \<subseteq> closure T"
  2113   unfolding closure_hull by (rule hull_mono)
  2114 
  2115 lemma closure_minimal: "S \<subseteq> T \<Longrightarrow> closed T \<Longrightarrow> closure S \<subseteq> T"
  2116   unfolding closure_hull by (rule hull_minimal)
  2117 
  2118 lemma closure_unique:
  2119   assumes "S \<subseteq> T"
  2120     and "closed T"
  2121     and "\<And>T'. S \<subseteq> T' \<Longrightarrow> closed T' \<Longrightarrow> T \<subseteq> T'"
  2122   shows "closure S = T"
  2123   using assms unfolding closure_hull by (rule hull_unique)
  2124 
  2125 lemma closure_empty [simp]: "closure {} = {}"
  2126   using closed_empty by (rule closure_closed)
  2127 
  2128 lemma closure_UNIV [simp]: "closure UNIV = UNIV"
  2129   using closed_UNIV by (rule closure_closed)
  2130 
  2131 lemma closure_Un [simp]: "closure (S \<union> T) = closure S \<union> closure T"
  2132   by (simp add: closure_interior)
  2133 
  2134 lemma closure_eq_empty [iff]: "closure S = {} \<longleftrightarrow> S = {}"
  2135   using closure_empty closure_subset[of S] by blast
  2136 
  2137 lemma closure_subset_eq: "closure S \<subseteq> S \<longleftrightarrow> closed S"
  2138   using closure_eq[of S] closure_subset[of S] by simp
  2139 
  2140 lemma open_Int_closure_eq_empty: "open S \<Longrightarrow> (S \<inter> closure T) = {} \<longleftrightarrow> S \<inter> T = {}"
  2141   using open_subset_interior[of S "- T"]
  2142   using interior_subset[of "- T"]
  2143   by (auto simp: closure_interior)
  2144 
  2145 lemma open_Int_closure_subset: "open S \<Longrightarrow> S \<inter> closure T \<subseteq> closure (S \<inter> T)"
  2146 proof
  2147   fix x
  2148   assume *: "open S" "x \<in> S \<inter> closure T"
  2149   have "x islimpt (S \<inter> T)" if **: "x islimpt T"
  2150   proof (rule islimptI)
  2151     fix A
  2152     assume "x \<in> A" "open A"
  2153     with * have "x \<in> A \<inter> S" "open (A \<inter> S)"
  2154       by (simp_all add: open_Int)
  2155     with ** obtain y where "y \<in> T" "y \<in> A \<inter> S" "y \<noteq> x"
  2156       by (rule islimptE)
  2157     then have "y \<in> S \<inter> T" "y \<in> A \<and> y \<noteq> x"
  2158       by simp_all
  2159     then show "\<exists>y\<in>(S \<inter> T). y \<in> A \<and> y \<noteq> x" ..
  2160   qed
  2161   with * show "x \<in> closure (S \<inter> T)"
  2162     unfolding closure_def by blast
  2163 qed
  2164 
  2165 lemma closure_complement: "closure (- S) = - interior S"
  2166   by (simp add: closure_interior)
  2167 
  2168 lemma interior_complement: "interior (- S) = - closure S"
  2169   by (simp add: closure_interior)
  2170 
  2171 lemma interior_diff: "interior(S - T) = interior S - closure T"
  2172   by (simp add: Diff_eq interior_complement)
  2173 
  2174 lemma closure_Times: "closure (A \<times> B) = closure A \<times> closure B"
  2175 proof (rule closure_unique)
  2176   show "A \<times> B \<subseteq> closure A \<times> closure B"
  2177     by (intro Sigma_mono closure_subset)
  2178   show "closed (closure A \<times> closure B)"
  2179     by (intro closed_Times closed_closure)
  2180   fix T
  2181   assume "A \<times> B \<subseteq> T" and "closed T"
  2182   then show "closure A \<times> closure B \<subseteq> T"
  2183     apply (simp add: closed_def open_prod_def)
  2184     apply clarify
  2185     apply (rule ccontr)
  2186     apply (drule_tac x="(a, b)" in bspec, simp, clarify, rename_tac C D)
  2187     apply (simp add: closure_interior interior_def)
  2188     apply (drule_tac x=C in spec)
  2189     apply (drule_tac x=D in spec)
  2190     apply auto
  2191     done
  2192 qed
  2193 
  2194 lemma islimpt_in_closure: "(x islimpt S) = (x:closure(S-{x}))"
  2195   unfolding closure_def using islimpt_punctured by blast
  2196 
  2197 lemma connected_imp_connected_closure: "connected S \<Longrightarrow> connected (closure S)"
  2198   by (rule connectedI) (meson closure_subset open_Int open_Int_closure_eq_empty subset_trans connectedD)
  2199 
  2200 lemma limpt_of_limpts: "x islimpt {y. y islimpt S} \<Longrightarrow> x islimpt S"
  2201   for x :: "'a::metric_space"
  2202   apply (clarsimp simp add: islimpt_approachable)
  2203   apply (drule_tac x="e/2" in spec)
  2204   apply (auto simp: simp del: less_divide_eq_numeral1)
  2205   apply (drule_tac x="dist x' x" in spec)
  2206   apply (auto simp: zero_less_dist_iff simp del: less_divide_eq_numeral1)
  2207   apply (erule rev_bexI)
  2208   apply (metis dist_commute dist_triangle_half_r less_trans less_irrefl)
  2209   done
  2210 
  2211 lemma closed_limpts:  "closed {x::'a::metric_space. x islimpt S}"
  2212   using closed_limpt limpt_of_limpts by blast
  2213 
  2214 lemma limpt_of_closure: "x islimpt closure S \<longleftrightarrow> x islimpt S"
  2215   for x :: "'a::metric_space"
  2216   by (auto simp: closure_def islimpt_Un dest: limpt_of_limpts)
  2217 
  2218 lemma closedin_limpt:
  2219   "closedin (subtopology euclidean T) S \<longleftrightarrow> S \<subseteq> T \<and> (\<forall>x. x islimpt S \<and> x \<in> T \<longrightarrow> x \<in> S)"
  2220   apply (simp add: closedin_closed, safe)
  2221    apply (simp add: closed_limpt islimpt_subset)
  2222   apply (rule_tac x="closure S" in exI)
  2223   apply simp
  2224   apply (force simp: closure_def)
  2225   done
  2226 
  2227 lemma closedin_closed_eq: "closed S \<Longrightarrow> closedin (subtopology euclidean S) T \<longleftrightarrow> closed T \<and> T \<subseteq> S"
  2228   by (meson closedin_limpt closed_subset closedin_closed_trans)
  2229 
  2230 lemma closedin_subset_trans:
  2231   "closedin (subtopology euclidean U) S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> T \<subseteq> U \<Longrightarrow>
  2232     closedin (subtopology euclidean T) S"
  2233   by (meson closedin_limpt subset_iff)
  2234 
  2235 lemma openin_subset_trans:
  2236   "openin (subtopology euclidean U) S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> T \<subseteq> U \<Longrightarrow>
  2237     openin (subtopology euclidean T) S"
  2238   by (auto simp: openin_open)
  2239 
  2240 lemma openin_Times:
  2241   "openin (subtopology euclidean S) S' \<Longrightarrow> openin (subtopology euclidean T) T' \<Longrightarrow>
  2242     openin (subtopology euclidean (S \<times> T)) (S' \<times> T')"
  2243   unfolding openin_open using open_Times by blast
  2244 
  2245 lemma Times_in_interior_subtopology:
  2246   fixes U :: "('a::metric_space \<times> 'b::metric_space) set"
  2247   assumes "(x, y) \<in> U" "openin (subtopology euclidean (S \<times> T)) U"
  2248   obtains V W where "openin (subtopology euclidean S) V" "x \<in> V"
  2249                     "openin (subtopology euclidean T) W" "y \<in> W" "(V \<times> W) \<subseteq> U"
  2250 proof -
  2251   from assms obtain e where "e > 0" and "U \<subseteq> S \<times> T"
  2252     and e: "\<And>x' y'. \<lbrakk>x'\<in>S; y'\<in>T; dist (x', y') (x, y) < e\<rbrakk> \<Longrightarrow> (x', y') \<in> U"
  2253     by (force simp: openin_euclidean_subtopology_iff)
  2254   with assms have "x \<in> S" "y \<in> T"
  2255     by auto
  2256   show ?thesis
  2257   proof
  2258     show "openin (subtopology euclidean S) (ball x (e/2) \<inter> S)"
  2259       by (simp add: Int_commute openin_open_Int)
  2260     show "x \<in> ball x (e / 2) \<inter> S"
  2261       by (simp add: \<open>0 < e\<close> \<open>x \<in> S\<close>)
  2262     show "openin (subtopology euclidean T) (ball y (e/2) \<inter> T)"
  2263       by (simp add: Int_commute openin_open_Int)
  2264     show "y \<in> ball y (e / 2) \<inter> T"
  2265       by (simp add: \<open>0 < e\<close> \<open>y \<in> T\<close>)
  2266     show "(ball x (e / 2) \<inter> S) \<times> (ball y (e / 2) \<inter> T) \<subseteq> U"
  2267       by clarify (simp add: e dist_Pair_Pair \<open>0 < e\<close> dist_commute sqrt_sum_squares_half_less)
  2268   qed
  2269 qed
  2270 
  2271 lemma openin_Times_eq:
  2272   fixes S :: "'a::metric_space set" and T :: "'b::metric_space set"
  2273   shows
  2274     "openin (subtopology euclidean (S \<times> T)) (S' \<times> T') \<longleftrightarrow>
  2275       S' = {} \<or> T' = {} \<or> openin (subtopology euclidean S) S' \<and> openin (subtopology euclidean T) T'"
  2276     (is "?lhs = ?rhs")
  2277 proof (cases "S' = {} \<or> T' = {}")
  2278   case True
  2279   then show ?thesis by auto
  2280 next
  2281   case False
  2282   then obtain x y where "x \<in> S'" "y \<in> T'"
  2283     by blast
  2284   show ?thesis
  2285   proof
  2286     assume ?lhs
  2287     have "openin (subtopology euclidean S) S'"
  2288       apply (subst openin_subopen, clarify)
  2289       apply (rule Times_in_interior_subtopology [OF _ \<open>?lhs\<close>])
  2290       using \<open>y \<in> T'\<close>
  2291        apply auto
  2292       done
  2293     moreover have "openin (subtopology euclidean T) T'"
  2294       apply (subst openin_subopen, clarify)
  2295       apply (rule Times_in_interior_subtopology [OF _ \<open>?lhs\<close>])
  2296       using \<open>x \<in> S'\<close>
  2297        apply auto
  2298       done
  2299     ultimately show ?rhs
  2300       by simp
  2301   next
  2302     assume ?rhs
  2303     with False show ?lhs
  2304       by (simp add: openin_Times)
  2305   qed
  2306 qed
  2307 
  2308 lemma closedin_Times:
  2309   "closedin (subtopology euclidean S) S' \<Longrightarrow> closedin (subtopology euclidean T) T' \<Longrightarrow>
  2310     closedin (subtopology euclidean (S \<times> T)) (S' \<times> T')"
  2311   unfolding closedin_closed using closed_Times by blast
  2312 
  2313 lemma bdd_below_closure:
  2314   fixes A :: "real set"
  2315   assumes "bdd_below A"
  2316   shows "bdd_below (closure A)"
  2317 proof -
  2318   from assms obtain m where "\<And>x. x \<in> A \<Longrightarrow> m \<le> x"
  2319     by (auto simp: bdd_below_def)
  2320   then have "A \<subseteq> {m..}" by auto
  2321   then have "closure A \<subseteq> {m..}"
  2322     using closed_real_atLeast by (rule closure_minimal)
  2323   then show ?thesis
  2324     by (auto simp: bdd_below_def)
  2325 qed
  2326 
  2327 
  2328 subsection \<open>Connected components, considered as a connectedness relation or a set\<close>
  2329 
  2330 definition "connected_component s x y \<equiv> \<exists>t. connected t \<and> t \<subseteq> s \<and> x \<in> t \<and> y \<in> t"
  2331 
  2332 abbreviation "connected_component_set s x \<equiv> Collect (connected_component s x)"
  2333 
  2334 lemma connected_componentI:
  2335   "connected t \<Longrightarrow> t \<subseteq> s \<Longrightarrow> x \<in> t \<Longrightarrow> y \<in> t \<Longrightarrow> connected_component s x y"
  2336   by (auto simp: connected_component_def)
  2337 
  2338 lemma connected_component_in: "connected_component s x y \<Longrightarrow> x \<in> s \<and> y \<in> s"
  2339   by (auto simp: connected_component_def)
  2340 
  2341 lemma connected_component_refl: "x \<in> s \<Longrightarrow> connected_component s x x"
  2342   by (auto simp: connected_component_def) (use connected_sing in blast)
  2343 
  2344 lemma connected_component_refl_eq [simp]: "connected_component s x x \<longleftrightarrow> x \<in> s"
  2345   by (auto simp: connected_component_refl) (auto simp: connected_component_def)
  2346 
  2347 lemma connected_component_sym: "connected_component s x y \<Longrightarrow> connected_component s y x"
  2348   by (auto simp: connected_component_def)
  2349 
  2350 lemma connected_component_trans:
  2351   "connected_component s x y \<Longrightarrow> connected_component s y z \<Longrightarrow> connected_component s x z"
  2352   unfolding connected_component_def
  2353   by (metis Int_iff Un_iff Un_subset_iff equals0D connected_Un)
  2354 
  2355 lemma connected_component_of_subset:
  2356   "connected_component s x y \<Longrightarrow> s \<subseteq> t \<Longrightarrow> connected_component t x y"
  2357   by (auto simp: connected_component_def)
  2358 
  2359 lemma connected_component_Union: "connected_component_set s x = \<Union>{t. connected t \<and> x \<in> t \<and> t \<subseteq> s}"
  2360   by (auto simp: connected_component_def)
  2361 
  2362 lemma connected_connected_component [iff]: "connected (connected_component_set s x)"
  2363   by (auto simp: connected_component_Union intro: connected_Union)
  2364 
  2365 lemma connected_iff_eq_connected_component_set:
  2366   "connected s \<longleftrightarrow> (\<forall>x \<in> s. connected_component_set s x = s)"
  2367 proof (cases "s = {}")
  2368   case True
  2369   then show ?thesis by simp
  2370 next
  2371   case False
  2372   then obtain x where "x \<in> s" by auto
  2373   show ?thesis
  2374   proof
  2375     assume "connected s"
  2376     then show "\<forall>x \<in> s. connected_component_set s x = s"
  2377       by (force simp: connected_component_def)
  2378   next
  2379     assume "\<forall>x \<in> s. connected_component_set s x = s"
  2380     then show "connected s"
  2381       by (metis \<open>x \<in> s\<close> connected_connected_component)
  2382   qed
  2383 qed
  2384 
  2385 lemma connected_component_subset: "connected_component_set s x \<subseteq> s"
  2386   using connected_component_in by blast
  2387 
  2388 lemma connected_component_eq_self: "connected s \<Longrightarrow> x \<in> s \<Longrightarrow> connected_component_set s x = s"
  2389   by (simp add: connected_iff_eq_connected_component_set)
  2390 
  2391 lemma connected_iff_connected_component:
  2392   "connected s \<longleftrightarrow> (\<forall>x \<in> s. \<forall>y \<in> s. connected_component s x y)"
  2393   using connected_component_in by (auto simp: connected_iff_eq_connected_component_set)
  2394 
  2395 lemma connected_component_maximal:
  2396   "x \<in> t \<Longrightarrow> connected t \<Longrightarrow> t \<subseteq> s \<Longrightarrow> t \<subseteq> (connected_component_set s x)"
  2397   using connected_component_eq_self connected_component_of_subset by blast
  2398 
  2399 lemma connected_component_mono:
  2400   "s \<subseteq> t \<Longrightarrow> connected_component_set s x \<subseteq> connected_component_set t x"
  2401   by (simp add: Collect_mono connected_component_of_subset)
  2402 
  2403 lemma connected_component_eq_empty [simp]: "connected_component_set s x = {} \<longleftrightarrow> x \<notin> s"
  2404   using connected_component_refl by (fastforce simp: connected_component_in)
  2405 
  2406 lemma connected_component_set_empty [simp]: "connected_component_set {} x = {}"
  2407   using connected_component_eq_empty by blast
  2408 
  2409 lemma connected_component_eq:
  2410   "y \<in> connected_component_set s x \<Longrightarrow> (connected_component_set s y = connected_component_set s x)"
  2411   by (metis (no_types, lifting)
  2412       Collect_cong connected_component_sym connected_component_trans mem_Collect_eq)
  2413 
  2414 lemma closed_connected_component:
  2415   assumes s: "closed s"
  2416   shows "closed (connected_component_set s x)"
  2417 proof (cases "x \<in> s")
  2418   case False
  2419   then show ?thesis
  2420     by (metis connected_component_eq_empty closed_empty)
  2421 next
  2422   case True
  2423   show ?thesis
  2424     unfolding closure_eq [symmetric]
  2425   proof
  2426     show "closure (connected_component_set s x) \<subseteq> connected_component_set s x"
  2427       apply (rule connected_component_maximal)
  2428         apply (simp add: closure_def True)
  2429        apply (simp add: connected_imp_connected_closure)
  2430       apply (simp add: s closure_minimal connected_component_subset)
  2431       done
  2432   next
  2433     show "connected_component_set s x \<subseteq> closure (connected_component_set s x)"
  2434       by (simp add: closure_subset)
  2435   qed
  2436 qed
  2437 
  2438 lemma connected_component_disjoint:
  2439   "connected_component_set s a \<inter> connected_component_set s b = {} \<longleftrightarrow>
  2440     a \<notin> connected_component_set s b"
  2441   apply (auto simp: connected_component_eq)
  2442   using connected_component_eq connected_component_sym
  2443   apply blast
  2444   done
  2445 
  2446 lemma connected_component_nonoverlap:
  2447   "connected_component_set s a \<inter> connected_component_set s b = {} \<longleftrightarrow>
  2448     a \<notin> s \<or> b \<notin> s \<or> connected_component_set s a \<noteq> connected_component_set s b"
  2449   apply (auto simp: connected_component_in)
  2450   using connected_component_refl_eq
  2451     apply blast
  2452    apply (metis connected_component_eq mem_Collect_eq)
  2453   apply (metis connected_component_eq mem_Collect_eq)
  2454   done
  2455 
  2456 lemma connected_component_overlap:
  2457   "connected_component_set s a \<inter> connected_component_set s b \<noteq> {} \<longleftrightarrow>
  2458     a \<in> s \<and> b \<in> s \<and> connected_component_set s a = connected_component_set s b"
  2459   by (auto simp: connected_component_nonoverlap)
  2460 
  2461 lemma connected_component_sym_eq: "connected_component s x y \<longleftrightarrow> connected_component s y x"
  2462   using connected_component_sym by blast
  2463 
  2464 lemma connected_component_eq_eq:
  2465   "connected_component_set s x = connected_component_set s y \<longleftrightarrow>
  2466     x \<notin> s \<and> y \<notin> s \<or> x \<in> s \<and> y \<in> s \<and> connected_component s x y"
  2467   apply (cases "y \<in> s")
  2468    apply (simp add:)
  2469    apply (metis connected_component_eq connected_component_eq_empty connected_component_refl_eq mem_Collect_eq)
  2470   apply (cases "x \<in> s")
  2471    apply (simp add:)
  2472    apply (metis connected_component_eq_empty)
  2473   using connected_component_eq_empty
  2474   apply blast
  2475   done
  2476 
  2477 lemma connected_iff_connected_component_eq:
  2478   "connected s \<longleftrightarrow> (\<forall>x \<in> s. \<forall>y \<in> s. connected_component_set s x = connected_component_set s y)"
  2479   by (simp add: connected_component_eq_eq connected_iff_connected_component)
  2480 
  2481 lemma connected_component_idemp:
  2482   "connected_component_set (connected_component_set s x) x = connected_component_set s x"
  2483   apply (rule subset_antisym)
  2484    apply (simp add: connected_component_subset)
  2485   apply (metis connected_component_eq_empty connected_component_maximal
  2486       connected_component_refl_eq connected_connected_component mem_Collect_eq set_eq_subset)
  2487   done
  2488 
  2489 lemma connected_component_unique:
  2490   "\<lbrakk>x \<in> c; c \<subseteq> s; connected c;
  2491     \<And>c'. x \<in> c' \<and> c' \<subseteq> s \<and> connected c'
  2492               \<Longrightarrow> c' \<subseteq> c\<rbrakk>
  2493         \<Longrightarrow> connected_component_set s x = c"
  2494 apply (rule subset_antisym)
  2495 apply (meson connected_component_maximal connected_component_subset connected_connected_component contra_subsetD)
  2496 by (simp add: connected_component_maximal)
  2497 
  2498 lemma joinable_connected_component_eq:
  2499   "\<lbrakk>connected t; t \<subseteq> s;
  2500     connected_component_set s x \<inter> t \<noteq> {};
  2501     connected_component_set s y \<inter> t \<noteq> {}\<rbrakk>
  2502     \<Longrightarrow> connected_component_set s x = connected_component_set s y"
  2503 apply (simp add: ex_in_conv [symmetric])
  2504 apply (rule connected_component_eq)
  2505 by (metis (no_types, hide_lams) connected_component_eq_eq connected_component_in connected_component_maximal subsetD mem_Collect_eq)
  2506 
  2507 
  2508 lemma Union_connected_component: "\<Union>(connected_component_set s ` s) = s"
  2509   apply (rule subset_antisym)
  2510   apply (simp add: SUP_least connected_component_subset)
  2511   using connected_component_refl_eq
  2512   by force
  2513 
  2514 
  2515 lemma complement_connected_component_unions:
  2516     "s - connected_component_set s x =
  2517      \<Union>(connected_component_set s ` s - {connected_component_set s x})"
  2518   apply (subst Union_connected_component [symmetric], auto)
  2519   apply (metis connected_component_eq_eq connected_component_in)
  2520   by (metis connected_component_eq mem_Collect_eq)
  2521 
  2522 lemma connected_component_intermediate_subset:
  2523         "\<lbrakk>connected_component_set u a \<subseteq> t; t \<subseteq> u\<rbrakk>
  2524         \<Longrightarrow> connected_component_set t a = connected_component_set u a"
  2525   apply (case_tac "a \<in> u")
  2526   apply (simp add: connected_component_maximal connected_component_mono subset_antisym)
  2527   using connected_component_eq_empty by blast
  2528 
  2529 proposition connected_Times:
  2530   assumes S: "connected S" and T: "connected T"
  2531   shows "connected (S \<times> T)"
  2532 proof (clarsimp simp add: connected_iff_connected_component)
  2533   fix x y x' y'
  2534   assume xy: "x \<in> S" "y \<in> T" "x' \<in> S" "y' \<in> T"
  2535   with xy obtain U V where U: "connected U" "U \<subseteq> S" "x \<in> U" "x' \<in> U"
  2536                        and V: "connected V" "V \<subseteq> T" "y \<in> V" "y' \<in> V"
  2537     using S T \<open>x \<in> S\<close> \<open>x' \<in> S\<close> by blast+
  2538   show "connected_component (S \<times> T) (x, y) (x', y')"
  2539     unfolding connected_component_def
  2540   proof (intro exI conjI)
  2541     show "connected ((\<lambda>x. (x, y)) ` U \<union> Pair x' ` V)"
  2542     proof (rule connected_Un)
  2543       have "continuous_on U (\<lambda>x. (x, y))"
  2544         by (intro continuous_intros)
  2545       then show "connected ((\<lambda>x. (x, y)) ` U)"
  2546         by (rule connected_continuous_image) (rule \<open>connected U\<close>)
  2547       have "continuous_on V (Pair x')"
  2548         by (intro continuous_intros)
  2549       then show "connected (Pair x' ` V)"
  2550         by (rule connected_continuous_image) (rule \<open>connected V\<close>)
  2551     qed (use U V in auto)
  2552   qed (use U V in auto)
  2553 qed
  2554 
  2555 corollary connected_Times_eq [simp]:
  2556    "connected (S \<times> T) \<longleftrightarrow> S = {} \<or> T = {} \<or> connected S \<and> connected T"  (is "?lhs = ?rhs")
  2557 proof
  2558   assume L: ?lhs
  2559   show ?rhs
  2560   proof (cases "S = {} \<or> T = {}")
  2561     case True
  2562     then show ?thesis by auto
  2563   next
  2564     case False
  2565     have "connected (fst ` (S \<times> T))" "connected (snd ` (S \<times> T))"
  2566       using continuous_on_fst continuous_on_snd continuous_on_id
  2567       by (blast intro: connected_continuous_image [OF _ L])+
  2568     with False show ?thesis
  2569       by auto
  2570   qed
  2571 next
  2572   assume ?rhs
  2573   then show ?lhs
  2574     by (auto simp: connected_Times)
  2575 qed
  2576 
  2577 
  2578 subsection \<open>The set of connected components of a set\<close>
  2579 
  2580 definition components:: "'a::topological_space set \<Rightarrow> 'a set set"
  2581   where "components s \<equiv> connected_component_set s ` s"
  2582 
  2583 lemma components_iff: "s \<in> components u \<longleftrightarrow> (\<exists>x. x \<in> u \<and> s = connected_component_set u x)"
  2584   by (auto simp: components_def)
  2585 
  2586 lemma componentsI: "x \<in> u \<Longrightarrow> connected_component_set u x \<in> components u"
  2587   by (auto simp: components_def)
  2588 
  2589 lemma componentsE:
  2590   assumes "s \<in> components u"
  2591   obtains x where "x \<in> u" "s = connected_component_set u x"
  2592   using assms by (auto simp: components_def)
  2593 
  2594 lemma Union_components [simp]: "\<Union>(components u) = u"
  2595   apply (rule subset_antisym)
  2596   using Union_connected_component components_def apply fastforce
  2597   apply (metis Union_connected_component components_def set_eq_subset)
  2598   done
  2599 
  2600 lemma pairwise_disjoint_components: "pairwise (\<lambda>X Y. X \<inter> Y = {}) (components u)"
  2601   apply (simp add: pairwise_def)
  2602   apply (auto simp: components_iff)
  2603   apply (metis connected_component_eq_eq connected_component_in)+
  2604   done
  2605 
  2606 lemma in_components_nonempty: "c \<in> components s \<Longrightarrow> c \<noteq> {}"
  2607     by (metis components_iff connected_component_eq_empty)
  2608 
  2609 lemma in_components_subset: "c \<in> components s \<Longrightarrow> c \<subseteq> s"
  2610   using Union_components by blast
  2611 
  2612 lemma in_components_connected: "c \<in> components s \<Longrightarrow> connected c"
  2613   by (metis components_iff connected_connected_component)
  2614 
  2615 lemma in_components_maximal:
  2616   "c \<in> components s \<longleftrightarrow>
  2617     c \<noteq> {} \<and> c \<subseteq> s \<and> connected c \<and> (\<forall>d. d \<noteq> {} \<and> c \<subseteq> d \<and> d \<subseteq> s \<and> connected d \<longrightarrow> d = c)"
  2618   apply (rule iffI)
  2619    apply (simp add: in_components_nonempty in_components_connected)
  2620    apply (metis (full_types) components_iff connected_component_eq_self connected_component_intermediate_subset connected_component_refl in_components_subset mem_Collect_eq rev_subsetD)
  2621   apply (metis bot.extremum_uniqueI components_iff connected_component_eq_empty connected_component_maximal connected_component_subset connected_connected_component subset_emptyI)
  2622   done
  2623 
  2624 lemma joinable_components_eq:
  2625   "connected t \<and> t \<subseteq> s \<and> c1 \<in> components s \<and> c2 \<in> components s \<and> c1 \<inter> t \<noteq> {} \<and> c2 \<inter> t \<noteq> {} \<Longrightarrow> c1 = c2"
  2626   by (metis (full_types) components_iff joinable_connected_component_eq)
  2627 
  2628 lemma closed_components: "\<lbrakk>closed s; c \<in> components s\<rbrakk> \<Longrightarrow> closed c"
  2629   by (metis closed_connected_component components_iff)
  2630 
  2631 lemma components_nonoverlap:
  2632     "\<lbrakk>c \<in> components s; c' \<in> components s\<rbrakk> \<Longrightarrow> (c \<inter> c' = {}) \<longleftrightarrow> (c \<noteq> c')"
  2633   apply (auto simp: in_components_nonempty components_iff)
  2634     using connected_component_refl apply blast
  2635    apply (metis connected_component_eq_eq connected_component_in)
  2636   by (metis connected_component_eq mem_Collect_eq)
  2637 
  2638 lemma components_eq: "\<lbrakk>c \<in> components s; c' \<in> components s\<rbrakk> \<Longrightarrow> (c = c' \<longleftrightarrow> c \<inter> c' \<noteq> {})"
  2639   by (metis components_nonoverlap)
  2640 
  2641 lemma components_eq_empty [simp]: "components s = {} \<longleftrightarrow> s = {}"
  2642   by (simp add: components_def)
  2643 
  2644 lemma components_empty [simp]: "components {} = {}"
  2645   by simp
  2646 
  2647 lemma connected_eq_connected_components_eq: "connected s \<longleftrightarrow> (\<forall>c \<in> components s. \<forall>c' \<in> components s. c = c')"
  2648   by (metis (no_types, hide_lams) components_iff connected_component_eq_eq connected_iff_connected_component)
  2649 
  2650 lemma components_eq_sing_iff: "components s = {s} \<longleftrightarrow> connected s \<and> s \<noteq> {}"
  2651   apply (rule iffI)
  2652   using in_components_connected apply fastforce
  2653   apply safe
  2654   using Union_components apply fastforce
  2655    apply (metis components_iff connected_component_eq_self)
  2656   using in_components_maximal
  2657   apply auto
  2658   done
  2659 
  2660 lemma components_eq_sing_exists: "(\<exists>a. components s = {a}) \<longleftrightarrow> connected s \<and> s \<noteq> {}"
  2661   apply (rule iffI)
  2662   using connected_eq_connected_components_eq apply fastforce
  2663   apply (metis components_eq_sing_iff)
  2664   done
  2665 
  2666 lemma connected_eq_components_subset_sing: "connected s \<longleftrightarrow> components s \<subseteq> {s}"
  2667   by (metis Union_components components_empty components_eq_sing_iff connected_empty insert_subset order_refl subset_singletonD)
  2668 
  2669 lemma connected_eq_components_subset_sing_exists: "connected s \<longleftrightarrow> (\<exists>a. components s \<subseteq> {a})"
  2670   by (metis components_eq_sing_exists connected_eq_components_subset_sing empty_iff subset_iff subset_singletonD)
  2671 
  2672 lemma in_components_self: "s \<in> components s \<longleftrightarrow> connected s \<and> s \<noteq> {}"
  2673   by (metis components_empty components_eq_sing_iff empty_iff in_components_connected insertI1)
  2674 
  2675 lemma components_maximal: "\<lbrakk>c \<in> components s; connected t; t \<subseteq> s; c \<inter> t \<noteq> {}\<rbrakk> \<Longrightarrow> t \<subseteq> c"
  2676   apply (simp add: components_def ex_in_conv [symmetric], clarify)
  2677   by (meson connected_component_def connected_component_trans)
  2678 
  2679 lemma exists_component_superset: "\<lbrakk>t \<subseteq> s; s \<noteq> {}; connected t\<rbrakk> \<Longrightarrow> \<exists>c. c \<in> components s \<and> t \<subseteq> c"
  2680   apply (cases "t = {}")
  2681    apply force
  2682   apply (metis components_def ex_in_conv connected_component_maximal contra_subsetD image_eqI)
  2683   done
  2684 
  2685 lemma components_intermediate_subset: "\<lbrakk>s \<in> components u; s \<subseteq> t; t \<subseteq> u\<rbrakk> \<Longrightarrow> s \<in> components t"
  2686   apply (auto simp: components_iff)
  2687   apply (metis connected_component_eq_empty connected_component_intermediate_subset)
  2688   done
  2689 
  2690 lemma in_components_unions_complement: "c \<in> components s \<Longrightarrow> s - c = \<Union>(components s - {c})"
  2691   by (metis complement_connected_component_unions components_def components_iff)
  2692 
  2693 lemma connected_intermediate_closure:
  2694   assumes cs: "connected s" and st: "s \<subseteq> t" and ts: "t \<subseteq> closure s"
  2695   shows "connected t"
  2696 proof (rule connectedI)
  2697   fix A B
  2698   assume A: "open A" and B: "open B" and Alap: "A \<inter> t \<noteq> {}" and Blap: "B \<inter> t \<noteq> {}"
  2699     and disj: "A \<inter> B \<inter> t = {}" and cover: "t \<subseteq> A \<union> B"
  2700   have disjs: "A \<inter> B \<inter> s = {}"
  2701     using disj st by auto
  2702   have "A \<inter> closure s \<noteq> {}"
  2703     using Alap Int_absorb1 ts by blast
  2704   then have Alaps: "A \<inter> s \<noteq> {}"
  2705     by (simp add: A open_Int_closure_eq_empty)
  2706   have "B \<inter> closure s \<noteq> {}"
  2707     using Blap Int_absorb1 ts by blast
  2708   then have Blaps: "B \<inter> s \<noteq> {}"
  2709     by (simp add: B open_Int_closure_eq_empty)
  2710   then show False
  2711     using cs [unfolded connected_def] A B disjs Alaps Blaps cover st
  2712     by blast
  2713 qed
  2714 
  2715 lemma closedin_connected_component: "closedin (subtopology euclidean s) (connected_component_set s x)"
  2716 proof (cases "connected_component_set s x = {}")
  2717   case True
  2718   then show ?thesis
  2719     by (metis closedin_empty)
  2720 next
  2721   case False
  2722   then obtain y where y: "connected_component s x y"
  2723     by blast
  2724   have *: "connected_component_set s x \<subseteq> s \<inter> closure (connected_component_set s x)"
  2725     by (auto simp: closure_def connected_component_in)
  2726   have "connected_component s x y \<Longrightarrow> s \<inter> closure (connected_component_set s x) \<subseteq> connected_component_set s x"
  2727     apply (rule connected_component_maximal)
  2728       apply simp
  2729     using closure_subset connected_component_in apply fastforce
  2730     using * connected_intermediate_closure apply blast+
  2731     done
  2732   with y * show ?thesis
  2733     by (auto simp add: Topology_Euclidean_Space.closedin_closed)
  2734 qed
  2735 
  2736 
  2737 subsection \<open>Frontier (also known as boundary)\<close>
  2738 
  2739 definition "frontier S = closure S - interior S"
  2740 
  2741 lemma frontier_closed [iff]: "closed (frontier S)"
  2742   by (simp add: frontier_def closed_Diff)
  2743 
  2744 lemma frontier_closures: "frontier S = closure S \<inter> closure (- S)"
  2745   by (auto simp add: frontier_def interior_closure)
  2746 
  2747 lemma frontier_straddle:
  2748   fixes a :: "'a::metric_space"
  2749   shows "a \<in> frontier S \<longleftrightarrow> (\<forall>e>0. (\<exists>x\<in>S. dist a x < e) \<and> (\<exists>x. x \<notin> S \<and> dist a x < e))"
  2750   unfolding frontier_def closure_interior
  2751   by (auto simp add: mem_interior subset_eq ball_def)
  2752 
  2753 lemma frontier_subset_closed: "closed S \<Longrightarrow> frontier S \<subseteq> S"
  2754   by (metis frontier_def closure_closed Diff_subset)
  2755 
  2756 lemma frontier_empty [simp]: "frontier {} = {}"
  2757   by (simp add: frontier_def)
  2758 
  2759 lemma frontier_subset_eq: "frontier S \<subseteq> S \<longleftrightarrow> closed S"
  2760 proof -
  2761   {
  2762     assume "frontier S \<subseteq> S"
  2763     then have "closure S \<subseteq> S"
  2764       using interior_subset unfolding frontier_def by auto
  2765     then have "closed S"
  2766       using closure_subset_eq by auto
  2767   }
  2768   then show ?thesis using frontier_subset_closed[of S] ..
  2769 qed
  2770 
  2771 lemma frontier_complement [simp]: "frontier (- S) = frontier S"
  2772   by (auto simp add: frontier_def closure_complement interior_complement)
  2773 
  2774 lemma frontier_disjoint_eq: "frontier S \<inter> S = {} \<longleftrightarrow> open S"
  2775   using frontier_complement frontier_subset_eq[of "- S"]
  2776   unfolding open_closed by auto
  2777 
  2778 lemma frontier_UNIV [simp]: "frontier UNIV = {}"
  2779   using frontier_complement frontier_empty by fastforce
  2780 
  2781 lemma frontier_interiors: "frontier s = - interior(s) - interior(-s)"
  2782   by (simp add: Int_commute frontier_def interior_closure)
  2783 
  2784 lemma frontier_interior_subset: "frontier(interior S) \<subseteq> frontier S"
  2785   by (simp add: Diff_mono frontier_interiors interior_mono interior_subset)
  2786 
  2787 lemma connected_Int_frontier:
  2788      "\<lbrakk>connected s; s \<inter> t \<noteq> {}; s - t \<noteq> {}\<rbrakk> \<Longrightarrow> (s \<inter> frontier t \<noteq> {})"
  2789   apply (simp add: frontier_interiors connected_openin, safe)
  2790   apply (drule_tac x="s \<inter> interior t" in spec, safe)
  2791    apply (drule_tac [2] x="s \<inter> interior (-t)" in spec)
  2792    apply (auto simp: disjoint_eq_subset_Compl dest: interior_subset [THEN subsetD])
  2793   done
  2794 
  2795 lemma closure_Un_frontier: "closure S = S \<union> frontier S"
  2796 proof -
  2797   have "S \<union> interior S = S"
  2798     using interior_subset by auto
  2799   then show ?thesis
  2800     using closure_subset by (auto simp: frontier_def)
  2801 qed
  2802 
  2803 
  2804 subsection \<open>Filters and the ``eventually true'' quantifier\<close>
  2805 
  2806 definition indirection :: "'a::real_normed_vector \<Rightarrow> 'a \<Rightarrow> 'a filter"  (infixr "indirection" 70)
  2807   where "a indirection v = at a within {b. \<exists>c\<ge>0. b - a = scaleR c v}"
  2808 
  2809 text \<open>Identify Trivial limits, where we can't approach arbitrarily closely.\<close>
  2810 
  2811 lemma trivial_limit_within: "trivial_limit (at a within S) \<longleftrightarrow> \<not> a islimpt S"
  2812 proof
  2813   assume "trivial_limit (at a within S)"
  2814   then show "\<not> a islimpt S"
  2815     unfolding trivial_limit_def
  2816     unfolding eventually_at_topological
  2817     unfolding islimpt_def
  2818     apply (clarsimp simp add: set_eq_iff)
  2819     apply (rename_tac T, rule_tac x=T in exI)
  2820     apply (clarsimp, drule_tac x=y in bspec, simp_all)
  2821     done
  2822 next
  2823   assume "\<not> a islimpt S"
  2824   then show "trivial_limit (at a within S)"
  2825     unfolding trivial_limit_def eventually_at_topological islimpt_def
  2826     by metis
  2827 qed
  2828 
  2829 lemma trivial_limit_at_iff: "trivial_limit (at a) \<longleftrightarrow> \<not> a islimpt UNIV"
  2830   using trivial_limit_within [of a UNIV] by simp
  2831 
  2832 lemma trivial_limit_at: "\<not> trivial_limit (at a)"
  2833   for a :: "'a::perfect_space"
  2834   by (rule at_neq_bot)
  2835 
  2836 lemma trivial_limit_at_infinity:
  2837   "\<not> trivial_limit (at_infinity :: ('a::{real_normed_vector,perfect_space}) filter)"
  2838   unfolding trivial_limit_def eventually_at_infinity
  2839   apply clarsimp
  2840   apply (subgoal_tac "\<exists>x::'a. x \<noteq> 0", clarify)
  2841    apply (rule_tac x="scaleR (b / norm x) x" in exI, simp)
  2842   apply (cut_tac islimpt_UNIV [of "0::'a", unfolded islimpt_def])
  2843   apply (drule_tac x=UNIV in spec, simp)
  2844   done
  2845 
  2846 lemma not_trivial_limit_within: "\<not> trivial_limit (at x within S) = (x \<in> closure (S - {x}))"
  2847   using islimpt_in_closure by (metis trivial_limit_within)
  2848 
  2849 lemma at_within_eq_bot_iff: "at c within A = bot \<longleftrightarrow> c \<notin> closure (A - {c})"
  2850   using not_trivial_limit_within[of c A] by blast
  2851 
  2852 text \<open>Some property holds "sufficiently close" to the limit point.\<close>
  2853 
  2854 lemma trivial_limit_eventually: "trivial_limit net \<Longrightarrow> eventually P net"
  2855   by simp
  2856 
  2857 lemma trivial_limit_eq: "trivial_limit net \<longleftrightarrow> (\<forall>P. eventually P net)"
  2858   by (simp add: filter_eq_iff)
  2859 
  2860 
  2861 subsection \<open>Limits\<close>
  2862 
  2863 lemma Lim: "(f \<longlongrightarrow> l) net \<longleftrightarrow> trivial_limit net \<or> (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) net)"
  2864   by (auto simp: tendsto_iff trivial_limit_eq)
  2865 
  2866 text \<open>Show that they yield usual definitions in the various cases.\<close>
  2867 
  2868 lemma Lim_within_le: "(f \<longlongrightarrow> l)(at a within S) \<longleftrightarrow>
  2869     (\<forall>e>0. \<exists>d>0. \<forall>x\<in>S. 0 < dist x a \<and> dist x a \<le> d \<longrightarrow> dist (f x) l < e)"
  2870   by (auto simp add: tendsto_iff eventually_at_le)
  2871 
  2872 lemma Lim_within: "(f \<longlongrightarrow> l) (at a within S) \<longleftrightarrow>
  2873     (\<forall>e >0. \<exists>d>0. \<forall>x \<in> S. 0 < dist x a \<and> dist x a  < d \<longrightarrow> dist (f x) l < e)"
  2874   by (auto simp add: tendsto_iff eventually_at)
  2875 
  2876 corollary Lim_withinI [intro?]:
  2877   assumes "\<And>e. e > 0 \<Longrightarrow> \<exists>d>0. \<forall>x \<in> S. 0 < dist x a \<and> dist x a < d \<longrightarrow> dist (f x) l \<le> e"
  2878   shows "(f \<longlongrightarrow> l) (at a within S)"
  2879   apply (simp add: Lim_within, clarify)
  2880   apply (rule ex_forward [OF assms [OF half_gt_zero]])
  2881   apply auto
  2882   done
  2883 
  2884 lemma Lim_at: "(f \<longlongrightarrow> l) (at a) \<longleftrightarrow>
  2885     (\<forall>e >0. \<exists>d>0. \<forall>x. 0 < dist x a \<and> dist x a < d  \<longrightarrow> dist (f x) l < e)"
  2886   by (auto simp add: tendsto_iff eventually_at)
  2887 
  2888 lemma Lim_at_infinity: "(f \<longlongrightarrow> l) at_infinity \<longleftrightarrow> (\<forall>e>0. \<exists>b. \<forall>x. norm x \<ge> b \<longrightarrow> dist (f x) l < e)"
  2889   by (auto simp add: tendsto_iff eventually_at_infinity)
  2890 
  2891 corollary Lim_at_infinityI [intro?]:
  2892   assumes "\<And>e. e > 0 \<Longrightarrow> \<exists>B. \<forall>x. norm x \<ge> B \<longrightarrow> dist (f x) l \<le> e"
  2893   shows "(f \<longlongrightarrow> l) at_infinity"
  2894   apply (simp add: Lim_at_infinity, clarify)
  2895   apply (rule ex_forward [OF assms [OF half_gt_zero]])
  2896    apply auto
  2897   done
  2898 
  2899 lemma Lim_eventually: "eventually (\<lambda>x. f x = l) net \<Longrightarrow> (f \<longlongrightarrow> l) net"
  2900   by (rule topological_tendstoI) (auto elim: eventually_mono)
  2901 
  2902 lemma Lim_transform_within_set:
  2903   fixes a :: "'a::metric_space" and l :: "'b::metric_space"
  2904   shows "\<lbrakk>(f \<longlongrightarrow> l) (at a within S); eventually (\<lambda>x. x \<in> S \<longleftrightarrow> x \<in> T) (at a)\<rbrakk>
  2905          \<Longrightarrow> (f \<longlongrightarrow> l) (at a within T)"
  2906 apply (clarsimp simp: eventually_at Lim_within)
  2907 apply (drule_tac x=e in spec, clarify)
  2908 apply (rename_tac k)
  2909 apply (rule_tac x="min d k" in exI, simp)
  2910 done
  2911 
  2912 lemma Lim_transform_within_set_eq:
  2913   fixes a l :: "'a::real_normed_vector"
  2914   shows "eventually (\<lambda>x. x \<in> s \<longleftrightarrow> x \<in> t) (at a)
  2915          \<Longrightarrow> ((f \<longlongrightarrow> l) (at a within s) \<longleftrightarrow> (f \<longlongrightarrow> l) (at a within t))"
  2916   by (force intro: Lim_transform_within_set elim: eventually_mono)
  2917 
  2918 lemma Lim_transform_within_openin:
  2919   fixes a :: "'a::metric_space"
  2920   assumes f: "(f \<longlongrightarrow> l) (at a within T)"
  2921     and "openin (subtopology euclidean T) S" "a \<in> S"
  2922     and eq: "\<And>x. \<lbrakk>x \<in> S; x \<noteq> a\<rbrakk> \<Longrightarrow> f x = g x"
  2923   shows "(g \<longlongrightarrow> l) (at a within T)"
  2924 proof -
  2925   obtain \<epsilon> where "0 < \<epsilon>" and \<epsilon>: "ball a \<epsilon> \<inter> T \<subseteq> S"
  2926     using assms by (force simp: openin_contains_ball)
  2927   then have "a \<in> ball a \<epsilon>"
  2928     by simp
  2929   show ?thesis
  2930     by (rule Lim_transform_within [OF f \<open>0 < \<epsilon>\<close> eq]) (use \<epsilon> in \<open>auto simp: dist_commute subset_iff\<close>)
  2931 qed
  2932 
  2933 lemma continuous_transform_within_openin:
  2934   fixes a :: "'a::metric_space"
  2935   assumes "continuous (at a within T) f"
  2936     and "openin (subtopology euclidean T) S" "a \<in> S"
  2937     and eq: "\<And>x. x \<in> S \<Longrightarrow> f x = g x"
  2938   shows "continuous (at a within T) g"
  2939   using assms by (simp add: Lim_transform_within_openin continuous_within)
  2940 
  2941 text \<open>The expected monotonicity property.\<close>
  2942 
  2943 lemma Lim_Un:
  2944   assumes "(f \<longlongrightarrow> l) (at x within S)" "(f \<longlongrightarrow> l) (at x within T)"
  2945   shows "(f \<longlongrightarrow> l) (at x within (S \<union> T))"
  2946   using assms unfolding at_within_union by (rule filterlim_sup)
  2947 
  2948 lemma Lim_Un_univ:
  2949   "(f \<longlongrightarrow> l) (at x within S) \<Longrightarrow> (f \<longlongrightarrow> l) (at x within T) \<Longrightarrow>
  2950     S \<union> T = UNIV \<Longrightarrow> (f \<longlongrightarrow> l) (at x)"
  2951   by (metis Lim_Un)
  2952 
  2953 text \<open>Interrelations between restricted and unrestricted limits.\<close>
  2954 
  2955 lemma Lim_at_imp_Lim_at_within: "(f \<longlongrightarrow> l) (at x) \<Longrightarrow> (f \<longlongrightarrow> l) (at x within S)"
  2956   by (metis order_refl filterlim_mono subset_UNIV at_le)
  2957 
  2958 lemma eventually_within_interior:
  2959   assumes "x \<in> interior S"
  2960   shows "eventually P (at x within S) \<longleftrightarrow> eventually P (at x)"
  2961   (is "?lhs = ?rhs")
  2962 proof
  2963   from assms obtain T where T: "open T" "x \<in> T" "T \<subseteq> S" ..
  2964   {
  2965     assume ?lhs
  2966     then obtain A where "open A" and "x \<in> A" and "\<forall>y\<in>A. y \<noteq> x \<longrightarrow> y \<in> S \<longrightarrow> P y"
  2967       by (auto simp: eventually_at_topological)
  2968     with T have "open (A \<inter> T)" and "x \<in> A \<inter> T" and "\<forall>y \<in> A \<inter> T. y \<noteq> x \<longrightarrow> P y"
  2969       by auto
  2970     then show ?rhs
  2971       by (auto simp: eventually_at_topological)
  2972   next
  2973     assume ?rhs
  2974     then show ?lhs
  2975       by (auto elim: eventually_mono simp: eventually_at_filter)
  2976   }
  2977 qed
  2978 
  2979 lemma at_within_interior: "x \<in> interior S \<Longrightarrow> at x within S = at x"
  2980   unfolding filter_eq_iff by (intro allI eventually_within_interior)
  2981 
  2982 lemma Lim_within_LIMSEQ:
  2983   fixes a :: "'a::first_countable_topology"
  2984   assumes "\<forall>S. (\<forall>n. S n \<noteq> a \<and> S n \<in> T) \<and> S \<longlonglongrightarrow> a \<longrightarrow> (\<lambda>n. X (S n)) \<longlonglongrightarrow> L"
  2985   shows "(X \<longlongrightarrow> L) (at a within T)"
  2986   using assms unfolding tendsto_def [where l=L]
  2987   by (simp add: sequentially_imp_eventually_within)
  2988 
  2989 lemma Lim_right_bound:
  2990   fixes f :: "'a :: {linorder_topology, conditionally_complete_linorder, no_top} \<Rightarrow>
  2991     'b::{linorder_topology, conditionally_complete_linorder}"
  2992   assumes mono: "\<And>a b. a \<in> I \<Longrightarrow> b \<in> I \<Longrightarrow> x < a \<Longrightarrow> a \<le> b \<Longrightarrow> f a \<le> f b"
  2993     and bnd: "\<And>a. a \<in> I \<Longrightarrow> x < a \<Longrightarrow> K \<le> f a"
  2994   shows "(f \<longlongrightarrow> Inf (f ` ({x<..} \<inter> I))) (at x within ({x<..} \<inter> I))"
  2995 proof (cases "{x<..} \<inter> I = {}")
  2996   case True
  2997   then show ?thesis by simp
  2998 next
  2999   case False
  3000   show ?thesis
  3001   proof (rule order_tendstoI)
  3002     fix a
  3003     assume a: "a < Inf (f ` ({x<..} \<inter> I))"
  3004     {
  3005       fix y
  3006       assume "y \<in> {x<..} \<inter> I"
  3007       with False bnd have "Inf (f ` ({x<..} \<inter> I)) \<le> f y"
  3008         by (auto intro!: cInf_lower bdd_belowI2)
  3009       with a have "a < f y"
  3010         by (blast intro: less_le_trans)
  3011     }
  3012     then show "eventually (\<lambda>x. a < f x) (at x within ({x<..} \<inter> I))"
  3013       by (auto simp: eventually_at_filter intro: exI[of _ 1] zero_less_one)
  3014   next
  3015     fix a
  3016     assume "Inf (f ` ({x<..} \<inter> I)) < a"
  3017     from cInf_lessD[OF _ this] False obtain y where y: "x < y" "y \<in> I" "f y < a"
  3018       by auto
  3019     then have "eventually (\<lambda>x. x \<in> I \<longrightarrow> f x < a) (at_right x)"
  3020       unfolding eventually_at_right[OF \<open>x < y\<close>] by (metis less_imp_le le_less_trans mono)
  3021     then show "eventually (\<lambda>x. f x < a) (at x within ({x<..} \<inter> I))"
  3022       unfolding eventually_at_filter by eventually_elim simp
  3023   qed
  3024 qed
  3025 
  3026 text \<open>Another limit point characterization.\<close>
  3027 
  3028 lemma limpt_sequential_inj:
  3029   fixes x :: "'a::metric_space"
  3030   shows "x islimpt S \<longleftrightarrow>
  3031          (\<exists>f. (\<forall>n::nat. f n \<in> S - {x}) \<and> inj f \<and> (f \<longlongrightarrow> x) sequentially)"
  3032          (is "?lhs = ?rhs")
  3033 proof
  3034   assume ?lhs
  3035   then have "\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e"
  3036     by (force simp: islimpt_approachable)
  3037   then obtain y where y: "\<And>e. e>0 \<Longrightarrow> y e \<in> S \<and> y e \<noteq> x \<and> dist (y e) x < e"
  3038     by metis
  3039   define f where "f \<equiv> rec_nat (y 1) (\<lambda>n fn. y (min (inverse(2 ^ (Suc n))) (dist fn x)))"
  3040   have [simp]: "f 0 = y 1"
  3041                "f(Suc n) = y (min (inverse(2 ^ (Suc n))) (dist (f n) x))" for n
  3042     by (simp_all add: f_def)
  3043   have f: "f n \<in> S \<and> (f n \<noteq> x) \<and> dist (f n) x < inverse(2 ^ n)" for n
  3044   proof (induction n)
  3045     case 0 show ?case
  3046       by (simp add: y)
  3047   next
  3048     case (Suc n) then show ?case
  3049       apply (auto simp: y)
  3050       by (metis half_gt_zero_iff inverse_positive_iff_positive less_divide_eq_numeral1(1) min_less_iff_conj y zero_less_dist_iff zero_less_numeral zero_less_power)
  3051   qed
  3052   show ?rhs
  3053   proof (rule_tac x=f in exI, intro conjI allI)
  3054     show "\<And>n. f n \<in> S - {x}"
  3055       using f by blast
  3056     have "dist (f n) x < dist (f m) x" if "m < n" for m n
  3057     using that
  3058     proof (induction n)
  3059       case 0 then show ?case by simp
  3060     next
  3061       case (Suc n)
  3062       then consider "m < n" | "m = n" using less_Suc_eq by blast
  3063       then show ?case
  3064       proof cases
  3065         assume "m < n"
  3066         have "dist (f(Suc n)) x = dist (y (min (inverse(2 ^ (Suc n))) (dist (f n) x))) x"
  3067           by simp
  3068         also have "... < dist (f n) x"
  3069           by (metis dist_pos_lt f min.strict_order_iff min_less_iff_conj y)
  3070         also have "... < dist (f m) x"
  3071           using Suc.IH \<open>m < n\<close> by blast
  3072         finally show ?thesis .
  3073       next
  3074         assume "m = n" then show ?case
  3075           by simp (metis dist_pos_lt f half_gt_zero_iff inverse_positive_iff_positive min_less_iff_conj y zero_less_numeral zero_less_power)
  3076       qed
  3077     qed
  3078     then show "inj f"
  3079       by (metis less_irrefl linorder_injI)
  3080     show "f \<longlonglongrightarrow> x"
  3081       apply (rule tendstoI)
  3082       apply (rule_tac c="nat (ceiling(1/e))" in eventually_sequentiallyI)
  3083       apply (rule less_trans [OF f [THEN conjunct2, THEN conjunct2]])
  3084       apply (simp add: field_simps)
  3085       by (meson le_less_trans mult_less_cancel_left not_le of_nat_less_two_power)
  3086   qed
  3087 next
  3088   assume ?rhs
  3089   then show ?lhs
  3090     by (fastforce simp add: islimpt_approachable lim_sequentially)
  3091 qed
  3092 
  3093 (*could prove directly from islimpt_sequential_inj, but only for metric spaces*)
  3094 lemma islimpt_sequential:
  3095   fixes x :: "'a::first_countable_topology"
  3096   shows "x islimpt S \<longleftrightarrow> (\<exists>f. (\<forall>n::nat. f n \<in> S - {x}) \<and> (f \<longlongrightarrow> x) sequentially)"
  3097     (is "?lhs = ?rhs")
  3098 proof
  3099   assume ?lhs
  3100   from countable_basis_at_decseq[of x] obtain A where A:
  3101       "\<And>i. open (A i)"
  3102       "\<And>i. x \<in> A i"
  3103       "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
  3104     by blast
  3105   define f where "f n = (SOME y. y \<in> S \<and> y \<in> A n \<and> x \<noteq> y)" for n
  3106   {
  3107     fix n
  3108     from \<open>?lhs\<close> have "\<exists>y. y \<in> S \<and> y \<in> A n \<and> x \<noteq> y"
  3109       unfolding islimpt_def using A(1,2)[of n] by auto
  3110     then have "f n \<in> S \<and> f n \<in> A n \<and> x \<noteq> f n"
  3111       unfolding f_def by (rule someI_ex)
  3112     then have "f n \<in> S" "f n \<in> A n" "x \<noteq> f n" by auto
  3113   }
  3114   then have "\<forall>n. f n \<in> S - {x}" by auto
  3115   moreover have "(\<lambda>n. f n) \<longlonglongrightarrow> x"
  3116   proof (rule topological_tendstoI)
  3117     fix S
  3118     assume "open S" "x \<in> S"
  3119     from A(3)[OF this] \<open>\<And>n. f n \<in> A n\<close>
  3120     show "eventually (\<lambda>x. f x \<in> S) sequentially"
  3121       by (auto elim!: eventually_mono)
  3122   qed
  3123   ultimately show ?rhs by fast
  3124 next
  3125   assume ?rhs
  3126   then obtain f :: "nat \<Rightarrow> 'a" where f: "\<And>n. f n \<in> S - {x}" and lim: "f \<longlonglongrightarrow> x"
  3127     by auto
  3128   show ?lhs
  3129     unfolding islimpt_def
  3130   proof safe
  3131     fix T
  3132     assume "open T" "x \<in> T"
  3133     from lim[THEN topological_tendstoD, OF this] f
  3134     show "\<exists>y\<in>S. y \<in> T \<and> y \<noteq> x"
  3135       unfolding eventually_sequentially by auto
  3136   qed
  3137 qed
  3138 
  3139 lemma Lim_null:
  3140   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  3141   shows "(f \<longlongrightarrow> l) net \<longleftrightarrow> ((\<lambda>x. f(x) - l) \<longlongrightarrow> 0) net"
  3142   by (simp add: Lim dist_norm)
  3143 
  3144 lemma Lim_null_comparison:
  3145   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  3146   assumes "eventually (\<lambda>x. norm (f x) \<le> g x) net" "(g \<longlongrightarrow> 0) net"
  3147   shows "(f \<longlongrightarrow> 0) net"
  3148   using assms(2)
  3149 proof (rule metric_tendsto_imp_tendsto)
  3150   show "eventually (\<lambda>x. dist (f x) 0 \<le> dist (g x) 0) net"
  3151     using assms(1) by (rule eventually_mono) (simp add: dist_norm)
  3152 qed
  3153 
  3154 lemma Lim_transform_bound:
  3155   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  3156     and g :: "'a \<Rightarrow> 'c::real_normed_vector"
  3157   assumes "eventually (\<lambda>n. norm (f n) \<le> norm (g n)) net"
  3158     and "(g \<longlongrightarrow> 0) net"
  3159   shows "(f \<longlongrightarrow> 0) net"
  3160   using assms(1) tendsto_norm_zero [OF assms(2)]
  3161   by (rule Lim_null_comparison)
  3162 
  3163 lemma lim_null_mult_right_bounded:
  3164   fixes f :: "'a \<Rightarrow> 'b::real_normed_div_algebra"
  3165   assumes f: "(f \<longlongrightarrow> 0) F" and g: "eventually (\<lambda>x. norm(g x) \<le> B) F"
  3166     shows "((\<lambda>z. f z * g z) \<longlongrightarrow> 0) F"
  3167 proof -
  3168   have *: "((\<lambda>x. norm (f x) * B) \<longlongrightarrow> 0) F"
  3169     by (simp add: f tendsto_mult_left_zero tendsto_norm_zero)
  3170   have "((\<lambda>x. norm (f x) * norm (g x)) \<longlongrightarrow> 0) F"
  3171     apply (rule Lim_null_comparison [OF _ *])
  3172     apply (simp add: eventually_mono [OF g] mult_left_mono)
  3173     done
  3174   then show ?thesis
  3175     by (subst tendsto_norm_zero_iff [symmetric]) (simp add: norm_mult)
  3176 qed
  3177 
  3178 lemma lim_null_mult_left_bounded:
  3179   fixes f :: "'a \<Rightarrow> 'b::real_normed_div_algebra"
  3180   assumes g: "eventually (\<lambda>x. norm(g x) \<le> B) F" and f: "(f \<longlongrightarrow> 0) F"
  3181     shows "((\<lambda>z. g z * f z) \<longlongrightarrow> 0) F"
  3182 proof -
  3183   have *: "((\<lambda>x. B * norm (f x)) \<longlongrightarrow> 0) F"
  3184     by (simp add: f tendsto_mult_right_zero tendsto_norm_zero)
  3185   have "((\<lambda>x. norm (g x) * norm (f x)) \<longlongrightarrow> 0) F"
  3186     apply (rule Lim_null_comparison [OF _ *])
  3187     apply (simp add: eventually_mono [OF g] mult_right_mono)
  3188     done
  3189   then show ?thesis
  3190     by (subst tendsto_norm_zero_iff [symmetric]) (simp add: norm_mult)
  3191 qed
  3192 
  3193 lemma lim_null_scaleR_bounded:
  3194   assumes f: "(f \<longlongrightarrow> 0) net" and gB: "eventually (\<lambda>a. f a = 0 \<or> norm(g a) \<le> B) net"
  3195     shows "((\<lambda>n. f n *\<^sub>R g n) \<longlongrightarrow> 0) net"
  3196 proof
  3197   fix \<epsilon>::real
  3198   assume "0 < \<epsilon>"
  3199   then have B: "0 < \<epsilon> / (abs B + 1)" by simp
  3200   have *: "\<bar>f x\<bar> * norm (g x) < \<epsilon>" if f: "\<bar>f x\<bar> * (\<bar>B\<bar> + 1) < \<epsilon>" and g: "norm (g x) \<le> B" for x
  3201   proof -
  3202     have "\<bar>f x\<bar> * norm (g x) \<le> \<bar>f x\<bar> * B"
  3203       by (simp add: mult_left_mono g)
  3204     also have "... \<le> \<bar>f x\<bar> * (\<bar>B\<bar> + 1)"
  3205       by (simp add: mult_left_mono)
  3206     also have "... < \<epsilon>"
  3207       by (rule f)
  3208     finally show ?thesis .
  3209   qed
  3210   show "\<forall>\<^sub>F x in net. dist (f x *\<^sub>R g x) 0 < \<epsilon>"
  3211     apply (rule eventually_mono [OF eventually_conj [OF tendstoD [OF f B] gB] ])
  3212     apply (auto simp: \<open>0 < \<epsilon>\<close> divide_simps * split: if_split_asm)
  3213     done
  3214 qed
  3215 
  3216 text\<open>Deducing things about the limit from the elements.\<close>
  3217 
  3218 lemma Lim_in_closed_set:
  3219   assumes "closed S"
  3220     and "eventually (\<lambda>x. f(x) \<in> S) net"
  3221     and "\<not> trivial_limit net" "(f \<longlongrightarrow> l) net"
  3222   shows "l \<in> S"
  3223 proof (rule ccontr)
  3224   assume "l \<notin> S"
  3225   with \<open>closed S\<close> have "open (- S)" "l \<in> - S"
  3226     by (simp_all add: open_Compl)
  3227   with assms(4) have "eventually (\<lambda>x. f x \<in> - S) net"
  3228     by (rule topological_tendstoD)
  3229   with assms(2) have "eventually (\<lambda>x. False) net"
  3230     by (rule eventually_elim2) simp
  3231   with assms(3) show "False"
  3232     by (simp add: eventually_False)
  3233 qed
  3234 
  3235 text\<open>Need to prove closed(cball(x,e)) before deducing this as a corollary.\<close>
  3236 
  3237 lemma Lim_dist_ubound:
  3238   assumes "\<not>(trivial_limit net)"
  3239     and "(f \<longlongrightarrow> l) net"
  3240     and "eventually (\<lambda>x. dist a (f x) \<le> e) net"
  3241   shows "dist a l \<le> e"
  3242   using assms by (fast intro: tendsto_le tendsto_intros)
  3243 
  3244 lemma Lim_norm_ubound:
  3245   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  3246   assumes "\<not>(trivial_limit net)" "(f \<longlongrightarrow> l) net" "eventually (\<lambda>x. norm(f x) \<le> e) net"
  3247   shows "norm(l) \<le> e"
  3248   using assms by (fast intro: tendsto_le tendsto_intros)
  3249 
  3250 lemma Lim_norm_lbound:
  3251   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  3252   assumes "\<not> trivial_limit net"
  3253     and "(f \<longlongrightarrow> l) net"
  3254     and "eventually (\<lambda>x. e \<le> norm (f x)) net"
  3255   shows "e \<le> norm l"
  3256   using assms by (fast intro: tendsto_le tendsto_intros)
  3257 
  3258 text\<open>Limit under bilinear function\<close>
  3259 
  3260 lemma Lim_bilinear:
  3261   assumes "(f \<longlongrightarrow> l) net"
  3262     and "(g \<longlongrightarrow> m) net"
  3263     and "bounded_bilinear h"
  3264   shows "((\<lambda>x. h (f x) (g x)) \<longlongrightarrow> (h l m)) net"
  3265   using \<open>bounded_bilinear h\<close> \<open>(f \<longlongrightarrow> l) net\<close> \<open>(g \<longlongrightarrow> m) net\<close>
  3266   by (rule bounded_bilinear.tendsto)
  3267 
  3268 text\<open>These are special for limits out of the same vector space.\<close>
  3269 
  3270 lemma Lim_within_id: "(id \<longlongrightarrow> a) (at a within s)"
  3271   unfolding id_def by (rule tendsto_ident_at)
  3272 
  3273 lemma Lim_at_id: "(id \<longlongrightarrow> a) (at a)"
  3274   unfolding id_def by (rule tendsto_ident_at)
  3275 
  3276 lemma Lim_at_zero:
  3277   fixes a :: "'a::real_normed_vector"
  3278     and l :: "'b::topological_space"
  3279   shows "(f \<longlongrightarrow> l) (at a) \<longleftrightarrow> ((\<lambda>x. f(a + x)) \<longlongrightarrow> l) (at 0)"
  3280   using LIM_offset_zero LIM_offset_zero_cancel ..
  3281 
  3282 text\<open>It's also sometimes useful to extract the limit point from the filter.\<close>
  3283 
  3284 abbreviation netlimit :: "'a::t2_space filter \<Rightarrow> 'a"
  3285   where "netlimit F \<equiv> Lim F (\<lambda>x. x)"
  3286 
  3287 lemma netlimit_within: "\<not> trivial_limit (at a within S) \<Longrightarrow> netlimit (at a within S) = a"
  3288   by (rule tendsto_Lim) (auto intro: tendsto_intros)
  3289 
  3290 lemma netlimit_at:
  3291   fixes a :: "'a::{perfect_space,t2_space}"
  3292   shows "netlimit (at a) = a"
  3293   using netlimit_within [of a UNIV] by simp
  3294 
  3295 lemma lim_within_interior:
  3296   "x \<in> interior S \<Longrightarrow> (f \<longlongrightarrow> l) (at x within S) \<longleftrightarrow> (f \<longlongrightarrow> l) (at x)"
  3297   by (metis at_within_interior)
  3298 
  3299 lemma netlimit_within_interior:
  3300   fixes x :: "'a::{t2_space,perfect_space}"
  3301   assumes "x \<in> interior S"
  3302   shows "netlimit (at x within S) = x"
  3303   using assms by (metis at_within_interior netlimit_at)
  3304 
  3305 lemma netlimit_at_vector:
  3306   fixes a :: "'a::real_normed_vector"
  3307   shows "netlimit (at a) = a"
  3308 proof (cases "\<exists>x. x \<noteq> a")
  3309   case True then obtain x where x: "x \<noteq> a" ..
  3310   have "\<not> trivial_limit (at a)"
  3311     unfolding trivial_limit_def eventually_at dist_norm
  3312     apply clarsimp
  3313     apply (rule_tac x="a + scaleR (d / 2) (sgn (x - a))" in exI)
  3314     apply (simp add: norm_sgn sgn_zero_iff x)
  3315     done
  3316   then show ?thesis
  3317     by (rule netlimit_within [of a UNIV])
  3318 qed simp
  3319 
  3320 
  3321 text\<open>Useful lemmas on closure and set of possible sequential limits.\<close>
  3322 
  3323 lemma closure_sequential:
  3324   fixes l :: "'a::first_countable_topology"
  3325   shows "l \<in> closure S \<longleftrightarrow> (\<exists>x. (\<forall>n. x n \<in> S) \<and> (x \<longlongrightarrow> l) sequentially)"
  3326   (is "?lhs = ?rhs")
  3327 proof
  3328   assume "?lhs"
  3329   moreover
  3330   {
  3331     assume "l \<in> S"
  3332     then have "?rhs" using tendsto_const[of l sequentially] by auto
  3333   }
  3334   moreover
  3335   {
  3336     assume "l islimpt S"
  3337     then have "?rhs" unfolding islimpt_sequential by auto
  3338   }
  3339   ultimately show "?rhs"
  3340     unfolding closure_def by auto
  3341 next
  3342   assume "?rhs"
  3343   then show "?lhs" unfolding closure_def islimpt_sequential by auto
  3344 qed
  3345 
  3346 lemma closed_sequential_limits:
  3347   fixes S :: "'a::first_countable_topology set"
  3348   shows "closed S \<longleftrightarrow> (\<forall>x l. (\<forall>n. x n \<in> S) \<and> (x \<longlongrightarrow> l) sequentially \<longrightarrow> l \<in> S)"
  3349 by (metis closure_sequential closure_subset_eq subset_iff)
  3350 
  3351 lemma closure_approachable:
  3352   fixes S :: "'a::metric_space set"
  3353   shows "x \<in> closure S \<longleftrightarrow> (\<forall>e>0. \<exists>y\<in>S. dist y x < e)"
  3354   apply (auto simp add: closure_def islimpt_approachable)
  3355   apply (metis dist_self)
  3356   done
  3357 
  3358 lemma closed_approachable:
  3359   fixes S :: "'a::metric_space set"
  3360   shows "closed S \<Longrightarrow> (\<forall>e>0. \<exists>y\<in>S. dist y x < e) \<longleftrightarrow> x \<in> S"
  3361   by (metis closure_closed closure_approachable)
  3362 
  3363 lemma closure_contains_Inf:
  3364   fixes S :: "real set"
  3365   assumes "S \<noteq> {}" "bdd_below S"
  3366   shows "Inf S \<in> closure S"
  3367 proof -
  3368   have *: "\<forall>x\<in>S. Inf S \<le> x"
  3369     using cInf_lower[of _ S] assms by metis
  3370   {
  3371     fix e :: real
  3372     assume "e > 0"
  3373     then have "Inf S < Inf S + e" by simp
  3374     with assms obtain x where "x \<in> S" "x < Inf S + e"
  3375       by (subst (asm) cInf_less_iff) auto
  3376     with * have "\<exists>x\<in>S. dist x (Inf S) < e"
  3377       by (intro bexI[of _ x]) (auto simp add: dist_real_def)
  3378   }
  3379   then show ?thesis unfolding closure_approachable by auto
  3380 qed
  3381 
  3382 lemma closed_contains_Inf:
  3383   fixes S :: "real set"
  3384   shows "S \<noteq> {} \<Longrightarrow> bdd_below S \<Longrightarrow> closed S \<Longrightarrow> Inf S \<in> S"
  3385   by (metis closure_contains_Inf closure_closed)
  3386 
  3387 lemma closed_subset_contains_Inf:
  3388   fixes A C :: "real set"
  3389   shows "closed C \<Longrightarrow> A \<subseteq> C \<Longrightarrow> A \<noteq> {} \<Longrightarrow> bdd_below A \<Longrightarrow> Inf A \<in> C"
  3390   by (metis closure_contains_Inf closure_minimal subset_eq)
  3391 
  3392 lemma atLeastAtMost_subset_contains_Inf:
  3393   fixes A :: "real set" and a b :: real
  3394   shows "A \<noteq> {} \<Longrightarrow> a \<le> b \<Longrightarrow> A \<subseteq> {a..b} \<Longrightarrow> Inf A \<in> {a..b}"
  3395   by (rule closed_subset_contains_Inf)
  3396      (auto intro: closed_real_atLeastAtMost intro!: bdd_belowI[of A a])
  3397 
  3398 lemma not_trivial_limit_within_ball:
  3399   "\<not> trivial_limit (at x within S) \<longleftrightarrow> (\<forall>e>0. S \<inter> ball x e - {x} \<noteq> {})"
  3400   (is "?lhs \<longleftrightarrow> ?rhs")
  3401 proof
  3402   show ?rhs if ?lhs
  3403   proof -
  3404     {
  3405       fix e :: real
  3406       assume "e > 0"
  3407       then obtain y where "y \<in> S - {x}" and "dist y x < e"
  3408         using \<open>?lhs\<close> not_trivial_limit_within[of x S] closure_approachable[of x "S - {x}"]
  3409         by auto
  3410       then have "y \<in> S \<inter> ball x e - {x}"
  3411         unfolding ball_def by (simp add: dist_commute)
  3412       then have "S \<inter> ball x e - {x} \<noteq> {}" by blast
  3413     }
  3414     then show ?thesis by auto
  3415   qed
  3416   show ?lhs if ?rhs
  3417   proof -
  3418     {
  3419       fix e :: real
  3420       assume "e > 0"
  3421       then obtain y where "y \<in> S \<inter> ball x e - {x}"
  3422         using \<open>?rhs\<close> by blast
  3423       then have "y \<in> S - {x}" and "dist y x < e"
  3424         unfolding ball_def by (simp_all add: dist_commute)
  3425       then have "\<exists>y \<in> S - {x}. dist y x < e"
  3426         by auto
  3427     }
  3428     then show ?thesis
  3429       using not_trivial_limit_within[of x S] closure_approachable[of x "S - {x}"]
  3430       by auto
  3431   qed
  3432 qed
  3433 
  3434 
  3435 subsection \<open>Infimum Distance\<close>
  3436 
  3437 definition "infdist x A = (if A = {} then 0 else INF a:A. dist x a)"
  3438 
  3439 lemma bdd_below_infdist[intro, simp]: "bdd_below (dist x`A)"
  3440   by (auto intro!: zero_le_dist)
  3441 
  3442 lemma infdist_notempty: "A \<noteq> {} \<Longrightarrow> infdist x A = (INF a:A. dist x a)"
  3443   by (simp add: infdist_def)
  3444 
  3445 lemma infdist_nonneg: "0 \<le> infdist x A"
  3446   by (auto simp add: infdist_def intro: cINF_greatest)
  3447 
  3448 lemma infdist_le: "a \<in> A \<Longrightarrow> infdist x A \<le> dist x a"
  3449   by (auto intro: cINF_lower simp add: infdist_def)
  3450 
  3451 lemma infdist_le2: "a \<in> A \<Longrightarrow> dist x a \<le> d \<Longrightarrow> infdist x A \<le> d"
  3452   by (auto intro!: cINF_lower2 simp add: infdist_def)
  3453 
  3454 lemma infdist_zero[simp]: "a \<in> A \<Longrightarrow> infdist a A = 0"
  3455   by (auto intro!: antisym infdist_nonneg infdist_le2)
  3456 
  3457 lemma infdist_triangle: "infdist x A \<le> infdist y A + dist x y"
  3458 proof (cases "A = {}")
  3459   case True
  3460   then show ?thesis by (simp add: infdist_def)
  3461 next
  3462   case False
  3463   then obtain a where "a \<in> A" by auto
  3464   have "infdist x A \<le> Inf {dist x y + dist y a |a. a \<in> A}"
  3465   proof (rule cInf_greatest)
  3466     from \<open>A \<noteq> {}\<close> show "{dist x y + dist y a |a. a \<in> A} \<noteq> {}"
  3467       by simp
  3468     fix d
  3469     assume "d \<in> {dist x y + dist y a |a. a \<in> A}"
  3470     then obtain a where d: "d = dist x y + dist y a" "a \<in> A"
  3471       by auto
  3472     show "infdist x A \<le> d"
  3473       unfolding infdist_notempty[OF \<open>A \<noteq> {}\<close>]
  3474     proof (rule cINF_lower2)
  3475       show "a \<in> A" by fact
  3476       show "dist x a \<le> d"
  3477         unfolding d by (rule dist_triangle)
  3478     qed simp
  3479   qed
  3480   also have "\<dots> = dist x y + infdist y A"
  3481   proof (rule cInf_eq, safe)
  3482     fix a
  3483     assume "a \<in> A"
  3484     then show "dist x y + infdist y A \<le> dist x y + dist y a"
  3485       by (auto intro: infdist_le)
  3486   next
  3487     fix i
  3488     assume inf: "\<And>d. d \<in> {dist x y + dist y a |a. a \<in> A} \<Longrightarrow> i \<le> d"
  3489     then have "i - dist x y \<le> infdist y A"
  3490       unfolding infdist_notempty[OF \<open>A \<noteq> {}\<close>] using \<open>a \<in> A\<close>
  3491       by (intro cINF_greatest) (auto simp: field_simps)
  3492     then show "i \<le> dist x y + infdist y A"
  3493       by simp
  3494   qed
  3495   finally show ?thesis by simp
  3496 qed
  3497 
  3498 lemma in_closure_iff_infdist_zero:
  3499   assumes "A \<noteq> {}"
  3500   shows "x \<in> closure A \<longleftrightarrow> infdist x A = 0"
  3501 proof
  3502   assume "x \<in> closure A"
  3503   show "infdist x A = 0"
  3504   proof (rule ccontr)
  3505     assume "infdist x A \<noteq> 0"
  3506     with infdist_nonneg[of x A] have "infdist x A > 0"
  3507       by auto
  3508     then have "ball x (infdist x A) \<inter> closure A = {}"
  3509       apply auto
  3510       apply (metis \<open>x \<in> closure A\<close> closure_approachable dist_commute infdist_le not_less)
  3511       done
  3512     then have "x \<notin> closure A"
  3513       by (metis \<open>0 < infdist x A\<close> centre_in_ball disjoint_iff_not_equal)
  3514     then show False using \<open>x \<in> closure A\<close> by simp
  3515   qed
  3516 next
  3517   assume x: "infdist x A = 0"
  3518   then obtain a where "a \<in> A"
  3519     by atomize_elim (metis all_not_in_conv assms)
  3520   show "x \<in> closure A"
  3521     unfolding closure_approachable
  3522     apply safe
  3523   proof (rule ccontr)
  3524     fix e :: real
  3525     assume "e > 0"
  3526     assume "\<not> (\<exists>y\<in>A. dist y x < e)"
  3527     then have "infdist x A \<ge> e" using \<open>a \<in> A\<close>
  3528       unfolding infdist_def
  3529       by (force simp: dist_commute intro: cINF_greatest)
  3530     with x \<open>e > 0\<close> show False by auto
  3531   qed
  3532 qed
  3533 
  3534 lemma in_closed_iff_infdist_zero:
  3535   assumes "closed A" "A \<noteq> {}"
  3536   shows "x \<in> A \<longleftrightarrow> infdist x A = 0"
  3537 proof -
  3538   have "x \<in> closure A \<longleftrightarrow> infdist x A = 0"
  3539     by (rule in_closure_iff_infdist_zero) fact
  3540   with assms show ?thesis by simp
  3541 qed
  3542 
  3543 lemma tendsto_infdist [tendsto_intros]:
  3544   assumes f: "(f \<longlongrightarrow> l) F"
  3545   shows "((\<lambda>x. infdist (f x) A) \<longlongrightarrow> infdist l A) F"
  3546 proof (rule tendstoI)
  3547   fix e ::real
  3548   assume "e > 0"
  3549   from tendstoD[OF f this]
  3550   show "eventually (\<lambda>x. dist (infdist (f x) A) (infdist l A) < e) F"
  3551   proof (eventually_elim)
  3552     fix x
  3553     from infdist_triangle[of l A "f x"] infdist_triangle[of "f x" A l]
  3554     have "dist (infdist (f x) A) (infdist l A) \<le> dist (f x) l"
  3555       by (simp add: dist_commute dist_real_def)
  3556     also assume "dist (f x) l < e"
  3557     finally show "dist (infdist (f x) A) (infdist l A) < e" .
  3558   qed
  3559 qed
  3560 
  3561 text\<open>Some other lemmas about sequences.\<close>
  3562 
  3563 lemma sequentially_offset: (* TODO: move to Topological_Spaces.thy *)
  3564   assumes "eventually (\<lambda>i. P i) sequentially"
  3565   shows "eventually (\<lambda>i. P (i + k)) sequentially"
  3566   using assms by (rule eventually_sequentially_seg [THEN iffD2])
  3567 
  3568 lemma seq_offset_neg: (* TODO: move to Topological_Spaces.thy *)
  3569   "(f \<longlongrightarrow> l) sequentially \<Longrightarrow> ((\<lambda>i. f(i - k)) \<longlongrightarrow> l) sequentially"
  3570   apply (erule filterlim_compose)
  3571   apply (simp add: filterlim_def le_sequentially eventually_filtermap eventually_sequentially)
  3572   apply arith
  3573   done
  3574 
  3575 lemma seq_harmonic: "((\<lambda>n. inverse (real n)) \<longlongrightarrow> 0) sequentially"
  3576   using LIMSEQ_inverse_real_of_nat by (rule LIMSEQ_imp_Suc) (* TODO: move to Limits.thy *)
  3577 
  3578 subsection \<open>More properties of closed balls\<close>
  3579 
  3580 lemma closed_cball [iff]: "closed (cball x e)"
  3581 proof -
  3582   have "closed (dist x -` {..e})"
  3583     by (intro closed_vimage closed_atMost continuous_intros)
  3584   also have "dist x -` {..e} = cball x e"
  3585     by auto
  3586   finally show ?thesis .
  3587 qed
  3588 
  3589 lemma open_contains_cball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0.  cball x e \<subseteq> S)"
  3590 proof -
  3591   {
  3592     fix x and e::real
  3593     assume "x\<in>S" "e>0" "ball x e \<subseteq> S"
  3594     then have "\<exists>d>0. cball x d \<subseteq> S" unfolding subset_eq by (rule_tac x="e/2" in exI, auto)
  3595   }
  3596   moreover
  3597   {
  3598     fix x and e::real
  3599     assume "x\<in>S" "e>0" "cball x e \<subseteq> S"
  3600     then have "\<exists>d>0. ball x d \<subseteq> S"
  3601       unfolding subset_eq
  3602       apply(rule_tac x="e/2" in exI)
  3603       apply auto
  3604       done
  3605   }
  3606   ultimately show ?thesis
  3607     unfolding open_contains_ball by auto
  3608 qed
  3609 
  3610 lemma open_contains_cball_eq: "open S \<Longrightarrow> (\<forall>x. x \<in> S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S))"
  3611   by (metis open_contains_cball subset_eq order_less_imp_le centre_in_cball)
  3612 
  3613 lemma mem_interior_cball: "x \<in> interior S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S)"
  3614   apply (simp add: interior_def, safe)
  3615   apply (force simp add: open_contains_cball)
  3616   apply (rule_tac x="ball x e" in exI)
  3617   apply (simp add: subset_trans [OF ball_subset_cball])
  3618   done
  3619 
  3620 lemma islimpt_ball:
  3621   fixes x y :: "'a::{real_normed_vector,perfect_space}"
  3622   shows "y islimpt ball x e \<longleftrightarrow> 0 < e \<and> y \<in> cball x e"
  3623   (is "?lhs \<longleftrightarrow> ?rhs")
  3624 proof
  3625   show ?rhs if ?lhs
  3626   proof
  3627     {
  3628       assume "e \<le> 0"
  3629       then have *: "ball x e = {}"
  3630         using ball_eq_empty[of x e] by auto
  3631       have False using \<open>?lhs\<close>
  3632         unfolding * using islimpt_EMPTY[of y] by auto
  3633     }
  3634     then show "e > 0" by (metis not_less)
  3635     show "y \<in> cball x e"
  3636       using closed_cball[of x e] islimpt_subset[of y "ball x e" "cball x e"]
  3637         ball_subset_cball[of x e] \<open>?lhs\<close>
  3638       unfolding closed_limpt by auto
  3639   qed
  3640   show ?lhs if ?rhs
  3641   proof -
  3642     from that have "e > 0" by auto
  3643     {
  3644       fix d :: real
  3645       assume "d > 0"
  3646       have "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
  3647       proof (cases "d \<le> dist x y")
  3648         case True
  3649         then show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
  3650         proof (cases "x = y")
  3651           case True
  3652           then have False
  3653             using \<open>d \<le> dist x y\<close> \<open>d>0\<close> by auto
  3654           then show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
  3655             by auto
  3656         next
  3657           case False
  3658           have "dist x (y - (d / (2 * dist y x)) *\<^sub>R (y - x)) =
  3659             norm (x - y + (d / (2 * norm (y - x))) *\<^sub>R (y - x))"
  3660             unfolding mem_cball mem_ball dist_norm diff_diff_eq2 diff_add_eq[symmetric]
  3661             by auto
  3662           also have "\<dots> = \<bar>- 1 + d / (2 * norm (x - y))\<bar> * norm (x - y)"
  3663             using scaleR_left_distrib[of "- 1" "d / (2 * norm (y - x))", symmetric, of "y - x"]
  3664             unfolding scaleR_minus_left scaleR_one
  3665             by (auto simp add: norm_minus_commute)
  3666           also have "\<dots> = \<bar>- norm (x - y) + d / 2\<bar>"
  3667             unfolding abs_mult_pos[of "norm (x - y)", OF norm_ge_zero[of "x - y"]]
  3668             unfolding distrib_right using \<open>x\<noteq>y\<close>  by auto
  3669           also have "\<dots> \<le> e - d/2" using \<open>d \<le> dist x y\<close> and \<open>d>0\<close> and \<open>?rhs\<close>
  3670             by (auto simp add: dist_norm)
  3671           finally have "y - (d / (2 * dist y x)) *\<^sub>R (y - x) \<in> ball x e" using \<open>d>0\<close>
  3672             by auto
  3673           moreover
  3674           have "(d / (2*dist y x)) *\<^sub>R (y - x) \<noteq> 0"
  3675             using \<open>x\<noteq>y\<close>[unfolded dist_nz] \<open>d>0\<close> unfolding scaleR_eq_0_iff
  3676             by (auto simp add: dist_commute)
  3677           moreover
  3678           have "dist (y - (d / (2 * dist y x)) *\<^sub>R (y - x)) y < d"
  3679             unfolding dist_norm
  3680             apply simp
  3681             unfolding norm_minus_cancel
  3682             using \<open>d > 0\<close> \<open>x\<noteq>y\<close>[unfolded dist_nz] dist_commute[of x y]
  3683             unfolding dist_norm
  3684             apply auto
  3685             done
  3686           ultimately show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
  3687             apply (rule_tac x = "y - (d / (2*dist y x)) *\<^sub>R (y - x)" in bexI)
  3688             apply auto
  3689             done
  3690         qed
  3691       next
  3692         case False
  3693         then have "d > dist x y" by auto
  3694         show "\<exists>x' \<in> ball x e. x' \<noteq> y \<and> dist x' y < d"
  3695         proof (cases "x = y")
  3696           case True
  3697           obtain z where **: "z \<noteq> y" "dist z y < min e d"
  3698             using perfect_choose_dist[of "min e d" y]
  3699             using \<open>d > 0\<close> \<open>e>0\<close> by auto
  3700           show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
  3701             unfolding \<open>x = y\<close>
  3702             using \<open>z \<noteq> y\<close> **
  3703             apply (rule_tac x=z in bexI)
  3704             apply (auto simp add: dist_commute)
  3705             done
  3706         next
  3707           case False
  3708           then show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
  3709             using \<open>d>0\<close> \<open>d > dist x y\<close> \<open>?rhs\<close>
  3710             apply (rule_tac x=x in bexI)
  3711             apply auto
  3712             done
  3713         qed
  3714       qed
  3715     }
  3716     then show ?thesis
  3717       unfolding mem_cball islimpt_approachable mem_ball by auto
  3718   qed
  3719 qed
  3720 
  3721 lemma closure_ball_lemma:
  3722   fixes x y :: "'a::real_normed_vector"
  3723   assumes "x \<noteq> y"
  3724   shows "y islimpt ball x (dist x y)"
  3725 proof (rule islimptI)
  3726   fix T
  3727   assume "y \<in> T" "open T"
  3728   then obtain r where "0 < r" "\<forall>z. dist z y < r \<longrightarrow> z \<in> T"
  3729     unfolding open_dist by fast
  3730   (* choose point between x and y, within distance r of y. *)
  3731   define k where "k = min 1 (r / (2 * dist x y))"
  3732   define z where "z = y + scaleR k (x - y)"
  3733   have z_def2: "z = x + scaleR (1 - k) (y - x)"
  3734     unfolding z_def by (simp add: algebra_simps)
  3735   have "dist z y < r"
  3736     unfolding z_def k_def using \<open>0 < r\<close>
  3737     by (simp add: dist_norm min_def)
  3738   then have "z \<in> T"
  3739     using \<open>\<forall>z. dist z y < r \<longrightarrow> z \<in> T\<close> by simp
  3740   have "dist x z < dist x y"
  3741     unfolding z_def2 dist_norm
  3742     apply (simp add: norm_minus_commute)
  3743     apply (simp only: dist_norm [symmetric])
  3744     apply (subgoal_tac "\<bar>1 - k\<bar> * dist x y < 1 * dist x y", simp)
  3745     apply (rule mult_strict_right_mono)
  3746     apply (simp add: k_def \<open>0 < r\<close> \<open>x \<noteq> y\<close>)
  3747     apply (simp add: \<open>x \<noteq> y\<close>)
  3748     done
  3749   then have "z \<in> ball x (dist x y)"
  3750     by simp
  3751   have "z \<noteq> y"
  3752     unfolding z_def k_def using \<open>x \<noteq> y\<close> \<open>0 < r\<close>
  3753     by (simp add: min_def)
  3754   show "\<exists>z\<in>ball x (dist x y). z \<in> T \<and> z \<noteq> y"
  3755     using \<open>z \<in> ball x (dist x y)\<close> \<open>z \<in> T\<close> \<open>z \<noteq> y\<close>
  3756     by fast
  3757 qed
  3758 
  3759 lemma closure_ball [simp]:
  3760   fixes x :: "'a::real_normed_vector"
  3761   shows "0 < e \<Longrightarrow> closure (ball x e) = cball x e"
  3762   apply (rule equalityI)
  3763   apply (rule closure_minimal)
  3764   apply (rule ball_subset_cball)
  3765   apply (rule closed_cball)
  3766   apply (rule subsetI, rename_tac y)
  3767   apply (simp add: le_less [where 'a=real])
  3768   apply (erule disjE)
  3769   apply (rule subsetD [OF closure_subset], simp)
  3770   apply (simp add: closure_def)
  3771   apply clarify
  3772   apply (rule closure_ball_lemma)
  3773   apply (simp add: zero_less_dist_iff)
  3774   done
  3775 
  3776 (* In a trivial vector space, this fails for e = 0. *)
  3777 lemma interior_cball [simp]:
  3778   fixes x :: "'a::{real_normed_vector, perfect_space}"
  3779   shows "interior (cball x e) = ball x e"
  3780 proof (cases "e \<ge> 0")
  3781   case False note cs = this
  3782   from cs have null: "ball x e = {}"
  3783     using ball_empty[of e x] by auto
  3784   moreover
  3785   {
  3786     fix y
  3787     assume "y \<in> cball x e"
  3788     then have False
  3789       by (metis ball_eq_empty null cs dist_eq_0_iff dist_le_zero_iff empty_subsetI mem_cball subset_antisym subset_ball)
  3790   }
  3791   then have "cball x e = {}" by auto
  3792   then have "interior (cball x e) = {}"
  3793     using interior_empty by auto
  3794   ultimately show ?thesis by blast
  3795 next
  3796   case True note cs = this
  3797   have "ball x e \<subseteq> cball x e"
  3798     using ball_subset_cball by auto
  3799   moreover
  3800   {
  3801     fix S y
  3802     assume as: "S \<subseteq> cball x e" "open S" "y\<in>S"
  3803     then obtain d where "d>0" and d: "\<forall>x'. dist x' y < d \<longrightarrow> x' \<in> S"
  3804       unfolding open_dist by blast
  3805     then obtain xa where xa_y: "xa \<noteq> y" and xa: "dist xa y < d"
  3806       using perfect_choose_dist [of d] by auto
  3807     have "xa \<in> S"
  3808       using d[THEN spec[where x = xa]]
  3809       using xa by (auto simp add: dist_commute)
  3810     then have xa_cball: "xa \<in> cball x e"
  3811       using as(1) by auto
  3812     then have "y \<in> ball x e"
  3813     proof (cases "x = y")
  3814       case True
  3815       then have "e > 0" using cs order.order_iff_strict xa_cball xa_y by fastforce
  3816       then show "y \<in> ball x e"
  3817         using \<open>x = y \<close> by simp
  3818     next
  3819       case False
  3820       have "dist (y + (d / 2 / dist y x) *\<^sub>R (y - x)) y < d"
  3821         unfolding dist_norm
  3822         using \<open>d>0\<close> norm_ge_zero[of "y - x"] \<open>x \<noteq> y\<close> by auto
  3823       then have *: "y + (d / 2 / dist y x) *\<^sub>R (y - x) \<in> cball x e"
  3824         using d as(1)[unfolded subset_eq] by blast
  3825       have "y - x \<noteq> 0" using \<open>x \<noteq> y\<close> by auto
  3826       hence **:"d / (2 * norm (y - x)) > 0"
  3827         unfolding zero_less_norm_iff[symmetric] using \<open>d>0\<close> by auto
  3828       have "dist (y + (d / 2 / dist y x) *\<^sub>R (y - x)) x =
  3829         norm (y + (d / (2 * norm (y - x))) *\<^sub>R y - (d / (2 * norm (y - x))) *\<^sub>R x - x)"
  3830         by (auto simp add: dist_norm algebra_simps)
  3831       also have "\<dots> = norm ((1 + d / (2 * norm (y - x))) *\<^sub>R (y - x))"
  3832         by (auto simp add: algebra_simps)
  3833       also have "\<dots> = \<bar>1 + d / (2 * norm (y - x))\<bar> * norm (y - x)"
  3834         using ** by auto
  3835       also have "\<dots> = (dist y x) + d/2"
  3836         using ** by (auto simp add: distrib_right dist_norm)
  3837       finally have "e \<ge> dist x y +d/2"
  3838         using *[unfolded mem_cball] by (auto simp add: dist_commute)
  3839       then show "y \<in> ball x e"
  3840         unfolding mem_ball using \<open>d>0\<close> by auto
  3841     qed
  3842   }
  3843   then have "\<forall>S \<subseteq> cball x e. open S \<longrightarrow> S \<subseteq> ball x e"
  3844     by auto
  3845   ultimately show ?thesis
  3846     using interior_unique[of "ball x e" "cball x e"]
  3847     using open_ball[of x e]
  3848     by auto
  3849 qed
  3850 
  3851 lemma interior_ball [simp]: "interior (ball x e) = ball x e"
  3852   by (simp add: interior_open)
  3853 
  3854 lemma frontier_ball [simp]:
  3855   fixes a :: "'a::real_normed_vector"
  3856   shows "0 < e \<Longrightarrow> frontier (ball a e) = sphere a e"
  3857   by (force simp: frontier_def)
  3858 
  3859 lemma frontier_cball [simp]:
  3860   fixes a :: "'a::{real_normed_vector, perfect_space}"
  3861   shows "frontier (cball a e) = sphere a e"
  3862   by (force simp: frontier_def)
  3863 
  3864 lemma cball_eq_empty [simp]: "cball x e = {} \<longleftrightarrow> e < 0"
  3865   apply (simp add: set_eq_iff not_le)
  3866   apply (metis zero_le_dist dist_self order_less_le_trans)
  3867   done
  3868 
  3869 lemma cball_empty [simp]: "e < 0 \<Longrightarrow> cball x e = {}"
  3870   by (simp add: cball_eq_empty)
  3871 
  3872 lemma cball_eq_sing:
  3873   fixes x :: "'a::{metric_space,perfect_space}"
  3874   shows "cball x e = {x} \<longleftrightarrow> e = 0"
  3875 proof (rule linorder_cases)
  3876   assume e: "0 < e"
  3877   obtain a where "a \<noteq> x" "dist a x < e"
  3878     using perfect_choose_dist [OF e] by auto
  3879   then have "a \<noteq> x" "dist x a \<le> e"
  3880     by (auto simp add: dist_commute)
  3881   with e show ?thesis by (auto simp add: set_eq_iff)
  3882 qed auto
  3883 
  3884 lemma cball_sing:
  3885   fixes x :: "'a::metric_space"
  3886   shows "e = 0 \<Longrightarrow> cball x e = {x}"
  3887   by (auto simp add: set_eq_iff)
  3888 
  3889 lemma ball_divide_subset: "d \<ge> 1 \<Longrightarrow> ball x (e/d) \<subseteq> ball x e"
  3890   apply (cases "e \<le> 0")
  3891   apply (simp add: ball_empty divide_simps)
  3892   apply (rule subset_ball)
  3893   apply (simp add: divide_simps)
  3894   done
  3895 
  3896 lemma ball_divide_subset_numeral: "ball x (e / numeral w) \<subseteq> ball x e"
  3897   using ball_divide_subset one_le_numeral by blast
  3898 
  3899 lemma cball_divide_subset: "d \<ge> 1 \<Longrightarrow> cball x (e/d) \<subseteq> cball x e"
  3900   apply (cases "e < 0")
  3901   apply (simp add: divide_simps)
  3902   apply (rule subset_cball)
  3903   apply (metis div_by_1 frac_le not_le order_refl zero_less_one)
  3904   done
  3905 
  3906 lemma cball_divide_subset_numeral: "cball x (e / numeral w) \<subseteq> cball x e"
  3907   using cball_divide_subset one_le_numeral by blast
  3908 
  3909 
  3910 subsection \<open>Boundedness\<close>
  3911 
  3912   (* FIXME: This has to be unified with BSEQ!! *)
  3913 definition (in metric_space) bounded :: "'a set \<Rightarrow> bool"
  3914   where "bounded S \<longleftrightarrow> (\<exists>x e. \<forall>y\<in>S. dist x y \<le> e)"
  3915 
  3916 lemma bounded_subset_cball: "bounded S \<longleftrightarrow> (\<exists>e x. S \<subseteq> cball x e \<and> 0 \<le> e)"
  3917   unfolding bounded_def subset_eq  by auto (meson order_trans zero_le_dist)
  3918 
  3919 lemma bounded_subset_ballD:
  3920   assumes "bounded S" shows "\<exists>r. 0 < r \<and> S \<subseteq> ball x r"
  3921 proof -
  3922   obtain e::real and y where "S \<subseteq> cball y e"  "0 \<le> e"
  3923     using assms by (auto simp: bounded_subset_cball)
  3924   then show ?thesis
  3925     apply (rule_tac x="dist x y + e + 1" in exI)
  3926     apply (simp add: add.commute add_pos_nonneg)
  3927     apply (erule subset_trans)
  3928     apply (clarsimp simp add: cball_def)
  3929     by (metis add_le_cancel_right add_strict_increasing dist_commute dist_triangle_le zero_less_one)
  3930 qed
  3931 
  3932 lemma bounded_any_center: "bounded S \<longleftrightarrow> (\<exists>e. \<forall>y\<in>S. dist a y \<le> e)"
  3933   unfolding bounded_def
  3934   by auto (metis add.commute add_le_cancel_right dist_commute dist_triangle_le)
  3935 
  3936 lemma bounded_iff: "bounded S \<longleftrightarrow> (\<exists>a. \<forall>x\<in>S. norm x \<le> a)"
  3937   unfolding bounded_any_center [where a=0]
  3938   by (simp add: dist_norm)
  3939 
  3940 lemma bdd_above_norm: "bdd_above (norm ` X) \<longleftrightarrow> bounded X"
  3941   by (simp add: bounded_iff bdd_above_def)
  3942 
  3943 lemma bounded_norm_comp: "bounded ((\<lambda>x. norm (f x)) ` S) = bounded (f ` S)"
  3944   by (simp add: bounded_iff)
  3945 
  3946 lemma boundedI:
  3947   assumes "\<And>x. x \<in> S \<Longrightarrow> norm x \<le> B"
  3948   shows "bounded S"
  3949   using assms bounded_iff by blast
  3950 
  3951 lemma bounded_empty [simp]: "bounded {}"
  3952   by (simp add: bounded_def)
  3953 
  3954 lemma bounded_subset: "bounded T \<Longrightarrow> S \<subseteq> T \<Longrightarrow> bounded S"
  3955   by (metis bounded_def subset_eq)
  3956 
  3957 lemma bounded_interior[intro]: "bounded S \<Longrightarrow> bounded(interior S)"
  3958   by (metis bounded_subset interior_subset)
  3959 
  3960 lemma bounded_closure[intro]:
  3961   assumes "bounded S"
  3962   shows "bounded (closure S)"
  3963 proof -
  3964   from assms obtain x and a where a: "\<forall>y\<in>S. dist x y \<le> a"
  3965     unfolding bounded_def by auto
  3966   {
  3967     fix y
  3968     assume "y \<in> closure S"
  3969     then obtain f where f: "\<forall>n. f n \<in> S"  "(f \<longlongrightarrow> y) sequentially"
  3970       unfolding closure_sequential by auto
  3971     have "\<forall>n. f n \<in> S \<longrightarrow> dist x (f n) \<le> a" using a by simp
  3972     then have "eventually (\<lambda>n. dist x (f n) \<le> a) sequentially"
  3973       by (simp add: f(1))
  3974     have "dist x y \<le> a"
  3975       apply (rule Lim_dist_ubound [of sequentially f])
  3976       apply (rule trivial_limit_sequentially)
  3977       apply (rule f(2))
  3978       apply fact
  3979       done
  3980   }
  3981   then show ?thesis
  3982     unfolding bounded_def by auto
  3983 qed
  3984 
  3985 lemma bounded_closure_image: "bounded (f ` closure S) \<Longrightarrow> bounded (f ` S)"
  3986   by (simp add: bounded_subset closure_subset image_mono)
  3987 
  3988 lemma bounded_cball[simp,intro]: "bounded (cball x e)"
  3989   apply (simp add: bounded_def)
  3990   apply (rule_tac x=x in exI)
  3991   apply (rule_tac x=e in exI)
  3992   apply auto
  3993   done
  3994 
  3995 lemma bounded_ball[simp,intro]: "bounded (ball x e)"
  3996   by (metis ball_subset_cball bounded_cball bounded_subset)
  3997 
  3998 lemma bounded_Un[simp]: "bounded (S \<union> T) \<longleftrightarrow> bounded S \<and> bounded T"
  3999   by (auto simp add: bounded_def) (metis Un_iff bounded_any_center le_max_iff_disj)
  4000 
  4001 lemma bounded_Union[intro]: "finite F \<Longrightarrow> \<forall>S\<in>F. bounded S \<Longrightarrow> bounded (\<Union>F)"
  4002   by (induct rule: finite_induct[of F]) auto
  4003 
  4004 lemma bounded_UN [intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. bounded (B x) \<Longrightarrow> bounded (\<Union>x\<in>A. B x)"
  4005   by (induct set: finite) auto
  4006 
  4007 lemma bounded_insert [simp]: "bounded (insert x S) \<longleftrightarrow> bounded S"
  4008 proof -
  4009   have "\<forall>y\<in>{x}. dist x y \<le> 0"
  4010     by simp
  4011   then have "bounded {x}"
  4012     unfolding bounded_def by fast
  4013   then show ?thesis
  4014     by (metis insert_is_Un bounded_Un)
  4015 qed
  4016 
  4017 lemma finite_imp_bounded [intro]: "finite S \<Longrightarrow> bounded S"
  4018   by (induct set: finite) simp_all
  4019 
  4020 lemma bounded_pos: "bounded S \<longleftrightarrow> (\<exists>b>0. \<forall>x\<in> S. norm x \<le> b)"
  4021   apply (simp add: bounded_iff)
  4022   apply (subgoal_tac "\<And>x (y::real). 0 < 1 + \<bar>y\<bar> \<and> (x \<le> y \<longrightarrow> x \<le> 1 + \<bar>y\<bar>)")
  4023   apply metis
  4024   apply arith
  4025   done
  4026 
  4027 lemma bounded_pos_less: "bounded S \<longleftrightarrow> (\<exists>b>0. \<forall>x\<in> S. norm x < b)"
  4028   apply (simp add: bounded_pos)
  4029   apply (safe; rule_tac x="b+1" in exI; force)
  4030   done
  4031 
  4032 lemma Bseq_eq_bounded:
  4033   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
  4034   shows "Bseq f \<longleftrightarrow> bounded (range f)"
  4035   unfolding Bseq_def bounded_pos by auto
  4036 
  4037 lemma bounded_Int[intro]: "bounded S \<or> bounded T \<Longrightarrow> bounded (S \<inter> T)"
  4038   by (metis Int_lower1 Int_lower2 bounded_subset)
  4039 
  4040 lemma bounded_diff[intro]: "bounded S \<Longrightarrow> bounded (S - T)"
  4041   by (metis Diff_subset bounded_subset)
  4042 
  4043 lemma not_bounded_UNIV[simp]:
  4044   "\<not> bounded (UNIV :: 'a::{real_normed_vector, perfect_space} set)"
  4045 proof (auto simp add: bounded_pos not_le)
  4046   obtain x :: 'a where "x \<noteq> 0"
  4047     using perfect_choose_dist [OF zero_less_one] by fast
  4048   fix b :: real
  4049   assume b: "b >0"
  4050   have b1: "b +1 \<ge> 0"
  4051     using b by simp
  4052   with \<open>x \<noteq> 0\<close> have "b < norm (scaleR (b + 1) (sgn x))"
  4053     by (simp add: norm_sgn)
  4054   then show "\<exists>x::'a. b < norm x" ..
  4055 qed
  4056 
  4057 corollary cobounded_imp_unbounded:
  4058     fixes S :: "'a::{real_normed_vector, perfect_space} set"
  4059     shows "bounded (- S) \<Longrightarrow> ~ (bounded S)"
  4060   using bounded_Un [of S "-S"]  by (simp add: sup_compl_top)
  4061 
  4062 lemma bounded_dist_comp:
  4063   assumes "bounded (f ` S)" "bounded (g ` S)"
  4064   shows "bounded ((\<lambda>x. dist (f x) (g x)) ` S)"
  4065 proof -
  4066   from assms obtain M1 M2 where *: "dist (f x) undefined \<le> M1" "dist undefined (g x) \<le> M2" if "x \<in> S" for x
  4067     by (auto simp: bounded_any_center[of _ undefined] dist_commute)
  4068   have "dist (f x) (g x) \<le> M1 + M2" if "x \<in> S" for x
  4069     using *[OF that]
  4070     by (rule order_trans[OF dist_triangle add_mono])
  4071   then show ?thesis
  4072     by (auto intro!: boundedI)
  4073 qed
  4074 
  4075 lemma bounded_linear_image:
  4076   assumes "bounded S"
  4077     and "bounded_linear f"
  4078   shows "bounded (f ` S)"
  4079 proof -
  4080   from assms(1) obtain b where b: "b > 0" "\<forall>x\<in>S. norm x \<le> b"
  4081     unfolding bounded_pos by auto
  4082   from assms(2) obtain B where B: "B > 0" "\<forall>x. norm (f x) \<le> B * norm x"
  4083     using bounded_linear.pos_bounded by (auto simp add: ac_simps)
  4084   {
  4085     fix x
  4086     assume "x \<in> S"
  4087     then have "norm x \<le> b"
  4088       using b by auto
  4089     then have "norm (f x) \<le> B * b"
  4090       using B(2)
  4091       apply (erule_tac x=x in allE)
  4092       apply (metis B(1) B(2) order_trans mult_le_cancel_left_pos)
  4093       done
  4094   }
  4095   then show ?thesis
  4096     unfolding bounded_pos
  4097     apply (rule_tac x="b*B" in exI)
  4098     using b B by (auto simp add: mult.commute)
  4099 qed
  4100 
  4101 lemma bounded_scaling:
  4102   fixes S :: "'a::real_normed_vector set"
  4103   shows "bounded S \<Longrightarrow> bounded ((\<lambda>x. c *\<^sub>R x) ` S)"
  4104   apply (rule bounded_linear_image)
  4105   apply assumption
  4106   apply (rule bounded_linear_scaleR_right)
  4107   done
  4108 
  4109 lemma bounded_scaleR_comp:
  4110   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  4111   assumes "bounded (f ` S)"
  4112   shows "bounded ((\<lambda>x. r *\<^sub>R f x) ` S)"
  4113   using bounded_scaling[of "f ` S" r] assms
  4114   by (auto simp: image_image)
  4115 
  4116 lemma bounded_translation:
  4117   fixes S :: "'a::real_normed_vector set"
  4118   assumes "bounded S"
  4119   shows "bounded ((\<lambda>x. a + x) ` S)"
  4120 proof -
  4121   from assms obtain b where b: "b > 0" "\<forall>x\<in>S. norm x \<le> b"
  4122     unfolding bounded_pos by auto
  4123   {
  4124     fix x
  4125     assume "x \<in> S"
  4126     then have "norm (a + x) \<le> b + norm a"
  4127       using norm_triangle_ineq[of a x] b by auto
  4128   }
  4129   then show ?thesis
  4130     unfolding bounded_pos
  4131     using norm_ge_zero[of a] b(1) and add_strict_increasing[of b 0 "norm a"]
  4132     by (auto intro!: exI[of _ "b + norm a"])
  4133 qed
  4134 
  4135 lemma bounded_translation_minus:
  4136   fixes S :: "'a::real_normed_vector set"
  4137   shows "bounded S \<Longrightarrow> bounded ((\<lambda>x. x - a) ` S)"
  4138 using bounded_translation [of S "-a"] by simp
  4139 
  4140 lemma bounded_uminus [simp]:
  4141   fixes X :: "'a::real_normed_vector set"
  4142   shows "bounded (uminus ` X) \<longleftrightarrow> bounded X"
  4143 by (auto simp: bounded_def dist_norm; rule_tac x="-x" in exI; force simp add: add.commute norm_minus_commute)
  4144 
  4145 lemma uminus_bounded_comp [simp]:
  4146   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  4147   shows "bounded ((\<lambda>x. - f x) ` S) \<longleftrightarrow> bounded (f ` S)"
  4148   using bounded_uminus[of "f ` S"]
  4149   by (auto simp: image_image)
  4150 
  4151 lemma bounded_plus_comp:
  4152   fixes f g::"'a \<Rightarrow> 'b::real_normed_vector"
  4153   assumes "bounded (f ` S)"
  4154   assumes "bounded (g ` S)"
  4155   shows "bounded ((\<lambda>x. f x + g x) ` S)"
  4156 proof -
  4157   {
  4158     fix B C
  4159     assume "\<And>x. x\<in>S \<Longrightarrow> norm (f x) \<le> B" "\<And>x. x\<in>S \<Longrightarrow> norm (g x) \<le> C"
  4160     then have "\<And>x. x \<in> S \<Longrightarrow> norm (f x + g x) \<le> B + C"
  4161       by (auto intro!: norm_triangle_le add_mono)
  4162   } then show ?thesis
  4163     using assms by (fastforce simp: bounded_iff)
  4164 qed
  4165 
  4166 lemma bounded_minus_comp:
  4167   "bounded (f ` S) \<Longrightarrow> bounded (g ` S) \<Longrightarrow> bounded ((\<lambda>x. f x - g x) ` S)"
  4168   for f g::"'a \<Rightarrow> 'b::real_normed_vector"
  4169   using bounded_plus_comp[of "f" S "\<lambda>x. - g x"]
  4170   by (auto simp: )
  4171 
  4172 
  4173 subsection\<open>Some theorems on sups and infs using the notion "bounded".\<close>
  4174 
  4175 lemma bounded_real: "bounded (S::real set) \<longleftrightarrow> (\<exists>a. \<forall>x\<in>S. \<bar>x\<bar> \<le> a)"
  4176   by (simp add: bounded_iff)
  4177 
  4178 lemma bounded_imp_bdd_above: "bounded S \<Longrightarrow> bdd_above (S :: real set)"
  4179   by (auto simp: bounded_def bdd_above_def dist_real_def)
  4180      (metis abs_le_D1 abs_minus_commute diff_le_eq)
  4181 
  4182 lemma bounded_imp_bdd_below: "bounded S \<Longrightarrow> bdd_below (S :: real set)"
  4183   by (auto simp: bounded_def bdd_below_def dist_real_def)
  4184      (metis abs_le_D1 add.commute diff_le_eq)
  4185 
  4186 lemma bounded_inner_imp_bdd_above:
  4187   assumes "bounded s"
  4188     shows "bdd_above ((\<lambda>x. x \<bullet> a) ` s)"
  4189 by (simp add: assms bounded_imp_bdd_above bounded_linear_image bounded_linear_inner_left)
  4190 
  4191 lemma bounded_inner_imp_bdd_below:
  4192   assumes "bounded s"
  4193     shows "bdd_below ((\<lambda>x. x \<bullet> a) ` s)"
  4194 by (simp add: assms bounded_imp_bdd_below bounded_linear_image bounded_linear_inner_left)
  4195 
  4196 lemma bounded_has_Sup:
  4197   fixes S :: "real set"
  4198   assumes "bounded S"
  4199     and "S \<noteq> {}"
  4200   shows "\<forall>x\<in>S. x \<le> Sup S"
  4201     and "\<forall>b. (\<forall>x\<in>S. x \<le> b) \<longrightarrow> Sup S \<le> b"
  4202 proof
  4203   show "\<forall>b. (\<forall>x\<in>S. x \<le> b) \<longrightarrow> Sup S \<le> b"
  4204     using assms by (metis cSup_least)
  4205 qed (metis cSup_upper assms(1) bounded_imp_bdd_above)
  4206 
  4207 lemma Sup_insert:
  4208   fixes S :: "real set"
  4209   shows "bounded S \<Longrightarrow> Sup (insert x S) = (if S = {} then x else max x (Sup S))"
  4210   by (auto simp: bounded_imp_bdd_above sup_max cSup_insert_If)
  4211 
  4212 lemma Sup_insert_finite:
  4213   fixes S :: "'a::conditionally_complete_linorder set"
  4214   shows "finite S \<Longrightarrow> Sup (insert x S) = (if S = {} then x else max x (Sup S))"
  4215 by (simp add: cSup_insert sup_max)
  4216 
  4217 lemma bounded_has_Inf:
  4218   fixes S :: "real set"
  4219   assumes "bounded S"
  4220     and "S \<noteq> {}"
  4221   shows "\<forall>x\<in>S. x \<ge> Inf S"
  4222     and "\<forall>b. (\<forall>x\<in>S. x \<ge> b) \<longrightarrow> Inf S \<ge> b"
  4223 proof
  4224   show "\<forall>b. (\<forall>x\<in>S. x \<ge> b) \<longrightarrow> Inf S \<ge> b"
  4225     using assms by (metis cInf_greatest)
  4226 qed (metis cInf_lower assms(1) bounded_imp_bdd_below)
  4227 
  4228 lemma Inf_insert:
  4229   fixes S :: "real set"
  4230   shows "bounded S \<Longrightarrow> Inf (insert x S) = (if S = {} then x else min x (Inf S))"
  4231   by (auto simp: bounded_imp_bdd_below inf_min cInf_insert_If)
  4232 
  4233 lemma Inf_insert_finite:
  4234   fixes S :: "'a::conditionally_complete_linorder set"
  4235   shows "finite S \<Longrightarrow> Inf (insert x S) = (if S = {} then x else min x (Inf S))"
  4236 by (simp add: cInf_eq_Min)
  4237 
  4238 lemma finite_imp_less_Inf:
  4239   fixes a :: "'a::conditionally_complete_linorder"
  4240   shows "\<lbrakk>finite X; x \<in> X; \<And>x. x\<in>X \<Longrightarrow> a < x\<rbrakk> \<Longrightarrow> a < Inf X"
  4241   by (induction X rule: finite_induct) (simp_all add: cInf_eq_Min Inf_insert_finite)
  4242 
  4243 lemma finite_less_Inf_iff:
  4244   fixes a :: "'a :: conditionally_complete_linorder"
  4245   shows "\<lbrakk>finite X; X \<noteq> {}\<rbrakk> \<Longrightarrow> a < Inf X \<longleftrightarrow> (\<forall>x \<in> X. a < x)"
  4246   by (auto simp: cInf_eq_Min)
  4247 
  4248 lemma finite_imp_Sup_less:
  4249   fixes a :: "'a::conditionally_complete_linorder"
  4250   shows "\<lbrakk>finite X; x \<in> X; \<And>x. x\<in>X \<Longrightarrow> a > x\<rbrakk> \<Longrightarrow> a > Sup X"
  4251   by (induction X rule: finite_induct) (simp_all add: cSup_eq_Max Sup_insert_finite)
  4252 
  4253 lemma finite_Sup_less_iff:
  4254   fixes a :: "'a :: conditionally_complete_linorder"
  4255   shows "\<lbrakk>finite X; X \<noteq> {}\<rbrakk> \<Longrightarrow> a > Sup X \<longleftrightarrow> (\<forall>x \<in> X. a > x)"
  4256   by (auto simp: cSup_eq_Max)
  4257 
  4258 subsection \<open>Compactness\<close>
  4259 
  4260 subsubsection \<open>Bolzano-Weierstrass property\<close>
  4261 
  4262 lemma heine_borel_imp_bolzano_weierstrass:
  4263   assumes "compact s"
  4264     and "infinite t"
  4265     and "t \<subseteq> s"
  4266   shows "\<exists>x \<in> s. x islimpt t"
  4267 proof (rule ccontr)
  4268   assume "\<not> (\<exists>x \<in> s. x islimpt t)"
  4269   then obtain f where f: "\<forall>x\<in>s. x \<in> f x \<and> open (f x) \<and> (\<forall>y\<in>t. y \<in> f x \<longrightarrow> y = x)"
  4270     unfolding islimpt_def
  4271     using bchoice[of s "\<lambda> x T. x \<in> T \<and> open T \<and> (\<forall>y\<in>t. y \<in> T \<longrightarrow> y = x)"]
  4272     by auto
  4273   obtain g where g: "g \<subseteq> {t. \<exists>x. x \<in> s \<and> t = f x}" "finite g" "s \<subseteq> \<Union>g"
  4274     using assms(1)[unfolded compact_eq_heine_borel, THEN spec[where x="{t. \<exists>x. x\<in>s \<and> t = f x}"]]
  4275     using f by auto
  4276   from g(1,3) have g':"\<forall>x\<in>g. \<exists>xa \<in> s. x = f xa"
  4277     by auto
  4278   {
  4279     fix x y
  4280     assume "x \<in> t" "y \<in> t" "f x = f y"
  4281     then have "x \<in> f x"  "y \<in> f x \<longrightarrow> y = x"
  4282       using f[THEN bspec[where x=x]] and \<open>t \<subseteq> s\<close> by auto
  4283     then have "x = y"
  4284       using \<open>f x = f y\<close> and f[THEN bspec[where x=y]] and \<open>y \<in> t\<close> and \<open>t \<subseteq> s\<close>
  4285       by auto
  4286   }
  4287   then have "inj_on f t"
  4288     unfolding inj_on_def by simp
  4289   then have "infinite (f ` t)"
  4290     using assms(2) using finite_imageD by auto
  4291   moreover
  4292   {
  4293     fix x
  4294     assume "x \<in> t" "f x \<notin> g"
  4295     from g(3) assms(3) \<open>x \<in> t\<close> obtain h where "h \<in> g" and "x \<in> h"
  4296       by auto
  4297     then obtain y where "y \<in> s" "h = f y"
  4298       using g'[THEN bspec[where x=h]] by auto
  4299     then have "y = x"
  4300       using f[THEN bspec[where x=y]] and \<open>x\<in>t\<close> and \<open>x\<in>h\<close>[unfolded \<open>h = f y\<close>]
  4301       by auto
  4302     then have False
  4303       using \<open>f x \<notin> g\<close> \<open>h \<in> g\<close> unfolding \<open>h = f y\<close>
  4304       by auto
  4305   }
  4306   then have "f ` t \<subseteq> g" by auto
  4307   ultimately show False
  4308     using g(2) using finite_subset by auto
  4309 qed
  4310 
  4311 lemma acc_point_range_imp_convergent_subsequence:
  4312   fixes l :: "'a :: first_countable_topology"
  4313   assumes l: "\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> range f)"
  4314   shows "\<exists>r. subseq r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
  4315 proof -
  4316   from countable_basis_at_decseq[of l]
  4317   obtain A where A:
  4318       "\<And>i. open (A i)"
  4319       "\<And>i. l \<in> A i"
  4320       "\<And>S. open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
  4321     by blast
  4322   define s where "s n i = (SOME j. i < j \<and> f j \<in> A (Suc n))" for n i
  4323   {
  4324     fix n i
  4325     have "infinite (A (Suc n) \<inter> range f - f`{.. i})"
  4326       using l A by auto
  4327     then have "\<exists>x. x \<in> A (Suc n) \<inter> range f - f`{.. i}"
  4328       unfolding ex_in_conv by (intro notI) simp
  4329     then have "\<exists>j. f j \<in> A (Suc n) \<and> j \<notin> {.. i}"
  4330       by auto
  4331     then have "\<exists>a. i < a \<and> f a \<in> A (Suc n)"
  4332       by (auto simp: not_le)
  4333     then have "i < s n i" "f (s n i) \<in> A (Suc n)"
  4334       unfolding s_def by (auto intro: someI2_ex)
  4335   }
  4336   note s = this
  4337   define r where "r = rec_nat (s 0 0) s"
  4338   have "subseq r"
  4339     by (auto simp: r_def s subseq_Suc_iff)
  4340   moreover
  4341   have "(\<lambda>n. f (r n)) \<longlonglongrightarrow> l"
  4342   proof (rule topological_tendstoI)
  4343     fix S
  4344     assume "open S" "l \<in> S"
  4345     with A(3) have "eventually (\<lambda>i. A i \<subseteq> S) sequentially"
  4346       by auto
  4347     moreover
  4348     {
  4349       fix i
  4350       assume "Suc 0 \<le> i"
  4351       then have "f (r i) \<in> A i"
  4352         by (cases i) (simp_all add: r_def s)
  4353     }
  4354     then have "eventually (\<lambda>i. f (r i) \<in> A i) sequentially"
  4355       by (auto simp: eventually_sequentially)
  4356     ultimately show "eventually (\<lambda>i. f (r i) \<in> S) sequentially"
  4357       by eventually_elim auto
  4358   qed
  4359   ultimately show "\<exists>r. subseq r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
  4360     by (auto simp: convergent_def comp_def)
  4361 qed
  4362 
  4363 lemma sequence_infinite_lemma:
  4364   fixes f :: "nat \<Rightarrow> 'a::t1_space"
  4365   assumes "\<forall>n. f n \<noteq> l"
  4366     and "(f \<longlongrightarrow> l) sequentially"
  4367   shows "infinite (range f)"
  4368 proof
  4369   assume "finite (range f)"
  4370   then have "closed (range f)"
  4371     by (rule finite_imp_closed)
  4372   then have "open (- range f)"
  4373     by (rule open_Compl)
  4374   from assms(1) have "l \<in> - range f"
  4375     by auto
  4376   from assms(2) have "eventually (\<lambda>n. f n \<in> - range f) sequentially"
  4377     using \<open>open (- range f)\<close> \<open>l \<in> - range f\<close>
  4378     by (rule topological_tendstoD)
  4379   then show False
  4380     unfolding eventually_sequentially
  4381     by auto
  4382 qed
  4383 
  4384 lemma closure_insert:
  4385   fixes x :: "'a::t1_space"
  4386   shows "closure (insert x s) = insert x (closure s)"
  4387   apply (rule closure_unique)
  4388   apply (rule insert_mono [OF closure_subset])
  4389   apply (rule closed_insert [OF closed_closure])
  4390   apply (simp add: closure_minimal)
  4391   done
  4392 
  4393 lemma islimpt_insert:
  4394   fixes x :: "'a::t1_space"
  4395   shows "x islimpt (insert a s) \<longleftrightarrow> x islimpt s"
  4396 proof
  4397   assume *: "x islimpt (insert a s)"
  4398   show "x islimpt s"
  4399   proof (rule islimptI)
  4400     fix t
  4401     assume t: "x \<in> t" "open t"
  4402     show "\<exists>y\<in>s. y \<in> t \<and> y \<noteq> x"
  4403     proof (cases "x = a")
  4404       case True
  4405       obtain y where "y \<in> insert a s" "y \<in> t" "y \<noteq> x"
  4406         using * t by (rule islimptE)
  4407       with \<open>x = a\<close> show ?thesis by auto
  4408     next
  4409       case False
  4410       with t have t': "x \<in> t - {a}" "open (t - {a})"
  4411         by (simp_all add: open_Diff)
  4412       obtain y where "y \<in> insert a s" "y \<in> t - {a}" "y \<noteq> x"
  4413         using * t' by (rule islimptE)
  4414       then show ?thesis by auto
  4415     qed
  4416   qed
  4417 next
  4418   assume "x islimpt s"
  4419   then show "x islimpt (insert a s)"
  4420     by (rule islimpt_subset) auto
  4421 qed
  4422 
  4423 lemma islimpt_finite:
  4424   fixes x :: "'a::t1_space"
  4425   shows "finite s \<Longrightarrow> \<not> x islimpt s"
  4426   by (induct set: finite) (simp_all add: islimpt_insert)
  4427 
  4428 lemma islimpt_Un_finite:
  4429   fixes x :: "'a::t1_space"
  4430   shows "finite s \<Longrightarrow> x islimpt (s \<union> t) \<longleftrightarrow> x islimpt t"
  4431   by (simp add: islimpt_Un islimpt_finite)
  4432 
  4433 lemma islimpt_eq_acc_point:
  4434   fixes l :: "'a :: t1_space"
  4435   shows "l islimpt S \<longleftrightarrow> (\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> S))"
  4436 proof (safe intro!: islimptI)
  4437   fix U
  4438   assume "l islimpt S" "l \<in> U" "open U" "finite (U \<inter> S)"
  4439   then have "l islimpt S" "l \<in> (U - (U \<inter> S - {l}))" "open (U - (U \<inter> S - {l}))"
  4440     by (auto intro: finite_imp_closed)
  4441   then show False
  4442     by (rule islimptE) auto
  4443 next
  4444   fix T
  4445   assume *: "\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> S)" "l \<in> T" "open T"
  4446   then have "infinite (T \<inter> S - {l})"
  4447     by auto
  4448   then have "\<exists>x. x \<in> (T \<inter> S - {l})"
  4449     unfolding ex_in_conv by (intro notI) simp
  4450   then show "\<exists>y\<in>S. y \<in> T \<and> y \<noteq> l"
  4451     by auto
  4452 qed
  4453 
  4454 corollary infinite_openin:
  4455   fixes S :: "'a :: t1_space set"
  4456   shows "\<lbrakk>openin (subtopology euclidean U) S; x \<in> S; x islimpt U\<rbrakk> \<Longrightarrow> infinite S"
  4457   by (clarsimp simp add: openin_open islimpt_eq_acc_point inf_commute)
  4458 
  4459 lemma islimpt_range_imp_convergent_subsequence:
  4460   fixes l :: "'a :: {t1_space, first_countable_topology}"
  4461   assumes l: "l islimpt (range f)"
  4462   shows "\<exists>r. subseq r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
  4463   using l unfolding islimpt_eq_acc_point
  4464   by (rule acc_point_range_imp_convergent_subsequence)
  4465 
  4466 lemma islimpt_eq_infinite_ball: "x islimpt S \<longleftrightarrow> (\<forall>e>0. infinite(S \<inter> ball x e))"
  4467   apply (simp add: islimpt_eq_acc_point, safe)
  4468    apply (metis Int_commute open_ball centre_in_ball)
  4469   by (metis open_contains_ball Int_mono finite_subset inf_commute subset_refl)
  4470 
  4471 lemma islimpt_eq_infinite_cball: "x islimpt S \<longleftrightarrow> (\<forall>e>0. infinite(S \<inter> cball x e))"
  4472   apply (simp add: islimpt_eq_infinite_ball, safe)
  4473    apply (meson Int_mono ball_subset_cball finite_subset order_refl)
  4474   by (metis open_ball centre_in_ball finite_Int inf.absorb_iff2 inf_assoc open_contains_cball_eq)
  4475 
  4476 lemma sequence_unique_limpt:
  4477   fixes f :: "nat \<Rightarrow> 'a::t2_space"
  4478   assumes "(f \<longlongrightarrow> l) sequentially"
  4479     and "l' islimpt (range f)"
  4480   shows "l' = l"
  4481 proof (rule ccontr)
  4482   assume "l' \<noteq> l"
  4483   obtain s t where "open s" "open t" "l' \<in> s" "l \<in> t" "s \<inter> t = {}"
  4484     using hausdorff [OF \<open>l' \<noteq> l\<close>] by auto
  4485   have "eventually (\<lambda>n. f n \<in> t) sequentially"
  4486     using assms(1) \<open>open t\<close> \<open>l \<in> t\<close> by (rule topological_tendstoD)
  4487   then obtain N where "\<forall>n\<ge>N. f n \<in> t"
  4488     unfolding eventually_sequentially by auto
  4489 
  4490   have "UNIV = {..<N} \<union> {N..}"
  4491     by auto
  4492   then have "l' islimpt (f ` ({..<N} \<union> {N..}))"
  4493     using assms(2) by simp
  4494   then have "l' islimpt (f ` {..<N} \<union> f ` {N..})"
  4495     by (simp add: image_Un)
  4496   then have "l' islimpt (f ` {N..})"
  4497     by (simp add: islimpt_Un_finite)
  4498   then obtain y where "y \<in> f ` {N..}" "y \<in> s" "y \<noteq> l'"
  4499     using \<open>l' \<in> s\<close> \<open>open s\<close> by (rule islimptE)
  4500   then obtain n where "N \<le> n" "f n \<in> s" "f n \<noteq> l'"
  4501     by auto
  4502   with \<open>\<forall>n\<ge>N. f n \<in> t\<close> have "f n \<in> s \<inter> t"
  4503     by simp
  4504   with \<open>s \<inter> t = {}\<close> show False
  4505     by simp
  4506 qed
  4507 
  4508 lemma bolzano_weierstrass_imp_closed:
  4509   fixes s :: "'a::{first_countable_topology,t2_space} set"
  4510   assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"
  4511   shows "closed s"
  4512 proof -
  4513   {
  4514     fix x l
  4515     assume as: "\<forall>n::nat. x n \<in> s" "(x \<longlongrightarrow> l) sequentially"
  4516     then have "l \<in> s"
  4517     proof (cases "\<forall>n. x n \<noteq> l")
  4518       case False
  4519       then show "l\<in>s" using as(1) by auto
  4520     next
  4521       case True note cas = this
  4522       with as(2) have "infinite (range x)"
  4523         using sequence_infinite_lemma[of x l] by auto
  4524       then obtain l' where "l'\<in>s" "l' islimpt (range x)"
  4525         using assms[THEN spec[where x="range x"]] as(1) by auto
  4526       then show "l\<in>s" using sequence_unique_limpt[of x l l']
  4527         using as cas by auto
  4528     qed
  4529   }
  4530   then show ?thesis
  4531     unfolding closed_sequential_limits by fast
  4532 qed
  4533 
  4534 lemma compact_imp_bounded:
  4535   assumes "compact U"
  4536   shows "bounded U"
  4537 proof -
  4538   have "compact U" "\<forall>x\<in>U. open (ball x 1)" "U \<subseteq> (\<Union>x\<in>U. ball x 1)"
  4539     using assms by auto
  4540   then obtain D where D: "D \<subseteq> U" "finite D" "U \<subseteq> (\<Union>x\<in>D. ball x 1)"
  4541     by (rule compactE_image)
  4542   from \<open>finite D\<close> have "bounded (\<Union>x\<in>D. ball x 1)"
  4543     by (simp add: bounded_UN)
  4544   then show "bounded U" using \<open>U \<subseteq> (\<Union>x\<in>D. ball x 1)\<close>
  4545     by (rule bounded_subset)
  4546 qed
  4547 
  4548 text\<open>In particular, some common special cases.\<close>
  4549 
  4550 lemma compact_Un [intro]:
  4551   assumes "compact s"
  4552     and "compact t"
  4553   shows " compact (s \<union> t)"
  4554 proof (rule compactI)
  4555   fix f
  4556   assume *: "Ball f open" "s \<union> t \<subseteq> \<Union>f"
  4557   from * \<open>compact s\<close> obtain s' where "s' \<subseteq> f \<and> finite s' \<and> s \<subseteq> \<Union>s'"
  4558     unfolding compact_eq_heine_borel by (auto elim!: allE[of _ f])
  4559   moreover
  4560   from * \<open>compact t\<close> obtain t' where "t' \<subseteq> f \<and> finite t' \<and> t \<subseteq> \<Union>t'"
  4561     unfolding compact_eq_heine_borel by (auto elim!: allE[of _ f])
  4562   ultimately show "\<exists>f'\<subseteq>f. finite f' \<and> s \<union> t \<subseteq> \<Union>f'"
  4563     by (auto intro!: exI[of _ "s' \<union> t'"])
  4564 qed
  4565 
  4566 lemma compact_Union [intro]: "finite S \<Longrightarrow> (\<And>T. T \<in> S \<Longrightarrow> compact T) \<Longrightarrow> compact (\<Union>S)"
  4567   by (induct set: finite) auto
  4568 
  4569 lemma compact_UN [intro]:
  4570   "finite A \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> compact (B x)) \<Longrightarrow> compact (\<Union>x\<in>A. B x)"
  4571   by (rule compact_Union) auto
  4572 
  4573 lemma closed_Int_compact [intro]:
  4574   assumes "closed s"
  4575     and "compact t"
  4576   shows "compact (s \<inter> t)"
  4577   using compact_Int_closed [of t s] assms
  4578   by (simp add: Int_commute)
  4579 
  4580 lemma compact_Int [intro]:
  4581   fixes s t :: "'a :: t2_space set"
  4582   assumes "compact s"
  4583     and "compact t"
  4584   shows "compact (s \<inter> t)"
  4585   using assms by (intro compact_Int_closed compact_imp_closed)
  4586 
  4587 lemma compact_sing [simp]: "compact {a}"
  4588   unfolding compact_eq_heine_borel by auto
  4589 
  4590 lemma compact_insert [simp]:
  4591   assumes "compact s"
  4592   shows "compact (insert x s)"
  4593 proof -
  4594   have "compact ({x} \<union> s)"
  4595     using compact_sing assms by (rule compact_Un)
  4596   then show ?thesis by simp
  4597 qed
  4598 
  4599 lemma finite_imp_compact: "finite s \<Longrightarrow> compact s"
  4600   by (induct set: finite) simp_all
  4601 
  4602 lemma open_delete:
  4603   fixes s :: "'a::t1_space set"
  4604   shows "open s \<Longrightarrow> open (s - {x})"
  4605   by (simp add: open_Diff)
  4606 
  4607 lemma openin_delete:
  4608   fixes a :: "'a :: t1_space"
  4609   shows "openin (subtopology euclidean u) s
  4610          \<Longrightarrow> openin (subtopology euclidean u) (s - {a})"
  4611 by (metis Int_Diff open_delete openin_open)
  4612 
  4613 
  4614 text\<open>Compactness expressed with filters\<close>
  4615 
  4616 lemma closure_iff_nhds_not_empty:
  4617   "x \<in> closure X \<longleftrightarrow> (\<forall>A. \<forall>S\<subseteq>A. open S \<longrightarrow> x \<in> S \<longrightarrow> X \<inter> A \<noteq> {})"
  4618 proof safe
  4619   assume x: "x \<in> closure X"
  4620   fix S A
  4621   assume "open S" "x \<in> S" "X \<inter> A = {}" "S \<subseteq> A"
  4622   then have "x \<notin> closure (-S)"
  4623     by (auto simp: closure_complement subset_eq[symmetric] intro: interiorI)
  4624   with x have "x \<in> closure X - closure (-S)"
  4625     by auto
  4626   also have "\<dots> \<subseteq> closure (X \<inter> S)"
  4627     using \<open>open S\<close> open_Int_closure_subset[of S X] by (simp add: closed_Compl ac_simps)
  4628   finally have "X \<inter> S \<noteq> {}" by auto
  4629   then show False using \<open>X \<inter> A = {}\<close> \<open>S \<subseteq> A\<close> by auto
  4630 next
  4631   assume "\<forall>A S. S \<subseteq> A \<longrightarrow> open S \<longrightarrow> x \<in> S \<longrightarrow> X \<inter> A \<noteq> {}"
  4632   from this[THEN spec, of "- X", THEN spec, of "- closure X"]
  4633   show "x \<in> closure X"
  4634     by (simp add: closure_subset open_Compl)
  4635 qed
  4636 
  4637 lemma compact_filter:
  4638   "compact U \<longleftrightarrow> (\<forall>F. F \<noteq> bot \<longrightarrow> eventually (\<lambda>x. x \<in> U) F \<longrightarrow> (\<exists>x\<in>U. inf (nhds x) F \<noteq> bot))"
  4639 proof (intro allI iffI impI compact_fip[THEN iffD2] notI)
  4640   fix F
  4641   assume "compact U"
  4642   assume F: "F \<noteq> bot" "eventually (\<lambda>x. x \<in> U) F"
  4643   then have "U \<noteq> {}"
  4644     by (auto simp: eventually_False)
  4645 
  4646   define Z where "Z = closure ` {A. eventually (\<lambda>x. x \<in> A) F}"
  4647   then have "\<forall>z\<in>Z. closed z"
  4648     by auto
  4649   moreover
  4650   have ev_Z: "\<And>z. z \<in> Z \<Longrightarrow> eventually (\<lambda>x. x \<in> z) F"
  4651     unfolding Z_def by (auto elim: eventually_mono intro: set_mp[OF closure_subset])
  4652   have "(\<forall>B \<subseteq> Z. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {})"
  4653   proof (intro allI impI)
  4654     fix B assume "finite B" "B \<subseteq> Z"
  4655     with \<open>finite B\<close> ev_Z F(2) have "eventually (\<lambda>x. x \<in> U \<inter> (\<Inter>B)) F"
  4656       by (auto simp: eventually_ball_finite_distrib eventually_conj_iff)
  4657     with F show "U \<inter> \<Inter>B \<noteq> {}"
  4658       by (intro notI) (simp add: eventually_False)
  4659   qed
  4660   ultimately have "U \<inter> \<Inter>Z \<noteq> {}"
  4661     using \<open>compact U\<close> unfolding compact_fip by blast
  4662   then obtain x where "x \<in> U" and x: "\<And>z. z \<in> Z \<Longrightarrow> x \<in> z"
  4663     by auto
  4664 
  4665   have "\<And>P. eventually P (inf (nhds x) F) \<Longrightarrow> P \<noteq> bot"
  4666     unfolding eventually_inf eventually_nhds
  4667   proof safe
  4668     fix P Q R S
  4669     assume "eventually R F" "open S" "x \<in> S"
  4670     with open_Int_closure_eq_empty[of S "{x. R x}"] x[of "closure {x. R x}"]
  4671     have "S \<inter> {x. R x} \<noteq> {}" by (auto simp: Z_def)
  4672     moreover assume "Ball S Q" "\<forall>x. Q x \<and> R x \<longrightarrow> bot x"
  4673     ultimately show False by (auto simp: set_eq_iff)
  4674   qed
  4675   with \<open>x \<in> U\<close> show "\<exists>x\<in>U. inf (nhds x) F \<noteq> bot"
  4676     by (metis eventually_bot)
  4677 next
  4678   fix A
  4679   assume A: "\<forall>a\<in>A. closed a" "\<forall>B\<subseteq>A. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {}" "U \<inter> \<Inter>A = {}"
  4680   define F where "F = (INF a:insert U A. principal a)"
  4681   have "F \<noteq> bot"
  4682     unfolding F_def
  4683   proof (rule INF_filter_not_bot)
  4684     fix X
  4685     assume X: "X \<subseteq> insert U A" "finite X"
  4686     with A(2)[THEN spec, of "X - {U}"] have "U \<inter> \<Inter>(X - {U}) \<noteq> {}"
  4687       by auto
  4688     with X show "(INF a:X. principal a) \<noteq> bot"
  4689       by (auto simp add: INF_principal_finite principal_eq_bot_iff)
  4690   qed
  4691   moreover
  4692   have "F \<le> principal U"
  4693     unfolding F_def by auto
  4694   then have "eventually (\<lambda>x. x \<in> U) F"
  4695     by (auto simp: le_filter_def eventually_principal)
  4696   moreover
  4697   assume "\<forall>F. F \<noteq> bot \<longrightarrow> eventually (\<lambda>x. x \<in> U) F \<longrightarrow> (\<exists>x\<in>U. inf (nhds x) F \<noteq> bot)"
  4698   ultimately obtain x where "x \<in> U" and x: "inf (nhds x) F \<noteq> bot"
  4699     by auto
  4700 
  4701   { fix V assume "V \<in> A"
  4702     then have "F \<le> principal V"
  4703       unfolding F_def by (intro INF_lower2[of V]) auto
  4704     then have V: "eventually (\<lambda>x. x \<in> V) F"
  4705       by (auto simp: le_filter_def eventually_principal)
  4706     have "x \<in> closure V"
  4707       unfolding closure_iff_nhds_not_empty
  4708     proof (intro impI allI)
  4709       fix S A
  4710       assume "open S" "x \<in> S" "S \<subseteq> A"
  4711       then have "eventually (\<lambda>x. x \<in> A) (nhds x)"
  4712         by (auto simp: eventually_nhds)
  4713       with V have "eventually (\<lambda>x. x \<in> V \<inter> A) (inf (nhds x) F)"
  4714         by (auto simp: eventually_inf)
  4715       with x show "V \<inter> A \<noteq> {}"
  4716         by (auto simp del: Int_iff simp add: trivial_limit_def)
  4717     qed
  4718     then have "x \<in> V"
  4719       using \<open>V \<in> A\<close> A(1) by simp
  4720   }
  4721   with \<open>x\<in>U\<close> have "x \<in> U \<inter> \<Inter>A" by auto
  4722   with \<open>U \<inter> \<Inter>A = {}\<close> show False by auto
  4723 qed
  4724 
  4725 definition "countably_compact U \<longleftrightarrow>
  4726     (\<forall>A. countable A \<longrightarrow> (\<forall>a\<in>A. open a) \<longrightarrow> U \<subseteq> \<Union>A \<longrightarrow> (\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T))"
  4727 
  4728 lemma countably_compactE:
  4729   assumes "countably_compact s" and "\<forall>t\<in>C. open t" and "s \<subseteq> \<Union>C" "countable C"
  4730   obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> \<Union>C'"
  4731   using assms unfolding countably_compact_def by metis
  4732 
  4733 lemma countably_compactI:
  4734   assumes "\<And>C. \<forall>t\<in>C. open t \<Longrightarrow> s \<subseteq> \<Union>C \<Longrightarrow> countable C \<Longrightarrow> (\<exists>C'\<subseteq>C. finite C' \<and> s \<subseteq> \<Union>C')"
  4735   shows "countably_compact s"
  4736   using assms unfolding countably_compact_def by metis
  4737 
  4738 lemma compact_imp_countably_compact: "compact U \<Longrightarrow> countably_compact U"
  4739   by (auto simp: compact_eq_heine_borel countably_compact_def)
  4740 
  4741 lemma countably_compact_imp_compact:
  4742   assumes "countably_compact U"
  4743     and ccover: "countable B" "\<forall>b\<in>B. open b"
  4744     and basis: "\<And>T x. open T \<Longrightarrow> x \<in> T \<Longrightarrow> x \<in> U \<Longrightarrow> \<exists>b\<in>B. x \<in> b \<and> b \<inter> U \<subseteq> T"
  4745   shows "compact U"
  4746   using \<open>countably_compact U\<close>
  4747   unfolding compact_eq_heine_borel countably_compact_def
  4748 proof safe
  4749   fix A
  4750   assume A: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A"
  4751   assume *: "\<forall>A. countable A \<longrightarrow> (\<forall>a\<in>A. open a) \<longrightarrow> U \<subseteq> \<Union>A \<longrightarrow> (\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T)"
  4752   moreover define C where "C = {b\<in>B. \<exists>a\<in>A. b \<inter> U \<subseteq> a}"
  4753   ultimately have "countable C" "\<forall>a\<in>C. open a"
  4754     unfolding C_def using ccover by auto
  4755   moreover
  4756   have "\<Union>A \<inter> U \<subseteq> \<Union>C"
  4757   proof safe
  4758     fix x a
  4759     assume "x \<in> U" "x \<in> a" "a \<in> A"
  4760     with basis[of a x] A obtain b where "b \<in> B" "x \<in> b" "b \<inter> U \<subseteq> a"
  4761       by blast
  4762     with \<open>a \<in> A\<close> show "x \<in> \<Union>C"
  4763       unfolding C_def by auto
  4764   qed
  4765   then have "U \<subseteq> \<Union>C" using \<open>U \<subseteq> \<Union>A\<close> by auto
  4766   ultimately obtain T where T: "T\<subseteq>C" "finite T" "U \<subseteq> \<Union>T"
  4767     using * by metis
  4768   then have "\<forall>t\<in>T. \<exists>a\<in>A. t \<inter> U \<subseteq> a"
  4769     by (auto simp: C_def)
  4770   then obtain f where "\<forall>t\<in>T. f t \<in> A \<and> t \<inter> U \<subseteq> f t"
  4771     unfolding bchoice_iff Bex_def ..
  4772   with T show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"
  4773     unfolding C_def by (intro exI[of _ "f`T"]) fastforce
  4774 qed
  4775 
  4776 lemma countably_compact_imp_compact_second_countable:
  4777   "countably_compact U \<Longrightarrow> compact (U :: 'a :: second_countable_topology set)"
  4778 proof (rule countably_compact_imp_compact)
  4779   fix T and x :: 'a
  4780   assume "open T" "x \<in> T"
  4781   from topological_basisE[OF is_basis this] obtain b where
  4782     "b \<in> (SOME B. countable B \<and> topological_basis B)" "x \<in> b" "b \<subseteq> T" .
  4783   then show "\<exists>b\<in>SOME B. countable B \<and> topological_basis B. x \<in> b \<and> b \<inter> U \<subseteq> T"
  4784     by blast
  4785 qed (insert countable_basis topological_basis_open[OF is_basis], auto)
  4786 
  4787 lemma countably_compact_eq_compact:
  4788   "countably_compact U \<longleftrightarrow> compact (U :: 'a :: second_countable_topology set)"
  4789   using countably_compact_imp_compact_second_countable compact_imp_countably_compact by blast
  4790 
  4791 subsubsection\<open>Sequential compactness\<close>
  4792 
  4793 definition seq_compact :: "'a::topological_space set \<Rightarrow> bool"
  4794   where "seq_compact S \<longleftrightarrow>
  4795     (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow> (\<exists>l\<in>S. \<exists>r. subseq r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially))"
  4796 
  4797 lemma seq_compactI:
  4798   assumes "\<And>f. \<forall>n. f n \<in> S \<Longrightarrow> \<exists>l\<in>S. \<exists>r. subseq r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
  4799   shows "seq_compact S"
  4800   unfolding seq_compact_def using assms by fast
  4801 
  4802 lemma seq_compactE:
  4803   assumes "seq_compact S" "\<forall>n. f n \<in> S"
  4804   obtains l r where "l \<in> S" "subseq r" "((f \<circ> r) \<longlongrightarrow> l) sequentially"
  4805   using assms unfolding seq_compact_def by fast
  4806 
  4807 lemma closed_sequentially: (* TODO: move upwards *)
  4808   assumes "closed s" and "\<forall>n. f n \<in> s" and "f \<longlonglongrightarrow> l"
  4809   shows "l \<in> s"
  4810 proof (rule ccontr)
  4811   assume "l \<notin> s"
  4812   with \<open>closed s\<close> and \<open>f \<longlonglongrightarrow> l\<close> have "eventually (\<lambda>n. f n \<in> - s) sequentially"
  4813     by (fast intro: topological_tendstoD)
  4814   with \<open>\<forall>n. f n \<in> s\<close> show "False"
  4815     by simp
  4816 qed
  4817 
  4818 lemma seq_compact_Int_closed:
  4819   assumes "seq_compact s" and "closed t"
  4820   shows "seq_compact (s \<inter> t)"
  4821 proof (rule seq_compactI)
  4822   fix f assume "\<forall>n::nat. f n \<in> s \<inter> t"
  4823   hence "\<forall>n. f n \<in> s" and "\<forall>n. f n \<in> t"
  4824     by simp_all
  4825   from \<open>seq_compact s\<close> and \<open>\<forall>n. f n \<in> s\<close>
  4826   obtain l r where "l \<in> s" and r: "subseq r" and l: "(f \<circ> r) \<longlonglongrightarrow> l"
  4827     by (rule seq_compactE)
  4828   from \<open>\<forall>n. f n \<in> t\<close> have "\<forall>n. (f \<circ> r) n \<in> t"
  4829     by simp
  4830   from \<open>closed t\<close> and this and l have "l \<in> t"
  4831     by (rule closed_sequentially)
  4832   with \<open>l \<in> s\<close> and r and l show "\<exists>l\<in>s \<inter> t. \<exists>r. subseq r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
  4833     by fast
  4834 qed
  4835 
  4836 lemma seq_compact_closed_subset:
  4837   assumes "closed s" and "s \<subseteq> t" and "seq_compact t"
  4838   shows "seq_compact s"
  4839   using assms seq_compact_Int_closed [of t s] by (simp add: Int_absorb1)
  4840 
  4841 lemma seq_compact_imp_countably_compact:
  4842   fixes U :: "'a :: first_countable_topology set"
  4843   assumes "seq_compact U"
  4844   shows "countably_compact U"
  4845 proof (safe intro!: countably_compactI)
  4846   fix A
  4847   assume A: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A" "countable A"
  4848   have subseq: "\<And>X. range X \<subseteq> U \<Longrightarrow> \<exists>r x. x \<in> U \<and> subseq r \<and> (X \<circ> r) \<longlonglongrightarrow> x"
  4849     using \<open>seq_compact U\<close> by (fastforce simp: seq_compact_def subset_eq)
  4850   show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"
  4851   proof cases
  4852     assume "finite A"
  4853     with A show ?thesis by auto
  4854   next
  4855     assume "infinite A"
  4856     then have "A \<noteq> {}" by auto
  4857     show ?thesis
  4858     proof (rule ccontr)
  4859       assume "\<not> (\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T)"
  4860       then have "\<forall>T. \<exists>x. T \<subseteq> A \<and> finite T \<longrightarrow> (x \<in> U - \<Union>T)"
  4861         by auto
  4862       then obtain X' where T: "\<And>T. T \<subseteq> A \<Longrightarrow> finite T \<Longrightarrow> X' T \<in> U - \<Union>T"
  4863         by metis
  4864       define X where "X n = X' (from_nat_into A ` {.. n})" for n
  4865       have X: "\<And>n. X n \<in> U - (\<Union>i\<le>n. from_nat_into A i)"
  4866         using \<open>A \<noteq> {}\<close> unfolding X_def by (intro T) (auto intro: from_nat_into)
  4867       then have "range X \<subseteq> U"
  4868         by auto
  4869       with subseq[of X] obtain r x where "x \<in> U" and r: "subseq r" "(X \<circ> r) \<longlonglongrightarrow> x"
  4870         by auto
  4871       from \<open>x\<in>U\<close> \<open>U \<subseteq> \<Union>A\<close> from_nat_into_surj[OF \<open>countable A\<close>]
  4872       obtain n where "x \<in> from_nat_into A n" by auto
  4873       with r(2) A(1) from_nat_into[OF \<open>A \<noteq> {}\<close>, of n]
  4874       have "eventually (\<lambda>i. X (r i) \<in> from_nat_into A n) sequentially"
  4875         unfolding tendsto_def by (auto simp: comp_def)
  4876       then obtain N where "\<And>i. N \<le> i \<Longrightarrow> X (r i) \<in> from_nat_into A n"
  4877         by (auto simp: eventually_sequentially)
  4878       moreover from X have "\<And>i. n \<le> r i \<Longrightarrow> X (r i) \<notin> from_nat_into A n"
  4879         by auto
  4880       moreover from \<open>subseq r\<close>[THEN seq_suble, of "max n N"] have "\<exists>i. n \<le> r i \<and> N \<le> i"
  4881         by (auto intro!: exI[of _ "max n N"])
  4882       ultimately show False
  4883         by auto
  4884     qed
  4885   qed
  4886 qed
  4887 
  4888 lemma compact_imp_seq_compact:
  4889   fixes U :: "'a :: first_countable_topology set"
  4890   assumes "compact U"
  4891   shows "seq_compact U"
  4892   unfolding seq_compact_def
  4893 proof safe
  4894   fix X :: "nat \<Rightarrow> 'a"
  4895   assume "\<forall>n. X n \<in> U"
  4896   then have "eventually (\<lambda>x. x \<in> U) (filtermap X sequentially)"
  4897     by (auto simp: eventually_filtermap)
  4898   moreover
  4899   have "filtermap X sequentially \<noteq> bot"
  4900     by (simp add: trivial_limit_def eventually_filtermap)
  4901   ultimately
  4902   obtain x where "x \<in> U" and x: "inf (nhds x) (filtermap X sequentially) \<noteq> bot" (is "?F \<noteq> _")
  4903     using \<open>compact U\<close> by (auto simp: compact_filter)
  4904 
  4905   from countable_basis_at_decseq[of x]
  4906   obtain A where A:
  4907       "\<And>i. open (A i)"
  4908       "\<And>i. x \<in> A i"
  4909       "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
  4910     by blast
  4911   define s where "s n i = (SOME j. i < j \<and> X j \<in> A (Suc n))" for n i
  4912   {
  4913     fix n i
  4914     have "\<exists>a. i < a \<and> X a \<in> A (Suc n)"
  4915     proof (rule ccontr)
  4916       assume "\<not> (\<exists>a>i. X a \<in> A (Suc n))"
  4917       then have "\<And>a. Suc i \<le> a \<Longrightarrow> X a \<notin> A (Suc n)"
  4918         by auto
  4919       then have "eventually (\<lambda>x. x \<notin> A (Suc n)) (filtermap X sequentially)"
  4920         by (auto simp: eventually_filtermap eventually_sequentially)
  4921       moreover have "eventually (\<lambda>x. x \<in> A (Suc n)) (nhds x)"
  4922         using A(1,2)[of "Suc n"] by (auto simp: eventually_nhds)
  4923       ultimately have "eventually (\<lambda>x. False) ?F"
  4924         by (auto simp add: eventually_inf)
  4925       with x show False
  4926         by (simp add: eventually_False)
  4927     qed
  4928     then have "i < s n i" "X (s n i) \<in> A (Suc n)"
  4929       unfolding s_def by (auto intro: someI2_ex)
  4930   }
  4931   note s = this
  4932   define r where "r = rec_nat (s 0 0) s"
  4933   have "subseq r"
  4934     by (auto simp: r_def s subseq_Suc_iff)
  4935   moreover
  4936   have "(\<lambda>n. X (r n)) \<longlonglongrightarrow> x"
  4937   proof (rule topological_tendstoI)
  4938     fix S
  4939     assume "open S" "x \<in> S"
  4940     with A(3) have "eventually (\<lambda>i. A i \<subseteq> S) sequentially"
  4941       by auto
  4942     moreover
  4943     {
  4944       fix i
  4945       assume "Suc 0 \<le> i"
  4946       then have "X (r i) \<in> A i"
  4947         by (cases i) (simp_all add: r_def s)
  4948     }
  4949     then have "eventually (\<lambda>i. X (r i) \<in> A i) sequentially"
  4950       by (auto simp: eventually_sequentially)
  4951     ultimately show "eventually (\<lambda>i. X (r i) \<in> S) sequentially"
  4952       by eventually_elim auto
  4953   qed
  4954   ultimately show "\<exists>x \<in> U. \<exists>r. subseq r \<and> (X \<circ> r) \<longlonglongrightarrow> x"
  4955     using \<open>x \<in> U\<close> by (auto simp: convergent_def comp_def)
  4956 qed
  4957 
  4958 lemma countably_compact_imp_acc_point:
  4959   assumes "countably_compact s"
  4960     and "countable t"
  4961     and "infinite t"
  4962     and "t \<subseteq> s"
  4963   shows "\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t)"
  4964 proof (rule ccontr)
  4965   define C where "C = (\<lambda>F. interior (F \<union> (- t))) ` {F. finite F \<and> F \<subseteq> t }"
  4966   note \<open>countably_compact s\<close>
  4967   moreover have "\<forall>t\<in>C. open t"
  4968     by (auto simp: C_def)
  4969   moreover
  4970   assume "\<not> (\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t))"
  4971   then have s: "\<And>x. x \<in> s \<Longrightarrow> \<exists>U. x\<in>U \<and> open U \<and> finite (U \<inter> t)" by metis
  4972   have "s \<subseteq> \<Union>C"
  4973     using \<open>t \<subseteq> s\<close>
  4974     unfolding C_def
  4975     apply (safe dest!: s)
  4976     apply (rule_tac a="U \<inter> t" in UN_I)
  4977     apply (auto intro!: interiorI simp add: finite_subset)
  4978     done
  4979   moreover
  4980   from \<open>countable t\<close> have "countable C"
  4981     unfolding C_def by (auto intro: countable_Collect_finite_subset)
  4982   ultimately
  4983   obtain D where "D \<subseteq> C" "finite D" "s \<subseteq> \<Union>D"
  4984     by (rule countably_compactE)
  4985   then obtain E where E: "E \<subseteq> {F. finite F \<and> F \<subseteq> t }" "finite E"
  4986     and s: "s \<subseteq> (\<Union>F\<in>E. interior (F \<union> (- t)))"
  4987     by (metis (lifting) finite_subset_image C_def)
  4988   from s \<open>t \<subseteq> s\<close> have "t \<subseteq> \<Union>E"
  4989     using interior_subset by blast
  4990   moreover have "finite (\<Union>E)"
  4991     using E by auto
  4992   ultimately show False using \<open>infinite t\<close>
  4993     by (auto simp: finite_subset)
  4994 qed
  4995 
  4996 lemma countable_acc_point_imp_seq_compact:
  4997   fixes s :: "'a::first_countable_topology set"
  4998   assumes "\<forall>t. infinite t \<and> countable t \<and> t \<subseteq> s \<longrightarrow>
  4999     (\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t))"
  5000   shows "seq_compact s"
  5001 proof -
  5002   {
  5003     fix f :: "nat \<Rightarrow> 'a"
  5004     assume f: "\<forall>n. f n \<in> s"
  5005     have "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
  5006     proof (cases "finite (range f)")
  5007       case True
  5008       obtain l where "infinite {n. f n = f l}"
  5009         using pigeonhole_infinite[OF _ True] by auto
  5010       then obtain r where "subseq r" and fr: "\<forall>n. f (r n) = f l"
  5011         using infinite_enumerate by blast
  5012       then have "subseq r \<and> (f \<circ> r) \<longlonglongrightarrow> f l"
  5013         by (simp add: fr o_def)
  5014       with f show "\<exists>l\<in>s. \<exists>r. subseq r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
  5015         by auto
  5016     next
  5017       case False
  5018       with f assms have "\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> range f)"
  5019         by auto
  5020       then obtain l where "l \<in> s" "\<forall>U. l\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> range f)" ..
  5021       from this(2) have "\<exists>r. subseq r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
  5022         using acc_point_range_imp_convergent_subsequence[of l f] by auto
  5023       with \<open>l \<in> s\<close> show "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially" ..
  5024     qed
  5025   }
  5026   then show ?thesis
  5027     unfolding seq_compact_def by auto
  5028 qed
  5029 
  5030 lemma seq_compact_eq_countably_compact:
  5031   fixes U :: "'a :: first_countable_topology set"
  5032   shows "seq_compact U \<longleftrightarrow> countably_compact U"
  5033   using
  5034     countable_acc_point_imp_seq_compact
  5035     countably_compact_imp_acc_point
  5036     seq_compact_imp_countably_compact
  5037   by metis
  5038 
  5039 lemma seq_compact_eq_acc_point:
  5040   fixes s :: "'a :: first_countable_topology set"
  5041   shows "seq_compact s \<longleftrightarrow>
  5042     (\<forall>t. infinite t \<and> countable t \<and> t \<subseteq> s --> (\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t)))"
  5043   using
  5044     countable_acc_point_imp_seq_compact[of s]
  5045     countably_compact_imp_acc_point[of s]
  5046     seq_compact_imp_countably_compact[of s]
  5047   by metis
  5048 
  5049 lemma seq_compact_eq_compact:
  5050   fixes U :: "'a :: second_countable_topology set"
  5051   shows "seq_compact U \<longleftrightarrow> compact U"
  5052   using seq_compact_eq_countably_compact countably_compact_eq_compact by blast
  5053 
  5054 lemma bolzano_weierstrass_imp_seq_compact:
  5055   fixes s :: "'a::{t1_space, first_countable_topology} set"
  5056   shows "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t) \<Longrightarrow> seq_compact s"
  5057   by (rule countable_acc_point_imp_seq_compact) (metis islimpt_eq_acc_point)
  5058 
  5059 
  5060 subsubsection\<open>Totally bounded\<close>
  5061 
  5062 lemma cauchy_def: "Cauchy s \<longleftrightarrow> (\<forall>e>0. \<exists>N. \<forall>m n. m \<ge> N \<and> n \<ge> N \<longrightarrow> dist (s m) (s n) < e)"
  5063   unfolding Cauchy_def by metis
  5064 
  5065 lemma seq_compact_imp_totally_bounded:
  5066   assumes "seq_compact s"
  5067   shows "\<forall>e>0. \<exists>k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> (\<Union>x\<in>k. ball x e)"
  5068 proof -
  5069   { fix e::real assume "e > 0" assume *: "\<And>k. finite k \<Longrightarrow> k \<subseteq> s \<Longrightarrow> \<not> s \<subseteq> (\<Union>x\<in>k. ball x e)"
  5070     let ?Q = "\<lambda>x n r. r \<in> s \<and> (\<forall>m < (n::nat). \<not> (dist (x m) r < e))"
  5071     have "\<exists>x. \<forall>n::nat. ?Q x n (x n)"
  5072     proof (rule dependent_wellorder_choice)
  5073       fix n x assume "\<And>y. y < n \<Longrightarrow> ?Q x y (x y)"
  5074       then have "\<not> s \<subseteq> (\<Union>x\<in>x ` {0..<n}. ball x e)"
  5075         using *[of "x ` {0 ..< n}"] by (auto simp: subset_eq)
  5076       then obtain z where z:"z\<in>s" "z \<notin> (\<Union>x\<in>x ` {0..<n}. ball x e)"
  5077         unfolding subset_eq by auto
  5078       show "\<exists>r. ?Q x n r"
  5079         using z by auto
  5080     qed simp
  5081     then obtain x where "\<forall>n::nat. x n \<in> s" and x:"\<And>n m. m < n \<Longrightarrow> \<not> (dist (x m) (x n) < e)"
  5082       by blast
  5083     then obtain l r where "l \<in> s" and r:"subseq r" and "((x \<circ> r) \<longlongrightarrow> l) sequentially"
  5084       using assms by (metis seq_compact_def)
  5085     from this(3) have "Cauchy (x \<circ> r)"
  5086       using LIMSEQ_imp_Cauchy by auto
  5087     then obtain N::nat where "\<And>m n. N \<le> m \<Longrightarrow> N \<le> n \<Longrightarrow> dist ((x \<circ> r) m) ((x \<circ> r) n) < e"
  5088       unfolding cauchy_def using \<open>e > 0\<close> by blast
  5089     then have False
  5090       using x[of "r N" "r (N+1)"] r by (auto simp: subseq_def) }
  5091   then show ?thesis
  5092     by metis
  5093 qed
  5094 
  5095 subsubsection\<open>Heine-Borel theorem\<close>
  5096 
  5097 lemma seq_compact_imp_heine_borel:
  5098   fixes s :: "'a :: metric_space set"
  5099   assumes "seq_compact s"
  5100   shows "compact s"
  5101 proof -
  5102   from seq_compact_imp_totally_bounded[OF \<open>seq_compact s\<close>]
  5103   obtain f where f: "\<forall>e>0. finite (f e) \<and> f e \<subseteq> s \<and> s \<subseteq> (\<Union>x\<in>f e. ball x e)"
  5104     unfolding choice_iff' ..
  5105   define K where "K = (\<lambda>(x, r). ball x r) ` ((\<Union>e \<in> \<rat> \<inter> {0 <..}. f e) \<times> \<rat>)"
  5106   have "countably_compact s"
  5107     using \<open>seq_compact s\<close> by (rule seq_compact_imp_countably_compact)
  5108   then show "compact s"
  5109   proof (rule countably_compact_imp_compact)
  5110     show "countable K"
  5111       unfolding K_def using f
  5112       by (auto intro: countable_finite countable_subset countable_rat
  5113                intro!: countable_image countable_SIGMA countable_UN)
  5114     show "\<forall>b\<in>K. open b" by (auto simp: K_def)
  5115   next
  5116     fix T x
  5117     assume T: "open T" "x \<in> T" and x: "x \<in> s"
  5118     from openE[OF T] obtain e where "0 < e" "ball x e \<subseteq> T"
  5119       by auto
  5120     then have "0 < e / 2" "ball x (e / 2) \<subseteq> T"
  5121       by auto
  5122     from Rats_dense_in_real[OF \<open>0 < e / 2\<close>] obtain r where "r \<in> \<rat>" "0 < r" "r < e / 2"
  5123       by auto
  5124     from f[rule_format, of r] \<open>0 < r\<close> \<open>x \<in> s\<close> obtain k where "k \<in> f r" "x \<in> ball k r"
  5125       by auto
  5126     from \<open>r \<in> \<rat>\<close> \<open>0 < r\<close> \<open>k \<in> f r\<close> have "ball k r \<in> K"
  5127       by (auto simp: K_def)
  5128     then show "\<exists>b\<in>K. x \<in> b \<and> b \<inter> s \<subseteq> T"
  5129     proof (rule bexI[rotated], safe)
  5130       fix y
  5131       assume "y \<in> ball k r"
  5132       with \<open>r < e / 2\<close> \<open>x \<in> ball k r\<close> have "dist x y < e"
  5133         by (intro dist_triangle_half_r [of k _ e]) (auto simp: dist_commute)
  5134       with \<open>ball x e \<subseteq> T\<close> show "y \<in> T"
  5135         by auto
  5136     next
  5137       show "x \<in> ball k r" by fact
  5138     qed
  5139   qed
  5140 qed
  5141 
  5142 lemma compact_eq_seq_compact_metric:
  5143   "compact (s :: 'a::metric_space set) \<longleftrightarrow> seq_compact s"
  5144   using compact_imp_seq_compact seq_compact_imp_heine_borel by blast
  5145 
  5146 lemma compact_def: --\<open>this is the definition of compactness in HOL Light\<close>
  5147   "compact (S :: 'a::metric_space set) \<longleftrightarrow>
  5148    (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow> (\<exists>l\<in>S. \<exists>r. subseq r \<and> (f \<circ> r) \<longlonglongrightarrow> l))"
  5149   unfolding compact_eq_seq_compact_metric seq_compact_def by auto
  5150 
  5151 subsubsection \<open>Complete the chain of compactness variants\<close>
  5152 
  5153 lemma compact_eq_bolzano_weierstrass:
  5154   fixes s :: "'a::metric_space set"
  5155   shows "compact s \<longleftrightarrow> (\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t))"
  5156   (is "?lhs = ?rhs")
  5157 proof
  5158   assume ?lhs
  5159   then show ?rhs
  5160     using heine_borel_imp_bolzano_weierstrass[of s] by auto
  5161 next
  5162   assume ?rhs
  5163   then show ?lhs
  5164     unfolding compact_eq_seq_compact_metric by (rule bolzano_weierstrass_imp_seq_compact)
  5165 qed
  5166 
  5167 lemma bolzano_weierstrass_imp_bounded:
  5168   "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t) \<Longrightarrow> bounded s"
  5169   using compact_imp_bounded unfolding compact_eq_bolzano_weierstrass .
  5170 
  5171 
  5172 subsection \<open>Metric spaces with the Heine-Borel property\<close>
  5173 
  5174 text \<open>
  5175   A metric space (or topological vector space) is said to have the
  5176   Heine-Borel property if every closed and bounded subset is compact.
  5177 \<close>
  5178 
  5179 class heine_borel = metric_space +
  5180   assumes bounded_imp_convergent_subsequence:
  5181     "bounded (range f) \<Longrightarrow> \<exists>l r. subseq r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
  5182 
  5183 lemma bounded_closed_imp_seq_compact:
  5184   fixes s::"'a::heine_borel set"
  5185   assumes "bounded s"
  5186     and "closed s"
  5187   shows "seq_compact s"
  5188 proof (unfold seq_compact_def, clarify)
  5189   fix f :: "nat \<Rightarrow> 'a"
  5190   assume f: "\<forall>n. f n \<in> s"
  5191   with \<open>bounded s\<close> have "bounded (range f)"
  5192     by (auto intro: bounded_subset)
  5193   obtain l r where r: "subseq r" and l: "((f \<circ> r) \<longlongrightarrow> l) sequentially"
  5194     using bounded_imp_convergent_subsequence [OF \<open>bounded (range f)\<close>] by auto
  5195   from f have fr: "\<forall>n. (f \<circ> r) n \<in> s"
  5196     by simp
  5197   have "l \<in> s" using \<open>closed s\<close> fr l
  5198     by (rule closed_sequentially)
  5199   show "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
  5200     using \<open>l \<in> s\<close> r l by blast
  5201 qed
  5202 
  5203 lemma compact_eq_bounded_closed:
  5204   fixes s :: "'a::heine_borel set"
  5205   shows "compact s \<longleftrightarrow> bounded s \<and> closed s"
  5206   (is "?lhs = ?rhs")
  5207 proof
  5208   assume ?lhs
  5209   then show ?rhs
  5210     using compact_imp_closed compact_imp_bounded
  5211     by blast
  5212 next
  5213   assume ?rhs
  5214   then show ?lhs
  5215     using bounded_closed_imp_seq_compact[of s]
  5216     unfolding compact_eq_seq_compact_metric
  5217     by auto
  5218 qed
  5219 
  5220 lemma compact_closure [simp]:
  5221   fixes S :: "'a::heine_borel set"
  5222   shows "compact(closure S) \<longleftrightarrow> bounded S"
  5223 by (meson bounded_closure bounded_subset closed_closure closure_subset compact_eq_bounded_closed)
  5224 
  5225 lemma compact_components:
  5226   fixes s :: "'a::heine_borel set"
  5227   shows "\<lbrakk>compact s; c \<in> components s\<rbrakk> \<Longrightarrow> compact c"
  5228 by (meson bounded_subset closed_components in_components_subset compact_eq_bounded_closed)
  5229 
  5230 lemma not_compact_UNIV[simp]:
  5231   fixes s :: "'a::{real_normed_vector,perfect_space,heine_borel} set"
  5232   shows "~ compact (UNIV::'a set)"
  5233     by (simp add: compact_eq_bounded_closed)
  5234 
  5235 (* TODO: is this lemma necessary? *)
  5236 lemma bounded_increasing_convergent:
  5237   fixes s :: "nat \<Rightarrow> real"
  5238   shows "bounded {s n| n. True} \<Longrightarrow> \<forall>n. s n \<le> s (Suc n) \<Longrightarrow> \<exists>l. s \<longlonglongrightarrow> l"
  5239   using Bseq_mono_convergent[of s] incseq_Suc_iff[of s]
  5240   by (auto simp: image_def Bseq_eq_bounded convergent_def incseq_def)
  5241 
  5242 instance real :: heine_borel
  5243 proof
  5244   fix f :: "nat \<Rightarrow> real"
  5245   assume f: "bounded (range f)"
  5246   obtain r where r: "subseq r" "monoseq (f \<circ> r)"
  5247     unfolding comp_def by (metis seq_monosub)
  5248   then have "Bseq (f \<circ> r)"
  5249     unfolding Bseq_eq_bounded using f by (force intro: bounded_subset)
  5250   with r show "\<exists>l r. subseq r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
  5251     using Bseq_monoseq_convergent[of "f \<circ> r"] by (auto simp: convergent_def)
  5252 qed
  5253 
  5254 lemma compact_lemma_general:
  5255   fixes f :: "nat \<Rightarrow> 'a"
  5256   fixes proj::"'a \<Rightarrow> 'b \<Rightarrow> 'c::heine_borel" (infixl "proj" 60)
  5257   fixes unproj:: "('b \<Rightarrow> 'c) \<Rightarrow> 'a"
  5258   assumes finite_basis: "finite basis"
  5259   assumes bounded_proj: "\<And>k. k \<in> basis \<Longrightarrow> bounded ((\<lambda>x. x proj k) ` range f)"
  5260   assumes proj_unproj: "\<And>e k. k \<in> basis \<Longrightarrow> (unproj e) proj k = e k"
  5261   assumes unproj_proj: "\<And>x. unproj (\<lambda>k. x proj k) = x"
  5262   shows "\<forall>d\<subseteq>basis. \<exists>l::'a. \<exists> r.
  5263     subseq r \<and> (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) proj i) (l proj i) < e) sequentially)"
  5264 proof safe
  5265   fix d :: "'b set"
  5266   assume d: "d \<subseteq> basis"
  5267   with finite_basis have "finite d"
  5268     by (blast intro: finite_subset)
  5269   from this d show "\<exists>l::'a. \<exists>r. subseq r \<and>
  5270     (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) proj i) (l proj i) < e) sequentially)"
  5271   proof (induct d)
  5272     case empty
  5273     then show ?case
  5274       unfolding subseq_def by auto
  5275   next
  5276     case (insert k d)
  5277     have k[intro]: "k \<in> basis"
  5278       using insert by auto
  5279     have s': "bounded ((\<lambda>x. x proj k) ` range f)"
  5280       using k
  5281       by (rule bounded_proj)
  5282     obtain l1::"'a" and r1 where r1: "subseq r1"
  5283       and lr1: "\<forall>e > 0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) proj i) (l1 proj i) < e) sequentially"
  5284       using insert(3) using insert(4) by auto
  5285     have f': "\<forall>n. f (r1 n) proj k \<in> (\<lambda>x. x proj k) ` range f"
  5286       by simp
  5287     have "bounded (range (\<lambda>i. f (r1 i) proj k))"
  5288       by (metis (lifting) bounded_subset f' image_subsetI s')
  5289     then obtain l2 r2 where r2:"subseq r2" and lr2:"((\<lambda>i. f (r1 (r2 i)) proj k) \<longlongrightarrow> l2) sequentially"
  5290       using bounded_imp_convergent_subsequence[of "\<lambda>i. f (r1 i) proj k"]
  5291       by (auto simp: o_def)
  5292     define r where "r = r1 \<circ> r2"
  5293     have r:"subseq r"
  5294       using r1 and r2 unfolding r_def o_def subseq_def by auto
  5295     moreover
  5296     define l where "l = unproj (\<lambda>i. if i = k then l2 else l1 proj i)"
  5297     {
  5298       fix e::real
  5299       assume "e > 0"
  5300       from lr1 \<open>e > 0\<close> have N1: "eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) proj i) (l1 proj i) < e) sequentially"
  5301         by blast
  5302       from lr2 \<open>e > 0\<close> have N2:"eventually (\<lambda>n. dist (f (r1 (r2 n)) proj k) l2 < e) sequentially"
  5303         by (rule tendstoD)
  5304       from r2 N1 have N1': "eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 (r2 n)) proj i) (l1 proj i) < e) sequentially"
  5305         by (rule eventually_subseq)
  5306       have "eventually (\<lambda>n. \<forall>i\<in>(insert k d). dist (f (r n) proj i) (l proj i) < e) sequentially"
  5307         using N1' N2
  5308         by eventually_elim (insert insert.prems, auto simp: l_def r_def o_def proj_unproj)
  5309     }
  5310     ultimately show ?case by auto
  5311   qed
  5312 qed
  5313 
  5314 lemma compact_lemma:
  5315   fixes f :: "nat \<Rightarrow> 'a::euclidean_space"
  5316   assumes "bounded (range f)"
  5317   shows "\<forall>d\<subseteq>Basis. \<exists>l::'a. \<exists> r.
  5318     subseq r \<and> (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) \<bullet> i) (l \<bullet> i) < e) sequentially)"
  5319   by (rule compact_lemma_general[where unproj="\<lambda>e. \<Sum>i\<in>Basis. e i *\<^sub>R i"])
  5320      (auto intro!: assms bounded_linear_inner_left bounded_linear_image
  5321        simp: euclidean_representation)
  5322 
  5323 instance euclidean_space \<subseteq> heine_borel
  5324 proof
  5325   fix f :: "nat \<Rightarrow> 'a"
  5326   assume f: "bounded (range f)"
  5327   then obtain l::'a and r where r: "subseq r"
  5328     and l: "\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>Basis. dist (f (r n) \<bullet> i) (l \<bullet> i) < e) sequentially"
  5329     using compact_lemma [OF f] by blast
  5330   {
  5331     fix e::real
  5332     assume "e > 0"
  5333     hence "e / real_of_nat DIM('a) > 0" by (simp add: DIM_positive)
  5334     with l have "eventually (\<lambda>n. \<forall>i\<in>Basis. dist (f (r n) \<bullet> i) (l \<bullet> i) < e / (real_of_nat DIM('a))) sequentially"
  5335       by simp
  5336     moreover
  5337     {
  5338       fix n
  5339       assume n: "\<forall>i\<in>Basis. dist (f (r n) \<bullet> i) (l \<bullet> i) < e / (real_of_nat DIM('a))"
  5340       have "dist (f (r n)) l \<le> (\<Sum>i\<in>Basis. dist (f (r n) \<bullet> i) (l \<bullet> i))"
  5341         apply (subst euclidean_dist_l2)
  5342         using zero_le_dist
  5343         apply (rule setL2_le_sum)
  5344         done
  5345       also have "\<dots> < (\<Sum>i\<in>(Basis::'a set). e / (real_of_nat DIM('a)))"
  5346         apply (rule sum_strict_mono)
  5347         using n
  5348         apply auto
  5349         done
  5350       finally have "dist (f (r n)) l < e"
  5351         by auto
  5352     }
  5353     ultimately have "eventually (\<lambda>n. dist (f (r n)) l < e) sequentially"
  5354       by (rule eventually_mono)
  5355   }
  5356   then have *: "((f \<circ> r) \<longlongrightarrow> l) sequentially"
  5357     unfolding o_def tendsto_iff by simp
  5358   with r show "\<exists>l r. subseq r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
  5359     by auto
  5360 qed
  5361 
  5362 lemma bounded_fst: "bounded s \<Longrightarrow> bounded (fst ` s)"
  5363   unfolding bounded_def
  5364   by (metis (erased, hide_lams) dist_fst_le image_iff order_trans)
  5365 
  5366 lemma bounded_snd: "bounded s \<Longrightarrow> bounded (snd ` s)"
  5367   unfolding bounded_def
  5368   by (metis (no_types, hide_lams) dist_snd_le image_iff order.trans)
  5369 
  5370 instance prod :: (heine_borel, heine_borel) heine_borel
  5371 proof
  5372   fix f :: "nat \<Rightarrow> 'a \<times> 'b"
  5373   assume f: "bounded (range f)"
  5374   then have "bounded (fst ` range f)"
  5375     by (rule bounded_fst)
  5376   then have s1: "bounded (range (fst \<circ> f))"
  5377     by (simp add: image_comp)
  5378   obtain l1 r1 where r1: "subseq r1" and l1: "(\<lambda>n. fst (f (r1 n))) \<longlonglongrightarrow> l1"
  5379     using bounded_imp_convergent_subsequence [OF s1] unfolding o_def by fast
  5380   from f have s2: "bounded (range (snd \<circ> f \<circ> r1))"
  5381     by (auto simp add: image_comp intro: bounded_snd bounded_subset)
  5382   obtain l2 r2 where r2: "subseq r2" and l2: "((\<lambda>n. snd (f (r1 (r2 n)))) \<longlongrightarrow> l2) sequentially"
  5383     using bounded_imp_convergent_subsequence [OF s2]
  5384     unfolding o_def by fast
  5385   have l1': "((\<lambda>n. fst (f (r1 (r2 n)))) \<longlongrightarrow> l1) sequentially"
  5386     using LIMSEQ_subseq_LIMSEQ [OF l1 r2] unfolding o_def .
  5387   have l: "((f \<circ> (r1 \<circ> r2)) \<longlongrightarrow> (l1, l2)) sequentially"
  5388     using tendsto_Pair [OF l1' l2] unfolding o_def by simp
  5389   have r: "subseq (r1 \<circ> r2)"
  5390     using r1 r2 unfolding subseq_def by simp
  5391   show "\<exists>l r. subseq r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
  5392     using l r by fast
  5393 qed
  5394 
  5395 subsubsection \<open>Intersecting chains of compact sets\<close>
  5396 
  5397 proposition bounded_closed_chain:
  5398   fixes \<F> :: "'a::heine_borel set set"
  5399   assumes "B \<in> \<F>" "bounded B" and \<F>: "\<And>S. S \<in> \<F> \<Longrightarrow> closed S" and "{} \<notin> \<F>"
  5400       and chain: "\<And>S T. S \<in> \<F> \<and> T \<in> \<F> \<Longrightarrow> S \<subseteq> T \<or> T \<subseteq> S"
  5401     shows "\<Inter>\<F> \<noteq> {}"
  5402 proof -
  5403   have "B \<inter> \<Inter>\<F> \<noteq> {}"
  5404   proof (rule compact_imp_fip)
  5405     show "compact B" "\<And>T. T \<in> \<F> \<Longrightarrow> closed T"
  5406       by (simp_all add: assms compact_eq_bounded_closed)
  5407     show "\<lbrakk>finite \<G>; \<G> \<subseteq> \<F>\<rbrakk> \<Longrightarrow> B \<inter> \<Inter>\<G> \<noteq> {}" for \<G>
  5408     proof (induction \<G> rule: finite_induct)
  5409       case empty
  5410       with assms show ?case by force
  5411     next
  5412       case (insert U \<G>)
  5413       then have "U \<in> \<F>" and ne: "B \<inter> \<Inter>\<G> \<noteq> {}" by auto
  5414       then consider "B \<subseteq> U" | "U \<subseteq> B"
  5415           using \<open>B \<in> \<F>\<close> chain by blast
  5416         then show ?case
  5417         proof cases
  5418           case 1
  5419           then show ?thesis
  5420             using Int_left_commute ne by auto
  5421         next
  5422           case 2
  5423           have "U \<noteq> {}"
  5424             using \<open>U \<in> \<F>\<close> \<open>{} \<notin> \<F>\<close> by blast
  5425           moreover
  5426           have False if "\<And>x. x \<in> U \<Longrightarrow> \<exists>Y\<in>\<G>. x \<notin> Y"
  5427           proof -
  5428             have "\<And>x. x \<in> U \<Longrightarrow> \<exists>Y\<in>\<G>. Y \<subseteq> U"
  5429               by (metis chain contra_subsetD insert.prems insert_subset that)
  5430             then obtain Y where "Y \<in> \<G>" "Y \<subseteq> U"
  5431               by (metis all_not_in_conv \<open>U \<noteq> {}\<close>)
  5432             moreover obtain x where "x \<in> \<Inter>\<G>"
  5433               by (metis Int_emptyI ne)
  5434             ultimately show ?thesis
  5435               by (metis Inf_lower subset_eq that)
  5436           qed
  5437           with 2 show ?thesis
  5438             by blast
  5439         qed
  5440       qed
  5441   qed
  5442   then show ?thesis by blast
  5443 qed
  5444 
  5445 corollary compact_chain:
  5446   fixes \<F> :: "'a::heine_borel set set"
  5447   assumes "\<And>S. S \<in> \<F> \<Longrightarrow> compact S" "{} \<notin> \<F>"
  5448           "\<And>S T. S \<in> \<F> \<and> T \<in> \<F> \<Longrightarrow> S \<subseteq> T \<or> T \<subseteq> S"
  5449     shows "\<Inter> \<F> \<noteq> {}"
  5450 proof (cases "\<F> = {}")
  5451   case True
  5452   then show ?thesis by auto
  5453 next
  5454   case False
  5455   show ?thesis
  5456     by (metis False all_not_in_conv assms compact_imp_bounded compact_imp_closed bounded_closed_chain)
  5457 qed
  5458 
  5459 lemma compact_nest:
  5460   fixes F :: "'a::linorder \<Rightarrow> 'b::heine_borel set"
  5461   assumes F: "\<And>n. compact(F n)" "\<And>n. F n \<noteq> {}" and mono: "\<And>m n. m \<le> n \<Longrightarrow> F n \<subseteq> F m"
  5462   shows "\<Inter>range F \<noteq> {}"
  5463 proof -
  5464   have *: "\<And>S T. S \<in> range F \<and> T \<in> range F \<Longrightarrow> S \<subseteq> T \<or> T \<subseteq> S"
  5465     by (metis mono image_iff le_cases)
  5466   show ?thesis
  5467     apply (rule compact_chain [OF _ _ *])
  5468     using F apply (blast intro: dest: *)+
  5469     done
  5470 qed
  5471 
  5472 
  5473 subsubsection \<open>Completeness\<close>
  5474 
  5475 lemma (in metric_space) completeI:
  5476   assumes "\<And>f. \<forall>n. f n \<in> s \<Longrightarrow> Cauchy f \<Longrightarrow> \<exists>l\<in>s. f \<longlonglongrightarrow> l"
  5477   shows "complete s"
  5478   using assms unfolding complete_def by fast
  5479 
  5480 lemma (in metric_space) completeE:
  5481   assumes "complete s" and "\<forall>n. f n \<in> s" and "Cauchy f"
  5482   obtains l where "l \<in> s" and "f \<longlonglongrightarrow> l"
  5483   using assms unfolding complete_def by fast
  5484 
  5485 (* TODO: generalize to uniform spaces *)
  5486 lemma compact_imp_complete:
  5487   fixes s :: "'a::metric_space set"
  5488   assumes "compact s"
  5489   shows "complete s"
  5490 proof -
  5491   {
  5492     fix f
  5493     assume as: "(\<forall>n::nat. f n \<in> s)" "Cauchy f"
  5494     from as(1) obtain l r where lr: "l\<in>s" "subseq r" "(f \<circ> r) \<longlonglongrightarrow> l"
  5495       using assms unfolding compact_def by blast
  5496 
  5497     note lr' = seq_suble [OF lr(2)]
  5498     {
  5499       fix e :: real
  5500       assume "e > 0"
  5501       from as(2) obtain N where N:"\<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (f m) (f n) < e/2"
  5502         unfolding cauchy_def
  5503         using \<open>e > 0\<close>
  5504         apply (erule_tac x="e/2" in allE)
  5505         apply auto
  5506         done
  5507       from lr(3)[unfolded lim_sequentially, THEN spec[where x="e/2"]]
  5508       obtain M where M:"\<forall>n\<ge>M. dist ((f \<circ> r) n) l < e/2"
  5509         using \<open>e > 0\<close> by auto
  5510       {
  5511         fix n :: nat
  5512         assume n: "n \<ge> max N M"
  5513         have "dist ((f \<circ> r) n) l < e/2"
  5514           using n M by auto
  5515         moreover have "r n \<ge> N"
  5516           using lr'[of n] n by auto
  5517         then have "dist (f n) ((f \<circ> r) n) < e / 2"
  5518           using N and n by auto
  5519         ultimately have "dist (f n) l < e"
  5520           using dist_triangle_half_r[of "f (r n)" "f n" e l]
  5521           by (auto simp add: dist_commute)
  5522       }
  5523       then have "\<exists>N. \<forall>n\<ge>N. dist (f n) l < e" by blast
  5524     }
  5525     then have "\<exists>l\<in>s. (f \<longlongrightarrow> l) sequentially" using \<open>l\<in>s\<close>
  5526       unfolding lim_sequentially by auto
  5527   }
  5528   then show ?thesis unfolding complete_def by auto
  5529 qed
  5530 
  5531 lemma nat_approx_posE:
  5532   fixes e::real
  5533   assumes "0 < e"
  5534   obtains n :: nat where "1 / (Suc n) < e"
  5535 proof atomize_elim
  5536   have "1 / real (Suc (nat \<lceil>1/e\<rceil>)) < 1 / \<lceil>1/e\<rceil>"
  5537     by (rule divide_strict_left_mono) (auto simp: \<open>0 < e\<close>)
  5538   also have "1 / \<lceil>1/e\<rceil> \<le> 1 / (1/e)"
  5539     by (rule divide_left_mono) (auto simp: \<open>0 < e\<close> ceiling_correct)
  5540   also have "\<dots> = e" by simp
  5541   finally show  "\<exists>n. 1 / real (Suc n) < e" ..
  5542 qed
  5543 
  5544 lemma compact_eq_totally_bounded:
  5545   "compact s \<longleftrightarrow> complete s \<and> (\<forall>e>0. \<exists>k. finite k \<and> s \<subseteq> (\<Union>x\<in>k. ball x e))"
  5546     (is "_ \<longleftrightarrow> ?rhs")
  5547 proof
  5548   assume assms: "?rhs"
  5549   then obtain k where k: "\<And>e. 0 < e \<Longrightarrow> finite (k e)" "\<And>e. 0 < e \<Longrightarrow> s \<subseteq> (\<Union>x\<in>k e. ball x e)"
  5550     by (auto simp: choice_iff')
  5551 
  5552   show "compact s"
  5553   proof cases
  5554     assume "s = {}"
  5555     then show "compact s" by (simp add: compact_def)
  5556   next
  5557     assume "s \<noteq> {}"
  5558     show ?thesis
  5559       unfolding compact_def
  5560     proof safe
  5561       fix f :: "nat \<Rightarrow> 'a"
  5562       assume f: "\<forall>n. f n \<in> s"
  5563 
  5564       define e where "e n = 1 / (2 * Suc n)" for n
  5565       then have [simp]: "\<And>n. 0 < e n" by auto
  5566       define B where "B n U = (SOME b. infinite {n. f n \<in> b} \<and> (\<exists>x. b \<subseteq> ball x (e n) \<inter> U))" for n U
  5567       {
  5568         fix n U
  5569         assume "infinite {n. f n \<in> U}"
  5570         then have "\<exists>b\<in>k (e n). infinite {i\<in>{n. f n \<in> U}. f i \<in> ball b (e n)}"
  5571           using k f by (intro pigeonhole_infinite_rel) (auto simp: subset_eq)
  5572         then obtain a where
  5573           "a \<in> k (e n)"
  5574           "infinite {i \<in> {n. f n \<in> U}. f i \<in> ball a (e n)}" ..
  5575         then have "\<exists>b. infinite {i. f i \<in> b} \<and> (\<exists>x. b \<subseteq> ball x (e n) \<inter> U)"
  5576           by (intro exI[of _ "ball a (e n) \<inter> U"] exI[of _ a]) (auto simp: ac_simps)
  5577         from someI_ex[OF this]
  5578         have "infinite {i. f i \<in> B n U}" "\<exists>x. B n U \<subseteq> ball x (e n) \<inter> U"
  5579           unfolding B_def by auto
  5580       }
  5581       note B = this
  5582 
  5583       define F where "F = rec_nat (B 0 UNIV) B"
  5584       {
  5585         fix n
  5586         have "infinite {i. f i \<in> F n}"
  5587           by (induct n) (auto simp: F_def B)
  5588       }
  5589       then have F: "\<And>n. \<exists>x. F (Suc n) \<subseteq> ball x (e n) \<inter> F n"
  5590         using B by (simp add: F_def)
  5591       then have F_dec: "\<And>m n. m \<le> n \<Longrightarrow> F n \<subseteq> F m"
  5592         using decseq_SucI[of F] by (auto simp: decseq_def)
  5593 
  5594       obtain sel where sel: "\<And>k i. i < sel k i" "\<And>k i. f (sel k i) \<in> F k"
  5595       proof (atomize_elim, unfold all_conj_distrib[symmetric], intro choice allI)
  5596         fix k i
  5597         have "infinite ({n. f n \<in> F k} - {.. i})"
  5598           using \<open>infinite {n. f n \<in> F k}\<close> by auto
  5599         from infinite_imp_nonempty[OF this]
  5600         show "\<exists>x>i. f x \<in> F k"
  5601           by (simp add: set_eq_iff not_le conj_commute)
  5602       qed
  5603 
  5604       define t where "t = rec_nat (sel 0 0) (\<lambda>n i. sel (Suc n) i)"
  5605       have "subseq t"
  5606         unfolding subseq_Suc_iff by (simp add: t_def sel)
  5607       moreover have "\<forall>i. (f \<circ> t) i \<in> s"
  5608         using f by auto
  5609       moreover
  5610       {
  5611         fix n
  5612         have "(f \<circ> t) n \<in> F n"
  5613           by (cases n) (simp_all add: t_def sel)
  5614       }
  5615       note t = this
  5616 
  5617       have "Cauchy (f \<circ> t)"
  5618       proof (safe intro!: metric_CauchyI exI elim!: nat_approx_posE)
  5619         fix r :: real and N n m
  5620         assume "1 / Suc N < r" "Suc N \<le> n" "Suc N \<le> m"
  5621         then have "(f \<circ> t) n \<in> F (Suc N)" "(f \<circ> t) m \<in> F (Suc N)" "2 * e N < r"
  5622           using F_dec t by (auto simp: e_def field_simps of_nat_Suc)
  5623         with F[of N] obtain x where "dist x ((f \<circ> t) n) < e N" "dist x ((f \<circ> t) m) < e N"
  5624           by (auto simp: subset_eq)
  5625         with dist_triangle[of "(f \<circ> t) m" "(f \<circ> t) n" x] \<open>2 * e N < r\<close>
  5626         show "dist ((f \<circ> t) m) ((f \<circ> t) n) < r"
  5627           by (simp add: dist_commute)
  5628       qed
  5629 
  5630       ultimately show "\<exists>l\<in>s. \<exists>r. subseq r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
  5631         using assms unfolding complete_def by blast
  5632     qed
  5633   qed
  5634 qed (metis compact_imp_complete compact_imp_seq_compact seq_compact_imp_totally_bounded)
  5635 
  5636 lemma cauchy: "Cauchy s \<longleftrightarrow> (\<forall>e>0.\<exists> N::nat. \<forall>n\<ge>N. dist(s n)(s N) < e)" (is "?lhs = ?rhs")
  5637 proof -
  5638   {
  5639     assume ?rhs
  5640     {
  5641       fix e::real
  5642       assume "e>0"
  5643       with \<open>?rhs\<close> obtain N where N:"\<forall>n\<ge>N. dist (s n) (s N) < e/2"
  5644         by (erule_tac x="e/2" in allE) auto
  5645       {
  5646         fix n m
  5647         assume nm:"N \<le> m \<and> N \<le> n"
  5648         then have "dist (s m) (s n) < e" using N
  5649           using dist_triangle_half_l[of "s m" "s N" "e" "s n"]
  5650           by blast
  5651       }
  5652       then have "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (s m) (s n) < e"
  5653         by blast
  5654     }
  5655     then have ?lhs
  5656       unfolding cauchy_def
  5657       by blast
  5658   }
  5659   then show ?thesis
  5660     unfolding cauchy_def
  5661     using dist_triangle_half_l
  5662     by blast
  5663 qed
  5664 
  5665 lemma cauchy_imp_bounded:
  5666   assumes "Cauchy s"
  5667   shows "bounded (range s)"
  5668 proof -
  5669   from assms obtain N :: nat where "\<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (s m) (s n) < 1"
  5670     unfolding cauchy_def
  5671     apply (erule_tac x= 1 in allE)
  5672     apply auto
  5673     done
  5674   then have N:"\<forall>n. N \<le> n \<longrightarrow> dist (s N) (s n) < 1" by auto
  5675   moreover
  5676   have "bounded (s ` {0..N})"
  5677     using finite_imp_bounded[of "s ` {1..N}"] by auto
  5678   then obtain a where a:"\<forall>x\<in>s ` {0..N}. dist (s N) x \<le> a"
  5679     unfolding bounded_any_center [where a="s N"] by auto
  5680   ultimately show "?thesis"
  5681     unfolding bounded_any_center [where a="s N"]
  5682     apply (rule_tac x="max a 1" in exI)
  5683     apply auto
  5684     apply (erule_tac x=y in allE)
  5685     apply (erule_tac x=y in ballE)
  5686     apply auto
  5687     done
  5688 qed
  5689 
  5690 instance heine_borel < complete_space
  5691 proof
  5692   fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
  5693   then have "bounded (range f)"
  5694     by (rule cauchy_imp_bounded)
  5695   then have "compact (closure (range f))"
  5696     unfolding compact_eq_bounded_closed by auto
  5697   then have "complete (closure (range f))"
  5698     by (rule compact_imp_complete)
  5699   moreover have "\<forall>n. f n \<in> closure (range f)"
  5700     using closure_subset [of "range f"] by auto
  5701   ultimately have "\<exists>l\<in>closure (range f). (f \<longlongrightarrow> l) sequentially"
  5702     using \<open>Cauchy f\<close> unfolding complete_def by auto
  5703   then show "convergent f"
  5704     unfolding convergent_def by auto
  5705 qed
  5706 
  5707 instance euclidean_space \<subseteq> banach ..
  5708 
  5709 lemma complete_UNIV: "complete (UNIV :: ('a::complete_space) set)"
  5710 proof (rule completeI)
  5711   fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
  5712   then have "convergent f" by (rule Cauchy_convergent)
  5713   then show "\<exists>l\<in>UNIV. f \<longlonglongrightarrow> l" unfolding convergent_def by simp
  5714 qed
  5715 
  5716 lemma complete_imp_closed:
  5717   fixes S :: "'a::metric_space set"
  5718   assumes "complete S"
  5719   shows "closed S"
  5720 proof (unfold closed_sequential_limits, clarify)
  5721   fix f x assume "\<forall>n. f n \<in> S" and "f \<longlonglongrightarrow> x"
  5722   from \<open>f \<longlonglongrightarrow> x\<close> have "Cauchy f"
  5723     by (rule LIMSEQ_imp_Cauchy)
  5724   with \<open>complete S\<close> and \<open>\<forall>n. f n \<in> S\<close> obtain l where "l \<in> S" and "f \<longlonglongrightarrow> l"
  5725     by (rule completeE)
  5726   from \<open>f \<longlonglongrightarrow> x\<close> and \<open>f \<longlonglongrightarrow> l\<close> have "x = l"
  5727     by (rule LIMSEQ_unique)
  5728   with \<open>l \<in> S\<close> show "x \<in> S"
  5729     by simp
  5730 qed
  5731 
  5732 lemma complete_Int_closed:
  5733   fixes S :: "'a::metric_space set"
  5734   assumes "complete S" and "closed t"
  5735   shows "complete (S \<inter> t)"
  5736 proof (rule completeI)
  5737   fix f assume "\<forall>n. f n \<in> S \<inter> t" and "Cauchy f"
  5738   then have "\<forall>n. f n \<in> S" and "\<forall>n. f n \<in> t"
  5739     by simp_all
  5740   from \<open>complete S\<close> obtain l where "l \<in> S" and "f \<longlonglongrightarrow> l"
  5741     using \<open>\<forall>n. f n \<in> S\<close> and \<open>Cauchy f\<close> by (rule completeE)
  5742   from \<open>closed t\<close> and \<open>\<forall>n. f n \<in> t\<close> and \<open>f \<longlonglongrightarrow> l\<close> have "l \<in> t"
  5743     by (rule closed_sequentially)
  5744   with \<open>l \<in> S\<close> and \<open>f \<longlonglongrightarrow> l\<close> show "\<exists>l\<in>S \<inter> t. f \<longlonglongrightarrow> l"
  5745     by fast
  5746 qed
  5747 
  5748 lemma complete_closed_subset:
  5749   fixes S :: "'a::metric_space set"
  5750   assumes "closed S" and "S \<subseteq> t" and "complete t"
  5751   shows "complete S"
  5752   using assms complete_Int_closed [of t S] by (simp add: Int_absorb1)
  5753 
  5754 lemma complete_eq_closed:
  5755   fixes S :: "('a::complete_space) set"
  5756   shows "complete S \<longleftrightarrow> closed S"
  5757 proof
  5758   assume "closed S" then show "complete S"
  5759     using subset_UNIV complete_UNIV by (rule complete_closed_subset)
  5760 next
  5761   assume "complete S" then show "closed S"
  5762     by (rule complete_imp_closed)
  5763 qed
  5764 
  5765 lemma convergent_eq_Cauchy:
  5766   fixes S :: "nat \<Rightarrow> 'a::complete_space"
  5767   shows "(\<exists>l. (S \<longlongrightarrow> l) sequentially) \<longleftrightarrow> Cauchy S"
  5768   unfolding Cauchy_convergent_iff convergent_def ..
  5769 
  5770 lemma convergent_imp_bounded:
  5771   fixes S :: "nat \<Rightarrow> 'a::metric_space"
  5772   shows "(S \<longlongrightarrow> l) sequentially \<Longrightarrow> bounded (range S)"
  5773   by (intro cauchy_imp_bounded LIMSEQ_imp_Cauchy)
  5774 
  5775 lemma compact_cball[simp]:
  5776   fixes x :: "'a::heine_borel"
  5777   shows "compact (cball x e)"
  5778   using compact_eq_bounded_closed bounded_cball closed_cball
  5779   by blast
  5780 
  5781 lemma compact_frontier_bounded[intro]:
  5782   fixes S :: "'a::heine_borel set"
  5783   shows "bounded S \<Longrightarrow> compact (frontier S)"
  5784   unfolding frontier_def
  5785   using compact_eq_bounded_closed
  5786   by blast
  5787 
  5788 lemma compact_frontier[intro]:
  5789   fixes S :: "'a::heine_borel set"
  5790   shows "compact S \<Longrightarrow> compact (frontier S)"
  5791   using compact_eq_bounded_closed compact_frontier_bounded
  5792   by blast
  5793 
  5794 corollary compact_sphere [simp]:
  5795   fixes a :: "'a::{real_normed_vector,perfect_space,heine_borel}"
  5796   shows "compact (sphere a r)"
  5797 using compact_frontier [of "cball a r"] by simp
  5798 
  5799 corollary bounded_sphere [simp]:
  5800   fixes a :: "'a::{real_normed_vector,perfect_space,heine_borel}"
  5801   shows "bounded (sphere a r)"
  5802 by (simp add: compact_imp_bounded)
  5803 
  5804 corollary closed_sphere  [simp]:
  5805   fixes a :: "'a::{real_normed_vector,perfect_space,heine_borel}"
  5806   shows "closed (sphere a r)"
  5807 by (simp add: compact_imp_closed)
  5808 
  5809 lemma frontier_subset_compact:
  5810   fixes S :: "'a::heine_borel set"
  5811   shows "compact S \<Longrightarrow> frontier S \<subseteq> S"
  5812   using frontier_subset_closed compact_eq_bounded_closed
  5813   by blast
  5814 
  5815 subsection\<open>Relations among convergence and absolute convergence for power series.\<close>
  5816 
  5817 lemma summable_imp_bounded:
  5818   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
  5819   shows "summable f \<Longrightarrow> bounded (range f)"
  5820 by (frule summable_LIMSEQ_zero) (simp add: convergent_imp_bounded)
  5821 
  5822 lemma summable_imp_sums_bounded:
  5823    "summable f \<Longrightarrow> bounded (range (\<lambda>n. sum f {..<n}))"
  5824 by (auto simp: summable_def sums_def dest: convergent_imp_bounded)
  5825 
  5826 lemma power_series_conv_imp_absconv_weak:
  5827   fixes a:: "nat \<Rightarrow> 'a::{real_normed_div_algebra,banach}" and w :: 'a
  5828   assumes sum: "summable (\<lambda>n. a n * z ^ n)" and no: "norm w < norm z"
  5829     shows "summable (\<lambda>n. of_real(norm(a n)) * w ^ n)"
  5830 proof -
  5831   obtain M where M: "\<And>x. norm (a x * z ^ x) \<le> M"
  5832     using summable_imp_bounded [OF sum] by (force simp add: bounded_iff)
  5833   then have *: "summable (\<lambda>n. norm (a n) * norm w ^ n)"
  5834     by (rule_tac M=M in Abel_lemma) (auto simp: norm_mult norm_power intro: no)
  5835   show ?thesis
  5836     apply (rule series_comparison_complex [of "(\<lambda>n. of_real(norm(a n) * norm w ^ n))"])
  5837     apply (simp only: summable_complex_of_real *)
  5838     apply (auto simp: norm_mult norm_power)
  5839     done
  5840 qed
  5841 
  5842 subsection \<open>Bounded closed nest property (proof does not use Heine-Borel)\<close>
  5843 
  5844 lemma bounded_closed_nest:
  5845   fixes s :: "nat \<Rightarrow> ('a::heine_borel) set"
  5846   assumes "\<forall>n. closed (s n)"
  5847     and "\<forall>n. s n \<noteq> {}"
  5848     and "\<forall>m n. m \<le> n \<longrightarrow> s n \<subseteq> s m"
  5849     and "bounded (s 0)"
  5850   shows "\<exists>a. \<forall>n. a \<in> s n"
  5851 proof -
  5852   from assms(2) obtain x where x: "\<forall>n. x n \<in> s n"
  5853     using choice[of "\<lambda>n x. x \<in> s n"] by auto
  5854   from assms(4,1) have "seq_compact (s 0)"
  5855     by (simp add: bounded_closed_imp_seq_compact)
  5856   then obtain l r where lr: "l \<in> s 0" "subseq r" "(x \<circ> r) \<longlonglongrightarrow> l"
  5857     using x and assms(3) unfolding seq_compact_def by blast
  5858   have "\<forall>n. l \<in> s n"
  5859   proof
  5860     fix n :: nat
  5861     have "closed (s n)"
  5862       using assms(1) by simp
  5863     moreover have "\<forall>i. (x \<circ> r) i \<in> s i"
  5864       using x and assms(3) and lr(2) [THEN seq_suble] by auto
  5865     then have "\<forall>i. (x \<circ> r) (i + n) \<in> s n"
  5866       using assms(3) by (fast intro!: le_add2)
  5867     moreover have "(\<lambda>i. (x \<circ> r) (i + n)) \<longlonglongrightarrow> l"
  5868       using lr(3) by (rule LIMSEQ_ignore_initial_segment)
  5869     ultimately show "l \<in> s n"
  5870       by (rule closed_sequentially)
  5871   qed
  5872   then show ?thesis ..
  5873 qed
  5874 
  5875 text \<open>Decreasing case does not even need compactness, just completeness.\<close>
  5876 
  5877 lemma decreasing_closed_nest:
  5878   fixes s :: "nat \<Rightarrow> ('a::complete_space) set"
  5879   assumes
  5880     "\<forall>n. closed (s n)"
  5881     "\<forall>n. s n \<noteq> {}"
  5882     "\<forall>m n. m \<le> n \<longrightarrow> s n \<subseteq> s m"
  5883     "\<forall>e>0. \<exists>n. \<forall>x\<in>s n. \<forall>y\<in>s n. dist x y < e"
  5884   shows "\<exists>a. \<forall>n. a \<in> s n"
  5885 proof -
  5886   have "\<forall>n. \<exists>x. x \<in> s n"
  5887     using assms(2) by auto
  5888   then have "\<exists>t. \<forall>n. t n \<in> s n"
  5889     using choice[of "\<lambda>n x. x \<in> s n"] by auto
  5890   then obtain t where t: "\<forall>n. t n \<in> s n" by auto
  5891   {
  5892     fix e :: real
  5893     assume "e > 0"
  5894     then obtain N where N:"\<forall>x\<in>s N. \<forall>y\<in>s N. dist x y < e"
  5895       using assms(4) by auto
  5896     {
  5897       fix m n :: nat
  5898       assume "N \<le> m \<and> N \<le> n"
  5899       then have "t m \<in> s N" "t n \<in> s N"
  5900         using assms(3) t unfolding  subset_eq t by blast+
  5901       then have "dist (t m) (t n) < e"
  5902         using N by auto
  5903     }
  5904     then have "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (t m) (t n) < e"
  5905       by auto
  5906   }
  5907   then have "Cauchy t"
  5908     unfolding cauchy_def by auto
  5909   then obtain l where l:"(t \<longlongrightarrow> l) sequentially"
  5910     using complete_UNIV unfolding complete_def by auto
  5911   {
  5912     fix n :: nat