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