author | blanchet |
Thu, 12 Sep 2013 09:59:45 +0200 | |
changeset 53555 | 12251bc889f1 |
parent 51717 | 9e7d1c139569 |
child 54388 | 8b165615ffe3 |
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 |
dff9f73a3763
more conventional exceptions for abstract syntax operations -- eliminated ancient SYS_ERROR;
wenzelm
parents:
38715
diff
changeset
|
9 |
val cancel_numerals: simproc list |
dff9f73a3763
more conventional exceptions for abstract syntax operations -- eliminated ancient SYS_ERROR;
wenzelm
parents:
38715
diff
changeset
|
10 |
val combine_numerals: simproc |
dff9f73a3763
more conventional exceptions for abstract syntax operations -- eliminated ancient SYS_ERROR;
wenzelm
parents:
38715
diff
changeset
|
11 |
val combine_numerals_prod: simproc |
dff9f73a3763
more conventional exceptions for abstract syntax operations -- eliminated ancient SYS_ERROR;
wenzelm
parents:
38715
diff
changeset
|
12 |
end |
dff9f73a3763
more conventional exceptions for abstract syntax operations -- eliminated ancient SYS_ERROR;
wenzelm
parents:
38715
diff
changeset
|
13 |
|
dff9f73a3763
more conventional exceptions for abstract syntax operations -- eliminated ancient SYS_ERROR;
wenzelm
parents:
38715
diff
changeset
|
14 |
structure Int_Numeral_Simprocs: INT_NUMERAL_SIMPROCS = |
23146 | 15 |
struct |
16 |
||
35112
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
17 |
(* abstract syntax operations *) |
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
18 |
|
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
19 |
fun mk_bit 0 = @{term "0"} |
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
20 |
| mk_bit 1 = @{term "succ(0)"} |
40312
dff9f73a3763
more conventional exceptions for abstract syntax operations -- eliminated ancient SYS_ERROR;
wenzelm
parents:
38715
diff
changeset
|
21 |
| mk_bit _ = raise TERM ("mk_bit", []); |
35112
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
22 |
|
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
23 |
fun dest_bit @{term "0"} = 0 |
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
24 |
| dest_bit @{term "succ(0)"} = 1 |
40312
dff9f73a3763
more conventional exceptions for abstract syntax operations -- eliminated ancient SYS_ERROR;
wenzelm
parents:
38715
diff
changeset
|
25 |
| dest_bit t = raise TERM ("dest_bit", [t]); |
35112
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
26 |
|
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
27 |
fun mk_bin i = |
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
28 |
let |
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
29 |
fun term_of [] = @{term Pls} |
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
30 |
| term_of [~1] = @{term Min} |
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
31 |
| term_of (b :: bs) = @{term Bit} $ term_of bs $ mk_bit b; |
35123 | 32 |
in term_of (Numeral_Syntax.make_binary i) end; |
35112
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
33 |
|
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
34 |
fun dest_bin tm = |
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
35 |
let |
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
36 |
fun bin_of @{term Pls} = [] |
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
37 |
| bin_of @{term Min} = [~1] |
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
38 |
| bin_of (@{term Bit} $ 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
|
39 |
| bin_of _ = raise TERM ("dest_bin", [tm]); |
35123 | 40 |
in Numeral_Syntax.dest_binary (bin_of tm) end; |
35112
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
41 |
|
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
42 |
|
23146 | 43 |
(*Utilities*) |
44 |
||
35112
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
35020
diff
changeset
|
45 |
fun mk_numeral i = @{const integ_of} $ mk_bin i; |
23146 | 46 |
|
40312
dff9f73a3763
more conventional exceptions for abstract syntax operations -- eliminated ancient SYS_ERROR;
wenzelm
parents:
38715
diff
changeset
|
47 |
fun dest_numeral (Const(@{const_name integ_of}, _) $ w) = dest_bin w |
dff9f73a3763
more conventional exceptions for abstract syntax operations -- eliminated ancient SYS_ERROR;
wenzelm
parents:
38715
diff
changeset
|
48 |
| dest_numeral t = raise TERM ("dest_numeral", [t]); |
23146 | 49 |
|
50 |
fun find_first_numeral past (t::terms) = |
|
51 |
((dest_numeral t, rev past @ terms) |
|
52 |
handle TERM _ => find_first_numeral (t::past) terms) |
|
53 |
| find_first_numeral past [] = raise TERM("find_first_numeral", []); |
|
54 |
||
55 |
val zero = mk_numeral 0; |
|
26059 | 56 |
val mk_plus = FOLogic.mk_binop @{const_name "zadd"}; |
23146 | 57 |
|
58 |
(*Thus mk_sum[t] yields t+#0; longer sums don't have a trailing zero*) |
|
59 |
fun mk_sum [] = zero |
|
60 |
| mk_sum [t,u] = mk_plus (t, u) |
|
61 |
| mk_sum (t :: ts) = mk_plus (t, mk_sum ts); |
|
62 |
||
63 |
(*this version ALWAYS includes a trailing zero*) |
|
64 |
fun long_mk_sum [] = zero |
|
65 |
| long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts); |
|
66 |
||
67 |
(*decompose additions AND subtractions as a sum*) |
|
26059 | 68 |
fun dest_summing (pos, Const (@{const_name "zadd"}, _) $ t $ u, ts) = |
23146 | 69 |
dest_summing (pos, t, dest_summing (pos, u, ts)) |
26059 | 70 |
| dest_summing (pos, Const (@{const_name "zdiff"}, _) $ t $ u, ts) = |
23146 | 71 |
dest_summing (pos, t, dest_summing (not pos, u, ts)) |
72 |
| dest_summing (pos, t, ts) = |
|
27237 | 73 |
if pos then t::ts else @{const zminus} $ t :: ts; |
23146 | 74 |
|
75 |
fun dest_sum t = dest_summing (true, t, []); |
|
76 |
||
77 |
val one = mk_numeral 1; |
|
26059 | 78 |
val mk_times = FOLogic.mk_binop @{const_name "zmult"}; |
23146 | 79 |
|
80 |
fun mk_prod [] = one |
|
81 |
| mk_prod [t] = t |
|
82 |
| mk_prod (t :: ts) = if t = one then mk_prod ts |
|
83 |
else mk_times (t, mk_prod ts); |
|
84 |
||
26190 | 85 |
val dest_times = FOLogic.dest_bin @{const_name "zmult"} @{typ i}; |
23146 | 86 |
|
87 |
fun dest_prod t = |
|
88 |
let val (t,u) = dest_times t |
|
89 |
in dest_prod t @ dest_prod u end |
|
90 |
handle TERM _ => [t]; |
|
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*) |
|
26059 | 96 |
fun dest_coeff sign (Const (@{const_name "zminus"}, _) $ t) = 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*) |
|
104 |
fun find_first_coeff past u [] = raise TERM("find_first_coeff", []) |
|
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 |
|
32155 | 149 |
fun prep_simproc thy (name, pats, proc) = |
38715
6513ea67d95d
renamed Simplifier.simproc(_i) to Simplifier.simproc_global(_i) to emphasize that this is not the real thing;
wenzelm
parents:
35409
diff
changeset
|
150 |
Simplifier.simproc_global thy name pats proc; |
23146 | 151 |
|
152 |
structure CancelNumeralsCommon = |
|
153 |
struct |
|
154 |
val mk_sum = (fn T:typ => mk_sum) |
|
155 |
val dest_sum = dest_sum |
|
156 |
val mk_coeff = mk_coeff |
|
157 |
val dest_coeff = dest_coeff 1 |
|
158 |
val find_first_coeff = find_first_coeff [] |
|
44947
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
haftmann
parents:
40878
diff
changeset
|
159 |
val trans_tac = ArithData.gen_trans_tac @{thm iff_trans} |
23146 | 160 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
161 |
val norm_ss1 = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
162 |
simpset_of (put_simpset ZF_ss @{context} |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
163 |
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
|
164 |
val norm_ss2 = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
165 |
simpset_of (put_simpset ZF_ss @{context} |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
166 |
addsimps bin_simps @ int_mult_minus_simps @ intifys) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
167 |
val norm_ss3 = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
168 |
simpset_of (put_simpset ZF_ss @{context} |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
169 |
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
|
170 |
fun norm_tac ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
171 |
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
|
172 |
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
|
173 |
THEN ALLGOALS (asm_simp_tac (put_simpset norm_ss3 ctxt)) |
23146 | 174 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
175 |
val numeral_simp_ss = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
176 |
simpset_of (put_simpset ZF_ss @{context} |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
177 |
addsimps add_0s @ bin_simps @ tc_rules @ intifys) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
178 |
fun numeral_simp_tac ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
179 |
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
|
180 |
THEN ALLGOALS (asm_simp_tac ctxt) |
23146 | 181 |
val simplify_meta_eq = ArithData.simplify_meta_eq (add_0s @ mult_1s) |
182 |
end; |
|
183 |
||
184 |
||
185 |
structure EqCancelNumerals = CancelNumeralsFun |
|
186 |
(open CancelNumeralsCommon |
|
187 |
val prove_conv = ArithData.prove_conv "inteq_cancel_numerals" |
|
188 |
val mk_bal = FOLogic.mk_eq |
|
189 |
val dest_bal = FOLogic.dest_eq |
|
35409 | 190 |
val bal_add1 = @{thm eq_add_iff1} RS @{thm iff_trans} |
191 |
val bal_add2 = @{thm eq_add_iff2} RS @{thm iff_trans} |
|
23146 | 192 |
); |
193 |
||
194 |
structure LessCancelNumerals = CancelNumeralsFun |
|
195 |
(open CancelNumeralsCommon |
|
196 |
val prove_conv = ArithData.prove_conv "intless_cancel_numerals" |
|
26059 | 197 |
val mk_bal = FOLogic.mk_binrel @{const_name "zless"} |
26190 | 198 |
val dest_bal = FOLogic.dest_bin @{const_name "zless"} @{typ i} |
35409 | 199 |
val bal_add1 = @{thm less_add_iff1} RS @{thm iff_trans} |
200 |
val bal_add2 = @{thm less_add_iff2} RS @{thm iff_trans} |
|
23146 | 201 |
); |
202 |
||
203 |
structure LeCancelNumerals = CancelNumeralsFun |
|
204 |
(open CancelNumeralsCommon |
|
205 |
val prove_conv = ArithData.prove_conv "intle_cancel_numerals" |
|
26059 | 206 |
val mk_bal = FOLogic.mk_binrel @{const_name "zle"} |
26190 | 207 |
val dest_bal = FOLogic.dest_bin @{const_name "zle"} @{typ i} |
35409 | 208 |
val bal_add1 = @{thm le_add_iff1} RS @{thm iff_trans} |
209 |
val bal_add2 = @{thm le_add_iff2} RS @{thm iff_trans} |
|
23146 | 210 |
); |
211 |
||
212 |
val cancel_numerals = |
|
32155 | 213 |
map (prep_simproc @{theory}) |
23146 | 214 |
[("inteq_cancel_numerals", |
215 |
["l $+ m = n", "l = m $+ n", |
|
216 |
"l $- m = n", "l = m $- n", |
|
217 |
"l $* m = n", "l = m $* n"], |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
218 |
EqCancelNumerals.proc), |
23146 | 219 |
("intless_cancel_numerals", |
220 |
["l $+ m $< n", "l $< m $+ n", |
|
221 |
"l $- m $< n", "l $< m $- n", |
|
222 |
"l $* m $< n", "l $< m $* n"], |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
223 |
LessCancelNumerals.proc), |
23146 | 224 |
("intle_cancel_numerals", |
225 |
["l $+ m $<= n", "l $<= m $+ n", |
|
226 |
"l $- m $<= n", "l $<= m $- n", |
|
227 |
"l $* m $<= n", "l $<= m $* n"], |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
228 |
LeCancelNumerals.proc)]; |
23146 | 229 |
|
230 |
||
231 |
(*version without the hyps argument*) |
|
232 |
fun prove_conv_nohyps name tacs sg = ArithData.prove_conv name tacs sg []; |
|
233 |
||
234 |
structure CombineNumeralsData = |
|
235 |
struct |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
23146
diff
changeset
|
236 |
type coeff = int |
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
23146
diff
changeset
|
237 |
val iszero = (fn x => x = 0) |
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
23146
diff
changeset
|
238 |
val add = op + |
23146 | 239 |
val mk_sum = (fn T:typ => long_mk_sum) (*to work for #2*x $+ #3*x *) |
240 |
val dest_sum = dest_sum |
|
241 |
val mk_coeff = mk_coeff |
|
242 |
val dest_coeff = dest_coeff 1 |
|
35409 | 243 |
val left_distrib = @{thm left_zadd_zmult_distrib} RS @{thm trans} |
23146 | 244 |
val prove_conv = prove_conv_nohyps "int_combine_numerals" |
44947
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
haftmann
parents:
40878
diff
changeset
|
245 |
val trans_tac = ArithData.gen_trans_tac @{thm trans} |
23146 | 246 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
247 |
val norm_ss1 = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
248 |
simpset_of (put_simpset ZF_ss @{context} |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
249 |
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
|
250 |
val norm_ss2 = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
251 |
simpset_of (put_simpset ZF_ss @{context} |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
252 |
addsimps bin_simps @ int_mult_minus_simps @ intifys) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
253 |
val norm_ss3 = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
254 |
simpset_of (put_simpset ZF_ss @{context} |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
255 |
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
|
256 |
fun norm_tac ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
257 |
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
|
258 |
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
|
259 |
THEN ALLGOALS (asm_simp_tac (put_simpset norm_ss3 ctxt)) |
23146 | 260 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
261 |
val numeral_simp_ss = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
262 |
simpset_of (put_simpset ZF_ss @{context} addsimps add_0s @ bin_simps @ tc_rules @ intifys) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
263 |
fun numeral_simp_tac ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
264 |
ALLGOALS (simp_tac (put_simpset numeral_simp_ss ctxt)) |
23146 | 265 |
val simplify_meta_eq = ArithData.simplify_meta_eq (add_0s @ mult_1s) |
266 |
end; |
|
267 |
||
268 |
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData); |
|
269 |
||
270 |
val combine_numerals = |
|
32155 | 271 |
prep_simproc @{theory} |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
272 |
("int_combine_numerals", ["i $+ j", "i $- j"], CombineNumerals.proc); |
23146 | 273 |
|
274 |
||
275 |
||
276 |
(** Constant folding for integer multiplication **) |
|
277 |
||
278 |
(*The trick is to regard products as sums, e.g. #3 $* x $* #4 as |
|
279 |
the "sum" of #3, x, #4; the literals are then multiplied*) |
|
280 |
||
281 |
||
282 |
structure CombineNumeralsProdData = |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
283 |
struct |
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
23146
diff
changeset
|
284 |
type coeff = int |
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
23146
diff
changeset
|
285 |
val iszero = (fn x => x = 0) |
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
23146
diff
changeset
|
286 |
val add = op * |
23146 | 287 |
val mk_sum = (fn T:typ => mk_prod) |
288 |
val dest_sum = dest_prod |
|
289 |
fun mk_coeff(k,t) = if t=one then mk_numeral k |
|
290 |
else raise TERM("mk_coeff", []) |
|
291 |
fun dest_coeff t = (dest_numeral t, one) (*We ONLY want pure numerals.*) |
|
35409 | 292 |
val left_distrib = @{thm zmult_assoc} RS @{thm sym} RS @{thm trans} |
23146 | 293 |
val prove_conv = prove_conv_nohyps "int_combine_numerals_prod" |
44947
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
haftmann
parents:
40878
diff
changeset
|
294 |
val trans_tac = ArithData.gen_trans_tac @{thm trans} |
23146 | 295 |
|
296 |
||
297 |
||
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
298 |
val norm_ss1 = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
299 |
simpset_of (put_simpset ZF_ss @{context} addsimps mult_1s @ diff_simps @ zminus_simps) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
300 |
val norm_ss2 = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
301 |
simpset_of (put_simpset ZF_ss @{context} addsimps [@{thm zmult_zminus_right} RS @{thm sym}] @ |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
302 |
bin_simps @ @{thms zmult_ac} @ tc_rules @ intifys) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
303 |
fun norm_tac ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
304 |
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
|
305 |
THEN ALLGOALS (asm_simp_tac (put_simpset norm_ss2 ctxt)) |
23146 | 306 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
307 |
val numeral_simp_ss = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
308 |
simpset_of (put_simpset ZF_ss @{context} addsimps bin_simps @ tc_rules @ intifys) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
309 |
fun numeral_simp_tac ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
310 |
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
|
311 |
val simplify_meta_eq = ArithData.simplify_meta_eq (mult_1s); |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
312 |
end; |
23146 | 313 |
|
314 |
||
315 |
structure CombineNumeralsProd = CombineNumeralsFun(CombineNumeralsProdData); |
|
316 |
||
317 |
val combine_numerals_prod = |
|
32155 | 318 |
prep_simproc @{theory} |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
44947
diff
changeset
|
319 |
("int_combine_numerals_prod", ["i $* j"], CombineNumeralsProd.proc); |
23146 | 320 |
|
321 |
end; |
|
322 |
||
323 |
||
324 |
Addsimprocs Int_Numeral_Simprocs.cancel_numerals; |
|
325 |
Addsimprocs [Int_Numeral_Simprocs.combine_numerals, |
|
326 |
Int_Numeral_Simprocs.combine_numerals_prod]; |
|
327 |
||
328 |
||
329 |
(*examples:*) |
|
330 |
(* |
|
331 |
print_depth 22; |
|
332 |
set timing; |
|
40878
7695e4de4d86
renamed trace_simp to simp_trace, and debug_simp to simp_debug;
wenzelm
parents:
40313
diff
changeset
|
333 |
set simp_trace; |
23146 | 334 |
fun test s = (Goal s; by (Asm_simp_tac 1)); |
335 |
val sg = #sign (rep_thm (topthm())); |
|
336 |
val t = FOLogic.dest_Trueprop (Logic.strip_assums_concl(getgoal 1)); |
|
337 |
val (t,_) = FOLogic.dest_eq t; |
|
338 |
||
339 |
(*combine_numerals_prod (products of separate literals) *) |
|
340 |
test "#5 $* x $* #3 = y"; |
|
341 |
||
342 |
test "y2 $+ ?x42 = y $+ y2"; |
|
343 |
||
344 |
test "oo : int ==> l $+ (l $+ #2) $+ oo = oo"; |
|
345 |
||
346 |
test "#9$*x $+ y = x$*#23 $+ z"; |
|
347 |
test "y $+ x = x $+ z"; |
|
348 |
||
349 |
test "x : int ==> x $+ y $+ z = x $+ z"; |
|
350 |
test "x : int ==> y $+ (z $+ x) = z $+ x"; |
|
351 |
test "z : int ==> x $+ y $+ z = (z $+ y) $+ (x $+ w)"; |
|
352 |
test "z : int ==> x$*y $+ z = (z $+ y) $+ (y$*x $+ w)"; |
|
353 |
||
354 |
test "#-3 $* x $+ y $<= x $* #2 $+ z"; |
|
355 |
test "y $+ x $<= x $+ z"; |
|
356 |
test "x $+ y $+ z $<= x $+ z"; |
|
357 |
||
358 |
test "y $+ (z $+ x) $< z $+ x"; |
|
359 |
test "x $+ y $+ z $< (z $+ y) $+ (x $+ w)"; |
|
360 |
test "x$*y $+ z $< (z $+ y) $+ (y$*x $+ w)"; |
|
361 |
||
362 |
test "l $+ #2 $+ #2 $+ #2 $+ (l $+ #2) $+ (oo $+ #2) = uu"; |
|
363 |
test "u : int ==> #2 $* u = u"; |
|
364 |
test "(i $+ j $+ #12 $+ k) $- #15 = y"; |
|
365 |
test "(i $+ j $+ #12 $+ k) $- #5 = y"; |
|
366 |
||
367 |
test "y $- b $< b"; |
|
368 |
test "y $- (#3 $* b $+ c) $< b $- #2 $* c"; |
|
369 |
||
370 |
test "(#2 $* x $- (u $* v) $+ y) $- v $* #3 $* u = w"; |
|
371 |
test "(#2 $* x $* u $* v $+ (u $* v) $* #4 $+ y) $- v $* u $* #4 = w"; |
|
372 |
test "(#2 $* x $* u $* v $+ (u $* v) $* #4 $+ y) $- v $* u = w"; |
|
373 |
test "u $* v $- (x $* u $* v $+ (u $* v) $* #4 $+ y) = w"; |
|
374 |
||
375 |
test "(i $+ j $+ #12 $+ k) = u $+ #15 $+ y"; |
|
376 |
test "(i $+ j $* #2 $+ #12 $+ k) = j $+ #5 $+ y"; |
|
377 |
||
378 |
test "#2 $* y $+ #3 $* z $+ #6 $* w $+ #2 $* y $+ #3 $* z $+ #2 $* u = #2 $* y' $+ #3 $* z' $+ #6 $* w' $+ #2 $* y' $+ #3 $* z' $+ u $+ vv"; |
|
379 |
||
380 |
test "a $+ $-(b$+c) $+ b = d"; |
|
381 |
test "a $+ $-(b$+c) $- b = d"; |
|
382 |
||
383 |
(*negative numerals*) |
|
384 |
test "(i $+ j $+ #-2 $+ k) $- (u $+ #5 $+ y) = zz"; |
|
385 |
test "(i $+ j $+ #-3 $+ k) $< u $+ #5 $+ y"; |
|
386 |
test "(i $+ j $+ #3 $+ k) $< u $+ #-6 $+ y"; |
|
387 |
test "(i $+ j $+ #-12 $+ k) $- #15 = y"; |
|
388 |
test "(i $+ j $+ #12 $+ k) $- #-15 = y"; |
|
389 |
test "(i $+ j $+ #-12 $+ k) $- #-15 = y"; |
|
390 |
||
391 |
(*Multiplying separated numerals*) |
|
392 |
Goal "#6 $* ($# x $* #2) = uu"; |
|
393 |
Goal "#4 $* ($# x $* $# x) $* (#2 $* $# x) = uu"; |
|
394 |
*) |
|
395 |