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