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