src/HOL/Tools/res_clause.ML
author paulson
Wed Dec 21 12:06:08 2005 +0100 (2005-12-21)
changeset 18449 e314fb38307d
parent 18439 4b517881ac7e
child 18676 5bce9fddce2e
permissions -rw-r--r--
new hash table module in HOL/Too/s
     1 (*  Author: Jia Meng, Cambridge University Computer Laboratory
     2     ID: $Id$
     3     Copyright 2004 University of Cambridge
     4 
     5 ML data structure for storing/printing FOL clauses and arity clauses.
     6 Typed equality is treated differently.
     7 *)
     8 
     9 (* works for writeoutclasimp on typed *)
    10 signature RES_CLAUSE =
    11   sig
    12   val keep_types : bool ref
    13   val special_equal : bool ref
    14   val tagged : bool ref
    15 
    16   exception ARCLAUSE of string
    17   exception CLAUSE of string * term
    18   type arityClause 
    19   type classrelClause
    20   type clause
    21   val init : theory -> unit
    22   val make_axiom_clause : Term.term -> string * int -> clause
    23   val make_conjecture_clauses : term list -> clause list
    24   val get_axiomName : clause ->  string
    25   val isTaut : clause -> bool
    26   val num_of_clauses : clause -> int
    27 
    28   val clause2dfg : clause -> string * string list
    29   val clauses2dfg : clause list -> string -> clause list -> clause list ->
    30 	   (string * int) list -> (string * int) list -> string
    31   val tfree_dfg_clause : string -> string
    32 
    33   val arity_clause_thy: theory -> arityClause list 
    34   val classrel_clauses_thy: theory -> classrelClause list 
    35 
    36   val tptp_arity_clause : arityClause -> string
    37   val tptp_classrelClause : classrelClause -> string
    38   val tptp_clause : clause -> string list
    39   val clause2tptp : clause -> string * string list
    40   val tfree_clause : string -> string
    41   val schematic_var_prefix : string
    42   val fixed_var_prefix : string
    43   val tvar_prefix : string
    44   val tfree_prefix : string
    45   val clause_prefix : string 
    46   val arclause_prefix : string
    47   val const_prefix : string
    48   val tconst_prefix : string 
    49   val class_prefix : string 
    50 
    51   val union_all : ''a list list -> ''a list
    52   val ascii_of : String.string -> String.string
    53   val paren_pack : string list -> string
    54   val bracket_pack : string list -> string
    55   val make_schematic_var : String.string * int -> string
    56   val make_fixed_var : String.string -> string
    57   val make_schematic_type_var : string * int -> string
    58   val make_fixed_type_var : string -> string
    59   val make_fixed_const : String.string -> string		
    60   val make_fixed_type_const : String.string -> string   
    61   val make_type_class : String.string -> string
    62   val isMeta : String.string -> bool
    63   
    64   type typ_var
    65   val mk_typ_var_sort : Term.typ -> typ_var * sort
    66   type type_literal
    67   val add_typs_aux2 : (typ_var * string list) list -> type_literal list * type_literal list
    68   val gen_tptp_cls : int * string * string * string -> string
    69   val gen_tptp_type_cls : int * string * string * string * int -> string
    70   val tptp_of_typeLit : type_literal -> string
    71 
    72   (*for hashing*)
    73   val clause_eq : clause * clause -> bool
    74   val hash_clause : clause -> int
    75 
    76   val list_ord : ('a * 'b -> order) -> 'a list * 'b list -> order
    77   type fol_type
    78   val types_ord : fol_type list * fol_type list -> order
    79   val string_of_fol_type : fol_type -> string
    80   val mk_fol_type: string * string * fol_type list -> fol_type
    81   val types_eq :fol_type list * fol_type list ->
    82    (string * string) list * (string * string) list -> bool * ((string * string) list * (string * string) list)
    83   val check_var_pairs: ''a * ''b -> (''a * ''b) list -> int
    84   end;
    85 
    86 structure ResClause : RES_CLAUSE =
    87 struct
    88 
    89 (* Added for typed equality *)
    90 val special_equal = ref false; (* by default,equality does not carry type information *)
    91 val eq_typ_wrapper = "typeinfo"; (* default string *)
    92 
    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 
   107 val class_prefix = "class_"; 
   108 
   109 
   110 fun union_all xss = foldl (op union) [] xss;
   111 
   112  
   113 (*Provide readable names for the more common symbolic functions*)
   114 val const_trans_table =
   115       Symtab.make [("op =", "equal"),
   116 	  	   ("op <=", "lessequals"),
   117 		   ("op <", "less"),
   118 		   ("op &", "and"),
   119 		   ("op |", "or"),
   120 		   ("op +", "plus"),
   121 		   ("op -", "minus"),
   122 		   ("op *", "times"),
   123 		   ("op -->", "implies"),
   124 		   ("{}", "emptyset"),
   125 		   ("op :", "in"),
   126 		   ("op Un", "union"),
   127 		   ("op Int", "inter"),
   128 		   ("List.op @", "append")];
   129 
   130 val type_const_trans_table =
   131       Symtab.make [("*", "prod"),
   132 	  	   ("+", "sum"),
   133 		   ("~=>", "map")];
   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 local
   141 
   142 val A_minus_space = Char.ord #"A" - Char.ord #" ";
   143 
   144 fun ascii_of_c c =
   145   if Char.isAlphaNum c then String.str c
   146   else if c = #"_" then "__"
   147   else if #" " <= c andalso c <= #"/" 
   148        then "_" ^ String.str (Char.chr (Char.ord c + A_minus_space))
   149   else if Char.isPrint c then ("_" ^ Int.toString (Char.ord c))
   150   else ""
   151 
   152 in
   153 
   154 val ascii_of = String.translate ascii_of_c;
   155 
   156 end;
   157 
   158 (* convert a list of strings into one single string; surrounded by brackets *)
   159 fun paren_pack [] = ""   (*empty argument list*)
   160   | paren_pack strings = "(" ^ commas strings ^ ")";
   161 
   162 fun bracket_pack strings = "[" ^ commas strings ^ "]";
   163 
   164 
   165 (*Remove the initial ' character from a type variable, if it is present*)
   166 fun trim_type_var s =
   167   if s <> "" andalso String.sub(s,0) = #"'" then String.extract(s,1,NONE)
   168   else error ("trim_type: Malformed type variable encountered: " ^ s);
   169 
   170 fun ascii_of_indexname (v,0) = ascii_of v
   171   | ascii_of_indexname (v,i) = ascii_of v ^ "_" ^ Int.toString i;
   172 
   173 fun make_schematic_var v = schematic_var_prefix ^ (ascii_of_indexname v);
   174 fun make_fixed_var x = fixed_var_prefix ^ (ascii_of x);
   175 
   176 fun make_schematic_type_var (x,i) = 
   177       tvar_prefix ^ (ascii_of_indexname (trim_type_var x,i));
   178 fun make_fixed_type_var x = tfree_prefix ^ (ascii_of (trim_type_var x));
   179 
   180 fun lookup_const c =
   181     case Symtab.lookup const_trans_table c of
   182         SOME c' => c'
   183       | NONE => ascii_of c;
   184 
   185 fun lookup_type_const c = 
   186     case Symtab.lookup type_const_trans_table c of
   187         SOME c' => c'
   188       | NONE => ascii_of c;
   189 
   190 fun make_fixed_const "op =" = "equal"   (*MUST BE "equal" because it's built-in to ATPs*)
   191   | make_fixed_const c      = const_prefix ^ lookup_const c;
   192 
   193 fun make_fixed_type_const c = tconst_prefix ^ lookup_type_const c;
   194 
   195 fun make_type_class clas = class_prefix ^ ascii_of clas;
   196 
   197 
   198 
   199 (***** definitions and functions for FOL clauses, prepared for conversion into TPTP format or SPASS format. *****)
   200 
   201 val keep_types = ref true;
   202 
   203 datatype kind = Axiom | Hypothesis | Conjecture;
   204 fun name_of_kind Axiom = "axiom"
   205   | name_of_kind Hypothesis = "hypothesis"
   206   | name_of_kind Conjecture = "conjecture";
   207 
   208 type clause_id = int;
   209 type axiom_name = string;
   210 
   211 
   212 type polarity = bool;
   213 
   214 type indexname = Term.indexname;
   215 
   216 
   217 (* "tag" is used for vampire specific syntax  *)
   218 type tag = bool; 
   219 
   220 
   221 (**** Isabelle FOL clauses ****)
   222 
   223 val tagged = ref false;
   224 
   225 type pred_name = string;
   226 type sort = Term.sort;
   227 
   228 
   229 
   230 datatype typ_var = FOLTVar of indexname | FOLTFree of string;
   231 
   232 datatype fol_type = AtomV of string
   233 		  | AtomF of string
   234 		  | Comp of string * fol_type list;
   235 
   236 fun string_of_fol_type (AtomV x) = x
   237   | string_of_fol_type (AtomF x) = x
   238   | string_of_fol_type (Comp(tcon,tps)) = 
   239     let val tstrs = map string_of_fol_type tps
   240     in
   241 	tcon ^ (paren_pack tstrs)
   242     end;
   243 
   244 fun mk_fol_type ("Var",x,_) = AtomV(x)
   245   | mk_fol_type ("Fixed",x,_) = AtomF(x)
   246   | mk_fol_type ("Comp",con,args) = Comp(con,args)
   247 
   248 
   249 
   250 datatype type_literal = LTVar of string | LTFree of string;
   251 
   252 datatype folTerm = UVar of string * fol_type
   253                  | Fun of string * fol_type list * folTerm list;
   254 datatype predicate = Predicate of pred_name * fol_type list * folTerm list;
   255 
   256 datatype literal = Literal of polarity * predicate * tag;
   257 
   258 fun mk_typ_var_sort (TFree(a,s)) = (FOLTFree a,s)
   259   | mk_typ_var_sort (TVar(v,s)) = (FOLTVar v,s);
   260 
   261 
   262 
   263 (* ML datatype used to repsent one single clause: disjunction of literals. *)
   264 datatype clause = 
   265 	 Clause of {clause_id: clause_id,
   266 		    axiom_name: axiom_name,
   267 		    kind: kind,
   268 		    literals: literal list,
   269 		    types_sorts: (typ_var * sort) list, 
   270                     tvar_type_literals: type_literal list, 
   271                     tfree_type_literals: type_literal list ,
   272                     tvars: string list,
   273                     predicates: (string*int) list,
   274                     functions: (string*int) list};
   275 
   276 
   277 exception CLAUSE of string * term;
   278 
   279 fun get_literals (c as Clause(cls)) = #literals cls;
   280 
   281 fun components_of_literal (Literal (pol,pred,tag)) = ((pol,pred),tag);
   282 
   283 fun predicate_name (Predicate(predname,_,_)) = predname;
   284 
   285 
   286 (*** make clauses ***)
   287 
   288 fun isFalse (Literal (pol,Predicate(a,_,[]),_)) =
   289       (pol andalso a = "c_False") orelse
   290       (not pol andalso a = "c_True")
   291   | isFalse _ = false;
   292 
   293 fun isTrue (Literal (pol,Predicate(a,_,[]),_)) =
   294       (pol andalso a = "c_True") orelse
   295       (not pol andalso a = "c_False")
   296   | isTrue _ = false;
   297   
   298 fun isTaut (Clause {literals,...}) = exists isTrue literals;  
   299 
   300 fun make_clause (clause_id,axiom_name,kind,literals,
   301                  types_sorts,tvar_type_literals,
   302                  tfree_type_literals,tvars, predicates, functions) =
   303   if forall isFalse literals 
   304   then error "Problem too trivial for resolution (empty clause)"
   305   else
   306      Clause {clause_id = clause_id, axiom_name = axiom_name, kind = kind, 
   307              literals = literals, types_sorts = types_sorts,
   308              tvar_type_literals = tvar_type_literals,
   309              tfree_type_literals = tfree_type_literals,
   310              tvars = tvars, predicates = predicates, 
   311              functions = functions};
   312 
   313 
   314 (** Some Clause destructor functions **)
   315 
   316 fun string_of_kind (Clause cls) = name_of_kind (#kind cls);
   317 
   318 fun get_axiomName (Clause cls) = #axiom_name cls;
   319 
   320 fun get_clause_id (Clause cls) = #clause_id cls;
   321 
   322 fun funcs_of_cls (Clause cls) = #functions cls;
   323 
   324 fun preds_of_cls (Clause cls) = #predicates cls;
   325 
   326 
   327 (*Declarations of the current theory--to allow suppressing types.*)
   328 val const_typargs = ref (Library.K [] : (string*typ -> typ list));
   329 
   330 fun num_typargs(s,T) = if !keep_types then length (!const_typargs (s,T)) else 0;
   331 
   332 (*Initialize the type suppression mechanism with the current theory before
   333     producing any clauses!*)
   334 fun init thy = (const_typargs := Sign.const_typargs thy);
   335     
   336 
   337 (*Flatten a type to a fol_type while accumulating sort constraints on the TFrees and
   338   TVars it contains.*)    
   339 fun type_of (Type (a, Ts)) = 
   340       let val (folTyps, (ts, funcs)) = types_of Ts 
   341 	  val t = make_fixed_type_const a
   342       in    
   343 	  (Comp(t,folTyps), (ts, (t, length Ts)::funcs))
   344       end
   345   | type_of (TFree (a,s)) = 
   346       let val t = make_fixed_type_var a
   347       in (AtomF(t), ([((FOLTFree a),s)], [(t,0)])) end
   348   | type_of (TVar (v, s)) = (AtomV(make_schematic_type_var v), ([((FOLTVar v),s)], []))
   349 and types_of Ts =
   350       let val foltyps_ts = map type_of Ts 
   351 	  val (folTyps,ts_funcs) = ListPair.unzip foltyps_ts
   352 	  val (ts, funcslist) = ListPair.unzip ts_funcs
   353       in    
   354 	  (folTyps, (union_all ts, union_all funcslist))
   355       end;
   356 
   357 
   358 
   359 fun const_types_of (c,T) = types_of (!const_typargs (c,T));
   360 
   361 (* Any variables created via the METAHYPS tactical should be treated as
   362    universal vars, although it is represented as "Free(...)" by Isabelle *)
   363 val isMeta = String.isPrefix "METAHYP1_"
   364 
   365 fun pred_name_type (Const(c,T)) = 
   366       let val (contys,(folTyps,funcs)) = const_types_of (c,T)
   367       in (make_fixed_const c, (contys,folTyps), funcs) end
   368   | pred_name_type (Free(x,T))  = 
   369       if isMeta x then raise CLAUSE("Predicate Not First Order 1", Free(x,T)) 
   370       else (make_fixed_var x, ([],[]), [])
   371   | pred_name_type (v as Var _) = raise CLAUSE("Predicate Not First Order 2", v)
   372   | pred_name_type t        = raise CLAUSE("Predicate input unexpected", t);
   373 
   374 
   375 (* For typed equality *)
   376 (* here "arg_typ" is the type of "="'s argument's type, not the type of the equality *)
   377 (* Find type of equality arg *)
   378 fun eq_arg_type (Type("fun",[T,_])) = 
   379     let val (folT,_) = type_of T;
   380     in  folT  end;
   381 
   382 fun fun_name_type (Const("op =",T)) args =   (*FIXME: Is this special treatment of = needed??*)
   383       let val t = make_fixed_const "op ="
   384       in (t, ([eq_arg_type T], []), [(t,2)]) end
   385   | fun_name_type (Const(c,T)) args = 
   386       let val t = make_fixed_const c
   387 	  val (contys, (folTyps,funcs)) = const_types_of (c,T)
   388 	  val arity = num_typargs(c,T) + length args
   389       in
   390 	  (t, (contys,folTyps), ((t,arity)::funcs))
   391       end
   392  | fun_name_type (Free(x,T)) args  = 
   393       let val t = make_fixed_var x
   394       in  (t, ([],[]), [(t, length args)]) end
   395   | fun_name_type f args = raise CLAUSE("Function Not First Order 1", f);
   396 
   397 
   398 fun term_of (Var(ind_nm,T)) = 
   399       let val (folType,(ts,funcs)) = type_of T
   400       in
   401 	  (UVar(make_schematic_var ind_nm, folType), (ts, funcs))
   402       end
   403   | term_of (Free(x,T)) = 
   404       let val (folType, (ts,funcs)) = type_of T
   405       in
   406 	  if isMeta x then (UVar(make_schematic_var(x,0),folType),
   407 			    (ts, ((make_schematic_var(x,0)),0)::funcs))
   408 	  else
   409 	      (Fun(make_fixed_var x, [folType], []), 
   410 	       (ts, ((make_fixed_var x),0)::funcs))
   411       end
   412   | term_of (Const(c,T)) =  (* impossible to be equality *)
   413       let val (contys, (folTyps,funcs)) = const_types_of (c,T)
   414       in
   415 	  (Fun(make_fixed_const c, contys, []),
   416 	   (folTyps, ((make_fixed_const c),0)::funcs))
   417       end    
   418   | term_of app = 
   419       let val (f,args) = strip_comb app
   420           val _ = case f of Const(_,_) => ()
   421 			  | Free(s,_)  => 
   422 			      if isMeta s 
   423 			      then raise CLAUSE("Function Not First Order 2", f)
   424 			      else ()
   425 			  | _ => raise CLAUSE("Function Not First Order 3", f);
   426 	  val (funName,(contys,ts1),funcs) = fun_name_type f args
   427 	  val (args',(ts2,funcs')) = terms_of args
   428       in
   429 	  (Fun(funName,contys,args'), 
   430 	   (union_all (ts1::ts2), 
   431 	    union_all(funcs::funcs')))
   432       end
   433 and terms_of ts =  
   434       let val (args, ts_funcs) = ListPair.unzip (map term_of ts)
   435       in
   436 	  (args, ListPair.unzip ts_funcs)
   437       end
   438 
   439 
   440 fun pred_of (Const("op =", typ), args) =
   441       let val arg_typ = eq_arg_type typ 
   442 	  val (args',(ts,funcs)) = terms_of args
   443 	  val equal_name = make_fixed_const "op ="
   444       in
   445 	  (Predicate(equal_name,[arg_typ],args'),
   446 	   union_all ts, 
   447 	   [((make_fixed_var equal_name), 2)], 
   448 	   union_all funcs)
   449       end
   450   | pred_of (pred,args) = 
   451       let val (predName,(predType,ts1), pfuncs) = pred_name_type pred
   452 	  val (args',(ts2,ffuncs)) = terms_of args
   453 	  val ts3 = union_all (ts1::ts2)
   454 	  val ffuncs' = union_all ffuncs
   455 	  val newfuncs = pfuncs union ffuncs'
   456 	  val arity = 
   457 	    case pred of
   458 		Const (c,T) => num_typargs(c,T) + length args
   459 	      | _ => length args
   460       in
   461 	  (Predicate(predName,predType,args'), ts3, 
   462 	   [(predName, arity)], newfuncs)
   463       end;
   464 
   465 
   466 (*Treatment of literals, possibly negated or tagged*)
   467 fun predicate_of ((Const("Not",_) $ P), polarity, tag) =
   468       predicate_of (P, not polarity, tag)
   469   | predicate_of ((Const("HOL.tag",_) $ P), polarity, tag) =
   470       predicate_of (P, polarity, true)
   471   | predicate_of (term,polarity,tag) =
   472         (pred_of (strip_comb term), polarity, tag);
   473 
   474 fun literals_of_term1 args (Const("Trueprop",_) $ P) = literals_of_term1 args P
   475   | literals_of_term1 (args as (lits, ts, preds, funcs)) (Const("op |",_) $ P $ Q) = 
   476       let val (lits', ts', preds', funcs') = literals_of_term1 args P
   477       in
   478 	  literals_of_term1 (lits', ts', preds' union preds, funcs' union funcs) Q
   479       end
   480   | literals_of_term1 (lits, ts, preds, funcs) P =
   481       let val ((pred, ts', preds', funcs'), pol, tag) = predicate_of (P,true,false)
   482 	  val lits' = Literal(pol,pred,tag) :: lits
   483       in
   484 	  (lits', ts union ts', preds' union preds, funcs' union funcs)
   485       end;
   486 
   487 
   488 val literals_of_term = literals_of_term1 ([],[],[],[]);
   489 
   490 
   491 
   492 fun list_ord _ ([],[]) = EQUAL
   493   | list_ord _ ([],_) = LESS
   494   | list_ord _ (_,[]) = GREATER
   495   | list_ord ord (x::xs, y::ys) =
   496     let val xy_ord = ord(x,y)
   497     in
   498 	case xy_ord of EQUAL => list_ord ord (xs,ys)
   499 		     | _ => xy_ord
   500     end;
   501 
   502 fun type_ord (AtomV(_),AtomV(_)) = EQUAL
   503   | type_ord (AtomV(_),_) = LESS
   504   | type_ord (AtomF(_),AtomV(_)) = GREATER
   505   | type_ord (AtomF(f1),AtomF(f2)) = string_ord (f1,f2)
   506   | type_ord (AtomF(_),_) = LESS
   507   | type_ord (Comp(_,_),AtomV(_)) = GREATER
   508   | type_ord (Comp(_,_),AtomF(_)) = GREATER
   509   | type_ord (Comp(con1,args1),Comp(con2,args2)) = 
   510     let val con_ord = string_ord(con1,con2)
   511     in
   512 	case con_ord of EQUAL => types_ord (args1,args2)
   513 		      | _ => con_ord
   514     end
   515 and
   516 
   517 types_ord ([],[]) = EQUAL
   518   | types_ord (tps1,tps2) = list_ord type_ord (tps1,tps2);
   519 
   520 
   521 fun term_ord (UVar(_,_),UVar(_,_)) = EQUAL
   522   | term_ord (UVar(_,_),_) = LESS
   523   | term_ord (Fun(_,_,_),UVar(_)) = GREATER
   524   | term_ord (Fun(f1,tps1,tms1),Fun(f2,tps2,tms2)) = 
   525      (case string_ord (f1,f2) of
   526          EQUAL => 
   527 	   (case terms_ord (tms1,tms2) of EQUAL => types_ord (tps1,tps2)
   528 	      | tms_ord => tms_ord)
   529        | fn_ord => fn_ord)
   530 
   531 and
   532 
   533   terms_ord ([],[]) = EQUAL
   534     | terms_ord (tms1,tms2) = list_ord term_ord (tms1,tms2);
   535 
   536 
   537 
   538 fun predicate_ord (Predicate(predname1,ftyps1,ftms1),Predicate(predname2,ftyps2,ftms2)) = 
   539     let val predname_ord = string_ord (predname1,predname2)
   540     in
   541 	case predname_ord of EQUAL => 
   542 			     let val ftms_ord = terms_ord(ftms1,ftms2)
   543 			     in
   544 				 case ftms_ord of EQUAL => types_ord(ftyps1,ftyps2)
   545 						| _ => ftms_ord
   546 			     end
   547 			   | _ => predname_ord
   548     end;
   549 
   550 fun literal_ord (Literal(false,_,_),Literal(true,_,_)) = LESS
   551   | literal_ord (Literal(true,_,_),Literal(false,_,_)) = GREATER
   552   | literal_ord (Literal(_,pred1,_),Literal(_,pred2,_)) = predicate_ord(pred1,pred2);
   553 
   554 fun sort_lits lits = sort literal_ord lits;
   555 
   556 
   557 (********** clause equivalence ******************)
   558 
   559 fun check_var_pairs (x,y) [] = 0 
   560   | check_var_pairs (x,y) ((u,v)::w) =
   561     if (x,y) = (u,v) then 1 
   562     else
   563 	if (x = u) orelse (y = v) then 2 (*conflict*)
   564 	else check_var_pairs (x,y) w;
   565 
   566 fun type_eq (AtomV(v1),AtomV(v2)) (vars,tvars) =
   567     (case check_var_pairs (v1,v2) tvars of 0 => (true,(vars,(v1,v2)::tvars))
   568 					 | 1 => (true,(vars,tvars))
   569 					 | 2 => (false,(vars,tvars)))
   570   | type_eq (AtomV(_),_) vtvars = (false,vtvars)
   571   | type_eq (AtomF(f1),AtomF(f2)) vtvars = (f1=f2,vtvars)
   572   | type_eq (AtomF(_),_) vtvars = (false,vtvars)
   573   | type_eq (Comp(con1,args1),Comp(con2,args2)) vtvars =
   574       let val (eq1,vtvars1) = 
   575 	      if con1 = con2 then types_eq (args1,args2) vtvars
   576 	      else (false,vtvars)
   577       in
   578 	  (eq1,vtvars1)
   579       end
   580   | type_eq (Comp(_,_),_) vtvars = (false,vtvars)
   581 
   582 and
   583 
   584     types_eq ([],[]) vtvars = (true,vtvars)
   585   | types_eq (tp1::tps1,tp2::tps2) vtvars =
   586       let val (eq1,vtvars1) = type_eq (tp1,tp2) vtvars
   587 	  val (eq2,vtvars2) = if eq1 then types_eq (tps1,tps2) vtvars1
   588 			      else (eq1,vtvars1)
   589       in
   590 	  (eq2,vtvars2)
   591       end;
   592 
   593 
   594 fun term_eq (UVar(v1,tp1),UVar(v2,tp2)) (vars,tvars) =
   595     (case check_var_pairs (v1,v2) vars of 0 => type_eq (tp1,tp2) (((v1,v2)::vars),tvars)
   596 					| 1 => type_eq (tp1,tp2) (vars,tvars)
   597 					| 2 => (false,(vars,tvars)))
   598   | term_eq (UVar(_,_),_) vtvars = (false,vtvars)
   599   | term_eq (Fun(f1,tps1,tms1),Fun(f2,tps2,tms2)) vtvars =
   600       let val (eq1,vtvars1) = 
   601 	      if f1 = f2 then terms_eq (tms1,tms2) vtvars
   602 	      else (false,vtvars)
   603 	  val (eq2,vtvars2) =
   604 	      if eq1 then types_eq (tps1,tps2) vtvars1
   605 	      else (eq1,vtvars1)
   606       in
   607 	  (eq2,vtvars2)
   608       end
   609   | term_eq (Fun(_,_,_),_) vtvars = (false,vtvars)
   610 
   611 and
   612 
   613     terms_eq ([],[]) vtvars = (true,vtvars)
   614   | terms_eq (tm1::tms1,tm2::tms2) vtvars =
   615       let val (eq1,vtvars1) = term_eq (tm1,tm2) vtvars
   616 	  val (eq2,vtvars2) = if eq1 then terms_eq (tms1,tms2) vtvars1
   617 				     else (eq1,vtvars1)
   618       in
   619 	  (eq2,vtvars2)
   620       end;
   621 					     
   622 
   623 fun pred_eq (Predicate(predname1,tps1,tms1),Predicate(predname2,tps2,tms2)) vtvars =
   624     let val (eq1,vtvars1) = 
   625 	    if (predname1 = predname2) then terms_eq (tms1,tms2) vtvars
   626 	    else (false,vtvars)
   627 	val (eq2,vtvars2) = 
   628 	    if eq1 then types_eq (tps1,tps2) vtvars1
   629 	    else (eq1,vtvars1)
   630     in
   631 	(eq2,vtvars2)
   632     end;
   633 					      
   634 
   635 fun lit_eq (Literal(pol1,pred1,_),Literal(pol2,pred2,_)) vtvars =
   636     if (pol1 = pol2) then pred_eq (pred1,pred2) vtvars
   637     else (false,vtvars);
   638 
   639 (*must have the same number of literals*)
   640 fun lits_eq ([],[]) vtvars = (true,vtvars)
   641   | lits_eq (l1::ls1,l2::ls2) vtvars = 
   642     let val (eq1,vtvars1) = lit_eq (l1,l2) vtvars
   643     in
   644 	if eq1 then lits_eq (ls1,ls2) vtvars1
   645 	else (false,vtvars1)
   646     end;
   647 
   648 
   649 (*Equality of two clauses up to variable renaming*)
   650 fun clause_eq (cls1,cls2) =
   651     let val lits1 = get_literals cls1
   652 	val lits2 = get_literals cls2
   653     in
   654 	length lits1 = length lits2 andalso #1 (lits_eq (lits1,lits2) ([],[]))
   655     end;
   656 
   657 
   658 (*** Hash function for clauses ***)
   659 
   660 val xor_words = List.foldl Word.xorb 0w0;
   661 
   662 fun hashw_term (UVar(_,_), w) = w
   663   | hashw_term (Fun(f,tps,args), w) = 
   664       List.foldl hashw_term (Polyhash.hashw_string (f,w)) args;
   665   
   666 fun hashw_pred (Predicate(pn,_,args), w) = 
   667     List.foldl hashw_term (Polyhash.hashw_string (pn,w)) args;
   668     
   669 fun hash1_literal (Literal(true,pred,_)) = hashw_pred (pred, 0w0)
   670   | hash1_literal (Literal(false,pred,_)) = Word.notb (hashw_pred (pred, 0w0));
   671   
   672 fun hash_clause clause = Word.toIntX (xor_words (map hash1_literal (get_literals clause)));
   673 
   674 
   675 (* FIX: not sure what to do with these funcs *)
   676 
   677 (*Make literals for sorted type variables*) 
   678 fun sorts_on_typs (_, [])   = ([]) 
   679   | sorts_on_typs (v, "HOL.type" :: s) =
   680       sorts_on_typs (v,s)                (*IGNORE sort "type"*)
   681   | sorts_on_typs ((FOLTVar indx), (s::ss)) =
   682       LTVar((make_type_class s) ^ 
   683         "(" ^ (make_schematic_type_var indx) ^ ")") :: 
   684       (sorts_on_typs ((FOLTVar indx), ss))
   685   | sorts_on_typs ((FOLTFree x), (s::ss)) =
   686       LTFree((make_type_class s) ^ "(" ^ (make_fixed_type_var x) ^ ")") :: 
   687       (sorts_on_typs ((FOLTFree x), ss));
   688 
   689 
   690 (*UGLY: seems to be parsing the "show sorts" output, removing anything that
   691   starts with a left parenthesis.*)
   692 fun remove_type str = hd (String.fields (fn c => c = #"(") str);
   693 
   694 fun pred_of_sort (LTVar x) = ((remove_type x),1)
   695 |   pred_of_sort (LTFree x) = ((remove_type x),1)
   696 
   697 
   698 
   699 (*Given a list of sorted type variables, return two separate lists.
   700   The first is for TVars, the second for TFrees.*)
   701 fun add_typs_aux [] preds  = ([],[], preds)
   702   | add_typs_aux ((FOLTVar indx,s)::tss) preds = 
   703       let val vs = sorts_on_typs (FOLTVar indx, s)
   704           val preds' = (map pred_of_sort vs)@preds
   705 	  val (vss,fss, preds'') = add_typs_aux tss preds'
   706       in
   707 	  (vs union vss, fss, preds'')
   708       end
   709   | add_typs_aux ((FOLTFree x,s)::tss) preds  =
   710       let val fs = sorts_on_typs (FOLTFree x, s)
   711           val preds' = (map pred_of_sort fs)@preds
   712 	  val (vss,fss, preds'') = add_typs_aux tss preds'
   713       in
   714 	  (vss, fs union fss, preds'')
   715       end;
   716 
   717 fun add_typs_aux2 [] = ([],[])
   718   | add_typs_aux2 ((FOLTVar indx,s)::tss) =
   719     let val vs = sorts_on_typs (FOLTVar indx,s)
   720 	val (vss,fss) = add_typs_aux2 tss
   721     in
   722 	(vs union vss,fss)
   723     end
   724   | add_typs_aux2 ((FOLTFree x,s)::tss) =
   725     let val fs = sorts_on_typs (FOLTFree x,s)
   726 	val (vss,fss) = add_typs_aux2 tss
   727     in
   728 	(vss,fs union fss)
   729     end;
   730 
   731 
   732 fun add_typs (Clause cls) preds  = add_typs_aux (#types_sorts cls) preds 
   733 
   734 
   735 (** make axiom clauses, hypothesis clauses and conjecture clauses. **)
   736 
   737 fun get_tvar_strs [] = []
   738   | get_tvar_strs ((FOLTVar indx,s)::tss) = 
   739       let val vstr = make_schematic_type_var indx
   740       in
   741 	  vstr ins (get_tvar_strs tss)
   742       end
   743   | get_tvar_strs((FOLTFree x,s)::tss) = distinct (get_tvar_strs tss)
   744 
   745 (* FIX add preds and funcs to add typs aux here *)
   746 
   747 fun make_axiom_clause_thm thm (ax_name,cls_id) =
   748     let val (lits,types_sorts, preds, funcs) = literals_of_term (prop_of thm)
   749 	val lits' = sort_lits lits
   750 	val (tvar_lits,tfree_lits, preds) = add_typs_aux types_sorts preds 
   751         val tvars = get_tvar_strs types_sorts
   752     in 
   753 	make_clause(cls_id,ax_name,Axiom,
   754 	            lits',types_sorts,tvar_lits,tfree_lits,
   755 	            tvars, preds, funcs)
   756     end;
   757 
   758 
   759 (* check if a clause is FOL first*)
   760 fun make_conjecture_clause n t =
   761     let val _ = check_is_fol_term t
   762 	    handle TERM("check_is_fol_term",_) => raise CLAUSE("Goal is not FOL",t)
   763 	val (lits,types_sorts, preds, funcs) = literals_of_term t
   764 	val (tvar_lits,tfree_lits, preds) = add_typs_aux types_sorts preds 
   765         val tvars = get_tvar_strs types_sorts
   766     in
   767 	make_clause(n,"conjecture",Conjecture,
   768 	            lits,types_sorts,tvar_lits,tfree_lits,
   769 	            tvars, preds, funcs)
   770     end;
   771     
   772 fun make_conjecture_clauses_aux _ [] = []
   773   | make_conjecture_clauses_aux n (t::ts) =
   774       make_conjecture_clause n t :: make_conjecture_clauses_aux (n+1) ts
   775 
   776 val make_conjecture_clauses = make_conjecture_clauses_aux 0
   777 
   778 
   779 (*before converting an axiom clause to "clause" format, check if it is FOL*)
   780 fun make_axiom_clause term (ax_name,cls_id) =
   781     let val _ = check_is_fol_term term 
   782 	    handle TERM("check_is_fol_term",_) => raise CLAUSE("Axiom is not FOL", term) 
   783 	val (lits,types_sorts, preds,funcs) = literals_of_term term
   784 	val lits' = sort_lits lits
   785 	val (tvar_lits,tfree_lits, preds) = add_typs_aux types_sorts preds
   786         val tvars = get_tvar_strs types_sorts	
   787     in 
   788 	make_clause(cls_id,ax_name,Axiom,
   789 	            lits',types_sorts,tvar_lits,tfree_lits,
   790 	            tvars, preds,funcs)
   791     end;
   792 
   793 
   794 
   795  
   796 (**** Isabelle arities ****)
   797 
   798 exception ARCLAUSE of string;
   799  
   800 
   801 type class = string; 
   802 type tcons = string; 
   803 
   804 
   805 datatype arLit = TConsLit of bool * (class * tcons * string list) | TVarLit of bool * (class * string);
   806  
   807 datatype arityClause =  
   808 	 ArityClause of {clause_id: clause_id,
   809 	  	         axiom_name: axiom_name,
   810 			 kind: kind,
   811 			 conclLit: arLit,
   812 			 premLits: arLit list};
   813 
   814 
   815 fun get_TVars 0 = []
   816   | get_TVars n = ("T_" ^ (Int.toString n)) :: get_TVars (n-1);
   817 
   818 fun pack_sort(_,[])  = []
   819   | pack_sort(tvar, "HOL.type"::srt) = pack_sort(tvar, srt)   (*IGNORE sort "type"*)
   820   | pack_sort(tvar, cls::srt) =  (make_type_class cls, tvar) :: pack_sort(tvar, srt);
   821     
   822 fun make_TVarLit (b,(cls,str)) = TVarLit(b,(cls,str));
   823 fun make_TConsLit (b,(cls,tcons,tvars)) = TConsLit(b,(make_type_class cls,make_fixed_type_const tcons,tvars));
   824 
   825 (*Arity of type constructor tcon :: (arg1,...,argN)res*)
   826 fun make_axiom_arity_clause (tcons, n, (res,args)) =
   827    let val nargs = length args
   828        val tvars = get_TVars nargs
   829        val tvars_srts = ListPair.zip (tvars,args)
   830        val tvars_srts' = union_all(map pack_sort tvars_srts)
   831        val false_tvars_srts' = map (pair false) tvars_srts'
   832    in
   833       ArityClause {clause_id = n, kind = Axiom, 
   834                    axiom_name = lookup_type_const tcons,
   835                    conclLit = make_TConsLit(true, (res,tcons,tvars)), 
   836                    premLits = map make_TVarLit false_tvars_srts'}
   837    end;
   838     
   839 (*The number of clauses generated from cls, including type clauses. It's always 1
   840   except for conjecture clauses.*)
   841 fun num_of_clauses (Clause cls) =
   842     let val num_tfree_lits = 
   843 	      if !keep_types then length (#tfree_type_literals cls)
   844 	      else 0
   845     in 	1 + num_tfree_lits  end;
   846 
   847 
   848 (**** Isabelle class relations ****)
   849 
   850 datatype classrelClause = 
   851 	 ClassrelClause of {clause_id: clause_id,
   852 			    subclass: class,
   853 			    superclass: class};
   854 
   855 fun make_axiom_classrelClause n subclass superclass =
   856   ClassrelClause {clause_id = n,
   857                   subclass = subclass, superclass = superclass};
   858 
   859 fun classrelClauses_of_aux n sub [] = []
   860   | classrelClauses_of_aux n sub ("HOL.type"::sups) = (*Should be ignored*)
   861       classrelClauses_of_aux n sub sups
   862   | classrelClauses_of_aux n sub (sup::sups) =
   863       ClassrelClause {clause_id = n, subclass = sub, superclass = sup} 
   864       :: classrelClauses_of_aux (n+1) sub sups;
   865 
   866 fun classrelClauses_of (sub,sups) = classrelClauses_of_aux 0 sub sups;
   867 
   868 
   869 (***** Isabelle arities *****)
   870 
   871 fun arity_clause _ (tcons, []) = []
   872   | arity_clause n (tcons, ("HOL.type",_)::ars) =  (*Should be ignored*)
   873       arity_clause n (tcons,ars)
   874   | arity_clause n (tcons, ar::ars) =
   875       make_axiom_arity_clause (tcons,n,ar) :: 
   876       arity_clause (n+1) (tcons,ars);
   877 
   878 fun multi_arity_clause [] = []
   879   | multi_arity_clause (tcon_ar :: tcons_ars)  =
   880       arity_clause 0 tcon_ar  @  multi_arity_clause tcons_ars 
   881 
   882 fun arity_clause_thy thy =
   883   let val arities = #arities (Type.rep_tsig (Sign.tsig_of thy))
   884   in multi_arity_clause (Symtab.dest arities) end;
   885 
   886 
   887 (* Isabelle classes *)
   888 
   889 type classrelClauses = classrelClause list Symtab.table;
   890 
   891 val classrel_of = #2 o #classes o Type.rep_tsig o Sign.tsig_of;
   892 fun classrel_clauses_classrel (C: Sorts.classes) = map classrelClauses_of (Graph.dest C);
   893 val classrel_clauses_thy = List.concat o classrel_clauses_classrel o classrel_of;
   894 
   895 
   896 
   897 (****!!!! Changed for typed equality !!!!****)
   898 
   899 fun wrap_eq_type typ t = eq_typ_wrapper ^"(" ^ t ^ "," ^ typ ^ ")";
   900 
   901 (*Only need to wrap equality's arguments with "typeinfo" if the output clauses are typed 
   902  and if we specifically ask for types to be included.   *)
   903 fun string_of_equality (typ,terms) =
   904       let val [tstr1,tstr2] = map string_of_term terms
   905 	  val typ' = string_of_fol_type typ
   906       in
   907 	  if !keep_types andalso !special_equal 
   908 	  then "equal(" ^ (wrap_eq_type typ' tstr1) ^ "," ^ 
   909 		 	  (wrap_eq_type typ' tstr2) ^ ")"
   910 	  else "equal(" ^ tstr1 ^ "," ^ tstr2 ^ ")"
   911       end
   912 and string_of_term (UVar(x,_)) = x
   913   | string_of_term (Fun("equal",[typ],terms)) = string_of_equality(typ,terms)
   914   | string_of_term (Fun (name,typs,[])) = name (*Overloaded consts like 0 don't get types!*)
   915   | string_of_term (Fun (name,typs,terms)) = 
   916       let val terms_as_strings = map string_of_term terms
   917 	  val typs' = if !keep_types then map string_of_fol_type typs else []
   918       in  name ^ (paren_pack (terms_as_strings @ typs'))  end;
   919 
   920 (* before output the string of the predicate, check if the predicate corresponds to an equality or not. *)
   921 fun string_of_predicate (Predicate("equal",[typ],terms)) = string_of_equality(typ,terms)
   922   | string_of_predicate (Predicate(name,typs,terms)) = 
   923       let val terms_as_strings = map string_of_term terms
   924 	  val typs' = if !keep_types then map string_of_fol_type typs else []
   925       in  name ^ (paren_pack (terms_as_strings @ typs'))  end;
   926 
   927 fun string_of_clausename (cls_id,ax_name) = 
   928     clause_prefix ^ ascii_of ax_name ^ "_" ^ Int.toString cls_id;
   929 
   930 fun string_of_type_clsname (cls_id,ax_name,idx) = 
   931     string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
   932     
   933 
   934 (********************************)
   935 (* Code for producing DFG files *)
   936 (********************************)
   937 
   938 fun dfg_literal (Literal(pol,pred,tag)) =
   939     let val pred_string = string_of_predicate pred
   940     in
   941 	if pol then pred_string else "not(" ^pred_string ^ ")"  
   942     end;
   943 
   944 
   945 (* FIX: what does this mean? *)
   946 (*fun dfg_of_typeLit (LTVar x) = "not(" ^ x ^ ")"
   947   | dfg_of_typeLit (LTFree x) = "(" ^ x ^ ")";*)
   948 
   949 fun dfg_of_typeLit (LTVar x) =  x 
   950   | dfg_of_typeLit (LTFree x) = x ;
   951  
   952 (*Make the string of universal quantifiers for a clause*)
   953 fun forall_open ([],[]) = ""
   954   | forall_open (vars,tvars) = "forall([" ^ (commas (tvars@vars))^ "],\n"
   955 
   956 fun forall_close ([],[]) = ""
   957   | forall_close (vars,tvars) = ")"
   958 
   959 fun gen_dfg_cls (cls_id,ax_name,knd,lits,tvars,vars) = 
   960     "clause( %(" ^ knd ^ ")\n" ^ forall_open(vars,tvars) ^ 
   961     "or(" ^ lits ^ ")" ^ forall_close(vars,tvars) ^ ",\n" ^ 
   962     string_of_clausename (cls_id,ax_name) ^  ").";
   963 
   964 fun gen_dfg_type_cls (cls_id,ax_name,knd,tfree_lit,idx,tvars,vars) = 
   965     "clause( %(" ^ knd ^ ")\n" ^ forall_open(vars,tvars) ^ 
   966     "or( " ^ tfree_lit ^ ")" ^ forall_close(vars,tvars) ^ ",\n" ^ 
   967     string_of_type_clsname (cls_id,ax_name,idx) ^  ").";
   968 
   969 fun dfg_clause_aux (Clause cls) = 
   970   let val lits = map dfg_literal (#literals cls)
   971       val tvar_lits_strs = 
   972 	  if !keep_types then map dfg_of_typeLit (#tvar_type_literals cls) 
   973 	  else []
   974       val tfree_lits =
   975           if !keep_types then map dfg_of_typeLit (#tfree_type_literals cls)
   976           else []
   977   in
   978       (tvar_lits_strs @ lits, tfree_lits)
   979   end; 
   980 
   981 
   982 fun dfg_folterms (Literal(pol,pred,tag)) = 
   983   let val Predicate (predname, _, folterms) = pred
   984   in  folterms  end
   985 
   986  
   987 fun get_uvars (UVar(a,typ)) = [a] 
   988 |   get_uvars (Fun (_,typ,tlist)) = union_all(map get_uvars tlist)
   989 
   990 
   991 fun is_uvar (UVar _) = true
   992 |   is_uvar (Fun _) = false;
   993 
   994 fun uvar_name (UVar(a,_)) = a
   995 |   uvar_name (Fun (a,_,_)) = raise CLAUSE("Not a variable", Const(a,dummyT));
   996 
   997 fun dfg_vars (Clause cls) =
   998     let val lits = #literals cls
   999         val folterms = List.concat (map dfg_folterms lits)
  1000     in 
  1001         union_all(map get_uvars folterms)
  1002     end
  1003 
  1004 
  1005 fun dfg_tvars (Clause cls) =(#tvars cls)
  1006 
  1007 
  1008 	
  1009 (* make this return funcs and preds too? *)
  1010 fun string_of_predname (Predicate("equal",_,terms)) = "EQUALITY"
  1011   | string_of_predname (Predicate(name,_,terms)) = name
  1012     
  1013 	
  1014 
  1015 fun concat_with sep []  = ""
  1016   | concat_with sep [x] = "(" ^ x ^ ")"
  1017   | concat_with sep (x::xs) = "(" ^ x ^ ")" ^  sep ^ (concat_with sep xs);
  1018 
  1019 fun dfg_pred (Literal(pol,pred,tag)) ax_name = 
  1020     (string_of_predname pred) ^ " " ^ ax_name
  1021 
  1022 fun dfg_clause cls =
  1023     let val (lits,tfree_lits) = dfg_clause_aux cls 
  1024              (*"lits" includes the typing assumptions (TVars)*)
  1025         val vars = dfg_vars cls
  1026         val tvars = dfg_tvars cls
  1027 	val knd = string_of_kind cls
  1028 	val lits_str = commas lits
  1029 	val cls_id = get_clause_id cls
  1030 	val axname = get_axiomName cls
  1031 	val cls_str = gen_dfg_cls(cls_id,axname,knd,lits_str,tvars, vars) 			
  1032         fun typ_clss k [] = []
  1033           | typ_clss k (tfree :: tfrees) = 
  1034               (gen_dfg_type_cls(cls_id,axname,knd,tfree,k, tvars,vars)) :: 
  1035               (typ_clss (k+1) tfrees)
  1036     in 
  1037 	cls_str :: (typ_clss 0 tfree_lits)
  1038     end;
  1039 
  1040 fun string_of_arity (name, num) =  name ^ "," ^ (Int.toString num) 
  1041 
  1042 fun string_of_preds preds = 
  1043   "predicates[" ^ (concat_with ", " (map string_of_arity preds)) ^ "].\n";
  1044 
  1045 fun string_of_funcs funcs =
  1046   "functions[" ^ (concat_with ", " (map string_of_arity funcs)) ^ "].\n" ;
  1047 
  1048 
  1049 fun string_of_symbols predstr funcstr = 
  1050   "list_of_symbols.\n" ^ predstr  ^ funcstr  ^ "end_of_list.\n\n";
  1051 
  1052 
  1053 fun string_of_axioms axstr = 
  1054   "list_of_clauses(axioms,cnf).\n" ^ axstr ^ "end_of_list.\n\n";
  1055 
  1056 
  1057 fun string_of_conjectures conjstr = 
  1058   "list_of_clauses(conjectures,cnf).\n" ^ conjstr ^ "end_of_list.\n\n";
  1059 
  1060 fun string_of_descrip () = 
  1061   "list_of_descriptions.\nname({*[ File     : ],[ Names    :]*}).\nauthor({*[ Source   :]*}).\nstatus(unknown).\ndescription({*[ Refs     :]*}).\nend_of_list.\n\n"
  1062 
  1063 
  1064 fun string_of_start name = "%------------------------------------------------------------------------------\nbegin_problem(" ^ name ^ ").\n\n";
  1065 
  1066 
  1067 fun string_of_end () = "end_problem.\n%------------------------------------------------------------------------------";
  1068 
  1069 
  1070 fun clause2dfg cls =
  1071     let val (lits,tfree_lits) = dfg_clause_aux cls 
  1072             (*"lits" includes the typing assumptions (TVars)*)
  1073 	val cls_id = get_clause_id cls
  1074 	val ax_name = get_axiomName cls
  1075         val vars = dfg_vars cls
  1076         val tvars = dfg_tvars cls
  1077         val funcs = funcs_of_cls cls
  1078         val preds = preds_of_cls cls
  1079 	val knd = string_of_kind cls
  1080 	val lits_str = commas lits
  1081 	val cls_str = gen_dfg_cls(cls_id,ax_name,knd,lits_str,tvars,vars) 
  1082     in
  1083 	(cls_str,tfree_lits) 
  1084     end;
  1085 
  1086 
  1087 
  1088 fun tfree_dfg_clause tfree_lit =
  1089   "clause( %(conjecture)\n" ^ "or( " ^ tfree_lit ^ "),\n" ^ "tfree_tcs" ^ ")."
  1090 
  1091 
  1092 fun gen_dfg_file probname axioms conjectures funcs preds = 
  1093     let val axstrs_tfrees = (map clause2dfg axioms)
  1094 	val (axstrs, atfrees) = ListPair.unzip axstrs_tfrees
  1095         val axstr = (space_implode "\n" axstrs) ^ "\n\n"
  1096         val conjstrs_tfrees = (map clause2dfg conjectures)
  1097 	val (conjstrs, atfrees) = ListPair.unzip conjstrs_tfrees
  1098         val tfree_clss = map tfree_dfg_clause (union_all atfrees) 
  1099         val conjstr = (space_implode "\n" (tfree_clss@conjstrs)) ^ "\n\n"
  1100         val funcstr = string_of_funcs funcs
  1101         val predstr = string_of_preds preds
  1102     in
  1103        (string_of_start probname) ^ (string_of_descrip ()) ^ 
  1104        (string_of_symbols funcstr predstr) ^  
  1105        (string_of_axioms axstr) ^
  1106        (string_of_conjectures conjstr) ^ (string_of_end ())
  1107     end;
  1108    
  1109 fun clauses2dfg [] probname axioms conjectures funcs preds = 
  1110       let val funcs' = (union_all(map funcs_of_cls axioms)) @ funcs
  1111 	  val preds' = (union_all(map preds_of_cls axioms)) @ preds
  1112       in
  1113 	 gen_dfg_file probname axioms conjectures funcs' preds' 
  1114       end
  1115  | clauses2dfg (cls::clss) probname axioms conjectures funcs preds = 
  1116      let val (lits,tfree_lits) = dfg_clause_aux cls
  1117 	       (*"lits" includes the typing assumptions (TVars)*)
  1118 	 val cls_id = get_clause_id cls
  1119 	 val ax_name = get_axiomName cls
  1120 	 val vars = dfg_vars cls
  1121 	 val tvars = dfg_tvars cls
  1122 	 val funcs' = (funcs_of_cls cls) union funcs
  1123 	 val preds' = (preds_of_cls cls) union preds
  1124 	 val knd = string_of_kind cls
  1125 	 val lits_str = concat_with ", " lits
  1126 	 val axioms' = if knd = "axiom" then (cls::axioms) else axioms
  1127 	 val conjectures' = 
  1128 	     if knd = "conjecture" then (cls::conjectures) else conjectures
  1129      in
  1130 	 clauses2dfg clss probname axioms' conjectures' funcs' preds' 
  1131      end;
  1132 
  1133 
  1134 fun string_of_arClauseID (ArityClause {clause_id,axiom_name,...}) =
  1135     arclause_prefix ^ ascii_of axiom_name ^ "_" ^ Int.toString clause_id;
  1136 
  1137 fun string_of_arKind (ArityClause arcls) = name_of_kind(#kind arcls);
  1138 
  1139 (*FIXME!!! currently is TPTP format!*)
  1140 fun dfg_of_arLit (TConsLit(b,(c,t,args))) =
  1141       let val pol = if b then "++" else "--"
  1142 	  val arg_strs = paren_pack args
  1143       in 
  1144 	  pol ^ c ^ "(" ^ t ^ arg_strs ^ ")"
  1145       end
  1146   | dfg_of_arLit (TVarLit(b,(c,str))) =
  1147       let val pol = if b then "++" else "--"
  1148       in
  1149 	  pol ^ c ^ "(" ^ str ^ ")"
  1150       end;
  1151     
  1152 
  1153 fun dfg_of_conclLit (ArityClause arcls) = dfg_of_arLit (#conclLit arcls);
  1154      
  1155 
  1156 fun dfg_of_premLits (ArityClause arcls) = map dfg_of_arLit (#premLits arcls);
  1157 		
  1158 
  1159 
  1160 (*FIXME: would this have variables in a forall? *)
  1161 
  1162 fun dfg_arity_clause arcls = 
  1163   let val arcls_id = string_of_arClauseID arcls
  1164       val concl_lit = dfg_of_conclLit arcls
  1165       val prems_lits = dfg_of_premLits arcls
  1166       val knd = string_of_arKind arcls
  1167       val all_lits = concl_lit :: prems_lits
  1168   in
  1169       "clause( %(" ^ knd ^ ")\n" ^  "or( " ^ (bracket_pack all_lits) ^ ")),\n" ^
  1170        arcls_id ^  ")."
  1171   end;
  1172 
  1173 
  1174 (********************************)
  1175 (* code to produce TPTP files   *)
  1176 (********************************)
  1177 
  1178 fun tptp_literal (Literal(pol,pred,tag)) =
  1179     let val pred_string = string_of_predicate pred
  1180 	val tagged_pol = 
  1181 	      if (tag andalso !tagged) then (if pol then "+++" else "---")
  1182 	      else (if pol then "++" else "--")
  1183      in
  1184 	tagged_pol ^ pred_string
  1185     end;
  1186 
  1187 fun tptp_of_typeLit (LTVar x) = "--" ^ x
  1188   | tptp_of_typeLit (LTFree x) = "++" ^ x;
  1189  
  1190 
  1191 fun gen_tptp_cls (cls_id,ax_name,knd,lits) = 
  1192     "input_clause(" ^ string_of_clausename (cls_id,ax_name) ^ "," ^ 
  1193     knd ^ "," ^ lits ^ ").";
  1194 
  1195 fun gen_tptp_type_cls (cls_id,ax_name,knd,tfree_lit,idx) = 
  1196     "input_clause(" ^ string_of_type_clsname (cls_id,ax_name,idx) ^ "," ^ 
  1197     knd ^ ",[" ^ tfree_lit ^ "]).";
  1198 
  1199 fun tptp_type_lits (Clause cls) = 
  1200     let val lits = map tptp_literal (#literals cls)
  1201 	val tvar_lits_strs =
  1202 	      if !keep_types 
  1203 	      then (map tptp_of_typeLit (#tvar_type_literals cls)) 
  1204 	      else []
  1205 	val tfree_lits = 
  1206 	      if !keep_types
  1207 	      then (map tptp_of_typeLit (#tfree_type_literals cls)) 
  1208 	      else []
  1209     in
  1210 	(tvar_lits_strs @ lits, tfree_lits)
  1211     end; 
  1212 
  1213 fun tptp_clause cls =
  1214     let val (lits,tfree_lits) = tptp_type_lits cls 
  1215             (*"lits" includes the typing assumptions (TVars)*)
  1216 	val cls_id = get_clause_id cls
  1217 	val ax_name = get_axiomName cls
  1218 	val knd = string_of_kind cls
  1219 	val lits_str = bracket_pack lits
  1220 	val cls_str = gen_tptp_cls(cls_id,ax_name,knd,lits_str) 			 
  1221 	fun typ_clss k [] = []
  1222           | typ_clss k (tfree :: tfrees) = 
  1223               gen_tptp_type_cls(cls_id,ax_name,knd,tfree,k) :: 
  1224               typ_clss (k+1) tfrees
  1225     in 
  1226 	cls_str :: (typ_clss 0 tfree_lits)
  1227     end;
  1228 
  1229 fun clause2tptp cls =
  1230     let val (lits,tfree_lits) = tptp_type_lits cls 
  1231             (*"lits" includes the typing assumptions (TVars)*)
  1232 	val cls_id = get_clause_id cls
  1233 	val ax_name = get_axiomName cls
  1234 	val knd = string_of_kind cls
  1235 	val lits_str = bracket_pack lits
  1236 	val cls_str = gen_tptp_cls(cls_id,ax_name,knd,lits_str) 
  1237     in
  1238 	(cls_str,tfree_lits) 
  1239     end;
  1240 
  1241 
  1242 fun tfree_clause tfree_lit =
  1243     "input_clause(" ^ "tfree_tcs," ^ "conjecture" ^ ",[" ^ tfree_lit ^ "]).";
  1244 
  1245 
  1246 fun tptp_of_arLit (TConsLit(b,(c,t,args))) =
  1247       let val pol = if b then "++" else "--"
  1248 	  val  arg_strs = paren_pack args
  1249       in 
  1250 	  pol ^ c ^ "(" ^ t ^ arg_strs ^ ")"
  1251       end
  1252   | tptp_of_arLit (TVarLit(b,(c,str))) =
  1253       let val pol = if b then "++" else "--"
  1254       in
  1255 	  pol ^ c ^ "(" ^ str ^ ")"
  1256       end;
  1257     
  1258 
  1259 fun tptp_of_conclLit (ArityClause arcls) = tptp_of_arLit (#conclLit arcls);
  1260      
  1261 fun tptp_of_premLits (ArityClause arcls) = map tptp_of_arLit (#premLits arcls);
  1262 		
  1263 fun tptp_arity_clause arcls = 
  1264     let val arcls_id = string_of_arClauseID arcls
  1265 	val concl_lit = tptp_of_conclLit arcls
  1266 	val prems_lits = tptp_of_premLits arcls
  1267 	val knd = string_of_arKind arcls
  1268 	val all_lits = concl_lit :: prems_lits
  1269     in
  1270 	"input_clause(" ^ arcls_id ^ "," ^ knd ^ "," ^ 
  1271 	(bracket_pack all_lits) ^ ")."
  1272     end;
  1273 
  1274 fun tptp_classrelLits sub sup = 
  1275     let val tvar = "(T)"
  1276     in 
  1277 	"[--" ^ sub ^ tvar ^ ",++" ^ sup ^ tvar ^ "]"
  1278     end;
  1279 
  1280 fun tptp_classrelClause (ClassrelClause {clause_id,subclass,superclass,...}) =
  1281     let val relcls_id = clrelclause_prefix ^ ascii_of subclass ^ "_" ^ 
  1282                         Int.toString clause_id
  1283 	val lits = tptp_classrelLits (make_type_class subclass) (make_type_class superclass)
  1284     in
  1285 	"input_clause(" ^ relcls_id ^ ",axiom," ^ lits ^ ")."
  1286     end; 
  1287 
  1288 end;