| author | wenzelm | 
| Fri, 16 May 2025 12:10:49 +0200 | |
| changeset 82626 | e840461d5370 | 
| parent 78800 | 0b3700d31758 | 
| child 82967 | 73af47bc277c | 
| 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*)  | 
| 78800 | 10  | 
val nateq_cancel_numerals_proc: Simplifier.proc  | 
11  | 
val natless_cancel_numerals_proc: Simplifier.proc  | 
|
12  | 
val natdiff_cancel_numerals_proc: Simplifier.proc  | 
|
| 
9570
 
e16e168984e1
installation of cancellation simprocs for the integers
 
paulson 
parents: 
9548 
diff
changeset
 | 
13  | 
(*tools for use in similar applications*)  | 
| 59530 | 14  | 
val gen_trans_tac: Proof.context -> thm -> thm option -> tactic  | 
| 20113 | 15  | 
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
 | 
16  | 
val simplify_meta_eq: thm list -> Proof.context -> 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  | 
||
| 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) =  | 
|
| 
74319
 
54b2e5f771da
clarified signature -- prefer antiquotations (with subtle change of exception content);
 
wenzelm 
parents: 
74316 
diff
changeset
 | 
52  | 
if fastype_of t = \<^Type>\<open>i\<close>  | 
| 74375 | 53  | 
then \<^Const>\<open>IFOL.eq \<^Type>\<open>i\<close> for t u\<close>  | 
| 
74319
 
54b2e5f771da
clarified signature -- prefer antiquotations (with subtle change of exception content);
 
wenzelm 
parents: 
74316 
diff
changeset
 | 
54  | 
else \<^Const>\<open>IFOL.iff for t u\<close>;  | 
| 9548 | 55  | 
|
| 9874 | 56  | 
(*We remove equality assumptions because they confuse the simplifier and  | 
57  | 
because only type-checking assumptions are necessary.*)  | 
|
| 74342 | 58  | 
fun is_eq_thm th = can FOLogic.dest_eq (\<^dest_judgment> (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  | 
| 74342 | 64  | 
val goal = Logic.list_implies (map Thm.prop_of prems', \<^make_judgment> (mk_eq_iff (t, u)));  | 
| 20113 | 65  | 
in SOME (prems' MRS Goal.prove ctxt [] [] goal (K (EVERY tacs)))  | 
| 18678 | 66  | 
handle ERROR msg =>  | 
| 15531 | 67  | 
        (warning (msg ^ "\nCancellation failed: no typing information? (" ^ name ^ ")"); NONE)
 | 
| 9548 | 68  | 
end;  | 
69  | 
||
70  | 
||
| 13462 | 71  | 
(*** Use CancelNumerals simproc without binary numerals,  | 
| 9548 | 72  | 
just for cancellation ***)  | 
73  | 
||
| 74297 | 74  | 
fun mk_times (t, u) = \<^Const>\<open>Arith.mult for t u\<close>;  | 
| 9548 | 75  | 
|
76  | 
fun mk_prod [] = one  | 
|
77  | 
| mk_prod [t] = t  | 
|
78  | 
| mk_prod (t :: ts) = if t = one then mk_prod ts  | 
|
79  | 
else mk_times (t, mk_prod ts);  | 
|
80  | 
||
| 
74319
 
54b2e5f771da
clarified signature -- prefer antiquotations (with subtle change of exception content);
 
wenzelm 
parents: 
74316 
diff
changeset
 | 
81  | 
fun dest_prod tm =  | 
| 
 
54b2e5f771da
clarified signature -- prefer antiquotations (with subtle change of exception content);
 
wenzelm 
parents: 
74316 
diff
changeset
 | 
82  | 
let val (t,u) = \<^Const_fn>\<open>Arith.mult for t u => \<open>(t, u)\<close>\<close> tm  | 
| 
 
54b2e5f771da
clarified signature -- prefer antiquotations (with subtle change of exception content);
 
wenzelm 
parents: 
74316 
diff
changeset
 | 
83  | 
in dest_prod t @ dest_prod u end  | 
| 
 
54b2e5f771da
clarified signature -- prefer antiquotations (with subtle change of exception content);
 
wenzelm 
parents: 
74316 
diff
changeset
 | 
84  | 
handle TERM _ => [tm];  | 
| 9548 | 85  | 
|
86  | 
(*Dummy version: the only arguments are 0 and 1*)  | 
|
| 
24630
 
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
 
wenzelm 
parents: 
20342 
diff
changeset
 | 
87  | 
fun mk_coeff (0, t) = zero  | 
| 9548 | 88  | 
| mk_coeff (1, t) = t  | 
89  | 
  | mk_coeff _       = raise TERM("mk_coeff", []);
 | 
|
90  | 
||
91  | 
(*Dummy version: the "coefficient" is always 1.  | 
|
92  | 
In the result, the factors are sorted terms*)  | 
|
| 35408 | 93  | 
fun dest_coeff t = (1, mk_prod (sort Term_Ord.term_ord (dest_prod t)));  | 
| 9548 | 94  | 
|
95  | 
(*Find first coefficient-term THAT MATCHES u*)  | 
|
96  | 
fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
 | 
|
97  | 
| find_first_coeff past u (t::terms) =  | 
|
98  | 
let val (n,u') = dest_coeff t  | 
|
99  | 
in if u aconv u' then (n, rev past @ terms)  | 
|
100  | 
else find_first_coeff (t::past) u terms  | 
|
101  | 
end  | 
|
102  | 
handle TERM _ => find_first_coeff (t::past) u terms;  | 
|
103  | 
||
104  | 
||
105  | 
(*Simplify #1*n and n*#1 to n*)  | 
|
| 24893 | 106  | 
val add_0s = [@{thm add_0_natify}, @{thm add_0_right_natify}];
 | 
107  | 
val add_succs = [@{thm add_succ}, @{thm add_succ_right}];
 | 
|
108  | 
val mult_1s = [@{thm mult_1_natify}, @{thm mult_1_right_natify}];
 | 
|
109  | 
val tc_rules = [@{thm natify_in_nat}, @{thm add_type}, @{thm diff_type}, @{thm mult_type}];
 | 
|
110  | 
val natifys = [@{thm natify_0}, @{thm natify_ident}, @{thm add_natify1}, @{thm add_natify2},
 | 
|
111  | 
               @{thm diff_natify1}, @{thm diff_natify2}];
 | 
|
| 9548 | 112  | 
|
113  | 
(*Final simplification: cancel + and **)  | 
|
| 
51717
 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 
wenzelm 
parents: 
45620 
diff
changeset
 | 
114  | 
fun simplify_meta_eq rules ctxt =  | 
| 
 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 
wenzelm 
parents: 
45620 
diff
changeset
 | 
115  | 
let val ctxt' =  | 
| 
 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 
wenzelm 
parents: 
45620 
diff
changeset
 | 
116  | 
put_simpset FOL_ss ctxt  | 
| 26287 | 117  | 
      delsimps @{thms iff_simps} (*these could erase the whole rule!*)
 | 
| 18328 | 118  | 
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
 | 
119  | 
      |> 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
 | 
120  | 
in mk_meta_eq o simplify ctxt' end;  | 
| 9548 | 121  | 
|
| 24893 | 122  | 
val final_rules = add_0s @ mult_1s @ [@{thm mult_0}, @{thm mult_0_right}];
 | 
| 9548 | 123  | 
|
124  | 
structure CancelNumeralsCommon =  | 
|
125  | 
struct  | 
|
| 
14387
 
e96d5c42c4b0
Polymorphic treatment of binary arithmetic using axclasses
 
paulson 
parents: 
13487 
diff
changeset
 | 
126  | 
val mk_sum = (fn T:typ => mk_sum)  | 
| 9548 | 127  | 
val dest_sum = dest_sum  | 
128  | 
val mk_coeff = mk_coeff  | 
|
129  | 
val dest_coeff = dest_coeff  | 
|
130  | 
val find_first_coeff = find_first_coeff []  | 
|
| 18328 | 131  | 
|
| 
51717
 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 
wenzelm 
parents: 
45620 
diff
changeset
 | 
132  | 
val norm_ss1 =  | 
| 69593 | 133  | 
    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
 | 
134  | 
val norm_ss2 =  | 
| 69593 | 135  | 
    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
 | 
136  | 
      @{thms mult_ac} @ tc_rules @ natifys)
 | 
| 
 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 
wenzelm 
parents: 
45620 
diff
changeset
 | 
137  | 
fun norm_tac ctxt =  | 
| 
 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 
wenzelm 
parents: 
45620 
diff
changeset
 | 
138  | 
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
 | 
139  | 
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
 | 
140  | 
val numeral_simp_ss =  | 
| 69593 | 141  | 
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
 | 
142  | 
fun numeral_simp_tac ctxt =  | 
| 
 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 
wenzelm 
parents: 
45620 
diff
changeset
 | 
143  | 
ALLGOALS (asm_simp_tac (put_simpset numeral_simp_ss ctxt))  | 
| 9548 | 144  | 
val simplify_meta_eq = simplify_meta_eq final_rules  | 
145  | 
end;  | 
|
146  | 
||
| 9874 | 147  | 
(** The functor argumnets are declared as separate structures  | 
148  | 
so that they can be exported to ease debugging. **)  | 
|
| 9548 | 149  | 
|
| 13462 | 150  | 
structure EqCancelNumeralsData =  | 
| 9874 | 151  | 
struct  | 
152  | 
open CancelNumeralsCommon  | 
|
| 9548 | 153  | 
val prove_conv = prove_conv "nateq_cancel_numerals"  | 
154  | 
val mk_bal = FOLogic.mk_eq  | 
|
| 
9649
 
89155e48fa53
simproc bug fix: only TYPING assumptions are given to the simplifier
 
paulson 
parents: 
9570 
diff
changeset
 | 
155  | 
val dest_bal = FOLogic.dest_eq  | 
| 70470 | 156  | 
  val bal_add1 = @{thm eq_add_iff [THEN iff_trans]}
 | 
157  | 
  val bal_add2 = @{thm eq_add_iff [THEN iff_trans]}
 | 
|
| 59530 | 158  | 
  fun trans_tac ctxt = gen_trans_tac ctxt @{thm iff_trans}
 | 
| 9874 | 159  | 
end;  | 
160  | 
||
161  | 
structure EqCancelNumerals = CancelNumeralsFun(EqCancelNumeralsData);  | 
|
| 9548 | 162  | 
|
| 13462 | 163  | 
structure LessCancelNumeralsData =  | 
| 9874 | 164  | 
struct  | 
165  | 
open CancelNumeralsCommon  | 
|
| 9548 | 166  | 
val prove_conv = prove_conv "natless_cancel_numerals"  | 
| 74297 | 167  | 
fun mk_bal (t, u) = \<^Const>\<open>Ordinal.lt for t u\<close>  | 
| 
74319
 
54b2e5f771da
clarified signature -- prefer antiquotations (with subtle change of exception content);
 
wenzelm 
parents: 
74316 
diff
changeset
 | 
168  | 
val dest_bal = \<^Const_fn>\<open>Ordinal.lt for t u => \<open>(t, u)\<close>\<close>  | 
| 70470 | 169  | 
  val bal_add1 = @{thm less_add_iff [THEN iff_trans]}
 | 
170  | 
  val bal_add2 = @{thm less_add_iff [THEN iff_trans]}
 | 
|
| 59530 | 171  | 
  fun trans_tac ctxt = gen_trans_tac ctxt @{thm iff_trans}
 | 
| 9874 | 172  | 
end;  | 
173  | 
||
174  | 
structure LessCancelNumerals = CancelNumeralsFun(LessCancelNumeralsData);  | 
|
| 9548 | 175  | 
|
| 13462 | 176  | 
structure DiffCancelNumeralsData =  | 
| 9874 | 177  | 
struct  | 
178  | 
open CancelNumeralsCommon  | 
|
| 9548 | 179  | 
val prove_conv = prove_conv "natdiff_cancel_numerals"  | 
| 74297 | 180  | 
fun mk_bal (t, u) = \<^Const>\<open>Arith.diff for t u\<close>  | 
| 
74319
 
54b2e5f771da
clarified signature -- prefer antiquotations (with subtle change of exception content);
 
wenzelm 
parents: 
74316 
diff
changeset
 | 
181  | 
val dest_bal = \<^Const_fn>\<open>Arith.diff for t u => \<open>(t, u)\<close>\<close>  | 
| 70470 | 182  | 
  val bal_add1 = @{thm diff_add_eq [THEN trans]}
 | 
183  | 
  val bal_add2 = @{thm diff_add_eq [THEN trans]}
 | 
|
| 59530 | 184  | 
  fun trans_tac ctxt = gen_trans_tac ctxt @{thm trans}
 | 
| 9874 | 185  | 
end;  | 
186  | 
||
187  | 
structure DiffCancelNumerals = CancelNumeralsFun(DiffCancelNumeralsData);  | 
|
| 9548 | 188  | 
|
| 78791 | 189  | 
val nateq_cancel_numerals_proc = EqCancelNumerals.proc;  | 
190  | 
val natless_cancel_numerals_proc = LessCancelNumerals.proc;  | 
|
191  | 
val natdiff_cancel_numerals_proc = DiffCancelNumerals.proc;  | 
|
| 9548 | 192  | 
|
193  | 
end;  |