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