src/HOL/Tools/res_axioms.ML
author wenzelm
Mon Oct 09 02:19:54 2006 +0200 (2006-10-09)
changeset 20902 a0034e545c13
parent 20867 e7b92a48e22b
child 20969 341808e0b7f2
permissions -rw-r--r--
replaced Drule.clhs/crhs_of by Drule.lhs/rhs_of;
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
wenzelm@20461
     5
Transformation of axiom rules (elim/intro/etc) into CNF forms.
paulson@15347
     6
*)
paulson@15347
     7
paulson@20445
     8
(*FIXME: does this signature serve any purpose?*)
paulson@15997
     9
signature RES_AXIOMS =
paulson@15997
    10
  sig
paulson@15997
    11
  val elimRule_tac : thm -> Tactical.tactic
paulson@16012
    12
  val elimR2Fol : thm -> term
paulson@15997
    13
  val transform_elim : thm -> thm
paulson@15997
    14
  val cnf_axiom : (string * thm) -> thm list
paulson@15997
    15
  val meta_cnf_axiom : thm -> thm list
paulson@15997
    16
  val claset_rules_of_thy : theory -> (string * thm) list
paulson@15997
    17
  val simpset_rules_of_thy : theory -> (string * thm) list
paulson@17484
    18
  val claset_rules_of_ctxt: Proof.context -> (string * thm) list
paulson@17484
    19
  val simpset_rules_of_ctxt : Proof.context -> (string * thm) list
mengj@17905
    20
  val pairname : thm -> (string * thm)
paulson@18510
    21
  val skolem_thm : thm -> thm list
paulson@20419
    22
  val to_nnf : thm -> thm
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;
wenzelm@20461
    30
paulson@20419
    31
structure ResAxioms =
wenzelm@20461
    32
paulson@15997
    33
struct
paulson@15347
    34
paulson@20419
    35
(*FIXME DELETE: For running the comparison between combinators and abstractions.
paulson@20419
    36
  CANNOT be a ref, as the setting is used while Isabelle is built.*)
paulson@20419
    37
val abstract_lambdas = true;
paulson@20419
    38
paulson@20419
    39
val trace_abs = ref false;
mengj@18000
    40
wenzelm@20902
    41
(* FIXME legacy *)
paulson@20863
    42
fun freeze_thm th = #1 (Drule.freeze_thaw th);
paulson@20863
    43
wenzelm@20902
    44
val lhs_of = #1 o Logic.dest_equals o Thm.prop_of;
wenzelm@20902
    45
val rhs_of = #2 o Logic.dest_equals o Thm.prop_of;
paulson@20863
    46
paulson@20863
    47
paulson@20445
    48
(*Store definitions of abstraction functions, ensuring that identical right-hand
paulson@20445
    49
  sides are denoted by the same functions and thereby reducing the need for
paulson@20445
    50
  extensionality in proofs.
paulson@20445
    51
  FIXME!  Store in theory data!!*)
paulson@20863
    52
paulson@20867
    53
(*Populate the abstraction cache with common combinators.*)
paulson@20863
    54
fun seed th net =
wenzelm@20902
    55
  let val (_,ct) = Thm.dest_abs NONE (Drule.rhs_of th)
paulson@20867
    56
      val t = Logic.legacy_varify (term_of ct)
paulson@20867
    57
  in  Net.insert_term eq_thm (t, th) net end;
paulson@20863
    58
  
paulson@20863
    59
val abstraction_cache = ref 
paulson@20867
    60
      (seed (thm"Reconstruction.I_simp") 
paulson@20867
    61
       (seed (thm"Reconstruction.B_simp") 
paulson@20867
    62
	(seed (thm"Reconstruction.K_simp") Net.empty)));
paulson@20867
    63
paulson@20445
    64
paulson@15997
    65
(**** Transformation of Elimination Rules into First-Order Formulas****)
paulson@15347
    66
paulson@15390
    67
(* a tactic used to prove an elim-rule. *)
paulson@16009
    68
fun elimRule_tac th =
paulson@20419
    69
    (resolve_tac [impI,notI] 1) THEN (etac th 1) THEN REPEAT(fast_tac HOL_cs 1);
paulson@15347
    70
paulson@15956
    71
fun add_EX tm [] = tm
paulson@15956
    72
  | add_EX tm ((x,xtp)::xs) = add_EX (HOLogic.exists_const xtp $ Abs(x,xtp,tm)) xs;
paulson@15347
    73
paulson@19894
    74
(*Checks for the premise ~P when the conclusion is P.*)
wenzelm@20461
    75
fun is_neg (Const("Trueprop",_) $ (Const("Not",_) $ Free(p,_)))
paulson@19894
    76
           (Const("Trueprop",_) $ Free(q,_)) = (p = q)
paulson@15371
    77
  | is_neg _ _ = false;
paulson@15371
    78
paulson@20017
    79
exception ELIMR2FOL;
paulson@20017
    80
paulson@20017
    81
(*Handles the case where the dummy "conclusion" variable appears negated in the
paulson@20017
    82
  premises, so the final consequent must be kept.*)
paulson@15371
    83
fun strip_concl' prems bvs (Const ("==>",_) $ P $ Q) =
paulson@19894
    84
      strip_concl' (HOLogic.dest_Trueprop P :: prems) bvs  Q
wenzelm@20461
    85
  | strip_concl' prems bvs P =
paulson@15956
    86
      let val P' = HOLogic.Not $ (HOLogic.dest_Trueprop P)
paulson@19894
    87
      in add_EX (foldr1 HOLogic.mk_conj (P'::prems)) bvs end;
paulson@15371
    88
paulson@20017
    89
(*Recurrsion over the minor premise of an elimination rule. Final consequent
paulson@20017
    90
  is ignored, as it is the dummy "conclusion" variable.*)
wenzelm@20461
    91
fun strip_concl prems bvs concl (Const ("all", _) $ Abs (x,xtp,body)) =
paulson@18141
    92
      strip_concl prems ((x,xtp)::bvs) concl body
paulson@15371
    93
  | strip_concl prems bvs concl (Const ("==>",_) $ P $ Q) =
paulson@18141
    94
      if (is_neg P concl) then (strip_concl' prems bvs Q)
paulson@18141
    95
      else strip_concl (HOLogic.dest_Trueprop P::prems) bvs  concl Q
wenzelm@20461
    96
  | strip_concl prems bvs concl Q =
paulson@20017
    97
      if concl aconv Q then add_EX (foldr1 HOLogic.mk_conj prems) bvs
paulson@20017
    98
      else raise ELIMR2FOL (*expected conclusion not found!*)
wenzelm@20461
    99
paulson@20017
   100
fun trans_elim (major,[],_) = HOLogic.Not $ major
paulson@20017
   101
  | trans_elim (major,minors,concl) =
paulson@20017
   102
      let val disjs = foldr1 HOLogic.mk_disj (map (strip_concl [] [] concl) minors)
paulson@20017
   103
      in  HOLogic.mk_imp (major, disjs)  end;
paulson@15347
   104
paulson@16012
   105
(* convert an elim rule into an equivalent formula, of type term. *)
wenzelm@20461
   106
fun elimR2Fol elimR =
paulson@20863
   107
  let val elimR' = freeze_thm elimR
paulson@19894
   108
      val (prems,concl) = (prems_of elimR', concl_of elimR')
paulson@20017
   109
      val cv = case concl of    (*conclusion variable*)
wenzelm@20461
   110
                  Const("Trueprop",_) $ (v as Free(_,Type("bool",[]))) => v
wenzelm@20461
   111
                | v as Free(_, Type("prop",[])) => v
wenzelm@20461
   112
                | _ => raise ELIMR2FOL
paulson@20017
   113
  in case prems of
paulson@20017
   114
      [] => raise ELIMR2FOL
wenzelm@20461
   115
    | (Const("Trueprop",_) $ major) :: minors =>
paulson@20017
   116
        if member (op aconv) (term_frees major) cv then raise ELIMR2FOL
paulson@20017
   117
        else (trans_elim (major, minors, concl) handle TERM _ => raise ELIMR2FOL)
paulson@20017
   118
    | _ => raise ELIMR2FOL
paulson@20017
   119
  end;
paulson@15347
   120
wenzelm@20461
   121
(* convert an elim-rule into an equivalent theorem that does not have the
wenzelm@20461
   122
   predicate variable.  Leave other theorems unchanged.*)
paulson@16009
   123
fun transform_elim th =
paulson@20017
   124
    let val ctm = cterm_of (sign_of_thm th) (HOLogic.mk_Trueprop (elimR2Fol th))
paulson@18009
   125
    in Goal.prove_raw [] ctm (fn _ => elimRule_tac th) end
paulson@20017
   126
    handle ELIMR2FOL => th (*not an elimination rule*)
wenzelm@20461
   127
         | exn => (warning ("transform_elim failed: " ^ Toplevel.exn_message exn ^
wenzelm@20461
   128
                            " for theorem " ^ string_of_thm th); th)
paulson@20017
   129
paulson@15997
   130
paulson@15997
   131
(**** Transformation of Clasets and Simpsets into First-Order Axioms ****)
paulson@15997
   132
paulson@16563
   133
(*Transfer a theorem into theory Reconstruction.thy if it is not already
paulson@15359
   134
  inside that theory -- because it's needed for Skolemization *)
paulson@15359
   135
paulson@16563
   136
(*This will refer to the final version of theory Reconstruction.*)
wenzelm@20461
   137
val recon_thy_ref = Theory.self_ref (the_context ());
paulson@15359
   138
paulson@16563
   139
(*If called while Reconstruction is being created, it will transfer to the
paulson@16563
   140
  current version. If called afterward, it will transfer to the final version.*)
paulson@16009
   141
fun transfer_to_Reconstruction th =
paulson@16563
   142
    transfer (Theory.deref recon_thy_ref) th handle THM _ => th;
paulson@15347
   143
paulson@15955
   144
fun is_taut th =
paulson@15955
   145
      case (prop_of th) of
paulson@15955
   146
           (Const ("Trueprop", _) $ Const ("True", _)) => true
paulson@15955
   147
         | _ => false;
paulson@15955
   148
paulson@15955
   149
(* remove tautologous clauses *)
paulson@15955
   150
val rm_redundant_cls = List.filter (not o is_taut);
wenzelm@20461
   151
wenzelm@20461
   152
paulson@16009
   153
(**** SKOLEMIZATION BY INFERENCE (lcp) ****)
paulson@16009
   154
paulson@18141
   155
(*Traverse a theorem, declaring Skolem function definitions. String s is the suggested
paulson@18141
   156
  prefix for the Skolem constant. Result is a new theory*)
paulson@18141
   157
fun declare_skofuns s th thy =
paulson@20419
   158
  let fun dec_sko (Const ("Ex",_) $ (xtp as Abs(_,T,p))) (thy, axs) =
wenzelm@20461
   159
            (*Existential: declare a Skolem function, then insert into body and continue*)
wenzelm@20624
   160
            let val cname = Name.internal (gensym ("sko_" ^ s ^ "_"))
wenzelm@20461
   161
                val args = term_frees xtp  (*get the formal parameter list*)
wenzelm@20461
   162
                val Ts = map type_of args
wenzelm@20461
   163
                val cT = Ts ---> T
wenzelm@20461
   164
                val c = Const (Sign.full_name thy cname, cT)
wenzelm@20461
   165
                val rhs = list_abs_free (map dest_Free args, HOLogic.choice_const T $ xtp)
wenzelm@20461
   166
                        (*Forms a lambda-abstraction over the formal parameters*)
wenzelm@20783
   167
                val thy' = Sign.add_consts_authentic [(cname, cT, NoSyn)] thy
wenzelm@20461
   168
                           (*Theory is augmented with the constant, then its def*)
wenzelm@20461
   169
                val cdef = cname ^ "_def"
wenzelm@20461
   170
                val thy'' = Theory.add_defs_i false false [(cdef, equals cT $ c $ rhs)] thy'
wenzelm@20461
   171
            in dec_sko (subst_bound (list_comb(c,args), p))
wenzelm@20461
   172
                       (thy'', get_axiom thy'' cdef :: axs)
wenzelm@20461
   173
            end
wenzelm@20461
   174
        | dec_sko (Const ("All",_) $ (xtp as Abs(a,T,p))) thx =
wenzelm@20461
   175
            (*Universal quant: insert a free variable into body and continue*)
wenzelm@20461
   176
            let val fname = Name.variant (add_term_names (p,[])) a
wenzelm@20461
   177
            in dec_sko (subst_bound (Free(fname,T), p)) thx end
wenzelm@20461
   178
        | dec_sko (Const ("op &", _) $ p $ q) thx = dec_sko q (dec_sko p thx)
wenzelm@20461
   179
        | dec_sko (Const ("op |", _) $ p $ q) thx = dec_sko q (dec_sko p thx)
wenzelm@20461
   180
        | dec_sko (Const ("Trueprop", _) $ p) thx = dec_sko p thx
wenzelm@20461
   181
        | dec_sko t thx = thx (*Do nothing otherwise*)
paulson@20419
   182
  in  dec_sko (prop_of th) (thy,[])  end;
paulson@18141
   183
paulson@18141
   184
(*Traverse a theorem, accumulating Skolem function definitions.*)
paulson@18141
   185
fun assume_skofuns th =
paulson@18141
   186
  let fun dec_sko (Const ("Ex",_) $ (xtp as Abs(_,T,p))) defs =
wenzelm@20461
   187
            (*Existential: declare a Skolem function, then insert into body and continue*)
wenzelm@20461
   188
            let val skos = map (#1 o Logic.dest_equals) defs  (*existing sko fns*)
wenzelm@20461
   189
                val args = term_frees xtp \\ skos  (*the formal parameters*)
wenzelm@20461
   190
                val Ts = map type_of args
wenzelm@20461
   191
                val cT = Ts ---> T
wenzelm@20461
   192
                val c = Free (gensym "sko_", cT)
wenzelm@20461
   193
                val rhs = list_abs_free (map dest_Free args,
wenzelm@20461
   194
                                         HOLogic.choice_const T $ xtp)
wenzelm@20461
   195
                      (*Forms a lambda-abstraction over the formal parameters*)
wenzelm@20461
   196
                val def = equals cT $ c $ rhs
wenzelm@20461
   197
            in dec_sko (subst_bound (list_comb(c,args), p))
wenzelm@20461
   198
                       (def :: defs)
wenzelm@20461
   199
            end
wenzelm@20461
   200
        | dec_sko (Const ("All",_) $ (xtp as Abs(a,T,p))) defs =
wenzelm@20461
   201
            (*Universal quant: insert a free variable into body and continue*)
wenzelm@20461
   202
            let val fname = Name.variant (add_term_names (p,[])) a
wenzelm@20461
   203
            in dec_sko (subst_bound (Free(fname,T), p)) defs end
wenzelm@20461
   204
        | dec_sko (Const ("op &", _) $ p $ q) defs = dec_sko q (dec_sko p defs)
wenzelm@20461
   205
        | dec_sko (Const ("op |", _) $ p $ q) defs = dec_sko q (dec_sko p defs)
wenzelm@20461
   206
        | dec_sko (Const ("Trueprop", _) $ p) defs = dec_sko p defs
wenzelm@20461
   207
        | dec_sko t defs = defs (*Do nothing otherwise*)
paulson@20419
   208
  in  dec_sko (prop_of th) []  end;
paulson@20419
   209
paulson@20419
   210
paulson@20419
   211
(**** REPLACING ABSTRACTIONS BY FUNCTION DEFINITIONS ****)
paulson@20419
   212
paulson@20419
   213
(*Returns the vars of a theorem*)
paulson@20419
   214
fun vars_of_thm th =
paulson@20445
   215
  map (Thm.cterm_of (theory_of_thm th) o Var) (Drule.fold_terms Term.add_vars th []);
paulson@20419
   216
paulson@20419
   217
(*Make a version of fun_cong with a given variable name*)
paulson@20419
   218
local
paulson@20419
   219
    val fun_cong' = fun_cong RS asm_rl; (*renumber f, g to prevent clashes with (a,0)*)
paulson@20419
   220
    val cx = hd (vars_of_thm fun_cong');
paulson@20419
   221
    val ty = typ_of (ctyp_of_term cx);
paulson@20445
   222
    val thy = theory_of_thm fun_cong;
paulson@20419
   223
    fun mkvar a = cterm_of thy (Var((a,0),ty));
paulson@20419
   224
in
paulson@20419
   225
fun xfun_cong x = Thm.instantiate ([], [(cx, mkvar x)]) fun_cong'
paulson@20419
   226
end;
paulson@20419
   227
paulson@20863
   228
(*Removes the lambdas from an equation of the form t = (%x. u).  A non-negative n,
paulson@20863
   229
  serves as an upper bound on how many to remove.*)
paulson@20863
   230
fun strip_lambdas 0 th = th
paulson@20863
   231
  | strip_lambdas n th = 
paulson@20863
   232
      case prop_of th of
paulson@20863
   233
	  _ $ (Const ("op =", _) $ _ $ Abs (x,_,_)) =>
paulson@20863
   234
	      strip_lambdas (n-1) (freeze_thm (th RS xfun_cong x))
paulson@20863
   235
	| _ => th;
paulson@20419
   236
wenzelm@20461
   237
(*Convert meta- to object-equality. Fails for theorems like split_comp_eq,
paulson@20419
   238
  where some types have the empty sort.*)
paulson@20863
   239
fun mk_object_eq th = th RS def_imp_eq
paulson@20419
   240
    handle THM _ => error ("Theorem contains empty sort: " ^ string_of_thm th);
wenzelm@20461
   241
paulson@20419
   242
(*Apply a function definition to an argument, beta-reducing the result.*)
paulson@20419
   243
fun beta_comb cf x =
paulson@20419
   244
  let val th1 = combination cf (reflexive x)
wenzelm@20902
   245
      val th2 = beta_conversion false (Drule.rhs_of th1)
paulson@20419
   246
  in  transitive th1 th2  end;
paulson@20419
   247
paulson@20419
   248
(*Apply a function definition to arguments, beta-reducing along the way.*)
paulson@20419
   249
fun list_combination cf [] = cf
paulson@20419
   250
  | list_combination cf (x::xs) = list_combination (beta_comb cf x) xs;
paulson@20419
   251
paulson@20419
   252
fun list_cabs ([] ,     t) = t
paulson@20419
   253
  | list_cabs (v::vars, t) = Thm.cabs v (list_cabs(vars,t));
paulson@20419
   254
wenzelm@20461
   255
fun assert_eta_free ct =
wenzelm@20461
   256
  let val t = term_of ct
wenzelm@20461
   257
  in if (t aconv Envir.eta_contract t) then ()
paulson@20419
   258
     else error ("Eta redex in term: " ^ string_of_cterm ct)
paulson@20419
   259
  end;
paulson@20419
   260
wenzelm@20461
   261
fun eq_absdef (th1, th2) =
paulson@20445
   262
    Context.joinable (theory_of_thm th1, theory_of_thm th2)  andalso
paulson@20445
   263
    rhs_of th1 aconv rhs_of th2;
paulson@20445
   264
paulson@20445
   265
fun lambda_free (Abs _) = false
paulson@20445
   266
  | lambda_free (t $ u) = lambda_free t andalso lambda_free u
paulson@20445
   267
  | lambda_free _ = true;
wenzelm@20461
   268
wenzelm@20461
   269
fun monomorphic t =
wenzelm@20461
   270
  Term.fold_types (Term.fold_atyps (fn TVar _ => K false | _ => I)) t true;
wenzelm@20461
   271
paulson@20710
   272
fun dest_abs_list ct =
paulson@20710
   273
  let val (cv,ct') = Thm.dest_abs NONE ct
paulson@20710
   274
      val (cvs,cu) = dest_abs_list ct'
paulson@20710
   275
  in (cv::cvs, cu) end
paulson@20710
   276
  handle CTERM _ => ([],ct);
paulson@20710
   277
paulson@20710
   278
fun lambda_list [] u = u
paulson@20710
   279
  | lambda_list (v::vs) u = lambda v (lambda_list vs u);
paulson@20710
   280
paulson@20710
   281
fun abstract_rule_list [] [] th = th
paulson@20710
   282
  | abstract_rule_list (v::vs) (ct::cts) th = abstract_rule v ct (abstract_rule_list vs cts th)
paulson@20710
   283
  | abstract_rule_list _ _ th = raise THM ("abstract_rule_list", 0, [th]);
paulson@20710
   284
paulson@20863
   285
paulson@20863
   286
val Envir.Envir {asol = tenv0, iTs = tyenv0, ...} = Envir.empty 0
paulson@20863
   287
paulson@20863
   288
(*Does an existing abstraction definition have an RHS that matches the one we need now?*)
paulson@20867
   289
fun match_rhs t th =
paulson@20863
   290
  let val thy = theory_of_thm th
paulson@20863
   291
      val _ = if !trace_abs then warning ("match_rhs: " ^ string_of_cterm (cterm_of thy t) ^ 
paulson@20863
   292
                                          " against\n" ^ string_of_thm th) else ();
paulson@20867
   293
      val (tyenv,tenv) = Pattern.first_order_match thy (rhs_of th, t) (tyenv0,tenv0)
paulson@20863
   294
      val term_insts = map Meson.term_pair_of (Vartab.dest tenv)
paulson@20863
   295
      val ct_pairs = if forall lambda_free (map #2 term_insts) then
paulson@20863
   296
                         map (pairself (cterm_of thy)) term_insts
paulson@20863
   297
                     else raise Pattern.MATCH (*Cannot allow lambdas in the instantiation*)
paulson@20863
   298
      fun ctyp2 (ixn, (S, T)) = (ctyp_of thy (TVar (ixn, S)), ctyp_of thy T)
paulson@20863
   299
      val th' = cterm_instantiate ct_pairs th
paulson@20863
   300
  in  SOME (th, instantiate (map ctyp2 (Vartab.dest tyenv), []) th')  end
paulson@20863
   301
  handle _ => NONE;
paulson@20863
   302
paulson@20419
   303
(*Traverse a theorem, declaring abstraction function definitions. String s is the suggested
paulson@20419
   304
  prefix for the constants. Resulting theory is returned in the first theorem. *)
paulson@20419
   305
fun declare_absfuns th =
wenzelm@20461
   306
  let fun abstract thy ct =
paulson@20445
   307
        if lambda_free (term_of ct) then (transfer thy (reflexive ct), [])
paulson@20445
   308
        else
paulson@20445
   309
        case term_of ct of
paulson@20710
   310
          Abs _ =>
wenzelm@20624
   311
            let val cname = Name.internal (gensym "abs_");
wenzelm@20461
   312
                val _ = assert_eta_free ct;
paulson@20710
   313
                val (cvs,cta) = dest_abs_list ct
paulson@20710
   314
                val (vs,Tvs) = ListPair.unzip (map (dest_Free o term_of) cvs)
paulson@20863
   315
                val _ = if !trace_abs then warning ("Nested lambda: " ^ string_of_cterm cta) else ();
wenzelm@20461
   316
                val (u'_th,defs) = abstract thy cta
paulson@20863
   317
                val _ = if !trace_abs then warning ("Returned " ^ string_of_thm u'_th) else ();
wenzelm@20902
   318
                val cu' = Drule.rhs_of u'_th
paulson@20863
   319
                val u' = term_of cu'
paulson@20863
   320
                val abs_v_u = lambda_list (map term_of cvs) u'
wenzelm@20461
   321
                (*get the formal parameters: ALL variables free in the term*)
wenzelm@20461
   322
                val args = term_frees abs_v_u
paulson@20863
   323
                val _ = if !trace_abs then warning (Int.toString (length args) ^ " arguments") else ();
wenzelm@20461
   324
                val rhs = list_abs_free (map dest_Free args, abs_v_u)
wenzelm@20461
   325
                      (*Forms a lambda-abstraction over the formal parameters*)
paulson@20863
   326
                val _ = if !trace_abs then warning ("Looking up " ^ string_of_cterm cu') else ();
paulson@20863
   327
                val (ax,ax',thy) =
paulson@20867
   328
                 case List.mapPartial (match_rhs abs_v_u) (Net.match_term (!abstraction_cache) u')
paulson@20863
   329
                        of
paulson@20863
   330
                     (ax,ax')::_ => 
paulson@20863
   331
                       (if !trace_abs then warning ("Re-using axiom " ^ string_of_thm ax) else ();
paulson@20863
   332
                        (ax,ax',thy))
paulson@20863
   333
                   | [] =>
paulson@20863
   334
                      let val _ = if !trace_abs then warning "Lookup was empty" else ();
paulson@20863
   335
                          val Ts = map type_of args
paulson@20710
   336
                          val cT = Ts ---> (Tvs ---> typ_of (ctyp_of_term cu'))
wenzelm@20461
   337
                          val thy = theory_of_thm u'_th
wenzelm@20461
   338
                          val c = Const (Sign.full_name thy cname, cT)
wenzelm@20783
   339
                          val thy = Sign.add_consts_authentic [(cname, cT, NoSyn)] thy
wenzelm@20461
   340
                                     (*Theory is augmented with the constant,
wenzelm@20461
   341
                                       then its definition*)
wenzelm@20461
   342
                          val cdef = cname ^ "_def"
wenzelm@20461
   343
                          val thy = Theory.add_defs_i false false
wenzelm@20461
   344
                                       [(cdef, equals cT $ c $ rhs)] thy
paulson@20863
   345
                          val _ = if !trace_abs then (warning ("Definition is " ^ 
paulson@20863
   346
                                                      string_of_thm (get_axiom thy cdef))) 
paulson@20863
   347
                                  else ();
paulson@20863
   348
                          val ax = get_axiom thy cdef |> freeze_thm
paulson@20863
   349
                                     |> mk_object_eq |> strip_lambdas (length args)
paulson@20863
   350
                                     |> mk_meta_eq |> Meson.generalize
paulson@20867
   351
                          val (_,ax') = Option.valOf (match_rhs abs_v_u ax)
paulson@20863
   352
                          val _ = if !trace_abs then 
paulson@20863
   353
                                    (warning ("Declaring: " ^ string_of_thm ax);
paulson@20863
   354
                                     warning ("Instance: " ^ string_of_thm ax')) 
paulson@20863
   355
                                  else ();
paulson@20863
   356
                          val _ = abstraction_cache := Net.insert_term eq_absdef 
paulson@20863
   357
                                            ((Logic.varify u'), ax) (!abstraction_cache)
wenzelm@20461
   358
                            handle Net.INSERT =>
wenzelm@20461
   359
                              raise THM ("declare_absfuns: INSERT", 0, [th,u'_th,ax])
paulson@20863
   360
                       in  (ax,ax',thy)  end
paulson@20863
   361
            in if !trace_abs then warning ("Lookup result: " ^ string_of_thm ax') else ();
paulson@20863
   362
               (transitive (abstract_rule_list vs cvs u'_th) (symmetric ax'), ax::defs) end
wenzelm@20461
   363
        | (t1$t2) =>
wenzelm@20461
   364
            let val (ct1,ct2) = Thm.dest_comb ct
wenzelm@20461
   365
                val (th1,defs1) = abstract thy ct1
wenzelm@20461
   366
                val (th2,defs2) = abstract (theory_of_thm th1) ct2
wenzelm@20461
   367
            in  (combination th1 th2, defs1@defs2)  end
paulson@20863
   368
      val _ = if !trace_abs then warning ("declare_absfuns, Abstracting: " ^ string_of_thm th) else ();
paulson@20419
   369
      val (eqth,defs) = abstract (theory_of_thm th) (cprop_of th)
paulson@20863
   370
      val ths = equal_elim eqth th :: map (strip_lambdas ~1 o mk_object_eq o freeze_thm) defs
paulson@20863
   371
      val _ = if !trace_abs then warning ("declare_absfuns, Result: " ^ string_of_thm (hd ths)) else ();
paulson@20863
   372
  in  (theory_of_thm eqth, map Drule.eta_contraction_rule ths)  end;
paulson@20419
   373
wenzelm@20902
   374
fun name_of def = try (#1 o dest_Free o lhs_of) def;
paulson@20567
   375
paulson@20525
   376
(*A name is valid provided it isn't the name of a defined abstraction.*)
paulson@20567
   377
fun valid_name defs (Free(x,T)) = not (x mem_string (List.mapPartial name_of defs))
paulson@20525
   378
  | valid_name defs _ = false;
paulson@20525
   379
paulson@20419
   380
fun assume_absfuns th =
paulson@20445
   381
  let val thy = theory_of_thm th
paulson@20445
   382
      val cterm = cterm_of thy
paulson@20525
   383
      fun abstract ct =
paulson@20445
   384
        if lambda_free (term_of ct) then (reflexive ct, [])
paulson@20445
   385
        else
paulson@20445
   386
        case term_of ct of
paulson@20419
   387
          Abs (_,T,u) =>
paulson@20710
   388
            let val _ = assert_eta_free ct;
paulson@20710
   389
                val (cvs,cta) = dest_abs_list ct
paulson@20710
   390
                val (vs,Tvs) = ListPair.unzip (map (dest_Free o term_of) cvs)
paulson@20525
   391
                val (u'_th,defs) = abstract cta
wenzelm@20902
   392
                val cu' = Drule.rhs_of u'_th
paulson@20863
   393
                val u' = term_of cu'
paulson@20710
   394
                (*Could use Thm.cabs instead of lambda to work at level of cterms*)
paulson@20710
   395
                val abs_v_u = lambda_list (map term_of cvs) (term_of cu')
paulson@20525
   396
                (*get the formal parameters: free variables not present in the defs
paulson@20525
   397
                  (to avoid taking abstraction function names as parameters) *)
paulson@20710
   398
                val args = filter (valid_name defs) (term_frees abs_v_u)
paulson@20710
   399
                val crhs = list_cabs (map cterm args, cterm abs_v_u)
wenzelm@20461
   400
                      (*Forms a lambda-abstraction over the formal parameters*)
wenzelm@20461
   401
                val rhs = term_of crhs
paulson@20863
   402
                val (ax,ax') =
paulson@20867
   403
                 case List.mapPartial (match_rhs abs_v_u) 
paulson@20863
   404
                        (Net.match_term (!abstraction_cache) u') of
paulson@20863
   405
                     (ax,ax')::_ => 
paulson@20863
   406
                       (if !trace_abs then warning ("Re-using axiom " ^ string_of_thm ax) else ();
paulson@20863
   407
                        (ax,ax'))
paulson@20863
   408
                   | [] =>
wenzelm@20461
   409
                      let val Ts = map type_of args
paulson@20710
   410
                          val const_ty = Ts ---> (Tvs ---> typ_of (ctyp_of_term cu'))
wenzelm@20461
   411
                          val c = Free (gensym "abs_", const_ty)
wenzelm@20461
   412
                          val ax = assume (Thm.capply (cterm (equals const_ty $ c)) crhs)
paulson@20863
   413
                                     |> mk_object_eq |> strip_lambdas (length args)
paulson@20863
   414
                                     |> mk_meta_eq |> Meson.generalize
paulson@20867
   415
                          val (_,ax') = Option.valOf (match_rhs abs_v_u ax)
wenzelm@20461
   416
                          val _ = abstraction_cache := Net.insert_term eq_absdef (rhs,ax)
wenzelm@20461
   417
                                    (!abstraction_cache)
wenzelm@20461
   418
                            handle Net.INSERT =>
wenzelm@20461
   419
                              raise THM ("assume_absfuns: INSERT", 0, [th,u'_th,ax])
paulson@20863
   420
                      in (ax,ax') end
paulson@20863
   421
            in if !trace_abs then warning ("Lookup result: " ^ string_of_thm ax') else ();
paulson@20863
   422
               (transitive (abstract_rule_list vs cvs u'_th) (symmetric ax'), ax::defs) end
wenzelm@20461
   423
        | (t1$t2) =>
wenzelm@20461
   424
            let val (ct1,ct2) = Thm.dest_comb ct
paulson@20525
   425
                val (t1',defs1) = abstract ct1
paulson@20525
   426
                val (t2',defs2) = abstract ct2
wenzelm@20461
   427
            in  (combination t1' t2', defs1@defs2)  end
paulson@20863
   428
      val _ = if !trace_abs then warning ("assume_absfuns, Abstracting: " ^ string_of_thm th) else ();
paulson@20525
   429
      val (eqth,defs) = abstract (cprop_of th)
paulson@20863
   430
      val ths = equal_elim eqth th :: map (strip_lambdas ~1 o mk_object_eq o freeze_thm) defs
paulson@20863
   431
      val _ = if !trace_abs then warning ("assume_absfuns, Result: " ^ string_of_thm (hd ths)) else ();
paulson@20863
   432
  in  map Drule.eta_contraction_rule ths  end;
paulson@20419
   433
paulson@16009
   434
paulson@16009
   435
(*cterms are used throughout for efficiency*)
paulson@18141
   436
val cTrueprop = Thm.cterm_of HOL.thy HOLogic.Trueprop;
paulson@16009
   437
paulson@16009
   438
(*cterm version of mk_cTrueprop*)
paulson@16009
   439
fun c_mkTrueprop A = Thm.capply cTrueprop A;
paulson@16009
   440
paulson@16009
   441
(*Given an abstraction over n variables, replace the bound variables by free
paulson@16009
   442
  ones. Return the body, along with the list of free variables.*)
wenzelm@20461
   443
fun c_variant_abs_multi (ct0, vars) =
paulson@16009
   444
      let val (cv,ct) = Thm.dest_abs NONE ct0
paulson@16009
   445
      in  c_variant_abs_multi (ct, cv::vars)  end
paulson@16009
   446
      handle CTERM _ => (ct0, rev vars);
paulson@16009
   447
wenzelm@20461
   448
(*Given the definition of a Skolem function, return a theorem to replace
wenzelm@20461
   449
  an existential formula by a use of that function.
paulson@18141
   450
   Example: "EX x. x : A & x ~: B ==> sko A B : A & sko A B ~: B"  [.] *)
wenzelm@20461
   451
fun skolem_of_def def =
paulson@20863
   452
  let val (c,rhs) = Drule.dest_equals (cprop_of (freeze_thm def))
paulson@16009
   453
      val (ch, frees) = c_variant_abs_multi (rhs, [])
paulson@18141
   454
      val (chilbert,cabs) = Thm.dest_comb ch
paulson@18141
   455
      val {sign,t, ...} = rep_cterm chilbert
paulson@18141
   456
      val T = case t of Const ("Hilbert_Choice.Eps", Type("fun",[_,T])) => T
paulson@18141
   457
                      | _ => raise THM ("skolem_of_def: expected Eps", 0, [def])
paulson@16009
   458
      val cex = Thm.cterm_of sign (HOLogic.exists_const T)
paulson@16009
   459
      val ex_tm = c_mkTrueprop (Thm.capply cex cabs)
paulson@16009
   460
      and conc =  c_mkTrueprop (Drule.beta_conv cabs (Drule.list_comb(c,frees)));
paulson@18141
   461
      fun tacf [prem] = rewrite_goals_tac [def] THEN rtac (prem RS someI_ex) 1
wenzelm@20461
   462
  in  Goal.prove_raw [ex_tm] conc tacf
paulson@18141
   463
       |> forall_intr_list frees
paulson@18141
   464
       |> forall_elim_vars 0  (*Introduce Vars, but don't discharge defs.*)
paulson@18141
   465
       |> Thm.varifyT
paulson@18141
   466
  end;
paulson@16009
   467
paulson@20863
   468
(*Converts an Isabelle theorem (intro, elim or simp format, even higher-order) into NNF.*)
wenzelm@20461
   469
fun to_nnf th =
paulson@18141
   470
    th |> transfer_to_Reconstruction
paulson@20863
   471
       |> transform_elim |> zero_var_indexes |> freeze_thm
paulson@20863
   472
       |> ObjectLogic.atomize_thm |> make_nnf |> strip_lambdas ~1;
paulson@16009
   473
wenzelm@20461
   474
(*The cache prevents repeated clausification of a theorem,
wenzelm@20461
   475
  and also repeated declaration of Skolem functions*)
paulson@18510
   476
  (* FIXME better use Termtab!? No, we MUST use theory data!!*)
paulson@15955
   477
val clause_cache = ref (Symtab.empty : (thm * thm list) Symtab.table)
paulson@15955
   478
paulson@18141
   479
paulson@18141
   480
(*Generate Skolem functions for a theorem supplied in nnf*)
paulson@18141
   481
fun skolem_of_nnf th =
paulson@18141
   482
  map (skolem_of_def o assume o (cterm_of (theory_of_thm th))) (assume_skofuns th);
paulson@18141
   483
paulson@20863
   484
fun assert_lambda_free ths msg = 
paulson@20863
   485
  case filter (not o lambda_free o prop_of) ths of
paulson@20863
   486
      [] => ()
paulson@20863
   487
     | ths' => error (msg ^ "\n" ^ space_implode "\n" (map string_of_thm ths'));
paulson@20457
   488
paulson@20445
   489
fun assume_abstract th =
paulson@20457
   490
  if lambda_free (prop_of th) then [th]
paulson@20863
   491
  else th |> Drule.eta_contraction_rule |> assume_absfuns
paulson@20457
   492
          |> tap (fn ths => assert_lambda_free ths "assume_abstract: lambdas")
paulson@20445
   493
paulson@20419
   494
(*Replace lambdas by assumed function definitions in the theorems*)
paulson@20445
   495
fun assume_abstract_list ths =
paulson@20445
   496
  if abstract_lambdas then List.concat (map assume_abstract ths)
paulson@20863
   497
  else map Drule.eta_contraction_rule ths;
paulson@20419
   498
paulson@20419
   499
(*Replace lambdas by declared function definitions in the theorems*)
paulson@20419
   500
fun declare_abstract' (thy, []) = (thy, [])
paulson@20419
   501
  | declare_abstract' (thy, th::ths) =
wenzelm@20461
   502
      let val (thy', th_defs) =
paulson@20457
   503
            if lambda_free (prop_of th) then (thy, [th])
paulson@20445
   504
            else
paulson@20863
   505
                th |> zero_var_indexes |> freeze_thm
paulson@20863
   506
                   |> Drule.eta_contraction_rule |> transfer thy |> declare_absfuns
wenzelm@20461
   507
          val _ = assert_lambda_free th_defs "declare_abstract: lambdas"
wenzelm@20461
   508
          val (thy'', ths') = declare_abstract' (thy', ths)
paulson@20419
   509
      in  (thy'', th_defs @ ths')  end;
paulson@20419
   510
paulson@20421
   511
(*FIXME DELETE if we decide to switch to abstractions*)
paulson@20419
   512
fun declare_abstract (thy, ths) =
paulson@20419
   513
  if abstract_lambdas then declare_abstract' (thy, ths)
paulson@20863
   514
  else (thy, map Drule.eta_contraction_rule ths);
paulson@20419
   515
paulson@18510
   516
(*Skolemize a named theorem, with Skolem functions as additional premises.*)
wenzelm@20461
   517
(*also works for HOL*)
wenzelm@20461
   518
fun skolem_thm th =
paulson@18510
   519
  let val nnfth = to_nnf th
paulson@20419
   520
  in  Meson.make_cnf (skolem_of_nnf nnfth) nnfth
paulson@20445
   521
      |> assume_abstract_list |> Meson.finish_cnf |> rm_redundant_cls
paulson@18510
   522
  end
paulson@18510
   523
  handle THM _ => [];
paulson@18141
   524
paulson@18510
   525
(*Declare Skolem functions for a theorem, supplied in nnf and with its name.
paulson@18510
   526
  It returns a modified theory, unless skolemization fails.*)
paulson@16009
   527
fun skolem thy (name,th) =
paulson@20419
   528
  let val cname = (case name of "" => gensym "" | s => Sign.base_name s)
paulson@20419
   529
      val _ = Output.debug ("skolemizing " ^ name ^ ": ")
wenzelm@20461
   530
  in Option.map
wenzelm@20461
   531
        (fn nnfth =>
paulson@18141
   532
          let val (thy',defs) = declare_skofuns cname nnfth thy
paulson@20419
   533
              val cnfs = Meson.make_cnf (map skolem_of_def defs) nnfth
paulson@20419
   534
              val (thy'',cnfs') = declare_abstract (thy',cnfs)
paulson@20419
   535
          in (thy'', rm_redundant_cls (Meson.finish_cnf cnfs'))
paulson@20419
   536
          end)
wenzelm@20461
   537
      (SOME (to_nnf th)  handle THM _ => NONE)
paulson@18141
   538
  end;
paulson@16009
   539
paulson@18510
   540
(*Populate the clause cache using the supplied theorem. Return the clausal form
paulson@18510
   541
  and modified theory.*)
wenzelm@20461
   542
fun skolem_cache_thm (name,th) thy =
paulson@18144
   543
  case Symtab.lookup (!clause_cache) name of
wenzelm@20461
   544
      NONE =>
wenzelm@20461
   545
        (case skolem thy (name, Thm.transfer thy th) of
wenzelm@20461
   546
             NONE => ([th],thy)
paulson@20473
   547
           | SOME (thy',cls) => 
paulson@20473
   548
               let val cls = map Drule.local_standard cls
paulson@20473
   549
               in
paulson@20473
   550
                  if null cls then warning ("skolem_cache: empty clause set for " ^ name)
paulson@20473
   551
                  else ();
paulson@20473
   552
                  change clause_cache (Symtab.update (name, (th, cls))); 
paulson@20473
   553
                  (cls,thy')
paulson@20473
   554
               end)
paulson@18144
   555
    | SOME (th',cls) =>
paulson@18510
   556
        if eq_thm(th,th') then (cls,thy)
wenzelm@20461
   557
        else (Output.debug ("skolem_cache: Ignoring variant of theorem " ^ name);
wenzelm@20461
   558
              Output.debug (string_of_thm th);
wenzelm@20461
   559
              Output.debug (string_of_thm th');
wenzelm@20461
   560
              ([th],thy));
wenzelm@20461
   561
wenzelm@20461
   562
(*Exported function to convert Isabelle theorems into axiom clauses*)
paulson@19894
   563
fun cnf_axiom (name,th) =
paulson@18144
   564
  case name of
wenzelm@20461
   565
        "" => skolem_thm th (*no name, so can't cache*)
paulson@18144
   566
      | s  => case Symtab.lookup (!clause_cache) s of
paulson@20473
   567
                NONE => 
paulson@20473
   568
                  let val cls = map Drule.local_standard (skolem_thm th)
wenzelm@20461
   569
                  in change clause_cache (Symtab.update (s, (th, cls))); cls end
wenzelm@20461
   570
              | SOME(th',cls) =>
wenzelm@20461
   571
                  if eq_thm(th,th') then cls
wenzelm@20461
   572
                  else (Output.debug ("cnf_axiom: duplicate or variant of theorem " ^ name);
wenzelm@20461
   573
                        Output.debug (string_of_thm th);
wenzelm@20461
   574
                        Output.debug (string_of_thm th');
wenzelm@20461
   575
                        cls);
paulson@15347
   576
paulson@18141
   577
fun pairname th = (Thm.name_of_thm th, th);
paulson@18141
   578
wenzelm@20461
   579
fun meta_cnf_axiom th =
paulson@15956
   580
    map Meson.make_meta_clause (cnf_axiom (pairname th));
paulson@15499
   581
paulson@15347
   582
paulson@15872
   583
(**** Extract and Clausify theorems from a theory's claset and simpset ****)
paulson@15347
   584
paulson@17404
   585
(*Preserve the name of "th" after the transformation "f"*)
paulson@17404
   586
fun preserve_name f th = Thm.name_thm (Thm.name_of_thm th, f th);
paulson@17404
   587
paulson@17484
   588
fun rules_of_claset cs =
paulson@17484
   589
  let val {safeIs,safeEs,hazIs,hazEs,...} = rep_cs cs
paulson@19175
   590
      val intros = safeIs @ hazIs
wenzelm@18532
   591
      val elims  = map Classical.classical_rule (safeEs @ hazEs)
paulson@17404
   592
  in
wenzelm@20461
   593
     Output.debug ("rules_of_claset intros: " ^ Int.toString(length intros) ^
paulson@17484
   594
            " elims: " ^ Int.toString(length elims));
paulson@20017
   595
     map pairname (intros @ elims)
paulson@17404
   596
  end;
paulson@15347
   597
paulson@17484
   598
fun rules_of_simpset ss =
paulson@17484
   599
  let val ({rules,...}, _) = rep_ss ss
paulson@17484
   600
      val simps = Net.entries rules
wenzelm@20461
   601
  in
wenzelm@18680
   602
      Output.debug ("rules_of_simpset: " ^ Int.toString(length simps));
paulson@17484
   603
      map (fn r => (#name r, #thm r)) simps
paulson@17484
   604
  end;
paulson@17484
   605
paulson@17484
   606
fun claset_rules_of_thy thy = rules_of_claset (claset_of thy);
paulson@17484
   607
fun simpset_rules_of_thy thy = rules_of_simpset (simpset_of thy);
paulson@17484
   608
wenzelm@20774
   609
fun atpset_rules_of_thy thy = map pairname (ResAtpset.get_atpset (Context.Theory thy));
mengj@19196
   610
mengj@19196
   611
paulson@17484
   612
fun claset_rules_of_ctxt ctxt = rules_of_claset (local_claset_of ctxt);
paulson@17484
   613
fun simpset_rules_of_ctxt ctxt = rules_of_simpset (local_simpset_of ctxt);
paulson@15347
   614
wenzelm@20774
   615
fun atpset_rules_of_ctxt ctxt = map pairname (ResAtpset.get_atpset (Context.Proof ctxt));
wenzelm@20774
   616
paulson@15347
   617
paulson@15872
   618
(**** Translate a set of classical/simplifier rules into CNF (still as type "thm")  ****)
paulson@15347
   619
paulson@19894
   620
(* classical rules: works for both FOL and HOL *)
paulson@19894
   621
fun cnf_rules [] err_list = ([],err_list)
wenzelm@20461
   622
  | cnf_rules ((name,th) :: ths) err_list =
paulson@19894
   623
      let val (ts,es) = cnf_rules ths err_list
wenzelm@20461
   624
      in  (cnf_axiom (name,th) :: ts,es) handle  _ => (ts, (th::es))  end;
paulson@15347
   625
paulson@19894
   626
fun pair_name_cls k (n, []) = []
paulson@19894
   627
  | pair_name_cls k (n, cls::clss) = (cls, (n,k)) :: pair_name_cls (k+1) (n, clss)
wenzelm@20461
   628
paulson@19894
   629
fun cnf_rules_pairs_aux pairs [] = pairs
paulson@19894
   630
  | cnf_rules_pairs_aux pairs ((name,th)::ths) =
paulson@20457
   631
      let val pairs' = (pair_name_cls 0 (name, cnf_axiom(name,th))) @ pairs
wenzelm@20461
   632
                       handle THM _ => pairs | ResClause.CLAUSE _ => pairs
paulson@19894
   633
      in  cnf_rules_pairs_aux pairs' ths  end;
wenzelm@20461
   634
paulson@19894
   635
val cnf_rules_pairs = cnf_rules_pairs_aux [];
mengj@19353
   636
mengj@19196
   637
mengj@18198
   638
(**** Convert all theorems of a claset/simpset into clauses (ResClause.clause, or ResHolClause.clause) ****)
paulson@15347
   639
paulson@20419
   640
(*Setup function: takes a theory and installs ALL known theorems into the clause cache*)
paulson@20457
   641
wenzelm@20461
   642
fun skolem_cache (name,th) thy =
wenzelm@20461
   643
  let val prop = Thm.prop_of th
paulson@20457
   644
  in
paulson@20867
   645
      if lambda_free prop 
paulson@20867
   646
         (*orelse monomorphic prop? Monomorphic theorems can be Skolemized on demand,
paulson@20867
   647
           but there are problems with re-use of abstraction functions if we don't
paulson@20867
   648
           do them now, even for monomorphic theorems.*)
paulson@20867
   649
      then thy  
wenzelm@20461
   650
      else #2 (skolem_cache_thm (name,th) thy)
paulson@20457
   651
  end;
paulson@20457
   652
wenzelm@20461
   653
fun clause_cache_setup thy = fold skolem_cache (PureThy.all_thms_of thy) thy;
wenzelm@20461
   654
paulson@16563
   655
paulson@16563
   656
(*** meson proof methods ***)
paulson@16563
   657
paulson@16563
   658
fun cnf_rules_of_ths ths = List.concat (#1 (cnf_rules (map pairname ths) []));
paulson@16563
   659
paulson@16563
   660
fun meson_meth ths ctxt =
paulson@16563
   661
  Method.SIMPLE_METHOD' HEADGOAL
paulson@16563
   662
    (CHANGED_PROP o Meson.meson_claset_tac (cnf_rules_of_ths ths) (local_claset_of ctxt));
paulson@16563
   663
paulson@16563
   664
val meson_method_setup =
wenzelm@18708
   665
  Method.add_methods
wenzelm@20461
   666
    [("meson", Method.thms_ctxt_args meson_meth,
wenzelm@18833
   667
      "MESON resolution proof procedure")];
paulson@15347
   668
paulson@18510
   669
paulson@18510
   670
paulson@18510
   671
(*** The Skolemization attribute ***)
paulson@18510
   672
paulson@18510
   673
fun conj2_rule (th1,th2) = conjI OF [th1,th2];
paulson@18510
   674
paulson@20457
   675
(*Conjoin a list of theorems to form a single theorem*)
paulson@20457
   676
fun conj_rule []  = TrueI
paulson@20445
   677
  | conj_rule ths = foldr1 conj2_rule ths;
paulson@18510
   678
paulson@20419
   679
fun skolem_attr (Context.Theory thy, th) =
paulson@20419
   680
      let val name = Thm.name_of_thm th
wenzelm@20461
   681
          val (cls, thy') = skolem_cache_thm (name, th) thy
wenzelm@18728
   682
      in (Context.Theory thy', conj_rule cls) end
paulson@20419
   683
  | skolem_attr (context, th) = (context, conj_rule (skolem_thm th));
paulson@18510
   684
paulson@18510
   685
val setup_attrs = Attrib.add_attributes
paulson@20419
   686
  [("skolem", Attrib.no_args skolem_attr, "skolemization of a theorem")];
paulson@18510
   687
wenzelm@18708
   688
val setup = clause_cache_setup #> setup_attrs;
paulson@18510
   689
wenzelm@20461
   690
end;