src/HOL/Metis_Examples/Big_O.thy
 author blanchet Mon Jan 30 17:15:59 2012 +0100 (2012-01-30) changeset 46364 abab10d1f4a3 parent 45705 a25ff4283352 child 46369 9ac0c64ad8e7 permissions -rw-r--r--
example tuning
```     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   "~~/src/HOL/Library/Function_Algebras"
```
```    14   "~~/src/HOL/Library/Set_Algebras"
```
```    15 begin
```
```    16
```
```    17 subsection {* Definitions *}
```
```    18
```
```    19 definition bigo :: "('a => 'b\<Colon>{linordered_idom,number_ring}) => ('a => 'b) set" ("(1O'(_'))") where
```
```    20   "O(f\<Colon>('a => 'b)) == {h. \<exists>c. \<forall>x. abs (h x) <= c * abs (f x)}"
```
```    21
```
```    22 lemma bigo_pos_const:
```
```    23   "(\<exists>c\<Colon>'a\<Colon>linordered_idom.
```
```    24     \<forall>x. abs (h x) \<le> c * abs (f x))
```
```    25     \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. abs(h x) \<le> c * abs (f x)))"
```
```    26 by (metis (no_types) abs_ge_zero
```
```    27       comm_semiring_1_class.normalizing_semiring_rules(7) mult.comm_neutral
```
```    28       mult_nonpos_nonneg not_leE order_trans zero_less_one)
```
```    29
```
```    30 (*** Now various verions with an increasing shrink factor ***)
```
```    31
```
```    32 sledgehammer_params [isar_proof, isar_shrink_factor = 1]
```
```    33
```
```    34 lemma
```
```    35   "(\<exists>c\<Colon>'a\<Colon>linordered_idom.
```
```    36     \<forall>x. abs (h x) \<le> c * abs (f x))
```
```    37     \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. abs(h x) \<le> c * abs (f x)))"
```
```    38   apply auto
```
```    39   apply (case_tac "c = 0", simp)
```
```    40   apply (rule_tac x = "1" in exI, simp)
```
```    41   apply (rule_tac x = "abs c" in exI, auto)
```
```    42 proof -
```
```    43   fix c :: 'a and x :: 'b
```
```    44   assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
```
```    45   have F1: "\<forall>x\<^isub>1\<Colon>'a\<Colon>linordered_idom. 0 \<le> \<bar>x\<^isub>1\<bar>" by (metis abs_ge_zero)
```
```    46   have F2: "\<forall>x\<^isub>1\<Colon>'a\<Colon>linordered_idom. 1 * x\<^isub>1 = x\<^isub>1" by (metis mult_1)
```
```    47   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)
```
```    48   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>"
```
```    49     by (metis abs_mult)
```
```    50   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"
```
```    51     by (metis abs_mult_pos)
```
```    52   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)
```
```    53   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)
```
```    54   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)
```
```    55   hence "\<forall>x\<^isub>3. \<bar>c * \<bar>f x\<^isub>3\<bar>\<bar> = c * \<bar>f x\<^isub>3\<bar>" by (metis F1)
```
```    56   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)
```
```    57   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)
```
```    58   hence "\<forall>x\<^isub>3. c * \<bar>f x\<^isub>3\<bar> = \<bar>c * f x\<^isub>3\<bar>" by (metis F1)
```
```    59   hence "\<bar>h x\<bar> \<le> \<bar>c * f x\<bar>" by (metis A1)
```
```    60   thus "\<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>" by (metis F4)
```
```    61 qed
```
```    62
```
```    63 sledgehammer_params [isar_proof, isar_shrink_factor = 2]
```
```    64
```
```    65 lemma
```
```    66   "(\<exists>c\<Colon>'a\<Colon>linordered_idom.
```
```    67     \<forall>x. abs (h x) \<le> c * abs (f x))
```
```    68     \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. abs(h x) \<le> c * abs (f x)))"
```
```    69   apply auto
```
```    70   apply (case_tac "c = 0", simp)
```
```    71   apply (rule_tac x = "1" in exI, simp)
```
```    72   apply (rule_tac x = "abs c" in exI, auto)
```
```    73 proof -
```
```    74   fix c :: 'a and x :: 'b
```
```    75   assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
```
```    76   have F1: "\<forall>x\<^isub>1\<Colon>'a\<Colon>linordered_idom. 1 * x\<^isub>1 = x\<^isub>1" by (metis mult_1)
```
```    77   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>"
```
```    78     by (metis abs_mult)
```
```    79   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)
```
```    80   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)
```
```    81   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)
```
```    82   hence "\<bar>h x\<bar> \<le> \<bar>c * f x\<bar>" by (metis A1 abs_ge_zero)
```
```    83   thus "\<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>" by (metis F2)
```
```    84 qed
```
```    85
```
```    86 sledgehammer_params [isar_proof, isar_shrink_factor = 3]
```
```    87
```
```    88 lemma
```
```    89   "(\<exists>c\<Colon>'a\<Colon>linordered_idom.
```
```    90     \<forall>x. abs (h x) \<le> c * abs (f x))
```
```    91     \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. abs(h x) \<le> c * abs (f x)))"
```
```    92   apply auto
```
```    93   apply (case_tac "c = 0", simp)
```
```    94   apply (rule_tac x = "1" in exI, simp)
```
```    95   apply (rule_tac x = "abs c" in exI, auto)
```
```    96 proof -
```
```    97   fix c :: 'a and x :: 'b
```
```    98   assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
```
```    99   have F1: "\<forall>x\<^isub>1\<Colon>'a\<Colon>linordered_idom. 1 * x\<^isub>1 = x\<^isub>1" by (metis mult_1)
```
```   100   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)
```
```   101   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)
```
```   102   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)
```
```   103   thus "\<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>" by (metis A1 abs_mult abs_ge_zero)
```
```   104 qed
```
```   105
```
```   106 sledgehammer_params [isar_proof, isar_shrink_factor = 4]
```
```   107
```
```   108 lemma
```
```   109   "(\<exists>c\<Colon>'a\<Colon>linordered_idom.
```
```   110     \<forall>x. abs (h x) \<le> c * abs (f x))
```
```   111     \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. abs(h x) \<le> c * abs (f x)))"
```
```   112   apply auto
```
```   113   apply (case_tac "c = 0", simp)
```
```   114   apply (rule_tac x = "1" in exI, simp)
```
```   115   apply (rule_tac x = "abs c" in exI, auto)
```
```   116 proof -
```
```   117   fix c :: 'a and x :: 'b
```
```   118   assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
```
```   119   have "\<forall>x\<^isub>1\<Colon>'a\<Colon>linordered_idom. 1 * x\<^isub>1 = x\<^isub>1" by (metis mult_1)
```
```   120   hence "\<forall>x\<^isub>3. \<bar>c * \<bar>f x\<^isub>3\<bar>\<bar> = c * \<bar>f x\<^isub>3\<bar>"
```
```   121     by (metis A1 abs_ge_zero order_trans abs_mult_pos abs_one)
```
```   122   hence "\<bar>h x\<bar> \<le> \<bar>c * f x\<bar>" by (metis A1 abs_ge_zero abs_mult_pos abs_mult)
```
```   123   thus "\<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>" by (metis abs_mult)
```
```   124 qed
```
```   125
```
```   126 sledgehammer_params [isar_proof, isar_shrink_factor = 1]
```
```   127
```
```   128 lemma bigo_alt_def: "O(f) = {h. \<exists>c. (0 < c \<and> (\<forall>x. abs (h x) <= c * abs (f x)))}"
```
```   129 by (auto simp add: bigo_def bigo_pos_const)
```
```   130
```
```   131 lemma bigo_elt_subset [intro]: "f : O(g) \<Longrightarrow> O(f) \<le> O(g)"
```
```   132 apply (auto simp add: bigo_alt_def)
```
```   133 apply (rule_tac x = "ca * c" in exI)
```
```   134 by (metis comm_semiring_1_class.normalizing_semiring_rules(7,19)
```
```   135           mult_le_cancel_left_pos order_trans mult_pos_pos)
```
```   136
```
```   137 lemma bigo_refl [intro]: "f : O(f)"
```
```   138 unfolding bigo_def mem_Collect_eq
```
```   139 by (metis mult_1 order_refl)
```
```   140
```
```   141 lemma bigo_zero: "0 : O(g)"
```
```   142 apply (auto simp add: bigo_def func_zero)
```
```   143 by (metis mult_zero_left order_refl)
```
```   144
```
```   145 lemma bigo_zero2: "O(\<lambda>x. 0) = {\<lambda>x. 0}"
```
```   146 by (auto simp add: bigo_def)
```
```   147
```
```   148 lemma bigo_plus_self_subset [intro]:
```
```   149   "O(f) \<oplus> O(f) <= O(f)"
```
```   150 apply (auto simp add: bigo_alt_def set_plus_def)
```
```   151 apply (rule_tac x = "c + ca" in exI)
```
```   152 apply auto
```
```   153 apply (simp add: ring_distribs func_plus)
```
```   154 by (metis order_trans abs_triangle_ineq add_mono)
```
```   155
```
```   156 lemma bigo_plus_idemp [simp]: "O(f) \<oplus> O(f) = O(f)"
```
```   157 by (metis bigo_plus_self_subset bigo_zero set_eq_subset set_zero_plus2)
```
```   158
```
```   159 lemma bigo_plus_subset [intro]: "O(f + g) <= O(f) \<oplus> O(g)"
```
```   160 apply (rule subsetI)
```
```   161 apply (auto simp add: bigo_def bigo_pos_const func_plus set_plus_def)
```
```   162 apply (subst bigo_pos_const [symmetric])+
```
```   163 apply (rule_tac x = "\<lambda>n. if abs (g n) <= (abs (f n)) then x n else 0" in exI)
```
```   164 apply (rule conjI)
```
```   165  apply (rule_tac x = "c + c" in exI)
```
```   166  apply clarsimp
```
```   167  apply auto
```
```   168   apply (subgoal_tac "c * abs (f xa + g xa) <= (c + c) * abs (f xa)")
```
```   169    apply (metis mult_2 order_trans)
```
```   170   apply (subgoal_tac "c * abs (f xa + g xa) <= c * (abs (f xa) + abs (g xa))")
```
```   171    apply (erule order_trans)
```
```   172    apply (simp add: ring_distribs)
```
```   173   apply (rule mult_left_mono)
```
```   174    apply (simp add: abs_triangle_ineq)
```
```   175   apply (simp add: order_less_le)
```
```   176  apply (rule mult_nonneg_nonneg)
```
```   177   apply auto
```
```   178 apply (rule_tac x = "\<lambda>n. if (abs (f n)) < abs (g n) then x n else 0" in exI)
```
```   179 apply (rule conjI)
```
```   180  apply (rule_tac x = "c + c" in exI)
```
```   181  apply auto
```
```   182  apply (subgoal_tac "c * abs (f xa + g xa) <= (c + c) * abs (g xa)")
```
```   183   apply (metis order_trans semiring_mult_2)
```
```   184  apply (subgoal_tac "c * abs (f xa + g xa) <= c * (abs (f xa) + abs (g xa))")
```
```   185   apply (erule order_trans)
```
```   186   apply (simp add: ring_distribs)
```
```   187  apply (metis abs_triangle_ineq mult_le_cancel_left_pos)
```
```   188 by (metis abs_ge_zero abs_of_pos zero_le_mult_iff)
```
```   189
```
```   190 lemma bigo_plus_subset2 [intro]: "A <= O(f) \<Longrightarrow> B <= O(f) \<Longrightarrow> A \<oplus> B <= O(f)"
```
```   191 by (metis bigo_plus_idemp set_plus_mono2)
```
```   192
```
```   193 lemma bigo_plus_eq: "\<forall>x. 0 <= f x \<Longrightarrow> \<forall>x. 0 <= g x \<Longrightarrow> O(f + g) = O(f) \<oplus> O(g)"
```
```   194 apply (rule equalityI)
```
```   195 apply (rule bigo_plus_subset)
```
```   196 apply (simp add: bigo_alt_def set_plus_def func_plus)
```
```   197 apply clarify
```
```   198 (* sledgehammer *)
```
```   199 apply (rule_tac x = "max c ca" in exI)
```
```   200 apply (rule conjI)
```
```   201  apply (metis less_max_iff_disj)
```
```   202 apply clarify
```
```   203 apply (drule_tac x = "xa" in spec)+
```
```   204 apply (subgoal_tac "0 <= f xa + g xa")
```
```   205  apply (simp add: ring_distribs)
```
```   206  apply (subgoal_tac "abs (a xa + b xa) <= abs (a xa) + abs (b xa)")
```
```   207   apply (subgoal_tac "abs (a xa) + abs (b xa) <=
```
```   208            max c ca * f xa + max c ca * g xa")
```
```   209    apply (metis order_trans)
```
```   210   defer 1
```
```   211   apply (metis abs_triangle_ineq)
```
```   212  apply (metis add_nonneg_nonneg)
```
```   213 apply (rule add_mono)
```
```   214  apply (metis le_maxI2 linorder_linear min_max.sup_absorb1 mult_right_mono xt1(6))
```
```   215 by (metis le_maxI2 linorder_not_le mult_le_cancel_right order_trans)
```
```   216
```
```   217 lemma bigo_bounded_alt: "\<forall>x. 0 <= f x \<Longrightarrow> \<forall>x. f x <= c * g x \<Longrightarrow> f : O(g)"
```
```   218 apply (auto simp add: bigo_def)
```
```   219 (* Version 1: one-line proof *)
```
```   220 by (metis abs_le_D1 linorder_class.not_less order_less_le Orderings.xt1(12) abs_mult)
```
```   221
```
```   222 lemma "\<forall>x. 0 <= f x \<Longrightarrow> \<forall>x. f x <= c * g x \<Longrightarrow> f : O(g)"
```
```   223 apply (auto simp add: bigo_def)
```
```   224 (* Version 2: structured proof *)
```
```   225 proof -
```
```   226   assume "\<forall>x. f x \<le> c * g x"
```
```   227   thus "\<exists>c. \<forall>x. f x \<le> c * \<bar>g x\<bar>" by (metis abs_mult abs_ge_self order_trans)
```
```   228 qed
```
```   229
```
```   230 lemma bigo_bounded: "\<forall>x. 0 <= f x \<Longrightarrow> \<forall>x. f x <= g x \<Longrightarrow> f : O(g)"
```
```   231 apply (erule bigo_bounded_alt [of f 1 g])
```
```   232 by (metis mult_1)
```
```   233
```
```   234 lemma bigo_bounded2: "\<forall>x. lb x <= f x \<Longrightarrow> \<forall>x. f x <= lb x + g x \<Longrightarrow> f : lb +o O(g)"
```
```   235 apply (rule set_minus_imp_plus)
```
```   236 apply (rule bigo_bounded)
```
```   237  apply (auto simp add: diff_minus fun_Compl_def func_plus)
```
```   238  prefer 2
```
```   239  apply (drule_tac x = x in spec)+
```
```   240  apply (metis add_right_mono add_commute diff_add_cancel diff_minus_eq_add le_less order_trans)
```
```   241 proof -
```
```   242   fix x :: 'a
```
```   243   assume "\<forall>x. lb x \<le> f x"
```
```   244   thus "(0\<Colon>'b) \<le> f x + - lb x" by (metis not_leE diff_minus less_iff_diff_less_0 less_le_not_le)
```
```   245 qed
```
```   246
```
```   247 lemma bigo_abs: "(\<lambda>x. abs(f x)) =o O(f)"
```
```   248 apply (unfold bigo_def)
```
```   249 apply auto
```
```   250 by (metis mult_1 order_refl)
```
```   251
```
```   252 lemma bigo_abs2: "f =o O(\<lambda>x. abs(f x))"
```
```   253 apply (unfold bigo_def)
```
```   254 apply auto
```
```   255 by (metis mult_1 order_refl)
```
```   256
```
```   257 lemma bigo_abs3: "O(f) = O(\<lambda>x. abs(f x))"
```
```   258 proof -
```
```   259   have F1: "\<forall>v u. u \<in> O(v) \<longrightarrow> O(u) \<subseteq> O(v)" by (metis bigo_elt_subset)
```
```   260   have F2: "\<forall>u. (\<lambda>R. \<bar>u R\<bar>) \<in> O(u)" by (metis bigo_abs)
```
```   261   have "\<forall>u. u \<in> O(\<lambda>R. \<bar>u R\<bar>)" by (metis bigo_abs2)
```
```   262   thus "O(f) = O(\<lambda>x. \<bar>f x\<bar>)" using F1 F2 by auto
```
```   263 qed
```
```   264
```
```   265 lemma bigo_abs4: "f =o g +o O(h) \<Longrightarrow> (\<lambda>x. abs (f x)) =o (\<lambda>x. abs (g x)) +o O(h)"
```
```   266   apply (drule set_plus_imp_minus)
```
```   267   apply (rule set_minus_imp_plus)
```
```   268   apply (subst fun_diff_def)
```
```   269 proof -
```
```   270   assume a: "f - g : O(h)"
```
```   271   have "(\<lambda>x. abs (f x) - abs (g x)) =o O(\<lambda>x. abs(abs (f x) - abs (g x)))"
```
```   272     by (rule bigo_abs2)
```
```   273   also have "... <= O(\<lambda>x. abs (f x - g x))"
```
```   274     apply (rule bigo_elt_subset)
```
```   275     apply (rule bigo_bounded)
```
```   276     apply force
```
```   277     apply (rule allI)
```
```   278     apply (rule abs_triangle_ineq3)
```
```   279     done
```
```   280   also have "... <= O(f - g)"
```
```   281     apply (rule bigo_elt_subset)
```
```   282     apply (subst fun_diff_def)
```
```   283     apply (rule bigo_abs)
```
```   284     done
```
```   285   also have "... <= O(h)"
```
```   286     using a by (rule bigo_elt_subset)
```
```   287   finally show "(\<lambda>x. abs (f x) - abs (g x)) : O(h)".
```
```   288 qed
```
```   289
```
```   290 lemma bigo_abs5: "f =o O(g) \<Longrightarrow> (\<lambda>x. abs(f x)) =o O(g)"
```
```   291 by (unfold bigo_def, auto)
```
```   292
```
```   293 lemma bigo_elt_subset2 [intro]: "f : g +o O(h) \<Longrightarrow> O(f) <= O(g) \<oplus> O(h)"
```
```   294 proof -
```
```   295   assume "f : g +o O(h)"
```
```   296   also have "... <= O(g) \<oplus> O(h)"
```
```   297     by (auto del: subsetI)
```
```   298   also have "... = O(\<lambda>x. abs(g x)) \<oplus> O(\<lambda>x. abs(h x))"
```
```   299     apply (subst bigo_abs3 [symmetric])+
```
```   300     apply (rule refl)
```
```   301     done
```
```   302   also have "... = O((\<lambda>x. abs(g x)) + (\<lambda>x. abs(h x)))"
```
```   303     by (rule bigo_plus_eq [symmetric], auto)
```
```   304   finally have "f : ...".
```
```   305   then have "O(f) <= ..."
```
```   306     by (elim bigo_elt_subset)
```
```   307   also have "... = O(\<lambda>x. abs(g x)) \<oplus> O(\<lambda>x. abs(h x))"
```
```   308     by (rule bigo_plus_eq, auto)
```
```   309   finally show ?thesis
```
```   310     by (simp add: bigo_abs3 [symmetric])
```
```   311 qed
```
```   312
```
```   313 lemma bigo_mult [intro]: "O(f)\<otimes>O(g) <= O(f * g)"
```
```   314   apply (rule subsetI)
```
```   315   apply (subst bigo_def)
```
```   316   apply (auto simp del: abs_mult mult_ac
```
```   317               simp add: bigo_alt_def set_times_def func_times)
```
```   318 (* sledgehammer *)
```
```   319   apply (rule_tac x = "c * ca" in exI)
```
```   320   apply(rule allI)
```
```   321   apply(erule_tac x = x in allE)+
```
```   322   apply(subgoal_tac "c * ca * abs(f x * g x) =
```
```   323       (c * abs(f x)) * (ca * abs(g x))")
```
```   324 prefer 2
```
```   325 apply (metis mult_assoc mult_left_commute
```
```   326   abs_of_pos mult_left_commute
```
```   327   abs_mult mult_pos_pos)
```
```   328   apply (erule ssubst)
```
```   329   apply (subst abs_mult)
```
```   330 (* not quite as hard as BigO__bigo_mult_simpler_1 (a hard problem!) since
```
```   331    abs_mult has just been done *)
```
```   332 by (metis abs_ge_zero mult_mono')
```
```   333
```
```   334 lemma bigo_mult2 [intro]: "f *o O(g) <= O(f * g)"
```
```   335   apply (auto simp add: bigo_def elt_set_times_def func_times abs_mult)
```
```   336 (* sledgehammer *)
```
```   337   apply (rule_tac x = c in exI)
```
```   338   apply clarify
```
```   339   apply (drule_tac x = x in spec)
```
```   340 (*sledgehammer [no luck]*)
```
```   341   apply (subgoal_tac "abs(f x) * abs(b x) <= abs(f x) * (c * abs(g x))")
```
```   342   apply (simp add: mult_ac)
```
```   343   apply (rule mult_left_mono, assumption)
```
```   344   apply (rule abs_ge_zero)
```
```   345 done
```
```   346
```
```   347 lemma bigo_mult3: "f : O(h) \<Longrightarrow> g : O(j) \<Longrightarrow> f * g : O(h * j)"
```
```   348 by (metis bigo_mult set_rev_mp set_times_intro)
```
```   349
```
```   350 lemma bigo_mult4 [intro]:"f : k +o O(h) \<Longrightarrow> g * f : (g * k) +o O(g * h)"
```
```   351 by (metis bigo_mult2 set_plus_mono_b set_times_intro2 set_times_plus_distrib)
```
```   352
```
```   353 lemma bigo_mult5: "\<forall>x. f x ~= 0 \<Longrightarrow>
```
```   354     O(f * g) <= (f\<Colon>'a => ('b\<Colon>{linordered_field,number_ring})) *o O(g)"
```
```   355 proof -
```
```   356   assume a: "\<forall>x. f x ~= 0"
```
```   357   show "O(f * g) <= f *o O(g)"
```
```   358   proof
```
```   359     fix h
```
```   360     assume h: "h : O(f * g)"
```
```   361     then have "(\<lambda>x. 1 / (f x)) * h : (\<lambda>x. 1 / f x) *o O(f * g)"
```
```   362       by auto
```
```   363     also have "... <= O((\<lambda>x. 1 / f x) * (f * g))"
```
```   364       by (rule bigo_mult2)
```
```   365     also have "(\<lambda>x. 1 / f x) * (f * g) = g"
```
```   366       apply (simp add: func_times)
```
```   367       apply (rule ext)
```
```   368       apply (simp add: a h nonzero_divide_eq_eq mult_ac)
```
```   369       done
```
```   370     finally have "(\<lambda>x. (1\<Colon>'b) / f x) * h : O(g)".
```
```   371     then have "f * ((\<lambda>x. (1\<Colon>'b) / f x) * h) : f *o O(g)"
```
```   372       by auto
```
```   373     also have "f * ((\<lambda>x. (1\<Colon>'b) / f x) * h) = h"
```
```   374       apply (simp add: func_times)
```
```   375       apply (rule ext)
```
```   376       apply (simp add: a h nonzero_divide_eq_eq mult_ac)
```
```   377       done
```
```   378     finally show "h : f *o O(g)".
```
```   379   qed
```
```   380 qed
```
```   381
```
```   382 lemma bigo_mult6: "\<forall>x. f x ~= 0 \<Longrightarrow>
```
```   383     O(f * g) = (f\<Colon>'a => ('b\<Colon>{linordered_field,number_ring})) *o O(g)"
```
```   384 by (metis bigo_mult2 bigo_mult5 order_antisym)
```
```   385
```
```   386 (*proof requires relaxing relevance: 2007-01-25*)
```
```   387 declare bigo_mult6 [simp]
```
```   388
```
```   389 lemma bigo_mult7: "\<forall>x. f x ~= 0 \<Longrightarrow>
```
```   390     O(f * g) <= O(f\<Colon>'a => ('b\<Colon>{linordered_field,number_ring})) \<otimes> O(g)"
```
```   391 (* sledgehammer *)
```
```   392   apply (subst bigo_mult6)
```
```   393   apply assumption
```
```   394   apply (rule set_times_mono3)
```
```   395   apply (rule bigo_refl)
```
```   396 done
```
```   397
```
```   398 declare bigo_mult6 [simp del]
```
```   399 declare bigo_mult7 [intro!]
```
```   400
```
```   401 lemma bigo_mult8: "\<forall>x. f x ~= 0 \<Longrightarrow>
```
```   402     O(f * g) = O(f\<Colon>'a => ('b\<Colon>{linordered_field,number_ring})) \<otimes> O(g)"
```
```   403 by (metis bigo_mult bigo_mult7 order_antisym_conv)
```
```   404
```
```   405 lemma bigo_minus [intro]: "f : O(g) \<Longrightarrow> - f : O(g)"
```
```   406   by (auto simp add: bigo_def fun_Compl_def)
```
```   407
```
```   408 lemma bigo_minus2: "f : g +o O(h) \<Longrightarrow> -f : -g +o O(h)"
```
```   409   apply (rule set_minus_imp_plus)
```
```   410   apply (drule set_plus_imp_minus)
```
```   411   apply (drule bigo_minus)
```
```   412   apply (simp add: diff_minus)
```
```   413 done
```
```   414
```
```   415 lemma bigo_minus3: "O(-f) = O(f)"
```
```   416   by (auto simp add: bigo_def fun_Compl_def abs_minus_cancel)
```
```   417
```
```   418 lemma bigo_plus_absorb_lemma1: "f : O(g) \<Longrightarrow> f +o O(g) <= O(g)"
```
```   419 proof -
```
```   420   assume a: "f : O(g)"
```
```   421   show "f +o O(g) <= O(g)"
```
```   422   proof -
```
```   423     have "f : O(f)" by auto
```
```   424     then have "f +o O(g) <= O(f) \<oplus> O(g)"
```
```   425       by (auto del: subsetI)
```
```   426     also have "... <= O(g) \<oplus> O(g)"
```
```   427     proof -
```
```   428       from a have "O(f) <= O(g)" by (auto del: subsetI)
```
```   429       thus ?thesis by (auto del: subsetI)
```
```   430     qed
```
```   431     also have "... <= O(g)" by (simp add: bigo_plus_idemp)
```
```   432     finally show ?thesis .
```
```   433   qed
```
```   434 qed
```
```   435
```
```   436 lemma bigo_plus_absorb_lemma2: "f : O(g) \<Longrightarrow> O(g) <= f +o O(g)"
```
```   437 proof -
```
```   438   assume a: "f : O(g)"
```
```   439   show "O(g) <= f +o O(g)"
```
```   440   proof -
```
```   441     from a have "-f : O(g)" by auto
```
```   442     then have "-f +o O(g) <= O(g)" by (elim bigo_plus_absorb_lemma1)
```
```   443     then have "f +o (-f +o O(g)) <= f +o O(g)" by auto
```
```   444     also have "f +o (-f +o O(g)) = O(g)"
```
```   445       by (simp add: set_plus_rearranges)
```
```   446     finally show ?thesis .
```
```   447   qed
```
```   448 qed
```
```   449
```
```   450 lemma bigo_plus_absorb [simp]: "f : O(g) \<Longrightarrow> f +o O(g) = O(g)"
```
```   451 by (metis bigo_plus_absorb_lemma1 bigo_plus_absorb_lemma2 order_eq_iff)
```
```   452
```
```   453 lemma bigo_plus_absorb2 [intro]: "f : O(g) \<Longrightarrow> A <= O(g) \<Longrightarrow> f +o A <= O(g)"
```
```   454   apply (subgoal_tac "f +o A <= f +o O(g)")
```
```   455   apply force+
```
```   456 done
```
```   457
```
```   458 lemma bigo_add_commute_imp: "f : g +o O(h) \<Longrightarrow> g : f +o O(h)"
```
```   459   apply (subst set_minus_plus [symmetric])
```
```   460   apply (subgoal_tac "g - f = - (f - g)")
```
```   461   apply (erule ssubst)
```
```   462   apply (rule bigo_minus)
```
```   463   apply (subst set_minus_plus)
```
```   464   apply assumption
```
```   465   apply (simp add: diff_minus add_ac)
```
```   466 done
```
```   467
```
```   468 lemma bigo_add_commute: "(f : g +o O(h)) = (g : f +o O(h))"
```
```   469   apply (rule iffI)
```
```   470   apply (erule bigo_add_commute_imp)+
```
```   471 done
```
```   472
```
```   473 lemma bigo_const1: "(\<lambda>x. c) : O(\<lambda>x. 1)"
```
```   474 by (auto simp add: bigo_def mult_ac)
```
```   475
```
```   476 lemma (*bigo_const2 [intro]:*) "O(\<lambda>x. c) <= O(\<lambda>x. 1)"
```
```   477 by (metis bigo_const1 bigo_elt_subset)
```
```   478
```
```   479 lemma bigo_const2 [intro]: "O(\<lambda>x. c\<Colon>'b\<Colon>{linordered_idom,number_ring}) <= O(\<lambda>x. 1)"
```
```   480 proof -
```
```   481   have "\<forall>u. (\<lambda>Q. u) \<in> O(\<lambda>Q. 1)" by (metis bigo_const1)
```
```   482   thus "O(\<lambda>x. c) \<subseteq> O(\<lambda>x. 1)" by (metis bigo_elt_subset)
```
```   483 qed
```
```   484
```
```   485 lemma bigo_const3: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow> (\<lambda>x. 1) : O(\<lambda>x. c)"
```
```   486 apply (simp add: bigo_def)
```
```   487 by (metis abs_eq_0 left_inverse order_refl)
```
```   488
```
```   489 lemma bigo_const4: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow> O(\<lambda>x. 1) <= O(\<lambda>x. c)"
```
```   490 by (rule bigo_elt_subset, rule bigo_const3, assumption)
```
```   491
```
```   492 lemma bigo_const [simp]: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow>
```
```   493     O(\<lambda>x. c) = O(\<lambda>x. 1)"
```
```   494 by (rule equalityI, rule bigo_const2, rule bigo_const4, assumption)
```
```   495
```
```   496 lemma bigo_const_mult1: "(\<lambda>x. c * f x) : O(f)"
```
```   497   apply (simp add: bigo_def abs_mult)
```
```   498 by (metis le_less)
```
```   499
```
```   500 lemma bigo_const_mult2: "O(\<lambda>x. c * f x) <= O(f)"
```
```   501 by (rule bigo_elt_subset, rule bigo_const_mult1)
```
```   502
```
```   503 lemma bigo_const_mult3: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow> f : O(\<lambda>x. c * f x)"
```
```   504 apply (simp add: bigo_def)
```
```   505 (* sledgehammer *)
```
```   506 apply (rule_tac x = "abs(inverse c)" in exI)
```
```   507 apply (simp only: abs_mult [symmetric] mult_assoc [symmetric])
```
```   508 apply (subst left_inverse)
```
```   509 by auto
```
```   510
```
```   511 lemma bigo_const_mult4: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow>
```
```   512     O(f) <= O(\<lambda>x. c * f x)"
```
```   513 by (rule bigo_elt_subset, rule bigo_const_mult3, assumption)
```
```   514
```
```   515 lemma bigo_const_mult [simp]: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow>
```
```   516     O(\<lambda>x. c * f x) = O(f)"
```
```   517 by (rule equalityI, rule bigo_const_mult2, erule bigo_const_mult4)
```
```   518
```
```   519 lemma bigo_const_mult5 [simp]: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow>
```
```   520     (\<lambda>x. c) *o O(f) = O(f)"
```
```   521   apply (auto del: subsetI)
```
```   522   apply (rule order_trans)
```
```   523   apply (rule bigo_mult2)
```
```   524   apply (simp add: func_times)
```
```   525   apply (auto intro!: subsetI simp add: bigo_def elt_set_times_def func_times)
```
```   526   apply (rule_tac x = "\<lambda>y. inverse c * x y" in exI)
```
```   527   apply (rename_tac g d)
```
```   528   apply safe
```
```   529   apply (rule_tac [2] ext)
```
```   530    prefer 2
```
```   531    apply simp
```
```   532   apply (simp add: mult_assoc [symmetric] abs_mult)
```
```   533   (* couldn't get this proof without the step above *)
```
```   534 proof -
```
```   535   fix g :: "'b \<Rightarrow> 'a" and d :: 'a
```
```   536   assume A1: "c \<noteq> (0\<Colon>'a)"
```
```   537   assume A2: "\<forall>x\<Colon>'b. \<bar>g x\<bar> \<le> d * \<bar>f x\<bar>"
```
```   538   have F1: "inverse \<bar>c\<bar> = \<bar>inverse c\<bar>" using A1 by (metis nonzero_abs_inverse)
```
```   539   have F2: "(0\<Colon>'a) < \<bar>c\<bar>" using A1 by (metis zero_less_abs_iff)
```
```   540   have "(0\<Colon>'a) < \<bar>c\<bar> \<longrightarrow> (0\<Colon>'a) < \<bar>inverse c\<bar>" using F1 by (metis positive_imp_inverse_positive)
```
```   541   hence "(0\<Colon>'a) < \<bar>inverse c\<bar>" using F2 by metis
```
```   542   hence F3: "(0\<Colon>'a) \<le> \<bar>inverse c\<bar>" by (metis order_le_less)
```
```   543   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>"
```
```   544     using A2 by metis
```
```   545   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>"
```
```   546     using F3 by metis
```
```   547   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>)"
```
```   548     by (metis comm_mult_left_mono)
```
```   549   thus "\<exists>ca\<Colon>'a. \<forall>x\<Colon>'b. \<bar>inverse c\<bar> * \<bar>g x\<bar> \<le> ca * \<bar>f x\<bar>"
```
```   550     using A2 F4 by (metis ab_semigroup_mult_class.mult_ac(1) comm_mult_left_mono)
```
```   551 qed
```
```   552
```
```   553 lemma bigo_const_mult6 [intro]: "(\<lambda>x. c) *o O(f) <= O(f)"
```
```   554   apply (auto intro!: subsetI
```
```   555     simp add: bigo_def elt_set_times_def func_times
```
```   556     simp del: abs_mult mult_ac)
```
```   557 (* sledgehammer *)
```
```   558   apply (rule_tac x = "ca * (abs c)" in exI)
```
```   559   apply (rule allI)
```
```   560   apply (subgoal_tac "ca * abs(c) * abs(f x) = abs(c) * (ca * abs(f x))")
```
```   561   apply (erule ssubst)
```
```   562   apply (subst abs_mult)
```
```   563   apply (rule mult_left_mono)
```
```   564   apply (erule spec)
```
```   565   apply simp
```
```   566   apply(simp add: mult_ac)
```
```   567 done
```
```   568
```
```   569 lemma bigo_const_mult7 [intro]: "f =o O(g) \<Longrightarrow> (\<lambda>x. c * f x) =o O(g)"
```
```   570 proof -
```
```   571   assume "f =o O(g)"
```
```   572   then have "(\<lambda>x. c) * f =o (\<lambda>x. c) *o O(g)"
```
```   573     by auto
```
```   574   also have "(\<lambda>x. c) * f = (\<lambda>x. c * f x)"
```
```   575     by (simp add: func_times)
```
```   576   also have "(\<lambda>x. c) *o O(g) <= O(g)"
```
```   577     by (auto del: subsetI)
```
```   578   finally show ?thesis .
```
```   579 qed
```
```   580
```
```   581 lemma bigo_compose1: "f =o O(g) \<Longrightarrow> (\<lambda>x. f(k x)) =o O(\<lambda>x. g(k x))"
```
```   582 by (unfold bigo_def, auto)
```
```   583
```
```   584 lemma bigo_compose2: "f =o g +o O(h) \<Longrightarrow> (\<lambda>x. f(k x)) =o (\<lambda>x. g(k x)) +o
```
```   585     O(\<lambda>x. h(k x))"
```
```   586   apply (simp only: set_minus_plus [symmetric] diff_minus fun_Compl_def
```
```   587       func_plus)
```
```   588   apply (erule bigo_compose1)
```
```   589 done
```
```   590
```
```   591 subsection {* Setsum *}
```
```   592
```
```   593 lemma bigo_setsum_main: "\<forall>x. \<forall>y \<in> A x. 0 <= h x y \<Longrightarrow>
```
```   594     \<exists>c. \<forall>x. \<forall>y \<in> A x. abs (f x y) <= c * (h x y) \<Longrightarrow>
```
```   595       (\<lambda>x. SUM y : A x. f x y) =o O(\<lambda>x. SUM y : A x. h x y)"
```
```   596   apply (auto simp add: bigo_def)
```
```   597   apply (rule_tac x = "abs c" in exI)
```
```   598   apply (subst abs_of_nonneg) back back
```
```   599   apply (rule setsum_nonneg)
```
```   600   apply force
```
```   601   apply (subst setsum_right_distrib)
```
```   602   apply (rule allI)
```
```   603   apply (rule order_trans)
```
```   604   apply (rule setsum_abs)
```
```   605   apply (rule setsum_mono)
```
```   606 apply (blast intro: order_trans mult_right_mono abs_ge_self)
```
```   607 done
```
```   608
```
```   609 lemma bigo_setsum1: "\<forall>x y. 0 <= h x y \<Longrightarrow>
```
```   610     \<exists>c. \<forall>x y. abs (f x y) <= c * (h x y) \<Longrightarrow>
```
```   611       (\<lambda>x. SUM y : A x. f x y) =o O(\<lambda>x. SUM y : A x. h x y)"
```
```   612 by (metis (no_types) bigo_setsum_main)
```
```   613
```
```   614 lemma bigo_setsum2: "\<forall>y. 0 <= h y \<Longrightarrow>
```
```   615     \<exists>c. \<forall>y. abs(f y) <= c * (h y) \<Longrightarrow>
```
```   616       (\<lambda>x. SUM y : A x. f y) =o O(\<lambda>x. SUM y : A x. h y)"
```
```   617 by (rule bigo_setsum1, auto)
```
```   618
```
```   619 lemma bigo_setsum3: "f =o O(h) \<Longrightarrow>
```
```   620     (\<lambda>x. SUM y : A x. (l x y) * f(k x y)) =o
```
```   621       O(\<lambda>x. SUM y : A x. abs(l x y * h(k x y)))"
```
```   622 apply (rule bigo_setsum1)
```
```   623  apply (rule allI)+
```
```   624  apply (rule abs_ge_zero)
```
```   625 apply (unfold bigo_def)
```
```   626 apply (auto simp add: abs_mult)
```
```   627 (* sledgehammer *)
```
```   628 apply (rule_tac x = c in exI)
```
```   629 apply (rule allI)+
```
```   630 apply (subst mult_left_commute)
```
```   631 apply (rule mult_left_mono)
```
```   632  apply (erule spec)
```
```   633 by (rule abs_ge_zero)
```
```   634
```
```   635 lemma bigo_setsum4: "f =o g +o O(h) \<Longrightarrow>
```
```   636     (\<lambda>x. SUM y : A x. l x y * f(k x y)) =o
```
```   637       (\<lambda>x. SUM y : A x. l x y * g(k x y)) +o
```
```   638         O(\<lambda>x. SUM y : A x. abs(l x y * h(k x y)))"
```
```   639 apply (rule set_minus_imp_plus)
```
```   640 apply (subst fun_diff_def)
```
```   641 apply (subst setsum_subtractf [symmetric])
```
```   642 apply (subst right_diff_distrib [symmetric])
```
```   643 apply (rule bigo_setsum3)
```
```   644 apply (subst fun_diff_def [symmetric])
```
```   645 by (erule set_plus_imp_minus)
```
```   646
```
```   647 lemma bigo_setsum5: "f =o O(h) \<Longrightarrow> \<forall>x y. 0 <= l x y \<Longrightarrow>
```
```   648     \<forall>x. 0 <= h x \<Longrightarrow>
```
```   649       (\<lambda>x. SUM y : A x. (l x y) * f(k x y)) =o
```
```   650         O(\<lambda>x. SUM y : A x. (l x y) * h(k x y))"
```
```   651   apply (subgoal_tac "(\<lambda>x. SUM y : A x. (l x y) * h(k x y)) =
```
```   652       (\<lambda>x. SUM y : A x. abs((l x y) * h(k x y)))")
```
```   653   apply (erule ssubst)
```
```   654   apply (erule bigo_setsum3)
```
```   655   apply (rule ext)
```
```   656   apply (rule setsum_cong2)
```
```   657   apply (thin_tac "f \<in> O(h)")
```
```   658 apply (metis abs_of_nonneg zero_le_mult_iff)
```
```   659 done
```
```   660
```
```   661 lemma bigo_setsum6: "f =o g +o O(h) \<Longrightarrow> \<forall>x y. 0 <= l x y \<Longrightarrow>
```
```   662     \<forall>x. 0 <= h x \<Longrightarrow>
```
```   663       (\<lambda>x. SUM y : A x. (l x y) * f(k x y)) =o
```
```   664         (\<lambda>x. SUM y : A x. (l x y) * g(k x y)) +o
```
```   665           O(\<lambda>x. SUM y : A x. (l x y) * h(k x y))"
```
```   666   apply (rule set_minus_imp_plus)
```
```   667   apply (subst fun_diff_def)
```
```   668   apply (subst setsum_subtractf [symmetric])
```
```   669   apply (subst right_diff_distrib [symmetric])
```
```   670   apply (rule bigo_setsum5)
```
```   671   apply (subst fun_diff_def [symmetric])
```
```   672   apply (drule set_plus_imp_minus)
```
```   673   apply auto
```
```   674 done
```
```   675
```
```   676 subsection {* Misc useful stuff *}
```
```   677
```
```   678 lemma bigo_useful_intro: "A <= O(f) \<Longrightarrow> B <= O(f) \<Longrightarrow>
```
```   679   A \<oplus> B <= O(f)"
```
```   680   apply (subst bigo_plus_idemp [symmetric])
```
```   681   apply (rule set_plus_mono2)
```
```   682   apply assumption+
```
```   683 done
```
```   684
```
```   685 lemma bigo_useful_add: "f =o O(h) \<Longrightarrow> g =o O(h) \<Longrightarrow> f + g =o O(h)"
```
```   686   apply (subst bigo_plus_idemp [symmetric])
```
```   687   apply (rule set_plus_intro)
```
```   688   apply assumption+
```
```   689 done
```
```   690
```
```   691 lemma bigo_useful_const_mult: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow>
```
```   692     (\<lambda>x. c) * f =o O(h) \<Longrightarrow> f =o O(h)"
```
```   693   apply (rule subsetD)
```
```   694   apply (subgoal_tac "(\<lambda>x. 1 / c) *o O(h) <= O(h)")
```
```   695   apply assumption
```
```   696   apply (rule bigo_const_mult6)
```
```   697   apply (subgoal_tac "f = (\<lambda>x. 1 / c) * ((\<lambda>x. c) * f)")
```
```   698   apply (erule ssubst)
```
```   699   apply (erule set_times_intro2)
```
```   700   apply (simp add: func_times)
```
```   701 done
```
```   702
```
```   703 lemma bigo_fix: "(\<lambda>x. f ((x\<Colon>nat) + 1)) =o O(\<lambda>x. h(x + 1)) \<Longrightarrow> f 0 = 0 \<Longrightarrow>
```
```   704     f =o O(h)"
```
```   705 apply (simp add: bigo_alt_def)
```
```   706 by (metis abs_ge_zero abs_mult abs_of_pos abs_zero not0_implies_Suc)
```
```   707
```
```   708 lemma bigo_fix2:
```
```   709     "(\<lambda>x. f ((x\<Colon>nat) + 1)) =o (\<lambda>x. g(x + 1)) +o O(\<lambda>x. h(x + 1)) \<Longrightarrow>
```
```   710        f 0 = g 0 \<Longrightarrow> f =o g +o O(h)"
```
```   711   apply (rule set_minus_imp_plus)
```
```   712   apply (rule bigo_fix)
```
```   713   apply (subst fun_diff_def)
```
```   714   apply (subst fun_diff_def [symmetric])
```
```   715   apply (rule set_plus_imp_minus)
```
```   716   apply simp
```
```   717   apply (simp add: fun_diff_def)
```
```   718 done
```
```   719
```
```   720 subsection {* Less than or equal to *}
```
```   721
```
```   722 definition lesso :: "('a => 'b\<Colon>linordered_idom) => ('a => 'b) => ('a => 'b)" (infixl "<o" 70) where
```
```   723   "f <o g == (\<lambda>x. max (f x - g x) 0)"
```
```   724
```
```   725 lemma bigo_lesseq1: "f =o O(h) \<Longrightarrow> \<forall>x. abs (g x) <= abs (f x) \<Longrightarrow>
```
```   726     g =o O(h)"
```
```   727   apply (unfold bigo_def)
```
```   728   apply clarsimp
```
```   729 apply (blast intro: order_trans)
```
```   730 done
```
```   731
```
```   732 lemma bigo_lesseq2: "f =o O(h) \<Longrightarrow> \<forall>x. abs (g x) <= f x \<Longrightarrow>
```
```   733       g =o O(h)"
```
```   734   apply (erule bigo_lesseq1)
```
```   735 apply (blast intro: abs_ge_self order_trans)
```
```   736 done
```
```   737
```
```   738 lemma bigo_lesseq3: "f =o O(h) \<Longrightarrow> \<forall>x. 0 <= g x \<Longrightarrow> \<forall>x. g x <= f x \<Longrightarrow>
```
```   739       g =o O(h)"
```
```   740   apply (erule bigo_lesseq2)
```
```   741   apply (rule allI)
```
```   742   apply (subst abs_of_nonneg)
```
```   743   apply (erule spec)+
```
```   744 done
```
```   745
```
```   746 lemma bigo_lesseq4: "f =o O(h) \<Longrightarrow>
```
```   747     \<forall>x. 0 <= g x \<Longrightarrow> \<forall>x. g x <= abs (f x) \<Longrightarrow>
```
```   748       g =o O(h)"
```
```   749   apply (erule bigo_lesseq1)
```
```   750   apply (rule allI)
```
```   751   apply (subst abs_of_nonneg)
```
```   752   apply (erule spec)+
```
```   753 done
```
```   754
```
```   755 lemma bigo_lesso1: "\<forall>x. f x <= g x \<Longrightarrow> f <o g =o O(h)"
```
```   756 apply (unfold lesso_def)
```
```   757 apply (subgoal_tac "(\<lambda>x. max (f x - g x) 0) = 0")
```
```   758  apply (metis bigo_zero)
```
```   759 by (metis (lifting, no_types) func_zero le_fun_def le_iff_diff_le_0
```
```   760       min_max.sup_absorb2 order_eq_iff)
```
```   761
```
```   762 lemma bigo_lesso2: "f =o g +o O(h) \<Longrightarrow>
```
```   763     \<forall>x. 0 <= k x \<Longrightarrow> \<forall>x. k x <= f x \<Longrightarrow>
```
```   764       k <o g =o O(h)"
```
```   765   apply (unfold lesso_def)
```
```   766   apply (rule bigo_lesseq4)
```
```   767   apply (erule set_plus_imp_minus)
```
```   768   apply (rule allI)
```
```   769   apply (rule le_maxI2)
```
```   770   apply (rule allI)
```
```   771   apply (subst fun_diff_def)
```
```   772 apply (erule thin_rl)
```
```   773 (* sledgehammer *)
```
```   774 apply (case_tac "0 <= k x - g x")
```
```   775  apply (metis (hide_lams, no_types) abs_le_iff add_le_imp_le_right diff_minus le_less
```
```   776           le_max_iff_disj min_max.le_supE min_max.sup_absorb2
```
```   777           min_max.sup_commute)
```
```   778 by (metis abs_ge_zero le_cases min_max.sup_absorb2)
```
```   779
```
```   780 lemma bigo_lesso3: "f =o g +o O(h) \<Longrightarrow>
```
```   781     \<forall>x. 0 <= k x \<Longrightarrow> \<forall>x. g x <= k x \<Longrightarrow>
```
```   782       f <o k =o O(h)"
```
```   783   apply (unfold lesso_def)
```
```   784   apply (rule bigo_lesseq4)
```
```   785   apply (erule set_plus_imp_minus)
```
```   786   apply (rule allI)
```
```   787   apply (rule le_maxI2)
```
```   788   apply (rule allI)
```
```   789   apply (subst fun_diff_def)
```
```   790   apply (erule thin_rl)
```
```   791   (* sledgehammer *)
```
```   792   apply (case_tac "0 <= f x - k x")
```
```   793   apply simp
```
```   794   apply (subst abs_of_nonneg)
```
```   795   apply (drule_tac x = x in spec) back
```
```   796   apply (metis diff_less_0_iff_less linorder_not_le not_leE xt1(12) xt1(6))
```
```   797  apply (metis add_minus_cancel diff_le_eq le_diff_eq uminus_add_conv_diff)
```
```   798 apply (metis abs_ge_zero linorder_linear min_max.sup_absorb1 min_max.sup_commute)
```
```   799 done
```
```   800
```
```   801 lemma bigo_lesso4:
```
```   802   "f <o g =o O(k\<Colon>'a=>'b\<Colon>{linordered_field,number_ring}) \<Longrightarrow>
```
```   803    g =o h +o O(k) \<Longrightarrow> f <o h =o O(k)"
```
```   804 apply (unfold lesso_def)
```
```   805 apply (drule set_plus_imp_minus)
```
```   806 apply (drule bigo_abs5) back
```
```   807 apply (simp add: fun_diff_def)
```
```   808 apply (drule bigo_useful_add, assumption)
```
```   809 apply (erule bigo_lesseq2) back
```
```   810 apply (rule allI)
```
```   811 by (auto simp add: func_plus fun_diff_def algebra_simps
```
```   812     split: split_max abs_split)
```
```   813
```
```   814 lemma bigo_lesso5: "f <o g =o O(h) \<Longrightarrow> \<exists>C. \<forall>x. f x <= g x + C * abs (h x)"
```
```   815 apply (simp only: lesso_def bigo_alt_def)
```
```   816 apply clarsimp
```
```   817 by (metis abs_if abs_mult add_commute diff_le_eq less_not_permute)
```
```   818
```
```   819 end
```