src/HOL/Tools/ATP/res_clasimpset.ML
author paulson
Fri Dec 23 17:36:00 2005 +0100 (2005-12-23)
changeset 18509 d2d96f12a1fc
parent 18449 e314fb38307d
child 18677 01265301db7f
permissions -rw-r--r--
blacklist of prolific theorems (must be replaced by an attribute later
     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 relevant : int ref
    79   val use_simpset: bool ref
    80   val get_clasimp_lemmas : 
    81          Proof.context -> term -> 
    82          (ResClause.clause * thm) Array.array * ResClause.clause list 
    83   end;
    84 
    85 structure ResClasimp : RES_CLASIMP =
    86 struct
    87 val use_simpset = ref false;   (*Performance is much better without simprules*)
    88 
    89 (*In general, these produce clauses that are prolific (match too many equality or
    90   membership literals) and relate to seldom-used facts. Some duplicate other rules.*)
    91 val blacklist = ref
    92   ["Finite_Set.Max_ge",
    93    "Finite_Set.Max_le_iff",
    94    "Finite_Set.Max_less_iff",
    95    "Finite_Set.Min_le",
    96    "Finite_Set.Min_ge_iff",
    97    "Finite_Set.Min_gr_iff",
    98    "Finite_Set.min_max.below_inf_sup_Inf_Sup.inf_Sup_absorb",
    99    "Finite_Set.min_max.below_inf_sup_Inf_Sup.sup_Inf_absorb",
   100    (*The next four are duplicates of the properties of min and max, from Orderings.*)
   101    "Finite_Set.max.f_below_strict_below.below_f_conv",
   102    "Finite_Set.max.f_below_strict_below.strict_below_f_conv",
   103    "Finite_Set.min.f_below_strict_below.strict_below_f_conv",
   104    "Finite_Set.min.f_below_strict_below.below_f_conv",
   105    "Infinite_Set.atmost_one_unique",
   106    "List.in_listsD",
   107    "List.in_listsI",
   108    "List.listsE",
   109    "List.lists.Cons",
   110    "Relation.ImageI",
   111    "Relation.diagI",
   112    "Set.Diff_insert0",
   113    "Set.Inter_UNIV_conv_1",
   114    "Set.Inter_UNIV_conv_2",
   115    "Set.Inter_iff",              (*We already have InterI, InterE*)
   116    "Set.Union_iff",              (*We already have UnionI, UnionE*)
   117    "Datatype.not_None_eq_iff2",
   118    "Datatype.not_Some_eq",
   119    "Datatype.not_Some_eq_D",
   120    "Set.disjoint_insert_1",
   121    "Set.disjoint_insert_2",
   122    "Set.insert_disjoint_1",
   123    "Set.insert_disjoint_2",
   124    "Set.singletonD",
   125    "Set.singletonI",
   126    "Sum_Type.InlI",
   127    "Sum_Type.InrI",
   128    "Set.singleton_insert_inj_eq",
   129    "Set.singleton_insert_inj_eq'"];
   130 
   131 (*These are the defining properties of min and max, but as they are prolific they
   132   could be included above.
   133    "Orderings.max_less_iff_conj",
   134    "Orderings.min_less_iff_conj",
   135    "Orderings.min_max.below_inf.below_inf_conv",
   136    "Orderings.min_max.below_sup.above_sup_conv",
   137 *)
   138 
   139 val relevant = ref 0;  (*Relevance filtering is off by default*)
   140 
   141 (*The "name" of a theorem is its statement, if nothing else is available.*)
   142 val plain_string_of_thm =
   143     setmp show_question_marks false 
   144       (setmp print_mode [] 
   145 	(Pretty.setmp_margin 999 string_of_thm));
   146 	
   147 (*Returns the first substring enclosed in quotation marks, typically omitting 
   148   the [.] of meta-level assumptions.*)
   149 val firstquoted = hd o (String.tokens (fn c => c = #"\""))
   150 	
   151 fun fake_thm_name th = 
   152     Context.theory_name (theory_of_thm th) ^ "." ^ firstquoted (plain_string_of_thm th);
   153 
   154 fun put_name_pair ("",th) = (fake_thm_name th, th)
   155   | put_name_pair (a,th)  = (a,th);
   156 
   157 (* outputs a list of (thm,clause) pairs *)
   158 
   159 fun multi x 0 xlist = xlist
   160    |multi x n xlist = multi x (n-1) (x::xlist);
   161 
   162 fun clause_numbering ((clause, theorem), num_of_cls) = 
   163     let val numbers = 0 upto (num_of_cls - 1)
   164     in 
   165 	multi (clause, theorem) num_of_cls []
   166     end;
   167     
   168 (*Hashing to detect duplicate and variant clauses, e.g. from the [iff] attribute
   169 Some primes from http://primes.utm.edu/:
   170 *)
   171 
   172 exception HASH_CLAUSE and HASH_STRING;
   173 
   174 (*Catches (for deletion) theorems automatically generated from other theorems*)
   175 fun insert_suffixed_names ht x = 
   176      (Polyhash.insert ht (x^"_iff1", ()); 
   177       Polyhash.insert ht (x^"_iff2", ()); 
   178       Polyhash.insert ht (x^"_dest", ())); 
   179 
   180 fun make_banned_test xs = 
   181   let val ht = Polyhash.mkTable (Polyhash.hash_string, op =)
   182                                 (6000, HASH_STRING)
   183       fun banned s = isSome (Polyhash.peek ht s)
   184   in  app (fn x => Polyhash.insert ht (x,())) (!blacklist);
   185       app (insert_suffixed_names ht) (!blacklist @ xs); 
   186       banned
   187   end;
   188 
   189 (*Create a hash table for clauses, of the given size*)
   190 fun mk_clause_table n =
   191       Polyhash.mkTable (ResClause.hash_clause, ResClause.clause_eq)
   192                        (n, HASH_CLAUSE);
   193 
   194 (*Use a hash table to eliminate duplicates from xs*)
   195 fun make_unique ht xs = 
   196       (app (ignore o Polyhash.peekInsert ht) xs;  Polyhash.listItems ht);
   197 
   198 (*Write out the claset and simpset rules of the supplied theory.
   199   FIXME: argument "goal" is a hack to allow relevance filtering.
   200   To reduce the number of clauses produced, set ResClasimp.relevant:=1*)
   201 fun get_clasimp_lemmas ctxt goal = 
   202   let val claset_thms =
   203 	    map put_name_pair
   204 	      (ReduceAxiomsN.relevant_filter (!relevant) goal 
   205 		(ResAxioms.claset_rules_of_ctxt ctxt))
   206       val simpset_thms = 
   207 	    if !use_simpset then 
   208 		  map put_name_pair 
   209 		    (ReduceAxiomsN.relevant_filter (!relevant) goal
   210 		      (ResAxioms.simpset_rules_of_ctxt ctxt))
   211 	    else []
   212       val banned = make_banned_test (map #1 (claset_thms@simpset_thms))
   213       fun ok (a,_) = not (banned a)
   214       val claset_cls_thms = ResAxioms.clausify_rules_pairs (filter ok claset_thms)
   215       val simpset_cls_thms = ResAxioms.clausify_rules_pairs (filter ok simpset_thms)
   216       val cls_thms_list = make_unique (mk_clause_table 2200) 
   217                                       (List.concat (simpset_cls_thms@claset_cls_thms))
   218       (* Identify the set of clauses to be written out *)
   219       val clauses = map #1(cls_thms_list);
   220       val cls_nums = map ResClause.num_of_clauses clauses;
   221       (*Note: in every case, cls_num = 1.  I think that only conjecture clauses
   222 	can have any other value.*)
   223       val whole_list = List.concat 
   224 	    (map clause_numbering (ListPair.zip (cls_thms_list, cls_nums)));
   225       
   226   in  (* create array of put clausename, number pairs into it *)
   227       (Array.fromList whole_list, clauses)
   228   end;
   229 
   230 
   231 end;
   232 
   233 
   234