src/HOL/Topological_Spaces.thy
author wenzelm
Fri Jun 26 10:20:33 2015 +0200 (2015-06-26)
changeset 60585 48fdff264eb2
parent 60182 e1ea5a6379c9
child 60720 8c99fa3b7c44
permissions -rw-r--r--
tuned whitespace;
     1 (*  Title:      HOL/Topological_Spaces.thy
     2     Author:     Brian Huffman
     3     Author:     Johannes Hölzl
     4 *)
     5 
     6 section {* Topological Spaces *}
     7 
     8 theory Topological_Spaces
     9 imports Main Conditionally_Complete_Lattices
    10 begin
    11 
    12 named_theorems continuous_intros "structural introduction rules for continuity"
    13 
    14 
    15 subsection {* Topological space *}
    16 
    17 class "open" =
    18   fixes "open" :: "'a set \<Rightarrow> bool"
    19 
    20 class topological_space = "open" +
    21   assumes open_UNIV [simp, intro]: "open UNIV"
    22   assumes open_Int [intro]: "open S \<Longrightarrow> open T \<Longrightarrow> open (S \<inter> T)"
    23   assumes open_Union [intro]: "\<forall>S\<in>K. open S \<Longrightarrow> open (\<Union>K)"
    24 begin
    25 
    26 definition
    27   closed :: "'a set \<Rightarrow> bool" where
    28   "closed S \<longleftrightarrow> open (- S)"
    29 
    30 lemma open_empty [continuous_intros, intro, simp]: "open {}"
    31   using open_Union [of "{}"] by simp
    32 
    33 lemma open_Un [continuous_intros, intro]: "open S \<Longrightarrow> open T \<Longrightarrow> open (S \<union> T)"
    34   using open_Union [of "{S, T}"] by simp
    35 
    36 lemma open_UN [continuous_intros, intro]: "\<forall>x\<in>A. open (B x) \<Longrightarrow> open (\<Union>x\<in>A. B x)"
    37   using open_Union [of "B ` A"] by simp
    38 
    39 lemma open_Inter [continuous_intros, intro]: "finite S \<Longrightarrow> \<forall>T\<in>S. open T \<Longrightarrow> open (\<Inter>S)"
    40   by (induct set: finite) auto
    41 
    42 lemma open_INT [continuous_intros, intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. open (B x) \<Longrightarrow> open (\<Inter>x\<in>A. B x)"
    43   using open_Inter [of "B ` A"] by simp
    44 
    45 lemma openI:
    46   assumes "\<And>x. x \<in> S \<Longrightarrow> \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S"
    47   shows "open S"
    48 proof -
    49   have "open (\<Union>{T. open T \<and> T \<subseteq> S})" by auto
    50   moreover have "\<Union>{T. open T \<and> T \<subseteq> S} = S" by (auto dest!: assms)
    51   ultimately show "open S" by simp
    52 qed
    53 
    54 lemma closed_empty [continuous_intros, intro, simp]:  "closed {}"
    55   unfolding closed_def by simp
    56 
    57 lemma closed_Un [continuous_intros, intro]: "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<union> T)"
    58   unfolding closed_def by auto
    59 
    60 lemma closed_UNIV [continuous_intros, intro, simp]: "closed UNIV"
    61   unfolding closed_def by simp
    62 
    63 lemma closed_Int [continuous_intros, intro]: "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<inter> T)"
    64   unfolding closed_def by auto
    65 
    66 lemma closed_INT [continuous_intros, intro]: "\<forall>x\<in>A. closed (B x) \<Longrightarrow> closed (\<Inter>x\<in>A. B x)"
    67   unfolding closed_def by auto
    68 
    69 lemma closed_Inter [continuous_intros, intro]: "\<forall>S\<in>K. closed S \<Longrightarrow> closed (\<Inter>K)"
    70   unfolding closed_def uminus_Inf by auto
    71 
    72 lemma closed_Union [continuous_intros, intro]: "finite S \<Longrightarrow> \<forall>T\<in>S. closed T \<Longrightarrow> closed (\<Union>S)"
    73   by (induct set: finite) auto
    74 
    75 lemma closed_UN [continuous_intros, intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. closed (B x) \<Longrightarrow> closed (\<Union>x\<in>A. B x)"
    76   using closed_Union [of "B ` A"] by simp
    77 
    78 lemma open_closed: "open S \<longleftrightarrow> closed (- S)"
    79   unfolding closed_def by simp
    80 
    81 lemma closed_open: "closed S \<longleftrightarrow> open (- S)"
    82   unfolding closed_def by simp
    83 
    84 lemma open_Diff [continuous_intros, intro]: "open S \<Longrightarrow> closed T \<Longrightarrow> open (S - T)"
    85   unfolding closed_open Diff_eq by (rule open_Int)
    86 
    87 lemma closed_Diff [continuous_intros, intro]: "closed S \<Longrightarrow> open T \<Longrightarrow> closed (S - T)"
    88   unfolding open_closed Diff_eq by (rule closed_Int)
    89 
    90 lemma open_Compl [continuous_intros, intro]: "closed S \<Longrightarrow> open (- S)"
    91   unfolding closed_open .
    92 
    93 lemma closed_Compl [continuous_intros, intro]: "open S \<Longrightarrow> closed (- S)"
    94   unfolding open_closed .
    95 
    96 lemma open_Collect_neg: "closed {x. P x} \<Longrightarrow> open {x. \<not> P x}"
    97   unfolding Collect_neg_eq by (rule open_Compl)
    98 
    99 lemma open_Collect_conj: assumes "open {x. P x}" "open {x. Q x}" shows "open {x. P x \<and> Q x}"
   100   using open_Int[OF assms] by (simp add: Int_def)
   101 
   102 lemma open_Collect_disj: assumes "open {x. P x}" "open {x. Q x}" shows "open {x. P x \<or> Q x}"
   103   using open_Un[OF assms] by (simp add: Un_def)
   104 
   105 lemma open_Collect_ex: "(\<And>i. open {x. P i x}) \<Longrightarrow> open {x. \<exists>i. P i x}"
   106   using open_UN[of UNIV "\<lambda>i. {x. P i x}"] unfolding Collect_ex_eq by simp 
   107 
   108 lemma open_Collect_imp: "closed {x. P x} \<Longrightarrow> open {x. Q x} \<Longrightarrow> open {x. P x \<longrightarrow> Q x}"
   109   unfolding imp_conv_disj by (intro open_Collect_disj open_Collect_neg)
   110 
   111 lemma open_Collect_const: "open {x. P}"
   112   by (cases P) auto
   113 
   114 lemma closed_Collect_neg: "open {x. P x} \<Longrightarrow> closed {x. \<not> P x}"
   115   unfolding Collect_neg_eq by (rule closed_Compl)
   116 
   117 lemma closed_Collect_conj: assumes "closed {x. P x}" "closed {x. Q x}" shows "closed {x. P x \<and> Q x}"
   118   using closed_Int[OF assms] by (simp add: Int_def)
   119 
   120 lemma closed_Collect_disj: assumes "closed {x. P x}" "closed {x. Q x}" shows "closed {x. P x \<or> Q x}"
   121   using closed_Un[OF assms] by (simp add: Un_def)
   122 
   123 lemma closed_Collect_all: "(\<And>i. closed {x. P i x}) \<Longrightarrow> closed {x. \<forall>i. P i x}"
   124   using closed_INT[of UNIV "\<lambda>i. {x. P i x}"] unfolding Collect_all_eq by simp 
   125 
   126 lemma closed_Collect_imp: "open {x. P x} \<Longrightarrow> closed {x. Q x} \<Longrightarrow> closed {x. P x \<longrightarrow> Q x}"
   127   unfolding imp_conv_disj by (intro closed_Collect_disj closed_Collect_neg)
   128 
   129 lemma closed_Collect_const: "closed {x. P}"
   130   by (cases P) auto
   131 
   132 end
   133 
   134 subsection{* Hausdorff and other separation properties *}
   135 
   136 class t0_space = topological_space +
   137   assumes t0_space: "x \<noteq> y \<Longrightarrow> \<exists>U. open U \<and> \<not> (x \<in> U \<longleftrightarrow> y \<in> U)"
   138 
   139 class t1_space = topological_space +
   140   assumes t1_space: "x \<noteq> y \<Longrightarrow> \<exists>U. open U \<and> x \<in> U \<and> y \<notin> U"
   141 
   142 instance t1_space \<subseteq> t0_space
   143 proof qed (fast dest: t1_space)
   144 
   145 lemma separation_t1:
   146   fixes x y :: "'a::t1_space"
   147   shows "x \<noteq> y \<longleftrightarrow> (\<exists>U. open U \<and> x \<in> U \<and> y \<notin> U)"
   148   using t1_space[of x y] by blast
   149 
   150 lemma closed_singleton:
   151   fixes a :: "'a::t1_space"
   152   shows "closed {a}"
   153 proof -
   154   let ?T = "\<Union>{S. open S \<and> a \<notin> S}"
   155   have "open ?T" by (simp add: open_Union)
   156   also have "?T = - {a}"
   157     by (simp add: set_eq_iff separation_t1, auto)
   158   finally show "closed {a}" unfolding closed_def .
   159 qed
   160 
   161 lemma closed_insert [continuous_intros, simp]:
   162   fixes a :: "'a::t1_space"
   163   assumes "closed S" shows "closed (insert a S)"
   164 proof -
   165   from closed_singleton assms
   166   have "closed ({a} \<union> S)" by (rule closed_Un)
   167   thus "closed (insert a S)" by simp
   168 qed
   169 
   170 lemma finite_imp_closed:
   171   fixes S :: "'a::t1_space set"
   172   shows "finite S \<Longrightarrow> closed S"
   173 by (induct set: finite, simp_all)
   174 
   175 text {* T2 spaces are also known as Hausdorff spaces. *}
   176 
   177 class t2_space = topological_space +
   178   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 = {}"
   179 
   180 instance t2_space \<subseteq> t1_space
   181 proof qed (fast dest: hausdorff)
   182 
   183 lemma separation_t2:
   184   fixes x y :: "'a::t2_space"
   185   shows "x \<noteq> y \<longleftrightarrow> (\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {})"
   186   using hausdorff[of x y] by blast
   187 
   188 lemma separation_t0:
   189   fixes x y :: "'a::t0_space"
   190   shows "x \<noteq> y \<longleftrightarrow> (\<exists>U. open U \<and> ~(x\<in>U \<longleftrightarrow> y\<in>U))"
   191   using t0_space[of x y] by blast
   192 
   193 text {* A perfect space is a topological space with no isolated points. *}
   194 
   195 class perfect_space = topological_space +
   196   assumes not_open_singleton: "\<not> open {x}"
   197 
   198 
   199 subsection {* Generators for toplogies *}
   200 
   201 inductive generate_topology for S where
   202   UNIV: "generate_topology S UNIV"
   203 | Int: "generate_topology S a \<Longrightarrow> generate_topology S b \<Longrightarrow> generate_topology S (a \<inter> b)"
   204 | UN: "(\<And>k. k \<in> K \<Longrightarrow> generate_topology S k) \<Longrightarrow> generate_topology S (\<Union>K)"
   205 | Basis: "s \<in> S \<Longrightarrow> generate_topology S s"
   206 
   207 hide_fact (open) UNIV Int UN Basis 
   208 
   209 lemma generate_topology_Union: 
   210   "(\<And>k. k \<in> I \<Longrightarrow> generate_topology S (K k)) \<Longrightarrow> generate_topology S (\<Union>k\<in>I. K k)"
   211   using generate_topology.UN [of "K ` I"] by auto
   212 
   213 lemma topological_space_generate_topology:
   214   "class.topological_space (generate_topology S)"
   215   by default (auto intro: generate_topology.intros)
   216 
   217 subsection {* Order topologies *}
   218 
   219 class order_topology = order + "open" +
   220   assumes open_generated_order: "open = generate_topology (range (\<lambda>a. {..< a}) \<union> range (\<lambda>a. {a <..}))"
   221 begin
   222 
   223 subclass topological_space
   224   unfolding open_generated_order
   225   by (rule topological_space_generate_topology)
   226 
   227 lemma open_greaterThan [continuous_intros, simp]: "open {a <..}"
   228   unfolding open_generated_order by (auto intro: generate_topology.Basis)
   229 
   230 lemma open_lessThan [continuous_intros, simp]: "open {..< a}"
   231   unfolding open_generated_order by (auto intro: generate_topology.Basis)
   232 
   233 lemma open_greaterThanLessThan [continuous_intros, simp]: "open {a <..< b}"
   234    unfolding greaterThanLessThan_eq by (simp add: open_Int)
   235 
   236 end
   237 
   238 class linorder_topology = linorder + order_topology
   239 
   240 lemma closed_atMost [continuous_intros, simp]: "closed {.. a::'a::linorder_topology}"
   241   by (simp add: closed_open)
   242 
   243 lemma closed_atLeast [continuous_intros, simp]: "closed {a::'a::linorder_topology ..}"
   244   by (simp add: closed_open)
   245 
   246 lemma closed_atLeastAtMost [continuous_intros, simp]: "closed {a::'a::linorder_topology .. b}"
   247 proof -
   248   have "{a .. b} = {a ..} \<inter> {.. b}"
   249     by auto
   250   then show ?thesis
   251     by (simp add: closed_Int)
   252 qed
   253 
   254 lemma (in linorder) less_separate:
   255   assumes "x < y"
   256   shows "\<exists>a b. x \<in> {..< a} \<and> y \<in> {b <..} \<and> {..< a} \<inter> {b <..} = {}"
   257 proof (cases "\<exists>z. x < z \<and> z < y")
   258   case True
   259   then obtain z where "x < z \<and> z < y" ..
   260   then have "x \<in> {..< z} \<and> y \<in> {z <..} \<and> {z <..} \<inter> {..< z} = {}"
   261     by auto
   262   then show ?thesis by blast
   263 next
   264   case False
   265   with `x < y` have "x \<in> {..< y} \<and> y \<in> {x <..} \<and> {x <..} \<inter> {..< y} = {}"
   266     by auto
   267   then show ?thesis by blast
   268 qed
   269 
   270 instance linorder_topology \<subseteq> t2_space
   271 proof
   272   fix x y :: 'a
   273   from less_separate[of x y] less_separate[of y x]
   274   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 = {}"
   275     by (elim neqE) (metis open_lessThan open_greaterThan Int_commute)+
   276 qed
   277 
   278 lemma (in linorder_topology) open_right:
   279   assumes "open S" "x \<in> S" and gt_ex: "x < y" shows "\<exists>b>x. {x ..< b} \<subseteq> S"
   280   using assms unfolding open_generated_order
   281 proof induction
   282   case (Int A B)
   283   then obtain a b where "a > x" "{x ..< a} \<subseteq> A"  "b > x" "{x ..< b} \<subseteq> B" by auto
   284   then show ?case by (auto intro!: exI[of _ "min a b"])
   285 next
   286   case (Basis S) then show ?case by (fastforce intro: exI[of _ y] gt_ex)
   287 qed blast+
   288 
   289 lemma (in linorder_topology) open_left:
   290   assumes "open S" "x \<in> S" and lt_ex: "y < x" shows "\<exists>b<x. {b <.. x} \<subseteq> S"
   291   using assms unfolding open_generated_order
   292 proof induction
   293   case (Int A B)
   294   then obtain a b where "a < x" "{a <.. x} \<subseteq> A"  "b < x" "{b <.. x} \<subseteq> B" by auto
   295   then show ?case by (auto intro!: exI[of _ "max a b"])
   296 next
   297   case (Basis S) then show ?case by (fastforce intro: exI[of _ y] lt_ex)
   298 qed blast+
   299 
   300 subsubsection {* Boolean is an order topology *}
   301 
   302 text {* It also is a discrete topology, but don't have a type class for it (yet). *}
   303 
   304 instantiation bool :: order_topology
   305 begin
   306 
   307 definition open_bool :: "bool set \<Rightarrow> bool" where
   308   "open_bool = generate_topology (range (\<lambda>a. {..< a}) \<union> range (\<lambda>a. {a <..}))"
   309 
   310 instance
   311   proof qed (rule open_bool_def)
   312 
   313 end
   314 
   315 lemma open_bool[simp, intro!]: "open (A::bool set)"
   316 proof -
   317   have *: "{False <..} = {True}" "{..< True} = {False}"
   318     by auto
   319   have "A = UNIV \<or> A = {} \<or> A = {False <..} \<or> A = {..< True}"
   320     using subset_UNIV[of A] unfolding UNIV_bool * by auto
   321   then show "open A"
   322     by auto
   323 qed
   324 
   325 subsubsection {* Topological filters *}
   326 
   327 definition (in topological_space) nhds :: "'a \<Rightarrow> 'a filter"
   328   where "nhds a = (INF S:{S. open S \<and> a \<in> S}. principal S)"
   329 
   330 definition (in topological_space) at_within :: "'a \<Rightarrow> 'a set \<Rightarrow> 'a filter" ("at (_) within (_)" [1000, 60] 60)
   331   where "at a within s = inf (nhds a) (principal (s - {a}))"
   332 
   333 abbreviation (in topological_space) at :: "'a \<Rightarrow> 'a filter" ("at") where
   334   "at x \<equiv> at x within (CONST UNIV)"
   335 
   336 abbreviation (in order_topology) at_right :: "'a \<Rightarrow> 'a filter" where
   337   "at_right x \<equiv> at x within {x <..}"
   338 
   339 abbreviation (in order_topology) at_left :: "'a \<Rightarrow> 'a filter" where
   340   "at_left x \<equiv> at x within {..< x}"
   341 
   342 lemma (in topological_space) nhds_generated_topology:
   343   "open = generate_topology T \<Longrightarrow> nhds x = (INF S:{S\<in>T. x \<in> S}. principal S)"
   344   unfolding nhds_def
   345 proof (safe intro!: antisym INF_greatest)
   346   fix S assume "generate_topology T S" "x \<in> S"
   347   then show "(INF S:{S \<in> T. x \<in> S}. principal S) \<le> principal S"
   348     by induction 
   349        (auto intro: INF_lower order_trans simp add: inf_principal[symmetric] simp del: inf_principal)
   350 qed (auto intro!: INF_lower intro: generate_topology.intros)
   351 
   352 lemma (in topological_space) eventually_nhds:
   353   "eventually P (nhds a) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x))"
   354   unfolding nhds_def by (subst eventually_INF_base) (auto simp: eventually_principal)
   355 
   356 lemma nhds_neq_bot [simp]: "nhds a \<noteq> bot"
   357   unfolding trivial_limit_def eventually_nhds by simp
   358 
   359 lemma (in t1_space) t1_space_nhds:
   360   "x \<noteq> y \<Longrightarrow> (\<forall>\<^sub>F x in nhds x. x \<noteq> y)"
   361   by (drule t1_space) (auto simp: eventually_nhds)
   362 
   363 lemma at_within_eq: "at x within s = (INF S:{S. open S \<and> x \<in> S}. principal (S \<inter> s - {x}))"
   364   unfolding nhds_def at_within_def by (subst INF_inf_const2[symmetric]) (auto simp add: Diff_Int_distrib)
   365 
   366 lemma eventually_at_filter:
   367   "eventually P (at a within s) \<longleftrightarrow> eventually (\<lambda>x. x \<noteq> a \<longrightarrow> x \<in> s \<longrightarrow> P x) (nhds a)"
   368   unfolding at_within_def eventually_inf_principal by (simp add: imp_conjL[symmetric] conj_commute)
   369 
   370 lemma at_le: "s \<subseteq> t \<Longrightarrow> at x within s \<le> at x within t"
   371   unfolding at_within_def by (intro inf_mono) auto
   372 
   373 lemma eventually_at_topological:
   374   "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))"
   375   unfolding eventually_nhds eventually_at_filter by simp
   376 
   377 lemma at_within_open: "a \<in> S \<Longrightarrow> open S \<Longrightarrow> at a within S = at a"
   378   unfolding filter_eq_iff eventually_at_topological by (metis open_Int Int_iff UNIV_I)
   379 
   380 lemma at_within_empty [simp]: "at a within {} = bot"
   381   unfolding at_within_def by simp
   382 
   383 lemma at_within_union: "at x within (S \<union> T) = sup (at x within S) (at x within T)"
   384   unfolding filter_eq_iff eventually_sup eventually_at_filter
   385   by (auto elim!: eventually_rev_mp)
   386 
   387 lemma at_eq_bot_iff: "at a = bot \<longleftrightarrow> open {a}"
   388   unfolding trivial_limit_def eventually_at_topological
   389   by (safe, case_tac "S = {a}", simp, fast, fast)
   390 
   391 lemma at_neq_bot [simp]: "at (a::'a::perfect_space) \<noteq> bot"
   392   by (simp add: at_eq_bot_iff not_open_singleton)
   393 
   394 lemma (in order_topology) nhds_order: "nhds x =
   395   inf (INF a:{x <..}. principal {..< a}) (INF a:{..< x}. principal {a <..})"
   396 proof -
   397   have 1: "{S \<in> range lessThan \<union> range greaterThan. x \<in> S} = 
   398       (\<lambda>a. {..< a}) ` {x <..} \<union> (\<lambda>a. {a <..}) ` {..< x}"
   399     by auto
   400   show ?thesis
   401     unfolding nhds_generated_topology[OF open_generated_order] INF_union 1 INF_image comp_def ..
   402 qed
   403 
   404 lemma (in linorder_topology) at_within_order: "UNIV \<noteq> {x} \<Longrightarrow> 
   405   at x within s = inf (INF a:{x <..}. principal ({..< a} \<inter> s - {x}))
   406                       (INF a:{..< x}. principal ({a <..} \<inter> s - {x}))"
   407 proof (cases "{x <..} = {}" "{..< x} = {}" rule: case_split[case_product case_split])
   408   assume "UNIV \<noteq> {x}" "{x<..} = {}" "{..< x} = {}"
   409   moreover have "UNIV = {..< x} \<union> {x} \<union> {x <..}"
   410     by auto
   411   ultimately show ?thesis
   412     by auto
   413 qed (auto simp: at_within_def nhds_order Int_Diff inf_principal[symmetric] INF_inf_const2
   414                 inf_sup_aci[where 'a="'a filter"]
   415           simp del: inf_principal)
   416 
   417 lemma (in linorder_topology) at_left_eq:
   418   "y < x \<Longrightarrow> at_left x = (INF a:{..< x}. principal {a <..< x})"
   419   by (subst at_within_order)
   420      (auto simp: greaterThan_Int_greaterThan greaterThanLessThan_eq[symmetric] min.absorb2 INF_constant
   421            intro!: INF_lower2 inf_absorb2)
   422 
   423 lemma (in linorder_topology) eventually_at_left:
   424   "y < x \<Longrightarrow> eventually P (at_left x) \<longleftrightarrow> (\<exists>b<x. \<forall>y>b. y < x \<longrightarrow> P y)"
   425   unfolding at_left_eq by (subst eventually_INF_base) (auto simp: eventually_principal Ball_def)
   426 
   427 lemma (in linorder_topology) at_right_eq:
   428   "x < y \<Longrightarrow> at_right x = (INF a:{x <..}. principal {x <..< a})"
   429   by (subst at_within_order)
   430      (auto simp: lessThan_Int_lessThan greaterThanLessThan_eq[symmetric] max.absorb2 INF_constant Int_commute
   431            intro!: INF_lower2 inf_absorb1)
   432 
   433 lemma (in linorder_topology) eventually_at_right:
   434   "x < y \<Longrightarrow> eventually P (at_right x) \<longleftrightarrow> (\<exists>b>x. \<forall>y>x. y < b \<longrightarrow> P y)"
   435   unfolding at_right_eq by (subst eventually_INF_base) (auto simp: eventually_principal Ball_def)
   436 
   437 lemma trivial_limit_at_right_top: "at_right (top::_::{order_top, linorder_topology}) = bot"
   438   unfolding filter_eq_iff eventually_at_topological by auto
   439 
   440 lemma trivial_limit_at_left_bot: "at_left (bot::_::{order_bot, linorder_topology}) = bot"
   441   unfolding filter_eq_iff eventually_at_topological by auto
   442 
   443 lemma trivial_limit_at_left_real [simp]:
   444   "\<not> trivial_limit (at_left (x::'a::{no_bot, dense_order, linorder_topology}))"
   445   using lt_ex[of x]
   446   by safe (auto simp add: trivial_limit_def eventually_at_left dest: dense)
   447 
   448 lemma trivial_limit_at_right_real [simp]:
   449   "\<not> trivial_limit (at_right (x::'a::{no_top, dense_order, linorder_topology}))"
   450   using gt_ex[of x]
   451   by safe (auto simp add: trivial_limit_def eventually_at_right dest: dense)
   452 
   453 lemma at_eq_sup_left_right: "at (x::'a::linorder_topology) = sup (at_left x) (at_right x)"
   454   by (auto simp: eventually_at_filter filter_eq_iff eventually_sup 
   455            elim: eventually_elim2 eventually_elim1)
   456 
   457 lemma eventually_at_split:
   458   "eventually P (at (x::'a::linorder_topology)) \<longleftrightarrow> eventually P (at_left x) \<and> eventually P (at_right x)"
   459   by (subst at_eq_sup_left_right) (simp add: eventually_sup)
   460 
   461 subsubsection {* Tendsto *}
   462 
   463 abbreviation (in topological_space)
   464   tendsto :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b filter \<Rightarrow> bool" (infixr "--->" 55) where
   465   "(f ---> l) F \<equiv> filterlim f (nhds l) F"
   466 
   467 definition (in t2_space) Lim :: "'f filter \<Rightarrow> ('f \<Rightarrow> 'a) \<Rightarrow> 'a" where
   468   "Lim A f = (THE l. (f ---> l) A)"
   469 
   470 lemma tendsto_eq_rhs: "(f ---> x) F \<Longrightarrow> x = y \<Longrightarrow> (f ---> y) F"
   471   by simp
   472 
   473 named_theorems tendsto_intros "introduction rules for tendsto"
   474 setup {*
   475   Global_Theory.add_thms_dynamic (@{binding tendsto_eq_intros},
   476     fn context =>
   477       Named_Theorems.get (Context.proof_of context) @{named_theorems tendsto_intros}
   478       |> map_filter (try (fn thm => @{thm tendsto_eq_rhs} OF [thm])))
   479 *}
   480 
   481 lemma (in topological_space) tendsto_def:
   482    "(f ---> l) F \<longleftrightarrow> (\<forall>S. open S \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) F)"
   483    unfolding nhds_def filterlim_INF filterlim_principal by auto
   484 
   485 lemma tendsto_mono: "F \<le> F' \<Longrightarrow> (f ---> l) F' \<Longrightarrow> (f ---> l) F"
   486   unfolding tendsto_def le_filter_def by fast
   487 
   488 lemma tendsto_within_subset: "(f ---> l) (at x within S) \<Longrightarrow> T \<subseteq> S \<Longrightarrow> (f ---> l) (at x within T)"
   489   by (blast intro: tendsto_mono at_le)
   490 
   491 lemma filterlim_at:
   492   "(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 ---> b) F)"
   493   by (simp add: at_within_def filterlim_inf filterlim_principal conj_commute)
   494 
   495 lemma (in topological_space) topological_tendstoI:
   496   "(\<And>S. open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F) \<Longrightarrow> (f ---> l) F"
   497   unfolding tendsto_def by auto
   498 
   499 lemma (in topological_space) topological_tendstoD:
   500   "(f ---> l) F \<Longrightarrow> open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F"
   501   unfolding tendsto_def by auto
   502 
   503 lemma (in order_topology) order_tendsto_iff:
   504   "(f ---> x) F \<longleftrightarrow> (\<forall>l<x. eventually (\<lambda>x. l < f x) F) \<and> (\<forall>u>x. eventually (\<lambda>x. f x < u) F)"
   505   unfolding nhds_order filterlim_inf filterlim_INF filterlim_principal by auto
   506 
   507 lemma (in order_topology) order_tendstoI:
   508   "(\<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>
   509     (f ---> y) F"
   510   unfolding order_tendsto_iff by auto
   511 
   512 lemma (in order_topology) order_tendstoD:
   513   assumes "(f ---> y) F"
   514   shows "a < y \<Longrightarrow> eventually (\<lambda>x. a < f x) F"
   515     and "y < a \<Longrightarrow> eventually (\<lambda>x. f x < a) F"
   516   using assms unfolding order_tendsto_iff by auto
   517 
   518 lemma tendsto_bot [simp]: "(f ---> a) bot"
   519   unfolding tendsto_def by simp
   520 
   521 lemma (in linorder_topology) tendsto_max:
   522   assumes X: "(X ---> x) net"
   523   assumes Y: "(Y ---> y) net"
   524   shows "((\<lambda>x. max (X x) (Y x)) ---> max x y) net"
   525 proof (rule order_tendstoI)
   526   fix a assume "a < max x y"
   527   then show "eventually (\<lambda>x. a < max (X x) (Y x)) net"
   528     using order_tendstoD(1)[OF X, of a] order_tendstoD(1)[OF Y, of a]
   529     by (auto simp: less_max_iff_disj elim: eventually_elim1)
   530 next
   531   fix a assume "max x y < a"
   532   then show "eventually (\<lambda>x. max (X x) (Y x) < a) net"
   533     using order_tendstoD(2)[OF X, of a] order_tendstoD(2)[OF Y, of a]
   534     by (auto simp: eventually_conj_iff)
   535 qed
   536 
   537 lemma (in linorder_topology) tendsto_min:
   538   assumes X: "(X ---> x) net"
   539   assumes Y: "(Y ---> y) net"
   540   shows "((\<lambda>x. min (X x) (Y x)) ---> min x y) net"
   541 proof (rule order_tendstoI)
   542   fix a assume "a < min x y"
   543   then show "eventually (\<lambda>x. a < min (X x) (Y x)) net"
   544     using order_tendstoD(1)[OF X, of a] order_tendstoD(1)[OF Y, of a]
   545     by (auto simp: eventually_conj_iff)
   546 next
   547   fix a assume "min x y < a"
   548   then show "eventually (\<lambda>x. min (X x) (Y x) < a) net"
   549     using order_tendstoD(2)[OF X, of a] order_tendstoD(2)[OF Y, of a]
   550     by (auto simp: min_less_iff_disj elim: eventually_elim1)
   551 qed
   552 
   553 lemma tendsto_ident_at [tendsto_intros, simp, intro]: "((\<lambda>x. x) ---> a) (at a within s)"
   554   unfolding tendsto_def eventually_at_topological by auto
   555 
   556 lemma (in topological_space) tendsto_const [tendsto_intros, simp, intro]: "((\<lambda>x. k) ---> k) F"
   557   by (simp add: tendsto_def)
   558 
   559 lemma (in t2_space) tendsto_unique:
   560   assumes "F \<noteq> bot" and "(f ---> a) F" and "(f ---> b) F"
   561   shows "a = b"
   562 proof (rule ccontr)
   563   assume "a \<noteq> b"
   564   obtain U V where "open U" "open V" "a \<in> U" "b \<in> V" "U \<inter> V = {}"
   565     using hausdorff [OF `a \<noteq> b`] by fast
   566   have "eventually (\<lambda>x. f x \<in> U) F"
   567     using `(f ---> a) F` `open U` `a \<in> U` by (rule topological_tendstoD)
   568   moreover
   569   have "eventually (\<lambda>x. f x \<in> V) F"
   570     using `(f ---> b) F` `open V` `b \<in> V` by (rule topological_tendstoD)
   571   ultimately
   572   have "eventually (\<lambda>x. False) F"
   573   proof eventually_elim
   574     case (elim x)
   575     hence "f x \<in> U \<inter> V" by simp
   576     with `U \<inter> V = {}` show ?case by simp
   577   qed
   578   with `\<not> trivial_limit F` show "False"
   579     by (simp add: trivial_limit_def)
   580 qed
   581 
   582 lemma (in t2_space) tendsto_const_iff:
   583   assumes "\<not> trivial_limit F" shows "((\<lambda>x. a :: 'a) ---> b) F \<longleftrightarrow> a = b"
   584   by (auto intro!: tendsto_unique [OF assms tendsto_const])
   585 
   586 lemma increasing_tendsto:
   587   fixes f :: "_ \<Rightarrow> 'a::order_topology"
   588   assumes bdd: "eventually (\<lambda>n. f n \<le> l) F"
   589       and en: "\<And>x. x < l \<Longrightarrow> eventually (\<lambda>n. x < f n) F"
   590   shows "(f ---> l) F"
   591   using assms by (intro order_tendstoI) (auto elim!: eventually_elim1)
   592 
   593 lemma decreasing_tendsto:
   594   fixes f :: "_ \<Rightarrow> 'a::order_topology"
   595   assumes bdd: "eventually (\<lambda>n. l \<le> f n) F"
   596       and en: "\<And>x. l < x \<Longrightarrow> eventually (\<lambda>n. f n < x) F"
   597   shows "(f ---> l) F"
   598   using assms by (intro order_tendstoI) (auto elim!: eventually_elim1)
   599 
   600 lemma tendsto_sandwich:
   601   fixes f g h :: "'a \<Rightarrow> 'b::order_topology"
   602   assumes ev: "eventually (\<lambda>n. f n \<le> g n) net" "eventually (\<lambda>n. g n \<le> h n) net"
   603   assumes lim: "(f ---> c) net" "(h ---> c) net"
   604   shows "(g ---> c) net"
   605 proof (rule order_tendstoI)
   606   fix a show "a < c \<Longrightarrow> eventually (\<lambda>x. a < g x) net"
   607     using order_tendstoD[OF lim(1), of a] ev by (auto elim: eventually_elim2)
   608 next
   609   fix a show "c < a \<Longrightarrow> eventually (\<lambda>x. g x < a) net"
   610     using order_tendstoD[OF lim(2), of a] ev by (auto elim: eventually_elim2)
   611 qed
   612 
   613 lemma tendsto_le:
   614   fixes f g :: "'a \<Rightarrow> 'b::linorder_topology"
   615   assumes F: "\<not> trivial_limit F"
   616   assumes x: "(f ---> x) F" and y: "(g ---> y) F"
   617   assumes ev: "eventually (\<lambda>x. g x \<le> f x) F"
   618   shows "y \<le> x"
   619 proof (rule ccontr)
   620   assume "\<not> y \<le> x"
   621   with less_separate[of x y] obtain a b where xy: "x < a" "b < y" "{..<a} \<inter> {b<..} = {}"
   622     by (auto simp: not_le)
   623   then have "eventually (\<lambda>x. f x < a) F" "eventually (\<lambda>x. b < g x) F"
   624     using x y by (auto intro: order_tendstoD)
   625   with ev have "eventually (\<lambda>x. False) F"
   626     by eventually_elim (insert xy, fastforce)
   627   with F show False
   628     by (simp add: eventually_False)
   629 qed
   630 
   631 lemma tendsto_le_const:
   632   fixes f :: "'a \<Rightarrow> 'b::linorder_topology"
   633   assumes F: "\<not> trivial_limit F"
   634   assumes x: "(f ---> x) F" and a: "eventually (\<lambda>i. a \<le> f i) F"
   635   shows "a \<le> x"
   636   using F x tendsto_const a by (rule tendsto_le)
   637 
   638 lemma tendsto_ge_const:
   639   fixes f :: "'a \<Rightarrow> 'b::linorder_topology"
   640   assumes F: "\<not> trivial_limit F"
   641   assumes x: "(f ---> x) F" and a: "eventually (\<lambda>i. a \<ge> f i) F"
   642   shows "a \<ge> x"
   643   by (rule tendsto_le [OF F tendsto_const x a])
   644 
   645 subsubsection {* Rules about @{const Lim} *}
   646 
   647 lemma tendsto_Lim:
   648   "\<not>(trivial_limit net) \<Longrightarrow> (f ---> l) net \<Longrightarrow> Lim net f = l"
   649   unfolding Lim_def using tendsto_unique[of net f] by auto
   650 
   651 lemma Lim_ident_at: "\<not> trivial_limit (at x within s) \<Longrightarrow> Lim (at x within s) (\<lambda>x. x) = x"
   652   by (rule tendsto_Lim[OF _ tendsto_ident_at]) auto
   653 
   654 lemma filterlim_at_bot_at_right:
   655   fixes f :: "'a::linorder_topology \<Rightarrow> 'b::linorder"
   656   assumes mono: "\<And>x y. Q x \<Longrightarrow> Q y \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
   657   assumes bij: "\<And>x. P x \<Longrightarrow> f (g x) = x" "\<And>x. P x \<Longrightarrow> Q (g x)"
   658   assumes Q: "eventually Q (at_right a)" and bound: "\<And>b. Q b \<Longrightarrow> a < b"
   659   assumes P: "eventually P at_bot"
   660   shows "filterlim f at_bot (at_right a)"
   661 proof -
   662   from P obtain x where x: "\<And>y. y \<le> x \<Longrightarrow> P y"
   663     unfolding eventually_at_bot_linorder by auto
   664   show ?thesis
   665   proof (intro filterlim_at_bot_le[THEN iffD2] allI impI)
   666     fix z assume "z \<le> x"
   667     with x have "P z" by auto
   668     have "eventually (\<lambda>x. x \<le> g z) (at_right a)"
   669       using bound[OF bij(2)[OF `P z`]]
   670       unfolding eventually_at_right[OF bound[OF bij(2)[OF `P z`]]] by (auto intro!: exI[of _ "g z"])
   671     with Q show "eventually (\<lambda>x. f x \<le> z) (at_right a)"
   672       by eventually_elim (metis bij `P z` mono)
   673   qed
   674 qed
   675 
   676 lemma filterlim_at_top_at_left:
   677   fixes f :: "'a::linorder_topology \<Rightarrow> 'b::linorder"
   678   assumes mono: "\<And>x y. Q x \<Longrightarrow> Q y \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
   679   assumes bij: "\<And>x. P x \<Longrightarrow> f (g x) = x" "\<And>x. P x \<Longrightarrow> Q (g x)"
   680   assumes Q: "eventually Q (at_left a)" and bound: "\<And>b. Q b \<Longrightarrow> b < a"
   681   assumes P: "eventually P at_top"
   682   shows "filterlim f at_top (at_left a)"
   683 proof -
   684   from P obtain x where x: "\<And>y. x \<le> y \<Longrightarrow> P y"
   685     unfolding eventually_at_top_linorder by auto
   686   show ?thesis
   687   proof (intro filterlim_at_top_ge[THEN iffD2] allI impI)
   688     fix z assume "x \<le> z"
   689     with x have "P z" by auto
   690     have "eventually (\<lambda>x. g z \<le> x) (at_left a)"
   691       using bound[OF bij(2)[OF `P z`]]
   692       unfolding eventually_at_left[OF bound[OF bij(2)[OF `P z`]]] by (auto intro!: exI[of _ "g z"])
   693     with Q show "eventually (\<lambda>x. z \<le> f x) (at_left a)"
   694       by eventually_elim (metis bij `P z` mono)
   695   qed
   696 qed
   697 
   698 lemma filterlim_split_at:
   699   "filterlim f F (at_left x) \<Longrightarrow> filterlim f F (at_right x) \<Longrightarrow> filterlim f F (at (x::'a::linorder_topology))"
   700   by (subst at_eq_sup_left_right) (rule filterlim_sup)
   701 
   702 lemma filterlim_at_split:
   703   "filterlim f F (at (x::'a::linorder_topology)) \<longleftrightarrow> filterlim f F (at_left x) \<and> filterlim f F (at_right x)"
   704   by (subst at_eq_sup_left_right) (simp add: filterlim_def filtermap_sup)
   705 
   706 lemma eventually_nhds_top:
   707   fixes P :: "'a :: {order_top, linorder_topology} \<Rightarrow> bool"
   708   assumes "(b::'a) < top"
   709   shows "eventually P (nhds top) \<longleftrightarrow> (\<exists>b<top. (\<forall>z. b < z \<longrightarrow> P z))"
   710   unfolding eventually_nhds
   711 proof safe
   712   fix S :: "'a set" assume "open S" "top \<in> S"
   713   note open_left[OF this `b < top`]
   714   moreover assume "\<forall>s\<in>S. P s"
   715   ultimately show "\<exists>b<top. \<forall>z>b. P z"
   716     by (auto simp: subset_eq Ball_def)
   717 next
   718   fix b assume "b < top" "\<forall>z>b. P z"
   719   then show "\<exists>S. open S \<and> top \<in> S \<and> (\<forall>xa\<in>S. P xa)"
   720     by (intro exI[of _ "{b <..}"]) auto
   721 qed
   722 
   723 lemma tendsto_at_within_iff_tendsto_nhds:
   724   "(g ---> g l) (at l within S) \<longleftrightarrow> (g ---> g l) (inf (nhds l) (principal S))"
   725   unfolding tendsto_def eventually_at_filter eventually_inf_principal
   726   by (intro ext all_cong imp_cong) (auto elim!: eventually_elim1)
   727 
   728 subsection {* Limits on sequences *}
   729 
   730 abbreviation (in topological_space)
   731   LIMSEQ :: "[nat \<Rightarrow> 'a, 'a] \<Rightarrow> bool"
   732     ("((_)/ ----> (_))" [60, 60] 60) where
   733   "X ----> L \<equiv> (X ---> L) sequentially"
   734 
   735 abbreviation (in t2_space) lim :: "(nat \<Rightarrow> 'a) \<Rightarrow> 'a" where
   736   "lim X \<equiv> Lim sequentially X"
   737 
   738 definition (in topological_space) convergent :: "(nat \<Rightarrow> 'a) \<Rightarrow> bool" where
   739   "convergent X = (\<exists>L. X ----> L)"
   740 
   741 lemma lim_def: "lim X = (THE L. X ----> L)"
   742   unfolding Lim_def ..
   743 
   744 subsubsection {* Monotone sequences and subsequences *}
   745 
   746 definition
   747   monoseq :: "(nat \<Rightarrow> 'a::order) \<Rightarrow> bool" where
   748     --{*Definition of monotonicity.
   749         The use of disjunction here complicates proofs considerably.
   750         One alternative is to add a Boolean argument to indicate the direction.
   751         Another is to develop the notions of increasing and decreasing first.*}
   752   "monoseq X = ((\<forall>m. \<forall>n\<ge>m. X m \<le> X n) \<or> (\<forall>m. \<forall>n\<ge>m. X n \<le> X m))"
   753 
   754 abbreviation incseq :: "(nat \<Rightarrow> 'a::order) \<Rightarrow> bool" where
   755   "incseq X \<equiv> mono X"
   756 
   757 lemma incseq_def: "incseq X \<longleftrightarrow> (\<forall>m. \<forall>n\<ge>m. X n \<ge> X m)"
   758   unfolding mono_def ..
   759 
   760 abbreviation decseq :: "(nat \<Rightarrow> 'a::order) \<Rightarrow> bool" where
   761   "decseq X \<equiv> antimono X"
   762 
   763 lemma decseq_def: "decseq X \<longleftrightarrow> (\<forall>m. \<forall>n\<ge>m. X n \<le> X m)"
   764   unfolding antimono_def ..
   765 
   766 definition
   767   subseq :: "(nat \<Rightarrow> nat) \<Rightarrow> bool" where
   768     --{*Definition of subsequence*}
   769   "subseq f \<longleftrightarrow> (\<forall>m. \<forall>n>m. f m < f n)"
   770 
   771 lemma incseq_SucI:
   772   "(\<And>n. X n \<le> X (Suc n)) \<Longrightarrow> incseq X"
   773   using lift_Suc_mono_le[of X]
   774   by (auto simp: incseq_def)
   775 
   776 lemma incseqD: "\<And>i j. incseq f \<Longrightarrow> i \<le> j \<Longrightarrow> f i \<le> f j"
   777   by (auto simp: incseq_def)
   778 
   779 lemma incseq_SucD: "incseq A \<Longrightarrow> A i \<le> A (Suc i)"
   780   using incseqD[of A i "Suc i"] by auto
   781 
   782 lemma incseq_Suc_iff: "incseq f \<longleftrightarrow> (\<forall>n. f n \<le> f (Suc n))"
   783   by (auto intro: incseq_SucI dest: incseq_SucD)
   784 
   785 lemma incseq_const[simp, intro]: "incseq (\<lambda>x. k)"
   786   unfolding incseq_def by auto
   787 
   788 lemma decseq_SucI:
   789   "(\<And>n. X (Suc n) \<le> X n) \<Longrightarrow> decseq X"
   790   using order.lift_Suc_mono_le[OF dual_order, of X]
   791   by (auto simp: decseq_def)
   792 
   793 lemma decseqD: "\<And>i j. decseq f \<Longrightarrow> i \<le> j \<Longrightarrow> f j \<le> f i"
   794   by (auto simp: decseq_def)
   795 
   796 lemma decseq_SucD: "decseq A \<Longrightarrow> A (Suc i) \<le> A i"
   797   using decseqD[of A i "Suc i"] by auto
   798 
   799 lemma decseq_Suc_iff: "decseq f \<longleftrightarrow> (\<forall>n. f (Suc n) \<le> f n)"
   800   by (auto intro: decseq_SucI dest: decseq_SucD)
   801 
   802 lemma decseq_const[simp, intro]: "decseq (\<lambda>x. k)"
   803   unfolding decseq_def by auto
   804 
   805 lemma monoseq_iff: "monoseq X \<longleftrightarrow> incseq X \<or> decseq X"
   806   unfolding monoseq_def incseq_def decseq_def ..
   807 
   808 lemma monoseq_Suc:
   809   "monoseq X \<longleftrightarrow> (\<forall>n. X n \<le> X (Suc n)) \<or> (\<forall>n. X (Suc n) \<le> X n)"
   810   unfolding monoseq_iff incseq_Suc_iff decseq_Suc_iff ..
   811 
   812 lemma monoI1: "\<forall>m. \<forall> n \<ge> m. X m \<le> X n ==> monoseq X"
   813 by (simp add: monoseq_def)
   814 
   815 lemma monoI2: "\<forall>m. \<forall> n \<ge> m. X n \<le> X m ==> monoseq X"
   816 by (simp add: monoseq_def)
   817 
   818 lemma mono_SucI1: "\<forall>n. X n \<le> X (Suc n) ==> monoseq X"
   819 by (simp add: monoseq_Suc)
   820 
   821 lemma mono_SucI2: "\<forall>n. X (Suc n) \<le> X n ==> monoseq X"
   822 by (simp add: monoseq_Suc)
   823 
   824 lemma monoseq_minus:
   825   fixes a :: "nat \<Rightarrow> 'a::ordered_ab_group_add"
   826   assumes "monoseq a"
   827   shows "monoseq (\<lambda> n. - a n)"
   828 proof (cases "\<forall> m. \<forall> n \<ge> m. a m \<le> a n")
   829   case True
   830   hence "\<forall> m. \<forall> n \<ge> m. - a n \<le> - a m" by auto
   831   thus ?thesis by (rule monoI2)
   832 next
   833   case False
   834   hence "\<forall> m. \<forall> n \<ge> m. - a m \<le> - a n" using `monoseq a`[unfolded monoseq_def] by auto
   835   thus ?thesis by (rule monoI1)
   836 qed
   837 
   838 text{*Subsequence (alternative definition, (e.g. Hoskins)*}
   839 
   840 lemma subseq_Suc_iff: "subseq f = (\<forall>n. (f n) < (f (Suc n)))"
   841 apply (simp add: subseq_def)
   842 apply (auto dest!: less_imp_Suc_add)
   843 apply (induct_tac k)
   844 apply (auto intro: less_trans)
   845 done
   846 
   847 text{* for any sequence, there is a monotonic subsequence *}
   848 lemma seq_monosub:
   849   fixes s :: "nat => 'a::linorder"
   850   shows "\<exists>f. subseq f \<and> monoseq (\<lambda>n. (s (f n)))"
   851 proof cases
   852   assume "\<forall>n. \<exists>p>n. \<forall>m\<ge>p. s m \<le> s p"
   853   then have "\<exists>f. \<forall>n. (\<forall>m\<ge>f n. s m \<le> s (f n)) \<and> f n < f (Suc n)"
   854     by (intro dependent_nat_choice) (auto simp: conj_commute)
   855   then obtain f where "subseq f" and mono: "\<And>n m. f n \<le> m \<Longrightarrow> s m \<le> s (f n)"
   856     by (auto simp: subseq_Suc_iff)
   857   moreover 
   858   then have "incseq f"
   859     unfolding subseq_Suc_iff incseq_Suc_iff by (auto intro: less_imp_le)
   860   then have "monoseq (\<lambda>n. s (f n))"
   861     by (auto simp add: incseq_def intro!: mono monoI2)
   862   ultimately show ?thesis
   863     by auto
   864 next
   865   assume "\<not> (\<forall>n. \<exists>p>n. (\<forall>m\<ge>p. s m \<le> s p))"
   866   then obtain N where N: "\<And>p. p > N \<Longrightarrow> \<exists>m>p. s p < s m" by (force simp: not_le le_less)
   867   have "\<exists>f. \<forall>n. N < f n \<and> f n < f (Suc n) \<and> s (f n) \<le> s (f (Suc n))"
   868   proof (intro dependent_nat_choice)
   869     fix x assume "N < x" with N[of x] show "\<exists>y>N. x < y \<and> s x \<le> s y"
   870       by (auto intro: less_trans)
   871   qed auto
   872   then show ?thesis
   873     by (auto simp: monoseq_iff incseq_Suc_iff subseq_Suc_iff)
   874 qed
   875 
   876 lemma seq_suble: assumes sf: "subseq f" shows "n \<le> f n"
   877 proof(induct n)
   878   case 0 thus ?case by simp
   879 next
   880   case (Suc n)
   881   from sf[unfolded subseq_Suc_iff, rule_format, of n] Suc.hyps
   882   have "n < f (Suc n)" by arith
   883   thus ?case by arith
   884 qed
   885 
   886 lemma eventually_subseq:
   887   "subseq r \<Longrightarrow> eventually P sequentially \<Longrightarrow> eventually (\<lambda>n. P (r n)) sequentially"
   888   unfolding eventually_sequentially by (metis seq_suble le_trans)
   889 
   890 lemma not_eventually_sequentiallyD:
   891   assumes P: "\<not> eventually P sequentially"
   892   shows "\<exists>r. subseq r \<and> (\<forall>n. \<not> P (r n))"
   893 proof -
   894   from P have "\<forall>n. \<exists>m\<ge>n. \<not> P m"
   895     unfolding eventually_sequentially by (simp add: not_less)
   896   then obtain r where "\<And>n. r n \<ge> n" "\<And>n. \<not> P (r n)"
   897     by (auto simp: choice_iff)
   898   then show ?thesis
   899     by (auto intro!: exI[of _ "\<lambda>n. r (((Suc \<circ> r) ^^ Suc n) 0)"]
   900              simp: less_eq_Suc_le subseq_Suc_iff)
   901 qed
   902 
   903 lemma filterlim_subseq: "subseq f \<Longrightarrow> filterlim f sequentially sequentially"
   904   unfolding filterlim_iff by (metis eventually_subseq)
   905 
   906 lemma subseq_o: "subseq r \<Longrightarrow> subseq s \<Longrightarrow> subseq (r \<circ> s)"
   907   unfolding subseq_def by simp
   908 
   909 lemma subseq_mono: assumes "subseq r" "m < n" shows "r m < r n"
   910   using assms by (auto simp: subseq_def)
   911 
   912 lemma incseq_imp_monoseq:  "incseq X \<Longrightarrow> monoseq X"
   913   by (simp add: incseq_def monoseq_def)
   914 
   915 lemma decseq_imp_monoseq:  "decseq X \<Longrightarrow> monoseq X"
   916   by (simp add: decseq_def monoseq_def)
   917 
   918 lemma decseq_eq_incseq:
   919   fixes X :: "nat \<Rightarrow> 'a::ordered_ab_group_add" shows "decseq X = incseq (\<lambda>n. - X n)" 
   920   by (simp add: decseq_def incseq_def)
   921 
   922 lemma INT_decseq_offset:
   923   assumes "decseq F"
   924   shows "(\<Inter>i. F i) = (\<Inter>i\<in>{n..}. F i)"
   925 proof safe
   926   fix x i assume x: "x \<in> (\<Inter>i\<in>{n..}. F i)"
   927   show "x \<in> F i"
   928   proof cases
   929     from x have "x \<in> F n" by auto
   930     also assume "i \<le> n" with `decseq F` have "F n \<subseteq> F i"
   931       unfolding decseq_def by simp
   932     finally show ?thesis .
   933   qed (insert x, simp)
   934 qed auto
   935 
   936 lemma LIMSEQ_const_iff:
   937   fixes k l :: "'a::t2_space"
   938   shows "(\<lambda>n. k) ----> l \<longleftrightarrow> k = l"
   939   using trivial_limit_sequentially by (rule tendsto_const_iff)
   940 
   941 lemma LIMSEQ_SUP:
   942   "incseq X \<Longrightarrow> X ----> (SUP i. X i :: 'a :: {complete_linorder, linorder_topology})"
   943   by (intro increasing_tendsto)
   944      (auto simp: SUP_upper less_SUP_iff incseq_def eventually_sequentially intro: less_le_trans)
   945 
   946 lemma LIMSEQ_INF:
   947   "decseq X \<Longrightarrow> X ----> (INF i. X i :: 'a :: {complete_linorder, linorder_topology})"
   948   by (intro decreasing_tendsto)
   949      (auto simp: INF_lower INF_less_iff decseq_def eventually_sequentially intro: le_less_trans)
   950 
   951 lemma LIMSEQ_ignore_initial_segment:
   952   "f ----> a \<Longrightarrow> (\<lambda>n. f (n + k)) ----> a"
   953   unfolding tendsto_def
   954   by (subst eventually_sequentially_seg[where k=k])
   955 
   956 lemma LIMSEQ_offset:
   957   "(\<lambda>n. f (n + k)) ----> a \<Longrightarrow> f ----> a"
   958   unfolding tendsto_def
   959   by (subst (asm) eventually_sequentially_seg[where k=k])
   960 
   961 lemma LIMSEQ_Suc: "f ----> l \<Longrightarrow> (\<lambda>n. f (Suc n)) ----> l"
   962 by (drule_tac k="Suc 0" in LIMSEQ_ignore_initial_segment, simp)
   963 
   964 lemma LIMSEQ_imp_Suc: "(\<lambda>n. f (Suc n)) ----> l \<Longrightarrow> f ----> l"
   965 by (rule_tac k="Suc 0" in LIMSEQ_offset, simp)
   966 
   967 lemma LIMSEQ_Suc_iff: "(\<lambda>n. f (Suc n)) ----> l = f ----> l"
   968 by (blast intro: LIMSEQ_imp_Suc LIMSEQ_Suc)
   969 
   970 lemma LIMSEQ_unique:
   971   fixes a b :: "'a::t2_space"
   972   shows "\<lbrakk>X ----> a; X ----> b\<rbrakk> \<Longrightarrow> a = b"
   973   using trivial_limit_sequentially by (rule tendsto_unique)
   974 
   975 lemma LIMSEQ_le_const:
   976   "\<lbrakk>X ----> (x::'a::linorder_topology); \<exists>N. \<forall>n\<ge>N. a \<le> X n\<rbrakk> \<Longrightarrow> a \<le> x"
   977   using tendsto_le_const[of sequentially X x a] by (simp add: eventually_sequentially)
   978 
   979 lemma LIMSEQ_le:
   980   "\<lbrakk>X ----> x; Y ----> y; \<exists>N. \<forall>n\<ge>N. X n \<le> Y n\<rbrakk> \<Longrightarrow> x \<le> (y::'a::linorder_topology)"
   981   using tendsto_le[of sequentially Y y X x] by (simp add: eventually_sequentially)
   982 
   983 lemma LIMSEQ_le_const2:
   984   "\<lbrakk>X ----> (x::'a::linorder_topology); \<exists>N. \<forall>n\<ge>N. X n \<le> a\<rbrakk> \<Longrightarrow> x \<le> a"
   985   by (rule LIMSEQ_le[of X x "\<lambda>n. a"]) auto
   986 
   987 lemma convergentD: "convergent X ==> \<exists>L. (X ----> L)"
   988 by (simp add: convergent_def)
   989 
   990 lemma convergentI: "(X ----> L) ==> convergent X"
   991 by (auto simp add: convergent_def)
   992 
   993 lemma convergent_LIMSEQ_iff: "convergent X = (X ----> lim X)"
   994 by (auto intro: theI LIMSEQ_unique simp add: convergent_def lim_def)
   995 
   996 lemma convergent_const: "convergent (\<lambda>n. c)"
   997   by (rule convergentI, rule tendsto_const)
   998 
   999 lemma monoseq_le:
  1000   "monoseq a \<Longrightarrow> a ----> (x::'a::linorder_topology) \<Longrightarrow>
  1001     ((\<forall> n. a n \<le> x) \<and> (\<forall>m. \<forall>n\<ge>m. a m \<le> a n)) \<or> ((\<forall> n. x \<le> a n) \<and> (\<forall>m. \<forall>n\<ge>m. a n \<le> a m))"
  1002   by (metis LIMSEQ_le_const LIMSEQ_le_const2 decseq_def incseq_def monoseq_iff)
  1003 
  1004 lemma LIMSEQ_subseq_LIMSEQ:
  1005   "\<lbrakk> X ----> L; subseq f \<rbrakk> \<Longrightarrow> (X o f) ----> L"
  1006   unfolding comp_def by (rule filterlim_compose[of X, OF _ filterlim_subseq])
  1007 
  1008 lemma convergent_subseq_convergent:
  1009   "\<lbrakk>convergent X; subseq f\<rbrakk> \<Longrightarrow> convergent (X o f)"
  1010   unfolding convergent_def by (auto intro: LIMSEQ_subseq_LIMSEQ)
  1011 
  1012 lemma limI: "X ----> L ==> lim X = L"
  1013   by (rule tendsto_Lim) (rule trivial_limit_sequentially)
  1014 
  1015 lemma lim_le: "convergent f \<Longrightarrow> (\<And>n. f n \<le> (x::'a::linorder_topology)) \<Longrightarrow> lim f \<le> x"
  1016   using LIMSEQ_le_const2[of f "lim f" x] by (simp add: convergent_LIMSEQ_iff)
  1017 
  1018 subsubsection{*Increasing and Decreasing Series*}
  1019 
  1020 lemma incseq_le: "incseq X \<Longrightarrow> X ----> L \<Longrightarrow> X n \<le> (L::'a::linorder_topology)"
  1021   by (metis incseq_def LIMSEQ_le_const)
  1022 
  1023 lemma decseq_le: "decseq X \<Longrightarrow> X ----> L \<Longrightarrow> (L::'a::linorder_topology) \<le> X n"
  1024   by (metis decseq_def LIMSEQ_le_const2)
  1025 
  1026 subsection {* First countable topologies *}
  1027 
  1028 class first_countable_topology = topological_space +
  1029   assumes first_countable_basis:
  1030     "\<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))"
  1031 
  1032 lemma (in first_countable_topology) countable_basis_at_decseq:
  1033   obtains A :: "nat \<Rightarrow> 'a set" where
  1034     "\<And>i. open (A i)" "\<And>i. x \<in> (A i)"
  1035     "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
  1036 proof atomize_elim
  1037   from first_countable_basis[of x] obtain A :: "nat \<Rightarrow> 'a set" where
  1038     nhds: "\<And>i. open (A i)" "\<And>i. x \<in> A i"
  1039     and incl: "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>i. A i \<subseteq> S"  by auto
  1040   def F \<equiv> "\<lambda>n. \<Inter>i\<le>n. A i"
  1041   show "\<exists>A. (\<forall>i. open (A i)) \<and> (\<forall>i. x \<in> A i) \<and>
  1042       (\<forall>S. open S \<longrightarrow> x \<in> S \<longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially)"
  1043   proof (safe intro!: exI[of _ F])
  1044     fix i
  1045     show "open (F i)" using nhds(1) by (auto simp: F_def)
  1046     show "x \<in> F i" using nhds(2) by (auto simp: F_def)
  1047   next
  1048     fix S assume "open S" "x \<in> S"
  1049     from incl[OF this] obtain i where "F i \<subseteq> S" unfolding F_def by auto
  1050     moreover have "\<And>j. i \<le> j \<Longrightarrow> F j \<subseteq> F i"
  1051       by (auto simp: F_def)
  1052     ultimately show "eventually (\<lambda>i. F i \<subseteq> S) sequentially"
  1053       by (auto simp: eventually_sequentially)
  1054   qed
  1055 qed
  1056 
  1057 lemma (in first_countable_topology) nhds_countable:
  1058   obtains X :: "nat \<Rightarrow> 'a set"
  1059   where "decseq X" "\<And>n. open (X n)" "\<And>n. x \<in> X n" "nhds x = (INF n. principal (X n))"
  1060 proof -
  1061   from first_countable_basis obtain A :: "nat \<Rightarrow> 'a set"
  1062     where A: "\<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"
  1063     by metis
  1064   show thesis
  1065   proof
  1066     show "decseq (\<lambda>n. \<Inter>i\<le>n. A i)"
  1067       by (auto simp: decseq_def)
  1068     show "\<And>n. x \<in> (\<Inter>i\<le>n. A i)" "\<And>n. open (\<Inter>i\<le>n. A i)"
  1069       using A by auto
  1070     show "nhds x = (INF n. principal (\<Inter>i\<le>n. A i))"
  1071       using A unfolding nhds_def
  1072       apply (intro INF_eq)
  1073       apply simp_all
  1074       apply force
  1075       apply (intro exI[of _ "\<Inter>i\<le>n. A i" for n] conjI open_INT)
  1076       apply auto
  1077       done
  1078   qed
  1079 qed
  1080 
  1081 lemma (in first_countable_topology) countable_basis:
  1082   obtains A :: "nat \<Rightarrow> 'a set" where
  1083     "\<And>i. open (A i)" "\<And>i. x \<in> A i"
  1084     "\<And>F. (\<forall>n. F n \<in> A n) \<Longrightarrow> F ----> x"
  1085 proof atomize_elim
  1086   obtain A :: "nat \<Rightarrow> 'a set" where A:
  1087     "\<And>i. open (A i)"
  1088     "\<And>i. x \<in> A i"
  1089     "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
  1090     by (rule countable_basis_at_decseq) blast
  1091   {
  1092     fix F S assume "\<forall>n. F n \<in> A n" "open S" "x \<in> S"
  1093     with A(3)[of S] have "eventually (\<lambda>n. F n \<in> S) sequentially"
  1094       by (auto elim: eventually_elim1 simp: subset_eq)
  1095   }
  1096   with A 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 ----> x)"
  1097     by (intro exI[of _ A]) (auto simp: tendsto_def)
  1098 qed
  1099 
  1100 lemma (in first_countable_topology) sequentially_imp_eventually_nhds_within:
  1101   assumes "\<forall>f. (\<forall>n. f n \<in> s) \<and> f ----> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially"
  1102   shows "eventually P (inf (nhds a) (principal s))"
  1103 proof (rule ccontr)
  1104   obtain A :: "nat \<Rightarrow> 'a set" where A:
  1105     "\<And>i. open (A i)"
  1106     "\<And>i. a \<in> A i"
  1107     "\<And>F. \<forall>n. F n \<in> A n \<Longrightarrow> F ----> a"
  1108     by (rule countable_basis) blast
  1109   assume "\<not> ?thesis"
  1110   with A have P: "\<exists>F. \<forall>n. F n \<in> s \<and> F n \<in> A n \<and> \<not> P (F n)"
  1111     unfolding eventually_inf_principal eventually_nhds by (intro choice) fastforce
  1112   then obtain F where F0: "\<forall>n. F n \<in> s" and F2: "\<forall>n. F n \<in> A n" and F3: "\<forall>n. \<not> P (F n)"
  1113     by blast
  1114   with A have "F ----> a" by auto
  1115   hence "eventually (\<lambda>n. P (F n)) sequentially"
  1116     using assms F0 by simp
  1117   thus "False" by (simp add: F3)
  1118 qed
  1119 
  1120 lemma (in first_countable_topology) eventually_nhds_within_iff_sequentially:
  1121   "eventually P (inf (nhds a) (principal s)) \<longleftrightarrow> 
  1122     (\<forall>f. (\<forall>n. f n \<in> s) \<and> f ----> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially)"
  1123 proof (safe intro!: sequentially_imp_eventually_nhds_within)
  1124   assume "eventually P (inf (nhds a) (principal s))" 
  1125   then obtain S where "open S" "a \<in> S" "\<forall>x\<in>S. x \<in> s \<longrightarrow> P x"
  1126     by (auto simp: eventually_inf_principal eventually_nhds)
  1127   moreover fix f assume "\<forall>n. f n \<in> s" "f ----> a"
  1128   ultimately show "eventually (\<lambda>n. P (f n)) sequentially"
  1129     by (auto dest!: topological_tendstoD elim: eventually_elim1)
  1130 qed
  1131 
  1132 lemma (in first_countable_topology) eventually_nhds_iff_sequentially:
  1133   "eventually P (nhds a) \<longleftrightarrow> (\<forall>f. f ----> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially)"
  1134   using eventually_nhds_within_iff_sequentially[of P a UNIV] by simp
  1135 
  1136 lemma tendsto_at_iff_sequentially:
  1137   fixes f :: "'a :: first_countable_topology \<Rightarrow> _"
  1138   shows "(f ---> a) (at x within s) \<longleftrightarrow> (\<forall>X. (\<forall>i. X i \<in> s - {x}) \<longrightarrow> X ----> x \<longrightarrow> ((f \<circ> X) ----> a))"
  1139   unfolding filterlim_def[of _ "nhds a"] le_filter_def eventually_filtermap at_within_def eventually_nhds_within_iff_sequentially comp_def
  1140   by metis
  1141 
  1142 subsection {* Function limit at a point *}
  1143 
  1144 abbreviation
  1145   LIM :: "('a::topological_space \<Rightarrow> 'b::topological_space) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool"
  1146         ("((_)/ -- (_)/ --> (_))" [60, 0, 60] 60) where
  1147   "f -- a --> L \<equiv> (f ---> L) (at a)"
  1148 
  1149 lemma tendsto_within_open: "a \<in> S \<Longrightarrow> open S \<Longrightarrow> (f ---> l) (at a within S) \<longleftrightarrow> (f -- a --> l)"
  1150   unfolding tendsto_def by (simp add: at_within_open[where S=S])
  1151 
  1152 lemma LIM_const_not_eq[tendsto_intros]:
  1153   fixes a :: "'a::perfect_space"
  1154   fixes k L :: "'b::t2_space"
  1155   shows "k \<noteq> L \<Longrightarrow> \<not> (\<lambda>x. k) -- a --> L"
  1156   by (simp add: tendsto_const_iff)
  1157 
  1158 lemmas LIM_not_zero = LIM_const_not_eq [where L = 0]
  1159 
  1160 lemma LIM_const_eq:
  1161   fixes a :: "'a::perfect_space"
  1162   fixes k L :: "'b::t2_space"
  1163   shows "(\<lambda>x. k) -- a --> L \<Longrightarrow> k = L"
  1164   by (simp add: tendsto_const_iff)
  1165 
  1166 lemma LIM_unique:
  1167   fixes a :: "'a::perfect_space" and L M :: "'b::t2_space"
  1168   shows "f -- a --> L \<Longrightarrow> f -- a --> M \<Longrightarrow> L = M"
  1169   using at_neq_bot by (rule tendsto_unique)
  1170 
  1171 text {* Limits are equal for functions equal except at limit point *}
  1172 
  1173 lemma LIM_equal: "\<forall>x. x \<noteq> a --> (f x = g x) \<Longrightarrow> (f -- a --> l) \<longleftrightarrow> (g -- a --> l)"
  1174   unfolding tendsto_def eventually_at_topological by simp
  1175 
  1176 lemma LIM_cong: "a = b \<Longrightarrow> (\<And>x. x \<noteq> b \<Longrightarrow> f x = g x) \<Longrightarrow> l = m \<Longrightarrow> (f -- a --> l) \<longleftrightarrow> (g -- b --> m)"
  1177   by (simp add: LIM_equal)
  1178 
  1179 lemma LIM_cong_limit: "f -- x --> L \<Longrightarrow> K = L \<Longrightarrow> f -- x --> K"
  1180   by simp
  1181 
  1182 lemma tendsto_at_iff_tendsto_nhds:
  1183   "g -- l --> g l \<longleftrightarrow> (g ---> g l) (nhds l)"
  1184   unfolding tendsto_def eventually_at_filter
  1185   by (intro ext all_cong imp_cong) (auto elim!: eventually_elim1)
  1186 
  1187 lemma tendsto_compose:
  1188   "g -- l --> g l \<Longrightarrow> (f ---> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) ---> g l) F"
  1189   unfolding tendsto_at_iff_tendsto_nhds by (rule filterlim_compose[of g])
  1190 
  1191 lemma LIM_o: "\<lbrakk>g -- l --> g l; f -- a --> l\<rbrakk> \<Longrightarrow> (g \<circ> f) -- a --> g l"
  1192   unfolding o_def by (rule tendsto_compose)
  1193 
  1194 lemma tendsto_compose_eventually:
  1195   "g -- l --> m \<Longrightarrow> (f ---> l) F \<Longrightarrow> eventually (\<lambda>x. f x \<noteq> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) ---> m) F"
  1196   by (rule filterlim_compose[of g _ "at l"]) (auto simp add: filterlim_at)
  1197 
  1198 lemma LIM_compose_eventually:
  1199   assumes f: "f -- a --> b"
  1200   assumes g: "g -- b --> c"
  1201   assumes inj: "eventually (\<lambda>x. f x \<noteq> b) (at a)"
  1202   shows "(\<lambda>x. g (f x)) -- a --> c"
  1203   using g f inj by (rule tendsto_compose_eventually)
  1204 
  1205 lemma tendsto_compose_filtermap: "((g \<circ> f) ---> T) F \<longleftrightarrow> (g ---> T) (filtermap f F)"
  1206   by (simp add: filterlim_def filtermap_filtermap comp_def)
  1207 
  1208 subsubsection {* Relation of LIM and LIMSEQ *}
  1209 
  1210 lemma (in first_countable_topology) sequentially_imp_eventually_within:
  1211   "(\<forall>f. (\<forall>n. f n \<in> s \<and> f n \<noteq> a) \<and> f ----> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially) \<Longrightarrow>
  1212     eventually P (at a within s)"
  1213   unfolding at_within_def
  1214   by (intro sequentially_imp_eventually_nhds_within) auto
  1215 
  1216 lemma (in first_countable_topology) sequentially_imp_eventually_at:
  1217   "(\<forall>f. (\<forall>n. f n \<noteq> a) \<and> f ----> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially) \<Longrightarrow> eventually P (at a)"
  1218   using assms sequentially_imp_eventually_within [where s=UNIV] by simp
  1219 
  1220 lemma LIMSEQ_SEQ_conv1:
  1221   fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
  1222   assumes f: "f -- a --> l"
  1223   shows "\<forall>S. (\<forall>n. S n \<noteq> a) \<and> S ----> a \<longrightarrow> (\<lambda>n. f (S n)) ----> l"
  1224   using tendsto_compose_eventually [OF f, where F=sequentially] by simp
  1225 
  1226 lemma LIMSEQ_SEQ_conv2:
  1227   fixes f :: "'a::first_countable_topology \<Rightarrow> 'b::topological_space"
  1228   assumes "\<forall>S. (\<forall>n. S n \<noteq> a) \<and> S ----> a \<longrightarrow> (\<lambda>n. f (S n)) ----> l"
  1229   shows "f -- a --> l"
  1230   using assms unfolding tendsto_def [where l=l] by (simp add: sequentially_imp_eventually_at)
  1231 
  1232 lemma LIMSEQ_SEQ_conv:
  1233   "(\<forall>S. (\<forall>n. S n \<noteq> a) \<and> S ----> (a::'a::first_countable_topology) \<longrightarrow> (\<lambda>n. X (S n)) ----> L) =
  1234    (X -- a --> (L::'b::topological_space))"
  1235   using LIMSEQ_SEQ_conv2 LIMSEQ_SEQ_conv1 ..
  1236 
  1237 lemma sequentially_imp_eventually_at_left:
  1238   fixes a :: "'a :: {linorder_topology, first_countable_topology}"
  1239   assumes b[simp]: "b < a"
  1240   assumes *: "\<And>f. (\<And>n. b < f n) \<Longrightarrow> (\<And>n. f n < a) \<Longrightarrow> incseq f \<Longrightarrow> f ----> a \<Longrightarrow> eventually (\<lambda>n. P (f n)) sequentially"
  1241   shows "eventually P (at_left a)"
  1242 proof (safe intro!: sequentially_imp_eventually_within)
  1243   fix X assume X: "\<forall>n. X n \<in> {..< a} \<and> X n \<noteq> a" "X ----> a"
  1244   show "eventually (\<lambda>n. P (X n)) sequentially"
  1245   proof (rule ccontr)
  1246     assume neg: "\<not> eventually (\<lambda>n. P (X n)) sequentially"
  1247     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))"
  1248     proof (rule dependent_nat_choice)
  1249       have "\<not> eventually (\<lambda>n. b < X n \<longrightarrow> P (X n)) sequentially"
  1250         by (intro not_eventually_impI neg order_tendstoD(1) [OF X(2) b])
  1251       then show "\<exists>x. \<not> P (X x) \<and> b < X x"
  1252         by (auto dest!: not_eventuallyD)
  1253     next
  1254       fix x n
  1255       have "\<not> eventually (\<lambda>n. Suc x \<le> n \<longrightarrow> b < X n \<longrightarrow> X x < X n \<longrightarrow> P (X n)) sequentially"
  1256         using X by (intro not_eventually_impI order_tendstoD(1)[OF X(2)] eventually_ge_at_top neg) auto
  1257       then show "\<exists>n. (\<not> P (X n) \<and> b < X n) \<and> (X x \<le> X n \<and> Suc x \<le> n)"
  1258         by (auto dest!: not_eventuallyD)
  1259     qed
  1260     then guess s ..
  1261     then have "\<And>n. b < X (s n)" "\<And>n. X (s n) < a" "incseq (\<lambda>n. X (s n))" "(\<lambda>n. X (s n)) ----> a" "\<And>n. \<not> P (X (s n))"
  1262       using X by (auto simp: subseq_Suc_iff Suc_le_eq incseq_Suc_iff intro!: LIMSEQ_subseq_LIMSEQ[OF `X ----> a`, unfolded comp_def])
  1263     from *[OF this(1,2,3,4)] this(5) show False by auto
  1264   qed
  1265 qed
  1266 
  1267 lemma tendsto_at_left_sequentially:
  1268   fixes a :: "_ :: {linorder_topology, first_countable_topology}"
  1269   assumes "b < a"
  1270   assumes *: "\<And>S. (\<And>n. S n < a) \<Longrightarrow> (\<And>n. b < S n) \<Longrightarrow> incseq S \<Longrightarrow> S ----> a \<Longrightarrow> (\<lambda>n. X (S n)) ----> L"
  1271   shows "(X ---> L) (at_left a)"
  1272   using assms unfolding tendsto_def [where l=L]
  1273   by (simp add: sequentially_imp_eventually_at_left)
  1274 
  1275 lemma sequentially_imp_eventually_at_right:
  1276   fixes a :: "'a :: {linorder_topology, first_countable_topology}"
  1277   assumes b[simp]: "a < b"
  1278   assumes *: "\<And>f. (\<And>n. a < f n) \<Longrightarrow> (\<And>n. f n < b) \<Longrightarrow> decseq f \<Longrightarrow> f ----> a \<Longrightarrow> eventually (\<lambda>n. P (f n)) sequentially"
  1279   shows "eventually P (at_right a)"
  1280 proof (safe intro!: sequentially_imp_eventually_within)
  1281   fix X assume X: "\<forall>n. X n \<in> {a <..} \<and> X n \<noteq> a" "X ----> a"
  1282   show "eventually (\<lambda>n. P (X n)) sequentially"
  1283   proof (rule ccontr)
  1284     assume neg: "\<not> eventually (\<lambda>n. P (X n)) sequentially"
  1285     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))"
  1286     proof (rule dependent_nat_choice)
  1287       have "\<not> eventually (\<lambda>n. X n < b \<longrightarrow> P (X n)) sequentially"
  1288         by (intro not_eventually_impI neg order_tendstoD(2) [OF X(2) b])
  1289       then show "\<exists>x. \<not> P (X x) \<and> X x < b"
  1290         by (auto dest!: not_eventuallyD)
  1291     next
  1292       fix x n
  1293       have "\<not> eventually (\<lambda>n. Suc x \<le> n \<longrightarrow> X n < b \<longrightarrow> X n < X x \<longrightarrow> P (X n)) sequentially"
  1294         using X by (intro not_eventually_impI order_tendstoD(2)[OF X(2)] eventually_ge_at_top neg) auto
  1295       then show "\<exists>n. (\<not> P (X n) \<and> X n < b) \<and> (X n \<le> X x \<and> Suc x \<le> n)"
  1296         by (auto dest!: not_eventuallyD)
  1297     qed
  1298     then guess s ..
  1299     then have "\<And>n. a < X (s n)" "\<And>n. X (s n) < b" "decseq (\<lambda>n. X (s n))" "(\<lambda>n. X (s n)) ----> a" "\<And>n. \<not> P (X (s n))"
  1300       using X by (auto simp: subseq_Suc_iff Suc_le_eq decseq_Suc_iff intro!: LIMSEQ_subseq_LIMSEQ[OF `X ----> a`, unfolded comp_def])
  1301     from *[OF this(1,2,3,4)] this(5) show False by auto
  1302   qed
  1303 qed
  1304 
  1305 lemma tendsto_at_right_sequentially:
  1306   fixes a :: "_ :: {linorder_topology, first_countable_topology}"
  1307   assumes "a < b"
  1308   assumes *: "\<And>S. (\<And>n. a < S n) \<Longrightarrow> (\<And>n. S n < b) \<Longrightarrow> decseq S \<Longrightarrow> S ----> a \<Longrightarrow> (\<lambda>n. X (S n)) ----> L"
  1309   shows "(X ---> L) (at_right a)"
  1310   using assms unfolding tendsto_def [where l=L]
  1311   by (simp add: sequentially_imp_eventually_at_right)
  1312 
  1313 subsection {* Continuity *}
  1314 
  1315 subsubsection {* Continuity on a set *}
  1316 
  1317 definition continuous_on :: "'a set \<Rightarrow> ('a :: topological_space \<Rightarrow> 'b :: topological_space) \<Rightarrow> bool" where
  1318   "continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. (f ---> f x) (at x within s))"
  1319 
  1320 lemma continuous_on_cong [cong]:
  1321   "s = t \<Longrightarrow> (\<And>x. x \<in> t \<Longrightarrow> f x = g x) \<Longrightarrow> continuous_on s f \<longleftrightarrow> continuous_on t g"
  1322   unfolding continuous_on_def by (intro ball_cong filterlim_cong) (auto simp: eventually_at_filter)
  1323 
  1324 lemma continuous_on_topological:
  1325   "continuous_on s f \<longleftrightarrow>
  1326     (\<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)))"
  1327   unfolding continuous_on_def tendsto_def eventually_at_topological by metis
  1328 
  1329 lemma continuous_on_open_invariant:
  1330   "continuous_on s f \<longleftrightarrow> (\<forall>B. open B \<longrightarrow> (\<exists>A. open A \<and> A \<inter> s = f -` B \<inter> s))"
  1331 proof safe
  1332   fix B :: "'b set" assume "continuous_on s f" "open B"
  1333   then have "\<forall>x\<in>f -` B \<inter> s. (\<exists>A. open A \<and> x \<in> A \<and> s \<inter> A \<subseteq> f -` B)"
  1334     by (auto simp: continuous_on_topological subset_eq Ball_def imp_conjL)
  1335   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"
  1336     unfolding bchoice_iff ..
  1337   then show "\<exists>A. open A \<and> A \<inter> s = f -` B \<inter> s"
  1338     by (intro exI[of _ "\<Union>x\<in>f -` B \<inter> s. A x"]) auto
  1339 next
  1340   assume B: "\<forall>B. open B \<longrightarrow> (\<exists>A. open A \<and> A \<inter> s = f -` B \<inter> s)"
  1341   show "continuous_on s f"
  1342     unfolding continuous_on_topological
  1343   proof safe
  1344     fix x B assume "x \<in> s" "open B" "f x \<in> B"
  1345     with B obtain A where A: "open A" "A \<inter> s = f -` B \<inter> s" by auto
  1346     with `x \<in> s` `f x \<in> B` show "\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)"
  1347       by (intro exI[of _ A]) auto
  1348   qed
  1349 qed
  1350 
  1351 lemma continuous_on_open_vimage:
  1352   "open s \<Longrightarrow> continuous_on s f \<longleftrightarrow> (\<forall>B. open B \<longrightarrow> open (f -` B \<inter> s))"
  1353   unfolding continuous_on_open_invariant
  1354   by (metis open_Int Int_absorb Int_commute[of s] Int_assoc[of _ _ s])
  1355 
  1356 corollary continuous_imp_open_vimage:
  1357   assumes "continuous_on s f" "open s" "open B" "f -` B \<subseteq> s"
  1358     shows "open (f -` B)"
  1359 by (metis assms continuous_on_open_vimage le_iff_inf)
  1360 
  1361 corollary open_vimage[continuous_intros]:
  1362   assumes "open s" and "continuous_on UNIV f"
  1363   shows "open (f -` s)"
  1364   using assms unfolding continuous_on_open_vimage [OF open_UNIV]
  1365   by simp
  1366 
  1367 lemma continuous_on_closed_invariant:
  1368   "continuous_on s f \<longleftrightarrow> (\<forall>B. closed B \<longrightarrow> (\<exists>A. closed A \<and> A \<inter> s = f -` B \<inter> s))"
  1369 proof -
  1370   have *: "\<And>P Q::'b set\<Rightarrow>bool. (\<And>A. P A \<longleftrightarrow> Q (- A)) \<Longrightarrow> (\<forall>A. P A) \<longleftrightarrow> (\<forall>A. Q A)"
  1371     by (metis double_compl)
  1372   show ?thesis
  1373     unfolding continuous_on_open_invariant by (intro *) (auto simp: open_closed[symmetric])
  1374 qed
  1375 
  1376 lemma continuous_on_closed_vimage:
  1377   "closed s \<Longrightarrow> continuous_on s f \<longleftrightarrow> (\<forall>B. closed B \<longrightarrow> closed (f -` B \<inter> s))"
  1378   unfolding continuous_on_closed_invariant
  1379   by (metis closed_Int Int_absorb Int_commute[of s] Int_assoc[of _ _ s])
  1380 
  1381 corollary closed_vimage[continuous_intros]:
  1382   assumes "closed s" and "continuous_on UNIV f"
  1383   shows "closed (f -` s)"
  1384   using assms unfolding continuous_on_closed_vimage [OF closed_UNIV]
  1385   by simp
  1386 
  1387 lemma continuous_on_open_Union:
  1388   "(\<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"
  1389   unfolding continuous_on_def by safe (metis open_Union at_within_open UnionI)
  1390 
  1391 lemma continuous_on_open_UN:
  1392   "(\<And>s. s \<in> S \<Longrightarrow> open (A s)) \<Longrightarrow> (\<And>s. s \<in> S \<Longrightarrow> continuous_on (A s) f) \<Longrightarrow> continuous_on (\<Union>s\<in>S. A s) f"
  1393   unfolding Union_image_eq[symmetric] by (rule continuous_on_open_Union) auto
  1394 
  1395 lemma continuous_on_closed_Un:
  1396   "closed s \<Longrightarrow> closed t \<Longrightarrow> continuous_on s f \<Longrightarrow> continuous_on t f \<Longrightarrow> continuous_on (s \<union> t) f"
  1397   by (auto simp add: continuous_on_closed_vimage closed_Un Int_Un_distrib)
  1398 
  1399 lemma continuous_on_If:
  1400   assumes closed: "closed s" "closed t" and cont: "continuous_on s f" "continuous_on t g"
  1401     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"
  1402   shows "continuous_on (s \<union> t) (\<lambda>x. if P x then f x else g x)" (is "continuous_on _ ?h")
  1403 proof-
  1404   from P have "\<forall>x\<in>s. f x = ?h x" "\<forall>x\<in>t. g x = ?h x"
  1405     by auto
  1406   with cont have "continuous_on s ?h" "continuous_on t ?h"
  1407     by simp_all
  1408   with closed show ?thesis
  1409     by (rule continuous_on_closed_Un)
  1410 qed
  1411 
  1412 lemma continuous_on_id[continuous_intros]: "continuous_on s (\<lambda>x. x)"
  1413   unfolding continuous_on_def by fast
  1414 
  1415 lemma continuous_on_const[continuous_intros]: "continuous_on s (\<lambda>x. c)"
  1416   unfolding continuous_on_def by auto
  1417 
  1418 lemma continuous_on_compose[continuous_intros]:
  1419   "continuous_on s f \<Longrightarrow> continuous_on (f ` s) g \<Longrightarrow> continuous_on s (g o f)"
  1420   unfolding continuous_on_topological by simp metis
  1421 
  1422 lemma continuous_on_compose2:
  1423   "continuous_on t g \<Longrightarrow> continuous_on s f \<Longrightarrow> t = f ` s \<Longrightarrow> continuous_on s (\<lambda>x. g (f x))"
  1424   using continuous_on_compose[of s f g] by (simp add: comp_def)
  1425 
  1426 subsubsection {* Continuity at a point *}
  1427 
  1428 definition continuous :: "'a::t2_space filter \<Rightarrow> ('a \<Rightarrow> 'b::topological_space) \<Rightarrow> bool" where
  1429   "continuous F f \<longleftrightarrow> (f ---> f (Lim F (\<lambda>x. x))) F"
  1430 
  1431 lemma continuous_bot[continuous_intros, simp]: "continuous bot f"
  1432   unfolding continuous_def by auto
  1433 
  1434 lemma continuous_trivial_limit: "trivial_limit net \<Longrightarrow> continuous net f"
  1435   by simp
  1436 
  1437 lemma continuous_within: "continuous (at x within s) f \<longleftrightarrow> (f ---> f x) (at x within s)"
  1438   by (cases "trivial_limit (at x within s)") (auto simp add: Lim_ident_at continuous_def)
  1439 
  1440 lemma continuous_within_topological:
  1441   "continuous (at x within s) f \<longleftrightarrow>
  1442     (\<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)))"
  1443   unfolding continuous_within tendsto_def eventually_at_topological by metis
  1444 
  1445 lemma continuous_within_compose[continuous_intros]:
  1446   "continuous (at x within s) f \<Longrightarrow> continuous (at (f x) within f ` s) g \<Longrightarrow>
  1447   continuous (at x within s) (g o f)"
  1448   by (simp add: continuous_within_topological) metis
  1449 
  1450 lemma continuous_within_compose2:
  1451   "continuous (at x within s) f \<Longrightarrow> continuous (at (f x) within f ` s) g \<Longrightarrow>
  1452   continuous (at x within s) (\<lambda>x. g (f x))"
  1453   using continuous_within_compose[of x s f g] by (simp add: comp_def)
  1454 
  1455 lemma continuous_at: "continuous (at x) f \<longleftrightarrow> f -- x --> f x"
  1456   using continuous_within[of x UNIV f] by simp
  1457 
  1458 lemma continuous_ident[continuous_intros, simp]: "continuous (at x within S) (\<lambda>x. x)"
  1459   unfolding continuous_within by (rule tendsto_ident_at)
  1460 
  1461 lemma continuous_const[continuous_intros, simp]: "continuous F (\<lambda>x. c)"
  1462   unfolding continuous_def by (rule tendsto_const)
  1463 
  1464 lemma continuous_on_eq_continuous_within:
  1465   "continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. continuous (at x within s) f)"
  1466   unfolding continuous_on_def continuous_within ..
  1467 
  1468 abbreviation isCont :: "('a::t2_space \<Rightarrow> 'b::topological_space) \<Rightarrow> 'a \<Rightarrow> bool" where
  1469   "isCont f a \<equiv> continuous (at a) f"
  1470 
  1471 lemma isCont_def: "isCont f a \<longleftrightarrow> f -- a --> f a"
  1472   by (rule continuous_at)
  1473 
  1474 lemma continuous_at_within: "isCont f x \<Longrightarrow> continuous (at x within s) f"
  1475   by (auto intro: tendsto_mono at_le simp: continuous_at continuous_within)
  1476 
  1477 lemma continuous_on_eq_continuous_at: "open s \<Longrightarrow> continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. isCont f x)"
  1478   by (simp add: continuous_on_def continuous_at at_within_open[of _ s])
  1479 
  1480 lemma continuous_on_subset: "continuous_on s f \<Longrightarrow> t \<subseteq> s \<Longrightarrow> continuous_on t f"
  1481   unfolding continuous_on_def by (metis subset_eq tendsto_within_subset)
  1482 
  1483 lemma continuous_at_imp_continuous_on: "\<forall>x\<in>s. isCont f x \<Longrightarrow> continuous_on s f"
  1484   by (auto intro: continuous_at_within simp: continuous_on_eq_continuous_within)
  1485 
  1486 lemma isCont_o2: "isCont f a \<Longrightarrow> isCont g (f a) \<Longrightarrow> isCont (\<lambda>x. g (f x)) a"
  1487   unfolding isCont_def by (rule tendsto_compose)
  1488 
  1489 lemma isCont_o[continuous_intros]: "isCont f a \<Longrightarrow> isCont g (f a) \<Longrightarrow> isCont (g \<circ> f) a"
  1490   unfolding o_def by (rule isCont_o2)
  1491 
  1492 lemma isCont_tendsto_compose: "isCont g l \<Longrightarrow> (f ---> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) ---> g l) F"
  1493   unfolding isCont_def by (rule tendsto_compose)
  1494 
  1495 lemma continuous_within_compose3:
  1496   "isCont g (f x) \<Longrightarrow> continuous (at x within s) f \<Longrightarrow> continuous (at x within s) (\<lambda>x. g (f x))"
  1497   using continuous_within_compose2[of x s f g] by (simp add: continuous_at_within)
  1498 
  1499 lemma filtermap_nhds_open_map:
  1500   assumes cont: "isCont f a" and open_map: "\<And>S. open S \<Longrightarrow> open (f`S)"
  1501   shows "filtermap f (nhds a) = nhds (f a)"
  1502   unfolding filter_eq_iff
  1503 proof safe
  1504   fix P assume "eventually P (filtermap f (nhds a))"
  1505   then guess S unfolding eventually_filtermap eventually_nhds ..
  1506   then show "eventually P (nhds (f a))"
  1507     unfolding eventually_nhds by (intro exI[of _ "f`S"]) (auto intro!: open_map)
  1508 qed (metis filterlim_iff tendsto_at_iff_tendsto_nhds isCont_def eventually_filtermap cont)
  1509 
  1510 lemma continuous_at_split: 
  1511   "continuous (at (x::'a::linorder_topology)) f = (continuous (at_left x) f \<and> continuous (at_right x) f)"
  1512   by (simp add: continuous_within filterlim_at_split)
  1513 
  1514 subsubsection{* Open-cover compactness *}
  1515 
  1516 context topological_space
  1517 begin
  1518 
  1519 definition compact :: "'a set \<Rightarrow> bool" where
  1520   compact_eq_heine_borel: -- "This name is used for backwards compatibility"
  1521     "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))"
  1522 
  1523 lemma compactI:
  1524   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'"
  1525   shows "compact s"
  1526   unfolding compact_eq_heine_borel using assms by metis
  1527 
  1528 lemma compact_empty[simp]: "compact {}"
  1529   by (auto intro!: compactI)
  1530 
  1531 lemma compactE:
  1532   assumes "compact s" and "\<forall>t\<in>C. open t" and "s \<subseteq> \<Union>C"
  1533   obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> \<Union>C'"
  1534   using assms unfolding compact_eq_heine_borel by metis
  1535 
  1536 lemma compactE_image:
  1537   assumes "compact s" and "\<forall>t\<in>C. open (f t)" and "s \<subseteq> (\<Union>c\<in>C. f c)"
  1538   obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> (\<Union>c\<in>C'. f c)"
  1539   using assms unfolding ball_simps[symmetric] SUP_def
  1540   by (metis (lifting) finite_subset_image compact_eq_heine_borel[of s])
  1541 
  1542 lemma compact_inter_closed [intro]:
  1543   assumes "compact s" and "closed t"
  1544   shows "compact (s \<inter> t)"
  1545 proof (rule compactI)
  1546   fix C assume C: "\<forall>c\<in>C. open c" and cover: "s \<inter> t \<subseteq> \<Union>C"
  1547   from C `closed t` have "\<forall>c\<in>C \<union> {-t}. open c" by auto
  1548   moreover from cover have "s \<subseteq> \<Union>(C \<union> {-t})" by auto
  1549   ultimately have "\<exists>D\<subseteq>C \<union> {-t}. finite D \<and> s \<subseteq> \<Union>D"
  1550     using `compact s` unfolding compact_eq_heine_borel by auto
  1551   then obtain D where "D \<subseteq> C \<union> {- t} \<and> finite D \<and> s \<subseteq> \<Union>D" ..
  1552   then show "\<exists>D\<subseteq>C. finite D \<and> s \<inter> t \<subseteq> \<Union>D"
  1553     by (intro exI[of _ "D - {-t}"]) auto
  1554 qed
  1555 
  1556 lemma inj_setminus: "inj_on uminus (A::'a set set)"
  1557   by (auto simp: inj_on_def)
  1558 
  1559 lemma compact_fip:
  1560   "compact U \<longleftrightarrow>
  1561     (\<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> {})"
  1562   (is "_ \<longleftrightarrow> ?R")
  1563 proof (safe intro!: compact_eq_heine_borel[THEN iffD2])
  1564   fix A
  1565   assume "compact U"
  1566     and A: "\<forall>a\<in>A. closed a" "U \<inter> \<Inter>A = {}"
  1567     and fi: "\<forall>B \<subseteq> A. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {}"
  1568   from A have "(\<forall>a\<in>uminus`A. open a) \<and> U \<subseteq> \<Union>(uminus`A)"
  1569     by auto
  1570   with `compact U` obtain B where "B \<subseteq> A" "finite (uminus`B)" "U \<subseteq> \<Union>(uminus`B)"
  1571     unfolding compact_eq_heine_borel by (metis subset_image_iff)
  1572   with fi[THEN spec, of B] show False
  1573     by (auto dest: finite_imageD intro: inj_setminus)
  1574 next
  1575   fix A
  1576   assume ?R
  1577   assume "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A"
  1578   then have "U \<inter> \<Inter>(uminus`A) = {}" "\<forall>a\<in>uminus`A. closed a"
  1579     by auto
  1580   with `?R` obtain B where "B \<subseteq> A" "finite (uminus`B)" "U \<inter> \<Inter>(uminus`B) = {}"
  1581     by (metis subset_image_iff)
  1582   then show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"
  1583     by  (auto intro!: exI[of _ B] inj_setminus dest: finite_imageD)
  1584 qed
  1585 
  1586 lemma compact_imp_fip:
  1587   "compact s \<Longrightarrow> \<forall>t \<in> f. closed t \<Longrightarrow> \<forall>f'. finite f' \<and> f' \<subseteq> f \<longrightarrow> (s \<inter> (\<Inter>f') \<noteq> {}) \<Longrightarrow>
  1588     s \<inter> (\<Inter>f) \<noteq> {}"
  1589   unfolding compact_fip by auto
  1590 
  1591 lemma compact_imp_fip_image:
  1592   assumes "compact s"
  1593     and P: "\<And>i. i \<in> I \<Longrightarrow> closed (f i)"
  1594     and Q: "\<And>I'. finite I' \<Longrightarrow> I' \<subseteq> I \<Longrightarrow> (s \<inter> (\<Inter>i\<in>I'. f i) \<noteq> {})"
  1595   shows "s \<inter> (\<Inter>i\<in>I. f i) \<noteq> {}"
  1596 proof -
  1597   note `compact s`
  1598   moreover from P have "\<forall>i \<in> f ` I. closed i" by blast
  1599   moreover have "\<forall>A. finite A \<and> A \<subseteq> f ` I \<longrightarrow> (s \<inter> (\<Inter>A) \<noteq> {})"
  1600   proof (rule, rule, erule conjE)
  1601     fix A :: "'a set set"
  1602     assume "finite A"
  1603     moreover assume "A \<subseteq> f ` I"
  1604     ultimately obtain B where "B \<subseteq> I" and "finite B" and "A = f ` B"
  1605       using finite_subset_image [of A f I] by blast
  1606     with Q [of B] show "s \<inter> \<Inter>A \<noteq> {}" by simp
  1607   qed
  1608   ultimately have "s \<inter> (\<Inter>(f ` I)) \<noteq> {}" by (rule compact_imp_fip)
  1609   then show ?thesis by simp
  1610 qed
  1611 
  1612 end
  1613 
  1614 lemma (in t2_space) compact_imp_closed:
  1615   assumes "compact s" shows "closed s"
  1616 unfolding closed_def
  1617 proof (rule openI)
  1618   fix y assume "y \<in> - s"
  1619   let ?C = "\<Union>x\<in>s. {u. open u \<and> x \<in> u \<and> eventually (\<lambda>y. y \<notin> u) (nhds y)}"
  1620   note `compact s`
  1621   moreover have "\<forall>u\<in>?C. open u" by simp
  1622   moreover have "s \<subseteq> \<Union>?C"
  1623   proof
  1624     fix x assume "x \<in> s"
  1625     with `y \<in> - s` have "x \<noteq> y" by clarsimp
  1626     hence "\<exists>u v. open u \<and> open v \<and> x \<in> u \<and> y \<in> v \<and> u \<inter> v = {}"
  1627       by (rule hausdorff)
  1628     with `x \<in> s` show "x \<in> \<Union>?C"
  1629       unfolding eventually_nhds by auto
  1630   qed
  1631   ultimately obtain D where "D \<subseteq> ?C" and "finite D" and "s \<subseteq> \<Union>D"
  1632     by (rule compactE)
  1633   from `D \<subseteq> ?C` have "\<forall>x\<in>D. eventually (\<lambda>y. y \<notin> x) (nhds y)" by auto
  1634   with `finite D` have "eventually (\<lambda>y. y \<notin> \<Union>D) (nhds y)"
  1635     by (simp add: eventually_ball_finite)
  1636   with `s \<subseteq> \<Union>D` have "eventually (\<lambda>y. y \<notin> s) (nhds y)"
  1637     by (auto elim!: eventually_mono [rotated])
  1638   thus "\<exists>t. open t \<and> y \<in> t \<and> t \<subseteq> - s"
  1639     by (simp add: eventually_nhds subset_eq)
  1640 qed
  1641 
  1642 lemma compact_continuous_image:
  1643   assumes f: "continuous_on s f" and s: "compact s"
  1644   shows "compact (f ` s)"
  1645 proof (rule compactI)
  1646   fix C assume "\<forall>c\<in>C. open c" and cover: "f`s \<subseteq> \<Union>C"
  1647   with f have "\<forall>c\<in>C. \<exists>A. open A \<and> A \<inter> s = f -` c \<inter> s"
  1648     unfolding continuous_on_open_invariant by blast
  1649   then obtain A where A: "\<forall>c\<in>C. open (A c) \<and> A c \<inter> s = f -` c \<inter> s"
  1650     unfolding bchoice_iff ..
  1651   with cover have "\<forall>c\<in>C. open (A c)" "s \<subseteq> (\<Union>c\<in>C. A c)"
  1652     by (fastforce simp add: subset_eq set_eq_iff)+
  1653   from compactE_image[OF s this] obtain D where "D \<subseteq> C" "finite D" "s \<subseteq> (\<Union>c\<in>D. A c)" .
  1654   with A show "\<exists>D \<subseteq> C. finite D \<and> f`s \<subseteq> \<Union>D"
  1655     by (intro exI[of _ D]) (fastforce simp add: subset_eq set_eq_iff)+
  1656 qed
  1657 
  1658 lemma continuous_on_inv:
  1659   fixes f :: "'a::topological_space \<Rightarrow> 'b::t2_space"
  1660   assumes "continuous_on s f"  "compact s"  "\<forall>x\<in>s. g (f x) = x"
  1661   shows "continuous_on (f ` s) g"
  1662 unfolding continuous_on_topological
  1663 proof (clarsimp simp add: assms(3))
  1664   fix x :: 'a and B :: "'a set"
  1665   assume "x \<in> s" and "open B" and "x \<in> B"
  1666   have 1: "\<forall>x\<in>s. f x \<in> f ` (s - B) \<longleftrightarrow> x \<in> s - B"
  1667     using assms(3) by (auto, metis)
  1668   have "continuous_on (s - B) f"
  1669     using `continuous_on s f` Diff_subset
  1670     by (rule continuous_on_subset)
  1671   moreover have "compact (s - B)"
  1672     using `open B` and `compact s`
  1673     unfolding Diff_eq by (intro compact_inter_closed closed_Compl)
  1674   ultimately have "compact (f ` (s - B))"
  1675     by (rule compact_continuous_image)
  1676   hence "closed (f ` (s - B))"
  1677     by (rule compact_imp_closed)
  1678   hence "open (- f ` (s - B))"
  1679     by (rule open_Compl)
  1680   moreover have "f x \<in> - f ` (s - B)"
  1681     using `x \<in> s` and `x \<in> B` by (simp add: 1)
  1682   moreover have "\<forall>y\<in>s. f y \<in> - f ` (s - B) \<longrightarrow> y \<in> B"
  1683     by (simp add: 1)
  1684   ultimately show "\<exists>A. open A \<and> f x \<in> A \<and> (\<forall>y\<in>s. f y \<in> A \<longrightarrow> y \<in> B)"
  1685     by fast
  1686 qed
  1687 
  1688 lemma continuous_on_inv_into:
  1689   fixes f :: "'a::topological_space \<Rightarrow> 'b::t2_space"
  1690   assumes s: "continuous_on s f" "compact s" and f: "inj_on f s"
  1691   shows "continuous_on (f ` s) (the_inv_into s f)"
  1692   by (rule continuous_on_inv[OF s]) (auto simp: the_inv_into_f_f[OF f])
  1693 
  1694 lemma (in linorder_topology) compact_attains_sup:
  1695   assumes "compact S" "S \<noteq> {}"
  1696   shows "\<exists>s\<in>S. \<forall>t\<in>S. t \<le> s"
  1697 proof (rule classical)
  1698   assume "\<not> (\<exists>s\<in>S. \<forall>t\<in>S. t \<le> s)"
  1699   then obtain t where t: "\<forall>s\<in>S. t s \<in> S" and "\<forall>s\<in>S. s < t s"
  1700     by (metis not_le)
  1701   then have "\<forall>s\<in>S. open {..< t s}" "S \<subseteq> (\<Union>s\<in>S. {..< t s})"
  1702     by auto
  1703   with `compact S` obtain C where "C \<subseteq> S" "finite C" and C: "S \<subseteq> (\<Union>s\<in>C. {..< t s})"
  1704     by (erule compactE_image)
  1705   with `S \<noteq> {}` have Max: "Max (t`C) \<in> t`C" and "\<forall>s\<in>t`C. s \<le> Max (t`C)"
  1706     by (auto intro!: Max_in)
  1707   with C have "S \<subseteq> {..< Max (t`C)}"
  1708     by (auto intro: less_le_trans simp: subset_eq)
  1709   with t Max `C \<subseteq> S` show ?thesis
  1710     by fastforce
  1711 qed
  1712 
  1713 lemma (in linorder_topology) compact_attains_inf:
  1714   assumes "compact S" "S \<noteq> {}"
  1715   shows "\<exists>s\<in>S. \<forall>t\<in>S. s \<le> t"
  1716 proof (rule classical)
  1717   assume "\<not> (\<exists>s\<in>S. \<forall>t\<in>S. s \<le> t)"
  1718   then obtain t where t: "\<forall>s\<in>S. t s \<in> S" and "\<forall>s\<in>S. t s < s"
  1719     by (metis not_le)
  1720   then have "\<forall>s\<in>S. open {t s <..}" "S \<subseteq> (\<Union>s\<in>S. {t s <..})"
  1721     by auto
  1722   with `compact S` obtain C where "C \<subseteq> S" "finite C" and C: "S \<subseteq> (\<Union>s\<in>C. {t s <..})"
  1723     by (erule compactE_image)
  1724   with `S \<noteq> {}` have Min: "Min (t`C) \<in> t`C" and "\<forall>s\<in>t`C. Min (t`C) \<le> s"
  1725     by (auto intro!: Min_in)
  1726   with C have "S \<subseteq> {Min (t`C) <..}"
  1727     by (auto intro: le_less_trans simp: subset_eq)
  1728   with t Min `C \<subseteq> S` show ?thesis
  1729     by fastforce
  1730 qed
  1731 
  1732 lemma continuous_attains_sup:
  1733   fixes f :: "'a::topological_space \<Rightarrow> 'b::linorder_topology"
  1734   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)"
  1735   using compact_attains_sup[of "f ` s"] compact_continuous_image[of s f] by auto
  1736 
  1737 lemma continuous_attains_inf:
  1738   fixes f :: "'a::topological_space \<Rightarrow> 'b::linorder_topology"
  1739   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)"
  1740   using compact_attains_inf[of "f ` s"] compact_continuous_image[of s f] by auto
  1741 
  1742 subsection {* Connectedness *}
  1743 
  1744 context topological_space
  1745 begin
  1746 
  1747 definition "connected S \<longleftrightarrow>
  1748   \<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> {})"
  1749 
  1750 lemma connectedI:
  1751   "(\<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)
  1752   \<Longrightarrow> connected U"
  1753   by (auto simp: connected_def)
  1754 
  1755 lemma connected_empty[simp]: "connected {}"
  1756   by (auto intro!: connectedI)
  1757 
  1758 lemma connectedD:
  1759   "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 = {}" 
  1760   by (auto simp: connected_def)
  1761 
  1762 end
  1763 
  1764 lemma connected_iff_const:
  1765   fixes S :: "'a::topological_space set"
  1766   shows "connected S \<longleftrightarrow> (\<forall>P::'a \<Rightarrow> bool. continuous_on S P \<longrightarrow> (\<exists>c. \<forall>s\<in>S. P s = c))"
  1767 proof safe
  1768   fix P :: "'a \<Rightarrow> bool" assume "connected S" "continuous_on S P"
  1769   then have "\<And>b. \<exists>A. open A \<and> A \<inter> S = P -` {b} \<inter> S"
  1770     unfolding continuous_on_open_invariant by simp
  1771   from this[of True] this[of False]
  1772   obtain t f where "open t" "open f" and *: "f \<inter> S = P -` {False} \<inter> S" "t \<inter> S = P -` {True} \<inter> S"
  1773     by auto
  1774   then have "t \<inter> S = {} \<or> f \<inter> S = {}"
  1775     by (intro connectedD[OF `connected S`])  auto
  1776   then show "\<exists>c. \<forall>s\<in>S. P s = c"
  1777   proof (rule disjE)
  1778     assume "t \<inter> S = {}" then show ?thesis
  1779       unfolding * by (intro exI[of _ False]) auto
  1780   next
  1781     assume "f \<inter> S = {}" then show ?thesis
  1782       unfolding * by (intro exI[of _ True]) auto
  1783   qed
  1784 next
  1785   assume P: "\<forall>P::'a \<Rightarrow> bool. continuous_on S P \<longrightarrow> (\<exists>c. \<forall>s\<in>S. P s = c)"
  1786   show "connected S"
  1787   proof (rule connectedI)
  1788     fix A B assume *: "open A" "open B" "A \<inter> S \<noteq> {}" "B \<inter> S \<noteq> {}" "A \<inter> B \<inter> S = {}" "S \<subseteq> A \<union> B"
  1789     have "continuous_on S (\<lambda>x. x \<in> A)"
  1790       unfolding continuous_on_open_invariant
  1791     proof safe
  1792       fix C :: "bool set"
  1793       have "C = UNIV \<or> C = {True} \<or> C = {False} \<or> C = {}"
  1794         using subset_UNIV[of C] unfolding UNIV_bool by auto
  1795       with * show "\<exists>T. open T \<and> T \<inter> S = (\<lambda>x. x \<in> A) -` C \<inter> S"
  1796         by (intro exI[of _ "(if True \<in> C then A else {}) \<union> (if False \<in> C then B else {})"]) auto
  1797     qed
  1798     from P[rule_format, OF this] obtain c where "\<And>s. s \<in> S \<Longrightarrow> (s \<in> A) = c" by blast
  1799     with * show False
  1800       by (cases c) auto
  1801   qed
  1802 qed
  1803 
  1804 lemma connectedD_const:
  1805   fixes P :: "'a::topological_space \<Rightarrow> bool"
  1806   shows "connected S \<Longrightarrow> continuous_on S P \<Longrightarrow> \<exists>c. \<forall>s\<in>S. P s = c"
  1807   unfolding connected_iff_const by auto
  1808 
  1809 lemma connectedI_const:
  1810   "(\<And>P::'a::topological_space \<Rightarrow> bool. continuous_on S P \<Longrightarrow> \<exists>c. \<forall>s\<in>S. P s = c) \<Longrightarrow> connected S"
  1811   unfolding connected_iff_const by auto
  1812 
  1813 lemma connected_local_const:
  1814   assumes "connected A" "a \<in> A" "b \<in> A"
  1815   assumes *: "\<forall>a\<in>A. eventually (\<lambda>b. f a = f b) (at a within A)"
  1816   shows "f a = f b"
  1817 proof -
  1818   obtain S where S: "\<And>a. a \<in> A \<Longrightarrow> a \<in> S a" "\<And>a. a \<in> A \<Longrightarrow> open (S a)"
  1819     "\<And>a x. a \<in> A \<Longrightarrow> x \<in> S a \<Longrightarrow> x \<in> A \<Longrightarrow> f a = f x"
  1820     using * unfolding eventually_at_topological by metis
  1821 
  1822   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"
  1823   have "?P \<inter> A = {} \<or> ?N \<inter> A = {}"
  1824     using `connected A` S `a\<in>A`
  1825     by (intro connectedD) (auto, metis)
  1826   then show "f a = f b"
  1827   proof
  1828     assume "?N \<inter> A = {}"
  1829     then have "\<forall>x\<in>A. f a = f x"
  1830       using S(1) by auto
  1831     with `b\<in>A` show ?thesis by auto
  1832   next
  1833     assume "?P \<inter> A = {}" then show ?thesis
  1834       using `a \<in> A` S(1)[of a] by auto
  1835   qed
  1836 qed
  1837 
  1838 lemma (in linorder_topology) connectedD_interval:
  1839   assumes "connected U" and xy: "x \<in> U" "y \<in> U" and "x \<le> z" "z \<le> y"
  1840   shows "z \<in> U"
  1841 proof -
  1842   have eq: "{..<z} \<union> {z<..} = - {z}"
  1843     by auto
  1844   { assume "z \<notin> U" "x < z" "z < y"
  1845     with xy have "\<not> connected U"
  1846       unfolding connected_def simp_thms
  1847       apply (rule_tac exI[of _ "{..< z}"])
  1848       apply (rule_tac exI[of _ "{z <..}"])
  1849       apply (auto simp add: eq)
  1850       done }
  1851   with assms show "z \<in> U"
  1852     by (metis less_le)
  1853 qed
  1854 
  1855 lemma connected_continuous_image:
  1856   assumes *: "continuous_on s f"
  1857   assumes "connected s"
  1858   shows "connected (f ` s)"
  1859 proof (rule connectedI_const)
  1860   fix P :: "'b \<Rightarrow> bool" assume "continuous_on (f ` s) P"
  1861   then have "continuous_on s (P \<circ> f)"
  1862     by (rule continuous_on_compose[OF *])
  1863   from connectedD_const[OF `connected s` this] show "\<exists>c. \<forall>s\<in>f ` s. P s = c"
  1864     by auto
  1865 qed
  1866 
  1867 section {* Connectedness *}
  1868 
  1869 class linear_continuum_topology = linorder_topology + linear_continuum
  1870 begin
  1871 
  1872 lemma Inf_notin_open:
  1873   assumes A: "open A" and bnd: "\<forall>a\<in>A. x < a"
  1874   shows "Inf A \<notin> A"
  1875 proof
  1876   assume "Inf A \<in> A"
  1877   then obtain b where "b < Inf A" "{b <.. Inf A} \<subseteq> A"
  1878     using open_left[of A "Inf A" x] assms by auto
  1879   with dense[of b "Inf A"] obtain c where "c < Inf A" "c \<in> A"
  1880     by (auto simp: subset_eq)
  1881   then show False
  1882     using cInf_lower[OF `c \<in> A`] bnd by (metis not_le less_imp_le bdd_belowI)
  1883 qed
  1884 
  1885 lemma Sup_notin_open:
  1886   assumes A: "open A" and bnd: "\<forall>a\<in>A. a < x"
  1887   shows "Sup A \<notin> A"
  1888 proof
  1889   assume "Sup A \<in> A"
  1890   then obtain b where "Sup A < b" "{Sup A ..< b} \<subseteq> A"
  1891     using open_right[of A "Sup A" x] assms by auto
  1892   with dense[of "Sup A" b] obtain c where "Sup A < c" "c \<in> A"
  1893     by (auto simp: subset_eq)
  1894   then show False
  1895     using cSup_upper[OF `c \<in> A`] bnd by (metis less_imp_le not_le bdd_aboveI)
  1896 qed
  1897 
  1898 end
  1899 
  1900 instance linear_continuum_topology \<subseteq> perfect_space
  1901 proof
  1902   fix x :: 'a
  1903   obtain y where "x < y \<or> y < x"
  1904     using ex_gt_or_lt [of x] ..
  1905   with Inf_notin_open[of "{x}" y] Sup_notin_open[of "{x}" y]
  1906   show "\<not> open {x}"
  1907     by auto
  1908 qed
  1909 
  1910 lemma connectedI_interval:
  1911   fixes U :: "'a :: linear_continuum_topology set"
  1912   assumes *: "\<And>x y z. x \<in> U \<Longrightarrow> y \<in> U \<Longrightarrow> x \<le> z \<Longrightarrow> z \<le> y \<Longrightarrow> z \<in> U"
  1913   shows "connected U"
  1914 proof (rule connectedI)
  1915   { fix A B assume "open A" "open B" "A \<inter> B \<inter> U = {}" "U \<subseteq> A \<union> B"
  1916     fix x y assume "x < y" "x \<in> A" "y \<in> B" "x \<in> U" "y \<in> U"
  1917 
  1918     let ?z = "Inf (B \<inter> {x <..})"
  1919 
  1920     have "x \<le> ?z" "?z \<le> y"
  1921       using `y \<in> B` `x < y` by (auto intro: cInf_lower cInf_greatest)
  1922     with `x \<in> U` `y \<in> U` have "?z \<in> U"
  1923       by (rule *)
  1924     moreover have "?z \<notin> B \<inter> {x <..}"
  1925       using `open B` by (intro Inf_notin_open) auto
  1926     ultimately have "?z \<in> A"
  1927       using `x \<le> ?z` `A \<inter> B \<inter> U = {}` `x \<in> A` `U \<subseteq> A \<union> B` by auto
  1928 
  1929     { assume "?z < y"
  1930       obtain a where "?z < a" "{?z ..< a} \<subseteq> A"
  1931         using open_right[OF `open A` `?z \<in> A` `?z < y`] by auto
  1932       moreover obtain b where "b \<in> B" "x < b" "b < min a y"
  1933         using cInf_less_iff[of "B \<inter> {x <..}" "min a y"] `?z < a` `?z < y` `x < y` `y \<in> B`
  1934         by (auto intro: less_imp_le)
  1935       moreover have "?z \<le> b"
  1936         using `b \<in> B` `x < b`
  1937         by (intro cInf_lower) auto
  1938       moreover have "b \<in> U"
  1939         using `x \<le> ?z` `?z \<le> b` `b < min a y`
  1940         by (intro *[OF `x \<in> U` `y \<in> U`]) (auto simp: less_imp_le)
  1941       ultimately have "\<exists>b\<in>B. b \<in> A \<and> b \<in> U"
  1942         by (intro bexI[of _ b]) auto }
  1943     then have False
  1944       using `?z \<le> y` `?z \<in> A` `y \<in> B` `y \<in> U` `A \<inter> B \<inter> U = {}` unfolding le_less by blast }
  1945   note not_disjoint = this
  1946 
  1947   fix A B assume AB: "open A" "open B" "U \<subseteq> A \<union> B" "A \<inter> B \<inter> U = {}"
  1948   moreover assume "A \<inter> U \<noteq> {}" then obtain x where x: "x \<in> U" "x \<in> A" by auto
  1949   moreover assume "B \<inter> U \<noteq> {}" then obtain y where y: "y \<in> U" "y \<in> B" by auto
  1950   moreover note not_disjoint[of B A y x] not_disjoint[of A B x y]
  1951   ultimately show False by (cases x y rule: linorder_cases) auto
  1952 qed
  1953 
  1954 lemma connected_iff_interval:
  1955   fixes U :: "'a :: linear_continuum_topology set"
  1956   shows "connected U \<longleftrightarrow> (\<forall>x\<in>U. \<forall>y\<in>U. \<forall>z. x \<le> z \<longrightarrow> z \<le> y \<longrightarrow> z \<in> U)"
  1957   by (auto intro: connectedI_interval dest: connectedD_interval)
  1958 
  1959 lemma connected_UNIV[simp]: "connected (UNIV::'a::linear_continuum_topology set)"
  1960   unfolding connected_iff_interval by auto
  1961 
  1962 lemma connected_Ioi[simp]: "connected {a::'a::linear_continuum_topology <..}"
  1963   unfolding connected_iff_interval by auto
  1964 
  1965 lemma connected_Ici[simp]: "connected {a::'a::linear_continuum_topology ..}"
  1966   unfolding connected_iff_interval by auto
  1967 
  1968 lemma connected_Iio[simp]: "connected {..< a::'a::linear_continuum_topology}"
  1969   unfolding connected_iff_interval by auto
  1970 
  1971 lemma connected_Iic[simp]: "connected {.. a::'a::linear_continuum_topology}"
  1972   unfolding connected_iff_interval by auto
  1973 
  1974 lemma connected_Ioo[simp]: "connected {a <..< b::'a::linear_continuum_topology}"
  1975   unfolding connected_iff_interval by auto
  1976 
  1977 lemma connected_Ioc[simp]: "connected {a <.. b::'a::linear_continuum_topology}"
  1978   unfolding connected_iff_interval by auto
  1979 
  1980 lemma connected_Ico[simp]: "connected {a ..< b::'a::linear_continuum_topology}"
  1981   unfolding connected_iff_interval by auto
  1982 
  1983 lemma connected_Icc[simp]: "connected {a .. b::'a::linear_continuum_topology}"
  1984   unfolding connected_iff_interval by auto
  1985 
  1986 lemma connected_contains_Ioo: 
  1987   fixes A :: "'a :: linorder_topology set"
  1988   assumes A: "connected A" "a \<in> A" "b \<in> A" shows "{a <..< b} \<subseteq> A"
  1989   using connectedD_interval[OF A] by (simp add: subset_eq Ball_def less_imp_le)
  1990 
  1991 subsection {* Intermediate Value Theorem *}
  1992 
  1993 lemma IVT':
  1994   fixes f :: "'a :: linear_continuum_topology \<Rightarrow> 'b :: linorder_topology"
  1995   assumes y: "f a \<le> y" "y \<le> f b" "a \<le> b"
  1996   assumes *: "continuous_on {a .. b} f"
  1997   shows "\<exists>x. a \<le> x \<and> x \<le> b \<and> f x = y"
  1998 proof -
  1999   have "connected {a..b}"
  2000     unfolding connected_iff_interval by auto
  2001   from connected_continuous_image[OF * this, THEN connectedD_interval, of "f a" "f b" y] y
  2002   show ?thesis
  2003     by (auto simp add: atLeastAtMost_def atLeast_def atMost_def)
  2004 qed
  2005 
  2006 lemma IVT2':
  2007   fixes f :: "'a :: linear_continuum_topology \<Rightarrow> 'b :: linorder_topology"
  2008   assumes y: "f b \<le> y" "y \<le> f a" "a \<le> b"
  2009   assumes *: "continuous_on {a .. b} f"
  2010   shows "\<exists>x. a \<le> x \<and> x \<le> b \<and> f x = y"
  2011 proof -
  2012   have "connected {a..b}"
  2013     unfolding connected_iff_interval by auto
  2014   from connected_continuous_image[OF * this, THEN connectedD_interval, of "f b" "f a" y] y
  2015   show ?thesis
  2016     by (auto simp add: atLeastAtMost_def atLeast_def atMost_def)
  2017 qed
  2018 
  2019 lemma IVT:
  2020   fixes f :: "'a :: linear_continuum_topology \<Rightarrow> 'b :: linorder_topology"
  2021   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> \<exists>x. a \<le> x \<and> x \<le> b \<and> f x = y"
  2022   by (rule IVT') (auto intro: continuous_at_imp_continuous_on)
  2023 
  2024 lemma IVT2:
  2025   fixes f :: "'a :: linear_continuum_topology \<Rightarrow> 'b :: linorder_topology"
  2026   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> \<exists>x. a \<le> x \<and> x \<le> b \<and> f x = y"
  2027   by (rule IVT2') (auto intro: continuous_at_imp_continuous_on)
  2028 
  2029 lemma continuous_inj_imp_mono:
  2030   fixes f :: "'a::linear_continuum_topology \<Rightarrow> 'b :: linorder_topology"
  2031   assumes x: "a < x" "x < b"
  2032   assumes cont: "continuous_on {a..b} f"
  2033   assumes inj: "inj_on f {a..b}"
  2034   shows "(f a < f x \<and> f x < f b) \<or> (f b < f x \<and> f x < f a)"
  2035 proof -
  2036   note I = inj_on_iff[OF inj]
  2037   { assume "f x < f a" "f x < f b"
  2038     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"
  2039       using IVT'[of f x "min (f a) (f b)" b] IVT2'[of f x "min (f a) (f b)" a] x
  2040       by (auto simp: continuous_on_subset[OF cont] less_imp_le)
  2041     with x I have False by auto }
  2042   moreover
  2043   { assume "f a < f x" "f b < f x"
  2044     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"
  2045       using IVT'[of f a "max (f a) (f b)" x] IVT2'[of f b "max (f a) (f b)" x] x
  2046       by (auto simp: continuous_on_subset[OF cont] less_imp_le)
  2047     with x I have False by auto }
  2048   ultimately show ?thesis
  2049     using I[of a x] I[of x b] x less_trans[OF x] by (auto simp add: le_less less_imp_neq neq_iff)
  2050 qed
  2051 
  2052 lemma continuous_at_Sup_mono:
  2053   fixes f :: "'a :: {linorder_topology, conditionally_complete_linorder} \<Rightarrow> 'b :: {linorder_topology, conditionally_complete_linorder}"
  2054   assumes "mono f"
  2055   assumes cont: "continuous (at_left (Sup S)) f"
  2056   assumes S: "S \<noteq> {}" "bdd_above S"
  2057   shows "f (Sup S) = (SUP s:S. f s)"
  2058 proof (rule antisym)
  2059   have f: "(f ---> f (Sup S)) (at_left (Sup S))"
  2060     using cont unfolding continuous_within .
  2061 
  2062   show "f (Sup S) \<le> (SUP s:S. f s)"
  2063   proof cases
  2064     assume "Sup S \<in> S" then show ?thesis
  2065       by (rule cSUP_upper) (auto intro: bdd_above_image_mono S `mono f`)
  2066   next
  2067     assume "Sup S \<notin> S"
  2068     from `S \<noteq> {}` obtain s where "s \<in> S"
  2069       by auto
  2070     with `Sup S \<notin> S` S have "s < Sup S"
  2071       unfolding less_le by (blast intro: cSup_upper)
  2072     show ?thesis
  2073     proof (rule ccontr)
  2074       assume "\<not> ?thesis"
  2075       with order_tendstoD(1)[OF f, of "SUP s:S. f s"] obtain b where "b < Sup S"
  2076         and *: "\<And>y. b < y \<Longrightarrow> y < Sup S \<Longrightarrow> (SUP s:S. f s) < f y"
  2077         by (auto simp: not_le eventually_at_left[OF `s < Sup S`])
  2078       with `S \<noteq> {}` obtain c where "c \<in> S" "b < c"
  2079         using less_cSupD[of S b] by auto
  2080       with `Sup S \<notin> S` S have "c < Sup S"
  2081         unfolding less_le by (blast intro: cSup_upper)
  2082       from *[OF `b < c` `c < Sup S`] cSUP_upper[OF `c \<in> S` bdd_above_image_mono[of f]]
  2083       show False
  2084         by (auto simp: assms)
  2085     qed
  2086   qed
  2087 qed (intro cSUP_least `mono f`[THEN monoD] cSup_upper S)
  2088 
  2089 lemma continuous_at_Sup_antimono:
  2090   fixes f :: "'a :: {linorder_topology, conditionally_complete_linorder} \<Rightarrow> 'b :: {linorder_topology, conditionally_complete_linorder}"
  2091   assumes "antimono f"
  2092   assumes cont: "continuous (at_left (Sup S)) f"
  2093   assumes S: "S \<noteq> {}" "bdd_above S"
  2094   shows "f (Sup S) = (INF s:S. f s)"
  2095 proof (rule antisym)
  2096   have f: "(f ---> f (Sup S)) (at_left (Sup S))"
  2097     using cont unfolding continuous_within .
  2098 
  2099   show "(INF s:S. f s) \<le> f (Sup S)"
  2100   proof cases
  2101     assume "Sup S \<in> S" then show ?thesis
  2102       by (intro cINF_lower) (auto intro: bdd_below_image_antimono S `antimono f`)
  2103   next
  2104     assume "Sup S \<notin> S"
  2105     from `S \<noteq> {}` obtain s where "s \<in> S"
  2106       by auto
  2107     with `Sup S \<notin> S` S have "s < Sup S"
  2108       unfolding less_le by (blast intro: cSup_upper)
  2109     show ?thesis
  2110     proof (rule ccontr)
  2111       assume "\<not> ?thesis"
  2112       with order_tendstoD(2)[OF f, of "INF s:S. f s"] obtain b where "b < Sup S"
  2113         and *: "\<And>y. b < y \<Longrightarrow> y < Sup S \<Longrightarrow> f y < (INF s:S. f s)"
  2114         by (auto simp: not_le eventually_at_left[OF `s < Sup S`])
  2115       with `S \<noteq> {}` obtain c where "c \<in> S" "b < c"
  2116         using less_cSupD[of S b] by auto
  2117       with `Sup S \<notin> S` S have "c < Sup S"
  2118         unfolding less_le by (blast intro: cSup_upper)
  2119       from *[OF `b < c` `c < Sup S`] cINF_lower[OF bdd_below_image_antimono, of f S c] `c \<in> S`
  2120       show False
  2121         by (auto simp: assms)
  2122     qed
  2123   qed
  2124 qed (intro cINF_greatest `antimono f`[THEN antimonoD] cSup_upper S)
  2125 
  2126 lemma continuous_at_Inf_mono:
  2127   fixes f :: "'a :: {linorder_topology, conditionally_complete_linorder} \<Rightarrow> 'b :: {linorder_topology, conditionally_complete_linorder}"
  2128   assumes "mono f"
  2129   assumes cont: "continuous (at_right (Inf S)) f"
  2130   assumes S: "S \<noteq> {}" "bdd_below S"
  2131   shows "f (Inf S) = (INF s:S. f s)"
  2132 proof (rule antisym)
  2133   have f: "(f ---> f (Inf S)) (at_right (Inf S))"
  2134     using cont unfolding continuous_within .
  2135 
  2136   show "(INF s:S. f s) \<le> f (Inf S)"
  2137   proof cases
  2138     assume "Inf S \<in> S" then show ?thesis
  2139       by (rule cINF_lower[rotated]) (auto intro: bdd_below_image_mono S `mono f`)
  2140   next
  2141     assume "Inf S \<notin> S"
  2142     from `S \<noteq> {}` obtain s where "s \<in> S"
  2143       by auto
  2144     with `Inf S \<notin> S` S have "Inf S < s"
  2145       unfolding less_le by (blast intro: cInf_lower)
  2146     show ?thesis
  2147     proof (rule ccontr)
  2148       assume "\<not> ?thesis"
  2149       with order_tendstoD(2)[OF f, of "INF s:S. f s"] obtain b where "Inf S < b"
  2150         and *: "\<And>y. Inf S < y \<Longrightarrow> y < b \<Longrightarrow> f y < (INF s:S. f s)"
  2151         by (auto simp: not_le eventually_at_right[OF `Inf S < s`])
  2152       with `S \<noteq> {}` obtain c where "c \<in> S" "c < b"
  2153         using cInf_lessD[of S b] by auto
  2154       with `Inf S \<notin> S` S have "Inf S < c"
  2155         unfolding less_le by (blast intro: cInf_lower)
  2156       from *[OF `Inf S < c` `c < b`] cINF_lower[OF bdd_below_image_mono[of f] `c \<in> S`]
  2157       show False
  2158         by (auto simp: assms)
  2159     qed
  2160   qed
  2161 qed (intro cINF_greatest `mono f`[THEN monoD] cInf_lower `bdd_below S` `S \<noteq> {}`)
  2162 
  2163 lemma continuous_at_Inf_antimono:
  2164   fixes f :: "'a :: {linorder_topology, conditionally_complete_linorder} \<Rightarrow> 'b :: {linorder_topology, conditionally_complete_linorder}"
  2165   assumes "antimono f"
  2166   assumes cont: "continuous (at_right (Inf S)) f"
  2167   assumes S: "S \<noteq> {}" "bdd_below S"
  2168   shows "f (Inf S) = (SUP s:S. f s)"
  2169 proof (rule antisym)
  2170   have f: "(f ---> f (Inf S)) (at_right (Inf S))"
  2171     using cont unfolding continuous_within .
  2172 
  2173   show "f (Inf S) \<le> (SUP s:S. f s)"
  2174   proof cases
  2175     assume "Inf S \<in> S" then show ?thesis
  2176       by (rule cSUP_upper) (auto intro: bdd_above_image_antimono S `antimono f`)
  2177   next
  2178     assume "Inf S \<notin> S"
  2179     from `S \<noteq> {}` obtain s where "s \<in> S"
  2180       by auto
  2181     with `Inf S \<notin> S` S have "Inf S < s"
  2182       unfolding less_le by (blast intro: cInf_lower)
  2183     show ?thesis
  2184     proof (rule ccontr)
  2185       assume "\<not> ?thesis"
  2186       with order_tendstoD(1)[OF f, of "SUP s:S. f s"] obtain b where "Inf S < b"
  2187         and *: "\<And>y. Inf S < y \<Longrightarrow> y < b \<Longrightarrow> (SUP s:S. f s) < f y"
  2188         by (auto simp: not_le eventually_at_right[OF `Inf S < s`])
  2189       with `S \<noteq> {}` obtain c where "c \<in> S" "c < b"
  2190         using cInf_lessD[of S b] by auto
  2191       with `Inf S \<notin> S` S have "Inf S < c"
  2192         unfolding less_le by (blast intro: cInf_lower)
  2193       from *[OF `Inf S < c` `c < b`] cSUP_upper[OF `c \<in> S` bdd_above_image_antimono[of f]]
  2194       show False
  2195         by (auto simp: assms)
  2196     qed
  2197   qed
  2198 qed (intro cSUP_least `antimono f`[THEN antimonoD] cInf_lower S)
  2199 
  2200 end