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