src/HOL/Metis_Examples/Big_O.thy
author huffman
Thu Oct 27 07:46:57 2011 +0200 (2011-10-27)
changeset 45270 d5b5c9259afd
parent 43197 c71657bbdbc0
child 45504 cad35ed6effa
permissions -rw-r--r--
fix bug in cancel_factor simprocs so they will work on goals like 'x * y < x * z' where the common term is already on the left
     1 (*  Title:      HOL/Metis_Examples/Big_O.thy
     2     Author:     Lawrence C. Paulson, Cambridge University Computer Laboratory
     3     Author:     Jasmin Blanchette, TU Muenchen
     4 
     5 Metis example featuring the Big O notation.
     6 *)
     7 
     8 header {* Metis Example Featuring the Big O Notation *}
     9 
    10 theory Big_O
    11 imports
    12   "~~/src/HOL/Decision_Procs/Dense_Linear_Order"
    13   Main
    14   "~~/src/HOL/Library/Function_Algebras"
    15   "~~/src/HOL/Library/Set_Algebras"
    16 begin
    17 
    18 declare [[metis_new_skolemizer]]
    19 
    20 subsection {* Definitions *}
    21 
    22 definition bigo :: "('a => 'b::linordered_idom) => ('a => 'b) set"    ("(1O'(_'))") where
    23   "O(f::('a => 'b)) ==   {h. EX c. ALL x. abs (h x) <= c * abs (f x)}"
    24 
    25 declare [[ sledgehammer_problem_prefix = "BigO__bigo_pos_const" ]]
    26 lemma bigo_pos_const: "(EX (c::'a::linordered_idom).
    27     ALL x. (abs (h x)) <= (c * (abs (f x))))
    28       = (EX c. 0 < c & (ALL x. (abs(h x)) <= (c * (abs (f x)))))"
    29   apply auto
    30   apply (case_tac "c = 0", simp)
    31   apply (rule_tac x = "1" in exI, simp)
    32   apply (rule_tac x = "abs c" in exI, auto)
    33   apply (metis abs_ge_zero abs_of_nonneg Orderings.xt1(6) abs_mult)
    34   done
    35 
    36 (*** Now various verions with an increasing shrink factor ***)
    37 
    38 sledgehammer_params [isar_proof, isar_shrink_factor = 1]
    39 
    40 lemma (*bigo_pos_const:*) "(EX (c::'a::linordered_idom).
    41     ALL x. (abs (h x)) <= (c * (abs (f x))))
    42       = (EX c. 0 < c & (ALL x. (abs(h x)) <= (c * (abs (f x)))))"
    43   apply auto
    44   apply (case_tac "c = 0", simp)
    45   apply (rule_tac x = "1" in exI, simp)
    46   apply (rule_tac x = "abs c" in exI, auto)
    47 proof -
    48   fix c :: 'a and x :: 'b
    49   assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
    50   have F1: "\<forall>x\<^isub>1\<Colon>'a\<Colon>linordered_idom. 0 \<le> \<bar>x\<^isub>1\<bar>" by (metis abs_ge_zero)
    51   have F2: "\<forall>x\<^isub>1\<Colon>'a\<Colon>linordered_idom. 1 * x\<^isub>1 = x\<^isub>1" by (metis mult_1)
    52   have F3: "\<forall>x\<^isub>1 x\<^isub>3. x\<^isub>3 \<le> \<bar>h x\<^isub>1\<bar> \<longrightarrow> x\<^isub>3 \<le> c * \<bar>f x\<^isub>1\<bar>" by (metis A1 order_trans)
    53   have F4: "\<forall>x\<^isub>2 x\<^isub>3\<Colon>'a\<Colon>linordered_idom. \<bar>x\<^isub>3\<bar> * \<bar>x\<^isub>2\<bar> = \<bar>x\<^isub>3 * x\<^isub>2\<bar>"
    54     by (metis abs_mult)
    55   have F5: "\<forall>x\<^isub>3 x\<^isub>1\<Colon>'a\<Colon>linordered_idom. 0 \<le> x\<^isub>1 \<longrightarrow> \<bar>x\<^isub>3 * x\<^isub>1\<bar> = \<bar>x\<^isub>3\<bar> * x\<^isub>1"
    56     by (metis abs_mult_pos)
    57   hence "\<forall>x\<^isub>1\<ge>0. \<bar>x\<^isub>1\<Colon>'a\<Colon>linordered_idom\<bar> = \<bar>1\<bar> * x\<^isub>1" by (metis F2)
    58   hence "\<forall>x\<^isub>1\<ge>0. \<bar>x\<^isub>1\<Colon>'a\<Colon>linordered_idom\<bar> = x\<^isub>1" by (metis F2 abs_one)
    59   hence "\<forall>x\<^isub>3. 0 \<le> \<bar>h x\<^isub>3\<bar> \<longrightarrow> \<bar>c * \<bar>f x\<^isub>3\<bar>\<bar> = c * \<bar>f x\<^isub>3\<bar>" by (metis F3)
    60   hence "\<forall>x\<^isub>3. \<bar>c * \<bar>f x\<^isub>3\<bar>\<bar> = c * \<bar>f x\<^isub>3\<bar>" by (metis F1)
    61   hence "\<forall>x\<^isub>3. (0\<Colon>'a) \<le> \<bar>f x\<^isub>3\<bar> \<longrightarrow> c * \<bar>f x\<^isub>3\<bar> = \<bar>c\<bar> * \<bar>f x\<^isub>3\<bar>" by (metis F5)
    62   hence "\<forall>x\<^isub>3. (0\<Colon>'a) \<le> \<bar>f x\<^isub>3\<bar> \<longrightarrow> c * \<bar>f x\<^isub>3\<bar> = \<bar>c * f x\<^isub>3\<bar>" by (metis F4)
    63   hence "\<forall>x\<^isub>3. c * \<bar>f x\<^isub>3\<bar> = \<bar>c * f x\<^isub>3\<bar>" by (metis F1)
    64   hence "\<bar>h x\<bar> \<le> \<bar>c * f x\<bar>" by (metis A1)
    65   thus "\<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>" by (metis F4)
    66 qed
    67 
    68 sledgehammer_params [isar_proof, isar_shrink_factor = 2]
    69 
    70 lemma (*bigo_pos_const:*) "(EX (c::'a::linordered_idom).
    71     ALL x. (abs (h x)) <= (c * (abs (f x))))
    72       = (EX c. 0 < c & (ALL x. (abs(h x)) <= (c * (abs (f x)))))"
    73   apply auto
    74   apply (case_tac "c = 0", simp)
    75   apply (rule_tac x = "1" in exI, simp)
    76   apply (rule_tac x = "abs c" in exI, auto)
    77 proof -
    78   fix c :: 'a and x :: 'b
    79   assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
    80   have F1: "\<forall>x\<^isub>1\<Colon>'a\<Colon>linordered_idom. 1 * x\<^isub>1 = x\<^isub>1" by (metis mult_1)
    81   have F2: "\<forall>x\<^isub>2 x\<^isub>3\<Colon>'a\<Colon>linordered_idom. \<bar>x\<^isub>3\<bar> * \<bar>x\<^isub>2\<bar> = \<bar>x\<^isub>3 * x\<^isub>2\<bar>"
    82     by (metis abs_mult)
    83   have "\<forall>x\<^isub>1\<ge>0. \<bar>x\<^isub>1\<Colon>'a\<Colon>linordered_idom\<bar> = x\<^isub>1" by (metis F1 abs_mult_pos abs_one)
    84   hence "\<forall>x\<^isub>3. \<bar>c * \<bar>f x\<^isub>3\<bar>\<bar> = c * \<bar>f x\<^isub>3\<bar>" by (metis A1 abs_ge_zero order_trans)
    85   hence "\<forall>x\<^isub>3. 0 \<le> \<bar>f x\<^isub>3\<bar> \<longrightarrow> c * \<bar>f x\<^isub>3\<bar> = \<bar>c * f x\<^isub>3\<bar>" by (metis F2 abs_mult_pos)
    86   hence "\<bar>h x\<bar> \<le> \<bar>c * f x\<bar>" by (metis A1 abs_ge_zero)
    87   thus "\<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>" by (metis F2)
    88 qed
    89 
    90 sledgehammer_params [isar_proof, isar_shrink_factor = 3]
    91 
    92 lemma (*bigo_pos_const:*) "(EX (c::'a::linordered_idom).
    93     ALL x. (abs (h x)) <= (c * (abs (f x))))
    94       = (EX c. 0 < c & (ALL x. (abs(h x)) <= (c * (abs (f x)))))"
    95   apply auto
    96   apply (case_tac "c = 0", simp)
    97   apply (rule_tac x = "1" in exI, simp)
    98   apply (rule_tac x = "abs c" in exI, auto)
    99 proof -
   100   fix c :: 'a and x :: 'b
   101   assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
   102   have F1: "\<forall>x\<^isub>1\<Colon>'a\<Colon>linordered_idom. 1 * x\<^isub>1 = x\<^isub>1" by (metis mult_1)
   103   have F2: "\<forall>x\<^isub>3 x\<^isub>1\<Colon>'a\<Colon>linordered_idom. 0 \<le> x\<^isub>1 \<longrightarrow> \<bar>x\<^isub>3 * x\<^isub>1\<bar> = \<bar>x\<^isub>3\<bar> * x\<^isub>1" by (metis abs_mult_pos)
   104   hence "\<forall>x\<^isub>1\<ge>0. \<bar>x\<^isub>1\<Colon>'a\<Colon>linordered_idom\<bar> = x\<^isub>1" by (metis F1 abs_one)
   105   hence "\<forall>x\<^isub>3. 0 \<le> \<bar>f x\<^isub>3\<bar> \<longrightarrow> c * \<bar>f x\<^isub>3\<bar> = \<bar>c\<bar> * \<bar>f x\<^isub>3\<bar>" by (metis F2 A1 abs_ge_zero order_trans)
   106   thus "\<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>" by (metis A1 abs_mult abs_ge_zero)
   107 qed
   108 
   109 sledgehammer_params [isar_proof, isar_shrink_factor = 4]
   110 
   111 lemma (*bigo_pos_const:*) "(EX (c::'a::linordered_idom).
   112     ALL x. (abs (h x)) <= (c * (abs (f x))))
   113       = (EX c. 0 < c & (ALL x. (abs(h x)) <= (c * (abs (f x)))))"
   114   apply auto
   115   apply (case_tac "c = 0", simp)
   116   apply (rule_tac x = "1" in exI, simp)
   117   apply (rule_tac x = "abs c" in exI, auto)
   118 proof -
   119   fix c :: 'a and x :: 'b
   120   assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
   121   have "\<forall>x\<^isub>1\<Colon>'a\<Colon>linordered_idom. 1 * x\<^isub>1 = x\<^isub>1" by (metis mult_1)
   122   hence "\<forall>x\<^isub>3. \<bar>c * \<bar>f x\<^isub>3\<bar>\<bar> = c * \<bar>f x\<^isub>3\<bar>"
   123     by (metis A1 abs_ge_zero order_trans abs_mult_pos abs_one)
   124   hence "\<bar>h x\<bar> \<le> \<bar>c * f x\<bar>" by (metis A1 abs_ge_zero abs_mult_pos abs_mult)
   125   thus "\<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>" by (metis abs_mult)
   126 qed
   127 
   128 sledgehammer_params [isar_proof, isar_shrink_factor = 1]
   129 
   130 lemma bigo_alt_def: "O(f) =
   131     {h. EX c. (0 < c & (ALL x. abs (h x) <= c * abs (f x)))}"
   132 by (auto simp add: bigo_def bigo_pos_const)
   133 
   134 declare [[ sledgehammer_problem_prefix = "BigO__bigo_elt_subset" ]]
   135 lemma bigo_elt_subset [intro]: "f : O(g) ==> O(f) <= O(g)"
   136   apply (auto simp add: bigo_alt_def)
   137   apply (rule_tac x = "ca * c" in exI)
   138   apply (rule conjI)
   139   apply (rule mult_pos_pos)
   140   apply (assumption)+
   141 (*sledgehammer*)
   142   apply (rule allI)
   143   apply (drule_tac x = "xa" in spec)+
   144   apply (subgoal_tac "ca * abs(f xa) <= ca * (c * abs(g xa))")
   145   apply (erule order_trans)
   146   apply (simp add: mult_ac)
   147   apply (rule mult_left_mono, assumption)
   148   apply (rule order_less_imp_le, assumption)
   149 done
   150 
   151 
   152 declare [[ sledgehammer_problem_prefix = "BigO__bigo_refl" ]]
   153 lemma bigo_refl [intro]: "f : O(f)"
   154 apply (auto simp add: bigo_def)
   155 by (metis mult_1 order_refl)
   156 
   157 declare [[ sledgehammer_problem_prefix = "BigO__bigo_zero" ]]
   158 lemma bigo_zero: "0 : O(g)"
   159 apply (auto simp add: bigo_def func_zero)
   160 by (metis mult_zero_left order_refl)
   161 
   162 lemma bigo_zero2: "O(%x.0) = {%x.0}"
   163   by (auto simp add: bigo_def)
   164 
   165 lemma bigo_plus_self_subset [intro]:
   166   "O(f) \<oplus> O(f) <= O(f)"
   167   apply (auto simp add: bigo_alt_def set_plus_def)
   168   apply (rule_tac x = "c + ca" in exI)
   169   apply auto
   170   apply (simp add: ring_distribs func_plus)
   171   apply (blast intro:order_trans abs_triangle_ineq add_mono elim:)
   172 done
   173 
   174 lemma bigo_plus_idemp [simp]: "O(f) \<oplus> O(f) = O(f)"
   175   apply (rule equalityI)
   176   apply (rule bigo_plus_self_subset)
   177   apply (rule set_zero_plus2)
   178   apply (rule bigo_zero)
   179 done
   180 
   181 lemma bigo_plus_subset [intro]: "O(f + g) <= O(f) \<oplus> O(g)"
   182   apply (rule subsetI)
   183   apply (auto simp add: bigo_def bigo_pos_const func_plus set_plus_def)
   184   apply (subst bigo_pos_const [symmetric])+
   185   apply (rule_tac x =
   186     "%n. if abs (g n) <= (abs (f n)) then x n else 0" in exI)
   187   apply (rule conjI)
   188   apply (rule_tac x = "c + c" in exI)
   189   apply (clarsimp)
   190   apply (auto)
   191   apply (subgoal_tac "c * abs (f xa + g xa) <= (c + c) * abs (f xa)")
   192   apply (erule_tac x = xa in allE)
   193   apply (erule order_trans)
   194   apply (simp)
   195   apply (subgoal_tac "c * abs (f xa + g xa) <= c * (abs (f xa) + abs (g xa))")
   196   apply (erule order_trans)
   197   apply (simp add: ring_distribs)
   198   apply (rule mult_left_mono)
   199   apply (simp add: abs_triangle_ineq)
   200   apply (simp add: order_less_le)
   201   apply (rule mult_nonneg_nonneg)
   202   apply (rule add_nonneg_nonneg)
   203   apply auto
   204   apply (rule_tac x = "%n. if (abs (f n)) <  abs (g n) then x n else 0"
   205      in exI)
   206   apply (rule conjI)
   207   apply (rule_tac x = "c + c" in exI)
   208   apply auto
   209   apply (subgoal_tac "c * abs (f xa + g xa) <= (c + c) * abs (g xa)")
   210   apply (erule_tac x = xa in allE)
   211   apply (erule order_trans)
   212   apply (simp)
   213   apply (subgoal_tac "c * abs (f xa + g xa) <= c * (abs (f xa) + abs (g xa))")
   214   apply (erule order_trans)
   215   apply (simp add: ring_distribs)
   216   apply (rule mult_left_mono)
   217   apply (rule abs_triangle_ineq)
   218   apply (simp add: order_less_le)
   219 apply (metis abs_not_less_zero double_less_0_iff less_not_permute linorder_not_less mult_less_0_iff)
   220   apply (rule ext)
   221   apply (auto simp add: if_splits linorder_not_le)
   222 done
   223 
   224 lemma bigo_plus_subset2 [intro]: "A <= O(f) ==> B <= O(f) ==> A \<oplus> B <= O(f)"
   225   apply (subgoal_tac "A \<oplus> B <= O(f) \<oplus> O(f)")
   226   apply (erule order_trans)
   227   apply simp
   228   apply (auto del: subsetI simp del: bigo_plus_idemp)
   229 done
   230 
   231 declare [[ sledgehammer_problem_prefix = "BigO__bigo_plus_eq" ]]
   232 lemma bigo_plus_eq: "ALL x. 0 <= f x ==> ALL x. 0 <= g x ==>
   233   O(f + g) = O(f) \<oplus> O(g)"
   234   apply (rule equalityI)
   235   apply (rule bigo_plus_subset)
   236   apply (simp add: bigo_alt_def set_plus_def func_plus)
   237   apply clarify
   238 (*sledgehammer*)
   239   apply (rule_tac x = "max c ca" in exI)
   240   apply (rule conjI)
   241    apply (metis Orderings.less_max_iff_disj)
   242   apply clarify
   243   apply (drule_tac x = "xa" in spec)+
   244   apply (subgoal_tac "0 <= f xa + g xa")
   245   apply (simp add: ring_distribs)
   246   apply (subgoal_tac "abs(a xa + b xa) <= abs(a xa) + abs(b xa)")
   247   apply (subgoal_tac "abs(a xa) + abs(b xa) <=
   248       max c ca * f xa + max c ca * g xa")
   249   apply (blast intro: order_trans)
   250   defer 1
   251   apply (rule abs_triangle_ineq)
   252   apply (metis add_nonneg_nonneg)
   253   apply (rule add_mono)
   254 using [[ sledgehammer_problem_prefix = "BigO__bigo_plus_eq_simpler" ]]
   255   apply (metis le_maxI2 linorder_linear min_max.sup_absorb1 mult_right_mono xt1(6))
   256   apply (metis le_maxI2 linorder_not_le mult_le_cancel_right order_trans)
   257 done
   258 
   259 declare [[ sledgehammer_problem_prefix = "BigO__bigo_bounded_alt" ]]
   260 lemma bigo_bounded_alt: "ALL x. 0 <= f x ==> ALL x. f x <= c * g x ==>
   261     f : O(g)"
   262   apply (auto simp add: bigo_def)
   263 (* Version 1: one-line proof *)
   264   apply (metis abs_le_D1 linorder_class.not_less  order_less_le  Orderings.xt1(12)  abs_mult)
   265   done
   266 
   267 lemma (*bigo_bounded_alt:*) "ALL x. 0 <= f x ==> ALL x. f x <= c * g x ==>
   268     f : O(g)"
   269 apply (auto simp add: bigo_def)
   270 (* Version 2: structured proof *)
   271 proof -
   272   assume "\<forall>x. f x \<le> c * g x"
   273   thus "\<exists>c. \<forall>x. f x \<le> c * \<bar>g x\<bar>" by (metis abs_mult abs_ge_self order_trans)
   274 qed
   275 
   276 text{*So here is the easier (and more natural) problem using transitivity*}
   277 declare [[ sledgehammer_problem_prefix = "BigO__bigo_bounded_alt_trans" ]]
   278 lemma "ALL x. 0 <= f x ==> ALL x. f x <= c * g x ==> f : O(g)"
   279 apply (auto simp add: bigo_def)
   280 (* Version 1: one-line proof *)
   281 by (metis abs_ge_self abs_mult order_trans)
   282 
   283 text{*So here is the easier (and more natural) problem using transitivity*}
   284 declare [[ sledgehammer_problem_prefix = "BigO__bigo_bounded_alt_trans" ]]
   285 lemma "ALL x. 0 <= f x ==> ALL x. f x <= c * g x ==> f : O(g)"
   286   apply (auto simp add: bigo_def)
   287 (* Version 2: structured proof *)
   288 proof -
   289   assume "\<forall>x. f x \<le> c * g x"
   290   thus "\<exists>c. \<forall>x. f x \<le> c * \<bar>g x\<bar>" by (metis abs_mult abs_ge_self order_trans)
   291 qed
   292 
   293 lemma bigo_bounded: "ALL x. 0 <= f x ==> ALL x. f x <= g x ==>
   294     f : O(g)"
   295   apply (erule bigo_bounded_alt [of f 1 g])
   296   apply simp
   297 done
   298 
   299 declare [[ sledgehammer_problem_prefix = "BigO__bigo_bounded2" ]]
   300 lemma bigo_bounded2: "ALL x. lb x <= f x ==> ALL x. f x <= lb x + g x ==>
   301     f : lb +o O(g)"
   302 apply (rule set_minus_imp_plus)
   303 apply (rule bigo_bounded)
   304  apply (auto simp add: diff_minus fun_Compl_def func_plus)
   305  prefer 2
   306  apply (drule_tac x = x in spec)+
   307  apply (metis add_right_mono add_commute diff_add_cancel diff_minus_eq_add le_less order_trans)
   308 proof -
   309   fix x :: 'a
   310   assume "\<forall>x. lb x \<le> f x"
   311   thus "(0\<Colon>'b) \<le> f x + - lb x" by (metis not_leE diff_minus less_iff_diff_less_0 less_le_not_le)
   312 qed
   313 
   314 declare [[ sledgehammer_problem_prefix = "BigO__bigo_abs" ]]
   315 lemma bigo_abs: "(%x. abs(f x)) =o O(f)"
   316 apply (unfold bigo_def)
   317 apply auto
   318 by (metis mult_1 order_refl)
   319 
   320 declare [[ sledgehammer_problem_prefix = "BigO__bigo_abs2" ]]
   321 lemma bigo_abs2: "f =o O(%x. abs(f x))"
   322 apply (unfold bigo_def)
   323 apply auto
   324 by (metis mult_1 order_refl)
   325 
   326 lemma bigo_abs3: "O(f) = O(%x. abs(f x))"
   327 proof -
   328   have F1: "\<forall>v u. u \<in> O(v) \<longrightarrow> O(u) \<subseteq> O(v)" by (metis bigo_elt_subset)
   329   have F2: "\<forall>u. (\<lambda>R. \<bar>u R\<bar>) \<in> O(u)" by (metis bigo_abs)
   330   have "\<forall>u. u \<in> O(\<lambda>R. \<bar>u R\<bar>)" by (metis bigo_abs2)
   331   thus "O(f) = O(\<lambda>x. \<bar>f x\<bar>)" using F1 F2 by auto
   332 qed
   333 
   334 lemma bigo_abs4: "f =o g +o O(h) ==>
   335     (%x. abs (f x)) =o (%x. abs (g x)) +o O(h)"
   336   apply (drule set_plus_imp_minus)
   337   apply (rule set_minus_imp_plus)
   338   apply (subst fun_diff_def)
   339 proof -
   340   assume a: "f - g : O(h)"
   341   have "(%x. abs (f x) - abs (g x)) =o O(%x. abs(abs (f x) - abs (g x)))"
   342     by (rule bigo_abs2)
   343   also have "... <= O(%x. abs (f x - g x))"
   344     apply (rule bigo_elt_subset)
   345     apply (rule bigo_bounded)
   346     apply force
   347     apply (rule allI)
   348     apply (rule abs_triangle_ineq3)
   349     done
   350   also have "... <= O(f - g)"
   351     apply (rule bigo_elt_subset)
   352     apply (subst fun_diff_def)
   353     apply (rule bigo_abs)
   354     done
   355   also have "... <= O(h)"
   356     using a by (rule bigo_elt_subset)
   357   finally show "(%x. abs (f x) - abs (g x)) : O(h)".
   358 qed
   359 
   360 lemma bigo_abs5: "f =o O(g) ==> (%x. abs(f x)) =o O(g)"
   361 by (unfold bigo_def, auto)
   362 
   363 lemma bigo_elt_subset2 [intro]: "f : g +o O(h) ==> O(f) <= O(g) \<oplus> O(h)"
   364 proof -
   365   assume "f : g +o O(h)"
   366   also have "... <= O(g) \<oplus> O(h)"
   367     by (auto del: subsetI)
   368   also have "... = O(%x. abs(g x)) \<oplus> O(%x. abs(h x))"
   369     apply (subst bigo_abs3 [symmetric])+
   370     apply (rule refl)
   371     done
   372   also have "... = O((%x. abs(g x)) + (%x. abs(h x)))"
   373     by (rule bigo_plus_eq [symmetric], auto)
   374   finally have "f : ...".
   375   then have "O(f) <= ..."
   376     by (elim bigo_elt_subset)
   377   also have "... = O(%x. abs(g x)) \<oplus> O(%x. abs(h x))"
   378     by (rule bigo_plus_eq, auto)
   379   finally show ?thesis
   380     by (simp add: bigo_abs3 [symmetric])
   381 qed
   382 
   383 declare [[ sledgehammer_problem_prefix = "BigO__bigo_mult" ]]
   384 lemma bigo_mult [intro]: "O(f)\<otimes>O(g) <= O(f * g)"
   385   apply (rule subsetI)
   386   apply (subst bigo_def)
   387   apply (auto simp del: abs_mult mult_ac
   388               simp add: bigo_alt_def set_times_def func_times)
   389 (*sledgehammer*)
   390   apply (rule_tac x = "c * ca" in exI)
   391   apply(rule allI)
   392   apply(erule_tac x = x in allE)+
   393   apply(subgoal_tac "c * ca * abs(f x * g x) =
   394       (c * abs(f x)) * (ca * abs(g x))")
   395 using [[ sledgehammer_problem_prefix = "BigO__bigo_mult_simpler" ]]
   396 prefer 2
   397 apply (metis mult_assoc mult_left_commute
   398   abs_of_pos mult_left_commute
   399   abs_mult mult_pos_pos)
   400   apply (erule ssubst)
   401   apply (subst abs_mult)
   402 (* not quite as hard as BigO__bigo_mult_simpler_1 (a hard problem!) since
   403    abs_mult has just been done *)
   404 by (metis abs_ge_zero mult_mono')
   405 
   406 declare [[ sledgehammer_problem_prefix = "BigO__bigo_mult2" ]]
   407 lemma bigo_mult2 [intro]: "f *o O(g) <= O(f * g)"
   408   apply (auto simp add: bigo_def elt_set_times_def func_times abs_mult)
   409 (*sledgehammer*)
   410   apply (rule_tac x = c in exI)
   411   apply clarify
   412   apply (drule_tac x = x in spec)
   413 using [[ sledgehammer_problem_prefix = "BigO__bigo_mult2_simpler" ]]
   414 (*sledgehammer [no luck]*)
   415   apply (subgoal_tac "abs(f x) * abs(b x) <= abs(f x) * (c * abs(g x))")
   416   apply (simp add: mult_ac)
   417   apply (rule mult_left_mono, assumption)
   418   apply (rule abs_ge_zero)
   419 done
   420 
   421 declare [[ sledgehammer_problem_prefix = "BigO__bigo_mult3" ]]
   422 lemma bigo_mult3: "f : O(h) ==> g : O(j) ==> f * g : O(h * j)"
   423 by (metis bigo_mult set_rev_mp set_times_intro)
   424 
   425 declare [[ sledgehammer_problem_prefix = "BigO__bigo_mult4" ]]
   426 lemma bigo_mult4 [intro]:"f : k +o O(h) ==> g * f : (g * k) +o O(g * h)"
   427 by (metis bigo_mult2 set_plus_mono_b set_times_intro2 set_times_plus_distrib)
   428 
   429 
   430 lemma bigo_mult5: "ALL x. f x ~= 0 ==>
   431     O(f * g) <= (f::'a => ('b::linordered_field)) *o O(g)"
   432 proof -
   433   assume a: "ALL x. f x ~= 0"
   434   show "O(f * g) <= f *o O(g)"
   435   proof
   436     fix h
   437     assume h: "h : O(f * g)"
   438     then have "(%x. 1 / (f x)) * h : (%x. 1 / f x) *o O(f * g)"
   439       by auto
   440     also have "... <= O((%x. 1 / f x) * (f * g))"
   441       by (rule bigo_mult2)
   442     also have "(%x. 1 / f x) * (f * g) = g"
   443       apply (simp add: func_times)
   444       apply (rule ext)
   445       apply (simp add: a h nonzero_divide_eq_eq mult_ac)
   446       done
   447     finally have "(%x. (1::'b) / f x) * h : O(g)".
   448     then have "f * ((%x. (1::'b) / f x) * h) : f *o O(g)"
   449       by auto
   450     also have "f * ((%x. (1::'b) / f x) * h) = h"
   451       apply (simp add: func_times)
   452       apply (rule ext)
   453       apply (simp add: a h nonzero_divide_eq_eq mult_ac)
   454       done
   455     finally show "h : f *o O(g)".
   456   qed
   457 qed
   458 
   459 declare [[ sledgehammer_problem_prefix = "BigO__bigo_mult6" ]]
   460 lemma bigo_mult6: "ALL x. f x ~= 0 ==>
   461     O(f * g) = (f::'a => ('b::linordered_field)) *o O(g)"
   462 by (metis bigo_mult2 bigo_mult5 order_antisym)
   463 
   464 (*proof requires relaxing relevance: 2007-01-25*)
   465 declare [[ sledgehammer_problem_prefix = "BigO__bigo_mult7" ]]
   466   declare bigo_mult6 [simp]
   467 lemma bigo_mult7: "ALL x. f x ~= 0 ==>
   468     O(f * g) <= O(f::'a => ('b::linordered_field)) \<otimes> O(g)"
   469 (*sledgehammer*)
   470   apply (subst bigo_mult6)
   471   apply assumption
   472   apply (rule set_times_mono3)
   473   apply (rule bigo_refl)
   474 done
   475   declare bigo_mult6 [simp del]
   476 
   477 declare [[ sledgehammer_problem_prefix = "BigO__bigo_mult8" ]]
   478   declare bigo_mult7[intro!]
   479 lemma bigo_mult8: "ALL x. f x ~= 0 ==>
   480     O(f * g) = O(f::'a => ('b::linordered_field)) \<otimes> O(g)"
   481 by (metis bigo_mult bigo_mult7 order_antisym_conv)
   482 
   483 lemma bigo_minus [intro]: "f : O(g) ==> - f : O(g)"
   484   by (auto simp add: bigo_def fun_Compl_def)
   485 
   486 lemma bigo_minus2: "f : g +o O(h) ==> -f : -g +o O(h)"
   487   apply (rule set_minus_imp_plus)
   488   apply (drule set_plus_imp_minus)
   489   apply (drule bigo_minus)
   490   apply (simp add: diff_minus)
   491 done
   492 
   493 lemma bigo_minus3: "O(-f) = O(f)"
   494   by (auto simp add: bigo_def fun_Compl_def abs_minus_cancel)
   495 
   496 lemma bigo_plus_absorb_lemma1: "f : O(g) ==> f +o O(g) <= O(g)"
   497 proof -
   498   assume a: "f : O(g)"
   499   show "f +o O(g) <= O(g)"
   500   proof -
   501     have "f : O(f)" by auto
   502     then have "f +o O(g) <= O(f) \<oplus> O(g)"
   503       by (auto del: subsetI)
   504     also have "... <= O(g) \<oplus> O(g)"
   505     proof -
   506       from a have "O(f) <= O(g)" by (auto del: subsetI)
   507       thus ?thesis by (auto del: subsetI)
   508     qed
   509     also have "... <= O(g)" by (simp add: bigo_plus_idemp)
   510     finally show ?thesis .
   511   qed
   512 qed
   513 
   514 lemma bigo_plus_absorb_lemma2: "f : O(g) ==> O(g) <= f +o O(g)"
   515 proof -
   516   assume a: "f : O(g)"
   517   show "O(g) <= f +o O(g)"
   518   proof -
   519     from a have "-f : O(g)" by auto
   520     then have "-f +o O(g) <= O(g)" by (elim bigo_plus_absorb_lemma1)
   521     then have "f +o (-f +o O(g)) <= f +o O(g)" by auto
   522     also have "f +o (-f +o O(g)) = O(g)"
   523       by (simp add: set_plus_rearranges)
   524     finally show ?thesis .
   525   qed
   526 qed
   527 
   528 declare [[ sledgehammer_problem_prefix = "BigO__bigo_plus_absorb" ]]
   529 lemma bigo_plus_absorb [simp]: "f : O(g) ==> f +o O(g) = O(g)"
   530 by (metis bigo_plus_absorb_lemma1 bigo_plus_absorb_lemma2 order_eq_iff)
   531 
   532 lemma bigo_plus_absorb2 [intro]: "f : O(g) ==> A <= O(g) ==> f +o A <= O(g)"
   533   apply (subgoal_tac "f +o A <= f +o O(g)")
   534   apply force+
   535 done
   536 
   537 lemma bigo_add_commute_imp: "f : g +o O(h) ==> g : f +o O(h)"
   538   apply (subst set_minus_plus [symmetric])
   539   apply (subgoal_tac "g - f = - (f - g)")
   540   apply (erule ssubst)
   541   apply (rule bigo_minus)
   542   apply (subst set_minus_plus)
   543   apply assumption
   544   apply  (simp add: diff_minus add_ac)
   545 done
   546 
   547 lemma bigo_add_commute: "(f : g +o O(h)) = (g : f +o O(h))"
   548   apply (rule iffI)
   549   apply (erule bigo_add_commute_imp)+
   550 done
   551 
   552 lemma bigo_const1: "(%x. c) : O(%x. 1)"
   553 by (auto simp add: bigo_def mult_ac)
   554 
   555 declare [[ sledgehammer_problem_prefix = "BigO__bigo_const2" ]]
   556 lemma (*bigo_const2 [intro]:*) "O(%x. c) <= O(%x. 1)"
   557 by (metis bigo_const1 bigo_elt_subset)
   558 
   559 lemma bigo_const2 [intro]: "O(%x. c::'b::linordered_idom) <= O(%x. 1)"
   560 (* "thus" had to be replaced by "show" with an explicit reference to "F1" *)
   561 proof -
   562   have F1: "\<forall>u. (\<lambda>Q. u) \<in> O(\<lambda>Q. 1)" by (metis bigo_const1)
   563   show "O(\<lambda>x. c) \<subseteq> O(\<lambda>x. 1)" by (metis F1 bigo_elt_subset)
   564 qed
   565 
   566 declare [[ sledgehammer_problem_prefix = "BigO__bigo_const3" ]]
   567 lemma bigo_const3: "(c::'a::linordered_field) ~= 0 ==> (%x. 1) : O(%x. c)"
   568 apply (simp add: bigo_def)
   569 by (metis abs_eq_0 left_inverse order_refl)
   570 
   571 lemma bigo_const4: "(c::'a::linordered_field) ~= 0 ==> O(%x. 1) <= O(%x. c)"
   572 by (rule bigo_elt_subset, rule bigo_const3, assumption)
   573 
   574 lemma bigo_const [simp]: "(c::'a::linordered_field) ~= 0 ==>
   575     O(%x. c) = O(%x. 1)"
   576 by (rule equalityI, rule bigo_const2, rule bigo_const4, assumption)
   577 
   578 declare [[ sledgehammer_problem_prefix = "BigO__bigo_const_mult1" ]]
   579 lemma bigo_const_mult1: "(%x. c * f x) : O(f)"
   580   apply (simp add: bigo_def abs_mult)
   581 by (metis le_less)
   582 
   583 lemma bigo_const_mult2: "O(%x. c * f x) <= O(f)"
   584 by (rule bigo_elt_subset, rule bigo_const_mult1)
   585 
   586 declare [[ sledgehammer_problem_prefix = "BigO__bigo_const_mult3" ]]
   587 lemma bigo_const_mult3: "(c::'a::linordered_field) ~= 0 ==> f : O(%x. c * f x)"
   588   apply (simp add: bigo_def)
   589 (*sledgehammer [no luck]*)
   590   apply (rule_tac x = "abs(inverse c)" in exI)
   591   apply (simp only: abs_mult [symmetric] mult_assoc [symmetric])
   592 apply (subst left_inverse)
   593 apply (auto )
   594 done
   595 
   596 lemma bigo_const_mult4: "(c::'a::linordered_field) ~= 0 ==>
   597     O(f) <= O(%x. c * f x)"
   598 by (rule bigo_elt_subset, rule bigo_const_mult3, assumption)
   599 
   600 lemma bigo_const_mult [simp]: "(c::'a::linordered_field) ~= 0 ==>
   601     O(%x. c * f x) = O(f)"
   602 by (rule equalityI, rule bigo_const_mult2, erule bigo_const_mult4)
   603 
   604 declare [[ sledgehammer_problem_prefix = "BigO__bigo_const_mult5" ]]
   605 lemma bigo_const_mult5 [simp]: "(c::'a::linordered_field) ~= 0 ==>
   606     (%x. c) *o O(f) = O(f)"
   607   apply (auto del: subsetI)
   608   apply (rule order_trans)
   609   apply (rule bigo_mult2)
   610   apply (simp add: func_times)
   611   apply (auto intro!: subsetI simp add: bigo_def elt_set_times_def func_times)
   612   apply (rule_tac x = "%y. inverse c * x y" in exI)
   613   apply (rename_tac g d)
   614   apply safe
   615   apply (rule_tac [2] ext)
   616    prefer 2
   617    apply simp
   618   apply (simp add: mult_assoc [symmetric] abs_mult)
   619   (* couldn't get this proof without the step above *)
   620 proof -
   621   fix g :: "'b \<Rightarrow> 'a" and d :: 'a
   622   assume A1: "c \<noteq> (0\<Colon>'a)"
   623   assume A2: "\<forall>x\<Colon>'b. \<bar>g x\<bar> \<le> d * \<bar>f x\<bar>"
   624   have F1: "inverse \<bar>c\<bar> = \<bar>inverse c\<bar>" using A1 by (metis nonzero_abs_inverse)
   625   have F2: "(0\<Colon>'a) < \<bar>c\<bar>" using A1 by (metis zero_less_abs_iff)
   626   have "(0\<Colon>'a) < \<bar>c\<bar> \<longrightarrow> (0\<Colon>'a) < \<bar>inverse c\<bar>" using F1 by (metis positive_imp_inverse_positive)
   627   hence "(0\<Colon>'a) < \<bar>inverse c\<bar>" using F2 by metis
   628   hence F3: "(0\<Colon>'a) \<le> \<bar>inverse c\<bar>" by (metis order_le_less)
   629   have "\<exists>(u\<Colon>'a) SKF\<^isub>7\<Colon>'a \<Rightarrow> 'b. \<bar>g (SKF\<^isub>7 (\<bar>inverse c\<bar> * u))\<bar> \<le> u * \<bar>f (SKF\<^isub>7 (\<bar>inverse c\<bar> * u))\<bar>"
   630     using A2 by metis
   631   hence F4: "\<exists>(u\<Colon>'a) SKF\<^isub>7\<Colon>'a \<Rightarrow> 'b. \<bar>g (SKF\<^isub>7 (\<bar>inverse c\<bar> * u))\<bar> \<le> u * \<bar>f (SKF\<^isub>7 (\<bar>inverse c\<bar> * u))\<bar> \<and> (0\<Colon>'a) \<le> \<bar>inverse c\<bar>"
   632     using F3 by metis
   633   hence "\<exists>(v\<Colon>'a) (u\<Colon>'a) SKF\<^isub>7\<Colon>'a \<Rightarrow> 'b. \<bar>inverse c\<bar> * \<bar>g (SKF\<^isub>7 (u * v))\<bar> \<le> u * (v * \<bar>f (SKF\<^isub>7 (u * v))\<bar>)"
   634     by (metis comm_mult_left_mono)
   635   thus "\<exists>ca\<Colon>'a. \<forall>x\<Colon>'b. \<bar>inverse c\<bar> * \<bar>g x\<bar> \<le> ca * \<bar>f x\<bar>"
   636     using A2 F4 by (metis ab_semigroup_mult_class.mult_ac(1) comm_mult_left_mono)
   637 qed
   638 
   639 
   640 declare [[ sledgehammer_problem_prefix = "BigO__bigo_const_mult6" ]]
   641 lemma bigo_const_mult6 [intro]: "(%x. c) *o O(f) <= O(f)"
   642   apply (auto intro!: subsetI
   643     simp add: bigo_def elt_set_times_def func_times
   644     simp del: abs_mult mult_ac)
   645 (*sledgehammer*)
   646   apply (rule_tac x = "ca * (abs c)" in exI)
   647   apply (rule allI)
   648   apply (subgoal_tac "ca * abs(c) * abs(f x) = abs(c) * (ca * abs(f x))")
   649   apply (erule ssubst)
   650   apply (subst abs_mult)
   651   apply (rule mult_left_mono)
   652   apply (erule spec)
   653   apply simp
   654   apply(simp add: mult_ac)
   655 done
   656 
   657 lemma bigo_const_mult7 [intro]: "f =o O(g) ==> (%x. c * f x) =o O(g)"
   658 proof -
   659   assume "f =o O(g)"
   660   then have "(%x. c) * f =o (%x. c) *o O(g)"
   661     by auto
   662   also have "(%x. c) * f = (%x. c * f x)"
   663     by (simp add: func_times)
   664   also have "(%x. c) *o O(g) <= O(g)"
   665     by (auto del: subsetI)
   666   finally show ?thesis .
   667 qed
   668 
   669 lemma bigo_compose1: "f =o O(g) ==> (%x. f(k x)) =o O(%x. g(k x))"
   670 by (unfold bigo_def, auto)
   671 
   672 lemma bigo_compose2: "f =o g +o O(h) ==> (%x. f(k x)) =o (%x. g(k x)) +o
   673     O(%x. h(k x))"
   674   apply (simp only: set_minus_plus [symmetric] diff_minus fun_Compl_def
   675       func_plus)
   676   apply (erule bigo_compose1)
   677 done
   678 
   679 subsection {* Setsum *}
   680 
   681 lemma bigo_setsum_main: "ALL x. ALL y : A x. 0 <= h x y ==>
   682     EX c. ALL x. ALL y : A x. abs(f x y) <= c * (h x y) ==>
   683       (%x. SUM y : A x. f x y) =o O(%x. SUM y : A x. h x y)"
   684   apply (auto simp add: bigo_def)
   685   apply (rule_tac x = "abs c" in exI)
   686   apply (subst abs_of_nonneg) back back
   687   apply (rule setsum_nonneg)
   688   apply force
   689   apply (subst setsum_right_distrib)
   690   apply (rule allI)
   691   apply (rule order_trans)
   692   apply (rule setsum_abs)
   693   apply (rule setsum_mono)
   694 apply (blast intro: order_trans mult_right_mono abs_ge_self)
   695 done
   696 
   697 declare [[ sledgehammer_problem_prefix = "BigO__bigo_setsum1" ]]
   698 lemma bigo_setsum1: "ALL x y. 0 <= h x y ==>
   699     EX c. ALL x y. abs(f x y) <= c * (h x y) ==>
   700       (%x. SUM y : A x. f x y) =o O(%x. SUM y : A x. h x y)"
   701   apply (rule bigo_setsum_main)
   702 (*sledgehammer*)
   703   apply force
   704   apply clarsimp
   705   apply (rule_tac x = c in exI)
   706   apply force
   707 done
   708 
   709 lemma bigo_setsum2: "ALL y. 0 <= h y ==>
   710     EX c. ALL y. abs(f y) <= c * (h y) ==>
   711       (%x. SUM y : A x. f y) =o O(%x. SUM y : A x. h y)"
   712 by (rule bigo_setsum1, auto)
   713 
   714 declare [[ sledgehammer_problem_prefix = "BigO__bigo_setsum3" ]]
   715 lemma bigo_setsum3: "f =o O(h) ==>
   716     (%x. SUM y : A x. (l x y) * f(k x y)) =o
   717       O(%x. SUM y : A x. abs(l x y * h(k x y)))"
   718   apply (rule bigo_setsum1)
   719   apply (rule allI)+
   720   apply (rule abs_ge_zero)
   721   apply (unfold bigo_def)
   722   apply (auto simp add: abs_mult)
   723 (*sledgehammer*)
   724   apply (rule_tac x = c in exI)
   725   apply (rule allI)+
   726   apply (subst mult_left_commute)
   727   apply (rule mult_left_mono)
   728   apply (erule spec)
   729   apply (rule abs_ge_zero)
   730 done
   731 
   732 lemma bigo_setsum4: "f =o g +o O(h) ==>
   733     (%x. SUM y : A x. l x y * f(k x y)) =o
   734       (%x. SUM y : A x. l x y * g(k x y)) +o
   735         O(%x. SUM y : A x. abs(l x y * h(k x y)))"
   736   apply (rule set_minus_imp_plus)
   737   apply (subst fun_diff_def)
   738   apply (subst setsum_subtractf [symmetric])
   739   apply (subst right_diff_distrib [symmetric])
   740   apply (rule bigo_setsum3)
   741   apply (subst fun_diff_def [symmetric])
   742   apply (erule set_plus_imp_minus)
   743 done
   744 
   745 declare [[ sledgehammer_problem_prefix = "BigO__bigo_setsum5" ]]
   746 lemma bigo_setsum5: "f =o O(h) ==> ALL x y. 0 <= l x y ==>
   747     ALL x. 0 <= h x ==>
   748       (%x. SUM y : A x. (l x y) * f(k x y)) =o
   749         O(%x. SUM y : A x. (l x y) * h(k x y))"
   750   apply (subgoal_tac "(%x. SUM y : A x. (l x y) * h(k x y)) =
   751       (%x. SUM y : A x. abs((l x y) * h(k x y)))")
   752   apply (erule ssubst)
   753   apply (erule bigo_setsum3)
   754   apply (rule ext)
   755   apply (rule setsum_cong2)
   756   apply (thin_tac "f \<in> O(h)")
   757 apply (metis abs_of_nonneg zero_le_mult_iff)
   758 done
   759 
   760 lemma bigo_setsum6: "f =o g +o O(h) ==> ALL x y. 0 <= l x y ==>
   761     ALL x. 0 <= h x ==>
   762       (%x. SUM y : A x. (l x y) * f(k x y)) =o
   763         (%x. SUM y : A x. (l x y) * g(k x y)) +o
   764           O(%x. SUM y : A x. (l x y) * h(k x y))"
   765   apply (rule set_minus_imp_plus)
   766   apply (subst fun_diff_def)
   767   apply (subst setsum_subtractf [symmetric])
   768   apply (subst right_diff_distrib [symmetric])
   769   apply (rule bigo_setsum5)
   770   apply (subst fun_diff_def [symmetric])
   771   apply (drule set_plus_imp_minus)
   772   apply auto
   773 done
   774 
   775 subsection {* Misc useful stuff *}
   776 
   777 lemma bigo_useful_intro: "A <= O(f) ==> B <= O(f) ==>
   778   A \<oplus> B <= O(f)"
   779   apply (subst bigo_plus_idemp [symmetric])
   780   apply (rule set_plus_mono2)
   781   apply assumption+
   782 done
   783 
   784 lemma bigo_useful_add: "f =o O(h) ==> g =o O(h) ==> f + g =o O(h)"
   785   apply (subst bigo_plus_idemp [symmetric])
   786   apply (rule set_plus_intro)
   787   apply assumption+
   788 done
   789 
   790 lemma bigo_useful_const_mult: "(c::'a::linordered_field) ~= 0 ==>
   791     (%x. c) * f =o O(h) ==> f =o O(h)"
   792   apply (rule subsetD)
   793   apply (subgoal_tac "(%x. 1 / c) *o O(h) <= O(h)")
   794   apply assumption
   795   apply (rule bigo_const_mult6)
   796   apply (subgoal_tac "f = (%x. 1 / c) * ((%x. c) * f)")
   797   apply (erule ssubst)
   798   apply (erule set_times_intro2)
   799   apply (simp add: func_times)
   800 done
   801 
   802 declare [[ sledgehammer_problem_prefix = "BigO__bigo_fix" ]]
   803 lemma bigo_fix: "(%x. f ((x::nat) + 1)) =o O(%x. h(x + 1)) ==> f 0 = 0 ==>
   804     f =o O(h)"
   805   apply (simp add: bigo_alt_def)
   806 (*sledgehammer*)
   807   apply clarify
   808   apply (rule_tac x = c in exI)
   809   apply safe
   810   apply (case_tac "x = 0")
   811 apply (metis abs_ge_zero  abs_zero  order_less_le  split_mult_pos_le)
   812   apply (subgoal_tac "x = Suc (x - 1)")
   813   apply metis
   814   apply simp
   815   done
   816 
   817 
   818 lemma bigo_fix2:
   819     "(%x. f ((x::nat) + 1)) =o (%x. g(x + 1)) +o O(%x. h(x + 1)) ==>
   820        f 0 = g 0 ==> f =o g +o O(h)"
   821   apply (rule set_minus_imp_plus)
   822   apply (rule bigo_fix)
   823   apply (subst fun_diff_def)
   824   apply (subst fun_diff_def [symmetric])
   825   apply (rule set_plus_imp_minus)
   826   apply simp
   827   apply (simp add: fun_diff_def)
   828 done
   829 
   830 subsection {* Less than or equal to *}
   831 
   832 definition lesso :: "('a => 'b::linordered_idom) => ('a => 'b) => ('a => 'b)" (infixl "<o" 70) where
   833   "f <o g == (%x. max (f x - g x) 0)"
   834 
   835 lemma bigo_lesseq1: "f =o O(h) ==> ALL x. abs (g x) <= abs (f x) ==>
   836     g =o O(h)"
   837   apply (unfold bigo_def)
   838   apply clarsimp
   839 apply (blast intro: order_trans)
   840 done
   841 
   842 lemma bigo_lesseq2: "f =o O(h) ==> ALL x. abs (g x) <= f x ==>
   843       g =o O(h)"
   844   apply (erule bigo_lesseq1)
   845 apply (blast intro: abs_ge_self order_trans)
   846 done
   847 
   848 lemma bigo_lesseq3: "f =o O(h) ==> ALL x. 0 <= g x ==> ALL x. g x <= f x ==>
   849       g =o O(h)"
   850   apply (erule bigo_lesseq2)
   851   apply (rule allI)
   852   apply (subst abs_of_nonneg)
   853   apply (erule spec)+
   854 done
   855 
   856 lemma bigo_lesseq4: "f =o O(h) ==>
   857     ALL x. 0 <= g x ==> ALL x. g x <= abs (f x) ==>
   858       g =o O(h)"
   859   apply (erule bigo_lesseq1)
   860   apply (rule allI)
   861   apply (subst abs_of_nonneg)
   862   apply (erule spec)+
   863 done
   864 
   865 declare [[ sledgehammer_problem_prefix = "BigO__bigo_lesso1" ]]
   866 lemma bigo_lesso1: "ALL x. f x <= g x ==> f <o g =o O(h)"
   867 apply (unfold lesso_def)
   868 apply (subgoal_tac "(%x. max (f x - g x) 0) = 0")
   869 proof -
   870   assume "(\<lambda>x. max (f x - g x) 0) = 0"
   871   thus "(\<lambda>x. max (f x - g x) 0) \<in> O(h)" by (metis bigo_zero)
   872 next
   873   show "\<forall>x\<Colon>'a. f x \<le> g x \<Longrightarrow> (\<lambda>x\<Colon>'a. max (f x - g x) (0\<Colon>'b)) = (0\<Colon>'a \<Rightarrow> 'b)"
   874   apply (unfold func_zero)
   875   apply (rule ext)
   876   by (simp split: split_max)
   877 qed
   878 
   879 declare [[ sledgehammer_problem_prefix = "BigO__bigo_lesso2" ]]
   880 lemma bigo_lesso2: "f =o g +o O(h) ==>
   881     ALL x. 0 <= k x ==> ALL x. k x <= f x ==>
   882       k <o g =o O(h)"
   883   apply (unfold lesso_def)
   884   apply (rule bigo_lesseq4)
   885   apply (erule set_plus_imp_minus)
   886   apply (rule allI)
   887   apply (rule le_maxI2)
   888   apply (rule allI)
   889   apply (subst fun_diff_def)
   890 apply (erule thin_rl)
   891 (*sledgehammer*)
   892   apply (case_tac "0 <= k x - g x")
   893 (* apply (metis abs_le_iff add_le_imp_le_right diff_minus le_less
   894                 le_max_iff_disj min_max.le_supE min_max.sup_absorb2
   895                 min_max.sup_commute) *)
   896 proof -
   897   fix x :: 'a
   898   assume "\<forall>x\<Colon>'a. k x \<le> f x"
   899   hence F1: "\<forall>x\<^isub>1\<Colon>'a. max (k x\<^isub>1) (f x\<^isub>1) = f x\<^isub>1" by (metis min_max.sup_absorb2)
   900   assume "(0\<Colon>'b) \<le> k x - g x"
   901   hence F2: "max (0\<Colon>'b) (k x - g x) = k x - g x" by (metis min_max.sup_absorb2)
   902   have F3: "\<forall>x\<^isub>1\<Colon>'b. x\<^isub>1 \<le> \<bar>x\<^isub>1\<bar>" by (metis abs_le_iff le_less)
   903   have "\<forall>(x\<^isub>2\<Colon>'b) x\<^isub>1\<Colon>'b. max x\<^isub>1 x\<^isub>2 \<le> x\<^isub>2 \<or> max x\<^isub>1 x\<^isub>2 \<le> x\<^isub>1" by (metis le_less le_max_iff_disj)
   904   hence "\<forall>(x\<^isub>3\<Colon>'b) (x\<^isub>2\<Colon>'b) x\<^isub>1\<Colon>'b. x\<^isub>1 - x\<^isub>2 \<le> x\<^isub>3 - x\<^isub>2 \<or> x\<^isub>3 \<le> x\<^isub>1" by (metis add_le_imp_le_right diff_minus min_max.le_supE)
   905   hence "k x - g x \<le> f x - g x" by (metis F1 le_less min_max.sup_absorb2 min_max.sup_commute)
   906   hence "k x - g x \<le> \<bar>f x - g x\<bar>" by (metis F3 le_max_iff_disj min_max.sup_absorb2)
   907   thus "max (k x - g x) (0\<Colon>'b) \<le> \<bar>f x - g x\<bar>" by (metis F2 min_max.sup_commute)
   908 next
   909   show "\<And>x\<Colon>'a.
   910        \<lbrakk>\<forall>x\<Colon>'a. (0\<Colon>'b) \<le> k x; \<forall>x\<Colon>'a. k x \<le> f x; \<not> (0\<Colon>'b) \<le> k x - g x\<rbrakk>
   911        \<Longrightarrow> max (k x - g x) (0\<Colon>'b) \<le> \<bar>f x - g x\<bar>"
   912     by (metis abs_ge_zero le_cases min_max.sup_absorb2)
   913 qed
   914 
   915 declare [[ sledgehammer_problem_prefix = "BigO__bigo_lesso3" ]]
   916 lemma bigo_lesso3: "f =o g +o O(h) ==>
   917     ALL x. 0 <= k x ==> ALL x. g x <= k x ==>
   918       f <o k =o O(h)"
   919   apply (unfold lesso_def)
   920   apply (rule bigo_lesseq4)
   921   apply (erule set_plus_imp_minus)
   922   apply (rule allI)
   923   apply (rule le_maxI2)
   924   apply (rule allI)
   925   apply (subst fun_diff_def)
   926 apply (erule thin_rl)
   927 (*sledgehammer*)
   928   apply (case_tac "0 <= f x - k x")
   929   apply (simp)
   930   apply (subst abs_of_nonneg)
   931   apply (drule_tac x = x in spec) back
   932 using [[ sledgehammer_problem_prefix = "BigO__bigo_lesso3_simpler" ]]
   933 apply (metis diff_less_0_iff_less linorder_not_le not_leE uminus_add_conv_diff xt1(12) xt1(6))
   934 apply (metis add_minus_cancel diff_le_eq le_diff_eq uminus_add_conv_diff)
   935 apply (metis abs_ge_zero linorder_linear min_max.sup_absorb1 min_max.sup_commute)
   936 done
   937 
   938 lemma bigo_lesso4: "f <o g =o O(k::'a=>'b::linordered_field) ==>
   939     g =o h +o O(k) ==> f <o h =o O(k)"
   940   apply (unfold lesso_def)
   941   apply (drule set_plus_imp_minus)
   942   apply (drule bigo_abs5) back
   943   apply (simp add: fun_diff_def)
   944   apply (drule bigo_useful_add)
   945   apply assumption
   946   apply (erule bigo_lesseq2) back
   947   apply (rule allI)
   948   apply (auto simp add: func_plus fun_diff_def algebra_simps
   949     split: split_max abs_split)
   950 done
   951 
   952 declare [[ sledgehammer_problem_prefix = "BigO__bigo_lesso5" ]]
   953 lemma bigo_lesso5: "f <o g =o O(h) ==>
   954     EX C. ALL x. f x <= g x + C * abs(h x)"
   955   apply (simp only: lesso_def bigo_alt_def)
   956   apply clarsimp
   957   apply (metis abs_if abs_mult add_commute diff_le_eq less_not_permute)
   958 done
   959 
   960 end