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