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