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