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