| author | wenzelm | 
| Fri, 17 Jul 2020 17:06:54 +0200 | |
| changeset 72056 | b9f5f30b623f | 
| parent 69712 | dc85b5b3a532 | 
| child 74690 | 55a4b319b2b9 | 
| permissions | -rw-r--r-- | 
| 43197 | 1 | (* Title: HOL/Metis_Examples/Big_O.thy | 
| 2 | Author: Lawrence C. Paulson, Cambridge University Computer Laboratory | |
| 41144 | 3 | Author: Jasmin Blanchette, TU Muenchen | 
| 23449 | 4 | |
| 43197 | 5 | Metis example featuring the Big O notation. | 
| 23449 | 6 | *) | 
| 7 | ||
| 63167 | 8 | section \<open>Metis Example Featuring the Big O Notation\<close> | 
| 23449 | 9 | |
| 43197 | 10 | theory Big_O | 
| 41413 
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
 wenzelm parents: 
41144diff
changeset | 11 | imports | 
| 66453 
cc19f7ca2ed6
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
 wenzelm parents: 
64267diff
changeset | 12 | "HOL-Decision_Procs.Dense_Linear_Order" | 
| 
cc19f7ca2ed6
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
 wenzelm parents: 
64267diff
changeset | 13 | "HOL-Library.Function_Algebras" | 
| 
cc19f7ca2ed6
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
 wenzelm parents: 
64267diff
changeset | 14 | "HOL-Library.Set_Algebras" | 
| 23449 | 15 | begin | 
| 16 | ||
| 63167 | 17 | subsection \<open>Definitions\<close> | 
| 23449 | 18 | |
| 61076 | 19 | definition bigo :: "('a => 'b::linordered_idom) => ('a => 'b) set" ("(1O'(_'))") where
 | 
| 61945 | 20 |   "O(f::('a => 'b)) == {h. \<exists>c. \<forall>x. \<bar>h x\<bar> <= c * \<bar>f x\<bar>}"
 | 
| 23449 | 21 | |
| 45575 | 22 | lemma bigo_pos_const: | 
| 61076 | 23 | "(\<exists>c::'a::linordered_idom. | 
| 61945 | 24 | \<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>) | 
| 25 | \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>))" | |
| 59554 
4044f53326c9
inlined rules to free user-space from technical names
 haftmann parents: 
58889diff
changeset | 26 | by (metis (no_types) abs_ge_zero | 
| 
4044f53326c9
inlined rules to free user-space from technical names
 haftmann parents: 
58889diff
changeset | 27 | algebra_simps mult.comm_neutral | 
| 61824 
dcbe9f756ae0
not_leE -> not_le_imp_less and other tidying
 paulson <lp15@cam.ac.uk> parents: 
61076diff
changeset | 28 | mult_nonpos_nonneg not_le_imp_less order_trans zero_less_one) | 
| 23449 | 29 | |
| 36407 | 30 | (*** Now various verions with an increasing shrink factor ***) | 
| 23449 | 31 | |
| 57245 | 32 | sledgehammer_params [isar_proofs, compress = 1] | 
| 23449 | 33 | |
| 45575 | 34 | lemma | 
| 61076 | 35 | "(\<exists>c::'a::linordered_idom. | 
| 61945 | 36 | \<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>) | 
| 37 | \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>))" | |
| 23449 | 38 | apply auto | 
| 39 | apply (case_tac "c = 0", simp) | |
| 40 | apply (rule_tac x = "1" in exI, simp) | |
| 61945 | 41 | apply (rule_tac x = "\<bar>c\<bar>" in exI, auto) | 
| 36561 | 42 | proof - | 
| 43 | fix c :: 'a and x :: 'b | |
| 44 | assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>" | |
| 61076 | 45 | have F1: "\<forall>x\<^sub>1::'a::linordered_idom. 0 \<le> \<bar>x\<^sub>1\<bar>" by (metis abs_ge_zero) | 
| 46 | have F2: "\<forall>x\<^sub>1::'a::linordered_idom. 1 * x\<^sub>1 = x\<^sub>1" by (metis mult_1) | |
| 53015 
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
 wenzelm parents: 
51130diff
changeset | 47 | have F3: "\<forall>x\<^sub>1 x\<^sub>3. x\<^sub>3 \<le> \<bar>h x\<^sub>1\<bar> \<longrightarrow> x\<^sub>3 \<le> c * \<bar>f x\<^sub>1\<bar>" by (metis A1 order_trans) | 
| 61076 | 48 | have F4: "\<forall>x\<^sub>2 x\<^sub>3::'a::linordered_idom. \<bar>x\<^sub>3\<bar> * \<bar>x\<^sub>2\<bar> = \<bar>x\<^sub>3 * x\<^sub>2\<bar>" | 
| 36561 | 49 | by (metis abs_mult) | 
| 61076 | 50 | have F5: "\<forall>x\<^sub>3 x\<^sub>1::'a::linordered_idom. 0 \<le> x\<^sub>1 \<longrightarrow> \<bar>x\<^sub>3 * x\<^sub>1\<bar> = \<bar>x\<^sub>3\<bar> * x\<^sub>1" | 
| 36561 | 51 | by (metis abs_mult_pos) | 
| 61076 | 52 | hence "\<forall>x\<^sub>1\<ge>0. \<bar>x\<^sub>1::'a::linordered_idom\<bar> = \<bar>1\<bar> * x\<^sub>1" by (metis F2) | 
| 53 | hence "\<forall>x\<^sub>1\<ge>0. \<bar>x\<^sub>1::'a::linordered_idom\<bar> = x\<^sub>1" by (metis F2 abs_one) | |
| 53015 
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
 wenzelm parents: 
51130diff
changeset | 54 | hence "\<forall>x\<^sub>3. 0 \<le> \<bar>h x\<^sub>3\<bar> \<longrightarrow> \<bar>c * \<bar>f x\<^sub>3\<bar>\<bar> = c * \<bar>f x\<^sub>3\<bar>" by (metis F3) | 
| 
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
 wenzelm parents: 
51130diff
changeset | 55 | hence "\<forall>x\<^sub>3. \<bar>c * \<bar>f x\<^sub>3\<bar>\<bar> = c * \<bar>f x\<^sub>3\<bar>" by (metis F1) | 
| 61076 | 56 | hence "\<forall>x\<^sub>3. (0::'a) \<le> \<bar>f x\<^sub>3\<bar> \<longrightarrow> c * \<bar>f x\<^sub>3\<bar> = \<bar>c\<bar> * \<bar>f x\<^sub>3\<bar>" by (metis F5) | 
| 57 | hence "\<forall>x\<^sub>3. (0::'a) \<le> \<bar>f x\<^sub>3\<bar> \<longrightarrow> c * \<bar>f x\<^sub>3\<bar> = \<bar>c * f x\<^sub>3\<bar>" by (metis F4) | |
| 53015 
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
 wenzelm parents: 
51130diff
changeset | 58 | hence "\<forall>x\<^sub>3. c * \<bar>f x\<^sub>3\<bar> = \<bar>c * f x\<^sub>3\<bar>" by (metis F1) | 
| 36561 | 59 | hence "\<bar>h x\<bar> \<le> \<bar>c * f x\<bar>" by (metis A1) | 
| 60 | thus "\<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>" by (metis F4) | |
| 23449 | 61 | qed | 
| 62 | ||
| 57245 | 63 | sledgehammer_params [isar_proofs, compress = 2] | 
| 25710 
4cdf7de81e1b
Replaced refs by config params; finer critical section in mets method
 paulson parents: 
25592diff
changeset | 64 | |
| 45575 | 65 | lemma | 
| 61076 | 66 | "(\<exists>c::'a::linordered_idom. | 
| 61945 | 67 | \<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>) | 
| 68 | \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>))" | |
| 23449 | 69 | apply auto | 
| 70 | apply (case_tac "c = 0", simp) | |
| 71 | apply (rule_tac x = "1" in exI, simp) | |
| 61945 | 72 | apply (rule_tac x = "\<bar>c\<bar>" in exI, auto) | 
| 36561 | 73 | proof - | 
| 74 | fix c :: 'a and x :: 'b | |
| 75 | assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>" | |
| 61076 | 76 | have F1: "\<forall>x\<^sub>1::'a::linordered_idom. 1 * x\<^sub>1 = x\<^sub>1" by (metis mult_1) | 
| 77 | have F2: "\<forall>x\<^sub>2 x\<^sub>3::'a::linordered_idom. \<bar>x\<^sub>3\<bar> * \<bar>x\<^sub>2\<bar> = \<bar>x\<^sub>3 * x\<^sub>2\<bar>" | |
| 36561 | 78 | by (metis abs_mult) | 
| 61076 | 79 | have "\<forall>x\<^sub>1\<ge>0. \<bar>x\<^sub>1::'a::linordered_idom\<bar> = x\<^sub>1" by (metis F1 abs_mult_pos abs_one) | 
| 53015 
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
 wenzelm parents: 
51130diff
changeset | 80 | hence "\<forall>x\<^sub>3. \<bar>c * \<bar>f x\<^sub>3\<bar>\<bar> = c * \<bar>f x\<^sub>3\<bar>" by (metis A1 abs_ge_zero order_trans) | 
| 
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
 wenzelm parents: 
51130diff
changeset | 81 | hence "\<forall>x\<^sub>3. 0 \<le> \<bar>f x\<^sub>3\<bar> \<longrightarrow> c * \<bar>f x\<^sub>3\<bar> = \<bar>c * f x\<^sub>3\<bar>" by (metis F2 abs_mult_pos) | 
| 36561 | 82 | hence "\<bar>h x\<bar> \<le> \<bar>c * f x\<bar>" by (metis A1 abs_ge_zero) | 
| 83 | thus "\<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>" by (metis F2) | |
| 23449 | 84 | qed | 
| 85 | ||
| 57245 | 86 | sledgehammer_params [isar_proofs, compress = 3] | 
| 25710 
4cdf7de81e1b
Replaced refs by config params; finer critical section in mets method
 paulson parents: 
25592diff
changeset | 87 | |
| 45575 | 88 | lemma | 
| 61076 | 89 | "(\<exists>c::'a::linordered_idom. | 
| 61945 | 90 | \<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>) | 
| 91 | \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>))" | |
| 23449 | 92 | apply auto | 
| 93 | apply (case_tac "c = 0", simp) | |
| 94 | apply (rule_tac x = "1" in exI, simp) | |
| 61945 | 95 | apply (rule_tac x = "\<bar>c\<bar>" in exI, auto) | 
| 36561 | 96 | proof - | 
| 97 | fix c :: 'a and x :: 'b | |
| 98 | assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>" | |
| 61076 | 99 | have F1: "\<forall>x\<^sub>1::'a::linordered_idom. 1 * x\<^sub>1 = x\<^sub>1" by (metis mult_1) | 
| 100 | have F2: "\<forall>x\<^sub>3 x\<^sub>1::'a::linordered_idom. 0 \<le> x\<^sub>1 \<longrightarrow> \<bar>x\<^sub>3 * x\<^sub>1\<bar> = \<bar>x\<^sub>3\<bar> * x\<^sub>1" by (metis abs_mult_pos) | |
| 101 | hence "\<forall>x\<^sub>1\<ge>0. \<bar>x\<^sub>1::'a::linordered_idom\<bar> = x\<^sub>1" by (metis F1 abs_one) | |
| 53015 
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
 wenzelm parents: 
51130diff
changeset | 102 | hence "\<forall>x\<^sub>3. 0 \<le> \<bar>f x\<^sub>3\<bar> \<longrightarrow> c * \<bar>f x\<^sub>3\<bar> = \<bar>c\<bar> * \<bar>f x\<^sub>3\<bar>" by (metis F2 A1 abs_ge_zero order_trans) | 
| 46644 | 103 | thus "\<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>" by (metis A1 abs_ge_zero) | 
| 23449 | 104 | qed | 
| 105 | ||
| 57245 | 106 | sledgehammer_params [isar_proofs, compress = 4] | 
| 24545 | 107 | |
| 45575 | 108 | lemma | 
| 61076 | 109 | "(\<exists>c::'a::linordered_idom. | 
| 61945 | 110 | \<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>) | 
| 111 | \<longleftrightarrow> (\<exists>c. 0 < c \<and> (\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>))" | |
| 24545 | 112 | apply auto | 
| 113 | apply (case_tac "c = 0", simp) | |
| 114 | apply (rule_tac x = "1" in exI, simp) | |
| 61945 | 115 | apply (rule_tac x = "\<bar>c\<bar>" in exI, auto) | 
| 36561 | 116 | proof - | 
| 117 | fix c :: 'a and x :: 'b | |
| 118 | assume A1: "\<forall>x. \<bar>h x\<bar> \<le> c * \<bar>f x\<bar>" | |
| 61076 | 119 | have "\<forall>x\<^sub>1::'a::linordered_idom. 1 * x\<^sub>1 = x\<^sub>1" by (metis mult_1) | 
| 53015 
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
 wenzelm parents: 
51130diff
changeset | 120 | hence "\<forall>x\<^sub>3. \<bar>c * \<bar>f x\<^sub>3\<bar>\<bar> = c * \<bar>f x\<^sub>3\<bar>" | 
| 36561 | 121 | by (metis A1 abs_ge_zero order_trans abs_mult_pos abs_one) | 
| 122 | hence "\<bar>h x\<bar> \<le> \<bar>c * f x\<bar>" by (metis A1 abs_ge_zero abs_mult_pos abs_mult) | |
| 123 | thus "\<bar>h x\<bar> \<le> \<bar>c\<bar> * \<bar>f x\<bar>" by (metis abs_mult) | |
| 24545 | 124 | qed | 
| 125 | ||
| 57245 | 126 | sledgehammer_params [isar_proofs, compress = 1] | 
| 24545 | 127 | |
| 61945 | 128 | lemma bigo_alt_def: "O(f) = {h. \<exists>c. (0 < c \<and> (\<forall>x. \<bar>h x\<bar> <= c * \<bar>f x\<bar>))}"
 | 
| 23449 | 129 | by (auto simp add: bigo_def bigo_pos_const) | 
| 130 | ||
| 67613 | 131 | lemma bigo_elt_subset [intro]: "f \<in> O(g) \<Longrightarrow> O(f) \<le> O(g)" | 
| 45575 | 132 | apply (auto simp add: bigo_alt_def) | 
| 133 | apply (rule_tac x = "ca * c" in exI) | |
| 59554 
4044f53326c9
inlined rules to free user-space from technical names
 haftmann parents: 
58889diff
changeset | 134 | apply (metis algebra_simps mult_le_cancel_left_pos order_trans mult_pos_pos) | 
| 
4044f53326c9
inlined rules to free user-space from technical names
 haftmann parents: 
58889diff
changeset | 135 | done | 
| 23449 | 136 | |
| 67613 | 137 | lemma bigo_refl [intro]: "f \<in> O(f)" | 
| 46364 | 138 | unfolding bigo_def mem_Collect_eq | 
| 36844 | 139 | by (metis mult_1 order_refl) | 
| 23449 | 140 | |
| 67613 | 141 | lemma bigo_zero: "0 \<in> O(g)" | 
| 36561 | 142 | apply (auto simp add: bigo_def func_zero) | 
| 36844 | 143 | by (metis mult_zero_left order_refl) | 
| 23449 | 144 | |
| 45575 | 145 | lemma bigo_zero2: "O(\<lambda>x. 0) = {\<lambda>x. 0}"
 | 
| 146 | by (auto simp add: bigo_def) | |
| 23449 | 147 | |
| 43197 | 148 | lemma bigo_plus_self_subset [intro]: | 
| 47445 
69e96e5500df
Set_Algebras: removed syntax \<oplus> and \<otimes>, in favour of plain + and *
 krauss parents: 
47108diff
changeset | 149 | "O(f) + O(f) <= O(f)" | 
| 45575 | 150 | apply (auto simp add: bigo_alt_def set_plus_def) | 
| 151 | apply (rule_tac x = "c + ca" in exI) | |
| 152 | apply auto | |
| 153 | apply (simp add: ring_distribs func_plus) | |
| 154 | by (metis order_trans abs_triangle_ineq add_mono) | |
| 23449 | 155 | |
| 47445 
69e96e5500df
Set_Algebras: removed syntax \<oplus> and \<otimes>, in favour of plain + and *
 krauss parents: 
47108diff
changeset | 156 | lemma bigo_plus_idemp [simp]: "O(f) + O(f) = O(f)" | 
| 45575 | 157 | by (metis bigo_plus_self_subset bigo_zero set_eq_subset set_zero_plus2) | 
| 23449 | 158 | |
| 47445 
69e96e5500df
Set_Algebras: removed syntax \<oplus> and \<otimes>, in favour of plain + and *
 krauss parents: 
47108diff
changeset | 159 | lemma bigo_plus_subset [intro]: "O(f + g) <= O(f) + O(g)" | 
| 45575 | 160 | apply (rule subsetI) | 
| 161 | apply (auto simp add: bigo_def bigo_pos_const func_plus set_plus_def) | |
| 162 | apply (subst bigo_pos_const [symmetric])+ | |
| 61945 | 163 | apply (rule_tac x = "\<lambda>n. if \<bar>g n\<bar> <= \<bar>f n\<bar> then x n else 0" in exI) | 
| 45575 | 164 | apply (rule conjI) | 
| 165 | apply (rule_tac x = "c + c" in exI) | |
| 166 | apply clarsimp | |
| 61945 | 167 | apply (subgoal_tac "c * \<bar>f xa + g xa\<bar> <= (c + c) * \<bar>f xa\<bar>") | 
| 56536 | 168 | apply (metis mult_2 order_trans) | 
| 61945 | 169 | apply (subgoal_tac "c * \<bar>f xa + g xa\<bar> <= c * (\<bar>f xa\<bar> + \<bar>g xa\<bar>)") | 
| 56536 | 170 | apply (erule order_trans) | 
| 171 | apply (simp add: ring_distribs) | |
| 172 | apply (rule mult_left_mono) | |
| 173 | apply (simp add: abs_triangle_ineq) | |
| 174 | apply (simp add: order_less_le) | |
| 61945 | 175 | apply (rule_tac x = "\<lambda>n. if \<bar>f n\<bar> < \<bar>g n\<bar> then x n else 0" in exI) | 
| 45575 | 176 | apply (rule conjI) | 
| 177 | apply (rule_tac x = "c + c" in exI) | |
| 178 | apply auto | |
| 61945 | 179 | apply (subgoal_tac "c * \<bar>f xa + g xa\<bar> <= (c + c) * \<bar>g xa\<bar>") | 
| 56536 | 180 | apply (metis order_trans mult_2) | 
| 61945 | 181 | apply (subgoal_tac "c * \<bar>f xa + g xa\<bar> <= c * (\<bar>f xa\<bar> + \<bar>g xa\<bar>)") | 
| 56536 | 182 | apply (erule order_trans) | 
| 183 | apply (simp add: ring_distribs) | |
| 184 | by (metis abs_triangle_ineq mult_le_cancel_left_pos) | |
| 23449 | 185 | |
| 47445 
69e96e5500df
Set_Algebras: removed syntax \<oplus> and \<otimes>, in favour of plain + and *
 krauss parents: 
47108diff
changeset | 186 | lemma bigo_plus_subset2 [intro]: "A <= O(f) \<Longrightarrow> B <= O(f) \<Longrightarrow> A + B <= O(f)" | 
| 45575 | 187 | by (metis bigo_plus_idemp set_plus_mono2) | 
| 23449 | 188 | |
| 47445 
69e96e5500df
Set_Algebras: removed syntax \<oplus> and \<otimes>, in favour of plain + and *
 krauss parents: 
47108diff
changeset | 189 | lemma bigo_plus_eq: "\<forall>x. 0 <= f x \<Longrightarrow> \<forall>x. 0 <= g x \<Longrightarrow> O(f + g) = O(f) + O(g)" | 
| 45575 | 190 | apply (rule equalityI) | 
| 191 | apply (rule bigo_plus_subset) | |
| 192 | apply (simp add: bigo_alt_def set_plus_def func_plus) | |
| 193 | apply clarify | |
| 194 | (* sledgehammer *) | |
| 195 | apply (rule_tac x = "max c ca" in exI) | |
| 46369 | 196 | |
| 45575 | 197 | apply (rule conjI) | 
| 198 | apply (metis less_max_iff_disj) | |
| 199 | apply clarify | |
| 200 | apply (drule_tac x = "xa" in spec)+ | |
| 201 | apply (subgoal_tac "0 <= f xa + g xa") | |
| 202 | apply (simp add: ring_distribs) | |
| 61945 | 203 | apply (subgoal_tac "\<bar>a xa + b xa\<bar> <= \<bar>a xa\<bar> + \<bar>b xa\<bar>") | 
| 204 | apply (subgoal_tac "\<bar>a xa\<bar> + \<bar>b xa\<bar> <= max c ca * f xa + max c ca * g xa") | |
| 45575 | 205 | apply (metis order_trans) | 
| 23449 | 206 | defer 1 | 
| 45575 | 207 | apply (metis abs_triangle_ineq) | 
| 208 | apply (metis add_nonneg_nonneg) | |
| 46644 | 209 | apply (rule add_mono) | 
| 54863 
82acc20ded73
prefer more canonical names for lemmas on min/max
 haftmann parents: 
54230diff
changeset | 210 | apply (metis max.cobounded2 linorder_linear max.absorb1 mult_right_mono xt1(6)) | 
| 
82acc20ded73
prefer more canonical names for lemmas on min/max
 haftmann parents: 
54230diff
changeset | 211 | by (metis max.cobounded2 linorder_not_le mult_le_cancel_right order_trans) | 
| 23449 | 212 | |
| 67613 | 213 | lemma bigo_bounded_alt: "\<forall>x. 0 \<le> f x \<Longrightarrow> \<forall>x. f x \<le> c * g x \<Longrightarrow> f \<in> O(g)" | 
| 45575 | 214 | apply (auto simp add: bigo_def) | 
| 36561 | 215 | (* Version 1: one-line proof *) | 
| 45575 | 216 | by (metis abs_le_D1 linorder_class.not_less order_less_le Orderings.xt1(12) abs_mult) | 
| 23449 | 217 | |
| 67613 | 218 | lemma "\<forall>x. 0 \<le> f x \<Longrightarrow> \<forall>x. f x \<le> c * g x \<Longrightarrow> f \<in> O(g)" | 
| 36561 | 219 | apply (auto simp add: bigo_def) | 
| 220 | (* Version 2: structured proof *) | |
| 221 | proof - | |
| 222 | assume "\<forall>x. f x \<le> c * g x" | |
| 223 | thus "\<exists>c. \<forall>x. f x \<le> c * \<bar>g x\<bar>" by (metis abs_mult abs_ge_self order_trans) | |
| 23449 | 224 | qed | 
| 225 | ||
| 67613 | 226 | lemma bigo_bounded: "\<forall>x. 0 \<le> f x \<Longrightarrow> \<forall>x. f x \<le> g x \<Longrightarrow> f \<in> O(g)" | 
| 45575 | 227 | apply (erule bigo_bounded_alt [of f 1 g]) | 
| 228 | by (metis mult_1) | |
| 23449 | 229 | |
| 67613 | 230 | lemma bigo_bounded2: "\<forall>x. lb x \<le> f x \<Longrightarrow> \<forall>x. f x \<le> lb x + g x \<Longrightarrow> f \<in> lb +o O(g)" | 
| 36561 | 231 | apply (rule set_minus_imp_plus) | 
| 232 | apply (rule bigo_bounded) | |
| 46369 | 233 | apply (metis add_le_cancel_left diff_add_cancel diff_self minus_apply | 
| 59554 
4044f53326c9
inlined rules to free user-space from technical names
 haftmann parents: 
58889diff
changeset | 234 | algebra_simps) | 
| 46369 | 235 | by (metis add_le_cancel_left diff_add_cancel func_plus le_fun_def | 
| 59554 
4044f53326c9
inlined rules to free user-space from technical names
 haftmann parents: 
58889diff
changeset | 236 | algebra_simps) | 
| 23449 | 237 | |
| 61945 | 238 | lemma bigo_abs: "(\<lambda>x. \<bar>f x\<bar>) =o O(f)" | 
| 36561 | 239 | apply (unfold bigo_def) | 
| 240 | apply auto | |
| 36844 | 241 | by (metis mult_1 order_refl) | 
| 23449 | 242 | |
| 61945 | 243 | lemma bigo_abs2: "f =o O(\<lambda>x. \<bar>f x\<bar>)" | 
| 36561 | 244 | apply (unfold bigo_def) | 
| 245 | apply auto | |
| 36844 | 246 | by (metis mult_1 order_refl) | 
| 43197 | 247 | |
| 61945 | 248 | lemma bigo_abs3: "O(f) = O(\<lambda>x. \<bar>f x\<bar>)" | 
| 36561 | 249 | proof - | 
| 250 | have F1: "\<forall>v u. u \<in> O(v) \<longrightarrow> O(u) \<subseteq> O(v)" by (metis bigo_elt_subset) | |
| 251 | have F2: "\<forall>u. (\<lambda>R. \<bar>u R\<bar>) \<in> O(u)" by (metis bigo_abs) | |
| 252 | have "\<forall>u. u \<in> O(\<lambda>R. \<bar>u R\<bar>)" by (metis bigo_abs2) | |
| 253 | thus "O(f) = O(\<lambda>x. \<bar>f x\<bar>)" using F1 F2 by auto | |
| 43197 | 254 | qed | 
| 23449 | 255 | |
| 61945 | 256 | lemma bigo_abs4: "f =o g +o O(h) \<Longrightarrow> (\<lambda>x. \<bar>f x\<bar>) =o (\<lambda>x. \<bar>g x\<bar>) +o O(h)" | 
| 23449 | 257 | apply (drule set_plus_imp_minus) | 
| 258 | apply (rule set_minus_imp_plus) | |
| 26814 
b3e8d5ec721d
Replaced + and * on sets by \<oplus> and \<otimes>, to avoid clash with
 berghofe parents: 
26645diff
changeset | 259 | apply (subst fun_diff_def) | 
| 23449 | 260 | proof - | 
| 67613 | 261 | assume a: "f - g \<in> O(h)" | 
| 61945 | 262 | have "(\<lambda>x. \<bar>f x\<bar> - \<bar>g x\<bar>) =o O(\<lambda>x. \<bar>\<bar>f x\<bar> - \<bar>g x\<bar>\<bar>)" | 
| 23449 | 263 | by (rule bigo_abs2) | 
| 67613 | 264 | also have "\<dots> <= O(\<lambda>x. \<bar>f x - g x\<bar>)" | 
| 23449 | 265 | apply (rule bigo_elt_subset) | 
| 266 | apply (rule bigo_bounded) | |
| 46369 | 267 | apply (metis abs_ge_zero) | 
| 268 | by (metis abs_triangle_ineq3) | |
| 67613 | 269 | also have "\<dots> <= O(f - g)" | 
| 23449 | 270 | apply (rule bigo_elt_subset) | 
| 26814 
b3e8d5ec721d
Replaced + and * on sets by \<oplus> and \<otimes>, to avoid clash with
 berghofe parents: 
26645diff
changeset | 271 | apply (subst fun_diff_def) | 
| 23449 | 272 | apply (rule bigo_abs) | 
| 273 | done | |
| 67613 | 274 | also have "\<dots> <= O(h)" | 
| 23464 | 275 | using a by (rule bigo_elt_subset) | 
| 67613 | 276 | finally show "(\<lambda>x. \<bar>f x\<bar> - \<bar>g x\<bar>) \<in> O(h)" . | 
| 23449 | 277 | qed | 
| 278 | ||
| 61945 | 279 | lemma bigo_abs5: "f =o O(g) \<Longrightarrow> (\<lambda>x. \<bar>f x\<bar>) =o O(g)" | 
| 23449 | 280 | by (unfold bigo_def, auto) | 
| 281 | ||
| 67613 | 282 | lemma bigo_elt_subset2 [intro]: "f \<in> g +o O(h) \<Longrightarrow> O(f) \<le> O(g) + O(h)" | 
| 23449 | 283 | proof - | 
| 67613 | 284 | assume "f \<in> g +o O(h)" | 
| 285 | also have "\<dots> \<le> O(g) + O(h)" | |
| 23449 | 286 | by (auto del: subsetI) | 
| 67613 | 287 | also have "\<dots> = O(\<lambda>x. \<bar>g x\<bar>) + O(\<lambda>x. \<bar>h x\<bar>)" | 
| 46369 | 288 | by (metis bigo_abs3) | 
| 61945 | 289 | also have "... = O((\<lambda>x. \<bar>g x\<bar>) + (\<lambda>x. \<bar>h x\<bar>))" | 
| 23449 | 290 | by (rule bigo_plus_eq [symmetric], auto) | 
| 67613 | 291 | finally have "f \<in> \<dots>". | 
| 292 | then have "O(f) \<le> \<dots>" | |
| 23449 | 293 | by (elim bigo_elt_subset) | 
| 67613 | 294 | also have "\<dots> = O(\<lambda>x. \<bar>g x\<bar>) + O(\<lambda>x. \<bar>h x\<bar>)" | 
| 23449 | 295 | by (rule bigo_plus_eq, auto) | 
| 296 | finally show ?thesis | |
| 297 | by (simp add: bigo_abs3 [symmetric]) | |
| 298 | qed | |
| 299 | ||
| 47445 
69e96e5500df
Set_Algebras: removed syntax \<oplus> and \<otimes>, in favour of plain + and *
 krauss parents: 
47108diff
changeset | 300 | lemma bigo_mult [intro]: "O(f) * O(g) <= O(f * g)" | 
| 46369 | 301 | apply (rule subsetI) | 
| 302 | apply (subst bigo_def) | |
| 57514 
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
 haftmann parents: 
57512diff
changeset | 303 | apply (auto simp del: abs_mult ac_simps | 
| 46369 | 304 | simp add: bigo_alt_def set_times_def func_times) | 
| 45575 | 305 | (* sledgehammer *) | 
| 46369 | 306 | apply (rule_tac x = "c * ca" in exI) | 
| 307 | apply (rule allI) | |
| 308 | apply (erule_tac x = x in allE)+ | |
| 61945 | 309 | apply (subgoal_tac "c * ca * \<bar>f x * g x\<bar> = (c * \<bar>f x\<bar>) * (ca * \<bar>g x\<bar>)") | 
| 46369 | 310 | apply (metis (no_types) abs_ge_zero abs_mult mult_mono') | 
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
57418diff
changeset | 311 | by (metis mult.assoc mult.left_commute abs_of_pos mult.left_commute abs_mult) | 
| 23449 | 312 | |
| 313 | lemma bigo_mult2 [intro]: "f *o O(g) <= O(f * g)" | |
| 46369 | 314 | by (metis bigo_mult bigo_refl set_times_mono3 subset_trans) | 
| 23449 | 315 | |
| 67613 | 316 | lemma bigo_mult3: "f \<in> O(h) \<Longrightarrow> g \<in> O(j) \<Longrightarrow> f * g \<in> O(h * j)" | 
| 69712 | 317 | by (metis bigo_mult rev_subsetD set_times_intro) | 
| 23449 | 318 | |
| 67613 | 319 | lemma bigo_mult4 [intro]:"f \<in> k +o O(h) \<Longrightarrow> g * f \<in> (g * k) +o O(g * h)" | 
| 23449 | 320 | by (metis bigo_mult2 set_plus_mono_b set_times_intro2 set_times_plus_distrib) | 
| 321 | ||
| 45575 | 322 | lemma bigo_mult5: "\<forall>x. f x ~= 0 \<Longrightarrow> | 
| 61076 | 323 |     O(f * g) <= (f::'a => ('b::linordered_field)) *o O(g)"
 | 
| 23449 | 324 | proof - | 
| 67613 | 325 | assume a: "\<forall>x. f x \<noteq> 0" | 
| 23449 | 326 | show "O(f * g) <= f *o O(g)" | 
| 327 | proof | |
| 328 | fix h | |
| 67613 | 329 | assume h: "h \<in> O(f * g)" | 
| 330 | then have "(\<lambda>x. 1 / (f x)) * h \<in> (\<lambda>x. 1 / f x) *o O(f * g)" | |
| 23449 | 331 | by auto | 
| 45575 | 332 | also have "... <= O((\<lambda>x. 1 / f x) * (f * g))" | 
| 23449 | 333 | by (rule bigo_mult2) | 
| 45575 | 334 | also have "(\<lambda>x. 1 / f x) * (f * g) = g" | 
| 59867 
58043346ca64
given up separate type classes demanding `inverse 0 = 0`
 haftmann parents: 
59557diff
changeset | 335 | by (simp add: fun_eq_iff a) | 
| 67613 | 336 | finally have "(\<lambda>x. (1::'b) / f x) * h \<in> O(g)". | 
| 337 | then have "f * ((\<lambda>x. (1::'b) / f x) * h) \<in> f *o O(g)" | |
| 23449 | 338 | by auto | 
| 61076 | 339 | also have "f * ((\<lambda>x. (1::'b) / f x) * h) = h" | 
| 59554 
4044f53326c9
inlined rules to free user-space from technical names
 haftmann parents: 
58889diff
changeset | 340 | by (simp add: func_times fun_eq_iff a) | 
| 67613 | 341 | finally show "h \<in> f *o O(g)". | 
| 23449 | 342 | qed | 
| 343 | qed | |
| 344 | ||
| 46369 | 345 | lemma bigo_mult6: | 
| 61076 | 346 | "\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) = (f::'a \<Rightarrow> ('b::linordered_field)) *o O(g)"
 | 
| 23449 | 347 | by (metis bigo_mult2 bigo_mult5 order_antisym) | 
| 348 | ||
| 349 | (*proof requires relaxing relevance: 2007-01-25*) | |
| 45705 | 350 | declare bigo_mult6 [simp] | 
| 351 | ||
| 46369 | 352 | lemma bigo_mult7: | 
| 61076 | 353 | "\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) \<le> O(f::'a \<Rightarrow> ('b::linordered_field)) * O(g)"
 | 
| 46369 | 354 | by (metis bigo_refl bigo_mult6 set_times_mono3) | 
| 23449 | 355 | |
| 45575 | 356 | declare bigo_mult6 [simp del] | 
| 357 | declare bigo_mult7 [intro!] | |
| 358 | ||
| 46369 | 359 | lemma bigo_mult8: | 
| 61076 | 360 | "\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) = O(f::'a \<Rightarrow> ('b::linordered_field)) * O(g)"
 | 
| 23449 | 361 | by (metis bigo_mult bigo_mult7 order_antisym_conv) | 
| 362 | ||
| 67613 | 363 | lemma bigo_minus [intro]: "f \<in> O(g) \<Longrightarrow> - f \<in> O(g)" | 
| 46369 | 364 | by (auto simp add: bigo_def fun_Compl_def) | 
| 23449 | 365 | |
| 67613 | 366 | lemma bigo_minus2: "f \<in> g +o O(h) \<Longrightarrow> -f \<in> -g +o O(h)" | 
| 59554 
4044f53326c9
inlined rules to free user-space from technical names
 haftmann parents: 
58889diff
changeset | 367 | by (metis (no_types, lifting) bigo_minus diff_minus_eq_add minus_add_distrib | 
| 
4044f53326c9
inlined rules to free user-space from technical names
 haftmann parents: 
58889diff
changeset | 368 | minus_minus set_minus_imp_plus set_plus_imp_minus) | 
| 23449 | 369 | |
| 370 | lemma bigo_minus3: "O(-f) = O(f)" | |
| 46369 | 371 | by (metis bigo_elt_subset bigo_minus bigo_refl equalityI minus_minus) | 
| 23449 | 372 | |
| 67613 | 373 | lemma bigo_plus_absorb_lemma1: "f \<in> O(g) \<Longrightarrow> f +o O(g) \<le> O(g)" | 
| 46369 | 374 | by (metis bigo_plus_idemp set_plus_mono3) | 
| 23449 | 375 | |
| 67613 | 376 | lemma bigo_plus_absorb_lemma2: "f \<in> O(g) \<Longrightarrow> O(g) \<le> f +o O(g)" | 
| 46369 | 377 | by (metis (no_types) bigo_minus bigo_plus_absorb_lemma1 right_minus | 
| 46644 | 378 | set_plus_mono set_plus_rearrange2 set_zero_plus subsetD subset_refl | 
| 379 | subset_trans) | |
| 23449 | 380 | |
| 67613 | 381 | lemma bigo_plus_absorb [simp]: "f \<in> O(g) \<Longrightarrow> f +o O(g) = O(g)" | 
| 41865 
4e8483cc2cc5
declare ext [intro]: Extensionality now available by default
 paulson parents: 
41541diff
changeset | 382 | by (metis bigo_plus_absorb_lemma1 bigo_plus_absorb_lemma2 order_eq_iff) | 
| 23449 | 383 | |
| 67613 | 384 | lemma bigo_plus_absorb2 [intro]: "f \<in> O(g) \<Longrightarrow> A \<subseteq> O(g) \<Longrightarrow> f +o A \<subseteq> O(g)" | 
| 46369 | 385 | by (metis bigo_plus_absorb set_plus_mono) | 
| 23449 | 386 | |
| 67613 | 387 | lemma bigo_add_commute_imp: "f \<in> g +o O(h) \<Longrightarrow> g \<in> f +o O(h)" | 
| 46369 | 388 | by (metis bigo_minus minus_diff_eq set_plus_imp_minus set_minus_plus) | 
| 23449 | 389 | |
| 67613 | 390 | lemma bigo_add_commute: "(f \<in> g +o O(h)) = (g \<in> f +o O(h))" | 
| 46369 | 391 | by (metis bigo_add_commute_imp) | 
| 23449 | 392 | |
| 67613 | 393 | lemma bigo_const1: "(\<lambda>x. c) \<in> O(\<lambda>x. 1)" | 
| 57514 
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
 haftmann parents: 
57512diff
changeset | 394 | by (auto simp add: bigo_def ac_simps) | 
| 23449 | 395 | |
| 46369 | 396 | lemma bigo_const2 [intro]: "O(\<lambda>x. c) \<le> O(\<lambda>x. 1)" | 
| 41865 
4e8483cc2cc5
declare ext [intro]: Extensionality now available by default
 paulson parents: 
41541diff
changeset | 397 | by (metis bigo_const1 bigo_elt_subset) | 
| 23449 | 398 | |
| 67613 | 399 | lemma bigo_const3: "(c::'a::linordered_field) \<noteq> 0 \<Longrightarrow> (\<lambda>x. 1) \<in> O(\<lambda>x. c)" | 
| 23449 | 400 | apply (simp add: bigo_def) | 
| 36561 | 401 | by (metis abs_eq_0 left_inverse order_refl) | 
| 23449 | 402 | |
| 67613 | 403 | lemma bigo_const4: "(c::'a::linordered_field) \<noteq> 0 \<Longrightarrow> O(\<lambda>x. 1) \<subseteq> O(\<lambda>x. c)" | 
| 46369 | 404 | by (metis bigo_elt_subset bigo_const3) | 
| 23449 | 405 | |
| 61076 | 406 | lemma bigo_const [simp]: "(c::'a::linordered_field) ~= 0 \<Longrightarrow> | 
| 45575 | 407 | O(\<lambda>x. c) = O(\<lambda>x. 1)" | 
| 46369 | 408 | by (metis bigo_const2 bigo_const4 equalityI) | 
| 23449 | 409 | |
| 67613 | 410 | lemma bigo_const_mult1: "(\<lambda>x. c * f x) \<in> O(f)" | 
| 46369 | 411 | apply (simp add: bigo_def abs_mult) | 
| 36561 | 412 | by (metis le_less) | 
| 23449 | 413 | |
| 46369 | 414 | lemma bigo_const_mult2: "O(\<lambda>x. c * f x) \<le> O(f)" | 
| 23449 | 415 | by (rule bigo_elt_subset, rule bigo_const_mult1) | 
| 416 | ||
| 67613 | 417 | lemma bigo_const_mult3: "(c::'a::linordered_field) \<noteq> 0 \<Longrightarrow> f \<in> O(\<lambda>x. c * f x)" | 
| 45575 | 418 | apply (simp add: bigo_def) | 
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
57418diff
changeset | 419 | by (metis (no_types) abs_mult mult.assoc mult_1 order_refl left_inverse) | 
| 23449 | 420 | |
| 46369 | 421 | lemma bigo_const_mult4: | 
| 61076 | 422 | "(c::'a::linordered_field) \<noteq> 0 \<Longrightarrow> O(f) \<le> O(\<lambda>x. c * f x)" | 
| 46369 | 423 | by (metis bigo_elt_subset bigo_const_mult3) | 
| 23449 | 424 | |
| 67613 | 425 | lemma bigo_const_mult [simp]: "(c::'a::linordered_field) \<noteq> 0 \<Longrightarrow> | 
| 45575 | 426 | O(\<lambda>x. c * f x) = O(f)" | 
| 46369 | 427 | by (metis equalityI bigo_const_mult2 bigo_const_mult4) | 
| 23449 | 428 | |
| 67613 | 429 | lemma bigo_const_mult5 [simp]: "(c::'a::linordered_field) \<noteq> 0 \<Longrightarrow> | 
| 45575 | 430 | (\<lambda>x. c) *o O(f) = O(f)" | 
| 23449 | 431 | apply (auto del: subsetI) | 
| 432 | apply (rule order_trans) | |
| 433 | apply (rule bigo_mult2) | |
| 434 | apply (simp add: func_times) | |
| 435 | apply (auto intro!: subsetI simp add: bigo_def elt_set_times_def func_times) | |
| 45575 | 436 | apply (rule_tac x = "\<lambda>y. inverse c * x y" in exI) | 
| 43197 | 437 | apply (rename_tac g d) | 
| 24942 | 438 | apply safe | 
| 43197 | 439 | apply (rule_tac [2] ext) | 
| 440 | prefer 2 | |
| 26041 
c2e15e65165f
locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
 haftmann parents: 
25710diff
changeset | 441 | apply simp | 
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
57418diff
changeset | 442 | apply (simp add: mult.assoc [symmetric] abs_mult) | 
| 39259 
194014eb4f9f
replace two slow "metis" proofs with faster proofs
 blanchet parents: 
38991diff
changeset | 443 | (* couldn't get this proof without the step above *) | 
| 
194014eb4f9f
replace two slow "metis" proofs with faster proofs
 blanchet parents: 
38991diff
changeset | 444 | proof - | 
| 
194014eb4f9f
replace two slow "metis" proofs with faster proofs
 blanchet parents: 
38991diff
changeset | 445 | fix g :: "'b \<Rightarrow> 'a" and d :: 'a | 
| 61076 | 446 | assume A1: "c \<noteq> (0::'a)" | 
| 447 | assume A2: "\<forall>x::'b. \<bar>g x\<bar> \<le> d * \<bar>f x\<bar>" | |
| 39259 
194014eb4f9f
replace two slow "metis" proofs with faster proofs
 blanchet parents: 
38991diff
changeset | 448 | have F1: "inverse \<bar>c\<bar> = \<bar>inverse c\<bar>" using A1 by (metis nonzero_abs_inverse) | 
| 61076 | 449 | have F2: "(0::'a) < \<bar>c\<bar>" using A1 by (metis zero_less_abs_iff) | 
| 450 | have "(0::'a) < \<bar>c\<bar> \<longrightarrow> (0::'a) < \<bar>inverse c\<bar>" using F1 by (metis positive_imp_inverse_positive) | |
| 451 | hence "(0::'a) < \<bar>inverse c\<bar>" using F2 by metis | |
| 452 | hence F3: "(0::'a) \<le> \<bar>inverse c\<bar>" by (metis order_le_less) | |
| 453 | have "\<exists>(u::'a) SKF\<^sub>7::'a \<Rightarrow> 'b. \<bar>g (SKF\<^sub>7 (\<bar>inverse c\<bar> * u))\<bar> \<le> u * \<bar>f (SKF\<^sub>7 (\<bar>inverse c\<bar> * u))\<bar>" | |
| 39259 
194014eb4f9f
replace two slow "metis" proofs with faster proofs
 blanchet parents: 
38991diff
changeset | 454 | using A2 by metis | 
| 61076 | 455 | hence F4: "\<exists>(u::'a) SKF\<^sub>7::'a \<Rightarrow> 'b. \<bar>g (SKF\<^sub>7 (\<bar>inverse c\<bar> * u))\<bar> \<le> u * \<bar>f (SKF\<^sub>7 (\<bar>inverse c\<bar> * u))\<bar> \<and> (0::'a) \<le> \<bar>inverse c\<bar>" | 
| 39259 
194014eb4f9f
replace two slow "metis" proofs with faster proofs
 blanchet parents: 
38991diff
changeset | 456 | using F3 by metis | 
| 61076 | 457 | hence "\<exists>(v::'a) (u::'a) SKF\<^sub>7::'a \<Rightarrow> 'b. \<bar>inverse c\<bar> * \<bar>g (SKF\<^sub>7 (u * v))\<bar> \<le> u * (v * \<bar>f (SKF\<^sub>7 (u * v))\<bar>)" | 
| 59557 | 458 | by (metis mult_left_mono) | 
| 61076 | 459 | then show "\<exists>ca::'a. \<forall>x::'b. inverse \<bar>c\<bar> * \<bar>g x\<bar> \<le> ca * \<bar>f x\<bar>" | 
| 68536 | 460 | using A2 F4 by (metis F1 \<open>0 < \<bar>inverse c\<bar>\<close> mult.assoc mult_le_cancel_left_pos) | 
| 39259 
194014eb4f9f
replace two slow "metis" proofs with faster proofs
 blanchet parents: 
38991diff
changeset | 461 | qed | 
| 23449 | 462 | |
| 45575 | 463 | lemma bigo_const_mult6 [intro]: "(\<lambda>x. c) *o O(f) <= O(f)" | 
| 23449 | 464 | apply (auto intro!: subsetI | 
| 465 | simp add: bigo_def elt_set_times_def func_times | |
| 57514 
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
 haftmann parents: 
57512diff
changeset | 466 | simp del: abs_mult ac_simps) | 
| 45575 | 467 | (* sledgehammer *) | 
| 61945 | 468 | apply (rule_tac x = "ca * \<bar>c\<bar>" in exI) | 
| 23449 | 469 | apply (rule allI) | 
| 61945 | 470 | apply (subgoal_tac "ca * \<bar>c\<bar> * \<bar>f x\<bar> = \<bar>c\<bar> * (ca * \<bar>f x\<bar>)") | 
| 23449 | 471 | apply (erule ssubst) | 
| 472 | apply (subst abs_mult) | |
| 473 | apply (rule mult_left_mono) | |
| 474 | apply (erule spec) | |
| 475 | apply simp | |
| 57514 
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
 haftmann parents: 
57512diff
changeset | 476 | apply (simp add: ac_simps) | 
| 23449 | 477 | done | 
| 478 | ||
| 45575 | 479 | lemma bigo_const_mult7 [intro]: "f =o O(g) \<Longrightarrow> (\<lambda>x. c * f x) =o O(g)" | 
| 46369 | 480 | by (metis bigo_const_mult1 bigo_elt_subset order_less_le psubsetD) | 
| 23449 | 481 | |
| 45575 | 482 | lemma bigo_compose1: "f =o O(g) \<Longrightarrow> (\<lambda>x. f(k x)) =o O(\<lambda>x. g(k x))" | 
| 23449 | 483 | by (unfold bigo_def, auto) | 
| 484 | ||
| 46369 | 485 | lemma bigo_compose2: | 
| 486 | "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))" | |
| 54230 
b1d955791529
more simplification rules on unary and binary minus
 haftmann parents: 
53015diff
changeset | 487 | apply (simp only: set_minus_plus [symmetric] fun_Compl_def func_plus) | 
| 
b1d955791529
more simplification rules on unary and binary minus
 haftmann parents: 
53015diff
changeset | 488 | apply (drule bigo_compose1 [of "f - g" h k]) | 
| 
b1d955791529
more simplification rules on unary and binary minus
 haftmann parents: 
53015diff
changeset | 489 | apply (simp add: fun_diff_def) | 
| 
b1d955791529
more simplification rules on unary and binary minus
 haftmann parents: 
53015diff
changeset | 490 | done | 
| 23449 | 491 | |
| 64267 | 492 | subsection \<open>Sum\<close> | 
| 23449 | 493 | |
| 67613 | 494 | lemma bigo_sum_main: "\<forall>x. \<forall>y \<in> A x. 0 \<le> h x y \<Longrightarrow> | 
| 61945 | 495 | \<exists>c. \<forall>x. \<forall>y \<in> A x. \<bar>f x y\<bar> <= c * (h x y) \<Longrightarrow> | 
| 61954 | 496 | (\<lambda>x. \<Sum>y \<in> A x. f x y) =o O(\<lambda>x. \<Sum>y \<in> A x. h x y)" | 
| 46369 | 497 | apply (auto simp add: bigo_def) | 
| 61945 | 498 | apply (rule_tac x = "\<bar>c\<bar>" in exI) | 
| 46369 | 499 | apply (subst abs_of_nonneg) back back | 
| 64267 | 500 | apply (rule sum_nonneg) | 
| 46369 | 501 | apply force | 
| 64267 | 502 | apply (subst sum_distrib_left) | 
| 46369 | 503 | apply (rule allI) | 
| 504 | apply (rule order_trans) | |
| 64267 | 505 | apply (rule sum_abs) | 
| 506 | apply (rule sum_mono) | |
| 46369 | 507 | by (metis abs_ge_self abs_mult_pos order_trans) | 
| 23449 | 508 | |
| 64267 | 509 | lemma bigo_sum1: "\<forall>x y. 0 <= h x y \<Longrightarrow> | 
| 61945 | 510 | \<exists>c. \<forall>x y. \<bar>f x y\<bar> <= c * (h x y) \<Longrightarrow> | 
| 61954 | 511 | (\<lambda>x. \<Sum>y \<in> A x. f x y) =o O(\<lambda>x. \<Sum>y \<in> A x. h x y)" | 
| 64267 | 512 | by (metis (no_types) bigo_sum_main) | 
| 23449 | 513 | |
| 64267 | 514 | lemma bigo_sum2: "\<forall>y. 0 <= h y \<Longrightarrow> | 
| 61945 | 515 | \<exists>c. \<forall>y. \<bar>f y\<bar> <= c * (h y) \<Longrightarrow> | 
| 61954 | 516 | (\<lambda>x. \<Sum>y \<in> A x. f y) =o O(\<lambda>x. \<Sum>y \<in> A x. h y)" | 
| 64267 | 517 | apply (rule bigo_sum1) | 
| 46369 | 518 | by metis+ | 
| 23449 | 519 | |
| 64267 | 520 | lemma bigo_sum3: "f =o O(h) \<Longrightarrow> | 
| 61954 | 521 | (\<lambda>x. \<Sum>y \<in> A x. (l x y) * f(k x y)) =o | 
| 522 | O(\<lambda>x. \<Sum>y \<in> A x. \<bar>l x y * h(k x y)\<bar>)" | |
| 64267 | 523 | apply (rule bigo_sum1) | 
| 45575 | 524 | apply (rule allI)+ | 
| 525 | apply (rule abs_ge_zero) | |
| 526 | apply (unfold bigo_def) | |
| 527 | apply (auto simp add: abs_mult) | |
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
57418diff
changeset | 528 | by (metis abs_ge_zero mult.left_commute mult_left_mono) | 
| 23449 | 529 | |
| 64267 | 530 | lemma bigo_sum4: "f =o g +o O(h) \<Longrightarrow> | 
| 61954 | 531 | (\<lambda>x. \<Sum>y \<in> A x. l x y * f(k x y)) =o | 
| 532 | (\<lambda>x. \<Sum>y \<in> A x. l x y * g(k x y)) +o | |
| 533 | O(\<lambda>x. \<Sum>y \<in> A x. \<bar>l x y * h(k x y)\<bar>)" | |
| 45575 | 534 | apply (rule set_minus_imp_plus) | 
| 535 | apply (subst fun_diff_def) | |
| 64267 | 536 | apply (subst sum_subtractf [symmetric]) | 
| 45575 | 537 | apply (subst right_diff_distrib [symmetric]) | 
| 64267 | 538 | apply (rule bigo_sum3) | 
| 46369 | 539 | by (metis (lifting, no_types) fun_diff_def set_plus_imp_minus ext) | 
| 23449 | 540 | |
| 64267 | 541 | lemma bigo_sum5: "f =o O(h) \<Longrightarrow> \<forall>x y. 0 <= l x y \<Longrightarrow> | 
| 45575 | 542 | \<forall>x. 0 <= h x \<Longrightarrow> | 
| 61954 | 543 | (\<lambda>x. \<Sum>y \<in> A x. (l x y) * f(k x y)) =o | 
| 544 | O(\<lambda>x. \<Sum>y \<in> A x. (l x y) * h(k x y))" | |
| 545 | apply (subgoal_tac "(\<lambda>x. \<Sum>y \<in> A x. (l x y) * h(k x y)) = | |
| 546 | (\<lambda>x. \<Sum>y \<in> A x. \<bar>(l x y) * h(k x y)\<bar>)") | |
| 46369 | 547 | apply (erule ssubst) | 
| 64267 | 548 | apply (erule bigo_sum3) | 
| 46369 | 549 | apply (rule ext) | 
| 64267 | 550 | apply (rule sum.cong) | 
| 57418 | 551 | apply (rule refl) | 
| 46369 | 552 | by (metis abs_of_nonneg zero_le_mult_iff) | 
| 23449 | 553 | |
| 64267 | 554 | lemma bigo_sum6: "f =o g +o O(h) \<Longrightarrow> \<forall>x y. 0 <= l x y \<Longrightarrow> | 
| 45575 | 555 | \<forall>x. 0 <= h x \<Longrightarrow> | 
| 61954 | 556 | (\<lambda>x. \<Sum>y \<in> A x. (l x y) * f(k x y)) =o | 
| 557 | (\<lambda>x. \<Sum>y \<in> A x. (l x y) * g(k x y)) +o | |
| 558 | O(\<lambda>x. \<Sum>y \<in> A x. (l x y) * h(k x y))" | |
| 23449 | 559 | apply (rule set_minus_imp_plus) | 
| 26814 
b3e8d5ec721d
Replaced + and * on sets by \<oplus> and \<otimes>, to avoid clash with
 berghofe parents: 
26645diff
changeset | 560 | apply (subst fun_diff_def) | 
| 64267 | 561 | apply (subst sum_subtractf [symmetric]) | 
| 23449 | 562 | apply (subst right_diff_distrib [symmetric]) | 
| 64267 | 563 | apply (rule bigo_sum5) | 
| 26814 
b3e8d5ec721d
Replaced + and * on sets by \<oplus> and \<otimes>, to avoid clash with
 berghofe parents: 
26645diff
changeset | 564 | apply (subst fun_diff_def [symmetric]) | 
| 23449 | 565 | apply (drule set_plus_imp_minus) | 
| 566 | apply auto | |
| 567 | done | |
| 568 | ||
| 63167 | 569 | subsection \<open>Misc useful stuff\<close> | 
| 23449 | 570 | |
| 45575 | 571 | lemma bigo_useful_intro: "A <= O(f) \<Longrightarrow> B <= O(f) \<Longrightarrow> | 
| 47445 
69e96e5500df
Set_Algebras: removed syntax \<oplus> and \<otimes>, in favour of plain + and *
 krauss parents: 
47108diff
changeset | 572 | A + B <= O(f)" | 
| 23449 | 573 | apply (subst bigo_plus_idemp [symmetric]) | 
| 574 | apply (rule set_plus_mono2) | |
| 575 | apply assumption+ | |
| 576 | done | |
| 577 | ||
| 45575 | 578 | lemma bigo_useful_add: "f =o O(h) \<Longrightarrow> g =o O(h) \<Longrightarrow> f + g =o O(h)" | 
| 23449 | 579 | apply (subst bigo_plus_idemp [symmetric]) | 
| 580 | apply (rule set_plus_intro) | |
| 581 | apply assumption+ | |
| 582 | done | |
| 43197 | 583 | |
| 61076 | 584 | lemma bigo_useful_const_mult: "(c::'a::linordered_field) ~= 0 \<Longrightarrow> | 
| 45575 | 585 | (\<lambda>x. c) * f =o O(h) \<Longrightarrow> f =o O(h)" | 
| 23449 | 586 | apply (rule subsetD) | 
| 45575 | 587 | apply (subgoal_tac "(\<lambda>x. 1 / c) *o O(h) <= O(h)") | 
| 23449 | 588 | apply assumption | 
| 589 | apply (rule bigo_const_mult6) | |
| 45575 | 590 | apply (subgoal_tac "f = (\<lambda>x. 1 / c) * ((\<lambda>x. c) * f)") | 
| 23449 | 591 | apply (erule ssubst) | 
| 592 | apply (erule set_times_intro2) | |
| 43197 | 593 | apply (simp add: func_times) | 
| 23449 | 594 | done | 
| 595 | ||
| 61076 | 596 | lemma bigo_fix: "(\<lambda>x. f ((x::nat) + 1)) =o O(\<lambda>x. h(x + 1)) \<Longrightarrow> f 0 = 0 \<Longrightarrow> | 
| 23449 | 597 | f =o O(h)" | 
| 45575 | 598 | apply (simp add: bigo_alt_def) | 
| 599 | by (metis abs_ge_zero abs_mult abs_of_pos abs_zero not0_implies_Suc) | |
| 23449 | 600 | |
| 43197 | 601 | lemma bigo_fix2: | 
| 61076 | 602 | "(\<lambda>x. f ((x::nat) + 1)) =o (\<lambda>x. g(x + 1)) +o O(\<lambda>x. h(x + 1)) \<Longrightarrow> | 
| 45575 | 603 | f 0 = g 0 \<Longrightarrow> f =o g +o O(h)" | 
| 23449 | 604 | apply (rule set_minus_imp_plus) | 
| 605 | apply (rule bigo_fix) | |
| 26814 
b3e8d5ec721d
Replaced + and * on sets by \<oplus> and \<otimes>, to avoid clash with
 berghofe parents: 
26645diff
changeset | 606 | apply (subst fun_diff_def) | 
| 
b3e8d5ec721d
Replaced + and * on sets by \<oplus> and \<otimes>, to avoid clash with
 berghofe parents: 
26645diff
changeset | 607 | apply (subst fun_diff_def [symmetric]) | 
| 23449 | 608 | apply (rule set_plus_imp_minus) | 
| 609 | apply simp | |
| 26814 
b3e8d5ec721d
Replaced + and * on sets by \<oplus> and \<otimes>, to avoid clash with
 berghofe parents: 
26645diff
changeset | 610 | apply (simp add: fun_diff_def) | 
| 23449 | 611 | done | 
| 612 | ||
| 63167 | 613 | subsection \<open>Less than or equal to\<close> | 
| 23449 | 614 | |
| 61076 | 615 | definition lesso :: "('a => 'b::linordered_idom) => ('a => 'b) => ('a => 'b)" (infixl "<o" 70) where
 | 
| 45575 | 616 | "f <o g == (\<lambda>x. max (f x - g x) 0)" | 
| 23449 | 617 | |
| 61945 | 618 | lemma bigo_lesseq1: "f =o O(h) \<Longrightarrow> \<forall>x. \<bar>g x\<bar> <= \<bar>f x\<bar> \<Longrightarrow> | 
| 23449 | 619 | g =o O(h)" | 
| 620 | apply (unfold bigo_def) | |
| 621 | apply clarsimp | |
| 43197 | 622 | apply (blast intro: order_trans) | 
| 23449 | 623 | done | 
| 624 | ||
| 61945 | 625 | lemma bigo_lesseq2: "f =o O(h) \<Longrightarrow> \<forall>x. \<bar>g x\<bar> <= f x \<Longrightarrow> | 
| 23449 | 626 | g =o O(h)" | 
| 627 | apply (erule bigo_lesseq1) | |
| 43197 | 628 | apply (blast intro: abs_ge_self order_trans) | 
| 23449 | 629 | done | 
| 630 | ||
| 45575 | 631 | lemma bigo_lesseq3: "f =o O(h) \<Longrightarrow> \<forall>x. 0 <= g x \<Longrightarrow> \<forall>x. g x <= f x \<Longrightarrow> | 
| 23449 | 632 | g =o O(h)" | 
| 633 | apply (erule bigo_lesseq2) | |
| 634 | apply (rule allI) | |
| 635 | apply (subst abs_of_nonneg) | |
| 636 | apply (erule spec)+ | |
| 637 | done | |
| 638 | ||
| 45575 | 639 | lemma bigo_lesseq4: "f =o O(h) \<Longrightarrow> | 
| 61945 | 640 | \<forall>x. 0 <= g x \<Longrightarrow> \<forall>x. g x <= \<bar>f x\<bar> \<Longrightarrow> | 
| 23449 | 641 | g =o O(h)" | 
| 642 | apply (erule bigo_lesseq1) | |
| 643 | apply (rule allI) | |
| 644 | apply (subst abs_of_nonneg) | |
| 645 | apply (erule spec)+ | |
| 646 | done | |
| 647 | ||
| 45575 | 648 | lemma bigo_lesso1: "\<forall>x. f x <= g x \<Longrightarrow> f <o g =o O(h)" | 
| 36561 | 649 | apply (unfold lesso_def) | 
| 45575 | 650 | apply (subgoal_tac "(\<lambda>x. max (f x - g x) 0) = 0") | 
| 651 | apply (metis bigo_zero) | |
| 46364 | 652 | by (metis (lifting, no_types) func_zero le_fun_def le_iff_diff_le_0 | 
| 54863 
82acc20ded73
prefer more canonical names for lemmas on min/max
 haftmann parents: 
54230diff
changeset | 653 | max.absorb2 order_eq_iff) | 
| 23449 | 654 | |
| 45575 | 655 | lemma bigo_lesso2: "f =o g +o O(h) \<Longrightarrow> | 
| 656 | \<forall>x. 0 <= k x \<Longrightarrow> \<forall>x. k x <= f x \<Longrightarrow> | |
| 23449 | 657 | k <o g =o O(h)" | 
| 658 | apply (unfold lesso_def) | |
| 659 | apply (rule bigo_lesseq4) | |
| 660 | apply (erule set_plus_imp_minus) | |
| 661 | apply (rule allI) | |
| 54863 
82acc20ded73
prefer more canonical names for lemmas on min/max
 haftmann parents: 
54230diff
changeset | 662 | apply (rule max.cobounded2) | 
| 23449 | 663 | apply (rule allI) | 
| 26814 
b3e8d5ec721d
Replaced + and * on sets by \<oplus> and \<otimes>, to avoid clash with
 berghofe parents: 
26645diff
changeset | 664 | apply (subst fun_diff_def) | 
| 23449 | 665 | apply (erule thin_rl) | 
| 45575 | 666 | (* sledgehammer *) | 
| 667 | apply (case_tac "0 <= k x - g x") | |
| 46644 | 668 | apply (metis (lifting) abs_le_D1 linorder_linear min_diff_distrib_left | 
| 54863 
82acc20ded73
prefer more canonical names for lemmas on min/max
 haftmann parents: 
54230diff
changeset | 669 | min.absorb1 min.absorb2 max.absorb1) | 
| 
82acc20ded73
prefer more canonical names for lemmas on min/max
 haftmann parents: 
54230diff
changeset | 670 | by (metis abs_ge_zero le_cases max.absorb2) | 
| 23449 | 671 | |
| 45575 | 672 | lemma bigo_lesso3: "f =o g +o O(h) \<Longrightarrow> | 
| 673 | \<forall>x. 0 <= k x \<Longrightarrow> \<forall>x. g x <= k x \<Longrightarrow> | |
| 23449 | 674 | f <o k =o O(h)" | 
| 46644 | 675 | apply (unfold lesso_def) | 
| 676 | apply (rule bigo_lesseq4) | |
| 23449 | 677 | apply (erule set_plus_imp_minus) | 
| 46644 | 678 | apply (rule allI) | 
| 54863 
82acc20ded73
prefer more canonical names for lemmas on min/max
 haftmann parents: 
54230diff
changeset | 679 | apply (rule max.cobounded2) | 
| 46644 | 680 | apply (rule allI) | 
| 681 | apply (subst fun_diff_def) | |
| 682 | apply (erule thin_rl) | |
| 683 | (* sledgehammer *) | |
| 684 | apply (case_tac "0 <= f x - k x") | |
| 685 | apply simp | |
| 686 | apply (subst abs_of_nonneg) | |
| 23449 | 687 | apply (drule_tac x = x in spec) back | 
| 61824 
dcbe9f756ae0
not_leE -> not_le_imp_less and other tidying
 paulson <lp15@cam.ac.uk> parents: 
61076diff
changeset | 688 | apply (metis diff_less_0_iff_less linorder_not_le not_le_imp_less xt1(12) xt1(6)) | 
| 45575 | 689 | apply (metis add_minus_cancel diff_le_eq le_diff_eq uminus_add_conv_diff) | 
| 54863 
82acc20ded73
prefer more canonical names for lemmas on min/max
 haftmann parents: 
54230diff
changeset | 690 | by (metis abs_ge_zero linorder_linear max.absorb1 max.commute) | 
| 23449 | 691 | |
| 45705 | 692 | lemma bigo_lesso4: | 
| 61076 | 693 |   "f <o g =o O(k::'a=>'b::{linordered_field}) \<Longrightarrow>
 | 
| 45705 | 694 | g =o h +o O(k) \<Longrightarrow> f <o h =o O(k)" | 
| 695 | apply (unfold lesso_def) | |
| 696 | apply (drule set_plus_imp_minus) | |
| 697 | apply (drule bigo_abs5) back | |
| 698 | apply (simp add: fun_diff_def) | |
| 699 | apply (drule bigo_useful_add, assumption) | |
| 700 | apply (erule bigo_lesseq2) back | |
| 701 | apply (rule allI) | |
| 702 | by (auto simp add: func_plus fun_diff_def algebra_simps | |
| 23449 | 703 | split: split_max abs_split) | 
| 704 | ||
| 61945 | 705 | lemma bigo_lesso5: "f <o g =o O(h) \<Longrightarrow> \<exists>C. \<forall>x. f x <= g x + C * \<bar>h x\<bar>" | 
| 45705 | 706 | apply (simp only: lesso_def bigo_alt_def) | 
| 707 | apply clarsimp | |
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
57418diff
changeset | 708 | by (metis add.commute diff_le_eq) | 
| 23449 | 709 | |
| 710 | end |