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