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