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