author | paulson |
Fri, 12 Oct 2007 10:24:49 +0200 | |
changeset 24998 | a339b33adfaf |
parent 24893 | b8ef7afe3a6b |
child 26287 | df8e5362cff9 |
permissions | -rw-r--r-- |
9548 | 1 |
(* Title: ZF/arith_data.ML |
2 |
ID: $Id$ |
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
4 |
Copyright 2000 University of Cambridge |
|
5 |
||
6 |
Arithmetic simplification: cancellation of common terms |
|
7 |
*) |
|
8 |
||
9 |
signature ARITH_DATA = |
|
10 |
sig |
|
9570
e16e168984e1
installation of cancellation simprocs for the integers
paulson
parents:
9548
diff
changeset
|
11 |
(*the main outcome*) |
9548 | 12 |
val nat_cancel: simproc list |
9570
e16e168984e1
installation of cancellation simprocs for the integers
paulson
parents:
9548
diff
changeset
|
13 |
(*tools for use in similar applications*) |
e16e168984e1
installation of cancellation simprocs for the integers
paulson
parents:
9548
diff
changeset
|
14 |
val gen_trans_tac: thm -> thm option -> tactic |
20113 | 15 |
val prove_conv: string -> tactic list -> Proof.context -> thm list -> term * term -> thm option |
16973 | 16 |
val simplify_meta_eq: thm list -> simpset -> thm -> thm |
9874 | 17 |
(*debugging*) |
18 |
structure EqCancelNumeralsData : CANCEL_NUMERALS_DATA |
|
19 |
structure LessCancelNumeralsData : CANCEL_NUMERALS_DATA |
|
20 |
structure DiffCancelNumeralsData : CANCEL_NUMERALS_DATA |
|
9548 | 21 |
end; |
22 |
||
9570
e16e168984e1
installation of cancellation simprocs for the integers
paulson
parents:
9548
diff
changeset
|
23 |
|
9548 | 24 |
structure ArithData: ARITH_DATA = |
25 |
struct |
|
26 |
||
27 |
val iT = Ind_Syntax.iT; |
|
28 |
||
29 |
val zero = Const("0", iT); |
|
30 |
val succ = Const("succ", iT --> iT); |
|
31 |
fun mk_succ t = succ $ t; |
|
32 |
val one = mk_succ zero; |
|
33 |
||
9570
e16e168984e1
installation of cancellation simprocs for the integers
paulson
parents:
9548
diff
changeset
|
34 |
val mk_plus = FOLogic.mk_binop "Arith.add"; |
9548 | 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 [] = zero |
|
43 |
| long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts); |
|
44 |
||
45 |
val dest_plus = FOLogic.dest_bin "Arith.add" iT; |
|
46 |
||
47 |
(* dest_sum *) |
|
48 |
||
49 |
fun dest_sum (Const("0",_)) = [] |
|
50 |
| dest_sum (Const("succ",_) $ t) = one :: dest_sum t |
|
51 |
| dest_sum (Const("Arith.add",_) $ t $ u) = dest_sum t @ dest_sum u |
|
52 |
| dest_sum tm = [tm]; |
|
53 |
||
54 |
(*Apply the given rewrite (if present) just once*) |
|
15531 | 55 |
fun gen_trans_tac th2 NONE = all_tac |
56 |
| gen_trans_tac th2 (SOME th) = ALLGOALS (rtac (th RS th2)); |
|
9548 | 57 |
|
58 |
(*Use <-> or = depending on the type of t*) |
|
59 |
fun mk_eq_iff(t,u) = |
|
60 |
if fastype_of t = iT then FOLogic.mk_eq(t,u) |
|
61 |
else FOLogic.mk_iff(t,u); |
|
62 |
||
9874 | 63 |
(*We remove equality assumptions because they confuse the simplifier and |
64 |
because only type-checking assumptions are necessary.*) |
|
13462 | 65 |
fun is_eq_thm th = |
9874 | 66 |
can FOLogic.dest_eq (FOLogic.dest_Trueprop (#prop (rep_thm th))); |
9649
89155e48fa53
simproc bug fix: only TYPING assumptions are given to the simplifier
paulson
parents:
9570
diff
changeset
|
67 |
|
9548 | 68 |
fun add_chyps chyps ct = Drule.list_implies (map cprop_of chyps, ct); |
69 |
||
20113 | 70 |
fun prove_conv name tacs ctxt prems (t,u) = |
15531 | 71 |
if t aconv u then NONE |
9548 | 72 |
else |
20113 | 73 |
let val prems' = List.filter (not o is_eq_thm) prems |
74 |
val goal = Logic.list_implies (map (#prop o Thm.rep_thm) prems', |
|
12134 | 75 |
FOLogic.mk_Trueprop (mk_eq_iff (t, u))); |
20113 | 76 |
in SOME (prems' MRS Goal.prove ctxt [] [] goal (K (EVERY tacs))) |
18678 | 77 |
handle ERROR msg => |
15531 | 78 |
(warning (msg ^ "\nCancellation failed: no typing information? (" ^ name ^ ")"); NONE) |
9548 | 79 |
end; |
80 |
||
13462 | 81 |
fun prep_simproc (name, pats, proc) = |
20342 | 82 |
Simplifier.simproc (the_context ()) name pats proc; |
9548 | 83 |
|
84 |
||
13462 | 85 |
(*** Use CancelNumerals simproc without binary numerals, |
9548 | 86 |
just for cancellation ***) |
87 |
||
9570
e16e168984e1
installation of cancellation simprocs for the integers
paulson
parents:
9548
diff
changeset
|
88 |
val mk_times = FOLogic.mk_binop "Arith.mult"; |
9548 | 89 |
|
90 |
fun mk_prod [] = one |
|
91 |
| mk_prod [t] = t |
|
92 |
| mk_prod (t :: ts) = if t = one then mk_prod ts |
|
93 |
else mk_times (t, mk_prod ts); |
|
94 |
||
95 |
val dest_times = FOLogic.dest_bin "Arith.mult" iT; |
|
96 |
||
97 |
fun dest_prod t = |
|
98 |
let val (t,u) = dest_times t |
|
99 |
in dest_prod t @ dest_prod u end |
|
100 |
handle TERM _ => [t]; |
|
101 |
||
102 |
(*Dummy version: the only arguments are 0 and 1*) |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
20342
diff
changeset
|
103 |
fun mk_coeff (0, t) = zero |
9548 | 104 |
| mk_coeff (1, t) = t |
105 |
| mk_coeff _ = raise TERM("mk_coeff", []); |
|
106 |
||
107 |
(*Dummy version: the "coefficient" is always 1. |
|
108 |
In the result, the factors are sorted terms*) |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
20342
diff
changeset
|
109 |
fun dest_coeff t = (1, mk_prod (sort Term.term_ord (dest_prod t))); |
9548 | 110 |
|
111 |
(*Find first coefficient-term THAT MATCHES u*) |
|
112 |
fun find_first_coeff past u [] = raise TERM("find_first_coeff", []) |
|
113 |
| find_first_coeff past u (t::terms) = |
|
114 |
let val (n,u') = dest_coeff t |
|
115 |
in if u aconv u' then (n, rev past @ terms) |
|
116 |
else find_first_coeff (t::past) u terms |
|
117 |
end |
|
118 |
handle TERM _ => find_first_coeff (t::past) u terms; |
|
119 |
||
120 |
||
121 |
(*Simplify #1*n and n*#1 to n*) |
|
24893 | 122 |
val add_0s = [@{thm add_0_natify}, @{thm add_0_right_natify}]; |
123 |
val add_succs = [@{thm add_succ}, @{thm add_succ_right}]; |
|
124 |
val mult_1s = [@{thm mult_1_natify}, @{thm mult_1_right_natify}]; |
|
125 |
val tc_rules = [@{thm natify_in_nat}, @{thm add_type}, @{thm diff_type}, @{thm mult_type}]; |
|
126 |
val natifys = [@{thm natify_0}, @{thm natify_ident}, @{thm add_natify1}, @{thm add_natify2}, |
|
127 |
@{thm diff_natify1}, @{thm diff_natify2}]; |
|
9548 | 128 |
|
129 |
(*Final simplification: cancel + and **) |
|
18328 | 130 |
fun simplify_meta_eq rules = |
131 |
let val ss0 = |
|
24893 | 132 |
FOL_ss addeqcongs [@{thm eq_cong2}, @{thm iff_cong2}] |
18328 | 133 |
delsimps iff_simps (*these could erase the whole rule!*) |
134 |
addsimps rules |
|
135 |
in fn ss => mk_meta_eq o simplify (Simplifier.inherit_context ss ss0) end; |
|
9548 | 136 |
|
24893 | 137 |
val final_rules = add_0s @ mult_1s @ [@{thm mult_0}, @{thm mult_0_right}]; |
9548 | 138 |
|
139 |
structure CancelNumeralsCommon = |
|
140 |
struct |
|
14387
e96d5c42c4b0
Polymorphic treatment of binary arithmetic using axclasses
paulson
parents:
13487
diff
changeset
|
141 |
val mk_sum = (fn T:typ => mk_sum) |
9548 | 142 |
val dest_sum = dest_sum |
143 |
val mk_coeff = mk_coeff |
|
144 |
val dest_coeff = dest_coeff |
|
145 |
val find_first_coeff = find_first_coeff [] |
|
18328 | 146 |
|
24893 | 147 |
val norm_ss1 = ZF_ss addsimps add_0s @ add_succs @ mult_1s @ @{thms add_ac} |
148 |
val norm_ss2 = ZF_ss addsimps add_0s @ mult_1s @ @{thms add_ac} @ |
|
149 |
@{thms mult_ac} @ tc_rules @ natifys |
|
16973 | 150 |
fun norm_tac ss = |
18328 | 151 |
ALLGOALS (asm_simp_tac (Simplifier.inherit_context ss norm_ss1)) |
152 |
THEN ALLGOALS (asm_simp_tac (Simplifier.inherit_context ss norm_ss2)) |
|
153 |
val numeral_simp_ss = ZF_ss addsimps add_0s @ tc_rules @ natifys |
|
16973 | 154 |
fun numeral_simp_tac ss = |
18328 | 155 |
ALLGOALS (asm_simp_tac (Simplifier.inherit_context ss numeral_simp_ss)) |
9548 | 156 |
val simplify_meta_eq = simplify_meta_eq final_rules |
157 |
end; |
|
158 |
||
9874 | 159 |
(** The functor argumnets are declared as separate structures |
160 |
so that they can be exported to ease debugging. **) |
|
9548 | 161 |
|
13462 | 162 |
structure EqCancelNumeralsData = |
9874 | 163 |
struct |
164 |
open CancelNumeralsCommon |
|
9548 | 165 |
val prove_conv = prove_conv "nateq_cancel_numerals" |
166 |
val mk_bal = FOLogic.mk_eq |
|
9649
89155e48fa53
simproc bug fix: only TYPING assumptions are given to the simplifier
paulson
parents:
9570
diff
changeset
|
167 |
val dest_bal = FOLogic.dest_eq |
24893 | 168 |
val bal_add1 = @{thm eq_add_iff} RS iff_trans |
169 |
val bal_add2 = @{thm eq_add_iff} RS iff_trans |
|
16973 | 170 |
fun trans_tac _ = gen_trans_tac iff_trans |
9874 | 171 |
end; |
172 |
||
173 |
structure EqCancelNumerals = CancelNumeralsFun(EqCancelNumeralsData); |
|
9548 | 174 |
|
13462 | 175 |
structure LessCancelNumeralsData = |
9874 | 176 |
struct |
177 |
open CancelNumeralsCommon |
|
9548 | 178 |
val prove_conv = prove_conv "natless_cancel_numerals" |
13155 | 179 |
val mk_bal = FOLogic.mk_binrel "Ordinal.lt" |
180 |
val dest_bal = FOLogic.dest_bin "Ordinal.lt" iT |
|
24893 | 181 |
val bal_add1 = @{thm less_add_iff} RS iff_trans |
182 |
val bal_add2 = @{thm less_add_iff} RS iff_trans |
|
16973 | 183 |
fun trans_tac _ = gen_trans_tac iff_trans |
9874 | 184 |
end; |
185 |
||
186 |
structure LessCancelNumerals = CancelNumeralsFun(LessCancelNumeralsData); |
|
9548 | 187 |
|
13462 | 188 |
structure DiffCancelNumeralsData = |
9874 | 189 |
struct |
190 |
open CancelNumeralsCommon |
|
9548 | 191 |
val prove_conv = prove_conv "natdiff_cancel_numerals" |
9570
e16e168984e1
installation of cancellation simprocs for the integers
paulson
parents:
9548
diff
changeset
|
192 |
val mk_bal = FOLogic.mk_binop "Arith.diff" |
9548 | 193 |
val dest_bal = FOLogic.dest_bin "Arith.diff" iT |
24893 | 194 |
val bal_add1 = @{thm diff_add_eq} RS trans |
195 |
val bal_add2 = @{thm diff_add_eq} RS trans |
|
16973 | 196 |
fun trans_tac _ = gen_trans_tac trans |
9874 | 197 |
end; |
198 |
||
199 |
structure DiffCancelNumerals = CancelNumeralsFun(DiffCancelNumeralsData); |
|
9548 | 200 |
|
201 |
||
202 |
val nat_cancel = |
|
13462 | 203 |
map prep_simproc |
204 |
[("nateq_cancel_numerals", |
|
205 |
["l #+ m = n", "l = m #+ n", |
|
206 |
"l #* m = n", "l = m #* n", |
|
207 |
"succ(m) = n", "m = succ(n)"], |
|
20044
92cc2f4c7335
simprocs: no theory argument -- use simpset context instead;
wenzelm
parents:
19250
diff
changeset
|
208 |
(K EqCancelNumerals.proc)), |
13462 | 209 |
("natless_cancel_numerals", |
210 |
["l #+ m < n", "l < m #+ n", |
|
211 |
"l #* m < n", "l < m #* n", |
|
212 |
"succ(m) < n", "m < succ(n)"], |
|
20044
92cc2f4c7335
simprocs: no theory argument -- use simpset context instead;
wenzelm
parents:
19250
diff
changeset
|
213 |
(K LessCancelNumerals.proc)), |
13462 | 214 |
("natdiff_cancel_numerals", |
215 |
["(l #+ m) #- n", "l #- (m #+ n)", |
|
216 |
"(l #* m) #- n", "l #- (m #* n)", |
|
217 |
"succ(m) #- n", "m #- succ(n)"], |
|
20044
92cc2f4c7335
simprocs: no theory argument -- use simpset context instead;
wenzelm
parents:
19250
diff
changeset
|
218 |
(K DiffCancelNumerals.proc))]; |
9548 | 219 |
|
220 |
end; |
|
221 |
||
13259 | 222 |
Addsimprocs ArithData.nat_cancel; |
223 |
||
224 |
||
9548 | 225 |
(*examples: |
226 |
print_depth 22; |
|
227 |
set timing; |
|
228 |
set trace_simp; |
|
229 |
fun test s = (Goal s; by (Asm_simp_tac 1)); |
|
230 |
||
231 |
test "x #+ y = x #+ z"; |
|
232 |
test "y #+ x = x #+ z"; |
|
233 |
test "x #+ y #+ z = x #+ z"; |
|
234 |
test "y #+ (z #+ x) = z #+ x"; |
|
235 |
test "x #+ y #+ z = (z #+ y) #+ (x #+ w)"; |
|
236 |
test "x#*y #+ z = (z #+ y) #+ (y#*x #+ w)"; |
|
237 |
||
238 |
test "x #+ succ(y) = x #+ z"; |
|
239 |
test "x #+ succ(y) = succ(z #+ x)"; |
|
240 |
test "succ(x) #+ succ(y) #+ z = succ(z #+ y) #+ succ(x #+ w)"; |
|
241 |
||
242 |
test "(x #+ y) #- (x #+ z) = w"; |
|
243 |
test "(y #+ x) #- (x #+ z) = dd"; |
|
244 |
test "(x #+ y #+ z) #- (x #+ z) = dd"; |
|
245 |
test "(y #+ (z #+ x)) #- (z #+ x) = dd"; |
|
246 |
test "(x #+ y #+ z) #- ((z #+ y) #+ (x #+ w)) = dd"; |
|
247 |
test "(x#*y #+ z) #- ((z #+ y) #+ (y#*x #+ w)) = dd"; |
|
248 |
||
249 |
(*BAD occurrence of natify*) |
|
250 |
test "(x #+ succ(y)) #- (x #+ z) = dd"; |
|
251 |
||
252 |
test "x #* y2 #+ y #* x2 = y #* x2 #+ x #* y2"; |
|
253 |
||
254 |
test "(x #+ succ(y)) #- (succ(z #+ x)) = dd"; |
|
255 |
test "(succ(x) #+ succ(y) #+ z) #- (succ(z #+ y) #+ succ(x #+ w)) = dd"; |
|
256 |
||
257 |
(*use of typing information*) |
|
258 |
test "x : nat ==> x #+ y = x"; |
|
259 |
test "x : nat --> x #+ y = x"; |
|
260 |
test "x : nat ==> x #+ y < x"; |
|
261 |
test "x : nat ==> x < y#+x"; |
|
13126 | 262 |
test "x : nat ==> x le succ(x)"; |
9548 | 263 |
|
264 |
(*fails: no typing information isn't visible*) |
|
265 |
test "x #+ y = x"; |
|
266 |
||
267 |
test "x #+ y < x #+ z"; |
|
268 |
test "y #+ x < x #+ z"; |
|
269 |
test "x #+ y #+ z < x #+ z"; |
|
270 |
test "y #+ z #+ x < x #+ z"; |
|
271 |
test "y #+ (z #+ x) < z #+ x"; |
|
272 |
test "x #+ y #+ z < (z #+ y) #+ (x #+ w)"; |
|
273 |
test "x#*y #+ z < (z #+ y) #+ (y#*x #+ w)"; |
|
274 |
||
275 |
test "x #+ succ(y) < x #+ z"; |
|
276 |
test "x #+ succ(y) < succ(z #+ x)"; |
|
277 |
test "succ(x) #+ succ(y) #+ z < succ(z #+ y) #+ succ(x #+ w)"; |
|
278 |
||
279 |
test "x #+ succ(y) le succ(z #+ x)"; |
|
280 |
*) |