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