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