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