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