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