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