src/HOL/Metis_Examples/BigO.thy
author wenzelm
Wed Dec 29 17:34:41 2010 +0100 (2010-12-29)
changeset 41413 64cd30d6b0b8
parent 41144 509e51b7509a
child 41541 1fa4725c4656
permissions -rw-r--r--
explicit file specifications -- avoid secondary load path;
wenzelm@33027
     1
(*  Title:      HOL/Metis_Examples/BigO.thy
paulson@23449
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
blanchet@41144
     3
    Author:     Jasmin Blanchette, TU Muenchen
paulson@23449
     4
blanchet@41144
     5
Testing Metis.
paulson@23449
     6
*)
paulson@23449
     7
paulson@23449
     8
header {* Big O notation *}
paulson@23449
     9
paulson@23449
    10
theory BigO
wenzelm@41413
    11
imports
wenzelm@41413
    12
  "~~/src/HOL/Decision_Procs/Dense_Linear_Order"
wenzelm@41413
    13
  Main
wenzelm@41413
    14
  "~~/src/HOL/Library/Function_Algebras"
wenzelm@41413
    15
  "~~/src/HOL/Library/Set_Algebras"
paulson@23449
    16
begin
paulson@23449
    17
paulson@23449
    18
subsection {* Definitions *}
paulson@23449
    19
haftmann@35028
    20
definition bigo :: "('a => 'b::linordered_idom) => ('a => 'b) set"    ("(1O'(_'))") where
paulson@23449
    21
  "O(f::('a => 'b)) ==   {h. EX c. ALL x. abs (h x) <= c * abs (f x)}"
paulson@23449
    22
blanchet@38991
    23
declare [[ sledgehammer_problem_prefix = "BigO__bigo_pos_const" ]]
haftmann@35028
    24
lemma bigo_pos_const: "(EX (c::'a::linordered_idom). 
paulson@23449
    25
    ALL x. (abs (h x)) <= (c * (abs (f x))))
paulson@23449
    26
      = (EX c. 0 < c & (ALL x. (abs(h x)) <= (c * (abs (f x)))))"
paulson@23449
    27
  apply auto
paulson@23449
    28
  apply (case_tac "c = 0", simp)
paulson@23449
    29
  apply (rule_tac x = "1" in exI, simp)
haftmann@25304
    30
  apply (rule_tac x = "abs c" in exI, auto)
blanchet@36561
    31
  apply (metis abs_ge_zero abs_of_nonneg Orderings.xt1(6) abs_mult)
paulson@23449
    32
  done
paulson@23449
    33
blanchet@36407
    34
(*** Now various verions with an increasing shrink factor ***)
paulson@23449
    35
blanchet@36925
    36
sledgehammer_params [isar_proof, isar_shrink_factor = 1]
paulson@23449
    37
haftmann@35028
    38
lemma (*bigo_pos_const:*) "(EX (c::'a::linordered_idom). 
paulson@23449
    39
    ALL x. (abs (h x)) <= (c * (abs (f x))))
paulson@23449
    40
      = (EX c. 0 < c & (ALL x. (abs(h x)) <= (c * (abs (f x)))))"
paulson@23449
    41
  apply auto
paulson@23449
    42
  apply (case_tac "c = 0", simp)
paulson@23449
    43
  apply (rule_tac x = "1" in exI, simp)
paulson@23449
    44
  apply (rule_tac x = "abs c" in exI, auto)
blanchet@36561
    45
proof -
blanchet@36561
    46
  fix c :: 'a and x :: 'b
blanchet@36561
    47
  assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
blanchet@36561
    48
  have F1: "\<forall>x\<^isub>1\<Colon>'a\<Colon>linordered_idom. 0 \<le> \<bar>x\<^isub>1\<bar>" by (metis abs_ge_zero)
hoelzl@36844
    49
  have F2: "\<forall>x\<^isub>1\<Colon>'a\<Colon>linordered_idom. 1 * x\<^isub>1 = x\<^isub>1" by (metis mult_1)
blanchet@36561
    50
  have F3: "\<forall>x\<^isub>1 x\<^isub>3. x\<^isub>3 \<le> \<bar>h x\<^isub>1\<bar> \<longrightarrow> x\<^isub>3 \<le> c * \<bar>f x\<^isub>1\<bar>" by (metis A1 order_trans)
blanchet@36561
    51
  have F4: "\<forall>x\<^isub>2 x\<^isub>3\<Colon>'a\<Colon>linordered_idom. \<bar>x\<^isub>3\<bar> * \<bar>x\<^isub>2\<bar> = \<bar>x\<^isub>3 * x\<^isub>2\<bar>"
blanchet@36561
    52
    by (metis abs_mult)
blanchet@36561
    53
  have F5: "\<forall>x\<^isub>3 x\<^isub>1\<Colon>'a\<Colon>linordered_idom. 0 \<le> x\<^isub>1 \<longrightarrow> \<bar>x\<^isub>3 * x\<^isub>1\<bar> = \<bar>x\<^isub>3\<bar> * x\<^isub>1"
blanchet@36561
    54
    by (metis abs_mult_pos)
blanchet@36561
    55
  hence "\<forall>x\<^isub>1\<ge>0. \<bar>x\<^isub>1\<Colon>'a\<Colon>linordered_idom\<bar> = \<bar>1\<bar> * x\<^isub>1" by (metis F2)
blanchet@36561
    56
  hence "\<forall>x\<^isub>1\<ge>0. \<bar>x\<^isub>1\<Colon>'a\<Colon>linordered_idom\<bar> = x\<^isub>1" by (metis F2 abs_one)
blanchet@36561
    57
  hence "\<forall>x\<^isub>3. 0 \<le> \<bar>h x\<^isub>3\<bar> \<longrightarrow> \<bar>c * \<bar>f x\<^isub>3\<bar>\<bar> = c * \<bar>f x\<^isub>3\<bar>" by (metis F3)
blanchet@36561
    58
  hence "\<forall>x\<^isub>3. \<bar>c * \<bar>f x\<^isub>3\<bar>\<bar> = c * \<bar>f x\<^isub>3\<bar>" by (metis F1)
blanchet@36561
    59
  hence "\<forall>x\<^isub>3. (0\<Colon>'a) \<le> \<bar>f x\<^isub>3\<bar> \<longrightarrow> c * \<bar>f x\<^isub>3\<bar> = \<bar>c\<bar> * \<bar>f x\<^isub>3\<bar>" by (metis F5)
blanchet@36561
    60
  hence "\<forall>x\<^isub>3. (0\<Colon>'a) \<le> \<bar>f x\<^isub>3\<bar> \<longrightarrow> c * \<bar>f x\<^isub>3\<bar> = \<bar>c * f x\<^isub>3\<bar>" by (metis F4)
blanchet@36561
    61
  hence "\<forall>x\<^isub>3. c * \<bar>f x\<^isub>3\<bar> = \<bar>c * f x\<^isub>3\<bar>" by (metis F1)
blanchet@36561
    62
  hence "\<bar>h x\<bar> \<le> \<bar>c * f x\<bar>" by (metis A1)
blanchet@36561
    63
  thus "\<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>" by (metis F4)
paulson@23449
    64
qed
paulson@23449
    65
blanchet@36925
    66
sledgehammer_params [isar_proof, isar_shrink_factor = 2]
paulson@25710
    67
haftmann@35028
    68
lemma (*bigo_pos_const:*) "(EX (c::'a::linordered_idom). 
paulson@23449
    69
    ALL x. (abs (h x)) <= (c * (abs (f x))))
paulson@23449
    70
      = (EX c. 0 < c & (ALL x. (abs(h x)) <= (c * (abs (f x)))))"
paulson@23449
    71
  apply auto
paulson@23449
    72
  apply (case_tac "c = 0", simp)
paulson@23449
    73
  apply (rule_tac x = "1" in exI, simp)
hoelzl@36844
    74
  apply (rule_tac x = "abs c" in exI, auto)
blanchet@36561
    75
proof -
blanchet@36561
    76
  fix c :: 'a and x :: 'b
blanchet@36561
    77
  assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
hoelzl@36844
    78
  have F1: "\<forall>x\<^isub>1\<Colon>'a\<Colon>linordered_idom. 1 * x\<^isub>1 = x\<^isub>1" by (metis mult_1)
blanchet@36561
    79
  have F2: "\<forall>x\<^isub>2 x\<^isub>3\<Colon>'a\<Colon>linordered_idom. \<bar>x\<^isub>3\<bar> * \<bar>x\<^isub>2\<bar> = \<bar>x\<^isub>3 * x\<^isub>2\<bar>"
blanchet@36561
    80
    by (metis abs_mult)
blanchet@36561
    81
  have "\<forall>x\<^isub>1\<ge>0. \<bar>x\<^isub>1\<Colon>'a\<Colon>linordered_idom\<bar> = x\<^isub>1" by (metis F1 abs_mult_pos abs_one)
blanchet@36561
    82
  hence "\<forall>x\<^isub>3. \<bar>c * \<bar>f x\<^isub>3\<bar>\<bar> = c * \<bar>f x\<^isub>3\<bar>" by (metis A1 abs_ge_zero order_trans)
blanchet@36561
    83
  hence "\<forall>x\<^isub>3. 0 \<le> \<bar>f x\<^isub>3\<bar> \<longrightarrow> c * \<bar>f x\<^isub>3\<bar> = \<bar>c * f x\<^isub>3\<bar>" by (metis F2 abs_mult_pos)
blanchet@36561
    84
  hence "\<bar>h x\<bar> \<le> \<bar>c * f x\<bar>" by (metis A1 abs_ge_zero)
blanchet@36561
    85
  thus "\<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>" by (metis F2)
paulson@23449
    86
qed
paulson@23449
    87
blanchet@36925
    88
sledgehammer_params [isar_proof, isar_shrink_factor = 3]
paulson@25710
    89
haftmann@35028
    90
lemma (*bigo_pos_const:*) "(EX (c::'a::linordered_idom). 
paulson@23449
    91
    ALL x. (abs (h x)) <= (c * (abs (f x))))
paulson@23449
    92
      = (EX c. 0 < c & (ALL x. (abs(h x)) <= (c * (abs (f x)))))"
paulson@23449
    93
  apply auto
paulson@23449
    94
  apply (case_tac "c = 0", simp)
paulson@23449
    95
  apply (rule_tac x = "1" in exI, simp)
blanchet@36561
    96
  apply (rule_tac x = "abs c" in exI, auto)
blanchet@36561
    97
proof -
blanchet@36561
    98
  fix c :: 'a and x :: 'b
blanchet@36561
    99
  assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
hoelzl@36844
   100
  have F1: "\<forall>x\<^isub>1\<Colon>'a\<Colon>linordered_idom. 1 * x\<^isub>1 = x\<^isub>1" by (metis mult_1)
blanchet@36561
   101
  have F2: "\<forall>x\<^isub>3 x\<^isub>1\<Colon>'a\<Colon>linordered_idom. 0 \<le> x\<^isub>1 \<longrightarrow> \<bar>x\<^isub>3 * x\<^isub>1\<bar> = \<bar>x\<^isub>3\<bar> * x\<^isub>1" by (metis abs_mult_pos)
blanchet@36561
   102
  hence "\<forall>x\<^isub>1\<ge>0. \<bar>x\<^isub>1\<Colon>'a\<Colon>linordered_idom\<bar> = x\<^isub>1" by (metis F1 abs_one)
blanchet@36561
   103
  hence "\<forall>x\<^isub>3. 0 \<le> \<bar>f x\<^isub>3\<bar> \<longrightarrow> c * \<bar>f x\<^isub>3\<bar> = \<bar>c\<bar> * \<bar>f x\<^isub>3\<bar>" by (metis F2 A1 abs_ge_zero order_trans)
blanchet@36561
   104
  thus "\<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>" by (metis A1 abs_mult abs_ge_zero)
paulson@23449
   105
qed
paulson@23449
   106
blanchet@36925
   107
sledgehammer_params [isar_proof, isar_shrink_factor = 4]
paulson@24545
   108
haftmann@35028
   109
lemma (*bigo_pos_const:*) "(EX (c::'a::linordered_idom). 
paulson@24545
   110
    ALL x. (abs (h x)) <= (c * (abs (f x))))
paulson@24545
   111
      = (EX c. 0 < c & (ALL x. (abs(h x)) <= (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>"
hoelzl@36844
   119
  have "\<forall>x\<^isub>1\<Colon>'a\<Colon>linordered_idom. 1 * x\<^isub>1 = x\<^isub>1" by (metis mult_1)
blanchet@36561
   120
  hence "\<forall>x\<^isub>3. \<bar>c * \<bar>f x\<^isub>3\<bar>\<bar> = c * \<bar>f x\<^isub>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@36925
   126
sledgehammer_params [isar_proof, isar_shrink_factor = 1]
paulson@24545
   127
paulson@23449
   128
lemma bigo_alt_def: "O(f) = 
paulson@23449
   129
    {h. EX c. (0 < c & (ALL x. abs (h x) <= c * abs (f x)))}"
paulson@23449
   130
by (auto simp add: bigo_def bigo_pos_const)
paulson@23449
   131
blanchet@38991
   132
declare [[ sledgehammer_problem_prefix = "BigO__bigo_elt_subset" ]]
paulson@23449
   133
lemma bigo_elt_subset [intro]: "f : O(g) ==> O(f) <= O(g)"
paulson@23449
   134
  apply (auto simp add: bigo_alt_def)
paulson@23449
   135
  apply (rule_tac x = "ca * c" in exI)
paulson@23449
   136
  apply (rule conjI)
paulson@23449
   137
  apply (rule mult_pos_pos)
paulson@23449
   138
  apply (assumption)+ 
hoelzl@36844
   139
(*sledgehammer*)
paulson@23449
   140
  apply (rule allI)
paulson@23449
   141
  apply (drule_tac x = "xa" in spec)+
hoelzl@36844
   142
  apply (subgoal_tac "ca * abs(f xa) <= ca * (c * abs(g xa))")
paulson@23449
   143
  apply (erule order_trans)
paulson@23449
   144
  apply (simp add: mult_ac)
paulson@23449
   145
  apply (rule mult_left_mono, assumption)
hoelzl@36844
   146
  apply (rule order_less_imp_le, assumption)
paulson@23449
   147
done
paulson@23449
   148
paulson@23449
   149
blanchet@38991
   150
declare [[ sledgehammer_problem_prefix = "BigO__bigo_refl" ]]
paulson@23449
   151
lemma bigo_refl [intro]: "f : O(f)"
blanchet@36561
   152
apply (auto simp add: bigo_def)
hoelzl@36844
   153
by (metis mult_1 order_refl)
paulson@23449
   154
blanchet@38991
   155
declare [[ sledgehammer_problem_prefix = "BigO__bigo_zero" ]]
paulson@23449
   156
lemma bigo_zero: "0 : O(g)"
blanchet@36561
   157
apply (auto simp add: bigo_def func_zero)
hoelzl@36844
   158
by (metis mult_zero_left order_refl)
paulson@23449
   159
paulson@23449
   160
lemma bigo_zero2: "O(%x.0) = {%x.0}"
paulson@23449
   161
  apply (auto simp add: bigo_def) 
paulson@23449
   162
  apply (rule ext)
paulson@23449
   163
  apply auto
paulson@23449
   164
done
paulson@23449
   165
paulson@23449
   166
lemma bigo_plus_self_subset [intro]: 
berghofe@26814
   167
  "O(f) \<oplus> O(f) <= O(f)"
berghofe@26814
   168
  apply (auto simp add: bigo_alt_def set_plus_def)
paulson@23449
   169
  apply (rule_tac x = "c + ca" in exI)
paulson@23449
   170
  apply auto
nipkow@23477
   171
  apply (simp add: ring_distribs func_plus)
paulson@23449
   172
  apply (blast intro:order_trans abs_triangle_ineq add_mono elim:) 
paulson@23449
   173
done
paulson@23449
   174
berghofe@26814
   175
lemma bigo_plus_idemp [simp]: "O(f) \<oplus> O(f) = O(f)"
paulson@23449
   176
  apply (rule equalityI)
paulson@23449
   177
  apply (rule bigo_plus_self_subset)
paulson@23449
   178
  apply (rule set_zero_plus2) 
paulson@23449
   179
  apply (rule bigo_zero)
paulson@23449
   180
done
paulson@23449
   181
berghofe@26814
   182
lemma bigo_plus_subset [intro]: "O(f + g) <= O(f) \<oplus> O(g)"
paulson@23449
   183
  apply (rule subsetI)
berghofe@26814
   184
  apply (auto simp add: bigo_def bigo_pos_const func_plus set_plus_def)
paulson@23449
   185
  apply (subst bigo_pos_const [symmetric])+
paulson@23449
   186
  apply (rule_tac x = 
paulson@23449
   187
    "%n. if abs (g n) <= (abs (f n)) then x n else 0" in exI)
paulson@23449
   188
  apply (rule conjI)
paulson@23449
   189
  apply (rule_tac x = "c + c" in exI)
paulson@23449
   190
  apply (clarsimp)
paulson@23449
   191
  apply (auto)
paulson@23449
   192
  apply (subgoal_tac "c * abs (f xa + g xa) <= (c + c) * abs (f xa)")
paulson@23449
   193
  apply (erule_tac x = xa in allE)
paulson@23449
   194
  apply (erule order_trans)
paulson@23449
   195
  apply (simp)
paulson@23449
   196
  apply (subgoal_tac "c * abs (f xa + g xa) <= c * (abs (f xa) + abs (g xa))")
paulson@23449
   197
  apply (erule order_trans)
nipkow@23477
   198
  apply (simp add: ring_distribs)
paulson@23449
   199
  apply (rule mult_left_mono)
paulson@23449
   200
  apply assumption
paulson@23449
   201
  apply (simp add: order_less_le)
paulson@23449
   202
  apply (rule mult_left_mono)
paulson@23449
   203
  apply (simp add: abs_triangle_ineq)
paulson@23449
   204
  apply (simp add: order_less_le)
paulson@23449
   205
  apply (rule mult_nonneg_nonneg)
paulson@23449
   206
  apply (rule add_nonneg_nonneg)
paulson@23449
   207
  apply auto
paulson@23449
   208
  apply (rule_tac x = "%n. if (abs (f n)) <  abs (g n) then x n else 0" 
paulson@23449
   209
     in exI)
paulson@23449
   210
  apply (rule conjI)
paulson@23449
   211
  apply (rule_tac x = "c + c" in exI)
paulson@23449
   212
  apply auto
paulson@23449
   213
  apply (subgoal_tac "c * abs (f xa + g xa) <= (c + c) * abs (g xa)")
paulson@23449
   214
  apply (erule_tac x = xa in allE)
paulson@23449
   215
  apply (erule order_trans)
paulson@23449
   216
  apply (simp)
paulson@23449
   217
  apply (subgoal_tac "c * abs (f xa + g xa) <= c * (abs (f xa) + abs (g xa))")
paulson@23449
   218
  apply (erule order_trans)
nipkow@23477
   219
  apply (simp add: ring_distribs)
paulson@23449
   220
  apply (rule mult_left_mono)
paulson@23449
   221
  apply (simp add: order_less_le)
paulson@23449
   222
  apply (simp add: order_less_le)
paulson@23449
   223
  apply (rule mult_left_mono)
paulson@23449
   224
  apply (rule abs_triangle_ineq)
paulson@23449
   225
  apply (simp add: order_less_le)
paulson@25087
   226
apply (metis abs_not_less_zero double_less_0_iff less_not_permute linorder_not_less mult_less_0_iff)
paulson@23449
   227
  apply (rule ext)
paulson@23449
   228
  apply (auto simp add: if_splits linorder_not_le)
paulson@23449
   229
done
paulson@23449
   230
berghofe@26814
   231
lemma bigo_plus_subset2 [intro]: "A <= O(f) ==> B <= O(f) ==> A \<oplus> B <= O(f)"
berghofe@26814
   232
  apply (subgoal_tac "A \<oplus> B <= O(f) \<oplus> O(f)")
paulson@23449
   233
  apply (erule order_trans)
paulson@23449
   234
  apply simp
paulson@23449
   235
  apply (auto del: subsetI simp del: bigo_plus_idemp)
paulson@23449
   236
done
paulson@23449
   237
blanchet@38991
   238
declare [[ sledgehammer_problem_prefix = "BigO__bigo_plus_eq" ]]
paulson@23449
   239
lemma bigo_plus_eq: "ALL x. 0 <= f x ==> ALL x. 0 <= g x ==> 
berghofe@26814
   240
  O(f + g) = O(f) \<oplus> O(g)"
paulson@23449
   241
  apply (rule equalityI)
paulson@23449
   242
  apply (rule bigo_plus_subset)
berghofe@26814
   243
  apply (simp add: bigo_alt_def set_plus_def func_plus)
paulson@23449
   244
  apply clarify 
hoelzl@36844
   245
(*sledgehammer*) 
paulson@23449
   246
  apply (rule_tac x = "max c ca" in exI)
paulson@23449
   247
  apply (rule conjI)
paulson@25087
   248
   apply (metis Orderings.less_max_iff_disj)
paulson@23449
   249
  apply clarify
paulson@23449
   250
  apply (drule_tac x = "xa" in spec)+
paulson@23449
   251
  apply (subgoal_tac "0 <= f xa + g xa")
nipkow@23477
   252
  apply (simp add: ring_distribs)
paulson@23449
   253
  apply (subgoal_tac "abs(a xa + b xa) <= abs(a xa) + abs(b xa)")
paulson@23449
   254
  apply (subgoal_tac "abs(a xa) + abs(b xa) <= 
paulson@23449
   255
      max c ca * f xa + max c ca * g xa")
paulson@23449
   256
  apply (blast intro: order_trans)
paulson@23449
   257
  defer 1
paulson@23449
   258
  apply (rule abs_triangle_ineq)
paulson@25087
   259
  apply (metis add_nonneg_nonneg)
paulson@23449
   260
  apply (rule add_mono)
blanchet@39259
   261
using [[ sledgehammer_problem_prefix = "BigO__bigo_plus_eq_simpler" ]]
blanchet@39259
   262
  apply (metis le_maxI2 linorder_linear min_max.sup_absorb1 mult_right_mono xt1(6))
blanchet@39259
   263
  apply (metis le_maxI2 linorder_not_le mult_le_cancel_right order_trans)
paulson@23449
   264
done
paulson@23449
   265
blanchet@38991
   266
declare [[ sledgehammer_problem_prefix = "BigO__bigo_bounded_alt" ]]
paulson@23449
   267
lemma bigo_bounded_alt: "ALL x. 0 <= f x ==> ALL x. f x <= c * g x ==> 
paulson@23449
   268
    f : O(g)" 
paulson@23449
   269
  apply (auto simp add: bigo_def)
blanchet@36561
   270
(* Version 1: one-line proof *)
haftmann@35050
   271
  apply (metis abs_le_D1 linorder_class.not_less  order_less_le  Orderings.xt1(12)  abs_mult)
paulson@23449
   272
  done
paulson@23449
   273
wenzelm@26312
   274
lemma (*bigo_bounded_alt:*) "ALL x. 0 <= f x ==> ALL x. f x <= c * g x ==> 
blanchet@36561
   275
    f : O(g)"
blanchet@36561
   276
apply (auto simp add: bigo_def)
blanchet@36561
   277
(* Version 2: structured proof *)
blanchet@36561
   278
proof -
blanchet@36561
   279
  assume "\<forall>x. f x \<le> c * g x"
blanchet@36561
   280
  thus "\<exists>c. \<forall>x. f x \<le> c * \<bar>g x\<bar>" by (metis abs_mult abs_ge_self order_trans)
paulson@23449
   281
qed
paulson@23449
   282
blanchet@36561
   283
text{*So here is the easier (and more natural) problem using transitivity*}
blanchet@38991
   284
declare [[ sledgehammer_problem_prefix = "BigO__bigo_bounded_alt_trans" ]]
blanchet@36561
   285
lemma "ALL x. 0 <= f x ==> ALL x. f x <= c * g x ==> f : O(g)" 
blanchet@36561
   286
apply (auto simp add: bigo_def)
blanchet@36561
   287
(* Version 1: one-line proof *)
blanchet@36561
   288
by (metis abs_ge_self abs_mult order_trans)
paulson@23449
   289
paulson@23449
   290
text{*So here is the easier (and more natural) problem using transitivity*}
blanchet@38991
   291
declare [[ sledgehammer_problem_prefix = "BigO__bigo_bounded_alt_trans" ]]
paulson@23449
   292
lemma "ALL x. 0 <= f x ==> ALL x. f x <= c * g x ==> f : O(g)" 
paulson@23449
   293
  apply (auto simp add: bigo_def)
blanchet@36561
   294
(* Version 2: structured proof *)
blanchet@36561
   295
proof -
blanchet@36561
   296
  assume "\<forall>x. f x \<le> c * g x"
blanchet@36561
   297
  thus "\<exists>c. \<forall>x. f x \<le> c * \<bar>g x\<bar>" by (metis abs_mult abs_ge_self order_trans)
paulson@23449
   298
qed
paulson@23449
   299
paulson@23449
   300
lemma bigo_bounded: "ALL x. 0 <= f x ==> ALL x. f x <= g x ==> 
paulson@23449
   301
    f : O(g)" 
paulson@23449
   302
  apply (erule bigo_bounded_alt [of f 1 g])
paulson@23449
   303
  apply simp
paulson@23449
   304
done
paulson@23449
   305
blanchet@38991
   306
declare [[ sledgehammer_problem_prefix = "BigO__bigo_bounded2" ]]
paulson@23449
   307
lemma bigo_bounded2: "ALL x. lb x <= f x ==> ALL x. f x <= lb x + g x ==>
paulson@23449
   308
    f : lb +o O(g)"
blanchet@36561
   309
apply (rule set_minus_imp_plus)
blanchet@36561
   310
apply (rule bigo_bounded)
blanchet@36561
   311
 apply (auto simp add: diff_minus fun_Compl_def func_plus)
blanchet@36561
   312
 prefer 2
blanchet@36561
   313
 apply (drule_tac x = x in spec)+
hoelzl@36844
   314
 apply (metis add_right_mono add_commute diff_add_cancel diff_minus_eq_add le_less order_trans)
blanchet@36561
   315
proof -
blanchet@36561
   316
  fix x :: 'a
blanchet@36561
   317
  assume "\<forall>x. lb x \<le> f x"
blanchet@36561
   318
  thus "(0\<Colon>'b) \<le> f x + - lb x" by (metis not_leE diff_minus less_iff_diff_less_0 less_le_not_le)
paulson@23449
   319
qed
paulson@23449
   320
blanchet@38991
   321
declare [[ sledgehammer_problem_prefix = "BigO__bigo_abs" ]]
paulson@23449
   322
lemma bigo_abs: "(%x. abs(f x)) =o O(f)" 
blanchet@36561
   323
apply (unfold bigo_def)
blanchet@36561
   324
apply auto
hoelzl@36844
   325
by (metis mult_1 order_refl)
paulson@23449
   326
blanchet@38991
   327
declare [[ sledgehammer_problem_prefix = "BigO__bigo_abs2" ]]
paulson@23449
   328
lemma bigo_abs2: "f =o O(%x. abs(f x))"
blanchet@36561
   329
apply (unfold bigo_def)
blanchet@36561
   330
apply auto
hoelzl@36844
   331
by (metis mult_1 order_refl)
paulson@23449
   332
 
paulson@23449
   333
lemma bigo_abs3: "O(f) = O(%x. abs(f x))"
blanchet@36561
   334
proof -
blanchet@36561
   335
  have F1: "\<forall>v u. u \<in> O(v) \<longrightarrow> O(u) \<subseteq> O(v)" by (metis bigo_elt_subset)
blanchet@36561
   336
  have F2: "\<forall>u. (\<lambda>R. \<bar>u R\<bar>) \<in> O(u)" by (metis bigo_abs)
blanchet@36561
   337
  have "\<forall>u. u \<in> O(\<lambda>R. \<bar>u R\<bar>)" by (metis bigo_abs2)
blanchet@36561
   338
  thus "O(f) = O(\<lambda>x. \<bar>f x\<bar>)" using F1 F2 by auto
blanchet@36561
   339
qed 
paulson@23449
   340
paulson@23449
   341
lemma bigo_abs4: "f =o g +o O(h) ==> 
paulson@23449
   342
    (%x. abs (f x)) =o (%x. abs (g x)) +o O(h)"
paulson@23449
   343
  apply (drule set_plus_imp_minus)
paulson@23449
   344
  apply (rule set_minus_imp_plus)
berghofe@26814
   345
  apply (subst fun_diff_def)
paulson@23449
   346
proof -
paulson@23449
   347
  assume a: "f - g : O(h)"
paulson@23449
   348
  have "(%x. abs (f x) - abs (g x)) =o O(%x. abs(abs (f x) - abs (g x)))"
paulson@23449
   349
    by (rule bigo_abs2)
paulson@23449
   350
  also have "... <= O(%x. abs (f x - g x))"
paulson@23449
   351
    apply (rule bigo_elt_subset)
paulson@23449
   352
    apply (rule bigo_bounded)
paulson@23449
   353
    apply force
paulson@23449
   354
    apply (rule allI)
paulson@23449
   355
    apply (rule abs_triangle_ineq3)
paulson@23449
   356
    done
paulson@23449
   357
  also have "... <= O(f - g)"
paulson@23449
   358
    apply (rule bigo_elt_subset)
berghofe@26814
   359
    apply (subst fun_diff_def)
paulson@23449
   360
    apply (rule bigo_abs)
paulson@23449
   361
    done
paulson@23449
   362
  also have "... <= O(h)"
wenzelm@23464
   363
    using a by (rule bigo_elt_subset)
paulson@23449
   364
  finally show "(%x. abs (f x) - abs (g x)) : O(h)".
paulson@23449
   365
qed
paulson@23449
   366
paulson@23449
   367
lemma bigo_abs5: "f =o O(g) ==> (%x. abs(f x)) =o O(g)" 
paulson@23449
   368
by (unfold bigo_def, auto)
paulson@23449
   369
berghofe@26814
   370
lemma bigo_elt_subset2 [intro]: "f : g +o O(h) ==> O(f) <= O(g) \<oplus> O(h)"
paulson@23449
   371
proof -
paulson@23449
   372
  assume "f : g +o O(h)"
berghofe@26814
   373
  also have "... <= O(g) \<oplus> O(h)"
paulson@23449
   374
    by (auto del: subsetI)
berghofe@26814
   375
  also have "... = O(%x. abs(g x)) \<oplus> O(%x. abs(h x))"
paulson@23449
   376
    apply (subst bigo_abs3 [symmetric])+
paulson@23449
   377
    apply (rule refl)
paulson@23449
   378
    done
paulson@23449
   379
  also have "... = O((%x. abs(g x)) + (%x. abs(h x)))"
paulson@23449
   380
    by (rule bigo_plus_eq [symmetric], auto)
paulson@23449
   381
  finally have "f : ...".
paulson@23449
   382
  then have "O(f) <= ..."
paulson@23449
   383
    by (elim bigo_elt_subset)
berghofe@26814
   384
  also have "... = O(%x. abs(g x)) \<oplus> O(%x. abs(h x))"
paulson@23449
   385
    by (rule bigo_plus_eq, auto)
paulson@23449
   386
  finally show ?thesis
paulson@23449
   387
    by (simp add: bigo_abs3 [symmetric])
paulson@23449
   388
qed
paulson@23449
   389
blanchet@38991
   390
declare [[ sledgehammer_problem_prefix = "BigO__bigo_mult" ]]
berghofe@26814
   391
lemma bigo_mult [intro]: "O(f)\<otimes>O(g) <= O(f * g)"
paulson@23449
   392
  apply (rule subsetI)
paulson@23449
   393
  apply (subst bigo_def)
paulson@23449
   394
  apply (auto simp del: abs_mult mult_ac
berghofe@26814
   395
              simp add: bigo_alt_def set_times_def func_times)
paulson@23449
   396
(*sledgehammer*); 
paulson@23449
   397
  apply (rule_tac x = "c * ca" in exI)
paulson@23449
   398
  apply(rule allI)
paulson@23449
   399
  apply(erule_tac x = x in allE)+
paulson@23449
   400
  apply(subgoal_tac "c * ca * abs(f x * g x) = 
paulson@23449
   401
      (c * abs(f x)) * (ca * abs(g x))")
blanchet@38991
   402
using [[ sledgehammer_problem_prefix = "BigO__bigo_mult_simpler" ]]
paulson@23449
   403
prefer 2 
haftmann@26041
   404
apply (metis mult_assoc mult_left_commute
haftmann@35050
   405
  abs_of_pos mult_left_commute
haftmann@35050
   406
  abs_mult mult_pos_pos)
haftmann@26041
   407
  apply (erule ssubst) 
paulson@23449
   408
  apply (subst abs_mult)
blanchet@36561
   409
(* not quite as hard as BigO__bigo_mult_simpler_1 (a hard problem!) since
blanchet@36561
   410
   abs_mult has just been done *)
blanchet@36561
   411
by (metis abs_ge_zero mult_mono')
paulson@23449
   412
blanchet@38991
   413
declare [[ sledgehammer_problem_prefix = "BigO__bigo_mult2" ]]
paulson@23449
   414
lemma bigo_mult2 [intro]: "f *o O(g) <= O(f * g)"
paulson@23449
   415
  apply (auto simp add: bigo_def elt_set_times_def func_times abs_mult)
paulson@23449
   416
(*sledgehammer*); 
paulson@23449
   417
  apply (rule_tac x = c in exI)
paulson@23449
   418
  apply clarify
paulson@23449
   419
  apply (drule_tac x = x in spec)
blanchet@38991
   420
using [[ sledgehammer_problem_prefix = "BigO__bigo_mult2_simpler" ]]
paulson@24942
   421
(*sledgehammer [no luck]*); 
paulson@23449
   422
  apply (subgoal_tac "abs(f x) * abs(b x) <= abs(f x) * (c * abs(g x))")
paulson@23449
   423
  apply (simp add: mult_ac)
paulson@23449
   424
  apply (rule mult_left_mono, assumption)
paulson@23449
   425
  apply (rule abs_ge_zero)
paulson@23449
   426
done
paulson@23449
   427
blanchet@38991
   428
declare [[ sledgehammer_problem_prefix = "BigO__bigo_mult3" ]]
paulson@23449
   429
lemma bigo_mult3: "f : O(h) ==> g : O(j) ==> f * g : O(h * j)"
blanchet@36561
   430
by (metis bigo_mult set_rev_mp set_times_intro)
paulson@23449
   431
blanchet@38991
   432
declare [[ sledgehammer_problem_prefix = "BigO__bigo_mult4" ]]
paulson@23449
   433
lemma bigo_mult4 [intro]:"f : k +o O(h) ==> g * f : (g * k) +o O(g * h)"
paulson@23449
   434
by (metis bigo_mult2 set_plus_mono_b set_times_intro2 set_times_plus_distrib)
paulson@23449
   435
paulson@23449
   436
paulson@23449
   437
lemma bigo_mult5: "ALL x. f x ~= 0 ==>
haftmann@35028
   438
    O(f * g) <= (f::'a => ('b::linordered_field)) *o O(g)"
paulson@23449
   439
proof -
paulson@23449
   440
  assume "ALL x. f x ~= 0"
paulson@23449
   441
  show "O(f * g) <= f *o O(g)"
paulson@23449
   442
  proof
paulson@23449
   443
    fix h
paulson@23449
   444
    assume "h : O(f * g)"
paulson@23449
   445
    then have "(%x. 1 / (f x)) * h : (%x. 1 / f x) *o O(f * g)"
paulson@23449
   446
      by auto
paulson@23449
   447
    also have "... <= O((%x. 1 / f x) * (f * g))"
paulson@23449
   448
      by (rule bigo_mult2)
paulson@23449
   449
    also have "(%x. 1 / f x) * (f * g) = g"
paulson@23449
   450
      apply (simp add: func_times) 
paulson@23449
   451
      apply (rule ext)
paulson@23449
   452
      apply (simp add: prems nonzero_divide_eq_eq mult_ac)
paulson@23449
   453
      done
paulson@23449
   454
    finally have "(%x. (1::'b) / f x) * h : O(g)".
paulson@23449
   455
    then have "f * ((%x. (1::'b) / f x) * h) : f *o O(g)"
paulson@23449
   456
      by auto
paulson@23449
   457
    also have "f * ((%x. (1::'b) / f x) * h) = h"
paulson@23449
   458
      apply (simp add: func_times) 
paulson@23449
   459
      apply (rule ext)
paulson@23449
   460
      apply (simp add: prems nonzero_divide_eq_eq mult_ac)
paulson@23449
   461
      done
paulson@23449
   462
    finally show "h : f *o O(g)".
paulson@23449
   463
  qed
paulson@23449
   464
qed
paulson@23449
   465
blanchet@38991
   466
declare [[ sledgehammer_problem_prefix = "BigO__bigo_mult6" ]]
paulson@23449
   467
lemma bigo_mult6: "ALL x. f x ~= 0 ==>
haftmann@35028
   468
    O(f * g) = (f::'a => ('b::linordered_field)) *o O(g)"
paulson@23449
   469
by (metis bigo_mult2 bigo_mult5 order_antisym)
paulson@23449
   470
paulson@23449
   471
(*proof requires relaxing relevance: 2007-01-25*)
blanchet@38991
   472
declare [[ sledgehammer_problem_prefix = "BigO__bigo_mult7" ]]
paulson@23449
   473
  declare bigo_mult6 [simp]
paulson@23449
   474
lemma bigo_mult7: "ALL x. f x ~= 0 ==>
haftmann@35028
   475
    O(f * g) <= O(f::'a => ('b::linordered_field)) \<otimes> O(g)"
paulson@23449
   476
(*sledgehammer*)
paulson@23449
   477
  apply (subst bigo_mult6)
paulson@23449
   478
  apply assumption
paulson@23449
   479
  apply (rule set_times_mono3) 
paulson@23449
   480
  apply (rule bigo_refl)
paulson@23449
   481
done
paulson@23449
   482
  declare bigo_mult6 [simp del]
paulson@23449
   483
blanchet@38991
   484
declare [[ sledgehammer_problem_prefix = "BigO__bigo_mult8" ]]
paulson@23449
   485
  declare bigo_mult7[intro!]
paulson@23449
   486
lemma bigo_mult8: "ALL x. f x ~= 0 ==>
haftmann@35028
   487
    O(f * g) = O(f::'a => ('b::linordered_field)) \<otimes> O(g)"
paulson@23449
   488
by (metis bigo_mult bigo_mult7 order_antisym_conv)
paulson@23449
   489
paulson@23449
   490
lemma bigo_minus [intro]: "f : O(g) ==> - f : O(g)"
berghofe@26814
   491
  by (auto simp add: bigo_def fun_Compl_def)
paulson@23449
   492
paulson@23449
   493
lemma bigo_minus2: "f : g +o O(h) ==> -f : -g +o O(h)"
paulson@23449
   494
  apply (rule set_minus_imp_plus)
paulson@23449
   495
  apply (drule set_plus_imp_minus)
paulson@23449
   496
  apply (drule bigo_minus)
paulson@23449
   497
  apply (simp add: diff_minus)
paulson@23449
   498
done
paulson@23449
   499
paulson@23449
   500
lemma bigo_minus3: "O(-f) = O(f)"
berghofe@26814
   501
  by (auto simp add: bigo_def fun_Compl_def abs_minus_cancel)
paulson@23449
   502
paulson@23449
   503
lemma bigo_plus_absorb_lemma1: "f : O(g) ==> f +o O(g) <= O(g)"
paulson@23449
   504
proof -
paulson@23449
   505
  assume a: "f : O(g)"
paulson@23449
   506
  show "f +o O(g) <= O(g)"
paulson@23449
   507
  proof -
paulson@23449
   508
    have "f : O(f)" by auto
berghofe@26814
   509
    then have "f +o O(g) <= O(f) \<oplus> O(g)"
paulson@23449
   510
      by (auto del: subsetI)
berghofe@26814
   511
    also have "... <= O(g) \<oplus> O(g)"
paulson@23449
   512
    proof -
paulson@23449
   513
      from a have "O(f) <= O(g)" by (auto del: subsetI)
paulson@23449
   514
      thus ?thesis by (auto del: subsetI)
paulson@23449
   515
    qed
paulson@23449
   516
    also have "... <= O(g)" by (simp add: bigo_plus_idemp)
paulson@23449
   517
    finally show ?thesis .
paulson@23449
   518
  qed
paulson@23449
   519
qed
paulson@23449
   520
paulson@23449
   521
lemma bigo_plus_absorb_lemma2: "f : O(g) ==> O(g) <= f +o O(g)"
paulson@23449
   522
proof -
paulson@23449
   523
  assume a: "f : O(g)"
paulson@23449
   524
  show "O(g) <= f +o O(g)"
paulson@23449
   525
  proof -
paulson@23449
   526
    from a have "-f : O(g)" by auto
paulson@23449
   527
    then have "-f +o O(g) <= O(g)" by (elim bigo_plus_absorb_lemma1)
paulson@23449
   528
    then have "f +o (-f +o O(g)) <= f +o O(g)" by auto
paulson@23449
   529
    also have "f +o (-f +o O(g)) = O(g)"
paulson@23449
   530
      by (simp add: set_plus_rearranges)
paulson@23449
   531
    finally show ?thesis .
paulson@23449
   532
  qed
paulson@23449
   533
qed
paulson@23449
   534
blanchet@38991
   535
declare [[ sledgehammer_problem_prefix = "BigO__bigo_plus_absorb" ]]
paulson@23449
   536
lemma bigo_plus_absorb [simp]: "f : O(g) ==> f +o O(g) = O(g)"
paulson@23449
   537
by (metis bigo_plus_absorb_lemma1 bigo_plus_absorb_lemma2 order_eq_iff);
paulson@23449
   538
paulson@23449
   539
lemma bigo_plus_absorb2 [intro]: "f : O(g) ==> A <= O(g) ==> f +o A <= O(g)"
paulson@23449
   540
  apply (subgoal_tac "f +o A <= f +o O(g)")
paulson@23449
   541
  apply force+
paulson@23449
   542
done
paulson@23449
   543
paulson@23449
   544
lemma bigo_add_commute_imp: "f : g +o O(h) ==> g : f +o O(h)"
paulson@23449
   545
  apply (subst set_minus_plus [symmetric])
paulson@23449
   546
  apply (subgoal_tac "g - f = - (f - g)")
paulson@23449
   547
  apply (erule ssubst)
paulson@23449
   548
  apply (rule bigo_minus)
paulson@23449
   549
  apply (subst set_minus_plus)
paulson@23449
   550
  apply assumption
paulson@23449
   551
  apply  (simp add: diff_minus add_ac)
paulson@23449
   552
done
paulson@23449
   553
paulson@23449
   554
lemma bigo_add_commute: "(f : g +o O(h)) = (g : f +o O(h))"
paulson@23449
   555
  apply (rule iffI)
paulson@23449
   556
  apply (erule bigo_add_commute_imp)+
paulson@23449
   557
done
paulson@23449
   558
paulson@23449
   559
lemma bigo_const1: "(%x. c) : O(%x. 1)"
paulson@23449
   560
by (auto simp add: bigo_def mult_ac)
paulson@23449
   561
blanchet@38991
   562
declare [[ sledgehammer_problem_prefix = "BigO__bigo_const2" ]]
paulson@23449
   563
lemma (*bigo_const2 [intro]:*) "O(%x. c) <= O(%x. 1)"
paulson@23449
   564
by (metis bigo_const1 bigo_elt_subset);
paulson@23449
   565
haftmann@35028
   566
lemma bigo_const2 [intro]: "O(%x. c::'b::linordered_idom) <= O(%x. 1)";
blanchet@36561
   567
(* "thus" had to be replaced by "show" with an explicit reference to "F1" *)
blanchet@36561
   568
proof -
blanchet@36561
   569
  have F1: "\<forall>u. (\<lambda>Q. u) \<in> O(\<lambda>Q. 1)" by (metis bigo_const1)
blanchet@36561
   570
  show "O(\<lambda>x. c) \<subseteq> O(\<lambda>x. 1)" by (metis F1 bigo_elt_subset)
paulson@23449
   571
qed
paulson@23449
   572
blanchet@38991
   573
declare [[ sledgehammer_problem_prefix = "BigO__bigo_const3" ]]
haftmann@35028
   574
lemma bigo_const3: "(c::'a::linordered_field) ~= 0 ==> (%x. 1) : O(%x. c)"
paulson@23449
   575
apply (simp add: bigo_def)
blanchet@36561
   576
by (metis abs_eq_0 left_inverse order_refl)
paulson@23449
   577
haftmann@35028
   578
lemma bigo_const4: "(c::'a::linordered_field) ~= 0 ==> O(%x. 1) <= O(%x. c)"
paulson@23449
   579
by (rule bigo_elt_subset, rule bigo_const3, assumption)
paulson@23449
   580
haftmann@35028
   581
lemma bigo_const [simp]: "(c::'a::linordered_field) ~= 0 ==> 
paulson@23449
   582
    O(%x. c) = O(%x. 1)"
paulson@23449
   583
by (rule equalityI, rule bigo_const2, rule bigo_const4, assumption)
paulson@23449
   584
blanchet@38991
   585
declare [[ sledgehammer_problem_prefix = "BigO__bigo_const_mult1" ]]
paulson@23449
   586
lemma bigo_const_mult1: "(%x. c * f x) : O(f)"
paulson@24937
   587
  apply (simp add: bigo_def abs_mult)
blanchet@36561
   588
by (metis le_less)
paulson@23449
   589
paulson@23449
   590
lemma bigo_const_mult2: "O(%x. c * f x) <= O(f)"
paulson@23449
   591
by (rule bigo_elt_subset, rule bigo_const_mult1)
paulson@23449
   592
blanchet@38991
   593
declare [[ sledgehammer_problem_prefix = "BigO__bigo_const_mult3" ]]
haftmann@35028
   594
lemma bigo_const_mult3: "(c::'a::linordered_field) ~= 0 ==> f : O(%x. c * f x)"
paulson@23449
   595
  apply (simp add: bigo_def)
blanchet@36561
   596
(*sledgehammer [no luck]*)
paulson@23449
   597
  apply (rule_tac x = "abs(inverse c)" in exI)
paulson@23449
   598
  apply (simp only: abs_mult [symmetric] mult_assoc [symmetric])
paulson@23449
   599
apply (subst left_inverse) 
paulson@23449
   600
apply (auto ); 
paulson@23449
   601
done
paulson@23449
   602
haftmann@35028
   603
lemma bigo_const_mult4: "(c::'a::linordered_field) ~= 0 ==> 
paulson@23449
   604
    O(f) <= O(%x. c * f x)"
paulson@23449
   605
by (rule bigo_elt_subset, rule bigo_const_mult3, assumption)
paulson@23449
   606
haftmann@35028
   607
lemma bigo_const_mult [simp]: "(c::'a::linordered_field) ~= 0 ==> 
paulson@23449
   608
    O(%x. c * f x) = O(f)"
paulson@23449
   609
by (rule equalityI, rule bigo_const_mult2, erule bigo_const_mult4)
paulson@23449
   610
blanchet@38991
   611
declare [[ sledgehammer_problem_prefix = "BigO__bigo_const_mult5" ]]
haftmann@35028
   612
lemma bigo_const_mult5 [simp]: "(c::'a::linordered_field) ~= 0 ==> 
paulson@23449
   613
    (%x. c) *o O(f) = O(f)"
paulson@23449
   614
  apply (auto del: subsetI)
paulson@23449
   615
  apply (rule order_trans)
paulson@23449
   616
  apply (rule bigo_mult2)
paulson@23449
   617
  apply (simp add: func_times)
paulson@23449
   618
  apply (auto intro!: subsetI simp add: bigo_def elt_set_times_def func_times)
paulson@23449
   619
  apply (rule_tac x = "%y. inverse c * x y" in exI)
paulson@24942
   620
  apply (rename_tac g d) 
paulson@24942
   621
  apply safe
paulson@24942
   622
  apply (rule_tac [2] ext) 
paulson@24942
   623
   prefer 2 
haftmann@26041
   624
   apply simp
paulson@24942
   625
  apply (simp add: mult_assoc [symmetric] abs_mult)
blanchet@39259
   626
  (* couldn't get this proof without the step above *)
blanchet@39259
   627
proof -
blanchet@39259
   628
  fix g :: "'b \<Rightarrow> 'a" and d :: 'a
blanchet@39259
   629
  assume A1: "c \<noteq> (0\<Colon>'a)"
blanchet@39259
   630
  assume A2: "\<forall>x\<Colon>'b. \<bar>g x\<bar> \<le> d * \<bar>f x\<bar>"
blanchet@39259
   631
  have F1: "inverse \<bar>c\<bar> = \<bar>inverse c\<bar>" using A1 by (metis nonzero_abs_inverse)
blanchet@39259
   632
  have F2: "(0\<Colon>'a) < \<bar>c\<bar>" using A1 by (metis zero_less_abs_iff)
blanchet@39259
   633
  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
   634
  hence "(0\<Colon>'a) < \<bar>inverse c\<bar>" using F2 by metis
blanchet@39259
   635
  hence F3: "(0\<Colon>'a) \<le> \<bar>inverse c\<bar>" by (metis order_le_less)
blanchet@39259
   636
  have "\<exists>(u\<Colon>'a) SKF\<^isub>7\<Colon>'a \<Rightarrow> 'b. \<bar>g (SKF\<^isub>7 (\<bar>inverse c\<bar> * u))\<bar> \<le> u * \<bar>f (SKF\<^isub>7 (\<bar>inverse c\<bar> * u))\<bar>"
blanchet@39259
   637
    using A2 by metis
blanchet@39259
   638
  hence F4: "\<exists>(u\<Colon>'a) SKF\<^isub>7\<Colon>'a \<Rightarrow> 'b. \<bar>g (SKF\<^isub>7 (\<bar>inverse c\<bar> * u))\<bar> \<le> u * \<bar>f (SKF\<^isub>7 (\<bar>inverse c\<bar> * u))\<bar> \<and> (0\<Colon>'a) \<le> \<bar>inverse c\<bar>"
blanchet@39259
   639
    using F3 by metis
blanchet@39259
   640
  hence "\<exists>(v\<Colon>'a) (u\<Colon>'a) SKF\<^isub>7\<Colon>'a \<Rightarrow> 'b. \<bar>inverse c\<bar> * \<bar>g (SKF\<^isub>7 (u * v))\<bar> \<le> u * (v * \<bar>f (SKF\<^isub>7 (u * v))\<bar>)"
blanchet@39259
   641
    by (metis comm_mult_left_mono)
blanchet@39259
   642
  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
   643
    using A2 F4 by (metis ab_semigroup_mult_class.mult_ac(1) comm_mult_left_mono)
blanchet@39259
   644
qed
paulson@23449
   645
paulson@23449
   646
blanchet@38991
   647
declare [[ sledgehammer_problem_prefix = "BigO__bigo_const_mult6" ]]
paulson@23449
   648
lemma bigo_const_mult6 [intro]: "(%x. c) *o O(f) <= O(f)"
paulson@23449
   649
  apply (auto intro!: subsetI
paulson@23449
   650
    simp add: bigo_def elt_set_times_def func_times
paulson@23449
   651
    simp del: abs_mult mult_ac)
paulson@23449
   652
(*sledgehammer*); 
paulson@23449
   653
  apply (rule_tac x = "ca * (abs c)" in exI)
paulson@23449
   654
  apply (rule allI)
paulson@23449
   655
  apply (subgoal_tac "ca * abs(c) * abs(f x) = abs(c) * (ca * abs(f x))")
paulson@23449
   656
  apply (erule ssubst)
paulson@23449
   657
  apply (subst abs_mult)
paulson@23449
   658
  apply (rule mult_left_mono)
paulson@23449
   659
  apply (erule spec)
paulson@23449
   660
  apply simp
paulson@23449
   661
  apply(simp add: mult_ac)
paulson@23449
   662
done
paulson@23449
   663
paulson@23449
   664
lemma bigo_const_mult7 [intro]: "f =o O(g) ==> (%x. c * f x) =o O(g)"
paulson@23449
   665
proof -
paulson@23449
   666
  assume "f =o O(g)"
paulson@23449
   667
  then have "(%x. c) * f =o (%x. c) *o O(g)"
paulson@23449
   668
    by auto
paulson@23449
   669
  also have "(%x. c) * f = (%x. c * f x)"
paulson@23449
   670
    by (simp add: func_times)
paulson@23449
   671
  also have "(%x. c) *o O(g) <= O(g)"
paulson@23449
   672
    by (auto del: subsetI)
paulson@23449
   673
  finally show ?thesis .
paulson@23449
   674
qed
paulson@23449
   675
paulson@23449
   676
lemma bigo_compose1: "f =o O(g) ==> (%x. f(k x)) =o O(%x. g(k x))"
paulson@23449
   677
by (unfold bigo_def, auto)
paulson@23449
   678
paulson@23449
   679
lemma bigo_compose2: "f =o g +o O(h) ==> (%x. f(k x)) =o (%x. g(k x)) +o 
paulson@23449
   680
    O(%x. h(k x))"
berghofe@26814
   681
  apply (simp only: set_minus_plus [symmetric] diff_minus fun_Compl_def
paulson@23449
   682
      func_plus)
paulson@23449
   683
  apply (erule bigo_compose1)
paulson@23449
   684
done
paulson@23449
   685
paulson@23449
   686
subsection {* Setsum *}
paulson@23449
   687
paulson@23449
   688
lemma bigo_setsum_main: "ALL x. ALL y : A x. 0 <= h x y ==> 
paulson@23449
   689
    EX c. ALL x. ALL y : A x. abs(f x y) <= c * (h x y) ==>
paulson@23449
   690
      (%x. SUM y : A x. f x y) =o O(%x. SUM y : A x. h x y)"  
paulson@23449
   691
  apply (auto simp add: bigo_def)
paulson@23449
   692
  apply (rule_tac x = "abs c" in exI)
paulson@23449
   693
  apply (subst abs_of_nonneg) back back
paulson@23449
   694
  apply (rule setsum_nonneg)
paulson@23449
   695
  apply force
paulson@23449
   696
  apply (subst setsum_right_distrib)
paulson@23449
   697
  apply (rule allI)
paulson@23449
   698
  apply (rule order_trans)
paulson@23449
   699
  apply (rule setsum_abs)
paulson@23449
   700
  apply (rule setsum_mono)
paulson@23449
   701
apply (blast intro: order_trans mult_right_mono abs_ge_self) 
paulson@23449
   702
done
paulson@23449
   703
blanchet@38991
   704
declare [[ sledgehammer_problem_prefix = "BigO__bigo_setsum1" ]]
paulson@23449
   705
lemma bigo_setsum1: "ALL x y. 0 <= h x y ==> 
paulson@23449
   706
    EX c. ALL x y. abs(f x y) <= c * (h x y) ==>
paulson@23449
   707
      (%x. SUM y : A x. f x y) =o O(%x. SUM y : A x. h x y)"
paulson@23449
   708
  apply (rule bigo_setsum_main)
paulson@23449
   709
(*sledgehammer*); 
paulson@23449
   710
  apply force
paulson@23449
   711
  apply clarsimp
paulson@23449
   712
  apply (rule_tac x = c in exI)
paulson@23449
   713
  apply force
paulson@23449
   714
done
paulson@23449
   715
paulson@23449
   716
lemma bigo_setsum2: "ALL y. 0 <= h y ==> 
paulson@23449
   717
    EX c. ALL y. abs(f y) <= c * (h y) ==>
paulson@23449
   718
      (%x. SUM y : A x. f y) =o O(%x. SUM y : A x. h y)"
paulson@23449
   719
by (rule bigo_setsum1, auto)  
paulson@23449
   720
blanchet@38991
   721
declare [[ sledgehammer_problem_prefix = "BigO__bigo_setsum3" ]]
paulson@23449
   722
lemma bigo_setsum3: "f =o O(h) ==>
paulson@23449
   723
    (%x. SUM y : A x. (l x y) * f(k x y)) =o
paulson@23449
   724
      O(%x. SUM y : A x. abs(l x y * h(k x y)))"
paulson@23449
   725
  apply (rule bigo_setsum1)
paulson@23449
   726
  apply (rule allI)+
paulson@23449
   727
  apply (rule abs_ge_zero)
paulson@23449
   728
  apply (unfold bigo_def)
paulson@23449
   729
  apply (auto simp add: abs_mult);
paulson@23449
   730
(*sledgehammer*); 
paulson@23449
   731
  apply (rule_tac x = c in exI)
paulson@23449
   732
  apply (rule allI)+
paulson@23449
   733
  apply (subst mult_left_commute)
paulson@23449
   734
  apply (rule mult_left_mono)
paulson@23449
   735
  apply (erule spec)
paulson@23449
   736
  apply (rule abs_ge_zero)
paulson@23449
   737
done
paulson@23449
   738
paulson@23449
   739
lemma bigo_setsum4: "f =o g +o O(h) ==>
paulson@23449
   740
    (%x. SUM y : A x. l x y * f(k x y)) =o
paulson@23449
   741
      (%x. SUM y : A x. l x y * g(k x y)) +o
paulson@23449
   742
        O(%x. SUM y : A x. abs(l x y * h(k x y)))"
paulson@23449
   743
  apply (rule set_minus_imp_plus)
berghofe@26814
   744
  apply (subst fun_diff_def)
paulson@23449
   745
  apply (subst setsum_subtractf [symmetric])
paulson@23449
   746
  apply (subst right_diff_distrib [symmetric])
paulson@23449
   747
  apply (rule bigo_setsum3)
berghofe@26814
   748
  apply (subst fun_diff_def [symmetric])
paulson@23449
   749
  apply (erule set_plus_imp_minus)
paulson@23449
   750
done
paulson@23449
   751
blanchet@38991
   752
declare [[ sledgehammer_problem_prefix = "BigO__bigo_setsum5" ]]
paulson@23449
   753
lemma bigo_setsum5: "f =o O(h) ==> ALL x y. 0 <= l x y ==> 
paulson@23449
   754
    ALL x. 0 <= h x ==>
paulson@23449
   755
      (%x. SUM y : A x. (l x y) * f(k x y)) =o
paulson@23449
   756
        O(%x. SUM y : A x. (l x y) * h(k x y))" 
paulson@23449
   757
  apply (subgoal_tac "(%x. SUM y : A x. (l x y) * h(k x y)) = 
paulson@23449
   758
      (%x. SUM y : A x. abs((l x y) * h(k x y)))")
paulson@23449
   759
  apply (erule ssubst)
paulson@23449
   760
  apply (erule bigo_setsum3)
paulson@23449
   761
  apply (rule ext)
paulson@23449
   762
  apply (rule setsum_cong2)
paulson@23449
   763
  apply (thin_tac "f \<in> O(h)") 
paulson@24942
   764
apply (metis abs_of_nonneg zero_le_mult_iff)
paulson@23449
   765
done
paulson@23449
   766
paulson@23449
   767
lemma bigo_setsum6: "f =o g +o O(h) ==> ALL x y. 0 <= l x y ==>
paulson@23449
   768
    ALL x. 0 <= h x ==>
paulson@23449
   769
      (%x. SUM y : A x. (l x y) * f(k x y)) =o
paulson@23449
   770
        (%x. SUM y : A x. (l x y) * g(k x y)) +o
paulson@23449
   771
          O(%x. SUM y : A x. (l x y) * h(k x y))" 
paulson@23449
   772
  apply (rule set_minus_imp_plus)
berghofe@26814
   773
  apply (subst fun_diff_def)
paulson@23449
   774
  apply (subst setsum_subtractf [symmetric])
paulson@23449
   775
  apply (subst right_diff_distrib [symmetric])
paulson@23449
   776
  apply (rule bigo_setsum5)
berghofe@26814
   777
  apply (subst fun_diff_def [symmetric])
paulson@23449
   778
  apply (drule set_plus_imp_minus)
paulson@23449
   779
  apply auto
paulson@23449
   780
done
paulson@23449
   781
paulson@23449
   782
subsection {* Misc useful stuff *}
paulson@23449
   783
paulson@23449
   784
lemma bigo_useful_intro: "A <= O(f) ==> B <= O(f) ==>
berghofe@26814
   785
  A \<oplus> B <= O(f)"
paulson@23449
   786
  apply (subst bigo_plus_idemp [symmetric])
paulson@23449
   787
  apply (rule set_plus_mono2)
paulson@23449
   788
  apply assumption+
paulson@23449
   789
done
paulson@23449
   790
paulson@23449
   791
lemma bigo_useful_add: "f =o O(h) ==> g =o O(h) ==> f + g =o O(h)"
paulson@23449
   792
  apply (subst bigo_plus_idemp [symmetric])
paulson@23449
   793
  apply (rule set_plus_intro)
paulson@23449
   794
  apply assumption+
paulson@23449
   795
done
paulson@23449
   796
  
haftmann@35028
   797
lemma bigo_useful_const_mult: "(c::'a::linordered_field) ~= 0 ==> 
paulson@23449
   798
    (%x. c) * f =o O(h) ==> f =o O(h)"
paulson@23449
   799
  apply (rule subsetD)
paulson@23449
   800
  apply (subgoal_tac "(%x. 1 / c) *o O(h) <= O(h)")
paulson@23449
   801
  apply assumption
paulson@23449
   802
  apply (rule bigo_const_mult6)
paulson@23449
   803
  apply (subgoal_tac "f = (%x. 1 / c) * ((%x. c) * f)")
paulson@23449
   804
  apply (erule ssubst)
paulson@23449
   805
  apply (erule set_times_intro2)
paulson@23449
   806
  apply (simp add: func_times) 
paulson@23449
   807
done
paulson@23449
   808
blanchet@38991
   809
declare [[ sledgehammer_problem_prefix = "BigO__bigo_fix" ]]
paulson@23449
   810
lemma bigo_fix: "(%x. f ((x::nat) + 1)) =o O(%x. h(x + 1)) ==> f 0 = 0 ==>
paulson@23449
   811
    f =o O(h)"
paulson@23449
   812
  apply (simp add: bigo_alt_def)
paulson@23449
   813
(*sledgehammer*); 
paulson@23449
   814
  apply clarify
paulson@23449
   815
  apply (rule_tac x = c in exI)
paulson@23449
   816
  apply safe
paulson@23449
   817
  apply (case_tac "x = 0")
haftmann@35050
   818
apply (metis abs_ge_zero  abs_zero  order_less_le  split_mult_pos_le) 
paulson@23449
   819
  apply (subgoal_tac "x = Suc (x - 1)")
paulson@23816
   820
  apply metis
paulson@23449
   821
  apply simp
paulson@23449
   822
  done
paulson@23449
   823
paulson@23449
   824
paulson@23449
   825
lemma bigo_fix2: 
paulson@23449
   826
    "(%x. f ((x::nat) + 1)) =o (%x. g(x + 1)) +o O(%x. h(x + 1)) ==> 
paulson@23449
   827
       f 0 = g 0 ==> f =o g +o O(h)"
paulson@23449
   828
  apply (rule set_minus_imp_plus)
paulson@23449
   829
  apply (rule bigo_fix)
berghofe@26814
   830
  apply (subst fun_diff_def)
berghofe@26814
   831
  apply (subst fun_diff_def [symmetric])
paulson@23449
   832
  apply (rule set_plus_imp_minus)
paulson@23449
   833
  apply simp
berghofe@26814
   834
  apply (simp add: fun_diff_def)
paulson@23449
   835
done
paulson@23449
   836
paulson@23449
   837
subsection {* Less than or equal to *}
paulson@23449
   838
haftmann@35416
   839
definition lesso :: "('a => 'b::linordered_idom) => ('a => 'b) => ('a => 'b)" (infixl "<o" 70) where
paulson@23449
   840
  "f <o g == (%x. max (f x - g x) 0)"
paulson@23449
   841
paulson@23449
   842
lemma bigo_lesseq1: "f =o O(h) ==> ALL x. abs (g x) <= abs (f x) ==>
paulson@23449
   843
    g =o O(h)"
paulson@23449
   844
  apply (unfold bigo_def)
paulson@23449
   845
  apply clarsimp
paulson@23449
   846
apply (blast intro: order_trans) 
paulson@23449
   847
done
paulson@23449
   848
paulson@23449
   849
lemma bigo_lesseq2: "f =o O(h) ==> ALL x. abs (g x) <= f x ==>
paulson@23449
   850
      g =o O(h)"
paulson@23449
   851
  apply (erule bigo_lesseq1)
paulson@23449
   852
apply (blast intro: abs_ge_self order_trans) 
paulson@23449
   853
done
paulson@23449
   854
paulson@23449
   855
lemma bigo_lesseq3: "f =o O(h) ==> ALL x. 0 <= g x ==> ALL x. g x <= f x ==>
paulson@23449
   856
      g =o O(h)"
paulson@23449
   857
  apply (erule bigo_lesseq2)
paulson@23449
   858
  apply (rule allI)
paulson@23449
   859
  apply (subst abs_of_nonneg)
paulson@23449
   860
  apply (erule spec)+
paulson@23449
   861
done
paulson@23449
   862
paulson@23449
   863
lemma bigo_lesseq4: "f =o O(h) ==>
paulson@23449
   864
    ALL x. 0 <= g x ==> ALL x. g x <= abs (f x) ==>
paulson@23449
   865
      g =o O(h)"
paulson@23449
   866
  apply (erule bigo_lesseq1)
paulson@23449
   867
  apply (rule allI)
paulson@23449
   868
  apply (subst abs_of_nonneg)
paulson@23449
   869
  apply (erule spec)+
paulson@23449
   870
done
paulson@23449
   871
blanchet@38991
   872
declare [[ sledgehammer_problem_prefix = "BigO__bigo_lesso1" ]]
paulson@23449
   873
lemma bigo_lesso1: "ALL x. f x <= g x ==> f <o g =o O(h)"
blanchet@36561
   874
apply (unfold lesso_def)
blanchet@36561
   875
apply (subgoal_tac "(%x. max (f x - g x) 0) = 0")
blanchet@36561
   876
proof -
blanchet@36561
   877
  assume "(\<lambda>x. max (f x - g x) 0) = 0"
blanchet@36561
   878
  thus "(\<lambda>x. max (f x - g x) 0) \<in> O(h)" by (metis bigo_zero)
blanchet@36561
   879
next
blanchet@36561
   880
  show "\<forall>x\<Colon>'a. f x \<le> g x \<Longrightarrow> (\<lambda>x\<Colon>'a. max (f x - g x) (0\<Colon>'b)) = (0\<Colon>'a \<Rightarrow> 'b)"
paulson@23449
   881
  apply (unfold func_zero)
paulson@23449
   882
  apply (rule ext)
blanchet@36561
   883
  by (simp split: split_max)
blanchet@36561
   884
qed
paulson@23449
   885
blanchet@38991
   886
declare [[ sledgehammer_problem_prefix = "BigO__bigo_lesso2" ]]
paulson@23449
   887
lemma bigo_lesso2: "f =o g +o O(h) ==>
paulson@23449
   888
    ALL x. 0 <= k x ==> ALL x. k x <= f x ==>
paulson@23449
   889
      k <o g =o O(h)"
paulson@23449
   890
  apply (unfold lesso_def)
paulson@23449
   891
  apply (rule bigo_lesseq4)
paulson@23449
   892
  apply (erule set_plus_imp_minus)
paulson@23449
   893
  apply (rule allI)
paulson@23449
   894
  apply (rule le_maxI2)
paulson@23449
   895
  apply (rule allI)
berghofe@26814
   896
  apply (subst fun_diff_def)
paulson@23449
   897
apply (erule thin_rl)
paulson@23449
   898
(*sledgehammer*);  
paulson@23449
   899
  apply (case_tac "0 <= k x - g x")
blanchet@36561
   900
(* apply (metis abs_le_iff add_le_imp_le_right diff_minus le_less
blanchet@36561
   901
                le_max_iff_disj min_max.le_supE min_max.sup_absorb2
blanchet@36561
   902
                min_max.sup_commute) *)
blanchet@37320
   903
proof -
blanchet@37320
   904
  fix x :: 'a
blanchet@37320
   905
  assume "\<forall>x\<Colon>'a. k x \<le> f x"
blanchet@37320
   906
  hence F1: "\<forall>x\<^isub>1\<Colon>'a. max (k x\<^isub>1) (f x\<^isub>1) = f x\<^isub>1" by (metis min_max.sup_absorb2)
blanchet@37320
   907
  assume "(0\<Colon>'b) \<le> k x - g x"
blanchet@37320
   908
  hence F2: "max (0\<Colon>'b) (k x - g x) = k x - g x" by (metis min_max.sup_absorb2)
blanchet@37320
   909
  have F3: "\<forall>x\<^isub>1\<Colon>'b. x\<^isub>1 \<le> \<bar>x\<^isub>1\<bar>" by (metis abs_le_iff le_less)
blanchet@37320
   910
  have "\<forall>(x\<^isub>2\<Colon>'b) x\<^isub>1\<Colon>'b. max x\<^isub>1 x\<^isub>2 \<le> x\<^isub>2 \<or> max x\<^isub>1 x\<^isub>2 \<le> x\<^isub>1" by (metis le_less le_max_iff_disj)
blanchet@37320
   911
  hence "\<forall>(x\<^isub>3\<Colon>'b) (x\<^isub>2\<Colon>'b) x\<^isub>1\<Colon>'b. x\<^isub>1 - x\<^isub>2 \<le> x\<^isub>3 - x\<^isub>2 \<or> x\<^isub>3 \<le> x\<^isub>1" by (metis add_le_imp_le_right diff_minus min_max.le_supE)
blanchet@37320
   912
  hence "k x - g x \<le> f x - g x" by (metis F1 le_less min_max.sup_absorb2 min_max.sup_commute)
blanchet@37320
   913
  hence "k x - g x \<le> \<bar>f x - g x\<bar>" by (metis F3 le_max_iff_disj min_max.sup_absorb2)
blanchet@37320
   914
  thus "max (k x - g x) (0\<Colon>'b) \<le> \<bar>f x - g x\<bar>" by (metis F2 min_max.sup_commute)
blanchet@36561
   915
next
blanchet@36561
   916
  show "\<And>x\<Colon>'a.
blanchet@36561
   917
       \<lbrakk>\<forall>x\<Colon>'a. (0\<Colon>'b) \<le> k x; \<forall>x\<Colon>'a. k x \<le> f x; \<not> (0\<Colon>'b) \<le> k x - g x\<rbrakk>
blanchet@36561
   918
       \<Longrightarrow> max (k x - g x) (0\<Colon>'b) \<le> \<bar>f x - g x\<bar>"
blanchet@36561
   919
    by (metis abs_ge_zero le_cases min_max.sup_absorb2)
paulson@24545
   920
qed
paulson@23449
   921
blanchet@38991
   922
declare [[ sledgehammer_problem_prefix = "BigO__bigo_lesso3" ]]
paulson@23449
   923
lemma bigo_lesso3: "f =o g +o O(h) ==>
paulson@23449
   924
    ALL x. 0 <= k x ==> ALL x. g x <= k x ==>
paulson@23449
   925
      f <o k =o O(h)"
paulson@23449
   926
  apply (unfold lesso_def)
paulson@23449
   927
  apply (rule bigo_lesseq4)
paulson@23449
   928
  apply (erule set_plus_imp_minus)
paulson@23449
   929
  apply (rule allI)
paulson@23449
   930
  apply (rule le_maxI2)
paulson@23449
   931
  apply (rule allI)
berghofe@26814
   932
  apply (subst fun_diff_def)
paulson@23449
   933
apply (erule thin_rl) 
paulson@23449
   934
(*sledgehammer*); 
paulson@23449
   935
  apply (case_tac "0 <= f x - k x")
nipkow@29667
   936
  apply (simp)
paulson@23449
   937
  apply (subst abs_of_nonneg)
paulson@23449
   938
  apply (drule_tac x = x in spec) back
blanchet@38991
   939
using [[ sledgehammer_problem_prefix = "BigO__bigo_lesso3_simpler" ]]
paulson@24545
   940
apply (metis diff_less_0_iff_less linorder_not_le not_leE uminus_add_conv_diff xt1(12) xt1(6))
paulson@24545
   941
apply (metis add_minus_cancel diff_le_eq le_diff_eq uminus_add_conv_diff)
haftmann@29511
   942
apply (metis abs_ge_zero linorder_linear min_max.sup_absorb1 min_max.sup_commute)
paulson@23449
   943
done
paulson@23449
   944
haftmann@35028
   945
lemma bigo_lesso4: "f <o g =o O(k::'a=>'b::linordered_field) ==>
paulson@23449
   946
    g =o h +o O(k) ==> f <o h =o O(k)"
paulson@23449
   947
  apply (unfold lesso_def)
paulson@23449
   948
  apply (drule set_plus_imp_minus)
paulson@23449
   949
  apply (drule bigo_abs5) back
berghofe@26814
   950
  apply (simp add: fun_diff_def)
paulson@23449
   951
  apply (drule bigo_useful_add)
paulson@23449
   952
  apply assumption
paulson@23449
   953
  apply (erule bigo_lesseq2) back
paulson@23449
   954
  apply (rule allI)
nipkow@29667
   955
  apply (auto simp add: func_plus fun_diff_def algebra_simps
paulson@23449
   956
    split: split_max abs_split)
paulson@23449
   957
done
paulson@23449
   958
blanchet@38991
   959
declare [[ sledgehammer_problem_prefix = "BigO__bigo_lesso5" ]]
paulson@23449
   960
lemma bigo_lesso5: "f <o g =o O(h) ==>
paulson@23449
   961
    EX C. ALL x. f x <= g x + C * abs(h x)"
paulson@23449
   962
  apply (simp only: lesso_def bigo_alt_def)
paulson@23449
   963
  apply clarsimp
paulson@24855
   964
  apply (metis abs_if abs_mult add_commute diff_le_eq less_not_permute)  
paulson@23449
   965
done
paulson@23449
   966
paulson@23449
   967
end