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