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