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