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