src/HOL/Tools/ATP/res_clasimpset.ML
author paulson
Tue Mar 07 16:50:33 2006 +0100 (2006-03-07)
changeset 19209 27b91724809f
parent 19201 294448903a66
child 19317 3d383e78b6f4
permissions -rw-r--r--
Indentation
     1 (*  ID:      $Id$
     2     Author:     Claire Quigley
     3     Copyright   2004  University of Cambridge
     4 *)
     5 
     6 signature RES_CLASIMP = 
     7   sig
     8   val blacklist : string list ref (*Theorems forbidden in the output*)
     9   val use_simpset: bool ref
    10   val get_clasimp_atp_lemmas : 
    11       Proof.context ->
    12       Term.term list ->
    13       (string * Thm.thm) list ->
    14       (bool * bool * bool) -> bool -> string Array.array * (Term.term * (string * int)) list
    15   end;
    16   
    17 structure ResClasimp : RES_CLASIMP =
    18 struct
    19 val use_simpset = ref false;   (*Performance is much better without simprules*)
    20 
    21 
    22 (*In general, these produce clauses that are prolific (match too many equality or
    23   membership literals) and relate to seldom-used facts. Some duplicate other rules.
    24   FIXME: this blacklist needs to be maintained using theory data and added to using
    25   an attribute.*)
    26 val blacklist = ref
    27   ["Datatype.not_None_eq",    (*Says everything is None or Some. Probably prolific.*)
    28    "Datatype.not_Some_eq_D",  (*Says everything is None or Some. Probably prolific.*)
    29    "Datatype.not_Some_eq",    (*Says everything is None or Some. Probably prolific.*)
    30    "Datatype.option.size_1",
    31    "Datatype.option.size_2",
    32    "Datatype.prod.size",
    33    "Datatype.sum.size_1",
    34    "Datatype.sum.size_2",
    35    "Datatype.unit.size",
    36    "Divides.dvd_0_left_iff",
    37    "Finite_Set.card_0_eq",
    38    "Finite_Set.card_infinite",
    39    "Finite_Set.Max_ge",
    40    "Finite_Set.Max_in",
    41    "Finite_Set.Max_le_iff",
    42    "Finite_Set.Max_less_iff",
    43    "Finite_Set.max.f_below_strict_below.below_f_conv", (*duplicates in Orderings.*)
    44    "Finite_Set.max.f_below_strict_below.strict_below_f_conv", (*duplicates in Orderings.*)
    45    "Finite_Set.Min_ge_iff",
    46    "Finite_Set.Min_gr_iff",
    47    "Finite_Set.Min_in",
    48    "Finite_Set.Min_le",
    49    "Finite_Set.min_max.below_inf_sup_Inf_Sup.inf_Sup_absorb", 
    50    "Finite_Set.min_max.below_inf_sup_Inf_Sup.sup_Inf_absorb", 
    51    "Finite_Set.min.f_below_strict_below.below_f_conv",        (*duplicates in Orderings.*)
    52    "Finite_Set.min.f_below_strict_below.strict_below_f_conv", (*duplicates in Orderings.*)
    53    "Infinite_Set.atmost_one_unique",
    54    "IntArith.zabs_less_one_iff",
    55    "IntDef.Integ.Abs_Integ_inject",
    56    "IntDef.Integ.Abs_Integ_inverse",
    57    "IntDiv.zdvd_0_left",
    58    "IntDiv.zero_less_zpower_abs_iff",
    59    "List.append_eq_append_conv",
    60    "List.Cons_in_lex",
    61    "List.hd_Cons_tl",   (*Says everything is [] or Cons. Probably prolific.*)
    62    "List.in_listsD",
    63    "List.in_listsI",
    64    "List.lists.Cons",
    65    "List.listsE",
    66    "List.take_eq_Nil",
    67    "Nat.less_one",
    68    "Nat.less_one", (*not directional? obscure*)
    69    "Nat.not_gr0",
    70    "Nat.one_eq_mult_iff", (*duplicate by symmetry*)
    71    "NatArith.of_nat_0_eq_iff",
    72    "NatArith.of_nat_eq_0_iff",
    73    "NatArith.of_nat_le_0_iff",
    74    "NatSimprocs.divide_le_0_iff_number_of",  (*seldom used; often prolific*)
    75    "NatSimprocs.divide_le_0_iff_number_of",  (*too many clauses*)
    76    "NatSimprocs.divide_less_0_iff_number_of",
    77    "NatSimprocs.divide_less_0_iff_number_of",   (*too many clauses*)
    78    "NatSimprocs.equation_minus_iff_1",  (*not directional*)
    79    "NatSimprocs.equation_minus_iff_number_of", (*not directional*)
    80    "NatSimprocs.le_minus_iff_1", (*not directional*)
    81    "NatSimprocs.le_minus_iff_number_of",  (*not directional*)
    82    "NatSimprocs.less_minus_iff_1", (*not directional*)
    83    "NatSimprocs.less_minus_iff_number_of", (*not directional*)
    84    "NatSimprocs.minus_equation_iff_number_of", (*not directional*)
    85    "NatSimprocs.minus_le_iff_1", (*not directional*)
    86    "NatSimprocs.minus_le_iff_number_of", (*not directional*)
    87    "NatSimprocs.minus_less_iff_1", (*not directional*)
    88    "NatSimprocs.mult_le_cancel_left_number_of", (*excessive case analysis*)
    89    "NatSimprocs.mult_le_cancel_right_number_of", (*excessive case analysis*)
    90    "NatSimprocs.mult_less_cancel_left_number_of", (*excessive case analysis*)
    91    "NatSimprocs.mult_less_cancel_right_number_of", (*excessive case analysis*)
    92    "NatSimprocs.zero_le_divide_iff_number_of",
    93    "NatSimprocs.zero_le_divide_iff_number_of", (*excessive case analysis*)
    94    "NatSimprocs.zero_less_divide_iff_number_of",
    95    "NatSimprocs.zero_less_divide_iff_number_of", (*excessive case analysis*)
    96    "OrderedGroup.abs_0_eq",
    97    "OrderedGroup.abs_0_eq", (*duplicate by symmetry*)
    98    "OrderedGroup.diff_eq_0_iff_eq", (*prolific?*)
    99    "OrderedGroup.join_0_eq_0",
   100    "OrderedGroup.meet_0_eq_0",
   101    "OrderedGroup.pprt_eq_0",   (*obscure*)
   102    "OrderedGroup.pprt_eq_id",   (*obscure*)
   103    "OrderedGroup.pprt_mono",   (*obscure*)
   104    "Parity.even_nat_power",   (*obscure, somewhat prolilfic*)
   105    "Parity.power_eq_0_iff_number_of",
   106    "Parity.power_eq_0_iff_number_of",
   107    "Parity.power_le_zero_eq_number_of",
   108    "Parity.power_le_zero_eq_number_of",   (*obscure and prolific*)
   109    "Parity.power_less_zero_eq_number_of",
   110    "Parity.zero_le_power_eq_number_of",   (*obscure and prolific*)
   111    "Parity.zero_less_power_eq_number_of",   (*obscure and prolific*)
   112    "Power.zero_less_power_abs_iff",
   113    "Relation.diagI",
   114    "Relation.ImageI",
   115    "Ring_and_Field.divide_cancel_left", (*fields are seldom used & often prolific*)
   116    "Ring_and_Field.divide_cancel_right",
   117    "Ring_and_Field.divide_divide_eq_left",
   118    "Ring_and_Field.divide_divide_eq_right",
   119    "Ring_and_Field.divide_eq_0_iff",
   120    "Ring_and_Field.divide_eq_1_iff",
   121    "Ring_and_Field.divide_eq_eq_1",
   122    "Ring_and_Field.divide_le_0_1_iff",
   123    "Ring_and_Field.divide_le_eq_1_neg",
   124    "Ring_and_Field.divide_le_eq_1_neg",  (*obscure and prolific*)
   125    "Ring_and_Field.divide_le_eq_1_pos",
   126    "Ring_and_Field.divide_le_eq_1_pos",  (*obscure and prolific*)
   127    "Ring_and_Field.divide_less_0_1_iff",
   128    "Ring_and_Field.divide_less_eq_1_neg",  (*obscure and prolific*)
   129    "Ring_and_Field.divide_less_eq_1_pos",
   130    "Ring_and_Field.divide_less_eq_1_pos",  (*obscure and prolific*)
   131    "Ring_and_Field.eq_divide_eq_1",
   132    "Ring_and_Field.eq_divide_eq_1", (*duplicate by symmetry*)
   133    "Ring_and_Field.field_mult_cancel_left",
   134    "Ring_and_Field.field_mult_cancel_right",
   135    "Ring_and_Field.inverse_le_iff_le_neg",
   136    "Ring_and_Field.inverse_le_iff_le",
   137    "Ring_and_Field.inverse_less_iff_less_neg",
   138    "Ring_and_Field.inverse_less_iff_less",
   139    "Ring_and_Field.le_divide_eq_1_neg",
   140    "Ring_and_Field.le_divide_eq_1_neg", (*obscure and prolific*)
   141    "Ring_and_Field.le_divide_eq_1_pos",
   142    "Ring_and_Field.le_divide_eq_1_pos", (*obscure and prolific*)
   143    "Ring_and_Field.less_divide_eq_1_neg",
   144    "Ring_and_Field.less_divide_eq_1_neg", (*obscure and prolific*)
   145    "Ring_and_Field.less_divide_eq_1_pos",
   146    "Ring_and_Field.less_divide_eq_1_pos", (*obscure and prolific*)
   147    "Ring_and_Field.one_eq_divide_iff",  (*duplicate by symmetry*)
   148    "Set.Diff_eq_empty_iff", (*redundant with paramodulation*)
   149    "Set.Diff_insert0",
   150    "Set.disjoint_insert_1",
   151    "Set.disjoint_insert_2",
   152    "Set.empty_Union_conv", (*redundant with paramodulation*)
   153    "Set.insert_disjoint_1",
   154    "Set.insert_disjoint_2",
   155    "Set.Int_UNIV", (*redundant with paramodulation*)
   156    "Set.Inter_iff",              (*We already have InterI, InterE*)
   157    "Set.Inter_UNIV_conv_1",
   158    "Set.Inter_UNIV_conv_2",
   159    "Set.psubsetE",    (*too prolific and obscure*)
   160    "Set.psubsetI",
   161    "Set.singleton_insert_inj_eq'",
   162    "Set.singleton_insert_inj_eq",
   163    "Set.singletonD",  (*these two duplicate some "insert" lemmas*)
   164    "Set.singletonI",
   165    "Set.Un_empty", (*redundant with paramodulation*)
   166    "Set.Union_empty_conv", (*redundant with paramodulation*)
   167    "Set.Union_iff",              (*We already have UnionI, UnionE*)
   168    "SetInterval.atLeastAtMost_iff", (*obscure and prolific*)
   169    "SetInterval.atLeastLessThan_iff", (*obscure and prolific*)
   170    "SetInterval.greaterThanAtMost_iff", (*obscure and prolific*)
   171    "SetInterval.greaterThanLessThan_iff", (*obscure and prolific*)
   172    "SetInterval.ivl_subset", (*excessive case analysis*)
   173    "Sum_Type.InlI",
   174    "Sum_Type.InrI"];
   175    
   176 (*These might be prolific but are probably OK, and min and max are basic.
   177    "Orderings.max_less_iff_conj", 
   178    "Orderings.min_less_iff_conj",
   179    "Orderings.min_max.below_inf.below_inf_conv",
   180    "Orderings.min_max.below_sup.above_sup_conv",
   181 Very prolific and somewhat obscure:
   182    "Set.InterD",
   183    "Set.UnionI",
   184 *)
   185 
   186 (*The "name" of a theorem is its statement, if nothing else is available.*)
   187 val plain_string_of_thm =
   188     setmp show_question_marks false 
   189       (setmp print_mode [] 
   190 	(Pretty.setmp_margin 999 string_of_thm));
   191 	
   192 (*Returns the first substring enclosed in quotation marks, typically omitting 
   193   the [.] of meta-level assumptions.*)
   194 val firstquoted = hd o (String.tokens (fn c => c = #"\""))
   195 	
   196 fun fake_thm_name th = 
   197     Context.theory_name (theory_of_thm th) ^ "." ^ firstquoted (plain_string_of_thm th);
   198 
   199 fun put_name_pair ("",th) = (fake_thm_name th, th)
   200   | put_name_pair (a,th)  = (a,th);
   201 
   202 (*Hashing to detect duplicate and variant clauses, e.g. from the [iff] attribute*)
   203 
   204 exception HASH_CLAUSE and HASH_STRING;
   205 
   206 (*Catches (for deletion) theorems automatically generated from other theorems*)
   207 fun insert_suffixed_names ht x = 
   208      (Polyhash.insert ht (x^"_iff1", ()); 
   209       Polyhash.insert ht (x^"_iff2", ()); 
   210       Polyhash.insert ht (x^"_dest", ())); 
   211 
   212 fun make_banned_test xs = 
   213   let val ht = Polyhash.mkTable (Polyhash.hash_string, op =)
   214                                 (6000, HASH_STRING)
   215       fun banned s = isSome (Polyhash.peek ht s)
   216   in  app (fn x => Polyhash.insert ht (x,())) (!blacklist);
   217       app (insert_suffixed_names ht) (!blacklist @ xs); 
   218       banned
   219   end;
   220 
   221 
   222 (*** a hash function from Term.term to int, and also a hash table ***)
   223 val xor_words = List.foldl Word.xorb 0w0;
   224 
   225 fun hashw_term ((Const(c,_)), w) = Polyhash.hashw_string (c,w)
   226   | hashw_term ((Free(_,_)), w) = w
   227   | hashw_term ((Var(_,_)), w) = w
   228   | hashw_term ((Bound _), w) = w
   229   | hashw_term ((Abs(_,_,t)), w) = hashw_term (t, w)
   230   | hashw_term ((P$Q), w) = hashw_term (Q, (hashw_term (P, w)));
   231 
   232 fun hashw_pred (P,w) = 
   233     let val (p,args) = strip_comb P
   234     in
   235 	List.foldl hashw_term w (p::args)
   236     end;
   237 
   238 fun hash_literal (Const("Not",_)$P) = Word.notb(hashw_pred(P,0w0))
   239   | hash_literal P = hashw_pred(P,0w0);
   240 
   241 
   242 fun get_literals (Const("Trueprop",_)$P) lits = get_literals P lits
   243   | get_literals (Const("op |",_)$P$Q) lits = get_literals Q (get_literals P lits)
   244   | get_literals lit lits = (lit::lits);
   245 
   246 
   247 fun hash_term term = Word.toIntX (xor_words (map hash_literal (get_literals term [])));
   248 
   249 
   250 (*Create a hash table for clauses, of the given size*)
   251 fun mk_clause_table n =
   252       Polyhash.mkTable (hash_term, Term.aconv)
   253                        (n, HASH_CLAUSE);
   254 
   255 (*Use a hash table to eliminate duplicates from xs*)
   256 fun make_unique ht xs = 
   257       (app (ignore o Polyhash.peekInsert ht) xs;  Polyhash.listItems ht);
   258 
   259 fun mem_tm tm [] = false
   260   | mem_tm tm ((tm',name)::tms_names) = Term.aconv (tm,tm') orelse mem_tm tm tms_names;
   261 
   262 fun insert_tms [] tms_names = tms_names
   263   | insert_tms ((tm,name)::tms_names) tms_names' =
   264       if mem_tm tm tms_names' then insert_tms tms_names tms_names' 
   265       else insert_tms tms_names ((tm,name)::tms_names');
   266 
   267 fun warning_thms [] = ()
   268   | warning_thms ((name,thm)::nthms) = 
   269       let val nthm = name ^ ": " ^ (string_of_thm thm)
   270       in warning nthm; warning_thms nthms  end;
   271  
   272 (*Write out the claset, simpset and atpset rules of the supplied theory.*)
   273 (* also write supplied user rules, they are not relevance filtered *)
   274 fun get_clasimp_atp_lemmas ctxt goals user_thms (use_claset, use_simpset', use_atpset) run_filter =
   275     let val claset_thms =
   276 	    if use_claset then
   277 		map put_name_pair (ResAxioms.claset_rules_of_ctxt ctxt)
   278 	    else []
   279       val simpset_thms = 
   280 	    if (!use_simpset andalso use_simpset') then (* temporary, may merge two use_simpset later *)  
   281 		map put_name_pair (ResAxioms.simpset_rules_of_ctxt ctxt)
   282 	    else []
   283       val atpset_thms =
   284 	  if use_atpset then
   285 	      map put_name_pair (ResAxioms.atpset_rules_of_ctxt ctxt)
   286 	  else []
   287       val _ = warning_thms atpset_thms
   288       val user_rules = map put_name_pair user_thms
   289       val banned = make_banned_test (map #1 (user_rules@atpset_thms@claset_thms@simpset_thms))
   290       fun ok (a,_) = not (banned a) 	   
   291       val claset_cls_tms = 
   292             if run_filter then ResAxioms.clausify_rules_pairs_abs (filter ok claset_thms)
   293             else ResAxioms.clausify_rules_pairs_abs claset_thms
   294       val simpset_cls_tms = 
   295       	    if run_filter then ResAxioms.clausify_rules_pairs_abs (filter ok simpset_thms)
   296 	    else ResAxioms.clausify_rules_pairs_abs simpset_thms
   297       val atpset_cls_tms = 
   298       	    if run_filter then ResAxioms.clausify_rules_pairs_abs (filter ok atpset_thms)
   299 	    else ResAxioms.clausify_rules_pairs_abs atpset_thms
   300       val user_cls_tms = ResAxioms.clausify_rules_pairs_abs user_rules (* no filter here, because user supplied rules *)
   301       val cls_tms_list = make_unique (mk_clause_table 2200) 
   302                            (List.concat (user_cls_tms@atpset_cls_tms@simpset_cls_tms@claset_cls_tms))
   303       val relevant_cls_tms_list =
   304 	  if run_filter 
   305 	  then ReduceAxiomsN.relevance_filter (ProofContext.theory_of ctxt) cls_tms_list goals
   306 	  else cls_tms_list
   307       val all_relevant_cls_tms_list = insert_tms (List.concat user_cls_tms) relevant_cls_tms_list (*ensure all user supplied rules are output*)	  
   308     in
   309 	(Array.fromList (map fst (map snd all_relevant_cls_tms_list)), all_relevant_cls_tms_list)
   310 end;
   311 
   312 
   313 	
   314 end;