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