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