src/HOL/Hyperreal/Lim.thy
author huffman
Thu Sep 28 06:21:06 2006 +0200 (2006-09-28)
changeset 20756 fec7f5834ffe
parent 20755 956a0377a408
child 20793 3b0489715b0e
permissions -rw-r--r--
more reorganizing sections
paulson@10751
     1
(*  Title       : Lim.thy
paulson@14477
     2
    ID          : $Id$
paulson@10751
     3
    Author      : Jacques D. Fleuriot
paulson@10751
     4
    Copyright   : 1998  University of Cambridge
paulson@14477
     5
    Conversion to Isar and new proofs by Lawrence C Paulson, 2004
kleing@19023
     6
    GMVT by Benjamin Porter, 2005
paulson@10751
     7
*)
paulson@10751
     8
paulson@14477
     9
header{*Limits, Continuity and Differentiation*}
paulson@10751
    10
nipkow@15131
    11
theory Lim
nipkow@15360
    12
imports SEQ
nipkow@15131
    13
begin
paulson@14477
    14
paulson@14477
    15
text{*Standard and Nonstandard Definitions*}
paulson@10751
    16
wenzelm@19765
    17
definition
huffman@20561
    18
  LIM :: "['a::real_normed_vector => 'b::real_normed_vector, 'a, 'b] => bool"
kleing@19023
    19
        ("((_)/ -- (_)/ --> (_))" [60, 0, 60] 60)
wenzelm@19765
    20
  "f -- a --> L =
huffman@20563
    21
     (\<forall>r > 0. \<exists>s > 0. \<forall>x. x \<noteq> a & norm (x - a) < s
huffman@20563
    22
        --> norm (f x - L) < r)"
paulson@10751
    23
huffman@20561
    24
  NSLIM :: "['a::real_normed_vector => 'b::real_normed_vector, 'a, 'b] => bool"
kleing@19023
    25
            ("((_)/ -- (_)/ --NS> (_))" [60, 0, 60] 60)
huffman@20552
    26
  "f -- a --NS> L =
huffman@20552
    27
    (\<forall>x. (x \<noteq> star_of a & x @= star_of a --> ( *f* f) x @= star_of L))"
paulson@10751
    28
huffman@20561
    29
  isCont :: "['a::real_normed_vector => 'b::real_normed_vector, 'a] => bool"
wenzelm@19765
    30
  "isCont f a = (f -- a --> (f a))"
paulson@10751
    31
huffman@20561
    32
  isNSCont :: "['a::real_normed_vector => 'b::real_normed_vector, 'a] => bool"
paulson@15228
    33
    --{*NS definition dispenses with limit notions*}
huffman@20552
    34
  "isNSCont f a = (\<forall>y. y @= star_of a -->
huffman@20552
    35
         ( *f* f) y @= star_of (f a))"
paulson@10751
    36
paulson@14477
    37
  deriv:: "[real=>real,real,real] => bool"
paulson@15228
    38
    --{*Differentiation: D is derivative of function f at x*}
kleing@19023
    39
          ("(DERIV (_)/ (_)/ :> (_))" [1000, 1000, 60] 60)
huffman@20563
    40
  "DERIV f x :> D = ((%h. (f(x + h) - f x)/h) -- 0 --> D)"
paulson@10751
    41
paulson@14477
    42
  nsderiv :: "[real=>real,real,real] => bool"
kleing@19023
    43
          ("(NSDERIV (_)/ (_)/ :> (_))" [1000, 1000, 60] 60)
wenzelm@19765
    44
  "NSDERIV f x :> D = (\<forall>h \<in> Infinitesimal - {0}.
huffman@20563
    45
      (( *f* f)(hypreal_of_real x + h)
kleing@19023
    46
       - hypreal_of_real (f x))/h @= hypreal_of_real D)"
paulson@10751
    47
paulson@14477
    48
  differentiable :: "[real=>real,real] => bool"   (infixl "differentiable" 60)
wenzelm@19765
    49
  "f differentiable x = (\<exists>D. DERIV f x :> D)"
paulson@10751
    50
kleing@19023
    51
  NSdifferentiable :: "[real=>real,real] => bool"
paulson@14477
    52
                       (infixl "NSdifferentiable" 60)
wenzelm@19765
    53
  "f NSdifferentiable x = (\<exists>D. NSDERIV f x :> D)"
paulson@10751
    54
paulson@14477
    55
  increment :: "[real=>real,real,hypreal] => hypreal"
wenzelm@19765
    56
  "increment f x h = (@inc. f NSdifferentiable x &
huffman@20563
    57
           inc = ( *f* f)(hypreal_of_real x + h) - hypreal_of_real (f x))"
paulson@10751
    58
huffman@20752
    59
  isUCont :: "['a::real_normed_vector => 'b::real_normed_vector] => bool"
huffman@20752
    60
  "isUCont f = (\<forall>r>0. \<exists>s>0. \<forall>x y. norm (x - y) < s \<longrightarrow> norm (f x - f y) < r)"
paulson@10751
    61
huffman@20752
    62
  isNSUCont :: "['a::real_normed_vector => 'b::real_normed_vector] => bool"
wenzelm@19765
    63
  "isNSUCont f = (\<forall>x y. x @= y --> ( *f* f) x @= ( *f* f) y)"
paulson@10751
    64
paulson@10751
    65
paulson@10751
    66
consts
paulson@10751
    67
  Bolzano_bisect :: "[real*real=>bool, real, real, nat] => (real*real)"
paulson@10751
    68
primrec
paulson@10751
    69
  "Bolzano_bisect P a b 0 = (a,b)"
paulson@10751
    70
  "Bolzano_bisect P a b (Suc n) =
paulson@10751
    71
      (let (x,y) = Bolzano_bisect P a b n
wenzelm@11704
    72
       in if P(x, (x+y)/2) then ((x+y)/2, y)
paulson@14477
    73
                            else (x, (x+y)/2))"
paulson@14477
    74
paulson@14477
    75
huffman@20755
    76
subsection {* Limits of Functions *}
paulson@14477
    77
huffman@20755
    78
subsubsection {* Purely standard proofs *}
paulson@14477
    79
paulson@14477
    80
lemma LIM_eq:
paulson@14477
    81
     "f -- a --> L =
huffman@20561
    82
     (\<forall>r>0.\<exists>s>0.\<forall>x. x \<noteq> a & norm (x-a) < s --> norm (f x - L) < r)"
paulson@14477
    83
by (simp add: LIM_def diff_minus)
paulson@14477
    84
huffman@20552
    85
lemma LIM_I:
huffman@20561
    86
     "(!!r. 0<r ==> \<exists>s>0.\<forall>x. x \<noteq> a & norm (x-a) < s --> norm (f x - L) < r)
huffman@20552
    87
      ==> f -- a --> L"
huffman@20552
    88
by (simp add: LIM_eq)
huffman@20552
    89
paulson@14477
    90
lemma LIM_D:
paulson@14477
    91
     "[| f -- a --> L; 0<r |]
huffman@20561
    92
      ==> \<exists>s>0.\<forall>x. x \<noteq> a & norm (x-a) < s --> norm (f x - L) < r"
paulson@14477
    93
by (simp add: LIM_eq)
paulson@14477
    94
huffman@20756
    95
lemma LIM_shift: "f -- a --> L \<Longrightarrow> (\<lambda>x. f (x + k)) -- a - k --> L"
huffman@20756
    96
apply (rule LIM_I)
huffman@20756
    97
apply (drule_tac r="r" in LIM_D, safe)
huffman@20756
    98
apply (rule_tac x="s" in exI, safe)
huffman@20756
    99
apply (drule_tac x="x + k" in spec)
huffman@20756
   100
apply (simp add: compare_rls)
huffman@20756
   101
done
huffman@20756
   102
paulson@15228
   103
lemma LIM_const [simp]: "(%x. k) -- x --> k"
paulson@14477
   104
by (simp add: LIM_def)
paulson@14477
   105
paulson@14477
   106
lemma LIM_add:
huffman@20561
   107
  fixes f g :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
paulson@14477
   108
  assumes f: "f -- a --> L" and g: "g -- a --> M"
paulson@14477
   109
  shows "(%x. f x + g(x)) -- a --> (L + M)"
huffman@20552
   110
proof (rule LIM_I)
paulson@14477
   111
  fix r :: real
huffman@20409
   112
  assume r: "0 < r"
paulson@14477
   113
  from LIM_D [OF f half_gt_zero [OF r]]
paulson@14477
   114
  obtain fs
paulson@14477
   115
    where fs:    "0 < fs"
huffman@20561
   116
      and fs_lt: "\<forall>x. x \<noteq> a & norm (x-a) < fs --> norm (f x - L) < r/2"
paulson@14477
   117
  by blast
paulson@14477
   118
  from LIM_D [OF g half_gt_zero [OF r]]
paulson@14477
   119
  obtain gs
paulson@14477
   120
    where gs:    "0 < gs"
huffman@20561
   121
      and gs_lt: "\<forall>x. x \<noteq> a & norm (x-a) < gs --> norm (g x - M) < r/2"
paulson@14477
   122
  by blast
huffman@20561
   123
  show "\<exists>s>0.\<forall>x. x \<noteq> a \<and> norm (x-a) < s \<longrightarrow> norm (f x + g x - (L + M)) < r"
paulson@14477
   124
  proof (intro exI conjI strip)
paulson@14477
   125
    show "0 < min fs gs"  by (simp add: fs gs)
huffman@20561
   126
    fix x :: 'a
huffman@20561
   127
    assume "x \<noteq> a \<and> norm (x-a) < min fs gs"
huffman@20561
   128
    hence "x \<noteq> a \<and> norm (x-a) < fs \<and> norm (x-a) < gs" by simp
paulson@14477
   129
    with fs_lt gs_lt
huffman@20552
   130
    have "norm (f x - L) < r/2" and "norm (g x - M) < r/2" by blast+
huffman@20552
   131
    hence "norm (f x - L) + norm (g x - M) < r" by arith
huffman@20552
   132
    thus "norm (f x + g x - (L + M)) < r"
huffman@20552
   133
      by (blast intro: norm_diff_triangle_ineq order_le_less_trans)
paulson@14477
   134
  qed
paulson@14477
   135
qed
paulson@14477
   136
huffman@20552
   137
lemma minus_diff_minus:
huffman@20552
   138
  fixes a b :: "'a::ab_group_add"
huffman@20552
   139
  shows "(- a) - (- b) = - (a - b)"
huffman@20552
   140
by simp
huffman@20552
   141
paulson@14477
   142
lemma LIM_minus: "f -- a --> L ==> (%x. -f(x)) -- a --> -L"
huffman@20552
   143
by (simp only: LIM_eq minus_diff_minus norm_minus_cancel)
paulson@14477
   144
paulson@14477
   145
lemma LIM_add_minus:
paulson@14477
   146
    "[| f -- x --> l; g -- x --> m |] ==> (%x. f(x) + -g(x)) -- x --> (l + -m)"
huffman@20552
   147
by (intro LIM_add LIM_minus)
paulson@14477
   148
paulson@14477
   149
lemma LIM_diff:
paulson@14477
   150
    "[| f -- x --> l; g -- x --> m |] ==> (%x. f(x) - g(x)) -- x --> l-m"
huffman@20552
   151
by (simp only: diff_minus LIM_add LIM_minus)
paulson@14477
   152
huffman@20561
   153
lemma LIM_const_not_eq:
huffman@20561
   154
  fixes a :: "'a::real_normed_div_algebra"
huffman@20561
   155
  shows "k \<noteq> L ==> ~ ((%x. k) -- a --> L)"
huffman@20552
   156
apply (simp add: LIM_eq)
huffman@20552
   157
apply (rule_tac x="norm (k - L)" in exI, simp, safe)
huffman@20561
   158
apply (rule_tac x="a + of_real (s/2)" in exI, simp add: norm_of_real)
huffman@20552
   159
done
paulson@14477
   160
huffman@20561
   161
lemma LIM_const_eq:
huffman@20561
   162
  fixes a :: "'a::real_normed_div_algebra"
huffman@20561
   163
  shows "(%x. k) -- a --> L ==> k = L"
paulson@14477
   164
apply (rule ccontr)
kleing@19023
   165
apply (blast dest: LIM_const_not_eq)
paulson@14477
   166
done
paulson@14477
   167
huffman@20561
   168
lemma LIM_unique:
huffman@20561
   169
  fixes a :: "'a::real_normed_div_algebra"
huffman@20561
   170
  shows "[| f -- a --> L; f -- a --> M |] ==> L = M"
kleing@19023
   171
apply (drule LIM_diff, assumption)
paulson@14477
   172
apply (auto dest!: LIM_const_eq)
paulson@14477
   173
done
paulson@14477
   174
paulson@14477
   175
lemma LIM_mult_zero:
huffman@20561
   176
  fixes f g :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_algebra"
paulson@14477
   177
  assumes f: "f -- a --> 0" and g: "g -- a --> 0"
paulson@14477
   178
  shows "(%x. f(x) * g(x)) -- a --> 0"
huffman@20552
   179
proof (rule LIM_I, simp)
paulson@14477
   180
  fix r :: real
paulson@14477
   181
  assume r: "0<r"
paulson@14477
   182
  from LIM_D [OF f zero_less_one]
paulson@14477
   183
  obtain fs
paulson@14477
   184
    where fs:    "0 < fs"
huffman@20561
   185
      and fs_lt: "\<forall>x. x \<noteq> a & norm (x-a) < fs --> norm (f x) < 1"
paulson@14477
   186
  by auto
paulson@14477
   187
  from LIM_D [OF g r]
paulson@14477
   188
  obtain gs
paulson@14477
   189
    where gs:    "0 < gs"
huffman@20561
   190
      and gs_lt: "\<forall>x. x \<noteq> a & norm (x-a) < gs --> norm (g x) < r"
paulson@14477
   191
  by auto
huffman@20561
   192
  show "\<exists>s. 0 < s \<and> (\<forall>x. x \<noteq> a \<and> norm (x-a) < s \<longrightarrow> norm (f x * g x) < r)"
paulson@14477
   193
  proof (intro exI conjI strip)
paulson@14477
   194
    show "0 < min fs gs"  by (simp add: fs gs)
huffman@20561
   195
    fix x :: 'a
huffman@20561
   196
    assume "x \<noteq> a \<and> norm (x-a) < min fs gs"
huffman@20561
   197
    hence  "x \<noteq> a \<and> norm (x-a) < fs \<and> norm (x-a) < gs" by simp
paulson@14477
   198
    with fs_lt gs_lt
huffman@20552
   199
    have "norm (f x) < 1" and "norm (g x) < r" by blast+
huffman@20552
   200
    hence "norm (f x) * norm (g x) < 1*r"
huffman@20552
   201
      by (rule mult_strict_mono' [OF _ _ norm_ge_zero norm_ge_zero])
huffman@20552
   202
    thus "norm (f x * g x) < r"
huffman@20552
   203
      by (simp add: order_le_less_trans [OF norm_mult_ineq])
paulson@14477
   204
  qed
paulson@14477
   205
qed
paulson@14477
   206
paulson@14477
   207
lemma LIM_self: "(%x. x) -- a --> a"
paulson@14477
   208
by (auto simp add: LIM_def)
paulson@14477
   209
paulson@14477
   210
text{*Limits are equal for functions equal except at limit point*}
paulson@14477
   211
lemma LIM_equal:
paulson@14477
   212
     "[| \<forall>x. x \<noteq> a --> (f x = g x) |] ==> (f -- a --> l) = (g -- a --> l)"
paulson@14477
   213
by (simp add: LIM_def)
paulson@14477
   214
paulson@14477
   215
text{*Two uses in Hyperreal/Transcendental.ML*}
paulson@14477
   216
lemma LIM_trans:
paulson@14477
   217
     "[| (%x. f(x) + -g(x)) -- a --> 0;  g -- a --> l |] ==> f -- a --> l"
paulson@14477
   218
apply (drule LIM_add, assumption)
paulson@14477
   219
apply (auto simp add: add_assoc)
paulson@14477
   220
done
paulson@14477
   221
huffman@20755
   222
subsubsection {* Purely nonstandard proofs *}
paulson@14477
   223
huffman@20754
   224
lemma NSLIM_I:
huffman@20754
   225
  "(\<And>x. \<lbrakk>x \<noteq> star_of a; x \<approx> star_of a\<rbrakk> \<Longrightarrow> starfun f x \<approx> star_of L)
huffman@20754
   226
   \<Longrightarrow> f -- a --NS> L"
huffman@20754
   227
by (simp add: NSLIM_def)
paulson@14477
   228
huffman@20754
   229
lemma NSLIM_D:
huffman@20754
   230
  "\<lbrakk>f -- a --NS> L; x \<noteq> star_of a; x \<approx> star_of a\<rbrakk>
huffman@20754
   231
   \<Longrightarrow> starfun f x \<approx> star_of L"
huffman@20754
   232
by (simp add: NSLIM_def)
paulson@14477
   233
huffman@20755
   234
text{*Proving properties of limits using nonstandard definition.
huffman@20755
   235
      The properties hold for standard limits as well!*}
huffman@20755
   236
huffman@20755
   237
lemma NSLIM_mult:
huffman@20755
   238
  fixes l m :: "'a::real_normed_algebra"
huffman@20755
   239
  shows "[| f -- x --NS> l; g -- x --NS> m |]
huffman@20755
   240
      ==> (%x. f(x) * g(x)) -- x --NS> (l * m)"
huffman@20755
   241
by (auto simp add: NSLIM_def intro!: approx_mult_HFinite)
huffman@20755
   242
huffman@20755
   243
lemma NSLIM_add:
huffman@20755
   244
     "[| f -- x --NS> l; g -- x --NS> m |]
huffman@20755
   245
      ==> (%x. f(x) + g(x)) -- x --NS> (l + m)"
huffman@20755
   246
by (auto simp add: NSLIM_def intro!: approx_add)
huffman@20755
   247
huffman@20755
   248
lemma NSLIM_const [simp]: "(%x. k) -- x --NS> k"
huffman@20755
   249
by (simp add: NSLIM_def)
huffman@20755
   250
huffman@20755
   251
lemma NSLIM_minus: "f -- a --NS> L ==> (%x. -f(x)) -- a --NS> -L"
huffman@20755
   252
by (simp add: NSLIM_def)
huffman@20755
   253
huffman@20755
   254
lemma NSLIM_add_minus: "[| f -- x --NS> l; g -- x --NS> m |] ==> (%x. f(x) + -g(x)) -- x --NS> (l + -m)"
huffman@20755
   255
by (simp only: NSLIM_add NSLIM_minus)
huffman@20755
   256
huffman@20755
   257
lemma NSLIM_inverse:
huffman@20755
   258
  fixes L :: "'a::real_normed_div_algebra"
huffman@20755
   259
  shows "[| f -- a --NS> L;  L \<noteq> 0 |]
huffman@20755
   260
      ==> (%x. inverse(f(x))) -- a --NS> (inverse L)"
huffman@20755
   261
apply (simp add: NSLIM_def, clarify)
huffman@20755
   262
apply (drule spec)
huffman@20755
   263
apply (auto simp add: star_of_approx_inverse)
huffman@20755
   264
done
huffman@20755
   265
huffman@20755
   266
lemma NSLIM_zero:
huffman@20755
   267
  assumes f: "f -- a --NS> l" shows "(%x. f(x) + -l) -- a --NS> 0"
huffman@20755
   268
proof -
huffman@20755
   269
  have "(\<lambda>x. f x + - l) -- a --NS> l + -l"
huffman@20755
   270
    by (rule NSLIM_add_minus [OF f NSLIM_const])
huffman@20755
   271
  thus ?thesis by simp
huffman@20755
   272
qed
huffman@20755
   273
huffman@20755
   274
lemma NSLIM_zero_cancel: "(%x. f(x) - l) -- x --NS> 0 ==> f -- x --NS> l"
huffman@20755
   275
apply (drule_tac g = "%x. l" and m = l in NSLIM_add)
huffman@20755
   276
apply (auto simp add: diff_minus add_assoc)
huffman@20755
   277
done
huffman@20755
   278
huffman@20755
   279
lemma NSLIM_const_not_eq:
huffman@20755
   280
  fixes a :: real (* TODO: generalize to real_normed_div_algebra *)
huffman@20755
   281
  shows "k \<noteq> L ==> ~ ((%x. k) -- a --NS> L)"
huffman@20755
   282
apply (simp add: NSLIM_def)
huffman@20755
   283
apply (rule_tac x="star_of a + epsilon" in exI)
huffman@20755
   284
apply (auto intro: Infinitesimal_add_approx_self [THEN approx_sym]
huffman@20755
   285
            simp add: hypreal_epsilon_not_zero)
huffman@20755
   286
done
huffman@20755
   287
huffman@20755
   288
lemma NSLIM_not_zero:
huffman@20755
   289
  fixes a :: real
huffman@20755
   290
  shows "k \<noteq> 0 ==> ~ ((%x. k) -- a --NS> 0)"
huffman@20755
   291
by (rule NSLIM_const_not_eq)
huffman@20755
   292
huffman@20755
   293
lemma NSLIM_const_eq:
huffman@20755
   294
  fixes a :: real
huffman@20755
   295
  shows "(%x. k) -- a --NS> L ==> k = L"
huffman@20755
   296
apply (rule ccontr)
huffman@20755
   297
apply (blast dest: NSLIM_const_not_eq)
huffman@20755
   298
done
huffman@20755
   299
huffman@20755
   300
text{* can actually be proved more easily by unfolding the definition!*}
huffman@20755
   301
lemma NSLIM_unique:
huffman@20755
   302
  fixes a :: real
huffman@20755
   303
  shows "[| f -- a --NS> L; f -- a --NS> M |] ==> L = M"
huffman@20755
   304
apply (drule NSLIM_minus)
huffman@20755
   305
apply (drule NSLIM_add, assumption)
huffman@20755
   306
apply (auto dest!: NSLIM_const_eq [symmetric])
huffman@20755
   307
apply (simp add: diff_def [symmetric])
huffman@20755
   308
done
huffman@20755
   309
huffman@20755
   310
lemma NSLIM_mult_zero:
huffman@20755
   311
  fixes f g :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_algebra"
huffman@20755
   312
  shows "[| f -- x --NS> 0; g -- x --NS> 0 |] ==> (%x. f(x)*g(x)) -- x --NS> 0"
huffman@20755
   313
by (drule NSLIM_mult, auto)
huffman@20755
   314
huffman@20755
   315
lemma NSLIM_self: "(%x. x) -- a --NS> a"
huffman@20755
   316
by (simp add: NSLIM_def)
huffman@20755
   317
huffman@20755
   318
subsubsection {* Equivalence of @{term LIM} and @{term NSLIM} *}
huffman@20755
   319
huffman@20754
   320
lemma LIM_NSLIM:
huffman@20754
   321
  assumes f: "f -- a --> L" shows "f -- a --NS> L"
huffman@20754
   322
proof (rule NSLIM_I)
huffman@20754
   323
  fix x
huffman@20754
   324
  assume neq: "x \<noteq> star_of a"
huffman@20754
   325
  assume approx: "x \<approx> star_of a"
huffman@20754
   326
  have "starfun f x - star_of L \<in> Infinitesimal"
huffman@20754
   327
  proof (rule InfinitesimalI2)
huffman@20754
   328
    fix r::real assume r: "0 < r"
huffman@20754
   329
    from LIM_D [OF f r]
huffman@20754
   330
    obtain s where s: "0 < s" and
huffman@20754
   331
      less_r: "\<And>x. \<lbrakk>x \<noteq> a; norm (x - a) < s\<rbrakk> \<Longrightarrow> norm (f x - L) < r"
huffman@20754
   332
      by fast
huffman@20754
   333
    from less_r have less_r':
huffman@20754
   334
       "\<And>x. \<lbrakk>x \<noteq> star_of a; hnorm (x - star_of a) < star_of s\<rbrakk>
huffman@20754
   335
        \<Longrightarrow> hnorm (starfun f x - star_of L) < star_of r"
huffman@20754
   336
      by transfer
huffman@20754
   337
    from approx have "x - star_of a \<in> Infinitesimal"
huffman@20754
   338
      by (unfold approx_def)
huffman@20754
   339
    hence "hnorm (x - star_of a) < star_of s"
huffman@20754
   340
      using s by (rule InfinitesimalD2)
huffman@20754
   341
    with neq show "hnorm (starfun f x - star_of L) < star_of r"
huffman@20754
   342
      by (rule less_r')
huffman@20754
   343
  qed
huffman@20754
   344
  thus "starfun f x \<approx> star_of L"
huffman@20754
   345
    by (unfold approx_def)
huffman@20754
   346
qed
huffman@20552
   347
huffman@20754
   348
lemma NSLIM_LIM:
huffman@20754
   349
  assumes f: "f -- a --NS> L" shows "f -- a --> L"
huffman@20754
   350
proof (rule LIM_I)
huffman@20754
   351
  fix r::real assume r: "0 < r"
huffman@20754
   352
  have "\<exists>s>0. \<forall>x. x \<noteq> star_of a \<and> hnorm (x - star_of a) < s
huffman@20754
   353
        \<longrightarrow> hnorm (starfun f x - star_of L) < star_of r"
huffman@20754
   354
  proof (rule exI, safe)
huffman@20754
   355
    show "0 < epsilon" by (rule hypreal_epsilon_gt_zero)
huffman@20754
   356
  next
huffman@20754
   357
    fix x assume neq: "x \<noteq> star_of a"
huffman@20754
   358
    assume "hnorm (x - star_of a) < epsilon"
huffman@20754
   359
    with Infinitesimal_epsilon
huffman@20754
   360
    have "x - star_of a \<in> Infinitesimal"
huffman@20754
   361
      by (rule hnorm_less_Infinitesimal)
huffman@20754
   362
    hence "x \<approx> star_of a"
huffman@20754
   363
      by (unfold approx_def)
huffman@20754
   364
    with f neq have "starfun f x \<approx> star_of L"
huffman@20754
   365
      by (rule NSLIM_D)
huffman@20754
   366
    hence "starfun f x - star_of L \<in> Infinitesimal"
huffman@20754
   367
      by (unfold approx_def)
huffman@20754
   368
    thus "hnorm (starfun f x - star_of L) < star_of r"
huffman@20754
   369
      using r by (rule InfinitesimalD2)
huffman@20754
   370
  qed
huffman@20754
   371
  thus "\<exists>s>0. \<forall>x. x \<noteq> a \<and> norm (x - a) < s \<longrightarrow> norm (f x - L) < r"
huffman@20754
   372
    by transfer
huffman@20754
   373
qed
paulson@14477
   374
paulson@15228
   375
theorem LIM_NSLIM_iff: "(f -- x --> L) = (f -- x --NS> L)"
paulson@14477
   376
by (blast intro: LIM_NSLIM NSLIM_LIM)
paulson@14477
   377
huffman@20755
   378
subsubsection {* Derived theorems about @{term LIM} *}
paulson@14477
   379
paulson@15228
   380
lemma LIM_mult2:
huffman@20552
   381
  fixes l m :: "'a::real_normed_algebra"
huffman@20552
   382
  shows "[| f -- x --> l; g -- x --> m |]
huffman@20552
   383
      ==> (%x. f(x) * g(x)) -- x --> (l * m)"
paulson@14477
   384
by (simp add: LIM_NSLIM_iff NSLIM_mult)
paulson@14477
   385
paulson@15228
   386
lemma LIM_add2:
paulson@15228
   387
     "[| f -- x --> l; g -- x --> m |] ==> (%x. f(x) + g(x)) -- x --> (l + m)"
paulson@14477
   388
by (simp add: LIM_NSLIM_iff NSLIM_add)
paulson@14477
   389
paulson@14477
   390
lemma LIM_const2: "(%x. k) -- x --> k"
paulson@14477
   391
by (simp add: LIM_NSLIM_iff)
paulson@14477
   392
paulson@14477
   393
lemma LIM_minus2: "f -- a --> L ==> (%x. -f(x)) -- a --> -L"
paulson@14477
   394
by (simp add: LIM_NSLIM_iff NSLIM_minus)
paulson@14477
   395
paulson@14477
   396
lemma LIM_add_minus2: "[| f -- x --> l; g -- x --> m |] ==> (%x. f(x) + -g(x)) -- x --> (l + -m)"
paulson@14477
   397
by (simp add: LIM_NSLIM_iff NSLIM_add_minus)
paulson@14477
   398
huffman@20552
   399
lemma LIM_inverse:
huffman@20653
   400
  fixes L :: "'a::real_normed_div_algebra"
huffman@20552
   401
  shows "[| f -- a --> L; L \<noteq> 0 |]
huffman@20552
   402
      ==> (%x. inverse(f(x))) -- a --> (inverse L)"
paulson@14477
   403
by (simp add: LIM_NSLIM_iff NSLIM_inverse)
paulson@14477
   404
paulson@14477
   405
lemma LIM_zero2: "f -- a --> l ==> (%x. f(x) + -l) -- a --> 0"
paulson@14477
   406
by (simp add: LIM_NSLIM_iff NSLIM_zero)
paulson@14477
   407
paulson@14477
   408
lemma LIM_zero_cancel: "(%x. f(x) - l) -- x --> 0 ==> f -- x --> l"
paulson@14477
   409
apply (drule_tac g = "%x. l" and M = l in LIM_add)
paulson@14477
   410
apply (auto simp add: diff_minus add_assoc)
paulson@14477
   411
done
paulson@14477
   412
huffman@20561
   413
lemma LIM_unique2:
huffman@20561
   414
  fixes a :: real
huffman@20561
   415
  shows "[| f -- a --> L; f -- a --> M |] ==> L = M"
paulson@14477
   416
by (simp add: LIM_NSLIM_iff NSLIM_unique)
paulson@14477
   417
paulson@14477
   418
(* we can use the corresponding thm LIM_mult2 *)
paulson@14477
   419
(* for standard definition of limit           *)
paulson@14477
   420
huffman@20552
   421
lemma LIM_mult_zero2:
huffman@20561
   422
  fixes f g :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_algebra"
huffman@20552
   423
  shows "[| f -- x --> 0; g -- x --> 0 |] ==> (%x. f(x)*g(x)) -- x --> 0"
paulson@14477
   424
by (drule LIM_mult2, auto)
paulson@14477
   425
paulson@14477
   426
huffman@20755
   427
subsection {* Continuity *}
paulson@14477
   428
paulson@14477
   429
lemma isNSContD: "[| isNSCont f a; y \<approx> hypreal_of_real a |] ==> ( *f* f) y \<approx> hypreal_of_real (f a)"
paulson@14477
   430
by (simp add: isNSCont_def)
paulson@14477
   431
paulson@14477
   432
lemma isNSCont_NSLIM: "isNSCont f a ==> f -- a --NS> (f a) "
paulson@14477
   433
by (simp add: isNSCont_def NSLIM_def)
paulson@14477
   434
paulson@14477
   435
lemma NSLIM_isNSCont: "f -- a --NS> (f a) ==> isNSCont f a"
paulson@14477
   436
apply (simp add: isNSCont_def NSLIM_def, auto)
huffman@20561
   437
apply (case_tac "y = star_of a", auto)
paulson@14477
   438
done
paulson@14477
   439
paulson@15228
   440
text{*NS continuity can be defined using NS Limit in
paulson@15228
   441
    similar fashion to standard def of continuity*}
paulson@14477
   442
lemma isNSCont_NSLIM_iff: "(isNSCont f a) = (f -- a --NS> (f a))"
paulson@14477
   443
by (blast intro: isNSCont_NSLIM NSLIM_isNSCont)
paulson@14477
   444
paulson@15228
   445
text{*Hence, NS continuity can be given
paulson@15228
   446
  in terms of standard limit*}
paulson@14477
   447
lemma isNSCont_LIM_iff: "(isNSCont f a) = (f -- a --> (f a))"
paulson@14477
   448
by (simp add: LIM_NSLIM_iff isNSCont_NSLIM_iff)
paulson@14477
   449
paulson@15228
   450
text{*Moreover, it's trivial now that NS continuity
paulson@15228
   451
  is equivalent to standard continuity*}
paulson@14477
   452
lemma isNSCont_isCont_iff: "(isNSCont f a) = (isCont f a)"
paulson@14477
   453
apply (simp add: isCont_def)
paulson@14477
   454
apply (rule isNSCont_LIM_iff)
paulson@14477
   455
done
paulson@14477
   456
paulson@15228
   457
text{*Standard continuity ==> NS continuity*}
paulson@14477
   458
lemma isCont_isNSCont: "isCont f a ==> isNSCont f a"
paulson@14477
   459
by (erule isNSCont_isCont_iff [THEN iffD2])
paulson@14477
   460
paulson@15228
   461
text{*NS continuity ==> Standard continuity*}
paulson@14477
   462
lemma isNSCont_isCont: "isNSCont f a ==> isCont f a"
paulson@14477
   463
by (erule isNSCont_isCont_iff [THEN iffD1])
paulson@14477
   464
paulson@14477
   465
text{*Alternative definition of continuity*}
paulson@14477
   466
(* Prove equivalence between NS limits - *)
paulson@14477
   467
(* seems easier than using standard def  *)
paulson@14477
   468
lemma NSLIM_h_iff: "(f -- a --NS> L) = ((%h. f(a + h)) -- 0 --NS> L)"
paulson@14477
   469
apply (simp add: NSLIM_def, auto)
huffman@20561
   470
apply (drule_tac x = "star_of a + x" in spec)
huffman@20561
   471
apply (drule_tac [2] x = "- star_of a + x" in spec, safe, simp)
huffman@20561
   472
apply (erule mem_infmal_iff [THEN iffD2, THEN Infinitesimal_add_approx_self [THEN approx_sym]])
huffman@20561
   473
apply (erule_tac [3] approx_minus_iff2 [THEN iffD1])
huffman@20561
   474
 prefer 2 apply (simp add: add_commute diff_def [symmetric])
huffman@20561
   475
apply (rule_tac x = x in star_cases)
huffman@17318
   476
apply (rule_tac [2] x = x in star_cases)
huffman@17318
   477
apply (auto simp add: starfun star_of_def star_n_minus star_n_add add_assoc approx_refl star_n_zero_num)
paulson@14477
   478
done
paulson@14477
   479
paulson@14477
   480
lemma NSLIM_isCont_iff: "(f -- a --NS> f a) = ((%h. f(a + h)) -- 0 --NS> f a)"
paulson@14477
   481
by (rule NSLIM_h_iff)
paulson@14477
   482
paulson@14477
   483
lemma LIM_isCont_iff: "(f -- a --> f a) = ((%h. f(a + h)) -- 0 --> f(a))"
paulson@14477
   484
by (simp add: LIM_NSLIM_iff NSLIM_isCont_iff)
paulson@14477
   485
paulson@14477
   486
lemma isCont_iff: "(isCont f x) = ((%h. f(x + h)) -- 0 --> f(x))"
paulson@14477
   487
by (simp add: isCont_def LIM_isCont_iff)
paulson@14477
   488
paulson@15228
   489
text{*Immediate application of nonstandard criterion for continuity can offer
paulson@15228
   490
   very simple proofs of some standard property of continuous functions*}
paulson@14477
   491
text{*sum continuous*}
paulson@14477
   492
lemma isCont_add: "[| isCont f a; isCont g a |] ==> isCont (%x. f(x) + g(x)) a"
paulson@14477
   493
by (auto intro: approx_add simp add: isNSCont_isCont_iff [symmetric] isNSCont_def)
paulson@14477
   494
paulson@14477
   495
text{*mult continuous*}
huffman@20552
   496
lemma isCont_mult:
huffman@20561
   497
  fixes f g :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_algebra"
huffman@20552
   498
  shows "[| isCont f a; isCont g a |] ==> isCont (%x. f(x) * g(x)) a"
paulson@15228
   499
by (auto intro!: starfun_mult_HFinite_approx
paulson@15228
   500
            simp del: starfun_mult [symmetric]
paulson@14477
   501
            simp add: isNSCont_isCont_iff [symmetric] isNSCont_def)
paulson@14477
   502
paulson@15228
   503
text{*composition of continuous functions
paulson@15228
   504
     Note very short straightforard proof!*}
paulson@14477
   505
lemma isCont_o: "[| isCont f a; isCont g (f a) |] ==> isCont (g o f) a"
paulson@14477
   506
by (auto simp add: isNSCont_isCont_iff [symmetric] isNSCont_def starfun_o [symmetric])
paulson@14477
   507
paulson@14477
   508
lemma isCont_o2: "[| isCont f a; isCont g (f a) |] ==> isCont (%x. g (f x)) a"
paulson@14477
   509
by (auto dest: isCont_o simp add: o_def)
paulson@14477
   510
paulson@14477
   511
lemma isNSCont_minus: "isNSCont f a ==> isNSCont (%x. - f x) a"
paulson@14477
   512
by (simp add: isNSCont_def)
paulson@14477
   513
paulson@14477
   514
lemma isCont_minus: "isCont f a ==> isCont (%x. - f x) a"
paulson@14477
   515
by (auto simp add: isNSCont_isCont_iff [symmetric] isNSCont_minus)
paulson@14477
   516
paulson@14477
   517
lemma isCont_inverse:
huffman@20653
   518
  fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_div_algebra"
huffman@20552
   519
  shows "[| isCont f x; f x \<noteq> 0 |] ==> isCont (%x. inverse (f x)) x"
paulson@14477
   520
apply (simp add: isCont_def)
paulson@14477
   521
apply (blast intro: LIM_inverse)
paulson@14477
   522
done
paulson@14477
   523
huffman@20552
   524
lemma isNSCont_inverse:
huffman@20653
   525
  fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_div_algebra"
huffman@20552
   526
  shows "[| isNSCont f x; f x \<noteq> 0 |] ==> isNSCont (%x. inverse (f x)) x"
paulson@14477
   527
by (auto intro: isCont_inverse simp add: isNSCont_isCont_iff)
paulson@14477
   528
paulson@14477
   529
lemma isCont_diff:
paulson@14477
   530
      "[| isCont f a; isCont g a |] ==> isCont (%x. f(x) - g(x)) a"
paulson@14477
   531
apply (simp add: diff_minus)
paulson@14477
   532
apply (auto intro: isCont_add isCont_minus)
paulson@14477
   533
done
paulson@14477
   534
paulson@15228
   535
lemma isCont_const [simp]: "isCont (%x. k) a"
paulson@14477
   536
by (simp add: isCont_def)
paulson@14477
   537
paulson@15228
   538
lemma isNSCont_const [simp]: "isNSCont (%x. k) a"
paulson@14477
   539
by (simp add: isNSCont_def)
paulson@14477
   540
huffman@20561
   541
lemma isNSCont_abs [simp]: "isNSCont abs (a::real)"
paulson@14477
   542
apply (simp add: isNSCont_def)
paulson@14477
   543
apply (auto intro: approx_hrabs simp add: hypreal_of_real_hrabs [symmetric] starfun_rabs_hrabs)
paulson@14477
   544
done
paulson@14477
   545
huffman@20561
   546
lemma isCont_abs [simp]: "isCont abs (a::real)"
paulson@14477
   547
by (auto simp add: isNSCont_isCont_iff [symmetric])
paulson@15228
   548
paulson@14477
   549
paulson@14477
   550
(****************************************************************
paulson@14477
   551
(%* Leave as commented until I add topology theory or remove? *%)
paulson@14477
   552
(%*------------------------------------------------------------
paulson@14477
   553
  Elementary topology proof for a characterisation of
paulson@14477
   554
  continuity now: a function f is continuous if and only
paulson@14477
   555
  if the inverse image, {x. f(x) \<in> A}, of any open set A
paulson@14477
   556
  is always an open set
paulson@14477
   557
 ------------------------------------------------------------*%)
paulson@14477
   558
Goal "[| isNSopen A; \<forall>x. isNSCont f x |]
paulson@14477
   559
               ==> isNSopen {x. f x \<in> A}"
paulson@14477
   560
by (auto_tac (claset(),simpset() addsimps [isNSopen_iff1]));
paulson@14477
   561
by (dtac (mem_monad_approx RS approx_sym);
paulson@14477
   562
by (dres_inst_tac [("x","a")] spec 1);
paulson@14477
   563
by (dtac isNSContD 1 THEN assume_tac 1)
paulson@14477
   564
by (dtac bspec 1 THEN assume_tac 1)
paulson@14477
   565
by (dres_inst_tac [("x","( *f* f) x")] approx_mem_monad2 1);
paulson@14477
   566
by (blast_tac (claset() addIs [starfun_mem_starset]);
paulson@14477
   567
qed "isNSCont_isNSopen";
paulson@14477
   568
paulson@14477
   569
Goalw [isNSCont_def]
paulson@14477
   570
          "\<forall>A. isNSopen A --> isNSopen {x. f x \<in> A} \
paulson@14477
   571
\              ==> isNSCont f x";
paulson@14477
   572
by (auto_tac (claset() addSIs [(mem_infmal_iff RS iffD1) RS
paulson@14477
   573
     (approx_minus_iff RS iffD2)],simpset() addsimps
paulson@14477
   574
      [Infinitesimal_def,SReal_iff]));
paulson@14477
   575
by (dres_inst_tac [("x","{z. abs(z + -f(x)) < ya}")] spec 1);
paulson@14477
   576
by (etac (isNSopen_open_interval RSN (2,impE));
paulson@14477
   577
by (auto_tac (claset(),simpset() addsimps [isNSopen_def,isNSnbhd_def]));
paulson@14477
   578
by (dres_inst_tac [("x","x")] spec 1);
paulson@14477
   579
by (auto_tac (claset() addDs [approx_sym RS approx_mem_monad],
paulson@14477
   580
    simpset() addsimps [hypreal_of_real_zero RS sym,STAR_starfun_rabs_add_minus]));
paulson@14477
   581
qed "isNSopen_isNSCont";
paulson@14477
   582
paulson@14477
   583
Goal "(\<forall>x. isNSCont f x) = \
paulson@14477
   584
\     (\<forall>A. isNSopen A --> isNSopen {x. f(x) \<in> A})";
paulson@14477
   585
by (blast_tac (claset() addIs [isNSCont_isNSopen,
paulson@14477
   586
    isNSopen_isNSCont]);
paulson@14477
   587
qed "isNSCont_isNSopen_iff";
paulson@14477
   588
paulson@14477
   589
(%*------- Standard version of same theorem --------*%)
paulson@14477
   590
Goal "(\<forall>x. isCont f x) = \
paulson@14477
   591
\         (\<forall>A. isopen A --> isopen {x. f(x) \<in> A})";
paulson@14477
   592
by (auto_tac (claset() addSIs [isNSCont_isNSopen_iff],
paulson@14477
   593
              simpset() addsimps [isNSopen_isopen_iff RS sym,
paulson@14477
   594
              isNSCont_isCont_iff RS sym]));
paulson@14477
   595
qed "isCont_isopen_iff";
paulson@14477
   596
*******************************************************************)
paulson@14477
   597
huffman@20755
   598
subsection {* Uniform Continuity *}
huffman@20755
   599
paulson@14477
   600
lemma isNSUContD: "[| isNSUCont f; x \<approx> y|] ==> ( *f* f) x \<approx> ( *f* f) y"
paulson@14477
   601
by (simp add: isNSUCont_def)
paulson@14477
   602
paulson@14477
   603
lemma isUCont_isCont: "isUCont f ==> isCont f x"
paulson@14477
   604
by (simp add: isUCont_def isCont_def LIM_def, meson)
paulson@14477
   605
huffman@20754
   606
lemma isUCont_isNSUCont:
huffman@20754
   607
  fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
huffman@20754
   608
  assumes f: "isUCont f" shows "isNSUCont f"
huffman@20754
   609
proof (unfold isNSUCont_def, safe)
huffman@20754
   610
  fix x y :: "'a star"
huffman@20754
   611
  assume approx: "x \<approx> y"
huffman@20754
   612
  have "starfun f x - starfun f y \<in> Infinitesimal"
huffman@20754
   613
  proof (rule InfinitesimalI2)
huffman@20754
   614
    fix r::real assume r: "0 < r"
huffman@20754
   615
    with f obtain s where s: "0 < s" and
huffman@20754
   616
      less_r: "\<And>x y. norm (x - y) < s \<Longrightarrow> norm (f x - f y) < r"
huffman@20754
   617
      by (auto simp add: isUCont_def)
huffman@20754
   618
    from less_r have less_r':
huffman@20754
   619
       "\<And>x y. hnorm (x - y) < star_of s
huffman@20754
   620
        \<Longrightarrow> hnorm (starfun f x - starfun f y) < star_of r"
huffman@20754
   621
      by transfer
huffman@20754
   622
    from approx have "x - y \<in> Infinitesimal"
huffman@20754
   623
      by (unfold approx_def)
huffman@20754
   624
    hence "hnorm (x - y) < star_of s"
huffman@20754
   625
      using s by (rule InfinitesimalD2)
huffman@20754
   626
    thus "hnorm (starfun f x - starfun f y) < star_of r"
huffman@20754
   627
      by (rule less_r')
huffman@20754
   628
  qed
huffman@20754
   629
  thus "starfun f x \<approx> starfun f y"
huffman@20754
   630
    by (unfold approx_def)
huffman@20754
   631
qed
paulson@14477
   632
paulson@14477
   633
lemma isNSUCont_isUCont:
huffman@20754
   634
  fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
huffman@20754
   635
  assumes f: "isNSUCont f" shows "isUCont f"
huffman@20754
   636
proof (unfold isUCont_def, safe)
huffman@20754
   637
  fix r::real assume r: "0 < r"
huffman@20754
   638
  have "\<exists>s>0. \<forall>x y. hnorm (x - y) < s
huffman@20754
   639
        \<longrightarrow> hnorm (starfun f x - starfun f y) < star_of r"
huffman@20754
   640
  proof (rule exI, safe)
huffman@20754
   641
    show "0 < epsilon" by (rule hypreal_epsilon_gt_zero)
huffman@20754
   642
  next
huffman@20754
   643
    fix x y :: "'a star"
huffman@20754
   644
    assume "hnorm (x - y) < epsilon"
huffman@20754
   645
    with Infinitesimal_epsilon
huffman@20754
   646
    have "x - y \<in> Infinitesimal"
huffman@20754
   647
      by (rule hnorm_less_Infinitesimal)
huffman@20754
   648
    hence "x \<approx> y"
huffman@20754
   649
      by (unfold approx_def)
huffman@20754
   650
    with f have "starfun f x \<approx> starfun f y"
huffman@20754
   651
      by (simp add: isNSUCont_def)
huffman@20754
   652
    hence "starfun f x - starfun f y \<in> Infinitesimal"
huffman@20754
   653
      by (unfold approx_def)
huffman@20754
   654
    thus "hnorm (starfun f x - starfun f y) < star_of r"
huffman@20754
   655
      using r by (rule InfinitesimalD2)
huffman@20754
   656
  qed
huffman@20754
   657
  thus "\<exists>s>0. \<forall>x y. norm (x - y) < s \<longrightarrow> norm (f x - f y) < r"
huffman@20754
   658
    by transfer
huffman@20754
   659
qed
paulson@14477
   660
huffman@20755
   661
subsection {* Derivatives *}
huffman@20755
   662
huffman@20756
   663
subsubsection {* Purely standard proofs *}
huffman@20756
   664
huffman@20563
   665
lemma DERIV_iff: "(DERIV f x :> D) = ((%h. (f(x + h) - f(x))/h) -- 0 --> D)"
paulson@14477
   666
by (simp add: deriv_def)
paulson@14477
   667
huffman@20563
   668
lemma DERIV_D: "DERIV f x :> D ==> (%h. (f(x + h) - f(x))/h) -- 0 --> D"
paulson@14477
   669
by (simp add: deriv_def)
paulson@14477
   670
paulson@14477
   671
lemma DERIV_unique:
paulson@14477
   672
      "[| DERIV f x :> D; DERIV f x :> E |] ==> D = E"
paulson@14477
   673
apply (simp add: deriv_def)
paulson@14477
   674
apply (blast intro: LIM_unique)
paulson@14477
   675
done
paulson@14477
   676
huffman@20756
   677
text{*Alternative definition for differentiability*}
huffman@20756
   678
huffman@20756
   679
lemma DERIV_LIM_iff:
huffman@20756
   680
     "((%h::real. (f(a + h) - f(a)) / h) -- 0 --> D) =
huffman@20756
   681
      ((%x. (f(x)-f(a)) / (x-a)) -- a --> D)"
huffman@20756
   682
apply (rule iffI)
huffman@20756
   683
apply (drule_tac k="- a" in LIM_shift)
huffman@20756
   684
apply (simp add: diff_minus)
huffman@20756
   685
apply (drule_tac k="a" in LIM_shift)
huffman@20756
   686
apply (simp add: add_commute)
huffman@20756
   687
done
huffman@20756
   688
huffman@20756
   689
lemma DERIV_iff2: "(DERIV f x :> D) = ((%z. (f(z) - f(x)) / (z-x)) -- x --> D)"
huffman@20756
   690
by (simp add: deriv_def diff_minus [symmetric] DERIV_LIM_iff)
huffman@20756
   691
huffman@20756
   692
(* ------------------------------------------------------------------------ *)
huffman@20756
   693
(* Caratheodory formulation of derivative at a point: standard proof        *)
huffman@20756
   694
(* ------------------------------------------------------------------------ *)
huffman@20756
   695
huffman@20756
   696
lemma CARAT_DERIV:
huffman@20756
   697
     "(DERIV f x :> l) =
huffman@20756
   698
      (\<exists>g. (\<forall>z. f z - f x = g z * (z-x)) & isCont g x & g x = l)"
huffman@20756
   699
      (is "?lhs = ?rhs")
huffman@20756
   700
proof
huffman@20756
   701
  assume der: "DERIV f x :> l"
huffman@20756
   702
  show "\<exists>g. (\<forall>z. f z - f x = g z * (z-x)) \<and> isCont g x \<and> g x = l"
huffman@20756
   703
  proof (intro exI conjI)
huffman@20756
   704
    let ?g = "(%z. if z = x then l else (f z - f x) / (z-x))"
huffman@20756
   705
    show "\<forall>z. f z - f x = ?g z * (z-x)" by (simp)
huffman@20756
   706
    show "isCont ?g x" using der
huffman@20756
   707
      by (simp add: isCont_iff DERIV_iff diff_minus
huffman@20756
   708
               cong: LIM_equal [rule_format])
huffman@20756
   709
    show "?g x = l" by simp
huffman@20756
   710
  qed
huffman@20756
   711
next
huffman@20756
   712
  assume "?rhs"
huffman@20756
   713
  then obtain g where
huffman@20756
   714
    "(\<forall>z. f z - f x = g z * (z-x))" and "isCont g x" and "g x = l" by blast
huffman@20756
   715
  thus "(DERIV f x :> l)"
huffman@20756
   716
     by (auto simp add: isCont_iff DERIV_iff diff_minus
huffman@20756
   717
               cong: LIM_equal [rule_format])
huffman@20756
   718
qed
huffman@20756
   719
huffman@20756
   720
huffman@20756
   721
huffman@20756
   722
subsubsection {* Purely nonstandard proofs *}
huffman@20756
   723
huffman@20756
   724
lemma DERIV_NS_iff:
huffman@20756
   725
      "(DERIV f x :> D) = ((%h. (f(x + h) - f(x))/h) -- 0 --NS> D)"
huffman@20756
   726
by (simp add: deriv_def LIM_NSLIM_iff)
huffman@20756
   727
huffman@20756
   728
lemma NS_DERIV_D: "DERIV f x :> D ==> (%h. (f(x + h) - f(x))/h) -- 0 --NS> D"
huffman@20756
   729
by (simp add: deriv_def LIM_NSLIM_iff)
huffman@20756
   730
paulson@14477
   731
lemma NSDeriv_unique:
paulson@14477
   732
     "[| NSDERIV f x :> D; NSDERIV f x :> E |] ==> D = E"
paulson@14477
   733
apply (simp add: nsderiv_def)
paulson@14477
   734
apply (cut_tac Infinitesimal_epsilon hypreal_epsilon_not_zero)
paulson@15228
   735
apply (auto dest!: bspec [where x=epsilon]
paulson@15228
   736
            intro!: inj_hypreal_of_real [THEN injD]
paulson@14477
   737
            dest: approx_trans3)
paulson@14477
   738
done
paulson@14477
   739
huffman@20755
   740
text {*First NSDERIV in terms of NSLIM*}
paulson@14477
   741
paulson@15228
   742
text{*first equivalence *}
paulson@14477
   743
lemma NSDERIV_NSLIM_iff:
huffman@20563
   744
      "(NSDERIV f x :> D) = ((%h. (f(x + h) - f(x))/h) -- 0 --NS> D)"
paulson@14477
   745
apply (simp add: nsderiv_def NSLIM_def, auto)
paulson@14477
   746
apply (drule_tac x = xa in bspec)
paulson@14477
   747
apply (rule_tac [3] ccontr)
paulson@14477
   748
apply (drule_tac [3] x = h in spec)
paulson@14477
   749
apply (auto simp add: mem_infmal_iff starfun_lambda_cancel)
paulson@14477
   750
done
paulson@14477
   751
paulson@15228
   752
text{*second equivalence *}
paulson@14477
   753
lemma NSDERIV_NSLIM_iff2:
paulson@14477
   754
     "(NSDERIV f x :> D) = ((%z. (f(z) - f(x)) / (z-x)) -- x --NS> D)"
paulson@15228
   755
by (simp add: NSDERIV_NSLIM_iff DERIV_LIM_iff  diff_minus [symmetric]
paulson@14477
   756
              LIM_NSLIM_iff [symmetric])
paulson@14477
   757
paulson@14477
   758
(* while we're at it! *)
paulson@14477
   759
lemma NSDERIV_iff2:
paulson@14477
   760
     "(NSDERIV f x :> D) =
paulson@14477
   761
      (\<forall>w.
paulson@14477
   762
        w \<noteq> hypreal_of_real x & w \<approx> hypreal_of_real x -->
paulson@14477
   763
        ( *f* (%z. (f z - f x) / (z-x))) w \<approx> hypreal_of_real D)"
paulson@14477
   764
by (simp add: NSDERIV_NSLIM_iff2 NSLIM_def)
paulson@14477
   765
paulson@14477
   766
(*FIXME DELETE*)
huffman@20563
   767
lemma hypreal_not_eq_minus_iff: "(x \<noteq> a) = (x - a \<noteq> (0::hypreal))"
paulson@14477
   768
by (auto dest: hypreal_eq_minus_iff [THEN iffD2])
paulson@14477
   769
paulson@14477
   770
lemma NSDERIVD5:
paulson@14477
   771
  "(NSDERIV f x :> D) ==>
paulson@14477
   772
   (\<forall>u. u \<approx> hypreal_of_real x -->
paulson@14477
   773
     ( *f* (%z. f z - f x)) u \<approx> hypreal_of_real D * (u - hypreal_of_real x))"
paulson@14477
   774
apply (auto simp add: NSDERIV_iff2)
paulson@14477
   775
apply (case_tac "u = hypreal_of_real x", auto)
paulson@14477
   776
apply (drule_tac x = u in spec, auto)
paulson@14477
   777
apply (drule_tac c = "u - hypreal_of_real x" and b = "hypreal_of_real D" in approx_mult1)
paulson@14477
   778
apply (drule_tac [!] hypreal_not_eq_minus_iff [THEN iffD1])
paulson@14477
   779
apply (subgoal_tac [2] "( *f* (%z. z-x)) u \<noteq> (0::hypreal) ")
huffman@20563
   780
apply (auto simp add:
kleing@19023
   781
         approx_minus_iff [THEN iffD1, THEN mem_infmal_iff [THEN iffD2]]
kleing@19023
   782
         Infinitesimal_subset_HFinite [THEN subsetD])
paulson@14477
   783
done
paulson@14477
   784
paulson@14477
   785
lemma NSDERIVD4:
paulson@14477
   786
     "(NSDERIV f x :> D) ==>
paulson@14477
   787
      (\<forall>h \<in> Infinitesimal.
paulson@14477
   788
               (( *f* f)(hypreal_of_real x + h) -
paulson@14477
   789
                 hypreal_of_real (f x))\<approx> (hypreal_of_real D) * h)"
paulson@14477
   790
apply (auto simp add: nsderiv_def)
paulson@14477
   791
apply (case_tac "h = (0::hypreal) ")
paulson@14477
   792
apply (auto simp add: diff_minus)
paulson@14477
   793
apply (drule_tac x = h in bspec)
paulson@14477
   794
apply (drule_tac [2] c = h in approx_mult1)
paulson@14477
   795
apply (auto intro: Infinitesimal_subset_HFinite [THEN subsetD]
nipkow@15539
   796
            simp add: diff_minus)
paulson@14477
   797
done
paulson@14477
   798
paulson@14477
   799
lemma NSDERIVD3:
paulson@14477
   800
     "(NSDERIV f x :> D) ==>
paulson@14477
   801
      (\<forall>h \<in> Infinitesimal - {0}.
paulson@14477
   802
               (( *f* f)(hypreal_of_real x + h) -
paulson@14477
   803
                 hypreal_of_real (f x))\<approx> (hypreal_of_real D) * h)"
paulson@14477
   804
apply (auto simp add: nsderiv_def)
paulson@14477
   805
apply (rule ccontr, drule_tac x = h in bspec)
paulson@14477
   806
apply (drule_tac [2] c = h in approx_mult1)
paulson@14477
   807
apply (auto intro: Infinitesimal_subset_HFinite [THEN subsetD]
nipkow@15539
   808
            simp add: mult_assoc diff_minus)
paulson@14477
   809
done
paulson@14477
   810
paulson@15228
   811
text{*Differentiability implies continuity
paulson@15228
   812
         nice and simple "algebraic" proof*}
paulson@14477
   813
lemma NSDERIV_isNSCont: "NSDERIV f x :> D ==> isNSCont f x"
paulson@14477
   814
apply (auto simp add: nsderiv_def isNSCont_NSLIM_iff NSLIM_def)
paulson@14477
   815
apply (drule approx_minus_iff [THEN iffD1])
paulson@14477
   816
apply (drule hypreal_not_eq_minus_iff [THEN iffD1])
huffman@20563
   817
apply (drule_tac x = "xa - hypreal_of_real x" in bspec)
paulson@15228
   818
 prefer 2 apply (simp add: add_assoc [symmetric])
paulson@15234
   819
apply (auto simp add: mem_infmal_iff [symmetric] add_commute)
huffman@20563
   820
apply (drule_tac c = "xa - hypreal_of_real x" in approx_mult1)
paulson@14477
   821
apply (auto intro: Infinitesimal_subset_HFinite [THEN subsetD]
paulson@14477
   822
            simp add: mult_assoc)
paulson@14477
   823
apply (drule_tac x3=D in
paulson@14477
   824
           HFinite_hypreal_of_real [THEN [2] Infinitesimal_HFinite_mult,
paulson@14477
   825
             THEN mem_infmal_iff [THEN iffD1]])
nipkow@15539
   826
apply (auto simp add: mult_commute
paulson@14477
   827
            intro: approx_trans approx_minus_iff [THEN iffD2])
paulson@14477
   828
done
paulson@14477
   829
paulson@15228
   830
text{*Differentiation rules for combinations of functions
paulson@14477
   831
      follow from clear, straightforard, algebraic
paulson@15228
   832
      manipulations*}
paulson@14477
   833
text{*Constant function*}
paulson@14477
   834
paulson@14477
   835
(* use simple constant nslimit theorem *)
paulson@15228
   836
lemma NSDERIV_const [simp]: "(NSDERIV (%x. k) x :> 0)"
paulson@14477
   837
by (simp add: NSDERIV_NSLIM_iff)
paulson@14477
   838
paulson@15228
   839
text{*Sum of functions- proved easily*}
paulson@14477
   840
paulson@14477
   841
lemma NSDERIV_add: "[| NSDERIV f x :> Da;  NSDERIV g x :> Db |]
paulson@14477
   842
      ==> NSDERIV (%x. f x + g x) x :> Da + Db"
paulson@14477
   843
apply (auto simp add: NSDERIV_NSLIM_iff NSLIM_def)
huffman@20563
   844
apply (auto simp add: add_divide_distrib diff_divide_distrib dest!: spec)
paulson@14477
   845
apply (drule_tac b = "hypreal_of_real Da" and d = "hypreal_of_real Db" in approx_add)
huffman@20563
   846
apply (auto simp add: diff_def add_ac)
paulson@14477
   847
done
paulson@14477
   848
paulson@15228
   849
text{*Product of functions - Proof is trivial but tedious
paulson@15228
   850
  and long due to rearrangement of terms*}
paulson@14477
   851
huffman@20563
   852
lemma lemma_nsderiv1: "((a::hypreal)*b) - (c*d) = (b*(a - c)) + (c*(b - d))"
huffman@20563
   853
by (simp add: right_diff_distrib)
paulson@14477
   854
huffman@20563
   855
lemma lemma_nsderiv2: "[| (x - y) / z = hypreal_of_real D + yb; z \<noteq> 0;
paulson@14477
   856
         z \<in> Infinitesimal; yb \<in> Infinitesimal |]
huffman@20563
   857
      ==> x - y \<approx> 0"
paulson@14477
   858
apply (frule_tac c1 = z in hypreal_mult_right_cancel [THEN iffD2], assumption)
huffman@20563
   859
apply (erule_tac V = "(x - y) / z = hypreal_of_real D + yb" in thin_rl)
paulson@14477
   860
apply (auto intro!: Infinitesimal_HFinite_mult2 HFinite_add
nipkow@15539
   861
            simp add: mult_assoc mem_infmal_iff [symmetric])
paulson@14477
   862
apply (erule Infinitesimal_subset_HFinite [THEN subsetD])
paulson@14477
   863
done
paulson@14477
   864
paulson@14477
   865
lemma NSDERIV_mult: "[| NSDERIV f x :> Da; NSDERIV g x :> Db |]
paulson@14477
   866
      ==> NSDERIV (%x. f x * g x) x :> (Da * g(x)) + (Db * f(x))"
paulson@14477
   867
apply (auto simp add: NSDERIV_NSLIM_iff NSLIM_def)
paulson@14477
   868
apply (auto dest!: spec
kleing@19023
   869
      simp add: starfun_lambda_cancel lemma_nsderiv1)
huffman@20563
   870
apply (simp (no_asm) add: add_divide_distrib diff_divide_distrib)
paulson@14477
   871
apply (drule bex_Infinitesimal_iff2 [THEN iffD2])+
paulson@15234
   872
apply (auto simp add: times_divide_eq_right [symmetric]
paulson@15234
   873
            simp del: times_divide_eq)
huffman@20563
   874
apply (drule_tac D = Db in lemma_nsderiv2, assumption+)
huffman@20563
   875
apply (drule_tac
paulson@15228
   876
     approx_minus_iff [THEN iffD2, THEN bex_Infinitesimal_iff2 [THEN iffD2]])
paulson@15228
   877
apply (auto intro!: approx_add_mono1
paulson@14477
   878
            simp add: left_distrib right_distrib mult_commute add_assoc)
paulson@15228
   879
apply (rule_tac b1 = "hypreal_of_real Db * hypreal_of_real (f x)"
paulson@14477
   880
         in add_commute [THEN subst])
paulson@15228
   881
apply (auto intro!: Infinitesimal_add_approx_self2 [THEN approx_sym]
paulson@15228
   882
                    Infinitesimal_add Infinitesimal_mult
paulson@15228
   883
                    Infinitesimal_hypreal_of_real_mult
paulson@14477
   884
                    Infinitesimal_hypreal_of_real_mult2
paulson@14477
   885
          simp add: add_assoc [symmetric])
paulson@14477
   886
done
paulson@14477
   887
paulson@14477
   888
text{*Multiplying by a constant*}
paulson@14477
   889
lemma NSDERIV_cmult: "NSDERIV f x :> D
paulson@14477
   890
      ==> NSDERIV (%x. c * f x) x :> c*D"
paulson@15228
   891
apply (simp only: times_divide_eq_right [symmetric] NSDERIV_NSLIM_iff
huffman@20563
   892
                  minus_mult_right right_diff_distrib [symmetric])
paulson@14477
   893
apply (erule NSLIM_const [THEN NSLIM_mult])
paulson@14477
   894
done
paulson@14477
   895
paulson@14477
   896
text{*Negation of function*}
paulson@14477
   897
lemma NSDERIV_minus: "NSDERIV f x :> D ==> NSDERIV (%x. -(f x)) x :> -D"
paulson@14477
   898
proof (simp add: NSDERIV_NSLIM_iff)
huffman@20563
   899
  assume "(\<lambda>h. (f (x + h) - f x) / h) -- 0 --NS> D"
huffman@20563
   900
  hence deriv: "(\<lambda>h. - ((f(x+h) - f x) / h)) -- 0 --NS> - D"
paulson@14477
   901
    by (rule NSLIM_minus)
huffman@20563
   902
  have "\<forall>h. - ((f (x + h) - f x) / h) = (- f (x + h) + f x) / h"
paulson@15228
   903
    by (simp add: minus_divide_left)
paulson@14477
   904
  with deriv
paulson@14477
   905
  show "(\<lambda>h. (- f (x + h) + f x) / h) -- 0 --NS> - D" by simp
paulson@14477
   906
qed
paulson@14477
   907
paulson@14477
   908
text{*Subtraction*}
paulson@14477
   909
lemma NSDERIV_add_minus: "[| NSDERIV f x :> Da; NSDERIV g x :> Db |] ==> NSDERIV (%x. f x + -g x) x :> Da + -Db"
paulson@14477
   910
by (blast dest: NSDERIV_add NSDERIV_minus)
paulson@14477
   911
paulson@14477
   912
lemma NSDERIV_diff:
paulson@14477
   913
     "[| NSDERIV f x :> Da; NSDERIV g x :> Db |]
paulson@14477
   914
      ==> NSDERIV (%x. f x - g x) x :> Da-Db"
paulson@14477
   915
apply (simp add: diff_minus)
paulson@14477
   916
apply (blast intro: NSDERIV_add_minus)
paulson@14477
   917
done
paulson@14477
   918
paulson@14477
   919
text{*  Similarly to the above, the chain rule admits an entirely
paulson@14477
   920
   straightforward derivation. Compare this with Harrison's
paulson@14477
   921
   HOL proof of the chain rule, which proved to be trickier and
paulson@14477
   922
   required an alternative characterisation of differentiability-
paulson@14477
   923
   the so-called Carathedory derivative. Our main problem is
paulson@14477
   924
   manipulation of terms.*}
paulson@14477
   925
paulson@14477
   926
paulson@14477
   927
(* lemmas *)
paulson@14477
   928
lemma NSDERIV_zero:
paulson@14477
   929
      "[| NSDERIV g x :> D;
paulson@14477
   930
               ( *f* g) (hypreal_of_real(x) + xa) = hypreal_of_real(g x);
paulson@14477
   931
               xa \<in> Infinitesimal;
paulson@14477
   932
               xa \<noteq> 0
paulson@14477
   933
            |] ==> D = 0"
paulson@14477
   934
apply (simp add: nsderiv_def)
paulson@14477
   935
apply (drule bspec, auto)
paulson@14477
   936
done
paulson@14477
   937
paulson@14477
   938
(* can be proved differently using NSLIM_isCont_iff *)
paulson@14477
   939
lemma NSDERIV_approx:
paulson@14477
   940
     "[| NSDERIV f x :> D;  h \<in> Infinitesimal;  h \<noteq> 0 |]
huffman@20563
   941
      ==> ( *f* f) (hypreal_of_real(x) + h) - hypreal_of_real(f x) \<approx> 0"
paulson@14477
   942
apply (simp add: nsderiv_def)
paulson@14477
   943
apply (simp add: mem_infmal_iff [symmetric])
paulson@14477
   944
apply (rule Infinitesimal_ratio)
paulson@14477
   945
apply (rule_tac [3] approx_hypreal_of_real_HFinite, auto)
paulson@14477
   946
done
paulson@14477
   947
paulson@14477
   948
(*---------------------------------------------------------------
paulson@14477
   949
   from one version of differentiability
paulson@14477
   950
paulson@14477
   951
                f(x) - f(a)
paulson@14477
   952
              --------------- \<approx> Db
paulson@14477
   953
                  x - a
paulson@14477
   954
 ---------------------------------------------------------------*)
paulson@14477
   955
lemma NSDERIVD1: "[| NSDERIV f (g x) :> Da;
paulson@14477
   956
         ( *f* g) (hypreal_of_real(x) + xa) \<noteq> hypreal_of_real (g x);
paulson@14477
   957
         ( *f* g) (hypreal_of_real(x) + xa) \<approx> hypreal_of_real (g x)
paulson@14477
   958
      |] ==> (( *f* f) (( *f* g) (hypreal_of_real(x) + xa))
huffman@20563
   959
                   - hypreal_of_real (f (g x)))
huffman@20563
   960
              / (( *f* g) (hypreal_of_real(x) + xa) - hypreal_of_real (g x))
paulson@14477
   961
             \<approx> hypreal_of_real(Da)"
paulson@14477
   962
by (auto simp add: NSDERIV_NSLIM_iff2 NSLIM_def diff_minus [symmetric])
paulson@14477
   963
paulson@14477
   964
(*--------------------------------------------------------------
paulson@14477
   965
   from other version of differentiability
paulson@14477
   966
paulson@14477
   967
                f(x + h) - f(x)
paulson@14477
   968
               ----------------- \<approx> Db
paulson@14477
   969
                       h
paulson@14477
   970
 --------------------------------------------------------------*)
paulson@14477
   971
lemma NSDERIVD2: "[| NSDERIV g x :> Db; xa \<in> Infinitesimal; xa \<noteq> 0 |]
huffman@20563
   972
      ==> (( *f* g) (hypreal_of_real(x) + xa) - hypreal_of_real(g x)) / xa
paulson@14477
   973
          \<approx> hypreal_of_real(Db)"
paulson@14477
   974
by (auto simp add: NSDERIV_NSLIM_iff NSLIM_def mem_infmal_iff starfun_lambda_cancel)
paulson@14477
   975
paulson@14477
   976
lemma lemma_chain: "(z::hypreal) \<noteq> 0 ==> x*y = (x*inverse(z))*(z*y)"
paulson@14477
   977
by auto
paulson@14477
   978
paulson@15228
   979
text{*This proof uses both definitions of differentiability.*}
paulson@14477
   980
lemma NSDERIV_chain: "[| NSDERIV f (g x) :> Da; NSDERIV g x :> Db |]
paulson@14477
   981
      ==> NSDERIV (f o g) x :> Da * Db"
paulson@14477
   982
apply (simp (no_asm_simp) add: NSDERIV_NSLIM_iff NSLIM_def
paulson@14477
   983
                mem_infmal_iff [symmetric])
paulson@14477
   984
apply clarify
paulson@14477
   985
apply (frule_tac f = g in NSDERIV_approx)
paulson@14477
   986
apply (auto simp add: starfun_lambda_cancel2 starfun_o [symmetric])
paulson@14477
   987
apply (case_tac "( *f* g) (hypreal_of_real (x) + xa) = hypreal_of_real (g x) ")
paulson@14477
   988
apply (drule_tac g = g in NSDERIV_zero)
paulson@14477
   989
apply (auto simp add: divide_inverse)
huffman@20563
   990
apply (rule_tac z1 = "( *f* g) (hypreal_of_real (x) + xa) - hypreal_of_real (g x) " and y1 = "inverse xa" in lemma_chain [THEN ssubst])
paulson@14477
   991
apply (erule hypreal_not_eq_minus_iff [THEN iffD1])
paulson@14477
   992
apply (rule approx_mult_hypreal_of_real)
paulson@14477
   993
apply (simp_all add: divide_inverse [symmetric])
paulson@14477
   994
apply (blast intro: NSDERIVD1 approx_minus_iff [THEN iffD2])
paulson@14477
   995
apply (blast intro: NSDERIVD2)
paulson@14477
   996
done
paulson@14477
   997
paulson@14477
   998
text{*Differentiation of natural number powers*}
paulson@15228
   999
lemma NSDERIV_Id [simp]: "NSDERIV (%x. x) x :> 1"
paulson@15228
  1000
by (simp add: NSDERIV_NSLIM_iff NSLIM_def divide_self del: divide_self_if)
paulson@14477
  1001
paulson@15228
  1002
lemma NSDERIV_cmult_Id [simp]: "NSDERIV (op * c) x :> c"
huffman@20756
  1003
by (cut_tac c = c and x = x in NSDERIV_Id [THEN NSDERIV_cmult], simp)
paulson@14477
  1004
paulson@14477
  1005
(*Can't get rid of x \<noteq> 0 because it isn't continuous at zero*)
paulson@14477
  1006
lemma NSDERIV_inverse:
paulson@14477
  1007
     "x \<noteq> 0 ==> NSDERIV (%x. inverse(x)) x :> (- (inverse x ^ Suc (Suc 0)))"
paulson@14477
  1008
apply (simp add: nsderiv_def)
paulson@15228
  1009
apply (rule ballI, simp, clarify)
huffman@20563
  1010
apply (frule (1) Infinitesimal_add_not_zero)
huffman@20563
  1011
apply (simp add: add_commute)
huffman@20563
  1012
(*apply (auto simp add: starfun_inverse_inverse realpow_two
huffman@20563
  1013
        simp del: minus_mult_left [symmetric] minus_mult_right [symmetric])*)
paulson@14477
  1014
apply (simp add: inverse_add inverse_mult_distrib [symmetric]
huffman@20563
  1015
              inverse_minus_eq [symmetric] add_ac mult_ac diff_def
paulson@15228
  1016
            del: inverse_mult_distrib inverse_minus_eq
paulson@14477
  1017
                 minus_mult_left [symmetric] minus_mult_right [symmetric])
paulson@14477
  1018
apply (simp (no_asm_simp) add: mult_assoc [symmetric] right_distrib
paulson@14477
  1019
            del: minus_mult_left [symmetric] minus_mult_right [symmetric])
paulson@15234
  1020
apply (rule_tac y = "inverse (- hypreal_of_real x * hypreal_of_real x)" in approx_trans)
paulson@14477
  1021
apply (rule inverse_add_Infinitesimal_approx2)
paulson@15228
  1022
apply (auto dest!: hypreal_of_real_HFinite_diff_Infinitesimal
paulson@14477
  1023
            simp add: inverse_minus_eq [symmetric] HFinite_minus_iff)
paulson@14477
  1024
apply (rule Infinitesimal_HFinite_mult2, auto)
paulson@14477
  1025
done
paulson@14477
  1026
huffman@20756
  1027
subsubsection {* Equivalence of NS and Standard definitions *}
paulson@14477
  1028
huffman@20756
  1029
text{*Now equivalence between NSDERIV and DERIV*}
huffman@20756
  1030
lemma NSDERIV_DERIV_iff: "(NSDERIV f x :> D) = (DERIV f x :> D)"
huffman@20756
  1031
by (simp add: deriv_def NSDERIV_NSLIM_iff LIM_NSLIM_iff)
paulson@14477
  1032
huffman@20756
  1033
text{*Now Standard proof*}
huffman@20756
  1034
lemma DERIV_isCont: "DERIV f x :> D ==> isCont f x"
huffman@20756
  1035
by (simp add: NSDERIV_DERIV_iff [symmetric] isNSCont_isCont_iff [symmetric]
huffman@20756
  1036
              NSDERIV_isNSCont)
huffman@20756
  1037
huffman@20756
  1038
lemma DERIV_const [simp]: "(DERIV (%x. k) x :> 0)"
huffman@20756
  1039
by (simp add: NSDERIV_DERIV_iff [symmetric])
huffman@20756
  1040
huffman@20756
  1041
(* Standard theorem *)
huffman@20756
  1042
lemma DERIV_add: "[| DERIV f x :> Da; DERIV g x :> Db |]
huffman@20756
  1043
      ==> DERIV (%x. f x + g x) x :> Da + Db"
huffman@20756
  1044
apply (simp add: NSDERIV_add NSDERIV_DERIV_iff [symmetric])
huffman@20756
  1045
done
huffman@20756
  1046
huffman@20756
  1047
lemma DERIV_mult:
huffman@20756
  1048
     "[| DERIV f x :> Da; DERIV g x :> Db |]
huffman@20756
  1049
      ==> DERIV (%x. f x * g x) x :> (Da * g(x)) + (Db * f(x))"
huffman@20756
  1050
by (simp add: NSDERIV_mult NSDERIV_DERIV_iff [symmetric])
huffman@20756
  1051
huffman@20756
  1052
(* let's do the standard proof though theorem *)
huffman@20756
  1053
(* LIM_mult2 follows from a NS proof          *)
huffman@20756
  1054
huffman@20756
  1055
lemma DERIV_cmult:
huffman@20756
  1056
      "DERIV f x :> D ==> DERIV (%x. c * f x) x :> c*D"
huffman@20756
  1057
apply (simp only: deriv_def times_divide_eq_right [symmetric]
huffman@20756
  1058
                  NSDERIV_NSLIM_iff minus_mult_right right_diff_distrib [symmetric])
huffman@20756
  1059
apply (erule LIM_const [THEN LIM_mult2])
huffman@20756
  1060
done
huffman@20756
  1061
huffman@20756
  1062
lemma DERIV_minus: "DERIV f x :> D ==> DERIV (%x. -(f x)) x :> -D"
huffman@20756
  1063
by (simp add: NSDERIV_minus NSDERIV_DERIV_iff [symmetric])
huffman@20756
  1064
huffman@20756
  1065
lemma DERIV_add_minus: "[| DERIV f x :> Da; DERIV g x :> Db |] ==> DERIV (%x. f x + -g x) x :> Da + -Db"
huffman@20756
  1066
by (blast dest: DERIV_add DERIV_minus)
huffman@20756
  1067
huffman@20756
  1068
lemma DERIV_diff:
huffman@20756
  1069
     "[| DERIV f x :> Da; DERIV g x :> Db |]
huffman@20756
  1070
       ==> DERIV (%x. f x - g x) x :> Da-Db"
huffman@20756
  1071
apply (simp add: diff_minus)
huffman@20756
  1072
apply (blast intro: DERIV_add_minus)
huffman@20756
  1073
done
huffman@20756
  1074
huffman@20756
  1075
(* standard version *)
huffman@20756
  1076
lemma DERIV_chain: "[| DERIV f (g x) :> Da; DERIV g x :> Db |] ==> DERIV (f o g) x :> Da * Db"
huffman@20756
  1077
by (simp add: NSDERIV_DERIV_iff [symmetric] NSDERIV_chain)
huffman@20756
  1078
huffman@20756
  1079
lemma DERIV_chain2: "[| DERIV f (g x) :> Da; DERIV g x :> Db |] ==> DERIV (%x. f (g x)) x :> Da * Db"
huffman@20756
  1080
by (auto dest: DERIV_chain simp add: o_def)
huffman@20756
  1081
huffman@20756
  1082
(*derivative of the identity function*)
huffman@20756
  1083
lemma DERIV_Id [simp]: "DERIV (%x. x) x :> 1"
huffman@20756
  1084
by (simp add: NSDERIV_DERIV_iff [symmetric])
huffman@20756
  1085
huffman@20756
  1086
lemmas isCont_Id = DERIV_Id [THEN DERIV_isCont, standard]
huffman@20756
  1087
huffman@20756
  1088
(*derivative of linear multiplication*)
huffman@20756
  1089
lemma DERIV_cmult_Id [simp]: "DERIV (op * c) x :> c"
huffman@20756
  1090
by (cut_tac c = c and x = x in DERIV_Id [THEN DERIV_cmult], simp)
huffman@20756
  1091
huffman@20756
  1092
lemma DERIV_pow: "DERIV (%x. x ^ n) x :> real n * (x ^ (n - Suc 0))"
huffman@20756
  1093
apply (induct "n")
huffman@20756
  1094
apply (drule_tac [2] DERIV_Id [THEN DERIV_mult])
huffman@20756
  1095
apply (auto simp add: real_of_nat_Suc left_distrib)
huffman@20756
  1096
apply (case_tac "0 < n")
huffman@20756
  1097
apply (drule_tac x = x in realpow_minus_mult)
huffman@20756
  1098
apply (auto simp add: mult_assoc add_commute)
huffman@20756
  1099
done
huffman@20756
  1100
huffman@20756
  1101
(* NS version *)
huffman@20756
  1102
lemma NSDERIV_pow: "NSDERIV (%x. x ^ n) x :> real n * (x ^ (n - Suc 0))"
huffman@20756
  1103
by (simp add: NSDERIV_DERIV_iff DERIV_pow)
huffman@20756
  1104
huffman@20756
  1105
text{*Power of -1*}
paulson@14477
  1106
paulson@14477
  1107
lemma DERIV_inverse: "x \<noteq> 0 ==> DERIV (%x. inverse(x)) x :> (-(inverse x ^ Suc (Suc 0)))"
paulson@14477
  1108
by (simp add: NSDERIV_inverse NSDERIV_DERIV_iff [symmetric] del: realpow_Suc)
paulson@14477
  1109
paulson@14477
  1110
text{*Derivative of inverse*}
paulson@14477
  1111
lemma DERIV_inverse_fun: "[| DERIV f x :> d; f(x) \<noteq> 0 |]
paulson@14477
  1112
      ==> DERIV (%x. inverse(f x)) x :> (- (d * inverse(f(x) ^ Suc (Suc 0))))"
paulson@14477
  1113
apply (simp only: mult_commute [of d] minus_mult_left power_inverse)
paulson@14477
  1114
apply (fold o_def)
paulson@14477
  1115
apply (blast intro!: DERIV_chain DERIV_inverse)
paulson@14477
  1116
done
paulson@14477
  1117
paulson@14477
  1118
lemma NSDERIV_inverse_fun: "[| NSDERIV f x :> d; f(x) \<noteq> 0 |]
paulson@14477
  1119
      ==> NSDERIV (%x. inverse(f x)) x :> (- (d * inverse(f(x) ^ Suc (Suc 0))))"
paulson@14477
  1120
by (simp add: NSDERIV_DERIV_iff DERIV_inverse_fun del: realpow_Suc)
paulson@14477
  1121
paulson@14477
  1122
text{*Derivative of quotient*}
paulson@14477
  1123
lemma DERIV_quotient: "[| DERIV f x :> d; DERIV g x :> e; g(x) \<noteq> 0 |]
huffman@20563
  1124
       ==> DERIV (%y. f(y) / (g y)) x :> (d*g(x) - (e*f(x))) / (g(x) ^ Suc (Suc 0))"
paulson@14477
  1125
apply (drule_tac f = g in DERIV_inverse_fun)
paulson@14477
  1126
apply (drule_tac [2] DERIV_mult)
paulson@14477
  1127
apply (assumption+)
paulson@14477
  1128
apply (simp add: divide_inverse right_distrib power_inverse minus_mult_left
huffman@20563
  1129
                 mult_ac diff_def
paulson@14477
  1130
     del: realpow_Suc minus_mult_right [symmetric] minus_mult_left [symmetric])
paulson@14477
  1131
done
paulson@14477
  1132
paulson@14477
  1133
lemma NSDERIV_quotient: "[| NSDERIV f x :> d; DERIV g x :> e; g(x) \<noteq> 0 |]
paulson@14477
  1134
       ==> NSDERIV (%y. f(y) / (g y)) x :> (d*g(x)
huffman@20563
  1135
                            - (e*f(x))) / (g(x) ^ Suc (Suc 0))"
paulson@14477
  1136
by (simp add: NSDERIV_DERIV_iff DERIV_quotient del: realpow_Suc)
paulson@14477
  1137
paulson@14477
  1138
lemma CARAT_NSDERIV: "NSDERIV f x :> l ==>
paulson@14477
  1139
      \<exists>g. (\<forall>z. f z - f x = g z * (z-x)) & isNSCont g x & g x = l"
paulson@14477
  1140
by (auto simp add: NSDERIV_DERIV_iff isNSCont_isCont_iff CARAT_DERIV)
paulson@14477
  1141
paulson@14477
  1142
lemma hypreal_eq_minus_iff3: "(x = y + z) = (x + -z = (y::hypreal))"
paulson@14477
  1143
by auto
paulson@14477
  1144
paulson@14477
  1145
lemma CARAT_DERIVD:
paulson@14477
  1146
  assumes all: "\<forall>z. f z - f x = g z * (z-x)"
paulson@14477
  1147
      and nsc: "isNSCont g x"
paulson@14477
  1148
  shows "NSDERIV f x :> g x"
paulson@14477
  1149
proof -
paulson@14477
  1150
  from nsc
paulson@14477
  1151
  have "\<forall>w. w \<noteq> hypreal_of_real x \<and> w \<approx> hypreal_of_real x \<longrightarrow>
paulson@14477
  1152
         ( *f* g) w * (w - hypreal_of_real x) / (w - hypreal_of_real x) \<approx>
paulson@15228
  1153
         hypreal_of_real (g x)"
paulson@14477
  1154
    by (simp add: diff_minus isNSCont_def)
paulson@14477
  1155
  thus ?thesis using all
paulson@15228
  1156
    by (simp add: NSDERIV_iff2 starfun_if_eq cong: if_cong)
paulson@14477
  1157
qed
paulson@14477
  1158
huffman@20755
  1159
subsubsection {* Differentiability predicate *}
huffman@20755
  1160
huffman@20755
  1161
lemma differentiableD: "f differentiable x ==> \<exists>D. DERIV f x :> D"
huffman@20755
  1162
by (simp add: differentiable_def)
huffman@20755
  1163
huffman@20755
  1164
lemma differentiableI: "DERIV f x :> D ==> f differentiable x"
huffman@20755
  1165
by (force simp add: differentiable_def)
huffman@20755
  1166
huffman@20755
  1167
lemma NSdifferentiableD: "f NSdifferentiable x ==> \<exists>D. NSDERIV f x :> D"
huffman@20755
  1168
by (simp add: NSdifferentiable_def)
huffman@20755
  1169
huffman@20755
  1170
lemma NSdifferentiableI: "NSDERIV f x :> D ==> f NSdifferentiable x"
huffman@20755
  1171
by (force simp add: NSdifferentiable_def)
huffman@20755
  1172
huffman@20755
  1173
lemma differentiable_const: "(\<lambda>z. a) differentiable x"
huffman@20755
  1174
  apply (unfold differentiable_def)
huffman@20755
  1175
  apply (rule_tac x=0 in exI)
huffman@20755
  1176
  apply simp
huffman@20755
  1177
  done
huffman@20755
  1178
huffman@20755
  1179
lemma differentiable_sum:
huffman@20755
  1180
  assumes "f differentiable x"
huffman@20755
  1181
  and "g differentiable x"
huffman@20755
  1182
  shows "(\<lambda>x. f x + g x) differentiable x"
huffman@20755
  1183
proof -
huffman@20755
  1184
  from prems have "\<exists>D. DERIV f x :> D" by (unfold differentiable_def)
huffman@20755
  1185
  then obtain df where "DERIV f x :> df" ..
huffman@20755
  1186
  moreover from prems have "\<exists>D. DERIV g x :> D" by (unfold differentiable_def)
huffman@20755
  1187
  then obtain dg where "DERIV g x :> dg" ..
huffman@20755
  1188
  ultimately have "DERIV (\<lambda>x. f x + g x) x :> df + dg" by (rule DERIV_add)
huffman@20755
  1189
  hence "\<exists>D. DERIV (\<lambda>x. f x + g x) x :> D" by auto
huffman@20755
  1190
  thus ?thesis by (fold differentiable_def)
huffman@20755
  1191
qed
huffman@20755
  1192
huffman@20755
  1193
lemma differentiable_diff:
huffman@20755
  1194
  assumes "f differentiable x"
huffman@20755
  1195
  and "g differentiable x"
huffman@20755
  1196
  shows "(\<lambda>x. f x - g x) differentiable x"
huffman@20755
  1197
proof -
huffman@20755
  1198
  from prems have "f differentiable x" by simp
huffman@20755
  1199
  moreover
huffman@20755
  1200
  from prems have "\<exists>D. DERIV g x :> D" by (unfold differentiable_def)
huffman@20755
  1201
  then obtain dg where "DERIV g x :> dg" ..
huffman@20755
  1202
  then have "DERIV (\<lambda>x. - g x) x :> -dg" by (rule DERIV_minus)
huffman@20755
  1203
  hence "\<exists>D. DERIV (\<lambda>x. - g x) x :> D" by auto
huffman@20755
  1204
  hence "(\<lambda>x. - g x) differentiable x" by (fold differentiable_def)
huffman@20755
  1205
  ultimately 
huffman@20755
  1206
  show ?thesis
huffman@20755
  1207
    by (auto simp: real_diff_def dest: differentiable_sum)
huffman@20755
  1208
qed
huffman@20755
  1209
huffman@20755
  1210
lemma differentiable_mult:
huffman@20755
  1211
  assumes "f differentiable x"
huffman@20755
  1212
  and "g differentiable x"
huffman@20755
  1213
  shows "(\<lambda>x. f x * g x) differentiable x"
huffman@20755
  1214
proof -
huffman@20755
  1215
  from prems have "\<exists>D. DERIV f x :> D" by (unfold differentiable_def)
huffman@20755
  1216
  then obtain df where "DERIV f x :> df" ..
huffman@20755
  1217
  moreover from prems have "\<exists>D. DERIV g x :> D" by (unfold differentiable_def)
huffman@20755
  1218
  then obtain dg where "DERIV g x :> dg" ..
huffman@20755
  1219
  ultimately have "DERIV (\<lambda>x. f x * g x) x :> df * g x + dg * f x" by (simp add: DERIV_mult)
huffman@20755
  1220
  hence "\<exists>D. DERIV (\<lambda>x. f x * g x) x :> D" by auto
huffman@20755
  1221
  thus ?thesis by (fold differentiable_def)
huffman@20755
  1222
qed
huffman@20755
  1223
huffman@20755
  1224
subsection {*(NS) Increment*}
huffman@20755
  1225
lemma incrementI:
huffman@20755
  1226
      "f NSdifferentiable x ==>
huffman@20755
  1227
      increment f x h = ( *f* f) (hypreal_of_real(x) + h) -
huffman@20755
  1228
      hypreal_of_real (f x)"
huffman@20755
  1229
by (simp add: increment_def)
huffman@20755
  1230
huffman@20755
  1231
lemma incrementI2: "NSDERIV f x :> D ==>
huffman@20755
  1232
     increment f x h = ( *f* f) (hypreal_of_real(x) + h) -
huffman@20755
  1233
     hypreal_of_real (f x)"
huffman@20755
  1234
apply (erule NSdifferentiableI [THEN incrementI])
huffman@20755
  1235
done
huffman@20755
  1236
huffman@20755
  1237
(* The Increment theorem -- Keisler p. 65 *)
huffman@20755
  1238
lemma increment_thm: "[| NSDERIV f x :> D; h \<in> Infinitesimal; h \<noteq> 0 |]
huffman@20755
  1239
      ==> \<exists>e \<in> Infinitesimal. increment f x h = hypreal_of_real(D)*h + e*h"
huffman@20755
  1240
apply (frule_tac h = h in incrementI2, simp add: nsderiv_def)
huffman@20755
  1241
apply (drule bspec, auto)
huffman@20755
  1242
apply (drule bex_Infinitesimal_iff2 [THEN iffD2], clarify)
huffman@20755
  1243
apply (frule_tac b1 = "hypreal_of_real (D) + y"
huffman@20755
  1244
        in hypreal_mult_right_cancel [THEN iffD2])
huffman@20755
  1245
apply (erule_tac [2] V = "(( *f* f) (hypreal_of_real (x) + h) - hypreal_of_real (f x)) / h = hypreal_of_real (D) + y" in thin_rl)
huffman@20755
  1246
apply assumption
huffman@20755
  1247
apply (simp add: times_divide_eq_right [symmetric])
huffman@20755
  1248
apply (auto simp add: left_distrib)
huffman@20755
  1249
done
huffman@20755
  1250
huffman@20755
  1251
lemma increment_thm2:
huffman@20755
  1252
     "[| NSDERIV f x :> D; h \<approx> 0; h \<noteq> 0 |]
huffman@20755
  1253
      ==> \<exists>e \<in> Infinitesimal. increment f x h =
huffman@20755
  1254
              hypreal_of_real(D)*h + e*h"
huffman@20755
  1255
by (blast dest!: mem_infmal_iff [THEN iffD2] intro!: increment_thm)
huffman@20755
  1256
huffman@20755
  1257
huffman@20755
  1258
lemma increment_approx_zero: "[| NSDERIV f x :> D; h \<approx> 0; h \<noteq> 0 |]
huffman@20755
  1259
      ==> increment f x h \<approx> 0"
huffman@20755
  1260
apply (drule increment_thm2,
huffman@20755
  1261
       auto intro!: Infinitesimal_HFinite_mult2 HFinite_add simp add: left_distrib [symmetric] mem_infmal_iff [symmetric])
huffman@20755
  1262
apply (erule Infinitesimal_subset_HFinite [THEN subsetD])
huffman@20755
  1263
done
huffman@20755
  1264
huffman@20755
  1265
subsection {* Nested Intervals and Bisection *}
huffman@20755
  1266
paulson@15234
  1267
text{*Lemmas about nested intervals and proof by bisection (cf.Harrison).
paulson@15234
  1268
     All considerably tidied by lcp.*}
paulson@14477
  1269
paulson@14477
  1270
lemma lemma_f_mono_add [rule_format (no_asm)]: "(\<forall>n. (f::nat=>real) n \<le> f (Suc n)) --> f m \<le> f(m + no)"
paulson@15251
  1271
apply (induct "no")
paulson@14477
  1272
apply (auto intro: order_trans)
paulson@14477
  1273
done
paulson@14477
  1274
paulson@14477
  1275
lemma f_inc_g_dec_Beq_f: "[| \<forall>n. f(n) \<le> f(Suc n);
paulson@14477
  1276
         \<forall>n. g(Suc n) \<le> g(n);
paulson@14477
  1277
         \<forall>n. f(n) \<le> g(n) |]
huffman@20552
  1278
      ==> Bseq (f :: nat \<Rightarrow> real)"
paulson@14477
  1279
apply (rule_tac k = "f 0" and K = "g 0" in BseqI2, rule allI)
paulson@14477
  1280
apply (induct_tac "n")
paulson@14477
  1281
apply (auto intro: order_trans)
paulson@15234
  1282
apply (rule_tac y = "g (Suc na)" in order_trans)
paulson@14477
  1283
apply (induct_tac [2] "na")
paulson@14477
  1284
apply (auto intro: order_trans)
paulson@14477
  1285
done
paulson@14477
  1286
paulson@14477
  1287
lemma f_inc_g_dec_Beq_g: "[| \<forall>n. f(n) \<le> f(Suc n);
paulson@14477
  1288
         \<forall>n. g(Suc n) \<le> g(n);
paulson@14477
  1289
         \<forall>n. f(n) \<le> g(n) |]
huffman@20552
  1290
      ==> Bseq (g :: nat \<Rightarrow> real)"
paulson@14477
  1291
apply (subst Bseq_minus_iff [symmetric])
paulson@15234
  1292
apply (rule_tac g = "%x. - (f x)" in f_inc_g_dec_Beq_f)
paulson@14477
  1293
apply auto
paulson@14477
  1294
done
paulson@14477
  1295
huffman@20693
  1296
lemma f_inc_imp_le_lim:
huffman@20693
  1297
  fixes f :: "nat \<Rightarrow> real"
huffman@20693
  1298
  shows "\<lbrakk>\<forall>n. f n \<le> f (Suc n); convergent f\<rbrakk> \<Longrightarrow> f n \<le> lim f"
paulson@14477
  1299
apply (rule linorder_not_less [THEN iffD1])
paulson@14477
  1300
apply (auto simp add: convergent_LIMSEQ_iff LIMSEQ_iff monoseq_Suc)
paulson@14477
  1301
apply (drule real_less_sum_gt_zero)
paulson@14477
  1302
apply (drule_tac x = "f n + - lim f" in spec, safe)
paulson@14477
  1303
apply (drule_tac P = "%na. no\<le>na --> ?Q na" and x = "no + n" in spec, auto)
paulson@14477
  1304
apply (subgoal_tac "lim f \<le> f (no + n) ")
paulson@14477
  1305
apply (drule_tac no=no and m=n in lemma_f_mono_add)
paulson@14477
  1306
apply (auto simp add: add_commute)
webertj@20254
  1307
apply (induct_tac "no")
webertj@20254
  1308
apply simp
webertj@20254
  1309
apply (auto intro: order_trans simp add: diff_minus abs_if)
paulson@14477
  1310
done
paulson@14477
  1311
paulson@14477
  1312
lemma lim_uminus: "convergent g ==> lim (%x. - g x) = - (lim g)"
paulson@14477
  1313
apply (rule LIMSEQ_minus [THEN limI])
paulson@14477
  1314
apply (simp add: convergent_LIMSEQ_iff)
paulson@14477
  1315
done
paulson@14477
  1316
huffman@20693
  1317
lemma g_dec_imp_lim_le:
huffman@20693
  1318
  fixes g :: "nat \<Rightarrow> real"
huffman@20693
  1319
  shows "\<lbrakk>\<forall>n. g (Suc n) \<le> g(n); convergent g\<rbrakk> \<Longrightarrow> lim g \<le> g n"
paulson@14477
  1320
apply (subgoal_tac "- (g n) \<le> - (lim g) ")
paulson@15234
  1321
apply (cut_tac [2] f = "%x. - (g x)" in f_inc_imp_le_lim)
paulson@14477
  1322
apply (auto simp add: lim_uminus convergent_minus_iff [symmetric])
paulson@14477
  1323
done
paulson@14477
  1324
paulson@14477
  1325
lemma lemma_nest: "[| \<forall>n. f(n) \<le> f(Suc n);
paulson@14477
  1326
         \<forall>n. g(Suc n) \<le> g(n);
paulson@14477
  1327
         \<forall>n. f(n) \<le> g(n) |]
huffman@20552
  1328
      ==> \<exists>l m :: real. l \<le> m &  ((\<forall>n. f(n) \<le> l) & f ----> l) &
paulson@14477
  1329
                            ((\<forall>n. m \<le> g(n)) & g ----> m)"
paulson@14477
  1330
apply (subgoal_tac "monoseq f & monoseq g")
paulson@14477
  1331
prefer 2 apply (force simp add: LIMSEQ_iff monoseq_Suc)
paulson@14477
  1332
apply (subgoal_tac "Bseq f & Bseq g")
paulson@14477
  1333
prefer 2 apply (blast intro: f_inc_g_dec_Beq_f f_inc_g_dec_Beq_g)
paulson@14477
  1334
apply (auto dest!: Bseq_monoseq_convergent simp add: convergent_LIMSEQ_iff)
paulson@14477
  1335
apply (rule_tac x = "lim f" in exI)
paulson@14477
  1336
apply (rule_tac x = "lim g" in exI)
paulson@14477
  1337
apply (auto intro: LIMSEQ_le)
paulson@14477
  1338
apply (auto simp add: f_inc_imp_le_lim g_dec_imp_lim_le convergent_LIMSEQ_iff)
paulson@14477
  1339
done
paulson@14477
  1340
paulson@14477
  1341
lemma lemma_nest_unique: "[| \<forall>n. f(n) \<le> f(Suc n);
paulson@14477
  1342
         \<forall>n. g(Suc n) \<le> g(n);
paulson@14477
  1343
         \<forall>n. f(n) \<le> g(n);
paulson@14477
  1344
         (%n. f(n) - g(n)) ----> 0 |]
huffman@20552
  1345
      ==> \<exists>l::real. ((\<forall>n. f(n) \<le> l) & f ----> l) &
paulson@14477
  1346
                ((\<forall>n. l \<le> g(n)) & g ----> l)"
paulson@14477
  1347
apply (drule lemma_nest, auto)
paulson@14477
  1348
apply (subgoal_tac "l = m")
paulson@14477
  1349
apply (drule_tac [2] X = f in LIMSEQ_diff)
paulson@14477
  1350
apply (auto intro: LIMSEQ_unique)
paulson@14477
  1351
done
paulson@14477
  1352
paulson@14477
  1353
text{*The universal quantifiers below are required for the declaration
paulson@14477
  1354
  of @{text Bolzano_nest_unique} below.*}
paulson@14477
  1355
paulson@14477
  1356
lemma Bolzano_bisect_le:
paulson@14477
  1357
 "a \<le> b ==> \<forall>n. fst (Bolzano_bisect P a b n) \<le> snd (Bolzano_bisect P a b n)"
paulson@14477
  1358
apply (rule allI)
paulson@14477
  1359
apply (induct_tac "n")
paulson@14477
  1360
apply (auto simp add: Let_def split_def)
paulson@14477
  1361
done
paulson@14477
  1362
paulson@14477
  1363
lemma Bolzano_bisect_fst_le_Suc: "a \<le> b ==>
paulson@14477
  1364
   \<forall>n. fst(Bolzano_bisect P a b n) \<le> fst (Bolzano_bisect P a b (Suc n))"
paulson@14477
  1365
apply (rule allI)
paulson@14477
  1366
apply (induct_tac "n")
paulson@14477
  1367
apply (auto simp add: Bolzano_bisect_le Let_def split_def)
paulson@14477
  1368
done
paulson@14477
  1369
paulson@14477
  1370
lemma Bolzano_bisect_Suc_le_snd: "a \<le> b ==>
paulson@14477
  1371
   \<forall>n. snd(Bolzano_bisect P a b (Suc n)) \<le> snd (Bolzano_bisect P a b n)"
paulson@14477
  1372
apply (rule allI)
paulson@14477
  1373
apply (induct_tac "n")
nipkow@15539
  1374
apply (auto simp add: Bolzano_bisect_le Let_def split_def)
paulson@14477
  1375
done
paulson@14477
  1376
kleing@19023
  1377
lemma eq_divide_2_times_iff: "((x::real) = y / (2 * z)) = (2 * x = y/z)"
nipkow@15539
  1378
apply (auto)
paulson@14477
  1379
apply (drule_tac f = "%u. (1/2) *u" in arg_cong)
nipkow@15539
  1380
apply (simp)
paulson@14477
  1381
done
paulson@14477
  1382
paulson@14477
  1383
lemma Bolzano_bisect_diff:
paulson@14477
  1384
     "a \<le> b ==>
paulson@14477
  1385
      snd(Bolzano_bisect P a b n) - fst(Bolzano_bisect P a b n) =
paulson@14477
  1386
      (b-a) / (2 ^ n)"
paulson@15251
  1387
apply (induct "n")
paulson@14477
  1388
apply (auto simp add: eq_divide_2_times_iff add_divide_distrib Let_def split_def)
paulson@14477
  1389
done
paulson@14477
  1390
paulson@14477
  1391
lemmas Bolzano_nest_unique =
paulson@14477
  1392
    lemma_nest_unique
paulson@14477
  1393
    [OF Bolzano_bisect_fst_le_Suc Bolzano_bisect_Suc_le_snd Bolzano_bisect_le]
paulson@14477
  1394
paulson@14477
  1395
paulson@14477
  1396
lemma not_P_Bolzano_bisect:
paulson@14477
  1397
  assumes P:    "!!a b c. [| P(a,b); P(b,c); a \<le> b; b \<le> c|] ==> P(a,c)"
paulson@14477
  1398
      and notP: "~ P(a,b)"
paulson@14477
  1399
      and le:   "a \<le> b"
paulson@14477
  1400
  shows "~ P(fst(Bolzano_bisect P a b n), snd(Bolzano_bisect P a b n))"
paulson@14477
  1401
proof (induct n)
paulson@14477
  1402
  case 0 thus ?case by simp
paulson@14477
  1403
 next
paulson@14477
  1404
  case (Suc n)
paulson@14477
  1405
  thus ?case
paulson@15228
  1406
 by (auto simp del: surjective_pairing [symmetric]
paulson@15228
  1407
             simp add: Let_def split_def Bolzano_bisect_le [OF le]
paulson@15228
  1408
     P [of "fst (Bolzano_bisect P a b n)" _ "snd (Bolzano_bisect P a b n)"])
paulson@14477
  1409
qed
paulson@14477
  1410
paulson@14477
  1411
(*Now we re-package P_prem as a formula*)
paulson@14477
  1412
lemma not_P_Bolzano_bisect':
paulson@14477
  1413
     "[| \<forall>a b c. P(a,b) & P(b,c) & a \<le> b & b \<le> c --> P(a,c);
paulson@14477
  1414
         ~ P(a,b);  a \<le> b |] ==>
paulson@14477
  1415
      \<forall>n. ~ P(fst(Bolzano_bisect P a b n), snd(Bolzano_bisect P a b n))"
paulson@14477
  1416
by (blast elim!: not_P_Bolzano_bisect [THEN [2] rev_notE])
paulson@14477
  1417
paulson@14477
  1418
paulson@14477
  1419
paulson@14477
  1420
lemma lemma_BOLZANO:
paulson@14477
  1421
     "[| \<forall>a b c. P(a,b) & P(b,c) & a \<le> b & b \<le> c --> P(a,c);
paulson@14477
  1422
         \<forall>x. \<exists>d::real. 0 < d &
paulson@14477
  1423
                (\<forall>a b. a \<le> x & x \<le> b & (b-a) < d --> P(a,b));
paulson@14477
  1424
         a \<le> b |]
paulson@14477
  1425
      ==> P(a,b)"
paulson@14477
  1426
apply (rule Bolzano_nest_unique [where P1=P, THEN exE], assumption+)
paulson@14477
  1427
apply (rule LIMSEQ_minus_cancel)
paulson@14477
  1428
apply (simp (no_asm_simp) add: Bolzano_bisect_diff LIMSEQ_divide_realpow_zero)
paulson@14477
  1429
apply (rule ccontr)
paulson@14477
  1430
apply (drule not_P_Bolzano_bisect', assumption+)
paulson@14477
  1431
apply (rename_tac "l")
paulson@14477
  1432
apply (drule_tac x = l in spec, clarify)
paulson@14477
  1433
apply (simp add: LIMSEQ_def)
paulson@14477
  1434
apply (drule_tac P = "%r. 0<r --> ?Q r" and x = "d/2" in spec)
paulson@14477
  1435
apply (drule_tac P = "%r. 0<r --> ?Q r" and x = "d/2" in spec)
paulson@15228
  1436
apply (drule real_less_half_sum, auto)
paulson@14477
  1437
apply (drule_tac x = "fst (Bolzano_bisect P a b (no + noa))" in spec)
paulson@14477
  1438
apply (drule_tac x = "snd (Bolzano_bisect P a b (no + noa))" in spec)
paulson@14477
  1439
apply safe
paulson@14477
  1440
apply (simp_all (no_asm_simp))
paulson@15234
  1441
apply (rule_tac y = "abs (fst (Bolzano_bisect P a b (no + noa)) - l) + abs (snd (Bolzano_bisect P a b (no + noa)) - l)" in order_le_less_trans)
paulson@14477
  1442
apply (simp (no_asm_simp) add: abs_if)
paulson@14477
  1443
apply (rule real_sum_of_halves [THEN subst])
paulson@14477
  1444
apply (rule add_strict_mono)
paulson@14477
  1445
apply (simp_all add: diff_minus [symmetric])
paulson@14477
  1446
done
paulson@14477
  1447
paulson@14477
  1448
paulson@14477
  1449
lemma lemma_BOLZANO2: "((\<forall>a b c. (a \<le> b & b \<le> c & P(a,b) & P(b,c)) --> P(a,c)) &
paulson@14477
  1450
       (\<forall>x. \<exists>d::real. 0 < d &
paulson@14477
  1451
                (\<forall>a b. a \<le> x & x \<le> b & (b-a) < d --> P(a,b))))
paulson@14477
  1452
      --> (\<forall>a b. a \<le> b --> P(a,b))"
paulson@14477
  1453
apply clarify
paulson@14477
  1454
apply (blast intro: lemma_BOLZANO)
paulson@14477
  1455
done
paulson@14477
  1456
paulson@14477
  1457
huffman@20755
  1458
subsection {* Intermediate Value Theorem *}
huffman@20755
  1459
huffman@20755
  1460
text {*Prove Contrapositive by Bisection*}
paulson@14477
  1461
huffman@20561
  1462
lemma IVT: "[| f(a::real) \<le> (y::real); y \<le> f(b);
paulson@14477
  1463
         a \<le> b;
paulson@14477
  1464
         (\<forall>x. a \<le> x & x \<le> b --> isCont f x) |]
paulson@14477
  1465
      ==> \<exists>x. a \<le> x & x \<le> b & f(x) = y"
paulson@14477
  1466
apply (rule contrapos_pp, assumption)
paulson@14477
  1467
apply (cut_tac P = "% (u,v) . a \<le> u & u \<le> v & v \<le> b --> ~ (f (u) \<le> y & y \<le> f (v))" in lemma_BOLZANO2)
paulson@14477
  1468
apply safe
paulson@14477
  1469
apply simp_all
paulson@14477
  1470
apply (simp add: isCont_iff LIM_def)
paulson@14477
  1471
apply (rule ccontr)
paulson@14477
  1472
apply (subgoal_tac "a \<le> x & x \<le> b")
paulson@14477
  1473
 prefer 2
paulson@15228
  1474
 apply simp
paulson@14477
  1475
 apply (drule_tac P = "%d. 0<d --> ?P d" and x = 1 in spec, arith)
paulson@14477
  1476
apply (drule_tac x = x in spec)+
paulson@14477
  1477
apply simp
nipkow@15360
  1478
apply (drule_tac P = "%r. ?P r --> (\<exists>s>0. ?Q r s) " and x = "\<bar>y - f x\<bar>" in spec)
paulson@14477
  1479
apply safe
paulson@14477
  1480
apply simp
paulson@14477
  1481
apply (drule_tac x = s in spec, clarify)
paulson@14477
  1482
apply (cut_tac x = "f x" and y = y in linorder_less_linear, safe)
paulson@14477
  1483
apply (drule_tac x = "ba-x" in spec)
paulson@14477
  1484
apply (simp_all add: abs_if)
paulson@14477
  1485
apply (drule_tac x = "aa-x" in spec)
paulson@14477
  1486
apply (case_tac "x \<le> aa", simp_all)
paulson@14477
  1487
done
paulson@14477
  1488
huffman@20561
  1489
lemma IVT2: "[| f(b::real) \<le> (y::real); y \<le> f(a);
paulson@14477
  1490
         a \<le> b;
paulson@14477
  1491
         (\<forall>x. a \<le> x & x \<le> b --> isCont f x)
paulson@14477
  1492
      |] ==> \<exists>x. a \<le> x & x \<le> b & f(x) = y"
paulson@15228
  1493
apply (subgoal_tac "- f a \<le> -y & -y \<le> - f b", clarify)
paulson@14477
  1494
apply (drule IVT [where f = "%x. - f x"], assumption)
paulson@14477
  1495
apply (auto intro: isCont_minus)
paulson@14477
  1496
done
paulson@14477
  1497
paulson@14477
  1498
(*HOL style here: object-level formulations*)
huffman@20561
  1499
lemma IVT_objl: "(f(a::real) \<le> (y::real) & y \<le> f(b) & a \<le> b &
paulson@14477
  1500
      (\<forall>x. a \<le> x & x \<le> b --> isCont f x))
paulson@14477
  1501
      --> (\<exists>x. a \<le> x & x \<le> b & f(x) = y)"
paulson@14477
  1502
apply (blast intro: IVT)
paulson@14477
  1503
done
paulson@14477
  1504
huffman@20561
  1505
lemma IVT2_objl: "(f(b::real) \<le> (y::real) & y \<le> f(a) & a \<le> b &
paulson@14477
  1506
      (\<forall>x. a \<le> x & x \<le> b --> isCont f x))
paulson@14477
  1507
      --> (\<exists>x. a \<le> x & x \<le> b & f(x) = y)"
paulson@14477
  1508
apply (blast intro: IVT2)
paulson@14477
  1509
done
paulson@14477
  1510
huffman@20755
  1511
text{*By bisection, function continuous on closed interval is bounded above*}
paulson@14477
  1512
paulson@14477
  1513
lemma isCont_bounded:
paulson@14477
  1514
     "[| a \<le> b; \<forall>x. a \<le> x & x \<le> b --> isCont f x |]
huffman@20561
  1515
      ==> \<exists>M::real. \<forall>x::real. a \<le> x & x \<le> b --> f(x) \<le> M"
paulson@15234
  1516
apply (cut_tac P = "% (u,v) . a \<le> u & u \<le> v & v \<le> b --> (\<exists>M. \<forall>x. u \<le> x & x \<le> v --> f x \<le> M)" in lemma_BOLZANO2)
paulson@14477
  1517
apply safe
paulson@14477
  1518
apply simp_all
paulson@14477
  1519
apply (rename_tac x xa ya M Ma)
paulson@14477
  1520
apply (cut_tac x = M and y = Ma in linorder_linear, safe)
paulson@14477
  1521
apply (rule_tac x = Ma in exI, clarify)
paulson@14477
  1522
apply (cut_tac x = xb and y = xa in linorder_linear, force)
paulson@14477
  1523
apply (rule_tac x = M in exI, clarify)
paulson@14477
  1524
apply (cut_tac x = xb and y = xa in linorder_linear, force)
paulson@14477
  1525
apply (case_tac "a \<le> x & x \<le> b")
paulson@14477
  1526
apply (rule_tac [2] x = 1 in exI)
paulson@14477
  1527
prefer 2 apply force
paulson@14477
  1528
apply (simp add: LIM_def isCont_iff)
paulson@14477
  1529
apply (drule_tac x = x in spec, auto)
paulson@14477
  1530
apply (erule_tac V = "\<forall>M. \<exists>x. a \<le> x & x \<le> b & ~ f x \<le> M" in thin_rl)
paulson@14477
  1531
apply (drule_tac x = 1 in spec, auto)
paulson@14477
  1532
apply (rule_tac x = s in exI, clarify)
paulson@14477
  1533
apply (rule_tac x = "\<bar>f x\<bar> + 1" in exI, clarify)
paulson@14477
  1534
apply (drule_tac x = "xa-x" in spec)
webertj@20217
  1535
apply (auto simp add: abs_ge_self)
paulson@14477
  1536
done
paulson@14477
  1537
paulson@15234
  1538
text{*Refine the above to existence of least upper bound*}
paulson@14477
  1539
paulson@14477
  1540
lemma lemma_reals_complete: "((\<exists>x. x \<in> S) & (\<exists>y. isUb UNIV S (y::real))) -->
paulson@14477
  1541
      (\<exists>t. isLub UNIV S t)"
paulson@15234
  1542
by (blast intro: reals_complete)
paulson@14477
  1543
paulson@14477
  1544
lemma isCont_has_Ub: "[| a \<le> b; \<forall>x. a \<le> x & x \<le> b --> isCont f x |]
huffman@20561
  1545
         ==> \<exists>M::real. (\<forall>x::real. a \<le> x & x \<le> b --> f(x) \<le> M) &
paulson@14477
  1546
                   (\<forall>N. N < M --> (\<exists>x. a \<le> x & x \<le> b & N < f(x)))"
kleing@19023
  1547
apply (cut_tac S = "Collect (%y. \<exists>x. a \<le> x & x \<le> b & y = f x)"
paulson@15234
  1548
        in lemma_reals_complete)
paulson@14477
  1549
apply auto
paulson@14477
  1550
apply (drule isCont_bounded, assumption)
paulson@14477
  1551
apply (auto simp add: isUb_def leastP_def isLub_def setge_def setle_def)
paulson@14477
  1552
apply (rule exI, auto)
paulson@15228
  1553
apply (auto dest!: spec simp add: linorder_not_less)
paulson@14477
  1554
done
paulson@14477
  1555
paulson@15234
  1556
text{*Now show that it attains its upper bound*}
paulson@14477
  1557
paulson@14477
  1558
lemma isCont_eq_Ub:
paulson@14477
  1559
  assumes le: "a \<le> b"
huffman@20561
  1560
      and con: "\<forall>x::real. a \<le> x & x \<le> b --> isCont f x"
huffman@20552
  1561
  shows "\<exists>M::real. (\<forall>x. a \<le> x & x \<le> b --> f(x) \<le> M) &
paulson@14477
  1562
             (\<exists>x. a \<le> x & x \<le> b & f(x) = M)"
paulson@14477
  1563
proof -
paulson@14477
  1564
  from isCont_has_Ub [OF le con]
paulson@14477
  1565
  obtain M where M1: "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> f x \<le> M"
paulson@14477
  1566
             and M2: "!!N. N<M ==> \<exists>x. a \<le> x \<and> x \<le> b \<and> N < f x"  by blast
paulson@14477
  1567
  show ?thesis
paulson@14477
  1568
  proof (intro exI, intro conjI)
paulson@14477
  1569
    show " \<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> f x \<le> M" by (rule M1)
paulson@15228
  1570
    show "\<exists>x. a \<le> x \<and> x \<le> b \<and> f x = M"
paulson@14477
  1571
    proof (rule ccontr)
paulson@14477
  1572
      assume "\<not> (\<exists>x. a \<le> x \<and> x \<le> b \<and> f x = M)"
paulson@14477
  1573
      with M1 have M3: "\<forall>x. a \<le> x & x \<le> b --> f x < M"
nipkow@15195
  1574
        by (fastsimp simp add: linorder_not_le [symmetric])
paulson@14477
  1575
      hence "\<forall>x. a \<le> x & x \<le> b --> isCont (%x. inverse (M - f x)) x"
paulson@14477
  1576
        by (auto simp add: isCont_inverse isCont_diff con)
paulson@14477
  1577
      from isCont_bounded [OF le this]
paulson@14477
  1578
      obtain k where k: "!!x. a \<le> x & x \<le> b --> inverse (M - f x) \<le> k" by auto
paulson@14477
  1579
      have Minv: "!!x. a \<le> x & x \<le> b --> 0 < inverse (M - f (x))"
paulson@15228
  1580
        by (simp add: M3 compare_rls)
paulson@15228
  1581
      have "!!x. a \<le> x & x \<le> b --> inverse (M - f x) < k+1" using k
paulson@15228
  1582
        by (auto intro: order_le_less_trans [of _ k])
paulson@15228
  1583
      with Minv
paulson@15228
  1584
      have "!!x. a \<le> x & x \<le> b --> inverse(k+1) < inverse(inverse(M - f x))"
paulson@14477
  1585
        by (intro strip less_imp_inverse_less, simp_all)
paulson@15228
  1586
      hence invlt: "!!x. a \<le> x & x \<le> b --> inverse(k+1) < M - f x"
paulson@14477
  1587
        by simp
paulson@15228
  1588
      have "M - inverse (k+1) < M" using k [of a] Minv [of a] le
paulson@14477
  1589
        by (simp, arith)
paulson@14477
  1590
      from M2 [OF this]
paulson@14477
  1591
      obtain x where ax: "a \<le> x & x \<le> b & M - inverse(k+1) < f x" ..
paulson@14477
  1592
      thus False using invlt [of x] by force
paulson@14477
  1593
    qed
paulson@14477
  1594
  qed
paulson@14477
  1595
qed
paulson@14477
  1596
paulson@14477
  1597
paulson@15234
  1598
text{*Same theorem for lower bound*}
paulson@14477
  1599
paulson@14477
  1600
lemma isCont_eq_Lb: "[| a \<le> b; \<forall>x. a \<le> x & x \<le> b --> isCont f x |]
huffman@20561
  1601
         ==> \<exists>M::real. (\<forall>x::real. a \<le> x & x \<le> b --> M \<le> f(x)) &
paulson@14477
  1602
                   (\<exists>x. a \<le> x & x \<le> b & f(x) = M)"
paulson@14477
  1603
apply (subgoal_tac "\<forall>x. a \<le> x & x \<le> b --> isCont (%x. - (f x)) x")
paulson@14477
  1604
prefer 2 apply (blast intro: isCont_minus)
paulson@15234
  1605
apply (drule_tac f = "(%x. - (f x))" in isCont_eq_Ub)
paulson@14477
  1606
apply safe
paulson@14477
  1607
apply auto
paulson@14477
  1608
done
paulson@14477
  1609
paulson@14477
  1610
paulson@15234
  1611
text{*Another version.*}
paulson@14477
  1612
paulson@14477
  1613
lemma isCont_Lb_Ub: "[|a \<le> b; \<forall>x. a \<le> x & x \<le> b --> isCont f x |]
huffman@20561
  1614
      ==> \<exists>L M::real. (\<forall>x::real. a \<le> x & x \<le> b --> L \<le> f(x) & f(x) \<le> M) &
paulson@14477
  1615
          (\<forall>y. L \<le> y & y \<le> M --> (\<exists>x. a \<le> x & x \<le> b & (f(x) = y)))"
paulson@14477
  1616
apply (frule isCont_eq_Lb)
paulson@14477
  1617
apply (frule_tac [2] isCont_eq_Ub)
paulson@14477
  1618
apply (assumption+, safe)
paulson@14477
  1619
apply (rule_tac x = "f x" in exI)
paulson@14477
  1620
apply (rule_tac x = "f xa" in exI, simp, safe)
paulson@14477
  1621
apply (cut_tac x = x and y = xa in linorder_linear, safe)
paulson@14477
  1622
apply (cut_tac f = f and a = x and b = xa and y = y in IVT_objl)
paulson@14477
  1623
apply (cut_tac [2] f = f and a = xa and b = x and y = y in IVT2_objl, safe)
paulson@14477
  1624
apply (rule_tac [2] x = xb in exI)
paulson@14477
  1625
apply (rule_tac [4] x = xb in exI, simp_all)
paulson@14477
  1626
done
paulson@14477
  1627
paulson@15003
  1628
huffman@20755
  1629
text{*If @{term "0 < f'(x)"} then @{term x} is Locally Strictly Increasing At The Right*}
paulson@14477
  1630
paulson@14477
  1631
lemma DERIV_left_inc:
paulson@15003
  1632
  assumes der: "DERIV f x :> l"
paulson@15003
  1633
      and l:   "0 < l"
nipkow@15360
  1634
  shows "\<exists>d > 0. \<forall>h > 0. h < d --> f(x) < f(x + h)"
paulson@15003
  1635
proof -
paulson@15003
  1636
  from l der [THEN DERIV_D, THEN LIM_D [where r = "l"]]
nipkow@15360
  1637
  have "\<exists>s > 0. (\<forall>z. z \<noteq> 0 \<and> \<bar>z\<bar> < s \<longrightarrow> \<bar>(f(x+z) - f x) / z - l\<bar> < l)"
paulson@15003
  1638
    by (simp add: diff_minus)
paulson@15003
  1639
  then obtain s
paulson@15228
  1640
        where s:   "0 < s"
paulson@15003
  1641
          and all: "!!z. z \<noteq> 0 \<and> \<bar>z\<bar> < s \<longrightarrow> \<bar>(f(x+z) - f x) / z - l\<bar> < l"
paulson@15003
  1642
    by auto
paulson@15003
  1643
  thus ?thesis
paulson@15003
  1644
  proof (intro exI conjI strip)
paulson@15003
  1645
    show "0<s" .
paulson@15003
  1646
    fix h::real
nipkow@15360
  1647
    assume "0 < h" "h < s"
paulson@15228
  1648
    with all [of h] show "f x < f (x+h)"
paulson@15228
  1649
    proof (simp add: abs_if pos_less_divide_eq diff_minus [symmetric]
kleing@19023
  1650
    split add: split_if_asm)
paulson@15228
  1651
      assume "~ (f (x+h) - f x) / h < l" and h: "0 < h"
paulson@15228
  1652
      with l
paulson@15003
  1653
      have "0 < (f (x+h) - f x) / h" by arith
paulson@15003
  1654
      thus "f x < f (x+h)"
kleing@19023
  1655
  by (simp add: pos_less_divide_eq h)
paulson@15003
  1656
    qed
paulson@15003
  1657
  qed
paulson@15003
  1658
qed
paulson@14477
  1659
paulson@14477
  1660
lemma DERIV_left_dec:
paulson@14477
  1661
  assumes der: "DERIV f x :> l"
paulson@14477
  1662
      and l:   "l < 0"
nipkow@15360
  1663
  shows "\<exists>d > 0. \<forall>h > 0. h < d --> f(x) < f(x-h)"
paulson@14477
  1664
proof -
paulson@14477
  1665
  from l der [THEN DERIV_D, THEN LIM_D [where r = "-l"]]
nipkow@15360
  1666
  have "\<exists>s > 0. (\<forall>z. z \<noteq> 0 \<and> \<bar>z\<bar> < s \<longrightarrow> \<bar>(f(x+z) - f x) / z - l\<bar> < -l)"
paulson@14477
  1667
    by (simp add: diff_minus)
paulson@14477
  1668
  then obtain s
paulson@15228
  1669
        where s:   "0 < s"
paulson@14477
  1670
          and all: "!!z. z \<noteq> 0 \<and> \<bar>z\<bar> < s \<longrightarrow> \<bar>(f(x+z) - f x) / z - l\<bar> < -l"
paulson@14477
  1671
    by auto
paulson@14477
  1672
  thus ?thesis
paulson@14477
  1673
  proof (intro exI conjI strip)
paulson@14477
  1674
    show "0<s" .
paulson@14477
  1675
    fix h::real
nipkow@15360
  1676
    assume "0 < h" "h < s"
paulson@15228
  1677
    with all [of "-h"] show "f x < f (x-h)"
paulson@15228
  1678
    proof (simp add: abs_if pos_less_divide_eq diff_minus [symmetric]
kleing@19023
  1679
    split add: split_if_asm)
paulson@15228
  1680
      assume " - ((f (x-h) - f x) / h) < l" and h: "0 < h"
paulson@15228
  1681
      with l
paulson@14477
  1682
      have "0 < (f (x-h) - f x) / h" by arith
paulson@14477
  1683
      thus "f x < f (x-h)"
kleing@19023
  1684
  by (simp add: pos_less_divide_eq h)
paulson@14477
  1685
    qed
paulson@14477
  1686
  qed
paulson@14477
  1687
qed
paulson@14477
  1688
paulson@15228
  1689
lemma DERIV_local_max:
paulson@14477
  1690
  assumes der: "DERIV f x :> l"
paulson@14477
  1691
      and d:   "0 < d"
paulson@14477
  1692
      and le:  "\<forall>y. \<bar>x-y\<bar> < d --> f(y) \<le> f(x)"
paulson@14477
  1693
  shows "l = 0"
paulson@14477
  1694
proof (cases rule: linorder_cases [of l 0])
paulson@14477
  1695
  case equal show ?thesis .
paulson@14477
  1696
next
paulson@14477
  1697
  case less
paulson@14477
  1698
  from DERIV_left_dec [OF der less]
paulson@14477
  1699
  obtain d' where d': "0 < d'"
nipkow@15360
  1700
             and lt: "\<forall>h > 0. h < d' \<longrightarrow> f x < f (x-h)" by blast
paulson@14477
  1701
  from real_lbound_gt_zero [OF d d']
paulson@14477
  1702
  obtain e where "0 < e \<and> e < d \<and> e < d'" ..
paulson@15228
  1703
  with lt le [THEN spec [where x="x-e"]]
paulson@14477
  1704
  show ?thesis by (auto simp add: abs_if)
paulson@14477
  1705
next
paulson@14477
  1706
  case greater
paulson@14477
  1707
  from DERIV_left_inc [OF der greater]
paulson@14477
  1708
  obtain d' where d': "0 < d'"
nipkow@15360
  1709
             and lt: "\<forall>h > 0. h < d' \<longrightarrow> f x < f (x + h)" by blast
paulson@14477
  1710
  from real_lbound_gt_zero [OF d d']
paulson@14477
  1711
  obtain e where "0 < e \<and> e < d \<and> e < d'" ..
paulson@14477
  1712
  with lt le [THEN spec [where x="x+e"]]
paulson@14477
  1713
  show ?thesis by (auto simp add: abs_if)
paulson@14477
  1714
qed
paulson@14477
  1715
paulson@14477
  1716
paulson@14477
  1717
text{*Similar theorem for a local minimum*}
paulson@14477
  1718
lemma DERIV_local_min:
paulson@14477
  1719
     "[| DERIV f x :> l; 0 < d; \<forall>y. \<bar>x-y\<bar> < d --> f(x) \<le> f(y) |] ==> l = 0"
paulson@14477
  1720
by (drule DERIV_minus [THEN DERIV_local_max], auto)
paulson@14477
  1721
paulson@14477
  1722
paulson@14477
  1723
text{*In particular, if a function is locally flat*}
paulson@14477
  1724
lemma DERIV_local_const:
paulson@14477
  1725
     "[| DERIV f x :> l; 0 < d; \<forall>y. \<bar>x-y\<bar> < d --> f(x) = f(y) |] ==> l = 0"
paulson@14477
  1726
by (auto dest!: DERIV_local_max)
paulson@14477
  1727
paulson@14477
  1728
text{*Lemma about introducing open ball in open interval*}
paulson@14477
  1729
lemma lemma_interval_lt:
paulson@15228
  1730
     "[| a < x;  x < b |]
paulson@14477
  1731
      ==> \<exists>d::real. 0 < d & (\<forall>y. \<bar>x-y\<bar> < d --> a < y & y < b)"
paulson@14477
  1732
apply (simp add: abs_interval_iff)
paulson@14477
  1733
apply (insert linorder_linear [of "x-a" "b-x"], safe)
paulson@14477
  1734
apply (rule_tac x = "x-a" in exI)
paulson@14477
  1735
apply (rule_tac [2] x = "b-x" in exI, auto)
paulson@14477
  1736
done
paulson@14477
  1737
paulson@14477
  1738
lemma lemma_interval: "[| a < x;  x < b |] ==>
paulson@14477
  1739
        \<exists>d::real. 0 < d &  (\<forall>y. \<bar>x-y\<bar> < d --> a \<le> y & y \<le> b)"
paulson@14477
  1740
apply (drule lemma_interval_lt, auto)
paulson@14477
  1741
apply (auto intro!: exI)
paulson@14477
  1742
done
paulson@14477
  1743
paulson@14477
  1744
text{*Rolle's Theorem.
paulson@15228
  1745
   If @{term f} is defined and continuous on the closed interval
paulson@15228
  1746
   @{text "[a,b]"} and differentiable on the open interval @{text "(a,b)"},
paulson@14477
  1747
   and @{term "f(a) = f(b)"},
paulson@14477
  1748
   then there exists @{text "x0 \<in> (a,b)"} such that @{term "f'(x0) = 0"}*}
paulson@15228
  1749
theorem Rolle:
paulson@14477
  1750
  assumes lt: "a < b"
paulson@14477
  1751
      and eq: "f(a) = f(b)"
paulson@14477
  1752
      and con: "\<forall>x. a \<le> x & x \<le> b --> isCont f x"
paulson@14477
  1753
      and dif [rule_format]: "\<forall>x. a < x & x < b --> f differentiable x"
paulson@14477
  1754
  shows "\<exists>z. a < z & z < b & DERIV f z :> 0"
paulson@14477
  1755
proof -
paulson@14477
  1756
  have le: "a \<le> b" using lt by simp
paulson@14477
  1757
  from isCont_eq_Ub [OF le con]
paulson@15228
  1758
  obtain x where x_max: "\<forall>z. a \<le> z \<and> z \<le> b \<longrightarrow> f z \<le> f x"
paulson@15228
  1759
             and alex: "a \<le> x" and xleb: "x \<le> b"
paulson@14477
  1760
    by blast
paulson@14477
  1761
  from isCont_eq_Lb [OF le con]
paulson@15228
  1762
  obtain x' where x'_min: "\<forall>z. a \<le> z \<and> z \<le> b \<longrightarrow> f x' \<le> f z"
paulson@15228
  1763
              and alex': "a \<le> x'" and x'leb: "x' \<le> b"
paulson@14477
  1764
    by blast
paulson@14477
  1765
  show ?thesis
paulson@14477
  1766
  proof cases
paulson@14477
  1767
    assume axb: "a < x & x < b"
paulson@14477
  1768
        --{*@{term f} attains its maximum within the interval*}
paulson@14477
  1769
    hence ax: "a<x" and xb: "x<b" by auto
paulson@14477
  1770
    from lemma_interval [OF ax xb]
paulson@14477
  1771
    obtain d where d: "0<d" and bound: "\<forall>y. \<bar>x-y\<bar> < d \<longrightarrow> a \<le> y \<and> y \<le> b"
paulson@14477
  1772
      by blast
paulson@14477
  1773
    hence bound': "\<forall>y. \<bar>x-y\<bar> < d \<longrightarrow> f y \<le> f x" using x_max
paulson@14477
  1774
      by blast
paulson@14477
  1775
    from differentiableD [OF dif [OF axb]]
paulson@14477
  1776
    obtain l where der: "DERIV f x :> l" ..
paulson@15228
  1777
    have "l=0" by (rule DERIV_local_max [OF der d bound'])
paulson@14477
  1778
        --{*the derivative at a local maximum is zero*}
paulson@14477
  1779
    thus ?thesis using ax xb der by auto
paulson@14477
  1780
  next
paulson@14477
  1781
    assume notaxb: "~ (a < x & x < b)"
paulson@14477
  1782
    hence xeqab: "x=a | x=b" using alex xleb by arith
paulson@15228
  1783
    hence fb_eq_fx: "f b = f x" by (auto simp add: eq)
paulson@14477
  1784
    show ?thesis
paulson@14477
  1785
    proof cases
paulson@14477
  1786
      assume ax'b: "a < x' & x' < b"
paulson@14477
  1787
        --{*@{term f} attains its minimum within the interval*}
paulson@14477
  1788
      hence ax': "a<x'" and x'b: "x'<b" by auto
paulson@14477
  1789
      from lemma_interval [OF ax' x'b]
paulson@14477
  1790
      obtain d where d: "0<d" and bound: "\<forall>y. \<bar>x'-y\<bar> < d \<longrightarrow> a \<le> y \<and> y \<le> b"
kleing@19023
  1791
  by blast
paulson@14477
  1792
      hence bound': "\<forall>y. \<bar>x'-y\<bar> < d \<longrightarrow> f x' \<le> f y" using x'_min
kleing@19023
  1793
  by blast
paulson@14477
  1794
      from differentiableD [OF dif [OF ax'b]]
paulson@14477
  1795
      obtain l where der: "DERIV f x' :> l" ..
paulson@15228
  1796
      have "l=0" by (rule DERIV_local_min [OF der d bound'])
paulson@14477
  1797
        --{*the derivative at a local minimum is zero*}
paulson@14477
  1798
      thus ?thesis using ax' x'b der by auto
paulson@14477
  1799
    next
paulson@14477
  1800
      assume notax'b: "~ (a < x' & x' < b)"
paulson@14477
  1801
        --{*@{term f} is constant througout the interval*}
paulson@14477
  1802
      hence x'eqab: "x'=a | x'=b" using alex' x'leb by arith
paulson@15228
  1803
      hence fb_eq_fx': "f b = f x'" by (auto simp add: eq)
paulson@14477
  1804
      from dense [OF lt]
paulson@14477
  1805
      obtain r where ar: "a < r" and rb: "r < b" by blast
paulson@14477
  1806
      from lemma_interval [OF ar rb]
paulson@14477
  1807
      obtain d where d: "0<d" and bound: "\<forall>y. \<bar>r-y\<bar> < d \<longrightarrow> a \<le> y \<and> y \<le> b"
kleing@19023
  1808
  by blast
paulson@15228
  1809
      have eq_fb: "\<forall>z. a \<le> z --> z \<le> b --> f z = f b"
paulson@15228
  1810
      proof (clarify)
paulson@14477
  1811
        fix z::real
paulson@14477
  1812
        assume az: "a \<le> z" and zb: "z \<le> b"
paulson@14477
  1813
        show "f z = f b"
paulson@14477
  1814
        proof (rule order_antisym)
nipkow@15195
  1815
          show "f z \<le> f b" by (simp add: fb_eq_fx x_max az zb)
nipkow@15195
  1816
          show "f b \<le> f z" by (simp add: fb_eq_fx' x'_min az zb)
paulson@14477
  1817
        qed
paulson@14477
  1818
      qed
paulson@14477
  1819
      have bound': "\<forall>y. \<bar>r-y\<bar> < d \<longrightarrow> f r = f y"
paulson@14477
  1820
      proof (intro strip)
paulson@14477
  1821
        fix y::real
paulson@14477
  1822
        assume lt: "\<bar>r-y\<bar> < d"
paulson@15228
  1823
        hence "f y = f b" by (simp add: eq_fb bound)
paulson@14477
  1824
        thus "f r = f y" by (simp add: eq_fb ar rb order_less_imp_le)
paulson@14477
  1825
      qed
paulson@14477
  1826
      from differentiableD [OF dif [OF conjI [OF ar rb]]]
paulson@14477
  1827
      obtain l where der: "DERIV f r :> l" ..
paulson@15228
  1828
      have "l=0" by (rule DERIV_local_const [OF der d bound'])
paulson@14477
  1829
        --{*the derivative of a constant function is zero*}
paulson@14477
  1830
      thus ?thesis using ar rb der by auto
paulson@14477
  1831
    qed
paulson@14477
  1832
  qed
paulson@14477
  1833
qed
paulson@14477
  1834
paulson@14477
  1835
paulson@14477
  1836
subsection{*Mean Value Theorem*}
paulson@14477
  1837
paulson@14477
  1838
lemma lemma_MVT:
paulson@14477
  1839
     "f a - (f b - f a)/(b-a) * a = f b - (f b - f a)/(b-a) * (b::real)"
paulson@14477
  1840
proof cases
paulson@14477
  1841
  assume "a=b" thus ?thesis by simp
paulson@14477
  1842
next
paulson@15228
  1843
  assume "a\<noteq>b"
paulson@14477
  1844
  hence ba: "b-a \<noteq> 0" by arith
paulson@14477
  1845
  show ?thesis
paulson@14477
  1846
    by (rule real_mult_left_cancel [OF ba, THEN iffD1],
kleing@19023
  1847
        simp add: right_diff_distrib,
paulson@15234
  1848
        simp add: left_diff_distrib)
paulson@14477
  1849
qed
paulson@14477
  1850
paulson@15228
  1851
theorem MVT:
paulson@14477
  1852
  assumes lt:  "a < b"
paulson@14477
  1853
      and con: "\<forall>x. a \<le> x & x \<le> b --> isCont f x"
paulson@14477
  1854
      and dif [rule_format]: "\<forall>x. a < x & x < b --> f differentiable x"
paulson@14477
  1855
  shows "\<exists>l z. a < z & z < b & DERIV f z :> l &
paulson@14477
  1856
                   (f(b) - f(a) = (b-a) * l)"
paulson@14477
  1857
proof -
paulson@14477
  1858
  let ?F = "%x. f x - ((f b - f a) / (b-a)) * x"
paulson@14477
  1859
  have contF: "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont ?F x" using con
paulson@15228
  1860
    by (fast intro: isCont_diff isCont_const isCont_mult isCont_Id)
paulson@14477
  1861
  have difF: "\<forall>x. a < x \<and> x < b \<longrightarrow> ?F differentiable x"
paulson@14477
  1862
  proof (clarify)
paulson@14477
  1863
    fix x::real
paulson@14477
  1864
    assume ax: "a < x" and xb: "x < b"
paulson@14477
  1865
    from differentiableD [OF dif [OF conjI [OF ax xb]]]
paulson@14477
  1866
    obtain l where der: "DERIV f x :> l" ..
paulson@14477
  1867
    show "?F differentiable x"
paulson@14477
  1868
      by (rule differentiableI [where D = "l - (f b - f a)/(b-a)"],
paulson@15228
  1869
          blast intro: DERIV_diff DERIV_cmult_Id der)
paulson@15228
  1870
  qed
paulson@14477
  1871
  from Rolle [where f = ?F, OF lt lemma_MVT contF difF]
paulson@15228
  1872
  obtain z where az: "a < z" and zb: "z < b" and der: "DERIV ?F z :> 0"
paulson@14477
  1873
    by blast
paulson@14477
  1874
  have "DERIV (%x. ((f b - f a)/(b-a)) * x) z :> (f b - f a)/(b-a)"
paulson@14477
  1875
    by (rule DERIV_cmult_Id)
paulson@15228
  1876
  hence derF: "DERIV (\<lambda>x. ?F x + (f b - f a) / (b - a) * x) z
paulson@14477
  1877
                   :> 0 + (f b - f a) / (b - a)"
paulson@14477
  1878
    by (rule DERIV_add [OF der])
paulson@15228
  1879
  show ?thesis
paulson@14477
  1880
  proof (intro exI conjI)
paulson@14477
  1881
    show "a < z" .
paulson@14477
  1882
    show "z < b" .
nipkow@15539
  1883
    show "f b - f a = (b - a) * ((f b - f a)/(b-a))" by (simp)
paulson@14477
  1884
    show "DERIV f z :> ((f b - f a)/(b-a))"  using derF by simp
paulson@14477
  1885
  qed
paulson@14477
  1886
qed
paulson@14477
  1887
paulson@14477
  1888
paulson@14477
  1889
text{*A function is constant if its derivative is 0 over an interval.*}
paulson@14477
  1890
paulson@14477
  1891
lemma DERIV_isconst_end: "[| a < b;
paulson@14477
  1892
         \<forall>x. a \<le> x & x \<le> b --> isCont f x;
paulson@14477
  1893
         \<forall>x. a < x & x < b --> DERIV f x :> 0 |]
nipkow@15360
  1894
        ==> f b = f a"
paulson@14477
  1895
apply (drule MVT, assumption)
paulson@14477
  1896
apply (blast intro: differentiableI)
paulson@14477
  1897
apply (auto dest!: DERIV_unique simp add: diff_eq_eq)
paulson@14477
  1898
done
paulson@14477
  1899
paulson@14477
  1900
lemma DERIV_isconst1: "[| a < b;
paulson@14477
  1901
         \<forall>x. a \<le> x & x \<le> b --> isCont f x;
paulson@14477
  1902
         \<forall>x. a < x & x < b --> DERIV f x :> 0 |]
paulson@14477
  1903
        ==> \<forall>x. a \<le> x & x \<le> b --> f x = f a"
paulson@14477
  1904
apply safe
paulson@14477
  1905
apply (drule_tac x = a in order_le_imp_less_or_eq, safe)
paulson@14477
  1906
apply (drule_tac b = x in DERIV_isconst_end, auto)
paulson@14477
  1907
done
paulson@14477
  1908
paulson@14477
  1909
lemma DERIV_isconst2: "[| a < b;
paulson@14477
  1910
         \<forall>x. a \<le> x & x \<le> b --> isCont f x;
paulson@14477
  1911
         \<forall>x. a < x & x < b --> DERIV f x :> 0;
paulson@14477
  1912
         a \<le> x; x \<le> b |]
paulson@14477
  1913
        ==> f x = f a"
paulson@14477
  1914
apply (blast dest: DERIV_isconst1)
paulson@14477
  1915
done
paulson@14477
  1916
paulson@14477
  1917
lemma DERIV_isconst_all: "\<forall>x. DERIV f x :> 0 ==> f(x) = f(y)"
paulson@14477
  1918
apply (rule linorder_cases [of x y])
paulson@14477
  1919
apply (blast intro: sym DERIV_isCont DERIV_isconst_end)+
paulson@14477
  1920
done
paulson@14477
  1921
paulson@14477
  1922
lemma DERIV_const_ratio_const:
paulson@14477
  1923
     "[|a \<noteq> b; \<forall>x. DERIV f x :> k |] ==> (f(b) - f(a)) = (b-a) * k"
paulson@14477
  1924
apply (rule linorder_cases [of a b], auto)
paulson@14477
  1925
apply (drule_tac [!] f = f in MVT)
paulson@14477
  1926
apply (auto dest: DERIV_isCont DERIV_unique simp add: differentiable_def)
paulson@14477
  1927
apply (auto dest: DERIV_unique simp add: left_distrib diff_minus)
paulson@14477
  1928
done
paulson@14477
  1929
paulson@14477
  1930
lemma DERIV_const_ratio_const2:
paulson@14477
  1931
     "[|a \<noteq> b; \<forall>x. DERIV f x :> k |] ==> (f(b) - f(a))/(b-a) = k"
paulson@14477
  1932
apply (rule_tac c1 = "b-a" in real_mult_right_cancel [THEN iffD1])
nipkow@15539
  1933
apply (auto dest!: DERIV_const_ratio_const simp add: mult_assoc)
paulson@14477
  1934
done
paulson@14477
  1935
paulson@15228
  1936
lemma real_average_minus_first [simp]: "((a + b) /2 - a) = (b-a)/(2::real)"
kleing@19023
  1937
by (simp)
paulson@14477
  1938
paulson@15228
  1939
lemma real_average_minus_second [simp]: "((b + a)/2 - a) = (b-a)/(2::real)"
kleing@19023
  1940
by (simp)
paulson@14477
  1941
paulson@14477
  1942
text{*Gallileo's "trick": average velocity = av. of end velocities*}
paulson@14477
  1943
paulson@14477
  1944
lemma DERIV_const_average:
paulson@14477
  1945
  assumes neq: "a \<noteq> (b::real)"
paulson@14477
  1946
      and der: "\<forall>x. DERIV v x :> k"
paulson@14477
  1947
  shows "v ((a + b)/2) = (v a + v b)/2"
paulson@14477
  1948
proof (cases rule: linorder_cases [of a b])
paulson@14477
  1949
  case equal with neq show ?thesis by simp
paulson@14477
  1950
next
paulson@14477
  1951
  case less
paulson@14477
  1952
  have "(v b - v a) / (b - a) = k"
paulson@14477
  1953
    by (rule DERIV_const_ratio_const2 [OF neq der])
paulson@15228
  1954
  hence "(b-a) * ((v b - v a) / (b-a)) = (b-a) * k" by simp
paulson@14477
  1955
  moreover have "(v ((a + b) / 2) - v a) / ((a + b) / 2 - a) = k"
paulson@14477
  1956
    by (rule DERIV_const_ratio_const2 [OF _ der], simp add: neq)
paulson@14477
  1957
  ultimately show ?thesis using neq by force
paulson@14477
  1958
next
paulson@14477
  1959
  case greater
paulson@14477
  1960
  have "(v b - v a) / (b - a) = k"
paulson@14477
  1961
    by (rule DERIV_const_ratio_const2 [OF neq der])
paulson@15228
  1962
  hence "(b-a) * ((v b - v a) / (b-a)) = (b-a) * k" by simp
paulson@14477
  1963
  moreover have " (v ((b + a) / 2) - v a) / ((b + a) / 2 - a) = k"
paulson@14477
  1964
    by (rule DERIV_const_ratio_const2 [OF _ der], simp add: neq)
paulson@15228
  1965
  ultimately show ?thesis using neq by (force simp add: add_commute)
paulson@14477
  1966
qed
paulson@14477
  1967
paulson@14477
  1968
paulson@14477
  1969
text{*Dull lemma: an continuous injection on an interval must have a
paulson@14477
  1970
strict maximum at an end point, not in the middle.*}
paulson@14477
  1971
paulson@14477
  1972
lemma lemma_isCont_inj:
huffman@20552
  1973
  fixes f :: "real \<Rightarrow> real"
paulson@14477
  1974
  assumes d: "0 < d"
paulson@14477
  1975
      and inj [rule_format]: "\<forall>z. \<bar>z-x\<bar> \<le> d --> g(f z) = z"
paulson@14477
  1976
      and cont: "\<forall>z. \<bar>z-x\<bar> \<le> d --> isCont f z"
paulson@14477
  1977
  shows "\<exists>z. \<bar>z-x\<bar> \<le> d & f x < f z"
paulson@14477
  1978
proof (rule ccontr)
paulson@14477
  1979
  assume  "~ (\<exists>z. \<bar>z-x\<bar> \<le> d & f x < f z)"
paulson@15228
  1980
  hence all [rule_format]: "\<forall>z. \<bar>z - x\<bar> \<le> d --> f z \<le> f x" by auto
paulson@14477
  1981
  show False
paulson@14477
  1982
  proof (cases rule: linorder_le_cases [of "f(x-d)" "f(x+d)"])
paulson@14477
  1983
    case le
paulson@14477
  1984
    from d cont all [of "x+d"]
paulson@15228
  1985
    have flef: "f(x+d) \<le> f x"
paulson@15228
  1986
     and xlex: "x - d \<le> x"
paulson@15228
  1987
     and cont': "\<forall>z. x - d \<le> z \<and> z \<le> x \<longrightarrow> isCont f z"
paulson@14477
  1988
       by (auto simp add: abs_if)
paulson@14477
  1989
    from IVT [OF le flef xlex cont']
paulson@14477
  1990
    obtain x' where "x-d \<le> x'" "x' \<le> x" "f x' = f(x+d)" by blast
paulson@14477
  1991
    moreover
paulson@14477
  1992
    hence "g(f x') = g (f(x+d))" by simp
paulson@14477
  1993
    ultimately show False using d inj [of x'] inj [of "x+d"]
paulson@14477
  1994
      by (simp add: abs_le_interval_iff)
paulson@14477
  1995
  next
paulson@14477
  1996
    case ge
paulson@14477
  1997
    from d cont all [of "x-d"]
paulson@15228
  1998
    have flef: "f(x-d) \<le> f x"
paulson@15228
  1999
     and xlex: "x \<le> x+d"
paulson@15228
  2000
     and cont': "\<forall>z. x \<le> z \<and> z \<le> x+d \<longrightarrow> isCont f z"
paulson@14477
  2001
       by (auto simp add: abs_if)
paulson@14477
  2002
    from IVT2 [OF ge flef xlex cont']
paulson@14477
  2003
    obtain x' where "x \<le> x'" "x' \<le> x+d" "f x' = f(x-d)" by blast
paulson@14477
  2004
    moreover
paulson@14477
  2005
    hence "g(f x') = g (f(x-d))" by simp
paulson@14477
  2006
    ultimately show False using d inj [of x'] inj [of "x-d"]
paulson@14477
  2007
      by (simp add: abs_le_interval_iff)
paulson@14477
  2008
  qed
paulson@14477
  2009
qed
paulson@14477
  2010
paulson@14477
  2011
paulson@14477
  2012
text{*Similar version for lower bound.*}
paulson@14477
  2013
paulson@14477
  2014
lemma lemma_isCont_inj2:
huffman@20552
  2015
  fixes f g :: "real \<Rightarrow> real"
huffman@20552
  2016
  shows "[|0 < d; \<forall>z. \<bar>z-x\<bar> \<le> d --> g(f z) = z;
paulson@14477
  2017
        \<forall>z. \<bar>z-x\<bar> \<le> d --> isCont f z |]
paulson@14477
  2018
      ==> \<exists>z. \<bar>z-x\<bar> \<le> d & f z < f x"
paulson@14477
  2019
apply (insert lemma_isCont_inj
paulson@14477
  2020
          [where f = "%x. - f x" and g = "%y. g(-y)" and x = x and d = d])
paulson@15228
  2021
apply (simp add: isCont_minus linorder_not_le)
paulson@14477
  2022
done
paulson@14477
  2023
paulson@15228
  2024
text{*Show there's an interval surrounding @{term "f(x)"} in
paulson@14477
  2025
@{text "f[[x - d, x + d]]"} .*}
paulson@14477
  2026
paulson@15228
  2027
lemma isCont_inj_range:
huffman@20552
  2028
  fixes f :: "real \<Rightarrow> real"
paulson@14477
  2029
  assumes d: "0 < d"
paulson@14477
  2030
      and inj: "\<forall>z. \<bar>z-x\<bar> \<le> d --> g(f z) = z"
paulson@14477
  2031
      and cont: "\<forall>z. \<bar>z-x\<bar> \<le> d --> isCont f z"
nipkow@15360
  2032
  shows "\<exists>e>0. \<forall>y. \<bar>y - f x\<bar> \<le> e --> (\<exists>z. \<bar>z-x\<bar> \<le> d & f z = y)"
paulson@14477
  2033
proof -
paulson@14477
  2034
  have "x-d \<le> x+d" "\<forall>z. x-d \<le> z \<and> z \<le> x+d \<longrightarrow> isCont f z" using cont d
paulson@14477
  2035
    by (auto simp add: abs_le_interval_iff)
paulson@14477
  2036
  from isCont_Lb_Ub [OF this]
paulson@15228
  2037
  obtain L M
paulson@14477
  2038
  where all1 [rule_format]: "\<forall>z. x-d \<le> z \<and> z \<le> x+d \<longrightarrow> L \<le> f z \<and> f z \<le> M"
paulson@14477
  2039
    and all2 [rule_format]:
paulson@14477
  2040
           "\<forall>y. L \<le> y \<and> y \<le> M \<longrightarrow> (\<exists>z. x-d \<le> z \<and> z \<le> x+d \<and> f z = y)"
paulson@14477
  2041
    by auto
paulson@14477
  2042
  with d have "L \<le> f x & f x \<le> M" by simp
paulson@14477
  2043
  moreover have "L \<noteq> f x"
paulson@14477
  2044
  proof -
paulson@14477
  2045
    from lemma_isCont_inj2 [OF d inj cont]
paulson@14477
  2046
    obtain u where "\<bar>u - x\<bar> \<le> d" "f u < f x"  by auto
paulson@14477
  2047
    thus ?thesis using all1 [of u] by arith
paulson@14477
  2048
  qed
paulson@14477
  2049
  moreover have "f x \<noteq> M"
paulson@14477
  2050
  proof -
paulson@14477
  2051
    from lemma_isCont_inj [OF d inj cont]
paulson@14477
  2052
    obtain u where "\<bar>u - x\<bar> \<le> d" "f x < f u"  by auto
paulson@14477
  2053
    thus ?thesis using all1 [of u] by arith
paulson@14477
  2054
  qed
paulson@14477
  2055
  ultimately have "L < f x & f x < M" by arith
paulson@14477
  2056
  hence "0 < f x - L" "0 < M - f x" by arith+
paulson@14477
  2057
  from real_lbound_gt_zero [OF this]
paulson@14477
  2058
  obtain e where e: "0 < e" "e < f x - L" "e < M - f x" by auto
paulson@14477
  2059
  thus ?thesis
paulson@14477
  2060
  proof (intro exI conjI)
paulson@14477
  2061
    show "0<e" .
paulson@14477
  2062
    show "\<forall>y. \<bar>y - f x\<bar> \<le> e \<longrightarrow> (\<exists>z. \<bar>z - x\<bar> \<le> d \<and> f z = y)"
paulson@14477
  2063
    proof (intro strip)
paulson@14477
  2064
      fix y::real
paulson@14477
  2065
      assume "\<bar>y - f x\<bar> \<le> e"
paulson@14477
  2066
      with e have "L \<le> y \<and> y \<le> M" by arith
paulson@14477
  2067
      from all2 [OF this]
paulson@14477
  2068
      obtain z where "x - d \<le> z" "z \<le> x + d" "f z = y" by blast
paulson@15228
  2069
      thus "\<exists>z. \<bar>z - x\<bar> \<le> d \<and> f z = y"
paulson@14477
  2070
        by (force simp add: abs_le_interval_iff)
paulson@14477
  2071
    qed
paulson@14477
  2072
  qed
paulson@14477
  2073
qed
paulson@14477
  2074
paulson@14477
  2075
paulson@14477
  2076
text{*Continuity of inverse function*}
paulson@14477
  2077
paulson@14477
  2078
lemma isCont_inverse_function:
huffman@20561
  2079
  fixes f g :: "real \<Rightarrow> real"
paulson@14477
  2080
  assumes d: "0 < d"
paulson@14477
  2081
      and inj: "\<forall>z. \<bar>z-x\<bar> \<le> d --> g(f z) = z"
paulson@14477
  2082
      and cont: "\<forall>z. \<bar>z-x\<bar> \<le> d --> isCont f z"
paulson@14477
  2083
  shows "isCont g (f x)"
paulson@14477
  2084
proof (simp add: isCont_iff LIM_eq)
paulson@14477
  2085
  show "\<forall>r. 0 < r \<longrightarrow>
nipkow@15360
  2086
         (\<exists>s>0. \<forall>z. z\<noteq>0 \<and> \<bar>z\<bar> < s \<longrightarrow> \<bar>g(f x + z) - g(f x)\<bar> < r)"
paulson@14477
  2087
  proof (intro strip)
paulson@14477
  2088
    fix r::real
paulson@14477
  2089
    assume r: "0<r"
paulson@14477
  2090
    from real_lbound_gt_zero [OF r d]
paulson@14477
  2091
    obtain e where e: "0 < e" and e_lt: "e < r \<and> e < d" by blast
paulson@14477
  2092
    with inj cont
paulson@15228
  2093
    have e_simps: "\<forall>z. \<bar>z-x\<bar> \<le> e --> g (f z) = z"
paulson@14477
  2094
                  "\<forall>z. \<bar>z-x\<bar> \<le> e --> isCont f z"   by auto
paulson@14477
  2095
    from isCont_inj_range [OF e this]
paulson@15228
  2096
    obtain e' where e': "0 < e'"
paulson@14477
  2097
        and all: "\<forall>y. \<bar>y - f x\<bar> \<le> e' \<longrightarrow> (\<exists>z. \<bar>z - x\<bar> \<le> e \<and> f z = y)"
paulson@14477
  2098
          by blast
nipkow@15360
  2099
    show "\<exists>s>0. \<forall>z. z\<noteq>0 \<and> \<bar>z\<bar> < s \<longrightarrow> \<bar>g(f x + z) - g(f x)\<bar> < r"
paulson@14477
  2100
    proof (intro exI conjI)
paulson@14477
  2101
      show "0<e'" .
paulson@14477
  2102
      show "\<forall>z. z \<noteq> 0 \<and> \<bar>z\<bar> < e' \<longrightarrow> \<bar>g (f x + z) - g (f x)\<bar> < r"
paulson@14477
  2103
      proof (intro strip)
paulson@14477
  2104
        fix z::real
paulson@14477
  2105
        assume z: "z \<noteq> 0 \<and> \<bar>z\<bar> < e'"
paulson@14477
  2106
        with e e_lt e_simps all [rule_format, of "f x + z"]
paulson@14477
  2107
        show "\<bar>g (f x + z) - g (f x)\<bar> < r" by force
paulson@14477
  2108
      qed
paulson@14477
  2109
    qed
paulson@14477
  2110
  qed
paulson@15228
  2111
qed
paulson@14477
  2112
kleing@19023
  2113
theorem GMVT:
kleing@19023
  2114
  assumes alb: "a < b"
kleing@19023
  2115
  and fc: "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont f x"
kleing@19023
  2116
  and fd: "\<forall>x. a < x \<and> x < b \<longrightarrow> f differentiable x"
kleing@19023
  2117
  and gc: "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont g x"
kleing@19023
  2118
  and gd: "\<forall>x. a < x \<and> x < b \<longrightarrow> g differentiable x"
kleing@19023
  2119
  shows "\<exists>g'c f'c c. DERIV g c :> g'c \<and> DERIV f c :> f'c \<and> a < c \<and> c < b \<and> ((f b - f a) * g'c) = ((g b - g a) * f'c)"
kleing@19023
  2120
proof -
kleing@19023
  2121
  let ?h = "\<lambda>x. (f b - f a)*(g x) - (g b - g a)*(f x)"
kleing@19023
  2122
  from prems have "a < b" by simp
kleing@19023
  2123
  moreover have "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont ?h x"
kleing@19023
  2124
  proof -
kleing@19023
  2125
    have "\<forall>x. a <= x \<and> x <= b \<longrightarrow> isCont (\<lambda>x. f b - f a) x" by simp
kleing@19023
  2126
    with gc have "\<forall>x. a <= x \<and> x <= b \<longrightarrow> isCont (\<lambda>x. (f b - f a) * g x) x"
kleing@19023
  2127
      by (auto intro: isCont_mult)
kleing@19023
  2128
    moreover
kleing@19023
  2129
    have "\<forall>x. a <= x \<and> x <= b \<longrightarrow> isCont (\<lambda>x. g b - g a) x" by simp
kleing@19023
  2130
    with fc have "\<forall>x. a <= x \<and> x <= b \<longrightarrow> isCont (\<lambda>x. (g b - g a) * f x) x"
kleing@19023
  2131
      by (auto intro: isCont_mult)
kleing@19023
  2132
    ultimately show ?thesis
kleing@19023
  2133
      by (fastsimp intro: isCont_diff)
kleing@19023
  2134
  qed
kleing@19023
  2135
  moreover
kleing@19023
  2136
  have "\<forall>x. a < x \<and> x < b \<longrightarrow> ?h differentiable x"
kleing@19023
  2137
  proof -
kleing@19023
  2138
    have "\<forall>x. a < x \<and> x < b \<longrightarrow> (\<lambda>x. f b - f a) differentiable x" by (simp add: differentiable_const)
kleing@19023
  2139
    with gd have "\<forall>x. a < x \<and> x < b \<longrightarrow> (\<lambda>x. (f b - f a) * g x) differentiable x" by (simp add: differentiable_mult)
kleing@19023
  2140
    moreover
kleing@19023
  2141
    have "\<forall>x. a < x \<and> x < b \<longrightarrow> (\<lambda>x. g b - g a) differentiable x" by (simp add: differentiable_const)
kleing@19023
  2142
    with fd have "\<forall>x. a < x \<and> x < b \<longrightarrow> (\<lambda>x. (g b - g a) * f x) differentiable x" by (simp add: differentiable_mult)
kleing@19023
  2143
    ultimately show ?thesis by (simp add: differentiable_diff)
kleing@19023
  2144
  qed
kleing@19023
  2145
  ultimately have "\<exists>l z. a < z \<and> z < b \<and> DERIV ?h z :> l \<and> ?h b - ?h a = (b - a) * l" by (rule MVT)
kleing@19023
  2146
  then obtain l where ldef: "\<exists>z. a < z \<and> z < b \<and> DERIV ?h z :> l \<and> ?h b - ?h a = (b - a) * l" ..
kleing@19023
  2147
  then obtain c where cdef: "a < c \<and> c < b \<and> DERIV ?h c :> l \<and> ?h b - ?h a = (b - a) * l" ..
kleing@19023
  2148
kleing@19023
  2149
  from cdef have cint: "a < c \<and> c < b" by auto
kleing@19023
  2150
  with gd have "g differentiable c" by simp
kleing@19023
  2151
  hence "\<exists>D. DERIV g c :> D" by (rule differentiableD)
kleing@19023
  2152
  then obtain g'c where g'cdef: "DERIV g c :> g'c" ..
kleing@19023
  2153
kleing@19023
  2154
  from cdef have "a < c \<and> c < b" by auto
kleing@19023
  2155
  with fd have "f differentiable c" by simp
kleing@19023
  2156
  hence "\<exists>D. DERIV f c :> D" by (rule differentiableD)
kleing@19023
  2157
  then obtain f'c where f'cdef: "DERIV f c :> f'c" ..
kleing@19023
  2158
kleing@19023
  2159
  from cdef have "DERIV ?h c :> l" by auto
kleing@19023
  2160
  moreover
kleing@19023
  2161
  {
kleing@19023
  2162
    from g'cdef have "DERIV (\<lambda>x. (f b - f a) * g x) c :> g'c * (f b - f a)"
kleing@19023
  2163
      apply (insert DERIV_const [where k="f b - f a"])
kleing@19023
  2164
      apply (drule meta_spec [of _ c])
kleing@19023
  2165
      apply (drule DERIV_mult [where f="(\<lambda>x. f b - f a)" and g=g])
kleing@19023
  2166
      by simp_all
kleing@19023
  2167
    moreover from f'cdef have "DERIV (\<lambda>x. (g b - g a) * f x) c :> f'c * (g b - g a)"
kleing@19023
  2168
      apply (insert DERIV_const [where k="g b - g a"])
kleing@19023
  2169
      apply (drule meta_spec [of _ c])
kleing@19023
  2170
      apply (drule DERIV_mult [where f="(\<lambda>x. g b - g a)" and g=f])
kleing@19023
  2171
      by simp_all
kleing@19023
  2172
    ultimately have "DERIV ?h c :>  g'c * (f b - f a) - f'c * (g b - g a)"
kleing@19023
  2173
      by (simp add: DERIV_diff)
kleing@19023
  2174
  }
kleing@19023
  2175
  ultimately have leq: "l =  g'c * (f b - f a) - f'c * (g b - g a)" by (rule DERIV_unique)
kleing@19023
  2176
kleing@19023
  2177
  {
kleing@19023
  2178
    from cdef have "?h b - ?h a = (b - a) * l" by auto
kleing@19023
  2179
    also with leq have "\<dots> = (b - a) * (g'c * (f b - f a) - f'c * (g b - g a))" by simp
kleing@19023
  2180
    finally have "?h b - ?h a = (b - a) * (g'c * (f b - f a) - f'c * (g b - g a))" by simp
kleing@19023
  2181
  }
kleing@19023
  2182
  moreover
kleing@19023
  2183
  {
kleing@19023
  2184
    have "?h b - ?h a =
kleing@19023
  2185
         ((f b)*(g b) - (f a)*(g b) - (g b)*(f b) + (g a)*(f b)) -
kleing@19023
  2186
          ((f b)*(g a) - (f a)*(g a) - (g b)*(f a) + (g a)*(f a))"
kleing@19023
  2187
      by (simp add: mult_ac add_ac real_diff_mult_distrib)
kleing@19023
  2188
    hence "?h b - ?h a = 0" by auto
kleing@19023
  2189
  }
kleing@19023
  2190
  ultimately have "(b - a) * (g'c * (f b - f a) - f'c * (g b - g a)) = 0" by auto
kleing@19023
  2191
  with alb have "g'c * (f b - f a) - f'c * (g b - g a) = 0" by simp
kleing@19023
  2192
  hence "g'c * (f b - f a) = f'c * (g b - g a)" by simp
kleing@19023
  2193
  hence "(f b - f a) * g'c = (g b - g a) * f'c" by (simp add: mult_ac)
kleing@19023
  2194
kleing@19023
  2195
  with g'cdef f'cdef cint show ?thesis by auto
kleing@19023
  2196
qed
kleing@19023
  2197
kleing@19023
  2198
kleing@19023
  2199
lemma LIMSEQ_SEQ_conv1:
huffman@20561
  2200
  fixes a :: real
kleing@19023
  2201
  assumes "X -- a --> L"
kleing@19023
  2202
  shows "\<forall>S. (\<forall>n. S n \<noteq> a) \<and> S ----> a \<longrightarrow> (\<lambda>n. X (S n)) ----> L"
kleing@19023
  2203
proof -
kleing@19023
  2204
  {
huffman@20563
  2205
    from prems have Xdef: "\<forall>r > 0. \<exists>s > 0. \<forall>x. x \<noteq> a & norm (x - a) < s --> norm (X x - L) < r" by (unfold LIM_def)
kleing@19023
  2206
    
kleing@19023
  2207
    fix S
kleing@19023
  2208
    assume as: "(\<forall>n. S n \<noteq> a) \<and> S ----> a"
kleing@19023
  2209
    then have "S ----> a" by auto
huffman@20563
  2210
    then have Sdef: "(\<forall>r. 0 < r --> (\<exists>no. \<forall>n. no \<le> n --> norm (S n - a) < r))" by (unfold LIMSEQ_def)
kleing@19023
  2211
    {
kleing@19023
  2212
      fix r
huffman@20563
  2213
      from Xdef have Xdef2: "0 < r --> (\<exists>s > 0. \<forall>x. x \<noteq> a \<and> \<bar>x - a\<bar> < s --> norm (X x - L) < r)" by simp
kleing@19023
  2214
      {
kleing@19023
  2215
        assume rgz: "0 < r"
kleing@19023
  2216
huffman@20563
  2217
        from Xdef2 rgz have "\<exists>s > 0. \<forall>x. x \<noteq> a \<and> \<bar>x - a\<bar> < s --> norm (X x - L) < r" by simp 
huffman@20563
  2218
        then obtain s where sdef: "s > 0 \<and> (\<forall>x. x\<noteq>a \<and> \<bar>x - a\<bar> < s \<longrightarrow> norm (X x - L) < r)" by auto
huffman@20563
  2219
        then have aux: "\<forall>x. x\<noteq>a \<and> \<bar>x - a\<bar> < s \<longrightarrow> norm (X x - L) < r" by auto
kleing@19023
  2220
        {
kleing@19023
  2221
          fix n
huffman@20563
  2222
          from aux have "S n \<noteq> a \<and> \<bar>S n - a\<bar> < s \<longrightarrow> norm (X (S n) - L) < r" by simp
huffman@20563
  2223
          with as have imp2: "\<bar>S n - a\<bar> < s --> norm (X (S n) - L) < r" by auto
kleing@19023
  2224
        }
huffman@20563
  2225
        hence "\<forall>n. \<bar>S n - a\<bar> < s --> norm (X (S n) - L) < r" ..
kleing@19023
  2226
        moreover
huffman@20563
  2227
        from Sdef sdef have imp1: "\<exists>no. \<forall>n. no \<le> n --> \<bar>S n - a\<bar> < s" by auto  
huffman@20563
  2228
        then obtain no where "\<forall>n. no \<le> n --> \<bar>S n - a\<bar> < s" by auto
huffman@20563
  2229
        ultimately have "\<forall>n. no \<le> n \<longrightarrow> norm (X (S n) - L) < r" by simp
huffman@20563
  2230
        hence "\<exists>no. \<forall>n. no \<le> n \<longrightarrow> norm (X (S n) - L) < r" by auto
kleing@19023
  2231
      }
kleing@19023
  2232
    }
huffman@20563
  2233
    hence "(\<forall>r. 0 < r --> (\<exists>no. \<forall>n. no \<le> n --> norm (X (S n) - L) < r))" by simp
kleing@19023
  2234
    hence "(\<lambda>n. X (S n)) ----> L" by (fold LIMSEQ_def)
kleing@19023
  2235
  }
kleing@19023
  2236
  thus ?thesis by simp
kleing@19023
  2237
qed
kleing@19023
  2238
webertj@20432
  2239
ML {* fast_arith_split_limit := 0; *}  (* FIXME *)
webertj@20217
  2240
kleing@19023
  2241
lemma LIMSEQ_SEQ_conv2:
huffman@20561
  2242
  fixes a :: real
kleing@19023
  2243
  assumes "\<forall>S. (\<forall>n. S n \<noteq> a) \<and> S ----> a \<longrightarrow> (\<lambda>n. X (S n)) ----> L"
kleing@19023
  2244
  shows "X -- a --> L"
kleing@19023
  2245
proof (rule ccontr)
kleing@19023
  2246
  assume "\<not> (X -- a --> L)"
huffman@20563
  2247
  hence "\<not> (\<forall>r > 0. \<exists>s > 0. \<forall>x. x \<noteq> a & norm (x - a) < s --> norm (X x - L) < r)" by (unfold LIM_def)
huffman@20563
  2248
  hence "\<exists>r > 0. \<forall>s > 0. \<exists>x. \<not>(x \<noteq> a \<and> \<bar>x - a\<bar> < s --> norm (X x - L) < r)" by simp
huffman@20563
  2249
  hence "\<exists>r > 0. \<forall>s > 0. \<exists>x. (x \<noteq> a \<and> \<bar>x - a\<bar> < s \<and> norm (X x - L) \<ge> r)" by (simp add: linorder_not_less)
huffman@20563
  2250
  then obtain r where rdef: "r > 0 \<and> (\<forall>s > 0. \<exists>x. (x \<noteq> a \<and> \<bar>x - a\<bar> < s \<and> norm (X x - L) \<ge> r))" by auto
kleing@19023
  2251
huffman@20563
  2252
  let ?F = "\<lambda>n::nat. SOME x. x\<noteq>a \<and> \<bar>x - a\<bar> < inverse (real (Suc n)) \<and> norm (X x - L) \<ge> r"
kleing@19023
  2253
  have "?F ----> a"
kleing@19023
  2254
  proof -
kleing@19023
  2255
    {
kleing@19023
  2256
      fix e::real
kleing@19023
  2257
      assume "0 < e"
kleing@19023
  2258
        (* choose no such that inverse (real (Suc n)) < e *)
kleing@19023
  2259
      have "\<exists>no. inverse (real (Suc no)) < e" by (rule reals_Archimedean)
kleing@19023
  2260
      then obtain m where nodef: "inverse (real (Suc m)) < e" by auto
kleing@19023
  2261
      {
kleing@19023
  2262
        fix n
kleing@19023
  2263
        assume mlen: "m \<le> n"
kleing@19023
  2264
        then have
kleing@19023
  2265
          "inverse (real (Suc n)) \<le> inverse (real (Suc m))"
kleing@19023
  2266
          by auto
kleing@19023
  2267
        moreover have
huffman@20563
  2268
          "\<bar>?F n - a\<bar> < inverse (real (Suc n))"
kleing@19023
  2269
        proof -
kleing@19023
  2270
          from rdef have
huffman@20563
  2271
            "\<exists>x. x\<noteq>a \<and> \<bar>x - a\<bar> < inverse (real (Suc n)) \<and> norm (X x - L) \<ge> r"
kleing@19023
  2272
            by simp
kleing@19023
  2273
          hence
huffman@20563
  2274
            "(?F n)\<noteq>a \<and> \<bar>(?F n) - a\<bar> < inverse (real (Suc n)) \<and> norm (X (?F n) - L) \<ge> r"
kleing@19023
  2275
            by (simp add: some_eq_ex [symmetric])
kleing@19023
  2276
          thus ?thesis by simp
kleing@19023
  2277
        qed
kleing@19023
  2278
        moreover from nodef have
kleing@19023
  2279
          "inverse (real (Suc m)) < e" .
huffman@20563
  2280
        ultimately have "\<bar>?F n - a\<bar> < e" by arith
kleing@19023
  2281
      }
huffman@20563
  2282
      then have "\<exists>no. \<forall>n. no \<le> n --> \<bar>?F n - a\<bar> < e" by auto
kleing@19023
  2283
    }
kleing@19023
  2284
    thus ?thesis by (unfold LIMSEQ_def, simp)
kleing@19023
  2285
  qed
kleing@19023
  2286
  
kleing@19023
  2287
  moreover have "\<forall>n. ?F n \<noteq> a"
kleing@19023
  2288
  proof -
kleing@19023
  2289
    {
kleing@19023
  2290
      fix n
kleing@19023
  2291
      from rdef have
huffman@20563
  2292
        "\<exists>x. x\<noteq>a \<and> \<bar>x - a\<bar> < inverse (real (Suc n)) \<and> norm (X x - L) \<ge> r"
kleing@19023
  2293
        by simp
kleing@19023
  2294
      hence "?F n \<noteq> a" by (simp add: some_eq_ex [symmetric])
kleing@19023
  2295
    }
kleing@19023
  2296
    thus ?thesis ..
kleing@19023
  2297
  qed
kleing@19023
  2298
  moreover from prems have "\<forall>S. (\<forall>n. S n \<noteq> a) \<and> S ----> a \<longrightarrow> (\<lambda>n. X (S n)) ----> L" by simp
kleing@19023
  2299
  ultimately have "(\<lambda>n. X (?F n)) ----> L" by simp
kleing@19023
  2300
  
kleing@19023
  2301
  moreover have "\<not> ((\<lambda>n. X (?F n)) ----> L)"
kleing@19023
  2302
  proof -
kleing@19023
  2303
    {
kleing@19023
  2304
      fix no::nat
kleing@19023
  2305
      obtain n where "n = no + 1" by simp
kleing@19023
  2306
      then have nolen: "no \<le> n" by simp
kleing@19023
  2307
        (* We prove this by showing that for any m there is an n\<ge>m such that |X (?F n) - L| \<ge> r *)
huffman@20563
  2308
      from rdef have "\<forall>s > 0. \<exists>x. (x \<noteq> a \<and> \<bar>x - a\<bar> < s \<and> norm (X x - L) \<ge> r)" ..
kleing@19023
  2309
huffman@20563
  2310
      then have "\<exists>x. x\<noteq>a \<and> \<bar>x - a\<bar> < inverse (real (Suc n)) \<and> norm (X x - L) \<ge> r" by simp
kleing@19023
  2311
      
huffman@20563
  2312
      hence "norm (X (?F n) - L) \<ge> r" by (simp add: some_eq_ex [symmetric])
huffman@20563
  2313
      with nolen have "\<exists>n. no \<le> n \<and> norm (X (?F n) - L) \<ge> r" by auto
kleing@19023
  2314
    }
huffman@20563
  2315
    then have "(\<forall>no. \<exists>n. no \<le> n \<and> norm (X (?F n) - L) \<ge> r)" by simp
huffman@20563
  2316
    with rdef have "\<exists>e>0. (\<forall>no. \<exists>n. no \<le> n \<and> norm (X (?F n) - L) \<ge> e)" by auto
kleing@19023
  2317
    thus ?thesis by (unfold LIMSEQ_def, auto simp add: linorder_not_less)
kleing@19023
  2318
  qed
kleing@19023
  2319
  ultimately show False by simp
kleing@19023
  2320
qed
kleing@19023
  2321
webertj@20432
  2322
ML {* fast_arith_split_limit := 9; *}  (* FIXME *)
kleing@19023
  2323
kleing@19023
  2324
lemma LIMSEQ_SEQ_conv:
huffman@20561
  2325
  "(\<forall>S. (\<forall>n. S n \<noteq> a) \<and> S ----> (a::real) \<longrightarrow> (\<lambda>n. X (S n)) ----> L) =
huffman@20561
  2326
   (X -- a --> L)"
kleing@19023
  2327
proof
kleing@19023
  2328
  assume "\<forall>S. (\<forall>n. S n \<noteq> a) \<and> S ----> a \<longrightarrow> (\<lambda>n. X (S n)) ----> L"
kleing@19023
  2329
  show "X -- a --> L" by (rule LIMSEQ_SEQ_conv2)
kleing@19023
  2330
next
kleing@19023
  2331
  assume "(X -- a --> L)"
kleing@19023
  2332
  show "\<forall>S. (\<forall>n. S n \<noteq> a) \<and> S ----> a \<longrightarrow> (\<lambda>n. X (S n)) ----> L" by (rule LIMSEQ_SEQ_conv1)
kleing@19023
  2333
qed
kleing@19023
  2334
kleing@19023
  2335
lemma real_sqz:
kleing@19023
  2336
  fixes a::real
kleing@19023
  2337
  assumes "a < c"
kleing@19023
  2338
  shows "\<exists>b. a < b \<and> b < c"
huffman@20563
  2339
by (rule dense)
kleing@19023
  2340
kleing@19023
  2341
lemma LIM_offset:
kleing@19023
  2342
  assumes "(\<lambda>x. f x) -- a --> L"
kleing@19023
  2343
  shows "(\<lambda>x. f (x+c)) -- (a-c) --> L"
kleing@19023
  2344
proof -
kleing@19023
  2345
  have "f -- a --> L" .
kleing@19023
  2346
  hence
huffman@20563
  2347
    fd: "\<forall>r > 0. \<exists>s > 0. \<forall>x. x \<noteq> a & norm (x - a) < s --> norm (f x - L) < r"
kleing@19023
  2348
    by (unfold LIM_def)
kleing@19023
  2349
  {
kleing@19023
  2350
    fix r::real
kleing@19023
  2351
    assume rgz: "0 < r"
huffman@20563
  2352
    with fd have "\<exists>s > 0. \<forall>x. x\<noteq>a \<and> norm (x - a) < s --> norm (f x - L) < r" by simp
huffman@20563
  2353
    then obtain s where sgz: "s > 0" and ax: "\<forall>x. x\<noteq>a \<and> norm (x - a) < s \<longrightarrow> norm (f x - L) < r" by auto
huffman@20563
  2354
    from ax have ax2: "\<forall>x. (x+c)\<noteq>a \<and> norm ((x+c) - a) < s \<longrightarrow> norm (f (x+c) - L) < r" by auto
kleing@19023
  2355
    {
huffman@20561
  2356
      fix x
huffman@20563
  2357
      from ax2 have nt: "(x+c)\<noteq>a \<and> norm ((x+c) - a) < s \<longrightarrow> norm (f (x+c) - L) < r" ..
kleing@19023
  2358
      moreover have "((x+c)\<noteq>a) = (x\<noteq>(a-c))" by auto
huffman@20563
  2359
      moreover have "((x+c) - a) = (x - (a-c))" by simp
huffman@20563
  2360
      ultimately have "x\<noteq>(a-c) \<and> norm (x - (a-c)) < s \<longrightarrow> norm (f (x+c) - L) < r" by simp
kleing@19023
  2361
    }
huffman@20563
  2362
    then have "\<forall>x. x\<noteq>(a-c) \<and> norm (x - (a-c)) < s \<longrightarrow> norm (f (x+c) - L) < r" ..
huffman@20563
  2363
    with sgz have "\<exists>s > 0. \<forall>x. x\<noteq>(a-c) \<and> norm (x - (a-c)) < s \<longrightarrow> norm (f (x+c) - L) < r" by auto
kleing@19023
  2364
  }
kleing@19023
  2365
  then have
huffman@20563
  2366
    "\<forall>r > 0. \<exists>s > 0. \<forall>x. x \<noteq> (a-c) & norm (x - (a-c)) < s --> norm (f (x+c) - L) < r" by simp
kleing@19023
  2367
  thus ?thesis by (fold LIM_def)
kleing@19023
  2368
qed
kleing@19023
  2369
paulson@10751
  2370
end