src/HOL/Tools/ATP/res_clasimpset.ML
author mengj
Fri Jan 27 05:37:12 2006 +0100 (2006-01-27)
changeset 18792 fb427f4a01f2
parent 18753 aa82bd41555d
child 18797 8559cc115673
permissions -rw-r--r--
Changed codes that call relevance filter.
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*)
paulson@18677
     9
  val theory_blacklist : string list ref (*entire blacklisted theories*)
paulson@16795
    10
  val relevant : int ref
quigley@16950
    11
  val use_simpset: bool ref
paulson@17305
    12
  val get_clasimp_lemmas : 
paulson@18753
    13
         Proof.context -> term list -> 
paulson@17484
    14
         (ResClause.clause * thm) Array.array * ResClause.clause list 
quigley@15919
    15
  end;
quigley@15919
    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
paulson@18509
    21
(*In general, these produce clauses that are prolific (match too many equality or
paulson@18677
    22
  membership literals) and relate to seldom-used facts. Some duplicate other rules.
paulson@18677
    23
  FIXME: this blacklist needs to be maintained using theory data and added to using
paulson@18677
    24
  an attribute.*)
paulson@18509
    25
val blacklist = ref
paulson@18677
    26
  ["Datatype.not_None_eq_iff2",
paulson@18677
    27
   "Datatype.not_Some_eq_D",
paulson@18677
    28
   "Datatype.not_Some_eq",
paulson@18677
    29
   "Datatype.option.size_1",
paulson@18677
    30
   "Datatype.option.size_2",
paulson@18677
    31
   "Datatype.prod.size",
paulson@18677
    32
   "Datatype.sum.size_1",
paulson@18677
    33
   "Datatype.sum.size_2",
paulson@18677
    34
   "Datatype.unit.size",
paulson@18677
    35
   "Divides.dvd_0_left_iff",
paulson@18677
    36
   "Finite_Set.card_0_eq",
paulson@18677
    37
   "Finite_Set.card_infinite",
paulson@18677
    38
   "Finite_Set.Max_ge",
paulson@18677
    39
   "Finite_Set.Max_in",
paulson@18509
    40
   "Finite_Set.Max_le_iff",
paulson@18509
    41
   "Finite_Set.Max_less_iff",
paulson@18677
    42
   "Finite_Set.max.f_below_strict_below.below_f_conv", (*duplicates in Orderings.*)
paulson@18677
    43
   "Finite_Set.max.f_below_strict_below.strict_below_f_conv", (*duplicates in Orderings.*)
paulson@18509
    44
   "Finite_Set.Min_ge_iff",
paulson@18509
    45
   "Finite_Set.Min_gr_iff",
paulson@18677
    46
   "Finite_Set.Min_in",
paulson@18677
    47
   "Finite_Set.Min_le",
paulson@18677
    48
   "Finite_Set.min_max.below_inf_sup_Inf_Sup.inf_Sup_absorb", 
paulson@18677
    49
   "Finite_Set.min_max.below_inf_sup_Inf_Sup.sup_Inf_absorb", 
paulson@18677
    50
   "Finite_Set.min.f_below_strict_below.below_f_conv",        (*duplicates in Orderings.*)
paulson@18677
    51
   "Finite_Set.min.f_below_strict_below.strict_below_f_conv", (*duplicates in Orderings.*)
paulson@18509
    52
   "Infinite_Set.atmost_one_unique",
paulson@18677
    53
   "IntArith.zabs_less_one_iff",
paulson@18677
    54
   "IntDef.Integ.Abs_Integ_inject",
paulson@18677
    55
   "IntDef.Integ.Abs_Integ_inverse",
paulson@18677
    56
   "IntDiv.zdvd_0_left",
paulson@18677
    57
   "IntDiv.zero_less_zpower_abs_iff",
paulson@18677
    58
   "List.append_eq_append_conv",
paulson@18677
    59
   "List.Cons_in_lex",
paulson@18509
    60
   "List.in_listsD",
paulson@18509
    61
   "List.in_listsI",
paulson@18677
    62
   "List.lists.Cons",
paulson@18509
    63
   "List.listsE",
paulson@18677
    64
   "List.take_eq_Nil",
paulson@18677
    65
   "Nat.less_one",
paulson@18677
    66
   "Nat.less_one", (*not directional? obscure*)
paulson@18677
    67
   "Nat.not_gr0",
paulson@18677
    68
   "Nat.one_eq_mult_iff", (*duplicate by symmetry*)
paulson@18677
    69
   "NatArith.of_nat_0_eq_iff",
paulson@18677
    70
   "NatArith.of_nat_eq_0_iff",
paulson@18677
    71
   "NatArith.of_nat_le_0_iff",
paulson@18677
    72
   "NatSimprocs.divide_le_0_iff_number_of",  (*seldom used; often prolific*)
paulson@18677
    73
   "NatSimprocs.divide_le_0_iff_number_of",  (*too many clauses*)
paulson@18677
    74
   "NatSimprocs.divide_less_0_iff_number_of",
paulson@18677
    75
   "NatSimprocs.divide_less_0_iff_number_of",   (*too many clauses*)
paulson@18677
    76
   "NatSimprocs.equation_minus_iff_1",  (*not directional*)
paulson@18677
    77
   "NatSimprocs.equation_minus_iff_number_of", (*not directional*)
paulson@18677
    78
   "NatSimprocs.le_minus_iff_1", (*not directional*)
paulson@18677
    79
   "NatSimprocs.le_minus_iff_number_of",  (*not directional*)
paulson@18677
    80
   "NatSimprocs.less_minus_iff_1", (*not directional*)
paulson@18677
    81
   "NatSimprocs.less_minus_iff_number_of", (*not directional*)
paulson@18677
    82
   "NatSimprocs.minus_equation_iff_number_of", (*not directional*)
paulson@18677
    83
   "NatSimprocs.minus_le_iff_1", (*not directional*)
paulson@18677
    84
   "NatSimprocs.minus_le_iff_number_of", (*not directional*)
paulson@18677
    85
   "NatSimprocs.minus_less_iff_1", (*not directional*)
paulson@18677
    86
   "NatSimprocs.mult_le_cancel_left_number_of", (*excessive case analysis*)
paulson@18677
    87
   "NatSimprocs.mult_le_cancel_right_number_of", (*excessive case analysis*)
paulson@18677
    88
   "NatSimprocs.mult_less_cancel_left_number_of", (*excessive case analysis*)
paulson@18677
    89
   "NatSimprocs.mult_less_cancel_right_number_of", (*excessive case analysis*)
paulson@18677
    90
   "NatSimprocs.zero_le_divide_iff_number_of",
paulson@18677
    91
   "NatSimprocs.zero_le_divide_iff_number_of", (*excessive case analysis*)
paulson@18677
    92
   "NatSimprocs.zero_less_divide_iff_number_of",
paulson@18677
    93
   "NatSimprocs.zero_less_divide_iff_number_of", (*excessive case analysis*)
paulson@18677
    94
   "OrderedGroup.abs_0_eq",
paulson@18677
    95
   "OrderedGroup.abs_0_eq", (*duplicate by symmetry*)
paulson@18677
    96
   "OrderedGroup.diff_eq_0_iff_eq", (*prolific?*)
paulson@18677
    97
   "OrderedGroup.join_0_eq_0",
paulson@18677
    98
   "OrderedGroup.meet_0_eq_0",
paulson@18677
    99
   "OrderedGroup.pprt_eq_0",   (*obscure*)
paulson@18677
   100
   "OrderedGroup.pprt_eq_id",   (*obscure*)
paulson@18677
   101
   "OrderedGroup.pprt_mono",   (*obscure*)
paulson@18677
   102
   "Parity.even_nat_power",   (*obscure, somewhat prolilfic*)
paulson@18677
   103
   "Parity.power_eq_0_iff_number_of",
paulson@18677
   104
   "Parity.power_eq_0_iff_number_of",
paulson@18677
   105
   "Parity.power_le_zero_eq_number_of",
paulson@18677
   106
   "Parity.power_le_zero_eq_number_of",   (*obscure and prolific*)
paulson@18677
   107
   "Parity.power_less_zero_eq_number_of",
paulson@18677
   108
   "Parity.zero_le_power_eq_number_of",   (*obscure and prolific*)
paulson@18677
   109
   "Parity.zero_less_power_eq_number_of",   (*obscure and prolific*)
paulson@18677
   110
   "Power.zero_less_power_abs_iff",
paulson@18677
   111
   "Relation.diagI",
paulson@18509
   112
   "Relation.ImageI",
paulson@18677
   113
   "Ring_and_Field.divide_cancel_left", (*fields are seldom used & often prolific*)
paulson@18677
   114
   "Ring_and_Field.divide_cancel_right",
paulson@18677
   115
   "Ring_and_Field.divide_divide_eq_left",
paulson@18677
   116
   "Ring_and_Field.divide_divide_eq_right",
paulson@18677
   117
   "Ring_and_Field.divide_eq_0_iff",
paulson@18677
   118
   "Ring_and_Field.divide_eq_1_iff",
paulson@18677
   119
   "Ring_and_Field.divide_eq_eq_1",
paulson@18677
   120
   "Ring_and_Field.divide_le_0_1_iff",
paulson@18677
   121
   "Ring_and_Field.divide_le_eq_1_neg",
paulson@18677
   122
   "Ring_and_Field.divide_le_eq_1_neg",  (*obscure and prolific*)
paulson@18677
   123
   "Ring_and_Field.divide_le_eq_1_pos",
paulson@18677
   124
   "Ring_and_Field.divide_le_eq_1_pos",  (*obscure and prolific*)
paulson@18677
   125
   "Ring_and_Field.divide_less_0_1_iff",
paulson@18677
   126
   "Ring_and_Field.divide_less_eq_1_neg",  (*obscure and prolific*)
paulson@18677
   127
   "Ring_and_Field.divide_less_eq_1_pos",
paulson@18677
   128
   "Ring_and_Field.divide_less_eq_1_pos",  (*obscure and prolific*)
paulson@18677
   129
   "Ring_and_Field.eq_divide_eq_1",
paulson@18677
   130
   "Ring_and_Field.eq_divide_eq_1", (*duplicate by symmetry*)
paulson@18677
   131
   "Ring_and_Field.field_mult_cancel_left",
paulson@18677
   132
   "Ring_and_Field.field_mult_cancel_right",
paulson@18677
   133
   "Ring_and_Field.inverse_le_iff_le_neg",
paulson@18677
   134
   "Ring_and_Field.inverse_le_iff_le",
paulson@18677
   135
   "Ring_and_Field.inverse_less_iff_less_neg",
paulson@18677
   136
   "Ring_and_Field.inverse_less_iff_less",
paulson@18677
   137
   "Ring_and_Field.le_divide_eq_1_neg",
paulson@18677
   138
   "Ring_and_Field.le_divide_eq_1_neg", (*obscure and prolific*)
paulson@18677
   139
   "Ring_and_Field.le_divide_eq_1_pos",
paulson@18677
   140
   "Ring_and_Field.le_divide_eq_1_pos", (*obscure and prolific*)
paulson@18677
   141
   "Ring_and_Field.less_divide_eq_1_neg",
paulson@18677
   142
   "Ring_and_Field.less_divide_eq_1_neg", (*obscure and prolific*)
paulson@18677
   143
   "Ring_and_Field.less_divide_eq_1_pos",
paulson@18677
   144
   "Ring_and_Field.less_divide_eq_1_pos", (*obscure and prolific*)
paulson@18677
   145
   "Ring_and_Field.one_eq_divide_iff",  (*duplicate by symmetry*)
paulson@18677
   146
   "Set.Diff_eq_empty_iff", (*redundant with paramodulation*)
paulson@18509
   147
   "Set.Diff_insert0",
paulson@18677
   148
   "Set.disjoint_insert_1",
paulson@18677
   149
   "Set.disjoint_insert_2",
paulson@18677
   150
   "Set.empty_Union_conv", (*redundant with paramodulation*)
paulson@18677
   151
   "Set.insert_disjoint_1",
paulson@18677
   152
   "Set.insert_disjoint_2",
paulson@18677
   153
   "Set.Int_UNIV", (*redundant with paramodulation*)
paulson@18677
   154
   "Set.Inter_iff",              (*We already have InterI, InterE*)
paulson@18509
   155
   "Set.Inter_UNIV_conv_1",
paulson@18509
   156
   "Set.Inter_UNIV_conv_2",
paulson@18677
   157
   "Set.psubsetE",    (*too prolific and obscure*)
paulson@18677
   158
   "Set.psubsetI",
paulson@18677
   159
   "Set.singleton_insert_inj_eq'",
paulson@18677
   160
   "Set.singleton_insert_inj_eq",
paulson@18677
   161
   "Set.singletonD",  (*these two duplicate some "insert" lemmas*)
paulson@18677
   162
   "Set.singletonI",
paulson@18677
   163
   "Set.Un_empty", (*redundant with paramodulation*)
paulson@18677
   164
   "Set.Union_empty_conv", (*redundant with paramodulation*)
paulson@18509
   165
   "Set.Union_iff",              (*We already have UnionI, UnionE*)
paulson@18677
   166
   "SetInterval.atLeastAtMost_iff", (*obscure and prolific*)
paulson@18677
   167
   "SetInterval.atLeastLessThan_iff", (*obscure and prolific*)
paulson@18677
   168
   "SetInterval.greaterThanAtMost_iff", (*obscure and prolific*)
paulson@18677
   169
   "SetInterval.greaterThanLessThan_iff", (*obscure and prolific*)
paulson@18677
   170
   "SetInterval.ivl_subset", (*excessive case analysis*)
paulson@18509
   171
   "Sum_Type.InlI",
paulson@18677
   172
   "Sum_Type.InrI"];
paulson@18677
   173
   
paulson@18677
   174
(*These might be prolific but are probably OK, and min and max are basic.
paulson@18677
   175
   "Orderings.max_less_iff_conj", 
paulson@18509
   176
   "Orderings.min_less_iff_conj",
paulson@18509
   177
   "Orderings.min_max.below_inf.below_inf_conv",
paulson@18509
   178
   "Orderings.min_max.below_sup.above_sup_conv",
paulson@18677
   179
Very prolific and somewhat obscure:
paulson@18677
   180
   "Set.InterD",
paulson@18677
   181
   "Set.UnionI",
paulson@18509
   182
*)
paulson@18509
   183
paulson@18677
   184
val theory_blacklist = ref
paulson@18677
   185
  ["Datatype_Universe.",    (*unnecessary in virtually all proofs*)
paulson@18677
   186
   "Equiv_Relations."]  
paulson@18677
   187
paulson@18677
   188
paulson@16957
   189
val relevant = ref 0;  (*Relevance filtering is off by default*)
paulson@16795
   190
paulson@16956
   191
(*The "name" of a theorem is its statement, if nothing else is available.*)
paulson@16956
   192
val plain_string_of_thm =
paulson@16956
   193
    setmp show_question_marks false 
paulson@16956
   194
      (setmp print_mode [] 
paulson@16956
   195
	(Pretty.setmp_margin 999 string_of_thm));
paulson@16957
   196
	
paulson@17828
   197
(*Returns the first substring enclosed in quotation marks, typically omitting 
paulson@17828
   198
  the [.] of meta-level assumptions.*)
paulson@17828
   199
val firstquoted = hd o (String.tokens (fn c => c = #"\""))
paulson@17828
   200
	
paulson@16957
   201
fun fake_thm_name th = 
paulson@17828
   202
    Context.theory_name (theory_of_thm th) ^ "." ^ firstquoted (plain_string_of_thm th);
paulson@16061
   203
paulson@17828
   204
fun put_name_pair ("",th) = (fake_thm_name th, th)
paulson@16956
   205
  | put_name_pair (a,th)  = (a,th);
quigley@16039
   206
quigley@16039
   207
(* outputs a list of (thm,clause) pairs *)
quigley@15643
   208
paulson@17305
   209
fun multi x 0 xlist = xlist
paulson@17305
   210
   |multi x n xlist = multi x (n-1) (x::xlist);
quigley@16156
   211
paulson@17305
   212
fun clause_numbering ((clause, theorem), num_of_cls) = 
paulson@17305
   213
    let val numbers = 0 upto (num_of_cls - 1)
paulson@16172
   214
    in 
paulson@17305
   215
	multi (clause, theorem) num_of_cls []
paulson@16172
   216
    end;
paulson@17234
   217
    
paulson@18677
   218
(*Hashing to detect duplicate and variant clauses, e.g. from the [iff] attribute*)
paulson@18420
   219
paulson@18449
   220
exception HASH_CLAUSE and HASH_STRING;
paulson@18449
   221
paulson@18449
   222
(*Catches (for deletion) theorems automatically generated from other theorems*)
paulson@18449
   223
fun insert_suffixed_names ht x = 
paulson@18449
   224
     (Polyhash.insert ht (x^"_iff1", ()); 
paulson@18449
   225
      Polyhash.insert ht (x^"_iff2", ()); 
paulson@18449
   226
      Polyhash.insert ht (x^"_dest", ())); 
paulson@18449
   227
paulson@18677
   228
fun banned_theory s = exists (fn thy => String.isPrefix thy s) (!theory_blacklist);
paulson@18677
   229
paulson@18509
   230
fun make_banned_test xs = 
paulson@18449
   231
  let val ht = Polyhash.mkTable (Polyhash.hash_string, op =)
paulson@18449
   232
                                (6000, HASH_STRING)
paulson@18677
   233
      fun banned s = isSome (Polyhash.peek ht s) orelse banned_theory s
paulson@18509
   234
  in  app (fn x => Polyhash.insert ht (x,())) (!blacklist);
paulson@18509
   235
      app (insert_suffixed_names ht) (!blacklist @ xs); 
paulson@18509
   236
      banned
paulson@18509
   237
  end;
paulson@18420
   238
paulson@18420
   239
(*Create a hash table for clauses, of the given size*)
paulson@18449
   240
fun mk_clause_table n =
paulson@18449
   241
      Polyhash.mkTable (ResClause.hash_clause, ResClause.clause_eq)
paulson@18449
   242
                       (n, HASH_CLAUSE);
paulson@18420
   243
paulson@18420
   244
(*Use a hash table to eliminate duplicates from xs*)
paulson@18449
   245
fun make_unique ht xs = 
paulson@18449
   246
      (app (ignore o Polyhash.peekInsert ht) xs;  Polyhash.listItems ht);
quigley@16950
   247
paulson@16741
   248
(*Write out the claset and simpset rules of the supplied theory.
paulson@16906
   249
  FIXME: argument "goal" is a hack to allow relevance filtering.
paulson@16906
   250
  To reduce the number of clauses produced, set ResClasimp.relevant:=1*)
paulson@18753
   251
fun get_clasimp_lemmas ctxt goals = 
paulson@18420
   252
  let val claset_thms =
mengj@18792
   253
	  map put_name_pair (ResAxioms.claset_rules_of_ctxt ctxt)
paulson@18449
   254
      val simpset_thms = 
paulson@18420
   255
	    if !use_simpset then 
mengj@18792
   256
		map put_name_pair (ResAxioms.simpset_rules_of_ctxt ctxt)
paulson@18420
   257
	    else []
paulson@18509
   258
      val banned = make_banned_test (map #1 (claset_thms@simpset_thms))
paulson@18509
   259
      fun ok (a,_) = not (banned a)
paulson@18449
   260
      val claset_cls_thms = ResAxioms.clausify_rules_pairs (filter ok claset_thms)
paulson@18449
   261
      val simpset_cls_thms = ResAxioms.clausify_rules_pairs (filter ok simpset_thms)
mengj@18792
   262
 
mengj@18792
   263
mengj@18792
   264
     val cls_thms_list = make_unique (mk_clause_table 2200) 
paulson@18420
   265
                                      (List.concat (simpset_cls_thms@claset_cls_thms))
mengj@18792
   266
mengj@18792
   267
     val relevant_cls_thms_list = ReduceAxiomsN.relevance_filter cls_thms_list goals
paulson@18420
   268
      (* Identify the set of clauses to be written out *)
mengj@18792
   269
      val clauses = map #1(relevant_cls_thms_list);
paulson@18420
   270
      val cls_nums = map ResClause.num_of_clauses clauses;
paulson@18420
   271
      (*Note: in every case, cls_num = 1.  I think that only conjecture clauses
paulson@18420
   272
	can have any other value.*)
paulson@18420
   273
      val whole_list = List.concat 
mengj@18792
   274
	    (map clause_numbering (ListPair.zip (relevant_cls_thms_list, cls_nums)));
paulson@18420
   275
      
paulson@17828
   276
  in  (* create array of put clausename, number pairs into it *)
paulson@17828
   277
      (Array.fromList whole_list, clauses)
paulson@16061
   278
  end;
quigley@15643
   279
quigley@16950
   280
quigley@15919
   281
end;
quigley@16039
   282
quigley@16156
   283
quigley@16156
   284