author | mengj |
Fri, 07 Apr 2006 05:14:06 +0200 | |
changeset 19355 | 3140daf6863d |
parent 19337 | 146b25b893bb |
child 19448 | 72dab71cb11e |
permissions | -rw-r--r-- |
19208 | 1 |
(* Authors: Jia Meng, NICTA and Lawrence C Paulson, Cambridge University Computer Laboratory |
2 |
ID: $Id$ |
|
3 |
Filtering strategies *) |
|
4 |
||
18791 | 5 |
structure ReduceAxiomsN = |
6 |
struct |
|
7 |
||
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
8 |
val pass_mark = ref 0.6; |
19337
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
9 |
val convergence = ref 2.4; (*Higher numbers allow longer inference chains*) |
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
10 |
val follow_defs = ref true; (*Follow definitions. Makes problems bigger.*) |
18791 | 11 |
|
19337
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
12 |
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
|
13 |
|
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
14 |
(*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
|
15 |
the constant frequencies.*) |
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
16 |
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
|
17 |
|
18791 | 18 |
|
19231 | 19 |
(*Including equality in this list might be expected to stop rules like subset_antisym from |
20 |
being chosen, but for some reason filtering works better with them listed.*) |
|
19208 | 21 |
val standard_consts = |
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
22 |
["Trueprop","==>","all","Ex","op &","op |","Not","All","op -->", |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
23 |
"op =","==","True","False"]; |
18791 | 24 |
|
25 |
||
19009 | 26 |
(*** constants with types ***) |
27 |
||
19231 | 28 |
(*An abstraction of Isabelle types*) |
19009 | 29 |
datatype const_typ = CTVar | CType of string * const_typ list |
30 |
||
19208 | 31 |
fun uni_type (CType(con1,args1)) (CType(con2,args2)) = con1=con2 andalso uni_types args1 args2 |
32 |
| uni_type (CType _) CTVar = true |
|
19009 | 33 |
| uni_type CTVar CTVar = true |
34 |
| uni_type CTVar _ = false |
|
19208 | 35 |
and uni_types [] [] = true |
36 |
| uni_types (a1::as1) (a2::as2) = uni_type a1 a2 andalso uni_types as1 as2; |
|
19009 | 37 |
|
38 |
||
19231 | 39 |
fun uni_constants (c1,ctp1) (c2,ctp2) = (c1=c2) andalso uni_types ctp1 ctp2; |
19009 | 40 |
|
41 |
fun uni_mem _ [] = false |
|
19208 | 42 |
| uni_mem (c,c_typ) ((c1,c_typ1)::ctyps) = |
43 |
uni_constants (c1,c_typ1) (c,c_typ) orelse uni_mem (c,c_typ) ctyps; |
|
19009 | 44 |
|
19231 | 45 |
fun const_typ_of (Type (c,typs)) = CType (c, map const_typ_of typs) |
46 |
| const_typ_of (TFree _) = CTVar |
|
47 |
| const_typ_of (TVar _) = CTVar |
|
19009 | 48 |
|
49 |
||
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
50 |
fun const_with_typ thy (c,typ) = |
19212 | 51 |
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
|
52 |
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
|
53 |
handle TYPE _ => (c,[]); (*Variable (locale constant): monomorphic*) |
19009 | 54 |
|
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
55 |
(*Free variables are counted, as well as constants, to handle locales*) |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
56 |
fun add_term_consts_typs_rm thy (Const(c, typ)) cs = |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
57 |
if (c mem standard_consts) then cs |
19335 | 58 |
else const_with_typ thy (c,typ) ins cs (*suppress multiples*) |
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
59 |
| add_term_consts_typs_rm thy (Free(c, typ)) cs = |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
60 |
const_with_typ thy (c,typ) ins cs |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
61 |
| add_term_consts_typs_rm thy (t $ u) cs = |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
62 |
add_term_consts_typs_rm thy t (add_term_consts_typs_rm thy u cs) |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
63 |
| add_term_consts_typs_rm thy (Abs(_,_,t)) cs = add_term_consts_typs_rm thy t cs |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
64 |
| add_term_consts_typs_rm thy _ cs = cs; |
19009 | 65 |
|
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
66 |
fun consts_typs_of_term thy t = add_term_consts_typs_rm thy t []; |
19009 | 67 |
|
19208 | 68 |
fun get_goal_consts_typs thy cs = foldl (op union) [] (map (consts_typs_of_term thy) cs) |
19009 | 69 |
|
19231 | 70 |
|
71 |
(**** Constant / Type Frequencies ****) |
|
72 |
||
73 |
local |
|
74 |
||
75 |
fun cons_nr CTVar = 0 |
|
76 |
| cons_nr (CType _) = 1; |
|
77 |
||
78 |
in |
|
79 |
||
80 |
fun const_typ_ord TU = |
|
81 |
case TU of |
|
82 |
(CType (a, Ts), CType (b, Us)) => |
|
83 |
(case fast_string_ord(a,b) of EQUAL => dict_ord const_typ_ord (Ts,Us) | ord => ord) |
|
84 |
| (T, U) => int_ord (cons_nr T, cons_nr U); |
|
19212 | 85 |
|
19231 | 86 |
end; |
87 |
||
88 |
structure CTtab = TableFun(type key = const_typ list val ord = dict_ord const_typ_ord); |
|
19212 | 89 |
|
19355 | 90 |
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
|
91 |
let fun count_const (a, T, tab) = |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
92 |
let val (c, cts) = const_with_typ thy (a,T) |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
93 |
val cttab = Option.getOpt (Symtab.lookup tab c, CTtab.empty) |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
94 |
val n = Option.getOpt (CTtab.lookup cttab cts, 0) |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
95 |
in |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
96 |
Symtab.update (c, CTtab.update (cts, n+1) cttab) tab |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
97 |
end |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
98 |
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
|
99 |
| count_term_consts (Free(a,T), tab) = count_const(a,T,tab) |
19231 | 100 |
| count_term_consts (t $ u, tab) = |
101 |
count_term_consts (t, count_term_consts (u, tab)) |
|
102 |
| count_term_consts (Abs(_,_,t), tab) = count_term_consts (t, tab) |
|
103 |
| count_term_consts (_, tab) = tab |
|
19355 | 104 |
in count_term_consts (prop_of thm, tab) end; |
19212 | 105 |
|
19009 | 106 |
|
107 |
(******** filter clauses ********) |
|
108 |
||
19231 | 109 |
fun const_weight ctab (c, cts) = |
110 |
let val pairs = CTtab.dest (Option.valOf (Symtab.lookup ctab c)) |
|
111 |
fun add ((cts',m), n) = if uni_types cts cts' then m+n else n |
|
112 |
in List.foldl add 0 pairs end; |
|
113 |
||
114 |
fun add_ct_weight ctab ((c,T), w) = |
|
115 |
w + !weight_fn (real (const_weight ctab (c,T))); |
|
19212 | 116 |
|
117 |
fun consts_typs_weight ctab = |
|
118 |
List.foldl (add_ct_weight ctab) 0.0; |
|
119 |
||
19231 | 120 |
(*Relevant constants are weighted according to frequency, |
121 |
but irrelevant constants are simply counted. Otherwise, Skolem functions, |
|
122 |
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
|
123 |
fun clause_weight ctab gctyps consts_typs = |
19208 | 124 |
let val rel = filter (fn s => uni_mem s gctyps) consts_typs |
19231 | 125 |
val rel_weight = consts_typs_weight ctab rel |
19009 | 126 |
in |
19231 | 127 |
rel_weight / (rel_weight + real (length consts_typs - length rel)) |
19009 | 128 |
end; |
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
129 |
|
19355 | 130 |
fun pair_consts_typs_axiom thy (thm,name) = |
131 |
((thm,name), (consts_typs_of_term thy (prop_of thm))); |
|
19009 | 132 |
|
19321 | 133 |
exception ConstFree; |
134 |
fun dest_ConstFree (Const aT) = aT |
|
135 |
| dest_ConstFree (Free aT) = aT |
|
136 |
| dest_ConstFree _ = raise ConstFree; |
|
137 |
||
138 |
(*Look for definitions of the form f ?x1 ... ?xn = t, but not reversed.*) |
|
19355 | 139 |
fun defines thy (thm,(name,n)) gctypes = |
140 |
let val tm = prop_of thm |
|
141 |
fun defs hs = |
|
142 |
let val (rator,args) = strip_comb hs |
|
143 |
val ct = const_with_typ thy (dest_ConstFree rator) |
|
144 |
in forall is_Var args andalso uni_mem ct gctypes end |
|
145 |
handle ConstFree => false |
|
146 |
in |
|
147 |
case tm of Const ("Trueprop",_) $ (Const("op =",_) $ lhs $ _) => |
|
148 |
defs lhs andalso |
|
149 |
(Output.debug ("Definition found: " ^ name ^ "_" ^ Int.toString n); true) |
|
150 |
| _ => false |
|
151 |
end |
|
19321 | 152 |
|
19337
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
153 |
fun relevant_clauses thy ctab p rel_consts = |
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
154 |
let fun relevant (newrels,rejects) [] = |
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
155 |
if null newrels then [] |
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
156 |
else |
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
157 |
let val new_consts = map #2 newrels |
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
158 |
val rel_consts' = foldl (op union) rel_consts new_consts |
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
159 |
val newp = p + (1.0-p) / !convergence |
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
160 |
in Output.debug ("found relevant: " ^ Int.toString (length newrels)); |
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
161 |
newrels @ relevant_clauses thy ctab newp rel_consts' rejects |
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
162 |
end |
19355 | 163 |
| relevant (newrels,rejects) ((ax as (clsthm,consts_typs)) :: axs) = |
19337
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
164 |
let val weight = clause_weight ctab rel_consts consts_typs |
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
165 |
in |
19355 | 166 |
if p <= weight orelse (!follow_defs andalso defines thy clsthm rel_consts) |
19337
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
167 |
then relevant (ax::newrels, rejects) axs |
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
168 |
else relevant (newrels, ax::rejects) axs |
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
169 |
end |
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
170 |
in Output.debug ("relevant_clauses: " ^ Real.toString p); |
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
171 |
relevant ([],[]) end; |
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
172 |
|
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
173 |
|
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
174 |
fun relevance_filter_aux thy axioms goals = |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
175 |
let val const_tab = List.foldl (count_axiom_consts thy) Symtab.empty axioms |
19231 | 176 |
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
|
177 |
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
|
178 |
(map (pair_consts_typs_axiom thy) axioms) |
19231 | 179 |
in |
19334
96ca738055a6
Simplified version of Jia's filter. Now all constants are pooled, rather than
paulson
parents:
19321
diff
changeset
|
180 |
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
|
181 |
rels |
19231 | 182 |
end; |
19009 | 183 |
|
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
184 |
fun relevance_filter thy axioms goals = |
19337
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
185 |
if !pass_mark < 0.1 then axioms |
146b25b893bb
pool of constants; definition expansion; current best settings
paulson
parents:
19335
diff
changeset
|
186 |
else map #1 (relevance_filter_aux thy axioms goals); |
19009 | 187 |
|
18791 | 188 |
|
189 |
end; |