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