src/HOL/Tools/res_hol_clause.ML
author haftmann
Fri Feb 10 09:09:07 2006 +0100 (2006-02-10)
changeset 19008 14c1b2f5dda4
parent 18856 4669dec681f4
child 19130 b23479b80828
permissions -rw-r--r--
improved code generator devarification
     1 (* ID: $Id$ 
     2    Author: Jia Meng, NICTA
     3 
     4 FOL clauses translated from HOL formulae.  Combinators are used to represent lambda terms.
     5 
     6 *)
     7 
     8 structure ResHolClause =
     9 
    10 struct
    11 
    12 
    13 val include_combS = ref false;
    14 val include_min_comb = ref false;
    15 
    16 val const_typargs = ref (Library.K [] : (string*typ -> typ list));
    17 
    18 fun init thy = (include_combS:=false;include_min_comb:=false;const_typargs := Sign.const_typargs thy);
    19 
    20 (**********************************************************************)
    21 (* convert a Term.term with lambdas into a Term.term with combinators *) 
    22 (**********************************************************************)
    23 
    24 fun is_free (Bound(a)) n = (a = n)
    25   | is_free (Abs(x,_,b)) n = (is_free b (n+1))
    26   | is_free (P $ Q) n = ((is_free P n) orelse (is_free Q n))
    27   | is_free _ _ = false;
    28 
    29 
    30 exception LAM2COMB of term;
    31 
    32 exception BND of term;
    33 
    34 fun decre_bndVar (Bound n) = Bound (n-1)
    35   | decre_bndVar (P $ Q) = (decre_bndVar P) $ (decre_bndVar Q)
    36   | decre_bndVar t =
    37     case t of Const(_,_) => t
    38 	    | Free(_,_) => t
    39 	    | Var(_,_) => t
    40 	    | Abs(_,_,_) => raise BND(t); (*should not occur*)
    41 
    42 
    43 (*******************************************)
    44 fun lam2comb (Abs(x,tp,Bound 0)) _ = 
    45     let val tpI = Type("fun",[tp,tp])
    46     in 
    47 	include_min_comb:=true;
    48 	Const("COMBI",tpI) 
    49     end
    50   | lam2comb (Abs(x,tp,Bound n)) Bnds = 
    51     let val (Bound n') = decre_bndVar (Bound n)
    52 	val tb = List.nth(Bnds,n')
    53 	val tK = Type("fun",[tb,Type("fun",[tp,tb])])
    54     in
    55 	include_min_comb:=true;
    56 	Const("COMBK",tK) $ (Bound n')
    57     end
    58   | lam2comb (Abs(x,t1,Const(c,t2))) _ = 
    59     let val tK = Type("fun",[t2,Type("fun",[t1,t2])])
    60     in 
    61 	include_min_comb:=true;
    62 	Const("COMBK",tK) $ Const(c,t2) 
    63     end
    64   | lam2comb (Abs(x,t1,Free(v,t2))) _ =
    65     let val tK = Type("fun",[t2,Type("fun",[t1,t2])])
    66     in
    67 	include_min_comb:=true;
    68 	Const("COMBK",tK) $ Free(v,t2)
    69     end
    70   | lam2comb (Abs(x,t1,Var(ind,t2))) _=
    71     let val tK = Type("fun",[t2,Type("fun",[t1,t2])])
    72     in
    73 	include_min_comb:=true;
    74 	Const("COMBK",tK) $ Var(ind,t2)
    75     end
    76   | lam2comb (t as (Abs(x,t1,P$(Bound 0)))) Bnds =
    77     let val nfreeP = not(is_free P 0)
    78 	val tr = Term.type_of1(t1::Bnds,P)
    79     in
    80 	if nfreeP then (decre_bndVar P)
    81 	else (
    82 	      let val tI = Type("fun",[t1,t1])
    83 		  val P' = lam2comb (Abs(x,t1,P)) Bnds
    84 		  val tp' = Term.type_of1(Bnds,P')
    85 		  val tS = Type("fun",[tp',Type("fun",[tI,tr])])
    86 	      in
    87 		  include_min_comb:=true;
    88 		  include_combS:=true;
    89 		  Const("COMBS",tS) $ P' $ Const("COMBI",tI)
    90 	      end)
    91     end
    92 	    
    93   | lam2comb (t as (Abs(x,t1,P$Q))) Bnds =
    94     let val (nfreeP,nfreeQ) = (not(is_free P 0),not(is_free Q 0))
    95 	val tpq = Term.type_of1(t1::Bnds, P$Q) 
    96     in
    97 	if(nfreeP andalso nfreeQ) then (
    98 	    let val tK = Type("fun",[tpq,Type("fun",[t1,tpq])])
    99 		val PQ' = decre_bndVar(P $ Q)
   100 	    in 
   101 		include_min_comb:=true;
   102 		Const("COMBK",tK) $ PQ'
   103 	    end)
   104 	else (
   105 	      if nfreeP then (
   106 			       let val Q' = lam2comb (Abs(x,t1,Q)) Bnds
   107 				   val P' = decre_bndVar P
   108 				   val tp = Term.type_of1(Bnds,P')
   109 				   val tq' = Term.type_of1(Bnds, Q')
   110 				   val tB = Type("fun",[tp,Type("fun",[tq',Type("fun",[t1,tpq])])])
   111 			       in
   112 				   include_min_comb:=true;
   113 				   Const("COMBB",tB) $ P' $ Q' 
   114 			       end)
   115 	      else (
   116 		    if nfreeQ then (
   117 				    let val P' = lam2comb (Abs(x,t1,P)) Bnds
   118 					val Q' = decre_bndVar Q
   119 					val tq = Term.type_of1(Bnds,Q')
   120 					val tp' = Term.type_of1(Bnds, P')
   121 					val tC = Type("fun",[tp',Type("fun",[tq,Type("fun",[t1,tpq])])])
   122 				    in
   123 					include_min_comb:=true;
   124 					Const("COMBC",tC) $ P' $ Q'
   125 				    end)
   126 		    else(
   127 			 let val P' = lam2comb (Abs(x,t1,P)) Bnds
   128 			     val Q' = lam2comb (Abs(x,t1,Q)) Bnds
   129 			     val tp' = Term.type_of1(Bnds,P')
   130 			     val tq' = Term.type_of1(Bnds,Q')
   131 			     val tS = Type("fun",[tp',Type("fun",[tq',Type("fun",[t1,tpq])])])
   132 			 in
   133 			     include_min_comb:=true;
   134 			     include_combS:=true;
   135 			     Const("COMBS",tS) $ P' $ Q'
   136 			 end)))
   137     end
   138   | lam2comb (t as (Abs(x,t1,_))) _ = raise LAM2COMB (t);
   139 
   140 	     
   141 
   142 (*********************)
   143 
   144 fun to_comb (Abs(x,tp,b)) Bnds =
   145     let val b' = to_comb b (tp::Bnds)
   146     in lam2comb (Abs(x,tp,b')) Bnds end
   147   | to_comb (P $ Q) Bnds = ((to_comb P Bnds) $ (to_comb Q Bnds))
   148   | to_comb t _ = t;
   149  
   150     
   151 fun comb_of t = to_comb t [];
   152 
   153 
   154 (* print a term containing combinators, used for debugging *)
   155 exception TERM_COMB of term;
   156 
   157 fun string_of_term (Const(c,t)) = c
   158   | string_of_term (Free(v,t)) = v
   159   | string_of_term (Var((x,n),t)) =
   160     let val xn = x ^ "_" ^ (string_of_int n)
   161     in xn end
   162   | string_of_term (P $ Q) =
   163     let val P' = string_of_term P
   164 	val Q' = string_of_term Q
   165     in
   166 	"(" ^ P' ^ " " ^ Q' ^ ")" end
   167   | string_of_term t =  raise TERM_COMB (t);
   168 
   169 
   170 
   171 (******************************************************)
   172 (* data types for typed combinator expressions        *)
   173 (******************************************************)
   174 
   175 type axiom_name = string;
   176 datatype kind = Axiom | Conjecture;
   177 fun name_of_kind Axiom = "axiom"
   178   | name_of_kind Conjecture = "conjecture";
   179 
   180 type polarity = bool;
   181 type indexname = Term.indexname;
   182 type clause_id = int;
   183 type csort = Term.sort;
   184 type ctyp = ResClause.fol_type;
   185 
   186 val string_of_ctyp = ResClause.string_of_fol_type;
   187 
   188 type ctyp_var = ResClause.typ_var;
   189 
   190 type ctype_literal = ResClause.type_literal;
   191 
   192 
   193 datatype combterm = CombConst of string * ctyp * ctyp list
   194 		  | CombFree of string * ctyp
   195 		  | CombVar of string * ctyp
   196 		  | CombApp of combterm * combterm * ctyp
   197 		  | Bool of combterm
   198 		  | Equal of combterm * combterm;
   199 datatype literal = Literal of polarity * combterm;
   200 
   201 
   202 
   203 datatype clause = 
   204 	 Clause of {clause_id: clause_id,
   205 		    axiom_name: axiom_name,
   206 		    kind: kind,
   207 		    literals: literal list,
   208 		    ctypes_sorts: (ctyp_var * csort) list, 
   209                     ctvar_type_literals: ctype_literal list, 
   210                     ctfree_type_literals: ctype_literal list};
   211 
   212 
   213 
   214 fun string_of_kind (Clause cls) = name_of_kind (#kind cls);
   215 fun get_axiomName (Clause cls) = #axiom_name cls;
   216 fun get_clause_id (Clause cls) = #clause_id cls;
   217 
   218 fun get_literals (c as Clause(cls)) = #literals cls;
   219 
   220 
   221 
   222 exception TERM_ORD of string
   223 
   224 fun term_ord (CombVar(_,_),CombVar(_,_)) = EQUAL
   225   | term_ord (CombVar(_,_),_) = LESS
   226   | term_ord (CombFree(_,_),CombVar(_,_)) = GREATER
   227   | term_ord (CombFree(f1,tp1),CombFree(f2,tp2)) = 
   228     let val ord1 = string_ord(f1,f2)
   229     in
   230 	case ord1 of EQUAL => ResClause.types_ord ([tp1],[tp2])
   231 		   | _ => ord1
   232     end
   233   | term_ord (CombFree(_,_),_) = LESS
   234   | term_ord (CombConst(_,_,_),CombVar(_,_)) = GREATER
   235   | term_ord (CombConst(_,_,_),CombFree(_,_)) = GREATER
   236   | term_ord (CombConst(c1,tp1,_),CombConst(c2,tp2,_)) = 
   237     let val ord1 = string_ord (c1,c2)
   238     in
   239 	case ord1 of EQUAL => ResClause.types_ord ([tp1],[tp2])
   240 		   | _ => ord1
   241     end
   242   | term_ord (CombConst(_,_,_),_) = LESS
   243   | term_ord (CombApp(_,_,_),Bool(_)) = raise TERM_ORD("bool")
   244   | term_ord (CombApp(_,_,_),Equal(_,_)) = LESS
   245   | term_ord (CombApp(f1,arg1,tp1),CombApp(f2,arg2,tp2)) =
   246     let val ord1 = term_ord (f1,f2)
   247 	val ord2 = case ord1 of EQUAL => term_ord (arg1,arg2)
   248 			      | _ => ord1
   249     in
   250 	case ord2 of EQUAL => ResClause.types_ord ([tp1],[tp2])
   251 		   | _ => ord2
   252     end
   253   | term_ord (CombApp(_,_,_),_) = GREATER
   254   | term_ord (Bool(_),_) = raise TERM_ORD("bool")
   255   | term_ord (Equal(t1,t2),Equal(t3,t4)) = ResClause.list_ord term_ord ([t1,t2],[t3,t4])
   256   | term_ord (Equal(_,_),_) = GREATER;
   257 
   258 fun predicate_ord (Equal(_,_),Bool(_)) = LESS
   259   | predicate_ord (Equal(t1,t2),Equal(t3,t4)) = 
   260     ResClause.list_ord term_ord ([t1,t2],[t3,t4])
   261   | predicate_ord (Bool(_),Equal(_,_)) = GREATER
   262   | predicate_ord (Bool(t1),Bool(t2)) = term_ord (t1,t2)
   263 
   264 
   265 fun literal_ord (Literal(false,_),Literal(true,_)) = LESS
   266   | literal_ord (Literal(true,_),Literal(false,_)) = GREATER
   267   | literal_ord (Literal(_,pred1),Literal(_,pred2)) = predicate_ord(pred1,pred2);
   268 
   269 fun sort_lits lits = sort literal_ord lits;
   270 
   271 (*********************************************************************)
   272 (* convert a clause with type Term.term to a clause with type clause *)
   273 (*********************************************************************)
   274 
   275 fun isFalse (Literal(pol,Bool(CombConst(c,_,_)))) =
   276     (pol andalso c = "c_False") orelse
   277     (not pol andalso c = "c_True")
   278   | isFalse _ = false;
   279 
   280 
   281 fun isTrue (Literal (pol,Bool(CombConst(c,_,_)))) =
   282       (pol andalso c = "c_True") orelse
   283       (not pol andalso c = "c_False")
   284   | isTrue _ = false;
   285   
   286 fun isTaut (Clause {literals,...}) = exists isTrue literals;  
   287 
   288 
   289 
   290 fun make_clause(clause_id,axiom_name,kind,literals,ctypes_sorts,ctvar_type_literals,ctfree_type_literals) =
   291     if forall isFalse literals
   292     then error "Problem too trivial for resolution (empty clause)"
   293     else
   294 	Clause {clause_id = clause_id, axiom_name = axiom_name, kind = kind,
   295 		literals = literals, ctypes_sorts = ctypes_sorts, 
   296 		ctvar_type_literals = ctvar_type_literals,
   297 		ctfree_type_literals = ctfree_type_literals};
   298 
   299 fun type_of (Type (a, Ts)) =
   300     let val (folTypes,ts) = types_of Ts
   301 	val t = ResClause.make_fixed_type_const a
   302     in
   303 	(ResClause.mk_fol_type("Comp",t,folTypes),ts)
   304     end
   305   | type_of (tp as (TFree(a,s))) =
   306     let val t = ResClause.make_fixed_type_var a
   307     in
   308 	(ResClause.mk_fol_type("Fixed",t,[]),[ResClause.mk_typ_var_sort tp])
   309     end
   310   | type_of (tp as (TVar(v,s))) =
   311     let val t = ResClause.make_schematic_type_var v
   312     in
   313 	(ResClause.mk_fol_type("Var",t,[]),[ResClause.mk_typ_var_sort tp])
   314     end
   315 
   316 and types_of Ts =
   317     let val foltyps_ts = map type_of Ts
   318 	val (folTyps,ts) = ListPair.unzip foltyps_ts
   319     in
   320 	(folTyps,ResClause.union_all ts)
   321     end;
   322 
   323 (* same as above, but no gathering of sort information *)
   324 fun simp_type_of (Type (a, Ts)) = 
   325     let val typs = map simp_type_of Ts
   326 	val t = ResClause.make_fixed_type_const a
   327     in
   328 	ResClause.mk_fol_type("Comp",t,typs)
   329     end
   330   | simp_type_of (TFree (a,s)) = ResClause.mk_fol_type("Fixed",ResClause.make_fixed_type_var a,[])
   331   | simp_type_of (TVar (v,s)) = ResClause.mk_fol_type("Var",ResClause.make_schematic_type_var v,[]);
   332 
   333 fun comb_typ ("COMBI",t) = 
   334     let val t' = domain_type t
   335     in
   336 	[simp_type_of t']
   337     end
   338   | comb_typ ("COMBK",t) = 
   339     let val a = domain_type t
   340 	val b = domain_type (range_type t)
   341     in
   342 	map simp_type_of [a,b]
   343     end
   344   | comb_typ ("COMBS",t) = 
   345     let val t' = domain_type t
   346 	val a = domain_type t'
   347 	val b = domain_type (range_type t')
   348 	val c = range_type (range_type t')
   349     in 
   350 	map simp_type_of [a,b,c]
   351     end
   352   | comb_typ ("COMBB",t) = 
   353     let val ab = domain_type t
   354 	val ca = domain_type (range_type t)
   355 	val a = domain_type ab
   356 	val b = range_type ab
   357 	val c = domain_type ca
   358     in
   359 	map simp_type_of [a,b,c]
   360     end
   361   | comb_typ ("COMBC",t) =
   362     let val t1 = domain_type t
   363 	val a = domain_type t1
   364 	val b = domain_type (range_type t1)
   365 	val c = range_type (range_type t1)
   366     in
   367 	map simp_type_of [a,b,c]
   368     end;
   369 
   370 fun const_type_of ("COMBI",t) = 
   371     let val (tp,ts) = type_of t
   372 	val I_var = comb_typ ("COMBI",t)
   373     in
   374 	(tp,ts,I_var)
   375     end
   376   | const_type_of ("COMBK",t) =
   377     let val (tp,ts) = type_of t
   378 	val K_var = comb_typ ("COMBK",t)
   379     in
   380 	(tp,ts,K_var)
   381     end
   382   | const_type_of ("COMBS",t) =
   383     let val (tp,ts) = type_of t
   384 	val S_var = comb_typ ("COMBS",t)
   385     in
   386 	(tp,ts,S_var)
   387     end
   388   | const_type_of ("COMBB",t) =
   389     let val (tp,ts) = type_of t
   390 	val B_var = comb_typ ("COMBB",t)
   391     in
   392 	(tp,ts,B_var)
   393     end
   394   | const_type_of ("COMBC",t) =
   395     let val (tp,ts) = type_of t
   396 	val C_var = comb_typ ("COMBC",t)
   397     in
   398 	(tp,ts,C_var)
   399     end
   400   | const_type_of (c,t) =
   401     let val (tp,ts) = type_of t
   402 	val tvars = !const_typargs(c,t)
   403 	val tvars' = map simp_type_of tvars
   404     in
   405 	(tp,ts,tvars')
   406     end;
   407 
   408 fun is_bool_type (Type("bool",[])) = true
   409   | is_bool_type _ = false;
   410 
   411 
   412 (* convert a Term.term (with combinators) into a combterm, also accummulate sort info *)
   413 fun combterm_of (Const(c,t)) =
   414     let val (tp,ts,tvar_list) = const_type_of (c,t)
   415 	val is_bool = is_bool_type t
   416 	val c' = CombConst(ResClause.make_fixed_const c,tp,tvar_list)
   417 	val c'' = if is_bool then Bool(c') else c'
   418     in
   419 	(c'',ts)
   420     end
   421   | combterm_of (Free(v,t)) =
   422     let val (tp,ts) = type_of t
   423 	val is_bool = is_bool_type t
   424 	val v' = if ResClause.isMeta v then CombVar(ResClause.make_schematic_var(v,0),tp)
   425 		 else CombFree(ResClause.make_fixed_var v,tp)
   426 	val v'' = if is_bool then Bool(v') else v'
   427     in
   428 	(v'',ts)
   429     end
   430   | combterm_of (Var(v,t)) =
   431     let val (tp,ts) = type_of t
   432 	val is_bool = is_bool_type t
   433 	val v' = CombVar(ResClause.make_schematic_var v,tp)
   434 	val v'' = if is_bool then Bool(v') else v'
   435     in
   436 	(v'',ts)
   437     end
   438   | combterm_of (Const("op =",T) $ P $ Q) = (*FIXME: allow equal between bools?*)
   439     let val (P',tsP) = combterm_of P        
   440 	val (Q',tsQ) = combterm_of Q
   441     in
   442 	(Equal(P',Q'),tsP union tsQ)
   443     end
   444   | combterm_of (t as (P $ Q)) =
   445     let val (P',tsP) = combterm_of P
   446 	val (Q',tsQ) = combterm_of Q
   447 	val tp = Term.type_of t
   448 	val tp' = simp_type_of tp
   449 	val is_bool = is_bool_type tp
   450 	val t' = CombApp(P',Q',tp')
   451 	val t'' = if is_bool then Bool(t') else t'
   452     in
   453 	(t'',tsP union tsQ)
   454     end;
   455 
   456 fun predicate_of ((Const("Not",_) $ P), polarity) =
   457     predicate_of (P, not polarity)
   458   | predicate_of (term,polarity) = (combterm_of term,polarity);
   459 
   460 
   461 fun literals_of_term1 args (Const("Trueprop",_) $ P) = literals_of_term1 args P
   462   | literals_of_term1 args (Const("op |",_) $ P $ Q) = 
   463     let val args' = literals_of_term1 args P
   464     in
   465 	literals_of_term1 args' Q
   466     end
   467   | literals_of_term1 (lits,ts) P =
   468     let val ((pred,ts'),pol) = predicate_of (P,true)
   469 	val lits' = Literal(pol,pred)::lits
   470     in
   471 	(lits',ts union ts')
   472     end;
   473 
   474 
   475 fun literals_of_term P = literals_of_term1 ([],[]) P;
   476 
   477 
   478 (* making axiom and conjecture clauses *)
   479 fun make_axiom_clause term (ax_name,cls_id) =
   480     let val term' = comb_of term
   481 	val (lits,ctypes_sorts) = literals_of_term term'
   482 	val lits' = sort_lits lits
   483 	val (ctvar_lits,ctfree_lits) = ResClause.add_typs_aux ctypes_sorts
   484     in
   485 	make_clause(cls_id,ax_name,Axiom,
   486 		    lits',ctypes_sorts,ctvar_lits,ctfree_lits)
   487     end;
   488 
   489 
   490 fun make_conjecture_clause n t =
   491     let val t' = comb_of t
   492 	val (lits,ctypes_sorts) = literals_of_term t'
   493 	val (ctvar_lits,ctfree_lits) = ResClause.add_typs_aux ctypes_sorts
   494     in
   495 	make_clause(n,"conjecture",Conjecture,lits,ctypes_sorts,ctvar_lits,ctfree_lits)
   496     end;
   497 
   498 
   499 
   500 fun make_conjecture_clauses_aux _ [] = []
   501   | make_conjecture_clauses_aux n (t::ts) =
   502     make_conjecture_clause n t :: make_conjecture_clauses_aux (n+1) ts;
   503 
   504 val make_conjecture_clauses = make_conjecture_clauses_aux 0;
   505 
   506 
   507 (**********************************************************************)
   508 (* convert clause into ATP specific formats:                          *)
   509 (* TPTP used by Vampire and E                                         *)
   510 (**********************************************************************)
   511 
   512 val type_wrapper = "typeinfo";
   513 
   514 datatype type_level = T_FULL | T_PARTIAL | T_CONST | T_NONE;
   515 
   516 val typ_level = ref T_PARTIAL;
   517 
   518 fun full_types () = (typ_level:=T_FULL);
   519 fun partial_types () = (typ_level:=T_PARTIAL);
   520 fun const_types_only () = (typ_level:=T_CONST);
   521 fun no_types () = (typ_level:=T_NONE);
   522 
   523 
   524 fun find_typ_level () = !typ_level;
   525 
   526 fun wrap_type (c,t) = 
   527     case !typ_level of T_FULL => type_wrapper ^ (ResClause.paren_pack [c,t])
   528 		     | _ => c;
   529     
   530 
   531 val bool_tp = ResClause.make_fixed_type_const "bool";
   532 
   533 val app_str = "hAPP";
   534 
   535 val bool_str = "hBOOL";
   536 
   537 exception STRING_OF_COMBTERM of int;
   538 
   539 (* convert literals of clauses into strings *)
   540 fun string_of_combterm1_aux _ (CombConst(c,tp,_)) = 
   541     let val tp' = string_of_ctyp tp
   542     in
   543 	(wrap_type (c,tp'),tp')
   544     end
   545   | string_of_combterm1_aux _ (CombFree(v,tp)) = 
   546     let val tp' = string_of_ctyp tp
   547     in
   548 	(wrap_type (v,tp'),tp')
   549     end
   550   | string_of_combterm1_aux _ (CombVar(v,tp)) = 
   551     let val tp' = string_of_ctyp tp
   552     in
   553 	(wrap_type (v,tp'),tp')
   554     end
   555   | string_of_combterm1_aux is_pred (CombApp(t1,t2,tp)) =
   556     let val (s1,tp1) = string_of_combterm1_aux is_pred t1
   557 	val (s2,tp2) = string_of_combterm1_aux is_pred t2
   558 	val tp' = ResClause.string_of_fol_type tp
   559 	val r =	case !typ_level of T_FULL => type_wrapper ^  (ResClause.paren_pack [(app_str ^ (ResClause.paren_pack [s1,s2])),tp'])
   560 				 | T_PARTIAL => app_str ^ (ResClause.paren_pack [s1,s2,tp1])
   561 				 | T_NONE => app_str ^ (ResClause.paren_pack [s1,s2])
   562 				 | T_CONST => raise STRING_OF_COMBTERM (1) (*should not happen, if happened may be a bug*)
   563     in	(r,tp')
   564 
   565     end
   566   | string_of_combterm1_aux is_pred (Bool(t)) = 
   567     let val (t',_) = string_of_combterm1_aux false t
   568 	val r = if is_pred then bool_str ^ (ResClause.paren_pack [t'])
   569 		else t'
   570     in
   571 	(r,bool_tp)
   572     end
   573   | string_of_combterm1_aux _ (Equal(t1,t2)) =
   574     let val (s1,_) = string_of_combterm1_aux false t1
   575 	val (s2,_) = string_of_combterm1_aux false t2
   576     in
   577 	("equal" ^ (ResClause.paren_pack [s1,s2]),bool_tp) 
   578     end;
   579 
   580 fun string_of_combterm1 is_pred term = fst (string_of_combterm1_aux is_pred term);
   581 
   582 fun string_of_combterm2 _ (CombConst(c,tp,tvars)) = 
   583     let val tvars' = map string_of_ctyp tvars
   584     in
   585 	c ^ (ResClause.paren_pack tvars')
   586     end
   587   | string_of_combterm2 _ (CombFree(v,tp)) = v
   588   | string_of_combterm2 _ (CombVar(v,tp)) = v
   589   | string_of_combterm2 is_pred (CombApp(t1,t2,tp)) =
   590     let val s1 = string_of_combterm2 is_pred t1
   591 	val s2 = string_of_combterm2 is_pred t2
   592     in
   593 	app_str ^ (ResClause.paren_pack [s1,s2])
   594     end
   595   | string_of_combterm2 is_pred (Bool(t)) = 
   596     let val t' = string_of_combterm2 false t
   597     in
   598 	if is_pred then bool_str ^ (ResClause.paren_pack [t'])
   599 	else t'
   600     end
   601   | string_of_combterm2 _ (Equal(t1,t2)) =
   602     let val s1 = string_of_combterm2 false t1
   603 	val s2 = string_of_combterm2 false t2
   604     in
   605 	("equal" ^ (ResClause.paren_pack [s1,s2])) 
   606     end;
   607 
   608 
   609 
   610 fun string_of_combterm is_pred term = 
   611     case !typ_level of T_CONST => string_of_combterm2 is_pred term
   612 		     | _ => string_of_combterm1 is_pred term;
   613 
   614 
   615 fun string_of_clausename (cls_id,ax_name) = 
   616     ResClause.clause_prefix ^ ResClause.ascii_of ax_name ^ "_" ^ Int.toString cls_id;
   617 
   618 fun string_of_type_clsname (cls_id,ax_name,idx) = 
   619     string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
   620 
   621 
   622 fun tptp_literal (Literal(pol,pred)) =
   623     let val pred_string = string_of_combterm true pred
   624 	val pol_str = if pol then "++" else "--"
   625     in
   626 	pol_str ^ pred_string
   627     end;
   628 
   629  
   630 fun tptp_type_lits (Clause cls) = 
   631     let val lits = map tptp_literal (#literals cls)
   632 	val ctvar_lits_strs =
   633 	    case !typ_level of T_NONE => []
   634 			     | _ => (map ResClause.tptp_of_typeLit (#ctvar_type_literals cls)) 
   635 	val ctfree_lits = 
   636 	    case !typ_level of T_NONE => []
   637 			     | _ => (map ResClause.tptp_of_typeLit (#ctfree_type_literals cls)) 
   638     in
   639 	(ctvar_lits_strs @ lits, ctfree_lits)
   640     end; 
   641     
   642     
   643 fun clause2tptp cls =
   644     let val (lits,ctfree_lits) = tptp_type_lits cls
   645 	val cls_id = get_clause_id cls
   646 	val ax_name = get_axiomName cls
   647 	val knd = string_of_kind cls
   648 	val lits_str = ResClause.bracket_pack lits
   649 	val cls_str = ResClause.gen_tptp_cls(cls_id,ax_name,knd,lits_str)
   650     in
   651 	(cls_str,ctfree_lits)
   652     end;
   653 
   654 
   655 
   656 (**********************************************************************)
   657 (* clause equalities and hashing functions                            *)
   658 (**********************************************************************)
   659 
   660 
   661 fun combterm_eq (CombConst(c1,tp1,tps1),CombConst(c2,tp2,tps2)) vtvars =
   662     let val (eq1,vtvars1) = if c1 = c2 then ResClause.types_eq (tps1,tps2) vtvars
   663 			    else (false,vtvars)
   664     in
   665 	(eq1,vtvars1)
   666     end
   667   | combterm_eq (CombConst(_,_,_),_) vtvars = (false,vtvars)
   668   | combterm_eq (CombFree(a1,tp1),CombFree(a2,tp2)) vtvars = 
   669     if a1 = a2 then ResClause.types_eq ([tp1],[tp2]) vtvars
   670     else (false,vtvars)
   671   | combterm_eq (CombFree(_,_),_) vtvars = (false,vtvars)
   672   | combterm_eq (CombVar(v1,tp1),CombVar(v2,tp2)) (vars,tvars) = 
   673     (case ResClause.check_var_pairs(v1,v2) vars of 0 => ResClause.types_eq ([tp1],[tp2]) ((v1,v2)::vars,tvars)
   674 						 | 1 => ResClause.types_eq ([tp1],[tp2]) (vars,tvars)
   675 						 | 2 => (false,(vars,tvars)))
   676   | combterm_eq (CombVar(_,_),_) vtvars = (false,vtvars)
   677   | combterm_eq (CombApp(f1,arg1,tp1),CombApp(f2,arg2,tp2)) vtvars =
   678     let val (eq1,vtvars1) = combterm_eq (f1,f2) vtvars
   679 	val (eq2,vtvars2) = if eq1 then combterm_eq (arg1,arg2) vtvars1
   680 			    else (eq1,vtvars1)
   681     in
   682 	if eq2 then ResClause.types_eq ([tp1],[tp2]) vtvars2
   683 	else (eq2,vtvars2)
   684     end
   685   | combterm_eq (CombApp(_,_,_),_) vtvars = (false,vtvars)
   686   | combterm_eq (Bool(t1),Bool(t2)) vtvars = combterm_eq (t1,t2) vtvars
   687   | combterm_eq (Bool(_),_) vtvars = (false,vtvars)
   688   | combterm_eq (Equal(t1,t2),Equal(t3,t4)) vtvars =
   689     let val (eq1,vtvars1) = combterm_eq (t1,t3) vtvars
   690     in
   691 	if eq1 then combterm_eq (t2,t4) vtvars1
   692 	else (eq1,vtvars1)
   693     end
   694   | combterm_eq (Equal(t1,t2),_) vtvars = (false,vtvars);
   695 
   696 fun lit_eq (Literal(pol1,pred1),Literal(pol2,pred2)) vtvars =
   697     if (pol1 = pol2) then combterm_eq (pred1,pred2) vtvars
   698     else (false,vtvars);
   699 
   700 fun lits_eq ([],[]) vtvars = (true,vtvars)
   701   | lits_eq (l1::ls1,l2::ls2) vtvars = 
   702     let val (eq1,vtvars1) = lit_eq (l1,l2) vtvars
   703     in
   704 	if eq1 then lits_eq (ls1,ls2) vtvars1
   705 	else (false,vtvars1)
   706     end;
   707 
   708 fun clause_eq (cls1,cls2) =
   709     let val lits1 = get_literals cls1
   710 	val lits2 = get_literals cls2
   711     in
   712 	length lits1 = length lits2 andalso #1 (lits_eq (lits1,lits2) ([],[]))
   713     end;
   714 
   715 val xor_words = List.foldl Word.xorb 0w0;
   716 
   717 fun hash_combterm (CombVar(_,_),w) = w
   718   | hash_combterm (CombFree(f,_),w) = Polyhash.hashw_string(f,w)
   719   | hash_combterm (CombConst(c,tp,tps),w) = Polyhash.hashw_string(c,w)
   720   | hash_combterm (CombApp(f,arg,tp),w) = hash_combterm (arg, hash_combterm (f,w))
   721   | hash_combterm (Bool(t),w) = hash_combterm (t,w)
   722   | hash_combterm (Equal(t1,t2),w) = 
   723     List.foldl hash_combterm (Polyhash.hashw_string ("equal",w)) [t1,t2]
   724 
   725 fun hash_literal (Literal(true,pred)) = hash_combterm(pred,0w0)
   726   | hash_literal (Literal(false,pred)) = Word.notb(hash_combterm(pred,0w0));
   727 
   728 fun hash_clause clause = xor_words (map hash_literal (get_literals clause));
   729 
   730 end