src/HOL/Metric_Spaces.thy
author hoelzl
Fri Mar 22 10:41:43 2013 +0100 (2013-03-22)
changeset 51474 1e9e68247ad1
parent 51473 1210309fddab
child 51478 270b21f3ae0a
permissions -rw-r--r--
generalize Bfun and Bseq to metric spaces; Bseq is an abbreviation for Bfun
hoelzl@51472
     1
(*  Title:      HOL/Metric_Spaces.thy
hoelzl@51472
     2
    Author:     Brian Huffman
hoelzl@51472
     3
    Author:     Johannes Hölzl
hoelzl@51472
     4
*)
hoelzl@51472
     5
hoelzl@51472
     6
header {* Metric Spaces *}
hoelzl@51472
     7
hoelzl@51472
     8
theory Metric_Spaces
hoelzl@51472
     9
imports RComplete Topological_Spaces
hoelzl@51472
    10
begin
hoelzl@51472
    11
hoelzl@51472
    12
hoelzl@51472
    13
subsection {* Metric spaces *}
hoelzl@51472
    14
hoelzl@51472
    15
class dist =
hoelzl@51472
    16
  fixes dist :: "'a \<Rightarrow> 'a \<Rightarrow> real"
hoelzl@51472
    17
hoelzl@51472
    18
class open_dist = "open" + dist +
hoelzl@51472
    19
  assumes open_dist: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
hoelzl@51472
    20
hoelzl@51472
    21
class metric_space = open_dist +
hoelzl@51472
    22
  assumes dist_eq_0_iff [simp]: "dist x y = 0 \<longleftrightarrow> x = y"
hoelzl@51472
    23
  assumes dist_triangle2: "dist x y \<le> dist x z + dist y z"
hoelzl@51472
    24
begin
hoelzl@51472
    25
hoelzl@51472
    26
lemma dist_self [simp]: "dist x x = 0"
hoelzl@51472
    27
by simp
hoelzl@51472
    28
hoelzl@51472
    29
lemma zero_le_dist [simp]: "0 \<le> dist x y"
hoelzl@51472
    30
using dist_triangle2 [of x x y] by simp
hoelzl@51472
    31
hoelzl@51472
    32
lemma zero_less_dist_iff: "0 < dist x y \<longleftrightarrow> x \<noteq> y"
hoelzl@51472
    33
by (simp add: less_le)
hoelzl@51472
    34
hoelzl@51472
    35
lemma dist_not_less_zero [simp]: "\<not> dist x y < 0"
hoelzl@51472
    36
by (simp add: not_less)
hoelzl@51472
    37
hoelzl@51472
    38
lemma dist_le_zero_iff [simp]: "dist x y \<le> 0 \<longleftrightarrow> x = y"
hoelzl@51472
    39
by (simp add: le_less)
hoelzl@51472
    40
hoelzl@51472
    41
lemma dist_commute: "dist x y = dist y x"
hoelzl@51472
    42
proof (rule order_antisym)
hoelzl@51472
    43
  show "dist x y \<le> dist y x"
hoelzl@51472
    44
    using dist_triangle2 [of x y x] by simp
hoelzl@51472
    45
  show "dist y x \<le> dist x y"
hoelzl@51472
    46
    using dist_triangle2 [of y x y] by simp
hoelzl@51472
    47
qed
hoelzl@51472
    48
hoelzl@51472
    49
lemma dist_triangle: "dist x z \<le> dist x y + dist y z"
hoelzl@51472
    50
using dist_triangle2 [of x z y] by (simp add: dist_commute)
hoelzl@51472
    51
hoelzl@51472
    52
lemma dist_triangle3: "dist x y \<le> dist a x + dist a y"
hoelzl@51472
    53
using dist_triangle2 [of x y a] by (simp add: dist_commute)
hoelzl@51472
    54
hoelzl@51472
    55
lemma dist_triangle_alt:
hoelzl@51472
    56
  shows "dist y z <= dist x y + dist x z"
hoelzl@51472
    57
by (rule dist_triangle3)
hoelzl@51472
    58
hoelzl@51472
    59
lemma dist_pos_lt:
hoelzl@51472
    60
  shows "x \<noteq> y ==> 0 < dist x y"
hoelzl@51472
    61
by (simp add: zero_less_dist_iff)
hoelzl@51472
    62
hoelzl@51472
    63
lemma dist_nz:
hoelzl@51472
    64
  shows "x \<noteq> y \<longleftrightarrow> 0 < dist x y"
hoelzl@51472
    65
by (simp add: zero_less_dist_iff)
hoelzl@51472
    66
hoelzl@51472
    67
lemma dist_triangle_le:
hoelzl@51472
    68
  shows "dist x z + dist y z <= e \<Longrightarrow> dist x y <= e"
hoelzl@51472
    69
by (rule order_trans [OF dist_triangle2])
hoelzl@51472
    70
hoelzl@51472
    71
lemma dist_triangle_lt:
hoelzl@51472
    72
  shows "dist x z + dist y z < e ==> dist x y < e"
hoelzl@51472
    73
by (rule le_less_trans [OF dist_triangle2])
hoelzl@51472
    74
hoelzl@51472
    75
lemma dist_triangle_half_l:
hoelzl@51472
    76
  shows "dist x1 y < e / 2 \<Longrightarrow> dist x2 y < e / 2 \<Longrightarrow> dist x1 x2 < e"
hoelzl@51472
    77
by (rule dist_triangle_lt [where z=y], simp)
hoelzl@51472
    78
hoelzl@51472
    79
lemma dist_triangle_half_r:
hoelzl@51472
    80
  shows "dist y x1 < e / 2 \<Longrightarrow> dist y x2 < e / 2 \<Longrightarrow> dist x1 x2 < e"
hoelzl@51472
    81
by (rule dist_triangle_half_l, simp_all add: dist_commute)
hoelzl@51472
    82
hoelzl@51472
    83
subclass topological_space
hoelzl@51472
    84
proof
hoelzl@51472
    85
  have "\<exists>e::real. 0 < e"
hoelzl@51472
    86
    by (fast intro: zero_less_one)
hoelzl@51472
    87
  then show "open UNIV"
hoelzl@51472
    88
    unfolding open_dist by simp
hoelzl@51472
    89
next
hoelzl@51472
    90
  fix S T assume "open S" "open T"
hoelzl@51472
    91
  then show "open (S \<inter> T)"
hoelzl@51472
    92
    unfolding open_dist
hoelzl@51472
    93
    apply clarify
hoelzl@51472
    94
    apply (drule (1) bspec)+
hoelzl@51472
    95
    apply (clarify, rename_tac r s)
hoelzl@51472
    96
    apply (rule_tac x="min r s" in exI, simp)
hoelzl@51472
    97
    done
hoelzl@51472
    98
next
hoelzl@51472
    99
  fix K assume "\<forall>S\<in>K. open S" thus "open (\<Union>K)"
hoelzl@51472
   100
    unfolding open_dist by fast
hoelzl@51472
   101
qed
hoelzl@51472
   102
hoelzl@51473
   103
lemma open_ball: "open {y. dist x y < d}"
hoelzl@51472
   104
proof (unfold open_dist, intro ballI)
hoelzl@51472
   105
  fix y assume *: "y \<in> {y. dist x y < d}"
hoelzl@51472
   106
  then show "\<exists>e>0. \<forall>z. dist z y < e \<longrightarrow> z \<in> {y. dist x y < d}"
hoelzl@51472
   107
    by (auto intro!: exI[of _ "d - dist x y"] simp: field_simps dist_triangle_lt)
hoelzl@51472
   108
qed
hoelzl@51472
   109
hoelzl@51473
   110
subclass first_countable_topology
hoelzl@51473
   111
proof
hoelzl@51473
   112
  fix x 
hoelzl@51473
   113
  show "\<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
   114
  proof (safe intro!: exI[of _ "\<lambda>n. {y. dist x y < inverse (Suc n)}"])
hoelzl@51473
   115
    fix S assume "open S" "x \<in> S"
hoelzl@51473
   116
    then obtain e where "0 < e" "{y. dist x y < e} \<subseteq> S"
hoelzl@51473
   117
      by (auto simp: open_dist subset_eq dist_commute)
hoelzl@51473
   118
    moreover
hoelzl@51473
   119
    then obtain i where "inverse (Suc i) < e"
hoelzl@51473
   120
      by (auto dest!: reals_Archimedean)
hoelzl@51473
   121
    then have "{y. dist x y < inverse (Suc i)} \<subseteq> {y. dist x y < e}"
hoelzl@51473
   122
      by auto
hoelzl@51473
   123
    ultimately show "\<exists>i. {y. dist x y < inverse (Suc i)} \<subseteq> S"
hoelzl@51473
   124
      by blast
hoelzl@51473
   125
  qed (auto intro: open_ball)
hoelzl@51473
   126
qed
hoelzl@51473
   127
hoelzl@51472
   128
end
hoelzl@51472
   129
hoelzl@51472
   130
instance metric_space \<subseteq> t2_space
hoelzl@51472
   131
proof
hoelzl@51472
   132
  fix x y :: "'a::metric_space"
hoelzl@51472
   133
  assume xy: "x \<noteq> y"
hoelzl@51472
   134
  let ?U = "{y'. dist x y' < dist x y / 2}"
hoelzl@51472
   135
  let ?V = "{x'. dist y x' < dist x y / 2}"
hoelzl@51472
   136
  have th0: "\<And>d x y z. (d x z :: real) \<le> d x y + d y z \<Longrightarrow> d y z = d z y
hoelzl@51472
   137
               \<Longrightarrow> \<not>(d x y * 2 < d x z \<and> d z y * 2 < d x z)" by arith
hoelzl@51472
   138
  have "open ?U \<and> open ?V \<and> x \<in> ?U \<and> y \<in> ?V \<and> ?U \<inter> ?V = {}"
hoelzl@51472
   139
    using dist_pos_lt[OF xy] th0[of dist, OF dist_triangle dist_commute]
hoelzl@51472
   140
    using open_ball[of _ "dist x y / 2"] by auto
hoelzl@51472
   141
  then show "\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
hoelzl@51472
   142
    by blast
hoelzl@51472
   143
qed
hoelzl@51472
   144
hoelzl@51472
   145
lemma eventually_nhds_metric:
hoelzl@51472
   146
  fixes a :: "'a :: metric_space"
hoelzl@51472
   147
  shows "eventually P (nhds a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. dist x a < d \<longrightarrow> P x)"
hoelzl@51472
   148
unfolding eventually_nhds open_dist
hoelzl@51472
   149
apply safe
hoelzl@51472
   150
apply fast
hoelzl@51472
   151
apply (rule_tac x="{x. dist x a < d}" in exI, simp)
hoelzl@51472
   152
apply clarsimp
hoelzl@51472
   153
apply (rule_tac x="d - dist x a" in exI, clarsimp)
hoelzl@51472
   154
apply (simp only: less_diff_eq)
hoelzl@51472
   155
apply (erule le_less_trans [OF dist_triangle])
hoelzl@51472
   156
done
hoelzl@51472
   157
hoelzl@51472
   158
lemma eventually_at:
hoelzl@51472
   159
  fixes a :: "'a::metric_space"
hoelzl@51472
   160
  shows "eventually P (at a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> P x)"
hoelzl@51472
   161
unfolding at_def eventually_within eventually_nhds_metric by auto
hoelzl@51472
   162
hoelzl@51472
   163
lemma eventually_within_less: (* COPY FROM Topo/eventually_within *)
hoelzl@51472
   164
  fixes a :: "'a :: metric_space"
hoelzl@51472
   165
  shows "eventually P (at a within S) \<longleftrightarrow> (\<exists>d>0. \<forall>x\<in>S. 0 < dist x a \<and> dist x a < d \<longrightarrow> P x)"
hoelzl@51472
   166
  unfolding eventually_within eventually_at dist_nz by auto
hoelzl@51472
   167
hoelzl@51472
   168
lemma eventually_within_le: (* COPY FROM Topo/eventually_within_le *)
hoelzl@51472
   169
  fixes a :: "'a :: metric_space"
hoelzl@51472
   170
  shows "eventually P (at a within S) \<longleftrightarrow> (\<exists>d>0. \<forall>x\<in>S. 0 < dist x a \<and> dist x a <= d \<longrightarrow> P x)"
hoelzl@51472
   171
  unfolding eventually_within_less by auto (metis dense order_le_less_trans)
hoelzl@51472
   172
hoelzl@51472
   173
lemma tendstoI:
hoelzl@51472
   174
  fixes l :: "'a :: metric_space"
hoelzl@51472
   175
  assumes "\<And>e. 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F"
hoelzl@51472
   176
  shows "(f ---> l) F"
hoelzl@51472
   177
  apply (rule topological_tendstoI)
hoelzl@51472
   178
  apply (simp add: open_dist)
hoelzl@51472
   179
  apply (drule (1) bspec, clarify)
hoelzl@51472
   180
  apply (drule assms)
hoelzl@51472
   181
  apply (erule eventually_elim1, simp)
hoelzl@51472
   182
  done
hoelzl@51472
   183
hoelzl@51472
   184
lemma tendstoD:
hoelzl@51472
   185
  fixes l :: "'a :: metric_space"
hoelzl@51472
   186
  shows "(f ---> l) F \<Longrightarrow> 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F"
hoelzl@51472
   187
  apply (drule_tac S="{x. dist x l < e}" in topological_tendstoD)
hoelzl@51472
   188
  apply (clarsimp simp add: open_dist)
hoelzl@51472
   189
  apply (rule_tac x="e - dist x l" in exI, clarsimp)
hoelzl@51472
   190
  apply (simp only: less_diff_eq)
hoelzl@51472
   191
  apply (erule le_less_trans [OF dist_triangle])
hoelzl@51472
   192
  apply simp
hoelzl@51472
   193
  apply simp
hoelzl@51472
   194
  done
hoelzl@51472
   195
hoelzl@51472
   196
lemma tendsto_iff:
hoelzl@51472
   197
  fixes l :: "'a :: metric_space"
hoelzl@51472
   198
  shows "(f ---> l) F \<longleftrightarrow> (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) F)"
hoelzl@51472
   199
  using tendstoI tendstoD by fast
hoelzl@51472
   200
hoelzl@51472
   201
lemma metric_tendsto_imp_tendsto:
hoelzl@51472
   202
  fixes a :: "'a :: metric_space" and b :: "'b :: metric_space"
hoelzl@51472
   203
  assumes f: "(f ---> a) F"
hoelzl@51472
   204
  assumes le: "eventually (\<lambda>x. dist (g x) b \<le> dist (f x) a) F"
hoelzl@51472
   205
  shows "(g ---> b) F"
hoelzl@51472
   206
proof (rule tendstoI)
hoelzl@51472
   207
  fix e :: real assume "0 < e"
hoelzl@51472
   208
  with f have "eventually (\<lambda>x. dist (f x) a < e) F" by (rule tendstoD)
hoelzl@51472
   209
  with le show "eventually (\<lambda>x. dist (g x) b < e) F"
hoelzl@51472
   210
    using le_less_trans by (rule eventually_elim2)
hoelzl@51472
   211
qed
hoelzl@51472
   212
hoelzl@51472
   213
lemma filterlim_real_sequentially: "LIM x sequentially. real x :> at_top"
hoelzl@51472
   214
  unfolding filterlim_at_top
hoelzl@51472
   215
  apply (intro allI)
hoelzl@51472
   216
  apply (rule_tac c="natceiling (Z + 1)" in eventually_sequentiallyI)
hoelzl@51472
   217
  apply (auto simp: natceiling_le_eq)
hoelzl@51472
   218
  done
hoelzl@51472
   219
hoelzl@51472
   220
subsubsection {* Limits of Sequences *}
hoelzl@51472
   221
hoelzl@51472
   222
lemma LIMSEQ_def: "X ----> (L::'a::metric_space) \<longleftrightarrow> (\<forall>r>0. \<exists>no. \<forall>n\<ge>no. dist (X n) L < r)"
hoelzl@51472
   223
  unfolding tendsto_iff eventually_sequentially ..
hoelzl@51472
   224
hoelzl@51472
   225
lemma LIMSEQ_iff_nz: "X ----> (L::'a::metric_space) = (\<forall>r>0. \<exists>no>0. \<forall>n\<ge>no. dist (X n) L < r)"
hoelzl@51472
   226
  unfolding LIMSEQ_def by (metis Suc_leD zero_less_Suc)
hoelzl@51472
   227
hoelzl@51472
   228
lemma metric_LIMSEQ_I:
hoelzl@51472
   229
  "(\<And>r. 0 < r \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. dist (X n) L < r) \<Longrightarrow> X ----> (L::'a::metric_space)"
hoelzl@51472
   230
by (simp add: LIMSEQ_def)
hoelzl@51472
   231
hoelzl@51472
   232
lemma metric_LIMSEQ_D:
hoelzl@51472
   233
  "\<lbrakk>X ----> (L::'a::metric_space); 0 < r\<rbrakk> \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. dist (X n) L < r"
hoelzl@51472
   234
by (simp add: LIMSEQ_def)
hoelzl@51472
   235
hoelzl@51472
   236
hoelzl@51472
   237
subsubsection {* Limits of Functions *}
hoelzl@51472
   238
hoelzl@51472
   239
lemma LIM_def: "f -- (a::'a::metric_space) --> (L::'b::metric_space) =
hoelzl@51472
   240
     (\<forall>r > 0. \<exists>s > 0. \<forall>x. x \<noteq> a & dist x a < s
hoelzl@51472
   241
        --> dist (f x) L < r)"
hoelzl@51472
   242
unfolding tendsto_iff eventually_at ..
hoelzl@51472
   243
hoelzl@51472
   244
lemma metric_LIM_I:
hoelzl@51472
   245
  "(\<And>r. 0 < r \<Longrightarrow> \<exists>s>0. \<forall>x. x \<noteq> a \<and> dist x a < s \<longrightarrow> dist (f x) L < r)
hoelzl@51472
   246
    \<Longrightarrow> f -- (a::'a::metric_space) --> (L::'b::metric_space)"
hoelzl@51472
   247
by (simp add: LIM_def)
hoelzl@51472
   248
hoelzl@51472
   249
lemma metric_LIM_D:
hoelzl@51472
   250
  "\<lbrakk>f -- (a::'a::metric_space) --> (L::'b::metric_space); 0 < r\<rbrakk>
hoelzl@51472
   251
    \<Longrightarrow> \<exists>s>0. \<forall>x. x \<noteq> a \<and> dist x a < s \<longrightarrow> dist (f x) L < r"
hoelzl@51472
   252
by (simp add: LIM_def)
hoelzl@51472
   253
hoelzl@51472
   254
lemma metric_LIM_imp_LIM:
hoelzl@51472
   255
  assumes f: "f -- a --> (l::'a::metric_space)"
hoelzl@51472
   256
  assumes le: "\<And>x. x \<noteq> a \<Longrightarrow> dist (g x) m \<le> dist (f x) l"
hoelzl@51472
   257
  shows "g -- a --> (m::'b::metric_space)"
hoelzl@51472
   258
  by (rule metric_tendsto_imp_tendsto [OF f]) (auto simp add: eventually_at_topological le)
hoelzl@51472
   259
hoelzl@51472
   260
lemma metric_LIM_equal2:
hoelzl@51472
   261
  assumes 1: "0 < R"
hoelzl@51472
   262
  assumes 2: "\<And>x. \<lbrakk>x \<noteq> a; dist x a < R\<rbrakk> \<Longrightarrow> f x = g x"
hoelzl@51472
   263
  shows "g -- a --> l \<Longrightarrow> f -- (a::'a::metric_space) --> l"
hoelzl@51472
   264
apply (rule topological_tendstoI)
hoelzl@51472
   265
apply (drule (2) topological_tendstoD)
hoelzl@51472
   266
apply (simp add: eventually_at, safe)
hoelzl@51472
   267
apply (rule_tac x="min d R" in exI, safe)
hoelzl@51472
   268
apply (simp add: 1)
hoelzl@51472
   269
apply (simp add: 2)
hoelzl@51472
   270
done
hoelzl@51472
   271
hoelzl@51472
   272
lemma metric_LIM_compose2:
hoelzl@51472
   273
  assumes f: "f -- (a::'a::metric_space) --> b"
hoelzl@51472
   274
  assumes g: "g -- b --> c"
hoelzl@51472
   275
  assumes inj: "\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<noteq> b"
hoelzl@51472
   276
  shows "(\<lambda>x. g (f x)) -- a --> c"
hoelzl@51472
   277
  using g f inj [folded eventually_at]
hoelzl@51472
   278
  by (rule tendsto_compose_eventually)
hoelzl@51472
   279
hoelzl@51472
   280
lemma metric_isCont_LIM_compose2:
hoelzl@51472
   281
  fixes f :: "'a :: metric_space \<Rightarrow> _"
hoelzl@51472
   282
  assumes f [unfolded isCont_def]: "isCont f a"
hoelzl@51472
   283
  assumes g: "g -- f a --> l"
hoelzl@51472
   284
  assumes inj: "\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<noteq> f a"
hoelzl@51472
   285
  shows "(\<lambda>x. g (f x)) -- a --> l"
hoelzl@51472
   286
by (rule metric_LIM_compose2 [OF f g inj])
hoelzl@51472
   287
hoelzl@51474
   288
subsubsection {* Boundedness *}
hoelzl@51474
   289
hoelzl@51474
   290
definition Bfun :: "('a \<Rightarrow> 'b::metric_space) \<Rightarrow> 'a filter \<Rightarrow> bool" where
hoelzl@51474
   291
  Bfun_metric_def: "Bfun f F = (\<exists>y. \<exists>K>0. eventually (\<lambda>x. dist (f x) y \<le> K) F)"
hoelzl@51474
   292
hoelzl@51474
   293
abbreviation Bseq :: "(nat \<Rightarrow> 'a::metric_space) \<Rightarrow> bool" where
hoelzl@51474
   294
  "Bseq X \<equiv> Bfun X sequentially"
hoelzl@51474
   295
hoelzl@51474
   296
lemma Bseq_conv_Bfun: "Bseq X \<longleftrightarrow> Bfun X sequentially" ..
hoelzl@51474
   297
hoelzl@51474
   298
lemma Bseq_ignore_initial_segment: "Bseq X \<Longrightarrow> Bseq (\<lambda>n. X (n + k))"
hoelzl@51474
   299
  unfolding Bfun_metric_def by (subst eventually_sequentially_seg)
hoelzl@51474
   300
hoelzl@51474
   301
lemma Bseq_offset: "Bseq (\<lambda>n. X (n + k)) \<Longrightarrow> Bseq X"
hoelzl@51474
   302
  unfolding Bfun_metric_def by (subst (asm) eventually_sequentially_seg)
hoelzl@51474
   303
hoelzl@51472
   304
subsection {* Complete metric spaces *}
hoelzl@51472
   305
hoelzl@51472
   306
subsection {* Cauchy sequences *}
hoelzl@51472
   307
hoelzl@51472
   308
definition (in metric_space) Cauchy :: "(nat \<Rightarrow> 'a) \<Rightarrow> bool" where
hoelzl@51472
   309
  "Cauchy X = (\<forall>e>0. \<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. dist (X m) (X n) < e)"
hoelzl@51472
   310
hoelzl@51472
   311
subsection {* Cauchy Sequences *}
hoelzl@51472
   312
hoelzl@51472
   313
lemma metric_CauchyI:
hoelzl@51472
   314
  "(\<And>e. 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e) \<Longrightarrow> Cauchy X"
hoelzl@51472
   315
  by (simp add: Cauchy_def)
hoelzl@51472
   316
hoelzl@51472
   317
lemma metric_CauchyD:
hoelzl@51472
   318
  "Cauchy X \<Longrightarrow> 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e"
hoelzl@51472
   319
  by (simp add: Cauchy_def)
hoelzl@51472
   320
hoelzl@51472
   321
lemma metric_Cauchy_iff2:
hoelzl@51472
   322
  "Cauchy X = (\<forall>j. (\<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. dist (X m) (X n) < inverse(real (Suc j))))"
hoelzl@51472
   323
apply (simp add: Cauchy_def, auto)
hoelzl@51472
   324
apply (drule reals_Archimedean, safe)
hoelzl@51472
   325
apply (drule_tac x = n in spec, auto)
hoelzl@51472
   326
apply (rule_tac x = M in exI, auto)
hoelzl@51472
   327
apply (drule_tac x = m in spec, simp)
hoelzl@51472
   328
apply (drule_tac x = na in spec, auto)
hoelzl@51472
   329
done
hoelzl@51472
   330
hoelzl@51472
   331
lemma Cauchy_subseq_Cauchy:
hoelzl@51472
   332
  "\<lbrakk> Cauchy X; subseq f \<rbrakk> \<Longrightarrow> Cauchy (X o f)"
hoelzl@51472
   333
apply (auto simp add: Cauchy_def)
hoelzl@51472
   334
apply (drule_tac x=e in spec, clarify)
hoelzl@51472
   335
apply (rule_tac x=M in exI, clarify)
hoelzl@51472
   336
apply (blast intro: le_trans [OF _ seq_suble] dest!: spec)
hoelzl@51472
   337
done
hoelzl@51472
   338
hoelzl@51474
   339
lemma Cauchy_Bseq: "Cauchy X \<Longrightarrow> Bseq X"
hoelzl@51474
   340
  unfolding Cauchy_def Bfun_metric_def eventually_sequentially
hoelzl@51474
   341
  apply (erule_tac x=1 in allE)
hoelzl@51474
   342
  apply simp
hoelzl@51474
   343
  apply safe
hoelzl@51474
   344
  apply (rule_tac x="X M" in exI)
hoelzl@51474
   345
  apply (rule_tac x=1 in exI)
hoelzl@51474
   346
  apply (erule_tac x=M in allE)
hoelzl@51474
   347
  apply simp
hoelzl@51474
   348
  apply (rule_tac x=M in exI)
hoelzl@51474
   349
  apply (auto simp: dist_commute)
hoelzl@51474
   350
  done
hoelzl@51472
   351
hoelzl@51472
   352
subsubsection {* Cauchy Sequences are Convergent *}
hoelzl@51472
   353
hoelzl@51472
   354
class complete_space = metric_space +
hoelzl@51472
   355
  assumes Cauchy_convergent: "Cauchy X \<Longrightarrow> convergent X"
hoelzl@51472
   356
hoelzl@51472
   357
theorem LIMSEQ_imp_Cauchy:
hoelzl@51472
   358
  assumes X: "X ----> a" shows "Cauchy X"
hoelzl@51472
   359
proof (rule metric_CauchyI)
hoelzl@51472
   360
  fix e::real assume "0 < e"
hoelzl@51472
   361
  hence "0 < e/2" by simp
hoelzl@51472
   362
  with X have "\<exists>N. \<forall>n\<ge>N. dist (X n) a < e/2" by (rule metric_LIMSEQ_D)
hoelzl@51472
   363
  then obtain N where N: "\<forall>n\<ge>N. dist (X n) a < e/2" ..
hoelzl@51472
   364
  show "\<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m) (X n) < e"
hoelzl@51472
   365
  proof (intro exI allI impI)
hoelzl@51472
   366
    fix m assume "N \<le> m"
hoelzl@51472
   367
    hence m: "dist (X m) a < e/2" using N by fast
hoelzl@51472
   368
    fix n assume "N \<le> n"
hoelzl@51472
   369
    hence n: "dist (X n) a < e/2" using N by fast
hoelzl@51472
   370
    have "dist (X m) (X n) \<le> dist (X m) a + dist (X n) a"
hoelzl@51472
   371
      by (rule dist_triangle2)
hoelzl@51472
   372
    also from m n have "\<dots> < e" by simp
hoelzl@51472
   373
    finally show "dist (X m) (X n) < e" .
hoelzl@51472
   374
  qed
hoelzl@51472
   375
qed
hoelzl@51472
   376
hoelzl@51472
   377
lemma convergent_Cauchy: "convergent X \<Longrightarrow> Cauchy X"
hoelzl@51472
   378
unfolding convergent_def
hoelzl@51472
   379
by (erule exE, erule LIMSEQ_imp_Cauchy)
hoelzl@51472
   380
hoelzl@51472
   381
lemma Cauchy_convergent_iff:
hoelzl@51472
   382
  fixes X :: "nat \<Rightarrow> 'a::complete_space"
hoelzl@51472
   383
  shows "Cauchy X = convergent X"
hoelzl@51472
   384
by (fast intro: Cauchy_convergent convergent_Cauchy)
hoelzl@51472
   385
hoelzl@51472
   386
subsection {* Uniform Continuity *}
hoelzl@51472
   387
hoelzl@51472
   388
definition
hoelzl@51472
   389
  isUCont :: "['a::metric_space \<Rightarrow> 'b::metric_space] \<Rightarrow> bool" where
hoelzl@51472
   390
  "isUCont f = (\<forall>r>0. \<exists>s>0. \<forall>x y. dist x y < s \<longrightarrow> dist (f x) (f y) < r)"
hoelzl@51472
   391
hoelzl@51472
   392
lemma isUCont_isCont: "isUCont f ==> isCont f x"
hoelzl@51472
   393
by (simp add: isUCont_def isCont_def LIM_def, force)
hoelzl@51472
   394
hoelzl@51472
   395
lemma isUCont_Cauchy:
hoelzl@51472
   396
  "\<lbrakk>isUCont f; Cauchy X\<rbrakk> \<Longrightarrow> Cauchy (\<lambda>n. f (X n))"
hoelzl@51472
   397
unfolding isUCont_def
hoelzl@51472
   398
apply (rule metric_CauchyI)
hoelzl@51472
   399
apply (drule_tac x=e in spec, safe)
hoelzl@51472
   400
apply (drule_tac e=s in metric_CauchyD, safe)
hoelzl@51472
   401
apply (rule_tac x=M in exI, simp)
hoelzl@51472
   402
done
hoelzl@51472
   403
hoelzl@51472
   404
subsection {* The set of real numbers is a complete metric space *}
hoelzl@51472
   405
hoelzl@51472
   406
instantiation real :: metric_space
hoelzl@51472
   407
begin
hoelzl@51472
   408
hoelzl@51472
   409
definition dist_real_def:
hoelzl@51472
   410
  "dist x y = \<bar>x - y\<bar>"
hoelzl@51472
   411
hoelzl@51472
   412
definition open_real_def:
hoelzl@51472
   413
  "open (S :: real set) \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
hoelzl@51472
   414
hoelzl@51472
   415
instance
hoelzl@51472
   416
  by default (auto simp: open_real_def dist_real_def)
hoelzl@51472
   417
end
hoelzl@51472
   418
hoelzl@51472
   419
instance real :: linorder_topology
hoelzl@51472
   420
proof
hoelzl@51472
   421
  show "(open :: real set \<Rightarrow> bool) = generate_topology (range lessThan \<union> range greaterThan)"
hoelzl@51472
   422
  proof (rule ext, safe)
hoelzl@51472
   423
    fix S :: "real set" assume "open S"
hoelzl@51472
   424
    then guess f unfolding open_real_def bchoice_iff ..
hoelzl@51472
   425
    then have *: "S = (\<Union>x\<in>S. {x - f x <..} \<inter> {..< x + f x})"
hoelzl@51472
   426
      by (fastforce simp: dist_real_def)
hoelzl@51472
   427
    show "generate_topology (range lessThan \<union> range greaterThan) S"
hoelzl@51472
   428
      apply (subst *)
hoelzl@51472
   429
      apply (intro generate_topology_Union generate_topology.Int)
hoelzl@51472
   430
      apply (auto intro: generate_topology.Basis)
hoelzl@51472
   431
      done
hoelzl@51472
   432
  next
hoelzl@51472
   433
    fix S :: "real set" assume "generate_topology (range lessThan \<union> range greaterThan) S"
hoelzl@51472
   434
    moreover have "\<And>a::real. open {..<a}"
hoelzl@51472
   435
      unfolding open_real_def dist_real_def
hoelzl@51472
   436
    proof clarify
hoelzl@51472
   437
      fix x a :: real assume "x < a"
hoelzl@51472
   438
      hence "0 < a - x \<and> (\<forall>y. \<bar>y - x\<bar> < a - x \<longrightarrow> y \<in> {..<a})" by auto
hoelzl@51472
   439
      thus "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {..<a}" ..
hoelzl@51472
   440
    qed
hoelzl@51472
   441
    moreover have "\<And>a::real. open {a <..}"
hoelzl@51472
   442
      unfolding open_real_def dist_real_def
hoelzl@51472
   443
    proof clarify
hoelzl@51472
   444
      fix x a :: real assume "a < x"
hoelzl@51472
   445
      hence "0 < x - a \<and> (\<forall>y. \<bar>y - x\<bar> < x - a \<longrightarrow> y \<in> {a<..})" by auto
hoelzl@51472
   446
      thus "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {a<..}" ..
hoelzl@51472
   447
    qed
hoelzl@51472
   448
    ultimately show "open S"
hoelzl@51472
   449
      by induct auto
hoelzl@51472
   450
  qed
hoelzl@51472
   451
qed
hoelzl@51472
   452
hoelzl@51472
   453
lemmas open_real_greaterThan = open_greaterThan[where 'a=real]
hoelzl@51472
   454
lemmas open_real_lessThan = open_lessThan[where 'a=real]
hoelzl@51472
   455
lemmas open_real_greaterThanLessThan = open_greaterThanLessThan[where 'a=real]
hoelzl@51472
   456
lemmas closed_real_atMost = closed_atMost[where 'a=real]
hoelzl@51472
   457
lemmas closed_real_atLeast = closed_atLeast[where 'a=real]
hoelzl@51472
   458
lemmas closed_real_atLeastAtMost = closed_atLeastAtMost[where 'a=real]
hoelzl@51472
   459
hoelzl@51472
   460
text {*
hoelzl@51472
   461
Proof that Cauchy sequences converge based on the one from
hoelzl@51472
   462
http://pirate.shu.edu/~wachsmut/ira/numseq/proofs/cauconv.html
hoelzl@51472
   463
*}
hoelzl@51472
   464
hoelzl@51472
   465
text {*
hoelzl@51472
   466
  If sequence @{term "X"} is Cauchy, then its limit is the lub of
hoelzl@51472
   467
  @{term "{r::real. \<exists>N. \<forall>n\<ge>N. r < X n}"}
hoelzl@51472
   468
*}
hoelzl@51472
   469
hoelzl@51472
   470
lemma isUb_UNIV_I: "(\<And>y. y \<in> S \<Longrightarrow> y \<le> u) \<Longrightarrow> isUb UNIV S u"
hoelzl@51472
   471
by (simp add: isUbI setleI)
hoelzl@51472
   472
hoelzl@51472
   473
lemma increasing_LIMSEQ:
hoelzl@51472
   474
  fixes f :: "nat \<Rightarrow> real"
hoelzl@51472
   475
  assumes inc: "\<And>n. f n \<le> f (Suc n)"
hoelzl@51472
   476
      and bdd: "\<And>n. f n \<le> l"
hoelzl@51472
   477
      and en: "\<And>e. 0 < e \<Longrightarrow> \<exists>n. l \<le> f n + e"
hoelzl@51472
   478
  shows "f ----> l"
hoelzl@51472
   479
proof (rule increasing_tendsto)
hoelzl@51472
   480
  fix x assume "x < l"
hoelzl@51472
   481
  with dense[of 0 "l - x"] obtain e where "0 < e" "e < l - x"
hoelzl@51472
   482
    by auto
hoelzl@51472
   483
  from en[OF `0 < e`] obtain n where "l - e \<le> f n"
hoelzl@51472
   484
    by (auto simp: field_simps)
hoelzl@51472
   485
  with `e < l - x` `0 < e` have "x < f n" by simp
hoelzl@51472
   486
  with incseq_SucI[of f, OF inc] show "eventually (\<lambda>n. x < f n) sequentially"
hoelzl@51472
   487
    by (auto simp: eventually_sequentially incseq_def intro: less_le_trans)
hoelzl@51472
   488
qed (insert bdd, auto)
hoelzl@51472
   489
hoelzl@51472
   490
lemma real_Cauchy_convergent:
hoelzl@51472
   491
  fixes X :: "nat \<Rightarrow> real"
hoelzl@51472
   492
  assumes X: "Cauchy X"
hoelzl@51472
   493
  shows "convergent X"
hoelzl@51472
   494
proof -
hoelzl@51472
   495
  def S \<equiv> "{x::real. \<exists>N. \<forall>n\<ge>N. x < X n}"
hoelzl@51472
   496
  then have mem_S: "\<And>N x. \<forall>n\<ge>N. x < X n \<Longrightarrow> x \<in> S" by auto
hoelzl@51472
   497
hoelzl@51472
   498
  { fix N x assume N: "\<forall>n\<ge>N. X n < x"
hoelzl@51472
   499
  have "isUb UNIV S x"
hoelzl@51472
   500
  proof (rule isUb_UNIV_I)
hoelzl@51472
   501
  fix y::real assume "y \<in> S"
hoelzl@51472
   502
  hence "\<exists>M. \<forall>n\<ge>M. y < X n"
hoelzl@51472
   503
    by (simp add: S_def)
hoelzl@51472
   504
  then obtain M where "\<forall>n\<ge>M. y < X n" ..
hoelzl@51472
   505
  hence "y < X (max M N)" by simp
hoelzl@51472
   506
  also have "\<dots> < x" using N by simp
hoelzl@51472
   507
  finally show "y \<le> x"
hoelzl@51472
   508
    by (rule order_less_imp_le)
hoelzl@51472
   509
  qed }
hoelzl@51472
   510
  note bound_isUb = this 
hoelzl@51472
   511
hoelzl@51472
   512
  have "\<exists>u. isLub UNIV S u"
hoelzl@51472
   513
  proof (rule reals_complete)
hoelzl@51472
   514
  obtain N where "\<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m) (X n) < 1"
hoelzl@51472
   515
    using X[THEN metric_CauchyD, OF zero_less_one] by auto
hoelzl@51472
   516
  hence N: "\<forall>n\<ge>N. dist (X n) (X N) < 1" by simp
hoelzl@51472
   517
  show "\<exists>x. x \<in> S"
hoelzl@51472
   518
  proof
hoelzl@51472
   519
    from N have "\<forall>n\<ge>N. X N - 1 < X n"
hoelzl@51472
   520
      by (simp add: abs_diff_less_iff dist_real_def)
hoelzl@51472
   521
    thus "X N - 1 \<in> S" by (rule mem_S)
hoelzl@51472
   522
  qed
hoelzl@51472
   523
  show "\<exists>u. isUb UNIV S u"
hoelzl@51472
   524
  proof
hoelzl@51472
   525
    from N have "\<forall>n\<ge>N. X n < X N + 1"
hoelzl@51472
   526
      by (simp add: abs_diff_less_iff dist_real_def)
hoelzl@51472
   527
    thus "isUb UNIV S (X N + 1)"
hoelzl@51472
   528
      by (rule bound_isUb)
hoelzl@51472
   529
  qed
hoelzl@51472
   530
  qed
hoelzl@51472
   531
  then obtain x where x: "isLub UNIV S x" ..
hoelzl@51472
   532
  have "X ----> x"
hoelzl@51472
   533
  proof (rule metric_LIMSEQ_I)
hoelzl@51472
   534
  fix r::real assume "0 < r"
hoelzl@51472
   535
  hence r: "0 < r/2" by simp
hoelzl@51472
   536
  obtain N where "\<forall>n\<ge>N. \<forall>m\<ge>N. dist (X n) (X m) < r/2"
hoelzl@51472
   537
    using metric_CauchyD [OF X r] by auto
hoelzl@51472
   538
  hence "\<forall>n\<ge>N. dist (X n) (X N) < r/2" by simp
hoelzl@51472
   539
  hence N: "\<forall>n\<ge>N. X N - r/2 < X n \<and> X n < X N + r/2"
hoelzl@51472
   540
    by (simp only: dist_real_def abs_diff_less_iff)
hoelzl@51472
   541
hoelzl@51472
   542
  from N have "\<forall>n\<ge>N. X N - r/2 < X n" by fast
hoelzl@51472
   543
  hence "X N - r/2 \<in> S" by (rule mem_S)
hoelzl@51472
   544
  hence 1: "X N - r/2 \<le> x" using x isLub_isUb isUbD by fast
hoelzl@51472
   545
hoelzl@51472
   546
  from N have "\<forall>n\<ge>N. X n < X N + r/2" by fast
hoelzl@51472
   547
  hence "isUb UNIV S (X N + r/2)" by (rule bound_isUb)
hoelzl@51472
   548
  hence 2: "x \<le> X N + r/2" using x isLub_le_isUb by fast
hoelzl@51472
   549
hoelzl@51472
   550
  show "\<exists>N. \<forall>n\<ge>N. dist (X n) x < r"
hoelzl@51472
   551
  proof (intro exI allI impI)
hoelzl@51472
   552
    fix n assume n: "N \<le> n"
hoelzl@51472
   553
    from N n have "X n < X N + r/2" and "X N - r/2 < X n" by simp+
hoelzl@51472
   554
    thus "dist (X n) x < r" using 1 2
hoelzl@51472
   555
      by (simp add: abs_diff_less_iff dist_real_def)
hoelzl@51472
   556
  qed
hoelzl@51472
   557
  qed
hoelzl@51472
   558
  then show ?thesis unfolding convergent_def by auto
hoelzl@51472
   559
qed
hoelzl@51472
   560
hoelzl@51472
   561
instance real :: complete_space
hoelzl@51472
   562
  by intro_classes (rule real_Cauchy_convergent)
hoelzl@51472
   563
hoelzl@51472
   564
lemma tendsto_dist [tendsto_intros]:
hoelzl@51472
   565
  fixes l m :: "'a :: metric_space"
hoelzl@51472
   566
  assumes f: "(f ---> l) F" and g: "(g ---> m) F"
hoelzl@51472
   567
  shows "((\<lambda>x. dist (f x) (g x)) ---> dist l m) F"
hoelzl@51472
   568
proof (rule tendstoI)
hoelzl@51472
   569
  fix e :: real assume "0 < e"
hoelzl@51472
   570
  hence e2: "0 < e/2" by simp
hoelzl@51472
   571
  from tendstoD [OF f e2] tendstoD [OF g e2]
hoelzl@51472
   572
  show "eventually (\<lambda>x. dist (dist (f x) (g x)) (dist l m) < e) F"
hoelzl@51472
   573
  proof (eventually_elim)
hoelzl@51472
   574
    case (elim x)
hoelzl@51472
   575
    then show "dist (dist (f x) (g x)) (dist l m) < e"
hoelzl@51472
   576
      unfolding dist_real_def
hoelzl@51472
   577
      using dist_triangle2 [of "f x" "g x" "l"]
hoelzl@51472
   578
      using dist_triangle2 [of "g x" "l" "m"]
hoelzl@51472
   579
      using dist_triangle3 [of "l" "m" "f x"]
hoelzl@51472
   580
      using dist_triangle [of "f x" "m" "g x"]
hoelzl@51472
   581
      by arith
hoelzl@51472
   582
  qed
hoelzl@51472
   583
qed
hoelzl@51472
   584
hoelzl@51472
   585
lemma tendsto_at_topI_sequentially:
hoelzl@51472
   586
  fixes f :: "real \<Rightarrow> real"
hoelzl@51472
   587
  assumes mono: "mono f"
hoelzl@51472
   588
  assumes limseq: "(\<lambda>n. f (real n)) ----> y"
hoelzl@51472
   589
  shows "(f ---> y) at_top"
hoelzl@51472
   590
proof (rule tendstoI)
hoelzl@51472
   591
  fix e :: real assume "0 < e"
hoelzl@51472
   592
  with limseq obtain N :: nat where N: "\<And>n. N \<le> n \<Longrightarrow> \<bar>f (real n) - y\<bar> < e"
hoelzl@51472
   593
    by (auto simp: LIMSEQ_def dist_real_def)
hoelzl@51472
   594
  { fix x :: real
hoelzl@51472
   595
    from ex_le_of_nat[of x] guess n ..
hoelzl@51472
   596
    note monoD[OF mono this]
hoelzl@51472
   597
    also have "f (real_of_nat n) \<le> y"
hoelzl@51472
   598
      by (rule LIMSEQ_le_const[OF limseq])
hoelzl@51472
   599
         (auto intro: exI[of _ n] monoD[OF mono] simp: real_eq_of_nat[symmetric])
hoelzl@51472
   600
    finally have "f x \<le> y" . }
hoelzl@51472
   601
  note le = this
hoelzl@51472
   602
  have "eventually (\<lambda>x. real N \<le> x) at_top"
hoelzl@51472
   603
    by (rule eventually_ge_at_top)
hoelzl@51472
   604
  then show "eventually (\<lambda>x. dist (f x) y < e) at_top"
hoelzl@51472
   605
  proof eventually_elim
hoelzl@51472
   606
    fix x assume N': "real N \<le> x"
hoelzl@51472
   607
    with N[of N] le have "y - f (real N) < e" by auto
hoelzl@51472
   608
    moreover note monoD[OF mono N']
hoelzl@51472
   609
    ultimately show "dist (f x) y < e"
hoelzl@51472
   610
      using le[of x] by (auto simp: dist_real_def field_simps)
hoelzl@51472
   611
  qed
hoelzl@51472
   612
qed
hoelzl@51472
   613
hoelzl@51472
   614
lemma Cauchy_iff2:
hoelzl@51472
   615
  "Cauchy X = (\<forall>j. (\<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. \<bar>X m - X n\<bar> < inverse(real (Suc j))))"
hoelzl@51472
   616
  unfolding metric_Cauchy_iff2 dist_real_def ..
hoelzl@51472
   617
hoelzl@51472
   618
end
hoelzl@51472
   619