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