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