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