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