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