src/HOL/Metis_Examples/Big_O.thy
author blanchet
Thu Jun 12 17:10:12 2014 +0200 (2014-06-12)
changeset 57245 f6bf6d5341ee
parent 56536 aefb4a8da31f
child 57418 6ab1c7cb0b8d
permissions -rw-r--r--
renamed Sledgehammer options
     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) => ('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_proofs, compress = 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\<^sub>1\<Colon>'a\<Colon>linordered_idom. 0 \<le> \<bar>x\<^sub>1\<bar>" by (metis abs_ge_zero)
    46   have F2: "\<forall>x\<^sub>1\<Colon>'a\<Colon>linordered_idom. 1 * x\<^sub>1 = x\<^sub>1" by (metis mult_1)
    47   have F3: "\<forall>x\<^sub>1 x\<^sub>3. x\<^sub>3 \<le> \<bar>h x\<^sub>1\<bar> \<longrightarrow> x\<^sub>3 \<le> c * \<bar>f x\<^sub>1\<bar>" by (metis A1 order_trans)
    48   have F4: "\<forall>x\<^sub>2 x\<^sub>3\<Colon>'a\<Colon>linordered_idom. \<bar>x\<^sub>3\<bar> * \<bar>x\<^sub>2\<bar> = \<bar>x\<^sub>3 * x\<^sub>2\<bar>"
    49     by (metis abs_mult)
    50   have F5: "\<forall>x\<^sub>3 x\<^sub>1\<Colon>'a\<Colon>linordered_idom. 0 \<le> x\<^sub>1 \<longrightarrow> \<bar>x\<^sub>3 * x\<^sub>1\<bar> = \<bar>x\<^sub>3\<bar> * x\<^sub>1"
    51     by (metis abs_mult_pos)
    52   hence "\<forall>x\<^sub>1\<ge>0. \<bar>x\<^sub>1\<Colon>'a\<Colon>linordered_idom\<bar> = \<bar>1\<bar> * x\<^sub>1" by (metis F2)
    53   hence "\<forall>x\<^sub>1\<ge>0. \<bar>x\<^sub>1\<Colon>'a\<Colon>linordered_idom\<bar> = x\<^sub>1" by (metis F2 abs_one)
    54   hence "\<forall>x\<^sub>3. 0 \<le> \<bar>h x\<^sub>3\<bar> \<longrightarrow> \<bar>c * \<bar>f x\<^sub>3\<bar>\<bar> = c * \<bar>f x\<^sub>3\<bar>" by (metis F3)
    55   hence "\<forall>x\<^sub>3. \<bar>c * \<bar>f x\<^sub>3\<bar>\<bar> = c * \<bar>f x\<^sub>3\<bar>" by (metis F1)
    56   hence "\<forall>x\<^sub>3. (0\<Colon>'a) \<le> \<bar>f x\<^sub>3\<bar> \<longrightarrow> c * \<bar>f x\<^sub>3\<bar> = \<bar>c\<bar> * \<bar>f x\<^sub>3\<bar>" by (metis F5)
    57   hence "\<forall>x\<^sub>3. (0\<Colon>'a) \<le> \<bar>f x\<^sub>3\<bar> \<longrightarrow> c * \<bar>f x\<^sub>3\<bar> = \<bar>c * f x\<^sub>3\<bar>" by (metis F4)
    58   hence "\<forall>x\<^sub>3. c * \<bar>f x\<^sub>3\<bar> = \<bar>c * f x\<^sub>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_proofs, compress = 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\<^sub>1\<Colon>'a\<Colon>linordered_idom. 1 * x\<^sub>1 = x\<^sub>1" by (metis mult_1)
    77   have F2: "\<forall>x\<^sub>2 x\<^sub>3\<Colon>'a\<Colon>linordered_idom. \<bar>x\<^sub>3\<bar> * \<bar>x\<^sub>2\<bar> = \<bar>x\<^sub>3 * x\<^sub>2\<bar>"
    78     by (metis abs_mult)
    79   have "\<forall>x\<^sub>1\<ge>0. \<bar>x\<^sub>1\<Colon>'a\<Colon>linordered_idom\<bar> = x\<^sub>1" by (metis F1 abs_mult_pos abs_one)
    80   hence "\<forall>x\<^sub>3. \<bar>c * \<bar>f x\<^sub>3\<bar>\<bar> = c * \<bar>f x\<^sub>3\<bar>" by (metis A1 abs_ge_zero order_trans)
    81   hence "\<forall>x\<^sub>3. 0 \<le> \<bar>f x\<^sub>3\<bar> \<longrightarrow> c * \<bar>f x\<^sub>3\<bar> = \<bar>c * f x\<^sub>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_proofs, compress = 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\<^sub>1\<Colon>'a\<Colon>linordered_idom. 1 * x\<^sub>1 = x\<^sub>1" by (metis mult_1)
   100   have F2: "\<forall>x\<^sub>3 x\<^sub>1\<Colon>'a\<Colon>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)
   101   hence "\<forall>x\<^sub>1\<ge>0. \<bar>x\<^sub>1\<Colon>'a\<Colon>linordered_idom\<bar> = x\<^sub>1" by (metis F1 abs_one)
   102   hence "\<forall>x\<^sub>3. 0 \<le> \<bar>f x\<^sub>3\<bar> \<longrightarrow> c * \<bar>f x\<^sub>3\<bar> = \<bar>c\<bar> * \<bar>f x\<^sub>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_ge_zero)
   104 qed
   105 
   106 sledgehammer_params [isar_proofs, compress = 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\<^sub>1\<Colon>'a\<Colon>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>"
   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_proofs, compress = 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) + 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) + 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) + 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 (subgoal_tac "c * abs (f xa + g xa) <= (c + c) * abs (f xa)")
   168   apply (metis mult_2 order_trans)
   169  apply (subgoal_tac "c * abs (f xa + g xa) <= c * (abs (f xa) + abs (g xa))")
   170   apply (erule order_trans)
   171   apply (simp add: ring_distribs)
   172  apply (rule mult_left_mono)
   173   apply (simp add: abs_triangle_ineq)
   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)
   176 apply (rule conjI)
   177  apply (rule_tac x = "c + c" in exI)
   178  apply auto
   179 apply (subgoal_tac "c * abs (f xa + g xa) <= (c + c) * abs (g xa)")
   180  apply (metis order_trans mult_2)
   181 apply (subgoal_tac "c * abs (f xa + g xa) <= c * (abs (f xa) + abs (g xa))")
   182  apply (erule order_trans)
   183  apply (simp add: ring_distribs)
   184 by (metis abs_triangle_ineq mult_le_cancel_left_pos)
   185 
   186 lemma bigo_plus_subset2 [intro]: "A <= O(f) \<Longrightarrow> B <= O(f) \<Longrightarrow> A + B <= O(f)"
   187 by (metis bigo_plus_idemp set_plus_mono2)
   188 
   189 lemma bigo_plus_eq: "\<forall>x. 0 <= f x \<Longrightarrow> \<forall>x. 0 <= g x \<Longrightarrow> O(f + g) = O(f) + O(g)"
   190 apply (rule equalityI)
   191 apply (rule bigo_plus_subset)
   192 apply (simp add: bigo_alt_def set_plus_def func_plus)
   193 apply clarify
   194 (* sledgehammer *)
   195 apply (rule_tac x = "max c ca" in exI)
   196 
   197 apply (rule conjI)
   198  apply (metis less_max_iff_disj)
   199 apply clarify
   200 apply (drule_tac x = "xa" in spec)+
   201 apply (subgoal_tac "0 <= f xa + g xa")
   202  apply (simp add: ring_distribs)
   203  apply (subgoal_tac "abs (a xa + b xa) <= abs (a xa) + abs (b xa)")
   204   apply (subgoal_tac "abs (a xa) + abs (b xa) <=
   205            max c ca * f xa + max c ca * g xa")
   206    apply (metis order_trans)
   207   defer 1
   208   apply (metis abs_triangle_ineq)
   209  apply (metis add_nonneg_nonneg)
   210 apply (rule add_mono)
   211  apply (metis max.cobounded2 linorder_linear max.absorb1 mult_right_mono xt1(6))
   212 by (metis max.cobounded2 linorder_not_le mult_le_cancel_right order_trans)
   213 
   214 lemma bigo_bounded_alt: "\<forall>x. 0 <= f x \<Longrightarrow> \<forall>x. f x <= c * g x \<Longrightarrow> f : O(g)"
   215 apply (auto simp add: bigo_def)
   216 (* Version 1: one-line proof *)
   217 by (metis abs_le_D1 linorder_class.not_less order_less_le Orderings.xt1(12) abs_mult)
   218 
   219 lemma "\<forall>x. 0 <= f x \<Longrightarrow> \<forall>x. f x <= c * g x \<Longrightarrow> f : O(g)"
   220 apply (auto simp add: bigo_def)
   221 (* Version 2: structured proof *)
   222 proof -
   223   assume "\<forall>x. f x \<le> c * g x"
   224   thus "\<exists>c. \<forall>x. f x \<le> c * \<bar>g x\<bar>" by (metis abs_mult abs_ge_self order_trans)
   225 qed
   226 
   227 lemma bigo_bounded: "\<forall>x. 0 <= f x \<Longrightarrow> \<forall>x. f x <= g x \<Longrightarrow> f : O(g)"
   228 apply (erule bigo_bounded_alt [of f 1 g])
   229 by (metis mult_1)
   230 
   231 lemma bigo_bounded2: "\<forall>x. lb x <= f x \<Longrightarrow> \<forall>x. f x <= lb x + g x \<Longrightarrow> f : lb +o O(g)"
   232 apply (rule set_minus_imp_plus)
   233 apply (rule bigo_bounded)
   234  apply (metis add_le_cancel_left diff_add_cancel diff_self minus_apply
   235               comm_semiring_1_class.normalizing_semiring_rules(24))
   236 by (metis add_le_cancel_left diff_add_cancel func_plus le_fun_def
   237           comm_semiring_1_class.normalizing_semiring_rules(24))
   238 
   239 lemma bigo_abs: "(\<lambda>x. abs(f x)) =o O(f)"
   240 apply (unfold bigo_def)
   241 apply auto
   242 by (metis mult_1 order_refl)
   243 
   244 lemma bigo_abs2: "f =o O(\<lambda>x. abs(f x))"
   245 apply (unfold bigo_def)
   246 apply auto
   247 by (metis mult_1 order_refl)
   248 
   249 lemma bigo_abs3: "O(f) = O(\<lambda>x. abs(f x))"
   250 proof -
   251   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)
   253   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
   255 qed
   256 
   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)"
   258   apply (drule set_plus_imp_minus)
   259   apply (rule set_minus_imp_plus)
   260   apply (subst fun_diff_def)
   261 proof -
   262   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)))"
   264     by (rule bigo_abs2)
   265   also have "... <= O(\<lambda>x. abs (f x - g x))"
   266     apply (rule bigo_elt_subset)
   267     apply (rule bigo_bounded)
   268      apply (metis abs_ge_zero)
   269     by (metis abs_triangle_ineq3)
   270   also have "... <= O(f - g)"
   271     apply (rule bigo_elt_subset)
   272     apply (subst fun_diff_def)
   273     apply (rule bigo_abs)
   274     done
   275   also have "... <= O(h)"
   276     using a by (rule bigo_elt_subset)
   277   finally show "(\<lambda>x. abs (f x) - abs (g x)) : O(h)".
   278 qed
   279 
   280 lemma bigo_abs5: "f =o O(g) \<Longrightarrow> (\<lambda>x. abs(f x)) =o O(g)"
   281 by (unfold bigo_def, auto)
   282 
   283 lemma bigo_elt_subset2 [intro]: "f : g +o O(h) \<Longrightarrow> O(f) <= O(g) + O(h)"
   284 proof -
   285   assume "f : g +o O(h)"
   286   also have "... <= O(g) + O(h)"
   287     by (auto del: subsetI)
   288   also have "... = O(\<lambda>x. abs(g x)) + O(\<lambda>x. abs(h x))"
   289     by (metis bigo_abs3)
   290   also have "... = O((\<lambda>x. abs(g x)) + (\<lambda>x. abs(h x)))"
   291     by (rule bigo_plus_eq [symmetric], auto)
   292   finally have "f : ...".
   293   then have "O(f) <= ..."
   294     by (elim bigo_elt_subset)
   295   also have "... = O(\<lambda>x. abs(g x)) + O(\<lambda>x. abs(h x))"
   296     by (rule bigo_plus_eq, auto)
   297   finally show ?thesis
   298     by (simp add: bigo_abs3 [symmetric])
   299 qed
   300 
   301 lemma bigo_mult [intro]: "O(f) * O(g) <= O(f * g)"
   302 apply (rule subsetI)
   303 apply (subst bigo_def)
   304 apply (auto simp del: abs_mult mult_ac
   305             simp add: bigo_alt_def set_times_def func_times)
   306 (* sledgehammer *)
   307 apply (rule_tac x = "c * ca" in exI)
   308 apply (rule allI)
   309 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))")
   311  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)
   313 
   314 lemma bigo_mult2 [intro]: "f *o O(g) <= O(f * g)"
   315 by (metis bigo_mult bigo_refl set_times_mono3 subset_trans)
   316 
   317 lemma bigo_mult3: "f : O(h) \<Longrightarrow> g : O(j) \<Longrightarrow> f * g : O(h * j)"
   318 by (metis bigo_mult set_rev_mp set_times_intro)
   319 
   320 lemma bigo_mult4 [intro]:"f : k +o O(h) \<Longrightarrow> g * f : (g * k) +o O(g * h)"
   321 by (metis bigo_mult2 set_plus_mono_b set_times_intro2 set_times_plus_distrib)
   322 
   323 lemma bigo_mult5: "\<forall>x. f x ~= 0 \<Longrightarrow>
   324     O(f * g) <= (f\<Colon>'a => ('b\<Colon>linordered_field)) *o O(g)"
   325 proof -
   326   assume a: "\<forall>x. f x ~= 0"
   327   show "O(f * g) <= f *o O(g)"
   328   proof
   329     fix h
   330     assume h: "h : O(f * g)"
   331     then have "(\<lambda>x. 1 / (f x)) * h : (\<lambda>x. 1 / f x) *o O(f * g)"
   332       by auto
   333     also have "... <= O((\<lambda>x. 1 / f x) * (f * g))"
   334       by (rule bigo_mult2)
   335     also have "(\<lambda>x. 1 / f x) * (f * g) = g"
   336       apply (simp add: func_times)
   337       by (metis (lifting, no_types) a ext mult_ac(2) nonzero_divide_eq_eq)
   338     finally have "(\<lambda>x. (1\<Colon>'b) / f x) * h : O(g)".
   339     then have "f * ((\<lambda>x. (1\<Colon>'b) / f x) * h) : f *o O(g)"
   340       by auto
   341     also have "f * ((\<lambda>x. (1\<Colon>'b) / f x) * h) = h"
   342       apply (simp add: func_times)
   343       by (metis (lifting, no_types) a eq_divide_imp ext
   344                 comm_semiring_1_class.normalizing_semiring_rules(7))
   345     finally show "h : f *o O(g)".
   346   qed
   347 qed
   348 
   349 lemma bigo_mult6:
   350 "\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) = (f\<Colon>'a \<Rightarrow> ('b\<Colon>linordered_field)) *o O(g)"
   351 by (metis bigo_mult2 bigo_mult5 order_antisym)
   352 
   353 (*proof requires relaxing relevance: 2007-01-25*)
   354 declare bigo_mult6 [simp]
   355 
   356 lemma bigo_mult7:
   357 "\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) \<le> O(f\<Colon>'a \<Rightarrow> ('b\<Colon>linordered_field)) * O(g)"
   358 by (metis bigo_refl bigo_mult6 set_times_mono3)
   359 
   360 declare bigo_mult6 [simp del]
   361 declare bigo_mult7 [intro!]
   362 
   363 lemma bigo_mult8:
   364 "\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) = O(f\<Colon>'a \<Rightarrow> ('b\<Colon>linordered_field)) * O(g)"
   365 by (metis bigo_mult bigo_mult7 order_antisym_conv)
   366 
   367 lemma bigo_minus [intro]: "f : O(g) \<Longrightarrow> - f : O(g)"
   368 by (auto simp add: bigo_def fun_Compl_def)
   369 
   370 lemma bigo_minus2: "f : g +o O(h) \<Longrightarrow> -f : -g +o O(h)"
   371 by (metis (no_types) bigo_elt_subset bigo_minus bigo_mult4 bigo_refl
   372           comm_semiring_1_class.normalizing_semiring_rules(11) minus_mult_left
   373           set_plus_mono_b)
   374 
   375 lemma bigo_minus3: "O(-f) = O(f)"
   376 by (metis bigo_elt_subset bigo_minus bigo_refl equalityI minus_minus)
   377 
   378 lemma bigo_plus_absorb_lemma1: "f : O(g) \<Longrightarrow> f +o O(g) \<le> O(g)"
   379 by (metis bigo_plus_idemp set_plus_mono3)
   380 
   381 lemma bigo_plus_absorb_lemma2: "f : O(g) \<Longrightarrow> O(g) \<le> f +o O(g)"
   382 by (metis (no_types) bigo_minus bigo_plus_absorb_lemma1 right_minus
   383           set_plus_mono set_plus_rearrange2 set_zero_plus subsetD subset_refl
   384           subset_trans)
   385 
   386 lemma bigo_plus_absorb [simp]: "f : O(g) \<Longrightarrow> f +o O(g) = O(g)"
   387 by (metis bigo_plus_absorb_lemma1 bigo_plus_absorb_lemma2 order_eq_iff)
   388 
   389 lemma bigo_plus_absorb2 [intro]: "f : O(g) \<Longrightarrow> A <= O(g) \<Longrightarrow> f +o A \<le> O(g)"
   390 by (metis bigo_plus_absorb set_plus_mono)
   391 
   392 lemma bigo_add_commute_imp: "f : g +o O(h) \<Longrightarrow> g : f +o O(h)"
   393 by (metis bigo_minus minus_diff_eq set_plus_imp_minus set_minus_plus)
   394 
   395 lemma bigo_add_commute: "(f : g +o O(h)) = (g : f +o O(h))"
   396 by (metis bigo_add_commute_imp)
   397 
   398 lemma bigo_const1: "(\<lambda>x. c) : O(\<lambda>x. 1)"
   399 by (auto simp add: bigo_def mult_ac)
   400 
   401 lemma bigo_const2 [intro]: "O(\<lambda>x. c) \<le> O(\<lambda>x. 1)"
   402 by (metis bigo_const1 bigo_elt_subset)
   403 
   404 lemma bigo_const3: "(c\<Colon>'a\<Colon>linordered_field) ~= 0 \<Longrightarrow> (\<lambda>x. 1) : O(\<lambda>x. c)"
   405 apply (simp add: bigo_def)
   406 by (metis abs_eq_0 left_inverse order_refl)
   407 
   408 lemma bigo_const4: "(c\<Colon>'a\<Colon>linordered_field) ~= 0 \<Longrightarrow> O(\<lambda>x. 1) <= O(\<lambda>x. c)"
   409 by (metis bigo_elt_subset bigo_const3)
   410 
   411 lemma bigo_const [simp]: "(c\<Colon>'a\<Colon>linordered_field) ~= 0 \<Longrightarrow>
   412     O(\<lambda>x. c) = O(\<lambda>x. 1)"
   413 by (metis bigo_const2 bigo_const4 equalityI)
   414 
   415 lemma bigo_const_mult1: "(\<lambda>x. c * f x) : O(f)"
   416 apply (simp add: bigo_def abs_mult)
   417 by (metis le_less)
   418 
   419 lemma bigo_const_mult2: "O(\<lambda>x. c * f x) \<le> O(f)"
   420 by (rule bigo_elt_subset, rule bigo_const_mult1)
   421 
   422 lemma bigo_const_mult3: "(c\<Colon>'a\<Colon>linordered_field) ~= 0 \<Longrightarrow> f : O(\<lambda>x. c * f x)"
   423 apply (simp add: bigo_def)
   424 by (metis (no_types) abs_mult mult_assoc mult_1 order_refl left_inverse)
   425 
   426 lemma bigo_const_mult4:
   427 "(c\<Colon>'a\<Colon>linordered_field) \<noteq> 0 \<Longrightarrow> O(f) \<le> O(\<lambda>x. c * f x)"
   428 by (metis bigo_elt_subset bigo_const_mult3)
   429 
   430 lemma bigo_const_mult [simp]: "(c\<Colon>'a\<Colon>linordered_field) ~= 0 \<Longrightarrow>
   431     O(\<lambda>x. c * f x) = O(f)"
   432 by (metis equalityI bigo_const_mult2 bigo_const_mult4)
   433 
   434 lemma bigo_const_mult5 [simp]: "(c\<Colon>'a\<Colon>linordered_field) ~= 0 \<Longrightarrow>
   435     (\<lambda>x. c) *o O(f) = O(f)"
   436   apply (auto del: subsetI)
   437   apply (rule order_trans)
   438   apply (rule bigo_mult2)
   439   apply (simp add: func_times)
   440   apply (auto intro!: subsetI simp add: bigo_def elt_set_times_def func_times)
   441   apply (rule_tac x = "\<lambda>y. inverse c * x y" in exI)
   442   apply (rename_tac g d)
   443   apply safe
   444   apply (rule_tac [2] ext)
   445    prefer 2
   446    apply simp
   447   apply (simp add: mult_assoc [symmetric] abs_mult)
   448   (* couldn't get this proof without the step above *)
   449 proof -
   450   fix g :: "'b \<Rightarrow> 'a" and d :: 'a
   451   assume A1: "c \<noteq> (0\<Colon>'a)"
   452   assume A2: "\<forall>x\<Colon>'b. \<bar>g x\<bar> \<le> d * \<bar>f x\<bar>"
   453   have F1: "inverse \<bar>c\<bar> = \<bar>inverse c\<bar>" using A1 by (metis nonzero_abs_inverse)
   454   have F2: "(0\<Colon>'a) < \<bar>c\<bar>" using A1 by (metis zero_less_abs_iff)
   455   have "(0\<Colon>'a) < \<bar>c\<bar> \<longrightarrow> (0\<Colon>'a) < \<bar>inverse c\<bar>" using F1 by (metis positive_imp_inverse_positive)
   456   hence "(0\<Colon>'a) < \<bar>inverse c\<bar>" using F2 by metis
   457   hence F3: "(0\<Colon>'a) \<le> \<bar>inverse c\<bar>" by (metis order_le_less)
   458   have "\<exists>(u\<Colon>'a) SKF\<^sub>7\<Colon>'a \<Rightarrow> 'b. \<bar>g (SKF\<^sub>7 (\<bar>inverse c\<bar> * u))\<bar> \<le> u * \<bar>f (SKF\<^sub>7 (\<bar>inverse c\<bar> * u))\<bar>"
   459     using A2 by metis
   460   hence F4: "\<exists>(u\<Colon>'a) SKF\<^sub>7\<Colon>'a \<Rightarrow> 'b. \<bar>g (SKF\<^sub>7 (\<bar>inverse c\<bar> * u))\<bar> \<le> u * \<bar>f (SKF\<^sub>7 (\<bar>inverse c\<bar> * u))\<bar> \<and> (0\<Colon>'a) \<le> \<bar>inverse c\<bar>"
   461     using F3 by metis
   462   hence "\<exists>(v\<Colon>'a) (u\<Colon>'a) SKF\<^sub>7\<Colon>'a \<Rightarrow> 'b. \<bar>inverse c\<bar> * \<bar>g (SKF\<^sub>7 (u * v))\<bar> \<le> u * (v * \<bar>f (SKF\<^sub>7 (u * v))\<bar>)"
   463     by (metis comm_mult_left_mono)
   464   thus "\<exists>ca\<Colon>'a. \<forall>x\<Colon>'b. \<bar>inverse c\<bar> * \<bar>g x\<bar> \<le> ca * \<bar>f x\<bar>"
   465     using A2 F4 by (metis ab_semigroup_mult_class.mult_ac(1) comm_mult_left_mono)
   466 qed
   467 
   468 lemma bigo_const_mult6 [intro]: "(\<lambda>x. c) *o O(f) <= O(f)"
   469   apply (auto intro!: subsetI
   470     simp add: bigo_def elt_set_times_def func_times
   471     simp del: abs_mult mult_ac)
   472 (* sledgehammer *)
   473   apply (rule_tac x = "ca * (abs c)" in exI)
   474   apply (rule allI)
   475   apply (subgoal_tac "ca * abs(c) * abs(f x) = abs(c) * (ca * abs(f x))")
   476   apply (erule ssubst)
   477   apply (subst abs_mult)
   478   apply (rule mult_left_mono)
   479   apply (erule spec)
   480   apply simp
   481   apply (simp add: mult_ac)
   482 done
   483 
   484 lemma bigo_const_mult7 [intro]: "f =o O(g) \<Longrightarrow> (\<lambda>x. c * f x) =o O(g)"
   485 by (metis bigo_const_mult1 bigo_elt_subset order_less_le psubsetD)
   486 
   487 lemma bigo_compose1: "f =o O(g) \<Longrightarrow> (\<lambda>x. f(k x)) =o O(\<lambda>x. g(k x))"
   488 by (unfold bigo_def, auto)
   489 
   490 lemma bigo_compose2:
   491 "f =o g +o O(h) \<Longrightarrow> (\<lambda>x. f(k x)) =o (\<lambda>x. g(k x)) +o O(\<lambda>x. h(k x))"
   492 apply (simp only: set_minus_plus [symmetric] fun_Compl_def func_plus)
   493 apply (drule bigo_compose1 [of "f - g" h k])
   494 apply (simp add: fun_diff_def)
   495 done
   496 
   497 subsection {* Setsum *}
   498 
   499 lemma bigo_setsum_main: "\<forall>x. \<forall>y \<in> A x. 0 <= h x y \<Longrightarrow>
   500     \<exists>c. \<forall>x. \<forall>y \<in> A x. abs (f x y) <= c * (h x y) \<Longrightarrow>
   501       (\<lambda>x. SUM y : A x. f x y) =o O(\<lambda>x. SUM y : A x. h x y)"
   502 apply (auto simp add: bigo_def)
   503 apply (rule_tac x = "abs c" in exI)
   504 apply (subst abs_of_nonneg) back back
   505  apply (rule setsum_nonneg)
   506  apply force
   507 apply (subst setsum_right_distrib)
   508 apply (rule allI)
   509 apply (rule order_trans)
   510  apply (rule setsum_abs)
   511 apply (rule setsum_mono)
   512 by (metis abs_ge_self abs_mult_pos order_trans)
   513 
   514 lemma bigo_setsum1: "\<forall>x y. 0 <= h x y \<Longrightarrow>
   515     \<exists>c. \<forall>x y. abs (f x y) <= c * (h x y) \<Longrightarrow>
   516       (\<lambda>x. SUM y : A x. f x y) =o O(\<lambda>x. SUM y : A x. h x y)"
   517 by (metis (no_types) bigo_setsum_main)
   518 
   519 lemma bigo_setsum2: "\<forall>y. 0 <= h y \<Longrightarrow>
   520     \<exists>c. \<forall>y. abs (f y) <= c * (h y) \<Longrightarrow>
   521       (\<lambda>x. SUM y : A x. f y) =o O(\<lambda>x. SUM y : A x. h y)"
   522 apply (rule bigo_setsum1)
   523 by metis+
   524 
   525 lemma bigo_setsum3: "f =o O(h) \<Longrightarrow>
   526     (\<lambda>x. SUM y : A x. (l x y) * f(k x y)) =o
   527       O(\<lambda>x. SUM y : A x. abs(l x y * h(k x y)))"
   528 apply (rule bigo_setsum1)
   529  apply (rule allI)+
   530  apply (rule abs_ge_zero)
   531 apply (unfold bigo_def)
   532 apply (auto simp add: abs_mult)
   533 by (metis abs_ge_zero mult_left_commute mult_left_mono)
   534 
   535 lemma bigo_setsum4: "f =o g +o O(h) \<Longrightarrow>
   536     (\<lambda>x. SUM y : A x. l x y * f(k x y)) =o
   537       (\<lambda>x. SUM y : A x. l x y * g(k x y)) +o
   538         O(\<lambda>x. SUM y : A x. abs(l x y * h(k x y)))"
   539 apply (rule set_minus_imp_plus)
   540 apply (subst fun_diff_def)
   541 apply (subst setsum_subtractf [symmetric])
   542 apply (subst right_diff_distrib [symmetric])
   543 apply (rule bigo_setsum3)
   544 by (metis (lifting, no_types) fun_diff_def set_plus_imp_minus ext)
   545 
   546 lemma bigo_setsum5: "f =o O(h) \<Longrightarrow> \<forall>x y. 0 <= l x y \<Longrightarrow>
   547     \<forall>x. 0 <= h x \<Longrightarrow>
   548       (\<lambda>x. SUM y : A x. (l x y) * f(k x y)) =o
   549         O(\<lambda>x. SUM y : A x. (l x y) * h(k x y))"
   550 apply (subgoal_tac "(\<lambda>x. SUM y : A x. (l x y) * h(k x y)) =
   551       (\<lambda>x. SUM y : A x. abs((l x y) * h(k x y)))")
   552  apply (erule ssubst)
   553  apply (erule bigo_setsum3)
   554 apply (rule ext)
   555 apply (rule setsum_cong2)
   556 by (metis abs_of_nonneg zero_le_mult_iff)
   557 
   558 lemma bigo_setsum6: "f =o g +o O(h) \<Longrightarrow> \<forall>x y. 0 <= l x y \<Longrightarrow>
   559     \<forall>x. 0 <= h x \<Longrightarrow>
   560       (\<lambda>x. SUM y : A x. (l x y) * f(k x y)) =o
   561         (\<lambda>x. SUM y : A x. (l x y) * g(k x y)) +o
   562           O(\<lambda>x. SUM y : A x. (l x y) * h(k x y))"
   563   apply (rule set_minus_imp_plus)
   564   apply (subst fun_diff_def)
   565   apply (subst setsum_subtractf [symmetric])
   566   apply (subst right_diff_distrib [symmetric])
   567   apply (rule bigo_setsum5)
   568   apply (subst fun_diff_def [symmetric])
   569   apply (drule set_plus_imp_minus)
   570   apply auto
   571 done
   572 
   573 subsection {* Misc useful stuff *}
   574 
   575 lemma bigo_useful_intro: "A <= O(f) \<Longrightarrow> B <= O(f) \<Longrightarrow>
   576   A + B <= O(f)"
   577   apply (subst bigo_plus_idemp [symmetric])
   578   apply (rule set_plus_mono2)
   579   apply assumption+
   580 done
   581 
   582 lemma bigo_useful_add: "f =o O(h) \<Longrightarrow> g =o O(h) \<Longrightarrow> f + g =o O(h)"
   583   apply (subst bigo_plus_idemp [symmetric])
   584   apply (rule set_plus_intro)
   585   apply assumption+
   586 done
   587 
   588 lemma bigo_useful_const_mult: "(c\<Colon>'a\<Colon>linordered_field) ~= 0 \<Longrightarrow>
   589     (\<lambda>x. c) * f =o O(h) \<Longrightarrow> f =o O(h)"
   590   apply (rule subsetD)
   591   apply (subgoal_tac "(\<lambda>x. 1 / c) *o O(h) <= O(h)")
   592   apply assumption
   593   apply (rule bigo_const_mult6)
   594   apply (subgoal_tac "f = (\<lambda>x. 1 / c) * ((\<lambda>x. c) * f)")
   595   apply (erule ssubst)
   596   apply (erule set_times_intro2)
   597   apply (simp add: func_times)
   598 done
   599 
   600 lemma bigo_fix: "(\<lambda>x. f ((x\<Colon>nat) + 1)) =o O(\<lambda>x. h(x + 1)) \<Longrightarrow> f 0 = 0 \<Longrightarrow>
   601     f =o O(h)"
   602 apply (simp add: bigo_alt_def)
   603 by (metis abs_ge_zero abs_mult abs_of_pos abs_zero not0_implies_Suc)
   604 
   605 lemma bigo_fix2:
   606     "(\<lambda>x. f ((x\<Colon>nat) + 1)) =o (\<lambda>x. g(x + 1)) +o O(\<lambda>x. h(x + 1)) \<Longrightarrow>
   607        f 0 = g 0 \<Longrightarrow> f =o g +o O(h)"
   608   apply (rule set_minus_imp_plus)
   609   apply (rule bigo_fix)
   610   apply (subst fun_diff_def)
   611   apply (subst fun_diff_def [symmetric])
   612   apply (rule set_plus_imp_minus)
   613   apply simp
   614   apply (simp add: fun_diff_def)
   615 done
   616 
   617 subsection {* Less than or equal to *}
   618 
   619 definition lesso :: "('a => 'b\<Colon>linordered_idom) => ('a => 'b) => ('a => 'b)" (infixl "<o" 70) where
   620   "f <o g == (\<lambda>x. max (f x - g x) 0)"
   621 
   622 lemma bigo_lesseq1: "f =o O(h) \<Longrightarrow> \<forall>x. abs (g x) <= abs (f x) \<Longrightarrow>
   623     g =o O(h)"
   624   apply (unfold bigo_def)
   625   apply clarsimp
   626 apply (blast intro: order_trans)
   627 done
   628 
   629 lemma bigo_lesseq2: "f =o O(h) \<Longrightarrow> \<forall>x. abs (g x) <= f x \<Longrightarrow>
   630       g =o O(h)"
   631   apply (erule bigo_lesseq1)
   632 apply (blast intro: abs_ge_self order_trans)
   633 done
   634 
   635 lemma bigo_lesseq3: "f =o O(h) \<Longrightarrow> \<forall>x. 0 <= g x \<Longrightarrow> \<forall>x. g x <= f x \<Longrightarrow>
   636       g =o O(h)"
   637   apply (erule bigo_lesseq2)
   638   apply (rule allI)
   639   apply (subst abs_of_nonneg)
   640   apply (erule spec)+
   641 done
   642 
   643 lemma bigo_lesseq4: "f =o O(h) \<Longrightarrow>
   644     \<forall>x. 0 <= g x \<Longrightarrow> \<forall>x. g x <= abs (f x) \<Longrightarrow>
   645       g =o O(h)"
   646   apply (erule bigo_lesseq1)
   647   apply (rule allI)
   648   apply (subst abs_of_nonneg)
   649   apply (erule spec)+
   650 done
   651 
   652 lemma bigo_lesso1: "\<forall>x. f x <= g x \<Longrightarrow> f <o g =o O(h)"
   653 apply (unfold lesso_def)
   654 apply (subgoal_tac "(\<lambda>x. max (f x - g x) 0) = 0")
   655  apply (metis bigo_zero)
   656 by (metis (lifting, no_types) func_zero le_fun_def le_iff_diff_le_0
   657       max.absorb2 order_eq_iff)
   658 
   659 lemma bigo_lesso2: "f =o g +o O(h) \<Longrightarrow>
   660     \<forall>x. 0 <= k x \<Longrightarrow> \<forall>x. k x <= f x \<Longrightarrow>
   661       k <o g =o O(h)"
   662   apply (unfold lesso_def)
   663   apply (rule bigo_lesseq4)
   664   apply (erule set_plus_imp_minus)
   665   apply (rule allI)
   666   apply (rule max.cobounded2)
   667   apply (rule allI)
   668   apply (subst fun_diff_def)
   669 apply (erule thin_rl)
   670 (* sledgehammer *)
   671 apply (case_tac "0 <= k x - g x")
   672  apply (metis (lifting) abs_le_D1 linorder_linear min_diff_distrib_left
   673           min.absorb1 min.absorb2 max.absorb1)
   674 by (metis abs_ge_zero le_cases max.absorb2)
   675 
   676 lemma bigo_lesso3: "f =o g +o O(h) \<Longrightarrow>
   677     \<forall>x. 0 <= k x \<Longrightarrow> \<forall>x. g x <= k x \<Longrightarrow>
   678       f <o k =o O(h)"
   679 apply (unfold lesso_def)
   680 apply (rule bigo_lesseq4)
   681   apply (erule set_plus_imp_minus)
   682  apply (rule allI)
   683  apply (rule max.cobounded2)
   684 apply (rule allI)
   685 apply (subst fun_diff_def)
   686 apply (erule thin_rl)
   687 (* sledgehammer *)
   688 apply (case_tac "0 <= f x - k x")
   689  apply simp
   690  apply (subst abs_of_nonneg)
   691   apply (drule_tac x = x in spec) back
   692   apply (metis diff_less_0_iff_less linorder_not_le not_leE xt1(12) xt1(6))
   693  apply (metis add_minus_cancel diff_le_eq le_diff_eq uminus_add_conv_diff)
   694 by (metis abs_ge_zero linorder_linear max.absorb1 max.commute)
   695 
   696 lemma bigo_lesso4:
   697   "f <o g =o O(k\<Colon>'a=>'b\<Colon>{linordered_field}) \<Longrightarrow>
   698    g =o h +o O(k) \<Longrightarrow> f <o h =o O(k)"
   699 apply (unfold lesso_def)
   700 apply (drule set_plus_imp_minus)
   701 apply (drule bigo_abs5) back
   702 apply (simp add: fun_diff_def)
   703 apply (drule bigo_useful_add, assumption)
   704 apply (erule bigo_lesseq2) back
   705 apply (rule allI)
   706 by (auto simp add: func_plus fun_diff_def algebra_simps
   707     split: split_max abs_split)
   708 
   709 lemma bigo_lesso5: "f <o g =o O(h) \<Longrightarrow> \<exists>C. \<forall>x. f x <= g x + C * abs (h x)"
   710 apply (simp only: lesso_def bigo_alt_def)
   711 apply clarsimp
   712 by (metis add_commute diff_le_eq)
   713 
   714 end