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