src/HOL/Tools/Sledgehammer/sledgehammer_fol_clause.ML
author blanchet
Mon Mar 29 15:50:18 2010 +0200 (2010-03-29)
changeset 36062 194cb6e3c13f
parent 35865 2f8fb5242799
child 36063 cdc6855a6387
permissions -rw-r--r--
get rid of Polyhash, since it's no longer used
     1 (*  Title:      HOL/Tools/Sledgehammer/sledgehammer_fol_clause.ML
     2     Author:     Jia Meng, Cambridge University Computer Laboratory
     3 
     4 Storing/printing FOL clauses and arity clauses.  Typed equality is
     5 treated differently.
     6 
     7 FIXME: combine with res_hol_clause!
     8 *)
     9 
    10 signature SLEDGEHAMMER_FOL_CLAUSE =
    11 sig
    12   val schematic_var_prefix: string
    13   val fixed_var_prefix: string
    14   val tvar_prefix: string
    15   val tfree_prefix: string
    16   val clause_prefix: string
    17   val const_prefix: string
    18   val tconst_prefix: string
    19   val class_prefix: string
    20   val union_all: ''a list list -> ''a list
    21   val const_trans_table: string Symtab.table
    22   val type_const_trans_table: string Symtab.table
    23   val ascii_of: string -> string
    24   val undo_ascii_of: string -> string
    25   val paren_pack : string list -> string
    26   val make_schematic_var : string * int -> string
    27   val make_fixed_var : string -> string
    28   val make_schematic_type_var : string * int -> string
    29   val make_fixed_type_var : string -> string
    30   val make_fixed_const : bool -> string -> string
    31   val make_fixed_type_const : bool -> string -> string
    32   val make_type_class : string -> string
    33   datatype kind = Axiom | Conjecture
    34   type axiom_name = string
    35   datatype fol_type =
    36       AtomV of string
    37     | AtomF of string
    38     | Comp of string * fol_type list
    39   val string_of_fol_type : fol_type -> string
    40   datatype type_literal = LTVar of string * string | LTFree of string * string
    41   exception CLAUSE of string * term
    42   val add_typs : typ list -> type_literal list
    43   val get_tvar_strs: typ list -> string list
    44   datatype arLit =
    45       TConsLit of class * string * string list
    46     | TVarLit of class * string
    47   datatype arity_clause = ArityClause of
    48    {axiom_name: axiom_name, conclLit: arLit, premLits: arLit list}
    49   datatype classrel_clause = ClassrelClause of
    50    {axiom_name: axiom_name, subclass: class, superclass: class}
    51   val make_classrel_clauses: theory -> class list -> class list -> classrel_clause list
    52   val make_arity_clauses_dfg: bool -> theory -> string list -> class list -> class list * arity_clause list
    53   val make_arity_clauses: theory -> string list -> class list -> class list * arity_clause list
    54   val add_type_sort_preds: typ * int Symtab.table -> int Symtab.table
    55   val add_classrel_clause_preds : classrel_clause * int Symtab.table -> int Symtab.table
    56   val class_of_arityLit: arLit -> class
    57   val add_arity_clause_preds: arity_clause * int Symtab.table -> int Symtab.table
    58   val add_foltype_funcs: fol_type * int Symtab.table -> int Symtab.table
    59   val add_arity_clause_funcs: arity_clause * int Symtab.table -> int Symtab.table
    60   val init_functab: int Symtab.table
    61   val dfg_sign: bool -> string -> string
    62   val dfg_of_typeLit: bool -> type_literal -> string
    63   val gen_dfg_cls: int * string * kind * string list * string list * string list -> string
    64   val string_of_preds: (string * Int.int) list -> string
    65   val string_of_funcs: (string * int) list -> string
    66   val string_of_symbols: string -> string -> string
    67   val string_of_start: string -> string
    68   val string_of_descrip : string -> string
    69   val dfg_tfree_clause : string -> string
    70   val dfg_classrel_clause: classrel_clause -> string
    71   val dfg_arity_clause: arity_clause -> string
    72   val tptp_sign: bool -> string -> string
    73   val tptp_of_typeLit : bool -> type_literal -> string
    74   val gen_tptp_cls : int * string * kind * string list * string list -> string
    75   val tptp_tfree_clause : string -> string
    76   val tptp_arity_clause : arity_clause -> string
    77   val tptp_classrel_clause : classrel_clause -> string
    78 end
    79 
    80 structure Sledgehammer_FOL_Clause : SLEDGEHAMMER_FOL_CLAUSE =
    81 struct
    82 
    83 val schematic_var_prefix = "V_";
    84 val fixed_var_prefix = "v_";
    85 
    86 val tvar_prefix = "T_";
    87 val tfree_prefix = "t_";
    88 
    89 val clause_prefix = "cls_";
    90 val arclause_prefix = "clsarity_"
    91 val clrelclause_prefix = "clsrel_";
    92 
    93 val const_prefix = "c_";
    94 val tconst_prefix = "tc_";
    95 val class_prefix = "class_";
    96 
    97 fun union_all xss = List.foldl (uncurry (union (op =))) [] xss;
    98 
    99 (* Provide readable names for the more common symbolic functions *)
   100 val const_trans_table =
   101   Symtab.make [(@{const_name "op ="}, "equal"),
   102                (@{const_name Orderings.less_eq}, "lessequals"),
   103                (@{const_name "op &"}, "and"),
   104                (@{const_name "op |"}, "or"),
   105                (@{const_name "op -->"}, "implies"),
   106                (@{const_name "op :"}, "in"),
   107                (@{const_name fequal}, "fequal"),
   108                (@{const_name COMBI}, "COMBI"),
   109                (@{const_name COMBK}, "COMBK"),
   110                (@{const_name COMBB}, "COMBB"),
   111                (@{const_name COMBC}, "COMBC"),
   112                (@{const_name COMBS}, "COMBS")];
   113 
   114 val type_const_trans_table =
   115   Symtab.make [("*", "prod"), ("+", "sum"), ("~=>", "map")];
   116 
   117 (*Escaping of special characters.
   118   Alphanumeric characters are left unchanged.
   119   The character _ goes to __
   120   Characters in the range ASCII space to / go to _A to _P, respectively.
   121   Other printing characters go to _nnn where nnn is the decimal ASCII code.*)
   122 val A_minus_space = Char.ord #"A" - Char.ord #" ";
   123 
   124 fun stringN_of_int 0 _ = ""
   125   | stringN_of_int k n = stringN_of_int (k-1) (n div 10) ^ Int.toString (n mod 10);
   126 
   127 fun ascii_of_c c =
   128   if Char.isAlphaNum c then String.str c
   129   else if c = #"_" then "__"
   130   else if #" " <= c andalso c <= #"/"
   131        then "_" ^ String.str (Char.chr (Char.ord c + A_minus_space))
   132   else if Char.isPrint c
   133        then ("_" ^ stringN_of_int 3 (Char.ord c))  (*fixed width, in case more digits follow*)
   134   else ""
   135 
   136 val ascii_of = String.translate ascii_of_c;
   137 
   138 (** Remove ASCII armouring from names in proof files **)
   139 
   140 (*We don't raise error exceptions because this code can run inside the watcher.
   141   Also, the errors are "impossible" (hah!)*)
   142 fun undo_ascii_aux rcs [] = String.implode(rev rcs)
   143   | undo_ascii_aux rcs [#"_"] = undo_ascii_aux (#"_"::rcs) []  (*ERROR*)
   144       (*Three types of _ escapes: __, _A to _P, _nnn*)
   145   | undo_ascii_aux rcs (#"_" :: #"_" :: cs) = undo_ascii_aux (#"_"::rcs) cs
   146   | undo_ascii_aux rcs (#"_" :: c :: cs) =
   147       if #"A" <= c andalso c<= #"P"  (*translation of #" " to #"/"*)
   148       then undo_ascii_aux (Char.chr(Char.ord c - A_minus_space) :: rcs) cs
   149       else
   150         let val digits = List.take (c::cs, 3) handle Subscript => []
   151         in
   152             case Int.fromString (String.implode digits) of
   153                 NONE => undo_ascii_aux (c:: #"_"::rcs) cs  (*ERROR*)
   154               | SOME n => undo_ascii_aux (Char.chr n :: rcs) (List.drop (cs, 2))
   155         end
   156   | undo_ascii_aux rcs (c::cs) = undo_ascii_aux (c::rcs) cs;
   157 
   158 val undo_ascii_of = undo_ascii_aux [] o String.explode;
   159 
   160 (* convert a list of strings into one single string; surrounded by brackets *)
   161 fun paren_pack [] = ""   (*empty argument list*)
   162   | paren_pack strings = "(" ^ commas strings ^ ")";
   163 
   164 (*TSTP format uses (...) rather than the old [...]*)
   165 fun tptp_pack strings = "(" ^ space_implode " | " strings ^ ")";
   166 
   167 
   168 (*Remove the initial ' character from a type variable, if it is present*)
   169 fun trim_type_var s =
   170   if s <> "" andalso String.sub(s,0) = #"'" then String.extract(s,1,NONE)
   171   else error ("trim_type: Malformed type variable encountered: " ^ s);
   172 
   173 fun ascii_of_indexname (v,0) = ascii_of v
   174   | ascii_of_indexname (v,i) = ascii_of v ^ "_" ^ Int.toString i;
   175 
   176 fun make_schematic_var v = schematic_var_prefix ^ (ascii_of_indexname v);
   177 fun make_fixed_var x = fixed_var_prefix ^ (ascii_of x);
   178 
   179 fun make_schematic_type_var (x,i) =
   180       tvar_prefix ^ (ascii_of_indexname (trim_type_var x,i));
   181 fun make_fixed_type_var x = tfree_prefix ^ (ascii_of (trim_type_var x));
   182 
   183 (* HACK because SPASS 3.0 truncates identifiers to 63 characters. (This is
   184    solved in 3.7 and perhaps in earlier versions too.) *)
   185 (* 32-bit hash, so we expect no collisions. *)
   186 fun controlled_length dfg s =
   187   if dfg andalso size s > 60 then Word.toString (hashw_string (s, 0w0))
   188   else s;
   189 
   190 fun lookup_const dfg c =
   191     case Symtab.lookup const_trans_table c of
   192         SOME c' => c'
   193       | NONE => controlled_length dfg (ascii_of c);
   194 
   195 fun lookup_type_const dfg c =
   196     case Symtab.lookup type_const_trans_table c of
   197         SOME c' => c'
   198       | NONE => controlled_length dfg (ascii_of c);
   199 
   200 (* "op =" MUST BE "equal" because it's built into ATPs. *)
   201 fun make_fixed_const _ (@{const_name "op ="}) = "equal"
   202   | make_fixed_const dfg c = const_prefix ^ lookup_const dfg c;
   203 
   204 fun make_fixed_type_const dfg c = tconst_prefix ^ lookup_type_const dfg c;
   205 
   206 fun make_type_class clas = class_prefix ^ ascii_of clas;
   207 
   208 
   209 (***** definitions and functions for FOL clauses, for conversion to TPTP or DFG format. *****)
   210 
   211 datatype kind = Axiom | Conjecture;
   212 
   213 type axiom_name = string;
   214 
   215 (**** Isabelle FOL clauses ****)
   216 
   217 (*FIXME: give the constructors more sensible names*)
   218 datatype fol_type = AtomV of string
   219                   | AtomF of string
   220                   | Comp of string * fol_type list;
   221 
   222 fun string_of_fol_type (AtomV x) = x
   223   | string_of_fol_type (AtomF x) = x
   224   | string_of_fol_type (Comp(tcon,tps)) =
   225       tcon ^ (paren_pack (map string_of_fol_type tps));
   226 
   227 (*First string is the type class; the second is a TVar or TFfree*)
   228 datatype type_literal = LTVar of string * string | LTFree of string * string;
   229 
   230 exception CLAUSE of string * term;
   231 
   232 fun atomic_type (TFree (a,_)) = AtomF(make_fixed_type_var a)
   233   | atomic_type (TVar (v,_))  = AtomV(make_schematic_type_var v);
   234 
   235 (*Flatten a type to a fol_type while accumulating sort constraints on the TFrees and
   236   TVars it contains.*)
   237 fun type_of dfg (Type (a, Ts)) =
   238       let val (folTyps, ts) = types_of dfg Ts
   239           val t = make_fixed_type_const dfg a
   240       in (Comp(t,folTyps), ts) end
   241   | type_of dfg T = (atomic_type T, [T])
   242 and types_of dfg Ts =
   243       let val (folTyps,ts) = ListPair.unzip (map (type_of dfg) Ts)
   244       in (folTyps, union_all ts) end;
   245 
   246 (*Make literals for sorted type variables*)
   247 fun sorts_on_typs_aux (_, [])   = []
   248   | sorts_on_typs_aux ((x,i),  s::ss) =
   249       let val sorts = sorts_on_typs_aux ((x,i), ss)
   250       in
   251           if s = "HOL.type" then sorts
   252           else if i = ~1 then LTFree(make_type_class s, make_fixed_type_var x) :: sorts
   253           else LTVar(make_type_class s, make_schematic_type_var (x,i)) :: sorts
   254       end;
   255 
   256 fun sorts_on_typs (TFree (a,s)) = sorts_on_typs_aux ((a,~1),s)
   257   | sorts_on_typs (TVar (v,s))  = sorts_on_typs_aux (v,s);
   258 
   259 fun pred_of_sort (LTVar (s,ty)) = (s,1)
   260   | pred_of_sort (LTFree (s,ty)) = (s,1)
   261 
   262 (*Given a list of sorted type variables, return a list of type literals.*)
   263 fun add_typs Ts = List.foldl (uncurry (union (op =))) [] (map sorts_on_typs Ts);
   264 
   265 (*The correct treatment of TFrees like 'a in lemmas (axiom clauses) is not clear.
   266   * Ignoring them leads to unsound proofs, since we do nothing to ensure that 'a
   267     in a lemma has the same sort as 'a in the conjecture.
   268   * Deleting such clauses will lead to problems with locales in other use of local results
   269     where 'a is fixed. Probably we should delete clauses unless the sorts agree.
   270   * Currently we include a class constraint in the clause, exactly as with TVars.
   271 *)
   272 
   273 (** make axiom and conjecture clauses. **)
   274 
   275 fun get_tvar_strs [] = []
   276   | get_tvar_strs ((TVar (indx,s))::Ts) =
   277       insert (op =) (make_schematic_type_var indx) (get_tvar_strs Ts)
   278   | get_tvar_strs((TFree _)::Ts) = get_tvar_strs Ts
   279 
   280 
   281 
   282 (**** Isabelle arities ****)
   283 
   284 exception ARCLAUSE of string;
   285 
   286 datatype arLit = TConsLit of class * string * string list
   287                | TVarLit of class * string;
   288 
   289 datatype arity_clause =
   290          ArityClause of {axiom_name: axiom_name,
   291                          conclLit: arLit,
   292                          premLits: arLit list};
   293 
   294 
   295 fun gen_TVars 0 = []
   296   | gen_TVars n = ("T_" ^ Int.toString n) :: gen_TVars (n-1);
   297 
   298 fun pack_sort(_,[])  = []
   299   | pack_sort(tvar, "HOL.type"::srt) = pack_sort(tvar, srt)   (*IGNORE sort "type"*)
   300   | pack_sort(tvar, cls::srt) =  (cls, tvar) :: pack_sort(tvar, srt);
   301 
   302 (*Arity of type constructor tcon :: (arg1,...,argN)res*)
   303 fun make_axiom_arity_clause dfg (tcons, axiom_name, (cls,args)) =
   304    let val tvars = gen_TVars (length args)
   305        val tvars_srts = ListPair.zip (tvars,args)
   306    in
   307       ArityClause {axiom_name = axiom_name, 
   308                    conclLit = TConsLit (cls, make_fixed_type_const dfg tcons, tvars),
   309                    premLits = map TVarLit (union_all(map pack_sort tvars_srts))}
   310    end;
   311 
   312 
   313 (**** Isabelle class relations ****)
   314 
   315 datatype classrel_clause =
   316          ClassrelClause of {axiom_name: axiom_name,
   317                             subclass: class,
   318                             superclass: class};
   319 
   320 (*Generate all pairs (sub,super) such that sub is a proper subclass of super in theory thy.*)
   321 fun class_pairs thy [] supers = []
   322   | class_pairs thy subs supers =
   323       let val class_less = Sorts.class_less(Sign.classes_of thy)
   324           fun add_super sub (super,pairs) =
   325                 if class_less (sub,super) then (sub,super)::pairs else pairs
   326           fun add_supers (sub,pairs) = List.foldl (add_super sub) pairs supers
   327       in  List.foldl add_supers [] subs  end;
   328 
   329 fun make_classrel_clause (sub,super) =
   330   ClassrelClause {axiom_name = clrelclause_prefix ^ ascii_of sub ^ "_" ^ ascii_of super,
   331                   subclass = make_type_class sub,
   332                   superclass = make_type_class super};
   333 
   334 fun make_classrel_clauses thy subs supers =
   335   map make_classrel_clause (class_pairs thy subs supers);
   336 
   337 
   338 (** Isabelle arities **)
   339 
   340 fun arity_clause dfg _ _ (tcons, []) = []
   341   | arity_clause dfg seen n (tcons, ("HOL.type",_)::ars) =  (*ignore*)
   342       arity_clause dfg seen n (tcons,ars)
   343   | arity_clause dfg seen n (tcons, (ar as (class,_)) :: ars) =
   344       if class mem_string seen then (*multiple arities for the same tycon, class pair*)
   345           make_axiom_arity_clause dfg (tcons, lookup_type_const dfg tcons ^ "_" ^ class ^ "_" ^ Int.toString n, ar) ::
   346           arity_clause dfg seen (n+1) (tcons,ars)
   347       else
   348           make_axiom_arity_clause dfg (tcons, lookup_type_const dfg tcons ^ "_" ^ class, ar) ::
   349           arity_clause dfg (class::seen) n (tcons,ars)
   350 
   351 fun multi_arity_clause dfg [] = []
   352   | multi_arity_clause dfg ((tcons,ars) :: tc_arlists) =
   353       arity_clause dfg [] 1 (tcons, ars)  @  multi_arity_clause dfg tc_arlists
   354 
   355 (*Generate all pairs (tycon,class,sorts) such that tycon belongs to class in theory thy
   356   provided its arguments have the corresponding sorts.*)
   357 fun type_class_pairs thy tycons classes =
   358   let val alg = Sign.classes_of thy
   359       fun domain_sorts (tycon,class) = Sorts.mg_domain alg tycon [class]
   360       fun add_class tycon (class,pairs) =
   361             (class, domain_sorts(tycon,class))::pairs
   362             handle Sorts.CLASS_ERROR _ => pairs
   363       fun try_classes tycon = (tycon, List.foldl (add_class tycon) [] classes)
   364   in  map try_classes tycons  end;
   365 
   366 (*Proving one (tycon, class) membership may require proving others, so iterate.*)
   367 fun iter_type_class_pairs thy tycons [] = ([], [])
   368   | iter_type_class_pairs thy tycons classes =
   369       let val cpairs = type_class_pairs thy tycons classes
   370           val newclasses = union_all (union_all (union_all (map (map #2 o #2) cpairs)))
   371             |> subtract (op =) classes |> subtract (op =) HOLogic.typeS
   372           val (classes', cpairs') = iter_type_class_pairs thy tycons newclasses
   373       in (union (op =) classes' classes, union (op =) cpairs' cpairs) end;
   374 
   375 fun make_arity_clauses_dfg dfg thy tycons classes =
   376   let val (classes', cpairs) = iter_type_class_pairs thy tycons classes
   377   in  (classes', multi_arity_clause dfg cpairs)  end;
   378 val make_arity_clauses = make_arity_clauses_dfg false;
   379 
   380 (**** Find occurrences of predicates in clauses ****)
   381 
   382 (*FIXME: multiple-arity checking doesn't work, as update_new is the wrong
   383   function (it flags repeated declarations of a function, even with the same arity)*)
   384 
   385 fun update_many (tab, keypairs) = List.foldl (uncurry Symtab.update) tab keypairs;
   386 
   387 fun add_type_sort_preds (T, preds) =
   388       update_many (preds, map pred_of_sort (sorts_on_typs T));
   389 
   390 fun add_classrel_clause_preds (ClassrelClause {subclass,superclass,...}, preds) =
   391   Symtab.update (subclass,1) (Symtab.update (superclass,1) preds);
   392 
   393 fun class_of_arityLit (TConsLit (tclass, _, _)) = tclass
   394   | class_of_arityLit (TVarLit (tclass, _)) = tclass;
   395 
   396 fun add_arity_clause_preds (ArityClause {conclLit,premLits,...}, preds) =
   397   let val classes = map (make_type_class o class_of_arityLit) (conclLit::premLits)
   398       fun upd (class,preds) = Symtab.update (class,1) preds
   399   in  List.foldl upd preds classes  end;
   400 
   401 (*** Find occurrences of functions in clauses ***)
   402 
   403 fun add_foltype_funcs (AtomV _, funcs) = funcs
   404   | add_foltype_funcs (AtomF a, funcs) = Symtab.update (a,0) funcs
   405   | add_foltype_funcs (Comp(a,tys), funcs) =
   406       List.foldl add_foltype_funcs (Symtab.update (a, length tys) funcs) tys;
   407 
   408 (*TFrees are recorded as constants*)
   409 fun add_type_sort_funcs (TVar _, funcs) = funcs
   410   | add_type_sort_funcs (TFree (a, _), funcs) =
   411       Symtab.update (make_fixed_type_var a, 0) funcs
   412 
   413 fun add_arity_clause_funcs (ArityClause {conclLit,...}, funcs) =
   414   let val TConsLit (_, tcons, tvars) = conclLit
   415   in  Symtab.update (tcons, length tvars) funcs  end;
   416 
   417 (*This type can be overlooked because it is built-in...*)
   418 val init_functab = Symtab.update ("tc_itself", 1) Symtab.empty;
   419 
   420 
   421 (**** String-oriented operations ****)
   422 
   423 fun string_of_clausename (cls_id,ax_name) =
   424     clause_prefix ^ ascii_of ax_name ^ "_" ^ Int.toString cls_id;
   425 
   426 fun string_of_type_clsname (cls_id,ax_name,idx) =
   427     string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
   428 
   429 
   430 (**** Producing DFG files ****)
   431 
   432 (*Attach sign in DFG syntax: false means negate.*)
   433 fun dfg_sign true s = s
   434   | dfg_sign false s = "not(" ^ s ^ ")"
   435 
   436 fun dfg_of_typeLit pos (LTVar (s,ty))  = dfg_sign pos (s ^ "(" ^ ty ^ ")")
   437   | dfg_of_typeLit pos (LTFree (s,ty)) = dfg_sign pos (s ^ "(" ^ ty ^ ")");
   438 
   439 (*Enclose the clause body by quantifiers, if necessary*)
   440 fun dfg_forall [] body = body
   441   | dfg_forall vars body = "forall([" ^ commas vars ^ "],\n" ^ body ^ ")"
   442 
   443 fun gen_dfg_cls (cls_id, ax_name, Axiom, lits, tylits, vars) =
   444       "clause( %(axiom)\n" ^
   445       dfg_forall vars ("or(" ^ commas (tylits@lits) ^ ")") ^ ",\n" ^
   446       string_of_clausename (cls_id,ax_name) ^  ").\n\n"
   447   | gen_dfg_cls (cls_id, ax_name, Conjecture, lits, _, vars) =
   448       "clause( %(negated_conjecture)\n" ^
   449       dfg_forall vars ("or(" ^ commas lits ^ ")") ^ ",\n" ^
   450       string_of_clausename (cls_id,ax_name) ^  ").\n\n";
   451 
   452 fun string_of_arity (name, num) =  "(" ^ name ^ "," ^ Int.toString num ^ ")"
   453 
   454 fun string_of_preds [] = ""
   455   | string_of_preds preds = "predicates[" ^ commas(map string_of_arity preds) ^ "].\n";
   456 
   457 fun string_of_funcs [] = ""
   458   | string_of_funcs funcs = "functions[" ^ commas(map string_of_arity funcs) ^ "].\n" ;
   459 
   460 fun string_of_symbols predstr funcstr =
   461   "list_of_symbols.\n" ^ predstr  ^ funcstr  ^ "end_of_list.\n\n";
   462 
   463 fun string_of_start name = "begin_problem(" ^ name ^ ").\n\n";
   464 
   465 fun string_of_descrip name =
   466   "list_of_descriptions.\nname({*" ^ name ^
   467   "*}).\nauthor({*Isabelle*}).\nstatus(unknown).\ndescription({*auto-generated*}).\nend_of_list.\n\n"
   468 
   469 fun dfg_tfree_clause tfree_lit =
   470   "clause( %(negated_conjecture)\n" ^ "or( " ^ tfree_lit ^ "),\n" ^ "tfree_tcs" ^ ").\n\n"
   471 
   472 fun dfg_of_arLit (TConsLit (c,t,args)) =
   473       dfg_sign true (make_type_class c ^ "(" ^ t ^ paren_pack args ^ ")")
   474   | dfg_of_arLit (TVarLit (c,str)) =
   475       dfg_sign false (make_type_class c ^ "(" ^ str ^ ")")
   476 
   477 fun dfg_classrelLits sub sup =  "not(" ^ sub ^ "(T)), " ^ sup ^ "(T)";
   478 
   479 fun dfg_classrel_clause (ClassrelClause {axiom_name,subclass,superclass,...}) =
   480   "clause(forall([T],\nor( " ^ dfg_classrelLits subclass superclass ^ ")),\n" ^
   481   axiom_name ^ ").\n\n";
   482 
   483 fun string_of_ar axiom_name = arclause_prefix ^ ascii_of axiom_name;
   484 
   485 fun dfg_arity_clause (ArityClause{axiom_name,conclLit,premLits,...}) =
   486   let val TConsLit (_,_,tvars) = conclLit
   487       val lits = map dfg_of_arLit (conclLit :: premLits)
   488   in
   489       "clause( %(axiom)\n" ^
   490       dfg_forall tvars ("or( " ^ commas lits ^ ")") ^ ",\n" ^
   491       string_of_ar axiom_name ^ ").\n\n"
   492   end;
   493 
   494 
   495 (**** Produce TPTP files ****)
   496 
   497 (*Attach sign in TPTP syntax: false means negate.*)
   498 fun tptp_sign true s = s
   499   | tptp_sign false s = "~ " ^ s
   500 
   501 fun tptp_of_typeLit pos (LTVar (s,ty))  = tptp_sign pos (s ^ "(" ^ ty ^ ")")
   502   | tptp_of_typeLit pos (LTFree (s,ty)) = tptp_sign pos  (s ^ "(" ^ ty ^ ")");
   503 
   504 fun gen_tptp_cls (cls_id,ax_name,Axiom,lits,tylits) =
   505       "cnf(" ^ string_of_clausename (cls_id,ax_name) ^ ",axiom," ^ 
   506                tptp_pack (tylits@lits) ^ ").\n"
   507   | gen_tptp_cls (cls_id,ax_name,Conjecture,lits,_) =
   508       "cnf(" ^ string_of_clausename (cls_id,ax_name) ^ ",negated_conjecture," ^ 
   509                tptp_pack lits ^ ").\n";
   510 
   511 fun tptp_tfree_clause tfree_lit =
   512     "cnf(" ^ "tfree_tcs," ^ "negated_conjecture" ^ "," ^ tptp_pack[tfree_lit] ^ ").\n";
   513 
   514 fun tptp_of_arLit (TConsLit (c,t,args)) =
   515       tptp_sign true (make_type_class c ^ "(" ^ t ^ paren_pack args ^ ")")
   516   | tptp_of_arLit (TVarLit (c,str)) =
   517       tptp_sign false (make_type_class c ^ "(" ^ str ^ ")")
   518 
   519 fun tptp_arity_clause (ArityClause{axiom_name,conclLit,premLits,...}) =
   520   "cnf(" ^ string_of_ar axiom_name ^ ",axiom," ^
   521   tptp_pack (map tptp_of_arLit (conclLit :: premLits)) ^ ").\n";
   522 
   523 fun tptp_classrelLits sub sup =
   524   let val tvar = "(T)"
   525   in  tptp_pack [tptp_sign false (sub^tvar), tptp_sign true (sup^tvar)]  end;
   526 
   527 fun tptp_classrel_clause (ClassrelClause {axiom_name,subclass,superclass,...}) =
   528   "cnf(" ^ axiom_name ^ ",axiom," ^ tptp_classrelLits subclass superclass ^ ").\n"
   529 
   530 end;