author | haftmann |
Mon, 20 Jan 2025 22:15:11 +0100 | |
changeset 81875 | 7fe20d394593 |
parent 78800 | 0b3700d31758 |
permissions | -rw-r--r-- |
23146 | 1 |
(* Title: ZF/int_arith.ML |
2 |
Author: Larry Paulson |
|
3 |
||
4 |
Simprocs for linear arithmetic. |
|
5 |
*) |
|
6 |
||
40312
dff9f73a3763
more conventional exceptions for abstract syntax operations -- eliminated ancient SYS_ERROR;
wenzelm
parents:
38715
diff
changeset
|
7 |
signature INT_NUMERAL_SIMPROCS = |
dff9f73a3763
more conventional exceptions for abstract syntax operations -- eliminated ancient SYS_ERROR;
wenzelm
parents:
38715
diff
changeset
|
8 |
sig |
78800 | 9 |
val inteq_cancel_numerals_proc: Simplifier.proc |
10 |
val intless_cancel_numerals_proc: Simplifier.proc |
|
11 |
val intle_cancel_numerals_proc: Simplifier.proc |
|
12 |
val int_combine_numerals_proc: Simplifier.proc |
|
13 |
val int_combine_numerals_prod_proc: Simplifier.proc |
|
40312
dff9f73a3763
more conventional exceptions for abstract syntax operations -- eliminated ancient SYS_ERROR;
wenzelm
parents:
38715
diff
changeset
|
14 |
end |
dff9f73a3763
more conventional exceptions for abstract syntax operations -- eliminated ancient SYS_ERROR;
wenzelm
parents:
38715
diff
changeset
|
15 |
|
dff9f73a3763
more conventional exceptions for abstract syntax operations -- eliminated ancient SYS_ERROR;
wenzelm
parents:
38715
diff
changeset
|
16 |
structure Int_Numeral_Simprocs: INT_NUMERAL_SIMPROCS = |
23146 | 17 |
struct |
18 |
||
35112
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
19 |
(* abstract syntax operations *) |
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
20 |
|
69593 | 21 |
fun mk_bit 0 = \<^term>\<open>0\<close> |
22 |
| mk_bit 1 = \<^term>\<open>succ(0)\<close> |
|
40312
dff9f73a3763
more conventional exceptions for abstract syntax operations -- eliminated ancient SYS_ERROR;
wenzelm
parents:
38715
diff
changeset
|
23 |
| mk_bit _ = raise TERM ("mk_bit", []); |
35112
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
24 |
|
69593 | 25 |
fun dest_bit \<^term>\<open>0\<close> = 0 |
26 |
| dest_bit \<^term>\<open>succ(0)\<close> = 1 |
|
40312
dff9f73a3763
more conventional exceptions for abstract syntax operations -- eliminated ancient SYS_ERROR;
wenzelm
parents:
38715
diff
changeset
|
27 |
| dest_bit t = raise TERM ("dest_bit", [t]); |
35112
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
28 |
|
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
29 |
fun mk_bin i = |
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
30 |
let |
69593 | 31 |
fun term_of [] = \<^term>\<open>Pls\<close> |
32 |
| term_of [~1] = \<^term>\<open>Min\<close> |
|
33 |
| term_of (b :: bs) = \<^term>\<open>Bit\<close> $ term_of bs $ mk_bit b; |
|
35123 | 34 |
in term_of (Numeral_Syntax.make_binary i) end; |
35112
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
35 |
|
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
36 |
fun dest_bin tm = |
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
37 |
let |
69593 | 38 |
fun bin_of \<^term>\<open>Pls\<close> = [] |
39 |
| bin_of \<^term>\<open>Min\<close> = [~1] |
|
40 |
| bin_of (\<^term>\<open>Bit\<close> $ bs $ b) = dest_bit b :: bin_of bs |
|
40312
dff9f73a3763
more conventional exceptions for abstract syntax operations -- eliminated ancient SYS_ERROR;
wenzelm
parents:
38715
diff
changeset
|
41 |
| bin_of _ = raise TERM ("dest_bin", [tm]); |
35123 | 42 |
in Numeral_Syntax.dest_binary (bin_of tm) end; |
35112
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
43 |
|
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
44 |
|
23146 | 45 |
(*Utilities*) |
46 |
||
74296 | 47 |
fun mk_numeral i = \<^Const>\<open>integ_of\<close> $ mk_bin i; |
23146 | 48 |
|
74294 | 49 |
fun dest_numeral \<^Const_>\<open>integ_of for w\<close> = dest_bin w |
40312
dff9f73a3763
more conventional exceptions for abstract syntax operations -- eliminated ancient SYS_ERROR;
wenzelm
parents:
38715
diff
changeset
|
50 |
| dest_numeral t = raise TERM ("dest_numeral", [t]); |
23146 | 51 |
|
52 |
fun find_first_numeral past (t::terms) = |
|
53 |
((dest_numeral t, rev past @ terms) |
|
54 |
handle TERM _ => find_first_numeral (t::past) terms) |
|
78791 | 55 |
| find_first_numeral _ [] = raise TERM("find_first_numeral", []); |
23146 | 56 |
|
57 |
val zero = mk_numeral 0; |
|
74297 | 58 |
fun mk_plus (t, u) = \<^Const>\<open>zadd for t u\<close>; |
23146 | 59 |
|
60 |
(*Thus mk_sum[t] yields t+#0; longer sums don't have a trailing zero*) |
|
61 |
fun mk_sum [] = zero |
|
62 |
| mk_sum [t,u] = mk_plus (t, u) |
|
63 |
| mk_sum (t :: ts) = mk_plus (t, mk_sum ts); |
|
64 |
||
65 |
(*this version ALWAYS includes a trailing zero*) |
|
66 |
fun long_mk_sum [] = zero |
|
67 |
| long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts); |
|
68 |
||
69 |
(*decompose additions AND subtractions as a sum*) |
|
74294 | 70 |
fun dest_summing (pos, \<^Const_>\<open>zadd for t u\<close>, ts) = |
23146 | 71 |
dest_summing (pos, t, dest_summing (pos, u, ts)) |
74294 | 72 |
| dest_summing (pos, \<^Const_>\<open>zdiff for t u\<close>, ts) = |
23146 | 73 |
dest_summing (pos, t, dest_summing (not pos, u, ts)) |
74 |
| dest_summing (pos, t, ts) = |
|
74296 | 75 |
if pos then t::ts else \<^Const>\<open>zminus for t\<close> :: ts; |
23146 | 76 |
|
77 |
fun dest_sum t = dest_summing (true, t, []); |
|
78 |
||
79 |
val one = mk_numeral 1; |
|
74297 | 80 |
fun mk_times (t, u) = \<^Const>\<open>zmult for t u\<close>; |
23146 | 81 |
|
82 |
fun mk_prod [] = one |
|
83 |
| mk_prod [t] = t |
|
84 |
| mk_prod (t :: ts) = if t = one then mk_prod ts |
|
85 |
else mk_times (t, mk_prod ts); |
|
86 |
||
74319
54b2e5f771da
clarified signature -- prefer antiquotations (with subtle change of exception content);
wenzelm
parents:
74297
diff
changeset
|
87 |
fun dest_prod tm = |
54b2e5f771da
clarified signature -- prefer antiquotations (with subtle change of exception content);
wenzelm
parents:
74297
diff
changeset
|
88 |
let val (t,u) = \<^Const_fn>\<open>zmult for t u => \<open>(t, u)\<close>\<close> tm |
54b2e5f771da
clarified signature -- prefer antiquotations (with subtle change of exception content);
wenzelm
parents:
74297
diff
changeset
|
89 |
in dest_prod t @ dest_prod u end |
54b2e5f771da
clarified signature -- prefer antiquotations (with subtle change of exception content);
wenzelm
parents:
74297
diff
changeset
|
90 |
handle TERM _ => [tm]; |
23146 | 91 |
|
92 |
(*DON'T do the obvious simplifications; that would create special cases*) |
|
93 |
fun mk_coeff (k, t) = mk_times (mk_numeral k, t); |
|
94 |
||
95 |
(*Express t as a product of (possibly) a numeral with other sorted terms*) |
|
74294 | 96 |
fun dest_coeff sign \<^Const_>\<open>zminus for t\<close> = dest_coeff (~sign) t |
23146 | 97 |
| dest_coeff sign t = |
35408 | 98 |
let val ts = sort Term_Ord.term_ord (dest_prod t) |
23146 | 99 |
val (n, ts') = find_first_numeral [] ts |
100 |
handle TERM _ => (1, ts) |
|
101 |
in (sign*n, mk_prod ts') end; |
|
102 |
||
103 |
(*Find first coefficient-term THAT MATCHES u*) |
|
78791 | 104 |
fun find_first_coeff _ _ [] = raise TERM("find_first_coeff", []) |
23146 | 105 |
| find_first_coeff past u (t::terms) = |
106 |
let val (n,u') = dest_coeff 1 t |
|
107 |
in if u aconv u' then (n, rev past @ terms) |
|
108 |
else find_first_coeff (t::past) u terms |
|
109 |
end |
|
110 |
handle TERM _ => find_first_coeff (t::past) u terms; |
|
111 |
||
112 |
||
113 |
(*Simplify #1*n and n*#1 to n*) |
|
24893 | 114 |
val add_0s = [@{thm zadd_0_intify}, @{thm zadd_0_right_intify}]; |
23146 | 115 |
|
24893 | 116 |
val mult_1s = [@{thm zmult_1_intify}, @{thm zmult_1_right_intify}, |
117 |
@{thm zmult_minus1}, @{thm zmult_minus1_right}]; |
|
23146 | 118 |
|
24893 | 119 |
val tc_rules = [@{thm integ_of_type}, @{thm intify_in_int}, |
120 |
@{thm int_of_type}, @{thm zadd_type}, @{thm zdiff_type}, @{thm zmult_type}] @ |
|
121 |
@{thms bin.intros}; |
|
122 |
val intifys = [@{thm intify_ident}, @{thm zadd_intify1}, @{thm zadd_intify2}, |
|
123 |
@{thm zdiff_intify1}, @{thm zdiff_intify2}, @{thm zmult_intify1}, @{thm zmult_intify2}, |
|
124 |
@{thm zless_intify1}, @{thm zless_intify2}, @{thm zle_intify1}, @{thm zle_intify2}]; |
|
23146 | 125 |
|
126 |
(*To perform binary arithmetic*) |
|
24893 | 127 |
val bin_simps = [@{thm add_integ_of_left}] @ @{thms bin_arith_simps} @ @{thms bin_rel_simps}; |
23146 | 128 |
|
129 |
(*To evaluate binary negations of coefficients*) |
|
24893 | 130 |
val zminus_simps = @{thms NCons_simps} @ |
35409 | 131 |
[@{thm integ_of_minus} RS @{thm sym}, |
24893 | 132 |
@{thm bin_minus_1}, @{thm bin_minus_0}, @{thm bin_minus_Pls}, @{thm bin_minus_Min}, |
133 |
@{thm bin_pred_1}, @{thm bin_pred_0}, @{thm bin_pred_Pls}, @{thm bin_pred_Min}]; |
|
23146 | 134 |
|
135 |
(*To let us treat subtraction as addition*) |
|
24893 | 136 |
val diff_simps = [@{thm zdiff_def}, @{thm zminus_zadd_distrib}, @{thm zminus_zminus}]; |
23146 | 137 |
|
35020
862a20ffa8e2
prefer explicit @{lemma} over adhoc forward reasoning;
wenzelm
parents:
32957
diff
changeset
|
138 |
(*push the unary minus down*) |
862a20ffa8e2
prefer explicit @{lemma} over adhoc forward reasoning;
wenzelm
parents:
32957
diff
changeset
|
139 |
val int_minus_mult_eq_1_to_2 = @{lemma "$- w $* z = w $* $- z" by simp}; |
23146 | 140 |
|
141 |
(*to extract again any uncancelled minuses*) |
|
142 |
val int_minus_from_mult_simps = |
|
24893 | 143 |
[@{thm zminus_zminus}, @{thm zmult_zminus}, @{thm zmult_zminus_right}]; |
23146 | 144 |
|
145 |
(*combine unary minus with numeric literals, however nested within a product*) |
|
146 |
val int_mult_minus_simps = |
|
35409 | 147 |
[@{thm zmult_assoc}, @{thm zmult_zminus} RS @{thm sym}, int_minus_mult_eq_1_to_2]; |
23146 | 148 |
|
149 |
structure CancelNumeralsCommon = |
|
150 |
struct |
|
59530 | 151 |
val mk_sum = (fn _ : typ => mk_sum) |
152 |
val dest_sum = dest_sum |
|
153 |
val mk_coeff = mk_coeff |
|
154 |
val dest_coeff = dest_coeff 1 |
|
155 |
val find_first_coeff = find_first_coeff [] |
|
156 |
fun trans_tac ctxt = ArithData.gen_trans_tac ctxt @{thm iff_trans} |
|
23146 | 157 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
158 |
val norm_ss1 = |
69593 | 159 |
simpset_of (put_simpset ZF_ss \<^context> |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
160 |
addsimps add_0s @ mult_1s @ diff_simps @ zminus_simps @ @{thms zadd_ac}) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
161 |
val norm_ss2 = |
69593 | 162 |
simpset_of (put_simpset ZF_ss \<^context> |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
163 |
addsimps bin_simps @ int_mult_minus_simps @ intifys) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
164 |
val norm_ss3 = |
69593 | 165 |
simpset_of (put_simpset ZF_ss \<^context> |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
166 |
addsimps int_minus_from_mult_simps @ @{thms zadd_ac} @ @{thms zmult_ac} @ tc_rules @ intifys) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
167 |
fun norm_tac ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
168 |
ALLGOALS (asm_simp_tac (put_simpset norm_ss1 ctxt)) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
169 |
THEN ALLGOALS (asm_simp_tac (put_simpset norm_ss2 ctxt)) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
170 |
THEN ALLGOALS (asm_simp_tac (put_simpset norm_ss3 ctxt)) |
23146 | 171 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
172 |
val numeral_simp_ss = |
69593 | 173 |
simpset_of (put_simpset ZF_ss \<^context> |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
174 |
addsimps add_0s @ bin_simps @ tc_rules @ intifys) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
175 |
fun numeral_simp_tac ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
176 |
ALLGOALS (simp_tac (put_simpset numeral_simp_ss ctxt)) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
177 |
THEN ALLGOALS (asm_simp_tac ctxt) |
23146 | 178 |
val simplify_meta_eq = ArithData.simplify_meta_eq (add_0s @ mult_1s) |
179 |
end; |
|
180 |
||
181 |
||
182 |
structure EqCancelNumerals = CancelNumeralsFun |
|
183 |
(open CancelNumeralsCommon |
|
184 |
val prove_conv = ArithData.prove_conv "inteq_cancel_numerals" |
|
185 |
val mk_bal = FOLogic.mk_eq |
|
186 |
val dest_bal = FOLogic.dest_eq |
|
35409 | 187 |
val bal_add1 = @{thm eq_add_iff1} RS @{thm iff_trans} |
188 |
val bal_add2 = @{thm eq_add_iff2} RS @{thm iff_trans} |
|
23146 | 189 |
); |
190 |
||
191 |
structure LessCancelNumerals = CancelNumeralsFun |
|
192 |
(open CancelNumeralsCommon |
|
193 |
val prove_conv = ArithData.prove_conv "intless_cancel_numerals" |
|
74297 | 194 |
fun mk_bal (t, u) = \<^Const>\<open>zless for t u\<close> |
74319
54b2e5f771da
clarified signature -- prefer antiquotations (with subtle change of exception content);
wenzelm
parents:
74297
diff
changeset
|
195 |
val dest_bal = \<^Const_fn>\<open>zless for t u => \<open>(t, u)\<close>\<close> |
35409 | 196 |
val bal_add1 = @{thm less_add_iff1} RS @{thm iff_trans} |
197 |
val bal_add2 = @{thm less_add_iff2} RS @{thm iff_trans} |
|
23146 | 198 |
); |
199 |
||
200 |
structure LeCancelNumerals = CancelNumeralsFun |
|
201 |
(open CancelNumeralsCommon |
|
202 |
val prove_conv = ArithData.prove_conv "intle_cancel_numerals" |
|
74297 | 203 |
fun mk_bal (t, u) = \<^Const>\<open>zle for t u\<close> |
74319
54b2e5f771da
clarified signature -- prefer antiquotations (with subtle change of exception content);
wenzelm
parents:
74297
diff
changeset
|
204 |
val dest_bal = \<^Const_fn>\<open>zle for t u => \<open>(t, u)\<close>\<close> |
35409 | 205 |
val bal_add1 = @{thm le_add_iff1} RS @{thm iff_trans} |
206 |
val bal_add2 = @{thm le_add_iff2} RS @{thm iff_trans} |
|
23146 | 207 |
); |
208 |
||
78791 | 209 |
val inteq_cancel_numerals_proc = EqCancelNumerals.proc; |
210 |
val intless_cancel_numerals_proc = LessCancelNumerals.proc; |
|
211 |
val intle_cancel_numerals_proc = LeCancelNumerals.proc; |
|
23146 | 212 |
|
213 |
||
214 |
(*version without the hyps argument*) |
|
215 |
fun prove_conv_nohyps name tacs sg = ArithData.prove_conv name tacs sg []; |
|
216 |
||
217 |
structure CombineNumeralsData = |
|
218 |
struct |
|
59530 | 219 |
type coeff = int |
220 |
val iszero = (fn x => x = 0) |
|
221 |
val add = op + |
|
222 |
val mk_sum = (fn _ : typ => long_mk_sum) (*to work for #2*x $+ #3*x *) |
|
223 |
val dest_sum = dest_sum |
|
224 |
val mk_coeff = mk_coeff |
|
225 |
val dest_coeff = dest_coeff 1 |
|
226 |
val left_distrib = @{thm left_zadd_zmult_distrib} RS @{thm trans} |
|
227 |
val prove_conv = prove_conv_nohyps "int_combine_numerals" |
|
228 |
fun trans_tac ctxt = ArithData.gen_trans_tac ctxt @{thm trans} |
|
23146 | 229 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
230 |
val norm_ss1 = |
69593 | 231 |
simpset_of (put_simpset ZF_ss \<^context> |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
232 |
addsimps add_0s @ mult_1s @ diff_simps @ zminus_simps @ @{thms zadd_ac} @ intifys) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
233 |
val norm_ss2 = |
69593 | 234 |
simpset_of (put_simpset ZF_ss \<^context> |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
235 |
addsimps bin_simps @ int_mult_minus_simps @ intifys) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
236 |
val norm_ss3 = |
69593 | 237 |
simpset_of (put_simpset ZF_ss \<^context> |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
238 |
addsimps int_minus_from_mult_simps @ @{thms zadd_ac} @ @{thms zmult_ac} @ tc_rules @ intifys) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
239 |
fun norm_tac ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
240 |
ALLGOALS (asm_simp_tac (put_simpset norm_ss1 ctxt)) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
241 |
THEN ALLGOALS (asm_simp_tac (put_simpset norm_ss2 ctxt)) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
242 |
THEN ALLGOALS (asm_simp_tac (put_simpset norm_ss3 ctxt)) |
23146 | 243 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
244 |
val numeral_simp_ss = |
69593 | 245 |
simpset_of (put_simpset ZF_ss \<^context> addsimps add_0s @ bin_simps @ tc_rules @ intifys) |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
246 |
fun numeral_simp_tac ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
247 |
ALLGOALS (simp_tac (put_simpset numeral_simp_ss ctxt)) |
23146 | 248 |
val simplify_meta_eq = ArithData.simplify_meta_eq (add_0s @ mult_1s) |
249 |
end; |
|
250 |
||
251 |
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData); |
|
78791 | 252 |
val int_combine_numerals_proc = CombineNumerals.proc |
23146 | 253 |
|
254 |
||
255 |
||
256 |
(** Constant folding for integer multiplication **) |
|
257 |
||
258 |
(*The trick is to regard products as sums, e.g. #3 $* x $* #4 as |
|
259 |
the "sum" of #3, x, #4; the literals are then multiplied*) |
|
260 |
||
261 |
||
262 |
structure CombineNumeralsProdData = |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
263 |
struct |
59530 | 264 |
type coeff = int |
265 |
val iszero = (fn x => x = 0) |
|
266 |
val add = op * |
|
267 |
val mk_sum = (fn _ : typ => mk_prod) |
|
268 |
val dest_sum = dest_prod |
|
269 |
fun mk_coeff(k,t) = |
|
270 |
if t = one then mk_numeral k |
|
271 |
else raise TERM("mk_coeff", []) |
|
23146 | 272 |
fun dest_coeff t = (dest_numeral t, one) (*We ONLY want pure numerals.*) |
59530 | 273 |
val left_distrib = @{thm zmult_assoc} RS @{thm sym} RS @{thm trans} |
274 |
val prove_conv = prove_conv_nohyps "int_combine_numerals_prod" |
|
275 |
fun trans_tac ctxt = ArithData.gen_trans_tac ctxt @{thm trans} |
|
23146 | 276 |
|
59530 | 277 |
val norm_ss1 = |
69593 | 278 |
simpset_of (put_simpset ZF_ss \<^context> addsimps mult_1s @ diff_simps @ zminus_simps) |
59530 | 279 |
val norm_ss2 = |
69593 | 280 |
simpset_of (put_simpset ZF_ss \<^context> addsimps [@{thm zmult_zminus_right} RS @{thm sym}] @ |
59530 | 281 |
bin_simps @ @{thms zmult_ac} @ tc_rules @ intifys) |
282 |
fun norm_tac ctxt = |
|
283 |
ALLGOALS (asm_simp_tac (put_simpset norm_ss1 ctxt)) |
|
284 |
THEN ALLGOALS (asm_simp_tac (put_simpset norm_ss2 ctxt)) |
|
23146 | 285 |
|
59530 | 286 |
val numeral_simp_ss = |
69593 | 287 |
simpset_of (put_simpset ZF_ss \<^context> addsimps bin_simps @ tc_rules @ intifys) |
59530 | 288 |
fun numeral_simp_tac ctxt = |
289 |
ALLGOALS (simp_tac (put_simpset numeral_simp_ss ctxt)) |
|
290 |
val simplify_meta_eq = ArithData.simplify_meta_eq (mult_1s); |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
291 |
end; |
23146 | 292 |
|
293 |
||
294 |
structure CombineNumeralsProd = CombineNumeralsFun(CombineNumeralsProdData); |
|
78791 | 295 |
val int_combine_numerals_prod_proc = CombineNumeralsProd.proc; |
23146 | 296 |
|
297 |
end; |