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