src/HOL/Topological_Spaces.thy
author hoelzl
Fri Mar 22 10:41:42 2013 +0100 (2013-03-22)
changeset 51471 cad22a3cc09c
child 51473 1210309fddab
permissions -rw-r--r--
move topological_space to its own theory
hoelzl@51471
     1
(*  Title:      HOL/Basic_Topology.thy
hoelzl@51471
     2
    Author:     Brian Huffman
hoelzl@51471
     3
    Author:     Johannes Hölzl
hoelzl@51471
     4
*)
hoelzl@51471
     5
hoelzl@51471
     6
header {* Topological Spaces *}
hoelzl@51471
     7
hoelzl@51471
     8
theory Topological_Spaces
hoelzl@51471
     9
imports Main
hoelzl@51471
    10
begin
hoelzl@51471
    11
hoelzl@51471
    12
subsection {* Topological space *}
hoelzl@51471
    13
hoelzl@51471
    14
class "open" =
hoelzl@51471
    15
  fixes "open" :: "'a set \<Rightarrow> bool"
hoelzl@51471
    16
hoelzl@51471
    17
class topological_space = "open" +
hoelzl@51471
    18
  assumes open_UNIV [simp, intro]: "open UNIV"
hoelzl@51471
    19
  assumes open_Int [intro]: "open S \<Longrightarrow> open T \<Longrightarrow> open (S \<inter> T)"
hoelzl@51471
    20
  assumes open_Union [intro]: "\<forall>S\<in>K. open S \<Longrightarrow> open (\<Union> K)"
hoelzl@51471
    21
begin
hoelzl@51471
    22
hoelzl@51471
    23
definition
hoelzl@51471
    24
  closed :: "'a set \<Rightarrow> bool" where
hoelzl@51471
    25
  "closed S \<longleftrightarrow> open (- S)"
hoelzl@51471
    26
hoelzl@51471
    27
lemma open_empty [intro, simp]: "open {}"
hoelzl@51471
    28
  using open_Union [of "{}"] by simp
hoelzl@51471
    29
hoelzl@51471
    30
lemma open_Un [intro]: "open S \<Longrightarrow> open T \<Longrightarrow> open (S \<union> T)"
hoelzl@51471
    31
  using open_Union [of "{S, T}"] by simp
hoelzl@51471
    32
hoelzl@51471
    33
lemma open_UN [intro]: "\<forall>x\<in>A. open (B x) \<Longrightarrow> open (\<Union>x\<in>A. B x)"
hoelzl@51471
    34
  unfolding SUP_def by (rule open_Union) auto
hoelzl@51471
    35
hoelzl@51471
    36
lemma open_Inter [intro]: "finite S \<Longrightarrow> \<forall>T\<in>S. open T \<Longrightarrow> open (\<Inter>S)"
hoelzl@51471
    37
  by (induct set: finite) auto
hoelzl@51471
    38
hoelzl@51471
    39
lemma open_INT [intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. open (B x) \<Longrightarrow> open (\<Inter>x\<in>A. B x)"
hoelzl@51471
    40
  unfolding INF_def by (rule open_Inter) auto
hoelzl@51471
    41
hoelzl@51471
    42
lemma closed_empty [intro, simp]:  "closed {}"
hoelzl@51471
    43
  unfolding closed_def by simp
hoelzl@51471
    44
hoelzl@51471
    45
lemma closed_Un [intro]: "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<union> T)"
hoelzl@51471
    46
  unfolding closed_def by auto
hoelzl@51471
    47
hoelzl@51471
    48
lemma closed_UNIV [intro, simp]: "closed UNIV"
hoelzl@51471
    49
  unfolding closed_def by simp
hoelzl@51471
    50
hoelzl@51471
    51
lemma closed_Int [intro]: "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<inter> T)"
hoelzl@51471
    52
  unfolding closed_def by auto
hoelzl@51471
    53
hoelzl@51471
    54
lemma closed_INT [intro]: "\<forall>x\<in>A. closed (B x) \<Longrightarrow> closed (\<Inter>x\<in>A. B x)"
hoelzl@51471
    55
  unfolding closed_def by auto
hoelzl@51471
    56
hoelzl@51471
    57
lemma closed_Inter [intro]: "\<forall>S\<in>K. closed S \<Longrightarrow> closed (\<Inter> K)"
hoelzl@51471
    58
  unfolding closed_def uminus_Inf by auto
hoelzl@51471
    59
hoelzl@51471
    60
lemma closed_Union [intro]: "finite S \<Longrightarrow> \<forall>T\<in>S. closed T \<Longrightarrow> closed (\<Union>S)"
hoelzl@51471
    61
  by (induct set: finite) auto
hoelzl@51471
    62
hoelzl@51471
    63
lemma closed_UN [intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. closed (B x) \<Longrightarrow> closed (\<Union>x\<in>A. B x)"
hoelzl@51471
    64
  unfolding SUP_def by (rule closed_Union) auto
hoelzl@51471
    65
hoelzl@51471
    66
lemma open_closed: "open S \<longleftrightarrow> closed (- S)"
hoelzl@51471
    67
  unfolding closed_def by simp
hoelzl@51471
    68
hoelzl@51471
    69
lemma closed_open: "closed S \<longleftrightarrow> open (- S)"
hoelzl@51471
    70
  unfolding closed_def by simp
hoelzl@51471
    71
hoelzl@51471
    72
lemma open_Diff [intro]: "open S \<Longrightarrow> closed T \<Longrightarrow> open (S - T)"
hoelzl@51471
    73
  unfolding closed_open Diff_eq by (rule open_Int)
hoelzl@51471
    74
hoelzl@51471
    75
lemma closed_Diff [intro]: "closed S \<Longrightarrow> open T \<Longrightarrow> closed (S - T)"
hoelzl@51471
    76
  unfolding open_closed Diff_eq by (rule closed_Int)
hoelzl@51471
    77
hoelzl@51471
    78
lemma open_Compl [intro]: "closed S \<Longrightarrow> open (- S)"
hoelzl@51471
    79
  unfolding closed_open .
hoelzl@51471
    80
hoelzl@51471
    81
lemma closed_Compl [intro]: "open S \<Longrightarrow> closed (- S)"
hoelzl@51471
    82
  unfolding open_closed .
hoelzl@51471
    83
hoelzl@51471
    84
end
hoelzl@51471
    85
hoelzl@51471
    86
subsection{* Hausdorff and other separation properties *}
hoelzl@51471
    87
hoelzl@51471
    88
class t0_space = topological_space +
hoelzl@51471
    89
  assumes t0_space: "x \<noteq> y \<Longrightarrow> \<exists>U. open U \<and> \<not> (x \<in> U \<longleftrightarrow> y \<in> U)"
hoelzl@51471
    90
hoelzl@51471
    91
class t1_space = topological_space +
hoelzl@51471
    92
  assumes t1_space: "x \<noteq> y \<Longrightarrow> \<exists>U. open U \<and> x \<in> U \<and> y \<notin> U"
hoelzl@51471
    93
hoelzl@51471
    94
instance t1_space \<subseteq> t0_space
hoelzl@51471
    95
proof qed (fast dest: t1_space)
hoelzl@51471
    96
hoelzl@51471
    97
lemma separation_t1:
hoelzl@51471
    98
  fixes x y :: "'a::t1_space"
hoelzl@51471
    99
  shows "x \<noteq> y \<longleftrightarrow> (\<exists>U. open U \<and> x \<in> U \<and> y \<notin> U)"
hoelzl@51471
   100
  using t1_space[of x y] by blast
hoelzl@51471
   101
hoelzl@51471
   102
lemma closed_singleton:
hoelzl@51471
   103
  fixes a :: "'a::t1_space"
hoelzl@51471
   104
  shows "closed {a}"
hoelzl@51471
   105
proof -
hoelzl@51471
   106
  let ?T = "\<Union>{S. open S \<and> a \<notin> S}"
hoelzl@51471
   107
  have "open ?T" by (simp add: open_Union)
hoelzl@51471
   108
  also have "?T = - {a}"
hoelzl@51471
   109
    by (simp add: set_eq_iff separation_t1, auto)
hoelzl@51471
   110
  finally show "closed {a}" unfolding closed_def .
hoelzl@51471
   111
qed
hoelzl@51471
   112
hoelzl@51471
   113
lemma closed_insert [simp]:
hoelzl@51471
   114
  fixes a :: "'a::t1_space"
hoelzl@51471
   115
  assumes "closed S" shows "closed (insert a S)"
hoelzl@51471
   116
proof -
hoelzl@51471
   117
  from closed_singleton assms
hoelzl@51471
   118
  have "closed ({a} \<union> S)" by (rule closed_Un)
hoelzl@51471
   119
  thus "closed (insert a S)" by simp
hoelzl@51471
   120
qed
hoelzl@51471
   121
hoelzl@51471
   122
lemma finite_imp_closed:
hoelzl@51471
   123
  fixes S :: "'a::t1_space set"
hoelzl@51471
   124
  shows "finite S \<Longrightarrow> closed S"
hoelzl@51471
   125
by (induct set: finite, simp_all)
hoelzl@51471
   126
hoelzl@51471
   127
text {* T2 spaces are also known as Hausdorff spaces. *}
hoelzl@51471
   128
hoelzl@51471
   129
class t2_space = topological_space +
hoelzl@51471
   130
  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
   131
hoelzl@51471
   132
instance t2_space \<subseteq> t1_space
hoelzl@51471
   133
proof qed (fast dest: hausdorff)
hoelzl@51471
   134
hoelzl@51471
   135
lemma separation_t2:
hoelzl@51471
   136
  fixes x y :: "'a::t2_space"
hoelzl@51471
   137
  shows "x \<noteq> y \<longleftrightarrow> (\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {})"
hoelzl@51471
   138
  using hausdorff[of x y] by blast
hoelzl@51471
   139
hoelzl@51471
   140
lemma separation_t0:
hoelzl@51471
   141
  fixes x y :: "'a::t0_space"
hoelzl@51471
   142
  shows "x \<noteq> y \<longleftrightarrow> (\<exists>U. open U \<and> ~(x\<in>U \<longleftrightarrow> y\<in>U))"
hoelzl@51471
   143
  using t0_space[of x y] by blast
hoelzl@51471
   144
hoelzl@51471
   145
text {* A perfect space is a topological space with no isolated points. *}
hoelzl@51471
   146
hoelzl@51471
   147
class perfect_space = topological_space +
hoelzl@51471
   148
  assumes not_open_singleton: "\<not> open {x}"
hoelzl@51471
   149
hoelzl@51471
   150
hoelzl@51471
   151
subsection {* Generators for toplogies *}
hoelzl@51471
   152
hoelzl@51471
   153
inductive generate_topology for S where
hoelzl@51471
   154
  UNIV: "generate_topology S UNIV"
hoelzl@51471
   155
| Int: "generate_topology S a \<Longrightarrow> generate_topology S b \<Longrightarrow> generate_topology S (a \<inter> b)"
hoelzl@51471
   156
| UN: "(\<And>k. k \<in> K \<Longrightarrow> generate_topology S k) \<Longrightarrow> generate_topology S (\<Union>K)"
hoelzl@51471
   157
| Basis: "s \<in> S \<Longrightarrow> generate_topology S s"
hoelzl@51471
   158
hoelzl@51471
   159
hide_fact (open) UNIV Int UN Basis 
hoelzl@51471
   160
hoelzl@51471
   161
lemma generate_topology_Union: 
hoelzl@51471
   162
  "(\<And>k. k \<in> I \<Longrightarrow> generate_topology S (K k)) \<Longrightarrow> generate_topology S (\<Union>k\<in>I. K k)"
hoelzl@51471
   163
  unfolding SUP_def by (intro generate_topology.UN) auto
hoelzl@51471
   164
hoelzl@51471
   165
lemma topological_space_generate_topology:
hoelzl@51471
   166
  "class.topological_space (generate_topology S)"
hoelzl@51471
   167
  by default (auto intro: generate_topology.intros)
hoelzl@51471
   168
hoelzl@51471
   169
subsection {* Order topologies *}
hoelzl@51471
   170
hoelzl@51471
   171
class order_topology = order + "open" +
hoelzl@51471
   172
  assumes open_generated_order: "open = generate_topology (range (\<lambda>a. {..< a}) \<union> range (\<lambda>a. {a <..}))"
hoelzl@51471
   173
begin
hoelzl@51471
   174
hoelzl@51471
   175
subclass topological_space
hoelzl@51471
   176
  unfolding open_generated_order
hoelzl@51471
   177
  by (rule topological_space_generate_topology)
hoelzl@51471
   178
hoelzl@51471
   179
lemma open_greaterThan [simp]: "open {a <..}"
hoelzl@51471
   180
  unfolding open_generated_order by (auto intro: generate_topology.Basis)
hoelzl@51471
   181
hoelzl@51471
   182
lemma open_lessThan [simp]: "open {..< a}"
hoelzl@51471
   183
  unfolding open_generated_order by (auto intro: generate_topology.Basis)
hoelzl@51471
   184
hoelzl@51471
   185
lemma open_greaterThanLessThan [simp]: "open {a <..< b}"
hoelzl@51471
   186
   unfolding greaterThanLessThan_eq by (simp add: open_Int)
hoelzl@51471
   187
hoelzl@51471
   188
end
hoelzl@51471
   189
hoelzl@51471
   190
class linorder_topology = linorder + order_topology
hoelzl@51471
   191
hoelzl@51471
   192
lemma closed_atMost [simp]: "closed {.. a::'a::linorder_topology}"
hoelzl@51471
   193
  by (simp add: closed_open)
hoelzl@51471
   194
hoelzl@51471
   195
lemma closed_atLeast [simp]: "closed {a::'a::linorder_topology ..}"
hoelzl@51471
   196
  by (simp add: closed_open)
hoelzl@51471
   197
hoelzl@51471
   198
lemma closed_atLeastAtMost [simp]: "closed {a::'a::linorder_topology .. b}"
hoelzl@51471
   199
proof -
hoelzl@51471
   200
  have "{a .. b} = {a ..} \<inter> {.. b}"
hoelzl@51471
   201
    by auto
hoelzl@51471
   202
  then show ?thesis
hoelzl@51471
   203
    by (simp add: closed_Int)
hoelzl@51471
   204
qed
hoelzl@51471
   205
hoelzl@51471
   206
lemma (in linorder) less_separate:
hoelzl@51471
   207
  assumes "x < y"
hoelzl@51471
   208
  shows "\<exists>a b. x \<in> {..< a} \<and> y \<in> {b <..} \<and> {..< a} \<inter> {b <..} = {}"
hoelzl@51471
   209
proof cases
hoelzl@51471
   210
  assume "\<exists>z. x < z \<and> z < y"
hoelzl@51471
   211
  then guess z ..
hoelzl@51471
   212
  then have "x \<in> {..< z} \<and> y \<in> {z <..} \<and> {z <..} \<inter> {..< z} = {}"
hoelzl@51471
   213
    by auto
hoelzl@51471
   214
  then show ?thesis by blast
hoelzl@51471
   215
next
hoelzl@51471
   216
  assume "\<not> (\<exists>z. x < z \<and> z < y)"
hoelzl@51471
   217
  with `x < y` have "x \<in> {..< y} \<and> y \<in> {x <..} \<and> {x <..} \<inter> {..< y} = {}"
hoelzl@51471
   218
    by auto
hoelzl@51471
   219
  then show ?thesis by blast
hoelzl@51471
   220
qed
hoelzl@51471
   221
hoelzl@51471
   222
instance linorder_topology \<subseteq> t2_space
hoelzl@51471
   223
proof
hoelzl@51471
   224
  fix x y :: 'a
hoelzl@51471
   225
  from less_separate[of x y] less_separate[of y x]
hoelzl@51471
   226
  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 = {}"
hoelzl@51471
   227
    by (elim neqE) (metis open_lessThan open_greaterThan Int_commute)+
hoelzl@51471
   228
qed
hoelzl@51471
   229
hoelzl@51471
   230
lemma open_right:
hoelzl@51471
   231
  fixes S :: "'a :: {no_top, linorder_topology} set"
hoelzl@51471
   232
  assumes "open S" "x \<in> S" shows "\<exists>b>x. {x ..< b} \<subseteq> S"
hoelzl@51471
   233
  using assms unfolding open_generated_order
hoelzl@51471
   234
proof induction
hoelzl@51471
   235
  case (Int A B)
hoelzl@51471
   236
  then obtain a b where "a > x" "{x ..< a} \<subseteq> A"  "b > x" "{x ..< b} \<subseteq> B" by auto
hoelzl@51471
   237
  then show ?case by (auto intro!: exI[of _ "min a b"])
hoelzl@51471
   238
next
hoelzl@51471
   239
  case (Basis S)
hoelzl@51471
   240
  moreover from gt_ex[of x] guess b ..
hoelzl@51471
   241
  ultimately show ?case by (fastforce intro: exI[of _ b])
hoelzl@51471
   242
qed (fastforce intro: gt_ex)+
hoelzl@51471
   243
hoelzl@51471
   244
lemma open_left:
hoelzl@51471
   245
  fixes S :: "'a :: {no_bot, linorder_topology} set"
hoelzl@51471
   246
  assumes "open S" "x \<in> S" shows "\<exists>b<x. {b <.. x} \<subseteq> S"
hoelzl@51471
   247
  using assms unfolding open_generated_order
hoelzl@51471
   248
proof induction
hoelzl@51471
   249
  case (Int A B)
hoelzl@51471
   250
  then obtain a b where "a < x" "{a <.. x} \<subseteq> A"  "b < x" "{b <.. x} \<subseteq> B" by auto
hoelzl@51471
   251
  then show ?case by (auto intro!: exI[of _ "max a b"])
hoelzl@51471
   252
next
hoelzl@51471
   253
  case (Basis S)
hoelzl@51471
   254
  moreover from lt_ex[of x] guess b ..
hoelzl@51471
   255
  ultimately show ?case by (fastforce intro: exI[of _ b])
hoelzl@51471
   256
next
hoelzl@51471
   257
  case UN then show ?case by blast
hoelzl@51471
   258
qed (fastforce intro: lt_ex)
hoelzl@51471
   259
hoelzl@51471
   260
subsection {* Filters *}
hoelzl@51471
   261
hoelzl@51471
   262
text {*
hoelzl@51471
   263
  This definition also allows non-proper filters.
hoelzl@51471
   264
*}
hoelzl@51471
   265
hoelzl@51471
   266
locale is_filter =
hoelzl@51471
   267
  fixes F :: "('a \<Rightarrow> bool) \<Rightarrow> bool"
hoelzl@51471
   268
  assumes True: "F (\<lambda>x. True)"
hoelzl@51471
   269
  assumes conj: "F (\<lambda>x. P x) \<Longrightarrow> F (\<lambda>x. Q x) \<Longrightarrow> F (\<lambda>x. P x \<and> Q x)"
hoelzl@51471
   270
  assumes mono: "\<forall>x. P x \<longrightarrow> Q x \<Longrightarrow> F (\<lambda>x. P x) \<Longrightarrow> F (\<lambda>x. Q x)"
hoelzl@51471
   271
hoelzl@51471
   272
typedef 'a filter = "{F :: ('a \<Rightarrow> bool) \<Rightarrow> bool. is_filter F}"
hoelzl@51471
   273
proof
hoelzl@51471
   274
  show "(\<lambda>x. True) \<in> ?filter" by (auto intro: is_filter.intro)
hoelzl@51471
   275
qed
hoelzl@51471
   276
hoelzl@51471
   277
lemma is_filter_Rep_filter: "is_filter (Rep_filter F)"
hoelzl@51471
   278
  using Rep_filter [of F] by simp
hoelzl@51471
   279
hoelzl@51471
   280
lemma Abs_filter_inverse':
hoelzl@51471
   281
  assumes "is_filter F" shows "Rep_filter (Abs_filter F) = F"
hoelzl@51471
   282
  using assms by (simp add: Abs_filter_inverse)
hoelzl@51471
   283
hoelzl@51471
   284
hoelzl@51471
   285
subsubsection {* Eventually *}
hoelzl@51471
   286
hoelzl@51471
   287
definition eventually :: "('a \<Rightarrow> bool) \<Rightarrow> 'a filter \<Rightarrow> bool"
hoelzl@51471
   288
  where "eventually P F \<longleftrightarrow> Rep_filter F P"
hoelzl@51471
   289
hoelzl@51471
   290
lemma eventually_Abs_filter:
hoelzl@51471
   291
  assumes "is_filter F" shows "eventually P (Abs_filter F) = F P"
hoelzl@51471
   292
  unfolding eventually_def using assms by (simp add: Abs_filter_inverse)
hoelzl@51471
   293
hoelzl@51471
   294
lemma filter_eq_iff:
hoelzl@51471
   295
  shows "F = F' \<longleftrightarrow> (\<forall>P. eventually P F = eventually P F')"
hoelzl@51471
   296
  unfolding Rep_filter_inject [symmetric] fun_eq_iff eventually_def ..
hoelzl@51471
   297
hoelzl@51471
   298
lemma eventually_True [simp]: "eventually (\<lambda>x. True) F"
hoelzl@51471
   299
  unfolding eventually_def
hoelzl@51471
   300
  by (rule is_filter.True [OF is_filter_Rep_filter])
hoelzl@51471
   301
hoelzl@51471
   302
lemma always_eventually: "\<forall>x. P x \<Longrightarrow> eventually P F"
hoelzl@51471
   303
proof -
hoelzl@51471
   304
  assume "\<forall>x. P x" hence "P = (\<lambda>x. True)" by (simp add: ext)
hoelzl@51471
   305
  thus "eventually P F" by simp
hoelzl@51471
   306
qed
hoelzl@51471
   307
hoelzl@51471
   308
lemma eventually_mono:
hoelzl@51471
   309
  "(\<forall>x. P x \<longrightarrow> Q x) \<Longrightarrow> eventually P F \<Longrightarrow> eventually Q F"
hoelzl@51471
   310
  unfolding eventually_def
hoelzl@51471
   311
  by (rule is_filter.mono [OF is_filter_Rep_filter])
hoelzl@51471
   312
hoelzl@51471
   313
lemma eventually_conj:
hoelzl@51471
   314
  assumes P: "eventually (\<lambda>x. P x) F"
hoelzl@51471
   315
  assumes Q: "eventually (\<lambda>x. Q x) F"
hoelzl@51471
   316
  shows "eventually (\<lambda>x. P x \<and> Q x) F"
hoelzl@51471
   317
  using assms unfolding eventually_def
hoelzl@51471
   318
  by (rule is_filter.conj [OF is_filter_Rep_filter])
hoelzl@51471
   319
hoelzl@51471
   320
lemma eventually_Ball_finite:
hoelzl@51471
   321
  assumes "finite A" and "\<forall>y\<in>A. eventually (\<lambda>x. P x y) net"
hoelzl@51471
   322
  shows "eventually (\<lambda>x. \<forall>y\<in>A. P x y) net"
hoelzl@51471
   323
using assms by (induct set: finite, simp, simp add: eventually_conj)
hoelzl@51471
   324
hoelzl@51471
   325
lemma eventually_all_finite:
hoelzl@51471
   326
  fixes P :: "'a \<Rightarrow> 'b::finite \<Rightarrow> bool"
hoelzl@51471
   327
  assumes "\<And>y. eventually (\<lambda>x. P x y) net"
hoelzl@51471
   328
  shows "eventually (\<lambda>x. \<forall>y. P x y) net"
hoelzl@51471
   329
using eventually_Ball_finite [of UNIV P] assms by simp
hoelzl@51471
   330
hoelzl@51471
   331
lemma eventually_mp:
hoelzl@51471
   332
  assumes "eventually (\<lambda>x. P x \<longrightarrow> Q x) F"
hoelzl@51471
   333
  assumes "eventually (\<lambda>x. P x) F"
hoelzl@51471
   334
  shows "eventually (\<lambda>x. Q x) F"
hoelzl@51471
   335
proof (rule eventually_mono)
hoelzl@51471
   336
  show "\<forall>x. (P x \<longrightarrow> Q x) \<and> P x \<longrightarrow> Q x" by simp
hoelzl@51471
   337
  show "eventually (\<lambda>x. (P x \<longrightarrow> Q x) \<and> P x) F"
hoelzl@51471
   338
    using assms by (rule eventually_conj)
hoelzl@51471
   339
qed
hoelzl@51471
   340
hoelzl@51471
   341
lemma eventually_rev_mp:
hoelzl@51471
   342
  assumes "eventually (\<lambda>x. P x) F"
hoelzl@51471
   343
  assumes "eventually (\<lambda>x. P x \<longrightarrow> Q x) F"
hoelzl@51471
   344
  shows "eventually (\<lambda>x. Q x) F"
hoelzl@51471
   345
using assms(2) assms(1) by (rule eventually_mp)
hoelzl@51471
   346
hoelzl@51471
   347
lemma eventually_conj_iff:
hoelzl@51471
   348
  "eventually (\<lambda>x. P x \<and> Q x) F \<longleftrightarrow> eventually P F \<and> eventually Q F"
hoelzl@51471
   349
  by (auto intro: eventually_conj elim: eventually_rev_mp)
hoelzl@51471
   350
hoelzl@51471
   351
lemma eventually_elim1:
hoelzl@51471
   352
  assumes "eventually (\<lambda>i. P i) F"
hoelzl@51471
   353
  assumes "\<And>i. P i \<Longrightarrow> Q i"
hoelzl@51471
   354
  shows "eventually (\<lambda>i. Q i) F"
hoelzl@51471
   355
  using assms by (auto elim!: eventually_rev_mp)
hoelzl@51471
   356
hoelzl@51471
   357
lemma eventually_elim2:
hoelzl@51471
   358
  assumes "eventually (\<lambda>i. P i) F"
hoelzl@51471
   359
  assumes "eventually (\<lambda>i. Q i) F"
hoelzl@51471
   360
  assumes "\<And>i. P i \<Longrightarrow> Q i \<Longrightarrow> R i"
hoelzl@51471
   361
  shows "eventually (\<lambda>i. R i) F"
hoelzl@51471
   362
  using assms by (auto elim!: eventually_rev_mp)
hoelzl@51471
   363
hoelzl@51471
   364
lemma eventually_subst:
hoelzl@51471
   365
  assumes "eventually (\<lambda>n. P n = Q n) F"
hoelzl@51471
   366
  shows "eventually P F = eventually Q F" (is "?L = ?R")
hoelzl@51471
   367
proof -
hoelzl@51471
   368
  from assms have "eventually (\<lambda>x. P x \<longrightarrow> Q x) F"
hoelzl@51471
   369
      and "eventually (\<lambda>x. Q x \<longrightarrow> P x) F"
hoelzl@51471
   370
    by (auto elim: eventually_elim1)
hoelzl@51471
   371
  then show ?thesis by (auto elim: eventually_elim2)
hoelzl@51471
   372
qed
hoelzl@51471
   373
hoelzl@51471
   374
ML {*
hoelzl@51471
   375
  fun eventually_elim_tac ctxt thms thm =
hoelzl@51471
   376
    let
hoelzl@51471
   377
      val thy = Proof_Context.theory_of ctxt
hoelzl@51471
   378
      val mp_thms = thms RL [@{thm eventually_rev_mp}]
hoelzl@51471
   379
      val raw_elim_thm =
hoelzl@51471
   380
        (@{thm allI} RS @{thm always_eventually})
hoelzl@51471
   381
        |> fold (fn thm1 => fn thm2 => thm2 RS thm1) mp_thms
hoelzl@51471
   382
        |> fold (fn _ => fn thm => @{thm impI} RS thm) thms
hoelzl@51471
   383
      val cases_prop = prop_of (raw_elim_thm RS thm)
hoelzl@51471
   384
      val cases = (Rule_Cases.make_common (thy, cases_prop) [(("elim", []), [])])
hoelzl@51471
   385
    in
hoelzl@51471
   386
      CASES cases (rtac raw_elim_thm 1) thm
hoelzl@51471
   387
    end
hoelzl@51471
   388
*}
hoelzl@51471
   389
hoelzl@51471
   390
method_setup eventually_elim = {*
hoelzl@51471
   391
  Scan.succeed (fn ctxt => METHOD_CASES (eventually_elim_tac ctxt))
hoelzl@51471
   392
*} "elimination of eventually quantifiers"
hoelzl@51471
   393
hoelzl@51471
   394
hoelzl@51471
   395
subsubsection {* Finer-than relation *}
hoelzl@51471
   396
hoelzl@51471
   397
text {* @{term "F \<le> F'"} means that filter @{term F} is finer than
hoelzl@51471
   398
filter @{term F'}. *}
hoelzl@51471
   399
hoelzl@51471
   400
instantiation filter :: (type) complete_lattice
hoelzl@51471
   401
begin
hoelzl@51471
   402
hoelzl@51471
   403
definition le_filter_def:
hoelzl@51471
   404
  "F \<le> F' \<longleftrightarrow> (\<forall>P. eventually P F' \<longrightarrow> eventually P F)"
hoelzl@51471
   405
hoelzl@51471
   406
definition
hoelzl@51471
   407
  "(F :: 'a filter) < F' \<longleftrightarrow> F \<le> F' \<and> \<not> F' \<le> F"
hoelzl@51471
   408
hoelzl@51471
   409
definition
hoelzl@51471
   410
  "top = Abs_filter (\<lambda>P. \<forall>x. P x)"
hoelzl@51471
   411
hoelzl@51471
   412
definition
hoelzl@51471
   413
  "bot = Abs_filter (\<lambda>P. True)"
hoelzl@51471
   414
hoelzl@51471
   415
definition
hoelzl@51471
   416
  "sup F F' = Abs_filter (\<lambda>P. eventually P F \<and> eventually P F')"
hoelzl@51471
   417
hoelzl@51471
   418
definition
hoelzl@51471
   419
  "inf F F' = Abs_filter
hoelzl@51471
   420
      (\<lambda>P. \<exists>Q R. eventually Q F \<and> eventually R F' \<and> (\<forall>x. Q x \<and> R x \<longrightarrow> P x))"
hoelzl@51471
   421
hoelzl@51471
   422
definition
hoelzl@51471
   423
  "Sup S = Abs_filter (\<lambda>P. \<forall>F\<in>S. eventually P F)"
hoelzl@51471
   424
hoelzl@51471
   425
definition
hoelzl@51471
   426
  "Inf S = Sup {F::'a filter. \<forall>F'\<in>S. F \<le> F'}"
hoelzl@51471
   427
hoelzl@51471
   428
lemma eventually_top [simp]: "eventually P top \<longleftrightarrow> (\<forall>x. P x)"
hoelzl@51471
   429
  unfolding top_filter_def
hoelzl@51471
   430
  by (rule eventually_Abs_filter, rule is_filter.intro, auto)
hoelzl@51471
   431
hoelzl@51471
   432
lemma eventually_bot [simp]: "eventually P bot"
hoelzl@51471
   433
  unfolding bot_filter_def
hoelzl@51471
   434
  by (subst eventually_Abs_filter, rule is_filter.intro, auto)
hoelzl@51471
   435
hoelzl@51471
   436
lemma eventually_sup:
hoelzl@51471
   437
  "eventually P (sup F F') \<longleftrightarrow> eventually P F \<and> eventually P F'"
hoelzl@51471
   438
  unfolding sup_filter_def
hoelzl@51471
   439
  by (rule eventually_Abs_filter, rule is_filter.intro)
hoelzl@51471
   440
     (auto elim!: eventually_rev_mp)
hoelzl@51471
   441
hoelzl@51471
   442
lemma eventually_inf:
hoelzl@51471
   443
  "eventually P (inf F F') \<longleftrightarrow>
hoelzl@51471
   444
   (\<exists>Q R. eventually Q F \<and> eventually R F' \<and> (\<forall>x. Q x \<and> R x \<longrightarrow> P x))"
hoelzl@51471
   445
  unfolding inf_filter_def
hoelzl@51471
   446
  apply (rule eventually_Abs_filter, rule is_filter.intro)
hoelzl@51471
   447
  apply (fast intro: eventually_True)
hoelzl@51471
   448
  apply clarify
hoelzl@51471
   449
  apply (intro exI conjI)
hoelzl@51471
   450
  apply (erule (1) eventually_conj)
hoelzl@51471
   451
  apply (erule (1) eventually_conj)
hoelzl@51471
   452
  apply simp
hoelzl@51471
   453
  apply auto
hoelzl@51471
   454
  done
hoelzl@51471
   455
hoelzl@51471
   456
lemma eventually_Sup:
hoelzl@51471
   457
  "eventually P (Sup S) \<longleftrightarrow> (\<forall>F\<in>S. eventually P F)"
hoelzl@51471
   458
  unfolding Sup_filter_def
hoelzl@51471
   459
  apply (rule eventually_Abs_filter, rule is_filter.intro)
hoelzl@51471
   460
  apply (auto intro: eventually_conj elim!: eventually_rev_mp)
hoelzl@51471
   461
  done
hoelzl@51471
   462
hoelzl@51471
   463
instance proof
hoelzl@51471
   464
  fix F F' F'' :: "'a filter" and S :: "'a filter set"
hoelzl@51471
   465
  { show "F < F' \<longleftrightarrow> F \<le> F' \<and> \<not> F' \<le> F"
hoelzl@51471
   466
    by (rule less_filter_def) }
hoelzl@51471
   467
  { show "F \<le> F"
hoelzl@51471
   468
    unfolding le_filter_def by simp }
hoelzl@51471
   469
  { assume "F \<le> F'" and "F' \<le> F''" thus "F \<le> F''"
hoelzl@51471
   470
    unfolding le_filter_def by simp }
hoelzl@51471
   471
  { assume "F \<le> F'" and "F' \<le> F" thus "F = F'"
hoelzl@51471
   472
    unfolding le_filter_def filter_eq_iff by fast }
hoelzl@51471
   473
  { show "F \<le> top"
hoelzl@51471
   474
    unfolding le_filter_def eventually_top by (simp add: always_eventually) }
hoelzl@51471
   475
  { show "bot \<le> F"
hoelzl@51471
   476
    unfolding le_filter_def by simp }
hoelzl@51471
   477
  { show "F \<le> sup F F'" and "F' \<le> sup F F'"
hoelzl@51471
   478
    unfolding le_filter_def eventually_sup by simp_all }
hoelzl@51471
   479
  { assume "F \<le> F''" and "F' \<le> F''" thus "sup F F' \<le> F''"
hoelzl@51471
   480
    unfolding le_filter_def eventually_sup by simp }
hoelzl@51471
   481
  { show "inf F F' \<le> F" and "inf F F' \<le> F'"
hoelzl@51471
   482
    unfolding le_filter_def eventually_inf by (auto intro: eventually_True) }
hoelzl@51471
   483
  { assume "F \<le> F'" and "F \<le> F''" thus "F \<le> inf F' F''"
hoelzl@51471
   484
    unfolding le_filter_def eventually_inf
hoelzl@51471
   485
    by (auto elim!: eventually_mono intro: eventually_conj) }
hoelzl@51471
   486
  { assume "F \<in> S" thus "F \<le> Sup S"
hoelzl@51471
   487
    unfolding le_filter_def eventually_Sup by simp }
hoelzl@51471
   488
  { assume "\<And>F. F \<in> S \<Longrightarrow> F \<le> F'" thus "Sup S \<le> F'"
hoelzl@51471
   489
    unfolding le_filter_def eventually_Sup by simp }
hoelzl@51471
   490
  { assume "F'' \<in> S" thus "Inf S \<le> F''"
hoelzl@51471
   491
    unfolding le_filter_def Inf_filter_def eventually_Sup Ball_def by simp }
hoelzl@51471
   492
  { assume "\<And>F'. F' \<in> S \<Longrightarrow> F \<le> F'" thus "F \<le> Inf S"
hoelzl@51471
   493
    unfolding le_filter_def Inf_filter_def eventually_Sup Ball_def by simp }
hoelzl@51471
   494
qed
hoelzl@51471
   495
hoelzl@51471
   496
end
hoelzl@51471
   497
hoelzl@51471
   498
lemma filter_leD:
hoelzl@51471
   499
  "F \<le> F' \<Longrightarrow> eventually P F' \<Longrightarrow> eventually P F"
hoelzl@51471
   500
  unfolding le_filter_def by simp
hoelzl@51471
   501
hoelzl@51471
   502
lemma filter_leI:
hoelzl@51471
   503
  "(\<And>P. eventually P F' \<Longrightarrow> eventually P F) \<Longrightarrow> F \<le> F'"
hoelzl@51471
   504
  unfolding le_filter_def by simp
hoelzl@51471
   505
hoelzl@51471
   506
lemma eventually_False:
hoelzl@51471
   507
  "eventually (\<lambda>x. False) F \<longleftrightarrow> F = bot"
hoelzl@51471
   508
  unfolding filter_eq_iff by (auto elim: eventually_rev_mp)
hoelzl@51471
   509
hoelzl@51471
   510
abbreviation (input) trivial_limit :: "'a filter \<Rightarrow> bool"
hoelzl@51471
   511
  where "trivial_limit F \<equiv> F = bot"
hoelzl@51471
   512
hoelzl@51471
   513
lemma trivial_limit_def: "trivial_limit F \<longleftrightarrow> eventually (\<lambda>x. False) F"
hoelzl@51471
   514
  by (rule eventually_False [symmetric])
hoelzl@51471
   515
hoelzl@51471
   516
lemma eventually_const: "\<not> trivial_limit net \<Longrightarrow> eventually (\<lambda>x. P) net \<longleftrightarrow> P"
hoelzl@51471
   517
  by (cases P) (simp_all add: eventually_False)
hoelzl@51471
   518
hoelzl@51471
   519
hoelzl@51471
   520
subsubsection {* Map function for filters *}
hoelzl@51471
   521
hoelzl@51471
   522
definition filtermap :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a filter \<Rightarrow> 'b filter"
hoelzl@51471
   523
  where "filtermap f F = Abs_filter (\<lambda>P. eventually (\<lambda>x. P (f x)) F)"
hoelzl@51471
   524
hoelzl@51471
   525
lemma eventually_filtermap:
hoelzl@51471
   526
  "eventually P (filtermap f F) = eventually (\<lambda>x. P (f x)) F"
hoelzl@51471
   527
  unfolding filtermap_def
hoelzl@51471
   528
  apply (rule eventually_Abs_filter)
hoelzl@51471
   529
  apply (rule is_filter.intro)
hoelzl@51471
   530
  apply (auto elim!: eventually_rev_mp)
hoelzl@51471
   531
  done
hoelzl@51471
   532
hoelzl@51471
   533
lemma filtermap_ident: "filtermap (\<lambda>x. x) F = F"
hoelzl@51471
   534
  by (simp add: filter_eq_iff eventually_filtermap)
hoelzl@51471
   535
hoelzl@51471
   536
lemma filtermap_filtermap:
hoelzl@51471
   537
  "filtermap f (filtermap g F) = filtermap (\<lambda>x. f (g x)) F"
hoelzl@51471
   538
  by (simp add: filter_eq_iff eventually_filtermap)
hoelzl@51471
   539
hoelzl@51471
   540
lemma filtermap_mono: "F \<le> F' \<Longrightarrow> filtermap f F \<le> filtermap f F'"
hoelzl@51471
   541
  unfolding le_filter_def eventually_filtermap by simp
hoelzl@51471
   542
hoelzl@51471
   543
lemma filtermap_bot [simp]: "filtermap f bot = bot"
hoelzl@51471
   544
  by (simp add: filter_eq_iff eventually_filtermap)
hoelzl@51471
   545
hoelzl@51471
   546
lemma filtermap_sup: "filtermap f (sup F1 F2) = sup (filtermap f F1) (filtermap f F2)"
hoelzl@51471
   547
  by (auto simp: filter_eq_iff eventually_filtermap eventually_sup)
hoelzl@51471
   548
hoelzl@51471
   549
subsubsection {* Order filters *}
hoelzl@51471
   550
hoelzl@51471
   551
definition at_top :: "('a::order) filter"
hoelzl@51471
   552
  where "at_top = Abs_filter (\<lambda>P. \<exists>k. \<forall>n\<ge>k. P n)"
hoelzl@51471
   553
hoelzl@51471
   554
lemma eventually_at_top_linorder: "eventually P at_top \<longleftrightarrow> (\<exists>N::'a::linorder. \<forall>n\<ge>N. P n)"
hoelzl@51471
   555
  unfolding at_top_def
hoelzl@51471
   556
proof (rule eventually_Abs_filter, rule is_filter.intro)
hoelzl@51471
   557
  fix P Q :: "'a \<Rightarrow> bool"
hoelzl@51471
   558
  assume "\<exists>i. \<forall>n\<ge>i. P n" and "\<exists>j. \<forall>n\<ge>j. Q n"
hoelzl@51471
   559
  then obtain i j where "\<forall>n\<ge>i. P n" and "\<forall>n\<ge>j. Q n" by auto
hoelzl@51471
   560
  then have "\<forall>n\<ge>max i j. P n \<and> Q n" by simp
hoelzl@51471
   561
  then show "\<exists>k. \<forall>n\<ge>k. P n \<and> Q n" ..
hoelzl@51471
   562
qed auto
hoelzl@51471
   563
hoelzl@51471
   564
lemma eventually_ge_at_top:
hoelzl@51471
   565
  "eventually (\<lambda>x. (c::_::linorder) \<le> x) at_top"
hoelzl@51471
   566
  unfolding eventually_at_top_linorder by auto
hoelzl@51471
   567
hoelzl@51471
   568
lemma eventually_at_top_dense: "eventually P at_top \<longleftrightarrow> (\<exists>N::'a::dense_linorder. \<forall>n>N. P n)"
hoelzl@51471
   569
  unfolding eventually_at_top_linorder
hoelzl@51471
   570
proof safe
hoelzl@51471
   571
  fix N assume "\<forall>n\<ge>N. P n" then show "\<exists>N. \<forall>n>N. P n" by (auto intro!: exI[of _ N])
hoelzl@51471
   572
next
hoelzl@51471
   573
  fix N assume "\<forall>n>N. P n"
hoelzl@51471
   574
  moreover from gt_ex[of N] guess y ..
hoelzl@51471
   575
  ultimately show "\<exists>N. \<forall>n\<ge>N. P n" by (auto intro!: exI[of _ y])
hoelzl@51471
   576
qed
hoelzl@51471
   577
hoelzl@51471
   578
lemma eventually_gt_at_top:
hoelzl@51471
   579
  "eventually (\<lambda>x. (c::_::dense_linorder) < x) at_top"
hoelzl@51471
   580
  unfolding eventually_at_top_dense by auto
hoelzl@51471
   581
hoelzl@51471
   582
definition at_bot :: "('a::order) filter"
hoelzl@51471
   583
  where "at_bot = Abs_filter (\<lambda>P. \<exists>k. \<forall>n\<le>k. P n)"
hoelzl@51471
   584
hoelzl@51471
   585
lemma eventually_at_bot_linorder:
hoelzl@51471
   586
  fixes P :: "'a::linorder \<Rightarrow> bool" shows "eventually P at_bot \<longleftrightarrow> (\<exists>N. \<forall>n\<le>N. P n)"
hoelzl@51471
   587
  unfolding at_bot_def
hoelzl@51471
   588
proof (rule eventually_Abs_filter, rule is_filter.intro)
hoelzl@51471
   589
  fix P Q :: "'a \<Rightarrow> bool"
hoelzl@51471
   590
  assume "\<exists>i. \<forall>n\<le>i. P n" and "\<exists>j. \<forall>n\<le>j. Q n"
hoelzl@51471
   591
  then obtain i j where "\<forall>n\<le>i. P n" and "\<forall>n\<le>j. Q n" by auto
hoelzl@51471
   592
  then have "\<forall>n\<le>min i j. P n \<and> Q n" by simp
hoelzl@51471
   593
  then show "\<exists>k. \<forall>n\<le>k. P n \<and> Q n" ..
hoelzl@51471
   594
qed auto
hoelzl@51471
   595
hoelzl@51471
   596
lemma eventually_le_at_bot:
hoelzl@51471
   597
  "eventually (\<lambda>x. x \<le> (c::_::linorder)) at_bot"
hoelzl@51471
   598
  unfolding eventually_at_bot_linorder by auto
hoelzl@51471
   599
hoelzl@51471
   600
lemma eventually_at_bot_dense:
hoelzl@51471
   601
  fixes P :: "'a::dense_linorder \<Rightarrow> bool" shows "eventually P at_bot \<longleftrightarrow> (\<exists>N. \<forall>n<N. P n)"
hoelzl@51471
   602
  unfolding eventually_at_bot_linorder
hoelzl@51471
   603
proof safe
hoelzl@51471
   604
  fix N assume "\<forall>n\<le>N. P n" then show "\<exists>N. \<forall>n<N. P n" by (auto intro!: exI[of _ N])
hoelzl@51471
   605
next
hoelzl@51471
   606
  fix N assume "\<forall>n<N. P n" 
hoelzl@51471
   607
  moreover from lt_ex[of N] guess y ..
hoelzl@51471
   608
  ultimately show "\<exists>N. \<forall>n\<le>N. P n" by (auto intro!: exI[of _ y])
hoelzl@51471
   609
qed
hoelzl@51471
   610
hoelzl@51471
   611
lemma eventually_gt_at_bot:
hoelzl@51471
   612
  "eventually (\<lambda>x. x < (c::_::dense_linorder)) at_bot"
hoelzl@51471
   613
  unfolding eventually_at_bot_dense by auto
hoelzl@51471
   614
hoelzl@51471
   615
subsection {* Sequentially *}
hoelzl@51471
   616
hoelzl@51471
   617
abbreviation sequentially :: "nat filter"
hoelzl@51471
   618
  where "sequentially == at_top"
hoelzl@51471
   619
hoelzl@51471
   620
lemma sequentially_def: "sequentially = Abs_filter (\<lambda>P. \<exists>k. \<forall>n\<ge>k. P n)"
hoelzl@51471
   621
  unfolding at_top_def by simp
hoelzl@51471
   622
hoelzl@51471
   623
lemma eventually_sequentially:
hoelzl@51471
   624
  "eventually P sequentially \<longleftrightarrow> (\<exists>N. \<forall>n\<ge>N. P n)"
hoelzl@51471
   625
  by (rule eventually_at_top_linorder)
hoelzl@51471
   626
hoelzl@51471
   627
lemma sequentially_bot [simp, intro]: "sequentially \<noteq> bot"
hoelzl@51471
   628
  unfolding filter_eq_iff eventually_sequentially by auto
hoelzl@51471
   629
hoelzl@51471
   630
lemmas trivial_limit_sequentially = sequentially_bot
hoelzl@51471
   631
hoelzl@51471
   632
lemma eventually_False_sequentially [simp]:
hoelzl@51471
   633
  "\<not> eventually (\<lambda>n. False) sequentially"
hoelzl@51471
   634
  by (simp add: eventually_False)
hoelzl@51471
   635
hoelzl@51471
   636
lemma le_sequentially:
hoelzl@51471
   637
  "F \<le> sequentially \<longleftrightarrow> (\<forall>N. eventually (\<lambda>n. N \<le> n) F)"
hoelzl@51471
   638
  unfolding le_filter_def eventually_sequentially
hoelzl@51471
   639
  by (safe, fast, drule_tac x=N in spec, auto elim: eventually_rev_mp)
hoelzl@51471
   640
hoelzl@51471
   641
lemma eventually_sequentiallyI:
hoelzl@51471
   642
  assumes "\<And>x. c \<le> x \<Longrightarrow> P x"
hoelzl@51471
   643
  shows "eventually P sequentially"
hoelzl@51471
   644
using assms by (auto simp: eventually_sequentially)
hoelzl@51471
   645
hoelzl@51471
   646
hoelzl@51471
   647
subsubsection {* Standard filters *}
hoelzl@51471
   648
hoelzl@51471
   649
definition within :: "'a filter \<Rightarrow> 'a set \<Rightarrow> 'a filter" (infixr "within" 70)
hoelzl@51471
   650
  where "F within S = Abs_filter (\<lambda>P. eventually (\<lambda>x. x \<in> S \<longrightarrow> P x) F)"
hoelzl@51471
   651
hoelzl@51471
   652
lemma eventually_within:
hoelzl@51471
   653
  "eventually P (F within S) = eventually (\<lambda>x. x \<in> S \<longrightarrow> P x) F"
hoelzl@51471
   654
  unfolding within_def
hoelzl@51471
   655
  by (rule eventually_Abs_filter, rule is_filter.intro)
hoelzl@51471
   656
     (auto elim!: eventually_rev_mp)
hoelzl@51471
   657
hoelzl@51471
   658
lemma within_UNIV [simp]: "F within UNIV = F"
hoelzl@51471
   659
  unfolding filter_eq_iff eventually_within by simp
hoelzl@51471
   660
hoelzl@51471
   661
lemma within_empty [simp]: "F within {} = bot"
hoelzl@51471
   662
  unfolding filter_eq_iff eventually_within by simp
hoelzl@51471
   663
hoelzl@51471
   664
lemma within_within_eq: "(F within S) within T = F within (S \<inter> T)"
hoelzl@51471
   665
  by (auto simp: filter_eq_iff eventually_within elim: eventually_elim1)
hoelzl@51471
   666
hoelzl@51471
   667
lemma within_le: "F within S \<le> F"
hoelzl@51471
   668
  unfolding le_filter_def eventually_within by (auto elim: eventually_elim1)
hoelzl@51471
   669
hoelzl@51471
   670
lemma le_withinI: "F \<le> F' \<Longrightarrow> eventually (\<lambda>x. x \<in> S) F \<Longrightarrow> F \<le> F' within S"
hoelzl@51471
   671
  unfolding le_filter_def eventually_within by (auto elim: eventually_elim2)
hoelzl@51471
   672
hoelzl@51471
   673
lemma le_within_iff: "eventually (\<lambda>x. x \<in> S) F \<Longrightarrow> F \<le> F' within S \<longleftrightarrow> F \<le> F'"
hoelzl@51471
   674
  by (blast intro: within_le le_withinI order_trans)
hoelzl@51471
   675
hoelzl@51471
   676
subsubsection {* Topological filters *}
hoelzl@51471
   677
hoelzl@51471
   678
definition (in topological_space) nhds :: "'a \<Rightarrow> 'a filter"
hoelzl@51471
   679
  where "nhds a = Abs_filter (\<lambda>P. \<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x))"
hoelzl@51471
   680
hoelzl@51471
   681
definition (in topological_space) at :: "'a \<Rightarrow> 'a filter"
hoelzl@51471
   682
  where "at a = nhds a within - {a}"
hoelzl@51471
   683
hoelzl@51471
   684
abbreviation at_right :: "'a\<Colon>{topological_space, order} \<Rightarrow> 'a filter" where
hoelzl@51471
   685
  "at_right x \<equiv> at x within {x <..}"
hoelzl@51471
   686
hoelzl@51471
   687
abbreviation at_left :: "'a\<Colon>{topological_space, order} \<Rightarrow> 'a filter" where
hoelzl@51471
   688
  "at_left x \<equiv> at x within {..< x}"
hoelzl@51471
   689
hoelzl@51471
   690
lemma eventually_nhds:
hoelzl@51471
   691
  "eventually P (nhds a) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x))"
hoelzl@51471
   692
  unfolding nhds_def
hoelzl@51471
   693
proof (rule eventually_Abs_filter, rule is_filter.intro)
hoelzl@51471
   694
  have "open (UNIV :: 'a :: topological_space set) \<and> a \<in> UNIV \<and> (\<forall>x\<in>UNIV. True)" by simp
hoelzl@51471
   695
  thus "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. True)" ..
hoelzl@51471
   696
next
hoelzl@51471
   697
  fix P Q
hoelzl@51471
   698
  assume "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x)"
hoelzl@51471
   699
     and "\<exists>T. open T \<and> a \<in> T \<and> (\<forall>x\<in>T. Q x)"
hoelzl@51471
   700
  then obtain S T where
hoelzl@51471
   701
    "open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x)"
hoelzl@51471
   702
    "open T \<and> a \<in> T \<and> (\<forall>x\<in>T. Q x)" by auto
hoelzl@51471
   703
  hence "open (S \<inter> T) \<and> a \<in> S \<inter> T \<and> (\<forall>x\<in>(S \<inter> T). P x \<and> Q x)"
hoelzl@51471
   704
    by (simp add: open_Int)
hoelzl@51471
   705
  thus "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x \<and> Q x)" ..
hoelzl@51471
   706
qed auto
hoelzl@51471
   707
hoelzl@51471
   708
lemma nhds_neq_bot [simp]: "nhds a \<noteq> bot"
hoelzl@51471
   709
  unfolding trivial_limit_def eventually_nhds by simp
hoelzl@51471
   710
hoelzl@51471
   711
lemma eventually_at_topological:
hoelzl@51471
   712
  "eventually P (at a) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. x \<noteq> a \<longrightarrow> P x))"
hoelzl@51471
   713
unfolding at_def eventually_within eventually_nhds by simp
hoelzl@51471
   714
hoelzl@51471
   715
lemma at_eq_bot_iff: "at a = bot \<longleftrightarrow> open {a}"
hoelzl@51471
   716
  unfolding trivial_limit_def eventually_at_topological
hoelzl@51471
   717
  by (safe, case_tac "S = {a}", simp, fast, fast)
hoelzl@51471
   718
hoelzl@51471
   719
lemma at_neq_bot [simp]: "at (a::'a::perfect_space) \<noteq> bot"
hoelzl@51471
   720
  by (simp add: at_eq_bot_iff not_open_singleton)
hoelzl@51471
   721
hoelzl@51471
   722
lemma eventually_at_right:
hoelzl@51471
   723
  fixes x :: "'a :: {no_top, linorder_topology}"
hoelzl@51471
   724
  shows "eventually P (at_right x) \<longleftrightarrow> (\<exists>b. x < b \<and> (\<forall>z. x < z \<and> z < b \<longrightarrow> P z))"
hoelzl@51471
   725
  unfolding eventually_nhds eventually_within at_def
hoelzl@51471
   726
proof safe
hoelzl@51471
   727
  fix S assume "open S" "x \<in> S"
hoelzl@51471
   728
  note open_right[OF this]
hoelzl@51471
   729
  moreover assume "\<forall>s\<in>S. s \<in> - {x} \<longrightarrow> s \<in> {x<..} \<longrightarrow> P s"
hoelzl@51471
   730
  ultimately show "\<exists>b>x. \<forall>z. x < z \<and> z < b \<longrightarrow> P z"
hoelzl@51471
   731
    by (auto simp: subset_eq Ball_def)
hoelzl@51471
   732
next
hoelzl@51471
   733
  fix b assume "x < b" "\<forall>z. x < z \<and> z < b \<longrightarrow> P z"
hoelzl@51471
   734
  then show "\<exists>S. open S \<and> x \<in> S \<and> (\<forall>xa\<in>S. xa \<in> - {x} \<longrightarrow> xa \<in> {x<..} \<longrightarrow> P xa)"
hoelzl@51471
   735
    by (intro exI[of _ "{..< b}"]) auto
hoelzl@51471
   736
qed
hoelzl@51471
   737
hoelzl@51471
   738
lemma eventually_at_left:
hoelzl@51471
   739
  fixes x :: "'a :: {no_bot, linorder_topology}"
hoelzl@51471
   740
  shows "eventually P (at_left x) \<longleftrightarrow> (\<exists>b. x > b \<and> (\<forall>z. b < z \<and> z < x \<longrightarrow> P z))"
hoelzl@51471
   741
  unfolding eventually_nhds eventually_within at_def
hoelzl@51471
   742
proof safe
hoelzl@51471
   743
  fix S assume "open S" "x \<in> S"
hoelzl@51471
   744
  note open_left[OF this]
hoelzl@51471
   745
  moreover assume "\<forall>s\<in>S. s \<in> - {x} \<longrightarrow> s \<in> {..<x} \<longrightarrow> P s"
hoelzl@51471
   746
  ultimately show "\<exists>b<x. \<forall>z. b < z \<and> z < x \<longrightarrow> P z"
hoelzl@51471
   747
    by (auto simp: subset_eq Ball_def)
hoelzl@51471
   748
next
hoelzl@51471
   749
  fix b assume "b < x" "\<forall>z. b < z \<and> z < x \<longrightarrow> P z"
hoelzl@51471
   750
  then show "\<exists>S. open S \<and> x \<in> S \<and> (\<forall>xa\<in>S. xa \<in> - {x} \<longrightarrow> xa \<in> {..<x} \<longrightarrow> P xa)"
hoelzl@51471
   751
    by (intro exI[of _ "{b <..}"]) auto
hoelzl@51471
   752
qed
hoelzl@51471
   753
hoelzl@51471
   754
lemma trivial_limit_at_left_real [simp]:
hoelzl@51471
   755
  "\<not> trivial_limit (at_left (x::'a::{no_bot, dense_linorder, linorder_topology}))"
hoelzl@51471
   756
  unfolding trivial_limit_def eventually_at_left by (auto dest: dense)
hoelzl@51471
   757
hoelzl@51471
   758
lemma trivial_limit_at_right_real [simp]:
hoelzl@51471
   759
  "\<not> trivial_limit (at_right (x::'a::{no_top, dense_linorder, linorder_topology}))"
hoelzl@51471
   760
  unfolding trivial_limit_def eventually_at_right by (auto dest: dense)
hoelzl@51471
   761
hoelzl@51471
   762
lemma at_within_eq: "at x within T = nhds x within (T - {x})"
hoelzl@51471
   763
  unfolding at_def within_within_eq by (simp add: ac_simps Diff_eq)
hoelzl@51471
   764
hoelzl@51471
   765
lemma at_eq_sup_left_right: "at (x::'a::linorder_topology) = sup (at_left x) (at_right x)"
hoelzl@51471
   766
  by (auto simp: eventually_within at_def filter_eq_iff eventually_sup 
hoelzl@51471
   767
           elim: eventually_elim2 eventually_elim1)
hoelzl@51471
   768
hoelzl@51471
   769
lemma eventually_at_split:
hoelzl@51471
   770
  "eventually P (at (x::'a::linorder_topology)) \<longleftrightarrow> eventually P (at_left x) \<and> eventually P (at_right x)"
hoelzl@51471
   771
  by (subst at_eq_sup_left_right) (simp add: eventually_sup)
hoelzl@51471
   772
hoelzl@51471
   773
subsection {* Limits *}
hoelzl@51471
   774
hoelzl@51471
   775
definition filterlim :: "('a \<Rightarrow> 'b) \<Rightarrow> 'b filter \<Rightarrow> 'a filter \<Rightarrow> bool" where
hoelzl@51471
   776
  "filterlim f F2 F1 \<longleftrightarrow> filtermap f F1 \<le> F2"
hoelzl@51471
   777
hoelzl@51471
   778
syntax
hoelzl@51471
   779
  "_LIM" :: "pttrns \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> bool" ("(3LIM (_)/ (_)./ (_) :> (_))" [1000, 10, 0, 10] 10)
hoelzl@51471
   780
hoelzl@51471
   781
translations
hoelzl@51471
   782
  "LIM x F1. f :> F2"   == "CONST filterlim (%x. f) F2 F1"
hoelzl@51471
   783
hoelzl@51471
   784
lemma filterlim_iff:
hoelzl@51471
   785
  "(LIM x F1. f x :> F2) \<longleftrightarrow> (\<forall>P. eventually P F2 \<longrightarrow> eventually (\<lambda>x. P (f x)) F1)"
hoelzl@51471
   786
  unfolding filterlim_def le_filter_def eventually_filtermap ..
hoelzl@51471
   787
hoelzl@51471
   788
lemma filterlim_compose:
hoelzl@51471
   789
  "filterlim g F3 F2 \<Longrightarrow> filterlim f F2 F1 \<Longrightarrow> filterlim (\<lambda>x. g (f x)) F3 F1"
hoelzl@51471
   790
  unfolding filterlim_def filtermap_filtermap[symmetric] by (metis filtermap_mono order_trans)
hoelzl@51471
   791
hoelzl@51471
   792
lemma filterlim_mono:
hoelzl@51471
   793
  "filterlim f F2 F1 \<Longrightarrow> F2 \<le> F2' \<Longrightarrow> F1' \<le> F1 \<Longrightarrow> filterlim f F2' F1'"
hoelzl@51471
   794
  unfolding filterlim_def by (metis filtermap_mono order_trans)
hoelzl@51471
   795
hoelzl@51471
   796
lemma filterlim_ident: "LIM x F. x :> F"
hoelzl@51471
   797
  by (simp add: filterlim_def filtermap_ident)
hoelzl@51471
   798
hoelzl@51471
   799
lemma filterlim_cong:
hoelzl@51471
   800
  "F1 = F1' \<Longrightarrow> F2 = F2' \<Longrightarrow> eventually (\<lambda>x. f x = g x) F2 \<Longrightarrow> filterlim f F1 F2 = filterlim g F1' F2'"
hoelzl@51471
   801
  by (auto simp: filterlim_def le_filter_def eventually_filtermap elim: eventually_elim2)
hoelzl@51471
   802
hoelzl@51471
   803
lemma filterlim_within:
hoelzl@51471
   804
  "(LIM x F1. f x :> F2 within S) \<longleftrightarrow> (eventually (\<lambda>x. f x \<in> S) F1 \<and> (LIM x F1. f x :> F2))"
hoelzl@51471
   805
  unfolding filterlim_def
hoelzl@51471
   806
proof safe
hoelzl@51471
   807
  assume "filtermap f F1 \<le> F2 within S" then show "eventually (\<lambda>x. f x \<in> S) F1"
hoelzl@51471
   808
    by (auto simp: le_filter_def eventually_filtermap eventually_within elim!: allE[of _ "\<lambda>x. x \<in> S"])
hoelzl@51471
   809
qed (auto intro: within_le order_trans simp: le_within_iff eventually_filtermap)
hoelzl@51471
   810
hoelzl@51471
   811
lemma filterlim_filtermap: "filterlim f F1 (filtermap g F2) = filterlim (\<lambda>x. f (g x)) F1 F2"
hoelzl@51471
   812
  unfolding filterlim_def filtermap_filtermap ..
hoelzl@51471
   813
hoelzl@51471
   814
lemma filterlim_sup:
hoelzl@51471
   815
  "filterlim f F F1 \<Longrightarrow> filterlim f F F2 \<Longrightarrow> filterlim f F (sup F1 F2)"
hoelzl@51471
   816
  unfolding filterlim_def filtermap_sup by auto
hoelzl@51471
   817
hoelzl@51471
   818
lemma filterlim_Suc: "filterlim Suc sequentially sequentially"
hoelzl@51471
   819
  by (simp add: filterlim_iff eventually_sequentially) (metis le_Suc_eq)
hoelzl@51471
   820
hoelzl@51471
   821
subsubsection {* Tendsto *}
hoelzl@51471
   822
hoelzl@51471
   823
abbreviation (in topological_space)
hoelzl@51471
   824
  tendsto :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b filter \<Rightarrow> bool" (infixr "--->" 55) where
hoelzl@51471
   825
  "(f ---> l) F \<equiv> filterlim f (nhds l) F"
hoelzl@51471
   826
hoelzl@51471
   827
lemma tendsto_eq_rhs: "(f ---> x) F \<Longrightarrow> x = y \<Longrightarrow> (f ---> y) F"
hoelzl@51471
   828
  by simp
hoelzl@51471
   829
hoelzl@51471
   830
ML {*
hoelzl@51471
   831
hoelzl@51471
   832
structure Tendsto_Intros = Named_Thms
hoelzl@51471
   833
(
hoelzl@51471
   834
  val name = @{binding tendsto_intros}
hoelzl@51471
   835
  val description = "introduction rules for tendsto"
hoelzl@51471
   836
)
hoelzl@51471
   837
hoelzl@51471
   838
*}
hoelzl@51471
   839
hoelzl@51471
   840
setup {*
hoelzl@51471
   841
  Tendsto_Intros.setup #>
hoelzl@51471
   842
  Global_Theory.add_thms_dynamic (@{binding tendsto_eq_intros},
hoelzl@51471
   843
    map (fn thm => @{thm tendsto_eq_rhs} OF [thm]) o Tendsto_Intros.get o Context.proof_of);
hoelzl@51471
   844
*}
hoelzl@51471
   845
hoelzl@51471
   846
lemma tendsto_def: "(f ---> l) F \<longleftrightarrow> (\<forall>S. open S \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) F)"
hoelzl@51471
   847
  unfolding filterlim_def
hoelzl@51471
   848
proof safe
hoelzl@51471
   849
  fix S assume "open S" "l \<in> S" "filtermap f F \<le> nhds l"
hoelzl@51471
   850
  then show "eventually (\<lambda>x. f x \<in> S) F"
hoelzl@51471
   851
    unfolding eventually_nhds eventually_filtermap le_filter_def
hoelzl@51471
   852
    by (auto elim!: allE[of _ "\<lambda>x. x \<in> S"] eventually_rev_mp)
hoelzl@51471
   853
qed (auto elim!: eventually_rev_mp simp: eventually_nhds eventually_filtermap le_filter_def)
hoelzl@51471
   854
hoelzl@51471
   855
lemma filterlim_at:
hoelzl@51471
   856
  "(LIM x F. f x :> at b) \<longleftrightarrow> (eventually (\<lambda>x. f x \<noteq> b) F \<and> (f ---> b) F)"
hoelzl@51471
   857
  by (simp add: at_def filterlim_within)
hoelzl@51471
   858
hoelzl@51471
   859
lemma tendsto_mono: "F \<le> F' \<Longrightarrow> (f ---> l) F' \<Longrightarrow> (f ---> l) F"
hoelzl@51471
   860
  unfolding tendsto_def le_filter_def by fast
hoelzl@51471
   861
hoelzl@51471
   862
lemma topological_tendstoI:
hoelzl@51471
   863
  "(\<And>S. open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F)
hoelzl@51471
   864
    \<Longrightarrow> (f ---> l) F"
hoelzl@51471
   865
  unfolding tendsto_def by auto
hoelzl@51471
   866
hoelzl@51471
   867
lemma topological_tendstoD:
hoelzl@51471
   868
  "(f ---> l) F \<Longrightarrow> open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F"
hoelzl@51471
   869
  unfolding tendsto_def by auto
hoelzl@51471
   870
hoelzl@51471
   871
lemma order_tendstoI:
hoelzl@51471
   872
  fixes y :: "_ :: order_topology"
hoelzl@51471
   873
  assumes "\<And>a. a < y \<Longrightarrow> eventually (\<lambda>x. a < f x) F"
hoelzl@51471
   874
  assumes "\<And>a. y < a \<Longrightarrow> eventually (\<lambda>x. f x < a) F"
hoelzl@51471
   875
  shows "(f ---> y) F"
hoelzl@51471
   876
proof (rule topological_tendstoI)
hoelzl@51471
   877
  fix S assume "open S" "y \<in> S"
hoelzl@51471
   878
  then show "eventually (\<lambda>x. f x \<in> S) F"
hoelzl@51471
   879
    unfolding open_generated_order
hoelzl@51471
   880
  proof induct
hoelzl@51471
   881
    case (UN K)
hoelzl@51471
   882
    then obtain k where "y \<in> k" "k \<in> K" by auto
hoelzl@51471
   883
    with UN(2)[of k] show ?case
hoelzl@51471
   884
      by (auto elim: eventually_elim1)
hoelzl@51471
   885
  qed (insert assms, auto elim: eventually_elim2)
hoelzl@51471
   886
qed
hoelzl@51471
   887
hoelzl@51471
   888
lemma order_tendstoD:
hoelzl@51471
   889
  fixes y :: "_ :: order_topology"
hoelzl@51471
   890
  assumes y: "(f ---> y) F"
hoelzl@51471
   891
  shows "a < y \<Longrightarrow> eventually (\<lambda>x. a < f x) F"
hoelzl@51471
   892
    and "y < a \<Longrightarrow> eventually (\<lambda>x. f x < a) F"
hoelzl@51471
   893
  using topological_tendstoD[OF y, of "{..< a}"] topological_tendstoD[OF y, of "{a <..}"] by auto
hoelzl@51471
   894
hoelzl@51471
   895
lemma order_tendsto_iff: 
hoelzl@51471
   896
  fixes f :: "_ \<Rightarrow> 'a :: order_topology"
hoelzl@51471
   897
  shows "(f ---> x) F \<longleftrightarrow>(\<forall>l<x. eventually (\<lambda>x. l < f x) F) \<and> (\<forall>u>x. eventually (\<lambda>x. f x < u) F)"
hoelzl@51471
   898
  by (metis order_tendstoI order_tendstoD)
hoelzl@51471
   899
hoelzl@51471
   900
lemma tendsto_bot [simp]: "(f ---> a) bot"
hoelzl@51471
   901
  unfolding tendsto_def by simp
hoelzl@51471
   902
hoelzl@51471
   903
lemma tendsto_ident_at [tendsto_intros]: "((\<lambda>x. x) ---> a) (at a)"
hoelzl@51471
   904
  unfolding tendsto_def eventually_at_topological by auto
hoelzl@51471
   905
hoelzl@51471
   906
lemma tendsto_ident_at_within [tendsto_intros]:
hoelzl@51471
   907
  "((\<lambda>x. x) ---> a) (at a within S)"
hoelzl@51471
   908
  unfolding tendsto_def eventually_within eventually_at_topological by auto
hoelzl@51471
   909
hoelzl@51471
   910
lemma tendsto_const [tendsto_intros]: "((\<lambda>x. k) ---> k) F"
hoelzl@51471
   911
  by (simp add: tendsto_def)
hoelzl@51471
   912
hoelzl@51471
   913
lemma tendsto_unique:
hoelzl@51471
   914
  fixes f :: "'a \<Rightarrow> 'b::t2_space"
hoelzl@51471
   915
  assumes "\<not> trivial_limit F" and "(f ---> a) F" and "(f ---> b) F"
hoelzl@51471
   916
  shows "a = b"
hoelzl@51471
   917
proof (rule ccontr)
hoelzl@51471
   918
  assume "a \<noteq> b"
hoelzl@51471
   919
  obtain U V where "open U" "open V" "a \<in> U" "b \<in> V" "U \<inter> V = {}"
hoelzl@51471
   920
    using hausdorff [OF `a \<noteq> b`] by fast
hoelzl@51471
   921
  have "eventually (\<lambda>x. f x \<in> U) F"
hoelzl@51471
   922
    using `(f ---> a) F` `open U` `a \<in> U` by (rule topological_tendstoD)
hoelzl@51471
   923
  moreover
hoelzl@51471
   924
  have "eventually (\<lambda>x. f x \<in> V) F"
hoelzl@51471
   925
    using `(f ---> b) F` `open V` `b \<in> V` by (rule topological_tendstoD)
hoelzl@51471
   926
  ultimately
hoelzl@51471
   927
  have "eventually (\<lambda>x. False) F"
hoelzl@51471
   928
  proof eventually_elim
hoelzl@51471
   929
    case (elim x)
hoelzl@51471
   930
    hence "f x \<in> U \<inter> V" by simp
hoelzl@51471
   931
    with `U \<inter> V = {}` show ?case by simp
hoelzl@51471
   932
  qed
hoelzl@51471
   933
  with `\<not> trivial_limit F` show "False"
hoelzl@51471
   934
    by (simp add: trivial_limit_def)
hoelzl@51471
   935
qed
hoelzl@51471
   936
hoelzl@51471
   937
lemma tendsto_const_iff:
hoelzl@51471
   938
  fixes a b :: "'a::t2_space"
hoelzl@51471
   939
  assumes "\<not> trivial_limit F" shows "((\<lambda>x. a) ---> b) F \<longleftrightarrow> a = b"
hoelzl@51471
   940
  by (safe intro!: tendsto_const tendsto_unique [OF assms tendsto_const])
hoelzl@51471
   941
hoelzl@51471
   942
lemma increasing_tendsto:
hoelzl@51471
   943
  fixes f :: "_ \<Rightarrow> 'a::order_topology"
hoelzl@51471
   944
  assumes bdd: "eventually (\<lambda>n. f n \<le> l) F"
hoelzl@51471
   945
      and en: "\<And>x. x < l \<Longrightarrow> eventually (\<lambda>n. x < f n) F"
hoelzl@51471
   946
  shows "(f ---> l) F"
hoelzl@51471
   947
  using assms by (intro order_tendstoI) (auto elim!: eventually_elim1)
hoelzl@51471
   948
hoelzl@51471
   949
lemma decreasing_tendsto:
hoelzl@51471
   950
  fixes f :: "_ \<Rightarrow> 'a::order_topology"
hoelzl@51471
   951
  assumes bdd: "eventually (\<lambda>n. l \<le> f n) F"
hoelzl@51471
   952
      and en: "\<And>x. l < x \<Longrightarrow> eventually (\<lambda>n. f n < x) F"
hoelzl@51471
   953
  shows "(f ---> l) F"
hoelzl@51471
   954
  using assms by (intro order_tendstoI) (auto elim!: eventually_elim1)
hoelzl@51471
   955
hoelzl@51471
   956
lemma tendsto_sandwich:
hoelzl@51471
   957
  fixes f g h :: "'a \<Rightarrow> 'b::order_topology"
hoelzl@51471
   958
  assumes ev: "eventually (\<lambda>n. f n \<le> g n) net" "eventually (\<lambda>n. g n \<le> h n) net"
hoelzl@51471
   959
  assumes lim: "(f ---> c) net" "(h ---> c) net"
hoelzl@51471
   960
  shows "(g ---> c) net"
hoelzl@51471
   961
proof (rule order_tendstoI)
hoelzl@51471
   962
  fix a show "a < c \<Longrightarrow> eventually (\<lambda>x. a < g x) net"
hoelzl@51471
   963
    using order_tendstoD[OF lim(1), of a] ev by (auto elim: eventually_elim2)
hoelzl@51471
   964
next
hoelzl@51471
   965
  fix a show "c < a \<Longrightarrow> eventually (\<lambda>x. g x < a) net"
hoelzl@51471
   966
    using order_tendstoD[OF lim(2), of a] ev by (auto elim: eventually_elim2)
hoelzl@51471
   967
qed
hoelzl@51471
   968
hoelzl@51471
   969
lemma tendsto_le:
hoelzl@51471
   970
  fixes f g :: "'a \<Rightarrow> 'b::linorder_topology"
hoelzl@51471
   971
  assumes F: "\<not> trivial_limit F"
hoelzl@51471
   972
  assumes x: "(f ---> x) F" and y: "(g ---> y) F"
hoelzl@51471
   973
  assumes ev: "eventually (\<lambda>x. g x \<le> f x) F"
hoelzl@51471
   974
  shows "y \<le> x"
hoelzl@51471
   975
proof (rule ccontr)
hoelzl@51471
   976
  assume "\<not> y \<le> x"
hoelzl@51471
   977
  with less_separate[of x y] obtain a b where xy: "x < a" "b < y" "{..<a} \<inter> {b<..} = {}"
hoelzl@51471
   978
    by (auto simp: not_le)
hoelzl@51471
   979
  then have "eventually (\<lambda>x. f x < a) F" "eventually (\<lambda>x. b < g x) F"
hoelzl@51471
   980
    using x y by (auto intro: order_tendstoD)
hoelzl@51471
   981
  with ev have "eventually (\<lambda>x. False) F"
hoelzl@51471
   982
    by eventually_elim (insert xy, fastforce)
hoelzl@51471
   983
  with F show False
hoelzl@51471
   984
    by (simp add: eventually_False)
hoelzl@51471
   985
qed
hoelzl@51471
   986
hoelzl@51471
   987
lemma tendsto_le_const:
hoelzl@51471
   988
  fixes f :: "'a \<Rightarrow> 'b::linorder_topology"
hoelzl@51471
   989
  assumes F: "\<not> trivial_limit F"
hoelzl@51471
   990
  assumes x: "(f ---> x) F" and a: "eventually (\<lambda>x. a \<le> f x) F"
hoelzl@51471
   991
  shows "a \<le> x"
hoelzl@51471
   992
  using F x tendsto_const a by (rule tendsto_le)
hoelzl@51471
   993
hoelzl@51471
   994
subsection {* Limits to @{const at_top} and @{const at_bot} *}
hoelzl@51471
   995
hoelzl@51471
   996
lemma filterlim_at_top:
hoelzl@51471
   997
  fixes f :: "'a \<Rightarrow> ('b::linorder)"
hoelzl@51471
   998
  shows "(LIM x F. f x :> at_top) \<longleftrightarrow> (\<forall>Z. eventually (\<lambda>x. Z \<le> f x) F)"
hoelzl@51471
   999
  by (auto simp: filterlim_iff eventually_at_top_linorder elim!: eventually_elim1)
hoelzl@51471
  1000
hoelzl@51471
  1001
lemma filterlim_at_top_dense:
hoelzl@51471
  1002
  fixes f :: "'a \<Rightarrow> ('b::dense_linorder)"
hoelzl@51471
  1003
  shows "(LIM x F. f x :> at_top) \<longleftrightarrow> (\<forall>Z. eventually (\<lambda>x. Z < f x) F)"
hoelzl@51471
  1004
  by (metis eventually_elim1[of _ F] eventually_gt_at_top order_less_imp_le
hoelzl@51471
  1005
            filterlim_at_top[of f F] filterlim_iff[of f at_top F])
hoelzl@51471
  1006
hoelzl@51471
  1007
lemma filterlim_at_top_ge:
hoelzl@51471
  1008
  fixes f :: "'a \<Rightarrow> ('b::linorder)" and c :: "'b"
hoelzl@51471
  1009
  shows "(LIM x F. f x :> at_top) \<longleftrightarrow> (\<forall>Z\<ge>c. eventually (\<lambda>x. Z \<le> f x) F)"
hoelzl@51471
  1010
  unfolding filterlim_at_top
hoelzl@51471
  1011
proof safe
hoelzl@51471
  1012
  fix Z assume *: "\<forall>Z\<ge>c. eventually (\<lambda>x. Z \<le> f x) F"
hoelzl@51471
  1013
  with *[THEN spec, of "max Z c"] show "eventually (\<lambda>x. Z \<le> f x) F"
hoelzl@51471
  1014
    by (auto elim!: eventually_elim1)
hoelzl@51471
  1015
qed simp
hoelzl@51471
  1016
hoelzl@51471
  1017
lemma filterlim_at_top_at_top:
hoelzl@51471
  1018
  fixes f :: "'a::linorder \<Rightarrow> 'b::linorder"
hoelzl@51471
  1019
  assumes mono: "\<And>x y. Q x \<Longrightarrow> Q y \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
hoelzl@51471
  1020
  assumes bij: "\<And>x. P x \<Longrightarrow> f (g x) = x" "\<And>x. P x \<Longrightarrow> Q (g x)"
hoelzl@51471
  1021
  assumes Q: "eventually Q at_top"
hoelzl@51471
  1022
  assumes P: "eventually P at_top"
hoelzl@51471
  1023
  shows "filterlim f at_top at_top"
hoelzl@51471
  1024
proof -
hoelzl@51471
  1025
  from P obtain x where x: "\<And>y. x \<le> y \<Longrightarrow> P y"
hoelzl@51471
  1026
    unfolding eventually_at_top_linorder by auto
hoelzl@51471
  1027
  show ?thesis
hoelzl@51471
  1028
  proof (intro filterlim_at_top_ge[THEN iffD2] allI impI)
hoelzl@51471
  1029
    fix z assume "x \<le> z"
hoelzl@51471
  1030
    with x have "P z" by auto
hoelzl@51471
  1031
    have "eventually (\<lambda>x. g z \<le> x) at_top"
hoelzl@51471
  1032
      by (rule eventually_ge_at_top)
hoelzl@51471
  1033
    with Q show "eventually (\<lambda>x. z \<le> f x) at_top"
hoelzl@51471
  1034
      by eventually_elim (metis mono bij `P z`)
hoelzl@51471
  1035
  qed
hoelzl@51471
  1036
qed
hoelzl@51471
  1037
hoelzl@51471
  1038
lemma filterlim_at_top_gt:
hoelzl@51471
  1039
  fixes f :: "'a \<Rightarrow> ('b::dense_linorder)" and c :: "'b"
hoelzl@51471
  1040
  shows "(LIM x F. f x :> at_top) \<longleftrightarrow> (\<forall>Z>c. eventually (\<lambda>x. Z \<le> f x) F)"
hoelzl@51471
  1041
  by (metis filterlim_at_top order_less_le_trans gt_ex filterlim_at_top_ge)
hoelzl@51471
  1042
hoelzl@51471
  1043
lemma filterlim_at_bot: 
hoelzl@51471
  1044
  fixes f :: "'a \<Rightarrow> ('b::linorder)"
hoelzl@51471
  1045
  shows "(LIM x F. f x :> at_bot) \<longleftrightarrow> (\<forall>Z. eventually (\<lambda>x. f x \<le> Z) F)"
hoelzl@51471
  1046
  by (auto simp: filterlim_iff eventually_at_bot_linorder elim!: eventually_elim1)
hoelzl@51471
  1047
hoelzl@51471
  1048
lemma filterlim_at_bot_le:
hoelzl@51471
  1049
  fixes f :: "'a \<Rightarrow> ('b::linorder)" and c :: "'b"
hoelzl@51471
  1050
  shows "(LIM x F. f x :> at_bot) \<longleftrightarrow> (\<forall>Z\<le>c. eventually (\<lambda>x. Z \<ge> f x) F)"
hoelzl@51471
  1051
  unfolding filterlim_at_bot
hoelzl@51471
  1052
proof safe
hoelzl@51471
  1053
  fix Z assume *: "\<forall>Z\<le>c. eventually (\<lambda>x. Z \<ge> f x) F"
hoelzl@51471
  1054
  with *[THEN spec, of "min Z c"] show "eventually (\<lambda>x. Z \<ge> f x) F"
hoelzl@51471
  1055
    by (auto elim!: eventually_elim1)
hoelzl@51471
  1056
qed simp
hoelzl@51471
  1057
hoelzl@51471
  1058
lemma filterlim_at_bot_lt:
hoelzl@51471
  1059
  fixes f :: "'a \<Rightarrow> ('b::dense_linorder)" and c :: "'b"
hoelzl@51471
  1060
  shows "(LIM x F. f x :> at_bot) \<longleftrightarrow> (\<forall>Z<c. eventually (\<lambda>x. Z \<ge> f x) F)"
hoelzl@51471
  1061
  by (metis filterlim_at_bot filterlim_at_bot_le lt_ex order_le_less_trans)
hoelzl@51471
  1062
hoelzl@51471
  1063
lemma filterlim_at_bot_at_right:
hoelzl@51471
  1064
  fixes f :: "'a::{no_top, linorder_topology} \<Rightarrow> 'b::linorder"
hoelzl@51471
  1065
  assumes mono: "\<And>x y. Q x \<Longrightarrow> Q y \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
hoelzl@51471
  1066
  assumes bij: "\<And>x. P x \<Longrightarrow> f (g x) = x" "\<And>x. P x \<Longrightarrow> Q (g x)"
hoelzl@51471
  1067
  assumes Q: "eventually Q (at_right a)" and bound: "\<And>b. Q b \<Longrightarrow> a < b"
hoelzl@51471
  1068
  assumes P: "eventually P at_bot"
hoelzl@51471
  1069
  shows "filterlim f at_bot (at_right a)"
hoelzl@51471
  1070
proof -
hoelzl@51471
  1071
  from P obtain x where x: "\<And>y. y \<le> x \<Longrightarrow> P y"
hoelzl@51471
  1072
    unfolding eventually_at_bot_linorder by auto
hoelzl@51471
  1073
  show ?thesis
hoelzl@51471
  1074
  proof (intro filterlim_at_bot_le[THEN iffD2] allI impI)
hoelzl@51471
  1075
    fix z assume "z \<le> x"
hoelzl@51471
  1076
    with x have "P z" by auto
hoelzl@51471
  1077
    have "eventually (\<lambda>x. x \<le> g z) (at_right a)"
hoelzl@51471
  1078
      using bound[OF bij(2)[OF `P z`]]
hoelzl@51471
  1079
      unfolding eventually_at_right by (auto intro!: exI[of _ "g z"])
hoelzl@51471
  1080
    with Q show "eventually (\<lambda>x. f x \<le> z) (at_right a)"
hoelzl@51471
  1081
      by eventually_elim (metis bij `P z` mono)
hoelzl@51471
  1082
  qed
hoelzl@51471
  1083
qed
hoelzl@51471
  1084
hoelzl@51471
  1085
lemma filterlim_at_top_at_left:
hoelzl@51471
  1086
  fixes f :: "'a::{no_bot, linorder_topology} \<Rightarrow> 'b::linorder"
hoelzl@51471
  1087
  assumes mono: "\<And>x y. Q x \<Longrightarrow> Q y \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
hoelzl@51471
  1088
  assumes bij: "\<And>x. P x \<Longrightarrow> f (g x) = x" "\<And>x. P x \<Longrightarrow> Q (g x)"
hoelzl@51471
  1089
  assumes Q: "eventually Q (at_left a)" and bound: "\<And>b. Q b \<Longrightarrow> b < a"
hoelzl@51471
  1090
  assumes P: "eventually P at_top"
hoelzl@51471
  1091
  shows "filterlim f at_top (at_left a)"
hoelzl@51471
  1092
proof -
hoelzl@51471
  1093
  from P obtain x where x: "\<And>y. x \<le> y \<Longrightarrow> P y"
hoelzl@51471
  1094
    unfolding eventually_at_top_linorder by auto
hoelzl@51471
  1095
  show ?thesis
hoelzl@51471
  1096
  proof (intro filterlim_at_top_ge[THEN iffD2] allI impI)
hoelzl@51471
  1097
    fix z assume "x \<le> z"
hoelzl@51471
  1098
    with x have "P z" by auto
hoelzl@51471
  1099
    have "eventually (\<lambda>x. g z \<le> x) (at_left a)"
hoelzl@51471
  1100
      using bound[OF bij(2)[OF `P z`]]
hoelzl@51471
  1101
      unfolding eventually_at_left by (auto intro!: exI[of _ "g z"])
hoelzl@51471
  1102
    with Q show "eventually (\<lambda>x. z \<le> f x) (at_left a)"
hoelzl@51471
  1103
      by eventually_elim (metis bij `P z` mono)
hoelzl@51471
  1104
  qed
hoelzl@51471
  1105
qed
hoelzl@51471
  1106
hoelzl@51471
  1107
lemma filterlim_split_at:
hoelzl@51471
  1108
  "filterlim f F (at_left x) \<Longrightarrow> filterlim f F (at_right x) \<Longrightarrow> filterlim f F (at (x::'a::linorder_topology))"
hoelzl@51471
  1109
  by (subst at_eq_sup_left_right) (rule filterlim_sup)
hoelzl@51471
  1110
hoelzl@51471
  1111
lemma filterlim_at_split:
hoelzl@51471
  1112
  "filterlim f F (at (x::'a::linorder_topology)) \<longleftrightarrow> filterlim f F (at_left x) \<and> filterlim f F (at_right x)"
hoelzl@51471
  1113
  by (subst at_eq_sup_left_right) (simp add: filterlim_def filtermap_sup)
hoelzl@51471
  1114
hoelzl@51471
  1115
hoelzl@51471
  1116
subsection {* Limits on sequences *}
hoelzl@51471
  1117
hoelzl@51471
  1118
abbreviation (in topological_space)
hoelzl@51471
  1119
  LIMSEQ :: "[nat \<Rightarrow> 'a, 'a] \<Rightarrow> bool"
hoelzl@51471
  1120
    ("((_)/ ----> (_))" [60, 60] 60) where
hoelzl@51471
  1121
  "X ----> L \<equiv> (X ---> L) sequentially"
hoelzl@51471
  1122
hoelzl@51471
  1123
definition
hoelzl@51471
  1124
  lim :: "(nat \<Rightarrow> 'a::t2_space) \<Rightarrow> 'a" where
hoelzl@51471
  1125
    --{*Standard definition of limit using choice operator*}
hoelzl@51471
  1126
  "lim X = (THE L. X ----> L)"
hoelzl@51471
  1127
hoelzl@51471
  1128
definition (in topological_space) convergent :: "(nat \<Rightarrow> 'a) \<Rightarrow> bool" where
hoelzl@51471
  1129
  "convergent X = (\<exists>L. X ----> L)"
hoelzl@51471
  1130
hoelzl@51471
  1131
subsubsection {* Monotone sequences and subsequences *}
hoelzl@51471
  1132
hoelzl@51471
  1133
definition
hoelzl@51471
  1134
  monoseq :: "(nat \<Rightarrow> 'a::order) \<Rightarrow> bool" where
hoelzl@51471
  1135
    --{*Definition of monotonicity.
hoelzl@51471
  1136
        The use of disjunction here complicates proofs considerably.
hoelzl@51471
  1137
        One alternative is to add a Boolean argument to indicate the direction.
hoelzl@51471
  1138
        Another is to develop the notions of increasing and decreasing first.*}
hoelzl@51471
  1139
  "monoseq X = ((\<forall>m. \<forall>n\<ge>m. X m \<le> X n) | (\<forall>m. \<forall>n\<ge>m. X n \<le> X m))"
hoelzl@51471
  1140
hoelzl@51471
  1141
definition
hoelzl@51471
  1142
  incseq :: "(nat \<Rightarrow> 'a::order) \<Rightarrow> bool" where
hoelzl@51471
  1143
    --{*Increasing sequence*}
hoelzl@51471
  1144
  "incseq X \<longleftrightarrow> (\<forall>m. \<forall>n\<ge>m. X m \<le> X n)"
hoelzl@51471
  1145
hoelzl@51471
  1146
definition
hoelzl@51471
  1147
  decseq :: "(nat \<Rightarrow> 'a::order) \<Rightarrow> bool" where
hoelzl@51471
  1148
    --{*Decreasing sequence*}
hoelzl@51471
  1149
  "decseq X \<longleftrightarrow> (\<forall>m. \<forall>n\<ge>m. X n \<le> X m)"
hoelzl@51471
  1150
hoelzl@51471
  1151
definition
hoelzl@51471
  1152
  subseq :: "(nat \<Rightarrow> nat) \<Rightarrow> bool" where
hoelzl@51471
  1153
    --{*Definition of subsequence*}
hoelzl@51471
  1154
  "subseq f \<longleftrightarrow> (\<forall>m. \<forall>n>m. f m < f n)"
hoelzl@51471
  1155
hoelzl@51471
  1156
lemma incseq_mono: "mono f \<longleftrightarrow> incseq f"
hoelzl@51471
  1157
  unfolding mono_def incseq_def by auto
hoelzl@51471
  1158
hoelzl@51471
  1159
lemma incseq_SucI:
hoelzl@51471
  1160
  "(\<And>n. X n \<le> X (Suc n)) \<Longrightarrow> incseq X"
hoelzl@51471
  1161
  using lift_Suc_mono_le[of X]
hoelzl@51471
  1162
  by (auto simp: incseq_def)
hoelzl@51471
  1163
hoelzl@51471
  1164
lemma incseqD: "\<And>i j. incseq f \<Longrightarrow> i \<le> j \<Longrightarrow> f i \<le> f j"
hoelzl@51471
  1165
  by (auto simp: incseq_def)
hoelzl@51471
  1166
hoelzl@51471
  1167
lemma incseq_SucD: "incseq A \<Longrightarrow> A i \<le> A (Suc i)"
hoelzl@51471
  1168
  using incseqD[of A i "Suc i"] by auto
hoelzl@51471
  1169
hoelzl@51471
  1170
lemma incseq_Suc_iff: "incseq f \<longleftrightarrow> (\<forall>n. f n \<le> f (Suc n))"
hoelzl@51471
  1171
  by (auto intro: incseq_SucI dest: incseq_SucD)
hoelzl@51471
  1172
hoelzl@51471
  1173
lemma incseq_const[simp, intro]: "incseq (\<lambda>x. k)"
hoelzl@51471
  1174
  unfolding incseq_def by auto
hoelzl@51471
  1175
hoelzl@51471
  1176
lemma decseq_SucI:
hoelzl@51471
  1177
  "(\<And>n. X (Suc n) \<le> X n) \<Longrightarrow> decseq X"
hoelzl@51471
  1178
  using order.lift_Suc_mono_le[OF dual_order, of X]
hoelzl@51471
  1179
  by (auto simp: decseq_def)
hoelzl@51471
  1180
hoelzl@51471
  1181
lemma decseqD: "\<And>i j. decseq f \<Longrightarrow> i \<le> j \<Longrightarrow> f j \<le> f i"
hoelzl@51471
  1182
  by (auto simp: decseq_def)
hoelzl@51471
  1183
hoelzl@51471
  1184
lemma decseq_SucD: "decseq A \<Longrightarrow> A (Suc i) \<le> A i"
hoelzl@51471
  1185
  using decseqD[of A i "Suc i"] by auto
hoelzl@51471
  1186
hoelzl@51471
  1187
lemma decseq_Suc_iff: "decseq f \<longleftrightarrow> (\<forall>n. f (Suc n) \<le> f n)"
hoelzl@51471
  1188
  by (auto intro: decseq_SucI dest: decseq_SucD)
hoelzl@51471
  1189
hoelzl@51471
  1190
lemma decseq_const[simp, intro]: "decseq (\<lambda>x. k)"
hoelzl@51471
  1191
  unfolding decseq_def by auto
hoelzl@51471
  1192
hoelzl@51471
  1193
lemma monoseq_iff: "monoseq X \<longleftrightarrow> incseq X \<or> decseq X"
hoelzl@51471
  1194
  unfolding monoseq_def incseq_def decseq_def ..
hoelzl@51471
  1195
hoelzl@51471
  1196
lemma monoseq_Suc:
hoelzl@51471
  1197
  "monoseq X \<longleftrightarrow> (\<forall>n. X n \<le> X (Suc n)) \<or> (\<forall>n. X (Suc n) \<le> X n)"
hoelzl@51471
  1198
  unfolding monoseq_iff incseq_Suc_iff decseq_Suc_iff ..
hoelzl@51471
  1199
hoelzl@51471
  1200
lemma monoI1: "\<forall>m. \<forall> n \<ge> m. X m \<le> X n ==> monoseq X"
hoelzl@51471
  1201
by (simp add: monoseq_def)
hoelzl@51471
  1202
hoelzl@51471
  1203
lemma monoI2: "\<forall>m. \<forall> n \<ge> m. X n \<le> X m ==> monoseq X"
hoelzl@51471
  1204
by (simp add: monoseq_def)
hoelzl@51471
  1205
hoelzl@51471
  1206
lemma mono_SucI1: "\<forall>n. X n \<le> X (Suc n) ==> monoseq X"
hoelzl@51471
  1207
by (simp add: monoseq_Suc)
hoelzl@51471
  1208
hoelzl@51471
  1209
lemma mono_SucI2: "\<forall>n. X (Suc n) \<le> X n ==> monoseq X"
hoelzl@51471
  1210
by (simp add: monoseq_Suc)
hoelzl@51471
  1211
hoelzl@51471
  1212
lemma monoseq_minus:
hoelzl@51471
  1213
  fixes a :: "nat \<Rightarrow> 'a::ordered_ab_group_add"
hoelzl@51471
  1214
  assumes "monoseq a"
hoelzl@51471
  1215
  shows "monoseq (\<lambda> n. - a n)"
hoelzl@51471
  1216
proof (cases "\<forall> m. \<forall> n \<ge> m. a m \<le> a n")
hoelzl@51471
  1217
  case True
hoelzl@51471
  1218
  hence "\<forall> m. \<forall> n \<ge> m. - a n \<le> - a m" by auto
hoelzl@51471
  1219
  thus ?thesis by (rule monoI2)
hoelzl@51471
  1220
next
hoelzl@51471
  1221
  case False
hoelzl@51471
  1222
  hence "\<forall> m. \<forall> n \<ge> m. - a m \<le> - a n" using `monoseq a`[unfolded monoseq_def] by auto
hoelzl@51471
  1223
  thus ?thesis by (rule monoI1)
hoelzl@51471
  1224
qed
hoelzl@51471
  1225
hoelzl@51471
  1226
text{*Subsequence (alternative definition, (e.g. Hoskins)*}
hoelzl@51471
  1227
hoelzl@51471
  1228
lemma subseq_Suc_iff: "subseq f = (\<forall>n. (f n) < (f (Suc n)))"
hoelzl@51471
  1229
apply (simp add: subseq_def)
hoelzl@51471
  1230
apply (auto dest!: less_imp_Suc_add)
hoelzl@51471
  1231
apply (induct_tac k)
hoelzl@51471
  1232
apply (auto intro: less_trans)
hoelzl@51471
  1233
done
hoelzl@51471
  1234
hoelzl@51471
  1235
text{* for any sequence, there is a monotonic subsequence *}
hoelzl@51471
  1236
lemma seq_monosub:
hoelzl@51471
  1237
  fixes s :: "nat => 'a::linorder"
hoelzl@51471
  1238
  shows "\<exists>f. subseq f \<and> monoseq (\<lambda> n. (s (f n)))"
hoelzl@51471
  1239
proof cases
hoelzl@51471
  1240
  let "?P p n" = "p > n \<and> (\<forall>m\<ge>p. s m \<le> s p)"
hoelzl@51471
  1241
  assume *: "\<forall>n. \<exists>p. ?P p n"
hoelzl@51471
  1242
  def f \<equiv> "nat_rec (SOME p. ?P p 0) (\<lambda>_ n. SOME p. ?P p n)"
hoelzl@51471
  1243
  have f_0: "f 0 = (SOME p. ?P p 0)" unfolding f_def by simp
hoelzl@51471
  1244
  have f_Suc: "\<And>i. f (Suc i) = (SOME p. ?P p (f i))" unfolding f_def nat_rec_Suc ..
hoelzl@51471
  1245
  have P_0: "?P (f 0) 0" unfolding f_0 using *[rule_format] by (rule someI2_ex) auto
hoelzl@51471
  1246
  have P_Suc: "\<And>i. ?P (f (Suc i)) (f i)" unfolding f_Suc using *[rule_format] by (rule someI2_ex) auto
hoelzl@51471
  1247
  then have "subseq f" unfolding subseq_Suc_iff by auto
hoelzl@51471
  1248
  moreover have "monoseq (\<lambda>n. s (f n))" unfolding monoseq_Suc
hoelzl@51471
  1249
  proof (intro disjI2 allI)
hoelzl@51471
  1250
    fix n show "s (f (Suc n)) \<le> s (f n)"
hoelzl@51471
  1251
    proof (cases n)
hoelzl@51471
  1252
      case 0 with P_Suc[of 0] P_0 show ?thesis by auto
hoelzl@51471
  1253
    next
hoelzl@51471
  1254
      case (Suc m)
hoelzl@51471
  1255
      from P_Suc[of n] Suc have "f (Suc m) \<le> f (Suc (Suc m))" by simp
hoelzl@51471
  1256
      with P_Suc Suc show ?thesis by simp
hoelzl@51471
  1257
    qed
hoelzl@51471
  1258
  qed
hoelzl@51471
  1259
  ultimately show ?thesis by auto
hoelzl@51471
  1260
next
hoelzl@51471
  1261
  let "?P p m" = "m < p \<and> s m < s p"
hoelzl@51471
  1262
  assume "\<not> (\<forall>n. \<exists>p>n. (\<forall>m\<ge>p. s m \<le> s p))"
hoelzl@51471
  1263
  then obtain N where N: "\<And>p. p > N \<Longrightarrow> \<exists>m>p. s p < s m" by (force simp: not_le le_less)
hoelzl@51471
  1264
  def f \<equiv> "nat_rec (SOME p. ?P p (Suc N)) (\<lambda>_ n. SOME p. ?P p n)"
hoelzl@51471
  1265
  have f_0: "f 0 = (SOME p. ?P p (Suc N))" unfolding f_def by simp
hoelzl@51471
  1266
  have f_Suc: "\<And>i. f (Suc i) = (SOME p. ?P p (f i))" unfolding f_def nat_rec_Suc ..
hoelzl@51471
  1267
  have P_0: "?P (f 0) (Suc N)"
hoelzl@51471
  1268
    unfolding f_0 some_eq_ex[of "\<lambda>p. ?P p (Suc N)"] using N[of "Suc N"] by auto
hoelzl@51471
  1269
  { fix i have "N < f i \<Longrightarrow> ?P (f (Suc i)) (f i)"
hoelzl@51471
  1270
      unfolding f_Suc some_eq_ex[of "\<lambda>p. ?P p (f i)"] using N[of "f i"] . }
hoelzl@51471
  1271
  note P' = this
hoelzl@51471
  1272
  { fix i have "N < f i \<and> ?P (f (Suc i)) (f i)"
hoelzl@51471
  1273
      by (induct i) (insert P_0 P', auto) }
hoelzl@51471
  1274
  then have "subseq f" "monoseq (\<lambda>x. s (f x))"
hoelzl@51471
  1275
    unfolding subseq_Suc_iff monoseq_Suc by (auto simp: not_le intro: less_imp_le)
hoelzl@51471
  1276
  then show ?thesis by auto
hoelzl@51471
  1277
qed
hoelzl@51471
  1278
hoelzl@51471
  1279
lemma seq_suble: assumes sf: "subseq f" shows "n \<le> f n"
hoelzl@51471
  1280
proof(induct n)
hoelzl@51471
  1281
  case 0 thus ?case by simp
hoelzl@51471
  1282
next
hoelzl@51471
  1283
  case (Suc n)
hoelzl@51471
  1284
  from sf[unfolded subseq_Suc_iff, rule_format, of n] Suc.hyps
hoelzl@51471
  1285
  have "n < f (Suc n)" by arith
hoelzl@51471
  1286
  thus ?case by arith
hoelzl@51471
  1287
qed
hoelzl@51471
  1288
hoelzl@51471
  1289
lemma eventually_subseq:
hoelzl@51471
  1290
  "subseq r \<Longrightarrow> eventually P sequentially \<Longrightarrow> eventually (\<lambda>n. P (r n)) sequentially"
hoelzl@51471
  1291
  unfolding eventually_sequentially by (metis seq_suble le_trans)
hoelzl@51471
  1292
hoelzl@51471
  1293
lemma filterlim_subseq: "subseq f \<Longrightarrow> filterlim f sequentially sequentially"
hoelzl@51471
  1294
  unfolding filterlim_iff by (metis eventually_subseq)
hoelzl@51471
  1295
hoelzl@51471
  1296
lemma subseq_o: "subseq r \<Longrightarrow> subseq s \<Longrightarrow> subseq (r \<circ> s)"
hoelzl@51471
  1297
  unfolding subseq_def by simp
hoelzl@51471
  1298
hoelzl@51471
  1299
lemma subseq_mono: assumes "subseq r" "m < n" shows "r m < r n"
hoelzl@51471
  1300
  using assms by (auto simp: subseq_def)
hoelzl@51471
  1301
hoelzl@51471
  1302
lemma incseq_imp_monoseq:  "incseq X \<Longrightarrow> monoseq X"
hoelzl@51471
  1303
  by (simp add: incseq_def monoseq_def)
hoelzl@51471
  1304
hoelzl@51471
  1305
lemma decseq_imp_monoseq:  "decseq X \<Longrightarrow> monoseq X"
hoelzl@51471
  1306
  by (simp add: decseq_def monoseq_def)
hoelzl@51471
  1307
hoelzl@51471
  1308
lemma decseq_eq_incseq:
hoelzl@51471
  1309
  fixes X :: "nat \<Rightarrow> 'a::ordered_ab_group_add" shows "decseq X = incseq (\<lambda>n. - X n)" 
hoelzl@51471
  1310
  by (simp add: decseq_def incseq_def)
hoelzl@51471
  1311
hoelzl@51471
  1312
lemma INT_decseq_offset:
hoelzl@51471
  1313
  assumes "decseq F"
hoelzl@51471
  1314
  shows "(\<Inter>i. F i) = (\<Inter>i\<in>{n..}. F i)"
hoelzl@51471
  1315
proof safe
hoelzl@51471
  1316
  fix x i assume x: "x \<in> (\<Inter>i\<in>{n..}. F i)"
hoelzl@51471
  1317
  show "x \<in> F i"
hoelzl@51471
  1318
  proof cases
hoelzl@51471
  1319
    from x have "x \<in> F n" by auto
hoelzl@51471
  1320
    also assume "i \<le> n" with `decseq F` have "F n \<subseteq> F i"
hoelzl@51471
  1321
      unfolding decseq_def by simp
hoelzl@51471
  1322
    finally show ?thesis .
hoelzl@51471
  1323
  qed (insert x, simp)
hoelzl@51471
  1324
qed auto
hoelzl@51471
  1325
hoelzl@51471
  1326
lemma LIMSEQ_const_iff:
hoelzl@51471
  1327
  fixes k l :: "'a::t2_space"
hoelzl@51471
  1328
  shows "(\<lambda>n. k) ----> l \<longleftrightarrow> k = l"
hoelzl@51471
  1329
  using trivial_limit_sequentially by (rule tendsto_const_iff)
hoelzl@51471
  1330
hoelzl@51471
  1331
lemma LIMSEQ_SUP:
hoelzl@51471
  1332
  "incseq X \<Longrightarrow> X ----> (SUP i. X i :: 'a :: {complete_linorder, linorder_topology})"
hoelzl@51471
  1333
  by (intro increasing_tendsto)
hoelzl@51471
  1334
     (auto simp: SUP_upper less_SUP_iff incseq_def eventually_sequentially intro: less_le_trans)
hoelzl@51471
  1335
hoelzl@51471
  1336
lemma LIMSEQ_INF:
hoelzl@51471
  1337
  "decseq X \<Longrightarrow> X ----> (INF i. X i :: 'a :: {complete_linorder, linorder_topology})"
hoelzl@51471
  1338
  by (intro decreasing_tendsto)
hoelzl@51471
  1339
     (auto simp: INF_lower INF_less_iff decseq_def eventually_sequentially intro: le_less_trans)
hoelzl@51471
  1340
hoelzl@51471
  1341
lemma LIMSEQ_ignore_initial_segment:
hoelzl@51471
  1342
  "f ----> a \<Longrightarrow> (\<lambda>n. f (n + k)) ----> a"
hoelzl@51471
  1343
apply (rule topological_tendstoI)
hoelzl@51471
  1344
apply (drule (2) topological_tendstoD)
hoelzl@51471
  1345
apply (simp only: eventually_sequentially)
hoelzl@51471
  1346
apply (erule exE, rename_tac N)
hoelzl@51471
  1347
apply (rule_tac x=N in exI)
hoelzl@51471
  1348
apply simp
hoelzl@51471
  1349
done
hoelzl@51471
  1350
hoelzl@51471
  1351
lemma LIMSEQ_offset:
hoelzl@51471
  1352
  "(\<lambda>n. f (n + k)) ----> a \<Longrightarrow> f ----> a"
hoelzl@51471
  1353
apply (rule topological_tendstoI)
hoelzl@51471
  1354
apply (drule (2) topological_tendstoD)
hoelzl@51471
  1355
apply (simp only: eventually_sequentially)
hoelzl@51471
  1356
apply (erule exE, rename_tac N)
hoelzl@51471
  1357
apply (rule_tac x="N + k" in exI)
hoelzl@51471
  1358
apply clarify
hoelzl@51471
  1359
apply (drule_tac x="n - k" in spec)
hoelzl@51471
  1360
apply (simp add: le_diff_conv2)
hoelzl@51471
  1361
done
hoelzl@51471
  1362
hoelzl@51471
  1363
lemma LIMSEQ_Suc: "f ----> l \<Longrightarrow> (\<lambda>n. f (Suc n)) ----> l"
hoelzl@51471
  1364
by (drule_tac k="Suc 0" in LIMSEQ_ignore_initial_segment, simp)
hoelzl@51471
  1365
hoelzl@51471
  1366
lemma LIMSEQ_imp_Suc: "(\<lambda>n. f (Suc n)) ----> l \<Longrightarrow> f ----> l"
hoelzl@51471
  1367
by (rule_tac k="Suc 0" in LIMSEQ_offset, simp)
hoelzl@51471
  1368
hoelzl@51471
  1369
lemma LIMSEQ_Suc_iff: "(\<lambda>n. f (Suc n)) ----> l = f ----> l"
hoelzl@51471
  1370
by (blast intro: LIMSEQ_imp_Suc LIMSEQ_Suc)
hoelzl@51471
  1371
hoelzl@51471
  1372
lemma LIMSEQ_unique:
hoelzl@51471
  1373
  fixes a b :: "'a::t2_space"
hoelzl@51471
  1374
  shows "\<lbrakk>X ----> a; X ----> b\<rbrakk> \<Longrightarrow> a = b"
hoelzl@51471
  1375
  using trivial_limit_sequentially by (rule tendsto_unique)
hoelzl@51471
  1376
hoelzl@51471
  1377
lemma LIMSEQ_le_const:
hoelzl@51471
  1378
  "\<lbrakk>X ----> (x::'a::linorder_topology); \<exists>N. \<forall>n\<ge>N. a \<le> X n\<rbrakk> \<Longrightarrow> a \<le> x"
hoelzl@51471
  1379
  using tendsto_le_const[of sequentially X x a] by (simp add: eventually_sequentially)
hoelzl@51471
  1380
hoelzl@51471
  1381
lemma LIMSEQ_le:
hoelzl@51471
  1382
  "\<lbrakk>X ----> x; Y ----> y; \<exists>N. \<forall>n\<ge>N. X n \<le> Y n\<rbrakk> \<Longrightarrow> x \<le> (y::'a::linorder_topology)"
hoelzl@51471
  1383
  using tendsto_le[of sequentially Y y X x] by (simp add: eventually_sequentially)
hoelzl@51471
  1384
hoelzl@51471
  1385
lemma LIMSEQ_le_const2:
hoelzl@51471
  1386
  "\<lbrakk>X ----> (x::'a::linorder_topology); \<exists>N. \<forall>n\<ge>N. X n \<le> a\<rbrakk> \<Longrightarrow> x \<le> a"
hoelzl@51471
  1387
  by (rule LIMSEQ_le[of X x "\<lambda>n. a"]) (auto simp: tendsto_const)
hoelzl@51471
  1388
hoelzl@51471
  1389
lemma convergentD: "convergent X ==> \<exists>L. (X ----> L)"
hoelzl@51471
  1390
by (simp add: convergent_def)
hoelzl@51471
  1391
hoelzl@51471
  1392
lemma convergentI: "(X ----> L) ==> convergent X"
hoelzl@51471
  1393
by (auto simp add: convergent_def)
hoelzl@51471
  1394
hoelzl@51471
  1395
lemma convergent_LIMSEQ_iff: "convergent X = (X ----> lim X)"
hoelzl@51471
  1396
by (auto intro: theI LIMSEQ_unique simp add: convergent_def lim_def)
hoelzl@51471
  1397
hoelzl@51471
  1398
lemma convergent_const: "convergent (\<lambda>n. c)"
hoelzl@51471
  1399
  by (rule convergentI, rule tendsto_const)
hoelzl@51471
  1400
hoelzl@51471
  1401
lemma monoseq_le:
hoelzl@51471
  1402
  "monoseq a \<Longrightarrow> a ----> (x::'a::linorder_topology) \<Longrightarrow>
hoelzl@51471
  1403
    ((\<forall> n. a n \<le> x) \<and> (\<forall>m. \<forall>n\<ge>m. a m \<le> a n)) \<or> ((\<forall> n. x \<le> a n) \<and> (\<forall>m. \<forall>n\<ge>m. a n \<le> a m))"
hoelzl@51471
  1404
  by (metis LIMSEQ_le_const LIMSEQ_le_const2 decseq_def incseq_def monoseq_iff)
hoelzl@51471
  1405
hoelzl@51471
  1406
lemma LIMSEQ_subseq_LIMSEQ:
hoelzl@51471
  1407
  "\<lbrakk> X ----> L; subseq f \<rbrakk> \<Longrightarrow> (X o f) ----> L"
hoelzl@51471
  1408
  unfolding comp_def by (rule filterlim_compose[of X, OF _ filterlim_subseq])
hoelzl@51471
  1409
hoelzl@51471
  1410
lemma convergent_subseq_convergent:
hoelzl@51471
  1411
  "\<lbrakk>convergent X; subseq f\<rbrakk> \<Longrightarrow> convergent (X o f)"
hoelzl@51471
  1412
  unfolding convergent_def by (auto intro: LIMSEQ_subseq_LIMSEQ)
hoelzl@51471
  1413
hoelzl@51471
  1414
lemma limI: "X ----> L ==> lim X = L"
hoelzl@51471
  1415
apply (simp add: lim_def)
hoelzl@51471
  1416
apply (blast intro: LIMSEQ_unique)
hoelzl@51471
  1417
done
hoelzl@51471
  1418
hoelzl@51471
  1419
lemma lim_le: "convergent f \<Longrightarrow> (\<And>n. f n \<le> (x::'a::linorder_topology)) \<Longrightarrow> lim f \<le> x"
hoelzl@51471
  1420
  using LIMSEQ_le_const2[of f "lim f" x] by (simp add: convergent_LIMSEQ_iff)
hoelzl@51471
  1421
hoelzl@51471
  1422
subsubsection{*Increasing and Decreasing Series*}
hoelzl@51471
  1423
hoelzl@51471
  1424
lemma incseq_le: "incseq X \<Longrightarrow> X ----> L \<Longrightarrow> X n \<le> (L::'a::linorder_topology)"
hoelzl@51471
  1425
  by (metis incseq_def LIMSEQ_le_const)
hoelzl@51471
  1426
hoelzl@51471
  1427
lemma decseq_le: "decseq X \<Longrightarrow> X ----> L \<Longrightarrow> (L::'a::linorder_topology) \<le> X n"
hoelzl@51471
  1428
  by (metis decseq_def LIMSEQ_le_const2)
hoelzl@51471
  1429
hoelzl@51471
  1430
subsection {* Function limit at a point *}
hoelzl@51471
  1431
hoelzl@51471
  1432
abbreviation
hoelzl@51471
  1433
  LIM :: "('a::topological_space \<Rightarrow> 'b::topological_space) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool"
hoelzl@51471
  1434
        ("((_)/ -- (_)/ --> (_))" [60, 0, 60] 60) where
hoelzl@51471
  1435
  "f -- a --> L \<equiv> (f ---> L) (at a)"
hoelzl@51471
  1436
hoelzl@51471
  1437
lemma LIM_const_not_eq[tendsto_intros]:
hoelzl@51471
  1438
  fixes a :: "'a::perfect_space"
hoelzl@51471
  1439
  fixes k L :: "'b::t2_space"
hoelzl@51471
  1440
  shows "k \<noteq> L \<Longrightarrow> \<not> (\<lambda>x. k) -- a --> L"
hoelzl@51471
  1441
  by (simp add: tendsto_const_iff)
hoelzl@51471
  1442
hoelzl@51471
  1443
lemmas LIM_not_zero = LIM_const_not_eq [where L = 0]
hoelzl@51471
  1444
hoelzl@51471
  1445
lemma LIM_const_eq:
hoelzl@51471
  1446
  fixes a :: "'a::perfect_space"
hoelzl@51471
  1447
  fixes k L :: "'b::t2_space"
hoelzl@51471
  1448
  shows "(\<lambda>x. k) -- a --> L \<Longrightarrow> k = L"
hoelzl@51471
  1449
  by (simp add: tendsto_const_iff)
hoelzl@51471
  1450
hoelzl@51471
  1451
lemma LIM_unique:
hoelzl@51471
  1452
  fixes a :: "'a::perfect_space" and L M :: "'b::t2_space"
hoelzl@51471
  1453
  shows "f -- a --> L \<Longrightarrow> f -- a --> M \<Longrightarrow> L = M"
hoelzl@51471
  1454
  using at_neq_bot by (rule tendsto_unique)
hoelzl@51471
  1455
hoelzl@51471
  1456
text {* Limits are equal for functions equal except at limit point *}
hoelzl@51471
  1457
hoelzl@51471
  1458
lemma LIM_equal: "\<forall>x. x \<noteq> a --> (f x = g x) \<Longrightarrow> (f -- a --> l) \<longleftrightarrow> (g -- a --> l)"
hoelzl@51471
  1459
  unfolding tendsto_def eventually_at_topological by simp
hoelzl@51471
  1460
hoelzl@51471
  1461
lemma LIM_cong: "a = b \<Longrightarrow> (\<And>x. x \<noteq> b \<Longrightarrow> f x = g x) \<Longrightarrow> l = m \<Longrightarrow> (f -- a --> l) \<longleftrightarrow> (g -- b --> m)"
hoelzl@51471
  1462
  by (simp add: LIM_equal)
hoelzl@51471
  1463
hoelzl@51471
  1464
lemma LIM_cong_limit: "f -- x --> L \<Longrightarrow> K = L \<Longrightarrow> f -- x --> K"
hoelzl@51471
  1465
  by simp
hoelzl@51471
  1466
hoelzl@51471
  1467
lemma tendsto_at_iff_tendsto_nhds:
hoelzl@51471
  1468
  "g -- l --> g l \<longleftrightarrow> (g ---> g l) (nhds l)"
hoelzl@51471
  1469
  unfolding tendsto_def at_def eventually_within
hoelzl@51471
  1470
  by (intro ext all_cong imp_cong) (auto elim!: eventually_elim1)
hoelzl@51471
  1471
hoelzl@51471
  1472
lemma tendsto_compose:
hoelzl@51471
  1473
  "g -- l --> g l \<Longrightarrow> (f ---> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) ---> g l) F"
hoelzl@51471
  1474
  unfolding tendsto_at_iff_tendsto_nhds by (rule filterlim_compose[of g])
hoelzl@51471
  1475
hoelzl@51471
  1476
lemma LIM_o: "\<lbrakk>g -- l --> g l; f -- a --> l\<rbrakk> \<Longrightarrow> (g \<circ> f) -- a --> g l"
hoelzl@51471
  1477
  unfolding o_def by (rule tendsto_compose)
hoelzl@51471
  1478
hoelzl@51471
  1479
lemma tendsto_compose_eventually:
hoelzl@51471
  1480
  "g -- l --> m \<Longrightarrow> (f ---> l) F \<Longrightarrow> eventually (\<lambda>x. f x \<noteq> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) ---> m) F"
hoelzl@51471
  1481
  by (rule filterlim_compose[of g _ "at l"]) (auto simp add: filterlim_at)
hoelzl@51471
  1482
hoelzl@51471
  1483
lemma LIM_compose_eventually:
hoelzl@51471
  1484
  assumes f: "f -- a --> b"
hoelzl@51471
  1485
  assumes g: "g -- b --> c"
hoelzl@51471
  1486
  assumes inj: "eventually (\<lambda>x. f x \<noteq> b) (at a)"
hoelzl@51471
  1487
  shows "(\<lambda>x. g (f x)) -- a --> c"
hoelzl@51471
  1488
  using g f inj by (rule tendsto_compose_eventually)
hoelzl@51471
  1489
hoelzl@51471
  1490
subsection {* Continuity *}
hoelzl@51471
  1491
hoelzl@51471
  1492
definition isCont :: "('a::topological_space \<Rightarrow> 'b::topological_space) \<Rightarrow> 'a \<Rightarrow> bool" where
hoelzl@51471
  1493
  "isCont f a \<longleftrightarrow> f -- a --> f a"
hoelzl@51471
  1494
hoelzl@51471
  1495
lemma isCont_ident [simp]: "isCont (\<lambda>x. x) a"
hoelzl@51471
  1496
  unfolding isCont_def by (rule tendsto_ident_at)
hoelzl@51471
  1497
hoelzl@51471
  1498
lemma isCont_const [simp]: "isCont (\<lambda>x. k) a"
hoelzl@51471
  1499
  unfolding isCont_def by (rule tendsto_const)
hoelzl@51471
  1500
hoelzl@51471
  1501
lemma isCont_tendsto_compose: "isCont g l \<Longrightarrow> (f ---> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) ---> g l) F"
hoelzl@51471
  1502
  unfolding isCont_def by (rule tendsto_compose)
hoelzl@51471
  1503
hoelzl@51471
  1504
lemma isCont_o2: "isCont f a \<Longrightarrow> isCont g (f a) \<Longrightarrow> isCont (\<lambda>x. g (f x)) a"
hoelzl@51471
  1505
  unfolding isCont_def by (rule tendsto_compose)
hoelzl@51471
  1506
hoelzl@51471
  1507
lemma isCont_o: "isCont f a \<Longrightarrow> isCont g (f a) \<Longrightarrow> isCont (g o f) a"
hoelzl@51471
  1508
  unfolding o_def by (rule isCont_o2)
hoelzl@51471
  1509
hoelzl@51471
  1510
end
hoelzl@51471
  1511