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