|
1 (* Title: HOL/Integ/cooper_dec.ML |
|
2 ID: $Id$ |
|
3 Author: Amine Chaieb and Tobias Nipkow, TU Muenchen |
|
4 License: GPL (GNU GENERAL PUBLIC LICENSE) |
|
5 |
|
6 File containing the implementation of Cooper Algorithm |
|
7 decision procedure (intensively inspired from J.Harrison) |
|
8 *) |
|
9 |
|
10 signature COOPER_DEC = |
|
11 sig |
|
12 exception COOPER |
|
13 val is_arith_rel : term -> bool |
|
14 val mk_numeral : int -> term |
|
15 val dest_numeral : term -> int |
|
16 val zero : term |
|
17 val one : term |
|
18 val linear_cmul : int -> term -> term |
|
19 val linear_add : string list -> term -> term -> term |
|
20 val linear_sub : string list -> term -> term -> term |
|
21 val linear_neg : term -> term |
|
22 val lint : string list -> term -> term |
|
23 val linform : string list -> term -> term |
|
24 val formlcm : term -> term -> int |
|
25 val adjustcoeff : term -> int -> term -> term |
|
26 val unitycoeff : term -> term -> term |
|
27 val divlcm : term -> term -> int |
|
28 val bset : term -> term -> term list |
|
29 val aset : term -> term -> term list |
|
30 val linrep : string list -> term -> term -> term -> term |
|
31 val list_disj : term list -> term |
|
32 val simpl : term -> term |
|
33 val fv : term -> string list |
|
34 val negate : term -> term |
|
35 val operations : (string * (int * int -> bool)) list |
|
36 end; |
|
37 |
|
38 structure CooperDec : COOPER_DEC = |
|
39 struct |
|
40 |
|
41 (* ========================================================================= *) |
|
42 (* Cooper's algorithm for Presburger arithmetic. *) |
|
43 (* ========================================================================= *) |
|
44 exception COOPER; |
|
45 |
|
46 (* ------------------------------------------------------------------------- *) |
|
47 (* Lift operations up to numerals. *) |
|
48 (* ------------------------------------------------------------------------- *) |
|
49 |
|
50 (*Assumption : The construction of atomar formulas in linearl arithmetic is based on |
|
51 relation operations of Type : [int,int]---> bool *) |
|
52 |
|
53 (* ------------------------------------------------------------------------- *) |
|
54 |
|
55 |
|
56 (*Function is_arith_rel returns true if and only if the term is an atomar presburger |
|
57 formula *) |
|
58 fun is_arith_rel tm = case tm of |
|
59 Const(p,Type ("fun",[Type ("Numeral.bin", []),Type ("fun",[Type ("Numeral.bin", |
|
60 []),Type ("bool",[])] )])) $ _ $_ => true |
|
61 |Const(p,Type ("fun",[Type ("IntDef.int", []),Type ("fun",[Type ("IntDef.int", |
|
62 []),Type ("bool",[])] )])) $ _ $_ => true |
|
63 |_ => false; |
|
64 |
|
65 (*Function is_arith_rel returns true if and only if the term is an operation of the |
|
66 form [int,int]---> int*) |
|
67 |
|
68 (*Transform a natural number to a term*) |
|
69 |
|
70 fun mk_numeral 0 = Const("0",HOLogic.intT) |
|
71 |mk_numeral 1 = Const("1",HOLogic.intT) |
|
72 |mk_numeral n = (HOLogic.number_of_const HOLogic.intT) $ (HOLogic.mk_bin n); |
|
73 |
|
74 (*Transform an Term to an natural number*) |
|
75 |
|
76 fun dest_numeral (Const("0",Type ("IntDef.int", []))) = 0 |
|
77 |dest_numeral (Const("1",Type ("IntDef.int", []))) = 1 |
|
78 |dest_numeral (Const ("Numeral.number_of",_) $ n)= HOLogic.dest_binum n; |
|
79 (*Some terms often used for pattern matching*) |
|
80 |
|
81 val zero = mk_numeral 0; |
|
82 val one = mk_numeral 1; |
|
83 |
|
84 (*Tests if a Term is representing a number*) |
|
85 |
|
86 fun is_numeral t = (t = zero) orelse (t = one) orelse (can dest_numeral t); |
|
87 |
|
88 (*maps a unary natural function on a term containing an natural number*) |
|
89 |
|
90 fun numeral1 f n = mk_numeral (f(dest_numeral n)); |
|
91 |
|
92 (*maps a binary natural function on 2 term containing natural numbers*) |
|
93 |
|
94 fun numeral2 f m n = mk_numeral(f(dest_numeral m) (dest_numeral n)); |
|
95 |
|
96 (* ------------------------------------------------------------------------- *) |
|
97 (* Operations on canonical linear terms c1 * x1 + ... + cn * xn + k *) |
|
98 (* *) |
|
99 (* Note that we're quite strict: the ci must be present even if 1 *) |
|
100 (* (but if 0 we expect the monomial to be omitted) and k must be there *) |
|
101 (* even if it's zero. Thus, it's a constant iff not an addition term. *) |
|
102 (* ------------------------------------------------------------------------- *) |
|
103 |
|
104 |
|
105 fun linear_cmul n tm = if n = 0 then zero else let fun times n k = n*k in |
|
106 ( case tm of |
|
107 (Const("op +",T) $ (Const ("op *",T1 ) $c1 $ x1) $ rest) => |
|
108 Const("op +",T) $ ((Const("op *",T1) $ (numeral1 (times n) c1) $ x1)) $ (linear_cmul n rest) |
|
109 |_ => numeral1 (times n) tm) |
|
110 end ; |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 (* Whether the first of two items comes earlier in the list *) |
|
116 fun earlier [] x y = false |
|
117 |earlier (h::t) x y =if h = y then false |
|
118 else if h = x then true |
|
119 else earlier t x y ; |
|
120 |
|
121 fun earlierv vars (Bound i) (Bound j) = i < j |
|
122 |earlierv vars (Bound _) _ = true |
|
123 |earlierv vars _ (Bound _) = false |
|
124 |earlierv vars (Free (x,_)) (Free (y,_)) = earlier vars x y; |
|
125 |
|
126 |
|
127 fun linear_add vars tm1 tm2 = |
|
128 let fun addwith x y = x + y in |
|
129 (case (tm1,tm2) of |
|
130 ((Const ("op +",T1) $ ( Const("op *",T2) $ c1 $ x1) $ rest1),(Const |
|
131 ("op +",T3)$( Const("op *",T4) $ c2 $ x2) $ rest2)) => |
|
132 if x1 = x2 then |
|
133 let val c = (numeral2 (addwith) c1 c2) |
|
134 in |
|
135 if c = zero then (linear_add vars rest1 rest2) |
|
136 else (Const("op +",T1) $ (Const("op *",T2) $ c $ x1) $ (linear_add vars rest1 rest2)) |
|
137 end |
|
138 else |
|
139 if earlierv vars x1 x2 then (Const("op +",T1) $ |
|
140 (Const("op *",T2)$ c1 $ x1) $ (linear_add vars rest1 tm2)) |
|
141 else (Const("op +",T1) $ (Const("op *",T2) $ c2 $ x2) $ (linear_add vars tm1 rest2)) |
|
142 |((Const("op +",T1) $ (Const("op *",T2) $ c1 $ x1) $ rest1) ,_) => |
|
143 (Const("op +",T1)$ (Const("op *",T2) $ c1 $ x1) $ (linear_add vars |
|
144 rest1 tm2)) |
|
145 |(_, (Const("op +",T1) $(Const("op *",T2) $ c2 $ x2) $ rest2)) => |
|
146 (Const("op +",T1) $ (Const("op *",T2) $ c2 $ x2) $ (linear_add vars tm1 |
|
147 rest2)) |
|
148 | (_,_) => numeral2 (addwith) tm1 tm2) |
|
149 |
|
150 end; |
|
151 |
|
152 (*To obtain the unary - applyed on a formula*) |
|
153 |
|
154 fun linear_neg tm = linear_cmul (0 - 1) tm; |
|
155 |
|
156 (*Substraction of two terms *) |
|
157 |
|
158 fun linear_sub vars tm1 tm2 = linear_add vars tm1 (linear_neg tm2); |
|
159 |
|
160 |
|
161 (* ------------------------------------------------------------------------- *) |
|
162 (* Linearize a term. *) |
|
163 (* ------------------------------------------------------------------------- *) |
|
164 |
|
165 (* linearises a term from the point of view of Variable Free (x,T). |
|
166 After this fuction the all expressions containig ths variable will have the form |
|
167 c*Free(x,T) + t where c is a constant ant t is a Term which is not containing |
|
168 Free(x,T)*) |
|
169 |
|
170 fun lint vars tm = if is_numeral tm then tm else case tm of |
|
171 (Free (x,T)) => (HOLogic.mk_binop "op +" ((HOLogic.mk_binop "op *" ((mk_numeral 1),Free (x,T))), zero)) |
|
172 |(Bound i) => (Const("op +",HOLogic.intT -->HOLogic.intT -->HOLogic.intT) $ |
|
173 (Const("op *",HOLogic.intT -->HOLogic.intT -->HOLogic.intT) $ (mk_numeral 1) $ (Bound i)) $ zero) |
|
174 |(Const("uminus",_) $ t ) => (linear_neg (lint vars t)) |
|
175 |(Const("op +",_) $ s $ t) => (linear_add vars (lint vars s) (lint vars t)) |
|
176 |(Const("op -",_) $ s $ t) => (linear_sub vars (lint vars s) (lint vars t)) |
|
177 |(Const ("op *",_) $ s $ t) => |
|
178 let val s' = lint vars s |
|
179 val t' = lint vars t |
|
180 in |
|
181 if is_numeral s' then (linear_cmul (dest_numeral s') t') |
|
182 else if is_numeral t' then (linear_cmul (dest_numeral t') s') |
|
183 |
|
184 else (warning "lint: apparent nonlinearity"; raise COOPER) |
|
185 end |
|
186 |_ => error "lint: unknown term"; |
|
187 |
|
188 |
|
189 |
|
190 (* ------------------------------------------------------------------------- *) |
|
191 (* Linearize the atoms in a formula, and eliminate non-strict inequalities. *) |
|
192 (* ------------------------------------------------------------------------- *) |
|
193 |
|
194 fun mkatom vars p t = Const(p,HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ zero $ (lint vars t); |
|
195 |
|
196 fun linform vars (Const ("Divides.op dvd",_) $ c $ t) = |
|
197 let val c' = (mk_numeral(abs(dest_numeral c))) |
|
198 in (HOLogic.mk_binrel "Divides.op dvd" (c,lint vars t)) |
|
199 end |
|
200 |linform vars (Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ s $ t ) = (mkatom vars "op =" (Const ("op -",HOLogic.intT --> HOLogic.intT --> HOLogic.intT) $ t $ s) ) |
|
201 |linform vars (Const("op <",_)$ s $t ) = (mkatom vars "op <" (Const ("op -",HOLogic.intT --> HOLogic.intT --> HOLogic.intT) $ t $ s)) |
|
202 |linform vars (Const("op >",_) $ s $ t ) = (mkatom vars "op <" (Const ("op -",HOLogic.intT --> HOLogic.intT --> HOLogic.intT) $ s $ t)) |
|
203 |linform vars (Const("op <=",_)$ s $ t ) = |
|
204 (mkatom vars "op <" (Const ("op -",HOLogic.intT --> HOLogic.intT --> HOLogic.intT) $ (Const("op +",HOLogic.intT --> HOLogic.intT --> HOLogic.intT) $t $(mk_numeral 1)) $ s)) |
|
205 |linform vars (Const("op >=",_)$ s $ t ) = |
|
206 (mkatom vars "op <" (Const ("op -",HOLogic.intT --> HOLogic.intT --> |
|
207 HOLogic.intT) $ (Const("op +",HOLogic.intT --> HOLogic.intT --> |
|
208 HOLogic.intT) $s $(mk_numeral 1)) $ t)) |
|
209 |
|
210 |linform vars fm = fm; |
|
211 |
|
212 (* ------------------------------------------------------------------------- *) |
|
213 (* Post-NNF transformation eliminating negated inequalities. *) |
|
214 (* ------------------------------------------------------------------------- *) |
|
215 |
|
216 fun posineq fm = case fm of |
|
217 (Const ("Not",_)$(Const("op <",_)$ c $ t)) => |
|
218 (HOLogic.mk_binrel "op <" (zero , (linear_sub [] (mk_numeral 1) (linear_add [] c t ) ))) |
|
219 | ( Const ("op &",_) $ p $ q) => HOLogic.mk_conj (posineq p,posineq q) |
|
220 | ( Const ("op |",_) $ p $ q ) => HOLogic.mk_disj (posineq p,posineq q) |
|
221 | _ => fm; |
|
222 |
|
223 |
|
224 (* ------------------------------------------------------------------------- *) |
|
225 (* Find the LCM of the coefficients of x. *) |
|
226 (* ------------------------------------------------------------------------- *) |
|
227 (*gcd calculates gcd (a,b) and helps lcm_num calculating lcm (a,b)*) |
|
228 |
|
229 fun gcd a b = if a=0 then b else gcd (b mod a) a ; |
|
230 fun lcm_num a b = (abs a*b) div (gcd (abs a) (abs b)); |
|
231 |
|
232 fun formlcm x fm = case fm of |
|
233 (Const (p,_)$ _ $(Const ("op +", _)$(Const ("op *",_)$ c $ y ) $z ) ) => if |
|
234 (is_arith_rel fm) andalso (x = y) then abs(dest_numeral c) else 1 |
|
235 | ( Const ("Not", _) $p) => formlcm x p |
|
236 | ( Const ("op &",_) $ p $ q) => lcm_num (formlcm x p) (formlcm x q) |
|
237 | ( Const ("op |",_) $ p $ q )=> lcm_num (formlcm x p) (formlcm x q) |
|
238 | _ => 1; |
|
239 |
|
240 (* ------------------------------------------------------------------------- *) |
|
241 (* Adjust all coefficients of x in formula; fold in reduction to +/- 1. *) |
|
242 (* ------------------------------------------------------------------------- *) |
|
243 |
|
244 fun adjustcoeff x l fm = |
|
245 case fm of |
|
246 (Const(p,_) $d $( Const ("op +", _)$(Const ("op *",_) $ |
|
247 c $ y ) $z )) => if (is_arith_rel fm) andalso (x = y) then |
|
248 let val m = l div (dest_numeral c) |
|
249 val n = (if p = "op <" then abs(m) else m) |
|
250 val xtm = HOLogic.mk_binop "op *" ((mk_numeral (m div n)), x) |
|
251 in |
|
252 (HOLogic.mk_binrel p ((linear_cmul n d),(HOLogic.mk_binop "op +" ( xtm ,( linear_cmul n z) )))) |
|
253 end |
|
254 else fm |
|
255 |( Const ("Not", _) $ p) => HOLogic.Not $ (adjustcoeff x l p) |
|
256 |( Const ("op &",_) $ p $ q) => HOLogic.conj$(adjustcoeff x l p) $(adjustcoeff x l q) |
|
257 |( Const ("op |",_) $ p $ q) => HOLogic.disj $(adjustcoeff x l p)$ (adjustcoeff x l q) |
|
258 |_ => fm; |
|
259 |
|
260 (* ------------------------------------------------------------------------- *) |
|
261 (* Hence make coefficient of x one in existential formula. *) |
|
262 (* ------------------------------------------------------------------------- *) |
|
263 |
|
264 fun unitycoeff x fm = |
|
265 let val l = formlcm x fm |
|
266 val fm' = adjustcoeff x l fm in |
|
267 if l = 1 then fm' else |
|
268 let val xp = (HOLogic.mk_binop "op +" |
|
269 ((HOLogic.mk_binop "op *" ((mk_numeral 1), x )), zero)) in |
|
270 HOLogic.conj $(HOLogic.mk_binrel "Divides.op dvd" ((mk_numeral l) , xp )) $ (adjustcoeff x l fm) |
|
271 end |
|
272 end; |
|
273 |
|
274 (* adjustcoeffeq l fm adjusts the coeffitients c_i of x overall in fm to l*) |
|
275 (* Here l must be a multiple of all c_i otherwise the obtained formula is not equivalent*) |
|
276 (* |
|
277 fun adjustcoeffeq x l fm = |
|
278 case fm of |
|
279 (Const(p,_) $d $( Const ("op +", _)$(Const ("op *",_) $ |
|
280 c $ y ) $z )) => if (is_arith_rel fm) andalso (x = y) then |
|
281 let val m = l div (dest_numeral c) |
|
282 val n = (if p = "op <" then abs(m) else m) |
|
283 val xtm = (HOLogic.mk_binop "op *" ((mk_numeral ((m div n)*l) ), x)) |
|
284 in (HOLogic.mk_binrel p ((linear_cmul n d),(HOLogic.mk_binop "op +" ( xtm ,( linear_cmul n z) )))) |
|
285 end |
|
286 else fm |
|
287 |( Const ("Not", _) $ p) => HOLogic.Not $ (adjustcoeffeq x l p) |
|
288 |( Const ("op &",_) $ p $ q) => HOLogic.conj$(adjustcoeffeq x l p) $(adjustcoeffeq x l q) |
|
289 |( Const ("op |",_) $ p $ q) => HOLogic.disj $(adjustcoeffeq x l p)$ (adjustcoeffeq x l q) |
|
290 |_ => fm; |
|
291 |
|
292 |
|
293 *) |
|
294 |
|
295 (* ------------------------------------------------------------------------- *) |
|
296 (* The "minus infinity" version. *) |
|
297 (* ------------------------------------------------------------------------- *) |
|
298 |
|
299 fun minusinf x fm = case fm of |
|
300 (Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ (c1 ) $(Const ("op +", _) $(Const ("op *",_) $ c2 $ y) $z)) => |
|
301 if (is_arith_rel fm) andalso (x=y) andalso (c2 = one) andalso (c1 =zero) then HOLogic.false_const |
|
302 else fm |
|
303 |
|
304 |(Const("op <",_) $ c $(Const ("op +", _) $(Const ("op *",_) $ pm1 $ y ) $ z |
|
305 )) => |
|
306 if (x =y) andalso (pm1 = one) andalso (c = zero) then HOLogic.false_const else HOLogic.true_const |
|
307 |
|
308 |(Const ("Not", _) $ p) => HOLogic.Not $ (minusinf x p) |
|
309 |(Const ("op &",_) $ p $ q) => HOLogic.conj $ (minusinf x p) $ (minusinf x q) |
|
310 |(Const ("op |",_) $ p $ q) => HOLogic.disj $ (minusinf x p) $ (minusinf x q) |
|
311 |_ => fm; |
|
312 |
|
313 (* ------------------------------------------------------------------------- *) |
|
314 (* The "Plus infinity" version. *) |
|
315 (* ------------------------------------------------------------------------- *) |
|
316 |
|
317 fun plusinf x fm = case fm of |
|
318 (Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ (c1 ) $(Const ("op +", _) $(Const ("op *",_) $ c2 $ y) $z)) => |
|
319 if (is_arith_rel fm) andalso (x=y) andalso (c2 = one) andalso (c1 =zero) then HOLogic.false_const |
|
320 else fm |
|
321 |
|
322 |(Const("op <",_) $ c $(Const ("op +", _) $(Const ("op *",_) $ pm1 $ y ) $ z |
|
323 )) => |
|
324 if (x =y) andalso (pm1 = one) andalso (c = zero) then HOLogic.true_const else HOLogic.false_const |
|
325 |
|
326 |(Const ("Not", _) $ p) => HOLogic.Not $ (plusinf x p) |
|
327 |(Const ("op &",_) $ p $ q) => HOLogic.conj $ (plusinf x p) $ (plusinf x q) |
|
328 |(Const ("op |",_) $ p $ q) => HOLogic.disj $ (plusinf x p) $ (plusinf x q) |
|
329 |_ => fm; |
|
330 |
|
331 (* ------------------------------------------------------------------------- *) |
|
332 (* The LCM of all the divisors that involve x. *) |
|
333 (* ------------------------------------------------------------------------- *) |
|
334 |
|
335 fun divlcm x (Const("Divides.op dvd",_)$ d $ (Const ("op +",_) $ (Const ("op *",_) $ c $ y ) $ z ) ) = |
|
336 if x = y then abs(dest_numeral d) else 1 |
|
337 |divlcm x ( Const ("Not", _) $ p) = divlcm x p |
|
338 |divlcm x ( Const ("op &",_) $ p $ q) = lcm_num (divlcm x p) (divlcm x q) |
|
339 |divlcm x ( Const ("op |",_) $ p $ q ) = lcm_num (divlcm x p) (divlcm x q) |
|
340 |divlcm x _ = 1; |
|
341 |
|
342 (* ------------------------------------------------------------------------- *) |
|
343 (* Construct the B-set. *) |
|
344 (* ------------------------------------------------------------------------- *) |
|
345 |
|
346 fun bset x fm = case fm of |
|
347 (Const ("Not", _) $ p) => if (is_arith_rel p) then |
|
348 (case p of |
|
349 (Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ c1 $ (Const ("op +", _) $(Const ("op *",_) $c2 $y) $a ) ) |
|
350 => if (is_arith_rel p) andalso (x= y) andalso (c2 = one) andalso (c1 = zero) |
|
351 then [linear_neg a] |
|
352 else bset x p |
|
353 |_ =>[]) |
|
354 |
|
355 else bset x p |
|
356 |(Const ("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ c1 $ (Const ("op +",_) $ (Const ("op *",_) $c2 $ x) $ a)) => if (c1 =zero) andalso (c2 = one) then [linear_neg(linear_add [] a (mk_numeral 1))] else [] |
|
357 |(Const ("op <",_) $ c1$ (Const ("op +",_) $(Const ("op *",_)$ c2 $ x) $ a)) => if (c1 =zero) andalso (c2 = one) then [linear_neg a] else [] |
|
358 |(Const ("op &",_) $ p $ q) => (bset x p) union (bset x q) |
|
359 |(Const ("op |",_) $ p $ q) => (bset x p) union (bset x q) |
|
360 |_ => []; |
|
361 |
|
362 (* ------------------------------------------------------------------------- *) |
|
363 (* Construct the A-set. *) |
|
364 (* ------------------------------------------------------------------------- *) |
|
365 |
|
366 fun aset x fm = case fm of |
|
367 (Const ("Not", _) $ p) => if (is_arith_rel p) then |
|
368 (case p of |
|
369 (Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ c1 $ (Const ("op +", _) $(Const ("op *",_) $c2 $y) $a ) ) |
|
370 => if (x= y) andalso (c2 = one) andalso (c1 = zero) |
|
371 then [linear_neg a] |
|
372 else [] |
|
373 |_ =>[]) |
|
374 |
|
375 else aset x p |
|
376 |(Const ("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ c1 $ (Const ("op +",_) $ (Const ("op *",_) $c2 $ x) $ a)) => if (c1 =zero) andalso (c2 = one) then [linear_sub [] (mk_numeral 1) a] else [] |
|
377 |(Const ("op <",_) $ c1$ (Const ("op +",_) $(Const ("op *",_)$ c2 $ x) $ a)) => if (c1 =zero) andalso (c2 = (mk_numeral (~1))) then [a] else [] |
|
378 |(Const ("op &",_) $ p $ q) => (aset x p) union (aset x q) |
|
379 |(Const ("op |",_) $ p $ q) => (aset x p) union (aset x q) |
|
380 |_ => []; |
|
381 |
|
382 |
|
383 (* ------------------------------------------------------------------------- *) |
|
384 (* Replace top variable with another linear form, retaining canonicality. *) |
|
385 (* ------------------------------------------------------------------------- *) |
|
386 |
|
387 fun linrep vars x t fm = case fm of |
|
388 ((Const(p,_)$ d $ (Const("op +",_)$(Const("op *",_)$ c $ y) $ z))) => |
|
389 if (x = y) andalso (is_arith_rel fm) |
|
390 then |
|
391 let val ct = linear_cmul (dest_numeral c) t |
|
392 in (HOLogic.mk_binrel p (d, linear_add vars ct z)) |
|
393 end |
|
394 else fm |
|
395 |(Const ("Not", _) $ p) => HOLogic.Not $ (linrep vars x t p) |
|
396 |(Const ("op &",_) $ p $ q) => HOLogic.conj $ (linrep vars x t p) $ (linrep vars x t q) |
|
397 |(Const ("op |",_) $ p $ q) => HOLogic.disj $ (linrep vars x t p) $ (linrep vars x t q) |
|
398 |_ => fm; |
|
399 |
|
400 (* ------------------------------------------------------------------------- *) |
|
401 (* Evaluation of constant expressions. *) |
|
402 (* ------------------------------------------------------------------------- *) |
|
403 |
|
404 val operations = |
|
405 [("op =",op=), ("op <",op<), ("op >",op>), ("op <=",op<=) , ("op >=",op>=), |
|
406 ("Divides.op dvd",fn (x,y) =>((y mod x) = 0))]; |
|
407 |
|
408 fun applyoperation (Some f) (a,b) = f (a, b) |
|
409 |applyoperation _ (_, _) = false; |
|
410 |
|
411 (*Evaluation of constant atomic formulas*) |
|
412 |
|
413 fun evalc_atom at = case at of |
|
414 (Const (p,_) $ s $ t) =>( |
|
415 case assoc (operations,p) of |
|
416 Some f => ((if (f ((dest_numeral s),(dest_numeral t))) then HOLogic.true_const else HOLogic.false_const) |
|
417 handle _ => at) |
|
418 | _ => at) |
|
419 |Const("Not",_)$(Const (p,_) $ s $ t) =>( |
|
420 case assoc (operations,p) of |
|
421 Some f => ((if (f ((dest_numeral s),(dest_numeral t))) then |
|
422 HOLogic.false_const else HOLogic.true_const) |
|
423 handle _ => at) |
|
424 | _ => at) |
|
425 | _ => at; |
|
426 |
|
427 (*Function onatoms apllys function f on the atomic formulas involved in a.*) |
|
428 |
|
429 fun onatoms f a = if (is_arith_rel a) then f a else case a of |
|
430 |
|
431 (Const ("Not",_) $ p) => if is_arith_rel p then HOLogic.Not $ (f p) |
|
432 |
|
433 else HOLogic.Not $ (onatoms f p) |
|
434 |(Const ("op &",_) $ p $ q) => HOLogic.conj $ (onatoms f p) $ (onatoms f q) |
|
435 |(Const ("op |",_) $ p $ q) => HOLogic.disj $ (onatoms f p) $ (onatoms f q) |
|
436 |(Const ("op -->",_) $ p $ q) => HOLogic.imp $ (onatoms f p) $ (onatoms f q) |
|
437 |((Const ("op =", Type ("fun",[Type ("bool", []),_]))) $ p $ q) => (Const ("op =", [HOLogic.boolT, HOLogic.boolT] ---> HOLogic.boolT)) $ (onatoms f p) $ (onatoms f q) |
|
438 |(Const("All",_) $ Abs(x,T,p)) => Const("All", [HOLogic.intT --> |
|
439 HOLogic.boolT] ---> HOLogic.boolT)$ Abs (x ,T, (onatoms f p)) |
|
440 |(Const("Ex",_) $ Abs(x,T,p)) => Const("Ex", [HOLogic.intT --> HOLogic.boolT]---> HOLogic.boolT) $ Abs( x ,T, (onatoms f p)) |
|
441 |_ => a; |
|
442 |
|
443 val evalc = onatoms evalc_atom; |
|
444 |
|
445 (* ------------------------------------------------------------------------- *) |
|
446 (* Hence overall quantifier elimination. *) |
|
447 (* ------------------------------------------------------------------------- *) |
|
448 |
|
449 (*Applyes a function iteratively on the list*) |
|
450 |
|
451 fun end_itlist f [] = error "end_itlist" |
|
452 |end_itlist f [x] = x |
|
453 |end_itlist f (h::t) = f h (end_itlist f t); |
|
454 |
|
455 |
|
456 (*list_disj[conj] makes a disj[conj] of a given list. used with conjucts or disjuncts |
|
457 it liearises iterated conj[disj]unctions. *) |
|
458 |
|
459 fun disj_help p q = HOLogic.disj $ p $ q ; |
|
460 |
|
461 fun list_disj l = |
|
462 if l = [] then HOLogic.false_const else end_itlist disj_help l; |
|
463 |
|
464 fun conj_help p q = HOLogic.conj $ p $ q ; |
|
465 |
|
466 fun list_conj l = |
|
467 if l = [] then HOLogic.true_const else end_itlist conj_help l; |
|
468 |
|
469 (*Simplification of Formulas *) |
|
470 |
|
471 (*Function q_bnd_chk checks if a quantified Formula makes sens : Means if in |
|
472 the body of the existential quantifier there are bound variables to the |
|
473 existential quantifier.*) |
|
474 |
|
475 fun has_bound fm =let fun has_boundh fm i = case fm of |
|
476 Bound n => (i = n) |
|
477 |Abs (_,_,p) => has_boundh p (i+1) |
|
478 |t1 $ t2 => (has_boundh t1 i) orelse (has_boundh t2 i) |
|
479 |_ =>false |
|
480 |
|
481 in case fm of |
|
482 Bound _ => true |
|
483 |Abs (_,_,p) => has_boundh p 0 |
|
484 |t1 $ t2 => (has_bound t1 ) orelse (has_bound t2 ) |
|
485 |_ =>false |
|
486 end; |
|
487 |
|
488 (*has_sub_abs checks if in a given Formula there are subformulas which are quantifed |
|
489 too. Is no used no more.*) |
|
490 |
|
491 fun has_sub_abs fm = case fm of |
|
492 Abs (_,_,_) => true |
|
493 |t1 $ t2 => (has_bound t1 ) orelse (has_bound t2 ) |
|
494 |_ =>false ; |
|
495 |
|
496 (*update_bounds called with i=0 udates the numeration of bounded variables because the |
|
497 formula will not be quantified any more.*) |
|
498 |
|
499 fun update_bounds fm i = case fm of |
|
500 Bound n => if n >= i then Bound (n-1) else fm |
|
501 |Abs (x,T,p) => Abs(x,T,(update_bounds p (i+1))) |
|
502 |t1 $ t2 => (update_bounds t1 i) $ (update_bounds t2 i) |
|
503 |_ => fm ; |
|
504 |
|
505 (*psimpl : Simplification of propositions (general purpose)*) |
|
506 fun psimpl1 fm = case fm of |
|
507 Const("Not",_) $ Const ("False",_) => HOLogic.true_const |
|
508 | Const("Not",_) $ Const ("True",_) => HOLogic.false_const |
|
509 | Const("op &",_) $ Const ("False",_) $ q => HOLogic.false_const |
|
510 | Const("op &",_) $ p $ Const ("False",_) => HOLogic.false_const |
|
511 | Const("op &",_) $ Const ("True",_) $ q => q |
|
512 | Const("op &",_) $ p $ Const ("True",_) => p |
|
513 | Const("op |",_) $ Const ("False",_) $ q => q |
|
514 | Const("op |",_) $ p $ Const ("False",_) => p |
|
515 | Const("op |",_) $ Const ("True",_) $ q => HOLogic.true_const |
|
516 | Const("op |",_) $ p $ Const ("True",_) => HOLogic.true_const |
|
517 | Const("op -->",_) $ Const ("False",_) $ q => HOLogic.true_const |
|
518 | Const("op -->",_) $ Const ("True",_) $ q => q |
|
519 | Const("op -->",_) $ p $ Const ("True",_) => HOLogic.true_const |
|
520 | Const("op -->",_) $ p $ Const ("False",_) => HOLogic.Not $ p |
|
521 | Const("op =", Type ("fun",[Type ("bool", []),_])) $ Const ("True",_) $ q => q |
|
522 | Const("op =", Type ("fun",[Type ("bool", []),_])) $ p $ Const ("True",_) => p |
|
523 | Const("op =", Type ("fun",[Type ("bool", []),_])) $ Const ("False",_) $ q => HOLogic.Not $ q |
|
524 | Const("op =", Type ("fun",[Type ("bool", []),_])) $ p $ Const ("False",_) => HOLogic.Not $ p |
|
525 | _ => fm; |
|
526 |
|
527 fun psimpl fm = case fm of |
|
528 Const ("Not",_) $ p => psimpl1 (HOLogic.Not $ (psimpl p)) |
|
529 | Const("op &",_) $ p $ q => psimpl1 (HOLogic.mk_conj (psimpl p,psimpl q)) |
|
530 | Const("op |",_) $ p $ q => psimpl1 (HOLogic.mk_disj (psimpl p,psimpl q)) |
|
531 | Const("op -->",_) $ p $ q => psimpl1 (HOLogic.mk_imp(psimpl p,psimpl q)) |
|
532 | Const("op =", Type ("fun",[Type ("bool", []),_])) $ p $ q => psimpl1 (HOLogic.mk_eq(psimpl p,psimpl q)) |
|
533 | _ => fm; |
|
534 |
|
535 |
|
536 (*simpl : Simplification of Terms involving quantifiers too. |
|
537 This function is able to drop out some quantified expressions where there are no |
|
538 bound varaibles.*) |
|
539 |
|
540 fun simpl1 fm = |
|
541 case fm of |
|
542 Const("All",_) $Abs(x,_,p) => if (has_bound fm ) then fm |
|
543 else (update_bounds p 0) |
|
544 | Const("Ex",_) $ Abs (x,_,p) => if has_bound fm then fm |
|
545 else (update_bounds p 0) |
|
546 | _ => psimpl1 fm; |
|
547 |
|
548 fun simpl fm = case fm of |
|
549 Const ("Not",_) $ p => simpl1 (HOLogic.Not $(simpl p)) |
|
550 | Const ("op &",_) $ p $ q => simpl1 (HOLogic.mk_conj (simpl p ,simpl q)) |
|
551 | Const ("op |",_) $ p $ q => simpl1 (HOLogic.mk_disj (simpl p ,simpl q )) |
|
552 | Const ("op -->",_) $ p $ q => simpl1 (HOLogic.mk_imp(simpl p ,simpl q )) |
|
553 | Const("op =", Type ("fun",[Type ("bool", []),_]))$ p $ q => simpl1 |
|
554 (HOLogic.mk_eq(simpl p ,simpl q )) |
|
555 | Const ("All",Ta) $ Abs(Vn,VT,p) => simpl1(Const("All",Ta) $ |
|
556 Abs(Vn,VT,simpl p )) |
|
557 | Const ("Ex",Ta) $ Abs(Vn,VT,p) => simpl1(Const("Ex",Ta) $ |
|
558 Abs(Vn,VT,simpl p )) |
|
559 | _ => fm; |
|
560 |
|
561 (* ------------------------------------------------------------------------- *) |
|
562 |
|
563 (* Puts fm into NNF*) |
|
564 |
|
565 fun nnf fm = if (is_arith_rel fm) then fm |
|
566 else (case fm of |
|
567 ( Const ("op &",_) $ p $ q) => HOLogic.conj $ (nnf p) $(nnf q) |
|
568 | (Const("op |",_) $ p $q) => HOLogic.disj $ (nnf p)$(nnf q) |
|
569 | (Const ("op -->",_) $ p $ q) => HOLogic.disj $ (nnf (HOLogic.Not $ p)) $ (nnf q) |
|
570 | ((Const ("op =", Type ("fun",[Type ("bool", []),_]))) $ p $ q) =>(HOLogic.disj $ (HOLogic.conj $ (nnf p) $ (nnf q)) $ (HOLogic.conj $ (nnf (HOLogic.Not $ p) ) $ (nnf(HOLogic.Not $ q)))) |
|
571 | (Const ("Not",_)) $ ((Const ("Not",_)) $ p) => (nnf p) |
|
572 | (Const ("Not",_)) $ (( Const ("op &",_)) $ p $ q) =>HOLogic.disj $ (nnf(HOLogic.Not $ p)) $ (nnf(HOLogic.Not $q)) |
|
573 | (Const ("Not",_)) $ (( Const ("op |",_)) $ p $ q) =>HOLogic.conj $ (nnf(HOLogic.Not $ p)) $ (nnf(HOLogic.Not $ q)) |
|
574 | (Const ("Not",_)) $ (( Const ("op -->",_)) $ p $ q ) =>HOLogic.conj $ (nnf p) $(nnf(HOLogic.Not $ q)) |
|
575 | (Const ("Not",_)) $ ((Const ("op =", Type ("fun",[Type ("bool", []),_]))) $ p $ q ) =>(HOLogic.disj $ (HOLogic.conj $(nnf p) $ (nnf(HOLogic.Not $ q))) $ (HOLogic.conj $(nnf(HOLogic.Not $ p)) $ (nnf q))) |
|
576 | _ => fm); |
|
577 |
|
578 |
|
579 (* Function remred to remove redundancy in a list while keeping the order of appearance of the |
|
580 elements. but VERY INEFFICIENT!! *) |
|
581 |
|
582 fun remred1 el [] = [] |
|
583 |remred1 el (h::t) = if el=h then (remred1 el t) else h::(remred1 el t); |
|
584 |
|
585 fun remred [] = [] |
|
586 |remred (x::l) = x::(remred1 x (remred l)); |
|
587 |
|
588 (*Makes sure that all free Variables are of the type integer but this function is only |
|
589 used temporarily, this job must be done by the parser later on.*) |
|
590 |
|
591 fun mk_uni_vars T (node $ rest) = (case node of |
|
592 Free (name,_) => Free (name,T) $ (mk_uni_vars T rest) |
|
593 |_=> (mk_uni_vars T node) $ (mk_uni_vars T rest ) ) |
|
594 |mk_uni_vars T (Free (v,_)) = Free (v,T) |
|
595 |mk_uni_vars T tm = tm; |
|
596 |
|
597 fun mk_uni_int T (Const ("0",T2)) = if T = T2 then (mk_numeral 0) else (Const ("0",T2)) |
|
598 |mk_uni_int T (Const ("1",T2)) = if T = T2 then (mk_numeral 1) else (Const ("1",T2)) |
|
599 |mk_uni_int T (node $ rest) = (mk_uni_int T node) $ (mk_uni_int T rest ) |
|
600 |mk_uni_int T (Abs(AV,AT,p)) = Abs(AV,AT,mk_uni_int T p) |
|
601 |mk_uni_int T tm = tm; |
|
602 |
|
603 |
|
604 (* Minusinfinity Version*) |
|
605 fun coopermi vars1 fm = |
|
606 case fm of |
|
607 Const ("Ex",_) $ Abs(x0,T,p0) => let |
|
608 val (xn,p1) = variant_abs (x0,T,p0) |
|
609 val x = Free (xn,T) |
|
610 val vars = (xn::vars1) |
|
611 val p = unitycoeff x (posineq (simpl p1)) |
|
612 val p_inf = simpl (minusinf x p) |
|
613 val bset = bset x p |
|
614 val js = 1 upto divlcm x p |
|
615 fun p_element j b = linrep vars x (linear_add vars b (mk_numeral j)) p |
|
616 fun stage j = list_disj (linrep vars x (mk_numeral j) p_inf :: map (p_element j) bset) |
|
617 in (list_disj (map stage js)) |
|
618 end |
|
619 | _ => error "cooper: not an existential formula"; |
|
620 |
|
621 |
|
622 |
|
623 (* The plusinfinity version of cooper*) |
|
624 fun cooperpi vars1 fm = |
|
625 case fm of |
|
626 Const ("Ex",_) $ Abs(x0,T,p0) => let |
|
627 val (xn,p1) = variant_abs (x0,T,p0) |
|
628 val x = Free (xn,T) |
|
629 val vars = (xn::vars1) |
|
630 val p = unitycoeff x (posineq (simpl p1)) |
|
631 val p_inf = simpl (plusinf x p) |
|
632 val aset = aset x p |
|
633 val js = 1 upto divlcm x p |
|
634 fun p_element j a = linrep vars x (linear_sub vars a (mk_numeral j)) p |
|
635 fun stage j = list_disj (linrep vars x (mk_numeral j) p_inf :: map (p_element j) aset) |
|
636 in (list_disj (map stage js)) |
|
637 end |
|
638 | _ => error "cooper: not an existential formula"; |
|
639 |
|
640 |
|
641 |
|
642 (*Cooper main procedure*) |
|
643 |
|
644 fun cooper vars1 fm = |
|
645 case fm of |
|
646 Const ("Ex",_) $ Abs(x0,T,p0) => let |
|
647 val (xn,p1) = variant_abs (x0,T,p0) |
|
648 val x = Free (xn,T) |
|
649 val vars = (xn::vars1) |
|
650 val p = unitycoeff x (posineq (simpl p1)) |
|
651 val ast = aset x p |
|
652 val bst = bset x p |
|
653 val js = 1 upto divlcm x p |
|
654 val (p_inf,f,S ) = |
|
655 if (length bst) < (length ast) |
|
656 then (minusinf x p,linear_add,bst) |
|
657 else (plusinf x p, linear_sub,ast) |
|
658 fun p_element j a = linrep vars x (f vars a (mk_numeral j)) p |
|
659 fun stage j = list_disj (linrep vars x (mk_numeral j) p_inf :: map (p_element j) S) |
|
660 in (list_disj (map stage js)) |
|
661 end |
|
662 | _ => error "cooper: not an existential formula"; |
|
663 |
|
664 |
|
665 |
|
666 |
|
667 (*Function itlist applys a double parametred function f : 'a->'b->b iteratively to a List l : 'a |
|
668 list With End condition b. ict calculates f(e1,f(f(e2,f(e3,...(...f(en,b))..))))) |
|
669 assuming l = [e1,e2,...,en]*) |
|
670 |
|
671 fun itlist f l b = case l of |
|
672 [] => b |
|
673 | (h::t) => f h (itlist f t b); |
|
674 |
|
675 (* ------------------------------------------------------------------------- *) |
|
676 (* Free variables in terms and formulas. *) |
|
677 (* ------------------------------------------------------------------------- *) |
|
678 |
|
679 fun fvt tml = case tml of |
|
680 [] => [] |
|
681 | Free(x,_)::r => x::(fvt r) |
|
682 |
|
683 fun fv fm = fvt (term_frees fm); |
|
684 |
|
685 |
|
686 (* ========================================================================= *) |
|
687 (* Quantifier elimination. *) |
|
688 (* ========================================================================= *) |
|
689 (*conj[/disj]uncts lists iterated conj[disj]unctions*) |
|
690 |
|
691 fun disjuncts fm = case fm of |
|
692 Const ("op |",_) $ p $ q => (disjuncts p) @ (disjuncts q) |
|
693 | _ => [fm]; |
|
694 |
|
695 fun conjuncts fm = case fm of |
|
696 Const ("op &",_) $p $ q => (conjuncts p) @ (conjuncts q) |
|
697 | _ => [fm]; |
|
698 |
|
699 |
|
700 |
|
701 (* ------------------------------------------------------------------------- *) |
|
702 (* Lift procedure given literal modifier, formula normalizer & basic quelim. *) |
|
703 (* ------------------------------------------------------------------------- *) |
|
704 |
|
705 fun lift_qelim afn nfn qfn isat = |
|
706 let fun qelim x vars p = |
|
707 let val cjs = conjuncts p |
|
708 val (ycjs,ncjs) = partition (has_bound) cjs in |
|
709 (if ycjs = [] then p else |
|
710 let val q = (qfn vars ((HOLogic.exists_const HOLogic.intT |
|
711 ) $ Abs(x,HOLogic.intT,(list_conj ycjs)))) in |
|
712 (itlist conj_help ncjs q) |
|
713 end) |
|
714 end |
|
715 |
|
716 fun qelift vars fm = if (isat fm) then afn vars fm |
|
717 else |
|
718 case fm of |
|
719 Const ("Not",_) $ p => HOLogic.Not $ (qelift vars p) |
|
720 | Const ("op &",_) $ p $q => HOLogic.conj $ (qelift vars p) $ (qelift vars q) |
|
721 | Const ("op |",_) $ p $ q => HOLogic.disj $ (qelift vars p) $ (qelift vars q) |
|
722 | Const ("op -->",_) $ p $ q => HOLogic.imp $ (qelift vars p) $ (qelift vars q) |
|
723 | Const ("op =",Type ("fun",[Type ("bool", []),_])) $ p $ q => HOLogic.mk_eq ((qelift vars p),(qelift vars q)) |
|
724 | Const ("All",QT) $ Abs(x,T,p) => HOLogic.Not $(qelift vars (Const ("Ex",QT) $ Abs(x,T,(HOLogic.Not $ p)))) |
|
725 | Const ("Ex",_) $ Abs (x,T,p) => let val djs = disjuncts(nfn(qelift (x::vars) p)) in |
|
726 list_disj(map (qelim x vars) djs) end |
|
727 | _ => fm |
|
728 |
|
729 in (fn fm => simpl(qelift (fv fm) fm)) |
|
730 end; |
|
731 |
|
732 |
|
733 (* ------------------------------------------------------------------------- *) |
|
734 (* Cleverer (proposisional) NNF with conditional and literal modification. *) |
|
735 (* ------------------------------------------------------------------------- *) |
|
736 |
|
737 (*Function Negate used by cnnf, negates a formula p*) |
|
738 |
|
739 fun negate (Const ("Not",_) $ p) = p |
|
740 |negate p = (HOLogic.Not $ p); |
|
741 |
|
742 fun cnnf lfn = |
|
743 let fun cnnfh fm = case fm of |
|
744 (Const ("op &",_) $ p $ q) => HOLogic.mk_conj(cnnfh p,cnnfh q) |
|
745 | (Const ("op |",_) $ p $ q) => HOLogic.mk_disj(cnnfh p,cnnfh q) |
|
746 | (Const ("op -->",_) $ p $q) => HOLogic.mk_disj(cnnfh(HOLogic.Not $ p),cnnfh q) |
|
747 | (Const ("op =",Type ("fun",[Type ("bool", []),_])) $ p $ q) => HOLogic.mk_disj( |
|
748 HOLogic.mk_conj(cnnfh p,cnnfh q), |
|
749 HOLogic.mk_conj(cnnfh(HOLogic.Not $ p),cnnfh(HOLogic.Not $q))) |
|
750 |
|
751 | (Const ("Not",_) $ (Const("Not",_) $ p)) => cnnfh p |
|
752 | (Const ("Not",_) $ (Const ("op &",_) $ p $ q)) => HOLogic.mk_disj(cnnfh(HOLogic.Not $ p),cnnfh(HOLogic.Not $ q)) |
|
753 | (Const ("Not",_) $(Const ("op |",_) $ (Const ("op &",_) $ p $ q) $ |
|
754 (Const ("op &",_) $ p1 $ r))) => if p1 = negate p then |
|
755 HOLogic.mk_disj( |
|
756 cnnfh (HOLogic.mk_conj(p,cnnfh(HOLogic.Not $ q))), |
|
757 cnnfh (HOLogic.mk_conj(p1,cnnfh(HOLogic.Not $ r)))) |
|
758 else HOLogic.mk_conj( |
|
759 cnnfh (HOLogic.mk_disj(cnnfh (HOLogic.Not $ p),cnnfh(HOLogic.Not $ q))), |
|
760 cnnfh (HOLogic.mk_disj(cnnfh (HOLogic.Not $ p1),cnnfh(HOLogic.Not $ r))) |
|
761 ) |
|
762 | (Const ("Not",_) $ (Const ("op |",_) $ p $ q)) => HOLogic.mk_conj(cnnfh(HOLogic.Not $ p),cnnfh(HOLogic.Not $ q)) |
|
763 | (Const ("Not",_) $ (Const ("op -->",_) $ p $q)) => HOLogic.mk_conj(cnnfh p,cnnfh(HOLogic.Not $ q)) |
|
764 | (Const ("Not",_) $ (Const ("op =",Type ("fun",[Type ("bool", []),_])) $ p $ q)) => HOLogic.mk_disj(HOLogic.mk_conj(cnnfh p,cnnfh(HOLogic.Not $ q)),HOLogic.mk_conj(cnnfh(HOLogic.Not $ p),cnnfh q)) |
|
765 | _ => lfn fm |
|
766 in cnnfh o simpl |
|
767 end; |
|
768 |
|
769 (*End- function the quantifierelimination an decion procedure of presburger formulas.*) |
|
770 val integer_qelim = simpl o evalc o (lift_qelim linform (simpl o (cnnf posineq o evalc)) cooper is_arith_rel) ; |
|
771 |
|
772 end; |
|
773 |