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