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