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