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