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