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
paulson@16795
     1
(*  ID:      $Id$
paulson@15789
     2
    Author:     Claire Quigley
paulson@15789
     3
    Copyright   2004  University of Cambridge
paulson@15789
     4
*)
paulson@15789
     5
paulson@16741
     6
structure ReduceAxiomsN =
paulson@16741
     7
(* Author: Jia Meng, Cambridge University Computer Laboratory
paulson@17234
     8
   Remove irrelevant axioms used for a proof of a goal, with with iteration control
paulson@17234
     9
   Initial version. Needs elaboration. *)
paulson@16741
    10
paulson@16741
    11
struct
paulson@16741
    12
paulson@18509
    13
fun add_term_consts_rm ncs (Const(c, _)) cs = if (c mem ncs) then cs else (c ins_string cs)
paulson@16795
    14
  | add_term_consts_rm ncs (t $ u) cs =
paulson@16795
    15
      add_term_consts_rm ncs t (add_term_consts_rm ncs u cs)
paulson@16741
    16
  | add_term_consts_rm ncs (Abs(_,_,t)) cs = add_term_consts_rm ncs t cs
paulson@16741
    17
  | add_term_consts_rm ncs _ cs = cs;
paulson@16741
    18
paulson@16741
    19
fun term_consts_rm ncs t = add_term_consts_rm ncs t [];
paulson@16741
    20
fun thm_consts_rm ncs thm = term_consts_rm ncs (prop_of thm);
paulson@16741
    21
fun consts_of_thm (n,thm) = thm_consts_rm ["Trueprop","==>","all","Ex","op &", "op |", "Not", "All", "op -->", "op =", "==", "True", "False"] thm;
paulson@16741
    22
fun consts_of_term term = term_consts_rm ["Trueprop","==>","all","Ex","op &", "op |", "Not", "All", "op -->", "op =", "==", "True", "False"] term;
paulson@16741
    23
paulson@16741
    24
fun make_pairs [] _ = []
paulson@16741
    25
  | make_pairs (x::xs) y = (x,y)::(make_pairs xs y);
paulson@16741
    26
paulson@16741
    27
fun const_thm_list_aux [] cthms = cthms
paulson@16741
    28
  | const_thm_list_aux (thm::thms) cthms =
paulson@16741
    29
    let val consts = consts_of_thm thm
paulson@16741
    30
	val cthms' = make_pairs consts thm 
paulson@18509
    31
    in const_thm_list_aux thms (cthms' @ cthms) end;
paulson@16741
    32
paulson@16741
    33
fun const_thm_list thms = const_thm_list_aux thms [];
paulson@16741
    34
paulson@18509
    35
fun make_thm_table thms = Symtab.make_multi (const_thm_list thms);
paulson@16741
    36
paulson@16741
    37
fun consts_in_goal goal = consts_of_term goal;
paulson@16741
    38
paulson@16741
    39
fun axioms_having_consts_aux [] tab thms = thms
paulson@16741
    40
  | axioms_having_consts_aux (c::cs) tab thms =
paulson@18509
    41
    let val thms2 = Option.getOpt (Symtab.lookup tab c, [])
paulson@18509
    42
    in axioms_having_consts_aux cs tab (thms2 union thms) end;
paulson@16741
    43
paulson@16741
    44
fun axioms_having_consts cs tab = axioms_having_consts_aux cs tab [];
paulson@16741
    45
paulson@16741
    46
fun relevant_axioms goal thmTab n =  
paulson@18509
    47
    let fun relevant_axioms_aux1 cs k =
paulson@16741
    48
	    let val thms1 = axioms_having_consts cs thmTab
paulson@17764
    49
		val cs1 = foldl (op union_string) [] (map consts_of_thm thms1)
paulson@16741
    50
	    in
paulson@18509
    51
		if (cs1 subset cs) orelse n <= k then (k,thms1) 
paulson@18509
    52
		else relevant_axioms_aux1 (cs1 union cs) (k+1)
paulson@16741
    53
	    end
paulson@18509
    54
    in  relevant_axioms_aux1 (consts_in_goal goal) 1  end;
paulson@16741
    55
paulson@16795
    56
fun relevant_filter n goal thms = 
paulson@16795
    57
    if n<=0 then thms 
paulson@16795
    58
    else #2 (relevant_axioms goal (make_thm_table thms) n);
paulson@16741
    59
paulson@16741
    60
(* find the thms from thy that contain relevant constants, n is the iteration number *)
paulson@16741
    61
fun find_axioms_n thy goal n =
paulson@16741
    62
    let val clasetR = ResAxioms.claset_rules_of_thy thy
paulson@16741
    63
	val simpsetR = ResAxioms.simpset_rules_of_thy thy	  
paulson@16741
    64
	val table = make_thm_table (clasetR @ simpsetR)	
paulson@18509
    65
    in relevant_axioms goal table n end;
paulson@16741
    66
paulson@16741
    67
fun find_axioms_n_c thy goal n =
paulson@16741
    68
    let val current_thms = PureThy.thms_of thy
paulson@16741
    69
	val table = make_thm_table current_thms
paulson@18509
    70
    in relevant_axioms goal table n end;
paulson@16741
    71
paulson@16741
    72
end;
paulson@16741
    73
paulson@16741
    74
quigley@16156
    75
signature RES_CLASIMP = 
quigley@15919
    76
  sig
paulson@18509
    77
  val blacklist : string list ref (*Theorems forbidden in the output*)
paulson@16795
    78
  val relevant : int ref
quigley@16950
    79
  val use_simpset: bool ref
paulson@17305
    80
  val get_clasimp_lemmas : 
paulson@17484
    81
         Proof.context -> term -> 
paulson@17484
    82
         (ResClause.clause * thm) Array.array * ResClause.clause list 
quigley@15919
    83
  end;
quigley@15919
    84
quigley@15919
    85
structure ResClasimp : RES_CLASIMP =
quigley@15919
    86
struct
paulson@17305
    87
val use_simpset = ref false;   (*Performance is much better without simprules*)
paulson@16957
    88
paulson@18509
    89
(*In general, these produce clauses that are prolific (match too many equality or
paulson@18509
    90
  membership literals) and relate to seldom-used facts. Some duplicate other rules.*)
paulson@18509
    91
val blacklist = ref
paulson@18509
    92
  ["Finite_Set.Max_ge",
paulson@18509
    93
   "Finite_Set.Max_le_iff",
paulson@18509
    94
   "Finite_Set.Max_less_iff",
paulson@18509
    95
   "Finite_Set.Min_le",
paulson@18509
    96
   "Finite_Set.Min_ge_iff",
paulson@18509
    97
   "Finite_Set.Min_gr_iff",
paulson@18509
    98
   "Finite_Set.min_max.below_inf_sup_Inf_Sup.inf_Sup_absorb",
paulson@18509
    99
   "Finite_Set.min_max.below_inf_sup_Inf_Sup.sup_Inf_absorb",
paulson@18509
   100
   (*The next four are duplicates of the properties of min and max, from Orderings.*)
paulson@18509
   101
   "Finite_Set.max.f_below_strict_below.below_f_conv",
paulson@18509
   102
   "Finite_Set.max.f_below_strict_below.strict_below_f_conv",
paulson@18509
   103
   "Finite_Set.min.f_below_strict_below.strict_below_f_conv",
paulson@18509
   104
   "Finite_Set.min.f_below_strict_below.below_f_conv",
paulson@18509
   105
   "Infinite_Set.atmost_one_unique",
paulson@18509
   106
   "List.in_listsD",
paulson@18509
   107
   "List.in_listsI",
paulson@18509
   108
   "List.listsE",
paulson@18509
   109
   "List.lists.Cons",
paulson@18509
   110
   "Relation.ImageI",
paulson@18509
   111
   "Relation.diagI",
paulson@18509
   112
   "Set.Diff_insert0",
paulson@18509
   113
   "Set.Inter_UNIV_conv_1",
paulson@18509
   114
   "Set.Inter_UNIV_conv_2",
paulson@18509
   115
   "Set.Inter_iff",              (*We already have InterI, InterE*)
paulson@18509
   116
   "Set.Union_iff",              (*We already have UnionI, UnionE*)
paulson@18509
   117
   "Datatype.not_None_eq_iff2",
paulson@18509
   118
   "Datatype.not_Some_eq",
paulson@18509
   119
   "Datatype.not_Some_eq_D",
paulson@18509
   120
   "Set.disjoint_insert_1",
paulson@18509
   121
   "Set.disjoint_insert_2",
paulson@18509
   122
   "Set.insert_disjoint_1",
paulson@18509
   123
   "Set.insert_disjoint_2",
paulson@18509
   124
   "Set.singletonD",
paulson@18509
   125
   "Set.singletonI",
paulson@18509
   126
   "Sum_Type.InlI",
paulson@18509
   127
   "Sum_Type.InrI",
paulson@18509
   128
   "Set.singleton_insert_inj_eq",
paulson@18509
   129
   "Set.singleton_insert_inj_eq'"];
paulson@18509
   130
paulson@18509
   131
(*These are the defining properties of min and max, but as they are prolific they
paulson@18509
   132
  could be included above.
paulson@18509
   133
   "Orderings.max_less_iff_conj",
paulson@18509
   134
   "Orderings.min_less_iff_conj",
paulson@18509
   135
   "Orderings.min_max.below_inf.below_inf_conv",
paulson@18509
   136
   "Orderings.min_max.below_sup.above_sup_conv",
paulson@18509
   137
*)
paulson@18509
   138
paulson@16957
   139
val relevant = ref 0;  (*Relevance filtering is off by default*)
paulson@16795
   140
paulson@16956
   141
(*The "name" of a theorem is its statement, if nothing else is available.*)
paulson@16956
   142
val plain_string_of_thm =
paulson@16956
   143
    setmp show_question_marks false 
paulson@16956
   144
      (setmp print_mode [] 
paulson@16956
   145
	(Pretty.setmp_margin 999 string_of_thm));
paulson@16957
   146
	
paulson@17828
   147
(*Returns the first substring enclosed in quotation marks, typically omitting 
paulson@17828
   148
  the [.] of meta-level assumptions.*)
paulson@17828
   149
val firstquoted = hd o (String.tokens (fn c => c = #"\""))
paulson@17828
   150
	
paulson@16957
   151
fun fake_thm_name th = 
paulson@17828
   152
    Context.theory_name (theory_of_thm th) ^ "." ^ firstquoted (plain_string_of_thm th);
paulson@16061
   153
paulson@17828
   154
fun put_name_pair ("",th) = (fake_thm_name th, th)
paulson@16956
   155
  | put_name_pair (a,th)  = (a,th);
quigley@16039
   156
quigley@16039
   157
(* outputs a list of (thm,clause) pairs *)
quigley@15643
   158
paulson@17305
   159
fun multi x 0 xlist = xlist
paulson@17305
   160
   |multi x n xlist = multi x (n-1) (x::xlist);
quigley@16156
   161
paulson@17305
   162
fun clause_numbering ((clause, theorem), num_of_cls) = 
paulson@17305
   163
    let val numbers = 0 upto (num_of_cls - 1)
paulson@16172
   164
    in 
paulson@17305
   165
	multi (clause, theorem) num_of_cls []
paulson@16172
   166
    end;
paulson@17234
   167
    
paulson@18420
   168
(*Hashing to detect duplicate and variant clauses, e.g. from the [iff] attribute
paulson@18420
   169
Some primes from http://primes.utm.edu/:
paulson@18420
   170
*)
paulson@18420
   171
paulson@18449
   172
exception HASH_CLAUSE and HASH_STRING;
paulson@18449
   173
paulson@18449
   174
(*Catches (for deletion) theorems automatically generated from other theorems*)
paulson@18449
   175
fun insert_suffixed_names ht x = 
paulson@18449
   176
     (Polyhash.insert ht (x^"_iff1", ()); 
paulson@18449
   177
      Polyhash.insert ht (x^"_iff2", ()); 
paulson@18449
   178
      Polyhash.insert ht (x^"_dest", ())); 
paulson@18449
   179
paulson@18509
   180
fun make_banned_test xs = 
paulson@18449
   181
  let val ht = Polyhash.mkTable (Polyhash.hash_string, op =)
paulson@18449
   182
                                (6000, HASH_STRING)
paulson@18509
   183
      fun banned s = isSome (Polyhash.peek ht s)
paulson@18509
   184
  in  app (fn x => Polyhash.insert ht (x,())) (!blacklist);
paulson@18509
   185
      app (insert_suffixed_names ht) (!blacklist @ xs); 
paulson@18509
   186
      banned
paulson@18509
   187
  end;
paulson@18420
   188
paulson@18420
   189
(*Create a hash table for clauses, of the given size*)
paulson@18449
   190
fun mk_clause_table n =
paulson@18449
   191
      Polyhash.mkTable (ResClause.hash_clause, ResClause.clause_eq)
paulson@18449
   192
                       (n, HASH_CLAUSE);
paulson@18420
   193
paulson@18420
   194
(*Use a hash table to eliminate duplicates from xs*)
paulson@18449
   195
fun make_unique ht xs = 
paulson@18449
   196
      (app (ignore o Polyhash.peekInsert ht) xs;  Polyhash.listItems ht);
quigley@16950
   197
paulson@16741
   198
(*Write out the claset and simpset rules of the supplied theory.
paulson@16906
   199
  FIXME: argument "goal" is a hack to allow relevance filtering.
paulson@16906
   200
  To reduce the number of clauses produced, set ResClasimp.relevant:=1*)
paulson@17484
   201
fun get_clasimp_lemmas ctxt goal = 
paulson@18420
   202
  let val claset_thms =
paulson@18420
   203
	    map put_name_pair
paulson@18420
   204
	      (ReduceAxiomsN.relevant_filter (!relevant) goal 
paulson@18420
   205
		(ResAxioms.claset_rules_of_ctxt ctxt))
paulson@18449
   206
      val simpset_thms = 
paulson@18420
   207
	    if !use_simpset then 
paulson@18449
   208
		  map put_name_pair 
paulson@18420
   209
		    (ReduceAxiomsN.relevant_filter (!relevant) goal
paulson@18449
   210
		      (ResAxioms.simpset_rules_of_ctxt ctxt))
paulson@18420
   211
	    else []
paulson@18509
   212
      val banned = make_banned_test (map #1 (claset_thms@simpset_thms))
paulson@18509
   213
      fun ok (a,_) = not (banned a)
paulson@18449
   214
      val claset_cls_thms = ResAxioms.clausify_rules_pairs (filter ok claset_thms)
paulson@18449
   215
      val simpset_cls_thms = ResAxioms.clausify_rules_pairs (filter ok simpset_thms)
paulson@18449
   216
      val cls_thms_list = make_unique (mk_clause_table 2200) 
paulson@18420
   217
                                      (List.concat (simpset_cls_thms@claset_cls_thms))
paulson@18420
   218
      (* Identify the set of clauses to be written out *)
paulson@18420
   219
      val clauses = map #1(cls_thms_list);
paulson@18420
   220
      val cls_nums = map ResClause.num_of_clauses clauses;
paulson@18420
   221
      (*Note: in every case, cls_num = 1.  I think that only conjecture clauses
paulson@18420
   222
	can have any other value.*)
paulson@18420
   223
      val whole_list = List.concat 
paulson@18420
   224
	    (map clause_numbering (ListPair.zip (cls_thms_list, cls_nums)));
paulson@18420
   225
      
paulson@17828
   226
  in  (* create array of put clausename, number pairs into it *)
paulson@17828
   227
      (Array.fromList whole_list, clauses)
paulson@16061
   228
  end;
quigley@15643
   229
quigley@16950
   230
quigley@15919
   231
end;
quigley@16039
   232
quigley@16156
   233
quigley@16156
   234