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