|
7707
|
1 |
(* Title: HOL/Integ/IntArith.thy
|
|
|
2 |
ID: $Id$
|
|
|
3 |
Authors: Larry Paulson and Tobias Nipkow
|
|
|
4 |
|
|
|
5 |
Simprocs and decision procedure for linear arithmetic.
|
|
|
6 |
*)
|
|
|
7 |
|
|
|
8 |
(*** Simprocs for numeric literals ***)
|
|
|
9 |
|
|
|
10 |
(** Combining of literal coefficients in sums of products **)
|
|
|
11 |
|
|
|
12 |
Goal "(x < y) = (x-y < (#0::int))";
|
|
|
13 |
by (simp_tac (simpset() addsimps zcompare_rls) 1);
|
|
|
14 |
qed "zless_iff_zdiff_zless_0";
|
|
|
15 |
|
|
|
16 |
Goal "(x = y) = (x-y = (#0::int))";
|
|
|
17 |
by (simp_tac (simpset() addsimps zcompare_rls) 1);
|
|
|
18 |
qed "eq_iff_zdiff_eq_0";
|
|
|
19 |
|
|
|
20 |
Goal "(x <= y) = (x-y <= (#0::int))";
|
|
|
21 |
by (simp_tac (simpset() addsimps zcompare_rls) 1);
|
|
|
22 |
qed "zle_iff_zdiff_zle_0";
|
|
|
23 |
|
|
|
24 |
|
|
8785
|
25 |
(** For combine_numerals **)
|
|
|
26 |
|
|
|
27 |
Goal "i*u + (j*u + k) = (i+j)*u + (k::int)";
|
|
|
28 |
by (asm_simp_tac (simpset() addsimps [zadd_zmult_distrib]) 1);
|
|
|
29 |
qed "left_zadd_zmult_distrib";
|
|
|
30 |
|
|
|
31 |
|
|
8763
|
32 |
(** For cancel_numerals **)
|
|
|
33 |
|
|
|
34 |
Goal "!!i::int. ((i*u + m) - (j*u + n)) = (((i-j)*u + m) - n)";
|
|
|
35 |
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]) 1);
|
|
|
36 |
qed "diff_add_eq1";
|
|
|
37 |
|
|
|
38 |
Goal "!!i::int. ((i*u + m) - (j*u + n)) = (m - ((j-i)*u + n))";
|
|
|
39 |
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]) 1);
|
|
|
40 |
qed "diff_add_eq2";
|
|
|
41 |
|
|
|
42 |
val rel_iff_rel_0_rls = map (inst "y" "?u+?v")
|
|
|
43 |
[zless_iff_zdiff_zless_0, eq_iff_zdiff_eq_0,
|
|
|
44 |
zle_iff_zdiff_zle_0] @
|
|
|
45 |
map (inst "y" "n")
|
|
|
46 |
[zless_iff_zdiff_zless_0, eq_iff_zdiff_eq_0,
|
|
|
47 |
zle_iff_zdiff_zle_0];
|
|
|
48 |
|
|
|
49 |
Goal "!!i::int. (i*u + m = j*u + n) = ((i-j)*u + m = n)";
|
|
|
50 |
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
|
|
|
51 |
zadd_ac@rel_iff_rel_0_rls) 1);
|
|
|
52 |
qed "eq_add_iff1";
|
|
|
53 |
|
|
|
54 |
Goal "!!i::int. (i*u + m = j*u + n) = (m = (j-i)*u + n)";
|
|
|
55 |
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
|
|
|
56 |
zadd_ac@rel_iff_rel_0_rls) 1);
|
|
|
57 |
qed "eq_add_iff2";
|
|
|
58 |
|
|
|
59 |
Goal "!!i::int. (i*u + m < j*u + n) = ((i-j)*u + m < n)";
|
|
|
60 |
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
|
|
|
61 |
zadd_ac@rel_iff_rel_0_rls) 1);
|
|
|
62 |
qed "less_add_iff1";
|
|
|
63 |
|
|
|
64 |
Goal "!!i::int. (i*u + m < j*u + n) = (m < (j-i)*u + n)";
|
|
|
65 |
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
|
|
|
66 |
zadd_ac@rel_iff_rel_0_rls) 1);
|
|
|
67 |
qed "less_add_iff2";
|
|
|
68 |
|
|
|
69 |
Goal "!!i::int. (i*u + m <= j*u + n) = ((i-j)*u + m <= n)";
|
|
|
70 |
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
|
|
|
71 |
zadd_ac@rel_iff_rel_0_rls) 1);
|
|
|
72 |
qed "le_add_iff1";
|
|
|
73 |
|
|
|
74 |
Goal "!!i::int. (i*u + m <= j*u + n) = (m <= (j-i)*u + n)";
|
|
|
75 |
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]
|
|
|
76 |
@zadd_ac@rel_iff_rel_0_rls) 1);
|
|
|
77 |
qed "le_add_iff2";
|
|
|
78 |
|
|
|
79 |
|
|
|
80 |
structure Int_Numeral_Simprocs =
|
|
|
81 |
struct
|
|
|
82 |
|
|
|
83 |
(*Utilities*)
|
|
|
84 |
|
|
|
85 |
fun mk_numeral n = HOLogic.number_of_const HOLogic.intT $
|
|
|
86 |
NumeralSyntax.mk_bin n;
|
|
|
87 |
|
|
|
88 |
(*Decodes a binary INTEGER*)
|
|
8785
|
89 |
fun dest_numeral (Const("Numeral.number_of", _) $ w) =
|
|
|
90 |
(NumeralSyntax.dest_bin w
|
|
|
91 |
handle Match => raise TERM("Int_Numeral_Simprocs.dest_numeral:1", [w]))
|
|
|
92 |
| dest_numeral t = raise TERM("Int_Numeral_Simprocs.dest_numeral:2", [t]);
|
|
8763
|
93 |
|
|
|
94 |
fun find_first_numeral past (t::terms) =
|
|
|
95 |
((dest_numeral t, rev past @ terms)
|
|
|
96 |
handle TERM _ => find_first_numeral (t::past) terms)
|
|
|
97 |
| find_first_numeral past [] = raise TERM("find_first_numeral", []);
|
|
|
98 |
|
|
|
99 |
val zero = mk_numeral 0;
|
|
|
100 |
val mk_plus = HOLogic.mk_binop "op +";
|
|
|
101 |
|
|
|
102 |
val uminus_const = Const ("uminus", HOLogic.intT --> HOLogic.intT);
|
|
|
103 |
|
|
|
104 |
(*Thus mk_sum[t] yields t+#0; longer sums don't have a trailing zero*)
|
|
|
105 |
fun mk_sum [] = zero
|
|
|
106 |
| mk_sum [t,u] = mk_plus (t, u)
|
|
|
107 |
| mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
|
|
|
108 |
|
|
8785
|
109 |
(*this version ALWAYS includes a trailing zero*)
|
|
|
110 |
fun long_mk_sum [] = zero
|
|
|
111 |
| long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
|
|
|
112 |
|
|
8763
|
113 |
val dest_plus = HOLogic.dest_bin "op +" HOLogic.intT;
|
|
|
114 |
|
|
|
115 |
(*decompose additions AND subtractions as a sum*)
|
|
|
116 |
fun dest_summing (pos, Const ("op +", _) $ t $ u, ts) =
|
|
|
117 |
dest_summing (pos, t, dest_summing (pos, u, ts))
|
|
|
118 |
| dest_summing (pos, Const ("op -", _) $ t $ u, ts) =
|
|
|
119 |
dest_summing (pos, t, dest_summing (not pos, u, ts))
|
|
|
120 |
| dest_summing (pos, t, ts) =
|
|
|
121 |
if pos then t::ts else uminus_const$t :: ts;
|
|
|
122 |
|
|
|
123 |
fun dest_sum t = dest_summing (true, t, []);
|
|
|
124 |
|
|
|
125 |
val mk_diff = HOLogic.mk_binop "op -";
|
|
|
126 |
val dest_diff = HOLogic.dest_bin "op -" HOLogic.intT;
|
|
|
127 |
|
|
|
128 |
val one = mk_numeral 1;
|
|
|
129 |
val mk_times = HOLogic.mk_binop "op *";
|
|
|
130 |
|
|
|
131 |
fun mk_prod [] = one
|
|
|
132 |
| mk_prod [t] = t
|
|
|
133 |
| mk_prod (t :: ts) = if t = one then mk_prod ts
|
|
|
134 |
else mk_times (t, mk_prod ts);
|
|
|
135 |
|
|
|
136 |
val dest_times = HOLogic.dest_bin "op *" HOLogic.intT;
|
|
|
137 |
|
|
|
138 |
fun dest_prod t =
|
|
|
139 |
let val (t,u) = dest_times t
|
|
|
140 |
in dest_prod t @ dest_prod u end
|
|
|
141 |
handle TERM _ => [t];
|
|
|
142 |
|
|
|
143 |
(*DON'T do the obvious simplifications; that would create special cases*)
|
|
|
144 |
fun mk_coeff (k, ts) = mk_times (mk_numeral k, ts);
|
|
|
145 |
|
|
|
146 |
(*Express t as a product of (possibly) a numeral with other sorted terms*)
|
|
|
147 |
fun dest_coeff sign (Const ("uminus", _) $ t) = dest_coeff (~sign) t
|
|
|
148 |
| dest_coeff sign t =
|
|
|
149 |
let val ts = sort Term.term_ord (dest_prod t)
|
|
|
150 |
val (n, ts') = find_first_numeral [] ts
|
|
|
151 |
handle TERM _ => (1, ts)
|
|
|
152 |
in (sign*n, mk_prod ts') end;
|
|
|
153 |
|
|
|
154 |
(*Find first coefficient-term THAT MATCHES u*)
|
|
|
155 |
fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
|
|
|
156 |
| find_first_coeff past u (t::terms) =
|
|
|
157 |
let val (n,u') = dest_coeff 1 t
|
|
|
158 |
in if u aconv u' then (n, rev past @ terms)
|
|
|
159 |
else find_first_coeff (t::past) u terms
|
|
|
160 |
end
|
|
|
161 |
handle TERM _ => find_first_coeff (t::past) u terms;
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
(*Simplify #1*n and n*#1 to n*)
|
|
|
165 |
val add_0s = [zadd_0, zadd_0_right];
|
|
|
166 |
val mult_1s = [zmult_1, zmult_1_right, zmult_minus1, zmult_minus1_right];
|
|
|
167 |
|
|
|
168 |
(*To perform binary arithmetic*)
|
|
|
169 |
val bin_simps = [number_of_add RS sym, add_number_of_left] @
|
|
|
170 |
bin_arith_simps @ bin_rel_simps;
|
|
|
171 |
|
|
8787
|
172 |
(*To evaluate binary negations of coefficients*)
|
|
|
173 |
val zminus_simps = NCons_simps @
|
|
|
174 |
[number_of_minus RS sym,
|
|
|
175 |
bin_minus_1, bin_minus_0, bin_minus_Pls, bin_minus_Min,
|
|
|
176 |
bin_pred_1, bin_pred_0, bin_pred_Pls, bin_pred_Min];
|
|
|
177 |
|
|
8763
|
178 |
(*To let us treat subtraction as addition*)
|
|
|
179 |
val diff_simps = [zdiff_def, zminus_zadd_distrib, zminus_zminus];
|
|
|
180 |
|
|
8776
|
181 |
val def_trans = def_imp_eq RS trans;
|
|
|
182 |
|
|
|
183 |
(*Apply the given rewrite (if present) just once*)
|
|
|
184 |
fun subst_tac None = all_tac
|
|
|
185 |
| subst_tac (Some th) = ALLGOALS (rtac (th RS def_trans));
|
|
|
186 |
|
|
8763
|
187 |
val mk_eqv = HOLogic.mk_Trueprop o HOLogic.mk_eq;
|
|
|
188 |
|
|
8776
|
189 |
fun prove_conv name tacs sg (t, u) =
|
|
8763
|
190 |
if t aconv u then None
|
|
|
191 |
else
|
|
|
192 |
Some
|
|
|
193 |
(mk_meta_eq (prove_goalw_cterm [] (cterm_of sg (mk_eqv (t, u)))
|
|
|
194 |
(K tacs))
|
|
|
195 |
handle ERROR => error
|
|
|
196 |
("The error(s) above occurred while trying to prove " ^
|
|
8776
|
197 |
string_of_cterm (cterm_of sg (mk_eqv (t, u))) ^
|
|
|
198 |
"\nInternal failure of simproc " ^ name));
|
|
8763
|
199 |
|
|
|
200 |
fun prep_simproc (name, pats, proc) = Simplifier.mk_simproc name pats proc;
|
|
|
201 |
fun prep_pat s = Thm.read_cterm (Theory.sign_of Int.thy) (s, HOLogic.termT);
|
|
|
202 |
val prep_pats = map prep_pat;
|
|
|
203 |
|
|
|
204 |
structure CancelNumeralsCommon =
|
|
|
205 |
struct
|
|
|
206 |
val mk_sum = mk_sum
|
|
|
207 |
val dest_sum = dest_sum
|
|
|
208 |
val mk_coeff = mk_coeff
|
|
|
209 |
val dest_coeff = dest_coeff 1
|
|
|
210 |
val find_first_coeff = find_first_coeff []
|
|
8776
|
211 |
val subst_tac = subst_tac
|
|
|
212 |
val norm_tac = ALLGOALS (simp_tac (HOL_ss addsimps add_0s@mult_1s@diff_simps@
|
|
8787
|
213 |
zminus_simps@zadd_ac))
|
|
8763
|
214 |
THEN ALLGOALS
|
|
8776
|
215 |
(simp_tac (HOL_ss addsimps [zmult_zminus_right RS sym]@
|
|
|
216 |
bin_simps@zadd_ac@zmult_ac))
|
|
8763
|
217 |
val numeral_simp_tac = ALLGOALS (simp_tac (HOL_ss addsimps add_0s@bin_simps))
|
|
|
218 |
end;
|
|
|
219 |
|
|
|
220 |
|
|
|
221 |
structure EqCancelNumerals = CancelNumeralsFun
|
|
|
222 |
(open CancelNumeralsCommon
|
|
8776
|
223 |
val prove_conv = prove_conv "inteq_cancel_numerals"
|
|
8763
|
224 |
val mk_bal = HOLogic.mk_eq
|
|
|
225 |
val dest_bal = HOLogic.dest_bin "op =" HOLogic.intT
|
|
8776
|
226 |
val bal_add1 = eq_add_iff1 RS trans
|
|
|
227 |
val bal_add2 = eq_add_iff2 RS trans
|
|
8763
|
228 |
);
|
|
|
229 |
|
|
|
230 |
structure LessCancelNumerals = CancelNumeralsFun
|
|
|
231 |
(open CancelNumeralsCommon
|
|
8776
|
232 |
val prove_conv = prove_conv "intless_cancel_numerals"
|
|
8763
|
233 |
val mk_bal = HOLogic.mk_binrel "op <"
|
|
|
234 |
val dest_bal = HOLogic.dest_bin "op <" HOLogic.intT
|
|
8776
|
235 |
val bal_add1 = less_add_iff1 RS trans
|
|
|
236 |
val bal_add2 = less_add_iff2 RS trans
|
|
8763
|
237 |
);
|
|
|
238 |
|
|
|
239 |
structure LeCancelNumerals = CancelNumeralsFun
|
|
|
240 |
(open CancelNumeralsCommon
|
|
8776
|
241 |
val prove_conv = prove_conv "intle_cancel_numerals"
|
|
8763
|
242 |
val mk_bal = HOLogic.mk_binrel "op <="
|
|
|
243 |
val dest_bal = HOLogic.dest_bin "op <=" HOLogic.intT
|
|
8776
|
244 |
val bal_add1 = le_add_iff1 RS trans
|
|
|
245 |
val bal_add2 = le_add_iff2 RS trans
|
|
8763
|
246 |
);
|
|
|
247 |
|
|
|
248 |
val cancel_numerals =
|
|
|
249 |
map prep_simproc
|
|
|
250 |
[("inteq_cancel_numerals",
|
|
|
251 |
prep_pats ["(l::int) + m = n", "(l::int) = m + n",
|
|
|
252 |
"(l::int) - m = n", "(l::int) = m - n",
|
|
|
253 |
"(l::int) * m = n", "(l::int) = m * n"],
|
|
|
254 |
EqCancelNumerals.proc),
|
|
|
255 |
("intless_cancel_numerals",
|
|
|
256 |
prep_pats ["(l::int) + m < n", "(l::int) < m + n",
|
|
|
257 |
"(l::int) - m < n", "(l::int) < m - n",
|
|
|
258 |
"(l::int) * m < n", "(l::int) < m * n"],
|
|
|
259 |
LessCancelNumerals.proc),
|
|
|
260 |
("intle_cancel_numerals",
|
|
|
261 |
prep_pats ["(l::int) + m <= n", "(l::int) <= m + n",
|
|
|
262 |
"(l::int) - m <= n", "(l::int) <= m - n",
|
|
|
263 |
"(l::int) * m <= n", "(l::int) <= m * n"],
|
|
8787
|
264 |
LeCancelNumerals.proc)];
|
|
8763
|
265 |
|
|
8785
|
266 |
|
|
|
267 |
structure CombineNumeralsData =
|
|
|
268 |
struct
|
|
|
269 |
val mk_sum = long_mk_sum (*to work for e.g. #2*x + #3*x *)
|
|
|
270 |
val dest_sum = dest_sum
|
|
|
271 |
val mk_coeff = mk_coeff
|
|
|
272 |
val dest_coeff = dest_coeff 1
|
|
|
273 |
val left_distrib = left_zadd_zmult_distrib RS trans
|
|
|
274 |
val prove_conv = prove_conv "int_combine_numerals"
|
|
|
275 |
val subst_tac = subst_tac
|
|
|
276 |
val norm_tac = ALLGOALS
|
|
|
277 |
(simp_tac (HOL_ss addsimps add_0s@mult_1s@diff_simps@
|
|
8787
|
278 |
zminus_simps@zadd_ac))
|
|
8785
|
279 |
THEN ALLGOALS
|
|
|
280 |
(simp_tac (HOL_ss addsimps [zmult_zminus_right RS sym]@
|
|
|
281 |
bin_simps@zadd_ac@zmult_ac))
|
|
|
282 |
val numeral_simp_tac = ALLGOALS
|
|
|
283 |
(simp_tac (HOL_ss addsimps add_0s@bin_simps))
|
|
|
284 |
end;
|
|
|
285 |
|
|
|
286 |
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
|
|
|
287 |
|
|
|
288 |
val combine_numerals =
|
|
|
289 |
prep_simproc ("int_combine_numerals",
|
|
8787
|
290 |
prep_pats ["(i::int) + j", "(i::int) - j"],
|
|
8785
|
291 |
CombineNumerals.proc);
|
|
|
292 |
|
|
8763
|
293 |
end;
|
|
|
294 |
|
|
|
295 |
|
|
|
296 |
Addsimprocs Int_Numeral_Simprocs.cancel_numerals;
|
|
8785
|
297 |
Addsimprocs [Int_Numeral_Simprocs.combine_numerals];
|
|
|
298 |
|
|
|
299 |
(*The Abel_Cancel simprocs are now obsolete*)
|
|
|
300 |
Delsimprocs [Int_Cancel.sum_conv, Int_Cancel.rel_conv];
|
|
8763
|
301 |
|
|
|
302 |
(*examples:
|
|
|
303 |
print_depth 22;
|
|
|
304 |
set proof_timing;
|
|
|
305 |
set trace_simp;
|
|
|
306 |
fun test s = (Goal s; by (Simp_tac 1));
|
|
|
307 |
|
|
8785
|
308 |
test "l + #2 + #2 + #2 + (l + #2) + (oo + #2) = (uu::int)";
|
|
|
309 |
|
|
8763
|
310 |
test "#2*u = (u::int)";
|
|
|
311 |
test "(i + j + #12 + (k::int)) - #15 = y";
|
|
|
312 |
test "(i + j + #12 + (k::int)) - #5 = y";
|
|
|
313 |
|
|
|
314 |
test "y - b < (b::int)";
|
|
|
315 |
test "y - (#3*b + c) < (b::int) - #2*c";
|
|
|
316 |
|
|
8785
|
317 |
test "(#2*x - (u*v) + y) - v*#3*u = (w::int)";
|
|
8763
|
318 |
test "(#2*x*u*v + (u*v)*#4 + y) - v*u*#4 = (w::int)";
|
|
|
319 |
test "(#2*x*u*v + (u*v)*#4 + y) - v*u = (w::int)";
|
|
8785
|
320 |
test "u*v - (x*u*v + (u*v)*#4 + y) = (w::int)";
|
|
8763
|
321 |
|
|
|
322 |
test "(i + j + #12 + (k::int)) = u + #15 + y";
|
|
|
323 |
test "(i + j*#2 + #12 + (k::int)) = j + #5 + y";
|
|
|
324 |
|
|
|
325 |
test "#2*y + #3*z + #6*w + #2*y + #3*z + #2*u = #2*y' + #3*z' + #6*w' + #2*y' + #3*z' + u + (vv::int)";
|
|
|
326 |
|
|
8785
|
327 |
test "a + -(b+c) + b = (d::int)";
|
|
|
328 |
test "a + -(b+c) - b = (d::int)";
|
|
|
329 |
|
|
8763
|
330 |
(*negative numerals*)
|
|
|
331 |
test "(i + j + #-2 + (k::int)) - (u + #5 + y) = zz";
|
|
|
332 |
test "(i + j + #-3 + (k::int)) < u + #5 + y";
|
|
|
333 |
test "(i + j + #3 + (k::int)) < u + #-6 + y";
|
|
|
334 |
test "(i + j + #-12 + (k::int)) - #15 = y";
|
|
|
335 |
test "(i + j + #12 + (k::int)) - #-15 = y";
|
|
|
336 |
test "(i + j + #-12 + (k::int)) - #-15 = y";
|
|
|
337 |
*)
|
|
|
338 |
|
|
|
339 |
|
|
7707
|
340 |
(** Constant folding for integer plus and times **)
|
|
|
341 |
|
|
|
342 |
(*We do not need
|
|
8785
|
343 |
structure Nat_Plus_Assoc = Assoc_Fold (Nat_Plus_Assoc_Data);
|
|
7707
|
344 |
structure Int_Plus_Assoc = Assoc_Fold (Int_Plus_Assoc_Data);
|
|
8785
|
345 |
because combine_numerals does the same thing*)
|
|
7707
|
346 |
|
|
|
347 |
structure Int_Times_Assoc_Data : ASSOC_FOLD_DATA =
|
|
|
348 |
struct
|
|
|
349 |
val ss = HOL_ss
|
|
|
350 |
val eq_reflection = eq_reflection
|
|
|
351 |
val thy = Bin.thy
|
|
|
352 |
val T = HOLogic.intT
|
|
|
353 |
val plus = Const ("op *", [HOLogic.intT,HOLogic.intT] ---> HOLogic.intT);
|
|
|
354 |
val add_ac = zmult_ac
|
|
|
355 |
end;
|
|
|
356 |
|
|
|
357 |
structure Int_Times_Assoc = Assoc_Fold (Int_Times_Assoc_Data);
|
|
|
358 |
|
|
|
359 |
Addsimprocs [Int_Times_Assoc.conv];
|
|
|
360 |
|
|
|
361 |
|
|
|
362 |
(** The same for the naturals **)
|
|
|
363 |
|
|
|
364 |
structure Nat_Times_Assoc_Data : ASSOC_FOLD_DATA =
|
|
|
365 |
struct
|
|
|
366 |
val ss = HOL_ss
|
|
|
367 |
val eq_reflection = eq_reflection
|
|
|
368 |
val thy = Bin.thy
|
|
|
369 |
val T = HOLogic.natT
|
|
|
370 |
val plus = Const ("op *", [HOLogic.natT,HOLogic.natT] ---> HOLogic.natT);
|
|
|
371 |
val add_ac = mult_ac
|
|
|
372 |
end;
|
|
|
373 |
|
|
|
374 |
structure Nat_Times_Assoc = Assoc_Fold (Nat_Times_Assoc_Data);
|
|
|
375 |
|
|
8785
|
376 |
Addsimprocs [Nat_Times_Assoc.conv];
|
|
7707
|
377 |
|
|
|
378 |
|
|
|
379 |
|
|
|
380 |
(*** decision procedure for linear arithmetic ***)
|
|
|
381 |
|
|
|
382 |
(*---------------------------------------------------------------------------*)
|
|
|
383 |
(* Linear arithmetic *)
|
|
|
384 |
(*---------------------------------------------------------------------------*)
|
|
|
385 |
|
|
|
386 |
(*
|
|
|
387 |
Instantiation of the generic linear arithmetic package for int.
|
|
|
388 |
*)
|
|
|
389 |
|
|
|
390 |
(* Update parameters of arithmetic prover *)
|
|
|
391 |
let
|
|
|
392 |
|
|
|
393 |
(* reduce contradictory <= to False *)
|
|
|
394 |
val add_rules = simp_thms @ bin_arith_simps @ bin_rel_simps @
|
|
8785
|
395 |
[int_0, zadd_0, zadd_0_right, zdiff_def,
|
|
|
396 |
zadd_zminus_inverse, zadd_zminus_inverse2,
|
|
|
397 |
zmult_0, zmult_0_right,
|
|
|
398 |
zmult_1, zmult_1_right,
|
|
|
399 |
zmult_minus1, zmult_minus1_right];
|
|
7707
|
400 |
|
|
8785
|
401 |
val simprocs = [Int_Times_Assoc.conv, Int_Numeral_Simprocs.combine_numerals]@
|
|
|
402 |
Int_Numeral_Simprocs.cancel_numerals;
|
|
7707
|
403 |
|
|
|
404 |
val add_mono_thms =
|
|
|
405 |
map (fn s => prove_goal Int.thy s
|
|
|
406 |
(fn prems => [cut_facts_tac prems 1,
|
|
|
407 |
asm_simp_tac (simpset() addsimps [zadd_zle_mono]) 1]))
|
|
|
408 |
["(i <= j) & (k <= l) ==> i + k <= j + (l::int)",
|
|
|
409 |
"(i = j) & (k <= l) ==> i + k <= j + (l::int)",
|
|
|
410 |
"(i <= j) & (k = l) ==> i + k <= j + (l::int)",
|
|
|
411 |
"(i = j) & (k = l) ==> i + k = j + (l::int)"
|
|
|
412 |
];
|
|
|
413 |
|
|
|
414 |
in
|
|
|
415 |
LA_Data_Ref.add_mono_thms := !LA_Data_Ref.add_mono_thms @ add_mono_thms;
|
|
|
416 |
LA_Data_Ref.lessD := !LA_Data_Ref.lessD @ [add1_zle_eq RS iffD2];
|
|
|
417 |
LA_Data_Ref.ss_ref := !LA_Data_Ref.ss_ref addsimps add_rules
|
|
8796
|
418 |
addsimprocs simprocs
|
|
|
419 |
addcongs [if_weak_cong];
|
|
7707
|
420 |
LA_Data_Ref.discrete := !LA_Data_Ref.discrete @ [("IntDef.int",true)]
|
|
|
421 |
end;
|
|
|
422 |
|
|
|
423 |
let
|
|
|
424 |
val int_arith_simproc_pats =
|
|
|
425 |
map (fn s => Thm.read_cterm (Theory.sign_of Int.thy) (s, HOLogic.boolT))
|
|
|
426 |
["(m::int) < n","(m::int) <= n", "(m::int) = n"];
|
|
|
427 |
|
|
|
428 |
val fast_int_arith_simproc = mk_simproc
|
|
|
429 |
"fast_int_arith" int_arith_simproc_pats Fast_Arith.lin_arith_prover;
|
|
|
430 |
in
|
|
|
431 |
Addsimprocs [fast_int_arith_simproc]
|
|
|
432 |
end;
|
|
|
433 |
|
|
|
434 |
(* Some test data
|
|
|
435 |
Goal "!!a::int. [| a <= b; c <= d; x+y<z |] ==> a+c <= b+d";
|
|
|
436 |
by (fast_arith_tac 1);
|
|
|
437 |
Goal "!!a::int. [| a < b; c < d |] ==> a-d+ #2 <= b+(-c)";
|
|
|
438 |
by (fast_arith_tac 1);
|
|
|
439 |
Goal "!!a::int. [| a < b; c < d |] ==> a+c+ #1 < b+d";
|
|
|
440 |
by (fast_arith_tac 1);
|
|
|
441 |
Goal "!!a::int. [| a <= b; b+b <= c |] ==> a+a <= c";
|
|
|
442 |
by (fast_arith_tac 1);
|
|
|
443 |
Goal "!!a::int. [| a+b <= i+j; a<=b; i<=j |] \
|
|
|
444 |
\ ==> a+a <= j+j";
|
|
|
445 |
by (fast_arith_tac 1);
|
|
|
446 |
Goal "!!a::int. [| a+b < i+j; a<b; i<j |] \
|
|
|
447 |
\ ==> a+a - - #-1 < j+j - #3";
|
|
|
448 |
by (fast_arith_tac 1);
|
|
|
449 |
Goal "!!a::int. a+b+c <= i+j+k & a<=b & b<=c & i<=j & j<=k --> a+a+a <= k+k+k";
|
|
|
450 |
by (arith_tac 1);
|
|
|
451 |
Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
|
|
|
452 |
\ ==> a <= l";
|
|
|
453 |
by (fast_arith_tac 1);
|
|
|
454 |
Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
|
|
|
455 |
\ ==> a+a+a+a <= l+l+l+l";
|
|
|
456 |
by (fast_arith_tac 1);
|
|
|
457 |
Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
|
|
|
458 |
\ ==> a+a+a+a+a <= l+l+l+l+i";
|
|
|
459 |
by (fast_arith_tac 1);
|
|
|
460 |
Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
|
|
|
461 |
\ ==> a+a+a+a+a+a <= l+l+l+l+i+l";
|
|
|
462 |
by (fast_arith_tac 1);
|
|
8257
|
463 |
Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
|
|
|
464 |
\ ==> #6*a <= #5*l+i";
|
|
|
465 |
by (fast_arith_tac 1);
|
|
7707
|
466 |
*)
|
|
|
467 |
|
|
|
468 |
(*---------------------------------------------------------------------------*)
|
|
|
469 |
(* End of linear arithmetic *)
|
|
|
470 |
(*---------------------------------------------------------------------------*)
|
|
|
471 |
|
|
|
472 |
(** Simplification of inequalities involving numerical constants **)
|
|
|
473 |
|
|
|
474 |
Goal "(w <= z + (#1::int)) = (w<=z | w = z + (#1::int))";
|
|
|
475 |
by (arith_tac 1);
|
|
|
476 |
qed "zle_add1_eq";
|
|
|
477 |
|
|
|
478 |
Goal "(w <= z - (#1::int)) = (w<(z::int))";
|
|
|
479 |
by (arith_tac 1);
|
|
|
480 |
qed "zle_diff1_eq";
|
|
|
481 |
Addsimps [zle_diff1_eq];
|
|
|
482 |
|
|
|
483 |
(*2nd premise can be proved automatically if v is a literal*)
|
|
|
484 |
Goal "[| w <= z; #0 <= v |] ==> w <= z + (v::int)";
|
|
|
485 |
by (fast_arith_tac 1);
|
|
|
486 |
qed "zle_imp_zle_zadd";
|
|
|
487 |
|
|
|
488 |
Goal "w <= z ==> w <= z + (#1::int)";
|
|
|
489 |
by (fast_arith_tac 1);
|
|
|
490 |
qed "zle_imp_zle_zadd1";
|
|
|
491 |
|
|
|
492 |
(*2nd premise can be proved automatically if v is a literal*)
|
|
|
493 |
Goal "[| w < z; #0 <= v |] ==> w < z + (v::int)";
|
|
|
494 |
by (fast_arith_tac 1);
|
|
|
495 |
qed "zless_imp_zless_zadd";
|
|
|
496 |
|
|
|
497 |
Goal "w < z ==> w < z + (#1::int)";
|
|
|
498 |
by (fast_arith_tac 1);
|
|
|
499 |
qed "zless_imp_zless_zadd1";
|
|
|
500 |
|
|
|
501 |
Goal "(w < z + #1) = (w<=(z::int))";
|
|
|
502 |
by (arith_tac 1);
|
|
|
503 |
qed "zle_add1_eq_le";
|
|
|
504 |
Addsimps [zle_add1_eq_le];
|
|
|
505 |
|
|
|
506 |
Goal "(z = z + w) = (w = (#0::int))";
|
|
|
507 |
by (arith_tac 1);
|
|
|
508 |
qed "zadd_left_cancel0";
|
|
|
509 |
Addsimps [zadd_left_cancel0];
|
|
|
510 |
|
|
|
511 |
(*LOOPS as a simprule!*)
|
|
|
512 |
Goal "[| w + v < z; #0 <= v |] ==> w < (z::int)";
|
|
|
513 |
by (fast_arith_tac 1);
|
|
|
514 |
qed "zless_zadd_imp_zless";
|
|
|
515 |
|
|
|
516 |
(*LOOPS as a simprule! Analogous to Suc_lessD*)
|
|
|
517 |
Goal "w + #1 < z ==> w < (z::int)";
|
|
|
518 |
by (fast_arith_tac 1);
|
|
|
519 |
qed "zless_zadd1_imp_zless";
|
|
|
520 |
|
|
|
521 |
Goal "w + #-1 = w - (#1::int)";
|
|
|
522 |
by (Simp_tac 1);
|
|
|
523 |
qed "zplus_minus1_conv";
|
|
|
524 |
|
|
|
525 |
|
|
|
526 |
(* nat *)
|
|
|
527 |
|
|
|
528 |
Goal "#0 <= z ==> int (nat z) = z";
|
|
|
529 |
by (asm_full_simp_tac
|
|
|
530 |
(simpset() addsimps [neg_eq_less_0, zle_def, not_neg_nat]) 1);
|
|
|
531 |
qed "nat_0_le";
|
|
|
532 |
|
|
|
533 |
Goal "z <= #0 ==> nat z = 0";
|
|
|
534 |
by (case_tac "z = #0" 1);
|
|
|
535 |
by (asm_simp_tac (simpset() addsimps [nat_le_int0]) 1);
|
|
|
536 |
by (asm_full_simp_tac
|
|
|
537 |
(simpset() addsimps [neg_eq_less_0, neg_nat, linorder_neq_iff]) 1);
|
|
|
538 |
qed "nat_le_0";
|
|
|
539 |
|
|
|
540 |
Addsimps [nat_0_le, nat_le_0];
|
|
|
541 |
|
|
|
542 |
val [major,minor] = Goal "[| #0 <= z; !!m. z = int m ==> P |] ==> P";
|
|
|
543 |
by (rtac (major RS nat_0_le RS sym RS minor) 1);
|
|
|
544 |
qed "nonneg_eq_int";
|
|
|
545 |
|
|
|
546 |
Goal "#0 <= w ==> (nat w = m) = (w = int m)";
|
|
|
547 |
by Auto_tac;
|
|
|
548 |
qed "nat_eq_iff";
|
|
|
549 |
|
|
8796
|
550 |
Goal "#0 <= w ==> (m = nat w) = (w = int m)";
|
|
|
551 |
by Auto_tac;
|
|
|
552 |
qed "nat_eq_iff2";
|
|
|
553 |
|
|
7707
|
554 |
Goal "#0 <= w ==> (nat w < m) = (w < int m)";
|
|
|
555 |
by (rtac iffI 1);
|
|
|
556 |
by (asm_full_simp_tac
|
|
|
557 |
(simpset() delsimps [zless_int] addsimps [zless_int RS sym]) 2);
|
|
|
558 |
by (etac (nat_0_le RS subst) 1);
|
|
|
559 |
by (Simp_tac 1);
|
|
|
560 |
qed "nat_less_iff";
|
|
|
561 |
|
|
|
562 |
|
|
|
563 |
(*Users don't want to see (int 0), int(Suc 0) or w + - z*)
|
|
|
564 |
Addsimps [int_0, int_Suc, symmetric zdiff_def];
|
|
|
565 |
|
|
|
566 |
Goal "nat #0 = 0";
|
|
|
567 |
by (simp_tac (simpset() addsimps [nat_eq_iff]) 1);
|
|
|
568 |
qed "nat_0";
|
|
|
569 |
|
|
|
570 |
Goal "nat #1 = 1";
|
|
|
571 |
by (simp_tac (simpset() addsimps [nat_eq_iff]) 1);
|
|
|
572 |
qed "nat_1";
|
|
|
573 |
|
|
|
574 |
Goal "nat #2 = 2";
|
|
|
575 |
by (simp_tac (simpset() addsimps [nat_eq_iff]) 1);
|
|
|
576 |
qed "nat_2";
|
|
|
577 |
|
|
|
578 |
Goal "#0 <= w ==> (nat w < nat z) = (w<z)";
|
|
|
579 |
by (case_tac "neg z" 1);
|
|
|
580 |
by (auto_tac (claset(), simpset() addsimps [nat_less_iff]));
|
|
|
581 |
by (auto_tac (claset() addIs [zless_trans],
|
|
|
582 |
simpset() addsimps [neg_eq_less_0, zle_def]));
|
|
|
583 |
qed "nat_less_eq_zless";
|
|
|
584 |
|
|
|
585 |
Goal "#0 < w | #0 <= z ==> (nat w <= nat z) = (w<=z)";
|
|
|
586 |
by (auto_tac (claset(),
|
|
|
587 |
simpset() addsimps [linorder_not_less RS sym,
|
|
|
588 |
zless_nat_conj]));
|
|
|
589 |
qed "nat_le_eq_zle";
|
|
|
590 |
|
|
|
591 |
(*Analogous to zadd_int, but more easily provable using the arithmetic in Bin*)
|
|
|
592 |
Goal "n<=m --> int m - int n = int (m-n)";
|
|
|
593 |
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
|
|
|
594 |
by Auto_tac;
|
|
|
595 |
qed_spec_mp "zdiff_int";
|
|
|
596 |
|
|
|
597 |
|
|
|
598 |
(** Products of signs **)
|
|
|
599 |
|
|
|
600 |
Goal "(m::int) < #0 ==> (#0 < m*n) = (n < #0)";
|
|
|
601 |
by Auto_tac;
|
|
|
602 |
by (force_tac (claset() addDs [zmult_zless_mono1_neg], simpset()) 2);
|
|
|
603 |
by (eres_inst_tac [("P", "#0 < m * n")] rev_mp 1);
|
|
|
604 |
by (simp_tac (simpset() addsimps [linorder_not_le RS sym]) 1);
|
|
|
605 |
by (force_tac (claset() addDs [inst "k" "m" zmult_zless_mono1_neg],
|
|
|
606 |
simpset()addsimps [order_le_less, zmult_commute]) 1);
|
|
|
607 |
qed "neg_imp_zmult_pos_iff";
|
|
|
608 |
|
|
|
609 |
Goal "(m::int) < #0 ==> (m*n < #0) = (#0 < n)";
|
|
|
610 |
by Auto_tac;
|
|
|
611 |
by (force_tac (claset() addDs [zmult_zless_mono1], simpset()) 2);
|
|
|
612 |
by (eres_inst_tac [("P", "m * n < #0")] rev_mp 1);
|
|
|
613 |
by (simp_tac (simpset() addsimps [linorder_not_le RS sym]) 1);
|
|
|
614 |
by (force_tac (claset() addDs [zmult_zless_mono1_neg],
|
|
|
615 |
simpset() addsimps [order_le_less]) 1);
|
|
|
616 |
qed "neg_imp_zmult_neg_iff";
|
|
|
617 |
|
|
|
618 |
Goal "#0 < (m::int) ==> (m*n < #0) = (n < #0)";
|
|
|
619 |
by Auto_tac;
|
|
|
620 |
by (force_tac (claset() addDs [zmult_zless_mono1_neg], simpset()) 2);
|
|
|
621 |
by (eres_inst_tac [("P", "m * n < #0")] rev_mp 1);
|
|
|
622 |
by (simp_tac (simpset() addsimps [linorder_not_le RS sym]) 1);
|
|
|
623 |
by (force_tac (claset() addDs [zmult_zless_mono1],
|
|
|
624 |
simpset() addsimps [order_le_less]) 1);
|
|
|
625 |
qed "pos_imp_zmult_neg_iff";
|
|
|
626 |
|
|
|
627 |
Goal "#0 < (m::int) ==> (#0 < m*n) = (#0 < n)";
|
|
|
628 |
by Auto_tac;
|
|
|
629 |
by (force_tac (claset() addDs [zmult_zless_mono1], simpset()) 2);
|
|
|
630 |
by (eres_inst_tac [("P", "#0 < m * n")] rev_mp 1);
|
|
|
631 |
by (simp_tac (simpset() addsimps [linorder_not_le RS sym]) 1);
|
|
|
632 |
by (force_tac (claset() addDs [inst "k" "m" zmult_zless_mono1],
|
|
|
633 |
simpset() addsimps [order_le_less, zmult_commute]) 1);
|
|
|
634 |
qed "pos_imp_zmult_pos_iff";
|
|
|
635 |
|
|
|
636 |
(** <= versions of the theorems above **)
|
|
|
637 |
|
|
|
638 |
Goal "(m::int) < #0 ==> (m*n <= #0) = (#0 <= n)";
|
|
|
639 |
by (asm_simp_tac (simpset() addsimps [linorder_not_less RS sym,
|
|
|
640 |
neg_imp_zmult_pos_iff]) 1);
|
|
|
641 |
qed "neg_imp_zmult_nonpos_iff";
|
|
|
642 |
|
|
|
643 |
Goal "(m::int) < #0 ==> (#0 <= m*n) = (n <= #0)";
|
|
|
644 |
by (asm_simp_tac (simpset() addsimps [linorder_not_less RS sym,
|
|
|
645 |
neg_imp_zmult_neg_iff]) 1);
|
|
|
646 |
qed "neg_imp_zmult_nonneg_iff";
|
|
|
647 |
|
|
|
648 |
Goal "#0 < (m::int) ==> (m*n <= #0) = (n <= #0)";
|
|
|
649 |
by (asm_simp_tac (simpset() addsimps [linorder_not_less RS sym,
|
|
|
650 |
pos_imp_zmult_pos_iff]) 1);
|
|
|
651 |
qed "pos_imp_zmult_nonpos_iff";
|
|
|
652 |
|
|
|
653 |
Goal "#0 < (m::int) ==> (#0 <= m*n) = (#0 <= n)";
|
|
|
654 |
by (asm_simp_tac (simpset() addsimps [linorder_not_less RS sym,
|
|
|
655 |
pos_imp_zmult_neg_iff]) 1);
|
|
|
656 |
qed "pos_imp_zmult_nonneg_iff";
|