src/HOL/SEQ.thy
author wenzelm
Fri Dec 17 17:43:54 2010 +0100 (2010-12-17)
changeset 41229 d797baa3d57c
parent 40811 ab0a8cc7976a
child 41367 1b65137d598c
permissions -rw-r--r--
replaced command 'nonterminals' by slightly modernized version 'nonterminal';
     1 (*  Title:      HOL/SEQ.thy
     2     Author:     Jacques D. Fleuriot, University of Cambridge
     3     Author:     Lawrence C Paulson
     4     Author:     Jeremy Avigad
     5     Author:     Brian Huffman
     6 
     7 Convergence of sequences and series.
     8 *)
     9 
    10 header {* Sequences and Convergence *}
    11 
    12 theory SEQ
    13 imports Limits RComplete
    14 begin
    15 
    16 abbreviation
    17   LIMSEQ :: "[nat \<Rightarrow> 'a::topological_space, 'a] \<Rightarrow> bool"
    18     ("((_)/ ----> (_))" [60, 60] 60) where
    19   "X ----> L \<equiv> (X ---> L) sequentially"
    20 
    21 definition
    22   lim :: "(nat \<Rightarrow> 'a::metric_space) \<Rightarrow> 'a" where
    23     --{*Standard definition of limit using choice operator*}
    24   "lim X = (THE L. X ----> L)"
    25 
    26 definition
    27   convergent :: "(nat \<Rightarrow> 'a::metric_space) \<Rightarrow> bool" where
    28     --{*Standard definition of convergence*}
    29   "convergent X = (\<exists>L. X ----> L)"
    30 
    31 definition
    32   Bseq :: "(nat => 'a::real_normed_vector) => bool" where
    33     --{*Standard definition for bounded sequence*}
    34   "Bseq X = (\<exists>K>0.\<forall>n. norm (X n) \<le> K)"
    35 
    36 definition
    37   monoseq :: "(nat=>real)=>bool" where
    38     --{*Definition of monotonicity. 
    39         The use of disjunction here complicates proofs considerably. 
    40         One alternative is to add a Boolean argument to indicate the direction. 
    41         Another is to develop the notions of increasing and decreasing first.*}
    42   "monoseq X = ((\<forall>m. \<forall>n\<ge>m. X m \<le> X n) | (\<forall>m. \<forall>n\<ge>m. X n \<le> X m))"
    43 
    44 definition
    45   incseq :: "(nat=>real)=>bool" where
    46     --{*Increasing sequence*}
    47   "incseq X = (\<forall>m. \<forall>n\<ge>m. X m \<le> X n)"
    48 
    49 definition
    50   decseq :: "(nat=>real)=>bool" where
    51     --{*Increasing sequence*}
    52   "decseq X = (\<forall>m. \<forall>n\<ge>m. X n \<le> X m)"
    53 
    54 definition
    55   subseq :: "(nat => nat) => bool" where
    56     --{*Definition of subsequence*}
    57   "subseq f = (\<forall>m. \<forall>n>m. (f m) < (f n))"
    58 
    59 definition
    60   Cauchy :: "(nat \<Rightarrow> 'a::metric_space) \<Rightarrow> bool" where
    61     --{*Standard definition of the Cauchy condition*}
    62   "Cauchy X = (\<forall>e>0. \<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. dist (X m) (X n) < e)"
    63 
    64 
    65 subsection {* Bounded Sequences *}
    66 
    67 lemma BseqI': assumes K: "\<And>n. norm (X n) \<le> K" shows "Bseq X"
    68 unfolding Bseq_def
    69 proof (intro exI conjI allI)
    70   show "0 < max K 1" by simp
    71 next
    72   fix n::nat
    73   have "norm (X n) \<le> K" by (rule K)
    74   thus "norm (X n) \<le> max K 1" by simp
    75 qed
    76 
    77 lemma BseqE: "\<lbrakk>Bseq X; \<And>K. \<lbrakk>0 < K; \<forall>n. norm (X n) \<le> K\<rbrakk> \<Longrightarrow> Q\<rbrakk> \<Longrightarrow> Q"
    78 unfolding Bseq_def by auto
    79 
    80 lemma BseqI2': assumes K: "\<forall>n\<ge>N. norm (X n) \<le> K" shows "Bseq X"
    81 proof (rule BseqI')
    82   let ?A = "norm ` X ` {..N}"
    83   have 1: "finite ?A" by simp
    84   fix n::nat
    85   show "norm (X n) \<le> max K (Max ?A)"
    86   proof (cases rule: linorder_le_cases)
    87     assume "n \<ge> N"
    88     hence "norm (X n) \<le> K" using K by simp
    89     thus "norm (X n) \<le> max K (Max ?A)" by simp
    90   next
    91     assume "n \<le> N"
    92     hence "norm (X n) \<in> ?A" by simp
    93     with 1 have "norm (X n) \<le> Max ?A" by (rule Max_ge)
    94     thus "norm (X n) \<le> max K (Max ?A)" by simp
    95   qed
    96 qed
    97 
    98 lemma Bseq_ignore_initial_segment: "Bseq X \<Longrightarrow> Bseq (\<lambda>n. X (n + k))"
    99 unfolding Bseq_def by auto
   100 
   101 lemma Bseq_offset: "Bseq (\<lambda>n. X (n + k)) \<Longrightarrow> Bseq X"
   102 apply (erule BseqE)
   103 apply (rule_tac N="k" and K="K" in BseqI2')
   104 apply clarify
   105 apply (drule_tac x="n - k" in spec, simp)
   106 done
   107 
   108 lemma Bseq_conv_Bfun: "Bseq X \<longleftrightarrow> Bfun X sequentially"
   109 unfolding Bfun_def eventually_sequentially
   110 apply (rule iffI)
   111 apply (simp add: Bseq_def)
   112 apply (auto intro: BseqI2')
   113 done
   114 
   115 
   116 subsection {* Limits of Sequences *}
   117 
   118 lemma [trans]: "X=Y ==> Y ----> z ==> X ----> z"
   119   by simp
   120 
   121 lemma LIMSEQ_def: "X ----> L = (\<forall>r>0. \<exists>no. \<forall>n\<ge>no. dist (X n) L < r)"
   122 unfolding tendsto_iff eventually_sequentially ..
   123 
   124 lemma LIMSEQ_iff:
   125   fixes L :: "'a::real_normed_vector"
   126   shows "(X ----> L) = (\<forall>r>0. \<exists>no. \<forall>n \<ge> no. norm (X n - L) < r)"
   127 unfolding LIMSEQ_def dist_norm ..
   128 
   129 lemma LIMSEQ_iff_nz: "X ----> L = (\<forall>r>0. \<exists>no>0. \<forall>n\<ge>no. dist (X n) L < r)"
   130   unfolding LIMSEQ_def by (metis Suc_leD zero_less_Suc)
   131 
   132 lemma LIMSEQ_Zfun_iff: "((\<lambda>n. X n) ----> L) = Zfun (\<lambda>n. X n - L) sequentially"
   133 by (rule tendsto_Zfun_iff)
   134 
   135 lemma metric_LIMSEQ_I:
   136   "(\<And>r. 0 < r \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. dist (X n) L < r) \<Longrightarrow> X ----> L"
   137 by (simp add: LIMSEQ_def)
   138 
   139 lemma metric_LIMSEQ_D:
   140   "\<lbrakk>X ----> L; 0 < r\<rbrakk> \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. dist (X n) L < r"
   141 by (simp add: LIMSEQ_def)
   142 
   143 lemma LIMSEQ_I:
   144   fixes L :: "'a::real_normed_vector"
   145   shows "(\<And>r. 0 < r \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. norm (X n - L) < r) \<Longrightarrow> X ----> L"
   146 by (simp add: LIMSEQ_iff)
   147 
   148 lemma LIMSEQ_D:
   149   fixes L :: "'a::real_normed_vector"
   150   shows "\<lbrakk>X ----> L; 0 < r\<rbrakk> \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. norm (X n - L) < r"
   151 by (simp add: LIMSEQ_iff)
   152 
   153 lemma LIMSEQ_const: "(\<lambda>n. k) ----> k"
   154 by (rule tendsto_const)
   155 
   156 lemma LIMSEQ_const_iff:
   157   fixes k l :: "'a::metric_space"
   158   shows "(\<lambda>n. k) ----> l \<longleftrightarrow> k = l"
   159 by (rule tendsto_const_iff, rule sequentially_bot)
   160 
   161 lemma LIMSEQ_norm:
   162   fixes a :: "'a::real_normed_vector"
   163   shows "X ----> a \<Longrightarrow> (\<lambda>n. norm (X n)) ----> norm a"
   164 by (rule tendsto_norm)
   165 
   166 lemma LIMSEQ_ignore_initial_segment:
   167   "f ----> a \<Longrightarrow> (\<lambda>n. f (n + k)) ----> a"
   168 apply (rule topological_tendstoI)
   169 apply (drule (2) topological_tendstoD)
   170 apply (simp only: eventually_sequentially)
   171 apply (erule exE, rename_tac N)
   172 apply (rule_tac x=N in exI)
   173 apply simp
   174 done
   175 
   176 lemma LIMSEQ_offset:
   177   "(\<lambda>n. f (n + k)) ----> a \<Longrightarrow> f ----> a"
   178 apply (rule topological_tendstoI)
   179 apply (drule (2) topological_tendstoD)
   180 apply (simp only: eventually_sequentially)
   181 apply (erule exE, rename_tac N)
   182 apply (rule_tac x="N + k" in exI)
   183 apply clarify
   184 apply (drule_tac x="n - k" in spec)
   185 apply (simp add: le_diff_conv2)
   186 done
   187 
   188 lemma LIMSEQ_Suc: "f ----> l \<Longrightarrow> (\<lambda>n. f (Suc n)) ----> l"
   189 by (drule_tac k="Suc 0" in LIMSEQ_ignore_initial_segment, simp)
   190 
   191 lemma LIMSEQ_imp_Suc: "(\<lambda>n. f (Suc n)) ----> l \<Longrightarrow> f ----> l"
   192 by (rule_tac k="Suc 0" in LIMSEQ_offset, simp)
   193 
   194 lemma LIMSEQ_Suc_iff: "(\<lambda>n. f (Suc n)) ----> l = f ----> l"
   195 by (blast intro: LIMSEQ_imp_Suc LIMSEQ_Suc)
   196 
   197 lemma LIMSEQ_linear: "\<lbrakk> X ----> x ; l > 0 \<rbrakk> \<Longrightarrow> (\<lambda> n. X (n * l)) ----> x"
   198   unfolding tendsto_def eventually_sequentially
   199   by (metis div_le_dividend div_mult_self1_is_m le_trans nat_mult_commute)
   200 
   201 lemma LIMSEQ_add:
   202   fixes a b :: "'a::real_normed_vector"
   203   shows "\<lbrakk>X ----> a; Y ----> b\<rbrakk> \<Longrightarrow> (\<lambda>n. X n + Y n) ----> a + b"
   204 by (rule tendsto_add)
   205 
   206 lemma LIMSEQ_minus:
   207   fixes a :: "'a::real_normed_vector"
   208   shows "X ----> a \<Longrightarrow> (\<lambda>n. - X n) ----> - a"
   209 by (rule tendsto_minus)
   210 
   211 lemma LIMSEQ_minus_cancel:
   212   fixes a :: "'a::real_normed_vector"
   213   shows "(\<lambda>n. - X n) ----> - a \<Longrightarrow> X ----> a"
   214 by (rule tendsto_minus_cancel)
   215 
   216 lemma LIMSEQ_diff:
   217   fixes a b :: "'a::real_normed_vector"
   218   shows "\<lbrakk>X ----> a; Y ----> b\<rbrakk> \<Longrightarrow> (\<lambda>n. X n - Y n) ----> a - b"
   219 by (rule tendsto_diff)
   220 
   221 lemma LIMSEQ_unique:
   222   fixes a b :: "'a::metric_space"
   223   shows "\<lbrakk>X ----> a; X ----> b\<rbrakk> \<Longrightarrow> a = b"
   224 by (drule (1) tendsto_dist, simp add: LIMSEQ_const_iff)
   225 
   226 lemma (in bounded_linear) LIMSEQ:
   227   "X ----> a \<Longrightarrow> (\<lambda>n. f (X n)) ----> f a"
   228 by (rule tendsto)
   229 
   230 lemma (in bounded_bilinear) LIMSEQ:
   231   "\<lbrakk>X ----> a; Y ----> b\<rbrakk> \<Longrightarrow> (\<lambda>n. X n ** Y n) ----> a ** b"
   232 by (rule tendsto)
   233 
   234 lemma LIMSEQ_mult:
   235   fixes a b :: "'a::real_normed_algebra"
   236   shows "[| X ----> a; Y ----> b |] ==> (%n. X n * Y n) ----> a * b"
   237 by (rule mult.tendsto)
   238 
   239 lemma increasing_LIMSEQ:
   240   fixes f :: "nat \<Rightarrow> real"
   241   assumes inc: "!!n. f n \<le> f (Suc n)"
   242       and bdd: "!!n. f n \<le> l"
   243       and en: "!!e. 0 < e \<Longrightarrow> \<exists>n. l \<le> f n + e"
   244   shows "f ----> l"
   245 proof (auto simp add: LIMSEQ_def)
   246   fix e :: real
   247   assume e: "0 < e"
   248   then obtain N where "l \<le> f N + e/2"
   249     by (metis half_gt_zero e en that)
   250   hence N: "l < f N + e" using e
   251     by simp
   252   { fix k
   253     have [simp]: "!!n. \<bar>f n - l\<bar> = l - f n"
   254       by (simp add: bdd) 
   255     have "\<bar>f (N+k) - l\<bar> < e"
   256     proof (induct k)
   257       case 0 show ?case using N
   258         by simp   
   259     next
   260       case (Suc k) thus ?case using N inc [of "N+k"]
   261         by simp
   262     qed 
   263   } note 1 = this
   264   { fix n
   265     have "N \<le> n \<Longrightarrow> \<bar>f n - l\<bar> < e" using 1 [of "n-N"]
   266       by simp 
   267   } note [intro] = this
   268   show " \<exists>no. \<forall>n\<ge>no. dist (f n) l < e"
   269     by (auto simp add: dist_real_def) 
   270   qed
   271 
   272 lemma Bseq_inverse_lemma:
   273   fixes x :: "'a::real_normed_div_algebra"
   274   shows "\<lbrakk>r \<le> norm x; 0 < r\<rbrakk> \<Longrightarrow> norm (inverse x) \<le> inverse r"
   275 apply (subst nonzero_norm_inverse, clarsimp)
   276 apply (erule (1) le_imp_inverse_le)
   277 done
   278 
   279 lemma Bseq_inverse:
   280   fixes a :: "'a::real_normed_div_algebra"
   281   shows "\<lbrakk>X ----> a; a \<noteq> 0\<rbrakk> \<Longrightarrow> Bseq (\<lambda>n. inverse (X n))"
   282 unfolding Bseq_conv_Bfun by (rule Bfun_inverse)
   283 
   284 lemma LIMSEQ_inverse:
   285   fixes a :: "'a::real_normed_div_algebra"
   286   shows "\<lbrakk>X ----> a; a \<noteq> 0\<rbrakk> \<Longrightarrow> (\<lambda>n. inverse (X n)) ----> inverse a"
   287 by (rule tendsto_inverse)
   288 
   289 lemma LIMSEQ_divide:
   290   fixes a b :: "'a::real_normed_field"
   291   shows "\<lbrakk>X ----> a; Y ----> b; b \<noteq> 0\<rbrakk> \<Longrightarrow> (\<lambda>n. X n / Y n) ----> a / b"
   292 by (rule tendsto_divide)
   293 
   294 lemma LIMSEQ_pow:
   295   fixes a :: "'a::{power, real_normed_algebra}"
   296   shows "X ----> a \<Longrightarrow> (\<lambda>n. (X n) ^ m) ----> a ^ m"
   297 by (induct m) (simp_all add: LIMSEQ_const LIMSEQ_mult)
   298 
   299 lemma LIMSEQ_setsum:
   300   fixes L :: "'a \<Rightarrow> 'b::real_normed_vector"
   301   assumes n: "\<And>n. n \<in> S \<Longrightarrow> X n ----> L n"
   302   shows "(\<lambda>m. \<Sum>n\<in>S. X n m) ----> (\<Sum>n\<in>S. L n)"
   303 using assms by (rule tendsto_setsum)
   304 
   305 lemma LIMSEQ_setprod:
   306   fixes L :: "'a \<Rightarrow> 'b::{real_normed_algebra,comm_ring_1}"
   307   assumes n: "\<And>n. n \<in> S \<Longrightarrow> X n ----> L n"
   308   shows "(\<lambda>m. \<Prod>n\<in>S. X n m) ----> (\<Prod>n\<in>S. L n)"
   309 proof (cases "finite S")
   310   case True
   311   thus ?thesis using n
   312   proof (induct)
   313     case empty
   314     show ?case
   315       by (simp add: LIMSEQ_const)
   316   next
   317     case insert
   318     thus ?case
   319       by (simp add: LIMSEQ_mult)
   320   qed
   321 next
   322   case False
   323   thus ?thesis
   324     by (simp add: setprod_def LIMSEQ_const)
   325 qed
   326 
   327 lemma LIMSEQ_add_const: (* FIXME: delete *)
   328   fixes a :: "'a::real_normed_vector"
   329   shows "f ----> a ==> (%n.(f n + b)) ----> a + b"
   330 by (intro tendsto_intros)
   331 
   332 (* FIXME: delete *)
   333 lemma LIMSEQ_add_minus:
   334   fixes a b :: "'a::real_normed_vector"
   335   shows "[| X ----> a; Y ----> b |] ==> (%n. X n + -Y n) ----> a + -b"
   336 by (intro tendsto_intros)
   337 
   338 lemma LIMSEQ_diff_const: (* FIXME: delete *)
   339   fixes a b :: "'a::real_normed_vector"
   340   shows "f ----> a ==> (%n.(f n  - b)) ----> a - b"
   341 by (intro tendsto_intros)
   342 
   343 lemma LIMSEQ_diff_approach_zero:
   344   fixes L :: "'a::real_normed_vector"
   345   shows "g ----> L ==> (%x. f x - g x) ----> 0 ==> f ----> L"
   346 by (drule (1) LIMSEQ_add, simp)
   347 
   348 lemma LIMSEQ_diff_approach_zero2:
   349   fixes L :: "'a::real_normed_vector"
   350   shows "f ----> L ==> (%x. f x - g x) ----> 0 ==> g ----> L"
   351 by (drule (1) LIMSEQ_diff, simp)
   352 
   353 text{*A sequence tends to zero iff its abs does*}
   354 lemma LIMSEQ_norm_zero:
   355   fixes X :: "nat \<Rightarrow> 'a::real_normed_vector"
   356   shows "((\<lambda>n. norm (X n)) ----> 0) \<longleftrightarrow> (X ----> 0)"
   357 by (simp add: LIMSEQ_iff)
   358 
   359 lemma LIMSEQ_rabs_zero: "((%n. \<bar>f n\<bar>) ----> 0) = (f ----> (0::real))"
   360 by (simp add: LIMSEQ_iff)
   361 
   362 lemma LIMSEQ_imp_rabs: "f ----> (l::real) ==> (%n. \<bar>f n\<bar>) ----> \<bar>l\<bar>"
   363 by (drule LIMSEQ_norm, simp)
   364 
   365 text{*An unbounded sequence's inverse tends to 0*}
   366 
   367 lemma LIMSEQ_inverse_zero:
   368   "\<forall>r::real. \<exists>N. \<forall>n\<ge>N. r < X n \<Longrightarrow> (\<lambda>n. inverse (X n)) ----> 0"
   369 apply (rule LIMSEQ_I)
   370 apply (drule_tac x="inverse r" in spec, safe)
   371 apply (rule_tac x="N" in exI, safe)
   372 apply (drule_tac x="n" in spec, safe)
   373 apply (frule positive_imp_inverse_positive)
   374 apply (frule (1) less_imp_inverse_less)
   375 apply (subgoal_tac "0 < X n", simp)
   376 apply (erule (1) order_less_trans)
   377 done
   378 
   379 text{*The sequence @{term "1/n"} tends to 0 as @{term n} tends to infinity*}
   380 
   381 lemma LIMSEQ_inverse_real_of_nat: "(%n. inverse(real(Suc n))) ----> 0"
   382 apply (rule LIMSEQ_inverse_zero, safe)
   383 apply (cut_tac x = r in reals_Archimedean2)
   384 apply (safe, rule_tac x = n in exI)
   385 apply (auto simp add: real_of_nat_Suc)
   386 done
   387 
   388 text{*The sequence @{term "r + 1/n"} tends to @{term r} as @{term n} tends to
   389 infinity is now easily proved*}
   390 
   391 lemma LIMSEQ_inverse_real_of_nat_add:
   392      "(%n. r + inverse(real(Suc n))) ----> r"
   393 by (cut_tac LIMSEQ_add [OF LIMSEQ_const LIMSEQ_inverse_real_of_nat], auto)
   394 
   395 lemma LIMSEQ_inverse_real_of_nat_add_minus:
   396      "(%n. r + -inverse(real(Suc n))) ----> r"
   397 by (cut_tac LIMSEQ_add_minus [OF LIMSEQ_const LIMSEQ_inverse_real_of_nat], auto)
   398 
   399 lemma LIMSEQ_inverse_real_of_nat_add_minus_mult:
   400      "(%n. r*( 1 + -inverse(real(Suc n)))) ----> r"
   401 by (cut_tac b=1 in
   402         LIMSEQ_mult [OF LIMSEQ_const LIMSEQ_inverse_real_of_nat_add_minus], auto)
   403 
   404 lemma LIMSEQ_le_const:
   405   "\<lbrakk>X ----> (x::real); \<exists>N. \<forall>n\<ge>N. a \<le> X n\<rbrakk> \<Longrightarrow> a \<le> x"
   406 apply (rule ccontr, simp only: linorder_not_le)
   407 apply (drule_tac r="a - x" in LIMSEQ_D, simp)
   408 apply clarsimp
   409 apply (drule_tac x="max N no" in spec, drule mp, rule le_maxI1)
   410 apply (drule_tac x="max N no" in spec, drule mp, rule le_maxI2)
   411 apply simp
   412 done
   413 
   414 lemma LIMSEQ_le_const2:
   415   "\<lbrakk>X ----> (x::real); \<exists>N. \<forall>n\<ge>N. X n \<le> a\<rbrakk> \<Longrightarrow> x \<le> a"
   416 apply (subgoal_tac "- a \<le> - x", simp)
   417 apply (rule LIMSEQ_le_const)
   418 apply (erule LIMSEQ_minus)
   419 apply simp
   420 done
   421 
   422 lemma LIMSEQ_le:
   423   "\<lbrakk>X ----> x; Y ----> y; \<exists>N. \<forall>n\<ge>N. X n \<le> Y n\<rbrakk> \<Longrightarrow> x \<le> (y::real)"
   424 apply (subgoal_tac "0 \<le> y - x", simp)
   425 apply (rule LIMSEQ_le_const)
   426 apply (erule (1) LIMSEQ_diff)
   427 apply (simp add: le_diff_eq)
   428 done
   429 
   430 
   431 subsection {* Convergence *}
   432 
   433 lemma limI: "X ----> L ==> lim X = L"
   434 apply (simp add: lim_def)
   435 apply (blast intro: LIMSEQ_unique)
   436 done
   437 
   438 lemma convergentD: "convergent X ==> \<exists>L. (X ----> L)"
   439 by (simp add: convergent_def)
   440 
   441 lemma convergentI: "(X ----> L) ==> convergent X"
   442 by (auto simp add: convergent_def)
   443 
   444 lemma convergent_LIMSEQ_iff: "convergent X = (X ----> lim X)"
   445 by (auto intro: theI LIMSEQ_unique simp add: convergent_def lim_def)
   446 
   447 lemma convergent_const: "convergent (\<lambda>n. c)"
   448 by (rule convergentI, rule LIMSEQ_const)
   449 
   450 lemma convergent_add:
   451   fixes X Y :: "nat \<Rightarrow> 'a::real_normed_vector"
   452   assumes "convergent (\<lambda>n. X n)"
   453   assumes "convergent (\<lambda>n. Y n)"
   454   shows "convergent (\<lambda>n. X n + Y n)"
   455 using assms unfolding convergent_def by (fast intro: LIMSEQ_add)
   456 
   457 lemma convergent_setsum:
   458   fixes X :: "'a \<Rightarrow> nat \<Rightarrow> 'b::real_normed_vector"
   459   assumes "\<And>i. i \<in> A \<Longrightarrow> convergent (\<lambda>n. X i n)"
   460   shows "convergent (\<lambda>n. \<Sum>i\<in>A. X i n)"
   461 proof (cases "finite A")
   462   case True from this and assms show ?thesis
   463     by (induct A set: finite) (simp_all add: convergent_const convergent_add)
   464 qed (simp add: convergent_const)
   465 
   466 lemma (in bounded_linear) convergent:
   467   assumes "convergent (\<lambda>n. X n)"
   468   shows "convergent (\<lambda>n. f (X n))"
   469 using assms unfolding convergent_def by (fast intro: LIMSEQ)
   470 
   471 lemma (in bounded_bilinear) convergent:
   472   assumes "convergent (\<lambda>n. X n)" and "convergent (\<lambda>n. Y n)"
   473   shows "convergent (\<lambda>n. X n ** Y n)"
   474 using assms unfolding convergent_def by (fast intro: LIMSEQ)
   475 
   476 lemma convergent_minus_iff:
   477   fixes X :: "nat \<Rightarrow> 'a::real_normed_vector"
   478   shows "convergent X \<longleftrightarrow> convergent (\<lambda>n. - X n)"
   479 apply (simp add: convergent_def)
   480 apply (auto dest: LIMSEQ_minus)
   481 apply (drule LIMSEQ_minus, auto)
   482 done
   483 
   484 lemma lim_le:
   485   fixes x :: real
   486   assumes f: "convergent f" and fn_le: "!!n. f n \<le> x"
   487   shows "lim f \<le> x"
   488 proof (rule classical)
   489   assume "\<not> lim f \<le> x"
   490   hence 0: "0 < lim f - x" by arith
   491   have 1: "f----> lim f"
   492     by (metis convergent_LIMSEQ_iff f) 
   493   thus ?thesis
   494     proof (simp add: LIMSEQ_iff)
   495       assume "\<forall>r>0. \<exists>no. \<forall>n\<ge>no. \<bar>f n - lim f\<bar> < r"
   496       hence "\<exists>no. \<forall>n\<ge>no. \<bar>f n - lim f\<bar> < lim f - x"
   497         by (metis 0)
   498       from this obtain no where "\<forall>n\<ge>no. \<bar>f n - lim f\<bar> < lim f - x"
   499         by blast
   500       thus "lim f \<le> x"
   501         by (metis 1 LIMSEQ_le_const2 fn_le)
   502     qed
   503 qed
   504 
   505 text{* Given a binary function @{text "f:: nat \<Rightarrow> 'a \<Rightarrow> 'a"}, its values are uniquely determined by a function g *}
   506 
   507 lemma nat_function_unique: "EX! g. g 0 = e \<and> (\<forall>n. g (Suc n) = f n (g n))"
   508   unfolding Ex1_def
   509   apply (rule_tac x="nat_rec e f" in exI)
   510   apply (rule conjI)+
   511 apply (rule def_nat_rec_0, simp)
   512 apply (rule allI, rule def_nat_rec_Suc, simp)
   513 apply (rule allI, rule impI, rule ext)
   514 apply (erule conjE)
   515 apply (induct_tac x)
   516 apply simp
   517 apply (erule_tac x="n" in allE)
   518 apply (simp)
   519 done
   520 
   521 text{*Subsequence (alternative definition, (e.g. Hoskins)*}
   522 
   523 lemma subseq_Suc_iff: "subseq f = (\<forall>n. (f n) < (f (Suc n)))"
   524 apply (simp add: subseq_def)
   525 apply (auto dest!: less_imp_Suc_add)
   526 apply (induct_tac k)
   527 apply (auto intro: less_trans)
   528 done
   529 
   530 lemma monoseq_Suc:
   531    "monoseq X = ((\<forall>n. X n \<le> X (Suc n))
   532                  | (\<forall>n. X (Suc n) \<le> X n))"
   533 apply (simp add: monoseq_def)
   534 apply (auto dest!: le_imp_less_or_eq)
   535 apply (auto intro!: lessI [THEN less_imp_le] dest!: less_imp_Suc_add)
   536 apply (induct_tac "ka")
   537 apply (auto intro: order_trans)
   538 apply (erule contrapos_np)
   539 apply (induct_tac "k")
   540 apply (auto intro: order_trans)
   541 done
   542 
   543 lemma monoI1: "\<forall>m. \<forall> n \<ge> m. X m \<le> X n ==> monoseq X"
   544 by (simp add: monoseq_def)
   545 
   546 lemma monoI2: "\<forall>m. \<forall> n \<ge> m. X n \<le> X m ==> monoseq X"
   547 by (simp add: monoseq_def)
   548 
   549 lemma mono_SucI1: "\<forall>n. X n \<le> X (Suc n) ==> monoseq X"
   550 by (simp add: monoseq_Suc)
   551 
   552 lemma mono_SucI2: "\<forall>n. X (Suc n) \<le> X n ==> monoseq X"
   553 by (simp add: monoseq_Suc)
   554 
   555 lemma monoseq_minus: assumes "monoseq a"
   556   shows "monoseq (\<lambda> n. - a n)"
   557 proof (cases "\<forall> m. \<forall> n \<ge> m. a m \<le> a n")
   558   case True
   559   hence "\<forall> m. \<forall> n \<ge> m. - a n \<le> - a m" by auto
   560   thus ?thesis by (rule monoI2)
   561 next
   562   case False
   563   hence "\<forall> m. \<forall> n \<ge> m. - a m \<le> - a n" using `monoseq a`[unfolded monoseq_def] by auto
   564   thus ?thesis by (rule monoI1)
   565 qed
   566 
   567 lemma monoseq_le: assumes "monoseq a" and "a ----> x"
   568   shows "((\<forall> n. a n \<le> x) \<and> (\<forall>m. \<forall>n\<ge>m. a m \<le> a n)) \<or> 
   569          ((\<forall> n. x \<le> a n) \<and> (\<forall>m. \<forall>n\<ge>m. a n \<le> a m))"
   570 proof -
   571   { fix x n fix a :: "nat \<Rightarrow> real"
   572     assume "a ----> x" and "\<forall> m. \<forall> n \<ge> m. a m \<le> a n"
   573     hence monotone: "\<And> m n. m \<le> n \<Longrightarrow> a m \<le> a n" by auto
   574     have "a n \<le> x"
   575     proof (rule ccontr)
   576       assume "\<not> a n \<le> x" hence "x < a n" by auto
   577       hence "0 < a n - x" by auto
   578       from `a ----> x`[THEN LIMSEQ_D, OF this]
   579       obtain no where "\<And>n'. no \<le> n' \<Longrightarrow> norm (a n' - x) < a n - x" by blast
   580       hence "norm (a (max no n) - x) < a n - x" by auto
   581       moreover
   582       { fix n' have "n \<le> n' \<Longrightarrow> x < a n'" using monotone[where m=n and n=n'] and `x < a n` by auto }
   583       hence "x < a (max no n)" by auto
   584       ultimately
   585       have "a (max no n) < a n" by auto
   586       with monotone[where m=n and n="max no n"]
   587       show False by (auto simp:max_def split:split_if_asm)
   588     qed
   589   } note top_down = this
   590   { fix x n m fix a :: "nat \<Rightarrow> real"
   591     assume "a ----> x" and "monoseq a" and "a m < x"
   592     have "a n \<le> x \<and> (\<forall> m. \<forall> n \<ge> m. a m \<le> a n)"
   593     proof (cases "\<forall> m. \<forall> n \<ge> m. a m \<le> a n")
   594       case True with top_down and `a ----> x` show ?thesis by auto
   595     next
   596       case False with `monoseq a`[unfolded monoseq_def] have "\<forall> m. \<forall> n \<ge> m. - a m \<le> - a n" by auto
   597       hence "- a m \<le> - x" using top_down[OF LIMSEQ_minus[OF `a ----> x`]] by blast
   598       hence False using `a m < x` by auto
   599       thus ?thesis ..
   600     qed
   601   } note when_decided = this
   602 
   603   show ?thesis
   604   proof (cases "\<exists> m. a m \<noteq> x")
   605     case True then obtain m where "a m \<noteq> x" by auto
   606     show ?thesis
   607     proof (cases "a m < x")
   608       case True with when_decided[OF `a ----> x` `monoseq a`, where m2=m]
   609       show ?thesis by blast
   610     next
   611       case False hence "- a m < - x" using `a m \<noteq> x` by auto
   612       with when_decided[OF LIMSEQ_minus[OF `a ----> x`] monoseq_minus[OF `monoseq a`], where m2=m]
   613       show ?thesis by auto
   614     qed
   615   qed auto
   616 qed
   617 
   618 text{* for any sequence, there is a mootonic subsequence *}
   619 lemma seq_monosub: "\<exists>f. subseq f \<and> monoseq (\<lambda> n. (s (f n)))"
   620 proof-
   621   {assume H: "\<forall>n. \<exists>p >n. \<forall> m\<ge>p. s m \<le> s p"
   622     let ?P = "\<lambda> p n. p > n \<and> (\<forall>m \<ge> p. s m \<le> s p)"
   623     from nat_function_unique[of "SOME p. ?P p 0" "\<lambda>p n. SOME p. ?P p n"]
   624     obtain f where f: "f 0 = (SOME p. ?P p 0)" "\<forall>n. f (Suc n) = (SOME p. ?P p (f n))" by blast
   625     have "?P (f 0) 0"  unfolding f(1) some_eq_ex[of "\<lambda>p. ?P p 0"]
   626       using H apply - 
   627       apply (erule allE[where x=0], erule exE, rule_tac x="p" in exI) 
   628       unfolding order_le_less by blast 
   629     hence f0: "f 0 > 0" "\<forall>m \<ge> f 0. s m \<le> s (f 0)" by blast+
   630     {fix n
   631       have "?P (f (Suc n)) (f n)" 
   632         unfolding f(2)[rule_format, of n] some_eq_ex[of "\<lambda>p. ?P p (f n)"]
   633         using H apply - 
   634       apply (erule allE[where x="f n"], erule exE, rule_tac x="p" in exI) 
   635       unfolding order_le_less by blast 
   636     hence "f (Suc n) > f n" "\<forall>m \<ge> f (Suc n). s m \<le> s (f (Suc n))" by blast+}
   637   note fSuc = this
   638     {fix p q assume pq: "p \<ge> f q"
   639       have "s p \<le> s(f(q))"  using f0(2)[rule_format, of p] pq fSuc
   640         by (cases q, simp_all) }
   641     note pqth = this
   642     {fix q
   643       have "f (Suc q) > f q" apply (induct q) 
   644         using f0(1) fSuc(1)[of 0] apply simp by (rule fSuc(1))}
   645     note fss = this
   646     from fss have th1: "subseq f" unfolding subseq_Suc_iff ..
   647     {fix a b 
   648       have "f a \<le> f (a + b)"
   649       proof(induct b)
   650         case 0 thus ?case by simp
   651       next
   652         case (Suc b)
   653         from fSuc(1)[of "a + b"] Suc.hyps show ?case by simp
   654       qed}
   655     note fmon0 = this
   656     have "monoseq (\<lambda>n. s (f n))" 
   657     proof-
   658       {fix n
   659         have "s (f n) \<ge> s (f (Suc n))" 
   660         proof(cases n)
   661           case 0
   662           assume n0: "n = 0"
   663           from fSuc(1)[of 0] have th0: "f 0 \<le> f (Suc 0)" by simp
   664           from f0(2)[rule_format, OF th0] show ?thesis  using n0 by simp
   665         next
   666           case (Suc m)
   667           assume m: "n = Suc m"
   668           from fSuc(1)[of n] m have th0: "f (Suc m) \<le> f (Suc (Suc m))" by simp
   669           from m fSuc(2)[rule_format, OF th0] show ?thesis by simp 
   670         qed}
   671       thus "monoseq (\<lambda>n. s (f n))" unfolding monoseq_Suc by blast 
   672     qed
   673     with th1 have ?thesis by blast}
   674   moreover
   675   {fix N assume N: "\<forall>p >N. \<exists> m\<ge>p. s m > s p"
   676     {fix p assume p: "p \<ge> Suc N" 
   677       hence pN: "p > N" by arith with N obtain m where m: "m \<ge> p" "s m > s p" by blast
   678       have "m \<noteq> p" using m(2) by auto 
   679       with m have "\<exists>m>p. s p < s m" by - (rule exI[where x=m], auto)}
   680     note th0 = this
   681     let ?P = "\<lambda>m x. m > x \<and> s x < s m"
   682     from nat_function_unique[of "SOME x. ?P x (Suc N)" "\<lambda>m x. SOME y. ?P y x"]
   683     obtain f where f: "f 0 = (SOME x. ?P x (Suc N))" 
   684       "\<forall>n. f (Suc n) = (SOME m. ?P m (f n))" by blast
   685     have "?P (f 0) (Suc N)"  unfolding f(1) some_eq_ex[of "\<lambda>p. ?P p (Suc N)"]
   686       using N apply - 
   687       apply (erule allE[where x="Suc N"], clarsimp)
   688       apply (rule_tac x="m" in exI)
   689       apply auto
   690       apply (subgoal_tac "Suc N \<noteq> m")
   691       apply simp
   692       apply (rule ccontr, simp)
   693       done
   694     hence f0: "f 0 > Suc N" "s (Suc N) < s (f 0)" by blast+
   695     {fix n
   696       have "f n > N \<and> ?P (f (Suc n)) (f n)"
   697         unfolding f(2)[rule_format, of n] some_eq_ex[of "\<lambda>p. ?P p (f n)"]
   698       proof (induct n)
   699         case 0 thus ?case
   700           using f0 N apply auto 
   701           apply (erule allE[where x="f 0"], clarsimp) 
   702           apply (rule_tac x="m" in exI, simp)
   703           by (subgoal_tac "f 0 \<noteq> m", auto)
   704       next
   705         case (Suc n)
   706         from Suc.hyps have Nfn: "N < f n" by blast
   707         from Suc.hyps obtain m where m: "m > f n" "s (f n) < s m" by blast
   708         with Nfn have mN: "m > N" by arith
   709         note key = Suc.hyps[unfolded some_eq_ex[of "\<lambda>p. ?P p (f n)", symmetric] f(2)[rule_format, of n, symmetric]]
   710         
   711         from key have th0: "f (Suc n) > N" by simp
   712         from N[rule_format, OF th0]
   713         obtain m' where m': "m' \<ge> f (Suc n)" "s (f (Suc n)) < s m'" by blast
   714         have "m' \<noteq> f (Suc (n))" apply (rule ccontr) using m'(2) by auto
   715         hence "m' > f (Suc n)" using m'(1) by simp
   716         with key m'(2) show ?case by auto
   717       qed}
   718     note fSuc = this
   719     {fix n
   720       have "f n \<ge> Suc N \<and> f(Suc n) > f n \<and> s(f n) < s(f(Suc n))" using fSuc[of n] by auto 
   721       hence "f n \<ge> Suc N" "f(Suc n) > f n" "s(f n) < s(f(Suc n))" by blast+}
   722     note thf = this
   723     have sqf: "subseq f" unfolding subseq_Suc_iff using thf by simp
   724     have "monoseq (\<lambda>n. s (f n))"  unfolding monoseq_Suc using thf
   725       apply -
   726       apply (rule disjI1)
   727       apply auto
   728       apply (rule order_less_imp_le)
   729       apply blast
   730       done
   731     then have ?thesis  using sqf by blast}
   732   ultimately show ?thesis unfolding linorder_not_less[symmetric] by blast
   733 qed
   734 
   735 lemma seq_suble: assumes sf: "subseq f" shows "n \<le> f n"
   736 proof(induct n)
   737   case 0 thus ?case by simp
   738 next
   739   case (Suc n)
   740   from sf[unfolded subseq_Suc_iff, rule_format, of n] Suc.hyps
   741   have "n < f (Suc n)" by arith 
   742   thus ?case by arith
   743 qed
   744 
   745 lemma LIMSEQ_subseq_LIMSEQ:
   746   "\<lbrakk> X ----> L; subseq f \<rbrakk> \<Longrightarrow> (X o f) ----> L"
   747 apply (rule topological_tendstoI)
   748 apply (drule (2) topological_tendstoD)
   749 apply (simp only: eventually_sequentially)
   750 apply (clarify, rule_tac x=N in exI, clarsimp)
   751 apply (blast intro: seq_suble le_trans dest!: spec) 
   752 done
   753 
   754 subsection {* Bounded Monotonic Sequences *}
   755 
   756 
   757 text{*Bounded Sequence*}
   758 
   759 lemma BseqD: "Bseq X ==> \<exists>K. 0 < K & (\<forall>n. norm (X n) \<le> K)"
   760 by (simp add: Bseq_def)
   761 
   762 lemma BseqI: "[| 0 < K; \<forall>n. norm (X n) \<le> K |] ==> Bseq X"
   763 by (auto simp add: Bseq_def)
   764 
   765 lemma lemma_NBseq_def:
   766      "(\<exists>K > 0. \<forall>n. norm (X n) \<le> K) =
   767       (\<exists>N. \<forall>n. norm (X n) \<le> real(Suc N))"
   768 proof auto
   769   fix K :: real
   770   from reals_Archimedean2 obtain n :: nat where "K < real n" ..
   771   then have "K \<le> real (Suc n)" by auto
   772   assume "\<forall>m. norm (X m) \<le> K"
   773   have "\<forall>m. norm (X m) \<le> real (Suc n)"
   774   proof
   775     fix m :: 'a
   776     from `\<forall>m. norm (X m) \<le> K` have "norm (X m) \<le> K" ..
   777     with `K \<le> real (Suc n)` show "norm (X m) \<le> real (Suc n)" by auto
   778   qed
   779   then show "\<exists>N. \<forall>n. norm (X n) \<le> real (Suc N)" ..
   780 next
   781   fix N :: nat
   782   have "real (Suc N) > 0" by (simp add: real_of_nat_Suc)
   783   moreover assume "\<forall>n. norm (X n) \<le> real (Suc N)"
   784   ultimately show "\<exists>K>0. \<forall>n. norm (X n) \<le> K" by blast
   785 qed
   786 
   787 
   788 text{* alternative definition for Bseq *}
   789 lemma Bseq_iff: "Bseq X = (\<exists>N. \<forall>n. norm (X n) \<le> real(Suc N))"
   790 apply (simp add: Bseq_def)
   791 apply (simp (no_asm) add: lemma_NBseq_def)
   792 done
   793 
   794 lemma lemma_NBseq_def2:
   795      "(\<exists>K > 0. \<forall>n. norm (X n) \<le> K) = (\<exists>N. \<forall>n. norm (X n) < real(Suc N))"
   796 apply (subst lemma_NBseq_def, auto)
   797 apply (rule_tac x = "Suc N" in exI)
   798 apply (rule_tac [2] x = N in exI)
   799 apply (auto simp add: real_of_nat_Suc)
   800  prefer 2 apply (blast intro: order_less_imp_le)
   801 apply (drule_tac x = n in spec, simp)
   802 done
   803 
   804 (* yet another definition for Bseq *)
   805 lemma Bseq_iff1a: "Bseq X = (\<exists>N. \<forall>n. norm (X n) < real(Suc N))"
   806 by (simp add: Bseq_def lemma_NBseq_def2)
   807 
   808 subsubsection{*Upper Bounds and Lubs of Bounded Sequences*}
   809 
   810 lemma Bseq_isUb:
   811   "!!(X::nat=>real). Bseq X ==> \<exists>U. isUb (UNIV::real set) {x. \<exists>n. X n = x} U"
   812 by (auto intro: isUbI setleI simp add: Bseq_def abs_le_iff)
   813 
   814 
   815 text{* Use completeness of reals (supremum property)
   816    to show that any bounded sequence has a least upper bound*}
   817 
   818 lemma Bseq_isLub:
   819   "!!(X::nat=>real). Bseq X ==>
   820    \<exists>U. isLub (UNIV::real set) {x. \<exists>n. X n = x} U"
   821 by (blast intro: reals_complete Bseq_isUb)
   822 
   823 subsubsection{*A Bounded and Monotonic Sequence Converges*}
   824 
   825 lemma lemma_converg1:
   826      "!!(X::nat=>real). [| \<forall>m. \<forall> n \<ge> m. X m \<le> X n;
   827                   isLub (UNIV::real set) {x. \<exists>n. X n = x} (X ma)
   828                |] ==> \<forall>n \<ge> ma. X n = X ma"
   829 apply safe
   830 apply (drule_tac y = "X n" in isLubD2)
   831 apply (blast dest: order_antisym)+
   832 done
   833 
   834 lemma Bmonoseq_LIMSEQ: "\<forall>n. m \<le> n --> X n = X m ==> \<exists>L. (X ----> L)"
   835 unfolding tendsto_def eventually_sequentially
   836 apply (rule_tac x = "X m" in exI, safe)
   837 apply (rule_tac x = m in exI, safe)
   838 apply (drule spec, erule impE, auto)
   839 done
   840 
   841 lemma lemma_converg2:
   842    "!!(X::nat=>real).
   843     [| \<forall>m. X m ~= U;  isLub UNIV {x. \<exists>n. X n = x} U |] ==> \<forall>m. X m < U"
   844 apply safe
   845 apply (drule_tac y = "X m" in isLubD2)
   846 apply (auto dest!: order_le_imp_less_or_eq)
   847 done
   848 
   849 lemma lemma_converg3: "!!(X ::nat=>real). \<forall>m. X m \<le> U ==> isUb UNIV {x. \<exists>n. X n = x} U"
   850 by (rule setleI [THEN isUbI], auto)
   851 
   852 text{* FIXME: @{term "U - T < U"} is redundant *}
   853 lemma lemma_converg4: "!!(X::nat=> real).
   854                [| \<forall>m. X m ~= U;
   855                   isLub UNIV {x. \<exists>n. X n = x} U;
   856                   0 < T;
   857                   U + - T < U
   858                |] ==> \<exists>m. U + -T < X m & X m < U"
   859 apply (drule lemma_converg2, assumption)
   860 apply (rule ccontr, simp)
   861 apply (simp add: linorder_not_less)
   862 apply (drule lemma_converg3)
   863 apply (drule isLub_le_isUb, assumption)
   864 apply (auto dest: order_less_le_trans)
   865 done
   866 
   867 text{*A standard proof of the theorem for monotone increasing sequence*}
   868 
   869 lemma Bseq_mono_convergent:
   870      "[| Bseq X; \<forall>m. \<forall>n \<ge> m. X m \<le> X n |] ==> convergent (X::nat=>real)"
   871 apply (simp add: convergent_def)
   872 apply (frule Bseq_isLub, safe)
   873 apply (case_tac "\<exists>m. X m = U", auto)
   874 apply (blast dest: lemma_converg1 Bmonoseq_LIMSEQ)
   875 (* second case *)
   876 apply (rule_tac x = U in exI)
   877 apply (subst LIMSEQ_iff, safe)
   878 apply (frule lemma_converg2, assumption)
   879 apply (drule lemma_converg4, auto)
   880 apply (rule_tac x = m in exI, safe)
   881 apply (subgoal_tac "X m \<le> X n")
   882  prefer 2 apply blast
   883 apply (drule_tac x=n and P="%m. X m < U" in spec, arith)
   884 done
   885 
   886 lemma Bseq_minus_iff: "Bseq (%n. -(X n)) = Bseq X"
   887 by (simp add: Bseq_def)
   888 
   889 text{*Main monotonicity theorem*}
   890 lemma Bseq_monoseq_convergent: "[| Bseq X; monoseq X |] ==> convergent X"
   891 apply (simp add: monoseq_def, safe)
   892 apply (rule_tac [2] convergent_minus_iff [THEN ssubst])
   893 apply (drule_tac [2] Bseq_minus_iff [THEN ssubst])
   894 apply (auto intro!: Bseq_mono_convergent)
   895 done
   896 
   897 subsubsection{*Increasing and Decreasing Series*}
   898 
   899 lemma incseq_imp_monoseq:  "incseq X \<Longrightarrow> monoseq X"
   900   by (simp add: incseq_def monoseq_def) 
   901 
   902 lemma incseq_le: assumes inc: "incseq X" and lim: "X ----> L" shows "X n \<le> L"
   903   using monoseq_le [OF incseq_imp_monoseq [OF inc] lim]
   904 proof
   905   assume "(\<forall>n. X n \<le> L) \<and> (\<forall>m n. m \<le> n \<longrightarrow> X m \<le> X n)"
   906   thus ?thesis by simp
   907 next
   908   assume "(\<forall>n. L \<le> X n) \<and> (\<forall>m n. m \<le> n \<longrightarrow> X n \<le> X m)"
   909   hence const: "(!!m n. m \<le> n \<Longrightarrow> X n = X m)" using inc
   910     by (auto simp add: incseq_def intro: order_antisym)
   911   have X: "!!n. X n = X 0"
   912     by (blast intro: const [of 0]) 
   913   have "X = (\<lambda>n. X 0)"
   914     by (blast intro: ext X)
   915   hence "L = X 0" using LIMSEQ_const [of "X 0"]
   916     by (auto intro: LIMSEQ_unique lim) 
   917   thus ?thesis
   918     by (blast intro: eq_refl X)
   919 qed
   920 
   921 lemma incseq_SucI:
   922   assumes "\<And>n. X n \<le> X (Suc n)"
   923   shows "incseq X" unfolding incseq_def
   924 proof safe
   925   fix m n :: nat
   926   { fix d m :: nat
   927     have "X m \<le> X (m + d)"
   928     proof (induct d)
   929       case (Suc d)
   930       also have "X (m + d) \<le> X (m + Suc d)"
   931         using assms by simp
   932       finally show ?case .
   933     qed simp }
   934   note this[of m "n - m"]
   935   moreover assume "m \<le> n"
   936   ultimately show "X m \<le> X n" by simp
   937 qed
   938 
   939 lemma decseq_imp_monoseq:  "decseq X \<Longrightarrow> monoseq X"
   940   by (simp add: decseq_def monoseq_def)
   941 
   942 lemma decseq_eq_incseq: "decseq X = incseq (\<lambda>n. - X n)" 
   943   by (simp add: decseq_def incseq_def)
   944 
   945 
   946 lemma decseq_le: assumes dec: "decseq X" and lim: "X ----> L" shows "L \<le> X n"
   947 proof -
   948   have inc: "incseq (\<lambda>n. - X n)" using dec
   949     by (simp add: decseq_eq_incseq)
   950   have "- X n \<le> - L" 
   951     by (blast intro: incseq_le [OF inc] LIMSEQ_minus lim) 
   952   thus ?thesis
   953     by simp
   954 qed
   955 
   956 subsubsection{*A Few More Equivalence Theorems for Boundedness*}
   957 
   958 text{*alternative formulation for boundedness*}
   959 lemma Bseq_iff2: "Bseq X = (\<exists>k > 0. \<exists>x. \<forall>n. norm (X(n) + -x) \<le> k)"
   960 apply (unfold Bseq_def, safe)
   961 apply (rule_tac [2] x = "k + norm x" in exI)
   962 apply (rule_tac x = K in exI, simp)
   963 apply (rule exI [where x = 0], auto)
   964 apply (erule order_less_le_trans, simp)
   965 apply (drule_tac x=n in spec, fold diff_minus)
   966 apply (drule order_trans [OF norm_triangle_ineq2])
   967 apply simp
   968 done
   969 
   970 text{*alternative formulation for boundedness*}
   971 lemma Bseq_iff3: "Bseq X = (\<exists>k > 0. \<exists>N. \<forall>n. norm(X(n) + -X(N)) \<le> k)"
   972 apply safe
   973 apply (simp add: Bseq_def, safe)
   974 apply (rule_tac x = "K + norm (X N)" in exI)
   975 apply auto
   976 apply (erule order_less_le_trans, simp)
   977 apply (rule_tac x = N in exI, safe)
   978 apply (drule_tac x = n in spec)
   979 apply (rule order_trans [OF norm_triangle_ineq], simp)
   980 apply (auto simp add: Bseq_iff2)
   981 done
   982 
   983 lemma BseqI2: "(\<forall>n. k \<le> f n & f n \<le> (K::real)) ==> Bseq f"
   984 apply (simp add: Bseq_def)
   985 apply (rule_tac x = " (\<bar>k\<bar> + \<bar>K\<bar>) + 1" in exI, auto)
   986 apply (drule_tac x = n in spec, arith)
   987 done
   988 
   989 
   990 subsection {* Cauchy Sequences *}
   991 
   992 lemma metric_CauchyI:
   993   "(\<And>e. 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e) \<Longrightarrow> Cauchy X"
   994 by (simp add: Cauchy_def)
   995 
   996 lemma metric_CauchyD:
   997   "\<lbrakk>Cauchy X; 0 < e\<rbrakk> \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e"
   998 by (simp add: Cauchy_def)
   999 
  1000 lemma Cauchy_iff:
  1001   fixes X :: "nat \<Rightarrow> 'a::real_normed_vector"
  1002   shows "Cauchy X \<longleftrightarrow> (\<forall>e>0. \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. norm (X m - X n) < e)"
  1003 unfolding Cauchy_def dist_norm ..
  1004 
  1005 lemma Cauchy_iff2:
  1006      "Cauchy X =
  1007       (\<forall>j. (\<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. \<bar>X m - X n\<bar> < inverse(real (Suc j))))"
  1008 apply (simp add: Cauchy_iff, auto)
  1009 apply (drule reals_Archimedean, safe)
  1010 apply (drule_tac x = n in spec, auto)
  1011 apply (rule_tac x = M in exI, auto)
  1012 apply (drule_tac x = m in spec, simp)
  1013 apply (drule_tac x = na in spec, auto)
  1014 done
  1015 
  1016 lemma CauchyI:
  1017   fixes X :: "nat \<Rightarrow> 'a::real_normed_vector"
  1018   shows "(\<And>e. 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. norm (X m - X n) < e) \<Longrightarrow> Cauchy X"
  1019 by (simp add: Cauchy_iff)
  1020 
  1021 lemma CauchyD:
  1022   fixes X :: "nat \<Rightarrow> 'a::real_normed_vector"
  1023   shows "\<lbrakk>Cauchy X; 0 < e\<rbrakk> \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. norm (X m - X n) < e"
  1024 by (simp add: Cauchy_iff)
  1025 
  1026 lemma Cauchy_subseq_Cauchy:
  1027   "\<lbrakk> Cauchy X; subseq f \<rbrakk> \<Longrightarrow> Cauchy (X o f)"
  1028 apply (auto simp add: Cauchy_def)
  1029 apply (drule_tac x=e in spec, clarify)
  1030 apply (rule_tac x=M in exI, clarify)
  1031 apply (blast intro: le_trans [OF _ seq_suble] dest!: spec)
  1032 done
  1033 
  1034 subsubsection {* Cauchy Sequences are Bounded *}
  1035 
  1036 text{*A Cauchy sequence is bounded -- this is the standard
  1037   proof mechanization rather than the nonstandard proof*}
  1038 
  1039 lemma lemmaCauchy: "\<forall>n \<ge> M. norm (X M - X n) < (1::real)
  1040           ==>  \<forall>n \<ge> M. norm (X n :: 'a::real_normed_vector) < 1 + norm (X M)"
  1041 apply (clarify, drule spec, drule (1) mp)
  1042 apply (simp only: norm_minus_commute)
  1043 apply (drule order_le_less_trans [OF norm_triangle_ineq2])
  1044 apply simp
  1045 done
  1046 
  1047 lemma Cauchy_Bseq: "Cauchy X ==> Bseq X"
  1048 apply (simp add: Cauchy_iff)
  1049 apply (drule spec, drule mp, rule zero_less_one, safe)
  1050 apply (drule_tac x="M" in spec, simp)
  1051 apply (drule lemmaCauchy)
  1052 apply (rule_tac k="M" in Bseq_offset)
  1053 apply (simp add: Bseq_def)
  1054 apply (rule_tac x="1 + norm (X M)" in exI)
  1055 apply (rule conjI, rule order_less_le_trans [OF zero_less_one], simp)
  1056 apply (simp add: order_less_imp_le)
  1057 done
  1058 
  1059 subsubsection {* Cauchy Sequences are Convergent *}
  1060 
  1061 class complete_space =
  1062   assumes Cauchy_convergent: "Cauchy X \<Longrightarrow> convergent X"
  1063 
  1064 class banach = real_normed_vector + complete_space
  1065 
  1066 theorem LIMSEQ_imp_Cauchy:
  1067   assumes X: "X ----> a" shows "Cauchy X"
  1068 proof (rule metric_CauchyI)
  1069   fix e::real assume "0 < e"
  1070   hence "0 < e/2" by simp
  1071   with X have "\<exists>N. \<forall>n\<ge>N. dist (X n) a < e/2" by (rule metric_LIMSEQ_D)
  1072   then obtain N where N: "\<forall>n\<ge>N. dist (X n) a < e/2" ..
  1073   show "\<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m) (X n) < e"
  1074   proof (intro exI allI impI)
  1075     fix m assume "N \<le> m"
  1076     hence m: "dist (X m) a < e/2" using N by fast
  1077     fix n assume "N \<le> n"
  1078     hence n: "dist (X n) a < e/2" using N by fast
  1079     have "dist (X m) (X n) \<le> dist (X m) a + dist (X n) a"
  1080       by (rule dist_triangle2)
  1081     also from m n have "\<dots> < e" by simp
  1082     finally show "dist (X m) (X n) < e" .
  1083   qed
  1084 qed
  1085 
  1086 lemma convergent_Cauchy: "convergent X \<Longrightarrow> Cauchy X"
  1087 unfolding convergent_def
  1088 by (erule exE, erule LIMSEQ_imp_Cauchy)
  1089 
  1090 lemma Cauchy_convergent_iff:
  1091   fixes X :: "nat \<Rightarrow> 'a::complete_space"
  1092   shows "Cauchy X = convergent X"
  1093 by (fast intro: Cauchy_convergent convergent_Cauchy)
  1094 
  1095 lemma convergent_subseq_convergent:
  1096   fixes X :: "nat \<Rightarrow> 'a::complete_space"
  1097   shows "\<lbrakk> convergent X; subseq f \<rbrakk> \<Longrightarrow> convergent (X o f)"
  1098   by (simp add: Cauchy_subseq_Cauchy Cauchy_convergent_iff [symmetric])
  1099 
  1100 text {*
  1101 Proof that Cauchy sequences converge based on the one from
  1102 http://pirate.shu.edu/~wachsmut/ira/numseq/proofs/cauconv.html
  1103 *}
  1104 
  1105 text {*
  1106   If sequence @{term "X"} is Cauchy, then its limit is the lub of
  1107   @{term "{r::real. \<exists>N. \<forall>n\<ge>N. r < X n}"}
  1108 *}
  1109 
  1110 lemma isUb_UNIV_I: "(\<And>y. y \<in> S \<Longrightarrow> y \<le> u) \<Longrightarrow> isUb UNIV S u"
  1111 by (simp add: isUbI setleI)
  1112 
  1113 locale real_Cauchy =
  1114   fixes X :: "nat \<Rightarrow> real"
  1115   assumes X: "Cauchy X"
  1116   fixes S :: "real set"
  1117   defines S_def: "S \<equiv> {x::real. \<exists>N. \<forall>n\<ge>N. x < X n}"
  1118 
  1119 lemma real_CauchyI:
  1120   assumes "Cauchy X"
  1121   shows "real_Cauchy X"
  1122   proof qed (fact assms)
  1123 
  1124 lemma (in real_Cauchy) mem_S: "\<forall>n\<ge>N. x < X n \<Longrightarrow> x \<in> S"
  1125 by (unfold S_def, auto)
  1126 
  1127 lemma (in real_Cauchy) bound_isUb:
  1128   assumes N: "\<forall>n\<ge>N. X n < x"
  1129   shows "isUb UNIV S x"
  1130 proof (rule isUb_UNIV_I)
  1131   fix y::real assume "y \<in> S"
  1132   hence "\<exists>M. \<forall>n\<ge>M. y < X n"
  1133     by (simp add: S_def)
  1134   then obtain M where "\<forall>n\<ge>M. y < X n" ..
  1135   hence "y < X (max M N)" by simp
  1136   also have "\<dots> < x" using N by simp
  1137   finally show "y \<le> x"
  1138     by (rule order_less_imp_le)
  1139 qed
  1140 
  1141 lemma (in real_Cauchy) isLub_ex: "\<exists>u. isLub UNIV S u"
  1142 proof (rule reals_complete)
  1143   obtain N where "\<forall>m\<ge>N. \<forall>n\<ge>N. norm (X m - X n) < 1"
  1144     using CauchyD [OF X zero_less_one] by auto
  1145   hence N: "\<forall>n\<ge>N. norm (X n - X N) < 1" by simp
  1146   show "\<exists>x. x \<in> S"
  1147   proof
  1148     from N have "\<forall>n\<ge>N. X N - 1 < X n"
  1149       by (simp add: abs_diff_less_iff)
  1150     thus "X N - 1 \<in> S" by (rule mem_S)
  1151   qed
  1152   show "\<exists>u. isUb UNIV S u"
  1153   proof
  1154     from N have "\<forall>n\<ge>N. X n < X N + 1"
  1155       by (simp add: abs_diff_less_iff)
  1156     thus "isUb UNIV S (X N + 1)"
  1157       by (rule bound_isUb)
  1158   qed
  1159 qed
  1160 
  1161 lemma (in real_Cauchy) isLub_imp_LIMSEQ:
  1162   assumes x: "isLub UNIV S x"
  1163   shows "X ----> x"
  1164 proof (rule LIMSEQ_I)
  1165   fix r::real assume "0 < r"
  1166   hence r: "0 < r/2" by simp
  1167   obtain N where "\<forall>n\<ge>N. \<forall>m\<ge>N. norm (X n - X m) < r/2"
  1168     using CauchyD [OF X r] by auto
  1169   hence "\<forall>n\<ge>N. norm (X n - X N) < r/2" by simp
  1170   hence N: "\<forall>n\<ge>N. X N - r/2 < X n \<and> X n < X N + r/2"
  1171     by (simp only: real_norm_def abs_diff_less_iff)
  1172 
  1173   from N have "\<forall>n\<ge>N. X N - r/2 < X n" by fast
  1174   hence "X N - r/2 \<in> S" by (rule mem_S)
  1175   hence 1: "X N - r/2 \<le> x" using x isLub_isUb isUbD by fast
  1176 
  1177   from N have "\<forall>n\<ge>N. X n < X N + r/2" by fast
  1178   hence "isUb UNIV S (X N + r/2)" by (rule bound_isUb)
  1179   hence 2: "x \<le> X N + r/2" using x isLub_le_isUb by fast
  1180 
  1181   show "\<exists>N. \<forall>n\<ge>N. norm (X n - x) < r"
  1182   proof (intro exI allI impI)
  1183     fix n assume n: "N \<le> n"
  1184     from N n have "X n < X N + r/2" and "X N - r/2 < X n" by simp+
  1185     thus "norm (X n - x) < r" using 1 2
  1186       by (simp add: abs_diff_less_iff)
  1187   qed
  1188 qed
  1189 
  1190 lemma (in real_Cauchy) LIMSEQ_ex: "\<exists>x. X ----> x"
  1191 proof -
  1192   obtain x where "isLub UNIV S x"
  1193     using isLub_ex by fast
  1194   hence "X ----> x"
  1195     by (rule isLub_imp_LIMSEQ)
  1196   thus ?thesis ..
  1197 qed
  1198 
  1199 lemma real_Cauchy_convergent:
  1200   fixes X :: "nat \<Rightarrow> real"
  1201   shows "Cauchy X \<Longrightarrow> convergent X"
  1202 unfolding convergent_def
  1203 by (rule real_Cauchy.LIMSEQ_ex)
  1204  (rule real_CauchyI)
  1205 
  1206 instance real :: banach
  1207 by intro_classes (rule real_Cauchy_convergent)
  1208 
  1209 
  1210 subsection {* Power Sequences *}
  1211 
  1212 text{*The sequence @{term "x^n"} tends to 0 if @{term "0\<le>x"} and @{term
  1213 "x<1"}.  Proof will use (NS) Cauchy equivalence for convergence and
  1214   also fact that bounded and monotonic sequence converges.*}
  1215 
  1216 lemma Bseq_realpow: "[| 0 \<le> (x::real); x \<le> 1 |] ==> Bseq (%n. x ^ n)"
  1217 apply (simp add: Bseq_def)
  1218 apply (rule_tac x = 1 in exI)
  1219 apply (simp add: power_abs)
  1220 apply (auto dest: power_mono)
  1221 done
  1222 
  1223 lemma monoseq_realpow: "[| 0 \<le> x; x \<le> 1 |] ==> monoseq (%n. x ^ n)"
  1224 apply (clarify intro!: mono_SucI2)
  1225 apply (cut_tac n = n and N = "Suc n" and a = x in power_decreasing, auto)
  1226 done
  1227 
  1228 lemma convergent_realpow:
  1229   "[| 0 \<le> (x::real); x \<le> 1 |] ==> convergent (%n. x ^ n)"
  1230 by (blast intro!: Bseq_monoseq_convergent Bseq_realpow monoseq_realpow)
  1231 
  1232 lemma LIMSEQ_inverse_realpow_zero_lemma:
  1233   fixes x :: real
  1234   assumes x: "0 \<le> x"
  1235   shows "real n * x + 1 \<le> (x + 1) ^ n"
  1236 apply (induct n)
  1237 apply simp
  1238 apply simp
  1239 apply (rule order_trans)
  1240 prefer 2
  1241 apply (erule mult_left_mono)
  1242 apply (rule add_increasing [OF x], simp)
  1243 apply (simp add: real_of_nat_Suc)
  1244 apply (simp add: ring_distribs)
  1245 apply (simp add: mult_nonneg_nonneg x)
  1246 done
  1247 
  1248 lemma LIMSEQ_inverse_realpow_zero:
  1249   "1 < (x::real) \<Longrightarrow> (\<lambda>n. inverse (x ^ n)) ----> 0"
  1250 proof (rule LIMSEQ_inverse_zero [rule_format])
  1251   fix y :: real
  1252   assume x: "1 < x"
  1253   hence "0 < x - 1" by simp
  1254   hence "\<forall>y. \<exists>N::nat. y < real N * (x - 1)"
  1255     by (rule reals_Archimedean3)
  1256   hence "\<exists>N::nat. y < real N * (x - 1)" ..
  1257   then obtain N::nat where "y < real N * (x - 1)" ..
  1258   also have "\<dots> \<le> real N * (x - 1) + 1" by simp
  1259   also have "\<dots> \<le> (x - 1 + 1) ^ N"
  1260     by (rule LIMSEQ_inverse_realpow_zero_lemma, cut_tac x, simp)
  1261   also have "\<dots> = x ^ N" by simp
  1262   finally have "y < x ^ N" .
  1263   hence "\<forall>n\<ge>N. y < x ^ n"
  1264     apply clarify
  1265     apply (erule order_less_le_trans)
  1266     apply (erule power_increasing)
  1267     apply (rule order_less_imp_le [OF x])
  1268     done
  1269   thus "\<exists>N. \<forall>n\<ge>N. y < x ^ n" ..
  1270 qed
  1271 
  1272 lemma LIMSEQ_realpow_zero:
  1273   "\<lbrakk>0 \<le> (x::real); x < 1\<rbrakk> \<Longrightarrow> (\<lambda>n. x ^ n) ----> 0"
  1274 proof (cases)
  1275   assume "x = 0"
  1276   hence "(\<lambda>n. x ^ Suc n) ----> 0" by (simp add: LIMSEQ_const)
  1277   thus ?thesis by (rule LIMSEQ_imp_Suc)
  1278 next
  1279   assume "0 \<le> x" and "x \<noteq> 0"
  1280   hence x0: "0 < x" by simp
  1281   assume x1: "x < 1"
  1282   from x0 x1 have "1 < inverse x"
  1283     by (rule one_less_inverse)
  1284   hence "(\<lambda>n. inverse (inverse x ^ n)) ----> 0"
  1285     by (rule LIMSEQ_inverse_realpow_zero)
  1286   thus ?thesis by (simp add: power_inverse)
  1287 qed
  1288 
  1289 lemma LIMSEQ_power_zero:
  1290   fixes x :: "'a::{real_normed_algebra_1}"
  1291   shows "norm x < 1 \<Longrightarrow> (\<lambda>n. x ^ n) ----> 0"
  1292 apply (drule LIMSEQ_realpow_zero [OF norm_ge_zero])
  1293 apply (simp only: LIMSEQ_Zfun_iff, erule Zfun_le)
  1294 apply (simp add: power_abs norm_power_ineq)
  1295 done
  1296 
  1297 lemma LIMSEQ_divide_realpow_zero:
  1298   "1 < (x::real) ==> (%n. a / (x ^ n)) ----> 0"
  1299 apply (cut_tac a = a and x1 = "inverse x" in
  1300         LIMSEQ_mult [OF LIMSEQ_const LIMSEQ_realpow_zero])
  1301 apply (auto simp add: divide_inverse power_inverse)
  1302 apply (simp add: inverse_eq_divide pos_divide_less_eq)
  1303 done
  1304 
  1305 text{*Limit of @{term "c^n"} for @{term"\<bar>c\<bar> < 1"}*}
  1306 
  1307 lemma LIMSEQ_rabs_realpow_zero: "\<bar>c\<bar> < (1::real) ==> (%n. \<bar>c\<bar> ^ n) ----> 0"
  1308 by (rule LIMSEQ_realpow_zero [OF abs_ge_zero])
  1309 
  1310 lemma LIMSEQ_rabs_realpow_zero2: "\<bar>c\<bar> < (1::real) ==> (%n. c ^ n) ----> 0"
  1311 apply (rule LIMSEQ_rabs_zero [THEN iffD1])
  1312 apply (auto intro: LIMSEQ_rabs_realpow_zero simp add: power_abs)
  1313 done
  1314 
  1315 end