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