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