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