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