24219
|
1 |
(* Title: Tools/code/code_package.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Florian Haftmann, TU Muenchen
|
|
4 |
|
|
5 |
Code generator translation kernel. Code generator Isar setup.
|
|
6 |
*)
|
|
7 |
|
|
8 |
signature CODE_PACKAGE =
|
|
9 |
sig
|
|
10 |
(* interfaces *)
|
|
11 |
val eval_conv: theory
|
24283
|
12 |
-> (CodeThingol.code -> CodeThingol.iterm * CodeThingol.itype -> string list -> cterm -> thm)
|
|
13 |
-> cterm -> thm;
|
|
14 |
val eval_term: theory
|
|
15 |
-> (CodeThingol.code -> CodeThingol.iterm * CodeThingol.itype -> string list -> cterm -> 'a)
|
|
16 |
-> cterm -> 'a;
|
|
17 |
val satisfies_ref: bool option ref;
|
|
18 |
val satisfies: theory -> cterm -> string list -> bool;
|
24219
|
19 |
val codegen_command: theory -> string -> unit;
|
|
20 |
|
|
21 |
(* axiomatic interfaces *)
|
|
22 |
type appgen;
|
|
23 |
val add_appconst: string * appgen -> theory -> theory;
|
|
24 |
val appgen_let: appgen;
|
|
25 |
val appgen_if: appgen;
|
|
26 |
val appgen_case: (theory -> term
|
|
27 |
-> ((string * typ) list * ((term * typ) * (term * term) list)) option)
|
|
28 |
-> appgen;
|
|
29 |
|
|
30 |
val timing: bool ref;
|
|
31 |
end;
|
|
32 |
|
|
33 |
structure CodePackage : CODE_PACKAGE =
|
|
34 |
struct
|
|
35 |
|
|
36 |
open BasicCodeThingol;
|
|
37 |
|
|
38 |
(** code translation **)
|
|
39 |
|
|
40 |
(* theory data *)
|
|
41 |
|
|
42 |
type appgen = theory -> ((sort -> sort) * Sorts.algebra) * Consts.T
|
|
43 |
-> CodeFuncgr.T
|
|
44 |
-> (string * typ) * term list -> CodeThingol.transact -> iterm * CodeThingol.transact;
|
|
45 |
|
|
46 |
type appgens = (int * (appgen * stamp)) Symtab.table;
|
|
47 |
val merge_appgens : appgens * appgens -> appgens =
|
|
48 |
Symtab.merge (fn ((bounds1, (_, stamp1)), (bounds2, (_, stamp2))) =>
|
|
49 |
bounds1 = bounds2 andalso stamp1 = stamp2);
|
|
50 |
|
|
51 |
structure Consttab = CodeUnit.Consttab;
|
|
52 |
type abstypes = typ Symtab.table * CodeUnit.const Consttab.table;
|
|
53 |
fun merge_abstypes ((typs1, consts1) : abstypes, (typs2, consts2) : abstypes) =
|
|
54 |
(Symtab.merge (Type.eq_type Vartab.empty) (typs1, typs2),
|
|
55 |
Consttab.merge CodeUnit.eq_const (consts1, consts2));
|
|
56 |
|
|
57 |
structure Translation = TheoryDataFun
|
|
58 |
(
|
|
59 |
type T = appgens * abstypes;
|
|
60 |
val empty = (Symtab.empty, (Symtab.empty, Consttab.empty));
|
|
61 |
val copy = I;
|
|
62 |
val extend = I;
|
|
63 |
fun merge _ ((appgens1, abstypes1), (appgens2, abstypes2)) =
|
|
64 |
(merge_appgens (appgens1, appgens2), merge_abstypes (abstypes1, abstypes2));
|
|
65 |
);
|
|
66 |
|
24283
|
67 |
fun code_depgr thy [] = CodeFuncgr.make thy []
|
24219
|
68 |
| code_depgr thy consts =
|
|
69 |
let
|
24283
|
70 |
val gr = CodeFuncgr.make thy consts;
|
24219
|
71 |
val select = CodeFuncgr.Constgraph.all_succs gr consts;
|
|
72 |
in
|
|
73 |
CodeFuncgr.Constgraph.subgraph
|
|
74 |
(member CodeUnit.eq_const select) gr
|
|
75 |
end;
|
|
76 |
|
|
77 |
fun code_thms thy =
|
|
78 |
Pretty.writeln o CodeFuncgr.pretty thy o code_depgr thy;
|
|
79 |
|
|
80 |
fun code_deps thy consts =
|
|
81 |
let
|
|
82 |
val gr = code_depgr thy consts;
|
|
83 |
fun mk_entry (const, (_, (_, parents))) =
|
|
84 |
let
|
|
85 |
val name = CodeUnit.string_of_const thy const;
|
|
86 |
val nameparents = map (CodeUnit.string_of_const thy) parents;
|
|
87 |
in { name = name, ID = name, dir = "", unfold = true,
|
|
88 |
path = "", parents = nameparents }
|
|
89 |
end;
|
|
90 |
val prgr = CodeFuncgr.Constgraph.fold ((fn x => fn xs => xs @ [x]) o mk_entry) gr [];
|
|
91 |
in Present.display_graph prgr end;
|
|
92 |
|
|
93 |
structure Program = CodeDataFun
|
|
94 |
(
|
|
95 |
type T = CodeThingol.code;
|
|
96 |
val empty = CodeThingol.empty_code;
|
|
97 |
fun merge _ = CodeThingol.merge_code;
|
|
98 |
fun purge _ NONE _ = CodeThingol.empty_code
|
|
99 |
| purge NONE _ _ = CodeThingol.empty_code
|
|
100 |
| purge (SOME thy) (SOME cs) code =
|
|
101 |
let
|
|
102 |
val cs_exisiting =
|
|
103 |
map_filter (CodeName.const_rev thy) (Graph.keys code);
|
|
104 |
val dels = (Graph.all_preds code
|
|
105 |
o map (CodeName.const thy)
|
|
106 |
o filter (member CodeUnit.eq_const cs_exisiting)
|
|
107 |
) cs;
|
|
108 |
in Graph.del_nodes dels code end;
|
|
109 |
);
|
|
110 |
|
|
111 |
|
|
112 |
(* translation kernel *)
|
|
113 |
|
|
114 |
fun get_abstype thy (tyco, tys) = case Symtab.lookup ((fst o snd o Translation.get) thy) tyco
|
|
115 |
of SOME ty => SOME ((map_atyps (fn TFree (n, _) => nth tys (the (Int.fromString n)))) ty)
|
|
116 |
| NONE => NONE;
|
|
117 |
|
24283
|
118 |
val value_name = "Isabelle_Eval.EVAL.EVAL";
|
|
119 |
|
|
120 |
fun ensure_def thy = CodeThingol.ensure_def
|
|
121 |
(fn s => if s = value_name then "<term>" else CodeName.labelled_name thy s);
|
24219
|
122 |
|
|
123 |
fun ensure_def_class thy (algbr as ((_, algebra), _)) funcgr class =
|
|
124 |
let
|
|
125 |
val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class;
|
|
126 |
val (v, cs) = AxClass.params_of_class thy class;
|
|
127 |
val class' = CodeName.class thy class;
|
|
128 |
val classrels' = map (curry (CodeName.classrel thy) class) superclasses;
|
|
129 |
val classops' = map (CodeName.const thy o CodeUnit.const_of_cexpr thy) cs;
|
|
130 |
val defgen_class =
|
|
131 |
fold_map (ensure_def_class thy algbr funcgr) superclasses
|
|
132 |
##>> (fold_map (exprgen_typ thy algbr funcgr) o map snd) cs
|
24250
|
133 |
#>> (fn (superclasses, classoptyps) =>
|
|
134 |
CodeThingol.Class (superclasses ~~ classrels', (unprefix "'" v, classops' ~~ classoptyps)))
|
24219
|
135 |
in
|
|
136 |
ensure_def thy defgen_class ("generating class " ^ quote class) class'
|
|
137 |
#> pair class'
|
|
138 |
end
|
|
139 |
and ensure_def_classrel thy algbr funcgr (subclass, superclass) =
|
|
140 |
ensure_def_class thy algbr funcgr subclass
|
|
141 |
#>> (fn _ => CodeName.classrel thy (subclass, superclass))
|
24250
|
142 |
and ensure_def_tyco thy algbr funcgr "fun" =
|
|
143 |
pair "fun"
|
|
144 |
| ensure_def_tyco thy algbr funcgr tyco =
|
24219
|
145 |
let
|
24250
|
146 |
val defgen_datatype =
|
24219
|
147 |
let
|
|
148 |
val (vs, cos) = Code.get_datatype thy tyco;
|
|
149 |
in
|
24250
|
150 |
fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
|
|
151 |
##>> fold_map (fn (c, tys) =>
|
24219
|
152 |
fold_map (exprgen_typ thy algbr funcgr) tys
|
24250
|
153 |
#>> (fn tys' =>
|
|
154 |
((CodeName.const thy o CodeUnit.const_of_cexpr thy)
|
24219
|
155 |
(c, tys ---> Type (tyco, map TFree vs)), tys'))) cos
|
24250
|
156 |
#>> (fn (vs, cos) => CodeThingol.Datatype (vs, cos))
|
24219
|
157 |
end;
|
|
158 |
val tyco' = CodeName.tyco thy tyco;
|
|
159 |
in
|
24250
|
160 |
ensure_def thy defgen_datatype ("generating type constructor " ^ quote tyco) tyco'
|
|
161 |
#> pair tyco'
|
24219
|
162 |
end
|
|
163 |
and exprgen_tyvar_sort thy (algbr as ((proj_sort, _), _)) funcgr (v, sort) trns =
|
|
164 |
trns
|
|
165 |
|> fold_map (ensure_def_class thy algbr funcgr) (proj_sort sort)
|
|
166 |
|>> (fn sort => (unprefix "'" v, sort))
|
|
167 |
and exprgen_typ thy algbr funcgr (TFree vs) trns =
|
|
168 |
trns
|
|
169 |
|> exprgen_tyvar_sort thy algbr funcgr vs
|
|
170 |
|>> (fn (v, sort) => ITyVar v)
|
|
171 |
| exprgen_typ thy algbr funcgr (Type (tyco, tys)) trns =
|
|
172 |
case get_abstype thy (tyco, tys)
|
|
173 |
of SOME ty =>
|
|
174 |
trns
|
|
175 |
|> exprgen_typ thy algbr funcgr ty
|
|
176 |
| NONE =>
|
|
177 |
trns
|
|
178 |
|> ensure_def_tyco thy algbr funcgr tyco
|
|
179 |
||>> fold_map (exprgen_typ thy algbr funcgr) tys
|
|
180 |
|>> (fn (tyco, tys) => tyco `%% tys);
|
|
181 |
|
|
182 |
exception CONSTRAIN of (string * typ) * typ;
|
|
183 |
val timing = ref false;
|
|
184 |
|
|
185 |
fun exprgen_dicts thy (algbr as ((proj_sort, algebra), consts)) funcgr (ty_ctxt, sort_decl) =
|
|
186 |
let
|
|
187 |
val pp = Sign.pp thy;
|
|
188 |
datatype typarg =
|
|
189 |
Global of (class * string) * typarg list list
|
|
190 |
| Local of (class * class) list * (string * (int * sort));
|
|
191 |
fun class_relation (Global ((_, tyco), yss), _) class =
|
|
192 |
Global ((class, tyco), yss)
|
|
193 |
| class_relation (Local (classrels, v), subclass) superclass =
|
|
194 |
Local ((subclass, superclass) :: classrels, v);
|
|
195 |
fun type_constructor tyco yss class =
|
|
196 |
Global ((class, tyco), (map o map) fst yss);
|
|
197 |
fun type_variable (TFree (v, sort)) =
|
|
198 |
let
|
|
199 |
val sort' = proj_sort sort;
|
|
200 |
in map_index (fn (n, class) => (Local ([], (v, (n, sort'))), class)) sort' end;
|
|
201 |
val typargs = Sorts.of_sort_derivation pp algebra
|
|
202 |
{class_relation = class_relation, type_constructor = type_constructor,
|
|
203 |
type_variable = type_variable}
|
|
204 |
(ty_ctxt, proj_sort sort_decl);
|
|
205 |
fun mk_dict (Global (inst, yss)) =
|
|
206 |
ensure_def_inst thy algbr funcgr inst
|
|
207 |
##>> (fold_map o fold_map) mk_dict yss
|
|
208 |
#>> (fn (inst, dss) => DictConst (inst, dss))
|
|
209 |
| mk_dict (Local (classrels, (v, (k, sort)))) =
|
|
210 |
fold_map (ensure_def_classrel thy algbr funcgr) classrels
|
|
211 |
#>> (fn classrels => DictVar (classrels, (unprefix "'" v, (k, length sort))))
|
|
212 |
in
|
|
213 |
fold_map mk_dict typargs
|
|
214 |
end
|
|
215 |
and exprgen_dict_parms thy (algbr as (_, consts)) funcgr (c, ty_ctxt) =
|
|
216 |
let
|
|
217 |
val c' = CodeUnit.const_of_cexpr thy (c, ty_ctxt)
|
|
218 |
val idf = CodeName.const thy c';
|
|
219 |
val ty_decl = Consts.the_declaration consts idf;
|
|
220 |
val (tys, tys_decl) = pairself (curry (Consts.typargs consts) idf) (ty_ctxt, ty_decl);
|
|
221 |
val sorts = map (snd o dest_TVar) tys_decl;
|
|
222 |
in
|
|
223 |
fold_map (exprgen_dicts thy algbr funcgr) (tys ~~ sorts)
|
|
224 |
end
|
|
225 |
and ensure_def_inst thy (algbr as ((_, algebra), _)) funcgr (class, tyco) trns =
|
|
226 |
let
|
|
227 |
val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class;
|
|
228 |
val (var, classops) = try (AxClass.params_of_class thy) class |> the_default ("'a", [])
|
|
229 |
val vs = Name.names (Name.declare var Name.context) "'a" (Sorts.mg_domain algebra tyco [class]);
|
|
230 |
val arity_typ = Type (tyco, map TFree vs);
|
|
231 |
fun gen_superarity superclass trns =
|
|
232 |
trns
|
|
233 |
|> ensure_def_class thy algbr funcgr superclass
|
|
234 |
||>> ensure_def_classrel thy algbr funcgr (class, superclass)
|
|
235 |
||>> exprgen_dicts thy algbr funcgr (arity_typ, [superclass])
|
|
236 |
|>> (fn ((superclass, classrel), [DictConst (inst, dss)]) =>
|
|
237 |
(superclass, (classrel, (inst, dss))));
|
|
238 |
fun gen_classop_def (classop as (c, ty)) trns =
|
|
239 |
trns
|
|
240 |
|> ensure_def_const thy algbr funcgr (CodeUnit.const_of_cexpr thy classop)
|
|
241 |
||>> exprgen_term thy algbr funcgr (Const (c, map_type_tfree (K arity_typ) ty));
|
|
242 |
fun defgen_inst trns =
|
|
243 |
trns
|
|
244 |
|> ensure_def_class thy algbr funcgr class
|
|
245 |
||>> ensure_def_tyco thy algbr funcgr tyco
|
|
246 |
||>> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
|
|
247 |
||>> fold_map gen_superarity superclasses
|
|
248 |
||>> fold_map gen_classop_def classops
|
24250
|
249 |
|>> (fn ((((class, tyco), arity), superarities), classops) =>
|
|
250 |
CodeThingol.Classinst ((class, (tyco, arity)), (superarities, classops)));
|
24219
|
251 |
val inst = CodeName.instance thy (class, tyco);
|
|
252 |
in
|
|
253 |
trns
|
|
254 |
|> ensure_def thy defgen_inst ("generating instance " ^ quote class ^ " / " ^ quote tyco) inst
|
|
255 |
|> pair inst
|
|
256 |
end
|
24250
|
257 |
and ensure_def_const thy (algbr as (_, consts)) funcgr (const as (c, opt_tyco)) =
|
24219
|
258 |
let
|
|
259 |
val c' = CodeName.const thy const;
|
|
260 |
fun defgen_datatypecons trns =
|
24250
|
261 |
trns
|
24219
|
262 |
|> ensure_def_tyco thy algbr funcgr
|
|
263 |
((the o Code.get_datatype_of_constr thy) const)
|
24250
|
264 |
|>> (fn _ => CodeThingol.Bot);
|
24219
|
265 |
fun defgen_classop trns =
|
24250
|
266 |
trns
|
|
267 |
|> ensure_def_class thy algbr funcgr
|
|
268 |
((the o AxClass.class_of_param thy) c)
|
|
269 |
|>> (fn _ => CodeThingol.Bot);
|
24219
|
270 |
fun defgen_fun trns =
|
|
271 |
let
|
|
272 |
val const' = perhaps (Consttab.lookup ((snd o snd o Translation.get) thy)) const;
|
|
273 |
val raw_thms = CodeFuncgr.funcs funcgr const';
|
|
274 |
val ty = (Logic.unvarifyT o CodeFuncgr.typ funcgr) const';
|
|
275 |
val thms = if (null o Term.typ_tfrees) ty orelse (null o fst o strip_type) ty
|
|
276 |
then raw_thms
|
|
277 |
else map (CodeUnit.expand_eta 1) raw_thms;
|
|
278 |
val timeap = if !timing then Output.timeap_msg ("time for " ^ c')
|
|
279 |
else I;
|
|
280 |
val vs = (map dest_TFree o Consts.typargs consts) (c', ty);
|
|
281 |
val dest_eqthm =
|
|
282 |
apfst (snd o strip_comb) o Logic.dest_equals o Logic.unvarify o prop_of;
|
24250
|
283 |
fun exprgen_eq (args, rhs) =
|
|
284 |
fold_map (exprgen_term thy algbr funcgr) args
|
|
285 |
##>> exprgen_term thy algbr funcgr rhs;
|
24219
|
286 |
in
|
|
287 |
trns
|
|
288 |
|> timeap (fold_map (exprgen_eq o dest_eqthm) thms)
|
|
289 |
||>> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
|
|
290 |
||>> exprgen_typ thy algbr funcgr ty
|
24250
|
291 |
|>> (fn ((eqs, vs), ty) => CodeThingol.Fun (eqs, (vs, ty)))
|
24219
|
292 |
end;
|
|
293 |
val defgen = if (is_some o Code.get_datatype_of_constr thy) const
|
|
294 |
then defgen_datatypecons
|
|
295 |
else if is_some opt_tyco
|
|
296 |
orelse (not o is_some o AxClass.class_of_param thy) c
|
|
297 |
then defgen_fun
|
|
298 |
else defgen_classop
|
|
299 |
in
|
24250
|
300 |
ensure_def thy defgen ("generating constant " ^ CodeUnit.string_of_const thy const) c'
|
|
301 |
#> pair c'
|
24219
|
302 |
end
|
|
303 |
and exprgen_term thy algbr funcgr (Const (c, ty)) trns =
|
|
304 |
trns
|
|
305 |
|> select_appgen thy algbr funcgr ((c, ty), [])
|
|
306 |
| exprgen_term thy algbr funcgr (Free (v, ty)) trns =
|
|
307 |
trns
|
|
308 |
|> exprgen_typ thy algbr funcgr ty
|
|
309 |
|>> (fn _ => IVar v)
|
|
310 |
| exprgen_term thy algbr funcgr (Abs (raw_v, ty, raw_t)) trns =
|
|
311 |
let
|
|
312 |
val (v, t) = Syntax.variant_abs (raw_v, ty, raw_t);
|
|
313 |
in
|
|
314 |
trns
|
|
315 |
|> exprgen_typ thy algbr funcgr ty
|
|
316 |
||>> exprgen_term thy algbr funcgr t
|
|
317 |
|>> (fn (ty, t) => (v, ty) `|-> t)
|
|
318 |
end
|
|
319 |
| exprgen_term thy algbr funcgr (t as _ $ _) trns =
|
|
320 |
case strip_comb t
|
|
321 |
of (Const (c, ty), ts) =>
|
|
322 |
trns
|
|
323 |
|> select_appgen thy algbr funcgr ((c, ty), ts)
|
|
324 |
| (t', ts) =>
|
|
325 |
trns
|
|
326 |
|> exprgen_term thy algbr funcgr t'
|
|
327 |
||>> fold_map (exprgen_term thy algbr funcgr) ts
|
|
328 |
|>> (fn (t, ts) => t `$$ ts)
|
|
329 |
and appgen_default thy algbr funcgr ((c, ty), ts) trns =
|
|
330 |
trns
|
|
331 |
|> ensure_def_const thy algbr funcgr (CodeUnit.const_of_cexpr thy (c, ty))
|
|
332 |
||>> fold_map (exprgen_typ thy algbr funcgr) ((fst o Term.strip_type) ty)
|
|
333 |
||>> exprgen_typ thy algbr funcgr ((snd o Term.strip_type) ty)
|
|
334 |
||>> exprgen_dict_parms thy algbr funcgr (c, ty)
|
|
335 |
||>> fold_map (exprgen_term thy algbr funcgr) ts
|
|
336 |
|>> (fn ((((c, tys), ty), iss), ts) => IConst (c, (iss, tys)) `$$ ts)
|
|
337 |
and select_appgen thy algbr funcgr ((c, ty), ts) trns =
|
|
338 |
case Symtab.lookup (fst (Translation.get thy)) c
|
|
339 |
of SOME (i, (appgen, _)) =>
|
|
340 |
if length ts < i then
|
|
341 |
let
|
|
342 |
val k = length ts;
|
|
343 |
val tys = (curry Library.take (i - k) o curry Library.drop k o fst o strip_type) ty;
|
|
344 |
val ctxt = (fold o fold_aterms)
|
|
345 |
(fn Free (v, _) => Name.declare v | _ => I) ts Name.context;
|
|
346 |
val vs = Name.names ctxt "a" tys;
|
|
347 |
in
|
|
348 |
trns
|
|
349 |
|> fold_map (exprgen_typ thy algbr funcgr) tys
|
|
350 |
||>> appgen thy algbr funcgr ((c, ty), ts @ map Free vs)
|
|
351 |
|>> (fn (tys, t) => map2 (fn (v, _) => pair v) vs tys `|--> t)
|
|
352 |
end
|
|
353 |
else if length ts > i then
|
|
354 |
trns
|
|
355 |
|> appgen thy algbr funcgr ((c, ty), Library.take (i, ts))
|
|
356 |
||>> fold_map (exprgen_term thy algbr funcgr) (Library.drop (i, ts))
|
|
357 |
|>> (fn (t, ts) => t `$$ ts)
|
|
358 |
else
|
|
359 |
trns
|
|
360 |
|> appgen thy algbr funcgr ((c, ty), ts)
|
|
361 |
| NONE =>
|
|
362 |
trns
|
|
363 |
|> appgen_default thy algbr funcgr ((c, ty), ts);
|
|
364 |
|
|
365 |
|
|
366 |
(* entrance points into translation kernel *)
|
|
367 |
|
|
368 |
fun ensure_def_const' thy algbr funcgr c trns =
|
|
369 |
ensure_def_const thy algbr funcgr c trns
|
|
370 |
handle CONSTRAIN ((c, ty), ty_decl) => error (
|
|
371 |
"Constant " ^ c ^ " with most general type\n"
|
|
372 |
^ CodeUnit.string_of_typ thy ty
|
|
373 |
^ "\noccurs with type\n"
|
|
374 |
^ CodeUnit.string_of_typ thy ty_decl);
|
|
375 |
|
|
376 |
fun perhaps_def_const thy algbr funcgr c trns =
|
|
377 |
case try (ensure_def_const thy algbr funcgr c) trns
|
|
378 |
of SOME (c, trns) => (SOME c, trns)
|
|
379 |
| NONE => (NONE, trns);
|
|
380 |
|
|
381 |
fun exprgen_term' thy algbr funcgr t trns =
|
|
382 |
exprgen_term thy algbr funcgr t trns
|
|
383 |
handle CONSTRAIN ((c, ty), ty_decl) => error ("In term " ^ (quote o Sign.string_of_term thy) t
|
|
384 |
^ ",\nconstant " ^ c ^ " with most general type\n"
|
|
385 |
^ CodeUnit.string_of_typ thy ty
|
|
386 |
^ "\noccurs with type\n"
|
|
387 |
^ CodeUnit.string_of_typ thy ty_decl);
|
|
388 |
|
|
389 |
|
|
390 |
(* parametrized application generators, for instantiation in object logic *)
|
|
391 |
(* (axiomatic extensions of translation kernel) *)
|
|
392 |
|
|
393 |
fun appgen_case dest_case_expr thy algbr funcgr (app as (c_ty, ts)) =
|
|
394 |
let
|
|
395 |
val SOME ([], ((st, sty), ds)) = dest_case_expr thy (list_comb (Const c_ty, ts));
|
|
396 |
fun clause_gen (dt, bt) =
|
|
397 |
exprgen_term thy algbr funcgr dt
|
|
398 |
##>> exprgen_term thy algbr funcgr bt;
|
|
399 |
in
|
|
400 |
exprgen_term thy algbr funcgr st
|
|
401 |
##>> exprgen_typ thy algbr funcgr sty
|
|
402 |
##>> fold_map clause_gen ds
|
|
403 |
##>> appgen_default thy algbr funcgr app
|
|
404 |
#>> (fn (((se, sty), ds), t0) => ICase (((se, sty), ds), t0))
|
|
405 |
end;
|
|
406 |
|
|
407 |
fun appgen_let thy algbr funcgr (app as (_, [st, ct])) =
|
|
408 |
exprgen_term thy algbr funcgr ct
|
|
409 |
##>> exprgen_term thy algbr funcgr st
|
|
410 |
##>> appgen_default thy algbr funcgr app
|
|
411 |
#>> (fn (((v, ty) `|-> be, se), t0) =>
|
|
412 |
ICase (CodeThingol.collapse_let (((v, ty), se), be), t0)
|
|
413 |
| (_, t0) => t0);
|
|
414 |
|
|
415 |
fun appgen_if thy algbr funcgr (app as (_, [tb, tt, tf])) =
|
|
416 |
exprgen_term thy algbr funcgr tb
|
|
417 |
##>> exprgen_typ thy algbr funcgr (Type ("bool", []))
|
|
418 |
##>> exprgen_term thy algbr funcgr (Const ("True", Type ("bool", [])))
|
|
419 |
##>> exprgen_term thy algbr funcgr tt
|
|
420 |
##>> exprgen_term thy algbr funcgr (Const ("False", Type ("bool", [])))
|
|
421 |
##>> exprgen_term thy algbr funcgr tf
|
|
422 |
##>> appgen_default thy algbr funcgr app
|
|
423 |
#>> (fn ((((((tb, B), T), tt), F), tf), t0) => ICase (((tb, B), [(T, tt), (F, tf)]), t0));
|
|
424 |
|
|
425 |
fun add_appconst (c, appgen) thy =
|
|
426 |
let
|
|
427 |
val i = (length o fst o strip_type o Sign.the_const_type thy) c;
|
|
428 |
val _ = Program.change thy (K CodeThingol.empty_code);
|
|
429 |
in
|
|
430 |
(Translation.map o apfst)
|
|
431 |
(Symtab.update (c, (i, (appgen, stamp ())))) thy
|
|
432 |
end;
|
|
433 |
|
|
434 |
|
|
435 |
|
|
436 |
(** abstype and constsubst interface **)
|
|
437 |
|
|
438 |
local
|
|
439 |
|
|
440 |
fun add_consts thy f (c1, c2 as (c, opt_tyco)) =
|
|
441 |
let
|
|
442 |
val _ = if
|
|
443 |
is_some (AxClass.class_of_param thy c) andalso is_none opt_tyco
|
|
444 |
orelse is_some (Code.get_datatype_of_constr thy c2)
|
|
445 |
then error ("Not a function: " ^ CodeUnit.string_of_const thy c2)
|
|
446 |
else ();
|
24283
|
447 |
val funcgr = CodeFuncgr.make thy [c1, c2];
|
24219
|
448 |
val ty1 = (f o CodeFuncgr.typ funcgr) c1;
|
|
449 |
val ty2 = CodeFuncgr.typ funcgr c2;
|
|
450 |
val _ = if Sign.typ_equiv thy (ty1, ty2) then () else
|
|
451 |
error ("Incompatiable type signatures of " ^ CodeUnit.string_of_const thy c1
|
|
452 |
^ " and " ^ CodeUnit.string_of_const thy c2 ^ ":\n"
|
|
453 |
^ CodeUnit.string_of_typ thy ty1 ^ "\n" ^ CodeUnit.string_of_typ thy ty2);
|
|
454 |
in Consttab.update (c1, c2) end;
|
|
455 |
|
|
456 |
fun gen_abstyp prep_const prep_typ (raw_abstyp, raw_substtyp) raw_absconsts thy =
|
|
457 |
let
|
|
458 |
val abstyp = Type.no_tvars (prep_typ thy raw_abstyp);
|
|
459 |
val substtyp = Type.no_tvars (prep_typ thy raw_substtyp);
|
|
460 |
val absconsts = (map o pairself) (prep_const thy) raw_absconsts;
|
|
461 |
val Type (abstyco, tys) = abstyp handle BIND => error ("Bad type: " ^ Sign.string_of_typ thy abstyp);
|
|
462 |
val typarms = map (fst o dest_TFree) tys handle MATCH => error ("Bad type: " ^ Sign.string_of_typ thy abstyp);
|
|
463 |
fun mk_index v =
|
|
464 |
let
|
|
465 |
val k = find_index (fn w => v = w) typarms;
|
|
466 |
in if k = ~1
|
|
467 |
then error ("Free type variable: " ^ quote v)
|
|
468 |
else TFree (string_of_int k, [])
|
|
469 |
end;
|
|
470 |
val typpat = map_atyps (fn TFree (v, _) => mk_index v) substtyp;
|
|
471 |
fun apply_typpat (Type (tyco, tys)) =
|
|
472 |
let
|
|
473 |
val tys' = map apply_typpat tys;
|
|
474 |
in if tyco = abstyco then
|
|
475 |
(map_atyps (fn TFree (n, _) => nth tys' (the (Int.fromString n)))) typpat
|
|
476 |
else
|
|
477 |
Type (tyco, tys')
|
|
478 |
end
|
|
479 |
| apply_typpat ty = ty;
|
|
480 |
val _ = Program.change thy (K CodeThingol.empty_code);
|
|
481 |
in
|
|
482 |
thy
|
|
483 |
|> (Translation.map o apsnd) (fn (abstypes, abscs) =>
|
|
484 |
(abstypes
|
|
485 |
|> Symtab.update (abstyco, typpat),
|
|
486 |
abscs
|
|
487 |
|> fold (add_consts thy apply_typpat) absconsts)
|
|
488 |
)
|
|
489 |
end;
|
|
490 |
|
|
491 |
fun gen_constsubst prep_const raw_constsubsts thy =
|
|
492 |
let
|
|
493 |
val constsubsts = (map o pairself) (prep_const thy) raw_constsubsts;
|
|
494 |
val _ = Program.change thy (K CodeThingol.empty_code);
|
|
495 |
in
|
|
496 |
thy
|
|
497 |
|> (Translation.map o apsnd o apsnd) (fold (add_consts thy I) constsubsts)
|
|
498 |
end;
|
|
499 |
|
|
500 |
in
|
|
501 |
|
|
502 |
val abstyp = gen_abstyp (K I) Sign.certify_typ;
|
|
503 |
val abstyp_e = gen_abstyp CodeUnit.read_const Sign.read_typ;
|
|
504 |
|
|
505 |
val constsubst = gen_constsubst (K I);
|
|
506 |
val constsubst_e = gen_constsubst CodeUnit.read_const;
|
|
507 |
|
|
508 |
end; (*local*)
|
|
509 |
|
|
510 |
|
|
511 |
(** code generation interfaces **)
|
|
512 |
|
|
513 |
(* generic generation combinators *)
|
|
514 |
|
|
515 |
fun generate thy funcgr gen it =
|
|
516 |
let
|
|
517 |
(*FIXME*)
|
24283
|
518 |
val _ = CodeFuncgr.intervene thy funcgr;
|
24219
|
519 |
val cs = map_filter (Consttab.lookup ((snd o snd o Translation.get) thy))
|
|
520 |
(CodeFuncgr.all funcgr);
|
24283
|
521 |
val CodeFuncgr' = CodeFuncgr.make thy cs;
|
24219
|
522 |
val naming = NameSpace.qualified_names NameSpace.default_naming;
|
|
523 |
val consttab = Consts.empty
|
|
524 |
|> fold (fn c => Consts.declare naming
|
24283
|
525 |
((CodeName.const thy c, CodeFuncgr.typ CodeFuncgr' c), true))
|
|
526 |
(CodeFuncgr.all CodeFuncgr');
|
24219
|
527 |
val algbr = (Code.operational_algebra thy, consttab);
|
|
528 |
in
|
|
529 |
Program.change_yield thy
|
24283
|
530 |
(CodeThingol.start_transact (gen thy algbr CodeFuncgr' it))
|
|
531 |
|> fst
|
24219
|
532 |
end;
|
|
533 |
|
24283
|
534 |
fun raw_eval f thy g =
|
24250
|
535 |
let
|
|
536 |
val value_name = "Isabelle_Eval.EVAL.EVAL";
|
|
537 |
fun ensure_eval thy algbr funcgr t =
|
|
538 |
let
|
|
539 |
val defgen_eval =
|
|
540 |
exprgen_term' thy algbr funcgr t
|
|
541 |
##>> exprgen_typ thy algbr funcgr (fastype_of t)
|
|
542 |
#>> (fn (t, ty) => CodeThingol.Fun ([([], t)], ([], ty)));
|
24283
|
543 |
fun result (dep, code) =
|
|
544 |
let
|
|
545 |
val CodeThingol.Fun ([([], t)], ([], ty)) = Graph.get_node code value_name;
|
|
546 |
val deps = Graph.imm_succs code value_name;
|
|
547 |
val code' = Graph.del_nodes [value_name] code;
|
|
548 |
val code'' = CodeThingol.project_code false [] (SOME deps) code';
|
|
549 |
in ((code'', (t, ty), deps), (dep, code')) end;
|
24250
|
550 |
in
|
|
551 |
ensure_def thy defgen_eval "evaluation" value_name
|
24283
|
552 |
#> result
|
24250
|
553 |
end;
|
24283
|
554 |
fun h funcgr ct =
|
24219
|
555 |
let
|
24283
|
556 |
val (code, (t, ty), deps) = generate thy funcgr ensure_eval (Thm.term_of ct);
|
|
557 |
in g code (t, ty) deps ct end;
|
|
558 |
in f thy h end;
|
24219
|
559 |
|
24283
|
560 |
fun eval_conv thy = raw_eval CodeFuncgr.eval_conv thy;
|
|
561 |
fun eval_term thy = raw_eval CodeFuncgr.eval_term thy;
|
24219
|
562 |
|
|
563 |
val satisfies_ref : bool option ref = ref NONE;
|
|
564 |
|
24283
|
565 |
fun satisfies thy ct witnesses =
|
|
566 |
let
|
|
567 |
fun evl code (t, ty) deps ct =
|
|
568 |
let
|
|
569 |
val t0 = Thm.term_of ct
|
|
570 |
val _ = (Term.map_types o Term.map_atyps) (fn _ =>
|
|
571 |
error ("Term " ^ Sign.string_of_term thy t0 ^ " contains polymorphic type"))
|
|
572 |
t0;
|
|
573 |
in
|
|
574 |
CodeTarget.eval_term thy ("CodePackage.satisfies_ref", satisfies_ref)
|
|
575 |
code (t, ty) witnesses
|
|
576 |
end;
|
|
577 |
in eval_term thy evl ct end;
|
24219
|
578 |
|
|
579 |
fun filter_generatable thy consts =
|
|
580 |
let
|
24283
|
581 |
val (consts', funcgr) = CodeFuncgr.make_consts thy consts;
|
|
582 |
val consts'' = generate thy funcgr (fold_map ooo perhaps_def_const) consts';
|
24219
|
583 |
val consts''' = map_filter (fn (const, SOME _) => SOME const | (_, NONE) => NONE)
|
|
584 |
(consts' ~~ consts'');
|
|
585 |
in consts''' end;
|
|
586 |
|
|
587 |
|
|
588 |
(** toplevel interface and setup **)
|
|
589 |
|
|
590 |
local
|
|
591 |
|
|
592 |
structure P = OuterParse
|
|
593 |
and K = OuterKeyword
|
|
594 |
|
|
595 |
fun code raw_cs seris thy =
|
|
596 |
let
|
|
597 |
val (perm1, cs) = CodeUnit.read_const_exprs thy
|
|
598 |
(filter_generatable thy) raw_cs;
|
24283
|
599 |
val (perm2, cs') = case generate thy (CodeFuncgr.make thy cs) (fold_map ooo ensure_def_const') cs
|
|
600 |
of [] => (true, NONE)
|
|
601 |
| cs => (false, SOME cs);
|
24219
|
602 |
val code = Program.get thy;
|
|
603 |
val seris' = map (fn (((target, module), file), args) =>
|
|
604 |
CodeTarget.get_serializer thy target (perm1 orelse perm2) module file args
|
|
605 |
CodeName.labelled_name cs') seris;
|
|
606 |
in
|
|
607 |
(map (fn f => f code) seris' : unit list; ())
|
|
608 |
end;
|
|
609 |
|
|
610 |
fun code_thms_cmd thy =
|
24283
|
611 |
code_thms thy o snd o CodeUnit.read_const_exprs thy (fst o CodeFuncgr.make_consts thy);
|
24219
|
612 |
|
|
613 |
fun code_deps_cmd thy =
|
24283
|
614 |
code_deps thy o snd o CodeUnit.read_const_exprs thy (fst o CodeFuncgr.make_consts thy);
|
24219
|
615 |
|
24250
|
616 |
val (inK, module_nameK, fileK) = ("in", "module_name", "file");
|
24219
|
617 |
|
|
618 |
val code_exprP =
|
|
619 |
(Scan.repeat P.term
|
|
620 |
-- Scan.repeat (P.$$$ inK |-- P.name
|
24250
|
621 |
-- Scan.option (P.$$$ module_nameK |-- P.name)
|
24219
|
622 |
-- Scan.option (P.$$$ fileK |-- P.name)
|
|
623 |
-- Scan.optional (P.$$$ "(" |-- P.arguments --| P.$$$ ")") []
|
|
624 |
) >> (fn (raw_cs, seris) => code raw_cs seris));
|
|
625 |
|
24250
|
626 |
val _ = OuterSyntax.add_keywords [inK, module_nameK, fileK];
|
24219
|
627 |
|
|
628 |
val (codeK, code_abstypeK, code_axiomsK, code_thmsK, code_depsK) =
|
24348
|
629 |
("export_code", "code_abstype", "code_axioms", "code_thms", "code_deps");
|
24219
|
630 |
|
|
631 |
in
|
|
632 |
|
|
633 |
val codeP =
|
|
634 |
OuterSyntax.improper_command codeK "generate executable code for constants"
|
|
635 |
K.diag (P.!!! code_exprP >> (fn f => Toplevel.keep (f o Toplevel.theory_of)));
|
|
636 |
|
|
637 |
fun codegen_command thy cmd =
|
|
638 |
case Scan.read OuterLex.stopper (P.!!! code_exprP) ((filter OuterLex.is_proper o OuterSyntax.scan) cmd)
|
|
639 |
of SOME f => (writeln "Now generating code..."; f thy)
|
|
640 |
| NONE => error ("Bad directive " ^ quote cmd);
|
|
641 |
|
|
642 |
val code_abstypeP =
|
|
643 |
OuterSyntax.command code_abstypeK "axiomatic abstypes for code generation" K.thy_decl (
|
|
644 |
(P.typ -- P.typ -- Scan.optional (P.$$$ "where" |-- Scan.repeat1
|
|
645 |
(P.term --| (P.$$$ "\\<equiv>" || P.$$$ "==") -- P.term)) [])
|
|
646 |
>> (Toplevel.theory o uncurry abstyp_e)
|
|
647 |
);
|
|
648 |
|
|
649 |
val code_axiomsP =
|
|
650 |
OuterSyntax.command code_axiomsK "axiomatic constant equalities for code generation" K.thy_decl (
|
|
651 |
Scan.repeat1 (P.term --| (P.$$$ "\\<equiv>" || P.$$$ "==") -- P.term)
|
|
652 |
>> (Toplevel.theory o constsubst_e)
|
|
653 |
);
|
|
654 |
|
|
655 |
val code_thmsP =
|
|
656 |
OuterSyntax.improper_command code_thmsK "print system of defining equations for code" OuterKeyword.diag
|
|
657 |
(Scan.repeat P.term
|
|
658 |
>> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
|
|
659 |
o Toplevel.keep ((fn thy => code_thms_cmd thy cs) o Toplevel.theory_of)));
|
|
660 |
|
|
661 |
val code_depsP =
|
|
662 |
OuterSyntax.improper_command code_depsK "visualize dependencies of defining equations for code" OuterKeyword.diag
|
|
663 |
(Scan.repeat P.term
|
|
664 |
>> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
|
|
665 |
o Toplevel.keep ((fn thy => code_deps_cmd thy cs) o Toplevel.theory_of)));
|
|
666 |
|
|
667 |
val _ = OuterSyntax.add_parsers [codeP, code_abstypeP, code_axiomsP, code_thmsP, code_depsP];
|
|
668 |
|
|
669 |
end; (* local *)
|
|
670 |
|
|
671 |
end; (* struct *)
|