src/HOL/Library/BigO.thy
author wenzelm
Thu Jul 28 15:19:47 2005 +0200 (2005-07-28)
changeset 16932 0bca871f5a21
parent 16908 d374530bfaaa
child 16961 9c5871b16553
permissions -rwxr-xr-x
proper header;
wenzelm@16932
     1
(*  Title:      HOL/Library/BigO.thy
wenzelm@16932
     2
    ID:		$Id$
avigad@16908
     3
    Authors:    Jeremy Avigad and Kevin Donnelly
avigad@16908
     4
*)
avigad@16908
     5
avigad@16908
     6
header {* Big O notation *}
avigad@16908
     7
avigad@16908
     8
theory BigO
avigad@16908
     9
imports SetsAndFunctions
avigad@16908
    10
begin
avigad@16908
    11
avigad@16908
    12
text {*
avigad@16908
    13
This library is designed to support asymptotic ``big O'' calculations,
avigad@16908
    14
i.e.~reasoning with expressions of the form $f = O(g)$ and $f = g + O(h)$.
avigad@16908
    15
An earlier version of this library is described in detail in
avigad@16908
    16
\begin{quote}
avigad@16908
    17
Avigad, Jeremy, and Kevin Donnelly, \emph{Formalizing O notation in 
avigad@16908
    18
Isabelle/HOL}, in David Basin and Micha\"el Rusiowitch, editors, 
avigad@16908
    19
\emph{Automated Reasoning: second international conference, IJCAR 2004}, 
avigad@16908
    20
Springer, 357--371, 2004.
avigad@16908
    21
\end{quote}
avigad@16908
    22
The main changes in this version are as follows:
avigad@16908
    23
\begin{itemize}
avigad@16908
    24
\item We have eliminated the $O$ operator on sets. (Most uses of this seem
avigad@16908
    25
  to be inessential.)
avigad@16908
    26
\item We no longer use $+$ as output syntax for $+o$.
avigad@16908
    27
\item Lemmas involving ``sumr-pos'' have been replaced by more
avigad@16908
    28
  general lemmas involving ``setsum''.
avigad@16908
    29
\item The library has been expanded, with e.g.~support for expressions of
avigad@16908
    30
  the form $f < g + O(h)$.
avigad@16908
    31
\end{itemize}
avigad@16908
    32
Note that two lemmas at the end of this file are commented out, as they 
avigad@16908
    33
require the HOL-Complex library.
avigad@16908
    34
avigad@16908
    35
Note also since the Big O library includes rules that demonstrate set 
avigad@16908
    36
inclusion, to use the automated reasoners effectively with the library one 
avigad@16908
    37
should redeclare the theorem ``subsetI'' as an intro rule, rather than as 
avigad@16908
    38
an intro! rule, for example, using ``declare subsetI [del, intro]''.
avigad@16908
    39
*}
avigad@16908
    40
avigad@16908
    41
subsection {* Definitions *}
avigad@16908
    42
avigad@16908
    43
constdefs 
avigad@16908
    44
avigad@16908
    45
  bigo :: "('a => 'b::ordered_idom) => ('a => 'b) set"    ("(1O'(_'))")
avigad@16908
    46
  "O(f::('a => 'b)) == 
avigad@16908
    47
      {h. EX c. ALL x. abs (h x) <= c * abs (f x)}"
avigad@16908
    48
avigad@16908
    49
lemma bigo_pos_const: "(EX (c::'a::ordered_idom). 
avigad@16908
    50
    ALL x. (abs (h x)) <= (c * (abs (f x))))
avigad@16908
    51
      = (EX c. 0 < c & (ALL x. (abs(h x)) <= (c * (abs (f x)))))"
avigad@16908
    52
  apply auto
avigad@16908
    53
  apply (case_tac "c = 0")
avigad@16908
    54
  apply simp
avigad@16908
    55
  apply (rule_tac x = "1" in exI)
avigad@16908
    56
  apply simp
avigad@16908
    57
  apply (rule_tac x = "abs c" in exI)
avigad@16908
    58
  apply auto
avigad@16908
    59
  apply (subgoal_tac "c * abs(f x) <= abs c * abs (f x)")
avigad@16908
    60
  apply (erule_tac x = x in allE)
avigad@16908
    61
  apply force
avigad@16908
    62
  apply (rule mult_right_mono)
avigad@16908
    63
  apply (rule abs_ge_self)
avigad@16908
    64
  apply (rule abs_ge_zero)
avigad@16908
    65
done
avigad@16908
    66
avigad@16908
    67
lemma bigo_alt_def: "O(f) = 
avigad@16908
    68
    {h. EX c. (0 < c & (ALL x. abs (h x) <= c * abs (f x)))}"
avigad@16908
    69
by (auto simp add: bigo_def bigo_pos_const)
avigad@16908
    70
avigad@16908
    71
lemma bigo_elt_subset [intro]: "f : O(g) ==> O(f) <= O(g)"
avigad@16908
    72
  apply (auto simp add: bigo_alt_def)
avigad@16908
    73
  apply (rule_tac x = "ca * c" in exI)
avigad@16908
    74
  apply (rule conjI)
avigad@16908
    75
  apply (rule mult_pos_pos)
avigad@16908
    76
  apply (assumption)+
avigad@16908
    77
  apply (rule allI)
avigad@16908
    78
  apply (drule_tac x = "xa" in spec)+
avigad@16908
    79
  apply (subgoal_tac "ca * abs(f xa) <= ca * (c * abs(g xa))")
avigad@16908
    80
  apply (erule order_trans)
avigad@16908
    81
  apply (simp add: mult_ac)
avigad@16908
    82
  apply (rule mult_left_mono, assumption)
avigad@16908
    83
  apply (rule order_less_imp_le, assumption)
avigad@16908
    84
done
avigad@16908
    85
avigad@16908
    86
lemma bigo_refl [intro]: "f : O(f)"
avigad@16908
    87
  apply(auto simp add: bigo_def)
avigad@16908
    88
  apply(rule_tac x = 1 in exI)
avigad@16908
    89
  apply simp
avigad@16908
    90
done
avigad@16908
    91
avigad@16908
    92
lemma bigo_zero: "0 : O(g)"
avigad@16908
    93
  apply (auto simp add: bigo_def func_zero)
avigad@16908
    94
  apply (rule_tac x = 0 in exI)
avigad@16908
    95
  apply auto
avigad@16908
    96
done
avigad@16908
    97
avigad@16908
    98
lemma bigo_zero2: "O(%x.0) = {%x.0}"
avigad@16908
    99
  apply (auto simp add: bigo_def) 
avigad@16908
   100
  apply (rule ext)
avigad@16908
   101
  apply auto
avigad@16908
   102
done
avigad@16908
   103
avigad@16908
   104
lemma bigo_plus_self_subset [intro]: 
avigad@16908
   105
  "O(f) + O(f) <= O(f)"
avigad@16908
   106
  apply (auto simp add: bigo_alt_def set_plus)
avigad@16908
   107
  apply (rule_tac x = "c + ca" in exI)
avigad@16908
   108
  apply auto
avigad@16908
   109
  apply (simp add: ring_distrib func_plus)
avigad@16908
   110
  apply (rule order_trans)
avigad@16908
   111
  apply (rule abs_triangle_ineq)
avigad@16908
   112
  apply (rule add_mono)
avigad@16908
   113
  apply force
avigad@16908
   114
  apply force
avigad@16908
   115
done
avigad@16908
   116
avigad@16908
   117
lemma bigo_plus_idemp [simp]: "O(f) + O(f) = O(f)"
avigad@16908
   118
  apply (rule equalityI)
avigad@16908
   119
  apply (rule bigo_plus_self_subset)
avigad@16908
   120
  apply (rule set_zero_plus2) 
avigad@16908
   121
  apply (rule bigo_zero)
avigad@16908
   122
done
avigad@16908
   123
avigad@16908
   124
lemma bigo_plus_subset [intro]: "O(f + g) <= O(f) + O(g)"
avigad@16908
   125
  apply (rule subsetI)
avigad@16908
   126
  apply (auto simp add: bigo_def bigo_pos_const func_plus set_plus)
avigad@16908
   127
  apply (subst bigo_pos_const [symmetric])+
avigad@16908
   128
  apply (rule_tac x = 
avigad@16908
   129
    "%n. if abs (g n) <= (abs (f n)) then x n else 0" in exI)
avigad@16908
   130
  apply (rule conjI)
avigad@16908
   131
  apply (rule_tac x = "c + c" in exI)
avigad@16908
   132
  apply (clarsimp)
avigad@16908
   133
  apply (auto)
avigad@16908
   134
  apply (subgoal_tac "c * abs (f xa + g xa) <= (c + c) * abs (f xa)")
avigad@16908
   135
  apply (erule_tac x = xa in allE)
avigad@16908
   136
  apply (erule order_trans)
avigad@16908
   137
  apply (simp)
avigad@16908
   138
  apply (subgoal_tac "c * abs (f xa + g xa) <= c * (abs (f xa) + abs (g xa))")
avigad@16908
   139
  apply (erule order_trans)
avigad@16908
   140
  apply (simp add: ring_distrib)
avigad@16908
   141
  apply (rule mult_left_mono)
avigad@16908
   142
  apply assumption
avigad@16908
   143
  apply (simp add: order_less_le)
avigad@16908
   144
  apply (rule mult_left_mono)
avigad@16908
   145
  apply (simp add: abs_triangle_ineq)
avigad@16908
   146
  apply (simp add: order_less_le)
avigad@16908
   147
  apply (rule mult_nonneg_nonneg)
avigad@16908
   148
  apply (rule add_nonneg_nonneg)
avigad@16908
   149
  apply auto
avigad@16908
   150
  apply (rule_tac x = "%n. if (abs (f n)) <  abs (g n) then x n else 0" 
avigad@16908
   151
     in exI)
avigad@16908
   152
  apply (rule conjI)
avigad@16908
   153
  apply (rule_tac x = "c + c" in exI)
avigad@16908
   154
  apply auto
avigad@16908
   155
  apply (subgoal_tac "c * abs (f xa + g xa) <= (c + c) * abs (g xa)")
avigad@16908
   156
  apply (erule_tac x = xa in allE)
avigad@16908
   157
  apply (erule order_trans)
avigad@16908
   158
  apply (simp)
avigad@16908
   159
  apply (subgoal_tac "c * abs (f xa + g xa) <= c * (abs (f xa) + abs (g xa))")
avigad@16908
   160
  apply (erule order_trans)
avigad@16908
   161
  apply (simp add: ring_distrib)
avigad@16908
   162
  apply (rule mult_left_mono)
avigad@16908
   163
  apply (simp add: order_less_le)
avigad@16908
   164
  apply (simp add: order_less_le)
avigad@16908
   165
  apply (rule mult_left_mono)
avigad@16908
   166
  apply (rule abs_triangle_ineq)
avigad@16908
   167
  apply (simp add: order_less_le)
avigad@16908
   168
  apply (rule mult_nonneg_nonneg)
avigad@16908
   169
  apply (rule add_nonneg_nonneg)
avigad@16908
   170
  apply (erule order_less_imp_le)+
avigad@16908
   171
  apply simp
avigad@16908
   172
  apply (rule ext)
avigad@16908
   173
  apply (auto simp add: if_splits linorder_not_le)
avigad@16908
   174
done
avigad@16908
   175
avigad@16908
   176
lemma bigo_plus_subset2 [intro]: "A <= O(f) ==> B <= O(f) ==> A + B <= O(f)"
avigad@16908
   177
  apply (subgoal_tac "A + B <= O(f) + O(f)")
avigad@16908
   178
  apply (erule order_trans)
avigad@16908
   179
  apply simp
avigad@16908
   180
  apply (auto del: subsetI simp del: bigo_plus_idemp)
avigad@16908
   181
done
avigad@16908
   182
avigad@16908
   183
lemma bigo_plus_eq: "ALL x. 0 <= f x ==> ALL x. 0 <= g x ==> 
avigad@16908
   184
  O(f + g) = O(f) + O(g)"
avigad@16908
   185
  apply (rule equalityI)
avigad@16908
   186
  apply (rule bigo_plus_subset)
avigad@16908
   187
  apply (simp add: bigo_alt_def set_plus func_plus)
avigad@16908
   188
  apply clarify
avigad@16908
   189
  apply (rule_tac x = "max c ca" in exI)
avigad@16908
   190
  apply (rule conjI)
avigad@16908
   191
  apply (subgoal_tac "c <= max c ca")
avigad@16908
   192
  apply (erule order_less_le_trans)
avigad@16908
   193
  apply assumption
avigad@16908
   194
  apply (rule le_maxI1)
avigad@16908
   195
  apply clarify
avigad@16908
   196
  apply (drule_tac x = "xa" in spec)+
avigad@16908
   197
  apply (subgoal_tac "0 <= f xa + g xa")
avigad@16908
   198
  apply (simp add: ring_distrib)
avigad@16908
   199
  apply (subgoal_tac "abs(a xa + b xa) <= abs(a xa) + abs(b xa)")
avigad@16908
   200
  apply (subgoal_tac "abs(a xa) + abs(b xa) <= 
avigad@16908
   201
      max c ca * f xa + max c ca * g xa")
avigad@16908
   202
  apply (force)
avigad@16908
   203
  apply (rule add_mono)
avigad@16908
   204
  apply (subgoal_tac "c * f xa <= max c ca * f xa")
avigad@16908
   205
  apply (force)
avigad@16908
   206
  apply (rule mult_right_mono)
avigad@16908
   207
  apply (rule le_maxI1)
avigad@16908
   208
  apply assumption
avigad@16908
   209
  apply (subgoal_tac "ca * g xa <= max c ca * g xa")
avigad@16908
   210
  apply (force)
avigad@16908
   211
  apply (rule mult_right_mono)
avigad@16908
   212
  apply (rule le_maxI2)
avigad@16908
   213
  apply assumption
avigad@16908
   214
  apply (rule abs_triangle_ineq)
avigad@16908
   215
  apply (rule add_nonneg_nonneg)
avigad@16908
   216
  apply assumption+
avigad@16908
   217
done
avigad@16908
   218
avigad@16908
   219
lemma bigo_bounded_alt: "ALL x. 0 <= f x ==> ALL x. f x <= c * g x ==> 
avigad@16908
   220
    f : O(g)" 
avigad@16908
   221
  apply (auto simp add: bigo_def)
avigad@16908
   222
  apply (rule_tac x = "abs c" in exI)
avigad@16908
   223
  apply auto
avigad@16908
   224
  apply (drule_tac x = x in spec)+
avigad@16908
   225
  apply (simp add: abs_mult [symmetric])
avigad@16908
   226
done
avigad@16908
   227
avigad@16908
   228
lemma bigo_bounded: "ALL x. 0 <= f x ==> ALL x. f x <= g x ==> 
avigad@16908
   229
    f : O(g)" 
avigad@16908
   230
  apply (erule bigo_bounded_alt [of f 1 g])
avigad@16908
   231
  apply simp
avigad@16908
   232
done
avigad@16908
   233
avigad@16908
   234
lemma bigo_bounded2: "ALL x. lb x <= f x ==> ALL x. f x <= lb x + g x ==>
avigad@16908
   235
    f : lb +o O(g)"
avigad@16908
   236
  apply (rule set_minus_imp_plus)
avigad@16908
   237
  apply (rule bigo_bounded)
avigad@16908
   238
  apply (auto simp add: diff_minus func_minus func_plus)
avigad@16908
   239
  apply (drule_tac x = x in spec)+
avigad@16908
   240
  apply force
avigad@16908
   241
  apply (drule_tac x = x in spec)+
avigad@16908
   242
  apply force
avigad@16908
   243
done
avigad@16908
   244
avigad@16908
   245
lemma bigo_abs: "(%x. abs(f x)) =o O(f)" 
avigad@16908
   246
  apply (unfold bigo_def)
avigad@16908
   247
  apply auto
avigad@16908
   248
  apply (rule_tac x = 1 in exI)
avigad@16908
   249
  apply auto
avigad@16908
   250
done
avigad@16908
   251
avigad@16908
   252
lemma bigo_abs2: "f =o O(%x. abs(f x))"
avigad@16908
   253
  apply (unfold bigo_def)
avigad@16908
   254
  apply auto
avigad@16908
   255
  apply (rule_tac x = 1 in exI)
avigad@16908
   256
  apply auto
avigad@16908
   257
done
avigad@16908
   258
avigad@16908
   259
lemma bigo_abs3: "O(f) = O(%x. abs(f x))"
avigad@16908
   260
  apply (rule equalityI)
avigad@16908
   261
  apply (rule bigo_elt_subset)
avigad@16908
   262
  apply (rule bigo_abs2)
avigad@16908
   263
  apply (rule bigo_elt_subset)
avigad@16908
   264
  apply (rule bigo_abs)
avigad@16908
   265
done
avigad@16908
   266
avigad@16908
   267
lemma bigo_abs4: "f =o g +o O(h) ==> 
avigad@16908
   268
    (%x. abs (f x)) =o (%x. abs (g x)) +o O(h)"
avigad@16908
   269
  apply (drule set_plus_imp_minus)
avigad@16908
   270
  apply (rule set_minus_imp_plus)
avigad@16908
   271
  apply (subst func_diff)
avigad@16908
   272
proof -
avigad@16908
   273
  assume a: "f - g : O(h)"
avigad@16908
   274
  have "(%x. abs (f x) - abs (g x)) =o O(%x. abs(abs (f x) - abs (g x)))"
avigad@16908
   275
    by (rule bigo_abs2)
avigad@16908
   276
  also have "... <= O(%x. abs (f x - g x))"
avigad@16908
   277
    apply (rule bigo_elt_subset)
avigad@16908
   278
    apply (rule bigo_bounded)
avigad@16908
   279
    apply force
avigad@16908
   280
    apply (rule allI)
avigad@16908
   281
    apply (rule abs_triangle_ineq3)
avigad@16908
   282
    done
avigad@16908
   283
  also have "... <= O(f - g)"
avigad@16908
   284
    apply (rule bigo_elt_subset)
avigad@16908
   285
    apply (subst func_diff)
avigad@16908
   286
    apply (rule bigo_abs)
avigad@16908
   287
    done
avigad@16908
   288
  also have "... <= O(h)"
avigad@16908
   289
    by (rule bigo_elt_subset)
avigad@16908
   290
  finally show "(%x. abs (f x) - abs (g x)) : O(h)".
avigad@16908
   291
qed
avigad@16908
   292
avigad@16908
   293
lemma bigo_abs5: "f =o O(g) ==> (%x. abs(f x)) =o O(g)" 
avigad@16908
   294
by (unfold bigo_def, auto)
avigad@16908
   295
avigad@16908
   296
lemma bigo_elt_subset2 [intro]: "f : g +o O(h) ==> O(f) <= O(g) + O(h)"
avigad@16908
   297
proof -
avigad@16908
   298
  assume "f : g +o O(h)"
avigad@16908
   299
  also have "... <= O(g) + O(h)"
avigad@16908
   300
    by (auto del: subsetI)
avigad@16908
   301
  also have "... = O(%x. abs(g x)) + O(%x. abs(h x))"
avigad@16908
   302
    apply (subst bigo_abs3 [symmetric])+
avigad@16908
   303
    apply (rule refl)
avigad@16908
   304
    done
avigad@16908
   305
  also have "... = O((%x. abs(g x)) + (%x. abs(h x)))"
avigad@16908
   306
    by (rule bigo_plus_eq [symmetric], auto)
avigad@16908
   307
  finally have "f : ...".
avigad@16908
   308
  then have "O(f) <= ..."
avigad@16908
   309
    by (elim bigo_elt_subset)
avigad@16908
   310
  also have "... = O(%x. abs(g x)) + O(%x. abs(h x))"
avigad@16908
   311
    by (rule bigo_plus_eq, auto)
avigad@16908
   312
  finally show ?thesis
avigad@16908
   313
    by (simp add: bigo_abs3 [symmetric])
avigad@16908
   314
qed
avigad@16908
   315
avigad@16908
   316
lemma bigo_mult [intro]: "O(f)*O(g) <= O(f * g)"
avigad@16908
   317
  apply (rule subsetI)
avigad@16908
   318
  apply (subst bigo_def)
avigad@16908
   319
  apply (auto simp add: bigo_alt_def set_times func_times)
avigad@16908
   320
  apply (rule_tac x = "c * ca" in exI)
avigad@16908
   321
  apply(rule allI)
avigad@16908
   322
  apply(erule_tac x = x in allE)+
avigad@16908
   323
  apply(subgoal_tac "c * ca * abs(f x * g x) = 
avigad@16908
   324
      (c * abs(f x)) * (ca * abs(g x))")
avigad@16908
   325
  apply(erule ssubst)
avigad@16908
   326
  apply (subst abs_mult)
avigad@16908
   327
  apply (rule mult_mono)
avigad@16908
   328
  apply assumption+
avigad@16908
   329
  apply (rule mult_nonneg_nonneg)
avigad@16908
   330
  apply auto
avigad@16908
   331
  apply (simp add: mult_ac abs_mult)
avigad@16908
   332
done
avigad@16908
   333
avigad@16908
   334
lemma bigo_mult2 [intro]: "f *o O(g) <= O(f * g)"
avigad@16908
   335
  apply (auto simp add: bigo_def elt_set_times_def func_times abs_mult)
avigad@16908
   336
  apply (rule_tac x = c in exI)
avigad@16908
   337
  apply auto
avigad@16908
   338
  apply (drule_tac x = x in spec)
avigad@16908
   339
  apply (subgoal_tac "abs(f x) * abs(b x) <= abs(f x) * (c * abs(g x))")
avigad@16908
   340
  apply (force simp add: mult_ac)
avigad@16908
   341
  apply (rule mult_left_mono, assumption)
avigad@16908
   342
  apply (rule abs_ge_zero)
avigad@16908
   343
done
avigad@16908
   344
avigad@16908
   345
lemma bigo_mult3: "f : O(h) ==> g : O(j) ==> f * g : O(h * j)"
avigad@16908
   346
  apply (rule subsetD)
avigad@16908
   347
  apply (rule bigo_mult)
avigad@16908
   348
  apply (erule set_times_intro, assumption)
avigad@16908
   349
done
avigad@16908
   350
avigad@16908
   351
lemma bigo_mult4 [intro]:"f : k +o O(h) ==> g * f : (g * k) +o O(g * h)"
avigad@16908
   352
  apply (drule set_plus_imp_minus)
avigad@16908
   353
  apply (rule set_minus_imp_plus)
avigad@16908
   354
  apply (drule bigo_mult3 [where g = g and j = g])
avigad@16908
   355
  apply (auto simp add: ring_eq_simps mult_ac)
avigad@16908
   356
done
avigad@16908
   357
avigad@16908
   358
lemma bigo_mult5: "ALL x. f x ~= 0 ==>
avigad@16908
   359
    O(f * g) <= (f::'a => ('b::ordered_field)) *o O(g)"
avigad@16908
   360
proof -
avigad@16908
   361
  assume "ALL x. f x ~= 0"
avigad@16908
   362
  show "O(f * g) <= f *o O(g)"
avigad@16908
   363
  proof
avigad@16908
   364
    fix h
avigad@16908
   365
    assume "h : O(f * g)"
avigad@16908
   366
    then have "(%x. 1 / (f x)) * h : (%x. 1 / f x) *o O(f * g)"
avigad@16908
   367
      by auto
avigad@16908
   368
    also have "... <= O((%x. 1 / f x) * (f * g))"
avigad@16908
   369
      by (rule bigo_mult2)
avigad@16908
   370
    also have "(%x. 1 / f x) * (f * g) = g"
avigad@16908
   371
      apply (simp add: func_times) 
avigad@16908
   372
      apply (rule ext)
avigad@16908
   373
      apply (simp add: prems nonzero_divide_eq_eq mult_ac)
avigad@16908
   374
      done
avigad@16908
   375
    finally have "(%x. (1::'b) / f x) * h : O(g)".
avigad@16908
   376
    then have "f * ((%x. (1::'b) / f x) * h) : f *o O(g)"
avigad@16908
   377
      by auto
avigad@16908
   378
    also have "f * ((%x. (1::'b) / f x) * h) = h"
avigad@16908
   379
      apply (simp add: func_times) 
avigad@16908
   380
      apply (rule ext)
avigad@16908
   381
      apply (simp add: prems nonzero_divide_eq_eq mult_ac)
avigad@16908
   382
      done
avigad@16908
   383
    finally show "h : f *o O(g)".
avigad@16908
   384
  qed
avigad@16908
   385
qed
avigad@16908
   386
avigad@16908
   387
lemma bigo_mult6: "ALL x. f x ~= 0 ==>
avigad@16908
   388
    O(f * g) = (f::'a => ('b::ordered_field)) *o O(g)"
avigad@16908
   389
  apply (rule equalityI)
avigad@16908
   390
  apply (erule bigo_mult5)
avigad@16908
   391
  apply (rule bigo_mult2)
avigad@16908
   392
done
avigad@16908
   393
avigad@16908
   394
lemma bigo_mult7: "ALL x. f x ~= 0 ==>
avigad@16908
   395
    O(f * g) <= O(f::'a => ('b::ordered_field)) * O(g)"
avigad@16908
   396
  apply (subst bigo_mult6)
avigad@16908
   397
  apply assumption
avigad@16908
   398
  apply (rule set_times_mono3)
avigad@16908
   399
  apply (rule bigo_refl)
avigad@16908
   400
done
avigad@16908
   401
avigad@16908
   402
lemma bigo_mult8: "ALL x. f x ~= 0 ==>
avigad@16908
   403
    O(f * g) = O(f::'a => ('b::ordered_field)) * O(g)"
avigad@16908
   404
  apply (rule equalityI)
avigad@16908
   405
  apply (erule bigo_mult7)
avigad@16908
   406
  apply (rule bigo_mult)
avigad@16908
   407
done
avigad@16908
   408
avigad@16908
   409
lemma bigo_minus [intro]: "f : O(g) ==> - f : O(g)"
avigad@16908
   410
  by (auto simp add: bigo_def func_minus)
avigad@16908
   411
avigad@16908
   412
lemma bigo_minus2: "f : g +o O(h) ==> -f : -g +o O(h)"
avigad@16908
   413
  apply (rule set_minus_imp_plus)
avigad@16908
   414
  apply (drule set_plus_imp_minus)
avigad@16908
   415
  apply (drule bigo_minus)
avigad@16908
   416
  apply (simp add: diff_minus)
avigad@16908
   417
done
avigad@16908
   418
avigad@16908
   419
lemma bigo_minus3: "O(-f) = O(f)"
avigad@16908
   420
  by (auto simp add: bigo_def func_minus abs_minus_cancel)
avigad@16908
   421
avigad@16908
   422
lemma bigo_plus_absorb_lemma1: "f : O(g) ==> f +o O(g) <= O(g)"
avigad@16908
   423
proof -
avigad@16908
   424
  assume a: "f : O(g)"
avigad@16908
   425
  show "f +o O(g) <= O(g)"
avigad@16908
   426
  proof -
avigad@16908
   427
    have "f : O(f)" by auto
avigad@16908
   428
    then have "f +o O(g) <= O(f) + O(g)"
avigad@16908
   429
      by (auto del: subsetI)
avigad@16908
   430
    also have "... <= O(g) + O(g)"
avigad@16908
   431
    proof -
avigad@16908
   432
      from a have "O(f) <= O(g)" by (auto del: subsetI)
avigad@16908
   433
      thus ?thesis by (auto del: subsetI)
avigad@16908
   434
    qed
avigad@16908
   435
    also have "... <= O(g)" by (simp add: bigo_plus_idemp)
avigad@16908
   436
    finally show ?thesis .
avigad@16908
   437
  qed
avigad@16908
   438
qed
avigad@16908
   439
avigad@16908
   440
lemma bigo_plus_absorb_lemma2: "f : O(g) ==> O(g) <= f +o O(g)"
avigad@16908
   441
proof -
avigad@16908
   442
  assume a: "f : O(g)"
avigad@16908
   443
  show "O(g) <= f +o O(g)"
avigad@16908
   444
  proof -
avigad@16908
   445
    from a have "-f : O(g)" by auto
avigad@16908
   446
    then have "-f +o O(g) <= O(g)" by (elim bigo_plus_absorb_lemma1)
avigad@16908
   447
    then have "f +o (-f +o O(g)) <= f +o O(g)" by auto
avigad@16908
   448
    also have "f +o (-f +o O(g)) = O(g)"
avigad@16908
   449
      by (simp add: set_plus_rearranges)
avigad@16908
   450
    finally show ?thesis .
avigad@16908
   451
  qed
avigad@16908
   452
qed
avigad@16908
   453
avigad@16908
   454
lemma bigo_plus_absorb [simp]: "f : O(g) ==> f +o O(g) = O(g)"
avigad@16908
   455
  apply (rule equalityI)
avigad@16908
   456
  apply (erule bigo_plus_absorb_lemma1)
avigad@16908
   457
  apply (erule bigo_plus_absorb_lemma2)
avigad@16908
   458
done
avigad@16908
   459
avigad@16908
   460
lemma bigo_plus_absorb2 [intro]: "f : O(g) ==> A <= O(g) ==> f +o A <= O(g)"
avigad@16908
   461
  apply (subgoal_tac "f +o A <= f +o O(g)")
avigad@16908
   462
  apply force+
avigad@16908
   463
done
avigad@16908
   464
avigad@16908
   465
lemma bigo_add_commute_imp: "f : g +o O(h) ==> g : f +o O(h)"
avigad@16908
   466
  apply (subst set_minus_plus [symmetric])
avigad@16908
   467
  apply (subgoal_tac "g - f = - (f - g)")
avigad@16908
   468
  apply (erule ssubst)
avigad@16908
   469
  apply (rule bigo_minus)
avigad@16908
   470
  apply (subst set_minus_plus)
avigad@16908
   471
  apply assumption
avigad@16908
   472
  apply  (simp add: diff_minus add_ac)
avigad@16908
   473
done
avigad@16908
   474
avigad@16908
   475
lemma bigo_add_commute: "(f : g +o O(h)) = (g : f +o O(h))"
avigad@16908
   476
  apply (rule iffI)
avigad@16908
   477
  apply (erule bigo_add_commute_imp)+
avigad@16908
   478
done
avigad@16908
   479
avigad@16908
   480
lemma bigo_const1: "(%x. c) : O(%x. 1)"
avigad@16908
   481
by (auto simp add: bigo_def mult_ac)
avigad@16908
   482
avigad@16908
   483
lemma bigo_const2 [intro]: "O(%x. c) <= O(%x. 1)"
avigad@16908
   484
  apply (rule bigo_elt_subset)
avigad@16908
   485
  apply (rule bigo_const1)
avigad@16908
   486
done
avigad@16908
   487
avigad@16908
   488
lemma bigo_const3: "(c::'a::ordered_field) ~= 0 ==> (%x. 1) : O(%x. c)"
avigad@16908
   489
  apply (simp add: bigo_def)
avigad@16908
   490
  apply (rule_tac x = "abs(inverse c)" in exI)
avigad@16908
   491
  apply (simp add: abs_mult [symmetric])
avigad@16908
   492
done
avigad@16908
   493
avigad@16908
   494
lemma bigo_const4: "(c::'a::ordered_field) ~= 0 ==> O(%x. 1) <= O(%x. c)"
avigad@16908
   495
by (rule bigo_elt_subset, rule bigo_const3, assumption)
avigad@16908
   496
avigad@16908
   497
lemma bigo_const [simp]: "(c::'a::ordered_field) ~= 0 ==> 
avigad@16908
   498
    O(%x. c) = O(%x. 1)"
avigad@16908
   499
by (rule equalityI, rule bigo_const2, rule bigo_const4, assumption)
avigad@16908
   500
avigad@16908
   501
lemma bigo_const_mult1: "(%x. c * f x) : O(f)"
avigad@16908
   502
  apply (simp add: bigo_def)
avigad@16908
   503
  apply (rule_tac x = "abs(c)" in exI)
avigad@16908
   504
  apply (auto simp add: abs_mult [symmetric])
avigad@16908
   505
done
avigad@16908
   506
avigad@16908
   507
lemma bigo_const_mult2: "O(%x. c * f x) <= O(f)"
avigad@16908
   508
by (rule bigo_elt_subset, rule bigo_const_mult1)
avigad@16908
   509
avigad@16908
   510
lemma bigo_const_mult3: "(c::'a::ordered_field) ~= 0 ==> f : O(%x. c * f x)"
avigad@16908
   511
  apply (simp add: bigo_def)
avigad@16908
   512
  apply (rule_tac x = "abs(inverse c)" in exI)
avigad@16908
   513
  apply (simp add: abs_mult [symmetric] mult_assoc [symmetric])
avigad@16908
   514
done
avigad@16908
   515
avigad@16908
   516
lemma bigo_const_mult4: "(c::'a::ordered_field) ~= 0 ==> 
avigad@16908
   517
    O(f) <= O(%x. c * f x)"
avigad@16908
   518
by (rule bigo_elt_subset, rule bigo_const_mult3, assumption)
avigad@16908
   519
avigad@16908
   520
lemma bigo_const_mult [simp]: "(c::'a::ordered_field) ~= 0 ==> 
avigad@16908
   521
    O(%x. c * f x) = O(f)"
avigad@16908
   522
by (rule equalityI, rule bigo_const_mult2, erule bigo_const_mult4)
avigad@16908
   523
avigad@16908
   524
lemma bigo_const_mult5 [simp]: "(c::'a::ordered_field) ~= 0 ==> 
avigad@16908
   525
    (%x. c) *o O(f) = O(f)"
avigad@16908
   526
  apply (auto del: subsetI)
avigad@16908
   527
  apply (rule order_trans)
avigad@16908
   528
  apply (rule bigo_mult2)
avigad@16908
   529
  apply (simp add: func_times)
avigad@16908
   530
  apply (auto intro!: subsetI simp add: bigo_def elt_set_times_def func_times)
avigad@16908
   531
  apply (rule_tac x = "%y. inverse c * x y" in exI)
avigad@16908
   532
  apply (simp add: mult_assoc [symmetric] abs_mult)
avigad@16908
   533
  apply (rule_tac x = "abs (inverse c) * ca" in exI)
avigad@16908
   534
  apply (rule allI)
avigad@16908
   535
  apply (subst mult_assoc)
avigad@16908
   536
  apply (rule mult_left_mono)
avigad@16908
   537
  apply (erule spec)
avigad@16908
   538
  apply force
avigad@16908
   539
done
avigad@16908
   540
avigad@16908
   541
lemma bigo_const_mult6 [intro]: "(%x. c) *o O(f) <= O(f)"
avigad@16908
   542
  apply (auto intro!: subsetI
avigad@16908
   543
    simp add: bigo_def elt_set_times_def func_times)
avigad@16908
   544
  apply (rule_tac x = "ca * (abs c)" in exI)
avigad@16908
   545
  apply (rule allI)
avigad@16908
   546
  apply (subgoal_tac "ca * abs(c) * abs(f x) = abs(c) * (ca * abs(f x))")
avigad@16908
   547
  apply (erule ssubst)
avigad@16908
   548
  apply (subst abs_mult)
avigad@16908
   549
  apply (rule mult_left_mono)
avigad@16908
   550
  apply (erule spec)
avigad@16908
   551
  apply simp
avigad@16908
   552
  apply(simp add: mult_ac)
avigad@16908
   553
done
avigad@16908
   554
avigad@16908
   555
lemma bigo_const_mult7 [intro]: "f =o O(g) ==> (%x. c * f x) =o O(g)"
avigad@16908
   556
proof -
avigad@16908
   557
  assume "f =o O(g)"
avigad@16908
   558
  then have "(%x. c) * f =o (%x. c) *o O(g)"
avigad@16908
   559
    by auto
avigad@16908
   560
  also have "(%x. c) * f = (%x. c * f x)"
avigad@16908
   561
    by (simp add: func_times)
avigad@16908
   562
  also have "(%x. c) *o O(g) <= O(g)"
avigad@16908
   563
    by (auto del: subsetI)
avigad@16908
   564
  finally show ?thesis .
avigad@16908
   565
qed
avigad@16908
   566
avigad@16908
   567
lemma bigo_compose1: "f =o O(g) ==> (%x. f(k x)) =o O(%x. g(k x))"
avigad@16908
   568
by (unfold bigo_def, auto)
avigad@16908
   569
avigad@16908
   570
lemma bigo_compose2: "f =o g +o O(h) ==> (%x. f(k x)) =o (%x. g(k x)) +o 
avigad@16908
   571
    O(%x. h(k x))"
avigad@16908
   572
  apply (simp only: set_minus_plus [symmetric] diff_minus func_minus
avigad@16908
   573
      func_plus)
avigad@16908
   574
  apply (erule bigo_compose1)
avigad@16908
   575
done
avigad@16908
   576
avigad@16908
   577
subsection {* Setsum *}
avigad@16908
   578
avigad@16908
   579
lemma bigo_setsum_main: "ALL x. ALL y : A x. 0 <= h x y ==> 
avigad@16908
   580
    EX c. ALL x. ALL y : A x. abs(f x y) <= c * (h x y) ==>
avigad@16908
   581
      (%x. SUM y : A x. f x y) =o O(%x. SUM y : A x. h x y)"  
avigad@16908
   582
  apply (auto simp add: bigo_def)
avigad@16908
   583
  apply (rule_tac x = "abs c" in exI)
avigad@16908
   584
  apply (subst abs_of_nonneg);back;back
avigad@16908
   585
  apply (rule setsum_nonneg)
avigad@16908
   586
  apply force
avigad@16908
   587
  apply (subst setsum_mult)
avigad@16908
   588
  apply (rule allI)
avigad@16908
   589
  apply (rule order_trans)
avigad@16908
   590
  apply (rule setsum_abs)
avigad@16908
   591
  apply (rule setsum_mono)
avigad@16908
   592
  apply (rule order_trans)
avigad@16908
   593
  apply (drule spec)+
avigad@16908
   594
  apply (drule bspec)+
avigad@16908
   595
  apply assumption+
avigad@16908
   596
  apply (drule bspec)
avigad@16908
   597
  apply assumption+
avigad@16908
   598
  apply (rule mult_right_mono) 
avigad@16908
   599
  apply (rule abs_ge_self)
avigad@16908
   600
  apply force
avigad@16908
   601
done
avigad@16908
   602
avigad@16908
   603
lemma bigo_setsum1: "ALL x y. 0 <= h x y ==> 
avigad@16908
   604
    EX c. ALL x y. abs(f x y) <= c * (h x y) ==>
avigad@16908
   605
      (%x. SUM y : A x. f x y) =o O(%x. SUM y : A x. h x y)"
avigad@16908
   606
  apply (rule bigo_setsum_main)
avigad@16908
   607
  apply force
avigad@16908
   608
  apply clarsimp
avigad@16908
   609
  apply (rule_tac x = c in exI)
avigad@16908
   610
  apply force
avigad@16908
   611
done
avigad@16908
   612
avigad@16908
   613
lemma bigo_setsum2: "ALL y. 0 <= h y ==> 
avigad@16908
   614
    EX c. ALL y. abs(f y) <= c * (h y) ==>
avigad@16908
   615
      (%x. SUM y : A x. f y) =o O(%x. SUM y : A x. h y)"
avigad@16908
   616
by (rule bigo_setsum1, auto)  
avigad@16908
   617
avigad@16908
   618
lemma bigo_setsum3: "f =o O(h) ==>
avigad@16908
   619
    (%x. SUM y : A x. (l x y) * f(k x y)) =o
avigad@16908
   620
      O(%x. SUM y : A x. abs(l x y * h(k x y)))"
avigad@16908
   621
  apply (rule bigo_setsum1)
avigad@16908
   622
  apply (rule allI)+
avigad@16908
   623
  apply (rule abs_ge_zero)
avigad@16908
   624
  apply (unfold bigo_def)
avigad@16908
   625
  apply auto
avigad@16908
   626
  apply (rule_tac x = c in exI)
avigad@16908
   627
  apply (rule allI)+
avigad@16908
   628
  apply (subst abs_mult)+
avigad@16908
   629
  apply (subst mult_left_commute)
avigad@16908
   630
  apply (rule mult_left_mono)
avigad@16908
   631
  apply (erule spec)
avigad@16908
   632
  apply (rule abs_ge_zero)
avigad@16908
   633
done
avigad@16908
   634
avigad@16908
   635
lemma bigo_setsum4: "f =o g +o O(h) ==>
avigad@16908
   636
    (%x. SUM y : A x. l x y * f(k x y)) =o
avigad@16908
   637
      (%x. SUM y : A x. l x y * g(k x y)) +o
avigad@16908
   638
        O(%x. SUM y : A x. abs(l x y * h(k x y)))"
avigad@16908
   639
  apply (rule set_minus_imp_plus)
avigad@16908
   640
  apply (subst func_diff)
avigad@16908
   641
  apply (subst setsum_subtractf [symmetric])
avigad@16908
   642
  apply (subst right_diff_distrib [symmetric])
avigad@16908
   643
  apply (rule bigo_setsum3)
avigad@16908
   644
  apply (subst func_diff [symmetric])
avigad@16908
   645
  apply (erule set_plus_imp_minus)
avigad@16908
   646
done
avigad@16908
   647
avigad@16908
   648
lemma bigo_setsum5: "f =o O(h) ==> ALL x y. 0 <= l x y ==> 
avigad@16908
   649
    ALL x. 0 <= h x ==>
avigad@16908
   650
      (%x. SUM y : A x. (l x y) * f(k x y)) =o
avigad@16908
   651
        O(%x. SUM y : A x. (l x y) * h(k x y))" 
avigad@16908
   652
  apply (subgoal_tac "(%x. SUM y : A x. (l x y) * h(k x y)) = 
avigad@16908
   653
      (%x. SUM y : A x. abs((l x y) * h(k x y)))")
avigad@16908
   654
  apply (erule ssubst)
avigad@16908
   655
  apply (erule bigo_setsum3)
avigad@16908
   656
  apply (rule ext)
avigad@16908
   657
  apply (rule setsum_cong2)
avigad@16908
   658
  apply (subst abs_of_nonneg)
avigad@16908
   659
  apply (rule mult_nonneg_nonneg)
avigad@16908
   660
  apply auto
avigad@16908
   661
done
avigad@16908
   662
avigad@16908
   663
lemma bigo_setsum6: "f =o g +o O(h) ==> ALL x y. 0 <= l x y ==>
avigad@16908
   664
    ALL x. 0 <= h x ==>
avigad@16908
   665
      (%x. SUM y : A x. (l x y) * f(k x y)) =o
avigad@16908
   666
        (%x. SUM y : A x. (l x y) * g(k x y)) +o
avigad@16908
   667
          O(%x. SUM y : A x. (l x y) * h(k x y))" 
avigad@16908
   668
  apply (rule set_minus_imp_plus)
avigad@16908
   669
  apply (subst func_diff)
avigad@16908
   670
  apply (subst setsum_subtractf [symmetric])
avigad@16908
   671
  apply (subst right_diff_distrib [symmetric])
avigad@16908
   672
  apply (rule bigo_setsum5)
avigad@16908
   673
  apply (subst func_diff [symmetric])
avigad@16908
   674
  apply (drule set_plus_imp_minus)
avigad@16908
   675
  apply auto
avigad@16908
   676
done
avigad@16908
   677
avigad@16908
   678
subsection {* Misc useful stuff *}
avigad@16908
   679
avigad@16908
   680
lemma bigo_useful_intro: "A <= O(f) ==> B <= O(f) ==>
avigad@16908
   681
  A + B <= O(f)"
avigad@16908
   682
  apply (subst bigo_plus_idemp [symmetric])
avigad@16908
   683
  apply (rule set_plus_mono2)
avigad@16908
   684
  apply assumption+
avigad@16908
   685
done
avigad@16908
   686
avigad@16908
   687
lemma bigo_useful_add: "f =o O(h) ==> g =o O(h) ==> f + g =o O(h)"
avigad@16908
   688
  apply (subst bigo_plus_idemp [symmetric])
avigad@16908
   689
  apply (rule set_plus_intro)
avigad@16908
   690
  apply assumption+
avigad@16908
   691
done
avigad@16908
   692
  
avigad@16908
   693
lemma bigo_useful_const_mult: "(c::'a::ordered_field) ~= 0 ==> 
avigad@16908
   694
    (%x. c) * f =o O(h) ==> f =o O(h)"
avigad@16908
   695
  apply (rule subsetD)
avigad@16908
   696
  apply (subgoal_tac "(%x. 1 / c) *o O(h) <= O(h)")
avigad@16908
   697
  apply assumption
avigad@16908
   698
  apply (rule bigo_const_mult6)
avigad@16908
   699
  apply (subgoal_tac "f = (%x. 1 / c) * ((%x. c) * f)")
avigad@16908
   700
  apply (erule ssubst)
avigad@16908
   701
  apply (erule set_times_intro2)
avigad@16908
   702
  apply (simp add: func_times) 
avigad@16908
   703
  apply (rule ext)
avigad@16908
   704
  apply (subst times_divide_eq_left [symmetric])
avigad@16908
   705
  apply (subst divide_self)
avigad@16908
   706
  apply (assumption, simp)
avigad@16908
   707
done
avigad@16908
   708
avigad@16908
   709
lemma bigo_fix: "(%x. f ((x::nat) + 1)) =o O(%x. h(x + 1)) ==> f 0 = 0 ==>
avigad@16908
   710
    f =o O(h)"
avigad@16908
   711
  apply (simp add: bigo_alt_def)
avigad@16908
   712
  apply auto
avigad@16908
   713
  apply (rule_tac x = c in exI)
avigad@16908
   714
  apply auto
avigad@16908
   715
  apply (case_tac "x = 0")
avigad@16908
   716
  apply simp
avigad@16908
   717
  apply (rule mult_nonneg_nonneg)
avigad@16908
   718
  apply force
avigad@16908
   719
  apply force
avigad@16908
   720
  apply (subgoal_tac "x = Suc (x - 1)")
avigad@16908
   721
  apply (erule ssubst)back
avigad@16908
   722
  apply (erule spec)
avigad@16908
   723
  apply simp
avigad@16908
   724
done
avigad@16908
   725
avigad@16908
   726
lemma bigo_fix2: 
avigad@16908
   727
    "(%x. f ((x::nat) + 1)) =o (%x. g(x + 1)) +o O(%x. h(x + 1)) ==> 
avigad@16908
   728
       f 0 = g 0 ==> f =o g +o O(h)"
avigad@16908
   729
  apply (rule set_minus_imp_plus)
avigad@16908
   730
  apply (rule bigo_fix)
avigad@16908
   731
  apply (subst func_diff)
avigad@16908
   732
  apply (subst func_diff [symmetric])
avigad@16908
   733
  apply (rule set_plus_imp_minus)
avigad@16908
   734
  apply simp
avigad@16908
   735
  apply (simp add: func_diff)
avigad@16908
   736
done
avigad@16908
   737
avigad@16908
   738
subsection {* Less than or equal to *}
avigad@16908
   739
avigad@16908
   740
constdefs 
avigad@16908
   741
  lesso :: "('a => 'b::ordered_idom) => ('a => 'b) => ('a => 'b)"
avigad@16908
   742
      (infixl "<o" 70)
avigad@16908
   743
  "f <o g == (%x. max (f x - g x) 0)"
avigad@16908
   744
avigad@16908
   745
lemma bigo_lesseq1: "f =o O(h) ==> ALL x. abs (g x) <= abs (f x) ==>
avigad@16908
   746
    g =o O(h)"
avigad@16908
   747
  apply (unfold bigo_def)
avigad@16908
   748
  apply clarsimp
avigad@16908
   749
  apply (rule_tac x = c in exI)
avigad@16908
   750
  apply (rule allI)
avigad@16908
   751
  apply (rule order_trans)
avigad@16908
   752
  apply (erule spec)+
avigad@16908
   753
done
avigad@16908
   754
avigad@16908
   755
lemma bigo_lesseq2: "f =o O(h) ==> ALL x. abs (g x) <= f x ==>
avigad@16908
   756
      g =o O(h)"
avigad@16908
   757
  apply (erule bigo_lesseq1)
avigad@16908
   758
  apply (rule allI)
avigad@16908
   759
  apply (drule_tac x = x in spec)
avigad@16908
   760
  apply (rule order_trans)
avigad@16908
   761
  apply assumption
avigad@16908
   762
  apply (rule abs_ge_self)
avigad@16908
   763
done
avigad@16908
   764
avigad@16908
   765
lemma bigo_lesseq3: "f =o O(h) ==> ALL x. 0 <= g x ==> ALL x. g x <= f x ==>
avigad@16908
   766
      g =o O(h)"
avigad@16908
   767
  apply (erule bigo_lesseq2)
avigad@16908
   768
  apply (rule allI)
avigad@16908
   769
  apply (subst abs_of_nonneg)
avigad@16908
   770
  apply (erule spec)+
avigad@16908
   771
done
avigad@16908
   772
avigad@16908
   773
lemma bigo_lesseq4: "f =o O(h) ==>
avigad@16908
   774
    ALL x. 0 <= g x ==> ALL x. g x <= abs (f x) ==>
avigad@16908
   775
      g =o O(h)"
avigad@16908
   776
  apply (erule bigo_lesseq1)
avigad@16908
   777
  apply (rule allI)
avigad@16908
   778
  apply (subst abs_of_nonneg)
avigad@16908
   779
  apply (erule spec)+
avigad@16908
   780
done
avigad@16908
   781
avigad@16908
   782
lemma bigo_lesso1: "ALL x. f x <= g x ==> f <o g =o O(h)"
avigad@16908
   783
  apply (unfold lesso_def)
avigad@16908
   784
  apply (subgoal_tac "(%x. max (f x - g x) 0) = 0")
avigad@16908
   785
  apply (erule ssubst)
avigad@16908
   786
  apply (rule bigo_zero)
avigad@16908
   787
  apply (unfold func_zero)
avigad@16908
   788
  apply (rule ext)
avigad@16908
   789
  apply (simp split: split_max)
avigad@16908
   790
done
avigad@16908
   791
avigad@16908
   792
lemma bigo_lesso2: "f =o g +o O(h) ==>
avigad@16908
   793
    ALL x. 0 <= k x ==> ALL x. k x <= f x ==>
avigad@16908
   794
      k <o g =o O(h)"
avigad@16908
   795
  apply (unfold lesso_def)
avigad@16908
   796
  apply (rule bigo_lesseq4)
avigad@16908
   797
  apply (erule set_plus_imp_minus)
avigad@16908
   798
  apply (rule allI)
avigad@16908
   799
  apply (rule le_maxI2)
avigad@16908
   800
  apply (rule allI)
avigad@16908
   801
  apply (subst func_diff)
avigad@16908
   802
  apply (case_tac "0 <= k x - g x")
avigad@16908
   803
  apply simp
avigad@16908
   804
  apply (subst abs_of_nonneg)
avigad@16908
   805
  apply (drule_tac x = x in spec)back
avigad@16908
   806
  apply (simp add: compare_rls)
avigad@16908
   807
  apply (subst diff_minus)+
avigad@16908
   808
  apply (rule add_right_mono)
avigad@16908
   809
  apply (erule spec)
avigad@16908
   810
  apply (rule order_trans) 
avigad@16908
   811
  prefer 2
avigad@16908
   812
  apply (rule abs_ge_zero)
avigad@16908
   813
  apply (simp add: compare_rls)
avigad@16908
   814
done
avigad@16908
   815
avigad@16908
   816
lemma bigo_lesso3: "f =o g +o O(h) ==>
avigad@16908
   817
    ALL x. 0 <= k x ==> ALL x. g x <= k x ==>
avigad@16908
   818
      f <o k =o O(h)"
avigad@16908
   819
  apply (unfold lesso_def)
avigad@16908
   820
  apply (rule bigo_lesseq4)
avigad@16908
   821
  apply (erule set_plus_imp_minus)
avigad@16908
   822
  apply (rule allI)
avigad@16908
   823
  apply (rule le_maxI2)
avigad@16908
   824
  apply (rule allI)
avigad@16908
   825
  apply (subst func_diff)
avigad@16908
   826
  apply (case_tac "0 <= f x - k x")
avigad@16908
   827
  apply simp
avigad@16908
   828
  apply (subst abs_of_nonneg)
avigad@16908
   829
  apply (drule_tac x = x in spec)back
avigad@16908
   830
  apply (simp add: compare_rls)
avigad@16908
   831
  apply (subst diff_minus)+
avigad@16908
   832
  apply (rule add_left_mono)
avigad@16908
   833
  apply (rule le_imp_neg_le)
avigad@16908
   834
  apply (erule spec)
avigad@16908
   835
  apply (rule order_trans) 
avigad@16908
   836
  prefer 2
avigad@16908
   837
  apply (rule abs_ge_zero)
avigad@16908
   838
  apply (simp add: compare_rls)
avigad@16908
   839
done
avigad@16908
   840
avigad@16908
   841
lemma bigo_lesso4: "f <o g =o O(k::'a=>'b::ordered_field) ==>
avigad@16908
   842
    g =o h +o O(k) ==> f <o h =o O(k)"
avigad@16908
   843
  apply (unfold lesso_def)
avigad@16908
   844
  apply (drule set_plus_imp_minus)
avigad@16908
   845
  apply (drule bigo_abs5)back
avigad@16908
   846
  apply (simp add: func_diff)
avigad@16908
   847
  apply (drule bigo_useful_add)
avigad@16908
   848
  apply assumption
avigad@16908
   849
  apply (erule bigo_lesseq2)back
avigad@16908
   850
  apply (rule allI)
avigad@16908
   851
  apply (auto simp add: func_plus func_diff compare_rls 
avigad@16908
   852
    split: split_max abs_split)
avigad@16908
   853
done
avigad@16908
   854
avigad@16908
   855
lemma bigo_lesso5: "f <o g =o O(h) ==>
avigad@16908
   856
    EX C. ALL x. f x <= g x + C * abs(h x)"
avigad@16908
   857
  apply (simp only: lesso_def bigo_alt_def)
avigad@16908
   858
  apply clarsimp
avigad@16908
   859
  apply (rule_tac x = c in exI)
avigad@16908
   860
  apply (rule allI)
avigad@16908
   861
  apply (drule_tac x = x in spec)
avigad@16908
   862
  apply (subgoal_tac "abs(max (f x - g x) 0) = max (f x - g x) 0")
avigad@16908
   863
  apply (clarsimp simp add: compare_rls add_ac) 
avigad@16908
   864
  apply (rule abs_of_nonneg)
avigad@16908
   865
  apply (rule le_maxI2)
avigad@16908
   866
done
avigad@16908
   867
avigad@16908
   868
lemma lesso_add: "f <o g =o O(h) ==>
avigad@16908
   869
      k <o l =o O(h) ==> (f + k) <o (g + l) =o O(h)"
avigad@16908
   870
  apply (unfold lesso_def)
avigad@16908
   871
  apply (rule bigo_lesseq3)
avigad@16908
   872
  apply (erule bigo_useful_add)
avigad@16908
   873
  apply assumption
avigad@16908
   874
  apply (force split: split_max)
avigad@16908
   875
  apply (auto split: split_max simp add: func_plus)
avigad@16908
   876
done
avigad@16908
   877
avigad@16908
   878
(* 
avigad@16908
   879
These last two lemmas require the HOL-Complex library.
avigad@16908
   880
avigad@16908
   881
lemma bigo_LIMSEQ1: "f =o O(g) ==> g ----> 0 ==> f ----> 0"
avigad@16908
   882
  apply (simp add: LIMSEQ_def bigo_alt_def)
avigad@16908
   883
  apply clarify
avigad@16908
   884
  apply (drule_tac x = "r / c" in spec)
avigad@16908
   885
  apply (drule mp)
avigad@16908
   886
  apply (erule divide_pos_pos)
avigad@16908
   887
  apply assumption
avigad@16908
   888
  apply clarify
avigad@16908
   889
  apply (rule_tac x = no in exI)
avigad@16908
   890
  apply (rule allI)
avigad@16908
   891
  apply (drule_tac x = n in spec)+
avigad@16908
   892
  apply (rule impI)
avigad@16908
   893
  apply (drule mp)
avigad@16908
   894
  apply assumption
avigad@16908
   895
  apply (rule order_le_less_trans)
avigad@16908
   896
  apply assumption
avigad@16908
   897
  apply (rule order_less_le_trans)
avigad@16908
   898
  apply (subgoal_tac "c * abs(g n) < c * (r / c)")
avigad@16908
   899
  apply assumption
avigad@16908
   900
  apply (erule mult_strict_left_mono)
avigad@16908
   901
  apply assumption
avigad@16908
   902
  apply simp
avigad@16908
   903
done
avigad@16908
   904
avigad@16908
   905
lemma bigo_LIMSEQ2: "f =o g +o O(h) ==> h ----> 0 ==> f ----> a 
avigad@16908
   906
    ==> g ----> a"
avigad@16908
   907
  apply (drule set_plus_imp_minus)
avigad@16908
   908
  apply (drule bigo_LIMSEQ1)
avigad@16908
   909
  apply assumption
avigad@16908
   910
  apply (simp only: func_diff)
avigad@16908
   911
  apply (erule LIMSEQ_diff_approach_zero2)
avigad@16908
   912
  apply assumption
avigad@16908
   913
done
avigad@16908
   914
avigad@16908
   915
*)
avigad@16908
   916
avigad@16908
   917
end