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