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