src/HOL/Tools/res_hol_clause.ML
author immler@in.tum.de
Mon Jun 22 17:07:09 2009 +0200 (2009-06-22)
changeset 31752 19a5f1c8a844
parent 31749 8ee34e3ceb5a
child 31791 c9a1caf218c8
permissions -rw-r--r--
use results of relevance-filter to determine additional clauses;
(needed for minimize to be able to prove the same problems as sledgehammer)
     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 make_conjecture_clauses:  bool -> theory -> thm list -> clause list (* dfg thy ccls *)
    32   val make_axiom_clauses: bool ->
    33        theory ->
    34        (thm * (axiom_name * clause_id)) list -> (axiom_name * clause) list (* dfg thy axcls *)
    35   val get_helper_clauses: bool ->
    36        theory ->
    37        bool ->
    38        clause list * (thm * (axiom_name * clause_id)) list * string list ->
    39        clause list
    40   val tptp_write_file: string ->
    41     clause list * clause list * clause list * ResClause.classrelClause list * ResClause.arityClause list -> unit
    42   val dfg_write_file: string -> 
    43     clause list * clause list * clause list * ResClause.classrelClause list * ResClause.arityClause list -> unit
    44 end
    45 
    46 structure ResHolClause: RES_HOL_CLAUSE =
    47 struct
    48 
    49 structure RC = ResClause;
    50 
    51 (* theorems for combinators and function extensionality *)
    52 val ext = thm "HOL.ext";
    53 val comb_I = thm "ATP_Linkup.COMBI_def";
    54 val comb_K = thm "ATP_Linkup.COMBK_def";
    55 val comb_B = thm "ATP_Linkup.COMBB_def";
    56 val comb_C = thm "ATP_Linkup.COMBC_def";
    57 val comb_S = thm "ATP_Linkup.COMBS_def";
    58 val fequal_imp_equal = thm "ATP_Linkup.fequal_imp_equal";
    59 val equal_imp_fequal = thm "ATP_Linkup.equal_imp_fequal";
    60 
    61 
    62 (*The different translations of types*)
    63 datatype type_level = T_FULL | T_CONST;
    64 
    65 val typ_level = T_CONST;
    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 (s, tp) =
   231   if typ_level=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 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 typ_level = T_CONST 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 cnh (head, s, tp) = if head_needs_hBOOL cnh head then wrap_type (s, tp) else s;
   261 
   262 fun string_of_combterm cma cnh t =
   263   let val (head, args) = strip_comb t
   264   in  wrap_type_if cnh (head,
   265                     string_of_applic cma (head, map (string_of_combterm cma cnh) args),
   266                     type_of_combterm t)
   267   end;
   268 
   269 (*Boolean-valued terms are here converted to literals.*)
   270 fun boolify cma cnh t = "hBOOL" ^ RC.paren_pack [string_of_combterm cma cnh t];
   271 
   272 fun string_of_predicate cma cnh t =
   273   case t of
   274       (CombApp(CombApp(CombConst("equal",_,_), t1), t2)) =>
   275           (*DFG only: new TPTP prefers infix equality*)
   276           ("equal" ^ RC.paren_pack [string_of_combterm cma cnh t1, string_of_combterm cma cnh t2])
   277     | _ =>
   278           case #1 (strip_comb t) of
   279               CombConst(c,_,_) => if needs_hBOOL cnh c then boolify cma cnh t else string_of_combterm cma cnh t
   280             | _ => boolify cma cnh t;
   281 
   282 fun string_of_clausename (cls_id,ax_name) =
   283     RC.clause_prefix ^ RC.ascii_of ax_name ^ "_" ^ Int.toString cls_id;
   284 
   285 fun string_of_type_clsname (cls_id,ax_name,idx) =
   286     string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
   287 
   288 
   289 (*** tptp format ***)
   290 
   291 fun tptp_of_equality cma cnh pol (t1,t2) =
   292   let val eqop = if pol then " = " else " != "
   293   in  string_of_combterm cma cnh t1 ^ eqop ^ string_of_combterm cma cnh t2  end;
   294 
   295 fun tptp_literal cma cnh (Literal(pol, CombApp(CombApp(CombConst("equal",_,_), t1), t2))) =
   296       tptp_of_equality cma cnh pol (t1,t2)
   297   | tptp_literal cma cnh (Literal(pol,pred)) =
   298       RC.tptp_sign pol (string_of_predicate cma cnh pred);
   299 
   300 (*Given a clause, returns its literals paired with a list of literals concerning TFrees;
   301   the latter should only occur in conjecture clauses.*)
   302 fun tptp_type_lits cma cnh pos (Clause{literals, ctypes_sorts, ...}) =
   303       (map (tptp_literal cma cnh) literals, 
   304        map (RC.tptp_of_typeLit pos) (RC.add_typs ctypes_sorts));
   305 
   306 fun clause2tptp (cma, cnh) (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   307   let val (lits,tylits) = tptp_type_lits cma cnh (kind = RC.Conjecture) cls
   308   in
   309       (RC.gen_tptp_cls(clause_id,axiom_name,kind,lits,tylits), tylits)
   310   end;
   311 
   312 
   313 (*** dfg format ***)
   314 
   315 fun dfg_literal cma cnh (Literal(pol,pred)) = RC.dfg_sign pol (string_of_predicate cma cnh pred);
   316 
   317 fun dfg_type_lits cma cnh pos (Clause{literals, ctypes_sorts, ...}) =
   318       (map (dfg_literal cma cnh) literals, 
   319        map (RC.dfg_of_typeLit pos) (RC.add_typs ctypes_sorts));
   320 
   321 fun get_uvars (CombConst _) vars = vars
   322   | get_uvars (CombVar(v,_)) vars = (v::vars)
   323   | get_uvars (CombApp(P,Q)) vars = get_uvars P (get_uvars Q vars);
   324 
   325 fun get_uvars_l (Literal(_,c)) = get_uvars c [];
   326 
   327 fun dfg_vars (Clause {literals,...}) = RC.union_all (map get_uvars_l literals);
   328 
   329 fun clause2dfg (cma, cnh) (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   330   let val (lits,tylits) = dfg_type_lits cma cnh (kind = RC.Conjecture) cls
   331       val vars = dfg_vars cls
   332       val tvars = RC.get_tvar_strs ctypes_sorts
   333   in
   334       (RC.gen_dfg_cls(clause_id, axiom_name, kind, lits, tylits, tvars@vars), tylits)
   335   end;
   336 
   337 
   338 (** For DFG format: accumulate function and predicate declarations **)
   339 
   340 fun addtypes tvars tab = List.foldl RC.add_foltype_funcs tab tvars;
   341 
   342 fun add_decls cma cnh (CombConst(c,tp,tvars), (funcs,preds)) =
   343       if c = "equal" then (addtypes tvars funcs, preds)
   344       else
   345 	let val arity = min_arity_of cma c
   346 	    val ntys = if typ_level = T_CONST then length tvars else 0
   347 	    val addit = Symtab.update(c, arity+ntys)
   348 	in
   349 	    if needs_hBOOL cnh c then (addtypes tvars (addit funcs), preds)
   350 	    else (addtypes tvars funcs, addit preds)
   351 	end
   352   | add_decls _ _ (CombVar(_,ctp), (funcs,preds)) =
   353       (RC.add_foltype_funcs (ctp,funcs), preds)
   354   | add_decls cma cnh (CombApp(P,Q),decls) = add_decls cma cnh (P,add_decls cma cnh (Q,decls));
   355 
   356 fun add_literal_decls cma cnh (Literal(_,c), decls) = add_decls cma cnh (c,decls);
   357 
   358 fun add_clause_decls cma cnh (Clause {literals, ...}, decls) =
   359     List.foldl (add_literal_decls cma cnh) decls literals
   360     handle Symtab.DUP a => error ("function " ^ a ^ " has multiple arities")
   361 
   362 fun decls_of_clauses (cma, cnh) clauses arity_clauses =
   363   let val init_functab = Symtab.update (type_wrapper,2) (Symtab.update ("hAPP",2) RC.init_functab)
   364       val init_predtab = Symtab.update ("hBOOL",1) Symtab.empty
   365       val (functab,predtab) = (List.foldl (add_clause_decls cma cnh) (init_functab, init_predtab) clauses)
   366   in
   367       (Symtab.dest (List.foldl RC.add_arityClause_funcs functab arity_clauses),
   368        Symtab.dest predtab)
   369   end;
   370 
   371 fun add_clause_preds (Clause {ctypes_sorts, ...}, preds) =
   372   List.foldl RC.add_type_sort_preds preds ctypes_sorts
   373   handle Symtab.DUP a => error ("predicate " ^ a ^ " has multiple arities")
   374 
   375 (*Higher-order clauses have only the predicates hBOOL and type classes.*)
   376 fun preds_of_clauses clauses clsrel_clauses arity_clauses =
   377     Symtab.dest
   378         (List.foldl RC.add_classrelClause_preds
   379                (List.foldl RC.add_arityClause_preds
   380                       (List.foldl add_clause_preds Symtab.empty clauses)
   381                       arity_clauses)
   382                clsrel_clauses)
   383 
   384 
   385 (**********************************************************************)
   386 (* write clauses to files                                             *)
   387 (**********************************************************************)
   388 
   389 val init_counters =
   390     Symtab.make [("c_COMBI", 0), ("c_COMBK", 0),
   391                  ("c_COMBB", 0), ("c_COMBC", 0),
   392                  ("c_COMBS", 0)];
   393 
   394 fun count_combterm (CombConst(c,tp,_), ct) =
   395      (case Symtab.lookup ct c of NONE => ct  (*no counter*)
   396                                | SOME n => Symtab.update (c,n+1) ct)
   397   | count_combterm (CombVar(v,tp), ct) = ct
   398   | count_combterm (CombApp(t1,t2), ct) = count_combterm(t1, count_combterm(t2, ct));
   399 
   400 fun count_literal (Literal(_,t), ct) = count_combterm(t,ct);
   401 
   402 fun count_clause (Clause{literals,...}, ct) = List.foldl count_literal ct literals;
   403 
   404 fun count_user_clause user_lemmas (Clause{axiom_name,literals,...}, ct) =
   405   if axiom_name mem_string user_lemmas then List.foldl count_literal ct literals
   406   else ct;
   407 
   408 fun cnf_helper_thms thy =
   409   ResAxioms.cnf_rules_pairs thy o map ResAxioms.pairname
   410 
   411 fun get_helper_clauses dfg thy isFO (conjectures, axcls, user_lemmas) =
   412   if isFO then []  (*first-order*)
   413   else
   414     let
   415         val axclauses = map #2 (make_axiom_clauses dfg thy axcls)
   416         val ct0 = List.foldl count_clause init_counters conjectures
   417         val ct = List.foldl (count_user_clause user_lemmas) ct0 axclauses
   418         fun needed c = valOf (Symtab.lookup ct c) > 0
   419         val IK = if needed "c_COMBI" orelse needed "c_COMBK"
   420                  then (Output.debug (fn () => "Include combinator I K"); cnf_helper_thms thy [comb_I,comb_K])
   421                  else []
   422         val BC = if needed "c_COMBB" orelse needed "c_COMBC"
   423                  then (Output.debug (fn () => "Include combinator B C"); cnf_helper_thms thy [comb_B,comb_C])
   424                  else []
   425         val S = if needed "c_COMBS"
   426                 then (Output.debug (fn () => "Include combinator S"); cnf_helper_thms thy [comb_S])
   427                 else []
   428         val other = cnf_helper_thms thy [ext,fequal_imp_equal,equal_imp_fequal]
   429     in
   430         map #2 (make_axiom_clauses dfg thy (other @ IK @ BC @ S))
   431     end;
   432 
   433 (*Find the minimal arity of each function mentioned in the term. Also, note which uses
   434   are not at top level, to see if hBOOL is needed.*)
   435 fun count_constants_term toplev t (const_min_arity, const_needs_hBOOL) =
   436   let val (head, args) = strip_comb t
   437       val n = length args
   438       val (const_min_arity, const_needs_hBOOL) = fold (count_constants_term false) args (const_min_arity, const_needs_hBOOL)
   439   in
   440       case head of
   441           CombConst (a,_,_) => (*predicate or function version of "equal"?*)
   442             let val a = if a="equal" andalso not toplev then "c_fequal" else a
   443             val const_min_arity = Symtab.map_default (a,n) (curry Int.min n) const_min_arity
   444             in
   445               if toplev then (const_min_arity, const_needs_hBOOL)
   446               else (const_min_arity, Symtab.update (a,true) (const_needs_hBOOL))
   447             end
   448         | ts => (const_min_arity, const_needs_hBOOL)
   449   end;
   450 
   451 (*A literal is a top-level term*)
   452 fun count_constants_lit (Literal (_,t)) (const_min_arity, const_needs_hBOOL) =
   453   count_constants_term true t (const_min_arity, const_needs_hBOOL);
   454 
   455 fun count_constants_clause (Clause{literals,...}) (const_min_arity, const_needs_hBOOL) =
   456   fold count_constants_lit literals (const_min_arity, const_needs_hBOOL);
   457 
   458 fun display_arity const_needs_hBOOL (c,n) =
   459   Output.debug (fn () => "Constant: " ^ c ^ " arity:\t" ^ Int.toString n ^
   460                 (if needs_hBOOL const_needs_hBOOL c then " needs hBOOL" else ""));
   461 
   462 fun count_constants (conjectures, axclauses, helper_clauses, _, _) =
   463   if minimize_applies then
   464      let val (const_min_arity, const_needs_hBOOL) =
   465           fold count_constants_clause conjectures (Symtab.empty, Symtab.empty)
   466        |> fold count_constants_clause axclauses
   467        |> fold count_constants_clause helper_clauses
   468      val _ = List.app (display_arity const_needs_hBOOL) (Symtab.dest (const_min_arity))
   469      in (const_min_arity, const_needs_hBOOL) end
   470   else (Symtab.empty, Symtab.empty);
   471 
   472 (* tptp format *)
   473 
   474 fun tptp_write_file filename clauses =
   475   let
   476     val (conjectures, axclauses, helper_clauses, classrel_clauses, arity_clauses) = clauses
   477     val const_counts = count_constants clauses
   478     val (tptp_clss,tfree_litss) = ListPair.unzip (map (clause2tptp const_counts) conjectures)
   479     val tfree_clss = map RC.tptp_tfree_clause (List.foldl (op union_string) [] tfree_litss)
   480     val out = TextIO.openOut filename
   481   in
   482     List.app (curry TextIO.output out o #1 o (clause2tptp const_counts)) axclauses;
   483     RC.writeln_strs out tfree_clss;
   484     RC.writeln_strs out tptp_clss;
   485     List.app (curry TextIO.output out o RC.tptp_classrelClause) classrel_clauses;
   486     List.app (curry TextIO.output out o RC.tptp_arity_clause) arity_clauses;
   487     List.app (curry TextIO.output out o #1 o (clause2tptp const_counts)) helper_clauses;
   488     TextIO.closeOut out
   489   end;
   490 
   491 
   492 (* dfg format *)
   493 
   494 fun dfg_write_file filename clauses =
   495   let
   496     val (conjectures, axclauses, helper_clauses, classrel_clauses, arity_clauses) = clauses
   497     val const_counts = count_constants clauses
   498     val (dfg_clss, tfree_litss) = ListPair.unzip (map (clause2dfg const_counts) conjectures)
   499     and probname = Path.implode (Path.base (Path.explode filename))
   500     val axstrs = map (#1 o (clause2dfg const_counts)) axclauses
   501     val tfree_clss = map RC.dfg_tfree_clause (RC.union_all tfree_litss)
   502     val out = TextIO.openOut filename
   503     val helper_clauses_strs = map (#1 o (clause2dfg const_counts)) helper_clauses
   504     val (funcs,cl_preds) = decls_of_clauses const_counts (helper_clauses @ conjectures @ axclauses) arity_clauses
   505     and ty_preds = preds_of_clauses axclauses classrel_clauses arity_clauses
   506   in
   507     TextIO.output (out, RC.string_of_start probname);
   508     TextIO.output (out, RC.string_of_descrip probname);
   509     TextIO.output (out, RC.string_of_symbols
   510                           (RC.string_of_funcs funcs)
   511                           (RC.string_of_preds (cl_preds @ ty_preds)));
   512     TextIO.output (out, "list_of_clauses(axioms,cnf).\n");
   513     RC.writeln_strs out axstrs;
   514     List.app (curry TextIO.output out o RC.dfg_classrelClause) classrel_clauses;
   515     List.app (curry TextIO.output out o RC.dfg_arity_clause) arity_clauses;
   516     RC.writeln_strs out helper_clauses_strs;
   517     TextIO.output (out, "end_of_list.\n\nlist_of_clauses(conjectures,cnf).\n");
   518     RC.writeln_strs out tfree_clss;
   519     RC.writeln_strs out dfg_clss;
   520     TextIO.output (out, "end_of_list.\n\n");
   521     (*VarWeight=3 helps the HO problems, probably by counteracting the presence of hAPP*)
   522     TextIO.output (out, "list_of_settings(SPASS).\n{*\nset_flag(VarWeight,3).\n*}\nend_of_list.\n\n");
   523     TextIO.output (out, "end_problem.\n");
   524     TextIO.closeOut out
   525   end;
   526 
   527 end