1 (* Author: Jia Meng, Cambridge University Computer Laboratory
3 Copyright 2004 University of Cambridge
5 ML data structure for storing/printing FOL clauses and arity clauses.
6 Typed equality is treated differently.
11 exception ARCLAUSE of string
12 exception CLAUSE of string
15 val classrelClauses_of :
16 string * string list -> classrelClause list
18 val keep_types : bool ref
19 val make_axiom_arity_clause :
20 string * (string * string list list) -> arityClause
21 val make_axiom_classrelClause :
22 string * string option -> classrelClause
23 val make_axiom_clause : Term.term -> string * int -> clause
24 val make_axiom_clause_thm : Thm.thm -> string * int -> clause
25 val make_conjecture_clause : Term.term -> clause
26 val make_conjecture_clause_thm : Thm.thm -> clause
27 val make_hypothesis_clause : Term.term -> clause
28 val make_hypothesis_clause_thm : Thm.thm -> clause
29 val special_equal : bool ref
30 val tptp_arity_clause : arityClause -> string
31 val tptp_classrelClause : classrelClause -> string
32 val tptp_clause : clause -> string list
33 val tptp_clauses2str : string list -> string
34 val typed : unit -> unit
35 val untyped : unit -> unit
36 val clause2tptp : clause -> string * string list
37 val tfree_clause : string -> string
40 structure ResClause : RES_CLAUSE =
43 (* Added for typed equality *)
44 val special_equal = ref false; (* by default,equality does not carry type information *)
45 val eq_typ_wrapper = "typeinfo"; (* default string *)
48 val schematic_var_prefix = "V_";
49 val fixed_var_prefix = "v_";
52 val tvar_prefix = "Typ_";
53 val tfree_prefix = "typ_";
56 val clause_prefix = "cls_";
58 val arclause_prefix = "arcls_"
60 val const_prefix = "const_";
61 val tconst_prefix = "tconst_";
63 val class_prefix = "clas_";
67 (**** some useful functions ****)
69 val const_trans_table =
70 Symtab.make [("op =", "equal"),
71 ("op <=", "lessequals"),
75 ("op -->", "implies"),
82 (*Escaping of special characters.
83 Alphanumeric characters are left unchanged.
84 The character _ goes to __
85 Characters in the range ASCII space to / go to _A to _P, respectively.
86 Other printing characters go to _NNN where NNN is the decimal ASCII code.*)
89 val A_minus_space = Char.ord #"A" - Char.ord #" ";
92 if Char.isAlphaNum c then String.str c
93 else if c = #"_" then "__"
94 else if #" " <= c andalso c <= #"/"
95 then "_" ^ String.str (Char.chr (Char.ord c + A_minus_space))
96 else if Char.isPrint c then ("_" ^ Int.toString (Char.ord c))
101 val ascii_of = String.translate ascii_of_c;
106 (* another version of above functions that remove chars that may not be allowed by Vampire *)
107 fun make_schematic_var v = schematic_var_prefix ^ (ascii_of v);
108 fun make_fixed_var x = fixed_var_prefix ^ (ascii_of x);
110 fun make_schematic_type_var v = tvar_prefix ^ (ascii_of v);
111 fun make_fixed_type_var x = tfree_prefix ^ (ascii_of x);
113 fun make_fixed_const c = const_prefix ^ (ascii_of c);
114 fun make_fixed_type_const c = tconst_prefix ^ (ascii_of c);
116 fun make_type_class clas = class_prefix ^ (ascii_of clas);
122 case Symtab.lookup (const_trans_table,c) of
124 | NONE => make_fixed_const c;
128 (***** definitions and functions for FOL clauses, prepared for conversion into TPTP format or SPASS format. *****)
130 val keep_types = ref true; (* default is true *)
131 fun untyped () = (keep_types := false);
132 fun typed () = (keep_types := true);
135 datatype kind = Axiom | Hypothesis | Conjecture;
136 fun name_of_kind Axiom = "axiom"
137 | name_of_kind Hypothesis = "hypothesis"
138 | name_of_kind Conjecture = "conjecture";
140 type clause_id = int;
141 type axiom_name = string;
144 type polarity = bool;
146 type indexname = Term.indexname;
149 (* "tag" is used for vampire specific syntax *)
154 fun string_of_indexname (name,index) = name ^ "_" ^ (string_of_int index);
166 (**** Isabelle FOL clauses ****)
168 (* by default it is false *)
169 val tagged = ref false;
171 type pred_name = string;
172 type sort = Term.sort;
173 type fol_type = string;
176 datatype type_literal = LTVar of string | LTFree of string;
179 datatype folTerm = UVar of string * fol_type| Fun of string * fol_type * folTerm list;
180 datatype predicate = Predicate of pred_name * fol_type * folTerm list;
183 datatype literal = Literal of polarity * predicate * tag;
186 datatype typ_var = FOLTVar of indexname | FOLTFree of string;
189 (* ML datatype used to repsent one single clause: disjunction of literals. *)
191 Clause of {clause_id: clause_id,
192 axiom_name: axiom_name,
194 literals: literal list,
195 types_sorts: (typ_var * sort) list,
196 tvar_type_literals: type_literal list,
197 tfree_type_literals: type_literal list };
200 exception CLAUSE of string;
204 (*** make clauses ***)
207 fun make_clause (clause_id,axiom_name,kind,literals,types_sorts,tvar_type_literals,tfree_type_literals) =
208 Clause {clause_id = clause_id, axiom_name = axiom_name, kind = kind, literals = literals, types_sorts = types_sorts,tvar_type_literals = tvar_type_literals,tfree_type_literals = tfree_type_literals};
212 fun type_of (Type (a, [])) = (make_fixed_type_const a,[])
213 | type_of (Type (a, Ts)) =
214 let val foltyps_ts = map type_of Ts
215 val (folTyps,ts) = ResLib.unzip foltyps_ts
216 val ts' = ResLib.flat_noDup ts
218 (((make_fixed_type_const a) ^ (ResLib.list_to_string folTyps)),ts')
220 | type_of (TFree (a, s)) = (make_fixed_type_var a, [((FOLTFree a),s)])
221 | type_of (TVar (v, s)) = (make_schematic_type_var (string_of_indexname v), [((FOLTVar v),s)]);
223 (* added: checkMeta: string -> bool *)
224 (* Any meta vars like ?x should be treated as universal vars,although it is represented as "Free(...)" by Isabelle *)
226 let val chars = explode s
228 ["M", "E", "T", "A", "H", "Y", "P", "1"] prefix chars
232 fun pred_name_type (Const(c,T)) = (lookup_const c,type_of T)
233 | pred_name_type (Free(x,T)) =
234 let val is_meta = checkMeta x
236 if is_meta then (raise CLAUSE("Predicate Not First Order")) else
237 (make_fixed_var x,type_of T)
239 | pred_name_type (Var(_,_)) = raise CLAUSE("Predicate Not First Order")
240 | pred_name_type _ = raise CLAUSE("Predicate input unexpected");
243 (* For type equality *)
244 (* here "arg_typ" is the type of "="'s argument's type, not the type of the equality *)
245 (* Find type of equality arg *)
246 fun eq_arg_type (Type("fun",[T,_])) =
247 let val (folT,_) = type_of T;
254 fun fun_name_type (Const(c,T)) = (lookup_const c,type_of T)
255 | fun_name_type (Free(x,T)) = (make_fixed_var x,type_of T)
256 | fun_name_type _ = raise CLAUSE("Function Not First Order");
260 fun term_of (Var(ind_nm,T)) =
261 let val (folType,ts) = type_of T
263 (UVar(make_schematic_var(string_of_indexname ind_nm),folType),ts)
265 | term_of (Free(x,T)) =
266 let val is_meta = checkMeta x
267 val (folType,ts) = type_of T
269 if is_meta then (UVar(make_schematic_var x,folType),ts)
271 (Fun(make_fixed_var x,folType,[]),ts)
273 | term_of (Const(c,T)) = (* impossible to be equality *)
274 let val (folType,ts) = type_of T
276 (Fun(lookup_const c,folType,[]),ts)
278 | term_of (app as (t $ a)) =
279 let val (f,args) = strip_comb app
281 let val (funName,(funType,ts1)) = fun_name_type f
282 val (args',ts2) = ResLib.unzip (map term_of args)
283 val ts3 = ResLib.flat_noDup (ts1::ts2)
285 (Fun(funName,funType,args'),ts3)
287 fun term_of_eq ((Const ("op =", typ)),args) =
288 let val arg_typ = eq_arg_type typ
289 val (args',ts) = ResLib.unzip (map term_of args)
290 val equal_name = lookup_const ("op =")
292 (Fun(equal_name,arg_typ,args'),ResLib.flat_noDup ts)
295 case f of Const ("op =", typ) => term_of_eq (f,args)
296 | Const(_,_) => term_of_aux ()
297 | Free(s,_) => if (checkMeta s) then (raise CLAUSE("Function Not First Order")) else term_of_aux ()
298 | _ => raise CLAUSE("Function Not First Order")
300 | term_of _ = raise CLAUSE("Function Not First Order");
305 fun pred_of_eq ((Const ("op =", typ)),args) =
306 let val arg_typ = eq_arg_type typ
307 val (args',ts) = ResLib.unzip (map term_of args)
308 val equal_name = lookup_const "op ="
310 (Predicate(equal_name,arg_typ,args'),ResLib.flat_noDup ts)
314 (* changed for non-equality predicate *)
315 (* The input "pred" cannot be an equality *)
316 fun pred_of_nonEq (pred,args) =
317 let val (predName,(predType,ts1)) = pred_name_type pred
318 val (args',ts2) = ResLib.unzip (map term_of args)
319 val ts3 = ResLib.flat_noDup (ts1::ts2)
321 (Predicate(predName,predType,args'),ts3)
325 (* Changed for typed equality *)
326 (* First check if the predicate is an equality or not, then call different functions for equality and non-equalities *)
327 fun predicate_of term =
328 let val (pred,args) = strip_comb term
330 case pred of (Const ("op =", _)) => pred_of_eq (pred,args)
331 | _ => pred_of_nonEq (pred,args)
336 fun literals_of_term ((Const("Trueprop",_) $ P),lits_ts) = literals_of_term(P,lits_ts)
337 | literals_of_term ((Const("op |",_) $ P $ Q),(lits,ts)) =
338 let val (lits',ts') = literals_of_term(P,(lits,ts))
340 literals_of_term(Q,(lits',ts'))
342 | literals_of_term ((Const("Not",_) $ P),(lits,ts)) =
343 let val (pred,ts') = predicate_of P
344 val lits' = Literal(false,pred,false) :: lits
345 val ts'' = ResLib.no_rep_app ts ts'
349 | literals_of_term (P,(lits,ts)) =
350 let val (pred,ts') = predicate_of P
351 val lits' = Literal(true,pred,false) :: lits
352 val ts'' = ResLib.no_rep_app ts ts'
356 | literals_of_term _ = raise CLAUSE("Unexpected clause format");
359 fun literals_of_thm thm =
360 let val term_of_thm = prop_of thm
363 literals_of_term (term_of_thm,([],[]))
367 fun sorts_on_typs (_, []) = []
368 | sorts_on_typs ((FOLTVar(indx)), [s]) = [LTVar((make_type_class s) ^ "(" ^ (make_schematic_type_var(string_of_indexname indx)) ^ ")")]
369 | sorts_on_typs ((FOLTVar(indx)), (s::ss))= LTVar((make_type_class s) ^ "(" ^ (make_schematic_type_var(string_of_indexname indx)) ^ ")") :: (sorts_on_typs ((FOLTVar(indx)), ss))
370 | sorts_on_typs ((FOLTFree(x)), [s]) = [LTFree((make_type_class s) ^ "(" ^ (make_fixed_type_var(x)) ^ ")")]
371 | sorts_on_typs ((FOLTFree(x)), (s::ss)) = LTFree((make_type_class s) ^ "(" ^ (make_fixed_type_var(x)) ^ ")") :: (sorts_on_typs ((FOLTFree(x)), ss));
374 fun add_typs_aux [] = ([],[])
375 | add_typs_aux ((FOLTVar(indx),s)::tss) =
376 let val vs = sorts_on_typs (FOLTVar(indx),s)
377 val (vss,fss) = add_typs_aux tss
379 (ResLib.no_rep_app vs vss,fss)
381 | add_typs_aux ((FOLTFree(x),s)::tss) =
382 let val fs = sorts_on_typs (FOLTFree(x),s)
383 val (vss,fss) = add_typs_aux tss
385 (vss,ResLib.no_rep_app fs fss)
389 fun add_typs (Clause cls) =
390 let val ts = #types_sorts cls
398 (** make axiom clauses, hypothesis clauses and conjecture clauses. **)
401 fun replace_dot "." = "_"
406 fun proper_ax_name ax_name =
407 let val chars = explode ax_name
409 implode (map replace_dot chars)
413 fun make_axiom_clause_thm thm (name,number)=
414 let val (lits,types_sorts) = literals_of_thm thm
416 val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
417 val ax_name = proper_ax_name name
419 make_clause(cls_id,ax_name,Axiom,lits,types_sorts,tvar_lits,tfree_lits)
422 fun make_hypothesis_clause_thm thm =
423 let val (lits,types_sorts) = literals_of_thm thm
424 val cls_id = generate_id()
425 val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
427 make_clause(cls_id,"",Hypothesis,lits,types_sorts,tvar_lits,tfree_lits)
431 fun make_conjecture_clause_thm thm =
432 let val (lits,types_sorts) = literals_of_thm thm
433 val cls_id = generate_id()
434 val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
436 make_clause(cls_id,"",Conjecture,lits,types_sorts,tvar_lits,tfree_lits)
440 fun make_axiom_clause term (name,number)=
441 let val (lits,types_sorts) = literals_of_term (term,([],[]))
443 val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
444 val ax_name = proper_ax_name name
446 make_clause(cls_id,ax_name,Axiom,lits,types_sorts,tvar_lits,tfree_lits)
450 fun make_hypothesis_clause term =
451 let val (lits,types_sorts) = literals_of_term (term,([],[]))
452 val cls_id = generate_id()
453 val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
455 make_clause(cls_id,"",Hypothesis,lits,types_sorts,tvar_lits,tfree_lits)
459 fun make_conjecture_clause term =
460 let val (lits,types_sorts) = literals_of_term (term,([],[]))
461 val cls_id = generate_id()
462 val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
464 make_clause(cls_id,"",Conjecture,lits,types_sorts,tvar_lits,tfree_lits)
469 (**** Isabelle arities ****)
471 exception ARCLAUSE of string;
478 datatype arLit = TConsLit of bool * (class * tcons * string list) | TVarLit of bool * (class * string);
480 datatype arityClause =
481 ArityClause of {clause_id: clause_id,
484 premLits: arLit list};
488 | get_TVars n = ("T_" ^ (string_of_int n)) :: get_TVars (n-1);
492 fun pack_sort(_,[]) = raise ARCLAUSE("Empty Sort Found")
493 | pack_sort(tvar, [cls]) = [(make_type_class cls, tvar)]
494 | pack_sort(tvar, cls::srt) = (make_type_class cls,tvar) :: (pack_sort(tvar, srt));
497 fun make_TVarLit (b,(cls,str)) = TVarLit(b,(cls,str));
498 fun make_TConsLit (b,(cls,tcons,tvars)) = TConsLit(b,(make_type_class cls,make_fixed_type_const tcons,tvars));
501 fun make_arity_clause (clause_id,kind,conclLit,premLits) =
502 ArityClause {clause_id = clause_id, kind = kind, conclLit = conclLit, premLits = premLits};
505 fun make_axiom_arity_clause (tcons,(res,args)) =
506 let val cls_id = generate_id()
507 val nargs = length args
508 val tvars = get_TVars nargs
509 val conclLit = make_TConsLit(true,(res,tcons,tvars))
510 val tvars_srts = ResLib.zip tvars args
511 val tvars_srts' = ResLib.flat_noDup(map pack_sort tvars_srts)
512 val false_tvars_srts' = ResLib.pair_ins false tvars_srts'
513 val premLits = map make_TVarLit false_tvars_srts'
515 make_arity_clause (cls_id,Axiom,conclLit,premLits)
520 (**** Isabelle class relations ****)
523 datatype classrelClause =
524 ClassrelClause of {clause_id: clause_id,
526 superclass: class option};
528 fun make_classrelClause (clause_id,subclass,superclass) =
529 ClassrelClause {clause_id = clause_id,subclass = subclass, superclass = superclass};
532 fun make_axiom_classrelClause (subclass,superclass) =
533 let val cls_id = generate_id()
534 val sub = make_type_class subclass
535 val sup = case superclass of NONE => NONE
536 | SOME s => SOME (make_type_class s)
538 make_classrelClause(cls_id,sub,sup)
543 fun classrelClauses_of_aux (sub,[]) = []
544 | classrelClauses_of_aux (sub,(sup::sups)) = make_axiom_classrelClause(sub,SOME sup) :: (classrelClauses_of_aux (sub,sups));
547 fun classrelClauses_of (sub,sups) =
548 case sups of [] => [make_axiom_classrelClause (sub,NONE)]
549 | _ => classrelClauses_of_aux (sub, sups);
553 (***** convert clauses to tptp format *****)
556 fun string_of_clauseID (Clause cls) = clause_prefix ^ (string_of_int (#clause_id cls));
559 fun string_of_kind (Clause cls) = name_of_kind (#kind cls);
561 fun string_of_axiomName (Clause cls) = #axiom_name cls;
563 (****!!!! Changed for typed equality !!!!****)
564 fun wrap_eq_type typ t = eq_typ_wrapper ^"(" ^ t ^ "," ^ typ ^ ")";
567 (****!!!! Changed for typed equality !!!!****)
568 (* Only need to wrap equality's arguments with "typeinfo" if the output clauses are typed && if we specifically ask for types to be included. *)
569 fun string_of_equality (typ,terms) =
570 let val [tstr1,tstr2] = map string_of_term terms
572 if ((!keep_types) andalso (!special_equal)) then
573 "equal(" ^ (wrap_eq_type typ tstr1) ^ "," ^ (wrap_eq_type typ tstr2) ^ ")"
575 "equal(" ^ tstr1 ^ "," ^ tstr2 ^ ")"
580 string_of_term (UVar(x,_)) = x
581 | string_of_term (Fun("equal",typ,terms)) = string_of_equality(typ,terms)
582 | string_of_term (Fun (name,typ,[])) = name
583 | string_of_term (Fun (name,typ,terms)) =
584 let val terms' = map string_of_term terms
586 if (!keep_types) then name ^ (ResLib.list_to_string (typ :: terms'))
587 else name ^ (ResLib.list_to_string terms')
592 (* Changed for typed equality *)
593 (* before output the string of the predicate, check if the predicate corresponds to an equality or not. *)
594 fun string_of_predicate (Predicate("equal",typ,terms)) = string_of_equality(typ,terms)
595 | string_of_predicate (Predicate(name,_,[])) = name
596 | string_of_predicate (Predicate(name,typ,terms)) =
597 let val terms_as_strings = map string_of_term terms
599 if (!keep_types) then name ^ (ResLib.list_to_string (typ :: terms_as_strings))
600 else name ^ (ResLib.list_to_string terms_as_strings)
606 fun tptp_literal (Literal(pol,pred,tag)) =
607 let val pred_string = string_of_predicate pred
608 val tagged_pol = if (tag andalso !tagged) then (if pol then "+++" else "---")
609 else (if pol then "++" else "--")
611 tagged_pol ^ pred_string
616 fun tptp_of_typeLit (LTVar x) = "--" ^ x
617 | tptp_of_typeLit (LTFree x) = "++" ^ x;
620 fun gen_tptp_cls (cls_id,ax_name,knd,lits) =
621 let val ax_str = (if ax_name = "" then "" else ("_" ^ ax_name))
623 "input_clause(" ^ cls_id ^ ax_str ^ "," ^ knd ^ "," ^ lits ^ ")."
627 fun gen_tptp_type_cls (cls_id,knd,tfree_lit,idx) = "input_clause(" ^ cls_id ^ "_tcs" ^ (string_of_int idx) ^ "," ^ knd ^ ",[" ^ tfree_lit ^ "]).";
630 fun tptp_clause_aux (Clause cls) =
631 let val lits = map tptp_literal (#literals cls)
632 val tvar_lits_strs = if (!keep_types) then (map tptp_of_typeLit (#tvar_type_literals cls)) else []
633 val tfree_lits = if (!keep_types) then (map tptp_of_typeLit (#tfree_type_literals cls)) else []
635 (tvar_lits_strs @ lits,tfree_lits)
639 fun tptp_clause cls =
640 let val (lits,tfree_lits) = tptp_clause_aux cls (*"lits" includes the typing assumptions (TVars)*)
641 val cls_id = string_of_clauseID cls
642 val ax_name = string_of_axiomName cls
643 val knd = string_of_kind cls
644 val lits_str = ResLib.list_to_string' lits
645 val cls_str = gen_tptp_cls(cls_id,ax_name,knd,lits_str) fun typ_clss k [] = []
646 | typ_clss k (tfree :: tfrees) =
647 (gen_tptp_type_cls(cls_id,knd,tfree,k)) :: (typ_clss (k+1) tfrees)
649 cls_str :: (typ_clss 0 tfree_lits)
653 fun clause2tptp cls =
654 let val (lits,tfree_lits) = tptp_clause_aux cls (*"lits" includes the typing assumptions (TVars)*)
655 val cls_id = string_of_clauseID cls
656 val ax_name = string_of_axiomName cls
657 val knd = string_of_kind cls
658 val lits_str = ResLib.list_to_string' lits
659 val cls_str = gen_tptp_cls(cls_id,ax_name,knd,lits_str)
665 fun tfree_clause tfree_lit = "input_clause(" ^ "tfree_tcs," ^ "conjecture" ^ ",[" ^ tfree_lit ^ "]).";
668 val tptp_clauses2str = ResLib.list2str_sep delim;
671 fun string_of_arClauseID (ArityClause arcls) = arclause_prefix ^ string_of_int(#clause_id arcls);
674 fun string_of_arLit (TConsLit(b,(c,t,args))) =
675 let val pol = if b then "++" else "--"
676 val arg_strs = (case args of [] => "" | _ => ResLib.list_to_string args)
678 pol ^ c ^ "(" ^ t ^ arg_strs ^ ")"
680 | string_of_arLit (TVarLit(b,(c,str))) =
681 let val pol = if b then "++" else "--"
683 pol ^ c ^ "(" ^ str ^ ")"
687 fun string_of_conclLit (ArityClause arcls) = string_of_arLit (#conclLit arcls);
690 fun strings_of_premLits (ArityClause arcls) = map string_of_arLit (#premLits arcls);
693 fun string_of_arKind (ArityClause arcls) = name_of_kind(#kind arcls);
695 fun tptp_arity_clause arcls =
696 let val arcls_id = string_of_arClauseID arcls
697 val concl_lit = string_of_conclLit arcls
698 val prems_lits = strings_of_premLits arcls
699 val knd = string_of_arKind arcls
700 val all_lits = concl_lit :: prems_lits
702 "input_clause(" ^ arcls_id ^ "," ^ knd ^ "," ^ (ResLib.list_to_string' all_lits) ^ ")."
707 val clrelclause_prefix = "relcls_";
710 fun tptp_classrelLits sub sup =
713 case sup of NONE => "[++" ^ sub ^ tvar ^ "]"
714 | (SOME supcls) => "[--" ^ sub ^ tvar ^ ",++" ^ supcls ^ tvar ^ "]"
718 fun tptp_classrelClause (ClassrelClause cls) =
719 let val relcls_id = clrelclause_prefix ^ string_of_int(#clause_id cls)
720 val sub = #subclass cls
721 val sup = #superclass cls
722 val lits = tptp_classrelLits sub sup
724 "input_clause(" ^ relcls_id ^ ",axiom," ^ lits ^ ")."