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