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