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