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