author | haftmann |
Sat, 15 Sep 2007 19:27:35 +0200 | |
changeset 24584 | 01e83ffa6c54 |
parent 24271 | 499608101177 |
child 25218 | fcf0f50e478c |
permissions | -rw-r--r-- |
24584 | 1 |
(* Title: Tools/Compute_Oracle/linker.ML |
24271 | 2 |
ID: $Id$ |
23663 | 3 |
Author: Steven Obua |
4 |
||
24271 | 5 |
Linker.ML solves the problem that the computing oracle does not |
6 |
instantiate polymorphic rules. By going through the PCompute interface, |
|
7 |
all possible instantiations are resolved by compiling new programs, if |
|
8 |
necessary. The obvious disadvantage of this approach is that in the |
|
9 |
worst case for each new term to be rewritten, a new program may be |
|
10 |
compiled. |
|
23663 | 11 |
*) |
12 |
||
24271 | 13 |
(* |
23663 | 14 |
Given constants/frees c_1::t_1, c_2::t_2, ...., c_n::t_n, |
15 |
and constants/frees d_1::d_1, d_2::s_2, ..., d_m::s_m |
|
16 |
||
17 |
Find all substitutions S such that |
|
24271 | 18 |
a) the domain of S is tvars (t_1, ..., t_n) |
23663 | 19 |
b) there are indices i_1, ..., i_k, and j_1, ..., j_k with |
20 |
1. S (c_i_1::t_i_1) = d_j_1::s_j_1, ..., S (c_i_k::t_i_k) = d_j_k::s_j_k |
|
21 |
2. tvars (t_i_1, ..., t_i_k) = tvars (t_1, ..., t_n) |
|
22 |
*) |
|
24271 | 23 |
signature LINKER = |
23663 | 24 |
sig |
25 |
exception Link of string |
|
24271 | 26 |
|
23663 | 27 |
datatype constant = Constant of bool * string * typ |
28 |
val constant_of : term -> constant |
|
29 |
||
30 |
type instances |
|
31 |
type subst = Type.tyenv |
|
24271 | 32 |
|
23663 | 33 |
val empty : constant list -> instances |
34 |
val typ_of_constant : constant -> typ |
|
24271 | 35 |
val add_instances : theory -> instances -> constant list -> subst list * instances |
23663 | 36 |
val substs_of : instances -> subst list |
37 |
val is_polymorphic : constant -> bool |
|
38 |
val distinct_constants : constant list -> constant list |
|
39 |
val collect_consts : term list -> constant list |
|
40 |
end |
|
41 |
||
42 |
structure Linker : LINKER = struct |
|
43 |
||
44 |
exception Link of string; |
|
45 |
||
46 |
type subst = Type.tyenv |
|
47 |
||
48 |
datatype constant = Constant of bool * string * typ |
|
49 |
fun constant_of (Const (name, ty)) = Constant (false, name, ty) |
|
50 |
| constant_of (Free (name, ty)) = Constant (true, name, ty) |
|
51 |
| constant_of _ = raise Link "constant_of" |
|
52 |
||
53 |
fun bool_ord (x,y) = if x then (if y then EQUAL else GREATER) else (if y then LESS else EQUAL) |
|
54 |
fun constant_ord (Constant (x1,x2,x3), Constant (y1,y2,y3)) = (prod_ord (prod_ord bool_ord fast_string_ord) Term.typ_ord) (((x1,x2),x3), ((y1,y2),y3)) |
|
55 |
fun constant_modty_ord (Constant (x1,x2,_), Constant (y1,y2,_)) = (prod_ord bool_ord fast_string_ord) ((x1,x2), (y1,y2)) |
|
56 |
||
57 |
||
58 |
structure Consttab = TableFun(type key = constant val ord = constant_ord); |
|
59 |
structure ConsttabModTy = TableFun(type key = constant val ord = constant_modty_ord); |
|
60 |
||
61 |
fun typ_of_constant (Constant (_, _, ty)) = ty |
|
62 |
||
63 |
val empty_subst = (Vartab.empty : Type.tyenv) |
|
64 |
||
24271 | 65 |
fun merge_subst (A:Type.tyenv) (B:Type.tyenv) = |
66 |
SOME (Vartab.fold (fn (v, t) => |
|
67 |
fn tab => |
|
68 |
(case Vartab.lookup tab v of |
|
69 |
NONE => Vartab.update (v, t) tab |
|
70 |
| SOME t' => if t = t' then tab else raise Type.TYPE_MATCH)) A B) |
|
23663 | 71 |
handle Type.TYPE_MATCH => NONE |
72 |
||
24271 | 73 |
fun subst_ord (A:Type.tyenv, B:Type.tyenv) = |
23663 | 74 |
(list_ord (prod_ord Term.fast_indexname_ord (prod_ord Term.sort_ord Term.typ_ord))) (Vartab.dest A, Vartab.dest B) |
75 |
||
76 |
structure Substtab = TableFun(type key = Type.tyenv val ord = subst_ord); |
|
77 |
||
23768 | 78 |
fun substtab_union c = Substtab.fold Substtab.update c |
23663 | 79 |
fun substtab_unions [] = Substtab.empty |
80 |
| substtab_unions [c] = c |
|
81 |
| substtab_unions (c::cs) = substtab_union c (substtab_unions cs) |
|
82 |
||
83 |
datatype instances = Instances of unit ConsttabModTy.table * Type.tyenv Consttab.table Consttab.table * constant list list * unit Substtab.table |
|
84 |
||
24271 | 85 |
fun is_polymorphic (Constant (_, _, ty)) = not (null (typ_tvars ty)) |
23663 | 86 |
|
87 |
fun distinct_constants cs = |
|
88 |
Consttab.keys (fold (fn c => Consttab.update (c, ())) cs Consttab.empty) |
|
89 |
||
24271 | 90 |
fun empty cs = |
91 |
let |
|
92 |
val cs = distinct_constants (filter is_polymorphic cs) |
|
93 |
val old_cs = cs |
|
94 |
(* fun collect_tvars ty tab = fold (fn v => fn tab => Typtab.update (TVar v, ()) tab) (typ_tvars ty) tab |
|
95 |
val tvars_count = length (Typtab.keys (fold (fn c => fn tab => collect_tvars (typ_of_constant c) tab) cs Typtab.empty)) |
|
96 |
fun tvars_of ty = collect_tvars ty Typtab.empty |
|
97 |
val cs = map (fn c => (c, tvars_of (typ_of_constant c))) cs |
|
23663 | 98 |
|
24271 | 99 |
fun tyunion A B = |
100 |
Typtab.fold |
|
101 |
(fn (v,()) => fn tab => Typtab.update (v, case Typtab.lookup tab v of NONE => 1 | SOME n => n+1) tab) |
|
102 |
A B |
|
23663 | 103 |
|
104 |
fun is_essential A B = |
|
24271 | 105 |
Typtab.fold |
106 |
(fn (v, ()) => fn essential => essential orelse (case Typtab.lookup B v of NONE => raise Link "is_essential" | SOME n => n=1)) |
|
107 |
A false |
|
23663 | 108 |
|
24271 | 109 |
fun add_minimal (c', tvs') (tvs, cs) = |
110 |
let |
|
111 |
val tvs = tyunion tvs' tvs |
|
112 |
val cs = (c', tvs')::cs |
|
113 |
in |
|
114 |
if forall (fn (c',tvs') => is_essential tvs' tvs) cs then |
|
115 |
SOME (tvs, cs) |
|
116 |
else |
|
117 |
NONE |
|
118 |
end |
|
23663 | 119 |
|
24271 | 120 |
fun is_spanning (tvs, _) = (length (Typtab.keys tvs) = tvars_count) |
23663 | 121 |
|
24271 | 122 |
fun generate_minimal_subsets subsets [] = subsets |
123 |
| generate_minimal_subsets subsets (c::cs) = |
|
124 |
let |
|
125 |
val subsets' = map_filter (add_minimal c) subsets |
|
126 |
in |
|
127 |
generate_minimal_subsets (subsets@subsets') cs |
|
128 |
end*) |
|
23663 | 129 |
|
24271 | 130 |
val minimal_subsets = [old_cs] (*map (fn (tvs, cs) => map fst cs) (filter is_spanning (generate_minimal_subsets [(Typtab.empty, [])] cs))*) |
23663 | 131 |
|
24271 | 132 |
val constants = Consttab.keys (fold (fold (fn c => Consttab.update (c, ()))) minimal_subsets Consttab.empty) |
23663 | 133 |
|
134 |
in |
|
24271 | 135 |
Instances ( |
136 |
fold (fn c => fn tab => ConsttabModTy.update (c, ()) tab) constants ConsttabModTy.empty, |
|
137 |
Consttab.make (map (fn c => (c, Consttab.empty : Type.tyenv Consttab.table)) constants), |
|
138 |
minimal_subsets, Substtab.empty) |
|
23663 | 139 |
end |
140 |
||
141 |
local |
|
142 |
fun calc ctab substtab [] = substtab |
|
24271 | 143 |
| calc ctab substtab (c::cs) = |
23663 | 144 |
let |
24271 | 145 |
val csubsts = map snd (Consttab.dest (the (Consttab.lookup ctab c))) |
146 |
fun merge_substs substtab subst = |
|
147 |
Substtab.fold (fn (s,_) => |
|
148 |
fn tab => |
|
149 |
(case merge_subst subst s of NONE => tab | SOME s => Substtab.update (s, ()) tab)) |
|
150 |
substtab Substtab.empty |
|
151 |
val substtab = substtab_unions (map (merge_substs substtab) csubsts) |
|
23663 | 152 |
in |
24271 | 153 |
calc ctab substtab cs |
23663 | 154 |
end |
155 |
in |
|
156 |
fun calc_substs ctab (cs:constant list) = calc ctab (Substtab.update (empty_subst, ()) Substtab.empty) cs |
|
157 |
end |
|
24271 | 158 |
|
159 |
fun add_instances thy (Instances (cfilter, ctab,minsets,substs)) cs = |
|
160 |
let |
|
161 |
(* val _ = writeln (makestring ("add_instances: ", length_cs, length cs, length (Consttab.keys ctab)))*) |
|
162 |
fun calc_instantiations (constant as Constant (free, name, ty)) instantiations = |
|
163 |
Consttab.fold (fn (constant' as Constant (free', name', ty'), insttab) => |
|
164 |
fn instantiations => |
|
165 |
if free <> free' orelse name <> name' then |
|
166 |
instantiations |
|
167 |
else case Consttab.lookup insttab constant of |
|
168 |
SOME _ => instantiations |
|
169 |
| NONE => ((constant', (constant, Sign.typ_match thy (ty', ty) empty_subst))::instantiations |
|
170 |
handle TYPE_MATCH => instantiations)) |
|
171 |
ctab instantiations |
|
172 |
val instantiations = fold calc_instantiations cs [] |
|
173 |
(*val _ = writeln ("instantiations = "^(makestring (length instantiations)))*) |
|
174 |
fun update_ctab (constant', entry) ctab = |
|
175 |
(case Consttab.lookup ctab constant' of |
|
176 |
NONE => raise Link "internal error: update_ctab" |
|
177 |
| SOME tab => Consttab.update (constant', Consttab.update entry tab) ctab) |
|
178 |
val ctab = fold update_ctab instantiations ctab |
|
179 |
val new_substs = fold (fn minset => fn substs => substtab_union (calc_substs ctab minset) substs) |
|
180 |
minsets Substtab.empty |
|
181 |
val (added_substs, substs) = |
|
182 |
Substtab.fold (fn (ns, _) => |
|
183 |
fn (added, substtab) => |
|
184 |
(case Substtab.lookup substs ns of |
|
185 |
NONE => (ns::added, Substtab.update (ns, ()) substtab) |
|
186 |
| SOME () => (added, substtab))) |
|
187 |
new_substs ([], substs) |
|
23663 | 188 |
in |
24271 | 189 |
(added_substs, Instances (cfilter, ctab, minsets, substs)) |
23663 | 190 |
end |
24271 | 191 |
|
23663 | 192 |
|
193 |
fun substs_of (Instances (_,_,_,substs)) = Substtab.keys substs |
|
194 |
||
195 |
local |
|
196 |
fun get_thm thmname = PureThy.get_thm (theory "Main") (Name thmname) |
|
197 |
val eq_th = get_thm "HOL.eq_reflection" |
|
198 |
in |
|
199 |
fun eq_to_meta th = (eq_th OF [th] handle _ => th) |
|
200 |
end |
|
201 |
||
24271 | 202 |
|
23663 | 203 |
local |
204 |
||
205 |
fun collect (Var x) tab = tab |
|
206 |
| collect (Bound _) tab = tab |
|
207 |
| collect (a $ b) tab = collect b (collect a tab) |
|
208 |
| collect (Abs (_, _, body)) tab = collect body tab |
|
209 |
| collect t tab = Consttab.update (constant_of t, ()) tab |
|
210 |
||
211 |
in |
|
212 |
fun collect_consts tms = Consttab.keys (fold collect tms Consttab.empty) |
|
213 |
end |
|
214 |
||
215 |
end |
|
216 |
||
217 |
signature PCOMPUTE = |
|
218 |
sig |
|
219 |
||
220 |
type pcomputer |
|
24271 | 221 |
|
23663 | 222 |
val make : Compute.machine -> theory -> thm list -> Linker.constant list -> pcomputer |
223 |
||
224 |
(* val add_thms : pcomputer -> thm list -> bool*) |
|
225 |
||
24271 | 226 |
val add_instances : pcomputer -> Linker.constant list -> bool |
23663 | 227 |
|
228 |
val rewrite : pcomputer -> cterm list -> thm list |
|
229 |
||
230 |
end |
|
231 |
||
232 |
structure PCompute : PCOMPUTE = struct |
|
233 |
||
234 |
exception PCompute of string |
|
235 |
||
236 |
datatype theorem = MonoThm of thm | PolyThm of thm * Linker.instances * thm list |
|
237 |
||
238 |
datatype pcomputer = PComputer of Compute.machine * theory_ref * Compute.computer ref * theorem list ref |
|
239 |
||
240 |
(*fun collect_consts (Var x) = [] |
|
241 |
| collect_consts (Bound _) = [] |
|
242 |
| collect_consts (a $ b) = (collect_consts a)@(collect_consts b) |
|
243 |
| collect_consts (Abs (_, _, body)) = collect_consts body |
|
244 |
| collect_consts t = [Linker.constant_of t]*) |
|
245 |
||
24271 | 246 |
fun collect_consts_of_thm th = |
23663 | 247 |
let |
24271 | 248 |
val th = prop_of th |
249 |
val (prems, th) = (Logic.strip_imp_prems th, Logic.strip_imp_concl th) |
|
250 |
val (left, right) = Logic.dest_equals th |
|
23663 | 251 |
in |
24271 | 252 |
(Linker.collect_consts [left], Linker.collect_consts (right::prems)) |
253 |
end |
|
23663 | 254 |
|
255 |
fun create_theorem th = |
|
24271 | 256 |
let |
23663 | 257 |
val (left, right) = collect_consts_of_thm th |
258 |
val polycs = filter Linker.is_polymorphic left |
|
259 |
val tytab = fold (fn p => fn tab => fold (fn n => fn tab => Typtab.update (TVar n, ()) tab) (typ_tvars (Linker.typ_of_constant p)) tab) polycs Typtab.empty |
|
24271 | 260 |
fun check_const (c::cs) cs' = |
261 |
let |
|
262 |
val tvars = typ_tvars (Linker.typ_of_constant c) |
|
263 |
val wrong = fold (fn n => fn wrong => wrong orelse is_none (Typtab.lookup tytab (TVar n))) tvars false |
|
264 |
in |
|
265 |
if wrong then raise PCompute "right hand side of theorem contains type variables which do not occur on the left hand side" |
|
266 |
else |
|
267 |
if null (tvars) then |
|
268 |
check_const cs (c::cs') |
|
269 |
else |
|
270 |
check_const cs cs' |
|
271 |
end |
|
23663 | 272 |
| check_const [] cs' = cs' |
24271 | 273 |
val monocs = check_const right [] |
23663 | 274 |
in |
24271 | 275 |
if null (polycs) then |
276 |
(monocs, MonoThm th) |
|
23663 | 277 |
else |
24271 | 278 |
(monocs, PolyThm (th, Linker.empty polycs, [])) |
23663 | 279 |
end |
280 |
||
24271 | 281 |
fun create_computer machine thy ths = |
23663 | 282 |
let |
24271 | 283 |
fun add (MonoThm th) ths = th::ths |
284 |
| add (PolyThm (_, _, ths')) ths = ths'@ths |
|
285 |
val ths = fold_rev add ths [] |
|
23663 | 286 |
in |
24271 | 287 |
Compute.make machine thy ths |
23663 | 288 |
end |
289 |
||
290 |
fun conv_subst thy (subst : Type.tyenv) = |
|
291 |
map (fn (iname, (sort, ty)) => (ctyp_of thy (TVar (iname, sort)), ctyp_of thy ty)) (Vartab.dest subst) |
|
292 |
||
24271 | 293 |
fun add_monos thy monocs ths = |
23663 | 294 |
let |
24271 | 295 |
val changed = ref false |
296 |
fun add monocs (th as (MonoThm _)) = ([], th) |
|
297 |
| add monocs (PolyThm (th, instances, instanceths)) = |
|
298 |
let |
|
299 |
val (newsubsts, instances) = Linker.add_instances thy instances monocs |
|
300 |
val _ = if not (null newsubsts) then changed := true else () |
|
301 |
val newths = map (fn subst => Thm.instantiate (conv_subst thy subst, []) th) newsubsts |
|
302 |
(* val _ = if not (null newths) then (print ("added new theorems: ", newths); ()) else ()*) |
|
303 |
val newmonos = fold (fn th => fn monos => (snd (collect_consts_of_thm th))@monos) newths [] |
|
304 |
in |
|
305 |
(newmonos, PolyThm (th, instances, instanceths@newths)) |
|
306 |
end |
|
307 |
fun step monocs ths = |
|
308 |
fold_rev (fn th => |
|
309 |
fn (newmonos, ths) => |
|
310 |
let val (newmonos', th') = add monocs th in |
|
311 |
(newmonos'@newmonos, th'::ths) |
|
312 |
end) |
|
313 |
ths ([], []) |
|
314 |
fun loop monocs ths = |
|
315 |
let val (monocs', ths') = step monocs ths in |
|
316 |
if null (monocs') then |
|
317 |
ths' |
|
318 |
else |
|
319 |
loop monocs' ths' |
|
320 |
end |
|
321 |
val result = loop monocs ths |
|
23663 | 322 |
in |
24271 | 323 |
(!changed, result) |
324 |
end |
|
23663 | 325 |
|
326 |
datatype cthm = ComputeThm of term list * sort list * term |
|
327 |
||
24271 | 328 |
fun thm2cthm th = |
23663 | 329 |
let |
24271 | 330 |
val {hyps, prop, shyps, ...} = Thm.rep_thm th |
23663 | 331 |
in |
24271 | 332 |
ComputeThm (hyps, shyps, prop) |
23663 | 333 |
end |
334 |
||
335 |
val cthm_ord' = prod_ord (prod_ord (list_ord Term.term_ord) (list_ord Term.sort_ord)) Term.term_ord |
|
336 |
||
337 |
fun cthm_ord (ComputeThm (h1, sh1, p1), ComputeThm (h2, sh2, p2)) = cthm_ord' (((h1,sh1), p1), ((h2, sh2), p2)) |
|
338 |
||
339 |
structure CThmtab = TableFun (type key = cthm val ord = cthm_ord) |
|
24271 | 340 |
|
23663 | 341 |
fun remove_duplicates ths = |
342 |
let |
|
24271 | 343 |
val counter = ref 0 |
344 |
val tab = ref (CThmtab.empty : unit CThmtab.table) |
|
345 |
val thstab = ref (Inttab.empty : thm Inttab.table) |
|
346 |
fun update th = |
|
347 |
let |
|
348 |
val key = thm2cthm th |
|
349 |
in |
|
350 |
case CThmtab.lookup (!tab) key of |
|
351 |
NONE => ((tab := CThmtab.update_new (key, ()) (!tab)); thstab := Inttab.update_new (!counter, th) (!thstab); counter := !counter + 1) |
|
352 |
| _ => () |
|
353 |
end |
|
354 |
val _ = map update ths |
|
23663 | 355 |
in |
24271 | 356 |
map snd (Inttab.dest (!thstab)) |
23663 | 357 |
end |
24271 | 358 |
|
23663 | 359 |
|
360 |
fun make machine thy ths cs = |
|
361 |
let |
|
24271 | 362 |
val ths = remove_duplicates ths |
363 |
val (monocs, ths) = fold_rev (fn th => |
|
364 |
fn (monocs, ths) => |
|
365 |
let val (m, t) = create_theorem th in |
|
366 |
(m@monocs, t::ths) |
|
367 |
end) |
|
368 |
ths (cs, []) |
|
369 |
val (_, ths) = add_monos thy monocs ths |
|
24137
8d7896398147
replaced Theory.self_ref by Theory.check_thy, which now produces a checked ref;
wenzelm
parents:
23768
diff
changeset
|
370 |
val computer = create_computer machine thy ths |
23663 | 371 |
in |
24271 | 372 |
PComputer (machine, Theory.check_thy thy, ref computer, ref ths) |
23663 | 373 |
end |
374 |
||
24271 | 375 |
fun add_instances (PComputer (machine, thyref, rcomputer, rths)) cs = |
23663 | 376 |
let |
24271 | 377 |
val thy = Theory.deref thyref |
378 |
val (changed, ths) = add_monos thy cs (!rths) |
|
23663 | 379 |
in |
24271 | 380 |
if changed then |
381 |
(rcomputer := create_computer machine thy ths; |
|
382 |
rths := ths; |
|
383 |
true) |
|
384 |
else |
|
385 |
false |
|
23663 | 386 |
end |
387 |
||
388 |
fun rewrite (pc as PComputer (_, _, rcomputer, _)) cts = |
|
389 |
let |
|
24271 | 390 |
val _ = map (fn ct => add_instances pc (Linker.collect_consts [term_of ct])) cts |
23663 | 391 |
in |
24271 | 392 |
map (fn ct => Compute.rewrite (!rcomputer) ct) cts |
23663 | 393 |
end |
24271 | 394 |
|
24137
8d7896398147
replaced Theory.self_ref by Theory.check_thy, which now produces a checked ref;
wenzelm
parents:
23768
diff
changeset
|
395 |
end |