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