src/HOL/Metis_Examples/Big_O.thy
author wenzelm
Mon Dec 28 19:23:15 2015 +0100 (2015-12-28)
changeset 61954 1d43f86f48be
parent 61945 1135b8de26c3
child 63167 0909deb8059b
permissions -rw-r--r--
more symbols;
blanchet@43197
     1
(*  Title:      HOL/Metis_Examples/Big_O.thy
blanchet@43197
     2
    Author:     Lawrence C. Paulson, Cambridge University Computer Laboratory
blanchet@41144
     3
    Author:     Jasmin Blanchette, TU Muenchen
paulson@23449
     4
blanchet@43197
     5
Metis example featuring the Big O notation.
paulson@23449
     6
*)
paulson@23449
     7
wenzelm@58889
     8
section {* Metis Example Featuring the Big O Notation *}
paulson@23449
     9
blanchet@43197
    10
theory Big_O
wenzelm@41413
    11
imports
wenzelm@41413
    12
  "~~/src/HOL/Decision_Procs/Dense_Linear_Order"
wenzelm@41413
    13
  "~~/src/HOL/Library/Function_Algebras"
wenzelm@41413
    14
  "~~/src/HOL/Library/Set_Algebras"
paulson@23449
    15
begin
paulson@23449
    16
paulson@23449
    17
subsection {* Definitions *}
paulson@23449
    18
wenzelm@61076
    19
definition bigo :: "('a => 'b::linordered_idom) => ('a => 'b) set" ("(1O'(_'))") where
wenzelm@61945
    20
  "O(f::('a => 'b)) == {h. \<exists>c. \<forall>x. \<bar>h x\<bar> <= c * \<bar>f x\<bar>}"
paulson@23449
    21
blanchet@45575
    22
lemma bigo_pos_const:
wenzelm@61076
    23
  "(\<exists>c::'a::linordered_idom.
wenzelm@61945
    24
    \<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>)
wenzelm@61945
    25
    \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>))"
haftmann@59554
    26
  by (metis (no_types) abs_ge_zero
haftmann@59554
    27
      algebra_simps mult.comm_neutral
lp15@61824
    28
      mult_nonpos_nonneg not_le_imp_less order_trans zero_less_one)
paulson@23449
    29
blanchet@36407
    30
(*** Now various verions with an increasing shrink factor ***)
paulson@23449
    31
blanchet@57245
    32
sledgehammer_params [isar_proofs, compress = 1]
paulson@23449
    33
blanchet@45575
    34
lemma
wenzelm@61076
    35
  "(\<exists>c::'a::linordered_idom.
wenzelm@61945
    36
    \<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>)
wenzelm@61945
    37
    \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>))"
paulson@23449
    38
  apply auto
paulson@23449
    39
  apply (case_tac "c = 0", simp)
paulson@23449
    40
  apply (rule_tac x = "1" in exI, simp)
wenzelm@61945
    41
  apply (rule_tac x = "\<bar>c\<bar>" in exI, auto)
blanchet@36561
    42
proof -
blanchet@36561
    43
  fix c :: 'a and x :: 'b
blanchet@36561
    44
  assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
wenzelm@61076
    45
  have F1: "\<forall>x\<^sub>1::'a::linordered_idom. 0 \<le> \<bar>x\<^sub>1\<bar>" by (metis abs_ge_zero)
wenzelm@61076
    46
  have F2: "\<forall>x\<^sub>1::'a::linordered_idom. 1 * x\<^sub>1 = x\<^sub>1" by (metis mult_1)
wenzelm@53015
    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)
wenzelm@61076
    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>"
blanchet@36561
    49
    by (metis abs_mult)
wenzelm@61076
    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"
blanchet@36561
    51
    by (metis abs_mult_pos)
wenzelm@61076
    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)
wenzelm@61076
    53
  hence "\<forall>x\<^sub>1\<ge>0. \<bar>x\<^sub>1::'a::linordered_idom\<bar> = x\<^sub>1" by (metis F2 abs_one)
wenzelm@53015
    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)
wenzelm@53015
    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)
wenzelm@61076
    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)
wenzelm@61076
    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)
wenzelm@53015
    58
  hence "\<forall>x\<^sub>3. c * \<bar>f x\<^sub>3\<bar> = \<bar>c * f x\<^sub>3\<bar>" by (metis F1)
blanchet@36561
    59
  hence "\<bar>h x\<bar> \<le> \<bar>c * f x\<bar>" by (metis A1)
blanchet@36561
    60
  thus "\<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>" by (metis F4)
paulson@23449
    61
qed
paulson@23449
    62
blanchet@57245
    63
sledgehammer_params [isar_proofs, compress = 2]
paulson@25710
    64
blanchet@45575
    65
lemma
wenzelm@61076
    66
  "(\<exists>c::'a::linordered_idom.
wenzelm@61945
    67
    \<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>)
wenzelm@61945
    68
    \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>))"
paulson@23449
    69
  apply auto
paulson@23449
    70
  apply (case_tac "c = 0", simp)
paulson@23449
    71
  apply (rule_tac x = "1" in exI, simp)
wenzelm@61945
    72
  apply (rule_tac x = "\<bar>c\<bar>" in exI, auto)
blanchet@36561
    73
proof -
blanchet@36561
    74
  fix c :: 'a and x :: 'b
blanchet@36561
    75
  assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
wenzelm@61076
    76
  have F1: "\<forall>x\<^sub>1::'a::linordered_idom. 1 * x\<^sub>1 = x\<^sub>1" by (metis mult_1)
wenzelm@61076
    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>"
blanchet@36561
    78
    by (metis abs_mult)
wenzelm@61076
    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)
wenzelm@53015
    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)
wenzelm@53015
    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)
blanchet@36561
    82
  hence "\<bar>h x\<bar> \<le> \<bar>c * f x\<bar>" by (metis A1 abs_ge_zero)
blanchet@36561
    83
  thus "\<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>" by (metis F2)
paulson@23449
    84
qed
paulson@23449
    85
blanchet@57245
    86
sledgehammer_params [isar_proofs, compress = 3]
paulson@25710
    87
blanchet@45575
    88
lemma
wenzelm@61076
    89
  "(\<exists>c::'a::linordered_idom.
wenzelm@61945
    90
    \<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>)
wenzelm@61945
    91
    \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>))"
paulson@23449
    92
  apply auto
paulson@23449
    93
  apply (case_tac "c = 0", simp)
paulson@23449
    94
  apply (rule_tac x = "1" in exI, simp)
wenzelm@61945
    95
  apply (rule_tac x = "\<bar>c\<bar>" in exI, auto)
blanchet@36561
    96
proof -
blanchet@36561
    97
  fix c :: 'a and x :: 'b
blanchet@36561
    98
  assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
wenzelm@61076
    99
  have F1: "\<forall>x\<^sub>1::'a::linordered_idom. 1 * x\<^sub>1 = x\<^sub>1" by (metis mult_1)
wenzelm@61076
   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)
wenzelm@61076
   101
  hence "\<forall>x\<^sub>1\<ge>0. \<bar>x\<^sub>1::'a::linordered_idom\<bar> = x\<^sub>1" by (metis F1 abs_one)
wenzelm@53015
   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)
blanchet@46644
   103
  thus "\<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>" by (metis A1 abs_ge_zero)
paulson@23449
   104
qed
paulson@23449
   105
blanchet@57245
   106
sledgehammer_params [isar_proofs, compress = 4]
paulson@24545
   107
blanchet@45575
   108
lemma
wenzelm@61076
   109
  "(\<exists>c::'a::linordered_idom.
wenzelm@61945
   110
    \<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>)
wenzelm@61945
   111
    \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>))"
paulson@24545
   112
  apply auto
paulson@24545
   113
  apply (case_tac "c = 0", simp)
paulson@24545
   114
  apply (rule_tac x = "1" in exI, simp)
wenzelm@61945
   115
  apply (rule_tac x = "\<bar>c\<bar>" in exI, auto)
blanchet@36561
   116
proof -
blanchet@36561
   117
  fix c :: 'a and x :: 'b
blanchet@36561
   118
  assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
wenzelm@61076
   119
  have "\<forall>x\<^sub>1::'a::linordered_idom. 1 * x\<^sub>1 = x\<^sub>1" by (metis mult_1)
wenzelm@53015
   120
  hence "\<forall>x\<^sub>3. \<bar>c * \<bar>f x\<^sub>3\<bar>\<bar> = c * \<bar>f x\<^sub>3\<bar>"
blanchet@36561
   121
    by (metis A1 abs_ge_zero order_trans abs_mult_pos abs_one)
blanchet@36561
   122
  hence "\<bar>h x\<bar> \<le> \<bar>c * f x\<bar>" by (metis A1 abs_ge_zero abs_mult_pos abs_mult)
blanchet@36561
   123
  thus "\<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>" by (metis abs_mult)
paulson@24545
   124
qed
paulson@24545
   125
blanchet@57245
   126
sledgehammer_params [isar_proofs, compress = 1]
paulson@24545
   127
wenzelm@61945
   128
lemma bigo_alt_def: "O(f) = {h. \<exists>c. (0 < c \<and> (\<forall>x. \<bar>h x\<bar> <= c * \<bar>f x\<bar>))}"
paulson@23449
   129
by (auto simp add: bigo_def bigo_pos_const)
paulson@23449
   130
blanchet@46364
   131
lemma bigo_elt_subset [intro]: "f : O(g) \<Longrightarrow> O(f) \<le> O(g)"
blanchet@45575
   132
apply (auto simp add: bigo_alt_def)
blanchet@45575
   133
apply (rule_tac x = "ca * c" in exI)
haftmann@59554
   134
apply (metis algebra_simps mult_le_cancel_left_pos order_trans mult_pos_pos)
haftmann@59554
   135
done
paulson@23449
   136
paulson@23449
   137
lemma bigo_refl [intro]: "f : O(f)"
blanchet@46364
   138
unfolding bigo_def mem_Collect_eq
hoelzl@36844
   139
by (metis mult_1 order_refl)
paulson@23449
   140
paulson@23449
   141
lemma bigo_zero: "0 : O(g)"
blanchet@36561
   142
apply (auto simp add: bigo_def func_zero)
hoelzl@36844
   143
by (metis mult_zero_left order_refl)
paulson@23449
   144
blanchet@45575
   145
lemma bigo_zero2: "O(\<lambda>x. 0) = {\<lambda>x. 0}"
blanchet@45575
   146
by (auto simp add: bigo_def)
paulson@23449
   147
blanchet@43197
   148
lemma bigo_plus_self_subset [intro]:
krauss@47445
   149
  "O(f) + O(f) <= O(f)"
blanchet@45575
   150
apply (auto simp add: bigo_alt_def set_plus_def)
blanchet@45575
   151
apply (rule_tac x = "c + ca" in exI)
blanchet@45575
   152
apply auto
blanchet@45575
   153
apply (simp add: ring_distribs func_plus)
blanchet@45575
   154
by (metis order_trans abs_triangle_ineq add_mono)
paulson@23449
   155
krauss@47445
   156
lemma bigo_plus_idemp [simp]: "O(f) + O(f) = O(f)"
blanchet@45575
   157
by (metis bigo_plus_self_subset bigo_zero set_eq_subset set_zero_plus2)
paulson@23449
   158
krauss@47445
   159
lemma bigo_plus_subset [intro]: "O(f + g) <= O(f) + O(g)"
blanchet@45575
   160
apply (rule subsetI)
blanchet@45575
   161
apply (auto simp add: bigo_def bigo_pos_const func_plus set_plus_def)
blanchet@45575
   162
apply (subst bigo_pos_const [symmetric])+
wenzelm@61945
   163
apply (rule_tac x = "\<lambda>n. if \<bar>g n\<bar> <= \<bar>f n\<bar> then x n else 0" in exI)
blanchet@45575
   164
apply (rule conjI)
blanchet@45575
   165
 apply (rule_tac x = "c + c" in exI)
blanchet@45575
   166
 apply clarsimp
wenzelm@61945
   167
 apply (subgoal_tac "c * \<bar>f xa + g xa\<bar> <= (c + c) * \<bar>f xa\<bar>")
nipkow@56536
   168
  apply (metis mult_2 order_trans)
wenzelm@61945
   169
 apply (subgoal_tac "c * \<bar>f xa + g xa\<bar> <= c * (\<bar>f xa\<bar> + \<bar>g xa\<bar>)")
nipkow@56536
   170
  apply (erule order_trans)
nipkow@56536
   171
  apply (simp add: ring_distribs)
nipkow@56536
   172
 apply (rule mult_left_mono)
nipkow@56536
   173
  apply (simp add: abs_triangle_ineq)
nipkow@56536
   174
 apply (simp add: order_less_le)
wenzelm@61945
   175
apply (rule_tac x = "\<lambda>n. if \<bar>f n\<bar> < \<bar>g n\<bar> then x n else 0" in exI)
blanchet@45575
   176
apply (rule conjI)
blanchet@45575
   177
 apply (rule_tac x = "c + c" in exI)
blanchet@45575
   178
 apply auto
wenzelm@61945
   179
apply (subgoal_tac "c * \<bar>f xa + g xa\<bar> <= (c + c) * \<bar>g xa\<bar>")
nipkow@56536
   180
 apply (metis order_trans mult_2)
wenzelm@61945
   181
apply (subgoal_tac "c * \<bar>f xa + g xa\<bar> <= c * (\<bar>f xa\<bar> + \<bar>g xa\<bar>)")
nipkow@56536
   182
 apply (erule order_trans)
nipkow@56536
   183
 apply (simp add: ring_distribs)
nipkow@56536
   184
by (metis abs_triangle_ineq mult_le_cancel_left_pos)
paulson@23449
   185
krauss@47445
   186
lemma bigo_plus_subset2 [intro]: "A <= O(f) \<Longrightarrow> B <= O(f) \<Longrightarrow> A + B <= O(f)"
blanchet@45575
   187
by (metis bigo_plus_idemp set_plus_mono2)
paulson@23449
   188
krauss@47445
   189
lemma bigo_plus_eq: "\<forall>x. 0 <= f x \<Longrightarrow> \<forall>x. 0 <= g x \<Longrightarrow> O(f + g) = O(f) + O(g)"
blanchet@45575
   190
apply (rule equalityI)
blanchet@45575
   191
apply (rule bigo_plus_subset)
blanchet@45575
   192
apply (simp add: bigo_alt_def set_plus_def func_plus)
blanchet@45575
   193
apply clarify
blanchet@45575
   194
(* sledgehammer *)
blanchet@45575
   195
apply (rule_tac x = "max c ca" in exI)
blanchet@46369
   196
blanchet@45575
   197
apply (rule conjI)
blanchet@45575
   198
 apply (metis less_max_iff_disj)
blanchet@45575
   199
apply clarify
blanchet@45575
   200
apply (drule_tac x = "xa" in spec)+
blanchet@45575
   201
apply (subgoal_tac "0 <= f xa + g xa")
blanchet@45575
   202
 apply (simp add: ring_distribs)
wenzelm@61945
   203
 apply (subgoal_tac "\<bar>a xa + b xa\<bar> <= \<bar>a xa\<bar> + \<bar>b xa\<bar>")
wenzelm@61945
   204
  apply (subgoal_tac "\<bar>a xa\<bar> + \<bar>b xa\<bar> <= max c ca * f xa + max c ca * g xa")
blanchet@45575
   205
   apply (metis order_trans)
paulson@23449
   206
  defer 1
blanchet@45575
   207
  apply (metis abs_triangle_ineq)
blanchet@45575
   208
 apply (metis add_nonneg_nonneg)
blanchet@46644
   209
apply (rule add_mono)
haftmann@54863
   210
 apply (metis max.cobounded2 linorder_linear max.absorb1 mult_right_mono xt1(6))
haftmann@54863
   211
by (metis max.cobounded2 linorder_not_le mult_le_cancel_right order_trans)
paulson@23449
   212
blanchet@45575
   213
lemma bigo_bounded_alt: "\<forall>x. 0 <= f x \<Longrightarrow> \<forall>x. f x <= c * g x \<Longrightarrow> f : O(g)"
blanchet@45575
   214
apply (auto simp add: bigo_def)
blanchet@36561
   215
(* Version 1: one-line proof *)
blanchet@45575
   216
by (metis abs_le_D1 linorder_class.not_less order_less_le Orderings.xt1(12) abs_mult)
paulson@23449
   217
blanchet@45575
   218
lemma "\<forall>x. 0 <= f x \<Longrightarrow> \<forall>x. f x <= c * g x \<Longrightarrow> f : O(g)"
blanchet@36561
   219
apply (auto simp add: bigo_def)
blanchet@36561
   220
(* Version 2: structured proof *)
blanchet@36561
   221
proof -
blanchet@36561
   222
  assume "\<forall>x. f x \<le> c * g x"
blanchet@36561
   223
  thus "\<exists>c. \<forall>x. f x \<le> c * \<bar>g x\<bar>" by (metis abs_mult abs_ge_self order_trans)
paulson@23449
   224
qed
paulson@23449
   225
blanchet@45575
   226
lemma bigo_bounded: "\<forall>x. 0 <= f x \<Longrightarrow> \<forall>x. f x <= g x \<Longrightarrow> f : O(g)"
blanchet@45575
   227
apply (erule bigo_bounded_alt [of f 1 g])
blanchet@45575
   228
by (metis mult_1)
paulson@23449
   229
blanchet@45575
   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)"
blanchet@36561
   231
apply (rule set_minus_imp_plus)
blanchet@36561
   232
apply (rule bigo_bounded)
blanchet@46369
   233
 apply (metis add_le_cancel_left diff_add_cancel diff_self minus_apply
haftmann@59554
   234
              algebra_simps)
blanchet@46369
   235
by (metis add_le_cancel_left diff_add_cancel func_plus le_fun_def
haftmann@59554
   236
          algebra_simps)
paulson@23449
   237
wenzelm@61945
   238
lemma bigo_abs: "(\<lambda>x. \<bar>f x\<bar>) =o O(f)"
blanchet@36561
   239
apply (unfold bigo_def)
blanchet@36561
   240
apply auto
hoelzl@36844
   241
by (metis mult_1 order_refl)
paulson@23449
   242
wenzelm@61945
   243
lemma bigo_abs2: "f =o O(\<lambda>x. \<bar>f x\<bar>)"
blanchet@36561
   244
apply (unfold bigo_def)
blanchet@36561
   245
apply auto
hoelzl@36844
   246
by (metis mult_1 order_refl)
blanchet@43197
   247
wenzelm@61945
   248
lemma bigo_abs3: "O(f) = O(\<lambda>x. \<bar>f x\<bar>)"
blanchet@36561
   249
proof -
blanchet@36561
   250
  have F1: "\<forall>v u. u \<in> O(v) \<longrightarrow> O(u) \<subseteq> O(v)" by (metis bigo_elt_subset)
blanchet@36561
   251
  have F2: "\<forall>u. (\<lambda>R. \<bar>u R\<bar>) \<in> O(u)" by (metis bigo_abs)
blanchet@36561
   252
  have "\<forall>u. u \<in> O(\<lambda>R. \<bar>u R\<bar>)" by (metis bigo_abs2)
blanchet@36561
   253
  thus "O(f) = O(\<lambda>x. \<bar>f x\<bar>)" using F1 F2 by auto
blanchet@43197
   254
qed
paulson@23449
   255
wenzelm@61945
   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)"
paulson@23449
   257
  apply (drule set_plus_imp_minus)
paulson@23449
   258
  apply (rule set_minus_imp_plus)
berghofe@26814
   259
  apply (subst fun_diff_def)
paulson@23449
   260
proof -
paulson@23449
   261
  assume a: "f - g : O(h)"
wenzelm@61945
   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>)"
paulson@23449
   263
    by (rule bigo_abs2)
wenzelm@61945
   264
  also have "... <= O(\<lambda>x. \<bar>f x - g x\<bar>)"
paulson@23449
   265
    apply (rule bigo_elt_subset)
paulson@23449
   266
    apply (rule bigo_bounded)
blanchet@46369
   267
     apply (metis abs_ge_zero)
blanchet@46369
   268
    by (metis abs_triangle_ineq3)
paulson@23449
   269
  also have "... <= O(f - g)"
paulson@23449
   270
    apply (rule bigo_elt_subset)
berghofe@26814
   271
    apply (subst fun_diff_def)
paulson@23449
   272
    apply (rule bigo_abs)
paulson@23449
   273
    done
paulson@23449
   274
  also have "... <= O(h)"
wenzelm@23464
   275
    using a by (rule bigo_elt_subset)
wenzelm@61945
   276
  finally show "(\<lambda>x. \<bar>f x\<bar> - \<bar>g x\<bar>) : O(h)" .
paulson@23449
   277
qed
paulson@23449
   278
wenzelm@61945
   279
lemma bigo_abs5: "f =o O(g) \<Longrightarrow> (\<lambda>x. \<bar>f x\<bar>) =o O(g)"
paulson@23449
   280
by (unfold bigo_def, auto)
paulson@23449
   281
krauss@47445
   282
lemma bigo_elt_subset2 [intro]: "f : g +o O(h) \<Longrightarrow> O(f) <= O(g) + O(h)"
paulson@23449
   283
proof -
paulson@23449
   284
  assume "f : g +o O(h)"
krauss@47445
   285
  also have "... <= O(g) + O(h)"
paulson@23449
   286
    by (auto del: subsetI)
wenzelm@61945
   287
  also have "... = O(\<lambda>x. \<bar>g x\<bar>) + O(\<lambda>x. \<bar>h x\<bar>)"
blanchet@46369
   288
    by (metis bigo_abs3)
wenzelm@61945
   289
  also have "... = O((\<lambda>x. \<bar>g x\<bar>) + (\<lambda>x. \<bar>h x\<bar>))"
paulson@23449
   290
    by (rule bigo_plus_eq [symmetric], auto)
paulson@23449
   291
  finally have "f : ...".
paulson@23449
   292
  then have "O(f) <= ..."
paulson@23449
   293
    by (elim bigo_elt_subset)
wenzelm@61945
   294
  also have "... = O(\<lambda>x. \<bar>g x\<bar>) + O(\<lambda>x. \<bar>h x\<bar>)"
paulson@23449
   295
    by (rule bigo_plus_eq, auto)
paulson@23449
   296
  finally show ?thesis
paulson@23449
   297
    by (simp add: bigo_abs3 [symmetric])
paulson@23449
   298
qed
paulson@23449
   299
krauss@47445
   300
lemma bigo_mult [intro]: "O(f) * O(g) <= O(f * g)"
blanchet@46369
   301
apply (rule subsetI)
blanchet@46369
   302
apply (subst bigo_def)
haftmann@57514
   303
apply (auto simp del: abs_mult ac_simps
blanchet@46369
   304
            simp add: bigo_alt_def set_times_def func_times)
blanchet@45575
   305
(* sledgehammer *)
blanchet@46369
   306
apply (rule_tac x = "c * ca" in exI)
blanchet@46369
   307
apply (rule allI)
blanchet@46369
   308
apply (erule_tac x = x in allE)+
wenzelm@61945
   309
apply (subgoal_tac "c * ca * \<bar>f x * g x\<bar> = (c * \<bar>f x\<bar>) * (ca * \<bar>g x\<bar>)")
blanchet@46369
   310
 apply (metis (no_types) abs_ge_zero abs_mult mult_mono')
haftmann@57512
   311
by (metis mult.assoc mult.left_commute abs_of_pos mult.left_commute abs_mult)
paulson@23449
   312
paulson@23449
   313
lemma bigo_mult2 [intro]: "f *o O(g) <= O(f * g)"
blanchet@46369
   314
by (metis bigo_mult bigo_refl set_times_mono3 subset_trans)
paulson@23449
   315
blanchet@45575
   316
lemma bigo_mult3: "f : O(h) \<Longrightarrow> g : O(j) \<Longrightarrow> f * g : O(h * j)"
blanchet@36561
   317
by (metis bigo_mult set_rev_mp set_times_intro)
paulson@23449
   318
blanchet@45575
   319
lemma bigo_mult4 [intro]:"f : k +o O(h) \<Longrightarrow> g * f : (g * k) +o O(g * h)"
paulson@23449
   320
by (metis bigo_mult2 set_plus_mono_b set_times_intro2 set_times_plus_distrib)
paulson@23449
   321
blanchet@45575
   322
lemma bigo_mult5: "\<forall>x. f x ~= 0 \<Longrightarrow>
wenzelm@61076
   323
    O(f * g) <= (f::'a => ('b::linordered_field)) *o O(g)"
paulson@23449
   324
proof -
blanchet@45575
   325
  assume a: "\<forall>x. f x ~= 0"
paulson@23449
   326
  show "O(f * g) <= f *o O(g)"
paulson@23449
   327
  proof
paulson@23449
   328
    fix h
wenzelm@41541
   329
    assume h: "h : O(f * g)"
blanchet@45575
   330
    then have "(\<lambda>x. 1 / (f x)) * h : (\<lambda>x. 1 / f x) *o O(f * g)"
paulson@23449
   331
      by auto
blanchet@45575
   332
    also have "... <= O((\<lambda>x. 1 / f x) * (f * g))"
paulson@23449
   333
      by (rule bigo_mult2)
blanchet@45575
   334
    also have "(\<lambda>x. 1 / f x) * (f * g) = g"
haftmann@59867
   335
      by (simp add: fun_eq_iff a)
wenzelm@61076
   336
    finally have "(\<lambda>x. (1::'b) / f x) * h : O(g)".
wenzelm@61076
   337
    then have "f * ((\<lambda>x. (1::'b) / f x) * h) : f *o O(g)"
paulson@23449
   338
      by auto
wenzelm@61076
   339
    also have "f * ((\<lambda>x. (1::'b) / f x) * h) = h"
haftmann@59554
   340
      by (simp add: func_times fun_eq_iff a)
paulson@23449
   341
    finally show "h : f *o O(g)".
paulson@23449
   342
  qed
paulson@23449
   343
qed
paulson@23449
   344
blanchet@46369
   345
lemma bigo_mult6:
wenzelm@61076
   346
"\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) = (f::'a \<Rightarrow> ('b::linordered_field)) *o O(g)"
paulson@23449
   347
by (metis bigo_mult2 bigo_mult5 order_antisym)
paulson@23449
   348
paulson@23449
   349
(*proof requires relaxing relevance: 2007-01-25*)
blanchet@45705
   350
declare bigo_mult6 [simp]
blanchet@45705
   351
blanchet@46369
   352
lemma bigo_mult7:
wenzelm@61076
   353
"\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) \<le> O(f::'a \<Rightarrow> ('b::linordered_field)) * O(g)"
blanchet@46369
   354
by (metis bigo_refl bigo_mult6 set_times_mono3)
paulson@23449
   355
blanchet@45575
   356
declare bigo_mult6 [simp del]
blanchet@45575
   357
declare bigo_mult7 [intro!]
blanchet@45575
   358
blanchet@46369
   359
lemma bigo_mult8:
wenzelm@61076
   360
"\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) = O(f::'a \<Rightarrow> ('b::linordered_field)) * O(g)"
paulson@23449
   361
by (metis bigo_mult bigo_mult7 order_antisym_conv)
paulson@23449
   362
blanchet@45575
   363
lemma bigo_minus [intro]: "f : O(g) \<Longrightarrow> - f : O(g)"
blanchet@46369
   364
by (auto simp add: bigo_def fun_Compl_def)
paulson@23449
   365
blanchet@45575
   366
lemma bigo_minus2: "f : g +o O(h) \<Longrightarrow> -f : -g +o O(h)"
haftmann@59554
   367
by (metis (no_types, lifting) bigo_minus diff_minus_eq_add minus_add_distrib
haftmann@59554
   368
    minus_minus set_minus_imp_plus set_plus_imp_minus)
paulson@23449
   369
paulson@23449
   370
lemma bigo_minus3: "O(-f) = O(f)"
blanchet@46369
   371
by (metis bigo_elt_subset bigo_minus bigo_refl equalityI minus_minus)
paulson@23449
   372
blanchet@46369
   373
lemma bigo_plus_absorb_lemma1: "f : O(g) \<Longrightarrow> f +o O(g) \<le> O(g)"
blanchet@46369
   374
by (metis bigo_plus_idemp set_plus_mono3)
paulson@23449
   375
blanchet@46369
   376
lemma bigo_plus_absorb_lemma2: "f : O(g) \<Longrightarrow> O(g) \<le> f +o O(g)"
blanchet@46369
   377
by (metis (no_types) bigo_minus bigo_plus_absorb_lemma1 right_minus
blanchet@46644
   378
          set_plus_mono set_plus_rearrange2 set_zero_plus subsetD subset_refl
blanchet@46644
   379
          subset_trans)
paulson@23449
   380
blanchet@45575
   381
lemma bigo_plus_absorb [simp]: "f : O(g) \<Longrightarrow> f +o O(g) = O(g)"
paulson@41865
   382
by (metis bigo_plus_absorb_lemma1 bigo_plus_absorb_lemma2 order_eq_iff)
paulson@23449
   383
blanchet@46369
   384
lemma bigo_plus_absorb2 [intro]: "f : O(g) \<Longrightarrow> A <= O(g) \<Longrightarrow> f +o A \<le> O(g)"
blanchet@46369
   385
by (metis bigo_plus_absorb set_plus_mono)
paulson@23449
   386
blanchet@45575
   387
lemma bigo_add_commute_imp: "f : g +o O(h) \<Longrightarrow> g : f +o O(h)"
blanchet@46369
   388
by (metis bigo_minus minus_diff_eq set_plus_imp_minus set_minus_plus)
paulson@23449
   389
paulson@23449
   390
lemma bigo_add_commute: "(f : g +o O(h)) = (g : f +o O(h))"
blanchet@46369
   391
by (metis bigo_add_commute_imp)
paulson@23449
   392
blanchet@45575
   393
lemma bigo_const1: "(\<lambda>x. c) : O(\<lambda>x. 1)"
haftmann@57514
   394
by (auto simp add: bigo_def ac_simps)
paulson@23449
   395
blanchet@46369
   396
lemma bigo_const2 [intro]: "O(\<lambda>x. c) \<le> O(\<lambda>x. 1)"
paulson@41865
   397
by (metis bigo_const1 bigo_elt_subset)
paulson@23449
   398
wenzelm@61076
   399
lemma bigo_const3: "(c::'a::linordered_field) ~= 0 \<Longrightarrow> (\<lambda>x. 1) : O(\<lambda>x. c)"
paulson@23449
   400
apply (simp add: bigo_def)
blanchet@36561
   401
by (metis abs_eq_0 left_inverse order_refl)
paulson@23449
   402
wenzelm@61076
   403
lemma bigo_const4: "(c::'a::linordered_field) ~= 0 \<Longrightarrow> O(\<lambda>x. 1) <= O(\<lambda>x. c)"
blanchet@46369
   404
by (metis bigo_elt_subset bigo_const3)
paulson@23449
   405
wenzelm@61076
   406
lemma bigo_const [simp]: "(c::'a::linordered_field) ~= 0 \<Longrightarrow>
blanchet@45575
   407
    O(\<lambda>x. c) = O(\<lambda>x. 1)"
blanchet@46369
   408
by (metis bigo_const2 bigo_const4 equalityI)
paulson@23449
   409
blanchet@45575
   410
lemma bigo_const_mult1: "(\<lambda>x. c * f x) : O(f)"
blanchet@46369
   411
apply (simp add: bigo_def abs_mult)
blanchet@36561
   412
by (metis le_less)
paulson@23449
   413
blanchet@46369
   414
lemma bigo_const_mult2: "O(\<lambda>x. c * f x) \<le> O(f)"
paulson@23449
   415
by (rule bigo_elt_subset, rule bigo_const_mult1)
paulson@23449
   416
wenzelm@61076
   417
lemma bigo_const_mult3: "(c::'a::linordered_field) ~= 0 \<Longrightarrow> f : O(\<lambda>x. c * f x)"
blanchet@45575
   418
apply (simp add: bigo_def)
haftmann@57512
   419
by (metis (no_types) abs_mult mult.assoc mult_1 order_refl left_inverse)
paulson@23449
   420
blanchet@46369
   421
lemma bigo_const_mult4:
wenzelm@61076
   422
"(c::'a::linordered_field) \<noteq> 0 \<Longrightarrow> O(f) \<le> O(\<lambda>x. c * f x)"
blanchet@46369
   423
by (metis bigo_elt_subset bigo_const_mult3)
paulson@23449
   424
wenzelm@61076
   425
lemma bigo_const_mult [simp]: "(c::'a::linordered_field) ~= 0 \<Longrightarrow>
blanchet@45575
   426
    O(\<lambda>x. c * f x) = O(f)"
blanchet@46369
   427
by (metis equalityI bigo_const_mult2 bigo_const_mult4)
paulson@23449
   428
wenzelm@61076
   429
lemma bigo_const_mult5 [simp]: "(c::'a::linordered_field) ~= 0 \<Longrightarrow>
blanchet@45575
   430
    (\<lambda>x. c) *o O(f) = O(f)"
paulson@23449
   431
  apply (auto del: subsetI)
paulson@23449
   432
  apply (rule order_trans)
paulson@23449
   433
  apply (rule bigo_mult2)
paulson@23449
   434
  apply (simp add: func_times)
paulson@23449
   435
  apply (auto intro!: subsetI simp add: bigo_def elt_set_times_def func_times)
blanchet@45575
   436
  apply (rule_tac x = "\<lambda>y. inverse c * x y" in exI)
blanchet@43197
   437
  apply (rename_tac g d)
paulson@24942
   438
  apply safe
blanchet@43197
   439
  apply (rule_tac [2] ext)
blanchet@43197
   440
   prefer 2
haftmann@26041
   441
   apply simp
haftmann@57512
   442
  apply (simp add: mult.assoc [symmetric] abs_mult)
blanchet@39259
   443
  (* couldn't get this proof without the step above *)
blanchet@39259
   444
proof -
blanchet@39259
   445
  fix g :: "'b \<Rightarrow> 'a" and d :: 'a
wenzelm@61076
   446
  assume A1: "c \<noteq> (0::'a)"
wenzelm@61076
   447
  assume A2: "\<forall>x::'b. \<bar>g x\<bar> \<le> d * \<bar>f x\<bar>"
blanchet@39259
   448
  have F1: "inverse \<bar>c\<bar> = \<bar>inverse c\<bar>" using A1 by (metis nonzero_abs_inverse)
wenzelm@61076
   449
  have F2: "(0::'a) < \<bar>c\<bar>" using A1 by (metis zero_less_abs_iff)
wenzelm@61076
   450
  have "(0::'a) < \<bar>c\<bar> \<longrightarrow> (0::'a) < \<bar>inverse c\<bar>" using F1 by (metis positive_imp_inverse_positive)
wenzelm@61076
   451
  hence "(0::'a) < \<bar>inverse c\<bar>" using F2 by metis
wenzelm@61076
   452
  hence F3: "(0::'a) \<le> \<bar>inverse c\<bar>" by (metis order_le_less)
wenzelm@61076
   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>"
blanchet@39259
   454
    using A2 by metis
wenzelm@61076
   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>"
blanchet@39259
   456
    using F3 by metis
wenzelm@61076
   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>)"
haftmann@59557
   458
    by (metis mult_left_mono)
wenzelm@61076
   459
  then show "\<exists>ca::'a. \<forall>x::'b. inverse \<bar>c\<bar> * \<bar>g x\<bar> \<le> ca * \<bar>f x\<bar>"
haftmann@59867
   460
    using A2 F4 by (metis F1 `0 < \<bar>inverse c\<bar>` linordered_field_class.sign_simps(23) mult_le_cancel_left_pos)
blanchet@39259
   461
qed
paulson@23449
   462
blanchet@45575
   463
lemma bigo_const_mult6 [intro]: "(\<lambda>x. c) *o O(f) <= O(f)"
paulson@23449
   464
  apply (auto intro!: subsetI
paulson@23449
   465
    simp add: bigo_def elt_set_times_def func_times
haftmann@57514
   466
    simp del: abs_mult ac_simps)
blanchet@45575
   467
(* sledgehammer *)
wenzelm@61945
   468
  apply (rule_tac x = "ca * \<bar>c\<bar>" in exI)
paulson@23449
   469
  apply (rule allI)
wenzelm@61945
   470
  apply (subgoal_tac "ca * \<bar>c\<bar> * \<bar>f x\<bar> = \<bar>c\<bar> * (ca * \<bar>f x\<bar>)")
paulson@23449
   471
  apply (erule ssubst)
paulson@23449
   472
  apply (subst abs_mult)
paulson@23449
   473
  apply (rule mult_left_mono)
paulson@23449
   474
  apply (erule spec)
paulson@23449
   475
  apply simp
haftmann@57514
   476
  apply (simp add: ac_simps)
paulson@23449
   477
done
paulson@23449
   478
blanchet@45575
   479
lemma bigo_const_mult7 [intro]: "f =o O(g) \<Longrightarrow> (\<lambda>x. c * f x) =o O(g)"
blanchet@46369
   480
by (metis bigo_const_mult1 bigo_elt_subset order_less_le psubsetD)
paulson@23449
   481
blanchet@45575
   482
lemma bigo_compose1: "f =o O(g) \<Longrightarrow> (\<lambda>x. f(k x)) =o O(\<lambda>x. g(k x))"
paulson@23449
   483
by (unfold bigo_def, auto)
paulson@23449
   484
blanchet@46369
   485
lemma bigo_compose2:
blanchet@46369
   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))"
haftmann@54230
   487
apply (simp only: set_minus_plus [symmetric] fun_Compl_def func_plus)
haftmann@54230
   488
apply (drule bigo_compose1 [of "f - g" h k])
haftmann@54230
   489
apply (simp add: fun_diff_def)
haftmann@54230
   490
done
paulson@23449
   491
paulson@23449
   492
subsection {* Setsum *}
paulson@23449
   493
blanchet@45575
   494
lemma bigo_setsum_main: "\<forall>x. \<forall>y \<in> A x. 0 <= h x y \<Longrightarrow>
wenzelm@61945
   495
    \<exists>c. \<forall>x. \<forall>y \<in> A x. \<bar>f x y\<bar> <= c * (h x y) \<Longrightarrow>
wenzelm@61954
   496
      (\<lambda>x. \<Sum>y \<in> A x. f x y) =o O(\<lambda>x. \<Sum>y \<in> A x. h x y)"
blanchet@46369
   497
apply (auto simp add: bigo_def)
wenzelm@61945
   498
apply (rule_tac x = "\<bar>c\<bar>" in exI)
blanchet@46369
   499
apply (subst abs_of_nonneg) back back
blanchet@46369
   500
 apply (rule setsum_nonneg)
blanchet@46369
   501
 apply force
blanchet@46369
   502
apply (subst setsum_right_distrib)
blanchet@46369
   503
apply (rule allI)
blanchet@46369
   504
apply (rule order_trans)
blanchet@46369
   505
 apply (rule setsum_abs)
blanchet@46369
   506
apply (rule setsum_mono)
blanchet@46369
   507
by (metis abs_ge_self abs_mult_pos order_trans)
paulson@23449
   508
blanchet@45575
   509
lemma bigo_setsum1: "\<forall>x y. 0 <= h x y \<Longrightarrow>
wenzelm@61945
   510
    \<exists>c. \<forall>x y. \<bar>f x y\<bar> <= c * (h x y) \<Longrightarrow>
wenzelm@61954
   511
      (\<lambda>x. \<Sum>y \<in> A x. f x y) =o O(\<lambda>x. \<Sum>y \<in> A x. h x y)"
blanchet@45575
   512
by (metis (no_types) bigo_setsum_main)
paulson@23449
   513
blanchet@45575
   514
lemma bigo_setsum2: "\<forall>y. 0 <= h y \<Longrightarrow>
wenzelm@61945
   515
    \<exists>c. \<forall>y. \<bar>f y\<bar> <= c * (h y) \<Longrightarrow>
wenzelm@61954
   516
      (\<lambda>x. \<Sum>y \<in> A x. f y) =o O(\<lambda>x. \<Sum>y \<in> A x. h y)"
blanchet@46369
   517
apply (rule bigo_setsum1)
blanchet@46369
   518
by metis+
paulson@23449
   519
blanchet@45575
   520
lemma bigo_setsum3: "f =o O(h) \<Longrightarrow>
wenzelm@61954
   521
    (\<lambda>x. \<Sum>y \<in> A x. (l x y) * f(k x y)) =o
wenzelm@61954
   522
      O(\<lambda>x. \<Sum>y \<in> A x. \<bar>l x y * h(k x y)\<bar>)"
blanchet@45575
   523
apply (rule bigo_setsum1)
blanchet@45575
   524
 apply (rule allI)+
blanchet@45575
   525
 apply (rule abs_ge_zero)
blanchet@45575
   526
apply (unfold bigo_def)
blanchet@45575
   527
apply (auto simp add: abs_mult)
haftmann@57512
   528
by (metis abs_ge_zero mult.left_commute mult_left_mono)
paulson@23449
   529
blanchet@45575
   530
lemma bigo_setsum4: "f =o g +o O(h) \<Longrightarrow>
wenzelm@61954
   531
    (\<lambda>x. \<Sum>y \<in> A x. l x y * f(k x y)) =o
wenzelm@61954
   532
      (\<lambda>x. \<Sum>y \<in> A x. l x y * g(k x y)) +o
wenzelm@61954
   533
        O(\<lambda>x. \<Sum>y \<in> A x. \<bar>l x y * h(k x y)\<bar>)"
blanchet@45575
   534
apply (rule set_minus_imp_plus)
blanchet@45575
   535
apply (subst fun_diff_def)
blanchet@45575
   536
apply (subst setsum_subtractf [symmetric])
blanchet@45575
   537
apply (subst right_diff_distrib [symmetric])
blanchet@45575
   538
apply (rule bigo_setsum3)
blanchet@46369
   539
by (metis (lifting, no_types) fun_diff_def set_plus_imp_minus ext)
paulson@23449
   540
blanchet@45575
   541
lemma bigo_setsum5: "f =o O(h) \<Longrightarrow> \<forall>x y. 0 <= l x y \<Longrightarrow>
blanchet@45575
   542
    \<forall>x. 0 <= h x \<Longrightarrow>
wenzelm@61954
   543
      (\<lambda>x. \<Sum>y \<in> A x. (l x y) * f(k x y)) =o
wenzelm@61954
   544
        O(\<lambda>x. \<Sum>y \<in> A x. (l x y) * h(k x y))"
wenzelm@61954
   545
apply (subgoal_tac "(\<lambda>x. \<Sum>y \<in> A x. (l x y) * h(k x y)) =
wenzelm@61954
   546
      (\<lambda>x. \<Sum>y \<in> A x. \<bar>(l x y) * h(k x y)\<bar>)")
blanchet@46369
   547
 apply (erule ssubst)
blanchet@46369
   548
 apply (erule bigo_setsum3)
blanchet@46369
   549
apply (rule ext)
haftmann@57418
   550
apply (rule setsum.cong)
haftmann@57418
   551
apply (rule refl)
blanchet@46369
   552
by (metis abs_of_nonneg zero_le_mult_iff)
paulson@23449
   553
blanchet@45575
   554
lemma bigo_setsum6: "f =o g +o O(h) \<Longrightarrow> \<forall>x y. 0 <= l x y \<Longrightarrow>
blanchet@45575
   555
    \<forall>x. 0 <= h x \<Longrightarrow>
wenzelm@61954
   556
      (\<lambda>x. \<Sum>y \<in> A x. (l x y) * f(k x y)) =o
wenzelm@61954
   557
        (\<lambda>x. \<Sum>y \<in> A x. (l x y) * g(k x y)) +o
wenzelm@61954
   558
          O(\<lambda>x. \<Sum>y \<in> A x. (l x y) * h(k x y))"
paulson@23449
   559
  apply (rule set_minus_imp_plus)
berghofe@26814
   560
  apply (subst fun_diff_def)
paulson@23449
   561
  apply (subst setsum_subtractf [symmetric])
paulson@23449
   562
  apply (subst right_diff_distrib [symmetric])
paulson@23449
   563
  apply (rule bigo_setsum5)
berghofe@26814
   564
  apply (subst fun_diff_def [symmetric])
paulson@23449
   565
  apply (drule set_plus_imp_minus)
paulson@23449
   566
  apply auto
paulson@23449
   567
done
paulson@23449
   568
paulson@23449
   569
subsection {* Misc useful stuff *}
paulson@23449
   570
blanchet@45575
   571
lemma bigo_useful_intro: "A <= O(f) \<Longrightarrow> B <= O(f) \<Longrightarrow>
krauss@47445
   572
  A + B <= O(f)"
paulson@23449
   573
  apply (subst bigo_plus_idemp [symmetric])
paulson@23449
   574
  apply (rule set_plus_mono2)
paulson@23449
   575
  apply assumption+
paulson@23449
   576
done
paulson@23449
   577
blanchet@45575
   578
lemma bigo_useful_add: "f =o O(h) \<Longrightarrow> g =o O(h) \<Longrightarrow> f + g =o O(h)"
paulson@23449
   579
  apply (subst bigo_plus_idemp [symmetric])
paulson@23449
   580
  apply (rule set_plus_intro)
paulson@23449
   581
  apply assumption+
paulson@23449
   582
done
blanchet@43197
   583
wenzelm@61076
   584
lemma bigo_useful_const_mult: "(c::'a::linordered_field) ~= 0 \<Longrightarrow>
blanchet@45575
   585
    (\<lambda>x. c) * f =o O(h) \<Longrightarrow> f =o O(h)"
paulson@23449
   586
  apply (rule subsetD)
blanchet@45575
   587
  apply (subgoal_tac "(\<lambda>x. 1 / c) *o O(h) <= O(h)")
paulson@23449
   588
  apply assumption
paulson@23449
   589
  apply (rule bigo_const_mult6)
blanchet@45575
   590
  apply (subgoal_tac "f = (\<lambda>x. 1 / c) * ((\<lambda>x. c) * f)")
paulson@23449
   591
  apply (erule ssubst)
paulson@23449
   592
  apply (erule set_times_intro2)
blanchet@43197
   593
  apply (simp add: func_times)
paulson@23449
   594
done
paulson@23449
   595
wenzelm@61076
   596
lemma bigo_fix: "(\<lambda>x. f ((x::nat) + 1)) =o O(\<lambda>x. h(x + 1)) \<Longrightarrow> f 0 = 0 \<Longrightarrow>
paulson@23449
   597
    f =o O(h)"
blanchet@45575
   598
apply (simp add: bigo_alt_def)
blanchet@45575
   599
by (metis abs_ge_zero abs_mult abs_of_pos abs_zero not0_implies_Suc)
paulson@23449
   600
blanchet@43197
   601
lemma bigo_fix2:
wenzelm@61076
   602
    "(\<lambda>x. f ((x::nat) + 1)) =o (\<lambda>x. g(x + 1)) +o O(\<lambda>x. h(x + 1)) \<Longrightarrow>
blanchet@45575
   603
       f 0 = g 0 \<Longrightarrow> f =o g +o O(h)"
paulson@23449
   604
  apply (rule set_minus_imp_plus)
paulson@23449
   605
  apply (rule bigo_fix)
berghofe@26814
   606
  apply (subst fun_diff_def)
berghofe@26814
   607
  apply (subst fun_diff_def [symmetric])
paulson@23449
   608
  apply (rule set_plus_imp_minus)
paulson@23449
   609
  apply simp
berghofe@26814
   610
  apply (simp add: fun_diff_def)
paulson@23449
   611
done
paulson@23449
   612
paulson@23449
   613
subsection {* Less than or equal to *}
paulson@23449
   614
wenzelm@61076
   615
definition lesso :: "('a => 'b::linordered_idom) => ('a => 'b) => ('a => 'b)" (infixl "<o" 70) where
blanchet@45575
   616
  "f <o g == (\<lambda>x. max (f x - g x) 0)"
paulson@23449
   617
wenzelm@61945
   618
lemma bigo_lesseq1: "f =o O(h) \<Longrightarrow> \<forall>x. \<bar>g x\<bar> <= \<bar>f x\<bar> \<Longrightarrow>
paulson@23449
   619
    g =o O(h)"
paulson@23449
   620
  apply (unfold bigo_def)
paulson@23449
   621
  apply clarsimp
blanchet@43197
   622
apply (blast intro: order_trans)
paulson@23449
   623
done
paulson@23449
   624
wenzelm@61945
   625
lemma bigo_lesseq2: "f =o O(h) \<Longrightarrow> \<forall>x. \<bar>g x\<bar> <= f x \<Longrightarrow>
paulson@23449
   626
      g =o O(h)"
paulson@23449
   627
  apply (erule bigo_lesseq1)
blanchet@43197
   628
apply (blast intro: abs_ge_self order_trans)
paulson@23449
   629
done
paulson@23449
   630
blanchet@45575
   631
lemma bigo_lesseq3: "f =o O(h) \<Longrightarrow> \<forall>x. 0 <= g x \<Longrightarrow> \<forall>x. g x <= f x \<Longrightarrow>
paulson@23449
   632
      g =o O(h)"
paulson@23449
   633
  apply (erule bigo_lesseq2)
paulson@23449
   634
  apply (rule allI)
paulson@23449
   635
  apply (subst abs_of_nonneg)
paulson@23449
   636
  apply (erule spec)+
paulson@23449
   637
done
paulson@23449
   638
blanchet@45575
   639
lemma bigo_lesseq4: "f =o O(h) \<Longrightarrow>
wenzelm@61945
   640
    \<forall>x. 0 <= g x \<Longrightarrow> \<forall>x. g x <= \<bar>f x\<bar> \<Longrightarrow>
paulson@23449
   641
      g =o O(h)"
paulson@23449
   642
  apply (erule bigo_lesseq1)
paulson@23449
   643
  apply (rule allI)
paulson@23449
   644
  apply (subst abs_of_nonneg)
paulson@23449
   645
  apply (erule spec)+
paulson@23449
   646
done
paulson@23449
   647
blanchet@45575
   648
lemma bigo_lesso1: "\<forall>x. f x <= g x \<Longrightarrow> f <o g =o O(h)"
blanchet@36561
   649
apply (unfold lesso_def)
blanchet@45575
   650
apply (subgoal_tac "(\<lambda>x. max (f x - g x) 0) = 0")
blanchet@45575
   651
 apply (metis bigo_zero)
blanchet@46364
   652
by (metis (lifting, no_types) func_zero le_fun_def le_iff_diff_le_0
haftmann@54863
   653
      max.absorb2 order_eq_iff)
paulson@23449
   654
blanchet@45575
   655
lemma bigo_lesso2: "f =o g +o O(h) \<Longrightarrow>
blanchet@45575
   656
    \<forall>x. 0 <= k x \<Longrightarrow> \<forall>x. k x <= f x \<Longrightarrow>
paulson@23449
   657
      k <o g =o O(h)"
paulson@23449
   658
  apply (unfold lesso_def)
paulson@23449
   659
  apply (rule bigo_lesseq4)
paulson@23449
   660
  apply (erule set_plus_imp_minus)
paulson@23449
   661
  apply (rule allI)
haftmann@54863
   662
  apply (rule max.cobounded2)
paulson@23449
   663
  apply (rule allI)
berghofe@26814
   664
  apply (subst fun_diff_def)
paulson@23449
   665
apply (erule thin_rl)
blanchet@45575
   666
(* sledgehammer *)
blanchet@45575
   667
apply (case_tac "0 <= k x - g x")
blanchet@46644
   668
 apply (metis (lifting) abs_le_D1 linorder_linear min_diff_distrib_left
haftmann@54863
   669
          min.absorb1 min.absorb2 max.absorb1)
haftmann@54863
   670
by (metis abs_ge_zero le_cases max.absorb2)
paulson@23449
   671
blanchet@45575
   672
lemma bigo_lesso3: "f =o g +o O(h) \<Longrightarrow>
blanchet@45575
   673
    \<forall>x. 0 <= k x \<Longrightarrow> \<forall>x. g x <= k x \<Longrightarrow>
paulson@23449
   674
      f <o k =o O(h)"
blanchet@46644
   675
apply (unfold lesso_def)
blanchet@46644
   676
apply (rule bigo_lesseq4)
paulson@23449
   677
  apply (erule set_plus_imp_minus)
blanchet@46644
   678
 apply (rule allI)
haftmann@54863
   679
 apply (rule max.cobounded2)
blanchet@46644
   680
apply (rule allI)
blanchet@46644
   681
apply (subst fun_diff_def)
blanchet@46644
   682
apply (erule thin_rl)
blanchet@46644
   683
(* sledgehammer *)
blanchet@46644
   684
apply (case_tac "0 <= f x - k x")
blanchet@46644
   685
 apply simp
blanchet@46644
   686
 apply (subst abs_of_nonneg)
paulson@23449
   687
  apply (drule_tac x = x in spec) back
lp15@61824
   688
  apply (metis diff_less_0_iff_less linorder_not_le not_le_imp_less xt1(12) xt1(6))
blanchet@45575
   689
 apply (metis add_minus_cancel diff_le_eq le_diff_eq uminus_add_conv_diff)
haftmann@54863
   690
by (metis abs_ge_zero linorder_linear max.absorb1 max.commute)
paulson@23449
   691
blanchet@45705
   692
lemma bigo_lesso4:
wenzelm@61076
   693
  "f <o g =o O(k::'a=>'b::{linordered_field}) \<Longrightarrow>
blanchet@45705
   694
   g =o h +o O(k) \<Longrightarrow> f <o h =o O(k)"
blanchet@45705
   695
apply (unfold lesso_def)
blanchet@45705
   696
apply (drule set_plus_imp_minus)
blanchet@45705
   697
apply (drule bigo_abs5) back
blanchet@45705
   698
apply (simp add: fun_diff_def)
blanchet@45705
   699
apply (drule bigo_useful_add, assumption)
blanchet@45705
   700
apply (erule bigo_lesseq2) back
blanchet@45705
   701
apply (rule allI)
blanchet@45705
   702
by (auto simp add: func_plus fun_diff_def algebra_simps
paulson@23449
   703
    split: split_max abs_split)
paulson@23449
   704
wenzelm@61945
   705
lemma bigo_lesso5: "f <o g =o O(h) \<Longrightarrow> \<exists>C. \<forall>x. f x <= g x + C * \<bar>h x\<bar>"
blanchet@45705
   706
apply (simp only: lesso_def bigo_alt_def)
blanchet@45705
   707
apply clarsimp
haftmann@57512
   708
by (metis add.commute diff_le_eq)
paulson@23449
   709
paulson@23449
   710
end