src/HOL/Tools/Sledgehammer/sledgehammer_fol_clause.ML
author blanchet
Mon Jun 21 12:31:41 2010 +0200 (2010-06-21 ago)
changeset 37479 f6b1ee5b420b
parent 37414 d0cea0796295
child 37498 b426cbdb5a23
permissions -rw-r--r--
try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
     1 (*  Title:      HOL/Tools/Sledgehammer/sledgehammer_fol_clause.ML
     2     Author:     Jia Meng, Cambridge University Computer Laboratory
     3     Author:     Jasmin Blanchette, TU Muenchen
     4 
     5 Storing/printing FOL clauses and arity clauses.  Typed equality is
     6 treated differently.
     7 
     8 FIXME: combine with sledgehammer_hol_clause!
     9 *)
    10 
    11 signature SLEDGEHAMMER_FOL_CLAUSE =
    12 sig
    13   val schematic_var_prefix: string
    14   val fixed_var_prefix: string
    15   val tvar_prefix: string
    16   val tfree_prefix: string
    17   val clause_prefix: string
    18   val const_prefix: string
    19   val tconst_prefix: string
    20   val class_prefix: string
    21   val union_all: ''a list list -> ''a list
    22   val const_trans_table: string Symtab.table
    23   val type_const_trans_table: string Symtab.table
    24   val ascii_of: string -> string
    25   val undo_ascii_of: string -> string
    26   val paren_pack : string list -> string
    27   val make_schematic_var : string * int -> string
    28   val make_fixed_var : string -> string
    29   val make_schematic_type_var : string * int -> string
    30   val make_fixed_type_var : string -> string
    31   val make_fixed_const : bool -> string -> string
    32   val make_fixed_type_const : bool -> string -> string
    33   val make_type_class : string -> string
    34   type name = string * string
    35   type name_pool = string Symtab.table * string Symtab.table
    36   val empty_name_pool : bool -> name_pool option
    37   val pool_map : ('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b
    38   val nice_name : name -> name_pool option -> string * name_pool option
    39   datatype kind = Axiom | Conjecture
    40   type axiom_name = string
    41   datatype fol_type =
    42     TyVar of name |
    43     TyFree of name |
    44     TyConstr of name * fol_type list
    45   val string_of_fol_type :
    46     fol_type -> name_pool option -> string * name_pool option
    47   datatype type_literal =
    48     TyLitVar of string * name |
    49     TyLitFree of string * name
    50   exception CLAUSE of string * term
    51   val type_literals_for_types : typ list -> type_literal list
    52   val get_tvar_strs: typ list -> string list
    53   datatype arLit =
    54       TConsLit of class * string * string list
    55     | TVarLit of class * string
    56   datatype arity_clause = ArityClause of
    57    {axiom_name: axiom_name, conclLit: arLit, premLits: arLit list}
    58   datatype classrel_clause = ClassrelClause of
    59    {axiom_name: axiom_name, subclass: class, superclass: class}
    60   val make_classrel_clauses: theory -> class list -> class list -> classrel_clause list
    61   val make_arity_clauses_dfg: bool -> theory -> string list -> class list -> class list * arity_clause list
    62   val make_arity_clauses: theory -> string list -> class list -> class list * arity_clause list
    63   val add_type_sort_preds: typ -> int Symtab.table -> int Symtab.table
    64   val add_classrel_clause_preds :
    65     classrel_clause -> int Symtab.table -> int Symtab.table
    66   val class_of_arityLit: arLit -> class
    67   val add_arity_clause_preds: arity_clause -> int Symtab.table -> int Symtab.table
    68   val add_fol_type_funcs: fol_type -> int Symtab.table -> int Symtab.table
    69   val add_arity_clause_funcs:
    70     arity_clause -> int Symtab.table -> int Symtab.table
    71   val init_functab: int Symtab.table
    72   val dfg_sign: bool -> string -> string
    73   val dfg_of_type_literal: bool -> type_literal -> string
    74   val gen_dfg_cls: int * string * kind * string list * string list * 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_classrel_clause: classrel_clause -> string
    82   val dfg_arity_clause: arity_clause -> string
    83   val tptp_sign: bool -> string -> string
    84   val tptp_of_type_literal :
    85     bool -> type_literal -> name_pool option -> string * name_pool option
    86   val gen_tptp_cls : int * string * kind * string list * string list -> string
    87   val tptp_tfree_clause : string -> string
    88   val tptp_arity_clause : arity_clause -> string
    89   val tptp_classrel_clause : classrel_clause -> string
    90 end
    91 
    92 structure Sledgehammer_FOL_Clause : SLEDGEHAMMER_FOL_CLAUSE =
    93 struct
    94 
    95 open Sledgehammer_Util
    96 
    97 val schematic_var_prefix = "V_";
    98 val fixed_var_prefix = "v_";
    99 
   100 val tvar_prefix = "T_";
   101 val tfree_prefix = "t_";
   102 
   103 val clause_prefix = "cls_";
   104 val arclause_prefix = "clsarity_"
   105 val clrelclause_prefix = "clsrel_";
   106 
   107 val const_prefix = "c_";
   108 val tconst_prefix = "tc_";
   109 val class_prefix = "class_";
   110 
   111 fun union_all xss = fold (union (op =)) xss []
   112 
   113 (* Readable names for the more common symbolic functions. Do not mess with the
   114    last nine entries of the table unless you know what you are doing. *)
   115 val const_trans_table =
   116   Symtab.make [(@{const_name "op ="}, "equal"),
   117                (@{const_name "op &"}, "and"),
   118                (@{const_name "op |"}, "or"),
   119                (@{const_name "op -->"}, "implies"),
   120                (@{const_name "op :"}, "in"),
   121                (@{const_name fequal}, "fequal"),
   122                (@{const_name COMBI}, "COMBI"),
   123                (@{const_name COMBK}, "COMBK"),
   124                (@{const_name COMBB}, "COMBB"),
   125                (@{const_name COMBC}, "COMBC"),
   126                (@{const_name COMBS}, "COMBS"),
   127                (@{const_name True}, "True"),
   128                (@{const_name False}, "False"),
   129                (@{const_name If}, "If")]
   130 
   131 val type_const_trans_table =
   132   Symtab.make [(@{type_name "*"}, "prod"),
   133                (@{type_name "+"}, "sum")]
   134 
   135 (*Escaping of special characters.
   136   Alphanumeric characters are left unchanged.
   137   The character _ goes to __
   138   Characters in the range ASCII space to / go to _A to _P, respectively.
   139   Other printing characters go to _nnn where nnn is the decimal ASCII code.*)
   140 val A_minus_space = Char.ord #"A" - Char.ord #" ";
   141 
   142 fun stringN_of_int 0 _ = ""
   143   | stringN_of_int k n = stringN_of_int (k-1) (n div 10) ^ Int.toString (n mod 10);
   144 
   145 fun ascii_of_c c =
   146   if Char.isAlphaNum c then String.str c
   147   else if c = #"_" then "__"
   148   else if #" " <= c andalso c <= #"/"
   149        then "_" ^ String.str (Char.chr (Char.ord c + A_minus_space))
   150   else if Char.isPrint c
   151        then ("_" ^ stringN_of_int 3 (Char.ord c))  (*fixed width, in case more digits follow*)
   152   else ""
   153 
   154 val ascii_of = String.translate ascii_of_c;
   155 
   156 (** Remove ASCII armouring from names in proof files **)
   157 
   158 (*We don't raise error exceptions because this code can run inside the watcher.
   159   Also, the errors are "impossible" (hah!)*)
   160 fun undo_ascii_aux rcs [] = String.implode(rev rcs)
   161   | undo_ascii_aux rcs [#"_"] = undo_ascii_aux (#"_"::rcs) []  (*ERROR*)
   162       (*Three types of _ escapes: __, _A to _P, _nnn*)
   163   | undo_ascii_aux rcs (#"_" :: #"_" :: cs) = undo_ascii_aux (#"_"::rcs) cs
   164   | undo_ascii_aux rcs (#"_" :: c :: cs) =
   165       if #"A" <= c andalso c<= #"P"  (*translation of #" " to #"/"*)
   166       then undo_ascii_aux (Char.chr(Char.ord c - A_minus_space) :: rcs) cs
   167       else
   168         let val digits = List.take (c::cs, 3) handle Subscript => []
   169         in
   170             case Int.fromString (String.implode digits) of
   171                 NONE => undo_ascii_aux (c:: #"_"::rcs) cs  (*ERROR*)
   172               | SOME n => undo_ascii_aux (Char.chr n :: rcs) (List.drop (cs, 2))
   173         end
   174   | undo_ascii_aux rcs (c::cs) = undo_ascii_aux (c::rcs) cs;
   175 
   176 val undo_ascii_of = undo_ascii_aux [] o String.explode;
   177 
   178 (* convert a list of strings into one single string; surrounded by brackets *)
   179 fun paren_pack [] = ""   (*empty argument list*)
   180   | paren_pack strings = "(" ^ commas strings ^ ")";
   181 
   182 fun tptp_clause strings = "(" ^ space_implode " | " strings ^ ")"
   183 
   184 (*Remove the initial ' character from a type variable, if it is present*)
   185 fun trim_type_var s =
   186   if s <> "" andalso String.sub(s,0) = #"'" then String.extract(s,1,NONE)
   187   else error ("trim_type: Malformed type variable encountered: " ^ s);
   188 
   189 fun ascii_of_indexname (v,0) = ascii_of v
   190   | ascii_of_indexname (v,i) = ascii_of v ^ "_" ^ Int.toString i;
   191 
   192 fun make_schematic_var v = schematic_var_prefix ^ (ascii_of_indexname v);
   193 fun make_fixed_var x = fixed_var_prefix ^ (ascii_of x);
   194 
   195 fun make_schematic_type_var (x,i) =
   196       tvar_prefix ^ (ascii_of_indexname (trim_type_var x,i));
   197 fun make_fixed_type_var x = tfree_prefix ^ (ascii_of (trim_type_var x));
   198 
   199 val max_dfg_symbol_length = 63
   200 
   201 (* HACK because SPASS 3.0 truncates identifiers to 63 characters. *)
   202 fun controlled_length dfg s =
   203   if dfg andalso size s > max_dfg_symbol_length then
   204     String.extract (s, 0, SOME (max_dfg_symbol_length div 2 - 1)) ^ "__" ^
   205     String.extract (s, size s - max_dfg_symbol_length div 2 + 1, NONE)
   206   else
   207     s
   208 
   209 fun lookup_const dfg c =
   210     case Symtab.lookup const_trans_table c of
   211         SOME c' => c'
   212       | NONE => controlled_length dfg (ascii_of c);
   213 
   214 fun lookup_type_const dfg c =
   215     case Symtab.lookup type_const_trans_table c of
   216         SOME c' => c'
   217       | NONE => controlled_length dfg (ascii_of c);
   218 
   219 (* "op =" MUST BE "equal" because it's built into ATPs. *)
   220 fun make_fixed_const _ (@{const_name "op ="}) = "equal"
   221   | make_fixed_const dfg c = const_prefix ^ lookup_const dfg c;
   222 
   223 fun make_fixed_type_const dfg c = tconst_prefix ^ lookup_type_const dfg c;
   224 
   225 fun make_type_class clas = class_prefix ^ ascii_of clas;
   226 
   227 
   228 (**** name pool ****)
   229  
   230 type name = string * string
   231 type name_pool = string Symtab.table * string Symtab.table
   232 
   233 fun empty_name_pool readable_names =
   234   if readable_names then SOME (`I Symtab.empty) else NONE
   235 
   236 fun pool_fold f xs z = pair z #> fold_rev (fn x => uncurry (f x)) xs
   237 fun pool_map f xs =
   238   pool_fold (fn x => fn ys => fn pool => f x pool |>> (fn y => y :: ys)) xs []
   239 
   240 fun add_nice_name full_name nice_prefix j the_pool =
   241   let
   242     val nice_name = nice_prefix ^ (if j = 0 then "" else "_" ^ Int.toString j)
   243   in
   244     case Symtab.lookup (snd the_pool) nice_name of
   245       SOME full_name' =>
   246       if full_name = full_name' then (nice_name, the_pool)
   247       else add_nice_name full_name nice_prefix (j + 1) the_pool
   248     | NONE =>
   249       (nice_name, (Symtab.update_new (full_name, nice_name) (fst the_pool),
   250                    Symtab.update_new (nice_name, full_name) (snd the_pool)))
   251   end
   252 
   253 fun translate_first_char f s =
   254   String.str (f (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
   255 
   256 fun readable_name full_name s =
   257   let
   258     val s = s |> Long_Name.base_name
   259               |> fold remove_all ["\<^sub>", "\<^bsub>", "\<^esub>", "\<^isub>"]
   260     val s' = s |> explode |> rev |> dropwhile (curry (op =) "'")
   261     val s' =
   262       (s' |> rev
   263           |> implode
   264           |> String.translate
   265                  (fn c => if Char.isAlphaNum c orelse c = #"_" then String.str c
   266                           else ""))
   267       ^ replicate_string (String.size s - length s') "_"
   268     val s' =
   269       if s' = "" orelse not (Char.isAlpha (String.sub (s', 0))) then "X" ^ s'
   270       else s'
   271     (* Avoid "equal", since it's built into ATPs; and "op" is very ambiguous
   272        ("op &", "op |", etc.). *)
   273     val s' = if s' = "equal" orelse s' = "op" then full_name else s'
   274   in
   275     case (Char.isLower (String.sub (full_name, 0)),
   276           Char.isLower (String.sub (s', 0))) of
   277       (true, false) => translate_first_char Char.toLower s'
   278     | (false, true) => translate_first_char Char.toUpper s'
   279     | _ => s'
   280   end
   281 
   282 fun nice_name (full_name, _) NONE = (full_name, NONE)
   283   | nice_name (full_name, desired_name) (SOME the_pool) =
   284     case Symtab.lookup (fst the_pool) full_name of
   285       SOME nice_name => (nice_name, SOME the_pool)
   286     | NONE => add_nice_name full_name (readable_name full_name desired_name) 0
   287                             the_pool
   288               |> apsnd SOME
   289 
   290 (**** Definitions and functions for FOL clauses, for conversion to TPTP or DFG
   291       format ****)
   292 
   293 datatype kind = Axiom | Conjecture;
   294 
   295 type axiom_name = string;
   296 
   297 (**** Isabelle FOL clauses ****)
   298 
   299 datatype fol_type =
   300   TyVar of name |
   301   TyFree of name |
   302   TyConstr of name * fol_type list
   303 
   304 fun string_of_fol_type (TyVar sp) pool = nice_name sp pool
   305   | string_of_fol_type (TyFree sp) pool = nice_name sp pool
   306   | string_of_fol_type (TyConstr (sp, tys)) pool =
   307     let
   308       val (s, pool) = nice_name sp pool
   309       val (ss, pool) = pool_map string_of_fol_type tys pool
   310     in (s ^ paren_pack ss, pool) end
   311 
   312 (* The first component is the type class; the second is a TVar or TFree. *)
   313 datatype type_literal =
   314   TyLitVar of string * name |
   315   TyLitFree of string * name
   316 
   317 exception CLAUSE of string * term;
   318 
   319 (*Make literals for sorted type variables*)
   320 fun sorts_on_typs_aux (_, [])   = []
   321   | sorts_on_typs_aux ((x,i),  s::ss) =
   322       let val sorts = sorts_on_typs_aux ((x,i), ss)
   323       in
   324           if s = "HOL.type" then sorts
   325           else if i = ~1 then TyLitFree (make_type_class s, `make_fixed_type_var x) :: sorts
   326           else TyLitVar (make_type_class s, (make_schematic_type_var (x,i), x)) :: sorts
   327       end;
   328 
   329 fun sorts_on_typs (TFree (a,s)) = sorts_on_typs_aux ((a,~1),s)
   330   | sorts_on_typs (TVar (v,s))  = sorts_on_typs_aux (v,s);
   331 
   332 fun pred_of_sort (TyLitVar (s, _)) = (s, 1)
   333   | pred_of_sort (TyLitFree (s, _)) = (s, 1)
   334 
   335 (*Given a list of sorted type variables, return a list of type literals.*)
   336 fun type_literals_for_types Ts =
   337   fold (union (op =)) (map sorts_on_typs Ts) []
   338 
   339 (*The correct treatment of TFrees like 'a in lemmas (axiom clauses) is not clear.
   340   *  Ignoring them leads to unsound proofs, since we do nothing to ensure that 'a
   341     in a lemma has the same sort as 'a in the conjecture.
   342   * Deleting such clauses will lead to problems with locales in other use of local results
   343     where 'a is fixed. Probably we should delete clauses unless the sorts agree.
   344   * Currently we include a class constraint in the clause, exactly as with TVars.
   345 *)
   346 
   347 (** make axiom and conjecture clauses. **)
   348 
   349 fun get_tvar_strs [] = []
   350   | get_tvar_strs ((TVar (indx,s))::Ts) =
   351       insert (op =) (make_schematic_type_var indx) (get_tvar_strs Ts)
   352   | get_tvar_strs((TFree _)::Ts) = get_tvar_strs Ts
   353 
   354 
   355 
   356 (**** Isabelle arities ****)
   357 
   358 datatype arLit = TConsLit of class * string * string list
   359                | TVarLit of class * string;
   360 
   361 datatype arity_clause =
   362          ArityClause of {axiom_name: axiom_name,
   363                          conclLit: arLit,
   364                          premLits: arLit list};
   365 
   366 
   367 fun gen_TVars 0 = []
   368   | gen_TVars n = ("T_" ^ Int.toString n) :: gen_TVars (n-1);
   369 
   370 fun pack_sort(_,[])  = []
   371   | pack_sort(tvar, "HOL.type"::srt) = pack_sort(tvar, srt)   (*IGNORE sort "type"*)
   372   | pack_sort(tvar, cls::srt) =  (cls, tvar) :: pack_sort(tvar, srt);
   373 
   374 (*Arity of type constructor tcon :: (arg1,...,argN)res*)
   375 fun make_axiom_arity_clause dfg (tcons, axiom_name, (cls,args)) =
   376    let val tvars = gen_TVars (length args)
   377        val tvars_srts = ListPair.zip (tvars,args)
   378    in
   379       ArityClause {axiom_name = axiom_name, 
   380                    conclLit = TConsLit (cls, make_fixed_type_const dfg tcons, tvars),
   381                    premLits = map TVarLit (union_all(map pack_sort tvars_srts))}
   382    end;
   383 
   384 
   385 (**** Isabelle class relations ****)
   386 
   387 datatype classrel_clause =
   388          ClassrelClause of {axiom_name: axiom_name,
   389                             subclass: class,
   390                             superclass: class};
   391 
   392 (*Generate all pairs (sub,super) such that sub is a proper subclass of super in theory thy.*)
   393 fun class_pairs thy [] supers = []
   394   | class_pairs thy subs supers =
   395       let
   396         val class_less = Sorts.class_less (Sign.classes_of thy)
   397         fun add_super sub super = class_less (sub, super) ? cons (sub, super)
   398         fun add_supers sub = fold (add_super sub) supers
   399       in fold add_supers subs [] end
   400 
   401 fun make_classrel_clause (sub,super) =
   402   ClassrelClause {axiom_name = clrelclause_prefix ^ ascii_of sub ^ "_" ^ ascii_of super,
   403                   subclass = make_type_class sub,
   404                   superclass = make_type_class super};
   405 
   406 fun make_classrel_clauses thy subs supers =
   407   map make_classrel_clause (class_pairs thy subs supers);
   408 
   409 
   410 (** Isabelle arities **)
   411 
   412 fun arity_clause dfg _ _ (tcons, []) = []
   413   | arity_clause dfg seen n (tcons, ("HOL.type",_)::ars) =  (*ignore*)
   414       arity_clause dfg seen n (tcons,ars)
   415   | arity_clause dfg seen n (tcons, (ar as (class,_)) :: ars) =
   416       if member (op =) seen class then (*multiple arities for the same tycon, class pair*)
   417           make_axiom_arity_clause dfg (tcons, lookup_type_const dfg tcons ^ "_" ^ class ^ "_" ^ Int.toString n, ar) ::
   418           arity_clause dfg seen (n+1) (tcons,ars)
   419       else
   420           make_axiom_arity_clause dfg (tcons, lookup_type_const dfg tcons ^ "_" ^ class, ar) ::
   421           arity_clause dfg (class::seen) n (tcons,ars)
   422 
   423 fun multi_arity_clause dfg [] = []
   424   | multi_arity_clause dfg ((tcons, ars) :: tc_arlists) =
   425       arity_clause dfg [] 1 (tcons, ars) @ multi_arity_clause dfg tc_arlists
   426 
   427 (*Generate all pairs (tycon,class,sorts) such that tycon belongs to class in theory thy
   428   provided its arguments have the corresponding sorts.*)
   429 fun type_class_pairs thy tycons classes =
   430   let val alg = Sign.classes_of thy
   431       fun domain_sorts tycon = Sorts.mg_domain alg tycon o single
   432       fun add_class tycon class =
   433         cons (class, domain_sorts tycon class)
   434         handle Sorts.CLASS_ERROR _ => I
   435       fun try_classes tycon = (tycon, fold (add_class tycon) classes [])
   436   in  map try_classes tycons  end;
   437 
   438 (*Proving one (tycon, class) membership may require proving others, so iterate.*)
   439 fun iter_type_class_pairs thy tycons [] = ([], [])
   440   | iter_type_class_pairs thy tycons classes =
   441       let val cpairs = type_class_pairs thy tycons classes
   442           val newclasses = union_all (union_all (union_all (map (map #2 o #2) cpairs)))
   443             |> subtract (op =) classes |> subtract (op =) HOLogic.typeS
   444           val (classes', cpairs') = iter_type_class_pairs thy tycons newclasses
   445       in (union (op =) classes' classes, union (op =) cpairs' cpairs) end;
   446 
   447 fun make_arity_clauses_dfg dfg thy tycons classes =
   448   let val (classes', cpairs) = iter_type_class_pairs thy tycons classes
   449   in  (classes', multi_arity_clause dfg cpairs)  end;
   450 val make_arity_clauses = make_arity_clauses_dfg false;
   451 
   452 (**** Find occurrences of predicates in clauses ****)
   453 
   454 (*FIXME: multiple-arity checking doesn't work, as update_new is the wrong
   455   function (it flags repeated declarations of a function, even with the same arity)*)
   456 
   457 fun update_many keypairs = fold Symtab.update keypairs
   458 
   459 val add_type_sort_preds = update_many o map pred_of_sort o sorts_on_typs
   460 
   461 fun add_classrel_clause_preds (ClassrelClause {subclass, superclass, ...}) =
   462   Symtab.update (subclass, 1) #> Symtab.update (superclass, 1)
   463 
   464 fun class_of_arityLit (TConsLit (tclass, _, _)) = tclass
   465   | class_of_arityLit (TVarLit (tclass, _)) = tclass;
   466 
   467 fun add_arity_clause_preds (ArityClause {conclLit, premLits, ...}) =
   468   let
   469     val classes = map (make_type_class o class_of_arityLit)
   470                       (conclLit :: premLits)
   471   in fold (Symtab.update o rpair 1) classes end;
   472 
   473 (*** Find occurrences of functions in clauses ***)
   474 
   475 fun add_fol_type_funcs (TyVar _) = I
   476   | add_fol_type_funcs (TyFree (s, _)) = Symtab.update (s, 0)
   477   | add_fol_type_funcs (TyConstr ((s, _), tys)) =
   478     Symtab.update (s, length tys) #> fold add_fol_type_funcs tys
   479 
   480 (*TFrees are recorded as constants*)
   481 fun add_type_sort_funcs (TVar _, funcs) = funcs
   482   | add_type_sort_funcs (TFree (a, _), funcs) =
   483       Symtab.update (make_fixed_type_var a, 0) funcs
   484 
   485 fun add_arity_clause_funcs (ArityClause {conclLit,...}) funcs =
   486   let val TConsLit (_, tcons, tvars) = conclLit
   487   in  Symtab.update (tcons, length tvars) funcs  end;
   488 
   489 (*This type can be overlooked because it is built-in...*)
   490 val init_functab = Symtab.update ("tc_itself", 1) Symtab.empty;
   491 
   492 
   493 (**** String-oriented operations ****)
   494 
   495 fun string_of_clausename (cls_id,ax_name) =
   496     clause_prefix ^ ascii_of ax_name ^ "_" ^ Int.toString cls_id;
   497 
   498 fun string_of_type_clsname (cls_id,ax_name,idx) =
   499     string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
   500 
   501 
   502 (**** Producing DFG files ****)
   503 
   504 (*Attach sign in DFG syntax: false means negate.*)
   505 fun dfg_sign true s = s
   506   | dfg_sign false s = "not(" ^ s ^ ")"
   507 
   508 fun dfg_of_type_literal pos (TyLitVar (s, (s', _))) =
   509     dfg_sign pos (s ^ "(" ^ s' ^ ")")
   510   | dfg_of_type_literal pos (TyLitFree (s, (s', _))) =
   511     dfg_sign pos (s ^ "(" ^ s' ^ ")");
   512 
   513 (*Enclose the clause body by quantifiers, if necessary*)
   514 fun dfg_forall [] body = body
   515   | dfg_forall vars body = "forall([" ^ commas vars ^ "],\n" ^ body ^ ")"
   516 
   517 fun gen_dfg_cls (cls_id, ax_name, Axiom, lits, tylits, vars) =
   518       "clause( %(axiom)\n" ^
   519       dfg_forall vars ("or(" ^ commas (tylits@lits) ^ ")") ^ ",\n" ^
   520       string_of_clausename (cls_id,ax_name) ^  ").\n\n"
   521   | gen_dfg_cls (cls_id, ax_name, Conjecture, lits, _, vars) =
   522       "clause( %(negated_conjecture)\n" ^
   523       dfg_forall vars ("or(" ^ commas lits ^ ")") ^ ",\n" ^
   524       string_of_clausename (cls_id,ax_name) ^  ").\n\n";
   525 
   526 fun string_of_arity (name, arity) =  "(" ^ name ^ ", " ^ Int.toString arity ^ ")"
   527 
   528 fun string_of_preds [] = ""
   529   | string_of_preds preds = "predicates[" ^ commas(map string_of_arity preds) ^ "].\n";
   530 
   531 fun string_of_funcs [] = ""
   532   | string_of_funcs funcs = "functions[" ^ commas(map string_of_arity funcs) ^ "].\n" ;
   533 
   534 fun string_of_symbols predstr funcstr =
   535   "list_of_symbols.\n" ^ predstr  ^ funcstr  ^ "end_of_list.\n\n";
   536 
   537 fun string_of_start name = "begin_problem(" ^ name ^ ").\n\n";
   538 
   539 fun string_of_descrip name =
   540   "list_of_descriptions.\nname({*" ^ name ^
   541   "*}).\nauthor({*Isabelle*}).\nstatus(unknown).\ndescription({*auto-generated*}).\nend_of_list.\n\n"
   542 
   543 fun dfg_tfree_clause tfree_lit =
   544   "clause( %(negated_conjecture)\n" ^ "or( " ^ tfree_lit ^ "),\n" ^ "tfree_tcs" ^ ").\n\n"
   545 
   546 fun dfg_of_arLit (TConsLit (c,t,args)) =
   547       dfg_sign true (make_type_class c ^ "(" ^ t ^ paren_pack args ^ ")")
   548   | dfg_of_arLit (TVarLit (c,str)) =
   549       dfg_sign false (make_type_class c ^ "(" ^ str ^ ")")
   550 
   551 fun dfg_classrelLits sub sup =  "not(" ^ sub ^ "(T)), " ^ sup ^ "(T)";
   552 
   553 fun dfg_classrel_clause (ClassrelClause {axiom_name,subclass,superclass,...}) =
   554   "clause(forall([T],\nor( " ^ dfg_classrelLits subclass superclass ^ ")),\n" ^
   555   axiom_name ^ ").\n\n";
   556 
   557 fun string_of_ar axiom_name = arclause_prefix ^ ascii_of axiom_name;
   558 
   559 fun dfg_arity_clause (ArityClause{axiom_name,conclLit,premLits,...}) =
   560   let val TConsLit (_,_,tvars) = conclLit
   561       val lits = map dfg_of_arLit (conclLit :: premLits)
   562   in
   563       "clause( %(axiom)\n" ^
   564       dfg_forall tvars ("or( " ^ commas lits ^ ")") ^ ",\n" ^
   565       string_of_ar axiom_name ^ ").\n\n"
   566   end;
   567 
   568 
   569 (**** Produce TPTP files ****)
   570 
   571 fun tptp_sign true s = s
   572   | tptp_sign false s = "~ " ^ s
   573 
   574 fun tptp_of_type_literal pos (TyLitVar (s, name)) =
   575     nice_name name #>> (fn s' => tptp_sign pos (s ^ "(" ^ s' ^ ")"))
   576   | tptp_of_type_literal pos (TyLitFree (s, name)) =
   577     nice_name name #>> (fn s' => tptp_sign pos (s ^ "(" ^ s' ^ ")"))
   578 
   579 fun tptp_cnf name kind formula =
   580   "cnf(" ^ name ^ ", " ^ kind ^ ",\n    " ^ formula ^ ").\n"
   581 
   582 fun gen_tptp_cls (cls_id, ax_name, Axiom, lits, tylits) =
   583       tptp_cnf (string_of_clausename (cls_id, ax_name)) "axiom"
   584                (tptp_clause (tylits @ lits))
   585   | gen_tptp_cls (cls_id, ax_name, Conjecture, lits, _) =
   586       tptp_cnf (string_of_clausename (cls_id, ax_name)) "negated_conjecture"
   587                (tptp_clause lits)
   588 
   589 fun tptp_tfree_clause tfree_lit =
   590     tptp_cnf "tfree_tcs" "negated_conjecture" (tptp_clause [tfree_lit])
   591 
   592 fun tptp_of_arLit (TConsLit (c,t,args)) =
   593       tptp_sign true (make_type_class c ^ "(" ^ t ^ paren_pack args ^ ")")
   594   | tptp_of_arLit (TVarLit (c,str)) =
   595       tptp_sign false (make_type_class c ^ "(" ^ str ^ ")")
   596 
   597 fun tptp_arity_clause (ArityClause{axiom_name,conclLit,premLits,...}) =
   598   tptp_cnf (string_of_ar axiom_name) "axiom"
   599            (tptp_clause (map tptp_of_arLit (conclLit :: premLits)))
   600 
   601 fun tptp_classrelLits sub sup =
   602   let val tvar = "(T)"
   603   in  tptp_clause [tptp_sign false (sub^tvar), tptp_sign true (sup^tvar)]  end;
   604 
   605 fun tptp_classrel_clause (ClassrelClause {axiom_name,subclass,superclass,...}) =
   606   tptp_cnf axiom_name "axiom" (tptp_classrelLits subclass superclass)
   607 
   608 end;