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