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