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