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