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