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