author | paulson |
Mon, 12 Jan 2004 16:51:45 +0100 | |
changeset 14353 | 79f9fbef9106 |
parent 14130 | 398bc4a885d6 |
child 14758 | af3b71a46a1c |
permissions | -rw-r--r-- |
13876 | 1 |
(* Title: HOL/Integ/presburger.ML |
2 |
ID: $Id$ |
|
3 |
Author: Amine Chaieb and Stefan Berghofer, TU Muenchen |
|
4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
|
5 |
||
6 |
Tactic for solving arithmetical Goals in Presburger Arithmetic |
|
7 |
*) |
|
8 |
||
9 |
signature PRESBURGER = |
|
10 |
sig |
|
11 |
val presburger_tac : bool -> int -> tactic |
|
12 |
val presburger_method : bool -> int -> Proof.method |
|
13 |
val setup : (theory -> theory) list |
|
14 |
val trace : bool ref |
|
15 |
end; |
|
16 |
||
17 |
structure Presburger: PRESBURGER = |
|
18 |
struct |
|
19 |
||
20 |
val trace = ref false; |
|
21 |
fun trace_msg s = if !trace then tracing s else (); |
|
22 |
||
23 |
(*-----------------------------------------------------------------*) |
|
24 |
(*cooper_pp: provefunction for the one-exstance quantifier elimination*) |
|
25 |
(* Here still only one problem : The proof for the arithmetical transformations done on the dvd atomic formulae*) |
|
26 |
(*-----------------------------------------------------------------*) |
|
27 |
||
28 |
val presburger_ss = simpset_of (theory "Presburger"); |
|
29 |
||
30 |
fun cooper_pp sg vrl (fm as e$Abs(xn,xT,p)) = |
|
31 |
let val (xn1,p1) = variant_abs (xn,xT,p) |
|
32 |
in (CooperProof.cooper_prv sg (Free (xn1, xT)) p1 vrl) end; |
|
33 |
||
34 |
fun mnnf_pp sg fm = CooperProof.proof_of_cnnf sg fm |
|
35 |
(CooperProof.proof_of_evalc sg); |
|
36 |
||
37 |
fun mproof_of_int_qelim sg fm = |
|
38 |
Qelim.proof_of_mlift_qelim sg CooperDec.is_arith_rel |
|
39 |
(CooperProof.proof_of_linform sg) (mnnf_pp sg) (cooper_pp sg) fm; |
|
40 |
||
41 |
(* Theorems to be used in this tactic*) |
|
42 |
||
43 |
val zdvd_int = thm "zdvd_int"; |
|
44 |
val zdiff_int_split = thm "zdiff_int_split"; |
|
45 |
val all_nat = thm "all_nat"; |
|
46 |
val ex_nat = thm "ex_nat"; |
|
47 |
val number_of1 = thm "number_of1"; |
|
48 |
val number_of2 = thm "number_of2"; |
|
49 |
val split_zdiv = thm "split_zdiv"; |
|
50 |
val split_zmod = thm "split_zmod"; |
|
51 |
val mod_div_equality' = thm "mod_div_equality'"; |
|
52 |
val split_div' = thm "split_div'"; |
|
53 |
val Suc_plus1 = thm "Suc_plus1"; |
|
54 |
val imp_le_cong = thm "imp_le_cong"; |
|
55 |
val conj_le_cong = thm "conj_le_cong"; |
|
56 |
||
57 |
(* extract all the constants in a term*) |
|
58 |
fun add_term_typed_consts (Const (c, T), cs) = (c,T) ins cs |
|
59 |
| add_term_typed_consts (t $ u, cs) = |
|
60 |
add_term_typed_consts (t, add_term_typed_consts (u, cs)) |
|
61 |
| add_term_typed_consts (Abs (_, _, t), cs) = add_term_typed_consts (t, cs) |
|
62 |
| add_term_typed_consts (_, cs) = cs; |
|
63 |
||
64 |
fun term_typed_consts t = add_term_typed_consts(t,[]); |
|
65 |
||
66 |
(* Some Types*) |
|
67 |
val bT = HOLogic.boolT; |
|
68 |
val iT = HOLogic.intT; |
|
69 |
val binT = HOLogic.binT; |
|
70 |
val nT = HOLogic.natT; |
|
71 |
||
72 |
(* Allowed Consts in formulae for presburger tactic*) |
|
73 |
||
74 |
val allowed_consts = |
|
75 |
[("All", (iT --> bT) --> bT), |
|
76 |
("Ex", (iT --> bT) --> bT), |
|
77 |
("All", (nT --> bT) --> bT), |
|
78 |
("Ex", (nT --> bT) --> bT), |
|
79 |
||
80 |
("op &", bT --> bT --> bT), |
|
81 |
("op |", bT --> bT --> bT), |
|
82 |
("op -->", bT --> bT --> bT), |
|
83 |
("op =", bT --> bT --> bT), |
|
84 |
("Not", bT --> bT), |
|
85 |
||
86 |
("op <=", iT --> iT --> bT), |
|
87 |
("op =", iT --> iT --> bT), |
|
88 |
("op <", iT --> iT --> bT), |
|
89 |
("Divides.op dvd", iT --> iT --> bT), |
|
90 |
("Divides.op div", iT --> iT --> iT), |
|
91 |
("Divides.op mod", iT --> iT --> iT), |
|
92 |
("op +", iT --> iT --> iT), |
|
93 |
("op -", iT --> iT --> iT), |
|
94 |
("op *", iT --> iT --> iT), |
|
95 |
("HOL.abs", iT --> iT), |
|
96 |
("uminus", iT --> iT), |
|
13997 | 97 |
("HOL.max", iT --> iT --> iT), |
98 |
("HOL.min", iT --> iT --> iT), |
|
13876 | 99 |
|
100 |
("op <=", nT --> nT --> bT), |
|
101 |
("op =", nT --> nT --> bT), |
|
102 |
("op <", nT --> nT --> bT), |
|
103 |
("Divides.op dvd", nT --> nT --> bT), |
|
104 |
("Divides.op div", nT --> nT --> nT), |
|
105 |
("Divides.op mod", nT --> nT --> nT), |
|
106 |
("op +", nT --> nT --> nT), |
|
107 |
("op -", nT --> nT --> nT), |
|
108 |
("op *", nT --> nT --> nT), |
|
109 |
("Suc", nT --> nT), |
|
13997 | 110 |
("HOL.max", nT --> nT --> nT), |
111 |
("HOL.min", nT --> nT --> nT), |
|
13876 | 112 |
|
113 |
("Numeral.bin.Bit", binT --> bT --> binT), |
|
114 |
("Numeral.bin.Pls", binT), |
|
115 |
("Numeral.bin.Min", binT), |
|
116 |
("Numeral.number_of", binT --> iT), |
|
117 |
("Numeral.number_of", binT --> nT), |
|
118 |
("0", nT), |
|
119 |
("0", iT), |
|
120 |
("1", nT), |
|
121 |
("1", iT), |
|
122 |
||
123 |
("False", bT), |
|
124 |
("True", bT)]; |
|
125 |
||
126 |
(*returns true if the formula is relevant for presburger arithmetic tactic*) |
|
14130 | 127 |
fun relevant ps t = (term_typed_consts t) subset allowed_consts andalso |
128 |
map (fn i => snd (nth_elem (i, ps))) (loose_bnos t) @ |
|
129 |
map (snd o dest_Free) (term_frees t) @ map (snd o dest_Var) (term_vars t) |
|
130 |
subset [iT, nT]; |
|
13876 | 131 |
|
132 |
(* Preparation of the formula to be sent to the Integer quantifier *) |
|
133 |
(* elimination procedure *) |
|
134 |
(* Transforms meta implications and meta quantifiers to object *) |
|
135 |
(* implications and object quantifiers *) |
|
136 |
||
137 |
fun prepare_for_presburger q fm = |
|
138 |
let |
|
139 |
val ps = Logic.strip_params fm |
|
140 |
val hs = map HOLogic.dest_Trueprop (Logic.strip_assums_hyp fm) |
|
141 |
val c = HOLogic.dest_Trueprop (Logic.strip_assums_concl fm) |
|
14130 | 142 |
val _ = if relevant (rev ps) c then () else raise CooperDec.COOPER |
13876 | 143 |
fun mk_all ((s, T), (P,n)) = |
144 |
if 0 mem loose_bnos P then |
|
145 |
(HOLogic.all_const T $ Abs (s, T, P), n) |
|
146 |
else (incr_boundvars ~1 P, n-1) |
|
147 |
fun mk_all2 (v, t) = HOLogic.all_const (fastype_of v) $ lambda v t; |
|
14130 | 148 |
val (rhs,irhs) = partition (relevant (rev ps)) hs |
13876 | 149 |
val np = length ps |
150 |
val (fm',np) = foldr (fn ((x, T), (fm,n)) => mk_all ((x, T), (fm,n))) |
|
151 |
(ps,(foldr HOLogic.mk_imp (rhs, c), np)) |
|
152 |
val (vs, _) = partition (fn t => q orelse (type_of t) = nT) |
|
153 |
(term_frees fm' @ term_vars fm'); |
|
154 |
val fm2 = foldr mk_all2 (vs, fm') |
|
155 |
in (fm2, np + length vs, length rhs) end; |
|
156 |
||
157 |
(*Object quantifier to meta --*) |
|
158 |
fun spec_step n th = if (n=0) then th else (spec_step (n-1) th) RS spec ; |
|
159 |
||
160 |
(* object implication to meta---*) |
|
161 |
fun mp_step n th = if (n=0) then th else (mp_step (n-1) th) RS mp; |
|
162 |
||
163 |
(* the presburger tactic*) |
|
14130 | 164 |
fun presburger_tac q i = ObjectLogic.atomize_tac i THEN (fn st => |
13876 | 165 |
let |
166 |
val g = BasisLibrary.List.nth (prems_of st, i - 1); |
|
167 |
val sg = sign_of_thm st; |
|
168 |
(* Transform the term*) |
|
169 |
val (t,np,nh) = prepare_for_presburger q g |
|
170 |
(* Some simpsets for dealing with mod div abs and nat*) |
|
171 |
||
172 |
val simpset0 = HOL_basic_ss |
|
173 |
addsimps [mod_div_equality', Suc_plus1] |
|
13997 | 174 |
addsplits [split_zdiv, split_zmod, split_div', split_min, split_max] |
13876 | 175 |
(* Simp rules for changing (n::int) to int n *) |
176 |
val simpset1 = HOL_basic_ss |
|
177 |
addsimps [nat_number_of_def, zdvd_int] @ map (fn r => r RS sym) |
|
178 |
[int_int_eq, zle_int, zless_int, zadd_int, zmult_int] |
|
179 |
addsplits [zdiff_int_split] |
|
180 |
(*simp rules for elimination of int n*) |
|
181 |
||
182 |
val simpset2 = HOL_basic_ss |
|
183 |
addsimps [nat_0_le, all_nat, ex_nat, number_of1, number_of2, int_0, int_1] |
|
184 |
addcongs [conj_le_cong, imp_le_cong] |
|
185 |
(* simp rules for elimination of abs *) |
|
186 |
||
14353
79f9fbef9106
Added lemmas to Ring_and_Field with slightly modified simplification rules
paulson
parents:
14130
diff
changeset
|
187 |
val simpset3 = HOL_basic_ss addsplits [abs_split] |
13876 | 188 |
|
189 |
val ct = cterm_of sg (HOLogic.mk_Trueprop t) |
|
190 |
||
191 |
(* Theorem for the nat --> int transformation *) |
|
192 |
val pre_thm = Seq.hd (EVERY |
|
193 |
[simp_tac simpset0 i, |
|
194 |
TRY (simp_tac simpset1 i), TRY (simp_tac simpset2 i), |
|
195 |
TRY (simp_tac simpset3 i), TRY (simp_tac presburger_ss i)] |
|
196 |
(trivial ct)) |
|
197 |
||
198 |
fun assm_tac i = REPEAT_DETERM_N nh (assume_tac i); |
|
199 |
||
200 |
(* The result of the quantifier elimination *) |
|
201 |
val (th, tac) = case (prop_of pre_thm) of |
|
202 |
Const ("==>", _) $ (Const ("Trueprop", _) $ t1) $ _ => |
|
203 |
(trace_msg ("calling procedure with term:\n" ^ |
|
204 |
Sign.string_of_term sg t1); |
|
13997 | 205 |
((mproof_of_int_qelim sg (Pattern.eta_long [] t1) RS iffD2) RS pre_thm, |
13876 | 206 |
assm_tac (i + 1) THEN (if q then I else TRY) (rtac TrueI i))) |
207 |
| _ => (pre_thm, assm_tac i) |
|
208 |
in (rtac (((mp_step nh) o (spec_step np)) th) i THEN tac) st |
|
14130 | 209 |
end handle Subscript => no_tac st | CooperDec.COOPER => no_tac st); |
13876 | 210 |
|
211 |
fun presburger_args meth = |
|
212 |
Method.simple_args (Scan.optional (Args.$$$ "no_quantify" >> K false) true) |
|
213 |
(fn q => fn _ => meth q 1); |
|
214 |
||
215 |
fun presburger_method q i = Method.METHOD (fn facts => |
|
216 |
Method.insert_tac facts 1 THEN presburger_tac q i) |
|
217 |
||
218 |
val setup = |
|
219 |
[Method.add_method ("presburger", |
|
220 |
presburger_args presburger_method, |
|
221 |
"decision procedure for Presburger arithmetic"), |
|
222 |
ArithTheoryData.map (fn {splits, inj_consts, discrete, presburger} => |
|
223 |
{splits = splits, inj_consts = inj_consts, discrete = discrete, |
|
224 |
presburger = Some (presburger_tac true)})]; |
|
225 |
||
226 |
end; |
|
227 |
||
228 |
val presburger_tac = Presburger.presburger_tac true; |