| author | bulwahn | 
| Fri, 11 Nov 2011 12:10:49 +0100 | |
| changeset 45461 | 130c90bb80b4 | 
| parent 45436 | 62bc9474d04b | 
| child 45462 | aba629d6cee5 | 
| permissions | -rw-r--r-- | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
30685diff
changeset | 1 | (* Author: Lawrence C Paulson, Cambridge University Computer Laboratory | 
| 23164 | 2 | |
| 3 | Simprocs for nat numerals. | |
| 4 | *) | |
| 5 | ||
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
30685diff
changeset | 6 | signature NAT_NUMERAL_SIMPROCS = | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
30685diff
changeset | 7 | sig | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
30685diff
changeset | 8 | val combine_numerals: simproc | 
| 45436 
62bc9474d04b
use simproc_setup for some nat_numeral simprocs; add simproc tests
 huffman parents: 
45306diff
changeset | 9 | val eq_cancel_numerals: simpset -> cterm -> thm option | 
| 
62bc9474d04b
use simproc_setup for some nat_numeral simprocs; add simproc tests
 huffman parents: 
45306diff
changeset | 10 | val less_cancel_numerals: simpset -> cterm -> thm option | 
| 
62bc9474d04b
use simproc_setup for some nat_numeral simprocs; add simproc tests
 huffman parents: 
45306diff
changeset | 11 | val le_cancel_numerals: simpset -> cterm -> thm option | 
| 
62bc9474d04b
use simproc_setup for some nat_numeral simprocs; add simproc tests
 huffman parents: 
45306diff
changeset | 12 | val diff_cancel_numerals: simpset -> cterm -> thm option | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
30685diff
changeset | 13 | val cancel_factors: simproc list | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
30685diff
changeset | 14 | val cancel_numeral_factors: simproc list | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
30685diff
changeset | 15 | end; | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
30685diff
changeset | 16 | |
| 23164 | 17 | structure Nat_Numeral_Simprocs = | 
| 18 | struct | |
| 19 | ||
| 20 | (*Maps n to #n for n = 0, 1, 2*) | |
| 30496 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 haftmann parents: 
29269diff
changeset | 21 | val numeral_syms = [@{thm nat_numeral_0_eq_0} RS sym, @{thm nat_numeral_1_eq_1} RS sym, @{thm numeral_2_eq_2} RS sym];
 | 
| 23164 | 22 | val numeral_sym_ss = HOL_ss addsimps numeral_syms; | 
| 23 | ||
| 44945 | 24 | val rename_numerals = simplify numeral_sym_ss o Thm.transfer @{theory};
 | 
| 23164 | 25 | |
| 26 | (*Utilities*) | |
| 27 | ||
| 28 | fun mk_number n = HOLogic.number_of_const HOLogic.natT $ HOLogic.mk_numeral n; | |
| 24630 
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
 wenzelm parents: 
24431diff
changeset | 29 | fun dest_number t = Int.max (0, snd (HOLogic.dest_number t)); | 
| 23164 | 30 | |
| 31 | fun find_first_numeral past (t::terms) = | |
| 32 | ((dest_number t, t, rev past @ terms) | |
| 33 | handle TERM _ => find_first_numeral (t::past) terms) | |
| 34 |   | find_first_numeral past [] = raise TERM("find_first_numeral", []);
 | |
| 35 | ||
| 36 | val zero = mk_number 0; | |
| 35267 
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
 haftmann parents: 
35092diff
changeset | 37 | val mk_plus = HOLogic.mk_binop @{const_name Groups.plus};
 | 
| 23164 | 38 | |
| 39 | (*Thus mk_sum[t] yields t+0; longer sums don't have a trailing zero*) | |
| 40 | fun mk_sum [] = zero | |
| 41 | | mk_sum [t,u] = mk_plus (t, u) | |
| 42 | | mk_sum (t :: ts) = mk_plus (t, mk_sum ts); | |
| 43 | ||
| 44 | (*this version ALWAYS includes a trailing zero*) | |
| 45 | fun long_mk_sum [] = HOLogic.zero | |
| 46 | | long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts); | |
| 47 | ||
| 35267 
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
 haftmann parents: 
35092diff
changeset | 48 | val dest_plus = HOLogic.dest_bin @{const_name Groups.plus} HOLogic.natT;
 | 
| 23164 | 49 | |
| 50 | ||
| 51 | (** Other simproc items **) | |
| 52 | ||
| 53 | val bin_simps = | |
| 23471 | 54 |      [@{thm nat_numeral_0_eq_0} RS sym, @{thm nat_numeral_1_eq_1} RS sym,
 | 
| 55 |       @{thm add_nat_number_of}, @{thm nat_number_of_add_left}, 
 | |
| 56 |       @{thm diff_nat_number_of}, @{thm le_number_of_eq_not_less},
 | |
| 57 |       @{thm mult_nat_number_of}, @{thm nat_number_of_mult_left}, 
 | |
| 58 |       @{thm less_nat_number_of}, 
 | |
| 59 |       @{thm Let_number_of}, @{thm nat_number_of}] @
 | |
| 29039 | 60 |      @{thms arith_simps} @ @{thms rel_simps} @ @{thms neg_simps};
 | 
| 23164 | 61 | |
| 62 | ||
| 63 | (*** CancelNumerals simprocs ***) | |
| 64 | ||
| 65 | val one = mk_number 1; | |
| 35267 
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
 haftmann parents: 
35092diff
changeset | 66 | val mk_times = HOLogic.mk_binop @{const_name Groups.times};
 | 
| 23164 | 67 | |
| 68 | fun mk_prod [] = one | |
| 69 | | mk_prod [t] = t | |
| 70 | | mk_prod (t :: ts) = if t = one then mk_prod ts | |
| 71 | else mk_times (t, mk_prod ts); | |
| 72 | ||
| 35267 
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
 haftmann parents: 
35092diff
changeset | 73 | val dest_times = HOLogic.dest_bin @{const_name Groups.times} HOLogic.natT;
 | 
| 23164 | 74 | |
| 75 | fun dest_prod t = | |
| 76 | let val (t,u) = dest_times t | |
| 77 | in dest_prod t @ dest_prod u end | |
| 78 | handle TERM _ => [t]; | |
| 79 | ||
| 80 | (*DON'T do the obvious simplifications; that would create special cases*) | |
| 81 | fun mk_coeff (k,t) = mk_times (mk_number k, t); | |
| 82 | ||
| 83 | (*Express t as a product of (possibly) a numeral with other factors, sorted*) | |
| 84 | fun dest_coeff t = | |
| 35408 | 85 | let val ts = sort Term_Ord.term_ord (dest_prod t) | 
| 23164 | 86 | val (n, _, ts') = find_first_numeral [] ts | 
| 87 | handle TERM _ => (1, one, ts) | |
| 88 | in (n, mk_prod ts') end; | |
| 89 | ||
| 90 | (*Find first coefficient-term THAT MATCHES u*) | |
| 91 | fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
 | |
| 92 | | find_first_coeff past u (t::terms) = | |
| 93 | let val (n,u') = dest_coeff t | |
| 94 | in if u aconv u' then (n, rev past @ terms) | |
| 95 | else find_first_coeff (t::past) u terms | |
| 96 | end | |
| 97 | handle TERM _ => find_first_coeff (t::past) u terms; | |
| 98 | ||
| 99 | ||
| 100 | (*Split up a sum into the list of its constituent terms, on the way removing any | |
| 101 | Sucs and counting them.*) | |
| 37388 | 102 | fun dest_Suc_sum (Const (@{const_name Suc}, _) $ t, (k,ts)) = dest_Suc_sum (t, (k+1,ts))
 | 
| 23164 | 103 | | dest_Suc_sum (t, (k,ts)) = | 
| 104 | let val (t1,t2) = dest_plus t | |
| 105 | in dest_Suc_sum (t1, dest_Suc_sum (t2, (k,ts))) end | |
| 106 | handle TERM _ => (k, t::ts); | |
| 107 | ||
| 108 | (*Code for testing whether numerals are already used in the goal*) | |
| 25919 
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
 haftmann parents: 
25481diff
changeset | 109 | fun is_numeral (Const(@{const_name Int.number_of}, _) $ w) = true
 | 
| 23164 | 110 | | is_numeral _ = false; | 
| 111 | ||
| 112 | fun prod_has_numeral t = exists is_numeral (dest_prod t); | |
| 113 | ||
| 114 | (*The Sucs found in the term are converted to a binary numeral. If relaxed is false, | |
| 115 | an exception is raised unless the original expression contains at least one | |
| 116 | numeral in a coefficient position. This prevents nat_combine_numerals from | |
| 117 | introducing numerals to goals.*) | |
| 118 | fun dest_Sucs_sum relaxed t = | |
| 119 | let val (k,ts) = dest_Suc_sum (t,(0,[])) | |
| 120 | in | |
| 121 | if relaxed orelse exists prod_has_numeral ts then | |
| 122 | if k=0 then ts | |
| 24630 
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
 wenzelm parents: 
24431diff
changeset | 123 | else mk_number k :: ts | 
| 23164 | 124 |      else raise TERM("Nat_Numeral_Simprocs.dest_Sucs_sum", [t])
 | 
| 125 | end; | |
| 126 | ||
| 127 | ||
| 128 | (*Simplify 1*n and n*1 to n*) | |
| 35064 
1bdef0c013d3
hide fact names clashing with fact names from Group.thy
 haftmann parents: 
35050diff
changeset | 129 | val add_0s  = map rename_numerals [@{thm Nat.add_0}, @{thm Nat.add_0_right}];
 | 
| 23164 | 130 | val mult_1s = map rename_numerals [@{thm nat_mult_1}, @{thm nat_mult_1_right}];
 | 
| 131 | ||
| 132 | (*Final simplification: cancel + and *; replace Numeral0 by 0 and Numeral1 by 1*) | |
| 133 | ||
| 134 | (*And these help the simproc return False when appropriate, which helps | |
| 135 | the arith prover.*) | |
| 23881 | 136 | val contra_rules = [@{thm add_Suc}, @{thm add_Suc_right}, @{thm Zero_not_Suc},
 | 
| 137 |   @{thm Suc_not_Zero}, @{thm le_0_eq}];
 | |
| 23164 | 138 | |
| 139 | val simplify_meta_eq = | |
| 30518 | 140 | Arith_Data.simplify_meta_eq | 
| 35064 
1bdef0c013d3
hide fact names clashing with fact names from Group.thy
 haftmann parents: 
35050diff
changeset | 141 |         ([@{thm nat_numeral_0_eq_0}, @{thm numeral_1_eq_Suc_0}, @{thm Nat.add_0}, @{thm Nat.add_0_right},
 | 
| 23471 | 142 |           @{thm mult_0}, @{thm mult_0_right}, @{thm mult_1}, @{thm mult_1_right}] @ contra_rules);
 | 
| 23164 | 143 | |
| 144 | ||
| 145 | (*** Applying CancelNumeralsFun ***) | |
| 146 | ||
| 147 | structure CancelNumeralsCommon = | |
| 44945 | 148 | struct | 
| 149 | val mk_sum = (fn T : typ => mk_sum) | |
| 150 | val dest_sum = dest_Sucs_sum true | |
| 151 | val mk_coeff = mk_coeff | |
| 152 | val dest_coeff = dest_coeff | |
| 153 | val find_first_coeff = find_first_coeff [] | |
| 44947 
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
 haftmann parents: 
44945diff
changeset | 154 | val trans_tac = Numeral_Simprocs.trans_tac | 
| 23164 | 155 | |
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
30685diff
changeset | 156 | val norm_ss1 = Numeral_Simprocs.num_ss addsimps numeral_syms @ add_0s @ mult_1s @ | 
| 31790 | 157 |     [@{thm Suc_eq_plus1_left}] @ @{thms add_ac}
 | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
30685diff
changeset | 158 |   val norm_ss2 = Numeral_Simprocs.num_ss addsimps bin_simps @ @{thms add_ac} @ @{thms mult_ac}
 | 
| 23164 | 159 | fun norm_tac ss = | 
| 160 | ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1)) | |
| 161 | THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2)) | |
| 162 | ||
| 163 | val numeral_simp_ss = HOL_ss addsimps add_0s @ bin_simps; | |
| 164 | fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss)); | |
| 165 | val simplify_meta_eq = simplify_meta_eq | |
| 44945 | 166 | val prove_conv = Arith_Data.prove_conv | 
| 167 | end; | |
| 23164 | 168 | |
| 169 | structure EqCancelNumerals = CancelNumeralsFun | |
| 170 | (open CancelNumeralsCommon | |
| 171 | val mk_bal = HOLogic.mk_eq | |
| 38864 
4abe644fcea5
formerly unnamed infix equality now named HOL.eq
 haftmann parents: 
37388diff
changeset | 172 |   val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} HOLogic.natT
 | 
| 23471 | 173 |   val bal_add1 = @{thm nat_eq_add_iff1} RS trans
 | 
| 174 |   val bal_add2 = @{thm nat_eq_add_iff2} RS trans
 | |
| 23164 | 175 | ); | 
| 176 | ||
| 177 | structure LessCancelNumerals = CancelNumeralsFun | |
| 178 | (open CancelNumeralsCommon | |
| 35092 
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
 haftmann parents: 
35064diff
changeset | 179 |   val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less}
 | 
| 
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
 haftmann parents: 
35064diff
changeset | 180 |   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} HOLogic.natT
 | 
| 23471 | 181 |   val bal_add1 = @{thm nat_less_add_iff1} RS trans
 | 
| 182 |   val bal_add2 = @{thm nat_less_add_iff2} RS trans
 | |
| 23164 | 183 | ); | 
| 184 | ||
| 185 | structure LeCancelNumerals = CancelNumeralsFun | |
| 186 | (open CancelNumeralsCommon | |
| 35092 
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
 haftmann parents: 
35064diff
changeset | 187 |   val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less_eq}
 | 
| 
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
 haftmann parents: 
35064diff
changeset | 188 |   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} HOLogic.natT
 | 
| 23471 | 189 |   val bal_add1 = @{thm nat_le_add_iff1} RS trans
 | 
| 190 |   val bal_add2 = @{thm nat_le_add_iff2} RS trans
 | |
| 23164 | 191 | ); | 
| 192 | ||
| 193 | structure DiffCancelNumerals = CancelNumeralsFun | |
| 194 | (open CancelNumeralsCommon | |
| 35267 
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
 haftmann parents: 
35092diff
changeset | 195 |   val mk_bal   = HOLogic.mk_binop @{const_name Groups.minus}
 | 
| 
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
 haftmann parents: 
35092diff
changeset | 196 |   val dest_bal = HOLogic.dest_bin @{const_name Groups.minus} HOLogic.natT
 | 
| 23471 | 197 |   val bal_add1 = @{thm nat_diff_add_eq1} RS trans
 | 
| 198 |   val bal_add2 = @{thm nat_diff_add_eq2} RS trans
 | |
| 23164 | 199 | ); | 
| 200 | ||
| 45436 
62bc9474d04b
use simproc_setup for some nat_numeral simprocs; add simproc tests
 huffman parents: 
45306diff
changeset | 201 | fun eq_cancel_numerals ss ct = EqCancelNumerals.proc ss (term_of ct) | 
| 
62bc9474d04b
use simproc_setup for some nat_numeral simprocs; add simproc tests
 huffman parents: 
45306diff
changeset | 202 | fun less_cancel_numerals ss ct = LessCancelNumerals.proc ss (term_of ct) | 
| 
62bc9474d04b
use simproc_setup for some nat_numeral simprocs; add simproc tests
 huffman parents: 
45306diff
changeset | 203 | fun le_cancel_numerals ss ct = LeCancelNumerals.proc ss (term_of ct) | 
| 
62bc9474d04b
use simproc_setup for some nat_numeral simprocs; add simproc tests
 huffman parents: 
45306diff
changeset | 204 | fun diff_cancel_numerals ss ct = DiffCancelNumerals.proc ss (term_of ct) | 
| 23164 | 205 | |
| 206 | ||
| 207 | (*** Applying CombineNumeralsFun ***) | |
| 208 | ||
| 209 | structure CombineNumeralsData = | |
| 44945 | 210 | struct | 
| 211 | type coeff = int | |
| 212 | val iszero = (fn x => x = 0) | |
| 213 | val add = op + | |
| 214 | val mk_sum = (fn T : typ => long_mk_sum) (*to work for 2*x + 3*x *) | |
| 215 | val dest_sum = dest_Sucs_sum false | |
| 216 | val mk_coeff = mk_coeff | |
| 217 | val dest_coeff = dest_coeff | |
| 218 |   val left_distrib = @{thm left_add_mult_distrib} RS trans
 | |
| 219 | val prove_conv = Arith_Data.prove_conv_nohyps | |
| 44947 
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
 haftmann parents: 
44945diff
changeset | 220 | val trans_tac = Numeral_Simprocs.trans_tac | 
| 23164 | 221 | |
| 31790 | 222 |   val norm_ss1 = Numeral_Simprocs.num_ss addsimps numeral_syms @ add_0s @ mult_1s @ [@{thm Suc_eq_plus1}] @ @{thms add_ac}
 | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
30685diff
changeset | 223 |   val norm_ss2 = Numeral_Simprocs.num_ss addsimps bin_simps @ @{thms add_ac} @ @{thms mult_ac}
 | 
| 23164 | 224 | fun norm_tac ss = | 
| 225 | ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1)) | |
| 226 | THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2)) | |
| 227 | ||
| 228 | val numeral_simp_ss = HOL_ss addsimps add_0s @ bin_simps; | |
| 229 | fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss)) | |
| 44945 | 230 | val simplify_meta_eq = simplify_meta_eq | 
| 231 | end; | |
| 23164 | 232 | |
| 233 | structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData); | |
| 234 | ||
| 235 | val combine_numerals = | |
| 44945 | 236 |   Numeral_Simprocs.prep_simproc @{theory}
 | 
| 32155 | 237 |     ("nat_combine_numerals", ["(i::nat) + j", "Suc (i + j)"], K CombineNumerals.proc);
 | 
| 23164 | 238 | |
| 239 | ||
| 240 | (*** Applying CancelNumeralFactorFun ***) | |
| 241 | ||
| 242 | structure CancelNumeralFactorCommon = | |
| 44945 | 243 | struct | 
| 244 | val mk_coeff = mk_coeff | |
| 245 | val dest_coeff = dest_coeff | |
| 44947 
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
 haftmann parents: 
44945diff
changeset | 246 | val trans_tac = Numeral_Simprocs.trans_tac | 
| 23164 | 247 | |
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
30685diff
changeset | 248 | val norm_ss1 = Numeral_Simprocs.num_ss addsimps | 
| 31790 | 249 |     numeral_syms @ add_0s @ mult_1s @ [@{thm Suc_eq_plus1_left}] @ @{thms add_ac}
 | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
30685diff
changeset | 250 |   val norm_ss2 = Numeral_Simprocs.num_ss addsimps bin_simps @ @{thms add_ac} @ @{thms mult_ac}
 | 
| 23164 | 251 | fun norm_tac ss = | 
| 252 | ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1)) | |
| 253 | THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2)) | |
| 254 | ||
| 255 | val numeral_simp_ss = HOL_ss addsimps bin_simps | |
| 256 | fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss)) | |
| 44945 | 257 | val simplify_meta_eq = simplify_meta_eq | 
| 258 | val prove_conv = Arith_Data.prove_conv | |
| 259 | end; | |
| 23164 | 260 | |
| 261 | structure DivCancelNumeralFactor = CancelNumeralFactorFun | |
| 262 | (open CancelNumeralFactorCommon | |
| 263 |   val mk_bal   = HOLogic.mk_binop @{const_name Divides.div}
 | |
| 264 |   val dest_bal = HOLogic.dest_bin @{const_name Divides.div} HOLogic.natT
 | |
| 23471 | 265 |   val cancel = @{thm nat_mult_div_cancel1} RS trans
 | 
| 23164 | 266 | val neg_exchanges = false | 
| 44945 | 267 | ); | 
| 23164 | 268 | |
| 23969 | 269 | structure DvdCancelNumeralFactor = CancelNumeralFactorFun | 
| 270 | (open CancelNumeralFactorCommon | |
| 35050 
9f841f20dca6
renamed OrderedGroup to Groups; split theory Ring_and_Field into Rings Fields
 haftmann parents: 
34974diff
changeset | 271 |   val mk_bal   = HOLogic.mk_binrel @{const_name Rings.dvd}
 | 
| 
9f841f20dca6
renamed OrderedGroup to Groups; split theory Ring_and_Field into Rings Fields
 haftmann parents: 
34974diff
changeset | 272 |   val dest_bal = HOLogic.dest_bin @{const_name Rings.dvd} HOLogic.natT
 | 
| 23969 | 273 |   val cancel = @{thm nat_mult_dvd_cancel1} RS trans
 | 
| 274 | val neg_exchanges = false | |
| 44945 | 275 | ); | 
| 23969 | 276 | |
| 23164 | 277 | structure EqCancelNumeralFactor = CancelNumeralFactorFun | 
| 278 | (open CancelNumeralFactorCommon | |
| 279 | val mk_bal = HOLogic.mk_eq | |
| 38864 
4abe644fcea5
formerly unnamed infix equality now named HOL.eq
 haftmann parents: 
37388diff
changeset | 280 |   val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} HOLogic.natT
 | 
| 23471 | 281 |   val cancel = @{thm nat_mult_eq_cancel1} RS trans
 | 
| 23164 | 282 | val neg_exchanges = false | 
| 44945 | 283 | ); | 
| 23164 | 284 | |
| 285 | structure LessCancelNumeralFactor = CancelNumeralFactorFun | |
| 286 | (open CancelNumeralFactorCommon | |
| 35092 
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
 haftmann parents: 
35064diff
changeset | 287 |   val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less}
 | 
| 
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
 haftmann parents: 
35064diff
changeset | 288 |   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} HOLogic.natT
 | 
| 23471 | 289 |   val cancel = @{thm nat_mult_less_cancel1} RS trans
 | 
| 23164 | 290 | val neg_exchanges = true | 
| 44945 | 291 | ); | 
| 23164 | 292 | |
| 293 | structure LeCancelNumeralFactor = CancelNumeralFactorFun | |
| 294 | (open CancelNumeralFactorCommon | |
| 35092 
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
 haftmann parents: 
35064diff
changeset | 295 |   val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less_eq}
 | 
| 
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
 haftmann parents: 
35064diff
changeset | 296 |   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} HOLogic.natT
 | 
| 23471 | 297 |   val cancel = @{thm nat_mult_le_cancel1} RS trans
 | 
| 23164 | 298 | val neg_exchanges = true | 
| 299 | ) | |
| 300 | ||
| 301 | val cancel_numeral_factors = | |
| 44945 | 302 |   map (Numeral_Simprocs.prep_simproc @{theory})
 | 
| 23164 | 303 |    [("nateq_cancel_numeral_factors",
 | 
| 304 | ["(l::nat) * m = n", "(l::nat) = m * n"], | |
| 305 | K EqCancelNumeralFactor.proc), | |
| 306 |     ("natless_cancel_numeral_factors",
 | |
| 307 | ["(l::nat) * m < n", "(l::nat) < m * n"], | |
| 308 | K LessCancelNumeralFactor.proc), | |
| 309 |     ("natle_cancel_numeral_factors",
 | |
| 310 | ["(l::nat) * m <= n", "(l::nat) <= m * n"], | |
| 311 | K LeCancelNumeralFactor.proc), | |
| 312 |     ("natdiv_cancel_numeral_factors",
 | |
| 313 | ["((l::nat) * m) div n", "(l::nat) div (m * n)"], | |
| 23969 | 314 | K DivCancelNumeralFactor.proc), | 
| 315 |     ("natdvd_cancel_numeral_factors",
 | |
| 316 | ["((l::nat) * m) dvd n", "(l::nat) dvd (m * n)"], | |
| 317 | K DvdCancelNumeralFactor.proc)]; | |
| 23164 | 318 | |
| 319 | ||
| 320 | ||
| 321 | (*** Applying ExtractCommonTermFun ***) | |
| 322 | ||
| 323 | (*this version ALWAYS includes a trailing one*) | |
| 324 | fun long_mk_prod [] = one | |
| 325 | | long_mk_prod (t :: ts) = mk_times (t, mk_prod ts); | |
| 326 | ||
| 327 | (*Find first term that matches u*) | |
| 328 | fun find_first_t past u []         = raise TERM("find_first_t", [])
 | |
| 329 | | find_first_t past u (t::terms) = | |
| 330 | if u aconv t then (rev past @ terms) | |
| 331 | else find_first_t (t::past) u terms | |
| 332 | handle TERM _ => find_first_t (t::past) u terms; | |
| 333 | ||
| 334 | (** Final simplification for the CancelFactor simprocs **) | |
| 30518 | 335 | val simplify_one = Arith_Data.simplify_meta_eq | 
| 23164 | 336 |   [@{thm mult_1_left}, @{thm mult_1_right}, @{thm div_1}, @{thm numeral_1_eq_Suc_0}];
 | 
| 337 | ||
| 30649 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 338 | fun cancel_simplify_meta_eq ss cancel_th th = | 
| 23164 | 339 | simplify_one ss (([th, cancel_th]) MRS trans); | 
| 340 | ||
| 341 | structure CancelFactorCommon = | |
| 44945 | 342 | struct | 
| 343 | val mk_sum = (fn T : typ => long_mk_prod) | |
| 344 | val dest_sum = dest_prod | |
| 345 | val mk_coeff = mk_coeff | |
| 346 | val dest_coeff = dest_coeff | |
| 347 | val find_first = find_first_t [] | |
| 44947 
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
 haftmann parents: 
44945diff
changeset | 348 | val trans_tac = Numeral_Simprocs.trans_tac | 
| 23881 | 349 |   val norm_ss = HOL_ss addsimps mult_1s @ @{thms mult_ac}
 | 
| 23164 | 350 | fun norm_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss)) | 
| 30649 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 351 | val simplify_meta_eq = cancel_simplify_meta_eq | 
| 45270 
d5b5c9259afd
fix bug in cancel_factor simprocs so they will work on goals like 'x * y < x * z' where the common term is already on the left
 huffman parents: 
44947diff
changeset | 352 | fun mk_eq (a, b) = HOLogic.mk_Trueprop (HOLogic.mk_eq (a, b)) | 
| 44945 | 353 | end; | 
| 23164 | 354 | |
| 355 | structure EqCancelFactor = ExtractCommonTermFun | |
| 356 | (open CancelFactorCommon | |
| 357 | val mk_bal = HOLogic.mk_eq | |
| 38864 
4abe644fcea5
formerly unnamed infix equality now named HOL.eq
 haftmann parents: 
37388diff
changeset | 358 |   val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} HOLogic.natT
 | 
| 31368 | 359 |   fun simp_conv _ _ = SOME @{thm nat_mult_eq_cancel_disj}
 | 
| 23164 | 360 | ); | 
| 361 | ||
| 44945 | 362 | structure LeCancelFactor = ExtractCommonTermFun | 
| 363 | (open CancelFactorCommon | |
| 364 |   val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less_eq}
 | |
| 365 |   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} HOLogic.natT
 | |
| 366 |   fun simp_conv _ _ = SOME @{thm nat_mult_le_cancel_disj}
 | |
| 367 | ); | |
| 368 | ||
| 23164 | 369 | structure LessCancelFactor = ExtractCommonTermFun | 
| 370 | (open CancelFactorCommon | |
| 35092 
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
 haftmann parents: 
35064diff
changeset | 371 |   val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less}
 | 
| 
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
 haftmann parents: 
35064diff
changeset | 372 |   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} HOLogic.natT
 | 
| 31368 | 373 |   fun simp_conv _ _ = SOME @{thm nat_mult_less_cancel_disj}
 | 
| 23164 | 374 | ); | 
| 375 | ||
| 376 | structure DivideCancelFactor = ExtractCommonTermFun | |
| 377 | (open CancelFactorCommon | |
| 378 |   val mk_bal   = HOLogic.mk_binop @{const_name Divides.div}
 | |
| 379 |   val dest_bal = HOLogic.dest_bin @{const_name Divides.div} HOLogic.natT
 | |
| 31368 | 380 |   fun simp_conv _ _ = SOME @{thm nat_mult_div_cancel_disj}
 | 
| 23164 | 381 | ); | 
| 382 | ||
| 23969 | 383 | structure DvdCancelFactor = ExtractCommonTermFun | 
| 384 | (open CancelFactorCommon | |
| 35050 
9f841f20dca6
renamed OrderedGroup to Groups; split theory Ring_and_Field into Rings Fields
 haftmann parents: 
34974diff
changeset | 385 |   val mk_bal   = HOLogic.mk_binrel @{const_name Rings.dvd}
 | 
| 
9f841f20dca6
renamed OrderedGroup to Groups; split theory Ring_and_Field into Rings Fields
 haftmann parents: 
34974diff
changeset | 386 |   val dest_bal = HOLogic.dest_bin @{const_name Rings.dvd} HOLogic.natT
 | 
| 31368 | 387 |   fun simp_conv _ _ = SOME @{thm nat_mult_dvd_cancel_disj}
 | 
| 23969 | 388 | ); | 
| 389 | ||
| 23164 | 390 | val cancel_factor = | 
| 44945 | 391 |   map (Numeral_Simprocs.prep_simproc @{theory})
 | 
| 23164 | 392 |    [("nat_eq_cancel_factor",
 | 
| 393 | ["(l::nat) * m = n", "(l::nat) = m * n"], | |
| 394 | K EqCancelFactor.proc), | |
| 395 |     ("nat_less_cancel_factor",
 | |
| 396 | ["(l::nat) * m < n", "(l::nat) < m * n"], | |
| 397 | K LessCancelFactor.proc), | |
| 398 |     ("nat_le_cancel_factor",
 | |
| 399 | ["(l::nat) * m <= n", "(l::nat) <= m * n"], | |
| 400 | K LeCancelFactor.proc), | |
| 401 |     ("nat_divide_cancel_factor",
 | |
| 402 | ["((l::nat) * m) div n", "(l::nat) div (m * n)"], | |
| 23969 | 403 | K DivideCancelFactor.proc), | 
| 404 |     ("nat_dvd_cancel_factor",
 | |
| 405 | ["((l::nat) * m) dvd n", "(l::nat) dvd (m * n)"], | |
| 406 | K DvdCancelFactor.proc)]; | |
| 23164 | 407 | |
| 408 | end; | |
| 409 | ||
| 410 | ||
| 411 | Addsimprocs [Nat_Numeral_Simprocs.combine_numerals]; | |
| 412 | Addsimprocs Nat_Numeral_Simprocs.cancel_numeral_factors; | |
| 413 | Addsimprocs Nat_Numeral_Simprocs.cancel_factor; | |
| 414 | ||
| 415 | ||
| 416 | (*examples: | |
| 417 | print_depth 22; | |
| 418 | set timing; | |
| 40878 
7695e4de4d86
renamed trace_simp to simp_trace, and debug_simp to simp_debug;
 wenzelm parents: 
38864diff
changeset | 419 | set simp_trace; | 
| 23164 | 420 | fun test s = (Goal s; by (Simp_tac 1)); | 
| 421 | ||
| 422 | (*combine_numerals*) | |
| 423 | test "k + 3*k = (u::nat)"; | |
| 424 | test "Suc (i + 3) = u"; | |
| 425 | test "Suc (i + j + 3 + k) = u"; | |
| 426 | test "k + j + 3*k + j = (u::nat)"; | |
| 427 | test "Suc (j*i + i + k + 5 + 3*k + i*j*4) = (u::nat)"; | |
| 428 | test "(2*n*m) + (3*(m*n)) = (u::nat)"; | |
| 429 | (*negative numerals: FAIL*) | |
| 430 | test "Suc (i + j + -3 + k) = u"; | |
| 431 | ||
| 432 | (*cancel_numeral_factors*) | |
| 433 | test "9*x = 12 * (y::nat)"; | |
| 434 | test "(9*x) div (12 * (y::nat)) = z"; | |
| 435 | test "9*x < 12 * (y::nat)"; | |
| 436 | test "9*x <= 12 * (y::nat)"; | |
| 437 | ||
| 438 | (*cancel_factor*) | |
| 439 | test "x*k = k*(y::nat)"; | |
| 440 | test "k = k*(y::nat)"; | |
| 441 | test "a*(b*c) = (b::nat)"; | |
| 442 | test "a*(b*c) = d*(b::nat)*(x*a)"; | |
| 443 | ||
| 444 | test "x*k < k*(y::nat)"; | |
| 445 | test "k < k*(y::nat)"; | |
| 446 | test "a*(b*c) < (b::nat)"; | |
| 447 | test "a*(b*c) < d*(b::nat)*(x*a)"; | |
| 448 | ||
| 449 | test "x*k <= k*(y::nat)"; | |
| 450 | test "k <= k*(y::nat)"; | |
| 451 | test "a*(b*c) <= (b::nat)"; | |
| 452 | test "a*(b*c) <= d*(b::nat)*(x*a)"; | |
| 453 | ||
| 454 | test "(x*k) div (k*(y::nat)) = (uu::nat)"; | |
| 455 | test "(k) div (k*(y::nat)) = (uu::nat)"; | |
| 456 | test "(a*(b*c)) div ((b::nat)) = (uu::nat)"; | |
| 457 | test "(a*(b*c)) div (d*(b::nat)*(x*a)) = (uu::nat)"; | |
| 458 | *) |