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