author | haftmann |
Mon, 23 Jan 2006 14:06:40 +0100 | |
changeset 18756 | 5eb3df798405 |
parent 18708 | 4b3dadb4fe33 |
child 18850 | 92ef83e5eaea |
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 |
|
18217 | 5 |
Code generator from Isabelle theories to |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
6 |
intermediate language ("Thin-gol"). |
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 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
9 |
signature CODEGEN_PACKAGE = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
10 |
sig |
18454 | 11 |
type auxtab; |
18702 | 12 |
type eqextr = theory -> auxtab |
13 |
-> (string * typ) -> (thm list * typ) option; |
|
18217 | 14 |
type defgen; |
18702 | 15 |
type appgen = theory -> auxtab |
18756 | 16 |
-> (string * typ) * term list -> CodegenThingol.transact |
17 |
-> CodegenThingol.iexpr * CodegenThingol.transact; |
|
18702 | 18 |
|
18516 | 19 |
val add_appconst: string * ((int * int) * appgen) -> theory -> theory; |
18517 | 20 |
val add_appconst_i: xstring * ((int * int) * appgen) -> theory -> theory; |
18702 | 21 |
val add_eqextr: string * eqextr -> theory -> theory; |
18217 | 22 |
val add_defgen: string * defgen -> theory -> theory; |
18517 | 23 |
val add_prim_class: xstring -> string list -> (string * string) |
24 |
-> theory -> theory; |
|
25 |
val add_prim_tyco: xstring -> string list -> (string * string) |
|
26 |
-> theory -> theory; |
|
27 |
val add_prim_const: xstring * string option -> string list -> (string * string) |
|
28 |
-> theory -> theory; |
|
29 |
val add_prim_i: string -> string list -> (string * Pretty.T) |
|
30 |
-> theory -> theory; |
|
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
31 |
val add_pretty_list: string -> string -> string * (int * string) |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
32 |
-> theory -> theory; |
18217 | 33 |
val add_alias: string * string -> theory -> theory; |
34 |
val set_is_datatype: (theory -> string -> bool) -> theory -> theory; |
|
18455 | 35 |
val set_get_all_datatype_cons : (theory -> (string * string) list) |
18454 | 36 |
-> theory -> theory; |
18702 | 37 |
val set_int_tyco: string -> theory -> theory; |
18217 | 38 |
|
18516 | 39 |
val exprgen_type: theory -> auxtab |
18217 | 40 |
-> typ -> CodegenThingol.transact -> CodegenThingol.itype * CodegenThingol.transact; |
18516 | 41 |
val exprgen_term: theory -> auxtab |
18217 | 42 |
-> term -> CodegenThingol.transact -> CodegenThingol.iexpr * CodegenThingol.transact; |
18702 | 43 |
val appgen_default: appgen; |
18217 | 44 |
|
18335 | 45 |
val appgen_let: (int -> term -> term list * term) |
46 |
-> appgen; |
|
47 |
val appgen_split: (int -> term -> term list * term) |
|
48 |
-> appgen; |
|
18702 | 49 |
val appgen_number_of: (term -> term) -> (term -> IntInf.int) -> string -> string |
18335 | 50 |
-> appgen; |
18702 | 51 |
val add_case_const: (theory -> string -> (string * int) list option) -> xstring |
18517 | 52 |
-> theory -> theory; |
18702 | 53 |
val add_case_const_i: (theory -> string -> (string * int) list option) -> string |
18517 | 54 |
-> theory -> theory; |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
55 |
val defgen_datatype: (theory -> string -> ((string * sort) list * string list) option) |
18335 | 56 |
-> (theory -> string * string -> typ list option) |
18217 | 57 |
-> defgen; |
58 |
||
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
59 |
val print_codegen_generated: theory -> unit; |
18454 | 60 |
val rename_inconsistent: theory -> theory; |
18517 | 61 |
val ensure_datatype_case_consts: (theory -> string list) |
62 |
-> (theory -> string -> (string * int) list option) |
|
63 |
-> theory -> theory; |
|
18515 | 64 |
|
65 |
(*debugging purpose only*) |
|
18454 | 66 |
structure InstNameMangler: NAME_MANGLER; |
67 |
structure ConstNameMangler: NAME_MANGLER; |
|
68 |
structure DatatypeconsNameMangler: NAME_MANGLER; |
|
18231 | 69 |
structure CodegenData: THEORY_DATA; |
18454 | 70 |
val mk_tabs: theory -> auxtab; |
71 |
val alias_get: theory -> string -> string; |
|
18515 | 72 |
val idf_of_name: theory -> string -> string -> string; |
73 |
val idf_of_const: theory -> auxtab -> string * typ -> string; |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
74 |
end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
75 |
|
18217 | 76 |
structure CodegenPackage : CODEGEN_PACKAGE = |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
77 |
struct |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
78 |
|
18385 | 79 |
open CodegenThingolOp; |
80 |
infix 8 `%%; |
|
81 |
infixr 6 `->; |
|
82 |
infixr 6 `-->; |
|
83 |
infix 4 `$; |
|
84 |
infix 4 `$$; |
|
85 |
infixr 5 `|->; |
|
86 |
infixr 5 `|-->; |
|
18217 | 87 |
|
88 |
(* auxiliary *) |
|
89 |
||
18454 | 90 |
fun devarify_type ty = (fst o Type.freeze_thaw_type) ty; |
18335 | 91 |
fun devarify_term t = (fst o Type.freeze_thaw) t; |
18454 | 92 |
|
93 |
val is_number = is_some o Int.fromString; |
|
18217 | 94 |
|
18702 | 95 |
fun merge_opt _ (x1, NONE) = x1 |
96 |
| merge_opt _ (NONE, x2) = x2 |
|
97 |
| merge_opt eq (SOME x1, SOME x2) = |
|
98 |
if eq (x1, x2) then SOME x1 else error ("incompatible options during merge"); |
|
18217 | 99 |
|
100 |
||
18702 | 101 |
(* shallow name spaces *) |
18217 | 102 |
|
103 |
val nsp_class = "class"; |
|
18454 | 104 |
val nsp_tyco = "tyco"; |
18217 | 105 |
val nsp_const = "const"; |
18454 | 106 |
val nsp_overl = "overl"; |
107 |
val nsp_dtcon = "dtcon"; |
|
18217 | 108 |
val nsp_mem = "mem"; |
109 |
val nsp_inst = "inst"; |
|
110 |
||
111 |
||
18702 | 112 |
(* code generator basics *) |
18454 | 113 |
|
114 |
structure InstNameMangler = NameManglerFun ( |
|
115 |
type ctxt = theory; |
|
116 |
type src = string * (class * string); |
|
117 |
val ord = prod_ord string_ord (prod_ord string_ord string_ord); |
|
118 |
fun mk thy ((thyname, (cls, tyco)), i) = |
|
119 |
NameSpace.base cls ^ "_" ^ NameSpace.base tyco ^ implode (replicate i "'") |
|
120 |
|> NameSpace.append thyname; |
|
121 |
fun is_valid _ _ = true; |
|
122 |
fun maybe_unique _ _ = NONE; |
|
123 |
fun re_mangle _ dst = error ("no such instance: " ^ quote dst); |
|
124 |
); |
|
125 |
||
126 |
structure ConstNameMangler = NameManglerFun ( |
|
127 |
type ctxt = theory; |
|
128 |
type src = string * (typ * typ); |
|
129 |
val ord = prod_ord string_ord (prod_ord Term.typ_ord Term.typ_ord); |
|
130 |
fun mk thy ((c, (ty_decl, ty)), i) = |
|
131 |
let |
|
132 |
fun mangle (Type (tyco, tys)) = |
|
133 |
NameSpace.base tyco :: Library.flat (List.mapPartial mangle tys) |> SOME |
|
134 |
| mangle _ = |
|
135 |
NONE |
|
136 |
in |
|
137 |
Vartab.empty |
|
138 |
|> Sign.typ_match thy (ty_decl, ty) |
|
139 |
|> map (snd o snd) o Vartab.dest |
|
140 |
|> List.mapPartial mangle |
|
141 |
|> Library.flat |
|
142 |
|> null ? K ["x"] |
|
143 |
|> cons c |
|
144 |
|> space_implode "_" |
|
145 |
|> curry (op ^ o swap) ((implode oo replicate) i "'") |
|
146 |
end; |
|
147 |
fun is_valid _ _ = true; |
|
148 |
fun maybe_unique _ _ = NONE; |
|
149 |
fun re_mangle _ dst = error ("no such constant: " ^ quote dst); |
|
150 |
); |
|
151 |
||
152 |
structure DatatypeconsNameMangler = NameManglerFun ( |
|
153 |
type ctxt = theory; |
|
154 |
type src = string * string; |
|
155 |
val ord = prod_ord string_ord string_ord; |
|
18702 | 156 |
fun mk thy ((co, dtco), i) = |
18454 | 157 |
let |
158 |
fun basename 0 = NameSpace.base co |
|
18455 | 159 |
| basename 1 = NameSpace.base dtco ^ "_" ^ NameSpace.base co |
18454 | 160 |
| basename i = NameSpace.base dtco ^ "_" ^ NameSpace.base co ^ "_" ^ (implode oo replicate) (i-1) "'"; |
161 |
fun strip_dtco name = |
|
162 |
case (rev o NameSpace.unpack) name |
|
163 |
of x1::x2::xs => |
|
164 |
if x2 = NameSpace.base dtco |
|
165 |
then NameSpace.pack (x1::xs) |
|
166 |
else name |
|
167 |
| _ => name; |
|
168 |
in |
|
169 |
NameSpace.append (NameSpace.drop_base dtco) (basename i) |
|
170 |
|> strip_dtco |
|
171 |
end; |
|
172 |
fun is_valid _ _ = true; |
|
173 |
fun maybe_unique _ _ = NONE; |
|
174 |
fun re_mangle _ dst = error ("no such datatype constructor: " ^ quote dst); |
|
175 |
); |
|
176 |
||
18702 | 177 |
type auxtab = ((typ * thm) list Symtab.table * string Symtab.table) |
18454 | 178 |
* (InstNameMangler.T * ((typ * typ list) Symtab.table * ConstNameMangler.T) * DatatypeconsNameMangler.T); |
18702 | 179 |
type eqextr = theory -> auxtab |
180 |
-> (string * typ) -> (thm list * typ) option; |
|
18454 | 181 |
type defgen = theory -> auxtab -> gen_defgen; |
18702 | 182 |
type appgen = theory -> auxtab -> (string * typ) * term list -> transact -> iexpr * transact; |
18217 | 183 |
|
18702 | 184 |
val serializers = ref ( |
185 |
Symtab.empty |
|
186 |
|> Symtab.update ( |
|
187 |
#ml CodegenSerializer.serializers |
|
188 |
|> apsnd (fn seri => seri |
|
189 |
(nsp_dtcon, nsp_class, "") |
|
190 |
[[nsp_class, nsp_tyco], [nsp_const, nsp_overl, nsp_dtcon, nsp_mem, nsp_inst]] |
|
191 |
) |
|
192 |
) |
|
193 |
|> Symtab.update ( |
|
194 |
#haskell CodegenSerializer.serializers |
|
195 |
|> apsnd (fn seri => seri |
|
196 |
nsp_dtcon |
|
197 |
[[nsp_class], [nsp_tyco], [nsp_const, nsp_overl, nsp_mem], [nsp_dtcon], [nsp_inst]] |
|
198 |
) |
|
199 |
) |
|
200 |
); |
|
18217 | 201 |
|
202 |
||
18454 | 203 |
(* theory data for code generator *) |
18217 | 204 |
|
205 |
type gens = { |
|
18516 | 206 |
appconst: ((int * int) * (appgen * stamp)) Symtab.table, |
18702 | 207 |
eqextrs: (string * (eqextr * stamp)) list, |
18217 | 208 |
defgens: (string * (defgen * stamp)) list |
209 |
}; |
|
210 |
||
18702 | 211 |
fun map_gens f { appconst, eqextrs, defgens } = |
18217 | 212 |
let |
18702 | 213 |
val (appconst, eqextrs, defgens) = |
214 |
f (appconst, eqextrs, defgens) |
|
215 |
in { appconst = appconst, eqextrs = eqextrs, defgens = defgens } : gens end; |
|
18217 | 216 |
|
217 |
fun merge_gens |
|
18702 | 218 |
({ appconst = appconst1 , eqextrs = eqextrs1, defgens = defgens1 }, |
219 |
{ appconst = appconst2 , eqextrs = eqextrs2, defgens = defgens2 }) = |
|
18516 | 220 |
{ appconst = Symtab.merge |
221 |
(fn ((bounds1, (_, stamp1)), (bounds2, (_, stamp2))) => bounds1 = bounds2 andalso stamp1 = stamp2) |
|
222 |
(appconst1, appconst2), |
|
18702 | 223 |
eqextrs = AList.merge (op =) (eq_snd (op =)) (eqextrs1, eqextrs2), |
224 |
defgens = AList.merge (op =) (eq_snd (op =)) (defgens1, defgens2) |
|
225 |
} : gens; |
|
18217 | 226 |
|
227 |
type logic_data = { |
|
228 |
is_datatype: ((theory -> string -> bool) * stamp) option, |
|
18455 | 229 |
get_all_datatype_cons: ((theory -> (string * string) list) * stamp) option, |
18217 | 230 |
alias: string Symtab.table * string Symtab.table |
231 |
}; |
|
232 |
||
18454 | 233 |
fun map_logic_data f { is_datatype, get_all_datatype_cons, alias } = |
18217 | 234 |
let |
18454 | 235 |
val ((is_datatype, get_all_datatype_cons), alias) = |
236 |
f ((is_datatype, get_all_datatype_cons), alias) |
|
18702 | 237 |
in { is_datatype = is_datatype, get_all_datatype_cons = get_all_datatype_cons, |
238 |
alias = alias } : logic_data end; |
|
18217 | 239 |
|
240 |
fun merge_logic_data |
|
18702 | 241 |
({ is_datatype = is_datatype1, get_all_datatype_cons = get_all_datatype_cons1, |
242 |
alias = alias1 }, |
|
243 |
{ is_datatype = is_datatype2, get_all_datatype_cons = get_all_datatype_cons2, |
|
244 |
alias = alias2 }) = |
|
18217 | 245 |
let |
246 |
in |
|
247 |
{ is_datatype = merge_opt (eq_snd (op =)) (is_datatype1, is_datatype2), |
|
18454 | 248 |
get_all_datatype_cons = merge_opt (eq_snd (op =)) (get_all_datatype_cons1, get_all_datatype_cons2), |
18217 | 249 |
alias = (Symtab.merge (op =) (fst alias1, fst alias2), |
18304 | 250 |
Symtab.merge (op =) (snd alias1, snd alias2)) } : logic_data |
18217 | 251 |
end; |
252 |
||
18702 | 253 |
type target_data = { |
18516 | 254 |
syntax_tyco: (itype CodegenSerializer.pretty_syntax * stamp) Symtab.table, |
255 |
syntax_const: (iexpr CodegenSerializer.pretty_syntax * stamp) Symtab.table |
|
18217 | 256 |
}; |
257 |
||
18702 | 258 |
fun map_target_data f { syntax_tyco, syntax_const } = |
18217 | 259 |
let |
18517 | 260 |
val (syntax_tyco, syntax_const) = |
261 |
f (syntax_tyco, syntax_const) |
|
18702 | 262 |
in { syntax_tyco = syntax_tyco, syntax_const = syntax_const } : target_data |
18454 | 263 |
end; |
18217 | 264 |
|
18702 | 265 |
fun merge_target_data |
266 |
({ syntax_tyco = syntax_tyco1, syntax_const = syntax_const1 }, |
|
267 |
{ syntax_tyco = syntax_tyco2, syntax_const = syntax_const2 }) = |
|
268 |
{ syntax_tyco = Symtab.merge (eq_snd (op =)) (syntax_tyco1, syntax_tyco2), |
|
269 |
syntax_const = Symtab.merge (eq_snd (op =)) (syntax_const1, syntax_const2) } : target_data; |
|
18217 | 270 |
|
271 |
structure CodegenData = TheoryDataFun |
|
272 |
(struct |
|
273 |
val name = "Pure/codegen_package"; |
|
274 |
type T = { |
|
275 |
modl: module, |
|
276 |
gens: gens, |
|
277 |
logic_data: logic_data, |
|
18702 | 278 |
target_data: target_data Symtab.table |
18217 | 279 |
}; |
280 |
val empty = { |
|
281 |
modl = empty_module, |
|
18702 | 282 |
gens = { appconst = Symtab.empty, eqextrs = [], defgens = [] } : gens, |
18454 | 283 |
logic_data = { is_datatype = NONE, get_all_datatype_cons = NONE, |
284 |
alias = (Symtab.empty, Symtab.empty) } : logic_data, |
|
18702 | 285 |
target_data = |
18217 | 286 |
Symtab.empty |
18702 | 287 |
|> Symtab.fold (fn (target, _) => |
288 |
Symtab.update (target, { syntax_tyco = Symtab.empty, syntax_const = Symtab.empty }) |
|
289 |
) (! serializers) |
|
18217 | 290 |
} : T; |
291 |
val copy = I; |
|
292 |
val extend = I; |
|
293 |
fun merge _ ( |
|
18702 | 294 |
{ modl = modl1, gens = gens1, |
295 |
target_data = target_data1, logic_data = logic_data1 }, |
|
296 |
{ modl = modl2, gens = gens2, |
|
297 |
target_data = target_data2, logic_data = logic_data2 } |
|
18217 | 298 |
) = { |
299 |
modl = merge_module (modl1, modl2), |
|
300 |
gens = merge_gens (gens1, gens2), |
|
301 |
logic_data = merge_logic_data (logic_data1, logic_data2), |
|
18702 | 302 |
target_data = Symtab.join (K (merge_target_data #> SOME)) |
303 |
(target_data1, target_data2) |
|
18217 | 304 |
}; |
18708 | 305 |
fun print _ _ = (); |
18217 | 306 |
end); |
307 |
||
18708 | 308 |
val _ = Context.add_setup CodegenData.init; |
309 |
||
18217 | 310 |
fun map_codegen_data f thy = |
311 |
case CodegenData.get thy |
|
18702 | 312 |
of { modl, gens, target_data, logic_data } => |
313 |
let val (modl, gens, target_data, logic_data) = |
|
314 |
f (modl, gens, target_data, logic_data) |
|
315 |
in CodegenData.put { modl = modl, gens = gens, |
|
316 |
target_data = target_data, logic_data = logic_data } thy end; |
|
18217 | 317 |
|
18517 | 318 |
fun print_codegen_generated thy = |
319 |
let |
|
320 |
val module = (#modl o CodegenData.get) thy; |
|
321 |
in |
|
322 |
(writeln o Pretty.output o Pretty.chunks) [pretty_module module, pretty_deps module] |
|
323 |
end; |
|
18217 | 324 |
|
18516 | 325 |
fun gen_add_appconst prep_const (raw_c, (bounds, ag)) thy = |
326 |
let |
|
327 |
val c = prep_const thy raw_c; |
|
328 |
in map_codegen_data |
|
18702 | 329 |
(fn (modl, gens, target_data, logic_data) => |
18217 | 330 |
(modl, |
331 |
gens |> map_gens |
|
18702 | 332 |
(fn (appconst, eqextrs, defgens) => |
18516 | 333 |
(appconst |
334 |
|> Symtab.update (c, (bounds, (ag, stamp ()))), |
|
18702 | 335 |
eqextrs, defgens)), target_data, logic_data)) thy |
18516 | 336 |
end; |
18217 | 337 |
|
18516 | 338 |
val add_appconst = gen_add_appconst Sign.intern_const; |
339 |
val add_appconst_i = gen_add_appconst (K I); |
|
18217 | 340 |
|
341 |
fun add_defgen (name, dg) = |
|
342 |
map_codegen_data |
|
18702 | 343 |
(fn (modl, gens, target_data, logic_data) => |
18217 | 344 |
(modl, |
345 |
gens |> map_gens |
|
18702 | 346 |
(fn (appconst, eqextrs, defgens) => |
347 |
(appconst, eqextrs, defgens |
|
18335 | 348 |
|> Output.update_warn (op =) ("overwriting existing definition definition generator " ^ name) (name, (dg, stamp ())))), |
18702 | 349 |
target_data, logic_data)); |
18217 | 350 |
|
18454 | 351 |
fun get_defgens thy tabs = |
352 |
(map (apsnd (fn (dg, _) => dg thy tabs)) o #defgens o #gens o CodegenData.get) thy; |
|
18217 | 353 |
|
18702 | 354 |
fun add_eqextr (name, eqx) = |
18217 | 355 |
map_codegen_data |
18702 | 356 |
(fn (modl, gens, target_data, logic_data) => |
357 |
(modl, |
|
358 |
gens |> map_gens |
|
359 |
(fn (appconst, eqextrs, defgens) => |
|
360 |
(appconst, eqextrs |
|
361 |
|> Output.update_warn (op =) ("overwriting existing equation extractor " ^ name) (name, (eqx, stamp ())), |
|
362 |
defgens)), |
|
363 |
target_data, logic_data)); |
|
18217 | 364 |
|
18702 | 365 |
fun get_eqextrs thy tabs = |
366 |
(map (fn (_, (eqx, _)) => eqx thy tabs) o #eqextrs o #gens o CodegenData.get) thy; |
|
18454 | 367 |
|
368 |
fun is_datatype thy = |
|
369 |
case (#is_datatype o #logic_data o CodegenData.get) thy |
|
370 |
of NONE => K false |
|
371 |
| SOME (f, _) => f thy; |
|
372 |
||
373 |
fun get_all_datatype_cons thy = |
|
374 |
case (#get_all_datatype_cons o #logic_data o CodegenData.get) thy |
|
375 |
of NONE => [] |
|
376 |
| SOME (f, _) => f thy; |
|
18217 | 377 |
|
378 |
fun add_alias (src, dst) = |
|
379 |
map_codegen_data |
|
18702 | 380 |
(fn (modl, gens, target_data, logic_data) => |
381 |
(modl, gens, target_data, |
|
18217 | 382 |
logic_data |> map_logic_data |
383 |
(apsnd (fn (tab, tab_rev) => |
|
384 |
(tab |> Symtab.update (src, dst), |
|
385 |
tab_rev |> Symtab.update (dst, src)))))); |
|
386 |
||
387 |
||
18454 | 388 |
(* name handling *) |
18217 | 389 |
|
18454 | 390 |
val alias_get = perhaps o Symtab.lookup o fst o #alias o #logic_data o CodegenData.get; |
391 |
val alias_rev = perhaps o Symtab.lookup o snd o #alias o #logic_data o CodegenData.get; |
|
18217 | 392 |
|
18454 | 393 |
fun add_nsp shallow name = |
394 |
name |
|
395 |
|> NameSpace.unpack |
|
396 |
|> split_last |
|
397 |
|> apsnd (single #> cons shallow) |
|
398 |
|> (op @) |
|
399 |
|> NameSpace.pack; |
|
18702 | 400 |
|
18454 | 401 |
fun dest_nsp nsp idf = |
18217 | 402 |
let |
403 |
val idf' = NameSpace.unpack idf; |
|
404 |
val (idf'', idf_base) = split_last idf'; |
|
405 |
val (modl, shallow) = split_last idf''; |
|
406 |
in |
|
407 |
if nsp = shallow |
|
18454 | 408 |
then (SOME o NameSpace.pack) (modl @ [idf_base]) |
18217 | 409 |
else NONE |
410 |
end; |
|
411 |
||
18454 | 412 |
fun idf_of_name thy shallow name = |
18702 | 413 |
name |
414 |
|> alias_get thy |
|
415 |
|> add_nsp shallow; |
|
416 |
||
18454 | 417 |
fun name_of_idf thy shallow idf = |
418 |
idf |
|
419 |
|> dest_nsp shallow |
|
420 |
|> Option.map (alias_rev thy); |
|
18217 | 421 |
|
18702 | 422 |
fun set_is_datatype f = |
423 |
map_codegen_data |
|
424 |
(fn (modl, gens, target_data, logic_data) => |
|
425 |
(modl, gens, target_data, |
|
426 |
logic_data |
|
427 |
|> map_logic_data (apfst (fn (is_datatype, get_all_datatype_cons) |
|
428 |
=> (SOME (f, stamp ()), get_all_datatype_cons))))); |
|
429 |
||
430 |
fun set_get_all_datatype_cons f = |
|
431 |
map_codegen_data |
|
432 |
(fn (modl, gens, target_data, logic_data) => |
|
433 |
(modl, gens, target_data, |
|
434 |
logic_data |
|
435 |
|> map_logic_data ((apfst (fn (is_datatype, get_all_datatype_cons) |
|
436 |
=> (is_datatype, SOME (f, stamp ()))))))); |
|
437 |
||
438 |
fun set_int_tyco tyco thy = |
|
439 |
(serializers := ( |
|
440 |
! serializers |
|
441 |
|> Symtab.update ( |
|
442 |
#ml CodegenSerializer.serializers |
|
443 |
|> apsnd (fn seri => seri |
|
444 |
(nsp_dtcon, nsp_class, idf_of_name thy nsp_tyco tyco) |
|
445 |
[[nsp_class, nsp_tyco], [nsp_const, nsp_overl, nsp_dtcon, nsp_mem, nsp_inst]] |
|
446 |
) |
|
447 |
) |
|
448 |
); thy); |
|
449 |
||
18516 | 450 |
|
18454 | 451 |
(* code generator instantiation *) |
18217 | 452 |
|
18454 | 453 |
fun ensure_def_class thy tabs cls trns = |
454 |
let |
|
455 |
val cls' = idf_of_name thy nsp_class cls; |
|
456 |
in |
|
457 |
trns |
|
458 |
|> debug 4 (fn _ => "generating class " ^ quote cls) |
|
459 |
|> gen_ensure_def (get_defgens thy tabs) ("generating class " ^ quote cls) cls' |
|
460 |
|> pair cls' |
|
461 |
end; |
|
18217 | 462 |
|
18454 | 463 |
fun ensure_def_inst thy (tabs as (_, (insttab, _, _))) (cls, tyco) trns = |
464 |
let |
|
18702 | 465 |
val thyname = (the o AList.lookup (op =) (ClassPackage.the_instances thy cls)) tyco; |
18454 | 466 |
val inst = idf_of_name thy nsp_inst (InstNameMangler.get thy insttab (thyname, (cls, tyco))); |
467 |
in |
|
468 |
trns |
|
469 |
|> debug 4 (fn _ => "generating instance " ^ quote cls ^ " / " ^ quote tyco) |
|
470 |
|> gen_ensure_def (get_defgens thy tabs) ("generating instance " ^ quote cls ^ " / " ^ quote tyco) inst |
|
471 |
|> pair inst |
|
472 |
end; |
|
18335 | 473 |
|
18454 | 474 |
fun ensure_def_tyco thy tabs tyco trns = |
475 |
let |
|
476 |
val tyco' = idf_of_name thy nsp_tyco tyco; |
|
18702 | 477 |
in |
478 |
trns |
|
479 |
|> debug 4 (fn _ => "generating type constructor " ^ quote tyco) |
|
480 |
|> gen_ensure_def (get_defgens thy tabs) ("generating type constructor " ^ quote tyco) tyco' |
|
481 |
|> pair tyco' |
|
18454 | 482 |
end; |
483 |
||
484 |
fun idf_of_const thy (tabs as ((_, clsmemtab), (_, (overltab1, overltab2), dtcontab))) (c, ty) = |
|
485 |
let |
|
18515 | 486 |
fun get_overloaded (c, ty) = |
487 |
case Symtab.lookup overltab1 c |
|
488 |
of SOME (ty_decl, tys) => |
|
489 |
(case find_first (curry (Sign.typ_instance thy) ty) tys |
|
490 |
of SOME ty' => ConstNameMangler.get thy overltab2 |
|
491 |
(idf_of_name thy nsp_overl c, (ty_decl, ty')) |> SOME |
|
492 |
| _ => NONE) |
|
493 |
| _ => NONE |
|
494 |
fun get_datatypecons (c, ty) = |
|
495 |
case (snd o strip_type) ty |
|
496 |
of Type (tyco, _) => |
|
497 |
try (DatatypeconsNameMangler.get thy dtcontab) (c, tyco) |
|
498 |
| _ => NONE; |
|
18702 | 499 |
in case get_datatypecons (c, ty) |
500 |
of SOME c' => idf_of_name thy nsp_dtcon c' |
|
501 |
| NONE => case get_overloaded (c, ty) |
|
18515 | 502 |
of SOME idf => idf |
503 |
| NONE => case Symtab.lookup clsmemtab c |
|
504 |
of SOME _ => idf_of_name thy nsp_mem c |
|
505 |
| NONE => idf_of_name thy nsp_const c |
|
506 |
end; |
|
18217 | 507 |
|
18454 | 508 |
fun recconst_of_idf thy (_, (_, (_, overltab2), _)) idf = |
509 |
case name_of_idf thy nsp_const idf |
|
18702 | 510 |
of SOME c => SOME (c, Sign.the_const_type thy c) |
18454 | 511 |
| NONE => ( |
512 |
case dest_nsp nsp_overl idf |
|
513 |
of SOME _ => |
|
514 |
idf |
|
515 |
|> ConstNameMangler.rev thy overltab2 |
|
516 |
|> apfst (the o name_of_idf thy nsp_overl) |
|
517 |
|> apsnd snd |
|
518 |
|> SOME |
|
519 |
| NONE => NONE |
|
520 |
); |
|
521 |
||
522 |
fun ensure_def_const thy (tabs as ((_, clsmemtab), (_, overltab, dtcontab))) (c, ty) trns = |
|
523 |
let |
|
524 |
val c' = idf_of_const thy tabs (c, ty); |
|
18702 | 525 |
in |
526 |
trns |
|
527 |
|> debug 4 (fn _ => "generating constant " ^ quote c) |
|
528 |
|> gen_ensure_def (get_defgens thy tabs) ("generating constant " ^ quote c) c' |
|
529 |
|> pair c' |
|
18454 | 530 |
end; |
18217 | 531 |
|
18702 | 532 |
(* fun ensure_def_eq thy tabs (dtco, (eqpred, arity)) trns = |
18454 | 533 |
let |
534 |
val name_dtco = (the ooo name_of_idf) thy nsp_tyco dtco; |
|
535 |
val idf_eqinst = idf_of_name thy nsp_eq_inst name_dtco; |
|
536 |
val idf_eqpred = idf_of_name thy nsp_eq_pred name_dtco; |
|
18515 | 537 |
val inst_sortlookup = map (fn (v, _) => [ClassPackage.Lookup ([], (v, 0))]) arity; |
18454 | 538 |
fun mk_eq_pred _ trns = |
539 |
trns |
|
18702 | 540 |
|> succeed (eqpred) |
18454 | 541 |
fun mk_eq_inst _ trns = |
542 |
trns |
|
543 |
|> gen_ensure_def [("eqpred", mk_eq_pred)] ("generating equality predicate for " ^ quote dtco) idf_eqpred |
|
18702 | 544 |
|> succeed (Classinst ((class_eq, (dtco, arity)), ([], [(fun_eq, (idf_eqpred, inst_sortlookup))]))); |
18454 | 545 |
in |
546 |
trns |
|
547 |
|> gen_ensure_def [("eqinst", mk_eq_inst)] ("generating equality instance for " ^ quote dtco) idf_eqinst |
|
18702 | 548 |
end; *) |
18454 | 549 |
|
18516 | 550 |
|
551 |
(* expression generators *) |
|
552 |
||
18702 | 553 |
fun exprgen_tyvar_sort thy tabs (v, sort) trns = |
18516 | 554 |
trns |
555 |
|> fold_map (ensure_def_class thy tabs) (ClassPackage.syntactic_sort_of thy sort) |
|
18702 | 556 |
|-> (fn sort => pair (unprefix "'" v, sort)); |
18516 | 557 |
|
558 |
fun exprgen_type thy tabs (TVar _) trns = |
|
559 |
error "TVar encountered during code generation" |
|
18702 | 560 |
| exprgen_type thy tabs (TFree v_s) trns = |
18516 | 561 |
trns |
18702 | 562 |
|> exprgen_tyvar_sort thy tabs v_s |
563 |
|-> (fn v_s => pair (IVarT v_s)) |
|
18516 | 564 |
| exprgen_type thy tabs (Type ("fun", [t1, t2])) trns = |
565 |
trns |
|
566 |
|> exprgen_type thy tabs t1 |
|
567 |
||>> exprgen_type thy tabs t2 |
|
568 |
|-> (fn (t1', t2') => pair (t1' `-> t2')) |
|
569 |
| exprgen_type thy tabs (Type (tyco, tys)) trns = |
|
570 |
trns |
|
571 |
|> ensure_def_tyco thy tabs tyco |
|
572 |
||>> fold_map (exprgen_type thy tabs) tys |
|
573 |
|-> (fn (tyco, tys) => pair (tyco `%% tys)); |
|
574 |
||
18517 | 575 |
fun mk_lookup thy tabs (ClassPackage.Instance (inst as (cls, tyco), ls)) trns = |
576 |
trns |
|
577 |
|> ensure_def_class thy tabs cls |
|
578 |
||>> ensure_def_inst thy tabs inst |
|
579 |
||>> (fold_map o fold_map) (mk_lookup thy tabs) ls |
|
580 |
|-> (fn ((cls, i), ls) => pair (ClassPackage.Instance ((cls, i), ls))) |
|
581 |
| mk_lookup thy tabs (ClassPackage.Lookup (clss, (v, i))) trns = |
|
18516 | 582 |
trns |
18517 | 583 |
|> fold_map (ensure_def_class thy tabs) clss |
584 |
|-> (fn clss => pair (ClassPackage.Lookup (clss, (v |> unprefix "'", i)))); |
|
585 |
||
18702 | 586 |
fun appgen_default thy tabs ((c, ty), ts) trns = |
587 |
trns |
|
588 |
|> ensure_def_const thy tabs (c, ty) |
|
589 |
||>> (fold_map o fold_map) (mk_lookup thy tabs) |
|
590 |
(ClassPackage.extract_sortlookup thy (c, ty)) |
|
591 |
||>> exprgen_type thy tabs ty |
|
592 |
||>> fold_map (exprgen_term thy tabs) ts |
|
593 |
|-> (fn (((c, ls), ty), es) => |
|
594 |
pair (Library.foldl IInst ((IConst (c, ty)), ls) `$$ es)) |
|
595 |
and appgen thy tabs ((f, ty), ts) trns = |
|
18517 | 596 |
case Symtab.lookup ((#appconst o #gens o CodegenData.get) thy) f |
597 |
of SOME ((imin, imax), (ag, _)) => |
|
18702 | 598 |
if length ts < imin then |
18517 | 599 |
let |
600 |
val d = imin - length ts; |
|
601 |
val vs = Term.invent_names (add_term_names (Const (f, ty), [])) "x" d; |
|
602 |
val tys = Library.take (d, ((fst o strip_type) ty)); |
|
603 |
in |
|
604 |
trns |
|
605 |
|> debug 10 (fn _ => "eta-expanding") |
|
606 |
|> fold_map (exprgen_type thy tabs) tys |
|
18702 | 607 |
||>> ag thy tabs ((f, ty), ts @ map2 (curry Free) vs tys) |
18517 | 608 |
|-> (fn (tys, e) => pair ((vs ~~ tys) `|--> e)) |
609 |
end |
|
610 |
else if length ts > imax then |
|
611 |
trns |
|
612 |
|> debug 10 (fn _ => "splitting arguments (" ^ string_of_int imax ^ ", " ^ string_of_int (length ts) ^ ")") |
|
18702 | 613 |
|> ag thy tabs ((f, ty), Library.take (imax, ts)) |
18517 | 614 |
||>> fold_map (exprgen_term thy tabs) (Library.drop (imax, ts)) |
615 |
|-> (fn es => pair (mk_apps es)) |
|
616 |
else |
|
617 |
trns |
|
618 |
|> debug 10 (fn _ => "keeping arguments") |
|
18702 | 619 |
|> ag thy tabs ((f, ty), ts) |
18517 | 620 |
| NONE => |
621 |
trns |
|
18702 | 622 |
|> appgen_default thy tabs ((f, ty), ts) |
18517 | 623 |
and exprgen_term thy tabs (Const (f, ty)) trns = |
624 |
trns |
|
625 |
|> appgen thy tabs ((f, ty), []) |
|
18516 | 626 |
|-> (fn e => pair e) |
627 |
| exprgen_term thy tabs (Var ((v, i), ty)) trns = |
|
628 |
trns |
|
629 |
|> exprgen_type thy tabs ty |
|
630 |
|-> (fn ty => pair (IVarE (if i = 0 then v else v ^ "_" ^ string_of_int i, ty))) |
|
631 |
| exprgen_term thy tabs (Free (v, ty)) trns = |
|
632 |
trns |
|
633 |
|> exprgen_type thy tabs ty |
|
634 |
|-> (fn ty => pair (IVarE (v, ty))) |
|
635 |
| exprgen_term thy tabs (Abs (v, ty, t)) trns = |
|
636 |
trns |
|
637 |
|> exprgen_type thy tabs ty |
|
638 |
||>> exprgen_term thy tabs (subst_bound (Free (v, ty), t)) |
|
639 |
|-> (fn (ty, e) => pair ((v, ty) `|-> e)) |
|
640 |
| exprgen_term thy tabs (t as t1 $ t2) trns = |
|
641 |
let |
|
642 |
val (t', ts) = strip_comb t |
|
643 |
in case t' |
|
644 |
of Const (f, ty) => |
|
645 |
trns |
|
18517 | 646 |
|> appgen thy tabs ((f, ty), ts) |
18516 | 647 |
|-> (fn e => pair e) |
648 |
| _ => |
|
649 |
trns |
|
650 |
|> exprgen_term thy tabs t' |
|
651 |
||>> fold_map (exprgen_term thy tabs) ts |
|
652 |
|-> (fn (e, es) => pair (e `$$ es)) |
|
653 |
end; |
|
654 |
||
18454 | 655 |
|
18517 | 656 |
(* application generators *) |
657 |
||
18702 | 658 |
(* fun appgen_eq thy tabs (("op =", Type ("fun", [ty, _])), [t1, t2]) trns = |
18517 | 659 |
trns |
660 |
|> invoke_eq (exprgen_type thy tabs) (ensure_def_eq thy tabs) ty |
|
661 |
|-> (fn false => error ("could not derive equality for " ^ Sign.string_of_typ thy ty) |
|
662 |
| true => fn trns => trns |
|
663 |
|> exprgen_term thy tabs t1 |
|
664 |
||>> exprgen_term thy tabs t2 |
|
18702 | 665 |
|-> (fn (e1, e2) => pair (Fun_eq `$ e1 `$ e2))); *) |
666 |
||
667 |
||
668 |
(* function extractors *) |
|
669 |
||
670 |
fun mk_fun thy tabs (c, ty) trns = |
|
671 |
case get_first (fn eqx => eqx (c, ty)) (get_eqextrs thy tabs) |
|
672 |
of SOME (eq_thms, ty) => |
|
673 |
let |
|
674 |
val sortctxt = ClassPackage.extract_sortctxt thy ty; |
|
675 |
fun dest_eqthm eq_thm = |
|
676 |
eq_thm |
|
677 |
|> prop_of |
|
678 |
|> Logic.dest_equals |
|
679 |
|> apfst (strip_comb #> snd); |
|
680 |
fun mk_eq (args, rhs) trns = |
|
681 |
trns |
|
682 |
|> fold_map (exprgen_term thy tabs o devarify_term) args |
|
683 |
||>> (exprgen_term thy tabs o devarify_term) rhs |
|
684 |
|-> (fn (args, rhs) => pair (map ipat_of_iexpr args, rhs)) |
|
685 |
in |
|
686 |
trns |
|
687 |
|> fold_map (mk_eq o dest_eqthm) eq_thms |
|
688 |
||>> exprgen_type thy tabs (devarify_type ty) |
|
689 |
||>> fold_map (exprgen_tyvar_sort thy tabs) sortctxt |
|
690 |
|-> (fn ((eqs, ty), sortctxt) => (pair o SOME) (eqs, (sortctxt, ty))) |
|
691 |
end |
|
692 |
| NONE => (NONE, trns); |
|
693 |
||
694 |
fun eqextr_defs thy ((deftab, _), _) (c, ty) = |
|
695 |
let |
|
696 |
fun eq_typ (ty1, ty2) = |
|
697 |
Sign.typ_instance thy (ty1, ty2) |
|
698 |
andalso Sign.typ_instance thy (ty2, ty1) |
|
699 |
in |
|
700 |
Option.mapPartial (get_first (fn (ty', thm) => if eq_typ (ty, ty') |
|
701 |
then SOME ([thm], ty') |
|
702 |
else NONE |
|
703 |
)) (Symtab.lookup deftab c) |
|
704 |
end; |
|
18517 | 705 |
|
706 |
||
707 |
(* definition generators *) |
|
18454 | 708 |
|
18702 | 709 |
fun defgen_clsdecl thy (tabs as (_, (insttab, _, _))) cls trns = |
18217 | 710 |
case name_of_idf thy nsp_class cls |
711 |
of SOME cls => |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
712 |
let |
18702 | 713 |
val cs = (snd o ClassPackage.the_consts_sign thy) cls; |
714 |
val sortctxts = map (ClassPackage.extract_sortctxt thy o snd) cs; |
|
715 |
val idfs = map (idf_of_name thy nsp_mem o fst) cs; |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
716 |
in |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
717 |
trns |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
718 |
|> debug 5 (fn _ => "trying defgen class declaration for " ^ quote cls) |
18702 | 719 |
|> fold_map (ensure_def_class thy tabs) (ClassPackage.the_superclasses thy cls) |
720 |
||>> fold_map (exprgen_type thy tabs o snd) cs |
|
721 |
||>> (fold_map o fold_map) (exprgen_tyvar_sort thy tabs) sortctxts |
|
722 |
|-> (fn ((supcls, memtypes), sortctxts) => succeed |
|
723 |
(Class ((supcls, ("a", idfs ~~ (sortctxts ~~ memtypes))), []))) |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
724 |
end |
18217 | 725 |
| _ => |
726 |
trns |
|
727 |
|> fail ("no class definition found for " ^ quote cls); |
|
728 |
||
18702 | 729 |
fun defgen_funs thy tabs c trns = |
730 |
case recconst_of_idf thy tabs c |
|
731 |
of SOME (c, ty) => |
|
732 |
trns |
|
733 |
|> mk_fun thy tabs (c, ty) |
|
734 |
|-> (fn (SOME funn) => succeed (Fun funn) |
|
735 |
| NONE => fail ("no defining equations found for " ^ quote c)) |
|
736 |
| NONE => |
|
737 |
trns |
|
738 |
|> fail ("not a constant: " ^ quote c); |
|
739 |
||
740 |
fun defgen_datatypecons thy (tabs as (_, (_, _, dtcontab))) co trns = |
|
741 |
case Option.map (DatatypeconsNameMangler.rev thy dtcontab) (name_of_idf thy nsp_dtcon co) |
|
742 |
of SOME (co, dtco) => |
|
743 |
trns |
|
744 |
|> debug 5 (fn _ => "trying defgen datatype constructor for " ^ quote co) |
|
745 |
|> ensure_def_tyco thy tabs dtco |
|
746 |
|-> (fn dtco => succeed Undef) |
|
747 |
| _ => |
|
748 |
trns |
|
749 |
|> fail ("not a datatype constructor: " ^ quote co); |
|
750 |
||
18454 | 751 |
fun defgen_clsmem thy tabs m trns = |
752 |
case name_of_idf thy nsp_mem m |
|
753 |
of SOME m => |
|
18360 | 754 |
trns |
18454 | 755 |
|> debug 5 (fn _ => "trying defgen class member for " ^ quote m) |
756 |
|> ensure_def_class thy tabs ((the o ClassPackage.lookup_const_class thy) m) |
|
18702 | 757 |
|-> (fn cls => succeed Undef) |
18217 | 758 |
| _ => |
18454 | 759 |
trns |> fail ("no class member found for " ^ quote m) |
18217 | 760 |
|
18454 | 761 |
fun defgen_clsinst thy (tabs as (_, (insttab, _, _))) inst trns = |
762 |
case Option.map (InstNameMangler.rev thy insttab) (name_of_idf thy nsp_inst inst) |
|
763 |
of SOME (_, (cls, tyco)) => |
|
18217 | 764 |
let |
18702 | 765 |
val (arity, memdefs) = ClassPackage.the_inst_sign thy (cls, tyco); |
766 |
fun gen_membr (m, ty) trns = |
|
767 |
trns |
|
768 |
|> mk_fun thy tabs (m, ty) |
|
769 |
|-> (fn SOME funn => pair (m, funn) |
|
770 |
| NONE => error ("could not derive definition for member " ^ quote m)); |
|
18217 | 771 |
in |
18702 | 772 |
trns |
18231 | 773 |
|> debug 5 (fn _ => "trying defgen class instance for (" ^ quote cls ^ ", " ^ quote tyco ^ ")") |
18702 | 774 |
|> ensure_def_class thy tabs cls |
18454 | 775 |
||>> ensure_def_tyco thy tabs tyco |
18702 | 776 |
||>> fold_map (exprgen_tyvar_sort thy tabs) arity |
777 |
||>> fold_map gen_membr memdefs |
|
778 |
|-> (fn (((cls, tyco), arity), memdefs) => |
|
779 |
succeed (Classinst ((cls, (tyco, arity)), memdefs))) |
|
18217 | 780 |
end |
781 |
| _ => |
|
18454 | 782 |
trns |> fail ("no class instance found for " ^ quote inst); |
18217 | 783 |
|
784 |
||
785 |
(* parametrized generators, for instantiation in HOL *) |
|
786 |
||
18702 | 787 |
fun appgen_let strip_abs thy tabs ((c, ty), [t2, t3]) trns = |
18517 | 788 |
let |
18702 | 789 |
fun dest_let (l as Const (c', _) $ t $ u) = |
790 |
if c = c' then |
|
791 |
case strip_abs 1 u |
|
792 |
of ([p], u') => apfst (cons (p, t)) (dest_let u') |
|
793 |
| _ => ([], l) |
|
794 |
else ([], t) |
|
18517 | 795 |
| dest_let t = ([], t); |
796 |
fun mk_let (l, r) trns = |
|
18335 | 797 |
trns |
18517 | 798 |
|> exprgen_term thy tabs l |
799 |
||>> exprgen_term thy tabs r |
|
800 |
|-> (fn (l, r) => pair (r, ipat_of_iexpr l)); |
|
18702 | 801 |
val (lets, body) = dest_let (Const (c, ty) $ t2 $ t3) |
18517 | 802 |
in |
803 |
trns |
|
804 |
|> fold_map mk_let lets |
|
805 |
||>> exprgen_term thy tabs body |
|
806 |
|-> (fn (lets, body) => |
|
18702 | 807 |
pair (Library.foldr (fn ((e, p), body) => ICase (e, [(p, body)])) (lets, body))) |
18517 | 808 |
end |
18217 | 809 |
|
18517 | 810 |
fun appgen_split strip_abs thy tabs (c, [t2]) trns = |
811 |
let |
|
812 |
val ([p], body) = strip_abs 1 (Const c $ t2) |
|
813 |
in |
|
814 |
trns |
|
815 |
|> exprgen_term thy tabs p |
|
816 |
||>> exprgen_term thy tabs body |
|
18702 | 817 |
|-> (fn (IVarE v, body) => pair (IAbs (v, body))) |
18517 | 818 |
end; |
18335 | 819 |
|
18702 | 820 |
fun appgen_number_of mk_int_to_nat bin_to_int tyco_int tyco_nat thy tabs ((_, |
821 |
Type (_, [_, ty as Type (tyco, [])])), [bin]) trns = |
|
822 |
if tyco = tyco_int then |
|
823 |
trns |
|
824 |
|> exprgen_type thy tabs ty |
|
825 |
|-> (fn ty => pair (CodegenThingol.IConst ((IntInf.toString o bin_to_int) bin, ty))) |
|
826 |
else if tyco = tyco_nat then |
|
827 |
trns |
|
828 |
|> exprgen_term thy tabs (mk_int_to_nat bin) |
|
829 |
else error ("invalid type constructor for numeral: " ^ quote tyco); |
|
18217 | 830 |
|
18517 | 831 |
fun appgen_datatype_case cos thy tabs ((_, ty), ts) trns = |
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
832 |
let |
18517 | 833 |
val (ts', t) = split_last ts; |
834 |
val (tys, dty) = (split_last o fst o strip_type) ty; |
|
835 |
fun gen_names i = |
|
836 |
variantlist (replicate i "x", foldr add_term_names |
|
837 |
(map (fst o fst o dest_Var) (foldr add_term_vars [] ts)) ts); |
|
838 |
fun cg_case_d (((cname, i), ty), t) trns = |
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
839 |
let |
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
840 |
val vs = gen_names i; |
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
841 |
val tys = Library.take (i, (fst o strip_type) ty); |
18330 | 842 |
val frees = map2 (curry Free) vs tys; |
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
843 |
val t' = Envir.beta_norm (list_comb (t, frees)); |
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
844 |
in |
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
845 |
trns |
18516 | 846 |
|> exprgen_term thy tabs (list_comb (Const (cname, tys ---> dty), frees)) |
847 |
||>> exprgen_term thy tabs t' |
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
848 |
|-> (fn (ep, e) => pair (ipat_of_iexpr ep, e)) |
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
849 |
end; |
18517 | 850 |
in |
851 |
trns |
|
852 |
|> exprgen_term thy tabs t |
|
853 |
||>> fold_map cg_case_d ((cos ~~ tys) ~~ ts') |
|
18702 | 854 |
|-> (fn (t, ds) => pair (ICase (t, ds))) |
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
855 |
end; |
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
856 |
|
18702 | 857 |
fun gen_add_case_const prep_c get_case_const_data raw_c thy = |
18517 | 858 |
let |
859 |
val c = prep_c thy raw_c; |
|
18702 | 860 |
val (tys, dty) = (split_last o fst o strip_type o Sign.the_const_type thy) c; |
18517 | 861 |
val cos = (the o get_case_const_data thy) c; |
862 |
val n_eta = length cos + 1; |
|
863 |
in |
|
864 |
thy |
|
865 |
|> add_appconst_i (c, ((n_eta, n_eta), appgen_datatype_case cos)) |
|
866 |
end; |
|
867 |
||
18702 | 868 |
val add_case_const = gen_add_case_const Sign.intern_const; |
869 |
val add_case_const_i = gen_add_case_const (K I); |
|
18517 | 870 |
|
18454 | 871 |
fun defgen_datatype get_datatype get_datacons thy (tabs as (_, (_, _, dtcontab))) dtco trns = |
872 |
case name_of_idf thy nsp_tyco dtco |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
873 |
of SOME dtco => |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
874 |
(case get_datatype thy dtco |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
875 |
of SOME (vars, cos) => |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
876 |
let |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
877 |
val cotys = map (the o get_datacons thy o rpair dtco) cos; |
18454 | 878 |
val coidfs = map (fn co => (DatatypeconsNameMangler.get thy dtcontab (co, dtco)) |> |
879 |
idf_of_name thy nsp_dtcon) cos; |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
880 |
in |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
881 |
trns |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
882 |
|> debug 5 (fn _ => "trying defgen datatype for " ^ quote dtco) |
18702 | 883 |
|> fold_map (exprgen_tyvar_sort thy tabs) vars |
18516 | 884 |
||>> (fold_map o fold_map) (exprgen_type thy tabs o devarify_type) cotys |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
885 |
|-> (fn (sorts, tys) => succeed (Datatype |
18702 | 886 |
((sorts, coidfs ~~ tys), []))) |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
887 |
end |
18217 | 888 |
| NONE => |
889 |
trns |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
890 |
|> fail ("no datatype found for " ^ quote dtco)) |
18217 | 891 |
| NONE => |
892 |
trns |
|
18454 | 893 |
|> fail ("not a type constructor: " ^ quote dtco) |
18335 | 894 |
|
18217 | 895 |
|
18516 | 896 |
|
897 |
(** theory interface **) |
|
18217 | 898 |
|
18454 | 899 |
fun mk_tabs thy = |
18217 | 900 |
let |
18454 | 901 |
fun extract_defs thy = |
18217 | 902 |
let |
18702 | 903 |
fun dest tm = |
18454 | 904 |
let |
18702 | 905 |
val (lhs, rhs) = Logic.dest_equals (prop_of tm); |
906 |
val (t, args) = strip_comb lhs; |
|
907 |
val (c, ty) = dest_Const t |
|
908 |
in if forall is_Var args then SOME ((c, ty), tm) else NONE |
|
18454 | 909 |
end handle TERM _ => NONE; |
910 |
fun prep_def def = (case Codegen.preprocess thy [def] of |
|
18702 | 911 |
[def'] => def' | _ => error "mk_auxtab: bad preprocessor"); |
912 |
fun add_def (name, _) = |
|
913 |
case (dest o prep_def o Thm.get_axiom thy) name |
|
914 |
of SOME ((c, ty), tm) => |
|
915 |
Symtab.default (c, []) #> Symtab.map_entry c (cons (ty, tm)) |
|
916 |
| NONE => I |
|
18217 | 917 |
in |
18454 | 918 |
Symtab.empty |
18702 | 919 |
|> fold (Symtab.fold add_def o snd o #axioms o Theory.rep_theory) |
920 |
(thy :: Theory.ancestors_of thy) |
|
18217 | 921 |
end; |
18454 | 922 |
fun mk_insttab thy = |
923 |
InstNameMangler.empty |
|
924 |
|> Symtab.fold_map |
|
18702 | 925 |
(fn (cls, (clsmems, clsinsts)) => fold_map |
18454 | 926 |
(fn (tyco, thyname) => InstNameMangler.declare thy (thyname, (cls, tyco))) clsinsts) |
927 |
(ClassPackage.get_classtab thy) |
|
928 |
|-> (fn _ => I); |
|
18702 | 929 |
fun mk_overltabs thy deftab = |
18454 | 930 |
(Symtab.empty, ConstNameMangler.empty) |
931 |
|> Symtab.fold |
|
932 |
(fn (c, [_]) => I |
|
933 |
| (c, tytab) => |
|
934 |
(fn (overltab1, overltab2) => ( |
|
935 |
overltab1 |
|
936 |
|> Symtab.update_new (c, (Sign.the_const_constraint thy c, map fst tytab)), |
|
937 |
overltab2 |
|
18756 | 938 |
|> fold (fn (ty, _) => ConstNameMangler.declare thy |
939 |
(idf_of_name thy nsp_overl c, (Sign.the_const_constraint thy c, ty)) #> snd) tytab |
|
18702 | 940 |
))) deftab; |
18454 | 941 |
fun mk_dtcontab thy = |
942 |
DatatypeconsNameMangler.empty |
|
943 |
|> fold_map |
|
18455 | 944 |
(fn (_, co_dtco) => DatatypeconsNameMangler.declare_multi thy co_dtco) |
945 |
(fold (fn (co, dtco) => |
|
946 |
let |
|
947 |
val key = ((NameSpace.drop_base o NameSpace.drop_base) co, NameSpace.base co) |
|
948 |
in AList.default (op =) (key, []) #> AList.map_entry (op =) key (cons (co, dtco)) end |
|
949 |
) (get_all_datatype_cons thy) []) |
|
18454 | 950 |
|-> (fn _ => I); |
951 |
fun mk_clsmemtab thy = |
|
952 |
Symtab.empty |
|
953 |
|> Symtab.fold |
|
954 |
(fn (class, (clsmems, _)) => fold |
|
955 |
(fn clsmem => Symtab.update (clsmem, class)) clsmems) |
|
956 |
(ClassPackage.get_classtab thy); |
|
18702 | 957 |
val deftab = extract_defs thy; |
18454 | 958 |
val insttab = mk_insttab thy; |
18702 | 959 |
val overltabs = mk_overltabs thy deftab; |
18454 | 960 |
val dtcontab = mk_dtcontab thy; |
961 |
val clsmemtab = mk_clsmemtab thy; |
|
962 |
in ((deftab, clsmemtab), (insttab, overltabs, dtcontab)) end; |
|
18217 | 963 |
|
18756 | 964 |
fun get_serializer target = |
965 |
case Symtab.lookup (!serializers) target |
|
966 |
of SOME seri => seri |
|
967 |
| NONE => error ("unknown code target language: " ^ quote target); |
|
18335 | 968 |
|
18516 | 969 |
fun map_module f = |
18702 | 970 |
map_codegen_data (fn (modl, gens, target_data, logic_data) => |
971 |
(f modl, gens, target_data, logic_data)); |
|
18516 | 972 |
|
18702 | 973 |
fun expand_module gen thy = |
18516 | 974 |
(#modl o CodegenData.get) thy |
18702 | 975 |
|> start_transact (gen thy (mk_tabs thy)) |
18516 | 976 |
|-> (fn x:'a => fn modl => (x, map_module (K modl) thy)); |
977 |
||
978 |
fun rename_inconsistent thy = |
|
18217 | 979 |
let |
18516 | 980 |
fun get_inconsistent thyname = |
981 |
let |
|
982 |
val thy = theory thyname; |
|
983 |
fun own_tables get = |
|
984 |
(get thy) |
|
985 |
|> fold (Symtab.fold (Symtab.remove (K true)) o get) (Theory.parents_of thy) |
|
986 |
|> Symtab.keys; |
|
987 |
val names = own_tables (#2 o #types o Type.rep_tsig o Sign.tsig_of) |
|
988 |
@ own_tables (#2 o #declarations o Consts.dest o #consts o Sign.rep_sg); |
|
989 |
fun diff names = |
|
990 |
fold (fn name => |
|
991 |
if is_prefix (op =) (NameSpace.unpack thyname) (NameSpace.unpack name) |
|
992 |
then I |
|
993 |
else cons (name, NameSpace.append thyname (NameSpace.base name))) names []; |
|
994 |
in diff names end; |
|
995 |
val inconsistent = map get_inconsistent (ThyInfo.names ()) |> Library.flat; |
|
996 |
fun add (src, dst) thy = |
|
997 |
if (is_some oo Symtab.lookup o fst o #alias o #logic_data o CodegenData.get) thy src |
|
998 |
then (warning ("code generator alias already defined for " ^ quote src ^ ", will not overwrite"); thy) |
|
999 |
else add_alias (src, dst) thy |
|
1000 |
in fold add inconsistent thy end; |
|
1001 |
||
18517 | 1002 |
fun ensure_datatype_case_consts get_datatype_case_consts get_case_const_data thy = |
1003 |
let |
|
1004 |
fun ensure case_c thy = |
|
1005 |
if |
|
1006 |
Symtab.defined ((#appconst o #gens o CodegenData.get) thy) case_c |
|
1007 |
then |
|
1008 |
(warning ("case constant " ^ quote case_c ^ " already present in application table, will not overwrite"); thy) |
|
1009 |
else |
|
18702 | 1010 |
add_case_const_i get_case_const_data case_c thy; |
18517 | 1011 |
in |
1012 |
fold ensure (get_datatype_case_consts thy) thy |
|
1013 |
end; |
|
1014 |
||
18516 | 1015 |
|
1016 |
||
1017 |
(** target languages **) |
|
1018 |
||
1019 |
(* primitive definitions *) |
|
1020 |
||
18702 | 1021 |
fun read_typ thy = |
1022 |
Sign.read_typ (thy, K NONE); |
|
1023 |
||
18517 | 1024 |
fun read_const thy (raw_c, raw_ty) = |
1025 |
let |
|
1026 |
val c = Sign.intern_const thy raw_c; |
|
18702 | 1027 |
val _ = if Sign.declared_const thy c |
1028 |
then () |
|
1029 |
else error ("no such constant: " ^ quote c); |
|
18517 | 1030 |
val ty = case raw_ty |
1031 |
of NONE => Sign.the_const_constraint thy c |
|
18702 | 1032 |
| SOME raw_ty => read_typ thy raw_ty; |
18517 | 1033 |
in (c, ty) end; |
1034 |
||
18702 | 1035 |
fun read_quote reader gen raw thy = |
1036 |
expand_module |
|
1037 |
(fn thy => fn tabs => gen thy tabs (reader thy raw)) |
|
1038 |
thy; |
|
1039 |
||
18517 | 1040 |
fun gen_add_prim prep_name prep_primdef raw_name deps (target, raw_primdef) thy = |
18516 | 1041 |
let |
18702 | 1042 |
val _ = if Symtab.defined ((#target_data o CodegenData.get) thy) target |
18516 | 1043 |
then () else error ("unknown target language: " ^ quote target); |
1044 |
val tabs = mk_tabs thy; |
|
1045 |
val name = prep_name thy tabs raw_name; |
|
1046 |
val primdef = prep_primdef raw_primdef; |
|
18217 | 1047 |
in |
18516 | 1048 |
thy |
18517 | 1049 |
|> map_module (CodegenThingol.add_prim name deps (target, primdef)) |
18217 | 1050 |
end; |
1051 |
||
18516 | 1052 |
val add_prim_i = gen_add_prim ((K o K) I) I; |
1053 |
val add_prim_class = gen_add_prim |
|
1054 |
(fn thy => K (idf_of_name thy nsp_class o Sign.intern_class thy)) |
|
18702 | 1055 |
(Pretty.str o CodegenSerializer.parse_targetdef I); |
18516 | 1056 |
val add_prim_tyco = gen_add_prim |
1057 |
(fn thy => K (idf_of_name thy nsp_tyco o Sign.intern_type thy)) |
|
18702 | 1058 |
(Pretty.str o CodegenSerializer.parse_targetdef I); |
18516 | 1059 |
val add_prim_const = gen_add_prim |
18517 | 1060 |
(fn thy => fn tabs => idf_of_const thy tabs o read_const thy) |
18702 | 1061 |
(Pretty.str o CodegenSerializer.parse_targetdef I); |
18516 | 1062 |
|
18702 | 1063 |
val ensure_prim = (map_module oo CodegenThingol.ensure_prim); |
18217 | 1064 |
|
18517 | 1065 |
|
18217 | 1066 |
(* syntax *) |
1067 |
||
18702 | 1068 |
val parse_syntax_tyco = |
18217 | 1069 |
let |
18702 | 1070 |
fun mk reader raw_tyco target thy = |
1071 |
let |
|
18756 | 1072 |
val _ = get_serializer target; |
18702 | 1073 |
fun check_tyco tyco = |
1074 |
if Sign.declared_tyname thy tyco |
|
1075 |
then tyco |
|
1076 |
else error ("no such type constructor: " ^ quote tyco); |
|
1077 |
fun prep_tyco thy tyco = |
|
1078 |
tyco |
|
1079 |
|> Sign.intern_type thy |
|
1080 |
|> check_tyco |
|
1081 |
|> idf_of_name thy nsp_tyco; |
|
1082 |
val tyco = prep_tyco thy raw_tyco; |
|
1083 |
in |
|
1084 |
thy |
|
1085 |
|> ensure_prim tyco target |
|
1086 |
|> reader |
|
1087 |
|-> (fn pretty => map_codegen_data |
|
1088 |
(fn (modl, gens, target_data, logic_data) => |
|
1089 |
(modl, gens, |
|
1090 |
target_data |> Symtab.map_entry target |
|
1091 |
(map_target_data |
|
1092 |
(fn (syntax_tyco, syntax_const) => |
|
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1093 |
(syntax_tyco |> Symtab.update |
18702 | 1094 |
(tyco, (pretty, stamp ())), |
1095 |
syntax_const))), |
|
1096 |
logic_data))) |
|
1097 |
end; |
|
18217 | 1098 |
in |
18702 | 1099 |
CodegenSerializer.parse_syntax (read_quote read_typ exprgen_type) |
1100 |
#-> (fn reader => pair (mk reader)) |
|
18217 | 1101 |
end; |
1102 |
||
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1103 |
fun add_pretty_syntax_const c target pretty = |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1104 |
map_codegen_data |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1105 |
(fn (modl, gens, target_data, logic_data) => |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1106 |
(modl, gens, |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1107 |
target_data |> Symtab.map_entry target |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1108 |
(map_target_data |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1109 |
(fn (syntax_tyco, syntax_const) => |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1110 |
(syntax_tyco, |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1111 |
syntax_const |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1112 |
|> Symtab.update |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1113 |
(c, (pretty, stamp ()))))), |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1114 |
logic_data)); |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1115 |
|
18702 | 1116 |
val parse_syntax_const = |
18217 | 1117 |
let |
18702 | 1118 |
fun mk reader raw_const target thy = |
1119 |
let |
|
18756 | 1120 |
val _ = get_serializer target; |
18702 | 1121 |
val tabs = mk_tabs thy; |
1122 |
val c = idf_of_const thy tabs (read_const thy raw_const); |
|
1123 |
in |
|
1124 |
thy |
|
1125 |
|> ensure_prim c target |
|
1126 |
|> reader |
|
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1127 |
|-> (fn pretty => add_pretty_syntax_const c target pretty) |
18702 | 1128 |
end; |
18217 | 1129 |
in |
18702 | 1130 |
CodegenSerializer.parse_syntax (read_quote Sign.read_term exprgen_term) |
1131 |
#-> (fn reader => pair (mk reader)) |
|
18217 | 1132 |
end; |
1133 |
||
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1134 |
fun add_pretty_list raw_nil raw_cons (target, seri) thy = |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1135 |
let |
18756 | 1136 |
val _ = get_serializer target; |
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1137 |
val tabs = mk_tabs thy; |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1138 |
fun mk_const raw_name = |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1139 |
let |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1140 |
val name = Sign.intern_const thy raw_name; |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1141 |
in idf_of_const thy tabs (name, Sign.the_const_type thy name) end; |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1142 |
val nil' = mk_const raw_nil; |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1143 |
val cons' = mk_const raw_cons; |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1144 |
val pr' = CodegenSerializer.pretty_list nil' cons' seri; |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1145 |
in |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1146 |
thy |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1147 |
|> ensure_prim cons' target |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1148 |
|> add_pretty_syntax_const cons' target pr' |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1149 |
end; |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1150 |
|
18217 | 1151 |
|
18516 | 1152 |
|
18756 | 1153 |
(** toplevel interface **) |
1154 |
||
1155 |
local |
|
18217 | 1156 |
|
18756 | 1157 |
fun generate_code (SOME raw_consts) thy = |
1158 |
let |
|
1159 |
val consts = map (read_const thy) raw_consts; |
|
1160 |
fun generate thy tabs = fold_map (ensure_def_const thy tabs) consts |
|
1161 |
in |
|
1162 |
thy |
|
1163 |
|> expand_module generate |
|
1164 |
|-> (fn cs => pair (SOME cs)) |
|
1165 |
end |
|
1166 |
| generate_code NONE thy = |
|
1167 |
(NONE, thy); |
|
1168 |
||
1169 |
fun serialize_code target seri raw_consts thy = |
|
18217 | 1170 |
let |
18756 | 1171 |
fun serialize cs thy = |
18702 | 1172 |
let |
18756 | 1173 |
val module = (#modl o CodegenData.get) thy; |
18702 | 1174 |
val target_data = |
1175 |
thy |
|
1176 |
|> CodegenData.get |
|
1177 |
|> #target_data |
|
1178 |
|> (fn data => (the oo Symtab.lookup) data target); |
|
18756 | 1179 |
in (seri ( |
18702 | 1180 |
(Option.map fst oo Symtab.lookup o #syntax_tyco) target_data, |
1181 |
(Option.map fst oo Symtab.lookup o #syntax_const) target_data |
|
18756 | 1182 |
) "Generated" cs module : unit; thy) end; |
18217 | 1183 |
in |
1184 |
thy |
|
18756 | 1185 |
|> generate_code raw_consts |
1186 |
|-> (fn cs => serialize cs) |
|
18217 | 1187 |
end; |
1188 |
||
1189 |
structure P = OuterParse |
|
1190 |
and K = OuterKeyword |
|
1191 |
||
1192 |
in |
|
1193 |
||
18517 | 1194 |
val (classK, generateK, serializeK, |
1195 |
primclassK, primtycoK, primconstK, |
|
1196 |
syntax_tycoK, syntax_constK, aliasK) = |
|
1197 |
("code_class", "code_generate", "code_serialize", |
|
1198 |
"code_primclass", "code_primtyco", "code_primconst", |
|
1199 |
"code_syntax_tyco", "code_syntax_const", "code_alias"); |
|
18756 | 1200 |
val dependingK = |
1201 |
("depending_on"); |
|
18335 | 1202 |
|
1203 |
val classP = |
|
1204 |
OuterSyntax.command classK "codegen data for classes" K.thy_decl ( |
|
1205 |
P.xname |
|
1206 |
-- ((P.$$$ "\\<Rightarrow>" || P.$$$ "=>") |-- (P.list1 P.name)) |
|
1207 |
-- (Scan.optional ((P.$$$ "\\<Rightarrow>" || P.$$$ "=>") |-- (P.list1 P.name)) []) |
|
1208 |
>> (fn ((name, tycos), consts) => (Toplevel.theory (ClassPackage.add_classentry name consts tycos))) |
|
1209 |
) |
|
18217 | 1210 |
|
1211 |
val generateP = |
|
18282 | 1212 |
OuterSyntax.command generateK "generate executable code for constants" K.thy_decl ( |
18756 | 1213 |
P.$$$ "(" |
1214 |
|-- Scan.repeat1 (P.name -- Scan.option (P.$$$ "::" |-- P.typ)) |
|
1215 |
--| P.$$$ ")" |
|
1216 |
>> (fn raw_consts => |
|
1217 |
Toplevel.theory (generate_code (SOME raw_consts) #> snd)) |
|
18217 | 1218 |
); |
1219 |
||
1220 |
val serializeP = |
|
18282 | 1221 |
OuterSyntax.command serializeK "serialize executable code for constants" K.thy_decl ( |
18217 | 1222 |
P.name |
1223 |
-- Scan.option ( |
|
18756 | 1224 |
P.$$$ "(" |
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
1225 |
|-- Scan.repeat1 (P.name -- Scan.option (P.$$$ "::" |-- P.typ)) |
18756 | 1226 |
--| P.$$$ ")" |
18217 | 1227 |
) |
18756 | 1228 |
#-> (fn (target, raw_consts) => |
1229 |
get_serializer target |
|
1230 |
>> (fn seri => |
|
1231 |
Toplevel.theory (serialize_code target seri raw_consts) |
|
1232 |
)) |
|
18217 | 1233 |
); |
1234 |
||
1235 |
val aliasP = |
|
18282 | 1236 |
OuterSyntax.command aliasK "declare an alias for a theory identifier" K.thy_decl ( |
18702 | 1237 |
Scan.repeat1 (P.name -- P.name) |
1238 |
>> (Toplevel.theory oo fold) add_alias |
|
18217 | 1239 |
); |
1240 |
||
18517 | 1241 |
val primclassP = |
1242 |
OuterSyntax.command primclassK "define target-lanugage specific class" K.thy_decl ( |
|
1243 |
P.xname |
|
18702 | 1244 |
-- Scan.optional (P.$$$ dependingK |-- |
1245 |
P.$$$ "(" |-- P.list1 P.name --| P.$$$ ")") [] |
|
18517 | 1246 |
-- Scan.repeat1 (P.name -- P.text) |
18702 | 1247 |
>> (fn ((raw_class, depends), primdefs) => |
18517 | 1248 |
(Toplevel.theory oo fold) (add_prim_class raw_class depends) primdefs) |
1249 |
); |
|
1250 |
||
1251 |
val primtycoP = |
|
1252 |
OuterSyntax.command primtycoK "define target-lanugage specific type constructor" K.thy_decl ( |
|
1253 |
P.xname |
|
18702 | 1254 |
-- Scan.optional (P.$$$ dependingK |-- |
1255 |
P.$$$ "(" |-- P.list1 P.name --| P.$$$ ")") [] |
|
18517 | 1256 |
-- Scan.repeat1 (P.name -- P.text) |
18702 | 1257 |
>> (fn ((raw_tyco, depends), primdefs) => |
18517 | 1258 |
(Toplevel.theory oo fold) (add_prim_tyco raw_tyco depends) primdefs) |
1259 |
); |
|
1260 |
||
1261 |
val primconstP = |
|
1262 |
OuterSyntax.command primconstK "define target-lanugage specific constant" K.thy_decl ( |
|
18702 | 1263 |
(P.xname -- Scan.option (P.$$$ "::" |-- P.typ)) |
1264 |
-- Scan.optional (P.$$$ dependingK |-- |
|
1265 |
P.$$$ "(" |-- P.list1 P.name --| P.$$$ ")") [] |
|
18517 | 1266 |
-- Scan.repeat1 (P.name -- P.text) |
18702 | 1267 |
>> (fn ((raw_const, depends), primdefs) => |
18517 | 1268 |
(Toplevel.theory oo fold) (add_prim_const raw_const depends) primdefs) |
1269 |
); |
|
1270 |
||
18702 | 1271 |
val _ : OuterParse.token list -> (string -> string -> theory -> theory) * OuterParse.token list |
1272 |
= parse_syntax_tyco; |
|
1273 |
||
18217 | 1274 |
val syntax_tycoP = |
1275 |
OuterSyntax.command syntax_tycoK "define code syntax for type constructor" K.thy_decl ( |
|
18702 | 1276 |
Scan.repeat1 ( |
1277 |
P.xname |
|
1278 |
-- Scan.repeat1 ( |
|
1279 |
P.name -- parse_syntax_tyco |
|
1280 |
) |
|
1281 |
) |
|
1282 |
>> (Toplevel.theory oo fold) (fn (raw_tyco, syns) => |
|
1283 |
fold (fn (target, modifier) => modifier raw_tyco target) syns) |
|
18217 | 1284 |
); |
1285 |
||
1286 |
val syntax_constP = |
|
1287 |
OuterSyntax.command syntax_constK "define code syntax for constant" K.thy_decl ( |
|
18702 | 1288 |
Scan.repeat1 ( |
1289 |
(P.xname -- Scan.option (P.$$$ "::" |-- P.typ)) |
|
1290 |
-- Scan.repeat1 ( |
|
1291 |
P.name -- parse_syntax_const |
|
1292 |
) |
|
1293 |
) |
|
1294 |
>> (Toplevel.theory oo fold) (fn (raw_c, syns) => |
|
1295 |
fold (fn (target, modifier) => modifier raw_c target) syns) |
|
18217 | 1296 |
); |
1297 |
||
18517 | 1298 |
val _ = OuterSyntax.add_parsers [classP, generateP, serializeP, aliasP, |
1299 |
primclassP, primtycoP, primconstP, syntax_tycoP, syntax_constP]; |
|
18756 | 1300 |
val _ = OuterSyntax.add_keywords ["\\<Rightarrow>", "=>", dependingK]; |
18217 | 1301 |
|
1302 |
||
18516 | 1303 |
|
18708 | 1304 |
(** theory setup **) |
18516 | 1305 |
|
18708 | 1306 |
val _ = Context.add_setup |
1307 |
(add_eqextr ("defs", eqextr_defs) #> |
|
1308 |
add_defgen ("clsdecl", defgen_clsdecl) #> |
|
1309 |
add_defgen ("funs", defgen_funs) #> |
|
1310 |
add_defgen ("clsmem", defgen_clsmem) #> |
|
1311 |
add_defgen ("datatypecons", defgen_datatypecons)); |
|
1312 |
||
1313 |
(* add_appconst_i ("op =", ((2, 2), appgen_eq)) *) |
|
1314 |
(* add_defgen ("clsinst", defgen_clsinst) *) |
|
18217 | 1315 |
|
1316 |
end; (* local *) |
|
1317 |
||
1318 |
end; (* struct *) |