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