src/HOL/Tools/Sledgehammer/sledgehammer_fol_clause.ML
author blanchet
Wed Apr 28 15:34:55 2010 +0200 (2010-04-28 ago)
changeset 36491 6769f8eacaac
parent 36476 a04cf4704668
child 36493 a3357a631b96
permissions -rw-r--r--
unskolemize formulas in proof reconstruction + detect newer SPASS versions to avoid truncating identifiers if not necessary (truncating confuses proof reconstruction)
     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 = LTVar of string * string | LTFree of string * string
    48   exception CLAUSE of string * term
    49   val add_typs : typ list -> type_literal list
    50   val get_tvar_strs: typ list -> string list
    51   datatype arLit =
    52       TConsLit of class * string * string list
    53     | TVarLit of class * string
    54   datatype arity_clause = ArityClause of
    55    {axiom_name: axiom_name, conclLit: arLit, premLits: arLit list}
    56   datatype classrel_clause = ClassrelClause of
    57    {axiom_name: axiom_name, subclass: class, superclass: class}
    58   val make_classrel_clauses: theory -> class list -> class list -> classrel_clause list
    59   val make_arity_clauses_dfg: bool -> theory -> string list -> class list -> class list * arity_clause list
    60   val make_arity_clauses: theory -> string list -> class list -> class list * arity_clause list
    61   val add_type_sort_preds: typ -> int Symtab.table -> int Symtab.table
    62   val add_classrel_clause_preds :
    63     classrel_clause -> int Symtab.table -> int Symtab.table
    64   val class_of_arityLit: arLit -> class
    65   val add_arity_clause_preds: arity_clause -> int Symtab.table -> int Symtab.table
    66   val add_fol_type_funcs: fol_type -> int Symtab.table -> int Symtab.table
    67   val add_arity_clause_funcs:
    68     arity_clause -> int Symtab.table -> int Symtab.table
    69   val init_functab: int Symtab.table
    70   val dfg_sign: bool -> string -> string
    71   val dfg_of_typeLit: bool -> type_literal -> string
    72   val gen_dfg_cls: int * string * kind * string list * string list * string list -> string
    73   val string_of_preds: (string * Int.int) list -> string
    74   val string_of_funcs: (string * int) list -> string
    75   val string_of_symbols: string -> string -> string
    76   val string_of_start: string -> string
    77   val string_of_descrip : string -> string
    78   val dfg_tfree_clause : string -> string
    79   val dfg_classrel_clause: classrel_clause -> string
    80   val dfg_arity_clause: arity_clause -> string
    81   val tptp_sign: bool -> string -> string
    82   val tptp_of_typeLit : bool -> type_literal -> string
    83   val gen_tptp_cls : int * string * kind * string list * string list -> string
    84   val tptp_tfree_clause : string -> string
    85   val tptp_arity_clause : arity_clause -> string
    86   val tptp_classrel_clause : classrel_clause -> string
    87 end
    88 
    89 structure Sledgehammer_FOL_Clause : SLEDGEHAMMER_FOL_CLAUSE =
    90 struct
    91 
    92 open Sledgehammer_Util
    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 = fold (union (op =)) xss []
   109 
   110 (* Provide readable names for the more common symbolic functions *)
   111 val const_trans_table =
   112   Symtab.make [(@{const_name "op ="}, "equal"),
   113                (@{const_name "op &"}, "and"),
   114                (@{const_name "op |"}, "or"),
   115                (@{const_name "op -->"}, "implies"),
   116                (@{const_name "op :"}, "in")]
   117 
   118 val type_const_trans_table =
   119   Symtab.make [(@{type_name "*"}, "prod"),
   120                (@{type_name "+"}, "sum")]
   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 val max_dfg_symbol_length =
   189   if is_new_spass_version then 1000000 (* arbitrary large number *) else 63
   190 
   191 (* HACK because SPASS 3.0 truncates identifiers to 63 characters. *)
   192 fun controlled_length dfg s =
   193   if dfg andalso size s > max_dfg_symbol_length then
   194     String.extract (s, 0, SOME (max_dfg_symbol_length div 2 - 1)) ^ "__" ^
   195     String.extract (s, size s - max_dfg_symbol_length div 2 + 1, NONE)
   196   else
   197     s
   198 
   199 fun lookup_const dfg c =
   200     case Symtab.lookup const_trans_table c of
   201         SOME c' => c'
   202       | NONE => controlled_length dfg (ascii_of c);
   203 
   204 fun lookup_type_const dfg c =
   205     case Symtab.lookup type_const_trans_table c of
   206         SOME c' => c'
   207       | NONE => controlled_length dfg (ascii_of c);
   208 
   209 (* "op =" MUST BE "equal" because it's built into ATPs. *)
   210 fun make_fixed_const _ (@{const_name "op ="}) = "equal"
   211   | make_fixed_const dfg c = const_prefix ^ lookup_const dfg c;
   212 
   213 fun make_fixed_type_const dfg c = tconst_prefix ^ lookup_type_const dfg c;
   214 
   215 fun make_type_class clas = class_prefix ^ ascii_of clas;
   216 
   217 
   218 (**** name pool ****)
   219  
   220 type name = string * string
   221 type name_pool = string Symtab.table * string Symtab.table
   222 
   223 fun empty_name_pool readable_names =
   224   if readable_names then SOME (`I Symtab.empty) else NONE
   225 
   226 fun pool_map f xs =
   227   fold_rev (fn x => fn (ys, pool) => f x pool |>> (fn y => y :: ys)) xs
   228   o pair []
   229 
   230 fun add_nice_name full_name nice_prefix j the_pool =
   231   let
   232     val nice_name = nice_prefix ^ (if j = 0 then "" else "_" ^ Int.toString j)
   233   in
   234     case Symtab.lookup (snd the_pool) nice_name of
   235       SOME full_name' =>
   236       if full_name = full_name' then (nice_name, the_pool)
   237       else add_nice_name full_name nice_prefix (j + 1) the_pool
   238     | NONE =>
   239       (nice_name, (Symtab.update_new (full_name, nice_name) (fst the_pool),
   240                    Symtab.update_new (nice_name, full_name) (snd the_pool)))
   241   end
   242 
   243 fun translate_first_char f s =
   244   String.str (f (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
   245 
   246 fun readable_name full_name s =
   247   let
   248     val s = s |> Long_Name.base_name
   249               |> fold remove_all ["\<^sub>", "\<^bsub>", "\<^esub>", "\<^isub>"]
   250     val s' = s |> explode |> rev |> dropwhile (curry (op =) "'")
   251     val s' =
   252       (s' |> rev
   253           |> implode
   254           |> String.translate
   255                  (fn c => if Char.isAlphaNum c orelse c = #"_" then String.str c
   256                           else ""))
   257       ^ replicate_string (String.size s - length s') "_"
   258     val s' =
   259       if s' = "" orelse not (Char.isAlpha (String.sub (s', 0))) then "X" ^ s'
   260       else s'
   261     (* Avoid "equal", since it's built into ATPs; and "op" is very ambiguous
   262        ("op &", "op |", etc.). *)
   263     val s' = if s' = "equal" orelse s' = "op" then full_name else s'
   264   in
   265     case (Char.isLower (String.sub (full_name, 0)),
   266           Char.isLower (String.sub (s', 0))) of
   267       (true, false) => translate_first_char Char.toLower s'
   268     | (false, true) => translate_first_char Char.toUpper s'
   269     | _ => s'
   270   end
   271 
   272 fun nice_name (full_name, _) NONE = (full_name, NONE)
   273   | nice_name (full_name, desired_name) (SOME the_pool) =
   274     case Symtab.lookup (fst the_pool) full_name of
   275       SOME nice_name => (nice_name, SOME the_pool)
   276     | NONE => add_nice_name full_name (readable_name full_name desired_name) 0
   277                             the_pool
   278               |> apsnd SOME
   279 
   280 (**** Definitions and functions for FOL clauses, for conversion to TPTP or DFG
   281       format ****)
   282 
   283 datatype kind = Axiom | Conjecture;
   284 
   285 type axiom_name = string;
   286 
   287 (**** Isabelle FOL clauses ****)
   288 
   289 datatype fol_type =
   290   TyVar of name |
   291   TyFree of name |
   292   TyConstr of name * fol_type list
   293 
   294 fun string_of_fol_type (TyVar sp) pool = nice_name sp pool
   295   | string_of_fol_type (TyFree sp) pool = nice_name sp pool
   296   | string_of_fol_type (TyConstr (sp, tys)) pool =
   297     let
   298       val (s, pool) = nice_name sp pool
   299       val (ss, pool) = pool_map string_of_fol_type tys pool
   300     in (s ^ paren_pack ss, pool) end
   301 
   302 (*First string is the type class; the second is a TVar or TFfree*)
   303 datatype type_literal = LTVar of string * string | LTFree of string * string;
   304 
   305 exception CLAUSE of string * term;
   306 
   307 (*Make literals for sorted type variables*)
   308 fun sorts_on_typs_aux (_, [])   = []
   309   | sorts_on_typs_aux ((x,i),  s::ss) =
   310       let val sorts = sorts_on_typs_aux ((x,i), ss)
   311       in
   312           if s = "HOL.type" then sorts
   313           else if i = ~1 then LTFree(make_type_class s, make_fixed_type_var x) :: sorts
   314           else LTVar(make_type_class s, make_schematic_type_var (x,i)) :: sorts
   315       end;
   316 
   317 fun sorts_on_typs (TFree (a,s)) = sorts_on_typs_aux ((a,~1),s)
   318   | sorts_on_typs (TVar (v,s))  = sorts_on_typs_aux (v,s);
   319 
   320 fun pred_of_sort (LTVar (s,ty)) = (s,1)
   321   | pred_of_sort (LTFree (s,ty)) = (s,1)
   322 
   323 (*Given a list of sorted type variables, return a list of type literals.*)
   324 fun add_typs Ts = fold (union (op =)) (map sorts_on_typs Ts) []
   325 
   326 (*The correct treatment of TFrees like 'a in lemmas (axiom clauses) is not clear.
   327   * Ignoring them leads to unsound proofs, since we do nothing to ensure that 'a
   328     in a lemma has the same sort as 'a in the conjecture.
   329   * Deleting such clauses will lead to problems with locales in other use of local results
   330     where 'a is fixed. Probably we should delete clauses unless the sorts agree.
   331   * Currently we include a class constraint in the clause, exactly as with TVars.
   332 *)
   333 
   334 (** make axiom and conjecture clauses. **)
   335 
   336 fun get_tvar_strs [] = []
   337   | get_tvar_strs ((TVar (indx,s))::Ts) =
   338       insert (op =) (make_schematic_type_var indx) (get_tvar_strs Ts)
   339   | get_tvar_strs((TFree _)::Ts) = get_tvar_strs Ts
   340 
   341 
   342 
   343 (**** Isabelle arities ****)
   344 
   345 datatype arLit = TConsLit of class * string * string list
   346                | TVarLit of class * string;
   347 
   348 datatype arity_clause =
   349          ArityClause of {axiom_name: axiom_name,
   350                          conclLit: arLit,
   351                          premLits: arLit list};
   352 
   353 
   354 fun gen_TVars 0 = []
   355   | gen_TVars n = ("T_" ^ Int.toString n) :: gen_TVars (n-1);
   356 
   357 fun pack_sort(_,[])  = []
   358   | pack_sort(tvar, "HOL.type"::srt) = pack_sort(tvar, srt)   (*IGNORE sort "type"*)
   359   | pack_sort(tvar, cls::srt) =  (cls, tvar) :: pack_sort(tvar, srt);
   360 
   361 (*Arity of type constructor tcon :: (arg1,...,argN)res*)
   362 fun make_axiom_arity_clause dfg (tcons, axiom_name, (cls,args)) =
   363    let val tvars = gen_TVars (length args)
   364        val tvars_srts = ListPair.zip (tvars,args)
   365    in
   366       ArityClause {axiom_name = axiom_name, 
   367                    conclLit = TConsLit (cls, make_fixed_type_const dfg tcons, tvars),
   368                    premLits = map TVarLit (union_all(map pack_sort tvars_srts))}
   369    end;
   370 
   371 
   372 (**** Isabelle class relations ****)
   373 
   374 datatype classrel_clause =
   375          ClassrelClause of {axiom_name: axiom_name,
   376                             subclass: class,
   377                             superclass: class};
   378 
   379 (*Generate all pairs (sub,super) such that sub is a proper subclass of super in theory thy.*)
   380 fun class_pairs thy [] supers = []
   381   | class_pairs thy subs supers =
   382       let
   383         val class_less = Sorts.class_less (Sign.classes_of thy)
   384         fun add_super sub super = class_less (sub, super) ? cons (sub, super)
   385         fun add_supers sub = fold (add_super sub) supers
   386       in fold add_supers subs [] end
   387 
   388 fun make_classrel_clause (sub,super) =
   389   ClassrelClause {axiom_name = clrelclause_prefix ^ ascii_of sub ^ "_" ^ ascii_of super,
   390                   subclass = make_type_class sub,
   391                   superclass = make_type_class super};
   392 
   393 fun make_classrel_clauses thy subs supers =
   394   map make_classrel_clause (class_pairs thy subs supers);
   395 
   396 
   397 (** Isabelle arities **)
   398 
   399 fun arity_clause dfg _ _ (tcons, []) = []
   400   | arity_clause dfg seen n (tcons, ("HOL.type",_)::ars) =  (*ignore*)
   401       arity_clause dfg seen n (tcons,ars)
   402   | arity_clause dfg seen n (tcons, (ar as (class,_)) :: ars) =
   403       if class mem_string seen then (*multiple arities for the same tycon, class pair*)
   404           make_axiom_arity_clause dfg (tcons, lookup_type_const dfg tcons ^ "_" ^ class ^ "_" ^ Int.toString n, ar) ::
   405           arity_clause dfg seen (n+1) (tcons,ars)
   406       else
   407           make_axiom_arity_clause dfg (tcons, lookup_type_const dfg tcons ^ "_" ^ class, ar) ::
   408           arity_clause dfg (class::seen) n (tcons,ars)
   409 
   410 fun multi_arity_clause dfg [] = []
   411   | multi_arity_clause dfg ((tcons, ars) :: tc_arlists) =
   412       arity_clause dfg [] 1 (tcons, ars) @ multi_arity_clause dfg tc_arlists
   413 
   414 (*Generate all pairs (tycon,class,sorts) such that tycon belongs to class in theory thy
   415   provided its arguments have the corresponding sorts.*)
   416 fun type_class_pairs thy tycons classes =
   417   let val alg = Sign.classes_of thy
   418       fun domain_sorts tycon = Sorts.mg_domain alg tycon o single
   419       fun add_class tycon class =
   420         cons (class, domain_sorts tycon class)
   421         handle Sorts.CLASS_ERROR _ => I
   422       fun try_classes tycon = (tycon, fold (add_class tycon) classes [])
   423   in  map try_classes tycons  end;
   424 
   425 (*Proving one (tycon, class) membership may require proving others, so iterate.*)
   426 fun iter_type_class_pairs thy tycons [] = ([], [])
   427   | iter_type_class_pairs thy tycons classes =
   428       let val cpairs = type_class_pairs thy tycons classes
   429           val newclasses = union_all (union_all (union_all (map (map #2 o #2) cpairs)))
   430             |> subtract (op =) classes |> subtract (op =) HOLogic.typeS
   431           val (classes', cpairs') = iter_type_class_pairs thy tycons newclasses
   432       in (union (op =) classes' classes, union (op =) cpairs' cpairs) end;
   433 
   434 fun make_arity_clauses_dfg dfg thy tycons classes =
   435   let val (classes', cpairs) = iter_type_class_pairs thy tycons classes
   436   in  (classes', multi_arity_clause dfg cpairs)  end;
   437 val make_arity_clauses = make_arity_clauses_dfg false;
   438 
   439 (**** Find occurrences of predicates in clauses ****)
   440 
   441 (*FIXME: multiple-arity checking doesn't work, as update_new is the wrong
   442   function (it flags repeated declarations of a function, even with the same arity)*)
   443 
   444 fun update_many keypairs = fold Symtab.update keypairs
   445 
   446 val add_type_sort_preds = update_many o map pred_of_sort o sorts_on_typs
   447 
   448 fun add_classrel_clause_preds (ClassrelClause {subclass, superclass, ...}) =
   449   Symtab.update (subclass, 1) #> Symtab.update (superclass, 1)
   450 
   451 fun class_of_arityLit (TConsLit (tclass, _, _)) = tclass
   452   | class_of_arityLit (TVarLit (tclass, _)) = tclass;
   453 
   454 fun add_arity_clause_preds (ArityClause {conclLit, premLits, ...}) =
   455   let
   456     val classes = map (make_type_class o class_of_arityLit)
   457                       (conclLit :: premLits)
   458   in fold (Symtab.update o rpair 1) classes end;
   459 
   460 (*** Find occurrences of functions in clauses ***)
   461 
   462 fun add_fol_type_funcs (TyVar _) = I
   463   | add_fol_type_funcs (TyFree (s, _)) = Symtab.update (s, 0)
   464   | add_fol_type_funcs (TyConstr ((s, _), tys)) =
   465     Symtab.update (s, length tys) #> fold add_fol_type_funcs tys
   466 
   467 (*TFrees are recorded as constants*)
   468 fun add_type_sort_funcs (TVar _, funcs) = funcs
   469   | add_type_sort_funcs (TFree (a, _), funcs) =
   470       Symtab.update (make_fixed_type_var a, 0) funcs
   471 
   472 fun add_arity_clause_funcs (ArityClause {conclLit,...}) funcs =
   473   let val TConsLit (_, tcons, tvars) = conclLit
   474   in  Symtab.update (tcons, length tvars) funcs  end;
   475 
   476 (*This type can be overlooked because it is built-in...*)
   477 val init_functab = Symtab.update ("tc_itself", 1) Symtab.empty;
   478 
   479 
   480 (**** String-oriented operations ****)
   481 
   482 fun string_of_clausename (cls_id,ax_name) =
   483     clause_prefix ^ ascii_of ax_name ^ "_" ^ Int.toString cls_id;
   484 
   485 fun string_of_type_clsname (cls_id,ax_name,idx) =
   486     string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
   487 
   488 
   489 (**** Producing DFG files ****)
   490 
   491 (*Attach sign in DFG syntax: false means negate.*)
   492 fun dfg_sign true s = s
   493   | dfg_sign false s = "not(" ^ s ^ ")"
   494 
   495 fun dfg_of_typeLit pos (LTVar (s,ty))  = dfg_sign pos (s ^ "(" ^ ty ^ ")")
   496   | dfg_of_typeLit pos (LTFree (s,ty)) = dfg_sign pos (s ^ "(" ^ ty ^ ")");
   497 
   498 (*Enclose the clause body by quantifiers, if necessary*)
   499 fun dfg_forall [] body = body
   500   | dfg_forall vars body = "forall([" ^ commas vars ^ "],\n" ^ body ^ ")"
   501 
   502 fun gen_dfg_cls (cls_id, ax_name, Axiom, lits, tylits, vars) =
   503       "clause( %(axiom)\n" ^
   504       dfg_forall vars ("or(" ^ commas (tylits@lits) ^ ")") ^ ",\n" ^
   505       string_of_clausename (cls_id,ax_name) ^  ").\n\n"
   506   | gen_dfg_cls (cls_id, ax_name, Conjecture, lits, _, vars) =
   507       "clause( %(negated_conjecture)\n" ^
   508       dfg_forall vars ("or(" ^ commas lits ^ ")") ^ ",\n" ^
   509       string_of_clausename (cls_id,ax_name) ^  ").\n\n";
   510 
   511 fun string_of_arity (name, num) =  "(" ^ name ^ "," ^ Int.toString num ^ ")"
   512 
   513 fun string_of_preds [] = ""
   514   | string_of_preds preds = "predicates[" ^ commas(map string_of_arity preds) ^ "].\n";
   515 
   516 fun string_of_funcs [] = ""
   517   | string_of_funcs funcs = "functions[" ^ commas(map string_of_arity funcs) ^ "].\n" ;
   518 
   519 fun string_of_symbols predstr funcstr =
   520   "list_of_symbols.\n" ^ predstr  ^ funcstr  ^ "end_of_list.\n\n";
   521 
   522 fun string_of_start name = "begin_problem(" ^ name ^ ").\n\n";
   523 
   524 fun string_of_descrip name =
   525   "list_of_descriptions.\nname({*" ^ name ^
   526   "*}).\nauthor({*Isabelle*}).\nstatus(unknown).\ndescription({*auto-generated*}).\nend_of_list.\n\n"
   527 
   528 fun dfg_tfree_clause tfree_lit =
   529   "clause( %(negated_conjecture)\n" ^ "or( " ^ tfree_lit ^ "),\n" ^ "tfree_tcs" ^ ").\n\n"
   530 
   531 fun dfg_of_arLit (TConsLit (c,t,args)) =
   532       dfg_sign true (make_type_class c ^ "(" ^ t ^ paren_pack args ^ ")")
   533   | dfg_of_arLit (TVarLit (c,str)) =
   534       dfg_sign false (make_type_class c ^ "(" ^ str ^ ")")
   535 
   536 fun dfg_classrelLits sub sup =  "not(" ^ sub ^ "(T)), " ^ sup ^ "(T)";
   537 
   538 fun dfg_classrel_clause (ClassrelClause {axiom_name,subclass,superclass,...}) =
   539   "clause(forall([T],\nor( " ^ dfg_classrelLits subclass superclass ^ ")),\n" ^
   540   axiom_name ^ ").\n\n";
   541 
   542 fun string_of_ar axiom_name = arclause_prefix ^ ascii_of axiom_name;
   543 
   544 fun dfg_arity_clause (ArityClause{axiom_name,conclLit,premLits,...}) =
   545   let val TConsLit (_,_,tvars) = conclLit
   546       val lits = map dfg_of_arLit (conclLit :: premLits)
   547   in
   548       "clause( %(axiom)\n" ^
   549       dfg_forall tvars ("or( " ^ commas lits ^ ")") ^ ",\n" ^
   550       string_of_ar axiom_name ^ ").\n\n"
   551   end;
   552 
   553 
   554 (**** Produce TPTP files ****)
   555 
   556 fun tptp_sign true s = s
   557   | tptp_sign false s = "~ " ^ s
   558 
   559 fun tptp_of_typeLit pos (LTVar (s, ty))  = tptp_sign pos (s ^ "(" ^ ty ^ ")")
   560   | tptp_of_typeLit pos (LTFree (s, ty)) = tptp_sign pos (s ^ "(" ^ ty ^ ")")
   561 
   562 fun tptp_cnf name kind formula =
   563   "cnf(" ^ name ^ ", " ^ kind ^ ",\n    " ^ formula ^ ").\n"
   564 
   565 fun gen_tptp_cls (cls_id, ax_name, Axiom, lits, tylits) =
   566       tptp_cnf (string_of_clausename (cls_id, ax_name)) "axiom"
   567                (tptp_pack (tylits @ lits))
   568   | gen_tptp_cls (cls_id, ax_name, Conjecture, lits, _) =
   569       tptp_cnf (string_of_clausename (cls_id, ax_name)) "negated_conjecture"
   570                (tptp_pack lits)
   571 
   572 fun tptp_tfree_clause tfree_lit =
   573     tptp_cnf "tfree_tcs" "negated_conjecture" (tptp_pack [tfree_lit])
   574 
   575 fun tptp_of_arLit (TConsLit (c,t,args)) =
   576       tptp_sign true (make_type_class c ^ "(" ^ t ^ paren_pack args ^ ")")
   577   | tptp_of_arLit (TVarLit (c,str)) =
   578       tptp_sign false (make_type_class c ^ "(" ^ str ^ ")")
   579 
   580 fun tptp_arity_clause (ArityClause{axiom_name,conclLit,premLits,...}) =
   581   tptp_cnf (string_of_ar axiom_name) "axiom"
   582            (tptp_pack (map tptp_of_arLit (conclLit :: premLits)))
   583 
   584 fun tptp_classrelLits sub sup =
   585   let val tvar = "(T)"
   586   in  tptp_pack [tptp_sign false (sub^tvar), tptp_sign true (sup^tvar)]  end;
   587 
   588 fun tptp_classrel_clause (ClassrelClause {axiom_name,subclass,superclass,...}) =
   589   tptp_cnf axiom_name "axiom" (tptp_classrelLits subclass superclass)
   590 
   591 end;