src/HOL/Tools/res_atp.ML
changeset 19768 9afd9b9c47d0
parent 19746 9ac97dc14214
child 19894 7c7e15b27145
     1.1 --- a/src/HOL/Tools/res_atp.ML	Sun Jun 04 10:50:41 2006 +0200
     1.2 +++ b/src/HOL/Tools/res_atp.ML	Sun Jun 04 10:52:47 2006 +0200
     1.3 @@ -37,6 +37,7 @@
     1.4    val hol_no_types: unit -> unit
     1.5    val hol_typ_level: unit -> ResHolClause.type_level
     1.6    val run_relevance_filter: bool ref
     1.7 +  val run_blacklist_filter: bool ref
     1.8    val invoke_atp_ml : ProofContext.context * thm -> unit
     1.9    val add_claset : unit -> unit
    1.10    val add_simpset : unit -> unit
    1.11 @@ -147,6 +148,7 @@
    1.12  
    1.13  (**** relevance filter ****)
    1.14  val run_relevance_filter = ref true;
    1.15 +val run_blacklist_filter = ref true;
    1.16  
    1.17  (******************************************************************)
    1.18  (* detect whether a given problem (clauses) is FOL/HOL/HOLC/HOLCS *)
    1.19 @@ -277,6 +279,280 @@
    1.20      
    1.21  fun problem_logic_goals subgoals = problem_logic_goals_aux subgoals (FOL,[]);
    1.22  
    1.23 +(***************************************************************)
    1.24 +(* Retrieving and filtering lemmas                             *)
    1.25 +(***************************************************************)
    1.26 +
    1.27 +(*** white list and black list of lemmas ***)
    1.28 +
    1.29 +(*The rule subsetI is frequently omitted by the relevance filter.*)
    1.30 +val whitelist = ref [subsetI]; 
    1.31 +
    1.32 +(*In general, these produce clauses that are prolific (match too many equality or
    1.33 +  membership literals) and relate to seldom-used facts. Some duplicate other rules.
    1.34 +  FIXME: this blacklist needs to be maintained using theory data and added to using
    1.35 +  an attribute.*)
    1.36 +val blacklist = ref
    1.37 +  ["Datatype.prod.size",
    1.38 +   "Divides.dvd_0_left_iff",
    1.39 +   "Finite_Set.card_0_eq",
    1.40 +   "Finite_Set.card_infinite",
    1.41 +   "Finite_Set.Max_ge",
    1.42 +   "Finite_Set.Max_in",
    1.43 +   "Finite_Set.Max_le_iff",
    1.44 +   "Finite_Set.Max_less_iff",
    1.45 +   "Finite_Set.max.f_below_strict_below.below_f_conv", (*duplicates in Orderings.*)
    1.46 +   "Finite_Set.max.f_below_strict_below.strict_below_f_conv", (*duplicates in Orderings.*)
    1.47 +   "Finite_Set.Min_ge_iff",
    1.48 +   "Finite_Set.Min_gr_iff",
    1.49 +   "Finite_Set.Min_in",
    1.50 +   "Finite_Set.Min_le",
    1.51 +   "Finite_Set.min_max.below_inf_sup_Inf_Sup.inf_Sup_absorb", 
    1.52 +   "Finite_Set.min_max.below_inf_sup_Inf_Sup.sup_Inf_absorb", 
    1.53 +   "Finite_Set.min.f_below_strict_below.below_f_conv",        (*duplicates in Orderings.*)
    1.54 +   "Finite_Set.min.f_below_strict_below.strict_below_f_conv", (*duplicates in Orderings.*)
    1.55 +   "IntDef.Integ.Abs_Integ_inject",
    1.56 +   "IntDef.Integ.Abs_Integ_inverse",
    1.57 +   "IntDiv.zdvd_0_left",
    1.58 +   "List.append_eq_append_conv",
    1.59 +   "List.hd_Cons_tl",   (*Says everything is [] or Cons. Probably prolific.*)
    1.60 +   "List.in_listsD",
    1.61 +   "List.in_listsI",
    1.62 +   "List.lists.Cons",
    1.63 +   "List.listsE",
    1.64 +   "Nat.less_one", (*not directional? obscure*)
    1.65 +   "Nat.not_gr0",
    1.66 +   "Nat.one_eq_mult_iff", (*duplicate by symmetry*)
    1.67 +   "NatArith.of_nat_0_eq_iff",
    1.68 +   "NatArith.of_nat_eq_0_iff",
    1.69 +   "NatArith.of_nat_le_0_iff",
    1.70 +   "NatSimprocs.divide_le_0_iff_number_of",  (*too many clauses*)
    1.71 +   "NatSimprocs.divide_less_0_iff_number_of",
    1.72 +   "NatSimprocs.equation_minus_iff_1",  (*not directional*)
    1.73 +   "NatSimprocs.equation_minus_iff_number_of", (*not directional*)
    1.74 +   "NatSimprocs.le_minus_iff_1", (*not directional*)
    1.75 +   "NatSimprocs.le_minus_iff_number_of",  (*not directional*)
    1.76 +   "NatSimprocs.less_minus_iff_1", (*not directional*)
    1.77 +   "NatSimprocs.less_minus_iff_number_of", (*not directional*)
    1.78 +   "NatSimprocs.minus_equation_iff_number_of", (*not directional*)
    1.79 +   "NatSimprocs.minus_le_iff_1", (*not directional*)
    1.80 +   "NatSimprocs.minus_le_iff_number_of", (*not directional*)
    1.81 +   "NatSimprocs.minus_less_iff_1", (*not directional*)
    1.82 +   "NatSimprocs.mult_le_cancel_left_number_of", (*excessive case analysis*)
    1.83 +   "NatSimprocs.mult_le_cancel_right_number_of", (*excessive case analysis*)
    1.84 +   "NatSimprocs.mult_less_cancel_left_number_of", (*excessive case analysis*)
    1.85 +   "NatSimprocs.mult_less_cancel_right_number_of", (*excessive case analysis*)
    1.86 +   "NatSimprocs.zero_le_divide_iff_number_of", (*excessive case analysis*)
    1.87 +   "NatSimprocs.zero_less_divide_iff_number_of",
    1.88 +   "OrderedGroup.abs_0_eq", (*duplicate by symmetry*)
    1.89 +   "OrderedGroup.diff_eq_0_iff_eq", (*prolific?*)
    1.90 +   "OrderedGroup.join_0_eq_0",
    1.91 +   "OrderedGroup.meet_0_eq_0",
    1.92 +   "OrderedGroup.pprt_eq_0",   (*obscure*)
    1.93 +   "OrderedGroup.pprt_eq_id",   (*obscure*)
    1.94 +   "OrderedGroup.pprt_mono",   (*obscure*)
    1.95 +   "Parity.even_nat_power",   (*obscure, somewhat prolilfic*)
    1.96 +   "Parity.power_eq_0_iff_number_of",
    1.97 +   "Parity.power_le_zero_eq_number_of",   (*obscure and prolific*)
    1.98 +   "Parity.power_less_zero_eq_number_of",
    1.99 +   "Parity.zero_le_power_eq_number_of",   (*obscure and prolific*)
   1.100 +   "Parity.zero_less_power_eq_number_of",   (*obscure and prolific*)
   1.101 +   "Power.zero_less_power_abs_iff",
   1.102 +   "Relation.diagI",
   1.103 +   "Relation.ImageI",
   1.104 +   "Ring_and_Field.divide_cancel_left", (*fields are seldom used & often prolific*)
   1.105 +   "Ring_and_Field.divide_cancel_right",
   1.106 +   "Ring_and_Field.divide_divide_eq_left",
   1.107 +   "Ring_and_Field.divide_divide_eq_right",
   1.108 +   "Ring_and_Field.divide_eq_0_iff",
   1.109 +   "Ring_and_Field.divide_eq_1_iff",
   1.110 +   "Ring_and_Field.divide_eq_eq_1",
   1.111 +   "Ring_and_Field.divide_le_0_1_iff",
   1.112 +   "Ring_and_Field.divide_le_eq_1_neg",  (*obscure and prolific*)
   1.113 +   "Ring_and_Field.divide_le_eq_1_pos",  (*obscure and prolific*)
   1.114 +   "Ring_and_Field.divide_less_0_1_iff",
   1.115 +   "Ring_and_Field.divide_less_eq_1_neg",  (*obscure and prolific*)
   1.116 +   "Ring_and_Field.divide_less_eq_1_pos",  (*obscure and prolific*)
   1.117 +   "Ring_and_Field.eq_divide_eq_1", (*duplicate by symmetry*)
   1.118 +   "Ring_and_Field.field_mult_cancel_left",
   1.119 +   "Ring_and_Field.field_mult_cancel_right",
   1.120 +   "Ring_and_Field.inverse_le_iff_le_neg",
   1.121 +   "Ring_and_Field.inverse_le_iff_le",
   1.122 +   "Ring_and_Field.inverse_less_iff_less_neg",
   1.123 +   "Ring_and_Field.inverse_less_iff_less",
   1.124 +   "Ring_and_Field.le_divide_eq_1_neg", (*obscure and prolific*)
   1.125 +   "Ring_and_Field.le_divide_eq_1_pos", (*obscure and prolific*)
   1.126 +   "Ring_and_Field.less_divide_eq_1_neg", (*obscure and prolific*)
   1.127 +   "Ring_and_Field.less_divide_eq_1_pos", (*obscure and prolific*)
   1.128 +   "Ring_and_Field.one_eq_divide_iff",  (*duplicate by symmetry*)
   1.129 +   "Set.Diff_eq_empty_iff", (*redundant with paramodulation*)
   1.130 +   "Set.Diff_insert0",
   1.131 +   "Set.disjoint_insert_1",
   1.132 +   "Set.disjoint_insert_2",
   1.133 +   "Set.empty_Union_conv", (*redundant with paramodulation*)
   1.134 +   "Set.insert_disjoint_1",
   1.135 +   "Set.insert_disjoint_2",
   1.136 +   "Set.Int_UNIV", (*redundant with paramodulation*)
   1.137 +   "Set.Inter_iff",              (*We already have InterI, InterE*)
   1.138 +   "Set.Inter_UNIV_conv_1",
   1.139 +   "Set.Inter_UNIV_conv_2",
   1.140 +   "Set.psubsetE",    (*too prolific and obscure*)
   1.141 +   "Set.psubsetI",
   1.142 +   "Set.singleton_insert_inj_eq'",
   1.143 +   "Set.singleton_insert_inj_eq",
   1.144 +   "Set.singletonD",  (*these two duplicate some "insert" lemmas*)
   1.145 +   "Set.singletonI",
   1.146 +   "Set.Un_empty", (*redundant with paramodulation*)
   1.147 +   "Set.Union_empty_conv", (*redundant with paramodulation*)
   1.148 +   "Set.Union_iff",              (*We already have UnionI, UnionE*)
   1.149 +   "SetInterval.atLeastAtMost_iff", (*obscure and prolific*)
   1.150 +   "SetInterval.atLeastLessThan_iff", (*obscure and prolific*)
   1.151 +   "SetInterval.greaterThanAtMost_iff", (*obscure and prolific*)
   1.152 +   "SetInterval.greaterThanLessThan_iff", (*obscure and prolific*)
   1.153 +   "SetInterval.ivl_subset"];  (*excessive case analysis*)
   1.154 +
   1.155 +
   1.156 +(*These might be prolific but are probably OK, and min and max are basic.
   1.157 +   "Orderings.max_less_iff_conj", 
   1.158 +   "Orderings.min_less_iff_conj",
   1.159 +   "Orderings.min_max.below_inf.below_inf_conv",
   1.160 +   "Orderings.min_max.below_sup.above_sup_conv",
   1.161 +Very prolific and somewhat obscure:
   1.162 +   "Set.InterD",
   1.163 +   "Set.UnionI",
   1.164 +*)
   1.165 +
   1.166 +(*** retrieve lemmas from clasimpset and atpset, may filter them ***)
   1.167 +
   1.168 +(*The "name" of a theorem is its statement, if nothing else is available.*)
   1.169 +val plain_string_of_thm =
   1.170 +    setmp show_question_marks false 
   1.171 +      (setmp print_mode [] 
   1.172 +	(Pretty.setmp_margin 999 string_of_thm));
   1.173 +	
   1.174 +(*Returns the first substring enclosed in quotation marks, typically omitting 
   1.175 +  the [.] of meta-level assumptions.*)
   1.176 +val firstquoted = hd o (String.tokens (fn c => c = #"\""))
   1.177 +	
   1.178 +fun fake_thm_name th = 
   1.179 +    Context.theory_name (theory_of_thm th) ^ "." ^ firstquoted (plain_string_of_thm th);
   1.180 +
   1.181 +fun put_name_pair ("",th) = (fake_thm_name th, th)
   1.182 +  | put_name_pair (a,th)  = (a,th);
   1.183 +
   1.184 +(*Hashing to detect duplicate and variant clauses, e.g. from the [iff] attribute*)
   1.185 +
   1.186 +exception HASH_CLAUSE and HASH_STRING;
   1.187 +
   1.188 +(*Catches (for deletion) theorems automatically generated from other theorems*)
   1.189 +fun insert_suffixed_names ht x = 
   1.190 +     (Polyhash.insert ht (x^"_iff1", ()); 
   1.191 +      Polyhash.insert ht (x^"_iff2", ()); 
   1.192 +      Polyhash.insert ht (x^"_dest", ())); 
   1.193 +
   1.194 +fun make_banned_test xs = 
   1.195 +  let val ht = Polyhash.mkTable (Polyhash.hash_string, op =)
   1.196 +                                (6000, HASH_STRING)
   1.197 +      fun banned s = isSome (Polyhash.peek ht s)
   1.198 +  in  app (fn x => Polyhash.insert ht (x,())) (!blacklist);
   1.199 +      app (insert_suffixed_names ht) (!blacklist @ xs); 
   1.200 +      banned
   1.201 +  end;
   1.202 +
   1.203 +(** a hash function from Term.term to int, and also a hash table **)
   1.204 +val xor_words = List.foldl Word.xorb 0w0;
   1.205 +
   1.206 +fun hashw_term ((Const(c,_)), w) = Polyhash.hashw_string (c,w)
   1.207 +  | hashw_term ((Free(_,_)), w) = w
   1.208 +  | hashw_term ((Var(_,_)), w) = w
   1.209 +  | hashw_term ((Bound _), w) = w
   1.210 +  | hashw_term ((Abs(_,_,t)), w) = hashw_term (t, w)
   1.211 +  | hashw_term ((P$Q), w) = hashw_term (Q, (hashw_term (P, w)));
   1.212 +
   1.213 +fun hashw_pred (P,w) = 
   1.214 +    let val (p,args) = strip_comb P
   1.215 +    in
   1.216 +	List.foldl hashw_term w (p::args)
   1.217 +    end;
   1.218 +
   1.219 +fun hash_literal (Const("Not",_)$P) = Word.notb(hashw_pred(P,0w0))
   1.220 +  | hash_literal P = hashw_pred(P,0w0);
   1.221 +
   1.222 +
   1.223 +fun get_literals (Const("Trueprop",_)$P) lits = get_literals P lits
   1.224 +  | get_literals (Const("op |",_)$P$Q) lits = get_literals Q (get_literals P lits)
   1.225 +  | get_literals lit lits = (lit::lits);
   1.226 +
   1.227 +
   1.228 +fun hash_term term = Word.toIntX (xor_words (map hash_literal (get_literals term [])));
   1.229 +
   1.230 +fun hash_thm  thm = hash_term (prop_of thm);
   1.231 +
   1.232 +fun equal_thm (thm1,thm2) = Term.aconv(prop_of thm1, prop_of thm2);
   1.233 +(*Create a hash table for clauses, of the given size*)
   1.234 +fun mk_clause_table n =
   1.235 +      Polyhash.mkTable (hash_thm, equal_thm)
   1.236 +                       (n, HASH_CLAUSE);
   1.237 +
   1.238 +(*Use a hash table to eliminate duplicates from xs*)
   1.239 +fun make_unique ht xs = 
   1.240 +      (app (ignore o Polyhash.peekInsert ht) xs;  Polyhash.listItems ht);
   1.241 +
   1.242 +fun mem_thm thm [] = false
   1.243 +  | mem_thm thm ((thm',name)::thms_names) = equal_thm (thm,thm') orelse mem_thm thm thms_names;
   1.244 +
   1.245 +fun insert_thms [] thms_names = thms_names
   1.246 +  | insert_thms ((thm,name)::thms_names) thms_names' =
   1.247 +      if mem_thm thm thms_names' then insert_thms thms_names thms_names' 
   1.248 +      else insert_thms thms_names ((thm,name)::thms_names');
   1.249 +
   1.250 +fun display_thms [] = ()
   1.251 +  | display_thms ((name,thm)::nthms) = 
   1.252 +      let val nthm = name ^ ": " ^ (string_of_thm thm)
   1.253 +      in Output.debug nthm; display_thms nthms  end;
   1.254 + 
   1.255 +
   1.256 +(* get lemmas from claset, simpset, atpset and extra supplied rules *)
   1.257 +fun get_clasimp_atp_lemmas ctxt user_thms = 
   1.258 +    let val claset_thms =
   1.259 +	    if !include_claset then
   1.260 +		map put_name_pair (ResAxioms.claset_rules_of_ctxt ctxt)
   1.261 +	    else []
   1.262 +	val simpset_thms = 
   1.263 +	    if !include_simpset then 
   1.264 +		map put_name_pair (ResAxioms.simpset_rules_of_ctxt ctxt)
   1.265 +	    else []
   1.266 +	val atpset_thms =
   1.267 +	    if !include_atpset then
   1.268 +		map put_name_pair (ResAxioms.atpset_rules_of_ctxt ctxt)
   1.269 +	    else []
   1.270 +	val _ = if !Output.show_debug_msgs then (Output.debug "ATP theorems: "; display_thms atpset_thms) else ()		 
   1.271 +	val user_rules = 
   1.272 +	    case user_thms of  (*use whitelist if there are no user-supplied rules*)
   1.273 +		[] => map (put_name_pair o ResAxioms.pairname) (!whitelist)
   1.274 +	      | _  => map put_name_pair user_thms
   1.275 +    in
   1.276 +	(claset_thms, simpset_thms, atpset_thms, user_rules)
   1.277 +    end;
   1.278 +
   1.279 +(* remove lemmas that are banned from the backlist *)
   1.280 +fun blacklist_filter thms = 
   1.281 +    if !run_blacklist_filter then 
   1.282 +	let val banned = make_banned_test (map #1 thms)
   1.283 +	    fun ok (a,_) = not (banned a)
   1.284 +	in
   1.285 +	    filter ok thms
   1.286 +	end
   1.287 +    else
   1.288 +	thms;
   1.289 +
   1.290 +(* filter axiom clauses, but keep supplied clauses and clauses in whitelist *)
   1.291 +fun get_relevant_clauses ctxt cls_thms white_cls goals =
   1.292 +    let val cls_thms_list = make_unique (mk_clause_table 2200) (List.concat (white_cls@cls_thms))
   1.293 +	val relevant_cls_thms_list = if !run_relevance_filter then ReduceAxiomsN.relevance_filter (ProofContext.theory_of ctxt) cls_thms_list goals else cls_thms_list
   1.294 +    in
   1.295 +        insert_thms (List.concat white_cls) relevant_cls_thms_list 
   1.296 +    end;
   1.297  
   1.298  (***************************************************************)
   1.299  (* ATP invocation methods setup                                *)
   1.300 @@ -307,13 +583,15 @@
   1.301      then ResClause.dfg_write_file goals filename (axioms, classrels, arities)
   1.302      else ResHolClause.dfg_write_file goals filename (axioms, classrels, arities);
   1.303  
   1.304 -
   1.305  fun write_subgoal_file dfg mode ctxt conjectures user_thms n =
   1.306      let val conj_cls = make_clauses conjectures 
   1.307  	val hyp_cls = cnf_hyps_thms ctxt
   1.308  	val goal_cls = conj_cls@hyp_cls
   1.309 -	val user_rules = map ResAxioms.pairname user_thms
   1.310 -	val axclauses_as_thms = ResClasimp.get_clasimp_atp_lemmas ctxt (map prop_of goal_cls) user_rules (!include_claset,!include_simpset,!include_atpset) (!run_relevance_filter)  
   1.311 +	val (cla_thms,simp_thms,atp_thms,user_rules) = get_clasimp_atp_lemmas ctxt (map ResAxioms.pairname user_thms)
   1.312 +	val rm_black_cls = blacklist_filter (cla_thms@simp_thms@atp_thms) 
   1.313 +	val cla_simp_atp_clauses = ResAxioms.cnf_rules_pairs rm_black_cls
   1.314 +	val user_cls = ResAxioms.cnf_rules_pairs user_rules
   1.315 +	val axclauses_as_thms = get_relevant_clauses ctxt cla_simp_atp_clauses user_cls (map prop_of goal_cls)
   1.316  	val thy = ProofContext.theory_of ctxt
   1.317  	val prob_logic = case mode of Auto => problem_logic_goals [map prop_of goal_cls]
   1.318  				    | Fol => FOL
   1.319 @@ -407,7 +685,10 @@
   1.320  fun write_problem_files pf (ctxt,th)  =
   1.321    let val goals = Thm.prems_of th
   1.322        val _ = Output.debug ("number of subgoals = " ^ Int.toString (length goals))
   1.323 -      val axclauses = ResClasimp.get_clasimp_atp_lemmas ctxt goals [] (true,true,true) (!run_relevance_filter) (* no user supplied rules here, because no user invocation *)
   1.324 +      val (cla_thms,simp_thms,atp_thms,white_thms) = get_clasimp_atp_lemmas ctxt []
   1.325 +      val rm_blacklist_cls = blacklist_filter (cla_thms@simp_thms@atp_thms)
   1.326 +      val cla_simp_atp_clauses = ResAxioms.cnf_rules_pairs rm_blacklist_cls
   1.327 +      val axclauses = get_relevant_clauses ctxt cla_simp_atp_clauses (ResAxioms.cnf_rules_pairs white_thms) goals 
   1.328        val _ = Output.debug ("claset, simprules and atprules total clauses = " ^ 
   1.329                       Int.toString (length axclauses))
   1.330        val thy = ProofContext.theory_of ctxt