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