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