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