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