src/HOL/Tools/ATP/res_clasimpset.ML
author paulson
Fri Jan 13 17:39:41 2006 +0100 (2006-01-13)
changeset 18677 01265301db7f
parent 18509 d2d96f12a1fc
child 18753 aa82bd41555d
permissions -rw-r--r--
blacklist experiments
     1 (*  ID:      $Id$
     2     Author:     Claire Quigley
     3     Copyright   2004  University of Cambridge
     4 *)
     5 
     6 structure ReduceAxiomsN =
     7 (* Author: Jia Meng, Cambridge University Computer Laboratory
     8    Remove irrelevant axioms used for a proof of a goal, with with iteration control
     9    Initial version. Needs elaboration. *)
    10 
    11 struct
    12 
    13 fun add_term_consts_rm ncs (Const(c, _)) cs = if (c mem ncs) then cs else (c ins_string cs)
    14   | add_term_consts_rm ncs (t $ u) cs =
    15       add_term_consts_rm ncs t (add_term_consts_rm ncs u cs)
    16   | add_term_consts_rm ncs (Abs(_,_,t)) cs = add_term_consts_rm ncs t cs
    17   | add_term_consts_rm ncs _ cs = cs;
    18 
    19 fun term_consts_rm ncs t = add_term_consts_rm ncs t [];
    20 fun thm_consts_rm ncs thm = term_consts_rm ncs (prop_of thm);
    21 fun consts_of_thm (n,thm) = thm_consts_rm ["Trueprop","==>","all","Ex","op &", "op |", "Not", "All", "op -->", "op =", "==", "True", "False"] thm;
    22 fun consts_of_term term = term_consts_rm ["Trueprop","==>","all","Ex","op &", "op |", "Not", "All", "op -->", "op =", "==", "True", "False"] term;
    23 
    24 fun make_pairs [] _ = []
    25   | make_pairs (x::xs) y = (x,y)::(make_pairs xs y);
    26 
    27 fun const_thm_list_aux [] cthms = cthms
    28   | const_thm_list_aux (thm::thms) cthms =
    29     let val consts = consts_of_thm thm
    30 	val cthms' = make_pairs consts thm 
    31     in const_thm_list_aux thms (cthms' @ cthms) end;
    32 
    33 fun const_thm_list thms = const_thm_list_aux thms [];
    34 
    35 fun make_thm_table thms = Symtab.make_multi (const_thm_list thms);
    36 
    37 fun consts_in_goal goal = consts_of_term goal;
    38 
    39 fun axioms_having_consts_aux [] tab thms = thms
    40   | axioms_having_consts_aux (c::cs) tab thms =
    41     let val thms2 = Option.getOpt (Symtab.lookup tab c, [])
    42     in axioms_having_consts_aux cs tab (thms2 union thms) end;
    43 
    44 fun axioms_having_consts cs tab = axioms_having_consts_aux cs tab [];
    45 
    46 fun relevant_axioms goal thmTab n =  
    47     let fun relevant_axioms_aux1 cs k =
    48 	    let val thms1 = axioms_having_consts cs thmTab
    49 		val cs1 = foldl (op union_string) [] (map consts_of_thm thms1)
    50 	    in
    51 		if (cs1 subset cs) orelse n <= k then (k,thms1) 
    52 		else relevant_axioms_aux1 (cs1 union cs) (k+1)
    53 	    end
    54     in  relevant_axioms_aux1 (consts_in_goal goal) 1  end;
    55 
    56 fun relevant_filter n goal thms = 
    57     if n<=0 then thms 
    58     else #2 (relevant_axioms goal (make_thm_table thms) n);
    59 
    60 (* find the thms from thy that contain relevant constants, n is the iteration number *)
    61 fun find_axioms_n thy goal n =
    62     let val clasetR = ResAxioms.claset_rules_of_thy thy
    63 	val simpsetR = ResAxioms.simpset_rules_of_thy thy	  
    64 	val table = make_thm_table (clasetR @ simpsetR)	
    65     in relevant_axioms goal table n end;
    66 
    67 fun find_axioms_n_c thy goal n =
    68     let val current_thms = PureThy.thms_of thy
    69 	val table = make_thm_table current_thms
    70     in relevant_axioms goal table n end;
    71 
    72 end;
    73 
    74 
    75 signature RES_CLASIMP = 
    76   sig
    77   val blacklist : string list ref (*Theorems forbidden in the output*)
    78   val theory_blacklist : string list ref (*entire blacklisted theories*)
    79   val relevant : int ref
    80   val use_simpset: bool ref
    81   val get_clasimp_lemmas : 
    82          Proof.context -> term -> 
    83          (ResClause.clause * thm) Array.array * ResClause.clause list 
    84   end;
    85 
    86 structure ResClasimp : RES_CLASIMP =
    87 struct
    88 val use_simpset = ref false;   (*Performance is much better without simprules*)
    89 
    90 (*In general, these produce clauses that are prolific (match too many equality or
    91   membership literals) and relate to seldom-used facts. Some duplicate other rules.
    92   FIXME: this blacklist needs to be maintained using theory data and added to using
    93   an attribute.*)
    94 val blacklist = ref
    95   ["Datatype.not_None_eq_iff2",
    96    "Datatype.not_Some_eq_D",
    97    "Datatype.not_Some_eq",
    98    "Datatype.option.size_1",
    99    "Datatype.option.size_2",
   100    "Datatype.prod.size",
   101    "Datatype.sum.size_1",
   102    "Datatype.sum.size_2",
   103    "Datatype.unit.size",
   104    "Divides.dvd_0_left_iff",
   105    "Finite_Set.card_0_eq",
   106    "Finite_Set.card_infinite",
   107    "Finite_Set.Max_ge",
   108    "Finite_Set.Max_in",
   109    "Finite_Set.Max_le_iff",
   110    "Finite_Set.Max_less_iff",
   111    "Finite_Set.max.f_below_strict_below.below_f_conv", (*duplicates in Orderings.*)
   112    "Finite_Set.max.f_below_strict_below.strict_below_f_conv", (*duplicates in Orderings.*)
   113    "Finite_Set.Min_ge_iff",
   114    "Finite_Set.Min_gr_iff",
   115    "Finite_Set.Min_in",
   116    "Finite_Set.Min_le",
   117    "Finite_Set.min_max.below_inf_sup_Inf_Sup.inf_Sup_absorb", 
   118    "Finite_Set.min_max.below_inf_sup_Inf_Sup.sup_Inf_absorb", 
   119    "Finite_Set.min.f_below_strict_below.below_f_conv",        (*duplicates in Orderings.*)
   120    "Finite_Set.min.f_below_strict_below.strict_below_f_conv", (*duplicates in Orderings.*)
   121    "Infinite_Set.atmost_one_unique",
   122    "IntArith.zabs_less_one_iff",
   123    "IntDef.Integ.Abs_Integ_inject",
   124    "IntDef.Integ.Abs_Integ_inverse",
   125    "IntDiv.zdvd_0_left",
   126    "IntDiv.zero_less_zpower_abs_iff",
   127    "List.append_eq_append_conv",
   128    "List.Cons_in_lex",
   129    "List.in_listsD",
   130    "List.in_listsI",
   131    "List.lists.Cons",
   132    "List.listsE",
   133    "List.take_eq_Nil",
   134    "Nat.less_one",
   135    "Nat.less_one", (*not directional? obscure*)
   136    "Nat.not_gr0",
   137    "Nat.one_eq_mult_iff", (*duplicate by symmetry*)
   138    "NatArith.of_nat_0_eq_iff",
   139    "NatArith.of_nat_eq_0_iff",
   140    "NatArith.of_nat_le_0_iff",
   141    "NatSimprocs.divide_le_0_iff_number_of",  (*seldom used; often prolific*)
   142    "NatSimprocs.divide_le_0_iff_number_of",  (*too many clauses*)
   143    "NatSimprocs.divide_less_0_iff_number_of",
   144    "NatSimprocs.divide_less_0_iff_number_of",   (*too many clauses*)
   145    "NatSimprocs.equation_minus_iff_1",  (*not directional*)
   146    "NatSimprocs.equation_minus_iff_number_of", (*not directional*)
   147    "NatSimprocs.le_minus_iff_1", (*not directional*)
   148    "NatSimprocs.le_minus_iff_number_of",  (*not directional*)
   149    "NatSimprocs.less_minus_iff_1", (*not directional*)
   150    "NatSimprocs.less_minus_iff_number_of", (*not directional*)
   151    "NatSimprocs.minus_equation_iff_number_of", (*not directional*)
   152    "NatSimprocs.minus_le_iff_1", (*not directional*)
   153    "NatSimprocs.minus_le_iff_number_of", (*not directional*)
   154    "NatSimprocs.minus_less_iff_1", (*not directional*)
   155    "NatSimprocs.mult_le_cancel_left_number_of", (*excessive case analysis*)
   156    "NatSimprocs.mult_le_cancel_right_number_of", (*excessive case analysis*)
   157    "NatSimprocs.mult_less_cancel_left_number_of", (*excessive case analysis*)
   158    "NatSimprocs.mult_less_cancel_right_number_of", (*excessive case analysis*)
   159    "NatSimprocs.zero_le_divide_iff_number_of",
   160    "NatSimprocs.zero_le_divide_iff_number_of", (*excessive case analysis*)
   161    "NatSimprocs.zero_less_divide_iff_number_of",
   162    "NatSimprocs.zero_less_divide_iff_number_of", (*excessive case analysis*)
   163    "OrderedGroup.abs_0_eq",
   164    "OrderedGroup.abs_0_eq", (*duplicate by symmetry*)
   165    "OrderedGroup.diff_eq_0_iff_eq", (*prolific?*)
   166    "OrderedGroup.join_0_eq_0",
   167    "OrderedGroup.meet_0_eq_0",
   168    "OrderedGroup.pprt_eq_0",   (*obscure*)
   169    "OrderedGroup.pprt_eq_id",   (*obscure*)
   170    "OrderedGroup.pprt_mono",   (*obscure*)
   171    "Parity.even_nat_power",   (*obscure, somewhat prolilfic*)
   172    "Parity.power_eq_0_iff_number_of",
   173    "Parity.power_eq_0_iff_number_of",
   174    "Parity.power_le_zero_eq_number_of",
   175    "Parity.power_le_zero_eq_number_of",   (*obscure and prolific*)
   176    "Parity.power_less_zero_eq_number_of",
   177    "Parity.zero_le_power_eq_number_of",   (*obscure and prolific*)
   178    "Parity.zero_less_power_eq_number_of",   (*obscure and prolific*)
   179    "Power.zero_less_power_abs_iff",
   180    "Relation.diagI",
   181    "Relation.ImageI",
   182    "Ring_and_Field.divide_cancel_left", (*fields are seldom used & often prolific*)
   183    "Ring_and_Field.divide_cancel_right",
   184    "Ring_and_Field.divide_divide_eq_left",
   185    "Ring_and_Field.divide_divide_eq_right",
   186    "Ring_and_Field.divide_eq_0_iff",
   187    "Ring_and_Field.divide_eq_1_iff",
   188    "Ring_and_Field.divide_eq_eq_1",
   189    "Ring_and_Field.divide_le_0_1_iff",
   190    "Ring_and_Field.divide_le_eq_1_neg",
   191    "Ring_and_Field.divide_le_eq_1_neg",  (*obscure and prolific*)
   192    "Ring_and_Field.divide_le_eq_1_pos",
   193    "Ring_and_Field.divide_le_eq_1_pos",  (*obscure and prolific*)
   194    "Ring_and_Field.divide_less_0_1_iff",
   195    "Ring_and_Field.divide_less_eq_1_neg",  (*obscure and prolific*)
   196    "Ring_and_Field.divide_less_eq_1_pos",
   197    "Ring_and_Field.divide_less_eq_1_pos",  (*obscure and prolific*)
   198    "Ring_and_Field.eq_divide_eq_1",
   199    "Ring_and_Field.eq_divide_eq_1", (*duplicate by symmetry*)
   200    "Ring_and_Field.field_mult_cancel_left",
   201    "Ring_and_Field.field_mult_cancel_right",
   202    "Ring_and_Field.inverse_le_iff_le_neg",
   203    "Ring_and_Field.inverse_le_iff_le",
   204    "Ring_and_Field.inverse_less_iff_less_neg",
   205    "Ring_and_Field.inverse_less_iff_less",
   206    "Ring_and_Field.le_divide_eq_1_neg",
   207    "Ring_and_Field.le_divide_eq_1_neg", (*obscure and prolific*)
   208    "Ring_and_Field.le_divide_eq_1_pos",
   209    "Ring_and_Field.le_divide_eq_1_pos", (*obscure and prolific*)
   210    "Ring_and_Field.less_divide_eq_1_neg",
   211    "Ring_and_Field.less_divide_eq_1_neg", (*obscure and prolific*)
   212    "Ring_and_Field.less_divide_eq_1_pos",
   213    "Ring_and_Field.less_divide_eq_1_pos", (*obscure and prolific*)
   214    "Ring_and_Field.one_eq_divide_iff",  (*duplicate by symmetry*)
   215    "Set.Diff_eq_empty_iff", (*redundant with paramodulation*)
   216    "Set.Diff_insert0",
   217    "Set.disjoint_insert_1",
   218    "Set.disjoint_insert_2",
   219    "Set.empty_Union_conv", (*redundant with paramodulation*)
   220    "Set.insert_disjoint_1",
   221    "Set.insert_disjoint_2",
   222    "Set.Int_UNIV", (*redundant with paramodulation*)
   223    "Set.Inter_iff",              (*We already have InterI, InterE*)
   224    "Set.Inter_UNIV_conv_1",
   225    "Set.Inter_UNIV_conv_2",
   226    "Set.psubsetE",    (*too prolific and obscure*)
   227    "Set.psubsetI",
   228    "Set.singleton_insert_inj_eq'",
   229    "Set.singleton_insert_inj_eq",
   230    "Set.singletonD",  (*these two duplicate some "insert" lemmas*)
   231    "Set.singletonI",
   232    "Set.Un_empty", (*redundant with paramodulation*)
   233    "Set.Union_empty_conv", (*redundant with paramodulation*)
   234    "Set.Union_iff",              (*We already have UnionI, UnionE*)
   235    "SetInterval.atLeastAtMost_iff", (*obscure and prolific*)
   236    "SetInterval.atLeastLessThan_iff", (*obscure and prolific*)
   237    "SetInterval.greaterThanAtMost_iff", (*obscure and prolific*)
   238    "SetInterval.greaterThanLessThan_iff", (*obscure and prolific*)
   239    "SetInterval.ivl_subset", (*excessive case analysis*)
   240    "Sum_Type.InlI",
   241    "Sum_Type.InrI"];
   242    
   243 (*These might be prolific but are probably OK, and min and max are basic.
   244    "Orderings.max_less_iff_conj", 
   245    "Orderings.min_less_iff_conj",
   246    "Orderings.min_max.below_inf.below_inf_conv",
   247    "Orderings.min_max.below_sup.above_sup_conv",
   248 Very prolific and somewhat obscure:
   249    "Set.InterD",
   250    "Set.UnionI",
   251 *)
   252 
   253 val theory_blacklist = ref
   254   ["Datatype_Universe.",    (*unnecessary in virtually all proofs*)
   255    "Equiv_Relations."]  
   256 
   257 
   258 val relevant = ref 0;  (*Relevance filtering is off by default*)
   259 
   260 (*The "name" of a theorem is its statement, if nothing else is available.*)
   261 val plain_string_of_thm =
   262     setmp show_question_marks false 
   263       (setmp print_mode [] 
   264 	(Pretty.setmp_margin 999 string_of_thm));
   265 	
   266 (*Returns the first substring enclosed in quotation marks, typically omitting 
   267   the [.] of meta-level assumptions.*)
   268 val firstquoted = hd o (String.tokens (fn c => c = #"\""))
   269 	
   270 fun fake_thm_name th = 
   271     Context.theory_name (theory_of_thm th) ^ "." ^ firstquoted (plain_string_of_thm th);
   272 
   273 fun put_name_pair ("",th) = (fake_thm_name th, th)
   274   | put_name_pair (a,th)  = (a,th);
   275 
   276 (* outputs a list of (thm,clause) pairs *)
   277 
   278 fun multi x 0 xlist = xlist
   279    |multi x n xlist = multi x (n-1) (x::xlist);
   280 
   281 fun clause_numbering ((clause, theorem), num_of_cls) = 
   282     let val numbers = 0 upto (num_of_cls - 1)
   283     in 
   284 	multi (clause, theorem) num_of_cls []
   285     end;
   286     
   287 (*Hashing to detect duplicate and variant clauses, e.g. from the [iff] attribute*)
   288 
   289 exception HASH_CLAUSE and HASH_STRING;
   290 
   291 (*Catches (for deletion) theorems automatically generated from other theorems*)
   292 fun insert_suffixed_names ht x = 
   293      (Polyhash.insert ht (x^"_iff1", ()); 
   294       Polyhash.insert ht (x^"_iff2", ()); 
   295       Polyhash.insert ht (x^"_dest", ())); 
   296 
   297 fun banned_theory s = exists (fn thy => String.isPrefix thy s) (!theory_blacklist);
   298 
   299 fun make_banned_test xs = 
   300   let val ht = Polyhash.mkTable (Polyhash.hash_string, op =)
   301                                 (6000, HASH_STRING)
   302       fun banned s = isSome (Polyhash.peek ht s) orelse banned_theory s
   303   in  app (fn x => Polyhash.insert ht (x,())) (!blacklist);
   304       app (insert_suffixed_names ht) (!blacklist @ xs); 
   305       banned
   306   end;
   307 
   308 (*Create a hash table for clauses, of the given size*)
   309 fun mk_clause_table n =
   310       Polyhash.mkTable (ResClause.hash_clause, ResClause.clause_eq)
   311                        (n, HASH_CLAUSE);
   312 
   313 (*Use a hash table to eliminate duplicates from xs*)
   314 fun make_unique ht xs = 
   315       (app (ignore o Polyhash.peekInsert ht) xs;  Polyhash.listItems ht);
   316 
   317 (*Write out the claset and simpset rules of the supplied theory.
   318   FIXME: argument "goal" is a hack to allow relevance filtering.
   319   To reduce the number of clauses produced, set ResClasimp.relevant:=1*)
   320 fun get_clasimp_lemmas ctxt goal = 
   321   let val claset_thms =
   322 	    map put_name_pair
   323 	      (ReduceAxiomsN.relevant_filter (!relevant) goal 
   324 		(ResAxioms.claset_rules_of_ctxt ctxt))
   325       val simpset_thms = 
   326 	    if !use_simpset then 
   327 		  map put_name_pair 
   328 		    (ReduceAxiomsN.relevant_filter (!relevant) goal
   329 		      (ResAxioms.simpset_rules_of_ctxt ctxt))
   330 	    else []
   331       val banned = make_banned_test (map #1 (claset_thms@simpset_thms))
   332       fun ok (a,_) = not (banned a)
   333       val claset_cls_thms = ResAxioms.clausify_rules_pairs (filter ok claset_thms)
   334       val simpset_cls_thms = ResAxioms.clausify_rules_pairs (filter ok simpset_thms)
   335       val cls_thms_list = make_unique (mk_clause_table 2200) 
   336                                       (List.concat (simpset_cls_thms@claset_cls_thms))
   337       (* Identify the set of clauses to be written out *)
   338       val clauses = map #1(cls_thms_list);
   339       val cls_nums = map ResClause.num_of_clauses clauses;
   340       (*Note: in every case, cls_num = 1.  I think that only conjecture clauses
   341 	can have any other value.*)
   342       val whole_list = List.concat 
   343 	    (map clause_numbering (ListPair.zip (cls_thms_list, cls_nums)));
   344       
   345   in  (* create array of put clausename, number pairs into it *)
   346       (Array.fromList whole_list, clauses)
   347   end;
   348 
   349 
   350 end;
   351 
   352 
   353