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