src/HOL/Integ/cooper_proof.ML
author chaieb
Sat Jun 05 18:34:06 2004 +0200 (2004-06-05)
changeset 14877 28084696907f
parent 14758 af3b71a46a1c
child 14920 a7525235e20f
permissions -rw-r--r--
More readable code.
     1 (*  Title:      HOL/Integ/cooper_proof.ML
     2     ID:         $Id$
     3     Author:     Amine Chaieb and Tobias Nipkow, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 File containing the implementation of the proof
     7 generation for Cooper Algorithm
     8 *)
     9 
    10 signature COOPER_PROOF =
    11 sig
    12   val qe_Not : thm
    13   val qe_conjI : thm
    14   val qe_disjI : thm
    15   val qe_impI : thm
    16   val qe_eqI : thm
    17   val qe_exI : thm
    18   val list_to_set : typ -> term list -> term
    19   val qe_get_terms : thm -> term * term
    20   val cooper_prv : Sign.sg -> term -> term -> thm
    21   val proof_of_evalc : Sign.sg -> term -> thm
    22   val proof_of_cnnf : Sign.sg -> term -> (term -> thm) -> thm
    23   val proof_of_linform : Sign.sg -> string list -> term -> thm
    24   val proof_of_adjustcoeffeq : Sign.sg -> term -> int -> term -> thm
    25   val prove_elementar : Sign.sg -> string -> term -> thm
    26   val thm_of : Sign.sg -> (term -> (term list * (thm list -> thm))) -> term -> thm
    27 end;
    28 
    29 structure CooperProof : COOPER_PROOF =
    30 struct
    31 open CooperDec;
    32 
    33 (*
    34 val presburger_ss = simpset_of (theory "Presburger")
    35   addsimps [zdiff_def] delsimps [symmetric zdiff_def];
    36 *)
    37 
    38 val presburger_ss = simpset_of (theory "Presburger")
    39   addsimps[diff_int_def] delsimps [thm"diff_int_def_symmetric"];
    40 val cboolT = ctyp_of (sign_of HOL.thy) HOLogic.boolT;
    41 
    42 (*Theorems that will be used later for the proofgeneration*)
    43 
    44 val zdvd_iff_zmod_eq_0 = thm "zdvd_iff_zmod_eq_0";
    45 val unity_coeff_ex = thm "unity_coeff_ex";
    46 
    47 (* Thorems for proving the adjustment of the coeffitients*)
    48 
    49 val ac_lt_eq =  thm "ac_lt_eq";
    50 val ac_eq_eq = thm "ac_eq_eq";
    51 val ac_dvd_eq = thm "ac_dvd_eq";
    52 val ac_pi_eq = thm "ac_pi_eq";
    53 
    54 (* The logical compination of the sythetised properties*)
    55 val qe_Not = thm "qe_Not";
    56 val qe_conjI = thm "qe_conjI";
    57 val qe_disjI = thm "qe_disjI";
    58 val qe_impI = thm "qe_impI";
    59 val qe_eqI = thm "qe_eqI";
    60 val qe_exI = thm "qe_exI";
    61 val qe_ALLI = thm "qe_ALLI";
    62 
    63 (*Modulo D property for Pminusinf an Plusinf *)
    64 val fm_modd_minf = thm "fm_modd_minf";
    65 val not_dvd_modd_minf = thm "not_dvd_modd_minf";
    66 val dvd_modd_minf = thm "dvd_modd_minf";
    67 
    68 val fm_modd_pinf = thm "fm_modd_pinf";
    69 val not_dvd_modd_pinf = thm "not_dvd_modd_pinf";
    70 val dvd_modd_pinf = thm "dvd_modd_pinf";
    71 
    72 (* the minusinfinity proprty*)
    73 
    74 val fm_eq_minf = thm "fm_eq_minf";
    75 val neq_eq_minf = thm "neq_eq_minf";
    76 val eq_eq_minf = thm "eq_eq_minf";
    77 val le_eq_minf = thm "le_eq_minf";
    78 val len_eq_minf = thm "len_eq_minf";
    79 val not_dvd_eq_minf = thm "not_dvd_eq_minf";
    80 val dvd_eq_minf = thm "dvd_eq_minf";
    81 
    82 (* the Plusinfinity proprty*)
    83 
    84 val fm_eq_pinf = thm "fm_eq_pinf";
    85 val neq_eq_pinf = thm "neq_eq_pinf";
    86 val eq_eq_pinf = thm "eq_eq_pinf";
    87 val le_eq_pinf = thm "le_eq_pinf";
    88 val len_eq_pinf = thm "len_eq_pinf";
    89 val not_dvd_eq_pinf = thm "not_dvd_eq_pinf";
    90 val dvd_eq_pinf = thm "dvd_eq_pinf";
    91 
    92 (*Logical construction of the Property*)
    93 val eq_minf_conjI = thm "eq_minf_conjI";
    94 val eq_minf_disjI = thm "eq_minf_disjI";
    95 val modd_minf_disjI = thm "modd_minf_disjI";
    96 val modd_minf_conjI = thm "modd_minf_conjI";
    97 
    98 val eq_pinf_conjI = thm "eq_pinf_conjI";
    99 val eq_pinf_disjI = thm "eq_pinf_disjI";
   100 val modd_pinf_disjI = thm "modd_pinf_disjI";
   101 val modd_pinf_conjI = thm "modd_pinf_conjI";
   102 
   103 (*Cooper Backwards...*)
   104 (*Bset*)
   105 val not_bst_p_fm = thm "not_bst_p_fm";
   106 val not_bst_p_ne = thm "not_bst_p_ne";
   107 val not_bst_p_eq = thm "not_bst_p_eq";
   108 val not_bst_p_gt = thm "not_bst_p_gt";
   109 val not_bst_p_lt = thm "not_bst_p_lt";
   110 val not_bst_p_ndvd = thm "not_bst_p_ndvd";
   111 val not_bst_p_dvd = thm "not_bst_p_dvd";
   112 
   113 (*Aset*)
   114 val not_ast_p_fm = thm "not_ast_p_fm";
   115 val not_ast_p_ne = thm "not_ast_p_ne";
   116 val not_ast_p_eq = thm "not_ast_p_eq";
   117 val not_ast_p_gt = thm "not_ast_p_gt";
   118 val not_ast_p_lt = thm "not_ast_p_lt";
   119 val not_ast_p_ndvd = thm "not_ast_p_ndvd";
   120 val not_ast_p_dvd = thm "not_ast_p_dvd";
   121 
   122 (*Logical construction of the prop*)
   123 (*Bset*)
   124 val not_bst_p_conjI = thm "not_bst_p_conjI";
   125 val not_bst_p_disjI = thm "not_bst_p_disjI";
   126 val not_bst_p_Q_elim = thm "not_bst_p_Q_elim";
   127 
   128 (*Aset*)
   129 val not_ast_p_conjI = thm "not_ast_p_conjI";
   130 val not_ast_p_disjI = thm "not_ast_p_disjI";
   131 val not_ast_p_Q_elim = thm "not_ast_p_Q_elim";
   132 
   133 (*Cooper*)
   134 val cppi_eq = thm "cppi_eq";
   135 val cpmi_eq = thm "cpmi_eq";
   136 
   137 (*Others*)
   138 val simp_from_to = thm "simp_from_to";
   139 val P_eqtrue = thm "P_eqtrue";
   140 val P_eqfalse = thm "P_eqfalse";
   141 
   142 (*For Proving NNF*)
   143 
   144 val nnf_nn = thm "nnf_nn";
   145 val nnf_im = thm "nnf_im";
   146 val nnf_eq = thm "nnf_eq";
   147 val nnf_sdj = thm "nnf_sdj";
   148 val nnf_ncj = thm "nnf_ncj";
   149 val nnf_nim = thm "nnf_nim";
   150 val nnf_neq = thm "nnf_neq";
   151 val nnf_ndj = thm "nnf_ndj";
   152 
   153 (*For Proving term linearizition*)
   154 val linearize_dvd = thm "linearize_dvd";
   155 val lf_lt = thm "lf_lt";
   156 val lf_eq = thm "lf_eq";
   157 val lf_dvd = thm "lf_dvd";
   158 
   159 
   160 (* ------------------------------------------------------------------------- *)
   161 (*This function norm_zero_one  replaces the occurences of Numeral1 and Numeral0*)
   162 (*Respectively by their abstract representation Const("1",..) and COnst("0",..)*)
   163 (*this is necessary because the theorems use this representation.*)
   164 (* This function should be elminated in next versions...*)
   165 (* ------------------------------------------------------------------------- *)
   166 
   167 fun norm_zero_one fm = case fm of
   168   (Const ("op *",_) $ c $ t) => 
   169     if c = one then (norm_zero_one t)
   170     else if (dest_numeral c = ~1) 
   171          then (Const("uminus",HOLogic.intT --> HOLogic.intT) $ (norm_zero_one t))
   172          else (HOLogic.mk_binop "op *" (norm_zero_one c,norm_zero_one t))
   173   |(node $ rest) => ((norm_zero_one node)$(norm_zero_one rest))
   174   |(Abs(x,T,p)) => (Abs(x,T,(norm_zero_one p)))
   175   |_ => fm;
   176 
   177 (* ------------------------------------------------------------------------- *)
   178 (*function list to Set, constructs a set containing all elements of a given list.*)
   179 (* ------------------------------------------------------------------------- *)
   180 fun list_to_set T1 l = let val T = (HOLogic.mk_setT T1) in 
   181 	case l of 
   182 		[] => Const ("{}",T)
   183 		|(h::t) => Const("insert", T1 --> (T --> T)) $ h $(list_to_set T1 t)
   184 		end;
   185 		
   186 (* ------------------------------------------------------------------------- *)
   187 (* Returns both sides of an equvalence in the theorem*)
   188 (* ------------------------------------------------------------------------- *)
   189 fun qe_get_terms th = let val (_$(Const("op =",Type ("fun",[Type ("bool", []),_])) $ A $ B )) = prop_of th in (A,B) end;
   190 
   191 (* ------------------------------------------------------------------------- *)
   192 (* Modified version of the simple version with minimal amount of checking and postprocessing*)
   193 (* ------------------------------------------------------------------------- *)
   194 
   195 fun simple_prove_goal_cterm2 G tacs =
   196   let
   197     fun check None = error "prove_goal: tactic failed"
   198       | check (Some (thm, _)) = (case nprems_of thm of
   199             0 => thm
   200           | i => !result_error_fn thm (string_of_int i ^ " unsolved goals!"))
   201   in check (Seq.pull (EVERY tacs (trivial G))) end;
   202 
   203 (*-------------------------------------------------------------*)
   204 (*-------------------------------------------------------------*)
   205 
   206 fun cert_Trueprop sg t = cterm_of sg (HOLogic.mk_Trueprop t);
   207 
   208 (* ------------------------------------------------------------------------- *)
   209 (*This function proove elementar will be used to generate proofs at runtime*)
   210 (*It is is based on the isabelle function proove_goalw_cterm and is thought to *)
   211 (*prove properties such as a dvd b (essentially) that are only to make at
   212 runtime.*)
   213 (* ------------------------------------------------------------------------- *)
   214 fun prove_elementar sg s fm2 = case s of 
   215   (*"ss" like simplification with simpset*)
   216   "ss" =>
   217     let
   218       val ss = presburger_ss addsimps
   219         [zdvd_iff_zmod_eq_0,unity_coeff_ex]
   220       val ct =  cert_Trueprop sg fm2
   221     in 
   222       simple_prove_goal_cterm2 ct [simp_tac ss 1, TRY (simple_arith_tac 1)]
   223     end
   224 
   225   (*"bl" like blast tactic*)
   226   (* Is only used in the harrisons like proof procedure *)
   227   | "bl" =>
   228      let val ct = cert_Trueprop sg fm2
   229      in
   230        simple_prove_goal_cterm2 ct [blast_tac HOL_cs 1]
   231      end
   232 
   233   (*"ed" like Existence disjunctions ...*)
   234   (* Is only used in the harrisons like proof procedure *)
   235   | "ed" =>
   236     let
   237       val ex_disj_tacs =
   238         let
   239           val tac1 = EVERY[REPEAT(resolve_tac [disjI1,disjI2] 1), etac exI 1]
   240           val tac2 = EVERY[etac exE 1, rtac exI 1,
   241             REPEAT(resolve_tac [disjI1,disjI2] 1), assumption 1]
   242 	in [rtac iffI 1,
   243           etac exE 1, REPEAT(EVERY[etac disjE 1, tac1]), tac1,
   244           REPEAT(EVERY[etac disjE 1, tac2]), tac2]
   245         end
   246 
   247       val ct = cert_Trueprop sg fm2
   248     in 
   249       simple_prove_goal_cterm2 ct ex_disj_tacs
   250     end
   251 
   252   | "fa" =>
   253     let val ct = cert_Trueprop sg fm2
   254     in simple_prove_goal_cterm2 ct [simple_arith_tac 1]
   255     end
   256 
   257   | "sa" =>
   258     let
   259       val ss = presburger_ss addsimps zadd_ac
   260       val ct = cert_Trueprop sg fm2
   261     in 
   262       simple_prove_goal_cterm2 ct [simp_tac ss 1, TRY (simple_arith_tac 1)]
   263     end
   264   (* like Existance Conjunction *)
   265   | "ec" =>
   266     let
   267       val ss = presburger_ss addsimps zadd_ac
   268       val ct = cert_Trueprop sg fm2
   269     in 
   270       simple_prove_goal_cterm2 ct [simp_tac ss 1, TRY (blast_tac HOL_cs 1)]
   271     end
   272 
   273   | "ac" =>
   274     let
   275       val ss = HOL_basic_ss addsimps zadd_ac
   276       val ct = cert_Trueprop sg fm2
   277     in 
   278       simple_prove_goal_cterm2 ct [simp_tac ss 1]
   279     end
   280 
   281   | "lf" =>
   282     let
   283       val ss = presburger_ss addsimps zadd_ac
   284       val ct = cert_Trueprop sg fm2
   285     in 
   286       simple_prove_goal_cterm2 ct [simp_tac ss 1, TRY (simple_arith_tac 1)]
   287     end;
   288 
   289 (*=============================================================*)
   290 (*-------------------------------------------------------------*)
   291 (*              The new compact model                          *)
   292 (*-------------------------------------------------------------*)
   293 (*=============================================================*)
   294 
   295 fun thm_of sg decomp t = 
   296     let val (ts,recomb) = decomp t 
   297     in recomb (map (thm_of sg decomp) ts) 
   298     end;
   299 
   300 (*==================================================*)
   301 (*     Compact Version for adjustcoeffeq            *)
   302 (*==================================================*)
   303 
   304 fun decomp_adjustcoeffeq sg x l fm = case fm of
   305     (Const("Not",_)$(Const("op <",_) $(Const("0",_)) $(rt as (Const ("op +", _)$(Const ("op *",_) $    c $ y ) $z )))) => 
   306      let  
   307         val m = l div (dest_numeral c) 
   308         val n = if (x = y) then abs (m) else 1
   309         val xtm = (HOLogic.mk_binop "op *" ((mk_numeral ((m div n)*l) ), x)) 
   310         val rs = if (x = y) 
   311                  then (HOLogic.mk_binrel "op <" (zero,linear_sub [] (mk_numeral n) (HOLogic.mk_binop "op +" ( xtm ,( linear_cmul n z) )))) 
   312                  else HOLogic.mk_binrel "op <" (zero,linear_sub [] one rt )
   313         val ck = cterm_of sg (mk_numeral n)
   314         val cc = cterm_of sg c
   315         val ct = cterm_of sg z
   316         val cx = cterm_of sg y
   317         val pre = prove_elementar sg "lf" 
   318             (HOLogic.mk_binrel "op <" (Const("0",HOLogic.intT),(mk_numeral n)))
   319         val th1 = (pre RS (instantiate' [] [Some ck,Some cc, Some cx, Some ct] (ac_pi_eq)))
   320         in ([], fn [] => [th1,(prove_elementar sg "sa" (HOLogic.mk_eq (snd (qe_get_terms th1) ,rs)))] MRS trans)
   321         end
   322 
   323   |(Const(p,_) $a $( Const ("op +", _)$(Const ("op *",_) $ 
   324       c $ y ) $t )) => 
   325    if (is_arith_rel fm) andalso (x = y) 
   326    then  
   327         let val m = l div (dest_numeral c) 
   328            val k = (if p = "op <" then abs(m) else m)  
   329            val xtm = (HOLogic.mk_binop "op *" ((mk_numeral ((m div k)*l) ), x))
   330            val rs = (HOLogic.mk_binrel p ((linear_cmul k a),(HOLogic.mk_binop "op +" ( xtm ,( linear_cmul k t) )))) 
   331 
   332            val ck = cterm_of sg (mk_numeral k)
   333            val cc = cterm_of sg c
   334            val ct = cterm_of sg t
   335            val cx = cterm_of sg x
   336            val ca = cterm_of sg a
   337 
   338 	   in 
   339 	case p of
   340 	  "op <" => 
   341 	let val pre = prove_elementar sg "lf" 
   342 	    (HOLogic.mk_binrel "op <" (Const("0",HOLogic.intT),(mk_numeral k)))
   343             val th1 = (pre RS (instantiate' [] [Some ck,Some ca,Some cc, Some cx, Some ct] (ac_lt_eq)))
   344 	in ([], fn [] => [th1,(prove_elementar sg "lf" (HOLogic.mk_eq (snd (qe_get_terms th1) ,rs)))] MRS trans)
   345          end
   346 
   347            |"op =" =>
   348 	     let val pre = prove_elementar sg "lf" 
   349 	    (HOLogic.Not $ (HOLogic.mk_binrel "op =" (Const("0",HOLogic.intT),(mk_numeral k))))
   350 	         val th1 = (pre RS(instantiate' [] [Some ck,Some ca,Some cc, Some cx, Some ct] (ac_eq_eq)))
   351 	     in ([], fn [] => [th1,(prove_elementar sg "lf" (HOLogic.mk_eq (snd (qe_get_terms th1) ,rs)))] MRS trans)
   352              end
   353 
   354              |"Divides.op dvd" =>
   355 	       let val pre = prove_elementar sg "lf" 
   356 	   (HOLogic.Not $ (HOLogic.mk_binrel "op =" (Const("0",HOLogic.intT),(mk_numeral k))))
   357                    val th1 = (pre RS (instantiate' [] [Some ck,Some ca,Some cc, Some cx, Some ct]) (ac_dvd_eq))
   358                in ([], fn [] => [th1,(prove_elementar sg "lf" (HOLogic.mk_eq (snd (qe_get_terms th1) ,rs)))] MRS trans)
   359                         
   360                end
   361               end
   362   else ([], fn [] => instantiate' [Some cboolT] [Some (cterm_of sg fm)] refl)
   363 
   364  |( Const ("Not", _) $ p) => ([p], fn [th] => th RS qe_Not)
   365   |( Const ("op &",_) $ p $ q) => ([p,q], fn [th1,th2] => [th1,th2] MRS qe_conjI)
   366   |( Const ("op |",_) $ p $ q) =>([p,q], fn [th1,th2] => [th1,th2] MRS qe_disjI)
   367 
   368   |_ => ([], fn [] => instantiate' [Some cboolT] [Some (cterm_of sg fm)] refl);
   369 
   370 fun proof_of_adjustcoeffeq sg x l = thm_of sg (decomp_adjustcoeffeq sg x l);
   371 
   372 
   373 
   374 (*==================================================*)
   375 (*   Finding rho for modd_minusinfinity             *)
   376 (*==================================================*)
   377 fun rho_for_modd_minf x dlcm sg fm1 =
   378 let
   379     (*Some certified Terms*)
   380     
   381    val ctrue = cterm_of sg HOLogic.true_const
   382    val cfalse = cterm_of sg HOLogic.false_const
   383    val fm = norm_zero_one fm1
   384   in  case fm1 of 
   385       (Const ("Not", _) $ (Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ c1 $ (Const ("op +", _) $(Const ("op *",_) $ c2 $ y) $z))) => 
   386          if (x=y) andalso (c1= zero) andalso (c2= one) then (instantiate' [Some cboolT] [Some ctrue] (fm_modd_minf))
   387            else (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_modd_minf))
   388 
   389       |(Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ c1 $(Const ("op +", _) $(Const ("op *",_) $ c2 $ y) $z)) =>
   390   	   if (is_arith_rel fm) andalso (x=y) andalso (c1= zero) andalso (c2= one) 
   391 	   then (instantiate' [Some cboolT] [Some cfalse] (fm_modd_minf))
   392 	 	 else (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_modd_minf)) 
   393 
   394       |(Const("op <",_) $ c1 $(Const ("op +", _) $(Const ("op *",_) $ pm1 $ y ) $ z )) =>
   395            if (y=x) andalso (c1 = zero) then 
   396             if (pm1 = one) then (instantiate' [Some cboolT] [Some cfalse] (fm_modd_minf)) else
   397 	     (instantiate' [Some cboolT] [Some ctrue] (fm_modd_minf))
   398 	    else (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_modd_minf))
   399   
   400       |Const ("Not",_) $ (Const("Divides.op dvd",_)$ d $ (Const ("op +",_) $ (Const ("op *",_) $ c $ y ) $ z)) => 
   401          if y=x then  let val cz = cterm_of sg (norm_zero_one z)
   402 			  val fm2 = HOLogic.mk_binrel "op =" (HOLogic.mk_binop "Divides.op mod" (dlcm,d),norm_zero_one zero)
   403 	 	      in(instantiate' [] [Some cz ] ((((prove_elementar sg "ss" fm2)) RS(((zdvd_iff_zmod_eq_0)RS sym) RS iffD1) ) RS (not_dvd_modd_minf)))
   404 		      end
   405 		else (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_modd_minf))
   406       |(Const("Divides.op dvd",_)$ d $ (db as (Const ("op +",_) $ (Const ("op *",_) $
   407       c $ y ) $ z))) => 
   408          if y=x then  let val cz = cterm_of sg (norm_zero_one z)
   409 			  val fm2 = HOLogic.mk_binrel "op =" (HOLogic.mk_binop "Divides.op mod" (dlcm,d),norm_zero_one zero)
   410 	 	      in(instantiate' [] [Some cz ] ((((prove_elementar sg "ss" fm2)) RS (((zdvd_iff_zmod_eq_0)RS sym) RS iffD1) ) RS (dvd_modd_minf)))
   411 		      end
   412 		else (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_modd_minf))
   413 		
   414     
   415    |_ => instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_modd_minf)
   416    end;	 
   417 (*=========================================================================*)
   418 (*=========================================================================*)
   419 fun rho_for_eq_minf x dlcm  sg fm1 =  
   420    let
   421    val fm = norm_zero_one fm1
   422     in  case fm1 of 
   423       (Const ("Not", _) $ (Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ c1 $ (Const ("op +", _) $(Const ("op *",_) $ c2 $ y) $z))) => 
   424          if  (x=y) andalso (c1=zero) andalso (c2=one) 
   425 	   then (instantiate' [] [Some (cterm_of sg (norm_zero_one z))] (neq_eq_minf))
   426            else (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_eq_minf))
   427 
   428       |(Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ c1 $(Const ("op +", _) $(Const ("op *",_) $ c2 $ y) $z)) =>
   429   	   if (is_arith_rel fm) andalso (x=y) andalso ((c1=zero) orelse (c1 = norm_zero_one zero)) andalso ((c2=one) orelse (c1 = norm_zero_one one))
   430 	     then (instantiate' [] [Some (cterm_of sg (norm_zero_one z))] (eq_eq_minf))
   431 	     else (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_eq_minf)) 
   432 
   433       |(Const("op <",_) $ c1 $(Const ("op +", _) $(Const ("op *",_) $ pm1 $ y ) $ z )) =>
   434            if (y=x) andalso (c1 =zero) then 
   435             if pm1 = one then (instantiate' [] [Some (cterm_of sg (norm_zero_one z))] (le_eq_minf)) else
   436 	     (instantiate' [] [Some (cterm_of sg (norm_zero_one z))] (len_eq_minf))
   437 	    else (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_eq_minf))
   438       |Const ("Not",_) $ (Const("Divides.op dvd",_)$ d $ (Const ("op +",_) $ (Const ("op *",_) $ c $ y ) $ z)) => 
   439          if y=x then  let val cd = cterm_of sg (norm_zero_one d)
   440 	 		  val cz = cterm_of sg (norm_zero_one z)
   441 	 	      in(instantiate' [] [Some cd,  Some cz] (not_dvd_eq_minf)) 
   442 		      end
   443 
   444 		else (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_eq_minf))
   445 		
   446       |(Const("Divides.op dvd",_)$ d $ (Const ("op +",_) $ (Const ("op *",_) $ c $ y ) $ z)) => 
   447          if y=x then  let val cd = cterm_of sg (norm_zero_one d)
   448 	 		  val cz = cterm_of sg (norm_zero_one z)
   449 	 	      in(instantiate' [] [Some cd, Some cz ] (dvd_eq_minf))
   450 		      end
   451 		else (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_eq_minf))
   452 
   453       		
   454     |_ => (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_eq_minf))
   455  end;
   456 
   457 (*=====================================================*)
   458 (*=====================================================*)
   459 (*=========== minf proofs with the compact version==========*)
   460 fun decomp_minf_eq x dlcm sg t =  case t of
   461    Const ("op &",_) $ p $q => ([p,q],fn [th1,th2] => [th1,th2] MRS eq_minf_conjI)
   462    |Const ("op |",_) $ p $q => ([p,q],fn [th1,th2] => [th1,th2] MRS eq_minf_disjI)
   463    |_ => ([],fn [] => rho_for_eq_minf x dlcm sg t);
   464 
   465 fun decomp_minf_modd x dlcm sg t = case t of
   466    Const ("op &",_) $ p $q => ([p,q],fn [th1,th2] => [th1,th2] MRS modd_minf_conjI)
   467    |Const ("op |",_) $ p $q => ([p,q],fn [th1,th2] => [th1,th2] MRS modd_minf_disjI)
   468    |_ => ([],fn [] => rho_for_modd_minf x dlcm sg t);
   469 
   470 (* -------------------------------------------------------------*)
   471 (*                    Finding rho for pinf_modd                 *)
   472 (* -------------------------------------------------------------*)
   473 fun rho_for_modd_pinf x dlcm sg fm1 = 
   474 let
   475     (*Some certified Terms*)
   476     
   477   val ctrue = cterm_of sg HOLogic.true_const
   478   val cfalse = cterm_of sg HOLogic.false_const
   479   val fm = norm_zero_one fm1
   480  in  case fm1 of 
   481       (Const ("Not", _) $ (Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ c1 $ (Const ("op +", _) $(Const ("op *",_) $ c2 $ y) $z))) => 
   482          if ((x=y) andalso (c1= zero) andalso (c2= one))
   483 	 then (instantiate' [Some cboolT] [Some ctrue] (fm_modd_pinf))
   484          else (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_modd_pinf))
   485 
   486       |(Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ c1 $(Const ("op +", _) $(Const ("op *",_) $ c2 $ y) $z)) =>
   487   	if ((is_arith_rel fm) andalso (x = y) andalso (c1 = zero)  andalso (c2 = one)) 
   488 	then (instantiate' [Some cboolT] [Some cfalse] (fm_modd_pinf))
   489 	else (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_modd_pinf))
   490 
   491       |(Const("op <",_) $ c1 $(Const ("op +", _) $(Const ("op *",_) $ pm1 $ y ) $ z )) =>
   492         if ((y=x) andalso (c1 = zero)) then 
   493           if (pm1 = one) 
   494 	  then (instantiate' [Some cboolT] [Some ctrue] (fm_modd_pinf)) 
   495 	  else (instantiate' [Some cboolT] [Some cfalse] (fm_modd_pinf))
   496 	else (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_modd_pinf))
   497   
   498       |Const ("Not",_) $ (Const("Divides.op dvd",_)$ d $ (Const ("op +",_) $ (Const ("op *",_) $ c $ y ) $ z)) => 
   499          if y=x then  let val cz = cterm_of sg (norm_zero_one z)
   500 			  val fm2 = HOLogic.mk_binrel "op =" (HOLogic.mk_binop "Divides.op mod" (dlcm,d),norm_zero_one zero)
   501 	 	      in(instantiate' [] [Some cz ] ((((prove_elementar sg "ss" fm2)) RS(((zdvd_iff_zmod_eq_0)RS sym) RS iffD1) ) RS (not_dvd_modd_pinf)))
   502 		      end
   503 		else (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_modd_pinf))
   504       |(Const("Divides.op dvd",_)$ d $ (db as (Const ("op +",_) $ (Const ("op *",_) $
   505       c $ y ) $ z))) => 
   506          if y=x then  let val cz = cterm_of sg (norm_zero_one z)
   507 			  val fm2 = HOLogic.mk_binrel "op =" (HOLogic.mk_binop "Divides.op mod" (dlcm,d),norm_zero_one zero)
   508 	 	      in(instantiate' [] [Some cz ] ((((prove_elementar sg "ss" fm2)) RS (((zdvd_iff_zmod_eq_0)RS sym) RS iffD1) ) RS (dvd_modd_pinf)))
   509 		      end
   510 		else (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_modd_pinf))
   511 		
   512     
   513    |_ => instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_modd_pinf)
   514    end;	
   515 (* -------------------------------------------------------------*)
   516 (*                    Finding rho for pinf_eq                 *)
   517 (* -------------------------------------------------------------*)
   518 fun rho_for_eq_pinf x dlcm sg fm1 = 
   519   let
   520 					val fm = norm_zero_one fm1
   521     in  case fm1 of 
   522       (Const ("Not", _) $ (Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ c1 $ (Const ("op +", _) $(Const ("op *",_) $ c2 $ y) $z))) => 
   523          if  (x=y) andalso (c1=zero) andalso (c2=one) 
   524 	   then (instantiate' [] [Some (cterm_of sg (norm_zero_one z))] (neq_eq_pinf))
   525            else (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_eq_pinf))
   526 
   527       |(Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ c1 $(Const ("op +", _) $(Const ("op *",_) $ c2 $ y) $z)) =>
   528   	   if (is_arith_rel fm) andalso (x=y) andalso ((c1=zero) orelse (c1 = norm_zero_one zero)) andalso ((c2=one) orelse (c1 = norm_zero_one one))
   529 	     then (instantiate' [] [Some (cterm_of sg (norm_zero_one z))] (eq_eq_pinf))
   530 	     else (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_eq_pinf)) 
   531 
   532       |(Const("op <",_) $ c1 $(Const ("op +", _) $(Const ("op *",_) $ pm1 $ y ) $ z )) =>
   533            if (y=x) andalso (c1 =zero) then 
   534             if pm1 = one then (instantiate' [] [Some (cterm_of sg (norm_zero_one z))] (le_eq_pinf)) else
   535 	     (instantiate' [] [Some (cterm_of sg (norm_zero_one z))] (len_eq_pinf))
   536 	    else (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_eq_pinf))
   537       |Const ("Not",_) $ (Const("Divides.op dvd",_)$ d $ (Const ("op +",_) $ (Const ("op *",_) $ c $ y ) $ z)) => 
   538          if y=x then  let val cd = cterm_of sg (norm_zero_one d)
   539 	 		  val cz = cterm_of sg (norm_zero_one z)
   540 	 	      in(instantiate' [] [Some cd,  Some cz] (not_dvd_eq_pinf)) 
   541 		      end
   542 
   543 		else (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_eq_pinf))
   544 		
   545       |(Const("Divides.op dvd",_)$ d $ (Const ("op +",_) $ (Const ("op *",_) $ c $ y ) $ z)) => 
   546          if y=x then  let val cd = cterm_of sg (norm_zero_one d)
   547 	 		  val cz = cterm_of sg (norm_zero_one z)
   548 	 	      in(instantiate' [] [Some cd, Some cz ] (dvd_eq_pinf))
   549 		      end
   550 		else (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_eq_pinf))
   551 
   552       		
   553     |_ => (instantiate' [Some cboolT] [Some (cterm_of sg fm)] (fm_eq_pinf))
   554  end;
   555 
   556 
   557 
   558 fun  minf_proof_of_c sg x dlcm t =
   559   let val minf_eqth   = thm_of sg (decomp_minf_eq x dlcm sg) t
   560       val minf_moddth = thm_of sg (decomp_minf_modd x dlcm sg) t
   561   in (minf_eqth, minf_moddth)
   562 end;
   563 
   564 (*=========== pinf proofs with the compact version==========*)
   565 fun decomp_pinf_eq x dlcm sg t = case t of
   566    Const ("op &",_) $ p $q => ([p,q],fn [th1,th2] => [th1,th2] MRS eq_pinf_conjI)
   567    |Const ("op |",_) $ p $q => ([p,q],fn [th1,th2] => [th1,th2] MRS eq_pinf_disjI)
   568    |_ =>([],fn [] => rho_for_eq_pinf x dlcm sg t) ;
   569 
   570 fun decomp_pinf_modd x dlcm sg t =  case t of
   571    Const ("op &",_) $ p $q => ([p,q],fn [th1,th2] => [th1,th2] MRS modd_pinf_conjI)
   572    |Const ("op |",_) $ p $q => ([p,q],fn [th1,th2] => [th1,th2] MRS modd_pinf_disjI)
   573    |_ => ([],fn [] => rho_for_modd_pinf x dlcm sg t);
   574 
   575 fun  pinf_proof_of_c sg x dlcm t =
   576   let val pinf_eqth   = thm_of sg (decomp_pinf_eq x dlcm sg) t
   577       val pinf_moddth = thm_of sg (decomp_pinf_modd x dlcm sg) t
   578   in (pinf_eqth,pinf_moddth)
   579 end;
   580 
   581 
   582 (* ------------------------------------------------------------------------- *)
   583 (* Here we generate the theorem for the Bset Property in the simple direction*)
   584 (* It is just an instantiation*)
   585 (* ------------------------------------------------------------------------- *)
   586 (*
   587 fun bsetproof_of sg (x as Free(xn,xT)) fm bs dlcm   = 
   588   let
   589     val cp = cterm_of sg (absfree (xn,xT,(norm_zero_one fm)))
   590     val cdlcm = cterm_of sg dlcm
   591     val cB = cterm_of sg (list_to_set HOLogic.intT (map norm_zero_one bs))
   592   in instantiate' [] [Some cdlcm,Some cB, Some cp] (bst_thm)
   593 end;
   594 
   595 fun asetproof_of sg (x as Free(xn,xT)) fm ast dlcm = 
   596   let
   597     val cp = cterm_of sg (absfree (xn,xT,(norm_zero_one fm)))
   598     val cdlcm = cterm_of sg dlcm
   599     val cA = cterm_of sg (list_to_set HOLogic.intT (map norm_zero_one ast))
   600   in instantiate' [] [Some cdlcm,Some cA, Some cp] (ast_thm)
   601 end;
   602 *)
   603 
   604 (* For the generation of atomic Theorems*)
   605 (* Prove the premisses on runtime and then make RS*)
   606 (* ------------------------------------------------------------------------- *)
   607 
   608 (*========= this is rho ============*)
   609 fun generate_atomic_not_bst_p sg (x as Free(xn,xT)) fm dlcm B at = 
   610   let
   611     val cdlcm = cterm_of sg dlcm
   612     val cB = cterm_of sg B
   613     val cfma = cterm_of sg (absfree (xn,xT,(norm_zero_one fm)))
   614     val cat = cterm_of sg (norm_zero_one at)
   615   in
   616   case at of 
   617    (Const ("Not", _) $ (Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ c1 $ (Const ("op +", _) $(Const ("op *",_) $ c2 $ y) $z))) => 
   618       if  (x=y) andalso (c1=zero) andalso (c2=one) 
   619 	 then let val th1 = prove_elementar sg "ss" (Const ("op :",HOLogic.intT --> (HOLogic.mk_setT HOLogic.intT) --> HOLogic.boolT) $ (norm_zero_one (linear_cmul ~1 z)) $ B)
   620 	          val th2 =  prove_elementar sg "ss" (HOLogic.mk_eq ((norm_zero_one (linear_cmul ~1 z)),Const("uminus",HOLogic.intT --> HOLogic.intT) $(norm_zero_one  z)))
   621 		  val th3 = prove_elementar sg "ss" (HOLogic.mk_binrel "op <" (Const("0",HOLogic.intT),dlcm))
   622 	 in  (instantiate' [] [Some cfma]([th3,th1,th2] MRS (not_bst_p_ne)))
   623 	 end
   624          else (instantiate' [] [Some cfma,  Some cdlcm, Some cB,Some cat] (not_bst_p_fm))
   625 
   626    |(Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ c1 $(Const ("op +", T) $(Const ("op *",_) $ c2 $ y) $z)) =>
   627      if (is_arith_rel at) andalso (x=y)
   628 	then let val bst_z = norm_zero_one (linear_neg (linear_add [] z (mk_numeral 1)))
   629 	         in let val th1 = prove_elementar sg "ss" (Const ("op :",HOLogic.intT --> (HOLogic.mk_setT HOLogic.intT) --> HOLogic.boolT) $ bst_z $ B)
   630 	          val th2 =  prove_elementar sg "ss" (HOLogic.mk_eq (bst_z,Const("op -",T) $ (Const("uminus",HOLogic.intT --> HOLogic.intT) $(norm_zero_one z)) $ (Const("1",HOLogic.intT))))
   631 		  val th3 = prove_elementar sg "ss" (HOLogic.mk_binrel "op <" (Const("0",HOLogic.intT),dlcm))
   632 	 in  (instantiate' [] [Some cfma] ([th3,th1,th2] MRS (not_bst_p_eq)))
   633 	 end
   634        end
   635          else (instantiate' [] [Some cfma,  Some cdlcm, Some cB,Some cat] (not_bst_p_fm))
   636 
   637    |(Const("op <",_) $ c1 $(Const ("op +", _) $(Const ("op *",_) $ pm1 $ y ) $ z )) =>
   638         if (y=x) andalso (c1 =zero) then 
   639         if pm1 = one then 
   640 	  let val th1 = prove_elementar sg "ss" (Const ("op :",HOLogic.intT --> (HOLogic.mk_setT HOLogic.intT) --> HOLogic.boolT) $ (norm_zero_one (linear_cmul ~1 z)) $ B)
   641               val th2 =  prove_elementar sg "ss" (HOLogic.mk_eq ((norm_zero_one (linear_cmul ~1 z)),Const("uminus",HOLogic.intT --> HOLogic.intT) $(norm_zero_one z)))
   642 	  in  (instantiate' [] [Some cfma,  Some cdlcm]([th1,th2] MRS (not_bst_p_gt)))
   643 	    end
   644 	 else let val th1 = prove_elementar sg "ss" (HOLogic.mk_binrel "op <" (Const("0",HOLogic.intT),dlcm))
   645 	      in (instantiate' [] [Some cfma, Some cB,Some (cterm_of sg (norm_zero_one z))] (th1 RS (not_bst_p_lt)))
   646 	      end
   647       else (instantiate' [] [Some cfma,  Some cdlcm, Some cB,Some cat] (not_bst_p_fm))
   648 
   649    |Const ("Not",_) $ (Const("Divides.op dvd",_)$ d $ (Const ("op +",_) $ (Const ("op *",_) $ c $ y ) $ z)) => 
   650       if y=x then  
   651            let val cz = cterm_of sg (norm_zero_one z)
   652 	       val th1 = (prove_elementar sg "ss"  (HOLogic.mk_binrel "op =" (HOLogic.mk_binop "Divides.op mod" (dlcm,d),norm_zero_one zero))) RS (((zdvd_iff_zmod_eq_0)RS sym) RS iffD1)
   653  	     in (instantiate' []  [Some cfma, Some cB,Some cz] (th1 RS (not_bst_p_ndvd)))
   654 	     end
   655       else (instantiate' [] [Some cfma,  Some cdlcm, Some cB,Some cat] (not_bst_p_fm))
   656 
   657    |(Const("Divides.op dvd",_)$ d $ (Const ("op +",_) $ (Const ("op *",_) $ c $ y ) $ z)) => 
   658        if y=x then  
   659 	 let val cz = cterm_of sg (norm_zero_one z)
   660 	     val th1 = (prove_elementar sg "ss"  (HOLogic.mk_binrel "op =" (HOLogic.mk_binop "Divides.op mod" (dlcm,d),norm_zero_one zero))) RS (((zdvd_iff_zmod_eq_0)RS sym) RS iffD1)
   661  	    in (instantiate' []  [Some cfma,Some cB,Some cz] (th1 RS (not_bst_p_dvd)))
   662 	  end
   663       else (instantiate' [] [Some cfma,  Some cdlcm, Some cB,Some cat] (not_bst_p_fm))
   664       		
   665    |_ => (instantiate' [] [Some cfma,  Some cdlcm, Some cB,Some cat] (not_bst_p_fm))
   666       		
   667     end;
   668     
   669 
   670 (* ------------------------------------------------------------------------- *)    
   671 (* Main interpretation function for this backwards dirction*)
   672 (* if atomic do generate atomis formulae else Construct theorems and then make RS with the construction theorems*)
   673 (*Help Function*)
   674 (* ------------------------------------------------------------------------- *)
   675 
   676 (*==================== Proof with the compact version   *)
   677 
   678 fun decomp_nbstp sg x dlcm B fm t = case t of 
   679    Const("op &",_) $ ls $ rs => ([ls,rs],fn [th1,th2] => [th1,th2] MRS not_bst_p_conjI )
   680   |Const("op |",_) $ ls $ rs => ([ls,rs],fn [th1,th2] => [th1,th2] MRS not_bst_p_disjI)
   681   |_ => ([], fn [] => generate_atomic_not_bst_p sg x fm dlcm B t);
   682 
   683 fun not_bst_p_proof_of_c sg (x as Free(xn,xT)) fm dlcm B t =
   684   let 
   685        val th =  thm_of sg (decomp_nbstp sg x dlcm (list_to_set xT (map norm_zero_one B)) fm) t
   686       val fma = absfree (xn,xT, norm_zero_one fm)
   687   in let val th1 =  prove_elementar sg "ss"  (HOLogic.mk_eq (fma,fma))
   688      in [th,th1] MRS (not_bst_p_Q_elim)
   689      end
   690   end;
   691 
   692 
   693 (* ------------------------------------------------------------------------- *)    
   694 (* Protokol interpretation function for the backwards direction for cooper's Theorem*)
   695 
   696 (* For the generation of atomic Theorems*)
   697 (* Prove the premisses on runtime and then make RS*)
   698 (* ------------------------------------------------------------------------- *)
   699 (*========= this is rho ============*)
   700 fun generate_atomic_not_ast_p sg (x as Free(xn,xT)) fm dlcm A at = 
   701   let
   702     val cdlcm = cterm_of sg dlcm
   703     val cA = cterm_of sg A
   704     val cfma = cterm_of sg (absfree (xn,xT,(norm_zero_one fm)))
   705     val cat = cterm_of sg (norm_zero_one at)
   706   in
   707   case at of 
   708    (Const ("Not", _) $ (Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ c1 $ (Const ("op +", _) $(Const ("op *",_) $ c2 $ y) $z))) => 
   709       if  (x=y) andalso (c1=zero) andalso (c2=one) 
   710 	 then let val th1 = prove_elementar sg "ss" (Const ("op :",HOLogic.intT --> (HOLogic.mk_setT HOLogic.intT) --> HOLogic.boolT) $ (norm_zero_one (linear_cmul ~1 z)) $ A)
   711 	          val th2 =  prove_elementar sg "ss" (HOLogic.mk_eq ((norm_zero_one (linear_cmul ~1 z)),Const("uminus",HOLogic.intT --> HOLogic.intT) $(norm_zero_one  z)))
   712 		  val th3 = prove_elementar sg "ss" (HOLogic.mk_binrel "op <" (Const("0",HOLogic.intT),dlcm))
   713 	 in  (instantiate' [] [Some cfma]([th3,th1,th2] MRS (not_ast_p_ne)))
   714 	 end
   715          else (instantiate' [] [Some cfma,  Some cdlcm, Some cA,Some cat] (not_ast_p_fm))
   716 
   717    |(Const("op =",Type ("fun",[Type ("IntDef.int", []),_])) $ c1 $(Const ("op +", T) $(Const ("op *",_) $ c2 $ y) $z)) =>
   718      if (is_arith_rel at) andalso (x=y)
   719 	then let val ast_z = norm_zero_one (linear_sub [] one z )
   720 	         val th1 = prove_elementar sg "ss" (Const ("op :",HOLogic.intT --> (HOLogic.mk_setT HOLogic.intT) --> HOLogic.boolT) $ ast_z $ A)
   721 	         val th2 =  prove_elementar sg "ss" (HOLogic.mk_eq (ast_z,Const("op +",T) $ (Const("uminus",HOLogic.intT --> HOLogic.intT) $(norm_zero_one z)) $ (Const("1",HOLogic.intT))))
   722 		 val th3 = prove_elementar sg "ss" (HOLogic.mk_binrel "op <" (Const("0",HOLogic.intT),dlcm))
   723 	 in  (instantiate' [] [Some cfma] ([th3,th1,th2] MRS (not_ast_p_eq)))
   724        end
   725          else (instantiate' [] [Some cfma,  Some cdlcm, Some cA,Some cat] (not_ast_p_fm))
   726 
   727    |(Const("op <",_) $ c1 $(Const ("op +", _) $(Const ("op *",_) $ pm1 $ y ) $ z )) =>
   728         if (y=x) andalso (c1 =zero) then 
   729         if pm1 = (mk_numeral ~1) then 
   730 	  let val th1 = prove_elementar sg "ss" (Const ("op :",HOLogic.intT --> (HOLogic.mk_setT HOLogic.intT) --> HOLogic.boolT) $ (norm_zero_one z) $ A)
   731               val th2 =  prove_elementar sg "ss" (HOLogic.mk_binrel "op <" (zero,dlcm))
   732 	  in  (instantiate' [] [Some cfma]([th2,th1] MRS (not_ast_p_lt)))
   733 	    end
   734 	 else let val th1 = prove_elementar sg "ss" (HOLogic.mk_binrel "op <" (Const("0",HOLogic.intT),dlcm))
   735 	      in (instantiate' [] [Some cfma, Some cA,Some (cterm_of sg (norm_zero_one z))] (th1 RS (not_ast_p_gt)))
   736 	      end
   737       else (instantiate' [] [Some cfma,  Some cdlcm, Some cA,Some cat] (not_ast_p_fm))
   738 
   739    |Const ("Not",_) $ (Const("Divides.op dvd",_)$ d $ (Const ("op +",_) $ (Const ("op *",_) $ c $ y ) $ z)) => 
   740       if y=x then  
   741            let val cz = cterm_of sg (norm_zero_one z)
   742 	       val th1 = (prove_elementar sg "ss"  (HOLogic.mk_binrel "op =" (HOLogic.mk_binop "Divides.op mod" (dlcm,d),norm_zero_one zero))) RS (((zdvd_iff_zmod_eq_0)RS sym) RS iffD1)
   743  	     in (instantiate' []  [Some cfma, Some cA,Some cz] (th1 RS (not_ast_p_ndvd)))
   744 	     end
   745       else (instantiate' [] [Some cfma,  Some cdlcm, Some cA,Some cat] (not_ast_p_fm))
   746 
   747    |(Const("Divides.op dvd",_)$ d $ (Const ("op +",_) $ (Const ("op *",_) $ c $ y ) $ z)) => 
   748        if y=x then  
   749 	 let val cz = cterm_of sg (norm_zero_one z)
   750 	     val th1 = (prove_elementar sg "ss"  (HOLogic.mk_binrel "op =" (HOLogic.mk_binop "Divides.op mod" (dlcm,d),norm_zero_one zero))) RS (((zdvd_iff_zmod_eq_0)RS sym) RS iffD1)
   751  	    in (instantiate' []  [Some cfma,Some cA,Some cz] (th1 RS (not_ast_p_dvd)))
   752 	  end
   753       else (instantiate' [] [Some cfma,  Some cdlcm, Some cA,Some cat] (not_ast_p_fm))
   754       		
   755    |_ => (instantiate' [] [Some cfma,  Some cdlcm, Some cA,Some cat] (not_ast_p_fm))
   756       		
   757     end;
   758 
   759 (* ------------------------------------------------------------------------ *)
   760 (* Main interpretation function for this backwards dirction*)
   761 (* if atomic do generate atomis formulae else Construct theorems and then make RS with the construction theorems*)
   762 (*Help Function*)
   763 (* ------------------------------------------------------------------------- *)
   764 
   765 fun decomp_nastp sg x dlcm A fm t = case t of 
   766    Const("op &",_) $ ls $ rs => ([ls,rs],fn [th1,th2] => [th1,th2] MRS not_ast_p_conjI )
   767   |Const("op |",_) $ ls $ rs => ([ls,rs],fn [th1,th2] => [th1,th2] MRS not_ast_p_disjI)
   768   |_ => ([], fn [] => generate_atomic_not_ast_p sg x fm dlcm A t);
   769 
   770 fun not_ast_p_proof_of_c sg (x as Free(xn,xT)) fm dlcm A t =
   771   let 
   772        val th =  thm_of sg (decomp_nastp sg x dlcm (list_to_set xT (map norm_zero_one A)) fm) t
   773       val fma = absfree (xn,xT, norm_zero_one fm)
   774   in let val th1 =  prove_elementar sg "ss"  (HOLogic.mk_eq (fma,fma))
   775      in [th,th1] MRS (not_ast_p_Q_elim)
   776      end
   777   end;
   778 
   779 
   780 (* -------------------------------*)
   781 (* Finding rho and beta for evalc *)
   782 (* -------------------------------*)
   783 
   784 fun rho_for_evalc sg at = case at of  
   785     (Const (p,_) $ s $ t) =>(  
   786     case assoc (operations,p) of 
   787         Some f => 
   788            ((if (f ((dest_numeral s),(dest_numeral t))) 
   789              then prove_elementar sg "ss" (HOLogic.mk_eq(at,HOLogic.true_const)) 
   790              else prove_elementar sg "ss" (HOLogic.mk_eq(at, HOLogic.false_const)))  
   791 		   handle _ => instantiate' [Some cboolT] [Some (cterm_of sg at)] refl
   792         | _ => instantiate' [Some cboolT] [Some (cterm_of sg at)] refl )) 
   793      |Const("Not",_)$(Const (p,_) $ s $ t) =>(  
   794        case assoc (operations,p) of 
   795          Some f => 
   796            ((if (f ((dest_numeral s),(dest_numeral t))) 
   797              then prove_elementar sg "ss" (HOLogic.mk_eq(at, HOLogic.false_const))  
   798              else prove_elementar sg "ss" (HOLogic.mk_eq(at,HOLogic.true_const)))  
   799 		      handle _ => instantiate' [Some cboolT] [Some (cterm_of sg at)] refl) 
   800          | _ => instantiate' [Some cboolT] [Some (cterm_of sg at)] refl ) 
   801      | _ =>   instantiate' [Some cboolT] [Some (cterm_of sg at)] refl;
   802 
   803 
   804 (*=========================================================*)
   805 fun decomp_evalc sg t = case t of
   806    (Const("op &",_)$A$B) => ([A,B], fn [th1,th2] => [th1,th2] MRS qe_conjI)
   807    |(Const("op |",_)$A$B) => ([A,B], fn [th1,th2] => [th1,th2] MRS qe_disjI)
   808    |(Const("op -->",_)$A$B) => ([A,B], fn [th1,th2] => [th1,th2] MRS qe_impI)
   809    |(Const("op =", Type ("fun",[Type ("bool", []),_]))$A$B) => ([A,B], fn [th1,th2] => [th1,th2] MRS qe_eqI)
   810    |_ => ([], fn [] => rho_for_evalc sg t);
   811 
   812 
   813 fun proof_of_evalc sg fm = thm_of sg (decomp_evalc sg) fm;
   814 
   815 (*==================================================*)
   816 (*     Proof of linform with the compact model      *)
   817 (*==================================================*)
   818 
   819 
   820 fun decomp_linform sg vars t = case t of
   821    (Const("op &",_)$A$B) => ([A,B], fn [th1,th2] => [th1,th2] MRS qe_conjI)
   822    |(Const("op |",_)$A$B) => ([A,B], fn [th1,th2] => [th1,th2] MRS qe_disjI)
   823    |(Const("op -->",_)$A$B) => ([A,B], fn [th1,th2] => [th1,th2] MRS qe_impI)
   824    |(Const("op =", Type ("fun",[Type ("bool", []),_]))$A$B) => ([A,B], fn [th1,th2] => [th1,th2] MRS qe_eqI)
   825    |(Const("Not",_)$p) => ([p],fn [th] => th RS qe_Not)
   826    |(Const("Divides.op dvd",_)$d$r) => ([], fn [] => (prove_elementar sg "lf" (HOLogic.mk_eq (r, lint vars r))) RS (instantiate' [] [None , None, Some (cterm_of sg d)](linearize_dvd)))
   827    |_ => ([], fn [] => prove_elementar sg "lf" (HOLogic.mk_eq (t, linform vars t)));
   828 
   829 fun proof_of_linform sg vars f = thm_of sg (decomp_linform sg vars) f;
   830 
   831 (* ------------------------------------------------------------------------- *)
   832 (* Interpretaion of Protocols of the cooper procedure : minusinfinity version*)
   833 (* ------------------------------------------------------------------------- *)
   834 fun coopermi_proof_of sg (x as Free(xn,xT)) fm B dlcm =
   835   (* Get the Bset thm*)
   836   let val (minf_eqth, minf_moddth) = minf_proof_of_c sg x dlcm fm 
   837       val dpos = prove_elementar sg "ss" (HOLogic.mk_binrel "op <" (zero,dlcm));
   838       val nbstpthm = not_bst_p_proof_of_c sg x fm dlcm B fm
   839   in (dpos,minf_eqth,nbstpthm,minf_moddth)
   840 end;
   841 
   842 (* ------------------------------------------------------------------------- *)
   843 (* Interpretaion of Protocols of the cooper procedure : plusinfinity version *)
   844 (* ------------------------------------------------------------------------- *)
   845 fun cooperpi_proof_of sg (x as Free(xn,xT)) fm A dlcm =
   846   let val (pinf_eqth,pinf_moddth) = pinf_proof_of_c sg x dlcm fm
   847       val dpos = prove_elementar sg "ss" (HOLogic.mk_binrel "op <" (zero,dlcm));
   848       val nastpthm = not_ast_p_proof_of_c sg x fm dlcm A fm
   849   in (dpos,pinf_eqth,nastpthm,pinf_moddth)
   850 end;
   851 
   852 (* ------------------------------------------------------------------------- *)
   853 (* Interpretaion of Protocols of the cooper procedure : full version*)
   854 (* ------------------------------------------------------------------------- *)
   855 fun cooper_thm sg s (x as Free(xn,xT)) cfm dlcm ast bst= case s of
   856   "pi" => let val (dpsthm,pinf_eqth,nbpth,pinf_moddth) = cooperpi_proof_of sg x cfm ast dlcm 
   857 	      in [dpsthm,pinf_eqth,nbpth,pinf_moddth] MRS (cppi_eq)
   858            end
   859   |"mi" => let val (dpsthm,minf_eqth,nbpth,minf_moddth) = coopermi_proof_of sg x cfm bst dlcm
   860 	       in [dpsthm,minf_eqth,nbpth,minf_moddth] MRS (cpmi_eq)
   861                 end
   862  |_ => error "parameter error";
   863 
   864 (* ------------------------------------------------------------------------- *)
   865 (* This function should evoluate to the end prove Procedure for one quantifier elimination for Presburger arithmetic*)
   866 (* It shoud be plugged in the qfnp argument of the quantifier elimination proof function*)
   867 (* ------------------------------------------------------------------------- *)
   868 
   869 fun cooper_prv sg (x as Free(xn,xT)) efm = let 
   870    (* lfm_thm : efm = linearized form of efm*)
   871    val lfm_thm = proof_of_linform sg [xn] efm
   872    (*efm2 is the linearized form of efm *) 
   873    val efm2 = snd(qe_get_terms lfm_thm)
   874    (* l is the lcm of all coefficients of x *)
   875    val l = formlcm x efm2
   876    (*ac_thm: efm = efm2 with adjusted coefficients of x *)
   877    val ac_thm = [lfm_thm , (proof_of_adjustcoeffeq sg x l efm2)] MRS trans
   878    (* fm is efm2 with adjusted coefficients of x *)
   879    val fm = snd (qe_get_terms ac_thm)
   880   (* cfm is l dvd x & fm' where fm' is fm where l*x is replaced by x*)
   881    val  cfm = unitycoeff x fm
   882    (*afm is fm where c*x is replaced by 1*x or -1*x *)
   883    val afm = adjustcoeff x l fm
   884    (* P = %x.afm*)
   885    val P = absfree(xn,xT,afm)
   886    (* This simpset allows the elimination of the sets in bex {1..d} *)
   887    val ss = presburger_ss addsimps
   888      [simp_from_to] delsimps [P_eqtrue, P_eqfalse, bex_triv, insert_iff]
   889    (* uth : EX x.P(l*x) = EX x. l dvd x & P x*)
   890    val uth = instantiate' [] [Some (cterm_of sg P) , Some (cterm_of sg (mk_numeral l))] (unity_coeff_ex)
   891    (* e_ac_thm : Ex x. efm = EX x. fm*)
   892    val e_ac_thm = (forall_intr (cterm_of sg x) ac_thm) COMP (qe_exI)
   893    (* A and B set of the formula*)
   894    val A = aset x cfm
   895    val B = bset x cfm
   896    (* the divlcm (delta) of the formula*)
   897    val dlcm = mk_numeral (divlcm x cfm)
   898    (* Which set is smaller to generate the (hoepfully) shorter proof*)
   899    val cms = if ((length A) < (length B )) then "pi" else "mi"
   900    (* synthesize the proof of cooper's theorem*)
   901     (* cp_thm: EX x. cfm = Q*)
   902    val cp_thm = cooper_thm sg cms x cfm dlcm A B
   903    (* Exxpand the right hand side to get rid of EX j : {1..d} to get a huge disjunction*)
   904    (* exp_cp_thm: EX x.cfm = Q' , where Q' is a simplified version of Q*)
   905    val exp_cp_thm = refl RS (simplify ss (cp_thm RSN (2,trans)))
   906    (* lsuth = EX.P(l*x) ; rsuth = EX x. l dvd x & P x*)
   907    val (lsuth,rsuth) = qe_get_terms (uth)
   908    (* lseacth = EX x. efm; rseacth = EX x. fm*)
   909    val (lseacth,rseacth) = qe_get_terms(e_ac_thm)
   910    (* lscth = EX x. cfm; rscth = Q' *)
   911    val (lscth,rscth) = qe_get_terms (exp_cp_thm)
   912    (* u_c_thm: EX x. P(l*x) = Q'*)
   913    val  u_c_thm = [([uth,prove_elementar sg "ss" (HOLogic.mk_eq (rsuth,lscth))] MRS trans),exp_cp_thm] MRS trans
   914    (* result: EX x. efm = Q'*)
   915  in  ([e_ac_thm,[(prove_elementar sg "ss" (HOLogic.mk_eq (rseacth,lsuth))),u_c_thm] MRS trans] MRS trans)
   916    end
   917 |cooper_prv _ _ _ =  error "Parameters format";
   918 
   919 
   920 
   921 fun decomp_cnnf sg lfnp P = case P of 
   922      Const ("op &",_) $ p $q => ([p,q] , fn [th1,th2] => [th1,th2] MRS qe_conjI )
   923    |Const ("op |",_) $ p $q => ([p,q] , fn [th1,th2] => [th1,th2] MRS  qe_disjI)
   924    |Const ("Not",_) $ (Const("Not",_) $ p) => ([p], fn [th] => th RS nnf_nn)
   925    |Const("Not",_) $ (Const(opn,T) $ p $ q) => 
   926      if opn = "op |" 
   927       then case (p,q) of 
   928          (A as (Const ("op &",_) $ r $ s),B as (Const ("op &",_) $ r1 $ t)) =>
   929           if r1 = negate r 
   930           then  ([r,HOLogic.Not$s,r1,HOLogic.Not$t],fn [th1_1,th1_2,th2_1,th2_2] => [[th1_1,th1_1] MRS qe_conjI,[th2_1,th2_2] MRS qe_conjI] MRS nnf_sdj)
   931 
   932           else ([HOLogic.Not $ p,HOLogic.Not $ q ], fn [th1,th2] => [th1,th2] MRS nnf_ndj)
   933         |(_,_) => ([HOLogic.Not $ p,HOLogic.Not $ q ], fn [th1,th2] => [th1,th2] MRS nnf_ndj)
   934       else (
   935          case (opn,T) of 
   936            ("op &",_) => ([HOLogic.Not $ p,HOLogic.Not $ q ], fn [th1,th2] =>[th1,th2] MRS nnf_ncj )
   937            |("op -->",_) => ([p,HOLogic.Not $ q ], fn [th1,th2] =>[th1,th2] MRS nnf_nim )
   938            |("op =",Type ("fun",[Type ("bool", []),_])) => 
   939            ([HOLogic.conj $ p $ (HOLogic.Not $ q),HOLogic.conj $ (HOLogic.Not $ p) $ q], fn [th1,th2] => [th1,th2] MRS nnf_neq)
   940             |(_,_) => ([], fn [] => lfnp P)
   941 )
   942 
   943    |(Const ("op -->",_) $ p $ q) => ([HOLogic.Not$p,q], fn [th1,th2] => [th1,th2] MRS nnf_im)
   944 
   945    |(Const ("op =", Type ("fun",[Type ("bool", []),_])) $ p $ q) =>
   946      ([HOLogic.conj $ p $ q,HOLogic.conj $ (HOLogic.Not $ p) $ (HOLogic.Not $ q) ], fn [th1,th2] =>[th1,th2] MRS nnf_eq )
   947    |_ => ([], fn [] => lfnp P);
   948 
   949 
   950 
   951 
   952 fun proof_of_cnnf sg p lfnp = 
   953  let val th1 = thm_of sg (decomp_cnnf sg lfnp) p
   954      val rs = snd(qe_get_terms th1)
   955      val th2 = prove_elementar sg "ss" (HOLogic.mk_eq(rs,simpl rs))
   956   in [th1,th2] MRS trans
   957   end;
   958 
   959 end;