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