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