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