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