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