src/HOL/Tools/res_hol_clause.ML
changeset 22078 5084f53cef39
parent 22064 3d716cc9bd7a
child 22130 0906fd95e0b5
equal deleted inserted replaced
22077:2882d9cc5e75 22078:5084f53cef39
     5 *)
     5 *)
     6 
     6 
     7 structure ResHolClause =
     7 structure ResHolClause =
     8 
     8 
     9 struct
     9 struct
       
    10 
       
    11 structure RC = ResClause;
    10 
    12 
    11 (* theorems for combinators and function extensionality *)
    13 (* theorems for combinators and function extensionality *)
    12 val ext = thm "HOL.ext";
    14 val ext = thm "HOL.ext";
    13 val comb_I = thm "ATP_Linkup.COMBI_def";
    15 val comb_I = thm "ATP_Linkup.COMBI_def";
    14 val comb_K = thm "ATP_Linkup.COMBK_def";
    16 val comb_K = thm "ATP_Linkup.COMBK_def";
    25   combinators appear to work best.*)
    27   combinators appear to work best.*)
    26 val use_Turner = ref false;
    28 val use_Turner = ref false;
    27 
    29 
    28 (*If true, each function will be directly applied to as many arguments as possible, avoiding
    30 (*If true, each function will be directly applied to as many arguments as possible, avoiding
    29   use of the "apply" operator. Use of hBOOL is also minimized.*)
    31   use of the "apply" operator. Use of hBOOL is also minimized.*)
    30 val minimize_applies = ref false;
    32 val minimize_applies = ref true;
    31 
    33 
    32 val const_typargs = ref (Library.K [] : (string*typ -> typ list));
    34 val const_typargs = ref (Library.K [] : (string*typ -> typ list));
    33 
    35 
    34 val const_min_arity = ref (Symtab.empty : int Symtab.table);
    36 val const_min_arity = ref (Symtab.empty : int Symtab.table);
    35 
    37 
   130 		val tp' = Term.type_of1(bnds,P')
   132 		val tp' = Term.type_of1(bnds,P')
   131 		val tq' = Term.type_of1(bnds,Q')
   133 		val tq' = Term.type_of1(bnds,Q')
   132 		val tS = [tp',tq',t1] ---> tpq
   134 		val tS = [tp',tq',t1] ---> tpq
   133 	    in  compact_comb (Const("ATP_Linkup.COMBS",tS) $ P' $ Q') bnds  end
   135 	    in  compact_comb (Const("ATP_Linkup.COMBS",tS) $ P' $ Q') bnds  end
   134       end
   136       end
   135   | lam2comb (t as (Abs(x,t1,_))) _ = raise ResClause.CLAUSE("HOL CLAUSE",t);
   137   | lam2comb (t as (Abs(x,t1,_))) _ = raise RC.CLAUSE("HOL CLAUSE",t);
   136 
   138 
   137 fun to_comb (Abs(x,tp,b)) bnds = lam2comb (Abs(x, tp, to_comb b (tp::bnds))) bnds 
   139 fun to_comb (Abs(x,tp,b)) bnds = lam2comb (Abs(x, tp, to_comb b (tp::bnds))) bnds 
   138   | to_comb (P $ Q) bnds = (to_comb P bnds) $ (to_comb Q bnds)
   140   | to_comb (P $ Q) bnds = (to_comb P bnds) $ (to_comb Q bnds)
   139   | to_comb t _ = t;
   141   | to_comb t _ = t;
   140 
   142 
   156 
   158 
   157 type axiom_name = string;
   159 type axiom_name = string;
   158 type polarity = bool;
   160 type polarity = bool;
   159 type clause_id = int;
   161 type clause_id = int;
   160 
   162 
   161 datatype combterm = CombConst of string * ResClause.fol_type * ResClause.fol_type list
   163 datatype combterm = CombConst of string * RC.fol_type * RC.fol_type list
   162 		  | CombFree of string * ResClause.fol_type
   164 		  | CombFree of string * RC.fol_type
   163 		  | CombVar of string * ResClause.fol_type
   165 		  | CombVar of string * RC.fol_type
   164 		  | CombApp of combterm * combterm * ResClause.fol_type
   166 		  | CombApp of combterm * combterm
   165 		  
   167 		  
   166 datatype literal = Literal of polarity * combterm;
   168 datatype literal = Literal of polarity * combterm;
   167 
   169 
   168 datatype clause = 
   170 datatype clause = 
   169 	 Clause of {clause_id: clause_id,
   171 	 Clause of {clause_id: clause_id,
   170 		    axiom_name: axiom_name,
   172 		    axiom_name: axiom_name,
   171 		    th: thm,
   173 		    th: thm,
   172 		    kind: ResClause.kind,
   174 		    kind: RC.kind,
   173 		    literals: literal list,
   175 		    literals: literal list,
   174 		    ctypes_sorts: (ResClause.typ_var * Term.sort) list, 
   176 		    ctypes_sorts: (RC.typ_var * Term.sort) list, 
   175                     ctvar_type_literals: ResClause.type_literal list, 
   177                     ctvar_type_literals: RC.type_literal list, 
   176                     ctfree_type_literals: ResClause.type_literal list};
   178                     ctfree_type_literals: RC.type_literal list};
   177 
   179 
   178 
   180 
   179 (*********************************************************************)
   181 (*********************************************************************)
   180 (* convert a clause with type Term.term to a clause with type clause *)
   182 (* convert a clause with type Term.term to a clause with type clause *)
   181 (*********************************************************************)
   183 (*********************************************************************)
   192   
   194   
   193 fun isTaut (Clause {literals,...}) = exists isTrue literals;  
   195 fun isTaut (Clause {literals,...}) = exists isTrue literals;  
   194 
   196 
   195 fun type_of (Type (a, Ts)) =
   197 fun type_of (Type (a, Ts)) =
   196       let val (folTypes,ts) = types_of Ts
   198       let val (folTypes,ts) = types_of Ts
   197 	  val t = ResClause.make_fixed_type_const a
   199       in  (RC.Comp(RC.make_fixed_type_const a, folTypes), ts)  end
   198       in
       
   199 	  (ResClause.mk_fol_type("Comp",t,folTypes), ts)
       
   200       end
       
   201   | type_of (tp as (TFree(a,s))) =
   200   | type_of (tp as (TFree(a,s))) =
   202       let val t = ResClause.make_fixed_type_var a
   201       (RC.AtomF (RC.make_fixed_type_var a), [RC.mk_typ_var_sort tp])
   203       in
       
   204 	  (ResClause.mk_fol_type("Fixed",t,[]), [ResClause.mk_typ_var_sort tp])
       
   205       end
       
   206   | type_of (tp as (TVar(v,s))) =
   202   | type_of (tp as (TVar(v,s))) =
   207       let val t = ResClause.make_schematic_type_var v
   203       (RC.AtomV (RC.make_schematic_type_var v), [RC.mk_typ_var_sort tp])
   208       in
       
   209 	  (ResClause.mk_fol_type("Var",t,[]), [ResClause.mk_typ_var_sort tp])
       
   210       end
       
   211 and types_of Ts =
   204 and types_of Ts =
   212       let val foltyps_ts = map type_of Ts
   205       let val (folTyps,ts) = ListPair.unzip (map type_of Ts)
   213 	  val (folTyps,ts) = ListPair.unzip foltyps_ts
   206       in  (folTyps, RC.union_all ts)  end;
   214       in
       
   215 	  (folTyps, ResClause.union_all ts)
       
   216       end;
       
   217 
   207 
   218 (* same as above, but no gathering of sort information *)
   208 (* same as above, but no gathering of sort information *)
   219 fun simp_type_of (Type (a, Ts)) = 
   209 fun simp_type_of (Type (a, Ts)) = 
   220       let val typs = map simp_type_of Ts
   210       RC.Comp(RC.make_fixed_type_const a, map simp_type_of Ts)
   221 	  val t = ResClause.make_fixed_type_const a
   211   | simp_type_of (TFree (a,s)) = RC.AtomF(RC.make_fixed_type_var a)
   222       in
   212   | simp_type_of (TVar (v,s)) = RC.AtomV(RC.make_schematic_type_var v);
   223 	  ResClause.mk_fol_type("Comp",t,typs)
       
   224       end
       
   225   | simp_type_of (TFree (a,s)) = ResClause.mk_fol_type("Fixed",ResClause.make_fixed_type_var a,[])
       
   226   | simp_type_of (TVar (v,s)) = ResClause.mk_fol_type("Var",ResClause.make_schematic_type_var v,[]);
       
   227 
   213 
   228 
   214 
   229 fun const_type_of (c,t) =
   215 fun const_type_of (c,t) =
   230     let val (tp,ts) = type_of t
   216       let val (tp,ts) = type_of t
   231 	val tvars = !const_typargs(c,t)
   217       in  (tp, ts, map simp_type_of (!const_typargs(c,t))) end;
   232     in
       
   233 	(tp, ts, map simp_type_of tvars)
       
   234     end;
       
   235 
   218 
   236 (* convert a Term.term (with combinators) into a combterm, also accummulate sort info *)
   219 (* convert a Term.term (with combinators) into a combterm, also accummulate sort info *)
   237 fun combterm_of (Const(c,t)) =
   220 fun combterm_of (Const(c,t)) =
   238       let val (tp,ts,tvar_list) = const_type_of (c,t)
   221       let val (tp,ts,tvar_list) = const_type_of (c,t)
   239 	  val c' = CombConst(ResClause.make_fixed_const c,tp,tvar_list)
   222 	  val c' = CombConst(RC.make_fixed_const c, tp, tvar_list)
   240       in
   223       in  (c',ts)  end
   241 	  (c',ts)
       
   242       end
       
   243   | combterm_of (Free(v,t)) =
   224   | combterm_of (Free(v,t)) =
   244       let val (tp,ts) = type_of t
   225       let val (tp,ts) = type_of t
   245 	  val v' = if ResClause.isMeta v then CombVar(ResClause.make_schematic_var(v,0),tp)
   226 	  val v' = if RC.isMeta v then CombVar(RC.make_schematic_var(v,0),tp)
   246 		   else CombFree(ResClause.make_fixed_var v,tp)
   227 		   else CombFree(RC.make_fixed_var v,tp)
   247       in
   228       in  (v',ts)  end
   248 	  (v',ts)
       
   249       end
       
   250   | combterm_of (Var(v,t)) =
   229   | combterm_of (Var(v,t)) =
   251       let val (tp,ts) = type_of t
   230       let val (tp,ts) = type_of t
   252 	  val v' = CombVar(ResClause.make_schematic_var v,tp)
   231 	  val v' = CombVar(RC.make_schematic_var v,tp)
   253       in
   232       in  (v',ts)  end
   254 	  (v',ts)
       
   255       end
       
   256   | combterm_of (t as (P $ Q)) =
   233   | combterm_of (t as (P $ Q)) =
   257       let val (P',tsP) = combterm_of P
   234       let val (P',tsP) = combterm_of P
   258 	  val (Q',tsQ) = combterm_of Q
   235 	  val (Q',tsQ) = combterm_of Q
   259 	  val tp = Term.type_of t
   236       in  (CombApp(P',Q'), tsP union tsQ)  end;
   260 	  val t' = CombApp(P',Q', simp_type_of tp)
       
   261       in
       
   262 	  (t',tsP union tsQ)
       
   263       end;
       
   264 
   237 
   265 fun predicate_of ((Const("Not",_) $ P), polarity) = predicate_of (P, not polarity)
   238 fun predicate_of ((Const("Not",_) $ P), polarity) = predicate_of (P, not polarity)
   266   | predicate_of (t,polarity) = (combterm_of t, polarity);
   239   | predicate_of (t,polarity) = (combterm_of t, polarity);
   267 
   240 
   268 fun literals_of_term1 args (Const("Trueprop",_) $ P) = literals_of_term1 args P
   241 fun literals_of_term1 args (Const("Trueprop",_) $ P) = literals_of_term1 args P
   277 fun literals_of_term P = literals_of_term1 ([],[]) P;
   250 fun literals_of_term P = literals_of_term1 ([],[]) P;
   278 
   251 
   279 (* making axiom and conjecture clauses *)
   252 (* making axiom and conjecture clauses *)
   280 fun make_clause(clause_id,axiom_name,kind,th) =
   253 fun make_clause(clause_id,axiom_name,kind,th) =
   281     let val (lits,ctypes_sorts) = literals_of_term (to_comb (prop_of th) [])
   254     let val (lits,ctypes_sorts) = literals_of_term (to_comb (prop_of th) [])
   282 	val (ctvar_lits,ctfree_lits) = ResClause.add_typs_aux ctypes_sorts
   255 	val (ctvar_lits,ctfree_lits) = RC.add_typs_aux ctypes_sorts
   283     in
   256     in
   284 	if forall isFalse lits
   257 	if forall isFalse lits
   285 	then error "Problem too trivial for resolution (empty clause)"
   258 	then error "Problem too trivial for resolution (empty clause)"
   286 	else
   259 	else
   287 	    Clause {clause_id = clause_id, axiom_name = axiom_name, th = th, kind = kind,
   260 	    Clause {clause_id = clause_id, axiom_name = axiom_name, th = th, kind = kind,
   290 		    ctfree_type_literals = ctfree_lits}
   263 		    ctfree_type_literals = ctfree_lits}
   291     end;
   264     end;
   292 
   265 
   293 
   266 
   294 fun add_axiom_clause ((th,(name,id)), pairs) =
   267 fun add_axiom_clause ((th,(name,id)), pairs) =
   295   let val cls = make_clause(id, name, ResClause.Axiom, th)
   268   let val cls = make_clause(id, name, RC.Axiom, th)
   296   in
   269   in
   297       if isTaut cls then pairs else (name,cls)::pairs
   270       if isTaut cls then pairs else (name,cls)::pairs
   298   end;
   271   end;
   299 
   272 
   300 val make_axiom_clauses = foldl add_axiom_clause [];
   273 val make_axiom_clauses = foldl add_axiom_clause [];
   301 
   274 
   302 fun make_conjecture_clauses_aux _ [] = []
   275 fun make_conjecture_clauses_aux _ [] = []
   303   | make_conjecture_clauses_aux n (th::ths) =
   276   | make_conjecture_clauses_aux n (th::ths) =
   304       make_clause(n,"conjecture", ResClause.Conjecture, th) ::
   277       make_clause(n,"conjecture", RC.Conjecture, th) ::
   305       make_conjecture_clauses_aux (n+1) ths;
   278       make_conjecture_clauses_aux (n+1) ths;
   306 
   279 
   307 val make_conjecture_clauses = make_conjecture_clauses_aux 0;
   280 val make_conjecture_clauses = make_conjecture_clauses_aux 0;
   308 
   281 
   309 
   282 
   314 (**********************************************************************)
   287 (**********************************************************************)
   315 
   288 
   316 val type_wrapper = "typeinfo";
   289 val type_wrapper = "typeinfo";
   317 
   290 
   318 fun wrap_type (c,tp) = case !typ_level of
   291 fun wrap_type (c,tp) = case !typ_level of
   319 	T_FULL => type_wrapper ^ ResClause.paren_pack [c, ResClause.string_of_fol_type tp]
   292 	T_FULL => type_wrapper ^ RC.paren_pack [c, RC.string_of_fol_type tp]
   320       | _ => c;
   293       | _ => c;
   321     
   294     
   322 
   295 
   323 val bool_tp = ResClause.make_fixed_type_const "bool";
   296 val bool_tp = RC.make_fixed_type_const "bool";
   324 
   297 
   325 val app_str = "hAPP";
   298 val app_str = "hAPP";
       
   299 
       
   300 (*Result of a function type; no need to check that the argument type matches.*)
       
   301 fun result_type (RC.Comp ("tc_fun", [_, tp2])) = tp2
       
   302   | result_type _ = raise ERROR "result_type"
   326 
   303 
   327 fun type_of_combterm (CombConst(c,tp,_)) = tp
   304 fun type_of_combterm (CombConst(c,tp,_)) = tp
   328   | type_of_combterm (CombFree(v,tp)) = tp
   305   | type_of_combterm (CombFree(v,tp)) = tp
   329   | type_of_combterm (CombVar(v,tp)) = tp
   306   | type_of_combterm (CombVar(v,tp)) = tp
   330   | type_of_combterm (CombApp(t1,t2,tp)) = tp;
   307   | type_of_combterm (CombApp(t1,t2)) = result_type (type_of_combterm t1);
   331 
   308 
   332 (*gets the head of a combinator application, along with the list of arguments*)
   309 (*gets the head of a combinator application, along with the list of arguments*)
   333 fun strip_comb u =
   310 fun strip_comb u =
   334     let fun stripc (CombApp(t,u,_), ts) = stripc (t, u::ts)
   311     let fun stripc (CombApp(t,u), ts) = stripc (t, u::ts)
   335         |   stripc  x =  x
   312         |   stripc  x =  x
   336     in  stripc(u,[])  end;
   313     in  stripc(u,[])  end;
   337 
   314 
   338 fun string_of_combterm1 (CombConst(c,tp,_)) = 
   315 fun string_of_combterm1 (CombConst(c,tp,_)) = 
   339       let val c' = if c = "equal" then "c_fequal" else c
   316       let val c' = if c = "equal" then "c_fequal" else c
   340       in  wrap_type (c',tp)  end
   317       in  wrap_type (c',tp)  end
   341   | string_of_combterm1 (CombFree(v,tp)) = wrap_type (v,tp)
   318   | string_of_combterm1 (CombFree(v,tp)) = wrap_type (v,tp)
   342   | string_of_combterm1 (CombVar(v,tp)) = wrap_type (v,tp)
   319   | string_of_combterm1 (CombVar(v,tp)) = wrap_type (v,tp)
   343   | string_of_combterm1 (CombApp(t1,t2,tp)) =
   320   | string_of_combterm1 (CombApp(t1,t2)) =
   344       let val s1 = string_of_combterm1 t1
   321       let val s1 = string_of_combterm1 t1
   345 	  val s2 = string_of_combterm1 t2
   322 	  and s2 = string_of_combterm1 t2
   346       in
   323       in
   347 	  case !typ_level of
   324 	  case !typ_level of
   348 	      T_FULL => type_wrapper ^ 
   325 	      T_FULL => type_wrapper ^ 
   349 	                ResClause.paren_pack 
   326 	                RC.paren_pack 
   350 	                  [app_str ^ ResClause.paren_pack [s1,s2], 
   327 	                  [app_str ^ RC.paren_pack [s1,s2], 
   351 	                   ResClause.string_of_fol_type tp]
   328 	                   RC.string_of_fol_type (result_type (type_of_combterm t1))]
   352 	    | T_PARTIAL => app_str ^ ResClause.paren_pack 
   329 	    | T_PARTIAL => app_str ^ RC.paren_pack 
   353 	                     [s1,s2, ResClause.string_of_fol_type (type_of_combterm t1)]
   330 	                     [s1,s2, RC.string_of_fol_type (type_of_combterm t1)]
   354 	    | T_NONE => app_str ^ ResClause.paren_pack [s1,s2]
   331 	    | T_NONE => app_str ^ RC.paren_pack [s1,s2]
   355 	    | T_CONST => raise ERROR "string_of_combterm1"
   332 	    | T_CONST => raise ERROR "string_of_combterm1"
   356       end;
   333       end;
   357 
   334 
   358 fun rev_apply (v, []) = v
   335 fun rev_apply (v, []) = v
   359   | rev_apply (v, arg::args) = app_str ^ ResClause.paren_pack [rev_apply (v, args), arg];
   336   | rev_apply (v, arg::args) = app_str ^ RC.paren_pack [rev_apply (v, args), arg];
   360 
   337 
   361 fun string_apply (v, args) = rev_apply (v, rev args);
   338 fun string_apply (v, args) = rev_apply (v, rev args);
   362 
   339 
   363 (*Apply an operator to the argument strings, using either the "apply" operator or
   340 (*Apply an operator to the argument strings, using either the "apply" operator or
   364   direct function application.*)
   341   direct function application.*)
   365 fun string_of_applic (CombConst(c,tp,tvars), args) =
   342 fun string_of_applic (CombConst(c,_,tvars), args) =
   366       let val c = if c = "equal" then "c_fequal" else c
   343       let val c = if c = "equal" then "c_fequal" else c
   367           val nargs = min_arity_of c
   344           val nargs = min_arity_of c
   368           val args1 = List.take(args, nargs) @ (map ResClause.string_of_fol_type tvars)
   345           val args1 = List.take(args, nargs) @ (map RC.string_of_fol_type tvars)
   369             handle Subscript => raise ERROR ("string_of_applic: " ^ c ^ " has arity " ^
   346             handle Subscript => raise ERROR ("string_of_applic: " ^ c ^ " has arity " ^
   370 					     Int.toString nargs ^ " but is applied to " ^ 
   347 					     Int.toString nargs ^ " but is applied to " ^ 
   371 					     space_implode ", " args) 
   348 					     space_implode ", " args) 
   372           val args2 = List.drop(args, nargs)
   349           val args2 = List.drop(args, nargs)
   373       in
   350       in
   374 	  string_apply (c ^ ResClause.paren_pack args1, args2)
   351 	  string_apply (c ^ RC.paren_pack args1, args2)
   375       end
   352       end
   376   | string_of_applic (CombFree(v,tp), args) = string_apply (v, args)
   353   | string_of_applic (CombFree(v,_), args) = string_apply (v, args)
   377   | string_of_applic (CombVar(v,tp), args) = string_apply (v, args)  
   354   | string_of_applic (CombVar(v,_), args) = string_apply (v, args)  
   378   | string_of_applic _ = raise ERROR "string_of_applic";
   355   | string_of_applic _ = raise ERROR "string_of_applic";
   379 
   356 
   380 fun string_of_combterm2 t = 
   357 fun string_of_combterm2 t = 
   381   let val (head, args) = strip_comb t
   358   let val (head, args) = strip_comb t
   382   in  string_of_applic (head, map string_of_combterm2 args)  end;
   359   in  string_of_applic (head, map string_of_combterm2 args)  end;
   384 fun string_of_combterm t = 
   361 fun string_of_combterm t = 
   385     case !typ_level of T_CONST => string_of_combterm2 t
   362     case !typ_level of T_CONST => string_of_combterm2 t
   386 		           | _ => string_of_combterm1 t;
   363 		           | _ => string_of_combterm1 t;
   387 
   364 
   388 (*Boolean-valued terms are here converted to literals.*)
   365 (*Boolean-valued terms are here converted to literals.*)
   389 fun boolify t = "hBOOL" ^ ResClause.paren_pack [string_of_combterm t];
   366 fun boolify t = "hBOOL" ^ RC.paren_pack [string_of_combterm t];
   390 
   367 
   391 fun string_of_predicate t = 
   368 fun string_of_predicate t = 
   392   case t of
   369   case t of
   393       (CombApp(CombApp(CombConst("equal",_,_),t1,_),t2,_)) =>
   370       (CombApp(CombApp(CombConst("equal",_,_), t1), t2)) =>
   394 	  (*DFG only: new TPTP prefers infix equality*)
   371 	  (*DFG only: new TPTP prefers infix equality*)
   395 	  ("equal" ^ ResClause.paren_pack [string_of_combterm t1, string_of_combterm t2])
   372 	  ("equal" ^ RC.paren_pack [string_of_combterm t1, string_of_combterm t2])
   396     | _ => 
   373     | _ => 
   397           case #1 (strip_comb t) of
   374           case #1 (strip_comb t) of
   398               CombConst(c,_,_) => if needs_hBOOL c then boolify t else string_of_combterm t
   375               CombConst(c,_,_) => if needs_hBOOL c then boolify t else string_of_combterm t
   399             | _ => boolify t;
   376             | _ => boolify t;
   400 
   377 
   401 fun string_of_clausename (cls_id,ax_name) = 
   378 fun string_of_clausename (cls_id,ax_name) = 
   402     ResClause.clause_prefix ^ ResClause.ascii_of ax_name ^ "_" ^ Int.toString cls_id;
   379     RC.clause_prefix ^ RC.ascii_of ax_name ^ "_" ^ Int.toString cls_id;
   403 
   380 
   404 fun string_of_type_clsname (cls_id,ax_name,idx) = 
   381 fun string_of_type_clsname (cls_id,ax_name,idx) = 
   405     string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
   382     string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
   406 
   383 
   407 
   384 
   409 
   386 
   410 fun tptp_of_equality pol (t1,t2) =
   387 fun tptp_of_equality pol (t1,t2) =
   411   let val eqop = if pol then " = " else " != "
   388   let val eqop = if pol then " = " else " != "
   412   in  string_of_combterm t1 ^ eqop ^ string_of_combterm t2  end;
   389   in  string_of_combterm t1 ^ eqop ^ string_of_combterm t2  end;
   413 
   390 
   414 fun tptp_literal (Literal(pol, CombApp(CombApp(CombConst("equal",_,_),t1,_),t2,_))) = 
   391 fun tptp_literal (Literal(pol, CombApp(CombApp(CombConst("equal",_,_), t1), t2))) = 
   415       tptp_of_equality pol (t1,t2)
   392       tptp_of_equality pol (t1,t2)
   416   | tptp_literal (Literal(pol,pred)) = 
   393   | tptp_literal (Literal(pol,pred)) = 
   417       ResClause.tptp_sign pol (string_of_predicate pred);
   394       RC.tptp_sign pol (string_of_predicate pred);
   418  
   395  
   419 (*Given a clause, returns its literals paired with a list of literals concerning TFrees;
   396 (*Given a clause, returns its literals paired with a list of literals concerning TFrees;
   420   the latter should only occur in conjecture clauses.*)
   397   the latter should only occur in conjecture clauses.*)
   421 fun tptp_type_lits (Clause cls) = 
   398 fun tptp_type_lits (Clause cls) = 
   422     let val lits = map tptp_literal (#literals cls)
   399     let val lits = map tptp_literal (#literals cls)
   423 	val ctvar_lits_strs =
   400 	val ctvar_lits_strs =
   424 	    case !typ_level of T_NONE => []
   401 	    case !typ_level of T_NONE => []
   425 	      | _ => map ResClause.tptp_of_typeLit (#ctvar_type_literals cls)
   402 	      | _ => map RC.tptp_of_typeLit (#ctvar_type_literals cls)
   426 	val ctfree_lits = 
   403 	val ctfree_lits = 
   427 	    case !typ_level of T_NONE => []
   404 	    case !typ_level of T_NONE => []
   428 	      | _ => map ResClause.tptp_of_typeLit (#ctfree_type_literals cls)
   405 	      | _ => map RC.tptp_of_typeLit (#ctfree_type_literals cls)
   429     in
   406     in
   430 	(ctvar_lits_strs @ lits, ctfree_lits)
   407 	(ctvar_lits_strs @ lits, ctfree_lits)
   431     end; 
   408     end; 
   432     
   409     
   433 fun clause2tptp (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   410 fun clause2tptp (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   434     let val (lits,ctfree_lits) = tptp_type_lits cls
   411     let val (lits,ctfree_lits) = tptp_type_lits cls
   435 	val cls_str = ResClause.gen_tptp_cls(clause_id,axiom_name,kind,lits)
   412 	val cls_str = RC.gen_tptp_cls(clause_id,axiom_name,kind,lits)
   436     in
   413     in
   437 	(cls_str,ctfree_lits)
   414 	(cls_str,ctfree_lits)
   438     end;
   415     end;
   439 
   416 
   440 
   417 
   441 (*** dfg format ***)
   418 (*** dfg format ***)
   442 
   419 
   443 fun dfg_literal (Literal(pol,pred)) = ResClause.dfg_sign pol (string_of_predicate pred);
   420 fun dfg_literal (Literal(pol,pred)) = RC.dfg_sign pol (string_of_predicate pred);
   444 
   421 
   445 fun dfg_clause_aux (Clause{literals, ctypes_sorts, ...}) = 
   422 fun dfg_clause_aux (Clause{literals, ctypes_sorts, ...}) = 
   446   let val lits = map dfg_literal literals
   423   let val lits = map dfg_literal literals
   447       val (tvar_lits,tfree_lits) = ResClause.add_typs_aux ctypes_sorts
   424       val (tvar_lits,tfree_lits) = RC.add_typs_aux ctypes_sorts
   448       val tvar_lits_strs = 
   425       val tvar_lits_strs = 
   449 	  case !typ_level of T_NONE => [] 
   426 	  case !typ_level of T_NONE => [] 
   450 	      | _ => map ResClause.dfg_of_typeLit tvar_lits
   427 	      | _ => map RC.dfg_of_typeLit tvar_lits
   451       val tfree_lits =
   428       val tfree_lits =
   452           case !typ_level of T_NONE => []
   429           case !typ_level of T_NONE => []
   453 	      | _ => map ResClause.dfg_of_typeLit tfree_lits 
   430 	      | _ => map RC.dfg_of_typeLit tfree_lits 
   454   in
   431   in
   455       (tvar_lits_strs @ lits, tfree_lits)
   432       (tvar_lits_strs @ lits, tfree_lits)
   456   end; 
   433   end; 
   457 
   434 
   458 fun get_uvars (CombConst(_,_,_)) vars = vars
   435 fun get_uvars (CombConst _) vars = vars
   459   | get_uvars (CombFree(_,_)) vars = vars
   436   | get_uvars (CombFree _) vars = vars
   460   | get_uvars (CombVar(v,tp)) vars = (v::vars)
   437   | get_uvars (CombVar(v,_)) vars = (v::vars)
   461   | get_uvars (CombApp(P,Q,tp)) vars = get_uvars P (get_uvars Q vars);
   438   | get_uvars (CombApp(P,Q)) vars = get_uvars P (get_uvars Q vars);
   462 
   439 
   463 fun get_uvars_l (Literal(_,c)) = get_uvars c [];
   440 fun get_uvars_l (Literal(_,c)) = get_uvars c [];
   464 
   441 
   465 fun dfg_vars (Clause {literals,...}) = ResClause.union_all (map get_uvars_l literals);
   442 fun dfg_vars (Clause {literals,...}) = RC.union_all (map get_uvars_l literals);
   466  
   443  
   467 fun clause2dfg (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   444 fun clause2dfg (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   468     let val (lits,tfree_lits) = dfg_clause_aux cls 
   445     let val (lits,tfree_lits) = dfg_clause_aux cls 
   469         val vars = dfg_vars cls
   446         val vars = dfg_vars cls
   470         val tvars = ResClause.get_tvar_strs ctypes_sorts
   447         val tvars = RC.get_tvar_strs ctypes_sorts
   471 	val knd = ResClause.name_of_kind kind
   448 	val knd = RC.name_of_kind kind
   472 	val lits_str = commas lits
   449 	val lits_str = commas lits
   473 	val cls_str = ResClause.gen_dfg_cls(clause_id, axiom_name, knd, lits_str, tvars@vars) 
   450 	val cls_str = RC.gen_dfg_cls(clause_id, axiom_name, knd, lits_str, tvars@vars) 
   474     in (cls_str, tfree_lits) end;
   451     in (cls_str, tfree_lits) end;
   475 
   452 
   476 (** For DFG format: accumulate function and predicate declarations **)
   453 (** For DFG format: accumulate function and predicate declarations **)
   477 
   454 
   478 fun addtypes tvars tab = foldl ResClause.add_foltype_funcs tab tvars;
   455 fun addtypes tvars tab = foldl RC.add_foltype_funcs tab tvars;
   479 
   456 
   480 fun add_decls (CombConst(c,_,tvars), (funcs,preds)) =
   457 fun add_decls (CombConst(c,_,tvars), (funcs,preds)) =
   481       if c = "equal" then (addtypes tvars funcs, preds)
   458       if c = "equal" then (addtypes tvars funcs, preds)
   482       else
   459       else
   483 	(case !typ_level of
   460 	(case !typ_level of
   488                    if needs_hBOOL c then (addtypes tvars (addit funcs), preds)
   465                    if needs_hBOOL c then (addtypes tvars (addit funcs), preds)
   489                    else (addtypes tvars funcs, addit preds)
   466                    else (addtypes tvars funcs, addit preds)
   490                end
   467                end
   491            | _ => (addtypes tvars (Symtab.update(c,0) funcs), preds))
   468            | _ => (addtypes tvars (Symtab.update(c,0) funcs), preds))
   492   | add_decls (CombFree(v,ctp), (funcs,preds)) = 
   469   | add_decls (CombFree(v,ctp), (funcs,preds)) = 
   493       (ResClause.add_foltype_funcs (ctp,Symtab.update (v,0) funcs), preds)
   470       (RC.add_foltype_funcs (ctp,Symtab.update (v,0) funcs), preds)
   494   | add_decls (CombVar(_,ctp), (funcs,preds)) = 
   471   | add_decls (CombVar(_,ctp), (funcs,preds)) = 
   495       (ResClause.add_foltype_funcs (ctp,funcs), preds)
   472       (RC.add_foltype_funcs (ctp,funcs), preds)
   496   | add_decls (CombApp(P,Q,_),decls) = add_decls(P,add_decls (Q,decls));
   473   | add_decls (CombApp(P,Q),decls) = add_decls(P,add_decls (Q,decls));
   497 
   474 
   498 fun add_literal_decls (Literal(_,c), decls) = add_decls (c,decls);
   475 fun add_literal_decls (Literal(_,c), decls) = add_decls (c,decls);
   499 
   476 
   500 fun add_clause_decls (Clause {literals, ...}, decls) =
   477 fun add_clause_decls (Clause {literals, ...}, decls) =
   501     foldl add_literal_decls decls literals
   478     foldl add_literal_decls decls literals
   505   let val happ_ar = case !typ_level of T_PARTIAL => 3 | _ => 2
   482   let val happ_ar = case !typ_level of T_PARTIAL => 3 | _ => 2
   506       val init_functab = Symtab.update ("typeinfo",2) (Symtab.update ("hAPP",happ_ar) Symtab.empty)
   483       val init_functab = Symtab.update ("typeinfo",2) (Symtab.update ("hAPP",happ_ar) Symtab.empty)
   507       val init_predtab = Symtab.update ("hBOOL",1) Symtab.empty
   484       val init_predtab = Symtab.update ("hBOOL",1) Symtab.empty
   508       val (functab,predtab) = (foldl add_clause_decls (init_functab, init_predtab) clauses)
   485       val (functab,predtab) = (foldl add_clause_decls (init_functab, init_predtab) clauses)
   509   in
   486   in
   510       (Symtab.dest (foldl ResClause.add_arityClause_funcs functab arity_clauses), 
   487       (Symtab.dest (foldl RC.add_arityClause_funcs functab arity_clauses), 
   511        Symtab.dest predtab)
   488        Symtab.dest predtab)
   512   end;
   489   end;
   513 
   490 
   514 fun add_clause_preds (Clause {ctypes_sorts, ...}, preds) =
   491 fun add_clause_preds (Clause {ctypes_sorts, ...}, preds) =
   515   foldl ResClause.add_type_sort_preds preds ctypes_sorts
   492   foldl RC.add_type_sort_preds preds ctypes_sorts
   516   handle Symtab.DUP a => raise ERROR ("predicate " ^ a ^ " has multiple arities")
   493   handle Symtab.DUP a => raise ERROR ("predicate " ^ a ^ " has multiple arities")
   517 
   494 
   518 (*Higher-order clauses have only the predicates hBOOL and type classes.*)
   495 (*Higher-order clauses have only the predicates hBOOL and type classes.*)
   519 fun preds_of_clauses clauses clsrel_clauses arity_clauses = 
   496 fun preds_of_clauses clauses clsrel_clauses arity_clauses = 
   520     Symtab.dest
   497     Symtab.dest
   521 	(foldl ResClause.add_classrelClause_preds 
   498 	(foldl RC.add_classrelClause_preds 
   522 	       (foldl ResClause.add_arityClause_preds
   499 	       (foldl RC.add_arityClause_preds
   523 		      (foldl add_clause_preds Symtab.empty clauses) 
   500 		      (foldl add_clause_preds Symtab.empty clauses) 
   524 		      arity_clauses)
   501 		      arity_clauses)
   525 	       clsrel_clauses)
   502 	       clsrel_clauses)
   526 
   503 
   527 
   504 
   540 fun count_combterm (CombConst(c,tp,_), ct) = 
   517 fun count_combterm (CombConst(c,tp,_), ct) = 
   541      (case Symtab.lookup ct c of NONE => ct  (*no counter*)
   518      (case Symtab.lookup ct c of NONE => ct  (*no counter*)
   542                                | SOME n => Symtab.update (c,n+1) ct)
   519                                | SOME n => Symtab.update (c,n+1) ct)
   543   | count_combterm (CombFree(v,tp), ct) = ct
   520   | count_combterm (CombFree(v,tp), ct) = ct
   544   | count_combterm (CombVar(v,tp), ct) = ct
   521   | count_combterm (CombVar(v,tp), ct) = ct
   545   | count_combterm (CombApp(t1,t2,tp), ct) = count_combterm(t1, count_combterm(t2, ct));
   522   | count_combterm (CombApp(t1,t2), ct) = count_combterm(t1, count_combterm(t2, ct));
   546 
   523 
   547 fun count_literal (Literal(_,t), ct) = count_combterm(t,ct);
   524 fun count_literal (Literal(_,t), ct) = count_combterm(t,ct);
   548 
   525 
   549 fun count_clause (Clause{literals,...}, ct) = foldl count_literal ct literals;
   526 fun count_clause (Clause{literals,...}, ct) = foldl count_literal ct literals;
   550 
   527 
   620     let val conjectures = make_conjecture_clauses thms
   597     let val conjectures = make_conjecture_clauses thms
   621         val (clnames,axclauses) = ListPair.unzip (make_axiom_clauses ax_tuples)
   598         val (clnames,axclauses) = ListPair.unzip (make_axiom_clauses ax_tuples)
   622 	val helper_clauses = get_helper_clauses (conjectures, axclauses, user_lemmas)
   599 	val helper_clauses = get_helper_clauses (conjectures, axclauses, user_lemmas)
   623 	val _ = count_constants (conjectures, axclauses, helper_clauses);
   600 	val _ = count_constants (conjectures, axclauses, helper_clauses);
   624 	val (tptp_clss,tfree_litss) = ListPair.unzip (map clause2tptp conjectures)
   601 	val (tptp_clss,tfree_litss) = ListPair.unzip (map clause2tptp conjectures)
   625 	val tfree_clss = map ResClause.tptp_tfree_clause (foldl (op union_string) [] tfree_litss)
   602 	val tfree_clss = map RC.tptp_tfree_clause (foldl (op union_string) [] tfree_litss)
   626         val out = TextIO.openOut filename
   603         val out = TextIO.openOut filename
   627     in
   604     in
   628 	List.app (curry TextIO.output out o #1 o clause2tptp) axclauses;
   605 	List.app (curry TextIO.output out o #1 o clause2tptp) axclauses;
   629 	ResClause.writeln_strs out tfree_clss;
   606 	RC.writeln_strs out tfree_clss;
   630 	ResClause.writeln_strs out tptp_clss;
   607 	RC.writeln_strs out tptp_clss;
   631 	List.app (curry TextIO.output out o ResClause.tptp_classrelClause) classrel_clauses;
   608 	List.app (curry TextIO.output out o RC.tptp_classrelClause) classrel_clauses;
   632 	List.app (curry TextIO.output out o ResClause.tptp_arity_clause) arity_clauses;
   609 	List.app (curry TextIO.output out o RC.tptp_arity_clause) arity_clauses;
   633 	List.app (curry TextIO.output out o #1 o clause2tptp) helper_clauses;
   610 	List.app (curry TextIO.output out o #1 o clause2tptp) helper_clauses;
   634 	TextIO.closeOut out;
   611 	TextIO.closeOut out;
   635 	clnames
   612 	clnames
   636     end;
   613     end;
   637 
   614 
   645 	val helper_clauses = get_helper_clauses (conjectures, axclauses, user_lemmas)
   622 	val helper_clauses = get_helper_clauses (conjectures, axclauses, user_lemmas)
   646 	val _ = count_constants (conjectures, axclauses, helper_clauses);
   623 	val _ = count_constants (conjectures, axclauses, helper_clauses);
   647 	val (dfg_clss,tfree_litss) = ListPair.unzip (map clause2dfg conjectures)
   624 	val (dfg_clss,tfree_litss) = ListPair.unzip (map clause2dfg conjectures)
   648 	and probname = Path.implode (Path.base (Path.explode filename))
   625 	and probname = Path.implode (Path.base (Path.explode filename))
   649 	val axstrs = map (#1 o clause2dfg) axclauses
   626 	val axstrs = map (#1 o clause2dfg) axclauses
   650 	val tfree_clss = map ResClause.dfg_tfree_clause (ResClause.union_all tfree_litss)
   627 	val tfree_clss = map RC.dfg_tfree_clause (RC.union_all tfree_litss)
   651 	val out = TextIO.openOut filename
   628 	val out = TextIO.openOut filename
   652 	val helper_clauses_strs = map (#1 o clause2dfg) helper_clauses
   629 	val helper_clauses_strs = map (#1 o clause2dfg) helper_clauses
   653 	val (funcs,cl_preds) = decls_of_clauses (helper_clauses @ conjectures @ axclauses) arity_clauses
   630 	val (funcs,cl_preds) = decls_of_clauses (helper_clauses @ conjectures @ axclauses) arity_clauses
   654 	and ty_preds = preds_of_clauses axclauses classrel_clauses arity_clauses
   631 	and ty_preds = preds_of_clauses axclauses classrel_clauses arity_clauses
   655     in
   632     in
   656 	TextIO.output (out, ResClause.string_of_start probname); 
   633 	TextIO.output (out, RC.string_of_start probname); 
   657 	TextIO.output (out, ResClause.string_of_descrip probname); 
   634 	TextIO.output (out, RC.string_of_descrip probname); 
   658 	TextIO.output (out, ResClause.string_of_symbols 
   635 	TextIO.output (out, RC.string_of_symbols 
   659 	                      (ResClause.string_of_funcs funcs) 
   636 	                      (RC.string_of_funcs funcs) 
   660 	                      (ResClause.string_of_preds (cl_preds @ ty_preds))); 
   637 	                      (RC.string_of_preds (cl_preds @ ty_preds))); 
   661 	TextIO.output (out, "list_of_clauses(axioms,cnf).\n");
   638 	TextIO.output (out, "list_of_clauses(axioms,cnf).\n");
   662 	ResClause.writeln_strs out axstrs;
   639 	RC.writeln_strs out axstrs;
   663 	List.app (curry TextIO.output out o ResClause.dfg_classrelClause) classrel_clauses;
   640 	List.app (curry TextIO.output out o RC.dfg_classrelClause) classrel_clauses;
   664 	List.app (curry TextIO.output out o ResClause.dfg_arity_clause) arity_clauses;
   641 	List.app (curry TextIO.output out o RC.dfg_arity_clause) arity_clauses;
   665 	ResClause.writeln_strs out helper_clauses_strs;
   642 	RC.writeln_strs out helper_clauses_strs;
   666 	TextIO.output (out, "end_of_list.\n\nlist_of_clauses(conjectures,cnf).\n");
   643 	TextIO.output (out, "end_of_list.\n\nlist_of_clauses(conjectures,cnf).\n");
   667 	ResClause.writeln_strs out tfree_clss;
   644 	RC.writeln_strs out tfree_clss;
   668 	ResClause.writeln_strs out dfg_clss;
   645 	RC.writeln_strs out dfg_clss;
   669 	TextIO.output (out, "end_of_list.\n\n");
   646 	TextIO.output (out, "end_of_list.\n\n");
   670 	(*VarWeight=3 helps the HO problems, probably by counteracting the presence of hAPP*)
   647 	(*VarWeight=3 helps the HO problems, probably by counteracting the presence of hAPP*)
   671 	TextIO.output (out, "list_of_settings(SPASS).\n{*\nset_flag(VarWeight,3).\n*}\nend_of_list.\n\n");
   648 	TextIO.output (out, "list_of_settings(SPASS).\n{*\nset_flag(VarWeight,3).\n*}\nend_of_list.\n\n");
   672 	TextIO.output (out, "end_problem.\n");
   649 	TextIO.output (out, "end_problem.\n");
   673 	TextIO.closeOut out;
   650 	TextIO.closeOut out;