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