author | wenzelm |
Tue, 03 Apr 2007 19:24:16 +0200 | |
changeset 22570 | f166a5416b3f |
parent 22554 | d1499fff65d8 |
child 22687 | 53943f4dab21 |
permissions | -rw-r--r-- |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1 |
(* Title: Pure/Tools/codegen_package.ML |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
2 |
ID: $Id$ |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
3 |
Author: Florian Haftmann, TU Muenchen |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
4 |
|
20855 | 5 |
Code generator extraction kernel. Code generator Isar setup. |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
6 |
*) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
7 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
8 |
signature CODEGEN_PACKAGE = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
9 |
sig |
20456 | 10 |
include BASIC_CODEGEN_THINGOL; |
21121 | 11 |
val codegen_term: theory -> term -> iterm; |
20600 | 12 |
val eval_term: theory -> (string (*reference name!*) * 'a option ref) * term -> 'a; |
22464 | 13 |
val satisfies_ref: bool option ref; |
14 |
val satisfies: theory -> term -> string list -> bool; |
|
21881 | 15 |
val codegen_command: theory -> string -> unit; |
18217 | 16 |
|
19884 | 17 |
type appgen; |
20439 | 18 |
val add_appconst: string * appgen -> theory -> theory; |
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
21722
diff
changeset
|
19 |
val appgen_numeral: (term -> IntInf.int option) -> appgen; |
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
20 |
val appgen_char: (term -> int option) -> appgen; |
20105 | 21 |
val appgen_case: (theory -> term |
22 |
-> ((string * typ) list * ((term * typ) * (term * term) list)) option) |
|
23 |
-> appgen; |
|
24 |
val appgen_let: appgen; |
|
21012 | 25 |
|
26 |
val timing: bool ref; |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
27 |
end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
28 |
|
18217 | 29 |
structure CodegenPackage : CODEGEN_PACKAGE = |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
30 |
struct |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
31 |
|
20896 | 32 |
open BasicCodegenThingol; |
33 |
val tracing = CodegenThingol.tracing; |
|
34 |
val succeed = CodegenThingol.succeed; |
|
35 |
val fail = CodegenThingol.fail; |
|
20699 | 36 |
|
37 |
(** code extraction **) |
|
38 |
||
39 |
(* theory data *) |
|
18217 | 40 |
|
20600 | 41 |
type appgen = theory -> ((sort -> sort) * Sorts.algebra) * Consts.T |
42 |
-> CodegenFuncgr.T |
|
20896 | 43 |
-> bool * string list option |
44 |
-> (string * typ) * term list -> CodegenThingol.transact -> iterm * CodegenThingol.transact; |
|
20439 | 45 |
|
46 |
type appgens = (int * (appgen * stamp)) Symtab.table; |
|
20931 | 47 |
val merge_appgens : appgens * appgens -> appgens = |
20105 | 48 |
Symtab.merge (fn ((bounds1, (_, stamp1)), (bounds2, (_, stamp2))) => |
20931 | 49 |
bounds1 = bounds2 andalso stamp1 = stamp2); |
18217 | 50 |
|
20931 | 51 |
structure Consttab = CodegenConsts.Consttab; |
52 |
type abstypes = typ Symtab.table * CodegenConsts.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 CodegenConsts.eq_const (consts1, consts2)); |
|
20456 | 56 |
|
20600 | 57 |
structure CodegenPackageData = TheoryDataFun |
18217 | 58 |
(struct |
22213 | 59 |
val name = "Pure/codegen_package_setup"; |
20931 | 60 |
type T = appgens * abstypes; |
61 |
val empty = (Symtab.empty, (Symtab.empty, Consttab.empty)); |
|
18217 | 62 |
val copy = I; |
63 |
val extend = I; |
|
20931 | 64 |
fun merge _ ((appgens1, abstypes1), (appgens2, abstypes2)) = |
65 |
(merge_appgens (appgens1, appgens2), merge_abstypes (abstypes1, abstypes2)); |
|
20456 | 66 |
fun print _ _ = (); |
18217 | 67 |
end); |
68 |
||
22213 | 69 |
structure Funcgr = CodegenFuncgrRetrieval ( |
70 |
val name = "Pure/codegen_package_thms"; |
|
71 |
fun rewrites thy = []; |
|
72 |
); |
|
73 |
||
74 |
fun print_codethms thy = |
|
75 |
Pretty.writeln o CodegenFuncgr.pretty thy o Funcgr.make thy; |
|
76 |
||
22507 | 77 |
fun code_deps thy consts = |
78 |
let |
|
79 |
val gr = Funcgr.make thy consts; |
|
80 |
fun mk_entry (const, (_, (_, parents))) = |
|
81 |
let |
|
82 |
val name = CodegenConsts.string_of_const thy const; |
|
83 |
val nameparents = map (CodegenConsts.string_of_const thy) parents; |
|
84 |
in { name = name, ID = name, dir = "", unfold = true, |
|
85 |
path = "", parents = nameparents } |
|
86 |
end; |
|
87 |
val prgr = CodegenFuncgr.Constgraph.fold ((fn x => fn xs => xs @ [x]) o mk_entry) gr []; |
|
88 |
in Present.display_graph prgr end; |
|
89 |
||
22213 | 90 |
structure Code = CodeDataFun |
91 |
(struct |
|
92 |
val name = "Pure/codegen_package_code"; |
|
93 |
type T = CodegenThingol.code; |
|
94 |
val empty = CodegenThingol.empty_code; |
|
95 |
fun merge _ = CodegenThingol.merge_code; |
|
96 |
fun purge _ NONE _ = CodegenThingol.empty_code |
|
97 |
| purge NONE _ _ = CodegenThingol.empty_code |
|
98 |
| purge (SOME thy) (SOME cs) code = |
|
99 |
let |
|
100 |
val cs_exisiting = |
|
101 |
map_filter (CodegenNames.const_rev thy) (Graph.keys code); |
|
102 |
val dels = (Graph.all_preds code |
|
103 |
o map (CodegenNames.const thy) |
|
104 |
o filter (member CodegenConsts.eq_const cs_exisiting) |
|
105 |
) cs; |
|
106 |
in Graph.del_nodes dels code end; |
|
107 |
end); |
|
108 |
||
109 |
val _ = Context.add_setup (CodegenPackageData.init #> Funcgr.init #> Code.init); |
|
18708 | 110 |
|
18865 | 111 |
|
22197 | 112 |
(* preparing defining equations *) |
21990 | 113 |
|
114 |
fun prep_eqs thy (thms as thm :: _) = |
|
115 |
let |
|
22035 | 116 |
val ty = (Logic.unvarifyT o CodegenFunc.typ_func) thm; |
21990 | 117 |
val thms' = if (null o Term.typ_tfrees) ty orelse (null o fst o strip_type) ty |
118 |
then thms |
|
22035 | 119 |
else map (CodegenFunc.expand_eta 1) thms; |
21990 | 120 |
in (ty, thms') end; |
121 |
||
122 |
||
20386 | 123 |
(* extraction kernel *) |
18865 | 124 |
|
20931 | 125 |
fun check_strict (false, _) has_seri x = |
19884 | 126 |
false |
20931 | 127 |
| check_strict (_, SOME targets) has_seri x = |
20699 | 128 |
not (has_seri targets x) |
20931 | 129 |
| check_strict (true, _) has_seri x = |
19884 | 130 |
true; |
131 |
||
20931 | 132 |
fun get_abstype thy (tyco, tys) = case Symtab.lookup ((fst o snd o CodegenPackageData.get) thy) tyco |
133 |
of SOME ty => SOME ((map_atyps (fn TFree (n, _) => nth tys (the (Int.fromString n)))) ty) |
|
134 |
| NONE => NONE; |
|
135 |
||
22035 | 136 |
fun ensure_def thy = CodegenThingol.ensure_def (CodegenNames.labelled_name thy); |
137 |
||
22197 | 138 |
fun ensure_def_class thy (algbr as ((_, algebra), _)) funcgr strct class = |
20386 | 139 |
let |
22185 | 140 |
val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class; |
21895 | 141 |
val (v, cs) = AxClass.params_of_class thy class; |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22035
diff
changeset
|
142 |
val class' = CodegenNames.class thy class; |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22035
diff
changeset
|
143 |
val classrels' = map (curry (CodegenNames.classrel thy) class) superclasses; |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
144 |
val classops' = map (CodegenNames.const thy o CodegenConsts.const_of_cexpr thy) cs; |
22197 | 145 |
val defgen_class = |
146 |
fold_map (ensure_def_class thy algbr funcgr strct) superclasses |
|
147 |
##>> (fold_map (exprgen_type thy algbr funcgr strct) o map snd) cs |
|
148 |
#-> (fn (superclasses, classoptyps) => succeed |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22035
diff
changeset
|
149 |
(CodegenThingol.Class (superclasses ~~ classrels', (unprefix "'" v, classops' ~~ classoptyps)))) |
18454 | 150 |
in |
22197 | 151 |
tracing (fn _ => "generating class " ^ quote class) |
152 |
#> ensure_def thy defgen_class true |
|
20896 | 153 |
("generating class " ^ quote class) class' |
22197 | 154 |
#> pair class' |
18865 | 155 |
end |
22197 | 156 |
and ensure_def_classrel thy algbr funcgr strct (subclass, superclass) = |
157 |
ensure_def_class thy algbr funcgr strct subclass |
|
158 |
#>> (fn _ => CodegenNames.classrel thy (subclass, superclass)) |
|
22354 | 159 |
and ensure_def_tyco thy algbr funcgr strct "fun" trns = |
160 |
trns |
|
161 |
|> pair "fun" |
|
162 |
| ensure_def_tyco thy algbr funcgr strct tyco trns = |
|
163 |
let |
|
164 |
fun defgen_datatype trns = |
|
22396 | 165 |
let |
22423 | 166 |
val (vs, cos) = CodegenData.get_datatype thy tyco; |
22396 | 167 |
in |
168 |
trns |
|
169 |
|> fold_map (exprgen_tyvar_sort thy algbr funcgr strct) vs |
|
170 |
||>> fold_map (fn (c, tys) => |
|
171 |
fold_map (exprgen_type thy algbr funcgr strct) tys |
|
172 |
#-> (fn tys' => |
|
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
173 |
pair ((CodegenNames.const thy o CodegenConsts.const_of_cexpr thy) |
22396 | 174 |
(c, tys ---> Type (tyco, map TFree vs)), tys'))) cos |
175 |
|-> (fn (vs, cos) => succeed (CodegenThingol.Datatype (vs, cos))) |
|
176 |
end; |
|
22354 | 177 |
val tyco' = CodegenNames.tyco thy tyco; |
178 |
in |
|
179 |
trns |
|
180 |
|> tracing (fn _ => "generating type constructor " ^ quote tyco) |
|
22396 | 181 |
|> ensure_def thy defgen_datatype true |
22354 | 182 |
("generating type constructor " ^ quote tyco) tyco' |
183 |
|> pair tyco' |
|
184 |
end |
|
20600 | 185 |
and exprgen_tyvar_sort thy (algbr as ((proj_sort, _), _)) funcgr strct (v, sort) trns = |
18516 | 186 |
trns |
20600 | 187 |
|> fold_map (ensure_def_class thy algbr funcgr strct) (proj_sort sort) |
22185 | 188 |
|>> (fn sort => (unprefix "'" v, sort)) |
22197 | 189 |
and exprgen_type thy algbr funcgr strct (TFree vs) trns = |
18516 | 190 |
trns |
20600 | 191 |
|> exprgen_tyvar_sort thy algbr funcgr strct vs |
22185 | 192 |
|>> (fn (v, sort) => ITyVar v) |
20600 | 193 |
| exprgen_type thy algbr funcgr strct (Type (tyco, tys)) trns = |
20931 | 194 |
case get_abstype thy (tyco, tys) |
195 |
of SOME ty => |
|
196 |
trns |
|
197 |
|> exprgen_type thy algbr funcgr strct ty |
|
198 |
| NONE => |
|
199 |
trns |
|
200 |
|> ensure_def_tyco thy algbr funcgr strct tyco |
|
201 |
||>> fold_map (exprgen_type thy algbr funcgr strct) tys |
|
22185 | 202 |
|>> (fn (tyco, tys) => tyco `%% tys); |
18516 | 203 |
|
20835 | 204 |
exception CONSTRAIN of (string * typ) * typ; |
21012 | 205 |
val timing = ref false; |
20600 | 206 |
|
22197 | 207 |
fun exprgen_dicts thy (algbr as ((proj_sort, algebra), consts)) funcgr strct (ty_ctxt, sort_decl) = |
20456 | 208 |
let |
209 |
val pp = Sign.pp thy; |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22035
diff
changeset
|
210 |
datatype typarg = |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22035
diff
changeset
|
211 |
Global of (class * string) * typarg list list |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22035
diff
changeset
|
212 |
| Local of (class * class) list * (string * (int * sort)); |
22570
f166a5416b3f
renamed of_sort_derivation record fields (avoid clash with Alice keywords);
wenzelm
parents:
22554
diff
changeset
|
213 |
fun class_relation (Global ((_, tyco), yss), _) class = |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22035
diff
changeset
|
214 |
Global ((class, tyco), yss) |
22570
f166a5416b3f
renamed of_sort_derivation record fields (avoid clash with Alice keywords);
wenzelm
parents:
22554
diff
changeset
|
215 |
| class_relation (Local (classrels, v), subclass) superclass = |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22035
diff
changeset
|
216 |
Local ((subclass, superclass) :: classrels, v); |
22570
f166a5416b3f
renamed of_sort_derivation record fields (avoid clash with Alice keywords);
wenzelm
parents:
22554
diff
changeset
|
217 |
fun type_constructor tyco yss class = |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22035
diff
changeset
|
218 |
Global ((class, tyco), (map o map) fst yss); |
22570
f166a5416b3f
renamed of_sort_derivation record fields (avoid clash with Alice keywords);
wenzelm
parents:
22554
diff
changeset
|
219 |
fun type_variable (TFree (v, sort)) = |
20456 | 220 |
let |
221 |
val sort' = proj_sort sort; |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22035
diff
changeset
|
222 |
in map_index (fn (n, class) => (Local ([], (v, (n, sort'))), class)) sort' end; |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22035
diff
changeset
|
223 |
val typargs = Sorts.of_sort_derivation pp algebra |
22570
f166a5416b3f
renamed of_sort_derivation record fields (avoid clash with Alice keywords);
wenzelm
parents:
22554
diff
changeset
|
224 |
{class_relation = class_relation, type_constructor = type_constructor, |
f166a5416b3f
renamed of_sort_derivation record fields (avoid clash with Alice keywords);
wenzelm
parents:
22554
diff
changeset
|
225 |
type_variable = type_variable} |
20456 | 226 |
(ty_ctxt, proj_sort sort_decl); |
22197 | 227 |
fun mk_dict (Global (inst, yss)) = |
228 |
ensure_def_inst thy algbr funcgr strct inst |
|
229 |
##>> (fold_map o fold_map) mk_dict yss |
|
230 |
#>> (fn (inst, dss) => DictConst (inst, dss)) |
|
231 |
| mk_dict (Local (classrels, (v, (k, sort)))) = |
|
232 |
fold_map (ensure_def_classrel thy algbr funcgr strct) classrels |
|
233 |
#>> (fn classrels => DictVar (classrels, (unprefix "'" v, (k, length sort)))) |
|
20456 | 234 |
in |
22197 | 235 |
fold_map mk_dict typargs |
20456 | 236 |
end |
22197 | 237 |
and exprgen_dict_parms thy (algbr as (_, consts)) funcgr strct (c, ty_ctxt) = |
20600 | 238 |
let |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
239 |
val c' = CodegenConsts.const_of_cexpr thy (c, ty_ctxt) |
20699 | 240 |
val idf = CodegenNames.const thy c'; |
21722 | 241 |
val ty_decl = Consts.the_declaration consts idf; |
22197 | 242 |
val (tys, tys_decl) = pairself (curry (Consts.typargs consts) idf) (ty_ctxt, ty_decl); |
243 |
val sorts = map (snd o dest_TVar) tys_decl; |
|
20456 | 244 |
in |
22197 | 245 |
fold_map (exprgen_dicts thy algbr funcgr strct) (tys ~~ sorts) |
20456 | 246 |
end |
22185 | 247 |
and ensure_def_inst thy (algbr as ((_, algebra), _)) funcgr strct (class, tyco) trns = |
20456 | 248 |
let |
22185 | 249 |
val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class; |
250 |
val (var, classops) = try (AxClass.params_of_class thy) class |> the_default ("'a", []) |
|
251 |
val vs = Name.names (Name.declare var Name.context) "'a" (Sorts.mg_domain algebra tyco [class]); |
|
252 |
val arity_typ = Type (tyco, map TFree vs); |
|
20896 | 253 |
fun gen_superarity superclass trns = |
254 |
trns |
|
255 |
|> ensure_def_class thy algbr funcgr strct superclass |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22035
diff
changeset
|
256 |
||>> ensure_def_classrel thy algbr funcgr strct (class, superclass) |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22035
diff
changeset
|
257 |
||>> exprgen_dicts thy algbr funcgr strct (arity_typ, [superclass]) |
22185 | 258 |
|>> (fn ((superclass, classrel), [DictConst (inst, dss)]) => |
259 |
(superclass, (classrel, (inst, dss)))); |
|
260 |
fun gen_classop_def (classop as (c, ty)) trns = |
|
20896 | 261 |
trns |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
262 |
|> ensure_def_const thy algbr funcgr strct (CodegenConsts.const_of_cexpr thy classop) |
22185 | 263 |
||>> exprgen_term thy algbr funcgr strct (Const (c, map_type_tfree (K arity_typ) ty)); |
20896 | 264 |
fun defgen_inst trns = |
265 |
trns |
|
266 |
|> ensure_def_class thy algbr funcgr strct class |
|
267 |
||>> ensure_def_tyco thy algbr funcgr strct tyco |
|
268 |
||>> fold_map (exprgen_tyvar_sort thy algbr funcgr strct) vs |
|
269 |
||>> fold_map gen_superarity superclasses |
|
22185 | 270 |
||>> fold_map gen_classop_def classops |
20896 | 271 |
|-> (fn ((((class, tyco), arity), superarities), classops) => |
272 |
succeed (CodegenThingol.Classinst ((class, (tyco, arity)), (superarities, classops)))); |
|
273 |
val inst = CodegenNames.instance thy (class, tyco); |
|
18865 | 274 |
in |
275 |
trns |
|
20896 | 276 |
|> tracing (fn _ => "generating instance " ^ quote class ^ " / " ^ quote tyco) |
22185 | 277 |
|> ensure_def thy defgen_inst true ("generating instance " ^ quote class ^ " / " ^ quote tyco) inst |
18865 | 278 |
|> pair inst |
279 |
end |
|
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
280 |
and ensure_def_const thy (algbr as (_, consts)) funcgr strct (const as (c, opt_tyco)) trns = |
18865 | 281 |
let |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
282 |
val c' = CodegenNames.const thy const; |
20896 | 283 |
fun defgen_datatypecons trns = |
284 |
trns |
|
285 |
|> ensure_def_tyco thy algbr funcgr strct |
|
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
286 |
((the o CodegenData.get_datatype_of_constr thy) const) |
20896 | 287 |
|-> (fn _ => succeed CodegenThingol.Bot); |
288 |
fun defgen_classop trns = |
|
289 |
trns |
|
290 |
|> ensure_def_class thy algbr funcgr strct ((the o AxClass.class_of_param thy) c) |
|
291 |
|-> (fn _ => succeed CodegenThingol.Bot); |
|
292 |
fun defgen_fun trns = |
|
21121 | 293 |
case CodegenFuncgr.funcs funcgr |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
294 |
(perhaps (Consttab.lookup ((snd o snd o CodegenPackageData.get) thy)) const) |
21990 | 295 |
of thms as _ :: _ => |
20456 | 296 |
let |
21990 | 297 |
val (ty, eq_thms) = prep_eqs thy thms; |
21012 | 298 |
val timeap = if !timing then Output.timeap_msg ("time for " ^ c') |
299 |
else I; |
|
20456 | 300 |
val msg = cat_lines ("generating code for theorems " :: map string_of_thm eq_thms); |
20466 | 301 |
val vs = (map dest_TFree o Consts.typargs consts) (c', ty); |
20835 | 302 |
val dest_eqthm = |
303 |
apfst (snd o strip_comb) o Logic.dest_equals o Logic.unvarify o prop_of; |
|
20456 | 304 |
fun exprgen_eq (args, rhs) trns = |
305 |
trns |
|
20600 | 306 |
|> fold_map (exprgen_term thy algbr funcgr strct) args |
307 |
||>> exprgen_term thy algbr funcgr strct rhs; |
|
20456 | 308 |
in |
309 |
trns |
|
20896 | 310 |
|> CodegenThingol.message msg (fn trns => trns |
21012 | 311 |
|> timeap (fold_map (exprgen_eq o dest_eqthm) eq_thms) |
20600 | 312 |
||>> fold_map (exprgen_tyvar_sort thy algbr funcgr strct) vs |
313 |
||>> exprgen_type thy algbr funcgr strct ty |
|
20896 | 314 |
|-> (fn ((eqs, vs), ty) => succeed (CodegenThingol.Fun (eqs, (vs, ty))))) |
20456 | 315 |
end |
316 |
| [] => |
|
317 |
trns |
|
318 |
|> fail ("No defining equations found for " |
|
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
319 |
^ (quote o CodegenConsts.string_of_const thy) const); |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
320 |
val defgen = if (is_some o CodegenData.get_datatype_of_constr thy) const |
21081 | 321 |
then defgen_datatypecons |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
322 |
else if is_some opt_tyco |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
323 |
orelse (not o is_some o AxClass.class_of_param thy) c |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
324 |
then defgen_fun |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
325 |
else defgen_classop |
20931 | 326 |
val strict = check_strict strct (CodegenSerializer.const_has_serialization thy) c'; |
18865 | 327 |
in |
328 |
trns |
|
20835 | 329 |
|> tracing (fn _ => "generating constant " |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
330 |
^ (quote o CodegenConsts.string_of_const thy) const) |
22035 | 331 |
|> ensure_def thy defgen strict ("generating constant " |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
332 |
^ CodegenConsts.string_of_const thy const) c' |
20896 | 333 |
|> pair c' |
18865 | 334 |
end |
20896 | 335 |
and exprgen_term thy algbr funcgr strct (Const (c, ty)) trns = |
18517 | 336 |
trns |
20896 | 337 |
|> select_appgen thy algbr funcgr strct ((c, ty), []) |
20600 | 338 |
| exprgen_term thy algbr funcgr strct (Free (v, ty)) trns = |
18516 | 339 |
trns |
20600 | 340 |
|> exprgen_type thy algbr funcgr strct ty |
22185 | 341 |
|>> (fn _ => IVar v) |
20600 | 342 |
| exprgen_term thy algbr funcgr strct (Abs (raw_v, ty, raw_t)) trns = |
19136 | 343 |
let |
22035 | 344 |
val (v, t) = Syntax.variant_abs (raw_v, ty, raw_t); |
19136 | 345 |
in |
346 |
trns |
|
20600 | 347 |
|> exprgen_type thy algbr funcgr strct ty |
348 |
||>> exprgen_term thy algbr funcgr strct t |
|
22185 | 349 |
|>> (fn (ty, t) => (v, ty) `|-> t) |
19136 | 350 |
end |
20896 | 351 |
| exprgen_term thy algbr funcgr strct (t as _ $ _) trns = |
352 |
case strip_comb t |
|
353 |
of (Const (c, ty), ts) => |
|
18516 | 354 |
trns |
20896 | 355 |
|> select_appgen thy algbr funcgr strct ((c, ty), ts) |
356 |
| (t', ts) => |
|
18516 | 357 |
trns |
20600 | 358 |
|> exprgen_term thy algbr funcgr strct t' |
359 |
||>> fold_map (exprgen_term thy algbr funcgr strct) ts |
|
22185 | 360 |
|>> (fn (t, ts) => t `$$ ts) |
20600 | 361 |
and appgen_default thy algbr funcgr strct ((c, ty), ts) trns = |
18865 | 362 |
trns |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
363 |
|> ensure_def_const thy algbr funcgr strct (CodegenConsts.const_of_cexpr thy (c, ty)) |
22305 | 364 |
||>> fold_map (exprgen_type thy algbr funcgr strct) ((fst o Term.strip_type) ty) |
365 |
||>> exprgen_type thy algbr funcgr strct ((snd o Term.strip_type) ty) |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22035
diff
changeset
|
366 |
||>> exprgen_dict_parms thy algbr funcgr strct (c, ty) |
20600 | 367 |
||>> fold_map (exprgen_term thy algbr funcgr strct) ts |
22305 | 368 |
|>> (fn ((((c, tys), ty), iss), ts) => IConst (c, (iss, tys)) `$$ ts) |
20896 | 369 |
and select_appgen thy algbr funcgr strct ((c, ty), ts) trns = |
20931 | 370 |
case Symtab.lookup (fst (CodegenPackageData.get thy)) c |
20896 | 371 |
of SOME (i, (appgen, _)) => |
20105 | 372 |
if length ts < i then |
18865 | 373 |
let |
21161 | 374 |
val k = length ts; |
375 |
val tys = (curry Library.take (i - k) o curry Library.drop k o fst o strip_type) ty; |
|
376 |
val ctxt = (fold o fold_aterms) |
|
377 |
(fn Free (v, _) => Name.declare v | _ => I) ts Name.context; |
|
378 |
val vs = Name.names ctxt "a" tys; |
|
18865 | 379 |
in |
380 |
trns |
|
20600 | 381 |
|> fold_map (exprgen_type thy algbr funcgr strct) tys |
20896 | 382 |
||>> appgen thy algbr funcgr strct ((c, ty), ts @ map Free vs) |
22185 | 383 |
|>> (fn (tys, t) => map2 (fn (v, _) => pair v) vs tys `|--> t) |
18865 | 384 |
end |
20105 | 385 |
else if length ts > i then |
18865 | 386 |
trns |
20896 | 387 |
|> appgen thy algbr funcgr strct ((c, ty), Library.take (i, ts)) |
20600 | 388 |
||>> fold_map (exprgen_term thy algbr funcgr strct) (Library.drop (i, ts)) |
22185 | 389 |
|>> (fn (t, ts) => t `$$ ts) |
18865 | 390 |
else |
391 |
trns |
|
20896 | 392 |
|> appgen thy algbr funcgr strct ((c, ty), ts) |
18865 | 393 |
| NONE => |
394 |
trns |
|
20896 | 395 |
|> appgen_default thy algbr funcgr strct ((c, ty), ts); |
20600 | 396 |
|
397 |
||
20835 | 398 |
(* entrance points into extraction kernel *) |
20600 | 399 |
|
400 |
fun ensure_def_const' thy algbr funcgr strct c trns = |
|
401 |
ensure_def_const thy algbr funcgr strct c trns |
|
20835 | 402 |
handle CONSTRAIN ((c, ty), ty_decl) => error ( |
20600 | 403 |
"Constant " ^ c ^ " with most general type\n" |
22197 | 404 |
^ CodegenConsts.string_of_typ thy ty |
20600 | 405 |
^ "\noccurs with type\n" |
22197 | 406 |
^ CodegenConsts.string_of_typ thy ty_decl); |
22185 | 407 |
|
408 |
fun perhaps_def_const thy algbr funcgr strct c trns = |
|
409 |
case try (ensure_def_const thy algbr funcgr strct c) trns |
|
410 |
of SOME (c, trns) => (SOME c, trns) |
|
411 |
| NONE => (NONE, trns); |
|
412 |
||
20600 | 413 |
fun exprgen_term' thy algbr funcgr strct t trns = |
414 |
exprgen_term thy algbr funcgr strct t trns |
|
20835 | 415 |
handle CONSTRAIN ((c, ty), ty_decl) => error ("In term " ^ (quote o Sign.string_of_term thy) t |
20600 | 416 |
^ ",\nconstant " ^ c ^ " with most general type\n" |
22197 | 417 |
^ CodegenConsts.string_of_typ thy ty |
20600 | 418 |
^ "\noccurs with type\n" |
22197 | 419 |
^ CodegenConsts.string_of_typ thy ty_decl); |
18516 | 420 |
|
18702 | 421 |
|
20439 | 422 |
(* parametrized application generators, for instantiation in object logic *) |
423 |
(* (axiomatic extensions of extraction kernel *) |
|
18217 | 424 |
|
20600 | 425 |
fun appgen_numeral int_of_numeral thy algbr funcgr strct (app as (c, ts)) trns = |
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
21722
diff
changeset
|
426 |
case int_of_numeral (list_comb (Const c, ts)) |
20835 | 427 |
of SOME i => |
20353 | 428 |
trns |
21012 | 429 |
|> pair (CodegenThingol.INum i) |
19884 | 430 |
| NONE => |
431 |
trns |
|
20835 | 432 |
|> appgen_default thy algbr funcgr strct app; |
18217 | 433 |
|
20600 | 434 |
fun appgen_char char_to_index thy algbr funcgr strct (app as ((_, ty), _)) trns = |
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
435 |
case (char_to_index o list_comb o apfst Const) app |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
436 |
of SOME i => |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
437 |
trns |
20600 | 438 |
|> exprgen_type thy algbr funcgr strct ty |
22185 | 439 |
|>> (fn _ => IChar (chr i)) |
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
440 |
| NONE => |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
441 |
trns |
20600 | 442 |
|> appgen_default thy algbr funcgr strct app; |
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
443 |
|
21012 | 444 |
val debug_term = ref (Bound 0); |
445 |
||
20600 | 446 |
fun appgen_case dest_case_expr thy algbr funcgr strct (app as (c_ty, ts)) trns = |
20105 | 447 |
let |
448 |
val SOME ([], ((st, sty), ds)) = dest_case_expr thy (list_comb (Const c_ty, ts)); |
|
22035 | 449 |
fun clausegen (dt, bt) trns = |
450 |
trns |
|
451 |
|> exprgen_term thy algbr funcgr strct dt |
|
452 |
||>> exprgen_term thy algbr funcgr strct bt; |
|
20105 | 453 |
in |
454 |
trns |
|
20600 | 455 |
|> exprgen_term thy algbr funcgr strct st |
456 |
||>> exprgen_type thy algbr funcgr strct sty |
|
20105 | 457 |
||>> fold_map clausegen ds |
22185 | 458 |
|>> (fn ((se, sty), ds) => ICase ((se, sty), ds)) |
20105 | 459 |
end; |
460 |
||
20600 | 461 |
fun appgen_let thy algbr funcgr strct (app as (_, [st, ct])) trns = |
20105 | 462 |
trns |
20600 | 463 |
|> exprgen_term thy algbr funcgr strct ct |
464 |
||>> exprgen_term thy algbr funcgr strct st |
|
21012 | 465 |
|-> (fn ((v, ty) `|-> be, se) => |
466 |
pair (ICase ((se, ty), case be |
|
467 |
of ICase ((IVar w, _), ds) => if v = w then ds else [(IVar v, be)] |
|
20105 | 468 |
| _ => [(IVar v, be)] |
21012 | 469 |
)) |
470 |
| _ => appgen_default thy algbr funcgr strct app); |
|
20105 | 471 |
|
20439 | 472 |
fun add_appconst (c, appgen) thy = |
473 |
let |
|
20931 | 474 |
val i = (length o fst o strip_type o Sign.the_const_type thy) c; |
475 |
val _ = Code.change thy (K CodegenThingol.empty_code); |
|
476 |
in |
|
477 |
(CodegenPackageData.map o apfst) |
|
478 |
(Symtab.update (c, (i, (appgen, stamp ())))) thy |
|
479 |
end; |
|
480 |
||
481 |
||
482 |
||
483 |
(** abstype and constsubst interface **) |
|
484 |
||
21916 | 485 |
local |
486 |
||
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
487 |
fun add_consts thy f (c1, c2 as (c, opt_tyco)) = |
21916 | 488 |
let |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
489 |
val _ = if |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
490 |
is_some (AxClass.class_of_param thy c) andalso is_none opt_tyco |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
491 |
orelse is_some (CodegenData.get_datatype_of_constr thy c2) |
22197 | 492 |
then error ("Not a function: " ^ CodegenConsts.string_of_const thy c2) |
21916 | 493 |
else (); |
22213 | 494 |
val funcgr = Funcgr.make thy [c1, c2]; |
21916 | 495 |
val ty1 = (f o CodegenFuncgr.typ funcgr) c1; |
496 |
val ty2 = CodegenFuncgr.typ funcgr c2; |
|
497 |
val _ = if Sign.typ_equiv thy (ty1, ty2) then () else |
|
498 |
error ("Incompatiable type signatures of " ^ CodegenConsts.string_of_const thy c1 |
|
499 |
^ " and " ^ CodegenConsts.string_of_const thy c2 ^ ":\n" |
|
22197 | 500 |
^ CodegenConsts.string_of_typ thy ty1 ^ "\n" ^ CodegenConsts.string_of_typ thy ty2); |
21916 | 501 |
in Consttab.update (c1, c2) end; |
502 |
||
20931 | 503 |
fun gen_abstyp prep_const prep_typ (raw_abstyp, raw_substtyp) raw_absconsts thy = |
504 |
let |
|
505 |
val abstyp = Type.no_tvars (prep_typ thy raw_abstyp); |
|
506 |
val substtyp = Type.no_tvars (prep_typ thy raw_substtyp); |
|
507 |
val absconsts = (map o pairself) (prep_const thy) raw_absconsts; |
|
22197 | 508 |
val Type (abstyco, tys) = abstyp handle BIND => error ("Bad type: " ^ Sign.string_of_typ thy abstyp); |
509 |
val typarms = map (fst o dest_TFree) tys handle MATCH => error ("Bad type: " ^ Sign.string_of_typ thy abstyp); |
|
20931 | 510 |
fun mk_index v = |
511 |
let |
|
512 |
val k = find_index (fn w => v = w) typarms; |
|
513 |
in if k = ~1 |
|
22197 | 514 |
then error ("Free type variable: " ^ quote v) |
20931 | 515 |
else TFree (string_of_int k, []) |
516 |
end; |
|
517 |
val typpat = map_atyps (fn TFree (v, _) => mk_index v) substtyp; |
|
518 |
fun apply_typpat (Type (tyco, tys)) = |
|
519 |
let |
|
520 |
val tys' = map apply_typpat tys; |
|
521 |
in if tyco = abstyco then |
|
522 |
(map_atyps (fn TFree (n, _) => nth tys' (the (Int.fromString n)))) typpat |
|
523 |
else |
|
524 |
Type (tyco, tys') |
|
525 |
end |
|
526 |
| apply_typpat ty = ty; |
|
527 |
val _ = Code.change thy (K CodegenThingol.empty_code); |
|
528 |
in |
|
529 |
thy |
|
530 |
|> (CodegenPackageData.map o apsnd) (fn (abstypes, abscs) => |
|
531 |
(abstypes |
|
532 |
|> Symtab.update (abstyco, typpat), |
|
533 |
abscs |
|
21916 | 534 |
|> fold (add_consts thy apply_typpat) absconsts) |
20931 | 535 |
) |
536 |
end; |
|
537 |
||
538 |
fun gen_constsubst prep_const raw_constsubsts thy = |
|
539 |
let |
|
540 |
val constsubsts = (map o pairself) (prep_const thy) raw_constsubsts; |
|
541 |
val _ = Code.change thy (K CodegenThingol.empty_code); |
|
542 |
in |
|
543 |
thy |
|
21916 | 544 |
|> (CodegenPackageData.map o apsnd o apsnd) (fold (add_consts thy I) constsubsts) |
20931 | 545 |
end; |
546 |
||
21916 | 547 |
in |
548 |
||
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
549 |
val abstyp = gen_abstyp (K I) Sign.certify_typ; |
20931 | 550 |
val abstyp_e = gen_abstyp CodegenConsts.read_const (fn thy => Sign.read_typ (thy, K NONE)); |
551 |
||
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
552 |
val constsubst = gen_constsubst (K I); |
20931 | 553 |
val constsubst_e = gen_constsubst CodegenConsts.read_const; |
20439 | 554 |
|
21916 | 555 |
end; (*local*) |
18217 | 556 |
|
18516 | 557 |
|
20439 | 558 |
(** code generation interfaces **) |
18516 | 559 |
|
21881 | 560 |
(* generic generation combinators *) |
561 |
||
22185 | 562 |
fun generate thy funcgr targets gen it = |
20466 | 563 |
let |
21121 | 564 |
val cs = map_filter (Consttab.lookup ((snd o snd o CodegenPackageData.get) thy)) |
565 |
(CodegenFuncgr.all funcgr); |
|
22213 | 566 |
val funcgr' = Funcgr.make thy cs; |
20931 | 567 |
val qnaming = NameSpace.qualified_names NameSpace.default_naming; |
20466 | 568 |
val consttab = Consts.empty |
21121 | 569 |
|> fold (fn c => Consts.declare qnaming |
570 |
((CodegenNames.const thy c, CodegenFuncgr.typ funcgr' c), true)) |
|
571 |
(CodegenFuncgr.all funcgr'); |
|
22185 | 572 |
val algbr = (CodegenData.operational_algebra thy, consttab); |
20466 | 573 |
in |
22185 | 574 |
Code.change_yield thy (CodegenThingol.start_transact (gen thy algbr funcgr' |
20600 | 575 |
(true, targets) it)) |
21121 | 576 |
|> fst |
20466 | 577 |
end; |
18516 | 578 |
|
20600 | 579 |
fun codegen_term thy t = |
20353 | 580 |
let |
20600 | 581 |
val ct = Thm.cterm_of thy t; |
22213 | 582 |
val (ct', funcgr) = Funcgr.make_term thy (K (K K)) ct; |
20835 | 583 |
val t' = Thm.term_of ct'; |
22185 | 584 |
in generate thy funcgr (SOME []) exprgen_term' t' end; |
19136 | 585 |
|
22464 | 586 |
fun raw_eval_term thy (ref_spec, t) args = |
20213 | 587 |
let |
22507 | 588 |
val _ = (Term.map_types o Term.map_atyps) (fn _ => |
589 |
error ("Term " ^ Sign.string_of_term thy t ^ " contains polymorphic type")) |
|
21388 | 590 |
t; |
21121 | 591 |
val t' = codegen_term thy t; |
22464 | 592 |
in |
593 |
CodegenSerializer.eval_term thy CodegenNames.labelled_name |
|
594 |
(Code.get thy) (ref_spec, t') args |
|
595 |
end; |
|
596 |
||
597 |
val satisfies_ref : bool option ref = ref NONE; |
|
598 |
||
599 |
fun eval_term thy t = raw_eval_term thy t []; |
|
600 |
fun satisfies thy t witnesses = raw_eval_term thy |
|
601 |
(("CodegenPackage.satisfies_ref", satisfies_ref), t) witnesses; |
|
18217 | 602 |
|
603 |
||
21881 | 604 |
(* constant specifications with wildcards *) |
605 |
||
606 |
fun consts_of thy thyname = |
|
607 |
let |
|
608 |
val this_thy = Option.map theory thyname |> the_default thy; |
|
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
609 |
val defs = (#defs o rep_theory) thy; |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
610 |
val cs = Symtab.fold (fn (c, (_, NONE)) => cons c | _ => I) |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
611 |
((snd o #constants o Consts.dest o #consts o Sign.rep_sg) this_thy) []; |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
612 |
fun classop c = case AxClass.class_of_param thy c |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
613 |
of NONE => [(c, NONE)] |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
614 |
| SOME class => Symtab.fold |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
615 |
(fn (tyco, classes) => if AList.defined (op =) classes class |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
616 |
then cons (c, SOME tyco) else I) |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
617 |
((#arities o Sorts.rep_algebra o Sign.classes_of) this_thy) |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
618 |
[(c, NONE)]; |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
619 |
val consts = maps classop cs; |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
620 |
fun test_instance thy (class, tyco) = |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
621 |
can (Sorts.mg_domain (Sign.classes_of thy) tyco) [class] |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
622 |
fun belongs_here thyname (c, NONE) = |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
623 |
not (exists (fn thy => Sign.declared_const thy c) (Theory.parents_of this_thy)) |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
624 |
| belongs_here thyname (c, SOME tyco) = |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
625 |
not (exists (fn thy => test_instance thy ((the o AxClass.class_of_param thy) c, tyco)) |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
626 |
(Theory.parents_of this_thy)) |
21881 | 627 |
in case thyname |
628 |
of NONE => consts |
|
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22507
diff
changeset
|
629 |
| SOME thyname => filter (belongs_here thyname) consts |
21881 | 630 |
end; |
631 |
||
22185 | 632 |
fun filter_generatable thy targets consts = |
21881 | 633 |
let |
22213 | 634 |
val (consts', funcgr) = Funcgr.make_consts thy consts; |
22185 | 635 |
val consts'' = generate thy funcgr targets (fold_map oooo perhaps_def_const) consts'; |
636 |
val consts''' = map_filter (fn (const, SOME _) => SOME const | (_, NONE) => NONE) |
|
637 |
(consts' ~~ consts''); |
|
638 |
in consts''' end; |
|
21881 | 639 |
|
22185 | 640 |
fun read_constspec thy targets "*" = filter_generatable thy targets (consts_of thy NONE) |
641 |
| read_constspec thy targets s = if String.isSuffix ".*" s |
|
642 |
then filter_generatable thy targets (consts_of thy (SOME (unsuffix ".*" s))) |
|
21881 | 643 |
else [CodegenConsts.read_const thy s]; |
644 |
||
18516 | 645 |
|
20439 | 646 |
(** toplevel interface and setup **) |
18756 | 647 |
|
648 |
local |
|
19150 | 649 |
|
20699 | 650 |
structure P = OuterParse |
651 |
and K = OuterKeyword |
|
652 |
||
20439 | 653 |
fun code raw_cs seris thy = |
18217 | 654 |
let |
21081 | 655 |
val seris' = map (fn (target, args as _ :: _) => |
22305 | 656 |
(target, SOME (CodegenSerializer.get_serializer thy target args CodegenNames.labelled_name)) |
21081 | 657 |
| (target, []) => (CodegenSerializer.assert_serializer thy target, NONE)) seris; |
22185 | 658 |
val targets = case map fst seris' of [] => NONE | xs => SOME xs; |
659 |
val cs = maps (read_constspec thy targets) raw_cs; |
|
20439 | 660 |
fun generate' thy = case cs |
20600 | 661 |
of [] => [] |
20439 | 662 |
| _ => |
22213 | 663 |
generate thy (Funcgr.make thy cs) targets |
22185 | 664 |
(fold_map oooo ensure_def_const') cs; |
20896 | 665 |
fun serialize' [] code seri = |
666 |
seri NONE code |
|
667 |
| serialize' cs code seri = |
|
668 |
seri (SOME cs) code; |
|
20600 | 669 |
val cs = generate' thy; |
20699 | 670 |
val code = Code.get thy; |
18217 | 671 |
in |
21081 | 672 |
(map (serialize' cs code) (map_filter snd seris'); ()) |
18217 | 673 |
end; |
674 |
||
22507 | 675 |
fun print_codethms_cmd thy = |
22213 | 676 |
print_codethms thy o map (CodegenConsts.read_const thy); |
20699 | 677 |
|
22507 | 678 |
fun code_deps_cmd thy = |
679 |
code_deps thy o map (CodegenConsts.read_const thy); |
|
18217 | 680 |
|
22213 | 681 |
val code_exprP = ( |
21916 | 682 |
(Scan.repeat P.term |
20439 | 683 |
-- Scan.repeat (P.$$$ "(" |-- |
20896 | 684 |
P.name -- P.arguments |
21881 | 685 |
--| P.$$$ ")")) |
686 |
>> (fn (raw_cs, seris) => code raw_cs seris) |
|
20439 | 687 |
); |
688 |
||
22507 | 689 |
val (codeK, code_abstypeK, code_axiomsK, code_thmsK, code_depsK) = |
690 |
("code_gen", "code_abstype", "code_axioms", "code_thms", "code_deps"); |
|
22213 | 691 |
|
692 |
in |
|
693 |
||
21881 | 694 |
val codeP = |
695 |
OuterSyntax.improper_command codeK "generate and serialize executable code for constants" |
|
22213 | 696 |
K.diag (P.!!! code_exprP >> (fn f => Toplevel.keep (f o Toplevel.theory_of))); |
21881 | 697 |
|
698 |
fun codegen_command thy cmd = |
|
22213 | 699 |
case Scan.read OuterLex.stopper (P.!!! code_exprP) ((filter OuterLex.is_proper o OuterSyntax.scan) cmd) |
21916 | 700 |
of SOME f => (writeln "Now generating code..."; f thy) |
22197 | 701 |
| NONE => error ("Bad directive " ^ quote cmd); |
21881 | 702 |
|
20931 | 703 |
val code_abstypeP = |
704 |
OuterSyntax.command code_abstypeK "axiomatic abstypes for code generation" K.thy_decl ( |
|
705 |
(P.typ -- P.typ -- Scan.optional (P.$$$ "where" |-- Scan.repeat1 |
|
706 |
(P.term --| (P.$$$ "\\<equiv>" || P.$$$ "==") -- P.term)) []) |
|
707 |
>> (Toplevel.theory o uncurry abstyp_e) |
|
20428 | 708 |
); |
709 |
||
21062 | 710 |
val code_axiomsP = |
711 |
OuterSyntax.command code_axiomsK "axiomatic constant equalities for code generation" K.thy_decl ( |
|
20931 | 712 |
Scan.repeat1 (P.term --| (P.$$$ "\\<equiv>" || P.$$$ "==") -- P.term) |
713 |
>> (Toplevel.theory o constsubst_e) |
|
18217 | 714 |
); |
715 |
||
22305 | 716 |
val code_thmsP = |
717 |
OuterSyntax.improper_command code_thmsK "print cached defining equations" OuterKeyword.diag |
|
718 |
(Scan.repeat P.term |
|
719 |
>> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory |
|
22507 | 720 |
o Toplevel.keep ((fn thy => print_codethms_cmd thy cs) o Toplevel.theory_of))); |
22213 | 721 |
|
22507 | 722 |
val code_depsP = |
723 |
OuterSyntax.improper_command code_depsK "visualize dependencies of defining equations" OuterKeyword.diag |
|
724 |
(Scan.repeat P.term |
|
725 |
>> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory |
|
726 |
o Toplevel.keep ((fn thy => code_deps_cmd thy cs) o Toplevel.theory_of))); |
|
727 |
||
728 |
val _ = OuterSyntax.add_parsers [codeP, code_abstypeP, code_axiomsP, code_thmsP, code_depsP]; |
|
18217 | 729 |
|
730 |
end; (* local *) |
|
731 |
||
732 |
end; (* struct *) |