src/HOL/Tools/res_axioms.ML
author mengj
Fri Oct 28 02:25:57 2005 +0200 (2005-10-28)
changeset 18000 ac059afd6b86
parent 17959 8db36a108213
child 18009 df077e122064
permissions -rw-r--r--
Added several new functions that convert HOL Isabelle rules to FOL axiom clauses. The original functions that convert FOL rules to clauses stay with the same names; the new functions have "H" at the end of their names.
Also added function "repeat_RS" to the signature.
paulson@15347
     1
(*  Author: Jia Meng, Cambridge University Computer Laboratory
paulson@15347
     2
    ID: $Id$
paulson@15347
     3
    Copyright 2004 University of Cambridge
paulson@15347
     4
paulson@15347
     5
Transformation of axiom rules (elim/intro/etc) into CNF forms.    
paulson@15347
     6
*)
paulson@15347
     7
paulson@15997
     8
signature RES_AXIOMS =
paulson@15997
     9
  sig
paulson@15997
    10
  exception ELIMR2FOL of string
paulson@17404
    11
  val tagging_enabled : bool
paulson@15997
    12
  val elimRule_tac : thm -> Tactical.tactic
paulson@16012
    13
  val elimR2Fol : thm -> term
paulson@15997
    14
  val transform_elim : thm -> thm
quigley@16039
    15
  val clausify_axiom_pairs : (string*thm) -> (ResClause.clause*thm) list
mengj@18000
    16
  val clausify_axiom_pairsH : (string*thm) -> (ResHolClause.clause*thm) list
paulson@15997
    17
  val cnf_axiom : (string * thm) -> thm list
mengj@18000
    18
  val cnf_axiomH : (string * thm) -> thm list
paulson@15997
    19
  val meta_cnf_axiom : thm -> thm list
mengj@18000
    20
  val meta_cnf_axiomH : thm -> thm list
paulson@16012
    21
  val rm_Eps : (term * term) list -> thm list -> term list
paulson@15997
    22
  val claset_rules_of_thy : theory -> (string * thm) list
paulson@15997
    23
  val simpset_rules_of_thy : theory -> (string * thm) list
paulson@17484
    24
  val claset_rules_of_ctxt: Proof.context -> (string * thm) list
paulson@17484
    25
  val simpset_rules_of_ctxt : Proof.context -> (string * thm) list
paulson@17829
    26
  val clausify_rules_pairs : (string*thm) list -> (ResClause.clause*thm) list list
mengj@18000
    27
  val clausify_rules_pairsH : (string*thm) list -> (ResHolClause.clause*thm) list list
paulson@16563
    28
  val clause_setup : (theory -> theory) list
paulson@16563
    29
  val meson_method_setup : (theory -> theory) list
mengj@17905
    30
  val pairname : thm -> (string * thm)
mengj@18000
    31
  val repeat_RS : thm -> thm -> thm
mengj@18000
    32
paulson@15997
    33
  end;
paulson@15347
    34
paulson@15997
    35
structure ResAxioms : RES_AXIOMS =
paulson@15997
    36
 
paulson@15997
    37
struct
paulson@15347
    38
mengj@18000
    39
paulson@17404
    40
val tagging_enabled = false (*compile_time option*)
paulson@17404
    41
paulson@15997
    42
(**** Transformation of Elimination Rules into First-Order Formulas****)
paulson@15347
    43
paulson@15390
    44
(* a tactic used to prove an elim-rule. *)
paulson@16009
    45
fun elimRule_tac th =
paulson@16009
    46
    ((rtac impI 1) ORELSE (rtac notI 1)) THEN (etac th 1) THEN
paulson@16588
    47
    REPEAT(fast_tac HOL_cs 1);
paulson@15347
    48
paulson@15347
    49
exception ELIMR2FOL of string;
paulson@15347
    50
paulson@15390
    51
(* functions used to construct a formula *)
paulson@15390
    52
paulson@15347
    53
fun make_disjs [x] = x
paulson@15956
    54
  | make_disjs (x :: xs) = HOLogic.mk_disj(x, make_disjs xs)
paulson@15347
    55
paulson@15347
    56
fun make_conjs [x] = x
paulson@15956
    57
  | make_conjs (x :: xs) =  HOLogic.mk_conj(x, make_conjs xs)
paulson@15956
    58
paulson@15956
    59
fun add_EX tm [] = tm
paulson@15956
    60
  | add_EX tm ((x,xtp)::xs) = add_EX (HOLogic.exists_const xtp $ Abs(x,xtp,tm)) xs;
paulson@15347
    61
paulson@15347
    62
paulson@15347
    63
paulson@15956
    64
fun is_neg (Const("Trueprop",_) $ (Const("Not",_) $ Free(p,_))) (Const("Trueprop",_) $ Free(q,_)) = (p = q)
paulson@15371
    65
  | is_neg _ _ = false;
paulson@15371
    66
paulson@15347
    67
paulson@15347
    68
exception STRIP_CONCL;
paulson@15347
    69
paulson@15347
    70
paulson@15371
    71
fun strip_concl' prems bvs (Const ("==>",_) $ P $ Q) =
paulson@15956
    72
      let val P' = HOLogic.dest_Trueprop P
paulson@15956
    73
  	  val prems' = P'::prems
paulson@15956
    74
      in
paulson@15371
    75
	strip_concl' prems' bvs  Q
paulson@15956
    76
      end
paulson@15371
    77
  | strip_concl' prems bvs P = 
paulson@15956
    78
      let val P' = HOLogic.Not $ (HOLogic.dest_Trueprop P)
paulson@15956
    79
      in
paulson@15371
    80
	add_EX (make_conjs (P'::prems)) bvs
paulson@15956
    81
      end;
paulson@15371
    82
paulson@15371
    83
paulson@15371
    84
fun strip_concl prems bvs concl (Const ("all", _) $ Abs (x,xtp,body))  = strip_concl prems ((x,xtp)::bvs) concl body
paulson@15371
    85
  | strip_concl prems bvs concl (Const ("==>",_) $ P $ Q) =
paulson@15371
    86
    if (is_neg P concl) then (strip_concl' prems bvs Q)
paulson@15371
    87
    else
paulson@15956
    88
	(let val P' = HOLogic.dest_Trueprop P
paulson@15371
    89
	     val prems' = P'::prems
paulson@15371
    90
	 in
paulson@15371
    91
	     strip_concl prems' bvs  concl Q
paulson@15371
    92
	 end)
paulson@15371
    93
  | strip_concl prems bvs concl _ = add_EX (make_conjs prems) bvs;
paulson@15347
    94
 
paulson@15347
    95
paulson@15347
    96
paulson@15371
    97
fun trans_elim (main,others,concl) =
paulson@15371
    98
    let val others' = map (strip_concl [] [] concl) others
paulson@15347
    99
	val disjs = make_disjs others'
paulson@15347
   100
    in
paulson@15956
   101
	HOLogic.mk_imp (HOLogic.dest_Trueprop main, disjs)
paulson@15347
   102
    end;
paulson@15347
   103
paulson@15347
   104
paulson@15390
   105
(* aux function of elim2Fol, take away predicate variable. *)
paulson@15371
   106
fun elimR2Fol_aux prems concl = 
paulson@15347
   107
    let val nprems = length prems
paulson@15347
   108
	val main = hd prems
paulson@15347
   109
    in
paulson@15956
   110
	if (nprems = 1) then HOLogic.Not $ (HOLogic.dest_Trueprop main)
paulson@15371
   111
        else trans_elim (main, tl prems, concl)
paulson@15347
   112
    end;
paulson@15347
   113
paulson@15956
   114
    
paulson@16012
   115
(* convert an elim rule into an equivalent formula, of type term. *)
paulson@15347
   116
fun elimR2Fol elimR = 
paulson@15347
   117
    let val elimR' = Drule.freeze_all elimR
paulson@15347
   118
	val (prems,concl) = (prems_of elimR', concl_of elimR')
paulson@15347
   119
    in
paulson@15347
   120
	case concl of Const("Trueprop",_) $ Free(_,Type("bool",[])) 
paulson@15956
   121
		      => HOLogic.mk_Trueprop (elimR2Fol_aux prems concl)
paulson@15956
   122
                    | Free(x,Type("prop",[])) => HOLogic.mk_Trueprop(elimR2Fol_aux prems concl) 
paulson@15347
   123
		    | _ => raise ELIMR2FOL("Not an elimination rule!")
paulson@15347
   124
    end;
paulson@15347
   125
paulson@15347
   126
paulson@15390
   127
(* check if a rule is an elim rule *)
paulson@16009
   128
fun is_elimR th = 
paulson@16009
   129
    case (concl_of th) of (Const ("Trueprop", _) $ Var (idx,_)) => true
paulson@15347
   130
			 | Var(indx,Type("prop",[])) => true
paulson@15347
   131
			 | _ => false;
paulson@15347
   132
paulson@15997
   133
(* convert an elim-rule into an equivalent theorem that does not have the 
paulson@15997
   134
   predicate variable.  Leave other theorems unchanged.*) 
paulson@16009
   135
fun transform_elim th =
paulson@16009
   136
  if is_elimR th then
paulson@16009
   137
    let val tm = elimR2Fol th
paulson@16009
   138
	val ctm = cterm_of (sign_of_thm th) tm	
paulson@15997
   139
    in
wenzelm@17959
   140
	OldGoals.prove_goalw_cterm [] ctm (fn prems => [elimRule_tac th])
paulson@15997
   141
    end
paulson@16563
   142
 else th;
paulson@15997
   143
paulson@15997
   144
paulson@15997
   145
(**** Transformation of Clasets and Simpsets into First-Order Axioms ****)
paulson@15997
   146
paulson@15390
   147
(* repeated resolution *)
paulson@15347
   148
fun repeat_RS thm1 thm2 =
paulson@15347
   149
    let val thm1' =  thm1 RS thm2 handle THM _ => thm1
paulson@15347
   150
    in
paulson@15347
   151
	if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2)
paulson@15347
   152
    end;
paulson@15347
   153
paulson@15347
   154
paulson@16009
   155
(*Convert a theorem into NNF and also skolemize it. Original version, using
paulson@16009
   156
  Hilbert's epsilon in the resulting clauses.*)
mengj@18000
   157
fun skolem_axiom_g mk_nnf th = 
mengj@18000
   158
  let val th' = (skolemize o mk_nnf o ObjectLogic.atomize_thm o Drule.freeze_all) th
paulson@16588
   159
  in  repeat_RS th' someI_ex
paulson@16588
   160
  end;
paulson@15347
   161
paulson@15347
   162
mengj@18000
   163
val skolem_axiom = skolem_axiom_g make_nnf;
mengj@18000
   164
val skolem_axiomH = skolem_axiom_g make_nnf1;
mengj@18000
   165
mengj@18000
   166
paulson@16009
   167
fun cnf_rule th = make_clauses [skolem_axiom (transform_elim th)];
paulson@15347
   168
mengj@18000
   169
fun cnf_ruleH th = make_clauses [skolem_axiomH (transform_elim th)];
mengj@18000
   170
paulson@16563
   171
(*Transfer a theorem into theory Reconstruction.thy if it is not already
paulson@15359
   172
  inside that theory -- because it's needed for Skolemization *)
paulson@15359
   173
paulson@16563
   174
(*This will refer to the final version of theory Reconstruction.*)
paulson@16563
   175
val recon_thy_ref = Theory.self_ref (the_context ());  
paulson@15359
   176
paulson@16563
   177
(*If called while Reconstruction is being created, it will transfer to the
paulson@16563
   178
  current version. If called afterward, it will transfer to the final version.*)
paulson@16009
   179
fun transfer_to_Reconstruction th =
paulson@16563
   180
    transfer (Theory.deref recon_thy_ref) th handle THM _ => th;
paulson@15347
   181
paulson@15955
   182
fun is_taut th =
paulson@15955
   183
      case (prop_of th) of
paulson@15955
   184
           (Const ("Trueprop", _) $ Const ("True", _)) => true
paulson@15955
   185
         | _ => false;
paulson@15955
   186
paulson@15955
   187
(* remove tautologous clauses *)
paulson@15955
   188
val rm_redundant_cls = List.filter (not o is_taut);
paulson@15347
   189
paulson@17829
   190
(* transform an Isabelle theorem into CNF *)
mengj@18000
   191
fun cnf_axiom_aux_g cnf_rule th =
paulson@16173
   192
    map zero_var_indexes
paulson@16009
   193
        (rm_redundant_cls (cnf_rule (transfer_to_Reconstruction th)));
mengj@18000
   194
mengj@18000
   195
val cnf_axiom_aux = cnf_axiom_aux_g cnf_rule;
mengj@18000
   196
val cnf_axiom_auxH = cnf_axiom_aux_g cnf_ruleH;
paulson@15997
   197
       
paulson@15997
   198
       
paulson@16009
   199
(**** SKOLEMIZATION BY INFERENCE (lcp) ****)
paulson@16009
   200
paulson@16009
   201
(*Traverse a term, accumulating Skolem function definitions.*)
paulson@16009
   202
fun declare_skofuns s t thy =
paulson@17404
   203
  let fun dec_sko (Const ("Ex",_) $ (xtp as Abs(_,T,p))) (n, (thy, axs)) =
paulson@16009
   204
	    (*Existential: declare a Skolem function, then insert into body and continue*)
paulson@16009
   205
	    let val cname = s ^ "_" ^ Int.toString n
paulson@16012
   206
		val args = term_frees xtp  (*get the formal parameter list*)
paulson@16009
   207
		val Ts = map type_of args
paulson@16009
   208
		val cT = Ts ---> T
wenzelm@16125
   209
		val c = Const (Sign.full_name (Theory.sign_of thy) cname, cT)
paulson@16009
   210
		val rhs = list_abs_free (map dest_Free args, HOLogic.choice_const T $ xtp)
paulson@16012
   211
		        (*Forms a lambda-abstraction over the formal parameters*)
paulson@16009
   212
		val def = equals cT $ c $ rhs
paulson@16009
   213
		val thy' = Theory.add_consts_i [(cname, cT, NoSyn)] thy
paulson@16012
   214
		           (*Theory is augmented with the constant, then its def*)
paulson@17404
   215
		val cdef = cname ^ "_def"
paulson@17404
   216
		val thy'' = Theory.add_defs_i false [(cdef, def)] thy'
paulson@17404
   217
	    in dec_sko (subst_bound (list_comb(c,args), p)) 
paulson@17404
   218
	               (n+1, (thy'', get_axiom thy'' cdef :: axs)) 
paulson@17404
   219
	    end
paulson@17404
   220
	| dec_sko (Const ("All",_) $ (xtp as Abs(a,T,p))) (n, thx) =
paulson@16012
   221
	    (*Universal quant: insert a free variable into body and continue*)
paulson@16009
   222
	    let val fname = variant (add_term_names (p,[])) a
paulson@17404
   223
	    in dec_sko (subst_bound (Free(fname,T), p)) (n, thx) end
paulson@16009
   224
	| dec_sko (Const ("op &", _) $ p $ q) nthy = 
paulson@16009
   225
	    dec_sko q (dec_sko p nthy)
paulson@16009
   226
	| dec_sko (Const ("op |", _) $ p $ q) nthy = 
paulson@16009
   227
	    dec_sko q (dec_sko p nthy)
paulson@17404
   228
	| dec_sko (Const ("HOL.tag", _) $ p) nthy = 
paulson@17404
   229
	    dec_sko p nthy
paulson@16009
   230
	| dec_sko (Const ("Trueprop", _) $ p) nthy = 
paulson@16009
   231
	    dec_sko p nthy
paulson@17404
   232
	| dec_sko t nthx = nthx (*Do nothing otherwise*)
paulson@17404
   233
  in  #2 (dec_sko t (1, (thy,[])))  end;
paulson@16009
   234
paulson@16009
   235
(*cterms are used throughout for efficiency*)
paulson@16009
   236
val cTrueprop = Thm.cterm_of (Theory.sign_of HOL.thy) HOLogic.Trueprop;
paulson@16009
   237
paulson@16009
   238
(*cterm version of mk_cTrueprop*)
paulson@16009
   239
fun c_mkTrueprop A = Thm.capply cTrueprop A;
paulson@16009
   240
paulson@16009
   241
(*Given an abstraction over n variables, replace the bound variables by free
paulson@16009
   242
  ones. Return the body, along with the list of free variables.*)
paulson@16009
   243
fun c_variant_abs_multi (ct0, vars) = 
paulson@16009
   244
      let val (cv,ct) = Thm.dest_abs NONE ct0
paulson@16009
   245
      in  c_variant_abs_multi (ct, cv::vars)  end
paulson@16009
   246
      handle CTERM _ => (ct0, rev vars);
paulson@16009
   247
paulson@16009
   248
(*Given the definition of a Skolem function, return a theorem to replace 
paulson@16009
   249
  an existential formula by a use of that function.*)
paulson@16588
   250
fun skolem_of_def def =  
paulson@16009
   251
  let val (c,rhs) = Drule.dest_equals (cprop_of (Drule.freeze_all def))
paulson@16009
   252
      val (ch, frees) = c_variant_abs_multi (rhs, [])
paulson@16009
   253
      val (chil,cabs) = Thm.dest_comb ch
paulson@16588
   254
      val {sign,t, ...} = rep_cterm chil
paulson@16009
   255
      val (Const ("Hilbert_Choice.Eps", Type("fun",[_,T]))) = t
paulson@16009
   256
      val cex = Thm.cterm_of sign (HOLogic.exists_const T)
paulson@16009
   257
      val ex_tm = c_mkTrueprop (Thm.capply cex cabs)
paulson@16009
   258
      and conc =  c_mkTrueprop (Drule.beta_conv cabs (Drule.list_comb(c,frees)));
wenzelm@17959
   259
  in  OldGoals.prove_goalw_cterm [def] (Drule.mk_implies (ex_tm, conc))
paulson@16009
   260
	    (fn [prem] => [ rtac (prem RS someI_ex) 1 ])
paulson@16009
   261
  end;	 
paulson@16009
   262
paulson@16009
   263
paulson@16009
   264
(*Converts an Isabelle theorem (intro, elim or simp format) into nnf.*)
paulson@16009
   265
fun to_nnf thy th = 
paulson@16588
   266
    th |> Thm.transfer thy
paulson@16588
   267
       |> transform_elim |> Drule.freeze_all
paulson@16588
   268
       |> ObjectLogic.atomize_thm |> make_nnf;
paulson@16009
   269
paulson@16009
   270
(*The cache prevents repeated clausification of a theorem, 
wenzelm@16800
   271
  and also repeated declaration of Skolem functions*)  (* FIXME better use Termtab!? *)
paulson@15955
   272
val clause_cache = ref (Symtab.empty : (thm * thm list) Symtab.table)
paulson@15955
   273
paulson@16009
   274
(*Declare Skolem functions for a theorem, supplied in nnf and with its name*)
paulson@16009
   275
fun skolem thy (name,th) =
paulson@16588
   276
  let val cname = (case name of "" => gensym "sko" | s => Sign.base_name s)
paulson@17404
   277
      val (thy',axs) = declare_skofuns cname (#prop (rep_thm th)) thy
paulson@17404
   278
  in (map skolem_of_def axs, thy') end;
paulson@16009
   279
paulson@16009
   280
(*Populate the clause cache using the supplied theorems*)
paulson@16009
   281
fun skolemlist [] thy = thy
paulson@16009
   282
  | skolemlist ((name,th)::nths) thy = 
wenzelm@17412
   283
      (case Symtab.lookup (!clause_cache) name of
paulson@16009
   284
	  NONE => 
paulson@16588
   285
	    let val (nnfth,ok) = (to_nnf thy th, true)  
paulson@16588
   286
	                 handle THM _ => (asm_rl, false)
paulson@16588
   287
            in
paulson@16588
   288
                if ok then
paulson@16588
   289
                    let val (skoths,thy') = skolem thy (name, nnfth)
paulson@16588
   290
			val cls = Meson.make_cnf skoths nnfth
wenzelm@17412
   291
		    in change clause_cache (Symtab.update (name, (th, cls)));
paulson@16588
   292
			skolemlist nths thy'
paulson@16588
   293
		    end
paulson@16588
   294
		else skolemlist nths thy
paulson@16588
   295
            end
paulson@16009
   296
	| SOME _ => skolemlist nths thy) (*FIXME: check for duplicate names?*)
paulson@16009
   297
paulson@16009
   298
(*Exported function to convert Isabelle theorems into axiom clauses*) 
mengj@18000
   299
fun cnf_axiom_g cnf_axiom_aux (name,th) =
paulson@15956
   300
    case name of
paulson@15955
   301
	  "" => cnf_axiom_aux th (*no name, so can't cache*)
wenzelm@17412
   302
	| s  => case Symtab.lookup (!clause_cache) s of
paulson@15955
   303
	  	  NONE => 
paulson@15955
   304
		    let val cls = cnf_axiom_aux th
wenzelm@17412
   305
		    in change clause_cache (Symtab.update (s, (th, cls))); cls end
paulson@15955
   306
	        | SOME(th',cls) =>
paulson@15955
   307
		    if eq_thm(th,th') then cls
paulson@15955
   308
		    else (*New theorem stored under the same name? Possible??*)
paulson@15955
   309
		      let val cls = cnf_axiom_aux th
wenzelm@17412
   310
		      in change clause_cache (Symtab.update (s, (th, cls))); cls end;
paulson@15347
   311
mengj@18000
   312
mengj@18000
   313
val cnf_axiom = cnf_axiom_g cnf_axiom_aux;
mengj@18000
   314
val cnf_axiomH = cnf_axiom_g cnf_axiom_auxH;
mengj@18000
   315
mengj@18000
   316
paulson@15956
   317
fun pairname th = (Thm.name_of_thm th, th);
paulson@15956
   318
paulson@15956
   319
fun meta_cnf_axiom th = 
paulson@15956
   320
    map Meson.make_meta_clause (cnf_axiom (pairname th));
paulson@15499
   321
paulson@15347
   322
mengj@18000
   323
fun meta_cnf_axiomH th = 
mengj@18000
   324
    map Meson.make_meta_clause (cnf_axiomH (pairname th));
mengj@18000
   325
paulson@15347
   326
(* changed: with one extra case added *)
paulson@15956
   327
fun univ_vars_of_aux (Const ("Hilbert_Choice.Eps",_) $ Abs(_,_,body)) vars =    
paulson@15956
   328
      univ_vars_of_aux body vars
paulson@15956
   329
  | univ_vars_of_aux (Const ("Ex",_) $ Abs(_,_,body)) vars = 
paulson@15956
   330
      univ_vars_of_aux body vars (* EX x. body *)
paulson@15347
   331
  | univ_vars_of_aux (P $ Q) vars =
paulson@15956
   332
      univ_vars_of_aux Q (univ_vars_of_aux P vars)
paulson@15347
   333
  | univ_vars_of_aux (t as Var(_,_)) vars = 
paulson@15956
   334
      if (t mem vars) then vars else (t::vars)
paulson@15347
   335
  | univ_vars_of_aux _ vars = vars;
paulson@15347
   336
  
paulson@15347
   337
fun univ_vars_of t = univ_vars_of_aux t [];
paulson@15347
   338
paulson@15347
   339
paulson@17819
   340
(**** Generating Skoklem Functions ****)
paulson@17819
   341
paulson@17819
   342
val skolem_prefix = "sk_";
paulson@17819
   343
val skolem_id = ref 0;
paulson@17819
   344
paulson@17819
   345
fun gen_skolem_name () =
paulson@17819
   346
  let val sk_fun = skolem_prefix ^ string_of_int (! skolem_id);
paulson@17819
   347
  in inc skolem_id; sk_fun end;
paulson@17819
   348
paulson@17819
   349
fun gen_skolem univ_vars tp =
paulson@17819
   350
  let
paulson@17819
   351
    val skolem_type = map Term.fastype_of univ_vars ---> tp
paulson@17819
   352
    val skolem_term = Const (gen_skolem_name (), skolem_type)
paulson@17819
   353
  in Term.list_comb (skolem_term, univ_vars) end;
paulson@17819
   354
paulson@15347
   355
fun get_new_skolem epss (t as (Const ("Hilbert_Choice.Eps",_) $ Abs(_,tp,_)))  = 
paulson@15347
   356
    let val all_vars = univ_vars_of t
paulson@17819
   357
	val sk_term = gen_skolem all_vars tp
paulson@15347
   358
    in
paulson@15347
   359
	(sk_term,(t,sk_term)::epss)
paulson@15347
   360
    end;
paulson@15347
   361
paulson@17404
   362
(*FIXME: use a-list lookup!!*)
skalberg@15531
   363
fun sk_lookup [] t = NONE
skalberg@15531
   364
  | sk_lookup ((tm,sk_tm)::tms) t = if (t = tm) then SOME (sk_tm) else (sk_lookup tms t);
paulson@15347
   365
paulson@15390
   366
(* get the proper skolem term to replace epsilon term *)
paulson@15347
   367
fun get_skolem epss t = 
paulson@15956
   368
    case (sk_lookup epss t) of NONE => get_new_skolem epss t
paulson@15956
   369
		             | SOME sk => (sk,epss);
paulson@15347
   370
paulson@16009
   371
fun rm_Eps_cls_aux epss (t as (Const ("Hilbert_Choice.Eps",_) $ Abs(_,_,_))) = 
paulson@16009
   372
       get_skolem epss t
paulson@15347
   373
  | rm_Eps_cls_aux epss (P $ Q) =
paulson@16009
   374
       let val (P',epss') = rm_Eps_cls_aux epss P
paulson@16009
   375
	   val (Q',epss'') = rm_Eps_cls_aux epss' Q
paulson@16009
   376
       in (P' $ Q',epss'') end
paulson@15347
   377
  | rm_Eps_cls_aux epss t = (t,epss);
paulson@15347
   378
paulson@16009
   379
fun rm_Eps_cls epss th = rm_Eps_cls_aux epss (prop_of th);
paulson@15347
   380
paulson@17404
   381
(* replace the epsilon terms in a formula by skolem terms. *)
paulson@15347
   382
fun rm_Eps _ [] = []
paulson@17829
   383
  | rm_Eps epss (th::ths) = 
paulson@16009
   384
      let val (th',epss') = rm_Eps_cls epss th
paulson@17829
   385
      in th' :: (rm_Eps epss' ths) end;
paulson@15347
   386
paulson@15347
   387
paulson@15347
   388
paulson@15872
   389
(**** Extract and Clausify theorems from a theory's claset and simpset ****)
paulson@15347
   390
paulson@17404
   391
(*Preserve the name of "th" after the transformation "f"*)
paulson@17404
   392
fun preserve_name f th = Thm.name_thm (Thm.name_of_thm th, f th);
paulson@17404
   393
paulson@17404
   394
(*Tags identify the major premise or conclusion, as hints to resolution provers.
paulson@17404
   395
  However, they don't appear to help in recent tests, and they complicate the code.*)
paulson@17404
   396
val tagI = thm "tagI";
paulson@17404
   397
val tagD = thm "tagD";
paulson@17404
   398
paulson@17404
   399
val tag_intro = preserve_name (fn th => th RS tagI);
paulson@17404
   400
val tag_elim  = preserve_name (fn th => tagD RS th);
paulson@17404
   401
paulson@17484
   402
fun rules_of_claset cs =
paulson@17484
   403
  let val {safeIs,safeEs,hazIs,hazEs,...} = rep_cs cs
paulson@17484
   404
      val intros = safeIs @ hazIs
paulson@17484
   405
      val elims  = safeEs @ hazEs
paulson@17404
   406
  in
paulson@17484
   407
     debug ("rules_of_claset intros: " ^ Int.toString(length intros) ^ 
paulson@17484
   408
            " elims: " ^ Int.toString(length elims));
paulson@17404
   409
     if tagging_enabled 
paulson@17404
   410
     then map pairname (map tag_intro intros @ map tag_elim elims)
paulson@17484
   411
     else map pairname (intros @ elims)
paulson@17404
   412
  end;
paulson@15347
   413
paulson@17484
   414
fun rules_of_simpset ss =
paulson@17484
   415
  let val ({rules,...}, _) = rep_ss ss
paulson@17484
   416
      val simps = Net.entries rules
paulson@17484
   417
  in 
paulson@17484
   418
      debug ("rules_of_simpset: " ^ Int.toString(length simps));
paulson@17484
   419
      map (fn r => (#name r, #thm r)) simps
paulson@17484
   420
  end;
paulson@17484
   421
paulson@17484
   422
fun claset_rules_of_thy thy = rules_of_claset (claset_of thy);
paulson@17484
   423
fun simpset_rules_of_thy thy = rules_of_simpset (simpset_of thy);
paulson@17484
   424
paulson@17484
   425
fun claset_rules_of_ctxt ctxt = rules_of_claset (local_claset_of ctxt);
paulson@17484
   426
fun simpset_rules_of_ctxt ctxt = rules_of_simpset (local_simpset_of ctxt);
paulson@15347
   427
paulson@15347
   428
paulson@15872
   429
(**** Translate a set of classical/simplifier rules into CNF (still as type "thm")  ****)
paulson@15347
   430
paulson@15347
   431
(* classical rules *)
mengj@18000
   432
fun cnf_rules_g cnf_axiom [] err_list = ([],err_list)
mengj@18000
   433
  | cnf_rules_g cnf_axiom ((name,th) :: ths) err_list = 
mengj@18000
   434
      let val (ts,es) = cnf_rules_g cnf_axiom ths err_list
paulson@17404
   435
      in  (cnf_axiom (name,th) :: ts,es) handle  _ => (ts, (th::es))  end;  
paulson@15347
   436
paulson@15347
   437
mengj@18000
   438
val cnf_rules = cnf_rules_g cnf_axiom;
mengj@18000
   439
val cnf_rulesH = cnf_rules_g cnf_axiomH;
mengj@18000
   440
mengj@18000
   441
paulson@15872
   442
(**** Convert all theorems of a claset/simpset into clauses (ResClause.clause) ****)
paulson@15347
   443
paulson@17404
   444
fun addclause (c,th) l =
paulson@17404
   445
  if ResClause.isTaut c then l else (c,th) :: l;
paulson@17404
   446
mengj@18000
   447
fun addclauseH (c,th) l =
mengj@18000
   448
    if ResHolClause.isTaut c then l else (c,th)::l;
mengj@18000
   449
mengj@18000
   450
paulson@17829
   451
(* outputs a list of (clause,theorem) pairs *)
paulson@17829
   452
fun clausify_axiom_pairs (thm_name,th) =
paulson@17829
   453
    let val isa_clauses = cnf_axiom (thm_name,th) 
quigley@16039
   454
        val isa_clauses' = rm_Eps [] isa_clauses
quigley@16039
   455
        val clauses_n = length isa_clauses
quigley@16039
   456
	fun make_axiom_clauses _ [] []= []
paulson@16897
   457
	  | make_axiom_clauses i (cls::clss) (cls'::clss') =
paulson@17404
   458
	      addclause (ResClause.make_axiom_clause cls (thm_name,i), cls') 
paulson@17404
   459
	                (make_axiom_clauses (i+1) clss clss')
paulson@15347
   460
    in
quigley@16039
   461
	make_axiom_clauses 0 isa_clauses' isa_clauses		
paulson@17404
   462
    end
paulson@15347
   463
mengj@18000
   464
mengj@18000
   465
fun clausify_axiom_pairsH (thm_name,th) =
mengj@18000
   466
    let val isa_clauses = cnf_axiomH (thm_name,th) 
mengj@18000
   467
        val isa_clauses' = rm_Eps [] isa_clauses
mengj@18000
   468
        val clauses_n = length isa_clauses
mengj@18000
   469
	fun make_axiom_clauses _ [] []= []
mengj@18000
   470
	  | make_axiom_clauses i (cls::clss) (cls'::clss') =
mengj@18000
   471
	      addclauseH (ResHolClause.make_axiom_clause cls (thm_name,i), cls') 
mengj@18000
   472
	                (make_axiom_clauses (i+1) clss clss')
mengj@18000
   473
    in
mengj@18000
   474
	make_axiom_clauses 0 isa_clauses' isa_clauses		
mengj@18000
   475
    end
mengj@18000
   476
mengj@18000
   477
mengj@18000
   478
paulson@17829
   479
fun clausify_rules_pairs_aux result [] = result
paulson@17829
   480
  | clausify_rules_pairs_aux result ((name,th)::ths) =
paulson@17829
   481
      clausify_rules_pairs_aux (clausify_axiom_pairs (name,th) :: result) ths
paulson@17829
   482
      handle THM (msg,_,_) =>  
paulson@17829
   483
	      (debug ("Cannot clausify " ^ name ^ ": " ^ msg); 
paulson@17829
   484
	       clausify_rules_pairs_aux result ths)
paulson@17829
   485
	 |  ResClause.CLAUSE (msg,t) => 
paulson@17829
   486
	      (debug ("Cannot clausify " ^ name ^ ": " ^ msg ^
paulson@17829
   487
		      ": " ^ TermLib.string_of_term t); 
paulson@17829
   488
	       clausify_rules_pairs_aux result ths)
paulson@17404
   489
mengj@18000
   490
mengj@18000
   491
fun clausify_rules_pairs_auxH result [] = result
mengj@18000
   492
  | clausify_rules_pairs_auxH result ((name,th)::ths) =
mengj@18000
   493
      clausify_rules_pairs_auxH (clausify_axiom_pairsH (name,th) :: result) ths
mengj@18000
   494
      handle THM (msg,_,_) =>  
mengj@18000
   495
	      (debug ("Cannot clausify " ^ name ^ ": " ^ msg); 
mengj@18000
   496
	       clausify_rules_pairs_auxH result ths)
mengj@18000
   497
	 |  ResHolClause.LAM2COMB (t) => 
mengj@18000
   498
	      (debug ("Cannot clausify "  ^ TermLib.string_of_term t); 
mengj@18000
   499
	       clausify_rules_pairs_auxH result ths)
quigley@16039
   500
paulson@15347
   501
mengj@18000
   502
mengj@18000
   503
val clausify_rules_pairs = clausify_rules_pairs_aux [];
mengj@18000
   504
mengj@18000
   505
val clausify_rules_pairsH = clausify_rules_pairs_auxH [];
paulson@16009
   506
(*Setup function: takes a theory and installs ALL simprules and claset rules 
paulson@16009
   507
  into the clause cache*)
paulson@16009
   508
fun clause_cache_setup thy =
paulson@16009
   509
  let val simps = simpset_rules_of_thy thy
paulson@16009
   510
      and clas  = claset_rules_of_thy thy
paulson@16009
   511
  in skolemlist clas (skolemlist simps thy) end;
paulson@16009
   512
  
paulson@16563
   513
val clause_setup = [clause_cache_setup];  
paulson@16563
   514
paulson@16563
   515
paulson@16563
   516
(*** meson proof methods ***)
paulson@16563
   517
paulson@16563
   518
fun cnf_rules_of_ths ths = List.concat (#1 (cnf_rules (map pairname ths) []));
paulson@16563
   519
paulson@16563
   520
fun meson_meth ths ctxt =
paulson@16563
   521
  Method.SIMPLE_METHOD' HEADGOAL
paulson@16563
   522
    (CHANGED_PROP o Meson.meson_claset_tac (cnf_rules_of_ths ths) (local_claset_of ctxt));
paulson@16563
   523
paulson@16563
   524
val meson_method_setup =
paulson@16563
   525
 [Method.add_methods
paulson@16563
   526
  [("meson", Method.thms_ctxt_args meson_meth, 
paulson@16563
   527
    "The MESON resolution proof procedure")]];
paulson@15347
   528
paulson@15347
   529
end;