src/HOL/MetisExamples/BigO.thy
author paulson
Wed Dec 19 17:40:48 2007 +0100 (2007-12-19)
changeset 25710 4cdf7de81e1b
parent 25592 e8ddaf6bf5df
child 26041 c2e15e65165f
permissions -rw-r--r--
Replaced refs by config params; finer critical section in mets method
paulson@23449
     1
(*  Title:      HOL/MetisExamples/BigO.thy
paulson@23449
     2
    ID:         $Id$
paulson@23449
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@23449
     4
paulson@23449
     5
Testing the metis method
paulson@23449
     6
*)
paulson@23449
     7
paulson@23449
     8
header {* Big O notation *}
paulson@23449
     9
paulson@23449
    10
theory BigO
haftmann@25592
    11
imports Main SetsAndFunctions 
paulson@23449
    12
begin
paulson@23449
    13
paulson@23449
    14
subsection {* Definitions *}
paulson@23449
    15
paulson@23449
    16
constdefs 
paulson@23449
    17
paulson@23449
    18
  bigo :: "('a => 'b::ordered_idom) => ('a => 'b) set"    ("(1O'(_'))")
paulson@23449
    19
  "O(f::('a => 'b)) ==   {h. EX c. ALL x. abs (h x) <= c * abs (f x)}"
paulson@23449
    20
paulson@23449
    21
ML{*ResAtp.problem_name := "BigO__bigo_pos_const"*}
paulson@23449
    22
lemma bigo_pos_const: "(EX (c::'a::ordered_idom). 
paulson@23449
    23
    ALL x. (abs (h x)) <= (c * (abs (f x))))
paulson@23449
    24
      = (EX c. 0 < c & (ALL x. (abs(h x)) <= (c * (abs (f x)))))"
paulson@23449
    25
  apply auto
paulson@23449
    26
  apply (case_tac "c = 0", simp)
paulson@23449
    27
  apply (rule_tac x = "1" in exI, simp)
haftmann@25304
    28
  apply (rule_tac x = "abs c" in exI, auto)
haftmann@25304
    29
  apply (metis abs_ge_minus_self abs_ge_zero abs_minus_cancel abs_of_nonneg equation_minus_iff Orderings.xt1(6) abs_mult)
paulson@23449
    30
  done
paulson@23449
    31
paulson@23449
    32
(*** Now various verions with an increasing modulus ***)
paulson@23449
    33
paulson@25710
    34
declare [[reconstruction_modulus = 1]]
paulson@23449
    35
paulson@23449
    36
lemma bigo_pos_const: "(EX (c::'a::ordered_idom). 
paulson@23449
    37
    ALL x. (abs (h x)) <= (c * (abs (f x))))
paulson@23449
    38
      = (EX c. 0 < c & (ALL x. (abs(h x)) <= (c * (abs (f x)))))"
paulson@23449
    39
  apply auto
paulson@23449
    40
  apply (case_tac "c = 0", simp)
paulson@23449
    41
  apply (rule_tac x = "1" in exI, simp)
paulson@23449
    42
  apply (rule_tac x = "abs c" in exI, auto)
paulson@23449
    43
proof (neg_clausify)
paulson@23449
    44
fix c x
paulson@24937
    45
have 0: "\<And>(X1\<Colon>'a\<Colon>ordered_idom) X2\<Colon>'a\<Colon>ordered_idom. \<bar>X1 * X2\<bar> = \<bar>X2 * X1\<bar>"
paulson@24937
    46
  by (metis abs_mult mult_commute)
paulson@24937
    47
have 1: "\<And>(X1\<Colon>'a\<Colon>ordered_idom) X2\<Colon>'a\<Colon>ordered_idom.
paulson@24937
    48
   X1 \<le> (0\<Colon>'a\<Colon>ordered_idom) \<or> \<bar>X2\<bar> * X1 = \<bar>X2 * X1\<bar>"
paulson@24937
    49
  by (metis abs_mult_pos linorder_linear)
paulson@24937
    50
have 2: "\<And>(X1\<Colon>'a\<Colon>ordered_idom) X2\<Colon>'a\<Colon>ordered_idom.
paulson@24937
    51
   \<not> (0\<Colon>'a\<Colon>ordered_idom) < X1 * X2 \<or>
paulson@24937
    52
   \<not> (0\<Colon>'a\<Colon>ordered_idom) \<le> X2 \<or> \<not> X1 \<le> (0\<Colon>'a\<Colon>ordered_idom)"
paulson@24937
    53
  by (metis linorder_not_less mult_nonneg_nonpos2)
paulson@24937
    54
assume 3: "\<And>x\<Colon>'b\<Colon>type.
paulson@24937
    55
   \<bar>(h\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) x\<bar>
paulson@24937
    56
   \<le> (c\<Colon>'a\<Colon>ordered_idom) * \<bar>(f\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) x\<bar>"
paulson@24937
    57
assume 4: "\<not> \<bar>(h\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) (x\<Colon>'b\<Colon>type)\<bar>
paulson@24937
    58
  \<le> \<bar>c\<Colon>'a\<Colon>ordered_idom\<bar> * \<bar>(f\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) x\<bar>"
paulson@24937
    59
have 5: "\<not> \<bar>(h\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) (x\<Colon>'b\<Colon>type)\<bar>
paulson@24937
    60
  \<le> \<bar>(c\<Colon>'a\<Colon>ordered_idom) * (f\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) x\<bar>"
paulson@24937
    61
  by (metis 4 abs_mult)
paulson@24937
    62
have 6: "\<And>(X1\<Colon>'a\<Colon>ordered_idom) X2\<Colon>'a\<Colon>ordered_idom.
paulson@24937
    63
   \<not> X1 \<le> (0\<Colon>'a\<Colon>ordered_idom) \<or> X1 \<le> \<bar>X2\<bar>"
paulson@24937
    64
  by (metis abs_ge_zero xt1(6))
paulson@24937
    65
have 7: "\<And>(X1\<Colon>'a\<Colon>ordered_idom) X2\<Colon>'a\<Colon>ordered_idom.
paulson@24937
    66
   X1 \<le> \<bar>X2\<bar> \<or> (0\<Colon>'a\<Colon>ordered_idom) < X1"
paulson@24937
    67
  by (metis not_leE 6)
paulson@24937
    68
have 8: "(0\<Colon>'a\<Colon>ordered_idom) < \<bar>(h\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) (x\<Colon>'b\<Colon>type)\<bar>"
paulson@24937
    69
  by (metis 5 7)
paulson@24937
    70
have 9: "\<And>X1\<Colon>'a\<Colon>ordered_idom.
paulson@24937
    71
   \<not> \<bar>(h\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) (x\<Colon>'b\<Colon>type)\<bar> \<le> X1 \<or>
paulson@24937
    72
   (0\<Colon>'a\<Colon>ordered_idom) < X1"
paulson@24937
    73
  by (metis 8 order_less_le_trans)
paulson@24937
    74
have 10: "(0\<Colon>'a\<Colon>ordered_idom)
paulson@24937
    75
< (c\<Colon>'a\<Colon>ordered_idom) * \<bar>(f\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) (x\<Colon>'b\<Colon>type)\<bar>"
paulson@24937
    76
  by (metis 3 9)
paulson@24937
    77
have 11: "\<not> (c\<Colon>'a\<Colon>ordered_idom) \<le> (0\<Colon>'a\<Colon>ordered_idom)"
paulson@24937
    78
  by (metis abs_ge_zero 2 10)
paulson@24937
    79
have 12: "\<And>X1\<Colon>'a\<Colon>ordered_idom. (c\<Colon>'a\<Colon>ordered_idom) * \<bar>X1\<bar> = \<bar>X1 * c\<bar>"
paulson@24937
    80
  by (metis mult_commute 1 11)
paulson@24937
    81
have 13: "\<And>X1\<Colon>'b\<Colon>type.
paulson@24937
    82
   - (h\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) X1
paulson@24937
    83
   \<le> (c\<Colon>'a\<Colon>ordered_idom) * \<bar>(f\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) X1\<bar>"
paulson@24937
    84
  by (metis 3 abs_le_D2)
paulson@24937
    85
have 14: "\<And>X1\<Colon>'b\<Colon>type.
paulson@24937
    86
   - (h\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) X1
paulson@24937
    87
   \<le> \<bar>(c\<Colon>'a\<Colon>ordered_idom) * (f\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) X1\<bar>"
paulson@24937
    88
  by (metis 0 12 13)
paulson@24937
    89
have 15: "\<And>(X1\<Colon>'a\<Colon>ordered_idom) X2\<Colon>'a\<Colon>ordered_idom. \<bar>X1 * \<bar>X2\<bar>\<bar> = \<bar>X1 * X2\<bar>"
paulson@24937
    90
  by (metis abs_mult abs_mult_pos abs_ge_zero)
paulson@24937
    91
have 16: "\<And>(X1\<Colon>'a\<Colon>ordered_idom) X2\<Colon>'a\<Colon>ordered_idom. X1 \<le> \<bar>X2\<bar> \<or> \<not> X1 \<le> X2"
paulson@24937
    92
  by (metis xt1(6) abs_ge_self)
paulson@24937
    93
have 17: "\<And>(X1\<Colon>'a\<Colon>ordered_idom) X2\<Colon>'a\<Colon>ordered_idom. \<not> \<bar>X1\<bar> \<le> X2 \<or> X1 \<le> \<bar>X2\<bar>"
paulson@24937
    94
  by (metis 16 abs_le_D1)
paulson@24937
    95
have 18: "\<And>X1\<Colon>'b\<Colon>type.
paulson@24937
    96
   (h\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) X1
paulson@24937
    97
   \<le> \<bar>(c\<Colon>'a\<Colon>ordered_idom) * (f\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) X1\<bar>"
paulson@24937
    98
  by (metis 17 3 15)
paulson@23449
    99
show "False"
paulson@24937
   100
  by (metis abs_le_iff 5 18 14)
paulson@23449
   101
qed
paulson@23449
   102
paulson@25710
   103
declare [[reconstruction_modulus = 2]]
paulson@25710
   104
paulson@23449
   105
lemma (*bigo_pos_const:*) "(EX (c::'a::ordered_idom). 
paulson@23449
   106
    ALL x. (abs (h x)) <= (c * (abs (f x))))
paulson@23449
   107
      = (EX c. 0 < c & (ALL x. (abs(h x)) <= (c * (abs (f x)))))"
paulson@23449
   108
  apply auto
paulson@23449
   109
  apply (case_tac "c = 0", simp)
paulson@23449
   110
  apply (rule_tac x = "1" in exI, simp)
paulson@23449
   111
  apply (rule_tac x = "abs c" in exI, auto);
paulson@23449
   112
proof (neg_clausify)
paulson@23449
   113
fix c x
paulson@24937
   114
have 0: "\<And>(X1\<Colon>'a\<Colon>ordered_idom) X2\<Colon>'a\<Colon>ordered_idom. \<bar>X1 * X2\<bar> = \<bar>X2 * X1\<bar>"
paulson@24937
   115
  by (metis abs_mult mult_commute)
paulson@24937
   116
assume 1: "\<And>x\<Colon>'b\<Colon>type.
paulson@24937
   117
   \<bar>(h\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) x\<bar>
paulson@24937
   118
   \<le> (c\<Colon>'a\<Colon>ordered_idom) * \<bar>(f\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) x\<bar>"
paulson@24937
   119
assume 2: "\<not> \<bar>(h\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) (x\<Colon>'b\<Colon>type)\<bar>
paulson@24937
   120
  \<le> \<bar>c\<Colon>'a\<Colon>ordered_idom\<bar> * \<bar>(f\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) x\<bar>"
paulson@24937
   121
have 3: "\<not> \<bar>(h\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) (x\<Colon>'b\<Colon>type)\<bar>
paulson@24937
   122
  \<le> \<bar>(c\<Colon>'a\<Colon>ordered_idom) * (f\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) x\<bar>"
paulson@24937
   123
  by (metis 2 abs_mult)
paulson@24937
   124
have 4: "\<And>(X1\<Colon>'a\<Colon>ordered_idom) X2\<Colon>'a\<Colon>ordered_idom.
paulson@24937
   125
   \<not> X1 \<le> (0\<Colon>'a\<Colon>ordered_idom) \<or> X1 \<le> \<bar>X2\<bar>"
paulson@24937
   126
  by (metis abs_ge_zero xt1(6))
paulson@24937
   127
have 5: "(0\<Colon>'a\<Colon>ordered_idom) < \<bar>(h\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) (x\<Colon>'b\<Colon>type)\<bar>"
paulson@24937
   128
  by (metis not_leE 4 3)
paulson@24937
   129
have 6: "(0\<Colon>'a\<Colon>ordered_idom)
paulson@24937
   130
< (c\<Colon>'a\<Colon>ordered_idom) * \<bar>(f\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) (x\<Colon>'b\<Colon>type)\<bar>"
paulson@24937
   131
  by (metis 1 order_less_le_trans 5)
paulson@24937
   132
have 7: "\<And>X1\<Colon>'a\<Colon>ordered_idom. (c\<Colon>'a\<Colon>ordered_idom) * \<bar>X1\<bar> = \<bar>X1 * c\<bar>"
paulson@24937
   133
  by (metis abs_ge_zero linorder_not_less mult_nonneg_nonpos2 6 linorder_linear abs_mult_pos mult_commute)
paulson@24937
   134
have 8: "\<And>X1\<Colon>'b\<Colon>type.
paulson@24937
   135
   - (h\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) X1
paulson@24937
   136
   \<le> \<bar>(c\<Colon>'a\<Colon>ordered_idom) * (f\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) X1\<bar>"
paulson@24937
   137
  by (metis 0 7 abs_le_D2 1)
paulson@24937
   138
have 9: "\<And>(X1\<Colon>'a\<Colon>ordered_idom) X2\<Colon>'a\<Colon>ordered_idom. \<not> \<bar>X1\<bar> \<le> X2 \<or> X1 \<le> \<bar>X2\<bar>"
paulson@24937
   139
  by (metis abs_ge_self xt1(6) abs_le_D1)
paulson@23449
   140
show "False"
paulson@24937
   141
  by (metis 8 abs_ge_zero abs_mult_pos abs_mult 1 9 3 abs_le_iff)
paulson@23449
   142
qed
paulson@23449
   143
paulson@25710
   144
declare [[reconstruction_modulus = 3]]
paulson@25710
   145
paulson@23449
   146
lemma (*bigo_pos_const:*) "(EX (c::'a::ordered_idom). 
paulson@23449
   147
    ALL x. (abs (h x)) <= (c * (abs (f x))))
paulson@23449
   148
      = (EX c. 0 < c & (ALL x. (abs(h x)) <= (c * (abs (f x)))))"
paulson@23449
   149
  apply auto
paulson@23449
   150
  apply (case_tac "c = 0", simp)
paulson@23449
   151
  apply (rule_tac x = "1" in exI, simp)
paulson@23449
   152
  apply (rule_tac x = "abs c" in exI, auto);
paulson@23449
   153
proof (neg_clausify)
paulson@23449
   154
fix c x
paulson@24937
   155
assume 0: "\<And>x\<Colon>'b\<Colon>type.
paulson@24937
   156
   \<bar>(h\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) x\<bar>
paulson@24937
   157
   \<le> (c\<Colon>'a\<Colon>ordered_idom) * \<bar>(f\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) x\<bar>"
paulson@24937
   158
assume 1: "\<not> \<bar>(h\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) (x\<Colon>'b\<Colon>type)\<bar>
paulson@23449
   159
  \<le> \<bar>c\<Colon>'a\<Colon>ordered_idom\<bar> * \<bar>(f\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) x\<bar>"
paulson@24937
   160
have 2: "\<And>(X1\<Colon>'a\<Colon>ordered_idom) X2\<Colon>'a\<Colon>ordered_idom.
paulson@24937
   161
   X1 \<le> \<bar>X2\<bar> \<or> (0\<Colon>'a\<Colon>ordered_idom) < X1"
paulson@24937
   162
  by (metis abs_ge_zero xt1(6) not_leE)
paulson@24937
   163
have 3: "\<not> (c\<Colon>'a\<Colon>ordered_idom) \<le> (0\<Colon>'a\<Colon>ordered_idom)"
paulson@24937
   164
  by (metis abs_ge_zero mult_nonneg_nonpos2 linorder_not_less order_less_le_trans 1 abs_mult 2 0)
paulson@24937
   165
have 4: "\<And>(X1\<Colon>'a\<Colon>ordered_idom) X2\<Colon>'a\<Colon>ordered_idom. \<bar>X1 * \<bar>X2\<bar>\<bar> = \<bar>X1 * X2\<bar>"
paulson@24937
   166
  by (metis abs_ge_zero abs_mult_pos abs_mult)
paulson@24937
   167
have 5: "\<And>X1\<Colon>'b\<Colon>type.
paulson@24937
   168
   (h\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) X1
paulson@24937
   169
   \<le> \<bar>(c\<Colon>'a\<Colon>ordered_idom) * (f\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) X1\<bar>"
paulson@24937
   170
  by (metis 4 0 xt1(6) abs_ge_self abs_le_D1)
paulson@23449
   171
show "False"
paulson@24937
   172
  by (metis abs_mult mult_commute 3 abs_mult_pos linorder_linear 0 abs_le_D2 5 1 abs_le_iff)
paulson@23449
   173
qed
paulson@23449
   174
paulson@23449
   175
paulson@25710
   176
declare [[reconstruction_modulus = 1]]
paulson@24545
   177
paulson@24545
   178
lemma (*bigo_pos_const:*) "(EX (c::'a::ordered_idom). 
paulson@24545
   179
    ALL x. (abs (h x)) <= (c * (abs (f x))))
paulson@24545
   180
      = (EX c. 0 < c & (ALL x. (abs(h x)) <= (c * (abs (f x)))))"
paulson@24545
   181
  apply auto
paulson@24545
   182
  apply (case_tac "c = 0", simp)
paulson@24545
   183
  apply (rule_tac x = "1" in exI, simp)
paulson@24545
   184
  apply (rule_tac x = "abs c" in exI, auto);
paulson@24545
   185
proof (neg_clausify)
paulson@24545
   186
fix c x  (*sort/type constraint inserted by hand!*)
paulson@24545
   187
have 0: "\<And>(X1\<Colon>'a\<Colon>ordered_idom) X2. \<bar>X1 * \<bar>X2\<bar>\<bar> = \<bar>X1 * X2\<bar>"
paulson@24545
   188
  by (metis abs_ge_zero abs_mult_pos abs_mult)
paulson@24545
   189
assume 1: "\<And>A. \<bar>h A\<bar> \<le> c * \<bar>f A\<bar>"
paulson@24545
   190
have 2: "\<And>X1 X2. \<not> \<bar>X1\<bar> \<le> X2 \<or> (0\<Colon>'a) \<le> X2"
paulson@24545
   191
  by (metis abs_ge_zero order_trans)
paulson@24545
   192
have 3: "\<And>X1. (0\<Colon>'a) \<le> c * \<bar>f X1\<bar>"
paulson@24545
   193
  by (metis 1 2)
paulson@24545
   194
have 4: "\<And>X1. c * \<bar>f X1\<bar> = \<bar>c * f X1\<bar>"
paulson@24545
   195
  by (metis 0 abs_of_nonneg 3)
paulson@24545
   196
have 5: "\<And>X1. - h X1 \<le> c * \<bar>f X1\<bar>"
paulson@24545
   197
  by (metis 1 abs_le_D2)
paulson@24545
   198
have 6: "\<And>X1. - h X1 \<le> \<bar>c * f X1\<bar>"
paulson@24545
   199
  by (metis 4 5)
paulson@24545
   200
have 7: "\<And>X1. h X1 \<le> c * \<bar>f X1\<bar>"
paulson@24545
   201
  by (metis 1 abs_le_D1)
paulson@24545
   202
have 8: "\<And>X1. h X1 \<le> \<bar>c * f X1\<bar>"
paulson@24545
   203
  by (metis 4 7)
paulson@24545
   204
assume 9: "\<not> \<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>"
paulson@24545
   205
have 10: "\<not> \<bar>h x\<bar> \<le> \<bar>c * f x\<bar>"
paulson@24545
   206
  by (metis abs_mult 9)
paulson@24545
   207
show "False"
paulson@24545
   208
  by (metis 6 8 10 abs_leI)
paulson@24545
   209
qed
paulson@24545
   210
paulson@24545
   211
paulson@25710
   212
declare [[reconstruction_sorts = true]]
paulson@24545
   213
paulson@23449
   214
lemma bigo_alt_def: "O(f) = 
paulson@23449
   215
    {h. EX c. (0 < c & (ALL x. abs (h x) <= c * abs (f x)))}"
paulson@23449
   216
by (auto simp add: bigo_def bigo_pos_const)
paulson@23449
   217
paulson@23449
   218
ML{*ResAtp.problem_name := "BigO__bigo_elt_subset"*}
paulson@23449
   219
lemma bigo_elt_subset [intro]: "f : O(g) ==> O(f) <= O(g)"
paulson@23449
   220
  apply (auto simp add: bigo_alt_def)
paulson@23449
   221
  apply (rule_tac x = "ca * c" in exI)
paulson@23449
   222
  apply (rule conjI)
paulson@23449
   223
  apply (rule mult_pos_pos)
paulson@23449
   224
  apply (assumption)+ 
paulson@23449
   225
(*sledgehammer*);
paulson@23449
   226
  apply (rule allI)
paulson@23449
   227
  apply (drule_tac x = "xa" in spec)+
paulson@23449
   228
  apply (subgoal_tac "ca * abs(f xa) <= ca * (c * abs(g xa))");
paulson@23449
   229
  apply (erule order_trans)
paulson@23449
   230
  apply (simp add: mult_ac)
paulson@23449
   231
  apply (rule mult_left_mono, assumption)
paulson@23449
   232
  apply (rule order_less_imp_le, assumption);
paulson@23449
   233
done
paulson@23449
   234
paulson@23449
   235
paulson@23449
   236
ML{*ResAtp.problem_name := "BigO__bigo_refl"*}
paulson@23449
   237
lemma bigo_refl [intro]: "f : O(f)"
paulson@23449
   238
  apply(auto simp add: bigo_def)
paulson@23449
   239
proof (neg_clausify)
paulson@23449
   240
fix x
paulson@24937
   241
assume 0: "\<And>xa. \<not> \<bar>f (x xa)\<bar> \<le> xa * \<bar>f (x xa)\<bar>"
paulson@24937
   242
have 1: "\<And>X2. X2 \<le> (1\<Colon>'b) * X2 \<or> \<not> (1\<Colon>'b) \<le> (1\<Colon>'b)"
paulson@24937
   243
  by (metis mult_le_cancel_right1 order_eq_iff)
paulson@24937
   244
have 2: "\<And>X2. X2 \<le> (1\<Colon>'b) * X2"
paulson@24937
   245
  by (metis order_eq_iff 1)
paulson@24937
   246
show "False"
paulson@23449
   247
  by (metis 0 2)
paulson@23449
   248
qed
paulson@23449
   249
paulson@23449
   250
ML{*ResAtp.problem_name := "BigO__bigo_zero"*}
paulson@23449
   251
lemma bigo_zero: "0 : O(g)"
paulson@23449
   252
  apply (auto simp add: bigo_def func_zero)
paulson@23449
   253
proof (neg_clausify)
paulson@23449
   254
fix x
paulson@24937
   255
assume 0: "\<And>xa. \<not> (0\<Colon>'b) \<le> xa * \<bar>g (x xa)\<bar>"
paulson@24937
   256
have 1: "\<not> (0\<Colon>'b) \<le> (0\<Colon>'b)"
paulson@24937
   257
  by (metis 0 mult_eq_0_iff)
paulson@24937
   258
show "False"
paulson@24937
   259
  by (metis 1 linorder_neq_iff linorder_antisym_conv1)
paulson@23449
   260
qed
paulson@23449
   261
paulson@23449
   262
lemma bigo_zero2: "O(%x.0) = {%x.0}"
paulson@23449
   263
  apply (auto simp add: bigo_def) 
paulson@23449
   264
  apply (rule ext)
paulson@23449
   265
  apply auto
paulson@23449
   266
done
paulson@23449
   267
paulson@23449
   268
lemma bigo_plus_self_subset [intro]: 
paulson@23449
   269
  "O(f) + O(f) <= O(f)"
paulson@23449
   270
  apply (auto simp add: bigo_alt_def set_plus)
paulson@23449
   271
  apply (rule_tac x = "c + ca" in exI)
paulson@23449
   272
  apply auto
nipkow@23477
   273
  apply (simp add: ring_distribs func_plus)
paulson@23449
   274
  apply (blast intro:order_trans abs_triangle_ineq add_mono elim:) 
paulson@23449
   275
done
paulson@23449
   276
paulson@23449
   277
lemma bigo_plus_idemp [simp]: "O(f) + O(f) = O(f)"
paulson@23449
   278
  apply (rule equalityI)
paulson@23449
   279
  apply (rule bigo_plus_self_subset)
paulson@23449
   280
  apply (rule set_zero_plus2) 
paulson@23449
   281
  apply (rule bigo_zero)
paulson@23449
   282
done
paulson@23449
   283
paulson@23449
   284
lemma bigo_plus_subset [intro]: "O(f + g) <= O(f) + O(g)"
paulson@23449
   285
  apply (rule subsetI)
paulson@23449
   286
  apply (auto simp add: bigo_def bigo_pos_const func_plus set_plus)
paulson@23449
   287
  apply (subst bigo_pos_const [symmetric])+
paulson@23449
   288
  apply (rule_tac x = 
paulson@23449
   289
    "%n. if abs (g n) <= (abs (f n)) then x n else 0" in exI)
paulson@23449
   290
  apply (rule conjI)
paulson@23449
   291
  apply (rule_tac x = "c + c" in exI)
paulson@23449
   292
  apply (clarsimp)
paulson@23449
   293
  apply (auto)
paulson@23449
   294
  apply (subgoal_tac "c * abs (f xa + g xa) <= (c + c) * abs (f xa)")
paulson@23449
   295
  apply (erule_tac x = xa in allE)
paulson@23449
   296
  apply (erule order_trans)
paulson@23449
   297
  apply (simp)
paulson@23449
   298
  apply (subgoal_tac "c * abs (f xa + g xa) <= c * (abs (f xa) + abs (g xa))")
paulson@23449
   299
  apply (erule order_trans)
nipkow@23477
   300
  apply (simp add: ring_distribs)
paulson@23449
   301
  apply (rule mult_left_mono)
paulson@23449
   302
  apply assumption
paulson@23449
   303
  apply (simp add: order_less_le)
paulson@23449
   304
  apply (rule mult_left_mono)
paulson@23449
   305
  apply (simp add: abs_triangle_ineq)
paulson@23449
   306
  apply (simp add: order_less_le)
paulson@23449
   307
  apply (rule mult_nonneg_nonneg)
paulson@23449
   308
  apply (rule add_nonneg_nonneg)
paulson@23449
   309
  apply auto
paulson@23449
   310
  apply (rule_tac x = "%n. if (abs (f n)) <  abs (g n) then x n else 0" 
paulson@23449
   311
     in exI)
paulson@23449
   312
  apply (rule conjI)
paulson@23449
   313
  apply (rule_tac x = "c + c" in exI)
paulson@23449
   314
  apply auto
paulson@23449
   315
  apply (subgoal_tac "c * abs (f xa + g xa) <= (c + c) * abs (g xa)")
paulson@23449
   316
  apply (erule_tac x = xa in allE)
paulson@23449
   317
  apply (erule order_trans)
paulson@23449
   318
  apply (simp)
paulson@23449
   319
  apply (subgoal_tac "c * abs (f xa + g xa) <= c * (abs (f xa) + abs (g xa))")
paulson@23449
   320
  apply (erule order_trans)
nipkow@23477
   321
  apply (simp add: ring_distribs)
paulson@23449
   322
  apply (rule mult_left_mono)
paulson@23449
   323
  apply (simp add: order_less_le)
paulson@23449
   324
  apply (simp add: order_less_le)
paulson@23449
   325
  apply (rule mult_left_mono)
paulson@23449
   326
  apply (rule abs_triangle_ineq)
paulson@23449
   327
  apply (simp add: order_less_le)
paulson@25087
   328
apply (metis abs_not_less_zero double_less_0_iff less_not_permute linorder_not_less mult_less_0_iff)
paulson@23449
   329
  apply (rule ext)
paulson@23449
   330
  apply (auto simp add: if_splits linorder_not_le)
paulson@23449
   331
done
paulson@23449
   332
paulson@23449
   333
lemma bigo_plus_subset2 [intro]: "A <= O(f) ==> B <= O(f) ==> A + B <= O(f)"
paulson@23449
   334
  apply (subgoal_tac "A + B <= O(f) + O(f)")
paulson@23449
   335
  apply (erule order_trans)
paulson@23449
   336
  apply simp
paulson@23449
   337
  apply (auto del: subsetI simp del: bigo_plus_idemp)
paulson@23449
   338
done
paulson@23449
   339
paulson@23449
   340
ML{*ResAtp.problem_name := "BigO__bigo_plus_eq"*}
paulson@23449
   341
lemma bigo_plus_eq: "ALL x. 0 <= f x ==> ALL x. 0 <= g x ==> 
paulson@23449
   342
  O(f + g) = O(f) + O(g)"
paulson@23449
   343
  apply (rule equalityI)
paulson@23449
   344
  apply (rule bigo_plus_subset)
paulson@23449
   345
  apply (simp add: bigo_alt_def set_plus func_plus)
paulson@23449
   346
  apply clarify 
paulson@23449
   347
(*sledgehammer*); 
paulson@23449
   348
  apply (rule_tac x = "max c ca" in exI)
paulson@23449
   349
  apply (rule conjI)
paulson@25087
   350
   apply (metis Orderings.less_max_iff_disj)
paulson@23449
   351
  apply clarify
paulson@23449
   352
  apply (drule_tac x = "xa" in spec)+
paulson@23449
   353
  apply (subgoal_tac "0 <= f xa + g xa")
nipkow@23477
   354
  apply (simp add: ring_distribs)
paulson@23449
   355
  apply (subgoal_tac "abs(a xa + b xa) <= abs(a xa) + abs(b xa)")
paulson@23449
   356
  apply (subgoal_tac "abs(a xa) + abs(b xa) <= 
paulson@23449
   357
      max c ca * f xa + max c ca * g xa")
paulson@23449
   358
  apply (blast intro: order_trans)
paulson@23449
   359
  defer 1
paulson@23449
   360
  apply (rule abs_triangle_ineq)
paulson@25087
   361
  apply (metis add_nonneg_nonneg)
paulson@23449
   362
  apply (rule add_mono)
paulson@23449
   363
ML{*ResAtp.problem_name := "BigO__bigo_plus_eq_simpler"*} 
paulson@24942
   364
(*Found by SPASS; SLOW*)
paulson@25710
   365
apply (metis le_maxI2 linorder_linear linorder_not_le min_max.less_eq_less_sup.sup_absorb1 mult_le_cancel_right order_trans)
paulson@25710
   366
apply (metis le_maxI2 linorder_not_le mult_le_cancel_right order_trans)
paulson@23449
   367
done
paulson@23449
   368
paulson@23449
   369
ML{*ResAtp.problem_name := "BigO__bigo_bounded_alt"*}
paulson@23449
   370
lemma bigo_bounded_alt: "ALL x. 0 <= f x ==> ALL x. f x <= c * g x ==> 
paulson@23449
   371
    f : O(g)" 
paulson@23449
   372
  apply (auto simp add: bigo_def)
paulson@23449
   373
(*Version 1: one-shot proof*)
paulson@24942
   374
  apply (metis OrderedGroup.abs_le_D1 Orderings.linorder_class.not_less  order_less_le  Orderings.xt1(12)  Ring_and_Field.abs_mult)
paulson@23449
   375
  done
paulson@23449
   376
paulson@23449
   377
lemma bigo_bounded_alt: "ALL x. 0 <= f x ==> ALL x. f x <= c * g x ==> 
paulson@23449
   378
    f : O(g)" 
paulson@23449
   379
  apply (auto simp add: bigo_def)
paulson@23449
   380
(*Version 2: single-step proof*)
paulson@23449
   381
proof (neg_clausify)
paulson@23449
   382
fix x
paulson@24937
   383
assume 0: "\<And>x. f x \<le> c * g x"
paulson@24937
   384
assume 1: "\<And>xa. \<not> f (x xa) \<le> xa * \<bar>g (x xa)\<bar>"
paulson@24937
   385
have 2: "\<And>X3. c * g X3 = f X3 \<or> \<not> c * g X3 \<le> f X3"
paulson@24937
   386
  by (metis 0 order_antisym_conv)
paulson@24937
   387
have 3: "\<And>X3. \<not> f (x \<bar>X3\<bar>) \<le> \<bar>X3 * g (x \<bar>X3\<bar>)\<bar>"
paulson@24937
   388
  by (metis 1 abs_mult)
paulson@24937
   389
have 4: "\<And>X1 X3\<Colon>'b\<Colon>ordered_idom. X3 \<le> X1 \<or> X1 \<le> \<bar>X3\<bar>"
paulson@24937
   390
  by (metis linorder_linear abs_le_D1)
paulson@24937
   391
have 5: "\<And>X3::'b. \<bar>X3\<bar> * \<bar>X3\<bar> = X3 * X3"
paulson@24937
   392
  by (metis abs_mult_self AC_mult.f.commute)
paulson@24937
   393
have 6: "\<And>X3. \<not> X3 * X3 < (0\<Colon>'b\<Colon>ordered_idom)"
paulson@24937
   394
  by (metis not_square_less_zero AC_mult.f.commute)
paulson@24937
   395
have 7: "\<And>X1 X3::'b. \<bar>X1\<bar> * \<bar>X3\<bar> = \<bar>X3 * X1\<bar>"
paulson@24937
   396
  by (metis abs_mult AC_mult.f.commute)
paulson@24937
   397
have 8: "\<And>X3::'b. X3 * X3 = \<bar>X3 * X3\<bar>"
paulson@24937
   398
  by (metis abs_mult 5)
paulson@24937
   399
have 9: "\<And>X3. X3 * g (x \<bar>X3\<bar>) \<le> f (x \<bar>X3\<bar>)"
paulson@24937
   400
  by (metis 3 4)
paulson@24937
   401
have 10: "c * g (x \<bar>c\<bar>) = f (x \<bar>c\<bar>)"
paulson@24937
   402
  by (metis 2 9)
paulson@24937
   403
have 11: "\<And>X3::'b. \<bar>X3\<bar> * \<bar>\<bar>X3\<bar>\<bar> = \<bar>X3\<bar> * \<bar>X3\<bar>"
paulson@24937
   404
  by (metis abs_idempotent abs_mult 8)
paulson@24937
   405
have 12: "\<And>X3::'b. \<bar>X3 * \<bar>X3\<bar>\<bar> = \<bar>X3\<bar> * \<bar>X3\<bar>"
paulson@24937
   406
  by (metis AC_mult.f.commute 7 11)
paulson@24937
   407
have 13: "\<And>X3::'b. \<bar>X3 * \<bar>X3\<bar>\<bar> = X3 * X3"
paulson@24937
   408
  by (metis 8 7 12)
paulson@24937
   409
have 14: "\<And>X3. X3 \<le> \<bar>X3\<bar> \<or> X3 < (0\<Colon>'b)"
paulson@24937
   410
  by (metis abs_ge_self abs_le_D1 abs_if)
paulson@24937
   411
have 15: "\<And>X3. X3 \<le> \<bar>X3\<bar> \<or> \<bar>X3\<bar> < (0\<Colon>'b)"
paulson@24937
   412
  by (metis abs_ge_self abs_le_D1 abs_if)
paulson@24937
   413
have 16: "\<And>X3. X3 * X3 < (0\<Colon>'b) \<or> X3 * \<bar>X3\<bar> \<le> X3 * X3"
paulson@24937
   414
  by (metis 15 13)
paulson@24937
   415
have 17: "\<And>X3::'b. X3 * \<bar>X3\<bar> \<le> X3 * X3"
paulson@24937
   416
  by (metis 16 6)
paulson@24937
   417
have 18: "\<And>X3. X3 \<le> \<bar>X3\<bar> \<or> \<not> X3 < (0\<Colon>'b)"
paulson@24937
   418
  by (metis mult_le_cancel_left 17)
paulson@24937
   419
have 19: "\<And>X3::'b. X3 \<le> \<bar>X3\<bar>"
paulson@24937
   420
  by (metis 18 14)
paulson@24937
   421
have 20: "\<not> f (x \<bar>c\<bar>) \<le> \<bar>f (x \<bar>c\<bar>)\<bar>"
paulson@24937
   422
  by (metis 3 10)
paulson@24937
   423
show "False"
paulson@24937
   424
  by (metis 20 19)
paulson@23449
   425
qed
paulson@23449
   426
paulson@23449
   427
paulson@23449
   428
text{*So here is the easier (and more natural) problem using transitivity*}
paulson@23449
   429
ML{*ResAtp.problem_name := "BigO__bigo_bounded_alt_trans"*}
paulson@23449
   430
lemma "ALL x. 0 <= f x ==> ALL x. f x <= c * g x ==> f : O(g)" 
paulson@23449
   431
  apply (auto simp add: bigo_def)
paulson@23449
   432
  (*Version 1: one-shot proof*) 
paulson@25710
   433
  apply (metis Orderings.leD Orderings.leI abs_ge_self abs_le_D1 abs_mult abs_of_nonneg order_le_less)
paulson@23449
   434
  done
paulson@23449
   435
paulson@23449
   436
text{*So here is the easier (and more natural) problem using transitivity*}
paulson@23449
   437
ML{*ResAtp.problem_name := "BigO__bigo_bounded_alt_trans"*}
paulson@23449
   438
lemma "ALL x. 0 <= f x ==> ALL x. f x <= c * g x ==> f : O(g)" 
paulson@23449
   439
  apply (auto simp add: bigo_def)
paulson@23449
   440
(*Version 2: single-step proof*)
paulson@23449
   441
proof (neg_clausify)
paulson@23449
   442
fix x
paulson@23519
   443
assume 0: "\<And>A\<Colon>'a\<Colon>type.
paulson@23519
   444
   (f\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) A
paulson@23519
   445
   \<le> (c\<Colon>'b\<Colon>ordered_idom) * (g\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) A"
paulson@23519
   446
assume 1: "\<And>A\<Colon>'b\<Colon>ordered_idom.
paulson@23519
   447
   \<not> (f\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) ((x\<Colon>'b\<Colon>ordered_idom \<Rightarrow> 'a\<Colon>type) A)
paulson@23519
   448
     \<le> A * \<bar>(g\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) (x A)\<bar>"
paulson@23519
   449
have 2: "\<And>X2\<Colon>'a\<Colon>type.
paulson@23519
   450
   \<not> (c\<Colon>'b\<Colon>ordered_idom) * (g\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) X2
paulson@23519
   451
     < (f\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) X2"
paulson@23519
   452
  by (metis 0 linorder_not_le)
paulson@23519
   453
have 3: "\<And>X2\<Colon>'b\<Colon>ordered_idom.
paulson@23519
   454
   \<not> (f\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) ((x\<Colon>'b\<Colon>ordered_idom \<Rightarrow> 'a\<Colon>type) \<bar>X2\<bar>)
paulson@23519
   455
     \<le> \<bar>X2 * (g\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) (x \<bar>X2\<bar>)\<bar>"
paulson@23519
   456
  by (metis abs_mult 1)
paulson@23519
   457
have 4: "\<And>X2\<Colon>'b\<Colon>ordered_idom.
paulson@23519
   458
   \<bar>X2 * (g\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) ((x\<Colon>'b\<Colon>ordered_idom \<Rightarrow> 'a\<Colon>type) \<bar>X2\<bar>)\<bar>
paulson@23519
   459
   < (f\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) (x \<bar>X2\<bar>)"
paulson@23519
   460
  by (metis 3 linorder_not_less)
paulson@23519
   461
have 5: "\<And>X2\<Colon>'b\<Colon>ordered_idom.
paulson@23519
   462
   X2 * (g\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) ((x\<Colon>'b\<Colon>ordered_idom \<Rightarrow> 'a\<Colon>type) \<bar>X2\<bar>)
paulson@23519
   463
   < (f\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) (x \<bar>X2\<bar>)"
paulson@23519
   464
  by (metis abs_less_iff 4)
paulson@23519
   465
show "False"
paulson@23519
   466
  by (metis 2 5)
paulson@23449
   467
qed
paulson@23449
   468
paulson@23449
   469
paulson@23449
   470
lemma bigo_bounded: "ALL x. 0 <= f x ==> ALL x. f x <= g x ==> 
paulson@23449
   471
    f : O(g)" 
paulson@23449
   472
  apply (erule bigo_bounded_alt [of f 1 g])
paulson@23449
   473
  apply simp
paulson@23449
   474
done
paulson@23449
   475
paulson@23449
   476
ML{*ResAtp.problem_name := "BigO__bigo_bounded2"*}
paulson@23449
   477
lemma bigo_bounded2: "ALL x. lb x <= f x ==> ALL x. f x <= lb x + g x ==>
paulson@23449
   478
    f : lb +o O(g)"
paulson@23449
   479
  apply (rule set_minus_imp_plus)
paulson@23449
   480
  apply (rule bigo_bounded)
paulson@23449
   481
  apply (auto simp add: diff_minus func_minus func_plus)
paulson@23449
   482
  prefer 2
paulson@23449
   483
  apply (drule_tac x = x in spec)+ 
paulson@23449
   484
  apply arith (*not clear that it's provable otherwise*) 
paulson@23449
   485
proof (neg_clausify)
paulson@23449
   486
fix x
paulson@23449
   487
assume 0: "\<And>y. lb y \<le> f y"
paulson@23449
   488
assume 1: "\<not> (0\<Colon>'b) \<le> f x + - lb x"
paulson@23449
   489
have 2: "\<And>X3. (0\<Colon>'b) + X3 = X3"
paulson@23449
   490
  by (metis diff_eq_eq right_minus_eq)
paulson@23449
   491
have 3: "\<not> (0\<Colon>'b) \<le> f x - lb x"
paulson@23449
   492
  by (metis 1 compare_rls(1))
paulson@23449
   493
have 4: "\<not> (0\<Colon>'b) + lb x \<le> f x"
paulson@23449
   494
  by (metis 3 le_diff_eq)
paulson@23449
   495
show "False"
paulson@23449
   496
  by (metis 4 2 0)
paulson@23449
   497
qed
paulson@23449
   498
paulson@23449
   499
ML{*ResAtp.problem_name := "BigO__bigo_abs"*}
paulson@23449
   500
lemma bigo_abs: "(%x. abs(f x)) =o O(f)" 
paulson@23449
   501
  apply (unfold bigo_def)
paulson@23449
   502
  apply auto
paulson@23449
   503
proof (neg_clausify)
paulson@23449
   504
fix x
paulson@24937
   505
assume 0: "\<And>xa. \<not> \<bar>f (x xa)\<bar> \<le> xa * \<bar>f (x xa)\<bar>"
paulson@24937
   506
have 1: "\<And>X2. X2 \<le> (1\<Colon>'b) * X2 \<or> \<not> (1\<Colon>'b) \<le> (1\<Colon>'b)"
paulson@24937
   507
  by (metis mult_le_cancel_right1 order_eq_iff)
paulson@24937
   508
have 2: "\<And>X2. X2 \<le> (1\<Colon>'b) * X2"
paulson@24937
   509
  by (metis order_eq_iff 1)
paulson@23449
   510
show "False"
paulson@23449
   511
  by (metis 0 2)
paulson@23449
   512
qed
paulson@23449
   513
paulson@23449
   514
ML{*ResAtp.problem_name := "BigO__bigo_abs2"*}
paulson@23449
   515
lemma bigo_abs2: "f =o O(%x. abs(f x))"
paulson@23449
   516
  apply (unfold bigo_def)
paulson@23449
   517
  apply auto
paulson@23449
   518
proof (neg_clausify)
paulson@23449
   519
fix x
paulson@24937
   520
assume 0: "\<And>xa. \<not> \<bar>f (x xa)\<bar> \<le> xa * \<bar>f (x xa)\<bar>"
paulson@24937
   521
have 1: "\<And>X2. X2 \<le> (1\<Colon>'b) * X2 \<or> \<not> (1\<Colon>'b) \<le> (1\<Colon>'b)"
paulson@24937
   522
  by (metis mult_le_cancel_right1 order_eq_iff)
paulson@24937
   523
have 2: "\<And>X2. X2 \<le> (1\<Colon>'b) * X2"
paulson@24937
   524
  by (metis order_eq_iff 1)
paulson@23449
   525
show "False"
paulson@23449
   526
  by (metis 0 2)
paulson@23449
   527
qed
paulson@23449
   528
 
paulson@23449
   529
lemma bigo_abs3: "O(f) = O(%x. abs(f x))"
paulson@23449
   530
  apply (rule equalityI)
paulson@23449
   531
  apply (rule bigo_elt_subset)
paulson@23449
   532
  apply (rule bigo_abs2)
paulson@23449
   533
  apply (rule bigo_elt_subset)
paulson@23449
   534
  apply (rule bigo_abs)
paulson@23449
   535
done
paulson@23449
   536
paulson@23449
   537
lemma bigo_abs4: "f =o g +o O(h) ==> 
paulson@23449
   538
    (%x. abs (f x)) =o (%x. abs (g x)) +o O(h)"
paulson@23449
   539
  apply (drule set_plus_imp_minus)
paulson@23449
   540
  apply (rule set_minus_imp_plus)
paulson@23449
   541
  apply (subst func_diff)
paulson@23449
   542
proof -
paulson@23449
   543
  assume a: "f - g : O(h)"
paulson@23449
   544
  have "(%x. abs (f x) - abs (g x)) =o O(%x. abs(abs (f x) - abs (g x)))"
paulson@23449
   545
    by (rule bigo_abs2)
paulson@23449
   546
  also have "... <= O(%x. abs (f x - g x))"
paulson@23449
   547
    apply (rule bigo_elt_subset)
paulson@23449
   548
    apply (rule bigo_bounded)
paulson@23449
   549
    apply force
paulson@23449
   550
    apply (rule allI)
paulson@23449
   551
    apply (rule abs_triangle_ineq3)
paulson@23449
   552
    done
paulson@23449
   553
  also have "... <= O(f - g)"
paulson@23449
   554
    apply (rule bigo_elt_subset)
paulson@23449
   555
    apply (subst func_diff)
paulson@23449
   556
    apply (rule bigo_abs)
paulson@23449
   557
    done
paulson@23449
   558
  also have "... <= O(h)"
wenzelm@23464
   559
    using a by (rule bigo_elt_subset)
paulson@23449
   560
  finally show "(%x. abs (f x) - abs (g x)) : O(h)".
paulson@23449
   561
qed
paulson@23449
   562
paulson@23449
   563
lemma bigo_abs5: "f =o O(g) ==> (%x. abs(f x)) =o O(g)" 
paulson@23449
   564
by (unfold bigo_def, auto)
paulson@23449
   565
paulson@23449
   566
lemma bigo_elt_subset2 [intro]: "f : g +o O(h) ==> O(f) <= O(g) + O(h)"
paulson@23449
   567
proof -
paulson@23449
   568
  assume "f : g +o O(h)"
paulson@23449
   569
  also have "... <= O(g) + O(h)"
paulson@23449
   570
    by (auto del: subsetI)
paulson@23449
   571
  also have "... = O(%x. abs(g x)) + O(%x. abs(h x))"
paulson@23449
   572
    apply (subst bigo_abs3 [symmetric])+
paulson@23449
   573
    apply (rule refl)
paulson@23449
   574
    done
paulson@23449
   575
  also have "... = O((%x. abs(g x)) + (%x. abs(h x)))"
paulson@23449
   576
    by (rule bigo_plus_eq [symmetric], auto)
paulson@23449
   577
  finally have "f : ...".
paulson@23449
   578
  then have "O(f) <= ..."
paulson@23449
   579
    by (elim bigo_elt_subset)
paulson@23449
   580
  also have "... = O(%x. abs(g x)) + O(%x. abs(h x))"
paulson@23449
   581
    by (rule bigo_plus_eq, auto)
paulson@23449
   582
  finally show ?thesis
paulson@23449
   583
    by (simp add: bigo_abs3 [symmetric])
paulson@23449
   584
qed
paulson@23449
   585
paulson@23449
   586
ML{*ResAtp.problem_name := "BigO__bigo_mult"*}
paulson@23449
   587
lemma bigo_mult [intro]: "O(f)*O(g) <= O(f * g)"
paulson@23449
   588
  apply (rule subsetI)
paulson@23449
   589
  apply (subst bigo_def)
paulson@23449
   590
  apply (auto simp del: abs_mult mult_ac
paulson@23449
   591
              simp add: bigo_alt_def set_times func_times)
paulson@23449
   592
(*sledgehammer*); 
paulson@23449
   593
  apply (rule_tac x = "c * ca" in exI)
paulson@23449
   594
  apply(rule allI)
paulson@23449
   595
  apply(erule_tac x = x in allE)+
paulson@23449
   596
  apply(subgoal_tac "c * ca * abs(f x * g x) = 
paulson@23449
   597
      (c * abs(f x)) * (ca * abs(g x))")
paulson@23449
   598
ML{*ResAtp.problem_name := "BigO__bigo_mult_simpler"*}
paulson@23449
   599
prefer 2 
paulson@23449
   600
apply (metis  Finite_Set.AC_mult.f.assoc  Finite_Set.AC_mult.f.left_commute  OrderedGroup.abs_of_pos  OrderedGroup.mult_left_commute  Ring_and_Field.abs_mult  Ring_and_Field.mult_pos_pos)
paulson@23449
   601
  apply(erule ssubst) 
paulson@23449
   602
  apply (subst abs_mult)
paulson@23449
   603
(*not qute BigO__bigo_mult_simpler_1 (a hard problem!) as abs_mult has
paulson@23449
   604
  just been done*)
paulson@23449
   605
proof (neg_clausify)
paulson@23449
   606
fix a c b ca x
paulson@23449
   607
assume 0: "(0\<Colon>'b\<Colon>ordered_idom) < (c\<Colon>'b\<Colon>ordered_idom)"
paulson@23449
   608
assume 1: "\<bar>(a\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) (x\<Colon>'a)\<bar>
paulson@23449
   609
\<le> (c\<Colon>'b\<Colon>ordered_idom) * \<bar>(f\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar>"
paulson@23449
   610
assume 2: "\<bar>(b\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) (x\<Colon>'a)\<bar>
paulson@23449
   611
\<le> (ca\<Colon>'b\<Colon>ordered_idom) * \<bar>(g\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar>"
paulson@23449
   612
assume 3: "\<not> \<bar>(a\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) (x\<Colon>'a)\<bar> *
paulson@23449
   613
  \<bar>(b\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar>
paulson@23449
   614
  \<le> (c\<Colon>'b\<Colon>ordered_idom) * \<bar>(f\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar> *
paulson@23449
   615
    ((ca\<Colon>'b\<Colon>ordered_idom) * \<bar>(g\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar>)"
paulson@23449
   616
have 4: "\<bar>c\<Colon>'b\<Colon>ordered_idom\<bar> = c"
paulson@23449
   617
  by (metis OrderedGroup.abs_of_pos 0)
paulson@23449
   618
have 5: "\<And>X1\<Colon>'b\<Colon>ordered_idom. (c\<Colon>'b\<Colon>ordered_idom) * \<bar>X1\<bar> = \<bar>c * X1\<bar>"
paulson@23449
   619
  by (metis Ring_and_Field.abs_mult 4)
paulson@23449
   620
have 6: "(0\<Colon>'b\<Colon>ordered_idom) = (1\<Colon>'b\<Colon>ordered_idom) \<or>
paulson@23449
   621
(0\<Colon>'b\<Colon>ordered_idom) < (1\<Colon>'b\<Colon>ordered_idom)"
paulson@23449
   622
  by (metis OrderedGroup.abs_not_less_zero Ring_and_Field.abs_one Ring_and_Field.linorder_neqE_ordered_idom)
paulson@23449
   623
have 7: "(0\<Colon>'b\<Colon>ordered_idom) < (1\<Colon>'b\<Colon>ordered_idom)"
paulson@23449
   624
  by (metis 6 Ring_and_Field.one_neq_zero)
paulson@23449
   625
have 8: "\<bar>1\<Colon>'b\<Colon>ordered_idom\<bar> = (1\<Colon>'b\<Colon>ordered_idom)"
paulson@23449
   626
  by (metis OrderedGroup.abs_of_pos 7)
paulson@23449
   627
have 9: "\<And>X1\<Colon>'b\<Colon>ordered_idom. (0\<Colon>'b\<Colon>ordered_idom) \<le> (c\<Colon>'b\<Colon>ordered_idom) * \<bar>X1\<bar>"
paulson@23449
   628
  by (metis OrderedGroup.abs_ge_zero 5)
paulson@23449
   629
have 10: "\<And>X1\<Colon>'b\<Colon>ordered_idom. X1 * (1\<Colon>'b\<Colon>ordered_idom) = X1"
paulson@23449
   630
  by (metis Ring_and_Field.mult_cancel_right2 Finite_Set.AC_mult.f.commute)
paulson@23449
   631
have 11: "\<And>X1\<Colon>'b\<Colon>ordered_idom. \<bar>\<bar>X1\<bar>\<bar> = \<bar>X1\<bar> * \<bar>1\<Colon>'b\<Colon>ordered_idom\<bar>"
paulson@23449
   632
  by (metis Ring_and_Field.abs_mult OrderedGroup.abs_idempotent 10)
paulson@23449
   633
have 12: "\<And>X1\<Colon>'b\<Colon>ordered_idom. \<bar>\<bar>X1\<bar>\<bar> = \<bar>X1\<bar>"
paulson@23449
   634
  by (metis 11 8 10)
paulson@23449
   635
have 13: "\<And>X1\<Colon>'b\<Colon>ordered_idom. (0\<Colon>'b\<Colon>ordered_idom) \<le> \<bar>X1\<bar>"
paulson@23449
   636
  by (metis OrderedGroup.abs_ge_zero 12)
paulson@23449
   637
have 14: "\<not> (0\<Colon>'b\<Colon>ordered_idom)
paulson@23449
   638
  \<le> (c\<Colon>'b\<Colon>ordered_idom) * \<bar>(f\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) (x\<Colon>'a)\<bar> \<or>
paulson@23449
   639
\<not> (0\<Colon>'b\<Colon>ordered_idom) \<le> \<bar>(b\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar> \<or>
paulson@23449
   640
\<not> \<bar>b x\<bar> \<le> (ca\<Colon>'b\<Colon>ordered_idom) * \<bar>(g\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar> \<or>
paulson@23449
   641
\<not> \<bar>(a\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar> \<le> c * \<bar>f x\<bar>"
paulson@23449
   642
  by (metis 3 Ring_and_Field.mult_mono)
paulson@23449
   643
have 15: "\<not> (0\<Colon>'b\<Colon>ordered_idom) \<le> \<bar>(b\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) (x\<Colon>'a)\<bar> \<or>
paulson@23449
   644
\<not> \<bar>b x\<bar> \<le> (ca\<Colon>'b\<Colon>ordered_idom) * \<bar>(g\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar> \<or>
paulson@23449
   645
\<not> \<bar>(a\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar>
paulson@23449
   646
  \<le> (c\<Colon>'b\<Colon>ordered_idom) * \<bar>(f\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar>"
paulson@23449
   647
  by (metis 14 9)
paulson@23449
   648
have 16: "\<not> \<bar>(b\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) (x\<Colon>'a)\<bar>
paulson@23449
   649
  \<le> (ca\<Colon>'b\<Colon>ordered_idom) * \<bar>(g\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar> \<or>
paulson@23449
   650
\<not> \<bar>(a\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar>
paulson@23449
   651
  \<le> (c\<Colon>'b\<Colon>ordered_idom) * \<bar>(f\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar>"
paulson@23449
   652
  by (metis 15 13)
paulson@23449
   653
have 17: "\<not> \<bar>(a\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) (x\<Colon>'a)\<bar>
paulson@23449
   654
  \<le> (c\<Colon>'b\<Colon>ordered_idom) * \<bar>(f\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar>"
paulson@23449
   655
  by (metis 16 2)
paulson@23449
   656
show 18: "False"
paulson@23449
   657
  by (metis 17 1)
paulson@23449
   658
qed
paulson@23449
   659
paulson@23449
   660
paulson@23449
   661
ML{*ResAtp.problem_name := "BigO__bigo_mult2"*}
paulson@23449
   662
lemma bigo_mult2 [intro]: "f *o O(g) <= O(f * g)"
paulson@23449
   663
  apply (auto simp add: bigo_def elt_set_times_def func_times abs_mult)
paulson@23449
   664
(*sledgehammer*); 
paulson@23449
   665
  apply (rule_tac x = c in exI)
paulson@23449
   666
  apply clarify
paulson@23449
   667
  apply (drule_tac x = x in spec)
paulson@23449
   668
ML{*ResAtp.problem_name := "BigO__bigo_mult2_simpler"*}
paulson@24942
   669
(*sledgehammer [no luck]*); 
paulson@23449
   670
  apply (subgoal_tac "abs(f x) * abs(b x) <= abs(f x) * (c * abs(g x))")
paulson@23449
   671
  apply (simp add: mult_ac)
paulson@23449
   672
  apply (rule mult_left_mono, assumption)
paulson@23449
   673
  apply (rule abs_ge_zero)
paulson@23449
   674
done
paulson@23449
   675
paulson@23449
   676
ML{*ResAtp.problem_name:="BigO__bigo_mult3"*}
paulson@23449
   677
lemma bigo_mult3: "f : O(h) ==> g : O(j) ==> f * g : O(h * j)"
paulson@23449
   678
by (metis bigo_mult set_times_intro subset_iff)
paulson@23449
   679
paulson@23449
   680
ML{*ResAtp.problem_name:="BigO__bigo_mult4"*}
paulson@23449
   681
lemma bigo_mult4 [intro]:"f : k +o O(h) ==> g * f : (g * k) +o O(g * h)"
paulson@23449
   682
by (metis bigo_mult2 set_plus_mono_b set_times_intro2 set_times_plus_distrib)
paulson@23449
   683
paulson@23449
   684
paulson@23449
   685
lemma bigo_mult5: "ALL x. f x ~= 0 ==>
paulson@23449
   686
    O(f * g) <= (f::'a => ('b::ordered_field)) *o O(g)"
paulson@23449
   687
proof -
paulson@23449
   688
  assume "ALL x. f x ~= 0"
paulson@23449
   689
  show "O(f * g) <= f *o O(g)"
paulson@23449
   690
  proof
paulson@23449
   691
    fix h
paulson@23449
   692
    assume "h : O(f * g)"
paulson@23449
   693
    then have "(%x. 1 / (f x)) * h : (%x. 1 / f x) *o O(f * g)"
paulson@23449
   694
      by auto
paulson@23449
   695
    also have "... <= O((%x. 1 / f x) * (f * g))"
paulson@23449
   696
      by (rule bigo_mult2)
paulson@23449
   697
    also have "(%x. 1 / f x) * (f * g) = g"
paulson@23449
   698
      apply (simp add: func_times) 
paulson@23449
   699
      apply (rule ext)
paulson@23449
   700
      apply (simp add: prems nonzero_divide_eq_eq mult_ac)
paulson@23449
   701
      done
paulson@23449
   702
    finally have "(%x. (1::'b) / f x) * h : O(g)".
paulson@23449
   703
    then have "f * ((%x. (1::'b) / f x) * h) : f *o O(g)"
paulson@23449
   704
      by auto
paulson@23449
   705
    also have "f * ((%x. (1::'b) / f x) * h) = h"
paulson@23449
   706
      apply (simp add: func_times) 
paulson@23449
   707
      apply (rule ext)
paulson@23449
   708
      apply (simp add: prems nonzero_divide_eq_eq mult_ac)
paulson@23449
   709
      done
paulson@23449
   710
    finally show "h : f *o O(g)".
paulson@23449
   711
  qed
paulson@23449
   712
qed
paulson@23449
   713
paulson@23449
   714
ML{*ResAtp.problem_name := "BigO__bigo_mult6"*}
paulson@23449
   715
lemma bigo_mult6: "ALL x. f x ~= 0 ==>
paulson@23449
   716
    O(f * g) = (f::'a => ('b::ordered_field)) *o O(g)"
paulson@23449
   717
by (metis bigo_mult2 bigo_mult5 order_antisym)
paulson@23449
   718
paulson@23449
   719
(*proof requires relaxing relevance: 2007-01-25*)
paulson@23449
   720
ML{*ResAtp.problem_name := "BigO__bigo_mult7"*}
paulson@23449
   721
  declare bigo_mult6 [simp]
paulson@23449
   722
lemma bigo_mult7: "ALL x. f x ~= 0 ==>
paulson@23449
   723
    O(f * g) <= O(f::'a => ('b::ordered_field)) * O(g)"
paulson@23449
   724
(*sledgehammer*)
paulson@23449
   725
  apply (subst bigo_mult6)
paulson@23449
   726
  apply assumption
paulson@23449
   727
  apply (rule set_times_mono3) 
paulson@23449
   728
  apply (rule bigo_refl)
paulson@23449
   729
done
paulson@23449
   730
  declare bigo_mult6 [simp del]
paulson@23449
   731
paulson@23449
   732
ML{*ResAtp.problem_name := "BigO__bigo_mult8"*}
paulson@23449
   733
  declare bigo_mult7[intro!]
paulson@23449
   734
lemma bigo_mult8: "ALL x. f x ~= 0 ==>
paulson@23449
   735
    O(f * g) = O(f::'a => ('b::ordered_field)) * O(g)"
paulson@23449
   736
by (metis bigo_mult bigo_mult7 order_antisym_conv)
paulson@23449
   737
paulson@23449
   738
lemma bigo_minus [intro]: "f : O(g) ==> - f : O(g)"
paulson@23449
   739
  by (auto simp add: bigo_def func_minus)
paulson@23449
   740
paulson@23449
   741
lemma bigo_minus2: "f : g +o O(h) ==> -f : -g +o O(h)"
paulson@23449
   742
  apply (rule set_minus_imp_plus)
paulson@23449
   743
  apply (drule set_plus_imp_minus)
paulson@23449
   744
  apply (drule bigo_minus)
paulson@23449
   745
  apply (simp add: diff_minus)
paulson@23449
   746
done
paulson@23449
   747
paulson@23449
   748
lemma bigo_minus3: "O(-f) = O(f)"
paulson@23449
   749
  by (auto simp add: bigo_def func_minus abs_minus_cancel)
paulson@23449
   750
paulson@23449
   751
lemma bigo_plus_absorb_lemma1: "f : O(g) ==> f +o O(g) <= O(g)"
paulson@23449
   752
proof -
paulson@23449
   753
  assume a: "f : O(g)"
paulson@23449
   754
  show "f +o O(g) <= O(g)"
paulson@23449
   755
  proof -
paulson@23449
   756
    have "f : O(f)" by auto
paulson@23449
   757
    then have "f +o O(g) <= O(f) + O(g)"
paulson@23449
   758
      by (auto del: subsetI)
paulson@23449
   759
    also have "... <= O(g) + O(g)"
paulson@23449
   760
    proof -
paulson@23449
   761
      from a have "O(f) <= O(g)" by (auto del: subsetI)
paulson@23449
   762
      thus ?thesis by (auto del: subsetI)
paulson@23449
   763
    qed
paulson@23449
   764
    also have "... <= O(g)" by (simp add: bigo_plus_idemp)
paulson@23449
   765
    finally show ?thesis .
paulson@23449
   766
  qed
paulson@23449
   767
qed
paulson@23449
   768
paulson@23449
   769
lemma bigo_plus_absorb_lemma2: "f : O(g) ==> O(g) <= f +o O(g)"
paulson@23449
   770
proof -
paulson@23449
   771
  assume a: "f : O(g)"
paulson@23449
   772
  show "O(g) <= f +o O(g)"
paulson@23449
   773
  proof -
paulson@23449
   774
    from a have "-f : O(g)" by auto
paulson@23449
   775
    then have "-f +o O(g) <= O(g)" by (elim bigo_plus_absorb_lemma1)
paulson@23449
   776
    then have "f +o (-f +o O(g)) <= f +o O(g)" by auto
paulson@23449
   777
    also have "f +o (-f +o O(g)) = O(g)"
paulson@23449
   778
      by (simp add: set_plus_rearranges)
paulson@23449
   779
    finally show ?thesis .
paulson@23449
   780
  qed
paulson@23449
   781
qed
paulson@23449
   782
paulson@23449
   783
ML{*ResAtp.problem_name:="BigO__bigo_plus_absorb"*}
paulson@23449
   784
lemma bigo_plus_absorb [simp]: "f : O(g) ==> f +o O(g) = O(g)"
paulson@23449
   785
by (metis bigo_plus_absorb_lemma1 bigo_plus_absorb_lemma2 order_eq_iff);
paulson@23449
   786
paulson@23449
   787
lemma bigo_plus_absorb2 [intro]: "f : O(g) ==> A <= O(g) ==> f +o A <= O(g)"
paulson@23449
   788
  apply (subgoal_tac "f +o A <= f +o O(g)")
paulson@23449
   789
  apply force+
paulson@23449
   790
done
paulson@23449
   791
paulson@23449
   792
lemma bigo_add_commute_imp: "f : g +o O(h) ==> g : f +o O(h)"
paulson@23449
   793
  apply (subst set_minus_plus [symmetric])
paulson@23449
   794
  apply (subgoal_tac "g - f = - (f - g)")
paulson@23449
   795
  apply (erule ssubst)
paulson@23449
   796
  apply (rule bigo_minus)
paulson@23449
   797
  apply (subst set_minus_plus)
paulson@23449
   798
  apply assumption
paulson@23449
   799
  apply  (simp add: diff_minus add_ac)
paulson@23449
   800
done
paulson@23449
   801
paulson@23449
   802
lemma bigo_add_commute: "(f : g +o O(h)) = (g : f +o O(h))"
paulson@23449
   803
  apply (rule iffI)
paulson@23449
   804
  apply (erule bigo_add_commute_imp)+
paulson@23449
   805
done
paulson@23449
   806
paulson@23449
   807
lemma bigo_const1: "(%x. c) : O(%x. 1)"
paulson@23449
   808
by (auto simp add: bigo_def mult_ac)
paulson@23449
   809
paulson@23449
   810
ML{*ResAtp.problem_name:="BigO__bigo_const2"*}
paulson@23449
   811
lemma (*bigo_const2 [intro]:*) "O(%x. c) <= O(%x. 1)"
paulson@23449
   812
by (metis bigo_const1 bigo_elt_subset);
paulson@23449
   813
paulson@24855
   814
lemma bigo_const2 [intro]: "O(%x. c::'b::ordered_idom) <= O(%x. 1)";
paulson@23449
   815
(*??FAILS because the two occurrences of COMBK have different polymorphic types
paulson@23449
   816
proof (neg_clausify)
paulson@23449
   817
assume 0: "\<not> O(COMBK (c\<Colon>'b\<Colon>ordered_idom)) \<subseteq> O(COMBK (1\<Colon>'b\<Colon>ordered_idom))"
paulson@23449
   818
have 1: "COMBK (c\<Colon>'b\<Colon>ordered_idom) \<notin> O(COMBK (1\<Colon>'b\<Colon>ordered_idom))"
paulson@23449
   819
apply (rule notI) 
paulson@23449
   820
apply (rule 0 [THEN notE]) 
paulson@23449
   821
apply (rule bigo_elt_subset) 
paulson@23449
   822
apply assumption; 
paulson@23449
   823
sorry
paulson@23449
   824
  by (metis 0 bigo_elt_subset)  loops??
paulson@23449
   825
show "False"
paulson@23449
   826
  by (metis 1 bigo_const1)
paulson@23449
   827
qed
paulson@23449
   828
*)
paulson@23449
   829
  apply (rule bigo_elt_subset)
paulson@23449
   830
  apply (rule bigo_const1)
paulson@23449
   831
done
paulson@23449
   832
paulson@23449
   833
ML{*ResAtp.problem_name := "BigO__bigo_const3"*}
paulson@23449
   834
lemma bigo_const3: "(c::'a::ordered_field) ~= 0 ==> (%x. 1) : O(%x. c)"
paulson@23449
   835
apply (simp add: bigo_def)
paulson@23449
   836
proof (neg_clausify)
paulson@23449
   837
assume 0: "(c\<Colon>'a\<Colon>ordered_field) \<noteq> (0\<Colon>'a\<Colon>ordered_field)"
paulson@23519
   838
assume 1: "\<And>A\<Colon>'a\<Colon>ordered_field. \<not> (1\<Colon>'a\<Colon>ordered_field) \<le> A * \<bar>c\<Colon>'a\<Colon>ordered_field\<bar>"
paulson@23449
   839
have 2: "(0\<Colon>'a\<Colon>ordered_field) = \<bar>c\<Colon>'a\<Colon>ordered_field\<bar> \<or>
paulson@23449
   840
\<not> (1\<Colon>'a\<Colon>ordered_field) \<le> (1\<Colon>'a\<Colon>ordered_field)"
paulson@23449
   841
  by (metis 1 field_inverse)
paulson@23449
   842
have 3: "\<bar>c\<Colon>'a\<Colon>ordered_field\<bar> = (0\<Colon>'a\<Colon>ordered_field)"
paulson@23519
   843
  by (metis linorder_neq_iff linorder_antisym_conv1 2)
paulson@23449
   844
have 4: "(0\<Colon>'a\<Colon>ordered_field) = (c\<Colon>'a\<Colon>ordered_field)"
paulson@23519
   845
  by (metis 3 abs_eq_0)
paulson@23519
   846
show "False"
paulson@23519
   847
  by (metis 0 4)
paulson@23449
   848
qed
paulson@23449
   849
paulson@23449
   850
lemma bigo_const4: "(c::'a::ordered_field) ~= 0 ==> O(%x. 1) <= O(%x. c)"
paulson@23449
   851
by (rule bigo_elt_subset, rule bigo_const3, assumption)
paulson@23449
   852
paulson@23449
   853
lemma bigo_const [simp]: "(c::'a::ordered_field) ~= 0 ==> 
paulson@23449
   854
    O(%x. c) = O(%x. 1)"
paulson@23449
   855
by (rule equalityI, rule bigo_const2, rule bigo_const4, assumption)
paulson@23449
   856
paulson@23449
   857
ML{*ResAtp.problem_name := "BigO__bigo_const_mult1"*}
paulson@23449
   858
lemma bigo_const_mult1: "(%x. c * f x) : O(f)"
paulson@24937
   859
  apply (simp add: bigo_def abs_mult)
paulson@23449
   860
proof (neg_clausify)
paulson@23449
   861
fix x
haftmann@25304
   862
assume 0: "\<And>xa\<Colon>'b\<Colon>ordered_idom.
haftmann@25304
   863
   \<not> \<bar>c\<Colon>'b\<Colon>ordered_idom\<bar> *
haftmann@25304
   864
     \<bar>(f\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) ((x\<Colon>'b\<Colon>ordered_idom \<Rightarrow> 'a\<Colon>type) xa)\<bar>
haftmann@25304
   865
     \<le> xa * \<bar>f (x xa)\<bar>"
paulson@24937
   866
show "False"
haftmann@25304
   867
  by (metis linorder_neq_iff linorder_antisym_conv1 0)
paulson@23449
   868
qed
paulson@23449
   869
paulson@23449
   870
lemma bigo_const_mult2: "O(%x. c * f x) <= O(f)"
paulson@23449
   871
by (rule bigo_elt_subset, rule bigo_const_mult1)
paulson@23449
   872
paulson@23449
   873
ML{*ResAtp.problem_name := "BigO__bigo_const_mult3"*}
paulson@23449
   874
lemma bigo_const_mult3: "(c::'a::ordered_field) ~= 0 ==> f : O(%x. c * f x)"
paulson@23449
   875
  apply (simp add: bigo_def)
paulson@24942
   876
(*sledgehammer [no luck]*); 
paulson@23449
   877
  apply (rule_tac x = "abs(inverse c)" in exI)
paulson@23449
   878
  apply (simp only: abs_mult [symmetric] mult_assoc [symmetric])
paulson@23449
   879
apply (subst left_inverse) 
paulson@23449
   880
apply (auto ); 
paulson@23449
   881
done
paulson@23449
   882
paulson@23449
   883
lemma bigo_const_mult4: "(c::'a::ordered_field) ~= 0 ==> 
paulson@23449
   884
    O(f) <= O(%x. c * f x)"
paulson@23449
   885
by (rule bigo_elt_subset, rule bigo_const_mult3, assumption)
paulson@23449
   886
paulson@23449
   887
lemma bigo_const_mult [simp]: "(c::'a::ordered_field) ~= 0 ==> 
paulson@23449
   888
    O(%x. c * f x) = O(f)"
paulson@23449
   889
by (rule equalityI, rule bigo_const_mult2, erule bigo_const_mult4)
paulson@23449
   890
paulson@23449
   891
ML{*ResAtp.problem_name := "BigO__bigo_const_mult5"*}
paulson@23449
   892
lemma bigo_const_mult5 [simp]: "(c::'a::ordered_field) ~= 0 ==> 
paulson@23449
   893
    (%x. c) *o O(f) = O(f)"
paulson@23449
   894
  apply (auto del: subsetI)
paulson@23449
   895
  apply (rule order_trans)
paulson@23449
   896
  apply (rule bigo_mult2)
paulson@23449
   897
  apply (simp add: func_times)
paulson@23449
   898
  apply (auto intro!: subsetI simp add: bigo_def elt_set_times_def func_times)
paulson@23449
   899
  apply (rule_tac x = "%y. inverse c * x y" in exI)
paulson@24942
   900
  apply (rename_tac g d) 
paulson@24942
   901
  apply safe
paulson@24942
   902
  apply (rule_tac [2] ext) 
paulson@24942
   903
   prefer 2 
paulson@24942
   904
   apply (metis AC_mult.f_e.left_ident mult_assoc right_inverse)
paulson@24942
   905
  apply (simp add: mult_assoc [symmetric] abs_mult)
paulson@24942
   906
  (*couldn't get this proof without the step above; SLOW*)
paulson@24942
   907
  apply (metis AC_mult.f.assoc abs_ge_zero mult_left_mono)
paulson@23449
   908
done
paulson@23449
   909
paulson@23449
   910
paulson@23449
   911
ML{*ResAtp.problem_name := "BigO__bigo_const_mult6"*}
paulson@23449
   912
lemma bigo_const_mult6 [intro]: "(%x. c) *o O(f) <= O(f)"
paulson@23449
   913
  apply (auto intro!: subsetI
paulson@23449
   914
    simp add: bigo_def elt_set_times_def func_times
paulson@23449
   915
    simp del: abs_mult mult_ac)
paulson@23449
   916
(*sledgehammer*); 
paulson@23449
   917
  apply (rule_tac x = "ca * (abs c)" in exI)
paulson@23449
   918
  apply (rule allI)
paulson@23449
   919
  apply (subgoal_tac "ca * abs(c) * abs(f x) = abs(c) * (ca * abs(f x))")
paulson@23449
   920
  apply (erule ssubst)
paulson@23449
   921
  apply (subst abs_mult)
paulson@23449
   922
  apply (rule mult_left_mono)
paulson@23449
   923
  apply (erule spec)
paulson@23449
   924
  apply simp
paulson@23449
   925
  apply(simp add: mult_ac)
paulson@23449
   926
done
paulson@23449
   927
paulson@23449
   928
lemma bigo_const_mult7 [intro]: "f =o O(g) ==> (%x. c * f x) =o O(g)"
paulson@23449
   929
proof -
paulson@23449
   930
  assume "f =o O(g)"
paulson@23449
   931
  then have "(%x. c) * f =o (%x. c) *o O(g)"
paulson@23449
   932
    by auto
paulson@23449
   933
  also have "(%x. c) * f = (%x. c * f x)"
paulson@23449
   934
    by (simp add: func_times)
paulson@23449
   935
  also have "(%x. c) *o O(g) <= O(g)"
paulson@23449
   936
    by (auto del: subsetI)
paulson@23449
   937
  finally show ?thesis .
paulson@23449
   938
qed
paulson@23449
   939
paulson@23449
   940
lemma bigo_compose1: "f =o O(g) ==> (%x. f(k x)) =o O(%x. g(k x))"
paulson@23449
   941
by (unfold bigo_def, auto)
paulson@23449
   942
paulson@23449
   943
lemma bigo_compose2: "f =o g +o O(h) ==> (%x. f(k x)) =o (%x. g(k x)) +o 
paulson@23449
   944
    O(%x. h(k x))"
paulson@23449
   945
  apply (simp only: set_minus_plus [symmetric] diff_minus func_minus
paulson@23449
   946
      func_plus)
paulson@23449
   947
  apply (erule bigo_compose1)
paulson@23449
   948
done
paulson@23449
   949
paulson@23449
   950
subsection {* Setsum *}
paulson@23449
   951
paulson@23449
   952
lemma bigo_setsum_main: "ALL x. ALL y : A x. 0 <= h x y ==> 
paulson@23449
   953
    EX c. ALL x. ALL y : A x. abs(f x y) <= c * (h x y) ==>
paulson@23449
   954
      (%x. SUM y : A x. f x y) =o O(%x. SUM y : A x. h x y)"  
paulson@23449
   955
  apply (auto simp add: bigo_def)
paulson@23449
   956
  apply (rule_tac x = "abs c" in exI)
paulson@23449
   957
  apply (subst abs_of_nonneg) back back
paulson@23449
   958
  apply (rule setsum_nonneg)
paulson@23449
   959
  apply force
paulson@23449
   960
  apply (subst setsum_right_distrib)
paulson@23449
   961
  apply (rule allI)
paulson@23449
   962
  apply (rule order_trans)
paulson@23449
   963
  apply (rule setsum_abs)
paulson@23449
   964
  apply (rule setsum_mono)
paulson@23449
   965
apply (blast intro: order_trans mult_right_mono abs_ge_self) 
paulson@23449
   966
done
paulson@23449
   967
paulson@23449
   968
ML{*ResAtp.problem_name := "BigO__bigo_setsum1"*}
paulson@23449
   969
lemma bigo_setsum1: "ALL x y. 0 <= h x y ==> 
paulson@23449
   970
    EX c. ALL x y. abs(f x y) <= c * (h x y) ==>
paulson@23449
   971
      (%x. SUM y : A x. f x y) =o O(%x. SUM y : A x. h x y)"
paulson@23449
   972
  apply (rule bigo_setsum_main)
paulson@23449
   973
(*sledgehammer*); 
paulson@23449
   974
  apply force
paulson@23449
   975
  apply clarsimp
paulson@23449
   976
  apply (rule_tac x = c in exI)
paulson@23449
   977
  apply force
paulson@23449
   978
done
paulson@23449
   979
paulson@23449
   980
lemma bigo_setsum2: "ALL y. 0 <= h y ==> 
paulson@23449
   981
    EX c. ALL y. abs(f y) <= c * (h y) ==>
paulson@23449
   982
      (%x. SUM y : A x. f y) =o O(%x. SUM y : A x. h y)"
paulson@23449
   983
by (rule bigo_setsum1, auto)  
paulson@23449
   984
paulson@23449
   985
ML{*ResAtp.problem_name := "BigO__bigo_setsum3"*}
paulson@23449
   986
lemma bigo_setsum3: "f =o O(h) ==>
paulson@23449
   987
    (%x. SUM y : A x. (l x y) * f(k x y)) =o
paulson@23449
   988
      O(%x. SUM y : A x. abs(l x y * h(k x y)))"
paulson@23449
   989
  apply (rule bigo_setsum1)
paulson@23449
   990
  apply (rule allI)+
paulson@23449
   991
  apply (rule abs_ge_zero)
paulson@23449
   992
  apply (unfold bigo_def)
paulson@23449
   993
  apply (auto simp add: abs_mult);
paulson@23449
   994
(*sledgehammer*); 
paulson@23449
   995
  apply (rule_tac x = c in exI)
paulson@23449
   996
  apply (rule allI)+
paulson@23449
   997
  apply (subst mult_left_commute)
paulson@23449
   998
  apply (rule mult_left_mono)
paulson@23449
   999
  apply (erule spec)
paulson@23449
  1000
  apply (rule abs_ge_zero)
paulson@23449
  1001
done
paulson@23449
  1002
paulson@23449
  1003
lemma bigo_setsum4: "f =o g +o O(h) ==>
paulson@23449
  1004
    (%x. SUM y : A x. l x y * f(k x y)) =o
paulson@23449
  1005
      (%x. SUM y : A x. l x y * g(k x y)) +o
paulson@23449
  1006
        O(%x. SUM y : A x. abs(l x y * h(k x y)))"
paulson@23449
  1007
  apply (rule set_minus_imp_plus)
paulson@23449
  1008
  apply (subst func_diff)
paulson@23449
  1009
  apply (subst setsum_subtractf [symmetric])
paulson@23449
  1010
  apply (subst right_diff_distrib [symmetric])
paulson@23449
  1011
  apply (rule bigo_setsum3)
paulson@23449
  1012
  apply (subst func_diff [symmetric])
paulson@23449
  1013
  apply (erule set_plus_imp_minus)
paulson@23449
  1014
done
paulson@23449
  1015
paulson@23449
  1016
ML{*ResAtp.problem_name := "BigO__bigo_setsum5"*}
paulson@23449
  1017
lemma bigo_setsum5: "f =o O(h) ==> ALL x y. 0 <= l x y ==> 
paulson@23449
  1018
    ALL x. 0 <= h x ==>
paulson@23449
  1019
      (%x. SUM y : A x. (l x y) * f(k x y)) =o
paulson@23449
  1020
        O(%x. SUM y : A x. (l x y) * h(k x y))" 
paulson@23449
  1021
  apply (subgoal_tac "(%x. SUM y : A x. (l x y) * h(k x y)) = 
paulson@23449
  1022
      (%x. SUM y : A x. abs((l x y) * h(k x y)))")
paulson@23449
  1023
  apply (erule ssubst)
paulson@23449
  1024
  apply (erule bigo_setsum3)
paulson@23449
  1025
  apply (rule ext)
paulson@23449
  1026
  apply (rule setsum_cong2)
paulson@23449
  1027
  apply (thin_tac "f \<in> O(h)") 
paulson@24942
  1028
apply (metis abs_of_nonneg zero_le_mult_iff)
paulson@23449
  1029
done
paulson@23449
  1030
paulson@23449
  1031
lemma bigo_setsum6: "f =o g +o O(h) ==> ALL x y. 0 <= l x y ==>
paulson@23449
  1032
    ALL x. 0 <= h x ==>
paulson@23449
  1033
      (%x. SUM y : A x. (l x y) * f(k x y)) =o
paulson@23449
  1034
        (%x. SUM y : A x. (l x y) * g(k x y)) +o
paulson@23449
  1035
          O(%x. SUM y : A x. (l x y) * h(k x y))" 
paulson@23449
  1036
  apply (rule set_minus_imp_plus)
paulson@23449
  1037
  apply (subst func_diff)
paulson@23449
  1038
  apply (subst setsum_subtractf [symmetric])
paulson@23449
  1039
  apply (subst right_diff_distrib [symmetric])
paulson@23449
  1040
  apply (rule bigo_setsum5)
paulson@23449
  1041
  apply (subst func_diff [symmetric])
paulson@23449
  1042
  apply (drule set_plus_imp_minus)
paulson@23449
  1043
  apply auto
paulson@23449
  1044
done
paulson@23449
  1045
paulson@23449
  1046
subsection {* Misc useful stuff *}
paulson@23449
  1047
paulson@23449
  1048
lemma bigo_useful_intro: "A <= O(f) ==> B <= O(f) ==>
paulson@23449
  1049
  A + B <= O(f)"
paulson@23449
  1050
  apply (subst bigo_plus_idemp [symmetric])
paulson@23449
  1051
  apply (rule set_plus_mono2)
paulson@23449
  1052
  apply assumption+
paulson@23449
  1053
done
paulson@23449
  1054
paulson@23449
  1055
lemma bigo_useful_add: "f =o O(h) ==> g =o O(h) ==> f + g =o O(h)"
paulson@23449
  1056
  apply (subst bigo_plus_idemp [symmetric])
paulson@23449
  1057
  apply (rule set_plus_intro)
paulson@23449
  1058
  apply assumption+
paulson@23449
  1059
done
paulson@23449
  1060
  
paulson@23449
  1061
lemma bigo_useful_const_mult: "(c::'a::ordered_field) ~= 0 ==> 
paulson@23449
  1062
    (%x. c) * f =o O(h) ==> f =o O(h)"
paulson@23449
  1063
  apply (rule subsetD)
paulson@23449
  1064
  apply (subgoal_tac "(%x. 1 / c) *o O(h) <= O(h)")
paulson@23449
  1065
  apply assumption
paulson@23449
  1066
  apply (rule bigo_const_mult6)
paulson@23449
  1067
  apply (subgoal_tac "f = (%x. 1 / c) * ((%x. c) * f)")
paulson@23449
  1068
  apply (erule ssubst)
paulson@23449
  1069
  apply (erule set_times_intro2)
paulson@23449
  1070
  apply (simp add: func_times) 
paulson@23449
  1071
done
paulson@23449
  1072
paulson@23449
  1073
ML{*ResAtp.problem_name := "BigO__bigo_fix"*}
paulson@23449
  1074
lemma bigo_fix: "(%x. f ((x::nat) + 1)) =o O(%x. h(x + 1)) ==> f 0 = 0 ==>
paulson@23449
  1075
    f =o O(h)"
paulson@23449
  1076
  apply (simp add: bigo_alt_def)
paulson@23449
  1077
(*sledgehammer*); 
paulson@23449
  1078
  apply clarify
paulson@23449
  1079
  apply (rule_tac x = c in exI)
paulson@23449
  1080
  apply safe
paulson@23449
  1081
  apply (case_tac "x = 0")
paulson@23816
  1082
apply (metis OrderedGroup.abs_ge_zero  OrderedGroup.abs_zero  order_less_le  Ring_and_Field.split_mult_pos_le) 
paulson@23449
  1083
  apply (subgoal_tac "x = Suc (x - 1)")
paulson@23816
  1084
  apply metis
paulson@23449
  1085
  apply simp
paulson@23449
  1086
  done
paulson@23449
  1087
paulson@23449
  1088
paulson@23449
  1089
lemma bigo_fix2: 
paulson@23449
  1090
    "(%x. f ((x::nat) + 1)) =o (%x. g(x + 1)) +o O(%x. h(x + 1)) ==> 
paulson@23449
  1091
       f 0 = g 0 ==> f =o g +o O(h)"
paulson@23449
  1092
  apply (rule set_minus_imp_plus)
paulson@23449
  1093
  apply (rule bigo_fix)
paulson@23449
  1094
  apply (subst func_diff)
paulson@23449
  1095
  apply (subst func_diff [symmetric])
paulson@23449
  1096
  apply (rule set_plus_imp_minus)
paulson@23449
  1097
  apply simp
paulson@23449
  1098
  apply (simp add: func_diff)
paulson@23449
  1099
done
paulson@23449
  1100
paulson@23449
  1101
subsection {* Less than or equal to *}
paulson@23449
  1102
paulson@23449
  1103
constdefs 
paulson@23449
  1104
  lesso :: "('a => 'b::ordered_idom) => ('a => 'b) => ('a => 'b)"
paulson@23449
  1105
      (infixl "<o" 70)
paulson@23449
  1106
  "f <o g == (%x. max (f x - g x) 0)"
paulson@23449
  1107
paulson@23449
  1108
lemma bigo_lesseq1: "f =o O(h) ==> ALL x. abs (g x) <= abs (f x) ==>
paulson@23449
  1109
    g =o O(h)"
paulson@23449
  1110
  apply (unfold bigo_def)
paulson@23449
  1111
  apply clarsimp
paulson@23449
  1112
apply (blast intro: order_trans) 
paulson@23449
  1113
done
paulson@23449
  1114
paulson@23449
  1115
lemma bigo_lesseq2: "f =o O(h) ==> ALL x. abs (g x) <= f x ==>
paulson@23449
  1116
      g =o O(h)"
paulson@23449
  1117
  apply (erule bigo_lesseq1)
paulson@23449
  1118
apply (blast intro: abs_ge_self order_trans) 
paulson@23449
  1119
done
paulson@23449
  1120
paulson@23449
  1121
lemma bigo_lesseq3: "f =o O(h) ==> ALL x. 0 <= g x ==> ALL x. g x <= f x ==>
paulson@23449
  1122
      g =o O(h)"
paulson@23449
  1123
  apply (erule bigo_lesseq2)
paulson@23449
  1124
  apply (rule allI)
paulson@23449
  1125
  apply (subst abs_of_nonneg)
paulson@23449
  1126
  apply (erule spec)+
paulson@23449
  1127
done
paulson@23449
  1128
paulson@23449
  1129
lemma bigo_lesseq4: "f =o O(h) ==>
paulson@23449
  1130
    ALL x. 0 <= g x ==> ALL x. g x <= abs (f x) ==>
paulson@23449
  1131
      g =o O(h)"
paulson@23449
  1132
  apply (erule bigo_lesseq1)
paulson@23449
  1133
  apply (rule allI)
paulson@23449
  1134
  apply (subst abs_of_nonneg)
paulson@23449
  1135
  apply (erule spec)+
paulson@23449
  1136
done
paulson@23449
  1137
paulson@23449
  1138
ML{*ResAtp.problem_name:="BigO__bigo_lesso1"*}
paulson@23449
  1139
lemma bigo_lesso1: "ALL x. f x <= g x ==> f <o g =o O(h)"
paulson@23449
  1140
  apply (unfold lesso_def)
paulson@23449
  1141
  apply (subgoal_tac "(%x. max (f x - g x) 0) = 0")
paulson@24937
  1142
(*??Translation of TSTP raised an exception: Type unification failed: Variable ?'X2.0::type not of sort ord*)
haftmann@25082
  1143
apply (metis bigo_zero)
paulson@23449
  1144
  apply (unfold func_zero)
paulson@23449
  1145
  apply (rule ext)
paulson@23449
  1146
  apply (simp split: split_max)
paulson@23449
  1147
done
paulson@23449
  1148
paulson@23449
  1149
paulson@23449
  1150
ML{*ResAtp.problem_name := "BigO__bigo_lesso2"*}
paulson@23449
  1151
lemma bigo_lesso2: "f =o g +o O(h) ==>
paulson@23449
  1152
    ALL x. 0 <= k x ==> ALL x. k x <= f x ==>
paulson@23449
  1153
      k <o g =o O(h)"
paulson@23449
  1154
  apply (unfold lesso_def)
paulson@23449
  1155
  apply (rule bigo_lesseq4)
paulson@23449
  1156
  apply (erule set_plus_imp_minus)
paulson@23449
  1157
  apply (rule allI)
paulson@23449
  1158
  apply (rule le_maxI2)
paulson@23449
  1159
  apply (rule allI)
paulson@23449
  1160
  apply (subst func_diff)
paulson@23449
  1161
apply (erule thin_rl)
paulson@23449
  1162
(*sledgehammer*);  
paulson@23449
  1163
  apply (case_tac "0 <= k x - g x")
paulson@24545
  1164
  prefer 2 (*re-order subgoals because I don't know what to put after a structured proof*)
paulson@24545
  1165
   apply (metis abs_ge_zero abs_minus_commute linorder_linear min_max.less_eq_less_sup.sup_absorb1 min_max.less_eq_less_sup.sup_commute)
paulson@24545
  1166
proof (neg_clausify)
paulson@24545
  1167
fix x
paulson@24545
  1168
assume 0: "\<And>A. k A \<le> f A"
paulson@24545
  1169
have 1: "\<And>(X1\<Colon>'b\<Colon>ordered_idom) X2. \<not> max X1 X2 < X1"
paulson@24545
  1170
  by (metis linorder_not_less le_maxI1)  (*sort inserted by hand*)
paulson@24545
  1171
assume 2: "(0\<Colon>'b) \<le> k x - g x"
paulson@24545
  1172
have 3: "\<not> k x - g x < (0\<Colon>'b)"
paulson@24545
  1173
  by (metis 2 linorder_not_less)
paulson@24545
  1174
have 4: "\<And>X1 X2. min X1 (k X2) \<le> f X2"
paulson@24545
  1175
  by (metis min_max.less_eq_less_inf.inf_le2 min_max.less_eq_less_inf.le_inf_iff min_max.less_eq_less_inf.le_iff_inf 0)
paulson@24545
  1176
have 5: "\<bar>g x - f x\<bar> = f x - g x"
paulson@24545
  1177
  by (metis abs_minus_commute combine_common_factor mult_zero_right minus_add_cancel minus_zero abs_if diff_less_eq min_max.less_eq_less_inf.inf_commute 4 linorder_not_le min_max.less_eq_less_inf.le_iff_inf 3 diff_less_0_iff_less linorder_not_less)
paulson@24545
  1178
have 6: "max (0\<Colon>'b) (k x - g x) = k x - g x"
paulson@24545
  1179
  by (metis min_max.less_eq_less_sup.le_iff_sup 2)
paulson@24545
  1180
assume 7: "\<not> max (k x - g x) (0\<Colon>'b) \<le> \<bar>f x - g x\<bar>"
paulson@24545
  1181
have 8: "\<not> k x - g x \<le> f x - g x"
paulson@24545
  1182
  by (metis 5 abs_minus_commute 7 min_max.less_eq_less_sup.sup_commute 6)
paulson@24545
  1183
show "False"
paulson@24545
  1184
  by (metis min_max.less_eq_less_sup.sup_commute min_max.less_eq_less_inf.inf_commute min_max.less_eq_less_inf_sup.sup_inf_absorb min_max.less_eq_less_inf.le_iff_inf 0 max_diff_distrib_left 1 linorder_not_le 8)
paulson@24545
  1185
qed
paulson@23449
  1186
paulson@23449
  1187
ML{*ResAtp.problem_name := "BigO__bigo_lesso3"*}
paulson@23449
  1188
lemma bigo_lesso3: "f =o g +o O(h) ==>
paulson@23449
  1189
    ALL x. 0 <= k x ==> ALL x. g x <= k x ==>
paulson@23449
  1190
      f <o k =o O(h)"
paulson@23449
  1191
  apply (unfold lesso_def)
paulson@23449
  1192
  apply (rule bigo_lesseq4)
paulson@23449
  1193
  apply (erule set_plus_imp_minus)
paulson@23449
  1194
  apply (rule allI)
paulson@23449
  1195
  apply (rule le_maxI2)
paulson@23449
  1196
  apply (rule allI)
paulson@23449
  1197
  apply (subst func_diff)
paulson@23449
  1198
apply (erule thin_rl) 
paulson@23449
  1199
(*sledgehammer*); 
paulson@23449
  1200
  apply (case_tac "0 <= f x - k x")
paulson@23449
  1201
  apply (simp del: compare_rls diff_minus);
paulson@23449
  1202
  apply (subst abs_of_nonneg)
paulson@23449
  1203
  apply (drule_tac x = x in spec) back
paulson@23449
  1204
ML{*ResAtp.problem_name := "BigO__bigo_lesso3_simpler"*}
paulson@24545
  1205
apply (metis diff_less_0_iff_less linorder_not_le not_leE uminus_add_conv_diff xt1(12) xt1(6))
paulson@24545
  1206
apply (metis add_minus_cancel diff_le_eq le_diff_eq uminus_add_conv_diff)
paulson@24545
  1207
apply (metis abs_ge_zero linorder_linear min_max.less_eq_less_sup.sup_absorb1 min_max.less_eq_less_sup.sup_commute)
paulson@23449
  1208
done
paulson@23449
  1209
paulson@23449
  1210
lemma bigo_lesso4: "f <o g =o O(k::'a=>'b::ordered_field) ==>
paulson@23449
  1211
    g =o h +o O(k) ==> f <o h =o O(k)"
paulson@23449
  1212
  apply (unfold lesso_def)
paulson@23449
  1213
  apply (drule set_plus_imp_minus)
paulson@23449
  1214
  apply (drule bigo_abs5) back
paulson@23449
  1215
  apply (simp add: func_diff)
paulson@23449
  1216
  apply (drule bigo_useful_add)
paulson@23449
  1217
  apply assumption
paulson@23449
  1218
  apply (erule bigo_lesseq2) back
paulson@23449
  1219
  apply (rule allI)
paulson@23449
  1220
  apply (auto simp add: func_plus func_diff compare_rls 
paulson@23449
  1221
    split: split_max abs_split)
paulson@23449
  1222
done
paulson@23449
  1223
paulson@23449
  1224
ML{*ResAtp.problem_name := "BigO__bigo_lesso5"*}
paulson@23449
  1225
lemma bigo_lesso5: "f <o g =o O(h) ==>
paulson@23449
  1226
    EX C. ALL x. f x <= g x + C * abs(h x)"
paulson@23449
  1227
  apply (simp only: lesso_def bigo_alt_def)
paulson@23449
  1228
  apply clarsimp
paulson@24855
  1229
  apply (metis abs_if abs_mult add_commute diff_le_eq less_not_permute)  
paulson@23449
  1230
done
paulson@23449
  1231
paulson@23449
  1232
end