src/HOL/Tools/res_atp.ML
changeset 30240 5b25fee0362c
parent 29270 0eade173f77e
child 30242 aea5d7fa7ef5
equal deleted inserted replaced
30239:179ff9cb160b 30240:5b25fee0362c
     4 sig
     4 sig
     5   datatype mode = Auto | Fol | Hol
     5   datatype mode = Auto | Fol | Hol
     6   val tvar_classes_of_terms : term list -> string list
     6   val tvar_classes_of_terms : term list -> string list
     7   val tfree_classes_of_terms : term list -> string list
     7   val tfree_classes_of_terms : term list -> string list
     8   val type_consts_of_terms : theory -> term list -> string list
     8   val type_consts_of_terms : theory -> term list -> string list
     9   val write_problem_files : (theory -> bool -> Thm.thm list -> string ->
     9   val write_problem_files : bool -> int -> bool
    10   (thm * (ResHolClause.axiom_name * ResHolClause.clause_id)) list * ResClause.classrelClause list *
       
    11     ResClause.arityClause list -> string list -> ResHolClause.axiom_name list)
       
    12     -> int -> bool
       
    13     -> (int -> Path.T) -> Proof.context * thm list * thm
    10     -> (int -> Path.T) -> Proof.context * thm list * thm
    14     -> string list * ResHolClause.axiom_name Vector.vector list
    11     -> string list * ResHolClause.axiom_name Vector.vector list
    15 end;
    12 end;
    16 
    13 
    17 structure ResAtp: RES_ATP =
    14 structure ResAtp: RES_ATP =
   116 
   113 
   117 (*The empty list here indicates that the constant is being ignored*)
   114 (*The empty list here indicates that the constant is being ignored*)
   118 fun add_standard_const (s,tab) = Symtab.update (s,[]) tab;
   115 fun add_standard_const (s,tab) = Symtab.update (s,[]) tab;
   119 
   116 
   120 val null_const_tab : const_typ list list Symtab.table = 
   117 val null_const_tab : const_typ list list Symtab.table = 
   121     foldl add_standard_const Symtab.empty standard_consts;
   118     List.foldl add_standard_const Symtab.empty standard_consts;
   122 
   119 
   123 fun get_goal_consts_typs thy = foldl (add_term_consts_typs_rm thy) null_const_tab;
   120 fun get_goal_consts_typs thy = List.foldl (add_term_consts_typs_rm thy) null_const_tab;
   124 
   121 
   125 (*Inserts a dummy "constant" referring to the theory name, so that relevance
   122 (*Inserts a dummy "constant" referring to the theory name, so that relevance
   126   takes the given theory into account.*)
   123   takes the given theory into account.*)
   127 fun const_prop_of theory_const th =
   124 fun const_prop_of theory_const th =
   128  if theory_const then
   125  if theory_const then
   191     in
   188     in
   192 	rel_weight / (rel_weight + real (length consts_typs - length rel))
   189 	rel_weight / (rel_weight + real (length consts_typs - length rel))
   193     end;
   190     end;
   194     
   191     
   195 (*Multiplies out to a list of pairs: 'a * 'b list -> ('a * 'b) list -> ('a * 'b) list*)
   192 (*Multiplies out to a list of pairs: 'a * 'b list -> ('a * 'b) list -> ('a * 'b) list*)
   196 fun add_expand_pairs (x,ys) xys = foldl (fn (y,acc) => (x,y)::acc) xys ys;
   193 fun add_expand_pairs (x,ys) xys = List.foldl (fn (y,acc) => (x,y)::acc) xys ys;
   197 
   194 
   198 fun consts_typs_of_term thy t = 
   195 fun consts_typs_of_term thy t = 
   199   let val tab = add_term_consts_typs_rm thy (t, null_const_tab)
   196   let val tab = add_term_consts_typs_rm thy (t, null_const_tab)
   200   in  Symtab.fold add_expand_pairs tab []  end;
   197   in  Symtab.fold add_expand_pairs tab []  end;
   201 
   198 
   251 fun relevant_clauses max_new thy ctab p rel_consts =
   248 fun relevant_clauses max_new thy ctab p rel_consts =
   252   let fun relevant ([],_) [] = [] : (thm * (string * int)) list  (*Nothing added this iteration*)
   249   let fun relevant ([],_) [] = [] : (thm * (string * int)) list  (*Nothing added this iteration*)
   253 	| relevant (newpairs,rejects) [] =
   250 	| relevant (newpairs,rejects) [] =
   254 	    let val (newrels,more_rejects) = take_best max_new newpairs
   251 	    let val (newrels,more_rejects) = take_best max_new newpairs
   255 		val new_consts = List.concat (map #2 newrels)
   252 		val new_consts = List.concat (map #2 newrels)
   256 		val rel_consts' = foldl add_const_typ_table rel_consts new_consts
   253 		val rel_consts' = List.foldl add_const_typ_table rel_consts new_consts
   257 		val newp = p + (1.0-p) / convergence
   254 		val newp = p + (1.0-p) / convergence
   258 	    in
   255 	    in
   259               Output.debug (fn () => ("relevant this iteration: " ^ Int.toString (length newrels)));
   256               Output.debug (fn () => ("relevant this iteration: " ^ Int.toString (length newrels)));
   260 	       (map #1 newrels) @ 
   257 	       (map #1 newrels) @ 
   261 	       (relevant_clauses max_new thy ctab newp rel_consts' (more_rejects@rejects))
   258 	       (relevant_clauses max_new thy ctab newp rel_consts' (more_rejects@rejects))
   377 fun multi_name a (th, (n,pairs)) =
   374 fun multi_name a (th, (n,pairs)) =
   378   (n+1, (a ^ "(" ^ Int.toString n ^ ")", th) :: pairs)
   375   (n+1, (a ^ "(" ^ Int.toString n ^ ")", th) :: pairs)
   379 
   376 
   380 fun add_single_names ((a, []), pairs) = pairs
   377 fun add_single_names ((a, []), pairs) = pairs
   381   | add_single_names ((a, [th]), pairs) = (a,th)::pairs
   378   | add_single_names ((a, [th]), pairs) = (a,th)::pairs
   382   | add_single_names ((a, ths), pairs) = #2 (foldl (multi_name a) (1,pairs) ths);
   379   | add_single_names ((a, ths), pairs) = #2 (List.foldl (multi_name a) (1,pairs) ths);
   383 
   380 
   384 (*Ignore blacklisted basenames*)
   381 (*Ignore blacklisted basenames*)
   385 fun add_multi_names ((a, ths), pairs) =
   382 fun add_multi_names ((a, ths), pairs) =
   386   if (Sign.base_name a) mem_string ResAxioms.multi_base_blacklist  then pairs
   383   if (Sign.base_name a) mem_string ResAxioms.multi_base_blacklist  then pairs
   387   else add_single_names ((a, ths), pairs);
   384   else add_single_names ((a, ths), pairs);
   394       val ht = mk_clause_table 900   (*ht for blacklisted theorems*)
   391       val ht = mk_clause_table 900   (*ht for blacklisted theorems*)
   395       fun blacklisted x = run_blacklist_filter andalso isSome (Polyhash.peek ht x)
   392       fun blacklisted x = run_blacklist_filter andalso isSome (Polyhash.peek ht x)
   396   in
   393   in
   397       app (fn th => ignore (Polyhash.peekInsert ht (th,()))) (ResBlacklist.get ctxt);
   394       app (fn th => ignore (Polyhash.peekInsert ht (th,()))) (ResBlacklist.get ctxt);
   398       filter (not o blacklisted o #2)
   395       filter (not o blacklisted o #2)
   399         (foldl add_single_names (foldl add_multi_names [] mults) singles)
   396         (List.foldl add_single_names (List.foldl add_multi_names [] mults) singles)
   400   end;
   397   end;
   401 
   398 
   402 fun check_named ("",th) = (warning ("No name for theorem " ^ Display.string_of_thm th); false)
   399 fun check_named ("",th) = (warning ("No name for theorem " ^ Display.string_of_thm th); false)
   403   | check_named (_,th) = true;
   400   | check_named (_,th) = true;
   404 
   401 
   432 
   429 
   433 (***************************************************************)
   430 (***************************************************************)
   434 (* Type Classes Present in the Axiom or Conjecture Clauses     *)
   431 (* Type Classes Present in the Axiom or Conjecture Clauses     *)
   435 (***************************************************************)
   432 (***************************************************************)
   436 
   433 
   437 fun add_classes (sorts, cset) = foldl setinsert cset (List.concat sorts);
   434 fun add_classes (sorts, cset) = List.foldl setinsert cset (List.concat sorts);
   438 
   435 
   439 (*Remove this trivial type class*)
   436 (*Remove this trivial type class*)
   440 fun delete_type cset = Symtab.delete_safe "HOL.type" cset;
   437 fun delete_type cset = Symtab.delete_safe "HOL.type" cset;
   441 
   438 
   442 fun tvar_classes_of_terms ts =
   439 fun tvar_classes_of_terms ts =
   443   let val sorts_list = map (map #2 o OldTerm.term_tvars) ts
   440   let val sorts_list = map (map #2 o OldTerm.term_tvars) ts
   444   in  Symtab.keys (delete_type (foldl add_classes Symtab.empty sorts_list))  end;
   441   in  Symtab.keys (delete_type (List.foldl add_classes Symtab.empty sorts_list))  end;
   445 
   442 
   446 fun tfree_classes_of_terms ts =
   443 fun tfree_classes_of_terms ts =
   447   let val sorts_list = map (map #2 o OldTerm.term_tfrees) ts
   444   let val sorts_list = map (map #2 o OldTerm.term_tfrees) ts
   448   in  Symtab.keys (delete_type (foldl add_classes Symtab.empty sorts_list))  end;
   445   in  Symtab.keys (delete_type (List.foldl add_classes Symtab.empty sorts_list))  end;
   449 
   446 
   450 (*fold type constructors*)
   447 (*fold type constructors*)
   451 fun fold_type_consts f (Type (a, Ts)) x = fold (fold_type_consts f) Ts (f (a,x))
   448 fun fold_type_consts f (Type (a, Ts)) x = fold (fold_type_consts f) Ts (f (a,x))
   452   | fold_type_consts f T x = x;
   449   | fold_type_consts f T x = x;
   453 
   450 
   522 fun remove_unwanted_clauses cls = filter (not o unwanted o prop_of o fst) cls;
   519 fun remove_unwanted_clauses cls = filter (not o unwanted o prop_of o fst) cls;
   523 
   520 
   524 (* TODO: problem file for *one* subgoal would be sufficient *)
   521 (* TODO: problem file for *one* subgoal would be sufficient *)
   525 (*Write out problem files for each subgoal.
   522 (*Write out problem files for each subgoal.
   526   Argument probfile generates filenames from subgoal-number
   523   Argument probfile generates filenames from subgoal-number
   527   Argument writer is either a tptp_write_file or dfg_write_file from ResHolClause
       
   528   Arguments max_new and theory_const are booleans controlling relevance_filter
   524   Arguments max_new and theory_const are booleans controlling relevance_filter
   529     (necessary for different provers)
   525     (necessary for different provers)
   530   *)
   526 *)
   531 fun write_problem_files writer max_new theory_const probfile (ctxt, chain_ths, th) =
   527 fun write_problem_files dfg max_new theory_const probfile (ctxt, chain_ths, th) =
   532   let val goals = Thm.prems_of th
   528   let val goals = Thm.prems_of th
   533       val thy = ProofContext.theory_of ctxt
   529       val thy = ProofContext.theory_of ctxt
   534       fun get_neg_subgoals [] _ = []
   530       fun get_neg_subgoals [] _ = []
   535         | get_neg_subgoals (gl::gls) n = #1 (ResAxioms.neg_conjecture_clauses th n) ::
   531         | get_neg_subgoals (gl::gls) n = #1 (ResAxioms.neg_conjecture_clauses th n) ::
   536                                          get_neg_subgoals gls (n+1)
   532                                          get_neg_subgoals gls (n+1)
   546                                        |> restrict_to_logic thy isFO
   542                                        |> restrict_to_logic thy isFO
   547                                        |> remove_unwanted_clauses
   543                                        |> remove_unwanted_clauses
   548       val white_cls = ResAxioms.cnf_rules_pairs thy white_thms
   544       val white_cls = ResAxioms.cnf_rules_pairs thy white_thms
   549       (*clauses relevant to goal gl*)
   545       (*clauses relevant to goal gl*)
   550       val axcls_list = map (fn ngcls => white_cls @ relevance_filter max_new theory_const thy included_cls (map prop_of ngcls)) goal_cls
   546       val axcls_list = map (fn ngcls => white_cls @ relevance_filter max_new theory_const thy included_cls (map prop_of ngcls)) goal_cls
       
   547       val writer = if dfg then ResHolClause.dfg_write_file else ResHolClause.tptp_write_file
   551       fun write_all [] [] _ = []
   548       fun write_all [] [] _ = []
   552         | write_all (ccls::ccls_list) (axcls::axcls_list) k =
   549         | write_all (ccls::ccls_list) (axcls::axcls_list) k =
   553             let val fname = File.platform_path (probfile k)
   550             let val fname = File.platform_path (probfile k)
   554                 val axcls = make_unique axcls
   551                 val axcls = make_unique axcls
   555                 val _ = app (fn th => Output.debug (fn _ => Display.string_of_thm th)) ccls
   552                 val _ = app (fn th => Output.debug (fn _ => Display.string_of_thm th)) ccls
   559                 and axtms = map (prop_of o #1) axcls
   556                 and axtms = map (prop_of o #1) axcls
   560                 val subs = tfree_classes_of_terms ccltms
   557                 val subs = tfree_classes_of_terms ccltms
   561                 and supers = tvar_classes_of_terms axtms
   558                 and supers = tvar_classes_of_terms axtms
   562                 and tycons = type_consts_of_terms thy (ccltms@axtms)
   559                 and tycons = type_consts_of_terms thy (ccltms@axtms)
   563                 (*TFrees in conjecture clauses; TVars in axiom clauses*)
   560                 (*TFrees in conjecture clauses; TVars in axiom clauses*)
   564                 val (supers',arity_clauses) = ResClause.make_arity_clauses thy tycons supers
   561                 val (supers',arity_clauses) = ResClause.make_arity_clauses_dfg dfg thy tycons supers
   565                 val classrel_clauses = ResClause.make_classrel_clauses thy subs supers'
   562                 val classrel_clauses = ResClause.make_classrel_clauses thy subs supers'
   566                 val clnames = writer thy isFO ccls fname (axcls,classrel_clauses,arity_clauses) []
   563                 val clnames = writer thy isFO ccls fname (axcls,classrel_clauses,arity_clauses) []
   567                 val thm_names = Vector.fromList clnames
   564                 val thm_names = Vector.fromList clnames
   568             in  (thm_names,fname) :: write_all ccls_list axcls_list (k+1)  end
   565             in  (thm_names,fname) :: write_all ccls_list axcls_list (k+1)  end
   569       val (thm_names_list, filenames) = ListPair.unzip (write_all goal_cls axcls_list 1)
   566       val (thm_names_list, filenames) = ListPair.unzip (write_all goal_cls axcls_list 1)