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