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