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