author | wenzelm |
Thu, 19 Oct 2023 17:06:39 +0200 | |
changeset 78800 | 0b3700d31758 |
parent 69593 | 3dda49e08b9d |
permissions | -rw-r--r-- |
31068
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents:
30685
diff
changeset
|
1 |
(* Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
23164 | 2 |
|
3 |
Simprocs for nat numerals. |
|
4 |
*) |
|
5 |
||
31068
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents:
30685
diff
changeset
|
6 |
signature NAT_NUMERAL_SIMPROCS = |
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents:
30685
diff
changeset
|
7 |
sig |
78800 | 8 |
val combine_numerals: Simplifier.proc |
9 |
val eq_cancel_numerals: Simplifier.proc |
|
10 |
val less_cancel_numerals: Simplifier.proc |
|
11 |
val le_cancel_numerals: Simplifier.proc |
|
12 |
val diff_cancel_numerals: Simplifier.proc |
|
13 |
val eq_cancel_numeral_factor: Simplifier.proc |
|
14 |
val less_cancel_numeral_factor: Simplifier.proc |
|
15 |
val le_cancel_numeral_factor: Simplifier.proc |
|
16 |
val div_cancel_numeral_factor: Simplifier.proc |
|
17 |
val dvd_cancel_numeral_factor: Simplifier.proc |
|
18 |
val eq_cancel_factor: Simplifier.proc |
|
19 |
val less_cancel_factor: Simplifier.proc |
|
20 |
val le_cancel_factor: Simplifier.proc |
|
21 |
val div_cancel_factor: Simplifier.proc |
|
22 |
val dvd_cancel_factor: Simplifier.proc |
|
31068
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents:
30685
diff
changeset
|
23 |
end; |
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents:
30685
diff
changeset
|
24 |
|
45462
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
huffman
parents:
45436
diff
changeset
|
25 |
structure Nat_Numeral_Simprocs : NAT_NUMERAL_SIMPROCS = |
23164 | 26 |
struct |
27 |
||
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
45668
diff
changeset
|
28 |
(*Maps n to #n for n = 1, 2*) |
61694
6571c78c9667
Removed some legacy theorems; minor adjustments to simplification rules; new material on homotopic paths
paulson <lp15@cam.ac.uk>
parents:
61144
diff
changeset
|
29 |
val numeral_syms = [@{thm numeral_One} RS sym, @{thm numeral_2_eq_2} RS sym]; |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
30 |
val numeral_sym_ss = |
69593 | 31 |
simpset_of (put_simpset HOL_basic_ss \<^context> addsimps numeral_syms); |
23164 | 32 |
|
33 |
(*Utilities*) |
|
34 |
||
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
45668
diff
changeset
|
35 |
fun mk_number 1 = HOLogic.numeral_const HOLogic.natT $ HOLogic.one_const |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
45668
diff
changeset
|
36 |
| mk_number n = HOLogic.mk_number HOLogic.natT n; |
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24431
diff
changeset
|
37 |
fun dest_number t = Int.max (0, snd (HOLogic.dest_number t)); |
23164 | 38 |
|
39 |
fun find_first_numeral past (t::terms) = |
|
40 |
((dest_number t, t, rev past @ terms) |
|
41 |
handle TERM _ => find_first_numeral (t::past) terms) |
|
42 |
| find_first_numeral past [] = raise TERM("find_first_numeral", []); |
|
43 |
||
66811 | 44 |
val mk_sum = Arith_Data.mk_sum HOLogic.natT; |
23164 | 45 |
|
66811 | 46 |
val long_mk_sum = Arith_Data.long_mk_sum HOLogic.natT; |
23164 | 47 |
|
69593 | 48 |
val dest_plus = HOLogic.dest_bin \<^const_name>\<open>Groups.plus\<close> HOLogic.natT; |
23164 | 49 |
|
50 |
||
51 |
(** Other simproc items **) |
|
52 |
||
53 |
val bin_simps = |
|
61694
6571c78c9667
Removed some legacy theorems; minor adjustments to simplification rules; new material on homotopic paths
paulson <lp15@cam.ac.uk>
parents:
61144
diff
changeset
|
54 |
[@{thm numeral_One} RS sym, |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
45668
diff
changeset
|
55 |
@{thm numeral_plus_numeral}, @{thm add_numeral_left}, |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
45668
diff
changeset
|
56 |
@{thm diff_nat_numeral}, @{thm diff_0_eq_0}, @{thm diff_0}, |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
45668
diff
changeset
|
57 |
@{thm numeral_times_numeral}, @{thm mult_numeral_left(1)}, |
45668
0ea1c705eebb
use HOL_basic_ss instead of HOL_ss for internal simproc proofs (cf. b36eedcd1633)
huffman
parents:
45463
diff
changeset
|
58 |
@{thm if_True}, @{thm if_False}, @{thm not_False_eq_True}, |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
45668
diff
changeset
|
59 |
@{thm nat_0}, @{thm nat_numeral}, @{thm nat_neg_numeral}] @ |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
45668
diff
changeset
|
60 |
@{thms arith_simps} @ @{thms rel_simps}; |
23164 | 61 |
|
62 |
||
63 |
(*** CancelNumerals simprocs ***) |
|
64 |
||
65 |
val one = mk_number 1; |
|
69593 | 66 |
val mk_times = HOLogic.mk_binop \<^const_name>\<open>Groups.times\<close>; |
23164 | 67 |
|
68 |
fun mk_prod [] = one |
|
69 |
| mk_prod [t] = t |
|
70 |
| mk_prod (t :: ts) = if t = one then mk_prod ts |
|
71 |
else mk_times (t, mk_prod ts); |
|
72 |
||
69593 | 73 |
val dest_times = HOLogic.dest_bin \<^const_name>\<open>Groups.times\<close> HOLogic.natT; |
23164 | 74 |
|
75 |
fun dest_prod t = |
|
76 |
let val (t,u) = dest_times t |
|
77 |
in dest_prod t @ dest_prod u end |
|
78 |
handle TERM _ => [t]; |
|
79 |
||
80 |
(*DON'T do the obvious simplifications; that would create special cases*) |
|
81 |
fun mk_coeff (k,t) = mk_times (mk_number k, t); |
|
82 |
||
83 |
(*Express t as a product of (possibly) a numeral with other factors, sorted*) |
|
84 |
fun dest_coeff t = |
|
35408 | 85 |
let val ts = sort Term_Ord.term_ord (dest_prod t) |
23164 | 86 |
val (n, _, ts') = find_first_numeral [] ts |
87 |
handle TERM _ => (1, one, ts) |
|
88 |
in (n, mk_prod ts') end; |
|
89 |
||
90 |
(*Find first coefficient-term THAT MATCHES u*) |
|
91 |
fun find_first_coeff past u [] = raise TERM("find_first_coeff", []) |
|
92 |
| find_first_coeff past u (t::terms) = |
|
93 |
let val (n,u') = dest_coeff t |
|
94 |
in if u aconv u' then (n, rev past @ terms) |
|
95 |
else find_first_coeff (t::past) u terms |
|
96 |
end |
|
97 |
handle TERM _ => find_first_coeff (t::past) u terms; |
|
98 |
||
99 |
||
100 |
(*Split up a sum into the list of its constituent terms, on the way removing any |
|
101 |
Sucs and counting them.*) |
|
69593 | 102 |
fun dest_Suc_sum (Const (\<^const_name>\<open>Suc\<close>, _) $ t, (k,ts)) = dest_Suc_sum (t, (k+1,ts)) |
23164 | 103 |
| dest_Suc_sum (t, (k,ts)) = |
104 |
let val (t1,t2) = dest_plus t |
|
105 |
in dest_Suc_sum (t1, dest_Suc_sum (t2, (k,ts))) end |
|
106 |
handle TERM _ => (k, t::ts); |
|
107 |
||
108 |
(*Code for testing whether numerals are already used in the goal*) |
|
69593 | 109 |
fun is_numeral (Const(\<^const_name>\<open>Num.numeral\<close>, _) $ w) = true |
23164 | 110 |
| is_numeral _ = false; |
111 |
||
112 |
fun prod_has_numeral t = exists is_numeral (dest_prod t); |
|
113 |
||
114 |
(*The Sucs found in the term are converted to a binary numeral. If relaxed is false, |
|
115 |
an exception is raised unless the original expression contains at least one |
|
116 |
numeral in a coefficient position. This prevents nat_combine_numerals from |
|
117 |
introducing numerals to goals.*) |
|
118 |
fun dest_Sucs_sum relaxed t = |
|
119 |
let val (k,ts) = dest_Suc_sum (t,(0,[])) |
|
120 |
in |
|
121 |
if relaxed orelse exists prod_has_numeral ts then |
|
122 |
if k=0 then ts |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24431
diff
changeset
|
123 |
else mk_number k :: ts |
23164 | 124 |
else raise TERM("Nat_Numeral_Simprocs.dest_Sucs_sum", [t]) |
125 |
end; |
|
126 |
||
127 |
||
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
128 |
(* FIXME !? *) |
69593 | 129 |
val rename_numerals = simplify (put_simpset numeral_sym_ss \<^context>) o Thm.transfer \<^theory>; |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
130 |
|
23164 | 131 |
(*Simplify 1*n and n*1 to n*) |
35064
1bdef0c013d3
hide fact names clashing with fact names from Group.thy
haftmann
parents:
35050
diff
changeset
|
132 |
val add_0s = map rename_numerals [@{thm Nat.add_0}, @{thm Nat.add_0_right}]; |
23164 | 133 |
val mult_1s = map rename_numerals [@{thm nat_mult_1}, @{thm nat_mult_1_right}]; |
134 |
||
135 |
(*Final simplification: cancel + and *; replace Numeral0 by 0 and Numeral1 by 1*) |
|
136 |
||
137 |
(*And these help the simproc return False when appropriate, which helps |
|
138 |
the arith prover.*) |
|
23881 | 139 |
val contra_rules = [@{thm add_Suc}, @{thm add_Suc_right}, @{thm Zero_not_Suc}, |
140 |
@{thm Suc_not_Zero}, @{thm le_0_eq}]; |
|
23164 | 141 |
|
142 |
val simplify_meta_eq = |
|
30518 | 143 |
Arith_Data.simplify_meta_eq |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
45668
diff
changeset
|
144 |
([@{thm numeral_1_eq_Suc_0}, @{thm Nat.add_0}, @{thm Nat.add_0_right}, |
23471 | 145 |
@{thm mult_0}, @{thm mult_0_right}, @{thm mult_1}, @{thm mult_1_right}] @ contra_rules); |
23164 | 146 |
|
147 |
||
148 |
(*** Applying CancelNumeralsFun ***) |
|
149 |
||
150 |
structure CancelNumeralsCommon = |
|
44945 | 151 |
struct |
152 |
val mk_sum = (fn T : typ => mk_sum) |
|
153 |
val dest_sum = dest_Sucs_sum true |
|
154 |
val mk_coeff = mk_coeff |
|
155 |
val dest_coeff = dest_coeff |
|
156 |
val find_first_coeff = find_first_coeff [] |
|
44947
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
haftmann
parents:
44945
diff
changeset
|
157 |
val trans_tac = Numeral_Simprocs.trans_tac |
23164 | 158 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
159 |
val norm_ss1 = |
69593 | 160 |
simpset_of (put_simpset Numeral_Simprocs.num_ss \<^context> |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
161 |
addsimps numeral_syms @ add_0s @ mult_1s @ |
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
51717
diff
changeset
|
162 |
[@{thm Suc_eq_plus1_left}] @ @{thms ac_simps}) |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
163 |
val norm_ss2 = |
69593 | 164 |
simpset_of (put_simpset Numeral_Simprocs.num_ss \<^context> |
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
51717
diff
changeset
|
165 |
addsimps bin_simps @ @{thms ac_simps} @ @{thms ac_simps}) |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
166 |
fun norm_tac ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
167 |
ALLGOALS (simp_tac (put_simpset norm_ss1 ctxt)) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
168 |
THEN ALLGOALS (simp_tac (put_simpset norm_ss2 ctxt)) |
23164 | 169 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
170 |
val numeral_simp_ss = |
69593 | 171 |
simpset_of (put_simpset HOL_basic_ss \<^context> |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
172 |
addsimps add_0s @ bin_simps); |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
173 |
fun numeral_simp_tac ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
174 |
ALLGOALS (simp_tac (put_simpset numeral_simp_ss ctxt)); |
23164 | 175 |
val simplify_meta_eq = simplify_meta_eq |
44945 | 176 |
val prove_conv = Arith_Data.prove_conv |
177 |
end; |
|
23164 | 178 |
|
179 |
structure EqCancelNumerals = CancelNumeralsFun |
|
180 |
(open CancelNumeralsCommon |
|
181 |
val mk_bal = HOLogic.mk_eq |
|
69593 | 182 |
val dest_bal = HOLogic.dest_bin \<^const_name>\<open>HOL.eq\<close> HOLogic.natT |
23471 | 183 |
val bal_add1 = @{thm nat_eq_add_iff1} RS trans |
184 |
val bal_add2 = @{thm nat_eq_add_iff2} RS trans |
|
23164 | 185 |
); |
186 |
||
187 |
structure LessCancelNumerals = CancelNumeralsFun |
|
188 |
(open CancelNumeralsCommon |
|
69593 | 189 |
val mk_bal = HOLogic.mk_binrel \<^const_name>\<open>Orderings.less\<close> |
190 |
val dest_bal = HOLogic.dest_bin \<^const_name>\<open>Orderings.less\<close> HOLogic.natT |
|
23471 | 191 |
val bal_add1 = @{thm nat_less_add_iff1} RS trans |
192 |
val bal_add2 = @{thm nat_less_add_iff2} RS trans |
|
23164 | 193 |
); |
194 |
||
195 |
structure LeCancelNumerals = CancelNumeralsFun |
|
196 |
(open CancelNumeralsCommon |
|
69593 | 197 |
val mk_bal = HOLogic.mk_binrel \<^const_name>\<open>Orderings.less_eq\<close> |
198 |
val dest_bal = HOLogic.dest_bin \<^const_name>\<open>Orderings.less_eq\<close> HOLogic.natT |
|
23471 | 199 |
val bal_add1 = @{thm nat_le_add_iff1} RS trans |
200 |
val bal_add2 = @{thm nat_le_add_iff2} RS trans |
|
23164 | 201 |
); |
202 |
||
203 |
structure DiffCancelNumerals = CancelNumeralsFun |
|
204 |
(open CancelNumeralsCommon |
|
69593 | 205 |
val mk_bal = HOLogic.mk_binop \<^const_name>\<open>Groups.minus\<close> |
206 |
val dest_bal = HOLogic.dest_bin \<^const_name>\<open>Groups.minus\<close> HOLogic.natT |
|
23471 | 207 |
val bal_add1 = @{thm nat_diff_add_eq1} RS trans |
208 |
val bal_add2 = @{thm nat_diff_add_eq2} RS trans |
|
23164 | 209 |
); |
210 |
||
61144 | 211 |
val eq_cancel_numerals = EqCancelNumerals.proc |
212 |
val less_cancel_numerals = LessCancelNumerals.proc |
|
213 |
val le_cancel_numerals = LeCancelNumerals.proc |
|
214 |
val diff_cancel_numerals = DiffCancelNumerals.proc |
|
23164 | 215 |
|
216 |
||
217 |
(*** Applying CombineNumeralsFun ***) |
|
218 |
||
219 |
structure CombineNumeralsData = |
|
44945 | 220 |
struct |
221 |
type coeff = int |
|
222 |
val iszero = (fn x => x = 0) |
|
223 |
val add = op + |
|
224 |
val mk_sum = (fn T : typ => long_mk_sum) (*to work for 2*x + 3*x *) |
|
225 |
val dest_sum = dest_Sucs_sum false |
|
226 |
val mk_coeff = mk_coeff |
|
227 |
val dest_coeff = dest_coeff |
|
228 |
val left_distrib = @{thm left_add_mult_distrib} RS trans |
|
229 |
val prove_conv = Arith_Data.prove_conv_nohyps |
|
44947
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
haftmann
parents:
44945
diff
changeset
|
230 |
val trans_tac = Numeral_Simprocs.trans_tac |
23164 | 231 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
232 |
val norm_ss1 = |
69593 | 233 |
simpset_of (put_simpset Numeral_Simprocs.num_ss \<^context> |
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
51717
diff
changeset
|
234 |
addsimps numeral_syms @ add_0s @ mult_1s @ [@{thm Suc_eq_plus1}] @ @{thms ac_simps}) |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
235 |
val norm_ss2 = |
69593 | 236 |
simpset_of (put_simpset Numeral_Simprocs.num_ss \<^context> |
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
51717
diff
changeset
|
237 |
addsimps bin_simps @ @{thms ac_simps} @ @{thms ac_simps}) |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
238 |
fun norm_tac ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
239 |
ALLGOALS (simp_tac (put_simpset norm_ss1 ctxt)) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
240 |
THEN ALLGOALS (simp_tac (put_simpset norm_ss2 ctxt)) |
23164 | 241 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
242 |
val numeral_simp_ss = |
69593 | 243 |
simpset_of (put_simpset HOL_basic_ss \<^context> |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
244 |
addsimps add_0s @ bin_simps); |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
245 |
fun numeral_simp_tac ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
246 |
ALLGOALS (simp_tac (put_simpset numeral_simp_ss ctxt)) |
44945 | 247 |
val simplify_meta_eq = simplify_meta_eq |
248 |
end; |
|
23164 | 249 |
|
250 |
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData); |
|
251 |
||
61144 | 252 |
val combine_numerals = CombineNumerals.proc |
23164 | 253 |
|
254 |
||
255 |
(*** Applying CancelNumeralFactorFun ***) |
|
256 |
||
257 |
structure CancelNumeralFactorCommon = |
|
44945 | 258 |
struct |
259 |
val mk_coeff = mk_coeff |
|
260 |
val dest_coeff = dest_coeff |
|
44947
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
haftmann
parents:
44945
diff
changeset
|
261 |
val trans_tac = Numeral_Simprocs.trans_tac |
23164 | 262 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
263 |
val norm_ss1 = |
69593 | 264 |
simpset_of (put_simpset Numeral_Simprocs.num_ss \<^context> |
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
51717
diff
changeset
|
265 |
addsimps numeral_syms @ add_0s @ mult_1s @ [@{thm Suc_eq_plus1_left}] @ @{thms ac_simps}) |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
266 |
val norm_ss2 = |
69593 | 267 |
simpset_of (put_simpset Numeral_Simprocs.num_ss \<^context> |
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
51717
diff
changeset
|
268 |
addsimps bin_simps @ @{thms ac_simps} @ @{thms ac_simps}) |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
269 |
fun norm_tac ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
270 |
ALLGOALS (simp_tac (put_simpset norm_ss1 ctxt)) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
271 |
THEN ALLGOALS (simp_tac (put_simpset norm_ss2 ctxt)) |
23164 | 272 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
273 |
val numeral_simp_ss = |
69593 | 274 |
simpset_of (put_simpset HOL_basic_ss \<^context> addsimps bin_simps) |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
275 |
fun numeral_simp_tac ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
276 |
ALLGOALS (simp_tac (put_simpset numeral_simp_ss ctxt)) |
44945 | 277 |
val simplify_meta_eq = simplify_meta_eq |
278 |
val prove_conv = Arith_Data.prove_conv |
|
279 |
end; |
|
23164 | 280 |
|
281 |
structure DivCancelNumeralFactor = CancelNumeralFactorFun |
|
282 |
(open CancelNumeralFactorCommon |
|
69593 | 283 |
val mk_bal = HOLogic.mk_binop \<^const_name>\<open>Rings.divide\<close> |
284 |
val dest_bal = HOLogic.dest_bin \<^const_name>\<open>Rings.divide\<close> HOLogic.natT |
|
23471 | 285 |
val cancel = @{thm nat_mult_div_cancel1} RS trans |
23164 | 286 |
val neg_exchanges = false |
44945 | 287 |
); |
23164 | 288 |
|
23969 | 289 |
structure DvdCancelNumeralFactor = CancelNumeralFactorFun |
290 |
(open CancelNumeralFactorCommon |
|
69593 | 291 |
val mk_bal = HOLogic.mk_binrel \<^const_name>\<open>Rings.dvd\<close> |
292 |
val dest_bal = HOLogic.dest_bin \<^const_name>\<open>Rings.dvd\<close> HOLogic.natT |
|
23969 | 293 |
val cancel = @{thm nat_mult_dvd_cancel1} RS trans |
294 |
val neg_exchanges = false |
|
44945 | 295 |
); |
23969 | 296 |
|
23164 | 297 |
structure EqCancelNumeralFactor = CancelNumeralFactorFun |
298 |
(open CancelNumeralFactorCommon |
|
299 |
val mk_bal = HOLogic.mk_eq |
|
69593 | 300 |
val dest_bal = HOLogic.dest_bin \<^const_name>\<open>HOL.eq\<close> HOLogic.natT |
23471 | 301 |
val cancel = @{thm nat_mult_eq_cancel1} RS trans |
23164 | 302 |
val neg_exchanges = false |
44945 | 303 |
); |
23164 | 304 |
|
305 |
structure LessCancelNumeralFactor = CancelNumeralFactorFun |
|
61144 | 306 |
( |
307 |
open CancelNumeralFactorCommon |
|
69593 | 308 |
val mk_bal = HOLogic.mk_binrel \<^const_name>\<open>Orderings.less\<close> |
309 |
val dest_bal = HOLogic.dest_bin \<^const_name>\<open>Orderings.less\<close> HOLogic.natT |
|
23471 | 310 |
val cancel = @{thm nat_mult_less_cancel1} RS trans |
23164 | 311 |
val neg_exchanges = true |
44945 | 312 |
); |
23164 | 313 |
|
314 |
structure LeCancelNumeralFactor = CancelNumeralFactorFun |
|
61144 | 315 |
( |
316 |
open CancelNumeralFactorCommon |
|
69593 | 317 |
val mk_bal = HOLogic.mk_binrel \<^const_name>\<open>Orderings.less_eq\<close> |
318 |
val dest_bal = HOLogic.dest_bin \<^const_name>\<open>Orderings.less_eq\<close> HOLogic.natT |
|
23471 | 319 |
val cancel = @{thm nat_mult_le_cancel1} RS trans |
23164 | 320 |
val neg_exchanges = true |
321 |
) |
|
322 |
||
61144 | 323 |
val eq_cancel_numeral_factor = EqCancelNumeralFactor.proc |
324 |
val less_cancel_numeral_factor = LessCancelNumeralFactor.proc |
|
325 |
val le_cancel_numeral_factor = LeCancelNumeralFactor.proc |
|
326 |
val div_cancel_numeral_factor = DivCancelNumeralFactor.proc |
|
327 |
val dvd_cancel_numeral_factor = DvdCancelNumeralFactor.proc |
|
23164 | 328 |
|
329 |
||
330 |
(*** Applying ExtractCommonTermFun ***) |
|
331 |
||
332 |
(*this version ALWAYS includes a trailing one*) |
|
333 |
fun long_mk_prod [] = one |
|
334 |
| long_mk_prod (t :: ts) = mk_times (t, mk_prod ts); |
|
335 |
||
336 |
(*Find first term that matches u*) |
|
337 |
fun find_first_t past u [] = raise TERM("find_first_t", []) |
|
338 |
| find_first_t past u (t::terms) = |
|
339 |
if u aconv t then (rev past @ terms) |
|
340 |
else find_first_t (t::past) u terms |
|
341 |
handle TERM _ => find_first_t (t::past) u terms; |
|
342 |
||
343 |
(** Final simplification for the CancelFactor simprocs **) |
|
30518 | 344 |
val simplify_one = Arith_Data.simplify_meta_eq |
64244 | 345 |
[@{thm mult_1_left}, @{thm mult_1_right}, @{thm div_by_Suc_0}, @{thm numeral_1_eq_Suc_0}]; |
23164 | 346 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
347 |
fun cancel_simplify_meta_eq ctxt cancel_th th = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
348 |
simplify_one ctxt (([th, cancel_th]) MRS trans); |
23164 | 349 |
|
350 |
structure CancelFactorCommon = |
|
44945 | 351 |
struct |
352 |
val mk_sum = (fn T : typ => long_mk_prod) |
|
353 |
val dest_sum = dest_prod |
|
354 |
val mk_coeff = mk_coeff |
|
355 |
val dest_coeff = dest_coeff |
|
356 |
val find_first = find_first_t [] |
|
44947
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
haftmann
parents:
44945
diff
changeset
|
357 |
val trans_tac = Numeral_Simprocs.trans_tac |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
358 |
val norm_ss = |
69593 | 359 |
simpset_of (put_simpset HOL_basic_ss \<^context> |
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
51717
diff
changeset
|
360 |
addsimps mult_1s @ @{thms ac_simps}) |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
361 |
fun norm_tac ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
47108
diff
changeset
|
362 |
ALLGOALS (simp_tac (put_simpset norm_ss ctxt)) |
30649
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
nipkow
parents:
30518
diff
changeset
|
363 |
val simplify_meta_eq = cancel_simplify_meta_eq |
45270
d5b5c9259afd
fix bug in cancel_factor simprocs so they will work on goals like 'x * y < x * z' where the common term is already on the left
huffman
parents:
44947
diff
changeset
|
364 |
fun mk_eq (a, b) = HOLogic.mk_Trueprop (HOLogic.mk_eq (a, b)) |
44945 | 365 |
end; |
23164 | 366 |
|
367 |
structure EqCancelFactor = ExtractCommonTermFun |
|
368 |
(open CancelFactorCommon |
|
369 |
val mk_bal = HOLogic.mk_eq |
|
69593 | 370 |
val dest_bal = HOLogic.dest_bin \<^const_name>\<open>HOL.eq\<close> HOLogic.natT |
31368 | 371 |
fun simp_conv _ _ = SOME @{thm nat_mult_eq_cancel_disj} |
23164 | 372 |
); |
373 |
||
44945 | 374 |
structure LeCancelFactor = ExtractCommonTermFun |
375 |
(open CancelFactorCommon |
|
69593 | 376 |
val mk_bal = HOLogic.mk_binrel \<^const_name>\<open>Orderings.less_eq\<close> |
377 |
val dest_bal = HOLogic.dest_bin \<^const_name>\<open>Orderings.less_eq\<close> HOLogic.natT |
|
44945 | 378 |
fun simp_conv _ _ = SOME @{thm nat_mult_le_cancel_disj} |
379 |
); |
|
380 |
||
23164 | 381 |
structure LessCancelFactor = ExtractCommonTermFun |
382 |
(open CancelFactorCommon |
|
69593 | 383 |
val mk_bal = HOLogic.mk_binrel \<^const_name>\<open>Orderings.less\<close> |
384 |
val dest_bal = HOLogic.dest_bin \<^const_name>\<open>Orderings.less\<close> HOLogic.natT |
|
31368 | 385 |
fun simp_conv _ _ = SOME @{thm nat_mult_less_cancel_disj} |
23164 | 386 |
); |
387 |
||
388 |
structure DivideCancelFactor = ExtractCommonTermFun |
|
389 |
(open CancelFactorCommon |
|
69593 | 390 |
val mk_bal = HOLogic.mk_binop \<^const_name>\<open>Rings.divide\<close> |
391 |
val dest_bal = HOLogic.dest_bin \<^const_name>\<open>Rings.divide\<close> HOLogic.natT |
|
31368 | 392 |
fun simp_conv _ _ = SOME @{thm nat_mult_div_cancel_disj} |
23164 | 393 |
); |
394 |
||
23969 | 395 |
structure DvdCancelFactor = ExtractCommonTermFun |
396 |
(open CancelFactorCommon |
|
69593 | 397 |
val mk_bal = HOLogic.mk_binrel \<^const_name>\<open>Rings.dvd\<close> |
398 |
val dest_bal = HOLogic.dest_bin \<^const_name>\<open>Rings.dvd\<close> HOLogic.natT |
|
31368 | 399 |
fun simp_conv _ _ = SOME @{thm nat_mult_dvd_cancel_disj} |
23969 | 400 |
); |
401 |
||
59582 | 402 |
fun eq_cancel_factor ctxt ct = EqCancelFactor.proc ctxt (Thm.term_of ct) |
403 |
fun less_cancel_factor ctxt ct = LessCancelFactor.proc ctxt (Thm.term_of ct) |
|
404 |
fun le_cancel_factor ctxt ct = LeCancelFactor.proc ctxt (Thm.term_of ct) |
|
405 |
fun div_cancel_factor ctxt ct = DivideCancelFactor.proc ctxt (Thm.term_of ct) |
|
406 |
fun dvd_cancel_factor ctxt ct = DvdCancelFactor.proc ctxt (Thm.term_of ct) |
|
23164 | 407 |
|
408 |
end; |