src/HOL/Tools/res_hol_clause.ML
author paulson
Fri Oct 20 11:04:15 2006 +0200 (2006-10-20 ago)
changeset 21070 0a898140fea2
parent 20997 4b500d78cb4f
child 21108 04d8e6eb9a2e
permissions -rw-r--r--
Added more debugging info
     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 (* theorems for combinators and function extensionality *)
    13 val ext = thm "HOL.ext";
    14 val comb_I = thm "Reconstruction.COMBI_def";
    15 val comb_K = thm "Reconstruction.COMBK_def";
    16 val comb_B = thm "Reconstruction.COMBB_def";
    17 val comb_C = thm "Reconstruction.COMBC_def";
    18 val comb_S = thm "Reconstruction.COMBS_def";
    19 val comb_B' = thm "Reconstruction.COMBB'_def";
    20 val comb_C' = thm "Reconstruction.COMBC'_def";
    21 val comb_S' = thm "Reconstruction.COMBS'_def";
    22 val fequal_imp_equal = thm "Reconstruction.fequal_imp_equal";
    23 val equal_imp_fequal = thm "Reconstruction.equal_imp_fequal";
    24 
    25 
    26 (*A flag to set if we use extra combinators B',C',S', currently FALSE as the 5 standard
    27   combinators appear to work best.*)
    28 val use_combB'C'S' = ref false;
    29 
    30 val combI_count = ref 0;
    31 val combK_count = ref 0;
    32 val combB_count = ref 0;
    33 val combC_count = ref 0;
    34 val combS_count = ref 0;
    35 
    36 val combB'_count = ref 0;
    37 val combC'_count = ref 0;
    38 val combS'_count = ref 0; 
    39 
    40 
    41 fun increI count_comb =  if count_comb then combI_count := !combI_count + 1 else ();
    42 fun increK count_comb =  if count_comb then combK_count := !combK_count + 1 else ();
    43 fun increB count_comb =  if count_comb then combB_count := !combB_count + 1 else ();
    44 fun increC count_comb =  if count_comb then combC_count := !combC_count + 1 else ();
    45 fun increS count_comb =  if count_comb then combS_count := !combS_count + 1 else (); 
    46 fun increB' count_comb =  if count_comb then combB'_count := !combB'_count + 1 else (); 
    47 fun increC' count_comb =  if count_comb then combC'_count := !combC'_count + 1 else ();
    48 fun increS' count_comb =  if count_comb then combS'_count := !combS'_count + 1 else (); 
    49 
    50 
    51 exception DECRE_COMB of string;
    52 fun decreB count_comb n = 
    53   if count_comb then 
    54     if !combB_count >= n then combB_count := !combB_count - n else raise DECRE_COMB "COMBB"
    55   else ();
    56 
    57 fun decreC count_comb n =
    58   if count_comb then 
    59     if !combC_count >= n then combC_count := !combC_count - n else raise DECRE_COMB "COMBC"
    60   else ();
    61 
    62 fun decreS count_comb n = 
    63   if count_comb then 
    64     if !combS_count >= n then combS_count := !combS_count - n else raise DECRE_COMB "COMBS"
    65   else ();
    66 
    67 val const_typargs = ref (Library.K [] : (string*typ -> typ list));
    68 
    69 fun init thy = (combI_count:=0; combK_count:=0;combB_count:=0;combC_count:=0;combS_count:=0;combB'_count:=0;combC'_count:=0;combS'_count:=0;
    70                 const_typargs := Sign.const_typargs thy);
    71 
    72 (**********************************************************************)
    73 (* convert a Term.term with lambdas into a Term.term with combinators *) 
    74 (**********************************************************************)
    75 
    76 fun is_free (Bound(a)) n = (a = n)
    77   | is_free (Abs(x,_,b)) n = (is_free b (n+1))
    78   | is_free (P $ Q) n = ((is_free P n) orelse (is_free Q n))
    79   | is_free _ _ = false;
    80 
    81 
    82 exception BND of term;
    83 
    84 fun decre_bndVar (Bound n) = Bound (n-1)
    85   | decre_bndVar (P $ Q) = (decre_bndVar P) $ (decre_bndVar Q)
    86   | decre_bndVar t =
    87     case t of Const(_,_) => t
    88 	    | Free(_,_) => t
    89 	    | Var(_,_) => t
    90 	    | Abs(_,_,_) => raise BND(t); (*should not occur*)
    91 
    92 
    93 (*******************************************)
    94 fun mk_compact_comb (tm as (Const("Reconstruction.COMBB",_)$p) $ (Const("Reconstruction.COMBB",_)$q$r)) Bnds count_comb =
    95     let val tp_p = Term.type_of1(Bnds,p)
    96 	val tp_q = Term.type_of1(Bnds,q)
    97 	val tp_r = Term.type_of1(Bnds,r)
    98 	val typ = Term.type_of1(Bnds,tm)
    99 	val typ_B' = [tp_p,tp_q,tp_r] ---> typ
   100 	val _ = increB' count_comb
   101 	val _ = decreB count_comb 2
   102     in
   103 	Const("Reconstruction.COMBB'",typ_B') $ p $ q $ r
   104     end
   105   | mk_compact_comb (tm as (Const("Reconstruction.COMBC",_) $ (Const("Reconstruction.COMBB",_)$p$q) $ r)) Bnds count_comb =
   106     let val tp_p = Term.type_of1(Bnds,p)
   107 	val tp_q = Term.type_of1(Bnds,q)
   108 	val tp_r = Term.type_of1(Bnds,r)
   109 	val typ = Term.type_of1(Bnds,tm)
   110 	val typ_C' = [tp_p,tp_q,tp_r] ---> typ
   111 	val _ = increC' count_comb
   112 	val _ = decreC count_comb 1
   113 	val _ = decreB count_comb 1
   114     in
   115 	Const("Reconstruction.COMBC'",typ_C') $ p $ q $ r
   116     end
   117   | mk_compact_comb (tm as (Const("Reconstruction.COMBS",_) $ (Const("Reconstruction.COMBB",_)$p$q) $ r)) Bnds count_comb =
   118     let val tp_p = Term.type_of1(Bnds,p)
   119 	val tp_q = Term.type_of1(Bnds,q)
   120 	val tp_r = Term.type_of1(Bnds,r)
   121 	val typ = Term.type_of1(Bnds,tm)
   122 	val typ_S' = [tp_p,tp_q,tp_r] ---> typ
   123 	val _ = increS' count_comb
   124 	val _ = decreS count_comb 1
   125 	val _ = decreB count_comb 1
   126     in
   127 	Const("Reconstruction.COMBS'",typ_S') $ p $ q $ r
   128     end
   129   | mk_compact_comb tm _ _ = tm;
   130 
   131 fun compact_comb t Bnds count_comb = if !use_combB'C'S' then mk_compact_comb t Bnds count_comb else t;
   132 
   133 fun lam2comb (Abs(x,tp,Bound 0)) _ count_comb = 
   134     let val tpI = Type("fun",[tp,tp])
   135 	val _ = increI count_comb
   136     in 
   137 	Const("Reconstruction.COMBI",tpI) 
   138     end
   139   | lam2comb (Abs(x,tp,Bound n)) Bnds count_comb = 
   140     let val (Bound n') = decre_bndVar (Bound n)
   141 	val tb = List.nth(Bnds,n')
   142 	val tK = Type("fun",[tb,Type("fun",[tp,tb])])
   143 	val _ = increK count_comb 
   144     in
   145 	Const("Reconstruction.COMBK",tK) $ (Bound n')
   146     end
   147   | lam2comb (Abs(x,t1,Const(c,t2))) _ count_comb = 
   148     let val tK = Type("fun",[t2,Type("fun",[t1,t2])])
   149 	val _ = increK count_comb 
   150     in 
   151 	Const("Reconstruction.COMBK",tK) $ Const(c,t2) 
   152     end
   153   | lam2comb (Abs(x,t1,Free(v,t2))) _ count_comb =
   154     let val tK = Type("fun",[t2,Type("fun",[t1,t2])])
   155 	val _ = increK count_comb
   156     in
   157 	Const("Reconstruction.COMBK",tK) $ Free(v,t2)
   158     end
   159   | lam2comb (Abs(x,t1,Var(ind,t2))) _ count_comb =
   160     let val tK = Type("fun",[t2,Type("fun",[t1,t2])])
   161 	val _ = increK count_comb 
   162     in
   163 	Const("Reconstruction.COMBK",tK) $ Var(ind,t2)
   164     end
   165   | lam2comb (t as (Abs(x,t1,P$(Bound 0)))) Bnds count_comb =
   166     let val nfreeP = not(is_free P 0)
   167 	val tr = Term.type_of1(t1::Bnds,P)
   168     in
   169 	if nfreeP then (decre_bndVar P)
   170 	else (
   171 	      let val tI = Type("fun",[t1,t1])
   172 		  val P' = lam2comb (Abs(x,t1,P)) Bnds count_comb
   173 		  val tp' = Term.type_of1(Bnds,P')
   174 		  val tS = Type("fun",[tp',Type("fun",[tI,tr])])
   175 		  val _ = increI count_comb
   176 		  val _ = increS count_comb
   177 	      in
   178 		  compact_comb (Const("Reconstruction.COMBS",tS) $ P' $ Const("Reconstruction.COMBI",tI)) Bnds count_comb
   179 	      end)
   180     end
   181 	    
   182   | lam2comb (t as (Abs(x,t1,P$Q))) Bnds count_comb =
   183     let val (nfreeP,nfreeQ) = (not(is_free P 0),not(is_free Q 0))
   184 	val tpq = Term.type_of1(t1::Bnds, P$Q) 
   185     in
   186 	if(nfreeP andalso nfreeQ) then (
   187 	    let val tK = Type("fun",[tpq,Type("fun",[t1,tpq])])
   188 		val PQ' = decre_bndVar(P $ Q)
   189 		val _ = increK count_comb
   190 	    in 
   191 		Const("Reconstruction.COMBK",tK) $ PQ'
   192 	    end)
   193 	else (
   194 	      if nfreeP then (
   195 			       let val Q' = lam2comb (Abs(x,t1,Q)) Bnds count_comb
   196 				   val P' = decre_bndVar P
   197 				   val tp = Term.type_of1(Bnds,P')
   198 				   val tq' = Term.type_of1(Bnds, Q')
   199 				   val tB = Type("fun",[tp,Type("fun",[tq',Type("fun",[t1,tpq])])])
   200 				   val _ = increB count_comb
   201 			       in
   202 				   compact_comb (Const("Reconstruction.COMBB",tB) $ P' $ Q') Bnds count_comb 
   203 			       end)
   204 	      else (
   205 		    if nfreeQ then (
   206 				    let val P' = lam2comb (Abs(x,t1,P)) Bnds count_comb
   207 					val Q' = decre_bndVar Q
   208 					val tq = Term.type_of1(Bnds,Q')
   209 					val tp' = Term.type_of1(Bnds, P')
   210 					val tC = Type("fun",[tp',Type("fun",[tq,Type("fun",[t1,tpq])])])
   211 					val _ = increC count_comb
   212 				    in
   213 					compact_comb (Const("Reconstruction.COMBC",tC) $ P' $ Q') Bnds count_comb
   214 				    end)
   215 		    else(
   216 			 let val P' = lam2comb (Abs(x,t1,P)) Bnds count_comb
   217 			     val Q' = lam2comb (Abs(x,t1,Q)) Bnds count_comb 
   218 			     val tp' = Term.type_of1(Bnds,P')
   219 			     val tq' = Term.type_of1(Bnds,Q')
   220 			     val tS = Type("fun",[tp',Type("fun",[tq',Type("fun",[t1,tpq])])])
   221 			     val _ = increS count_comb
   222 			 in
   223 			     compact_comb (Const("Reconstruction.COMBS",tS) $ P' $ Q') Bnds count_comb
   224 			 end)))
   225     end
   226   | lam2comb (t as (Abs(x,t1,_))) _ _ = raise ResClause.CLAUSE("HOL CLAUSE",t);
   227 
   228 (*********************)
   229 
   230 fun to_comb (Abs(x,tp,b)) Bnds count_comb =
   231     let val b' = to_comb b (tp::Bnds) count_comb
   232     in lam2comb (Abs(x,tp,b')) Bnds count_comb end
   233   | to_comb (P $ Q) Bnds count_comb = ((to_comb P Bnds count_comb) $ (to_comb Q Bnds count_comb))
   234   | to_comb t _ _ = t;
   235  
   236    
   237 fun comb_of t count_comb = to_comb t [] count_comb;
   238  
   239 (* print a term containing combinators, used for debugging *)
   240 exception TERM_COMB of term;
   241 
   242 fun string_of_term (Const(c,t)) = c
   243   | string_of_term (Free(v,t)) = v
   244   | string_of_term (Var((x,n),t)) = x ^ "_" ^ (string_of_int n)
   245   | string_of_term (P $ Q) =
   246       "(" ^ string_of_term P ^ " " ^ string_of_term Q ^ ")" 
   247   | string_of_term t =  raise TERM_COMB (t);
   248 
   249 
   250 
   251 (******************************************************)
   252 (* data types for typed combinator expressions        *)
   253 (******************************************************)
   254 
   255 datatype type_level = T_FULL | T_PARTIAL | T_CONST | T_NONE;
   256 
   257 val typ_level = ref T_CONST;
   258 
   259 fun full_types () = (typ_level:=T_FULL);
   260 fun partial_types () = (typ_level:=T_PARTIAL);
   261 fun const_types_only () = (typ_level:=T_CONST);
   262 fun no_types () = (typ_level:=T_NONE);
   263 
   264 
   265 fun find_typ_level () = !typ_level;
   266 
   267 
   268 type axiom_name = string;
   269 datatype kind = Axiom | Conjecture;
   270 
   271 fun name_of_kind Axiom = "axiom"
   272   | name_of_kind Conjecture = "conjecture";
   273 
   274 type polarity = bool;
   275 type indexname = Term.indexname;
   276 type clause_id = int;
   277 type csort = Term.sort;
   278 type ctyp = ResClause.fol_type;
   279 
   280 val string_of_ctyp = ResClause.string_of_fol_type;
   281 
   282 type ctyp_var = ResClause.typ_var;
   283 
   284 type ctype_literal = ResClause.type_literal;
   285 
   286 
   287 datatype combterm = CombConst of string * ctyp * ctyp list
   288 		  | CombFree of string * ctyp
   289 		  | CombVar of string * ctyp
   290 		  | CombApp of combterm * combterm * ctyp
   291 		  | Bool of combterm;
   292 		  
   293 datatype literal = Literal of polarity * combterm;
   294 
   295 datatype clause = 
   296 	 Clause of {clause_id: clause_id,
   297 		    axiom_name: axiom_name,
   298 		    th: thm,
   299 		    kind: kind,
   300 		    literals: literal list,
   301 		    ctypes_sorts: (ctyp_var * csort) list, 
   302                     ctvar_type_literals: ctype_literal list, 
   303                     ctfree_type_literals: ctype_literal list};
   304 
   305 
   306 fun string_of_kind (Clause cls) = name_of_kind (#kind cls);
   307 fun get_axiomName (Clause cls) = #axiom_name cls;
   308 fun get_clause_id (Clause cls) = #clause_id cls;
   309 
   310 fun get_literals (c as Clause(cls)) = #literals cls;
   311 
   312 
   313 (*********************************************************************)
   314 (* convert a clause with type Term.term to a clause with type clause *)
   315 (*********************************************************************)
   316 
   317 fun isFalse (Literal(pol,Bool(CombConst(c,_,_)))) =
   318       (pol andalso c = "c_False") orelse
   319       (not pol andalso c = "c_True")
   320   | isFalse _ = false;
   321 
   322 
   323 fun isTrue (Literal (pol,Bool(CombConst(c,_,_)))) =
   324       (pol andalso c = "c_True") orelse
   325       (not pol andalso c = "c_False")
   326   | isTrue _ = false;
   327   
   328 fun isTaut (Clause {literals,...}) = exists isTrue literals;  
   329 
   330 fun type_of (Type (a, Ts)) =
   331     let val (folTypes,ts) = types_of Ts
   332 	val t = ResClause.make_fixed_type_const a
   333     in
   334 	(ResClause.mk_fol_type("Comp",t,folTypes),ts)
   335     end
   336   | type_of (tp as (TFree(a,s))) =
   337     let val t = ResClause.make_fixed_type_var a
   338     in
   339 	(ResClause.mk_fol_type("Fixed",t,[]),[ResClause.mk_typ_var_sort tp])
   340     end
   341   | type_of (tp as (TVar(v,s))) =
   342     let val t = ResClause.make_schematic_type_var v
   343     in
   344 	(ResClause.mk_fol_type("Var",t,[]),[ResClause.mk_typ_var_sort tp])
   345     end
   346 
   347 and types_of Ts =
   348     let val foltyps_ts = map type_of Ts
   349 	val (folTyps,ts) = ListPair.unzip foltyps_ts
   350     in
   351 	(folTyps,ResClause.union_all ts)
   352     end;
   353 
   354 (* same as above, but no gathering of sort information *)
   355 fun simp_type_of (Type (a, Ts)) = 
   356     let val typs = map simp_type_of Ts
   357 	val t = ResClause.make_fixed_type_const a
   358     in
   359 	ResClause.mk_fol_type("Comp",t,typs)
   360     end
   361   | simp_type_of (TFree (a,s)) = ResClause.mk_fol_type("Fixed",ResClause.make_fixed_type_var a,[])
   362   | simp_type_of (TVar (v,s)) = ResClause.mk_fol_type("Var",ResClause.make_schematic_type_var v,[]);
   363 
   364 
   365 fun const_type_of (c,t) =
   366     let val (tp,ts) = type_of t
   367 	val tvars = !const_typargs(c,t)
   368 	val tvars' = map simp_type_of tvars
   369     in
   370 	(tp,ts,tvars')
   371     end;
   372 
   373 
   374 fun is_bool_type (Type("bool",[])) = true
   375   | is_bool_type _ = false;
   376 
   377 
   378 (* convert a Term.term (with combinators) into a combterm, also accummulate sort info *)
   379 fun combterm_of (Const(c,t)) =
   380     let val (tp,ts,tvar_list) = const_type_of (c,t)
   381 	val is_bool = is_bool_type t
   382 	val c' = CombConst(ResClause.make_fixed_const c,tp,tvar_list)
   383 	val c'' = if is_bool then Bool(c') else c'
   384     in
   385 	(c'',ts)
   386     end
   387   | combterm_of (Free(v,t)) =
   388     let val (tp,ts) = type_of t
   389 	val is_bool = is_bool_type t
   390 	val v' = if ResClause.isMeta v then CombVar(ResClause.make_schematic_var(v,0),tp)
   391 		 else CombFree(ResClause.make_fixed_var v,tp)
   392 	val v'' = if is_bool then Bool(v') else v'
   393     in
   394 	(v'',ts)
   395     end
   396   | combterm_of (Var(v,t)) =
   397     let val (tp,ts) = type_of t
   398 	val is_bool = is_bool_type t
   399 	val v' = CombVar(ResClause.make_schematic_var v,tp)
   400 	val v'' = if is_bool then Bool(v') else v'
   401     in
   402 	(v'',ts)
   403     end
   404   | combterm_of (t as (P $ Q)) =
   405     let val (P',tsP) = combterm_of P
   406 	val (Q',tsQ) = combterm_of Q
   407 	val tp = Term.type_of t
   408 	val tp' = simp_type_of tp
   409 	val is_bool = is_bool_type tp
   410 	val t' = CombApp(P',Q',tp')
   411 	val t'' = if is_bool then Bool(t') else t'
   412     in
   413 	(t'',tsP union tsQ)
   414     end;
   415 
   416 fun predicate_of ((Const("Not",_) $ P), polarity) =
   417     predicate_of (P, not polarity)
   418   | predicate_of (term,polarity) = (combterm_of term,polarity);
   419 
   420 
   421 fun literals_of_term1 args (Const("Trueprop",_) $ P) = literals_of_term1 args P
   422   | literals_of_term1 args (Const("op |",_) $ P $ Q) = 
   423     let val args' = literals_of_term1 args P
   424     in
   425 	literals_of_term1 args' Q
   426     end
   427   | literals_of_term1 (lits,ts) P =
   428     let val ((pred,ts'),pol) = predicate_of (P,true)
   429 	val lits' = Literal(pol,pred)::lits
   430     in
   431 	(lits',ts union ts')
   432     end;
   433 
   434 
   435 fun literals_of_term P = literals_of_term1 ([],[]) P;
   436 
   437 fun occurs a (CombVar(b,_)) = a = b
   438   | occurs a (CombApp(t1,t2,_)) = (occurs a t1) orelse (occurs a t2)
   439   | occurs _ _ = false
   440 
   441 fun too_general_terms (CombVar(v,_), t) = not (occurs v t)
   442   | too_general_terms _ = false;
   443 
   444 fun too_general_lit (Literal(true,(Bool(CombApp(CombApp(CombConst("equal",tp,tps),t1,tp1),t2,tp2))))) =
   445       too_general_terms (t1,t2) orelse too_general_terms (t2,t1)
   446   | too_general_lit _ = false;
   447 
   448 (* forbid a clause that contains hBOOL(V) *)
   449 fun too_general [] = false
   450   | too_general (lit::lits) = 
   451     case lit of Literal(_,Bool(CombVar(_,_))) => true
   452 	      | _ => too_general lits;
   453 
   454 (* making axiom and conjecture clauses *)
   455 exception MAKE_CLAUSE
   456 fun make_clause(clause_id,axiom_name,kind,thm,is_user) =
   457     let val term = prop_of thm
   458 	val term' = comb_of term is_user
   459 	val (lits,ctypes_sorts) = literals_of_term term'
   460 	val (ctvar_lits,ctfree_lits) = ResClause.add_typs_aux ctypes_sorts
   461     in
   462 	if forall isFalse lits
   463 	then error "Problem too trivial for resolution (empty clause)"
   464 	else if too_general lits 
   465 	then (Output.debug ("Omitting " ^ axiom_name ^ ": clause contains universal predicates"); 
   466 	     raise MAKE_CLAUSE)
   467 	else
   468 	    if (!typ_level <> T_FULL) andalso kind=Axiom andalso forall too_general_lit lits 
   469 	    then (Output.debug ("Omitting " ^ axiom_name ^ ": equalities are too general"); 
   470 	          raise MAKE_CLAUSE) 
   471 	else
   472 	    Clause {clause_id = clause_id, axiom_name = axiom_name, th = thm, kind = kind,
   473 		    literals = lits, ctypes_sorts = ctypes_sorts, 
   474 		    ctvar_type_literals = ctvar_lits,
   475 		    ctfree_type_literals = ctfree_lits}
   476     end;
   477 
   478 
   479 fun make_axiom_clause thm (ax_name,cls_id,is_user) = 
   480       make_clause(cls_id,ax_name,Axiom,thm,is_user);
   481  
   482 fun make_axiom_clauses [] user_lemmas = []
   483   | make_axiom_clauses ((thm,(name,id))::thms) user_lemmas =
   484     let val is_user = name mem user_lemmas
   485 	val cls = SOME (make_axiom_clause thm (name,id,is_user)) 
   486 	          handle MAKE_CLAUSE => NONE
   487 	val clss = make_axiom_clauses thms user_lemmas
   488     in
   489 	case cls of NONE => clss
   490 		  | SOME(cls') => if isTaut cls' then clss 
   491 		                  else (name,cls')::clss
   492     end;
   493 
   494 
   495 fun make_conjecture_clauses_aux _ [] = []
   496   | make_conjecture_clauses_aux n (th::ths) =
   497       make_clause(n,"conjecture",Conjecture,th,true) ::
   498       make_conjecture_clauses_aux (n+1) ths;
   499 
   500 val make_conjecture_clauses = make_conjecture_clauses_aux 0;
   501 
   502 
   503 (**********************************************************************)
   504 (* convert clause into ATP specific formats:                          *)
   505 (* TPTP used by Vampire and E                                         *)
   506 (* DFG used by SPASS                                                  *)
   507 (**********************************************************************)
   508 
   509 val type_wrapper = "typeinfo";
   510 
   511 fun wrap_type (c,t) = 
   512     case !typ_level of T_FULL => type_wrapper ^ (ResClause.paren_pack [c,t])
   513 		     | _ => c;
   514     
   515 
   516 val bool_tp = ResClause.make_fixed_type_const "bool";
   517 
   518 val app_str = "hAPP";
   519 
   520 val bool_str = "hBOOL";
   521 
   522 exception STRING_OF_COMBTERM of int;
   523 
   524 (* convert literals of clauses into strings *)
   525 fun string_of_combterm1_aux _ (CombConst(c,tp,_)) = 
   526     let val tp' = string_of_ctyp tp
   527 	val c' = if c = "equal" then "c_fequal" else c
   528     in
   529 	(wrap_type (c',tp'),tp')
   530     end
   531   | string_of_combterm1_aux _ (CombFree(v,tp)) = 
   532     let val tp' = string_of_ctyp tp
   533     in
   534 	(wrap_type (v,tp'),tp')
   535     end
   536   | string_of_combterm1_aux _ (CombVar(v,tp)) = 
   537     let val tp' = string_of_ctyp tp
   538     in
   539 	(wrap_type (v,tp'),tp')
   540     end
   541   | string_of_combterm1_aux is_pred (CombApp(t1,t2,tp)) =
   542     let val (s1,tp1) = string_of_combterm1_aux is_pred t1
   543 	val (s2,tp2) = string_of_combterm1_aux is_pred t2
   544 	val tp' = ResClause.string_of_fol_type tp
   545 	val r =	case !typ_level of T_FULL => type_wrapper ^  (ResClause.paren_pack [(app_str ^ (ResClause.paren_pack [s1,s2])),tp'])
   546 				 | T_PARTIAL => app_str ^ (ResClause.paren_pack [s1,s2,tp1])
   547 				 | T_NONE => app_str ^ (ResClause.paren_pack [s1,s2])
   548 				 | T_CONST => raise STRING_OF_COMBTERM (1) (*should not happen, if happened may be a bug*)
   549     in	(r,tp')
   550 
   551     end
   552   | string_of_combterm1_aux is_pred (Bool(CombApp(CombApp(CombConst("equal",tp,tps),t1,tp1),t2,tp2))) =
   553     if is_pred then 
   554 	let val (s1,_) = string_of_combterm1_aux false t1
   555 	    val (s2,_) = string_of_combterm1_aux false t2
   556 	in
   557 	    ("equal" ^ (ResClause.paren_pack [s1,s2]),bool_tp)
   558 	end
   559     else
   560 	let val (t,_) = string_of_combterm1_aux false (CombApp(CombApp(CombConst("equal",tp,tps),t1,tp1),t2,tp2))
   561 	in
   562 	    (t,bool_tp)
   563 	end
   564   | string_of_combterm1_aux is_pred (Bool(t)) = 
   565     let val (t',_) = string_of_combterm1_aux false t
   566 	val r = if is_pred then bool_str ^ (ResClause.paren_pack [t'])
   567 		else t'
   568     in
   569 	(r,bool_tp)
   570     end;
   571 
   572 fun string_of_combterm1 is_pred term = fst (string_of_combterm1_aux is_pred term);
   573 
   574 fun string_of_combterm2 _ (CombConst(c,tp,tvars)) = 
   575     let val tvars' = map string_of_ctyp tvars
   576 	val c' = if c = "equal" then "c_fequal" else c
   577     in
   578 	c' ^ (ResClause.paren_pack tvars')
   579     end
   580   | string_of_combterm2 _ (CombFree(v,tp)) = v
   581   | string_of_combterm2 _ (CombVar(v,tp)) = v
   582   | string_of_combterm2 is_pred (CombApp(t1,t2,tp)) =
   583     let val s1 = string_of_combterm2 is_pred t1
   584 	val s2 = string_of_combterm2 is_pred t2
   585     in
   586 	app_str ^ (ResClause.paren_pack [s1,s2])
   587     end
   588   | string_of_combterm2 is_pred (Bool(CombApp(CombApp(CombConst("equal",tp,tps),t1,tp1),t2,tp2))) =
   589     if is_pred then 
   590 	let val s1 = string_of_combterm2 false t1
   591 	    val s2 = string_of_combterm2 false t2
   592 	in
   593 	    ("equal" ^ (ResClause.paren_pack [s1,s2]))
   594 	end
   595     else
   596 	string_of_combterm2 false (CombApp(CombApp(CombConst("equal",tp,tps),t1,tp1),t2,tp2))
   597  
   598   | string_of_combterm2 is_pred (Bool(t)) = 
   599     let val t' = string_of_combterm2 false t
   600     in
   601 	if is_pred then bool_str ^ (ResClause.paren_pack [t'])
   602 	else t'
   603     end;
   604 
   605 
   606 
   607 fun string_of_combterm is_pred term = 
   608     case !typ_level of T_CONST => string_of_combterm2 is_pred term
   609 		     | _ => string_of_combterm1 is_pred term;
   610 
   611 
   612 fun string_of_clausename (cls_id,ax_name) = 
   613     ResClause.clause_prefix ^ ResClause.ascii_of ax_name ^ "_" ^ Int.toString cls_id;
   614 
   615 fun string_of_type_clsname (cls_id,ax_name,idx) = 
   616     string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
   617 
   618 
   619 (* tptp format *)
   620 
   621 fun tptp_literal (Literal(pol,pred)) =
   622     let val pred_string = string_of_combterm true pred
   623 	val pol_str = if pol then "++" else "--"
   624     in
   625 	pol_str ^ pred_string
   626     end;
   627 
   628  
   629 fun tptp_type_lits (Clause cls) = 
   630     let val lits = map tptp_literal (#literals cls)
   631 	val ctvar_lits_strs =
   632 	    case !typ_level of T_NONE => []
   633 	      | _ => map ResClause.tptp_of_typeLit (#ctvar_type_literals cls)
   634 	val ctfree_lits = 
   635 	    case !typ_level of T_NONE => []
   636 	      | _ => map ResClause.tptp_of_typeLit (#ctfree_type_literals cls)
   637     in
   638 	(ctvar_lits_strs @ lits, ctfree_lits)
   639     end; 
   640     
   641     
   642 fun clause2tptp cls =
   643     let val (lits,ctfree_lits) = tptp_type_lits cls
   644 	val cls_id = get_clause_id cls
   645 	val ax_name = get_axiomName cls
   646 	val knd = string_of_kind cls
   647 	val lits_str = ResClause.bracket_pack lits
   648 	val cls_str = ResClause.gen_tptp_cls(cls_id,ax_name,knd,lits_str)
   649     in
   650 	(cls_str,ctfree_lits)
   651     end;
   652 
   653 
   654 (* dfg format *)
   655 fun dfg_literal (Literal(pol,pred)) = ResClause.dfg_sign pol (string_of_combterm true pred);
   656 
   657 fun dfg_clause_aux (Clause{literals, ctypes_sorts, ...}) = 
   658   let val lits = map dfg_literal literals
   659       val (tvar_lits,tfree_lits) = ResClause.add_typs_aux ctypes_sorts
   660       val tvar_lits_strs = 
   661 	  case !typ_level of T_NONE => [] 
   662 	      | _ => map ResClause.dfg_of_typeLit tvar_lits
   663       val tfree_lits =
   664           case !typ_level of T_NONE => []
   665 	      | _ => map ResClause.dfg_of_typeLit tfree_lits 
   666   in
   667       (tvar_lits_strs @ lits, tfree_lits)
   668   end; 
   669 
   670 fun get_uvars (CombConst(_,_,_)) vars = vars
   671   | get_uvars (CombFree(_,_)) vars = vars
   672   | get_uvars (CombVar(v,tp)) vars = (v::vars)
   673   | get_uvars (CombApp(P,Q,tp)) vars = get_uvars P (get_uvars Q vars)
   674   | get_uvars (Bool(c)) vars = get_uvars c vars;
   675 
   676 
   677 fun get_uvars_l (Literal(_,c)) = get_uvars c [];
   678 
   679 fun dfg_vars (Clause {literals,...}) = ResClause.union_all (map get_uvars_l literals);
   680  
   681 fun clause2dfg (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   682     let val (lits,tfree_lits) = dfg_clause_aux cls 
   683         val vars = dfg_vars cls
   684         val tvars = ResClause.get_tvar_strs ctypes_sorts
   685 	val knd = name_of_kind kind
   686 	val lits_str = commas lits
   687 	val cls_str = ResClause.gen_dfg_cls(clause_id, axiom_name, knd, lits_str, tvars@vars) 
   688     in (cls_str, tfree_lits) end;
   689 
   690 
   691 fun init_funcs_tab funcs = 
   692     let val tp = !typ_level
   693 	val funcs1 = case tp of T_PARTIAL => Symtab.update ("hAPP",3) funcs
   694 				      | _ => Symtab.update ("hAPP",2) funcs
   695 	val funcs2 = case tp of T_FULL => Symtab.update ("typeinfo",2) funcs1
   696 				      | _ => funcs1
   697     in
   698 	funcs2
   699     end;
   700 
   701 
   702 fun add_funcs (CombConst(c,_,tvars),funcs) =
   703     if c = "equal" then foldl ResClause.add_foltype_funcs funcs tvars
   704     else
   705 	(case !typ_level of T_CONST => foldl ResClause.add_foltype_funcs (Symtab.update(c,length tvars) funcs) tvars
   706 			  | _ => foldl ResClause.add_foltype_funcs (Symtab.update(c,0) funcs) tvars)
   707   | add_funcs (CombFree(v,ctp),funcs) = ResClause.add_foltype_funcs (ctp,Symtab.update (v,0) funcs) 
   708   | add_funcs (CombVar(_,ctp),funcs) = ResClause.add_foltype_funcs (ctp,funcs)
   709   | add_funcs (CombApp(P,Q,_),funcs) = add_funcs(P,add_funcs (Q,funcs))
   710   | add_funcs (Bool(t),funcs) = add_funcs (t,funcs);
   711 
   712 
   713 fun add_literal_funcs (Literal(_,c), funcs) = add_funcs (c,funcs);
   714 
   715 fun add_clause_funcs (Clause {literals, ...}, funcs) =
   716     foldl add_literal_funcs funcs literals
   717     handle Symtab.DUP a => raise ERROR ("function " ^ a ^ " has multiple arities")
   718 
   719 fun funcs_of_clauses clauses arity_clauses =
   720     Symtab.dest (foldl ResClause.add_arityClause_funcs 
   721                        (foldl add_clause_funcs (init_funcs_tab Symtab.empty) clauses)
   722                        arity_clauses)
   723 
   724 fun preds_of clsrel_clauses arity_clauses = 
   725     Symtab.dest
   726 	(foldl ResClause.add_classrelClause_preds 
   727 	       (foldl ResClause.add_arityClause_preds
   728 		      (Symtab.update ("hBOOL",1) Symtab.empty)
   729 		      arity_clauses)
   730 	       clsrel_clauses)
   731 
   732 
   733 (**********************************************************************)
   734 (* write clauses to files                                             *)
   735 (**********************************************************************)
   736 
   737 local
   738 
   739 val cnf_helper_thms = ResAxioms.cnf_rules_pairs o (map ResAxioms.pairname)
   740 
   741 in
   742 
   743 fun get_helper_clauses () =
   744     let val IK = if !combI_count > 0 orelse !combK_count > 0 then (Output.debug "Include combinator I K"; cnf_helper_thms [comb_I,comb_K]) else []
   745 	val BC = if !combB_count > 0 orelse !combC_count > 0 then (Output.debug "Include combinator B C"; cnf_helper_thms [comb_B,comb_C]) else []
   746 	val S = if !combS_count > 0 then (Output.debug "Include combinator S"; cnf_helper_thms [comb_S]) else []
   747 	val B'C' = if !combB'_count > 0 orelse !combC'_count > 0 then (Output.debug "Include combinator B' C'"; cnf_helper_thms [comb_B', comb_C']) else []
   748 	val S' = if !combS'_count > 0 then (Output.debug "Include combinator S'"; cnf_helper_thms [comb_S']) else []
   749 	val other = cnf_helper_thms [ext,fequal_imp_equal,equal_imp_fequal]
   750     in
   751 	make_axiom_clauses (other @ IK @ BC @ S @ B'C' @ S') []
   752     end
   753 
   754 end
   755 
   756 (* tptp format *)
   757 						  
   758 (* write TPTP format to a single file *)
   759 (* when "get_helper_clauses" is called, "include_combS" and "include_min_comb" should have correct values already *)
   760 fun tptp_write_file thms filename (axclauses,classrel_clauses,arity_clauses) user_lemmas=
   761     let val clss = make_conjecture_clauses thms
   762         val (clnames,axclauses') = ListPair.unzip (make_axiom_clauses axclauses user_lemmas)
   763 	val (tptp_clss,tfree_litss) = ListPair.unzip (map clause2tptp clss)
   764 	val tfree_clss = map ResClause.tptp_tfree_clause (foldl (op union_string) [] tfree_litss)
   765 	val out = TextIO.openOut filename
   766 	val helper_clauses = (#2 o ListPair.unzip o get_helper_clauses) ()
   767     in
   768 	List.app (curry TextIO.output out o #1 o clause2tptp) axclauses';
   769 	ResClause.writeln_strs out tfree_clss;
   770 	ResClause.writeln_strs out tptp_clss;
   771 	List.app (curry TextIO.output out o ResClause.tptp_classrelClause) classrel_clauses;
   772 	List.app (curry TextIO.output out o ResClause.tptp_arity_clause) arity_clauses;
   773 	List.app (curry TextIO.output out o #1 o clause2tptp) helper_clauses;
   774 	TextIO.closeOut out;
   775 	clnames
   776     end;
   777 
   778 
   779 
   780 (* dfg format *)
   781 
   782 fun dfg_write_file  thms filename (axclauses,classrel_clauses,arity_clauses) user_lemmas =
   783     let val _ = Output.debug ("Preparing to write the DFG file " ^ filename) 
   784 	val conjectures = make_conjecture_clauses thms
   785         val (clnames,axclauses') = ListPair.unzip (make_axiom_clauses axclauses user_lemmas)
   786 	val (dfg_clss,tfree_litss) = ListPair.unzip (map clause2dfg conjectures)
   787 	and probname = Path.pack (Path.base (Path.unpack filename))
   788 	val (axstrs,_) =  ListPair.unzip (map clause2dfg axclauses')
   789 	val tfree_clss = map ResClause.dfg_tfree_clause (ResClause.union_all tfree_litss)
   790 	val out = TextIO.openOut filename
   791 	val helper_clauses = (#2 o ListPair.unzip o get_helper_clauses) ()
   792 	val helper_clauses_strs = (#1 o ListPair.unzip o (map clause2dfg)) helper_clauses
   793 	val funcs = funcs_of_clauses (helper_clauses @ conjectures @ axclauses') arity_clauses
   794 	and preds = preds_of classrel_clauses arity_clauses
   795     in
   796 	TextIO.output (out, ResClause.string_of_start probname); 
   797 	TextIO.output (out, ResClause.string_of_descrip probname); 
   798 	TextIO.output (out, ResClause.string_of_symbols (ResClause.string_of_funcs funcs) (ResClause.string_of_preds preds)); 
   799 	TextIO.output (out, "list_of_clauses(axioms,cnf).\n");
   800 	ResClause.writeln_strs out axstrs;
   801 	List.app (curry TextIO.output out o ResClause.dfg_classrelClause) classrel_clauses;
   802 	List.app (curry TextIO.output out o ResClause.dfg_arity_clause) arity_clauses;
   803 	ResClause.writeln_strs out helper_clauses_strs;
   804 	TextIO.output (out, "end_of_list.\n\nlist_of_clauses(conjectures,cnf).\n");
   805 	ResClause.writeln_strs out tfree_clss;
   806 	ResClause.writeln_strs out dfg_clss;
   807 	TextIO.output (out, "end_of_list.\n\n");
   808 	(*VarWeight=3 helps the HO problems, probably by counteracting the presence of hAPP*)
   809 	TextIO.output (out, "list_of_settings(SPASS).\n{*\nset_flag(VarWeight,3).\n*}\nend_of_list.\n\n");
   810 	TextIO.output (out, "end_problem.\n");
   811 	TextIO.closeOut out;
   812 	clnames
   813     end;
   814 
   815 end