src/HOL/Metis_Examples/Big_O.thy
author blanchet
Mon Jan 30 17:15:59 2012 +0100 (2012-01-30)
changeset 46369 9ac0c64ad8e7
parent 46364 abab10d1f4a3
child 46644 bd03e0890699
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@46369
   200
blanchet@45575
   201
apply (rule conjI)
blanchet@45575
   202
 apply (metis less_max_iff_disj)
blanchet@45575
   203
apply clarify
blanchet@45575
   204
apply (drule_tac x = "xa" in spec)+
blanchet@45575
   205
apply (subgoal_tac "0 <= f xa + g xa")
blanchet@45575
   206
 apply (simp add: ring_distribs)
blanchet@45575
   207
 apply (subgoal_tac "abs (a xa + b xa) <= abs (a xa) + abs (b xa)")
blanchet@45575
   208
  apply (subgoal_tac "abs (a xa) + abs (b xa) <=
blanchet@45575
   209
           max c ca * f xa + max c ca * g xa")
blanchet@45575
   210
   apply (metis order_trans)
paulson@23449
   211
  defer 1
blanchet@45575
   212
  apply (metis abs_triangle_ineq)
blanchet@45575
   213
 apply (metis add_nonneg_nonneg)
blanchet@46369
   214
by (metis (no_types) add_mono le_maxI2 linorder_linear min_max.sup_absorb1
blanchet@46369
   215
          mult_right_mono xt1(6))
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@46369
   237
 apply (metis add_le_cancel_left diff_add_cancel diff_self minus_apply
blanchet@46369
   238
              comm_semiring_1_class.normalizing_semiring_rules(24))
blanchet@46369
   239
by (metis add_le_cancel_left diff_add_cancel func_plus le_fun_def
blanchet@46369
   240
          comm_semiring_1_class.normalizing_semiring_rules(24))
paulson@23449
   241
blanchet@45575
   242
lemma bigo_abs: "(\<lambda>x. abs(f x)) =o O(f)"
blanchet@36561
   243
apply (unfold bigo_def)
blanchet@36561
   244
apply auto
hoelzl@36844
   245
by (metis mult_1 order_refl)
paulson@23449
   246
blanchet@45575
   247
lemma bigo_abs2: "f =o O(\<lambda>x. abs(f x))"
blanchet@36561
   248
apply (unfold bigo_def)
blanchet@36561
   249
apply auto
hoelzl@36844
   250
by (metis mult_1 order_refl)
blanchet@43197
   251
blanchet@45575
   252
lemma bigo_abs3: "O(f) = O(\<lambda>x. abs(f x))"
blanchet@36561
   253
proof -
blanchet@36561
   254
  have F1: "\<forall>v u. u \<in> O(v) \<longrightarrow> O(u) \<subseteq> O(v)" by (metis bigo_elt_subset)
blanchet@36561
   255
  have F2: "\<forall>u. (\<lambda>R. \<bar>u R\<bar>) \<in> O(u)" by (metis bigo_abs)
blanchet@36561
   256
  have "\<forall>u. u \<in> O(\<lambda>R. \<bar>u R\<bar>)" by (metis bigo_abs2)
blanchet@36561
   257
  thus "O(f) = O(\<lambda>x. \<bar>f x\<bar>)" using F1 F2 by auto
blanchet@43197
   258
qed
paulson@23449
   259
blanchet@45575
   260
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
   261
  apply (drule set_plus_imp_minus)
paulson@23449
   262
  apply (rule set_minus_imp_plus)
berghofe@26814
   263
  apply (subst fun_diff_def)
paulson@23449
   264
proof -
paulson@23449
   265
  assume a: "f - g : O(h)"
blanchet@45575
   266
  have "(\<lambda>x. abs (f x) - abs (g x)) =o O(\<lambda>x. abs(abs (f x) - abs (g x)))"
paulson@23449
   267
    by (rule bigo_abs2)
blanchet@45575
   268
  also have "... <= O(\<lambda>x. abs (f x - g x))"
paulson@23449
   269
    apply (rule bigo_elt_subset)
paulson@23449
   270
    apply (rule bigo_bounded)
blanchet@46369
   271
     apply (metis abs_ge_zero)
blanchet@46369
   272
    by (metis abs_triangle_ineq3)
paulson@23449
   273
  also have "... <= O(f - g)"
paulson@23449
   274
    apply (rule bigo_elt_subset)
berghofe@26814
   275
    apply (subst fun_diff_def)
paulson@23449
   276
    apply (rule bigo_abs)
paulson@23449
   277
    done
paulson@23449
   278
  also have "... <= O(h)"
wenzelm@23464
   279
    using a by (rule bigo_elt_subset)
blanchet@45575
   280
  finally show "(\<lambda>x. abs (f x) - abs (g x)) : O(h)".
paulson@23449
   281
qed
paulson@23449
   282
blanchet@45575
   283
lemma bigo_abs5: "f =o O(g) \<Longrightarrow> (\<lambda>x. abs(f x)) =o O(g)"
paulson@23449
   284
by (unfold bigo_def, auto)
paulson@23449
   285
blanchet@45575
   286
lemma bigo_elt_subset2 [intro]: "f : g +o O(h) \<Longrightarrow> O(f) <= O(g) \<oplus> O(h)"
paulson@23449
   287
proof -
paulson@23449
   288
  assume "f : g +o O(h)"
berghofe@26814
   289
  also have "... <= O(g) \<oplus> O(h)"
paulson@23449
   290
    by (auto del: subsetI)
blanchet@45575
   291
  also have "... = O(\<lambda>x. abs(g x)) \<oplus> O(\<lambda>x. abs(h x))"
blanchet@46369
   292
    by (metis bigo_abs3)
blanchet@45575
   293
  also have "... = O((\<lambda>x. abs(g x)) + (\<lambda>x. abs(h x)))"
paulson@23449
   294
    by (rule bigo_plus_eq [symmetric], auto)
paulson@23449
   295
  finally have "f : ...".
paulson@23449
   296
  then have "O(f) <= ..."
paulson@23449
   297
    by (elim bigo_elt_subset)
blanchet@45575
   298
  also have "... = O(\<lambda>x. abs(g x)) \<oplus> O(\<lambda>x. abs(h x))"
paulson@23449
   299
    by (rule bigo_plus_eq, auto)
paulson@23449
   300
  finally show ?thesis
paulson@23449
   301
    by (simp add: bigo_abs3 [symmetric])
paulson@23449
   302
qed
paulson@23449
   303
blanchet@46369
   304
lemma bigo_mult [intro]: "O(f) \<otimes> O(g) <= O(f * g)"
blanchet@46369
   305
apply (rule subsetI)
blanchet@46369
   306
apply (subst bigo_def)
blanchet@46369
   307
apply (auto simp del: abs_mult mult_ac
blanchet@46369
   308
            simp add: bigo_alt_def set_times_def func_times)
blanchet@45575
   309
(* sledgehammer *)
blanchet@46369
   310
apply (rule_tac x = "c * ca" in exI)
blanchet@46369
   311
apply (rule allI)
blanchet@46369
   312
apply (erule_tac x = x in allE)+
blanchet@46369
   313
apply (subgoal_tac "c * ca * abs (f x * g x) = (c * abs(f x)) * (ca * abs (g x))")
blanchet@46369
   314
 apply (metis (no_types) abs_ge_zero abs_mult mult_mono')
blanchet@46369
   315
by (metis mult_assoc mult_left_commute abs_of_pos mult_left_commute abs_mult)
paulson@23449
   316
paulson@23449
   317
lemma bigo_mult2 [intro]: "f *o O(g) <= O(f * g)"
blanchet@46369
   318
by (metis bigo_mult bigo_refl set_times_mono3 subset_trans)
paulson@23449
   319
blanchet@45575
   320
lemma bigo_mult3: "f : O(h) \<Longrightarrow> g : O(j) \<Longrightarrow> f * g : O(h * j)"
blanchet@36561
   321
by (metis bigo_mult set_rev_mp set_times_intro)
paulson@23449
   322
blanchet@45575
   323
lemma bigo_mult4 [intro]:"f : k +o O(h) \<Longrightarrow> g * f : (g * k) +o O(g * h)"
paulson@23449
   324
by (metis bigo_mult2 set_plus_mono_b set_times_intro2 set_times_plus_distrib)
paulson@23449
   325
blanchet@45575
   326
lemma bigo_mult5: "\<forall>x. f x ~= 0 \<Longrightarrow>
blanchet@45575
   327
    O(f * g) <= (f\<Colon>'a => ('b\<Colon>{linordered_field,number_ring})) *o O(g)"
paulson@23449
   328
proof -
blanchet@45575
   329
  assume a: "\<forall>x. f x ~= 0"
paulson@23449
   330
  show "O(f * g) <= f *o O(g)"
paulson@23449
   331
  proof
paulson@23449
   332
    fix h
wenzelm@41541
   333
    assume h: "h : O(f * g)"
blanchet@45575
   334
    then have "(\<lambda>x. 1 / (f x)) * h : (\<lambda>x. 1 / f x) *o O(f * g)"
paulson@23449
   335
      by auto
blanchet@45575
   336
    also have "... <= O((\<lambda>x. 1 / f x) * (f * g))"
paulson@23449
   337
      by (rule bigo_mult2)
blanchet@45575
   338
    also have "(\<lambda>x. 1 / f x) * (f * g) = g"
blanchet@43197
   339
      apply (simp add: func_times)
blanchet@46369
   340
      by (metis (lifting, no_types) a ext mult_ac(2) nonzero_divide_eq_eq)
blanchet@45575
   341
    finally have "(\<lambda>x. (1\<Colon>'b) / f x) * h : O(g)".
blanchet@45575
   342
    then have "f * ((\<lambda>x. (1\<Colon>'b) / f x) * h) : f *o O(g)"
paulson@23449
   343
      by auto
blanchet@45575
   344
    also have "f * ((\<lambda>x. (1\<Colon>'b) / f x) * h) = h"
blanchet@43197
   345
      apply (simp add: func_times)
blanchet@46369
   346
      by (metis (lifting, no_types) a eq_divide_imp ext
blanchet@46369
   347
                comm_semiring_1_class.normalizing_semiring_rules(7))
paulson@23449
   348
    finally show "h : f *o O(g)".
paulson@23449
   349
  qed
paulson@23449
   350
qed
paulson@23449
   351
blanchet@46369
   352
lemma bigo_mult6:
blanchet@46369
   353
"\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) = (f\<Colon>'a \<Rightarrow> ('b\<Colon>{linordered_field,number_ring})) *o O(g)"
paulson@23449
   354
by (metis bigo_mult2 bigo_mult5 order_antisym)
paulson@23449
   355
paulson@23449
   356
(*proof requires relaxing relevance: 2007-01-25*)
blanchet@45705
   357
declare bigo_mult6 [simp]
blanchet@45705
   358
blanchet@46369
   359
lemma bigo_mult7:
blanchet@46369
   360
"\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) \<le> O(f\<Colon>'a \<Rightarrow> ('b\<Colon>{linordered_field,number_ring})) \<otimes> O(g)"
blanchet@46369
   361
by (metis bigo_refl bigo_mult6 set_times_mono3)
paulson@23449
   362
blanchet@45575
   363
declare bigo_mult6 [simp del]
blanchet@45575
   364
declare bigo_mult7 [intro!]
blanchet@45575
   365
blanchet@46369
   366
lemma bigo_mult8:
blanchet@46369
   367
"\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) = O(f\<Colon>'a \<Rightarrow> ('b\<Colon>{linordered_field,number_ring})) \<otimes> O(g)"
paulson@23449
   368
by (metis bigo_mult bigo_mult7 order_antisym_conv)
paulson@23449
   369
blanchet@45575
   370
lemma bigo_minus [intro]: "f : O(g) \<Longrightarrow> - f : O(g)"
blanchet@46369
   371
by (auto simp add: bigo_def fun_Compl_def)
paulson@23449
   372
blanchet@45575
   373
lemma bigo_minus2: "f : g +o O(h) \<Longrightarrow> -f : -g +o O(h)"
blanchet@46369
   374
by (metis (no_types) bigo_elt_subset bigo_minus bigo_mult4 bigo_refl
blanchet@46369
   375
          comm_semiring_1_class.normalizing_semiring_rules(11) minus_mult_left
blanchet@46369
   376
          set_plus_mono_b)
paulson@23449
   377
paulson@23449
   378
lemma bigo_minus3: "O(-f) = O(f)"
blanchet@46369
   379
by (metis bigo_elt_subset bigo_minus bigo_refl equalityI minus_minus)
paulson@23449
   380
blanchet@46369
   381
lemma bigo_plus_absorb_lemma1: "f : O(g) \<Longrightarrow> f +o O(g) \<le> O(g)"
blanchet@46369
   382
by (metis bigo_plus_idemp set_plus_mono3)
paulson@23449
   383
blanchet@46369
   384
lemma bigo_plus_absorb_lemma2: "f : O(g) \<Longrightarrow> O(g) \<le> f +o O(g)"
blanchet@46369
   385
by (metis (no_types) bigo_minus bigo_plus_absorb_lemma1 right_minus
blanchet@46369
   386
          set_plus_mono_b set_plus_rearrange2 set_zero_plus subsetI)
paulson@23449
   387
blanchet@45575
   388
lemma bigo_plus_absorb [simp]: "f : O(g) \<Longrightarrow> f +o O(g) = O(g)"
paulson@41865
   389
by (metis bigo_plus_absorb_lemma1 bigo_plus_absorb_lemma2 order_eq_iff)
paulson@23449
   390
blanchet@46369
   391
lemma bigo_plus_absorb2 [intro]: "f : O(g) \<Longrightarrow> A <= O(g) \<Longrightarrow> f +o A \<le> O(g)"
blanchet@46369
   392
by (metis bigo_plus_absorb set_plus_mono)
paulson@23449
   393
blanchet@45575
   394
lemma bigo_add_commute_imp: "f : g +o O(h) \<Longrightarrow> g : f +o O(h)"
blanchet@46369
   395
by (metis bigo_minus minus_diff_eq set_plus_imp_minus set_minus_plus)
paulson@23449
   396
paulson@23449
   397
lemma bigo_add_commute: "(f : g +o O(h)) = (g : f +o O(h))"
blanchet@46369
   398
by (metis bigo_add_commute_imp)
paulson@23449
   399
blanchet@45575
   400
lemma bigo_const1: "(\<lambda>x. c) : O(\<lambda>x. 1)"
paulson@23449
   401
by (auto simp add: bigo_def mult_ac)
paulson@23449
   402
blanchet@46369
   403
lemma bigo_const2 [intro]: "O(\<lambda>x. c) \<le> O(\<lambda>x. 1)"
paulson@41865
   404
by (metis bigo_const1 bigo_elt_subset)
paulson@23449
   405
blanchet@45575
   406
lemma bigo_const3: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow> (\<lambda>x. 1) : O(\<lambda>x. c)"
paulson@23449
   407
apply (simp add: bigo_def)
blanchet@36561
   408
by (metis abs_eq_0 left_inverse order_refl)
paulson@23449
   409
blanchet@45575
   410
lemma bigo_const4: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow> O(\<lambda>x. 1) <= O(\<lambda>x. c)"
blanchet@46369
   411
by (metis bigo_elt_subset bigo_const3)
paulson@23449
   412
blanchet@45575
   413
lemma bigo_const [simp]: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow>
blanchet@45575
   414
    O(\<lambda>x. c) = O(\<lambda>x. 1)"
blanchet@46369
   415
by (metis bigo_const2 bigo_const4 equalityI)
paulson@23449
   416
blanchet@45575
   417
lemma bigo_const_mult1: "(\<lambda>x. c * f x) : O(f)"
blanchet@46369
   418
apply (simp add: bigo_def abs_mult)
blanchet@36561
   419
by (metis le_less)
paulson@23449
   420
blanchet@46369
   421
lemma bigo_const_mult2: "O(\<lambda>x. c * f x) \<le> O(f)"
paulson@23449
   422
by (rule bigo_elt_subset, rule bigo_const_mult1)
paulson@23449
   423
blanchet@45575
   424
lemma bigo_const_mult3: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow> f : O(\<lambda>x. c * f x)"
blanchet@45575
   425
apply (simp add: bigo_def)
blanchet@46369
   426
by (metis (no_types) abs_mult mult_assoc mult_1 order_refl left_inverse)
paulson@23449
   427
blanchet@46369
   428
lemma bigo_const_mult4:
blanchet@46369
   429
"(c\<Colon>'a\<Colon>{linordered_field,number_ring}) \<noteq> 0 \<Longrightarrow> O(f) \<le> O(\<lambda>x. c * f x)"
blanchet@46369
   430
by (metis bigo_elt_subset bigo_const_mult3)
paulson@23449
   431
blanchet@45575
   432
lemma bigo_const_mult [simp]: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow>
blanchet@45575
   433
    O(\<lambda>x. c * f x) = O(f)"
blanchet@46369
   434
by (metis equalityI bigo_const_mult2 bigo_const_mult4)
paulson@23449
   435
blanchet@45575
   436
lemma bigo_const_mult5 [simp]: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow>
blanchet@45575
   437
    (\<lambda>x. c) *o O(f) = O(f)"
paulson@23449
   438
  apply (auto del: subsetI)
paulson@23449
   439
  apply (rule order_trans)
paulson@23449
   440
  apply (rule bigo_mult2)
paulson@23449
   441
  apply (simp add: func_times)
paulson@23449
   442
  apply (auto intro!: subsetI simp add: bigo_def elt_set_times_def func_times)
blanchet@45575
   443
  apply (rule_tac x = "\<lambda>y. inverse c * x y" in exI)
blanchet@43197
   444
  apply (rename_tac g d)
paulson@24942
   445
  apply safe
blanchet@43197
   446
  apply (rule_tac [2] ext)
blanchet@43197
   447
   prefer 2
haftmann@26041
   448
   apply simp
paulson@24942
   449
  apply (simp add: mult_assoc [symmetric] abs_mult)
blanchet@39259
   450
  (* couldn't get this proof without the step above *)
blanchet@39259
   451
proof -
blanchet@39259
   452
  fix g :: "'b \<Rightarrow> 'a" and d :: 'a
blanchet@39259
   453
  assume A1: "c \<noteq> (0\<Colon>'a)"
blanchet@39259
   454
  assume A2: "\<forall>x\<Colon>'b. \<bar>g x\<bar> \<le> d * \<bar>f x\<bar>"
blanchet@39259
   455
  have F1: "inverse \<bar>c\<bar> = \<bar>inverse c\<bar>" using A1 by (metis nonzero_abs_inverse)
blanchet@39259
   456
  have F2: "(0\<Colon>'a) < \<bar>c\<bar>" using A1 by (metis zero_less_abs_iff)
blanchet@39259
   457
  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
   458
  hence "(0\<Colon>'a) < \<bar>inverse c\<bar>" using F2 by metis
blanchet@39259
   459
  hence F3: "(0\<Colon>'a) \<le> \<bar>inverse c\<bar>" by (metis order_le_less)
blanchet@39259
   460
  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
   461
    using A2 by metis
blanchet@39259
   462
  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
   463
    using F3 by metis
blanchet@39259
   464
  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
   465
    by (metis comm_mult_left_mono)
blanchet@39259
   466
  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
   467
    using A2 F4 by (metis ab_semigroup_mult_class.mult_ac(1) comm_mult_left_mono)
blanchet@39259
   468
qed
paulson@23449
   469
blanchet@45575
   470
lemma bigo_const_mult6 [intro]: "(\<lambda>x. c) *o O(f) <= O(f)"
paulson@23449
   471
  apply (auto intro!: subsetI
paulson@23449
   472
    simp add: bigo_def elt_set_times_def func_times
paulson@23449
   473
    simp del: abs_mult mult_ac)
blanchet@45575
   474
(* sledgehammer *)
paulson@23449
   475
  apply (rule_tac x = "ca * (abs c)" in exI)
paulson@23449
   476
  apply (rule allI)
paulson@23449
   477
  apply (subgoal_tac "ca * abs(c) * abs(f x) = abs(c) * (ca * abs(f x))")
paulson@23449
   478
  apply (erule ssubst)
paulson@23449
   479
  apply (subst abs_mult)
paulson@23449
   480
  apply (rule mult_left_mono)
paulson@23449
   481
  apply (erule spec)
paulson@23449
   482
  apply simp
blanchet@46369
   483
  apply (simp add: mult_ac)
paulson@23449
   484
done
paulson@23449
   485
blanchet@45575
   486
lemma bigo_const_mult7 [intro]: "f =o O(g) \<Longrightarrow> (\<lambda>x. c * f x) =o O(g)"
blanchet@46369
   487
by (metis bigo_const_mult1 bigo_elt_subset order_less_le psubsetD)
paulson@23449
   488
blanchet@45575
   489
lemma bigo_compose1: "f =o O(g) \<Longrightarrow> (\<lambda>x. f(k x)) =o O(\<lambda>x. g(k x))"
paulson@23449
   490
by (unfold bigo_def, auto)
paulson@23449
   491
blanchet@46369
   492
lemma bigo_compose2:
blanchet@46369
   493
"f =o g +o O(h) \<Longrightarrow> (\<lambda>x. f(k x)) =o (\<lambda>x. g(k x)) +o O(\<lambda>x. h(k x))"
blanchet@46369
   494
apply (simp only: set_minus_plus [symmetric] diff_minus fun_Compl_def func_plus)
blanchet@46369
   495
by (erule bigo_compose1)
paulson@23449
   496
paulson@23449
   497
subsection {* Setsum *}
paulson@23449
   498
blanchet@45575
   499
lemma bigo_setsum_main: "\<forall>x. \<forall>y \<in> A x. 0 <= h x y \<Longrightarrow>
blanchet@45575
   500
    \<exists>c. \<forall>x. \<forall>y \<in> A x. abs (f x y) <= c * (h x y) \<Longrightarrow>
blanchet@45575
   501
      (\<lambda>x. SUM y : A x. f x y) =o O(\<lambda>x. SUM y : A x. h x y)"
blanchet@46369
   502
apply (auto simp add: bigo_def)
blanchet@46369
   503
apply (rule_tac x = "abs c" in exI)
blanchet@46369
   504
apply (subst abs_of_nonneg) back back
blanchet@46369
   505
 apply (rule setsum_nonneg)
blanchet@46369
   506
 apply force
blanchet@46369
   507
apply (subst setsum_right_distrib)
blanchet@46369
   508
apply (rule allI)
blanchet@46369
   509
apply (rule order_trans)
blanchet@46369
   510
 apply (rule setsum_abs)
blanchet@46369
   511
apply (rule setsum_mono)
blanchet@46369
   512
by (metis abs_ge_self abs_mult_pos order_trans)
paulson@23449
   513
blanchet@45575
   514
lemma bigo_setsum1: "\<forall>x y. 0 <= h x y \<Longrightarrow>
blanchet@45575
   515
    \<exists>c. \<forall>x y. abs (f x y) <= c * (h x y) \<Longrightarrow>
blanchet@45575
   516
      (\<lambda>x. SUM y : A x. f x y) =o O(\<lambda>x. SUM y : A x. h x y)"
blanchet@45575
   517
by (metis (no_types) bigo_setsum_main)
paulson@23449
   518
blanchet@45575
   519
lemma bigo_setsum2: "\<forall>y. 0 <= h y \<Longrightarrow>
blanchet@46369
   520
    \<exists>c. \<forall>y. abs (f y) <= c * (h y) \<Longrightarrow>
blanchet@45575
   521
      (\<lambda>x. SUM y : A x. f y) =o O(\<lambda>x. SUM y : A x. h y)"
blanchet@46369
   522
apply (rule bigo_setsum1)
blanchet@46369
   523
by metis+
paulson@23449
   524
blanchet@45575
   525
lemma bigo_setsum3: "f =o O(h) \<Longrightarrow>
blanchet@45575
   526
    (\<lambda>x. SUM y : A x. (l x y) * f(k x y)) =o
blanchet@45575
   527
      O(\<lambda>x. SUM y : A x. abs(l x y * h(k x y)))"
blanchet@45575
   528
apply (rule bigo_setsum1)
blanchet@45575
   529
 apply (rule allI)+
blanchet@45575
   530
 apply (rule abs_ge_zero)
blanchet@45575
   531
apply (unfold bigo_def)
blanchet@45575
   532
apply (auto simp add: abs_mult)
blanchet@46369
   533
by (metis abs_ge_zero mult_left_commute mult_left_mono)
paulson@23449
   534
blanchet@45575
   535
lemma bigo_setsum4: "f =o g +o O(h) \<Longrightarrow>
blanchet@45575
   536
    (\<lambda>x. SUM y : A x. l x y * f(k x y)) =o
blanchet@45575
   537
      (\<lambda>x. SUM y : A x. l x y * g(k x y)) +o
blanchet@45575
   538
        O(\<lambda>x. SUM y : A x. abs(l x y * h(k x y)))"
blanchet@45575
   539
apply (rule set_minus_imp_plus)
blanchet@45575
   540
apply (subst fun_diff_def)
blanchet@45575
   541
apply (subst setsum_subtractf [symmetric])
blanchet@45575
   542
apply (subst right_diff_distrib [symmetric])
blanchet@45575
   543
apply (rule bigo_setsum3)
blanchet@46369
   544
by (metis (lifting, no_types) fun_diff_def set_plus_imp_minus ext)
paulson@23449
   545
blanchet@45575
   546
lemma bigo_setsum5: "f =o O(h) \<Longrightarrow> \<forall>x y. 0 <= l x y \<Longrightarrow>
blanchet@45575
   547
    \<forall>x. 0 <= h x \<Longrightarrow>
blanchet@45575
   548
      (\<lambda>x. SUM y : A x. (l x y) * f(k x y)) =o
blanchet@45575
   549
        O(\<lambda>x. SUM y : A x. (l x y) * h(k x y))"
blanchet@46369
   550
apply (subgoal_tac "(\<lambda>x. SUM y : A x. (l x y) * h(k x y)) =
blanchet@45575
   551
      (\<lambda>x. SUM y : A x. abs((l x y) * h(k x y)))")
blanchet@46369
   552
 apply (erule ssubst)
blanchet@46369
   553
 apply (erule bigo_setsum3)
blanchet@46369
   554
apply (rule ext)
blanchet@46369
   555
apply (rule setsum_cong2)
blanchet@46369
   556
by (metis abs_of_nonneg zero_le_mult_iff)
paulson@23449
   557
blanchet@45575
   558
lemma bigo_setsum6: "f =o g +o O(h) \<Longrightarrow> \<forall>x y. 0 <= l x y \<Longrightarrow>
blanchet@45575
   559
    \<forall>x. 0 <= h x \<Longrightarrow>
blanchet@45575
   560
      (\<lambda>x. SUM y : A x. (l x y) * f(k x y)) =o
blanchet@45575
   561
        (\<lambda>x. SUM y : A x. (l x y) * g(k x y)) +o
blanchet@45575
   562
          O(\<lambda>x. SUM y : A x. (l x y) * h(k x y))"
paulson@23449
   563
  apply (rule set_minus_imp_plus)
berghofe@26814
   564
  apply (subst fun_diff_def)
paulson@23449
   565
  apply (subst setsum_subtractf [symmetric])
paulson@23449
   566
  apply (subst right_diff_distrib [symmetric])
paulson@23449
   567
  apply (rule bigo_setsum5)
berghofe@26814
   568
  apply (subst fun_diff_def [symmetric])
paulson@23449
   569
  apply (drule set_plus_imp_minus)
paulson@23449
   570
  apply auto
paulson@23449
   571
done
paulson@23449
   572
paulson@23449
   573
subsection {* Misc useful stuff *}
paulson@23449
   574
blanchet@45575
   575
lemma bigo_useful_intro: "A <= O(f) \<Longrightarrow> B <= O(f) \<Longrightarrow>
berghofe@26814
   576
  A \<oplus> B <= O(f)"
paulson@23449
   577
  apply (subst bigo_plus_idemp [symmetric])
paulson@23449
   578
  apply (rule set_plus_mono2)
paulson@23449
   579
  apply assumption+
paulson@23449
   580
done
paulson@23449
   581
blanchet@45575
   582
lemma bigo_useful_add: "f =o O(h) \<Longrightarrow> g =o O(h) \<Longrightarrow> f + g =o O(h)"
paulson@23449
   583
  apply (subst bigo_plus_idemp [symmetric])
paulson@23449
   584
  apply (rule set_plus_intro)
paulson@23449
   585
  apply assumption+
paulson@23449
   586
done
blanchet@43197
   587
blanchet@45575
   588
lemma bigo_useful_const_mult: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow>
blanchet@45575
   589
    (\<lambda>x. c) * f =o O(h) \<Longrightarrow> f =o O(h)"
paulson@23449
   590
  apply (rule subsetD)
blanchet@45575
   591
  apply (subgoal_tac "(\<lambda>x. 1 / c) *o O(h) <= O(h)")
paulson@23449
   592
  apply assumption
paulson@23449
   593
  apply (rule bigo_const_mult6)
blanchet@45575
   594
  apply (subgoal_tac "f = (\<lambda>x. 1 / c) * ((\<lambda>x. c) * f)")
paulson@23449
   595
  apply (erule ssubst)
paulson@23449
   596
  apply (erule set_times_intro2)
blanchet@43197
   597
  apply (simp add: func_times)
paulson@23449
   598
done
paulson@23449
   599
blanchet@45575
   600
lemma bigo_fix: "(\<lambda>x. f ((x\<Colon>nat) + 1)) =o O(\<lambda>x. h(x + 1)) \<Longrightarrow> f 0 = 0 \<Longrightarrow>
paulson@23449
   601
    f =o O(h)"
blanchet@45575
   602
apply (simp add: bigo_alt_def)
blanchet@45575
   603
by (metis abs_ge_zero abs_mult abs_of_pos abs_zero not0_implies_Suc)
paulson@23449
   604
blanchet@43197
   605
lemma bigo_fix2:
blanchet@45575
   606
    "(\<lambda>x. f ((x\<Colon>nat) + 1)) =o (\<lambda>x. g(x + 1)) +o O(\<lambda>x. h(x + 1)) \<Longrightarrow>
blanchet@45575
   607
       f 0 = g 0 \<Longrightarrow> f =o g +o O(h)"
paulson@23449
   608
  apply (rule set_minus_imp_plus)
paulson@23449
   609
  apply (rule bigo_fix)
berghofe@26814
   610
  apply (subst fun_diff_def)
berghofe@26814
   611
  apply (subst fun_diff_def [symmetric])
paulson@23449
   612
  apply (rule set_plus_imp_minus)
paulson@23449
   613
  apply simp
berghofe@26814
   614
  apply (simp add: fun_diff_def)
paulson@23449
   615
done
paulson@23449
   616
paulson@23449
   617
subsection {* Less than or equal to *}
paulson@23449
   618
blanchet@45575
   619
definition lesso :: "('a => 'b\<Colon>linordered_idom) => ('a => 'b) => ('a => 'b)" (infixl "<o" 70) where
blanchet@45575
   620
  "f <o g == (\<lambda>x. max (f x - g x) 0)"
paulson@23449
   621
blanchet@45575
   622
lemma bigo_lesseq1: "f =o O(h) \<Longrightarrow> \<forall>x. abs (g x) <= abs (f x) \<Longrightarrow>
paulson@23449
   623
    g =o O(h)"
paulson@23449
   624
  apply (unfold bigo_def)
paulson@23449
   625
  apply clarsimp
blanchet@43197
   626
apply (blast intro: order_trans)
paulson@23449
   627
done
paulson@23449
   628
blanchet@45575
   629
lemma bigo_lesseq2: "f =o O(h) \<Longrightarrow> \<forall>x. abs (g x) <= f x \<Longrightarrow>
paulson@23449
   630
      g =o O(h)"
paulson@23449
   631
  apply (erule bigo_lesseq1)
blanchet@43197
   632
apply (blast intro: abs_ge_self order_trans)
paulson@23449
   633
done
paulson@23449
   634
blanchet@45575
   635
lemma bigo_lesseq3: "f =o O(h) \<Longrightarrow> \<forall>x. 0 <= g x \<Longrightarrow> \<forall>x. g x <= f x \<Longrightarrow>
paulson@23449
   636
      g =o O(h)"
paulson@23449
   637
  apply (erule bigo_lesseq2)
paulson@23449
   638
  apply (rule allI)
paulson@23449
   639
  apply (subst abs_of_nonneg)
paulson@23449
   640
  apply (erule spec)+
paulson@23449
   641
done
paulson@23449
   642
blanchet@45575
   643
lemma bigo_lesseq4: "f =o O(h) \<Longrightarrow>
blanchet@45575
   644
    \<forall>x. 0 <= g x \<Longrightarrow> \<forall>x. g x <= abs (f x) \<Longrightarrow>
paulson@23449
   645
      g =o O(h)"
paulson@23449
   646
  apply (erule bigo_lesseq1)
paulson@23449
   647
  apply (rule allI)
paulson@23449
   648
  apply (subst abs_of_nonneg)
paulson@23449
   649
  apply (erule spec)+
paulson@23449
   650
done
paulson@23449
   651
blanchet@45575
   652
lemma bigo_lesso1: "\<forall>x. f x <= g x \<Longrightarrow> f <o g =o O(h)"
blanchet@36561
   653
apply (unfold lesso_def)
blanchet@45575
   654
apply (subgoal_tac "(\<lambda>x. max (f x - g x) 0) = 0")
blanchet@45575
   655
 apply (metis bigo_zero)
blanchet@46364
   656
by (metis (lifting, no_types) func_zero le_fun_def le_iff_diff_le_0
blanchet@45575
   657
      min_max.sup_absorb2 order_eq_iff)
paulson@23449
   658
blanchet@45575
   659
lemma bigo_lesso2: "f =o g +o O(h) \<Longrightarrow>
blanchet@45575
   660
    \<forall>x. 0 <= k x \<Longrightarrow> \<forall>x. k x <= f x \<Longrightarrow>
paulson@23449
   661
      k <o g =o O(h)"
paulson@23449
   662
  apply (unfold lesso_def)
paulson@23449
   663
  apply (rule bigo_lesseq4)
paulson@23449
   664
  apply (erule set_plus_imp_minus)
paulson@23449
   665
  apply (rule allI)
paulson@23449
   666
  apply (rule le_maxI2)
paulson@23449
   667
  apply (rule allI)
berghofe@26814
   668
  apply (subst fun_diff_def)
paulson@23449
   669
apply (erule thin_rl)
blanchet@45575
   670
(* sledgehammer *)
blanchet@45575
   671
apply (case_tac "0 <= k x - g x")
blanchet@45575
   672
 apply (metis (hide_lams, no_types) abs_le_iff add_le_imp_le_right diff_minus le_less
blanchet@45575
   673
          le_max_iff_disj min_max.le_supE min_max.sup_absorb2
blanchet@45575
   674
          min_max.sup_commute)
blanchet@45575
   675
by (metis abs_ge_zero le_cases min_max.sup_absorb2)
paulson@23449
   676
blanchet@45575
   677
lemma bigo_lesso3: "f =o g +o O(h) \<Longrightarrow>
blanchet@45575
   678
    \<forall>x. 0 <= k x \<Longrightarrow> \<forall>x. g x <= k x \<Longrightarrow>
paulson@23449
   679
      f <o k =o O(h)"
paulson@23449
   680
  apply (unfold lesso_def)
paulson@23449
   681
  apply (rule bigo_lesseq4)
paulson@23449
   682
  apply (erule set_plus_imp_minus)
paulson@23449
   683
  apply (rule allI)
paulson@23449
   684
  apply (rule le_maxI2)
paulson@23449
   685
  apply (rule allI)
berghofe@26814
   686
  apply (subst fun_diff_def)
blanchet@45575
   687
  apply (erule thin_rl)
blanchet@45575
   688
  (* sledgehammer *)
paulson@23449
   689
  apply (case_tac "0 <= f x - k x")
blanchet@45575
   690
  apply simp
paulson@23449
   691
  apply (subst abs_of_nonneg)
paulson@23449
   692
  apply (drule_tac x = x in spec) back
blanchet@45705
   693
  apply (metis diff_less_0_iff_less linorder_not_le not_leE xt1(12) xt1(6))
blanchet@45575
   694
 apply (metis add_minus_cancel diff_le_eq le_diff_eq uminus_add_conv_diff)
haftmann@29511
   695
apply (metis abs_ge_zero linorder_linear min_max.sup_absorb1 min_max.sup_commute)
paulson@23449
   696
done
paulson@23449
   697
blanchet@45705
   698
lemma bigo_lesso4:
blanchet@45705
   699
  "f <o g =o O(k\<Colon>'a=>'b\<Colon>{linordered_field,number_ring}) \<Longrightarrow>
blanchet@45705
   700
   g =o h +o O(k) \<Longrightarrow> f <o h =o O(k)"
blanchet@45705
   701
apply (unfold lesso_def)
blanchet@45705
   702
apply (drule set_plus_imp_minus)
blanchet@45705
   703
apply (drule bigo_abs5) back
blanchet@45705
   704
apply (simp add: fun_diff_def)
blanchet@45705
   705
apply (drule bigo_useful_add, assumption)
blanchet@45705
   706
apply (erule bigo_lesseq2) back
blanchet@45705
   707
apply (rule allI)
blanchet@45705
   708
by (auto simp add: func_plus fun_diff_def algebra_simps
paulson@23449
   709
    split: split_max abs_split)
paulson@23449
   710
blanchet@45705
   711
lemma bigo_lesso5: "f <o g =o O(h) \<Longrightarrow> \<exists>C. \<forall>x. f x <= g x + C * abs (h x)"
blanchet@45705
   712
apply (simp only: lesso_def bigo_alt_def)
blanchet@45705
   713
apply clarsimp
blanchet@45705
   714
by (metis abs_if abs_mult add_commute diff_le_eq less_not_permute)
paulson@23449
   715
paulson@23449
   716
end