src/HOL/Tools/Sledgehammer/sledgehammer_fact_preprocessor.ML
author blanchet
Tue Jun 22 16:23:29 2010 +0200 (2010-06-22 ago)
changeset 37500 7587b6e63454
parent 37498 b426cbdb5a23
child 37511 26afa11a1fb2
permissions -rw-r--r--
thread original theorem along with CNF theorem, as a step toward killing the Skolem cache
     1 (*  Title:      HOL/Tools/Sledgehammer/sledgehammer_fact_preprocessor.ML
     2     Author:     Jia Meng, Cambridge University Computer Laboratory
     3     Author:     Jasmin Blanchette, TU Muenchen
     4 
     5 Transformation of axiom rules (elim/intro/etc) into CNF forms.
     6 *)
     7 
     8 signature SLEDGEHAMMER_FACT_PREPROCESSOR =
     9 sig
    10   type cnf_thm = thm * ((string * int) * thm)
    11   val chained_prefix: string
    12   val trace: bool Unsynchronized.ref
    13   val trace_msg: (unit -> string) -> unit
    14   val skolem_theory_name: string
    15   val skolem_prefix: string
    16   val skolem_infix: string
    17   val is_skolem_const_name: string -> bool
    18   val cnf_axiom: theory -> thm -> thm list
    19   val multi_base_blacklist: string list
    20   val is_theorem_bad_for_atps: thm -> bool
    21   val type_has_topsort: typ -> bool
    22   val cnf_rules_pairs : theory -> (string * thm) list -> cnf_thm list
    23   val saturate_skolem_cache: theory -> theory option
    24   val auto_saturate_skolem_cache: bool Unsynchronized.ref
    25     (* for emergency use where the Skolem cache causes problems *)
    26   val neg_clausify: thm -> thm list
    27   val neg_conjecture_clauses:
    28     Proof.context -> thm -> int -> thm list list * (string * typ) list
    29   val neg_clausify_tac: Proof.context -> int -> tactic
    30   val setup: theory -> theory
    31 end;
    32 
    33 structure Sledgehammer_Fact_Preprocessor : SLEDGEHAMMER_FACT_PREPROCESSOR =
    34 struct
    35 
    36 open Sledgehammer_FOL_Clause
    37 
    38 type cnf_thm = thm * ((string * int) * thm)
    39 
    40 (* Used to label theorems chained into the goal. *)
    41 val chained_prefix = "Sledgehammer.chained_"
    42 
    43 val trace = Unsynchronized.ref false;
    44 fun trace_msg msg = if !trace then tracing (msg ()) else ();
    45 
    46 val skolem_theory_name = "Sledgehammer.Sko"
    47 val skolem_prefix = "sko_"
    48 val skolem_infix = "$"
    49 
    50 fun freeze_thm th = #1 (Drule.legacy_freeze_thaw th);
    51 
    52 val type_has_topsort = Term.exists_subtype
    53   (fn TFree (_, []) => true
    54     | TVar (_, []) => true
    55     | _ => false);
    56 
    57 
    58 (**** Transformation of Elimination Rules into First-Order Formulas****)
    59 
    60 val cfalse = cterm_of @{theory HOL} HOLogic.false_const;
    61 val ctp_false = cterm_of @{theory HOL} (HOLogic.mk_Trueprop HOLogic.false_const);
    62 
    63 (*Converts an elim-rule into an equivalent theorem that does not have the
    64   predicate variable.  Leaves other theorems unchanged.  We simply instantiate the
    65   conclusion variable to False.*)
    66 fun transform_elim th =
    67   case concl_of th of    (*conclusion variable*)
    68        @{const Trueprop} $ (v as Var (_, @{typ bool})) =>
    69            Thm.instantiate ([], [(cterm_of @{theory HOL} v, cfalse)]) th
    70     | v as Var(_, @{typ prop}) =>
    71            Thm.instantiate ([], [(cterm_of @{theory HOL} v, ctp_false)]) th
    72     | _ => th;
    73 
    74 (*To enforce single-threading*)
    75 exception Clausify_failure of theory;
    76 
    77 
    78 (**** SKOLEMIZATION BY INFERENCE (lcp) ****)
    79 
    80 (*Keep the full complexity of the original name*)
    81 fun flatten_name s = space_implode "_X" (Long_Name.explode s);
    82 
    83 fun skolem_name thm_name j var_name =
    84   skolem_prefix ^ thm_name ^ "_" ^ Int.toString j ^
    85   skolem_infix ^ (if var_name = "" then "g" else flatten_name var_name)
    86 
    87 (* Hack: Could return false positives (e.g., a user happens to declare a
    88    constant called "SomeTheory.sko_means_shoe_in_$wedish". *)
    89 val is_skolem_const_name =
    90   Long_Name.base_name
    91   #> String.isPrefix skolem_prefix andf String.isSubstring skolem_infix
    92 
    93 fun rhs_extra_types lhsT rhs =
    94   let val lhs_vars = Term.add_tfreesT lhsT []
    95       fun add_new_TFrees (TFree v) =
    96             if member (op =) lhs_vars v then I else insert (op =) (TFree v)
    97         | add_new_TFrees _ = I
    98       val rhs_consts = fold_aterms (fn Const c => insert (op =) c | _ => I) rhs []
    99   in fold (#2 #> Term.fold_atyps add_new_TFrees) rhs_consts [] end;
   100 
   101 fun skolem_type_and_args bound_T body =
   102   let
   103     val args1 = OldTerm.term_frees body
   104     val Ts1 = map type_of args1
   105     val Ts2 = rhs_extra_types (Ts1 ---> bound_T) body
   106     val args2 = map (fn T => Free (gensym "vsk", T)) Ts2
   107   in (Ts2 ---> Ts1 ---> bound_T, args2 @ args1) end
   108 
   109 (* Traverse a theorem, declaring Skolem function definitions. String "s" is the
   110    suggested prefix for the Skolem constants. *)
   111 fun declare_skolem_funs s th thy =
   112   let
   113     val skolem_count = Unsynchronized.ref 0    (* FIXME ??? *)
   114     fun dec_sko (Const (@{const_name Ex}, _) $ (body as Abs (s', T, p)))
   115                 (axs, thy) =
   116         (*Existential: declare a Skolem function, then insert into body and continue*)
   117         let
   118           val id = skolem_name s (Unsynchronized.inc skolem_count) s'
   119           val (cT, args) = skolem_type_and_args T body
   120           val rhs = list_abs_free (map dest_Free args,
   121                                    HOLogic.choice_const T $ body)
   122                   (*Forms a lambda-abstraction over the formal parameters*)
   123           val (c, thy) =
   124             Sign.declare_const ((Binding.conceal (Binding.name id), cT), NoSyn) thy
   125           val cdef = id ^ "_def"
   126           val ((_, ax), thy) =
   127             Thm.add_def true false (Binding.name cdef, Logic.mk_equals (c, rhs)) thy
   128           val ax' = Drule.export_without_context ax
   129         in dec_sko (subst_bound (list_comb (c, args), p)) (ax' :: axs, thy) end
   130       | dec_sko (Const (@{const_name All}, _) $ (Abs (a, T, p))) thx =
   131         (*Universal quant: insert a free variable into body and continue*)
   132         let val fname = Name.variant (OldTerm.add_term_names (p, [])) a
   133         in dec_sko (subst_bound (Free (fname, T), p)) thx end
   134       | dec_sko (@{const "op &"} $ p $ q) thx = dec_sko q (dec_sko p thx)
   135       | dec_sko (@{const "op |"} $ p $ q) thx = dec_sko q (dec_sko p thx)
   136       | dec_sko (@{const Trueprop} $ p) thx = dec_sko p thx
   137       | dec_sko _ thx = thx
   138   in dec_sko (prop_of th) ([], thy) end
   139 
   140 fun mk_skolem_id t =
   141   let val T = fastype_of t in
   142     Const (@{const_name skolem_id}, T --> T) $ t
   143   end
   144 
   145 (*Traverse a theorem, accumulating Skolem function definitions.*)
   146 fun assume_skolem_funs inline s th =
   147   let
   148     val skolem_count = Unsynchronized.ref 0   (* FIXME ??? *)
   149     fun dec_sko (Const (@{const_name Ex}, _) $ (body as Abs (s', T, p))) defs =
   150         (*Existential: declare a Skolem function, then insert into body and continue*)
   151         let
   152           val skos = map (#1 o Logic.dest_equals) defs  (*existing sko fns*)
   153           val args = subtract (op =) skos (OldTerm.term_frees body) (*the formal parameters*)
   154           val Ts = map type_of args
   155           val cT = Ts ---> T (* FIXME: use "skolem_type_and_args" *)
   156           val id = skolem_name s (Unsynchronized.inc skolem_count) s'
   157           val c = Free (id, cT)
   158           (* Forms a lambda-abstraction over the formal parameters *)
   159           val rhs =
   160             list_abs_free (map dest_Free args,
   161                            HOLogic.choice_const T $ body)
   162             |> inline ? mk_skolem_id
   163           val def = Logic.mk_equals (c, rhs)
   164           val comb = list_comb (if inline then rhs else c, args)
   165         in dec_sko (subst_bound (comb, p)) (def :: defs) end
   166       | dec_sko (Const (@{const_name All},_) $ Abs (a, T, p)) defs =
   167         (*Universal quant: insert a free variable into body and continue*)
   168         let val fname = Name.variant (OldTerm.add_term_names (p,[])) a
   169         in dec_sko (subst_bound (Free(fname,T), p)) defs end
   170       | dec_sko (@{const "op &"} $ p $ q) defs = dec_sko q (dec_sko p defs)
   171       | dec_sko (@{const "op |"} $ p $ q) defs = dec_sko q (dec_sko p defs)
   172       | dec_sko (@{const Trueprop} $ p) defs = dec_sko p defs
   173       | dec_sko _ defs = defs
   174   in  dec_sko (prop_of th) []  end;
   175 
   176 
   177 (**** REPLACING ABSTRACTIONS BY COMBINATORS ****)
   178 
   179 (*Returns the vars of a theorem*)
   180 fun vars_of_thm th =
   181   map (Thm.cterm_of (theory_of_thm th) o Var) (Thm.fold_terms Term.add_vars th []);
   182 
   183 (*Make a version of fun_cong with a given variable name*)
   184 local
   185     val fun_cong' = fun_cong RS asm_rl; (*renumber f, g to prevent clashes with (a,0)*)
   186     val cx = hd (vars_of_thm fun_cong');
   187     val ty = typ_of (ctyp_of_term cx);
   188     val thy = theory_of_thm fun_cong;
   189     fun mkvar a = cterm_of thy (Var((a,0),ty));
   190 in
   191 fun xfun_cong x = Thm.instantiate ([], [(cx, mkvar x)]) fun_cong'
   192 end;
   193 
   194 (*Removes the lambdas from an equation of the form t = (%x. u).  A non-negative n,
   195   serves as an upper bound on how many to remove.*)
   196 fun strip_lambdas 0 th = th
   197   | strip_lambdas n th =
   198       case prop_of th of
   199           _ $ (Const (@{const_name "op ="}, _) $ _ $ Abs (x, _, _)) =>
   200               strip_lambdas (n-1) (freeze_thm (th RS xfun_cong x))
   201         | _ => th;
   202 
   203 fun is_quasi_lambda_free (Const (@{const_name skolem_id}, _) $ _) = true
   204   | is_quasi_lambda_free (t1 $ t2) =
   205     is_quasi_lambda_free t1 andalso is_quasi_lambda_free t2
   206   | is_quasi_lambda_free (Abs _) = false
   207   | is_quasi_lambda_free _ = true
   208 
   209 val [f_B,g_B] = map (cterm_of @{theory}) (OldTerm.term_vars (prop_of @{thm abs_B}));
   210 val [g_C,f_C] = map (cterm_of @{theory}) (OldTerm.term_vars (prop_of @{thm abs_C}));
   211 val [f_S,g_S] = map (cterm_of @{theory}) (OldTerm.term_vars (prop_of @{thm abs_S}));
   212 
   213 (*FIXME: requires more use of cterm constructors*)
   214 fun abstract ct =
   215   let
   216       val thy = theory_of_cterm ct
   217       val Abs(x,_,body) = term_of ct
   218       val Type(@{type_name fun}, [xT,bodyT]) = typ_of (ctyp_of_term ct)
   219       val cxT = ctyp_of thy xT and cbodyT = ctyp_of thy bodyT
   220       fun makeK() = instantiate' [SOME cxT, SOME cbodyT] [SOME (cterm_of thy body)] @{thm abs_K}
   221   in
   222       case body of
   223           Const _ => makeK()
   224         | Free _ => makeK()
   225         | Var _ => makeK()  (*though Var isn't expected*)
   226         | Bound 0 => instantiate' [SOME cxT] [] @{thm abs_I} (*identity: I*)
   227         | rator$rand =>
   228             if loose_bvar1 (rator,0) then (*C or S*)
   229                if loose_bvar1 (rand,0) then (*S*)
   230                  let val crator = cterm_of thy (Abs(x,xT,rator))
   231                      val crand = cterm_of thy (Abs(x,xT,rand))
   232                      val abs_S' = cterm_instantiate [(f_S,crator),(g_S,crand)] @{thm abs_S}
   233                      val (_,rhs) = Thm.dest_equals (cprop_of abs_S')
   234                  in
   235                    Thm.transitive abs_S' (Conv.binop_conv abstract rhs)
   236                  end
   237                else (*C*)
   238                  let val crator = cterm_of thy (Abs(x,xT,rator))
   239                      val abs_C' = cterm_instantiate [(f_C,crator),(g_C,cterm_of thy rand)] @{thm abs_C}
   240                      val (_,rhs) = Thm.dest_equals (cprop_of abs_C')
   241                  in
   242                    Thm.transitive abs_C' (Conv.fun_conv (Conv.arg_conv abstract) rhs)
   243                  end
   244             else if loose_bvar1 (rand,0) then (*B or eta*)
   245                if rand = Bound 0 then Thm.eta_conversion ct
   246                else (*B*)
   247                  let val crand = cterm_of thy (Abs(x,xT,rand))
   248                      val crator = cterm_of thy rator
   249                      val abs_B' = cterm_instantiate [(f_B,crator),(g_B,crand)] @{thm abs_B}
   250                      val (_,rhs) = Thm.dest_equals (cprop_of abs_B')
   251                  in Thm.transitive abs_B' (Conv.arg_conv abstract rhs) end
   252             else makeK()
   253         | _ => raise Fail "abstract: Bad term"
   254   end;
   255 
   256 (* Traverse a theorem, remplacing lambda-abstractions with combinators. *)
   257 fun do_introduce_combinators ct =
   258   if is_quasi_lambda_free (term_of ct) then
   259     Thm.reflexive ct
   260   else case term_of ct of
   261     Abs _ =>
   262     let
   263       val (cv, cta) = Thm.dest_abs NONE ct
   264       val (v, _) = dest_Free (term_of cv)
   265       val u_th = do_introduce_combinators cta
   266       val cu = Thm.rhs_of u_th
   267       val comb_eq = abstract (Thm.cabs cv cu)
   268     in Thm.transitive (Thm.abstract_rule v cv u_th) comb_eq end
   269   | _ $ _ =>
   270     let val (ct1, ct2) = Thm.dest_comb ct in
   271         Thm.combination (do_introduce_combinators ct1)
   272                         (do_introduce_combinators ct2)
   273     end
   274 
   275 fun introduce_combinators th =
   276   if is_quasi_lambda_free (prop_of th) then
   277     th
   278   else
   279     let
   280       val th = Drule.eta_contraction_rule th
   281       val eqth = do_introduce_combinators (cprop_of th)
   282     in Thm.equal_elim eqth th end
   283     handle THM (msg, _, _) =>
   284            (warning ("Error in the combinator translation of " ^
   285                      Display.string_of_thm_without_context th ^
   286                      "\nException message: " ^ msg ^ ".");
   287             (* A type variable of sort "{}" will make abstraction fail. *)
   288             TrueI)
   289 
   290 (*cterms are used throughout for efficiency*)
   291 val cTrueprop = Thm.cterm_of @{theory HOL} HOLogic.Trueprop;
   292 
   293 (*cterm version of mk_cTrueprop*)
   294 fun c_mkTrueprop A = Thm.capply cTrueprop A;
   295 
   296 (*Given an abstraction over n variables, replace the bound variables by free
   297   ones. Return the body, along with the list of free variables.*)
   298 fun c_variant_abs_multi (ct0, vars) =
   299       let val (cv,ct) = Thm.dest_abs NONE ct0
   300       in  c_variant_abs_multi (ct, cv::vars)  end
   301       handle CTERM _ => (ct0, rev vars);
   302 
   303 (*Given the definition of a Skolem function, return a theorem to replace
   304   an existential formula by a use of that function.
   305    Example: "EX x. x : A & x ~: B ==> sko A B : A & sko A B ~: B"  [.] *)
   306 fun skolem_theorem_of_def inline def =
   307   let
   308       val (c, rhs) = Thm.dest_equals (cprop_of (freeze_thm def))
   309       val rhs' = rhs |> inline ? (snd o Thm.dest_comb)
   310       val (ch, frees) = c_variant_abs_multi (rhs', [])
   311       val (chilbert, cabs) = ch |> Thm.dest_comb
   312       val thy = Thm.theory_of_cterm chilbert
   313       val t = Thm.term_of chilbert
   314       val T =
   315         case t of
   316           Const (@{const_name Eps}, Type (@{type_name fun}, [_, T])) => T
   317         | _ => raise TERM ("skolem_theorem_of_def: expected \"Eps\"", [t])
   318       val cex = Thm.cterm_of thy (HOLogic.exists_const T)
   319       val ex_tm = c_mkTrueprop (Thm.capply cex cabs)
   320       and conc =
   321         Drule.list_comb (if inline then rhs else c, frees)
   322         |> Drule.beta_conv cabs |> c_mkTrueprop
   323       fun tacf [prem] =
   324         (if inline then rewrite_goals_tac @{thms skolem_id_def_raw}
   325          else rewrite_goals_tac [def])
   326         THEN rtac ((prem |> inline ? rewrite_rule @{thms skolem_id_def_raw})
   327                    RS @{thm someI_ex}) 1
   328   in  Goal.prove_internal [ex_tm] conc tacf
   329        |> forall_intr_list frees
   330        |> Thm.forall_elim_vars 0  (*Introduce Vars, but don't discharge defs.*)
   331        |> Thm.varifyT_global
   332   end;
   333 
   334 
   335 (*Converts an Isabelle theorem (intro, elim or simp format, even higher-order) into NNF.*)
   336 fun to_nnf th ctxt0 =
   337   let val th1 = th |> transform_elim |> zero_var_indexes
   338       val ((_, [th2]), ctxt) = Variable.import true [th1] ctxt0
   339       val th3 = th2
   340         |> Conv.fconv_rule Object_Logic.atomize
   341         |> Meson.make_nnf ctxt |> strip_lambdas ~1
   342   in  (th3, ctxt)  end;
   343 
   344 (*Generate Skolem functions for a theorem supplied in nnf*)
   345 fun skolem_theorems_of_assume inline s th =
   346   map (skolem_theorem_of_def inline o Thm.assume o cterm_of (theory_of_thm th))
   347       (assume_skolem_funs inline s th)
   348 
   349 
   350 (*** Blacklisting (more in "Sledgehammer_Fact_Filter") ***)
   351 
   352 val max_lambda_nesting = 3
   353 
   354 fun term_has_too_many_lambdas max (t1 $ t2) =
   355     exists (term_has_too_many_lambdas max) [t1, t2]
   356   | term_has_too_many_lambdas max (Abs (_, _, t)) =
   357     max = 0 orelse term_has_too_many_lambdas (max - 1) t
   358   | term_has_too_many_lambdas _ _ = false
   359 
   360 fun is_formula_type T = (T = HOLogic.boolT orelse T = propT)
   361 
   362 (* Don't count nested lambdas at the level of formulas, since they are
   363    quantifiers. *)
   364 fun formula_has_too_many_lambdas Ts (Abs (_, T, t)) =
   365     formula_has_too_many_lambdas (T :: Ts) t
   366   | formula_has_too_many_lambdas Ts t =
   367     if is_formula_type (fastype_of1 (Ts, t)) then
   368       exists (formula_has_too_many_lambdas Ts) (#2 (strip_comb t))
   369     else
   370       term_has_too_many_lambdas max_lambda_nesting t
   371 
   372 (* The max apply depth of any "metis" call in "Metis_Examples" (on 31-10-2007)
   373    was 11. *)
   374 val max_apply_depth = 15
   375 
   376 fun apply_depth (f $ t) = Int.max (apply_depth f, apply_depth t + 1)
   377   | apply_depth (Abs (_, _, t)) = apply_depth t
   378   | apply_depth _ = 0
   379 
   380 fun is_formula_too_complex t =
   381   apply_depth t > max_apply_depth orelse Meson.too_many_clauses NONE t orelse
   382   formula_has_too_many_lambdas [] t
   383 
   384 fun is_strange_thm th =
   385   case head_of (concl_of th) of
   386       Const (a, _) => (a <> @{const_name Trueprop} andalso
   387                        a <> @{const_name "=="})
   388     | _ => false;
   389 
   390 fun is_theorem_bad_for_atps thm =
   391   let val t = prop_of thm in
   392     is_formula_too_complex t orelse exists_type type_has_topsort t orelse
   393     is_strange_thm thm
   394   end
   395 
   396 (* FIXME: put other record thms here, or declare as "no_atp" *)
   397 val multi_base_blacklist =
   398   ["defs", "select_defs", "update_defs", "induct", "inducts", "split", "splits",
   399    "split_asm", "cases", "ext_cases"];
   400 
   401 fun fake_name th =
   402   if Thm.has_name_hint th then flatten_name (Thm.get_name_hint th)
   403   else gensym "unknown_thm_";
   404 
   405 (*Skolemize a named theorem, with Skolem functions as additional premises.*)
   406 fun skolemize_theorem s th =
   407   if member (op =) multi_base_blacklist (Long_Name.base_name s) orelse
   408      is_theorem_bad_for_atps th then
   409     []
   410   else
   411     let
   412       val ctxt0 = Variable.global_thm_context th
   413       val (nnfth, ctxt) = to_nnf th ctxt0
   414       val inline = exists_type (exists_subtype (can dest_TFree)) (prop_of nnfth)
   415       val defs = skolem_theorems_of_assume inline s nnfth
   416       val (cnfs, ctxt) = Meson.make_cnf defs nnfth ctxt
   417     in
   418       cnfs |> map introduce_combinators
   419            |> Variable.export ctxt ctxt0
   420            |> Meson.finish_cnf
   421     end
   422     handle THM _ => []
   423 
   424 (*The cache prevents repeated clausification of a theorem, and also repeated declaration of
   425   Skolem functions.*)
   426 structure ThmCache = Theory_Data
   427 (
   428   type T = thm list Thmtab.table * unit Symtab.table;
   429   val empty = (Thmtab.empty, Symtab.empty);
   430   val extend = I;
   431   fun merge ((cache1, seen1), (cache2, seen2)) : T =
   432     (Thmtab.merge (K true) (cache1, cache2), Symtab.merge (K true) (seen1, seen2));
   433 );
   434 
   435 val lookup_cache = Thmtab.lookup o #1 o ThmCache.get;
   436 val already_seen = Symtab.defined o #2 o ThmCache.get;
   437 
   438 val update_cache = ThmCache.map o apfst o Thmtab.update;
   439 fun mark_seen name = ThmCache.map (apsnd (Symtab.update (name, ())));
   440 
   441 (* Convert Isabelle theorems into axiom clauses. *)
   442 fun cnf_axiom thy th0 =
   443   let val th = Thm.transfer thy th0 in
   444     case lookup_cache thy th of
   445       SOME cls => cls
   446     | NONE => map Thm.close_derivation (skolemize_theorem (fake_name th) th)
   447   end;
   448 
   449 
   450 (**** Translate a set of theorems into CNF ****)
   451 
   452 (*The combination of rev and tail recursion preserves the original order*)
   453 fun cnf_rules_pairs thy =
   454   let
   455     fun do_one _ [] = []
   456       | do_one ((name, k), th) (cls :: clss) =
   457         (cls, ((name, k), th)) :: do_one ((name, k + 1), th) clss
   458     fun do_all pairs [] = pairs
   459       | do_all pairs ((name, th) :: ths) =
   460         let
   461           val new_pairs = do_one ((name, 0), th) (cnf_axiom thy th)
   462                           handle THM _ => [] |
   463                                  CLAUSE _ => []
   464         in do_all (new_pairs @ pairs) ths end
   465   in do_all [] o rev end
   466 
   467 
   468 (**** Convert all facts of the theory into FOL or HOL clauses ****)
   469 
   470 local
   471 
   472 fun skolem_def (name, th) thy =
   473   let val ctxt0 = Variable.global_thm_context th in
   474     case try (to_nnf th) ctxt0 of
   475       NONE => (NONE, thy)
   476     | SOME (nnfth, ctxt) =>
   477       let val (defs, thy') = declare_skolem_funs (flatten_name name) nnfth thy
   478       in (SOME (th, ctxt0, ctxt, nnfth, defs), thy') end
   479   end;
   480 
   481 fun skolem_cnfs (th, ctxt0, ctxt, nnfth, defs) =
   482   let
   483     val (cnfs, ctxt) =
   484       Meson.make_cnf (map (skolem_theorem_of_def false) defs) nnfth ctxt
   485     val cnfs' = cnfs
   486       |> map introduce_combinators
   487       |> Variable.export ctxt ctxt0
   488       |> Meson.finish_cnf
   489       |> map Thm.close_derivation;
   490     in (th, cnfs') end;
   491 
   492 in
   493 
   494 fun saturate_skolem_cache thy =
   495   let
   496     val facts = PureThy.facts_of thy;
   497     val new_facts = (facts, []) |-> Facts.fold_static (fn (name, ths) =>
   498       if Facts.is_concealed facts name orelse already_seen thy name then I
   499       else cons (name, ths));
   500     val new_thms = (new_facts, []) |-> fold (fn (name, ths) =>
   501       if member (op =) multi_base_blacklist (Long_Name.base_name name) then
   502         I
   503       else
   504         fold_index (fn (i, th) =>
   505           if is_theorem_bad_for_atps th orelse
   506              is_some (lookup_cache thy th) then
   507             I
   508           else
   509             cons (name ^ "_" ^ string_of_int (i + 1), Thm.transfer thy th)) ths)
   510   in
   511     if null new_facts then
   512       NONE
   513     else
   514       let
   515         val (defs, thy') = thy
   516           |> fold (mark_seen o #1) new_facts
   517           |> fold_map skolem_def (sort_distinct (Thm.thm_ord o pairself snd) new_thms)
   518           |>> map_filter I;
   519         val cache_entries = Par_List.map skolem_cnfs defs;
   520       in SOME (fold update_cache cache_entries thy') end
   521   end;
   522 
   523 end;
   524 
   525 val auto_saturate_skolem_cache = Unsynchronized.ref true
   526 
   527 fun conditionally_saturate_skolem_cache thy =
   528   if !auto_saturate_skolem_cache then saturate_skolem_cache thy else NONE
   529 
   530 
   531 (*** Converting a subgoal into negated conjecture clauses. ***)
   532 
   533 fun neg_skolemize_tac ctxt =
   534   EVERY' [rtac ccontr, Object_Logic.atomize_prems_tac, Meson.skolemize_tac ctxt]
   535 
   536 val neg_clausify =
   537   single
   538   #> Meson.make_clauses_unsorted
   539   #> map introduce_combinators
   540   #> Meson.finish_cnf
   541 
   542 fun neg_conjecture_clauses ctxt st0 n =
   543   let
   544     (* "Option" is thrown if the assumptions contain schematic variables. *)
   545     val st = Seq.hd (neg_skolemize_tac ctxt n st0) handle Option.Option => st0
   546     val ({params, prems, ...}, _) =
   547       Subgoal.focus (Variable.set_body false ctxt) n st
   548   in (map neg_clausify prems, map (dest_Free o term_of o #2) params) end
   549 
   550 (*Conversion of a subgoal to conjecture clauses. Each clause has
   551   leading !!-bound universal variables, to express generality. *)
   552 fun neg_clausify_tac ctxt =
   553   neg_skolemize_tac ctxt THEN'
   554   SUBGOAL (fn (prop, i) =>
   555     let val ts = Logic.strip_assums_hyp prop in
   556       EVERY'
   557        [Subgoal.FOCUS
   558          (fn {prems, ...} =>
   559            (Method.insert_tac
   560              (map forall_intr_vars (maps neg_clausify prems)) i)) ctxt,
   561         REPEAT_DETERM_N (length ts) o etac thin_rl] i
   562      end);
   563 
   564 
   565 (** setup **)
   566 
   567 val setup =
   568   perhaps conditionally_saturate_skolem_cache
   569   #> Theory.at_end conditionally_saturate_skolem_cache
   570 
   571 end;