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