author | haftmann |
Fri, 30 Mar 2007 16:19:03 +0200 | |
changeset 22554 | d1499fff65d8 |
parent 22507 | 3572bc633d9a |
child 22566 | 535ae9dd4c45 |
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 |
||
22484
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22423
diff
changeset
|
14 |
val add_func: bool -> thm -> theory -> theory |
20600 | 15 |
val del_func: thm -> theory -> theory |
21338 | 16 |
val add_funcl: CodegenConsts.const * thm list Susp.T -> theory -> theory |
20600 | 17 |
val add_inline: thm -> theory -> theory |
18 |
val del_inline: thm -> theory -> theory |
|
22050 | 19 |
val add_inline_proc: string * (theory -> cterm list -> thm list) -> theory -> theory |
20 |
val del_inline_proc: string -> theory -> theory |
|
21 |
val add_preproc: string * (theory -> thm list -> thm list) -> theory -> theory |
|
22 |
val del_preproc: string -> theory -> theory |
|
22507 | 23 |
val add_datatype: string * ((string * sort) list * (string * typ list) list) |
24 |
-> theory -> theory |
|
25 |
val add_datatype_consts: CodegenConsts.const list -> theory -> theory |
|
26 |
||
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
27 |
val coregular_algebra: theory -> Sorts.algebra |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
28 |
val operational_algebra: theory -> (sort -> sort) * Sorts.algebra |
20600 | 29 |
val these_funcs: theory -> CodegenConsts.const -> thm list |
22423 | 30 |
val get_datatype: theory -> string -> ((string * sort) list * (string * typ list) list) |
20600 | 31 |
val get_datatype_of_constr: theory -> CodegenConsts.const -> string option |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
32 |
val default_typ: theory -> CodegenConsts.const -> typ |
20600 | 33 |
|
22033 | 34 |
val preprocess_cterm: cterm -> thm |
20600 | 35 |
|
20844 | 36 |
val trace: bool ref |
20600 | 37 |
end; |
38 |
||
39 |
signature PRIVATE_CODEGEN_DATA = |
|
40 |
sig |
|
41 |
include CODEGEN_DATA |
|
42 |
type data |
|
43 |
structure CodeData: THEORY_DATA |
|
44 |
val declare: string -> Object.T -> (Pretty.pp -> Object.T * Object.T -> Object.T) |
|
20937 | 45 |
-> (theory option -> CodegenConsts.const list option -> Object.T -> Object.T) -> serial |
20600 | 46 |
val init: serial -> theory -> theory |
47 |
val get: serial -> (Object.T -> 'a) -> data -> 'a |
|
48 |
val put: serial -> ('a -> Object.T) -> 'a -> data -> data |
|
49 |
end; |
|
50 |
||
51 |
structure CodegenData : PRIVATE_CODEGEN_DATA = |
|
52 |
struct |
|
53 |
||
54 |
(** diagnostics **) |
|
55 |
||
20844 | 56 |
val trace = ref false; |
57 |
fun tracing f x = (if !trace then Output.tracing (f x) else (); x); |
|
20600 | 58 |
|
59 |
||
60 |
||
61 |
(** lazy theorems, certificate theorems **) |
|
62 |
||
63 |
val eval_always = ref false; |
|
64 |
||
65 |
fun lazy f = if !eval_always |
|
66 |
then Susp.value (f ()) |
|
67 |
else Susp.delay f; |
|
68 |
||
69 |
fun string_of_lthms r = case Susp.peek r |
|
70 |
of SOME thms => (map string_of_thm o rev) thms |
|
71 |
| NONE => ["[...]"]; |
|
72 |
||
73 |
fun pretty_lthms ctxt r = case Susp.peek r |
|
21066
ce6759d1d0b4
code nofunc now permits theorems violating typing discipline
haftmann
parents:
20937
diff
changeset
|
74 |
of SOME thms => map (ProofContext.pretty_thm ctxt) thms |
20600 | 75 |
| NONE => [Pretty.str "[...]"]; |
76 |
||
20844 | 77 |
fun certificate thy f r = |
20600 | 78 |
case Susp.peek r |
20844 | 79 |
of SOME thms => (Susp.value o f thy) thms |
80 |
| NONE => let |
|
81 |
val thy_ref = Theory.self_ref thy; |
|
82 |
in lazy (fn () => (f (Theory.deref thy_ref) o Susp.force) r) end; |
|
20600 | 83 |
|
84 |
fun merge' _ ([], []) = (false, []) |
|
85 |
| merge' _ ([], ys) = (true, ys) |
|
86 |
| merge' eq (xs, ys) = fold_rev |
|
87 |
(fn y => fn (t, xs) => (t orelse not (member eq xs y), insert eq y xs)) ys (false, xs); |
|
88 |
||
89 |
fun merge_alist eq_key eq (xys as (xs, ys)) = |
|
90 |
if eq_list (eq_pair eq_key eq) (xs, ys) |
|
91 |
then (false, xs) |
|
92 |
else (true, AList.merge eq_key eq xys); |
|
93 |
||
22360
26ead7ed4f4b
moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents:
22319
diff
changeset
|
94 |
val merge_thms = merge' Thm.eq_thm; |
20600 | 95 |
|
96 |
fun merge_lthms (r1, r2) = |
|
21119 | 97 |
if Susp.same (r1, r2) |
98 |
then (false, r1) |
|
20600 | 99 |
else case Susp.peek r1 |
100 |
of SOME [] => (true, r2) |
|
101 |
| _ => case Susp.peek r2 |
|
102 |
of SOME [] => (true, r1) |
|
103 |
| _ => (apsnd (lazy o K)) (merge_thms (Susp.force r1, Susp.force r2)); |
|
104 |
||
105 |
||
106 |
||
107 |
(** code theorems **) |
|
108 |
||
22197 | 109 |
(* pairs of (selected, deleted) defining equations *) |
20600 | 110 |
|
21338 | 111 |
type sdthms = thm list Susp.T * thm list; |
20600 | 112 |
|
22006
9dc365b03573
dropped function theorems are considered as deleted
haftmann
parents:
21988
diff
changeset
|
113 |
fun add_drop_redundant thm (sels, dels) = |
20600 | 114 |
let |
22006
9dc365b03573
dropped function theorems are considered as deleted
haftmann
parents:
21988
diff
changeset
|
115 |
val thy = Thm.theory_of_thm thm; |
21284 | 116 |
val args_of = snd o strip_comb o fst o Logic.dest_equals o Drule.plain_prop_of; |
117 |
val args = args_of thm; |
|
118 |
fun matches [] _ = true |
|
119 |
| matches (Var _ :: xs) [] = matches xs [] |
|
120 |
| matches (_ :: _) [] = false |
|
121 |
| 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
|
122 |
fun drop thm' = not (matches args (args_of thm')) |
22197 | 123 |
orelse (warning ("Dropping redundant defining equation\n" ^ string_of_thm thm'); false); |
22006
9dc365b03573
dropped function theorems are considered as deleted
haftmann
parents:
21988
diff
changeset
|
124 |
val (keeps, drops) = List.partition drop sels; |
22360
26ead7ed4f4b
moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents:
22319
diff
changeset
|
125 |
in (thm :: keeps, dels |> fold (insert Thm.eq_thm) drops |> remove Thm.eq_thm thm) end; |
20600 | 126 |
|
127 |
fun add_thm thm (sels, dels) = |
|
22006
9dc365b03573
dropped function theorems are considered as deleted
haftmann
parents:
21988
diff
changeset
|
128 |
apfst Susp.value (add_drop_redundant thm (Susp.force sels, dels)); |
20600 | 129 |
|
130 |
fun add_lthms lthms (sels, []) = |
|
131 |
(lazy (fn () => fold add_drop_redundant |
|
22006
9dc365b03573
dropped function theorems are considered as deleted
haftmann
parents:
21988
diff
changeset
|
132 |
(Susp.force lthms) (Susp.force sels, []) |> fst), []) |
9dc365b03573
dropped function theorems are considered as deleted
haftmann
parents:
21988
diff
changeset
|
133 |
(*FIXME*) |
20600 | 134 |
| add_lthms lthms (sels, dels) = |
135 |
fold add_thm (Susp.force lthms) (sels, dels); |
|
136 |
||
137 |
fun del_thm thm (sels, dels) = |
|
22360
26ead7ed4f4b
moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents:
22319
diff
changeset
|
138 |
(Susp.value (remove Thm.eq_thm thm (Susp.force sels)), thm :: dels); |
20600 | 139 |
|
140 |
fun pretty_sdthms ctxt (sels, _) = pretty_lthms ctxt sels; |
|
141 |
||
21119 | 142 |
fun merge_sdthms ((sels1, dels1), (sels2, dels2)) = |
20600 | 143 |
let |
144 |
val (dels_t, dels) = merge_thms (dels1, dels2); |
|
145 |
in if dels_t |
|
146 |
then let |
|
22360
26ead7ed4f4b
moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents:
22319
diff
changeset
|
147 |
val (_, sels) = merge_thms (Susp.force sels1, subtract Thm.eq_thm dels1 (Susp.force sels2)) |
26ead7ed4f4b
moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents:
22319
diff
changeset
|
148 |
val (_, dels) = merge_thms (dels1, subtract Thm.eq_thm (Susp.force sels1) dels2) |
20600 | 149 |
in (true, ((lazy o K) sels, dels)) end |
150 |
else let |
|
151 |
val (sels_t, sels) = merge_lthms (sels1, sels2) |
|
152 |
in (sels_t, (sels, dels)) end |
|
153 |
end; |
|
154 |
||
155 |
||
156 |
(** data structures **) |
|
157 |
||
158 |
structure Consttab = CodegenConsts.Consttab; |
|
159 |
||
160 |
datatype preproc = Preproc of { |
|
161 |
inlines: thm list, |
|
22050 | 162 |
inline_procs: (string * (serial * (theory -> cterm list -> thm list))) list, |
163 |
preprocs: (string * (serial * (theory -> thm list -> thm list))) list |
|
20600 | 164 |
}; |
165 |
||
21119 | 166 |
fun mk_preproc ((inlines, inline_procs), preprocs) = |
167 |
Preproc { inlines = inlines, inline_procs = inline_procs, preprocs = preprocs }; |
|
168 |
fun map_preproc f (Preproc { inlines, inline_procs, preprocs }) = |
|
169 |
mk_preproc (f ((inlines, inline_procs), preprocs)); |
|
170 |
fun merge_preproc (Preproc { inlines = inlines1, inline_procs = inline_procs1, preprocs = preprocs1 }, |
|
171 |
Preproc { inlines = inlines2, inline_procs = inline_procs2, preprocs = preprocs2 }) = |
|
20600 | 172 |
let |
173 |
val (touched1, inlines) = merge_thms (inlines1, inlines2); |
|
22050 | 174 |
val (touched2, inline_procs) = merge_alist (op =) (eq_fst (op =)) (inline_procs1, inline_procs2); |
175 |
val (touched3, preprocs) = merge_alist (op =) (eq_fst (op =)) (preprocs1, preprocs2); |
|
21119 | 176 |
in (touched1 orelse touched2 orelse touched3, |
177 |
mk_preproc ((inlines, inline_procs), preprocs)) end; |
|
20600 | 178 |
|
179 |
fun join_func_thms (tabs as (tab1, tab2)) = |
|
180 |
let |
|
181 |
val cs1 = Consttab.keys tab1; |
|
182 |
val cs2 = Consttab.keys tab2; |
|
183 |
val cs' = filter (member CodegenConsts.eq_const cs2) cs1; |
|
184 |
val cs'' = subtract (op =) cs' cs1 @ subtract (op =) cs' cs2; |
|
185 |
val cs''' = ref [] : CodegenConsts.const list ref; |
|
21119 | 186 |
fun merge c x = let val (touched, thms') = merge_sdthms x in |
20600 | 187 |
(if touched then cs''' := cons c (!cs''') else (); thms') end; |
188 |
in (cs'' @ !cs''', Consttab.join merge tabs) end; |
|
189 |
fun merge_funcs (thms1, thms2) = |
|
190 |
let |
|
191 |
val (consts, thms) = join_func_thms (thms1, thms2); |
|
192 |
in (SOME consts, thms) end; |
|
193 |
||
194 |
val eq_string = op = : string * string -> bool; |
|
22507 | 195 |
val eq_co = eq_pair eq_string (eq_list (is_equal o Term.typ_ord)); |
22423 | 196 |
fun eq_dtyp ((vs1, cs1), (vs2, cs2)) = |
20600 | 197 |
gen_eq_set (eq_pair eq_string (gen_eq_set eq_string)) (vs1, vs2) |
22507 | 198 |
andalso gen_eq_set eq_co (cs1, cs2); |
20600 | 199 |
fun merge_dtyps (tabs as (tab1, tab2)) = |
200 |
let |
|
201 |
val tycos1 = Symtab.keys tab1; |
|
202 |
val tycos2 = Symtab.keys tab2; |
|
203 |
val tycos' = filter (member eq_string tycos2) tycos1; |
|
22507 | 204 |
val new_types = not (gen_eq_set (op =) (tycos1, tycos2)); |
205 |
val diff_types = not (gen_eq_set (eq_pair (op =) eq_dtyp) |
|
20600 | 206 |
(AList.make (the o Symtab.lookup tab1) tycos', |
20844 | 207 |
AList.make (the o Symtab.lookup tab2) tycos')); |
22507 | 208 |
in ((new_types, diff_types), Symtab.merge (K true) tabs) end; |
20600 | 209 |
|
210 |
datatype spec = Spec of { |
|
211 |
funcs: sdthms Consttab.table, |
|
22423 | 212 |
dtyps: ((string * sort) list * (string * typ list) list) Symtab.table |
20600 | 213 |
}; |
214 |
||
22507 | 215 |
fun mk_spec (funcs, dtyps) = |
216 |
Spec { funcs = funcs, dtyps = dtyps }; |
|
217 |
fun map_spec f (Spec { funcs = funcs, dtyps = dtyps }) = |
|
218 |
mk_spec (f (funcs, dtyps)); |
|
219 |
fun merge_spec (Spec { funcs = funcs1, dtyps = dtyps1 }, |
|
220 |
Spec { funcs = funcs2, dtyps = dtyps2 }) = |
|
20600 | 221 |
let |
222 |
val (touched_cs, funcs) = merge_funcs (funcs1, funcs2); |
|
22507 | 223 |
val ((new_types, diff_types), dtyps) = merge_dtyps (dtyps1, dtyps2); |
224 |
val touched = if new_types orelse diff_types then NONE else touched_cs; |
|
225 |
in (touched, mk_spec (funcs, dtyps)) end; |
|
20600 | 226 |
|
227 |
datatype exec = Exec of { |
|
228 |
preproc: preproc, |
|
229 |
spec: spec |
|
230 |
}; |
|
231 |
||
232 |
fun mk_exec (preproc, spec) = |
|
233 |
Exec { preproc = preproc, spec = spec }; |
|
234 |
fun map_exec f (Exec { preproc = preproc, spec = spec }) = |
|
235 |
mk_exec (f (preproc, spec)); |
|
236 |
fun merge_exec (Exec { preproc = preproc1, spec = spec1 }, |
|
237 |
Exec { preproc = preproc2, spec = spec2 }) = |
|
238 |
let |
|
239 |
val (touched', preproc) = merge_preproc (preproc1, preproc2); |
|
240 |
val (touched_cs, spec) = merge_spec (spec1, spec2); |
|
241 |
val touched = if touched' then NONE else touched_cs; |
|
242 |
in (touched, mk_exec (preproc, spec)) end; |
|
21119 | 243 |
val empty_exec = mk_exec (mk_preproc (([], []), []), |
22507 | 244 |
mk_spec (Consttab.empty, Symtab.empty)); |
20600 | 245 |
|
246 |
fun the_preproc (Exec { preproc = Preproc x, ...}) = x; |
|
247 |
fun the_spec (Exec { spec = Spec x, ...}) = x; |
|
248 |
val the_funcs = #funcs o the_spec; |
|
249 |
val the_dtyps = #dtyps o the_spec; |
|
250 |
val map_preproc = map_exec o apfst o map_preproc; |
|
22507 | 251 |
val map_funcs = map_exec o apsnd o map_spec o apfst; |
20600 | 252 |
val map_dtyps = map_exec o apsnd o map_spec o apsnd; |
253 |
||
254 |
||
255 |
(** code data structures **) |
|
256 |
||
257 |
(*private copy avoids potential conflict of table exceptions*) |
|
258 |
structure Datatab = TableFun(type key = int val ord = int_ord); |
|
259 |
||
260 |
||
261 |
(* data slots *) |
|
262 |
||
263 |
local |
|
264 |
||
265 |
type kind = { |
|
266 |
name: string, |
|
267 |
empty: Object.T, |
|
268 |
merge: Pretty.pp -> Object.T * Object.T -> Object.T, |
|
20937 | 269 |
purge: theory option -> CodegenConsts.const list option -> Object.T -> Object.T |
20600 | 270 |
}; |
271 |
||
272 |
val kinds = ref (Datatab.empty: kind Datatab.table); |
|
273 |
||
274 |
fun invoke meth_name meth_fn k = |
|
275 |
(case Datatab.lookup (! kinds) k of |
|
276 |
SOME kind => meth_fn kind |> transform_failure (fn exn => |
|
277 |
EXCEPTION (exn, "Code data method " ^ #name kind ^ "." ^ meth_name ^ " failed")) |
|
278 |
| NONE => sys_error ("Invalid code data identifier " ^ string_of_int k)); |
|
279 |
||
280 |
||
281 |
in |
|
282 |
||
283 |
fun invoke_name k = invoke "name" (K o #name) k (); |
|
284 |
fun invoke_empty k = invoke "empty" (K o #empty) k (); |
|
285 |
fun invoke_merge pp = invoke "merge" (fn kind => #merge kind pp); |
|
20937 | 286 |
fun invoke_purge thy_opt cs = invoke "purge" (fn kind => #purge kind thy_opt cs); |
20600 | 287 |
|
288 |
fun declare name empty merge purge = |
|
289 |
let |
|
290 |
val k = serial (); |
|
291 |
val kind = {name = name, empty = empty, merge = merge, purge = purge}; |
|
21962 | 292 |
val _ = |
293 |
if Datatab.exists (equal name o #name o #2) (! kinds) then |
|
294 |
warning ("Duplicate declaration of code data " ^ quote name) |
|
295 |
else (); |
|
20600 | 296 |
val _ = change kinds (Datatab.update (k, kind)); |
297 |
in k end; |
|
298 |
||
299 |
end; (*local*) |
|
300 |
||
301 |
||
302 |
(* theory store *) |
|
303 |
||
304 |
type data = Object.T Datatab.table; |
|
305 |
||
306 |
structure CodeData = TheoryDataFun |
|
307 |
(struct |
|
308 |
val name = "Pure/codegen_data"; |
|
309 |
type T = exec * data ref; |
|
310 |
val empty = (empty_exec, ref Datatab.empty : data ref); |
|
311 |
fun copy (exec, data) = (exec, ref (! data)); |
|
312 |
val extend = copy; |
|
313 |
fun merge pp ((exec1, data1), (exec2, data2)) = |
|
314 |
let |
|
315 |
val (touched, exec) = merge_exec (exec1, exec2); |
|
20937 | 316 |
val data1' = Datatab.map' (invoke_purge NONE touched) (! data1); |
317 |
val data2' = Datatab.map' (invoke_purge NONE touched) (! data2); |
|
20600 | 318 |
val data = Datatab.join (invoke_merge pp) (data1', data2'); |
319 |
in (exec, ref data) end; |
|
320 |
fun print thy (exec, _) = |
|
321 |
let |
|
322 |
val ctxt = ProofContext.init thy; |
|
323 |
fun pretty_func (s, lthms) = |
|
324 |
(Pretty.block o Pretty.fbreaks) ( |
|
325 |
Pretty.str s :: pretty_sdthms ctxt lthms |
|
326 |
); |
|
22423 | 327 |
fun pretty_dtyp (s, []) = |
328 |
Pretty.str s |
|
329 |
| pretty_dtyp (s, cos) = |
|
330 |
(Pretty.block o Pretty.breaks) ( |
|
331 |
Pretty.str s |
|
332 |
:: Pretty.str "=" |
|
333 |
:: separate (Pretty.str "|") (map (fn (c, []) => Pretty.str c |
|
334 |
| (c, tys) => |
|
335 |
(Pretty.block o Pretty.breaks) |
|
336 |
(Pretty.str c :: Pretty.str "of" :: map (Pretty.quote o Sign.pretty_typ thy) tys)) cos) |
|
337 |
); |
|
20600 | 338 |
val inlines = (#inlines o the_preproc) exec; |
22050 | 339 |
val inline_procs = (map fst o #inline_procs o the_preproc) exec; |
340 |
val preprocs = (map fst o #preprocs o the_preproc) exec; |
|
20600 | 341 |
val funs = the_funcs exec |
342 |
|> Consttab.dest |
|
343 |
|> (map o apfst) (CodegenConsts.string_of_const thy) |
|
344 |
|> sort (string_ord o pairself fst); |
|
345 |
val dtyps = the_dtyps exec |
|
346 |
|> Symtab.dest |
|
22423 | 347 |
|> map (fn (dtco, (vs, cos)) => (Sign.string_of_typ thy (Type (dtco, map TFree vs)), cos)) |
20600 | 348 |
|> sort (string_ord o pairself fst) |
349 |
in |
|
22303 | 350 |
(Pretty.writeln o Pretty.chunks) [ |
351 |
Pretty.block ( |
|
352 |
Pretty.str "defining equations:" |
|
22423 | 353 |
:: Pretty.fbrk |
354 |
:: (Pretty.fbreaks o map pretty_func) funs |
|
22303 | 355 |
), |
20600 | 356 |
Pretty.block ( |
22050 | 357 |
Pretty.str "inlining theorems:" |
20600 | 358 |
:: Pretty.fbrk |
359 |
:: (Pretty.fbreaks o map (ProofContext.pretty_thm ctxt)) inlines |
|
360 |
), |
|
361 |
Pretty.block ( |
|
22050 | 362 |
Pretty.str "inlining procedures:" |
363 |
:: Pretty.fbrk |
|
364 |
:: (Pretty.fbreaks o map Pretty.str) inline_procs |
|
365 |
), |
|
366 |
Pretty.block ( |
|
367 |
Pretty.str "preprocessors:" |
|
368 |
:: Pretty.fbrk |
|
369 |
:: (Pretty.fbreaks o map Pretty.str) preprocs |
|
370 |
), |
|
371 |
Pretty.block ( |
|
20600 | 372 |
Pretty.str "datatypes:" |
373 |
:: Pretty.fbrk |
|
374 |
:: (Pretty.fbreaks o map pretty_dtyp) dtyps |
|
22303 | 375 |
) |
376 |
] |
|
20600 | 377 |
end; |
378 |
end); |
|
379 |
||
380 |
fun init k = CodeData.map |
|
381 |
(fn (exec, data) => (exec, ref (Datatab.update (k, invoke_empty k) (! data)))); |
|
382 |
||
383 |
fun get k dest data = |
|
384 |
(case Datatab.lookup data k of |
|
385 |
SOME x => (dest x handle Match => |
|
386 |
error ("Failed to access code data " ^ quote (invoke_name k))) |
|
387 |
| NONE => error ("Uninitialized code data " ^ quote (invoke_name k))); |
|
388 |
||
389 |
fun put k mk x = Datatab.update (k, mk x); |
|
390 |
||
20937 | 391 |
fun map_exec_purge touched f thy = |
20600 | 392 |
CodeData.map (fn (exec, data) => |
20937 | 393 |
(f exec, ref (Datatab.map' (invoke_purge (SOME thy) touched) (! data)))) thy; |
20600 | 394 |
|
395 |
val get_exec = fst o CodeData.get; |
|
396 |
||
397 |
val _ = Context.add_setup CodeData.init; |
|
398 |
||
399 |
||
400 |
||
401 |
(** theorem transformation and certification **) |
|
402 |
||
22033 | 403 |
fun common_typ_funcs [] = [] |
404 |
| common_typ_funcs [thm] = [thm] |
|
405 |
| common_typ_funcs thms = |
|
20600 | 406 |
let |
22033 | 407 |
val thy = Thm.theory_of_thm (hd thms) |
20600 | 408 |
fun incr_thm thm max = |
409 |
let |
|
410 |
val thm' = incr_indexes max thm; |
|
21119 | 411 |
val max' = Thm.maxidx_of thm' + 1; |
20600 | 412 |
in (thm', max') end; |
413 |
val (thms', maxidx) = fold_map incr_thm thms 0; |
|
22033 | 414 |
val (ty1::tys) = map CodegenFunc.typ_func thms'; |
20600 | 415 |
fun unify ty env = Sign.typ_unify thy (ty1, ty) env |
416 |
handle Type.TUNIFY => |
|
22197 | 417 |
error ("Type unificaton failed, while unifying defining equations\n" |
20600 | 418 |
^ (cat_lines o map Display.string_of_thm) thms |
419 |
^ "\nwith types\n" |
|
22197 | 420 |
^ (cat_lines o map (CodegenConsts.string_of_typ thy)) (ty1 :: tys)); |
20600 | 421 |
val (env, _) = fold unify tys (Vartab.empty, maxidx) |
422 |
val instT = Vartab.fold (fn (x_i, (sort, ty)) => |
|
423 |
cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env []; |
|
21119 | 424 |
in map (Thm.instantiate (instT, [])) thms' end; |
20600 | 425 |
|
20844 | 426 |
fun certify_const thy c c_thms = |
20600 | 427 |
let |
428 |
fun cert (c', thm) = if CodegenConsts.eq_const (c, c') |
|
22197 | 429 |
then thm else error ("Wrong head of defining equation,\nexpected constant " |
22033 | 430 |
^ CodegenConsts.string_of_const thy c ^ "\n" ^ string_of_thm thm) |
20844 | 431 |
in map cert c_thms end; |
20600 | 432 |
|
433 |
||
434 |
||
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
435 |
(** operational sort algebra and class discipline **) |
22050 | 436 |
|
437 |
local |
|
438 |
||
439 |
fun aggr_neutr f y [] = y |
|
440 |
| aggr_neutr f y (x::xs) = aggr_neutr f (f y x) xs; |
|
441 |
||
442 |
fun aggregate f [] = NONE |
|
443 |
| aggregate f (x::xs) = SOME (aggr_neutr f x xs); |
|
444 |
||
445 |
fun inter_sorts thy = |
|
446 |
let |
|
447 |
val algebra = Sign.classes_of thy; |
|
448 |
val inters = curry (Sorts.inter_sort algebra); |
|
449 |
in aggregate (map2 inters) end; |
|
450 |
||
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
451 |
fun specific_constraints thy (class, tyco) = |
22050 | 452 |
let |
453 |
val vs = Name.invents Name.context "" (Sign.arity_number thy tyco); |
|
454 |
val clsops = (these o Option.map snd o try (AxClass.params_of_class thy)) class; |
|
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
455 |
val funcs = clsops |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
456 |
|> map (fn (clsop, _) => (clsop, SOME tyco)) |
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
457 |
|> map (Consttab.lookup ((the_funcs o get_exec) thy)) |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
458 |
|> (map o Option.map) (Susp.force o fst) |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
459 |
|> maps these |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
460 |
|> map (Thm.transfer thy); |
22050 | 461 |
val sorts = map (map (snd o dest_TVar) o snd o dest_Type o the_single |
462 |
o Sign.const_typargs thy o fst o CodegenFunc.dest_func) funcs; |
|
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
463 |
in sorts end; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
464 |
|
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
465 |
fun weakest_constraints thy (class, tyco) = |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
466 |
let |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
467 |
val all_superclasses = class :: Graph.all_succs ((#classes o Sorts.rep_algebra o Sign.classes_of) thy) [class]; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
468 |
in case inter_sorts thy (maps (fn class => specific_constraints thy (class, tyco)) all_superclasses) |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
469 |
of SOME sorts => sorts |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
470 |
| NONE => Sign.arity_sorts thy tyco [class] |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
471 |
end; |
22050 | 472 |
|
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
473 |
fun strongest_constraints thy (class, tyco) = |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
474 |
let |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
475 |
val algebra = Sign.classes_of thy; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
476 |
val all_subclasses = class :: Graph.all_preds ((#classes o Sorts.rep_algebra) algebra) [class]; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
477 |
val inst_subclasses = filter (can (Sorts.mg_domain algebra tyco) o single) all_subclasses; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
478 |
in case inter_sorts thy (maps (fn class => specific_constraints thy (class, tyco)) inst_subclasses) |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
479 |
of SOME sorts => sorts |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
480 |
| NONE => replicate |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
481 |
(Sign.arity_number thy tyco) (Sign.certify_sort thy (Sign.all_classes thy)) |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
482 |
end; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
483 |
|
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
484 |
fun gen_classop_typ constr thy class (c, tyco) = |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
485 |
let |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
486 |
val (var, cs) = try (AxClass.params_of_class thy) class |> the_default ("'a", []) |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
487 |
val ty = (the o AList.lookup (op =) cs) c; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
488 |
val sort_args = Name.names (Name.declare var Name.context) "'a" |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
489 |
(constr thy (class, tyco)); |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
490 |
val ty_inst = Type (tyco, map TFree sort_args); |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
491 |
in Logic.varifyT (map_type_tfree (K ty_inst) ty) end; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
492 |
|
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
493 |
fun retrieve_algebra thy operational = |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
494 |
Sorts.subalgebra (Sign.pp thy) operational |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
495 |
(weakest_constraints thy) |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
496 |
(Sign.classes_of thy); |
22050 | 497 |
|
498 |
in |
|
499 |
||
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
500 |
fun coregular_algebra thy = retrieve_algebra thy (K true) |> snd; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
501 |
fun operational_algebra thy = |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
502 |
let |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
503 |
fun add_iff_operational class classes = |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
504 |
if (not o null o these o Option.map #params o try (AxClass.get_definition thy)) class |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
505 |
orelse (length o gen_inter (op =)) |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
506 |
((Sign.certify_sort thy o Sign.super_classes thy) class, classes) >= 2 |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
507 |
then class :: classes |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
508 |
else classes; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
509 |
val operational_classes = fold add_iff_operational (Sign.all_classes thy) [] |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
510 |
in retrieve_algebra thy (member (op =) operational_classes) end; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
511 |
|
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
512 |
val classop_weakest_typ = gen_classop_typ weakest_constraints; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
513 |
val classop_strongest_typ = gen_classop_typ strongest_constraints; |
22050 | 514 |
|
22484
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22423
diff
changeset
|
515 |
fun gen_mk_func_typ strict thm = |
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
516 |
let |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
517 |
val thy = Thm.theory_of_thm thm; |
22484
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22423
diff
changeset
|
518 |
val raw_funcs = CodegenFunc.mk_func strict thm; |
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22423
diff
changeset
|
519 |
val error_warning = if strict then error else warning #> K NONE; |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
520 |
fun check_typ_classop class (const as (c, SOME tyco), thm) = |
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
521 |
let |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
522 |
val ((_, ty), _) = CodegenFunc.dest_func thm; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
523 |
val ty_decl = classop_weakest_typ thy class (c, tyco); |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
524 |
val ty_strongest = classop_strongest_typ thy class (c, tyco); |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
525 |
fun constrain thm = |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
526 |
let |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
527 |
val max = Thm.maxidx_of thm + 1; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
528 |
val ty_decl' = Logic.incr_tvar max ty_decl; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
529 |
val ((_, ty'), _) = CodegenFunc.dest_func thm; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
530 |
val (env, _) = Sign.typ_unify thy (ty_decl', ty') (Vartab.empty, max); |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
531 |
val instT = Vartab.fold (fn (x_i, (sort, ty)) => |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
532 |
cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env []; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
533 |
in Thm.instantiate (instT, []) thm end; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
534 |
in if Sign.typ_instance thy (ty_strongest, ty) |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
535 |
then if Sign.typ_instance thy (ty, ty_decl) |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
536 |
then SOME (const, thm) |
22197 | 537 |
else (warning ("Constraining type\n" ^ CodegenConsts.string_of_typ thy ty |
538 |
^ "\nof defining equation\n" |
|
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
539 |
^ string_of_thm thm |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
540 |
^ "\nto permitted most general type\n" |
22197 | 541 |
^ CodegenConsts.string_of_typ thy ty_decl); |
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
542 |
SOME (const, constrain thm)) |
22197 | 543 |
else error_warning ("Type\n" ^ CodegenConsts.string_of_typ thy ty |
544 |
^ "\nof defining equation\n" |
|
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
545 |
^ string_of_thm thm |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
546 |
^ "\nis incompatible with permitted least general type\n" |
22197 | 547 |
^ CodegenConsts.string_of_typ thy ty_strongest) |
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
548 |
end |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
549 |
| check_typ_classop class ((c, NONE), thm) = |
22484
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22423
diff
changeset
|
550 |
error_warning ("Illegal type for class operation " ^ quote c |
22197 | 551 |
^ "\nin defining equation\n" |
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
552 |
^ string_of_thm thm); |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
553 |
fun check_typ_fun (const as (c, _), thm) = |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
554 |
let |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
555 |
val ((_, ty), _) = CodegenFunc.dest_func thm; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
556 |
val ty_decl = Sign.the_const_type thy c; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
557 |
in if Sign.typ_equiv thy (Type.strip_sorts ty_decl, Type.strip_sorts ty) |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
558 |
then SOME (const, thm) |
22197 | 559 |
else error_warning ("Type\n" ^ CodegenConsts.string_of_typ thy ty |
560 |
^ "\nof defining equation\n" |
|
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
561 |
^ string_of_thm thm |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
562 |
^ "\nis incompatible declared function type\n" |
22197 | 563 |
^ CodegenConsts.string_of_typ thy ty_decl) |
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
564 |
end; |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
565 |
fun check_typ (const as (c, _), thm) = |
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
566 |
case AxClass.class_of_param thy c |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
567 |
of SOME class => check_typ_classop class (const, thm) |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
568 |
| NONE => check_typ_fun (const, thm); |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
569 |
val funcs = map_filter check_typ raw_funcs; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
570 |
in funcs end; |
22050 | 571 |
|
572 |
end; |
|
573 |
||
574 |
||
20600 | 575 |
(** interfaces **) |
576 |
||
22484
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22423
diff
changeset
|
577 |
fun add_func strict thm thy = |
20600 | 578 |
let |
22484
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22423
diff
changeset
|
579 |
val funcs = gen_mk_func_typ strict thm; |
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
580 |
val cs = map fst funcs; |
20600 | 581 |
in |
582 |
map_exec_purge (SOME cs) (map_funcs |
|
583 |
(fold (fn (c, thm) => Consttab.map_default |
|
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
584 |
(c, (Susp.value [], [])) (add_thm thm)) funcs)) thy |
20600 | 585 |
end; |
586 |
||
22319 | 587 |
fun delete_force msg key xs = |
588 |
if AList.defined (op =) xs key then AList.delete (op =) key xs |
|
589 |
else error ("No such " ^ msg ^ ": " ^ quote key); |
|
590 |
||
20600 | 591 |
fun del_func thm thy = |
592 |
let |
|
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
593 |
val funcs = gen_mk_func_typ false thm; |
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
594 |
val cs = map fst funcs; |
20600 | 595 |
in |
596 |
map_exec_purge (SOME cs) (map_funcs |
|
597 |
(fold (fn (c, thm) => Consttab.map_entry c |
|
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
598 |
(del_thm thm)) funcs)) thy |
20600 | 599 |
end; |
600 |
||
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
601 |
fun add_funcl (const, lthms) thy = |
20600 | 602 |
let |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
603 |
val lthms' = certificate thy (fn thy => certify_const thy const |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
604 |
o maps (CodegenFunc.mk_func true)) lthms; |
20600 | 605 |
in |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
606 |
map_exec_purge (SOME [const]) (map_funcs (Consttab.map_default (const, (Susp.value [], [])) |
20600 | 607 |
(add_lthms lthms'))) thy |
608 |
end; |
|
609 |
||
22423 | 610 |
local |
611 |
||
20600 | 612 |
fun del_datatype tyco thy = |
22423 | 613 |
case Symtab.lookup ((the_dtyps o get_exec) thy) tyco |
614 |
of SOME (vs, cos) => let |
|
22507 | 615 |
val consts = CodegenConsts.consts_of_cos thy tyco vs cos; |
616 |
in map_exec_purge (SOME consts) (map_dtyps (Symtab.delete tyco)) thy end |
|
22423 | 617 |
| NONE => thy; |
618 |
||
619 |
in |
|
620 |
||
621 |
fun add_datatype (tyco, (vs_cos as (vs, cos))) thy = |
|
20600 | 622 |
let |
22507 | 623 |
val consts = CodegenConsts.consts_of_cos thy tyco vs cos; |
22423 | 624 |
in |
625 |
thy |
|
626 |
|> del_datatype tyco |
|
22507 | 627 |
|> map_exec_purge (SOME consts) (map_dtyps (Symtab.update_new (tyco, vs_cos))) |
22423 | 628 |
end; |
629 |
||
22507 | 630 |
fun add_datatype_consts consts thy = |
631 |
add_datatype (CodegenConsts.cos_of_consts thy consts) thy; |
|
22423 | 632 |
|
633 |
fun add_datatype_consts_cmd raw_cs thy = |
|
22507 | 634 |
add_datatype_consts (map (CodegenConsts.read_const thy) raw_cs) thy |
22423 | 635 |
|
636 |
end; (*local*) |
|
20600 | 637 |
|
638 |
fun add_inline thm thy = |
|
22423 | 639 |
(map_exec_purge NONE o map_preproc o apfst o apfst) |
640 |
(fold (insert Thm.eq_thm) (CodegenFunc.mk_rew thm)) thy; |
|
641 |
(*fully applied in order to get right context for mk_rew!*) |
|
20600 | 642 |
|
643 |
fun del_inline thm thy = |
|
22423 | 644 |
(map_exec_purge NONE o map_preproc o apfst o apfst) |
645 |
(fold (remove Thm.eq_thm) (CodegenFunc.mk_rew thm)) thy; |
|
646 |
(*fully applied in order to get right context for mk_rew!*) |
|
20844 | 647 |
|
22050 | 648 |
fun add_inline_proc (name, f) = |
22423 | 649 |
(map_exec_purge NONE o map_preproc o apfst o apsnd) |
650 |
(AList.update (op =) (name, (serial (), f))); |
|
22050 | 651 |
|
652 |
fun del_inline_proc name = |
|
22423 | 653 |
(map_exec_purge NONE o map_preproc o apfst o apsnd) |
654 |
(delete_force "inline procedure" name); |
|
20844 | 655 |
|
22050 | 656 |
fun add_preproc (name, f) = |
657 |
(map_exec_purge NONE o map_preproc o apsnd) (AList.update (op =) (name, (serial (), f))); |
|
658 |
||
659 |
fun del_preproc name = |
|
22319 | 660 |
(map_exec_purge NONE o map_preproc o apsnd) (delete_force "preprocessor" name); |
20844 | 661 |
|
22507 | 662 |
|
663 |
||
664 |
(** retrieval **) |
|
665 |
||
20844 | 666 |
local |
667 |
||
668 |
fun gen_apply_inline_proc prep post thy f x = |
|
669 |
let |
|
670 |
val cts = prep x; |
|
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
671 |
val rews = map CodegenFunc.assert_rew (f thy cts); |
20844 | 672 |
in post rews x end; |
673 |
||
674 |
val apply_inline_proc = gen_apply_inline_proc (maps |
|
675 |
((fn [args, rhs] => rhs :: (snd o Drule.strip_comb) args) o snd o Drule.strip_comb o Thm.cprop_of)) |
|
22033 | 676 |
(fn rews => map (CodegenFunc.rewrite_func rews)); |
20844 | 677 |
val apply_inline_proc_cterm = gen_apply_inline_proc single |
21708 | 678 |
(MetaSimplifier.rewrite false); |
20600 | 679 |
|
20844 | 680 |
fun apply_preproc thy f [] = [] |
681 |
| apply_preproc thy f (thms as (thm :: _)) = |
|
682 |
let |
|
683 |
val thms' = f thy thms; |
|
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
684 |
val thms'' as ((const, _) :: _) = map CodegenFunc.mk_head thms' |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
685 |
in (certify_const thy const o map CodegenFunc.mk_head) thms' end; |
20844 | 686 |
|
687 |
fun cmp_thms thy = |
|
22033 | 688 |
make_ord (fn (thm1, thm2) => not (Sign.typ_instance thy (CodegenFunc.typ_func thm1, CodegenFunc.typ_func thm2))); |
20844 | 689 |
|
690 |
fun rhs_conv conv thm = |
|
691 |
let |
|
692 |
val thm' = (conv o snd o Drule.dest_equals o Thm.cprop_of) thm; |
|
693 |
in Thm.transitive thm thm' end |
|
694 |
||
695 |
in |
|
20600 | 696 |
|
697 |
fun preprocess thy thms = |
|
20844 | 698 |
thms |
22050 | 699 |
|> fold (fn (_, (_, f)) => apply_preproc thy f) ((#preprocs o the_preproc o get_exec) thy) |
22033 | 700 |
|> map (CodegenFunc.rewrite_func ((#inlines o the_preproc o get_exec) thy)) |
22050 | 701 |
|> fold (fn (_, (_, f)) => apply_inline_proc thy f) ((#inline_procs o the_preproc o get_exec) thy) |
22197 | 702 |
(*FIXME - must check: rewrite rule, defining equation, proper constant |> map (snd o check_func false thy) *) |
20844 | 703 |
|> sort (cmp_thms thy) |
22033 | 704 |
|> common_typ_funcs; |
20600 | 705 |
|
22033 | 706 |
fun preprocess_cterm ct = |
707 |
let |
|
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
708 |
val thy = Thm.theory_of_cterm ct; |
22033 | 709 |
in |
710 |
ct |
|
22319 | 711 |
|> MetaSimplifier.rewrite false ((#inlines o the_preproc o get_exec) thy) |
22050 | 712 |
|> fold (fn (_, (_, f)) => rhs_conv (apply_inline_proc_cterm thy f)) |
22184
a125f38a559a
added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents:
22050
diff
changeset
|
713 |
((#inline_procs o the_preproc o get_exec) thy) |
22033 | 714 |
end; |
20844 | 715 |
|
716 |
end; (*local*) |
|
20600 | 717 |
|
22507 | 718 |
fun get_datatype thy tyco = |
719 |
case Symtab.lookup ((the_dtyps o get_exec) thy) tyco |
|
720 |
of SOME spec => spec |
|
721 |
| NONE => Sign.arity_number thy tyco |
|
722 |
|> Name.invents Name.context "'a" |
|
723 |
|> map (rpair []) |
|
724 |
|> rpair []; |
|
725 |
||
726 |
fun get_datatype_of_constr thy const = |
|
727 |
case CodegenConsts.co_of_const' thy const |
|
728 |
of SOME (tyco, (_, co)) => if member eq_co |
|
729 |
(Symtab.lookup (((the_dtyps o get_exec) thy)) tyco |
|
730 |
|> Option.map snd |
|
731 |
|> the_default []) co then SOME tyco else NONE |
|
732 |
| NONE => NONE; |
|
733 |
||
734 |
fun get_constr_typ thy const = |
|
735 |
case get_datatype_of_constr thy const |
|
736 |
of SOME tyco => let |
|
737 |
val (vs, cos) = get_datatype thy tyco; |
|
738 |
val (_, (_, (co, tys))) = CodegenConsts.co_of_const thy const |
|
739 |
in (tys ---> Type (tyco, map TFree vs)) |
|
740 |
|> map_atyps (fn TFree (v, _) => TFree (v, AList.lookup (op =) vs v |> the)) |
|
741 |
|> Logic.varifyT |
|
742 |
|> SOME end |
|
743 |
| NONE => NONE; |
|
744 |
||
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
745 |
fun default_typ_proto thy (const as (c, SOME tyco)) = classop_weakest_typ thy |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
746 |
((the o AxClass.class_of_param thy) c) (c, tyco) |> SOME |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
747 |
| default_typ_proto thy (const as (c, NONE)) = case AxClass.class_of_param thy c |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
748 |
of SOME class => SOME (Term.map_type_tvar |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
749 |
(K (TVar (("'a", 0), [class]))) (Sign.the_const_type thy c)) |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
750 |
| NONE => get_constr_typ thy const; |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
751 |
|
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
752 |
local |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
753 |
|
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
754 |
fun get_funcs thy const = |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
755 |
Consttab.lookup ((the_funcs o get_exec) thy) const |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
756 |
|> Option.map (Susp.force o fst) |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
757 |
|> these |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
758 |
|> map (Thm.transfer thy); |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
759 |
|
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
760 |
fun find_def thy (const as (c, _)) = |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
761 |
let |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
762 |
val specs = Defs.specifications_of (Theory.defs_of thy) c; |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
763 |
val ty = case try (default_typ_proto thy) const |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
764 |
of NONE => NONE |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
765 |
| SOME ty => ty; |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
766 |
val tys = Sign.const_typargs thy (c, ty |> the_default (Sign.the_const_type thy c)); |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
767 |
fun get_def (_, { is_def, name, lhs, rhs, thyname }) = |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
768 |
if is_def andalso forall (Sign.typ_instance thy) (tys ~~ lhs) then |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
769 |
try (Thm.get_axiom_i thy) name |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
770 |
else NONE |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
771 |
in get_first get_def specs end; |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
772 |
|
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
773 |
in |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
774 |
|
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
775 |
fun these_funcs thy const = |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
776 |
let |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
777 |
fun get_prim_def_funcs (const as (c, tys)) = |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
778 |
case find_def thy const |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
779 |
of SOME thm => |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
780 |
thm |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
781 |
|> Thm.transfer thy |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
782 |
|> gen_mk_func_typ false |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
783 |
|> map (CodegenFunc.expand_eta ~1 o snd) |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
784 |
| NONE => [] |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
785 |
fun drop_refl thy = filter_out (is_equal o Term.fast_term_ord o Logic.dest_equals |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
786 |
o ObjectLogic.drop_judgment thy o Drule.plain_prop_of); |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
787 |
val funcs = case get_funcs thy const |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
788 |
of [] => get_prim_def_funcs const |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
789 |
| funcs => funcs |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
790 |
in |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
791 |
funcs |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
792 |
|> preprocess thy |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
793 |
|> drop_refl thy |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
794 |
end; |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
795 |
|
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
796 |
fun default_typ thy (const as (c, _)) = case default_typ_proto thy const |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
797 |
of SOME ty => ty |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
798 |
| NONE => (case get_funcs thy const |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
799 |
of thm :: _ => CodegenFunc.typ_func thm |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
800 |
| [] => Sign.the_const_type thy c); |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
801 |
|
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
802 |
end; (*local*) |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
803 |
|
20600 | 804 |
|
805 |
(** code attributes **) |
|
806 |
||
807 |
local |
|
808 |
fun add_simple_attribute (name, f) = |
|
809 |
(Codegen.add_attribute name o (Scan.succeed o Thm.declaration_attribute)) |
|
20897 | 810 |
(fn th => Context.mapping (f th) I); |
20600 | 811 |
in |
812 |
val _ = map (Context.add_setup o add_simple_attribute) [ |
|
22484
25dfebd7b4c8
improved treatment of defining equations stemming from specification tools
haftmann
parents:
22423
diff
changeset
|
813 |
("func", add_func true), |
20600 | 814 |
("nofunc", del_func), |
815 |
("unfold", (fn thm => Codegen.add_unfold thm #> add_inline thm)), |
|
816 |
("inline", add_inline), |
|
817 |
("noinline", del_inline) |
|
818 |
] |
|
819 |
end; (*local*) |
|
820 |
||
22303 | 821 |
|
822 |
(** Isar setup **) |
|
823 |
||
824 |
local |
|
825 |
||
826 |
structure P = OuterParse |
|
827 |
and K = OuterKeyword |
|
828 |
||
829 |
val print_codesetupK = "print_codesetup"; |
|
22423 | 830 |
val code_datatypeK = "code_datatype"; |
22303 | 831 |
|
832 |
in |
|
833 |
||
834 |
val print_codesetupP = |
|
22423 | 835 |
OuterSyntax.improper_command print_codesetupK "print code generator setup of this theory" K.diag |
22303 | 836 |
(Scan.succeed |
837 |
(Toplevel.no_timing o Toplevel.unknown_theory o Toplevel.keep (CodeData.print o Toplevel.theory_of))); |
|
838 |
||
22423 | 839 |
val code_datatypeP = |
840 |
OuterSyntax.command code_datatypeK "define set of code datatype constructors" K.thy_decl ( |
|
841 |
Scan.repeat1 P.term |
|
842 |
>> (Toplevel.theory o add_datatype_consts_cmd) |
|
843 |
); |
|
844 |
||
845 |
||
846 |
val _ = OuterSyntax.add_parsers [print_codesetupP, code_datatypeP]; |
|
22303 | 847 |
|
848 |
end; (*local*) |
|
849 |
||
20600 | 850 |
end; (*struct*) |
851 |
||
852 |
||
853 |
||
854 |
(** type-safe interfaces for data depedent on executable content **) |
|
855 |
||
856 |
signature CODE_DATA_ARGS = |
|
857 |
sig |
|
858 |
val name: string |
|
859 |
type T |
|
860 |
val empty: T |
|
861 |
val merge: Pretty.pp -> T * T -> T |
|
20937 | 862 |
val purge: theory option -> CodegenConsts.const list option -> T -> T |
20600 | 863 |
end; |
864 |
||
865 |
signature CODE_DATA = |
|
866 |
sig |
|
867 |
type T |
|
868 |
val init: theory -> theory |
|
869 |
val get: theory -> T |
|
870 |
val change: theory -> (T -> T) -> T |
|
871 |
val change_yield: theory -> (T -> 'a * T) -> 'a * T |
|
872 |
end; |
|
873 |
||
874 |
functor CodeDataFun(Data: CODE_DATA_ARGS): CODE_DATA = |
|
875 |
struct |
|
876 |
||
877 |
type T = Data.T; |
|
878 |
exception Data of T; |
|
879 |
fun dest (Data x) = x |
|
880 |
||
881 |
val kind = CodegenData.declare Data.name (Data Data.empty) |
|
882 |
(fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2))) |
|
20937 | 883 |
(fn thy_opt => fn cs => fn Data x => Data (Data.purge thy_opt cs x)); |
20600 | 884 |
|
885 |
val init = CodegenData.init kind; |
|
22210 | 886 |
|
20600 | 887 |
fun get thy = CodegenData.get kind dest ((! o snd o CodegenData.CodeData.get) thy); |
22210 | 888 |
|
20600 | 889 |
fun change thy f = |
890 |
let |
|
891 |
val data_ref = (snd o CodegenData.CodeData.get) thy; |
|
892 |
val x = (f o CodegenData.get kind dest o !) data_ref; |
|
893 |
val data = CodegenData.put kind Data x (! data_ref); |
|
894 |
in (data_ref := data; x) end; |
|
22210 | 895 |
|
20600 | 896 |
fun change_yield thy f = |
897 |
let |
|
898 |
val data_ref = (snd o CodegenData.CodeData.get) thy; |
|
899 |
val (y, x) = (f o CodegenData.get kind dest o !) data_ref; |
|
900 |
val data = CodegenData.put kind Data x (! data_ref); |
|
901 |
in (data_ref := data; (y, x)) end; |
|
902 |
||
903 |
end; |
|
904 |
||
905 |
structure CodegenData : CODEGEN_DATA = |
|
906 |
struct |
|
907 |
||
908 |
open CodegenData; |
|
909 |
||
910 |
end; |