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