src/HOL/Tools/res_axioms.ML
author wenzelm
Tue Jul 11 12:16:54 2006 +0200 (2006-07-11)
changeset 20071 8f3e1ddb50e6
parent 20017 a2070352371c
child 20292 6f2b8ed987ec
permissions -rw-r--r--
replaced Term.variant(list) by Name.variant(_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
  val elimRule_tac : thm -> Tactical.tactic
paulson@16012
    11
  val elimR2Fol : thm -> term
paulson@15997
    12
  val transform_elim : thm -> thm
paulson@15997
    13
  val cnf_axiom : (string * thm) -> thm list
paulson@15997
    14
  val meta_cnf_axiom : thm -> thm list
paulson@15997
    15
  val claset_rules_of_thy : theory -> (string * thm) list
paulson@15997
    16
  val simpset_rules_of_thy : theory -> (string * thm) list
paulson@17484
    17
  val claset_rules_of_ctxt: Proof.context -> (string * thm) list
paulson@17484
    18
  val simpset_rules_of_ctxt : Proof.context -> (string * thm) list
mengj@17905
    19
  val pairname : thm -> (string * thm)
paulson@18510
    20
  val skolem_thm : thm -> thm list
mengj@19353
    21
  val cnf_rules_pairs : (string * Thm.thm) list -> (Thm.thm * (string * int)) list list;
wenzelm@18708
    22
  val meson_method_setup : theory -> theory
wenzelm@18708
    23
  val setup : theory -> theory
mengj@19196
    24
mengj@19196
    25
  val atpset_rules_of_thy : theory -> (string * thm) list
mengj@19196
    26
  val atpset_rules_of_ctxt : Proof.context -> (string * thm) list
paulson@15997
    27
  end;
paulson@15347
    28
mengj@18198
    29
structure ResAxioms : RES_AXIOMS =
paulson@15997
    30
 
paulson@15997
    31
struct
paulson@15347
    32
mengj@18000
    33
paulson@15997
    34
(**** Transformation of Elimination Rules into First-Order Formulas****)
paulson@15347
    35
paulson@15390
    36
(* a tactic used to prove an elim-rule. *)
paulson@16009
    37
fun elimRule_tac th =
paulson@20017
    38
    (resolve_tac [impI,notI] 1) THEN (etac th 1) THEN REPEAT(blast_tac HOL_cs 1);
paulson@15347
    39
paulson@15956
    40
fun add_EX tm [] = tm
paulson@15956
    41
  | add_EX tm ((x,xtp)::xs) = add_EX (HOLogic.exists_const xtp $ Abs(x,xtp,tm)) xs;
paulson@15347
    42
paulson@19894
    43
(*Checks for the premise ~P when the conclusion is P.*)
paulson@19894
    44
fun is_neg (Const("Trueprop",_) $ (Const("Not",_) $ Free(p,_))) 
paulson@19894
    45
           (Const("Trueprop",_) $ Free(q,_)) = (p = q)
paulson@15371
    46
  | is_neg _ _ = false;
paulson@15371
    47
paulson@20017
    48
exception ELIMR2FOL;
paulson@20017
    49
paulson@20017
    50
(*Handles the case where the dummy "conclusion" variable appears negated in the
paulson@20017
    51
  premises, so the final consequent must be kept.*)
paulson@15371
    52
fun strip_concl' prems bvs (Const ("==>",_) $ P $ Q) =
paulson@19894
    53
      strip_concl' (HOLogic.dest_Trueprop P :: prems) bvs  Q
paulson@15371
    54
  | strip_concl' prems bvs P = 
paulson@15956
    55
      let val P' = HOLogic.Not $ (HOLogic.dest_Trueprop P)
paulson@19894
    56
      in add_EX (foldr1 HOLogic.mk_conj (P'::prems)) bvs end;
paulson@15371
    57
paulson@20017
    58
(*Recurrsion over the minor premise of an elimination rule. Final consequent
paulson@20017
    59
  is ignored, as it is the dummy "conclusion" variable.*)
paulson@18141
    60
fun strip_concl prems bvs concl (Const ("all", _) $ Abs (x,xtp,body)) = 
paulson@18141
    61
      strip_concl prems ((x,xtp)::bvs) concl body
paulson@15371
    62
  | strip_concl prems bvs concl (Const ("==>",_) $ P $ Q) =
paulson@18141
    63
      if (is_neg P concl) then (strip_concl' prems bvs Q)
paulson@18141
    64
      else strip_concl (HOLogic.dest_Trueprop P::prems) bvs  concl Q
paulson@20017
    65
  | strip_concl prems bvs concl Q = 
paulson@20017
    66
      if concl aconv Q then add_EX (foldr1 HOLogic.mk_conj prems) bvs
paulson@20017
    67
      else raise ELIMR2FOL (*expected conclusion not found!*)
paulson@15347
    68
 
paulson@20017
    69
fun trans_elim (major,[],_) = HOLogic.Not $ major
paulson@20017
    70
  | trans_elim (major,minors,concl) =
paulson@20017
    71
      let val disjs = foldr1 HOLogic.mk_disj (map (strip_concl [] [] concl) minors)
paulson@20017
    72
      in  HOLogic.mk_imp (major, disjs)  end;
paulson@15347
    73
paulson@16012
    74
(* convert an elim rule into an equivalent formula, of type term. *)
paulson@15347
    75
fun elimR2Fol elimR = 
paulson@19894
    76
  let val elimR' = Drule.freeze_all elimR
paulson@19894
    77
      val (prems,concl) = (prems_of elimR', concl_of elimR')
paulson@20017
    78
      val cv = case concl of    (*conclusion variable*)
paulson@20017
    79
		  Const("Trueprop",_) $ (v as Free(_,Type("bool",[]))) => v
paulson@20017
    80
		| v as Free(_, Type("prop",[])) => v
paulson@20017
    81
		| _ => raise ELIMR2FOL
paulson@20017
    82
  in case prems of
paulson@20017
    83
      [] => raise ELIMR2FOL
paulson@20017
    84
    | (Const("Trueprop",_) $ major) :: minors => 
paulson@20017
    85
        if member (op aconv) (term_frees major) cv then raise ELIMR2FOL
paulson@20017
    86
        else (trans_elim (major, minors, concl) handle TERM _ => raise ELIMR2FOL)
paulson@20017
    87
    | _ => raise ELIMR2FOL
paulson@20017
    88
  end;
paulson@15347
    89
paulson@15997
    90
(* convert an elim-rule into an equivalent theorem that does not have the 
paulson@15997
    91
   predicate variable.  Leave other theorems unchanged.*) 
paulson@16009
    92
fun transform_elim th =
paulson@20017
    93
    let val ctm = cterm_of (sign_of_thm th) (HOLogic.mk_Trueprop (elimR2Fol th))
paulson@18009
    94
    in Goal.prove_raw [] ctm (fn _ => elimRule_tac th) end
paulson@20017
    95
    handle ELIMR2FOL => th (*not an elimination rule*)
paulson@20017
    96
         | exn => (warning ("transform_elim failed: " ^ Toplevel.exn_message exn ^ 
paulson@20017
    97
                            " for theorem " ^ string_of_thm th); th) 
paulson@20017
    98
paulson@15997
    99
paulson@15997
   100
paulson@15997
   101
(**** Transformation of Clasets and Simpsets into First-Order Axioms ****)
paulson@15997
   102
paulson@15347
   103
paulson@16563
   104
(*Transfer a theorem into theory Reconstruction.thy if it is not already
paulson@15359
   105
  inside that theory -- because it's needed for Skolemization *)
paulson@15359
   106
paulson@16563
   107
(*This will refer to the final version of theory Reconstruction.*)
paulson@16563
   108
val recon_thy_ref = Theory.self_ref (the_context ());  
paulson@15359
   109
paulson@16563
   110
(*If called while Reconstruction is being created, it will transfer to the
paulson@16563
   111
  current version. If called afterward, it will transfer to the final version.*)
paulson@16009
   112
fun transfer_to_Reconstruction th =
paulson@16563
   113
    transfer (Theory.deref recon_thy_ref) th handle THM _ => th;
paulson@15347
   114
paulson@15955
   115
fun is_taut th =
paulson@15955
   116
      case (prop_of th) of
paulson@15955
   117
           (Const ("Trueprop", _) $ Const ("True", _)) => true
paulson@15955
   118
         | _ => false;
paulson@15955
   119
paulson@15955
   120
(* remove tautologous clauses *)
paulson@15955
   121
val rm_redundant_cls = List.filter (not o is_taut);
paulson@18141
   122
     
paulson@15997
   123
       
paulson@16009
   124
(**** SKOLEMIZATION BY INFERENCE (lcp) ****)
paulson@16009
   125
paulson@18141
   126
(*Traverse a theorem, declaring Skolem function definitions. String s is the suggested
paulson@18141
   127
  prefix for the Skolem constant. Result is a new theory*)
paulson@18141
   128
fun declare_skofuns s th thy =
paulson@17404
   129
  let fun dec_sko (Const ("Ex",_) $ (xtp as Abs(_,T,p))) (n, (thy, axs)) =
paulson@16009
   130
	    (*Existential: declare a Skolem function, then insert into body and continue*)
paulson@16009
   131
	    let val cname = s ^ "_" ^ Int.toString n
paulson@16012
   132
		val args = term_frees xtp  (*get the formal parameter list*)
paulson@16009
   133
		val Ts = map type_of args
paulson@16009
   134
		val cT = Ts ---> T
paulson@18141
   135
		val c = Const (Sign.full_name thy cname, cT)
paulson@16009
   136
		val rhs = list_abs_free (map dest_Free args, HOLogic.choice_const T $ xtp)
paulson@16012
   137
		        (*Forms a lambda-abstraction over the formal parameters*)
paulson@16009
   138
		val def = equals cT $ c $ rhs
paulson@16009
   139
		val thy' = Theory.add_consts_i [(cname, cT, NoSyn)] thy
paulson@16012
   140
		           (*Theory is augmented with the constant, then its def*)
paulson@17404
   141
		val cdef = cname ^ "_def"
wenzelm@19630
   142
		val thy'' = Theory.add_defs_i false false [(cdef, def)] thy'
paulson@17404
   143
	    in dec_sko (subst_bound (list_comb(c,args), p)) 
paulson@17404
   144
	               (n+1, (thy'', get_axiom thy'' cdef :: axs)) 
paulson@17404
   145
	    end
paulson@17404
   146
	| dec_sko (Const ("All",_) $ (xtp as Abs(a,T,p))) (n, thx) =
paulson@16012
   147
	    (*Universal quant: insert a free variable into body and continue*)
wenzelm@20071
   148
	    let val fname = Name.variant (add_term_names (p,[])) a
paulson@17404
   149
	    in dec_sko (subst_bound (Free(fname,T), p)) (n, thx) end
paulson@18141
   150
	| dec_sko (Const ("op &", _) $ p $ q) nthy = dec_sko q (dec_sko p nthy)
paulson@18141
   151
	| dec_sko (Const ("op |", _) $ p $ q) nthy = dec_sko q (dec_sko p nthy)
paulson@18141
   152
	| dec_sko (Const ("Trueprop", _) $ p) nthy = dec_sko p nthy
paulson@17404
   153
	| dec_sko t nthx = nthx (*Do nothing otherwise*)
paulson@18141
   154
  in  #2 (dec_sko (#prop (rep_thm th)) (1, (thy,[])))  end;
paulson@18141
   155
paulson@18141
   156
(*Traverse a theorem, accumulating Skolem function definitions.*)
paulson@18141
   157
fun assume_skofuns th =
paulson@18141
   158
  let fun dec_sko (Const ("Ex",_) $ (xtp as Abs(_,T,p))) defs =
paulson@18141
   159
	    (*Existential: declare a Skolem function, then insert into body and continue*)
wenzelm@20071
   160
	    let val name = Name.variant (add_term_names (p,[])) (gensym "sko_")
paulson@18141
   161
                val skos = map (#1 o Logic.dest_equals) defs  (*existing sko fns*)
paulson@18141
   162
		val args = term_frees xtp \\ skos  (*the formal parameters*)
paulson@18141
   163
		val Ts = map type_of args
paulson@18141
   164
		val cT = Ts ---> T
paulson@18141
   165
		val c = Free (name, cT)
paulson@18141
   166
		val rhs = list_abs_free (map dest_Free args,        
paulson@18141
   167
		                         HOLogic.choice_const T $ xtp)
paulson@18141
   168
		      (*Forms a lambda-abstraction over the formal parameters*)
paulson@18141
   169
		val def = equals cT $ c $ rhs
paulson@18141
   170
	    in dec_sko (subst_bound (list_comb(c,args), p)) 
paulson@18141
   171
	               (def :: defs)
paulson@18141
   172
	    end
paulson@18141
   173
	| dec_sko (Const ("All",_) $ (xtp as Abs(a,T,p))) defs =
paulson@18141
   174
	    (*Universal quant: insert a free variable into body and continue*)
wenzelm@20071
   175
	    let val fname = Name.variant (add_term_names (p,[])) a
paulson@18141
   176
	    in dec_sko (subst_bound (Free(fname,T), p)) defs end
paulson@18141
   177
	| dec_sko (Const ("op &", _) $ p $ q) defs = dec_sko q (dec_sko p defs)
paulson@18141
   178
	| dec_sko (Const ("op |", _) $ p $ q) defs = dec_sko q (dec_sko p defs)
paulson@18141
   179
	| dec_sko (Const ("Trueprop", _) $ p) defs = dec_sko p defs
paulson@18141
   180
	| dec_sko t defs = defs (*Do nothing otherwise*)
paulson@18141
   181
  in  dec_sko (#prop (rep_thm th)) []  end;
paulson@16009
   182
paulson@16009
   183
(*cterms are used throughout for efficiency*)
paulson@18141
   184
val cTrueprop = Thm.cterm_of HOL.thy HOLogic.Trueprop;
paulson@16009
   185
paulson@16009
   186
(*cterm version of mk_cTrueprop*)
paulson@16009
   187
fun c_mkTrueprop A = Thm.capply cTrueprop A;
paulson@16009
   188
paulson@16009
   189
(*Given an abstraction over n variables, replace the bound variables by free
paulson@16009
   190
  ones. Return the body, along with the list of free variables.*)
paulson@16009
   191
fun c_variant_abs_multi (ct0, vars) = 
paulson@16009
   192
      let val (cv,ct) = Thm.dest_abs NONE ct0
paulson@16009
   193
      in  c_variant_abs_multi (ct, cv::vars)  end
paulson@16009
   194
      handle CTERM _ => (ct0, rev vars);
paulson@16009
   195
paulson@16009
   196
(*Given the definition of a Skolem function, return a theorem to replace 
paulson@18141
   197
  an existential formula by a use of that function. 
paulson@18141
   198
   Example: "EX x. x : A & x ~: B ==> sko A B : A & sko A B ~: B"  [.] *)
paulson@16588
   199
fun skolem_of_def def =  
paulson@16009
   200
  let val (c,rhs) = Drule.dest_equals (cprop_of (Drule.freeze_all def))
paulson@16009
   201
      val (ch, frees) = c_variant_abs_multi (rhs, [])
paulson@18141
   202
      val (chilbert,cabs) = Thm.dest_comb ch
paulson@18141
   203
      val {sign,t, ...} = rep_cterm chilbert
paulson@18141
   204
      val T = case t of Const ("Hilbert_Choice.Eps", Type("fun",[_,T])) => T
paulson@18141
   205
                      | _ => raise THM ("skolem_of_def: expected Eps", 0, [def])
paulson@16009
   206
      val cex = Thm.cterm_of sign (HOLogic.exists_const T)
paulson@16009
   207
      val ex_tm = c_mkTrueprop (Thm.capply cex cabs)
paulson@16009
   208
      and conc =  c_mkTrueprop (Drule.beta_conv cabs (Drule.list_comb(c,frees)));
paulson@18141
   209
      fun tacf [prem] = rewrite_goals_tac [def] THEN rtac (prem RS someI_ex) 1
paulson@18141
   210
  in  Goal.prove_raw [ex_tm] conc tacf 
paulson@18141
   211
       |> forall_intr_list frees
paulson@18141
   212
       |> forall_elim_vars 0  (*Introduce Vars, but don't discharge defs.*)
paulson@18141
   213
       |> Thm.varifyT
paulson@18141
   214
  end;
paulson@16009
   215
mengj@18198
   216
(*Converts an Isabelle theorem (intro, elim or simp format) into nnf.*)
mengj@18198
   217
(*It now works for HOL too. *)
paulson@18141
   218
fun to_nnf th = 
paulson@18141
   219
    th |> transfer_to_Reconstruction
paulson@16588
   220
       |> transform_elim |> Drule.freeze_all
paulson@16588
   221
       |> ObjectLogic.atomize_thm |> make_nnf;
paulson@16009
   222
paulson@16009
   223
(*The cache prevents repeated clausification of a theorem, 
paulson@18510
   224
  and also repeated declaration of Skolem functions*)  
paulson@18510
   225
  (* FIXME better use Termtab!? No, we MUST use theory data!!*)
paulson@15955
   226
val clause_cache = ref (Symtab.empty : (thm * thm list) Symtab.table)
paulson@15955
   227
paulson@18141
   228
paulson@18141
   229
(*Generate Skolem functions for a theorem supplied in nnf*)
paulson@18141
   230
fun skolem_of_nnf th =
paulson@18141
   231
  map (skolem_of_def o assume o (cterm_of (theory_of_thm th))) (assume_skofuns th);
paulson@18141
   232
paulson@18510
   233
(*Skolemize a named theorem, with Skolem functions as additional premises.*)
mengj@18198
   234
(*also works for HOL*) 
paulson@18141
   235
fun skolem_thm th = 
paulson@18510
   236
  let val nnfth = to_nnf th
paulson@19175
   237
  in  rm_redundant_cls (Meson.make_cnf (skolem_of_nnf nnfth) nnfth)
paulson@18510
   238
  end
paulson@18510
   239
  handle THM _ => [];
paulson@18141
   240
paulson@18510
   241
(*Declare Skolem functions for a theorem, supplied in nnf and with its name.
paulson@18510
   242
  It returns a modified theory, unless skolemization fails.*)
paulson@16009
   243
fun skolem thy (name,th) =
paulson@16588
   244
  let val cname = (case name of "" => gensym "sko" | s => Sign.base_name s)
paulson@18141
   245
  in Option.map 
paulson@18141
   246
        (fn nnfth => 
paulson@18141
   247
          let val (thy',defs) = declare_skofuns cname nnfth thy
paulson@18141
   248
              val skoths = map skolem_of_def defs
paulson@19175
   249
          in (thy', rm_redundant_cls (Meson.make_cnf skoths nnfth)) end)
mengj@18198
   250
      (SOME (to_nnf th)  handle THM _ => NONE) 
paulson@18141
   251
  end;
paulson@16009
   252
paulson@18510
   253
(*Populate the clause cache using the supplied theorem. Return the clausal form
paulson@18510
   254
  and modified theory.*)
paulson@18510
   255
fun skolem_cache_thm ((name,th), thy) = 
paulson@18144
   256
  case Symtab.lookup (!clause_cache) name of
paulson@18144
   257
      NONE => 
paulson@18144
   258
	(case skolem thy (name, Thm.transfer thy th) of
paulson@18510
   259
	     NONE => ([th],thy)
paulson@18144
   260
	   | SOME (thy',cls) => 
paulson@18510
   261
	       (change clause_cache (Symtab.update (name, (th, cls))); (cls,thy')))
paulson@18144
   262
    | SOME (th',cls) =>
paulson@18510
   263
        if eq_thm(th,th') then (cls,thy)
paulson@19232
   264
	else (Output.debug ("skolem_cache: Ignoring variant of theorem " ^ name); 
paulson@19232
   265
	      Output.debug (string_of_thm th);
paulson@19232
   266
	      Output.debug (string_of_thm th');
paulson@18510
   267
	      ([th],thy));
paulson@18510
   268
	      
paulson@18510
   269
fun skolem_cache ((name,th), thy) = #2 (skolem_cache_thm ((name,th), thy));
paulson@18141
   270
paulson@16009
   271
paulson@16009
   272
(*Exported function to convert Isabelle theorems into axiom clauses*) 
paulson@19894
   273
fun cnf_axiom (name,th) =
paulson@18144
   274
  case name of
paulson@19894
   275
	"" => skolem_thm th (*no name, so can't cache*)
paulson@18144
   276
      | s  => case Symtab.lookup (!clause_cache) s of
paulson@18144
   277
		NONE => 
paulson@19894
   278
		  let val cls = skolem_thm th
paulson@18144
   279
		  in change clause_cache (Symtab.update (s, (th, cls))); cls end
paulson@18144
   280
	      | SOME(th',cls) =>
paulson@18144
   281
		  if eq_thm(th,th') then cls
paulson@19232
   282
		  else (Output.debug ("cnf_axiom: duplicate or variant of theorem " ^ name); 
paulson@19232
   283
		        Output.debug (string_of_thm th);
paulson@19232
   284
		        Output.debug (string_of_thm th');
paulson@18144
   285
		        cls);
paulson@15347
   286
paulson@18141
   287
fun pairname th = (Thm.name_of_thm th, th);
paulson@18141
   288
paulson@15956
   289
fun meta_cnf_axiom th = 
paulson@15956
   290
    map Meson.make_meta_clause (cnf_axiom (pairname th));
paulson@15499
   291
paulson@15347
   292
paulson@15872
   293
(**** Extract and Clausify theorems from a theory's claset and simpset ****)
paulson@15347
   294
paulson@17404
   295
(*Preserve the name of "th" after the transformation "f"*)
paulson@17404
   296
fun preserve_name f th = Thm.name_thm (Thm.name_of_thm th, f th);
paulson@17404
   297
paulson@17484
   298
fun rules_of_claset cs =
paulson@17484
   299
  let val {safeIs,safeEs,hazIs,hazEs,...} = rep_cs cs
paulson@19175
   300
      val intros = safeIs @ hazIs
wenzelm@18532
   301
      val elims  = map Classical.classical_rule (safeEs @ hazEs)
paulson@17404
   302
  in
wenzelm@18680
   303
     Output.debug ("rules_of_claset intros: " ^ Int.toString(length intros) ^ 
paulson@17484
   304
            " elims: " ^ Int.toString(length elims));
paulson@20017
   305
     map pairname (intros @ elims)
paulson@17404
   306
  end;
paulson@15347
   307
paulson@17484
   308
fun rules_of_simpset ss =
paulson@17484
   309
  let val ({rules,...}, _) = rep_ss ss
paulson@17484
   310
      val simps = Net.entries rules
paulson@17484
   311
  in 
wenzelm@18680
   312
      Output.debug ("rules_of_simpset: " ^ Int.toString(length simps));
paulson@17484
   313
      map (fn r => (#name r, #thm r)) simps
paulson@17484
   314
  end;
paulson@17484
   315
paulson@17484
   316
fun claset_rules_of_thy thy = rules_of_claset (claset_of thy);
paulson@17484
   317
fun simpset_rules_of_thy thy = rules_of_simpset (simpset_of thy);
paulson@17484
   318
mengj@19196
   319
fun atpset_rules_of_thy thy = map pairname (ResAtpSet.atp_rules_of_thy thy);
mengj@19196
   320
mengj@19196
   321
paulson@17484
   322
fun claset_rules_of_ctxt ctxt = rules_of_claset (local_claset_of ctxt);
paulson@17484
   323
fun simpset_rules_of_ctxt ctxt = rules_of_simpset (local_simpset_of ctxt);
paulson@15347
   324
mengj@19196
   325
fun atpset_rules_of_ctxt ctxt = map pairname (ResAtpSet.atp_rules_of_ctxt ctxt);
paulson@15347
   326
paulson@15872
   327
(**** Translate a set of classical/simplifier rules into CNF (still as type "thm")  ****)
paulson@15347
   328
paulson@19894
   329
(* classical rules: works for both FOL and HOL *)
paulson@19894
   330
fun cnf_rules [] err_list = ([],err_list)
paulson@19894
   331
  | cnf_rules ((name,th) :: ths) err_list = 
paulson@19894
   332
      let val (ts,es) = cnf_rules ths err_list
paulson@17404
   333
      in  (cnf_axiom (name,th) :: ts,es) handle  _ => (ts, (th::es))  end;  
paulson@15347
   334
paulson@19894
   335
fun pair_name_cls k (n, []) = []
paulson@19894
   336
  | pair_name_cls k (n, cls::clss) = (cls, (n,k)) :: pair_name_cls (k+1) (n, clss)
paulson@19894
   337
 	    
paulson@19894
   338
fun cnf_rules_pairs_aux pairs [] = pairs
paulson@19894
   339
  | cnf_rules_pairs_aux pairs ((name,th)::ths) =
paulson@19894
   340
      let val pairs' = (pair_name_cls 0 (name, cnf_axiom(name,th))) :: pairs
paulson@19894
   341
		       handle THM _ => pairs | ResClause.CLAUSE _ => pairs
paulson@19894
   342
			    | ResHolClause.LAM2COMB _ => pairs
paulson@19894
   343
      in  cnf_rules_pairs_aux pairs' ths  end;
mengj@19353
   344
    
paulson@19894
   345
val cnf_rules_pairs = cnf_rules_pairs_aux [];
mengj@19353
   346
mengj@19196
   347
mengj@18198
   348
(**** Convert all theorems of a claset/simpset into clauses (ResClause.clause, or ResHolClause.clause) ****)
paulson@15347
   349
paulson@18510
   350
(*These should include any plausibly-useful theorems, especially if they need
paulson@18510
   351
  Skolem functions. FIXME: this list is VERY INCOMPLETE*)
paulson@18510
   352
val default_initial_thms = map pairname
paulson@18510
   353
  [refl_def, antisym_def, sym_def, trans_def, single_valued_def,
paulson@18510
   354
   subset_refl, Union_least, Inter_greatest];
paulson@18510
   355
paulson@16009
   356
(*Setup function: takes a theory and installs ALL simprules and claset rules 
paulson@16009
   357
  into the clause cache*)
paulson@16009
   358
fun clause_cache_setup thy =
paulson@16009
   359
  let val simps = simpset_rules_of_thy thy
paulson@16009
   360
      and clas  = claset_rules_of_thy thy
paulson@18510
   361
      and thy0  = List.foldl skolem_cache thy default_initial_thms
paulson@18510
   362
      val thy1  = List.foldl skolem_cache thy0 clas
paulson@18510
   363
  in List.foldl skolem_cache thy1 simps end;
paulson@18141
   364
(*Could be duplicate theorem names, due to multiple attributes*)
paulson@16009
   365
  
paulson@16563
   366
paulson@16563
   367
(*** meson proof methods ***)
paulson@16563
   368
paulson@16563
   369
fun cnf_rules_of_ths ths = List.concat (#1 (cnf_rules (map pairname ths) []));
paulson@16563
   370
paulson@16563
   371
fun meson_meth ths ctxt =
paulson@16563
   372
  Method.SIMPLE_METHOD' HEADGOAL
paulson@16563
   373
    (CHANGED_PROP o Meson.meson_claset_tac (cnf_rules_of_ths ths) (local_claset_of ctxt));
paulson@16563
   374
paulson@16563
   375
val meson_method_setup =
wenzelm@18708
   376
  Method.add_methods
wenzelm@18708
   377
    [("meson", Method.thms_ctxt_args meson_meth, 
wenzelm@18833
   378
      "MESON resolution proof procedure")];
paulson@15347
   379
paulson@18510
   380
paulson@18510
   381
paulson@18510
   382
(*** The Skolemization attribute ***)
paulson@18510
   383
paulson@18510
   384
fun conj2_rule (th1,th2) = conjI OF [th1,th2];
paulson@18510
   385
paulson@18510
   386
(*Conjoin a list of clauses to recreate a single theorem*)
paulson@18510
   387
val conj_rule = foldr1 conj2_rule;
paulson@18510
   388
wenzelm@18728
   389
fun skolem (Context.Theory thy, th) =
wenzelm@18728
   390
      let
wenzelm@18728
   391
        val name = Thm.name_of_thm th
wenzelm@18728
   392
        val (cls, thy') = skolem_cache_thm ((name, th), thy)
wenzelm@18728
   393
      in (Context.Theory thy', conj_rule cls) end
wenzelm@18728
   394
  | skolem (context, th) = (context, conj_rule (skolem_thm th));
paulson@18510
   395
paulson@18510
   396
val setup_attrs = Attrib.add_attributes
wenzelm@18728
   397
  [("skolem", Attrib.no_args skolem, "skolemization of a theorem")];
paulson@18510
   398
wenzelm@18708
   399
val setup = clause_cache_setup #> setup_attrs;
paulson@18510
   400
paulson@15347
   401
end;