src/HOL/Integ/cooper_dec.ML
changeset 23146 0bc590051d95
parent 23145 5d8faadf3ecf
child 23147 a5db2f7d7654
     1.1 --- a/src/HOL/Integ/cooper_dec.ML	Thu May 31 11:00:06 2007 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,920 +0,0 @@
     1.4 -(*  Title:      HOL/Integ/cooper_dec.ML
     1.5 -    ID:         $Id$
     1.6 -    Author:     Amine Chaieb and Tobias Nipkow, TU Muenchen
     1.7 -
     1.8 -File containing the implementation of Cooper Algorithm
     1.9 -decision procedure (intensively inspired from J.Harrison)
    1.10 -*)
    1.11 -
    1.12 -
    1.13 -signature COOPER_DEC = 
    1.14 -sig
    1.15 -  exception COOPER
    1.16 -  val mk_number : IntInf.int -> term
    1.17 -  val zero : term
    1.18 -  val one : term
    1.19 -  val dest_number : term -> IntInf.int
    1.20 -  val is_number : term -> bool
    1.21 -  val is_arith_rel : term -> bool
    1.22 -  val linear_cmul : IntInf.int -> term -> term
    1.23 -  val linear_add : string list -> term -> term -> term 
    1.24 -  val linear_sub : string list -> term -> term -> term 
    1.25 -  val linear_neg : term -> term
    1.26 -  val lint : string list -> term -> term
    1.27 -  val linform : string list -> term -> term
    1.28 -  val formlcm : term -> term -> IntInf.int
    1.29 -  val adjustcoeff : term -> IntInf.int -> term -> term
    1.30 -  val unitycoeff : term -> term -> term
    1.31 -  val divlcm : term -> term -> IntInf.int
    1.32 -  val bset : term -> term -> term list
    1.33 -  val aset : term -> term -> term list
    1.34 -  val linrep : string list -> term -> term -> term -> term
    1.35 -  val list_disj : term list -> term
    1.36 -  val list_conj : term list -> term
    1.37 -  val simpl : term -> term
    1.38 -  val fv : term -> string list
    1.39 -  val negate : term -> term
    1.40 -  val operations : (string * (IntInf.int * IntInf.int -> bool)) list
    1.41 -  val conjuncts : term -> term list
    1.42 -  val disjuncts : term -> term list
    1.43 -  val has_bound : term -> bool
    1.44 -  val minusinf : term -> term -> term
    1.45 -  val plusinf : term -> term -> term
    1.46 -  val onatoms : (term -> term) -> term -> term
    1.47 -  val evalc : term -> term
    1.48 -  val cooper_w : string list -> term -> (term option * term)
    1.49 -  val integer_qelim : Term.term -> Term.term
    1.50 -end;
    1.51 -
    1.52 -structure CooperDec : COOPER_DEC =
    1.53 -struct
    1.54 -
    1.55 -(* ========================================================================= *) 
    1.56 -(* Cooper's algorithm for Presburger arithmetic.                             *) 
    1.57 -(* ========================================================================= *) 
    1.58 -exception COOPER;
    1.59 -
    1.60 -
    1.61 -(* ------------------------------------------------------------------------- *) 
    1.62 -(* Lift operations up to numerals.                                           *) 
    1.63 -(* ------------------------------------------------------------------------- *) 
    1.64 - 
    1.65 -(*Assumption : The construction of atomar formulas in linearl arithmetic is based on 
    1.66 -relation operations of Type : [IntInf.int,IntInf.int]---> bool *) 
    1.67 - 
    1.68 -(* ------------------------------------------------------------------------- *) 
    1.69 - 
    1.70 -(*Function is_arith_rel returns true if and only if the term is an atomar presburger 
    1.71 -formula *) 
    1.72 -fun is_arith_rel tm = case tm
    1.73 - of Const(p, Type ("fun", [Type ("IntDef.int", []), Type ("fun", [Type ("IntDef.int", []),
    1.74 -      Type ("bool", [])])])) $ _ $_ => true
    1.75 -  | _ => false;
    1.76 - 
    1.77 -(*Function is_arith_rel returns true if and only if the term is an operation of the 
    1.78 -form [int,int]---> int*) 
    1.79 - 
    1.80 -val mk_number = HOLogic.mk_number HOLogic.intT;
    1.81 -val zero = mk_number 0; 
    1.82 -val one = mk_number 1; 
    1.83 -fun dest_number t = let
    1.84 -    val (T, n) = HOLogic.dest_number t
    1.85 -  in if T = HOLogic.intT then n else error ("bad typ: " ^ Display.raw_string_of_typ T) end;
    1.86 -val is_number = can dest_number; 
    1.87 -
    1.88 -(*maps a unary natural function on a term containing an natural number*) 
    1.89 -fun numeral1 f n = mk_number (f (dest_number n)); 
    1.90 - 
    1.91 -(*maps a binary natural function on 2 term containing  natural numbers*) 
    1.92 -fun numeral2 f m n = mk_number (f (dest_number m) (dest_number n));
    1.93 - 
    1.94 -(* ------------------------------------------------------------------------- *) 
    1.95 -(* Operations on canonical linear terms c1 * x1 + ... + cn * xn + k          *) 
    1.96 -(*                                                                           *) 
    1.97 -(* Note that we're quite strict: the ci must be present even if 1            *) 
    1.98 -(* (but if 0 we expect the monomial to be omitted) and k must be there       *) 
    1.99 -(* even if it's zero. Thus, it's a constant iff not an addition term.        *) 
   1.100 -(* ------------------------------------------------------------------------- *)  
   1.101 - 
   1.102 - 
   1.103 -fun linear_cmul n tm =  if n = 0 then zero else let fun times n k = n*k in  
   1.104 -  ( case tm of  
   1.105 -     (Const(@{const_name HOL.plus},T)  $  (Const (@{const_name HOL.times},T1 ) $c1 $  x1) $ rest) => 
   1.106 -       Const(@{const_name HOL.plus},T) $ ((Const(@{const_name HOL.times},T1) $ (numeral1 (times n) c1) $ x1)) $ (linear_cmul n rest) 
   1.107 -    |_ =>  numeral1 (times n) tm) 
   1.108 -    end ; 
   1.109 - 
   1.110 - 
   1.111 - 
   1.112 - 
   1.113 -(* Whether the first of two items comes earlier in the list  *) 
   1.114 -fun earlier [] x y = false 
   1.115 -	|earlier (h::t) x y =if h = y then false 
   1.116 -              else if h = x then true 
   1.117 -              	else earlier t x y ; 
   1.118 - 
   1.119 -fun earlierv vars (Bound i) (Bound j) = i < j 
   1.120 -   |earlierv vars (Bound _) _ = true 
   1.121 -   |earlierv vars _ (Bound _)  = false 
   1.122 -   |earlierv vars (Free (x,_)) (Free (y,_)) = earlier vars x y; 
   1.123 - 
   1.124 - 
   1.125 -fun linear_add vars tm1 tm2 = 
   1.126 -  let fun addwith x y = x + y in
   1.127 - (case (tm1,tm2) of 
   1.128 -	((Const (@{const_name HOL.plus},T1) $ ( Const(@{const_name HOL.times},T2) $ c1 $  x1) $ rest1),(Const 
   1.129 -	(@{const_name HOL.plus},T3)$( Const(@{const_name HOL.times},T4) $ c2 $  x2) $ rest2)) => 
   1.130 -         if x1 = x2 then 
   1.131 -              let val c = (numeral2 (addwith) c1 c2) 
   1.132 -	      in 
   1.133 -              if c = zero then (linear_add vars rest1  rest2)  
   1.134 -	      else (Const(@{const_name HOL.plus},T1) $ (Const(@{const_name HOL.times},T2) $ c $ x1) $ (linear_add vars  rest1 rest2)) 
   1.135 -              end 
   1.136 -	   else 
   1.137 -		if earlierv vars x1 x2 then (Const(@{const_name HOL.plus},T1) $  
   1.138 -		(Const(@{const_name HOL.times},T2)$ c1 $ x1) $ (linear_add vars rest1 tm2)) 
   1.139 -    	       else (Const(@{const_name HOL.plus},T1) $ (Const(@{const_name HOL.times},T2) $ c2 $ x2) $ (linear_add vars tm1 rest2)) 
   1.140 -   	|((Const(@{const_name HOL.plus},T1) $ (Const(@{const_name HOL.times},T2) $ c1 $ x1) $ rest1) ,_) => 
   1.141 -    	  (Const(@{const_name HOL.plus},T1)$ (Const(@{const_name HOL.times},T2) $ c1 $ x1) $ (linear_add vars 
   1.142 -	  rest1 tm2)) 
   1.143 -   	|(_, (Const(@{const_name HOL.plus},T1) $(Const(@{const_name HOL.times},T2) $ c2 $ x2) $ rest2)) => 
   1.144 -      	  (Const(@{const_name HOL.plus},T1) $ (Const(@{const_name HOL.times},T2) $ c2 $ x2) $ (linear_add vars tm1 
   1.145 -	  rest2)) 
   1.146 -   	| (_,_) => numeral2 (addwith) tm1 tm2) 
   1.147 -	 
   1.148 -	end; 
   1.149 - 
   1.150 -(*To obtain the unary - applyed on a formula*) 
   1.151 - 
   1.152 -fun linear_neg tm = linear_cmul (0 - 1) tm; 
   1.153 - 
   1.154 -(*Substraction of two terms *) 
   1.155 - 
   1.156 -fun linear_sub vars tm1 tm2 = linear_add vars tm1 (linear_neg tm2); 
   1.157 - 
   1.158 - 
   1.159 -(* ------------------------------------------------------------------------- *) 
   1.160 -(* Linearize a term.                                                         *) 
   1.161 -(* ------------------------------------------------------------------------- *) 
   1.162 - 
   1.163 -(* linearises a term from the point of view of Variable Free (x,T). 
   1.164 -After this fuction the all expressions containig ths variable will have the form  
   1.165 - c*Free(x,T) + t where c is a constant ant t is a Term which is not containing 
   1.166 - Free(x,T)*) 
   1.167 -  
   1.168 -fun lint vars tm = if is_number tm then tm else case tm of 
   1.169 -   (Free (x,T)) =>  (HOLogic.mk_binop @{const_name HOL.plus} ((HOLogic.mk_binop @{const_name HOL.times} ((mk_number 1),Free (x,T))), zero)) 
   1.170 -  |(Bound i) =>  (Const(@{const_name HOL.plus},HOLogic.intT -->HOLogic.intT -->HOLogic.intT) $ 
   1.171 -  (Const(@{const_name HOL.times},HOLogic.intT -->HOLogic.intT -->HOLogic.intT) $ (mk_number 1) $ (Bound i)) $ zero) 
   1.172 -  |(Const(@{const_name HOL.uminus},_) $ t ) => (linear_neg (lint vars t)) 
   1.173 -  |(Const(@{const_name HOL.plus},_) $ s $ t) => (linear_add vars (lint vars s) (lint vars t)) 
   1.174 -  |(Const(@{const_name HOL.minus},_) $ s $ t) => (linear_sub vars (lint vars s) (lint vars t)) 
   1.175 -  |(Const (@{const_name HOL.times},_) $ s $ t) => 
   1.176 -        let val s' = lint vars s  
   1.177 -            val t' = lint vars t  
   1.178 -        in 
   1.179 -        if is_number s' then (linear_cmul (dest_number s') t') 
   1.180 -        else if is_number t' then (linear_cmul (dest_number t') s') 
   1.181 - 
   1.182 -         else raise COOPER
   1.183 -         end 
   1.184 -  |_ =>  raise COOPER;
   1.185 -   
   1.186 - 
   1.187 - 
   1.188 -(* ------------------------------------------------------------------------- *) 
   1.189 -(* Linearize the atoms in a formula, and eliminate non-strict inequalities.  *) 
   1.190 -(* ------------------------------------------------------------------------- *) 
   1.191 - 
   1.192 -fun mkatom vars p t = Const(p,HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ zero $ (lint vars t); 
   1.193 - 
   1.194 -fun linform vars (Const ("Divides.dvd",_) $ c $ t) =
   1.195 -    if is_number c then   
   1.196 -      let val c' = (mk_number(abs(dest_number c)))  
   1.197 -      in (HOLogic.mk_binrel "Divides.dvd" (c,lint vars t)) 
   1.198 -      end 
   1.199 -    else (warning "Nonlinear term --- Non numeral leftside at dvd"
   1.200 -      ;raise COOPER)
   1.201 -  |linform vars  (Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ s $ t ) = (mkatom vars "op =" (Const (@{const_name HOL.minus},HOLogic.intT --> HOLogic.intT --> HOLogic.intT) $ t $ s) ) 
   1.202 -  |linform vars  (Const(@{const_name Orderings.less},_)$ s $t ) = (mkatom vars @{const_name Orderings.less} (Const (@{const_name HOL.minus},HOLogic.intT --> HOLogic.intT --> HOLogic.intT) $ t $ s))
   1.203 -  |linform vars  (Const("op >",_) $ s $ t ) = (mkatom vars @{const_name Orderings.less} (Const (@{const_name HOL.minus},HOLogic.intT --> HOLogic.intT --> HOLogic.intT) $ s $ t)) 
   1.204 -  |linform vars  (Const(@{const_name Orderings.less_eq},_)$ s $ t ) = 
   1.205 -        (mkatom vars @{const_name Orderings.less} (Const (@{const_name HOL.minus},HOLogic.intT --> HOLogic.intT --> HOLogic.intT) $ (Const(@{const_name HOL.plus},HOLogic.intT --> HOLogic.intT --> HOLogic.intT) $t $(mk_number 1)) $ s)) 
   1.206 -  |linform vars  (Const("op >=",_)$ s $ t ) = 
   1.207 -        (mkatom vars @{const_name Orderings.less} (Const (@{const_name HOL.minus},HOLogic.intT --> HOLogic.intT --> 
   1.208 -	HOLogic.intT) $ (Const(@{const_name HOL.plus},HOLogic.intT --> HOLogic.intT --> 
   1.209 -	HOLogic.intT) $s $(mk_number 1)) $ t)) 
   1.210 - 
   1.211 -   |linform vars  fm =  fm; 
   1.212 - 
   1.213 -(* ------------------------------------------------------------------------- *) 
   1.214 -(* Post-NNF transformation eliminating negated inequalities.                 *) 
   1.215 -(* ------------------------------------------------------------------------- *) 
   1.216 - 
   1.217 -fun posineq fm = case fm of  
   1.218 - (Const ("Not",_)$(Const(@{const_name Orderings.less},_)$ c $ t)) =>
   1.219 -   (HOLogic.mk_binrel @{const_name Orderings.less}  (zero , (linear_sub [] (mk_number 1) (linear_add [] c t ) ))) 
   1.220 -  | ( Const ("op &",_) $ p $ q)  => HOLogic.mk_conj (posineq p,posineq q)
   1.221 -  | ( Const ("op |",_) $ p $ q ) => HOLogic.mk_disj (posineq p,posineq q)
   1.222 -  | _ => fm; 
   1.223 -  
   1.224 -
   1.225 -(* ------------------------------------------------------------------------- *) 
   1.226 -(* Find the LCM of the coefficients of x.                                    *) 
   1.227 -(* ------------------------------------------------------------------------- *) 
   1.228 -(*gcd calculates gcd (a,b) and helps lcm_num calculating lcm (a,b)*) 
   1.229 - 
   1.230 -(*BEWARE: replaces Library.gcd!! There is also Library.lcm!*)
   1.231 -fun gcd (a:IntInf.int) b = if a=0 then b else gcd (b mod a) a ; 
   1.232 -fun lcm_num a b = (abs a*b) div (gcd (abs a) (abs b)); 
   1.233 - 
   1.234 -fun formlcm x fm = case fm of 
   1.235 -    (Const (p,_)$ _ $(Const (@{const_name HOL.plus}, _)$(Const (@{const_name HOL.times},_)$ c $ y ) $z ) ) =>  if 
   1.236 -    (is_arith_rel fm) andalso (x = y) then  (abs(dest_number c)) else 1 
   1.237 -  | ( Const ("Not", _) $p) => formlcm x p 
   1.238 -  | ( Const ("op &",_) $ p $ q) => lcm_num (formlcm x p) (formlcm x q) 
   1.239 -  | ( Const ("op |",_) $ p $ q )=> lcm_num (formlcm x p) (formlcm x q) 
   1.240 -  |  _ => 1; 
   1.241 - 
   1.242 -(* ------------------------------------------------------------------------- *) 
   1.243 -(* Adjust all coefficients of x in formula; fold in reduction to +/- 1.      *) 
   1.244 -(* ------------------------------------------------------------------------- *) 
   1.245 - 
   1.246 -fun adjustcoeff x l fm = 
   1.247 -     case fm of  
   1.248 -      (Const(p,_) $d $( Const (@{const_name HOL.plus}, _)$(Const (@{const_name HOL.times},_) $ 
   1.249 -      c $ y ) $z )) => if (is_arith_rel fm) andalso (x = y) then  
   1.250 -        let val m = l div (dest_number c) 
   1.251 -            val n = (if p = @{const_name Orderings.less} then abs(m) else m) 
   1.252 -            val xtm = HOLogic.mk_binop @{const_name HOL.times} ((mk_number (m div n)), x) 
   1.253 -	in
   1.254 -        (HOLogic.mk_binrel p ((linear_cmul n d),(HOLogic.mk_binop @{const_name HOL.plus} ( xtm ,( linear_cmul n z) )))) 
   1.255 -	end 
   1.256 -	else fm 
   1.257 -  |( Const ("Not", _) $ p) => HOLogic.Not $ (adjustcoeff x l p) 
   1.258 -  |( Const ("op &",_) $ p $ q) => HOLogic.conj$(adjustcoeff x l p) $(adjustcoeff x l q) 
   1.259 -  |( Const ("op |",_) $ p $ q) => HOLogic.disj $(adjustcoeff x l p)$ (adjustcoeff x l q) 
   1.260 -  |_ => fm; 
   1.261 - 
   1.262 -(* ------------------------------------------------------------------------- *) 
   1.263 -(* Hence make coefficient of x one in existential formula.                   *) 
   1.264 -(* ------------------------------------------------------------------------- *) 
   1.265 - 
   1.266 -fun unitycoeff x fm = 
   1.267 -  let val l = formlcm x fm
   1.268 -      val fm' = adjustcoeff x l fm in
   1.269 -      if l = 1 then fm' 
   1.270 -	 else 
   1.271 -     let val xp = (HOLogic.mk_binop @{const_name HOL.plus}  
   1.272 -     		((HOLogic.mk_binop @{const_name HOL.times} ((mk_number 1), x )), zero))
   1.273 -	in 
   1.274 -      HOLogic.conj $(HOLogic.mk_binrel "Divides.dvd" ((mk_number l) , xp )) $ (adjustcoeff x l fm) 
   1.275 -      end 
   1.276 -  end; 
   1.277 - 
   1.278 -(* adjustcoeffeq l fm adjusts the coeffitients c_i of x  overall in fm to l*)
   1.279 -(* Here l must be a multiple of all c_i otherwise the obtained formula is not equivalent*)
   1.280 -(*
   1.281 -fun adjustcoeffeq x l fm = 
   1.282 -    case fm of  
   1.283 -      (Const(p,_) $d $( Const (@{const_name HOL.plus}, _)$(Const (@{const_name HOL.times},_) $ 
   1.284 -      c $ y ) $z )) => if (is_arith_rel fm) andalso (x = y) then  
   1.285 -        let val m = l div (dest_number c) 
   1.286 -            val n = (if p = @{const_name Orderings.less} then abs(m) else m)  
   1.287 -            val xtm = (HOLogic.mk_binop @{const_name HOL.times} ((mk_number ((m div n)*l) ), x))
   1.288 -            in (HOLogic.mk_binrel p ((linear_cmul n d),(HOLogic.mk_binop @{const_name HOL.plus} ( xtm ,( linear_cmul n z) )))) 
   1.289 -	    end 
   1.290 -	else fm 
   1.291 -  |( Const ("Not", _) $ p) => HOLogic.Not $ (adjustcoeffeq x l p) 
   1.292 -  |( Const ("op &",_) $ p $ q) => HOLogic.conj$(adjustcoeffeq x l p) $(adjustcoeffeq x l q) 
   1.293 -  |( Const ("op |",_) $ p $ q) => HOLogic.disj $(adjustcoeffeq x l p)$ (adjustcoeffeq x l q) 
   1.294 -  |_ => fm;
   1.295 - 
   1.296 -
   1.297 -*)
   1.298 -
   1.299 -(* ------------------------------------------------------------------------- *) 
   1.300 -(* The "minus infinity" version.                                             *) 
   1.301 -(* ------------------------------------------------------------------------- *) 
   1.302 - 
   1.303 -fun minusinf x fm = case fm of  
   1.304 -    (Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ (c1 ) $(Const (@{const_name HOL.plus}, _) $(Const (@{const_name HOL.times},_) $ c2 $ y) $z)) => 
   1.305 -  	 if (is_arith_rel fm) andalso (x=y) andalso (c2 = one) andalso (c1 =zero) then HOLogic.false_const  
   1.306 -	 				 else fm 
   1.307 - 
   1.308 -  |(Const(@{const_name Orderings.less},_) $ c $(Const (@{const_name HOL.plus}, _) $(Const (@{const_name HOL.times},_) $ pm1 $ y ) $ z 
   1.309 -  )) => if (x = y) 
   1.310 -	then if (pm1 = one) andalso (c = zero) then HOLogic.false_const 
   1.311 -	     else if (dest_number pm1 = ~1) andalso (c = zero) then HOLogic.true_const 
   1.312 -	          else error "minusinf : term not in normal form!!!"
   1.313 -	else fm
   1.314 -	 
   1.315 -  |(Const ("Not", _) $ p) => HOLogic.Not $ (minusinf x p) 
   1.316 -  |(Const ("op &",_) $ p $ q) => HOLogic.conj $ (minusinf x p) $ (minusinf x q) 
   1.317 -  |(Const ("op |",_) $ p $ q) => HOLogic.disj $ (minusinf x p) $ (minusinf x q) 
   1.318 -  |_ => fm; 
   1.319 -
   1.320 -(* ------------------------------------------------------------------------- *)
   1.321 -(* The "Plus infinity" version.                                             *)
   1.322 -(* ------------------------------------------------------------------------- *)
   1.323 -
   1.324 -fun plusinf x fm = case fm of
   1.325 -    (Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ (c1 ) $(Const (@{const_name HOL.plus}, _) $(Const (@{const_name HOL.times},_) $ c2 $ y) $z)) =>
   1.326 -  	 if (is_arith_rel fm) andalso (x=y) andalso (c2 = one) andalso (c1 =zero) then HOLogic.false_const
   1.327 -	 				 else fm
   1.328 -
   1.329 -  |(Const(@{const_name Orderings.less},_) $ c $(Const (@{const_name HOL.plus}, _) $(Const (@{const_name HOL.times},_) $ pm1 $ y ) $ z
   1.330 -  )) => if (x = y) 
   1.331 -	then if (pm1 = one) andalso (c = zero) then HOLogic.true_const 
   1.332 -	     else if (dest_number pm1 = ~1) andalso (c = zero) then HOLogic.false_const
   1.333 -	     else error "plusinf : term not in normal form!!!"
   1.334 -	else fm 
   1.335 -
   1.336 -  |(Const ("Not", _) $ p) => HOLogic.Not $ (plusinf x p)
   1.337 -  |(Const ("op &",_) $ p $ q) => HOLogic.conj $ (plusinf x p) $ (plusinf x q)
   1.338 -  |(Const ("op |",_) $ p $ q) => HOLogic.disj $ (plusinf x p) $ (plusinf x q)
   1.339 -  |_ => fm;
   1.340 - 
   1.341 -(* ------------------------------------------------------------------------- *) 
   1.342 -(* The LCM of all the divisors that involve x.                               *) 
   1.343 -(* ------------------------------------------------------------------------- *) 
   1.344 - 
   1.345 -fun divlcm x (Const("Divides.dvd",_)$ d $ (Const (@{const_name HOL.plus},_) $ (Const (@{const_name HOL.times},_) $ c $ y ) $ z ) ) =  
   1.346 -        if x = y then abs(dest_number d) else 1 
   1.347 -  |divlcm x ( Const ("Not", _) $ p) = divlcm x p 
   1.348 -  |divlcm x ( Const ("op &",_) $ p $ q) = lcm_num (divlcm x p) (divlcm x q) 
   1.349 -  |divlcm x ( Const ("op |",_) $ p $ q ) = lcm_num (divlcm x p) (divlcm x q) 
   1.350 -  |divlcm x  _ = 1; 
   1.351 - 
   1.352 -(* ------------------------------------------------------------------------- *) 
   1.353 -(* Construct the B-set.                                                      *) 
   1.354 -(* ------------------------------------------------------------------------- *) 
   1.355 - 
   1.356 -fun bset x fm = case fm of 
   1.357 -   (Const ("Not", _) $ p) => if (is_arith_rel p) then  
   1.358 -          (case p of  
   1.359 -	      (Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ c1 $ (Const (@{const_name HOL.plus}, _) $(Const (@{const_name HOL.times},_) $c2 $y) $a ) )  
   1.360 -	             => if (is_arith_rel p) andalso (x=	y) andalso (c2 = one) andalso (c1 = zero)  
   1.361 -	                then [linear_neg a] 
   1.362 -			else  bset x p 
   1.363 -   	  |_ =>[]) 
   1.364 -			 
   1.365 -			else bset x p 
   1.366 -  |(Const ("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ c1 $ (Const (@{const_name HOL.plus},_) $ (Const (@{const_name HOL.times},_) $c2 $ x) $ a)) =>  if (c1 =zero) andalso (c2 = one) then [linear_neg(linear_add [] a (mk_number 1))]  else [] 
   1.367 -  |(Const (@{const_name Orderings.less},_) $ c1$ (Const (@{const_name HOL.plus},_) $(Const (@{const_name HOL.times},_)$ c2 $ x) $ a)) => if (c1 =zero) andalso (c2 = one) then [linear_neg a] else [] 
   1.368 -  |(Const ("op &",_) $ p $ q) => (bset x p) union (bset x q) 
   1.369 -  |(Const ("op |",_) $ p $ q) => (bset x p) union (bset x q) 
   1.370 -  |_ => []; 
   1.371 - 
   1.372 -(* ------------------------------------------------------------------------- *)
   1.373 -(* Construct the A-set.                                                      *)
   1.374 -(* ------------------------------------------------------------------------- *)
   1.375 -
   1.376 -fun aset x fm = case fm of
   1.377 -   (Const ("Not", _) $ p) => if (is_arith_rel p) then
   1.378 -          (case p of
   1.379 -	      (Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ c1 $ (Const (@{const_name HOL.plus}, _) $(Const (@{const_name HOL.times},_) $c2 $y) $a ) )
   1.380 -	             => if (x=	y) andalso (c2 = one) andalso (c1 = zero)
   1.381 -	                then [linear_neg a]
   1.382 -			else  []
   1.383 -   	  |_ =>[])
   1.384 -
   1.385 -			else aset x p
   1.386 -  |(Const ("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ c1 $ (Const (@{const_name HOL.plus},_) $ (Const (@{const_name HOL.times},_) $c2 $ x) $ a)) =>  if (c1 =zero) andalso (c2 = one) then [linear_sub [] (mk_number 1) a]  else []
   1.387 -  |(Const (@{const_name Orderings.less},_) $ c1$ (Const (@{const_name HOL.plus},_) $(Const (@{const_name HOL.times},_)$ c2 $ x) $ a)) => if (c1 =zero) andalso (c2 = (mk_number (~1))) then [a] else []
   1.388 -  |(Const ("op &",_) $ p $ q) => (aset x p) union (aset x q)
   1.389 -  |(Const ("op |",_) $ p $ q) => (aset x p) union (aset x q)
   1.390 -  |_ => [];
   1.391 -
   1.392 -
   1.393 -(* ------------------------------------------------------------------------- *) 
   1.394 -(* Replace top variable with another linear form, retaining canonicality.    *) 
   1.395 -(* ------------------------------------------------------------------------- *) 
   1.396 - 
   1.397 -fun linrep vars x t fm = case fm of  
   1.398 -   ((Const(p,_)$ d $ (Const(@{const_name HOL.plus},_)$(Const(@{const_name HOL.times},_)$ c $ y) $ z))) => 
   1.399 -      if (x = y) andalso (is_arith_rel fm)  
   1.400 -      then  
   1.401 -        let val ct = linear_cmul (dest_number c) t  
   1.402 -	in (HOLogic.mk_binrel p (d, linear_add vars ct z)) 
   1.403 -	end 
   1.404 -	else fm 
   1.405 -  |(Const ("Not", _) $ p) => HOLogic.Not $ (linrep vars x t p) 
   1.406 -  |(Const ("op &",_) $ p $ q) => HOLogic.conj $ (linrep vars x t p) $ (linrep vars x t q) 
   1.407 -  |(Const ("op |",_) $ p $ q) => HOLogic.disj $ (linrep vars x t p) $ (linrep vars x t q) 
   1.408 -  |_ => fm;
   1.409 - 
   1.410 -(* ------------------------------------------------------------------------- *) 
   1.411 -(* Evaluation of constant expressions.                                       *) 
   1.412 -(* ------------------------------------------------------------------------- *) 
   1.413 -
   1.414 -(* An other implementation of divides, that covers more cases*) 
   1.415 -
   1.416 -exception DVD_UNKNOWN
   1.417 -
   1.418 -fun dvd_op (d, t) = 
   1.419 - if not(is_number d) then raise DVD_UNKNOWN
   1.420 - else let 
   1.421 -   val dn = dest_number d
   1.422 -   fun coeffs_of x = case x of 
   1.423 -     Const(p,_) $ tl $ tr => 
   1.424 -       if p = @{const_name HOL.plus} then (coeffs_of tl) union (coeffs_of tr)
   1.425 -          else if p = @{const_name HOL.times} 
   1.426 -	        then if (is_number tr) 
   1.427 -		 then [(dest_number tr) * (dest_number tl)] 
   1.428 -		 else [dest_number tl]
   1.429 -	        else []
   1.430 -    |_ => if (is_number t) then [dest_number t]  else []
   1.431 -   val ts = coeffs_of t
   1.432 -   in case ts of
   1.433 -     [] => raise DVD_UNKNOWN
   1.434 -    |_  => fold_rev (fn k => fn r => r andalso (k mod dn = 0)) ts true
   1.435 -   end;
   1.436 -
   1.437 -
   1.438 -val operations = 
   1.439 -  [("op =",op=), (@{const_name Orderings.less},IntInf.<), ("op >",IntInf.>), (@{const_name Orderings.less_eq},IntInf.<=) , 
   1.440 -   ("op >=",IntInf.>=), 
   1.441 -   ("Divides.dvd",fn (x,y) =>((IntInf.mod(y, x)) = 0))]; 
   1.442 - 
   1.443 -fun applyoperation (SOME f) (a,b) = f (a, b) 
   1.444 -    |applyoperation _ (_, _) = false; 
   1.445 - 
   1.446 -(*Evaluation of constant atomic formulas*) 
   1.447 - (*FIXME : This is an optimation but still incorrect !! *)
   1.448 -(*
   1.449 -fun evalc_atom at = case at of  
   1.450 -  (Const (p,_) $ s $ t) =>
   1.451 -   (if p="Divides.dvd" then 
   1.452 -     ((if dvd_op(s,t) then HOLogic.true_const
   1.453 -     else HOLogic.false_const)
   1.454 -      handle _ => at)
   1.455 -    else
   1.456 -  case AList.lookup (op =) operations p of 
   1.457 -    SOME f => ((if (f ((dest_number s),(dest_number t))) then HOLogic.true_const else HOLogic.false_const)  
   1.458 -    handle _ => at) 
   1.459 -      | _ =>  at) 
   1.460 -      |Const("Not",_)$(Const (p,_) $ s $ t) =>(  
   1.461 -  case AList.lookup (op =) operations p of 
   1.462 -    SOME f => ((if (f ((dest_number s),(dest_number t))) then 
   1.463 -    HOLogic.false_const else HOLogic.true_const)  
   1.464 -    handle _ => at) 
   1.465 -      | _ =>  at) 
   1.466 -      | _ =>  at; 
   1.467 -
   1.468 -*)
   1.469 -
   1.470 -fun evalc_atom at = case at of  
   1.471 -  (Const (p,_) $ s $ t) =>
   1.472 -   ( case AList.lookup (op =) operations p of 
   1.473 -    SOME f => ((if (f ((dest_number s),(dest_number t))) then HOLogic.true_const 
   1.474 -                else HOLogic.false_const)  
   1.475 -    handle _ => at) 
   1.476 -      | _ =>  at) 
   1.477 -      |Const("Not",_)$(Const (p,_) $ s $ t) =>(  
   1.478 -  case AList.lookup (op =) operations p of 
   1.479 -    SOME f => ((if (f ((dest_number s),(dest_number t))) 
   1.480 -               then HOLogic.false_const else HOLogic.true_const)  
   1.481 -    handle _ => at) 
   1.482 -      | _ =>  at) 
   1.483 -      | _ =>  at; 
   1.484 -
   1.485 - (*Function onatoms apllys function f on the atomic formulas involved in a.*) 
   1.486 - 
   1.487 -fun onatoms f a = if (is_arith_rel a) then f a else case a of 
   1.488 - 
   1.489 -  	(Const ("Not",_) $ p) => if is_arith_rel p then HOLogic.Not $ (f p) 
   1.490 -				 
   1.491 -				else HOLogic.Not $ (onatoms f p) 
   1.492 -  	|(Const ("op &",_) $ p $ q) => HOLogic.conj $ (onatoms f p) $ (onatoms f q) 
   1.493 -  	|(Const ("op |",_) $ p $ q) => HOLogic.disj $ (onatoms f p) $ (onatoms f q) 
   1.494 -  	|(Const ("op -->",_) $ p $ q) => HOLogic.imp $ (onatoms f p) $ (onatoms f q) 
   1.495 -  	|((Const ("op =", Type ("fun",[Type ("bool", []),_]))) $ p $ q) => (Const ("op =", [HOLogic.boolT, HOLogic.boolT] ---> HOLogic.boolT)) $ (onatoms f p) $ (onatoms f q) 
   1.496 -  	|(Const("All",_) $ Abs(x,T,p)) => Const("All", [HOLogic.intT --> 
   1.497 -	HOLogic.boolT] ---> HOLogic.boolT)$ Abs (x ,T, (onatoms f p)) 
   1.498 -  	|(Const("Ex",_) $ Abs(x,T,p)) => Const("Ex", [HOLogic.intT --> HOLogic.boolT]---> HOLogic.boolT) $ Abs( x ,T, (onatoms f p)) 
   1.499 -  	|_ => a; 
   1.500 - 
   1.501 -val evalc = onatoms evalc_atom; 
   1.502 - 
   1.503 -(* ------------------------------------------------------------------------- *) 
   1.504 -(* Hence overall quantifier elimination.                                     *) 
   1.505 -(* ------------------------------------------------------------------------- *) 
   1.506 - 
   1.507 - 
   1.508 -(*list_disj[conj] makes a disj[conj] of a given list. used with conjucts or disjuncts 
   1.509 -it liearises iterated conj[disj]unctions. *) 
   1.510 - 
   1.511 -fun list_disj [] = HOLogic.false_const
   1.512 -  | list_disj ps = foldr1 (fn (p, q) => HOLogic.disj $ p $ q) ps;
   1.513 -
   1.514 -fun list_conj [] = HOLogic.true_const
   1.515 -  | list_conj ps = foldr1 (fn (p, q) => HOLogic.conj $ p $ q) ps;
   1.516 -
   1.517 -
   1.518 -(*Simplification of Formulas *) 
   1.519 - 
   1.520 -(*Function q_bnd_chk checks if a quantified Formula makes sens : Means if in 
   1.521 -the body of the existential quantifier there are bound variables to the 
   1.522 -existential quantifier.*) 
   1.523 - 
   1.524 -fun has_bound fm =let fun has_boundh fm i = case fm of 
   1.525 -		 Bound n => (i = n) 
   1.526 -		 |Abs (_,_,p) => has_boundh p (i+1) 
   1.527 -		 |t1 $ t2 => (has_boundh t1 i) orelse (has_boundh t2 i) 
   1.528 -		 |_ =>false
   1.529 -
   1.530 -in  case fm of 
   1.531 -	Bound _ => true 
   1.532 -       |Abs (_,_,p) => has_boundh p 0 
   1.533 -       |t1 $ t2 => (has_bound t1 ) orelse (has_bound t2 ) 
   1.534 -       |_ =>false
   1.535 -end;
   1.536 - 
   1.537 -(*has_sub_abs checks if in a given Formula there are subformulas which are quantifed 
   1.538 -too. Is no used no more.*) 
   1.539 - 
   1.540 -fun has_sub_abs fm = case fm of  
   1.541 -		 Abs (_,_,_) => true 
   1.542 -		 |t1 $ t2 => (has_bound t1 ) orelse (has_bound t2 ) 
   1.543 -		 |_ =>false ; 
   1.544 -		  
   1.545 -(*update_bounds called with i=0 udates the numeration of bounded variables because the 
   1.546 -formula will not be quantified any more.*) 
   1.547 - 
   1.548 -fun update_bounds fm i = case fm of 
   1.549 -		 Bound n => if n >= i then Bound (n-1) else fm 
   1.550 -		 |Abs (x,T,p) => Abs(x,T,(update_bounds p (i+1))) 
   1.551 -		 |t1 $ t2 => (update_bounds t1 i) $ (update_bounds t2 i) 
   1.552 -		 |_ => fm ; 
   1.553 - 
   1.554 -(*psimpl : Simplification of propositions (general purpose)*) 
   1.555 -fun psimpl1 fm = case fm of 
   1.556 -    Const("Not",_) $ Const ("False",_) => HOLogic.true_const 
   1.557 -  | Const("Not",_) $ Const ("True",_) => HOLogic.false_const 
   1.558 -  | Const("op &",_) $ Const ("False",_) $ q => HOLogic.false_const 
   1.559 -  | Const("op &",_) $ p $ Const ("False",_)  => HOLogic.false_const 
   1.560 -  | Const("op &",_) $ Const ("True",_) $ q => q 
   1.561 -  | Const("op &",_) $ p $ Const ("True",_) => p 
   1.562 -  | Const("op |",_) $ Const ("False",_) $ q => q 
   1.563 -  | Const("op |",_) $ p $ Const ("False",_)  => p 
   1.564 -  | Const("op |",_) $ Const ("True",_) $ q => HOLogic.true_const 
   1.565 -  | Const("op |",_) $ p $ Const ("True",_)  => HOLogic.true_const 
   1.566 -  | Const("op -->",_) $ Const ("False",_) $ q => HOLogic.true_const 
   1.567 -  | Const("op -->",_) $ Const ("True",_) $  q => q 
   1.568 -  | Const("op -->",_) $ p $ Const ("True",_)  => HOLogic.true_const 
   1.569 -  | Const("op -->",_) $ p $ Const ("False",_)  => HOLogic.Not $  p 
   1.570 -  | Const("op =", Type ("fun",[Type ("bool", []),_])) $ Const ("True",_) $ q => q 
   1.571 -  | Const("op =", Type ("fun",[Type ("bool", []),_])) $ p $ Const ("True",_) => p 
   1.572 -  | Const("op =", Type ("fun",[Type ("bool", []),_])) $ Const ("False",_) $ q => HOLogic.Not $  q 
   1.573 -  | Const("op =", Type ("fun",[Type ("bool", []),_])) $ p $ Const ("False",_)  => HOLogic.Not $  p 
   1.574 -  | _ => fm; 
   1.575 - 
   1.576 -fun psimpl fm = case fm of 
   1.577 -   Const ("Not",_) $ p => psimpl1 (HOLogic.Not $ (psimpl p)) 
   1.578 -  | Const("op &",_) $ p $ q => psimpl1 (HOLogic.mk_conj (psimpl p,psimpl q)) 
   1.579 -  | Const("op |",_) $ p $ q => psimpl1 (HOLogic.mk_disj (psimpl p,psimpl q)) 
   1.580 -  | Const("op -->",_) $ p $ q => psimpl1 (HOLogic.mk_imp(psimpl p,psimpl q)) 
   1.581 -  | Const("op =", Type ("fun",[Type ("bool", []),_])) $ p $ q => psimpl1 (HOLogic.mk_eq(psimpl p,psimpl q))
   1.582 -  | _ => fm; 
   1.583 - 
   1.584 - 
   1.585 -(*simpl : Simplification of Terms involving quantifiers too. 
   1.586 - This function is able to drop out some quantified expressions where there are no 
   1.587 - bound varaibles.*) 
   1.588 -  
   1.589 -fun simpl1 fm  = 
   1.590 -  case fm of 
   1.591 -    Const("All",_) $Abs(x,_,p) => if (has_bound fm ) then fm  
   1.592 -    				else (update_bounds p 0) 
   1.593 -  | Const("Ex",_) $ Abs (x,_,p) => if has_bound fm then fm  
   1.594 -    				else (update_bounds p 0) 
   1.595 -  | _ => psimpl fm; 
   1.596 - 
   1.597 -fun simpl fm = case fm of 
   1.598 -    Const ("Not",_) $ p => simpl1 (HOLogic.Not $(simpl p))  
   1.599 -  | Const ("op &",_) $ p $ q => simpl1 (HOLogic.mk_conj (simpl p ,simpl q))  
   1.600 -  | Const ("op |",_) $ p $ q => simpl1 (HOLogic.mk_disj (simpl p ,simpl q ))  
   1.601 -  | Const ("op -->",_) $ p $ q => simpl1 (HOLogic.mk_imp(simpl p ,simpl q ))  
   1.602 -  | Const("op =", Type ("fun",[Type ("bool", []),_]))$ p $ q => simpl1 
   1.603 -  (HOLogic.mk_eq(simpl p ,simpl q ))  
   1.604 -(*  | Const ("All",Ta) $ Abs(Vn,VT,p) => simpl1(Const("All",Ta) $ 
   1.605 -  Abs(Vn,VT,simpl p ))  
   1.606 -  | Const ("Ex",Ta)  $ Abs(Vn,VT,p) => simpl1(Const("Ex",Ta)  $ 
   1.607 -  Abs(Vn,VT,simpl p ))  
   1.608 -*)
   1.609 -  | _ => fm; 
   1.610 - 
   1.611 -(* ------------------------------------------------------------------------- *) 
   1.612 - 
   1.613 -(* Puts fm into NNF*) 
   1.614 - 
   1.615 -fun  nnf fm = if (is_arith_rel fm) then fm  
   1.616 -else (case fm of 
   1.617 -  ( Const ("op &",_) $ p $ q)  => HOLogic.conj $ (nnf p) $(nnf q) 
   1.618 -  | (Const("op |",_) $ p $q) => HOLogic.disj $ (nnf p)$(nnf q) 
   1.619 -  | (Const ("op -->",_)  $ p $ q) => HOLogic.disj $ (nnf (HOLogic.Not $ p)) $ (nnf q) 
   1.620 -  | ((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)))) 
   1.621 -  | (Const ("Not",_)) $ ((Const ("Not",_)) $ p) => (nnf p) 
   1.622 -  | (Const ("Not",_)) $ (( Const ("op &",_)) $ p $ q) =>HOLogic.disj $ (nnf(HOLogic.Not $ p)) $ (nnf(HOLogic.Not $q)) 
   1.623 -  | (Const ("Not",_)) $ (( Const ("op |",_)) $ p $ q) =>HOLogic.conj $ (nnf(HOLogic.Not $ p)) $ (nnf(HOLogic.Not $ q)) 
   1.624 -  | (Const ("Not",_)) $ (( Const ("op -->",_)) $ p $ q ) =>HOLogic.conj $ (nnf p) $(nnf(HOLogic.Not $ q)) 
   1.625 -  | (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))) 
   1.626 -  | _ => fm); 
   1.627 - 
   1.628 - 
   1.629 -(* Function remred to remove redundancy in a list while keeping the order of appearance of the 
   1.630 -elements. but VERY INEFFICIENT!! *) 
   1.631 - 
   1.632 -fun remred1 el [] = [] 
   1.633 -    |remred1 el (h::t) = if el=h then (remred1 el t) else h::(remred1 el t); 
   1.634 -     
   1.635 -fun remred [] = [] 
   1.636 -    |remred (x::l) =  x::(remred1 x (remred l)); 
   1.637 - 
   1.638 -(*Makes sure that all free Variables are of the type integer but this function is only 
   1.639 -used temporarily, this job must be done by the parser later on.*) 
   1.640 - 
   1.641 -fun mk_uni_vars T  (node $ rest) = (case node of 
   1.642 -    Free (name,_) => Free (name,T) $ (mk_uni_vars T rest) 
   1.643 -    |_=> (mk_uni_vars T node) $ (mk_uni_vars T rest )  ) 
   1.644 -    |mk_uni_vars T (Free (v,_)) = Free (v,T) 
   1.645 -    |mk_uni_vars T tm = tm; 
   1.646 - 
   1.647 -fun mk_uni_int T (Const (@{const_name HOL.zero},T2)) = if T = T2 then (mk_number 0) else (Const (@{const_name HOL.zero},T2)) 
   1.648 -    |mk_uni_int T (Const (@{const_name HOL.one},T2)) = if T = T2 then (mk_number 1) else (Const (@{const_name HOL.one},T2)) 
   1.649 -    |mk_uni_int T (node $ rest) = (mk_uni_int T node) $ (mk_uni_int T rest )  
   1.650 -    |mk_uni_int T (Abs(AV,AT,p)) = Abs(AV,AT,mk_uni_int T p) 
   1.651 -    |mk_uni_int T tm = tm; 
   1.652 - 
   1.653 -
   1.654 -(* Minusinfinity Version*)    
   1.655 -fun myupto (m:IntInf.int) n = if m > n then [] else m::(myupto (m+1) n)
   1.656 -
   1.657 -fun coopermi vars1 fm = 
   1.658 -  case fm of 
   1.659 -   Const ("Ex",_) $ Abs(x0,T,p0) => 
   1.660 -   let 
   1.661 -    val (xn,p1) = Syntax.variant_abs (x0,T,p0) 
   1.662 -    val x = Free (xn,T)  
   1.663 -    val vars = (xn::vars1) 
   1.664 -    val p = unitycoeff x  (posineq (simpl p1))
   1.665 -    val p_inf = simpl (minusinf x p) 
   1.666 -    val bset = bset x p 
   1.667 -    val js = myupto 1 (divlcm x p)
   1.668 -    fun p_element j b = linrep vars x (linear_add vars b (mk_number j)) p  
   1.669 -    fun stage j = list_disj (linrep vars x (mk_number j) p_inf :: map (p_element j) bset)  
   1.670 -   in (list_disj (map stage js))
   1.671 -    end 
   1.672 -  | _ => error "cooper: not an existential formula"; 
   1.673 - 
   1.674 -
   1.675 -
   1.676 -(* The plusinfinity version of cooper*)
   1.677 -fun cooperpi vars1 fm =
   1.678 -  case fm of
   1.679 -   Const ("Ex",_) $ Abs(x0,T,p0) => let 
   1.680 -    val (xn,p1) = Syntax.variant_abs (x0,T,p0)
   1.681 -    val x = Free (xn,T)
   1.682 -    val vars = (xn::vars1)
   1.683 -    val p = unitycoeff x  (posineq (simpl p1))
   1.684 -    val p_inf = simpl (plusinf x p)
   1.685 -    val aset = aset x p
   1.686 -    val js = myupto 1 (divlcm x p)
   1.687 -    fun p_element j a = linrep vars x (linear_sub vars a (mk_number j)) p
   1.688 -    fun stage j = list_disj (linrep vars x (mk_number j) p_inf :: map (p_element j) aset)
   1.689 -   in (list_disj (map stage js))
   1.690 -   end
   1.691 -  | _ => error "cooper: not an existential formula";
   1.692 -  
   1.693 -
   1.694 -(* Try to find a withness for the formula *)
   1.695 -
   1.696 -fun inf_w mi d vars x p = 
   1.697 -  let val f = if mi then minusinf else plusinf in
   1.698 -   case (simpl (minusinf x p)) of
   1.699 -   Const("True",_)  => (SOME (mk_number 1), HOLogic.true_const)
   1.700 -  |Const("False",_) => (NONE,HOLogic.false_const)
   1.701 -  |F => 
   1.702 -      let 
   1.703 -      fun h n =
   1.704 -       case ((simpl o evalc) (linrep vars x (mk_number n) F)) of 
   1.705 -	Const("True",_) => (SOME (mk_number n),HOLogic.true_const)
   1.706 -       |F' => if n=1 then (NONE,F')
   1.707 -	     else let val (rw,rf) = h (n-1) in 
   1.708 -	       (rw,HOLogic.mk_disj(F',rf))
   1.709 -	     end
   1.710 -
   1.711 -      in (h d)
   1.712 -      end
   1.713 -  end;
   1.714 -
   1.715 -fun set_w d b st vars x p = let 
   1.716 -    fun h ns = case ns of 
   1.717 -    [] => (NONE,HOLogic.false_const)
   1.718 -   |n::nl => ( case ((simpl o evalc) (linrep vars x n p)) of
   1.719 -      Const("True",_) => (SOME n,HOLogic.true_const)
   1.720 -      |F' => let val (rw,rf) = h nl 
   1.721 -             in (rw,HOLogic.mk_disj(F',rf)) 
   1.722 -	     end)
   1.723 -    val f = if b then linear_add else linear_sub
   1.724 -    val p_elements = fold_rev (fn i => fn l => l union (map (fn e => f [] e (mk_number i)) st)) (myupto 1 d) []
   1.725 -    in h p_elements
   1.726 -    end;
   1.727 -
   1.728 -fun withness d b st vars x p = case (inf_w b d vars x p) of 
   1.729 -   (SOME n,_) => (SOME n,HOLogic.true_const)
   1.730 -  |(NONE,Pinf) => (case (set_w d b st vars x p) of 
   1.731 -    (SOME n,_) => (SOME n,HOLogic.true_const)
   1.732 -    |(_,Pst) => (NONE,HOLogic.mk_disj(Pinf,Pst)));
   1.733 -
   1.734 -
   1.735 -
   1.736 -
   1.737 -(*Cooper main procedure*) 
   1.738 -
   1.739 -exception STAGE_TRUE;
   1.740 -
   1.741 -  
   1.742 -fun cooper vars1 fm =
   1.743 -  case fm of
   1.744 -   Const ("Ex",_) $ Abs(x0,T,p0) => let 
   1.745 -    val (xn,p1) = Syntax.variant_abs (x0,T,p0)
   1.746 -    val x = Free (xn,T)
   1.747 -    val vars = (xn::vars1)
   1.748 -(*     val p = unitycoeff x  (posineq (simpl p1)) *)
   1.749 -    val p = unitycoeff x  p1 
   1.750 -    val ast = aset x p
   1.751 -    val bst = bset x p
   1.752 -    val js = myupto 1 (divlcm x p)
   1.753 -    val (p_inf,f,S ) = 
   1.754 -    if (length bst) <= (length ast) 
   1.755 -     then (simpl (minusinf x p),linear_add,bst)
   1.756 -     else (simpl (plusinf x p), linear_sub,ast)
   1.757 -    fun p_element j a = linrep vars x (f vars a (mk_number j)) p
   1.758 -    fun stage j = list_disj (linrep vars x (mk_number j) p_inf :: map (p_element j) S)
   1.759 -    fun stageh n = ((if n = 0 then []
   1.760 -	else 
   1.761 -	let 
   1.762 -	val nth_stage = simpl (evalc (stage n))
   1.763 -	in 
   1.764 -	if (nth_stage = HOLogic.true_const) 
   1.765 -	  then raise STAGE_TRUE 
   1.766 -	  else if (nth_stage = HOLogic.false_const) then stageh (n-1)
   1.767 -	    else nth_stage::(stageh (n-1))
   1.768 -	end )
   1.769 -        handle STAGE_TRUE => [HOLogic.true_const])
   1.770 -    val slist = stageh (divlcm x p)
   1.771 -   in (list_disj slist)
   1.772 -   end
   1.773 -  | _ => error "cooper: not an existential formula";
   1.774 -
   1.775 -
   1.776 -(* A Version of cooper that returns a withness *)
   1.777 -fun cooper_w vars1 fm =
   1.778 -  case fm of
   1.779 -   Const ("Ex",_) $ Abs(x0,T,p0) => let 
   1.780 -    val (xn,p1) = Syntax.variant_abs (x0,T,p0)
   1.781 -    val x = Free (xn,T)
   1.782 -    val vars = (xn::vars1)
   1.783 -(*     val p = unitycoeff x  (posineq (simpl p1)) *)
   1.784 -    val p = unitycoeff x  p1 
   1.785 -    val ast = aset x p
   1.786 -    val bst = bset x p
   1.787 -    val d = divlcm x p
   1.788 -    val (p_inf,S ) = 
   1.789 -    if (length bst) <= (length ast) 
   1.790 -     then (true,bst)
   1.791 -     else (false,ast)
   1.792 -    in withness d p_inf S vars x p 
   1.793 -(*    fun p_element j a = linrep vars x (f vars a (mk_number j)) p
   1.794 -    fun stage j = list_disj (linrep vars x (mk_number j) p_inf :: map (p_element j) S)
   1.795 -   in (list_disj (map stage js))
   1.796 -*)
   1.797 -   end
   1.798 -  | _ => error "cooper: not an existential formula";
   1.799 -
   1.800 - 
   1.801 -(* ------------------------------------------------------------------------- *) 
   1.802 -(* Free variables in terms and formulas.	                             *) 
   1.803 -(* ------------------------------------------------------------------------- *) 
   1.804 - 
   1.805 -fun fvt tml = case tml of 
   1.806 -    [] => [] 
   1.807 -  | Free(x,_)::r => x::(fvt r) 
   1.808 - 
   1.809 -fun fv fm = fvt (term_frees fm); 
   1.810 - 
   1.811 - 
   1.812 -(* ========================================================================= *) 
   1.813 -(* Quantifier elimination.                                                   *) 
   1.814 -(* ========================================================================= *) 
   1.815 -(*conj[/disj]uncts lists iterated conj[disj]unctions*) 
   1.816 - 
   1.817 -fun disjuncts fm = case fm of 
   1.818 -    Const ("op |",_) $ p $ q => (disjuncts p) @ (disjuncts q) 
   1.819 -  | _ => [fm]; 
   1.820 - 
   1.821 -fun conjuncts fm = case fm of 
   1.822 -    Const ("op &",_) $p $ q => (conjuncts p) @ (conjuncts q) 
   1.823 -  | _ => [fm]; 
   1.824 - 
   1.825 - 
   1.826 - 
   1.827 -(* ------------------------------------------------------------------------- *) 
   1.828 -(* Lift procedure given literal modifier, formula normalizer & basic quelim. *) 
   1.829 -(* ------------------------------------------------------------------------- *)
   1.830 -
   1.831 -fun lift_qelim afn nfn qfn isat = 
   1.832 -let 
   1.833 -fun qelift vars fm = if (isat fm) then afn vars fm 
   1.834 -else  
   1.835 -case fm of 
   1.836 -  Const ("Not",_) $ p => HOLogic.Not $ (qelift vars p) 
   1.837 -  | Const ("op &",_) $ p $q => HOLogic.conj $ (qelift vars p) $ (qelift vars q) 
   1.838 -  | Const ("op |",_) $ p $ q => HOLogic.disj $ (qelift vars p) $ (qelift vars q) 
   1.839 -  | Const ("op -->",_) $ p $ q => HOLogic.imp $ (qelift vars p) $ (qelift vars q) 
   1.840 -  | Const ("op =",Type ("fun",[Type ("bool", []),_])) $ p $ q => HOLogic.mk_eq ((qelift vars p),(qelift vars q)) 
   1.841 -  | Const ("All",QT) $ Abs(x,T,p) => HOLogic.Not $(qelift vars (Const ("Ex",QT) $ Abs(x,T,(HOLogic.Not $ p)))) 
   1.842 -  | (e as Const ("Ex",_)) $ Abs (x,T,p)  =>  qfn vars (e$Abs (x,T,(nfn(qelift (x::vars) p))))
   1.843 -  | _ => fm 
   1.844 - 
   1.845 -in (fn fm => qelift (fv fm) fm)
   1.846 -end; 
   1.847 -
   1.848 - 
   1.849 -(*   
   1.850 -fun lift_qelim afn nfn qfn isat = 
   1.851 - let   fun qelim x vars p = 
   1.852 -  let val cjs = conjuncts p 
   1.853 -      val (ycjs,ncjs) = List.partition (has_bound) cjs in 
   1.854 -      (if ycjs = [] then p else 
   1.855 -                          let val q = (qfn vars ((HOLogic.exists_const HOLogic.intT 
   1.856 -			  ) $ Abs(x,HOLogic.intT,(list_conj ycjs)))) in 
   1.857 -                          (fold_rev conj_help ncjs q)  
   1.858 -			  end) 
   1.859 -       end 
   1.860 -    
   1.861 -  fun qelift vars fm = if (isat fm) then afn vars fm 
   1.862 -    else  
   1.863 -    case fm of 
   1.864 -      Const ("Not",_) $ p => HOLogic.Not $ (qelift vars p) 
   1.865 -    | Const ("op &",_) $ p $q => HOLogic.conj $ (qelift vars p) $ (qelift vars q) 
   1.866 -    | Const ("op |",_) $ p $ q => HOLogic.disj $ (qelift vars p) $ (qelift vars q) 
   1.867 -    | Const ("op -->",_) $ p $ q => HOLogic.imp $ (qelift vars p) $ (qelift vars q) 
   1.868 -    | Const ("op =",Type ("fun",[Type ("bool", []),_])) $ p $ q => HOLogic.mk_eq ((qelift vars p),(qelift vars q)) 
   1.869 -    | Const ("All",QT) $ Abs(x,T,p) => HOLogic.Not $(qelift vars (Const ("Ex",QT) $ Abs(x,T,(HOLogic.Not $ p)))) 
   1.870 -    | Const ("Ex",_) $ Abs (x,T,p)  => let  val djs = disjuncts(nfn(qelift (x::vars) p)) in 
   1.871 -    			list_disj(map (qelim x vars) djs) end 
   1.872 -    | _ => fm 
   1.873 - 
   1.874 -  in (fn fm => simpl(qelift (fv fm) fm)) 
   1.875 -  end; 
   1.876 -*)
   1.877 - 
   1.878 -(* ------------------------------------------------------------------------- *) 
   1.879 -(* Cleverer (proposisional) NNF with conditional and literal modification.   *) 
   1.880 -(* ------------------------------------------------------------------------- *) 
   1.881 - 
   1.882 -(*Function Negate used by cnnf, negates a formula p*) 
   1.883 - 
   1.884 -fun negate (Const ("Not",_) $ p) = p 
   1.885 -    |negate p = (HOLogic.Not $ p); 
   1.886 - 
   1.887 -fun cnnf lfn = 
   1.888 -  let fun cnnfh fm = case  fm of 
   1.889 -      (Const ("op &",_) $ p $ q) => HOLogic.mk_conj(cnnfh p,cnnfh q) 
   1.890 -    | (Const ("op |",_) $ p $ q) => HOLogic.mk_disj(cnnfh p,cnnfh q) 
   1.891 -    | (Const ("op -->",_) $ p $q) => HOLogic.mk_disj(cnnfh(HOLogic.Not $ p),cnnfh q) 
   1.892 -    | (Const ("op =",Type ("fun",[Type ("bool", []),_])) $ p $ q) => HOLogic.mk_disj( 
   1.893 -    		HOLogic.mk_conj(cnnfh p,cnnfh q), 
   1.894 -		HOLogic.mk_conj(cnnfh(HOLogic.Not $ p),cnnfh(HOLogic.Not $q))) 
   1.895 -
   1.896 -    | (Const ("Not",_) $ (Const("Not",_) $ p)) => cnnfh p 
   1.897 -    | (Const ("Not",_) $ (Const ("op &",_) $ p $ q)) => HOLogic.mk_disj(cnnfh(HOLogic.Not $ p),cnnfh(HOLogic.Not $ q)) 
   1.898 -    | (Const ("Not",_) $(Const ("op |",_) $ (Const ("op &",_) $ p $ q) $  
   1.899 -    			(Const ("op &",_) $ p1 $ r))) => if p1 = negate p then 
   1.900 -		         HOLogic.mk_disj(  
   1.901 -			   cnnfh (HOLogic.mk_conj(p,cnnfh(HOLogic.Not $ q))), 
   1.902 -			   cnnfh (HOLogic.mk_conj(p1,cnnfh(HOLogic.Not $ r)))) 
   1.903 -			 else  HOLogic.mk_conj(
   1.904 -			  cnnfh (HOLogic.mk_disj(cnnfh (HOLogic.Not $ p),cnnfh(HOLogic.Not $ q))), 
   1.905 -			   cnnfh (HOLogic.mk_disj(cnnfh (HOLogic.Not $ p1),cnnfh(HOLogic.Not $ r)))
   1.906 -			 ) 
   1.907 -    | (Const ("Not",_) $ (Const ("op |",_) $ p $ q)) => HOLogic.mk_conj(cnnfh(HOLogic.Not $ p),cnnfh(HOLogic.Not $ q)) 
   1.908 -    | (Const ("Not",_) $ (Const ("op -->",_) $ p $q)) => HOLogic.mk_conj(cnnfh p,cnnfh(HOLogic.Not $ q)) 
   1.909 -    | (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)) 
   1.910 -    | _ => lfn fm  
   1.911 -in cnnfh
   1.912 - end; 
   1.913 - 
   1.914 -(*End- function the quantifierelimination an decion procedure of presburger formulas.*)   
   1.915 -
   1.916 -(*
   1.917 -val integer_qelim = simpl o evalc o (lift_qelim linform (simpl o (cnnf posineq o evalc)) cooper is_arith_rel) ; 
   1.918 -*)
   1.919 -
   1.920 -
   1.921 -val integer_qelim = simpl o evalc o (lift_qelim linform (cnnf posineq o evalc) cooper is_arith_rel) ; 
   1.922 -
   1.923 -end;