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