src/HOL/Tools/ATP/reduce_axiomsN.ML
author paulson
Wed, 13 Sep 2006 12:21:35 +0200
changeset 20527 958ec4833d87
parent 20457 85414caac94a
child 20566 499500b1e348
permissions -rw-r--r--
Added the "theory_const" option. Only it is OFF because it's often harmful!
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;
20527
958ec4833d87 Added the "theory_const" option. Only it is OFF because it's often harmful!
paulson
parents: 20457
diff changeset
    15
val theory_const = ref false;
19315
b218cc3d1bb4 Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents: 19231
diff changeset
    16
val pass_mark = ref 0.6;
19337
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
    17
val convergence = ref 2.4;   (*Higher numbers allow longer inference chains*)
20423
593053389701 minor bug fixes
paulson
parents: 20197
diff changeset
    18
val follow_defs = ref false;  (*Follow definitions. Makes problems bigger.*)
18791
9b4ae9fa67a4 Relevance filtering. Has replaced the previous version.
mengj
parents:
diff changeset
    19
19337
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
    20
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
    21
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
    22
(*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
    23
  the constant frequencies.*)
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
    24
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
    25
18791
9b4ae9fa67a4 Relevance filtering. Has replaced the previous version.
mengj
parents:
diff changeset
    26
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
    27
(*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
    28
  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
    29
  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
    30
  must be within comprehensions.*)
958ec4833d87 Added the "theory_const" option. Only it is OFF because it's often harmful!
paulson
parents: 20457
diff changeset
    31
val standard_consts = ["Trueprop","==>","all","==","op |","Not","op ="];
18791
9b4ae9fa67a4 Relevance filtering. Has replaced the previous version.
mengj
parents:
diff changeset
    32
9b4ae9fa67a4 Relevance filtering. Has replaced the previous version.
mengj
parents:
diff changeset
    33
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
    34
(*** constants with types ***)
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
    35
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
    36
(*An abstraction of Isabelle types*)
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
    37
datatype const_typ =  CTVar | CType of string * const_typ list
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
    38
20153
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
    39
(*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
    40
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
    41
      con1=con2 andalso match_types args1 args2
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
    42
  | match_type CTVar (CType _) = true
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
    43
  | match_type CTVar CTVar = true
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
  
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
    54
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
    55
  | const_typ_of (TFree _) = CTVar
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
    56
  | const_typ_of (TVar _) = CTVar
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
    57
19315
b218cc3d1bb4 Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents: 19231
diff changeset
    58
fun const_with_typ thy (c,typ) = 
19212
ec53c138277a Frequency strategy. Revised indentation, etc.
paulson
parents: 19208
diff changeset
    59
    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
    60
    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
    61
    handle TYPE _ => (c,[]);   (*Variable (locale constant): monomorphic*)   
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
    62
20153
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
    63
(*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
    64
  which we ignore.*)
20132
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    65
fun add_const_typ_table ((c,ctyps), tab) =
20153
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
    66
  Symtab.map_default (c, [ctyps]) (fn [] => [] | ctyps_list => ctyps ins ctyps_list) 
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
    67
    tab;
20132
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    68
19315
b218cc3d1bb4 Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents: 19231
diff changeset
    69
(*Free variables are counted, as well as constants, to handle locales*)
20132
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    70
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
    71
      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
    72
  | add_term_consts_typs_rm thy (Free(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 (t $ u, tab) =
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    75
      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
    76
  | 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
    77
  | add_term_consts_typs_rm thy (_, tab) = tab;
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
    78
20132
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    79
(*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
    80
fun add_standard_const (s,tab) = Symtab.update (s,[]) tab;
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
    81
20132
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    82
val null_const_tab : const_typ list list Symtab.table = 
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    83
    foldl add_standard_const Symtab.empty standard_consts;
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
    84
20457
85414caac94a refinements to conversion into clause form, esp for the HO case
paulson
parents: 20423
diff changeset
    85
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
    86
20527
958ec4833d87 Added the "theory_const" option. Only it is OFF because it's often harmful!
paulson
parents: 20457
diff changeset
    87
(*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
    88
  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
    89
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
    90
 if !theory_const then
958ec4833d87 Added the "theory_const" option. Only it is OFF because it's often harmful!
paulson
parents: 20457
diff changeset
    91
  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
    92
      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
    93
  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
    94
 else prop_of th;
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
    95
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
    96
(**** Constant / Type Frequencies ****)
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
local
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
    99
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   100
fun cons_nr CTVar = 0
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   101
  | cons_nr (CType _) = 1;
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   102
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   103
in
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   104
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   105
fun const_typ_ord TU =
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   106
  case TU of
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   107
    (CType (a, Ts), CType (b, Us)) =>
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   108
      (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
   109
  | (T, U) => int_ord (cons_nr T, cons_nr U);
19212
ec53c138277a Frequency strategy. Revised indentation, etc.
paulson
parents: 19208
diff changeset
   110
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   111
end;
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   112
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   113
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
   114
19355
3140daf6863d filter now accepts axioms as thm, instead of term.
mengj
parents: 19337
diff changeset
   115
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
   116
  let fun count_const (a, T, tab) =
b218cc3d1bb4 Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents: 19231
diff changeset
   117
	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
   118
	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
   119
	    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
   120
	                       (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
   121
	end
b218cc3d1bb4 Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents: 19231
diff changeset
   122
      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
   123
	| 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
   124
	| count_term_consts (t $ u, tab) =
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   125
	    count_term_consts (t, count_term_consts (u, tab))
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   126
	| count_term_consts (Abs(_,_,t), tab) = count_term_consts (t, tab)
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   127
	| 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
   128
  in  count_term_consts (const_prop_of thm, tab)  end;
19212
ec53c138277a Frequency strategy. Revised indentation, etc.
paulson
parents: 19208
diff changeset
   129
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
   130
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
   131
(******** filter clauses ********)
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
   132
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   133
fun const_weight ctab (c, cts) =
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   134
  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
   135
      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
   136
  in  List.foldl add 0 pairs  end;
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   137
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   138
fun add_ct_weight ctab ((c,T), w) =
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   139
  w + !weight_fn (real (const_weight ctab (c,T)));
19212
ec53c138277a Frequency strategy. Revised indentation, etc.
paulson
parents: 19208
diff changeset
   140
ec53c138277a Frequency strategy. Revised indentation, etc.
paulson
parents: 19208
diff changeset
   141
fun consts_typs_weight ctab =
ec53c138277a Frequency strategy. Revised indentation, etc.
paulson
parents: 19208
diff changeset
   142
    List.foldl (add_ct_weight ctab) 0.0;
ec53c138277a Frequency strategy. Revised indentation, etc.
paulson
parents: 19208
diff changeset
   143
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   144
(*Relevant constants are weighted according to frequency, 
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   145
  but irrelevant constants are simply counted. Otherwise, Skolem functions,
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   146
  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
   147
fun clause_weight ctab gctyps consts_typs =
20132
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
   148
    let val rel = filter (uni_mem gctyps) consts_typs
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   149
        val rel_weight = consts_typs_weight ctab rel
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
   150
    in
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   151
	rel_weight / (rel_weight + real (length consts_typs - length rel))
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
   152
    end;
19315
b218cc3d1bb4 Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents: 19231
diff changeset
   153
    
20153
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
   154
(*Multiplies out to a list of pairs: 'a * 'b list -> ('a * 'b) list -> ('a * 'b) list*)
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
   155
fun add_expand_pairs (c, ctyps_list) cpairs =
20132
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
   156
      foldl (fn (ctyps,cpairs) => (c,ctyps)::cpairs) cpairs ctyps_list;
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
   157
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
   158
fun consts_typs_of_term thy t = 
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
   159
  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
   160
  in  Symtab.fold add_expand_pairs tab []  end;
20132
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
   161
19355
3140daf6863d filter now accepts axioms as thm, instead of term.
mengj
parents: 19337
diff changeset
   162
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
   163
    ((thm,name), (consts_typs_of_term thy (const_prop_of thm)));
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
   164
19321
30b5bb35dd33 detection of definitions of relevant constants
paulson
parents: 19315
diff changeset
   165
exception ConstFree;
30b5bb35dd33 detection of definitions of relevant constants
paulson
parents: 19315
diff changeset
   166
fun dest_ConstFree (Const aT) = aT
30b5bb35dd33 detection of definitions of relevant constants
paulson
parents: 19315
diff changeset
   167
  | dest_ConstFree (Free aT) = aT
30b5bb35dd33 detection of definitions of relevant constants
paulson
parents: 19315
diff changeset
   168
  | dest_ConstFree _ = raise ConstFree;
30b5bb35dd33 detection of definitions of relevant constants
paulson
parents: 19315
diff changeset
   169
30b5bb35dd33 detection of definitions of relevant constants
paulson
parents: 19315
diff changeset
   170
(*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
   171
fun defines thy (thm,(name,n)) gctypes =
3140daf6863d filter now accepts axioms as thm, instead of term.
mengj
parents: 19337
diff changeset
   172
    let val tm = prop_of thm
19448
72dab71cb11e definition expansion checks for excess variables
paulson
parents: 19355
diff changeset
   173
	fun defs lhs rhs =
72dab71cb11e definition expansion checks for excess variables
paulson
parents: 19355
diff changeset
   174
            let val (rator,args) = strip_comb lhs
19355
3140daf6863d filter now accepts axioms as thm, instead of term.
mengj
parents: 19337
diff changeset
   175
		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
   176
            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
   177
                Term.add_vars rhs [] subset Term.add_vars lhs []
19448
72dab71cb11e definition expansion checks for excess variables
paulson
parents: 19355
diff changeset
   178
            end
72dab71cb11e definition expansion checks for excess variables
paulson
parents: 19355
diff changeset
   179
	    handle ConstFree => false
19355
3140daf6863d filter now accepts axioms as thm, instead of term.
mengj
parents: 19337
diff changeset
   180
    in    
19448
72dab71cb11e definition expansion checks for excess variables
paulson
parents: 19355
diff changeset
   181
	case tm of Const ("Trueprop",_) $ (Const("op =",_) $ lhs $ rhs) => 
72dab71cb11e definition expansion checks for excess variables
paulson
parents: 19355
diff changeset
   182
		   defs lhs rhs andalso
19355
3140daf6863d filter now accepts axioms as thm, instead of term.
mengj
parents: 19337
diff changeset
   183
		   (Output.debug ("Definition found: " ^ name ^ "_" ^ Int.toString n); true)
3140daf6863d filter now accepts axioms as thm, instead of term.
mengj
parents: 19337
diff changeset
   184
		 | _ => false
20132
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
   185
    end;
19321
30b5bb35dd33 detection of definitions of relevant constants
paulson
parents: 19315
diff changeset
   186
19337
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   187
fun relevant_clauses thy ctab p rel_consts =
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   188
  let fun relevant (newrels,rejects) []  =
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   189
	    if null newrels then [] 
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   190
	    else 
20132
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
   191
	      let val new_consts = List.concat (map #2 newrels)
de3c295000b2 Replaced a-lists by tables to improve efficiency
paulson
parents: 19448
diff changeset
   192
	          val rel_consts' = foldl add_const_typ_table rel_consts new_consts
19337
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   193
                  val newp = p + (1.0-p) / !convergence
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   194
	      in Output.debug ("found relevant: " ^ Int.toString (length newrels));
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   195
                 newrels @ relevant_clauses thy ctab newp rel_consts' rejects
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   196
	      end
20153
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
   197
	| 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
   198
	    let val weight = clause_weight ctab rel_consts consts_typs
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   199
	    in
19355
3140daf6863d filter now accepts axioms as thm, instead of term.
mengj
parents: 19337
diff changeset
   200
	      if p <= weight orelse (!follow_defs andalso defines thy clsthm rel_consts)
20153
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
   201
	      then (Output.debug name; Output.debug "\n";
6ff5d35749b0 Fixed the bugs introduced by the last commit! Output is now *identical* to that
paulson
parents: 20132
diff changeset
   202
	            relevant (ax::newrels, rejects) axs)
19337
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   203
	      else relevant (newrels, ax::rejects) axs
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   204
	    end
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   205
    in  Output.debug ("relevant_clauses: " ^ Real.toString p);
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   206
        relevant ([],[]) end;
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   207
	
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   208
     
19315
b218cc3d1bb4 Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents: 19231
diff changeset
   209
fun relevance_filter_aux thy axioms goals = 
b218cc3d1bb4 Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents: 19231
diff changeset
   210
  let val const_tab = List.foldl (count_axiom_consts thy) Symtab.empty axioms
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   211
      val goals_consts_typs = get_goal_consts_typs thy goals
19337
146b25b893bb pool of constants; definition expansion; current best settings
paulson
parents: 19335
diff changeset
   212
      val rels = relevant_clauses thy const_tab (!pass_mark) goals_consts_typs 
19334
96ca738055a6 Simplified version of Jia's filter. Now all constants are pooled, rather than
paulson
parents: 19321
diff changeset
   213
                   (map (pair_consts_typs_axiom thy) axioms)
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   214
  in
19334
96ca738055a6 Simplified version of Jia's filter. Now all constants are pooled, rather than
paulson
parents: 19321
diff changeset
   215
      Output.debug ("Total relevant: " ^ Int.toString (length rels));
96ca738055a6 Simplified version of Jia's filter. Now all constants are pooled, rather than
paulson
parents: 19321
diff changeset
   216
      rels
19231
c8879dd3a953 Frequency analysis of constants (with types).
paulson
parents: 19212
diff changeset
   217
  end;
19009
bb29bf9d3a72 Added another filter strategy.
mengj
parents: 18791
diff changeset
   218
19315
b218cc3d1bb4 Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents: 19231
diff changeset
   219
fun relevance_filter thy axioms goals =
20457
85414caac94a refinements to conversion into clause form, esp for the HO case
paulson
parents: 20423
diff changeset
   220
  if !run_relevance_filter andalso !pass_mark >= 0.1
85414caac94a refinements to conversion into clause form, esp for the HO case
paulson
parents: 20423
diff changeset
   221
  then map #1 (relevance_filter_aux thy axioms goals)
85414caac94a refinements to conversion into clause form, esp for the HO case
paulson
parents: 20423
diff changeset
   222
  else axioms
18791
9b4ae9fa67a4 Relevance filtering. Has replaced the previous version.
mengj
parents:
diff changeset
   223
20197
ffc64d90fc1f use Term.add_vars instead of obsolete term_varnames;
wenzelm
parents: 20153
diff changeset
   224
end;