src/HOL/Tools/ATP/res_clasimpset.ML
author haftmann
Fri Feb 10 09:09:07 2006 +0100 (2006-02-10)
changeset 19008 14c1b2f5dda4
parent 18985 bc23b1d1ddea
child 19010 fef9e4881e83
permissions -rw-r--r--
improved code generator devarification
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@18985
    26
  ["Datatype.not_None_eq",    (*Says everything is None or Some. Probably prolific.*)
paulson@18985
    27
   "Datatype.not_Some_eq_D",  (*Says everything is None or Some. Probably prolific.*)
paulson@18985
    28
   "Datatype.not_Some_eq",    (*Says everything is None or Some. Probably prolific.*)
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@18985
    60
   "List.hd_Cons_tl",   (*Says everything is [] or Cons. Probably prolific.*)
paulson@18509
    61
   "List.in_listsD",
paulson@18509
    62
   "List.in_listsI",
paulson@18677
    63
   "List.lists.Cons",
paulson@18509
    64
   "List.listsE",
paulson@18677
    65
   "List.take_eq_Nil",
paulson@18677
    66
   "Nat.less_one",
paulson@18677
    67
   "Nat.less_one", (*not directional? obscure*)
paulson@18677
    68
   "Nat.not_gr0",
paulson@18677
    69
   "Nat.one_eq_mult_iff", (*duplicate by symmetry*)
paulson@18677
    70
   "NatArith.of_nat_0_eq_iff",
paulson@18677
    71
   "NatArith.of_nat_eq_0_iff",
paulson@18677
    72
   "NatArith.of_nat_le_0_iff",
paulson@18677
    73
   "NatSimprocs.divide_le_0_iff_number_of",  (*seldom used; often prolific*)
paulson@18677
    74
   "NatSimprocs.divide_le_0_iff_number_of",  (*too many clauses*)
paulson@18677
    75
   "NatSimprocs.divide_less_0_iff_number_of",
paulson@18677
    76
   "NatSimprocs.divide_less_0_iff_number_of",   (*too many clauses*)
paulson@18677
    77
   "NatSimprocs.equation_minus_iff_1",  (*not directional*)
paulson@18677
    78
   "NatSimprocs.equation_minus_iff_number_of", (*not directional*)
paulson@18677
    79
   "NatSimprocs.le_minus_iff_1", (*not directional*)
paulson@18677
    80
   "NatSimprocs.le_minus_iff_number_of",  (*not directional*)
paulson@18677
    81
   "NatSimprocs.less_minus_iff_1", (*not directional*)
paulson@18677
    82
   "NatSimprocs.less_minus_iff_number_of", (*not directional*)
paulson@18677
    83
   "NatSimprocs.minus_equation_iff_number_of", (*not directional*)
paulson@18677
    84
   "NatSimprocs.minus_le_iff_1", (*not directional*)
paulson@18677
    85
   "NatSimprocs.minus_le_iff_number_of", (*not directional*)
paulson@18677
    86
   "NatSimprocs.minus_less_iff_1", (*not directional*)
paulson@18677
    87
   "NatSimprocs.mult_le_cancel_left_number_of", (*excessive case analysis*)
paulson@18677
    88
   "NatSimprocs.mult_le_cancel_right_number_of", (*excessive case analysis*)
paulson@18677
    89
   "NatSimprocs.mult_less_cancel_left_number_of", (*excessive case analysis*)
paulson@18677
    90
   "NatSimprocs.mult_less_cancel_right_number_of", (*excessive case analysis*)
paulson@18677
    91
   "NatSimprocs.zero_le_divide_iff_number_of",
paulson@18677
    92
   "NatSimprocs.zero_le_divide_iff_number_of", (*excessive case analysis*)
paulson@18677
    93
   "NatSimprocs.zero_less_divide_iff_number_of",
paulson@18677
    94
   "NatSimprocs.zero_less_divide_iff_number_of", (*excessive case analysis*)
paulson@18677
    95
   "OrderedGroup.abs_0_eq",
paulson@18677
    96
   "OrderedGroup.abs_0_eq", (*duplicate by symmetry*)
paulson@18677
    97
   "OrderedGroup.diff_eq_0_iff_eq", (*prolific?*)
paulson@18677
    98
   "OrderedGroup.join_0_eq_0",
paulson@18677
    99
   "OrderedGroup.meet_0_eq_0",
paulson@18677
   100
   "OrderedGroup.pprt_eq_0",   (*obscure*)
paulson@18677
   101
   "OrderedGroup.pprt_eq_id",   (*obscure*)
paulson@18677
   102
   "OrderedGroup.pprt_mono",   (*obscure*)
paulson@18677
   103
   "Parity.even_nat_power",   (*obscure, somewhat prolilfic*)
paulson@18677
   104
   "Parity.power_eq_0_iff_number_of",
paulson@18677
   105
   "Parity.power_eq_0_iff_number_of",
paulson@18677
   106
   "Parity.power_le_zero_eq_number_of",
paulson@18677
   107
   "Parity.power_le_zero_eq_number_of",   (*obscure and prolific*)
paulson@18677
   108
   "Parity.power_less_zero_eq_number_of",
paulson@18677
   109
   "Parity.zero_le_power_eq_number_of",   (*obscure and prolific*)
paulson@18677
   110
   "Parity.zero_less_power_eq_number_of",   (*obscure and prolific*)
paulson@18677
   111
   "Power.zero_less_power_abs_iff",
paulson@18677
   112
   "Relation.diagI",
paulson@18509
   113
   "Relation.ImageI",
paulson@18677
   114
   "Ring_and_Field.divide_cancel_left", (*fields are seldom used & often prolific*)
paulson@18677
   115
   "Ring_and_Field.divide_cancel_right",
paulson@18677
   116
   "Ring_and_Field.divide_divide_eq_left",
paulson@18677
   117
   "Ring_and_Field.divide_divide_eq_right",
paulson@18677
   118
   "Ring_and_Field.divide_eq_0_iff",
paulson@18677
   119
   "Ring_and_Field.divide_eq_1_iff",
paulson@18677
   120
   "Ring_and_Field.divide_eq_eq_1",
paulson@18677
   121
   "Ring_and_Field.divide_le_0_1_iff",
paulson@18677
   122
   "Ring_and_Field.divide_le_eq_1_neg",
paulson@18677
   123
   "Ring_and_Field.divide_le_eq_1_neg",  (*obscure and prolific*)
paulson@18677
   124
   "Ring_and_Field.divide_le_eq_1_pos",
paulson@18677
   125
   "Ring_and_Field.divide_le_eq_1_pos",  (*obscure and prolific*)
paulson@18677
   126
   "Ring_and_Field.divide_less_0_1_iff",
paulson@18677
   127
   "Ring_and_Field.divide_less_eq_1_neg",  (*obscure and prolific*)
paulson@18677
   128
   "Ring_and_Field.divide_less_eq_1_pos",
paulson@18677
   129
   "Ring_and_Field.divide_less_eq_1_pos",  (*obscure and prolific*)
paulson@18677
   130
   "Ring_and_Field.eq_divide_eq_1",
paulson@18677
   131
   "Ring_and_Field.eq_divide_eq_1", (*duplicate by symmetry*)
paulson@18677
   132
   "Ring_and_Field.field_mult_cancel_left",
paulson@18677
   133
   "Ring_and_Field.field_mult_cancel_right",
paulson@18677
   134
   "Ring_and_Field.inverse_le_iff_le_neg",
paulson@18677
   135
   "Ring_and_Field.inverse_le_iff_le",
paulson@18677
   136
   "Ring_and_Field.inverse_less_iff_less_neg",
paulson@18677
   137
   "Ring_and_Field.inverse_less_iff_less",
paulson@18677
   138
   "Ring_and_Field.le_divide_eq_1_neg",
paulson@18677
   139
   "Ring_and_Field.le_divide_eq_1_neg", (*obscure and prolific*)
paulson@18677
   140
   "Ring_and_Field.le_divide_eq_1_pos",
paulson@18677
   141
   "Ring_and_Field.le_divide_eq_1_pos", (*obscure and prolific*)
paulson@18677
   142
   "Ring_and_Field.less_divide_eq_1_neg",
paulson@18677
   143
   "Ring_and_Field.less_divide_eq_1_neg", (*obscure and prolific*)
paulson@18677
   144
   "Ring_and_Field.less_divide_eq_1_pos",
paulson@18677
   145
   "Ring_and_Field.less_divide_eq_1_pos", (*obscure and prolific*)
paulson@18677
   146
   "Ring_and_Field.one_eq_divide_iff",  (*duplicate by symmetry*)
paulson@18677
   147
   "Set.Diff_eq_empty_iff", (*redundant with paramodulation*)
paulson@18509
   148
   "Set.Diff_insert0",
paulson@18677
   149
   "Set.disjoint_insert_1",
paulson@18677
   150
   "Set.disjoint_insert_2",
paulson@18677
   151
   "Set.empty_Union_conv", (*redundant with paramodulation*)
paulson@18677
   152
   "Set.insert_disjoint_1",
paulson@18677
   153
   "Set.insert_disjoint_2",
paulson@18677
   154
   "Set.Int_UNIV", (*redundant with paramodulation*)
paulson@18677
   155
   "Set.Inter_iff",              (*We already have InterI, InterE*)
paulson@18509
   156
   "Set.Inter_UNIV_conv_1",
paulson@18509
   157
   "Set.Inter_UNIV_conv_2",
paulson@18677
   158
   "Set.psubsetE",    (*too prolific and obscure*)
paulson@18677
   159
   "Set.psubsetI",
paulson@18677
   160
   "Set.singleton_insert_inj_eq'",
paulson@18677
   161
   "Set.singleton_insert_inj_eq",
paulson@18677
   162
   "Set.singletonD",  (*these two duplicate some "insert" lemmas*)
paulson@18677
   163
   "Set.singletonI",
paulson@18677
   164
   "Set.Un_empty", (*redundant with paramodulation*)
paulson@18677
   165
   "Set.Union_empty_conv", (*redundant with paramodulation*)
paulson@18509
   166
   "Set.Union_iff",              (*We already have UnionI, UnionE*)
paulson@18677
   167
   "SetInterval.atLeastAtMost_iff", (*obscure and prolific*)
paulson@18677
   168
   "SetInterval.atLeastLessThan_iff", (*obscure and prolific*)
paulson@18677
   169
   "SetInterval.greaterThanAtMost_iff", (*obscure and prolific*)
paulson@18677
   170
   "SetInterval.greaterThanLessThan_iff", (*obscure and prolific*)
paulson@18677
   171
   "SetInterval.ivl_subset", (*excessive case analysis*)
paulson@18509
   172
   "Sum_Type.InlI",
paulson@18677
   173
   "Sum_Type.InrI"];
paulson@18677
   174
   
paulson@18677
   175
(*These might be prolific but are probably OK, and min and max are basic.
paulson@18677
   176
   "Orderings.max_less_iff_conj", 
paulson@18509
   177
   "Orderings.min_less_iff_conj",
paulson@18509
   178
   "Orderings.min_max.below_inf.below_inf_conv",
paulson@18509
   179
   "Orderings.min_max.below_sup.above_sup_conv",
paulson@18677
   180
Very prolific and somewhat obscure:
paulson@18677
   181
   "Set.InterD",
paulson@18677
   182
   "Set.UnionI",
paulson@18509
   183
*)
paulson@18509
   184
paulson@18677
   185
val theory_blacklist = ref
paulson@18677
   186
  ["Datatype_Universe.",    (*unnecessary in virtually all proofs*)
paulson@18677
   187
   "Equiv_Relations."]  
paulson@18677
   188
paulson@18677
   189
paulson@16957
   190
val relevant = ref 0;  (*Relevance filtering is off by default*)
paulson@16795
   191
paulson@16956
   192
(*The "name" of a theorem is its statement, if nothing else is available.*)
paulson@16956
   193
val plain_string_of_thm =
paulson@16956
   194
    setmp show_question_marks false 
paulson@16956
   195
      (setmp print_mode [] 
paulson@16956
   196
	(Pretty.setmp_margin 999 string_of_thm));
paulson@16957
   197
	
paulson@17828
   198
(*Returns the first substring enclosed in quotation marks, typically omitting 
paulson@17828
   199
  the [.] of meta-level assumptions.*)
paulson@17828
   200
val firstquoted = hd o (String.tokens (fn c => c = #"\""))
paulson@17828
   201
	
paulson@16957
   202
fun fake_thm_name th = 
paulson@17828
   203
    Context.theory_name (theory_of_thm th) ^ "." ^ firstquoted (plain_string_of_thm th);
paulson@16061
   204
paulson@17828
   205
fun put_name_pair ("",th) = (fake_thm_name th, th)
paulson@16956
   206
  | put_name_pair (a,th)  = (a,th);
quigley@16039
   207
paulson@18677
   208
(*Hashing to detect duplicate and variant clauses, e.g. from the [iff] attribute*)
paulson@18420
   209
paulson@18449
   210
exception HASH_CLAUSE and HASH_STRING;
paulson@18449
   211
paulson@18449
   212
(*Catches (for deletion) theorems automatically generated from other theorems*)
paulson@18449
   213
fun insert_suffixed_names ht x = 
paulson@18449
   214
     (Polyhash.insert ht (x^"_iff1", ()); 
paulson@18449
   215
      Polyhash.insert ht (x^"_iff2", ()); 
paulson@18449
   216
      Polyhash.insert ht (x^"_dest", ())); 
paulson@18449
   217
paulson@18677
   218
fun banned_theory s = exists (fn thy => String.isPrefix thy s) (!theory_blacklist);
paulson@18677
   219
paulson@18509
   220
fun make_banned_test xs = 
paulson@18449
   221
  let val ht = Polyhash.mkTable (Polyhash.hash_string, op =)
paulson@18449
   222
                                (6000, HASH_STRING)
paulson@18677
   223
      fun banned s = isSome (Polyhash.peek ht s) orelse banned_theory s
paulson@18509
   224
  in  app (fn x => Polyhash.insert ht (x,())) (!blacklist);
paulson@18509
   225
      app (insert_suffixed_names ht) (!blacklist @ xs); 
paulson@18509
   226
      banned
paulson@18509
   227
  end;
paulson@18420
   228
paulson@18420
   229
(*Create a hash table for clauses, of the given size*)
paulson@18449
   230
fun mk_clause_table n =
paulson@18449
   231
      Polyhash.mkTable (ResClause.hash_clause, ResClause.clause_eq)
paulson@18449
   232
                       (n, HASH_CLAUSE);
paulson@18420
   233
paulson@18420
   234
(*Use a hash table to eliminate duplicates from xs*)
paulson@18449
   235
fun make_unique ht xs = 
paulson@18449
   236
      (app (ignore o Polyhash.peekInsert ht) xs;  Polyhash.listItems ht);
quigley@16950
   237
paulson@16741
   238
(*Write out the claset and simpset rules of the supplied theory.
paulson@16906
   239
  FIXME: argument "goal" is a hack to allow relevance filtering.
paulson@16906
   240
  To reduce the number of clauses produced, set ResClasimp.relevant:=1*)
paulson@18753
   241
fun get_clasimp_lemmas ctxt goals = 
paulson@18420
   242
  let val claset_thms =
mengj@18792
   243
	  map put_name_pair (ResAxioms.claset_rules_of_ctxt ctxt)
paulson@18449
   244
      val simpset_thms = 
paulson@18420
   245
	    if !use_simpset then 
mengj@18792
   246
		map put_name_pair (ResAxioms.simpset_rules_of_ctxt ctxt)
paulson@18420
   247
	    else []
paulson@18509
   248
      val banned = make_banned_test (map #1 (claset_thms@simpset_thms))
paulson@18509
   249
      fun ok (a,_) = not (banned a)
paulson@18449
   250
      val claset_cls_thms = ResAxioms.clausify_rules_pairs (filter ok claset_thms)
paulson@18449
   251
      val simpset_cls_thms = ResAxioms.clausify_rules_pairs (filter ok simpset_thms)
mengj@18792
   252
 
paulson@18797
   253
      val cls_thms_list = make_unique (mk_clause_table 2200) 
paulson@18420
   254
                                      (List.concat (simpset_cls_thms@claset_cls_thms))
paulson@18797
   255
      val relevant_cls_thms_list = ReduceAxiomsN.relevance_filter cls_thms_list goals
paulson@17828
   256
  in  (* create array of put clausename, number pairs into it *)
paulson@18869
   257
      (Array.fromList relevant_cls_thms_list, map #1 (relevant_cls_thms_list))
paulson@16061
   258
  end;
quigley@15643
   259
quigley@16950
   260
quigley@15919
   261
end;
quigley@16039
   262
quigley@16156
   263
quigley@16156
   264