src/HOL/Tools/res_hol_clause.ML
author paulson
Mon Nov 27 18:18:25 2006 +0100 (2006-11-27)
changeset 21561 cfd2258f0b23
parent 21513 9e9fff87dc6c
child 21573 8a7a68c0096c
permissions -rw-r--r--
Tidied code. Bool constructor is not needed.
     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 structure ResHolClause =
     8 
     9 struct
    10 
    11 (* theorems for combinators and function extensionality *)
    12 val ext = thm "HOL.ext";
    13 val comb_I = thm "ATP_Linkup.COMBI_def";
    14 val comb_K = thm "ATP_Linkup.COMBK_def";
    15 val comb_B = thm "ATP_Linkup.COMBB_def";
    16 val comb_C = thm "ATP_Linkup.COMBC_def";
    17 val comb_S = thm "ATP_Linkup.COMBS_def";
    18 val comb_B' = thm "ATP_Linkup.COMBB'_def";
    19 val comb_C' = thm "ATP_Linkup.COMBC'_def";
    20 val comb_S' = thm "ATP_Linkup.COMBS'_def";
    21 val fequal_imp_equal = thm "ATP_Linkup.fequal_imp_equal";
    22 val equal_imp_fequal = thm "ATP_Linkup.equal_imp_fequal";
    23 
    24 (*A flag to set if we use the Turner optimizations. Currently FALSE, as the 5 standard
    25   combinators appear to work best.*)
    26 val use_Turner = ref false;
    27 
    28 (*FIXME: these refs should probaby replaced by code to count the combinators in the 
    29   translated form of the term.*)
    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 (*******************************************)
    83 fun mk_compact_comb (tm as (Const("ATP_Linkup.COMBB",_)$p) $ (Const("ATP_Linkup.COMBB",_)$q$r)) Bnds count_comb =
    84     let val tp_p = Term.type_of1(Bnds,p)
    85 	val tp_q = Term.type_of1(Bnds,q)
    86 	val tp_r = Term.type_of1(Bnds,r)
    87 	val typ = Term.type_of1(Bnds,tm)
    88 	val typ_B' = [tp_p,tp_q,tp_r] ---> typ
    89 	val _ = increB' count_comb
    90 	val _ = decreB count_comb 2
    91     in
    92 	Const("ATP_Linkup.COMBB'",typ_B') $ p $ q $ r
    93     end
    94   | mk_compact_comb (tm as (Const("ATP_Linkup.COMBC",_) $ (Const("ATP_Linkup.COMBB",_)$p$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_C' = [tp_p,tp_q,tp_r] ---> typ
   100 	val _ = increC' count_comb
   101 	val _ = decreC count_comb 1
   102 	val _ = decreB count_comb 1
   103     in
   104 	Const("ATP_Linkup.COMBC'",typ_C') $ p $ q $ r
   105     end
   106   | mk_compact_comb (tm as (Const("ATP_Linkup.COMBS",_) $ (Const("ATP_Linkup.COMBB",_)$p$q) $ r)) Bnds count_comb =
   107     let val tp_p = Term.type_of1(Bnds,p)
   108 	val tp_q = Term.type_of1(Bnds,q)
   109 	val tp_r = Term.type_of1(Bnds,r)
   110 	val typ = Term.type_of1(Bnds,tm)
   111 	val typ_S' = [tp_p,tp_q,tp_r] ---> typ
   112 	val _ = increS' count_comb
   113 	val _ = decreS count_comb 1
   114 	val _ = decreB count_comb 1
   115     in
   116 	Const("ATP_Linkup.COMBS'",typ_S') $ p $ q $ r
   117     end
   118   | mk_compact_comb tm _ _ = tm;
   119 
   120 fun compact_comb t Bnds count_comb = 
   121   if !use_Turner then mk_compact_comb t Bnds count_comb else t;
   122 
   123 fun lam2comb (Abs(x,tp,Bound 0)) _ count_comb = 
   124       let val _ = increI count_comb
   125       in 
   126 	  Const("ATP_Linkup.COMBI",tp-->tp) 
   127       end
   128   | lam2comb (Abs(x,tp,Bound n)) Bnds count_comb = 
   129       let val tb = List.nth(Bnds,n-1)
   130 	  val _ = increK count_comb 
   131       in
   132 	  Const("ATP_Linkup.COMBK", [tb,tp] ---> tb) $ (Bound (n-1))
   133       end
   134   | lam2comb (Abs(x,t1,Const(c,t2))) _ count_comb = 
   135       let val _ = increK count_comb 
   136       in 
   137 	  Const("ATP_Linkup.COMBK",[t2,t1] ---> t2) $ Const(c,t2) 
   138       end
   139   | lam2comb (Abs(x,t1,Free(v,t2))) _ count_comb =
   140       let val _ = increK count_comb
   141       in
   142 	  Const("ATP_Linkup.COMBK",[t2,t1] ---> t2) $ Free(v,t2)
   143       end
   144   | lam2comb (Abs(x,t1,Var(ind,t2))) _ count_comb =
   145       let val _ = increK count_comb 
   146       in
   147 	  Const("ATP_Linkup.COMBK", [t2,t1] ---> t2) $ Var(ind,t2)
   148       end
   149   | lam2comb (t as (Abs(x,t1,P$(Bound 0)))) Bnds count_comb =
   150       let val tr = Term.type_of1(t1::Bnds,P)
   151       in
   152 	  if not(is_free P 0) then (incr_boundvars ~1 P)
   153 	  else 
   154 	  let val tI = [t1] ---> t1
   155 	      val P' = lam2comb (Abs(x,t1,P)) Bnds count_comb
   156 	      val tp' = Term.type_of1(Bnds,P')
   157 	      val tS = [tp',tI] ---> tr
   158 	      val _ = increI count_comb
   159 	      val _ = increS count_comb
   160 	  in
   161 	      compact_comb (Const("ATP_Linkup.COMBS",tS) $ P' $ 
   162 	                     Const("ATP_Linkup.COMBI",tI)) Bnds count_comb
   163 	  end
   164       end	    
   165   | lam2comb (t as (Abs(x,t1,P$Q))) Bnds count_comb =
   166       let val nfreeP = not(is_free P 0)
   167 	  and nfreeQ = not(is_free Q 0)
   168 	  val tpq = Term.type_of1(t1::Bnds, P$Q) 
   169       in
   170 	  if nfreeP andalso nfreeQ 
   171 	  then 
   172 	    let val tK = [tpq,t1] ---> tpq
   173 		val PQ' = incr_boundvars ~1(P $ Q)
   174 		val _ = increK count_comb
   175 	    in 
   176 		Const("ATP_Linkup.COMBK",tK) $ PQ'
   177 	    end
   178 	  else if nfreeP then 
   179 	    let val Q' = lam2comb (Abs(x,t1,Q)) Bnds count_comb
   180 		val P' = incr_boundvars ~1 P
   181 		val tp = Term.type_of1(Bnds,P')
   182 		val tq' = Term.type_of1(Bnds, Q')
   183 		val tB = [tp,tq',t1] ---> tpq
   184 		val _ = increB count_comb
   185 	    in
   186 		compact_comb (Const("ATP_Linkup.COMBB",tB) $ P' $ Q') Bnds count_comb 
   187 	    end
   188 	  else if nfreeQ then 
   189 	    let val P' = lam2comb (Abs(x,t1,P)) Bnds count_comb
   190 		val Q' = incr_boundvars ~1 Q
   191 		val tq = Term.type_of1(Bnds,Q')
   192 		val tp' = Term.type_of1(Bnds, P')
   193 		val tC = [tp',tq,t1] ---> tpq
   194 		val _ = increC count_comb
   195 	    in
   196 		compact_comb (Const("ATP_Linkup.COMBC",tC) $ P' $ Q') Bnds count_comb
   197 	    end
   198           else
   199 	    let val P' = lam2comb (Abs(x,t1,P)) Bnds count_comb
   200 		val Q' = lam2comb (Abs(x,t1,Q)) Bnds count_comb 
   201 		val tp' = Term.type_of1(Bnds,P')
   202 		val tq' = Term.type_of1(Bnds,Q')
   203 		val tS = [tp',tq',t1] ---> tpq
   204 		val _ = increS count_comb
   205 	    in
   206 		compact_comb (Const("ATP_Linkup.COMBS",tS) $ P' $ Q') Bnds count_comb
   207 	    end
   208       end
   209   | lam2comb (t as (Abs(x,t1,_))) _ _ = raise ResClause.CLAUSE("HOL CLAUSE",t);
   210 
   211 (*********************)
   212 
   213 fun to_comb (Abs(x,tp,b)) Bnds count_comb =
   214     let val b' = to_comb b (tp::Bnds) count_comb
   215     in lam2comb (Abs(x,tp,b')) Bnds count_comb end
   216   | to_comb (P $ Q) Bnds count_comb = ((to_comb P Bnds count_comb) $ (to_comb Q Bnds count_comb))
   217   | to_comb t _ _ = t;
   218  
   219    
   220 fun comb_of t count_comb = to_comb t [] count_comb;
   221  
   222 (* print a term containing combinators, used for debugging *)
   223 exception TERM_COMB of term;
   224 
   225 fun string_of_term (Const(c,t)) = c
   226   | string_of_term (Free(v,t)) = v
   227   | string_of_term (Var((x,n),t)) = x ^ "_" ^ (string_of_int n)
   228   | string_of_term (P $ Q) =
   229       "(" ^ string_of_term P ^ " " ^ string_of_term Q ^ ")" 
   230   | string_of_term t =  raise TERM_COMB (t);
   231 
   232 
   233 
   234 (******************************************************)
   235 (* data types for typed combinator expressions        *)
   236 (******************************************************)
   237 
   238 datatype type_level = T_FULL | T_PARTIAL | T_CONST | T_NONE;
   239 
   240 val typ_level = ref T_CONST;
   241 
   242 fun full_types () = (typ_level:=T_FULL);
   243 fun partial_types () = (typ_level:=T_PARTIAL);
   244 fun const_types_only () = (typ_level:=T_CONST);
   245 fun no_types () = (typ_level:=T_NONE);
   246 
   247 fun find_typ_level () = !typ_level;
   248 
   249 type axiom_name = string;
   250 type polarity = bool;
   251 type clause_id = int;
   252 
   253 datatype combterm = CombConst of string * ResClause.fol_type * ResClause.fol_type list
   254 		  | CombFree of string * ResClause.fol_type
   255 		  | CombVar of string * ResClause.fol_type
   256 		  | CombApp of combterm * combterm * ResClause.fol_type
   257 		  
   258 datatype literal = Literal of polarity * combterm;
   259 
   260 datatype clause = 
   261 	 Clause of {clause_id: clause_id,
   262 		    axiom_name: axiom_name,
   263 		    th: thm,
   264 		    kind: ResClause.kind,
   265 		    literals: literal list,
   266 		    ctypes_sorts: (ResClause.typ_var * Term.sort) list, 
   267                     ctvar_type_literals: ResClause.type_literal list, 
   268                     ctfree_type_literals: ResClause.type_literal list};
   269 
   270 
   271 (*********************************************************************)
   272 (* convert a clause with type Term.term to a clause with type clause *)
   273 (*********************************************************************)
   274 
   275 fun isFalse (Literal(pol, CombConst(c,_,_))) =
   276       (pol andalso c = "c_False") orelse
   277       (not pol andalso c = "c_True")
   278   | isFalse _ = false;
   279 
   280 fun isTrue (Literal (pol, CombConst(c,_,_))) =
   281       (pol andalso c = "c_True") orelse
   282       (not pol andalso c = "c_False")
   283   | isTrue _ = false;
   284   
   285 fun isTaut (Clause {literals,...}) = exists isTrue literals;  
   286 
   287 fun type_of (Type (a, Ts)) =
   288       let val (folTypes,ts) = types_of Ts
   289 	  val t = ResClause.make_fixed_type_const a
   290       in
   291 	  (ResClause.mk_fol_type("Comp",t,folTypes),ts)
   292       end
   293   | type_of (tp as (TFree(a,s))) =
   294       let val t = ResClause.make_fixed_type_var a
   295       in
   296 	  (ResClause.mk_fol_type("Fixed",t,[]),[ResClause.mk_typ_var_sort tp])
   297       end
   298   | type_of (tp as (TVar(v,s))) =
   299       let val t = ResClause.make_schematic_type_var v
   300       in
   301 	  (ResClause.mk_fol_type("Var",t,[]),[ResClause.mk_typ_var_sort tp])
   302       end
   303 and types_of Ts =
   304       let val foltyps_ts = map type_of Ts
   305 	  val (folTyps,ts) = ListPair.unzip foltyps_ts
   306       in
   307 	  (folTyps,ResClause.union_all ts)
   308       end;
   309 
   310 (* same as above, but no gathering of sort information *)
   311 fun simp_type_of (Type (a, Ts)) = 
   312       let val typs = map simp_type_of Ts
   313 	  val t = ResClause.make_fixed_type_const a
   314       in
   315 	  ResClause.mk_fol_type("Comp",t,typs)
   316       end
   317   | simp_type_of (TFree (a,s)) = ResClause.mk_fol_type("Fixed",ResClause.make_fixed_type_var a,[])
   318   | simp_type_of (TVar (v,s)) = ResClause.mk_fol_type("Var",ResClause.make_schematic_type_var v,[]);
   319 
   320 
   321 fun const_type_of (c,t) =
   322     let val (tp,ts) = type_of t
   323 	val tvars = !const_typargs(c,t)
   324     in
   325 	(tp, ts, map simp_type_of tvars)
   326     end;
   327 
   328 (* convert a Term.term (with combinators) into a combterm, also accummulate sort info *)
   329 fun combterm_of (Const(c,t)) =
   330       let val (tp,ts,tvar_list) = const_type_of (c,t)
   331 	  val c' = CombConst(ResClause.make_fixed_const c,tp,tvar_list)
   332       in
   333 	  (c',ts)
   334       end
   335   | combterm_of (Free(v,t)) =
   336       let val (tp,ts) = type_of t
   337 	  val v' = if ResClause.isMeta v then CombVar(ResClause.make_schematic_var(v,0),tp)
   338 		   else CombFree(ResClause.make_fixed_var v,tp)
   339       in
   340 	  (v',ts)
   341       end
   342   | combterm_of (Var(v,t)) =
   343       let val (tp,ts) = type_of t
   344 	  val v' = CombVar(ResClause.make_schematic_var v,tp)
   345       in
   346 	  (v',ts)
   347       end
   348   | combterm_of (t as (P $ Q)) =
   349       let val (P',tsP) = combterm_of P
   350 	  val (Q',tsQ) = combterm_of Q
   351 	  val tp = Term.type_of t
   352 	  val t' = CombApp(P',Q', simp_type_of tp)
   353       in
   354 	  (t',tsP union tsQ)
   355       end;
   356 
   357 fun predicate_of ((Const("Not",_) $ P), polarity) =
   358     predicate_of (P, not polarity)
   359   | predicate_of (term,polarity) = (combterm_of term,polarity);
   360 
   361 
   362 fun literals_of_term1 args (Const("Trueprop",_) $ P) = literals_of_term1 args P
   363   | literals_of_term1 args (Const("op |",_) $ P $ Q) = 
   364       literals_of_term1 (literals_of_term1 args P) Q
   365   | literals_of_term1 (lits,ts) P =
   366       let val ((pred,ts'),pol) = predicate_of (P,true)
   367       in
   368 	  (Literal(pol,pred)::lits, ts union ts')
   369       end;
   370 
   371 fun literals_of_term P = literals_of_term1 ([],[]) P;
   372 
   373 (* making axiom and conjecture clauses *)
   374 exception MAKE_CLAUSE
   375 fun make_clause(clause_id,axiom_name,kind,th,is_user) =
   376     let val (lits,ctypes_sorts) = literals_of_term (comb_of (prop_of th) is_user)
   377 	val (ctvar_lits,ctfree_lits) = ResClause.add_typs_aux ctypes_sorts
   378     in
   379 	if forall isFalse lits
   380 	then error "Problem too trivial for resolution (empty clause)"
   381 	else
   382 	    Clause {clause_id = clause_id, axiom_name = axiom_name, th = th, kind = kind,
   383 		    literals = lits, ctypes_sorts = ctypes_sorts, 
   384 		    ctvar_type_literals = ctvar_lits,
   385 		    ctfree_type_literals = ctfree_lits}
   386     end;
   387 
   388 
   389 fun make_axiom_clauses [] user_lemmas = []
   390   | make_axiom_clauses ((th,(name,id))::ths) user_lemmas =
   391     let val is_user = name mem user_lemmas
   392 	val cls = SOME (make_clause(id, name, ResClause.Axiom, th, is_user)) 
   393 	          handle MAKE_CLAUSE => NONE
   394 	val clss = make_axiom_clauses ths user_lemmas
   395     in
   396 	case cls of NONE => clss
   397 		  | SOME(cls') => if isTaut cls' then clss 
   398 		                  else (name,cls')::clss
   399     end;
   400 
   401 
   402 fun make_conjecture_clauses_aux _ [] = []
   403   | make_conjecture_clauses_aux n (th::ths) =
   404       make_clause(n,"conjecture", ResClause.Conjecture, th, true) ::
   405       make_conjecture_clauses_aux (n+1) ths;
   406 
   407 val make_conjecture_clauses = make_conjecture_clauses_aux 0;
   408 
   409 
   410 (**********************************************************************)
   411 (* convert clause into ATP specific formats:                          *)
   412 (* TPTP used by Vampire and E                                         *)
   413 (* DFG used by SPASS                                                  *)
   414 (**********************************************************************)
   415 
   416 val type_wrapper = "typeinfo";
   417 
   418 fun wrap_type (c,tp) = case !typ_level of
   419 	T_FULL => type_wrapper ^ ResClause.paren_pack [c, ResClause.string_of_fol_type tp]
   420       | _ => c;
   421     
   422 
   423 val bool_tp = ResClause.make_fixed_type_const "bool";
   424 
   425 val app_str = "hAPP";
   426 
   427 val bool_str = "hBOOL";
   428 
   429 fun type_of_combterm (CombConst(c,tp,_)) = tp
   430   | type_of_combterm (CombFree(v,tp)) = tp
   431   | type_of_combterm (CombVar(v,tp)) = tp
   432   | type_of_combterm (CombApp(t1,t2,tp)) = tp;
   433 
   434 fun string_of_combterm1 (CombConst(c,tp,_)) = 
   435       let val c' = if c = "equal" then "c_fequal" else c
   436       in  wrap_type (c',tp)  end
   437   | string_of_combterm1 (CombFree(v,tp)) = wrap_type (v,tp)
   438   | string_of_combterm1 (CombVar(v,tp)) = wrap_type (v,tp)
   439   | string_of_combterm1 (CombApp(t1,t2,tp)) =
   440       let val s1 = string_of_combterm1 t1
   441 	  val s2 = string_of_combterm1 t2
   442       in
   443 	  case !typ_level of
   444 	      T_FULL => type_wrapper ^ 
   445 	                ResClause.paren_pack 
   446 	                  [app_str ^ ResClause.paren_pack [s1,s2], 
   447 	                   ResClause.string_of_fol_type tp]
   448 	    | T_PARTIAL => app_str ^ ResClause.paren_pack 
   449 	                     [s1,s2, ResClause.string_of_fol_type (type_of_combterm t1)]
   450 	    | T_NONE => app_str ^ ResClause.paren_pack [s1,s2]
   451 	    | T_CONST => raise ERROR "string_of_combterm1"
   452       end;
   453 
   454 fun string_of_combterm2 (CombConst(c,tp,tvars)) = 
   455       let val tvars' = map ResClause.string_of_fol_type tvars
   456 	  val c' = if c = "equal" then "c_fequal" else c
   457       in
   458 	  c' ^ ResClause.paren_pack tvars'
   459       end
   460   | string_of_combterm2 (CombFree(v,tp)) = v
   461   | string_of_combterm2 (CombVar(v,tp)) = v
   462   | string_of_combterm2 (CombApp(t1,t2,_)) =
   463       app_str ^ ResClause.paren_pack [string_of_combterm2 t1, string_of_combterm2 t2];
   464 
   465 fun string_of_combterm t = 
   466     case !typ_level of T_CONST => string_of_combterm2 t
   467 		           | _ => string_of_combterm1 t;
   468 		           
   469 fun string_of_predicate (CombApp(CombApp(CombConst("equal",_,_),t1,_),t2,_)) =
   470       ("equal" ^ ResClause.paren_pack [string_of_combterm t1, string_of_combterm t2])
   471   | string_of_predicate t = 
   472       bool_str ^ ResClause.paren_pack [string_of_combterm t]
   473 
   474 fun string_of_clausename (cls_id,ax_name) = 
   475     ResClause.clause_prefix ^ ResClause.ascii_of ax_name ^ "_" ^ Int.toString cls_id;
   476 
   477 fun string_of_type_clsname (cls_id,ax_name,idx) = 
   478     string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
   479 
   480 
   481 (*** tptp format ***)
   482 
   483 fun tptp_of_equality pol (t1,t2) =
   484   let val eqop = if pol then " = " else " != "
   485   in  string_of_combterm t1 ^ eqop ^ string_of_combterm t2  end;
   486 
   487 fun tptp_literal (Literal(pol, CombApp(CombApp(CombConst("equal",_,_),t1,_),t2,_))) = 
   488       tptp_of_equality pol (t1,t2)
   489   | tptp_literal (Literal(pol,pred)) = 
   490       ResClause.tptp_sign pol (string_of_predicate pred);
   491  
   492 fun tptp_type_lits (Clause cls) = 
   493     let val lits = map tptp_literal (#literals cls)
   494 	val ctvar_lits_strs =
   495 	    case !typ_level of T_NONE => []
   496 	      | _ => map ResClause.tptp_of_typeLit (#ctvar_type_literals cls)
   497 	val ctfree_lits = 
   498 	    case !typ_level of T_NONE => []
   499 	      | _ => map ResClause.tptp_of_typeLit (#ctfree_type_literals cls)
   500     in
   501 	(ctvar_lits_strs @ lits, ctfree_lits)
   502     end; 
   503     
   504     
   505 fun clause2tptp (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   506     let val (lits,ctfree_lits) = tptp_type_lits cls
   507 	val cls_str = ResClause.gen_tptp_cls(clause_id,axiom_name,kind,lits)
   508     in
   509 	(cls_str,ctfree_lits)
   510     end;
   511 
   512 
   513 (*** dfg format ***)
   514 
   515 fun dfg_literal (Literal(pol,pred)) = ResClause.dfg_sign pol (string_of_predicate pred);
   516 
   517 fun dfg_clause_aux (Clause{literals, ctypes_sorts, ...}) = 
   518   let val lits = map dfg_literal literals
   519       val (tvar_lits,tfree_lits) = ResClause.add_typs_aux ctypes_sorts
   520       val tvar_lits_strs = 
   521 	  case !typ_level of T_NONE => [] 
   522 	      | _ => map ResClause.dfg_of_typeLit tvar_lits
   523       val tfree_lits =
   524           case !typ_level of T_NONE => []
   525 	      | _ => map ResClause.dfg_of_typeLit tfree_lits 
   526   in
   527       (tvar_lits_strs @ lits, tfree_lits)
   528   end; 
   529 
   530 fun get_uvars (CombConst(_,_,_)) vars = vars
   531   | get_uvars (CombFree(_,_)) vars = vars
   532   | get_uvars (CombVar(v,tp)) vars = (v::vars)
   533   | get_uvars (CombApp(P,Q,tp)) vars = get_uvars P (get_uvars Q vars);
   534 
   535 fun get_uvars_l (Literal(_,c)) = get_uvars c [];
   536 
   537 fun dfg_vars (Clause {literals,...}) = ResClause.union_all (map get_uvars_l literals);
   538  
   539 fun clause2dfg (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   540     let val (lits,tfree_lits) = dfg_clause_aux cls 
   541         val vars = dfg_vars cls
   542         val tvars = ResClause.get_tvar_strs ctypes_sorts
   543 	val knd = ResClause.name_of_kind kind
   544 	val lits_str = commas lits
   545 	val cls_str = ResClause.gen_dfg_cls(clause_id, axiom_name, knd, lits_str, tvars@vars) 
   546     in (cls_str, tfree_lits) end;
   547 
   548 
   549 fun init_funcs_tab funcs = 
   550     let val funcs1 = case !typ_level of T_PARTIAL => Symtab.update ("hAPP",3) funcs
   551 				      | _ => Symtab.update ("hAPP",2) funcs
   552     in
   553 	Symtab.update ("typeinfo",2) funcs1
   554     end;
   555 
   556 
   557 fun add_funcs (CombConst(c,_,tvars),funcs) =
   558       if c = "equal" then foldl ResClause.add_foltype_funcs funcs tvars
   559       else
   560 	(case !typ_level of
   561 	     T_CONST => foldl ResClause.add_foltype_funcs (Symtab.update(c,length tvars) funcs) tvars
   562            | _ => foldl ResClause.add_foltype_funcs (Symtab.update(c,0) funcs) tvars)
   563   | add_funcs (CombFree(v,ctp),funcs) = ResClause.add_foltype_funcs (ctp,Symtab.update (v,0) funcs) 
   564   | add_funcs (CombVar(_,ctp),funcs) = ResClause.add_foltype_funcs (ctp,funcs)
   565   | add_funcs (CombApp(P,Q,_),funcs) = add_funcs(P,add_funcs (Q,funcs));
   566 
   567 fun add_literal_funcs (Literal(_,c), funcs) = add_funcs (c,funcs);
   568 
   569 fun add_clause_funcs (Clause {literals, ...}, funcs) =
   570     foldl add_literal_funcs funcs literals
   571     handle Symtab.DUP a => raise ERROR ("function " ^ a ^ " has multiple arities")
   572 
   573 fun funcs_of_clauses clauses arity_clauses =
   574     Symtab.dest (foldl ResClause.add_arityClause_funcs 
   575                        (foldl add_clause_funcs (init_funcs_tab Symtab.empty) clauses)
   576                        arity_clauses)
   577 
   578 fun add_clause_preds (Clause {ctypes_sorts, ...}, preds) =
   579   foldl ResClause.add_type_sort_preds preds ctypes_sorts
   580   handle Symtab.DUP a => raise ERROR ("predicate " ^ a ^ " has multiple arities")
   581 
   582 (*Higher-order clauses have only the predicates hBOOL and type classes.*)
   583 fun preds_of_clauses clauses clsrel_clauses arity_clauses = 
   584     Symtab.dest
   585 	(foldl ResClause.add_classrelClause_preds 
   586 	       (foldl ResClause.add_arityClause_preds
   587 		      (Symtab.update ("hBOOL",1) 
   588 		        (foldl add_clause_preds Symtab.empty clauses))
   589 		      arity_clauses)
   590 	       clsrel_clauses)
   591 
   592 
   593 
   594 (**********************************************************************)
   595 (* write clauses to files                                             *)
   596 (**********************************************************************)
   597 
   598 val cnf_helper_thms = ResAxioms.cnf_rules_pairs o (map ResAxioms.pairname)
   599 
   600 fun get_helper_clauses () =
   601     let val IK = if !combI_count > 0 orelse !combK_count > 0 
   602                  then (Output.debug "Include combinator I K"; cnf_helper_thms [comb_I,comb_K]) 
   603                  else []
   604 	val BC = if !combB_count > 0 orelse !combC_count > 0 
   605 	         then (Output.debug "Include combinator B C"; cnf_helper_thms [comb_B,comb_C]) 
   606 	         else []
   607 	val S = if !combS_count > 0 
   608 	        then (Output.debug "Include combinator S"; cnf_helper_thms [comb_S]) 
   609 	        else []
   610 	val B'C' = if !combB'_count > 0 orelse !combC'_count > 0 
   611 	           then (Output.debug "Include combinator B' C'"; cnf_helper_thms [comb_B', comb_C']) 
   612 	           else []
   613 	val S' = if !combS'_count > 0 
   614 	         then (Output.debug "Include combinator S'"; cnf_helper_thms [comb_S']) 
   615 	         else []
   616 	val other = cnf_helper_thms [ext,fequal_imp_equal,equal_imp_fequal]
   617     in
   618 	make_axiom_clauses (other @ IK @ BC @ S @ B'C' @ S') []
   619     end
   620 
   621 
   622 (* tptp format *)
   623 						  
   624 (* write TPTP format to a single file *)
   625 (* when "get_helper_clauses" is called, "include_combS" and "include_min_comb" should have correct values already *)
   626 fun tptp_write_file thms filename (axclauses,classrel_clauses,arity_clauses) user_lemmas=
   627     let val clss = make_conjecture_clauses thms
   628         val (clnames,axclauses') = ListPair.unzip (make_axiom_clauses axclauses user_lemmas)
   629 	val (tptp_clss,tfree_litss) = ListPair.unzip (map clause2tptp clss)
   630 	val tfree_clss = map ResClause.tptp_tfree_clause (foldl (op union_string) [] tfree_litss)
   631 	val out = TextIO.openOut filename
   632 	val helper_clauses = (#2 o ListPair.unzip o get_helper_clauses) ()
   633     in
   634 	List.app (curry TextIO.output out o #1 o clause2tptp) axclauses';
   635 	ResClause.writeln_strs out tfree_clss;
   636 	ResClause.writeln_strs out tptp_clss;
   637 	List.app (curry TextIO.output out o ResClause.tptp_classrelClause) classrel_clauses;
   638 	List.app (curry TextIO.output out o ResClause.tptp_arity_clause) arity_clauses;
   639 	List.app (curry TextIO.output out o #1 o clause2tptp) helper_clauses;
   640 	TextIO.closeOut out;
   641 	clnames
   642     end;
   643 
   644 
   645 
   646 (* dfg format *)
   647 
   648 fun dfg_write_file  thms filename (axclauses,classrel_clauses,arity_clauses) user_lemmas =
   649     let val _ = Output.debug ("Preparing to write the DFG file " ^ filename) 
   650 	val conjectures = make_conjecture_clauses thms
   651         val (clnames,axclauses') = ListPair.unzip (make_axiom_clauses axclauses user_lemmas)
   652 	val (dfg_clss,tfree_litss) = ListPair.unzip (map clause2dfg conjectures)
   653 	and probname = Path.pack (Path.base (Path.unpack filename))
   654 	val (axstrs,_) =  ListPair.unzip (map clause2dfg axclauses')
   655 	val tfree_clss = map ResClause.dfg_tfree_clause (ResClause.union_all tfree_litss)
   656 	val out = TextIO.openOut filename
   657 	val helper_clauses = (#2 o ListPair.unzip o get_helper_clauses) ()
   658 	val helper_clauses_strs = (#1 o ListPair.unzip o (map clause2dfg)) helper_clauses
   659 	val funcs = funcs_of_clauses (helper_clauses @ conjectures @ axclauses') arity_clauses
   660 	and preds = preds_of_clauses axclauses' classrel_clauses arity_clauses
   661     in
   662 	TextIO.output (out, ResClause.string_of_start probname); 
   663 	TextIO.output (out, ResClause.string_of_descrip probname); 
   664 	TextIO.output (out, ResClause.string_of_symbols (ResClause.string_of_funcs funcs) (ResClause.string_of_preds preds)); 
   665 	TextIO.output (out, "list_of_clauses(axioms,cnf).\n");
   666 	ResClause.writeln_strs out axstrs;
   667 	List.app (curry TextIO.output out o ResClause.dfg_classrelClause) classrel_clauses;
   668 	List.app (curry TextIO.output out o ResClause.dfg_arity_clause) arity_clauses;
   669 	ResClause.writeln_strs out helper_clauses_strs;
   670 	TextIO.output (out, "end_of_list.\n\nlist_of_clauses(conjectures,cnf).\n");
   671 	ResClause.writeln_strs out tfree_clss;
   672 	ResClause.writeln_strs out dfg_clss;
   673 	TextIO.output (out, "end_of_list.\n\n");
   674 	(*VarWeight=3 helps the HO problems, probably by counteracting the presence of hAPP*)
   675 	TextIO.output (out, "list_of_settings(SPASS).\n{*\nset_flag(VarWeight,3).\n*}\nend_of_list.\n\n");
   676 	TextIO.output (out, "end_problem.\n");
   677 	TextIO.closeOut out;
   678 	clnames
   679     end;
   680 
   681 end