src/HOL/Tools/ATP/reduce_axiomsN.ML
author haftmann
Mon, 16 Jul 2007 09:29:04 +0200
changeset 23811 b18557301bf9
parent 22194 3b1da1ff65df
child 24195 7d1a16c77f7c
permissions -rw-r--r--
added function for case certificates
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19208
3e8006cbc925 Tidying and restructuring.
paulson
parents: 19200
diff changeset
     1
(* Authors: Jia Meng, NICTA and Lawrence C Paulson, Cambridge University Computer Laboratory
3e8006cbc925 Tidying and restructuring.
paulson
parents: 19200
diff changeset
     2
   ID: $Id$
3e8006cbc925 Tidying and restructuring.
paulson
parents: 19200
diff changeset
     3
   Filtering strategies *)
3e8006cbc925 Tidying and restructuring.
paulson
parents: 19200
diff changeset
     4
20457
85414caac94a refinements to conversion into clause form, esp for the HO case
paulson
parents: 20423
diff changeset
     5
(*A surprising number of theorems contain only a few significant constants.
85414caac94a refinements to conversion into clause form, esp for the HO case
paulson
parents: 20423
diff changeset
     6
  These include all induction rules, and other general theorems. Filtering
85414caac94a refinements to conversion into clause form, esp for the HO case
paulson
parents: 20423
diff changeset
     7
  theorems in clause form reveals these complexities in the form of Skolem 
85414caac94a refinements to conversion into clause form, esp for the HO case
paulson
parents: 20423
diff changeset
     8
  functions. If we were instead to filter theorems in their natural form,
85414caac94a refinements to conversion into clause form, esp for the HO case
paulson
parents: 20423
diff changeset
     9
  some other method of measuring theorem complexity would become necessary.*)
85414caac94a refinements to conversion into clause form, esp for the HO case
paulson
parents: 20423
diff changeset
    10
18791
9b4ae9fa67a4 Relevance filtering. Has replaced the previous version.
mengj
parents:
diff changeset
    11
structure ReduceAxiomsN =
9b4ae9fa67a4 Relevance filtering. Has replaced the previous version.
mengj
parents:
diff changeset
    12
struct
9b4ae9fa67a4 Relevance filtering. Has replaced the previous version.
mengj
parents:
diff changeset
    13
20457
85414caac94a refinements to conversion into clause form, esp for the HO case
paulson
parents: 20423
diff changeset
    14
val run_relevance_filter = ref true;
20825
4b48fd429b18 Changing the default for theory_const
paulson
parents: 20566
diff changeset
    15
val theory_const = ref true;
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
    16
val pass_mark = ref 0.5;
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
    17
val convergence = ref 3.2;    (*Higher numbers allow longer inference chains*)
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
    18
val max_new = ref 60;         (*Limits how many clauses can be picked up per stage*)
20423
593053389701 minor bug fixes
paulson
parents: 20197
diff changeset
    19
val follow_defs = ref false;  (*Follow definitions. Makes problems bigger.*)
18791
9b4ae9fa67a4 Relevance filtering. Has replaced the previous version.
mengj
parents:
diff changeset
    20
19337
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
    21
fun log_weight2 (x:real) = 1.0 + 2.0/Math.ln (x+1.0);
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
    22
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
    23
(*The default seems best in practice. A constant function of one ignores
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
    24
  the constant frequencies.*)
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
    25
val weight_fn = ref log_weight2;
19334
96ca738055a6 Simplified version of Jia's filter. Now all constants are pooled, rather than
paulson
parents: 19321
diff changeset
    26
18791
9b4ae9fa67a4 Relevance filtering. Has replaced the previous version.
mengj
parents:
diff changeset
    27
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
    28
(*Including equality in this list might be expected to stop rules like subset_antisym from
20527
958ec4833d87 Added the "theory_const" option. Only it is OFF because it's often harmful!
paulson
parents: 20457
diff changeset
    29
  being chosen, but for some reason filtering works better with them listed. The
958ec4833d87 Added the "theory_const" option. Only it is OFF because it's often harmful!
paulson
parents: 20457
diff changeset
    30
  logical signs All, Ex, &, and --> are omitted because any remaining occurrrences
958ec4833d87 Added the "theory_const" option. Only it is OFF because it's often harmful!
paulson
parents: 20457
diff changeset
    31
  must be within comprehensions.*)
958ec4833d87 Added the "theory_const" option. Only it is OFF because it's often harmful!
paulson
parents: 20457
diff changeset
    32
val standard_consts = ["Trueprop","==>","all","==","op |","Not","op ="];
18791
9b4ae9fa67a4 Relevance filtering. Has replaced the previous version.
mengj
parents:
diff changeset
    33
9b4ae9fa67a4 Relevance filtering. Has replaced the previous version.
mengj
parents:
diff changeset
    34
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
    35
(*** constants with types ***)
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
    36
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
    37
(*An abstraction of Isabelle types*)
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
    38
datatype const_typ =  CTVar | CType of string * const_typ list
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
    39
20153
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
    40
(*Is the second type an instance of the first one?*)
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
    41
fun match_type (CType(con1,args1)) (CType(con2,args2)) = 
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
    42
      con1=con2 andalso match_types args1 args2
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
    43
  | match_type CTVar _ = true
20153
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
    44
  | match_type _ CTVar = false
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
    45
and match_types [] [] = true
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
    46
  | match_types (a1::as1) (a2::as2) = match_type a1 a2 andalso match_types as1 as2;
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
    47
20132
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    48
(*Is there a unifiable constant?*)
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    49
fun uni_mem gctab (c,c_typ) =
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    50
  case Symtab.lookup gctab c of
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    51
      NONE => false
20153
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
    52
    | SOME ctyps_list => exists (match_types c_typ) ctyps_list;
20132
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    53
  
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
    54
(*Maps a "real" type to a const_typ*)
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
    55
fun const_typ_of (Type (c,typs)) = CType (c, map const_typ_of typs) 
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
    56
  | const_typ_of (TFree _) = CTVar
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
    57
  | const_typ_of (TVar _) = CTVar
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
    58
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
    59
(*Pairs a constant with the list of its type instantiations (using const_typ)*)
19315
b218cc3d1bb4 Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents: 19231
diff changeset
    60
fun const_with_typ thy (c,typ) = 
19212
ec53c138277a Frequency strategy. Revised indentation, etc.
paulson
parents: 19208
diff changeset
    61
    let val tvars = Sign.const_typargs thy (c,typ)
19315
b218cc3d1bb4 Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents: 19231
diff changeset
    62
    in (c, map const_typ_of tvars) end
b218cc3d1bb4 Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents: 19231
diff changeset
    63
    handle TYPE _ => (c,[]);   (*Variable (locale constant): monomorphic*)   
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
    64
20153
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
    65
(*Add a const/type pair to the table, but a [] entry means a standard connective,
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
    66
  which we ignore.*)
20132
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    67
fun add_const_typ_table ((c,ctyps), tab) =
20854
f9cf9e62d11c insert replacing ins ins_int ins_string
haftmann
parents: 20825
diff changeset
    68
  Symtab.map_default (c, [ctyps]) (fn [] => [] | ctyps_list => insert (op =) ctyps ctyps_list) 
20153
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
    69
    tab;
20132
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    70
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
    71
(*Free variables are included, as well as constants, to handle locales*)
20132
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    72
fun add_term_consts_typs_rm thy (Const(c, typ), tab) =
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    73
      add_const_typ_table (const_with_typ thy (c,typ), tab) 
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    74
  | add_term_consts_typs_rm thy (Free(c, typ), tab) =
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    75
      add_const_typ_table (const_with_typ thy (c,typ), tab) 
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    76
  | add_term_consts_typs_rm thy (t $ u, tab) =
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    77
      add_term_consts_typs_rm thy (t, add_term_consts_typs_rm thy (u, tab))
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    78
  | add_term_consts_typs_rm thy (Abs(_,_,t), tab) = add_term_consts_typs_rm thy (t, tab)
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    79
  | add_term_consts_typs_rm thy (_, tab) = tab;
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
    80
20132
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    81
(*The empty list here indicates that the constant is being ignored*)
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    82
fun add_standard_const (s,tab) = Symtab.update (s,[]) tab;
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
    83
20132
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    84
val null_const_tab : const_typ list list Symtab.table = 
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    85
    foldl add_standard_const Symtab.empty standard_consts;
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    86
20457
85414caac94a refinements to conversion into clause form, esp for the HO case
paulson
parents: 20423
diff changeset
    87
fun get_goal_consts_typs thy = foldl (add_term_consts_typs_rm thy) null_const_tab;
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
    88
20527
958ec4833d87 Added the "theory_const" option. Only it is OFF because it's often harmful!
paulson
parents: 20457
diff changeset
    89
(*Inserts a dummy "constant" referring to the theory name, so that relevance
958ec4833d87 Added the "theory_const" option. Only it is OFF because it's often harmful!
paulson
parents: 20457
diff changeset
    90
  takes the given theory into account.*)
958ec4833d87 Added the "theory_const" option. Only it is OFF because it's often harmful!
paulson
parents: 20457
diff changeset
    91
fun const_prop_of th =
958ec4833d87 Added the "theory_const" option. Only it is OFF because it's often harmful!
paulson
parents: 20457
diff changeset
    92
 if !theory_const then
958ec4833d87 Added the "theory_const" option. Only it is OFF because it's often harmful!
paulson
parents: 20457
diff changeset
    93
  let val name = Context.theory_name (theory_of_thm th)
958ec4833d87 Added the "theory_const" option. Only it is OFF because it's often harmful!
paulson
parents: 20457
diff changeset
    94
      val t = Const (name ^ ". 1", HOLogic.boolT)
958ec4833d87 Added the "theory_const" option. Only it is OFF because it's often harmful!
paulson
parents: 20457
diff changeset
    95
  in  t $ prop_of th  end
958ec4833d87 Added the "theory_const" option. Only it is OFF because it's often harmful!
paulson
parents: 20457
diff changeset
    96
 else prop_of th;
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
    97
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
    98
(**** Constant / Type Frequencies ****)
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
    99
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   100
(*A two-dimensional symbol table counts frequencies of constants. It's keyed first by
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   101
  constant name and second by its list of type instantiations. For the latter, we need
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   102
  a linear ordering on type const_typ list.*)
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   103
  
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   104
local
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   105
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   106
fun cons_nr CTVar = 0
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   107
  | cons_nr (CType _) = 1;
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   108
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   109
in
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   110
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   111
fun const_typ_ord TU =
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   112
  case TU of
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   113
    (CType (a, Ts), CType (b, Us)) =>
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   114
      (case fast_string_ord(a,b) of EQUAL => dict_ord const_typ_ord (Ts,Us) | ord => ord)
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   115
  | (T, U) => int_ord (cons_nr T, cons_nr U);
19212
ec53c138277a Frequency strategy. Revised indentation, etc.
paulson
parents: 19208
diff changeset
   116
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   117
end;
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   118
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   119
structure CTtab = TableFun(type key = const_typ list val ord = dict_ord const_typ_ord);
19212
ec53c138277a Frequency strategy. Revised indentation, etc.
paulson
parents: 19208
diff changeset
   120
19355
3140daf6863d filter now accepts axioms as thm, instead of term.
mengj
parents: 19337
diff changeset
   121
fun count_axiom_consts thy ((thm,_), tab) = 
19315
b218cc3d1bb4 Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents: 19231
diff changeset
   122
  let fun count_const (a, T, tab) =
b218cc3d1bb4 Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents: 19231
diff changeset
   123
	let val (c, cts) = const_with_typ thy (a,T)
20153
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
   124
	in  (*Two-dimensional table update. Constant maps to types maps to count.*)
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
   125
	    Symtab.map_default (c, CTtab.empty) 
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
   126
	                       (CTtab.map_default (cts,0) (fn n => n+1)) tab
19315
b218cc3d1bb4 Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents: 19231
diff changeset
   127
	end
b218cc3d1bb4 Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents: 19231
diff changeset
   128
      fun count_term_consts (Const(a,T), tab) = count_const(a,T,tab)
b218cc3d1bb4 Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents: 19231
diff changeset
   129
	| count_term_consts (Free(a,T), tab) = count_const(a,T,tab)
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   130
	| count_term_consts (t $ u, tab) =
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   131
	    count_term_consts (t, count_term_consts (u, tab))
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   132
	| count_term_consts (Abs(_,_,t), tab) = count_term_consts (t, tab)
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   133
	| count_term_consts (_, tab) = tab
20527
958ec4833d87 Added the "theory_const" option. Only it is OFF because it's often harmful!
paulson
parents: 20457
diff changeset
   134
  in  count_term_consts (const_prop_of thm, tab)  end;
19212
ec53c138277a Frequency strategy. Revised indentation, etc.
paulson
parents: 19208
diff changeset
   135
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
   136
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   137
(**** Actual Filtering Code ****)
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
   138
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   139
(*The frequency of a constant is the sum of those of all instances of its type.*)
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   140
fun const_frequency ctab (c, cts) =
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   141
  let val pairs = CTtab.dest (Option.valOf (Symtab.lookup ctab c))
20153
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
   142
      fun add ((cts',m), n) = if match_types cts cts' then m+n else n
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   143
  in  List.foldl add 0 pairs  end;
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   144
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   145
(*Add in a constant's weight, as determined by its frequency.*)
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   146
fun add_ct_weight ctab ((c,T), w) =
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   147
  w + !weight_fn (real (const_frequency ctab (c,T)));
19212
ec53c138277a Frequency strategy. Revised indentation, etc.
paulson
parents: 19208
diff changeset
   148
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   149
(*Relevant constants are weighted according to frequency, 
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   150
  but irrelevant constants are simply counted. Otherwise, Skolem functions,
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   151
  which are rare, would harm a clause's chances of being picked.*)
19315
b218cc3d1bb4 Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents: 19231
diff changeset
   152
fun clause_weight ctab gctyps consts_typs =
20132
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
   153
    let val rel = filter (uni_mem gctyps) consts_typs
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   154
        val rel_weight = List.foldl (add_ct_weight ctab) 0.0 rel
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
   155
    in
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   156
	rel_weight / (rel_weight + real (length consts_typs - length rel))
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
   157
    end;
19315
b218cc3d1bb4 Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents: 19231
diff changeset
   158
    
20153
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
   159
(*Multiplies out to a list of pairs: 'a * 'b list -> ('a * 'b) list -> ('a * 'b) list*)
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   160
fun add_expand_pairs (x,ys) xys = foldl (fn (y,acc) => (x,y)::acc) xys ys;
20132
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
   161
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
   162
fun consts_typs_of_term thy t = 
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
   163
  let val tab = add_term_consts_typs_rm thy (t, null_const_tab)
20153
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
   164
  in  Symtab.fold add_expand_pairs tab []  end;
20132
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
   165
19355
3140daf6863d filter now accepts axioms as thm, instead of term.
mengj
parents: 19337
diff changeset
   166
fun pair_consts_typs_axiom thy (thm,name) =
20527
958ec4833d87 Added the "theory_const" option. Only it is OFF because it's often harmful!
paulson
parents: 20457
diff changeset
   167
    ((thm,name), (consts_typs_of_term thy (const_prop_of thm)));
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
   168
19321
30b5bb35dd33 detection of definitions of relevant constants
paulson
parents: 19315
diff changeset
   169
exception ConstFree;
30b5bb35dd33 detection of definitions of relevant constants
paulson
parents: 19315
diff changeset
   170
fun dest_ConstFree (Const aT) = aT
30b5bb35dd33 detection of definitions of relevant constants
paulson
parents: 19315
diff changeset
   171
  | dest_ConstFree (Free aT) = aT
30b5bb35dd33 detection of definitions of relevant constants
paulson
parents: 19315
diff changeset
   172
  | dest_ConstFree _ = raise ConstFree;
30b5bb35dd33 detection of definitions of relevant constants
paulson
parents: 19315
diff changeset
   173
30b5bb35dd33 detection of definitions of relevant constants
paulson
parents: 19315
diff changeset
   174
(*Look for definitions of the form f ?x1 ... ?xn = t, but not reversed.*)
19355
3140daf6863d filter now accepts axioms as thm, instead of term.
mengj
parents: 19337
diff changeset
   175
fun defines thy (thm,(name,n)) gctypes =
3140daf6863d filter now accepts axioms as thm, instead of term.
mengj
parents: 19337
diff changeset
   176
    let val tm = prop_of thm
19448
72dab71cb11e definition expansion checks for excess variables
paulson
parents: 19355
diff changeset
   177
	fun defs lhs rhs =
72dab71cb11e definition expansion checks for excess variables
paulson
parents: 19355
diff changeset
   178
            let val (rator,args) = strip_comb lhs
19355
3140daf6863d filter now accepts axioms as thm, instead of term.
mengj
parents: 19337
diff changeset
   179
		val ct = const_with_typ thy (dest_ConstFree rator)
20153
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
   180
            in  forall is_Var args andalso uni_mem gctypes ct andalso
20197
ffc64d90fc1f use Term.add_vars instead of obsolete term_varnames;
wenzelm
parents: 20153
diff changeset
   181
                Term.add_vars rhs [] subset Term.add_vars lhs []
19448
72dab71cb11e definition expansion checks for excess variables
paulson
parents: 19355
diff changeset
   182
            end
72dab71cb11e definition expansion checks for excess variables
paulson
parents: 19355
diff changeset
   183
	    handle ConstFree => false
19355
3140daf6863d filter now accepts axioms as thm, instead of term.
mengj
parents: 19337
diff changeset
   184
    in    
19448
72dab71cb11e definition expansion checks for excess variables
paulson
parents: 19355
diff changeset
   185
	case tm of Const ("Trueprop",_) $ (Const("op =",_) $ lhs $ rhs) => 
72dab71cb11e definition expansion checks for excess variables
paulson
parents: 19355
diff changeset
   186
		   defs lhs rhs andalso
22130
0906fd95e0b5 Output.debug: non-strict;
wenzelm
parents: 21677
diff changeset
   187
		   (Output.debug (fn () => "Definition found: " ^ name ^ "_" ^ Int.toString n); true)
19355
3140daf6863d filter now accepts axioms as thm, instead of term.
mengj
parents: 19337
diff changeset
   188
		 | _ => false
20132
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
   189
    end;
19321
30b5bb35dd33 detection of definitions of relevant constants
paulson
parents: 19315
diff changeset
   190
21677
8ce2e9ef0bd2 Improved tracing
paulson
parents: 20854
diff changeset
   191
type annotd_cls = (thm * (string * int)) * ((string * const_typ list) list);
8ce2e9ef0bd2 Improved tracing
paulson
parents: 20854
diff changeset
   192
       
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   193
(*For a reverse sort, putting the largest values first.*)
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   194
fun compare_pairs ((_,w1),(_,w2)) = Real.compare (w2,w1);
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   195
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   196
(*Limit the number of new clauses, to prevent runaway acceptance.*)
21677
8ce2e9ef0bd2 Improved tracing
paulson
parents: 20854
diff changeset
   197
fun take_best (newpairs : (annotd_cls*real) list) =
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   198
  let val nnew = length newpairs
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   199
  in
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   200
    if nnew <= !max_new then (map #1 newpairs, [])
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   201
    else 
21677
8ce2e9ef0bd2 Improved tracing
paulson
parents: 20854
diff changeset
   202
      let val cls = sort compare_pairs newpairs
8ce2e9ef0bd2 Improved tracing
paulson
parents: 20854
diff changeset
   203
          val accepted = List.take (cls, !max_new)
22130
0906fd95e0b5 Output.debug: non-strict;
wenzelm
parents: 21677
diff changeset
   204
      in
0906fd95e0b5 Output.debug: non-strict;
wenzelm
parents: 21677
diff changeset
   205
        Output.debug (fn () => ("Number of candidates, " ^ Int.toString nnew ^ 
0906fd95e0b5 Output.debug: non-strict;
wenzelm
parents: 21677
diff changeset
   206
		       ", exceeds the limit of " ^ Int.toString (!max_new)));
0906fd95e0b5 Output.debug: non-strict;
wenzelm
parents: 21677
diff changeset
   207
        Output.debug (fn () => ("Effective pass mark: " ^ Real.toString (#2 (List.last accepted))));
0906fd95e0b5 Output.debug: non-strict;
wenzelm
parents: 21677
diff changeset
   208
        Output.debug (fn () => "Actually passed: " ^
0906fd95e0b5 Output.debug: non-strict;
wenzelm
parents: 21677
diff changeset
   209
          space_implode ", " (map (fn (((_,(name,_)),_),_) => name) accepted));
0906fd95e0b5 Output.debug: non-strict;
wenzelm
parents: 21677
diff changeset
   210
0906fd95e0b5 Output.debug: non-strict;
wenzelm
parents: 21677
diff changeset
   211
	(map #1 accepted, map #1 (List.drop (cls, !max_new)))
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   212
      end
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   213
  end;
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   214
19337
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   215
fun relevant_clauses thy ctab p rel_consts =
21677
8ce2e9ef0bd2 Improved tracing
paulson
parents: 20854
diff changeset
   216
  let fun relevant ([],_) [] = [] : (thm * (string * int)) list  (*Nothing added this iteration*)
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   217
	| relevant (newpairs,rejects) [] =
21677
8ce2e9ef0bd2 Improved tracing
paulson
parents: 20854
diff changeset
   218
	    let val (newrels,more_rejects) = take_best newpairs
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   219
		val new_consts = List.concat (map #2 newrels)
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   220
		val rel_consts' = foldl add_const_typ_table rel_consts new_consts
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   221
		val newp = p + (1.0-p) / !convergence
22130
0906fd95e0b5 Output.debug: non-strict;
wenzelm
parents: 21677
diff changeset
   222
	    in
0906fd95e0b5 Output.debug: non-strict;
wenzelm
parents: 21677
diff changeset
   223
              Output.debug (fn () => ("relevant this iteration: " ^ Int.toString (length newrels)));
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   224
	       (map #1 newrels) @ 
21677
8ce2e9ef0bd2 Improved tracing
paulson
parents: 20854
diff changeset
   225
	       (relevant_clauses thy ctab newp rel_consts' (more_rejects@rejects))
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   226
	    end
20153
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
   227
	| relevant (newrels,rejects) ((ax as (clsthm as (_,(name,n)),consts_typs)) :: axs) =
19337
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   228
	    let val weight = clause_weight ctab rel_consts consts_typs
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   229
	    in
19355
3140daf6863d filter now accepts axioms as thm, instead of term.
mengj
parents: 19337
diff changeset
   230
	      if p <= weight orelse (!follow_defs andalso defines thy clsthm rel_consts)
22194
3b1da1ff65df Improved debugging
paulson
parents: 22130
diff changeset
   231
	      then (Output.debug (fn () => (name ^ " clause " ^ Int.toString n ^ 
3b1da1ff65df Improved debugging
paulson
parents: 22130
diff changeset
   232
	                                    " passes: " ^ Real.toString weight));
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   233
	            relevant ((ax,weight)::newrels, rejects) axs)
19337
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   234
	      else relevant (newrels, ax::rejects) axs
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   235
	    end
22130
0906fd95e0b5 Output.debug: non-strict;
wenzelm
parents: 21677
diff changeset
   236
    in  Output.debug (fn () => ("relevant_clauses, current pass mark = " ^ Real.toString p));
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   237
        relevant ([],[]) 
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   238
    end;
19337
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   239
	
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   240
fun relevance_filter thy axioms goals = 
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   241
 if !run_relevance_filter andalso !pass_mark >= 0.1
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   242
 then
22130
0906fd95e0b5 Output.debug: non-strict;
wenzelm
parents: 21677
diff changeset
   243
  let val _ = Output.debug (fn () => "Start of relevance filtering");
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   244
      val const_tab = List.foldl (count_axiom_consts thy) Symtab.empty axioms
21677
8ce2e9ef0bd2 Improved tracing
paulson
parents: 20854
diff changeset
   245
      val goal_const_tab = get_goal_consts_typs thy goals
22130
0906fd95e0b5 Output.debug: non-strict;
wenzelm
parents: 21677
diff changeset
   246
      val _ = Output.debug (fn () => ("Initial constants: " ^
0906fd95e0b5 Output.debug: non-strict;
wenzelm
parents: 21677
diff changeset
   247
                                 space_implode ", " (Symtab.keys goal_const_tab)));
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   248
      val rels = relevant_clauses thy const_tab (!pass_mark) 
21677
8ce2e9ef0bd2 Improved tracing
paulson
parents: 20854
diff changeset
   249
                   goal_const_tab  (map (pair_consts_typs_axiom thy) axioms)
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   250
  in
22130
0906fd95e0b5 Output.debug: non-strict;
wenzelm
parents: 21677
diff changeset
   251
      Output.debug (fn () => ("Total relevant: " ^ Int.toString (length rels)));
19334
96ca738055a6 Simplified version of Jia's filter. Now all constants are pooled, rather than
paulson
parents: 19321
diff changeset
   252
      rels
20566
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   253
  end
499500b1e348 Added the max_new parameter, which is a cap on how many clauses may be admitted per round.
paulson
parents: 20527
diff changeset
   254
 else axioms;
18791
9b4ae9fa67a4 Relevance filtering. Has replaced the previous version.
mengj
parents:
diff changeset
   255
20197
ffc64d90fc1f use Term.add_vars instead of obsolete term_varnames;
wenzelm
parents: 20153
diff changeset
   256
end;