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