author | haftmann |
Wed, 13 Dec 2006 20:38:19 +0100 | |
changeset 21835 | 84fd5de0691c |
parent 21708 | 45e7491bea47 |
child 21962 | 279b129498b6 |
permissions | -rw-r--r-- |
20600 | 1 |
(* Title: Pure/Tools/codegen_data.ML |
2 |
ID: $Id$ |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
||
20855 | 5 |
Abstract executable content of theory. Management of data dependent on |
6 |
executabl content. |
|
20600 | 7 |
*) |
8 |
||
9 |
signature CODEGEN_DATA = |
|
10 |
sig |
|
21338 | 11 |
val lazy: (unit -> thm list) -> thm list Susp.T |
20600 | 12 |
val eval_always: bool ref |
13 |
||
14 |
val add_func: thm -> theory -> theory |
|
21128 | 15 |
val add_func_legacy: thm -> theory -> theory |
20600 | 16 |
val del_func: thm -> theory -> theory |
21338 | 17 |
val add_funcl: CodegenConsts.const * thm list Susp.T -> theory -> theory |
18 |
val add_datatype: string * (((string * sort) list * (string * typ list) list) * thm list Susp.T) |
|
20600 | 19 |
-> theory -> theory |
20 |
val del_datatype: string -> theory -> theory |
|
21 |
val add_inline: thm -> theory -> theory |
|
22 |
val del_inline: thm -> theory -> theory |
|
20844 | 23 |
val add_inline_proc: (theory -> cterm list -> thm list) -> theory -> theory |
20600 | 24 |
val add_preproc: (theory -> thm list -> thm list) -> theory -> theory |
25 |
val these_funcs: theory -> CodegenConsts.const -> thm list |
|
26 |
val get_datatype: theory -> string |
|
27 |
-> ((string * sort) list * (string * typ list) list) option |
|
28 |
val get_datatype_of_constr: theory -> CodegenConsts.const -> string option |
|
29 |
||
30 |
val print_thms: theory -> unit |
|
31 |
||
32 |
val typ_func: theory -> thm -> typ |
|
20937 | 33 |
val typ_funcs: theory -> CodegenConsts.const * thm list -> typ |
20600 | 34 |
val rewrite_func: thm list -> thm -> thm |
21119 | 35 |
val preprocess_cterm: theory -> cterm -> thm |
20600 | 36 |
|
20844 | 37 |
val trace: bool ref |
20600 | 38 |
end; |
39 |
||
40 |
signature PRIVATE_CODEGEN_DATA = |
|
41 |
sig |
|
42 |
include CODEGEN_DATA |
|
43 |
type data |
|
44 |
structure CodeData: THEORY_DATA |
|
45 |
val declare: string -> Object.T -> (Pretty.pp -> Object.T * Object.T -> Object.T) |
|
20937 | 46 |
-> (theory option -> CodegenConsts.const list option -> Object.T -> Object.T) -> serial |
20600 | 47 |
val init: serial -> theory -> theory |
48 |
val get: serial -> (Object.T -> 'a) -> data -> 'a |
|
49 |
val put: serial -> ('a -> Object.T) -> 'a -> data -> data |
|
50 |
end; |
|
51 |
||
52 |
structure CodegenData : PRIVATE_CODEGEN_DATA = |
|
53 |
struct |
|
54 |
||
55 |
(** diagnostics **) |
|
56 |
||
20844 | 57 |
val trace = ref false; |
58 |
fun tracing f x = (if !trace then Output.tracing (f x) else (); x); |
|
20600 | 59 |
|
60 |
||
61 |
||
62 |
(** lazy theorems, certificate theorems **) |
|
63 |
||
64 |
val eval_always = ref false; |
|
65 |
||
66 |
fun lazy f = if !eval_always |
|
67 |
then Susp.value (f ()) |
|
68 |
else Susp.delay f; |
|
69 |
||
70 |
fun string_of_lthms r = case Susp.peek r |
|
71 |
of SOME thms => (map string_of_thm o rev) thms |
|
72 |
| NONE => ["[...]"]; |
|
73 |
||
74 |
fun pretty_lthms ctxt r = case Susp.peek r |
|
21066
ce6759d1d0b4
code nofunc now permits theorems violating typing discipline
haftmann
parents:
20937
diff
changeset
|
75 |
of SOME thms => map (ProofContext.pretty_thm ctxt) thms |
20600 | 76 |
| NONE => [Pretty.str "[...]"]; |
77 |
||
20844 | 78 |
fun certificate thy f r = |
20600 | 79 |
case Susp.peek r |
20844 | 80 |
of SOME thms => (Susp.value o f thy) thms |
81 |
| NONE => let |
|
82 |
val thy_ref = Theory.self_ref thy; |
|
83 |
in lazy (fn () => (f (Theory.deref thy_ref) o Susp.force) r) end; |
|
20600 | 84 |
|
85 |
fun merge' _ ([], []) = (false, []) |
|
86 |
| merge' _ ([], ys) = (true, ys) |
|
87 |
| merge' eq (xs, ys) = fold_rev |
|
88 |
(fn y => fn (t, xs) => (t orelse not (member eq xs y), insert eq y xs)) ys (false, xs); |
|
89 |
||
90 |
fun merge_alist eq_key eq (xys as (xs, ys)) = |
|
91 |
if eq_list (eq_pair eq_key eq) (xs, ys) |
|
92 |
then (false, xs) |
|
93 |
else (true, AList.merge eq_key eq xys); |
|
94 |
||
95 |
val merge_thms = merge' eq_thm; |
|
96 |
||
97 |
fun merge_lthms (r1, r2) = |
|
21119 | 98 |
if Susp.same (r1, r2) |
99 |
then (false, r1) |
|
20600 | 100 |
else case Susp.peek r1 |
101 |
of SOME [] => (true, r2) |
|
102 |
| _ => case Susp.peek r2 |
|
103 |
of SOME [] => (true, r1) |
|
104 |
| _ => (apsnd (lazy o K)) (merge_thms (Susp.force r1, Susp.force r2)); |
|
105 |
||
106 |
||
107 |
||
108 |
(** code theorems **) |
|
109 |
||
20844 | 110 |
(* making rewrite theorems *) |
20600 | 111 |
|
112 |
fun bad_thm msg thm = |
|
113 |
error (msg ^ ": " ^ string_of_thm thm); |
|
114 |
||
20844 | 115 |
fun check_rew thy thm = |
116 |
let |
|
117 |
val (lhs, rhs) = (Logic.dest_equals o Thm.prop_of) thm; |
|
118 |
fun vars_of t = fold_aterms |
|
119 |
(fn Var (v, _) => insert (op =) v |
|
120 |
| Free _ => bad_thm "Illegal free variable in rewrite theorem" thm |
|
121 |
| _ => I) t []; |
|
122 |
fun tvars_of t = fold_term_types |
|
123 |
(fn _ => fold_atyps (fn TVar (v, _) => insert (op =) v |
|
124 |
| TFree _ => bad_thm "Illegal free type variable in rewrite theorem" thm)) t []; |
|
125 |
val lhs_vs = vars_of lhs; |
|
126 |
val rhs_vs = vars_of rhs; |
|
127 |
val lhs_tvs = tvars_of lhs; |
|
128 |
val rhs_tvs = tvars_of lhs; |
|
129 |
val _ = if null (subtract (op =) lhs_vs rhs_vs) |
|
130 |
then () |
|
131 |
else bad_thm "Free variables on right hand side of rewrite theorems" thm |
|
132 |
val _ = if null (subtract (op =) lhs_tvs rhs_tvs) |
|
133 |
then () |
|
134 |
else bad_thm "Free type variables on right hand side of rewrite theorems" thm |
|
135 |
in thm end; |
|
136 |
||
137 |
fun mk_rew thy thm = |
|
138 |
let |
|
139 |
val thms = (#mk o #mk_rews o snd o MetaSimplifier.rep_ss o Simplifier.simpset_of) thy thm; |
|
140 |
in |
|
141 |
map (check_rew thy) thms |
|
142 |
end; |
|
143 |
||
144 |
||
145 |
(* making function theorems *) |
|
146 |
||
21119 | 147 |
fun typ_func thy = snd o dest_Const o fst o strip_comb |
148 |
o fst o Logic.dest_equals o ObjectLogic.drop_judgment thy o Drule.plain_prop_of; |
|
20600 | 149 |
|
20844 | 150 |
val strict_functyp = ref true; |
151 |
||
21119 | 152 |
fun dest_func thy = apfst dest_Const o strip_comb |
153 |
o fst o Logic.dest_equals o ObjectLogic.drop_judgment thy o Drule.plain_prop_of |
|
154 |
o Drule.fconv_rule Drule.beta_eta_conversion; |
|
20844 | 155 |
|
156 |
fun mk_head thy thm = |
|
157 |
((CodegenConsts.norm_of_typ thy o fst o dest_func thy) thm, thm); |
|
20600 | 158 |
|
21128 | 159 |
fun check_func thy thm = case try (dest_func thy) thm |
20844 | 160 |
of SOME (c_ty as (c, ty), args) => |
161 |
let |
|
162 |
val _ = |
|
163 |
if has_duplicates (op =) |
|
21158 | 164 |
((fold o fold_aterms) (fn Var (v, _) => cons v |
165 |
| Abs _ => bad_thm "Abstraction on left hand side of function equation" thm |
|
166 |
| _ => I |
|
167 |
) args []) |
|
20844 | 168 |
then bad_thm "Repeated variables on left hand side of function equation" thm |
169 |
else () |
|
170 |
val is_classop = (is_some o AxClass.class_of_param thy) c; |
|
171 |
val const = CodegenConsts.norm_of_typ thy c_ty; |
|
172 |
val ty_decl = CodegenConsts.disc_typ_of_const thy |
|
173 |
(snd o CodegenConsts.typ_of_inst thy) const; |
|
174 |
val string_of_typ = setmp show_sorts true (Sign.string_of_typ thy); |
|
175 |
in if Sign.typ_equiv thy (ty_decl, ty) |
|
21128 | 176 |
then SOME (const, thm) |
177 |
else (if is_classop |
|
21421
3436c269dd23
code thms for classops violating type discipline ignored
haftmann
parents:
21338
diff
changeset
|
178 |
then if !strict_functyp |
3436c269dd23
code thms for classops violating type discipline ignored
haftmann
parents:
21338
diff
changeset
|
179 |
then error |
3436c269dd23
code thms for classops violating type discipline ignored
haftmann
parents:
21338
diff
changeset
|
180 |
else warning #> K NONE |
21128 | 181 |
else if Sign.typ_equiv thy (Type.strip_sorts ty_decl, Type.strip_sorts ty) |
182 |
then warning #> (K o SOME) (const, thm) |
|
183 |
else if !strict_functyp |
|
184 |
then error |
|
185 |
else warning #> K NONE) |
|
20844 | 186 |
("Type\n" ^ string_of_typ ty |
187 |
^ "\nof function theorem\n" |
|
188 |
^ string_of_thm thm |
|
189 |
^ "\nis strictly less general than declared function type\n" |
|
21128 | 190 |
^ string_of_typ ty_decl) |
20844 | 191 |
end |
192 |
| NONE => bad_thm "Not a function equation" thm; |
|
193 |
||
194 |
fun check_typ_classop thy thm = |
|
195 |
let |
|
196 |
val (c_ty as (c, ty), _) = dest_func thy thm; |
|
197 |
in case AxClass.class_of_param thy c |
|
198 |
of SOME class => let |
|
199 |
val const = CodegenConsts.norm_of_typ thy c_ty; |
|
200 |
val ty_decl = CodegenConsts.disc_typ_of_const thy |
|
201 |
(snd o CodegenConsts.typ_of_inst thy) const; |
|
202 |
val string_of_typ = setmp show_sorts true (Sign.string_of_typ thy); |
|
203 |
in if Sign.typ_equiv thy (ty_decl, ty) |
|
204 |
then thm |
|
205 |
else error |
|
206 |
("Type\n" ^ string_of_typ ty |
|
207 |
^ "\nof function theorem\n" |
|
208 |
^ string_of_thm thm |
|
209 |
^ "\nis strictly less general than declared function type\n" |
|
210 |
^ string_of_typ ty_decl) |
|
211 |
end |
|
212 |
| NONE => thm |
|
213 |
end; |
|
20600 | 214 |
|
215 |
fun mk_func thy raw_thm = |
|
20844 | 216 |
mk_rew thy raw_thm |
21128 | 217 |
|> map_filter (check_func thy); |
20600 | 218 |
|
219 |
fun get_prim_def_funcs thy c = |
|
220 |
let |
|
221 |
fun constrain thm0 thm = case AxClass.class_of_param thy (fst c) |
|
222 |
of SOME _ => |
|
223 |
let |
|
20704 | 224 |
val ty_decl = CodegenConsts.disc_typ_of_classop thy c; |
20600 | 225 |
val max = maxidx_of_typ ty_decl + 1; |
226 |
val thm = Thm.incr_indexes max thm; |
|
227 |
val ty = typ_func thy thm; |
|
228 |
val (env, _) = Sign.typ_unify thy (ty_decl, ty) (Vartab.empty, max); |
|
229 |
val instT = Vartab.fold (fn (x_i, (sort, ty)) => |
|
230 |
cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env []; |
|
231 |
in Thm.instantiate (instT, []) thm end |
|
232 |
| NONE => thm |
|
233 |
in case CodegenConsts.find_def thy c |
|
234 |
of SOME ((_, thm), _) => |
|
235 |
thm |
|
236 |
|> Thm.transfer thy |
|
237 |
|> try (map snd o mk_func thy) |
|
238 |
|> these |
|
239 |
|> map (constrain thm) |
|
240 |
| NONE => [] |
|
241 |
end; |
|
242 |
||
243 |
||
244 |
(* pairs of (selected, deleted) function theorems *) |
|
245 |
||
21338 | 246 |
type sdthms = thm list Susp.T * thm list; |
20600 | 247 |
|
248 |
fun add_drop_redundant thm thms = |
|
249 |
let |
|
250 |
val thy = Context.check_thy (Thm.theory_of_thm thm); |
|
21284 | 251 |
val args_of = snd o strip_comb o fst o Logic.dest_equals o Drule.plain_prop_of; |
252 |
val args = args_of thm; |
|
253 |
fun matches [] _ = true |
|
254 |
| matches (Var _ :: xs) [] = matches xs [] |
|
255 |
| matches (_ :: _) [] = false |
|
256 |
| matches (x :: xs) (y :: ys) = Pattern.matches thy (x, y) andalso matches xs ys; |
|
257 |
fun drop thm' = if matches args (args_of thm') |
|
258 |
then (warning ("Dropping redundant function theorem\n" ^ string_of_thm thm'); false) |
|
259 |
else true |
|
260 |
in thm :: filter drop thms end; |
|
20600 | 261 |
|
262 |
fun add_thm thm (sels, dels) = |
|
263 |
(Susp.value (add_drop_redundant thm (Susp.force sels)), remove eq_thm thm dels); |
|
264 |
||
265 |
fun add_lthms lthms (sels, []) = |
|
266 |
(lazy (fn () => fold add_drop_redundant |
|
267 |
(Susp.force lthms) (Susp.force sels)), []) |
|
268 |
| add_lthms lthms (sels, dels) = |
|
269 |
fold add_thm (Susp.force lthms) (sels, dels); |
|
270 |
||
271 |
fun del_thm thm (sels, dels) = |
|
272 |
(Susp.value (remove eq_thm thm (Susp.force sels)), thm :: dels); |
|
273 |
||
274 |
fun pretty_sdthms ctxt (sels, _) = pretty_lthms ctxt sels; |
|
275 |
||
21119 | 276 |
fun merge_sdthms ((sels1, dels1), (sels2, dels2)) = |
20600 | 277 |
let |
278 |
val (dels_t, dels) = merge_thms (dels1, dels2); |
|
279 |
in if dels_t |
|
280 |
then let |
|
281 |
val (_, sels) = merge_thms (Susp.force sels1, subtract eq_thm dels1 (Susp.force sels2)) |
|
282 |
val (_, dels) = merge_thms (dels1, subtract eq_thm (Susp.force sels1) dels2) |
|
283 |
in (true, ((lazy o K) sels, dels)) end |
|
284 |
else let |
|
285 |
val (sels_t, sels) = merge_lthms (sels1, sels2) |
|
286 |
in (sels_t, (sels, dels)) end |
|
287 |
end; |
|
288 |
||
289 |
||
290 |
(** data structures **) |
|
291 |
||
292 |
structure Consttab = CodegenConsts.Consttab; |
|
293 |
||
294 |
datatype preproc = Preproc of { |
|
295 |
inlines: thm list, |
|
20844 | 296 |
inline_procs: (serial * (theory -> cterm list -> thm list)) list, |
20600 | 297 |
preprocs: (serial * (theory -> thm list -> thm list)) list |
298 |
}; |
|
299 |
||
21119 | 300 |
fun mk_preproc ((inlines, inline_procs), preprocs) = |
301 |
Preproc { inlines = inlines, inline_procs = inline_procs, preprocs = preprocs }; |
|
302 |
fun map_preproc f (Preproc { inlines, inline_procs, preprocs }) = |
|
303 |
mk_preproc (f ((inlines, inline_procs), preprocs)); |
|
304 |
fun merge_preproc (Preproc { inlines = inlines1, inline_procs = inline_procs1, preprocs = preprocs1 }, |
|
305 |
Preproc { inlines = inlines2, inline_procs = inline_procs2, preprocs = preprocs2 }) = |
|
20600 | 306 |
let |
307 |
val (touched1, inlines) = merge_thms (inlines1, inlines2); |
|
20844 | 308 |
val (touched2, inline_procs) = merge_alist (op =) (K true) (inline_procs1, inline_procs2); |
21119 | 309 |
val (touched3, preprocs) = merge_alist (op =) (K true) (preprocs1, preprocs2); |
310 |
in (touched1 orelse touched2 orelse touched3, |
|
311 |
mk_preproc ((inlines, inline_procs), preprocs)) end; |
|
20600 | 312 |
|
313 |
fun join_func_thms (tabs as (tab1, tab2)) = |
|
314 |
let |
|
315 |
val cs1 = Consttab.keys tab1; |
|
316 |
val cs2 = Consttab.keys tab2; |
|
317 |
val cs' = filter (member CodegenConsts.eq_const cs2) cs1; |
|
318 |
val cs'' = subtract (op =) cs' cs1 @ subtract (op =) cs' cs2; |
|
319 |
val cs''' = ref [] : CodegenConsts.const list ref; |
|
21119 | 320 |
fun merge c x = let val (touched, thms') = merge_sdthms x in |
20600 | 321 |
(if touched then cs''' := cons c (!cs''') else (); thms') end; |
322 |
in (cs'' @ !cs''', Consttab.join merge tabs) end; |
|
323 |
fun merge_funcs (thms1, thms2) = |
|
324 |
let |
|
325 |
val (consts, thms) = join_func_thms (thms1, thms2); |
|
326 |
in (SOME consts, thms) end; |
|
327 |
||
328 |
val eq_string = op = : string * string -> bool; |
|
329 |
fun eq_dtyp (((vs1, cs1), _), ((vs2, cs2), _)) = |
|
330 |
gen_eq_set (eq_pair eq_string (gen_eq_set eq_string)) (vs1, vs2) |
|
331 |
andalso gen_eq_set (eq_pair eq_string (eq_list (is_equal o Term.typ_ord))) (cs1, cs2); |
|
332 |
fun merge_dtyps (tabs as (tab1, tab2)) = |
|
333 |
let |
|
334 |
val tycos1 = Symtab.keys tab1; |
|
335 |
val tycos2 = Symtab.keys tab2; |
|
336 |
val tycos' = filter (member eq_string tycos2) tycos1; |
|
20844 | 337 |
val touched = not (gen_eq_set (op =) (tycos1, tycos2) andalso |
338 |
gen_eq_set (eq_pair (op =) (eq_dtyp)) |
|
20600 | 339 |
(AList.make (the o Symtab.lookup tab1) tycos', |
20844 | 340 |
AList.make (the o Symtab.lookup tab2) tycos')); |
20600 | 341 |
in (touched, Symtab.merge (K true) tabs) end; |
342 |
||
343 |
datatype spec = Spec of { |
|
344 |
funcs: sdthms Consttab.table, |
|
345 |
dconstrs: string Consttab.table, |
|
21338 | 346 |
dtyps: (((string * sort) list * (string * typ list) list) * thm list Susp.T) Symtab.table |
20600 | 347 |
}; |
348 |
||
349 |
fun mk_spec ((funcs, dconstrs), dtyps) = |
|
350 |
Spec { funcs = funcs, dconstrs = dconstrs, dtyps = dtyps }; |
|
351 |
fun map_spec f (Spec { funcs = funcs, dconstrs = dconstrs, dtyps = dtyps }) = |
|
352 |
mk_spec (f ((funcs, dconstrs), dtyps)); |
|
353 |
fun merge_spec (Spec { funcs = funcs1, dconstrs = dconstrs1, dtyps = dtyps1 }, |
|
354 |
Spec { funcs = funcs2, dconstrs = dconstrs2, dtyps = dtyps2 }) = |
|
355 |
let |
|
356 |
val (touched_cs, funcs) = merge_funcs (funcs1, funcs2); |
|
357 |
val dconstrs = Consttab.merge (K true) (dconstrs1, dconstrs2); |
|
358 |
val (touched', dtyps) = merge_dtyps (dtyps1, dtyps2); |
|
359 |
val touched = if touched' then NONE else touched_cs; |
|
360 |
in (touched, mk_spec ((funcs, dconstrs), dtyps)) end; |
|
361 |
||
362 |
datatype exec = Exec of { |
|
363 |
preproc: preproc, |
|
364 |
spec: spec |
|
365 |
}; |
|
366 |
||
367 |
fun mk_exec (preproc, spec) = |
|
368 |
Exec { preproc = preproc, spec = spec }; |
|
369 |
fun map_exec f (Exec { preproc = preproc, spec = spec }) = |
|
370 |
mk_exec (f (preproc, spec)); |
|
371 |
fun merge_exec (Exec { preproc = preproc1, spec = spec1 }, |
|
372 |
Exec { preproc = preproc2, spec = spec2 }) = |
|
373 |
let |
|
374 |
val (touched', preproc) = merge_preproc (preproc1, preproc2); |
|
375 |
val (touched_cs, spec) = merge_spec (spec1, spec2); |
|
376 |
val touched = if touched' then NONE else touched_cs; |
|
377 |
in (touched, mk_exec (preproc, spec)) end; |
|
21119 | 378 |
val empty_exec = mk_exec (mk_preproc (([], []), []), |
20600 | 379 |
mk_spec ((Consttab.empty, Consttab.empty), Symtab.empty)); |
380 |
||
381 |
fun the_preproc (Exec { preproc = Preproc x, ...}) = x; |
|
382 |
fun the_spec (Exec { spec = Spec x, ...}) = x; |
|
383 |
val the_funcs = #funcs o the_spec; |
|
384 |
val the_dcontrs = #dconstrs o the_spec; |
|
385 |
val the_dtyps = #dtyps o the_spec; |
|
386 |
val map_preproc = map_exec o apfst o map_preproc; |
|
387 |
val map_funcs = map_exec o apsnd o map_spec o apfst o apfst; |
|
388 |
val map_dconstrs = map_exec o apsnd o map_spec o apfst o apsnd; |
|
389 |
val map_dtyps = map_exec o apsnd o map_spec o apsnd; |
|
390 |
||
391 |
||
392 |
(** code data structures **) |
|
393 |
||
394 |
(*private copy avoids potential conflict of table exceptions*) |
|
395 |
structure Datatab = TableFun(type key = int val ord = int_ord); |
|
396 |
||
397 |
||
398 |
(* data slots *) |
|
399 |
||
400 |
local |
|
401 |
||
402 |
type kind = { |
|
403 |
name: string, |
|
404 |
empty: Object.T, |
|
405 |
merge: Pretty.pp -> Object.T * Object.T -> Object.T, |
|
20937 | 406 |
purge: theory option -> CodegenConsts.const list option -> Object.T -> Object.T |
20600 | 407 |
}; |
408 |
||
409 |
val kinds = ref (Datatab.empty: kind Datatab.table); |
|
410 |
||
411 |
fun invoke meth_name meth_fn k = |
|
412 |
(case Datatab.lookup (! kinds) k of |
|
413 |
SOME kind => meth_fn kind |> transform_failure (fn exn => |
|
414 |
EXCEPTION (exn, "Code data method " ^ #name kind ^ "." ^ meth_name ^ " failed")) |
|
415 |
| NONE => sys_error ("Invalid code data identifier " ^ string_of_int k)); |
|
416 |
||
417 |
||
418 |
in |
|
419 |
||
420 |
fun invoke_name k = invoke "name" (K o #name) k (); |
|
421 |
fun invoke_empty k = invoke "empty" (K o #empty) k (); |
|
422 |
fun invoke_merge pp = invoke "merge" (fn kind => #merge kind pp); |
|
20937 | 423 |
fun invoke_purge thy_opt cs = invoke "purge" (fn kind => #purge kind thy_opt cs); |
20600 | 424 |
|
425 |
fun declare name empty merge purge = |
|
426 |
let |
|
427 |
val k = serial (); |
|
428 |
val kind = {name = name, empty = empty, merge = merge, purge = purge}; |
|
429 |
val _ = conditional (Datatab.exists (equal name o #name o #2) (! kinds)) (fn () => |
|
430 |
warning ("Duplicate declaration of code data " ^ quote name)); |
|
431 |
val _ = change kinds (Datatab.update (k, kind)); |
|
432 |
in k end; |
|
433 |
||
434 |
end; (*local*) |
|
435 |
||
436 |
||
437 |
(* theory store *) |
|
438 |
||
439 |
type data = Object.T Datatab.table; |
|
440 |
||
441 |
structure CodeData = TheoryDataFun |
|
442 |
(struct |
|
443 |
val name = "Pure/codegen_data"; |
|
444 |
type T = exec * data ref; |
|
445 |
val empty = (empty_exec, ref Datatab.empty : data ref); |
|
446 |
fun copy (exec, data) = (exec, ref (! data)); |
|
447 |
val extend = copy; |
|
448 |
fun merge pp ((exec1, data1), (exec2, data2)) = |
|
449 |
let |
|
450 |
val (touched, exec) = merge_exec (exec1, exec2); |
|
20937 | 451 |
val data1' = Datatab.map' (invoke_purge NONE touched) (! data1); |
452 |
val data2' = Datatab.map' (invoke_purge NONE touched) (! data2); |
|
20600 | 453 |
val data = Datatab.join (invoke_merge pp) (data1', data2'); |
454 |
in (exec, ref data) end; |
|
455 |
fun print thy (exec, _) = |
|
456 |
let |
|
457 |
val ctxt = ProofContext.init thy; |
|
458 |
fun pretty_func (s, lthms) = |
|
459 |
(Pretty.block o Pretty.fbreaks) ( |
|
460 |
Pretty.str s :: pretty_sdthms ctxt lthms |
|
461 |
); |
|
462 |
fun pretty_dtyp (s, cos) = |
|
463 |
(Pretty.block o Pretty.breaks) ( |
|
464 |
Pretty.str s |
|
465 |
:: Pretty.str "=" |
|
466 |
:: Pretty.separate "|" (map (fn (c, []) => Pretty.str c |
|
467 |
| (c, tys) => |
|
468 |
Pretty.block |
|
469 |
(Pretty.str c :: Pretty.brk 1 :: Pretty.str "of" :: Pretty.brk 1 |
|
470 |
:: Pretty.breaks (map (Pretty.quote o Sign.pretty_typ thy) tys))) cos) |
|
471 |
) |
|
472 |
val inlines = (#inlines o the_preproc) exec; |
|
473 |
val funs = the_funcs exec |
|
474 |
|> Consttab.dest |
|
475 |
|> (map o apfst) (CodegenConsts.string_of_const thy) |
|
476 |
|> sort (string_ord o pairself fst); |
|
477 |
val dtyps = the_dtyps exec |
|
478 |
|> Symtab.dest |
|
479 |
|> map (fn (dtco, ((vs, cos), _)) => (Sign.string_of_typ thy (Type (dtco, map TFree vs)), cos)) |
|
480 |
|> sort (string_ord o pairself fst) |
|
481 |
in |
|
482 |
(Pretty.writeln o Pretty.block o Pretty.fbreaks) ([ |
|
483 |
Pretty.str "code theorems:", |
|
484 |
Pretty.str "function theorems:" ] @ |
|
485 |
map pretty_func funs @ [ |
|
486 |
Pretty.block ( |
|
487 |
Pretty.str "inlined theorems:" |
|
488 |
:: Pretty.fbrk |
|
489 |
:: (Pretty.fbreaks o map (ProofContext.pretty_thm ctxt)) inlines |
|
490 |
), |
|
491 |
Pretty.block ( |
|
492 |
Pretty.str "datatypes:" |
|
493 |
:: Pretty.fbrk |
|
494 |
:: (Pretty.fbreaks o map pretty_dtyp) dtyps |
|
495 |
)] |
|
496 |
) |
|
497 |
end; |
|
498 |
end); |
|
499 |
||
500 |
fun print_thms thy = CodeData.print thy; |
|
501 |
||
502 |
fun init k = CodeData.map |
|
503 |
(fn (exec, data) => (exec, ref (Datatab.update (k, invoke_empty k) (! data)))); |
|
504 |
||
505 |
fun get k dest data = |
|
506 |
(case Datatab.lookup data k of |
|
507 |
SOME x => (dest x handle Match => |
|
508 |
error ("Failed to access code data " ^ quote (invoke_name k))) |
|
509 |
| NONE => error ("Uninitialized code data " ^ quote (invoke_name k))); |
|
510 |
||
511 |
fun put k mk x = Datatab.update (k, mk x); |
|
512 |
||
20937 | 513 |
fun map_exec_purge touched f thy = |
20600 | 514 |
CodeData.map (fn (exec, data) => |
20937 | 515 |
(f exec, ref (Datatab.map' (invoke_purge (SOME thy) touched) (! data)))) thy; |
20600 | 516 |
|
517 |
val get_exec = fst o CodeData.get; |
|
518 |
||
519 |
val _ = Context.add_setup CodeData.init; |
|
520 |
||
521 |
||
522 |
||
523 |
(** theorem transformation and certification **) |
|
524 |
||
525 |
fun rewrite_func rewrites thm = |
|
526 |
let |
|
21708 | 527 |
val rewrite = MetaSimplifier.rewrite false rewrites; |
20844 | 528 |
val (ct_eq, [ct_lhs, ct_rhs]) = (Drule.strip_comb o Thm.cprop_of) thm; |
529 |
val Const ("==", _) = Thm.term_of ct_eq; |
|
20600 | 530 |
val (ct_f, ct_args) = Drule.strip_comb ct_lhs; |
531 |
val rhs' = rewrite ct_rhs; |
|
532 |
val args' = map rewrite ct_args; |
|
533 |
val lhs' = Thm.symmetric (fold (fn th1 => fn th2 => Thm.combination th2 th1) |
|
534 |
args' (Thm.reflexive ct_f)); |
|
535 |
in |
|
536 |
Thm.transitive (Thm.transitive lhs' thm) rhs' |
|
537 |
end handle Bind => raise ERROR "rewrite_func" |
|
538 |
||
539 |
fun common_typ_funcs thy [] = [] |
|
540 |
| common_typ_funcs thy [thm] = [thm] |
|
541 |
| common_typ_funcs thy thms = |
|
542 |
let |
|
543 |
fun incr_thm thm max = |
|
544 |
let |
|
545 |
val thm' = incr_indexes max thm; |
|
21119 | 546 |
val max' = Thm.maxidx_of thm' + 1; |
20600 | 547 |
in (thm', max') end; |
548 |
val (thms', maxidx) = fold_map incr_thm thms 0; |
|
21119 | 549 |
val (ty1::tys) = map (typ_func thy) thms'; |
20600 | 550 |
fun unify ty env = Sign.typ_unify thy (ty1, ty) env |
551 |
handle Type.TUNIFY => |
|
552 |
error ("Type unificaton failed, while unifying function equations\n" |
|
553 |
^ (cat_lines o map Display.string_of_thm) thms |
|
554 |
^ "\nwith types\n" |
|
555 |
^ (cat_lines o map (Sign.string_of_typ thy)) (ty1 :: tys)); |
|
556 |
val (env, _) = fold unify tys (Vartab.empty, maxidx) |
|
557 |
val instT = Vartab.fold (fn (x_i, (sort, ty)) => |
|
558 |
cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env []; |
|
21119 | 559 |
in map (Thm.instantiate (instT, [])) thms' end; |
20600 | 560 |
|
20844 | 561 |
fun certify_const thy c c_thms = |
20600 | 562 |
let |
563 |
fun cert (c', thm) = if CodegenConsts.eq_const (c, c') |
|
564 |
then thm else bad_thm ("Wrong head of function equation,\nexpected constant " |
|
565 |
^ CodegenConsts.string_of_const thy c) thm |
|
20844 | 566 |
in map cert c_thms end; |
20600 | 567 |
|
568 |
fun mk_cos tyco vs cos = |
|
569 |
let |
|
570 |
val dty = Type (tyco, map TFree vs); |
|
571 |
fun mk_co (co, tys) = (Const (co, (tys ---> dty)), map I tys); |
|
572 |
in map mk_co cos end; |
|
573 |
||
574 |
fun mk_co_args (co, tys) ctxt = |
|
575 |
let |
|
576 |
val names = Name.invents ctxt "a" (length tys); |
|
577 |
val ctxt' = fold Name.declare names ctxt; |
|
578 |
val vs = map2 (fn v => fn ty => Free (fst (v, 0), I ty)) names tys; |
|
579 |
in (vs, ctxt') end; |
|
580 |
||
581 |
fun check_freeness thy cos thms = |
|
582 |
let |
|
583 |
val props = AList.make Drule.plain_prop_of thms; |
|
584 |
fun sym_product [] = [] |
|
585 |
| sym_product (x::xs) = map (pair x) xs @ sym_product xs; |
|
586 |
val quodlibet = |
|
587 |
let |
|
588 |
val judg = ObjectLogic.fixed_judgment (the_context ()) "x"; |
|
589 |
val [free] = fold_aterms (fn v as Free _ => cons v | _ => I) judg []; |
|
590 |
val judg' = Term.subst_free [(free, Bound 0)] judg; |
|
591 |
val prop = Type ("prop", []); |
|
592 |
val prop' = fastype_of judg'; |
|
593 |
in |
|
594 |
Const ("all", (prop' --> prop) --> prop) $ Abs ("P", prop', judg') |
|
595 |
end; |
|
596 |
fun check_inj (co, []) = |
|
597 |
NONE |
|
598 |
| check_inj (co, tys) = |
|
599 |
let |
|
600 |
val ((xs, ys), _) = Name.context |
|
601 |
|> mk_co_args (co, tys) |
|
602 |
||>> mk_co_args (co, tys); |
|
603 |
val prem = Logic.mk_equals |
|
604 |
(list_comb (co, xs), list_comb (co, ys)); |
|
605 |
val concl = Logic.mk_conjunction_list |
|
606 |
(map2 (curry Logic.mk_equals) xs ys); |
|
607 |
val t = Logic.mk_implies (prem, concl); |
|
608 |
in case find_first (curry Term.could_unify t o snd) props |
|
609 |
of SOME (thm, _) => SOME thm |
|
610 |
| NONE => error ("Could not prove injectiveness statement\n" |
|
611 |
^ Sign.string_of_term thy t |
|
612 |
^ "\nfor constructor " |
|
613 |
^ CodegenConsts.string_of_const_typ thy (dest_Const co) |
|
614 |
^ "\nwith theorems\n" ^ cat_lines (map string_of_thm thms)) |
|
615 |
end; |
|
616 |
fun check_dist ((co1, tys1), (co2, tys2)) = |
|
617 |
let |
|
618 |
val ((xs1, xs2), _) = Name.context |
|
619 |
|> mk_co_args (co1, tys1) |
|
620 |
||>> mk_co_args (co2, tys2); |
|
621 |
val prem = Logic.mk_equals |
|
622 |
(list_comb (co1, xs1), list_comb (co2, xs2)); |
|
623 |
val t = Logic.mk_implies (prem, quodlibet); |
|
624 |
in case find_first (curry Term.could_unify t o snd) props |
|
625 |
of SOME (thm, _) => thm |
|
626 |
| NONE => error ("Could not prove distinctness statement\n" |
|
627 |
^ Sign.string_of_term thy t |
|
628 |
^ "\nfor constructors " |
|
629 |
^ CodegenConsts.string_of_const_typ thy (dest_Const co1) |
|
630 |
^ " and " |
|
631 |
^ CodegenConsts.string_of_const_typ thy (dest_Const co2) |
|
632 |
^ "\nwith theorems\n" ^ cat_lines (map string_of_thm thms)) |
|
633 |
end; |
|
634 |
in (map_filter check_inj cos, map check_dist (sym_product cos)) end; |
|
635 |
||
636 |
fun certify_datatype thy dtco cs thms = |
|
637 |
(op @) (check_freeness thy cs thms); |
|
638 |
||
639 |
||
640 |
||
641 |
(** interfaces **) |
|
642 |
||
643 |
fun add_func thm thy = |
|
644 |
let |
|
645 |
val thms = mk_func thy thm; |
|
646 |
val cs = map fst thms; |
|
647 |
in |
|
648 |
map_exec_purge (SOME cs) (map_funcs |
|
649 |
(fold (fn (c, thm) => Consttab.map_default |
|
650 |
(c, (Susp.value [], [])) (add_thm thm)) thms)) thy |
|
651 |
end; |
|
652 |
||
21128 | 653 |
fun add_func_legacy thm = setmp strict_functyp false (add_func thm); |
654 |
||
20600 | 655 |
fun del_func thm thy = |
656 |
let |
|
21158 | 657 |
val thms = mk_func thy thm; |
20600 | 658 |
val cs = map fst thms; |
659 |
in |
|
660 |
map_exec_purge (SOME cs) (map_funcs |
|
661 |
(fold (fn (c, thm) => Consttab.map_entry c |
|
662 |
(del_thm thm)) thms)) thy |
|
663 |
end; |
|
664 |
||
665 |
fun add_funcl (c, lthms) thy = |
|
666 |
let |
|
667 |
val c' = CodegenConsts.norm thy c; |
|
20844 | 668 |
val lthms' = certificate thy (fn thy => certify_const thy c' o maps (mk_func thy)) lthms; |
20600 | 669 |
in |
670 |
map_exec_purge (SOME [c]) (map_funcs (Consttab.map_default (c', (Susp.value [], [])) |
|
671 |
(add_lthms lthms'))) thy |
|
672 |
end; |
|
673 |
||
674 |
fun add_datatype (tyco, (vs_cos as (vs, cos), lthms)) thy = |
|
675 |
let |
|
676 |
val cs = mk_cos tyco vs cos; |
|
677 |
val consts = map (CodegenConsts.norm_of_typ thy o dest_Const o fst) cs; |
|
678 |
val add = |
|
679 |
map_dtyps (Symtab.update_new (tyco, |
|
20844 | 680 |
(vs_cos, certificate thy (fn thy => certify_datatype thy tyco cs) lthms))) |
20600 | 681 |
#> map_dconstrs (fold (fn c => Consttab.update (c, tyco)) consts) |
682 |
in map_exec_purge (SOME consts) add thy end; |
|
683 |
||
684 |
fun del_datatype tyco thy = |
|
685 |
let |
|
20639 | 686 |
val SOME ((vs, cos), _) = Symtab.lookup ((the_dtyps o get_exec) thy) tyco; |
687 |
val cs = mk_cos tyco vs cos; |
|
688 |
val consts = map (CodegenConsts.norm_of_typ thy o dest_Const o fst) cs; |
|
20600 | 689 |
val del = |
690 |
map_dtyps (Symtab.delete tyco) |
|
20639 | 691 |
#> map_dconstrs (fold Consttab.delete consts) |
692 |
in map_exec_purge (SOME consts) del thy end; |
|
20600 | 693 |
|
694 |
fun add_inline thm thy = |
|
20844 | 695 |
(map_exec_purge NONE o map_preproc o apfst o apfst) (fold (insert eq_thm) (mk_rew thy thm)) thy; |
20600 | 696 |
|
697 |
fun del_inline thm thy = |
|
20844 | 698 |
(map_exec_purge NONE o map_preproc o apfst o apfst) (fold (remove eq_thm) (mk_rew thy thm)) thy ; |
699 |
||
700 |
fun add_inline_proc f = |
|
701 |
(map_exec_purge NONE o map_preproc o apfst o apsnd) (cons (serial (), f)); |
|
702 |
||
20600 | 703 |
fun add_preproc f = |
21119 | 704 |
(map_exec_purge NONE o map_preproc o apsnd) (cons (serial (), f)); |
20844 | 705 |
|
706 |
local |
|
707 |
||
708 |
fun gen_apply_inline_proc prep post thy f x = |
|
709 |
let |
|
710 |
val cts = prep x; |
|
711 |
val rews = map (check_rew thy) (f thy cts); |
|
712 |
in post rews x end; |
|
713 |
||
714 |
val apply_inline_proc = gen_apply_inline_proc (maps |
|
715 |
((fn [args, rhs] => rhs :: (snd o Drule.strip_comb) args) o snd o Drule.strip_comb o Thm.cprop_of)) |
|
716 |
(fn rews => map (rewrite_func rews)); |
|
717 |
val apply_inline_proc_cterm = gen_apply_inline_proc single |
|
21708 | 718 |
(MetaSimplifier.rewrite false); |
20600 | 719 |
|
20844 | 720 |
fun apply_preproc thy f [] = [] |
721 |
| apply_preproc thy f (thms as (thm :: _)) = |
|
722 |
let |
|
723 |
val thms' = f thy thms; |
|
724 |
val c = (CodegenConsts.norm_of_typ thy o fst o dest_func thy) thm; |
|
725 |
in (certify_const thy c o map (mk_head thy)) thms' end; |
|
726 |
||
727 |
fun cmp_thms thy = |
|
728 |
make_ord (fn (thm1, thm2) => not (Sign.typ_instance thy (typ_func thy thm1, typ_func thy thm2))); |
|
729 |
||
730 |
fun rhs_conv conv thm = |
|
731 |
let |
|
732 |
val thm' = (conv o snd o Drule.dest_equals o Thm.cprop_of) thm; |
|
733 |
in Thm.transitive thm thm' end |
|
734 |
||
735 |
in |
|
20600 | 736 |
|
737 |
fun preprocess thy thms = |
|
20844 | 738 |
thms |
739 |
|> fold (fn (_, f) => apply_preproc thy f) ((#preprocs o the_preproc o get_exec) thy) |
|
740 |
|> map (rewrite_func ((#inlines o the_preproc o get_exec) thy)) |
|
741 |
|> fold (fn (_, f) => apply_inline_proc thy f) ((#inline_procs o the_preproc o get_exec) thy) |
|
21835 | 742 |
(*FIXME - must check: rewrite rule, function equation, proper constant |> map (snd o check_func false thy) *) |
20844 | 743 |
|> sort (cmp_thms thy) |
744 |
|> common_typ_funcs thy; |
|
20600 | 745 |
|
21119 | 746 |
fun preprocess_cterm thy ct = |
20844 | 747 |
ct |
21119 | 748 |
|> Thm.reflexive |
21708 | 749 |
|> fold (rhs_conv o MetaSimplifier.rewrite false o single) |
750 |
((#inlines o the_preproc o get_exec) thy) |
|
751 |
|> fold (fn (_, f) => rhs_conv (apply_inline_proc_cterm thy f)) |
|
752 |
((#inline_procs o the_preproc o get_exec) thy) |
|
20844 | 753 |
|
754 |
end; (*local*) |
|
20600 | 755 |
|
756 |
fun these_funcs thy c = |
|
757 |
let |
|
20844 | 758 |
val funcs_1 = |
20600 | 759 |
Consttab.lookup ((the_funcs o get_exec) thy) c |
760 |
|> Option.map (Susp.force o fst) |
|
761 |
|> these |
|
762 |
|> map (Thm.transfer thy); |
|
20844 | 763 |
val funcs_2 = case funcs_1 |
764 |
of [] => get_prim_def_funcs thy c |
|
765 |
| xs => xs; |
|
20600 | 766 |
fun drop_refl thy = filter_out (is_equal o Term.fast_term_ord o Logic.dest_equals |
767 |
o ObjectLogic.drop_judgment thy o Drule.plain_prop_of); |
|
768 |
in |
|
20844 | 769 |
funcs_2 |
20600 | 770 |
|> preprocess thy |
771 |
|> drop_refl thy |
|
772 |
end; |
|
773 |
||
774 |
fun get_datatype thy tyco = |
|
775 |
Symtab.lookup ((the_dtyps o get_exec) thy) tyco |
|
776 |
|> Option.map (fn (spec, thms) => (Susp.force thms; spec)); |
|
777 |
||
778 |
fun get_datatype_of_constr thy c = |
|
779 |
Consttab.lookup ((the_dcontrs o get_exec) thy) c |
|
780 |
|> (Option.map o tap) (fn dtco => get_datatype thy dtco); |
|
781 |
||
20937 | 782 |
fun typ_funcs thy (c as (name, _), []) = (case AxClass.class_of_param thy name |
783 |
of SOME class => CodegenConsts.disc_typ_of_classop thy c |
|
784 |
| NONE => (case Option.map (Susp.force o fst) (Consttab.lookup ((the_funcs o get_exec) thy) c) |
|
785 |
of SOME [eq] => typ_func thy eq |
|
786 |
| _ => Sign.the_const_type thy name)) |
|
787 |
| typ_funcs thy (_, eq :: _) = typ_func thy eq; |
|
20600 | 788 |
|
789 |
||
790 |
(** code attributes **) |
|
791 |
||
792 |
local |
|
793 |
fun add_simple_attribute (name, f) = |
|
794 |
(Codegen.add_attribute name o (Scan.succeed o Thm.declaration_attribute)) |
|
20897 | 795 |
(fn th => Context.mapping (f th) I); |
20600 | 796 |
in |
797 |
val _ = map (Context.add_setup o add_simple_attribute) [ |
|
798 |
("func", add_func), |
|
799 |
("nofunc", del_func), |
|
800 |
("unfold", (fn thm => Codegen.add_unfold thm #> add_inline thm)), |
|
801 |
("inline", add_inline), |
|
802 |
("noinline", del_inline) |
|
803 |
] |
|
804 |
end; (*local*) |
|
805 |
||
806 |
end; (*struct*) |
|
807 |
||
808 |
||
809 |
||
810 |
(** type-safe interfaces for data depedent on executable content **) |
|
811 |
||
812 |
signature CODE_DATA_ARGS = |
|
813 |
sig |
|
814 |
val name: string |
|
815 |
type T |
|
816 |
val empty: T |
|
817 |
val merge: Pretty.pp -> T * T -> T |
|
20937 | 818 |
val purge: theory option -> CodegenConsts.const list option -> T -> T |
20600 | 819 |
end; |
820 |
||
821 |
signature CODE_DATA = |
|
822 |
sig |
|
823 |
type T |
|
824 |
val init: theory -> theory |
|
825 |
val get: theory -> T |
|
826 |
val change: theory -> (T -> T) -> T |
|
827 |
val change_yield: theory -> (T -> 'a * T) -> 'a * T |
|
828 |
end; |
|
829 |
||
830 |
functor CodeDataFun(Data: CODE_DATA_ARGS): CODE_DATA = |
|
831 |
struct |
|
832 |
||
833 |
type T = Data.T; |
|
834 |
exception Data of T; |
|
835 |
fun dest (Data x) = x |
|
836 |
||
837 |
val kind = CodegenData.declare Data.name (Data Data.empty) |
|
838 |
(fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2))) |
|
20937 | 839 |
(fn thy_opt => fn cs => fn Data x => Data (Data.purge thy_opt cs x)); |
20600 | 840 |
|
841 |
val init = CodegenData.init kind; |
|
842 |
fun get thy = CodegenData.get kind dest ((! o snd o CodegenData.CodeData.get) thy); |
|
843 |
fun change thy f = |
|
844 |
let |
|
845 |
val data_ref = (snd o CodegenData.CodeData.get) thy; |
|
846 |
val x = (f o CodegenData.get kind dest o !) data_ref; |
|
847 |
val data = CodegenData.put kind Data x (! data_ref); |
|
848 |
in (data_ref := data; x) end; |
|
849 |
fun change_yield thy f = |
|
850 |
let |
|
851 |
val data_ref = (snd o CodegenData.CodeData.get) thy; |
|
852 |
val (y, x) = (f o CodegenData.get kind dest o !) data_ref; |
|
853 |
val data = CodegenData.put kind Data x (! data_ref); |
|
854 |
in (data_ref := data; (y, x)) end; |
|
855 |
||
856 |
end; |
|
857 |
||
858 |
structure CodegenData : CODEGEN_DATA = |
|
859 |
struct |
|
860 |
||
861 |
open CodegenData; |
|
862 |
||
863 |
end; |