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