src/HOL/Tools/res_axioms.ML
author wenzelm
Fri Aug 17 00:03:50 2007 +0200 (2007-08-17)
changeset 24300 e170cee91c66
parent 24215 5458fbf18276
child 24632 779fc4fcbf8b
permissions -rw-r--r--
proper signature for Meson;
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@15997
     8
signature RES_AXIOMS =
wenzelm@21505
     9
sig
wenzelm@21505
    10
  val cnf_axiom : string * thm -> thm list
paulson@21071
    11
  val cnf_name : string -> thm list
paulson@15997
    12
  val meta_cnf_axiom : thm -> thm list
wenzelm@21505
    13
  val pairname : thm -> string * thm
paulson@18510
    14
  val skolem_thm : thm -> thm list
wenzelm@21505
    15
  val cnf_rules_pairs : (string * thm) list -> (thm * (string * int)) list
wenzelm@18708
    16
  val meson_method_setup : theory -> theory
wenzelm@18708
    17
  val setup : theory -> theory
paulson@22731
    18
  val assume_abstract_list: string -> thm list -> thm list
paulson@21999
    19
  val neg_conjecture_clauses: thm -> int -> thm list * (string * typ) list
paulson@21999
    20
  val claset_rules_of: Proof.context -> (string * thm) list   (*FIXME DELETE*)
paulson@21999
    21
  val simpset_rules_of: Proof.context -> (string * thm) list  (*FIXME DELETE*)
wenzelm@21505
    22
  val atpset_rules_of: Proof.context -> (string * thm) list
wenzelm@21505
    23
end;
mengj@19196
    24
paulson@21900
    25
structure ResAxioms =
paulson@15997
    26
struct
paulson@15347
    27
paulson@20996
    28
(*For running the comparison between combinators and abstractions.
paulson@20996
    29
  CANNOT be a ref, as the setting is used while Isabelle is built.
paulson@22644
    30
  Currently TRUE: the combinator code cannot be used with proof reconstruction
paulson@22644
    31
  because it is not performed by inference!!*)
paulson@21900
    32
val abstract_lambdas = true;
paulson@20419
    33
wenzelm@20902
    34
(* FIXME legacy *)
paulson@20863
    35
fun freeze_thm th = #1 (Drule.freeze_thaw th);
paulson@20863
    36
wenzelm@20902
    37
val lhs_of = #1 o Logic.dest_equals o Thm.prop_of;
wenzelm@20902
    38
val rhs_of = #2 o Logic.dest_equals o Thm.prop_of;
paulson@20863
    39
paulson@20863
    40
paulson@20445
    41
(*Store definitions of abstraction functions, ensuring that identical right-hand
paulson@20445
    42
  sides are denoted by the same functions and thereby reducing the need for
paulson@20445
    43
  extensionality in proofs.
paulson@20445
    44
  FIXME!  Store in theory data!!*)
paulson@20863
    45
paulson@20867
    46
(*Populate the abstraction cache with common combinators.*)
paulson@20863
    47
fun seed th net =
wenzelm@22902
    48
  let val (_,ct) = Thm.dest_abs NONE (Thm.rhs_of th)
paulson@20867
    49
      val t = Logic.legacy_varify (term_of ct)
wenzelm@22360
    50
  in  Net.insert_term Thm.eq_thm (t, th) net end;
paulson@20863
    51
  
paulson@20863
    52
val abstraction_cache = ref 
wenzelm@21254
    53
      (seed (thm"ATP_Linkup.I_simp") 
wenzelm@21254
    54
       (seed (thm"ATP_Linkup.B_simp") 
wenzelm@21254
    55
	(seed (thm"ATP_Linkup.K_simp") Net.empty)));
paulson@20867
    56
paulson@20445
    57
paulson@15997
    58
(**** Transformation of Elimination Rules into First-Order Formulas****)
paulson@15347
    59
paulson@21430
    60
val cfalse = cterm_of HOL.thy HOLogic.false_const;
paulson@21430
    61
val ctp_false = cterm_of HOL.thy (HOLogic.mk_Trueprop HOLogic.false_const);
wenzelm@20461
    62
paulson@21430
    63
(*Converts an elim-rule into an equivalent theorem that does not have the
paulson@21430
    64
  predicate variable.  Leaves other theorems unchanged.  We simply instantiate the
paulson@21430
    65
  conclusion variable to False.*)
paulson@16009
    66
fun transform_elim th =
paulson@21430
    67
  case concl_of th of    (*conclusion variable*)
paulson@21430
    68
       Const("Trueprop",_) $ (v as Var(_,Type("bool",[]))) => 
paulson@21430
    69
           Thm.instantiate ([], [(cterm_of HOL.thy v, cfalse)]) th
paulson@21430
    70
    | v as Var(_, Type("prop",[])) => 
paulson@21430
    71
           Thm.instantiate ([], [(cterm_of HOL.thy v, ctp_false)]) th
paulson@21430
    72
    | _ => th;
paulson@15997
    73
paulson@15997
    74
(**** Transformation of Clasets and Simpsets into First-Order Axioms ****)
paulson@15997
    75
wenzelm@21254
    76
(*Transfer a theorem into theory ATP_Linkup.thy if it is not already
paulson@15359
    77
  inside that theory -- because it's needed for Skolemization *)
paulson@15359
    78
wenzelm@21254
    79
(*This will refer to the final version of theory ATP_Linkup.*)
wenzelm@24137
    80
val recon_thy_ref = Theory.check_thy @{theory}
paulson@15359
    81
wenzelm@21254
    82
(*If called while ATP_Linkup is being created, it will transfer to the
paulson@16563
    83
  current version. If called afterward, it will transfer to the final version.*)
wenzelm@21254
    84
fun transfer_to_ATP_Linkup th =
paulson@16563
    85
    transfer (Theory.deref recon_thy_ref) th handle THM _ => th;
paulson@15347
    86
wenzelm@20461
    87
paulson@16009
    88
(**** SKOLEMIZATION BY INFERENCE (lcp) ****)
paulson@16009
    89
paulson@18141
    90
(*Traverse a theorem, declaring Skolem function definitions. String s is the suggested
paulson@18141
    91
  prefix for the Skolem constant. Result is a new theory*)
paulson@18141
    92
fun declare_skofuns s th thy =
paulson@21071
    93
  let val nref = ref 0
paulson@21071
    94
      fun dec_sko (Const ("Ex",_) $ (xtp as Abs(_,T,p))) (thy, axs) =
wenzelm@20461
    95
            (*Existential: declare a Skolem function, then insert into body and continue*)
paulson@22731
    96
            let val cname = Name.internal ("sko_" ^ s ^ "_" ^ Int.toString (inc nref))
wenzelm@20461
    97
                val args = term_frees xtp  (*get the formal parameter list*)
wenzelm@20461
    98
                val Ts = map type_of args
wenzelm@20461
    99
                val cT = Ts ---> T
wenzelm@20461
   100
                val c = Const (Sign.full_name thy cname, cT)
wenzelm@20461
   101
                val rhs = list_abs_free (map dest_Free args, HOLogic.choice_const T $ xtp)
wenzelm@20461
   102
                        (*Forms a lambda-abstraction over the formal parameters*)
wenzelm@20783
   103
                val thy' = Sign.add_consts_authentic [(cname, cT, NoSyn)] thy
wenzelm@20461
   104
                           (*Theory is augmented with the constant, then its def*)
wenzelm@20461
   105
                val cdef = cname ^ "_def"
wenzelm@20461
   106
                val thy'' = Theory.add_defs_i false false [(cdef, equals cT $ c $ rhs)] thy'
wenzelm@20461
   107
            in dec_sko (subst_bound (list_comb(c,args), p))
wenzelm@20461
   108
                       (thy'', get_axiom thy'' cdef :: axs)
wenzelm@20461
   109
            end
wenzelm@20461
   110
        | dec_sko (Const ("All",_) $ (xtp as Abs(a,T,p))) thx =
wenzelm@20461
   111
            (*Universal quant: insert a free variable into body and continue*)
wenzelm@20461
   112
            let val fname = Name.variant (add_term_names (p,[])) a
wenzelm@20461
   113
            in dec_sko (subst_bound (Free(fname,T), p)) thx end
wenzelm@20461
   114
        | dec_sko (Const ("op &", _) $ p $ q) thx = dec_sko q (dec_sko p thx)
wenzelm@20461
   115
        | dec_sko (Const ("op |", _) $ p $ q) thx = dec_sko q (dec_sko p thx)
wenzelm@20461
   116
        | dec_sko (Const ("Trueprop", _) $ p) thx = dec_sko p thx
wenzelm@20461
   117
        | dec_sko t thx = thx (*Do nothing otherwise*)
paulson@20419
   118
  in  dec_sko (prop_of th) (thy,[])  end;
paulson@18141
   119
paulson@18141
   120
(*Traverse a theorem, accumulating Skolem function definitions.*)
paulson@22731
   121
fun assume_skofuns s th =
paulson@22731
   122
  let val sko_count = ref 0
paulson@22731
   123
      fun dec_sko (Const ("Ex",_) $ (xtp as Abs(_,T,p))) defs =
wenzelm@20461
   124
            (*Existential: declare a Skolem function, then insert into body and continue*)
wenzelm@20461
   125
            let val skos = map (#1 o Logic.dest_equals) defs  (*existing sko fns*)
wenzelm@20461
   126
                val args = term_frees xtp \\ skos  (*the formal parameters*)
wenzelm@20461
   127
                val Ts = map type_of args
wenzelm@20461
   128
                val cT = Ts ---> T
paulson@22731
   129
                val id = "sko_" ^ s ^ "_" ^ Int.toString (inc sko_count)
paulson@22731
   130
                val c = Free (id, cT)
wenzelm@20461
   131
                val rhs = list_abs_free (map dest_Free args,
wenzelm@20461
   132
                                         HOLogic.choice_const T $ xtp)
wenzelm@20461
   133
                      (*Forms a lambda-abstraction over the formal parameters*)
wenzelm@20461
   134
                val def = equals cT $ c $ rhs
wenzelm@20461
   135
            in dec_sko (subst_bound (list_comb(c,args), p))
wenzelm@20461
   136
                       (def :: defs)
wenzelm@20461
   137
            end
wenzelm@20461
   138
        | dec_sko (Const ("All",_) $ (xtp as Abs(a,T,p))) defs =
wenzelm@20461
   139
            (*Universal quant: insert a free variable into body and continue*)
wenzelm@20461
   140
            let val fname = Name.variant (add_term_names (p,[])) a
wenzelm@20461
   141
            in dec_sko (subst_bound (Free(fname,T), p)) defs end
wenzelm@20461
   142
        | dec_sko (Const ("op &", _) $ p $ q) defs = dec_sko q (dec_sko p defs)
wenzelm@20461
   143
        | dec_sko (Const ("op |", _) $ p $ q) defs = dec_sko q (dec_sko p defs)
wenzelm@20461
   144
        | dec_sko (Const ("Trueprop", _) $ p) defs = dec_sko p defs
wenzelm@20461
   145
        | dec_sko t defs = defs (*Do nothing otherwise*)
paulson@20419
   146
  in  dec_sko (prop_of th) []  end;
paulson@20419
   147
paulson@20419
   148
paulson@20419
   149
(**** REPLACING ABSTRACTIONS BY FUNCTION DEFINITIONS ****)
paulson@20419
   150
paulson@20419
   151
(*Returns the vars of a theorem*)
paulson@20419
   152
fun vars_of_thm th =
wenzelm@22691
   153
  map (Thm.cterm_of (theory_of_thm th) o Var) (Thm.fold_terms Term.add_vars th []);
paulson@20419
   154
paulson@20419
   155
(*Make a version of fun_cong with a given variable name*)
paulson@20419
   156
local
paulson@20419
   157
    val fun_cong' = fun_cong RS asm_rl; (*renumber f, g to prevent clashes with (a,0)*)
paulson@20419
   158
    val cx = hd (vars_of_thm fun_cong');
paulson@20419
   159
    val ty = typ_of (ctyp_of_term cx);
paulson@20445
   160
    val thy = theory_of_thm fun_cong;
paulson@20419
   161
    fun mkvar a = cterm_of thy (Var((a,0),ty));
paulson@20419
   162
in
paulson@20419
   163
fun xfun_cong x = Thm.instantiate ([], [(cx, mkvar x)]) fun_cong'
paulson@20419
   164
end;
paulson@20419
   165
paulson@20863
   166
(*Removes the lambdas from an equation of the form t = (%x. u).  A non-negative n,
paulson@20863
   167
  serves as an upper bound on how many to remove.*)
paulson@20863
   168
fun strip_lambdas 0 th = th
paulson@20863
   169
  | strip_lambdas n th = 
paulson@20863
   170
      case prop_of th of
paulson@20863
   171
	  _ $ (Const ("op =", _) $ _ $ Abs (x,_,_)) =>
paulson@20863
   172
	      strip_lambdas (n-1) (freeze_thm (th RS xfun_cong x))
paulson@20863
   173
	| _ => th;
paulson@20419
   174
wenzelm@20461
   175
(*Convert meta- to object-equality. Fails for theorems like split_comp_eq,
paulson@20419
   176
  where some types have the empty sort.*)
haftmann@22218
   177
val meta_eq_to_obj_eq = thm "meta_eq_to_obj_eq";
haftmann@22218
   178
fun mk_object_eq th = th RS meta_eq_to_obj_eq
paulson@20419
   179
    handle THM _ => error ("Theorem contains empty sort: " ^ string_of_thm th);
wenzelm@20461
   180
paulson@20419
   181
(*Apply a function definition to an argument, beta-reducing the result.*)
paulson@20419
   182
fun beta_comb cf x =
paulson@20419
   183
  let val th1 = combination cf (reflexive x)
wenzelm@22902
   184
      val th2 = beta_conversion false (Thm.rhs_of th1)
paulson@20419
   185
  in  transitive th1 th2  end;
paulson@20419
   186
paulson@20419
   187
(*Apply a function definition to arguments, beta-reducing along the way.*)
paulson@20419
   188
fun list_combination cf [] = cf
paulson@20419
   189
  | list_combination cf (x::xs) = list_combination (beta_comb cf x) xs;
paulson@20419
   190
paulson@20419
   191
fun list_cabs ([] ,     t) = t
paulson@20419
   192
  | list_cabs (v::vars, t) = Thm.cabs v (list_cabs(vars,t));
paulson@20419
   193
wenzelm@20461
   194
fun assert_eta_free ct =
wenzelm@20461
   195
  let val t = term_of ct
wenzelm@20461
   196
  in if (t aconv Envir.eta_contract t) then ()
paulson@20419
   197
     else error ("Eta redex in term: " ^ string_of_cterm ct)
paulson@20419
   198
  end;
paulson@20419
   199
wenzelm@20461
   200
fun eq_absdef (th1, th2) =
paulson@20445
   201
    Context.joinable (theory_of_thm th1, theory_of_thm th2)  andalso
paulson@20445
   202
    rhs_of th1 aconv rhs_of th2;
paulson@20445
   203
paulson@20445
   204
fun lambda_free (Abs _) = false
paulson@20445
   205
  | lambda_free (t $ u) = lambda_free t andalso lambda_free u
paulson@20445
   206
  | lambda_free _ = true;
wenzelm@20461
   207
wenzelm@20461
   208
fun monomorphic t =
wenzelm@20461
   209
  Term.fold_types (Term.fold_atyps (fn TVar _ => K false | _ => I)) t true;
wenzelm@20461
   210
paulson@20710
   211
fun dest_abs_list ct =
paulson@20710
   212
  let val (cv,ct') = Thm.dest_abs NONE ct
paulson@20710
   213
      val (cvs,cu) = dest_abs_list ct'
paulson@20710
   214
  in (cv::cvs, cu) end
paulson@20710
   215
  handle CTERM _ => ([],ct);
paulson@20710
   216
paulson@20710
   217
fun lambda_list [] u = u
paulson@20710
   218
  | lambda_list (v::vs) u = lambda v (lambda_list vs u);
paulson@20710
   219
paulson@20710
   220
fun abstract_rule_list [] [] th = th
paulson@20710
   221
  | abstract_rule_list (v::vs) (ct::cts) th = abstract_rule v ct (abstract_rule_list vs cts th)
paulson@20710
   222
  | abstract_rule_list _ _ th = raise THM ("abstract_rule_list", 0, [th]);
paulson@20710
   223
paulson@20863
   224
paulson@20863
   225
val Envir.Envir {asol = tenv0, iTs = tyenv0, ...} = Envir.empty 0
paulson@20863
   226
paulson@20969
   227
(*Does an existing abstraction definition have an RHS that matches the one we need now?
paulson@20969
   228
  thy is the current theory, which must extend that of theorem th.*)
paulson@20969
   229
fun match_rhs thy t th =
paulson@24215
   230
  let val _ = Output.debug (fn()=> "match_rhs: " ^ string_of_cterm (cterm_of thy t) ^ 
paulson@24215
   231
                                   " against\n" ^ string_of_thm th);
paulson@20867
   232
      val (tyenv,tenv) = Pattern.first_order_match thy (rhs_of th, t) (tyenv0,tenv0)
paulson@20863
   233
      val term_insts = map Meson.term_pair_of (Vartab.dest tenv)
paulson@20969
   234
      val ct_pairs = if subthy (theory_of_thm th, thy) andalso 
paulson@20969
   235
                        forall lambda_free (map #2 term_insts) 
paulson@20969
   236
                     then map (pairself (cterm_of thy)) term_insts
paulson@20863
   237
                     else raise Pattern.MATCH (*Cannot allow lambdas in the instantiation*)
paulson@20863
   238
      fun ctyp2 (ixn, (S, T)) = (ctyp_of thy (TVar (ixn, S)), ctyp_of thy T)
paulson@20863
   239
      val th' = cterm_instantiate ct_pairs th
paulson@20863
   240
  in  SOME (th, instantiate (map ctyp2 (Vartab.dest tyenv), []) th')  end
paulson@20863
   241
  handle _ => NONE;
paulson@20863
   242
paulson@20419
   243
(*Traverse a theorem, declaring abstraction function definitions. String s is the suggested
paulson@20419
   244
  prefix for the constants. Resulting theory is returned in the first theorem. *)
paulson@22731
   245
fun declare_absfuns s th =
paulson@22731
   246
  let val nref = ref 0
paulson@22731
   247
      fun abstract thy ct =
paulson@20445
   248
        if lambda_free (term_of ct) then (transfer thy (reflexive ct), [])
paulson@20445
   249
        else
paulson@20445
   250
        case term_of ct of
paulson@20710
   251
          Abs _ =>
paulson@22731
   252
            let val cname = Name.internal ("llabs_" ^ s ^ "_" ^ Int.toString (inc nref))
wenzelm@20461
   253
                val _ = assert_eta_free ct;
paulson@20710
   254
                val (cvs,cta) = dest_abs_list ct
paulson@20710
   255
                val (vs,Tvs) = ListPair.unzip (map (dest_Free o term_of) cvs)
paulson@24215
   256
                val _ = Output.debug (fn()=>"Nested lambda: " ^ string_of_cterm cta);
wenzelm@20461
   257
                val (u'_th,defs) = abstract thy cta
paulson@24215
   258
                val _ = Output.debug (fn()=>"Returned " ^ string_of_thm u'_th);
wenzelm@22902
   259
                val cu' = Thm.rhs_of u'_th
paulson@20863
   260
                val u' = term_of cu'
paulson@20863
   261
                val abs_v_u = lambda_list (map term_of cvs) u'
wenzelm@20461
   262
                (*get the formal parameters: ALL variables free in the term*)
wenzelm@20461
   263
                val args = term_frees abs_v_u
paulson@24215
   264
                val _ = Output.debug (fn()=>Int.toString (length args) ^ " arguments");
wenzelm@20461
   265
                val rhs = list_abs_free (map dest_Free args, abs_v_u)
wenzelm@20461
   266
                      (*Forms a lambda-abstraction over the formal parameters*)
paulson@24215
   267
                val _ = Output.debug (fn()=>"Looking up " ^ string_of_cterm cu');
paulson@20969
   268
                val thy = theory_of_thm u'_th
paulson@20863
   269
                val (ax,ax',thy) =
paulson@20969
   270
                 case List.mapPartial (match_rhs thy abs_v_u) 
paulson@20969
   271
                         (Net.match_term (!abstraction_cache) u') of
paulson@20863
   272
                     (ax,ax')::_ => 
paulson@24215
   273
                       (Output.debug (fn()=>"Re-using axiom " ^ string_of_thm ax);
paulson@20863
   274
                        (ax,ax',thy))
paulson@20863
   275
                   | [] =>
paulson@24215
   276
                      let val _ = Output.debug (fn()=>"Lookup was empty");
paulson@20863
   277
                          val Ts = map type_of args
paulson@20710
   278
                          val cT = Ts ---> (Tvs ---> typ_of (ctyp_of_term cu'))
wenzelm@20461
   279
                          val c = Const (Sign.full_name thy cname, cT)
wenzelm@20783
   280
                          val thy = Sign.add_consts_authentic [(cname, cT, NoSyn)] thy
wenzelm@20461
   281
                                     (*Theory is augmented with the constant,
wenzelm@20461
   282
                                       then its definition*)
wenzelm@20461
   283
                          val cdef = cname ^ "_def"
wenzelm@20461
   284
                          val thy = Theory.add_defs_i false false
wenzelm@20461
   285
                                       [(cdef, equals cT $ c $ rhs)] thy
paulson@24215
   286
                          val _ = Output.debug (fn()=> "Definition is " ^ string_of_thm (get_axiom thy cdef));
paulson@20863
   287
                          val ax = get_axiom thy cdef |> freeze_thm
paulson@20863
   288
                                     |> mk_object_eq |> strip_lambdas (length args)
paulson@20863
   289
                                     |> mk_meta_eq |> Meson.generalize
paulson@20969
   290
                          val (_,ax') = Option.valOf (match_rhs thy abs_v_u ax)
paulson@24215
   291
                          val _ = Output.debug (fn()=> "Declaring: " ^ string_of_thm ax ^ "\n" ^
paulson@24215
   292
                                                       "Instance: " ^ string_of_thm ax');
paulson@20863
   293
                          val _ = abstraction_cache := Net.insert_term eq_absdef 
paulson@20863
   294
                                            ((Logic.varify u'), ax) (!abstraction_cache)
wenzelm@20461
   295
                            handle Net.INSERT =>
wenzelm@20461
   296
                              raise THM ("declare_absfuns: INSERT", 0, [th,u'_th,ax])
paulson@20863
   297
                       in  (ax,ax',thy)  end
paulson@24215
   298
            in Output.debug (fn()=>"Lookup result: " ^ string_of_thm ax');
paulson@20863
   299
               (transitive (abstract_rule_list vs cvs u'_th) (symmetric ax'), ax::defs) end
wenzelm@20461
   300
        | (t1$t2) =>
wenzelm@20461
   301
            let val (ct1,ct2) = Thm.dest_comb ct
wenzelm@20461
   302
                val (th1,defs1) = abstract thy ct1
wenzelm@20461
   303
                val (th2,defs2) = abstract (theory_of_thm th1) ct2
wenzelm@20461
   304
            in  (combination th1 th2, defs1@defs2)  end
paulson@24215
   305
      val _ = Output.debug (fn()=>"declare_absfuns, Abstracting: " ^ string_of_thm th);
paulson@20419
   306
      val (eqth,defs) = abstract (theory_of_thm th) (cprop_of th)
paulson@20863
   307
      val ths = equal_elim eqth th :: map (strip_lambdas ~1 o mk_object_eq o freeze_thm) defs
paulson@24215
   308
      val _ = Output.debug (fn()=>"declare_absfuns, Result: " ^ string_of_thm (hd ths));
paulson@20863
   309
  in  (theory_of_thm eqth, map Drule.eta_contraction_rule ths)  end;
paulson@20419
   310
wenzelm@20902
   311
fun name_of def = try (#1 o dest_Free o lhs_of) def;
paulson@20567
   312
paulson@20525
   313
(*A name is valid provided it isn't the name of a defined abstraction.*)
paulson@20567
   314
fun valid_name defs (Free(x,T)) = not (x mem_string (List.mapPartial name_of defs))
paulson@20525
   315
  | valid_name defs _ = false;
paulson@20525
   316
paulson@22731
   317
(*s is the theorem name (hint) or the word "subgoal"*)
paulson@22731
   318
fun assume_absfuns s th =
paulson@20445
   319
  let val thy = theory_of_thm th
paulson@20445
   320
      val cterm = cterm_of thy
paulson@22724
   321
      val abs_count = ref 0
paulson@20525
   322
      fun abstract ct =
paulson@20445
   323
        if lambda_free (term_of ct) then (reflexive ct, [])
paulson@20445
   324
        else
paulson@20445
   325
        case term_of ct of
paulson@20419
   326
          Abs (_,T,u) =>
paulson@20710
   327
            let val _ = assert_eta_free ct;
paulson@20710
   328
                val (cvs,cta) = dest_abs_list ct
paulson@20710
   329
                val (vs,Tvs) = ListPair.unzip (map (dest_Free o term_of) cvs)
paulson@20525
   330
                val (u'_th,defs) = abstract cta
wenzelm@22902
   331
                val cu' = Thm.rhs_of u'_th
paulson@20863
   332
                val u' = term_of cu'
paulson@20710
   333
                (*Could use Thm.cabs instead of lambda to work at level of cterms*)
paulson@20710
   334
                val abs_v_u = lambda_list (map term_of cvs) (term_of cu')
paulson@20525
   335
                (*get the formal parameters: free variables not present in the defs
paulson@20525
   336
                  (to avoid taking abstraction function names as parameters) *)
paulson@20710
   337
                val args = filter (valid_name defs) (term_frees abs_v_u)
paulson@20710
   338
                val crhs = list_cabs (map cterm args, cterm abs_v_u)
wenzelm@20461
   339
                      (*Forms a lambda-abstraction over the formal parameters*)
wenzelm@20461
   340
                val rhs = term_of crhs
paulson@20863
   341
                val (ax,ax') =
paulson@20969
   342
                 case List.mapPartial (match_rhs thy abs_v_u) 
paulson@20863
   343
                        (Net.match_term (!abstraction_cache) u') of
paulson@20863
   344
                     (ax,ax')::_ => 
paulson@24215
   345
                       (Output.debug (fn()=>"Re-using axiom " ^ string_of_thm ax);
paulson@20863
   346
                        (ax,ax'))
paulson@20863
   347
                   | [] =>
wenzelm@20461
   348
                      let val Ts = map type_of args
paulson@20710
   349
                          val const_ty = Ts ---> (Tvs ---> typ_of (ctyp_of_term cu'))
paulson@22731
   350
                          val id = "llabs_" ^ s ^ "_" ^ Int.toString (inc abs_count)
paulson@22724
   351
                          val c = Free (id, const_ty)
wenzelm@20461
   352
                          val ax = assume (Thm.capply (cterm (equals const_ty $ c)) crhs)
paulson@20863
   353
                                     |> mk_object_eq |> strip_lambdas (length args)
paulson@20863
   354
                                     |> mk_meta_eq |> Meson.generalize
paulson@20969
   355
                          val (_,ax') = Option.valOf (match_rhs thy abs_v_u ax)
wenzelm@20461
   356
                          val _ = abstraction_cache := Net.insert_term eq_absdef (rhs,ax)
wenzelm@20461
   357
                                    (!abstraction_cache)
wenzelm@20461
   358
                            handle Net.INSERT =>
wenzelm@20461
   359
                              raise THM ("assume_absfuns: INSERT", 0, [th,u'_th,ax])
paulson@20863
   360
                      in (ax,ax') end
paulson@24215
   361
            in Output.debug (fn()=>"Lookup result: " ^ string_of_thm ax');
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
paulson@20525
   365
                val (t1',defs1) = abstract ct1
paulson@20525
   366
                val (t2',defs2) = abstract ct2
wenzelm@20461
   367
            in  (combination t1' t2', defs1@defs2)  end
paulson@24215
   368
      val _ = Output.debug (fn()=>"assume_absfuns, Abstracting: " ^ string_of_thm th);
paulson@20525
   369
      val (eqth,defs) = abstract (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@24215
   371
      val _ = Output.debug (fn()=>"assume_absfuns, Result: " ^ string_of_thm (hd ths));
paulson@20863
   372
  in  map Drule.eta_contraction_rule ths  end;
paulson@20419
   373
paulson@16009
   374
paulson@16009
   375
(*cterms are used throughout for efficiency*)
paulson@18141
   376
val cTrueprop = Thm.cterm_of HOL.thy HOLogic.Trueprop;
paulson@16009
   377
paulson@16009
   378
(*cterm version of mk_cTrueprop*)
paulson@16009
   379
fun c_mkTrueprop A = Thm.capply cTrueprop A;
paulson@16009
   380
paulson@16009
   381
(*Given an abstraction over n variables, replace the bound variables by free
paulson@16009
   382
  ones. Return the body, along with the list of free variables.*)
wenzelm@20461
   383
fun c_variant_abs_multi (ct0, vars) =
paulson@16009
   384
      let val (cv,ct) = Thm.dest_abs NONE ct0
paulson@16009
   385
      in  c_variant_abs_multi (ct, cv::vars)  end
paulson@16009
   386
      handle CTERM _ => (ct0, rev vars);
paulson@16009
   387
wenzelm@20461
   388
(*Given the definition of a Skolem function, return a theorem to replace
wenzelm@20461
   389
  an existential formula by a use of that function.
paulson@18141
   390
   Example: "EX x. x : A & x ~: B ==> sko A B : A & sko A B ~: B"  [.] *)
wenzelm@20461
   391
fun skolem_of_def def =
wenzelm@22902
   392
  let val (c,rhs) = Thm.dest_equals (cprop_of (freeze_thm def))
paulson@16009
   393
      val (ch, frees) = c_variant_abs_multi (rhs, [])
paulson@18141
   394
      val (chilbert,cabs) = Thm.dest_comb ch
wenzelm@22596
   395
      val {thy,t, ...} = rep_cterm chilbert
paulson@18141
   396
      val T = case t of Const ("Hilbert_Choice.Eps", Type("fun",[_,T])) => T
paulson@18141
   397
                      | _ => raise THM ("skolem_of_def: expected Eps", 0, [def])
wenzelm@22596
   398
      val cex = Thm.cterm_of thy (HOLogic.exists_const T)
paulson@16009
   399
      val ex_tm = c_mkTrueprop (Thm.capply cex cabs)
paulson@16009
   400
      and conc =  c_mkTrueprop (Drule.beta_conv cabs (Drule.list_comb(c,frees)));
paulson@18141
   401
      fun tacf [prem] = rewrite_goals_tac [def] THEN rtac (prem RS someI_ex) 1
wenzelm@23352
   402
  in  Goal.prove_internal [ex_tm] conc tacf
paulson@18141
   403
       |> forall_intr_list frees
paulson@18141
   404
       |> forall_elim_vars 0  (*Introduce Vars, but don't discharge defs.*)
paulson@18141
   405
       |> Thm.varifyT
paulson@18141
   406
  end;
paulson@16009
   407
paulson@20863
   408
(*Converts an Isabelle theorem (intro, elim or simp format, even higher-order) into NNF.*)
wenzelm@20461
   409
fun to_nnf th =
wenzelm@21254
   410
    th |> transfer_to_ATP_Linkup
paulson@20863
   411
       |> transform_elim |> zero_var_indexes |> freeze_thm
wenzelm@24300
   412
       |> Conv.fconv_rule ObjectLogic.atomize |> Meson.make_nnf |> strip_lambdas ~1;
paulson@16009
   413
paulson@18141
   414
(*Generate Skolem functions for a theorem supplied in nnf*)
paulson@22731
   415
fun skolem_of_nnf s th =
paulson@22731
   416
  map (skolem_of_def o assume o (cterm_of (theory_of_thm th))) (assume_skofuns s th);
paulson@18141
   417
paulson@20863
   418
fun assert_lambda_free ths msg = 
paulson@20863
   419
  case filter (not o lambda_free o prop_of) ths of
paulson@20863
   420
      [] => ()
paulson@22731
   421
    | ths' => error (msg ^ "\n" ^ cat_lines (map string_of_thm ths'));
paulson@20457
   422
paulson@22731
   423
fun assume_abstract s th =
paulson@20457
   424
  if lambda_free (prop_of th) then [th]
paulson@22731
   425
  else th |> Drule.eta_contraction_rule |> assume_absfuns s
paulson@20457
   426
          |> tap (fn ths => assert_lambda_free ths "assume_abstract: lambdas")
paulson@20445
   427
paulson@20419
   428
(*Replace lambdas by assumed function definitions in the theorems*)
paulson@22731
   429
fun assume_abstract_list s ths =
paulson@22731
   430
  if abstract_lambdas then List.concat (map (assume_abstract s) ths)
paulson@20863
   431
  else map Drule.eta_contraction_rule ths;
paulson@20419
   432
paulson@20419
   433
(*Replace lambdas by declared function definitions in the theorems*)
paulson@22731
   434
fun declare_abstract' s (thy, []) = (thy, [])
paulson@22731
   435
  | declare_abstract' s (thy, th::ths) =
wenzelm@20461
   436
      let val (thy', th_defs) =
paulson@20457
   437
            if lambda_free (prop_of th) then (thy, [th])
paulson@20445
   438
            else
paulson@20863
   439
                th |> zero_var_indexes |> freeze_thm
paulson@22731
   440
                   |> Drule.eta_contraction_rule |> transfer thy |> declare_absfuns s
wenzelm@20461
   441
          val _ = assert_lambda_free th_defs "declare_abstract: lambdas"
paulson@22731
   442
          val (thy'', ths') = declare_abstract' s (thy', ths)
paulson@20419
   443
      in  (thy'', th_defs @ ths')  end;
paulson@20419
   444
paulson@22731
   445
fun declare_abstract s (thy, ths) =
paulson@22731
   446
  if abstract_lambdas then declare_abstract' s (thy, ths)
paulson@20863
   447
  else (thy, map Drule.eta_contraction_rule ths);
paulson@20419
   448
paulson@21071
   449
(*Keep the full complexity of the original name*)
wenzelm@21858
   450
fun flatten_name s = space_implode "_X" (NameSpace.explode s);
paulson@21071
   451
paulson@22731
   452
fun fake_name th =
paulson@22731
   453
  if PureThy.has_name_hint th then flatten_name (PureThy.get_name_hint th) 
paulson@22731
   454
  else gensym "unknown_thm_";
paulson@22731
   455
paulson@22731
   456
(*Skolemize a named theorem, with Skolem functions as additional premises.*)
paulson@22731
   457
fun skolem_thm th =
paulson@22731
   458
  let val nnfth = to_nnf th and s = fake_name th
paulson@22731
   459
  in  Meson.make_cnf (skolem_of_nnf s nnfth) nnfth |> assume_abstract_list s |> Meson.finish_cnf
paulson@22731
   460
  end
paulson@22731
   461
  handle THM _ => [];
paulson@22731
   462
paulson@18510
   463
(*Declare Skolem functions for a theorem, supplied in nnf and with its name.
paulson@18510
   464
  It returns a modified theory, unless skolemization fails.*)
paulson@22471
   465
fun skolem thy th =
paulson@22731
   466
     Option.map
paulson@22731
   467
        (fn (nnfth, s) =>
paulson@22731
   468
          let val _ = Output.debug (fn () => "skolemizing " ^ s ^ ": ")
paulson@22731
   469
              val (thy',defs) = declare_skofuns s nnfth thy
paulson@20419
   470
              val cnfs = Meson.make_cnf (map skolem_of_def defs) nnfth
paulson@22731
   471
              val (thy'',cnfs') = declare_abstract s (thy',cnfs)
paulson@22345
   472
          in (map Goal.close_result (Meson.finish_cnf cnfs'), thy'')
paulson@20419
   473
          end)
paulson@22731
   474
      (SOME (to_nnf th, fake_name th)  handle THM _ => NONE);
paulson@16009
   475
paulson@22516
   476
structure ThmCache = TheoryDataFun
wenzelm@22846
   477
(
paulson@22516
   478
  type T = (thm list) Thmtab.table ref;
paulson@22516
   479
  val empty : T = ref Thmtab.empty;
paulson@22516
   480
  fun copy (ref tab) : T = ref tab;
paulson@22516
   481
  val extend = copy;
paulson@22516
   482
  fun merge _ (ref tab1, ref tab2) : T = ref (Thmtab.merge (K true) (tab1, tab2));
wenzelm@22846
   483
);
paulson@22516
   484
paulson@22516
   485
(*The cache prevents repeated clausification of a theorem, and also repeated declaration of 
paulson@22516
   486
  Skolem functions. The global one holds theorems proved prior to this point. Theory data
paulson@22516
   487
  holds the remaining ones.*)
paulson@22516
   488
val global_clause_cache = ref (Thmtab.empty : (thm list) Thmtab.table);
paulson@22516
   489
paulson@18510
   490
(*Populate the clause cache using the supplied theorem. Return the clausal form
paulson@18510
   491
  and modified theory.*)
paulson@22516
   492
fun skolem_cache_thm clause_cache th thy =
paulson@22471
   493
  case Thmtab.lookup (!clause_cache) th of
wenzelm@20461
   494
      NONE =>
paulson@22471
   495
        (case skolem thy (Thm.transfer thy th) of
wenzelm@20461
   496
             NONE => ([th],thy)
paulson@22345
   497
           | SOME (cls,thy') => 
paulson@22471
   498
                 (if null cls 
paulson@22471
   499
                  then warning ("skolem_cache: empty clause set for " ^ string_of_thm th)
paulson@20473
   500
                  else ();
paulson@22471
   501
                  change clause_cache (Thmtab.update (th, cls)); 
paulson@22345
   502
                  (cls,thy')))
paulson@22471
   503
    | SOME cls => (cls,thy);
wenzelm@20461
   504
wenzelm@20461
   505
(*Exported function to convert Isabelle theorems into axiom clauses*)
paulson@22471
   506
fun cnf_axiom th =
paulson@22516
   507
  let val cache = ThmCache.get (Thm.theory_of_thm th)
paulson@22516
   508
                  handle ERROR _ => global_clause_cache
paulson@22516
   509
      val in_cache = if cache = global_clause_cache then NONE else Thmtab.lookup (!cache) th
paulson@22516
   510
  in
paulson@22516
   511
     case in_cache of
paulson@22516
   512
       NONE => 
paulson@22516
   513
	 (case Thmtab.lookup (!global_clause_cache) th of
paulson@22516
   514
	   NONE => 
paulson@22516
   515
	     let val cls = map Goal.close_result (skolem_thm th)
paulson@22724
   516
	     in Output.debug (fn () => Int.toString (length cls) ^ " clauses inserted into cache: " ^ 
paulson@22724
   517
	                         (if PureThy.has_name_hint th then PureThy.get_name_hint th
paulson@22724
   518
	                          else string_of_thm th));
paulson@22516
   519
		change cache (Thmtab.update (th, cls)); cls 
paulson@22516
   520
	     end
paulson@22516
   521
	 | SOME cls => cls)
paulson@22516
   522
     | SOME cls => cls
paulson@22516
   523
  end;
paulson@15347
   524
wenzelm@21646
   525
fun pairname th = (PureThy.get_name_hint th, th);
paulson@18141
   526
paulson@15872
   527
(**** Extract and Clausify theorems from a theory's claset and simpset ****)
paulson@15347
   528
paulson@17484
   529
fun rules_of_claset cs =
paulson@17484
   530
  let val {safeIs,safeEs,hazIs,hazEs,...} = rep_cs cs
paulson@19175
   531
      val intros = safeIs @ hazIs
wenzelm@18532
   532
      val elims  = map Classical.classical_rule (safeEs @ hazEs)
paulson@17404
   533
  in
wenzelm@22130
   534
     Output.debug (fn () => "rules_of_claset intros: " ^ Int.toString(length intros) ^
paulson@17484
   535
            " elims: " ^ Int.toString(length elims));
paulson@20017
   536
     map pairname (intros @ elims)
paulson@17404
   537
  end;
paulson@15347
   538
paulson@17484
   539
fun rules_of_simpset ss =
paulson@17484
   540
  let val ({rules,...}, _) = rep_ss ss
paulson@17484
   541
      val simps = Net.entries rules
wenzelm@20461
   542
  in
wenzelm@22130
   543
    Output.debug (fn () => "rules_of_simpset: " ^ Int.toString(length simps));
wenzelm@22130
   544
    map (fn r => (#name r, #thm r)) simps
paulson@17484
   545
  end;
paulson@17484
   546
wenzelm@21505
   547
fun claset_rules_of ctxt = rules_of_claset (local_claset_of ctxt);
wenzelm@21505
   548
fun simpset_rules_of ctxt = rules_of_simpset (local_simpset_of ctxt);
mengj@19196
   549
wenzelm@24042
   550
fun atpset_rules_of ctxt = map pairname (ResAtpset.get ctxt);
wenzelm@20774
   551
paulson@15347
   552
paulson@22471
   553
(**** Translate a set of theorems into CNF ****)
paulson@15347
   554
paulson@19894
   555
(* classical rules: works for both FOL and HOL *)
paulson@19894
   556
fun cnf_rules [] err_list = ([],err_list)
wenzelm@20461
   557
  | cnf_rules ((name,th) :: ths) err_list =
paulson@19894
   558
      let val (ts,es) = cnf_rules ths err_list
paulson@22471
   559
      in  (cnf_axiom th :: ts,es) handle  _ => (ts, (th::es))  end;
paulson@15347
   560
paulson@19894
   561
fun pair_name_cls k (n, []) = []
paulson@19894
   562
  | pair_name_cls k (n, cls::clss) = (cls, (n,k)) :: pair_name_cls (k+1) (n, clss)
wenzelm@20461
   563
paulson@19894
   564
fun cnf_rules_pairs_aux pairs [] = pairs
paulson@19894
   565
  | cnf_rules_pairs_aux pairs ((name,th)::ths) =
paulson@22471
   566
      let val pairs' = (pair_name_cls 0 (name, cnf_axiom th)) @ pairs
wenzelm@20461
   567
                       handle THM _ => pairs | ResClause.CLAUSE _ => pairs
paulson@19894
   568
      in  cnf_rules_pairs_aux pairs' ths  end;
wenzelm@20461
   569
paulson@21290
   570
(*The combination of rev and tail recursion preserves the original order*)
paulson@21290
   571
fun cnf_rules_pairs l = cnf_rules_pairs_aux [] (rev l);
mengj@19353
   572
mengj@19196
   573
mengj@18198
   574
(**** Convert all theorems of a claset/simpset into clauses (ResClause.clause, or ResHolClause.clause) ****)
paulson@15347
   575
paulson@20419
   576
(*Setup function: takes a theory and installs ALL known theorems into the clause cache*)
paulson@20457
   577
paulson@22516
   578
fun skolem_cache clause_cache th thy = #2 (skolem_cache_thm clause_cache th thy);
paulson@20457
   579
paulson@22516
   580
(*The cache can be kept smaller by inspecting the prop of each thm. Can ignore all that are
paulson@22516
   581
  lambda_free, but then the individual theory caches become much bigger.*)
paulson@21071
   582
paulson@22516
   583
fun clause_cache_setup thy = 
paulson@22516
   584
  fold (skolem_cache global_clause_cache) (map #2 (PureThy.all_thms_of thy)) thy;
wenzelm@20461
   585
paulson@16563
   586
paulson@16563
   587
(*** meson proof methods ***)
paulson@16563
   588
paulson@22516
   589
fun cnf_rules_of_ths ths = List.concat (map cnf_axiom ths);
paulson@16563
   590
paulson@22731
   591
(*Expand all new*definitions of abstraction or Skolem functions in a proof state.*)
paulson@22731
   592
fun is_absko (Const ("==", _) $ Free (a,_) $ u) = String.isPrefix "llabs_" a orelse String.isPrefix "sko_" a
paulson@22731
   593
  | is_absko _ = false;
paulson@22731
   594
paulson@22731
   595
fun is_okdef xs (Const ("==", _) $ t $ u) =   (*Definition of Free, not in certain terms*)
paulson@22731
   596
      is_Free t andalso not (member (op aconv) xs t)
paulson@22731
   597
  | is_okdef _ _ = false
paulson@22724
   598
paulson@24215
   599
(*This function tries to cope with open locales, which introduce hypotheses of the form
paulson@24215
   600
  Free == t, conjecture clauses, which introduce various hypotheses, and also definitions
paulson@24215
   601
  of llabs_ and sko_ functions. *)
paulson@22731
   602
fun expand_defs_tac st0 st =
paulson@22731
   603
  let val hyps0 = #hyps (rep_thm st0)
paulson@22731
   604
      val hyps = #hyps (crep_thm st)
paulson@22731
   605
      val newhyps = filter_out (member (op aconv) hyps0 o Thm.term_of) hyps
paulson@22731
   606
      val defs = filter (is_absko o Thm.term_of) newhyps
paulson@22731
   607
      val remaining_hyps = filter_out (member (op aconv) (map Thm.term_of defs)) 
paulson@22731
   608
                                      (map Thm.term_of hyps)
paulson@22731
   609
      val fixed = term_frees (concl_of st) @
paulson@22731
   610
                  foldl (gen_union (op aconv)) [] (map term_frees remaining_hyps)
paulson@22731
   611
  in  Output.debug (fn _ => "expand_defs_tac: " ^ string_of_thm st);
paulson@22731
   612
      Output.debug (fn _ => "  st0: " ^ string_of_thm st0);
paulson@22731
   613
      Output.debug (fn _ => "  defs: " ^ commas (map string_of_cterm defs));
paulson@22731
   614
      Seq.of_list [LocalDefs.expand (filter (is_okdef fixed o Thm.term_of) defs) st]
paulson@22731
   615
  end;
paulson@22724
   616
paulson@22731
   617
paulson@22731
   618
fun meson_general_tac ths i st0 =
paulson@22731
   619
 let val _ = Output.debug (fn () => "Meson called: " ^ cat_lines (map string_of_thm ths))
paulson@22731
   620
 in  (Meson.meson_claset_tac (cnf_rules_of_ths ths) HOL_cs i THEN expand_defs_tac st0) st0 end;
paulson@22724
   621
wenzelm@21588
   622
val meson_method_setup = Method.add_methods
wenzelm@21588
   623
  [("meson", Method.thms_args (fn ths =>
paulson@22724
   624
      Method.SIMPLE_METHOD' (CHANGED_PROP o meson_general_tac ths)),
wenzelm@21588
   625
    "MESON resolution proof procedure")];
paulson@15347
   626
paulson@21102
   627
(** Attribute for converting a theorem into clauses **)
paulson@18510
   628
paulson@22471
   629
fun meta_cnf_axiom th = map Meson.make_meta_clause (cnf_axiom th);
paulson@18510
   630
paulson@21102
   631
fun clausify_rule (th,i) = List.nth (meta_cnf_axiom th, i)
paulson@21102
   632
paulson@21102
   633
val clausify = Attrib.syntax (Scan.lift Args.nat
paulson@21102
   634
  >> (fn i => Thm.rule_attribute (fn _ => fn th => clausify_rule (th, i))));
paulson@21102
   635
paulson@21999
   636
paulson@21999
   637
(*** Converting a subgoal into negated conjecture clauses. ***)
paulson@21999
   638
wenzelm@24300
   639
val neg_skolemize_tac = EVERY' [rtac ccontr, ObjectLogic.atomize_prems_tac, Meson.skolemize_tac];
paulson@22471
   640
paulson@22471
   641
(*finish_cnf removes tautologies and functional reflexivity axioms, but by calling Thm.varifyT
paulson@22644
   642
  it can introduce TVars, which are useless in conjecture clauses.*)
paulson@22644
   643
val no_tvars = null o term_tvars o prop_of;
paulson@22644
   644
wenzelm@24300
   645
val neg_clausify =
wenzelm@24300
   646
  filter no_tvars o Meson.finish_cnf o assume_abstract_list "subgoal" o Meson.make_clauses;
paulson@21999
   647
paulson@21999
   648
fun neg_conjecture_clauses st0 n =
paulson@21999
   649
  let val st = Seq.hd (neg_skolemize_tac n st0)
paulson@21999
   650
      val (params,_,_) = strip_context (Logic.nth_prem (n, Thm.prop_of st))
paulson@22516
   651
  in (neg_clausify (Option.valOf (metahyps_thms n st)), params) end
paulson@22516
   652
  handle Option => raise ERROR "unable to Skolemize subgoal";
paulson@21999
   653
paulson@21999
   654
(*Conversion of a subgoal to conjecture clauses. Each clause has  
paulson@21999
   655
  leading !!-bound universal variables, to express generality. *)
paulson@21999
   656
val neg_clausify_tac = 
paulson@21999
   657
  neg_skolemize_tac THEN' 
paulson@21999
   658
  SUBGOAL
paulson@21999
   659
    (fn (prop,_) =>
paulson@21999
   660
     let val ts = Logic.strip_assums_hyp prop
paulson@21999
   661
     in EVERY1 
paulson@21999
   662
	 [METAHYPS
paulson@21999
   663
	    (fn hyps => 
paulson@21999
   664
              (Method.insert_tac
paulson@21999
   665
                (map forall_intr_vars (neg_clausify hyps)) 1)),
paulson@21999
   666
	  REPEAT_DETERM_N (length ts) o (etac thin_rl)]
paulson@21999
   667
     end);
paulson@21999
   668
paulson@21102
   669
(** The Skolemization attribute **)
paulson@18510
   670
paulson@18510
   671
fun conj2_rule (th1,th2) = conjI OF [th1,th2];
paulson@18510
   672
paulson@20457
   673
(*Conjoin a list of theorems to form a single theorem*)
paulson@20457
   674
fun conj_rule []  = TrueI
paulson@20445
   675
  | conj_rule ths = foldr1 conj2_rule ths;
paulson@18510
   676
paulson@20419
   677
fun skolem_attr (Context.Theory thy, th) =
paulson@22516
   678
      let val (cls, thy') = skolem_cache_thm (ThmCache.get thy) th thy
wenzelm@18728
   679
      in (Context.Theory thy', conj_rule cls) end
paulson@22724
   680
  | skolem_attr (context, th) = (context, th)
paulson@18510
   681
paulson@18510
   682
val setup_attrs = Attrib.add_attributes
paulson@21102
   683
  [("skolem", Attrib.no_args skolem_attr, "skolemization of a theorem"),
paulson@21999
   684
   ("clausify", clausify, "conversion of theorem to clauses")];
paulson@21999
   685
paulson@21999
   686
val setup_methods = Method.add_methods
paulson@21999
   687
  [("neg_clausify", Method.no_args (Method.SIMPLE_METHOD' neg_clausify_tac), 
paulson@21999
   688
    "conversion of goal to conjecture clauses")];
paulson@21102
   689
     
paulson@22516
   690
val setup = clause_cache_setup #> ThmCache.init #> setup_attrs #> setup_methods;
paulson@18510
   691
wenzelm@20461
   692
end;