author | wenzelm |
Thu, 07 Dec 2006 23:16:55 +0100 | |
changeset 21708 | 45e7491bea47 |
parent 21588 | cd0dc678a205 |
child 22548 | 6ce4bddf3bcb |
permissions | -rw-r--r-- |
13876 | 1 |
(* Title: HOL/Integ/presburger.ML |
2 |
ID: $Id$ |
|
3 |
Author: Amine Chaieb and Stefan Berghofer, TU Muenchen |
|
4 |
||
17465 | 5 |
Tactic for solving arithmetical Goals in Presburger Arithmetic. |
13876 | 6 |
|
17465 | 7 |
This version of presburger deals with occurences of functional symbols |
8 |
in the subgoal and abstract over them to try to prove the more general |
|
9 |
formula. It then resolves with the subgoal. To enable this feature |
|
10 |
call the procedure with the parameter abs. |
|
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
11 |
*) |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
12 |
|
21588 | 13 |
signature PRESBURGER = |
13876 | 14 |
sig |
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
15 |
val presburger_tac : bool -> bool -> int -> tactic |
18708 | 16 |
val setup : theory -> theory |
13876 | 17 |
val trace : bool ref |
18 |
end; |
|
19 |
||
20 |
structure Presburger: PRESBURGER = |
|
21 |
struct |
|
22 |
||
23 |
val trace = ref false; |
|
24 |
fun trace_msg s = if !trace then tracing s else (); |
|
25 |
||
26 |
(*-----------------------------------------------------------------*) |
|
21588 | 27 |
(*cooper_pp: provefunction for the one-existance quantifier elimination*) |
13876 | 28 |
(* Here still only one problem : The proof for the arithmetical transformations done on the dvd atomic formulae*) |
29 |
(*-----------------------------------------------------------------*) |
|
30 |
||
14941 | 31 |
|
20053 | 32 |
val presburger_ss = simpset (); |
18202
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18155
diff
changeset
|
33 |
val binarith = map thm |
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18155
diff
changeset
|
34 |
["Pls_0_eq", "Min_1_eq", |
20485 | 35 |
"pred_Pls","pred_Min","pred_1","pred_0", |
36 |
"succ_Pls", "succ_Min", "succ_1", "succ_0", |
|
37 |
"add_Pls", "add_Min", "add_BIT_0", "add_BIT_10", |
|
21588 | 38 |
"add_BIT_11", "minus_Pls", "minus_Min", "minus_1", |
39 |
"minus_0", "mult_Pls", "mult_Min", "mult_num1", "mult_num0", |
|
20485 | 40 |
"add_Pls_right", "add_Min_right"]; |
21588 | 41 |
val intarithrel = |
42 |
(map thm ["int_eq_number_of_eq","int_neg_number_of_BIT", |
|
43 |
"int_le_number_of_eq","int_iszero_number_of_0", |
|
44 |
"int_less_number_of_eq_neg"]) @ |
|
45 |
(map (fn s => thm s RS thm "lift_bool") |
|
46 |
["int_iszero_number_of_Pls","int_iszero_number_of_1", |
|
47 |
"int_neg_number_of_Min"])@ |
|
48 |
(map (fn s => thm s RS thm "nlift_bool") |
|
49 |
["int_nonzero_number_of_Min","int_not_neg_number_of_Pls"]); |
|
50 |
||
18202
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18155
diff
changeset
|
51 |
val intarith = map thm ["int_number_of_add_sym", "int_number_of_minus_sym", |
21588 | 52 |
"int_number_of_diff_sym", "int_number_of_mult_sym"]; |
18202
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18155
diff
changeset
|
53 |
val natarith = map thm ["add_nat_number_of", "diff_nat_number_of", |
21588 | 54 |
"mult_nat_number_of", "eq_nat_number_of", |
55 |
"less_nat_number_of"] |
|
56 |
val powerarith = |
|
57 |
(map thm ["nat_number_of", "zpower_number_of_even", |
|
58 |
"zpower_Pls", "zpower_Min"]) @ |
|
21708 | 59 |
[(MetaSimplifier.simplify true [thm "zero_eq_Numeral0_nring", |
21588 | 60 |
thm "one_eq_Numeral1_nring"] |
18202
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18155
diff
changeset
|
61 |
(thm "zpower_number_of_odd"))] |
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18155
diff
changeset
|
62 |
|
21588 | 63 |
val comp_arith = binarith @ intarith @ intarithrel @ natarith |
64 |
@ powerarith @[thm"not_false_eq_true", thm "not_true_eq_false"]; |
|
14801
2d27b5ebc447
- deleted unneeded function eta_long (now in Pure/pattern.ML
berghofe
parents:
14758
diff
changeset
|
65 |
|
21588 | 66 |
fun cooper_pp sg (fm as e$Abs(xn,xT,p)) = |
20194 | 67 |
let val (xn1,p1) = Syntax.variant_abs (xn,xT,p) |
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
68 |
in (CooperProof.cooper_prv sg (Free (xn1, xT)) p1) end; |
13876 | 69 |
|
70 |
fun mnnf_pp sg fm = CooperProof.proof_of_cnnf sg fm |
|
71 |
(CooperProof.proof_of_evalc sg); |
|
72 |
||
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
73 |
fun tmproof_of_int_qelim sg fm = |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
74 |
Qelim.tproof_of_mlift_qelim sg CooperDec.is_arith_rel |
13876 | 75 |
(CooperProof.proof_of_linform sg) (mnnf_pp sg) (cooper_pp sg) fm; |
76 |
||
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
77 |
|
13876 | 78 |
(* Theorems to be used in this tactic*) |
79 |
||
80 |
val zdvd_int = thm "zdvd_int"; |
|
81 |
val zdiff_int_split = thm "zdiff_int_split"; |
|
82 |
val all_nat = thm "all_nat"; |
|
83 |
val ex_nat = thm "ex_nat"; |
|
84 |
val number_of1 = thm "number_of1"; |
|
85 |
val number_of2 = thm "number_of2"; |
|
86 |
val split_zdiv = thm "split_zdiv"; |
|
87 |
val split_zmod = thm "split_zmod"; |
|
88 |
val mod_div_equality' = thm "mod_div_equality'"; |
|
89 |
val split_div' = thm "split_div'"; |
|
90 |
val Suc_plus1 = thm "Suc_plus1"; |
|
91 |
val imp_le_cong = thm "imp_le_cong"; |
|
92 |
val conj_le_cong = thm "conj_le_cong"; |
|
18202
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18155
diff
changeset
|
93 |
val nat_mod_add_eq = mod_add1_eq RS sym; |
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18155
diff
changeset
|
94 |
val nat_mod_add_left_eq = mod_add_left_eq RS sym; |
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18155
diff
changeset
|
95 |
val nat_mod_add_right_eq = mod_add_right_eq RS sym; |
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18155
diff
changeset
|
96 |
val int_mod_add_eq = zmod_zadd1_eq RS sym; |
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18155
diff
changeset
|
97 |
val int_mod_add_left_eq = zmod_zadd_left_eq RS sym; |
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18155
diff
changeset
|
98 |
val int_mod_add_right_eq = zmod_zadd_right_eq RS sym; |
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18155
diff
changeset
|
99 |
val nat_div_add_eq = div_add1_eq RS sym; |
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18155
diff
changeset
|
100 |
val int_div_add_eq = zdiv_zadd1_eq RS sym; |
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18155
diff
changeset
|
101 |
val ZDIVISION_BY_ZERO_MOD = DIVISION_BY_ZERO RS conjunct2; |
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18155
diff
changeset
|
102 |
val ZDIVISION_BY_ZERO_DIV = DIVISION_BY_ZERO RS conjunct1; |
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18155
diff
changeset
|
103 |
|
13876 | 104 |
|
105 |
(* extract all the constants in a term*) |
|
20854 | 106 |
fun add_term_typed_consts (Const (c, T), cs) = insert (op =) (c, T) cs |
13876 | 107 |
| add_term_typed_consts (t $ u, cs) = |
108 |
add_term_typed_consts (t, add_term_typed_consts (u, cs)) |
|
109 |
| add_term_typed_consts (Abs (_, _, t), cs) = add_term_typed_consts (t, cs) |
|
110 |
| add_term_typed_consts (_, cs) = cs; |
|
111 |
||
112 |
fun term_typed_consts t = add_term_typed_consts(t,[]); |
|
113 |
||
15661
9ef583b08647
reverted renaming of Some/None in comments and strings;
wenzelm
parents:
15620
diff
changeset
|
114 |
(* Some Types*) |
13876 | 115 |
val bT = HOLogic.boolT; |
15620
8ccdc8bc66a2
replaced bool by a new datatype "bit" for binary numerals
paulson
parents:
15574
diff
changeset
|
116 |
val bitT = HOLogic.bitT; |
13876 | 117 |
val iT = HOLogic.intT; |
118 |
val nT = HOLogic.natT; |
|
119 |
||
120 |
(* Allowed Consts in formulae for presburger tactic*) |
|
121 |
||
122 |
val allowed_consts = |
|
123 |
[("All", (iT --> bT) --> bT), |
|
124 |
("Ex", (iT --> bT) --> bT), |
|
125 |
("All", (nT --> bT) --> bT), |
|
126 |
("Ex", (nT --> bT) --> bT), |
|
127 |
||
128 |
("op &", bT --> bT --> bT), |
|
129 |
("op |", bT --> bT --> bT), |
|
130 |
("op -->", bT --> bT --> bT), |
|
131 |
("op =", bT --> bT --> bT), |
|
132 |
("Not", bT --> bT), |
|
133 |
||
19277 | 134 |
("Orderings.less_eq", iT --> iT --> bT), |
13876 | 135 |
("op =", iT --> iT --> bT), |
19277 | 136 |
("Orderings.less", iT --> iT --> bT), |
21416 | 137 |
("Divides.dvd", iT --> iT --> bT), |
138 |
("Divides.div", iT --> iT --> iT), |
|
139 |
("Divides.mod", iT --> iT --> iT), |
|
19233
77ca20b0ed77
renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents:
18708
diff
changeset
|
140 |
("HOL.plus", iT --> iT --> iT), |
77ca20b0ed77
renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents:
18708
diff
changeset
|
141 |
("HOL.minus", iT --> iT --> iT), |
21588 | 142 |
("HOL.times", iT --> iT --> iT), |
13876 | 143 |
("HOL.abs", iT --> iT), |
19233
77ca20b0ed77
renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents:
18708
diff
changeset
|
144 |
("HOL.uminus", iT --> iT), |
14801
2d27b5ebc447
- deleted unneeded function eta_long (now in Pure/pattern.ML
berghofe
parents:
14758
diff
changeset
|
145 |
("HOL.max", iT --> iT --> iT), |
2d27b5ebc447
- deleted unneeded function eta_long (now in Pure/pattern.ML
berghofe
parents:
14758
diff
changeset
|
146 |
("HOL.min", iT --> iT --> iT), |
13876 | 147 |
|
19277 | 148 |
("Orderings.less_eq", nT --> nT --> bT), |
13876 | 149 |
("op =", nT --> nT --> bT), |
19277 | 150 |
("Orderings.less", nT --> nT --> bT), |
21416 | 151 |
("Divides.dvd", nT --> nT --> bT), |
152 |
("Divides.div", nT --> nT --> nT), |
|
153 |
("Divides.mod", nT --> nT --> nT), |
|
19233
77ca20b0ed77
renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents:
18708
diff
changeset
|
154 |
("HOL.plus", nT --> nT --> nT), |
77ca20b0ed77
renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents:
18708
diff
changeset
|
155 |
("HOL.minus", nT --> nT --> nT), |
21588 | 156 |
("HOL.times", nT --> nT --> nT), |
13876 | 157 |
("Suc", nT --> nT), |
14801
2d27b5ebc447
- deleted unneeded function eta_long (now in Pure/pattern.ML
berghofe
parents:
14758
diff
changeset
|
158 |
("HOL.max", nT --> nT --> nT), |
2d27b5ebc447
- deleted unneeded function eta_long (now in Pure/pattern.ML
berghofe
parents:
14758
diff
changeset
|
159 |
("HOL.min", nT --> nT --> nT), |
13876 | 160 |
|
15620
8ccdc8bc66a2
replaced bool by a new datatype "bit" for binary numerals
paulson
parents:
15574
diff
changeset
|
161 |
("Numeral.bit.B0", bitT), |
8ccdc8bc66a2
replaced bool by a new datatype "bit" for binary numerals
paulson
parents:
15574
diff
changeset
|
162 |
("Numeral.bit.B1", bitT), |
20485 | 163 |
("Numeral.Bit", iT --> bitT --> iT), |
164 |
("Numeral.Pls", iT), |
|
165 |
("Numeral.Min", iT), |
|
166 |
("Numeral.number_of", iT --> iT), |
|
167 |
("Numeral.number_of", iT --> nT), |
|
20713
823967ef47f1
renamed 0 and 1 to HOL.zero and HOL.one respectivly; introduced corresponding syntactic classes
haftmann
parents:
20485
diff
changeset
|
168 |
("HOL.zero", nT), |
823967ef47f1
renamed 0 and 1 to HOL.zero and HOL.one respectivly; introduced corresponding syntactic classes
haftmann
parents:
20485
diff
changeset
|
169 |
("HOL.zero", iT), |
823967ef47f1
renamed 0 and 1 to HOL.zero and HOL.one respectivly; introduced corresponding syntactic classes
haftmann
parents:
20485
diff
changeset
|
170 |
("HOL.one", nT), |
823967ef47f1
renamed 0 and 1 to HOL.zero and HOL.one respectivly; introduced corresponding syntactic classes
haftmann
parents:
20485
diff
changeset
|
171 |
("HOL.one", iT), |
13876 | 172 |
("False", bT), |
173 |
("True", bT)]; |
|
174 |
||
175 |
(* Preparation of the formula to be sent to the Integer quantifier *) |
|
176 |
(* elimination procedure *) |
|
177 |
(* Transforms meta implications and meta quantifiers to object *) |
|
178 |
(* implications and object quantifiers *) |
|
179 |
||
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
180 |
|
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
181 |
(*==================================*) |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
182 |
(* Abstracting on subterms ========*) |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
183 |
(*==================================*) |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
184 |
(* Returns occurences of terms that are function application of type int or nat*) |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
185 |
|
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
186 |
fun getfuncs fm = case strip_comb fm of |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
187 |
(Free (_, T), ts as _ :: _) => |
21588 | 188 |
if body_type T mem [iT, nT] |
189 |
andalso not (ts = []) andalso forall (null o loose_bnos) ts |
|
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
190 |
then [fm] |
15570 | 191 |
else Library.foldl op union ([], map getfuncs ts) |
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
192 |
| (Var (_, T), ts as _ :: _) => |
21588 | 193 |
if body_type T mem [iT, nT] |
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
194 |
andalso not (ts = []) andalso forall (null o loose_bnos) ts then [fm] |
15570 | 195 |
else Library.foldl op union ([], map getfuncs ts) |
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
196 |
| (Const (s, T), ts) => |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
197 |
if (s, T) mem allowed_consts orelse not (body_type T mem [iT, nT]) |
15570 | 198 |
then Library.foldl op union ([], map getfuncs ts) |
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
199 |
else [fm] |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
200 |
| (Abs (s, T, t), _) => getfuncs t |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
201 |
| _ => []; |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
202 |
|
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
203 |
|
21588 | 204 |
fun abstract_pres sg fm = |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
205 |
foldr (fn (t, u) => |
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
206 |
let val T = fastype_of t |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
207 |
in all T $ Abs ("x", T, abstract_over (t, u)) end) |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
208 |
fm (getfuncs fm); |
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
209 |
|
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
210 |
|
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
211 |
|
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
212 |
(* hasfuncs_on_bounds dont care of the type of the functions applied! |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
213 |
It returns true if there is a subterm coresponding to the application of |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
214 |
a function on a bounded variable. |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
215 |
|
21588 | 216 |
Function applications are allowed only for well predefined functions a |
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
217 |
consts*) |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
218 |
|
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
219 |
fun has_free_funcs fm = case strip_comb fm of |
21588 | 220 |
(Free (_, T), ts as _ :: _) => |
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
221 |
if (body_type T mem [iT,nT]) andalso (not (T mem [iT,nT])) |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
222 |
then true |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
223 |
else exists (fn x => x) (map has_free_funcs ts) |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
224 |
| (Var (_, T), ts as _ :: _) => |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
225 |
if (body_type T mem [iT,nT]) andalso not (T mem [iT,nT]) |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
226 |
then true |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
227 |
else exists (fn x => x) (map has_free_funcs ts) |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
228 |
| (Const (s, T), ts) => exists (fn x => x) (map has_free_funcs ts) |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
229 |
| (Abs (s, T, t), _) => has_free_funcs t |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
230 |
|_ => false; |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
231 |
|
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
232 |
|
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
233 |
(*returns true if the formula is relevant for presburger arithmetic tactic |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
234 |
The constants occuring in term t should be a subset of the allowed_consts |
21588 | 235 |
There also should be no occurences of application of functions on bounded |
236 |
variables. Whenever this function will be used, it will be ensured that t |
|
237 |
will not contain subterms with function symbols that could have been |
|
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
238 |
abstracted over.*) |
21588 | 239 |
|
240 |
fun relevant ps t = (term_typed_consts t) subset allowed_consts andalso |
|
15570 | 241 |
map (fn i => snd (List.nth (ps, i))) (loose_bnos t) @ |
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
242 |
map (snd o dest_Free) (term_frees t) @ map (snd o dest_Var) (term_vars t) |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
243 |
subset [iT, nT] |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
244 |
andalso not (has_free_funcs t); |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
245 |
|
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
246 |
|
21588 | 247 |
fun prepare_for_presburger sg q fm = |
13876 | 248 |
let |
249 |
val ps = Logic.strip_params fm |
|
250 |
val hs = map HOLogic.dest_Trueprop (Logic.strip_assums_hyp fm) |
|
251 |
val c = HOLogic.dest_Trueprop (Logic.strip_assums_concl fm) |
|
21588 | 252 |
val _ = if relevant (rev ps) c then () |
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
253 |
else (trace_msg ("Conclusion is not a presburger term:\n" ^ |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
254 |
Sign.string_of_term sg c); raise CooperDec.COOPER) |
13876 | 255 |
fun mk_all ((s, T), (P,n)) = |
256 |
if 0 mem loose_bnos P then |
|
257 |
(HOLogic.all_const T $ Abs (s, T, P), n) |
|
258 |
else (incr_boundvars ~1 P, n-1) |
|
259 |
fun mk_all2 (v, t) = HOLogic.all_const (fastype_of v) $ lambda v t; |
|
15570 | 260 |
val (rhs,irhs) = List.partition (relevant (rev ps)) hs |
13876 | 261 |
val np = length ps |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
262 |
val (fm',np) = foldr (fn ((x, T), (fm,n)) => mk_all ((x, T), (fm,n))) |
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
263 |
(foldr HOLogic.mk_imp c rhs, np) ps |
15570 | 264 |
val (vs, _) = List.partition (fn t => q orelse (type_of t) = nT) |
13876 | 265 |
(term_frees fm' @ term_vars fm'); |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
266 |
val fm2 = foldr mk_all2 fm' vs |
13876 | 267 |
in (fm2, np + length vs, length rhs) end; |
268 |
||
269 |
(*Object quantifier to meta --*) |
|
270 |
fun spec_step n th = if (n=0) then th else (spec_step (n-1) th) RS spec ; |
|
271 |
||
272 |
(* object implication to meta---*) |
|
273 |
fun mp_step n th = if (n=0) then th else (mp_step (n-1) th) RS mp; |
|
274 |
||
275 |
(* the presburger tactic*) |
|
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
276 |
|
21588 | 277 |
(* Parameters : q = flag for quantify ofer free variables ; |
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
278 |
a = flag for abstracting over function occurences |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
279 |
i = subgoal *) |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
280 |
|
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
281 |
fun presburger_tac q a i = ObjectLogic.atomize_tac i THEN (fn st => |
13876 | 282 |
let |
17465 | 283 |
val g = List.nth (prems_of st, i - 1) |
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
284 |
val sg = sign_of_thm st |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
285 |
(* The Abstraction step *) |
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
286 |
val g' = if a then abstract_pres sg g else g |
13876 | 287 |
(* Transform the term*) |
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
288 |
val (t,np,nh) = prepare_for_presburger sg q g' |
15661
9ef583b08647
reverted renaming of Some/None in comments and strings;
wenzelm
parents:
15620
diff
changeset
|
289 |
(* Some simpsets for dealing with mod div abs and nat*) |
20255 | 290 |
val mod_div_simpset = Simplifier.theory_context sg HOL_basic_ss |
21588 | 291 |
addsimps [refl,nat_mod_add_eq, nat_mod_add_left_eq, |
292 |
nat_mod_add_right_eq, int_mod_add_eq, |
|
293 |
int_mod_add_right_eq, int_mod_add_left_eq, |
|
294 |
nat_div_add_eq, int_div_add_eq, |
|
295 |
mod_self, zmod_self, |
|
296 |
DIVISION_BY_ZERO_MOD,DIVISION_BY_ZERO_DIV, |
|
297 |
ZDIVISION_BY_ZERO_MOD,ZDIVISION_BY_ZERO_DIV, |
|
298 |
zdiv_zero,zmod_zero,div_0,mod_0, |
|
299 |
zdiv_1,zmod_1,div_1,mod_1, |
|
300 |
Suc_plus1] |
|
301 |
addsimps add_ac |
|
302 |
addsimprocs [cancel_div_mod_proc] |
|
13876 | 303 |
val simpset0 = HOL_basic_ss |
304 |
addsimps [mod_div_equality', Suc_plus1] |
|
18393 | 305 |
addsimps comp_arith |
13997 | 306 |
addsplits [split_zdiv, split_zmod, split_div', split_min, split_max] |
13876 | 307 |
(* Simp rules for changing (n::int) to int n *) |
308 |
val simpset1 = HOL_basic_ss |
|
309 |
addsimps [nat_number_of_def, zdvd_int] @ map (fn r => r RS sym) |
|
310 |
[int_int_eq, zle_int, zless_int, zadd_int, zmult_int] |
|
311 |
addsplits [zdiff_int_split] |
|
312 |
(*simp rules for elimination of int n*) |
|
313 |
||
314 |
val simpset2 = HOL_basic_ss |
|
315 |
addsimps [nat_0_le, all_nat, ex_nat, number_of1, number_of2, int_0, int_1] |
|
316 |
addcongs [conj_le_cong, imp_le_cong] |
|
317 |
(* simp rules for elimination of abs *) |
|
14353
79f9fbef9106
Added lemmas to Ring_and_Field with slightly modified simplification rules
paulson
parents:
14130
diff
changeset
|
318 |
val simpset3 = HOL_basic_ss addsplits [abs_split] |
13876 | 319 |
val ct = cterm_of sg (HOLogic.mk_Trueprop t) |
320 |
(* Theorem for the nat --> int transformation *) |
|
321 |
val pre_thm = Seq.hd (EVERY |
|
18202
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18155
diff
changeset
|
322 |
[simp_tac mod_div_simpset 1, simp_tac simpset0 1, |
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
323 |
TRY (simp_tac simpset1 1), TRY (simp_tac simpset2 1), |
14801
2d27b5ebc447
- deleted unneeded function eta_long (now in Pure/pattern.ML
berghofe
parents:
14758
diff
changeset
|
324 |
TRY (simp_tac simpset3 1), TRY (simp_tac presburger_ss 1)] |
13876 | 325 |
(trivial ct)) |
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
326 |
fun assm_tac i = REPEAT_DETERM_N nh (assume_tac i) |
13876 | 327 |
(* The result of the quantifier elimination *) |
328 |
val (th, tac) = case (prop_of pre_thm) of |
|
329 |
Const ("==>", _) $ (Const ("Trueprop", _) $ t1) $ _ => |
|
21588 | 330 |
let val pth = |
14920
a7525235e20f
An oracle is built in. The tactic will not generate any proofs any more, if the quick_and_dirty flag is set on.
chaieb
parents:
14882
diff
changeset
|
331 |
(* If quick_and_dirty then run without proof generation as oracle*) |
21588 | 332 |
if !quick_and_dirty |
16836 | 333 |
then presburger_oracle sg (Pattern.eta_long [] t1) |
14941 | 334 |
(* |
21588 | 335 |
assume (cterm_of sg |
336 |
(HOLogic.mk_Trueprop(HOLogic.mk_eq(t1,CooperDec.integer_qelim (Pattern.eta_long [] t1))))) |
|
14941 | 337 |
*) |
21588 | 338 |
else tmproof_of_int_qelim sg (Pattern.eta_long [] t1) |
339 |
in |
|
13876 | 340 |
(trace_msg ("calling procedure with term:\n" ^ |
341 |
Sign.string_of_term sg t1); |
|
14920
a7525235e20f
An oracle is built in. The tactic will not generate any proofs any more, if the quick_and_dirty flag is set on.
chaieb
parents:
14882
diff
changeset
|
342 |
((pth RS iffD2) RS pre_thm, |
13876 | 343 |
assm_tac (i + 1) THEN (if q then I else TRY) (rtac TrueI i))) |
14920
a7525235e20f
An oracle is built in. The tactic will not generate any proofs any more, if the quick_and_dirty flag is set on.
chaieb
parents:
14882
diff
changeset
|
344 |
end |
13876 | 345 |
| _ => (pre_thm, assm_tac i) |
21588 | 346 |
in (rtac (((mp_step nh) o (spec_step np)) th) i |
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
347 |
THEN tac) st |
14130 | 348 |
end handle Subscript => no_tac st | CooperDec.COOPER => no_tac st); |
13876 | 349 |
|
21588 | 350 |
val presburger_meth = |
351 |
let val parse_flag = |
|
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
352 |
Args.$$$ "no_quantify" >> K (apfst (K false)) |
18155
e5ab63ca6b0f
old argument "abs" is replaced by "no_abs". Abstraction is turned on by default.
chaieb
parents:
17465
diff
changeset
|
353 |
|| Args.$$$ "no_abs" >> K (apsnd (K false)); |
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
354 |
in |
21588 | 355 |
Method.simple_args |
356 |
(Scan.optional (Args.$$$ "(" |-- Scan.repeat1 parse_flag --| Args.$$$ ")") [] >> |
|
357 |
curry (Library.foldl op |>) (true, true)) |
|
358 |
(fn (q,a) => K (Method.SIMPLE_METHOD' (presburger_tac q a))) |
|
14758
af3b71a46a1c
A new implementation for presburger arithmetic following the one suggested in technical report Chaieb Amine and Tobias Nipkow. It is generic an smaller.
chaieb
parents:
14353
diff
changeset
|
359 |
end; |
13876 | 360 |
|
20413 | 361 |
val presburger_arith_tac = mk_arith_tactic "presburger" (fn i => fn st => |
362 |
(warning "Trying full Presburger arithmetic ..."; |
|
363 |
presburger_tac true true i st)); |
|
364 |
||
13876 | 365 |
val setup = |
21588 | 366 |
Method.add_method ("presburger", presburger_meth, |
18708 | 367 |
"decision procedure for Presburger arithmetic") #> |
20413 | 368 |
arith_tactic_add presburger_arith_tac; |
13876 | 369 |
|
370 |
end; |
|
371 |
||
18155
e5ab63ca6b0f
old argument "abs" is replaced by "no_abs". Abstraction is turned on by default.
chaieb
parents:
17465
diff
changeset
|
372 |
val presburger_tac = Presburger.presburger_tac true true; |