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