src/HOL/Metric_Spaces.thy
author hoelzl
Fri Mar 22 10:41:43 2013 +0100 (2013-03-22)
changeset 51472 adb441e4b9e9
child 51473 1210309fddab
permissions -rw-r--r--
move metric_space to its own theory
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@51472
   103
lemma (in metric_space) 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@51472
   110
end
hoelzl@51472
   111
hoelzl@51472
   112
instance metric_space \<subseteq> t2_space
hoelzl@51472
   113
proof
hoelzl@51472
   114
  fix x y :: "'a::metric_space"
hoelzl@51472
   115
  assume xy: "x \<noteq> y"
hoelzl@51472
   116
  let ?U = "{y'. dist x y' < dist x y / 2}"
hoelzl@51472
   117
  let ?V = "{x'. dist y x' < dist x y / 2}"
hoelzl@51472
   118
  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
   119
               \<Longrightarrow> \<not>(d x y * 2 < d x z \<and> d z y * 2 < d x z)" by arith
hoelzl@51472
   120
  have "open ?U \<and> open ?V \<and> x \<in> ?U \<and> y \<in> ?V \<and> ?U \<inter> ?V = {}"
hoelzl@51472
   121
    using dist_pos_lt[OF xy] th0[of dist, OF dist_triangle dist_commute]
hoelzl@51472
   122
    using open_ball[of _ "dist x y / 2"] by auto
hoelzl@51472
   123
  then show "\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
hoelzl@51472
   124
    by blast
hoelzl@51472
   125
qed
hoelzl@51472
   126
hoelzl@51472
   127
lemma eventually_nhds_metric:
hoelzl@51472
   128
  fixes a :: "'a :: metric_space"
hoelzl@51472
   129
  shows "eventually P (nhds a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. dist x a < d \<longrightarrow> P x)"
hoelzl@51472
   130
unfolding eventually_nhds open_dist
hoelzl@51472
   131
apply safe
hoelzl@51472
   132
apply fast
hoelzl@51472
   133
apply (rule_tac x="{x. dist x a < d}" in exI, simp)
hoelzl@51472
   134
apply clarsimp
hoelzl@51472
   135
apply (rule_tac x="d - dist x a" in exI, clarsimp)
hoelzl@51472
   136
apply (simp only: less_diff_eq)
hoelzl@51472
   137
apply (erule le_less_trans [OF dist_triangle])
hoelzl@51472
   138
done
hoelzl@51472
   139
hoelzl@51472
   140
lemma eventually_at:
hoelzl@51472
   141
  fixes a :: "'a::metric_space"
hoelzl@51472
   142
  shows "eventually P (at a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> P x)"
hoelzl@51472
   143
unfolding at_def eventually_within eventually_nhds_metric by auto
hoelzl@51472
   144
hoelzl@51472
   145
lemma eventually_within_less: (* COPY FROM Topo/eventually_within *)
hoelzl@51472
   146
  fixes a :: "'a :: metric_space"
hoelzl@51472
   147
  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
   148
  unfolding eventually_within eventually_at dist_nz by auto
hoelzl@51472
   149
hoelzl@51472
   150
lemma eventually_within_le: (* COPY FROM Topo/eventually_within_le *)
hoelzl@51472
   151
  fixes a :: "'a :: metric_space"
hoelzl@51472
   152
  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
   153
  unfolding eventually_within_less by auto (metis dense order_le_less_trans)
hoelzl@51472
   154
hoelzl@51472
   155
lemma tendstoI:
hoelzl@51472
   156
  fixes l :: "'a :: metric_space"
hoelzl@51472
   157
  assumes "\<And>e. 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F"
hoelzl@51472
   158
  shows "(f ---> l) F"
hoelzl@51472
   159
  apply (rule topological_tendstoI)
hoelzl@51472
   160
  apply (simp add: open_dist)
hoelzl@51472
   161
  apply (drule (1) bspec, clarify)
hoelzl@51472
   162
  apply (drule assms)
hoelzl@51472
   163
  apply (erule eventually_elim1, simp)
hoelzl@51472
   164
  done
hoelzl@51472
   165
hoelzl@51472
   166
lemma tendstoD:
hoelzl@51472
   167
  fixes l :: "'a :: metric_space"
hoelzl@51472
   168
  shows "(f ---> l) F \<Longrightarrow> 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F"
hoelzl@51472
   169
  apply (drule_tac S="{x. dist x l < e}" in topological_tendstoD)
hoelzl@51472
   170
  apply (clarsimp simp add: open_dist)
hoelzl@51472
   171
  apply (rule_tac x="e - dist x l" in exI, clarsimp)
hoelzl@51472
   172
  apply (simp only: less_diff_eq)
hoelzl@51472
   173
  apply (erule le_less_trans [OF dist_triangle])
hoelzl@51472
   174
  apply simp
hoelzl@51472
   175
  apply simp
hoelzl@51472
   176
  done
hoelzl@51472
   177
hoelzl@51472
   178
lemma tendsto_iff:
hoelzl@51472
   179
  fixes l :: "'a :: metric_space"
hoelzl@51472
   180
  shows "(f ---> l) F \<longleftrightarrow> (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) F)"
hoelzl@51472
   181
  using tendstoI tendstoD by fast
hoelzl@51472
   182
hoelzl@51472
   183
lemma metric_tendsto_imp_tendsto:
hoelzl@51472
   184
  fixes a :: "'a :: metric_space" and b :: "'b :: metric_space"
hoelzl@51472
   185
  assumes f: "(f ---> a) F"
hoelzl@51472
   186
  assumes le: "eventually (\<lambda>x. dist (g x) b \<le> dist (f x) a) F"
hoelzl@51472
   187
  shows "(g ---> b) F"
hoelzl@51472
   188
proof (rule tendstoI)
hoelzl@51472
   189
  fix e :: real assume "0 < e"
hoelzl@51472
   190
  with f have "eventually (\<lambda>x. dist (f x) a < e) F" by (rule tendstoD)
hoelzl@51472
   191
  with le show "eventually (\<lambda>x. dist (g x) b < e) F"
hoelzl@51472
   192
    using le_less_trans by (rule eventually_elim2)
hoelzl@51472
   193
qed
hoelzl@51472
   194
hoelzl@51472
   195
lemma filterlim_real_sequentially: "LIM x sequentially. real x :> at_top"
hoelzl@51472
   196
  unfolding filterlim_at_top
hoelzl@51472
   197
  apply (intro allI)
hoelzl@51472
   198
  apply (rule_tac c="natceiling (Z + 1)" in eventually_sequentiallyI)
hoelzl@51472
   199
  apply (auto simp: natceiling_le_eq)
hoelzl@51472
   200
  done
hoelzl@51472
   201
hoelzl@51472
   202
subsubsection {* Limits of Sequences *}
hoelzl@51472
   203
hoelzl@51472
   204
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
   205
  unfolding tendsto_iff eventually_sequentially ..
hoelzl@51472
   206
hoelzl@51472
   207
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
   208
  unfolding LIMSEQ_def by (metis Suc_leD zero_less_Suc)
hoelzl@51472
   209
hoelzl@51472
   210
lemma metric_LIMSEQ_I:
hoelzl@51472
   211
  "(\<And>r. 0 < r \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. dist (X n) L < r) \<Longrightarrow> X ----> (L::'a::metric_space)"
hoelzl@51472
   212
by (simp add: LIMSEQ_def)
hoelzl@51472
   213
hoelzl@51472
   214
lemma metric_LIMSEQ_D:
hoelzl@51472
   215
  "\<lbrakk>X ----> (L::'a::metric_space); 0 < r\<rbrakk> \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. dist (X n) L < r"
hoelzl@51472
   216
by (simp add: LIMSEQ_def)
hoelzl@51472
   217
hoelzl@51472
   218
hoelzl@51472
   219
subsubsection {* Limits of Functions *}
hoelzl@51472
   220
hoelzl@51472
   221
lemma LIM_def: "f -- (a::'a::metric_space) --> (L::'b::metric_space) =
hoelzl@51472
   222
     (\<forall>r > 0. \<exists>s > 0. \<forall>x. x \<noteq> a & dist x a < s
hoelzl@51472
   223
        --> dist (f x) L < r)"
hoelzl@51472
   224
unfolding tendsto_iff eventually_at ..
hoelzl@51472
   225
hoelzl@51472
   226
lemma metric_LIM_I:
hoelzl@51472
   227
  "(\<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
   228
    \<Longrightarrow> f -- (a::'a::metric_space) --> (L::'b::metric_space)"
hoelzl@51472
   229
by (simp add: LIM_def)
hoelzl@51472
   230
hoelzl@51472
   231
lemma metric_LIM_D:
hoelzl@51472
   232
  "\<lbrakk>f -- (a::'a::metric_space) --> (L::'b::metric_space); 0 < r\<rbrakk>
hoelzl@51472
   233
    \<Longrightarrow> \<exists>s>0. \<forall>x. x \<noteq> a \<and> dist x a < s \<longrightarrow> dist (f x) L < r"
hoelzl@51472
   234
by (simp add: LIM_def)
hoelzl@51472
   235
hoelzl@51472
   236
lemma metric_LIM_imp_LIM:
hoelzl@51472
   237
  assumes f: "f -- a --> (l::'a::metric_space)"
hoelzl@51472
   238
  assumes le: "\<And>x. x \<noteq> a \<Longrightarrow> dist (g x) m \<le> dist (f x) l"
hoelzl@51472
   239
  shows "g -- a --> (m::'b::metric_space)"
hoelzl@51472
   240
  by (rule metric_tendsto_imp_tendsto [OF f]) (auto simp add: eventually_at_topological le)
hoelzl@51472
   241
hoelzl@51472
   242
lemma metric_LIM_equal2:
hoelzl@51472
   243
  assumes 1: "0 < R"
hoelzl@51472
   244
  assumes 2: "\<And>x. \<lbrakk>x \<noteq> a; dist x a < R\<rbrakk> \<Longrightarrow> f x = g x"
hoelzl@51472
   245
  shows "g -- a --> l \<Longrightarrow> f -- (a::'a::metric_space) --> l"
hoelzl@51472
   246
apply (rule topological_tendstoI)
hoelzl@51472
   247
apply (drule (2) topological_tendstoD)
hoelzl@51472
   248
apply (simp add: eventually_at, safe)
hoelzl@51472
   249
apply (rule_tac x="min d R" in exI, safe)
hoelzl@51472
   250
apply (simp add: 1)
hoelzl@51472
   251
apply (simp add: 2)
hoelzl@51472
   252
done
hoelzl@51472
   253
hoelzl@51472
   254
lemma metric_LIM_compose2:
hoelzl@51472
   255
  assumes f: "f -- (a::'a::metric_space) --> b"
hoelzl@51472
   256
  assumes g: "g -- b --> c"
hoelzl@51472
   257
  assumes inj: "\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<noteq> b"
hoelzl@51472
   258
  shows "(\<lambda>x. g (f x)) -- a --> c"
hoelzl@51472
   259
  using g f inj [folded eventually_at]
hoelzl@51472
   260
  by (rule tendsto_compose_eventually)
hoelzl@51472
   261
hoelzl@51472
   262
lemma metric_isCont_LIM_compose2:
hoelzl@51472
   263
  fixes f :: "'a :: metric_space \<Rightarrow> _"
hoelzl@51472
   264
  assumes f [unfolded isCont_def]: "isCont f a"
hoelzl@51472
   265
  assumes g: "g -- f a --> l"
hoelzl@51472
   266
  assumes inj: "\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<noteq> f a"
hoelzl@51472
   267
  shows "(\<lambda>x. g (f x)) -- a --> l"
hoelzl@51472
   268
by (rule metric_LIM_compose2 [OF f g inj])
hoelzl@51472
   269
hoelzl@51472
   270
subsection {* Complete metric spaces *}
hoelzl@51472
   271
hoelzl@51472
   272
subsection {* Cauchy sequences *}
hoelzl@51472
   273
hoelzl@51472
   274
definition (in metric_space) Cauchy :: "(nat \<Rightarrow> 'a) \<Rightarrow> bool" where
hoelzl@51472
   275
  "Cauchy X = (\<forall>e>0. \<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. dist (X m) (X n) < e)"
hoelzl@51472
   276
hoelzl@51472
   277
subsection {* Cauchy Sequences *}
hoelzl@51472
   278
hoelzl@51472
   279
lemma metric_CauchyI:
hoelzl@51472
   280
  "(\<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
   281
  by (simp add: Cauchy_def)
hoelzl@51472
   282
hoelzl@51472
   283
lemma metric_CauchyD:
hoelzl@51472
   284
  "Cauchy X \<Longrightarrow> 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e"
hoelzl@51472
   285
  by (simp add: Cauchy_def)
hoelzl@51472
   286
hoelzl@51472
   287
lemma metric_Cauchy_iff2:
hoelzl@51472
   288
  "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
   289
apply (simp add: Cauchy_def, auto)
hoelzl@51472
   290
apply (drule reals_Archimedean, safe)
hoelzl@51472
   291
apply (drule_tac x = n in spec, auto)
hoelzl@51472
   292
apply (rule_tac x = M in exI, auto)
hoelzl@51472
   293
apply (drule_tac x = m in spec, simp)
hoelzl@51472
   294
apply (drule_tac x = na in spec, auto)
hoelzl@51472
   295
done
hoelzl@51472
   296
hoelzl@51472
   297
lemma Cauchy_subseq_Cauchy:
hoelzl@51472
   298
  "\<lbrakk> Cauchy X; subseq f \<rbrakk> \<Longrightarrow> Cauchy (X o f)"
hoelzl@51472
   299
apply (auto simp add: Cauchy_def)
hoelzl@51472
   300
apply (drule_tac x=e in spec, clarify)
hoelzl@51472
   301
apply (rule_tac x=M in exI, clarify)
hoelzl@51472
   302
apply (blast intro: le_trans [OF _ seq_suble] dest!: spec)
hoelzl@51472
   303
done
hoelzl@51472
   304
hoelzl@51472
   305
hoelzl@51472
   306
subsubsection {* Cauchy Sequences are Convergent *}
hoelzl@51472
   307
hoelzl@51472
   308
class complete_space = metric_space +
hoelzl@51472
   309
  assumes Cauchy_convergent: "Cauchy X \<Longrightarrow> convergent X"
hoelzl@51472
   310
hoelzl@51472
   311
theorem LIMSEQ_imp_Cauchy:
hoelzl@51472
   312
  assumes X: "X ----> a" shows "Cauchy X"
hoelzl@51472
   313
proof (rule metric_CauchyI)
hoelzl@51472
   314
  fix e::real assume "0 < e"
hoelzl@51472
   315
  hence "0 < e/2" by simp
hoelzl@51472
   316
  with X have "\<exists>N. \<forall>n\<ge>N. dist (X n) a < e/2" by (rule metric_LIMSEQ_D)
hoelzl@51472
   317
  then obtain N where N: "\<forall>n\<ge>N. dist (X n) a < e/2" ..
hoelzl@51472
   318
  show "\<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m) (X n) < e"
hoelzl@51472
   319
  proof (intro exI allI impI)
hoelzl@51472
   320
    fix m assume "N \<le> m"
hoelzl@51472
   321
    hence m: "dist (X m) a < e/2" using N by fast
hoelzl@51472
   322
    fix n assume "N \<le> n"
hoelzl@51472
   323
    hence n: "dist (X n) a < e/2" using N by fast
hoelzl@51472
   324
    have "dist (X m) (X n) \<le> dist (X m) a + dist (X n) a"
hoelzl@51472
   325
      by (rule dist_triangle2)
hoelzl@51472
   326
    also from m n have "\<dots> < e" by simp
hoelzl@51472
   327
    finally show "dist (X m) (X n) < e" .
hoelzl@51472
   328
  qed
hoelzl@51472
   329
qed
hoelzl@51472
   330
hoelzl@51472
   331
lemma convergent_Cauchy: "convergent X \<Longrightarrow> Cauchy X"
hoelzl@51472
   332
unfolding convergent_def
hoelzl@51472
   333
by (erule exE, erule LIMSEQ_imp_Cauchy)
hoelzl@51472
   334
hoelzl@51472
   335
lemma Cauchy_convergent_iff:
hoelzl@51472
   336
  fixes X :: "nat \<Rightarrow> 'a::complete_space"
hoelzl@51472
   337
  shows "Cauchy X = convergent X"
hoelzl@51472
   338
by (fast intro: Cauchy_convergent convergent_Cauchy)
hoelzl@51472
   339
hoelzl@51472
   340
subsection {* Uniform Continuity *}
hoelzl@51472
   341
hoelzl@51472
   342
definition
hoelzl@51472
   343
  isUCont :: "['a::metric_space \<Rightarrow> 'b::metric_space] \<Rightarrow> bool" where
hoelzl@51472
   344
  "isUCont f = (\<forall>r>0. \<exists>s>0. \<forall>x y. dist x y < s \<longrightarrow> dist (f x) (f y) < r)"
hoelzl@51472
   345
hoelzl@51472
   346
lemma isUCont_isCont: "isUCont f ==> isCont f x"
hoelzl@51472
   347
by (simp add: isUCont_def isCont_def LIM_def, force)
hoelzl@51472
   348
hoelzl@51472
   349
lemma isUCont_Cauchy:
hoelzl@51472
   350
  "\<lbrakk>isUCont f; Cauchy X\<rbrakk> \<Longrightarrow> Cauchy (\<lambda>n. f (X n))"
hoelzl@51472
   351
unfolding isUCont_def
hoelzl@51472
   352
apply (rule metric_CauchyI)
hoelzl@51472
   353
apply (drule_tac x=e in spec, safe)
hoelzl@51472
   354
apply (drule_tac e=s in metric_CauchyD, safe)
hoelzl@51472
   355
apply (rule_tac x=M in exI, simp)
hoelzl@51472
   356
done
hoelzl@51472
   357
hoelzl@51472
   358
subsection {* The set of real numbers is a complete metric space *}
hoelzl@51472
   359
hoelzl@51472
   360
instantiation real :: metric_space
hoelzl@51472
   361
begin
hoelzl@51472
   362
hoelzl@51472
   363
definition dist_real_def:
hoelzl@51472
   364
  "dist x y = \<bar>x - y\<bar>"
hoelzl@51472
   365
hoelzl@51472
   366
definition open_real_def:
hoelzl@51472
   367
  "open (S :: real set) \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
hoelzl@51472
   368
hoelzl@51472
   369
instance
hoelzl@51472
   370
  by default (auto simp: open_real_def dist_real_def)
hoelzl@51472
   371
end
hoelzl@51472
   372
hoelzl@51472
   373
instance real :: linorder_topology
hoelzl@51472
   374
proof
hoelzl@51472
   375
  show "(open :: real set \<Rightarrow> bool) = generate_topology (range lessThan \<union> range greaterThan)"
hoelzl@51472
   376
  proof (rule ext, safe)
hoelzl@51472
   377
    fix S :: "real set" assume "open S"
hoelzl@51472
   378
    then guess f unfolding open_real_def bchoice_iff ..
hoelzl@51472
   379
    then have *: "S = (\<Union>x\<in>S. {x - f x <..} \<inter> {..< x + f x})"
hoelzl@51472
   380
      by (fastforce simp: dist_real_def)
hoelzl@51472
   381
    show "generate_topology (range lessThan \<union> range greaterThan) S"
hoelzl@51472
   382
      apply (subst *)
hoelzl@51472
   383
      apply (intro generate_topology_Union generate_topology.Int)
hoelzl@51472
   384
      apply (auto intro: generate_topology.Basis)
hoelzl@51472
   385
      done
hoelzl@51472
   386
  next
hoelzl@51472
   387
    fix S :: "real set" assume "generate_topology (range lessThan \<union> range greaterThan) S"
hoelzl@51472
   388
    moreover have "\<And>a::real. open {..<a}"
hoelzl@51472
   389
      unfolding open_real_def dist_real_def
hoelzl@51472
   390
    proof clarify
hoelzl@51472
   391
      fix x a :: real assume "x < a"
hoelzl@51472
   392
      hence "0 < a - x \<and> (\<forall>y. \<bar>y - x\<bar> < a - x \<longrightarrow> y \<in> {..<a})" by auto
hoelzl@51472
   393
      thus "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {..<a}" ..
hoelzl@51472
   394
    qed
hoelzl@51472
   395
    moreover have "\<And>a::real. open {a <..}"
hoelzl@51472
   396
      unfolding open_real_def dist_real_def
hoelzl@51472
   397
    proof clarify
hoelzl@51472
   398
      fix x a :: real assume "a < x"
hoelzl@51472
   399
      hence "0 < x - a \<and> (\<forall>y. \<bar>y - x\<bar> < x - a \<longrightarrow> y \<in> {a<..})" by auto
hoelzl@51472
   400
      thus "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {a<..}" ..
hoelzl@51472
   401
    qed
hoelzl@51472
   402
    ultimately show "open S"
hoelzl@51472
   403
      by induct auto
hoelzl@51472
   404
  qed
hoelzl@51472
   405
qed
hoelzl@51472
   406
hoelzl@51472
   407
lemmas open_real_greaterThan = open_greaterThan[where 'a=real]
hoelzl@51472
   408
lemmas open_real_lessThan = open_lessThan[where 'a=real]
hoelzl@51472
   409
lemmas open_real_greaterThanLessThan = open_greaterThanLessThan[where 'a=real]
hoelzl@51472
   410
lemmas closed_real_atMost = closed_atMost[where 'a=real]
hoelzl@51472
   411
lemmas closed_real_atLeast = closed_atLeast[where 'a=real]
hoelzl@51472
   412
lemmas closed_real_atLeastAtMost = closed_atLeastAtMost[where 'a=real]
hoelzl@51472
   413
hoelzl@51472
   414
text {*
hoelzl@51472
   415
Proof that Cauchy sequences converge based on the one from
hoelzl@51472
   416
http://pirate.shu.edu/~wachsmut/ira/numseq/proofs/cauconv.html
hoelzl@51472
   417
*}
hoelzl@51472
   418
hoelzl@51472
   419
text {*
hoelzl@51472
   420
  If sequence @{term "X"} is Cauchy, then its limit is the lub of
hoelzl@51472
   421
  @{term "{r::real. \<exists>N. \<forall>n\<ge>N. r < X n}"}
hoelzl@51472
   422
*}
hoelzl@51472
   423
hoelzl@51472
   424
lemma isUb_UNIV_I: "(\<And>y. y \<in> S \<Longrightarrow> y \<le> u) \<Longrightarrow> isUb UNIV S u"
hoelzl@51472
   425
by (simp add: isUbI setleI)
hoelzl@51472
   426
hoelzl@51472
   427
lemma increasing_LIMSEQ:
hoelzl@51472
   428
  fixes f :: "nat \<Rightarrow> real"
hoelzl@51472
   429
  assumes inc: "\<And>n. f n \<le> f (Suc n)"
hoelzl@51472
   430
      and bdd: "\<And>n. f n \<le> l"
hoelzl@51472
   431
      and en: "\<And>e. 0 < e \<Longrightarrow> \<exists>n. l \<le> f n + e"
hoelzl@51472
   432
  shows "f ----> l"
hoelzl@51472
   433
proof (rule increasing_tendsto)
hoelzl@51472
   434
  fix x assume "x < l"
hoelzl@51472
   435
  with dense[of 0 "l - x"] obtain e where "0 < e" "e < l - x"
hoelzl@51472
   436
    by auto
hoelzl@51472
   437
  from en[OF `0 < e`] obtain n where "l - e \<le> f n"
hoelzl@51472
   438
    by (auto simp: field_simps)
hoelzl@51472
   439
  with `e < l - x` `0 < e` have "x < f n" by simp
hoelzl@51472
   440
  with incseq_SucI[of f, OF inc] show "eventually (\<lambda>n. x < f n) sequentially"
hoelzl@51472
   441
    by (auto simp: eventually_sequentially incseq_def intro: less_le_trans)
hoelzl@51472
   442
qed (insert bdd, auto)
hoelzl@51472
   443
hoelzl@51472
   444
lemma real_Cauchy_convergent:
hoelzl@51472
   445
  fixes X :: "nat \<Rightarrow> real"
hoelzl@51472
   446
  assumes X: "Cauchy X"
hoelzl@51472
   447
  shows "convergent X"
hoelzl@51472
   448
proof -
hoelzl@51472
   449
  def S \<equiv> "{x::real. \<exists>N. \<forall>n\<ge>N. x < X n}"
hoelzl@51472
   450
  then have mem_S: "\<And>N x. \<forall>n\<ge>N. x < X n \<Longrightarrow> x \<in> S" by auto
hoelzl@51472
   451
hoelzl@51472
   452
  { fix N x assume N: "\<forall>n\<ge>N. X n < x"
hoelzl@51472
   453
  have "isUb UNIV S x"
hoelzl@51472
   454
  proof (rule isUb_UNIV_I)
hoelzl@51472
   455
  fix y::real assume "y \<in> S"
hoelzl@51472
   456
  hence "\<exists>M. \<forall>n\<ge>M. y < X n"
hoelzl@51472
   457
    by (simp add: S_def)
hoelzl@51472
   458
  then obtain M where "\<forall>n\<ge>M. y < X n" ..
hoelzl@51472
   459
  hence "y < X (max M N)" by simp
hoelzl@51472
   460
  also have "\<dots> < x" using N by simp
hoelzl@51472
   461
  finally show "y \<le> x"
hoelzl@51472
   462
    by (rule order_less_imp_le)
hoelzl@51472
   463
  qed }
hoelzl@51472
   464
  note bound_isUb = this 
hoelzl@51472
   465
hoelzl@51472
   466
  have "\<exists>u. isLub UNIV S u"
hoelzl@51472
   467
  proof (rule reals_complete)
hoelzl@51472
   468
  obtain N where "\<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m) (X n) < 1"
hoelzl@51472
   469
    using X[THEN metric_CauchyD, OF zero_less_one] by auto
hoelzl@51472
   470
  hence N: "\<forall>n\<ge>N. dist (X n) (X N) < 1" by simp
hoelzl@51472
   471
  show "\<exists>x. x \<in> S"
hoelzl@51472
   472
  proof
hoelzl@51472
   473
    from N have "\<forall>n\<ge>N. X N - 1 < X n"
hoelzl@51472
   474
      by (simp add: abs_diff_less_iff dist_real_def)
hoelzl@51472
   475
    thus "X N - 1 \<in> S" by (rule mem_S)
hoelzl@51472
   476
  qed
hoelzl@51472
   477
  show "\<exists>u. isUb UNIV S u"
hoelzl@51472
   478
  proof
hoelzl@51472
   479
    from N have "\<forall>n\<ge>N. X n < X N + 1"
hoelzl@51472
   480
      by (simp add: abs_diff_less_iff dist_real_def)
hoelzl@51472
   481
    thus "isUb UNIV S (X N + 1)"
hoelzl@51472
   482
      by (rule bound_isUb)
hoelzl@51472
   483
  qed
hoelzl@51472
   484
  qed
hoelzl@51472
   485
  then obtain x where x: "isLub UNIV S x" ..
hoelzl@51472
   486
  have "X ----> x"
hoelzl@51472
   487
  proof (rule metric_LIMSEQ_I)
hoelzl@51472
   488
  fix r::real assume "0 < r"
hoelzl@51472
   489
  hence r: "0 < r/2" by simp
hoelzl@51472
   490
  obtain N where "\<forall>n\<ge>N. \<forall>m\<ge>N. dist (X n) (X m) < r/2"
hoelzl@51472
   491
    using metric_CauchyD [OF X r] by auto
hoelzl@51472
   492
  hence "\<forall>n\<ge>N. dist (X n) (X N) < r/2" by simp
hoelzl@51472
   493
  hence N: "\<forall>n\<ge>N. X N - r/2 < X n \<and> X n < X N + r/2"
hoelzl@51472
   494
    by (simp only: dist_real_def abs_diff_less_iff)
hoelzl@51472
   495
hoelzl@51472
   496
  from N have "\<forall>n\<ge>N. X N - r/2 < X n" by fast
hoelzl@51472
   497
  hence "X N - r/2 \<in> S" by (rule mem_S)
hoelzl@51472
   498
  hence 1: "X N - r/2 \<le> x" using x isLub_isUb isUbD by fast
hoelzl@51472
   499
hoelzl@51472
   500
  from N have "\<forall>n\<ge>N. X n < X N + r/2" by fast
hoelzl@51472
   501
  hence "isUb UNIV S (X N + r/2)" by (rule bound_isUb)
hoelzl@51472
   502
  hence 2: "x \<le> X N + r/2" using x isLub_le_isUb by fast
hoelzl@51472
   503
hoelzl@51472
   504
  show "\<exists>N. \<forall>n\<ge>N. dist (X n) x < r"
hoelzl@51472
   505
  proof (intro exI allI impI)
hoelzl@51472
   506
    fix n assume n: "N \<le> n"
hoelzl@51472
   507
    from N n have "X n < X N + r/2" and "X N - r/2 < X n" by simp+
hoelzl@51472
   508
    thus "dist (X n) x < r" using 1 2
hoelzl@51472
   509
      by (simp add: abs_diff_less_iff dist_real_def)
hoelzl@51472
   510
  qed
hoelzl@51472
   511
  qed
hoelzl@51472
   512
  then show ?thesis unfolding convergent_def by auto
hoelzl@51472
   513
qed
hoelzl@51472
   514
hoelzl@51472
   515
instance real :: complete_space
hoelzl@51472
   516
  by intro_classes (rule real_Cauchy_convergent)
hoelzl@51472
   517
hoelzl@51472
   518
lemma tendsto_dist [tendsto_intros]:
hoelzl@51472
   519
  fixes l m :: "'a :: metric_space"
hoelzl@51472
   520
  assumes f: "(f ---> l) F" and g: "(g ---> m) F"
hoelzl@51472
   521
  shows "((\<lambda>x. dist (f x) (g x)) ---> dist l m) F"
hoelzl@51472
   522
proof (rule tendstoI)
hoelzl@51472
   523
  fix e :: real assume "0 < e"
hoelzl@51472
   524
  hence e2: "0 < e/2" by simp
hoelzl@51472
   525
  from tendstoD [OF f e2] tendstoD [OF g e2]
hoelzl@51472
   526
  show "eventually (\<lambda>x. dist (dist (f x) (g x)) (dist l m) < e) F"
hoelzl@51472
   527
  proof (eventually_elim)
hoelzl@51472
   528
    case (elim x)
hoelzl@51472
   529
    then show "dist (dist (f x) (g x)) (dist l m) < e"
hoelzl@51472
   530
      unfolding dist_real_def
hoelzl@51472
   531
      using dist_triangle2 [of "f x" "g x" "l"]
hoelzl@51472
   532
      using dist_triangle2 [of "g x" "l" "m"]
hoelzl@51472
   533
      using dist_triangle3 [of "l" "m" "f x"]
hoelzl@51472
   534
      using dist_triangle [of "f x" "m" "g x"]
hoelzl@51472
   535
      by arith
hoelzl@51472
   536
  qed
hoelzl@51472
   537
qed
hoelzl@51472
   538
hoelzl@51472
   539
lemma tendsto_at_topI_sequentially:
hoelzl@51472
   540
  fixes f :: "real \<Rightarrow> real"
hoelzl@51472
   541
  assumes mono: "mono f"
hoelzl@51472
   542
  assumes limseq: "(\<lambda>n. f (real n)) ----> y"
hoelzl@51472
   543
  shows "(f ---> y) at_top"
hoelzl@51472
   544
proof (rule tendstoI)
hoelzl@51472
   545
  fix e :: real assume "0 < e"
hoelzl@51472
   546
  with limseq obtain N :: nat where N: "\<And>n. N \<le> n \<Longrightarrow> \<bar>f (real n) - y\<bar> < e"
hoelzl@51472
   547
    by (auto simp: LIMSEQ_def dist_real_def)
hoelzl@51472
   548
  { fix x :: real
hoelzl@51472
   549
    from ex_le_of_nat[of x] guess n ..
hoelzl@51472
   550
    note monoD[OF mono this]
hoelzl@51472
   551
    also have "f (real_of_nat n) \<le> y"
hoelzl@51472
   552
      by (rule LIMSEQ_le_const[OF limseq])
hoelzl@51472
   553
         (auto intro: exI[of _ n] monoD[OF mono] simp: real_eq_of_nat[symmetric])
hoelzl@51472
   554
    finally have "f x \<le> y" . }
hoelzl@51472
   555
  note le = this
hoelzl@51472
   556
  have "eventually (\<lambda>x. real N \<le> x) at_top"
hoelzl@51472
   557
    by (rule eventually_ge_at_top)
hoelzl@51472
   558
  then show "eventually (\<lambda>x. dist (f x) y < e) at_top"
hoelzl@51472
   559
  proof eventually_elim
hoelzl@51472
   560
    fix x assume N': "real N \<le> x"
hoelzl@51472
   561
    with N[of N] le have "y - f (real N) < e" by auto
hoelzl@51472
   562
    moreover note monoD[OF mono N']
hoelzl@51472
   563
    ultimately show "dist (f x) y < e"
hoelzl@51472
   564
      using le[of x] by (auto simp: dist_real_def field_simps)
hoelzl@51472
   565
  qed
hoelzl@51472
   566
qed
hoelzl@51472
   567
hoelzl@51472
   568
lemma Cauchy_iff2:
hoelzl@51472
   569
  "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
   570
  unfolding metric_Cauchy_iff2 dist_real_def ..
hoelzl@51472
   571
hoelzl@51472
   572
end
hoelzl@51472
   573