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