src/HOL/Tools/res_axioms.ML
author quigley
Mon May 23 00:18:51 2005 +0200 (2005-05-23)
changeset 16039 dfe264950511
parent 16012 4ae42d8f2fea
child 16125 bd13a0017137
permissions -rw-r--r--
Moved some of the clausify functions from ATP/res_clasimpset.ML to res_axioms.ML.
Changed watcher.ML so that the Unix.execute and Unix.reap functions are used instead of those in modUnix.ML, and
consequently removed modUnix.ML from Reconstruction.thy
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@15997
    11
  val elimRule_tac : thm -> Tactical.tactic
paulson@16012
    12
  val elimR2Fol : thm -> term
paulson@15997
    13
  val transform_elim : thm -> thm
quigley@16039
    14
  val clausify_axiom_pairs : (string*thm) -> (ResClause.clause*thm) list
paulson@15997
    15
  val cnf_axiom : (string * thm) -> thm list
paulson@15997
    16
  val meta_cnf_axiom : thm -> thm list
paulson@15997
    17
  val cnf_rule : thm -> thm list
paulson@15997
    18
  val cnf_classical_rules_thy : theory -> thm list list * thm list
quigley@16039
    19
 
paulson@15997
    20
  val cnf_simpset_rules_thy : theory -> thm list list * thm list
quigley@16039
    21
 
paulson@16012
    22
  val rm_Eps : (term * term) list -> thm list -> term list
paulson@15997
    23
  val claset_rules_of_thy : theory -> (string * thm) list
paulson@15997
    24
  val simpset_rules_of_thy : theory -> (string * thm) list
quigley@16039
    25
  val clausify_rules_pairs : (string * thm) list -> thm list -> (ResClause.clause * thm) list list * thm list
paulson@16009
    26
  val setup : (theory -> theory) list
paulson@15997
    27
  end;
paulson@15347
    28
paulson@15997
    29
structure ResAxioms : RES_AXIOMS =
paulson@15997
    30
 
paulson@15997
    31
struct
paulson@15347
    32
paulson@15997
    33
(**** Transformation of Elimination Rules into First-Order Formulas****)
paulson@15347
    34
paulson@15390
    35
(* a tactic used to prove an elim-rule. *)
paulson@16009
    36
fun elimRule_tac th =
paulson@16009
    37
    ((rtac impI 1) ORELSE (rtac notI 1)) THEN (etac th 1) THEN
paulson@15371
    38
    REPEAT(Fast_tac 1);
paulson@15347
    39
paulson@15347
    40
paulson@15347
    41
(* This following version fails sometimes, need to investigate, do not use it now. *)
paulson@16009
    42
fun elimRule_tac' th =
paulson@16009
    43
   ((rtac impI 1) ORELSE (rtac notI 1)) THEN (etac th 1) THEN
paulson@15347
    44
   REPEAT(SOLVE((etac exI 1) ORELSE (rtac conjI 1) ORELSE (rtac disjI1 1) ORELSE (rtac disjI2 1))); 
paulson@15347
    45
paulson@15347
    46
paulson@15347
    47
exception ELIMR2FOL of string;
paulson@15347
    48
paulson@15390
    49
(* functions used to construct a formula *)
paulson@15390
    50
paulson@15347
    51
fun make_disjs [x] = x
paulson@15956
    52
  | make_disjs (x :: xs) = HOLogic.mk_disj(x, make_disjs xs)
paulson@15347
    53
paulson@15347
    54
fun make_conjs [x] = x
paulson@15956
    55
  | make_conjs (x :: xs) =  HOLogic.mk_conj(x, make_conjs xs)
paulson@15956
    56
paulson@15956
    57
fun add_EX tm [] = tm
paulson@15956
    58
  | add_EX tm ((x,xtp)::xs) = add_EX (HOLogic.exists_const xtp $ Abs(x,xtp,tm)) xs;
paulson@15347
    59
paulson@15347
    60
paulson@15347
    61
paulson@15956
    62
fun is_neg (Const("Trueprop",_) $ (Const("Not",_) $ Free(p,_))) (Const("Trueprop",_) $ Free(q,_)) = (p = q)
paulson@15371
    63
  | is_neg _ _ = false;
paulson@15371
    64
paulson@15347
    65
paulson@15347
    66
exception STRIP_CONCL;
paulson@15347
    67
paulson@15347
    68
paulson@15371
    69
fun strip_concl' prems bvs (Const ("==>",_) $ P $ Q) =
paulson@15956
    70
      let val P' = HOLogic.dest_Trueprop P
paulson@15956
    71
  	  val prems' = P'::prems
paulson@15956
    72
      in
paulson@15371
    73
	strip_concl' prems' bvs  Q
paulson@15956
    74
      end
paulson@15371
    75
  | strip_concl' prems bvs P = 
paulson@15956
    76
      let val P' = HOLogic.Not $ (HOLogic.dest_Trueprop P)
paulson@15956
    77
      in
paulson@15371
    78
	add_EX (make_conjs (P'::prems)) bvs
paulson@15956
    79
      end;
paulson@15371
    80
paulson@15371
    81
paulson@15371
    82
fun strip_concl prems bvs concl (Const ("all", _) $ Abs (x,xtp,body))  = strip_concl prems ((x,xtp)::bvs) concl body
paulson@15371
    83
  | strip_concl prems bvs concl (Const ("==>",_) $ P $ Q) =
paulson@15371
    84
    if (is_neg P concl) then (strip_concl' prems bvs Q)
paulson@15371
    85
    else
paulson@15956
    86
	(let val P' = HOLogic.dest_Trueprop P
paulson@15371
    87
	     val prems' = P'::prems
paulson@15371
    88
	 in
paulson@15371
    89
	     strip_concl prems' bvs  concl Q
paulson@15371
    90
	 end)
paulson@15371
    91
  | strip_concl prems bvs concl _ = add_EX (make_conjs prems) bvs;
paulson@15347
    92
 
paulson@15347
    93
paulson@15347
    94
paulson@15371
    95
fun trans_elim (main,others,concl) =
paulson@15371
    96
    let val others' = map (strip_concl [] [] concl) others
paulson@15347
    97
	val disjs = make_disjs others'
paulson@15347
    98
    in
paulson@15956
    99
	HOLogic.mk_imp (HOLogic.dest_Trueprop main, disjs)
paulson@15347
   100
    end;
paulson@15347
   101
paulson@15347
   102
paulson@15390
   103
(* aux function of elim2Fol, take away predicate variable. *)
paulson@15371
   104
fun elimR2Fol_aux prems concl = 
paulson@15347
   105
    let val nprems = length prems
paulson@15347
   106
	val main = hd prems
paulson@15347
   107
    in
paulson@15956
   108
	if (nprems = 1) then HOLogic.Not $ (HOLogic.dest_Trueprop main)
paulson@15371
   109
        else trans_elim (main, tl prems, concl)
paulson@15347
   110
    end;
paulson@15347
   111
paulson@15956
   112
    
paulson@16012
   113
(* convert an elim rule into an equivalent formula, of type term. *)
paulson@15347
   114
fun elimR2Fol elimR = 
paulson@15347
   115
    let val elimR' = Drule.freeze_all elimR
paulson@15347
   116
	val (prems,concl) = (prems_of elimR', concl_of elimR')
paulson@15347
   117
    in
paulson@15347
   118
	case concl of Const("Trueprop",_) $ Free(_,Type("bool",[])) 
paulson@15956
   119
		      => HOLogic.mk_Trueprop (elimR2Fol_aux prems concl)
paulson@15956
   120
                    | Free(x,Type("prop",[])) => HOLogic.mk_Trueprop(elimR2Fol_aux prems concl) 
paulson@15347
   121
		    | _ => raise ELIMR2FOL("Not an elimination rule!")
paulson@15347
   122
    end;
paulson@15347
   123
paulson@15347
   124
paulson@15390
   125
(* check if a rule is an elim rule *)
paulson@16009
   126
fun is_elimR th = 
paulson@16009
   127
    case (concl_of th) of (Const ("Trueprop", _) $ Var (idx,_)) => true
paulson@15347
   128
			 | Var(indx,Type("prop",[])) => true
paulson@15347
   129
			 | _ => false;
paulson@15347
   130
paulson@15997
   131
(* convert an elim-rule into an equivalent theorem that does not have the 
paulson@15997
   132
   predicate variable.  Leave other theorems unchanged.*) 
paulson@16009
   133
fun transform_elim th =
paulson@16009
   134
  if is_elimR th then
paulson@16009
   135
    let val tm = elimR2Fol th
paulson@16009
   136
	val ctm = cterm_of (sign_of_thm th) tm	
paulson@15997
   137
    in
quigley@16039
   138
paulson@16009
   139
	prove_goalw_cterm [] ctm (fn prems => [elimRule_tac th])
paulson@15997
   140
    end
quigley@16039
   141
 else th;;	
quigley@16039
   142
quigley@16039
   143
quigley@16039
   144
paulson@15997
   145
paulson@15997
   146
paulson@15997
   147
(**** Transformation of Clasets and Simpsets into First-Order Axioms ****)
paulson@15997
   148
paulson@15997
   149
(* to be fixed: cnf_intro, cnf_rule, is_introR *)
paulson@15347
   150
paulson@15390
   151
(* repeated resolution *)
paulson@15347
   152
fun repeat_RS thm1 thm2 =
paulson@15347
   153
    let val thm1' =  thm1 RS thm2 handle THM _ => thm1
paulson@15347
   154
    in
paulson@15347
   155
	if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2)
paulson@15347
   156
    end;
paulson@15347
   157
paulson@15347
   158
paulson@16009
   159
(*Convert a theorem into NNF and also skolemize it. Original version, using
paulson@16009
   160
  Hilbert's epsilon in the resulting clauses.*)
paulson@16009
   161
fun skolem_axiom th = 
paulson@16009
   162
  if Term.is_first_order (prop_of th) then
paulson@16009
   163
    let val th' = (skolemize o make_nnf o ObjectLogic.atomize_thm o Drule.freeze_all) th
paulson@15347
   164
    in 
paulson@16009
   165
	repeat_RS th' someI_ex
paulson@15872
   166
    end
paulson@16009
   167
  else raise THM ("skolem_axiom: not first-order", 0, [th]);
paulson@15347
   168
paulson@15347
   169
paulson@16009
   170
fun cnf_rule th = make_clauses [skolem_axiom (transform_elim th)];
paulson@15347
   171
paulson@15370
   172
(*Transfer a theorem in to theory Reconstruction.thy if it is not already
paulson@15359
   173
  inside that theory -- because it's needed for Skolemization *)
paulson@15359
   174
paulson@15370
   175
val recon_thy = ThyInfo.get_theory"Reconstruction";
paulson@15359
   176
paulson@16009
   177
fun transfer_to_Reconstruction th =
paulson@16009
   178
    transfer recon_thy th handle THM _ => th;
paulson@15347
   179
paulson@15955
   180
fun is_taut th =
paulson@15955
   181
      case (prop_of th) of
paulson@15955
   182
           (Const ("Trueprop", _) $ Const ("True", _)) => true
paulson@15955
   183
         | _ => false;
paulson@15955
   184
paulson@15955
   185
(* remove tautologous clauses *)
paulson@15955
   186
val rm_redundant_cls = List.filter (not o is_taut);
paulson@15347
   187
paulson@15347
   188
(* transform an Isabelle thm into CNF *)
paulson@16009
   189
fun cnf_axiom_aux th =
paulson@15997
   190
    map (zero_var_indexes o Thm.varifyT) 
paulson@16009
   191
        (rm_redundant_cls (cnf_rule (transfer_to_Reconstruction th)));
paulson@15997
   192
       
paulson@15997
   193
       
paulson@16009
   194
(**** SKOLEMIZATION BY INFERENCE (lcp) ****)
paulson@16009
   195
paulson@16009
   196
(*Traverse a term, accumulating Skolem function definitions.*)
paulson@16009
   197
fun declare_skofuns s t thy =
paulson@16009
   198
  let fun dec_sko (Const ("Ex",_) $ (xtp as Abs(_,T,p))) (n, thy) =
paulson@16009
   199
	    (*Existential: declare a Skolem function, then insert into body and continue*)
paulson@16009
   200
	    let val cname = s ^ "_" ^ Int.toString n
paulson@16012
   201
		val args = term_frees xtp  (*get the formal parameter list*)
paulson@16009
   202
		val Ts = map type_of args
paulson@16009
   203
		val cT = Ts ---> T
paulson@16009
   204
		val c = Const(NameSpace.append (PureThy.get_name thy) cname, cT)
paulson@16012
   205
		        (*Generates a compound constant name in the given theory*)
paulson@16009
   206
		val rhs = list_abs_free (map dest_Free args, HOLogic.choice_const T $ xtp)
paulson@16012
   207
		        (*Forms a lambda-abstraction over the formal parameters*)
paulson@16009
   208
		val def = equals cT $ c $ rhs
paulson@16009
   209
		val thy' = Theory.add_consts_i [(cname, cT, NoSyn)] thy
paulson@16012
   210
		           (*Theory is augmented with the constant, then its def*)
paulson@16009
   211
		val thy'' = Theory.add_defs_i false [(cname ^ "_def", def)] thy'
paulson@16009
   212
	    in dec_sko (subst_bound (list_comb(c,args), p)) (n+1, thy'') end
paulson@16009
   213
	| dec_sko (Const ("All",_) $ (xtp as Abs(a,T,p))) (n, thy) =
paulson@16012
   214
	    (*Universal quant: insert a free variable into body and continue*)
paulson@16009
   215
	    let val fname = variant (add_term_names (p,[])) a
paulson@16012
   216
	    in dec_sko (subst_bound (Free(fname,T), p)) (n, thy) end
paulson@16009
   217
	| dec_sko (Const ("op &", _) $ p $ q) nthy = 
paulson@16009
   218
	    dec_sko q (dec_sko p nthy)
paulson@16009
   219
	| dec_sko (Const ("op |", _) $ p $ q) nthy = 
paulson@16009
   220
	    dec_sko q (dec_sko p nthy)
paulson@16009
   221
	| dec_sko (Const ("Trueprop", _) $ p) nthy = 
paulson@16009
   222
	    dec_sko p nthy
paulson@16009
   223
	| dec_sko t (n,thy) = (n,thy) (*Do nothing otherwise*)
paulson@16009
   224
  in  #2 (dec_sko t (1,thy))  end;
paulson@16009
   225
paulson@16009
   226
(*cterms are used throughout for efficiency*)
paulson@16009
   227
val cTrueprop = Thm.cterm_of (Theory.sign_of HOL.thy) HOLogic.Trueprop;
paulson@16009
   228
paulson@16009
   229
(*cterm version of mk_cTrueprop*)
paulson@16009
   230
fun c_mkTrueprop A = Thm.capply cTrueprop A;
paulson@16009
   231
paulson@16009
   232
(*Given an abstraction over n variables, replace the bound variables by free
paulson@16009
   233
  ones. Return the body, along with the list of free variables.*)
paulson@16009
   234
fun c_variant_abs_multi (ct0, vars) = 
paulson@16009
   235
      let val (cv,ct) = Thm.dest_abs NONE ct0
paulson@16009
   236
      in  c_variant_abs_multi (ct, cv::vars)  end
paulson@16009
   237
      handle CTERM _ => (ct0, rev vars);
paulson@16009
   238
paulson@16009
   239
(*Given the definition of a Skolem function, return a theorem to replace 
paulson@16009
   240
  an existential formula by a use of that function.*)
paulson@16009
   241
fun skolem_of_def def =
paulson@16009
   242
  let val (c,rhs) = Drule.dest_equals (cprop_of (Drule.freeze_all def))
paulson@16009
   243
      val (ch, frees) = c_variant_abs_multi (rhs, [])
paulson@16009
   244
      val (chil,cabs) = Thm.dest_comb ch
paulson@16009
   245
      val {sign, t, ...} = rep_cterm chil
paulson@16009
   246
      val (Const ("Hilbert_Choice.Eps", Type("fun",[_,T]))) = t
paulson@16009
   247
      val cex = Thm.cterm_of sign (HOLogic.exists_const T)
paulson@16009
   248
      val ex_tm = c_mkTrueprop (Thm.capply cex cabs)
paulson@16009
   249
      and conc =  c_mkTrueprop (Drule.beta_conv cabs (Drule.list_comb(c,frees)));
paulson@16009
   250
  in  prove_goalw_cterm [def] (Drule.mk_implies (ex_tm, conc))
paulson@16009
   251
	    (fn [prem] => [ rtac (prem RS someI_ex) 1 ])
paulson@16009
   252
  end;	 
paulson@16009
   253
paulson@16009
   254
paulson@16009
   255
(*Converts an Isabelle theorem (intro, elim or simp format) into nnf.*)
paulson@16009
   256
fun to_nnf thy th = 
paulson@16009
   257
    if Term.is_first_order (prop_of th) then
paulson@16009
   258
      th |> Thm.transfer thy |> transform_elim |> Drule.freeze_all
paulson@16009
   259
	 |> ObjectLogic.atomize_thm |> make_nnf
paulson@16009
   260
    else raise THM ("to_nnf: not first-order", 0, [th]);
paulson@16009
   261
paulson@16009
   262
(*The cache prevents repeated clausification of a theorem, 
paulson@16009
   263
  and also repeated declaration of Skolem functions*)  
paulson@15955
   264
val clause_cache = ref (Symtab.empty : (thm * thm list) Symtab.table)
paulson@15955
   265
paulson@16009
   266
(*Declare Skolem functions for a theorem, supplied in nnf and with its name*)
paulson@16009
   267
fun skolem thy (name,th) =
paulson@16009
   268
  let val cname = (case name of
paulson@16009
   269
                         "" => gensym "sko" | s => Sign.base_name s)
paulson@16009
   270
      val thy' = declare_skofuns cname (#prop (rep_thm th)) thy
paulson@16009
   271
  in (map (skolem_of_def o #2) (axioms_of thy'), thy') end;
paulson@16009
   272
paulson@16009
   273
(*Populate the clause cache using the supplied theorems*)
paulson@16009
   274
fun skolemlist [] thy = thy
paulson@16009
   275
  | skolemlist ((name,th)::nths) thy = 
paulson@16009
   276
      (case Symtab.lookup (!clause_cache,name) of
paulson@16009
   277
	  NONE => 
paulson@16009
   278
	    let val nnfth = to_nnf thy th
paulson@16009
   279
		val (skoths,thy') = skolem thy (name, nnfth)
paulson@16009
   280
		val cls = Meson.make_cnf skoths nnfth
paulson@16009
   281
	    in  clause_cache := Symtab.update ((name, (th,cls)), !clause_cache);
paulson@16009
   282
		skolemlist nths thy'
paulson@16009
   283
	    end
paulson@16009
   284
	| SOME _ => skolemlist nths thy) (*FIXME: check for duplicate names?*)
paulson@16009
   285
      handle THM _ => skolemlist nths thy;
paulson@16009
   286
paulson@16009
   287
(*Exported function to convert Isabelle theorems into axiom clauses*) 
paulson@15956
   288
fun cnf_axiom (name,th) =
paulson@15956
   289
    case name of
paulson@15955
   290
	  "" => cnf_axiom_aux th (*no name, so can't cache*)
paulson@15955
   291
	| s  => case Symtab.lookup (!clause_cache,s) of
paulson@15955
   292
	  	  NONE => 
paulson@15955
   293
		    let val cls = cnf_axiom_aux th
paulson@15955
   294
		    in  clause_cache := Symtab.update ((s, (th,cls)), !clause_cache); cls
paulson@15955
   295
		    end
paulson@15955
   296
	        | SOME(th',cls) =>
paulson@15955
   297
		    if eq_thm(th,th') then cls
paulson@15955
   298
		    else (*New theorem stored under the same name? Possible??*)
paulson@15955
   299
		      let val cls = cnf_axiom_aux th
paulson@15955
   300
		      in  clause_cache := Symtab.update ((s, (th,cls)), !clause_cache); cls
paulson@15955
   301
		      end;
paulson@15347
   302
paulson@15956
   303
fun pairname th = (Thm.name_of_thm th, th);
paulson@15956
   304
paulson@15956
   305
fun meta_cnf_axiom th = 
paulson@15956
   306
    map Meson.make_meta_clause (cnf_axiom (pairname th));
paulson@15499
   307
paulson@15347
   308
paulson@15347
   309
(* changed: with one extra case added *)
paulson@15956
   310
fun univ_vars_of_aux (Const ("Hilbert_Choice.Eps",_) $ Abs(_,_,body)) vars =    
paulson@15956
   311
      univ_vars_of_aux body vars
paulson@15956
   312
  | univ_vars_of_aux (Const ("Ex",_) $ Abs(_,_,body)) vars = 
paulson@15956
   313
      univ_vars_of_aux body vars (* EX x. body *)
paulson@15347
   314
  | univ_vars_of_aux (P $ Q) vars =
paulson@15956
   315
      univ_vars_of_aux Q (univ_vars_of_aux P vars)
paulson@15347
   316
  | univ_vars_of_aux (t as Var(_,_)) vars = 
paulson@15956
   317
      if (t mem vars) then vars else (t::vars)
paulson@15347
   318
  | univ_vars_of_aux _ vars = vars;
paulson@15347
   319
  
paulson@15347
   320
fun univ_vars_of t = univ_vars_of_aux t [];
paulson@15347
   321
paulson@15347
   322
paulson@15347
   323
fun get_new_skolem epss (t as (Const ("Hilbert_Choice.Eps",_) $ Abs(_,tp,_)))  = 
paulson@15347
   324
    let val all_vars = univ_vars_of t
paulson@15347
   325
	val sk_term = ResSkolemFunction.gen_skolem all_vars tp
paulson@15347
   326
    in
paulson@15347
   327
	(sk_term,(t,sk_term)::epss)
paulson@15347
   328
    end;
paulson@15347
   329
paulson@15347
   330
skalberg@15531
   331
fun sk_lookup [] t = NONE
skalberg@15531
   332
  | sk_lookup ((tm,sk_tm)::tms) t = if (t = tm) then SOME (sk_tm) else (sk_lookup tms t);
paulson@15347
   333
paulson@15347
   334
paulson@15390
   335
paulson@15390
   336
(* get the proper skolem term to replace epsilon term *)
paulson@15347
   337
fun get_skolem epss t = 
paulson@15956
   338
    case (sk_lookup epss t) of NONE => get_new_skolem epss t
paulson@15956
   339
		             | SOME sk => (sk,epss);
paulson@15347
   340
paulson@15347
   341
paulson@16009
   342
fun rm_Eps_cls_aux epss (t as (Const ("Hilbert_Choice.Eps",_) $ Abs(_,_,_))) = 
paulson@16009
   343
       get_skolem epss t
paulson@15347
   344
  | rm_Eps_cls_aux epss (P $ Q) =
paulson@16009
   345
       let val (P',epss') = rm_Eps_cls_aux epss P
paulson@16009
   346
	   val (Q',epss'') = rm_Eps_cls_aux epss' Q
paulson@16009
   347
       in (P' $ Q',epss'') end
paulson@15347
   348
  | rm_Eps_cls_aux epss t = (t,epss);
paulson@15347
   349
paulson@15347
   350
paulson@16009
   351
fun rm_Eps_cls epss th = rm_Eps_cls_aux epss (prop_of th);
paulson@15347
   352
paulson@15347
   353
paulson@15390
   354
(* remove the epsilon terms in a formula, by skolem terms. *)
paulson@15347
   355
fun rm_Eps _ [] = []
paulson@16009
   356
  | rm_Eps epss (th::thms) = 
paulson@16009
   357
      let val (th',epss') = rm_Eps_cls epss th
paulson@16009
   358
      in th' :: (rm_Eps epss' thms) end;
paulson@15347
   359
paulson@15347
   360
paulson@15347
   361
paulson@15872
   362
(**** Extract and Clausify theorems from a theory's claset and simpset ****)
paulson@15347
   363
paulson@15347
   364
fun claset_rules_of_thy thy =
paulson@15347
   365
    let val clsset = rep_cs (claset_of thy)
paulson@15347
   366
	val safeEs = #safeEs clsset
paulson@15347
   367
	val safeIs = #safeIs clsset
paulson@15347
   368
	val hazEs = #hazEs clsset
paulson@15347
   369
	val hazIs = #hazIs clsset
paulson@15347
   370
    in
paulson@15956
   371
	map pairname (safeEs @ safeIs @ hazEs @ hazIs)
paulson@15347
   372
    end;
paulson@15347
   373
paulson@15347
   374
fun simpset_rules_of_thy thy =
paulson@15872
   375
    let val rules = #rules(fst (rep_ss (simpset_of thy)))
paulson@15347
   376
    in
paulson@15872
   377
	map (fn (_,r) => (#name r, #thm r)) (Net.dest rules)
paulson@15347
   378
    end;
paulson@15347
   379
paulson@15347
   380
paulson@15872
   381
(**** Translate a set of classical/simplifier rules into CNF (still as type "thm")  ****)
paulson@15347
   382
paulson@15347
   383
(* classical rules *)
paulson@15872
   384
fun cnf_rules [] err_list = ([],err_list)
paulson@16009
   385
  | cnf_rules ((name,th) :: thms) err_list = 
paulson@15872
   386
      let val (ts,es) = cnf_rules thms err_list
paulson@16009
   387
      in  (cnf_axiom (name,th) :: ts,es) handle  _ => (ts, (th::es))  end;
paulson@15347
   388
paulson@15347
   389
(* CNF all rules from a given theory's classical reasoner *)
paulson@15347
   390
fun cnf_classical_rules_thy thy = 
paulson@15872
   391
    cnf_rules (claset_rules_of_thy thy) [];
paulson@15347
   392
paulson@15347
   393
(* CNF all simplifier rules from a given theory's simpset *)
paulson@15347
   394
fun cnf_simpset_rules_thy thy =
paulson@15956
   395
    cnf_rules (simpset_rules_of_thy thy) [];
paulson@15347
   396
paulson@15347
   397
paulson@15872
   398
(**** Convert all theorems of a claset/simpset into clauses (ResClause.clause) ****)
paulson@15347
   399
quigley@16039
   400
(* outputs a list of (thm,clause) pairs *)
quigley@16039
   401
fun clausify_axiom_pairs (thm_name,thm) =
quigley@16039
   402
    let val isa_clauses = cnf_axiom (thm_name,thm) (*"isa_clauses" are already "standard"ed. *)
quigley@16039
   403
        val isa_clauses' = rm_Eps [] isa_clauses
quigley@16039
   404
        val clauses_n = length isa_clauses
quigley@16039
   405
	fun make_axiom_clauses _ [] []= []
quigley@16039
   406
	  | make_axiom_clauses i (cls::clss) (cls'::clss')= ((ResClause.make_axiom_clause cls (thm_name,i)),cls') :: make_axiom_clauses (i+1) clss clss'
paulson@15347
   407
    in
quigley@16039
   408
	make_axiom_clauses 0 isa_clauses' isa_clauses		
paulson@15347
   409
    end;
paulson@15347
   410
paulson@15347
   411
quigley@16039
   412
fun clausify_rules_pairs [] err_list = ([],err_list)
quigley@16039
   413
  | clausify_rules_pairs ((name,thm)::thms) err_list =
quigley@16039
   414
    let val (ts,es) = clausify_rules_pairs thms err_list
quigley@16039
   415
    in
quigley@16039
   416
	((clausify_axiom_pairs (name,thm))::ts,es) handle  _ => (ts,(thm::es))
quigley@16039
   417
    end;
quigley@16039
   418
(* classical rules *)
quigley@16039
   419
paulson@15347
   420
paulson@16009
   421
(*Setup function: takes a theory and installs ALL simprules and claset rules 
paulson@16009
   422
  into the clause cache*)
paulson@16009
   423
fun clause_cache_setup thy =
paulson@16009
   424
  let val simps = simpset_rules_of_thy thy
paulson@16009
   425
      and clas  = claset_rules_of_thy thy
paulson@16009
   426
  in skolemlist clas (skolemlist simps thy) end;
paulson@16009
   427
  
paulson@16009
   428
val setup = [clause_cache_setup];  
paulson@15347
   429
paulson@15347
   430
end;