src/HOL/Tools/res_clause.ML
changeset 18868 7cfc21ee0370
parent 18863 a113b6839df1
child 18869 00741f7280f7
equal deleted inserted replaced
18867:f8e4322c9567 18868:7cfc21ee0370
     4 
     4 
     5 ML data structure for storing/printing FOL clauses and arity clauses.
     5 ML data structure for storing/printing FOL clauses and arity clauses.
     6 Typed equality is treated differently.
     6 Typed equality is treated differently.
     7 *)
     7 *)
     8 
     8 
     9 (* works for writeoutclasimp on typed *)
       
    10 signature RES_CLAUSE =
     9 signature RES_CLAUSE =
    11   sig
    10   sig
       
    11   exception CLAUSE of string * term
       
    12   type clause and arityClause and classrelClause
       
    13   type fol_type
       
    14   type typ_var
       
    15   type type_literal
       
    16   val add_typs_aux : (typ_var * string list) list -> type_literal list * type_literal list
       
    17   val arity_clause_thy: theory -> arityClause list 
       
    18   val ascii_of : string -> string
       
    19   val bracket_pack : string list -> string
       
    20   val check_var_pairs: ''a * ''b -> (''a * ''b) list -> int
       
    21   val classrel_clauses_thy: theory -> classrelClause list 
       
    22   val clause_eq : clause * clause -> bool
       
    23   val clause_prefix : string 
       
    24   val clause2tptp : clause -> string * string list
       
    25   val const_prefix : string
       
    26   val dfg_write_file: thm list -> string -> (clause list * classrelClause list * arityClause list) -> unit
       
    27   val fixed_var_prefix : string
       
    28   val gen_tptp_cls : int * string * string * string -> string
       
    29   val gen_tptp_type_cls : int * string * string * string * int -> string
       
    30   val get_axiomName : clause ->  string
       
    31   val hash_clause : clause -> int
       
    32   val init : theory -> unit
       
    33   val isMeta : string -> bool
       
    34   val isTaut : clause -> bool
    12   val keep_types : bool ref
    35   val keep_types : bool ref
    13   val special_equal : bool ref
    36   val list_ord : ('a * 'b -> order) -> 'a list * 'b list -> order
    14   val tagged : bool ref
       
    15 
       
    16   exception ARCLAUSE of string
       
    17   exception CLAUSE of string * term
       
    18   type arityClause 
       
    19   type classrelClause
       
    20   type clause
       
    21   val init : theory -> unit
       
    22   val make_axiom_clause : Term.term -> string * int -> clause
    37   val make_axiom_clause : Term.term -> string * int -> clause
    23   val make_conjecture_clauses : term list -> clause list
    38   val make_conjecture_clauses : term list -> clause list
    24   val get_axiomName : clause ->  string
    39   val make_fixed_const : string -> string		
    25   val isTaut : clause -> bool
    40   val make_fixed_type_const : string -> string   
       
    41   val make_fixed_type_var : string -> string
       
    42   val make_fixed_var : string -> string
       
    43   val make_schematic_type_var : string * int -> string
       
    44   val make_schematic_var : string * int -> string
       
    45   val make_type_class : string -> string
       
    46   val mk_fol_type: string * string * fol_type list -> fol_type
       
    47   val mk_typ_var_sort : Term.typ -> typ_var * sort
    26   val num_of_clauses : clause -> int
    48   val num_of_clauses : clause -> int
    27 
    49   val paren_pack : string list -> string
    28   val arity_clause_thy: theory -> arityClause list 
    50   val schematic_var_prefix : string
    29   val classrel_clauses_thy: theory -> classrelClause list 
    51   val special_equal : bool ref
    30 
    52   val string_of_fol_type : fol_type -> string
       
    53   val tconst_prefix : string 
       
    54   val tfree_prefix : string
    31   val tptp_arity_clause : arityClause -> string
    55   val tptp_arity_clause : arityClause -> string
    32   val tptp_classrelClause : classrelClause -> string
    56   val tptp_classrelClause : classrelClause -> string
    33   val tptp_clause : clause -> string list
    57   val tptp_of_typeLit : type_literal -> string
    34   val clause2tptp : clause -> string * string list
       
    35   val tptp_tfree_clause : string -> string
    58   val tptp_tfree_clause : string -> string
    36   val schematic_var_prefix : string
    59   val tptp_write_file: thm list -> string -> (clause list * classrelClause list * arityClause list) -> unit
    37   val fixed_var_prefix : string
       
    38   val tvar_prefix : string
    60   val tvar_prefix : string
    39   val tfree_prefix : string
    61   val types_eq: fol_type list * fol_type list -> (string*string) list * (string*string) list -> bool * ((string*string) list * (string*string) list)
    40   val clause_prefix : string 
    62   val types_ord : fol_type list * fol_type list -> order
    41   val arclause_prefix : string
       
    42   val const_prefix : string
       
    43   val tconst_prefix : string 
       
    44   val class_prefix : string 
       
    45 
       
    46   val union_all : ''a list list -> ''a list
    63   val union_all : ''a list list -> ''a list
    47   val ascii_of : String.string -> String.string
       
    48   val paren_pack : string list -> string
       
    49   val bracket_pack : string list -> string
       
    50   val make_schematic_var : String.string * int -> string
       
    51   val make_fixed_var : String.string -> string
       
    52   val make_schematic_type_var : string * int -> string
       
    53   val make_fixed_type_var : string -> string
       
    54   val make_fixed_const : String.string -> string		
       
    55   val make_fixed_type_const : String.string -> string   
       
    56   val make_type_class : String.string -> string
       
    57   val isMeta : String.string -> bool
       
    58   
       
    59   type typ_var
       
    60   val mk_typ_var_sort : Term.typ -> typ_var * sort
       
    61   type type_literal
       
    62   val add_typs_aux : (typ_var * string list) list -> type_literal list * type_literal list
       
    63   val gen_tptp_cls : int * string * string * string -> string
       
    64   val gen_tptp_type_cls : int * string * string * string * int -> string
       
    65   val tptp_of_typeLit : type_literal -> string
       
    66 
       
    67   (*for hashing*)
       
    68   val clause_eq : clause * clause -> bool
       
    69   val hash_clause : clause -> int
       
    70 
       
    71   val list_ord : ('a * 'b -> order) -> 'a list * 'b list -> order
       
    72   type fol_type
       
    73   val types_ord : fol_type list * fol_type list -> order
       
    74   val string_of_fol_type : fol_type -> string
       
    75   val mk_fol_type: string * string * fol_type list -> fol_type
       
    76   val types_eq: fol_type list * fol_type list -> 
       
    77         (string*string) list * (string*string) list -> 
       
    78         bool * ((string*string) list * (string*string) list)
       
    79   val check_var_pairs: ''a * ''b -> (''a * ''b) list -> int
       
    80 
       
    81   val dfg_write_file: thm list -> string -> 
       
    82         (clause list * classrelClause list * arityClause list) -> unit
       
    83   val tptp_write_file: thm list -> string -> 
       
    84         (clause list * classrelClause list * arityClause list) -> unit
       
    85   val writeln_strs: TextIO.outstream -> TextIO.vector list -> unit
    64   val writeln_strs: TextIO.outstream -> TextIO.vector list -> unit
    86   end;
    65   end;
    87 
    66 
    88 structure ResClause : RES_CLAUSE =
    67 structure ResClause : RES_CLAUSE =
    89 struct
    68 struct
   359   | term_of app = 
   338   | term_of app = 
   360       let val (f,args) = strip_comb app
   339       let val (f,args) = strip_comb app
   361 	  val (funName,(contys,ts1)) = fun_name_type f args
   340 	  val (funName,(contys,ts1)) = fun_name_type f args
   362 	  val (args',ts2) = terms_of args
   341 	  val (args',ts2) = terms_of args
   363       in
   342       in
   364 	  (Fun(funName,contys,args'), 
   343 	  (Fun(funName,contys,args'), union_all (ts1::ts2))
   365 	   (union_all (ts1::ts2)))
       
   366       end
   344       end
   367 and terms_of ts = ListPair.unzip (map term_of ts)
   345 and terms_of ts = ListPair.unzip (map term_of ts)
   368 
   346 
   369 (*Create a predicate value, again accumulating sort constraints.*)    
   347 (*Create a predicate value, again accumulating sort constraints.*)    
   370 fun pred_of (Const("op =", typ), args) =
   348 fun pred_of (Const("op =", typ), args) =
   669 exception ARCLAUSE of string;
   647 exception ARCLAUSE of string;
   670  
   648  
   671 type class = string; 
   649 type class = string; 
   672 type tcons = string; 
   650 type tcons = string; 
   673 
   651 
   674 datatype arLit = TConsLit of bool * (class * tcons * string list) | TVarLit of bool * (class * string);
   652 datatype arLit = TConsLit of bool * (class * tcons * string list)
       
   653                | TVarLit of bool * (class * string);
   675  
   654  
   676 datatype arityClause =  
   655 datatype arityClause =  
   677 	 ArityClause of {clause_id: clause_id,
   656 	 ArityClause of {clause_id: clause_id,
   678 	  	         axiom_name: axiom_name,
   657 	  	         axiom_name: axiom_name,
   679 			 kind: kind,
   658 			 kind: kind,
   686 
   665 
   687 fun pack_sort(_,[])  = []
   666 fun pack_sort(_,[])  = []
   688   | pack_sort(tvar, "HOL.type"::srt) = pack_sort(tvar, srt)   (*IGNORE sort "type"*)
   667   | pack_sort(tvar, "HOL.type"::srt) = pack_sort(tvar, srt)   (*IGNORE sort "type"*)
   689   | pack_sort(tvar, cls::srt) =  (make_type_class cls, tvar) :: pack_sort(tvar, srt);
   668   | pack_sort(tvar, cls::srt) =  (make_type_class cls, tvar) :: pack_sort(tvar, srt);
   690     
   669     
   691 fun make_TVarLit (b,(cls,str)) = TVarLit(b,(cls,str));
   670 fun make_TVarLit (b, (cls,str)) = TVarLit(b, (cls,str));
   692 fun make_TConsLit (b,(cls,tcons,tvars)) = TConsLit(b,(make_type_class cls,make_fixed_type_const tcons,tvars));
   671 fun make_TConsLit (b, (cls,tcons,tvars)) = 
       
   672       TConsLit(b, (make_type_class cls, make_fixed_type_const tcons, tvars));
   693 
   673 
   694 (*Arity of type constructor tcon :: (arg1,...,argN)res*)
   674 (*Arity of type constructor tcon :: (arg1,...,argN)res*)
   695 fun make_axiom_arity_clause (tcons, n, (res,args)) =
   675 fun make_axiom_arity_clause (tcons, n, (res,args)) =
   696    let val nargs = length args
   676    let val nargs = length args
   697        val tvars = gen_TVars nargs
   677        val tvars = gen_TVars nargs
   715 
   695 
   716 
   696 
   717 (**** Isabelle class relations ****)
   697 (**** Isabelle class relations ****)
   718 
   698 
   719 datatype classrelClause = 
   699 datatype classrelClause = 
   720 	 ClassrelClause of {clause_id: clause_id,
   700 	 ClassrelClause of {axiom_name: axiom_name,
   721 			    subclass: class,
   701 			    subclass: class,
   722 			    superclass: class};
   702 			    superclass: class};
   723 
   703 
   724 fun make_axiom_classrelClause n subclass superclass =
   704 fun make_axiom_classrelClause n subclass superclass =
   725   ClassrelClause {clause_id = n,
   705   ClassrelClause {axiom_name = clrelclause_prefix ^ ascii_of subclass ^ 
   726                   subclass = subclass, superclass = superclass};
   706                                 "_" ^ Int.toString n,
       
   707                   subclass = make_type_class subclass, 
       
   708                   superclass = make_type_class superclass};
   727 
   709 
   728 fun classrelClauses_of_aux n sub [] = []
   710 fun classrelClauses_of_aux n sub [] = []
   729   | classrelClauses_of_aux n sub ("HOL.type"::sups) = (*Should be ignored*)
   711   | classrelClauses_of_aux n sub ("HOL.type"::sups) = (*Should be ignored*)
   730       classrelClauses_of_aux n sub sups
   712       classrelClauses_of_aux n sub sups
   731   | classrelClauses_of_aux n sub (sup::sups) =
   713   | classrelClauses_of_aux n sub (sup::sups) =
   732       ClassrelClause {clause_id = n, subclass = sub, superclass = sup} 
   714       make_axiom_classrelClause n sub sup :: classrelClauses_of_aux (n+1) sub sups;
   733       :: classrelClauses_of_aux (n+1) sub sups;
       
   734 
   715 
   735 fun classrelClauses_of (sub,sups) = classrelClauses_of_aux 0 sub sups;
   716 fun classrelClauses_of (sub,sups) = classrelClauses_of_aux 0 sub sups;
   736 
   717 
   737 
   718 val classrel_of = #2 o #classes o Type.rep_tsig o Sign.tsig_of;
   738 (***** Isabelle arities *****)
   719 
       
   720 fun classrel_clauses_classrel (C: Sorts.classes) =
       
   721   map classrelClauses_of (Graph.dest C);
       
   722 
       
   723 val classrel_clauses_thy = List.concat o classrel_clauses_classrel o classrel_of;
       
   724 
       
   725 
       
   726 (** Isabelle arities **)
   739 
   727 
   740 fun arity_clause _ (tcons, []) = []
   728 fun arity_clause _ (tcons, []) = []
   741   | arity_clause n (tcons, ("HOL.type",_)::ars) =  (*Should be ignored*)
   729   | arity_clause n (tcons, ("HOL.type",_)::ars) =  (*Should be ignored*)
   742       arity_clause n (tcons,ars)
   730       arity_clause n (tcons,ars)
   743   | arity_clause n (tcons, ar::ars) =
   731   | arity_clause n (tcons, ar::ars) =
   751 fun arity_clause_thy thy =
   739 fun arity_clause_thy thy =
   752   let val arities = #arities (Type.rep_tsig (Sign.tsig_of thy))
   740   let val arities = #arities (Type.rep_tsig (Sign.tsig_of thy))
   753   in multi_arity_clause (Symtab.dest arities) end;
   741   in multi_arity_clause (Symtab.dest arities) end;
   754 
   742 
   755 
   743 
   756 (* Isabelle classes *)
   744 (**** Find occurrences of predicates in clauses ****)
   757 
   745 
   758 type classrelClauses = classrelClause list Symtab.table;
   746 (*FIXME: multiple-arity checking doesn't work, as update_new is the wrong 
   759 
   747   function (it flags repeated declarations of a function, even with the same arity)*)
   760 val classrel_of = #2 o #classes o Type.rep_tsig o Sign.tsig_of;
   748 
   761 
   749 fun update_many (tab, keypairs) = foldl (uncurry Symtab.update) tab keypairs;
   762 fun classrel_clauses_classrel (C: Sorts.classes) =
   750 
   763   map classrelClauses_of (Graph.dest C);
   751 fun add_predicate_preds (Predicate(pname,tys,tms), preds) = 
   764 
   752   if pname = "equal" then preds (*equality is built-in and requires no declaration*)
   765 val classrel_clauses_thy = List.concat o classrel_clauses_classrel o classrel_of;
   753   else Symtab.update (pname, length tys + length tms) preds
   766 
   754 
       
   755 fun add_literal_preds (Literal(_,pred,_), preds) = add_predicate_preds (pred,preds)
       
   756 
       
   757 fun add_type_sort_preds ((FOLTVar indx,s), preds) = 
       
   758       update_many (preds, map pred_of_sort (sorts_on_typs (FOLTVar indx, s)))
       
   759   | add_type_sort_preds ((FOLTFree x,s), preds) =
       
   760       update_many (preds, map pred_of_sort (sorts_on_typs (FOLTFree x, s)));
       
   761 
       
   762 fun add_clause_preds (Clause {literals, types_sorts, ...}, preds) =
       
   763   foldl add_literal_preds (foldl add_type_sort_preds preds types_sorts) literals
       
   764   handle Symtab.DUP a => raise ERROR ("predicate " ^ a ^ " has multiple arities")
       
   765 
       
   766 fun add_classrelClause_preds (ClassrelClause {subclass,superclass,...}, preds) =
       
   767   Symtab.update (subclass,1) (Symtab.update (superclass,1) preds);
       
   768 
       
   769 fun add_arityClause_preds (ArityClause {conclLit,...}, preds) =
       
   770   let val TConsLit(_, (tclass, _, _)) = conclLit
       
   771   in  Symtab.update (tclass,1) preds  end;
       
   772 
       
   773 fun preds_of_clauses clauses clsrel_clauses arity_clauses = 
       
   774   Symtab.dest
       
   775     (foldl add_classrelClause_preds 
       
   776       (foldl add_arityClause_preds
       
   777         (foldl add_clause_preds Symtab.empty clauses)
       
   778         arity_clauses)
       
   779       clsrel_clauses)
       
   780 
       
   781 (*** Find occurrences of functions in clauses ***)
       
   782 
       
   783 fun add_foltype_funcs (AtomV _, funcs) = funcs
       
   784   | add_foltype_funcs (AtomF a, funcs) = Symtab.update (a,0) funcs
       
   785   | add_foltype_funcs (Comp(a,tys), funcs) = 
       
   786       foldl add_foltype_funcs (Symtab.update (a, length tys) funcs) tys;
       
   787 
       
   788 fun add_folterm_funcs (UVar _, funcs) = funcs
       
   789   | add_folterm_funcs (Fun(a,tys,[]), funcs) = Symtab.update (a,0) funcs
       
   790       (*A constant is a special case: it has no type argument even if overloaded*)
       
   791   | add_folterm_funcs (Fun(a,tys,tms), funcs) = 
       
   792       foldl add_foltype_funcs 
       
   793 	    (foldl add_folterm_funcs (Symtab.update (a, length tys + length tms) funcs) 
       
   794 	           tms) 
       
   795 	    tys
       
   796 
       
   797 fun add_predicate_funcs (Predicate(_,tys,tms), funcs) = 
       
   798     foldl add_foltype_funcs (foldl add_folterm_funcs funcs tms) tys;
       
   799 
       
   800 fun add_literal_funcs (Literal(_,pred,_), funcs) = add_predicate_funcs (pred,funcs)
       
   801 
       
   802 fun add_arityClause_funcs (ArityClause {conclLit,...}, funcs) =
       
   803   let val TConsLit(_, (_, tcons, tvars)) = conclLit
       
   804   in  Symtab.update (tcons, length tvars) funcs  end;
       
   805 
       
   806 fun add_clause_funcs (Clause {literals, ...}, funcs) =
       
   807   foldl add_literal_funcs funcs literals
       
   808   handle Symtab.DUP a => raise ERROR ("function " ^ a ^ " has multiple arities")
       
   809 
       
   810 fun funcs_of_clauses clauses arity_clauses = 
       
   811   Symtab.dest (foldl add_arityClause_funcs 
       
   812                      (foldl add_clause_funcs Symtab.empty clauses)
       
   813                      arity_clauses)
       
   814 
       
   815 
       
   816 (**** String-oriented operations ****)
   767 
   817 
   768 fun wrap_eq_type typ t = eq_typ_wrapper ^"(" ^ t ^ "," ^ typ ^ ")";
   818 fun wrap_eq_type typ t = eq_typ_wrapper ^"(" ^ t ^ "," ^ typ ^ ")";
   769 
   819 
   770 (*Only need to wrap equality's arguments with "typeinfo" if the output clauses are typed 
   820 (*Only need to wrap equality's arguments with "typeinfo" if the output clauses are typed 
   771  and if we specifically ask for types to be included.   *)
   821  and if we specifically ask for types to be included.   *)
   801 
   851 
   802 (*Write a list of strings to a file*)
   852 (*Write a list of strings to a file*)
   803 fun writeln_strs os = List.app (fn s => TextIO.output (os,s));
   853 fun writeln_strs os = List.app (fn s => TextIO.output (os,s));
   804 
   854 
   805     
   855     
   806 
   856 (**** Producing DFG files ****)
   807 (********************************)
       
   808 (* Code for producing DFG files *)
       
   809 (********************************)
       
   810 
   857 
   811 (*Attach sign in DFG syntax: false means negate.*)
   858 (*Attach sign in DFG syntax: false means negate.*)
   812 fun dfg_sign true s = s
   859 fun dfg_sign true s = s
   813   | dfg_sign false s = "not(" ^ s ^ ")"  
   860   | dfg_sign false s = "not(" ^ s ^ ")"  
   814 
   861 
   815 fun dfg_literal (Literal(pol,pred,tag)) = dfg_sign pol (string_of_predicate pred)
   862 fun dfg_literal (Literal(pol,pred,tag)) = dfg_sign pol (string_of_predicate pred)
   816 
   863 
   817 fun dfg_of_typeLit (LTVar (s,ty)) = "not(" ^ s ^ "(" ^ ty ^ "))"
   864 fun dfg_of_typeLit (LTVar (s,ty)) = "not(" ^ s ^ "(" ^ ty ^ "))"
   818   | dfg_of_typeLit (LTFree (s,ty)) = s ^ "(" ^ ty ^ ")";
   865   | dfg_of_typeLit (LTFree (s,ty)) = s ^ "(" ^ ty ^ ")";
   819  
   866  
   820 (*Make the string of universal quantifiers for a clause*)
   867 (*Enclose the clause body by quantifiers, if necessary*)
   821 fun forall_open ([],[]) = ""
   868 fun dfg_forall [] body = body  
   822   | forall_open (vars,tvars) = "forall([" ^ (commas (tvars@vars))^ "],\n"
   869   | dfg_forall vars body = "forall([" ^ commas vars ^ "],\n" ^ body ^ ")"
   823 
   870 
   824 fun forall_close ([],[]) = ""
   871 fun gen_dfg_cls (cls_id, ax_name, knd, lits, vars) = 
   825   | forall_close (vars,tvars) = ")"
   872     "clause( %(" ^ knd ^ ")\n" ^ 
   826 
   873     dfg_forall vars ("or(" ^ lits ^ ")") ^ ",\n" ^ 
   827 fun gen_dfg_cls (cls_id,ax_name,knd,lits,tvars,vars) = 
       
   828     "clause( %(" ^ knd ^ ")\n" ^ forall_open(vars,tvars) ^ 
       
   829     "or(" ^ lits ^ ")" ^ forall_close(vars,tvars) ^ ",\n" ^ 
       
   830     string_of_clausename (cls_id,ax_name) ^  ").\n\n";
   874     string_of_clausename (cls_id,ax_name) ^  ").\n\n";
   831 
   875 
   832 (*FIXME: UNUSED*)
   876 fun dfg_clause_aux (Clause{literals, tvar_type_literals, tfree_type_literals, ...}) = 
   833 fun gen_dfg_type_cls (cls_id,ax_name,knd,tfree_lit,idx,tvars,vars) = 
   877   let val lits = map dfg_literal literals
   834     "clause( %(" ^ knd ^ ")\n" ^ forall_open(vars,tvars) ^ 
       
   835     "or( " ^ tfree_lit ^ ")" ^ forall_close(vars,tvars) ^ ",\n" ^ 
       
   836     string_of_type_clsname (cls_id,ax_name,idx) ^  ").\n\n";
       
   837 
       
   838 fun dfg_clause_aux (Clause cls) = 
       
   839   let val lits = map dfg_literal (#literals cls)
       
   840       val tvar_lits_strs = 
   878       val tvar_lits_strs = 
   841 	  if !keep_types then map dfg_of_typeLit (#tvar_type_literals cls) 
   879 	  if !keep_types then map dfg_of_typeLit tvar_type_literals
   842 	  else []
   880 	  else []
   843       val tfree_lits =
   881       val tfree_lits =
   844           if !keep_types then map dfg_of_typeLit (#tfree_type_literals cls)
   882           if !keep_types then map dfg_of_typeLit tfree_type_literals
   845           else []
   883           else []
   846   in
   884   in
   847       (tvar_lits_strs @ lits, tfree_lits)
   885       (tvar_lits_strs @ lits, tfree_lits)
   848   end; 
   886   end; 
   849 
   887 
   850 
       
   851 fun dfg_folterms (Literal(pol,pred,tag)) = 
   888 fun dfg_folterms (Literal(pol,pred,tag)) = 
   852   let val Predicate (_, _, folterms) = pred
   889   let val Predicate (_, _, folterms) = pred
   853   in  folterms  end
   890   in  folterms  end
   854 
   891 
   855 fun get_uvars (UVar(a,typ)) = [a] 
   892 fun get_uvars (UVar(a,typ)) = [a] 
   856 |   get_uvars (Fun (_,typ,tlist)) = union_all(map get_uvars tlist)
   893   | get_uvars (Fun (_,typ,tlist)) = union_all(map get_uvars tlist)
   857 
   894 
   858 fun is_uvar (UVar _) = true
   895 fun dfg_vars (Clause {literals,...}) =
   859 |   is_uvar (Fun _) = false;
   896     let val folterms = List.concat (map dfg_folterms literals)
   860 
       
   861 fun uvar_name (UVar(a,_)) = a
       
   862 |   uvar_name (Fun (a,_,_)) = raise CLAUSE("Not a variable", Const(a,dummyT));
       
   863 
       
   864 fun dfg_vars (Clause cls) =
       
   865     let val lits = #literals cls
       
   866         val folterms = List.concat (map dfg_folterms lits)
       
   867     in 
   897     in 
   868         union_all(map get_uvars folterms)
   898         union_all(map get_uvars folterms)
   869     end
   899     end
   870 
       
   871 fun string_of_predname (Predicate("equal",_,terms)) = "EQUALITY"
       
   872   | string_of_predname (Predicate(name,_,terms)) = name
       
   873 
       
   874 fun dfg_pred (Literal(pol,pred,tag)) ax_name = 
       
   875     (string_of_predname pred) ^ " " ^ ax_name
       
   876 
   900 
   877 fun clause2dfg (cls as Clause{axiom_name,clause_id,kind,types_sorts,...}) =
   901 fun clause2dfg (cls as Clause{axiom_name,clause_id,kind,types_sorts,...}) =
   878     let val (lits,tfree_lits) = dfg_clause_aux cls 
   902     let val (lits,tfree_lits) = dfg_clause_aux cls 
   879             (*"lits" includes the typing assumptions (TVars)*)
   903             (*"lits" includes the typing assumptions (TVars)*)
   880         val vars = dfg_vars cls
   904         val vars = dfg_vars cls
   881         val tvars = get_tvar_strs types_sorts
   905         val tvars = get_tvar_strs types_sorts
   882 	val knd = name_of_kind kind
   906 	val knd = name_of_kind kind
   883 	val lits_str = commas lits
   907 	val lits_str = commas lits
   884 	val cls_str = gen_dfg_cls(clause_id,axiom_name,knd,lits_str,tvars,vars) 
   908 	val cls_str = gen_dfg_cls(clause_id, axiom_name, knd, lits_str, tvars@vars) 
   885     in (cls_str, tfree_lits) end;
   909     in (cls_str, tfree_lits) end;
   886 
   910 
   887 fun string_of_arity (name, num) =  "(" ^ name ^ "," ^ Int.toString num ^ ")"
   911 fun string_of_arity (name, num) =  "(" ^ name ^ "," ^ Int.toString num ^ ")"
   888 
   912 
   889 fun string_of_preds [] = ""
   913 fun string_of_preds [] = ""
   893   | string_of_funcs funcs = "functions[" ^ commas(map string_of_arity funcs) ^ "].\n" ;
   917   | string_of_funcs funcs = "functions[" ^ commas(map string_of_arity funcs) ^ "].\n" ;
   894 
   918 
   895 fun string_of_symbols predstr funcstr = 
   919 fun string_of_symbols predstr funcstr = 
   896   "list_of_symbols.\n" ^ predstr  ^ funcstr  ^ "end_of_list.\n\n";
   920   "list_of_symbols.\n" ^ predstr  ^ funcstr  ^ "end_of_list.\n\n";
   897 
   921 
   898 fun write_axioms (out, strs) = 
       
   899   (TextIO.output (out, "list_of_clauses(axioms,cnf).\n");
       
   900    writeln_strs out strs;
       
   901    TextIO.output (out, "end_of_list.\n\n"));
       
   902 
       
   903 fun write_conjectures (out, strs) = 
       
   904   (TextIO.output (out, "list_of_clauses(conjectures,cnf).\n");
       
   905    writeln_strs out strs;
       
   906    TextIO.output (out, "end_of_list.\n\n"));
       
   907 
       
   908 fun string_of_start name = "begin_problem(" ^ name ^ ").\n\n";
   922 fun string_of_start name = "begin_problem(" ^ name ^ ").\n\n";
   909 
   923 
   910 fun string_of_descrip name = 
   924 fun string_of_descrip name = 
   911   "list_of_descriptions.\nname({*" ^ name ^ "*}).\nauthor({*Isabelle*}).\nstatus(unknown).\ndescription({*auto-generated*}).\nend_of_list.\n\n"
   925   "list_of_descriptions.\nname({*" ^ name ^ 
       
   926   "*}).\nauthor({*Isabelle*}).\nstatus(unknown).\ndescription({*auto-generated*}).\nend_of_list.\n\n"
   912 
   927 
   913 fun dfg_tfree_clause tfree_lit =
   928 fun dfg_tfree_clause tfree_lit =
   914   "clause( %(conjecture)\n" ^ "or( " ^ tfree_lit ^ "),\n" ^ "tfree_tcs" ^ ").\n\n"
   929   "clause( %(conjecture)\n" ^ "or( " ^ tfree_lit ^ "),\n" ^ "tfree_tcs" ^ ").\n\n"
   915 
   930 
   916 
       
   917 (*** Find all occurrences of predicates in types, terms, literals... ***)
       
   918 
       
   919 (*FIXME: multiple-arity checking doesn't work, as update_new is the wrong 
       
   920   function (it flags repeated declarations of a function, even with the same arity)*)
       
   921 
       
   922 fun update_many (tab, keypairs) = foldl (uncurry Symtab.update) tab keypairs;
       
   923 
       
   924 fun add_predicate_preds (Predicate(pname,tys,tms), preds) = 
       
   925   if pname = "equal" then preds (*equality is built-in and requires no declaration*)
       
   926   else Symtab.update (pname, length tys + length tms) preds
       
   927 
       
   928 fun add_literal_preds (Literal(_,pred,_), preds) = add_predicate_preds (pred,preds)
       
   929 
       
   930 fun add_type_sort_preds ((FOLTVar indx,s), preds) = 
       
   931       update_many (preds, map pred_of_sort (sorts_on_typs (FOLTVar indx, s)))
       
   932   | add_type_sort_preds ((FOLTFree x,s), preds) =
       
   933       update_many (preds, map pred_of_sort (sorts_on_typs (FOLTFree x, s)));
       
   934 
       
   935 fun add_clause_preds (Clause {literals, types_sorts, ...}, preds) =
       
   936   foldl add_literal_preds (foldl add_type_sort_preds preds types_sorts) literals
       
   937   handle Symtab.DUP a => raise ERROR ("predicate " ^ a ^ " has multiple arities")
       
   938 
       
   939 val preds_of_clauses = Symtab.dest o (foldl add_clause_preds Symtab.empty)
       
   940 
       
   941 
       
   942 (*** Find all occurrences of functions in types, terms, literals... ***)
       
   943 
       
   944 fun add_foltype_funcs (AtomV _, funcs) = funcs
       
   945   | add_foltype_funcs (AtomF a, funcs) = Symtab.update (a,0) funcs
       
   946   | add_foltype_funcs (Comp(a,tys), funcs) = 
       
   947       foldl add_foltype_funcs (Symtab.update (a, length tys) funcs) tys;
       
   948 
       
   949 fun add_folterm_funcs (UVar _, funcs) = funcs
       
   950   | add_folterm_funcs (Fun(a,tys,[]), funcs) = Symtab.update (a,0) funcs
       
   951       (*A constant is a special case: it has no type argument even if overloaded*)
       
   952   | add_folterm_funcs (Fun(a,tys,tms), funcs) = 
       
   953       foldl add_foltype_funcs 
       
   954 	    (foldl add_folterm_funcs (Symtab.update (a, length tys + length tms) funcs) 
       
   955 	           tms) 
       
   956 	    tys
       
   957 
       
   958 fun add_predicate_funcs (Predicate(_,tys,tms), funcs) = 
       
   959     foldl add_foltype_funcs (foldl add_folterm_funcs funcs tms) tys;
       
   960 
       
   961 fun add_literal_funcs (Literal(_,pred,_), funcs) = add_predicate_funcs (pred,funcs)
       
   962 
       
   963 fun add_clause_funcs (Clause {literals, ...}, funcs) =
       
   964   foldl add_literal_funcs funcs literals
       
   965   handle Symtab.DUP a => raise ERROR ("function " ^ a ^ " has multiple arities")
       
   966 
       
   967 val funcs_of_clauses = Symtab.dest o (foldl add_clause_funcs Symtab.empty)
       
   968 
       
   969 fun string_of_arClauseID (ArityClause {clause_id,axiom_name,...}) =
   931 fun string_of_arClauseID (ArityClause {clause_id,axiom_name,...}) =
   970     arclause_prefix ^ ascii_of axiom_name ^ "_" ^ Int.toString clause_id;
   932     arclause_prefix ^ ascii_of axiom_name ^ "_" ^ Int.toString clause_id;
   971 
       
   972 fun string_of_arKind (ArityClause arcls) = name_of_kind(#kind arcls);
       
   973 
   933 
   974 fun dfg_of_arLit (TConsLit(pol,(c,t,args))) =
   934 fun dfg_of_arLit (TConsLit(pol,(c,t,args))) =
   975       dfg_sign pol (c ^ "(" ^ t ^ paren_pack args ^ ")")
   935       dfg_sign pol (c ^ "(" ^ t ^ paren_pack args ^ ")")
   976   | dfg_of_arLit (TVarLit(pol,(c,str))) =
   936   | dfg_of_arLit (TVarLit(pol,(c,str))) =
   977       dfg_sign pol (c ^ "(" ^ str ^ ")")
   937       dfg_sign pol (c ^ "(" ^ str ^ ")")
   978     
   938     
   979 fun dfg_of_conclLit (ArityClause arcls) = dfg_of_arLit (#conclLit arcls);
       
   980      
       
   981 fun dfg_of_premLits (ArityClause arcls) = map dfg_of_arLit (#premLits arcls);
       
   982 		
       
   983 fun dfg_classrelLits sub sup = 
   939 fun dfg_classrelLits sub sup = 
   984     let val tvar = "(T)"
   940     let val tvar = "(T)"
   985     in 
   941     in 
   986 	"not(" ^ sub ^ tvar ^ "), " ^ sup ^ tvar
   942 	"not(" ^ sub ^ tvar ^ "), " ^ sup ^ tvar
   987     end;
   943     end;
   988 
   944 
   989 fun dfg_classrelClause (ClassrelClause {clause_id,subclass,superclass,...}) =
   945 fun dfg_classrelClause (ClassrelClause {axiom_name,subclass,superclass,...}) =
   990   let val relcls_id = clrelclause_prefix ^ ascii_of subclass ^ "_" ^ 
   946   "clause(forall([T],\nor( " ^ dfg_classrelLits subclass superclass ^ ")),\n" ^
   991 		      Int.toString clause_id
   947   axiom_name ^ ").\n\n";
   992       val lits = dfg_classrelLits (make_type_class subclass) (make_type_class superclass)
   948       
       
   949 fun dfg_arity_clause (arcls as ArityClause{kind,conclLit,premLits,...}) = 
       
   950   let val arcls_id = string_of_arClauseID arcls
       
   951       val knd = name_of_kind kind
       
   952       val TConsLit(_, (_,_,tvars)) = conclLit
       
   953       val lits = map dfg_of_arLit (conclLit :: premLits)
   993   in
   954   in
   994       "clause(\nor( " ^ lits ^ ")),\n" ^
   955       "clause( %(" ^ knd ^ ")\n" ^ 
   995       relcls_id ^ ").\n\n"
   956       dfg_forall tvars ("or( " ^ commas lits ^ ")") ^ ",\n" ^
   996   end; 
   957       arcls_id ^ ").\n\n"
   997 
       
   998 fun dfg_arity_clause arcls = 
       
   999   let val arcls_id = string_of_arClauseID arcls
       
  1000       val concl_lit = dfg_of_conclLit arcls
       
  1001       val prems_lits = dfg_of_premLits arcls
       
  1002       val knd = string_of_arKind arcls
       
  1003       val all_lits = concl_lit :: prems_lits
       
  1004   in
       
  1005       "clause( %(" ^ knd ^ ")\n" ^  "or( " ^ bracket_pack all_lits ^ ")),\n" ^
       
  1006        arcls_id ^ ").\n\n"
       
  1007   end;
   958   end;
  1008 
   959 
  1009 (* write out a subgoal in DFG format to the file "xxxx_N"*)
   960 (* write out a subgoal in DFG format to the file "xxxx_N"*)
  1010 fun dfg_write_file ths filename (axclauses,classrel_clauses,arity_clauses) = 
   961 fun dfg_write_file ths filename (axclauses,classrel_clauses,arity_clauses) = 
  1011   let 
   962   let 
  1012     val conjectures = make_conjecture_clauses (map prop_of ths)
   963     val conjectures = make_conjecture_clauses (map prop_of ths)
  1013     val (dfg_clss,tfree_litss) = ListPair.unzip (map clause2dfg conjectures)
   964     val (dfg_clss, tfree_litss) = ListPair.unzip (map clause2dfg conjectures)
  1014     val classrel_cls = map dfg_classrelClause classrel_clauses
       
  1015     val arity_cls = map dfg_arity_clause arity_clauses
       
  1016     val clss = conjectures @ axclauses
   965     val clss = conjectures @ axclauses
  1017     val funcs = (funcs_of_clauses clss)
   966     val funcs = funcs_of_clauses clss arity_clauses
  1018     and preds = (preds_of_clauses clss)
   967     and preds = preds_of_clauses clss classrel_clauses arity_clauses
  1019     val out = TextIO.openOut filename
   968     val out = TextIO.openOut filename
  1020     and probname = Path.pack (Path.base (Path.unpack filename))
   969     and probname = Path.pack (Path.base (Path.unpack filename))
  1021     val axstrs_tfrees = (map clause2dfg axclauses)
   970     val (axstrs, _) = ListPair.unzip (map clause2dfg axclauses)
  1022     val (axstrs, _) = ListPair.unzip axstrs_tfrees
       
  1023     val tfree_clss = map dfg_tfree_clause (union_all tfree_litss) 
   971     val tfree_clss = map dfg_tfree_clause (union_all tfree_litss) 
  1024     val funcstr = string_of_funcs funcs
       
  1025     val predstr = string_of_preds preds
       
  1026   in
   972   in
  1027       TextIO.output (out, string_of_start probname); 
   973     TextIO.output (out, string_of_start probname); 
  1028       TextIO.output (out, string_of_descrip probname); 
   974     TextIO.output (out, string_of_descrip probname); 
  1029       TextIO.output (out, string_of_symbols funcstr predstr); 
   975     TextIO.output (out, string_of_symbols (string_of_funcs funcs) (string_of_preds preds)); 
  1030       write_axioms (out, axstrs); 
   976     TextIO.output (out, "list_of_clauses(axioms,cnf).\n");
  1031       write_conjectures (out, tfree_clss@dfg_clss); 
   977     writeln_strs out axstrs;
  1032       TextIO.output (out, "end_problem.\n");
   978     List.app (curry TextIO.output out o dfg_classrelClause) classrel_clauses;
  1033       TextIO.closeOut out
   979     List.app (curry TextIO.output out o dfg_arity_clause) arity_clauses;
       
   980     TextIO.output (out, "end_of_list.\n\nlist_of_clauses(conjectures,cnf).\n");
       
   981     writeln_strs out tfree_clss;
       
   982     writeln_strs out dfg_clss;
       
   983     TextIO.output (out, "end_of_list.\n\nend_problem.\n");
       
   984     TextIO.closeOut out
  1034   end;
   985   end;
  1035 
   986 
  1036 
   987 
  1037 (********************************)
   988 (********************************)
  1038 (* code to produce TPTP files   *)
   989 (* code to produce TPTP files   *)
  1039 (********************************)
   990 (********************************)
  1040 
   991 
  1041 fun tptp_literal (Literal(pol,pred,tag)) =
   992 
       
   993 (*Attach sign in TPTP syntax: false means negate.*)
       
   994 fun tptp_sign true s = "++" ^ s
       
   995   | tptp_sign false s = "--" ^ s
       
   996 
       
   997 fun tptp_literal (Literal(pol,pred,tag)) =  (*FIXME REMOVE TAGGING*)
  1042     let val pred_string = string_of_predicate pred
   998     let val pred_string = string_of_predicate pred
  1043 	val tagged_pol = 
   999 	val tagged_pol = 
  1044 	      if (tag andalso !tagged) then (if pol then "+++" else "---")
  1000 	      if (tag andalso !tagged) then (if pol then "+++" else "---")
  1045 	      else (if pol then "++" else "--")
  1001 	      else (if pol then "++" else "--")
  1046      in
  1002      in
  1105 fun tptp_tfree_clause tfree_lit =
  1061 fun tptp_tfree_clause tfree_lit =
  1106     "input_clause(" ^ "tfree_tcs," ^ "conjecture" ^ ",[" ^ tfree_lit ^ "]).\n";
  1062     "input_clause(" ^ "tfree_tcs," ^ "conjecture" ^ ",[" ^ tfree_lit ^ "]).\n";
  1107 
  1063 
  1108 
  1064 
  1109 fun tptp_of_arLit (TConsLit(b,(c,t,args))) =
  1065 fun tptp_of_arLit (TConsLit(b,(c,t,args))) =
  1110       let val pol = if b then "++" else "--"
  1066       tptp_sign b (c ^ "(" ^ t ^ paren_pack args ^ ")")
  1111 	  val  arg_strs = paren_pack args
       
  1112       in 
       
  1113 	  pol ^ c ^ "(" ^ t ^ arg_strs ^ ")"
       
  1114       end
       
  1115   | tptp_of_arLit (TVarLit(b,(c,str))) =
  1067   | tptp_of_arLit (TVarLit(b,(c,str))) =
  1116       let val pol = if b then "++" else "--"
  1068       tptp_sign b (c ^ "(" ^ str ^ ")")
  1117       in
       
  1118 	  pol ^ c ^ "(" ^ str ^ ")"
       
  1119       end;
       
  1120     
  1069     
  1121 
  1070 fun tptp_arity_clause (arcls as ArityClause{kind,conclLit,premLits,...}) = 
  1122 fun tptp_of_conclLit (ArityClause arcls) = tptp_of_arLit (#conclLit arcls);
  1071   let val arcls_id = string_of_arClauseID arcls
  1123      
  1072       val knd = name_of_kind kind
  1124 fun tptp_of_premLits (ArityClause arcls) = map tptp_of_arLit (#premLits arcls);
  1073       val lits = map tptp_of_arLit (conclLit :: premLits)
  1125 		
  1074   in
  1126 fun tptp_arity_clause arcls = 
  1075     "input_clause(" ^ arcls_id ^ "," ^ knd ^ "," ^ bracket_pack lits ^ ").\n"
  1127     let val arcls_id = string_of_arClauseID arcls
  1076   end;
  1128 	val concl_lit = tptp_of_conclLit arcls
       
  1129 	val prems_lits = tptp_of_premLits arcls
       
  1130 	val knd = string_of_arKind arcls
       
  1131 	val all_lits = concl_lit :: prems_lits
       
  1132     in
       
  1133 	"input_clause(" ^ arcls_id ^ "," ^ knd ^ "," ^ 
       
  1134 	(bracket_pack all_lits) ^ ").\n"
       
  1135     end;
       
  1136 
  1077 
  1137 fun tptp_classrelLits sub sup = 
  1078 fun tptp_classrelLits sub sup = 
  1138     let val tvar = "(T)"
  1079     let val tvar = "(T)"
  1139     in 
  1080     in 
  1140 	"[--" ^ sub ^ tvar ^ ",++" ^ sup ^ tvar ^ "]"
  1081 	"[--" ^ sub ^ tvar ^ ",++" ^ sup ^ tvar ^ "]"
  1141     end;
  1082     end;
  1142 
  1083 
  1143 fun tptp_classrelClause (ClassrelClause {clause_id,subclass,superclass,...}) =
  1084 fun tptp_classrelClause (ClassrelClause {axiom_name,subclass,superclass,...}) =
  1144     let val relcls_id = clrelclause_prefix ^ ascii_of subclass ^ "_" ^ 
  1085   "input_clause(" ^ axiom_name ^ ",axiom," ^ tptp_classrelLits subclass superclass ^ ").\n" 
  1145                         Int.toString clause_id
       
  1146 	val lits = tptp_classrelLits (make_type_class subclass) (make_type_class superclass)
       
  1147     in
       
  1148 	"input_clause(" ^ relcls_id ^ ",axiom," ^ lits ^ ").\n"
       
  1149     end; 
       
  1150 
  1086 
  1151 (* write out a subgoal as tptp clauses to the file "xxxx_N"*)
  1087 (* write out a subgoal as tptp clauses to the file "xxxx_N"*)
  1152 fun tptp_write_file ths filename (axclauses,classrel_clauses,arity_clauses) =
  1088 fun tptp_write_file ths filename (axclauses,classrel_clauses,arity_clauses) =
  1153   let
  1089   let
  1154     val clss = make_conjecture_clauses (map prop_of ths)
  1090     val clss = make_conjecture_clauses (map prop_of ths)
  1155     val (tptp_clss,tfree_litss) = ListPair.unzip (map clause2tptp clss)
  1091     val (tptp_clss,tfree_litss) = ListPair.unzip (map clause2tptp clss)
  1156     val tfree_clss = map tptp_tfree_clause (foldl (op union_string) [] tfree_litss)
  1092     val tfree_clss = map tptp_tfree_clause (foldl (op union_string) [] tfree_litss)
  1157     val classrel_cls = map tptp_classrelClause classrel_clauses
       
  1158     val arity_cls = map tptp_arity_clause arity_clauses
       
  1159     val out = TextIO.openOut filename
  1093     val out = TextIO.openOut filename
  1160   in
  1094   in
  1161     List.app (writeln_strs out o tptp_clause) axclauses;
  1095     List.app (writeln_strs out o tptp_clause) axclauses;
  1162     writeln_strs out tfree_clss;
  1096     writeln_strs out tfree_clss;
  1163     writeln_strs out tptp_clss;
  1097     writeln_strs out tptp_clss;
  1164     writeln_strs out classrel_cls;
  1098     List.app (curry TextIO.output out o tptp_classrelClause) classrel_clauses;
  1165     writeln_strs out arity_cls;
  1099     List.app (curry TextIO.output out o tptp_arity_clause) arity_clauses;
  1166     TextIO.closeOut out
  1100     TextIO.closeOut out
  1167   end;
  1101   end;
  1168 
  1102 
  1169 
       
  1170 end;
  1103 end;