51472

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 

51473

103 
lemma open_ball: "open {y. dist x y < d}"

51472

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 

51473

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 

51472

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 
subsection {* Complete metric spaces *}


289 


290 
subsection {* Cauchy sequences *}


291 


292 
definition (in metric_space) Cauchy :: "(nat \<Rightarrow> 'a) \<Rightarrow> bool" where


293 
"Cauchy X = (\<forall>e>0. \<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. dist (X m) (X n) < e)"


294 


295 
subsection {* Cauchy Sequences *}


296 


297 
lemma metric_CauchyI:


298 
"(\<And>e. 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e) \<Longrightarrow> Cauchy X"


299 
by (simp add: Cauchy_def)


300 


301 
lemma metric_CauchyD:


302 
"Cauchy X \<Longrightarrow> 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e"


303 
by (simp add: Cauchy_def)


304 


305 
lemma metric_Cauchy_iff2:


306 
"Cauchy X = (\<forall>j. (\<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. dist (X m) (X n) < inverse(real (Suc j))))"


307 
apply (simp add: Cauchy_def, auto)


308 
apply (drule reals_Archimedean, safe)


309 
apply (drule_tac x = n in spec, auto)


310 
apply (rule_tac x = M in exI, auto)


311 
apply (drule_tac x = m in spec, simp)


312 
apply (drule_tac x = na in spec, auto)


313 
done


314 


315 
lemma Cauchy_subseq_Cauchy:


316 
"\<lbrakk> Cauchy X; subseq f \<rbrakk> \<Longrightarrow> Cauchy (X o f)"


317 
apply (auto simp add: Cauchy_def)


318 
apply (drule_tac x=e in spec, clarify)


319 
apply (rule_tac x=M in exI, clarify)


320 
apply (blast intro: le_trans [OF _ seq_suble] dest!: spec)


321 
done


322 


323 


324 
subsubsection {* Cauchy Sequences are Convergent *}


325 


326 
class complete_space = metric_space +


327 
assumes Cauchy_convergent: "Cauchy X \<Longrightarrow> convergent X"


328 


329 
theorem LIMSEQ_imp_Cauchy:


330 
assumes X: "X > a" shows "Cauchy X"


331 
proof (rule metric_CauchyI)


332 
fix e::real assume "0 < e"


333 
hence "0 < e/2" by simp


334 
with X have "\<exists>N. \<forall>n\<ge>N. dist (X n) a < e/2" by (rule metric_LIMSEQ_D)


335 
then obtain N where N: "\<forall>n\<ge>N. dist (X n) a < e/2" ..


336 
show "\<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m) (X n) < e"


337 
proof (intro exI allI impI)


338 
fix m assume "N \<le> m"


339 
hence m: "dist (X m) a < e/2" using N by fast


340 
fix n assume "N \<le> n"


341 
hence n: "dist (X n) a < e/2" using N by fast


342 
have "dist (X m) (X n) \<le> dist (X m) a + dist (X n) a"


343 
by (rule dist_triangle2)


344 
also from m n have "\<dots> < e" by simp


345 
finally show "dist (X m) (X n) < e" .


346 
qed


347 
qed


348 


349 
lemma convergent_Cauchy: "convergent X \<Longrightarrow> Cauchy X"


350 
unfolding convergent_def


351 
by (erule exE, erule LIMSEQ_imp_Cauchy)


352 


353 
lemma Cauchy_convergent_iff:


354 
fixes X :: "nat \<Rightarrow> 'a::complete_space"


355 
shows "Cauchy X = convergent X"


356 
by (fast intro: Cauchy_convergent convergent_Cauchy)


357 


358 
subsection {* Uniform Continuity *}


359 


360 
definition


361 
isUCont :: "['a::metric_space \<Rightarrow> 'b::metric_space] \<Rightarrow> bool" where


362 
"isUCont f = (\<forall>r>0. \<exists>s>0. \<forall>x y. dist x y < s \<longrightarrow> dist (f x) (f y) < r)"


363 


364 
lemma isUCont_isCont: "isUCont f ==> isCont f x"


365 
by (simp add: isUCont_def isCont_def LIM_def, force)


366 


367 
lemma isUCont_Cauchy:


368 
"\<lbrakk>isUCont f; Cauchy X\<rbrakk> \<Longrightarrow> Cauchy (\<lambda>n. f (X n))"


369 
unfolding isUCont_def


370 
apply (rule metric_CauchyI)


371 
apply (drule_tac x=e in spec, safe)


372 
apply (drule_tac e=s in metric_CauchyD, safe)


373 
apply (rule_tac x=M in exI, simp)


374 
done


375 


376 
subsection {* The set of real numbers is a complete metric space *}


377 


378 
instantiation real :: metric_space


379 
begin


380 


381 
definition dist_real_def:


382 
"dist x y = \<bar>x  y\<bar>"


383 


384 
definition open_real_def:


385 
"open (S :: real set) \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"


386 


387 
instance


388 
by default (auto simp: open_real_def dist_real_def)


389 
end


390 


391 
instance real :: linorder_topology


392 
proof


393 
show "(open :: real set \<Rightarrow> bool) = generate_topology (range lessThan \<union> range greaterThan)"


394 
proof (rule ext, safe)


395 
fix S :: "real set" assume "open S"


396 
then guess f unfolding open_real_def bchoice_iff ..


397 
then have *: "S = (\<Union>x\<in>S. {x  f x <..} \<inter> {..< x + f x})"


398 
by (fastforce simp: dist_real_def)


399 
show "generate_topology (range lessThan \<union> range greaterThan) S"


400 
apply (subst *)


401 
apply (intro generate_topology_Union generate_topology.Int)


402 
apply (auto intro: generate_topology.Basis)


403 
done


404 
next


405 
fix S :: "real set" assume "generate_topology (range lessThan \<union> range greaterThan) S"


406 
moreover have "\<And>a::real. open {..<a}"


407 
unfolding open_real_def dist_real_def


408 
proof clarify


409 
fix x a :: real assume "x < a"


410 
hence "0 < a  x \<and> (\<forall>y. \<bar>y  x\<bar> < a  x \<longrightarrow> y \<in> {..<a})" by auto


411 
thus "\<exists>e>0. \<forall>y. \<bar>y  x\<bar> < e \<longrightarrow> y \<in> {..<a}" ..


412 
qed


413 
moreover have "\<And>a::real. open {a <..}"


414 
unfolding open_real_def dist_real_def


415 
proof clarify


416 
fix x a :: real assume "a < x"


417 
hence "0 < x  a \<and> (\<forall>y. \<bar>y  x\<bar> < x  a \<longrightarrow> y \<in> {a<..})" by auto


418 
thus "\<exists>e>0. \<forall>y. \<bar>y  x\<bar> < e \<longrightarrow> y \<in> {a<..}" ..


419 
qed


420 
ultimately show "open S"


421 
by induct auto


422 
qed


423 
qed


424 


425 
lemmas open_real_greaterThan = open_greaterThan[where 'a=real]


426 
lemmas open_real_lessThan = open_lessThan[where 'a=real]


427 
lemmas open_real_greaterThanLessThan = open_greaterThanLessThan[where 'a=real]


428 
lemmas closed_real_atMost = closed_atMost[where 'a=real]


429 
lemmas closed_real_atLeast = closed_atLeast[where 'a=real]


430 
lemmas closed_real_atLeastAtMost = closed_atLeastAtMost[where 'a=real]


431 


432 
text {*


433 
Proof that Cauchy sequences converge based on the one from


434 
http://pirate.shu.edu/~wachsmut/ira/numseq/proofs/cauconv.html


435 
*}


436 


437 
text {*


438 
If sequence @{term "X"} is Cauchy, then its limit is the lub of


439 
@{term "{r::real. \<exists>N. \<forall>n\<ge>N. r < X n}"}


440 
*}


441 


442 
lemma isUb_UNIV_I: "(\<And>y. y \<in> S \<Longrightarrow> y \<le> u) \<Longrightarrow> isUb UNIV S u"


443 
by (simp add: isUbI setleI)


444 


445 
lemma increasing_LIMSEQ:


446 
fixes f :: "nat \<Rightarrow> real"


447 
assumes inc: "\<And>n. f n \<le> f (Suc n)"


448 
and bdd: "\<And>n. f n \<le> l"


449 
and en: "\<And>e. 0 < e \<Longrightarrow> \<exists>n. l \<le> f n + e"


450 
shows "f > l"


451 
proof (rule increasing_tendsto)


452 
fix x assume "x < l"


453 
with dense[of 0 "l  x"] obtain e where "0 < e" "e < l  x"


454 
by auto


455 
from en[OF `0 < e`] obtain n where "l  e \<le> f n"


456 
by (auto simp: field_simps)


457 
with `e < l  x` `0 < e` have "x < f n" by simp


458 
with incseq_SucI[of f, OF inc] show "eventually (\<lambda>n. x < f n) sequentially"


459 
by (auto simp: eventually_sequentially incseq_def intro: less_le_trans)


460 
qed (insert bdd, auto)


461 


462 
lemma real_Cauchy_convergent:


463 
fixes X :: "nat \<Rightarrow> real"


464 
assumes X: "Cauchy X"


465 
shows "convergent X"


466 
proof 


467 
def S \<equiv> "{x::real. \<exists>N. \<forall>n\<ge>N. x < X n}"


468 
then have mem_S: "\<And>N x. \<forall>n\<ge>N. x < X n \<Longrightarrow> x \<in> S" by auto


469 


470 
{ fix N x assume N: "\<forall>n\<ge>N. X n < x"


471 
have "isUb UNIV S x"


472 
proof (rule isUb_UNIV_I)


473 
fix y::real assume "y \<in> S"


474 
hence "\<exists>M. \<forall>n\<ge>M. y < X n"


475 
by (simp add: S_def)


476 
then obtain M where "\<forall>n\<ge>M. y < X n" ..


477 
hence "y < X (max M N)" by simp


478 
also have "\<dots> < x" using N by simp


479 
finally show "y \<le> x"


480 
by (rule order_less_imp_le)


481 
qed }


482 
note bound_isUb = this


483 


484 
have "\<exists>u. isLub UNIV S u"


485 
proof (rule reals_complete)


486 
obtain N where "\<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m) (X n) < 1"


487 
using X[THEN metric_CauchyD, OF zero_less_one] by auto


488 
hence N: "\<forall>n\<ge>N. dist (X n) (X N) < 1" by simp


489 
show "\<exists>x. x \<in> S"


490 
proof


491 
from N have "\<forall>n\<ge>N. X N  1 < X n"


492 
by (simp add: abs_diff_less_iff dist_real_def)


493 
thus "X N  1 \<in> S" by (rule mem_S)


494 
qed


495 
show "\<exists>u. isUb UNIV S u"


496 
proof


497 
from N have "\<forall>n\<ge>N. X n < X N + 1"


498 
by (simp add: abs_diff_less_iff dist_real_def)


499 
thus "isUb UNIV S (X N + 1)"


500 
by (rule bound_isUb)


501 
qed


502 
qed


503 
then obtain x where x: "isLub UNIV S x" ..


504 
have "X > x"


505 
proof (rule metric_LIMSEQ_I)


506 
fix r::real assume "0 < r"


507 
hence r: "0 < r/2" by simp


508 
obtain N where "\<forall>n\<ge>N. \<forall>m\<ge>N. dist (X n) (X m) < r/2"


509 
using metric_CauchyD [OF X r] by auto


510 
hence "\<forall>n\<ge>N. dist (X n) (X N) < r/2" by simp


511 
hence N: "\<forall>n\<ge>N. X N  r/2 < X n \<and> X n < X N + r/2"


512 
by (simp only: dist_real_def abs_diff_less_iff)


513 


514 
from N have "\<forall>n\<ge>N. X N  r/2 < X n" by fast


515 
hence "X N  r/2 \<in> S" by (rule mem_S)


516 
hence 1: "X N  r/2 \<le> x" using x isLub_isUb isUbD by fast


517 


518 
from N have "\<forall>n\<ge>N. X n < X N + r/2" by fast


519 
hence "isUb UNIV S (X N + r/2)" by (rule bound_isUb)


520 
hence 2: "x \<le> X N + r/2" using x isLub_le_isUb by fast


521 


522 
show "\<exists>N. \<forall>n\<ge>N. dist (X n) x < r"


523 
proof (intro exI allI impI)


524 
fix n assume n: "N \<le> n"


525 
from N n have "X n < X N + r/2" and "X N  r/2 < X n" by simp+


526 
thus "dist (X n) x < r" using 1 2


527 
by (simp add: abs_diff_less_iff dist_real_def)


528 
qed


529 
qed


530 
then show ?thesis unfolding convergent_def by auto


531 
qed


532 


533 
instance real :: complete_space


534 
by intro_classes (rule real_Cauchy_convergent)


535 


536 
lemma tendsto_dist [tendsto_intros]:


537 
fixes l m :: "'a :: metric_space"


538 
assumes f: "(f > l) F" and g: "(g > m) F"


539 
shows "((\<lambda>x. dist (f x) (g x)) > dist l m) F"


540 
proof (rule tendstoI)


541 
fix e :: real assume "0 < e"


542 
hence e2: "0 < e/2" by simp


543 
from tendstoD [OF f e2] tendstoD [OF g e2]


544 
show "eventually (\<lambda>x. dist (dist (f x) (g x)) (dist l m) < e) F"


545 
proof (eventually_elim)


546 
case (elim x)


547 
then show "dist (dist (f x) (g x)) (dist l m) < e"


548 
unfolding dist_real_def


549 
using dist_triangle2 [of "f x" "g x" "l"]


550 
using dist_triangle2 [of "g x" "l" "m"]


551 
using dist_triangle3 [of "l" "m" "f x"]


552 
using dist_triangle [of "f x" "m" "g x"]


553 
by arith


554 
qed


555 
qed


556 


557 
lemma tendsto_at_topI_sequentially:


558 
fixes f :: "real \<Rightarrow> real"


559 
assumes mono: "mono f"


560 
assumes limseq: "(\<lambda>n. f (real n)) > y"


561 
shows "(f > y) at_top"


562 
proof (rule tendstoI)


563 
fix e :: real assume "0 < e"


564 
with limseq obtain N :: nat where N: "\<And>n. N \<le> n \<Longrightarrow> \<bar>f (real n)  y\<bar> < e"


565 
by (auto simp: LIMSEQ_def dist_real_def)


566 
{ fix x :: real


567 
from ex_le_of_nat[of x] guess n ..


568 
note monoD[OF mono this]


569 
also have "f (real_of_nat n) \<le> y"


570 
by (rule LIMSEQ_le_const[OF limseq])


571 
(auto intro: exI[of _ n] monoD[OF mono] simp: real_eq_of_nat[symmetric])


572 
finally have "f x \<le> y" . }


573 
note le = this


574 
have "eventually (\<lambda>x. real N \<le> x) at_top"


575 
by (rule eventually_ge_at_top)


576 
then show "eventually (\<lambda>x. dist (f x) y < e) at_top"


577 
proof eventually_elim


578 
fix x assume N': "real N \<le> x"


579 
with N[of N] le have "y  f (real N) < e" by auto


580 
moreover note monoD[OF mono N']


581 
ultimately show "dist (f x) y < e"


582 
using le[of x] by (auto simp: dist_real_def field_simps)


583 
qed


584 
qed


585 


586 
lemma Cauchy_iff2:


587 
"Cauchy X = (\<forall>j. (\<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. \<bar>X m  X n\<bar> < inverse(real (Suc j))))"


588 
unfolding metric_Cauchy_iff2 dist_real_def ..


589 


590 
end


591 
