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