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