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