author | huffman |
Fri, 11 Nov 2011 11:11:03 +0100 | |
changeset 45462 | aba629d6cee5 |
parent 45436 | 62bc9474d04b |
child 45463 | 9a588a835c1e |
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 |
45462
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
huffman
parents:
45436
diff
changeset
|
8 |
val combine_numerals: simpset -> cterm -> thm option |
45436
62bc9474d04b
use simproc_setup for some nat_numeral simprocs; add simproc tests
huffman
parents:
45306
diff
changeset
|
9 |
val eq_cancel_numerals: simpset -> cterm -> thm option |
62bc9474d04b
use simproc_setup for some nat_numeral simprocs; add simproc tests
huffman
parents:
45306
diff
changeset
|
10 |
val less_cancel_numerals: simpset -> cterm -> thm option |
62bc9474d04b
use simproc_setup for some nat_numeral simprocs; add simproc tests
huffman
parents:
45306
diff
changeset
|
11 |
val le_cancel_numerals: simpset -> cterm -> thm option |
62bc9474d04b
use simproc_setup for some nat_numeral simprocs; add simproc tests
huffman
parents:
45306
diff
changeset
|
12 |
val diff_cancel_numerals: simpset -> cterm -> thm option |
45462
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
huffman
parents:
45436
diff
changeset
|
13 |
val eq_cancel_factor: simpset -> cterm -> thm option |
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
huffman
parents:
45436
diff
changeset
|
14 |
val less_cancel_factor: simpset -> cterm -> thm option |
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
huffman
parents:
45436
diff
changeset
|
15 |
val le_cancel_factor: simpset -> cterm -> thm option |
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
huffman
parents:
45436
diff
changeset
|
16 |
val divide_cancel_factor: simpset -> cterm -> thm option |
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
huffman
parents:
45436
diff
changeset
|
17 |
val dvd_cancel_factor: simpset -> cterm -> thm option |
31068
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents:
30685
diff
changeset
|
18 |
val cancel_numeral_factors: simproc list |
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents:
30685
diff
changeset
|
19 |
end; |
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents:
30685
diff
changeset
|
20 |
|
45462
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
huffman
parents:
45436
diff
changeset
|
21 |
structure Nat_Numeral_Simprocs : NAT_NUMERAL_SIMPROCS = |
23164 | 22 |
struct |
23 |
||
24 |
(*Maps n to #n for n = 0, 1, 2*) |
|
30496
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents:
29269
diff
changeset
|
25 |
val numeral_syms = [@{thm nat_numeral_0_eq_0} RS sym, @{thm nat_numeral_1_eq_1} RS sym, @{thm numeral_2_eq_2} RS sym]; |
23164 | 26 |
val numeral_sym_ss = HOL_ss addsimps numeral_syms; |
27 |
||
44945 | 28 |
val rename_numerals = simplify numeral_sym_ss o Thm.transfer @{theory}; |
23164 | 29 |
|
30 |
(*Utilities*) |
|
31 |
||
32 |
fun mk_number n = HOLogic.number_of_const HOLogic.natT $ HOLogic.mk_numeral n; |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24431
diff
changeset
|
33 |
fun dest_number t = Int.max (0, snd (HOLogic.dest_number t)); |
23164 | 34 |
|
35 |
fun find_first_numeral past (t::terms) = |
|
36 |
((dest_number t, t, rev past @ terms) |
|
37 |
handle TERM _ => find_first_numeral (t::past) terms) |
|
38 |
| find_first_numeral past [] = raise TERM("find_first_numeral", []); |
|
39 |
||
40 |
val zero = mk_number 0; |
|
35267
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents:
35092
diff
changeset
|
41 |
val mk_plus = HOLogic.mk_binop @{const_name Groups.plus}; |
23164 | 42 |
|
43 |
(*Thus mk_sum[t] yields t+0; longer sums don't have a trailing zero*) |
|
44 |
fun mk_sum [] = zero |
|
45 |
| mk_sum [t,u] = mk_plus (t, u) |
|
46 |
| mk_sum (t :: ts) = mk_plus (t, mk_sum ts); |
|
47 |
||
48 |
(*this version ALWAYS includes a trailing zero*) |
|
49 |
fun long_mk_sum [] = HOLogic.zero |
|
50 |
| long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts); |
|
51 |
||
35267
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents:
35092
diff
changeset
|
52 |
val dest_plus = HOLogic.dest_bin @{const_name Groups.plus} HOLogic.natT; |
23164 | 53 |
|
54 |
||
55 |
(** Other simproc items **) |
|
56 |
||
57 |
val bin_simps = |
|
23471 | 58 |
[@{thm nat_numeral_0_eq_0} RS sym, @{thm nat_numeral_1_eq_1} RS sym, |
59 |
@{thm add_nat_number_of}, @{thm nat_number_of_add_left}, |
|
60 |
@{thm diff_nat_number_of}, @{thm le_number_of_eq_not_less}, |
|
61 |
@{thm mult_nat_number_of}, @{thm nat_number_of_mult_left}, |
|
62 |
@{thm less_nat_number_of}, |
|
63 |
@{thm Let_number_of}, @{thm nat_number_of}] @ |
|
29039 | 64 |
@{thms arith_simps} @ @{thms rel_simps} @ @{thms neg_simps}; |
23164 | 65 |
|
66 |
||
67 |
(*** CancelNumerals simprocs ***) |
|
68 |
||
69 |
val one = mk_number 1; |
|
35267
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents:
35092
diff
changeset
|
70 |
val mk_times = HOLogic.mk_binop @{const_name Groups.times}; |
23164 | 71 |
|
72 |
fun mk_prod [] = one |
|
73 |
| mk_prod [t] = t |
|
74 |
| mk_prod (t :: ts) = if t = one then mk_prod ts |
|
75 |
else mk_times (t, mk_prod ts); |
|
76 |
||
35267
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents:
35092
diff
changeset
|
77 |
val dest_times = HOLogic.dest_bin @{const_name Groups.times} HOLogic.natT; |
23164 | 78 |
|
79 |
fun dest_prod t = |
|
80 |
let val (t,u) = dest_times t |
|
81 |
in dest_prod t @ dest_prod u end |
|
82 |
handle TERM _ => [t]; |
|
83 |
||
84 |
(*DON'T do the obvious simplifications; that would create special cases*) |
|
85 |
fun mk_coeff (k,t) = mk_times (mk_number k, t); |
|
86 |
||
87 |
(*Express t as a product of (possibly) a numeral with other factors, sorted*) |
|
88 |
fun dest_coeff t = |
|
35408 | 89 |
let val ts = sort Term_Ord.term_ord (dest_prod t) |
23164 | 90 |
val (n, _, ts') = find_first_numeral [] ts |
91 |
handle TERM _ => (1, one, ts) |
|
92 |
in (n, mk_prod ts') end; |
|
93 |
||
94 |
(*Find first coefficient-term THAT MATCHES u*) |
|
95 |
fun find_first_coeff past u [] = raise TERM("find_first_coeff", []) |
|
96 |
| find_first_coeff past u (t::terms) = |
|
97 |
let val (n,u') = dest_coeff t |
|
98 |
in if u aconv u' then (n, rev past @ terms) |
|
99 |
else find_first_coeff (t::past) u terms |
|
100 |
end |
|
101 |
handle TERM _ => find_first_coeff (t::past) u terms; |
|
102 |
||
103 |
||
104 |
(*Split up a sum into the list of its constituent terms, on the way removing any |
|
105 |
Sucs and counting them.*) |
|
37388 | 106 |
fun dest_Suc_sum (Const (@{const_name Suc}, _) $ t, (k,ts)) = dest_Suc_sum (t, (k+1,ts)) |
23164 | 107 |
| dest_Suc_sum (t, (k,ts)) = |
108 |
let val (t1,t2) = dest_plus t |
|
109 |
in dest_Suc_sum (t1, dest_Suc_sum (t2, (k,ts))) end |
|
110 |
handle TERM _ => (k, t::ts); |
|
111 |
||
112 |
(*Code for testing whether numerals are already used in the goal*) |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
25481
diff
changeset
|
113 |
fun is_numeral (Const(@{const_name Int.number_of}, _) $ w) = true |
23164 | 114 |
| is_numeral _ = false; |
115 |
||
116 |
fun prod_has_numeral t = exists is_numeral (dest_prod t); |
|
117 |
||
118 |
(*The Sucs found in the term are converted to a binary numeral. If relaxed is false, |
|
119 |
an exception is raised unless the original expression contains at least one |
|
120 |
numeral in a coefficient position. This prevents nat_combine_numerals from |
|
121 |
introducing numerals to goals.*) |
|
122 |
fun dest_Sucs_sum relaxed t = |
|
123 |
let val (k,ts) = dest_Suc_sum (t,(0,[])) |
|
124 |
in |
|
125 |
if relaxed orelse exists prod_has_numeral ts then |
|
126 |
if k=0 then ts |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24431
diff
changeset
|
127 |
else mk_number k :: ts |
23164 | 128 |
else raise TERM("Nat_Numeral_Simprocs.dest_Sucs_sum", [t]) |
129 |
end; |
|
130 |
||
131 |
||
132 |
(*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
|
133 |
val add_0s = map rename_numerals [@{thm Nat.add_0}, @{thm Nat.add_0_right}]; |
23164 | 134 |
val mult_1s = map rename_numerals [@{thm nat_mult_1}, @{thm nat_mult_1_right}]; |
135 |
||
136 |
(*Final simplification: cancel + and *; replace Numeral0 by 0 and Numeral1 by 1*) |
|
137 |
||
138 |
(*And these help the simproc return False when appropriate, which helps |
|
139 |
the arith prover.*) |
|
23881 | 140 |
val contra_rules = [@{thm add_Suc}, @{thm add_Suc_right}, @{thm Zero_not_Suc}, |
141 |
@{thm Suc_not_Zero}, @{thm le_0_eq}]; |
|
23164 | 142 |
|
143 |
val simplify_meta_eq = |
|
30518 | 144 |
Arith_Data.simplify_meta_eq |
35064
1bdef0c013d3
hide fact names clashing with fact names from Group.thy
haftmann
parents:
35050
diff
changeset
|
145 |
([@{thm nat_numeral_0_eq_0}, @{thm numeral_1_eq_Suc_0}, @{thm Nat.add_0}, @{thm Nat.add_0_right}, |
23471 | 146 |
@{thm mult_0}, @{thm mult_0_right}, @{thm mult_1}, @{thm mult_1_right}] @ contra_rules); |
23164 | 147 |
|
148 |
||
149 |
(*** Applying CancelNumeralsFun ***) |
|
150 |
||
151 |
structure CancelNumeralsCommon = |
|
44945 | 152 |
struct |
153 |
val mk_sum = (fn T : typ => mk_sum) |
|
154 |
val dest_sum = dest_Sucs_sum true |
|
155 |
val mk_coeff = mk_coeff |
|
156 |
val dest_coeff = dest_coeff |
|
157 |
val find_first_coeff = find_first_coeff [] |
|
44947
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
haftmann
parents:
44945
diff
changeset
|
158 |
val trans_tac = Numeral_Simprocs.trans_tac |
23164 | 159 |
|
31068
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents:
30685
diff
changeset
|
160 |
val norm_ss1 = Numeral_Simprocs.num_ss addsimps numeral_syms @ add_0s @ mult_1s @ |
31790 | 161 |
[@{thm Suc_eq_plus1_left}] @ @{thms add_ac} |
31068
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents:
30685
diff
changeset
|
162 |
val norm_ss2 = Numeral_Simprocs.num_ss addsimps bin_simps @ @{thms add_ac} @ @{thms mult_ac} |
23164 | 163 |
fun norm_tac ss = |
164 |
ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1)) |
|
165 |
THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2)) |
|
166 |
||
167 |
val numeral_simp_ss = HOL_ss addsimps add_0s @ bin_simps; |
|
168 |
fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss)); |
|
169 |
val simplify_meta_eq = simplify_meta_eq |
|
44945 | 170 |
val prove_conv = Arith_Data.prove_conv |
171 |
end; |
|
23164 | 172 |
|
173 |
structure EqCancelNumerals = CancelNumeralsFun |
|
174 |
(open CancelNumeralsCommon |
|
175 |
val mk_bal = HOLogic.mk_eq |
|
38864
4abe644fcea5
formerly unnamed infix equality now named HOL.eq
haftmann
parents:
37388
diff
changeset
|
176 |
val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} HOLogic.natT |
23471 | 177 |
val bal_add1 = @{thm nat_eq_add_iff1} RS trans |
178 |
val bal_add2 = @{thm nat_eq_add_iff2} RS trans |
|
23164 | 179 |
); |
180 |
||
181 |
structure LessCancelNumerals = CancelNumeralsFun |
|
182 |
(open CancelNumeralsCommon |
|
35092
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
haftmann
parents:
35064
diff
changeset
|
183 |
val mk_bal = HOLogic.mk_binrel @{const_name Orderings.less} |
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
haftmann
parents:
35064
diff
changeset
|
184 |
val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} HOLogic.natT |
23471 | 185 |
val bal_add1 = @{thm nat_less_add_iff1} RS trans |
186 |
val bal_add2 = @{thm nat_less_add_iff2} RS trans |
|
23164 | 187 |
); |
188 |
||
189 |
structure LeCancelNumerals = CancelNumeralsFun |
|
190 |
(open CancelNumeralsCommon |
|
35092
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
haftmann
parents:
35064
diff
changeset
|
191 |
val mk_bal = HOLogic.mk_binrel @{const_name Orderings.less_eq} |
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
haftmann
parents:
35064
diff
changeset
|
192 |
val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} HOLogic.natT |
23471 | 193 |
val bal_add1 = @{thm nat_le_add_iff1} RS trans |
194 |
val bal_add2 = @{thm nat_le_add_iff2} RS trans |
|
23164 | 195 |
); |
196 |
||
197 |
structure DiffCancelNumerals = CancelNumeralsFun |
|
198 |
(open CancelNumeralsCommon |
|
35267
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents:
35092
diff
changeset
|
199 |
val mk_bal = HOLogic.mk_binop @{const_name Groups.minus} |
8dfd816713c6
moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents:
35092
diff
changeset
|
200 |
val dest_bal = HOLogic.dest_bin @{const_name Groups.minus} HOLogic.natT |
23471 | 201 |
val bal_add1 = @{thm nat_diff_add_eq1} RS trans |
202 |
val bal_add2 = @{thm nat_diff_add_eq2} RS trans |
|
23164 | 203 |
); |
204 |
||
45436
62bc9474d04b
use simproc_setup for some nat_numeral simprocs; add simproc tests
huffman
parents:
45306
diff
changeset
|
205 |
fun eq_cancel_numerals ss ct = EqCancelNumerals.proc ss (term_of ct) |
62bc9474d04b
use simproc_setup for some nat_numeral simprocs; add simproc tests
huffman
parents:
45306
diff
changeset
|
206 |
fun less_cancel_numerals ss ct = LessCancelNumerals.proc ss (term_of ct) |
62bc9474d04b
use simproc_setup for some nat_numeral simprocs; add simproc tests
huffman
parents:
45306
diff
changeset
|
207 |
fun le_cancel_numerals ss ct = LeCancelNumerals.proc ss (term_of ct) |
62bc9474d04b
use simproc_setup for some nat_numeral simprocs; add simproc tests
huffman
parents:
45306
diff
changeset
|
208 |
fun diff_cancel_numerals ss ct = DiffCancelNumerals.proc ss (term_of ct) |
23164 | 209 |
|
210 |
||
211 |
(*** Applying CombineNumeralsFun ***) |
|
212 |
||
213 |
structure CombineNumeralsData = |
|
44945 | 214 |
struct |
215 |
type coeff = int |
|
216 |
val iszero = (fn x => x = 0) |
|
217 |
val add = op + |
|
218 |
val mk_sum = (fn T : typ => long_mk_sum) (*to work for 2*x + 3*x *) |
|
219 |
val dest_sum = dest_Sucs_sum false |
|
220 |
val mk_coeff = mk_coeff |
|
221 |
val dest_coeff = dest_coeff |
|
222 |
val left_distrib = @{thm left_add_mult_distrib} RS trans |
|
223 |
val prove_conv = Arith_Data.prove_conv_nohyps |
|
44947
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
haftmann
parents:
44945
diff
changeset
|
224 |
val trans_tac = Numeral_Simprocs.trans_tac |
23164 | 225 |
|
31790 | 226 |
val norm_ss1 = Numeral_Simprocs.num_ss addsimps numeral_syms @ add_0s @ mult_1s @ [@{thm Suc_eq_plus1}] @ @{thms add_ac} |
31068
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents:
30685
diff
changeset
|
227 |
val norm_ss2 = Numeral_Simprocs.num_ss addsimps bin_simps @ @{thms add_ac} @ @{thms mult_ac} |
23164 | 228 |
fun norm_tac ss = |
229 |
ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1)) |
|
230 |
THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2)) |
|
231 |
||
232 |
val numeral_simp_ss = HOL_ss addsimps add_0s @ bin_simps; |
|
233 |
fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss)) |
|
44945 | 234 |
val simplify_meta_eq = simplify_meta_eq |
235 |
end; |
|
23164 | 236 |
|
237 |
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData); |
|
238 |
||
45462
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
huffman
parents:
45436
diff
changeset
|
239 |
fun combine_numerals ss ct = CombineNumerals.proc ss (term_of ct) |
23164 | 240 |
|
241 |
||
242 |
(*** Applying CancelNumeralFactorFun ***) |
|
243 |
||
244 |
structure CancelNumeralFactorCommon = |
|
44945 | 245 |
struct |
246 |
val mk_coeff = mk_coeff |
|
247 |
val dest_coeff = dest_coeff |
|
44947
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
haftmann
parents:
44945
diff
changeset
|
248 |
val trans_tac = Numeral_Simprocs.trans_tac |
23164 | 249 |
|
31068
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents:
30685
diff
changeset
|
250 |
val norm_ss1 = Numeral_Simprocs.num_ss addsimps |
31790 | 251 |
numeral_syms @ add_0s @ mult_1s @ [@{thm Suc_eq_plus1_left}] @ @{thms add_ac} |
31068
f591144b0f17
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents:
30685
diff
changeset
|
252 |
val norm_ss2 = Numeral_Simprocs.num_ss addsimps bin_simps @ @{thms add_ac} @ @{thms mult_ac} |
23164 | 253 |
fun norm_tac ss = |
254 |
ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1)) |
|
255 |
THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2)) |
|
256 |
||
257 |
val numeral_simp_ss = HOL_ss addsimps bin_simps |
|
258 |
fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss)) |
|
44945 | 259 |
val simplify_meta_eq = simplify_meta_eq |
260 |
val prove_conv = Arith_Data.prove_conv |
|
261 |
end; |
|
23164 | 262 |
|
263 |
structure DivCancelNumeralFactor = CancelNumeralFactorFun |
|
264 |
(open CancelNumeralFactorCommon |
|
265 |
val mk_bal = HOLogic.mk_binop @{const_name Divides.div} |
|
266 |
val dest_bal = HOLogic.dest_bin @{const_name Divides.div} HOLogic.natT |
|
23471 | 267 |
val cancel = @{thm nat_mult_div_cancel1} RS trans |
23164 | 268 |
val neg_exchanges = false |
44945 | 269 |
); |
23164 | 270 |
|
23969 | 271 |
structure DvdCancelNumeralFactor = CancelNumeralFactorFun |
272 |
(open CancelNumeralFactorCommon |
|
35050
9f841f20dca6
renamed OrderedGroup to Groups; split theory Ring_and_Field into Rings Fields
haftmann
parents:
34974
diff
changeset
|
273 |
val mk_bal = HOLogic.mk_binrel @{const_name Rings.dvd} |
9f841f20dca6
renamed OrderedGroup to Groups; split theory Ring_and_Field into Rings Fields
haftmann
parents:
34974
diff
changeset
|
274 |
val dest_bal = HOLogic.dest_bin @{const_name Rings.dvd} HOLogic.natT |
23969 | 275 |
val cancel = @{thm nat_mult_dvd_cancel1} RS trans |
276 |
val neg_exchanges = false |
|
44945 | 277 |
); |
23969 | 278 |
|
23164 | 279 |
structure EqCancelNumeralFactor = CancelNumeralFactorFun |
280 |
(open CancelNumeralFactorCommon |
|
281 |
val mk_bal = HOLogic.mk_eq |
|
38864
4abe644fcea5
formerly unnamed infix equality now named HOL.eq
haftmann
parents:
37388
diff
changeset
|
282 |
val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} HOLogic.natT |
23471 | 283 |
val cancel = @{thm nat_mult_eq_cancel1} RS trans |
23164 | 284 |
val neg_exchanges = false |
44945 | 285 |
); |
23164 | 286 |
|
287 |
structure LessCancelNumeralFactor = CancelNumeralFactorFun |
|
288 |
(open CancelNumeralFactorCommon |
|
35092
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
haftmann
parents:
35064
diff
changeset
|
289 |
val mk_bal = HOLogic.mk_binrel @{const_name Orderings.less} |
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
haftmann
parents:
35064
diff
changeset
|
290 |
val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} HOLogic.natT |
23471 | 291 |
val cancel = @{thm nat_mult_less_cancel1} RS trans |
23164 | 292 |
val neg_exchanges = true |
44945 | 293 |
); |
23164 | 294 |
|
295 |
structure LeCancelNumeralFactor = CancelNumeralFactorFun |
|
296 |
(open CancelNumeralFactorCommon |
|
35092
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
haftmann
parents:
35064
diff
changeset
|
297 |
val mk_bal = HOLogic.mk_binrel @{const_name Orderings.less_eq} |
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
haftmann
parents:
35064
diff
changeset
|
298 |
val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} HOLogic.natT |
23471 | 299 |
val cancel = @{thm nat_mult_le_cancel1} RS trans |
23164 | 300 |
val neg_exchanges = true |
301 |
) |
|
302 |
||
303 |
val cancel_numeral_factors = |
|
44945 | 304 |
map (Numeral_Simprocs.prep_simproc @{theory}) |
23164 | 305 |
[("nateq_cancel_numeral_factors", |
306 |
["(l::nat) * m = n", "(l::nat) = m * n"], |
|
307 |
K EqCancelNumeralFactor.proc), |
|
308 |
("natless_cancel_numeral_factors", |
|
309 |
["(l::nat) * m < n", "(l::nat) < m * n"], |
|
310 |
K LessCancelNumeralFactor.proc), |
|
311 |
("natle_cancel_numeral_factors", |
|
312 |
["(l::nat) * m <= n", "(l::nat) <= m * n"], |
|
313 |
K LeCancelNumeralFactor.proc), |
|
314 |
("natdiv_cancel_numeral_factors", |
|
315 |
["((l::nat) * m) div n", "(l::nat) div (m * n)"], |
|
23969 | 316 |
K DivCancelNumeralFactor.proc), |
317 |
("natdvd_cancel_numeral_factors", |
|
318 |
["((l::nat) * m) dvd n", "(l::nat) dvd (m * n)"], |
|
319 |
K DvdCancelNumeralFactor.proc)]; |
|
23164 | 320 |
|
321 |
||
322 |
||
323 |
(*** Applying ExtractCommonTermFun ***) |
|
324 |
||
325 |
(*this version ALWAYS includes a trailing one*) |
|
326 |
fun long_mk_prod [] = one |
|
327 |
| long_mk_prod (t :: ts) = mk_times (t, mk_prod ts); |
|
328 |
||
329 |
(*Find first term that matches u*) |
|
330 |
fun find_first_t past u [] = raise TERM("find_first_t", []) |
|
331 |
| find_first_t past u (t::terms) = |
|
332 |
if u aconv t then (rev past @ terms) |
|
333 |
else find_first_t (t::past) u terms |
|
334 |
handle TERM _ => find_first_t (t::past) u terms; |
|
335 |
||
336 |
(** Final simplification for the CancelFactor simprocs **) |
|
30518 | 337 |
val simplify_one = Arith_Data.simplify_meta_eq |
23164 | 338 |
[@{thm mult_1_left}, @{thm mult_1_right}, @{thm div_1}, @{thm numeral_1_eq_Suc_0}]; |
339 |
||
30649
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
nipkow
parents:
30518
diff
changeset
|
340 |
fun cancel_simplify_meta_eq ss cancel_th th = |
23164 | 341 |
simplify_one ss (([th, cancel_th]) MRS trans); |
342 |
||
343 |
structure CancelFactorCommon = |
|
44945 | 344 |
struct |
345 |
val mk_sum = (fn T : typ => long_mk_prod) |
|
346 |
val dest_sum = dest_prod |
|
347 |
val mk_coeff = mk_coeff |
|
348 |
val dest_coeff = dest_coeff |
|
349 |
val find_first = find_first_t [] |
|
44947
8ae418dfe561
dropped unused argument – avoids problem with SML/NJ
haftmann
parents:
44945
diff
changeset
|
350 |
val trans_tac = Numeral_Simprocs.trans_tac |
23881 | 351 |
val norm_ss = HOL_ss addsimps mult_1s @ @{thms mult_ac} |
23164 | 352 |
fun norm_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss)) |
30649
57753e0ec1d4
1. New cancellation simprocs for common factors in inequations
nipkow
parents:
30518
diff
changeset
|
353 |
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
|
354 |
fun mk_eq (a, b) = HOLogic.mk_Trueprop (HOLogic.mk_eq (a, b)) |
44945 | 355 |
end; |
23164 | 356 |
|
357 |
structure EqCancelFactor = ExtractCommonTermFun |
|
358 |
(open CancelFactorCommon |
|
359 |
val mk_bal = HOLogic.mk_eq |
|
38864
4abe644fcea5
formerly unnamed infix equality now named HOL.eq
haftmann
parents:
37388
diff
changeset
|
360 |
val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} HOLogic.natT |
31368 | 361 |
fun simp_conv _ _ = SOME @{thm nat_mult_eq_cancel_disj} |
23164 | 362 |
); |
363 |
||
44945 | 364 |
structure LeCancelFactor = ExtractCommonTermFun |
365 |
(open CancelFactorCommon |
|
366 |
val mk_bal = HOLogic.mk_binrel @{const_name Orderings.less_eq} |
|
367 |
val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} HOLogic.natT |
|
368 |
fun simp_conv _ _ = SOME @{thm nat_mult_le_cancel_disj} |
|
369 |
); |
|
370 |
||
23164 | 371 |
structure LessCancelFactor = ExtractCommonTermFun |
372 |
(open CancelFactorCommon |
|
35092
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
haftmann
parents:
35064
diff
changeset
|
373 |
val mk_bal = HOLogic.mk_binrel @{const_name Orderings.less} |
cfe605c54e50
moved less_eq, less to Orderings.thy; moved abs, sgn to Groups.thy
haftmann
parents:
35064
diff
changeset
|
374 |
val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} HOLogic.natT |
31368 | 375 |
fun simp_conv _ _ = SOME @{thm nat_mult_less_cancel_disj} |
23164 | 376 |
); |
377 |
||
378 |
structure DivideCancelFactor = ExtractCommonTermFun |
|
379 |
(open CancelFactorCommon |
|
380 |
val mk_bal = HOLogic.mk_binop @{const_name Divides.div} |
|
381 |
val dest_bal = HOLogic.dest_bin @{const_name Divides.div} HOLogic.natT |
|
31368 | 382 |
fun simp_conv _ _ = SOME @{thm nat_mult_div_cancel_disj} |
23164 | 383 |
); |
384 |
||
23969 | 385 |
structure DvdCancelFactor = ExtractCommonTermFun |
386 |
(open CancelFactorCommon |
|
35050
9f841f20dca6
renamed OrderedGroup to Groups; split theory Ring_and_Field into Rings Fields
haftmann
parents:
34974
diff
changeset
|
387 |
val mk_bal = HOLogic.mk_binrel @{const_name Rings.dvd} |
9f841f20dca6
renamed OrderedGroup to Groups; split theory Ring_and_Field into Rings Fields
haftmann
parents:
34974
diff
changeset
|
388 |
val dest_bal = HOLogic.dest_bin @{const_name Rings.dvd} HOLogic.natT |
31368 | 389 |
fun simp_conv _ _ = SOME @{thm nat_mult_dvd_cancel_disj} |
23969 | 390 |
); |
391 |
||
45462
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
huffman
parents:
45436
diff
changeset
|
392 |
fun eq_cancel_factor ss ct = EqCancelFactor.proc ss (term_of ct) |
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
huffman
parents:
45436
diff
changeset
|
393 |
fun less_cancel_factor ss ct = LessCancelFactor.proc ss (term_of ct) |
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
huffman
parents:
45436
diff
changeset
|
394 |
fun le_cancel_factor ss ct = LeCancelFactor.proc ss (term_of ct) |
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
huffman
parents:
45436
diff
changeset
|
395 |
fun divide_cancel_factor ss ct = DivideCancelFactor.proc ss (term_of ct) |
aba629d6cee5
use simproc_setup for more nat_numeral simprocs; add simproc tests
huffman
parents:
45436
diff
changeset
|
396 |
fun dvd_cancel_factor ss ct = DvdCancelFactor.proc ss (term_of ct) |
23164 | 397 |
|
398 |
end; |
|
399 |
||
400 |
||
401 |
Addsimprocs Nat_Numeral_Simprocs.cancel_numeral_factors; |
|
402 |
||
403 |
||
404 |
(*examples: |
|
405 |
print_depth 22; |
|
406 |
set timing; |
|
40878
7695e4de4d86
renamed trace_simp to simp_trace, and debug_simp to simp_debug;
wenzelm
parents:
38864
diff
changeset
|
407 |
set simp_trace; |
23164 | 408 |
fun test s = (Goal s; by (Simp_tac 1)); |
409 |
||
410 |
(*cancel_numeral_factors*) |
|
411 |
test "9*x = 12 * (y::nat)"; |
|
412 |
test "(9*x) div (12 * (y::nat)) = z"; |
|
413 |
test "9*x < 12 * (y::nat)"; |
|
414 |
test "9*x <= 12 * (y::nat)"; |
|
415 |
||
416 |
*) |