src/HOL/MetisExamples/BigO.thy
author paulson
Thu Sep 06 16:54:03 2007 +0200 (2007-09-06)
changeset 24545 f406a5744756
parent 23816 3879cb3d0ba7
child 24855 161eb8381b49
permissions -rw-r--r--
new proofs found
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
paulson@23449
    11
imports 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)
paulson@23449
    28
  apply (rule_tac x = "abs c" in exI, auto);
paulson@23449
    29
txt{*Version 1: one-shot proof. MUCH SLOWER with types: 24 versus 6.7 seconds*}
paulson@23449
    30
  apply (metis abs_ge_minus_self abs_ge_zero abs_minus_cancel abs_of_nonneg equation_minus_iff Orderings.xt1(6) abs_le_mult)
paulson@23449
    31
  done
paulson@23449
    32
paulson@23449
    33
(*** Now various verions with an increasing modulus ***)
paulson@23449
    34
paulson@23449
    35
ML{*ResReconstruct.modulus := 1*}
paulson@23449
    36
paulson@23449
    37
lemma bigo_pos_const: "(EX (c::'a::ordered_idom). 
paulson@23449
    38
    ALL x. (abs (h x)) <= (c * (abs (f x))))
paulson@23449
    39
      = (EX c. 0 < c & (ALL x. (abs(h x)) <= (c * (abs (f x)))))"
paulson@23449
    40
  apply auto
paulson@23449
    41
  apply (case_tac "c = 0", simp)
paulson@23449
    42
  apply (rule_tac x = "1" in exI, simp)
paulson@23449
    43
  apply (rule_tac x = "abs c" in exI, auto)
paulson@23449
    44
(*hand-modified to give 'a sort ordered_idom and X3 type 'a*)
paulson@23449
    45
proof (neg_clausify)
paulson@23449
    46
fix c x
paulson@23449
    47
assume 0: "\<And>A. \<bar>h A\<bar> \<le> c * \<bar>f A\<bar>"
paulson@23449
    48
assume 1: "c \<noteq> (0\<Colon>'a::ordered_idom)"
paulson@23449
    49
assume 2: "\<not> \<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>"
paulson@23449
    50
have 3: "\<And>X1 X3. \<bar>h X3\<bar> < X1 \<or> \<not> c * \<bar>f X3\<bar> < X1"
paulson@23449
    51
  by (metis order_le_less_trans 0)
paulson@23449
    52
have 4: "\<And>X3. (1\<Colon>'a) * X3 \<le> X3 \<or> \<not> (1\<Colon>'a) \<le> (1\<Colon>'a)"
paulson@23449
    53
  by (metis mult_le_cancel_right2 order_refl)
paulson@23449
    54
have 5: "\<And>X3. (1\<Colon>'a) * X3 \<le> X3"
paulson@23449
    55
  by (metis 4 order_refl)
paulson@23449
    56
have 6: "\<And>X3. \<bar>0\<Colon>'a\<bar> = \<bar>X3\<bar> * (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> (0\<Colon>'a)"
paulson@23449
    57
  by (metis abs_mult_pos mult_cancel_right1)
paulson@23449
    58
have 7: "\<bar>0\<Colon>'a\<bar> = (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> (0\<Colon>'a)"
paulson@23449
    59
  by (metis 6 mult_cancel_right1)
paulson@23449
    60
have 8: "\<bar>0\<Colon>'a\<bar> = (0\<Colon>'a)"
paulson@23449
    61
  by (metis 7 order_refl)
paulson@23449
    62
have 9: "\<not> (0\<Colon>'a) < (0\<Colon>'a)"
paulson@23449
    63
  by (metis abs_not_less_zero 8)
paulson@23449
    64
have 10: "\<bar>(1\<Colon>'a) * (0\<Colon>'a)\<bar> = - ((1\<Colon>'a) * (0\<Colon>'a))"
paulson@23449
    65
  by (metis abs_of_nonpos 5)
paulson@23449
    66
have 11: "(0\<Colon>'a) = - ((1\<Colon>'a) * (0\<Colon>'a))"
paulson@23449
    67
  by (metis 10 mult_cancel_right1 8)
paulson@23449
    68
have 12: "(0\<Colon>'a) = - (0\<Colon>'a)"
paulson@23449
    69
  by (metis 11 mult_cancel_right1)
paulson@23449
    70
have 13: "\<And>X3. \<bar>X3\<bar> = X3 \<or> X3 \<le> (0\<Colon>'a)"
paulson@23449
    71
  by (metis abs_of_nonneg linorder_linear)
paulson@23449
    72
have 14: "c \<le> (0\<Colon>'a) \<or> \<not> \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>"
paulson@23449
    73
  by (metis 2 13)
paulson@23449
    74
have 15: "c \<le> (0\<Colon>'a)"
paulson@23449
    75
  by (metis 14 0)
paulson@23449
    76
have 16: "c = (0\<Colon>'a) \<or> c < (0\<Colon>'a)"
paulson@23449
    77
  by (metis linorder_antisym_conv2 15)
paulson@23449
    78
have 17: "\<bar>c\<bar> = - c"
paulson@23449
    79
  by (metis abs_of_nonpos 15)
paulson@23449
    80
have 18: "c < (0\<Colon>'a)"
paulson@23449
    81
  by (metis 16 1)
paulson@23449
    82
have 19: "\<not> \<bar>h x\<bar> \<le> - c * \<bar>f x\<bar>"
paulson@23449
    83
  by (metis 2 17)
paulson@23449
    84
have 20: "\<And>X3. X3 * (1\<Colon>'a) = X3"
paulson@23449
    85
  by (metis mult_cancel_right1 AC_mult.f.commute)
paulson@23449
    86
have 21: "\<And>X3. (0\<Colon>'a) \<le> X3 * X3"
paulson@23449
    87
  by (metis zero_le_square AC_mult.f.commute)
paulson@23449
    88
have 22: "(0\<Colon>'a) \<le> (1\<Colon>'a)"
paulson@23449
    89
  by (metis 21 mult_cancel_left1)
paulson@23449
    90
have 23: "\<And>X3. (0\<Colon>'a) = X3 \<or> (0\<Colon>'a) \<noteq> - X3"
paulson@23449
    91
  by (metis neg_equal_iff_equal 12)
paulson@23449
    92
have 24: "\<And>X3. (0\<Colon>'a) = - X3 \<or> X3 \<noteq> (0\<Colon>'a)"
paulson@23449
    93
  by (metis 23 minus_equation_iff)
paulson@23449
    94
have 25: "\<And>X3. \<bar>0\<Colon>'a\<bar> = \<bar>X3\<bar> \<or> X3 \<noteq> (0\<Colon>'a)"
paulson@23449
    95
  by (metis abs_minus_cancel 24)
paulson@23449
    96
have 26: "\<And>X3. (0\<Colon>'a) = \<bar>X3\<bar> \<or> X3 \<noteq> (0\<Colon>'a)"
paulson@23449
    97
  by (metis 25 8)
paulson@23449
    98
have 27: "\<And>X1 X3. (0\<Colon>'a) * \<bar>X1\<bar> = \<bar>X3 * X1\<bar> \<or> X3 \<noteq> (0\<Colon>'a)"
paulson@23449
    99
  by (metis abs_mult 26)
paulson@23449
   100
have 28: "\<And>X1 X3. (0\<Colon>'a) = \<bar>X3 * X1\<bar> \<or> X3 \<noteq> (0\<Colon>'a)"
paulson@23449
   101
  by (metis 27 mult_cancel_left1)
paulson@23449
   102
have 29: "\<And>X1 X3. (0\<Colon>'a) = X3 * X1 \<or> (0\<Colon>'a) < (0\<Colon>'a) \<or> X3 \<noteq> (0\<Colon>'a)"
paulson@23449
   103
  by (metis zero_less_abs_iff 28)
paulson@23449
   104
have 30: "\<And>X1 X3. X3 * X1 = (0\<Colon>'a) \<or> X3 \<noteq> (0\<Colon>'a)"
paulson@23449
   105
  by (metis 29 9)
paulson@23449
   106
have 31: "\<And>X1 X3. (0\<Colon>'a) = X1 * X3 \<or> X3 \<noteq> (0\<Colon>'a)"
paulson@23449
   107
  by (metis AC_mult.f.commute 30)
paulson@23449
   108
have 32: "\<And>X1 X3. (0\<Colon>'a) = \<bar>X3 * X1\<bar> \<or> \<bar>X1\<bar> \<noteq> (0\<Colon>'a)"
paulson@23449
   109
  by (metis abs_mult 31)
paulson@23449
   110
have 33: "\<And>X3::'a. \<bar>X3 * X3\<bar> = X3 * X3"
paulson@23449
   111
  by (metis abs_mult_self abs_mult AC_mult.f.commute)
paulson@23449
   112
have 34: "\<And>X3. (0\<Colon>'a) \<le> \<bar>X3\<bar> \<or> \<not> (0\<Colon>'a) \<le> (1\<Colon>'a)"
paulson@23449
   113
  by (metis abs_ge_zero abs_mult_pos 20)
paulson@23449
   114
have 35: "\<And>X3. (0\<Colon>'a) \<le> \<bar>X3\<bar>"
paulson@23449
   115
  by (metis 34 22)
paulson@23449
   116
have 36: "\<And>X3. X3 * (1\<Colon>'a) = (0\<Colon>'a) \<or> \<bar>X3\<bar> \<noteq> (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> (1\<Colon>'a)"
paulson@23449
   117
  by (metis abs_eq_0 abs_mult_pos 20)
paulson@23449
   118
have 37: "\<And>X3. X3 = (0\<Colon>'a) \<or> \<bar>X3\<bar> \<noteq> (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> (1\<Colon>'a)"
paulson@23449
   119
  by (metis 36 20)
paulson@23449
   120
have 38: "\<And>X3. X3 = (0\<Colon>'a) \<or> \<bar>X3\<bar> \<noteq> (0\<Colon>'a)"
paulson@23449
   121
  by (metis 37 22)
paulson@23449
   122
have 39: "\<And>X1 X3. X3 * X1 = (0\<Colon>'a) \<or> \<bar>X1\<bar> \<noteq> (0\<Colon>'a)"
paulson@23449
   123
  by (metis 38 32)
paulson@23449
   124
have 40: "\<And>X3::'a. \<bar>\<bar>X3\<bar>\<bar> = \<bar>X3\<bar> \<or> \<not> (0\<Colon>'a) \<le> (1\<Colon>'a)"
paulson@23449
   125
  by (metis abs_idempotent abs_mult_pos 20)
paulson@23449
   126
have 41: "\<And>X3::'a. \<bar>\<bar>X3\<bar>\<bar> = \<bar>X3\<bar>"
paulson@23449
   127
  by (metis 40 22)
paulson@23449
   128
have 42: "\<And>X3. \<not> \<bar>X3\<bar> < (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> (1\<Colon>'a)"
paulson@23449
   129
  by (metis abs_not_less_zero abs_mult_pos 20)
paulson@23449
   130
have 43: "\<And>X3. \<not> \<bar>X3\<bar> < (0\<Colon>'a)"
paulson@23449
   131
  by (metis 42 22)
paulson@23449
   132
have 44: "\<And>X3. X3 * (1\<Colon>'a) = (0\<Colon>'a) \<or> \<not> \<bar>X3\<bar> \<le> (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> (1\<Colon>'a)"
paulson@23449
   133
  by (metis abs_le_zero_iff abs_mult_pos 20)
paulson@23449
   134
have 45: "\<And>X3. X3 = (0\<Colon>'a) \<or> \<not> \<bar>X3\<bar> \<le> (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> (1\<Colon>'a)"
paulson@23449
   135
  by (metis 44 20)
paulson@23449
   136
have 46: "\<And>X3. X3 = (0\<Colon>'a) \<or> \<not> \<bar>X3\<bar> \<le> (0\<Colon>'a)"
paulson@23449
   137
  by (metis 45 22)
paulson@23449
   138
have 47: "\<And>X3. X3 * X3 = (0\<Colon>'a) \<or> \<not> X3 * X3 \<le> (0\<Colon>'a)"
paulson@23449
   139
  by (metis 46 33)
paulson@23449
   140
have 48: "\<And>X3. X3 * X3 = (0\<Colon>'a) \<or> \<not> X3 \<le> (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> X3"
paulson@23449
   141
  by (metis 47 mult_le_0_iff)
paulson@23449
   142
have 49: "\<And>X3. \<bar>X3\<bar> = (0\<Colon>'a) \<or> \<not> X3 \<le> (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> X3"
paulson@23449
   143
  by (metis mult_eq_0_iff abs_mult_self 48)
paulson@23449
   144
have 50: "\<And>X1 X3.
paulson@23449
   145
   (0\<Colon>'a) * \<bar>X1\<bar> = \<bar>\<bar>X3 * X1\<bar>\<bar> \<or>
paulson@23449
   146
   \<not> (0\<Colon>'a) \<le> \<bar>X1\<bar> \<or> \<not> \<bar>X3\<bar> \<le> (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> \<bar>X3\<bar>"
paulson@23449
   147
  by (metis abs_mult_pos abs_mult 49)
paulson@23449
   148
have 51: "\<And>X1 X3. X3 * X1 = (0\<Colon>'a) \<or> \<not> X1 \<le> (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> X1"
paulson@23449
   149
  by (metis 39 49)
paulson@23449
   150
have 52: "\<And>X1 X3.
paulson@23449
   151
   (0\<Colon>'a) = \<bar>\<bar>X3 * X1\<bar>\<bar> \<or>
paulson@23449
   152
   \<not> (0\<Colon>'a) \<le> \<bar>X1\<bar> \<or> \<not> \<bar>X3\<bar> \<le> (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> \<bar>X3\<bar>"
paulson@23449
   153
  by (metis 50 mult_cancel_left1)
paulson@23449
   154
have 53: "\<And>X1 X3.
paulson@23449
   155
   (0\<Colon>'a) = \<bar>X3 * X1\<bar> \<or> \<not> (0\<Colon>'a) \<le> \<bar>X1\<bar> \<or> \<not> \<bar>X3\<bar> \<le> (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> \<bar>X3\<bar>"
paulson@23449
   156
  by (metis 52 41)
paulson@23449
   157
have 54: "\<And>X1 X3. (0\<Colon>'a) = \<bar>X3 * X1\<bar> \<or> \<not> \<bar>X3\<bar> \<le> (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> \<bar>X3\<bar>"
paulson@23449
   158
  by (metis 53 35)
paulson@23449
   159
have 55: "\<And>X1 X3. (0\<Colon>'a) = \<bar>X3 * X1\<bar> \<or> \<not> \<bar>X3\<bar> \<le> (0\<Colon>'a)"
paulson@23449
   160
  by (metis 54 35)
paulson@23449
   161
have 56: "\<And>X1 X3. \<bar>X1 * X3\<bar> = (0\<Colon>'a) \<or> \<not> \<bar>X3\<bar> \<le> (0\<Colon>'a)"
paulson@23449
   162
  by (metis 55 AC_mult.f.commute)
paulson@23449
   163
have 57: "\<And>X1 X3. X3 * X1 = (0\<Colon>'a) \<or> \<not> \<bar>X1\<bar> \<le> (0\<Colon>'a)"
paulson@23449
   164
  by (metis 38 56)
paulson@23449
   165
have 58: "\<And>X3. \<bar>h X3\<bar> \<le> (0\<Colon>'a) \<or> \<not> \<bar>f X3\<bar> \<le> (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> \<bar>f X3\<bar>"
paulson@23449
   166
  by (metis 0 51)
paulson@23449
   167
have 59: "\<And>X3. \<bar>h X3\<bar> \<le> (0\<Colon>'a) \<or> \<not> \<bar>f X3\<bar> \<le> (0\<Colon>'a)"
paulson@23449
   168
  by (metis 58 35)
paulson@23449
   169
have 60: "\<And>X3. \<bar>h X3\<bar> \<le> (0\<Colon>'a) \<or> (0\<Colon>'a) < \<bar>f X3\<bar>"
paulson@23449
   170
  by (metis 59 linorder_not_le)
paulson@23449
   171
have 61: "\<And>X1 X3. X3 * X1 = (0\<Colon>'a) \<or> (0\<Colon>'a) < \<bar>X1\<bar>"
paulson@23449
   172
  by (metis 57 linorder_not_le)
paulson@23449
   173
have 62: "(0\<Colon>'a) < \<bar>\<bar>f x\<bar>\<bar> \<or> \<not> \<bar>h x\<bar> \<le> (0\<Colon>'a)"
paulson@23449
   174
  by (metis 19 61)
paulson@23449
   175
have 63: "(0\<Colon>'a) < \<bar>f x\<bar> \<or> \<not> \<bar>h x\<bar> \<le> (0\<Colon>'a)"
paulson@23449
   176
  by (metis 62 41)
paulson@23449
   177
have 64: "(0\<Colon>'a) < \<bar>f x\<bar>"
paulson@23449
   178
  by (metis 63 60)
paulson@23449
   179
have 65: "\<And>X3. \<bar>h X3\<bar> < (0\<Colon>'a) \<or> \<not> c < (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) < \<bar>f X3\<bar>"
paulson@23449
   180
  by (metis 3 mult_less_0_iff)
paulson@23449
   181
have 66: "\<And>X3. \<bar>h X3\<bar> < (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) < \<bar>f X3\<bar>"
paulson@23449
   182
  by (metis 65 18)
paulson@23449
   183
have 67: "\<And>X3. \<not> (0\<Colon>'a) < \<bar>f X3\<bar>"
paulson@23449
   184
  by (metis 66 43)
paulson@23449
   185
show "False"
paulson@23449
   186
  by (metis 67 64)
paulson@23449
   187
qed
paulson@23449
   188
paulson@23449
   189
lemma (*bigo_pos_const:*) "(EX (c::'a::ordered_idom). 
paulson@23449
   190
    ALL x. (abs (h x)) <= (c * (abs (f x))))
paulson@23449
   191
      = (EX c. 0 < c & (ALL x. (abs(h x)) <= (c * (abs (f x)))))"
paulson@23449
   192
  apply auto
paulson@23449
   193
  apply (case_tac "c = 0", simp)
paulson@23449
   194
  apply (rule_tac x = "1" in exI, simp)
paulson@23449
   195
  apply (rule_tac x = "abs c" in exI, auto);
paulson@23449
   196
ML{*ResReconstruct.modulus:=2*}
paulson@23449
   197
proof (neg_clausify)
paulson@23449
   198
fix c x
paulson@23449
   199
assume 0: "\<And>A. \<bar>h A\<bar> \<le> c * \<bar>f A\<bar>"
paulson@23449
   200
assume 1: "c \<noteq> (0\<Colon>'a::ordered_idom)"
paulson@23449
   201
assume 2: "\<not> \<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>"
paulson@23449
   202
have 3: "\<And>X3. (1\<Colon>'a) * X3 \<le> X3"
paulson@23449
   203
  by (metis mult_le_cancel_right2 order_refl order_refl)
paulson@23449
   204
have 4: "\<bar>0\<Colon>'a\<bar> = (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> (0\<Colon>'a)"
paulson@23449
   205
  by (metis abs_mult_pos mult_cancel_right1 mult_cancel_right1)
paulson@23449
   206
have 5: "\<not> (0\<Colon>'a) < (0\<Colon>'a)"
paulson@23449
   207
  by (metis abs_not_less_zero 4 order_refl)
paulson@23449
   208
have 6: "(0\<Colon>'a) = - ((1\<Colon>'a) * (0\<Colon>'a))"
paulson@23449
   209
  by (metis abs_of_nonpos 3 mult_cancel_right1 4 order_refl)
paulson@23449
   210
have 7: "\<And>X3. \<bar>X3\<bar> = X3 \<or> X3 \<le> (0\<Colon>'a)"
paulson@23449
   211
  by (metis abs_of_nonneg linorder_linear)
paulson@23449
   212
have 8: "c \<le> (0\<Colon>'a)"
paulson@23449
   213
  by (metis 2 7 0)
paulson@23449
   214
have 9: "\<bar>c\<bar> = - c"
paulson@23449
   215
  by (metis abs_of_nonpos 8)
paulson@23449
   216
have 10: "\<not> \<bar>h x\<bar> \<le> - c * \<bar>f x\<bar>"
paulson@23449
   217
  by (metis 2 9)
paulson@23449
   218
have 11: "\<And>X3. X3 * (1\<Colon>'a) = X3"
paulson@23449
   219
  by (metis mult_cancel_right1 AC_mult.f.commute)
paulson@23449
   220
have 12: "(0\<Colon>'a) \<le> (1\<Colon>'a)"
paulson@23449
   221
  by (metis zero_le_square AC_mult.f.commute mult_cancel_left1)
paulson@23449
   222
have 13: "\<And>X3. (0\<Colon>'a) = - X3 \<or> X3 \<noteq> (0\<Colon>'a)"
paulson@23449
   223
  by (metis neg_equal_iff_equal 6 mult_cancel_right1 minus_equation_iff)
paulson@23449
   224
have 14: "\<And>X3. (0\<Colon>'a) = \<bar>X3\<bar> \<or> X3 \<noteq> (0\<Colon>'a)"
paulson@23449
   225
  by (metis abs_minus_cancel 13 4 order_refl)
paulson@23449
   226
have 15: "\<And>X1 X3. (0\<Colon>'a) = \<bar>X3 * X1\<bar> \<or> X3 \<noteq> (0\<Colon>'a)"
paulson@23449
   227
  by (metis abs_mult 14 mult_cancel_left1)
paulson@23449
   228
have 16: "\<And>X1 X3. X3 * X1 = (0\<Colon>'a) \<or> X3 \<noteq> (0\<Colon>'a)"
paulson@23449
   229
  by (metis zero_less_abs_iff 15 5)
paulson@23449
   230
have 17: "\<And>X1 X3. (0\<Colon>'a) = \<bar>X3 * X1\<bar> \<or> \<bar>X1\<bar> \<noteq> (0\<Colon>'a)"
paulson@23449
   231
  by (metis abs_mult AC_mult.f.commute 16)
paulson@23449
   232
have 18: "\<And>X3. (0\<Colon>'a) \<le> \<bar>X3\<bar>"
paulson@23449
   233
  by (metis abs_ge_zero abs_mult_pos 11 12)
paulson@23449
   234
have 19: "\<And>X3. X3 * (1\<Colon>'a) = (0\<Colon>'a) \<or> \<bar>X3\<bar> \<noteq> (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> (1\<Colon>'a)"
paulson@23449
   235
  by (metis abs_eq_0 abs_mult_pos 11)
paulson@23449
   236
have 20: "\<And>X3. X3 = (0\<Colon>'a) \<or> \<bar>X3\<bar> \<noteq> (0\<Colon>'a)"
paulson@23449
   237
  by (metis 19 11 12)
paulson@23449
   238
have 21: "\<And>X3::'a. \<bar>\<bar>X3\<bar>\<bar> = \<bar>X3\<bar> \<or> \<not> (0\<Colon>'a) \<le> (1\<Colon>'a)"
paulson@23449
   239
  by (metis abs_idempotent abs_mult_pos 11)
paulson@23449
   240
have 22: "\<And>X3. \<not> \<bar>X3\<bar> < (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> (1\<Colon>'a)"
paulson@23449
   241
  by (metis abs_not_less_zero abs_mult_pos 11)
paulson@23449
   242
have 23: "\<And>X3. X3 = (0\<Colon>'a) \<or> \<not> \<bar>X3\<bar> \<le> (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> (1\<Colon>'a)"
paulson@23449
   243
  by (metis abs_le_zero_iff abs_mult_pos 11 11)
paulson@23449
   244
have 24: "\<And>X3. X3 * X3 = (0\<Colon>'a) \<or> \<not> X3 * X3 \<le> (0\<Colon>'a)"
paulson@23449
   245
  by (metis 23 12 abs_mult_self abs_mult AC_mult.f.commute)
paulson@23449
   246
have 25: "\<And>X3. \<bar>X3\<bar> = (0\<Colon>'a) \<or> \<not> X3 \<le> (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> X3"
paulson@23449
   247
  by (metis mult_eq_0_iff abs_mult_self 24 mult_le_0_iff)
paulson@23449
   248
have 26: "\<And>X1 X3. X3 * X1 = (0\<Colon>'a) \<or> \<not> X1 \<le> (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> X1"
paulson@23449
   249
  by (metis 20 17 25)
paulson@23449
   250
have 27: "\<And>X1 X3.
paulson@23449
   251
   (0\<Colon>'a) = \<bar>X3 * X1\<bar> \<or> \<not> (0\<Colon>'a) \<le> \<bar>X1\<bar> \<or> \<not> \<bar>X3\<bar> \<le> (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> \<bar>X3\<bar>"
paulson@23449
   252
  by (metis abs_mult_pos abs_mult 25 mult_cancel_left1 21 12)
paulson@23449
   253
have 28: "\<And>X1 X3. (0\<Colon>'a) = \<bar>X3 * X1\<bar> \<or> \<not> \<bar>X3\<bar> \<le> (0\<Colon>'a)"
paulson@23449
   254
  by (metis 27 18 18)
paulson@23449
   255
have 29: "\<And>X1 X3. X3 * X1 = (0\<Colon>'a) \<or> \<not> \<bar>X1\<bar> \<le> (0\<Colon>'a)"
paulson@23449
   256
  by (metis 20 28 AC_mult.f.commute)
paulson@23449
   257
have 30: "\<And>X3. \<bar>h X3\<bar> \<le> (0\<Colon>'a) \<or> \<not> \<bar>f X3\<bar> \<le> (0\<Colon>'a)"
paulson@23449
   258
  by (metis 0 26 18)
paulson@23449
   259
have 31: "\<And>X1 X3. X3 * X1 = (0\<Colon>'a) \<or> (0\<Colon>'a) < \<bar>X1\<bar>"
paulson@23449
   260
  by (metis 29 linorder_not_le)
paulson@23449
   261
have 32: "(0\<Colon>'a) < \<bar>f x\<bar> \<or> \<not> \<bar>h x\<bar> \<le> (0\<Colon>'a)"
paulson@23449
   262
  by (metis 10 31 21 12)
paulson@23449
   263
have 33: "\<And>X3. \<bar>h X3\<bar> < (0\<Colon>'a) \<or> \<not> c < (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) < \<bar>f X3\<bar>"
paulson@23449
   264
  by (metis order_le_less_trans 0 mult_less_0_iff)
paulson@23449
   265
have 34: "\<And>X3. \<not> (0\<Colon>'a) < \<bar>f X3\<bar>"
paulson@23449
   266
  by (metis 33 linorder_antisym_conv2 8 1 22 12)
paulson@23449
   267
show "False"
paulson@23449
   268
  by (metis 34 32 30 linorder_not_le)
paulson@23449
   269
qed
paulson@23449
   270
paulson@23449
   271
lemma (*bigo_pos_const:*) "(EX (c::'a::ordered_idom). 
paulson@23449
   272
    ALL x. (abs (h x)) <= (c * (abs (f x))))
paulson@23449
   273
      = (EX c. 0 < c & (ALL x. (abs(h x)) <= (c * (abs (f x)))))"
paulson@23449
   274
  apply auto
paulson@23449
   275
  apply (case_tac "c = 0", simp)
paulson@23449
   276
  apply (rule_tac x = "1" in exI, simp)
paulson@23449
   277
  apply (rule_tac x = "abs c" in exI, auto);
paulson@23449
   278
ML{*ResReconstruct.modulus:=3*}
paulson@23449
   279
proof (neg_clausify)
paulson@23449
   280
fix c x
paulson@23449
   281
assume 0: "\<And>A\<Colon>'b\<Colon>type.
paulson@23449
   282
   \<bar>(h\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) A\<bar>
paulson@23449
   283
   \<le> (c\<Colon>'a\<Colon>ordered_idom) * \<bar>(f\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) A\<bar>"
paulson@23449
   284
assume 1: "(c\<Colon>'a\<Colon>ordered_idom) \<noteq> (0\<Colon>'a\<Colon>ordered_idom)"
paulson@23449
   285
assume 2: "\<not> \<bar>(h\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) (x\<Colon>'b\<Colon>type)\<bar>
paulson@23449
   286
  \<le> \<bar>c\<Colon>'a\<Colon>ordered_idom\<bar> * \<bar>(f\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) x\<bar>"
paulson@23449
   287
have 3: "\<And>X3\<Colon>'a\<Colon>ordered_idom. (1\<Colon>'a\<Colon>ordered_idom) * X3 \<le> X3"
paulson@23449
   288
  by (metis mult_le_cancel_right2 order_refl order_refl)
paulson@23449
   289
have 4: "\<bar>0\<Colon>'a\<Colon>ordered_idom\<bar> = (0\<Colon>'a\<Colon>ordered_idom)"
paulson@23449
   290
  by (metis abs_mult_pos mult_cancel_right1 mult_cancel_right1 order_refl)
paulson@23449
   291
have 5: "(0\<Colon>'a\<Colon>ordered_idom) = - ((1\<Colon>'a\<Colon>ordered_idom) * (0\<Colon>'a\<Colon>ordered_idom))"
paulson@23449
   292
  by (metis abs_of_nonpos 3 mult_cancel_right1 4)
paulson@23449
   293
have 6: "(c\<Colon>'a\<Colon>ordered_idom) \<le> (0\<Colon>'a\<Colon>ordered_idom)"
paulson@23449
   294
  by (metis 2 abs_of_nonneg linorder_linear 0)
paulson@23449
   295
have 7: "(c\<Colon>'a\<Colon>ordered_idom) < (0\<Colon>'a\<Colon>ordered_idom)"
paulson@23449
   296
  by (metis linorder_antisym_conv2 6 1)
paulson@23449
   297
have 8: "\<And>X3\<Colon>'a\<Colon>ordered_idom. X3 * (1\<Colon>'a\<Colon>ordered_idom) = X3"
paulson@23449
   298
  by (metis mult_cancel_right1 AC_mult.f.commute)
paulson@23449
   299
have 9: "\<And>X3\<Colon>'a\<Colon>ordered_idom. (0\<Colon>'a\<Colon>ordered_idom) = X3 \<or> (0\<Colon>'a\<Colon>ordered_idom) \<noteq> - X3"
paulson@23449
   300
  by (metis neg_equal_iff_equal 5 mult_cancel_right1)
paulson@23449
   301
have 10: "\<And>X3\<Colon>'a\<Colon>ordered_idom. (0\<Colon>'a\<Colon>ordered_idom) = \<bar>X3\<bar> \<or> X3 \<noteq> (0\<Colon>'a\<Colon>ordered_idom)"
paulson@23449
   302
  by (metis abs_minus_cancel 9 minus_equation_iff 4)
paulson@23449
   303
have 11: "\<And>(X1\<Colon>'a\<Colon>ordered_idom) X3\<Colon>'a\<Colon>ordered_idom.
paulson@23449
   304
   (0\<Colon>'a\<Colon>ordered_idom) * \<bar>X1\<bar> = \<bar>X3 * X1\<bar> \<or> X3 \<noteq> (0\<Colon>'a\<Colon>ordered_idom)"
paulson@23449
   305
  by (metis abs_mult 10)
paulson@23449
   306
have 12: "\<And>(X1\<Colon>'a\<Colon>ordered_idom) X3\<Colon>'a\<Colon>ordered_idom.
paulson@23449
   307
   X3 * X1 = (0\<Colon>'a\<Colon>ordered_idom) \<or> X3 \<noteq> (0\<Colon>'a\<Colon>ordered_idom)"
paulson@23449
   308
  by (metis zero_less_abs_iff 11 mult_cancel_left1 abs_not_less_zero 4)
paulson@23449
   309
have 13: "\<And>X3\<Colon>'a\<Colon>ordered_idom. \<bar>X3 * X3\<bar> = X3 * X3"
paulson@23449
   310
  by (metis abs_mult_self abs_mult AC_mult.f.commute)
paulson@23449
   311
have 14: "\<And>X3\<Colon>'a\<Colon>ordered_idom. (0\<Colon>'a\<Colon>ordered_idom) \<le> \<bar>X3\<bar>"
paulson@23449
   312
  by (metis abs_ge_zero abs_mult_pos 8 zero_le_square AC_mult.f.commute mult_cancel_left1)
paulson@23449
   313
have 15: "\<And>X3\<Colon>'a\<Colon>ordered_idom.
paulson@23449
   314
   X3 = (0\<Colon>'a\<Colon>ordered_idom) \<or>
paulson@23449
   315
   \<bar>X3\<bar> \<noteq> (0\<Colon>'a\<Colon>ordered_idom) \<or> \<not> (0\<Colon>'a\<Colon>ordered_idom) \<le> (1\<Colon>'a\<Colon>ordered_idom)"
paulson@23449
   316
  by (metis abs_eq_0 abs_mult_pos 8 8)
paulson@23449
   317
have 16: "\<And>X3\<Colon>'a\<Colon>ordered_idom.
paulson@23449
   318
   \<bar>\<bar>X3\<bar>\<bar> = \<bar>X3\<bar> \<or> \<not> (0\<Colon>'a\<Colon>ordered_idom) \<le> (1\<Colon>'a\<Colon>ordered_idom)"
paulson@23449
   319
  by (metis abs_idempotent abs_mult_pos 8)
paulson@23449
   320
have 17: "\<And>X3\<Colon>'a\<Colon>ordered_idom. \<not> \<bar>X3\<bar> < (0\<Colon>'a\<Colon>ordered_idom)"
paulson@23449
   321
  by (metis abs_not_less_zero abs_mult_pos 8 zero_le_square AC_mult.f.commute mult_cancel_left1)
paulson@23449
   322
have 18: "\<And>X3\<Colon>'a\<Colon>ordered_idom.
paulson@23449
   323
   X3 = (0\<Colon>'a\<Colon>ordered_idom) \<or>
paulson@23449
   324
   \<not> \<bar>X3\<bar> \<le> (0\<Colon>'a\<Colon>ordered_idom) \<or>
paulson@23449
   325
   \<not> (0\<Colon>'a\<Colon>ordered_idom) \<le> (1\<Colon>'a\<Colon>ordered_idom)"
paulson@23449
   326
  by (metis abs_le_zero_iff abs_mult_pos 8 8)
paulson@23449
   327
have 19: "\<And>X3\<Colon>'a\<Colon>ordered_idom.
paulson@23449
   328
   X3 * X3 = (0\<Colon>'a\<Colon>ordered_idom) \<or>
paulson@23449
   329
   \<not> X3 \<le> (0\<Colon>'a\<Colon>ordered_idom) \<or> \<not> (0\<Colon>'a\<Colon>ordered_idom) \<le> X3"
paulson@23449
   330
  by (metis 18 zero_le_square AC_mult.f.commute mult_cancel_left1 13 mult_le_0_iff)
paulson@23449
   331
have 20: "\<And>(X1\<Colon>'a\<Colon>ordered_idom) X3\<Colon>'a\<Colon>ordered_idom.
paulson@23449
   332
   X3 * X1 = (0\<Colon>'a\<Colon>ordered_idom) \<or>
paulson@23449
   333
   \<not> X1 \<le> (0\<Colon>'a\<Colon>ordered_idom) \<or> \<not> (0\<Colon>'a\<Colon>ordered_idom) \<le> X1"
paulson@23449
   334
  by (metis 15 zero_le_square AC_mult.f.commute mult_cancel_left1 abs_mult AC_mult.f.commute 12 mult_eq_0_iff abs_mult_self 19)
paulson@23449
   335
have 21: "\<And>(X1\<Colon>'a\<Colon>ordered_idom) X3\<Colon>'a\<Colon>ordered_idom.
paulson@23449
   336
   (0\<Colon>'a\<Colon>ordered_idom) = \<bar>X3 * X1\<bar> \<or>
paulson@23449
   337
   \<not> \<bar>X3\<bar> \<le> (0\<Colon>'a\<Colon>ordered_idom) \<or> \<not> (0\<Colon>'a\<Colon>ordered_idom) \<le> \<bar>X3\<bar>"
paulson@23449
   338
  by (metis abs_mult_pos abs_mult mult_eq_0_iff abs_mult_self 19 mult_cancel_left1 16 zero_le_square AC_mult.f.commute mult_cancel_left1 14)
paulson@23449
   339
have 22: "\<And>(X1\<Colon>'a\<Colon>ordered_idom) X3\<Colon>'a\<Colon>ordered_idom.
paulson@23449
   340
   X3 * X1 = (0\<Colon>'a\<Colon>ordered_idom) \<or> \<not> \<bar>X1\<bar> \<le> (0\<Colon>'a\<Colon>ordered_idom)"
paulson@23449
   341
  by (metis 15 zero_le_square AC_mult.f.commute mult_cancel_left1 21 14 AC_mult.f.commute)
paulson@23449
   342
have 23: "\<And>X3\<Colon>'b\<Colon>type.
paulson@23449
   343
   \<bar>(h\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) X3\<bar> \<le> (0\<Colon>'a\<Colon>ordered_idom) \<or>
paulson@23449
   344
   (0\<Colon>'a\<Colon>ordered_idom) < \<bar>(f\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) X3\<bar>"
paulson@23449
   345
  by (metis 0 20 14 linorder_not_le)
paulson@23449
   346
have 24: "(0\<Colon>'a\<Colon>ordered_idom) < \<bar>(f\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) (x\<Colon>'b\<Colon>type)\<bar> \<or>
paulson@23449
   347
\<not> \<bar>(h\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) x\<bar> \<le> (0\<Colon>'a\<Colon>ordered_idom)"
paulson@23449
   348
  by (metis 2 abs_of_nonpos 6 22 linorder_not_le 16 zero_le_square AC_mult.f.commute mult_cancel_left1)
paulson@23449
   349
have 25: "\<And>X3\<Colon>'b\<Colon>type.
paulson@23449
   350
   \<bar>(h\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) X3\<bar> < (0\<Colon>'a\<Colon>ordered_idom) \<or>
paulson@23449
   351
   \<not> (0\<Colon>'a\<Colon>ordered_idom) < \<bar>(f\<Colon>'b\<Colon>type \<Rightarrow> 'a\<Colon>ordered_idom) X3\<bar>"
paulson@23449
   352
  by (metis order_le_less_trans 0 mult_less_0_iff 7)
paulson@23449
   353
show "False"
paulson@23449
   354
  by (metis 25 17 24 23)
paulson@23449
   355
qed
paulson@23449
   356
paulson@23449
   357
lemma (*bigo_pos_const:*) "(EX (c::'a::ordered_idom). 
paulson@23449
   358
    ALL x. (abs (h x)) <= (c * (abs (f x))))
paulson@23449
   359
      = (EX c. 0 < c & (ALL x. (abs(h x)) <= (c * (abs (f x)))))"
paulson@23449
   360
  apply auto
paulson@23449
   361
  apply (case_tac "c = 0", simp)
paulson@23449
   362
  apply (rule_tac x = "1" in exI, simp)
paulson@23449
   363
  apply (rule_tac x = "abs c" in exI, auto);
paulson@23449
   364
ML{*ResReconstruct.modulus:=4*}
paulson@23449
   365
ML{*ResReconstruct.recon_sorts:=false*}
paulson@23449
   366
proof (neg_clausify)
paulson@23449
   367
fix c x
paulson@23449
   368
assume 0: "\<And>A. \<bar>h A\<bar> \<le> c * \<bar>f A\<bar>"
paulson@23449
   369
assume 1: "c \<noteq> (0\<Colon>'a)"
paulson@23449
   370
assume 2: "\<not> \<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>"
paulson@23449
   371
have 3: "\<And>X3. (1\<Colon>'a) * X3 \<le> X3"
paulson@23449
   372
  by (metis mult_le_cancel_right2 order_refl order_refl)
paulson@23449
   373
have 4: "\<not> (0\<Colon>'a) < (0\<Colon>'a)"
paulson@23449
   374
  by (metis abs_not_less_zero abs_mult_pos mult_cancel_right1 mult_cancel_right1 order_refl)
paulson@23449
   375
have 5: "c \<le> (0\<Colon>'a)"
paulson@23449
   376
  by (metis 2 abs_of_nonneg linorder_linear 0)
paulson@23449
   377
have 6: "\<not> \<bar>h x\<bar> \<le> - c * \<bar>f x\<bar>"
paulson@23449
   378
  by (metis 2 abs_of_nonpos 5)
paulson@23449
   379
have 7: "(0\<Colon>'a) \<le> (1\<Colon>'a)"
paulson@23449
   380
  by (metis zero_le_square AC_mult.f.commute mult_cancel_left1)
paulson@23449
   381
have 8: "\<And>X3. (0\<Colon>'a) = \<bar>X3\<bar> \<or> X3 \<noteq> (0\<Colon>'a)"
paulson@23449
   382
  by (metis abs_minus_cancel neg_equal_iff_equal abs_of_nonpos 3 mult_cancel_right1 abs_mult_pos mult_cancel_right1 mult_cancel_right1 order_refl mult_cancel_right1 minus_equation_iff abs_mult_pos mult_cancel_right1 mult_cancel_right1 order_refl)
paulson@23449
   383
have 9: "\<And>X1 X3. (0\<Colon>'a) = \<bar>X3 * X1\<bar> \<or> X3 \<noteq> (0\<Colon>'a)"
paulson@23449
   384
  by (metis abs_mult 8 mult_cancel_left1)
paulson@23449
   385
have 10: "\<And>X1 X3. (0\<Colon>'a) = \<bar>X3 * X1\<bar> \<or> \<bar>X1\<bar> \<noteq> (0\<Colon>'a)"
paulson@23449
   386
  by (metis abs_mult AC_mult.f.commute zero_less_abs_iff 9 4)
paulson@23449
   387
have 11: "\<And>X3. (0\<Colon>'a) \<le> \<bar>X3\<bar>"
paulson@23449
   388
  by (metis abs_ge_zero abs_mult_pos mult_cancel_right1 AC_mult.f.commute 7)
paulson@23449
   389
have 12: "\<And>X3. X3 = (0\<Colon>'a) \<or> \<bar>X3\<bar> \<noteq> (0\<Colon>'a)"
paulson@23449
   390
  by (metis abs_eq_0 abs_mult_pos mult_cancel_right1 AC_mult.f.commute mult_cancel_right1 AC_mult.f.commute 7)
paulson@23449
   391
have 13: "\<And>X3. \<not> \<bar>X3\<bar> < (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> (1\<Colon>'a)"
paulson@23449
   392
  by (metis abs_not_less_zero abs_mult_pos mult_cancel_right1 AC_mult.f.commute)
paulson@23449
   393
have 14: "\<And>X3. X3 = (0\<Colon>'a) \<or> \<not> \<bar>X3\<bar> \<le> (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> (1\<Colon>'a)"
paulson@23449
   394
  by (metis abs_le_zero_iff abs_mult_pos mult_cancel_right1 AC_mult.f.commute mult_cancel_right1 AC_mult.f.commute)
paulson@23449
   395
have 15: "\<And>X3. \<bar>X3\<bar> = (0\<Colon>'a) \<or> \<not> X3 \<le> (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> X3"
paulson@23449
   396
  by (metis mult_eq_0_iff abs_mult_self 14 7 abs_mult_self abs_mult AC_mult.f.commute mult_le_0_iff)
paulson@23449
   397
have 16: "\<And>X1 X3.
paulson@23449
   398
   (0\<Colon>'a) = \<bar>X3 * X1\<bar> \<or> \<not> (0\<Colon>'a) \<le> \<bar>X1\<bar> \<or> \<not> \<bar>X3\<bar> \<le> (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) \<le> \<bar>X3\<bar>"
paulson@23449
   399
  by (metis abs_mult_pos abs_mult 15 mult_cancel_left1 abs_idempotent abs_mult_pos mult_cancel_right1 AC_mult.f.commute 7)
paulson@23449
   400
have 17: "\<And>X1 X3. X3 * X1 = (0\<Colon>'a) \<or> \<not> \<bar>X1\<bar> \<le> (0\<Colon>'a)"
paulson@23449
   401
  by (metis 12 16 11 11 AC_mult.f.commute)
paulson@23449
   402
have 18: "\<And>X1 X3. X3 * X1 = (0\<Colon>'a) \<or> (0\<Colon>'a) < \<bar>X1\<bar>"
paulson@23449
   403
  by (metis 17 linorder_not_le)
paulson@23449
   404
have 19: "\<And>X3. \<bar>h X3\<bar> < (0\<Colon>'a) \<or> \<not> c < (0\<Colon>'a) \<or> \<not> (0\<Colon>'a) < \<bar>f X3\<bar>"
paulson@23449
   405
  by (metis order_le_less_trans 0 mult_less_0_iff)
paulson@23449
   406
show "False"
paulson@23449
   407
  by (metis 19 linorder_antisym_conv2 5 1 13 7 6 18 abs_idempotent abs_mult_pos mult_cancel_right1 AC_mult.f.commute 7 0 12 10 15 11 linorder_not_le)
paulson@23449
   408
qed
paulson@23449
   409
paulson@23449
   410
paulson@23449
   411
ML{*ResReconstruct.modulus:=1*}
paulson@24545
   412
paulson@24545
   413
(*Vampire finds this structured proof*)
paulson@24545
   414
lemma (*bigo_pos_const:*) "(EX (c::'a::ordered_idom). 
paulson@24545
   415
    ALL x. (abs (h x)) <= (c * (abs (f x))))
paulson@24545
   416
      = (EX c. 0 < c & (ALL x. (abs(h x)) <= (c * (abs (f x)))))"
paulson@24545
   417
  apply auto
paulson@24545
   418
  apply (case_tac "c = 0", simp)
paulson@24545
   419
  apply (rule_tac x = "1" in exI, simp)
paulson@24545
   420
  apply (rule_tac x = "abs c" in exI, auto);
paulson@24545
   421
proof (neg_clausify)
paulson@24545
   422
fix c x  (*sort/type constraint inserted by hand!*)
paulson@24545
   423
have 0: "\<And>(X1\<Colon>'a\<Colon>ordered_idom) X2. \<bar>X1 * \<bar>X2\<bar>\<bar> = \<bar>X1 * X2\<bar>"
paulson@24545
   424
  by (metis abs_ge_zero abs_mult_pos abs_mult)
paulson@24545
   425
assume 1: "\<And>A. \<bar>h A\<bar> \<le> c * \<bar>f A\<bar>"
paulson@24545
   426
have 2: "\<And>X1 X2. \<not> \<bar>X1\<bar> \<le> X2 \<or> (0\<Colon>'a) \<le> X2"
paulson@24545
   427
  by (metis abs_ge_zero order_trans)
paulson@24545
   428
have 3: "\<And>X1. (0\<Colon>'a) \<le> c * \<bar>f X1\<bar>"
paulson@24545
   429
  by (metis 1 2)
paulson@24545
   430
have 4: "\<And>X1. c * \<bar>f X1\<bar> = \<bar>c * f X1\<bar>"
paulson@24545
   431
  by (metis 0 abs_of_nonneg 3)
paulson@24545
   432
have 5: "\<And>X1. - h X1 \<le> c * \<bar>f X1\<bar>"
paulson@24545
   433
  by (metis 1 abs_le_D2)
paulson@24545
   434
have 6: "\<And>X1. - h X1 \<le> \<bar>c * f X1\<bar>"
paulson@24545
   435
  by (metis 4 5)
paulson@24545
   436
have 7: "\<And>X1. h X1 \<le> c * \<bar>f X1\<bar>"
paulson@24545
   437
  by (metis 1 abs_le_D1)
paulson@24545
   438
have 8: "\<And>X1. h X1 \<le> \<bar>c * f X1\<bar>"
paulson@24545
   439
  by (metis 4 7)
paulson@24545
   440
assume 9: "\<not> \<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>"
paulson@24545
   441
have 10: "\<not> \<bar>h x\<bar> \<le> \<bar>c * f x\<bar>"
paulson@24545
   442
  by (metis abs_mult 9)
paulson@24545
   443
show "False"
paulson@24545
   444
  by (metis 6 8 10 abs_leI)
paulson@24545
   445
qed
paulson@24545
   446
paulson@24545
   447
paulson@23449
   448
ML{*ResReconstruct.recon_sorts:=true*}
paulson@23449
   449
paulson@24545
   450
paulson@23449
   451
lemma bigo_alt_def: "O(f) = 
paulson@23449
   452
    {h. EX c. (0 < c & (ALL x. abs (h x) <= c * abs (f x)))}"
paulson@23449
   453
by (auto simp add: bigo_def bigo_pos_const)
paulson@23449
   454
paulson@23449
   455
ML{*ResAtp.problem_name := "BigO__bigo_elt_subset"*}
paulson@23449
   456
lemma bigo_elt_subset [intro]: "f : O(g) ==> O(f) <= O(g)"
paulson@23449
   457
  apply (auto simp add: bigo_alt_def)
paulson@23449
   458
  apply (rule_tac x = "ca * c" in exI)
paulson@23449
   459
  apply (rule conjI)
paulson@23449
   460
  apply (rule mult_pos_pos)
paulson@23449
   461
  apply (assumption)+ 
paulson@23449
   462
(*sledgehammer*);
paulson@23449
   463
  apply (rule allI)
paulson@23449
   464
  apply (drule_tac x = "xa" in spec)+
paulson@23449
   465
  apply (subgoal_tac "ca * abs(f xa) <= ca * (c * abs(g xa))");
paulson@23449
   466
  apply (erule order_trans)
paulson@23449
   467
  apply (simp add: mult_ac)
paulson@23449
   468
  apply (rule mult_left_mono, assumption)
paulson@23449
   469
  apply (rule order_less_imp_le, assumption);
paulson@23449
   470
done
paulson@23449
   471
paulson@23449
   472
paulson@23449
   473
ML{*ResAtp.problem_name := "BigO__bigo_refl"*}
paulson@23449
   474
lemma bigo_refl [intro]: "f : O(f)"
paulson@23449
   475
  apply(auto simp add: bigo_def)
paulson@23449
   476
proof (neg_clausify)
paulson@23449
   477
fix x
paulson@23449
   478
assume 0: "\<And>mes_pSG\<Colon>'b\<Colon>ordered_idom.
paulson@23449
   479
   \<not> \<bar>(f\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) ((x\<Colon>'b\<Colon>ordered_idom \<Rightarrow> 'a) mes_pSG)\<bar>
paulson@23449
   480
     \<le> mes_pSG * \<bar>f (x mes_pSG)\<bar>"
paulson@23449
   481
have 1: "\<And>X3\<Colon>'b. X3 \<le> (1\<Colon>'b) * X3 \<or> \<not> (1\<Colon>'b) \<le> (1\<Colon>'b)"
paulson@23449
   482
  by (metis Ring_and_Field.mult_le_cancel_right1 order_refl)
paulson@23449
   483
have 2: "\<And>X3\<Colon>'b. X3 \<le> (1\<Colon>'b) * X3"
paulson@23449
   484
  by (metis 1 order_refl)
paulson@23449
   485
show 3: "False"
paulson@23449
   486
  by (metis 0 2)
paulson@23449
   487
qed
paulson@23449
   488
paulson@23449
   489
ML{*ResAtp.problem_name := "BigO__bigo_zero"*}
paulson@23449
   490
lemma bigo_zero: "0 : O(g)"
paulson@23449
   491
  apply (auto simp add: bigo_def func_zero)
paulson@23449
   492
proof (neg_clausify)
paulson@23449
   493
fix x
paulson@23449
   494
assume 0: "\<And>mes_mVM\<Colon>'b\<Colon>ordered_idom.
paulson@23449
   495
   \<not> (0\<Colon>'b\<Colon>ordered_idom)
paulson@23449
   496
     \<le> mes_mVM *
paulson@23449
   497
       \<bar>(g\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom)
paulson@23449
   498
         ((x\<Colon>'b\<Colon>ordered_idom \<Rightarrow> 'a) mes_mVM)\<bar>"
paulson@23449
   499
have 1: "(0\<Colon>'b\<Colon>ordered_idom) < (0\<Colon>'b\<Colon>ordered_idom)"
paulson@23449
   500
  by (metis 0 Ring_and_Field.mult_le_cancel_left1)
paulson@23449
   501
show 2: "False"
paulson@23449
   502
  by (metis Orderings.linorder_class.neq_iff 1)
paulson@23449
   503
qed
paulson@23449
   504
paulson@23449
   505
lemma bigo_zero2: "O(%x.0) = {%x.0}"
paulson@23449
   506
  apply (auto simp add: bigo_def) 
paulson@23449
   507
  apply (rule ext)
paulson@23449
   508
  apply auto
paulson@23449
   509
done
paulson@23449
   510
paulson@23449
   511
lemma bigo_plus_self_subset [intro]: 
paulson@23449
   512
  "O(f) + O(f) <= O(f)"
paulson@23449
   513
  apply (auto simp add: bigo_alt_def set_plus)
paulson@23449
   514
  apply (rule_tac x = "c + ca" in exI)
paulson@23449
   515
  apply auto
nipkow@23477
   516
  apply (simp add: ring_distribs func_plus)
paulson@23449
   517
  apply (blast intro:order_trans abs_triangle_ineq add_mono elim:) 
paulson@23449
   518
done
paulson@23449
   519
paulson@23449
   520
lemma bigo_plus_idemp [simp]: "O(f) + O(f) = O(f)"
paulson@23449
   521
  apply (rule equalityI)
paulson@23449
   522
  apply (rule bigo_plus_self_subset)
paulson@23449
   523
  apply (rule set_zero_plus2) 
paulson@23449
   524
  apply (rule bigo_zero)
paulson@23449
   525
done
paulson@23449
   526
paulson@23449
   527
lemma bigo_plus_subset [intro]: "O(f + g) <= O(f) + O(g)"
paulson@23449
   528
  apply (rule subsetI)
paulson@23449
   529
  apply (auto simp add: bigo_def bigo_pos_const func_plus set_plus)
paulson@23449
   530
  apply (subst bigo_pos_const [symmetric])+
paulson@23449
   531
  apply (rule_tac x = 
paulson@23449
   532
    "%n. if abs (g n) <= (abs (f n)) then x n else 0" in exI)
paulson@23449
   533
  apply (rule conjI)
paulson@23449
   534
  apply (rule_tac x = "c + c" in exI)
paulson@23449
   535
  apply (clarsimp)
paulson@23449
   536
  apply (auto)
paulson@23449
   537
  apply (subgoal_tac "c * abs (f xa + g xa) <= (c + c) * abs (f xa)")
paulson@23449
   538
  apply (erule_tac x = xa in allE)
paulson@23449
   539
  apply (erule order_trans)
paulson@23449
   540
  apply (simp)
paulson@23449
   541
  apply (subgoal_tac "c * abs (f xa + g xa) <= c * (abs (f xa) + abs (g xa))")
paulson@23449
   542
  apply (erule order_trans)
nipkow@23477
   543
  apply (simp add: ring_distribs)
paulson@23449
   544
  apply (rule mult_left_mono)
paulson@23449
   545
  apply assumption
paulson@23449
   546
  apply (simp add: order_less_le)
paulson@23449
   547
  apply (rule mult_left_mono)
paulson@23449
   548
  apply (simp add: abs_triangle_ineq)
paulson@23449
   549
  apply (simp add: order_less_le)
paulson@23449
   550
  apply (rule mult_nonneg_nonneg)
paulson@23449
   551
  apply (rule add_nonneg_nonneg)
paulson@23449
   552
  apply auto
paulson@23449
   553
  apply (rule_tac x = "%n. if (abs (f n)) <  abs (g n) then x n else 0" 
paulson@23449
   554
     in exI)
paulson@23449
   555
  apply (rule conjI)
paulson@23449
   556
  apply (rule_tac x = "c + c" in exI)
paulson@23449
   557
  apply auto
paulson@23449
   558
  apply (subgoal_tac "c * abs (f xa + g xa) <= (c + c) * abs (g xa)")
paulson@23449
   559
  apply (erule_tac x = xa in allE)
paulson@23449
   560
  apply (erule order_trans)
paulson@23449
   561
  apply (simp)
paulson@23449
   562
  apply (subgoal_tac "c * abs (f xa + g xa) <= c * (abs (f xa) + abs (g xa))")
paulson@23449
   563
  apply (erule order_trans)
nipkow@23477
   564
  apply (simp add: ring_distribs)
paulson@23449
   565
  apply (rule mult_left_mono)
paulson@23449
   566
  apply (simp add: order_less_le)
paulson@23449
   567
  apply (simp add: order_less_le)
paulson@23449
   568
  apply (rule mult_left_mono)
paulson@23449
   569
  apply (rule abs_triangle_ineq)
paulson@23449
   570
  apply (simp add: order_less_le)
paulson@23449
   571
  apply (rule mult_nonneg_nonneg)
paulson@23449
   572
  apply (rule add_nonneg_nonneg)
paulson@23449
   573
  apply (erule order_less_imp_le)+
paulson@23449
   574
  apply simp
paulson@23449
   575
  apply (rule ext)
paulson@23449
   576
  apply (auto simp add: if_splits linorder_not_le)
paulson@23449
   577
done
paulson@23449
   578
paulson@23449
   579
lemma bigo_plus_subset2 [intro]: "A <= O(f) ==> B <= O(f) ==> A + B <= O(f)"
paulson@23449
   580
  apply (subgoal_tac "A + B <= O(f) + O(f)")
paulson@23449
   581
  apply (erule order_trans)
paulson@23449
   582
  apply simp
paulson@23449
   583
  apply (auto del: subsetI simp del: bigo_plus_idemp)
paulson@23449
   584
done
paulson@23449
   585
paulson@23449
   586
ML{*ResAtp.problem_name := "BigO__bigo_plus_eq"*}
paulson@23449
   587
lemma bigo_plus_eq: "ALL x. 0 <= f x ==> ALL x. 0 <= g x ==> 
paulson@23449
   588
  O(f + g) = O(f) + O(g)"
paulson@23449
   589
  apply (rule equalityI)
paulson@23449
   590
  apply (rule bigo_plus_subset)
paulson@23449
   591
  apply (simp add: bigo_alt_def set_plus func_plus)
paulson@23449
   592
  apply clarify 
paulson@23449
   593
(*sledgehammer*); 
paulson@23449
   594
  apply (rule_tac x = "max c ca" in exI)
paulson@23449
   595
  apply (rule conjI)
paulson@23449
   596
  apply (subgoal_tac "c <= max c ca")
paulson@23449
   597
  apply (erule order_less_le_trans)
paulson@23449
   598
  apply assumption
paulson@23449
   599
  apply (rule le_maxI1)
paulson@23449
   600
  apply clarify
paulson@23449
   601
  apply (drule_tac x = "xa" in spec)+
paulson@23449
   602
  apply (subgoal_tac "0 <= f xa + g xa")
nipkow@23477
   603
  apply (simp add: ring_distribs)
paulson@23449
   604
  apply (subgoal_tac "abs(a xa + b xa) <= abs(a xa) + abs(b xa)")
paulson@23449
   605
  apply (subgoal_tac "abs(a xa) + abs(b xa) <= 
paulson@23449
   606
      max c ca * f xa + max c ca * g xa")
paulson@23449
   607
  apply (blast intro: order_trans)
paulson@23449
   608
  defer 1
paulson@23449
   609
  apply (rule abs_triangle_ineq)
paulson@23449
   610
  apply (rule add_nonneg_nonneg)
paulson@23449
   611
  apply assumption+
paulson@23449
   612
  apply (rule add_mono)
paulson@23449
   613
ML{*ResAtp.problem_name := "BigO__bigo_plus_eq_simpler"*} 
paulson@23449
   614
(*sledgehammer...fails*); 
paulson@23449
   615
  apply (subgoal_tac "c * f xa <= max c ca * f xa")
paulson@23449
   616
  apply (blast intro: order_trans)
paulson@23449
   617
  apply (rule mult_right_mono)
paulson@23449
   618
  apply (rule le_maxI1)
paulson@23449
   619
  apply assumption
paulson@23449
   620
  apply (subgoal_tac "ca * g xa <= max c ca * g xa")
paulson@23449
   621
  apply (blast intro: order_trans)
paulson@23449
   622
  apply (rule mult_right_mono)
paulson@23449
   623
  apply (rule le_maxI2)
paulson@23449
   624
  apply assumption
paulson@23449
   625
done
paulson@23449
   626
paulson@23449
   627
ML{*ResAtp.problem_name := "BigO__bigo_bounded_alt"*}
paulson@23449
   628
lemma bigo_bounded_alt: "ALL x. 0 <= f x ==> ALL x. f x <= c * g x ==> 
paulson@23449
   629
    f : O(g)" 
paulson@23449
   630
  apply (auto simp add: bigo_def)
paulson@23449
   631
(*Version 1: one-shot proof*)
paulson@23449
   632
  apply (metis OrderedGroup.abs_ge_self  OrderedGroup.abs_le_D1  OrderedGroup.abs_of_nonneg  Orderings.linorder_class.not_less  order_less_le  Orderings.xt1(12)  Ring_and_Field.abs_mult)
paulson@23449
   633
  done
paulson@23449
   634
paulson@23449
   635
lemma bigo_bounded_alt: "ALL x. 0 <= f x ==> ALL x. f x <= c * g x ==> 
paulson@23449
   636
    f : O(g)" 
paulson@23449
   637
  apply (auto simp add: bigo_def)
paulson@23449
   638
(*Version 2: single-step proof*)
paulson@23449
   639
proof (neg_clausify)
paulson@23449
   640
fix x
paulson@23449
   641
assume 0: "\<And>mes_mbt\<Colon>'a.
paulson@23449
   642
   (f\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) mes_mbt
paulson@23449
   643
   \<le> (c\<Colon>'b\<Colon>ordered_idom) * (g\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) mes_mbt"
paulson@23449
   644
assume 1: "\<And>mes_mbs\<Colon>'b\<Colon>ordered_idom.
paulson@23449
   645
   \<not> (f\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) ((x\<Colon>'b\<Colon>ordered_idom \<Rightarrow> 'a) mes_mbs)
paulson@23449
   646
     \<le> mes_mbs * \<bar>(g\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) (x mes_mbs)\<bar>"
paulson@23449
   647
have 2: "\<And>X3\<Colon>'a.
paulson@23449
   648
   (c\<Colon>'b\<Colon>ordered_idom) * (g\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) X3 =
paulson@23449
   649
   (f\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) X3 \<or>
paulson@23449
   650
   \<not> c * g X3 \<le> f X3"
paulson@23449
   651
  by (metis Lattices.min_max.less_eq_less_inf.antisym_intro 0)
paulson@23449
   652
have 3: "\<And>X3\<Colon>'b\<Colon>ordered_idom.
paulson@23449
   653
   \<not> (f\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) ((x\<Colon>'b\<Colon>ordered_idom \<Rightarrow> 'a) \<bar>X3\<bar>)
paulson@23449
   654
     \<le> \<bar>X3 * (g\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) (x \<bar>X3\<bar>)\<bar>"
paulson@23449
   655
  by (metis 1 Ring_and_Field.abs_mult)
paulson@23449
   656
have 4: "\<And>X3\<Colon>'b\<Colon>ordered_idom. (1\<Colon>'b\<Colon>ordered_idom) * X3 = X3"
paulson@23449
   657
  by (metis Ring_and_Field.mult_cancel_left2 Finite_Set.AC_mult.f.commute)
paulson@23449
   658
have 5: "\<And>X3\<Colon>'b\<Colon>ordered_idom. X3 * (1\<Colon>'b\<Colon>ordered_idom) = X3"
paulson@23449
   659
  by (metis Ring_and_Field.mult_cancel_right2 Finite_Set.AC_mult.f.commute)
paulson@23449
   660
have 6: "\<And>X3\<Colon>'b\<Colon>ordered_idom. \<bar>X3\<bar> * \<bar>X3\<bar> = X3 * X3"
paulson@23449
   661
  by (metis Ring_and_Field.abs_mult_self Finite_Set.AC_mult.f.commute)
paulson@23449
   662
have 7: "\<And>X3\<Colon>'b\<Colon>ordered_idom. (0\<Colon>'b\<Colon>ordered_idom) \<le> X3 * X3"
paulson@23449
   663
  by (metis Ring_and_Field.zero_le_square Finite_Set.AC_mult.f.commute)
paulson@23449
   664
have 8: "(0\<Colon>'b\<Colon>ordered_idom) \<le> (1\<Colon>'b\<Colon>ordered_idom)"
paulson@23449
   665
  by (metis 7 Ring_and_Field.mult_cancel_left2)
paulson@23449
   666
have 9: "\<And>X3\<Colon>'b\<Colon>ordered_idom. X3 * X3 = \<bar>X3 * X3\<bar>"
paulson@23449
   667
  by (metis Ring_and_Field.abs_mult 6)
paulson@23449
   668
have 10: "\<bar>1\<Colon>'b\<Colon>ordered_idom\<bar> = (1\<Colon>'b\<Colon>ordered_idom)"
paulson@23449
   669
  by (metis 9 4)
paulson@23449
   670
have 11: "\<And>X3\<Colon>'b\<Colon>ordered_idom. \<bar>\<bar>X3\<bar>\<bar> = \<bar>X3\<bar> * \<bar>1\<Colon>'b\<Colon>ordered_idom\<bar>"
paulson@23449
   671
  by (metis Ring_and_Field.abs_mult OrderedGroup.abs_idempotent 5)
paulson@23449
   672
have 12: "\<And>X3\<Colon>'b\<Colon>ordered_idom. \<bar>\<bar>X3\<bar>\<bar> = \<bar>X3\<bar>"
paulson@23449
   673
  by (metis 11 10 5)
paulson@23449
   674
have 13: "\<And>(X1\<Colon>'b\<Colon>ordered_idom) X3\<Colon>'b\<Colon>ordered_idom.
paulson@23449
   675
   X3 * (1\<Colon>'b\<Colon>ordered_idom) \<le> X1 \<or>
paulson@23449
   676
   \<not> \<bar>X3\<bar> \<le> X1 \<or> \<not> (0\<Colon>'b\<Colon>ordered_idom) \<le> (1\<Colon>'b\<Colon>ordered_idom)"
paulson@23449
   677
  by (metis OrderedGroup.abs_le_D1 Ring_and_Field.abs_mult_pos 5)
paulson@23449
   678
have 14: "\<And>(X1\<Colon>'b\<Colon>ordered_idom) X3\<Colon>'b\<Colon>ordered_idom.
paulson@23449
   679
   X3 \<le> X1 \<or> \<not> \<bar>X3\<bar> \<le> X1 \<or> \<not> (0\<Colon>'b\<Colon>ordered_idom) \<le> (1\<Colon>'b\<Colon>ordered_idom)"
paulson@23449
   680
  by (metis 13 5)
paulson@23449
   681
have 15: "\<And>(X1\<Colon>'b\<Colon>ordered_idom) X3\<Colon>'b\<Colon>ordered_idom. X3 \<le> X1 \<or> \<not> \<bar>X3\<bar> \<le> X1"
paulson@23449
   682
  by (metis 14 8)
paulson@23449
   683
have 16: "\<And>(X1\<Colon>'b\<Colon>ordered_idom) X3\<Colon>'b\<Colon>ordered_idom. X3 \<le> X1 \<or> X1 \<le> \<bar>X3\<bar>"
paulson@23449
   684
  by (metis 15 Orderings.linorder_class.less_eq_less.linear)
paulson@23449
   685
have 17: "\<And>X3\<Colon>'b\<Colon>ordered_idom.
paulson@23449
   686
   X3 * (g\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) ((x\<Colon>'b\<Colon>ordered_idom \<Rightarrow> 'a) \<bar>X3\<bar>)
paulson@23449
   687
   \<le> (f\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) (x \<bar>X3\<bar>)"
paulson@23449
   688
  by (metis 3 16)
paulson@23449
   689
have 18: "(c\<Colon>'b\<Colon>ordered_idom) *
paulson@23449
   690
(g\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) ((x\<Colon>'b\<Colon>ordered_idom \<Rightarrow> 'a) \<bar>c\<bar>) =
paulson@23449
   691
(f\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) (x \<bar>c\<bar>)"
paulson@23449
   692
  by (metis 2 17)
paulson@23449
   693
have 19: "\<And>(X1\<Colon>'b\<Colon>ordered_idom) X3\<Colon>'b\<Colon>ordered_idom. \<bar>X3 * X1\<bar> \<le> \<bar>\<bar>X3\<bar>\<bar> * \<bar>\<bar>X1\<bar>\<bar>"
paulson@23449
   694
  by (metis 15 Ring_and_Field.abs_le_mult Ring_and_Field.abs_mult)
paulson@23449
   695
have 20: "\<And>(X1\<Colon>'b\<Colon>ordered_idom) X3\<Colon>'b\<Colon>ordered_idom. \<bar>X3 * X1\<bar> \<le> \<bar>X3\<bar> * \<bar>X1\<bar>"
paulson@23449
   696
  by (metis 19 12 12)
paulson@23449
   697
have 21: "\<And>(X1\<Colon>'b\<Colon>ordered_idom) X3\<Colon>'b\<Colon>ordered_idom. X3 * X1 \<le> \<bar>X3\<bar> * \<bar>X1\<bar>"
paulson@23449
   698
  by (metis 15 20)
paulson@23449
   699
have 22: "(f\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom)
paulson@23449
   700
 ((x\<Colon>'b\<Colon>ordered_idom \<Rightarrow> 'a) \<bar>c\<Colon>'b\<Colon>ordered_idom\<bar>)
paulson@23449
   701
\<le> \<bar>c\<bar> * \<bar>(g\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) (x \<bar>c\<bar>)\<bar>"
paulson@23449
   702
  by (metis 21 18)
paulson@23449
   703
show 23: "False"
paulson@23449
   704
  by (metis 22 1)
paulson@23449
   705
qed
paulson@23449
   706
paulson@23449
   707
paulson@23449
   708
text{*So here is the easier (and more natural) problem using transitivity*}
paulson@23449
   709
ML{*ResAtp.problem_name := "BigO__bigo_bounded_alt_trans"*}
paulson@23449
   710
lemma "ALL x. 0 <= f x ==> ALL x. f x <= c * g x ==> f : O(g)" 
paulson@23449
   711
  apply (auto simp add: bigo_def)
paulson@23449
   712
  (*Version 1: one-shot proof*) 
paulson@23449
   713
apply (metis Orderings.leD Orderings.leI abs_ge_self abs_le_D1 abs_mult abs_of_nonneg order_le_less xt1(12));
paulson@23449
   714
  done
paulson@23449
   715
paulson@23449
   716
text{*So here is the easier (and more natural) problem using transitivity*}
paulson@23449
   717
ML{*ResAtp.problem_name := "BigO__bigo_bounded_alt_trans"*}
paulson@23449
   718
lemma "ALL x. 0 <= f x ==> ALL x. f x <= c * g x ==> f : O(g)" 
paulson@23449
   719
  apply (auto simp add: bigo_def)
paulson@23449
   720
(*Version 2: single-step proof*)
paulson@23449
   721
proof (neg_clausify)
paulson@23449
   722
fix x
paulson@23519
   723
assume 0: "\<And>A\<Colon>'a\<Colon>type.
paulson@23519
   724
   (f\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) A
paulson@23519
   725
   \<le> (c\<Colon>'b\<Colon>ordered_idom) * (g\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) A"
paulson@23519
   726
assume 1: "\<And>A\<Colon>'b\<Colon>ordered_idom.
paulson@23519
   727
   \<not> (f\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) ((x\<Colon>'b\<Colon>ordered_idom \<Rightarrow> 'a\<Colon>type) A)
paulson@23519
   728
     \<le> A * \<bar>(g\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) (x A)\<bar>"
paulson@23519
   729
have 2: "\<And>X2\<Colon>'a\<Colon>type.
paulson@23519
   730
   \<not> (c\<Colon>'b\<Colon>ordered_idom) * (g\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) X2
paulson@23519
   731
     < (f\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) X2"
paulson@23519
   732
  by (metis 0 linorder_not_le)
paulson@23519
   733
have 3: "\<And>X2\<Colon>'b\<Colon>ordered_idom.
paulson@23519
   734
   \<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
   735
     \<le> \<bar>X2 * (g\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) (x \<bar>X2\<bar>)\<bar>"
paulson@23519
   736
  by (metis abs_mult 1)
paulson@23519
   737
have 4: "\<And>X2\<Colon>'b\<Colon>ordered_idom.
paulson@23519
   738
   \<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
   739
   < (f\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) (x \<bar>X2\<bar>)"
paulson@23519
   740
  by (metis 3 linorder_not_less)
paulson@23519
   741
have 5: "\<And>X2\<Colon>'b\<Colon>ordered_idom.
paulson@23519
   742
   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
   743
   < (f\<Colon>'a\<Colon>type \<Rightarrow> 'b\<Colon>ordered_idom) (x \<bar>X2\<bar>)"
paulson@23519
   744
  by (metis abs_less_iff 4)
paulson@23519
   745
show "False"
paulson@23519
   746
  by (metis 2 5)
paulson@23449
   747
qed
paulson@23449
   748
paulson@23449
   749
paulson@23449
   750
lemma bigo_bounded: "ALL x. 0 <= f x ==> ALL x. f x <= g x ==> 
paulson@23449
   751
    f : O(g)" 
paulson@23449
   752
  apply (erule bigo_bounded_alt [of f 1 g])
paulson@23449
   753
  apply simp
paulson@23449
   754
done
paulson@23449
   755
paulson@23449
   756
ML{*ResAtp.problem_name := "BigO__bigo_bounded2"*}
paulson@23449
   757
lemma bigo_bounded2: "ALL x. lb x <= f x ==> ALL x. f x <= lb x + g x ==>
paulson@23449
   758
    f : lb +o O(g)"
paulson@23449
   759
  apply (rule set_minus_imp_plus)
paulson@23449
   760
  apply (rule bigo_bounded)
paulson@23449
   761
  apply (auto simp add: diff_minus func_minus func_plus)
paulson@23449
   762
  prefer 2
paulson@23449
   763
  apply (drule_tac x = x in spec)+ 
paulson@23449
   764
  apply arith (*not clear that it's provable otherwise*) 
paulson@23449
   765
proof (neg_clausify)
paulson@23449
   766
fix x
paulson@23449
   767
assume 0: "\<And>y. lb y \<le> f y"
paulson@23449
   768
assume 1: "\<not> (0\<Colon>'b) \<le> f x + - lb x"
paulson@23449
   769
have 2: "\<And>X3. (0\<Colon>'b) + X3 = X3"
paulson@23449
   770
  by (metis diff_eq_eq right_minus_eq)
paulson@23449
   771
have 3: "\<not> (0\<Colon>'b) \<le> f x - lb x"
paulson@23449
   772
  by (metis 1 compare_rls(1))
paulson@23449
   773
have 4: "\<not> (0\<Colon>'b) + lb x \<le> f x"
paulson@23449
   774
  by (metis 3 le_diff_eq)
paulson@23449
   775
show "False"
paulson@23449
   776
  by (metis 4 2 0)
paulson@23449
   777
qed
paulson@23449
   778
paulson@23449
   779
ML{*ResAtp.problem_name := "BigO__bigo_abs"*}
paulson@23449
   780
lemma bigo_abs: "(%x. abs(f x)) =o O(f)" 
paulson@23449
   781
  apply (unfold bigo_def)
paulson@23449
   782
  apply auto
paulson@23449
   783
proof (neg_clausify)
paulson@23449
   784
fix x
paulson@23449
   785
assume 0: "!!mes_o43::'b::ordered_idom.
paulson@23449
   786
   ~ abs ((f::'a::type => 'b::ordered_idom)
paulson@23449
   787
           ((x::'b::ordered_idom => 'a::type) mes_o43))
paulson@23449
   788
     <= mes_o43 * abs (f (x mes_o43))"
paulson@23449
   789
have 1: "!!X3::'b::ordered_idom.
paulson@23449
   790
   X3 <= (1::'b::ordered_idom) * X3 |
paulson@23449
   791
   ~ (1::'b::ordered_idom) <= (1::'b::ordered_idom)"
paulson@23449
   792
  by (metis mult_le_cancel_right1 order_refl)
paulson@23449
   793
have 2: "!!X3::'b::ordered_idom. X3 <= (1::'b::ordered_idom) * X3"
paulson@23449
   794
  by (metis 1 order_refl)
paulson@23449
   795
show "False"
paulson@23449
   796
  by (metis 0 2)
paulson@23449
   797
qed
paulson@23449
   798
paulson@23449
   799
ML{*ResAtp.problem_name := "BigO__bigo_abs2"*}
paulson@23449
   800
lemma bigo_abs2: "f =o O(%x. abs(f x))"
paulson@23449
   801
  apply (unfold bigo_def)
paulson@23449
   802
  apply auto
paulson@23449
   803
proof (neg_clausify)
paulson@23449
   804
fix x
paulson@23449
   805
assume 0: "\<And>mes_o4C\<Colon>'b\<Colon>ordered_idom.
paulson@23449
   806
   \<not> \<bar>(f\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) ((x\<Colon>'b\<Colon>ordered_idom \<Rightarrow> 'a) mes_o4C)\<bar>
paulson@23449
   807
     \<le> mes_o4C * \<bar>f (x mes_o4C)\<bar>"
paulson@23449
   808
have 1: "\<And>X3\<Colon>'b\<Colon>ordered_idom.
paulson@23449
   809
   X3 \<le> (1\<Colon>'b\<Colon>ordered_idom) * X3 \<or>
paulson@23449
   810
   \<not> (1\<Colon>'b\<Colon>ordered_idom) \<le> (1\<Colon>'b\<Colon>ordered_idom)"
paulson@23449
   811
  by (metis mult_le_cancel_right1 order_refl)
paulson@23449
   812
have 2: "\<And>X3\<Colon>'b\<Colon>ordered_idom. X3 \<le> (1\<Colon>'b\<Colon>ordered_idom) * X3"
paulson@23449
   813
  by (metis 1 order_refl)
paulson@23449
   814
show "False"
paulson@23449
   815
  by (metis 0 2)
paulson@23449
   816
qed
paulson@23449
   817
 
paulson@23449
   818
lemma bigo_abs3: "O(f) = O(%x. abs(f x))"
paulson@23449
   819
  apply (rule equalityI)
paulson@23449
   820
  apply (rule bigo_elt_subset)
paulson@23449
   821
  apply (rule bigo_abs2)
paulson@23449
   822
  apply (rule bigo_elt_subset)
paulson@23449
   823
  apply (rule bigo_abs)
paulson@23449
   824
done
paulson@23449
   825
paulson@23449
   826
lemma bigo_abs4: "f =o g +o O(h) ==> 
paulson@23449
   827
    (%x. abs (f x)) =o (%x. abs (g x)) +o O(h)"
paulson@23449
   828
  apply (drule set_plus_imp_minus)
paulson@23449
   829
  apply (rule set_minus_imp_plus)
paulson@23449
   830
  apply (subst func_diff)
paulson@23449
   831
proof -
paulson@23449
   832
  assume a: "f - g : O(h)"
paulson@23449
   833
  have "(%x. abs (f x) - abs (g x)) =o O(%x. abs(abs (f x) - abs (g x)))"
paulson@23449
   834
    by (rule bigo_abs2)
paulson@23449
   835
  also have "... <= O(%x. abs (f x - g x))"
paulson@23449
   836
    apply (rule bigo_elt_subset)
paulson@23449
   837
    apply (rule bigo_bounded)
paulson@23449
   838
    apply force
paulson@23449
   839
    apply (rule allI)
paulson@23449
   840
    apply (rule abs_triangle_ineq3)
paulson@23449
   841
    done
paulson@23449
   842
  also have "... <= O(f - g)"
paulson@23449
   843
    apply (rule bigo_elt_subset)
paulson@23449
   844
    apply (subst func_diff)
paulson@23449
   845
    apply (rule bigo_abs)
paulson@23449
   846
    done
paulson@23449
   847
  also have "... <= O(h)"
wenzelm@23464
   848
    using a by (rule bigo_elt_subset)
paulson@23449
   849
  finally show "(%x. abs (f x) - abs (g x)) : O(h)".
paulson@23449
   850
qed
paulson@23449
   851
paulson@23449
   852
lemma bigo_abs5: "f =o O(g) ==> (%x. abs(f x)) =o O(g)" 
paulson@23449
   853
by (unfold bigo_def, auto)
paulson@23449
   854
paulson@23449
   855
lemma bigo_elt_subset2 [intro]: "f : g +o O(h) ==> O(f) <= O(g) + O(h)"
paulson@23449
   856
proof -
paulson@23449
   857
  assume "f : g +o O(h)"
paulson@23449
   858
  also have "... <= O(g) + O(h)"
paulson@23449
   859
    by (auto del: subsetI)
paulson@23449
   860
  also have "... = O(%x. abs(g x)) + O(%x. abs(h x))"
paulson@23449
   861
    apply (subst bigo_abs3 [symmetric])+
paulson@23449
   862
    apply (rule refl)
paulson@23449
   863
    done
paulson@23449
   864
  also have "... = O((%x. abs(g x)) + (%x. abs(h x)))"
paulson@23449
   865
    by (rule bigo_plus_eq [symmetric], auto)
paulson@23449
   866
  finally have "f : ...".
paulson@23449
   867
  then have "O(f) <= ..."
paulson@23449
   868
    by (elim bigo_elt_subset)
paulson@23449
   869
  also have "... = O(%x. abs(g x)) + O(%x. abs(h x))"
paulson@23449
   870
    by (rule bigo_plus_eq, auto)
paulson@23449
   871
  finally show ?thesis
paulson@23449
   872
    by (simp add: bigo_abs3 [symmetric])
paulson@23449
   873
qed
paulson@23449
   874
paulson@23449
   875
ML{*ResAtp.problem_name := "BigO__bigo_mult"*}
paulson@23449
   876
lemma bigo_mult [intro]: "O(f)*O(g) <= O(f * g)"
paulson@23449
   877
  apply (rule subsetI)
paulson@23449
   878
  apply (subst bigo_def)
paulson@23449
   879
  apply (auto simp del: abs_mult mult_ac
paulson@23449
   880
              simp add: bigo_alt_def set_times func_times)
paulson@23449
   881
(*sledgehammer*); 
paulson@23449
   882
  apply (rule_tac x = "c * ca" in exI)
paulson@23449
   883
  apply(rule allI)
paulson@23449
   884
  apply(erule_tac x = x in allE)+
paulson@23449
   885
  apply(subgoal_tac "c * ca * abs(f x * g x) = 
paulson@23449
   886
      (c * abs(f x)) * (ca * abs(g x))")
paulson@23449
   887
ML{*ResAtp.problem_name := "BigO__bigo_mult_simpler"*}
paulson@23449
   888
prefer 2 
paulson@23449
   889
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
   890
  apply(erule ssubst) 
paulson@23449
   891
  apply (subst abs_mult)
paulson@23449
   892
(*not qute BigO__bigo_mult_simpler_1 (a hard problem!) as abs_mult has
paulson@23449
   893
  just been done*)
paulson@23449
   894
proof (neg_clausify)
paulson@23449
   895
fix a c b ca x
paulson@23449
   896
assume 0: "(0\<Colon>'b\<Colon>ordered_idom) < (c\<Colon>'b\<Colon>ordered_idom)"
paulson@23449
   897
assume 1: "\<bar>(a\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) (x\<Colon>'a)\<bar>
paulson@23449
   898
\<le> (c\<Colon>'b\<Colon>ordered_idom) * \<bar>(f\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar>"
paulson@23449
   899
assume 2: "\<bar>(b\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) (x\<Colon>'a)\<bar>
paulson@23449
   900
\<le> (ca\<Colon>'b\<Colon>ordered_idom) * \<bar>(g\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar>"
paulson@23449
   901
assume 3: "\<not> \<bar>(a\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) (x\<Colon>'a)\<bar> *
paulson@23449
   902
  \<bar>(b\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar>
paulson@23449
   903
  \<le> (c\<Colon>'b\<Colon>ordered_idom) * \<bar>(f\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar> *
paulson@23449
   904
    ((ca\<Colon>'b\<Colon>ordered_idom) * \<bar>(g\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar>)"
paulson@23449
   905
have 4: "\<bar>c\<Colon>'b\<Colon>ordered_idom\<bar> = c"
paulson@23449
   906
  by (metis OrderedGroup.abs_of_pos 0)
paulson@23449
   907
have 5: "\<And>X1\<Colon>'b\<Colon>ordered_idom. (c\<Colon>'b\<Colon>ordered_idom) * \<bar>X1\<bar> = \<bar>c * X1\<bar>"
paulson@23449
   908
  by (metis Ring_and_Field.abs_mult 4)
paulson@23449
   909
have 6: "(0\<Colon>'b\<Colon>ordered_idom) = (1\<Colon>'b\<Colon>ordered_idom) \<or>
paulson@23449
   910
(0\<Colon>'b\<Colon>ordered_idom) < (1\<Colon>'b\<Colon>ordered_idom)"
paulson@23449
   911
  by (metis OrderedGroup.abs_not_less_zero Ring_and_Field.abs_one Ring_and_Field.linorder_neqE_ordered_idom)
paulson@23449
   912
have 7: "(0\<Colon>'b\<Colon>ordered_idom) < (1\<Colon>'b\<Colon>ordered_idom)"
paulson@23449
   913
  by (metis 6 Ring_and_Field.one_neq_zero)
paulson@23449
   914
have 8: "\<bar>1\<Colon>'b\<Colon>ordered_idom\<bar> = (1\<Colon>'b\<Colon>ordered_idom)"
paulson@23449
   915
  by (metis OrderedGroup.abs_of_pos 7)
paulson@23449
   916
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
   917
  by (metis OrderedGroup.abs_ge_zero 5)
paulson@23449
   918
have 10: "\<And>X1\<Colon>'b\<Colon>ordered_idom. X1 * (1\<Colon>'b\<Colon>ordered_idom) = X1"
paulson@23449
   919
  by (metis Ring_and_Field.mult_cancel_right2 Finite_Set.AC_mult.f.commute)
paulson@23449
   920
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
   921
  by (metis Ring_and_Field.abs_mult OrderedGroup.abs_idempotent 10)
paulson@23449
   922
have 12: "\<And>X1\<Colon>'b\<Colon>ordered_idom. \<bar>\<bar>X1\<bar>\<bar> = \<bar>X1\<bar>"
paulson@23449
   923
  by (metis 11 8 10)
paulson@23449
   924
have 13: "\<And>X1\<Colon>'b\<Colon>ordered_idom. (0\<Colon>'b\<Colon>ordered_idom) \<le> \<bar>X1\<bar>"
paulson@23449
   925
  by (metis OrderedGroup.abs_ge_zero 12)
paulson@23449
   926
have 14: "\<not> (0\<Colon>'b\<Colon>ordered_idom)
paulson@23449
   927
  \<le> (c\<Colon>'b\<Colon>ordered_idom) * \<bar>(f\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) (x\<Colon>'a)\<bar> \<or>
paulson@23449
   928
\<not> (0\<Colon>'b\<Colon>ordered_idom) \<le> \<bar>(b\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar> \<or>
paulson@23449
   929
\<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
   930
\<not> \<bar>(a\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar> \<le> c * \<bar>f x\<bar>"
paulson@23449
   931
  by (metis 3 Ring_and_Field.mult_mono)
paulson@23449
   932
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
   933
\<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
   934
\<not> \<bar>(a\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar>
paulson@23449
   935
  \<le> (c\<Colon>'b\<Colon>ordered_idom) * \<bar>(f\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar>"
paulson@23449
   936
  by (metis 14 9)
paulson@23449
   937
have 16: "\<not> \<bar>(b\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) (x\<Colon>'a)\<bar>
paulson@23449
   938
  \<le> (ca\<Colon>'b\<Colon>ordered_idom) * \<bar>(g\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar> \<or>
paulson@23449
   939
\<not> \<bar>(a\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar>
paulson@23449
   940
  \<le> (c\<Colon>'b\<Colon>ordered_idom) * \<bar>(f\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar>"
paulson@23449
   941
  by (metis 15 13)
paulson@23449
   942
have 17: "\<not> \<bar>(a\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) (x\<Colon>'a)\<bar>
paulson@23449
   943
  \<le> (c\<Colon>'b\<Colon>ordered_idom) * \<bar>(f\<Colon>'a \<Rightarrow> 'b\<Colon>ordered_idom) x\<bar>"
paulson@23449
   944
  by (metis 16 2)
paulson@23449
   945
show 18: "False"
paulson@23449
   946
  by (metis 17 1)
paulson@23449
   947
qed
paulson@23449
   948
paulson@23449
   949
paulson@23449
   950
ML{*ResAtp.problem_name := "BigO__bigo_mult2"*}
paulson@23449
   951
lemma bigo_mult2 [intro]: "f *o O(g) <= O(f * g)"
paulson@23449
   952
  apply (auto simp add: bigo_def elt_set_times_def func_times abs_mult)
paulson@23449
   953
(*sledgehammer*); 
paulson@23449
   954
  apply (rule_tac x = c in exI)
paulson@23449
   955
  apply clarify
paulson@23449
   956
  apply (drule_tac x = x in spec)
paulson@23449
   957
ML{*ResAtp.problem_name := "BigO__bigo_mult2_simpler"*}
paulson@23449
   958
(*sledgehammer*); 
paulson@23449
   959
  apply (subgoal_tac "abs(f x) * abs(b x) <= abs(f x) * (c * abs(g x))")
paulson@23449
   960
  apply (simp add: mult_ac)
paulson@23449
   961
  apply (rule mult_left_mono, assumption)
paulson@23449
   962
  apply (rule abs_ge_zero)
paulson@23449
   963
done
paulson@23449
   964
paulson@23449
   965
ML{*ResAtp.problem_name:="BigO__bigo_mult3"*}
paulson@23449
   966
lemma bigo_mult3: "f : O(h) ==> g : O(j) ==> f * g : O(h * j)"
paulson@23449
   967
by (metis bigo_mult set_times_intro subset_iff)
paulson@23449
   968
paulson@23449
   969
ML{*ResAtp.problem_name:="BigO__bigo_mult4"*}
paulson@23449
   970
lemma bigo_mult4 [intro]:"f : k +o O(h) ==> g * f : (g * k) +o O(g * h)"
paulson@23449
   971
by (metis bigo_mult2 set_plus_mono_b set_times_intro2 set_times_plus_distrib)
paulson@23449
   972
paulson@23449
   973
paulson@23449
   974
lemma bigo_mult5: "ALL x. f x ~= 0 ==>
paulson@23449
   975
    O(f * g) <= (f::'a => ('b::ordered_field)) *o O(g)"
paulson@23449
   976
proof -
paulson@23449
   977
  assume "ALL x. f x ~= 0"
paulson@23449
   978
  show "O(f * g) <= f *o O(g)"
paulson@23449
   979
  proof
paulson@23449
   980
    fix h
paulson@23449
   981
    assume "h : O(f * g)"
paulson@23449
   982
    then have "(%x. 1 / (f x)) * h : (%x. 1 / f x) *o O(f * g)"
paulson@23449
   983
      by auto
paulson@23449
   984
    also have "... <= O((%x. 1 / f x) * (f * g))"
paulson@23449
   985
      by (rule bigo_mult2)
paulson@23449
   986
    also have "(%x. 1 / f x) * (f * g) = g"
paulson@23449
   987
      apply (simp add: func_times) 
paulson@23449
   988
      apply (rule ext)
paulson@23449
   989
      apply (simp add: prems nonzero_divide_eq_eq mult_ac)
paulson@23449
   990
      done
paulson@23449
   991
    finally have "(%x. (1::'b) / f x) * h : O(g)".
paulson@23449
   992
    then have "f * ((%x. (1::'b) / f x) * h) : f *o O(g)"
paulson@23449
   993
      by auto
paulson@23449
   994
    also have "f * ((%x. (1::'b) / f x) * h) = h"
paulson@23449
   995
      apply (simp add: func_times) 
paulson@23449
   996
      apply (rule ext)
paulson@23449
   997
      apply (simp add: prems nonzero_divide_eq_eq mult_ac)
paulson@23449
   998
      done
paulson@23449
   999
    finally show "h : f *o O(g)".
paulson@23449
  1000
  qed
paulson@23449
  1001
qed
paulson@23449
  1002
paulson@23449
  1003
ML{*ResAtp.problem_name := "BigO__bigo_mult6"*}
paulson@23449
  1004
lemma bigo_mult6: "ALL x. f x ~= 0 ==>
paulson@23449
  1005
    O(f * g) = (f::'a => ('b::ordered_field)) *o O(g)"
paulson@23449
  1006
by (metis bigo_mult2 bigo_mult5 order_antisym)
paulson@23449
  1007
paulson@23449
  1008
(*proof requires relaxing relevance: 2007-01-25*)
paulson@23449
  1009
ML{*ResAtp.problem_name := "BigO__bigo_mult7"*}
paulson@23449
  1010
  declare bigo_mult6 [simp]
paulson@23449
  1011
lemma bigo_mult7: "ALL x. f x ~= 0 ==>
paulson@23449
  1012
    O(f * g) <= O(f::'a => ('b::ordered_field)) * O(g)"
paulson@23449
  1013
(*sledgehammer*)
paulson@23449
  1014
  apply (subst bigo_mult6)
paulson@23449
  1015
  apply assumption
paulson@23449
  1016
  apply (rule set_times_mono3) 
paulson@23449
  1017
  apply (rule bigo_refl)
paulson@23449
  1018
done
paulson@23449
  1019
  declare bigo_mult6 [simp del]
paulson@23449
  1020
paulson@23449
  1021
ML{*ResAtp.problem_name := "BigO__bigo_mult8"*}
paulson@23449
  1022
  declare bigo_mult7[intro!]
paulson@23449
  1023
lemma bigo_mult8: "ALL x. f x ~= 0 ==>
paulson@23449
  1024
    O(f * g) = O(f::'a => ('b::ordered_field)) * O(g)"
paulson@23449
  1025
by (metis bigo_mult bigo_mult7 order_antisym_conv)
paulson@23449
  1026
paulson@23449
  1027
lemma bigo_minus [intro]: "f : O(g) ==> - f : O(g)"
paulson@23449
  1028
  by (auto simp add: bigo_def func_minus)
paulson@23449
  1029
paulson@23449
  1030
lemma bigo_minus2: "f : g +o O(h) ==> -f : -g +o O(h)"
paulson@23449
  1031
  apply (rule set_minus_imp_plus)
paulson@23449
  1032
  apply (drule set_plus_imp_minus)
paulson@23449
  1033
  apply (drule bigo_minus)
paulson@23449
  1034
  apply (simp add: diff_minus)
paulson@23449
  1035
done
paulson@23449
  1036
paulson@23449
  1037
lemma bigo_minus3: "O(-f) = O(f)"
paulson@23449
  1038
  by (auto simp add: bigo_def func_minus abs_minus_cancel)
paulson@23449
  1039
paulson@23449
  1040
lemma bigo_plus_absorb_lemma1: "f : O(g) ==> f +o O(g) <= O(g)"
paulson@23449
  1041
proof -
paulson@23449
  1042
  assume a: "f : O(g)"
paulson@23449
  1043
  show "f +o O(g) <= O(g)"
paulson@23449
  1044
  proof -
paulson@23449
  1045
    have "f : O(f)" by auto
paulson@23449
  1046
    then have "f +o O(g) <= O(f) + O(g)"
paulson@23449
  1047
      by (auto del: subsetI)
paulson@23449
  1048
    also have "... <= O(g) + O(g)"
paulson@23449
  1049
    proof -
paulson@23449
  1050
      from a have "O(f) <= O(g)" by (auto del: subsetI)
paulson@23449
  1051
      thus ?thesis by (auto del: subsetI)
paulson@23449
  1052
    qed
paulson@23449
  1053
    also have "... <= O(g)" by (simp add: bigo_plus_idemp)
paulson@23449
  1054
    finally show ?thesis .
paulson@23449
  1055
  qed
paulson@23449
  1056
qed
paulson@23449
  1057
paulson@23449
  1058
lemma bigo_plus_absorb_lemma2: "f : O(g) ==> O(g) <= f +o O(g)"
paulson@23449
  1059
proof -
paulson@23449
  1060
  assume a: "f : O(g)"
paulson@23449
  1061
  show "O(g) <= f +o O(g)"
paulson@23449
  1062
  proof -
paulson@23449
  1063
    from a have "-f : O(g)" by auto
paulson@23449
  1064
    then have "-f +o O(g) <= O(g)" by (elim bigo_plus_absorb_lemma1)
paulson@23449
  1065
    then have "f +o (-f +o O(g)) <= f +o O(g)" by auto
paulson@23449
  1066
    also have "f +o (-f +o O(g)) = O(g)"
paulson@23449
  1067
      by (simp add: set_plus_rearranges)
paulson@23449
  1068
    finally show ?thesis .
paulson@23449
  1069
  qed
paulson@23449
  1070
qed
paulson@23449
  1071
paulson@23449
  1072
ML{*ResAtp.problem_name:="BigO__bigo_plus_absorb"*}
paulson@23449
  1073
lemma bigo_plus_absorb [simp]: "f : O(g) ==> f +o O(g) = O(g)"
paulson@23449
  1074
by (metis bigo_plus_absorb_lemma1 bigo_plus_absorb_lemma2 order_eq_iff);
paulson@23449
  1075
paulson@23449
  1076
lemma bigo_plus_absorb2 [intro]: "f : O(g) ==> A <= O(g) ==> f +o A <= O(g)"
paulson@23449
  1077
  apply (subgoal_tac "f +o A <= f +o O(g)")
paulson@23449
  1078
  apply force+
paulson@23449
  1079
done
paulson@23449
  1080
paulson@23449
  1081
lemma bigo_add_commute_imp: "f : g +o O(h) ==> g : f +o O(h)"
paulson@23449
  1082
  apply (subst set_minus_plus [symmetric])
paulson@23449
  1083
  apply (subgoal_tac "g - f = - (f - g)")
paulson@23449
  1084
  apply (erule ssubst)
paulson@23449
  1085
  apply (rule bigo_minus)
paulson@23449
  1086
  apply (subst set_minus_plus)
paulson@23449
  1087
  apply assumption
paulson@23449
  1088
  apply  (simp add: diff_minus add_ac)
paulson@23449
  1089
done
paulson@23449
  1090
paulson@23449
  1091
lemma bigo_add_commute: "(f : g +o O(h)) = (g : f +o O(h))"
paulson@23449
  1092
  apply (rule iffI)
paulson@23449
  1093
  apply (erule bigo_add_commute_imp)+
paulson@23449
  1094
done
paulson@23449
  1095
paulson@23449
  1096
lemma bigo_const1: "(%x. c) : O(%x. 1)"
paulson@23449
  1097
by (auto simp add: bigo_def mult_ac)
paulson@23449
  1098
paulson@23449
  1099
declare bigo_const1 [skolem]
paulson@23449
  1100
paulson@23449
  1101
ML{*ResAtp.problem_name:="BigO__bigo_const2"*}
paulson@23449
  1102
lemma (*bigo_const2 [intro]:*) "O(%x. c) <= O(%x. 1)"
paulson@23449
  1103
by (metis bigo_const1 bigo_elt_subset);
paulson@23449
  1104
paulson@23449
  1105
lemma bigo_const2 [intro]: "O(%x. c) <= O(%x. 1)"; 
paulson@23449
  1106
(*??FAILS because the two occurrences of COMBK have different polymorphic types
paulson@23449
  1107
proof (neg_clausify)
paulson@23449
  1108
assume 0: "\<not> O(COMBK (c\<Colon>'b\<Colon>ordered_idom)) \<subseteq> O(COMBK (1\<Colon>'b\<Colon>ordered_idom))"
paulson@23449
  1109
have 1: "COMBK (c\<Colon>'b\<Colon>ordered_idom) \<notin> O(COMBK (1\<Colon>'b\<Colon>ordered_idom))"
paulson@23449
  1110
apply (rule notI) 
paulson@23449
  1111
apply (rule 0 [THEN notE]) 
paulson@23449
  1112
apply (rule bigo_elt_subset) 
paulson@23449
  1113
apply assumption; 
paulson@23449
  1114
sorry
paulson@23449
  1115
  by (metis 0 bigo_elt_subset)  loops??
paulson@23449
  1116
show "False"
paulson@23449
  1117
  by (metis 1 bigo_const1)
paulson@23449
  1118
qed
paulson@23449
  1119
*)
paulson@23449
  1120
  apply (rule bigo_elt_subset)
paulson@23449
  1121
  apply (rule bigo_const1)
paulson@23449
  1122
done
paulson@23449
  1123
paulson@23449
  1124
declare bigo_const2 [skolem]
paulson@23449
  1125
paulson@23449
  1126
ML{*ResAtp.problem_name := "BigO__bigo_const3"*}
paulson@23449
  1127
lemma bigo_const3: "(c::'a::ordered_field) ~= 0 ==> (%x. 1) : O(%x. c)"
paulson@23449
  1128
apply (simp add: bigo_def)
paulson@23449
  1129
proof (neg_clausify)
paulson@23449
  1130
assume 0: "(c\<Colon>'a\<Colon>ordered_field) \<noteq> (0\<Colon>'a\<Colon>ordered_field)"
paulson@23519
  1131
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
  1132
have 2: "(0\<Colon>'a\<Colon>ordered_field) = \<bar>c\<Colon>'a\<Colon>ordered_field\<bar> \<or>
paulson@23449
  1133
\<not> (1\<Colon>'a\<Colon>ordered_field) \<le> (1\<Colon>'a\<Colon>ordered_field)"
paulson@23449
  1134
  by (metis 1 field_inverse)
paulson@23449
  1135
have 3: "\<bar>c\<Colon>'a\<Colon>ordered_field\<bar> = (0\<Colon>'a\<Colon>ordered_field)"
paulson@23519
  1136
  by (metis linorder_neq_iff linorder_antisym_conv1 2)
paulson@23449
  1137
have 4: "(0\<Colon>'a\<Colon>ordered_field) = (c\<Colon>'a\<Colon>ordered_field)"
paulson@23519
  1138
  by (metis 3 abs_eq_0)
paulson@23519
  1139
show "False"
paulson@23519
  1140
  by (metis 0 4)
paulson@23449
  1141
qed
paulson@23449
  1142
paulson@23449
  1143
lemma bigo_const4: "(c::'a::ordered_field) ~= 0 ==> O(%x. 1) <= O(%x. c)"
paulson@23449
  1144
by (rule bigo_elt_subset, rule bigo_const3, assumption)
paulson@23449
  1145
paulson@23449
  1146
lemma bigo_const [simp]: "(c::'a::ordered_field) ~= 0 ==> 
paulson@23449
  1147
    O(%x. c) = O(%x. 1)"
paulson@23449
  1148
by (rule equalityI, rule bigo_const2, rule bigo_const4, assumption)
paulson@23449
  1149
paulson@23449
  1150
ML{*ResAtp.problem_name := "BigO__bigo_const_mult1"*}
paulson@23449
  1151
lemma bigo_const_mult1: "(%x. c * f x) : O(f)"
paulson@23449
  1152
  apply (simp add: bigo_def abs_mult) 
paulson@23449
  1153
proof (neg_clausify)
paulson@23449
  1154
fix x
paulson@23449
  1155
assume 0: "\<And>mes_vAL\<Colon>'b.
paulson@23449
  1156
   \<not> \<bar>c\<Colon>'b\<bar> *
paulson@23449
  1157
     \<bar>(f\<Colon>'a \<Rightarrow> 'b) ((x\<Colon>'b \<Rightarrow> 'a) mes_vAL)\<bar>
paulson@23449
  1158
     \<le> mes_vAL * \<bar>f (x mes_vAL)\<bar>"
paulson@23449
  1159
have 1: "\<And>Y\<Colon>'b. Y \<le> Y"
paulson@23449
  1160
  by (metis order_refl)
paulson@23449
  1161
show 2: "False"
paulson@23449
  1162
  by (metis 0 1)
paulson@23449
  1163
qed
paulson@23449
  1164
paulson@23449
  1165
lemma bigo_const_mult2: "O(%x. c * f x) <= O(f)"
paulson@23449
  1166
by (rule bigo_elt_subset, rule bigo_const_mult1)
paulson@23449
  1167
paulson@23449
  1168
ML{*ResAtp.problem_name := "BigO__bigo_const_mult3"*}
paulson@23449
  1169
lemma bigo_const_mult3: "(c::'a::ordered_field) ~= 0 ==> f : O(%x. c * f x)"
paulson@23449
  1170
  apply (simp add: bigo_def)
paulson@23449
  1171
(*sledgehammer*); 
paulson@23449
  1172
  apply (rule_tac x = "abs(inverse c)" in exI)
paulson@23449
  1173
  apply (simp only: abs_mult [symmetric] mult_assoc [symmetric])
paulson@23449
  1174
apply (subst left_inverse) 
paulson@23449
  1175
apply (auto ); 
paulson@23449
  1176
done
paulson@23449
  1177
paulson@23449
  1178
lemma bigo_const_mult4: "(c::'a::ordered_field) ~= 0 ==> 
paulson@23449
  1179
    O(f) <= O(%x. c * f x)"
paulson@23449
  1180
by (rule bigo_elt_subset, rule bigo_const_mult3, assumption)
paulson@23449
  1181
paulson@23449
  1182
lemma bigo_const_mult [simp]: "(c::'a::ordered_field) ~= 0 ==> 
paulson@23449
  1183
    O(%x. c * f x) = O(f)"
paulson@23449
  1184
by (rule equalityI, rule bigo_const_mult2, erule bigo_const_mult4)
paulson@23449
  1185
paulson@23449
  1186
ML{*ResAtp.problem_name := "BigO__bigo_const_mult5"*}
paulson@23449
  1187
lemma bigo_const_mult5 [simp]: "(c::'a::ordered_field) ~= 0 ==> 
paulson@23449
  1188
    (%x. c) *o O(f) = O(f)"
paulson@23449
  1189
  apply (auto del: subsetI)
paulson@23449
  1190
  apply (rule order_trans)
paulson@23449
  1191
  apply (rule bigo_mult2)
paulson@23449
  1192
  apply (simp add: func_times)
paulson@23449
  1193
  apply (auto intro!: subsetI simp add: bigo_def elt_set_times_def func_times)
paulson@23449
  1194
  apply (rule_tac x = "%y. inverse c * x y" in exI)
paulson@23449
  1195
apply (rename_tac g d) 
paulson@23449
  1196
apply safe;
paulson@23449
  1197
apply (rule_tac [2] ext) 
paulson@23449
  1198
(*sledgehammer*); 
paulson@23449
  1199
  apply (simp_all del: mult_assoc add: mult_assoc [symmetric] abs_mult)
paulson@23449
  1200
  apply (rule_tac x = "abs (inverse c) * d" in exI)
paulson@23449
  1201
  apply (rule allI)
paulson@23449
  1202
  apply (subst mult_assoc)
paulson@23449
  1203
  apply (rule mult_left_mono)
paulson@23449
  1204
  apply (erule spec)
paulson@23449
  1205
apply (simp add: ); 
paulson@23449
  1206
done
paulson@23449
  1207
paulson@23449
  1208
paulson@23449
  1209
ML{*ResAtp.problem_name := "BigO__bigo_const_mult6"*}
paulson@23449
  1210
lemma bigo_const_mult6 [intro]: "(%x. c) *o O(f) <= O(f)"
paulson@23449
  1211
  apply (auto intro!: subsetI
paulson@23449
  1212
    simp add: bigo_def elt_set_times_def func_times
paulson@23449
  1213
    simp del: abs_mult mult_ac)
paulson@23449
  1214
(*sledgehammer*); 
paulson@23449
  1215
  apply (rule_tac x = "ca * (abs c)" in exI)
paulson@23449
  1216
  apply (rule allI)
paulson@23449
  1217
  apply (subgoal_tac "ca * abs(c) * abs(f x) = abs(c) * (ca * abs(f x))")
paulson@23449
  1218
  apply (erule ssubst)
paulson@23449
  1219
  apply (subst abs_mult)
paulson@23449
  1220
  apply (rule mult_left_mono)
paulson@23449
  1221
  apply (erule spec)
paulson@23449
  1222
  apply simp
paulson@23449
  1223
  apply(simp add: mult_ac)
paulson@23449
  1224
done
paulson@23449
  1225
paulson@23449
  1226
lemma bigo_const_mult7 [intro]: "f =o O(g) ==> (%x. c * f x) =o O(g)"
paulson@23449
  1227
proof -
paulson@23449
  1228
  assume "f =o O(g)"
paulson@23449
  1229
  then have "(%x. c) * f =o (%x. c) *o O(g)"
paulson@23449
  1230
    by auto
paulson@23449
  1231
  also have "(%x. c) * f = (%x. c * f x)"
paulson@23449
  1232
    by (simp add: func_times)
paulson@23449
  1233
  also have "(%x. c) *o O(g) <= O(g)"
paulson@23449
  1234
    by (auto del: subsetI)
paulson@23449
  1235
  finally show ?thesis .
paulson@23449
  1236
qed
paulson@23449
  1237
paulson@23449
  1238
lemma bigo_compose1: "f =o O(g) ==> (%x. f(k x)) =o O(%x. g(k x))"
paulson@23449
  1239
by (unfold bigo_def, auto)
paulson@23449
  1240
paulson@23449
  1241
lemma bigo_compose2: "f =o g +o O(h) ==> (%x. f(k x)) =o (%x. g(k x)) +o 
paulson@23449
  1242
    O(%x. h(k x))"
paulson@23449
  1243
  apply (simp only: set_minus_plus [symmetric] diff_minus func_minus
paulson@23449
  1244
      func_plus)
paulson@23449
  1245
  apply (erule bigo_compose1)
paulson@23449
  1246
done
paulson@23449
  1247
paulson@23449
  1248
subsection {* Setsum *}
paulson@23449
  1249
paulson@23449
  1250
lemma bigo_setsum_main: "ALL x. ALL y : A x. 0 <= h x y ==> 
paulson@23449
  1251
    EX c. ALL x. ALL y : A x. abs(f x y) <= c * (h x y) ==>
paulson@23449
  1252
      (%x. SUM y : A x. f x y) =o O(%x. SUM y : A x. h x y)"  
paulson@23449
  1253
  apply (auto simp add: bigo_def)
paulson@23449
  1254
  apply (rule_tac x = "abs c" in exI)
paulson@23449
  1255
  apply (subst abs_of_nonneg) back back
paulson@23449
  1256
  apply (rule setsum_nonneg)
paulson@23449
  1257
  apply force
paulson@23449
  1258
  apply (subst setsum_right_distrib)
paulson@23449
  1259
  apply (rule allI)
paulson@23449
  1260
  apply (rule order_trans)
paulson@23449
  1261
  apply (rule setsum_abs)
paulson@23449
  1262
  apply (rule setsum_mono)
paulson@23449
  1263
apply (blast intro: order_trans mult_right_mono abs_ge_self) 
paulson@23449
  1264
done
paulson@23449
  1265
paulson@23449
  1266
ML{*ResAtp.problem_name := "BigO__bigo_setsum1"*}
paulson@23449
  1267
lemma bigo_setsum1: "ALL x y. 0 <= h x y ==> 
paulson@23449
  1268
    EX c. ALL x y. abs(f x y) <= c * (h x y) ==>
paulson@23449
  1269
      (%x. SUM y : A x. f x y) =o O(%x. SUM y : A x. h x y)"
paulson@23449
  1270
  apply (rule bigo_setsum_main)
paulson@23449
  1271
(*sledgehammer*); 
paulson@23449
  1272
  apply force
paulson@23449
  1273
  apply clarsimp
paulson@23449
  1274
  apply (rule_tac x = c in exI)
paulson@23449
  1275
  apply force
paulson@23449
  1276
done
paulson@23449
  1277
paulson@23449
  1278
lemma bigo_setsum2: "ALL y. 0 <= h y ==> 
paulson@23449
  1279
    EX c. ALL y. abs(f y) <= c * (h y) ==>
paulson@23449
  1280
      (%x. SUM y : A x. f y) =o O(%x. SUM y : A x. h y)"
paulson@23449
  1281
by (rule bigo_setsum1, auto)  
paulson@23449
  1282
paulson@23449
  1283
ML{*ResAtp.problem_name := "BigO__bigo_setsum3"*}
paulson@23449
  1284
lemma bigo_setsum3: "f =o O(h) ==>
paulson@23449
  1285
    (%x. SUM y : A x. (l x y) * f(k x y)) =o
paulson@23449
  1286
      O(%x. SUM y : A x. abs(l x y * h(k x y)))"
paulson@23449
  1287
  apply (rule bigo_setsum1)
paulson@23449
  1288
  apply (rule allI)+
paulson@23449
  1289
  apply (rule abs_ge_zero)
paulson@23449
  1290
  apply (unfold bigo_def)
paulson@23449
  1291
  apply (auto simp add: abs_mult);
paulson@23449
  1292
(*sledgehammer*); 
paulson@23449
  1293
  apply (rule_tac x = c in exI)
paulson@23449
  1294
  apply (rule allI)+
paulson@23449
  1295
  apply (subst mult_left_commute)
paulson@23449
  1296
  apply (rule mult_left_mono)
paulson@23449
  1297
  apply (erule spec)
paulson@23449
  1298
  apply (rule abs_ge_zero)
paulson@23449
  1299
done
paulson@23449
  1300
paulson@23449
  1301
lemma bigo_setsum4: "f =o g +o O(h) ==>
paulson@23449
  1302
    (%x. SUM y : A x. l x y * f(k x y)) =o
paulson@23449
  1303
      (%x. SUM y : A x. l x y * g(k x y)) +o
paulson@23449
  1304
        O(%x. SUM y : A x. abs(l x y * h(k x y)))"
paulson@23449
  1305
  apply (rule set_minus_imp_plus)
paulson@23449
  1306
  apply (subst func_diff)
paulson@23449
  1307
  apply (subst setsum_subtractf [symmetric])
paulson@23449
  1308
  apply (subst right_diff_distrib [symmetric])
paulson@23449
  1309
  apply (rule bigo_setsum3)
paulson@23449
  1310
  apply (subst func_diff [symmetric])
paulson@23449
  1311
  apply (erule set_plus_imp_minus)
paulson@23449
  1312
done
paulson@23449
  1313
paulson@23449
  1314
ML{*ResAtp.problem_name := "BigO__bigo_setsum5"*}
paulson@23449
  1315
lemma bigo_setsum5: "f =o O(h) ==> ALL x y. 0 <= l x y ==> 
paulson@23449
  1316
    ALL x. 0 <= h x ==>
paulson@23449
  1317
      (%x. SUM y : A x. (l x y) * f(k x y)) =o
paulson@23449
  1318
        O(%x. SUM y : A x. (l x y) * h(k x y))" 
paulson@23449
  1319
  apply (subgoal_tac "(%x. SUM y : A x. (l x y) * h(k x y)) = 
paulson@23449
  1320
      (%x. SUM y : A x. abs((l x y) * h(k x y)))")
paulson@23449
  1321
  apply (erule ssubst)
paulson@23449
  1322
  apply (erule bigo_setsum3)
paulson@23449
  1323
  apply (rule ext)
paulson@23449
  1324
  apply (rule setsum_cong2)
paulson@23449
  1325
  apply (thin_tac "f \<in> O(h)") 
paulson@23449
  1326
(*sledgehammer*); 
paulson@23449
  1327
  apply (subst abs_of_nonneg)
paulson@23449
  1328
  apply (rule mult_nonneg_nonneg)
paulson@23449
  1329
  apply auto
paulson@23449
  1330
done
paulson@23449
  1331
paulson@23449
  1332
lemma bigo_setsum6: "f =o g +o O(h) ==> ALL x y. 0 <= l x y ==>
paulson@23449
  1333
    ALL x. 0 <= h x ==>
paulson@23449
  1334
      (%x. SUM y : A x. (l x y) * f(k x y)) =o
paulson@23449
  1335
        (%x. SUM y : A x. (l x y) * g(k x y)) +o
paulson@23449
  1336
          O(%x. SUM y : A x. (l x y) * h(k x y))" 
paulson@23449
  1337
  apply (rule set_minus_imp_plus)
paulson@23449
  1338
  apply (subst func_diff)
paulson@23449
  1339
  apply (subst setsum_subtractf [symmetric])
paulson@23449
  1340
  apply (subst right_diff_distrib [symmetric])
paulson@23449
  1341
  apply (rule bigo_setsum5)
paulson@23449
  1342
  apply (subst func_diff [symmetric])
paulson@23449
  1343
  apply (drule set_plus_imp_minus)
paulson@23449
  1344
  apply auto
paulson@23449
  1345
done
paulson@23449
  1346
paulson@23449
  1347
subsection {* Misc useful stuff *}
paulson@23449
  1348
paulson@23449
  1349
lemma bigo_useful_intro: "A <= O(f) ==> B <= O(f) ==>
paulson@23449
  1350
  A + B <= O(f)"
paulson@23449
  1351
  apply (subst bigo_plus_idemp [symmetric])
paulson@23449
  1352
  apply (rule set_plus_mono2)
paulson@23449
  1353
  apply assumption+
paulson@23449
  1354
done
paulson@23449
  1355
paulson@23449
  1356
lemma bigo_useful_add: "f =o O(h) ==> g =o O(h) ==> f + g =o O(h)"
paulson@23449
  1357
  apply (subst bigo_plus_idemp [symmetric])
paulson@23449
  1358
  apply (rule set_plus_intro)
paulson@23449
  1359
  apply assumption+
paulson@23449
  1360
done
paulson@23449
  1361
  
paulson@23449
  1362
lemma bigo_useful_const_mult: "(c::'a::ordered_field) ~= 0 ==> 
paulson@23449
  1363
    (%x. c) * f =o O(h) ==> f =o O(h)"
paulson@23449
  1364
  apply (rule subsetD)
paulson@23449
  1365
  apply (subgoal_tac "(%x. 1 / c) *o O(h) <= O(h)")
paulson@23449
  1366
  apply assumption
paulson@23449
  1367
  apply (rule bigo_const_mult6)
paulson@23449
  1368
  apply (subgoal_tac "f = (%x. 1 / c) * ((%x. c) * f)")
paulson@23449
  1369
  apply (erule ssubst)
paulson@23449
  1370
  apply (erule set_times_intro2)
paulson@23449
  1371
  apply (simp add: func_times) 
paulson@23449
  1372
done
paulson@23449
  1373
paulson@23449
  1374
ML{*ResAtp.problem_name := "BigO__bigo_fix"*}
paulson@23449
  1375
lemma bigo_fix: "(%x. f ((x::nat) + 1)) =o O(%x. h(x + 1)) ==> f 0 = 0 ==>
paulson@23449
  1376
    f =o O(h)"
paulson@23449
  1377
  apply (simp add: bigo_alt_def)
paulson@23449
  1378
(*sledgehammer*); 
paulson@23449
  1379
  apply clarify
paulson@23449
  1380
  apply (rule_tac x = c in exI)
paulson@23449
  1381
  apply safe
paulson@23449
  1382
  apply (case_tac "x = 0")
paulson@23816
  1383
apply (metis OrderedGroup.abs_ge_zero  OrderedGroup.abs_zero  order_less_le  Ring_and_Field.split_mult_pos_le) 
paulson@23449
  1384
  apply (subgoal_tac "x = Suc (x - 1)")
paulson@23816
  1385
  apply metis
paulson@23449
  1386
  apply simp
paulson@23449
  1387
  done
paulson@23449
  1388
paulson@23449
  1389
paulson@23449
  1390
lemma bigo_fix2: 
paulson@23449
  1391
    "(%x. f ((x::nat) + 1)) =o (%x. g(x + 1)) +o O(%x. h(x + 1)) ==> 
paulson@23449
  1392
       f 0 = g 0 ==> f =o g +o O(h)"
paulson@23449
  1393
  apply (rule set_minus_imp_plus)
paulson@23449
  1394
  apply (rule bigo_fix)
paulson@23449
  1395
  apply (subst func_diff)
paulson@23449
  1396
  apply (subst func_diff [symmetric])
paulson@23449
  1397
  apply (rule set_plus_imp_minus)
paulson@23449
  1398
  apply simp
paulson@23449
  1399
  apply (simp add: func_diff)
paulson@23449
  1400
done
paulson@23449
  1401
paulson@23449
  1402
subsection {* Less than or equal to *}
paulson@23449
  1403
paulson@23449
  1404
constdefs 
paulson@23449
  1405
  lesso :: "('a => 'b::ordered_idom) => ('a => 'b) => ('a => 'b)"
paulson@23449
  1406
      (infixl "<o" 70)
paulson@23449
  1407
  "f <o g == (%x. max (f x - g x) 0)"
paulson@23449
  1408
paulson@23449
  1409
lemma bigo_lesseq1: "f =o O(h) ==> ALL x. abs (g x) <= abs (f x) ==>
paulson@23449
  1410
    g =o O(h)"
paulson@23449
  1411
  apply (unfold bigo_def)
paulson@23449
  1412
  apply clarsimp
paulson@23449
  1413
apply (blast intro: order_trans) 
paulson@23449
  1414
done
paulson@23449
  1415
paulson@23449
  1416
lemma bigo_lesseq2: "f =o O(h) ==> ALL x. abs (g x) <= f x ==>
paulson@23449
  1417
      g =o O(h)"
paulson@23449
  1418
  apply (erule bigo_lesseq1)
paulson@23449
  1419
apply (blast intro: abs_ge_self order_trans) 
paulson@23449
  1420
done
paulson@23449
  1421
paulson@23449
  1422
lemma bigo_lesseq3: "f =o O(h) ==> ALL x. 0 <= g x ==> ALL x. g x <= f x ==>
paulson@23449
  1423
      g =o O(h)"
paulson@23449
  1424
  apply (erule bigo_lesseq2)
paulson@23449
  1425
  apply (rule allI)
paulson@23449
  1426
  apply (subst abs_of_nonneg)
paulson@23449
  1427
  apply (erule spec)+
paulson@23449
  1428
done
paulson@23449
  1429
paulson@23449
  1430
lemma bigo_lesseq4: "f =o O(h) ==>
paulson@23449
  1431
    ALL x. 0 <= g x ==> ALL x. g x <= abs (f x) ==>
paulson@23449
  1432
      g =o O(h)"
paulson@23449
  1433
  apply (erule bigo_lesseq1)
paulson@23449
  1434
  apply (rule allI)
paulson@23449
  1435
  apply (subst abs_of_nonneg)
paulson@23449
  1436
  apply (erule spec)+
paulson@23449
  1437
done
paulson@23449
  1438
paulson@23449
  1439
ML{*ResAtp.problem_name:="BigO__bigo_lesso1"*}
paulson@23449
  1440
lemma bigo_lesso1: "ALL x. f x <= g x ==> f <o g =o O(h)"
paulson@23449
  1441
  apply (unfold lesso_def)
paulson@23449
  1442
  apply (subgoal_tac "(%x. max (f x - g x) 0) = 0")
paulson@23449
  1443
(*
paulson@23449
  1444
?? abstractions don't work: abstraction function gets the wrong type?
paulson@23449
  1445
proof (neg_clausify)
paulson@23449
  1446
assume 0: "llabs_subgoal_1 f g = 0"
paulson@23449
  1447
assume 1: "llabs_subgoal_1 f g \<notin> O(h)"
paulson@23449
  1448
show "False"
paulson@23449
  1449
  by (metis 1 0 bigo_zero)
paulson@23449
  1450
*)
paulson@23449
  1451
  apply (erule ssubst)
paulson@23449
  1452
  apply (rule bigo_zero)
paulson@23449
  1453
  apply (unfold func_zero)
paulson@23449
  1454
  apply (rule ext)
paulson@23449
  1455
  apply (simp split: split_max)
paulson@23449
  1456
done
paulson@23449
  1457
paulson@23449
  1458
paulson@23449
  1459
ML{*ResAtp.problem_name := "BigO__bigo_lesso2"*}
paulson@23449
  1460
lemma bigo_lesso2: "f =o g +o O(h) ==>
paulson@23449
  1461
    ALL x. 0 <= k x ==> ALL x. k x <= f x ==>
paulson@23449
  1462
      k <o g =o O(h)"
paulson@23449
  1463
  apply (unfold lesso_def)
paulson@23449
  1464
  apply (rule bigo_lesseq4)
paulson@23449
  1465
  apply (erule set_plus_imp_minus)
paulson@23449
  1466
  apply (rule allI)
paulson@23449
  1467
  apply (rule le_maxI2)
paulson@23449
  1468
  apply (rule allI)
paulson@23449
  1469
  apply (subst func_diff)
paulson@23449
  1470
apply (erule thin_rl)
paulson@23449
  1471
(*sledgehammer*);  
paulson@23449
  1472
  apply (case_tac "0 <= k x - g x")
paulson@24545
  1473
  prefer 2 (*re-order subgoals because I don't know what to put after a structured proof*)
paulson@24545
  1474
   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
  1475
proof (neg_clausify)
paulson@24545
  1476
fix x
paulson@24545
  1477
assume 0: "\<And>A. k A \<le> f A"
paulson@24545
  1478
have 1: "\<And>(X1\<Colon>'b\<Colon>ordered_idom) X2. \<not> max X1 X2 < X1"
paulson@24545
  1479
  by (metis linorder_not_less le_maxI1)  (*sort inserted by hand*)
paulson@24545
  1480
assume 2: "(0\<Colon>'b) \<le> k x - g x"
paulson@24545
  1481
have 3: "\<not> k x - g x < (0\<Colon>'b)"
paulson@24545
  1482
  by (metis 2 linorder_not_less)
paulson@24545
  1483
have 4: "\<And>X1 X2. min X1 (k X2) \<le> f X2"
paulson@24545
  1484
  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
  1485
have 5: "\<bar>g x - f x\<bar> = f x - g x"
paulson@24545
  1486
  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
  1487
have 6: "max (0\<Colon>'b) (k x - g x) = k x - g x"
paulson@24545
  1488
  by (metis min_max.less_eq_less_sup.le_iff_sup 2)
paulson@24545
  1489
assume 7: "\<not> max (k x - g x) (0\<Colon>'b) \<le> \<bar>f x - g x\<bar>"
paulson@24545
  1490
have 8: "\<not> k x - g x \<le> f x - g x"
paulson@24545
  1491
  by (metis 5 abs_minus_commute 7 min_max.less_eq_less_sup.sup_commute 6)
paulson@24545
  1492
show "False"
paulson@24545
  1493
  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
  1494
qed
paulson@23449
  1495
paulson@23449
  1496
ML{*ResAtp.problem_name := "BigO__bigo_lesso3"*}
paulson@23449
  1497
lemma bigo_lesso3: "f =o g +o O(h) ==>
paulson@23449
  1498
    ALL x. 0 <= k x ==> ALL x. g x <= k x ==>
paulson@23449
  1499
      f <o k =o O(h)"
paulson@23449
  1500
  apply (unfold lesso_def)
paulson@23449
  1501
  apply (rule bigo_lesseq4)
paulson@23449
  1502
  apply (erule set_plus_imp_minus)
paulson@23449
  1503
  apply (rule allI)
paulson@23449
  1504
  apply (rule le_maxI2)
paulson@23449
  1505
  apply (rule allI)
paulson@23449
  1506
  apply (subst func_diff)
paulson@23449
  1507
apply (erule thin_rl) 
paulson@23449
  1508
(*sledgehammer*); 
paulson@23449
  1509
  apply (case_tac "0 <= f x - k x")
paulson@23449
  1510
  apply (simp del: compare_rls diff_minus);
paulson@23449
  1511
  apply (subst abs_of_nonneg)
paulson@23449
  1512
  apply (drule_tac x = x in spec) back
paulson@23449
  1513
ML{*ResAtp.problem_name := "BigO__bigo_lesso3_simpler"*}
paulson@24545
  1514
apply (metis diff_less_0_iff_less linorder_not_le not_leE uminus_add_conv_diff xt1(12) xt1(6))
paulson@24545
  1515
apply (metis add_minus_cancel diff_le_eq le_diff_eq uminus_add_conv_diff)
paulson@24545
  1516
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
  1517
done
paulson@23449
  1518
paulson@23449
  1519
lemma bigo_lesso4: "f <o g =o O(k::'a=>'b::ordered_field) ==>
paulson@23449
  1520
    g =o h +o O(k) ==> f <o h =o O(k)"
paulson@23449
  1521
  apply (unfold lesso_def)
paulson@23449
  1522
  apply (drule set_plus_imp_minus)
paulson@23449
  1523
  apply (drule bigo_abs5) back
paulson@23449
  1524
  apply (simp add: func_diff)
paulson@23449
  1525
  apply (drule bigo_useful_add)
paulson@23449
  1526
  apply assumption
paulson@23449
  1527
  apply (erule bigo_lesseq2) back
paulson@23449
  1528
  apply (rule allI)
paulson@23449
  1529
  apply (auto simp add: func_plus func_diff compare_rls 
paulson@23449
  1530
    split: split_max abs_split)
paulson@23449
  1531
done
paulson@23449
  1532
paulson@23449
  1533
ML{*ResAtp.problem_name := "BigO__bigo_lesso5"*}
paulson@23449
  1534
lemma bigo_lesso5: "f <o g =o O(h) ==>
paulson@23449
  1535
    EX C. ALL x. f x <= g x + C * abs(h x)"
paulson@23449
  1536
  apply (simp only: lesso_def bigo_alt_def)
paulson@23449
  1537
  apply clarsimp
paulson@24545
  1538
  apply (metis abs_if abs_mult add_commute diff_le_eq less_not_permute mult_commute)  
paulson@23449
  1539
done
paulson@23449
  1540
paulson@23449
  1541
end