src/HOL/Tools/res_hol_clause.ML
author immler@in.tum.de
Wed Jun 03 16:56:41 2009 +0200 (2009-06-03)
changeset 31409 d8537ba165b5
parent 30242 aea5d7fa7ef5
child 31749 8ee34e3ceb5a
permissions -rw-r--r--
split preparing clauses and writing problemfile;
included results of count_constants in return-type of prover;
optionally pass counted constants to prover;
removed unused external_prover from signature
     1 (*
     2    Author: Jia Meng, NICTA
     3 
     4 FOL clauses translated from HOL formulae.
     5 *)
     6 
     7 signature RES_HOL_CLAUSE =
     8 sig
     9   val ext: thm
    10   val comb_I: thm
    11   val comb_K: thm
    12   val comb_B: thm
    13   val comb_C: thm
    14   val comb_S: thm
    15   datatype type_level = T_FULL | T_CONST
    16   val typ_level: type_level
    17   val minimize_applies: bool
    18   type axiom_name = string
    19   type polarity = bool
    20   type clause_id = int
    21   datatype combterm =
    22       CombConst of string * ResClause.fol_type * ResClause.fol_type list (*Const and Free*)
    23     | CombVar of string * ResClause.fol_type
    24     | CombApp of combterm * combterm
    25   datatype literal = Literal of polarity * combterm
    26   datatype clause = Clause of {clause_id: clause_id, axiom_name: axiom_name, th: thm,
    27                     kind: ResClause.kind,literals: literal list, ctypes_sorts: typ list}
    28   val strip_comb: combterm -> combterm * combterm list
    29   val literals_of_term: theory -> term -> literal list * typ list
    30   exception TOO_TRIVIAL
    31   val count_constants: clause list * clause list * clause list * 'a * 'b ->
    32     int Symtab.table * bool Symtab.table
    33   val prepare_clauses: bool -> theory -> bool -> thm list -> (thm * (axiom_name * clause_id)) list ->
    34     string list -> axiom_name list * (clause list * clause list * clause list)
    35   val tptp_write_file: string -> int Symtab.table * bool Symtab.table ->
    36     clause list * clause list * clause list * ResClause.classrelClause list * ResClause.arityClause list -> unit
    37   val dfg_write_file: string -> int Symtab.table * bool Symtab.table ->
    38     clause list * clause list * clause list * ResClause.classrelClause list * ResClause.arityClause list -> unit
    39 end
    40 
    41 structure ResHolClause: RES_HOL_CLAUSE =
    42 struct
    43 
    44 structure RC = ResClause;
    45 
    46 (* theorems for combinators and function extensionality *)
    47 val ext = thm "HOL.ext";
    48 val comb_I = thm "ATP_Linkup.COMBI_def";
    49 val comb_K = thm "ATP_Linkup.COMBK_def";
    50 val comb_B = thm "ATP_Linkup.COMBB_def";
    51 val comb_C = thm "ATP_Linkup.COMBC_def";
    52 val comb_S = thm "ATP_Linkup.COMBS_def";
    53 val fequal_imp_equal = thm "ATP_Linkup.fequal_imp_equal";
    54 val equal_imp_fequal = thm "ATP_Linkup.equal_imp_fequal";
    55 
    56 
    57 (*The different translations of types*)
    58 datatype type_level = T_FULL | T_CONST;
    59 
    60 val typ_level = T_CONST;
    61 
    62 (*If true, each function will be directly applied to as many arguments as possible, avoiding
    63   use of the "apply" operator. Use of hBOOL is also minimized.*)
    64 val minimize_applies = true;
    65 
    66 fun min_arity_of const_min_arity c = getOpt (Symtab.lookup const_min_arity c, 0);
    67 
    68 (*True if the constant ever appears outside of the top-level position in literals.
    69   If false, the constant always receives all of its arguments and is used as a predicate.*)
    70 fun needs_hBOOL const_needs_hBOOL c = not minimize_applies orelse
    71                     getOpt (Symtab.lookup const_needs_hBOOL c, false);
    72 
    73 
    74 (******************************************************)
    75 (* data types for typed combinator expressions        *)
    76 (******************************************************)
    77 
    78 type axiom_name = string;
    79 type polarity = bool;
    80 type clause_id = int;
    81 
    82 datatype combterm = CombConst of string * RC.fol_type * RC.fol_type list (*Const and Free*)
    83                   | CombVar of string * RC.fol_type
    84                   | CombApp of combterm * combterm
    85 
    86 datatype literal = Literal of polarity * combterm;
    87 
    88 datatype clause =
    89          Clause of {clause_id: clause_id,
    90                     axiom_name: axiom_name,
    91                     th: thm,
    92                     kind: RC.kind,
    93                     literals: literal list,
    94                     ctypes_sorts: typ list};
    95 
    96 
    97 (*********************************************************************)
    98 (* convert a clause with type Term.term to a clause with type clause *)
    99 (*********************************************************************)
   100 
   101 fun isFalse (Literal(pol, CombConst(c,_,_))) =
   102       (pol andalso c = "c_False") orelse
   103       (not pol andalso c = "c_True")
   104   | isFalse _ = false;
   105 
   106 fun isTrue (Literal (pol, CombConst(c,_,_))) =
   107       (pol andalso c = "c_True") orelse
   108       (not pol andalso c = "c_False")
   109   | isTrue _ = false;
   110 
   111 fun isTaut (Clause {literals,...}) = exists isTrue literals;
   112 
   113 fun type_of dfg (Type (a, Ts)) =
   114       let val (folTypes,ts) = types_of dfg Ts
   115       in  (RC.Comp(RC.make_fixed_type_const dfg a, folTypes), ts)  end
   116   | type_of dfg (tp as (TFree(a,s))) =
   117       (RC.AtomF (RC.make_fixed_type_var a), [tp])
   118   | type_of dfg (tp as (TVar(v,s))) =
   119       (RC.AtomV (RC.make_schematic_type_var v), [tp])
   120 and types_of dfg Ts =
   121       let val (folTyps,ts) = ListPair.unzip (map (type_of dfg) Ts)
   122       in  (folTyps, RC.union_all ts)  end;
   123 
   124 (* same as above, but no gathering of sort information *)
   125 fun simp_type_of dfg (Type (a, Ts)) =
   126       RC.Comp(RC.make_fixed_type_const dfg a, map (simp_type_of dfg) Ts)
   127   | simp_type_of dfg (TFree (a,s)) = RC.AtomF(RC.make_fixed_type_var a)
   128   | simp_type_of dfg (TVar (v,s)) = RC.AtomV(RC.make_schematic_type_var v);
   129 
   130 
   131 fun const_type_of dfg thy (c,t) =
   132       let val (tp,ts) = type_of dfg t
   133       in  (tp, ts, map (simp_type_of dfg) (Sign.const_typargs thy (c,t))) end;
   134 
   135 (* convert a Term.term (with combinators) into a combterm, also accummulate sort info *)
   136 fun combterm_of dfg thy (Const(c,t)) =
   137       let val (tp,ts,tvar_list) = const_type_of dfg thy (c,t)
   138           val c' = CombConst(RC.make_fixed_const dfg c, tp, tvar_list)
   139       in  (c',ts)  end
   140   | combterm_of dfg thy (Free(v,t)) =
   141       let val (tp,ts) = type_of dfg t
   142           val v' = CombConst(RC.make_fixed_var v, tp, [])
   143       in  (v',ts)  end
   144   | combterm_of dfg thy (Var(v,t)) =
   145       let val (tp,ts) = type_of dfg t
   146           val v' = CombVar(RC.make_schematic_var v,tp)
   147       in  (v',ts)  end
   148   | combterm_of dfg thy (P $ Q) =
   149       let val (P',tsP) = combterm_of dfg thy P
   150           val (Q',tsQ) = combterm_of dfg thy Q
   151       in  (CombApp(P',Q'), tsP union tsQ)  end
   152   | combterm_of _ thy (t as Abs _) = raise RC.CLAUSE("HOL CLAUSE",t);
   153 
   154 fun predicate_of dfg thy ((Const("Not",_) $ P), polarity) = predicate_of dfg thy (P, not polarity)
   155   | predicate_of dfg thy (t,polarity) = (combterm_of dfg thy (Envir.eta_contract t), polarity);
   156 
   157 fun literals_of_term1 dfg thy args (Const("Trueprop",_) $ P) = literals_of_term1 dfg thy args P
   158   | literals_of_term1 dfg thy args (Const("op |",_) $ P $ Q) =
   159       literals_of_term1 dfg thy (literals_of_term1 dfg thy args P) Q
   160   | literals_of_term1 dfg thy (lits,ts) P =
   161       let val ((pred,ts'),pol) = predicate_of dfg thy (P,true)
   162       in
   163           (Literal(pol,pred)::lits, ts union ts')
   164       end;
   165 
   166 fun literals_of_term_dfg dfg thy P = literals_of_term1 dfg thy ([],[]) P;
   167 val literals_of_term = literals_of_term_dfg false;
   168 
   169 (* Problem too trivial for resolution (empty clause) *)
   170 exception TOO_TRIVIAL;
   171 
   172 (* making axiom and conjecture clauses *)
   173 fun make_clause dfg thy (clause_id,axiom_name,kind,th) =
   174     let val (lits,ctypes_sorts) = literals_of_term_dfg dfg thy (prop_of th)
   175     in
   176         if forall isFalse lits
   177         then raise TOO_TRIVIAL
   178         else
   179             Clause {clause_id = clause_id, axiom_name = axiom_name, th = th, kind = kind,
   180                     literals = lits, ctypes_sorts = ctypes_sorts}
   181     end;
   182 
   183 
   184 fun add_axiom_clause dfg thy ((th,(name,id)), pairs) =
   185   let val cls = make_clause dfg thy (id, name, RC.Axiom, th)
   186   in
   187       if isTaut cls then pairs else (name,cls)::pairs
   188   end;
   189 
   190 fun make_axiom_clauses dfg thy = List.foldl (add_axiom_clause dfg thy) [];
   191 
   192 fun make_conjecture_clauses_aux dfg _ _ [] = []
   193   | make_conjecture_clauses_aux dfg thy n (th::ths) =
   194       make_clause dfg thy (n,"conjecture", RC.Conjecture, th) ::
   195       make_conjecture_clauses_aux dfg thy (n+1) ths;
   196 
   197 fun make_conjecture_clauses dfg thy = make_conjecture_clauses_aux dfg thy 0;
   198 
   199 
   200 (**********************************************************************)
   201 (* convert clause into ATP specific formats:                          *)
   202 (* TPTP used by Vampire and E                                         *)
   203 (* DFG used by SPASS                                                  *)
   204 (**********************************************************************)
   205 
   206 (*Result of a function type; no need to check that the argument type matches.*)
   207 fun result_type (RC.Comp ("tc_fun", [_, tp2])) = tp2
   208   | result_type _ = error "result_type"
   209 
   210 fun type_of_combterm (CombConst(c,tp,_)) = tp
   211   | type_of_combterm (CombVar(v,tp)) = tp
   212   | type_of_combterm (CombApp(t1,t2)) = result_type (type_of_combterm t1);
   213 
   214 (*gets the head of a combinator application, along with the list of arguments*)
   215 fun strip_comb u =
   216     let fun stripc (CombApp(t,u), ts) = stripc (t, u::ts)
   217         |   stripc  x =  x
   218     in  stripc(u,[])  end;
   219 
   220 val type_wrapper = "ti";
   221 
   222 fun head_needs_hBOOL const_needs_hBOOL (CombConst(c,_,_)) = needs_hBOOL const_needs_hBOOL c
   223   | head_needs_hBOOL const_needs_hBOOL _ = true;
   224 
   225 fun wrap_type (s, tp) =
   226   if typ_level=T_FULL then
   227       type_wrapper ^ RC.paren_pack [s, RC.string_of_fol_type tp]
   228   else s;
   229 
   230 fun apply ss = "hAPP" ^ RC.paren_pack ss;
   231 
   232 fun rev_apply (v, []) = v
   233   | rev_apply (v, arg::args) = apply [rev_apply (v, args), arg];
   234 
   235 fun string_apply (v, args) = rev_apply (v, rev args);
   236 
   237 (*Apply an operator to the argument strings, using either the "apply" operator or
   238   direct function application.*)
   239 fun string_of_applic cma (CombConst(c,tp,tvars), args) =
   240       let val c = if c = "equal" then "c_fequal" else c
   241           val nargs = min_arity_of cma c
   242           val args1 = List.take(args, nargs)
   243             handle Subscript => error ("string_of_applic: " ^ c ^ " has arity " ^
   244                                          Int.toString nargs ^ " but is applied to " ^
   245                                          space_implode ", " args)
   246           val args2 = List.drop(args, nargs)
   247           val targs = if typ_level = T_CONST then map RC.string_of_fol_type tvars
   248                       else []
   249       in
   250           string_apply (c ^ RC.paren_pack (args1@targs), args2)
   251       end
   252   | string_of_applic cma (CombVar(v,tp), args) = string_apply (v, args)
   253   | string_of_applic _ _ = error "string_of_applic";
   254 
   255 fun wrap_type_if cnh (head, s, tp) = if head_needs_hBOOL cnh head then wrap_type (s, tp) else s;
   256 
   257 fun string_of_combterm cma cnh t =
   258   let val (head, args) = strip_comb t
   259   in  wrap_type_if cnh (head,
   260                     string_of_applic cma (head, map (string_of_combterm cma cnh) args),
   261                     type_of_combterm t)
   262   end;
   263 
   264 (*Boolean-valued terms are here converted to literals.*)
   265 fun boolify cma cnh t = "hBOOL" ^ RC.paren_pack [string_of_combterm cma cnh t];
   266 
   267 fun string_of_predicate cma cnh t =
   268   case t of
   269       (CombApp(CombApp(CombConst("equal",_,_), t1), t2)) =>
   270           (*DFG only: new TPTP prefers infix equality*)
   271           ("equal" ^ RC.paren_pack [string_of_combterm cma cnh t1, string_of_combterm cma cnh t2])
   272     | _ =>
   273           case #1 (strip_comb t) of
   274               CombConst(c,_,_) => if needs_hBOOL cnh c then boolify cma cnh t else string_of_combterm cma cnh t
   275             | _ => boolify cma cnh t;
   276 
   277 fun string_of_clausename (cls_id,ax_name) =
   278     RC.clause_prefix ^ RC.ascii_of ax_name ^ "_" ^ Int.toString cls_id;
   279 
   280 fun string_of_type_clsname (cls_id,ax_name,idx) =
   281     string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
   282 
   283 
   284 (*** tptp format ***)
   285 
   286 fun tptp_of_equality cma cnh pol (t1,t2) =
   287   let val eqop = if pol then " = " else " != "
   288   in  string_of_combterm cma cnh t1 ^ eqop ^ string_of_combterm cma cnh t2  end;
   289 
   290 fun tptp_literal cma cnh (Literal(pol, CombApp(CombApp(CombConst("equal",_,_), t1), t2))) =
   291       tptp_of_equality cma cnh pol (t1,t2)
   292   | tptp_literal cma cnh (Literal(pol,pred)) =
   293       RC.tptp_sign pol (string_of_predicate cma cnh pred);
   294 
   295 (*Given a clause, returns its literals paired with a list of literals concerning TFrees;
   296   the latter should only occur in conjecture clauses.*)
   297 fun tptp_type_lits cma cnh pos (Clause{literals, ctypes_sorts, ...}) =
   298       (map (tptp_literal cma cnh) literals, 
   299        map (RC.tptp_of_typeLit pos) (RC.add_typs ctypes_sorts));
   300 
   301 fun clause2tptp (cma, cnh) (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   302   let val (lits,tylits) = tptp_type_lits cma cnh (kind = RC.Conjecture) cls
   303   in
   304       (RC.gen_tptp_cls(clause_id,axiom_name,kind,lits,tylits), tylits)
   305   end;
   306 
   307 
   308 (*** dfg format ***)
   309 
   310 fun dfg_literal cma cnh (Literal(pol,pred)) = RC.dfg_sign pol (string_of_predicate cma cnh pred);
   311 
   312 fun dfg_type_lits cma cnh pos (Clause{literals, ctypes_sorts, ...}) =
   313       (map (dfg_literal cma cnh) literals, 
   314        map (RC.dfg_of_typeLit pos) (RC.add_typs ctypes_sorts));
   315 
   316 fun get_uvars (CombConst _) vars = vars
   317   | get_uvars (CombVar(v,_)) vars = (v::vars)
   318   | get_uvars (CombApp(P,Q)) vars = get_uvars P (get_uvars Q vars);
   319 
   320 fun get_uvars_l (Literal(_,c)) = get_uvars c [];
   321 
   322 fun dfg_vars (Clause {literals,...}) = RC.union_all (map get_uvars_l literals);
   323 
   324 fun clause2dfg (cma, cnh) (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   325   let val (lits,tylits) = dfg_type_lits cma cnh (kind = RC.Conjecture) cls
   326       val vars = dfg_vars cls
   327       val tvars = RC.get_tvar_strs ctypes_sorts
   328   in
   329       (RC.gen_dfg_cls(clause_id, axiom_name, kind, lits, tylits, tvars@vars), tylits)
   330   end;
   331 
   332 
   333 (** For DFG format: accumulate function and predicate declarations **)
   334 
   335 fun addtypes tvars tab = List.foldl RC.add_foltype_funcs tab tvars;
   336 
   337 fun add_decls cma cnh (CombConst(c,tp,tvars), (funcs,preds)) =
   338       if c = "equal" then (addtypes tvars funcs, preds)
   339       else
   340 	let val arity = min_arity_of cma c
   341 	    val ntys = if typ_level = T_CONST then length tvars else 0
   342 	    val addit = Symtab.update(c, arity+ntys)
   343 	in
   344 	    if needs_hBOOL cnh c then (addtypes tvars (addit funcs), preds)
   345 	    else (addtypes tvars funcs, addit preds)
   346 	end
   347   | add_decls _ _ (CombVar(_,ctp), (funcs,preds)) =
   348       (RC.add_foltype_funcs (ctp,funcs), preds)
   349   | add_decls cma cnh (CombApp(P,Q),decls) = add_decls cma cnh (P,add_decls cma cnh (Q,decls));
   350 
   351 fun add_literal_decls cma cnh (Literal(_,c), decls) = add_decls cma cnh (c,decls);
   352 
   353 fun add_clause_decls cma cnh (Clause {literals, ...}, decls) =
   354     List.foldl (add_literal_decls cma cnh) decls literals
   355     handle Symtab.DUP a => error ("function " ^ a ^ " has multiple arities")
   356 
   357 fun decls_of_clauses (cma, cnh) clauses arity_clauses =
   358   let val init_functab = Symtab.update (type_wrapper,2) (Symtab.update ("hAPP",2) RC.init_functab)
   359       val init_predtab = Symtab.update ("hBOOL",1) Symtab.empty
   360       val (functab,predtab) = (List.foldl (add_clause_decls cma cnh) (init_functab, init_predtab) clauses)
   361   in
   362       (Symtab.dest (List.foldl RC.add_arityClause_funcs functab arity_clauses),
   363        Symtab.dest predtab)
   364   end;
   365 
   366 fun add_clause_preds (Clause {ctypes_sorts, ...}, preds) =
   367   List.foldl RC.add_type_sort_preds preds ctypes_sorts
   368   handle Symtab.DUP a => error ("predicate " ^ a ^ " has multiple arities")
   369 
   370 (*Higher-order clauses have only the predicates hBOOL and type classes.*)
   371 fun preds_of_clauses clauses clsrel_clauses arity_clauses =
   372     Symtab.dest
   373         (List.foldl RC.add_classrelClause_preds
   374                (List.foldl RC.add_arityClause_preds
   375                       (List.foldl add_clause_preds Symtab.empty clauses)
   376                       arity_clauses)
   377                clsrel_clauses)
   378 
   379 
   380 (**********************************************************************)
   381 (* write clauses to files                                             *)
   382 (**********************************************************************)
   383 
   384 val init_counters =
   385     Symtab.make [("c_COMBI", 0), ("c_COMBK", 0),
   386                  ("c_COMBB", 0), ("c_COMBC", 0),
   387                  ("c_COMBS", 0)];
   388 
   389 fun count_combterm (CombConst(c,tp,_), ct) =
   390      (case Symtab.lookup ct c of NONE => ct  (*no counter*)
   391                                | SOME n => Symtab.update (c,n+1) ct)
   392   | count_combterm (CombVar(v,tp), ct) = ct
   393   | count_combterm (CombApp(t1,t2), ct) = count_combterm(t1, count_combterm(t2, ct));
   394 
   395 fun count_literal (Literal(_,t), ct) = count_combterm(t,ct);
   396 
   397 fun count_clause (Clause{literals,...}, ct) = List.foldl count_literal ct literals;
   398 
   399 fun count_user_clause user_lemmas (Clause{axiom_name,literals,...}, ct) =
   400   if axiom_name mem_string user_lemmas then List.foldl count_literal ct literals
   401   else ct;
   402 
   403 fun cnf_helper_thms thy =
   404   ResAxioms.cnf_rules_pairs thy o map ResAxioms.pairname
   405 
   406 fun get_helper_clauses dfg thy isFO (conjectures, axclauses, user_lemmas) =
   407   if isFO then []  (*first-order*)
   408   else
   409     let val ct0 = List.foldl count_clause init_counters conjectures
   410         val ct = List.foldl (count_user_clause user_lemmas) ct0 axclauses
   411         fun needed c = valOf (Symtab.lookup ct c) > 0
   412         val IK = if needed "c_COMBI" orelse needed "c_COMBK"
   413                  then (Output.debug (fn () => "Include combinator I K"); cnf_helper_thms thy [comb_I,comb_K])
   414                  else []
   415         val BC = if needed "c_COMBB" orelse needed "c_COMBC"
   416                  then (Output.debug (fn () => "Include combinator B C"); cnf_helper_thms thy [comb_B,comb_C])
   417                  else []
   418         val S = if needed "c_COMBS"
   419                 then (Output.debug (fn () => "Include combinator S"); cnf_helper_thms thy [comb_S])
   420                 else []
   421         val other = cnf_helper_thms thy [ext,fequal_imp_equal,equal_imp_fequal]
   422     in
   423         map #2 (make_axiom_clauses dfg thy (other @ IK @ BC @ S))
   424     end;
   425 
   426 (*Find the minimal arity of each function mentioned in the term. Also, note which uses
   427   are not at top level, to see if hBOOL is needed.*)
   428 fun count_constants_term toplev t (const_min_arity, const_needs_hBOOL) =
   429   let val (head, args) = strip_comb t
   430       val n = length args
   431       val (const_min_arity, const_needs_hBOOL) = fold (count_constants_term false) args (const_min_arity, const_needs_hBOOL)
   432   in
   433       case head of
   434           CombConst (a,_,_) => (*predicate or function version of "equal"?*)
   435             let val a = if a="equal" andalso not toplev then "c_fequal" else a
   436             val const_min_arity = Symtab.map_default (a,n) (curry Int.min n) const_min_arity
   437             in
   438               if toplev then (const_min_arity, const_needs_hBOOL)
   439               else (const_min_arity, Symtab.update (a,true) (const_needs_hBOOL))
   440             end
   441         | ts => (const_min_arity, const_needs_hBOOL)
   442   end;
   443 
   444 (*A literal is a top-level term*)
   445 fun count_constants_lit (Literal (_,t)) (const_min_arity, const_needs_hBOOL) =
   446   count_constants_term true t (const_min_arity, const_needs_hBOOL);
   447 
   448 fun count_constants_clause (Clause{literals,...}) (const_min_arity, const_needs_hBOOL) =
   449   fold count_constants_lit literals (const_min_arity, const_needs_hBOOL);
   450 
   451 fun display_arity const_needs_hBOOL (c,n) =
   452   Output.debug (fn () => "Constant: " ^ c ^ " arity:\t" ^ Int.toString n ^
   453                 (if needs_hBOOL const_needs_hBOOL c then " needs hBOOL" else ""));
   454 
   455 fun count_constants (conjectures, axclauses, helper_clauses, _, _) =
   456   if minimize_applies then
   457      let val (const_min_arity, const_needs_hBOOL) =
   458           fold count_constants_clause conjectures (Symtab.empty, Symtab.empty)
   459        |> fold count_constants_clause axclauses
   460        |> fold count_constants_clause helper_clauses
   461      val _ = List.app (display_arity const_needs_hBOOL) (Symtab.dest (const_min_arity))
   462      in (const_min_arity, const_needs_hBOOL) end
   463   else (Symtab.empty, Symtab.empty);
   464 
   465 (* tptp format *)
   466 
   467 fun prepare_clauses dfg thy isFO thms ax_tuples user_lemmas =
   468   let
   469     val conjectures = make_conjecture_clauses dfg thy thms
   470     val (clnames,axclauses) = ListPair.unzip (make_axiom_clauses dfg thy ax_tuples)
   471     val helper_clauses = get_helper_clauses dfg thy isFO (conjectures, axclauses, user_lemmas)
   472   in
   473     (clnames, (conjectures, axclauses, helper_clauses))
   474   end
   475 
   476 (* write TPTP format to a single file *)
   477 fun tptp_write_file filename const_counts (conjectures, axclauses, helper_clauses, classrel_clauses, arity_clauses) =
   478   let
   479     val (tptp_clss,tfree_litss) = ListPair.unzip (map (clause2tptp const_counts) conjectures)
   480     val tfree_clss = map RC.tptp_tfree_clause (List.foldl (op union_string) [] tfree_litss)
   481     val out = TextIO.openOut filename
   482   in
   483     List.app (curry TextIO.output out o #1 o (clause2tptp const_counts)) axclauses;
   484     RC.writeln_strs out tfree_clss;
   485     RC.writeln_strs out tptp_clss;
   486     List.app (curry TextIO.output out o RC.tptp_classrelClause) classrel_clauses;
   487     List.app (curry TextIO.output out o RC.tptp_arity_clause) arity_clauses;
   488     List.app (curry TextIO.output out o #1 o (clause2tptp const_counts)) helper_clauses;
   489     TextIO.closeOut out
   490   end;
   491 
   492 
   493 (* dfg format *)
   494 
   495 fun dfg_write_file filename const_counts (conjectures, axclauses, helper_clauses, classrel_clauses, arity_clauses) =
   496   let
   497     val (dfg_clss, tfree_litss) = ListPair.unzip (map (clause2dfg const_counts) conjectures)
   498     and probname = Path.implode (Path.base (Path.explode filename))
   499     val axstrs = map (#1 o (clause2dfg const_counts)) axclauses
   500     val tfree_clss = map RC.dfg_tfree_clause (RC.union_all tfree_litss)
   501     val out = TextIO.openOut filename
   502     val helper_clauses_strs = map (#1 o (clause2dfg const_counts)) helper_clauses
   503     val (funcs,cl_preds) = decls_of_clauses const_counts (helper_clauses @ conjectures @ axclauses) arity_clauses
   504     and ty_preds = preds_of_clauses axclauses classrel_clauses arity_clauses
   505   in
   506     TextIO.output (out, RC.string_of_start probname);
   507     TextIO.output (out, RC.string_of_descrip probname);
   508     TextIO.output (out, RC.string_of_symbols
   509                           (RC.string_of_funcs funcs)
   510                           (RC.string_of_preds (cl_preds @ ty_preds)));
   511     TextIO.output (out, "list_of_clauses(axioms,cnf).\n");
   512     RC.writeln_strs out axstrs;
   513     List.app (curry TextIO.output out o RC.dfg_classrelClause) classrel_clauses;
   514     List.app (curry TextIO.output out o RC.dfg_arity_clause) arity_clauses;
   515     RC.writeln_strs out helper_clauses_strs;
   516     TextIO.output (out, "end_of_list.\n\nlist_of_clauses(conjectures,cnf).\n");
   517     RC.writeln_strs out tfree_clss;
   518     RC.writeln_strs out dfg_clss;
   519     TextIO.output (out, "end_of_list.\n\n");
   520     (*VarWeight=3 helps the HO problems, probably by counteracting the presence of hAPP*)
   521     TextIO.output (out, "list_of_settings(SPASS).\n{*\nset_flag(VarWeight,3).\n*}\nend_of_list.\n\n");
   522     TextIO.output (out, "end_problem.\n");
   523     TextIO.closeOut out
   524   end;
   525 
   526 end