author | haftmann |
Thu, 12 Mar 2009 18:01:26 +0100 | |
changeset 30496 | 7cdcc9dd95cb |
parent 29269 | 5c25a2012975 |
child 30518 | 07b45c1aa788 |
permissions | -rw-r--r-- |
29269
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29039
diff
changeset
|
1 |
(* Title: HOL/Tools/nat_simprocs.ML |
23164 | 2 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
3 |
||
4 |
Simprocs for nat numerals. |
|
5 |
*) |
|
6 |
||
7 |
structure Nat_Numeral_Simprocs = |
|
8 |
struct |
|
9 |
||
10 |
(*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
|
11 |
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 | 12 |
val numeral_sym_ss = HOL_ss addsimps numeral_syms; |
13 |
||
14 |
fun rename_numerals th = |
|
15 |
simplify numeral_sym_ss (Thm.transfer (the_context ()) th); |
|
16 |
||
17 |
(*Utilities*) |
|
18 |
||
19 |
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
|
20 |
fun dest_number t = Int.max (0, snd (HOLogic.dest_number t)); |
23164 | 21 |
|
22 |
fun find_first_numeral past (t::terms) = |
|
23 |
((dest_number t, t, rev past @ terms) |
|
24 |
handle TERM _ => find_first_numeral (t::past) terms) |
|
25 |
| find_first_numeral past [] = raise TERM("find_first_numeral", []); |
|
26 |
||
27 |
val zero = mk_number 0; |
|
28 |
val mk_plus = HOLogic.mk_binop @{const_name HOL.plus}; |
|
29 |
||
30 |
(*Thus mk_sum[t] yields t+0; longer sums don't have a trailing zero*) |
|
31 |
fun mk_sum [] = zero |
|
32 |
| mk_sum [t,u] = mk_plus (t, u) |
|
33 |
| mk_sum (t :: ts) = mk_plus (t, mk_sum ts); |
|
34 |
||
35 |
(*this version ALWAYS includes a trailing zero*) |
|
36 |
fun long_mk_sum [] = HOLogic.zero |
|
37 |
| long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts); |
|
38 |
||
39 |
val dest_plus = HOLogic.dest_bin @{const_name HOL.plus} HOLogic.natT; |
|
40 |
||
41 |
||
42 |
(** Other simproc items **) |
|
43 |
||
44 |
val trans_tac = Int_Numeral_Simprocs.trans_tac; |
|
45 |
||
46 |
val bin_simps = |
|
23471 | 47 |
[@{thm nat_numeral_0_eq_0} RS sym, @{thm nat_numeral_1_eq_1} RS sym, |
48 |
@{thm add_nat_number_of}, @{thm nat_number_of_add_left}, |
|
49 |
@{thm diff_nat_number_of}, @{thm le_number_of_eq_not_less}, |
|
50 |
@{thm mult_nat_number_of}, @{thm nat_number_of_mult_left}, |
|
51 |
@{thm less_nat_number_of}, |
|
52 |
@{thm Let_number_of}, @{thm nat_number_of}] @ |
|
29039 | 53 |
@{thms arith_simps} @ @{thms rel_simps} @ @{thms neg_simps}; |
23164 | 54 |
|
55 |
||
56 |
(*** CancelNumerals simprocs ***) |
|
57 |
||
58 |
val one = mk_number 1; |
|
59 |
val mk_times = HOLogic.mk_binop @{const_name HOL.times}; |
|
60 |
||
61 |
fun mk_prod [] = one |
|
62 |
| mk_prod [t] = t |
|
63 |
| mk_prod (t :: ts) = if t = one then mk_prod ts |
|
64 |
else mk_times (t, mk_prod ts); |
|
65 |
||
66 |
val dest_times = HOLogic.dest_bin @{const_name HOL.times} HOLogic.natT; |
|
67 |
||
68 |
fun dest_prod t = |
|
69 |
let val (t,u) = dest_times t |
|
70 |
in dest_prod t @ dest_prod u end |
|
71 |
handle TERM _ => [t]; |
|
72 |
||
73 |
(*DON'T do the obvious simplifications; that would create special cases*) |
|
74 |
fun mk_coeff (k,t) = mk_times (mk_number k, t); |
|
75 |
||
76 |
(*Express t as a product of (possibly) a numeral with other factors, sorted*) |
|
77 |
fun dest_coeff t = |
|
29269
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29039
diff
changeset
|
78 |
let val ts = sort TermOrd.term_ord (dest_prod t) |
23164 | 79 |
val (n, _, ts') = find_first_numeral [] ts |
80 |
handle TERM _ => (1, one, ts) |
|
81 |
in (n, mk_prod ts') end; |
|
82 |
||
83 |
(*Find first coefficient-term THAT MATCHES u*) |
|
84 |
fun find_first_coeff past u [] = raise TERM("find_first_coeff", []) |
|
85 |
| find_first_coeff past u (t::terms) = |
|
86 |
let val (n,u') = dest_coeff t |
|
87 |
in if u aconv u' then (n, rev past @ terms) |
|
88 |
else find_first_coeff (t::past) u terms |
|
89 |
end |
|
90 |
handle TERM _ => find_first_coeff (t::past) u terms; |
|
91 |
||
92 |
||
93 |
(*Split up a sum into the list of its constituent terms, on the way removing any |
|
94 |
Sucs and counting them.*) |
|
95 |
fun dest_Suc_sum (Const ("Suc", _) $ t, (k,ts)) = dest_Suc_sum (t, (k+1,ts)) |
|
96 |
| dest_Suc_sum (t, (k,ts)) = |
|
97 |
let val (t1,t2) = dest_plus t |
|
98 |
in dest_Suc_sum (t1, dest_Suc_sum (t2, (k,ts))) end |
|
99 |
handle TERM _ => (k, t::ts); |
|
100 |
||
101 |
(*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
|
102 |
fun is_numeral (Const(@{const_name Int.number_of}, _) $ w) = true |
23164 | 103 |
| is_numeral _ = false; |
104 |
||
105 |
fun prod_has_numeral t = exists is_numeral (dest_prod t); |
|
106 |
||
107 |
(*The Sucs found in the term are converted to a binary numeral. If relaxed is false, |
|
108 |
an exception is raised unless the original expression contains at least one |
|
109 |
numeral in a coefficient position. This prevents nat_combine_numerals from |
|
110 |
introducing numerals to goals.*) |
|
111 |
fun dest_Sucs_sum relaxed t = |
|
112 |
let val (k,ts) = dest_Suc_sum (t,(0,[])) |
|
113 |
in |
|
114 |
if relaxed orelse exists prod_has_numeral ts then |
|
115 |
if k=0 then ts |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24431
diff
changeset
|
116 |
else mk_number k :: ts |
23164 | 117 |
else raise TERM("Nat_Numeral_Simprocs.dest_Sucs_sum", [t]) |
118 |
end; |
|
119 |
||
120 |
||
121 |
(*Simplify 1*n and n*1 to n*) |
|
23881 | 122 |
val add_0s = map rename_numerals [@{thm add_0}, @{thm add_0_right}]; |
23164 | 123 |
val mult_1s = map rename_numerals [@{thm nat_mult_1}, @{thm nat_mult_1_right}]; |
124 |
||
125 |
(*Final simplification: cancel + and *; replace Numeral0 by 0 and Numeral1 by 1*) |
|
126 |
||
127 |
(*And these help the simproc return False when appropriate, which helps |
|
128 |
the arith prover.*) |
|
23881 | 129 |
val contra_rules = [@{thm add_Suc}, @{thm add_Suc_right}, @{thm Zero_not_Suc}, |
130 |
@{thm Suc_not_Zero}, @{thm le_0_eq}]; |
|
23164 | 131 |
|
132 |
val simplify_meta_eq = |
|
133 |
Int_Numeral_Simprocs.simplify_meta_eq |
|
23471 | 134 |
([@{thm nat_numeral_0_eq_0}, @{thm numeral_1_eq_Suc_0}, @{thm add_0}, @{thm add_0_right}, |
135 |
@{thm mult_0}, @{thm mult_0_right}, @{thm mult_1}, @{thm mult_1_right}] @ contra_rules); |
|
23164 | 136 |
|
137 |
||
138 |
(*Like HOL_ss but with an ordering that brings numerals to the front |
|
139 |
under AC-rewriting.*) |
|
140 |
val num_ss = Int_Numeral_Simprocs.num_ss; |
|
141 |
||
142 |
(*** Applying CancelNumeralsFun ***) |
|
143 |
||
144 |
structure CancelNumeralsCommon = |
|
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 [] |
|
151 |
val trans_tac = fn _ => trans_tac |
|
152 |
||
153 |
val norm_ss1 = num_ss addsimps numeral_syms @ add_0s @ mult_1s @ |
|
23881 | 154 |
[@{thm Suc_eq_add_numeral_1_left}] @ @{thms add_ac} |
155 |
val norm_ss2 = 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 |
|
163 |
end; |
|
164 |
||
165 |
||
166 |
structure EqCancelNumerals = CancelNumeralsFun |
|
167 |
(open CancelNumeralsCommon |
|
30496
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents:
29269
diff
changeset
|
168 |
val prove_conv = Arith_Data.prove_conv |
23164 | 169 |
val mk_bal = HOLogic.mk_eq |
170 |
val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT |
|
23471 | 171 |
val bal_add1 = @{thm nat_eq_add_iff1} RS trans |
172 |
val bal_add2 = @{thm nat_eq_add_iff2} RS trans |
|
23164 | 173 |
); |
174 |
||
175 |
structure LessCancelNumerals = CancelNumeralsFun |
|
176 |
(open CancelNumeralsCommon |
|
30496
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents:
29269
diff
changeset
|
177 |
val prove_conv = Arith_Data.prove_conv |
23881 | 178 |
val mk_bal = HOLogic.mk_binrel @{const_name HOL.less} |
179 |
val dest_bal = HOLogic.dest_bin @{const_name HOL.less} HOLogic.natT |
|
23471 | 180 |
val bal_add1 = @{thm nat_less_add_iff1} RS trans |
181 |
val bal_add2 = @{thm nat_less_add_iff2} RS trans |
|
23164 | 182 |
); |
183 |
||
184 |
structure LeCancelNumerals = CancelNumeralsFun |
|
185 |
(open CancelNumeralsCommon |
|
30496
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents:
29269
diff
changeset
|
186 |
val prove_conv = Arith_Data.prove_conv |
23881 | 187 |
val mk_bal = HOLogic.mk_binrel @{const_name HOL.less_eq} |
188 |
val dest_bal = HOLogic.dest_bin @{const_name HOL.less_eq} HOLogic.natT |
|
23471 | 189 |
val bal_add1 = @{thm nat_le_add_iff1} RS trans |
190 |
val bal_add2 = @{thm nat_le_add_iff2} RS trans |
|
23164 | 191 |
); |
192 |
||
193 |
structure DiffCancelNumerals = CancelNumeralsFun |
|
194 |
(open CancelNumeralsCommon |
|
30496
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents:
29269
diff
changeset
|
195 |
val prove_conv = Arith_Data.prove_conv |
23164 | 196 |
val mk_bal = HOLogic.mk_binop @{const_name HOL.minus} |
197 |
val dest_bal = HOLogic.dest_bin @{const_name HOL.minus} HOLogic.natT |
|
23471 | 198 |
val bal_add1 = @{thm nat_diff_add_eq1} RS trans |
199 |
val bal_add2 = @{thm nat_diff_add_eq2} RS trans |
|
23164 | 200 |
); |
201 |
||
202 |
||
203 |
val cancel_numerals = |
|
30496
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents:
29269
diff
changeset
|
204 |
map Arith_Data.prep_simproc |
23164 | 205 |
[("nateq_cancel_numerals", |
206 |
["(l::nat) + m = n", "(l::nat) = m + n", |
|
207 |
"(l::nat) * m = n", "(l::nat) = m * n", |
|
208 |
"Suc m = n", "m = Suc n"], |
|
209 |
K EqCancelNumerals.proc), |
|
210 |
("natless_cancel_numerals", |
|
211 |
["(l::nat) + m < n", "(l::nat) < m + n", |
|
212 |
"(l::nat) * m < n", "(l::nat) < m * n", |
|
213 |
"Suc m < n", "m < Suc n"], |
|
214 |
K LessCancelNumerals.proc), |
|
215 |
("natle_cancel_numerals", |
|
216 |
["(l::nat) + m <= n", "(l::nat) <= m + n", |
|
217 |
"(l::nat) * m <= n", "(l::nat) <= m * n", |
|
218 |
"Suc m <= n", "m <= Suc n"], |
|
219 |
K LeCancelNumerals.proc), |
|
220 |
("natdiff_cancel_numerals", |
|
221 |
["((l::nat) + m) - n", "(l::nat) - (m + n)", |
|
222 |
"(l::nat) * m - n", "(l::nat) - m * n", |
|
223 |
"Suc m - n", "m - Suc n"], |
|
224 |
K DiffCancelNumerals.proc)]; |
|
225 |
||
226 |
||
227 |
(*** Applying CombineNumeralsFun ***) |
|
228 |
||
229 |
structure CombineNumeralsData = |
|
230 |
struct |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24431
diff
changeset
|
231 |
type coeff = int |
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24431
diff
changeset
|
232 |
val iszero = (fn x => x = 0) |
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24431
diff
changeset
|
233 |
val add = op + |
23164 | 234 |
val mk_sum = (fn T:typ => long_mk_sum) (*to work for 2*x + 3*x *) |
235 |
val dest_sum = dest_Sucs_sum false |
|
236 |
val mk_coeff = mk_coeff |
|
237 |
val dest_coeff = dest_coeff |
|
23471 | 238 |
val left_distrib = @{thm left_add_mult_distrib} RS trans |
30496
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents:
29269
diff
changeset
|
239 |
val prove_conv = Arith_Data.prove_conv_nohyps |
23164 | 240 |
val trans_tac = fn _ => trans_tac |
241 |
||
23881 | 242 |
val norm_ss1 = num_ss addsimps numeral_syms @ add_0s @ mult_1s @ [@{thm Suc_eq_add_numeral_1}] @ @{thms add_ac} |
243 |
val norm_ss2 = num_ss addsimps bin_simps @ @{thms add_ac} @ @{thms mult_ac} |
|
23164 | 244 |
fun norm_tac ss = |
245 |
ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1)) |
|
246 |
THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2)) |
|
247 |
||
248 |
val numeral_simp_ss = HOL_ss addsimps add_0s @ bin_simps; |
|
249 |
fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss)) |
|
250 |
val simplify_meta_eq = simplify_meta_eq |
|
251 |
end; |
|
252 |
||
253 |
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData); |
|
254 |
||
255 |
val combine_numerals = |
|
30496
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents:
29269
diff
changeset
|
256 |
Arith_Data.prep_simproc ("nat_combine_numerals", ["(i::nat) + j", "Suc (i + j)"], K CombineNumerals.proc); |
23164 | 257 |
|
258 |
||
259 |
(*** Applying CancelNumeralFactorFun ***) |
|
260 |
||
261 |
structure CancelNumeralFactorCommon = |
|
262 |
struct |
|
263 |
val mk_coeff = mk_coeff |
|
264 |
val dest_coeff = dest_coeff |
|
265 |
val trans_tac = fn _ => trans_tac |
|
266 |
||
267 |
val norm_ss1 = num_ss addsimps |
|
23881 | 268 |
numeral_syms @ add_0s @ mult_1s @ [@{thm Suc_eq_add_numeral_1_left}] @ @{thms add_ac} |
269 |
val norm_ss2 = num_ss addsimps bin_simps @ @{thms add_ac} @ @{thms mult_ac} |
|
23164 | 270 |
fun norm_tac ss = |
271 |
ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1)) |
|
272 |
THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2)) |
|
273 |
||
274 |
val numeral_simp_ss = HOL_ss addsimps bin_simps |
|
275 |
fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss)) |
|
276 |
val simplify_meta_eq = simplify_meta_eq |
|
277 |
end |
|
278 |
||
279 |
structure DivCancelNumeralFactor = CancelNumeralFactorFun |
|
280 |
(open CancelNumeralFactorCommon |
|
30496
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents:
29269
diff
changeset
|
281 |
val prove_conv = Arith_Data.prove_conv |
23164 | 282 |
val mk_bal = HOLogic.mk_binop @{const_name Divides.div} |
283 |
val dest_bal = HOLogic.dest_bin @{const_name Divides.div} HOLogic.natT |
|
23471 | 284 |
val cancel = @{thm nat_mult_div_cancel1} RS trans |
23164 | 285 |
val neg_exchanges = false |
286 |
) |
|
287 |
||
23969 | 288 |
structure DvdCancelNumeralFactor = CancelNumeralFactorFun |
289 |
(open CancelNumeralFactorCommon |
|
30496
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents:
29269
diff
changeset
|
290 |
val prove_conv = Arith_Data.prove_conv |
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
25919
diff
changeset
|
291 |
val mk_bal = HOLogic.mk_binrel @{const_name Ring_and_Field.dvd} |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
25919
diff
changeset
|
292 |
val dest_bal = HOLogic.dest_bin @{const_name Ring_and_Field.dvd} HOLogic.natT |
23969 | 293 |
val cancel = @{thm nat_mult_dvd_cancel1} RS trans |
294 |
val neg_exchanges = false |
|
295 |
) |
|
296 |
||
23164 | 297 |
structure EqCancelNumeralFactor = CancelNumeralFactorFun |
298 |
(open CancelNumeralFactorCommon |
|
30496
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents:
29269
diff
changeset
|
299 |
val prove_conv = Arith_Data.prove_conv |
23164 | 300 |
val mk_bal = HOLogic.mk_eq |
301 |
val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT |
|
23471 | 302 |
val cancel = @{thm nat_mult_eq_cancel1} RS trans |
23164 | 303 |
val neg_exchanges = false |
304 |
) |
|
305 |
||
306 |
structure LessCancelNumeralFactor = CancelNumeralFactorFun |
|
307 |
(open CancelNumeralFactorCommon |
|
30496
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents:
29269
diff
changeset
|
308 |
val prove_conv = Arith_Data.prove_conv |
23881 | 309 |
val mk_bal = HOLogic.mk_binrel @{const_name HOL.less} |
310 |
val dest_bal = HOLogic.dest_bin @{const_name HOL.less} HOLogic.natT |
|
23471 | 311 |
val cancel = @{thm nat_mult_less_cancel1} RS trans |
23164 | 312 |
val neg_exchanges = true |
313 |
) |
|
314 |
||
315 |
structure LeCancelNumeralFactor = CancelNumeralFactorFun |
|
316 |
(open CancelNumeralFactorCommon |
|
30496
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents:
29269
diff
changeset
|
317 |
val prove_conv = Arith_Data.prove_conv |
23881 | 318 |
val mk_bal = HOLogic.mk_binrel @{const_name HOL.less_eq} |
319 |
val dest_bal = HOLogic.dest_bin @{const_name HOL.less_eq} HOLogic.natT |
|
23471 | 320 |
val cancel = @{thm nat_mult_le_cancel1} RS trans |
23164 | 321 |
val neg_exchanges = true |
322 |
) |
|
323 |
||
324 |
val cancel_numeral_factors = |
|
30496
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents:
29269
diff
changeset
|
325 |
map Arith_Data.prep_simproc |
23164 | 326 |
[("nateq_cancel_numeral_factors", |
327 |
["(l::nat) * m = n", "(l::nat) = m * n"], |
|
328 |
K EqCancelNumeralFactor.proc), |
|
329 |
("natless_cancel_numeral_factors", |
|
330 |
["(l::nat) * m < n", "(l::nat) < m * n"], |
|
331 |
K LessCancelNumeralFactor.proc), |
|
332 |
("natle_cancel_numeral_factors", |
|
333 |
["(l::nat) * m <= n", "(l::nat) <= m * n"], |
|
334 |
K LeCancelNumeralFactor.proc), |
|
335 |
("natdiv_cancel_numeral_factors", |
|
336 |
["((l::nat) * m) div n", "(l::nat) div (m * n)"], |
|
23969 | 337 |
K DivCancelNumeralFactor.proc), |
338 |
("natdvd_cancel_numeral_factors", |
|
339 |
["((l::nat) * m) dvd n", "(l::nat) dvd (m * n)"], |
|
340 |
K DvdCancelNumeralFactor.proc)]; |
|
23164 | 341 |
|
342 |
||
343 |
||
344 |
(*** Applying ExtractCommonTermFun ***) |
|
345 |
||
346 |
(*this version ALWAYS includes a trailing one*) |
|
347 |
fun long_mk_prod [] = one |
|
348 |
| long_mk_prod (t :: ts) = mk_times (t, mk_prod ts); |
|
349 |
||
350 |
(*Find first term that matches u*) |
|
351 |
fun find_first_t past u [] = raise TERM("find_first_t", []) |
|
352 |
| find_first_t past u (t::terms) = |
|
353 |
if u aconv t then (rev past @ terms) |
|
354 |
else find_first_t (t::past) u terms |
|
355 |
handle TERM _ => find_first_t (t::past) u terms; |
|
356 |
||
357 |
(** Final simplification for the CancelFactor simprocs **) |
|
358 |
val simplify_one = Int_Numeral_Simprocs.simplify_meta_eq |
|
359 |
[@{thm mult_1_left}, @{thm mult_1_right}, @{thm div_1}, @{thm numeral_1_eq_Suc_0}]; |
|
360 |
||
361 |
fun cancel_simplify_meta_eq cancel_th ss th = |
|
362 |
simplify_one ss (([th, cancel_th]) MRS trans); |
|
363 |
||
364 |
structure CancelFactorCommon = |
|
365 |
struct |
|
366 |
val mk_sum = (fn T:typ => long_mk_prod) |
|
367 |
val dest_sum = dest_prod |
|
368 |
val mk_coeff = mk_coeff |
|
369 |
val dest_coeff = dest_coeff |
|
370 |
val find_first = find_first_t [] |
|
371 |
val trans_tac = fn _ => trans_tac |
|
23881 | 372 |
val norm_ss = HOL_ss addsimps mult_1s @ @{thms mult_ac} |
23164 | 373 |
fun norm_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss)) |
374 |
end; |
|
375 |
||
376 |
structure EqCancelFactor = ExtractCommonTermFun |
|
377 |
(open CancelFactorCommon |
|
30496
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents:
29269
diff
changeset
|
378 |
val prove_conv = Arith_Data.prove_conv |
23164 | 379 |
val mk_bal = HOLogic.mk_eq |
380 |
val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT |
|
23471 | 381 |
val simplify_meta_eq = cancel_simplify_meta_eq @{thm nat_mult_eq_cancel_disj} |
23164 | 382 |
); |
383 |
||
384 |
structure LessCancelFactor = ExtractCommonTermFun |
|
385 |
(open CancelFactorCommon |
|
30496
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents:
29269
diff
changeset
|
386 |
val prove_conv = Arith_Data.prove_conv |
23881 | 387 |
val mk_bal = HOLogic.mk_binrel @{const_name HOL.less} |
388 |
val dest_bal = HOLogic.dest_bin @{const_name HOL.less} HOLogic.natT |
|
23471 | 389 |
val simplify_meta_eq = cancel_simplify_meta_eq @{thm nat_mult_less_cancel_disj} |
23164 | 390 |
); |
391 |
||
392 |
structure LeCancelFactor = ExtractCommonTermFun |
|
393 |
(open CancelFactorCommon |
|
30496
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents:
29269
diff
changeset
|
394 |
val prove_conv = Arith_Data.prove_conv |
23881 | 395 |
val mk_bal = HOLogic.mk_binrel @{const_name HOL.less_eq} |
396 |
val dest_bal = HOLogic.dest_bin @{const_name HOL.less_eq} HOLogic.natT |
|
23471 | 397 |
val simplify_meta_eq = cancel_simplify_meta_eq @{thm nat_mult_le_cancel_disj} |
23164 | 398 |
); |
399 |
||
400 |
structure DivideCancelFactor = ExtractCommonTermFun |
|
401 |
(open CancelFactorCommon |
|
30496
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents:
29269
diff
changeset
|
402 |
val prove_conv = Arith_Data.prove_conv |
23164 | 403 |
val mk_bal = HOLogic.mk_binop @{const_name Divides.div} |
404 |
val dest_bal = HOLogic.dest_bin @{const_name Divides.div} HOLogic.natT |
|
23471 | 405 |
val simplify_meta_eq = cancel_simplify_meta_eq @{thm nat_mult_div_cancel_disj} |
23164 | 406 |
); |
407 |
||
23969 | 408 |
structure DvdCancelFactor = ExtractCommonTermFun |
409 |
(open CancelFactorCommon |
|
30496
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents:
29269
diff
changeset
|
410 |
val prove_conv = Arith_Data.prove_conv |
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
25919
diff
changeset
|
411 |
val mk_bal = HOLogic.mk_binrel @{const_name Ring_and_Field.dvd} |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
25919
diff
changeset
|
412 |
val dest_bal = HOLogic.dest_bin @{const_name Ring_and_Field.dvd} HOLogic.natT |
23969 | 413 |
val simplify_meta_eq = cancel_simplify_meta_eq @{thm nat_mult_dvd_cancel_disj} |
414 |
); |
|
415 |
||
23164 | 416 |
val cancel_factor = |
30496
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents:
29269
diff
changeset
|
417 |
map Arith_Data.prep_simproc |
23164 | 418 |
[("nat_eq_cancel_factor", |
419 |
["(l::nat) * m = n", "(l::nat) = m * n"], |
|
420 |
K EqCancelFactor.proc), |
|
421 |
("nat_less_cancel_factor", |
|
422 |
["(l::nat) * m < n", "(l::nat) < m * n"], |
|
423 |
K LessCancelFactor.proc), |
|
424 |
("nat_le_cancel_factor", |
|
425 |
["(l::nat) * m <= n", "(l::nat) <= m * n"], |
|
426 |
K LeCancelFactor.proc), |
|
427 |
("nat_divide_cancel_factor", |
|
428 |
["((l::nat) * m) div n", "(l::nat) div (m * n)"], |
|
23969 | 429 |
K DivideCancelFactor.proc), |
430 |
("nat_dvd_cancel_factor", |
|
431 |
["((l::nat) * m) dvd n", "(l::nat) dvd (m * n)"], |
|
432 |
K DvdCancelFactor.proc)]; |
|
23164 | 433 |
|
434 |
end; |
|
435 |
||
436 |
||
437 |
Addsimprocs Nat_Numeral_Simprocs.cancel_numerals; |
|
438 |
Addsimprocs [Nat_Numeral_Simprocs.combine_numerals]; |
|
439 |
Addsimprocs Nat_Numeral_Simprocs.cancel_numeral_factors; |
|
440 |
Addsimprocs Nat_Numeral_Simprocs.cancel_factor; |
|
441 |
||
442 |
||
443 |
(*examples: |
|
444 |
print_depth 22; |
|
445 |
set timing; |
|
446 |
set trace_simp; |
|
447 |
fun test s = (Goal s; by (Simp_tac 1)); |
|
448 |
||
449 |
(*cancel_numerals*) |
|
450 |
test "l +( 2) + (2) + 2 + (l + 2) + (oo + 2) = (uu::nat)"; |
|
451 |
test "(2*length xs < 2*length xs + j)"; |
|
452 |
test "(2*length xs < length xs * 2 + j)"; |
|
453 |
test "2*u = (u::nat)"; |
|
454 |
test "2*u = Suc (u)"; |
|
455 |
test "(i + j + 12 + (k::nat)) - 15 = y"; |
|
456 |
test "(i + j + 12 + (k::nat)) - 5 = y"; |
|
457 |
test "Suc u - 2 = y"; |
|
458 |
test "Suc (Suc (Suc u)) - 2 = y"; |
|
459 |
test "(i + j + 2 + (k::nat)) - 1 = y"; |
|
460 |
test "(i + j + 1 + (k::nat)) - 2 = y"; |
|
461 |
||
462 |
test "(2*x + (u*v) + y) - v*3*u = (w::nat)"; |
|
463 |
test "(2*x*u*v + 5 + (u*v)*4 + y) - v*u*4 = (w::nat)"; |
|
464 |
test "(2*x*u*v + (u*v)*4 + y) - v*u = (w::nat)"; |
|
465 |
test "Suc (Suc (2*x*u*v + u*4 + y)) - u = w"; |
|
466 |
test "Suc ((u*v)*4) - v*3*u = w"; |
|
467 |
test "Suc (Suc ((u*v)*3)) - v*3*u = w"; |
|
468 |
||
469 |
test "(i + j + 12 + (k::nat)) = u + 15 + y"; |
|
470 |
test "(i + j + 32 + (k::nat)) - (u + 15 + y) = zz"; |
|
471 |
test "(i + j + 12 + (k::nat)) = u + 5 + y"; |
|
472 |
(*Suc*) |
|
473 |
test "(i + j + 12 + k) = Suc (u + y)"; |
|
474 |
test "Suc (Suc (Suc (Suc (Suc (u + y))))) <= ((i + j) + 41 + k)"; |
|
475 |
test "(i + j + 5 + k) < Suc (Suc (Suc (Suc (Suc (u + y)))))"; |
|
476 |
test "Suc (Suc (Suc (Suc (Suc (u + y))))) - 5 = v"; |
|
477 |
test "(i + j + 5 + k) = Suc (Suc (Suc (Suc (Suc (Suc (Suc (u + y)))))))"; |
|
478 |
test "2*y + 3*z + 2*u = Suc (u)"; |
|
479 |
test "2*y + 3*z + 6*w + 2*y + 3*z + 2*u = Suc (u)"; |
|
480 |
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)"; |
|
481 |
test "6 + 2*y + 3*z + 4*u = Suc (vv + 2*u + z)"; |
|
482 |
test "(2*n*m) < (3*(m*n)) + (u::nat)"; |
|
483 |
||
484 |
test "(Suc (Suc (Suc (Suc (Suc (Suc (case length (f c) of 0 => 0 | Suc k => k)))))) <= Suc 0)"; |
|
485 |
||
486 |
test "Suc (Suc (Suc (Suc (Suc (Suc (length l1 + length l2)))))) <= length l1"; |
|
487 |
||
488 |
test "( (Suc (Suc (Suc (Suc (Suc (length (compT P E A ST mxr e) + length l3)))))) <= length (compT P E A ST mxr e))"; |
|
489 |
||
490 |
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))"; |
|
491 |
||
492 |
||
493 |
(*negative numerals: FAIL*) |
|
494 |
test "(i + j + -23 + (k::nat)) < u + 15 + y"; |
|
495 |
test "(i + j + 3 + (k::nat)) < u + -15 + y"; |
|
496 |
test "(i + j + -12 + (k::nat)) - 15 = y"; |
|
497 |
test "(i + j + 12 + (k::nat)) - -15 = y"; |
|
498 |
test "(i + j + -12 + (k::nat)) - -15 = y"; |
|
499 |
||
500 |
(*combine_numerals*) |
|
501 |
test "k + 3*k = (u::nat)"; |
|
502 |
test "Suc (i + 3) = u"; |
|
503 |
test "Suc (i + j + 3 + k) = u"; |
|
504 |
test "k + j + 3*k + j = (u::nat)"; |
|
505 |
test "Suc (j*i + i + k + 5 + 3*k + i*j*4) = (u::nat)"; |
|
506 |
test "(2*n*m) + (3*(m*n)) = (u::nat)"; |
|
507 |
(*negative numerals: FAIL*) |
|
508 |
test "Suc (i + j + -3 + k) = u"; |
|
509 |
||
510 |
(*cancel_numeral_factors*) |
|
511 |
test "9*x = 12 * (y::nat)"; |
|
512 |
test "(9*x) div (12 * (y::nat)) = z"; |
|
513 |
test "9*x < 12 * (y::nat)"; |
|
514 |
test "9*x <= 12 * (y::nat)"; |
|
515 |
||
516 |
(*cancel_factor*) |
|
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 < k*(y::nat)"; |
|
523 |
test "k < k*(y::nat)"; |
|
524 |
test "a*(b*c) < (b::nat)"; |
|
525 |
test "a*(b*c) < d*(b::nat)*(x*a)"; |
|
526 |
||
527 |
test "x*k <= k*(y::nat)"; |
|
528 |
test "k <= k*(y::nat)"; |
|
529 |
test "a*(b*c) <= (b::nat)"; |
|
530 |
test "a*(b*c) <= d*(b::nat)*(x*a)"; |
|
531 |
||
532 |
test "(x*k) div (k*(y::nat)) = (uu::nat)"; |
|
533 |
test "(k) div (k*(y::nat)) = (uu::nat)"; |
|
534 |
test "(a*(b*c)) div ((b::nat)) = (uu::nat)"; |
|
535 |
test "(a*(b*c)) div (d*(b::nat)*(x*a)) = (uu::nat)"; |
|
536 |
*) |
|
537 |
||
538 |
||
539 |
(*** Prepare linear arithmetic for nat numerals ***) |
|
540 |
||
541 |
local |
|
542 |
||
543 |
(* reduce contradictory <= to False *) |
|
24431
02d29baa42ff
tuned linear arith (once again) with ring_distribs
nipkow
parents:
24093
diff
changeset
|
544 |
val add_rules = @{thms ring_distribs} @ |
23471 | 545 |
[@{thm Let_number_of}, @{thm Let_0}, @{thm Let_1}, @{thm nat_0}, @{thm nat_1}, |
546 |
@{thm add_nat_number_of}, @{thm diff_nat_number_of}, @{thm mult_nat_number_of}, |
|
547 |
@{thm eq_nat_number_of}, @{thm less_nat_number_of}, @{thm le_number_of_eq_not_less}, |
|
548 |
@{thm le_Suc_number_of}, @{thm le_number_of_Suc}, |
|
549 |
@{thm less_Suc_number_of}, @{thm less_number_of_Suc}, |
|
550 |
@{thm Suc_eq_number_of}, @{thm eq_number_of_Suc}, |
|
551 |
@{thm mult_Suc}, @{thm mult_Suc_right}, |
|
24431
02d29baa42ff
tuned linear arith (once again) with ring_distribs
nipkow
parents:
24093
diff
changeset
|
552 |
@{thm add_Suc}, @{thm add_Suc_right}, |
23471 | 553 |
@{thm eq_number_of_0}, @{thm eq_0_number_of}, @{thm less_0_number_of}, |
554 |
@{thm of_int_number_of_eq}, @{thm of_nat_number_of_eq}, @{thm nat_number_of}, @{thm if_True}, @{thm if_False}]; |
|
23164 | 555 |
|
24431
02d29baa42ff
tuned linear arith (once again) with ring_distribs
nipkow
parents:
24093
diff
changeset
|
556 |
(* Products are multiplied out during proof (re)construction via |
02d29baa42ff
tuned linear arith (once again) with ring_distribs
nipkow
parents:
24093
diff
changeset
|
557 |
ring_distribs. Ideally they should remain atomic. But that is |
02d29baa42ff
tuned linear arith (once again) with ring_distribs
nipkow
parents:
24093
diff
changeset
|
558 |
currently not possible because 1 is replaced by Suc 0, and then some |
02d29baa42ff
tuned linear arith (once again) with ring_distribs
nipkow
parents:
24093
diff
changeset
|
559 |
simprocs start to mess around with products like (n+1)*m. The rule |
02d29baa42ff
tuned linear arith (once again) with ring_distribs
nipkow
parents:
24093
diff
changeset
|
560 |
1 == Suc 0 is necessary for early parts of HOL where numerals and |
02d29baa42ff
tuned linear arith (once again) with ring_distribs
nipkow
parents:
24093
diff
changeset
|
561 |
simprocs are not yet available. But then it is difficult to remove |
02d29baa42ff
tuned linear arith (once again) with ring_distribs
nipkow
parents:
24093
diff
changeset
|
562 |
that rule later on, because it may find its way back in when theories |
02d29baa42ff
tuned linear arith (once again) with ring_distribs
nipkow
parents:
24093
diff
changeset
|
563 |
(and thus lin-arith simpsets) are merged. Otherwise one could turn the |
02d29baa42ff
tuned linear arith (once again) with ring_distribs
nipkow
parents:
24093
diff
changeset
|
564 |
rule around (Suc n = n+1) and see if that helps products being left |
02d29baa42ff
tuned linear arith (once again) with ring_distribs
nipkow
parents:
24093
diff
changeset
|
565 |
alone. *) |
02d29baa42ff
tuned linear arith (once again) with ring_distribs
nipkow
parents:
24093
diff
changeset
|
566 |
|
23164 | 567 |
val simprocs = Nat_Numeral_Simprocs.combine_numerals |
568 |
:: Nat_Numeral_Simprocs.cancel_numerals; |
|
569 |
||
570 |
in |
|
571 |
||
572 |
val nat_simprocs_setup = |
|
24093 | 573 |
LinArith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset} => |
23164 | 574 |
{add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms, |
575 |
inj_thms = inj_thms, lessD = lessD, neqE = neqE, |
|
576 |
simpset = simpset addsimps add_rules |
|
577 |
addsimprocs simprocs}); |
|
578 |
||
579 |
end; |