src/HOL/Metis_Examples/Big_O.thy
changeset 61945 1135b8de26c3
parent 61824 dcbe9f756ae0
child 61954 1d43f86f48be
equal deleted inserted replaced
61944:5d06ecfdb472 61945:1135b8de26c3
    15 begin
    15 begin
    16 
    16 
    17 subsection {* Definitions *}
    17 subsection {* Definitions *}
    18 
    18 
    19 definition bigo :: "('a => 'b::linordered_idom) => ('a => 'b) set" ("(1O'(_'))") where
    19 definition bigo :: "('a => 'b::linordered_idom) => ('a => 'b) set" ("(1O'(_'))") where
    20   "O(f::('a => 'b)) == {h. \<exists>c. \<forall>x. abs (h x) <= c * abs (f x)}"
    20   "O(f::('a => 'b)) == {h. \<exists>c. \<forall>x. \<bar>h x\<bar> <= c * \<bar>f x\<bar>}"
    21 
    21 
    22 lemma bigo_pos_const:
    22 lemma bigo_pos_const:
    23   "(\<exists>c::'a::linordered_idom.
    23   "(\<exists>c::'a::linordered_idom.
    24     \<forall>x. abs (h x) \<le> c * abs (f x))
    24     \<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>)
    25     \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. abs(h x) \<le> c * abs (f x)))"
    25     \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>))"
    26   by (metis (no_types) abs_ge_zero
    26   by (metis (no_types) abs_ge_zero
    27       algebra_simps mult.comm_neutral
    27       algebra_simps mult.comm_neutral
    28       mult_nonpos_nonneg not_le_imp_less order_trans zero_less_one)
    28       mult_nonpos_nonneg not_le_imp_less order_trans zero_less_one)
    29 
    29 
    30 (*** Now various verions with an increasing shrink factor ***)
    30 (*** Now various verions with an increasing shrink factor ***)
    31 
    31 
    32 sledgehammer_params [isar_proofs, compress = 1]
    32 sledgehammer_params [isar_proofs, compress = 1]
    33 
    33 
    34 lemma
    34 lemma
    35   "(\<exists>c::'a::linordered_idom.
    35   "(\<exists>c::'a::linordered_idom.
    36     \<forall>x. abs (h x) \<le> c * abs (f x))
    36     \<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>)
    37     \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. abs(h x) \<le> c * abs (f x)))"
    37     \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>))"
    38   apply auto
    38   apply auto
    39   apply (case_tac "c = 0", simp)
    39   apply (case_tac "c = 0", simp)
    40   apply (rule_tac x = "1" in exI, simp)
    40   apply (rule_tac x = "1" in exI, simp)
    41   apply (rule_tac x = "abs c" in exI, auto)
    41   apply (rule_tac x = "\<bar>c\<bar>" in exI, auto)
    42 proof -
    42 proof -
    43   fix c :: 'a and x :: 'b
    43   fix c :: 'a and x :: 'b
    44   assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
    44   assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
    45   have F1: "\<forall>x\<^sub>1::'a::linordered_idom. 0 \<le> \<bar>x\<^sub>1\<bar>" by (metis abs_ge_zero)
    45   have F1: "\<forall>x\<^sub>1::'a::linordered_idom. 0 \<le> \<bar>x\<^sub>1\<bar>" by (metis abs_ge_zero)
    46   have F2: "\<forall>x\<^sub>1::'a::linordered_idom. 1 * x\<^sub>1 = x\<^sub>1" by (metis mult_1)
    46   have F2: "\<forall>x\<^sub>1::'a::linordered_idom. 1 * x\<^sub>1 = x\<^sub>1" by (metis mult_1)
    62 
    62 
    63 sledgehammer_params [isar_proofs, compress = 2]
    63 sledgehammer_params [isar_proofs, compress = 2]
    64 
    64 
    65 lemma
    65 lemma
    66   "(\<exists>c::'a::linordered_idom.
    66   "(\<exists>c::'a::linordered_idom.
    67     \<forall>x. abs (h x) \<le> c * abs (f x))
    67     \<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>)
    68     \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. abs(h x) \<le> c * abs (f x)))"
    68     \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>))"
    69   apply auto
    69   apply auto
    70   apply (case_tac "c = 0", simp)
    70   apply (case_tac "c = 0", simp)
    71   apply (rule_tac x = "1" in exI, simp)
    71   apply (rule_tac x = "1" in exI, simp)
    72   apply (rule_tac x = "abs c" in exI, auto)
    72   apply (rule_tac x = "\<bar>c\<bar>" in exI, auto)
    73 proof -
    73 proof -
    74   fix c :: 'a and x :: 'b
    74   fix c :: 'a and x :: 'b
    75   assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
    75   assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
    76   have F1: "\<forall>x\<^sub>1::'a::linordered_idom. 1 * x\<^sub>1 = x\<^sub>1" by (metis mult_1)
    76   have F1: "\<forall>x\<^sub>1::'a::linordered_idom. 1 * x\<^sub>1 = x\<^sub>1" by (metis mult_1)
    77   have F2: "\<forall>x\<^sub>2 x\<^sub>3::'a::linordered_idom. \<bar>x\<^sub>3\<bar> * \<bar>x\<^sub>2\<bar> = \<bar>x\<^sub>3 * x\<^sub>2\<bar>"
    77   have F2: "\<forall>x\<^sub>2 x\<^sub>3::'a::linordered_idom. \<bar>x\<^sub>3\<bar> * \<bar>x\<^sub>2\<bar> = \<bar>x\<^sub>3 * x\<^sub>2\<bar>"
    85 
    85 
    86 sledgehammer_params [isar_proofs, compress = 3]
    86 sledgehammer_params [isar_proofs, compress = 3]
    87 
    87 
    88 lemma
    88 lemma
    89   "(\<exists>c::'a::linordered_idom.
    89   "(\<exists>c::'a::linordered_idom.
    90     \<forall>x. abs (h x) \<le> c * abs (f x))
    90     \<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>)
    91     \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. abs(h x) \<le> c * abs (f x)))"
    91     \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>))"
    92   apply auto
    92   apply auto
    93   apply (case_tac "c = 0", simp)
    93   apply (case_tac "c = 0", simp)
    94   apply (rule_tac x = "1" in exI, simp)
    94   apply (rule_tac x = "1" in exI, simp)
    95   apply (rule_tac x = "abs c" in exI, auto)
    95   apply (rule_tac x = "\<bar>c\<bar>" in exI, auto)
    96 proof -
    96 proof -
    97   fix c :: 'a and x :: 'b
    97   fix c :: 'a and x :: 'b
    98   assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
    98   assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
    99   have F1: "\<forall>x\<^sub>1::'a::linordered_idom. 1 * x\<^sub>1 = x\<^sub>1" by (metis mult_1)
    99   have F1: "\<forall>x\<^sub>1::'a::linordered_idom. 1 * x\<^sub>1 = x\<^sub>1" by (metis mult_1)
   100   have F2: "\<forall>x\<^sub>3 x\<^sub>1::'a::linordered_idom. 0 \<le> x\<^sub>1 \<longrightarrow> \<bar>x\<^sub>3 * x\<^sub>1\<bar> = \<bar>x\<^sub>3\<bar> * x\<^sub>1" by (metis abs_mult_pos)
   100   have F2: "\<forall>x\<^sub>3 x\<^sub>1::'a::linordered_idom. 0 \<le> x\<^sub>1 \<longrightarrow> \<bar>x\<^sub>3 * x\<^sub>1\<bar> = \<bar>x\<^sub>3\<bar> * x\<^sub>1" by (metis abs_mult_pos)
   105 
   105 
   106 sledgehammer_params [isar_proofs, compress = 4]
   106 sledgehammer_params [isar_proofs, compress = 4]
   107 
   107 
   108 lemma
   108 lemma
   109   "(\<exists>c::'a::linordered_idom.
   109   "(\<exists>c::'a::linordered_idom.
   110     \<forall>x. abs (h x) \<le> c * abs (f x))
   110     \<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>)
   111     \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. abs(h x) \<le> c * abs (f x)))"
   111     \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>))"
   112   apply auto
   112   apply auto
   113   apply (case_tac "c = 0", simp)
   113   apply (case_tac "c = 0", simp)
   114   apply (rule_tac x = "1" in exI, simp)
   114   apply (rule_tac x = "1" in exI, simp)
   115   apply (rule_tac x = "abs c" in exI, auto)
   115   apply (rule_tac x = "\<bar>c\<bar>" in exI, auto)
   116 proof -
   116 proof -
   117   fix c :: 'a and x :: 'b
   117   fix c :: 'a and x :: 'b
   118   assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
   118   assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
   119   have "\<forall>x\<^sub>1::'a::linordered_idom. 1 * x\<^sub>1 = x\<^sub>1" by (metis mult_1)
   119   have "\<forall>x\<^sub>1::'a::linordered_idom. 1 * x\<^sub>1 = x\<^sub>1" by (metis mult_1)
   120   hence "\<forall>x\<^sub>3. \<bar>c * \<bar>f x\<^sub>3\<bar>\<bar> = c * \<bar>f x\<^sub>3\<bar>"
   120   hence "\<forall>x\<^sub>3. \<bar>c * \<bar>f x\<^sub>3\<bar>\<bar> = c * \<bar>f x\<^sub>3\<bar>"
   123   thus "\<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>" by (metis abs_mult)
   123   thus "\<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>" by (metis abs_mult)
   124 qed
   124 qed
   125 
   125 
   126 sledgehammer_params [isar_proofs, compress = 1]
   126 sledgehammer_params [isar_proofs, compress = 1]
   127 
   127 
   128 lemma bigo_alt_def: "O(f) = {h. \<exists>c. (0 < c \<and> (\<forall>x. abs (h x) <= c * abs (f x)))}"
   128 lemma bigo_alt_def: "O(f) = {h. \<exists>c. (0 < c \<and> (\<forall>x. \<bar>h x\<bar> <= c * \<bar>f x\<bar>))}"
   129 by (auto simp add: bigo_def bigo_pos_const)
   129 by (auto simp add: bigo_def bigo_pos_const)
   130 
   130 
   131 lemma bigo_elt_subset [intro]: "f : O(g) \<Longrightarrow> O(f) \<le> O(g)"
   131 lemma bigo_elt_subset [intro]: "f : O(g) \<Longrightarrow> O(f) \<le> O(g)"
   132 apply (auto simp add: bigo_alt_def)
   132 apply (auto simp add: bigo_alt_def)
   133 apply (rule_tac x = "ca * c" in exI)
   133 apply (rule_tac x = "ca * c" in exI)
   158 
   158 
   159 lemma bigo_plus_subset [intro]: "O(f + g) <= O(f) + O(g)"
   159 lemma bigo_plus_subset [intro]: "O(f + g) <= O(f) + O(g)"
   160 apply (rule subsetI)
   160 apply (rule subsetI)
   161 apply (auto simp add: bigo_def bigo_pos_const func_plus set_plus_def)
   161 apply (auto simp add: bigo_def bigo_pos_const func_plus set_plus_def)
   162 apply (subst bigo_pos_const [symmetric])+
   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)
   163 apply (rule_tac x = "\<lambda>n. if \<bar>g n\<bar> <= \<bar>f n\<bar> then x n else 0" in exI)
   164 apply (rule conjI)
   164 apply (rule conjI)
   165  apply (rule_tac x = "c + c" in exI)
   165  apply (rule_tac x = "c + c" in exI)
   166  apply clarsimp
   166  apply clarsimp
   167  apply (subgoal_tac "c * abs (f xa + g xa) <= (c + c) * abs (f xa)")
   167  apply (subgoal_tac "c * \<bar>f xa + g xa\<bar> <= (c + c) * \<bar>f xa\<bar>")
   168   apply (metis mult_2 order_trans)
   168   apply (metis mult_2 order_trans)
   169  apply (subgoal_tac "c * abs (f xa + g xa) <= c * (abs (f xa) + abs (g xa))")
   169  apply (subgoal_tac "c * \<bar>f xa + g xa\<bar> <= c * (\<bar>f xa\<bar> + \<bar>g xa\<bar>)")
   170   apply (erule order_trans)
   170   apply (erule order_trans)
   171   apply (simp add: ring_distribs)
   171   apply (simp add: ring_distribs)
   172  apply (rule mult_left_mono)
   172  apply (rule mult_left_mono)
   173   apply (simp add: abs_triangle_ineq)
   173   apply (simp add: abs_triangle_ineq)
   174  apply (simp add: order_less_le)
   174  apply (simp add: order_less_le)
   175 apply (rule_tac x = "\<lambda>n. if (abs (f n)) < abs (g n) then x n else 0" in exI)
   175 apply (rule_tac x = "\<lambda>n. if \<bar>f n\<bar> < \<bar>g n\<bar> then x n else 0" in exI)
   176 apply (rule conjI)
   176 apply (rule conjI)
   177  apply (rule_tac x = "c + c" in exI)
   177  apply (rule_tac x = "c + c" in exI)
   178  apply auto
   178  apply auto
   179 apply (subgoal_tac "c * abs (f xa + g xa) <= (c + c) * abs (g xa)")
   179 apply (subgoal_tac "c * \<bar>f xa + g xa\<bar> <= (c + c) * \<bar>g xa\<bar>")
   180  apply (metis order_trans mult_2)
   180  apply (metis order_trans mult_2)
   181 apply (subgoal_tac "c * abs (f xa + g xa) <= c * (abs (f xa) + abs (g xa))")
   181 apply (subgoal_tac "c * \<bar>f xa + g xa\<bar> <= c * (\<bar>f xa\<bar> + \<bar>g xa\<bar>)")
   182  apply (erule order_trans)
   182  apply (erule order_trans)
   183  apply (simp add: ring_distribs)
   183  apply (simp add: ring_distribs)
   184 by (metis abs_triangle_ineq mult_le_cancel_left_pos)
   184 by (metis abs_triangle_ineq mult_le_cancel_left_pos)
   185 
   185 
   186 lemma bigo_plus_subset2 [intro]: "A <= O(f) \<Longrightarrow> B <= O(f) \<Longrightarrow> A + B <= O(f)"
   186 lemma bigo_plus_subset2 [intro]: "A <= O(f) \<Longrightarrow> B <= O(f) \<Longrightarrow> A + B <= O(f)"
   198  apply (metis less_max_iff_disj)
   198  apply (metis less_max_iff_disj)
   199 apply clarify
   199 apply clarify
   200 apply (drule_tac x = "xa" in spec)+
   200 apply (drule_tac x = "xa" in spec)+
   201 apply (subgoal_tac "0 <= f xa + g xa")
   201 apply (subgoal_tac "0 <= f xa + g xa")
   202  apply (simp add: ring_distribs)
   202  apply (simp add: ring_distribs)
   203  apply (subgoal_tac "abs (a xa + b xa) <= abs (a xa) + abs (b xa)")
   203  apply (subgoal_tac "\<bar>a xa + b xa\<bar> <= \<bar>a xa\<bar> + \<bar>b xa\<bar>")
   204   apply (subgoal_tac "abs (a xa) + abs (b xa) <=
   204   apply (subgoal_tac "\<bar>a xa\<bar> + \<bar>b xa\<bar> <= max c ca * f xa + max c ca * g xa")
   205            max c ca * f xa + max c ca * g xa")
       
   206    apply (metis order_trans)
   205    apply (metis order_trans)
   207   defer 1
   206   defer 1
   208   apply (metis abs_triangle_ineq)
   207   apply (metis abs_triangle_ineq)
   209  apply (metis add_nonneg_nonneg)
   208  apply (metis add_nonneg_nonneg)
   210 apply (rule add_mono)
   209 apply (rule add_mono)
   234  apply (metis add_le_cancel_left diff_add_cancel diff_self minus_apply
   233  apply (metis add_le_cancel_left diff_add_cancel diff_self minus_apply
   235               algebra_simps)
   234               algebra_simps)
   236 by (metis add_le_cancel_left diff_add_cancel func_plus le_fun_def
   235 by (metis add_le_cancel_left diff_add_cancel func_plus le_fun_def
   237           algebra_simps)
   236           algebra_simps)
   238 
   237 
   239 lemma bigo_abs: "(\<lambda>x. abs(f x)) =o O(f)"
   238 lemma bigo_abs: "(\<lambda>x. \<bar>f x\<bar>) =o O(f)"
   240 apply (unfold bigo_def)
   239 apply (unfold bigo_def)
   241 apply auto
   240 apply auto
   242 by (metis mult_1 order_refl)
   241 by (metis mult_1 order_refl)
   243 
   242 
   244 lemma bigo_abs2: "f =o O(\<lambda>x. abs(f x))"
   243 lemma bigo_abs2: "f =o O(\<lambda>x. \<bar>f x\<bar>)"
   245 apply (unfold bigo_def)
   244 apply (unfold bigo_def)
   246 apply auto
   245 apply auto
   247 by (metis mult_1 order_refl)
   246 by (metis mult_1 order_refl)
   248 
   247 
   249 lemma bigo_abs3: "O(f) = O(\<lambda>x. abs(f x))"
   248 lemma bigo_abs3: "O(f) = O(\<lambda>x. \<bar>f x\<bar>)"
   250 proof -
   249 proof -
   251   have F1: "\<forall>v u. u \<in> O(v) \<longrightarrow> O(u) \<subseteq> O(v)" by (metis bigo_elt_subset)
   250   have F1: "\<forall>v u. u \<in> O(v) \<longrightarrow> O(u) \<subseteq> O(v)" by (metis bigo_elt_subset)
   252   have F2: "\<forall>u. (\<lambda>R. \<bar>u R\<bar>) \<in> O(u)" by (metis bigo_abs)
   251   have F2: "\<forall>u. (\<lambda>R. \<bar>u R\<bar>) \<in> O(u)" by (metis bigo_abs)
   253   have "\<forall>u. u \<in> O(\<lambda>R. \<bar>u R\<bar>)" by (metis bigo_abs2)
   252   have "\<forall>u. u \<in> O(\<lambda>R. \<bar>u R\<bar>)" by (metis bigo_abs2)
   254   thus "O(f) = O(\<lambda>x. \<bar>f x\<bar>)" using F1 F2 by auto
   253   thus "O(f) = O(\<lambda>x. \<bar>f x\<bar>)" using F1 F2 by auto
   255 qed
   254 qed
   256 
   255 
   257 lemma bigo_abs4: "f =o g +o O(h) \<Longrightarrow> (\<lambda>x. abs (f x)) =o (\<lambda>x. abs (g x)) +o O(h)"
   256 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)"
   258   apply (drule set_plus_imp_minus)
   257   apply (drule set_plus_imp_minus)
   259   apply (rule set_minus_imp_plus)
   258   apply (rule set_minus_imp_plus)
   260   apply (subst fun_diff_def)
   259   apply (subst fun_diff_def)
   261 proof -
   260 proof -
   262   assume a: "f - g : O(h)"
   261   assume a: "f - g : O(h)"
   263   have "(\<lambda>x. abs (f x) - abs (g x)) =o O(\<lambda>x. abs(abs (f x) - abs (g x)))"
   262   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>)"
   264     by (rule bigo_abs2)
   263     by (rule bigo_abs2)
   265   also have "... <= O(\<lambda>x. abs (f x - g x))"
   264   also have "... <= O(\<lambda>x. \<bar>f x - g x\<bar>)"
   266     apply (rule bigo_elt_subset)
   265     apply (rule bigo_elt_subset)
   267     apply (rule bigo_bounded)
   266     apply (rule bigo_bounded)
   268      apply (metis abs_ge_zero)
   267      apply (metis abs_ge_zero)
   269     by (metis abs_triangle_ineq3)
   268     by (metis abs_triangle_ineq3)
   270   also have "... <= O(f - g)"
   269   also have "... <= O(f - g)"
   272     apply (subst fun_diff_def)
   271     apply (subst fun_diff_def)
   273     apply (rule bigo_abs)
   272     apply (rule bigo_abs)
   274     done
   273     done
   275   also have "... <= O(h)"
   274   also have "... <= O(h)"
   276     using a by (rule bigo_elt_subset)
   275     using a by (rule bigo_elt_subset)
   277   finally show "(\<lambda>x. abs (f x) - abs (g x)) : O(h)".
   276   finally show "(\<lambda>x. \<bar>f x\<bar> - \<bar>g x\<bar>) : O(h)" .
   278 qed
   277 qed
   279 
   278 
   280 lemma bigo_abs5: "f =o O(g) \<Longrightarrow> (\<lambda>x. abs(f x)) =o O(g)"
   279 lemma bigo_abs5: "f =o O(g) \<Longrightarrow> (\<lambda>x. \<bar>f x\<bar>) =o O(g)"
   281 by (unfold bigo_def, auto)
   280 by (unfold bigo_def, auto)
   282 
   281 
   283 lemma bigo_elt_subset2 [intro]: "f : g +o O(h) \<Longrightarrow> O(f) <= O(g) + O(h)"
   282 lemma bigo_elt_subset2 [intro]: "f : g +o O(h) \<Longrightarrow> O(f) <= O(g) + O(h)"
   284 proof -
   283 proof -
   285   assume "f : g +o O(h)"
   284   assume "f : g +o O(h)"
   286   also have "... <= O(g) + O(h)"
   285   also have "... <= O(g) + O(h)"
   287     by (auto del: subsetI)
   286     by (auto del: subsetI)
   288   also have "... = O(\<lambda>x. abs(g x)) + O(\<lambda>x. abs(h x))"
   287   also have "... = O(\<lambda>x. \<bar>g x\<bar>) + O(\<lambda>x. \<bar>h x\<bar>)"
   289     by (metis bigo_abs3)
   288     by (metis bigo_abs3)
   290   also have "... = O((\<lambda>x. abs(g x)) + (\<lambda>x. abs(h x)))"
   289   also have "... = O((\<lambda>x. \<bar>g x\<bar>) + (\<lambda>x. \<bar>h x\<bar>))"
   291     by (rule bigo_plus_eq [symmetric], auto)
   290     by (rule bigo_plus_eq [symmetric], auto)
   292   finally have "f : ...".
   291   finally have "f : ...".
   293   then have "O(f) <= ..."
   292   then have "O(f) <= ..."
   294     by (elim bigo_elt_subset)
   293     by (elim bigo_elt_subset)
   295   also have "... = O(\<lambda>x. abs(g x)) + O(\<lambda>x. abs(h x))"
   294   also have "... = O(\<lambda>x. \<bar>g x\<bar>) + O(\<lambda>x. \<bar>h x\<bar>)"
   296     by (rule bigo_plus_eq, auto)
   295     by (rule bigo_plus_eq, auto)
   297   finally show ?thesis
   296   finally show ?thesis
   298     by (simp add: bigo_abs3 [symmetric])
   297     by (simp add: bigo_abs3 [symmetric])
   299 qed
   298 qed
   300 
   299 
   305             simp add: bigo_alt_def set_times_def func_times)
   304             simp add: bigo_alt_def set_times_def func_times)
   306 (* sledgehammer *)
   305 (* sledgehammer *)
   307 apply (rule_tac x = "c * ca" in exI)
   306 apply (rule_tac x = "c * ca" in exI)
   308 apply (rule allI)
   307 apply (rule allI)
   309 apply (erule_tac x = x in allE)+
   308 apply (erule_tac x = x in allE)+
   310 apply (subgoal_tac "c * ca * abs (f x * g x) = (c * abs(f x)) * (ca * abs (g x))")
   309 apply (subgoal_tac "c * ca * \<bar>f x * g x\<bar> = (c * \<bar>f x\<bar>) * (ca * \<bar>g x\<bar>)")
   311  apply (metis (no_types) abs_ge_zero abs_mult mult_mono')
   310  apply (metis (no_types) abs_ge_zero abs_mult mult_mono')
   312 by (metis mult.assoc mult.left_commute abs_of_pos mult.left_commute abs_mult)
   311 by (metis mult.assoc mult.left_commute abs_of_pos mult.left_commute abs_mult)
   313 
   312 
   314 lemma bigo_mult2 [intro]: "f *o O(g) <= O(f * g)"
   313 lemma bigo_mult2 [intro]: "f *o O(g) <= O(f * g)"
   315 by (metis bigo_mult bigo_refl set_times_mono3 subset_trans)
   314 by (metis bigo_mult bigo_refl set_times_mono3 subset_trans)
   464 lemma bigo_const_mult6 [intro]: "(\<lambda>x. c) *o O(f) <= O(f)"
   463 lemma bigo_const_mult6 [intro]: "(\<lambda>x. c) *o O(f) <= O(f)"
   465   apply (auto intro!: subsetI
   464   apply (auto intro!: subsetI
   466     simp add: bigo_def elt_set_times_def func_times
   465     simp add: bigo_def elt_set_times_def func_times
   467     simp del: abs_mult ac_simps)
   466     simp del: abs_mult ac_simps)
   468 (* sledgehammer *)
   467 (* sledgehammer *)
   469   apply (rule_tac x = "ca * (abs c)" in exI)
   468   apply (rule_tac x = "ca * \<bar>c\<bar>" in exI)
   470   apply (rule allI)
   469   apply (rule allI)
   471   apply (subgoal_tac "ca * abs(c) * abs(f x) = abs(c) * (ca * abs(f x))")
   470   apply (subgoal_tac "ca * \<bar>c\<bar> * \<bar>f x\<bar> = \<bar>c\<bar> * (ca * \<bar>f x\<bar>)")
   472   apply (erule ssubst)
   471   apply (erule ssubst)
   473   apply (subst abs_mult)
   472   apply (subst abs_mult)
   474   apply (rule mult_left_mono)
   473   apply (rule mult_left_mono)
   475   apply (erule spec)
   474   apply (erule spec)
   476   apply simp
   475   apply simp
   491 done
   490 done
   492 
   491 
   493 subsection {* Setsum *}
   492 subsection {* Setsum *}
   494 
   493 
   495 lemma bigo_setsum_main: "\<forall>x. \<forall>y \<in> A x. 0 <= h x y \<Longrightarrow>
   494 lemma bigo_setsum_main: "\<forall>x. \<forall>y \<in> A x. 0 <= h x y \<Longrightarrow>
   496     \<exists>c. \<forall>x. \<forall>y \<in> A x. abs (f x y) <= c * (h x y) \<Longrightarrow>
   495     \<exists>c. \<forall>x. \<forall>y \<in> A x. \<bar>f x y\<bar> <= c * (h x y) \<Longrightarrow>
   497       (\<lambda>x. SUM y : A x. f x y) =o O(\<lambda>x. SUM y : A x. h x y)"
   496       (\<lambda>x. SUM y : A x. f x y) =o O(\<lambda>x. SUM y : A x. h x y)"
   498 apply (auto simp add: bigo_def)
   497 apply (auto simp add: bigo_def)
   499 apply (rule_tac x = "abs c" in exI)
   498 apply (rule_tac x = "\<bar>c\<bar>" in exI)
   500 apply (subst abs_of_nonneg) back back
   499 apply (subst abs_of_nonneg) back back
   501  apply (rule setsum_nonneg)
   500  apply (rule setsum_nonneg)
   502  apply force
   501  apply force
   503 apply (subst setsum_right_distrib)
   502 apply (subst setsum_right_distrib)
   504 apply (rule allI)
   503 apply (rule allI)
   506  apply (rule setsum_abs)
   505  apply (rule setsum_abs)
   507 apply (rule setsum_mono)
   506 apply (rule setsum_mono)
   508 by (metis abs_ge_self abs_mult_pos order_trans)
   507 by (metis abs_ge_self abs_mult_pos order_trans)
   509 
   508 
   510 lemma bigo_setsum1: "\<forall>x y. 0 <= h x y \<Longrightarrow>
   509 lemma bigo_setsum1: "\<forall>x y. 0 <= h x y \<Longrightarrow>
   511     \<exists>c. \<forall>x y. abs (f x y) <= c * (h x y) \<Longrightarrow>
   510     \<exists>c. \<forall>x y. \<bar>f x y\<bar> <= c * (h x y) \<Longrightarrow>
   512       (\<lambda>x. SUM y : A x. f x y) =o O(\<lambda>x. SUM y : A x. h x y)"
   511       (\<lambda>x. SUM y : A x. f x y) =o O(\<lambda>x. SUM y : A x. h x y)"
   513 by (metis (no_types) bigo_setsum_main)
   512 by (metis (no_types) bigo_setsum_main)
   514 
   513 
   515 lemma bigo_setsum2: "\<forall>y. 0 <= h y \<Longrightarrow>
   514 lemma bigo_setsum2: "\<forall>y. 0 <= h y \<Longrightarrow>
   516     \<exists>c. \<forall>y. abs (f y) <= c * (h y) \<Longrightarrow>
   515     \<exists>c. \<forall>y. \<bar>f y\<bar> <= c * (h y) \<Longrightarrow>
   517       (\<lambda>x. SUM y : A x. f y) =o O(\<lambda>x. SUM y : A x. h y)"
   516       (\<lambda>x. SUM y : A x. f y) =o O(\<lambda>x. SUM y : A x. h y)"
   518 apply (rule bigo_setsum1)
   517 apply (rule bigo_setsum1)
   519 by metis+
   518 by metis+
   520 
   519 
   521 lemma bigo_setsum3: "f =o O(h) \<Longrightarrow>
   520 lemma bigo_setsum3: "f =o O(h) \<Longrightarrow>
   522     (\<lambda>x. SUM y : A x. (l x y) * f(k x y)) =o
   521     (\<lambda>x. SUM y : A x. (l x y) * f(k x y)) =o
   523       O(\<lambda>x. SUM y : A x. abs(l x y * h(k x y)))"
   522       O(\<lambda>x. SUM y : A x. \<bar>l x y * h(k x y)\<bar>)"
   524 apply (rule bigo_setsum1)
   523 apply (rule bigo_setsum1)
   525  apply (rule allI)+
   524  apply (rule allI)+
   526  apply (rule abs_ge_zero)
   525  apply (rule abs_ge_zero)
   527 apply (unfold bigo_def)
   526 apply (unfold bigo_def)
   528 apply (auto simp add: abs_mult)
   527 apply (auto simp add: abs_mult)
   529 by (metis abs_ge_zero mult.left_commute mult_left_mono)
   528 by (metis abs_ge_zero mult.left_commute mult_left_mono)
   530 
   529 
   531 lemma bigo_setsum4: "f =o g +o O(h) \<Longrightarrow>
   530 lemma bigo_setsum4: "f =o g +o O(h) \<Longrightarrow>
   532     (\<lambda>x. SUM y : A x. l x y * f(k x y)) =o
   531     (\<lambda>x. SUM y : A x. l x y * f(k x y)) =o
   533       (\<lambda>x. SUM y : A x. l x y * g(k x y)) +o
   532       (\<lambda>x. SUM y : A x. l x y * g(k x y)) +o
   534         O(\<lambda>x. SUM y : A x. abs(l x y * h(k x y)))"
   533         O(\<lambda>x. SUM y : A x. \<bar>l x y * h(k x y)\<bar>)"
   535 apply (rule set_minus_imp_plus)
   534 apply (rule set_minus_imp_plus)
   536 apply (subst fun_diff_def)
   535 apply (subst fun_diff_def)
   537 apply (subst setsum_subtractf [symmetric])
   536 apply (subst setsum_subtractf [symmetric])
   538 apply (subst right_diff_distrib [symmetric])
   537 apply (subst right_diff_distrib [symmetric])
   539 apply (rule bigo_setsum3)
   538 apply (rule bigo_setsum3)
   542 lemma bigo_setsum5: "f =o O(h) \<Longrightarrow> \<forall>x y. 0 <= l x y \<Longrightarrow>
   541 lemma bigo_setsum5: "f =o O(h) \<Longrightarrow> \<forall>x y. 0 <= l x y \<Longrightarrow>
   543     \<forall>x. 0 <= h x \<Longrightarrow>
   542     \<forall>x. 0 <= h x \<Longrightarrow>
   544       (\<lambda>x. SUM y : A x. (l x y) * f(k x y)) =o
   543       (\<lambda>x. SUM y : A x. (l x y) * f(k x y)) =o
   545         O(\<lambda>x. SUM y : A x. (l x y) * h(k x y))"
   544         O(\<lambda>x. SUM y : A x. (l x y) * h(k x y))"
   546 apply (subgoal_tac "(\<lambda>x. SUM y : A x. (l x y) * h(k x y)) =
   545 apply (subgoal_tac "(\<lambda>x. SUM y : A x. (l x y) * h(k x y)) =
   547       (\<lambda>x. SUM y : A x. abs((l x y) * h(k x y)))")
   546       (\<lambda>x. SUM y : A x. \<bar>(l x y) * h(k x y)\<bar>)")
   548  apply (erule ssubst)
   547  apply (erule ssubst)
   549  apply (erule bigo_setsum3)
   548  apply (erule bigo_setsum3)
   550 apply (rule ext)
   549 apply (rule ext)
   551 apply (rule setsum.cong)
   550 apply (rule setsum.cong)
   552 apply (rule refl)
   551 apply (rule refl)
   614 subsection {* Less than or equal to *}
   613 subsection {* Less than or equal to *}
   615 
   614 
   616 definition lesso :: "('a => 'b::linordered_idom) => ('a => 'b) => ('a => 'b)" (infixl "<o" 70) where
   615 definition lesso :: "('a => 'b::linordered_idom) => ('a => 'b) => ('a => 'b)" (infixl "<o" 70) where
   617   "f <o g == (\<lambda>x. max (f x - g x) 0)"
   616   "f <o g == (\<lambda>x. max (f x - g x) 0)"
   618 
   617 
   619 lemma bigo_lesseq1: "f =o O(h) \<Longrightarrow> \<forall>x. abs (g x) <= abs (f x) \<Longrightarrow>
   618 lemma bigo_lesseq1: "f =o O(h) \<Longrightarrow> \<forall>x. \<bar>g x\<bar> <= \<bar>f x\<bar> \<Longrightarrow>
   620     g =o O(h)"
   619     g =o O(h)"
   621   apply (unfold bigo_def)
   620   apply (unfold bigo_def)
   622   apply clarsimp
   621   apply clarsimp
   623 apply (blast intro: order_trans)
   622 apply (blast intro: order_trans)
   624 done
   623 done
   625 
   624 
   626 lemma bigo_lesseq2: "f =o O(h) \<Longrightarrow> \<forall>x. abs (g x) <= f x \<Longrightarrow>
   625 lemma bigo_lesseq2: "f =o O(h) \<Longrightarrow> \<forall>x. \<bar>g x\<bar> <= f x \<Longrightarrow>
   627       g =o O(h)"
   626       g =o O(h)"
   628   apply (erule bigo_lesseq1)
   627   apply (erule bigo_lesseq1)
   629 apply (blast intro: abs_ge_self order_trans)
   628 apply (blast intro: abs_ge_self order_trans)
   630 done
   629 done
   631 
   630 
   636   apply (subst abs_of_nonneg)
   635   apply (subst abs_of_nonneg)
   637   apply (erule spec)+
   636   apply (erule spec)+
   638 done
   637 done
   639 
   638 
   640 lemma bigo_lesseq4: "f =o O(h) \<Longrightarrow>
   639 lemma bigo_lesseq4: "f =o O(h) \<Longrightarrow>
   641     \<forall>x. 0 <= g x \<Longrightarrow> \<forall>x. g x <= abs (f x) \<Longrightarrow>
   640     \<forall>x. 0 <= g x \<Longrightarrow> \<forall>x. g x <= \<bar>f x\<bar> \<Longrightarrow>
   642       g =o O(h)"
   641       g =o O(h)"
   643   apply (erule bigo_lesseq1)
   642   apply (erule bigo_lesseq1)
   644   apply (rule allI)
   643   apply (rule allI)
   645   apply (subst abs_of_nonneg)
   644   apply (subst abs_of_nonneg)
   646   apply (erule spec)+
   645   apply (erule spec)+
   701 apply (erule bigo_lesseq2) back
   700 apply (erule bigo_lesseq2) back
   702 apply (rule allI)
   701 apply (rule allI)
   703 by (auto simp add: func_plus fun_diff_def algebra_simps
   702 by (auto simp add: func_plus fun_diff_def algebra_simps
   704     split: split_max abs_split)
   703     split: split_max abs_split)
   705 
   704 
   706 lemma bigo_lesso5: "f <o g =o O(h) \<Longrightarrow> \<exists>C. \<forall>x. f x <= g x + C * abs (h x)"
   705 lemma bigo_lesso5: "f <o g =o O(h) \<Longrightarrow> \<exists>C. \<forall>x. f x <= g x + C * \<bar>h x\<bar>"
   707 apply (simp only: lesso_def bigo_alt_def)
   706 apply (simp only: lesso_def bigo_alt_def)
   708 apply clarsimp
   707 apply clarsimp
   709 by (metis add.commute diff_le_eq)
   708 by (metis add.commute diff_le_eq)
   710 
   709 
   711 end
   710 end