| author | blanchet | 
| Mon, 23 Jul 2012 15:32:30 +0200 | |
| changeset 48442 | 3c9890c19e90 | 
| parent 47108 | 2a1953f0d20d | 
| child 51717 | 9e7d1c139569 | 
| 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 | 
| 45462 
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
 huffman parents: 
45436diff
changeset | 8 | val combine_numerals: simpset -> cterm -> thm option | 
| 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 | 
| 45463 
9a588a835c1e
use simproc_setup for the remaining nat_numeral simprocs
 huffman parents: 
45462diff
changeset | 13 | val eq_cancel_numeral_factor: simpset -> cterm -> thm option | 
| 
9a588a835c1e
use simproc_setup for the remaining nat_numeral simprocs
 huffman parents: 
45462diff
changeset | 14 | val less_cancel_numeral_factor: simpset -> cterm -> thm option | 
| 
9a588a835c1e
use simproc_setup for the remaining nat_numeral simprocs
 huffman parents: 
45462diff
changeset | 15 | val le_cancel_numeral_factor: simpset -> cterm -> thm option | 
| 
9a588a835c1e
use simproc_setup for the remaining nat_numeral simprocs
 huffman parents: 
45462diff
changeset | 16 | val div_cancel_numeral_factor: simpset -> cterm -> thm option | 
| 
9a588a835c1e
use simproc_setup for the remaining nat_numeral simprocs
 huffman parents: 
45462diff
changeset | 17 | val dvd_cancel_numeral_factor: simpset -> cterm -> thm option | 
| 45462 
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
 huffman parents: 
45436diff
changeset | 18 | val eq_cancel_factor: simpset -> cterm -> thm option | 
| 
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
 huffman parents: 
45436diff
changeset | 19 | val less_cancel_factor: simpset -> cterm -> thm option | 
| 
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
 huffman parents: 
45436diff
changeset | 20 | val le_cancel_factor: simpset -> cterm -> thm option | 
| 45463 
9a588a835c1e
use simproc_setup for the remaining nat_numeral simprocs
 huffman parents: 
45462diff
changeset | 21 | val div_cancel_factor: simpset -> cterm -> thm option | 
| 45462 
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
 huffman parents: 
45436diff
changeset | 22 | val dvd_cancel_factor: simpset -> cterm -> thm option | 
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
30685diff
changeset | 23 | end; | 
| 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
30685diff
changeset | 24 | |
| 45462 
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
 huffman parents: 
45436diff
changeset | 25 | structure Nat_Numeral_Simprocs : NAT_NUMERAL_SIMPROCS = | 
| 23164 | 26 | struct | 
| 27 | ||
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
45668diff
changeset | 28 | (*Maps n to #n for n = 1, 2*) | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
45668diff
changeset | 29 | val numeral_syms = [@{thm numeral_1_eq_1} RS sym, @{thm numeral_2_eq_2} RS sym];
 | 
| 45668 
0ea1c705eebb
use HOL_basic_ss instead of HOL_ss for internal simproc proofs (cf. b36eedcd1633)
 huffman parents: 
45463diff
changeset | 30 | val numeral_sym_ss = HOL_basic_ss addsimps numeral_syms; | 
| 23164 | 31 | |
| 44945 | 32 | val rename_numerals = simplify numeral_sym_ss o Thm.transfer @{theory};
 | 
| 23164 | 33 | |
| 34 | (*Utilities*) | |
| 35 | ||
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
45668diff
changeset | 36 | fun mk_number 1 = HOLogic.numeral_const HOLogic.natT $ HOLogic.one_const | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
45668diff
changeset | 37 | | mk_number n = HOLogic.mk_number HOLogic.natT n; | 
| 24630 
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
 wenzelm parents: 
24431diff
changeset | 38 | fun dest_number t = Int.max (0, snd (HOLogic.dest_number t)); | 
| 23164 | 39 | |
| 40 | fun find_first_numeral past (t::terms) = | |
| 41 | ((dest_number t, t, rev past @ terms) | |
| 42 | handle TERM _ => find_first_numeral (t::past) terms) | |
| 43 |   | find_first_numeral past [] = raise TERM("find_first_numeral", []);
 | |
| 44 | ||
| 45 | val zero = mk_number 0; | |
| 35267 
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
 haftmann parents: 
35092diff
changeset | 46 | val mk_plus = HOLogic.mk_binop @{const_name Groups.plus};
 | 
| 23164 | 47 | |
| 48 | (*Thus mk_sum[t] yields t+0; longer sums don't have a trailing zero*) | |
| 49 | fun mk_sum [] = zero | |
| 50 | | mk_sum [t,u] = mk_plus (t, u) | |
| 51 | | mk_sum (t :: ts) = mk_plus (t, mk_sum ts); | |
| 52 | ||
| 53 | (*this version ALWAYS includes a trailing zero*) | |
| 54 | fun long_mk_sum [] = HOLogic.zero | |
| 55 | | long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts); | |
| 56 | ||
| 35267 
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
 haftmann parents: 
35092diff
changeset | 57 | val dest_plus = HOLogic.dest_bin @{const_name Groups.plus} HOLogic.natT;
 | 
| 23164 | 58 | |
| 59 | ||
| 60 | (** Other simproc items **) | |
| 61 | ||
| 62 | val bin_simps = | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
45668diff
changeset | 63 |      [@{thm numeral_1_eq_1} RS sym,
 | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
45668diff
changeset | 64 |       @{thm numeral_plus_numeral}, @{thm add_numeral_left},
 | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
45668diff
changeset | 65 |       @{thm diff_nat_numeral}, @{thm diff_0_eq_0}, @{thm diff_0},
 | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
45668diff
changeset | 66 |       @{thm numeral_times_numeral}, @{thm mult_numeral_left(1)},
 | 
| 45668 
0ea1c705eebb
use HOL_basic_ss instead of HOL_ss for internal simproc proofs (cf. b36eedcd1633)
 huffman parents: 
45463diff
changeset | 67 |       @{thm if_True}, @{thm if_False}, @{thm not_False_eq_True},
 | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
45668diff
changeset | 68 |       @{thm nat_0}, @{thm nat_numeral}, @{thm nat_neg_numeral}] @
 | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
45668diff
changeset | 69 |      @{thms arith_simps} @ @{thms rel_simps};
 | 
| 23164 | 70 | |
| 71 | ||
| 72 | (*** CancelNumerals simprocs ***) | |
| 73 | ||
| 74 | val one = mk_number 1; | |
| 35267 
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
 haftmann parents: 
35092diff
changeset | 75 | val mk_times = HOLogic.mk_binop @{const_name Groups.times};
 | 
| 23164 | 76 | |
| 77 | fun mk_prod [] = one | |
| 78 | | mk_prod [t] = t | |
| 79 | | mk_prod (t :: ts) = if t = one then mk_prod ts | |
| 80 | else mk_times (t, mk_prod ts); | |
| 81 | ||
| 35267 
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
 haftmann parents: 
35092diff
changeset | 82 | val dest_times = HOLogic.dest_bin @{const_name Groups.times} HOLogic.natT;
 | 
| 23164 | 83 | |
| 84 | fun dest_prod t = | |
| 85 | let val (t,u) = dest_times t | |
| 86 | in dest_prod t @ dest_prod u end | |
| 87 | handle TERM _ => [t]; | |
| 88 | ||
| 89 | (*DON'T do the obvious simplifications; that would create special cases*) | |
| 90 | fun mk_coeff (k,t) = mk_times (mk_number k, t); | |
| 91 | ||
| 92 | (*Express t as a product of (possibly) a numeral with other factors, sorted*) | |
| 93 | fun dest_coeff t = | |
| 35408 | 94 | let val ts = sort Term_Ord.term_ord (dest_prod t) | 
| 23164 | 95 | val (n, _, ts') = find_first_numeral [] ts | 
| 96 | handle TERM _ => (1, one, ts) | |
| 97 | in (n, mk_prod ts') end; | |
| 98 | ||
| 99 | (*Find first coefficient-term THAT MATCHES u*) | |
| 100 | fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
 | |
| 101 | | find_first_coeff past u (t::terms) = | |
| 102 | let val (n,u') = dest_coeff t | |
| 103 | in if u aconv u' then (n, rev past @ terms) | |
| 104 | else find_first_coeff (t::past) u terms | |
| 105 | end | |
| 106 | handle TERM _ => find_first_coeff (t::past) u terms; | |
| 107 | ||
| 108 | ||
| 109 | (*Split up a sum into the list of its constituent terms, on the way removing any | |
| 110 | Sucs and counting them.*) | |
| 37388 | 111 | fun dest_Suc_sum (Const (@{const_name Suc}, _) $ t, (k,ts)) = dest_Suc_sum (t, (k+1,ts))
 | 
| 23164 | 112 | | dest_Suc_sum (t, (k,ts)) = | 
| 113 | let val (t1,t2) = dest_plus t | |
| 114 | in dest_Suc_sum (t1, dest_Suc_sum (t2, (k,ts))) end | |
| 115 | handle TERM _ => (k, t::ts); | |
| 116 | ||
| 117 | (*Code for testing whether numerals are already used in the goal*) | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
45668diff
changeset | 118 | fun is_numeral (Const(@{const_name Num.numeral}, _) $ w) = true
 | 
| 23164 | 119 | | is_numeral _ = false; | 
| 120 | ||
| 121 | fun prod_has_numeral t = exists is_numeral (dest_prod t); | |
| 122 | ||
| 123 | (*The Sucs found in the term are converted to a binary numeral. If relaxed is false, | |
| 124 | an exception is raised unless the original expression contains at least one | |
| 125 | numeral in a coefficient position. This prevents nat_combine_numerals from | |
| 126 | introducing numerals to goals.*) | |
| 127 | fun dest_Sucs_sum relaxed t = | |
| 128 | let val (k,ts) = dest_Suc_sum (t,(0,[])) | |
| 129 | in | |
| 130 | if relaxed orelse exists prod_has_numeral ts then | |
| 131 | if k=0 then ts | |
| 24630 
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
 wenzelm parents: 
24431diff
changeset | 132 | else mk_number k :: ts | 
| 23164 | 133 |      else raise TERM("Nat_Numeral_Simprocs.dest_Sucs_sum", [t])
 | 
| 134 | end; | |
| 135 | ||
| 136 | ||
| 137 | (*Simplify 1*n and n*1 to n*) | |
| 35064 
1bdef0c013d3
hide fact names clashing with fact names from Group.thy
 haftmann parents: 
35050diff
changeset | 138 | val add_0s  = map rename_numerals [@{thm Nat.add_0}, @{thm Nat.add_0_right}];
 | 
| 23164 | 139 | val mult_1s = map rename_numerals [@{thm nat_mult_1}, @{thm nat_mult_1_right}];
 | 
| 140 | ||
| 141 | (*Final simplification: cancel + and *; replace Numeral0 by 0 and Numeral1 by 1*) | |
| 142 | ||
| 143 | (*And these help the simproc return False when appropriate, which helps | |
| 144 | the arith prover.*) | |
| 23881 | 145 | val contra_rules = [@{thm add_Suc}, @{thm add_Suc_right}, @{thm Zero_not_Suc},
 | 
| 146 |   @{thm Suc_not_Zero}, @{thm le_0_eq}];
 | |
| 23164 | 147 | |
| 148 | val simplify_meta_eq = | |
| 30518 | 149 | Arith_Data.simplify_meta_eq | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
45668diff
changeset | 150 |         ([@{thm numeral_1_eq_Suc_0}, @{thm Nat.add_0}, @{thm Nat.add_0_right},
 | 
| 23471 | 151 |           @{thm mult_0}, @{thm mult_0_right}, @{thm mult_1}, @{thm mult_1_right}] @ contra_rules);
 | 
| 23164 | 152 | |
| 153 | ||
| 154 | (*** Applying CancelNumeralsFun ***) | |
| 155 | ||
| 156 | structure CancelNumeralsCommon = | |
| 44945 | 157 | struct | 
| 158 | val mk_sum = (fn T : typ => mk_sum) | |
| 159 | val dest_sum = dest_Sucs_sum true | |
| 160 | val mk_coeff = mk_coeff | |
| 161 | val dest_coeff = dest_coeff | |
| 162 | val find_first_coeff = find_first_coeff [] | |
| 44947 
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
 haftmann parents: 
44945diff
changeset | 163 | val trans_tac = Numeral_Simprocs.trans_tac | 
| 23164 | 164 | |
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
30685diff
changeset | 165 | val norm_ss1 = Numeral_Simprocs.num_ss addsimps numeral_syms @ add_0s @ mult_1s @ | 
| 31790 | 166 |     [@{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 | 167 |   val norm_ss2 = Numeral_Simprocs.num_ss addsimps bin_simps @ @{thms add_ac} @ @{thms mult_ac}
 | 
| 23164 | 168 | fun norm_tac ss = | 
| 169 | ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1)) | |
| 170 | THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2)) | |
| 171 | ||
| 45668 
0ea1c705eebb
use HOL_basic_ss instead of HOL_ss for internal simproc proofs (cf. b36eedcd1633)
 huffman parents: 
45463diff
changeset | 172 | val numeral_simp_ss = HOL_basic_ss addsimps add_0s @ bin_simps; | 
| 23164 | 173 | fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss)); | 
| 174 | val simplify_meta_eq = simplify_meta_eq | |
| 44945 | 175 | val prove_conv = Arith_Data.prove_conv | 
| 176 | end; | |
| 23164 | 177 | |
| 178 | structure EqCancelNumerals = CancelNumeralsFun | |
| 179 | (open CancelNumeralsCommon | |
| 180 | val mk_bal = HOLogic.mk_eq | |
| 38864 
4abe644fcea5
formerly unnamed infix equality now named HOL.eq
 haftmann parents: 
37388diff
changeset | 181 |   val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} HOLogic.natT
 | 
| 23471 | 182 |   val bal_add1 = @{thm nat_eq_add_iff1} RS trans
 | 
| 183 |   val bal_add2 = @{thm nat_eq_add_iff2} RS trans
 | |
| 23164 | 184 | ); | 
| 185 | ||
| 186 | structure LessCancelNumerals = CancelNumeralsFun | |
| 187 | (open CancelNumeralsCommon | |
| 35092 
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
 haftmann parents: 
35064diff
changeset | 188 |   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 | 189 |   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} HOLogic.natT
 | 
| 23471 | 190 |   val bal_add1 = @{thm nat_less_add_iff1} RS trans
 | 
| 191 |   val bal_add2 = @{thm nat_less_add_iff2} RS trans
 | |
| 23164 | 192 | ); | 
| 193 | ||
| 194 | structure LeCancelNumerals = CancelNumeralsFun | |
| 195 | (open CancelNumeralsCommon | |
| 35092 
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
 haftmann parents: 
35064diff
changeset | 196 |   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 | 197 |   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} HOLogic.natT
 | 
| 23471 | 198 |   val bal_add1 = @{thm nat_le_add_iff1} RS trans
 | 
| 199 |   val bal_add2 = @{thm nat_le_add_iff2} RS trans
 | |
| 23164 | 200 | ); | 
| 201 | ||
| 202 | structure DiffCancelNumerals = CancelNumeralsFun | |
| 203 | (open CancelNumeralsCommon | |
| 35267 
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
 haftmann parents: 
35092diff
changeset | 204 |   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 | 205 |   val dest_bal = HOLogic.dest_bin @{const_name Groups.minus} HOLogic.natT
 | 
| 23471 | 206 |   val bal_add1 = @{thm nat_diff_add_eq1} RS trans
 | 
| 207 |   val bal_add2 = @{thm nat_diff_add_eq2} RS trans
 | |
| 23164 | 208 | ); | 
| 209 | ||
| 45436 
62bc9474d04b
use simproc_setup for some nat_numeral simprocs; add simproc tests
 huffman parents: 
45306diff
changeset | 210 | 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 | 211 | 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 | 212 | 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 | 213 | fun diff_cancel_numerals ss ct = DiffCancelNumerals.proc ss (term_of ct) | 
| 23164 | 214 | |
| 215 | ||
| 216 | (*** Applying CombineNumeralsFun ***) | |
| 217 | ||
| 218 | structure CombineNumeralsData = | |
| 44945 | 219 | struct | 
| 220 | type coeff = int | |
| 221 | val iszero = (fn x => x = 0) | |
| 222 | val add = op + | |
| 223 | val mk_sum = (fn T : typ => long_mk_sum) (*to work for 2*x + 3*x *) | |
| 224 | val dest_sum = dest_Sucs_sum false | |
| 225 | val mk_coeff = mk_coeff | |
| 226 | val dest_coeff = dest_coeff | |
| 227 |   val left_distrib = @{thm left_add_mult_distrib} RS trans
 | |
| 228 | val prove_conv = Arith_Data.prove_conv_nohyps | |
| 44947 
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
 haftmann parents: 
44945diff
changeset | 229 | val trans_tac = Numeral_Simprocs.trans_tac | 
| 23164 | 230 | |
| 31790 | 231 |   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 | 232 |   val norm_ss2 = Numeral_Simprocs.num_ss addsimps bin_simps @ @{thms add_ac} @ @{thms mult_ac}
 | 
| 23164 | 233 | fun norm_tac ss = | 
| 234 | ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1)) | |
| 235 | THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2)) | |
| 236 | ||
| 45668 
0ea1c705eebb
use HOL_basic_ss instead of HOL_ss for internal simproc proofs (cf. b36eedcd1633)
 huffman parents: 
45463diff
changeset | 237 | val numeral_simp_ss = HOL_basic_ss addsimps add_0s @ bin_simps; | 
| 23164 | 238 | fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss)) | 
| 44945 | 239 | val simplify_meta_eq = simplify_meta_eq | 
| 240 | end; | |
| 23164 | 241 | |
| 242 | structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData); | |
| 243 | ||
| 45462 
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
 huffman parents: 
45436diff
changeset | 244 | fun combine_numerals ss ct = CombineNumerals.proc ss (term_of ct) | 
| 23164 | 245 | |
| 246 | ||
| 247 | (*** Applying CancelNumeralFactorFun ***) | |
| 248 | ||
| 249 | structure CancelNumeralFactorCommon = | |
| 44945 | 250 | struct | 
| 251 | val mk_coeff = mk_coeff | |
| 252 | val dest_coeff = dest_coeff | |
| 44947 
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
 haftmann parents: 
44945diff
changeset | 253 | val trans_tac = Numeral_Simprocs.trans_tac | 
| 23164 | 254 | |
| 31068 
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
 haftmann parents: 
30685diff
changeset | 255 | val norm_ss1 = Numeral_Simprocs.num_ss addsimps | 
| 31790 | 256 |     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 | 257 |   val norm_ss2 = Numeral_Simprocs.num_ss addsimps bin_simps @ @{thms add_ac} @ @{thms mult_ac}
 | 
| 23164 | 258 | fun norm_tac ss = | 
| 259 | ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1)) | |
| 260 | THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2)) | |
| 261 | ||
| 45668 
0ea1c705eebb
use HOL_basic_ss instead of HOL_ss for internal simproc proofs (cf. b36eedcd1633)
 huffman parents: 
45463diff
changeset | 262 | val numeral_simp_ss = HOL_basic_ss addsimps bin_simps | 
| 23164 | 263 | fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss)) | 
| 44945 | 264 | val simplify_meta_eq = simplify_meta_eq | 
| 265 | val prove_conv = Arith_Data.prove_conv | |
| 266 | end; | |
| 23164 | 267 | |
| 268 | structure DivCancelNumeralFactor = CancelNumeralFactorFun | |
| 269 | (open CancelNumeralFactorCommon | |
| 270 |   val mk_bal   = HOLogic.mk_binop @{const_name Divides.div}
 | |
| 271 |   val dest_bal = HOLogic.dest_bin @{const_name Divides.div} HOLogic.natT
 | |
| 23471 | 272 |   val cancel = @{thm nat_mult_div_cancel1} RS trans
 | 
| 23164 | 273 | val neg_exchanges = false | 
| 44945 | 274 | ); | 
| 23164 | 275 | |
| 23969 | 276 | structure DvdCancelNumeralFactor = CancelNumeralFactorFun | 
| 277 | (open CancelNumeralFactorCommon | |
| 35050 
9f841f20dca6
renamed OrderedGroup to Groups; split theory Ring_and_Field into Rings Fields
 haftmann parents: 
34974diff
changeset | 278 |   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 | 279 |   val dest_bal = HOLogic.dest_bin @{const_name Rings.dvd} HOLogic.natT
 | 
| 23969 | 280 |   val cancel = @{thm nat_mult_dvd_cancel1} RS trans
 | 
| 281 | val neg_exchanges = false | |
| 44945 | 282 | ); | 
| 23969 | 283 | |
| 23164 | 284 | structure EqCancelNumeralFactor = CancelNumeralFactorFun | 
| 285 | (open CancelNumeralFactorCommon | |
| 286 | val mk_bal = HOLogic.mk_eq | |
| 38864 
4abe644fcea5
formerly unnamed infix equality now named HOL.eq
 haftmann parents: 
37388diff
changeset | 287 |   val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} HOLogic.natT
 | 
| 23471 | 288 |   val cancel = @{thm nat_mult_eq_cancel1} RS trans
 | 
| 23164 | 289 | val neg_exchanges = false | 
| 44945 | 290 | ); | 
| 23164 | 291 | |
| 292 | structure LessCancelNumeralFactor = CancelNumeralFactorFun | |
| 293 | (open CancelNumeralFactorCommon | |
| 35092 
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
 haftmann parents: 
35064diff
changeset | 294 |   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 | 295 |   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} HOLogic.natT
 | 
| 23471 | 296 |   val cancel = @{thm nat_mult_less_cancel1} RS trans
 | 
| 23164 | 297 | val neg_exchanges = true | 
| 44945 | 298 | ); | 
| 23164 | 299 | |
| 300 | structure LeCancelNumeralFactor = CancelNumeralFactorFun | |
| 301 | (open CancelNumeralFactorCommon | |
| 35092 
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
 haftmann parents: 
35064diff
changeset | 302 |   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 | 303 |   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} HOLogic.natT
 | 
| 23471 | 304 |   val cancel = @{thm nat_mult_le_cancel1} RS trans
 | 
| 23164 | 305 | val neg_exchanges = true | 
| 306 | ) | |
| 307 | ||
| 45463 
9a588a835c1e
use simproc_setup for the remaining nat_numeral simprocs
 huffman parents: 
45462diff
changeset | 308 | fun eq_cancel_numeral_factor ss ct = EqCancelNumeralFactor.proc ss (term_of ct) | 
| 
9a588a835c1e
use simproc_setup for the remaining nat_numeral simprocs
 huffman parents: 
45462diff
changeset | 309 | fun less_cancel_numeral_factor ss ct = LessCancelNumeralFactor.proc ss (term_of ct) | 
| 
9a588a835c1e
use simproc_setup for the remaining nat_numeral simprocs
 huffman parents: 
45462diff
changeset | 310 | fun le_cancel_numeral_factor ss ct = LeCancelNumeralFactor.proc ss (term_of ct) | 
| 
9a588a835c1e
use simproc_setup for the remaining nat_numeral simprocs
 huffman parents: 
45462diff
changeset | 311 | fun div_cancel_numeral_factor ss ct = DivCancelNumeralFactor.proc ss (term_of ct) | 
| 
9a588a835c1e
use simproc_setup for the remaining nat_numeral simprocs
 huffman parents: 
45462diff
changeset | 312 | fun dvd_cancel_numeral_factor ss ct = DvdCancelNumeralFactor.proc ss (term_of ct) | 
| 23164 | 313 | |
| 314 | ||
| 315 | (*** Applying ExtractCommonTermFun ***) | |
| 316 | ||
| 317 | (*this version ALWAYS includes a trailing one*) | |
| 318 | fun long_mk_prod [] = one | |
| 319 | | long_mk_prod (t :: ts) = mk_times (t, mk_prod ts); | |
| 320 | ||
| 321 | (*Find first term that matches u*) | |
| 322 | fun find_first_t past u []         = raise TERM("find_first_t", [])
 | |
| 323 | | find_first_t past u (t::terms) = | |
| 324 | if u aconv t then (rev past @ terms) | |
| 325 | else find_first_t (t::past) u terms | |
| 326 | handle TERM _ => find_first_t (t::past) u terms; | |
| 327 | ||
| 328 | (** Final simplification for the CancelFactor simprocs **) | |
| 30518 | 329 | val simplify_one = Arith_Data.simplify_meta_eq | 
| 23164 | 330 |   [@{thm mult_1_left}, @{thm mult_1_right}, @{thm div_1}, @{thm numeral_1_eq_Suc_0}];
 | 
| 331 | ||
| 30649 
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
 nipkow parents: 
30518diff
changeset | 332 | fun cancel_simplify_meta_eq ss cancel_th th = | 
| 23164 | 333 | simplify_one ss (([th, cancel_th]) MRS trans); | 
| 334 | ||
| 335 | structure CancelFactorCommon = | |
| 44945 | 336 | struct | 
| 337 | val mk_sum = (fn T : typ => long_mk_prod) | |
| 338 | val dest_sum = dest_prod | |
| 339 | val mk_coeff = mk_coeff | |
| 340 | val dest_coeff = dest_coeff | |
| 341 | val find_first = find_first_t [] | |
| 44947 
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
 haftmann parents: 
44945diff
changeset | 342 | val trans_tac = Numeral_Simprocs.trans_tac | 
| 45668 
0ea1c705eebb
use HOL_basic_ss instead of HOL_ss for internal simproc proofs (cf. b36eedcd1633)
 huffman parents: 
45463diff
changeset | 343 |   val norm_ss = HOL_basic_ss addsimps mult_1s @ @{thms mult_ac}
 | 
| 23164 | 344 | 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 | 345 | 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 | 346 | fun mk_eq (a, b) = HOLogic.mk_Trueprop (HOLogic.mk_eq (a, b)) | 
| 44945 | 347 | end; | 
| 23164 | 348 | |
| 349 | structure EqCancelFactor = ExtractCommonTermFun | |
| 350 | (open CancelFactorCommon | |
| 351 | val mk_bal = HOLogic.mk_eq | |
| 38864 
4abe644fcea5
formerly unnamed infix equality now named HOL.eq
 haftmann parents: 
37388diff
changeset | 352 |   val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} HOLogic.natT
 | 
| 31368 | 353 |   fun simp_conv _ _ = SOME @{thm nat_mult_eq_cancel_disj}
 | 
| 23164 | 354 | ); | 
| 355 | ||
| 44945 | 356 | structure LeCancelFactor = ExtractCommonTermFun | 
| 357 | (open CancelFactorCommon | |
| 358 |   val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less_eq}
 | |
| 359 |   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} HOLogic.natT
 | |
| 360 |   fun simp_conv _ _ = SOME @{thm nat_mult_le_cancel_disj}
 | |
| 361 | ); | |
| 362 | ||
| 23164 | 363 | structure LessCancelFactor = ExtractCommonTermFun | 
| 364 | (open CancelFactorCommon | |
| 35092 
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
 haftmann parents: 
35064diff
changeset | 365 |   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 | 366 |   val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} HOLogic.natT
 | 
| 31368 | 367 |   fun simp_conv _ _ = SOME @{thm nat_mult_less_cancel_disj}
 | 
| 23164 | 368 | ); | 
| 369 | ||
| 370 | structure DivideCancelFactor = ExtractCommonTermFun | |
| 371 | (open CancelFactorCommon | |
| 372 |   val mk_bal   = HOLogic.mk_binop @{const_name Divides.div}
 | |
| 373 |   val dest_bal = HOLogic.dest_bin @{const_name Divides.div} HOLogic.natT
 | |
| 31368 | 374 |   fun simp_conv _ _ = SOME @{thm nat_mult_div_cancel_disj}
 | 
| 23164 | 375 | ); | 
| 376 | ||
| 23969 | 377 | structure DvdCancelFactor = ExtractCommonTermFun | 
| 378 | (open CancelFactorCommon | |
| 35050 
9f841f20dca6
renamed OrderedGroup to Groups; split theory Ring_and_Field into Rings Fields
 haftmann parents: 
34974diff
changeset | 379 |   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 | 380 |   val dest_bal = HOLogic.dest_bin @{const_name Rings.dvd} HOLogic.natT
 | 
| 31368 | 381 |   fun simp_conv _ _ = SOME @{thm nat_mult_dvd_cancel_disj}
 | 
| 23969 | 382 | ); | 
| 383 | ||
| 45462 
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
 huffman parents: 
45436diff
changeset | 384 | fun eq_cancel_factor ss ct = EqCancelFactor.proc ss (term_of ct) | 
| 
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
 huffman parents: 
45436diff
changeset | 385 | fun less_cancel_factor ss ct = LessCancelFactor.proc ss (term_of ct) | 
| 
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
 huffman parents: 
45436diff
changeset | 386 | fun le_cancel_factor ss ct = LeCancelFactor.proc ss (term_of ct) | 
| 45463 
9a588a835c1e
use simproc_setup for the remaining nat_numeral simprocs
 huffman parents: 
45462diff
changeset | 387 | fun div_cancel_factor ss ct = DivideCancelFactor.proc ss (term_of ct) | 
| 45462 
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
 huffman parents: 
45436diff
changeset | 388 | fun dvd_cancel_factor ss ct = DvdCancelFactor.proc ss (term_of ct) | 
| 23164 | 389 | |
| 390 | end; |