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