src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy
 author hoelzl Thu Jan 17 13:20:17 2013 +0100 (2013-01-17) changeset 50941 3690724028b1 parent 50940 a7c273a83d27 child 50942 1aa1a7991fd9 permissions -rw-r--r--
add countable compacteness; replace finite_range_imp_infinite_repeats by pigeonhole_infinite
     1 (*  title:      HOL/Library/Topology_Euclidian_Space.thy

     2     Author:     Amine Chaieb, University of Cambridge

     3     Author:     Robert Himmelmann, TU Muenchen

     4     Author:     Brian Huffman, Portland State University

     5 *)

     6

     7 header {* Elementary topology in Euclidean space. *}

     8

     9 theory Topology_Euclidean_Space

    10 imports

    11   Complex_Main

    12   "~~/src/HOL/Library/Diagonal_Subsequence"

    13   "~~/src/HOL/Library/Countable_Set"

    14   "~~/src/HOL/Library/Glbs"

    15   "~~/src/HOL/Library/FuncSet"

    16   Linear_Algebra

    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 subsection {* Topological Basis *}

    25

    26 context topological_space

    27 begin

    28

    29 definition "topological_basis B =

    30   ((\<forall>b\<in>B. open b) \<and> (\<forall>x. open x \<longrightarrow> (\<exists>B'. B' \<subseteq> B \<and> Union B' = x)))"

    31

    32 lemma topological_basis_iff:

    33   assumes "\<And>B'. B' \<in> B \<Longrightarrow> open B'"

    34   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'))"

    35     (is "_ \<longleftrightarrow> ?rhs")

    36 proof safe

    37   fix O' and x::'a

    38   assume H: "topological_basis B" "open O'" "x \<in> O'"

    39   hence "(\<exists>B'\<subseteq>B. \<Union>B' = O')" by (simp add: topological_basis_def)

    40   then obtain B' where "B' \<subseteq> B" "O' = \<Union>B'" by auto

    41   thus "\<exists>B'\<in>B. x \<in> B' \<and> B' \<subseteq> O'" using H by auto

    42 next

    43   assume H: ?rhs

    44   show "topological_basis B" using assms unfolding topological_basis_def

    45   proof safe

    46     fix O'::"'a set" assume "open O'"

    47     with H obtain f where "\<forall>x\<in>O'. f x \<in> B \<and> x \<in> f x \<and> f x \<subseteq> O'"

    48       by (force intro: bchoice simp: Bex_def)

    49     thus "\<exists>B'\<subseteq>B. \<Union>B' = O'"

    50       by (auto intro: exI[where x="{f x |x. x \<in> O'}"])

    51   qed

    52 qed

    53

    54 lemma topological_basisI:

    55   assumes "\<And>B'. B' \<in> B \<Longrightarrow> open B'"

    56   assumes "\<And>O' x. open O' \<Longrightarrow> x \<in> O' \<Longrightarrow> \<exists>B'\<in>B. x \<in> B' \<and> B' \<subseteq> O'"

    57   shows "topological_basis B"

    58   using assms by (subst topological_basis_iff) auto

    59

    60 lemma topological_basisE:

    61   fixes O'

    62   assumes "topological_basis B"

    63   assumes "open O'"

    64   assumes "x \<in> O'"

    65   obtains B' where "B' \<in> B" "x \<in> B'" "B' \<subseteq> O'"

    66 proof atomize_elim

    67   from assms have "\<And>B'. B'\<in>B \<Longrightarrow> open B'" by (simp add: topological_basis_def)

    68   with topological_basis_iff assms

    69   show  "\<exists>B'. B' \<in> B \<and> x \<in> B' \<and> B' \<subseteq> O'" using assms by (simp add: Bex_def)

    70 qed

    71

    72 lemma topological_basis_open:

    73   assumes "topological_basis B"

    74   assumes "X \<in> B"

    75   shows "open X"

    76   using assms

    77   by (simp add: topological_basis_def)

    78

    79 lemma basis_dense:

    80   fixes B::"'a set set" and f::"'a set \<Rightarrow> 'a"

    81   assumes "topological_basis B"

    82   assumes choosefrom_basis: "\<And>B'. B' \<noteq> {} \<Longrightarrow> f B' \<in> B'"

    83   shows "(\<forall>X. open X \<longrightarrow> X \<noteq> {} \<longrightarrow> (\<exists>B' \<in> B. f B' \<in> X))"

    84 proof (intro allI impI)

    85   fix X::"'a set" assume "open X" "X \<noteq> {}"

    86   from topological_basisE[OF topological_basis B open X choosefrom_basis[OF X \<noteq> {}]]

    87   guess B' . note B' = this

    88   thus "\<exists>B'\<in>B. f B' \<in> X" by (auto intro!: choosefrom_basis)

    89 qed

    90

    91 end

    92

    93 lemma topological_basis_prod:

    94   assumes A: "topological_basis A" and B: "topological_basis B"

    95   shows "topological_basis ((\<lambda>(a, b). a \<times> b)  (A \<times> B))"

    96   unfolding topological_basis_def

    97 proof (safe, simp_all del: ex_simps add: subset_image_iff ex_simps(1)[symmetric])

    98   fix S :: "('a \<times> 'b) set" assume "open S"

    99   then show "\<exists>X\<subseteq>A \<times> B. (\<Union>(a,b)\<in>X. a \<times> b) = S"

   100   proof (safe intro!: exI[of _ "{x\<in>A \<times> B. fst x \<times> snd x \<subseteq> S}"])

   101     fix x y assume "(x, y) \<in> S"

   102     from open_prod_elim[OF open S this]

   103     obtain a b where a: "open a""x \<in> a" and b: "open b" "y \<in> b" and "a \<times> b \<subseteq> S"

   104       by (metis mem_Sigma_iff)

   105     moreover from topological_basisE[OF A a] guess A0 .

   106     moreover from topological_basisE[OF B b] guess B0 .

   107     ultimately show "(x, y) \<in> (\<Union>(a, b)\<in>{X \<in> A \<times> B. fst X \<times> snd X \<subseteq> S}. a \<times> b)"

   108       by (intro UN_I[of "(A0, B0)"]) auto

   109   qed auto

   110 qed (metis A B topological_basis_open open_Times)

   111

   112 subsection {* Countable Basis *}

   113

   114 locale countable_basis =

   115   fixes B::"'a::topological_space set set"

   116   assumes is_basis: "topological_basis B"

   117   assumes countable_basis: "countable B"

   118 begin

   119

   120 lemma open_countable_basis_ex:

   121   assumes "open X"

   122   shows "\<exists>B' \<subseteq> B. X = Union B'"

   123   using assms countable_basis is_basis unfolding topological_basis_def by blast

   124

   125 lemma open_countable_basisE:

   126   assumes "open X"

   127   obtains B' where "B' \<subseteq> B" "X = Union B'"

   128   using assms open_countable_basis_ex by (atomize_elim) simp

   129

   130 lemma countable_dense_exists:

   131   shows "\<exists>D::'a set. countable D \<and> (\<forall>X. open X \<longrightarrow> X \<noteq> {} \<longrightarrow> (\<exists>d \<in> D. d \<in> X))"

   132 proof -

   133   let ?f = "(\<lambda>B'. SOME x. x \<in> B')"

   134   have "countable (?f  B)" using countable_basis by simp

   135   with basis_dense[OF is_basis, of ?f] show ?thesis

   136     by (intro exI[where x="?f  B"]) (metis (mono_tags) all_not_in_conv imageI someI)

   137 qed

   138

   139 lemma countable_dense_setE:

   140   obtains D :: "'a set"

   141   where "countable D" "\<And>X. open X \<Longrightarrow> X \<noteq> {} \<Longrightarrow> \<exists>d \<in> D. d \<in> X"

   142   using countable_dense_exists by blast

   143

   144 text {* Construction of an increasing sequence approximating open sets,

   145   therefore basis which is closed under union. *}

   146

   147 definition union_closed_basis::"'a set set" where

   148   "union_closed_basis = (\<lambda>l. \<Union>set l)  lists B"

   149

   150 lemma basis_union_closed_basis: "topological_basis union_closed_basis"

   151 proof (rule topological_basisI)

   152   fix O' and x::'a assume "open O'" "x \<in> O'"

   153   from topological_basisE[OF is_basis this] guess B' . note B' = this

   154   thus "\<exists>B'\<in>union_closed_basis. x \<in> B' \<and> B' \<subseteq> O'" unfolding union_closed_basis_def

   155     by (auto intro!: bexI[where x="[B']"])

   156 next

   157   fix B' assume "B' \<in> union_closed_basis"

   158   thus "open B'"

   159     using topological_basis_open[OF is_basis]

   160     by (auto simp: union_closed_basis_def)

   161 qed

   162

   163 lemma countable_union_closed_basis: "countable union_closed_basis"

   164   unfolding union_closed_basis_def using countable_basis by simp

   165

   166 lemmas open_union_closed_basis = topological_basis_open[OF basis_union_closed_basis]

   167

   168 lemma union_closed_basis_ex:

   169  assumes X: "X \<in> union_closed_basis"

   170  shows "\<exists>B'. finite B' \<and> X = \<Union>B' \<and> B' \<subseteq> B"

   171 proof -

   172   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)

   173   thus ?thesis by auto

   174 qed

   175

   176 lemma union_closed_basisE:

   177   assumes "X \<in> union_closed_basis"

   178   obtains B' where "finite B'" "X = \<Union>B'" "B' \<subseteq> B" using union_closed_basis_ex[OF assms] by blast

   179

   180 lemma union_closed_basisI:

   181   assumes "finite B'" "X = \<Union>B'" "B' \<subseteq> B"

   182   shows "X \<in> union_closed_basis"

   183 proof -

   184   from finite_list[OF finite B'] guess l ..

   185   thus ?thesis using assms unfolding union_closed_basis_def by (auto intro!: image_eqI[where x=l])

   186 qed

   187

   188 lemma empty_basisI[intro]: "{} \<in> union_closed_basis"

   189   by (rule union_closed_basisI[of "{}"]) auto

   190

   191 lemma union_basisI[intro]:

   192   assumes "X \<in> union_closed_basis" "Y \<in> union_closed_basis"

   193   shows "X \<union> Y \<in> union_closed_basis"

   194   using assms by (auto intro: union_closed_basisI elim!:union_closed_basisE)

   195

   196 lemma open_imp_Union_of_incseq:

   197   assumes "open X"

   198   shows "\<exists>S. incseq S \<and> (\<Union>j. S j) = X \<and> range S \<subseteq> union_closed_basis"

   199 proof -

   200   from open_countable_basis_ex[OF open X]

   201   obtain B' where B': "B'\<subseteq>B" "X = \<Union>B'" by auto

   202   from this(1) countable_basis have "countable B'" by (rule countable_subset)

   203   show ?thesis

   204   proof cases

   205     assume "B' \<noteq> {}"

   206     def S \<equiv> "\<lambda>n. \<Union>i\<in>{0..n}. from_nat_into B' i"

   207     have S:"\<And>n. S n = \<Union>{from_nat_into B' i|i. i\<in>{0..n}}" unfolding S_def by force

   208     have "incseq S" by (force simp: S_def incseq_Suc_iff)

   209     moreover

   210     have "(\<Union>j. S j) = X" unfolding B'

   211     proof safe

   212       fix x X assume "X \<in> B'" "x \<in> X"

   213       then obtain n where "X = from_nat_into B' n"

   214         by (metis countable B' from_nat_into_surj)

   215       also have "\<dots> \<subseteq> S n" by (auto simp: S_def)

   216       finally show "x \<in> (\<Union>j. S j)" using x \<in> X by auto

   217     next

   218       fix x n

   219       assume "x \<in> S n"

   220       also have "\<dots> = (\<Union>i\<in>{0..n}. from_nat_into B' i)"

   221         by (simp add: S_def)

   222       also have "\<dots> \<subseteq> (\<Union>i. from_nat_into B' i)" by auto

   223       also have "\<dots> \<subseteq> \<Union>B'" using B' \<noteq> {} by (auto intro: from_nat_into)

   224       finally show "x \<in> \<Union>B'" .

   225     qed

   226     moreover have "range S \<subseteq> union_closed_basis" using B'

   227       by (auto intro!: union_closed_basisI[OF _ S] simp: from_nat_into B' \<noteq> {})

   228     ultimately show ?thesis by auto

   229   qed (auto simp: B')

   230 qed

   231

   232 lemma open_incseqE:

   233   assumes "open X"

   234   obtains S where "incseq S" "(\<Union>j. S j) = X" "range S \<subseteq> union_closed_basis"

   235   using open_imp_Union_of_incseq assms by atomize_elim

   236

   237 end

   238

   239 class first_countable_topology = topological_space +

   240   assumes first_countable_basis:

   241     "\<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))"

   242

   243 lemma (in first_countable_topology) countable_basis_at_decseq:

   244   obtains A :: "nat \<Rightarrow> 'a set" where

   245     "\<And>i. open (A i)" "\<And>i. x \<in> (A i)"

   246     "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"

   247 proof atomize_elim

   248   from first_countable_basis[of x] obtain A

   249     where "countable A"

   250     and nhds: "\<And>a. a \<in> A \<Longrightarrow> open a" "\<And>a. a \<in> A \<Longrightarrow> x \<in> a"

   251     and incl: "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>a\<in>A. a \<subseteq> S"  by auto

   252   then have "A \<noteq> {}" by auto

   253   with countable A have r: "A = range (from_nat_into A)" by auto

   254   def F \<equiv> "\<lambda>n. \<Inter>i\<le>n. from_nat_into A i"

   255   show "\<exists>A. (\<forall>i. open (A i)) \<and> (\<forall>i. x \<in> A i) \<and>

   256       (\<forall>S. open S \<longrightarrow> x \<in> S \<longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially)"

   257   proof (safe intro!: exI[of _ F])

   258     fix i

   259     show "open (F i)" using nhds(1) r by (auto simp: F_def intro!: open_INT)

   260     show "x \<in> F i" using nhds(2) r by (auto simp: F_def)

   261   next

   262     fix S assume "open S" "x \<in> S"

   263     from incl[OF this] obtain i where "F i \<subseteq> S"

   264       by (subst (asm) r) (auto simp: F_def)

   265     moreover have "\<And>j. i \<le> j \<Longrightarrow> F j \<subseteq> F i"

   266       by (auto simp: F_def)

   267     ultimately show "eventually (\<lambda>i. F i \<subseteq> S) sequentially"

   268       by (auto simp: eventually_sequentially)

   269   qed

   270 qed

   271

   272 lemma (in first_countable_topology) first_countable_basisE:

   273   obtains A where "countable A" "\<And>a. a \<in> A \<Longrightarrow> x \<in> a" "\<And>a. a \<in> A \<Longrightarrow> open a"

   274     "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> (\<exists>a\<in>A. a \<subseteq> S)"

   275   using first_countable_basis[of x]

   276   by atomize_elim auto

   277

   278 instance prod :: (first_countable_topology, first_countable_topology) first_countable_topology

   279 proof

   280   fix x :: "'a \<times> 'b"

   281   from first_countable_basisE[of "fst x"] guess A :: "'a set set" . note A = this

   282   from first_countable_basisE[of "snd x"] guess B :: "'b set set" . note B = this

   283   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))"

   284   proof (intro exI[of _ "(\<lambda>(a, b). a \<times> b)  (A \<times> B)"], safe)

   285     fix a b assume x: "a \<in> A" "b \<in> B"

   286     with A(2, 3)[of a] B(2, 3)[of b] show "x \<in> a \<times> b" "open (a \<times> b)"

   287       unfolding mem_Times_iff by (auto intro: open_Times)

   288   next

   289     fix S assume "open S" "x \<in> S"

   290     from open_prod_elim[OF this] guess a' b' .

   291     moreover with A(4)[of a'] B(4)[of b']

   292     obtain a b where "a \<in> A" "a \<subseteq> a'" "b \<in> B" "b \<subseteq> b'" by auto

   293     ultimately show "\<exists>a\<in>(\<lambda>(a, b). a \<times> b)  (A \<times> B). a \<subseteq> S"

   294       by (auto intro!: bexI[of _ "a \<times> b"] bexI[of _ a] bexI[of _ b])

   295   qed (simp add: A B)

   296 qed

   297

   298 instance metric_space \<subseteq> first_countable_topology

   299 proof

   300   fix x :: 'a

   301   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))"

   302   proof (intro exI, safe)

   303     fix S assume "open S" "x \<in> S"

   304     then obtain r where "0 < r" "{y. dist x y < r} \<subseteq> S"

   305       by (auto simp: open_dist dist_commute subset_eq)

   306     moreover from reals_Archimedean[OF 0 < r] guess n ..

   307     moreover

   308     then have "{y. dist x y < inverse (Suc n)} \<subseteq> {y. dist x y < r}"

   309       by (auto simp: inverse_eq_divide)

   310     ultimately show "\<exists>a\<in>range (\<lambda>n. {y. dist x y < inverse (Suc n)}). a \<subseteq> S"

   311       by auto

   312   qed (auto intro: open_ball)

   313 qed

   314

   315 class second_countable_topology = topological_space +

   316   assumes ex_countable_basis:

   317     "\<exists>B::'a::topological_space set set. countable B \<and> topological_basis B"

   318

   319 sublocale second_countable_topology < countable_basis "SOME B. countable B \<and> topological_basis B"

   320   using someI_ex[OF ex_countable_basis] by unfold_locales safe

   321

   322 instance prod :: (second_countable_topology, second_countable_topology) second_countable_topology

   323 proof

   324   obtain A :: "'a set set" where "countable A" "topological_basis A"

   325     using ex_countable_basis by auto

   326   moreover

   327   obtain B :: "'b set set" where "countable B" "topological_basis B"

   328     using ex_countable_basis by auto

   329   ultimately show "\<exists>B::('a \<times> 'b) set set. countable B \<and> topological_basis B"

   330     by (auto intro!: exI[of _ "(\<lambda>(a, b). a \<times> b)  (A \<times> B)"] topological_basis_prod)

   331 qed

   332

   333 instance second_countable_topology \<subseteq> first_countable_topology

   334 proof

   335   fix x :: 'a

   336   def B \<equiv> "SOME B::'a set set. countable B \<and> topological_basis B"

   337   then have B: "countable B" "topological_basis B"

   338     using countable_basis is_basis

   339     by (auto simp: countable_basis is_basis)

   340   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))"

   341     by (intro exI[of _ "{b\<in>B. x \<in> b}"])

   342        (fastforce simp: topological_space_class.topological_basis_def)

   343 qed

   344

   345 subsection {* Polish spaces *}

   346

   347 text {* Textbooks define Polish spaces as completely metrizable.

   348   We assume the topology to be complete for a given metric. *}

   349

   350 class polish_space = complete_space + second_countable_topology

   351

   352 subsection {* General notion of a topology as a value *}

   353

   354 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))"

   355 typedef 'a topology = "{L::('a set) \<Rightarrow> bool. istopology L}"

   356   morphisms "openin" "topology"

   357   unfolding istopology_def by blast

   358

   359 lemma istopology_open_in[intro]: "istopology(openin U)"

   360   using openin[of U] by blast

   361

   362 lemma topology_inverse': "istopology U \<Longrightarrow> openin (topology U) = U"

   363   using topology_inverse[unfolded mem_Collect_eq] .

   364

   365 lemma topology_inverse_iff: "istopology U \<longleftrightarrow> openin (topology U) = U"

   366   using topology_inverse[of U] istopology_open_in[of "topology U"] by auto

   367

   368 lemma topology_eq: "T1 = T2 \<longleftrightarrow> (\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S)"

   369 proof-

   370   { assume "T1=T2"

   371     hence "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S" by simp }

   372   moreover

   373   { assume H: "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S"

   374     hence "openin T1 = openin T2" by (simp add: fun_eq_iff)

   375     hence "topology (openin T1) = topology (openin T2)" by simp

   376     hence "T1 = T2" unfolding openin_inverse .

   377   }

   378   ultimately show ?thesis by blast

   379 qed

   380

   381 text{* Infer the "universe" from union of all sets in the topology. *}

   382

   383 definition "topspace T =  \<Union>{S. openin T S}"

   384

   385 subsubsection {* Main properties of open sets *}

   386

   387 lemma openin_clauses:

   388   fixes U :: "'a topology"

   389   shows "openin U {}"

   390   "\<And>S T. openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S\<inter>T)"

   391   "\<And>K. (\<forall>S \<in> K. openin U S) \<Longrightarrow> openin U (\<Union>K)"

   392   using openin[of U] unfolding istopology_def mem_Collect_eq

   393   by fast+

   394

   395 lemma openin_subset[intro]: "openin U S \<Longrightarrow> S \<subseteq> topspace U"

   396   unfolding topspace_def by blast

   397 lemma openin_empty[simp]: "openin U {}" by (simp add: openin_clauses)

   398

   399 lemma openin_Int[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<inter> T)"

   400   using openin_clauses by simp

   401

   402 lemma openin_Union[intro]: "(\<forall>S \<in>K. openin U S) \<Longrightarrow> openin U (\<Union> K)"

   403   using openin_clauses by simp

   404

   405 lemma openin_Un[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<union> T)"

   406   using openin_Union[of "{S,T}" U] by auto

   407

   408 lemma openin_topspace[intro, simp]: "openin U (topspace U)" by (simp add: openin_Union topspace_def)

   409

   410 lemma openin_subopen: "openin U S \<longleftrightarrow> (\<forall>x \<in> S. \<exists>T. openin U T \<and> x \<in> T \<and> T \<subseteq> S)"

   411   (is "?lhs \<longleftrightarrow> ?rhs")

   412 proof

   413   assume ?lhs

   414   then show ?rhs by auto

   415 next

   416   assume H: ?rhs

   417   let ?t = "\<Union>{T. openin U T \<and> T \<subseteq> S}"

   418   have "openin U ?t" by (simp add: openin_Union)

   419   also have "?t = S" using H by auto

   420   finally show "openin U S" .

   421 qed

   422

   423

   424 subsubsection {* Closed sets *}

   425

   426 definition "closedin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> openin U (topspace U - S)"

   427

   428 lemma closedin_subset: "closedin U S \<Longrightarrow> S \<subseteq> topspace U" by (metis closedin_def)

   429 lemma closedin_empty[simp]: "closedin U {}" by (simp add: closedin_def)

   430 lemma closedin_topspace[intro,simp]:

   431   "closedin U (topspace U)" by (simp add: closedin_def)

   432 lemma closedin_Un[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<union> T)"

   433   by (auto simp add: Diff_Un closedin_def)

   434

   435 lemma Diff_Inter[intro]: "A - \<Inter>S = \<Union> {A - s|s. s\<in>S}" by auto

   436 lemma closedin_Inter[intro]: assumes Ke: "K \<noteq> {}" and Kc: "\<forall>S \<in>K. closedin U S"

   437   shows "closedin U (\<Inter> K)"  using Ke Kc unfolding closedin_def Diff_Inter by auto

   438

   439 lemma closedin_Int[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<inter> T)"

   440   using closedin_Inter[of "{S,T}" U] by auto

   441

   442 lemma Diff_Diff_Int: "A - (A - B) = A \<inter> B" by blast

   443 lemma openin_closedin_eq: "openin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> closedin U (topspace U - S)"

   444   apply (auto simp add: closedin_def Diff_Diff_Int inf_absorb2)

   445   apply (metis openin_subset subset_eq)

   446   done

   447

   448 lemma openin_closedin:  "S \<subseteq> topspace U \<Longrightarrow> (openin U S \<longleftrightarrow> closedin U (topspace U - S))"

   449   by (simp add: openin_closedin_eq)

   450

   451 lemma openin_diff[intro]: assumes oS: "openin U S" and cT: "closedin U T" shows "openin U (S - T)"

   452 proof-

   453   have "S - T = S \<inter> (topspace U - T)" using openin_subset[of U S]  oS cT

   454     by (auto simp add: topspace_def openin_subset)

   455   then show ?thesis using oS cT by (auto simp add: closedin_def)

   456 qed

   457

   458 lemma closedin_diff[intro]: assumes oS: "closedin U S" and cT: "openin U T" shows "closedin U (S - T)"

   459 proof-

   460   have "S - T = S \<inter> (topspace U - T)" using closedin_subset[of U S]  oS cT

   461     by (auto simp add: topspace_def )

   462   then show ?thesis using oS cT by (auto simp add: openin_closedin_eq)

   463 qed

   464

   465 subsubsection {* Subspace topology *}

   466

   467 definition "subtopology U V = topology (\<lambda>T. \<exists>S. T = S \<inter> V \<and> openin U S)"

   468

   469 lemma istopology_subtopology: "istopology (\<lambda>T. \<exists>S. T = S \<inter> V \<and> openin U S)"

   470   (is "istopology ?L")

   471 proof-

   472   have "?L {}" by blast

   473   {fix A B assume A: "?L A" and B: "?L B"

   474     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

   475     have "A\<inter>B = (Sa \<inter> Sb) \<inter> V" "openin U (Sa \<inter> Sb)"  using Sa Sb by blast+

   476     then have "?L (A \<inter> B)" by blast}

   477   moreover

   478   {fix K assume K: "K \<subseteq> Collect ?L"

   479     have th0: "Collect ?L = (\<lambda>S. S \<inter> V)  Collect (openin U)"

   480       apply (rule set_eqI)

   481       apply (simp add: Ball_def image_iff)

   482       by metis

   483     from K[unfolded th0 subset_image_iff]

   484     obtain Sk where Sk: "Sk \<subseteq> Collect (openin U)" "K = (\<lambda>S. S \<inter> V)  Sk" by blast

   485     have "\<Union>K = (\<Union>Sk) \<inter> V" using Sk by auto

   486     moreover have "openin U (\<Union> Sk)" using Sk by (auto simp add: subset_eq)

   487     ultimately have "?L (\<Union>K)" by blast}

   488   ultimately show ?thesis

   489     unfolding subset_eq mem_Collect_eq istopology_def by blast

   490 qed

   491

   492 lemma openin_subtopology:

   493   "openin (subtopology U V) S \<longleftrightarrow> (\<exists> T. (openin U T) \<and> (S = T \<inter> V))"

   494   unfolding subtopology_def topology_inverse'[OF istopology_subtopology]

   495   by auto

   496

   497 lemma topspace_subtopology: "topspace(subtopology U V) = topspace U \<inter> V"

   498   by (auto simp add: topspace_def openin_subtopology)

   499

   500 lemma closedin_subtopology:

   501   "closedin (subtopology U V) S \<longleftrightarrow> (\<exists>T. closedin U T \<and> S = T \<inter> V)"

   502   unfolding closedin_def topspace_subtopology

   503   apply (simp add: openin_subtopology)

   504   apply (rule iffI)

   505   apply clarify

   506   apply (rule_tac x="topspace U - T" in exI)

   507   by auto

   508

   509 lemma openin_subtopology_refl: "openin (subtopology U V) V \<longleftrightarrow> V \<subseteq> topspace U"

   510   unfolding openin_subtopology

   511   apply (rule iffI, clarify)

   512   apply (frule openin_subset[of U])  apply blast

   513   apply (rule exI[where x="topspace U"])

   514   apply auto

   515   done

   516

   517 lemma subtopology_superset:

   518   assumes UV: "topspace U \<subseteq> V"

   519   shows "subtopology U V = U"

   520 proof-

   521   {fix S

   522     {fix T assume T: "openin U T" "S = T \<inter> V"

   523       from T openin_subset[OF T(1)] UV have eq: "S = T" by blast

   524       have "openin U S" unfolding eq using T by blast}

   525     moreover

   526     {assume S: "openin U S"

   527       hence "\<exists>T. openin U T \<and> S = T \<inter> V"

   528         using openin_subset[OF S] UV by auto}

   529     ultimately have "(\<exists>T. openin U T \<and> S = T \<inter> V) \<longleftrightarrow> openin U S" by blast}

   530   then show ?thesis unfolding topology_eq openin_subtopology by blast

   531 qed

   532

   533 lemma subtopology_topspace[simp]: "subtopology U (topspace U) = U"

   534   by (simp add: subtopology_superset)

   535

   536 lemma subtopology_UNIV[simp]: "subtopology U UNIV = U"

   537   by (simp add: subtopology_superset)

   538

   539 subsubsection {* The standard Euclidean topology *}

   540

   541 definition

   542   euclidean :: "'a::topological_space topology" where

   543   "euclidean = topology open"

   544

   545 lemma open_openin: "open S \<longleftrightarrow> openin euclidean S"

   546   unfolding euclidean_def

   547   apply (rule cong[where x=S and y=S])

   548   apply (rule topology_inverse[symmetric])

   549   apply (auto simp add: istopology_def)

   550   done

   551

   552 lemma topspace_euclidean: "topspace euclidean = UNIV"

   553   apply (simp add: topspace_def)

   554   apply (rule set_eqI)

   555   by (auto simp add: open_openin[symmetric])

   556

   557 lemma topspace_euclidean_subtopology[simp]: "topspace (subtopology euclidean S) = S"

   558   by (simp add: topspace_euclidean topspace_subtopology)

   559

   560 lemma closed_closedin: "closed S \<longleftrightarrow> closedin euclidean S"

   561   by (simp add: closed_def closedin_def topspace_euclidean open_openin Compl_eq_Diff_UNIV)

   562

   563 lemma open_subopen: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S)"

   564   by (simp add: open_openin openin_subopen[symmetric])

   565

   566 text {* Basic "localization" results are handy for connectedness. *}

   567

   568 lemma openin_open: "openin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. open T \<and> (S = U \<inter> T))"

   569   by (auto simp add: openin_subtopology open_openin[symmetric])

   570

   571 lemma openin_open_Int[intro]: "open S \<Longrightarrow> openin (subtopology euclidean U) (U \<inter> S)"

   572   by (auto simp add: openin_open)

   573

   574 lemma open_openin_trans[trans]:

   575  "open S \<Longrightarrow> open T \<Longrightarrow> T \<subseteq> S \<Longrightarrow> openin (subtopology euclidean S) T"

   576   by (metis Int_absorb1  openin_open_Int)

   577

   578 lemma open_subset:  "S \<subseteq> T \<Longrightarrow> open S \<Longrightarrow> openin (subtopology euclidean T) S"

   579   by (auto simp add: openin_open)

   580

   581 lemma closedin_closed: "closedin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. closed T \<and> S = U \<inter> T)"

   582   by (simp add: closedin_subtopology closed_closedin Int_ac)

   583

   584 lemma closedin_closed_Int: "closed S ==> closedin (subtopology euclidean U) (U \<inter> S)"

   585   by (metis closedin_closed)

   586

   587 lemma closed_closedin_trans: "closed S \<Longrightarrow> closed T \<Longrightarrow> T \<subseteq> S \<Longrightarrow> closedin (subtopology euclidean S) T"

   588   apply (subgoal_tac "S \<inter> T = T" )

   589   apply auto

   590   apply (frule closedin_closed_Int[of T S])

   591   by simp

   592

   593 lemma closed_subset: "S \<subseteq> T \<Longrightarrow> closed S \<Longrightarrow> closedin (subtopology euclidean T) S"

   594   by (auto simp add: closedin_closed)

   595

   596 lemma openin_euclidean_subtopology_iff:

   597   fixes S U :: "'a::metric_space set"

   598   shows "openin (subtopology euclidean U) S

   599   \<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")

   600 proof

   601   assume ?lhs thus ?rhs unfolding openin_open open_dist by blast

   602 next

   603   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}"

   604   have 1: "\<forall>x\<in>T. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> T"

   605     unfolding T_def

   606     apply clarsimp

   607     apply (rule_tac x="d - dist x a" in exI)

   608     apply (clarsimp simp add: less_diff_eq)

   609     apply (erule rev_bexI)

   610     apply (rule_tac x=d in exI, clarify)

   611     apply (erule le_less_trans [OF dist_triangle])

   612     done

   613   assume ?rhs hence 2: "S = U \<inter> T"

   614     unfolding T_def

   615     apply auto

   616     apply (drule (1) bspec, erule rev_bexI)

   617     apply auto

   618     done

   619   from 1 2 show ?lhs

   620     unfolding openin_open open_dist by fast

   621 qed

   622

   623 text {* These "transitivity" results are handy too *}

   624

   625 lemma openin_trans[trans]: "openin (subtopology euclidean T) S \<Longrightarrow> openin (subtopology euclidean U) T

   626   \<Longrightarrow> openin (subtopology euclidean U) S"

   627   unfolding open_openin openin_open by blast

   628

   629 lemma openin_open_trans: "openin (subtopology euclidean T) S \<Longrightarrow> open T \<Longrightarrow> open S"

   630   by (auto simp add: openin_open intro: openin_trans)

   631

   632 lemma closedin_trans[trans]:

   633  "closedin (subtopology euclidean T) S \<Longrightarrow>

   634            closedin (subtopology euclidean U) T

   635            ==> closedin (subtopology euclidean U) S"

   636   by (auto simp add: closedin_closed closed_closedin closed_Inter Int_assoc)

   637

   638 lemma closedin_closed_trans: "closedin (subtopology euclidean T) S \<Longrightarrow> closed T \<Longrightarrow> closed S"

   639   by (auto simp add: closedin_closed intro: closedin_trans)

   640

   641

   642 subsection {* Open and closed balls *}

   643

   644 definition

   645   ball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set" where

   646   "ball x e = {y. dist x y < e}"

   647

   648 definition

   649   cball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set" where

   650   "cball x e = {y. dist x y \<le> e}"

   651

   652 lemma mem_ball [simp]: "y \<in> ball x e \<longleftrightarrow> dist x y < e"

   653   by (simp add: ball_def)

   654

   655 lemma mem_cball [simp]: "y \<in> cball x e \<longleftrightarrow> dist x y \<le> e"

   656   by (simp add: cball_def)

   657

   658 lemma mem_ball_0:

   659   fixes x :: "'a::real_normed_vector"

   660   shows "x \<in> ball 0 e \<longleftrightarrow> norm x < e"

   661   by (simp add: dist_norm)

   662

   663 lemma mem_cball_0:

   664   fixes x :: "'a::real_normed_vector"

   665   shows "x \<in> cball 0 e \<longleftrightarrow> norm x \<le> e"

   666   by (simp add: dist_norm)

   667

   668 lemma centre_in_ball: "x \<in> ball x e \<longleftrightarrow> 0 < e"

   669   by simp

   670

   671 lemma centre_in_cball: "x \<in> cball x e \<longleftrightarrow> 0 \<le> e"

   672   by simp

   673

   674 lemma ball_subset_cball[simp,intro]: "ball x e \<subseteq> cball x e" by (simp add: subset_eq)

   675 lemma subset_ball[intro]: "d <= e ==> ball x d \<subseteq> ball x e" by (simp add: subset_eq)

   676 lemma subset_cball[intro]: "d <= e ==> cball x d \<subseteq> cball x e" by (simp add: subset_eq)

   677 lemma ball_max_Un: "ball a (max r s) = ball a r \<union> ball a s"

   678   by (simp add: set_eq_iff) arith

   679

   680 lemma ball_min_Int: "ball a (min r s) = ball a r \<inter> ball a s"

   681   by (simp add: set_eq_iff)

   682

   683 lemma diff_less_iff: "(a::real) - b > 0 \<longleftrightarrow> a > b"

   684   "(a::real) - b < 0 \<longleftrightarrow> a < b"

   685   "a - b < c \<longleftrightarrow> a < c +b" "a - b > c \<longleftrightarrow> a > c +b" by arith+

   686 lemma diff_le_iff: "(a::real) - b \<ge> 0 \<longleftrightarrow> a \<ge> b" "(a::real) - b \<le> 0 \<longleftrightarrow> a \<le> b"

   687   "a - b \<le> c \<longleftrightarrow> a \<le> c +b" "a - b \<ge> c \<longleftrightarrow> a \<ge> c +b"  by arith+

   688

   689 lemma open_ball[intro, simp]: "open (ball x e)"

   690   unfolding open_dist ball_def mem_Collect_eq Ball_def

   691   unfolding dist_commute

   692   apply clarify

   693   apply (rule_tac x="e - dist xa x" in exI)

   694   using dist_triangle_alt[where z=x]

   695   apply (clarsimp simp add: diff_less_iff)

   696   apply atomize

   697   apply (erule_tac x="y" in allE)

   698   apply (erule_tac x="xa" in allE)

   699   by arith

   700

   701 lemma open_contains_ball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. ball x e \<subseteq> S)"

   702   unfolding open_dist subset_eq mem_ball Ball_def dist_commute ..

   703

   704 lemma openE[elim?]:

   705   assumes "open S" "x\<in>S"

   706   obtains e where "e>0" "ball x e \<subseteq> S"

   707   using assms unfolding open_contains_ball by auto

   708

   709 lemma open_contains_ball_eq: "open S \<Longrightarrow> \<forall>x. x\<in>S \<longleftrightarrow> (\<exists>e>0. ball x e \<subseteq> S)"

   710   by (metis open_contains_ball subset_eq centre_in_ball)

   711

   712 lemma ball_eq_empty[simp]: "ball x e = {} \<longleftrightarrow> e \<le> 0"

   713   unfolding mem_ball set_eq_iff

   714   apply (simp add: not_less)

   715   by (metis zero_le_dist order_trans dist_self)

   716

   717 lemma ball_empty[intro]: "e \<le> 0 ==> ball x e = {}" by simp

   718

   719 lemma euclidean_dist_l2:

   720   fixes x y :: "'a :: euclidean_space"

   721   shows "dist x y = setL2 (\<lambda>i. dist (x \<bullet> i) (y \<bullet> i)) Basis"

   722   unfolding dist_norm norm_eq_sqrt_inner setL2_def

   723   by (subst euclidean_inner) (simp add: power2_eq_square inner_diff_left)

   724

   725 definition "box a b = {x. \<forall>i\<in>Basis. a \<bullet> i < x \<bullet> i \<and> x \<bullet> i < b \<bullet> i}"

   726

   727 lemma rational_boxes:

   728   fixes x :: "'a\<Colon>euclidean_space"

   729   assumes "0 < e"

   730   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"

   731 proof -

   732   def e' \<equiv> "e / (2 * sqrt (real (DIM ('a))))"

   733   then have e: "0 < e'" using assms by (auto intro!: divide_pos_pos simp: DIM_positive)

   734   have "\<forall>i. \<exists>y. y \<in> \<rat> \<and> y < x \<bullet> i \<and> x \<bullet> i - y < e'" (is "\<forall>i. ?th i")

   735   proof

   736     fix i from Rats_dense_in_real[of "x \<bullet> i - e'" "x \<bullet> i"] e show "?th i" by auto

   737   qed

   738   from choice[OF this] guess a .. note a = this

   739   have "\<forall>i. \<exists>y. y \<in> \<rat> \<and> x \<bullet> i < y \<and> y - x \<bullet> i < e'" (is "\<forall>i. ?th i")

   740   proof

   741     fix i from Rats_dense_in_real[of "x \<bullet> i" "x \<bullet> i + e'"] e show "?th i" by auto

   742   qed

   743   from choice[OF this] guess b .. note b = this

   744   let ?a = "\<Sum>i\<in>Basis. a i *\<^sub>R i" and ?b = "\<Sum>i\<in>Basis. b i *\<^sub>R i"

   745   show ?thesis

   746   proof (rule exI[of _ ?a], rule exI[of _ ?b], safe)

   747     fix y :: 'a assume *: "y \<in> box ?a ?b"

   748     have "dist x y = sqrt (\<Sum>i\<in>Basis. (dist (x \<bullet> i) (y \<bullet> i))\<twosuperior>)"

   749       unfolding setL2_def[symmetric] by (rule euclidean_dist_l2)

   750     also have "\<dots> < sqrt (\<Sum>(i::'a)\<in>Basis. e^2 / real (DIM('a)))"

   751     proof (rule real_sqrt_less_mono, rule setsum_strict_mono)

   752       fix i :: "'a" assume i: "i \<in> Basis"

   753       have "a i < y\<bullet>i \<and> y\<bullet>i < b i" using * i by (auto simp: box_def)

   754       moreover have "a i < x\<bullet>i" "x\<bullet>i - a i < e'" using a by auto

   755       moreover have "x\<bullet>i < b i" "b i - x\<bullet>i < e'" using b by auto

   756       ultimately have "\<bar>x\<bullet>i - y\<bullet>i\<bar> < 2 * e'" by auto

   757       then have "dist (x \<bullet> i) (y \<bullet> i) < e/sqrt (real (DIM('a)))"

   758         unfolding e'_def by (auto simp: dist_real_def)

   759       then have "(dist (x \<bullet> i) (y \<bullet> i))\<twosuperior> < (e/sqrt (real (DIM('a))))\<twosuperior>"

   760         by (rule power_strict_mono) auto

   761       then show "(dist (x \<bullet> i) (y \<bullet> i))\<twosuperior> < e\<twosuperior> / real DIM('a)"

   762         by (simp add: power_divide)

   763     qed auto

   764     also have "\<dots> = e" using 0 < e by (simp add: real_eq_of_nat)

   765     finally show "y \<in> ball x e" by (auto simp: ball_def)

   766   qed (insert a b, auto simp: box_def)

   767 qed

   768

   769 lemma open_UNION_box:

   770   fixes M :: "'a\<Colon>euclidean_space set"

   771   assumes "open M"

   772   defines "a' \<equiv> \<lambda>f :: 'a \<Rightarrow> real \<times> real. (\<Sum>(i::'a)\<in>Basis. fst (f i) *\<^sub>R i)"

   773   defines "b' \<equiv> \<lambda>f :: 'a \<Rightarrow> real \<times> real. (\<Sum>(i::'a)\<in>Basis. snd (f i) *\<^sub>R i)"

   774   defines "I \<equiv> {f\<in>Basis \<rightarrow>\<^isub>E \<rat> \<times> \<rat>. box (a' f) (b' f) \<subseteq> M}"

   775   shows "M = (\<Union>f\<in>I. box (a' f) (b' f))"

   776 proof safe

   777   fix x assume "x \<in> M"

   778   obtain e where e: "e > 0" "ball x e \<subseteq> M"

   779     using openE[OF open M x \<in> M] by auto

   780   moreover then obtain a b where ab: "x \<in> box a b"

   781     "\<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"

   782     using rational_boxes[OF e(1)] by metis

   783   ultimately show "x \<in> (\<Union>f\<in>I. box (a' f) (b' f))"

   784      by (intro UN_I[of "\<lambda>i\<in>Basis. (a \<bullet> i, b \<bullet> i)"])

   785         (auto simp: euclidean_representation I_def a'_def b'_def)

   786 qed (auto simp: I_def)

   787

   788 subsection{* Connectedness *}

   789

   790 definition "connected S \<longleftrightarrow>

   791   ~(\<exists>e1 e2. open e1 \<and> open e2 \<and> S \<subseteq> (e1 \<union> e2) \<and> (e1 \<inter> e2 \<inter> S = {})

   792   \<and> ~(e1 \<inter> S = {}) \<and> ~(e2 \<inter> S = {}))"

   793

   794 lemma connected_local:

   795  "connected S \<longleftrightarrow> ~(\<exists>e1 e2.

   796                  openin (subtopology euclidean S) e1 \<and>

   797                  openin (subtopology euclidean S) e2 \<and>

   798                  S \<subseteq> e1 \<union> e2 \<and>

   799                  e1 \<inter> e2 = {} \<and>

   800                  ~(e1 = {}) \<and>

   801                  ~(e2 = {}))"

   802 unfolding connected_def openin_open by (safe, blast+)

   803

   804 lemma exists_diff:

   805   fixes P :: "'a set \<Rightarrow> bool"

   806   shows "(\<exists>S. P(- S)) \<longleftrightarrow> (\<exists>S. P S)" (is "?lhs \<longleftrightarrow> ?rhs")

   807 proof-

   808   {assume "?lhs" hence ?rhs by blast }

   809   moreover

   810   {fix S assume H: "P S"

   811     have "S = - (- S)" by auto

   812     with H have "P (- (- S))" by metis }

   813   ultimately show ?thesis by metis

   814 qed

   815

   816 lemma connected_clopen: "connected S \<longleftrightarrow>

   817         (\<forall>T. openin (subtopology euclidean S) T \<and>

   818             closedin (subtopology euclidean S) T \<longrightarrow> T = {} \<or> T = S)" (is "?lhs \<longleftrightarrow> ?rhs")

   819 proof-

   820   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> {})"

   821     unfolding connected_def openin_open closedin_closed

   822     apply (subst exists_diff) by blast

   823   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> {})"

   824     (is " _ \<longleftrightarrow> \<not> (\<exists>e2 e1. ?P e2 e1)") apply (simp add: closed_def) by metis

   825

   826   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'))"

   827     (is "_ \<longleftrightarrow> \<not> (\<exists>t' t. ?Q t' t)")

   828     unfolding connected_def openin_open closedin_closed by auto

   829   {fix e2

   830     {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)"

   831         by auto}

   832     then have "(\<exists>e1. ?P e2 e1) \<longleftrightarrow> (\<exists>t. ?Q e2 t)" by metis}

   833   then have "\<forall>e2. (\<exists>e1. ?P e2 e1) \<longleftrightarrow> (\<exists>t. ?Q e2 t)" by blast

   834   then show ?thesis unfolding th0 th1 by simp

   835 qed

   836

   837 lemma connected_empty[simp, intro]: "connected {}"

   838   by (simp add: connected_def)

   839

   840

   841 subsection{* Limit points *}

   842

   843 definition (in topological_space)

   844   islimpt:: "'a \<Rightarrow> 'a set \<Rightarrow> bool" (infixr "islimpt" 60) where

   845   "x islimpt S \<longleftrightarrow> (\<forall>T. x\<in>T \<longrightarrow> open T \<longrightarrow> (\<exists>y\<in>S. y\<in>T \<and> y\<noteq>x))"

   846

   847 lemma islimptI:

   848   assumes "\<And>T. x \<in> T \<Longrightarrow> open T \<Longrightarrow> \<exists>y\<in>S. y \<in> T \<and> y \<noteq> x"

   849   shows "x islimpt S"

   850   using assms unfolding islimpt_def by auto

   851

   852 lemma islimptE:

   853   assumes "x islimpt S" and "x \<in> T" and "open T"

   854   obtains y where "y \<in> S" and "y \<in> T" and "y \<noteq> x"

   855   using assms unfolding islimpt_def by auto

   856

   857 lemma islimpt_iff_eventually: "x islimpt S \<longleftrightarrow> \<not> eventually (\<lambda>y. y \<notin> S) (at x)"

   858   unfolding islimpt_def eventually_at_topological by auto

   859

   860 lemma islimpt_subset: "\<lbrakk>x islimpt S; S \<subseteq> T\<rbrakk> \<Longrightarrow> x islimpt T"

   861   unfolding islimpt_def by fast

   862

   863 lemma islimpt_approachable:

   864   fixes x :: "'a::metric_space"

   865   shows "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e)"

   866   unfolding islimpt_iff_eventually eventually_at by fast

   867

   868 lemma islimpt_approachable_le:

   869   fixes x :: "'a::metric_space"

   870   shows "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in> S. x' \<noteq> x \<and> dist x' x <= e)"

   871   unfolding islimpt_approachable

   872   using approachable_lt_le [where f="\<lambda>y. dist y x" and P="\<lambda>y. y \<notin> S \<or> y = x",

   873     THEN arg_cong [where f=Not]]

   874   by (simp add: Bex_def conj_commute conj_left_commute)

   875

   876 lemma islimpt_UNIV_iff: "x islimpt UNIV \<longleftrightarrow> \<not> open {x}"

   877   unfolding islimpt_def by (safe, fast, case_tac "T = {x}", fast, fast)

   878

   879 text {* A perfect space has no isolated points. *}

   880

   881 lemma islimpt_UNIV [simp, intro]: "(x::'a::perfect_space) islimpt UNIV"

   882   unfolding islimpt_UNIV_iff by (rule not_open_singleton)

   883

   884 lemma perfect_choose_dist:

   885   fixes x :: "'a::{perfect_space, metric_space}"

   886   shows "0 < r \<Longrightarrow> \<exists>a. a \<noteq> x \<and> dist a x < r"

   887 using islimpt_UNIV [of x]

   888 by (simp add: islimpt_approachable)

   889

   890 lemma closed_limpt: "closed S \<longleftrightarrow> (\<forall>x. x islimpt S \<longrightarrow> x \<in> S)"

   891   unfolding closed_def

   892   apply (subst open_subopen)

   893   apply (simp add: islimpt_def subset_eq)

   894   by (metis ComplE ComplI)

   895

   896 lemma islimpt_EMPTY[simp]: "\<not> x islimpt {}"

   897   unfolding islimpt_def by auto

   898

   899 lemma finite_set_avoid:

   900   fixes a :: "'a::metric_space"

   901   assumes fS: "finite S" shows  "\<exists>d>0. \<forall>x\<in>S. x \<noteq> a \<longrightarrow> d <= dist a x"

   902 proof(induct rule: finite_induct[OF fS])

   903   case 1 thus ?case by (auto intro: zero_less_one)

   904 next

   905   case (2 x F)

   906   from 2 obtain d where d: "d >0" "\<forall>x\<in>F. x\<noteq>a \<longrightarrow> d \<le> dist a x" by blast

   907   {assume "x = a" hence ?case using d by auto  }

   908   moreover

   909   {assume xa: "x\<noteq>a"

   910     let ?d = "min d (dist a x)"

   911     have dp: "?d > 0" using xa d(1) using dist_nz by auto

   912     from d have d': "\<forall>x\<in>F. x\<noteq>a \<longrightarrow> ?d \<le> dist a x" by auto

   913     with dp xa have ?case by(auto intro!: exI[where x="?d"]) }

   914   ultimately show ?case by blast

   915 qed

   916

   917 lemma islimpt_Un: "x islimpt (S \<union> T) \<longleftrightarrow> x islimpt S \<or> x islimpt T"

   918   by (simp add: islimpt_iff_eventually eventually_conj_iff)

   919

   920 lemma discrete_imp_closed:

   921   fixes S :: "'a::metric_space set"

   922   assumes e: "0 < e" and d: "\<forall>x \<in> S. \<forall>y \<in> S. dist y x < e \<longrightarrow> y = x"

   923   shows "closed S"

   924 proof-

   925   {fix x assume C: "\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e"

   926     from e have e2: "e/2 > 0" by arith

   927     from C[rule_format, OF e2] obtain y where y: "y \<in> S" "y\<noteq>x" "dist y x < e/2" by blast

   928     let ?m = "min (e/2) (dist x y) "

   929     from e2 y(2) have mp: "?m > 0" by (simp add: dist_nz[THEN sym])

   930     from C[rule_format, OF mp] obtain z where z: "z \<in> S" "z\<noteq>x" "dist z x < ?m" by blast

   931     have th: "dist z y < e" using z y

   932       by (intro dist_triangle_lt [where z=x], simp)

   933     from d[rule_format, OF y(1) z(1) th] y z

   934     have False by (auto simp add: dist_commute)}

   935   then show ?thesis by (metis islimpt_approachable closed_limpt [where 'a='a])

   936 qed

   937

   938

   939 subsection {* Interior of a Set *}

   940

   941 definition "interior S = \<Union>{T. open T \<and> T \<subseteq> S}"

   942

   943 lemma interiorI [intro?]:

   944   assumes "open T" and "x \<in> T" and "T \<subseteq> S"

   945   shows "x \<in> interior S"

   946   using assms unfolding interior_def by fast

   947

   948 lemma interiorE [elim?]:

   949   assumes "x \<in> interior S"

   950   obtains T where "open T" and "x \<in> T" and "T \<subseteq> S"

   951   using assms unfolding interior_def by fast

   952

   953 lemma open_interior [simp, intro]: "open (interior S)"

   954   by (simp add: interior_def open_Union)

   955

   956 lemma interior_subset: "interior S \<subseteq> S"

   957   by (auto simp add: interior_def)

   958

   959 lemma interior_maximal: "T \<subseteq> S \<Longrightarrow> open T \<Longrightarrow> T \<subseteq> interior S"

   960   by (auto simp add: interior_def)

   961

   962 lemma interior_open: "open S \<Longrightarrow> interior S = S"

   963   by (intro equalityI interior_subset interior_maximal subset_refl)

   964

   965 lemma interior_eq: "interior S = S \<longleftrightarrow> open S"

   966   by (metis open_interior interior_open)

   967

   968 lemma open_subset_interior: "open S \<Longrightarrow> S \<subseteq> interior T \<longleftrightarrow> S \<subseteq> T"

   969   by (metis interior_maximal interior_subset subset_trans)

   970

   971 lemma interior_empty [simp]: "interior {} = {}"

   972   using open_empty by (rule interior_open)

   973

   974 lemma interior_UNIV [simp]: "interior UNIV = UNIV"

   975   using open_UNIV by (rule interior_open)

   976

   977 lemma interior_interior [simp]: "interior (interior S) = interior S"

   978   using open_interior by (rule interior_open)

   979

   980 lemma interior_mono: "S \<subseteq> T \<Longrightarrow> interior S \<subseteq> interior T"

   981   by (auto simp add: interior_def)

   982

   983 lemma interior_unique:

   984   assumes "T \<subseteq> S" and "open T"

   985   assumes "\<And>T'. T' \<subseteq> S \<Longrightarrow> open T' \<Longrightarrow> T' \<subseteq> T"

   986   shows "interior S = T"

   987   by (intro equalityI assms interior_subset open_interior interior_maximal)

   988

   989 lemma interior_inter [simp]: "interior (S \<inter> T) = interior S \<inter> interior T"

   990   by (intro equalityI Int_mono Int_greatest interior_mono Int_lower1

   991     Int_lower2 interior_maximal interior_subset open_Int open_interior)

   992

   993 lemma mem_interior: "x \<in> interior S \<longleftrightarrow> (\<exists>e>0. ball x e \<subseteq> S)"

   994   using open_contains_ball_eq [where S="interior S"]

   995   by (simp add: open_subset_interior)

   996

   997 lemma interior_limit_point [intro]:

   998   fixes x :: "'a::perfect_space"

   999   assumes x: "x \<in> interior S" shows "x islimpt S"

  1000   using x islimpt_UNIV [of x]

  1001   unfolding interior_def islimpt_def

  1002   apply (clarsimp, rename_tac T T')

  1003   apply (drule_tac x="T \<inter> T'" in spec)

  1004   apply (auto simp add: open_Int)

  1005   done

  1006

  1007 lemma interior_closed_Un_empty_interior:

  1008   assumes cS: "closed S" and iT: "interior T = {}"

  1009   shows "interior (S \<union> T) = interior S"

  1010 proof

  1011   show "interior S \<subseteq> interior (S \<union> T)"

  1012     by (rule interior_mono, rule Un_upper1)

  1013 next

  1014   show "interior (S \<union> T) \<subseteq> interior S"

  1015   proof

  1016     fix x assume "x \<in> interior (S \<union> T)"

  1017     then obtain R where "open R" "x \<in> R" "R \<subseteq> S \<union> T" ..

  1018     show "x \<in> interior S"

  1019     proof (rule ccontr)

  1020       assume "x \<notin> interior S"

  1021       with x \<in> R open R obtain y where "y \<in> R - S"

  1022         unfolding interior_def by fast

  1023       from open R closed S have "open (R - S)" by (rule open_Diff)

  1024       from R \<subseteq> S \<union> T have "R - S \<subseteq> T" by fast

  1025       from y \<in> R - S open (R - S) R - S \<subseteq> T interior T = {}

  1026       show "False" unfolding interior_def by fast

  1027     qed

  1028   qed

  1029 qed

  1030

  1031 lemma interior_Times: "interior (A \<times> B) = interior A \<times> interior B"

  1032 proof (rule interior_unique)

  1033   show "interior A \<times> interior B \<subseteq> A \<times> B"

  1034     by (intro Sigma_mono interior_subset)

  1035   show "open (interior A \<times> interior B)"

  1036     by (intro open_Times open_interior)

  1037   fix T assume "T \<subseteq> A \<times> B" and "open T" thus "T \<subseteq> interior A \<times> interior B"

  1038   proof (safe)

  1039     fix x y assume "(x, y) \<in> T"

  1040     then obtain C D where "open C" "open D" "C \<times> D \<subseteq> T" "x \<in> C" "y \<in> D"

  1041       using open T unfolding open_prod_def by fast

  1042     hence "open C" "open D" "C \<subseteq> A" "D \<subseteq> B" "x \<in> C" "y \<in> D"

  1043       using T \<subseteq> A \<times> B by auto

  1044     thus "x \<in> interior A" and "y \<in> interior B"

  1045       by (auto intro: interiorI)

  1046   qed

  1047 qed

  1048

  1049

  1050 subsection {* Closure of a Set *}

  1051

  1052 definition "closure S = S \<union> {x | x. x islimpt S}"

  1053

  1054 lemma interior_closure: "interior S = - (closure (- S))"

  1055   unfolding interior_def closure_def islimpt_def by auto

  1056

  1057 lemma closure_interior: "closure S = - interior (- S)"

  1058   unfolding interior_closure by simp

  1059

  1060 lemma closed_closure[simp, intro]: "closed (closure S)"

  1061   unfolding closure_interior by (simp add: closed_Compl)

  1062

  1063 lemma closure_subset: "S \<subseteq> closure S"

  1064   unfolding closure_def by simp

  1065

  1066 lemma closure_hull: "closure S = closed hull S"

  1067   unfolding hull_def closure_interior interior_def by auto

  1068

  1069 lemma closure_eq: "closure S = S \<longleftrightarrow> closed S"

  1070   unfolding closure_hull using closed_Inter by (rule hull_eq)

  1071

  1072 lemma closure_closed [simp]: "closed S \<Longrightarrow> closure S = S"

  1073   unfolding closure_eq .

  1074

  1075 lemma closure_closure [simp]: "closure (closure S) = closure S"

  1076   unfolding closure_hull by (rule hull_hull)

  1077

  1078 lemma closure_mono: "S \<subseteq> T \<Longrightarrow> closure S \<subseteq> closure T"

  1079   unfolding closure_hull by (rule hull_mono)

  1080

  1081 lemma closure_minimal: "S \<subseteq> T \<Longrightarrow> closed T \<Longrightarrow> closure S \<subseteq> T"

  1082   unfolding closure_hull by (rule hull_minimal)

  1083

  1084 lemma closure_unique:

  1085   assumes "S \<subseteq> T" and "closed T"

  1086   assumes "\<And>T'. S \<subseteq> T' \<Longrightarrow> closed T' \<Longrightarrow> T \<subseteq> T'"

  1087   shows "closure S = T"

  1088   using assms unfolding closure_hull by (rule hull_unique)

  1089

  1090 lemma closure_empty [simp]: "closure {} = {}"

  1091   using closed_empty by (rule closure_closed)

  1092

  1093 lemma closure_UNIV [simp]: "closure UNIV = UNIV"

  1094   using closed_UNIV by (rule closure_closed)

  1095

  1096 lemma closure_union [simp]: "closure (S \<union> T) = closure S \<union> closure T"

  1097   unfolding closure_interior by simp

  1098

  1099 lemma closure_eq_empty: "closure S = {} \<longleftrightarrow> S = {}"

  1100   using closure_empty closure_subset[of S]

  1101   by blast

  1102

  1103 lemma closure_subset_eq: "closure S \<subseteq> S \<longleftrightarrow> closed S"

  1104   using closure_eq[of S] closure_subset[of S]

  1105   by simp

  1106

  1107 lemma open_inter_closure_eq_empty:

  1108   "open S \<Longrightarrow> (S \<inter> closure T) = {} \<longleftrightarrow> S \<inter> T = {}"

  1109   using open_subset_interior[of S "- T"]

  1110   using interior_subset[of "- T"]

  1111   unfolding closure_interior

  1112   by auto

  1113

  1114 lemma open_inter_closure_subset:

  1115   "open S \<Longrightarrow> (S \<inter> (closure T)) \<subseteq> closure(S \<inter> T)"

  1116 proof

  1117   fix x

  1118   assume as: "open S" "x \<in> S \<inter> closure T"

  1119   { assume *:"x islimpt T"

  1120     have "x islimpt (S \<inter> T)"

  1121     proof (rule islimptI)

  1122       fix A

  1123       assume "x \<in> A" "open A"

  1124       with as have "x \<in> A \<inter> S" "open (A \<inter> S)"

  1125         by (simp_all add: open_Int)

  1126       with * obtain y where "y \<in> T" "y \<in> A \<inter> S" "y \<noteq> x"

  1127         by (rule islimptE)

  1128       hence "y \<in> S \<inter> T" "y \<in> A \<and> y \<noteq> x"

  1129         by simp_all

  1130       thus "\<exists>y\<in>(S \<inter> T). y \<in> A \<and> y \<noteq> x" ..

  1131     qed

  1132   }

  1133   then show "x \<in> closure (S \<inter> T)" using as

  1134     unfolding closure_def

  1135     by blast

  1136 qed

  1137

  1138 lemma closure_complement: "closure (- S) = - interior S"

  1139   unfolding closure_interior by simp

  1140

  1141 lemma interior_complement: "interior (- S) = - closure S"

  1142   unfolding closure_interior by simp

  1143

  1144 lemma closure_Times: "closure (A \<times> B) = closure A \<times> closure B"

  1145 proof (rule closure_unique)

  1146   show "A \<times> B \<subseteq> closure A \<times> closure B"

  1147     by (intro Sigma_mono closure_subset)

  1148   show "closed (closure A \<times> closure B)"

  1149     by (intro closed_Times closed_closure)

  1150   fix T assume "A \<times> B \<subseteq> T" and "closed T" thus "closure A \<times> closure B \<subseteq> T"

  1151     apply (simp add: closed_def open_prod_def, clarify)

  1152     apply (rule ccontr)

  1153     apply (drule_tac x="(a, b)" in bspec, simp, clarify, rename_tac C D)

  1154     apply (simp add: closure_interior interior_def)

  1155     apply (drule_tac x=C in spec)

  1156     apply (drule_tac x=D in spec)

  1157     apply auto

  1158     done

  1159 qed

  1160

  1161

  1162 subsection {* Frontier (aka boundary) *}

  1163

  1164 definition "frontier S = closure S - interior S"

  1165

  1166 lemma frontier_closed: "closed(frontier S)"

  1167   by (simp add: frontier_def closed_Diff)

  1168

  1169 lemma frontier_closures: "frontier S = (closure S) \<inter> (closure(- S))"

  1170   by (auto simp add: frontier_def interior_closure)

  1171

  1172 lemma frontier_straddle:

  1173   fixes a :: "'a::metric_space"

  1174   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))"

  1175   unfolding frontier_def closure_interior

  1176   by (auto simp add: mem_interior subset_eq ball_def)

  1177

  1178 lemma frontier_subset_closed: "closed S \<Longrightarrow> frontier S \<subseteq> S"

  1179   by (metis frontier_def closure_closed Diff_subset)

  1180

  1181 lemma frontier_empty[simp]: "frontier {} = {}"

  1182   by (simp add: frontier_def)

  1183

  1184 lemma frontier_subset_eq: "frontier S \<subseteq> S \<longleftrightarrow> closed S"

  1185 proof-

  1186   { assume "frontier S \<subseteq> S"

  1187     hence "closure S \<subseteq> S" using interior_subset unfolding frontier_def by auto

  1188     hence "closed S" using closure_subset_eq by auto

  1189   }

  1190   thus ?thesis using frontier_subset_closed[of S] ..

  1191 qed

  1192

  1193 lemma frontier_complement: "frontier(- S) = frontier S"

  1194   by (auto simp add: frontier_def closure_complement interior_complement)

  1195

  1196 lemma frontier_disjoint_eq: "frontier S \<inter> S = {} \<longleftrightarrow> open S"

  1197   using frontier_complement frontier_subset_eq[of "- S"]

  1198   unfolding open_closed by auto

  1199

  1200 subsection {* Filters and the eventually true'' quantifier *}

  1201

  1202 definition

  1203   indirection :: "'a::real_normed_vector \<Rightarrow> 'a \<Rightarrow> 'a filter"

  1204     (infixr "indirection" 70) where

  1205   "a indirection v = (at a) within {b. \<exists>c\<ge>0. b - a = scaleR c v}"

  1206

  1207 text {* Identify Trivial limits, where we can't approach arbitrarily closely. *}

  1208

  1209 lemma trivial_limit_within:

  1210   shows "trivial_limit (at a within S) \<longleftrightarrow> \<not> a islimpt S"

  1211 proof

  1212   assume "trivial_limit (at a within S)"

  1213   thus "\<not> a islimpt S"

  1214     unfolding trivial_limit_def

  1215     unfolding eventually_within eventually_at_topological

  1216     unfolding islimpt_def

  1217     apply (clarsimp simp add: set_eq_iff)

  1218     apply (rename_tac T, rule_tac x=T in exI)

  1219     apply (clarsimp, drule_tac x=y in bspec, simp_all)

  1220     done

  1221 next

  1222   assume "\<not> a islimpt S"

  1223   thus "trivial_limit (at a within S)"

  1224     unfolding trivial_limit_def

  1225     unfolding eventually_within eventually_at_topological

  1226     unfolding islimpt_def

  1227     apply clarsimp

  1228     apply (rule_tac x=T in exI)

  1229     apply auto

  1230     done

  1231 qed

  1232

  1233 lemma trivial_limit_at_iff: "trivial_limit (at a) \<longleftrightarrow> \<not> a islimpt UNIV"

  1234   using trivial_limit_within [of a UNIV] by simp

  1235

  1236 lemma trivial_limit_at:

  1237   fixes a :: "'a::perfect_space"

  1238   shows "\<not> trivial_limit (at a)"

  1239   by (rule at_neq_bot)

  1240

  1241 lemma trivial_limit_at_infinity:

  1242   "\<not> trivial_limit (at_infinity :: ('a::{real_normed_vector,perfect_space}) filter)"

  1243   unfolding trivial_limit_def eventually_at_infinity

  1244   apply clarsimp

  1245   apply (subgoal_tac "\<exists>x::'a. x \<noteq> 0", clarify)

  1246    apply (rule_tac x="scaleR (b / norm x) x" in exI, simp)

  1247   apply (cut_tac islimpt_UNIV [of "0::'a", unfolded islimpt_def])

  1248   apply (drule_tac x=UNIV in spec, simp)

  1249   done

  1250

  1251 text {* Some property holds "sufficiently close" to the limit point. *}

  1252

  1253 lemma eventually_at: (* FIXME: this replaces Limits.eventually_at *)

  1254   "eventually P (at a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. 0 < dist x a \<and> dist x a < d \<longrightarrow> P x)"

  1255 unfolding eventually_at dist_nz by auto

  1256

  1257 lemma eventually_within: (* FIXME: this replaces Limits.eventually_within *)

  1258   "eventually P (at a within S) \<longleftrightarrow>

  1259         (\<exists>d>0. \<forall>x\<in>S. 0 < dist x a \<and> dist x a < d \<longrightarrow> P x)"

  1260   by (rule eventually_within_less)

  1261

  1262 lemma eventually_happens: "eventually P net ==> trivial_limit net \<or> (\<exists>x. P x)"

  1263   unfolding trivial_limit_def

  1264   by (auto elim: eventually_rev_mp)

  1265

  1266 lemma trivial_limit_eventually: "trivial_limit net \<Longrightarrow> eventually P net"

  1267   by simp

  1268

  1269 lemma trivial_limit_eq: "trivial_limit net \<longleftrightarrow> (\<forall>P. eventually P net)"

  1270   by (simp add: filter_eq_iff)

  1271

  1272 text{* Combining theorems for "eventually" *}

  1273

  1274 lemma eventually_rev_mono:

  1275   "eventually P net \<Longrightarrow> (\<forall>x. P x \<longrightarrow> Q x) \<Longrightarrow> eventually Q net"

  1276 using eventually_mono [of P Q] by fast

  1277

  1278 lemma not_eventually: "(\<forall>x. \<not> P x ) \<Longrightarrow> ~(trivial_limit net) ==> ~(eventually (\<lambda>x. P x) net)"

  1279   by (simp add: eventually_False)

  1280

  1281

  1282 subsection {* Limits *}

  1283

  1284 text{* Notation Lim to avoid collition with lim defined in analysis *}

  1285

  1286 definition Lim :: "'a filter \<Rightarrow> ('a \<Rightarrow> 'b::t2_space) \<Rightarrow> 'b"

  1287   where "Lim A f = (THE l. (f ---> l) A)"

  1288

  1289 lemma Lim:

  1290  "(f ---> l) net \<longleftrightarrow>

  1291         trivial_limit net \<or>

  1292         (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) net)"

  1293   unfolding tendsto_iff trivial_limit_eq by auto

  1294

  1295 text{* Show that they yield usual definitions in the various cases. *}

  1296

  1297 lemma Lim_within_le: "(f ---> l)(at a within S) \<longleftrightarrow>

  1298            (\<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)"

  1299   by (auto simp add: tendsto_iff eventually_within_le)

  1300

  1301 lemma Lim_within: "(f ---> l) (at a within S) \<longleftrightarrow>

  1302         (\<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)"

  1303   by (auto simp add: tendsto_iff eventually_within)

  1304

  1305 lemma Lim_at: "(f ---> l) (at a) \<longleftrightarrow>

  1306         (\<forall>e >0. \<exists>d>0. \<forall>x. 0 < dist x a  \<and> dist x a  < d  \<longrightarrow> dist (f x) l < e)"

  1307   by (auto simp add: tendsto_iff eventually_at)

  1308

  1309 lemma Lim_at_infinity:

  1310   "(f ---> l) at_infinity \<longleftrightarrow> (\<forall>e>0. \<exists>b. \<forall>x. norm x >= b \<longrightarrow> dist (f x) l < e)"

  1311   by (auto simp add: tendsto_iff eventually_at_infinity)

  1312

  1313 lemma Lim_eventually: "eventually (\<lambda>x. f x = l) net \<Longrightarrow> (f ---> l) net"

  1314   by (rule topological_tendstoI, auto elim: eventually_rev_mono)

  1315

  1316 text{* The expected monotonicity property. *}

  1317

  1318 lemma Lim_within_empty: "(f ---> l) (net within {})"

  1319   unfolding tendsto_def Limits.eventually_within by simp

  1320

  1321 lemma Lim_within_subset: "(f ---> l) (net within S) \<Longrightarrow> T \<subseteq> S \<Longrightarrow> (f ---> l) (net within T)"

  1322   unfolding tendsto_def Limits.eventually_within

  1323   by (auto elim!: eventually_elim1)

  1324

  1325 lemma Lim_Un: assumes "(f ---> l) (net within S)" "(f ---> l) (net within T)"

  1326   shows "(f ---> l) (net within (S \<union> T))"

  1327   using assms unfolding tendsto_def Limits.eventually_within

  1328   apply clarify

  1329   apply (drule spec, drule (1) mp, drule (1) mp)

  1330   apply (drule spec, drule (1) mp, drule (1) mp)

  1331   apply (auto elim: eventually_elim2)

  1332   done

  1333

  1334 lemma Lim_Un_univ:

  1335  "(f ---> l) (net within S) \<Longrightarrow> (f ---> l) (net within T) \<Longrightarrow>  S \<union> T = UNIV

  1336         ==> (f ---> l) net"

  1337   by (metis Lim_Un within_UNIV)

  1338

  1339 text{* Interrelations between restricted and unrestricted limits. *}

  1340

  1341 lemma Lim_at_within: "(f ---> l) net ==> (f ---> l)(net within S)"

  1342   (* FIXME: rename *)

  1343   unfolding tendsto_def Limits.eventually_within

  1344   apply (clarify, drule spec, drule (1) mp, drule (1) mp)

  1345   by (auto elim!: eventually_elim1)

  1346

  1347 lemma eventually_within_interior:

  1348   assumes "x \<in> interior S"

  1349   shows "eventually P (at x within S) \<longleftrightarrow> eventually P (at x)" (is "?lhs = ?rhs")

  1350 proof-

  1351   from assms obtain T where T: "open T" "x \<in> T" "T \<subseteq> S" ..

  1352   { assume "?lhs"

  1353     then obtain A where "open A" "x \<in> A" "\<forall>y\<in>A. y \<noteq> x \<longrightarrow> y \<in> S \<longrightarrow> P y"

  1354       unfolding Limits.eventually_within Limits.eventually_at_topological

  1355       by auto

  1356     with T have "open (A \<inter> T)" "x \<in> A \<inter> T" "\<forall>y\<in>(A \<inter> T). y \<noteq> x \<longrightarrow> P y"

  1357       by auto

  1358     then have "?rhs"

  1359       unfolding Limits.eventually_at_topological by auto

  1360   } moreover

  1361   { assume "?rhs" hence "?lhs"

  1362       unfolding Limits.eventually_within

  1363       by (auto elim: eventually_elim1)

  1364   } ultimately

  1365   show "?thesis" ..

  1366 qed

  1367

  1368 lemma at_within_interior:

  1369   "x \<in> interior S \<Longrightarrow> at x within S = at x"

  1370   by (simp add: filter_eq_iff eventually_within_interior)

  1371

  1372 lemma at_within_open:

  1373   "\<lbrakk>x \<in> S; open S\<rbrakk> \<Longrightarrow> at x within S = at x"

  1374   by (simp only: at_within_interior interior_open)

  1375

  1376 lemma Lim_within_open:

  1377   fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"

  1378   assumes"a \<in> S" "open S"

  1379   shows "(f ---> l)(at a within S) \<longleftrightarrow> (f ---> l)(at a)"

  1380   using assms by (simp only: at_within_open)

  1381

  1382 lemma Lim_within_LIMSEQ:

  1383   fixes a :: "'a::metric_space"

  1384   assumes "\<forall>S. (\<forall>n. S n \<noteq> a \<and> S n \<in> T) \<and> S ----> a \<longrightarrow> (\<lambda>n. X (S n)) ----> L"

  1385   shows "(X ---> L) (at a within T)"

  1386   using assms unfolding tendsto_def [where l=L]

  1387   by (simp add: sequentially_imp_eventually_within)

  1388

  1389 lemma Lim_right_bound:

  1390   fixes f :: "real \<Rightarrow> real"

  1391   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"

  1392   assumes bnd: "\<And>a. a \<in> I \<Longrightarrow> x < a \<Longrightarrow> K \<le> f a"

  1393   shows "(f ---> Inf (f  ({x<..} \<inter> I))) (at x within ({x<..} \<inter> I))"

  1394 proof cases

  1395   assume "{x<..} \<inter> I = {}" then show ?thesis by (simp add: Lim_within_empty)

  1396 next

  1397   assume [simp]: "{x<..} \<inter> I \<noteq> {}"

  1398   show ?thesis

  1399   proof (rule Lim_within_LIMSEQ, safe)

  1400     fix S assume S: "\<forall>n. S n \<noteq> x \<and> S n \<in> {x <..} \<inter> I" "S ----> x"

  1401

  1402     show "(\<lambda>n. f (S n)) ----> Inf (f  ({x<..} \<inter> I))"

  1403     proof (rule LIMSEQ_I, rule ccontr)

  1404       fix r :: real assume "0 < r"

  1405       with Inf_close[of "f  ({x<..} \<inter> I)" r]

  1406       obtain y where y: "x < y" "y \<in> I" "f y < Inf (f  ({x <..} \<inter> I)) + r" by auto

  1407       from x < y have "0 < y - x" by auto

  1408       from S(2)[THEN LIMSEQ_D, OF this]

  1409       obtain N where N: "\<And>n. N \<le> n \<Longrightarrow> \<bar>S n - x\<bar> < y - x" by auto

  1410

  1411       assume "\<not> (\<exists>N. \<forall>n\<ge>N. norm (f (S n) - Inf (f  ({x<..} \<inter> I))) < r)"

  1412       moreover have "\<And>n. Inf (f  ({x<..} \<inter> I)) \<le> f (S n)"

  1413         using S bnd by (intro Inf_lower[where z=K]) auto

  1414       ultimately obtain n where n: "N \<le> n" "r + Inf (f  ({x<..} \<inter> I)) \<le> f (S n)"

  1415         by (auto simp: not_less field_simps)

  1416       with N[OF n(1)] mono[OF _ y \<in> I, of "S n"] S(1)[THEN spec, of n] y

  1417       show False by auto

  1418     qed

  1419   qed

  1420 qed

  1421

  1422 text{* Another limit point characterization. *}

  1423

  1424 lemma islimpt_sequential:

  1425   fixes x :: "'a::first_countable_topology"

  1426   shows "x islimpt S \<longleftrightarrow> (\<exists>f. (\<forall>n::nat. f n \<in> S - {x}) \<and> (f ---> x) sequentially)"

  1427     (is "?lhs = ?rhs")

  1428 proof

  1429   assume ?lhs

  1430   from countable_basis_at_decseq[of x] guess A . note A = this

  1431   def f \<equiv> "\<lambda>n. SOME y. y \<in> S \<and> y \<in> A n \<and> x \<noteq> y"

  1432   { fix n

  1433     from ?lhs have "\<exists>y. y \<in> S \<and> y \<in> A n \<and> x \<noteq> y"

  1434       unfolding islimpt_def using A(1,2)[of n] by auto

  1435     then have "f n \<in> S \<and> f n \<in> A n \<and> x \<noteq> f n"

  1436       unfolding f_def by (rule someI_ex)

  1437     then have "f n \<in> S" "f n \<in> A n" "x \<noteq> f n" by auto }

  1438   then have "\<forall>n. f n \<in> S - {x}" by auto

  1439   moreover have "(\<lambda>n. f n) ----> x"

  1440   proof (rule topological_tendstoI)

  1441     fix S assume "open S" "x \<in> S"

  1442     from A(3)[OF this] \<And>n. f n \<in> A n

  1443     show "eventually (\<lambda>x. f x \<in> S) sequentially" by (auto elim!: eventually_elim1)

  1444   qed

  1445   ultimately show ?rhs by fast

  1446 next

  1447   assume ?rhs

  1448   then obtain f :: "nat \<Rightarrow> 'a" where f: "\<And>n. f n \<in> S - {x}" and lim: "f ----> x" by auto

  1449   show ?lhs

  1450     unfolding islimpt_def

  1451   proof safe

  1452     fix T assume "open T" "x \<in> T"

  1453     from lim[THEN topological_tendstoD, OF this] f

  1454     show "\<exists>y\<in>S. y \<in> T \<and> y \<noteq> x"

  1455       unfolding eventually_sequentially by auto

  1456   qed

  1457 qed

  1458

  1459 lemma Lim_inv: (* TODO: delete *)

  1460   fixes f :: "'a \<Rightarrow> real" and A :: "'a filter"

  1461   assumes "(f ---> l) A" and "l \<noteq> 0"

  1462   shows "((inverse o f) ---> inverse l) A"

  1463   unfolding o_def using assms by (rule tendsto_inverse)

  1464

  1465 lemma Lim_null:

  1466   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"

  1467   shows "(f ---> l) net \<longleftrightarrow> ((\<lambda>x. f(x) - l) ---> 0) net"

  1468   by (simp add: Lim dist_norm)

  1469

  1470 lemma Lim_null_comparison:

  1471   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"

  1472   assumes "eventually (\<lambda>x. norm (f x) \<le> g x) net" "(g ---> 0) net"

  1473   shows "(f ---> 0) net"

  1474 proof (rule metric_tendsto_imp_tendsto)

  1475   show "(g ---> 0) net" by fact

  1476   show "eventually (\<lambda>x. dist (f x) 0 \<le> dist (g x) 0) net"

  1477     using assms(1) by (rule eventually_elim1, simp add: dist_norm)

  1478 qed

  1479

  1480 lemma Lim_transform_bound:

  1481   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"

  1482   fixes g :: "'a \<Rightarrow> 'c::real_normed_vector"

  1483   assumes "eventually (\<lambda>n. norm(f n) <= norm(g n)) net"  "(g ---> 0) net"

  1484   shows "(f ---> 0) net"

  1485   using assms(1) tendsto_norm_zero [OF assms(2)]

  1486   by (rule Lim_null_comparison)

  1487

  1488 text{* Deducing things about the limit from the elements. *}

  1489

  1490 lemma Lim_in_closed_set:

  1491   assumes "closed S" "eventually (\<lambda>x. f(x) \<in> S) net" "\<not>(trivial_limit net)" "(f ---> l) net"

  1492   shows "l \<in> S"

  1493 proof (rule ccontr)

  1494   assume "l \<notin> S"

  1495   with closed S have "open (- S)" "l \<in> - S"

  1496     by (simp_all add: open_Compl)

  1497   with assms(4) have "eventually (\<lambda>x. f x \<in> - S) net"

  1498     by (rule topological_tendstoD)

  1499   with assms(2) have "eventually (\<lambda>x. False) net"

  1500     by (rule eventually_elim2) simp

  1501   with assms(3) show "False"

  1502     by (simp add: eventually_False)

  1503 qed

  1504

  1505 text{* Need to prove closed(cball(x,e)) before deducing this as a corollary. *}

  1506

  1507 lemma Lim_dist_ubound:

  1508   assumes "\<not>(trivial_limit net)" "(f ---> l) net" "eventually (\<lambda>x. dist a (f x) <= e) net"

  1509   shows "dist a l <= e"

  1510 proof-

  1511   have "dist a l \<in> {..e}"

  1512   proof (rule Lim_in_closed_set)

  1513     show "closed {..e}" by simp

  1514     show "eventually (\<lambda>x. dist a (f x) \<in> {..e}) net" by (simp add: assms)

  1515     show "\<not> trivial_limit net" by fact

  1516     show "((\<lambda>x. dist a (f x)) ---> dist a l) net" by (intro tendsto_intros assms)

  1517   qed

  1518   thus ?thesis by simp

  1519 qed

  1520

  1521 lemma Lim_norm_ubound:

  1522   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"

  1523   assumes "\<not>(trivial_limit net)" "(f ---> l) net" "eventually (\<lambda>x. norm(f x) <= e) net"

  1524   shows "norm(l) <= e"

  1525 proof-

  1526   have "norm l \<in> {..e}"

  1527   proof (rule Lim_in_closed_set)

  1528     show "closed {..e}" by simp

  1529     show "eventually (\<lambda>x. norm (f x) \<in> {..e}) net" by (simp add: assms)

  1530     show "\<not> trivial_limit net" by fact

  1531     show "((\<lambda>x. norm (f x)) ---> norm l) net" by (intro tendsto_intros assms)

  1532   qed

  1533   thus ?thesis by simp

  1534 qed

  1535

  1536 lemma Lim_norm_lbound:

  1537   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"

  1538   assumes "\<not> (trivial_limit net)"  "(f ---> l) net"  "eventually (\<lambda>x. e <= norm(f x)) net"

  1539   shows "e \<le> norm l"

  1540 proof-

  1541   have "norm l \<in> {e..}"

  1542   proof (rule Lim_in_closed_set)

  1543     show "closed {e..}" by simp

  1544     show "eventually (\<lambda>x. norm (f x) \<in> {e..}) net" by (simp add: assms)

  1545     show "\<not> trivial_limit net" by fact

  1546     show "((\<lambda>x. norm (f x)) ---> norm l) net" by (intro tendsto_intros assms)

  1547   qed

  1548   thus ?thesis by simp

  1549 qed

  1550

  1551 text{* Uniqueness of the limit, when nontrivial. *}

  1552

  1553 lemma tendsto_Lim:

  1554   fixes f :: "'a \<Rightarrow> 'b::t2_space"

  1555   shows "~(trivial_limit net) \<Longrightarrow> (f ---> l) net ==> Lim net f = l"

  1556   unfolding Lim_def using tendsto_unique[of net f] by auto

  1557

  1558 text{* Limit under bilinear function *}

  1559

  1560 lemma Lim_bilinear:

  1561   assumes "(f ---> l) net" and "(g ---> m) net" and "bounded_bilinear h"

  1562   shows "((\<lambda>x. h (f x) (g x)) ---> (h l m)) net"

  1563 using bounded_bilinear h (f ---> l) net (g ---> m) net

  1564 by (rule bounded_bilinear.tendsto)

  1565

  1566 text{* These are special for limits out of the same vector space. *}

  1567

  1568 lemma Lim_within_id: "(id ---> a) (at a within s)"

  1569   unfolding id_def by (rule tendsto_ident_at_within)

  1570

  1571 lemma Lim_at_id: "(id ---> a) (at a)"

  1572   unfolding id_def by (rule tendsto_ident_at)

  1573

  1574 lemma Lim_at_zero:

  1575   fixes a :: "'a::real_normed_vector"

  1576   fixes l :: "'b::topological_space"

  1577   shows "(f ---> l) (at a) \<longleftrightarrow> ((\<lambda>x. f(a + x)) ---> l) (at 0)" (is "?lhs = ?rhs")

  1578   using LIM_offset_zero LIM_offset_zero_cancel ..

  1579

  1580 text{* It's also sometimes useful to extract the limit point from the filter. *}

  1581

  1582 definition

  1583   netlimit :: "'a::t2_space filter \<Rightarrow> 'a" where

  1584   "netlimit net = (SOME a. ((\<lambda>x. x) ---> a) net)"

  1585

  1586 lemma netlimit_within:

  1587   assumes "\<not> trivial_limit (at a within S)"

  1588   shows "netlimit (at a within S) = a"

  1589 unfolding netlimit_def

  1590 apply (rule some_equality)

  1591 apply (rule Lim_at_within)

  1592 apply (rule tendsto_ident_at)

  1593 apply (erule tendsto_unique [OF assms])

  1594 apply (rule Lim_at_within)

  1595 apply (rule tendsto_ident_at)

  1596 done

  1597

  1598 lemma netlimit_at:

  1599   fixes a :: "'a::{perfect_space,t2_space}"

  1600   shows "netlimit (at a) = a"

  1601   using netlimit_within [of a UNIV] by simp

  1602

  1603 lemma lim_within_interior:

  1604   "x \<in> interior S \<Longrightarrow> (f ---> l) (at x within S) \<longleftrightarrow> (f ---> l) (at x)"

  1605   by (simp add: at_within_interior)

  1606

  1607 lemma netlimit_within_interior:

  1608   fixes x :: "'a::{t2_space,perfect_space}"

  1609   assumes "x \<in> interior S"

  1610   shows "netlimit (at x within S) = x"

  1611 using assms by (simp add: at_within_interior netlimit_at)

  1612

  1613 text{* Transformation of limit. *}

  1614

  1615 lemma Lim_transform:

  1616   fixes f g :: "'a::type \<Rightarrow> 'b::real_normed_vector"

  1617   assumes "((\<lambda>x. f x - g x) ---> 0) net" "(f ---> l) net"

  1618   shows "(g ---> l) net"

  1619   using tendsto_diff [OF assms(2) assms(1)] by simp

  1620

  1621 lemma Lim_transform_eventually:

  1622   "eventually (\<lambda>x. f x = g x) net \<Longrightarrow> (f ---> l) net \<Longrightarrow> (g ---> l) net"

  1623   apply (rule topological_tendstoI)

  1624   apply (drule (2) topological_tendstoD)

  1625   apply (erule (1) eventually_elim2, simp)

  1626   done

  1627

  1628 lemma Lim_transform_within:

  1629   assumes "0 < d" and "\<forall>x'\<in>S. 0 < dist x' x \<and> dist x' x < d \<longrightarrow> f x' = g x'"

  1630   and "(f ---> l) (at x within S)"

  1631   shows "(g ---> l) (at x within S)"

  1632 proof (rule Lim_transform_eventually)

  1633   show "eventually (\<lambda>x. f x = g x) (at x within S)"

  1634     unfolding eventually_within

  1635     using assms(1,2) by auto

  1636   show "(f ---> l) (at x within S)" by fact

  1637 qed

  1638

  1639 lemma Lim_transform_at:

  1640   assumes "0 < d" and "\<forall>x'. 0 < dist x' x \<and> dist x' x < d \<longrightarrow> f x' = g x'"

  1641   and "(f ---> l) (at x)"

  1642   shows "(g ---> l) (at x)"

  1643 proof (rule Lim_transform_eventually)

  1644   show "eventually (\<lambda>x. f x = g x) (at x)"

  1645     unfolding eventually_at

  1646     using assms(1,2) by auto

  1647   show "(f ---> l) (at x)" by fact

  1648 qed

  1649

  1650 text{* Common case assuming being away from some crucial point like 0. *}

  1651

  1652 lemma Lim_transform_away_within:

  1653   fixes a b :: "'a::t1_space"

  1654   assumes "a \<noteq> b" and "\<forall>x\<in>S. x \<noteq> a \<and> x \<noteq> b \<longrightarrow> f x = g x"

  1655   and "(f ---> l) (at a within S)"

  1656   shows "(g ---> l) (at a within S)"

  1657 proof (rule Lim_transform_eventually)

  1658   show "(f ---> l) (at a within S)" by fact

  1659   show "eventually (\<lambda>x. f x = g x) (at a within S)"

  1660     unfolding Limits.eventually_within eventually_at_topological

  1661     by (rule exI [where x="- {b}"], simp add: open_Compl assms)

  1662 qed

  1663

  1664 lemma Lim_transform_away_at:

  1665   fixes a b :: "'a::t1_space"

  1666   assumes ab: "a\<noteq>b" and fg: "\<forall>x. x \<noteq> a \<and> x \<noteq> b \<longrightarrow> f x = g x"

  1667   and fl: "(f ---> l) (at a)"

  1668   shows "(g ---> l) (at a)"

  1669   using Lim_transform_away_within[OF ab, of UNIV f g l] fg fl

  1670   by simp

  1671

  1672 text{* Alternatively, within an open set. *}

  1673

  1674 lemma Lim_transform_within_open:

  1675   assumes "open S" and "a \<in> S" and "\<forall>x\<in>S. x \<noteq> a \<longrightarrow> f x = g x"

  1676   and "(f ---> l) (at a)"

  1677   shows "(g ---> l) (at a)"

  1678 proof (rule Lim_transform_eventually)

  1679   show "eventually (\<lambda>x. f x = g x) (at a)"

  1680     unfolding eventually_at_topological

  1681     using assms(1,2,3) by auto

  1682   show "(f ---> l) (at a)" by fact

  1683 qed

  1684

  1685 text{* A congruence rule allowing us to transform limits assuming not at point. *}

  1686

  1687 (* FIXME: Only one congruence rule for tendsto can be used at a time! *)

  1688

  1689 lemma Lim_cong_within(*[cong add]*):

  1690   assumes "a = b" "x = y" "S = T"

  1691   assumes "\<And>x. x \<noteq> b \<Longrightarrow> x \<in> T \<Longrightarrow> f x = g x"

  1692   shows "(f ---> x) (at a within S) \<longleftrightarrow> (g ---> y) (at b within T)"

  1693   unfolding tendsto_def Limits.eventually_within eventually_at_topological

  1694   using assms by simp

  1695

  1696 lemma Lim_cong_at(*[cong add]*):

  1697   assumes "a = b" "x = y"

  1698   assumes "\<And>x. x \<noteq> a \<Longrightarrow> f x = g x"

  1699   shows "((\<lambda>x. f x) ---> x) (at a) \<longleftrightarrow> ((g ---> y) (at a))"

  1700   unfolding tendsto_def eventually_at_topological

  1701   using assms by simp

  1702

  1703 text{* Useful lemmas on closure and set of possible sequential limits.*}

  1704

  1705 lemma closure_sequential:

  1706   fixes l :: "'a::first_countable_topology"

  1707   shows "l \<in> closure S \<longleftrightarrow> (\<exists>x. (\<forall>n. x n \<in> S) \<and> (x ---> l) sequentially)" (is "?lhs = ?rhs")

  1708 proof

  1709   assume "?lhs" moreover

  1710   { assume "l \<in> S"

  1711     hence "?rhs" using tendsto_const[of l sequentially] by auto

  1712   } moreover

  1713   { assume "l islimpt S"

  1714     hence "?rhs" unfolding islimpt_sequential by auto

  1715   } ultimately

  1716   show "?rhs" unfolding closure_def by auto

  1717 next

  1718   assume "?rhs"

  1719   thus "?lhs" unfolding closure_def unfolding islimpt_sequential by auto

  1720 qed

  1721

  1722 lemma closed_sequential_limits:

  1723   fixes S :: "'a::first_countable_topology set"

  1724   shows "closed S \<longleftrightarrow> (\<forall>x l. (\<forall>n. x n \<in> S) \<and> (x ---> l) sequentially \<longrightarrow> l \<in> S)"

  1725   unfolding closed_limpt

  1726   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]

  1727   by metis

  1728

  1729 lemma closure_approachable:

  1730   fixes S :: "'a::metric_space set"

  1731   shows "x \<in> closure S \<longleftrightarrow> (\<forall>e>0. \<exists>y\<in>S. dist y x < e)"

  1732   apply (auto simp add: closure_def islimpt_approachable)

  1733   by (metis dist_self)

  1734

  1735 lemma closed_approachable:

  1736   fixes S :: "'a::metric_space set"

  1737   shows "closed S ==> (\<forall>e>0. \<exists>y\<in>S. dist y x < e) \<longleftrightarrow> x \<in> S"

  1738   by (metis closure_closed closure_approachable)

  1739

  1740 subsection {* Infimum Distance *}

  1741

  1742 definition "infdist x A = (if A = {} then 0 else Inf {dist x a|a. a \<in> A})"

  1743

  1744 lemma infdist_notempty: "A \<noteq> {} \<Longrightarrow> infdist x A = Inf {dist x a|a. a \<in> A}"

  1745   by (simp add: infdist_def)

  1746

  1747 lemma infdist_nonneg:

  1748   shows "0 \<le> infdist x A"

  1749   using assms by (auto simp add: infdist_def)

  1750

  1751 lemma infdist_le:

  1752   assumes "a \<in> A"

  1753   assumes "d = dist x a"

  1754   shows "infdist x A \<le> d"

  1755   using assms by (auto intro!: SupInf.Inf_lower[where z=0] simp add: infdist_def)

  1756

  1757 lemma infdist_zero[simp]:

  1758   assumes "a \<in> A" shows "infdist a A = 0"

  1759 proof -

  1760   from infdist_le[OF assms, of "dist a a"] have "infdist a A \<le> 0" by auto

  1761   with infdist_nonneg[of a A] assms show "infdist a A = 0" by auto

  1762 qed

  1763

  1764 lemma infdist_triangle:

  1765   shows "infdist x A \<le> infdist y A + dist x y"

  1766 proof cases

  1767   assume "A = {}" thus ?thesis by (simp add: infdist_def)

  1768 next

  1769   assume "A \<noteq> {}" then obtain a where "a \<in> A" by auto

  1770   have "infdist x A \<le> Inf {dist x y + dist y a |a. a \<in> A}"

  1771   proof

  1772     from A \<noteq> {} show "{dist x y + dist y a |a. a \<in> A} \<noteq> {}" by simp

  1773     fix d assume "d \<in> {dist x y + dist y a |a. a \<in> A}"

  1774     then obtain a where d: "d = dist x y + dist y a" "a \<in> A" by auto

  1775     show "infdist x A \<le> d"

  1776       unfolding infdist_notempty[OF A \<noteq> {}]

  1777     proof (rule Inf_lower2)

  1778       show "dist x a \<in> {dist x a |a. a \<in> A}" using a \<in> A by auto

  1779       show "dist x a \<le> d" unfolding d by (rule dist_triangle)

  1780       fix d assume "d \<in> {dist x a |a. a \<in> A}"

  1781       then obtain a where "a \<in> A" "d = dist x a" by auto

  1782       thus "infdist x A \<le> d" by (rule infdist_le)

  1783     qed

  1784   qed

  1785   also have "\<dots> = dist x y + infdist y A"

  1786   proof (rule Inf_eq, safe)

  1787     fix a assume "a \<in> A"

  1788     thus "dist x y + infdist y A \<le> dist x y + dist y a" by (auto intro: infdist_le)

  1789   next

  1790     fix i assume inf: "\<And>d. d \<in> {dist x y + dist y a |a. a \<in> A} \<Longrightarrow> i \<le> d"

  1791     hence "i - dist x y \<le> infdist y A" unfolding infdist_notempty[OF A \<noteq> {}] using a \<in> A

  1792       by (intro Inf_greatest) (auto simp: field_simps)

  1793     thus "i \<le> dist x y + infdist y A" by simp

  1794   qed

  1795   finally show ?thesis by simp

  1796 qed

  1797

  1798 lemma

  1799   in_closure_iff_infdist_zero:

  1800   assumes "A \<noteq> {}"

  1801   shows "x \<in> closure A \<longleftrightarrow> infdist x A = 0"

  1802 proof

  1803   assume "x \<in> closure A"

  1804   show "infdist x A = 0"

  1805   proof (rule ccontr)

  1806     assume "infdist x A \<noteq> 0"

  1807     with infdist_nonneg[of x A] have "infdist x A > 0" by auto

  1808     hence "ball x (infdist x A) \<inter> closure A = {}" apply auto

  1809       by (metis 0 < infdist x A x \<in> closure A closure_approachable dist_commute

  1810         eucl_less_not_refl euclidean_trans(2) infdist_le)

  1811     hence "x \<notin> closure A" by (metis 0 < infdist x A centre_in_ball disjoint_iff_not_equal)

  1812     thus False using x \<in> closure A by simp

  1813   qed

  1814 next

  1815   assume x: "infdist x A = 0"

  1816   then obtain a where "a \<in> A" by atomize_elim (metis all_not_in_conv assms)

  1817   show "x \<in> closure A" unfolding closure_approachable

  1818   proof (safe, rule ccontr)

  1819     fix e::real assume "0 < e"

  1820     assume "\<not> (\<exists>y\<in>A. dist y x < e)"

  1821     hence "infdist x A \<ge> e" using a \<in> A

  1822       unfolding infdist_def

  1823       by (force simp: dist_commute)

  1824     with x 0 < e show False by auto

  1825   qed

  1826 qed

  1827

  1828 lemma

  1829   in_closed_iff_infdist_zero:

  1830   assumes "closed A" "A \<noteq> {}"

  1831   shows "x \<in> A \<longleftrightarrow> infdist x A = 0"

  1832 proof -

  1833   have "x \<in> closure A \<longleftrightarrow> infdist x A = 0"

  1834     by (rule in_closure_iff_infdist_zero) fact

  1835   with assms show ?thesis by simp

  1836 qed

  1837

  1838 lemma tendsto_infdist [tendsto_intros]:

  1839   assumes f: "(f ---> l) F"

  1840   shows "((\<lambda>x. infdist (f x) A) ---> infdist l A) F"

  1841 proof (rule tendstoI)

  1842   fix e ::real assume "0 < e"

  1843   from tendstoD[OF f this]

  1844   show "eventually (\<lambda>x. dist (infdist (f x) A) (infdist l A) < e) F"

  1845   proof (eventually_elim)

  1846     fix x

  1847     from infdist_triangle[of l A "f x"] infdist_triangle[of "f x" A l]

  1848     have "dist (infdist (f x) A) (infdist l A) \<le> dist (f x) l"

  1849       by (simp add: dist_commute dist_real_def)

  1850     also assume "dist (f x) l < e"

  1851     finally show "dist (infdist (f x) A) (infdist l A) < e" .

  1852   qed

  1853 qed

  1854

  1855 text{* Some other lemmas about sequences. *}

  1856

  1857 lemma sequentially_offset:

  1858   assumes "eventually (\<lambda>i. P i) sequentially"

  1859   shows "eventually (\<lambda>i. P (i + k)) sequentially"

  1860   using assms unfolding eventually_sequentially by (metis trans_le_add1)

  1861

  1862 lemma seq_offset:

  1863   assumes "(f ---> l) sequentially"

  1864   shows "((\<lambda>i. f (i + k)) ---> l) sequentially"

  1865   using assms by (rule LIMSEQ_ignore_initial_segment) (* FIXME: redundant *)

  1866

  1867 lemma seq_offset_neg:

  1868   "(f ---> l) sequentially ==> ((\<lambda>i. f(i - k)) ---> l) sequentially"

  1869   apply (rule topological_tendstoI)

  1870   apply (drule (2) topological_tendstoD)

  1871   apply (simp only: eventually_sequentially)

  1872   apply (subgoal_tac "\<And>N k (n::nat). N + k <= n ==> N <= n - k")

  1873   apply metis

  1874   by arith

  1875

  1876 lemma seq_offset_rev:

  1877   "((\<lambda>i. f(i + k)) ---> l) sequentially ==> (f ---> l) sequentially"

  1878   by (rule LIMSEQ_offset) (* FIXME: redundant *)

  1879

  1880 lemma seq_harmonic: "((\<lambda>n. inverse (real n)) ---> 0) sequentially"

  1881   using LIMSEQ_inverse_real_of_nat by (rule LIMSEQ_imp_Suc)

  1882

  1883 subsection {* More properties of closed balls *}

  1884

  1885 lemma closed_cball: "closed (cball x e)"

  1886 unfolding cball_def closed_def

  1887 unfolding Collect_neg_eq [symmetric] not_le

  1888 apply (clarsimp simp add: open_dist, rename_tac y)

  1889 apply (rule_tac x="dist x y - e" in exI, clarsimp)

  1890 apply (rename_tac x')

  1891 apply (cut_tac x=x and y=x' and z=y in dist_triangle)

  1892 apply simp

  1893 done

  1894

  1895 lemma open_contains_cball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0.  cball x e \<subseteq> S)"

  1896 proof-

  1897   { fix x and e::real assume "x\<in>S" "e>0" "ball x e \<subseteq> S"

  1898     hence "\<exists>d>0. cball x d \<subseteq> S" unfolding subset_eq by (rule_tac x="e/2" in exI, auto)

  1899   } moreover

  1900   { fix x and e::real assume "x\<in>S" "e>0" "cball x e \<subseteq> S"

  1901     hence "\<exists>d>0. ball x d \<subseteq> S" unfolding subset_eq apply(rule_tac x="e/2" in exI) by auto

  1902   } ultimately

  1903   show ?thesis unfolding open_contains_ball by auto

  1904 qed

  1905

  1906 lemma open_contains_cball_eq: "open S ==> (\<forall>x. x \<in> S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S))"

  1907   by (metis open_contains_cball subset_eq order_less_imp_le centre_in_cball)

  1908

  1909 lemma mem_interior_cball: "x \<in> interior S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S)"

  1910   apply (simp add: interior_def, safe)

  1911   apply (force simp add: open_contains_cball)

  1912   apply (rule_tac x="ball x e" in exI)

  1913   apply (simp add: subset_trans [OF ball_subset_cball])

  1914   done

  1915

  1916 lemma islimpt_ball:

  1917   fixes x y :: "'a::{real_normed_vector,perfect_space}"

  1918   shows "y islimpt ball x e \<longleftrightarrow> 0 < e \<and> y \<in> cball x e" (is "?lhs = ?rhs")

  1919 proof

  1920   assume "?lhs"

  1921   { assume "e \<le> 0"

  1922     hence *:"ball x e = {}" using ball_eq_empty[of x e] by auto

  1923     have False using ?lhs unfolding * using islimpt_EMPTY[of y] by auto

  1924   }

  1925   hence "e > 0" by (metis not_less)

  1926   moreover

  1927   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

  1928   ultimately show "?rhs" by auto

  1929 next

  1930   assume "?rhs" hence "e>0"  by auto

  1931   { fix d::real assume "d>0"

  1932     have "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"

  1933     proof(cases "d \<le> dist x y")

  1934       case True thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"

  1935       proof(cases "x=y")

  1936         case True hence False using d \<le> dist x y d>0 by auto

  1937         thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d" by auto

  1938       next

  1939         case False

  1940

  1941         have "dist x (y - (d / (2 * dist y x)) *\<^sub>R (y - x))

  1942               = norm (x - y + (d / (2 * norm (y - x))) *\<^sub>R (y - x))"

  1943           unfolding mem_cball mem_ball dist_norm diff_diff_eq2 diff_add_eq[THEN sym] by auto

  1944         also have "\<dots> = \<bar>- 1 + d / (2 * norm (x - y))\<bar> * norm (x - y)"

  1945           using scaleR_left_distrib[of "- 1" "d / (2 * norm (y - x))", THEN sym, of "y - x"]

  1946           unfolding scaleR_minus_left scaleR_one

  1947           by (auto simp add: norm_minus_commute)

  1948         also have "\<dots> = \<bar>- norm (x - y) + d / 2\<bar>"

  1949           unfolding abs_mult_pos[of "norm (x - y)", OF norm_ge_zero[of "x - y"]]

  1950           unfolding distrib_right using x\<noteq>y[unfolded dist_nz, unfolded dist_norm] by auto

  1951         also have "\<dots> \<le> e - d/2" using d \<le> dist x y and d>0 and ?rhs by(auto simp add: dist_norm)

  1952         finally have "y - (d / (2 * dist y x)) *\<^sub>R (y - x) \<in> ball x e" using d>0 by auto

  1953

  1954         moreover

  1955

  1956         have "(d / (2*dist y x)) *\<^sub>R (y - x) \<noteq> 0"

  1957           using x\<noteq>y[unfolded dist_nz] d>0 unfolding scaleR_eq_0_iff by (auto simp add: dist_commute)

  1958         moreover

  1959         have "dist (y - (d / (2 * dist y x)) *\<^sub>R (y - x)) y < d" unfolding dist_norm apply simp unfolding norm_minus_cancel

  1960           using d>0 x\<noteq>y[unfolded dist_nz] dist_commute[of x y]

  1961           unfolding dist_norm by auto

  1962         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

  1963       qed

  1964     next

  1965       case False hence "d > dist x y" by auto

  1966       show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"

  1967       proof(cases "x=y")

  1968         case True

  1969         obtain z where **: "z \<noteq> y" "dist z y < min e d"

  1970           using perfect_choose_dist[of "min e d" y]

  1971           using d > 0 e>0 by auto

  1972         show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"

  1973           unfolding x = y

  1974           using z \<noteq> y **

  1975           by (rule_tac x=z in bexI, auto simp add: dist_commute)

  1976       next

  1977         case False thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"

  1978           using d>0 d > dist x y ?rhs by(rule_tac x=x in bexI, auto)

  1979       qed

  1980     qed  }

  1981   thus "?lhs" unfolding mem_cball islimpt_approachable mem_ball by auto

  1982 qed

  1983

  1984 lemma closure_ball_lemma:

  1985   fixes x y :: "'a::real_normed_vector"

  1986   assumes "x \<noteq> y" shows "y islimpt ball x (dist x y)"

  1987 proof (rule islimptI)

  1988   fix T assume "y \<in> T" "open T"

  1989   then obtain r where "0 < r" "\<forall>z. dist z y < r \<longrightarrow> z \<in> T"

  1990     unfolding open_dist by fast

  1991   (* choose point between x and y, within distance r of y. *)

  1992   def k \<equiv> "min 1 (r / (2 * dist x y))"

  1993   def z \<equiv> "y + scaleR k (x - y)"

  1994   have z_def2: "z = x + scaleR (1 - k) (y - x)"

  1995     unfolding z_def by (simp add: algebra_simps)

  1996   have "dist z y < r"

  1997     unfolding z_def k_def using 0 < r

  1998     by (simp add: dist_norm min_def)

  1999   hence "z \<in> T" using \<forall>z. dist z y < r \<longrightarrow> z \<in> T by simp

  2000   have "dist x z < dist x y"

  2001     unfolding z_def2 dist_norm

  2002     apply (simp add: norm_minus_commute)

  2003     apply (simp only: dist_norm [symmetric])

  2004     apply (subgoal_tac "\<bar>1 - k\<bar> * dist x y < 1 * dist x y", simp)

  2005     apply (rule mult_strict_right_mono)

  2006     apply (simp add: k_def divide_pos_pos zero_less_dist_iff 0 < r x \<noteq> y)

  2007     apply (simp add: zero_less_dist_iff x \<noteq> y)

  2008     done

  2009   hence "z \<in> ball x (dist x y)" by simp

  2010   have "z \<noteq> y"

  2011     unfolding z_def k_def using x \<noteq> y 0 < r

  2012     by (simp add: min_def)

  2013   show "\<exists>z\<in>ball x (dist x y). z \<in> T \<and> z \<noteq> y"

  2014     using z \<in> ball x (dist x y) z \<in> T z \<noteq> y

  2015     by fast

  2016 qed

  2017

  2018 lemma closure_ball:

  2019   fixes x :: "'a::real_normed_vector"

  2020   shows "0 < e \<Longrightarrow> closure (ball x e) = cball x e"

  2021 apply (rule equalityI)

  2022 apply (rule closure_minimal)

  2023 apply (rule ball_subset_cball)

  2024 apply (rule closed_cball)

  2025 apply (rule subsetI, rename_tac y)

  2026 apply (simp add: le_less [where 'a=real])

  2027 apply (erule disjE)

  2028 apply (rule subsetD [OF closure_subset], simp)

  2029 apply (simp add: closure_def)

  2030 apply clarify

  2031 apply (rule closure_ball_lemma)

  2032 apply (simp add: zero_less_dist_iff)

  2033 done

  2034

  2035 (* In a trivial vector space, this fails for e = 0. *)

  2036 lemma interior_cball:

  2037   fixes x :: "'a::{real_normed_vector, perfect_space}"

  2038   shows "interior (cball x e) = ball x e"

  2039 proof(cases "e\<ge>0")

  2040   case False note cs = this

  2041   from cs have "ball x e = {}" using ball_empty[of e x] by auto moreover

  2042   { fix y assume "y \<in> cball x e"

  2043     hence False unfolding mem_cball using dist_nz[of x y] cs by auto  }

  2044   hence "cball x e = {}" by auto

  2045   hence "interior (cball x e) = {}" using interior_empty by auto

  2046   ultimately show ?thesis by blast

  2047 next

  2048   case True note cs = this

  2049   have "ball x e \<subseteq> cball x e" using ball_subset_cball by auto moreover

  2050   { fix S y assume as: "S \<subseteq> cball x e" "open S" "y\<in>S"

  2051     then obtain d where "d>0" and d:"\<forall>x'. dist x' y < d \<longrightarrow> x' \<in> S" unfolding open_dist by blast

  2052

  2053     then obtain xa where xa_y: "xa \<noteq> y" and xa: "dist xa y < d"

  2054       using perfect_choose_dist [of d] by auto

  2055     have "xa\<in>S" using d[THEN spec[where x=xa]] using xa by(auto simp add: dist_commute)

  2056     hence xa_cball:"xa \<in> cball x e" using as(1) by auto

  2057

  2058     hence "y \<in> ball x e" proof(cases "x = y")

  2059       case True

  2060       hence "e>0" using xa_y[unfolded dist_nz] xa_cball[unfolded mem_cball] by (auto simp add: dist_commute)

  2061       thus "y \<in> ball x e" using x = y  by simp

  2062     next

  2063       case False

  2064       have "dist (y + (d / 2 / dist y x) *\<^sub>R (y - x)) y < d" unfolding dist_norm

  2065         using d>0 norm_ge_zero[of "y - x"] x \<noteq> y by auto

  2066       hence *:"y + (d / 2 / dist y x) *\<^sub>R (y - x) \<in> cball x e" using d as(1)[unfolded subset_eq] by blast

  2067       have "y - x \<noteq> 0" using x \<noteq> y by auto

  2068       hence **:"d / (2 * norm (y - x)) > 0" unfolding zero_less_norm_iff[THEN sym]

  2069         using d>0 divide_pos_pos[of d "2*norm (y - x)"] by auto

  2070

  2071       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)"

  2072         by (auto simp add: dist_norm algebra_simps)

  2073       also have "\<dots> = norm ((1 + d / (2 * norm (y - x))) *\<^sub>R (y - x))"

  2074         by (auto simp add: algebra_simps)

  2075       also have "\<dots> = \<bar>1 + d / (2 * norm (y - x))\<bar> * norm (y - x)"

  2076         using ** by auto

  2077       also have "\<dots> = (dist y x) + d/2"using ** by (auto simp add: distrib_right dist_norm)

  2078       finally have "e \<ge> dist x y +d/2" using *[unfolded mem_cball] by (auto simp add: dist_commute)

  2079       thus "y \<in> ball x e" unfolding mem_ball using d>0 by auto

  2080     qed  }

  2081   hence "\<forall>S \<subseteq> cball x e. open S \<longrightarrow> S \<subseteq> ball x e" by auto

  2082   ultimately show ?thesis using interior_unique[of "ball x e" "cball x e"] using open_ball[of x e] by auto

  2083 qed

  2084

  2085 lemma frontier_ball:

  2086   fixes a :: "'a::real_normed_vector"

  2087   shows "0 < e ==> frontier(ball a e) = {x. dist a x = e}"

  2088   apply (simp add: frontier_def closure_ball interior_open order_less_imp_le)

  2089   apply (simp add: set_eq_iff)

  2090   by arith

  2091

  2092 lemma frontier_cball:

  2093   fixes a :: "'a::{real_normed_vector, perfect_space}"

  2094   shows "frontier(cball a e) = {x. dist a x = e}"

  2095   apply (simp add: frontier_def interior_cball closed_cball order_less_imp_le)

  2096   apply (simp add: set_eq_iff)

  2097   by arith

  2098

  2099 lemma cball_eq_empty: "(cball x e = {}) \<longleftrightarrow> e < 0"

  2100   apply (simp add: set_eq_iff not_le)

  2101   by (metis zero_le_dist dist_self order_less_le_trans)

  2102 lemma cball_empty: "e < 0 ==> cball x e = {}" by (simp add: cball_eq_empty)

  2103

  2104 lemma cball_eq_sing:

  2105   fixes x :: "'a::{metric_space,perfect_space}"

  2106   shows "(cball x e = {x}) \<longleftrightarrow> e = 0"

  2107 proof (rule linorder_cases)

  2108   assume e: "0 < e"

  2109   obtain a where "a \<noteq> x" "dist a x < e"

  2110     using perfect_choose_dist [OF e] by auto

  2111   hence "a \<noteq> x" "dist x a \<le> e" by (auto simp add: dist_commute)

  2112   with e show ?thesis by (auto simp add: set_eq_iff)

  2113 qed auto

  2114

  2115 lemma cball_sing:

  2116   fixes x :: "'a::metric_space"

  2117   shows "e = 0 ==> cball x e = {x}"

  2118   by (auto simp add: set_eq_iff)

  2119

  2120

  2121 subsection {* Boundedness *}

  2122

  2123   (* FIXME: This has to be unified with BSEQ!! *)

  2124 definition (in metric_space)

  2125   bounded :: "'a set \<Rightarrow> bool" where

  2126   "bounded S \<longleftrightarrow> (\<exists>x e. \<forall>y\<in>S. dist x y \<le> e)"

  2127

  2128 lemma bounded_any_center: "bounded S \<longleftrightarrow> (\<exists>e. \<forall>y\<in>S. dist a y \<le> e)"

  2129 unfolding bounded_def

  2130 apply safe

  2131 apply (rule_tac x="dist a x + e" in exI, clarify)

  2132 apply (drule (1) bspec)

  2133 apply (erule order_trans [OF dist_triangle add_left_mono])

  2134 apply auto

  2135 done

  2136

  2137 lemma bounded_iff: "bounded S \<longleftrightarrow> (\<exists>a. \<forall>x\<in>S. norm x \<le> a)"

  2138 unfolding bounded_any_center [where a=0]

  2139 by (simp add: dist_norm)

  2140

  2141 lemma bounded_realI: assumes "\<forall>x\<in>s. abs (x::real) \<le> B" shows "bounded s"

  2142   unfolding bounded_def dist_real_def apply(rule_tac x=0 in exI)

  2143   using assms by auto

  2144

  2145 lemma bounded_empty[simp]: "bounded {}" by (simp add: bounded_def)

  2146 lemma bounded_subset: "bounded T \<Longrightarrow> S \<subseteq> T ==> bounded S"

  2147   by (metis bounded_def subset_eq)

  2148

  2149 lemma bounded_interior[intro]: "bounded S ==> bounded(interior S)"

  2150   by (metis bounded_subset interior_subset)

  2151

  2152 lemma bounded_closure[intro]: assumes "bounded S" shows "bounded(closure S)"

  2153 proof-

  2154   from assms obtain x and a where a: "\<forall>y\<in>S. dist x y \<le> a" unfolding bounded_def by auto

  2155   { fix y assume "y \<in> closure S"

  2156     then obtain f where f: "\<forall>n. f n \<in> S"  "(f ---> y) sequentially"

  2157       unfolding closure_sequential by auto

  2158     have "\<forall>n. f n \<in> S \<longrightarrow> dist x (f n) \<le> a" using a by simp

  2159     hence "eventually (\<lambda>n. dist x (f n) \<le> a) sequentially"

  2160       by (rule eventually_mono, simp add: f(1))

  2161     have "dist x y \<le> a"

  2162       apply (rule Lim_dist_ubound [of sequentially f])

  2163       apply (rule trivial_limit_sequentially)

  2164       apply (rule f(2))

  2165       apply fact

  2166       done

  2167   }

  2168   thus ?thesis unfolding bounded_def by auto

  2169 qed

  2170

  2171 lemma bounded_cball[simp,intro]: "bounded (cball x e)"

  2172   apply (simp add: bounded_def)

  2173   apply (rule_tac x=x in exI)

  2174   apply (rule_tac x=e in exI)

  2175   apply auto

  2176   done

  2177

  2178 lemma bounded_ball[simp,intro]: "bounded(ball x e)"

  2179   by (metis ball_subset_cball bounded_cball bounded_subset)

  2180

  2181 lemma finite_imp_bounded[intro]:

  2182   fixes S :: "'a::metric_space set" assumes "finite S" shows "bounded S"

  2183 proof-

  2184   { fix a and F :: "'a set" assume as:"bounded F"

  2185     then obtain x e where "\<forall>y\<in>F. dist x y \<le> e" unfolding bounded_def by auto

  2186     hence "\<forall>y\<in>(insert a F). dist x y \<le> max e (dist x a)" by auto

  2187     hence "bounded (insert a F)" unfolding bounded_def by (intro exI)

  2188   }

  2189   thus ?thesis using finite_induct[of S bounded]  using bounded_empty assms by auto

  2190 qed

  2191

  2192 lemma bounded_Un[simp]: "bounded (S \<union> T) \<longleftrightarrow> bounded S \<and> bounded T"

  2193   apply (auto simp add: bounded_def)

  2194   apply (rename_tac x y r s)

  2195   apply (rule_tac x=x in exI)

  2196   apply (rule_tac x="max r (dist x y + s)" in exI)

  2197   apply (rule ballI, rename_tac z, safe)

  2198   apply (drule (1) bspec, simp)

  2199   apply (drule (1) bspec)

  2200   apply (rule min_max.le_supI2)

  2201   apply (erule order_trans [OF dist_triangle add_left_mono])

  2202   done

  2203

  2204 lemma bounded_Union[intro]: "finite F \<Longrightarrow> (\<forall>S\<in>F. bounded S) \<Longrightarrow> bounded(\<Union>F)"

  2205   by (induct rule: finite_induct[of F], auto)

  2206

  2207 lemma bounded_pos: "bounded S \<longleftrightarrow> (\<exists>b>0. \<forall>x\<in> S. norm x <= b)"

  2208   apply (simp add: bounded_iff)

  2209   apply (subgoal_tac "\<And>x (y::real). 0 < 1 + abs y \<and> (x <= y \<longrightarrow> x <= 1 + abs y)")

  2210   by metis arith

  2211

  2212 lemma bounded_Int[intro]: "bounded S \<or> bounded T \<Longrightarrow> bounded (S \<inter> T)"

  2213   by (metis Int_lower1 Int_lower2 bounded_subset)

  2214

  2215 lemma bounded_diff[intro]: "bounded S ==> bounded (S - T)"

  2216 apply (metis Diff_subset bounded_subset)

  2217 done

  2218

  2219 lemma bounded_insert[intro]:"bounded(insert x S) \<longleftrightarrow> bounded S"

  2220   by (metis Diff_cancel Un_empty_right Un_insert_right bounded_Un bounded_subset finite.emptyI finite_imp_bounded infinite_remove subset_insertI)

  2221

  2222 lemma not_bounded_UNIV[simp, intro]:

  2223   "\<not> bounded (UNIV :: 'a::{real_normed_vector, perfect_space} set)"

  2224 proof(auto simp add: bounded_pos not_le)

  2225   obtain x :: 'a where "x \<noteq> 0"

  2226     using perfect_choose_dist [OF zero_less_one] by fast

  2227   fix b::real  assume b: "b >0"

  2228   have b1: "b +1 \<ge> 0" using b by simp

  2229   with x \<noteq> 0 have "b < norm (scaleR (b + 1) (sgn x))"

  2230     by (simp add: norm_sgn)

  2231   then show "\<exists>x::'a. b < norm x" ..

  2232 qed

  2233

  2234 lemma bounded_linear_image:

  2235   assumes "bounded S" "bounded_linear f"

  2236   shows "bounded(f  S)"

  2237 proof-

  2238   from assms(1) obtain b where b:"b>0" "\<forall>x\<in>S. norm x \<le> b" unfolding bounded_pos by auto

  2239   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)

  2240   { fix x assume "x\<in>S"

  2241     hence "norm x \<le> b" using b by auto

  2242     hence "norm (f x) \<le> B * b" using B(2) apply(erule_tac x=x in allE)

  2243       by (metis B(1) B(2) order_trans mult_le_cancel_left_pos)

  2244   }

  2245   thus ?thesis unfolding bounded_pos apply(rule_tac x="b*B" in exI)

  2246     using b B mult_pos_pos [of b B] by (auto simp add: mult_commute)

  2247 qed

  2248

  2249 lemma bounded_scaling:

  2250   fixes S :: "'a::real_normed_vector set"

  2251   shows "bounded S \<Longrightarrow> bounded ((\<lambda>x. c *\<^sub>R x)  S)"

  2252   apply (rule bounded_linear_image, assumption)

  2253   apply (rule bounded_linear_scaleR_right)

  2254   done

  2255

  2256 lemma bounded_translation:

  2257   fixes S :: "'a::real_normed_vector set"

  2258   assumes "bounded S" shows "bounded ((\<lambda>x. a + x)  S)"

  2259 proof-

  2260   from assms obtain b where b:"b>0" "\<forall>x\<in>S. norm x \<le> b" unfolding bounded_pos by auto

  2261   { fix x assume "x\<in>S"

  2262     hence "norm (a + x) \<le> b + norm a" using norm_triangle_ineq[of a x] b by auto

  2263   }

  2264   thus ?thesis unfolding bounded_pos using norm_ge_zero[of a] b(1) using add_strict_increasing[of b 0 "norm a"]

  2265     by (auto intro!: exI[of _ "b + norm a"])

  2266 qed

  2267

  2268

  2269 text{* Some theorems on sups and infs using the notion "bounded". *}

  2270

  2271 lemma bounded_real:

  2272   fixes S :: "real set"

  2273   shows "bounded S \<longleftrightarrow>  (\<exists>a. \<forall>x\<in>S. abs x <= a)"

  2274   by (simp add: bounded_iff)

  2275

  2276 lemma bounded_has_Sup:

  2277   fixes S :: "real set"

  2278   assumes "bounded S" "S \<noteq> {}"

  2279   shows "\<forall>x\<in>S. x <= Sup S" and "\<forall>b. (\<forall>x\<in>S. x <= b) \<longrightarrow> Sup S <= b"

  2280 proof

  2281   fix x assume "x\<in>S"

  2282   thus "x \<le> Sup S"

  2283     by (metis SupInf.Sup_upper abs_le_D1 assms(1) bounded_real)

  2284 next

  2285   show "\<forall>b. (\<forall>x\<in>S. x \<le> b) \<longrightarrow> Sup S \<le> b" using assms

  2286     by (metis SupInf.Sup_least)

  2287 qed

  2288

  2289 lemma Sup_insert:

  2290   fixes S :: "real set"

  2291   shows "bounded S ==> Sup(insert x S) = (if S = {} then x else max x (Sup S))"

  2292 by auto (metis Int_absorb Sup_insert_nonempty assms bounded_has_Sup(1) disjoint_iff_not_equal)

  2293

  2294 lemma Sup_insert_finite:

  2295   fixes S :: "real set"

  2296   shows "finite S \<Longrightarrow> Sup(insert x S) = (if S = {} then x else max x (Sup S))"

  2297   apply (rule Sup_insert)

  2298   apply (rule finite_imp_bounded)

  2299   by simp

  2300

  2301 lemma bounded_has_Inf:

  2302   fixes S :: "real set"

  2303   assumes "bounded S"  "S \<noteq> {}"

  2304   shows "\<forall>x\<in>S. x >= Inf S" and "\<forall>b. (\<forall>x\<in>S. x >= b) \<longrightarrow> Inf S >= b"

  2305 proof

  2306   fix x assume "x\<in>S"

  2307   from assms(1) obtain a where a:"\<forall>x\<in>S. \<bar>x\<bar> \<le> a" unfolding bounded_real by auto

  2308   thus "x \<ge> Inf S" using x\<in>S

  2309     by (metis Inf_lower_EX abs_le_D2 minus_le_iff)

  2310 next

  2311   show "\<forall>b. (\<forall>x\<in>S. x >= b) \<longrightarrow> Inf S \<ge> b" using assms

  2312     by (metis SupInf.Inf_greatest)

  2313 qed

  2314

  2315 lemma Inf_insert:

  2316   fixes S :: "real set"

  2317   shows "bounded S ==> Inf(insert x S) = (if S = {} then x else min x (Inf S))"

  2318 by auto (metis Int_absorb Inf_insert_nonempty bounded_has_Inf(1) disjoint_iff_not_equal)

  2319 lemma Inf_insert_finite:

  2320   fixes S :: "real set"

  2321   shows "finite S ==> Inf(insert x S) = (if S = {} then x else min x (Inf S))"

  2322   by (rule Inf_insert, rule finite_imp_bounded, simp)

  2323

  2324 (* TODO: Move this to RComplete.thy -- would need to include Glb into RComplete *)

  2325 lemma real_isGlb_unique: "[| isGlb R S x; isGlb R S y |] ==> x = (y::real)"

  2326   apply (frule isGlb_isLb)

  2327   apply (frule_tac x = y in isGlb_isLb)

  2328   apply (blast intro!: order_antisym dest!: isGlb_le_isLb)

  2329   done

  2330

  2331 subsection {* Compactness *}

  2332

  2333 subsubsection{* Open-cover compactness *}

  2334

  2335 definition compact :: "'a::topological_space set \<Rightarrow> bool" where

  2336   compact_eq_heine_borel: -- "This name is used for backwards compatibility"

  2337     "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))"

  2338

  2339 lemma compactI:

  2340   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'"

  2341   shows "compact s"

  2342   unfolding compact_eq_heine_borel using assms by metis

  2343

  2344 lemma compactE:

  2345   assumes "compact s" and "\<forall>t\<in>C. open t" and "s \<subseteq> \<Union>C"

  2346   obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> \<Union>C'"

  2347   using assms unfolding compact_eq_heine_borel by metis

  2348

  2349 subsubsection {* Bolzano-Weierstrass property *}

  2350

  2351 lemma heine_borel_imp_bolzano_weierstrass:

  2352   assumes "compact s" "infinite t"  "t \<subseteq> s"

  2353   shows "\<exists>x \<in> s. x islimpt t"

  2354 proof(rule ccontr)

  2355   assume "\<not> (\<exists>x \<in> s. x islimpt t)"

  2356   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

  2357     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

  2358   obtain g where g:"g\<subseteq>{t. \<exists>x. x \<in> s \<and> t = f x}" "finite g" "s \<subseteq> \<Union>g"

  2359     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

  2360   from g(1,3) have g':"\<forall>x\<in>g. \<exists>xa \<in> s. x = f xa" by auto

  2361   { fix x y assume "x\<in>t" "y\<in>t" "f x = f y"

  2362     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

  2363     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  }

  2364   hence "inj_on f t" unfolding inj_on_def by simp

  2365   hence "infinite (f  t)" using assms(2) using finite_imageD by auto

  2366   moreover

  2367   { fix x assume "x\<in>t" "f x \<notin> g"

  2368     from g(3) assms(3) x\<in>t obtain h where "h\<in>g" and "x\<in>h" by auto

  2369     then obtain y where "y\<in>s" "h = f y" using g'[THEN bspec[where x=h]] by auto

  2370     hence "y = x" using f[THEN bspec[where x=y]] and x\<in>t and x\<in>h[unfolded h = f y] by auto

  2371     hence False using f x \<notin> g h\<in>g unfolding h = f y by auto  }

  2372   hence "f  t \<subseteq> g" by auto

  2373   ultimately show False using g(2) using finite_subset by auto

  2374 qed

  2375

  2376 lemma islimpt_range_imp_convergent_subsequence:

  2377   fixes l :: "'a :: {t1_space, first_countable_topology}"

  2378   assumes l: "l islimpt (range f)"

  2379   shows "\<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"

  2380 proof -

  2381   from first_countable_topology_class.countable_basis_at_decseq[of l] guess A . note A = this

  2382

  2383   def s \<equiv> "\<lambda>n i. SOME j. i < j \<and> f j \<in> A (Suc n)"

  2384   { fix n i

  2385     have "\<exists>a. i < a \<and> f a \<in> A (Suc n) - (f  {.. i} - {l})" (is "\<exists>a. _ \<and> _ \<in> ?A")

  2386       apply (rule l[THEN islimptE, of "?A"])

  2387       using A(2) apply fastforce

  2388       using A(1)

  2389       apply (intro open_Diff finite_imp_closed)

  2390       apply auto

  2391       apply (rule_tac x=x in exI)

  2392       apply auto

  2393       done

  2394     then have "\<exists>a. i < a \<and> f a \<in> A (Suc n)" by blast

  2395     then have "i < s n i" "f (s n i) \<in> A (Suc n)"

  2396       unfolding s_def by (auto intro: someI2_ex) }

  2397   note s = this

  2398   def r \<equiv> "nat_rec (s 0 0) s"

  2399   have "subseq r"

  2400     by (auto simp: r_def s subseq_Suc_iff)

  2401   moreover

  2402   have "(\<lambda>n. f (r n)) ----> l"

  2403   proof (rule topological_tendstoI)

  2404     fix S assume "open S" "l \<in> S"

  2405     with A(3) have "eventually (\<lambda>i. A i \<subseteq> S) sequentially" by auto

  2406     moreover

  2407     { fix i assume "Suc 0 \<le> i" then have "f (r i) \<in> A i"

  2408         by (cases i) (simp_all add: r_def s) }

  2409     then have "eventually (\<lambda>i. f (r i) \<in> A i) sequentially" by (auto simp: eventually_sequentially)

  2410     ultimately show "eventually (\<lambda>i. f (r i) \<in> S) sequentially"

  2411       by eventually_elim auto

  2412   qed

  2413   ultimately show "\<exists>r. subseq r \<and> (f \<circ> r) ----> l"

  2414     by (auto simp: convergent_def comp_def)

  2415 qed

  2416

  2417 lemma sequence_infinite_lemma:

  2418   fixes f :: "nat \<Rightarrow> 'a::t1_space"

  2419   assumes "\<forall>n. f n \<noteq> l" and "(f ---> l) sequentially"

  2420   shows "infinite (range f)"

  2421 proof

  2422   assume "finite (range f)"

  2423   hence "closed (range f)" by (rule finite_imp_closed)

  2424   hence "open (- range f)" by (rule open_Compl)

  2425   from assms(1) have "l \<in> - range f" by auto

  2426   from assms(2) have "eventually (\<lambda>n. f n \<in> - range f) sequentially"

  2427     using open (- range f) l \<in> - range f by (rule topological_tendstoD)

  2428   thus False unfolding eventually_sequentially by auto

  2429 qed

  2430

  2431 lemma closure_insert:

  2432   fixes x :: "'a::t1_space"

  2433   shows "closure (insert x s) = insert x (closure s)"

  2434 apply (rule closure_unique)

  2435 apply (rule insert_mono [OF closure_subset])

  2436 apply (rule closed_insert [OF closed_closure])

  2437 apply (simp add: closure_minimal)

  2438 done

  2439

  2440 lemma islimpt_insert:

  2441   fixes x :: "'a::t1_space"

  2442   shows "x islimpt (insert a s) \<longleftrightarrow> x islimpt s"

  2443 proof

  2444   assume *: "x islimpt (insert a s)"

  2445   show "x islimpt s"

  2446   proof (rule islimptI)

  2447     fix t assume t: "x \<in> t" "open t"

  2448     show "\<exists>y\<in>s. y \<in> t \<and> y \<noteq> x"

  2449     proof (cases "x = a")

  2450       case True

  2451       obtain y where "y \<in> insert a s" "y \<in> t" "y \<noteq> x"

  2452         using * t by (rule islimptE)

  2453       with x = a show ?thesis by auto

  2454     next

  2455       case False

  2456       with t have t': "x \<in> t - {a}" "open (t - {a})"

  2457         by (simp_all add: open_Diff)

  2458       obtain y where "y \<in> insert a s" "y \<in> t - {a}" "y \<noteq> x"

  2459         using * t' by (rule islimptE)

  2460       thus ?thesis by auto

  2461     qed

  2462   qed

  2463 next

  2464   assume "x islimpt s" thus "x islimpt (insert a s)"

  2465     by (rule islimpt_subset) auto

  2466 qed

  2467

  2468 lemma islimpt_finite:

  2469   fixes x :: "'a::t1_space"

  2470   shows "finite s \<Longrightarrow> \<not> x islimpt s"

  2471 by (induct set: finite, simp_all add: islimpt_insert)

  2472

  2473 lemma islimpt_union_finite:

  2474   fixes x :: "'a::t1_space"

  2475   shows "finite s \<Longrightarrow> x islimpt (s \<union> t) \<longleftrightarrow> x islimpt t"

  2476 by (simp add: islimpt_Un islimpt_finite)

  2477

  2478 lemma sequence_unique_limpt:

  2479   fixes f :: "nat \<Rightarrow> 'a::t2_space"

  2480   assumes "(f ---> l) sequentially" and "l' islimpt (range f)"

  2481   shows "l' = l"

  2482 proof (rule ccontr)

  2483   assume "l' \<noteq> l"

  2484   obtain s t where "open s" "open t" "l' \<in> s" "l \<in> t" "s \<inter> t = {}"

  2485     using hausdorff [OF l' \<noteq> l] by auto

  2486   have "eventually (\<lambda>n. f n \<in> t) sequentially"

  2487     using assms(1) open t l \<in> t by (rule topological_tendstoD)

  2488   then obtain N where "\<forall>n\<ge>N. f n \<in> t"

  2489     unfolding eventually_sequentially by auto

  2490

  2491   have "UNIV = {..<N} \<union> {N..}" by auto

  2492   hence "l' islimpt (f  ({..<N} \<union> {N..}))" using assms(2) by simp

  2493   hence "l' islimpt (f  {..<N} \<union> f  {N..})" by (simp add: image_Un)

  2494   hence "l' islimpt (f  {N..})" by (simp add: islimpt_union_finite)

  2495   then obtain y where "y \<in> f  {N..}" "y \<in> s" "y \<noteq> l'"

  2496     using l' \<in> s open s by (rule islimptE)

  2497   then obtain n where "N \<le> n" "f n \<in> s" "f n \<noteq> l'" by auto

  2498   with \<forall>n\<ge>N. f n \<in> t have "f n \<in> s \<inter> t" by simp

  2499   with s \<inter> t = {} show False by simp

  2500 qed

  2501

  2502 lemma bolzano_weierstrass_imp_closed:

  2503   fixes s :: "'a::{first_countable_topology, t2_space} set"

  2504   assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"

  2505   shows "closed s"

  2506 proof-

  2507   { fix x l assume as: "\<forall>n::nat. x n \<in> s" "(x ---> l) sequentially"

  2508     hence "l \<in> s"

  2509     proof(cases "\<forall>n. x n \<noteq> l")

  2510       case False thus "l\<in>s" using as(1) by auto

  2511     next

  2512       case True note cas = this

  2513       with as(2) have "infinite (range x)" using sequence_infinite_lemma[of x l] by auto

  2514       then obtain l' where "l'\<in>s" "l' islimpt (range x)" using assms[THEN spec[where x="range x"]] as(1) by auto

  2515       thus "l\<in>s" using sequence_unique_limpt[of x l l'] using as cas by auto

  2516     qed  }

  2517   thus ?thesis unfolding closed_sequential_limits by fast

  2518 qed

  2519

  2520 lemma compact_imp_closed:

  2521   fixes s :: "'a::t2_space set"

  2522   assumes "compact s" shows "closed s"

  2523 unfolding closed_def

  2524 proof (rule openI)

  2525   fix y assume "y \<in> - s"

  2526   let ?C = "\<Union>x\<in>s. {u. open u \<and> x \<in> u \<and> eventually (\<lambda>y. y \<notin> u) (nhds y)}"

  2527   note compact s

  2528   moreover have "\<forall>u\<in>?C. open u" by simp

  2529   moreover have "s \<subseteq> \<Union>?C"

  2530   proof

  2531     fix x assume "x \<in> s"

  2532     with y \<in> - s have "x \<noteq> y" by clarsimp

  2533     hence "\<exists>u v. open u \<and> open v \<and> x \<in> u \<and> y \<in> v \<and> u \<inter> v = {}"

  2534       by (rule hausdorff)

  2535     with x \<in> s show "x \<in> \<Union>?C"

  2536       unfolding eventually_nhds by auto

  2537   qed

  2538   ultimately obtain D where "D \<subseteq> ?C" and "finite D" and "s \<subseteq> \<Union>D"

  2539     by (rule compactE)

  2540   from D \<subseteq> ?C have "\<forall>x\<in>D. eventually (\<lambda>y. y \<notin> x) (nhds y)" by auto

  2541   with finite D have "eventually (\<lambda>y. y \<notin> \<Union>D) (nhds y)"

  2542     by (simp add: eventually_Ball_finite)

  2543   with s \<subseteq> \<Union>D have "eventually (\<lambda>y. y \<notin> s) (nhds y)"

  2544     by (auto elim!: eventually_mono [rotated])

  2545   thus "\<exists>t. open t \<and> y \<in> t \<and> t \<subseteq> - s"

  2546     by (simp add: eventually_nhds subset_eq)

  2547 qed

  2548

  2549 text{* In particular, some common special cases. *}

  2550

  2551 lemma compact_empty[simp]:

  2552  "compact {}"

  2553   unfolding compact_eq_heine_borel

  2554   by auto

  2555

  2556 lemma compact_union [intro]:

  2557   assumes "compact s" "compact t" shows " compact (s \<union> t)"

  2558 proof (rule compactI)

  2559   fix f assume *: "Ball f open" "s \<union> t \<subseteq> \<Union>f"

  2560   from * compact s obtain s' where "s' \<subseteq> f \<and> finite s' \<and> s \<subseteq> \<Union>s'"

  2561     unfolding compact_eq_heine_borel by (auto elim!: allE[of _ f]) metis

  2562   moreover from * compact t obtain t' where "t' \<subseteq> f \<and> finite t' \<and> t \<subseteq> \<Union>t'"

  2563     unfolding compact_eq_heine_borel by (auto elim!: allE[of _ f]) metis

  2564   ultimately show "\<exists>f'\<subseteq>f. finite f' \<and> s \<union> t \<subseteq> \<Union>f'"

  2565     by (auto intro!: exI[of _ "s' \<union> t'"])

  2566 qed

  2567

  2568 lemma compact_Union [intro]: "finite S \<Longrightarrow> (\<And>T. T \<in> S \<Longrightarrow> compact T) \<Longrightarrow> compact (\<Union>S)"

  2569   by (induct set: finite) auto

  2570

  2571 lemma compact_UN [intro]:

  2572   "finite A \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> compact (B x)) \<Longrightarrow> compact (\<Union>x\<in>A. B x)"

  2573   unfolding SUP_def by (rule compact_Union) auto

  2574

  2575 lemma compact_inter_closed [intro]:

  2576   assumes "compact s" and "closed t"

  2577   shows "compact (s \<inter> t)"

  2578 proof (rule compactI)

  2579   fix C assume C: "\<forall>c\<in>C. open c" and cover: "s \<inter> t \<subseteq> \<Union>C"

  2580   from C closed t have "\<forall>c\<in>C \<union> {-t}. open c" by auto

  2581   moreover from cover have "s \<subseteq> \<Union>(C \<union> {-t})" by auto

  2582   ultimately have "\<exists>D\<subseteq>C \<union> {-t}. finite D \<and> s \<subseteq> \<Union>D"

  2583     using compact s unfolding compact_eq_heine_borel by auto

  2584   then guess D ..

  2585   then show "\<exists>D\<subseteq>C. finite D \<and> s \<inter> t \<subseteq> \<Union>D"

  2586     by (intro exI[of _ "D - {-t}"]) auto

  2587 qed

  2588

  2589 lemma closed_inter_compact [intro]:

  2590   assumes "closed s" and "compact t"

  2591   shows "compact (s \<inter> t)"

  2592   using compact_inter_closed [of t s] assms

  2593   by (simp add: Int_commute)

  2594

  2595 lemma compact_inter [intro]:

  2596   fixes s t :: "'a :: t2_space set"

  2597   assumes "compact s" and "compact t"

  2598   shows "compact (s \<inter> t)"

  2599   using assms by (intro compact_inter_closed compact_imp_closed)

  2600

  2601 lemma compact_sing [simp]: "compact {a}"

  2602   unfolding compact_eq_heine_borel by auto

  2603

  2604 lemma compact_insert [simp]:

  2605   assumes "compact s" shows "compact (insert x s)"

  2606 proof -

  2607   have "compact ({x} \<union> s)"

  2608     using compact_sing assms by (rule compact_union)

  2609   thus ?thesis by simp

  2610 qed

  2611

  2612 lemma finite_imp_compact:

  2613   shows "finite s \<Longrightarrow> compact s"

  2614   by (induct set: finite) simp_all

  2615

  2616 lemma open_delete:

  2617   fixes s :: "'a::t1_space set"

  2618   shows "open s \<Longrightarrow> open (s - {x})"

  2619   by (simp add: open_Diff)

  2620

  2621 text{* Finite intersection property *}

  2622

  2623 lemma inj_setminus: "inj_on uminus (A::'a set set)"

  2624   by (auto simp: inj_on_def)

  2625

  2626 lemma compact_fip:

  2627   "compact U \<longleftrightarrow>

  2628     (\<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> {})"

  2629   (is "_ \<longleftrightarrow> ?R")

  2630 proof (safe intro!: compact_eq_heine_borel[THEN iffD2])

  2631   fix A assume "compact U" and A: "\<forall>a\<in>A. closed a" "U \<inter> \<Inter>A = {}"

  2632     and fi: "\<forall>B \<subseteq> A. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {}"

  2633   from A have "(\<forall>a\<in>uminusA. open a) \<and> U \<subseteq> \<Union>uminusA"

  2634     by auto

  2635   with compact U obtain B where "B \<subseteq> A" "finite (uminusB)" "U \<subseteq> \<Union>(uminusB)"

  2636     unfolding compact_eq_heine_borel by (metis subset_image_iff)

  2637   with fi[THEN spec, of B] show False

  2638     by (auto dest: finite_imageD intro: inj_setminus)

  2639 next

  2640   fix A assume ?R and cover: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A"

  2641   from cover have "U \<inter> \<Inter>(uminusA) = {}" "\<forall>a\<in>uminusA. closed a"

  2642     by auto

  2643   with ?R obtain B where "B \<subseteq> A" "finite (uminusB)" "U \<inter> \<Inter>uminusB = {}"

  2644     by (metis subset_image_iff)

  2645   then show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"

  2646     by  (auto intro!: exI[of _ B] inj_setminus dest: finite_imageD)

  2647 qed

  2648

  2649 lemma compact_imp_fip:

  2650   "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>

  2651     s \<inter> (\<Inter> f) \<noteq> {}"

  2652   unfolding compact_fip by auto

  2653

  2654 text{*Compactness expressed with filters*}

  2655

  2656 definition "filter_from_subbase B = Abs_filter (\<lambda>P. \<exists>X \<subseteq> B. finite X \<and> Inf X \<le> P)"

  2657

  2658 lemma eventually_filter_from_subbase:

  2659   "eventually P (filter_from_subbase B) \<longleftrightarrow> (\<exists>X \<subseteq> B. finite X \<and> Inf X \<le> P)"

  2660     (is "_ \<longleftrightarrow> ?R P")

  2661   unfolding filter_from_subbase_def

  2662 proof (rule eventually_Abs_filter is_filter.intro)+

  2663   show "?R (\<lambda>x. True)"

  2664     by (rule exI[of _ "{}"]) (simp add: le_fun_def)

  2665 next

  2666   fix P Q assume "?R P" then guess X ..

  2667   moreover assume "?R Q" then guess Y ..

  2668   ultimately show "?R (\<lambda>x. P x \<and> Q x)"

  2669     by (intro exI[of _ "X \<union> Y"]) auto

  2670 next

  2671   fix P Q

  2672   assume "?R P" then guess X ..

  2673   moreover assume "\<forall>x. P x \<longrightarrow> Q x"

  2674   ultimately show "?R Q"

  2675     by (intro exI[of _ X]) auto

  2676 qed

  2677

  2678 lemma eventually_filter_from_subbaseI: "P \<in> B \<Longrightarrow> eventually P (filter_from_subbase B)"

  2679   by (subst eventually_filter_from_subbase) (auto intro!: exI[of _ "{P}"])

  2680

  2681 lemma filter_from_subbase_not_bot:

  2682   "\<forall>X \<subseteq> B. finite X \<longrightarrow> Inf X \<noteq> bot \<Longrightarrow> filter_from_subbase B \<noteq> bot"

  2683   unfolding trivial_limit_def eventually_filter_from_subbase by auto

  2684

  2685 lemma closure_iff_nhds_not_empty:

  2686   "x \<in> closure X \<longleftrightarrow> (\<forall>A. \<forall>S\<subseteq>A. open S \<longrightarrow> x \<in> S \<longrightarrow> X \<inter> A \<noteq> {})"

  2687 proof safe

  2688   assume x: "x \<in> closure X"

  2689   fix S A assume "open S" "x \<in> S" "X \<inter> A = {}" "S \<subseteq> A"

  2690   then have "x \<notin> closure (-S)"

  2691     by (auto simp: closure_complement subset_eq[symmetric] intro: interiorI)

  2692   with x have "x \<in> closure X - closure (-S)"

  2693     by auto

  2694   also have "\<dots> \<subseteq> closure (X \<inter> S)"

  2695     using open S open_inter_closure_subset[of S X] by (simp add: closed_Compl ac_simps)

  2696   finally have "X \<inter> S \<noteq> {}" by auto

  2697   then show False using X \<inter> A = {} S \<subseteq> A by auto

  2698 next

  2699   assume "\<forall>A S. S \<subseteq> A \<longrightarrow> open S \<longrightarrow> x \<in> S \<longrightarrow> X \<inter> A \<noteq> {}"

  2700   from this[THEN spec, of "- X", THEN spec, of "- closure X"]

  2701   show "x \<in> closure X"

  2702     by (simp add: closure_subset open_Compl)

  2703 qed

  2704

  2705 lemma compact_filter:

  2706   "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))"

  2707 proof (intro allI iffI impI compact_fip[THEN iffD2] notI)

  2708   fix F assume "compact U" and F: "F \<noteq> bot" "eventually (\<lambda>x. x \<in> U) F"

  2709   from F have "U \<noteq> {}"

  2710     by (auto simp: eventually_False)

  2711

  2712   def Z \<equiv> "closure  {A. eventually (\<lambda>x. x \<in> A) F}"

  2713   then have "\<forall>z\<in>Z. closed z"

  2714     by auto

  2715   moreover

  2716   have ev_Z: "\<And>z. z \<in> Z \<Longrightarrow> eventually (\<lambda>x. x \<in> z) F"

  2717     unfolding Z_def by (auto elim: eventually_elim1 intro: set_mp[OF closure_subset])

  2718   have "(\<forall>B \<subseteq> Z. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {})"

  2719   proof (intro allI impI)

  2720     fix B assume "finite B" "B \<subseteq> Z"

  2721     with finite B ev_Z have "eventually (\<lambda>x. \<forall>b\<in>B. x \<in> b) F"

  2722       by (auto intro!: eventually_Ball_finite)

  2723     with F(2) have "eventually (\<lambda>x. x \<in> U \<inter> (\<Inter>B)) F"

  2724       by eventually_elim auto

  2725     with F show "U \<inter> \<Inter>B \<noteq> {}"

  2726       by (intro notI) (simp add: eventually_False)

  2727   qed

  2728   ultimately have "U \<inter> \<Inter>Z \<noteq> {}"

  2729     using compact U unfolding compact_fip by blast

  2730   then obtain x where "x \<in> U" and x: "\<And>z. z \<in> Z \<Longrightarrow> x \<in> z" by auto

  2731

  2732   have "\<And>P. eventually P (inf (nhds x) F) \<Longrightarrow> P \<noteq> bot"

  2733     unfolding eventually_inf eventually_nhds

  2734   proof safe

  2735     fix P Q R S

  2736     assume "eventually R F" "open S" "x \<in> S"

  2737     with open_inter_closure_eq_empty[of S "{x. R x}"] x[of "closure {x. R x}"]

  2738     have "S \<inter> {x. R x} \<noteq> {}" by (auto simp: Z_def)

  2739     moreover assume "Ball S Q" "\<forall>x. Q x \<and> R x \<longrightarrow> bot x"

  2740     ultimately show False by (auto simp: set_eq_iff)

  2741   qed

  2742   with x \<in> U show "\<exists>x\<in>U. inf (nhds x) F \<noteq> bot"

  2743     by (metis eventually_bot)

  2744 next

  2745   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 = {}"

  2746

  2747   def P' \<equiv> "(\<lambda>a (x::'a). x \<in> a)"

  2748   then have inj_P': "\<And>A. inj_on P' A"

  2749     by (auto intro!: inj_onI simp: fun_eq_iff)

  2750   def F \<equiv> "filter_from_subbase (P'  insert U A)"

  2751   have "F \<noteq> bot"

  2752     unfolding F_def

  2753   proof (safe intro!: filter_from_subbase_not_bot)

  2754     fix X assume "X \<subseteq> P'  insert U A" "finite X" "Inf X = bot"

  2755     then obtain B where "B \<subseteq> insert U A" "finite B" and B: "Inf (P'  B) = bot"

  2756       unfolding subset_image_iff by (auto intro: inj_P' finite_imageD)

  2757     with A(2)[THEN spec, of "B - {U}"] have "U \<inter> \<Inter>(B - {U}) \<noteq> {}" by auto

  2758     with B show False by (auto simp: P'_def fun_eq_iff)

  2759   qed

  2760   moreover have "eventually (\<lambda>x. x \<in> U) F"

  2761     unfolding F_def by (rule eventually_filter_from_subbaseI) (auto simp: P'_def)

  2762   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)"

  2763   ultimately obtain x where "x \<in> U" and x: "inf (nhds x) F \<noteq> bot"

  2764     by auto

  2765

  2766   { fix V assume "V \<in> A"

  2767     then have V: "eventually (\<lambda>x. x \<in> V) F"

  2768       by (auto simp add: F_def image_iff P'_def intro!: eventually_filter_from_subbaseI)

  2769     have "x \<in> closure V"

  2770       unfolding closure_iff_nhds_not_empty

  2771     proof (intro impI allI)

  2772       fix S A assume "open S" "x \<in> S" "S \<subseteq> A"

  2773       then have "eventually (\<lambda>x. x \<in> A) (nhds x)" by (auto simp: eventually_nhds)

  2774       with V have "eventually (\<lambda>x. x \<in> V \<inter> A) (inf (nhds x) F)"

  2775         by (auto simp: eventually_inf)

  2776       with x show "V \<inter> A \<noteq> {}"

  2777         by (auto simp del: Int_iff simp add: trivial_limit_def)

  2778     qed

  2779     then have "x \<in> V"

  2780       using V \<in> A A(1) by simp }

  2781   with x\<in>U have "x \<in> U \<inter> \<Inter>A" by auto

  2782   with U \<inter> \<Inter>A = {} show False by auto

  2783 qed

  2784

  2785 definition "countably_compact U \<longleftrightarrow>

  2786     (\<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))"

  2787

  2788 lemma countably_compactE:

  2789   assumes "countably_compact s" and "\<forall>t\<in>C. open t" and "s \<subseteq> \<Union>C" "countable C"

  2790   obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> \<Union>C'"

  2791   using assms unfolding countably_compact_def by metis

  2792

  2793 lemma countably_compactI:

  2794   assumes "\<And>C. \<forall>t\<in>C. open t \<Longrightarrow> s \<subseteq> \<Union>C \<Longrightarrow> countable C \<Longrightarrow> (\<exists>C'\<subseteq>C. finite C' \<and> s \<subseteq> \<Union>C')"

  2795   shows "countably_compact s"

  2796   using assms unfolding countably_compact_def by metis

  2797

  2798 lemma compact_imp_countably_compact: "compact U \<Longrightarrow> countably_compact U"

  2799   by (auto simp: compact_eq_heine_borel countably_compact_def)

  2800

  2801 lemma countably_compact_imp_compact:

  2802   assumes "countably_compact U"

  2803   assumes ccover: "countable B" "\<forall>b\<in>B. open b"

  2804   assumes basis: "\<And>T x. open T \<Longrightarrow> x \<in> T \<Longrightarrow> x \<in> U \<Longrightarrow> \<exists>b\<in>B. x \<in> b \<and> b \<inter> U \<subseteq> T"

  2805   shows "compact U"

  2806   using countably_compact U unfolding compact_eq_heine_borel countably_compact_def

  2807 proof safe

  2808   fix A assume A: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A"

  2809   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)"

  2810

  2811   moreover def C \<equiv> "{b\<in>B. \<exists>a\<in>A. b \<inter> U \<subseteq> a}"

  2812   ultimately have "countable C" "\<forall>a\<in>C. open a"

  2813     unfolding C_def using ccover by auto

  2814   moreover

  2815   have "\<Union>A \<inter> U \<subseteq> \<Union>C"

  2816   proof safe

  2817     fix x a assume "x \<in> U" "x \<in> a" "a \<in> A"

  2818     with basis[of a x] A obtain b where "b \<in> B" "x \<in> b" "b \<inter> U \<subseteq> a" by blast

  2819     with a \<in> A show "x \<in> \<Union>C" unfolding C_def

  2820       by auto

  2821   qed

  2822   then have "U \<subseteq> \<Union>C" using U \<subseteq> \<Union>A by auto

  2823   ultimately obtain T where "T\<subseteq>C" "finite T" "U \<subseteq> \<Union>T"

  2824     using * by metis

  2825   moreover then have "\<forall>t\<in>T. \<exists>a\<in>A. t \<inter> U \<subseteq> a"

  2826     by (auto simp: C_def)

  2827   then guess f unfolding bchoice_iff Bex_def ..

  2828   ultimately show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"

  2829     unfolding C_def by (intro exI[of _ "fT"]) fastforce

  2830 qed

  2831

  2832 lemma countably_compact_imp_compact_second_countable:

  2833   "countably_compact U \<Longrightarrow> compact (U :: 'a :: second_countable_topology set)"

  2834 proof (rule countably_compact_imp_compact)

  2835   fix T and x :: 'a assume "open T" "x \<in> T"

  2836   from topological_basisE[OF is_basis this] guess b .

  2837   then show "\<exists>b\<in>SOME B. countable B \<and> topological_basis B. x \<in> b \<and> b \<inter> U \<subseteq> T" by auto

  2838 qed (insert countable_basis topological_basis_open[OF is_basis], auto)

  2839

  2840 subsubsection{* Sequential compactness *}

  2841

  2842 definition

  2843   seq_compact :: "'a::topological_space set \<Rightarrow> bool" where

  2844   "seq_compact S \<longleftrightarrow>

  2845    (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow>

  2846        (\<exists>l\<in>S. \<exists>r. subseq r \<and> ((f o r) ---> l) sequentially))"

  2847

  2848 lemma seq_compact_imp_countably_compact:

  2849   fixes U :: "'a :: first_countable_topology set"

  2850   assumes "seq_compact U"

  2851   shows "countably_compact U"

  2852 proof (safe intro!: countably_compactI)

  2853   fix A assume A: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A" "countable A"

  2854   have subseq: "\<And>X. range X \<subseteq> U \<Longrightarrow> \<exists>r x. x \<in> U \<and> subseq r \<and> (X \<circ> r) ----> x"

  2855     using seq_compact U by (fastforce simp: seq_compact_def subset_eq)

  2856   show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"

  2857   proof cases

  2858     assume "finite A" with A show ?thesis by auto

  2859   next

  2860     assume "infinite A"

  2861     then have "A \<noteq> {}" by auto

  2862     show ?thesis

  2863     proof (rule ccontr)

  2864       assume "\<not> (\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T)"

  2865       then have "\<forall>T. \<exists>x. T \<subseteq> A \<and> finite T \<longrightarrow> (x \<in> U - \<Union>T)" by auto

  2866       then obtain X' where T: "\<And>T. T \<subseteq> A \<Longrightarrow> finite T \<Longrightarrow> X' T \<in> U - \<Union>T" by metis

  2867       def X \<equiv> "\<lambda>n. X' (from_nat_into A  {.. n})"

  2868       have X: "\<And>n. X n \<in> U - (\<Union>i\<le>n. from_nat_into A i)"

  2869         using A \<noteq> {} unfolding X_def SUP_def by (intro T) (auto intro: from_nat_into)

  2870       then have "range X \<subseteq> U" by auto

  2871       with subseq[of X] obtain r x where "x \<in> U" and r: "subseq r" "(X \<circ> r) ----> x" by auto

  2872       from x\<in>U U \<subseteq> \<Union>A from_nat_into_surj[OF countable A]

  2873       obtain n where "x \<in> from_nat_into A n" by auto

  2874       with r(2) A(1) from_nat_into[OF A \<noteq> {}, of n]

  2875       have "eventually (\<lambda>i. X (r i) \<in> from_nat_into A n) sequentially"

  2876         unfolding tendsto_def by (auto simp: comp_def)

  2877       then obtain N where "\<And>i. N \<le> i \<Longrightarrow> X (r i) \<in> from_nat_into A n"

  2878         by (auto simp: eventually_sequentially)

  2879       moreover from X have "\<And>i. n \<le> r i \<Longrightarrow> X (r i) \<notin> from_nat_into A n"

  2880         by auto

  2881       moreover from subseq r[THEN seq_suble, of "max n N"] have "\<exists>i. n \<le> r i \<and> N \<le> i"

  2882         by (auto intro!: exI[of _ "max n N"])

  2883       ultimately show False

  2884         by auto

  2885     qed

  2886   qed

  2887 qed

  2888

  2889 lemma compact_imp_seq_compact:

  2890   fixes U :: "'a :: first_countable_topology set"

  2891   assumes "compact U" shows "seq_compact U"

  2892   unfolding seq_compact_def

  2893 proof safe

  2894   fix X :: "nat \<Rightarrow> 'a" assume "\<forall>n. X n \<in> U"

  2895   then have "eventually (\<lambda>x. x \<in> U) (filtermap X sequentially)"

  2896     by (auto simp: eventually_filtermap)

  2897   moreover have "filtermap X sequentially \<noteq> bot"

  2898     by (simp add: trivial_limit_def eventually_filtermap)

  2899   ultimately obtain x where "x \<in> U" and x: "inf (nhds x) (filtermap X sequentially) \<noteq> bot" (is "?F \<noteq> _")

  2900     using compact U by (auto simp: compact_filter)

  2901

  2902   from countable_basis_at_decseq[of x] guess A . note A = this

  2903   def s \<equiv> "\<lambda>n i. SOME j. i < j \<and> X j \<in> A (Suc n)"

  2904   { fix n i

  2905     have "\<exists>a. i < a \<and> X a \<in> A (Suc n)"

  2906     proof (rule ccontr)

  2907       assume "\<not> (\<exists>a>i. X a \<in> A (Suc n))"

  2908       then have "\<And>a. Suc i \<le> a \<Longrightarrow> X a \<notin> A (Suc n)" by auto

  2909       then have "eventually (\<lambda>x. x \<notin> A (Suc n)) (filtermap X sequentially)"

  2910         by (auto simp: eventually_filtermap eventually_sequentially)

  2911       moreover have "eventually (\<lambda>x. x \<in> A (Suc n)) (nhds x)"

  2912         using A(1,2)[of "Suc n"] by (auto simp: eventually_nhds)

  2913       ultimately have "eventually (\<lambda>x. False) ?F"

  2914         by (auto simp add: eventually_inf)

  2915       with x show False

  2916         by (simp add: eventually_False)

  2917     qed

  2918     then have "i < s n i" "X (s n i) \<in> A (Suc n)"

  2919       unfolding s_def by (auto intro: someI2_ex) }

  2920   note s = this

  2921   def r \<equiv> "nat_rec (s 0 0) s"

  2922   have "subseq r"

  2923     by (auto simp: r_def s subseq_Suc_iff)

  2924   moreover

  2925   have "(\<lambda>n. X (r n)) ----> x"

  2926   proof (rule topological_tendstoI)

  2927     fix S assume "open S" "x \<in> S"

  2928     with A(3) have "eventually (\<lambda>i. A i \<subseteq> S) sequentially" by auto

  2929     moreover

  2930     { fix i assume "Suc 0 \<le> i" then have "X (r i) \<in> A i"

  2931         by (cases i) (simp_all add: r_def s) }

  2932     then have "eventually (\<lambda>i. X (r i) \<in> A i) sequentially" by (auto simp: eventually_sequentially)

  2933     ultimately show "eventually (\<lambda>i. X (r i) \<in> S) sequentially"

  2934       by eventually_elim auto

  2935   qed

  2936   ultimately show "\<exists>x \<in> U. \<exists>r. subseq r \<and> (X \<circ> r) ----> x"

  2937     using x \<in> U by (auto simp: convergent_def comp_def)

  2938 qed

  2939

  2940 lemma seq_compact_eq_compact:

  2941   fixes U :: "'a :: second_countable_topology set"

  2942   shows "seq_compact U \<longleftrightarrow> compact U"

  2943   using compact_imp_seq_compact seq_compact_imp_countably_compact countably_compact_imp_compact_second_countable

  2944     by blast

  2945

  2946 lemma seq_compactI:

  2947   assumes "\<And>f. \<forall>n. f n \<in> S \<Longrightarrow> \<exists>l\<in>S. \<exists>r. subseq r \<and> ((f o r) ---> l) sequentially"

  2948   shows "seq_compact S"

  2949   unfolding seq_compact_def using assms by fast

  2950

  2951 lemma seq_compactE:

  2952   assumes "seq_compact S" "\<forall>n. f n \<in> S"

  2953   obtains l r where "l \<in> S" "subseq r" "((f \<circ> r) ---> l) sequentially"

  2954   using assms unfolding seq_compact_def by fast

  2955

  2956 lemma bolzano_weierstrass_imp_seq_compact:

  2957   fixes s :: "'a::{t1_space, first_countable_topology} set"

  2958   assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"

  2959   shows "seq_compact s"

  2960 proof -

  2961   { fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> s"

  2962     have "\<exists>l\<in>s. \<exists>r. subseq r \<and> (f \<circ> r) ----> l"

  2963     proof (cases "finite (range f)")

  2964       case True

  2965       obtain l where "infinite {n. f n = f l}"

  2966         using pigeonhole_infinite[OF _ True] by auto

  2967       then obtain r where "subseq r" and fr: "\<forall>n. f (r n) = f l"

  2968         using infinite_enumerate by blast

  2969       hence "subseq r \<and> (f \<circ> r) ----> f l"

  2970         by (simp add: fr tendsto_const o_def)

  2971       with f show "\<exists>l\<in>s. \<exists>r. subseq r \<and> (f \<circ> r) ----> l"

  2972         by auto

  2973     next

  2974       case False

  2975       with f assms have "\<exists>x\<in>s. x islimpt (range f)" by auto

  2976       then obtain l where "l \<in> s" "l islimpt (range f)" ..

  2977       have "\<exists>r. subseq r \<and> (f \<circ> r) ----> l"

  2978         using l islimpt (range f)

  2979         by (rule islimpt_range_imp_convergent_subsequence)

  2980       with l \<in> s show "\<exists>l\<in>s. \<exists>r. subseq r \<and> (f \<circ> r) ----> l" ..

  2981     qed

  2982   }

  2983   thus ?thesis unfolding seq_compact_def by auto

  2984 qed

  2985

  2986 subsubsection{* Total boundedness *}

  2987

  2988 lemma cauchy_def:

  2989   "Cauchy s \<longleftrightarrow> (\<forall>e>0. \<exists>N. \<forall>m n. m \<ge> N \<and> n \<ge> N --> dist(s m)(s n) < e)"

  2990 unfolding Cauchy_def by blast

  2991

  2992 fun helper_1::"('a::metric_space set) \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> 'a" where

  2993   "helper_1 s e n = (SOME y::'a. y \<in> s \<and> (\<forall>m<n. \<not> (dist (helper_1 s e m) y < e)))"

  2994 declare helper_1.simps[simp del]

  2995

  2996 lemma seq_compact_imp_totally_bounded:

  2997   assumes "seq_compact s"

  2998   shows "\<forall>e>0. \<exists>k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> (\<Union>((\<lambda>x. ball x e)  k))"

  2999 proof(rule, rule, rule ccontr)

  3000   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)"

  3001   def x \<equiv> "helper_1 s e"

  3002   { fix n

  3003     have "x n \<in> s \<and> (\<forall>m<n. \<not> dist (x m) (x n) < e)"

  3004     proof(induct_tac rule:nat_less_induct)

  3005       fix n  def Q \<equiv> "(\<lambda>y. y \<in> s \<and> (\<forall>m<n. \<not> dist (x m) y < e))"

  3006       assume as:"\<forall>m<n. x m \<in> s \<and> (\<forall>ma<m. \<not> dist (x ma) (x m) < e)"

  3007       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

  3008       then obtain z where z:"z\<in>s" "z \<notin> (\<Union>x\<in>x  {0..<n}. ball x e)" unfolding subset_eq by auto

  3009       have "Q (x n)" unfolding x_def and helper_1.simps[of s e n]

  3010         apply(rule someI2[where a=z]) unfolding x_def[symmetric] and Q_def using z by auto

  3011       thus "x n \<in> s \<and> (\<forall>m<n. \<not> dist (x m) (x n) < e)" unfolding Q_def by auto

  3012     qed }

  3013   hence "\<forall>n::nat. x n \<in> s" and x:"\<forall>n. \<forall>m < n. \<not> (dist (x m) (x n) < e)" by blast+

  3014   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

  3015   from this(3) have "Cauchy (x \<circ> r)" using LIMSEQ_imp_Cauchy by auto

  3016   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

  3017   show False

  3018     using N[THEN spec[where x=N], THEN spec[where x="N+1"]]

  3019     using r[unfolded subseq_def, THEN spec[where x=N], THEN spec[where x="N+1"]]

  3020     using x[THEN spec[where x="r (N+1)"], THEN spec[where x="r (N)"]] by auto

  3021 qed

  3022

  3023 subsubsection{* Heine-Borel theorem *}

  3024

  3025 text {* Following Burkill \& Burkill vol. 2. *}

  3026

  3027 lemma heine_borel_lemma: fixes s::"'a::metric_space set"

  3028   assumes "seq_compact s"  "s \<subseteq> (\<Union> t)"  "\<forall>b \<in> t. open b"

  3029   shows "\<exists>e>0. \<forall>x \<in> s. \<exists>b \<in> t. ball x e \<subseteq> b"

  3030 proof(rule ccontr)

  3031   assume "\<not> (\<exists>e>0. \<forall>x\<in>s. \<exists>b\<in>t. ball x e \<subseteq> b)"

  3032   hence cont:"\<forall>e>0. \<exists>x\<in>s. \<forall>xa\<in>t. \<not> (ball x e \<subseteq> xa)" by auto

  3033   { fix n::nat

  3034     have "1 / real (n + 1) > 0" by auto

  3035     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 }

  3036   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

  3037   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)"

  3038     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

  3039

  3040   then obtain l r where l:"l\<in>s" and r:"subseq r" and lr:"((f \<circ> r) ---> l) sequentially"

  3041     using assms(1)[unfolded seq_compact_def, THEN spec[where x=f]] by auto

  3042

  3043   obtain b where "l\<in>b" "b\<in>t" using assms(2) and l by auto

  3044   then obtain e where "e>0" and e:"\<forall>z. dist z l < e \<longrightarrow> z\<in>b"

  3045     using assms(3)[THEN bspec[where x=b]] unfolding open_dist by auto

  3046

  3047   then obtain N1 where N1:"\<forall>n\<ge>N1. dist ((f \<circ> r) n) l < e / 2"

  3048     using lr[unfolded LIMSEQ_def, THEN spec[where x="e/2"]] by auto

  3049

  3050   obtain N2::nat where N2:"N2>0" "inverse (real N2) < e /2" using real_arch_inv[of "e/2"] and e>0 by auto

  3051   have N2':"inverse (real (r (N1 + N2) +1 )) < e/2"

  3052     apply(rule order_less_trans) apply(rule less_imp_inverse_less) using N2

  3053     using seq_suble[OF r, of "N1 + N2"] by auto

  3054

  3055   def x \<equiv> "(f (r (N1 + N2)))"

  3056   have x:"\<not> ball x (inverse (real (r (N1 + N2) + 1))) \<subseteq> b" unfolding x_def

  3057     using f[THEN spec[where x="r (N1 + N2)"]] using b\<in>t by auto

  3058   have "\<exists>y\<in>ball x (inverse (real (r (N1 + N2) + 1))). y\<notin>b" apply(rule ccontr) using x by auto

  3059   then obtain y where y:"y \<in> ball x (inverse (real (r (N1 + N2) + 1)))" "y \<notin> b" by auto

  3060

  3061   have "dist x l < e/2" using N1 unfolding x_def o_def by auto

  3062   hence "dist y l < e" using y N2' using dist_triangle[of y l x]by (auto simp add:dist_commute)

  3063

  3064   thus False using e and y\<notin>b by auto

  3065 qed

  3066

  3067 lemma seq_compact_imp_heine_borel:

  3068   fixes s :: "'a :: metric_space set"

  3069   shows "seq_compact s \<Longrightarrow> compact s"

  3070   unfolding compact_eq_heine_borel

  3071 proof clarify

  3072   fix f assume "seq_compact s" " \<forall>t\<in>f. open t" "s \<subseteq> \<Union>f"

  3073   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

  3074   hence "\<forall>x\<in>s. \<exists>b. b\<in>f \<and> ball x e \<subseteq> b" by auto

  3075   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

  3076   then obtain  bb where bb:"\<forall>x\<in>s. (bb x) \<in> f \<and> ball x e \<subseteq> (bb x)" by blast

  3077

  3078   from seq_compact s have  "\<exists> k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> \<Union>(\<lambda>x. ball x e)  k"

  3079     using seq_compact_imp_totally_bounded[of s] e>0 by auto

  3080   then obtain k where k:"finite k" "k \<subseteq> s" "s \<subseteq> \<Union>(\<lambda>x. ball x e)  k" by auto

  3081

  3082   have "finite (bb  k)" using k(1) by auto

  3083   moreover

  3084   { fix x assume "x\<in>s"

  3085     hence "x\<in>\<Union>(\<lambda>x. ball x e)  k" using k(3)  unfolding subset_eq by auto

  3086     hence "\<exists>X\<in>bb  k. x \<in> X" using bb k(2) by blast

  3087     hence "x \<in> \<Union>(bb  k)" using  Union_iff[of x "bb  k"] by auto

  3088   }

  3089   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

  3090 qed

  3091

  3092 lemma compact_eq_seq_compact_metric:

  3093   "compact (s :: 'a::metric_space set) \<longleftrightarrow> seq_compact s"

  3094   using compact_imp_seq_compact seq_compact_imp_heine_borel by blast

  3095

  3096 lemma compact_def:

  3097   "compact (S :: 'a::metric_space set) \<longleftrightarrow>

  3098    (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow>

  3099        (\<exists>l\<in>S. \<exists>r. subseq r \<and> ((f o r) ---> l) sequentially)) "

  3100   unfolding compact_eq_seq_compact_metric seq_compact_def by auto

  3101

  3102 text {*

  3103   A metric space (or topological vector space) is said to have the

  3104   Heine-Borel property if every closed and bounded subset is compact.

  3105 *}

  3106

  3107 class heine_borel = metric_space +

  3108   assumes bounded_imp_convergent_subsequence:

  3109     "bounded s \<Longrightarrow> \<forall>n. f n \<in> s

  3110       \<Longrightarrow> \<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"

  3111

  3112 lemma bounded_closed_imp_seq_compact:

  3113   fixes s::"'a::heine_borel set"

  3114   assumes "bounded s" and "closed s" shows "seq_compact s"

  3115 proof (unfold seq_compact_def, clarify)

  3116   fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> s"

  3117   obtain l r where r: "subseq r" and l: "((f \<circ> r) ---> l) sequentially"

  3118     using bounded_imp_convergent_subsequence [OF bounded s \<forall>n. f n \<in> s] by auto

  3119   from f have fr: "\<forall>n. (f \<circ> r) n \<in> s" by simp

  3120   have "l \<in> s" using closed s fr l

  3121     unfolding closed_sequential_limits by blast

  3122   show "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"

  3123     using l \<in> s r l by blast

  3124 qed

  3125

  3126 lemma lim_subseq:

  3127   "subseq r \<Longrightarrow> (s ---> l) sequentially \<Longrightarrow> ((s o r) ---> l) sequentially"

  3128 unfolding tendsto_def eventually_sequentially o_def

  3129 by (metis seq_suble le_trans)

  3130

  3131 lemma num_Axiom: "EX! g. g 0 = e \<and> (\<forall>n. g (Suc n) = f n (g n))"

  3132   unfolding Ex1_def

  3133   apply (rule_tac x="nat_rec e f" in exI)

  3134   apply (rule conjI)+

  3135 apply (rule def_nat_rec_0, simp)

  3136 apply (rule allI, rule def_nat_rec_Suc, simp)

  3137 apply (rule allI, rule impI, rule ext)

  3138 apply (erule conjE)

  3139 apply (induct_tac x)

  3140 apply simp

  3141 apply (erule_tac x="n" in allE)

  3142 apply (simp)

  3143 done

  3144

  3145 lemma convergent_bounded_increasing: fixes s ::"nat\<Rightarrow>real"

  3146   assumes "incseq s" and "\<forall>n. abs(s n) \<le> b"

  3147   shows "\<exists> l. \<forall>e::real>0. \<exists> N. \<forall>n \<ge> N.  abs(s n - l) < e"

  3148 proof-

  3149   have "isUb UNIV (range s) b" using assms(2) and abs_le_D1 unfolding isUb_def and setle_def by auto

  3150   then obtain t where t:"isLub UNIV (range s) t" using reals_complete[of "range s" ] by auto

  3151   { fix e::real assume "e>0" and as:"\<forall>N. \<exists>n\<ge>N. \<not> \<bar>s n - t\<bar> < e"

  3152     { fix n::nat

  3153       obtain N where "N\<ge>n" and n:"\<bar>s N - t\<bar> \<ge> e" using as[THEN spec[where x=n]] by auto

  3154       have "t \<ge> s N" using isLub_isUb[OF t, unfolded isUb_def setle_def] by auto

  3155       with n have "s N \<le> t - e" using e>0 by auto

  3156       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  }

  3157     hence "isUb UNIV (range s) (t - e)" unfolding isUb_def and setle_def by auto

  3158     hence False using isLub_le_isUb[OF t, of "t - e"] and e>0 by auto  }

  3159   thus ?thesis by blast

  3160 qed

  3161

  3162 lemma convergent_bounded_monotone: fixes s::"nat \<Rightarrow> real"

  3163   assumes "\<forall>n. abs(s n) \<le> b" and "monoseq s"

  3164   shows "\<exists>l. \<forall>e::real>0. \<exists>N. \<forall>n\<ge>N. abs(s n - l) < e"

  3165   using convergent_bounded_increasing[of s b] assms using convergent_bounded_increasing[of "\<lambda>n. - s n" b]

  3166   unfolding monoseq_def incseq_def

  3167   apply auto unfolding minus_add_distrib[THEN sym, unfolded diff_minus[THEN sym]]

  3168   unfolding abs_minus_cancel by(rule_tac x="-l" in exI)auto

  3169

  3170 (* TODO: merge this lemma with the ones above *)

  3171 lemma bounded_increasing_convergent: fixes s::"nat \<Rightarrow> real"

  3172   assumes "bounded {s n| n::nat. True}"  "\<forall>n. (s n) \<le>(s(Suc n))"

  3173   shows "\<exists>l. (s ---> l) sequentially"

  3174 proof-

  3175   obtain a where a:"\<forall>n. \<bar> (s n)\<bar> \<le>  a" using assms(1)[unfolded bounded_iff] by auto

  3176   { fix m::nat

  3177     have "\<And> n. n\<ge>m \<longrightarrow>  (s m) \<le> (s n)"

  3178       apply(induct_tac n) apply simp using assms(2) apply(erule_tac x="na" in allE)

  3179       apply(case_tac "m \<le> na") unfolding not_less_eq_eq by(auto simp add: not_less_eq_eq)  }

  3180   hence "\<forall>m n. m \<le> n \<longrightarrow> (s m) \<le> (s n)" by auto

  3181   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]

  3182     unfolding monoseq_def by auto

  3183   thus ?thesis unfolding LIMSEQ_def apply(rule_tac x="l" in exI)

  3184     unfolding dist_norm  by auto

  3185 qed

  3186

  3187 lemma compact_real_lemma:

  3188   assumes "\<forall>n::nat. abs(s n) \<le> b"

  3189   shows "\<exists>(l::real) r. subseq r \<and> ((s \<circ> r) ---> l) sequentially"

  3190 proof-

  3191   obtain r where r:"subseq r" "monoseq (\<lambda>n. s (r n))"

  3192     using seq_monosub[of s] by auto

  3193   thus ?thesis using convergent_bounded_monotone[of "\<lambda>n. s (r n)" b] and assms

  3194     unfolding tendsto_iff dist_norm eventually_sequentially by auto

  3195 qed

  3196

  3197 instance real :: heine_borel

  3198 proof

  3199   fix s :: "real set" and f :: "nat \<Rightarrow> real"

  3200   assume s: "bounded s" and f: "\<forall>n. f n \<in> s"

  3201   then obtain b where b: "\<forall>n. abs (f n) \<le> b"

  3202     unfolding bounded_iff by auto

  3203   obtain l :: real and r :: "nat \<Rightarrow> nat" where

  3204     r: "subseq r" and l: "((f \<circ> r) ---> l) sequentially"

  3205     using compact_real_lemma [OF b] by auto

  3206   thus "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"

  3207     by auto

  3208 qed

  3209

  3210 lemma compact_lemma:

  3211   fixes f :: "nat \<Rightarrow> 'a::euclidean_space"

  3212   assumes "bounded s" and "\<forall>n. f n \<in> s"

  3213   shows "\<forall>d\<subseteq>Basis. \<exists>l::'a. \<exists> r. subseq r \<and>

  3214         (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) \<bullet> i) (l \<bullet> i) < e) sequentially)"

  3215 proof safe

  3216   fix d :: "'a set" assume d: "d \<subseteq> Basis"

  3217   with finite_Basis have "finite d" by (blast intro: finite_subset)

  3218   from this d show "\<exists>l::'a. \<exists>r. subseq r \<and>

  3219       (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) \<bullet> i) (l \<bullet> i) < e) sequentially)"

  3220   proof(induct d) case empty thus ?case unfolding subseq_def by auto

  3221   next case (insert k d) have k[intro]:"k\<in>Basis" using insert by auto

  3222     have s': "bounded ((\<lambda>x. x \<bullet> k)  s)" using bounded s

  3223       by (auto intro!: bounded_linear_image bounded_linear_inner_left)

  3224     obtain l1::"'a" and r1 where r1:"subseq r1" and

  3225       lr1:"\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) \<bullet> i) (l1 \<bullet> i) < e) sequentially"

  3226       using insert(3) using insert(4) by auto

  3227     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

  3228     obtain l2 r2 where r2:"subseq r2" and lr2:"((\<lambda>i. f (r1 (r2 i)) \<bullet> k) ---> l2) sequentially"

  3229       using bounded_imp_convergent_subsequence[OF s' f'] unfolding o_def by auto

  3230     def r \<equiv> "r1 \<circ> r2" have r:"subseq r"

  3231       using r1 and r2 unfolding r_def o_def subseq_def by auto

  3232     moreover

  3233     def l \<equiv> "(\<Sum>i\<in>Basis. (if i = k then l2 else l1\<bullet>i) *\<^sub>R i)::'a"

  3234     { fix e::real assume "e>0"

  3235       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

  3236       from lr2 e>0 have N2:"eventually (\<lambda>n. dist (f (r1 (r2 n)) \<bullet> k) l2 < e) sequentially" by (rule tendstoD)

  3237       from r2 N1 have N1': "eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 (r2 n)) \<bullet> i) (l1 \<bullet> i) < e) sequentially"

  3238         by (rule eventually_subseq)

  3239       have "eventually (\<lambda>n. \<forall>i\<in>(insert k d). dist (f (r n) \<bullet> i) (l \<bullet> i) < e) sequentially"

  3240         using N1' N2

  3241         by eventually_elim (insert insert.prems, auto simp: l_def r_def o_def)

  3242     }

  3243     ultimately show ?case by auto

  3244   qed

  3245 qed

  3246

  3247 instance euclidean_space \<subseteq> heine_borel

  3248 proof

  3249   fix s :: "'a set" and f :: "nat \<Rightarrow> 'a"

  3250   assume s: "bounded s" and f: "\<forall>n. f n \<in> s"

  3251   then obtain l::'a and r where r: "subseq r"

  3252     and l: "\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>Basis. dist (f (r n) \<bullet> i) (l \<bullet> i) < e) sequentially"

  3253     using compact_lemma [OF s f] by blast

  3254   { fix e::real assume "e>0"

  3255     hence "0 < e / real_of_nat DIM('a)" by (auto intro!: divide_pos_pos DIM_positive)

  3256     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"

  3257       by simp

  3258     moreover

  3259     { fix n assume n: "\<forall>i\<in>Basis. dist (f (r n) \<bullet> i) (l \<bullet> i) < e / (real_of_nat DIM('a))"

  3260       have "dist (f (r n)) l \<le> (\<Sum>i\<in>Basis. dist (f (r n) \<bullet> i) (l \<bullet> i))"

  3261         apply(subst euclidean_dist_l2) using zero_le_dist by (rule setL2_le_setsum)

  3262       also have "\<dots> < (\<Sum>i\<in>(Basis::'a set). e / (real_of_nat DIM('a)))"

  3263         apply(rule setsum_strict_mono) using n by auto

  3264       finally have "dist (f (r n)) l < e"

  3265         by auto

  3266     }

  3267     ultimately have "eventually (\<lambda>n. dist (f (r n)) l < e) sequentially"

  3268       by (rule eventually_elim1)

  3269   }

  3270   hence *:"((f \<circ> r) ---> l) sequentially" unfolding o_def tendsto_iff by simp

  3271   with r show "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially" by auto

  3272 qed

  3273

  3274 lemma bounded_fst: "bounded s \<Longrightarrow> bounded (fst  s)"

  3275 unfolding bounded_def

  3276 apply clarify

  3277 apply (rule_tac x="a" in exI)

  3278 apply (rule_tac x="e" in exI)

  3279 apply clarsimp

  3280 apply (drule (1) bspec)

  3281 apply (simp add: dist_Pair_Pair)

  3282 apply (erule order_trans [OF real_sqrt_sum_squares_ge1])

  3283 done

  3284

  3285 lemma bounded_snd: "bounded s \<Longrightarrow> bounded (snd  s)"

  3286 unfolding bounded_def

  3287 apply clarify

  3288 apply (rule_tac x="b" in exI)

  3289 apply (rule_tac x="e" in exI)

  3290 apply clarsimp

  3291 apply (drule (1) bspec)

  3292 apply (simp add: dist_Pair_Pair)

  3293 apply (erule order_trans [OF real_sqrt_sum_squares_ge2])

  3294 done

  3295

  3296 instance prod :: (heine_borel, heine_borel) heine_borel

  3297 proof

  3298   fix s :: "('a * 'b) set" and f :: "nat \<Rightarrow> 'a * 'b"

  3299   assume s: "bounded s" and f: "\<forall>n. f n \<in> s"

  3300   from s have s1: "bounded (fst  s)" by (rule bounded_fst)

  3301   from f have f1: "\<forall>n. fst (f n) \<in> fst  s" by simp

  3302   obtain l1 r1 where r1: "subseq r1"

  3303     and l1: "((\<lambda>n. fst (f (r1 n))) ---> l1) sequentially"

  3304     using bounded_imp_convergent_subsequence [OF s1 f1]

  3305     unfolding o_def by fast

  3306   from s have s2: "bounded (snd  s)" by (rule bounded_snd)

  3307   from f have f2: "\<forall>n. snd (f (r1 n)) \<in> snd  s" by simp

  3308   obtain l2 r2 where r2: "subseq r2"

  3309     and l2: "((\<lambda>n. snd (f (r1 (r2 n)))) ---> l2) sequentially"

  3310     using bounded_imp_convergent_subsequence [OF s2 f2]

  3311     unfolding o_def by fast

  3312   have l1': "((\<lambda>n. fst (f (r1 (r2 n)))) ---> l1) sequentially"

  3313     using lim_subseq [OF r2 l1] unfolding o_def .

  3314   have l: "((f \<circ> (r1 \<circ> r2)) ---> (l1, l2)) sequentially"

  3315     using tendsto_Pair [OF l1' l2] unfolding o_def by simp

  3316   have r: "subseq (r1 \<circ> r2)"

  3317     using r1 r2 unfolding subseq_def by simp

  3318   show "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"

  3319     using l r by fast

  3320 qed

  3321

  3322 subsubsection{* Completeness *}

  3323

  3324 definition

  3325   complete :: "'a::metric_space set \<Rightarrow> bool" where

  3326   "complete s \<longleftrightarrow> (\<forall>f. (\<forall>n. f n \<in> s) \<and> Cauchy f

  3327                       --> (\<exists>l \<in> s. (f ---> l) sequentially))"

  3328

  3329 lemma cauchy: "Cauchy s \<longleftrightarrow> (\<forall>e>0.\<exists> N::nat. \<forall>n\<ge>N. dist(s n)(s N) < e)" (is "?lhs = ?rhs")

  3330 proof-

  3331   { assume ?rhs

  3332     { fix e::real

  3333       assume "e>0"

  3334       with ?rhs obtain N where N:"\<forall>n\<ge>N. dist (s n) (s N) < e/2"

  3335         by (erule_tac x="e/2" in allE) auto

  3336       { fix n m

  3337         assume nm:"N \<le> m \<and> N \<le> n"

  3338         hence "dist (s m) (s n) < e" using N

  3339           using dist_triangle_half_l[of "s m" "s N" "e" "s n"]

  3340           by blast

  3341       }

  3342       hence "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (s m) (s n) < e"

  3343         by blast

  3344     }

  3345     hence ?lhs

  3346       unfolding cauchy_def

  3347       by blast

  3348   }

  3349   thus ?thesis

  3350     unfolding cauchy_def

  3351     using dist_triangle_half_l

  3352     by blast

  3353 qed

  3354

  3355 lemma cauchy_imp_bounded: assumes "Cauchy s" shows "bounded (range s)"

  3356 proof-

  3357   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

  3358   hence N:"\<forall>n. N \<le> n \<longrightarrow> dist (s N) (s n) < 1" by auto

  3359   moreover

  3360   have "bounded (s  {0..N})" using finite_imp_bounded[of "s  {1..N}"] by auto

  3361   then obtain a where a:"\<forall>x\<in>s  {0..N}. dist (s N) x \<le> a"

  3362     unfolding bounded_any_center [where a="s N"] by auto

  3363   ultimately show "?thesis"

  3364     unfolding bounded_any_center [where a="s N"]

  3365     apply(rule_tac x="max a 1" in exI) apply auto

  3366     apply(erule_tac x=y in allE) apply(erule_tac x=y in ballE) by auto

  3367 qed

  3368

  3369 lemma seq_compact_imp_complete: assumes "seq_compact s" shows "complete s"

  3370 proof-

  3371   { fix f assume as: "(\<forall>n::nat. f n \<in> s)" "Cauchy f"

  3372     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

  3373

  3374     note lr' = seq_suble [OF lr(2)]

  3375

  3376     { fix e::real assume "e>0"

  3377       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

  3378       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

  3379       { fix n::nat assume n:"n \<ge> max N M"

  3380         have "dist ((f \<circ> r) n) l < e/2" using n M by auto

  3381         moreover have "r n \<ge> N" using lr'[of n] n by auto

  3382         hence "dist (f n) ((f \<circ> r) n) < e / 2" using N using n by auto

  3383         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)  }

  3384       hence "\<exists>N. \<forall>n\<ge>N. dist (f n) l < e" by blast  }

  3385     hence "\<exists>l\<in>s. (f ---> l) sequentially" using l\<in>s unfolding LIMSEQ_def by auto  }

  3386   thus ?thesis unfolding complete_def by auto

  3387 qed

  3388

  3389 instance heine_borel < complete_space

  3390 proof

  3391   fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"

  3392   hence "bounded (range f)"

  3393     by (rule cauchy_imp_bounded)

  3394   hence "seq_compact (closure (range f))"

  3395     using bounded_closed_imp_seq_compact [of "closure (range f)"] by auto

  3396   hence "complete (closure (range f))"

  3397     by (rule seq_compact_imp_complete)

  3398   moreover have "\<forall>n. f n \<in> closure (range f)"

  3399     using closure_subset [of "range f"] by auto

  3400   ultimately have "\<exists>l\<in>closure (range f). (f ---> l) sequentially"

  3401     using Cauchy f unfolding complete_def by auto

  3402   then show "convergent f"

  3403     unfolding convergent_def by auto

  3404 qed

  3405

  3406 instance euclidean_space \<subseteq> banach ..

  3407

  3408 lemma complete_univ: "complete (UNIV :: 'a::complete_space set)"

  3409 proof(simp add: complete_def, rule, rule)

  3410   fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"

  3411   hence "convergent f" by (rule Cauchy_convergent)

  3412   thus "\<exists>l. f ----> l" unfolding convergent_def .

  3413 qed

  3414

  3415 lemma complete_imp_closed: assumes "complete s" shows "closed s"

  3416 proof -

  3417   { fix x assume "x islimpt s"

  3418     then obtain f where f: "\<forall>n. f n \<in> s - {x}" "(f ---> x) sequentially"

  3419       unfolding islimpt_sequential by auto

  3420     then obtain l where l: "l\<in>s" "(f ---> l) sequentially"

  3421       using complete s[unfolded complete_def] using LIMSEQ_imp_Cauchy[of f x] by auto

  3422     hence "x \<in> s"  using tendsto_unique[of sequentially f l x] trivial_limit_sequentially f(2) by auto

  3423   }

  3424   thus "closed s" unfolding closed_limpt by auto

  3425 qed

  3426

  3427 lemma complete_eq_closed:

  3428   fixes s :: "'a::complete_space set"

  3429   shows "complete s \<longleftrightarrow> closed s" (is "?lhs = ?rhs")

  3430 proof

  3431   assume ?lhs thus ?rhs by (rule complete_imp_closed)

  3432 next

  3433   assume ?rhs

  3434   { fix f assume as:"\<forall>n::nat. f n \<in> s" "Cauchy f"

  3435     then obtain l where "(f ---> l) sequentially" using complete_univ[unfolded complete_def, THEN spec[where x=f]] by auto

  3436     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  }

  3437   thus ?lhs unfolding complete_def by auto

  3438 qed

  3439

  3440 lemma convergent_eq_cauchy:

  3441   fixes s :: "nat \<Rightarrow> 'a::complete_space"

  3442   shows "(\<exists>l. (s ---> l) sequentially) \<longleftrightarrow> Cauchy s"

  3443   unfolding Cauchy_convergent_iff convergent_def ..

  3444

  3445 lemma convergent_imp_bounded:

  3446   fixes s :: "nat \<Rightarrow> 'a::metric_space"

  3447   shows "(s ---> l) sequentially \<Longrightarrow> bounded (range s)"

  3448   by (intro cauchy_imp_bounded LIMSEQ_imp_Cauchy)

  3449

  3450 subsubsection {* Complete the chain of compactness variants *}

  3451

  3452 primrec helper_2::"(real \<Rightarrow> 'a::metric_space) \<Rightarrow> nat \<Rightarrow> 'a" where

  3453   "helper_2 beyond 0 = beyond 0" |

  3454   "helper_2 beyond (Suc n) = beyond (dist undefined (helper_2 beyond n) + 1 )"

  3455

  3456 lemma bolzano_weierstrass_imp_bounded: fixes s::"'a::metric_space set"

  3457   assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"

  3458   shows "bounded s"

  3459 proof(rule ccontr)

  3460   assume "\<not> bounded s"

  3461   then obtain beyond where "\<forall>a. beyond a \<in>s \<and> \<not> dist undefined (beyond a) \<le> a"

  3462     unfolding bounded_any_center [where a=undefined]

  3463     apply simp using choice[of "\<lambda>a x. x\<in>s \<and> \<not> dist undefined x \<le> a"] by auto

  3464   hence beyond:"\<And>a. beyond a \<in>s" "\<And>a. dist undefined (beyond a) > a"

  3465     unfolding linorder_not_le by auto

  3466   def x \<equiv> "helper_2 beyond"

  3467

  3468   { fix m n ::nat assume "m<n"

  3469     hence "dist undefined (x m) + 1 < dist undefined (x n)"

  3470     proof(induct n)

  3471       case 0 thus ?case by auto

  3472     next

  3473       case (Suc n)

  3474       have *:"dist undefined (x n) + 1 < dist undefined (x (Suc n))"

  3475         unfolding x_def and helper_2.simps

  3476         using beyond(2)[of "dist undefined (helper_2 beyond n) + 1"] by auto

  3477       thus ?case proof(cases "m < n")

  3478         case True thus ?thesis using Suc and * by auto

  3479       next

  3480         case False hence "m = n" using Suc(2) by auto

  3481         thus ?thesis using * by auto

  3482       qed

  3483     qed  } note * = this

  3484   { fix m n ::nat assume "m\<noteq>n"

  3485     have "1 < dist (x m) (x n)"

  3486     proof(cases "m<n")

  3487       case True

  3488       hence "1 < dist undefined (x n) - dist undefined (x m)" using *[of m n] by auto

  3489       thus ?thesis using dist_triangle [of undefined "x n" "x m"] by arith

  3490     next

  3491       case False hence "n<m" using m\<noteq>n by auto

  3492       hence "1 < dist undefined (x m) - dist undefined (x n)" using *[of n m] by auto

  3493       thus ?thesis using dist_triangle2 [of undefined "x m" "x n"] by arith

  3494     qed  } note ** = this

  3495   { fix a b assume "x a = x b" "a \<noteq> b"

  3496     hence False using **[of a b] by auto  }

  3497   hence "inj x" unfolding inj_on_def by auto

  3498   moreover

  3499   { fix n::nat

  3500     have "x n \<in> s"

  3501     proof(cases "n = 0")

  3502       case True thus ?thesis unfolding x_def using beyond by auto

  3503     next

  3504       case False then obtain z where "n = Suc z" using not0_implies_Suc by auto

  3505       thus ?thesis unfolding x_def using beyond by auto

  3506     qed  }

  3507   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

  3508

  3509   then obtain l where "l\<in>s" and l:"l islimpt range x" using assms[THEN spec[where x="range x"]] by auto

  3510   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

  3511   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"]]

  3512     unfolding dist_nz by auto

  3513   show False using y and z and dist_triangle_half_l[of "x y" l 1 "x z"] and **[of y z] by auto

  3514 qed

  3515

  3516 text {* Hence express everything as an equivalence. *}

  3517

  3518 lemma compact_eq_bolzano_weierstrass:

  3519   fixes s :: "'a::metric_space set"

  3520   shows "compact s \<longleftrightarrow> (\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t))" (is "?lhs = ?rhs")

  3521 proof

  3522   assume ?lhs thus ?rhs using heine_borel_imp_bolzano_weierstrass[of s] by auto

  3523 next

  3524   assume ?rhs thus ?lhs

  3525     unfolding compact_eq_seq_compact_metric by (rule bolzano_weierstrass_imp_seq_compact)

  3526 qed

  3527

  3528 lemma nat_approx_posE:

  3529   fixes e::real

  3530   assumes "0 < e"

  3531   obtains n::nat where "1 / (Suc n) < e"

  3532 proof atomize_elim

  3533   have " 1 / real (Suc (nat (ceiling (1/e)))) < 1 / (ceiling (1/e))"

  3534     by (rule divide_strict_left_mono) (auto intro!: mult_pos_pos simp: 0 < e)

  3535   also have "1 / (ceiling (1/e)) \<le> 1 / (1/e)"

  3536     by (rule divide_left_mono) (auto intro!: divide_pos_pos simp: 0 < e)

  3537   also have "\<dots> = e" by simp

  3538   finally show  "\<exists>n. 1 / real (Suc n) < e" ..

  3539 qed

  3540

  3541 lemma compact_eq_totally_bounded:

  3542   "compact s \<longleftrightarrow> complete s \<and> (\<forall>e>0. \<exists>k. finite k \<and> s \<subseteq> (\<Union>((\<lambda>x. ball x e)  k)))"

  3543 proof (safe intro!: seq_compact_imp_complete[unfolded  compact_eq_seq_compact_metric[symmetric]])

  3544   fix e::real

  3545   def f \<equiv> "(\<lambda>x::'a. ball x e)  UNIV"

  3546   assume "0 < e" "compact s"

  3547   hence "(\<forall>t\<in>f. open t) \<and> s \<subseteq> \<Union>f \<longrightarrow> (\<exists>f'\<subseteq>f. finite f' \<and> s \<subseteq> \<Union>f')"

  3548     by (simp add: compact_eq_heine_borel)

  3549   moreover

  3550   have d0: "\<And>x::'a. dist x x < e" using 0 < e by simp

  3551   hence "(\<forall>t\<in>f. open t) \<and> s \<subseteq> \<Union>f" by (auto simp: f_def intro!: d0)

  3552   ultimately have "(\<exists>f'\<subseteq>f. finite f' \<and> s \<subseteq> \<Union>f')" ..

  3553   then guess K .. note K = this

  3554   have "\<forall>K'\<in>K. \<exists>k. K' = ball k e" using K by (auto simp: f_def)

  3555   then obtain k where "\<And>K'. K' \<in> K \<Longrightarrow> K' = ball (k K') e" unfolding bchoice_iff by blast

  3556   thus "\<exists>k. finite k \<and> s \<subseteq> \<Union>(\<lambda>x. ball x e)  k" using K

  3557     by (intro exI[where x="k  K"]) (auto simp: f_def)

  3558 next

  3559   assume assms: "complete s" "\<forall>e>0. \<exists>k. finite k \<and> s \<subseteq> \<Union>(\<lambda>x. ball x e)  k"

  3560   show "compact s"

  3561   proof cases

  3562     assume "s = {}" thus "compact s" by (simp add: compact_def)

  3563   next

  3564     assume "s \<noteq> {}"

  3565     show ?thesis

  3566       unfolding compact_def

  3567     proof safe

  3568       fix f::"nat \<Rightarrow> _" assume "\<forall>n. f n \<in> s" hence f: "\<And>n. f n \<in> s" by simp

  3569       from assms have "\<forall>e. \<exists>k. e>0 \<longrightarrow> finite k \<and> s \<subseteq> (\<Union>((\<lambda>x. ball x e)  k))" by simp

  3570       then obtain K where

  3571         K: "\<And>e. e > 0 \<Longrightarrow> finite (K e) \<and> s \<subseteq> (\<Union>((\<lambda>x. ball x e)  (K e)))"

  3572         unfolding choice_iff by blast

  3573       {

  3574         fix e::real and f' have f': "\<And>n::nat. (f o f') n \<in> s" using f by auto

  3575         assume "e > 0"

  3576         from K[OF this] have K: "finite (K e)" "s \<subseteq> (\<Union>((\<lambda>x. ball x e)  (K e)))"

  3577           by simp_all

  3578         have "\<exists>k\<in>(K e). \<exists>r. subseq r \<and> (\<forall>i. (f o f' o r) i \<in> ball k e)"

  3579         proof (rule ccontr)

  3580           from K have "finite (K e)" "K e \<noteq> {}" "s \<subseteq> (\<Union>((\<lambda>x. ball x e)  (K e)))"

  3581             using s \<noteq> {}

  3582             by auto

  3583           moreover

  3584           assume "\<not> (\<exists>k\<in>K e. \<exists>r. subseq r \<and> (\<forall>i. (f \<circ> f' o r) i \<in> ball k e))"

  3585           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

  3586           ultimately

  3587           show False using f'

  3588           proof (induct arbitrary: s f f' rule: finite_ne_induct)

  3589             case (singleton x)

  3590             have "\<exists>i. (f \<circ> f' o id) i \<notin> ball x e" by (rule singleton) (auto simp: subseq_def)

  3591             thus ?case using singleton by (auto simp: ball_def)

  3592           next

  3593             case (insert x A)

  3594             show ?case

  3595             proof cases

  3596               have inf_ms: "infinite ((f o f') - s)" using insert by (simp add: vimage_def)

  3597               have "infinite ((f o f') - \<Union>((\<lambda>x. ball x e)  (insert x A)))"

  3598                 using insert by (intro infinite_super[OF _ inf_ms]) auto

  3599               also have "((f o f') - \<Union>((\<lambda>x. ball x e)  (insert x A))) =

  3600                 {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

  3601               finally have "infinite \<dots>" .

  3602               moreover assume "finite {m. (f o f') m \<in> ball x e}"

  3603               ultimately have inf: "infinite {m. (f o f') m \<in> \<Union>((\<lambda>x. ball x e)  A)}" by blast

  3604               hence "A \<noteq> {}" by auto then obtain k where "k \<in> A" by auto

  3605               def r \<equiv> "enumerate {m. (f o f') m \<in> \<Union>((\<lambda>x. ball x e)  A)}"

  3606               have r_mono: "\<And>n m. n < m \<Longrightarrow> r n < r m"

  3607                 using enumerate_mono[OF _ inf] by (simp add: r_def)

  3608               hence "subseq r" by (simp add: subseq_def)

  3609               have r_in_set: "\<And>n. r n \<in> {m. (f o f') m \<in> \<Union>((\<lambda>x. ball x e)  A)}"

  3610                 using enumerate_in_set[OF inf] by (simp add: r_def)

  3611               show False

  3612               proof (rule insert)

  3613                 show "\<Union>(\<lambda>x. ball x e)  A \<subseteq> \<Union>(\<lambda>x. ball x e)  A" by simp

  3614                 fix k s assume "k \<in> A" "subseq s"

  3615                 thus "\<exists>i. (f o f' o r o s) i \<notin> ball k e" using subseq r

  3616                   by (subst (2) o_assoc[symmetric]) (intro insert(6) subseq_o, simp_all)

  3617               next

  3618                 fix n show "(f \<circ> f' o r) n \<in> \<Union>(\<lambda>x. ball x e)  A" using r_in_set by auto

  3619               qed

  3620             next

  3621               assume inf: "infinite {m. (f o f') m \<in> ball x e}"

  3622               def r \<equiv> "enumerate {m. (f o f') m \<in> ball x e}"

  3623               have r_mono: "\<And>n m. n < m \<Longrightarrow> r n < r m"

  3624                 using enumerate_mono[OF _ inf] by (simp add: r_def)

  3625               hence "subseq r" by (simp add: subseq_def)

  3626               from insert(6)[OF insertI1 this] obtain i where "(f o f') (r i) \<notin> ball x e" by auto

  3627               moreover

  3628               have r_in_set: "\<And>n. r n \<in> {m. (f o f') m \<in> ball x e}"

  3629                 using enumerate_in_set[OF inf] by (simp add: r_def)

  3630               hence "(f o f') (r i) \<in> ball x e" by simp

  3631               ultimately show False by simp

  3632             qed

  3633           qed

  3634         qed

  3635       }

  3636       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

  3637       let ?e = "\<lambda>n. 1 / real (Suc n)"

  3638       let ?P = "\<lambda>n s. \<exists>k\<in>K (?e n). (\<forall>i. (f o s) i \<in> ball k (?e n))"

  3639       interpret subseqs ?P using ex by unfold_locales force

  3640       from complete s have limI: "\<And>f. (\<And>n. f n \<in> s) \<Longrightarrow> Cauchy f \<Longrightarrow> (\<exists>l\<in>s. f ----> l)"

  3641         by (simp add: complete_def)

  3642       have "\<exists>l\<in>s. (f o diagseq) ----> l"

  3643       proof (intro limI metric_CauchyI)

  3644         fix e::real assume "0 < e" hence "0 < e / 2" by auto

  3645         from nat_approx_posE[OF this] guess n . note n = this

  3646         show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist ((f \<circ> diagseq) m) ((f \<circ> diagseq) n) < e"

  3647         proof (rule exI[where x="Suc n"], safe)

  3648           fix m mm assume "Suc n \<le> m" "Suc n \<le> mm"

  3649           let ?e = "1 / real (Suc n)"

  3650           from reducer_reduces[of n] obtain k where

  3651             "k\<in>K ?e"  "\<And>i. (f o seqseq (Suc n)) i \<in> ball k ?e"

  3652             unfolding seqseq_reducer by auto

  3653           moreover

  3654           note diagseq_sub[OF Suc n \<le> m] diagseq_sub[OF Suc n \<le> mm]

  3655           ultimately have "{(f o diagseq) m, (f o diagseq) mm} \<subseteq> ball k ?e" by auto

  3656           also have "\<dots> \<subseteq> ball k (e / 2)" using n by (intro subset_ball) simp

  3657           finally

  3658           have "dist k ((f \<circ> diagseq) m) + dist k ((f \<circ> diagseq) mm) < e / 2 + e /2"

  3659             by (intro add_strict_mono) auto

  3660           hence "dist ((f \<circ> diagseq) m) k + dist ((f \<circ> diagseq) mm) k < e"

  3661             by (simp add: dist_commute)

  3662           moreover have "dist ((f \<circ> diagseq) m) ((f \<circ> diagseq) mm) \<le>

  3663             dist ((f \<circ> diagseq) m) k + dist ((f \<circ> diagseq) mm) k"

  3664             by (rule dist_triangle2)

  3665           ultimately show "dist ((f \<circ> diagseq) m) ((f \<circ> diagseq) mm) < e"

  3666             by simp

  3667         qed

  3668       next

  3669         fix n show "(f o diagseq) n \<in> s" using f by simp

  3670       qed

  3671       thus "\<exists>l\<in>s. \<exists>r. subseq r \<and> (f \<circ> r) ----> l" using subseq_diagseq by auto

  3672     qed

  3673   qed

  3674 qed

  3675

  3676 lemma compact_eq_bounded_closed:

  3677   fixes s :: "'a::heine_borel set"

  3678   shows "compact s \<longleftrightarrow> bounded s \<and> closed s"  (is "?lhs = ?rhs")

  3679 proof

  3680   assume ?lhs thus ?rhs

  3681     unfolding compact_eq_bolzano_weierstrass using bolzano_weierstrass_imp_bounded bolzano_weierstrass_imp_closed by auto

  3682 next

  3683   assume ?rhs thus ?lhs

  3684     using bounded_closed_imp_seq_compact[of s] unfolding compact_eq_seq_compact_metric by auto

  3685 qed

  3686

  3687 lemma compact_imp_bounded:

  3688   fixes s :: "'a::metric_space set"

  3689   shows "compact s \<Longrightarrow> bounded s"

  3690 proof -

  3691   assume "compact s"

  3692   hence "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t)"

  3693     using heine_borel_imp_bolzano_weierstrass[of s] by auto

  3694   thus "bounded s"

  3695     by (rule bolzano_weierstrass_imp_bounded)

  3696 qed

  3697

  3698 lemma compact_cball[simp]:

  3699   fixes x :: "'a::heine_borel"

  3700   shows "compact(cball x e)"

  3701   using compact_eq_bounded_closed bounded_cball closed_cball

  3702   by blast

  3703

  3704 lemma compact_frontier_bounded[intro]:

  3705   fixes s :: "'a::heine_borel set"

  3706   shows "bounded s ==> compact(frontier s)"

  3707   unfolding frontier_def

  3708   using compact_eq_bounded_closed

  3709   by blast

  3710

  3711 lemma compact_frontier[intro]:

  3712   fixes s :: "'a::heine_borel set"

  3713   shows "compact s ==> compact (frontier s)"

  3714   using compact_eq_bounded_closed compact_frontier_bounded

  3715   by blast

  3716

  3717 lemma frontier_subset_compact:

  3718   fixes s :: "'a::heine_borel set"

  3719   shows "compact s ==> frontier s \<subseteq> s"

  3720   using frontier_subset_closed compact_eq_bounded_closed

  3721   by blast

  3722

  3723 subsection {* Bounded closed nest property (proof does not use Heine-Borel) *}

  3724

  3725 lemma bounded_closed_nest:

  3726   assumes "\<forall>n. closed(s n)" "\<forall>n. (s n \<noteq> {})"

  3727   "(\<forall>m n. m \<le> n --> s n \<subseteq> s m)"  "bounded(s 0)"

  3728   shows "\<exists>a::'a::heine_borel. \<forall>n::nat. a \<in> s(n)"

  3729 proof-

  3730   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

  3731   from assms(4,1) have *:"seq_compact (s 0)" using bounded_closed_imp_seq_compact[of "s 0"] by auto

  3732

  3733   then obtain l r where lr:"l\<in>s 0" "subseq r" "((x \<circ> r) ---> l) sequentially"

  3734     unfolding seq_compact_def apply(erule_tac x=x in allE)  using x using assms(3) by blast

  3735

  3736   { fix n::nat

  3737     { fix e::real assume "e>0"

  3738       with lr(3) obtain N where N:"\<forall>m\<ge>N. dist ((x \<circ> r) m) l < e" unfolding LIMSEQ_def by auto

  3739       hence "dist ((x \<circ> r) (max N n)) l < e" by auto

  3740       moreover

  3741       have "r (max N n) \<ge> n" using lr(2) using seq_suble[of r "max N n"] by auto

  3742       hence "(x \<circ> r) (max N n) \<in> s n"

  3743         using x apply(erule_tac x=n in allE)

  3744         using x apply(erule_tac x="r (max N n)" in allE)

  3745         using assms(3) apply(erule_tac x=n in allE) apply(erule_tac x="r (max N n)" in allE) by auto

  3746       ultimately have "\<exists>y\<in>s n. dist y l < e" by auto

  3747     }

  3748     hence "l \<in> s n" using closed_approachable[of "s n" l] assms(1) by blast

  3749   }

  3750   thus ?thesis by auto

  3751 qed

  3752

  3753 text {* Decreasing case does not even need compactness, just completeness. *}

  3754

  3755 lemma decreasing_closed_nest:

  3756   assumes "\<forall>n. closed(s n)"

  3757           "\<forall>n. (s n \<noteq> {})"

  3758           "\<forall>m n. m \<le> n --> s n \<subseteq> s m"

  3759           "\<forall>e>0. \<exists>n. \<forall>x \<in> (s n). \<forall> y \<in> (s n). dist x y < e"

  3760   shows "\<exists>a::'a::complete_space. \<forall>n::nat. a \<in> s n"

  3761 proof-

  3762   have "\<forall>n. \<exists> x. x\<in>s n" using assms(2) by auto

  3763   hence "\<exists>t. \<forall>n. t n \<in> s n" using choice[of "\<lambda> n x. x \<in> s n"] by auto

  3764   then obtain t where t: "\<forall>n. t n \<in> s n" by auto

  3765   { fix e::real assume "e>0"

  3766     then obtain N where N:"\<forall>x\<in>s N. \<forall>y\<in>s N. dist x y < e" using assms(4) by auto

  3767     { fix m n ::nat assume "N \<le> m \<and> N \<le> n"

  3768       hence "t m \<in> s N" "t n \<in> s N" using assms(3) t unfolding  subset_eq t by blast+

  3769       hence "dist (t m) (t n) < e" using N by auto

  3770     }

  3771     hence "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (t m) (t n) < e" by auto

  3772   }

  3773   hence  "Cauchy t" unfolding cauchy_def by auto

  3774   then obtain l where l:"(t ---> l) sequentially" using complete_univ unfolding complete_def by auto

  3775   { fix n::nat

  3776     { fix e::real assume "e>0"

  3777       then obtain N::nat where N:"\<forall>n\<ge>N. dist (t n) l < e" using l[unfolded LIMSEQ_def] by auto

  3778       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

  3779       hence "\<exists>y\<in>s n. dist y l < e" apply(rule_tac x="t (max n N)" in bexI) using N by auto

  3780     }

  3781     hence "l \<in> s n" using closed_approachable[of "s n" l] assms(1) by auto

  3782   }

  3783   then show ?thesis by auto

  3784 qed

  3785

  3786 text {* Strengthen it to the intersection actually being a singleton. *}

  3787

  3788 lemma decreasing_closed_nest_sing:

  3789   fixes s :: "nat \<Rightarrow> 'a::complete_space set"

  3790   assumes "\<forall>n. closed(s n)"

  3791           "\<forall>n. s n \<noteq> {}"

  3792           "\<forall>m n. m \<le> n --> s n \<subseteq> s m"

  3793           "\<forall>e>0. \<exists>n. \<forall>x \<in> (s n). \<forall> y\<in>(s n). dist x y < e"

  3794   shows "\<exists>a. \<Inter>(range s) = {a}"

  3795 proof-

  3796   obtain a where a:"\<forall>n. a \<in> s n" using decreasing_closed_nest[of s] using assms by auto

  3797   { fix b assume b:"b \<in> \<Inter>(range s)"

  3798     { fix e::real assume "e>0"

  3799       hence "dist a b < e" using assms(4 )using b using a by blast

  3800     }

  3801     hence "dist a b = 0" by (metis dist_eq_0_iff dist_nz less_le)

  3802   }

  3803   with a have "\<Inter>(range s) = {a}" unfolding image_def by auto

  3804   thus ?thesis ..

  3805 qed

  3806

  3807 text{* Cauchy-type criteria for uniform convergence. *}

  3808

  3809 lemma uniformly_convergent_eq_cauchy: fixes s::"nat \<Rightarrow> 'b \<Rightarrow> 'a::heine_borel" shows

  3810  "(\<exists>l. \<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x --> dist(s n x)(l x) < e) \<longleftrightarrow>

  3811   (\<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")

  3812 proof(rule)

  3813   assume ?lhs

  3814   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

  3815   { fix e::real assume "e>0"

  3816     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

  3817     { fix n m::nat and x::"'b" assume "N \<le> m \<and> N \<le> n \<and> P x"

  3818       hence "dist (s m x) (s n x) < e"

  3819         using N[THEN spec[where x=m], THEN spec[where x=x]]

  3820         using N[THEN spec[where x=n], THEN spec[where x=x]]

  3821         using dist_triangle_half_l[of "s m x" "l x" e "s n x"] by auto  }

  3822     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  }

  3823   thus ?rhs by auto

  3824 next

  3825   assume ?rhs

  3826   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

  3827   then obtain l where l:"\<forall>x. P x \<longrightarrow> ((\<lambda>n. s n x) ---> l x) sequentially" unfolding convergent_eq_cauchy[THEN sym]

  3828     using choice[of "\<lambda>x l. P x \<longrightarrow> ((\<lambda>n. s n x) ---> l) sequentially"] by auto

  3829   { fix e::real assume "e>0"

  3830     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"

  3831       using ?rhs[THEN spec[where x="e/2"]] by auto

  3832     { fix x assume "P x"

  3833       then obtain M where M:"\<forall>n\<ge>M. dist (s n x) (l x) < e/2"

  3834         using l[THEN spec[where x=x], unfolded LIMSEQ_def] using e>0 by(auto elim!: allE[where x="e/2"])

  3835       fix n::nat assume "n\<ge>N"

  3836       hence "dist(s n x)(l x) < e"  using P xand N[THEN spec[where x=n], THEN spec[where x="N+M"], THEN spec[where x=x]]

  3837         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)  }

  3838     hence "\<exists>N. \<forall>n x. N \<le> n \<and> P x \<longrightarrow> dist(s n x)(l x) < e" by auto }

  3839   thus ?lhs by auto

  3840 qed

  3841

  3842 lemma uniformly_cauchy_imp_uniformly_convergent:

  3843   fixes s :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::heine_borel"

  3844   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"

  3845           "\<forall>x. P x --> (\<forall>e>0. \<exists>N. \<forall>n. N \<le> n --> dist(s n x)(l x) < e)"

  3846   shows "\<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x --> dist(s n x)(l x) < e"

  3847 proof-

  3848   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"

  3849     using assms(1) unfolding uniformly_convergent_eq_cauchy[THEN sym] by auto

  3850   moreover

  3851   { fix x assume "P x"

  3852     hence "l x = l' x" using tendsto_unique[OF trivial_limit_sequentially, of "\<lambda>n. s n x" "l x" "l' x"]

  3853       using l and assms(2) unfolding LIMSEQ_def by blast  }

  3854   ultimately show ?thesis by auto

  3855 qed

  3856

  3857

  3858 subsection {* Continuity *}

  3859

  3860 text {* Define continuity over a net to take in restrictions of the set. *}

  3861

  3862 definition

  3863   continuous :: "'a::t2_space filter \<Rightarrow> ('a \<Rightarrow> 'b::topological_space) \<Rightarrow> bool"

  3864   where "continuous net f \<longleftrightarrow> (f ---> f(netlimit net)) net"

  3865

  3866 lemma continuous_trivial_limit:

  3867  "trivial_limit net ==> continuous net f"

  3868   unfolding continuous_def tendsto_def trivial_limit_eq by auto

  3869

  3870 lemma continuous_within: "continuous (at x within s) f \<longleftrightarrow> (f ---> f(x)) (at x within s)"

  3871   unfolding continuous_def

  3872   unfolding tendsto_def

  3873   using netlimit_within[of x s]

  3874   by (cases "trivial_limit (at x within s)") (auto simp add: trivial_limit_eventually)

  3875

  3876 lemma continuous_at: "continuous (at x) f \<longleftrightarrow> (f ---> f(x)) (at x)"

  3877   using continuous_within [of x UNIV f] by simp

  3878

  3879 lemma continuous_at_within:

  3880   assumes "continuous (at x) f"  shows "continuous (at x within s) f"

  3881   using assms unfolding continuous_at continuous_within

  3882   by (rule Lim_at_within)

  3883

  3884 text{* Derive the epsilon-delta forms, which we often use as "definitions" *}

  3885

  3886 lemma continuous_within_eps_delta:

  3887   "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)"

  3888   unfolding continuous_within and Lim_within

  3889   apply auto unfolding dist_nz[THEN sym] apply(auto del: allE elim!:allE) apply(rule_tac x=d in exI) by auto

  3890

  3891 lemma continuous_at_eps_delta: "continuous (at x) f \<longleftrightarrow>  (\<forall>e>0. \<exists>d>0.

  3892                            \<forall>x'. dist x' x < d --> dist(f x')(f x) < e)"

  3893   using continuous_within_eps_delta [of x UNIV f] by simp

  3894

  3895 text{* Versions in terms of open balls. *}

  3896

  3897 lemma continuous_within_ball:

  3898  "continuous (at x within s) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0.

  3899                             f  (ball x d \<inter> s) \<subseteq> ball (f x) e)" (is "?lhs = ?rhs")

  3900 proof

  3901   assume ?lhs

  3902   { fix e::real assume "e>0"

  3903     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"

  3904       using ?lhs[unfolded continuous_within Lim_within] by auto

  3905     { fix y assume "y\<in>f  (ball x d \<inter> s)"

  3906       hence "y \<in> ball (f x) e" using d(2) unfolding dist_nz[THEN sym]

  3907         apply (auto simp add: dist_commute) apply(erule_tac x=xa in ballE) apply auto using e>0 by auto

  3908     }

  3909     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)  }

  3910   thus ?rhs by auto

  3911 next

  3912   assume ?rhs thus ?lhs unfolding continuous_within Lim_within ball_def subset_eq

  3913     apply (auto simp add: dist_commute) apply(erule_tac x=e in allE) by auto

  3914 qed

  3915

  3916 lemma continuous_at_ball:

  3917   "continuous (at x) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0. f  (ball x d) \<subseteq> ball (f x) e)" (is "?lhs = ?rhs")

  3918 proof

  3919   assume ?lhs thus ?rhs unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball

  3920     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)

  3921     unfolding dist_nz[THEN sym] by auto

  3922 next

  3923   assume ?rhs thus ?lhs unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball

  3924     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)

  3925 qed

  3926

  3927 text{* Define setwise continuity in terms of limits within the set. *}

  3928

  3929 definition

  3930   continuous_on ::

  3931     "'a set \<Rightarrow> ('a::topological_space \<Rightarrow> 'b::topological_space) \<Rightarrow> bool"

  3932 where

  3933   "continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. (f ---> f x) (at x within s))"

  3934

  3935 lemma continuous_on_topological:

  3936   "continuous_on s f \<longleftrightarrow>

  3937     (\<forall>x\<in>s. \<forall>B. open B \<longrightarrow> f x \<in> B \<longrightarrow>

  3938       (\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)))"

  3939 unfolding continuous_on_def tendsto_def

  3940 unfolding Limits.eventually_within eventually_at_topological

  3941 by (intro ball_cong [OF refl] all_cong imp_cong ex_cong conj_cong refl) auto

  3942

  3943 lemma continuous_on_iff:

  3944   "continuous_on s f \<longleftrightarrow>

  3945     (\<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)"

  3946 unfolding continuous_on_def Lim_within

  3947 apply (intro ball_cong [OF refl] all_cong ex_cong)

  3948 apply (rename_tac y, case_tac "y = x", simp)

  3949 apply (simp add: dist_nz)

  3950 done

  3951

  3952 definition

  3953   uniformly_continuous_on ::

  3954     "'a set \<Rightarrow> ('a::metric_space \<Rightarrow> 'b::metric_space) \<Rightarrow> bool"

  3955 where

  3956   "uniformly_continuous_on s f \<longleftrightarrow>

  3957     (\<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)"

  3958

  3959 text{* Some simple consequential lemmas. *}

  3960

  3961 lemma uniformly_continuous_imp_continuous:

  3962  " uniformly_continuous_on s f ==> continuous_on s f"

  3963   unfolding uniformly_continuous_on_def continuous_on_iff by blast

  3964

  3965 lemma continuous_at_imp_continuous_within:

  3966  "continuous (at x) f ==> continuous (at x within s) f"

  3967   unfolding continuous_within continuous_at using Lim_at_within by auto

  3968

  3969 lemma Lim_trivial_limit: "trivial_limit net \<Longrightarrow> (f ---> l) net"

  3970 unfolding tendsto_def by (simp add: trivial_limit_eq)

  3971

  3972 lemma continuous_at_imp_continuous_on:

  3973   assumes "\<forall>x\<in>s. continuous (at x) f"

  3974   shows "continuous_on s f"

  3975 unfolding continuous_on_def

  3976 proof

  3977   fix x assume "x \<in> s"

  3978   with assms have *: "(f ---> f (netlimit (at x))) (at x)"

  3979     unfolding continuous_def by simp

  3980   have "(f ---> f x) (at x)"

  3981   proof (cases "trivial_limit (at x)")

  3982     case True thus ?thesis

  3983       by (rule Lim_trivial_limit)

  3984   next

  3985     case False

  3986     hence 1: "netlimit (at x) = x"

  3987       using netlimit_within [of x UNIV] by simp

  3988     with * show ?thesis by simp

  3989   qed

  3990   thus "(f ---> f x) (at x within s)"

  3991     by (rule Lim_at_within)

  3992 qed

  3993

  3994 lemma continuous_on_eq_continuous_within:

  3995   "continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. continuous (at x within s) f)"

  3996 unfolding continuous_on_def continuous_def

  3997 apply (rule ball_cong [OF refl])

  3998 apply (case_tac "trivial_limit (at x within s)")

  3999 apply (simp add: Lim_trivial_limit)

  4000 apply (simp add: netlimit_within)

  4001 done

  4002

  4003 lemmas continuous_on = continuous_on_def -- "legacy theorem name"

  4004

  4005 lemma continuous_on_eq_continuous_at:

  4006   shows "open s ==> (continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. continuous (at x) f))"

  4007   by (auto simp add: continuous_on continuous_at Lim_within_open)

  4008

  4009 lemma continuous_within_subset:

  4010  "continuous (at x within s) f \<Longrightarrow> t \<subseteq> s

  4011              ==> continuous (at x within t) f"

  4012   unfolding continuous_within by(metis Lim_within_subset)

  4013

  4014 lemma continuous_on_subset:

  4015   shows "continuous_on s f \<Longrightarrow> t \<subseteq> s ==> continuous_on t f"

  4016   unfolding continuous_on by (metis subset_eq Lim_within_subset)

  4017

  4018 lemma continuous_on_interior:

  4019   shows "continuous_on s f \<Longrightarrow> x \<in> interior s \<Longrightarrow> continuous (at x) f"

  4020   by (erule interiorE, drule (1) continuous_on_subset,

  4021     simp add: continuous_on_eq_continuous_at)

  4022

  4023 lemma continuous_on_eq:

  4024   "(\<forall>x \<in> s. f x = g x) \<Longrightarrow> continuous_on s f \<Longrightarrow> continuous_on s g"

  4025   unfolding continuous_on_def tendsto_def Limits.eventually_within

  4026   by simp

  4027

  4028 text {* Characterization of various kinds of continuity in terms of sequences. *}

  4029

  4030 lemma continuous_within_sequentially:

  4031   fixes f :: "'a::metric_space \<Rightarrow> 'b::topological_space"

  4032   shows "continuous (at a within s) f \<longleftrightarrow>

  4033                 (\<forall>x. (\<forall>n::nat. x n \<in> s) \<and> (x ---> a) sequentially

  4034                      --> ((f o x) ---> f a) sequentially)" (is "?lhs = ?rhs")

  4035 proof

  4036   assume ?lhs

  4037   { 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"

  4038     fix T::"'b set" assume "open T" and "f a \<in> T"

  4039     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"

  4040       unfolding continuous_within tendsto_def eventually_within by auto

  4041     have "eventually (\<lambda>n. dist (x n) a < d) sequentially"

  4042       using x(2) d>0 by simp

  4043     hence "eventually (\<lambda>n. (f \<circ> x) n \<in> T) sequentially"

  4044     proof eventually_elim

  4045       case (elim n) thus ?case

  4046         using d x(1) f a \<in> T unfolding dist_nz[THEN sym] by auto

  4047     qed

  4048   }

  4049   thus ?rhs unfolding tendsto_iff unfolding tendsto_def by simp

  4050 next

  4051   assume ?rhs thus ?lhs

  4052     unfolding continuous_within tendsto_def [where l="f a"]

  4053     by (simp add: sequentially_imp_eventually_within)

  4054 qed

  4055

  4056 lemma continuous_at_sequentially:

  4057   fixes f :: "'a::metric_space \<Rightarrow> 'b::topological_space"

  4058   shows "continuous (at a) f \<longleftrightarrow> (\<forall>x. (x ---> a) sequentially

  4059                   --> ((f o x) ---> f a) sequentially)"

  4060   using continuous_within_sequentially[of a UNIV f] by simp

  4061

  4062 lemma continuous_on_sequentially:

  4063   fixes f :: "'a::metric_space \<Rightarrow> 'b::topological_space"

  4064   shows "continuous_on s f \<longleftrightarrow>

  4065     (\<forall>x. \<forall>a \<in> s. (\<forall>n. x(n) \<in> s) \<and> (x ---> a) sequentially

  4066                     --> ((f o x) ---> f(a)) sequentially)" (is "?lhs = ?rhs")

  4067 proof

  4068   assume ?rhs thus ?lhs using continuous_within_sequentially[of _ s f] unfolding continuous_on_eq_continuous_within by auto

  4069 next

  4070   assume ?lhs thus ?rhs unfolding continuous_on_eq_continuous_within using continuous_within_sequentially[of _ s f] by auto

  4071 qed

  4072

  4073 lemma uniformly_continuous_on_sequentially:

  4074   "uniformly_continuous_on s f \<longleftrightarrow> (\<forall>x y. (\<forall>n. x n \<in> s) \<and> (\<forall>n. y n \<in> s) \<and>

  4075                     ((\<lambda>n. dist (x n) (y n)) ---> 0) sequentially

  4076                     \<longrightarrow> ((\<lambda>n. dist (f(x n)) (f(y n))) ---> 0) sequentially)" (is "?lhs = ?rhs")

  4077 proof

  4078   assume ?lhs

  4079   { 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"

  4080     { fix e::real assume "e>0"

  4081       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"

  4082         using ?lhs[unfolded uniformly_continuous_on_def, THEN spec[where x=e]] by auto

  4083       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

  4084       { fix n assume "n\<ge>N"

  4085         hence "dist (f (x n)) (f (y n)) < e"

  4086           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

  4087           unfolding dist_commute by simp  }

  4088       hence "\<exists>N. \<forall>n\<ge>N. dist (f (x n)) (f (y n)) < e"  by auto  }

  4089     hence "((\<lambda>n. dist (f(x n)) (f(y n))) ---> 0) sequentially" unfolding LIMSEQ_def and dist_real_def by auto  }

  4090   thus ?rhs by auto

  4091 next

  4092   assume ?rhs

  4093   { assume "\<not> ?lhs"

  4094     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

  4095     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"

  4096       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

  4097       by (auto simp add: dist_commute)

  4098     def x \<equiv> "\<lambda>n::nat. fst (fa (inverse (real n + 1)))"

  4099     def y \<equiv> "\<lambda>n::nat. snd (fa (inverse (real n + 1)))"

  4100     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"

  4101       unfolding x_def and y_def using fa by auto

  4102     { fix e::real assume "e>0"

  4103       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

  4104       { fix n::nat assume "n\<ge>N"

  4105         hence "inverse (real n + 1) < inverse (real N)" using real_of_nat_ge_zero and N\<noteq>0 by auto

  4106         also have "\<dots> < e" using N by auto

  4107         finally have "inverse (real n + 1) < e" by auto

  4108         hence "dist (x n) (y n) < e" using xy0[THEN spec[where x=n]] by auto  }

  4109       hence "\<exists>N. \<forall>n\<ge>N. dist (x n) (y n) < e" by auto  }

  4110     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

  4111     hence False using fxy and e>0 by auto  }

  4112   thus ?lhs unfolding uniformly_continuous_on_def by blast

  4113 qed

  4114

  4115 text{* The usual transformation theorems. *}

  4116

  4117 lemma continuous_transform_within:

  4118   fixes f g :: "'a::metric_space \<Rightarrow> 'b::topological_space"

  4119   assumes "0 < d" "x \<in> s" "\<forall>x' \<in> s. dist x' x < d --> f x' = g x'"

  4120           "continuous (at x within s) f"

  4121   shows "continuous (at x within s) g"

  4122 unfolding continuous_within

  4123 proof (rule Lim_transform_within)

  4124   show "0 < d" by fact

  4125   show "\<forall>x'\<in>s. 0 < dist x' x \<and> dist x' x < d \<longrightarrow> f x' = g x'"

  4126     using assms(3) by auto

  4127   have "f x = g x"

  4128     using assms(1,2,3) by auto

  4129   thus "(f ---> g x) (at x within s)"

  4130     using assms(4) unfolding continuous_within by simp

  4131 qed

  4132

  4133 lemma continuous_transform_at:

  4134   fixes f g :: "'a::metric_space \<Rightarrow> 'b::topological_space"

  4135   assumes "0 < d" "\<forall>x'. dist x' x < d --> f x' = g x'"

  4136           "continuous (at x) f"

  4137   shows "continuous (at x) g"

  4138   using continuous_transform_within [of d x UNIV f g] assms by simp

  4139

  4140 subsubsection {* Structural rules for pointwise continuity *}

  4141

  4142 lemma continuous_within_id: "continuous (at a within s) (\<lambda>x. x)"

  4143   unfolding continuous_within by (rule tendsto_ident_at_within)

  4144

  4145 lemma continuous_at_id: "continuous (at a) (\<lambda>x. x)"

  4146   unfolding continuous_at by (rule tendsto_ident_at)

  4147

  4148 lemma continuous_const: "continuous F (\<lambda>x. c)"

  4149   unfolding continuous_def by (rule tendsto_const)

  4150

  4151 lemma continuous_dist:

  4152   assumes "continuous F f" and "continuous F g"

  4153   shows "continuous F (\<lambda>x. dist (f x) (g x))"

  4154   using assms unfolding continuous_def by (rule tendsto_dist)

  4155

  4156 lemma continuous_infdist:

  4157   assumes "continuous F f"

  4158   shows "continuous F (\<lambda>x. infdist (f x) A)"

  4159   using assms unfolding continuous_def by (rule tendsto_infdist)

  4160

  4161 lemma continuous_norm:

  4162   shows "continuous F f \<Longrightarrow> continuous F (\<lambda>x. norm (f x))"

  4163   unfolding continuous_def by (rule tendsto_norm)

  4164

  4165 lemma continuous_infnorm:

  4166   shows "continuous F f \<Longrightarrow> continuous F (\<lambda>x. infnorm (f x))"

  4167   unfolding continuous_def by (rule tendsto_infnorm)

  4168

  4169 lemma continuous_add:

  4170   fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"

  4171   shows "\<lbrakk>continuous F f; continuous F g\<rbrakk> \<Longrightarrow> continuous F (\<lambda>x. f x + g x)"

  4172   unfolding continuous_def by (rule tendsto_add)

  4173

  4174 lemma continuous_minus:

  4175   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"

  4176   shows "continuous F f \<Longrightarrow> continuous F (\<lambda>x. - f x)"

  4177   unfolding continuous_def by (rule tendsto_minus)

  4178

  4179 lemma continuous_diff:

  4180   fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"

  4181   shows "\<lbrakk>continuous F f; continuous F g\<rbrakk> \<Longrightarrow> continuous F (\<lambda>x. f x - g x)"

  4182   unfolding continuous_def by (rule tendsto_diff)

  4183

  4184 lemma continuous_scaleR:

  4185   fixes g :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"

  4186   shows "\<lbrakk>continuous F f; continuous F g\<rbrakk> \<Longrightarrow> continuous F (\<lambda>x. f x *\<^sub>R g x)"

  4187   unfolding continuous_def by (rule tendsto_scaleR)

  4188

  4189 lemma continuous_mult:

  4190   fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_algebra"

  4191   shows "\<lbrakk>continuous F f; continuous F g\<rbrakk> \<Longrightarrow> continuous F (\<lambda>x. f x * g x)"

  4192   unfolding continuous_def by (rule tendsto_mult)

  4193

  4194 lemma continuous_inner:

  4195   assumes "continuous F f" and "continuous F g"

  4196   shows "continuous F (\<lambda>x. inner (f x) (g x))"

  4197   using assms unfolding continuous_def by (rule tendsto_inner)

  4198

  4199 lemma continuous_inverse:

  4200   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_div_algebra"

  4201   assumes "continuous F f" and "f (netlimit F) \<noteq> 0"

  4202   shows "continuous F (\<lambda>x. inverse (f x))"

  4203   using assms unfolding continuous_def by (rule tendsto_inverse)

  4204

  4205 lemma continuous_at_within_inverse:

  4206   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_div_algebra"

  4207   assumes "continuous (at a within s) f" and "f a \<noteq> 0"

  4208   shows "continuous (at a within s) (\<lambda>x. inverse (f x))"

  4209   using assms unfolding continuous_within by (rule tendsto_inverse)

  4210

  4211 lemma continuous_at_inverse:

  4212   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_div_algebra"

  4213   assumes "continuous (at a) f" and "f a \<noteq> 0"

  4214   shows "continuous (at a) (\<lambda>x. inverse (f x))"

  4215   using assms unfolding continuous_at by (rule tendsto_inverse)

  4216

  4217 lemmas continuous_intros = continuous_at_id continuous_within_id

  4218   continuous_const continuous_dist continuous_norm continuous_infnorm

  4219   continuous_add continuous_minus continuous_diff continuous_scaleR continuous_mult

  4220   continuous_inner continuous_at_inverse continuous_at_within_inverse

  4221

  4222 subsubsection {* Structural rules for setwise continuity *}

  4223

  4224 lemma continuous_on_id: "continuous_on s (\<lambda>x. x)"

  4225   unfolding continuous_on_def by (fast intro: tendsto_ident_at_within)

  4226

  4227 lemma continuous_on_const: "continuous_on s (\<lambda>x. c)"

  4228   unfolding continuous_on_def by (auto intro: tendsto_intros)

  4229

  4230 lemma continuous_on_norm:

  4231   shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. norm (f x))"

  4232   unfolding continuous_on_def by (fast intro: tendsto_norm)

  4233

  4234 lemma continuous_on_infnorm:

  4235   shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. infnorm (f x))"

  4236   unfolding continuous_on by (fast intro: tendsto_infnorm)

  4237

  4238 lemma continuous_on_minus:

  4239   fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"

  4240   shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. - f x)"

  4241   unfolding continuous_on_def by (auto intro: tendsto_intros)

  4242

  4243 lemma continuous_on_add:

  4244   fixes f g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"

  4245   shows "continuous_on s f \<Longrightarrow> continuous_on s g

  4246            \<Longrightarrow> continuous_on s (\<lambda>x. f x + g x)"

  4247   unfolding continuous_on_def by (auto intro: tendsto_intros)

  4248

  4249 lemma continuous_on_diff:

  4250   fixes f g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"

  4251   shows "continuous_on s f \<Longrightarrow> continuous_on s g

  4252            \<Longrightarrow> continuous_on s (\<lambda>x. f x - g x)"

  4253   unfolding continuous_on_def by (auto intro: tendsto_intros)

  4254

  4255 lemma (in bounded_linear) continuous_on:

  4256   "continuous_on s g \<Longrightarrow> continuous_on s (\<lambda>x. f (g x))"

  4257   unfolding continuous_on_def by (fast intro: tendsto)

  4258

  4259 lemma (in bounded_bilinear) continuous_on:

  4260   "\<lbrakk>continuous_on s f; continuous_on s g\<rbrakk> \<Longrightarrow> continuous_on s (\<lambda>x. f x ** g x)"

  4261   unfolding continuous_on_def by (fast intro: tendsto)

  4262

  4263 lemma continuous_on_scaleR:

  4264   fixes g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"

  4265   assumes "continuous_on s f" and "continuous_on s g"

  4266   shows "continuous_on s (\<lambda>x. f x *\<^sub>R g x)"

  4267   using bounded_bilinear_scaleR assms

  4268   by (rule bounded_bilinear.continuous_on)

  4269

  4270 lemma continuous_on_mult:

  4271   fixes g :: "'a::topological_space \<Rightarrow> 'b::real_normed_algebra"

  4272   assumes "continuous_on s f" and "continuous_on s g"

  4273   shows "continuous_on s (\<lambda>x. f x * g x)"

  4274   using bounded_bilinear_mult assms

  4275   by (rule bounded_bilinear.continuous_on)

  4276

  4277 lemma continuous_on_inner:

  4278   fixes g :: "'a::topological_space \<Rightarrow> 'b::real_inner"

  4279   assumes "continuous_on s f" and "continuous_on s g"

  4280   shows "continuous_on s (\<lambda>x. inner (f x) (g x))"

  4281   using bounded_bilinear_inner assms

  4282   by (rule bounded_bilinear.continuous_on)

  4283

  4284 lemma continuous_on_inverse:

  4285   fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_div_algebra"

  4286   assumes "continuous_on s f" and "\<forall>x\<in>s. f x \<noteq> 0"

  4287   shows "continuous_on s (\<lambda>x. inverse (f x))"

  4288   using assms unfolding continuous_on by (fast intro: tendsto_inverse)

  4289

  4290 subsubsection {* Structural rules for uniform continuity *}

  4291

  4292 lemma uniformly_continuous_on_id:

  4293   shows "uniformly_continuous_on s (\<lambda>x. x)"

  4294   unfolding uniformly_continuous_on_def by auto

  4295

  4296 lemma uniformly_continuous_on_const:

  4297   shows "uniformly_continuous_on s (\<lambda>x. c)"

  4298   unfolding uniformly_continuous_on_def by simp

  4299

  4300 lemma uniformly_continuous_on_dist:

  4301   fixes f g :: "'a::metric_space \<Rightarrow> 'b::metric_space"

  4302   assumes "uniformly_continuous_on s f"

  4303   assumes "uniformly_continuous_on s g"

  4304   shows "uniformly_continuous_on s (\<lambda>x. dist (f x) (g x))"

  4305 proof -

  4306   { fix a b c d :: 'b have "\<bar>dist a b - dist c d\<bar> \<le> dist a c + dist b d"

  4307       using dist_triangle2 [of a b c] dist_triangle2 [of b c d]

  4308       using dist_triangle3 [of c d a] dist_triangle [of a d b]

  4309       by arith

  4310   } note le = this

  4311   { fix x y

  4312     assume f: "(\<lambda>n. dist (f (x n)) (f (y n))) ----> 0"

  4313     assume g: "(\<lambda>n. dist (g (x n)) (g (y n))) ----> 0"

  4314     have "(\<lambda>n. \<bar>dist (f (x n)) (g (x n)) - dist (f (y n)) (g (y n))\<bar>) ----> 0"

  4315       by (rule Lim_transform_bound [OF _ tendsto_add_zero [OF f g]],

  4316         simp add: le)

  4317   }

  4318   thus ?thesis using assms unfolding uniformly_continuous_on_sequentially

  4319     unfolding dist_real_def by simp

  4320 qed

  4321

  4322 lemma uniformly_continuous_on_norm:

  4323   assumes "uniformly_continuous_on s f"

  4324   shows "uniformly_continuous_on s (\<lambda>x. norm (f x))"

  4325   unfolding norm_conv_dist using assms

  4326   by (intro uniformly_continuous_on_dist uniformly_continuous_on_const)

  4327

  4328 lemma (in bounded_linear) uniformly_continuous_on:

  4329   assumes "uniformly_continuous_on s g"

  4330   shows "uniformly_continuous_on s (\<lambda>x. f (g x))"

  4331   using assms unfolding uniformly_continuous_on_sequentially

  4332   unfolding dist_norm tendsto_norm_zero_iff diff[symmetric]

  4333   by (auto intro: tendsto_zero)

  4334

  4335 lemma uniformly_continuous_on_cmul:

  4336   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"

  4337   assumes "uniformly_continuous_on s f"

  4338   shows "uniformly_continuous_on s (\<lambda>x. c *\<^sub>R f(x))"

  4339   using bounded_linear_scaleR_right assms

  4340   by (rule bounded_linear.uniformly_continuous_on)

  4341

  4342 lemma dist_minus:

  4343   fixes x y :: "'a::real_normed_vector"

  4344   shows "dist (- x) (- y) = dist x y"

  4345   unfolding dist_norm minus_diff_minus norm_minus_cancel ..

  4346

  4347 lemma uniformly_continuous_on_minus:

  4348   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"

  4349   shows "uniformly_continuous_on s f \<Longrightarrow> uniformly_continuous_on s (\<lambda>x. - f x)"

  4350   unfolding uniformly_continuous_on_def dist_minus .

  4351

  4352 lemma uniformly_continuous_on_add:

  4353   fixes f g :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"

  4354   assumes "uniformly_continuous_on s f"

  4355   assumes "uniformly_continuous_on s g"

  4356   shows "uniformly_continuous_on s (\<lambda>x. f x + g x)"

  4357   using assms unfolding uniformly_continuous_on_sequentially

  4358   unfolding dist_norm tendsto_norm_zero_iff add_diff_add

  4359   by (auto intro: tendsto_add_zero)

  4360

  4361 lemma uniformly_continuous_on_diff:

  4362   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"

  4363   assumes "uniformly_continuous_on s f" and "uniformly_continuous_on s g"

  4364   shows "uniformly_continuous_on s (\<lambda>x. f x - g x)"

  4365   unfolding ab_diff_minus using assms

  4366   by (intro uniformly_continuous_on_add uniformly_continuous_on_minus)

  4367

  4368 text{* Continuity of all kinds is preserved under composition. *}

  4369

  4370 lemma continuous_within_topological:

  4371   "continuous (at x within s) f \<longleftrightarrow>

  4372     (\<forall>B. open B \<longrightarrow> f x \<in> B \<longrightarrow>

  4373       (\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)))"

  4374 unfolding continuous_within

  4375 unfolding tendsto_def Limits.eventually_within eventually_at_topological

  4376 by (intro ball_cong [OF refl] all_cong imp_cong ex_cong conj_cong refl) auto

  4377

  4378 lemma continuous_within_compose:

  4379   assumes "continuous (at x within s) f"

  4380   assumes "continuous (at (f x) within f  s) g"

  4381   shows "continuous (at x within s) (g o f)"

  4382 using assms unfolding continuous_within_topological by simp metis

  4383

  4384 lemma continuous_at_compose:

  4385   assumes "continuous (at x) f" and "continuous (at (f x)) g"

  4386   shows "continuous (at x) (g o f)"

  4387 proof-

  4388   have "continuous (at (f x) within range f) g" using assms(2)

  4389     using continuous_within_subset[of "f x" UNIV g "range f"] by simp

  4390   thus ?thesis using assms(1)

  4391     using continuous_within_compose[of x UNIV f g] by simp

  4392 qed

  4393

  4394 lemma continuous_on_compose:

  4395   "continuous_on s f \<Longrightarrow> continuous_on (f  s) g \<Longrightarrow> continuous_on s (g o f)"

  4396   unfolding continuous_on_topological by simp metis

  4397

  4398 lemma uniformly_continuous_on_compose:

  4399   assumes "uniformly_continuous_on s f"  "uniformly_continuous_on (f  s) g"

  4400   shows "uniformly_continuous_on s (g o f)"

  4401 proof-

  4402   { fix e::real assume "e>0"

  4403     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

  4404     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

  4405     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  }

  4406   thus ?thesis using assms unfolding uniformly_continuous_on_def by auto

  4407 qed

  4408

  4409 lemmas continuous_on_intros = continuous_on_id continuous_on_const

  4410   continuous_on_compose continuous_on_norm continuous_on_infnorm

  4411   continuous_on_add continuous_on_minus continuous_on_diff

  4412   continuous_on_scaleR continuous_on_mult continuous_on_inverse

  4413   continuous_on_inner

  4414   uniformly_continuous_on_id uniformly_continuous_on_const

  4415   uniformly_continuous_on_dist uniformly_continuous_on_norm

  4416   uniformly_continuous_on_compose uniformly_continuous_on_add

  4417   uniformly_continuous_on_minus uniformly_continuous_on_diff

  4418   uniformly_continuous_on_cmul

  4419

  4420 text{* Continuity in terms of open preimages. *}

  4421

  4422 lemma continuous_at_open:

  4423   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)))"

  4424 unfolding continuous_within_topological [of x UNIV f, unfolded within_UNIV]

  4425 unfolding imp_conjL by (intro all_cong imp_cong ex_cong conj_cong refl) auto

  4426

  4427 lemma continuous_on_open:

  4428   shows "continuous_on s f \<longleftrightarrow>

  4429         (\<forall>t. openin (subtopology euclidean (f  s)) t

  4430             --> openin (subtopology euclidean s) {x \<in> s. f x \<in> t})" (is "?lhs = ?rhs")

  4431 proof (safe)

  4432   fix t :: "'b set"

  4433   assume 1: "continuous_on s f"

  4434   assume 2: "openin (subtopology euclidean (f  s)) t"

  4435   from 2 obtain B where B: "open B" and t: "t = f  s \<inter> B"

  4436     unfolding openin_open by auto

  4437   def U == "\<Union>{A. open A \<and> (\<forall>x\<in>s. x \<in> A \<longrightarrow> f x \<in> B)}"

  4438   have "open U" unfolding U_def by (simp add: open_Union)

  4439   moreover have "\<forall>x\<in>s. x \<in> U \<longleftrightarrow> f x \<in> t"

  4440   proof (intro ballI iffI)

  4441     fix x assume "x \<in> s" and "x \<in> U" thus "f x \<in> t"

  4442       unfolding U_def t by auto

  4443   next

  4444     fix x assume "x \<in> s" and "f x \<in> t"

  4445     hence "x \<in> s" and "f x \<in> B"

  4446       unfolding t by auto

  4447     with 1 B obtain A where "open A" "x \<in> A" "\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B"

  4448       unfolding t continuous_on_topological by metis

  4449     then show "x \<in> U"

  4450       unfolding U_def by auto

  4451   qed

  4452   ultimately have "open U \<and> {x \<in> s. f x \<in> t} = s \<inter> U" by auto

  4453   then show "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}"

  4454     unfolding openin_open by fast

  4455 next

  4456   assume "?rhs" show "continuous_on s f"

  4457   unfolding continuous_on_topological

  4458   proof (clarify)

  4459     fix x and B assume "x \<in> s" and "open B" and "f x \<in> B"

  4460     have "openin (subtopology euclidean (f  s)) (f  s \<inter> B)"

  4461       unfolding openin_open using open B by auto

  4462     then have "openin (subtopology euclidean s) {x \<in> s. f x \<in> f  s \<inter> B}"

  4463       using ?rhs by fast

  4464     then show "\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)"

  4465       unfolding openin_open using x \<in> s and f x \<in> B by auto

  4466   qed

  4467 qed

  4468

  4469 text {* Similarly in terms of closed sets. *}

  4470

  4471 lemma continuous_on_closed:

  4472   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")

  4473 proof

  4474   assume ?lhs

  4475   { fix t

  4476     have *:"s - {x \<in> s. f x \<in> f  s - t} = {x \<in> s. f x \<in> t}" by auto

  4477     have **:"f  s - (f  s - (f  s - t)) = f  s - t" by auto

  4478     assume as:"closedin (subtopology euclidean (f  s)) t"

  4479     hence "closedin (subtopology euclidean (f  s)) (f  s - (f  s - t))" unfolding closedin_def topspace_euclidean_subtopology unfolding ** by auto

  4480     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"]]

  4481       unfolding openin_closedin_eq topspace_euclidean_subtopology unfolding * by auto  }

  4482   thus ?rhs by auto

  4483 next

  4484   assume ?rhs

  4485   { fix t

  4486     have *:"s - {x \<in> s. f x \<in> f  s - t} = {x \<in> s. f x \<in> t}" by auto

  4487     assume as:"openin (subtopology euclidean (f  s)) t"

  4488     hence "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}" using ?rhs[THEN spec[where x="(f  s) - t"]]

  4489       unfolding openin_closedin_eq topspace_euclidean_subtopology *[THEN sym] closedin_subtopology by auto }

  4490   thus ?lhs unfolding continuous_on_open by auto

  4491 qed

  4492

  4493 text {* Half-global and completely global cases. *}

  4494

  4495 lemma continuous_open_in_preimage:

  4496   assumes "continuous_on s f"  "open t"

  4497   shows "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}"

  4498 proof-

  4499   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

  4500   have "openin (subtopology euclidean (f  s)) (t \<inter> f  s)"

  4501     using openin_open_Int[of t "f  s", OF assms(2)] unfolding openin_open by auto

  4502   thus ?thesis using assms(1)[unfolded continuous_on_open, THEN spec[where x="t \<inter> f  s"]] using * by auto

  4503 qed

  4504

  4505 lemma continuous_closed_in_preimage:

  4506   assumes "continuous_on s f"  "closed t"

  4507   shows "closedin (subtopology euclidean s) {x \<in> s. f x \<in> t}"

  4508 proof-

  4509   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

  4510   have "closedin (subtopology euclidean (f  s)) (t \<inter> f  s)"

  4511     using closedin_closed_Int[of t "f  s", OF assms(2)] unfolding Int_commute by auto

  4512   thus ?thesis

  4513     using assms(1)[unfolded continuous_on_closed, THEN spec[where x="t \<inter> f  s"]] using * by auto

  4514 qed

  4515

  4516 lemma continuous_open_preimage:

  4517   assumes "continuous_on s f" "open s" "open t"

  4518   shows "open {x \<in> s. f x \<in> t}"

  4519 proof-

  4520   obtain T where T: "open T" "{x \<in> s. f x \<in> t} = s \<inter> T"

  4521     using continuous_open_in_preimage[OF assms(1,3)] unfolding openin_open by auto

  4522   thus ?thesis using open_Int[of s T, OF assms(2)] by auto

  4523 qed

  4524

  4525 lemma continuous_closed_preimage:

  4526   assumes "continuous_on s f" "closed s" "closed t"

  4527   shows "closed {x \<in> s. f x \<in> t}"

  4528 proof-

  4529   obtain T where T: "closed T" "{x \<in> s. f x \<in> t} = s \<inter> T"

  4530     using continuous_closed_in_preimage[OF assms(1,3)] unfolding closedin_closed by auto

  4531   thus ?thesis using closed_Int[of s T, OF assms(2)] by auto

  4532 qed

  4533

  4534 lemma continuous_open_preimage_univ:

  4535   shows "\<forall>x. continuous (at x) f \<Longrightarrow> open s \<Longrightarrow> open {x. f x \<in> s}"

  4536   using continuous_open_preimage[of UNIV f s] open_UNIV continuous_at_imp_continuous_on by auto

  4537

  4538 lemma continuous_closed_preimage_univ:

  4539   shows "(\<forall>x. continuous (at x) f) \<Longrightarrow> closed s ==> closed {x. f x \<in> s}"

  4540   using continuous_closed_preimage[of UNIV f s] closed_UNIV continuous_at_imp_continuous_on by auto

  4541

  4542 lemma continuous_open_vimage:

  4543   shows "\<forall>x. continuous (at x) f \<Longrightarrow> open s \<Longrightarrow> open (f - s)"

  4544   unfolding vimage_def by (rule continuous_open_preimage_univ)

  4545

  4546 lemma continuous_closed_vimage:

  4547   shows "\<forall>x. continuous (at x) f \<Longrightarrow> closed s \<Longrightarrow> closed (f - s)"

  4548   unfolding vimage_def by (rule continuous_closed_preimage_univ)

  4549

  4550 lemma interior_image_subset:

  4551   assumes "\<forall>x. continuous (at x) f" "inj f"

  4552   shows "interior (f  s) \<subseteq> f  (interior s)"

  4553 proof

  4554   fix x assume "x \<in> interior (f  s)"

  4555   then obtain T where as: "open T" "x \<in> T" "T \<subseteq> f  s" ..

  4556   hence "x \<in> f  s" by auto

  4557   then obtain y where y: "y \<in> s" "x = f y" by auto

  4558   have "open (vimage f T)"

  4559     using assms(1) open T by (rule continuous_open_vimage)

  4560   moreover have "y \<in> vimage f T"

  4561     using x = f y x \<in> T by simp

  4562   moreover have "vimage f T \<subseteq> s"

  4563     using T \<subseteq> image f s inj f unfolding inj_on_def subset_eq by auto

  4564   ultimately have "y \<in> interior s" ..

  4565   with x = f y show "x \<in> f  interior s" ..

  4566 qed

  4567

  4568 text {* Equality of continuous functions on closure and related results. *}

  4569

  4570 lemma continuous_closed_in_preimage_constant:

  4571   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  4572   shows "continuous_on s f ==> closedin (subtopology euclidean s) {x \<in> s. f x = a}"

  4573   using continuous_closed_in_preimage[of s f "{a}"] by auto

  4574

  4575 lemma continuous_closed_preimage_constant:

  4576   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  4577   shows "continuous_on s f \<Longrightarrow> closed s ==> closed {x \<in> s. f x = a}"

  4578   using continuous_closed_preimage[of s f "{a}"] by auto

  4579

  4580 lemma continuous_constant_on_closure:

  4581   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  4582   assumes "continuous_on (closure s) f"

  4583           "\<forall>x \<in> s. f x = a"

  4584   shows "\<forall>x \<in> (closure s). f x = a"

  4585     using continuous_closed_preimage_constant[of "closure s" f a]

  4586     assms closure_minimal[of s "{x \<in> closure s. f x = a}"] closure_subset unfolding subset_eq by auto

  4587

  4588 lemma image_closure_subset:

  4589   assumes "continuous_on (closure s) f"  "closed t"  "(f  s) \<subseteq> t"

  4590   shows "f  (closure s) \<subseteq> t"

  4591 proof-

  4592   have "s \<subseteq> {x \<in> closure s. f x \<in> t}" using assms(3) closure_subset by auto

  4593   moreover have "closed {x \<in> closure s. f x \<in> t}"

  4594     using continuous_closed_preimage[OF assms(1)] and assms(2) by auto

  4595   ultimately have "closure s = {x \<in> closure s . f x \<in> t}"

  4596     using closure_minimal[of s "{x \<in> closure s. f x \<in> t}"] by auto

  4597   thus ?thesis by auto

  4598 qed

  4599

  4600 lemma continuous_on_closure_norm_le:

  4601   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"

  4602   assumes "continuous_on (closure s) f"  "\<forall>y \<in> s. norm(f y) \<le> b"  "x \<in> (closure s)"

  4603   shows "norm(f x) \<le> b"

  4604 proof-

  4605   have *:"f  s \<subseteq> cball 0 b" using assms(2)[unfolded mem_cball_0[THEN sym]] by auto

  4606   show ?thesis

  4607     using image_closure_subset[OF assms(1) closed_cball[of 0 b] *] assms(3)

  4608     unfolding subset_eq apply(erule_tac x="f x" in ballE) by (auto simp add: dist_norm)

  4609 qed

  4610

  4611 text {* Making a continuous function avoid some value in a neighbourhood. *}

  4612

  4613 lemma continuous_within_avoid:

  4614   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"

  4615   assumes "continuous (at x within s) f" and "f x \<noteq> a"

  4616   shows "\<exists>e>0. \<forall>y \<in> s. dist x y < e --> f y \<noteq> a"

  4617 proof-

  4618   obtain U where "open U" and "f x \<in> U" and "a \<notin> U"

  4619     using t1_space [OF f x \<noteq> a] by fast

  4620   have "(f ---> f x) (at x within s)"

  4621     using assms(1) by (simp add: continuous_within)

  4622   hence "eventually (\<lambda>y. f y \<in> U) (at x within s)"

  4623     using open U and f x \<in> U

  4624     unfolding tendsto_def by fast

  4625   hence "eventually (\<lambda>y. f y \<noteq> a) (at x within s)"

  4626     using a \<notin> U by (fast elim: eventually_mono [rotated])

  4627   thus ?thesis

  4628     unfolding Limits.eventually_within Limits.eventually_at

  4629     by (rule ex_forward, cut_tac f x \<noteq> a, auto simp: dist_commute)

  4630 qed

  4631

  4632 lemma continuous_at_avoid:

  4633   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"

  4634   assumes "continuous (at x) f" and "f x \<noteq> a"

  4635   shows "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> f y \<noteq> a"

  4636   using assms continuous_within_avoid[of x UNIV f a] by simp

  4637

  4638 lemma continuous_on_avoid:

  4639   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"

  4640   assumes "continuous_on s f"  "x \<in> s"  "f x \<noteq> a"

  4641   shows "\<exists>e>0. \<forall>y \<in> s. dist x y < e \<longrightarrow> f y \<noteq> a"

  4642 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

  4643

  4644 lemma continuous_on_open_avoid:

  4645   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"

  4646   assumes "continuous_on s f"  "open s"  "x \<in> s"  "f x \<noteq> a"

  4647   shows "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> f y \<noteq> a"

  4648 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

  4649

  4650 text {* Proving a function is constant by proving open-ness of level set. *}

  4651

  4652 lemma continuous_levelset_open_in_cases:

  4653   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  4654   shows "connected s \<Longrightarrow> continuous_on s f \<Longrightarrow>

  4655         openin (subtopology euclidean s) {x \<in> s. f x = a}

  4656         ==> (\<forall>x \<in> s. f x \<noteq> a) \<or> (\<forall>x \<in> s. f x = a)"

  4657 unfolding connected_clopen using continuous_closed_in_preimage_constant by auto

  4658

  4659 lemma continuous_levelset_open_in:

  4660   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  4661   shows "connected s \<Longrightarrow> continuous_on s f \<Longrightarrow>

  4662         openin (subtopology euclidean s) {x \<in> s. f x = a} \<Longrightarrow>

  4663         (\<exists>x \<in> s. f x = a)  ==> (\<forall>x \<in> s. f x = a)"

  4664 using continuous_levelset_open_in_cases[of s f ]

  4665 by meson

  4666

  4667 lemma continuous_levelset_open:

  4668   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  4669   assumes "connected s"  "continuous_on s f"  "open {x \<in> s. f x = a}"  "\<exists>x \<in> s.  f x = a"

  4670   shows "\<forall>x \<in> s. f x = a"

  4671 using continuous_levelset_open_in[OF assms(1,2), of a, unfolded openin_open] using assms (3,4) by fast

  4672

  4673 text {* Some arithmetical combinations (more to prove). *}

  4674

  4675 lemma open_scaling[intro]:

  4676   fixes s :: "'a::real_normed_vector set"

  4677   assumes "c \<noteq> 0"  "open s"

  4678   shows "open((\<lambda>x. c *\<^sub>R x)  s)"

  4679 proof-

  4680   { fix x assume "x \<in> s"

  4681     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

  4682     have "e * abs c > 0" using assms(1)[unfolded zero_less_abs_iff[THEN sym]] using mult_pos_pos[OF e>0] by auto

  4683     moreover

  4684     { fix y assume "dist y (c *\<^sub>R x) < e * \<bar>c\<bar>"

  4685       hence "norm ((1 / c) *\<^sub>R y - x) < e" unfolding dist_norm

  4686         using norm_scaleR[of c "(1 / c) *\<^sub>R y - x", unfolded scaleR_right_diff_distrib, unfolded scaleR_scaleR] assms(1)

  4687           assms(1)[unfolded zero_less_abs_iff[THEN sym]] by (simp del:zero_less_abs_iff)

  4688       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  }

  4689     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  }

  4690   thus ?thesis unfolding open_dist by auto

  4691 qed

  4692

  4693 lemma minus_image_eq_vimage:

  4694   fixes A :: "'a::ab_group_add set"

  4695   shows "(\<lambda>x. - x)  A = (\<lambda>x. - x) - A"

  4696   by (auto intro!: image_eqI [where f="\<lambda>x. - x"])

  4697

  4698 lemma open_negations:

  4699   fixes s :: "'a::real_normed_vector set"

  4700   shows "open s ==> open ((\<lambda> x. -x)  s)"

  4701   unfolding scaleR_minus1_left [symmetric]

  4702   by (rule open_scaling, auto)

  4703

  4704 lemma open_translation:

  4705   fixes s :: "'a::real_normed_vector set"

  4706   assumes "open s"  shows "open((\<lambda>x. a + x)  s)"

  4707 proof-

  4708   { fix x have "continuous (at x) (\<lambda>x. x - a)"

  4709       by (intro continuous_diff continuous_at_id continuous_const) }

  4710   moreover have "{x. x - a \<in> s} = op + a  s" by force

  4711   ultimately show ?thesis using continuous_open_preimage_univ[of "\<lambda>x. x - a" s] using assms by auto

  4712 qed

  4713

  4714 lemma open_affinity:

  4715   fixes s :: "'a::real_normed_vector set"

  4716   assumes "open s"  "c \<noteq> 0"

  4717   shows "open ((\<lambda>x. a + c *\<^sub>R x)  s)"

  4718 proof-

  4719   have *:"(\<lambda>x. a + c *\<^sub>R x) = (\<lambda>x. a + x) \<circ> (\<lambda>x. c *\<^sub>R x)" unfolding o_def ..

  4720   have "op + a  op *\<^sub>R c  s = (op + a \<circ> op *\<^sub>R c)  s" by auto

  4721   thus ?thesis using assms open_translation[of "op *\<^sub>R c  s" a] unfolding * by auto

  4722 qed

  4723

  4724 lemma interior_translation:

  4725   fixes s :: "'a::real_normed_vector set"

  4726   shows "interior ((\<lambda>x. a + x)  s) = (\<lambda>x. a + x)  (interior s)"

  4727 proof (rule set_eqI, rule)

  4728   fix x assume "x \<in> interior (op + a  s)"

  4729   then obtain e where "e>0" and e:"ball x e \<subseteq> op + a  s" unfolding mem_interior by auto

  4730   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

  4731   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

  4732 next

  4733   fix x assume "x \<in> op + a  interior s"

  4734   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

  4735   { fix z have *:"a + y - z = y + a - z" by auto

  4736     assume "z\<in>ball x e"

  4737     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

  4738     hence "z \<in> op + a  s" unfolding image_iff by(auto intro!: bexI[where x="z - a"])  }

  4739   hence "ball x e \<subseteq> op + a  s" unfolding subset_eq by auto

  4740   thus "x \<in> interior (op + a  s)" unfolding mem_interior using e>0 by auto

  4741 qed

  4742

  4743 text {* Topological properties of linear functions. *}

  4744

  4745 lemma linear_lim_0:

  4746   assumes "bounded_linear f" shows "(f ---> 0) (at (0))"

  4747 proof-

  4748   interpret f: bounded_linear f by fact

  4749   have "(f ---> f 0) (at 0)"

  4750     using tendsto_ident_at by (rule f.tendsto)

  4751   thus ?thesis unfolding f.zero .

  4752 qed

  4753

  4754 lemma linear_continuous_at:

  4755   assumes "bounded_linear f"  shows "continuous (at a) f"

  4756   unfolding continuous_at using assms

  4757   apply (rule bounded_linear.tendsto)

  4758   apply (rule tendsto_ident_at)

  4759   done

  4760

  4761 lemma linear_continuous_within:

  4762   shows "bounded_linear f ==> continuous (at x within s) f"

  4763   using continuous_at_imp_continuous_within[of x f s] using linear_continuous_at[of f] by auto

  4764

  4765 lemma linear_continuous_on:

  4766   shows "bounded_linear f ==> continuous_on s f"

  4767   using continuous_at_imp_continuous_on[of s f] using linear_continuous_at[of f] by auto

  4768

  4769 text {* Also bilinear functions, in composition form. *}

  4770

  4771 lemma bilinear_continuous_at_compose:

  4772   shows "continuous (at x) f \<Longrightarrow> continuous (at x) g \<Longrightarrow> bounded_bilinear h

  4773         ==> continuous (at x) (\<lambda>x. h (f x) (g x))"

  4774   unfolding continuous_at using Lim_bilinear[of f "f x" "(at x)" g "g x" h] by auto

  4775

  4776 lemma bilinear_continuous_within_compose:

  4777   shows "continuous (at x within s) f \<Longrightarrow> continuous (at x within s) g \<Longrightarrow> bounded_bilinear h

  4778         ==> continuous (at x within s) (\<lambda>x. h (f x) (g x))"

  4779   unfolding continuous_within using Lim_bilinear[of f "f x"] by auto

  4780

  4781 lemma bilinear_continuous_on_compose:

  4782   shows "continuous_on s f \<Longrightarrow> continuous_on s g \<Longrightarrow> bounded_bilinear h

  4783              ==> continuous_on s (\<lambda>x. h (f x) (g x))"

  4784   unfolding continuous_on_def

  4785   by (fast elim: bounded_bilinear.tendsto)

  4786

  4787 text {* Preservation of compactness and connectedness under continuous function. *}

  4788

  4789 lemma compact_eq_openin_cover:

  4790   "compact S \<longleftrightarrow>

  4791     (\<forall>C. (\<forall>c\<in>C. openin (subtopology euclidean S) c) \<and> S \<subseteq> \<Union>C \<longrightarrow>

  4792       (\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D))"

  4793 proof safe

  4794   fix C

  4795   assume "compact S" and "\<forall>c\<in>C. openin (subtopology euclidean S) c" and "S \<subseteq> \<Union>C"

  4796   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}"

  4797     unfolding openin_open by force+

  4798   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"

  4799     by (rule compactE)

  4800   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)"

  4801     by auto

  4802   thus "\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D" ..

  4803 next

  4804   assume 1: "\<forall>C. (\<forall>c\<in>C. openin (subtopology euclidean S) c) \<and> S \<subseteq> \<Union>C \<longrightarrow>

  4805         (\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D)"

  4806   show "compact S"

  4807   proof (rule compactI)

  4808     fix C

  4809     let ?C = "image (\<lambda>T. S \<inter> T) C"

  4810     assume "\<forall>t\<in>C. open t" and "S \<subseteq> \<Union>C"

  4811     hence "(\<forall>c\<in>?C. openin (subtopology euclidean S) c) \<and> S \<subseteq> \<Union>?C"

  4812       unfolding openin_open by auto

  4813     with 1 obtain D where "D \<subseteq> ?C" and "finite D" and "S \<subseteq> \<Union>D"

  4814       by metis

  4815     let ?D = "inv_into C (\<lambda>T. S \<inter> T)  D"

  4816     have "?D \<subseteq> C \<and> finite ?D \<and> S \<subseteq> \<Union>?D"

  4817     proof (intro conjI)

  4818       from D \<subseteq> ?C show "?D \<subseteq> C"

  4819         by (fast intro: inv_into_into)

  4820       from finite D show "finite ?D"

  4821         by (rule finite_imageI)

  4822       from S \<subseteq> \<Union>D show "S \<subseteq> \<Union>?D"

  4823         apply (rule subset_trans)

  4824         apply clarsimp

  4825         apply (frule subsetD [OF D \<subseteq> ?C, THEN f_inv_into_f])

  4826         apply (erule rev_bexI, fast)

  4827         done

  4828     qed

  4829     thus "\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D" ..

  4830   qed

  4831 qed

  4832

  4833 lemma compact_continuous_image:

  4834   assumes "continuous_on s f" and "compact s"

  4835   shows "compact (f  s)"

  4836 using assms (* FIXME: long unstructured proof *)

  4837 unfolding continuous_on_open

  4838 unfolding compact_eq_openin_cover

  4839 apply clarify

  4840 apply (drule_tac x="image (\<lambda>t. {x \<in> s. f x \<in> t}) C" in spec)

  4841 apply (drule mp)

  4842 apply (rule conjI)

  4843 apply simp

  4844 apply clarsimp

  4845 apply (drule subsetD)

  4846 apply (erule imageI)

  4847 apply fast

  4848 apply (erule thin_rl)

  4849 apply clarify

  4850 apply (rule_tac x="image (inv_into C (\<lambda>t. {x \<in> s. f x \<in> t})) D" in exI)

  4851 apply (intro conjI)

  4852 apply clarify

  4853 apply (rule inv_into_into)

  4854 apply (erule (1) subsetD)

  4855 apply (erule finite_imageI)

  4856 apply (clarsimp, rename_tac x)

  4857 apply (drule (1) subsetD, clarify)

  4858 apply (drule (1) subsetD, clarify)

  4859 apply (rule rev_bexI)

  4860 apply assumption

  4861 apply (subgoal_tac "{x \<in> s. f x \<in> t} \<in> (\<lambda>t. {x \<in> s. f x \<in> t})  C")

  4862 apply (drule f_inv_into_f)

  4863 apply fast

  4864 apply (erule imageI)

  4865 done

  4866

  4867 lemma connected_continuous_image:

  4868   assumes "continuous_on s f"  "connected s"

  4869   shows "connected(f  s)"

  4870 proof-

  4871   { fix T assume as: "T \<noteq> {}"  "T \<noteq> f  s"  "openin (subtopology euclidean (f  s)) T"  "closedin (subtopology euclidean (f  s)) T"

  4872     have "{x \<in> s. f x \<in> T} = {} \<or> {x \<in> s. f x \<in> T} = s"

  4873       using assms(1)[unfolded continuous_on_open, THEN spec[where x=T]]

  4874       using assms(1)[unfolded continuous_on_closed, THEN spec[where x=T]]

  4875       using assms(2)[unfolded connected_clopen, THEN spec[where x="{x \<in> s. f x \<in> T}"]] as(3,4) by auto

  4876     hence False using as(1,2)

  4877       using as(4)[unfolded closedin_def topspace_euclidean_subtopology] by auto }

  4878   thus ?thesis unfolding connected_clopen by auto

  4879 qed

  4880

  4881 text {* Continuity implies uniform continuity on a compact domain. *}

  4882

  4883 lemma compact_uniformly_continuous:

  4884   assumes "continuous_on s f"  "compact s"

  4885   shows "uniformly_continuous_on s f"

  4886 proof-

  4887     { fix x assume x:"x\<in>s"

  4888       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

  4889       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  }

  4890     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

  4891     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)"

  4892       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

  4893

  4894   { fix e::real assume "e>0"

  4895

  4896     { 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  }

  4897     hence "s \<subseteq> \<Union>{ball x (d x (e / 2)) |x. x \<in> s}" unfolding subset_eq by auto

  4898     moreover

  4899     { fix b assume "b\<in>{ball x (d x (e / 2)) |x. x \<in> s}" hence "open b" by auto  }

  4900     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"

  4901       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

  4902

  4903     { fix x y assume "x\<in>s" "y\<in>s" and as:"dist y x < ea"

  4904       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

  4905       hence "x\<in>ball z (d z (e / 2))" using ea>0 unfolding subset_eq by auto

  4906       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

  4907         by (auto  simp add: dist_commute)

  4908       moreover have "y\<in>ball z (d z (e / 2))" using as and ea>0 and z[unfolded subset_eq]

  4909         by (auto simp add: dist_commute)

  4910       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

  4911         by (auto  simp add: dist_commute)

  4912       ultimately have "dist (f y) (f x) < e" using dist_triangle_half_r[of "f z" "f x" e "f y"]

  4913         by (auto simp add: dist_commute)  }

  4914     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  }

  4915   thus ?thesis unfolding uniformly_continuous_on_def by auto

  4916 qed

  4917

  4918 text{* Continuity of inverse function on compact domain. *}

  4919

  4920 lemma continuous_on_inv:

  4921   fixes f :: "'a::topological_space \<Rightarrow> 'b::t2_space"

  4922   assumes "continuous_on s f"  "compact s"  "\<forall>x \<in> s. g (f x) = x"

  4923   shows "continuous_on (f  s) g"

  4924 unfolding continuous_on_topological

  4925 proof (clarsimp simp add: assms(3))

  4926   fix x :: 'a and B :: "'a set"

  4927   assume "x \<in> s" and "open B" and "x \<in> B"

  4928   have 1: "\<forall>x\<in>s. f x \<in> f  (s - B) \<longleftrightarrow> x \<in> s - B"

  4929     using assms(3) by (auto, metis)

  4930   have "continuous_on (s - B) f"

  4931     using continuous_on s f Diff_subset

  4932     by (rule continuous_on_subset)

  4933   moreover have "compact (s - B)"

  4934     using open B and compact s

  4935     unfolding Diff_eq by (intro compact_inter_closed closed_Compl)

  4936   ultimately have "compact (f  (s - B))"

  4937     by (rule compact_continuous_image)

  4938   hence "closed (f  (s - B))"

  4939     by (rule compact_imp_closed)

  4940   hence "open (- f  (s - B))"

  4941     by (rule open_Compl)

  4942   moreover have "f x \<in> - f  (s - B)"

  4943     using x \<in> s and x \<in> B by (simp add: 1)

  4944   moreover have "\<forall>y\<in>s. f y \<in> - f  (s - B) \<longrightarrow> y \<in> B"

  4945     by (simp add: 1)

  4946   ultimately show "\<exists>A. open A \<and> f x \<in> A \<and> (\<forall>y\<in>s. f y \<in> A \<longrightarrow> y \<in> B)"

  4947     by fast

  4948 qed

  4949

  4950 text {* A uniformly convergent limit of continuous functions is continuous. *}

  4951

  4952 lemma continuous_uniform_limit:

  4953   fixes f :: "'a \<Rightarrow> 'b::metric_space \<Rightarrow> 'c::metric_space"

  4954   assumes "\<not> trivial_limit F"

  4955   assumes "eventually (\<lambda>n. continuous_on s (f n)) F"

  4956   assumes "\<forall>e>0. eventually (\<lambda>n. \<forall>x\<in>s. dist (f n x) (g x) < e) F"

  4957   shows "continuous_on s g"

  4958 proof-

  4959   { fix x and e::real assume "x\<in>s" "e>0"

  4960     have "eventually (\<lambda>n. \<forall>x\<in>s. dist (f n x) (g x) < e / 3) F"

  4961       using e>0 assms(3)[THEN spec[where x="e/3"]] by auto

  4962     from eventually_happens [OF eventually_conj [OF this assms(2)]]

  4963     obtain n where n:"\<forall>x\<in>s. dist (f n x) (g x) < e / 3"  "continuous_on s (f n)"

  4964       using assms(1) by blast

  4965     have "e / 3 > 0" using e>0 by auto

  4966     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"

  4967       using n(2)[unfolded continuous_on_iff, THEN bspec[where x=x], OF x\<in>s, THEN spec[where x="e/3"]] by blast

  4968     { fix y assume "y \<in> s" and "dist y x < d"

  4969       hence "dist (f n y) (f n x) < e / 3"

  4970         by (rule d [rule_format])

  4971       hence "dist (f n y) (g x) < 2 * e / 3"

  4972         using dist_triangle [of "f n y" "g x" "f n x"]

  4973         using n(1)[THEN bspec[where x=x], OF x\<in>s]

  4974         by auto

  4975       hence "dist (g y) (g x) < e"

  4976         using n(1)[THEN bspec[where x=y], OF y\<in>s]

  4977         using dist_triangle3 [of "g y" "g x" "f n y"]

  4978         by auto }

  4979     hence "\<exists>d>0. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (g x') (g x) < e"

  4980       using d>0 by auto }

  4981   thus ?thesis unfolding continuous_on_iff by auto

  4982 qed

  4983

  4984

  4985 subsection {* Topological stuff lifted from and dropped to R *}

  4986

  4987 lemma open_real:

  4988   fixes s :: "real set" shows

  4989  "open s \<longleftrightarrow>

  4990         (\<forall>x \<in> s. \<exists>e>0. \<forall>x'. abs(x' - x) < e --> x' \<in> s)" (is "?lhs = ?rhs")

  4991   unfolding open_dist dist_norm by simp

  4992

  4993 lemma islimpt_approachable_real:

  4994   fixes s :: "real set"

  4995   shows "x islimpt s \<longleftrightarrow> (\<forall>e>0.  \<exists>x'\<in> s. x' \<noteq> x \<and> abs(x' - x) < e)"

  4996   unfolding islimpt_approachable dist_norm by simp

  4997

  4998 lemma closed_real:

  4999   fixes s :: "real set"

  5000   shows "closed s \<longleftrightarrow>

  5001         (\<forall>x. (\<forall>e>0.  \<exists>x' \<in> s. x' \<noteq> x \<and> abs(x' - x) < e)

  5002             --> x \<in> s)"

  5003   unfolding closed_limpt islimpt_approachable dist_norm by simp

  5004

  5005 lemma continuous_at_real_range:

  5006   fixes f :: "'a::real_normed_vector \<Rightarrow> real"

  5007   shows "continuous (at x) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0.

  5008         \<forall>x'. norm(x' - x) < d --> abs(f x' - f x) < e)"

  5009   unfolding continuous_at unfolding Lim_at

  5010   unfolding dist_nz[THEN sym] unfolding dist_norm apply auto

  5011   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

  5012   apply(erule_tac x=e in allE) by auto

  5013

  5014 lemma continuous_on_real_range:

  5015   fixes f :: "'a::real_normed_vector \<Rightarrow> real"

  5016   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))"

  5017   unfolding continuous_on_iff dist_norm by simp

  5018

  5019 text {* Hence some handy theorems on distance, diameter etc. of/from a set. *}

  5020

  5021 lemma compact_attains_sup:

  5022   fixes s :: "real set"

  5023   assumes "compact s"  "s \<noteq> {}"

  5024   shows "\<exists>x \<in> s. \<forall>y \<in> s. y \<le> x"

  5025 proof-

  5026   from assms(1) have a:"bounded s" "closed s" unfolding compact_eq_bounded_closed by auto

  5027   { 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"

  5028     have "isLub UNIV s (Sup s)" using Sup[OF assms(2)] unfolding setle_def using as(1) by auto

  5029     moreover have "isUb UNIV s (Sup s - e)" unfolding isUb_def unfolding setle_def using as(4,2) by auto

  5030     ultimately have False using isLub_le_isUb[of UNIV s "Sup s" "Sup s - e"] using e>0 by auto  }

  5031   thus ?thesis using bounded_has_Sup(1)[OF a(1) assms(2)] using a(2)[unfolded closed_real, THEN spec[where x="Sup s"]]

  5032     apply(rule_tac x="Sup s" in bexI) by auto

  5033 qed

  5034

  5035 lemma Inf:

  5036   fixes S :: "real set"

  5037   shows "S \<noteq> {} ==> (\<exists>b. b <=* S) ==> isGlb UNIV S (Inf S)"

  5038 by (auto simp add: isLb_def setle_def setge_def isGlb_def greatestP_def)

  5039

  5040 lemma compact_attains_inf:

  5041   fixes s :: "real set"

  5042   assumes "compact s" "s \<noteq> {}"  shows "\<exists>x \<in> s. \<forall>y \<in> s. x \<le> y"

  5043 proof-

  5044   from assms(1) have a:"bounded s" "closed s" unfolding compact_eq_bounded_closed by auto

  5045   { fix e::real assume as: "\<forall>x\<in>s. x \<ge> Inf s"  "Inf s \<notin> s"  "0 < e"

  5046       "\<forall>x'\<in>s. x' = Inf s \<or> \<not> abs (x' - Inf s) < e"

  5047     have "isGlb UNIV s (Inf s)" using Inf[OF assms(2)] unfolding setge_def using as(1) by auto

  5048     moreover

  5049     { fix x assume "x \<in> s"

  5050       hence *:"abs (x - Inf s) = x - Inf s" using as(1)[THEN bspec[where x=x]] by auto

  5051       have "Inf s + e \<le> x" using as(4)[THEN bspec[where x=x]] using as(2) x\<in>s unfolding * by auto }

  5052     hence "isLb UNIV s (Inf s + e)" unfolding isLb_def and setge_def by auto

  5053     ultimately have False using isGlb_le_isLb[of UNIV s "Inf s" "Inf s + e"] using e>0 by auto  }

  5054   thus ?thesis using bounded_has_Inf(1)[OF a(1) assms(2)] using a(2)[unfolded closed_real, THEN spec[where x="Inf s"]]

  5055     apply(rule_tac x="Inf s" in bexI) by auto

  5056 qed

  5057

  5058 lemma continuous_attains_sup:

  5059   fixes f :: "'a::metric_space \<Rightarrow> real"

  5060   shows "compact s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> continuous_on s f

  5061         ==> (\<exists>x \<in> s. \<forall>y \<in> s.  f y \<le> f x)"

  5062   using compact_attains_sup[of "f  s"]

  5063   using compact_continuous_image[of s f] by auto

  5064

  5065 lemma continuous_attains_inf:

  5066   fixes f :: "'a::metric_space \<Rightarrow> real"

  5067   shows "compact s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> continuous_on s f

  5068         \<Longrightarrow> (\<exists>x \<in> s. \<forall>y \<in> s. f x \<le> f y)"

  5069   using compact_attains_inf[of "f  s"]

  5070   using compact_continuous_image[of s f] by auto

  5071

  5072 lemma distance_attains_sup:

  5073   assumes "compact s" "s \<noteq> {}"

  5074   shows "\<exists>x \<in> s. \<forall>y \<in> s. dist a y \<le> dist a x"

  5075 proof (rule continuous_attains_sup [OF assms])

  5076   { fix x assume "x\<in>s"

  5077     have "(dist a ---> dist a x) (at x within s)"

  5078       by (intro tendsto_dist tendsto_const Lim_at_within tendsto_ident_at)

  5079   }

  5080   thus "continuous_on s (dist a)"

  5081     unfolding continuous_on ..

  5082 qed

  5083

  5084 text {* For \emph{minimal} distance, we only need closure, not compactness. *}

  5085

  5086 lemma distance_attains_inf:

  5087   fixes a :: "'a::heine_borel"

  5088   assumes "closed s"  "s \<noteq> {}"

  5089   shows "\<exists>x \<in> s. \<forall>y \<in> s. dist a x \<le> dist a y"

  5090 proof-

  5091   from assms(2) obtain b where "b\<in>s" by auto

  5092   let ?B = "cball a (dist b a) \<inter> s"

  5093   have "b \<in> ?B" using b\<in>s by (simp add: dist_commute)

  5094   hence "?B \<noteq> {}" by auto

  5095   moreover

  5096   { fix x assume "x\<in>?B"

  5097     fix e::real assume "e>0"

  5098     { fix x' assume "x'\<in>?B" and as:"dist x' x < e"

  5099       from as have "\<bar>dist a x' - dist a x\<bar> < e"

  5100         unfolding abs_less_iff minus_diff_eq

  5101         using dist_triangle2 [of a x' x]

  5102         using dist_triangle [of a x x']

  5103         by arith

  5104     }

  5105     hence "\<exists>d>0. \<forall>x'\<in>?B. dist x' x < d \<longrightarrow> \<bar>dist a x' - dist a x\<bar> < e"

  5106       using e>0 by auto

  5107   }

  5108   hence "continuous_on (cball a (dist b a) \<inter> s) (dist a)"

  5109     unfolding continuous_on Lim_within dist_norm real_norm_def

  5110     by fast

  5111   moreover have "compact ?B"

  5112     using compact_cball[of a "dist b a"]

  5113     unfolding compact_eq_bounded_closed

  5114     using bounded_Int and closed_Int and assms(1) by auto

  5115   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"

  5116     using continuous_attains_inf[of ?B "dist a"] by fastforce

  5117   thus ?thesis by fastforce

  5118 qed

  5119

  5120

  5121 subsection {* Pasted sets *}

  5122

  5123 lemma bounded_Times:

  5124   assumes "bounded s" "bounded t" shows "bounded (s \<times> t)"

  5125 proof-

  5126   obtain x y a b where "\<forall>z\<in>s. dist x z \<le> a" "\<forall>z\<in>t. dist y z \<le> b"

  5127     using assms [unfolded bounded_def] by auto

  5128   then have "\<forall>z\<in>s \<times> t. dist (x, y) z \<le> sqrt (a\<twosuperior> + b\<twosuperior>)"

  5129     by (auto simp add: dist_Pair_Pair real_sqrt_le_mono add_mono power_mono)

  5130   thus ?thesis unfolding bounded_any_center [where a="(x, y)"] by auto

  5131 qed

  5132

  5133 lemma mem_Times_iff: "x \<in> A \<times> B \<longleftrightarrow> fst x \<in> A \<and> snd x \<in> B"

  5134 by (induct x) simp

  5135

  5136 lemma seq_compact_Times: "seq_compact s \<Longrightarrow> seq_compact t \<Longrightarrow> seq_compact (s \<times> t)"

  5137 unfolding seq_compact_def

  5138 apply clarify

  5139 apply (drule_tac x="fst \<circ> f" in spec)

  5140 apply (drule mp, simp add: mem_Times_iff)

  5141 apply (clarify, rename_tac l1 r1)

  5142 apply (drule_tac x="snd \<circ> f \<circ> r1" in spec)

  5143 apply (drule mp, simp add: mem_Times_iff)

  5144 apply (clarify, rename_tac l2 r2)

  5145 apply (rule_tac x="(l1, l2)" in rev_bexI, simp)

  5146 apply (rule_tac x="r1 \<circ> r2" in exI)

  5147 apply (rule conjI, simp add: subseq_def)

  5148 apply (drule_tac r=r2 in lim_subseq [rotated], assumption)

  5149 apply (drule (1) tendsto_Pair) back

  5150 apply (simp add: o_def)

  5151 done

  5152

  5153 text {* Generalize to @{class topological_space} *}

  5154 lemma compact_Times:

  5155   fixes s :: "'a::metric_space set" and t :: "'b::metric_space set"

  5156   shows "compact s \<Longrightarrow> compact t \<Longrightarrow> compact (s \<times> t)"

  5157   unfolding compact_eq_seq_compact_metric by (rule seq_compact_Times)

  5158

  5159 text{* Hence some useful properties follow quite easily. *}

  5160

  5161 lemma compact_scaling:

  5162   fixes s :: "'a::real_normed_vector set"

  5163   assumes "compact s"  shows "compact ((\<lambda>x. c *\<^sub>R x)  s)"

  5164 proof-

  5165   let ?f = "\<lambda>x. scaleR c x"

  5166   have *:"bounded_linear ?f" by (rule bounded_linear_scaleR_right)

  5167   show ?thesis using compact_continuous_image[of s ?f] continuous_at_imp_continuous_on[of s ?f]

  5168     using linear_continuous_at[OF *] assms by auto

  5169 qed

  5170

  5171 lemma compact_negations:

  5172   fixes s :: "'a::real_normed_vector set"

  5173   assumes "compact s"  shows "compact ((\<lambda>x. -x)  s)"

  5174   using compact_scaling [OF assms, of "- 1"] by auto

  5175

  5176 lemma compact_sums:

  5177   fixes s t :: "'a::real_normed_vector set"

  5178   assumes "compact s"  "compact t"  shows "compact {x + y | x y. x \<in> s \<and> y \<in> t}"

  5179 proof-

  5180   have *:"{x + y | x y. x \<in> s \<and> y \<in> t} = (\<lambda>z. fst z + snd z)  (s \<times> t)"

  5181     apply auto unfolding image_iff apply(rule_tac x="(xa, y)" in bexI) by auto

  5182   have "continuous_on (s \<times> t) (\<lambda>z. fst z + snd z)"

  5183     unfolding continuous_on by (rule ballI) (intro tendsto_intros)

  5184   thus ?thesis unfolding * using compact_continuous_image compact_Times [OF assms] by auto

  5185 qed

  5186

  5187 lemma compact_differences:

  5188   fixes s t :: "'a::real_normed_vector set"

  5189   assumes "compact s" "compact t"  shows "compact {x - y | x y. x \<in> s \<and> y \<in> t}"

  5190 proof-

  5191   have "{x - y | x y. x\<in>s \<and> y \<in> t} =  {x + y | x y. x \<in> s \<and> y \<in> (uminus  t)}"

  5192     apply auto apply(rule_tac x= xa in exI) apply auto apply(rule_tac x=xa in exI) by auto

  5193   thus ?thesis using compact_sums[OF assms(1) compact_negations[OF assms(2)]] by auto

  5194 qed

  5195

  5196 lemma compact_translation:

  5197   fixes s :: "'a::real_normed_vector set"

  5198   assumes "compact s"  shows "compact ((\<lambda>x. a + x)  s)"

  5199 proof-

  5200   have "{x + y |x y. x \<in> s \<and> y \<in> {a}} = (\<lambda>x. a + x)  s" by auto

  5201   thus ?thesis using compact_sums[OF assms compact_sing[of a]] by auto

  5202 qed

  5203

  5204 lemma compact_affinity:

  5205   fixes s :: "'a::real_normed_vector set"

  5206   assumes "compact s"  shows "compact ((\<lambda>x. a + c *\<^sub>R x)  s)"

  5207 proof-

  5208   have "op + a  op *\<^sub>R c  s = (\<lambda>x. a + c *\<^sub>R x)  s" by auto

  5209   thus ?thesis using compact_translation[OF compact_scaling[OF assms], of a c] by auto

  5210 qed

  5211

  5212 text {* Hence we get the following. *}

  5213

  5214 lemma compact_sup_maxdistance:

  5215   fixes s :: "'a::real_normed_vector set"

  5216   assumes "compact s"  "s \<noteq> {}"

  5217   shows "\<exists>x\<in>s. \<exists>y\<in>s. \<forall>u\<in>s. \<forall>v\<in>s. norm(u - v) \<le> norm(x - y)"

  5218 proof-

  5219   have "{x - y | x y . x\<in>s \<and> y\<in>s} \<noteq> {}" using s \<noteq> {} by auto

  5220   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"

  5221     using compact_differences[OF assms(1) assms(1)]

  5222     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

  5223   from x(1) obtain a b where "a\<in>s" "b\<in>s" "x = a - b" by auto

  5224   thus ?thesis using x(2)[unfolded x = a - b] by blast

  5225 qed

  5226

  5227 text {* We can state this in terms of diameter of a set. *}

  5228

  5229 definition "diameter s = (if s = {} then 0::real else Sup {norm(x - y) | x y. x \<in> s \<and> y \<in> s})"

  5230   (* TODO: generalize to class metric_space *)

  5231

  5232 lemma diameter_bounded:

  5233   assumes "bounded s"

  5234   shows "\<forall>x\<in>s. \<forall>y\<in>s. norm(x - y) \<le> diameter s"

  5235         "\<forall>d>0. d < diameter s --> (\<exists>x\<in>s. \<exists>y\<in>s. norm(x - y) > d)"

  5236 proof-

  5237   let ?D = "{norm (x - y) |x y. x \<in> s \<and> y \<in> s}"

  5238   obtain a where a:"\<forall>x\<in>s. norm x \<le> a" using assms[unfolded bounded_iff] by auto

  5239   { fix x y assume "x \<in> s" "y \<in> s"

  5240     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)  }

  5241   note * = this

  5242   { fix x y assume "x\<in>s" "y\<in>s"  hence "s \<noteq> {}" by auto

  5243     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

  5244       by simp (blast del: Sup_upper intro!: * Sup_upper) }

  5245   moreover

  5246   { fix d::real assume "d>0" "d < diameter s"

  5247     hence "s\<noteq>{}" unfolding diameter_def by auto

  5248     have "\<exists>d' \<in> ?D. d' > d"

  5249     proof(rule ccontr)

  5250       assume "\<not> (\<exists>d'\<in>{norm (x - y) |x y. x \<in> s \<and> y \<in> s}. d < d')"

  5251       hence "\<forall>d'\<in>?D. d' \<le> d" by auto (metis not_leE)

  5252       thus False using d < diameter s s\<noteq>{}

  5253         apply (auto simp add: diameter_def)

  5254         apply (drule Sup_real_iff [THEN [2] rev_iffD2])

  5255         apply (auto, force)

  5256         done

  5257     qed

  5258     hence "\<exists>x\<in>s. \<exists>y\<in>s. norm(x - y) > d" by auto  }

  5259   ultimately show "\<forall>x\<in>s. \<forall>y\<in>s. norm(x - y) \<le> diameter s"

  5260         "\<forall>d>0. d < diameter s --> (\<exists>x\<in>s. \<exists>y\<in>s. norm(x - y) > d)" by auto

  5261 qed

  5262

  5263 lemma diameter_bounded_bound:

  5264  "bounded s \<Longrightarrow> x \<in> s \<Longrightarrow> y \<in> s ==> norm(x - y) \<le> diameter s"

  5265   using diameter_bounded by blast

  5266

  5267 lemma diameter_compact_attained:

  5268   fixes s :: "'a::real_normed_vector set"

  5269   assumes "compact s"  "s \<noteq> {}"

  5270   shows "\<exists>x\<in>s. \<exists>y\<in>s. (norm(x - y) = diameter s)"

  5271 proof-

  5272   have b:"bounded s" using assms(1) by (rule compact_imp_bounded)

  5273   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

  5274   hence "diameter s \<le> norm (x - y)"

  5275     unfolding diameter_def by clarsimp (rule Sup_least, fast+)

  5276   thus ?thesis

  5277     by (metis b diameter_bounded_bound order_antisym xys)

  5278 qed

  5279

  5280 text {* Related results with closure as the conclusion. *}

  5281

  5282 lemma closed_scaling:

  5283   fixes s :: "'a::real_normed_vector set"

  5284   assumes "closed s" shows "closed ((\<lambda>x. c *\<^sub>R x)  s)"

  5285 proof(cases "s={}")

  5286   case True thus ?thesis by auto

  5287 next

  5288   case False

  5289   show ?thesis

  5290   proof(cases "c=0")

  5291     have *:"(\<lambda>x. 0)  s = {0}" using s\<noteq>{} by auto

  5292     case True thus ?thesis apply auto unfolding * by auto

  5293   next

  5294     case False

  5295     { fix x l assume as:"\<forall>n::nat. x n \<in> scaleR c  s"  "(x ---> l) sequentially"

  5296       { fix n::nat have "scaleR (1 / c) (x n) \<in> s"

  5297           using as(1)[THEN spec[where x=n]]

  5298           using c\<noteq>0 by auto

  5299       }

  5300       moreover

  5301       { fix e::real assume "e>0"

  5302         hence "0 < e *\<bar>c\<bar>"  using c\<noteq>0 mult_pos_pos[of e "abs c"] by auto

  5303         then obtain N where "\<forall>n\<ge>N. dist (x n) l < e * \<bar>c\<bar>"

  5304           using as(2)[unfolded LIMSEQ_def, THEN spec[where x="e * abs c"]] by auto

  5305         hence "\<exists>N. \<forall>n\<ge>N. dist (scaleR (1 / c) (x n)) (scaleR (1 / c) l) < e"

  5306           unfolding dist_norm unfolding scaleR_right_diff_distrib[THEN sym]

  5307           using mult_imp_div_pos_less[of "abs c" _ e] c\<noteq>0 by auto  }

  5308       hence "((\<lambda>n. scaleR (1 / c) (x n)) ---> scaleR (1 / c) l) sequentially" unfolding LIMSEQ_def by auto

  5309       ultimately have "l \<in> scaleR c  s"

  5310         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"]]

  5311         unfolding image_iff using c\<noteq>0 apply(rule_tac x="scaleR (1 / c) l" in bexI) by auto  }

  5312     thus ?thesis unfolding closed_sequential_limits by fast

  5313   qed

  5314 qed

  5315

  5316 lemma closed_negations:

  5317   fixes s :: "'a::real_normed_vector set"

  5318   assumes "closed s"  shows "closed ((\<lambda>x. -x)  s)"

  5319   using closed_scaling[OF assms, of "- 1"] by simp

  5320

  5321 lemma compact_closed_sums:

  5322   fixes s :: "'a::real_normed_vector set"

  5323   assumes "compact s"  "closed t"  shows "closed {x + y | x y. x \<in> s \<and> y \<in> t}"

  5324 proof-

  5325   let ?S = "{x + y |x y. x \<in> s \<and> y \<in> t}"

  5326   { fix x l assume as:"\<forall>n. x n \<in> ?S"  "(x ---> l) sequentially"

  5327     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"

  5328       using choice[of "\<lambda>n y. x n = (fst y) + (snd y) \<and> fst y \<in> s \<and> snd y \<in> t"] by auto

  5329     obtain l' r where "l'\<in>s" and r:"subseq r" and lr:"(((\<lambda>n. fst (f n)) \<circ> r) ---> l') sequentially"

  5330       using assms(1)[unfolded compact_def, THEN spec[where x="\<lambda> n. fst (f n)"]] using f(2) by auto

  5331     have "((\<lambda>n. snd (f (r n))) ---> l - l') sequentially"

  5332       using tendsto_diff[OF lim_subseq[OF r as(2)] lr] and f(1) unfolding o_def by auto

  5333     hence "l - l' \<in> t"

  5334       using assms(2)[unfolded closed_sequential_limits, THEN spec[where x="\<lambda> n. snd (f (r n))"], THEN spec[where x="l - l'"]]

  5335       using f(3) by auto

  5336     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

  5337   }

  5338   thus ?thesis unfolding closed_sequential_limits by fast

  5339 qed

  5340

  5341 lemma closed_compact_sums:

  5342   fixes s t :: "'a::real_normed_vector set"

  5343   assumes "closed s"  "compact t"

  5344   shows "closed {x + y | x y. x \<in> s \<and> y \<in> t}"

  5345 proof-

  5346   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

  5347     apply(rule_tac x=y in exI) apply auto apply(rule_tac x=y in exI) by auto

  5348   thus ?thesis using compact_closed_sums[OF assms(2,1)] by simp

  5349 qed

  5350

  5351 lemma compact_closed_differences:

  5352   fixes s t :: "'a::real_normed_vector set"

  5353   assumes "compact s"  "closed t"

  5354   shows "closed {x - y | x y. x \<in> s \<and> y \<in> t}"

  5355 proof-

  5356   have "{x + y |x y. x \<in> s \<and> y \<in> uminus  t} =  {x - y |x y. x \<in> s \<and> y \<in> t}"

  5357     apply auto apply(rule_tac x=xa in exI) apply auto apply(rule_tac x=xa in exI) by auto

  5358   thus ?thesis using compact_closed_sums[OF assms(1) closed_negations[OF assms(2)]] by auto

  5359 qed

  5360

  5361 lemma closed_compact_differences:

  5362   fixes s t :: "'a::real_normed_vector set"

  5363   assumes "closed s" "compact t"

  5364   shows "closed {x - y | x y. x \<in> s \<and> y \<in> t}"

  5365 proof-

  5366   have "{x + y |x y. x \<in> s \<and> y \<in> uminus  t} = {x - y |x y. x \<in> s \<and> y \<in> t}"

  5367     apply auto apply(rule_tac x=xa in exI) apply auto apply(rule_tac x=xa in exI) by auto

  5368  thus ?thesis using closed_compact_sums[OF assms(1) compact_negations[OF assms(2)]] by simp

  5369 qed

  5370

  5371 lemma closed_translation:

  5372   fixes a :: "'a::real_normed_vector"

  5373   assumes "closed s"  shows "closed ((\<lambda>x. a + x)  s)"

  5374 proof-

  5375   have "{a + y |y. y \<in> s} = (op + a  s)" by auto

  5376   thus ?thesis using compact_closed_sums[OF compact_sing[of a] assms] by auto

  5377 qed

  5378

  5379 lemma translation_Compl:

  5380   fixes a :: "'a::ab_group_add"

  5381   shows "(\<lambda>x. a + x)  (- t) = - ((\<lambda>x. a + x)  t)"

  5382   apply (auto simp add: image_iff) apply(rule_tac x="x - a" in bexI) by auto

  5383

  5384 lemma translation_UNIV:

  5385   fixes a :: "'a::ab_group_add" shows "range (\<lambda>x. a + x) = UNIV"

  5386   apply (auto simp add: image_iff) apply(rule_tac x="x - a" in exI) by auto

  5387

  5388 lemma translation_diff:

  5389   fixes a :: "'a::ab_group_add"

  5390   shows "(\<lambda>x. a + x)  (s - t) = ((\<lambda>x. a + x)  s) - ((\<lambda>x. a + x)  t)"

  5391   by auto

  5392

  5393 lemma closure_translation:

  5394   fixes a :: "'a::real_normed_vector"

  5395   shows "closure ((\<lambda>x. a + x)  s) = (\<lambda>x. a + x)  (closure s)"

  5396 proof-

  5397   have *:"op + a  (- s) = - op + a  s"

  5398     apply auto unfolding image_iff apply(rule_tac x="x - a" in bexI) by auto

  5399   show ?thesis unfolding closure_interior translation_Compl

  5400     using interior_translation[of a "- s"] unfolding * by auto

  5401 qed

  5402

  5403 lemma frontier_translation:

  5404   fixes a :: "'a::real_normed_vector"

  5405   shows "frontier((\<lambda>x. a + x)  s) = (\<lambda>x. a + x)  (frontier s)"

  5406   unfolding frontier_def translation_diff interior_translation closure_translation by auto

  5407

  5408

  5409 subsection {* Separation between points and sets *}

  5410

  5411 lemma separate_point_closed:

  5412   fixes s :: "'a::heine_borel set"

  5413   shows "closed s \<Longrightarrow> a \<notin> s  ==> (\<exists>d>0. \<forall>x\<in>s. d \<le> dist a x)"

  5414 proof(cases "s = {}")

  5415   case True

  5416   thus ?thesis by(auto intro!: exI[where x=1])

  5417 next

  5418   case False

  5419   assume "closed s" "a \<notin> s"

  5420   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

  5421   with x\<in>s show ?thesis using dist_pos_lt[of a x] anda \<notin> s by blast

  5422 qed

  5423

  5424 lemma separate_compact_closed:

  5425   fixes s t :: "'a::{heine_borel, real_normed_vector} set"

  5426     (* TODO: does this generalize to heine_borel? *)

  5427   assumes "compact s" and "closed t" and "s \<inter> t = {}"

  5428   shows "\<exists>d>0. \<forall>x\<in>s. \<forall>y\<in>t. d \<le> dist x y"

  5429 proof-

  5430   have "0 \<notin> {x - y |x y. x \<in> s \<and> y \<in> t}" using assms(3) by auto

  5431   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"

  5432     using separate_point_closed[OF compact_closed_differences[OF assms(1,2)], of 0] by auto

  5433   { fix x y assume "x\<in>s" "y\<in>t"

  5434     hence "x - y \<in> {x - y |x y. x \<in> s \<and> y \<in> t}" by auto

  5435     hence "d \<le> dist (x - y) 0" using d[THEN bspec[where x="x - y"]] using dist_commute

  5436       by (auto  simp add: dist_commute)

  5437     hence "d \<le> dist x y" unfolding dist_norm by auto  }

  5438   thus ?thesis using d>0 by auto

  5439 qed

  5440

  5441 lemma separate_closed_compact:

  5442   fixes s t :: "'a::{heine_borel, real_normed_vector} set"

  5443   assumes "closed s" and "compact t" and "s \<inter> t = {}"

  5444   shows "\<exists>d>0. \<forall>x\<in>s. \<forall>y\<in>t. d \<le> dist x y"

  5445 proof-

  5446   have *:"t \<inter> s = {}" using assms(3) by auto

  5447   show ?thesis using separate_compact_closed[OF assms(2,1) *]

  5448     apply auto apply(rule_tac x=d in exI) apply auto apply (erule_tac x=y in ballE)

  5449     by (auto simp add: dist_commute)

  5450 qed

  5451

  5452

  5453 subsection {* Intervals *}

  5454

  5455 lemma interval: fixes a :: "'a::ordered_euclidean_space" shows

  5456   "{a <..< b} = {x::'a. \<forall>i\<in>Basis. a\<bullet>i < x\<bullet>i \<and> x\<bullet>i < b\<bullet>i}" and

  5457   "{a .. b} = {x::'a. \<forall>i\<in>Basis. a\<bullet>i \<le> x\<bullet>i \<and> x\<bullet>i \<le> b\<bullet>i}"

  5458   by(auto simp add:set_eq_iff eucl_le[where 'a='a] eucl_less[where 'a='a])

  5459

  5460 lemma mem_interval: fixes a :: "'a::ordered_euclidean_space" shows

  5461   "x \<in> {a<..<b} \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i < x\<bullet>i \<and> x\<bullet>i < b\<bullet>i)"

  5462   "x \<in> {a .. b} \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i \<le> x\<bullet>i \<and> x\<bullet>i \<le> b\<bullet>i)"

  5463   using interval[of a b] by(auto simp add: set_eq_iff eucl_le[where 'a='a] eucl_less[where 'a='a])

  5464

  5465 lemma interval_eq_empty: fixes a :: "'a::ordered_euclidean_space" shows

  5466  "({a <..< b} = {} \<longleftrightarrow> (\<exists>i\<in>Basis. b\<bullet>i \<le> a\<bullet>i))" (is ?th1) and

  5467  "({a  ..  b} = {} \<longleftrightarrow> (\<exists>i\<in>Basis. b\<bullet>i < a\<bullet>i))" (is ?th2)

  5468 proof-

  5469   { fix i x assume i:"i\<in>Basis" and as:"b\<bullet>i \<le> a\<bullet>i" and x:"x\<in>{a <..< b}"

  5470     hence "a \<bullet> i < x \<bullet> i \<and> x \<bullet> i < b \<bullet> i" unfolding mem_interval by auto

  5471     hence "a\<bullet>i < b\<bullet>i" by auto

  5472     hence False using as by auto  }

  5473   moreover

  5474   { assume as:"\<forall>i\<in>Basis. \<not> (b\<bullet>i \<le> a\<bullet>i)"

  5475     let ?x = "(1/2) *\<^sub>R (a + b)"

  5476     { fix i :: 'a assume i:"i\<in>Basis"

  5477       have "a\<bullet>i < b\<bullet>i" using as[THEN bspec[where x=i]] i by auto

  5478       hence "a\<bullet>i < ((1/2) *\<^sub>R (a+b)) \<bullet> i" "((1/2) *\<^sub>R (a+b)) \<bullet> i < b\<bullet>i"

  5479         by (auto simp: inner_add_left) }

  5480     hence "{a <..< b} \<noteq> {}" using mem_interval(1)[of "?x" a b] by auto  }

  5481   ultimately show ?th1 by blast

  5482

  5483   { fix i x assume i:"i\<in>Basis" and as:"b\<bullet>i < a\<bullet>i" and x:"x\<in>{a .. b}"

  5484     hence "a \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> b \<bullet> i" unfolding mem_interval by auto

  5485     hence "a\<bullet>i \<le> b\<bullet>i" by auto

  5486     hence False using as by auto  }

  5487   moreover

  5488   { assume as:"\<forall>i\<in>Basis. \<not> (b\<bullet>i < a\<bullet>i)"

  5489     let ?x = "(1/2) *\<^sub>R (a + b)"

  5490     { fix i :: 'a assume i:"i\<in>Basis"

  5491       have "a\<bullet>i \<le> b\<bullet>i" using as[THEN bspec[where x=i]] i by auto

  5492       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"

  5493         by (auto simp: inner_add_left) }

  5494     hence "{a .. b} \<noteq> {}" using mem_interval(2)[of "?x" a b] by auto  }

  5495   ultimately show ?th2 by blast

  5496 qed

  5497

  5498 lemma interval_ne_empty: fixes a :: "'a::ordered_euclidean_space" shows

  5499   "{a  ..  b} \<noteq> {} \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i)" and

  5500   "{a <..< b} \<noteq> {} \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i)"

  5501   unfolding interval_eq_empty[of a b] by fastforce+

  5502

  5503 lemma interval_sing:

  5504   fixes a :: "'a::ordered_euclidean_space"

  5505   shows "{a .. a} = {a}" and "{a<..<a} = {}"

  5506   unfolding set_eq_iff mem_interval eq_iff [symmetric]

  5507   by (auto intro: euclidean_eqI simp: ex_in_conv)

  5508

  5509 lemma subset_interval_imp: fixes a :: "'a::ordered_euclidean_space" shows

  5510  "(\<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

  5511  "(\<forall>i\<in>Basis. a\<bullet>i < c\<bullet>i \<and> d\<bullet>i < b\<bullet>i) \<Longrightarrow> {c .. d} \<subseteq> {a<..<b}" and

  5512  "(\<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

  5513  "(\<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}"

  5514   unfolding subset_eq[unfolded Ball_def] unfolding mem_interval

  5515   by (best intro: order_trans less_le_trans le_less_trans less_imp_le)+

  5516

  5517 lemma interval_open_subset_closed:

  5518   fixes a :: "'a::ordered_euclidean_space"

  5519   shows "{a<..<b} \<subseteq> {a .. b}"

  5520   unfolding subset_eq [unfolded Ball_def] mem_interval

  5521   by (fast intro: less_imp_le)

  5522

  5523 lemma subset_interval: fixes a :: "'a::ordered_euclidean_space" shows

  5524  "{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

  5525  "{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

  5526  "{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

  5527  "{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)

  5528 proof-

  5529   show ?th1 unfolding subset_eq and Ball_def and mem_interval by (auto intro: order_trans)

  5530   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)

  5531   { assume as: "{c<..<d} \<subseteq> {a .. b}" "\<forall>i\<in>Basis. c\<bullet>i < d\<bullet>i"

  5532     hence "{c<..<d} \<noteq> {}" unfolding interval_eq_empty by auto

  5533     fix i :: 'a assume i:"i\<in>Basis"

  5534     (** TODO combine the following two parts as done in the HOL_light version. **)

  5535     { 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"

  5536       assume as2: "a\<bullet>i > c\<bullet>i"

  5537       { fix j :: 'a assume j:"j\<in>Basis"

  5538         hence "c \<bullet> j < ?x \<bullet> j \<and> ?x \<bullet> j < d \<bullet> j"

  5539           apply(cases "j=i") using as(2)[THEN bspec[where x=j]] i

  5540           by (auto simp add: as2)  }

  5541       hence "?x\<in>{c<..<d}" using i unfolding mem_interval by auto

  5542       moreover

  5543       have "?x\<notin>{a .. b}"

  5544         unfolding mem_interval apply auto apply(rule_tac x=i in bexI)

  5545         using as(2)[THEN bspec[where x=i]] and as2 i

  5546         by auto

  5547       ultimately have False using as by auto  }

  5548     hence "a\<bullet>i \<le> c\<bullet>i" by(rule ccontr)auto

  5549     moreover

  5550     { 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"

  5551       assume as2: "b\<bullet>i < d\<bullet>i"

  5552       { fix j :: 'a assume "j\<in>Basis"

  5553         hence "d \<bullet> j > ?x \<bullet> j \<and> ?x \<bullet> j > c \<bullet> j"

  5554           apply(cases "j=i") using as(2)[THEN bspec[where x=j]]

  5555           by (auto simp add: as2) }

  5556       hence "?x\<in>{c<..<d}" unfolding mem_interval by auto

  5557       moreover

  5558       have "?x\<notin>{a .. b}"

  5559         unfolding mem_interval apply auto apply(rule_tac x=i in bexI)

  5560         using as(2)[THEN bspec[where x=i]] and as2 using i

  5561         by auto

  5562       ultimately have False using as by auto  }

  5563     hence "b\<bullet>i \<ge> d\<bullet>i" by(rule ccontr)auto

  5564     ultimately

  5565     have "a\<bullet>i \<le> c\<bullet>i \<and> d\<bullet>i \<le> b\<bullet>i" by auto

  5566   } note part1 = this

  5567   show ?th3

  5568     unfolding subset_eq and Ball_def and mem_interval

  5569     apply(rule,rule,rule,rule)

  5570     apply(rule part1)

  5571     unfolding subset_eq and Ball_def and mem_interval

  5572     prefer 4

  5573     apply auto

  5574     by(erule_tac x=xa in allE,erule_tac x=xa in allE,fastforce)+

  5575   { assume as:"{c<..<d} \<subseteq> {a<..<b}" "\<forall>i\<in>Basis. c\<bullet>i < d\<bullet>i"

  5576     fix i :: 'a assume i:"i\<in>Basis"

  5577     from as(1) have "{c<..<d} \<subseteq> {a..b}" using interval_open_subset_closed[of a b] by auto

  5578     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

  5579   show ?th4 unfolding subset_eq and Ball_def and mem_interval

  5580     apply(rule,rule,rule,rule) apply(rule *) unfolding subset_eq and Ball_def and mem_interval prefer 4

  5581     apply auto by(erule_tac x=xa in allE, simp)+

  5582 qed

  5583

  5584 lemma inter_interval: fixes a :: "'a::ordered_euclidean_space" shows

  5585  "{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)}"

  5586   unfolding set_eq_iff and Int_iff and mem_interval by auto

  5587

  5588 lemma disjoint_interval: fixes a::"'a::ordered_euclidean_space" shows

  5589   "{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

  5590   "{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

  5591   "{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

  5592   "{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)

  5593 proof-

  5594   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"

  5595   have **: "\<And>P Q. (\<And>i :: 'a. i \<in> Basis \<Longrightarrow> Q ?z i \<Longrightarrow> P i) \<Longrightarrow>

  5596       (\<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)"

  5597     by blast

  5598   note * = set_eq_iff Int_iff empty_iff mem_interval ball_conj_distrib[symmetric] eq_False ball_simps(10)

  5599   show ?th1 unfolding * by (intro **) auto

  5600   show ?th2 unfolding * by (intro **) auto

  5601   show ?th3 unfolding * by (intro **) auto

  5602   show ?th4 unfolding * by (intro **) auto

  5603 qed

  5604

  5605 (* Moved interval_open_subset_closed a bit upwards *)

  5606

  5607 lemma open_interval[intro]:

  5608   fixes a b :: "'a::ordered_euclidean_space" shows "open {a<..<b}"

  5609 proof-

  5610   have "open (\<Inter>i\<in>Basis. (\<lambda>x. x\<bullet>i) - {a\<bullet>i<..<b\<bullet>i})"

  5611     by (intro open_INT finite_lessThan ballI continuous_open_vimage allI

  5612       linear_continuous_at open_real_greaterThanLessThan finite_Basis bounded_linear_inner_left)

  5613   also have "(\<Inter>i\<in>Basis. (\<lambda>x. x\<bullet>i) - {a\<bullet>i<..<b\<bullet>i}) = {a<..<b}"

  5614     by (auto simp add: eucl_less [where 'a='a])

  5615   finally show "open {a<..<b}" .

  5616 qed

  5617

  5618 lemma closed_interval[intro]:

  5619   fixes a b :: "'a::ordered_euclidean_space" shows "closed {a .. b}"

  5620 proof-

  5621   have "closed (\<Inter>i\<in>Basis. (\<lambda>x. x\<bullet>i) - {a\<bullet>i .. b\<bullet>i})"

  5622     by (intro closed_INT ballI continuous_closed_vimage allI

  5623       linear_continuous_at closed_real_atLeastAtMost finite_Basis bounded_linear_inner_left)

  5624   also have "(\<Inter>i\<in>Basis. (\<lambda>x. x\<bullet>i) - {a\<bullet>i .. b\<bullet>i}) = {a .. b}"

  5625     by (auto simp add: eucl_le [where 'a='a])

  5626   finally show "closed {a .. b}" .

  5627 qed

  5628

  5629 lemma interior_closed_interval [intro]:

  5630   fixes a b :: "'a::ordered_euclidean_space"

  5631   shows "interior {a..b} = {a<..<b}" (is "?L = ?R")

  5632 proof(rule subset_antisym)

  5633   show "?R \<subseteq> ?L" using interval_open_subset_closed open_interval

  5634     by (rule interior_maximal)

  5635 next

  5636   { fix x assume "x \<in> interior {a..b}"

  5637     then obtain s where s:"open s" "x \<in> s" "s \<subseteq> {a..b}" ..

  5638     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

  5639     { fix i :: 'a assume i:"i\<in>Basis"

  5640       have "dist (x - (e / 2) *\<^sub>R i) x < e"

  5641            "dist (x + (e / 2) *\<^sub>R i) x < e"

  5642         unfolding dist_norm apply auto

  5643         unfolding norm_minus_cancel using norm_Basis[OF i] e>0 by auto

  5644       hence "a \<bullet> i \<le> (x - (e / 2) *\<^sub>R i) \<bullet> i"

  5645                      "(x + (e / 2) *\<^sub>R i) \<bullet> i \<le> b \<bullet> i"

  5646         using e[THEN spec[where x="x - (e/2) *\<^sub>R i"]]

  5647         and   e[THEN spec[where x="x + (e/2) *\<^sub>R i"]]

  5648         unfolding mem_interval using i by blast+

  5649       hence "a \<bullet> i < x \<bullet> i" and "x \<bullet> i < b \<bullet> i"

  5650         using e>0 i by (auto simp: inner_diff_left inner_Basis inner_add_left) }

  5651     hence "x \<in> {a<..<b}" unfolding mem_interval by auto  }

  5652   thus "?L \<subseteq> ?R" ..

  5653 qed

  5654

  5655 lemma bounded_closed_interval: fixes a :: "'a::ordered_euclidean_space" shows "bounded {a .. b}"

  5656 proof-

  5657   let ?b = "\<Sum>i\<in>Basis. \<bar>a\<bullet>i\<bar> + \<bar>b\<bullet>i\<bar>"

  5658   { fix x::"'a" assume x:"\<forall>i\<in>Basis. a \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> b \<bullet> i"

  5659     { fix i :: 'a assume "i\<in>Basis"

  5660       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  }

  5661     hence "(\<Sum>i\<in>Basis. \<bar>x \<bullet> i\<bar>) \<le> ?b" apply-apply(rule setsum_mono) by auto

  5662     hence "norm x \<le> ?b" using norm_le_l1[of x] by auto  }

  5663   thus ?thesis unfolding interval and bounded_iff by auto

  5664 qed

  5665

  5666 lemma bounded_interval: fixes a :: "'a::ordered_euclidean_space" shows

  5667  "bounded {a .. b} \<and> bounded {a<..<b}"

  5668   using bounded_closed_interval[of a b]

  5669   using interval_open_subset_closed[of a b]

  5670   using bounded_subset[of "{a..b}" "{a<..<b}"]

  5671   by simp

  5672

  5673 lemma not_interval_univ: fixes a :: "'a::ordered_euclidean_space" shows

  5674  "({a .. b} \<noteq> UNIV) \<and> ({a<..<b} \<noteq> UNIV)"

  5675   using bounded_interval[of a b] by auto

  5676

  5677 lemma compact_interval: fixes a :: "'a::ordered_euclidean_space" shows "compact {a .. b}"

  5678   using bounded_closed_imp_seq_compact[of "{a..b}"] using bounded_interval[of a b]

  5679   by (auto simp: compact_eq_seq_compact_metric)

  5680

  5681 lemma open_interval_midpoint: fixes a :: "'a::ordered_euclidean_space"

  5682   assumes "{a<..<b} \<noteq> {}" shows "((1/2) *\<^sub>R (a + b)) \<in> {a<..<b}"

  5683 proof-

  5684   { fix i :: 'a assume "i\<in>Basis"

  5685     hence "a \<bullet> i < ((1 / 2) *\<^sub>R (a + b)) \<bullet> i \<and> ((1 / 2) *\<^sub>R (a + b)) \<bullet> i < b \<bullet> i"

  5686       using assms[unfolded interval_ne_empty, THEN bspec[where x=i]] by (auto simp: inner_add_left)  }

  5687   thus ?thesis unfolding mem_interval by auto

  5688 qed

  5689

  5690 lemma open_closed_interval_convex: fixes x :: "'a::ordered_euclidean_space"

  5691   assumes x:"x \<in> {a<..<b}" and y:"y \<in> {a .. b}" and e:"0 < e" "e \<le> 1"

  5692   shows "(e *\<^sub>R x + (1 - e) *\<^sub>R y) \<in> {a<..<b}"

  5693 proof-

  5694   { fix i :: 'a assume i:"i\<in>Basis"

  5695     have "a \<bullet> i = e * (a \<bullet> i) + (1 - e) * (a \<bullet> i)" unfolding left_diff_distrib by simp

  5696     also have "\<dots> < e * (x \<bullet> i) + (1 - e) * (y \<bullet> i)" apply(rule add_less_le_mono)

  5697       using e unfolding mult_less_cancel_left and mult_le_cancel_left apply simp_all

  5698       using x unfolding mem_interval using i apply simp

  5699       using y unfolding mem_interval using i apply simp

  5700       done

  5701     finally have "a \<bullet> i < (e *\<^sub>R x + (1 - e) *\<^sub>R y) \<bullet> i" unfolding inner_simps by auto

  5702     moreover {

  5703     have "b \<bullet> i = e * (b\<bullet>i) + (1 - e) * (b\<bullet>i)" unfolding left_diff_distrib by simp

  5704     also have "\<dots> > e * (x \<bullet> i) + (1 - e) * (y \<bullet> i)" apply(rule add_less_le_mono)

  5705       using e unfolding mult_less_cancel_left and mult_le_cancel_left apply simp_all

  5706       using x unfolding mem_interval using i apply simp

  5707       using y unfolding mem_interval using i apply simp

  5708       done

  5709     finally have "(e *\<^sub>R x + (1 - e) *\<^sub>R y) \<bullet> i < b \<bullet> i" unfolding inner_simps by auto

  5710     } ultimately have "a \<bullet> i < (e *\<^sub>R x + (1 - e) *\<^sub>R y) \<bullet> i \<and> (e *\<^sub>R x + (1 - e) *\<^sub>R y) \<bullet> i < b \<bullet> i" by auto }

  5711   thus ?thesis unfolding mem_in