author | paulson |
Thu, 23 Mar 2006 10:05:03 +0100 | |
changeset 19321 | 30b5bb35dd33 |
parent 19315 | b218cc3d1bb4 |
child 19334 | 96ca738055a6 |
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; |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
9 |
val reduction_factor = ref 1.0; |
18791 | 10 |
|
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
11 |
(*Whether all "simple" unit clauses should be included*) |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
12 |
val add_unit = ref false; |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
13 |
val unit_pass_mark = ref 0.0; |
18791 | 14 |
|
15 |
||
19231 | 16 |
(*Including equality in this list might be expected to stop rules like subset_antisym from |
17 |
being chosen, but for some reason filtering works better with them listed.*) |
|
19208 | 18 |
val standard_consts = |
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
19 |
["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
|
20 |
"op =","==","True","False"]; |
18791 | 21 |
|
22 |
||
19009 | 23 |
(*** unit clauses ***) |
19231 | 24 |
datatype clause_kind = Unit_neq | Unit_geq | Other |
19009 | 25 |
|
26 |
||
27 |
fun literals_of_term args (Const ("Trueprop",_) $ P) = literals_of_term args P |
|
28 |
| literals_of_term args (Const ("op |",_) $ P $ Q) = |
|
29 |
literals_of_term (literals_of_term args P) Q |
|
19208 | 30 |
| literals_of_term args P = P::args; |
19009 | 31 |
|
19208 | 32 |
fun is_ground t = (term_vars t = []) andalso (term_frees t = []); |
19009 | 33 |
|
34 |
fun eq_clause_type (P,Q) = |
|
35 |
if ((is_ground P) orelse (is_ground Q)) then Unit_geq else Other; |
|
36 |
||
37 |
fun unit_clause_type (Const ("op =",_) $ P $ Q) = eq_clause_type (P,Q) |
|
38 |
| unit_clause_type _ = Unit_neq; |
|
39 |
||
19231 | 40 |
fun clause_kind tm = |
19208 | 41 |
case literals_of_term [] tm of |
42 |
[lit] => unit_clause_type lit |
|
43 |
| _ => Other; |
|
19009 | 44 |
|
45 |
(*** constants with types ***) |
|
46 |
||
19231 | 47 |
(*An abstraction of Isabelle types*) |
19009 | 48 |
datatype const_typ = CTVar | CType of string * const_typ list |
49 |
||
19208 | 50 |
fun uni_type (CType(con1,args1)) (CType(con2,args2)) = con1=con2 andalso uni_types args1 args2 |
51 |
| uni_type (CType _) CTVar = true |
|
19009 | 52 |
| uni_type CTVar CTVar = true |
53 |
| uni_type CTVar _ = false |
|
19208 | 54 |
and uni_types [] [] = true |
55 |
| uni_types (a1::as1) (a2::as2) = uni_type a1 a2 andalso uni_types as1 as2; |
|
19009 | 56 |
|
57 |
||
19231 | 58 |
fun uni_constants (c1,ctp1) (c2,ctp2) = (c1=c2) andalso uni_types ctp1 ctp2; |
19009 | 59 |
|
60 |
fun uni_mem _ [] = false |
|
19208 | 61 |
| uni_mem (c,c_typ) ((c1,c_typ1)::ctyps) = |
62 |
uni_constants (c1,c_typ1) (c,c_typ) orelse uni_mem (c,c_typ) ctyps; |
|
19009 | 63 |
|
19231 | 64 |
fun const_typ_of (Type (c,typs)) = CType (c, map const_typ_of typs) |
65 |
| const_typ_of (TFree _) = CTVar |
|
66 |
| const_typ_of (TVar _) = CTVar |
|
19009 | 67 |
|
68 |
||
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
69 |
fun const_with_typ thy (c,typ) = |
19212 | 70 |
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
|
71 |
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
|
72 |
handle TYPE _ => (c,[]); (*Variable (locale constant): monomorphic*) |
19009 | 73 |
|
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
74 |
(*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
|
75 |
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
|
76 |
if (c mem standard_consts) then cs |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
77 |
else 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
|
78 |
| 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
|
79 |
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
|
80 |
| 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
|
81 |
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
|
82 |
| 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
|
83 |
| add_term_consts_typs_rm thy _ cs = cs; |
19009 | 84 |
|
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
85 |
fun consts_typs_of_term thy t = add_term_consts_typs_rm thy t []; |
19009 | 86 |
|
19208 | 87 |
fun get_goal_consts_typs thy cs = foldl (op union) [] (map (consts_typs_of_term thy) cs) |
19009 | 88 |
|
19231 | 89 |
|
90 |
(**** Constant / Type Frequencies ****) |
|
91 |
||
19321 | 92 |
|
19231 | 93 |
local |
94 |
||
95 |
fun cons_nr CTVar = 0 |
|
96 |
| cons_nr (CType _) = 1; |
|
97 |
||
98 |
in |
|
99 |
||
100 |
fun const_typ_ord TU = |
|
101 |
case TU of |
|
102 |
(CType (a, Ts), CType (b, Us)) => |
|
103 |
(case fast_string_ord(a,b) of EQUAL => dict_ord const_typ_ord (Ts,Us) | ord => ord) |
|
104 |
| (T, U) => int_ord (cons_nr T, cons_nr U); |
|
19212 | 105 |
|
19231 | 106 |
end; |
107 |
||
108 |
structure CTtab = TableFun(type key = const_typ list val ord = dict_ord const_typ_ord); |
|
19212 | 109 |
|
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
110 |
fun count_axiom_consts thy ((t,_), tab) = |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
111 |
let fun count_const (a, T, tab) = |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
112 |
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
|
113 |
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
|
114 |
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
|
115 |
in |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
116 |
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
|
117 |
end |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
118 |
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
|
119 |
| count_term_consts (Free(a,T), tab) = count_const(a,T,tab) |
19231 | 120 |
| count_term_consts (t $ u, tab) = |
121 |
count_term_consts (t, count_term_consts (u, tab)) |
|
122 |
| count_term_consts (Abs(_,_,t), tab) = count_term_consts (t, tab) |
|
123 |
| count_term_consts (_, tab) = tab |
|
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
124 |
in count_term_consts (t, tab) end; |
19212 | 125 |
|
19009 | 126 |
|
127 |
(******** filter clauses ********) |
|
128 |
||
19212 | 129 |
(*The default ignores the constant-count and gives the old Strategy 3*) |
130 |
val weight_fn = ref (fn x : real => 1.0); |
|
131 |
||
19231 | 132 |
fun const_weight ctab (c, cts) = |
133 |
let val pairs = CTtab.dest (Option.valOf (Symtab.lookup ctab c)) |
|
134 |
fun add ((cts',m), n) = if uni_types cts cts' then m+n else n |
|
135 |
in List.foldl add 0 pairs end; |
|
136 |
||
137 |
fun add_ct_weight ctab ((c,T), w) = |
|
138 |
w + !weight_fn (real (const_weight ctab (c,T))); |
|
19212 | 139 |
|
140 |
fun consts_typs_weight ctab = |
|
141 |
List.foldl (add_ct_weight ctab) 0.0; |
|
142 |
||
19231 | 143 |
(*Relevant constants are weighted according to frequency, |
144 |
but irrelevant constants are simply counted. Otherwise, Skolem functions, |
|
145 |
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
|
146 |
fun clause_weight ctab gctyps consts_typs = |
19208 | 147 |
let val rel = filter (fn s => uni_mem s gctyps) consts_typs |
19231 | 148 |
val rel_weight = consts_typs_weight ctab rel |
19009 | 149 |
in |
19231 | 150 |
rel_weight / (rel_weight + real (length consts_typs - length rel)) |
19009 | 151 |
end; |
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
152 |
|
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
153 |
fun relevant_clauses ctab rel_axs [] (addc,tmpc) keep = |
19208 | 154 |
if null addc orelse null tmpc |
155 |
then (addc @ rel_axs @ keep, tmpc) (*termination!*) |
|
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
156 |
else relevant_clauses ctab addc tmpc ([],[]) (rel_axs @ keep) |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
157 |
| relevant_clauses ctab rel_axs ((clstm,(consts_typs,w))::e_axs) (addc,tmpc) keep = |
19231 | 158 |
let fun clause_weight_ax (_,(refconsts_typs,wa)) = |
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
159 |
wa * clause_weight ctab refconsts_typs consts_typs; |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
160 |
val weight' = List.foldl Real.max w (map clause_weight_ax rel_axs) |
19212 | 161 |
val e_ax' = (clstm, (consts_typs,weight')) |
19208 | 162 |
in |
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
163 |
if !pass_mark <= weight' |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
164 |
then relevant_clauses ctab rel_axs e_axs (e_ax'::addc, tmpc) keep |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
165 |
else relevant_clauses ctab rel_axs e_axs (addc, e_ax'::tmpc) keep |
19208 | 166 |
end; |
19009 | 167 |
|
19231 | 168 |
fun pair_consts_typs_axiom thy (tm,name) = |
19212 | 169 |
((tm,name), (consts_typs_of_term thy tm)); |
19009 | 170 |
|
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
171 |
(*Unit clauses other than non-trivial equations can be included subject to |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
172 |
a separate (presumably lower) mark. *) |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
173 |
fun good_unit_clause ((t,_), (_,w)) = |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
174 |
!unit_pass_mark <= w andalso |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
175 |
(case clause_kind t of |
19212 | 176 |
Unit_neq => true |
177 |
| Unit_geq => true |
|
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
178 |
| Other => false); |
19231 | 179 |
|
180 |
fun axiom_ord ((_,(_,w1)), (_,(_,w2))) = Real.compare (w2,w1); |
|
19009 | 181 |
|
19231 | 182 |
fun showconst (c,cttab) = |
183 |
List.app (fn n => Output.debug (Int.toString n ^ " occurrences of " ^ c)) |
|
184 |
(map #2 (CTtab.dest cttab)) |
|
185 |
||
186 |
fun show_cname (name,k) = name ^ "__" ^ Int.toString k; |
|
187 |
||
188 |
fun showax ((_,cname), (_,w)) = |
|
189 |
Output.debug ("Axiom " ^ show_cname cname ^ " has weight " ^ Real.toString w) |
|
190 |
||
19321 | 191 |
exception ConstFree; |
192 |
fun dest_ConstFree (Const aT) = aT |
|
193 |
| dest_ConstFree (Free aT) = aT |
|
194 |
| dest_ConstFree _ = raise ConstFree; |
|
195 |
||
196 |
(*Look for definitions of the form f ?x1 ... ?xn = t, but not reversed.*) |
|
197 |
fun defines thy (tm,(name,n)) gctypes = |
|
198 |
let fun defs hs = |
|
199 |
let val (rator,args) = strip_comb hs |
|
200 |
val ct = const_with_typ thy (dest_ConstFree rator) |
|
201 |
in forall is_Var args andalso uni_mem ct gctypes end |
|
202 |
handle ConstFree => false |
|
203 |
in |
|
204 |
case tm of Const ("Trueprop",_) $ (Const("op =",_) $ lhs $ _) => |
|
205 |
defs lhs andalso |
|
206 |
(Output.debug ("Definition found: " ^ name ^ "_" ^ Int.toString n); true) |
|
207 |
| _ => false |
|
208 |
end |
|
209 |
||
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
210 |
fun relevance_filter_aux thy axioms goals = |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
211 |
let val const_tab = List.foldl (count_axiom_consts thy) Symtab.empty axioms |
19231 | 212 |
val goals_consts_typs = get_goal_consts_typs thy goals |
19321 | 213 |
fun relevant [] (rels,nonrels) = (rels,nonrels) |
214 |
| relevant ((clstm,consts_typs)::axs) (rels,nonrels) = |
|
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
215 |
let val weight = clause_weight const_tab goals_consts_typs consts_typs |
19231 | 216 |
val ccc = (clstm, (consts_typs,weight)) |
217 |
in |
|
19321 | 218 |
if !pass_mark <= weight orelse defines thy clstm goals_consts_typs |
219 |
then relevant axs (ccc::rels, nonrels) |
|
220 |
else relevant axs (rels, ccc::nonrels) |
|
19231 | 221 |
end |
222 |
val (rel_clauses,nrel_clauses) = |
|
223 |
relevant (map (pair_consts_typs_axiom thy) axioms) ([],[]) |
|
19321 | 224 |
val (rels,nonrels) = relevant_clauses const_tab rel_clauses nrel_clauses ([],[]) [] |
225 |
val max_filtered = floor (!reduction_factor * real (length rels)) |
|
226 |
val rels' = Library.take(max_filtered, Library.sort axiom_ord rels) |
|
19231 | 227 |
in |
228 |
if !Output.show_debug_msgs then |
|
229 |
(List.app showconst (Symtab.dest const_tab); |
|
19321 | 230 |
List.app showax rels) |
19231 | 231 |
else (); |
19321 | 232 |
if !add_unit then (filter good_unit_clause nonrels) @ rels' |
233 |
else rels' |
|
19231 | 234 |
end; |
19009 | 235 |
|
19315
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
236 |
fun relevance_filter thy axioms goals = |
b218cc3d1bb4
Removal of obsolete strategies. Initial support for locales: Frees and Consts
paulson
parents:
19231
diff
changeset
|
237 |
map #1 (relevance_filter_aux thy axioms goals); |
19009 | 238 |
|
18791 | 239 |
|
240 |
end; |