1(*  Title:      HOL/Topological_Spaces.thy
2    Author:     Brian Huffman
3    Author:     Johannes H��lzl
4*)
5
6section \<open>Topological Spaces\<close>
7
8theory Topological_Spaces
9  imports Main
10begin
11
12named_theorems continuous_intros "structural introduction rules for continuity"
13
14subsection \<open>Topological space\<close>
15
16class "open" =
17  fixes "open" :: "'a set \<Rightarrow> bool"
18
19class topological_space = "open" +
20  assumes open_UNIV [simp, intro]: "open UNIV"
21  assumes open_Int [intro]: "open S \<Longrightarrow> open T \<Longrightarrow> open (S \<inter> T)"
22  assumes open_Union [intro]: "\<forall>S\<in>K. open S \<Longrightarrow> open (\<Union>K)"
23begin
24
25definition closed :: "'a set \<Rightarrow> bool"
26  where "closed S \<longleftrightarrow> open (- S)"
27
28lemma open_empty [continuous_intros, intro, simp]: "open {}"
29  using open_Union [of "{}"] by simp
30
31lemma open_Un [continuous_intros, intro]: "open S \<Longrightarrow> open T \<Longrightarrow> open (S \<union> T)"
32  using open_Union [of "{S, T}"] by simp
33
34lemma open_UN [continuous_intros, intro]: "\<forall>x\<in>A. open (B x) \<Longrightarrow> open (\<Union>x\<in>A. B x)"
35  using open_Union [of "B ` A"] by simp
36
37lemma open_Inter [continuous_intros, intro]: "finite S \<Longrightarrow> \<forall>T\<in>S. open T \<Longrightarrow> open (\<Inter>S)"
38  by (induct set: finite) auto
39
40lemma open_INT [continuous_intros, intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. open (B x) \<Longrightarrow> open (\<Inter>x\<in>A. B x)"
41  using open_Inter [of "B ` A"] by simp
42
43lemma openI:
44  assumes "\<And>x. x \<in> S \<Longrightarrow> \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S"
45  shows "open S"
46proof -
47  have "open (\<Union>{T. open T \<and> T \<subseteq> S})" by auto
48  moreover have "\<Union>{T. open T \<and> T \<subseteq> S} = S" by (auto dest!: assms)
49  ultimately show "open S" by simp
50qed
51
52lemma open_subopen: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S)"
53by (auto intro: openI)
54
55lemma closed_empty [continuous_intros, intro, simp]: "closed {}"
56  unfolding closed_def by simp
57
58lemma closed_Un [continuous_intros, intro]: "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<union> T)"
59  unfolding closed_def by auto
60
61lemma closed_UNIV [continuous_intros, intro, simp]: "closed UNIV"
62  unfolding closed_def by simp
63
64lemma closed_Int [continuous_intros, intro]: "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<inter> T)"
65  unfolding closed_def by auto
66
67lemma closed_INT [continuous_intros, intro]: "\<forall>x\<in>A. closed (B x) \<Longrightarrow> closed (\<Inter>x\<in>A. B x)"
68  unfolding closed_def by auto
69
70lemma closed_Inter [continuous_intros, intro]: "\<forall>S\<in>K. closed S \<Longrightarrow> closed (\<Inter>K)"
71  unfolding closed_def uminus_Inf by auto
72
73lemma closed_Union [continuous_intros, intro]: "finite S \<Longrightarrow> \<forall>T\<in>S. closed T \<Longrightarrow> closed (\<Union>S)"
74  by (induct set: finite) auto
75
76lemma closed_UN [continuous_intros, intro]:
77  "finite A \<Longrightarrow> \<forall>x\<in>A. closed (B x) \<Longrightarrow> closed (\<Union>x\<in>A. B x)"
78  using closed_Union [of "B ` A"] by simp
79
80lemma open_closed: "open S \<longleftrightarrow> closed (- S)"
81  by (simp add: closed_def)
82
83lemma closed_open: "closed S \<longleftrightarrow> open (- S)"
84  by (rule closed_def)
85
86lemma open_Diff [continuous_intros, intro]: "open S \<Longrightarrow> closed T \<Longrightarrow> open (S - T)"
87  by (simp add: closed_open Diff_eq open_Int)
88
89lemma closed_Diff [continuous_intros, intro]: "closed S \<Longrightarrow> open T \<Longrightarrow> closed (S - T)"
90  by (simp add: open_closed Diff_eq closed_Int)
91
92lemma open_Compl [continuous_intros, intro]: "closed S \<Longrightarrow> open (- S)"
93  by (simp add: closed_open)
94
95lemma closed_Compl [continuous_intros, intro]: "open S \<Longrightarrow> closed (- S)"
96  by (simp add: open_closed)
97
98lemma open_Collect_neg: "closed {x. P x} \<Longrightarrow> open {x. \<not> P x}"
99  unfolding Collect_neg_eq by (rule open_Compl)
100
101lemma open_Collect_conj:
102  assumes "open {x. P x}" "open {x. Q x}"
103  shows "open {x. P x \<and> Q x}"
104  using open_Int[OF assms] by (simp add: Int_def)
105
106lemma open_Collect_disj:
107  assumes "open {x. P x}" "open {x. Q x}"
108  shows "open {x. P x \<or> Q x}"
109  using open_Un[OF assms] by (simp add: Un_def)
110
111lemma open_Collect_ex: "(\<And>i. open {x. P i x}) \<Longrightarrow> open {x. \<exists>i. P i x}"
112  using open_UN[of UNIV "\<lambda>i. {x. P i x}"] unfolding Collect_ex_eq by simp
113
114lemma open_Collect_imp: "closed {x. P x} \<Longrightarrow> open {x. Q x} \<Longrightarrow> open {x. P x \<longrightarrow> Q x}"
115  unfolding imp_conv_disj by (intro open_Collect_disj open_Collect_neg)
116
117lemma open_Collect_const: "open {x. P}"
118  by (cases P) auto
119
120lemma closed_Collect_neg: "open {x. P x} \<Longrightarrow> closed {x. \<not> P x}"
121  unfolding Collect_neg_eq by (rule closed_Compl)
122
123lemma closed_Collect_conj:
124  assumes "closed {x. P x}" "closed {x. Q x}"
125  shows "closed {x. P x \<and> Q x}"
126  using closed_Int[OF assms] by (simp add: Int_def)
127
128lemma closed_Collect_disj:
129  assumes "closed {x. P x}" "closed {x. Q x}"
130  shows "closed {x. P x \<or> Q x}"
131  using closed_Un[OF assms] by (simp add: Un_def)
132
133lemma closed_Collect_all: "(\<And>i. closed {x. P i x}) \<Longrightarrow> closed {x. \<forall>i. P i x}"
134  using closed_INT[of UNIV "\<lambda>i. {x. P i x}"] by (simp add: Collect_all_eq)
135
136lemma closed_Collect_imp: "open {x. P x} \<Longrightarrow> closed {x. Q x} \<Longrightarrow> closed {x. P x \<longrightarrow> Q x}"
137  unfolding imp_conv_disj by (intro closed_Collect_disj closed_Collect_neg)
138
139lemma closed_Collect_const: "closed {x. P}"
140  by (cases P) auto
141
142end
143
144
145subsection \<open>Hausdorff and other separation properties\<close>
146
147class t0_space = topological_space +
148  assumes t0_space: "x \<noteq> y \<Longrightarrow> \<exists>U. open U \<and> \<not> (x \<in> U \<longleftrightarrow> y \<in> U)"
149
150class t1_space = topological_space +
151  assumes t1_space: "x \<noteq> y \<Longrightarrow> \<exists>U. open U \<and> x \<in> U \<and> y \<notin> U"
152
153instance t1_space \<subseteq> t0_space
154  by standard (fast dest: t1_space)
155
156context t1_space begin
157
158lemma separation_t1: "x \<noteq> y \<longleftrightarrow> (\<exists>U. open U \<and> x \<in> U \<and> y \<notin> U)"
159  using t1_space[of x y] by blast
160
161lemma closed_singleton [iff]: "closed {a}"
162proof -
163  let ?T = "\<Union>{S. open S \<and> a \<notin> S}"
164  have "open ?T"
165    by (simp add: open_Union)
166  also have "?T = - {a}"
167    by (auto simp add: set_eq_iff separation_t1)
168  finally show "closed {a}"
169    by (simp only: closed_def)
170qed
171
172lemma closed_insert [continuous_intros, simp]:
173  assumes "closed S"
174  shows "closed (insert a S)"
175proof -
176  from closed_singleton assms have "closed ({a} \<union> S)"
177    by (rule closed_Un)
178  then show "closed (insert a S)"
179    by simp
180qed
181
182lemma finite_imp_closed: "finite S \<Longrightarrow> closed S"
183  by (induct pred: finite) simp_all
184
185end
186
187text \<open>T2 spaces are also known as Hausdorff spaces.\<close>
188
189class t2_space = topological_space +
190  assumes hausdorff: "x \<noteq> y \<Longrightarrow> \<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
191
192instance t2_space \<subseteq> t1_space
193  by standard (fast dest: hausdorff)
194
195lemma (in t2_space) separation_t2: "x \<noteq> y \<longleftrightarrow> (\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {})"
196  using hausdorff [of x y] by blast
197
198lemma (in t0_space) separation_t0: "x \<noteq> y \<longleftrightarrow> (\<exists>U. open U \<and> \<not> (x \<in> U \<longleftrightarrow> y \<in> U))"
199  using t0_space [of x y] by blast
200
201
202text \<open>A classical separation axiom for topological space, the T3 axiom -- also called regularity:
203if a point is not in a closed set, then there are open sets separating them.\<close>
204
205class t3_space = t2_space +
206  assumes t3_space: "closed S \<Longrightarrow> y \<notin> S \<Longrightarrow> \<exists>U V. open U \<and> open V \<and> y \<in> U \<and> S \<subseteq> V \<and> U \<inter> V = {}"
207
208text \<open>A classical separation axiom for topological space, the T4 axiom -- also called normality:
209if two closed sets are disjoint, then there are open sets separating them.\<close>
210
211class t4_space = t2_space +
212  assumes t4_space: "closed S \<Longrightarrow> closed T \<Longrightarrow> S \<inter> T = {} \<Longrightarrow> \<exists>U V. open U \<and> open V \<and> S \<subseteq> U \<and> T \<subseteq> V \<and> U \<inter> V = {}"
213
214text \<open>T4 is stronger than T3, and weaker than metric.\<close>
215
216instance t4_space \<subseteq> t3_space
217proof
218  fix S and y::'a assume "closed S" "y \<notin> S"
219  then show "\<exists>U V. open U \<and> open V \<and> y \<in> U \<and> S \<subseteq> V \<and> U \<inter> V = {}"
220    using t4_space[of "{y}" S] by auto
221qed
222
223text \<open>A perfect space is a topological space with no isolated points.\<close>
224
225class perfect_space = topological_space +
226  assumes not_open_singleton: "\<not> open {x}"
227
228lemma (in perfect_space) UNIV_not_singleton: "UNIV \<noteq> {x}"
229  for x::'a
230  by (metis (no_types) open_UNIV not_open_singleton)
231
232
233subsection \<open>Generators for toplogies\<close>
234
235inductive generate_topology :: "'a set set \<Rightarrow> 'a set \<Rightarrow> bool" for S :: "'a set set"
236  where
237    UNIV: "generate_topology S UNIV"
238  | Int: "generate_topology S (a \<inter> b)" if "generate_topology S a" and "generate_topology S b"
239  | UN: "generate_topology S (\<Union>K)" if "(\<And>k. k \<in> K \<Longrightarrow> generate_topology S k)"
240  | Basis: "generate_topology S s" if "s \<in> S"
241
242hide_fact (open) UNIV Int UN Basis
243
244lemma generate_topology_Union:
245  "(\<And>k. k \<in> I \<Longrightarrow> generate_topology S (K k)) \<Longrightarrow> generate_topology S (\<Union>k\<in>I. K k)"
246  using generate_topology.UN [of "K ` I"] by auto
247
248lemma topological_space_generate_topology: "class.topological_space (generate_topology S)"
249  by standard (auto intro: generate_topology.intros)
250
251
252subsection \<open>Order topologies\<close>
253
254class order_topology = order + "open" +
255  assumes open_generated_order: "open = generate_topology (range (\<lambda>a. {..< a}) \<union> range (\<lambda>a. {a <..}))"
256begin
257
258subclass topological_space
259  unfolding open_generated_order
260  by (rule topological_space_generate_topology)
261
262lemma open_greaterThan [continuous_intros, simp]: "open {a <..}"
263  unfolding open_generated_order by (auto intro: generate_topology.Basis)
264
265lemma open_lessThan [continuous_intros, simp]: "open {..< a}"
266  unfolding open_generated_order by (auto intro: generate_topology.Basis)
267
268lemma open_greaterThanLessThan [continuous_intros, simp]: "open {a <..< b}"
269   unfolding greaterThanLessThan_eq by (simp add: open_Int)
270
271end
272
273class linorder_topology = linorder + order_topology
274
275lemma closed_atMost [continuous_intros, simp]: "closed {..a}"
276  for a :: "'a::linorder_topology"
277  by (simp add: closed_open)
278
279lemma closed_atLeast [continuous_intros, simp]: "closed {a..}"
280  for a :: "'a::linorder_topology"
281  by (simp add: closed_open)
282
283lemma closed_atLeastAtMost [continuous_intros, simp]: "closed {a..b}"
284  for a b :: "'a::linorder_topology"
285proof -
286  have "{a .. b} = {a ..} \<inter> {.. b}"
287    by auto
288  then show ?thesis
289    by (simp add: closed_Int)
290qed
291
292lemma (in order) less_separate:
293  assumes "x < y"
294  shows "\<exists>a b. x \<in> {..< a} \<and> y \<in> {b <..} \<and> {..< a} \<inter> {b <..} = {}"
295proof (cases "\<exists>z. x < z \<and> z < y")
296  case True
297  then obtain z where "x < z \<and> z < y" ..
298  then have "x \<in> {..< z} \<and> y \<in> {z <..} \<and> {z <..} \<inter> {..< z} = {}"
299    by auto
300  then show ?thesis by blast
301next
302  case False
303  with \<open>x < y\<close> have "x \<in> {..< y}" "y \<in> {x <..}" "{x <..} \<inter> {..< y} = {}"
304    by auto
305  then show ?thesis by blast
306qed
307
308instance linorder_topology \<subseteq> t2_space
309proof
310  fix x y :: 'a
311  show "x \<noteq> y \<Longrightarrow> \<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
312    using less_separate [of x y] less_separate [of y x]
313    by (elim neqE; metis open_lessThan open_greaterThan Int_commute)
314qed
315
316lemma (in linorder_topology) open_right:
317  assumes "open S" "x \<in> S"
318    and gt_ex: "x < y"
319  shows "\<exists>b>x. {x ..< b} \<subseteq> S"
320  using assms unfolding open_generated_order
321proof induct
322  case UNIV
323  then show ?case by blast
324next
325  case (Int A B)
326  then obtain a b where "a > x" "{x ..< a} \<subseteq> A"  "b > x" "{x ..< b} \<subseteq> B"
327    by auto
328  then show ?case
329    by (auto intro!: exI[of _ "min a b"])
330next
331  case UN
332  then show ?case by blast
333next
334  case Basis
335  then show ?case
336    by (fastforce intro: exI[of _ y] gt_ex)
337qed
338
339lemma (in linorder_topology) open_left:
340  assumes "open S" "x \<in> S"
341    and lt_ex: "y < x"
342  shows "\<exists>b<x. {b <.. x} \<subseteq> S"
343  using assms unfolding open_generated_order
344proof induction
345  case UNIV
346  then show ?case by blast
347next
348  case (Int A B)
349  then obtain a b where "a < x" "{a <.. x} \<subseteq> A"  "b < x" "{b <.. x} \<subseteq> B"
350    by auto
351  then show ?case
352    by (auto intro!: exI[of _ "max a b"])
353next
354  case UN
355  then show ?case by blast
356next
357  case Basis
358  then show ?case
359    by (fastforce intro: exI[of _ y] lt_ex)
360qed
361
362
363subsection \<open>Setup some topologies\<close>
364
365subsubsection \<open>Boolean is an order topology\<close>
366
367class discrete_topology = topological_space +
368  assumes open_discrete: "\<And>A. open A"
369
370instance discrete_topology < t2_space
371proof
372  fix x y :: 'a
373  assume "x \<noteq> y"
374  then show "\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
375    by (intro exI[of _ "{_}"]) (auto intro!: open_discrete)
376qed
377
378instantiation bool :: linorder_topology
379begin
380
381definition open_bool :: "bool set \<Rightarrow> bool"
382  where "open_bool = generate_topology (range (\<lambda>a. {..< a}) \<union> range (\<lambda>a. {a <..}))"
383
384instance
385  by standard (rule open_bool_def)
386
387end
388
389instance bool :: discrete_topology
390proof
391  fix A :: "bool set"
392  have *: "{False <..} = {True}" "{..< True} = {False}"
393    by auto
394  have "A = UNIV \<or> A = {} \<or> A = {False <..} \<or> A = {..< True}"
395    using subset_UNIV[of A] unfolding UNIV_bool * by blast
396  then show "open A"
397    by auto
398qed
399
400instantiation nat :: linorder_topology
401begin
402
403definition open_nat :: "nat set \<Rightarrow> bool"
404  where "open_nat = generate_topology (range (\<lambda>a. {..< a}) \<union> range (\<lambda>a. {a <..}))"
405
406instance
407  by standard (rule open_nat_def)
408
409end
410
411instance nat :: discrete_topology
412proof
413  fix A :: "nat set"
414  have "open {n}" for n :: nat
415  proof (cases n)
416    case 0
417    moreover have "{0} = {..<1::nat}"
418      by auto
419    ultimately show ?thesis
420       by auto
421  next
422    case (Suc n')
423    then have "{n} = {..<Suc n} \<inter> {n' <..}"
424      by auto
425    with Suc show ?thesis
426      by (auto intro: open_lessThan open_greaterThan)
427  qed
428  then have "open (\<Union>a\<in>A. {a})"
429    by (intro open_UN) auto
430  then show "open A"
431    by simp
432qed
433
434instantiation int :: linorder_topology
435begin
436
437definition open_int :: "int set \<Rightarrow> bool"
438  where "open_int = generate_topology (range (\<lambda>a. {..< a}) \<union> range (\<lambda>a. {a <..}))"
439
440instance
441  by standard (rule open_int_def)
442
443end
444
445instance int :: discrete_topology
446proof
447  fix A :: "int set"
448  have "{..<i + 1} \<inter> {i-1 <..} = {i}" for i :: int
449    by auto
450  then have "open {i}" for i :: int
451    using open_Int[OF open_lessThan[of "i + 1"] open_greaterThan[of "i - 1"]] by auto
452  then have "open (\<Union>a\<in>A. {a})"
453    by (intro open_UN) auto
454  then show "open A"
455    by simp
456qed
457
458
459subsubsection \<open>Topological filters\<close>
460
461definition (in topological_space) nhds :: "'a \<Rightarrow> 'a filter"
462  where "nhds a = (INF S\<in>{S. open S \<and> a \<in> S}. principal S)"
463
464definition (in topological_space) at_within :: "'a \<Rightarrow> 'a set \<Rightarrow> 'a filter"
465    ("at (_)/ within (_)" [1000, 60] 60)
466  where "at a within s = inf (nhds a) (principal (s - {a}))"
467
468abbreviation (in topological_space) at :: "'a \<Rightarrow> 'a filter"  ("at")
469  where "at x \<equiv> at x within (CONST UNIV)"
470
471abbreviation (in order_topology) at_right :: "'a \<Rightarrow> 'a filter"
472  where "at_right x \<equiv> at x within {x <..}"
473
474abbreviation (in order_topology) at_left :: "'a \<Rightarrow> 'a filter"
475  where "at_left x \<equiv> at x within {..< x}"
476
477lemma (in topological_space) nhds_generated_topology:
478  "open = generate_topology T \<Longrightarrow> nhds x = (INF S\<in>{S\<in>T. x \<in> S}. principal S)"
479  unfolding nhds_def
480proof (safe intro!: antisym INF_greatest)
481  fix S
482  assume "generate_topology T S" "x \<in> S"
483  then show "(INF S\<in>{S \<in> T. x \<in> S}. principal S) \<le> principal S"
484    by induct
485      (auto intro: INF_lower order_trans simp: inf_principal[symmetric] simp del: inf_principal)
486qed (auto intro!: INF_lower intro: generate_topology.intros)
487
488lemma (in topological_space) eventually_nhds:
489  "eventually P (nhds a) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x))"
490  unfolding nhds_def by (subst eventually_INF_base) (auto simp: eventually_principal)
491
492lemma eventually_eventually:
493  "eventually (\<lambda>y. eventually P (nhds y)) (nhds x) = eventually P (nhds x)"
494  by (auto simp: eventually_nhds)
495
496lemma (in topological_space) eventually_nhds_in_open:
497  "open s \<Longrightarrow> x \<in> s \<Longrightarrow> eventually (\<lambda>y. y \<in> s) (nhds x)"
498  by (subst eventually_nhds) blast
499
500lemma (in topological_space) eventually_nhds_x_imp_x: "eventually P (nhds x) \<Longrightarrow> P x"
501  by (subst (asm) eventually_nhds) blast
502
503lemma (in topological_space) nhds_neq_bot [simp]: "nhds a \<noteq> bot"
504  by (simp add: trivial_limit_def eventually_nhds)
505
506lemma (in t1_space) t1_space_nhds: "x \<noteq> y \<Longrightarrow> (\<forall>\<^sub>F x in nhds x. x \<noteq> y)"
507  by (drule t1_space) (auto simp: eventually_nhds)
508
509lemma (in topological_space) nhds_discrete_open: "open {x} \<Longrightarrow> nhds x = principal {x}"
510  by (auto simp: nhds_def intro!: antisym INF_greatest INF_lower2[of "{x}"])
511
512lemma (in discrete_topology) nhds_discrete: "nhds x = principal {x}"
513  by (simp add: nhds_discrete_open open_discrete)
514
515lemma (in discrete_topology) at_discrete: "at x within S = bot"
516  unfolding at_within_def nhds_discrete by simp
517
518lemma (in discrete_topology) tendsto_discrete:
519  "filterlim (f :: 'b \<Rightarrow> 'a) (nhds y) F \<longleftrightarrow> eventually (\<lambda>x. f x = y) F"
520  by (auto simp: nhds_discrete filterlim_principal)
521
522lemma (in topological_space) at_within_eq:
523  "at x within s = (INF S\<in>{S. open S \<and> x \<in> S}. principal (S \<inter> s - {x}))"
524  unfolding nhds_def at_within_def
525  by (subst INF_inf_const2[symmetric]) (auto simp: Diff_Int_distrib)
526
527lemma (in topological_space) eventually_at_filter:
528  "eventually P (at a within s) \<longleftrightarrow> eventually (\<lambda>x. x \<noteq> a \<longrightarrow> x \<in> s \<longrightarrow> P x) (nhds a)"
529  by (simp add: at_within_def eventually_inf_principal imp_conjL[symmetric] conj_commute)
530
531lemma (in topological_space) at_le: "s \<subseteq> t \<Longrightarrow> at x within s \<le> at x within t"
532  unfolding at_within_def by (intro inf_mono) auto
533
534lemma (in topological_space) eventually_at_topological:
535  "eventually P (at a within s) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. x \<noteq> a \<longrightarrow> x \<in> s \<longrightarrow> P x))"
536  by (simp add: eventually_nhds eventually_at_filter)
537
538lemma (in topological_space) at_within_open: "a \<in> S \<Longrightarrow> open S \<Longrightarrow> at a within S = at a"
539  unfolding filter_eq_iff eventually_at_topological by (metis open_Int Int_iff UNIV_I)
540
541lemma (in topological_space) at_within_open_NO_MATCH:
542  "a \<in> s \<Longrightarrow> open s \<Longrightarrow> NO_MATCH UNIV s \<Longrightarrow> at a within s = at a"
543  by (simp only: at_within_open)
544
545lemma (in topological_space) at_within_open_subset:
546  "a \<in> S \<Longrightarrow> open S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> at a within T = at a"
547  by (metis at_le at_within_open dual_order.antisym subset_UNIV)
548
549lemma (in topological_space) at_within_nhd:
550  assumes "x \<in> S" "open S" "T \<inter> S - {x} = U \<inter> S - {x}"
551  shows "at x within T = at x within U"
552  unfolding filter_eq_iff eventually_at_filter
553proof (intro allI eventually_subst)
554  have "eventually (\<lambda>x. x \<in> S) (nhds x)"
555    using \<open>x \<in> S\<close> \<open>open S\<close> by (auto simp: eventually_nhds)
556  then show "\<forall>\<^sub>F n in nhds x. (n \<noteq> x \<longrightarrow> n \<in> T \<longrightarrow> P n) = (n \<noteq> x \<longrightarrow> n \<in> U \<longrightarrow> P n)" for P
557    by eventually_elim (insert \<open>T \<inter> S - {x} = U \<inter> S - {x}\<close>, blast)
558qed
559
560lemma (in topological_space) at_within_empty [simp]: "at a within {} = bot"
561  unfolding at_within_def by simp
562
563lemma (in topological_space) at_within_union:
564  "at x within (S \<union> T) = sup (at x within S) (at x within T)"
565  unfolding filter_eq_iff eventually_sup eventually_at_filter
566  by (auto elim!: eventually_rev_mp)
567
568lemma (in topological_space) at_eq_bot_iff: "at a = bot \<longleftrightarrow> open {a}"
569  unfolding trivial_limit_def eventually_at_topological
570  apply safe
571   apply (case_tac "S = {a}")
572    apply simp
573   apply fast
574  apply fast
575  done
576
577lemma (in perfect_space) at_neq_bot [simp]: "at a \<noteq> bot"
578  by (simp add: at_eq_bot_iff not_open_singleton)
579
580lemma (in order_topology) nhds_order:
581  "nhds x = inf (INF a\<in>{x <..}. principal {..< a}) (INF a\<in>{..< x}. principal {a <..})"
582proof -
583  have 1: "{S \<in> range lessThan \<union> range greaterThan. x \<in> S} =
584      (\<lambda>a. {..< a}) ` {x <..} \<union> (\<lambda>a. {a <..}) ` {..< x}"
585    by auto
586  show ?thesis
587    by (simp only: nhds_generated_topology[OF open_generated_order] INF_union 1 INF_image comp_def)
588qed
589
590lemma (in topological_space) filterlim_at_within_If:
591  assumes "filterlim f G (at x within (A \<inter> {x. P x}))"
592    and "filterlim g G (at x within (A \<inter> {x. \<not>P x}))"
593  shows "filterlim (\<lambda>x. if P x then f x else g x) G (at x within A)"
594proof (rule filterlim_If)
595  note assms(1)
596  also have "at x within (A \<inter> {x. P x}) = inf (nhds x) (principal (A \<inter> Collect P - {x}))"
597    by (simp add: at_within_def)
598  also have "A \<inter> Collect P - {x} = (A - {x}) \<inter> Collect P"
599    by blast
600  also have "inf (nhds x) (principal \<dots>) = inf (at x within A) (principal (Collect P))"
601    by (simp add: at_within_def inf_assoc)
602  finally show "filterlim f G (inf (at x within A) (principal (Collect P)))" .
603next
604  note assms(2)
605  also have "at x within (A \<inter> {x. \<not> P x}) = inf (nhds x) (principal (A \<inter> {x. \<not> P x} - {x}))"
606    by (simp add: at_within_def)
607  also have "A \<inter> {x. \<not> P x} - {x} = (A - {x}) \<inter> {x. \<not> P x}"
608    by blast
609  also have "inf (nhds x) (principal \<dots>) = inf (at x within A) (principal {x. \<not> P x})"
610    by (simp add: at_within_def inf_assoc)
611  finally show "filterlim g G (inf (at x within A) (principal {x. \<not> P x}))" .
612qed
613
614lemma (in topological_space) filterlim_at_If:
615  assumes "filterlim f G (at x within {x. P x})"
616    and "filterlim g G (at x within {x. \<not>P x})"
617  shows "filterlim (\<lambda>x. if P x then f x else g x) G (at x)"
618  using assms by (intro filterlim_at_within_If) simp_all
619lemma (in linorder_topology) at_within_order:
620  assumes "UNIV \<noteq> {x}"
621  shows "at x within s =
622    inf (INF a\<in>{x <..}. principal ({..< a} \<inter> s - {x}))
623        (INF a\<in>{..< x}. principal ({a <..} \<inter> s - {x}))"
624proof (cases "{x <..} = {}" "{..< x} = {}" rule: case_split [case_product case_split])
625  case True_True
626  have "UNIV = {..< x} \<union> {x} \<union> {x <..}"
627    by auto
628  with assms True_True show ?thesis
629    by auto
630qed (auto simp del: inf_principal simp: at_within_def nhds_order Int_Diff
631      inf_principal[symmetric] INF_inf_const2 inf_sup_aci[where 'a="'a filter"])
632
633lemma (in linorder_topology) at_left_eq:
634  "y < x \<Longrightarrow> at_left x = (INF a\<in>{..< x}. principal {a <..< x})"
635  by (subst at_within_order)
636     (auto simp: greaterThan_Int_greaterThan greaterThanLessThan_eq[symmetric] min.absorb2 INF_constant
637           intro!: INF_lower2 inf_absorb2)
638
639lemma (in linorder_topology) eventually_at_left:
640  "y < x \<Longrightarrow> eventually P (at_left x) \<longleftrightarrow> (\<exists>b<x. \<forall>y>b. y < x \<longrightarrow> P y)"
641  unfolding at_left_eq
642  by (subst eventually_INF_base) (auto simp: eventually_principal Ball_def)
643
644lemma (in linorder_topology) at_right_eq:
645  "x < y \<Longrightarrow> at_right x = (INF a\<in>{x <..}. principal {x <..< a})"
646  by (subst at_within_order)
647     (auto simp: lessThan_Int_lessThan greaterThanLessThan_eq[symmetric] max.absorb2 INF_constant Int_commute
648           intro!: INF_lower2 inf_absorb1)
649
650lemma (in linorder_topology) eventually_at_right:
651  "x < y \<Longrightarrow> eventually P (at_right x) \<longleftrightarrow> (\<exists>b>x. \<forall>y>x. y < b \<longrightarrow> P y)"
652  unfolding at_right_eq
653  by (subst eventually_INF_base) (auto simp: eventually_principal Ball_def)
654
655lemma eventually_at_right_less: "\<forall>\<^sub>F y in at_right (x::'a::{linorder_topology, no_top}). x < y"
656  using gt_ex[of x] eventually_at_right[of x] by auto
657
658lemma trivial_limit_at_right_top: "at_right (top::_::{order_top,linorder_topology}) = bot"
659  by (auto simp: filter_eq_iff eventually_at_topological)
660
661lemma trivial_limit_at_left_bot: "at_left (bot::_::{order_bot,linorder_topology}) = bot"
662  by (auto simp: filter_eq_iff eventually_at_topological)
663
664lemma trivial_limit_at_left_real [simp]: "\<not> trivial_limit (at_left x)"
665  for x :: "'a::{no_bot,dense_order,linorder_topology}"
666  using lt_ex [of x]
667  by safe (auto simp add: trivial_limit_def eventually_at_left dest: dense)
668
669lemma trivial_limit_at_right_real [simp]: "\<not> trivial_limit (at_right x)"
670  for x :: "'a::{no_top,dense_order,linorder_topology}"
671  using gt_ex[of x]
672  by safe (auto simp add: trivial_limit_def eventually_at_right dest: dense)
673
674lemma (in linorder_topology) at_eq_sup_left_right: "at x = sup (at_left x) (at_right x)"
675  by (auto simp: eventually_at_filter filter_eq_iff eventually_sup
676      elim: eventually_elim2 eventually_mono)
677
678lemma (in linorder_topology) eventually_at_split:
679  "eventually P (at x) \<longleftrightarrow> eventually P (at_left x) \<and> eventually P (at_right x)"
680  by (subst at_eq_sup_left_right) (simp add: eventually_sup)
681
682lemma (in order_topology) eventually_at_leftI:
683  assumes "\<And>x. x \<in> {a<..<b} \<Longrightarrow> P x" "a < b"
684  shows   "eventually P (at_left b)"
685  using assms unfolding eventually_at_topological by (intro exI[of _ "{a<..}"]) auto
686
687lemma (in order_topology) eventually_at_rightI:
688  assumes "\<And>x. x \<in> {a<..<b} \<Longrightarrow> P x" "a < b"
689  shows   "eventually P (at_right a)"
690  using assms unfolding eventually_at_topological by (intro exI[of _ "{..<b}"]) auto
691
692lemma eventually_filtercomap_nhds:
693  "eventually P (filtercomap f (nhds x)) \<longleftrightarrow> (\<exists>S. open S \<and> x \<in> S \<and> (\<forall>x. f x \<in> S \<longrightarrow> P x))"
694  unfolding eventually_filtercomap eventually_nhds by auto
695
696lemma eventually_filtercomap_at_topological:
697  "eventually P (filtercomap f (at A within B)) \<longleftrightarrow> 
698     (\<exists>S. open S \<and> A \<in> S \<and> (\<forall>x. f x \<in> S \<inter> B - {A} \<longrightarrow> P x))" (is "?lhs = ?rhs")
699  unfolding at_within_def filtercomap_inf eventually_inf_principal filtercomap_principal 
700          eventually_filtercomap_nhds eventually_principal by blast
701
702lemma eventually_at_right_field:
703  "eventually P (at_right x) \<longleftrightarrow> (\<exists>b>x. \<forall>y>x. y < b \<longrightarrow> P y)"
704  for x :: "'a::{linordered_field, linorder_topology}"
705  using linordered_field_no_ub[rule_format, of x]
706  by (auto simp: eventually_at_right)
707
708lemma eventually_at_left_field:
709  "eventually P (at_left x) \<longleftrightarrow> (\<exists>b<x. \<forall>y>b. y < x \<longrightarrow> P y)"
710  for x :: "'a::{linordered_field, linorder_topology}"
711  using linordered_field_no_lb[rule_format, of x]
712  by (auto simp: eventually_at_left)
713
714
715subsubsection \<open>Tendsto\<close>
716
717abbreviation (in topological_space)
718  tendsto :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b filter \<Rightarrow> bool"  (infixr "\<longlongrightarrow>" 55)
719  where "(f \<longlongrightarrow> l) F \<equiv> filterlim f (nhds l) F"
720
721definition (in t2_space) Lim :: "'f filter \<Rightarrow> ('f \<Rightarrow> 'a) \<Rightarrow> 'a"
722  where "Lim A f = (THE l. (f \<longlongrightarrow> l) A)"
723
724lemma (in topological_space) tendsto_eq_rhs: "(f \<longlongrightarrow> x) F \<Longrightarrow> x = y \<Longrightarrow> (f \<longlongrightarrow> y) F"
725  by simp
726
727named_theorems tendsto_intros "introduction rules for tendsto"
728setup \<open>
729  Global_Theory.add_thms_dynamic (\<^binding>\<open>tendsto_eq_intros\<close>,
730    fn context =>
731      Named_Theorems.get (Context.proof_of context) \<^named_theorems>\<open>tendsto_intros\<close>
732      |> map_filter (try (fn thm => @{thm tendsto_eq_rhs} OF [thm])))
733\<close>
734
735context topological_space begin
736
737lemma tendsto_def:
738   "(f \<longlongrightarrow> l) F \<longleftrightarrow> (\<forall>S. open S \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) F)"
739   unfolding nhds_def filterlim_INF filterlim_principal by auto
740
741lemma tendsto_cong: "(f \<longlongrightarrow> c) F \<longleftrightarrow> (g \<longlongrightarrow> c) F" if "eventually (\<lambda>x. f x = g x) F"
742  by (rule filterlim_cong [OF refl refl that])
743
744lemma tendsto_mono: "F \<le> F' \<Longrightarrow> (f \<longlongrightarrow> l) F' \<Longrightarrow> (f \<longlongrightarrow> l) F"
745  unfolding tendsto_def le_filter_def by fast
746
747lemma tendsto_ident_at [tendsto_intros, simp, intro]: "((\<lambda>x. x) \<longlongrightarrow> a) (at a within s)"
748  by (auto simp: tendsto_def eventually_at_topological)
749
750lemma tendsto_const [tendsto_intros, simp, intro]: "((\<lambda>x. k) \<longlongrightarrow> k) F"
751  by (simp add: tendsto_def)
752
753lemma filterlim_at:
754  "(LIM x F. f x :> at b within s) \<longleftrightarrow> eventually (\<lambda>x. f x \<in> s \<and> f x \<noteq> b) F \<and> (f \<longlongrightarrow> b) F"
755  by (simp add: at_within_def filterlim_inf filterlim_principal conj_commute)
756
757lemma (in -)
758  assumes "filterlim f (nhds L) F"
759  shows tendsto_imp_filterlim_at_right:
760          "eventually (\<lambda>x. f x > L) F \<Longrightarrow> filterlim f (at_right L) F"
761    and tendsto_imp_filterlim_at_left:
762          "eventually (\<lambda>x. f x < L) F \<Longrightarrow> filterlim f (at_left L) F"
763  using assms by (auto simp: filterlim_at elim: eventually_mono)
764
765lemma  filterlim_at_withinI:
766  assumes "filterlim f (nhds c) F"
767  assumes "eventually (\<lambda>x. f x \<in> A - {c}) F"
768  shows   "filterlim f (at c within A) F"
769  using assms by (simp add: filterlim_at)
770
771lemma filterlim_atI:
772  assumes "filterlim f (nhds c) F"
773  assumes "eventually (\<lambda>x. f x \<noteq> c) F"
774  shows   "filterlim f (at c) F"
775  using assms by (intro filterlim_at_withinI) simp_all
776
777lemma topological_tendstoI:
778  "(\<And>S. open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F) \<Longrightarrow> (f \<longlongrightarrow> l) F"
779  by (auto simp: tendsto_def)
780
781lemma topological_tendstoD:
782  "(f \<longlongrightarrow> l) F \<Longrightarrow> open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F"
783  by (auto simp: tendsto_def)
784
785lemma tendsto_bot [simp]: "(f \<longlongrightarrow> a) bot"
786  by (simp add: tendsto_def)
787
788lemma tendsto_eventually: "eventually (\<lambda>x. f x = l) net \<Longrightarrow> ((\<lambda>x. f x) \<longlongrightarrow> l) net"
789  by (rule topological_tendstoI) (auto elim: eventually_mono)
790
791end
792
793lemma (in topological_space) filterlim_within_subset:
794  "filterlim f l (at x within S) \<Longrightarrow> T \<subseteq> S \<Longrightarrow> filterlim f l (at x within T)"
795  by (blast intro: filterlim_mono at_le)
796
797lemmas tendsto_within_subset = filterlim_within_subset
798
799lemma (in order_topology) order_tendsto_iff:
800  "(f \<longlongrightarrow> x) F \<longleftrightarrow> (\<forall>l<x. eventually (\<lambda>x. l < f x) F) \<and> (\<forall>u>x. eventually (\<lambda>x. f x < u) F)"
801  by (auto simp: nhds_order filterlim_inf filterlim_INF filterlim_principal)
802
803lemma (in order_topology) order_tendstoI:
804  "(\<And>a. a < y \<Longrightarrow> eventually (\<lambda>x. a < f x) F) \<Longrightarrow> (\<And>a. y < a \<Longrightarrow> eventually (\<lambda>x. f x < a) F) \<Longrightarrow>
805    (f \<longlongrightarrow> y) F"
806  by (auto simp: order_tendsto_iff)
807
808lemma (in order_topology) order_tendstoD:
809  assumes "(f \<longlongrightarrow> y) F"
810  shows "a < y \<Longrightarrow> eventually (\<lambda>x. a < f x) F"
811    and "y < a \<Longrightarrow> eventually (\<lambda>x. f x < a) F"
812  using assms by (auto simp: order_tendsto_iff)
813
814lemma (in linorder_topology) tendsto_max[tendsto_intros]:
815  assumes X: "(X \<longlongrightarrow> x) net"
816    and Y: "(Y \<longlongrightarrow> y) net"
817  shows "((\<lambda>x. max (X x) (Y x)) \<longlongrightarrow> max x y) net"
818proof (rule order_tendstoI)
819  fix a
820  assume "a < max x y"
821  then show "eventually (\<lambda>x. a < max (X x) (Y x)) net"
822    using order_tendstoD(1)[OF X, of a] order_tendstoD(1)[OF Y, of a]
823    by (auto simp: less_max_iff_disj elim: eventually_mono)
824next
825  fix a
826  assume "max x y < a"
827  then show "eventually (\<lambda>x. max (X x) (Y x) < a) net"
828    using order_tendstoD(2)[OF X, of a] order_tendstoD(2)[OF Y, of a]
829    by (auto simp: eventually_conj_iff)
830qed
831
832lemma (in linorder_topology) tendsto_min[tendsto_intros]:
833  assumes X: "(X \<longlongrightarrow> x) net"
834    and Y: "(Y \<longlongrightarrow> y) net"
835  shows "((\<lambda>x. min (X x) (Y x)) \<longlongrightarrow> min x y) net"
836proof (rule order_tendstoI)
837  fix a
838  assume "a < min x y"
839  then show "eventually (\<lambda>x. a < min (X x) (Y x)) net"
840    using order_tendstoD(1)[OF X, of a] order_tendstoD(1)[OF Y, of a]
841    by (auto simp: eventually_conj_iff)
842next
843  fix a
844  assume "min x y < a"
845  then show "eventually (\<lambda>x. min (X x) (Y x) < a) net"
846    using order_tendstoD(2)[OF X, of a] order_tendstoD(2)[OF Y, of a]
847    by (auto simp: min_less_iff_disj elim: eventually_mono)
848qed
849
850lemma (in order_topology)
851  assumes "a < b"
852  shows at_within_Icc_at_right: "at a within {a..b} = at_right a"
853    and at_within_Icc_at_left:  "at b within {a..b} = at_left b"
854  using order_tendstoD(2)[OF tendsto_ident_at assms, of "{a<..}"]
855  using order_tendstoD(1)[OF tendsto_ident_at assms, of "{..<b}"]
856  by (auto intro!: order_class.antisym filter_leI
857      simp: eventually_at_filter less_le
858      elim: eventually_elim2)
859
860lemma (in order_topology) at_within_Icc_at: "a < x \<Longrightarrow> x < b \<Longrightarrow> at x within {a..b} = at x"
861  by (rule at_within_open_subset[where S="{a<..<b}"]) auto
862
863lemma (in t2_space) tendsto_unique:
864  assumes "F \<noteq> bot"
865    and "(f \<longlongrightarrow> a) F"
866    and "(f \<longlongrightarrow> b) F"
867  shows "a = b"
868proof (rule ccontr)
869  assume "a \<noteq> b"
870  obtain U V where "open U" "open V" "a \<in> U" "b \<in> V" "U \<inter> V = {}"
871    using hausdorff [OF \<open>a \<noteq> b\<close>] by fast
872  have "eventually (\<lambda>x. f x \<in> U) F"
873    using \<open>(f \<longlongrightarrow> a) F\<close> \<open>open U\<close> \<open>a \<in> U\<close> by (rule topological_tendstoD)
874  moreover
875  have "eventually (\<lambda>x. f x \<in> V) F"
876    using \<open>(f \<longlongrightarrow> b) F\<close> \<open>open V\<close> \<open>b \<in> V\<close> by (rule topological_tendstoD)
877  ultimately
878  have "eventually (\<lambda>x. False) F"
879  proof eventually_elim
880    case (elim x)
881    then have "f x \<in> U \<inter> V" by simp
882    with \<open>U \<inter> V = {}\<close> show ?case by simp
883  qed
884  with \<open>\<not> trivial_limit F\<close> show "False"
885    by (simp add: trivial_limit_def)
886qed
887
888lemma (in t2_space) tendsto_const_iff:
889  fixes a b :: 'a
890  assumes "\<not> trivial_limit F"
891  shows "((\<lambda>x. a) \<longlongrightarrow> b) F \<longleftrightarrow> a = b"
892  by (auto intro!: tendsto_unique [OF assms tendsto_const])
893
894lemma Lim_in_closed_set:
895  assumes "closed S" "eventually (\<lambda>x. f(x) \<in> S) F" "F \<noteq> bot" "(f \<longlongrightarrow> l) F"
896  shows "l \<in> S"
897proof (rule ccontr)
898  assume "l \<notin> S"
899  with \<open>closed S\<close> have "open (- S)" "l \<in> - S"
900    by (simp_all add: open_Compl)
901  with assms(4) have "eventually (\<lambda>x. f x \<in> - S) F"
902    by (rule topological_tendstoD)
903  with assms(2) have "eventually (\<lambda>x. False) F"
904    by (rule eventually_elim2) simp
905  with assms(3) show "False"
906    by (simp add: eventually_False)
907qed
908
909lemma (in t3_space) nhds_closed:
910  assumes "x \<in> A" and "open A"
911  shows   "\<exists>A'. x \<in> A' \<and> closed A' \<and> A' \<subseteq> A \<and> eventually (\<lambda>y. y \<in> A') (nhds x)"
912proof -
913  from assms have "\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> - A \<subseteq> V \<and> U \<inter> V = {}"
914    by (intro t3_space) auto
915  then obtain U V where UV: "open U" "open V" "x \<in> U" "-A \<subseteq> V" "U \<inter> V = {}"
916    by auto
917  have "eventually (\<lambda>y. y \<in> U) (nhds x)"
918    using \<open>open U\<close> and \<open>x \<in> U\<close> by (intro eventually_nhds_in_open)
919  hence "eventually (\<lambda>y. y \<in> -V) (nhds x)"
920    by eventually_elim (use UV in auto)
921  with UV show ?thesis by (intro exI[of _ "-V"]) auto
922qed
923
924lemma (in order_topology) increasing_tendsto:
925  assumes bdd: "eventually (\<lambda>n. f n \<le> l) F"
926    and en: "\<And>x. x < l \<Longrightarrow> eventually (\<lambda>n. x < f n) F"
927  shows "(f \<longlongrightarrow> l) F"
928  using assms by (intro order_tendstoI) (auto elim!: eventually_mono)
929
930lemma (in order_topology) decreasing_tendsto:
931  assumes bdd: "eventually (\<lambda>n. l \<le> f n) F"
932    and en: "\<And>x. l < x \<Longrightarrow> eventually (\<lambda>n. f n < x) F"
933  shows "(f \<longlongrightarrow> l) F"
934  using assms by (intro order_tendstoI) (auto elim!: eventually_mono)
935
936lemma (in order_topology) tendsto_sandwich:
937  assumes ev: "eventually (\<lambda>n. f n \<le> g n) net" "eventually (\<lambda>n. g n \<le> h n) net"
938  assumes lim: "(f \<longlongrightarrow> c) net" "(h \<longlongrightarrow> c) net"
939  shows "(g \<longlongrightarrow> c) net"
940proof (rule order_tendstoI)
941  fix a
942  show "a < c \<Longrightarrow> eventually (\<lambda>x. a < g x) net"
943    using order_tendstoD[OF lim(1), of a] ev by (auto elim: eventually_elim2)
944next
945  fix a
946  show "c < a \<Longrightarrow> eventually (\<lambda>x. g x < a) net"
947    using order_tendstoD[OF lim(2), of a] ev by (auto elim: eventually_elim2)
948qed
949
950lemma (in t1_space) limit_frequently_eq:
951  assumes "F \<noteq> bot"
952    and "frequently (\<lambda>x. f x = c) F"
953    and "(f \<longlongrightarrow> d) F"
954  shows "d = c"
955proof (rule ccontr)
956  assume "d \<noteq> c"
957  from t1_space[OF this] obtain U where "open U" "d \<in> U" "c \<notin> U"
958    by blast
959  with assms have "eventually (\<lambda>x. f x \<in> U) F"
960    unfolding tendsto_def by blast
961  then have "eventually (\<lambda>x. f x \<noteq> c) F"
962    by eventually_elim (insert \<open>c \<notin> U\<close>, blast)
963  with assms(2) show False
964    unfolding frequently_def by contradiction
965qed
966
967lemma (in t1_space) tendsto_imp_eventually_ne:
968  assumes  "(f \<longlongrightarrow> c) F" "c \<noteq> c'"
969  shows "eventually (\<lambda>z. f z \<noteq> c') F"
970proof (cases "F=bot")
971  case True
972  thus ?thesis by auto
973next
974  case False
975  show ?thesis
976  proof (rule ccontr)
977    assume "\<not> eventually (\<lambda>z. f z \<noteq> c') F"
978    then have "frequently (\<lambda>z. f z = c') F"
979      by (simp add: frequently_def)
980    from limit_frequently_eq[OF False this \<open>(f \<longlongrightarrow> c) F\<close>] and \<open>c \<noteq> c'\<close> show False
981      by contradiction
982  qed
983qed
984
985lemma (in linorder_topology) tendsto_le:
986  assumes F: "\<not> trivial_limit F"
987    and x: "(f \<longlongrightarrow> x) F"
988    and y: "(g \<longlongrightarrow> y) F"
989    and ev: "eventually (\<lambda>x. g x \<le> f x) F"
990  shows "y \<le> x"
991proof (rule ccontr)
992  assume "\<not> y \<le> x"
993  with less_separate[of x y] obtain a b where xy: "x < a" "b < y" "{..<a} \<inter> {b<..} = {}"
994    by (auto simp: not_le)
995  then have "eventually (\<lambda>x. f x < a) F" "eventually (\<lambda>x. b < g x) F"
996    using x y by (auto intro: order_tendstoD)
997  with ev have "eventually (\<lambda>x. False) F"
998    by eventually_elim (insert xy, fastforce)
999  with F show False
1000    by (simp add: eventually_False)
1001qed
1002
1003lemma (in linorder_topology) tendsto_lowerbound:
1004  assumes x: "(f \<longlongrightarrow> x) F"
1005      and ev: "eventually (\<lambda>i. a \<le> f i) F"
1006      and F: "\<not> trivial_limit F"
1007  shows "a \<le> x"
1008  using F x tendsto_const ev by (rule tendsto_le)
1009
1010lemma (in linorder_topology) tendsto_upperbound:
1011  assumes x: "(f \<longlongrightarrow> x) F"
1012      and ev: "eventually (\<lambda>i. a \<ge> f i) F"
1013      and F: "\<not> trivial_limit F"
1014  shows "a \<ge> x"
1015  by (rule tendsto_le [OF F tendsto_const x ev])
1016
1017lemma filterlim_at_within_not_equal:
1018  fixes f::"'a \<Rightarrow> 'b::t2_space"
1019  assumes "filterlim f (at a within s) F"
1020  shows "eventually (\<lambda>w. f w\<in>s \<and> f w \<noteq>b) F"
1021proof (cases "a=b")
1022  case True
1023  then show ?thesis using assms by (simp add: filterlim_at)
1024next
1025  case False
1026  from hausdorff[OF this] obtain U V where UV:"open U" "open V" "a \<in> U" "b \<in> V" "U \<inter> V = {}"
1027    by auto  
1028  have "(f \<longlongrightarrow> a) F" using assms filterlim_at by auto
1029  then have "\<forall>\<^sub>F x in F. f x \<in> U" using UV unfolding tendsto_def by auto
1030  moreover have  "\<forall>\<^sub>F x in F. f x \<in> s \<and> f x\<noteq>a" using assms filterlim_at by auto
1031  ultimately show ?thesis 
1032    apply eventually_elim
1033    using UV by auto
1034qed
1035
1036subsubsection \<open>Rules about \<^const>\<open>Lim\<close>\<close>
1037
1038lemma tendsto_Lim: "\<not> trivial_limit net \<Longrightarrow> (f \<longlongrightarrow> l) net \<Longrightarrow> Lim net f = l"
1039  unfolding Lim_def using tendsto_unique [of net f] by auto
1040
1041lemma Lim_ident_at: "\<not> trivial_limit (at x within s) \<Longrightarrow> Lim (at x within s) (\<lambda>x. x) = x"
1042  by (rule tendsto_Lim[OF _ tendsto_ident_at]) auto
1043
1044lemma eventually_Lim_ident_at:
1045  "(\<forall>\<^sub>F y in at x within X. P (Lim (at x within X) (\<lambda>x. x)) y) \<longleftrightarrow>
1046    (\<forall>\<^sub>F y in at x within X. P x y)" for x::"'a::t2_space"
1047  by (cases "at x within X = bot") (auto simp: Lim_ident_at)
1048
1049lemma filterlim_at_bot_at_right:
1050  fixes f :: "'a::linorder_topology \<Rightarrow> 'b::linorder"
1051  assumes mono: "\<And>x y. Q x \<Longrightarrow> Q y \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
1052    and bij: "\<And>x. P x \<Longrightarrow> f (g x) = x" "\<And>x. P x \<Longrightarrow> Q (g x)"
1053    and Q: "eventually Q (at_right a)"
1054    and bound: "\<And>b. Q b \<Longrightarrow> a < b"
1055    and P: "eventually P at_bot"
1056  shows "filterlim f at_bot (at_right a)"
1057proof -
1058  from P obtain x where x: "\<And>y. y \<le> x \<Longrightarrow> P y"
1059    unfolding eventually_at_bot_linorder by auto
1060  show ?thesis
1061  proof (intro filterlim_at_bot_le[THEN iffD2] allI impI)
1062    fix z
1063    assume "z \<le> x"
1064    with x have "P z" by auto
1065    have "eventually (\<lambda>x. x \<le> g z) (at_right a)"
1066      using bound[OF bij(2)[OF \<open>P z\<close>]]
1067      unfolding eventually_at_right[OF bound[OF bij(2)[OF \<open>P z\<close>]]]
1068      by (auto intro!: exI[of _ "g z"])
1069    with Q show "eventually (\<lambda>x. f x \<le> z) (at_right a)"
1070      by eventually_elim (metis bij \<open>P z\<close> mono)
1071  qed
1072qed
1073
1074lemma filterlim_at_top_at_left:
1075  fixes f :: "'a::linorder_topology \<Rightarrow> 'b::linorder"
1076  assumes mono: "\<And>x y. Q x \<Longrightarrow> Q y \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
1077    and bij: "\<And>x. P x \<Longrightarrow> f (g x) = x" "\<And>x. P x \<Longrightarrow> Q (g x)"
1078    and Q: "eventually Q (at_left a)"
1079    and bound: "\<And>b. Q b \<Longrightarrow> b < a"
1080    and P: "eventually P at_top"
1081  shows "filterlim f at_top (at_left a)"
1082proof -
1083  from P obtain x where x: "\<And>y. x \<le> y \<Longrightarrow> P y"
1084    unfolding eventually_at_top_linorder by auto
1085  show ?thesis
1086  proof (intro filterlim_at_top_ge[THEN iffD2] allI impI)
1087    fix z
1088    assume "x \<le> z"
1089    with x have "P z" by auto
1090    have "eventually (\<lambda>x. g z \<le> x) (at_left a)"
1091      using bound[OF bij(2)[OF \<open>P z\<close>]]
1092      unfolding eventually_at_left[OF bound[OF bij(2)[OF \<open>P z\<close>]]]
1093      by (auto intro!: exI[of _ "g z"])
1094    with Q show "eventually (\<lambda>x. z \<le> f x) (at_left a)"
1095      by eventually_elim (metis bij \<open>P z\<close> mono)
1096  qed
1097qed
1098
1099lemma filterlim_split_at:
1100  "filterlim f F (at_left x) \<Longrightarrow> filterlim f F (at_right x) \<Longrightarrow>
1101    filterlim f F (at x)"
1102  for x :: "'a::linorder_topology"
1103  by (subst at_eq_sup_left_right) (rule filterlim_sup)
1104
1105lemma filterlim_at_split:
1106  "filterlim f F (at x) \<longleftrightarrow> filterlim f F (at_left x) \<and> filterlim f F (at_right x)"
1107  for x :: "'a::linorder_topology"
1108  by (subst at_eq_sup_left_right) (simp add: filterlim_def filtermap_sup)
1109
1110lemma eventually_nhds_top:
1111  fixes P :: "'a :: {order_top,linorder_topology} \<Rightarrow> bool"
1112    and b :: 'a
1113  assumes "b < top"
1114  shows "eventually P (nhds top) \<longleftrightarrow> (\<exists>b<top. (\<forall>z. b < z \<longrightarrow> P z))"
1115  unfolding eventually_nhds
1116proof safe
1117  fix S :: "'a set"
1118  assume "open S" "top \<in> S"
1119  note open_left[OF this \<open>b < top\<close>]
1120  moreover assume "\<forall>s\<in>S. P s"
1121  ultimately show "\<exists>b<top. \<forall>z>b. P z"
1122    by (auto simp: subset_eq Ball_def)
1123next
1124  fix b
1125  assume "b < top" "\<forall>z>b. P z"
1126  then show "\<exists>S. open S \<and> top \<in> S \<and> (\<forall>xa\<in>S. P xa)"
1127    by (intro exI[of _ "{b <..}"]) auto
1128qed
1129
1130lemma tendsto_at_within_iff_tendsto_nhds:
1131  "(g \<longlongrightarrow> g l) (at l within S) \<longleftrightarrow> (g \<longlongrightarrow> g l) (inf (nhds l) (principal S))"
1132  unfolding tendsto_def eventually_at_filter eventually_inf_principal
1133  by (intro ext all_cong imp_cong) (auto elim!: eventually_mono)
1134
1135
1136subsection \<open>Limits on sequences\<close>
1137
1138abbreviation (in topological_space)
1139  LIMSEQ :: "[nat \<Rightarrow> 'a, 'a] \<Rightarrow> bool"  ("((_)/ \<longlonglongrightarrow> (_))" [60, 60] 60)
1140  where "X \<longlonglongrightarrow> L \<equiv> (X \<longlongrightarrow> L) sequentially"
1141
1142abbreviation (in t2_space) lim :: "(nat \<Rightarrow> 'a) \<Rightarrow> 'a"
1143  where "lim X \<equiv> Lim sequentially X"
1144
1145definition (in topological_space) convergent :: "(nat \<Rightarrow> 'a) \<Rightarrow> bool"
1146  where "convergent X = (\<exists>L. X \<longlonglongrightarrow> L)"
1147
1148lemma lim_def: "lim X = (THE L. X \<longlonglongrightarrow> L)"
1149  unfolding Lim_def ..
1150
1151lemma lim_explicit:
1152  "f \<longlonglongrightarrow> f0 \<longleftrightarrow> (\<forall>S. open S \<longrightarrow> f0 \<in> S \<longrightarrow> (\<exists>N. \<forall>n\<ge>N. f n \<in> S))"
1153  unfolding tendsto_def eventually_sequentially by auto
1154
1155
1156subsection \<open>Monotone sequences and subsequences\<close>
1157
1158text \<open>
1159  Definition of monotonicity.
1160  The use of disjunction here complicates proofs considerably.
1161  One alternative is to add a Boolean argument to indicate the direction.
1162  Another is to develop the notions of increasing and decreasing first.
1163\<close>
1164definition monoseq :: "(nat \<Rightarrow> 'a::order) \<Rightarrow> bool"
1165  where "monoseq X \<longleftrightarrow> (\<forall>m. \<forall>n\<ge>m. X m \<le> X n) \<or> (\<forall>m. \<forall>n\<ge>m. X n \<le> X m)"
1166
1167abbreviation incseq :: "(nat \<Rightarrow> 'a::order) \<Rightarrow> bool"
1168  where "incseq X \<equiv> mono X"
1169
1170lemma incseq_def: "incseq X \<longleftrightarrow> (\<forall>m. \<forall>n\<ge>m. X n \<ge> X m)"
1171  unfolding mono_def ..
1172
1173abbreviation decseq :: "(nat \<Rightarrow> 'a::order) \<Rightarrow> bool"
1174  where "decseq X \<equiv> antimono X"
1175
1176lemma decseq_def: "decseq X \<longleftrightarrow> (\<forall>m. \<forall>n\<ge>m. X n \<le> X m)"
1177  unfolding antimono_def ..
1178
1179subsubsection \<open>Definition of subsequence.\<close>
1180
1181(* For compatibility with the old "subseq" *)
1182lemma strict_mono_leD: "strict_mono r \<Longrightarrow> m \<le> n \<Longrightarrow> r m \<le> r n"
1183  by (erule (1) monoD [OF strict_mono_mono])
1184
1185lemma strict_mono_id: "strict_mono id"
1186  by (simp add: strict_mono_def)
1187
1188lemma incseq_SucI: "(\<And>n. X n \<le> X (Suc n)) \<Longrightarrow> incseq X"
1189  using lift_Suc_mono_le[of X] by (auto simp: incseq_def)
1190
1191lemma incseqD: "incseq f \<Longrightarrow> i \<le> j \<Longrightarrow> f i \<le> f j"
1192  by (auto simp: incseq_def)
1193
1194lemma incseq_SucD: "incseq A \<Longrightarrow> A i \<le> A (Suc i)"
1195  using incseqD[of A i "Suc i"] by auto
1196
1197lemma incseq_Suc_iff: "incseq f \<longleftrightarrow> (\<forall>n. f n \<le> f (Suc n))"
1198  by (auto intro: incseq_SucI dest: incseq_SucD)
1199
1200lemma incseq_const[simp, intro]: "incseq (\<lambda>x. k)"
1201  unfolding incseq_def by auto
1202
1203lemma decseq_SucI: "(\<And>n. X (Suc n) \<le> X n) \<Longrightarrow> decseq X"
1204  using order.lift_Suc_mono_le[OF dual_order, of X] by (auto simp: decseq_def)
1205
1206lemma decseqD: "decseq f \<Longrightarrow> i \<le> j \<Longrightarrow> f j \<le> f i"
1207  by (auto simp: decseq_def)
1208
1209lemma decseq_SucD: "decseq A \<Longrightarrow> A (Suc i) \<le> A i"
1210  using decseqD[of A i "Suc i"] by auto
1211
1212lemma decseq_Suc_iff: "decseq f \<longleftrightarrow> (\<forall>n. f (Suc n) \<le> f n)"
1213  by (auto intro: decseq_SucI dest: decseq_SucD)
1214
1215lemma decseq_const[simp, intro]: "decseq (\<lambda>x. k)"
1216  unfolding decseq_def by auto
1217
1218lemma monoseq_iff: "monoseq X \<longleftrightarrow> incseq X \<or> decseq X"
1219  unfolding monoseq_def incseq_def decseq_def ..
1220
1221lemma monoseq_Suc: "monoseq X \<longleftrightarrow> (\<forall>n. X n \<le> X (Suc n)) \<or> (\<forall>n. X (Suc n) \<le> X n)"
1222  unfolding monoseq_iff incseq_Suc_iff decseq_Suc_iff ..
1223
1224lemma monoI1: "\<forall>m. \<forall>n \<ge> m. X m \<le> X n \<Longrightarrow> monoseq X"
1225  by (simp add: monoseq_def)
1226
1227lemma monoI2: "\<forall>m. \<forall>n \<ge> m. X n \<le> X m \<Longrightarrow> monoseq X"
1228  by (simp add: monoseq_def)
1229
1230lemma mono_SucI1: "\<forall>n. X n \<le> X (Suc n) \<Longrightarrow> monoseq X"
1231  by (simp add: monoseq_Suc)
1232
1233lemma mono_SucI2: "\<forall>n. X (Suc n) \<le> X n \<Longrightarrow> monoseq X"
1234  by (simp add: monoseq_Suc)
1235
1236lemma monoseq_minus:
1237  fixes a :: "nat \<Rightarrow> 'a::ordered_ab_group_add"
1238  assumes "monoseq a"
1239  shows "monoseq (\<lambda> n. - a n)"
1240proof (cases "\<forall>m. \<forall>n \<ge> m. a m \<le> a n")
1241  case True
1242  then have "\<forall>m. \<forall>n \<ge> m. - a n \<le> - a m" by auto
1243  then show ?thesis by (rule monoI2)
1244next
1245  case False
1246  then have "\<forall>m. \<forall>n \<ge> m. - a m \<le> - a n"
1247    using \<open>monoseq a\<close>[unfolded monoseq_def] by auto
1248  then show ?thesis by (rule monoI1)
1249qed
1250
1251
1252subsubsection \<open>Subsequence (alternative definition, (e.g. Hoskins)\<close>
1253
1254lemma strict_mono_Suc_iff: "strict_mono f \<longleftrightarrow> (\<forall>n. f n < f (Suc n))"
1255proof (intro iffI strict_monoI)
1256  assume *: "\<forall>n. f n < f (Suc n)"
1257  fix m n :: nat assume "m < n"
1258  thus "f m < f n"
1259    by (induction rule: less_Suc_induct) (use * in auto)
1260qed (auto simp: strict_mono_def)
1261
1262lemma strict_mono_add: "strict_mono (\<lambda>n::'a::linordered_semidom. n + k)"
1263  by (auto simp: strict_mono_def)
1264
1265text \<open>For any sequence, there is a monotonic subsequence.\<close>
1266lemma seq_monosub:
1267  fixes s :: "nat \<Rightarrow> 'a::linorder"
1268  shows "\<exists>f. strict_mono f \<and> monoseq (\<lambda>n. (s (f n)))"
1269proof (cases "\<forall>n. \<exists>p>n. \<forall>m\<ge>p. s m \<le> s p")
1270  case True
1271  then have "\<exists>f. \<forall>n. (\<forall>m\<ge>f n. s m \<le> s (f n)) \<and> f n < f (Suc n)"
1272    by (intro dependent_nat_choice) (auto simp: conj_commute)
1273  then obtain f :: "nat \<Rightarrow> nat" 
1274    where f: "strict_mono f" and mono: "\<And>n m. f n \<le> m \<Longrightarrow> s m \<le> s (f n)"
1275    by (auto simp: strict_mono_Suc_iff)
1276  then have "incseq f"
1277    unfolding strict_mono_Suc_iff incseq_Suc_iff by (auto intro: less_imp_le)
1278  then have "monoseq (\<lambda>n. s (f n))"
1279    by (auto simp add: incseq_def intro!: mono monoI2)
1280  with f show ?thesis
1281    by auto
1282next
1283  case False
1284  then obtain N where N: "p > N \<Longrightarrow> \<exists>m>p. s p < s m" for p
1285    by (force simp: not_le le_less)
1286  have "\<exists>f. \<forall>n. N < f n \<and> f n < f (Suc n) \<and> s (f n) \<le> s (f (Suc n))"
1287  proof (intro dependent_nat_choice)
1288    fix x
1289    assume "N < x" with N[of x]
1290    show "\<exists>y>N. x < y \<and> s x \<le> s y"
1291      by (auto intro: less_trans)
1292  qed auto
1293  then show ?thesis
1294    by (auto simp: monoseq_iff incseq_Suc_iff strict_mono_Suc_iff)
1295qed
1296
1297lemma seq_suble:
1298  assumes sf: "strict_mono (f :: nat \<Rightarrow> nat)"
1299  shows "n \<le> f n"
1300proof (induct n)
1301  case 0
1302  show ?case by simp
1303next
1304  case (Suc n)
1305  with sf [unfolded strict_mono_Suc_iff, rule_format, of n] have "n < f (Suc n)"
1306     by arith
1307  then show ?case by arith
1308qed
1309
1310lemma eventually_subseq:
1311  "strict_mono r \<Longrightarrow> eventually P sequentially \<Longrightarrow> eventually (\<lambda>n. P (r n)) sequentially"
1312  unfolding eventually_sequentially by (metis seq_suble le_trans)
1313
1314lemma not_eventually_sequentiallyD:
1315  assumes "\<not> eventually P sequentially"
1316  shows "\<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (\<forall>n. \<not> P (r n))"
1317proof -
1318  from assms have "\<forall>n. \<exists>m\<ge>n. \<not> P m"
1319    unfolding eventually_sequentially by (simp add: not_less)
1320  then obtain r where "\<And>n. r n \<ge> n" "\<And>n. \<not> P (r n)"
1321    by (auto simp: choice_iff)
1322  then show ?thesis
1323    by (auto intro!: exI[of _ "\<lambda>n. r (((Suc \<circ> r) ^^ Suc n) 0)"]
1324             simp: less_eq_Suc_le strict_mono_Suc_iff)
1325qed
1326
1327lemma sequentially_offset: 
1328  assumes "eventually (\<lambda>i. P i) sequentially"
1329  shows "eventually (\<lambda>i. P (i + k)) sequentially"
1330  using assms by (rule eventually_sequentially_seg [THEN iffD2])
1331
1332lemma seq_offset_neg: 
1333  "(f \<longlongrightarrow> l) sequentially \<Longrightarrow> ((\<lambda>i. f(i - k)) \<longlongrightarrow> l) sequentially"
1334  apply (erule filterlim_compose)
1335  apply (simp add: filterlim_def le_sequentially eventually_filtermap eventually_sequentially, arith)
1336  done
1337
1338lemma filterlim_subseq: "strict_mono f \<Longrightarrow> filterlim f sequentially sequentially"
1339  unfolding filterlim_iff by (metis eventually_subseq)
1340
1341lemma strict_mono_o: "strict_mono r \<Longrightarrow> strict_mono s \<Longrightarrow> strict_mono (r \<circ> s)"
1342  unfolding strict_mono_def by simp
1343
1344lemma strict_mono_compose: "strict_mono r \<Longrightarrow> strict_mono s \<Longrightarrow> strict_mono (\<lambda>x. r (s x))"
1345  using strict_mono_o[of r s] by (simp add: o_def)
1346
1347lemma incseq_imp_monoseq:  "incseq X \<Longrightarrow> monoseq X"
1348  by (simp add: incseq_def monoseq_def)
1349
1350lemma decseq_imp_monoseq:  "decseq X \<Longrightarrow> monoseq X"
1351  by (simp add: decseq_def monoseq_def)
1352
1353lemma decseq_eq_incseq: "decseq X = incseq (\<lambda>n. - X n)"
1354  for X :: "nat \<Rightarrow> 'a::ordered_ab_group_add"
1355  by (simp add: decseq_def incseq_def)
1356
1357lemma INT_decseq_offset:
1358  assumes "decseq F"
1359  shows "(\<Inter>i. F i) = (\<Inter>i\<in>{n..}. F i)"
1360proof safe
1361  fix x i
1362  assume x: "x \<in> (\<Inter>i\<in>{n..}. F i)"
1363  show "x \<in> F i"
1364  proof cases
1365    from x have "x \<in> F n" by auto
1366    also assume "i \<le> n" with \<open>decseq F\<close> have "F n \<subseteq> F i"
1367      unfolding decseq_def by simp
1368    finally show ?thesis .
1369  qed (insert x, simp)
1370qed auto
1371
1372lemma LIMSEQ_const_iff: "(\<lambda>n. k) \<longlonglongrightarrow> l \<longleftrightarrow> k = l"
1373  for k l :: "'a::t2_space"
1374  using trivial_limit_sequentially by (rule tendsto_const_iff)
1375
1376lemma LIMSEQ_SUP: "incseq X \<Longrightarrow> X \<longlonglongrightarrow> (SUP i. X i :: 'a::{complete_linorder,linorder_topology})"
1377  by (intro increasing_tendsto)
1378    (auto simp: SUP_upper less_SUP_iff incseq_def eventually_sequentially intro: less_le_trans)
1379
1380lemma LIMSEQ_INF: "decseq X \<Longrightarrow> X \<longlonglongrightarrow> (INF i. X i :: 'a::{complete_linorder,linorder_topology})"
1381  by (intro decreasing_tendsto)
1382    (auto simp: INF_lower INF_less_iff decseq_def eventually_sequentially intro: le_less_trans)
1383
1384lemma LIMSEQ_ignore_initial_segment: "f \<longlonglongrightarrow> a \<Longrightarrow> (\<lambda>n. f (n + k)) \<longlonglongrightarrow> a"
1385  unfolding tendsto_def by (subst eventually_sequentially_seg[where k=k])
1386
1387lemma LIMSEQ_offset: "(\<lambda>n. f (n + k)) \<longlonglongrightarrow> a \<Longrightarrow> f \<longlonglongrightarrow> a"
1388  unfolding tendsto_def
1389  by (subst (asm) eventually_sequentially_seg[where k=k])
1390
1391lemma LIMSEQ_Suc: "f \<longlonglongrightarrow> l \<Longrightarrow> (\<lambda>n. f (Suc n)) \<longlonglongrightarrow> l"
1392  by (drule LIMSEQ_ignore_initial_segment [where k="Suc 0"]) simp
1393
1394lemma LIMSEQ_imp_Suc: "(\<lambda>n. f (Suc n)) \<longlonglongrightarrow> l \<Longrightarrow> f \<longlonglongrightarrow> l"
1395  by (rule LIMSEQ_offset [where k="Suc 0"]) simp
1396
1397lemma LIMSEQ_Suc_iff: "(\<lambda>n. f (Suc n)) \<longlonglongrightarrow> l = f \<longlonglongrightarrow> l"
1398  by (rule filterlim_sequentially_Suc)
1399
1400lemma LIMSEQ_lessThan_iff_atMost:
1401  shows "(\<lambda>n. f {..<n}) \<longlonglongrightarrow> x \<longleftrightarrow> (\<lambda>n. f {..n}) \<longlonglongrightarrow> x"
1402  apply (subst LIMSEQ_Suc_iff [symmetric])
1403  apply (simp only: lessThan_Suc_atMost)
1404  done
1405
1406lemma LIMSEQ_unique: "X \<longlonglongrightarrow> a \<Longrightarrow> X \<longlonglongrightarrow> b \<Longrightarrow> a = b"
1407  for a b :: "'a::t2_space"
1408  using trivial_limit_sequentially by (rule tendsto_unique)
1409
1410lemma LIMSEQ_le_const: "X \<longlonglongrightarrow> x \<Longrightarrow> \<exists>N. \<forall>n\<ge>N. a \<le> X n \<Longrightarrow> a \<le> x"
1411  for a x :: "'a::linorder_topology"
1412  by (simp add: eventually_at_top_linorder tendsto_lowerbound)
1413
1414lemma LIMSEQ_le: "X \<longlonglongrightarrow> x \<Longrightarrow> Y \<longlonglongrightarrow> y \<Longrightarrow> \<exists>N. \<forall>n\<ge>N. X n \<le> Y n \<Longrightarrow> x \<le> y"
1415  for x y :: "'a::linorder_topology"
1416  using tendsto_le[of sequentially Y y X x] by (simp add: eventually_sequentially)
1417
1418lemma LIMSEQ_le_const2: "X \<longlonglongrightarrow> x \<Longrightarrow> \<exists>N. \<forall>n\<ge>N. X n \<le> a \<Longrightarrow> x \<le> a"
1419  for a x :: "'a::linorder_topology"
1420  by (rule LIMSEQ_le[of X x "\<lambda>n. a"]) auto
1421
1422lemma Lim_bounded: "f \<longlonglongrightarrow> l \<Longrightarrow> \<forall>n\<ge>M. f n \<le> C \<Longrightarrow> l \<le> C"
1423  for l :: "'a::linorder_topology"
1424  by (intro LIMSEQ_le_const2) auto
1425
1426lemma Lim_bounded2:
1427  fixes f :: "nat \<Rightarrow> 'a::linorder_topology"
1428  assumes lim:"f \<longlonglongrightarrow> l" and ge: "\<forall>n\<ge>N. f n \<ge> C"
1429  shows "l \<ge> C"
1430  using ge
1431  by (intro tendsto_le[OF trivial_limit_sequentially lim tendsto_const])
1432     (auto simp: eventually_sequentially)
1433
1434lemma lim_mono:
1435  fixes X Y :: "nat \<Rightarrow> 'a::linorder_topology"
1436  assumes "\<And>n. N \<le> n \<Longrightarrow> X n \<le> Y n"
1437    and "X \<longlonglongrightarrow> x"
1438    and "Y \<longlonglongrightarrow> y"
1439  shows "x \<le> y"
1440  using assms(1) by (intro LIMSEQ_le[OF assms(2,3)]) auto
1441
1442lemma Sup_lim:
1443  fixes a :: "'a::{complete_linorder,linorder_topology}"
1444  assumes "\<And>n. b n \<in> s"
1445    and "b \<longlonglongrightarrow> a"
1446  shows "a \<le> Sup s"
1447  by (metis Lim_bounded assms complete_lattice_class.Sup_upper)
1448
1449lemma Inf_lim:
1450  fixes a :: "'a::{complete_linorder,linorder_topology}"
1451  assumes "\<And>n. b n \<in> s"
1452    and "b \<longlonglongrightarrow> a"
1453  shows "Inf s \<le> a"
1454  by (metis Lim_bounded2 assms complete_lattice_class.Inf_lower)
1455
1456lemma SUP_Lim:
1457  fixes X :: "nat \<Rightarrow> 'a::{complete_linorder,linorder_topology}"
1458  assumes inc: "incseq X"
1459    and l: "X \<longlonglongrightarrow> l"
1460  shows "(SUP n. X n) = l"
1461  using LIMSEQ_SUP[OF inc] tendsto_unique[OF trivial_limit_sequentially l]
1462  by simp
1463
1464lemma INF_Lim:
1465  fixes X :: "nat \<Rightarrow> 'a::{complete_linorder,linorder_topology}"
1466  assumes dec: "decseq X"
1467    and l: "X \<longlonglongrightarrow> l"
1468  shows "(INF n. X n) = l"
1469  using LIMSEQ_INF[OF dec] tendsto_unique[OF trivial_limit_sequentially l]
1470  by simp
1471
1472lemma convergentD: "convergent X \<Longrightarrow> \<exists>L. X \<longlonglongrightarrow> L"
1473  by (simp add: convergent_def)
1474
1475lemma convergentI: "X \<longlonglongrightarrow> L \<Longrightarrow> convergent X"
1476  by (auto simp add: convergent_def)
1477
1478lemma convergent_LIMSEQ_iff: "convergent X \<longleftrightarrow> X \<longlonglongrightarrow> lim X"
1479  by (auto intro: theI LIMSEQ_unique simp add: convergent_def lim_def)
1480
1481lemma convergent_const: "convergent (\<lambda>n. c)"
1482  by (rule convergentI) (rule tendsto_const)
1483
1484lemma monoseq_le:
1485  "monoseq a \<Longrightarrow> a \<longlonglongrightarrow> x \<Longrightarrow>
1486    (\<forall>n. a n \<le> x) \<and> (\<forall>m. \<forall>n\<ge>m. a m \<le> a n) \<or>
1487    (\<forall>n. x \<le> a n) \<and> (\<forall>m. \<forall>n\<ge>m. a n \<le> a m)"
1488  for x :: "'a::linorder_topology"
1489  by (metis LIMSEQ_le_const LIMSEQ_le_const2 decseq_def incseq_def monoseq_iff)
1490
1491lemma LIMSEQ_subseq_LIMSEQ: "X \<longlonglongrightarrow> L \<Longrightarrow> strict_mono f \<Longrightarrow> (X \<circ> f) \<longlonglongrightarrow> L"
1492  unfolding comp_def by (rule filterlim_compose [of X, OF _ filterlim_subseq])
1493
1494lemma convergent_subseq_convergent: "convergent X \<Longrightarrow> strict_mono f \<Longrightarrow> convergent (X \<circ> f)"
1495  by (auto simp: convergent_def intro: LIMSEQ_subseq_LIMSEQ)
1496
1497lemma limI: "X \<longlonglongrightarrow> L \<Longrightarrow> lim X = L"
1498  by (rule tendsto_Lim) (rule trivial_limit_sequentially)
1499
1500lemma lim_le: "convergent f \<Longrightarrow> (\<And>n. f n \<le> x) \<Longrightarrow> lim f \<le> x"
1501  for x :: "'a::linorder_topology"
1502  using LIMSEQ_le_const2[of f "lim f" x] by (simp add: convergent_LIMSEQ_iff)
1503
1504lemma lim_const [simp]: "lim (\<lambda>m. a) = a"
1505  by (simp add: limI)
1506
1507
1508subsubsection \<open>Increasing and Decreasing Series\<close>
1509
1510lemma incseq_le: "incseq X \<Longrightarrow> X \<longlonglongrightarrow> L \<Longrightarrow> X n \<le> L"
1511  for L :: "'a::linorder_topology"
1512  by (metis incseq_def LIMSEQ_le_const)
1513
1514lemma decseq_ge: "decseq X \<Longrightarrow> X \<longlonglongrightarrow> L \<Longrightarrow> L \<le> X n"
1515  for L :: "'a::linorder_topology"
1516  by (metis decseq_def LIMSEQ_le_const2)
1517
1518
1519subsection \<open>First countable topologies\<close>
1520
1521class first_countable_topology = topological_space +
1522  assumes first_countable_basis:
1523    "\<exists>A::nat \<Rightarrow> 'a set. (\<forall>i. x \<in> A i \<and> open (A i)) \<and> (\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>i. A i \<subseteq> S))"
1524
1525lemma (in first_countable_topology) countable_basis_at_decseq:
1526  obtains A :: "nat \<Rightarrow> 'a set" where
1527    "\<And>i. open (A i)" "\<And>i. x \<in> (A i)"
1528    "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
1529proof atomize_elim
1530  from first_countable_basis[of x] obtain A :: "nat \<Rightarrow> 'a set"
1531    where nhds: "\<And>i. open (A i)" "\<And>i. x \<in> A i"
1532      and incl: "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>i. A i \<subseteq> S"
1533    by auto
1534  define F where "F n = (\<Inter>i\<le>n. A i)" for n
1535  show "\<exists>A. (\<forall>i. open (A i)) \<and> (\<forall>i. x \<in> A i) \<and>
1536    (\<forall>S. open S \<longrightarrow> x \<in> S \<longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially)"
1537  proof (safe intro!: exI[of _ F])
1538    fix i
1539    show "open (F i)"
1540      using nhds(1) by (auto simp: F_def)
1541    show "x \<in> F i"
1542      using nhds(2) by (auto simp: F_def)
1543  next
1544    fix S
1545    assume "open S" "x \<in> S"
1546    from incl[OF this] obtain i where "F i \<subseteq> S"
1547      unfolding F_def by auto
1548    moreover have "\<And>j. i \<le> j \<Longrightarrow> F j \<subseteq> F i"
1549      by (simp add: Inf_superset_mono F_def image_mono)
1550    ultimately show "eventually (\<lambda>i. F i \<subseteq> S) sequentially"
1551      by (auto simp: eventually_sequentially)
1552  qed
1553qed
1554
1555lemma (in first_countable_topology) nhds_countable:
1556  obtains X :: "nat \<Rightarrow> 'a set"
1557  where "decseq X" "\<And>n. open (X n)" "\<And>n. x \<in> X n" "nhds x = (INF n. principal (X n))"
1558proof -
1559  from first_countable_basis obtain A :: "nat \<Rightarrow> 'a set"
1560    where *: "\<And>n. x \<in> A n" "\<And>n. open (A n)" "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>i. A i \<subseteq> S"
1561    by metis
1562  show thesis
1563  proof
1564    show "decseq (\<lambda>n. \<Inter>i\<le>n. A i)"
1565      by (simp add: antimono_iff_le_Suc atMost_Suc)
1566    show "x \<in> (\<Inter>i\<le>n. A i)" "\<And>n. open (\<Inter>i\<le>n. A i)" for n
1567      using * by auto
1568    show "nhds x = (INF n. principal (\<Inter>i\<le>n. A i))"
1569      using *
1570      unfolding nhds_def
1571      apply -
1572      apply (rule INF_eq)
1573       apply simp_all
1574       apply fastforce
1575      apply (intro exI [of _ "\<Inter>i\<le>n. A i" for n] conjI open_INT)
1576         apply auto
1577      done
1578  qed
1579qed
1580
1581lemma (in first_countable_topology) countable_basis:
1582  obtains A :: "nat \<Rightarrow> 'a set" where
1583    "\<And>i. open (A i)" "\<And>i. x \<in> A i"
1584    "\<And>F. (\<forall>n. F n \<in> A n) \<Longrightarrow> F \<longlonglongrightarrow> x"
1585proof atomize_elim
1586  obtain A :: "nat \<Rightarrow> 'a set" where *:
1587    "\<And>i. open (A i)"
1588    "\<And>i. x \<in> A i"
1589    "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
1590    by (rule countable_basis_at_decseq) blast
1591  have "eventually (\<lambda>n. F n \<in> S) sequentially"
1592    if "\<forall>n. F n \<in> A n" "open S" "x \<in> S" for F S
1593    using *(3)[of S] that by (auto elim: eventually_mono simp: subset_eq)
1594  with * show "\<exists>A. (\<forall>i. open (A i)) \<and> (\<forall>i. x \<in> A i) \<and> (\<forall>F. (\<forall>n. F n \<in> A n) \<longrightarrow> F \<longlonglongrightarrow> x)"
1595    by (intro exI[of _ A]) (auto simp: tendsto_def)
1596qed
1597
1598lemma (in first_countable_topology) sequentially_imp_eventually_nhds_within:
1599  assumes "\<forall>f. (\<forall>n. f n \<in> s) \<and> f \<longlonglongrightarrow> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially"
1600  shows "eventually P (inf (nhds a) (principal s))"
1601proof (rule ccontr)
1602  obtain A :: "nat \<Rightarrow> 'a set" where *:
1603    "\<And>i. open (A i)"
1604    "\<And>i. a \<in> A i"
1605    "\<And>F. \<forall>n. F n \<in> A n \<Longrightarrow> F \<longlonglongrightarrow> a"
1606    by (rule countable_basis) blast
1607  assume "\<not> ?thesis"
1608  with * have "\<exists>F. \<forall>n. F n \<in> s \<and> F n \<in> A n \<and> \<not> P (F n)"
1609    unfolding eventually_inf_principal eventually_nhds
1610    by (intro choice) fastforce
1611  then obtain F where F: "\<forall>n. F n \<in> s" and "\<forall>n. F n \<in> A n" and F': "\<forall>n. \<not> P (F n)"
1612    by blast
1613  with * have "F \<longlonglongrightarrow> a"
1614    by auto
1615  then have "eventually (\<lambda>n. P (F n)) sequentially"
1616    using assms F by simp
1617  then show False
1618    by (simp add: F')
1619qed
1620
1621lemma (in first_countable_topology) eventually_nhds_within_iff_sequentially:
1622  "eventually P (inf (nhds a) (principal s)) \<longleftrightarrow>
1623    (\<forall>f. (\<forall>n. f n \<in> s) \<and> f \<longlonglongrightarrow> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially)"
1624proof (safe intro!: sequentially_imp_eventually_nhds_within)
1625  assume "eventually P (inf (nhds a) (principal s))"
1626  then obtain S where "open S" "a \<in> S" "\<forall>x\<in>S. x \<in> s \<longrightarrow> P x"
1627    by (auto simp: eventually_inf_principal eventually_nhds)
1628  moreover
1629  fix f
1630  assume "\<forall>n. f n \<in> s" "f \<longlonglongrightarrow> a"
1631  ultimately show "eventually (\<lambda>n. P (f n)) sequentially"
1632    by (auto dest!: topological_tendstoD elim: eventually_mono)
1633qed
1634
1635lemma (in first_countable_topology) eventually_nhds_iff_sequentially:
1636  "eventually P (nhds a) \<longleftrightarrow> (\<forall>f. f \<longlonglongrightarrow> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially)"
1637  using eventually_nhds_within_iff_sequentially[of P a UNIV] by simp
1638
1639(*Thanks to S��bastien Gou��zel*)
1640lemma Inf_as_limit:
1641  fixes A::"'a::{linorder_topology, first_countable_topology, complete_linorder} set"
1642  assumes "A \<noteq> {}"
1643  shows "\<exists>u. (\<forall>n. u n \<in> A) \<and> u \<longlonglongrightarrow> Inf A"
1644proof (cases "Inf A \<in> A")
1645  case True
1646  show ?thesis
1647    by (rule exI[of _ "\<lambda>n. Inf A"], auto simp add: True)
1648next
1649  case False
1650  obtain y where "y \<in> A" using assms by auto
1651  then have "Inf A < y" using False Inf_lower less_le by auto
1652  obtain F :: "nat \<Rightarrow> 'a set" where F: "\<And>i. open (F i)" "\<And>i. Inf A \<in> F i"
1653                                       "\<And>u. (\<forall>n. u n \<in> F n) \<Longrightarrow> u \<longlonglongrightarrow> Inf A"
1654    by (metis first_countable_topology_class.countable_basis)
1655  define u where "u = (\<lambda>n. SOME z. z \<in> F n \<and> z \<in> A)"
1656  have "\<exists>z. z \<in> U \<and> z \<in> A" if "Inf A \<in> U" "open U" for U
1657  proof -
1658    obtain b where "b > Inf A" "{Inf A ..<b} \<subseteq> U"
1659      using open_right[OF \<open>open U\<close> \<open>Inf A \<in> U\<close> \<open>Inf A < y\<close>] by auto
1660    obtain z where "z < b" "z \<in> A"
1661      using \<open>Inf A < b\<close> Inf_less_iff by auto
1662    then have "z \<in> {Inf A ..<b}"
1663      by (simp add: Inf_lower)
1664    then show ?thesis using \<open>z \<in> A\<close> \<open>{Inf A ..<b} \<subseteq> U\<close> by auto
1665  qed
1666  then have *: "u n \<in> F n \<and> u n \<in> A" for n
1667    using \<open>Inf A \<in> F n\<close> \<open>open (F n)\<close> unfolding u_def by (metis (no_types, lifting) someI_ex)
1668  then have "u \<longlonglongrightarrow> Inf A" using F(3) by simp
1669  then show ?thesis using * by auto
1670qed
1671
1672lemma tendsto_at_iff_sequentially:
1673  "(f \<longlongrightarrow> a) (at x within s) \<longleftrightarrow> (\<forall>X. (\<forall>i. X i \<in> s - {x}) \<longrightarrow> X \<longlonglongrightarrow> x \<longrightarrow> ((f \<circ> X) \<longlonglongrightarrow> a))"
1674  for f :: "'a::first_countable_topology \<Rightarrow> _"
1675  unfolding filterlim_def[of _ "nhds a"] le_filter_def eventually_filtermap
1676    at_within_def eventually_nhds_within_iff_sequentially comp_def
1677  by metis
1678
1679lemma approx_from_above_dense_linorder:
1680  fixes x::"'a::{dense_linorder, linorder_topology, first_countable_topology}"
1681  assumes "x < y"
1682  shows "\<exists>u. (\<forall>n. u n > x) \<and> (u \<longlonglongrightarrow> x)"
1683proof -
1684  obtain A :: "nat \<Rightarrow> 'a set" where A: "\<And>i. open (A i)" "\<And>i. x \<in> A i"
1685                                      "\<And>F. (\<forall>n. F n \<in> A n) \<Longrightarrow> F \<longlonglongrightarrow> x"
1686    by (metis first_countable_topology_class.countable_basis)
1687  define u where "u = (\<lambda>n. SOME z. z \<in> A n \<and> z > x)"
1688  have "\<exists>z. z \<in> U \<and> x < z" if "x \<in> U" "open U" for U
1689    using open_right[OF \<open>open U\<close> \<open>x \<in> U\<close> \<open>x < y\<close>]
1690    by (meson atLeastLessThan_iff dense less_imp_le subset_eq)
1691  then have *: "u n \<in> A n \<and> x < u n" for n
1692    using \<open>x \<in> A n\<close> \<open>open (A n)\<close> unfolding u_def by (metis (no_types, lifting) someI_ex)
1693  then have "u \<longlonglongrightarrow> x" using A(3) by simp
1694  then show ?thesis using * by auto
1695qed
1696
1697lemma approx_from_below_dense_linorder:
1698  fixes x::"'a::{dense_linorder, linorder_topology, first_countable_topology}"
1699  assumes "x > y"
1700  shows "\<exists>u. (\<forall>n. u n < x) \<and> (u \<longlonglongrightarrow> x)"
1701proof -
1702  obtain A :: "nat \<Rightarrow> 'a set" where A: "\<And>i. open (A i)" "\<And>i. x \<in> A i"
1703                                      "\<And>F. (\<forall>n. F n \<in> A n) \<Longrightarrow> F \<longlonglongrightarrow> x"
1704    by (metis first_countable_topology_class.countable_basis)
1705  define u where "u = (\<lambda>n. SOME z. z \<in> A n \<and> z < x)"
1706  have "\<exists>z. z \<in> U \<and> z < x" if "x \<in> U" "open U" for U
1707    using open_left[OF \<open>open U\<close> \<open>x \<in> U\<close> \<open>x > y\<close>]
1708    by (meson dense greaterThanAtMost_iff less_imp_le subset_eq)
1709  then have *: "u n \<in> A n \<and> u n < x" for n
1710    using \<open>x \<in> A n\<close> \<open>open (A n)\<close> unfolding u_def by (metis (no_types, lifting) someI_ex)
1711  then have "u \<longlonglongrightarrow> x" using A(3) by simp
1712  then show ?thesis using * by auto
1713qed
1714
1715
1716subsection \<open>Function limit at a point\<close>
1717
1718abbreviation LIM :: "('a::topological_space \<Rightarrow> 'b::topological_space) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool"
1719    ("((_)/ \<midarrow>(_)/\<rightarrow> (_))" [60, 0, 60] 60)
1720  where "f \<midarrow>a\<rightarrow> L \<equiv> (f \<longlongrightarrow> L) (at a)"
1721
1722lemma tendsto_within_open: "a \<in> S \<Longrightarrow> open S \<Longrightarrow> (f \<longlongrightarrow> l) (at a within S) \<longleftrightarrow> (f \<midarrow>a\<rightarrow> l)"
1723  by (simp add: tendsto_def at_within_open[where S = S])
1724
1725lemma tendsto_within_open_NO_MATCH:
1726  "a \<in> S \<Longrightarrow> NO_MATCH UNIV S \<Longrightarrow> open S \<Longrightarrow> (f \<longlongrightarrow> l)(at a within S) \<longleftrightarrow> (f \<longlongrightarrow> l)(at a)"
1727  for f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
1728  using tendsto_within_open by blast
1729
1730lemma LIM_const_not_eq[tendsto_intros]: "k \<noteq> L \<Longrightarrow> \<not> (\<lambda>x. k) \<midarrow>a\<rightarrow> L"
1731  for a :: "'a::perfect_space" and k L :: "'b::t2_space"
1732  by (simp add: tendsto_const_iff)
1733
1734lemmas LIM_not_zero = LIM_const_not_eq [where L = 0]
1735
1736lemma LIM_const_eq: "(\<lambda>x. k) \<midarrow>a\<rightarrow> L \<Longrightarrow> k = L"
1737  for a :: "'a::perfect_space" and k L :: "'b::t2_space"
1738  by (simp add: tendsto_const_iff)
1739
1740lemma LIM_unique: "f \<midarrow>a\<rightarrow> L \<Longrightarrow> f \<midarrow>a\<rightarrow> M \<Longrightarrow> L = M"
1741  for a :: "'a::perfect_space" and L M :: "'b::t2_space"
1742  using at_neq_bot by (rule tendsto_unique)
1743
1744
1745text \<open>Limits are equal for functions equal except at limit point.\<close>
1746lemma LIM_equal: "\<forall>x. x \<noteq> a \<longrightarrow> f x = g x \<Longrightarrow> (f \<midarrow>a\<rightarrow> l) \<longleftrightarrow> (g \<midarrow>a\<rightarrow> l)"
1747  by (simp add: tendsto_def eventually_at_topological)
1748
1749lemma LIM_cong: "a = b \<Longrightarrow> (\<And>x. x \<noteq> b \<Longrightarrow> f x = g x) \<Longrightarrow> l = m \<Longrightarrow> (f \<midarrow>a\<rightarrow> l) \<longleftrightarrow> (g \<midarrow>b\<rightarrow> m)"
1750  by (simp add: LIM_equal)
1751
1752lemma tendsto_cong_limit: "(f \<longlongrightarrow> l) F \<Longrightarrow> k = l \<Longrightarrow> (f \<longlongrightarrow> k) F"
1753  by simp
1754
1755lemma tendsto_at_iff_tendsto_nhds: "g \<midarrow>l\<rightarrow> g l \<longleftrightarrow> (g \<longlongrightarrow> g l) (nhds l)"
1756  unfolding tendsto_def eventually_at_filter
1757  by (intro ext all_cong imp_cong) (auto elim!: eventually_mono)
1758
1759lemma tendsto_compose: "g \<midarrow>l\<rightarrow> g l \<Longrightarrow> (f \<longlongrightarrow> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) \<longlongrightarrow> g l) F"
1760  unfolding tendsto_at_iff_tendsto_nhds by (rule filterlim_compose[of g])
1761
1762lemma tendsto_compose_eventually:
1763  "g \<midarrow>l\<rightarrow> m \<Longrightarrow> (f \<longlongrightarrow> l) F \<Longrightarrow> eventually (\<lambda>x. f x \<noteq> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) \<longlongrightarrow> m) F"
1764  by (rule filterlim_compose[of g _ "at l"]) (auto simp add: filterlim_at)
1765
1766lemma LIM_compose_eventually:
1767  assumes "f \<midarrow>a\<rightarrow> b"
1768    and "g \<midarrow>b\<rightarrow> c"
1769    and "eventually (\<lambda>x. f x \<noteq> b) (at a)"
1770  shows "(\<lambda>x. g (f x)) \<midarrow>a\<rightarrow> c"
1771  using assms(2,1,3) by (rule tendsto_compose_eventually)
1772
1773lemma tendsto_compose_filtermap: "((g \<circ> f) \<longlongrightarrow> T) F \<longleftrightarrow> (g \<longlongrightarrow> T) (filtermap f F)"
1774  by (simp add: filterlim_def filtermap_filtermap comp_def)
1775
1776lemma tendsto_compose_at:
1777  assumes f: "(f \<longlongrightarrow> y) F" and g: "(g \<longlongrightarrow> z) (at y)" and fg: "eventually (\<lambda>w. f w = y \<longrightarrow> g y = z) F"
1778  shows "((g \<circ> f) \<longlongrightarrow> z) F"
1779proof -
1780  have "(\<forall>\<^sub>F a in F. f a \<noteq> y) \<or> g y = z"
1781    using fg by force
1782  moreover have "(g \<longlongrightarrow> z) (filtermap f F) \<or> \<not> (\<forall>\<^sub>F a in F. f a \<noteq> y)"
1783    by (metis (no_types) filterlim_atI filterlim_def tendsto_mono f g)
1784  ultimately show ?thesis
1785    by (metis (no_types) f filterlim_compose filterlim_filtermap g tendsto_at_iff_tendsto_nhds tendsto_compose_filtermap)
1786qed
1787
1788
1789subsubsection \<open>Relation of \<open>LIM\<close> and \<open>LIMSEQ\<close>\<close>
1790
1791lemma (in first_countable_topology) sequentially_imp_eventually_within:
1792  "(\<forall>f. (\<forall>n. f n \<in> s \<and> f n \<noteq> a) \<and> f \<longlonglongrightarrow> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially) \<Longrightarrow>
1793    eventually P (at a within s)"
1794  unfolding at_within_def
1795  by (intro sequentially_imp_eventually_nhds_within) auto
1796
1797lemma (in first_countable_topology) sequentially_imp_eventually_at:
1798  "(\<forall>f. (\<forall>n. f n \<noteq> a) \<and> f \<longlonglongrightarrow> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially) \<Longrightarrow> eventually P (at a)"
1799  using sequentially_imp_eventually_within [where s=UNIV] by simp
1800
1801lemma LIMSEQ_SEQ_conv1:
1802  fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
1803  assumes f: "f \<midarrow>a\<rightarrow> l"
1804  shows "\<forall>S. (\<forall>n. S n \<noteq> a) \<and> S \<longlonglongrightarrow> a \<longrightarrow> (\<lambda>n. f (S n)) \<longlonglongrightarrow> l"
1805  using tendsto_compose_eventually [OF f, where F=sequentially] by simp
1806
1807lemma LIMSEQ_SEQ_conv2:
1808  fixes f :: "'a::first_countable_topology \<Rightarrow> 'b::topological_space"
1809  assumes "\<forall>S. (\<forall>n. S n \<noteq> a) \<and> S \<longlonglongrightarrow> a \<longrightarrow> (\<lambda>n. f (S n)) \<longlonglongrightarrow> l"
1810  shows "f \<midarrow>a\<rightarrow> l"
1811  using assms unfolding tendsto_def [where l=l] by (simp add: sequentially_imp_eventually_at)
1812
1813lemma LIMSEQ_SEQ_conv: "(\<forall>S. (\<forall>n. S n \<noteq> a) \<and> S \<longlonglongrightarrow> a \<longrightarrow> (\<lambda>n. X (S n)) \<longlonglongrightarrow> L) \<longleftrightarrow> X \<midarrow>a\<rightarrow> L"
1814  for a :: "'a::first_countable_topology" and L :: "'b::topological_space"
1815  using LIMSEQ_SEQ_conv2 LIMSEQ_SEQ_conv1 ..
1816
1817lemma sequentially_imp_eventually_at_left:
1818  fixes a :: "'a::{linorder_topology,first_countable_topology}"
1819  assumes b[simp]: "b < a"
1820    and *: "\<And>f. (\<And>n. b < f n) \<Longrightarrow> (\<And>n. f n < a) \<Longrightarrow> incseq f \<Longrightarrow> f \<longlonglongrightarrow> a \<Longrightarrow>
1821      eventually (\<lambda>n. P (f n)) sequentially"
1822  shows "eventually P (at_left a)"
1823proof (safe intro!: sequentially_imp_eventually_within)
1824  fix X
1825  assume X: "\<forall>n. X n \<in> {..< a} \<and> X n \<noteq> a" "X \<longlonglongrightarrow> a"
1826  show "eventually (\<lambda>n. P (X n)) sequentially"
1827  proof (rule ccontr)
1828    assume neg: "\<not> ?thesis"
1829    have "\<exists>s. \<forall>n. (\<not> P (X (s n)) \<and> b < X (s n)) \<and> (X (s n) \<le> X (s (Suc n)) \<and> Suc (s n) \<le> s (Suc n))"
1830      (is "\<exists>s. ?P s")
1831    proof (rule dependent_nat_choice)
1832      have "\<not> eventually (\<lambda>n. b < X n \<longrightarrow> P (X n)) sequentially"
1833        by (intro not_eventually_impI neg order_tendstoD(1) [OF X(2) b])
1834      then show "\<exists>x. \<not> P (X x) \<and> b < X x"
1835        by (auto dest!: not_eventuallyD)
1836    next
1837      fix x n
1838      have "\<not> eventually (\<lambda>n. Suc x \<le> n \<longrightarrow> b < X n \<longrightarrow> X x < X n \<longrightarrow> P (X n)) sequentially"
1839        using X
1840        by (intro not_eventually_impI order_tendstoD(1)[OF X(2)] eventually_ge_at_top neg) auto
1841      then show "\<exists>n. (\<not> P (X n) \<and> b < X n) \<and> (X x \<le> X n \<and> Suc x \<le> n)"
1842        by (auto dest!: not_eventuallyD)
1843    qed
1844    then obtain s where "?P s" ..
1845    with X have "b < X (s n)"
1846      and "X (s n) < a"
1847      and "incseq (\<lambda>n. X (s n))"
1848      and "(\<lambda>n. X (s n)) \<longlonglongrightarrow> a"
1849      and "\<not> P (X (s n))"
1850      for n
1851      by (auto simp: strict_mono_Suc_iff Suc_le_eq incseq_Suc_iff
1852          intro!: LIMSEQ_subseq_LIMSEQ[OF \<open>X \<longlonglongrightarrow> a\<close>, unfolded comp_def])
1853    from *[OF this(1,2,3,4)] this(5) show False
1854      by auto
1855  qed
1856qed
1857
1858lemma tendsto_at_left_sequentially:
1859  fixes a b :: "'b::{linorder_topology,first_countable_topology}"
1860  assumes "b < a"
1861  assumes *: "\<And>S. (\<And>n. S n < a) \<Longrightarrow> (\<And>n. b < S n) \<Longrightarrow> incseq S \<Longrightarrow> S \<longlonglongrightarrow> a \<Longrightarrow>
1862    (\<lambda>n. X (S n)) \<longlonglongrightarrow> L"
1863  shows "(X \<longlongrightarrow> L) (at_left a)"
1864  using assms by (simp add: tendsto_def [where l=L] sequentially_imp_eventually_at_left)
1865
1866lemma sequentially_imp_eventually_at_right:
1867  fixes a b :: "'a::{linorder_topology,first_countable_topology}"
1868  assumes b[simp]: "a < b"
1869  assumes *: "\<And>f. (\<And>n. a < f n) \<Longrightarrow> (\<And>n. f n < b) \<Longrightarrow> decseq f \<Longrightarrow> f \<longlonglongrightarrow> a \<Longrightarrow>
1870    eventually (\<lambda>n. P (f n)) sequentially"
1871  shows "eventually P (at_right a)"
1872proof (safe intro!: sequentially_imp_eventually_within)
1873  fix X
1874  assume X: "\<forall>n. X n \<in> {a <..} \<and> X n \<noteq> a" "X \<longlonglongrightarrow> a"
1875  show "eventually (\<lambda>n. P (X n)) sequentially"
1876  proof (rule ccontr)
1877    assume neg: "\<not> ?thesis"
1878    have "\<exists>s. \<forall>n. (\<not> P (X (s n)) \<and> X (s n) < b) \<and> (X (s (Suc n)) \<le> X (s n) \<and> Suc (s n) \<le> s (Suc n))"
1879      (is "\<exists>s. ?P s")
1880    proof (rule dependent_nat_choice)
1881      have "\<not> eventually (\<lambda>n. X n < b \<longrightarrow> P (X n)) sequentially"
1882        by (intro not_eventually_impI neg order_tendstoD(2) [OF X(2) b])
1883      then show "\<exists>x. \<not> P (X x) \<and> X x < b"
1884        by (auto dest!: not_eventuallyD)
1885    next
1886      fix x n
1887      have "\<not> eventually (\<lambda>n. Suc x \<le> n \<longrightarrow> X n < b \<longrightarrow> X n < X x \<longrightarrow> P (X n)) sequentially"
1888        using X
1889        by (intro not_eventually_impI order_tendstoD(2)[OF X(2)] eventually_ge_at_top neg) auto
1890      then show "\<exists>n. (\<not> P (X n) \<and> X n < b) \<and> (X n \<le> X x \<and> Suc x \<le> n)"
1891        by (auto dest!: not_eventuallyD)
1892    qed
1893    then obtain s where "?P s" ..
1894    with X have "a < X (s n)"
1895      and "X (s n) < b"
1896      and "decseq (\<lambda>n. X (s n))"
1897      and "(\<lambda>n. X (s n)) \<longlonglongrightarrow> a"
1898      and "\<not> P (X (s n))"
1899      for n
1900      by (auto simp: strict_mono_Suc_iff Suc_le_eq decseq_Suc_iff
1901          intro!: LIMSEQ_subseq_LIMSEQ[OF \<open>X \<longlonglongrightarrow> a\<close>, unfolded comp_def])
1902    from *[OF this(1,2,3,4)] this(5) show False
1903      by auto
1904  qed
1905qed
1906
1907lemma tendsto_at_right_sequentially:
1908  fixes a :: "_ :: {linorder_topology, first_countable_topology}"
1909  assumes "a < b"
1910    and *: "\<And>S. (\<And>n. a < S n) \<Longrightarrow> (\<And>n. S n < b) \<Longrightarrow> decseq S \<Longrightarrow> S \<longlonglongrightarrow> a \<Longrightarrow>
1911      (\<lambda>n. X (S n)) \<longlonglongrightarrow> L"
1912  shows "(X \<longlongrightarrow> L) (at_right a)"
1913  using assms by (simp add: tendsto_def [where l=L] sequentially_imp_eventually_at_right)
1914
1915
1916subsection \<open>Continuity\<close>
1917
1918subsubsection \<open>Continuity on a set\<close>
1919
1920definition continuous_on :: "'a set \<Rightarrow> ('a::topological_space \<Rightarrow> 'b::topological_space) \<Rightarrow> bool"
1921  where "continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. (f \<longlongrightarrow> f x) (at x within s))"
1922
1923lemma continuous_on_cong [cong]:
1924  "s = t \<Longrightarrow> (\<And>x. x \<in> t \<Longrightarrow> f x = g x) \<Longrightarrow> continuous_on s f \<longleftrightarrow> continuous_on t g"
1925  unfolding continuous_on_def
1926  by (intro ball_cong filterlim_cong) (auto simp: eventually_at_filter)
1927
1928lemma continuous_on_cong_simp:
1929  "s = t \<Longrightarrow> (\<And>x. x \<in> t =simp=> f x = g x) \<Longrightarrow> continuous_on s f \<longleftrightarrow> continuous_on t g"
1930  unfolding simp_implies_def by (rule continuous_on_cong)
1931
1932lemma continuous_on_topological:
1933  "continuous_on s f \<longleftrightarrow>
1934    (\<forall>x\<in>s. \<forall>B. open B \<longrightarrow> f x \<in> B \<longrightarrow> (\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)))"
1935  unfolding continuous_on_def tendsto_def eventually_at_topological by metis
1936
1937lemma continuous_on_open_invariant:
1938  "continuous_on s f \<longleftrightarrow> (\<forall>B. open B \<longrightarrow> (\<exists>A. open A \<and> A \<inter> s = f -` B \<inter> s))"
1939proof safe
1940  fix B :: "'b set"
1941  assume "continuous_on s f" "open B"
1942  then have "\<forall>x\<in>f -` B \<inter> s. (\<exists>A. open A \<and> x \<in> A \<and> s \<inter> A \<subseteq> f -` B)"
1943    by (auto simp: continuous_on_topological subset_eq Ball_def imp_conjL)
1944  then obtain A where "\<forall>x\<in>f -` B \<inter> s. open (A x) \<and> x \<in> A x \<and> s \<inter> A x \<subseteq> f -` B"
1945    unfolding bchoice_iff ..
1946  then show "\<exists>A. open A \<and> A \<inter> s = f -` B \<inter> s"
1947    by (intro exI[of _ "\<Union>x\<in>f -` B \<inter> s. A x"]) auto
1948next
1949  assume B: "\<forall>B. open B \<longrightarrow> (\<exists>A. open A \<and> A \<inter> s = f -` B \<inter> s)"
1950  show "continuous_on s f"
1951    unfolding continuous_on_topological
1952  proof safe
1953    fix x B
1954    assume "x \<in> s" "open B" "f x \<in> B"
1955    with B obtain A where A: "open A" "A \<inter> s = f -` B \<inter> s"
1956      by auto
1957    with \<open>x \<in> s\<close> \<open>f x \<in> B\<close> show "\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)"
1958      by (intro exI[of _ A]) auto
1959  qed
1960qed
1961
1962lemma continuous_on_open_vimage:
1963  "open s \<Longrightarrow> continuous_on s f \<longleftrightarrow> (\<forall>B. open B \<longrightarrow> open (f -` B \<inter> s))"
1964  unfolding continuous_on_open_invariant
1965  by (metis open_Int Int_absorb Int_commute[of s] Int_assoc[of _ _ s])
1966
1967corollary continuous_imp_open_vimage:
1968  assumes "continuous_on s f" "open s" "open B" "f -` B \<subseteq> s"
1969  shows "open (f -` B)"
1970  by (metis assms continuous_on_open_vimage le_iff_inf)
1971
1972corollary open_vimage[continuous_intros]:
1973  assumes "open s"
1974    and "continuous_on UNIV f"
1975  shows "open (f -` s)"
1976  using assms by (simp add: continuous_on_open_vimage [OF open_UNIV])
1977
1978lemma continuous_on_closed_invariant:
1979  "continuous_on s f \<longleftrightarrow> (\<forall>B. closed B \<longrightarrow> (\<exists>A. closed A \<and> A \<inter> s = f -` B \<inter> s))"
1980proof -
1981  have *: "(\<And>A. P A \<longleftrightarrow> Q (- A)) \<Longrightarrow> (\<forall>A. P A) \<longleftrightarrow> (\<forall>A. Q A)"
1982    for P Q :: "'b set \<Rightarrow> bool"
1983    by (metis double_compl)
1984  show ?thesis
1985    unfolding continuous_on_open_invariant
1986    by (intro *) (auto simp: open_closed[symmetric])
1987qed
1988
1989lemma continuous_on_closed_vimage:
1990  "closed s \<Longrightarrow> continuous_on s f \<longleftrightarrow> (\<forall>B. closed B \<longrightarrow> closed (f -` B \<inter> s))"
1991  unfolding continuous_on_closed_invariant
1992  by (metis closed_Int Int_absorb Int_commute[of s] Int_assoc[of _ _ s])
1993
1994corollary closed_vimage_Int[continuous_intros]:
1995  assumes "closed s"
1996    and "continuous_on t f"
1997    and t: "closed t"
1998  shows "closed (f -` s \<inter> t)"
1999  using assms by (simp add: continuous_on_closed_vimage [OF t])
2000
2001corollary closed_vimage[continuous_intros]:
2002  assumes "closed s"
2003    and "continuous_on UNIV f"
2004  shows "closed (f -` s)"
2005  using closed_vimage_Int [OF assms] by simp
2006
2007lemma continuous_on_empty [simp]: "continuous_on {} f"
2008  by (simp add: continuous_on_def)
2009
2010lemma continuous_on_sing [simp]: "continuous_on {x} f"
2011  by (simp add: continuous_on_def at_within_def)
2012
2013lemma continuous_on_open_Union:
2014  "(\<And>s. s \<in> S \<Longrightarrow> open s) \<Longrightarrow> (\<And>s. s \<in> S \<Longrightarrow> continuous_on s f) \<Longrightarrow> continuous_on (\<Union>S) f"
2015  unfolding continuous_on_def
2016  by safe (metis open_Union at_within_open UnionI)
2017
2018lemma continuous_on_open_UN:
2019  "(\<And>s. s \<in> S \<Longrightarrow> open (A s)) \<Longrightarrow> (\<And>s. s \<in> S \<Longrightarrow> continuous_on (A s) f) \<Longrightarrow>
2020    continuous_on (\<Union>s\<in>S. A s) f"
2021  by (rule continuous_on_open_Union) auto
2022
2023lemma continuous_on_open_Un:
2024  "open s \<Longrightarrow> open t \<Longrightarrow> continuous_on s f \<Longrightarrow> continuous_on t f \<Longrightarrow> continuous_on (s \<union> t) f"
2025  using continuous_on_open_Union [of "{s,t}"] by auto
2026
2027lemma continuous_on_closed_Un:
2028  "closed s \<Longrightarrow> closed t \<Longrightarrow> continuous_on s f \<Longrightarrow> continuous_on t f \<Longrightarrow> continuous_on (s \<union> t) f"
2029  by (auto simp add: continuous_on_closed_vimage closed_Un Int_Un_distrib)
2030
2031lemma continuous_on_closed_Union:
2032  assumes "finite I"
2033    "\<And>i. i \<in> I \<Longrightarrow> closed (U i)"
2034    "\<And>i. i \<in> I \<Longrightarrow> continuous_on (U i) f"
2035  shows "continuous_on (\<Union> i \<in> I. U i) f"
2036  using assms
2037  by (induction I) (auto intro!: continuous_on_closed_Un)
2038
2039lemma continuous_on_If:
2040  assumes closed: "closed s" "closed t"
2041    and cont: "continuous_on s f" "continuous_on t g"
2042    and P: "\<And>x. x \<in> s \<Longrightarrow> \<not> P x \<Longrightarrow> f x = g x" "\<And>x. x \<in> t \<Longrightarrow> P x \<Longrightarrow> f x = g x"
2043  shows "continuous_on (s \<union> t) (\<lambda>x. if P x then f x else g x)"
2044    (is "continuous_on _ ?h")
2045proof-
2046  from P have "\<forall>x\<in>s. f x = ?h x" "\<forall>x\<in>t. g x = ?h x"
2047    by auto
2048  with cont have "continuous_on s ?h" "continuous_on t ?h"
2049    by simp_all
2050  with closed show ?thesis
2051    by (rule continuous_on_closed_Un)
2052qed
2053
2054lemma continuous_on_cases:
2055  "closed s \<Longrightarrow> closed t \<Longrightarrow> continuous_on s f \<Longrightarrow> continuous_on t g \<Longrightarrow>
2056    \<forall>x. (x\<in>s \<and> \<not> P x) \<or> (x \<in> t \<and> P x) \<longrightarrow> f x = g x \<Longrightarrow>
2057    continuous_on (s \<union> t) (\<lambda>x. if P x then f x else g x)"
2058  by (rule continuous_on_If) auto
2059
2060lemma continuous_on_id[continuous_intros,simp]: "continuous_on s (\<lambda>x. x)"
2061  unfolding continuous_on_def by fast
2062
2063lemma continuous_on_id'[continuous_intros,simp]: "continuous_on s id"
2064  unfolding continuous_on_def id_def by fast
2065
2066lemma continuous_on_const[continuous_intros,simp]: "continuous_on s (\<lambda>x. c)"
2067  unfolding continuous_on_def by auto
2068
2069lemma continuous_on_subset: "continuous_on s f \<Longrightarrow> t \<subseteq> s \<Longrightarrow> continuous_on t f"
2070  unfolding continuous_on_def
2071  by (metis subset_eq tendsto_within_subset)
2072
2073lemma continuous_on_compose[continuous_intros]:
2074  "continuous_on s f \<Longrightarrow> continuous_on (f ` s) g \<Longrightarrow> continuous_on s (g \<circ> f)"
2075  unfolding continuous_on_topological by simp metis
2076
2077lemma continuous_on_compose2:
2078  "continuous_on t g \<Longrightarrow> continuous_on s f \<Longrightarrow> f ` s \<subseteq> t \<Longrightarrow> continuous_on s (\<lambda>x. g (f x))"
2079  using continuous_on_compose[of s f g] continuous_on_subset by (force simp add: comp_def)
2080
2081lemma continuous_on_generate_topology:
2082  assumes *: "open = generate_topology X"
2083    and **: "\<And>B. B \<in> X \<Longrightarrow> \<exists>C. open C \<and> C \<inter> A = f -` B \<inter> A"
2084  shows "continuous_on A f"
2085  unfolding continuous_on_open_invariant
2086proof safe
2087  fix B :: "'a set"
2088  assume "open B"
2089  then show "\<exists>C. open C \<and> C \<inter> A = f -` B \<inter> A"
2090    unfolding *
2091  proof induct
2092    case (UN K)
2093    then obtain C where "\<And>k. k \<in> K \<Longrightarrow> open (C k)" "\<And>k. k \<in> K \<Longrightarrow> C k \<inter> A = f -` k \<inter> A"
2094      by metis
2095    then show ?case
2096      by (intro exI[of _ "\<Union>k\<in>K. C k"]) blast
2097  qed (auto intro: **)
2098qed
2099
2100lemma continuous_onI_mono:
2101  fixes f :: "'a::linorder_topology \<Rightarrow> 'b::{dense_order,linorder_topology}"
2102  assumes "open (f`A)"
2103    and mono: "\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
2104  shows "continuous_on A f"
2105proof (rule continuous_on_generate_topology[OF open_generated_order], safe)
2106  have monoD: "\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> f x < f y \<Longrightarrow> x < y"
2107    by (auto simp: not_le[symmetric] mono)
2108  have "\<exists>x. x \<in> A \<and> f x < b \<and> a < x" if a: "a \<in> A" and fa: "f a < b" for a b
2109  proof -
2110    obtain y where "f a < y" "{f a ..< y} \<subseteq> f`A"
2111      using open_right[OF \<open>open (f`A)\<close>, of "f a" b] a fa
2112      by auto
2113    obtain z where z: "f a < z" "z < min b y"
2114      using dense[of "f a" "min b y"] \<open>f a < y\<close> \<open>f a < b\<close> by auto
2115    then obtain c where "z = f c" "c \<in> A"
2116      using \<open>{f a ..< y} \<subseteq> f`A\<close>[THEN subsetD, of z] by (auto simp: less_imp_le)
2117    with a z show ?thesis
2118      by (auto intro!: exI[of _ c] simp: monoD)
2119  qed
2120  then show "\<exists>C. open C \<and> C \<inter> A = f -` {..<b} \<inter> A" for b
2121    by (intro exI[of _ "(\<Union>x\<in>{x\<in>A. f x < b}. {..< x})"])
2122       (auto intro: le_less_trans[OF mono] less_imp_le)
2123
2124  have "\<exists>x. x \<in> A \<and> b < f x \<and> x < a" if a: "a \<in> A" and fa: "b < f a" for a b
2125  proof -
2126    note a fa
2127    moreover
2128    obtain y where "y < f a" "{y <.. f a} \<subseteq> f`A"
2129      using open_left[OF \<open>open (f`A)\<close>, of "f a" b]  a fa
2130      by auto
2131    then obtain z where z: "max b y < z" "z < f a"
2132      using dense[of "max b y" "f a"] \<open>y < f a\<close> \<open>b < f a\<close> by auto
2133    then obtain c where "z = f c" "c \<in> A"
2134      using \<open>{y <.. f a} \<subseteq> f`A\<close>[THEN subsetD, of z] by (auto simp: less_imp_le)
2135    with a z show ?thesis
2136      by (auto intro!: exI[of _ c] simp: monoD)
2137  qed
2138  then show "\<exists>C. open C \<and> C \<inter> A = f -` {b <..} \<inter> A" for b
2139    by (intro exI[of _ "(\<Union>x\<in>{x\<in>A. b < f x}. {x <..})"])
2140       (auto intro: less_le_trans[OF _ mono] less_imp_le)
2141qed
2142
2143lemma continuous_on_IccI:
2144  "\<lbrakk>(f \<longlongrightarrow> f a) (at_right a);
2145    (f \<longlongrightarrow> f b) (at_left b);
2146    (\<And>x. a < x \<Longrightarrow> x < b \<Longrightarrow> f \<midarrow>x\<rightarrow> f x); a < b\<rbrakk> \<Longrightarrow>
2147    continuous_on {a .. b} f"
2148  for a::"'a::linorder_topology"
2149  using at_within_open[of _ "{a<..<b}"]
2150  by (auto simp: continuous_on_def at_within_Icc_at_right at_within_Icc_at_left le_less
2151      at_within_Icc_at)
2152
2153lemma
2154  fixes a b::"'a::linorder_topology"
2155  assumes "continuous_on {a .. b} f" "a < b"
2156  shows continuous_on_Icc_at_rightD: "(f \<longlongrightarrow> f a) (at_right a)"
2157    and continuous_on_Icc_at_leftD: "(f \<longlongrightarrow> f b) (at_left b)"
2158  using assms
2159  by (auto simp: at_within_Icc_at_right at_within_Icc_at_left continuous_on_def
2160      dest: bspec[where x=a] bspec[where x=b])
2161
2162lemma continuous_on_discrete [simp]:
2163  "continuous_on A (f :: 'a :: discrete_topology \<Rightarrow> _)"
2164  by (auto simp: continuous_on_def at_discrete)
2165
2166subsubsection \<open>Continuity at a point\<close>
2167
2168definition continuous :: "'a::t2_space filter \<Rightarrow> ('a \<Rightarrow> 'b::topological_space) \<Rightarrow> bool"
2169  where "continuous F f \<longleftrightarrow> (f \<longlongrightarrow> f (Lim F (\<lambda>x. x))) F"
2170
2171lemma continuous_bot[continuous_intros, simp]: "continuous bot f"
2172  unfolding continuous_def by auto
2173
2174lemma continuous_trivial_limit: "trivial_limit net \<Longrightarrow> continuous net f"
2175  by simp
2176
2177lemma continuous_within: "continuous (at x within s) f \<longleftrightarrow> (f \<longlongrightarrow> f x) (at x within s)"
2178  by (cases "trivial_limit (at x within s)") (auto simp add: Lim_ident_at continuous_def)
2179
2180lemma continuous_within_topological:
2181  "continuous (at x within s) f \<longleftrightarrow>
2182    (\<forall>B. open B \<longrightarrow> f x \<in> B \<longrightarrow> (\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)))"
2183  unfolding continuous_within tendsto_def eventually_at_topological by metis
2184
2185lemma continuous_within_compose[continuous_intros]:
2186  "continuous (at x within s) f \<Longrightarrow> continuous (at (f x) within f ` s) g \<Longrightarrow>
2187    continuous (at x within s) (g \<circ> f)"
2188  by (simp add: continuous_within_topological) metis
2189
2190lemma continuous_within_compose2:
2191  "continuous (at x within s) f \<Longrightarrow> continuous (at (f x) within f ` s) g \<Longrightarrow>
2192    continuous (at x within s) (\<lambda>x. g (f x))"
2193  using continuous_within_compose[of x s f g] by (simp add: comp_def)
2194
2195lemma continuous_at: "continuous (at x) f \<longleftrightarrow> f \<midarrow>x\<rightarrow> f x"
2196  using continuous_within[of x UNIV f] by simp
2197
2198lemma continuous_ident[continuous_intros, simp]: "continuous (at x within S) (\<lambda>x. x)"
2199  unfolding continuous_within by (rule tendsto_ident_at)
2200
2201lemma continuous_id[continuous_intros, simp]: "continuous (at x within S) id"
2202  by (simp add: id_def)
2203
2204lemma continuous_const[continuous_intros, simp]: "continuous F (\<lambda>x. c)"
2205  unfolding continuous_def by (rule tendsto_const)
2206
2207lemma continuous_on_eq_continuous_within:
2208  "continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. continuous (at x within s) f)"
2209  unfolding continuous_on_def continuous_within ..
2210
2211lemma continuous_discrete [simp]:
2212  "continuous (at x within A) (f :: 'a :: discrete_topology \<Rightarrow> _)"
2213  by (auto simp: continuous_def at_discrete)
2214
2215abbreviation isCont :: "('a::t2_space \<Rightarrow> 'b::topological_space) \<Rightarrow> 'a \<Rightarrow> bool"
2216  where "isCont f a \<equiv> continuous (at a) f"
2217
2218lemma isCont_def: "isCont f a \<longleftrightarrow> f \<midarrow>a\<rightarrow> f a"
2219  by (rule continuous_at)
2220
2221lemma isContD: "isCont f x \<Longrightarrow> f \<midarrow>x\<rightarrow> f x"
2222  by (simp add: isCont_def)
2223
2224lemma isCont_cong:
2225  assumes "eventually (\<lambda>x. f x = g x) (nhds x)"
2226  shows "isCont f x \<longleftrightarrow> isCont g x"
2227proof -
2228  from assms have [simp]: "f x = g x"
2229    by (rule eventually_nhds_x_imp_x)
2230  from assms have "eventually (\<lambda>x. f x = g x) (at x)"
2231    by (auto simp: eventually_at_filter elim!: eventually_mono)
2232  with assms have "isCont f x \<longleftrightarrow> isCont g x" unfolding isCont_def
2233    by (intro filterlim_cong) (auto elim!: eventually_mono)
2234  with assms show ?thesis by simp
2235qed
2236
2237lemma continuous_at_imp_continuous_at_within: "isCont f x \<Longrightarrow> continuous (at x within s) f"
2238  by (auto intro: tendsto_mono at_le simp: continuous_at continuous_within)
2239
2240lemma continuous_on_eq_continuous_at: "open s \<Longrightarrow> continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. isCont f x)"
2241  by (simp add: continuous_on_def continuous_at at_within_open[of _ s])
2242
2243lemma continuous_within_open: "a \<in> A \<Longrightarrow> open A \<Longrightarrow> continuous (at a within A) f \<longleftrightarrow> isCont f a"
2244  by (simp add: at_within_open_NO_MATCH)
2245
2246lemma continuous_at_imp_continuous_on: "\<forall>x\<in>s. isCont f x \<Longrightarrow> continuous_on s f"
2247  by (auto intro: continuous_at_imp_continuous_at_within simp: continuous_on_eq_continuous_within)
2248
2249lemma isCont_o2: "isCont f a \<Longrightarrow> isCont g (f a) \<Longrightarrow> isCont (\<lambda>x. g (f x)) a"
2250  unfolding isCont_def by (rule tendsto_compose)
2251
2252lemma continuous_at_compose[continuous_intros]: "isCont f a \<Longrightarrow> isCont g (f a) \<Longrightarrow> isCont (g \<circ> f) a"
2253  unfolding o_def by (rule isCont_o2)
2254
2255lemma isCont_tendsto_compose: "isCont g l \<Longrightarrow> (f \<longlongrightarrow> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) \<longlongrightarrow> g l) F"
2256  unfolding isCont_def by (rule tendsto_compose)
2257
2258lemma continuous_on_tendsto_compose:
2259  assumes f_cont: "continuous_on s f"
2260    and g: "(g \<longlongrightarrow> l) F"
2261    and l: "l \<in> s"
2262    and ev: "\<forall>\<^sub>Fx in F. g x \<in> s"
2263  shows "((\<lambda>x. f (g x)) \<longlongrightarrow> f l) F"
2264proof -
2265  from f_cont l have f: "(f \<longlongrightarrow> f l) (at l within s)"
2266    by (simp add: continuous_on_def)
2267  have i: "((\<lambda>x. if g x = l then f l else f (g x)) \<longlongrightarrow> f l) F"
2268    by (rule filterlim_If)
2269       (auto intro!: filterlim_compose[OF f] eventually_conj tendsto_mono[OF _ g]
2270             simp: filterlim_at eventually_inf_principal eventually_mono[OF ev])
2271  show ?thesis
2272    by (rule filterlim_cong[THEN iffD1[OF _ i]]) auto
2273qed
2274
2275lemma continuous_within_compose3:
2276  "isCont g (f x) \<Longrightarrow> continuous (at x within s) f \<Longrightarrow> continuous (at x within s) (\<lambda>x. g (f x))"
2277  using continuous_at_imp_continuous_at_within continuous_within_compose2 by blast
2278
2279lemma filtermap_nhds_open_map:
2280  assumes cont: "isCont f a"
2281    and open_map: "\<And>S. open S \<Longrightarrow> open (f`S)"
2282  shows "filtermap f (nhds a) = nhds (f a)"
2283  unfolding filter_eq_iff
2284proof safe
2285  fix P
2286  assume "eventually P (filtermap f (nhds a))"
2287  then obtain S where "open S" "a \<in> S" "\<forall>x\<in>S. P (f x)"
2288    by (auto simp: eventually_filtermap eventually_nhds)
2289  then show "eventually P (nhds (f a))"
2290    unfolding eventually_nhds by (intro exI[of _ "f`S"]) (auto intro!: open_map)
2291qed (metis filterlim_iff tendsto_at_iff_tendsto_nhds isCont_def eventually_filtermap cont)
2292
2293lemma continuous_at_split:
2294  "continuous (at x) f \<longleftrightarrow> continuous (at_left x) f \<and> continuous (at_right x) f"
2295  for x :: "'a::linorder_topology"
2296  by (simp add: continuous_within filterlim_at_split)
2297
2298lemma continuous_on_max [continuous_intros]:
2299  fixes f g :: "'a::topological_space \<Rightarrow> 'b::linorder_topology"
2300  shows "continuous_on A f \<Longrightarrow> continuous_on A g \<Longrightarrow> continuous_on A (\<lambda>x. max (f x) (g x))"
2301  by (auto simp: continuous_on_def intro!: tendsto_max)
2302
2303lemma continuous_on_min [continuous_intros]:
2304  fixes f g :: "'a::topological_space \<Rightarrow> 'b::linorder_topology"
2305  shows "continuous_on A f \<Longrightarrow> continuous_on A g \<Longrightarrow> continuous_on A (\<lambda>x. min (f x) (g x))"
2306  by (auto simp: continuous_on_def intro!: tendsto_min)
2307
2308lemma continuous_max [continuous_intros]:
2309  fixes f :: "'a::t2_space \<Rightarrow> 'b::linorder_topology"
2310  shows "\<lbrakk>continuous F f; continuous F g\<rbrakk> \<Longrightarrow> continuous F (\<lambda>x. (max (f x) (g x)))"
2311  by (simp add: tendsto_max continuous_def)
2312
2313lemma continuous_min [continuous_intros]:
2314  fixes f :: "'a::t2_space \<Rightarrow> 'b::linorder_topology"
2315  shows "\<lbrakk>continuous F f; continuous F g\<rbrakk> \<Longrightarrow> continuous F (\<lambda>x. (min (f x) (g x)))"
2316  by (simp add: tendsto_min continuous_def)
2317
2318text \<open>
2319  The following open/closed Collect lemmas are ported from
2320  S��bastien Gou��zel's \<open>Ergodic_Theory\<close>.
2321\<close>
2322lemma open_Collect_neq:
2323  fixes f g :: "'a::topological_space \<Rightarrow> 'b::t2_space"
2324  assumes f: "continuous_on UNIV f" and g: "continuous_on UNIV g"
2325  shows "open {x. f x \<noteq> g x}"
2326proof (rule openI)
2327  fix t
2328  assume "t \<in> {x. f x \<noteq> g x}"
2329  then obtain U V where *: "open U" "open V" "f t \<in> U" "g t \<in> V" "U \<inter> V = {}"
2330    by (auto simp add: separation_t2)
2331  with open_vimage[OF \<open>open U\<close> f] open_vimage[OF \<open>open V\<close> g]
2332  show "\<exists>T. open T \<and> t \<in> T \<and> T \<subseteq> {x. f x \<noteq> g x}"
2333    by (intro exI[of _ "f -` U \<inter> g -` V"]) auto
2334qed
2335
2336lemma closed_Collect_eq:
2337  fixes f g :: "'a::topological_space \<Rightarrow> 'b::t2_space"
2338  assumes f: "continuous_on UNIV f" and g: "continuous_on UNIV g"
2339  shows "closed {x. f x = g x}"
2340  using open_Collect_neq[OF f g] by (simp add: closed_def Collect_neg_eq)
2341
2342lemma open_Collect_less:
2343  fixes f g :: "'a::topological_space \<Rightarrow> 'b::linorder_topology"
2344  assumes f: "continuous_on UNIV f" and g: "continuous_on UNIV g"
2345  shows "open {x. f x < g x}"
2346proof (rule openI)
2347  fix t
2348  assume t: "t \<in> {x. f x < g x}"
2349  show "\<exists>T. open T \<and> t \<in> T \<and> T \<subseteq> {x. f x < g x}"
2350  proof (cases "\<exists>z. f t < z \<and> z < g t")
2351    case True
2352    then obtain z where "f t < z \<and> z < g t" by blast
2353    then show ?thesis
2354      using open_vimage[OF _ f, of "{..< z}"] open_vimage[OF _ g, of "{z <..}"]
2355      by (intro exI[of _ "f -` {..<z} \<inter> g -` {z<..}"]) auto
2356  next
2357    case False
2358    then have *: "{g t ..} = {f t <..}" "{..< g t} = {.. f t}"
2359      using t by (auto intro: leI)
2360    show ?thesis
2361      using open_vimage[OF _ f, of "{..< g t}"] open_vimage[OF _ g, of "{f t <..}"] t
2362      apply (intro exI[of _ "f -` {..< g t} \<inter> g -` {f t<..}"])
2363      apply (simp add: open_Int)
2364      apply (auto simp add: *)
2365      done
2366  qed
2367qed
2368
2369lemma closed_Collect_le:
2370  fixes f g :: "'a :: topological_space \<Rightarrow> 'b::linorder_topology"
2371  assumes f: "continuous_on UNIV f"
2372    and g: "continuous_on UNIV g"
2373  shows "closed {x. f x \<le> g x}"
2374  using open_Collect_less [OF g f]
2375  by (simp add: closed_def Collect_neg_eq[symmetric] not_le)
2376
2377
2378subsubsection \<open>Open-cover compactness\<close>
2379
2380context topological_space
2381begin
2382
2383definition compact :: "'a set \<Rightarrow> bool" where
2384compact_eq_Heine_Borel:  (* This name is used for backwards compatibility *)
2385    "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))"
2386
2387lemma compactI:
2388  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'"
2389  shows "compact s"
2390  unfolding compact_eq_Heine_Borel using assms by metis
2391
2392lemma compact_empty[simp]: "compact {}"
2393  by (auto intro!: compactI)
2394
2395lemma compactE: (*related to COMPACT_IMP_HEINE_BOREL in HOL Light*)
2396  assumes "compact S" "S \<subseteq> \<Union>\<T>" "\<And>B. B \<in> \<T> \<Longrightarrow> open B"
2397  obtains \<T>' where "\<T>' \<subseteq> \<T>" "finite \<T>'" "S \<subseteq> \<Union>\<T>'"
2398  by (meson assms compact_eq_Heine_Borel)
2399
2400lemma compactE_image:
2401  assumes "compact S"
2402    and opn: "\<And>T. T \<in> C \<Longrightarrow> open (f T)"
2403    and S: "S \<subseteq> (\<Union>c\<in>C. f c)"
2404  obtains C' where "C' \<subseteq> C" and "finite C'" and "S \<subseteq> (\<Union>c\<in>C'. f c)"
2405    apply (rule compactE[OF \<open>compact S\<close> S])
2406    using opn apply force
2407    by (metis finite_subset_image)
2408
2409lemma compact_Int_closed [intro]:
2410  assumes "compact S"
2411    and "closed T"
2412  shows "compact (S \<inter> T)"
2413proof (rule compactI)
2414  fix C
2415  assume C: "\<forall>c\<in>C. open c"
2416  assume cover: "S \<inter> T \<subseteq> \<Union>C"
2417  from C \<open>closed T\<close> have "\<forall>c\<in>C \<union> {- T}. open c"
2418    by auto
2419  moreover from cover have "S \<subseteq> \<Union>(C \<union> {- T})"
2420    by auto
2421  ultimately have "\<exists>D\<subseteq>C \<union> {- T}. finite D \<and> S \<subseteq> \<Union>D"
2422    using \<open>compact S\<close> unfolding compact_eq_Heine_Borel by auto
2423  then obtain D where "D \<subseteq> C \<union> {- T} \<and> finite D \<and> S \<subseteq> \<Union>D" ..
2424  then show "\<exists>D\<subseteq>C. finite D \<and> S \<inter> T \<subseteq> \<Union>D"
2425    by (intro exI[of _ "D - {-T}"]) auto
2426qed
2427
2428lemma compact_diff: "\<lbrakk>compact S; open T\<rbrakk> \<Longrightarrow> compact(S - T)"
2429  by (simp add: Diff_eq compact_Int_closed open_closed)
2430
2431lemma inj_setminus: "inj_on uminus (A::'a set set)"
2432  by (auto simp: inj_on_def)
2433
2434
2435subsection \<open>Finite intersection property\<close>
2436
2437lemma compact_fip:
2438  "compact U \<longleftrightarrow>
2439    (\<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> {})"
2440  (is "_ \<longleftrightarrow> ?R")
2441proof (safe intro!: compact_eq_Heine_Borel[THEN iffD2])
2442  fix A
2443  assume "compact U"
2444  assume A: "\<forall>a\<in>A. closed a" "U \<inter> \<Inter>A = {}"
2445  assume fin: "\<forall>B \<subseteq> A. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {}"
2446  from A have "(\<forall>a\<in>uminus`A. open a) \<and> U \<subseteq> \<Union>(uminus`A)"
2447    by auto
2448  with \<open>compact U\<close> obtain B where "B \<subseteq> A" "finite (uminus`B)" "U \<subseteq> \<Union>(uminus`B)"
2449    unfolding compact_eq_Heine_Borel by (metis subset_image_iff)
2450  with fin[THEN spec, of B] show False
2451    by (auto dest: finite_imageD intro: inj_setminus)
2452next
2453  fix A
2454  assume ?R
2455  assume "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A"
2456  then have "U \<inter> \<Inter>(uminus`A) = {}" "\<forall>a\<in>uminus`A. closed a"
2457    by auto
2458  with \<open>?R\<close> obtain B where "B \<subseteq> A" "finite (uminus`B)" "U \<inter> \<Inter>(uminus`B) = {}"
2459    by (metis subset_image_iff)
2460  then show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"
2461    by (auto intro!: exI[of _ B] inj_setminus dest: finite_imageD)
2462qed
2463
2464lemma compact_imp_fip:
2465  assumes "compact S"
2466    and "\<And>T. T \<in> F \<Longrightarrow> closed T"
2467    and "\<And>F'. finite F' \<Longrightarrow> F' \<subseteq> F \<Longrightarrow> S \<inter> (\<Inter>F') \<noteq> {}"
2468  shows "S \<inter> (\<Inter>F) \<noteq> {}"
2469  using assms unfolding compact_fip by auto
2470
2471lemma compact_imp_fip_image:
2472  assumes "compact s"
2473    and P: "\<And>i. i \<in> I \<Longrightarrow> closed (f i)"
2474    and Q: "\<And>I'. finite I' \<Longrightarrow> I' \<subseteq> I \<Longrightarrow> (s \<inter> (\<Inter>i\<in>I'. f i) \<noteq> {})"
2475  shows "s \<inter> (\<Inter>i\<in>I. f i) \<noteq> {}"
2476proof -
2477  note \<open>compact s\<close>
2478  moreover from P have "\<forall>i \<in> f ` I. closed i"
2479    by blast
2480  moreover have "\<forall>A. finite A \<and> A \<subseteq> f ` I \<longrightarrow> (s \<inter> (\<Inter>A) \<noteq> {})"
2481    apply rule
2482    apply rule
2483    apply (erule conjE)
2484  proof -
2485    fix A :: "'a set set"
2486    assume "finite A" and "A \<subseteq> f ` I"
2487    then obtain B where "B \<subseteq> I" and "finite B" and "A = f ` B"
2488      using finite_subset_image [of A f I] by blast
2489    with Q [of B] show "s \<inter> \<Inter>A \<noteq> {}"
2490      by simp
2491  qed
2492  ultimately have "s \<inter> (\<Inter>(f ` I)) \<noteq> {}"
2493    by (metis compact_imp_fip)
2494  then show ?thesis by simp
2495qed
2496
2497end
2498
2499lemma (in t2_space) compact_imp_closed:
2500  assumes "compact s"
2501  shows "closed s"
2502  unfolding closed_def
2503proof (rule openI)
2504  fix y
2505  assume "y \<in> - s"
2506  let ?C = "\<Union>x\<in>s. {u. open u \<and> x \<in> u \<and> eventually (\<lambda>y. y \<notin> u) (nhds y)}"
2507  have "s \<subseteq> \<Union>?C"
2508  proof
2509    fix x
2510    assume "x \<in> s"
2511    with \<open>y \<in> - s\<close> have "x \<noteq> y" by clarsimp
2512    then have "\<exists>u v. open u \<and> open v \<and> x \<in> u \<and> y \<in> v \<and> u \<inter> v = {}"
2513      by (rule hausdorff)
2514    with \<open>x \<in> s\<close> show "x \<in> \<Union>?C"
2515      unfolding eventually_nhds by auto
2516  qed
2517  then obtain D where "D \<subseteq> ?C" and "finite D" and "s \<subseteq> \<Union>D"
2518    by (rule compactE [OF \<open>compact s\<close>]) auto
2519  from \<open>D \<subseteq> ?C\<close> have "\<forall>x\<in>D. eventually (\<lambda>y. y \<notin> x) (nhds y)"
2520    by auto
2521  with \<open>finite D\<close> have "eventually (\<lambda>y. y \<notin> \<Union>D) (nhds y)"
2522    by (simp add: eventually_ball_finite)
2523  with \<open>s \<subseteq> \<Union>D\<close> have "eventually (\<lambda>y. y \<notin> s) (nhds y)"
2524    by (auto elim!: eventually_mono)
2525  then show "\<exists>t. open t \<and> y \<in> t \<and> t \<subseteq> - s"
2526    by (simp add: eventually_nhds subset_eq)
2527qed
2528
2529lemma compact_continuous_image:
2530  assumes f: "continuous_on s f"
2531    and s: "compact s"
2532  shows "compact (f ` s)"
2533proof (rule compactI)
2534  fix C
2535  assume "\<forall>c\<in>C. open c" and cover: "f`s \<subseteq> \<Union>C"
2536  with f have "\<forall>c\<in>C. \<exists>A. open A \<and> A \<inter> s = f -` c \<inter> s"
2537    unfolding continuous_on_open_invariant by blast
2538  then obtain A where A: "\<forall>c\<in>C. open (A c) \<and> A c \<inter> s = f -` c \<inter> s"
2539    unfolding bchoice_iff ..
2540  with cover have "\<And>c. c \<in> C \<Longrightarrow> open (A c)" "s \<subseteq> (\<Union>c\<in>C. A c)"
2541    by (fastforce simp add: subset_eq set_eq_iff)+
2542  from compactE_image[OF s this] obtain D where "D \<subseteq> C" "finite D" "s \<subseteq> (\<Union>c\<in>D. A c)" .
2543  with A show "\<exists>D \<subseteq> C. finite D \<and> f`s \<subseteq> \<Union>D"
2544    by (intro exI[of _ D]) (fastforce simp add: subset_eq set_eq_iff)+
2545qed
2546
2547lemma continuous_on_inv:
2548  fixes f :: "'a::topological_space \<Rightarrow> 'b::t2_space"
2549  assumes "continuous_on s f"
2550    and "compact s"
2551    and "\<forall>x\<in>s. g (f x) = x"
2552  shows "continuous_on (f ` s) g"
2553  unfolding continuous_on_topological
2554proof (clarsimp simp add: assms(3))
2555  fix x :: 'a and B :: "'a set"
2556  assume "x \<in> s" and "open B" and "x \<in> B"
2557  have 1: "\<forall>x\<in>s. f x \<in> f ` (s - B) \<longleftrightarrow> x \<in> s - B"
2558    using assms(3) by (auto, metis)
2559  have "continuous_on (s - B) f"
2560    using \<open>continuous_on s f\<close> Diff_subset
2561    by (rule continuous_on_subset)
2562  moreover have "compact (s - B)"
2563    using \<open>open B\<close> and \<open>compact s\<close>
2564    unfolding Diff_eq by (intro compact_Int_closed closed_Compl)
2565  ultimately have "compact (f ` (s - B))"
2566    by (rule compact_continuous_image)
2567  then have "closed (f ` (s - B))"
2568    by (rule compact_imp_closed)
2569  then have "open (- f ` (s - B))"
2570    by (rule open_Compl)
2571  moreover have "f x \<in> - f ` (s - B)"
2572    using \<open>x \<in> s\<close> and \<open>x \<in> B\<close> by (simp add: 1)
2573  moreover have "\<forall>y\<in>s. f y \<in> - f ` (s - B) \<longrightarrow> y \<in> B"
2574    by (simp add: 1)
2575  ultimately show "\<exists>A. open A \<and> f x \<in> A \<and> (\<forall>y\<in>s. f y \<in> A \<longrightarrow> y \<in> B)"
2576    by fast
2577qed
2578
2579lemma continuous_on_inv_into:
2580  fixes f :: "'a::topological_space \<Rightarrow> 'b::t2_space"
2581  assumes s: "continuous_on s f" "compact s"
2582    and f: "inj_on f s"
2583  shows "continuous_on (f ` s) (the_inv_into s f)"
2584  by (rule continuous_on_inv[OF s]) (auto simp: the_inv_into_f_f[OF f])
2585
2586lemma (in linorder_topology) compact_attains_sup:
2587  assumes "compact S" "S \<noteq> {}"
2588  shows "\<exists>s\<in>S. \<forall>t\<in>S. t \<le> s"
2589proof (rule classical)
2590  assume "\<not> (\<exists>s\<in>S. \<forall>t\<in>S. t \<le> s)"
2591  then obtain t where t: "\<forall>s\<in>S. t s \<in> S" and "\<forall>s\<in>S. s < t s"
2592    by (metis not_le)
2593  then have "\<And>s. s\<in>S \<Longrightarrow> open {..< t s}" "S \<subseteq> (\<Union>s\<in>S. {..< t s})"
2594    by auto
2595  with \<open>compact S\<close> obtain C where "C \<subseteq> S" "finite C" and C: "S \<subseteq> (\<Union>s\<in>C. {..< t s})"
2596    by (metis compactE_image)
2597  with \<open>S \<noteq> {}\<close> have Max: "Max (t`C) \<in> t`C" and "\<forall>s\<in>t`C. s \<le> Max (t`C)"
2598    by (auto intro!: Max_in)
2599  with C have "S \<subseteq> {..< Max (t`C)}"
2600    by (auto intro: less_le_trans simp: subset_eq)
2601  with t Max \<open>C \<subseteq> S\<close> show ?thesis
2602    by fastforce
2603qed
2604
2605lemma (in linorder_topology) compact_attains_inf:
2606  assumes "compact S" "S \<noteq> {}"
2607  shows "\<exists>s\<in>S. \<forall>t\<in>S. s \<le> t"
2608proof (rule classical)
2609  assume "\<not> (\<exists>s\<in>S. \<forall>t\<in>S. s \<le> t)"
2610  then obtain t where t: "\<forall>s\<in>S. t s \<in> S" and "\<forall>s\<in>S. t s < s"
2611    by (metis not_le)
2612  then have "\<And>s. s\<in>S \<Longrightarrow> open {t s <..}" "S \<subseteq> (\<Union>s\<in>S. {t s <..})"
2613    by auto
2614  with \<open>compact S\<close> obtain C where "C \<subseteq> S" "finite C" and C: "S \<subseteq> (\<Union>s\<in>C. {t s <..})"
2615    by (metis compactE_image)
2616  with \<open>S \<noteq> {}\<close> have Min: "Min (t`C) \<in> t`C" and "\<forall>s\<in>t`C. Min (t`C) \<le> s"
2617    by (auto intro!: Min_in)
2618  with C have "S \<subseteq> {Min (t`C) <..}"
2619    by (auto intro: le_less_trans simp: subset_eq)
2620  with t Min \<open>C \<subseteq> S\<close> show ?thesis
2621    by fastforce
2622qed
2623
2624lemma continuous_attains_sup:
2625  fixes f :: "'a::topological_space \<Rightarrow> 'b::linorder_topology"
2626  shows "compact s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> continuous_on s f \<Longrightarrow> (\<exists>x\<in>s. \<forall>y\<in>s.  f y \<le> f x)"
2627  using compact_attains_sup[of "f ` s"] compact_continuous_image[of s f] by auto
2628
2629lemma continuous_attains_inf:
2630  fixes f :: "'a::topological_space \<Rightarrow> 'b::linorder_topology"
2631  shows "compact s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> continuous_on s f \<Longrightarrow> (\<exists>x\<in>s. \<forall>y\<in>s. f x \<le> f y)"
2632  using compact_attains_inf[of "f ` s"] compact_continuous_image[of s f] by auto
2633
2634
2635subsection \<open>Connectedness\<close>
2636
2637context topological_space
2638begin
2639
2640definition "connected S \<longleftrightarrow>
2641  \<not> (\<exists>A B. open A \<and> open B \<and> S \<subseteq> A \<union> B \<and> A \<inter> B \<inter> S = {} \<and> A \<inter> S \<noteq> {} \<and> B \<inter> S \<noteq> {})"
2642
2643lemma connectedI:
2644  "(\<And>A B. open A \<Longrightarrow> open B \<Longrightarrow> A \<inter> U \<noteq> {} \<Longrightarrow> B \<inter> U \<noteq> {} \<Longrightarrow> A \<inter> B \<inter> U = {} \<Longrightarrow> U \<subseteq> A \<union> B \<Longrightarrow> False)
2645  \<Longrightarrow> connected U"
2646  by (auto simp: connected_def)
2647
2648lemma connected_empty [simp]: "connected {}"
2649  by (auto intro!: connectedI)
2650
2651lemma connected_sing [simp]: "connected {x}"
2652  by (auto intro!: connectedI)
2653
2654lemma connectedD:
2655  "connected A \<Longrightarrow> open U \<Longrightarrow> open V \<Longrightarrow> U \<inter> V \<inter> A = {} \<Longrightarrow> A \<subseteq> U \<union> V \<Longrightarrow> U \<inter> A = {} \<or> V \<inter> A = {}"
2656  by (auto simp: connected_def)
2657
2658end
2659
2660lemma connected_closed:
2661  "connected s \<longleftrightarrow>
2662    \<not> (\<exists>A B. closed A \<and> closed B \<and> s \<subseteq> A \<union> B \<and> A \<inter> B \<inter> s = {} \<and> A \<inter> s \<noteq> {} \<and> B \<inter> s \<noteq> {})"
2663  apply (simp add: connected_def del: ex_simps, safe)
2664   apply (drule_tac x="-A" in spec)
2665   apply (drule_tac x="-B" in spec)
2666   apply (fastforce simp add: closed_def [symmetric])
2667  apply (drule_tac x="-A" in spec)
2668  apply (drule_tac x="-B" in spec)
2669  apply (fastforce simp add: open_closed [symmetric])
2670  done
2671
2672lemma connected_closedD:
2673  "\<lbrakk>connected s; A \<inter> B \<inter> s = {}; s \<subseteq> A \<union> B; closed A; closed B\<rbrakk> \<Longrightarrow> A \<inter> s = {} \<or> B \<inter> s = {}"
2674  by (simp add: connected_closed)
2675
2676lemma connected_Union:
2677  assumes cs: "\<And>s. s \<in> S \<Longrightarrow> connected s"
2678    and ne: "\<Inter>S \<noteq> {}"
2679  shows "connected(\<Union>S)"
2680proof (rule connectedI)
2681  fix A B
2682  assume A: "open A" and B: "open B" and Alap: "A \<inter> \<Union>S \<noteq> {}" and Blap: "B \<inter> \<Union>S \<noteq> {}"
2683    and disj: "A \<inter> B \<inter> \<Union>S = {}" and cover: "\<Union>S \<subseteq> A \<union> B"
2684  have disjs:"\<And>s. s \<in> S \<Longrightarrow> A \<inter> B \<inter> s = {}"
2685    using disj by auto
2686  obtain sa where sa: "sa \<in> S" "A \<inter> sa \<noteq> {}"
2687    using Alap by auto
2688  obtain sb where sb: "sb \<in> S" "B \<inter> sb \<noteq> {}"
2689    using Blap by auto
2690  obtain x where x: "\<And>s. s \<in> S \<Longrightarrow> x \<in> s"
2691    using ne by auto
2692  then have "x \<in> \<Union>S"
2693    using \<open>sa \<in> S\<close> by blast
2694  then have "x \<in> A \<or> x \<in> B"
2695    using cover by auto
2696  then show False
2697    using cs [unfolded connected_def]
2698    by (metis A B IntI Sup_upper sa sb disjs x cover empty_iff subset_trans)
2699qed
2700
2701lemma connected_Un: "connected s \<Longrightarrow> connected t \<Longrightarrow> s \<inter> t \<noteq> {} \<Longrightarrow> connected (s \<union> t)"
2702  using connected_Union [of "{s,t}"] by auto
2703
2704lemma connected_diff_open_from_closed:
2705  assumes st: "s \<subseteq> t"
2706    and tu: "t \<subseteq> u"
2707    and s: "open s"
2708    and t: "closed t"
2709    and u: "connected u"
2710    and ts: "connected (t - s)"
2711  shows "connected(u - s)"
2712proof (rule connectedI)
2713  fix A B
2714  assume AB: "open A" "open B" "A \<inter> (u - s) \<noteq> {}" "B \<inter> (u - s) \<noteq> {}"
2715    and disj: "A \<inter> B \<inter> (u - s) = {}"
2716    and cover: "u - s \<subseteq> A \<union> B"
2717  then consider "A \<inter> (t - s) = {}" | "B \<inter> (t - s) = {}"
2718    using st ts tu connectedD [of "t-s" "A" "B"] by auto
2719  then show False
2720  proof cases
2721    case 1
2722    then have "(A - t) \<inter> (B \<union> s) \<inter> u = {}"
2723      using disj st by auto
2724    moreover have "u \<subseteq> (A - t) \<union> (B \<union> s)"
2725      using 1 cover by auto
2726    ultimately show False
2727      using connectedD [of u "A - t" "B \<union> s"] AB s t 1 u by auto
2728  next
2729    case 2
2730    then have "(A \<union> s) \<inter> (B - t) \<inter> u = {}"
2731      using disj st by auto
2732    moreover have "u \<subseteq> (A \<union> s) \<union> (B - t)"
2733      using 2 cover by auto
2734    ultimately show False
2735      using connectedD [of u "A \<union> s" "B - t"] AB s t 2 u by auto
2736  qed
2737qed
2738
2739lemma connected_iff_const:
2740  fixes S :: "'a::topological_space set"
2741  shows "connected S \<longleftrightarrow> (\<forall>P::'a \<Rightarrow> bool. continuous_on S P \<longrightarrow> (\<exists>c. \<forall>s\<in>S. P s = c))"
2742proof safe
2743  fix P :: "'a \<Rightarrow> bool"
2744  assume "connected S" "continuous_on S P"
2745  then have "\<And>b. \<exists>A. open A \<and> A \<inter> S = P -` {b} \<inter> S"
2746    unfolding continuous_on_open_invariant by (simp add: open_discrete)
2747  from this[of True] this[of False]
2748  obtain t f where "open t" "open f" and *: "f \<inter> S = P -` {False} \<inter> S" "t \<inter> S = P -` {True} \<inter> S"
2749    by meson
2750  then have "t \<inter> S = {} \<or> f \<inter> S = {}"
2751    by (intro connectedD[OF \<open>connected S\<close>])  auto
2752  then show "\<exists>c. \<forall>s\<in>S. P s = c"
2753  proof (rule disjE)
2754    assume "t \<inter> S = {}"
2755    then show ?thesis
2756      unfolding * by (intro exI[of _ False]) auto
2757  next
2758    assume "f \<inter> S = {}"
2759    then show ?thesis
2760      unfolding * by (intro exI[of _ True]) auto
2761  qed
2762next
2763  assume P: "\<forall>P::'a \<Rightarrow> bool. continuous_on S P \<longrightarrow> (\<exists>c. \<forall>s\<in>S. P s = c)"
2764  show "connected S"
2765  proof (rule connectedI)
2766    fix A B
2767    assume *: "open A" "open B" "A \<inter> S \<noteq> {}" "B \<inter> S \<noteq> {}" "A \<inter> B \<inter> S = {}" "S \<subseteq> A \<union> B"
2768    have "continuous_on S (\<lambda>x. x \<in> A)"
2769      unfolding continuous_on_open_invariant
2770    proof safe
2771      fix C :: "bool set"
2772      have "C = UNIV \<or> C = {True} \<or> C = {False} \<or> C = {}"
2773        using subset_UNIV[of C] unfolding UNIV_bool by auto
2774      with * show "\<exists>T. open T \<and> T \<inter> S = (\<lambda>x. x \<in> A) -` C \<inter> S"
2775        by (intro exI[of _ "(if True \<in> C then A else {}) \<union> (if False \<in> C then B else {})"]) auto
2776    qed
2777    from P[rule_format, OF this] obtain c where "\<And>s. s \<in> S \<Longrightarrow> (s \<in> A) = c"
2778      by blast
2779    with * show False
2780      by (cases c) auto
2781  qed
2782qed
2783
2784lemma connectedD_const: "connected S \<Longrightarrow> continuous_on S P \<Longrightarrow> \<exists>c. \<forall>s\<in>S. P s = c"
2785  for P :: "'a::topological_space \<Rightarrow> bool"
2786  by (auto simp: connected_iff_const)
2787
2788lemma connectedI_const:
2789  "(\<And>P::'a::topological_space \<Rightarrow> bool. continuous_on S P \<Longrightarrow> \<exists>c. \<forall>s\<in>S. P s = c) \<Longrightarrow> connected S"
2790  by (auto simp: connected_iff_const)
2791
2792lemma connected_local_const:
2793  assumes "connected A" "a \<in> A" "b \<in> A"
2794    and *: "\<forall>a\<in>A. eventually (\<lambda>b. f a = f b) (at a within A)"
2795  shows "f a = f b"
2796proof -
2797  obtain S where S: "\<And>a. a \<in> A \<Longrightarrow> a \<in> S a" "\<And>a. a \<in> A \<Longrightarrow> open (S a)"
2798    "\<And>a x. a \<in> A \<Longrightarrow> x \<in> S a \<Longrightarrow> x \<in> A \<Longrightarrow> f a = f x"
2799    using * unfolding eventually_at_topological by metis
2800  let ?P = "\<Union>b\<in>{b\<in>A. f a = f b}. S b" and ?N = "\<Union>b\<in>{b\<in>A. f a \<noteq> f b}. S b"
2801  have "?P \<inter> A = {} \<or> ?N \<inter> A = {}"
2802    using \<open>connected A\<close> S \<open>a\<in>A\<close>
2803    by (intro connectedD) (auto, metis)
2804  then show "f a = f b"
2805  proof
2806    assume "?N \<inter> A = {}"
2807    then have "\<forall>x\<in>A. f a = f x"
2808      using S(1) by auto
2809    with \<open>b\<in>A\<close> show ?thesis by auto
2810  next
2811    assume "?P \<inter> A = {}" then show ?thesis
2812      using \<open>a \<in> A\<close> S(1)[of a] by auto
2813  qed
2814qed
2815
2816lemma (in linorder_topology) connectedD_interval:
2817  assumes "connected U"
2818    and xy: "x \<in> U" "y \<in> U"
2819    and "x \<le> z" "z \<le> y"
2820  shows "z \<in> U"
2821proof -
2822  have eq: "{..<z} \<union> {z<..} = - {z}"
2823    by auto
2824  have "\<not> connected U" if "z \<notin> U" "x < z" "z < y"
2825    using xy that
2826    apply (simp only: connected_def simp_thms)
2827    apply (rule_tac exI[of _ "{..< z}"])
2828    apply (rule_tac exI[of _ "{z <..}"])
2829    apply (auto simp add: eq)
2830    done
2831  with assms show "z \<in> U"
2832    by (metis less_le)
2833qed
2834
2835lemma (in linorder_topology) not_in_connected_cases:
2836  assumes conn: "connected S"
2837  assumes nbdd: "x \<notin> S"
2838  assumes ne: "S \<noteq> {}"
2839  obtains "bdd_above S" "\<And>y. y \<in> S \<Longrightarrow> x \<ge> y" | "bdd_below S" "\<And>y. y \<in> S \<Longrightarrow> x \<le> y"
2840proof -
2841  obtain s where "s \<in> S" using ne by blast
2842  {
2843    assume "s \<le> x"
2844    have "False" if "x \<le> y" "y \<in> S" for y
2845      using connectedD_interval[OF conn \<open>s \<in> S\<close> \<open>y \<in> S\<close> \<open>s \<le> x\<close> \<open>x \<le> y\<close>] \<open>x \<notin> S\<close>
2846      by simp
2847    then have wit: "y \<in> S \<Longrightarrow> x \<ge> y" for y
2848      using le_cases by blast
2849    then have "bdd_above S"
2850      by (rule local.bdd_aboveI)
2851    note this wit
2852  } moreover {
2853    assume "x \<le> s"
2854    have "False" if "x \<ge> y" "y \<in> S" for y
2855      using connectedD_interval[OF conn \<open>y \<in> S\<close> \<open>s \<in> S\<close> \<open>x \<ge> y\<close> \<open>s \<ge> x\<close> ] \<open>x \<notin> S\<close>
2856      by simp
2857    then have wit: "y \<in> S \<Longrightarrow> x \<le> y" for y
2858      using le_cases by blast
2859    then have "bdd_below S"
2860      by (rule bdd_belowI)
2861    note this wit
2862  } ultimately show ?thesis
2863    by (meson le_cases that)
2864qed
2865
2866lemma connected_continuous_image:
2867  assumes *: "continuous_on s f"
2868    and "connected s"
2869  shows "connected (f ` s)"
2870proof (rule connectedI_const)
2871  fix P :: "'b \<Rightarrow> bool"
2872  assume "continuous_on (f ` s) P"
2873  then have "continuous_on s (P \<circ> f)"
2874    by (rule continuous_on_compose[OF *])
2875  from connectedD_const[OF \<open>connected s\<close> this] show "\<exists>c. \<forall>s\<in>f ` s. P s = c"
2876    by auto
2877qed
2878
2879
2880section \<open>Linear Continuum Topologies\<close>
2881
2882class linear_continuum_topology = linorder_topology + linear_continuum
2883begin
2884
2885lemma Inf_notin_open:
2886  assumes A: "open A"
2887    and bnd: "\<forall>a\<in>A. x < a"
2888  shows "Inf A \<notin> A"
2889proof
2890  assume "Inf A \<in> A"
2891  then obtain b where "b < Inf A" "{b <.. Inf A} \<subseteq> A"
2892    using open_left[of A "Inf A" x] assms by auto
2893  with dense[of b "Inf A"] obtain c where "c < Inf A" "c \<in> A"
2894    by (auto simp: subset_eq)
2895  then show False
2896    using cInf_lower[OF \<open>c \<in> A\<close>] bnd
2897    by (metis not_le less_imp_le bdd_belowI)
2898qed
2899
2900lemma Sup_notin_open:
2901  assumes A: "open A"
2902    and bnd: "\<forall>a\<in>A. a < x"
2903  shows "Sup A \<notin> A"
2904proof
2905  assume "Sup A \<in> A"
2906  with assms obtain b where "Sup A < b" "{Sup A ..< b} \<subseteq> A"
2907    using open_right[of A "Sup A" x] by auto
2908  with dense[of "Sup A" b] obtain c where "Sup A < c" "c \<in> A"
2909    by (auto simp: subset_eq)
2910  then show False
2911    using cSup_upper[OF \<open>c \<in> A\<close>] bnd
2912    by (metis less_imp_le not_le bdd_aboveI)
2913qed
2914
2915end
2916
2917instance linear_continuum_topology \<subseteq> perfect_space
2918proof
2919  fix x :: 'a
2920  obtain y where "x < y \<or> y < x"
2921    using ex_gt_or_lt [of x] ..
2922  with Inf_notin_open[of "{x}" y] Sup_notin_open[of "{x}" y] show "\<not> open {x}"
2923    by auto
2924qed
2925
2926lemma connectedI_interval:
2927  fixes U :: "'a :: linear_continuum_topology set"
2928  assumes *: "\<And>x y z. x \<in> U \<Longrightarrow> y \<in> U \<Longrightarrow> x \<le> z \<Longrightarrow> z \<le> y \<Longrightarrow> z \<in> U"
2929  shows "connected U"
2930proof (rule connectedI)
2931  {
2932    fix A B
2933    assume "open A" "open B" "A \<inter> B \<inter> U = {}" "U \<subseteq> A \<union> B"
2934    fix x y
2935    assume "x < y" "x \<in> A" "y \<in> B" "x \<in> U" "y \<in> U"
2936
2937    let ?z = "Inf (B \<inter> {x <..})"
2938
2939    have "x \<le> ?z" "?z \<le> y"
2940      using \<open>y \<in> B\<close> \<open>x < y\<close> by (auto intro: cInf_lower cInf_greatest)
2941    with \<open>x \<in> U\<close> \<open>y \<in> U\<close> have "?z \<in> U"
2942      by (rule *)
2943    moreover have "?z \<notin> B \<inter> {x <..}"
2944      using \<open>open B\<close> by (intro Inf_notin_open) auto
2945    ultimately have "?z \<in> A"
2946      using \<open>x \<le> ?z\<close> \<open>A \<inter> B \<inter> U = {}\<close> \<open>x \<in> A\<close> \<open>U \<subseteq> A \<union> B\<close> by auto
2947    have "\<exists>b\<in>B. b \<in> A \<and> b \<in> U" if "?z < y"
2948    proof -
2949      obtain a where "?z < a" "{?z ..< a} \<subseteq> A"
2950        using open_right[OF \<open>open A\<close> \<open>?z \<in> A\<close> \<open>?z < y\<close>] by auto
2951      moreover obtain b where "b \<in> B" "x < b" "b < min a y"
2952        using cInf_less_iff[of "B \<inter> {x <..}" "min a y"] \<open>?z < a\<close> \<open>?z < y\<close> \<open>x < y\<close> \<open>y \<in> B\<close>
2953        by auto
2954      moreover have "?z \<le> b"
2955        using \<open>b \<in> B\<close> \<open>x < b\<close>
2956        by (intro cInf_lower) auto
2957      moreover have "b \<in> U"
2958        using \<open>x \<le> ?z\<close> \<open>?z \<le> b\<close> \<open>b < min a y\<close>
2959        by (intro *[OF \<open>x \<in> U\<close> \<open>y \<in> U\<close>]) (auto simp: less_imp_le)
2960      ultimately show ?thesis
2961        by (intro bexI[of _ b]) auto
2962    qed
2963    then have False
2964      using \<open>?z \<le> y\<close> \<open>?z \<in> A\<close> \<open>y \<in> B\<close> \<open>y \<in> U\<close> \<open>A \<inter> B \<inter> U = {}\<close>
2965      unfolding le_less by blast
2966  }
2967  note not_disjoint = this
2968
2969  fix A B assume AB: "open A" "open B" "U \<subseteq> A \<union> B" "A \<inter> B \<inter> U = {}"
2970  moreover assume "A \<inter> U \<noteq> {}" then obtain x where x: "x \<in> U" "x \<in> A" by auto
2971  moreover assume "B \<inter> U \<noteq> {}" then obtain y where y: "y \<in> U" "y \<in> B" by auto
2972  moreover note not_disjoint[of B A y x] not_disjoint[of A B x y]
2973  ultimately show False
2974    by (cases x y rule: linorder_cases) auto
2975qed
2976
2977lemma connected_iff_interval: "connected U \<longleftrightarrow> (\<forall>x\<in>U. \<forall>y\<in>U. \<forall>z. x \<le> z \<longrightarrow> z \<le> y \<longrightarrow> z \<in> U)"
2978  for U :: "'a::linear_continuum_topology set"
2979  by (auto intro: connectedI_interval dest: connectedD_interval)
2980
2981lemma connected_UNIV[simp]: "connected (UNIV::'a::linear_continuum_topology set)"
2982  by (simp add: connected_iff_interval)
2983
2984lemma connected_Ioi[simp]: "connected {a<..}"
2985  for a :: "'a::linear_continuum_topology"
2986  by (auto simp: connected_iff_interval)
2987
2988lemma connected_Ici[simp]: "connected {a..}"
2989  for a :: "'a::linear_continuum_topology"
2990  by (auto simp: connected_iff_interval)
2991
2992lemma connected_Iio[simp]: "connected {..<a}"
2993  for a :: "'a::linear_continuum_topology"
2994  by (auto simp: connected_iff_interval)
2995
2996lemma connected_Iic[simp]: "connected {..a}"
2997  for a :: "'a::linear_continuum_topology"
2998  by (auto simp: connected_iff_interval)
2999
3000lemma connected_Ioo[simp]: "connected {a<..<b}"
3001  for a b :: "'a::linear_continuum_topology"
3002  unfolding connected_iff_interval by auto
3003
3004lemma connected_Ioc[simp]: "connected {a<..b}"
3005  for a b :: "'a::linear_continuum_topology"
3006  by (auto simp: connected_iff_interval)
3007
3008lemma connected_Ico[simp]: "connected {a..<b}"
3009  for a b :: "'a::linear_continuum_topology"
3010  by (auto simp: connected_iff_interval)
3011
3012lemma connected_Icc[simp]: "connected {a..b}"
3013  for a b :: "'a::linear_continuum_topology"
3014  by (auto simp: connected_iff_interval)
3015
3016lemma connected_contains_Ioo:
3017  fixes A :: "'a :: linorder_topology set"
3018  assumes "connected A" "a \<in> A" "b \<in> A" shows "{a <..< b} \<subseteq> A"
3019  using connectedD_interval[OF assms] by (simp add: subset_eq Ball_def less_imp_le)
3020
3021lemma connected_contains_Icc:
3022  fixes A :: "'a::linorder_topology set"
3023  assumes "connected A" "a \<in> A" "b \<in> A"
3024  shows "{a..b} \<subseteq> A"
3025proof
3026  fix x assume "x \<in> {a..b}"
3027  then have "x = a \<or> x = b \<or> x \<in> {a<..<b}"
3028    by auto
3029  then show "x \<in> A"
3030    using assms connected_contains_Ioo[of A a b] by auto
3031qed
3032
3033
3034subsection \<open>Intermediate Value Theorem\<close>
3035
3036lemma IVT':
3037  fixes f :: "'a::linear_continuum_topology \<Rightarrow> 'b::linorder_topology"
3038  assumes y: "f a \<le> y" "y \<le> f b" "a \<le> b"
3039    and *: "continuous_on {a .. b} f"
3040  shows "\<exists>x. a \<le> x \<and> x \<le> b \<and> f x = y"
3041proof -
3042  have "connected {a..b}"
3043    unfolding connected_iff_interval by auto
3044  from connected_continuous_image[OF * this, THEN connectedD_interval, of "f a" "f b" y] y
3045  show ?thesis
3046    by (auto simp add: atLeastAtMost_def atLeast_def atMost_def)
3047qed
3048
3049lemma IVT2':
3050  fixes f :: "'a :: linear_continuum_topology \<Rightarrow> 'b :: linorder_topology"
3051  assumes y: "f b \<le> y" "y \<le> f a" "a \<le> b"
3052    and *: "continuous_on {a .. b} f"
3053  shows "\<exists>x. a \<le> x \<and> x \<le> b \<and> f x = y"
3054proof -
3055  have "connected {a..b}"
3056    unfolding connected_iff_interval by auto
3057  from connected_continuous_image[OF * this, THEN connectedD_interval, of "f b" "f a" y] y
3058  show ?thesis
3059    by (auto simp add: atLeastAtMost_def atLeast_def atMost_def)
3060qed
3061
3062lemma IVT:
3063  fixes f :: "'a::linear_continuum_topology \<Rightarrow> 'b::linorder_topology"
3064  shows "f a \<le> y \<Longrightarrow> y \<le> f b \<Longrightarrow> a \<le> b \<Longrightarrow> (\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont f x) \<Longrightarrow>
3065    \<exists>x. a \<le> x \<and> x \<le> b \<and> f x = y"
3066  by (rule IVT') (auto intro: continuous_at_imp_continuous_on)
3067
3068lemma IVT2:
3069  fixes f :: "'a::linear_continuum_topology \<Rightarrow> 'b::linorder_topology"
3070  shows "f b \<le> y \<Longrightarrow> y \<le> f a \<Longrightarrow> a \<le> b \<Longrightarrow> (\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont f x) \<Longrightarrow>
3071    \<exists>x. a \<le> x \<and> x \<le> b \<and> f x = y"
3072  by (rule IVT2') (auto intro: continuous_at_imp_continuous_on)
3073
3074lemma continuous_inj_imp_mono:
3075  fixes f :: "'a::linear_continuum_topology \<Rightarrow> 'b::linorder_topology"
3076  assumes x: "a < x" "x < b"
3077    and cont: "continuous_on {a..b} f"
3078    and inj: "inj_on f {a..b}"
3079  shows "(f a < f x \<and> f x < f b) \<or> (f b < f x \<and> f x < f a)"
3080proof -
3081  note I = inj_on_eq_iff[OF inj]
3082  {
3083    assume "f x < f a" "f x < f b"
3084    then obtain s t where "x \<le> s" "s \<le> b" "a \<le> t" "t \<le> x" "f s = f t" "f x < f s"
3085      using IVT'[of f x "min (f a) (f b)" b] IVT2'[of f x "min (f a) (f b)" a] x
3086      by (auto simp: continuous_on_subset[OF cont] less_imp_le)
3087    with x I have False by auto
3088  }
3089  moreover
3090  {
3091    assume "f a < f x" "f b < f x"
3092    then obtain s t where "x \<le> s" "s \<le> b" "a \<le> t" "t \<le> x" "f s = f t" "f s < f x"
3093      using IVT'[of f a "max (f a) (f b)" x] IVT2'[of f b "max (f a) (f b)" x] x
3094      by (auto simp: continuous_on_subset[OF cont] less_imp_le)
3095    with x I have False by auto
3096  }
3097  ultimately show ?thesis
3098    using I[of a x] I[of x b] x less_trans[OF x]
3099    by (auto simp add: le_less less_imp_neq neq_iff)
3100qed
3101
3102lemma continuous_at_Sup_mono:
3103  fixes f :: "'a::{linorder_topology,conditionally_complete_linorder} \<Rightarrow>
3104    'b::{linorder_topology,conditionally_complete_linorder}"
3105  assumes "mono f"
3106    and cont: "continuous (at_left (Sup S)) f"
3107    and S: "S \<noteq> {}" "bdd_above S"
3108  shows "f (Sup S) = (SUP s\<in>S. f s)"
3109proof (rule antisym)
3110  have f: "(f \<longlongrightarrow> f (Sup S)) (at_left (Sup S))"
3111    using cont unfolding continuous_within .
3112  show "f (Sup S) \<le> (SUP s\<in>S. f s)"
3113  proof cases
3114    assume "Sup S \<in> S"
3115    then show ?thesis
3116      by (rule cSUP_upper) (auto intro: bdd_above_image_mono S \<open>mono f\<close>)
3117  next
3118    assume "Sup S \<notin> S"
3119    from \<open>S \<noteq> {}\<close> obtain s where "s \<in> S"
3120      by auto
3121    with \<open>Sup S \<notin> S\<close> S have "s < Sup S"
3122      unfolding less_le by (blast intro: cSup_upper)
3123    show ?thesis
3124    proof (rule ccontr)
3125      assume "\<not> ?thesis"
3126      with order_tendstoD(1)[OF f, of "SUP s\<in>S. f s"] obtain b where "b < Sup S"
3127        and *: "\<And>y. b < y \<Longrightarrow> y < Sup S \<Longrightarrow> (SUP s\<in>S. f s) < f y"
3128        by (auto simp: not_le eventually_at_left[OF \<open>s < Sup S\<close>])
3129      with \<open>S \<noteq> {}\<close> obtain c where "c \<in> S" "b < c"
3130        using less_cSupD[of S b] by auto
3131      with \<open>Sup S \<notin> S\<close> S have "c < Sup S"
3132        unfolding less_le by (blast intro: cSup_upper)
3133      from *[OF \<open>b < c\<close> \<open>c < Sup S\<close>] cSUP_upper[OF \<open>c \<in> S\<close> bdd_above_image_mono[of f]]
3134      show False
3135        by (auto simp: assms)
3136    qed
3137  qed
3138qed (intro cSUP_least \<open>mono f\<close>[THEN monoD] cSup_upper S)
3139
3140lemma continuous_at_Sup_antimono:
3141  fixes f :: "'a::{linorder_topology,conditionally_complete_linorder} \<Rightarrow>
3142    'b::{linorder_topology,conditionally_complete_linorder}"
3143  assumes "antimono f"
3144    and cont: "continuous (at_left (Sup S)) f"
3145    and S: "S \<noteq> {}" "bdd_above S"
3146  shows "f (Sup S) = (INF s\<in>S. f s)"
3147proof (rule antisym)
3148  have f: "(f \<longlongrightarrow> f (Sup S)) (at_left (Sup S))"
3149    using cont unfolding continuous_within .
3150  show "(INF s\<in>S. f s) \<le> f (Sup S)"
3151  proof cases
3152    assume "Sup S \<in> S"
3153    then show ?thesis
3154      by (intro cINF_lower) (auto intro: bdd_below_image_antimono S \<open>antimono f\<close>)
3155  next
3156    assume "Sup S \<notin> S"
3157    from \<open>S \<noteq> {}\<close> obtain s where "s \<in> S"
3158      by auto
3159    with \<open>Sup S \<notin> S\<close> S have "s < Sup S"
3160      unfolding less_le by (blast intro: cSup_upper)
3161    show ?thesis
3162    proof (rule ccontr)
3163      assume "\<not> ?thesis"
3164      with order_tendstoD(2)[OF f, of "INF s\<in>S. f s"] obtain b where "b < Sup S"
3165        and *: "\<And>y. b < y \<Longrightarrow> y < Sup S \<Longrightarrow> f y < (INF s\<in>S. f s)"
3166        by (auto simp: not_le eventually_at_left[OF \<open>s < Sup S\<close>])
3167      with \<open>S \<noteq> {}\<close> obtain c where "c \<in> S" "b < c"
3168        using less_cSupD[of S b] by auto
3169      with \<open>Sup S \<notin> S\<close> S have "c < Sup S"
3170        unfolding less_le by (blast intro: cSup_upper)
3171      from *[OF \<open>b < c\<close> \<open>c < Sup S\<close>] cINF_lower[OF bdd_below_image_antimono, of f S c] \<open>c \<in> S\<close>
3172      show False
3173        by (auto simp: assms)
3174    qed
3175  qed
3176qed (intro cINF_greatest \<open>antimono f\<close>[THEN antimonoD] cSup_upper S)
3177
3178lemma continuous_at_Inf_mono:
3179  fixes f :: "'a::{linorder_topology,conditionally_complete_linorder} \<Rightarrow>
3180    'b::{linorder_topology,conditionally_complete_linorder}"
3181  assumes "mono f"
3182    and cont: "continuous (at_right (Inf S)) f"
3183    and S: "S \<noteq> {}" "bdd_below S"
3184  shows "f (Inf S) = (INF s\<in>S. f s)"
3185proof (rule antisym)
3186  have f: "(f \<longlongrightarrow> f (Inf S)) (at_right (Inf S))"
3187    using cont unfolding continuous_within .
3188  show "(INF s\<in>S. f s) \<le> f (Inf S)"
3189  proof cases
3190    assume "Inf S \<in> S"
3191    then show ?thesis
3192      by (rule cINF_lower[rotated]) (auto intro: bdd_below_image_mono S \<open>mono f\<close>)
3193  next
3194    assume "Inf S \<notin> S"
3195    from \<open>S \<noteq> {}\<close> obtain s where "s \<in> S"
3196      by auto
3197    with \<open>Inf S \<notin> S\<close> S have "Inf S < s"
3198      unfolding less_le by (blast intro: cInf_lower)
3199    show ?thesis
3200    proof (rule ccontr)
3201      assume "\<not> ?thesis"
3202      with order_tendstoD(2)[OF f, of "INF s\<in>S. f s"] obtain b where "Inf S < b"
3203        and *: "\<And>y. Inf S < y \<Longrightarrow> y < b \<Longrightarrow> f y < (INF s\<in>S. f s)"
3204        by (auto simp: not_le eventually_at_right[OF \<open>Inf S < s\<close>])
3205      with \<open>S \<noteq> {}\<close> obtain c where "c \<in> S" "c < b"
3206        using cInf_lessD[of S b] by auto
3207      with \<open>Inf S \<notin> S\<close> S have "Inf S < c"
3208        unfolding less_le by (blast intro: cInf_lower)
3209      from *[OF \<open>Inf S < c\<close> \<open>c < b\<close>] cINF_lower[OF bdd_below_image_mono[of f] \<open>c \<in> S\<close>]
3210      show False
3211        by (auto simp: assms)
3212    qed
3213  qed
3214qed (intro cINF_greatest \<open>mono f\<close>[THEN monoD] cInf_lower \<open>bdd_below S\<close> \<open>S \<noteq> {}\<close>)
3215
3216lemma continuous_at_Inf_antimono:
3217  fixes f :: "'a::{linorder_topology,conditionally_complete_linorder} \<Rightarrow>
3218    'b::{linorder_topology,conditionally_complete_linorder}"
3219  assumes "antimono f"
3220    and cont: "continuous (at_right (Inf S)) f"
3221    and S: "S \<noteq> {}" "bdd_below S"
3222  shows "f (Inf S) = (SUP s\<in>S. f s)"
3223proof (rule antisym)
3224  have f: "(f \<longlongrightarrow> f (Inf S)) (at_right (Inf S))"
3225    using cont unfolding continuous_within .
3226  show "f (Inf S) \<le> (SUP s\<in>S. f s)"
3227  proof cases
3228    assume "Inf S \<in> S"
3229    then show ?thesis
3230      by (rule cSUP_upper) (auto intro: bdd_above_image_antimono S \<open>antimono f\<close>)
3231  next
3232    assume "Inf S \<notin> S"
3233    from \<open>S \<noteq> {}\<close> obtain s where "s \<in> S"
3234      by auto
3235    with \<open>Inf S \<notin> S\<close> S have "Inf S < s"
3236      unfolding less_le by (blast intro: cInf_lower)
3237    show ?thesis
3238    proof (rule ccontr)
3239      assume "\<not> ?thesis"
3240      with order_tendstoD(1)[OF f, of "SUP s\<in>S. f s"] obtain b where "Inf S < b"
3241        and *: "\<And>y. Inf S < y \<Longrightarrow> y < b \<Longrightarrow> (SUP s\<in>S. f s) < f y"
3242        by (auto simp: not_le eventually_at_right[OF \<open>Inf S < s\<close>])
3243      with \<open>S \<noteq> {}\<close> obtain c where "c \<in> S" "c < b"
3244        using cInf_lessD[of S b] by auto
3245      with \<open>Inf S \<notin> S\<close> S have "Inf S < c"
3246        unfolding less_le by (blast intro: cInf_lower)
3247      from *[OF \<open>Inf S < c\<close> \<open>c < b\<close>] cSUP_upper[OF \<open>c \<in> S\<close> bdd_above_image_antimono[of f]]
3248      show False
3249        by (auto simp: assms)
3250    qed
3251  qed
3252qed (intro cSUP_least \<open>antimono f\<close>[THEN antimonoD] cInf_lower S)
3253
3254
3255subsection \<open>Uniform spaces\<close>
3256
3257class uniformity =
3258  fixes uniformity :: "('a \<times> 'a) filter"
3259begin
3260
3261abbreviation uniformity_on :: "'a set \<Rightarrow> ('a \<times> 'a) filter"
3262  where "uniformity_on s \<equiv> inf uniformity (principal (s\<times>s))"
3263
3264end
3265
3266lemma uniformity_Abort:
3267  "uniformity =
3268    Filter.abstract_filter (\<lambda>u. Code.abort (STR ''uniformity is not executable'') (\<lambda>u. uniformity))"
3269  by simp
3270
3271class open_uniformity = "open" + uniformity +
3272  assumes open_uniformity:
3273    "\<And>U. open U \<longleftrightarrow> (\<forall>x\<in>U. eventually (\<lambda>(x', y). x' = x \<longrightarrow> y \<in> U) uniformity)"
3274begin
3275
3276subclass topological_space
3277  by standard (force elim: eventually_mono eventually_elim2 simp: split_beta' open_uniformity)+
3278
3279end
3280
3281class uniform_space = open_uniformity +
3282  assumes uniformity_refl: "eventually E uniformity \<Longrightarrow> E (x, x)"
3283    and uniformity_sym: "eventually E uniformity \<Longrightarrow> eventually (\<lambda>(x, y). E (y, x)) uniformity"
3284    and uniformity_trans:
3285      "eventually E uniformity \<Longrightarrow>
3286        \<exists>D. eventually D uniformity \<and> (\<forall>x y z. D (x, y) \<longrightarrow> D (y, z) \<longrightarrow> E (x, z))"
3287begin
3288
3289lemma uniformity_bot: "uniformity \<noteq> bot"
3290  using uniformity_refl by auto
3291
3292lemma uniformity_trans':
3293  "eventually E uniformity \<Longrightarrow>
3294    eventually (\<lambda>((x, y), (y', z)). y = y' \<longrightarrow> E (x, z)) (uniformity \<times>\<^sub>F uniformity)"
3295  by (drule uniformity_trans) (auto simp add: eventually_prod_same)
3296
3297lemma uniformity_transE:
3298  assumes "eventually E uniformity"
3299  obtains D where "eventually D uniformity" "\<And>x y z. D (x, y) \<Longrightarrow> D (y, z) \<Longrightarrow> E (x, z)"
3300  using uniformity_trans [OF assms] by auto
3301
3302lemma eventually_nhds_uniformity:
3303  "eventually P (nhds x) \<longleftrightarrow> eventually (\<lambda>(x', y). x' = x \<longrightarrow> P y) uniformity"
3304  (is "_ \<longleftrightarrow> ?N P x")
3305  unfolding eventually_nhds
3306proof safe
3307  assume *: "?N P x"
3308  have "?N (?N P) x" if "?N P x" for x
3309  proof -
3310    from that obtain D where ev: "eventually D uniformity"
3311      and D: "D (a, b) \<Longrightarrow> D (b, c) \<Longrightarrow> case (a, c) of (x', y) \<Rightarrow> x' = x \<longrightarrow> P y" for a b c
3312      by (rule uniformity_transE) simp
3313    from ev show ?thesis
3314      by eventually_elim (insert ev D, force elim: eventually_mono split: prod.split)
3315  qed
3316  then have "open {x. ?N P x}"
3317    by (simp add: open_uniformity)
3318  then show "\<exists>S. open S \<and> x \<in> S \<and> (\<forall>x\<in>S. P x)"
3319    by (intro exI[of _ "{x. ?N P x}"]) (auto dest: uniformity_refl simp: *)
3320qed (force simp add: open_uniformity elim: eventually_mono)
3321
3322
3323subsubsection \<open>Totally bounded sets\<close>
3324
3325definition totally_bounded :: "'a set \<Rightarrow> bool"
3326  where "totally_bounded S \<longleftrightarrow>
3327    (\<forall>E. eventually E uniformity \<longrightarrow> (\<exists>X. finite X \<and> (\<forall>s\<in>S. \<exists>x\<in>X. E (x, s))))"
3328
3329lemma totally_bounded_empty[iff]: "totally_bounded {}"
3330  by (auto simp add: totally_bounded_def)
3331
3332lemma totally_bounded_subset: "totally_bounded S \<Longrightarrow> T \<subseteq> S \<Longrightarrow> totally_bounded T"
3333  by (fastforce simp add: totally_bounded_def)
3334
3335lemma totally_bounded_Union[intro]:
3336  assumes M: "finite M" "\<And>S. S \<in> M \<Longrightarrow> totally_bounded S"
3337  shows "totally_bounded (\<Union>M)"
3338  unfolding totally_bounded_def
3339proof safe
3340  fix E
3341  assume "eventually E uniformity"
3342  with M obtain X where "\<forall>S\<in>M. finite (X S) \<and> (\<forall>s\<in>S. \<exists>x\<in>X S. E (x, s))"
3343    by (metis totally_bounded_def)
3344  with \<open>finite M\<close> show "\<exists>X. finite X \<and> (\<forall>s\<in>\<Union>M. \<exists>x\<in>X. E (x, s))"
3345    by (intro exI[of _ "\<Union>S\<in>M. X S"]) force
3346qed
3347
3348
3349subsubsection \<open>Cauchy filter\<close>
3350
3351definition cauchy_filter :: "'a filter \<Rightarrow> bool"
3352  where "cauchy_filter F \<longleftrightarrow> F \<times>\<^sub>F F \<le> uniformity"
3353
3354definition Cauchy :: "(nat \<Rightarrow> 'a) \<Rightarrow> bool"
3355  where Cauchy_uniform: "Cauchy X = cauchy_filter (filtermap X sequentially)"
3356
3357lemma Cauchy_uniform_iff:
3358  "Cauchy X \<longleftrightarrow> (\<forall>P. eventually P uniformity \<longrightarrow> (\<exists>N. \<forall>n\<ge>N. \<forall>m\<ge>N. P (X n, X m)))"
3359  unfolding Cauchy_uniform cauchy_filter_def le_filter_def eventually_prod_same
3360    eventually_filtermap eventually_sequentially
3361proof safe
3362  let ?U = "\<lambda>P. eventually P uniformity"
3363  {
3364    fix P
3365    assume "?U P" "\<forall>P. ?U P \<longrightarrow> (\<exists>Q. (\<exists>N. \<forall>n\<ge>N. Q (X n)) \<and> (\<forall>x y. Q x \<longrightarrow> Q y \<longrightarrow> P (x, y)))"
3366    then obtain Q N where "\<And>n. n \<ge> N \<Longrightarrow> Q (X n)" "\<And>x y. Q x \<Longrightarrow> Q y \<Longrightarrow> P (x, y)"
3367      by metis
3368    then show "\<exists>N. \<forall>n\<ge>N. \<forall>m\<ge>N. P (X n, X m)"
3369      by blast
3370  next
3371    fix P
3372    assume "?U P" and P: "\<forall>P. ?U P \<longrightarrow> (\<exists>N. \<forall>n\<ge>N. \<forall>m\<ge>N. P (X n, X m))"
3373    then obtain Q where "?U Q" and Q: "\<And>x y z. Q (x, y) \<Longrightarrow> Q (y, z) \<Longrightarrow> P (x, z)"
3374      by (auto elim: uniformity_transE)
3375    then have "?U (\<lambda>x. Q x \<and> (\<lambda>(x, y). Q (y, x)) x)"
3376      unfolding eventually_conj_iff by (simp add: uniformity_sym)
3377    from P[rule_format, OF this]
3378    obtain N where N: "\<And>n m. n \<ge> N \<Longrightarrow> m \<ge> N \<Longrightarrow> Q (X n, X m) \<and> Q (X m, X n)"
3379      by auto
3380    show "\<exists>Q. (\<exists>N. \<forall>n\<ge>N. Q (X n)) \<and> (\<forall>x y. Q x \<longrightarrow> Q y \<longrightarrow> P (x, y))"
3381    proof (safe intro!: exI[of _ "\<lambda>x. \<forall>n\<ge>N. Q (x, X n) \<and> Q (X n, x)"] exI[of _ N] N)
3382      fix x y
3383      assume "\<forall>n\<ge>N. Q (x, X n) \<and> Q (X n, x)" "\<forall>n\<ge>N. Q (y, X n) \<and> Q (X n, y)"
3384      then have "Q (x, X N)" "Q (X N, y)" by auto
3385      then show "P (x, y)"
3386        by (rule Q)
3387    qed
3388  }
3389qed
3390
3391lemma nhds_imp_cauchy_filter:
3392  assumes *: "F \<le> nhds x"
3393  shows "cauchy_filter F"
3394proof -
3395  have "F \<times>\<^sub>F F \<le> nhds x \<times>\<^sub>F nhds x"
3396    by (intro prod_filter_mono *)
3397  also have "\<dots> \<le> uniformity"
3398    unfolding le_filter_def eventually_nhds_uniformity eventually_prod_same
3399  proof safe
3400    fix P
3401    assume "eventually P uniformity"
3402    then obtain Ql where ev: "eventually Ql uniformity"
3403      and "Ql (x, y) \<Longrightarrow> Ql (y, z) \<Longrightarrow> P (x, z)" for x y z
3404      by (rule uniformity_transE) simp
3405    with ev[THEN uniformity_sym]
3406    show "\<exists>Q. eventually (\<lambda>(x', y). x' = x \<longrightarrow> Q y) uniformity \<and>
3407        (\<forall>x y. Q x \<longrightarrow> Q y \<longrightarrow> P (x, y))"
3408      by (rule_tac exI[of _ "\<lambda>y. Ql (y, x) \<and> Ql (x, y)"]) (fastforce elim: eventually_elim2)
3409  qed
3410  finally show ?thesis
3411    by (simp add: cauchy_filter_def)
3412qed
3413
3414lemma LIMSEQ_imp_Cauchy: "X \<longlonglongrightarrow> x \<Longrightarrow> Cauchy X"
3415  unfolding Cauchy_uniform filterlim_def by (intro nhds_imp_cauchy_filter)
3416
3417lemma Cauchy_subseq_Cauchy:
3418  assumes "Cauchy X" "strict_mono f"
3419  shows "Cauchy (X \<circ> f)"
3420  unfolding Cauchy_uniform comp_def filtermap_filtermap[symmetric] cauchy_filter_def
3421  by (rule order_trans[OF _ \<open>Cauchy X\<close>[unfolded Cauchy_uniform cauchy_filter_def]])
3422     (intro prod_filter_mono filtermap_mono filterlim_subseq[OF \<open>strict_mono f\<close>, unfolded filterlim_def])
3423
3424lemma convergent_Cauchy: "convergent X \<Longrightarrow> Cauchy X"
3425  unfolding convergent_def by (erule exE, erule LIMSEQ_imp_Cauchy)
3426
3427definition complete :: "'a set \<Rightarrow> bool"
3428  where complete_uniform: "complete S \<longleftrightarrow>
3429    (\<forall>F \<le> principal S. F \<noteq> bot \<longrightarrow> cauchy_filter F \<longrightarrow> (\<exists>x\<in>S. F \<le> nhds x))"
3430
3431end
3432
3433
3434subsubsection \<open>Uniformly continuous functions\<close>
3435
3436definition uniformly_continuous_on :: "'a set \<Rightarrow> ('a::uniform_space \<Rightarrow> 'b::uniform_space) \<Rightarrow> bool"
3437  where uniformly_continuous_on_uniformity: "uniformly_continuous_on s f \<longleftrightarrow>
3438    (LIM (x, y) (uniformity_on s). (f x, f y) :> uniformity)"
3439
3440lemma uniformly_continuous_onD:
3441  "uniformly_continuous_on s f \<Longrightarrow> eventually E uniformity \<Longrightarrow>
3442    eventually (\<lambda>(x, y). x \<in> s \<longrightarrow> y \<in> s \<longrightarrow> E (f x, f y)) uniformity"
3443  by (simp add: uniformly_continuous_on_uniformity filterlim_iff
3444      eventually_inf_principal split_beta' mem_Times_iff imp_conjL)
3445
3446lemma uniformly_continuous_on_const[continuous_intros]: "uniformly_continuous_on s (\<lambda>x. c)"
3447  by (auto simp: uniformly_continuous_on_uniformity filterlim_iff uniformity_refl)
3448
3449lemma uniformly_continuous_on_id[continuous_intros]: "uniformly_continuous_on s (\<lambda>x. x)"
3450  by (auto simp: uniformly_continuous_on_uniformity filterlim_def)
3451
3452lemma uniformly_continuous_on_compose[continuous_intros]:
3453  "uniformly_continuous_on s g \<Longrightarrow> uniformly_continuous_on (g`s) f \<Longrightarrow>
3454    uniformly_continuous_on s (\<lambda>x. f (g x))"
3455  using filterlim_compose[of "\<lambda>(x, y). (f x, f y)" uniformity
3456      "uniformity_on (g`s)"  "\<lambda>(x, y). (g x, g y)" "uniformity_on s"]
3457  by (simp add: split_beta' uniformly_continuous_on_uniformity
3458      filterlim_inf filterlim_principal eventually_inf_principal mem_Times_iff)
3459
3460lemma uniformly_continuous_imp_continuous:
3461  assumes f: "uniformly_continuous_on s f"
3462  shows "continuous_on s f"
3463  by (auto simp: filterlim_iff eventually_at_filter eventually_nhds_uniformity continuous_on_def
3464           elim: eventually_mono dest!: uniformly_continuous_onD[OF f])
3465
3466
3467section \<open>Product Topology\<close>
3468
3469subsection \<open>Product is a topological space\<close>
3470
3471instantiation prod :: (topological_space, topological_space) topological_space
3472begin
3473
3474definition open_prod_def[code del]:
3475  "open (S :: ('a \<times> 'b) set) \<longleftrightarrow>
3476    (\<forall>x\<in>S. \<exists>A B. open A \<and> open B \<and> x \<in> A \<times> B \<and> A \<times> B \<subseteq> S)"
3477
3478lemma open_prod_elim:
3479  assumes "open S" and "x \<in> S"
3480  obtains A B where "open A" and "open B" and "x \<in> A \<times> B" and "A \<times> B \<subseteq> S"
3481  using assms unfolding open_prod_def by fast
3482
3483lemma open_prod_intro:
3484  assumes "\<And>x. x \<in> S \<Longrightarrow> \<exists>A B. open A \<and> open B \<and> x \<in> A \<times> B \<and> A \<times> B \<subseteq> S"
3485  shows "open S"
3486  using assms unfolding open_prod_def by fast
3487
3488instance
3489proof
3490  show "open (UNIV :: ('a \<times> 'b) set)"
3491    unfolding open_prod_def by auto
3492next
3493  fix S T :: "('a \<times> 'b) set"
3494  assume "open S" "open T"
3495  show "open (S \<inter> T)"
3496  proof (rule open_prod_intro)
3497    fix x
3498    assume x: "x \<in> S \<inter> T"
3499    from x have "x \<in> S" by simp
3500    obtain Sa Sb where A: "open Sa" "open Sb" "x \<in> Sa \<times> Sb" "Sa \<times> Sb \<subseteq> S"
3501      using \<open>open S\<close> and \<open>x \<in> S\<close> by (rule open_prod_elim)
3502    from x have "x \<in> T" by simp
3503    obtain Ta Tb where B: "open Ta" "open Tb" "x \<in> Ta \<times> Tb" "Ta \<times> Tb \<subseteq> T"
3504      using \<open>open T\<close> and \<open>x \<in> T\<close> by (rule open_prod_elim)
3505    let ?A = "Sa \<inter> Ta" and ?B = "Sb \<inter> Tb"
3506    have "open ?A \<and> open ?B \<and> x \<in> ?A \<times> ?B \<and> ?A \<times> ?B \<subseteq> S \<inter> T"
3507      using A B by (auto simp add: open_Int)
3508    then show "\<exists>A B. open A \<and> open B \<and> x \<in> A \<times> B \<and> A \<times> B \<subseteq> S \<inter> T"
3509      by fast
3510  qed
3511next
3512  fix K :: "('a \<times> 'b) set set"
3513  assume "\<forall>S\<in>K. open S"
3514  then show "open (\<Union>K)"
3515    unfolding open_prod_def by fast
3516qed
3517
3518end
3519
3520declare [[code abort: "open :: ('a::topological_space \<times> 'b::topological_space) set \<Rightarrow> bool"]]
3521
3522lemma open_Times: "open S \<Longrightarrow> open T \<Longrightarrow> open (S \<times> T)"
3523  unfolding open_prod_def by auto
3524
3525lemma fst_vimage_eq_Times: "fst -` S = S \<times> UNIV"
3526  by auto
3527
3528lemma snd_vimage_eq_Times: "snd -` S = UNIV \<times> S"
3529  by auto
3530
3531lemma open_vimage_fst: "open S \<Longrightarrow> open (fst -` S)"
3532  by (simp add: fst_vimage_eq_Times open_Times)
3533
3534lemma open_vimage_snd: "open S \<Longrightarrow> open (snd -` S)"
3535  by (simp add: snd_vimage_eq_Times open_Times)
3536
3537lemma closed_vimage_fst: "closed S \<Longrightarrow> closed (fst -` S)"
3538  unfolding closed_open vimage_Compl [symmetric]
3539  by (rule open_vimage_fst)
3540
3541lemma closed_vimage_snd: "closed S \<Longrightarrow> closed (snd -` S)"
3542  unfolding closed_open vimage_Compl [symmetric]
3543  by (rule open_vimage_snd)
3544
3545lemma closed_Times: "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<times> T)"
3546proof -
3547  have "S \<times> T = (fst -` S) \<inter> (snd -` T)"
3548    by auto
3549  then show "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<times> T)"
3550    by (simp add: closed_vimage_fst closed_vimage_snd closed_Int)
3551qed
3552
3553lemma subset_fst_imageI: "A \<times> B \<subseteq> S \<Longrightarrow> y \<in> B \<Longrightarrow> A \<subseteq> fst ` S"
3554  unfolding image_def subset_eq by force
3555
3556lemma subset_snd_imageI: "A \<times> B \<subseteq> S \<Longrightarrow> x \<in> A \<Longrightarrow> B \<subseteq> snd ` S"
3557  unfolding image_def subset_eq by force
3558
3559lemma open_image_fst:
3560  assumes "open S"
3561  shows "open (fst ` S)"
3562proof (rule openI)
3563  fix x
3564  assume "x \<in> fst ` S"
3565  then obtain y where "(x, y) \<in> S"
3566    by auto
3567  then obtain A B where "open A" "open B" "x \<in> A" "y \<in> B" "A \<times> B \<subseteq> S"
3568    using \<open>open S\<close> unfolding open_prod_def by auto
3569  from \<open>A \<times> B \<subseteq> S\<close> \<open>y \<in> B\<close> have "A \<subseteq> fst ` S"
3570    by (rule subset_fst_imageI)
3571  with \<open>open A\<close> \<open>x \<in> A\<close> have "open A \<and> x \<in> A \<and> A \<subseteq> fst ` S"
3572    by simp
3573  then show "\<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> fst ` S" ..
3574qed
3575
3576lemma open_image_snd:
3577  assumes "open S"
3578  shows "open (snd ` S)"
3579proof (rule openI)
3580  fix y
3581  assume "y \<in> snd ` S"
3582  then obtain x where "(x, y) \<in> S"
3583    by auto
3584  then obtain A B where "open A" "open B" "x \<in> A" "y \<in> B" "A \<times> B \<subseteq> S"
3585    using \<open>open S\<close> unfolding open_prod_def by auto
3586  from \<open>A \<times> B \<subseteq> S\<close> \<open>x \<in> A\<close> have "B \<subseteq> snd ` S"
3587    by (rule subset_snd_imageI)
3588  with \<open>open B\<close> \<open>y \<in> B\<close> have "open B \<and> y \<in> B \<and> B \<subseteq> snd ` S"
3589    by simp
3590  then show "\<exists>T. open T \<and> y \<in> T \<and> T \<subseteq> snd ` S" ..
3591qed
3592
3593lemma nhds_prod: "nhds (a, b) = nhds a \<times>\<^sub>F nhds b"
3594  unfolding nhds_def
3595proof (subst prod_filter_INF, auto intro!: antisym INF_greatest simp: principal_prod_principal)
3596  fix S T
3597  assume "open S" "a \<in> S" "open T" "b \<in> T"
3598  then show "(INF x \<in> {S. open S \<and> (a, b) \<in> S}. principal x) \<le> principal (S \<times> T)"
3599    by (intro INF_lower) (auto intro!: open_Times)
3600next
3601  fix S'
3602  assume "open S'" "(a, b) \<in> S'"
3603  then obtain S T where "open S" "a \<in> S" "open T" "b \<in> T" "S \<times> T \<subseteq> S'"
3604    by (auto elim: open_prod_elim)
3605  then show "(INF x \<in> {S. open S \<and> a \<in> S}. INF y \<in> {S. open S \<and> b \<in> S}.
3606      principal (x \<times> y)) \<le> principal S'"
3607    by (auto intro!: INF_lower2)
3608qed
3609
3610
3611subsubsection \<open>Continuity of operations\<close>
3612
3613lemma tendsto_fst [tendsto_intros]:
3614  assumes "(f \<longlongrightarrow> a) F"
3615  shows "((\<lambda>x. fst (f x)) \<longlongrightarrow> fst a) F"
3616proof (rule topological_tendstoI)
3617  fix S
3618  assume "open S" and "fst a \<in> S"
3619  then have "open (fst -` S)" and "a \<in> fst -` S"
3620    by (simp_all add: open_vimage_fst)
3621  with assms have "eventually (\<lambda>x. f x \<in> fst -` S) F"
3622    by (rule topological_tendstoD)
3623  then show "eventually (\<lambda>x. fst (f x) \<in> S) F"
3624    by simp
3625qed
3626
3627lemma tendsto_snd [tendsto_intros]:
3628  assumes "(f \<longlongrightarrow> a) F"
3629  shows "((\<lambda>x. snd (f x)) \<longlongrightarrow> snd a) F"
3630proof (rule topological_tendstoI)
3631  fix S
3632  assume "open S" and "snd a \<in> S"
3633  then have "open (snd -` S)" and "a \<in> snd -` S"
3634    by (simp_all add: open_vimage_snd)
3635  with assms have "eventually (\<lambda>x. f x \<in> snd -` S) F"
3636    by (rule topological_tendstoD)
3637  then show "eventually (\<lambda>x. snd (f x) \<in> S) F"
3638    by simp
3639qed
3640
3641lemma tendsto_Pair [tendsto_intros]:
3642  assumes "(f \<longlongrightarrow> a) F" and "(g \<longlongrightarrow> b) F"
3643  shows "((\<lambda>x. (f x, g x)) \<longlongrightarrow> (a, b)) F"
3644  unfolding nhds_prod using assms by (rule filterlim_Pair)
3645
3646lemma continuous_fst[continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. fst (f x))"
3647  unfolding continuous_def by (rule tendsto_fst)
3648
3649lemma continuous_snd[continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. snd (f x))"
3650  unfolding continuous_def by (rule tendsto_snd)
3651
3652lemma continuous_Pair[continuous_intros]:
3653  "continuous F f \<Longrightarrow> continuous F g \<Longrightarrow> continuous F (\<lambda>x. (f x, g x))"
3654  unfolding continuous_def by (rule tendsto_Pair)
3655
3656lemma continuous_on_fst[continuous_intros]:
3657  "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. fst (f x))"
3658  unfolding continuous_on_def by (auto intro: tendsto_fst)
3659
3660lemma continuous_on_snd[continuous_intros]:
3661  "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. snd (f x))"
3662  unfolding continuous_on_def by (auto intro: tendsto_snd)
3663
3664lemma continuous_on_Pair[continuous_intros]:
3665  "continuous_on s f \<Longrightarrow> continuous_on s g \<Longrightarrow> continuous_on s (\<lambda>x. (f x, g x))"
3666  unfolding continuous_on_def by (auto intro: tendsto_Pair)
3667
3668lemma continuous_on_swap[continuous_intros]: "continuous_on A prod.swap"
3669  by (simp add: prod.swap_def continuous_on_fst continuous_on_snd
3670      continuous_on_Pair continuous_on_id)
3671
3672lemma continuous_on_swap_args:
3673  assumes "continuous_on (A\<times>B) (\<lambda>(x,y). d x y)"
3674    shows "continuous_on (B\<times>A) (\<lambda>(x,y). d y x)"
3675proof -
3676  have "(\<lambda>(x,y). d y x) = (\<lambda>(x,y). d x y) \<circ> prod.swap"
3677    by force
3678  then show ?thesis
3679    by (metis assms continuous_on_compose continuous_on_swap product_swap)
3680qed
3681
3682lemma isCont_fst [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. fst (f x)) a"
3683  by (fact continuous_fst)
3684
3685lemma isCont_snd [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. snd (f x)) a"
3686  by (fact continuous_snd)
3687
3688lemma isCont_Pair [simp]: "\<lbrakk>isCont f a; isCont g a\<rbrakk> \<Longrightarrow> isCont (\<lambda>x. (f x, g x)) a"
3689  by (fact continuous_Pair)
3690
3691lemma continuous_on_compose_Pair:
3692  assumes f: "continuous_on (Sigma A B) (\<lambda>(a, b). f a b)"
3693  assumes g: "continuous_on C g"
3694  assumes h: "continuous_on C h"
3695  assumes subset: "\<And>c. c \<in> C \<Longrightarrow> g c \<in> A" "\<And>c. c \<in> C \<Longrightarrow> h c \<in> B (g c)"
3696  shows "continuous_on C (\<lambda>c. f (g c) (h c))"
3697  using continuous_on_compose2[OF f continuous_on_Pair[OF g h]] subset
3698  by auto
3699
3700
3701subsubsection \<open>Connectedness of products\<close>
3702
3703proposition connected_Times:
3704  assumes S: "connected S" and T: "connected T"
3705  shows "connected (S \<times> T)"
3706proof (rule connectedI_const)
3707  fix P::"'a \<times> 'b \<Rightarrow> bool"
3708  assume P[THEN continuous_on_compose2, continuous_intros]: "continuous_on (S \<times> T) P"
3709  have "continuous_on S (\<lambda>s. P (s, t))" if "t \<in> T" for t
3710    by (auto intro!: continuous_intros that)
3711  from connectedD_const[OF S this]
3712  obtain c1 where c1: "\<And>s t. t \<in> T \<Longrightarrow> s \<in> S \<Longrightarrow> P (s, t) = c1 t"
3713    by metis
3714  moreover
3715  have "continuous_on T (\<lambda>t. P (s, t))" if "s \<in> S" for s
3716    by (auto intro!: continuous_intros that)
3717  from connectedD_const[OF T this]
3718  obtain c2 where "\<And>s t. t \<in> T \<Longrightarrow> s \<in> S \<Longrightarrow> P (s, t) = c2 s"
3719    by metis
3720  ultimately show "\<exists>c. \<forall>s\<in>S \<times> T. P s = c"
3721    by auto
3722qed
3723
3724corollary connected_Times_eq [simp]:
3725   "connected (S \<times> T) \<longleftrightarrow> S = {} \<or> T = {} \<or> connected S \<and> connected T"  (is "?lhs = ?rhs")
3726proof
3727  assume L: ?lhs
3728  show ?rhs
3729  proof cases
3730    assume "S \<noteq> {} \<and> T \<noteq> {}"
3731    moreover
3732    have "connected (fst ` (S \<times> T))" "connected (snd ` (S \<times> T))"
3733      using continuous_on_fst continuous_on_snd continuous_on_id
3734      by (blast intro: connected_continuous_image [OF _ L])+
3735    ultimately show ?thesis
3736      by auto
3737  qed auto
3738qed (auto simp: connected_Times)
3739
3740
3741subsubsection \<open>Separation axioms\<close>
3742
3743instance prod :: (t0_space, t0_space) t0_space
3744proof
3745  fix x y :: "'a \<times> 'b"
3746  assume "x \<noteq> y"
3747  then have "fst x \<noteq> fst y \<or> snd x \<noteq> snd y"
3748    by (simp add: prod_eq_iff)
3749  then show "\<exists>U. open U \<and> (x \<in> U) \<noteq> (y \<in> U)"
3750    by (fast dest: t0_space elim: open_vimage_fst open_vimage_snd)
3751qed
3752
3753instance prod :: (t1_space, t1_space) t1_space
3754proof
3755  fix x y :: "'a \<times> 'b"
3756  assume "x \<noteq> y"
3757  then have "fst x \<noteq> fst y \<or> snd x \<noteq> snd y"
3758    by (simp add: prod_eq_iff)
3759  then show "\<exists>U. open U \<and> x \<in> U \<and> y \<notin> U"
3760    by (fast dest: t1_space elim: open_vimage_fst open_vimage_snd)
3761qed
3762
3763instance prod :: (t2_space, t2_space) t2_space
3764proof
3765  fix x y :: "'a \<times> 'b"
3766  assume "x \<noteq> y"
3767  then have "fst x \<noteq> fst y \<or> snd x \<noteq> snd y"
3768    by (simp add: prod_eq_iff)
3769  then show "\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
3770    by (fast dest: hausdorff elim: open_vimage_fst open_vimage_snd)
3771qed
3772
3773lemma isCont_swap[continuous_intros]: "isCont prod.swap a"
3774  using continuous_on_eq_continuous_within continuous_on_swap by blast
3775
3776lemma open_diagonal_complement:
3777  "open {(x,y) |x y. x \<noteq> (y::('a::t2_space))}"
3778proof -
3779  have "open {(x, y). x \<noteq> (y::'a)}"
3780    unfolding split_def by (intro open_Collect_neq continuous_intros)
3781  also have "{(x, y). x \<noteq> (y::'a)} = {(x, y) |x y. x \<noteq> (y::'a)}"
3782    by auto
3783  finally show ?thesis .
3784qed
3785
3786lemma closed_diagonal:
3787  "closed {y. \<exists> x::('a::t2_space). y = (x,x)}"
3788proof -
3789  have "{y. \<exists> x::'a. y = (x,x)} = UNIV - {(x,y) | x y. x \<noteq> y}" by auto
3790  then show ?thesis using open_diagonal_complement closed_Diff by auto
3791qed
3792
3793lemma open_superdiagonal:
3794  "open {(x,y) | x y. x > (y::'a::{linorder_topology})}"
3795proof -
3796  have "open {(x, y). x > (y::'a)}"
3797    unfolding split_def by (intro open_Collect_less continuous_intros)
3798  also have "{(x, y). x > (y::'a)} = {(x, y) |x y. x > (y::'a)}"
3799    by auto
3800  finally show ?thesis .
3801qed
3802
3803lemma closed_subdiagonal:
3804  "closed {(x,y) | x y. x \<le> (y::'a::{linorder_topology})}"
3805proof -
3806  have "{(x,y) | x y. x \<le> (y::'a)} = UNIV - {(x,y) | x y. x > (y::'a)}" by auto
3807  then show ?thesis using open_superdiagonal closed_Diff by auto
3808qed
3809
3810lemma open_subdiagonal:
3811  "open {(x,y) | x y. x < (y::'a::{linorder_topology})}"
3812proof -
3813  have "open {(x, y). x < (y::'a)}"
3814    unfolding split_def by (intro open_Collect_less continuous_intros)
3815  also have "{(x, y). x < (y::'a)} = {(x, y) |x y. x < (y::'a)}"
3816    by auto
3817  finally show ?thesis .
3818qed
3819
3820lemma closed_superdiagonal:
3821  "closed {(x,y) | x y. x \<ge> (y::('a::{linorder_topology}))}"
3822proof -
3823  have "{(x,y) | x y. x \<ge> (y::'a)} = UNIV - {(x,y) | x y. x < y}" by auto
3824  then show ?thesis using open_subdiagonal closed_Diff by auto
3825qed
3826
3827end
3828