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