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