| author | wenzelm |
| Fri, 25 Nov 2005 20:57:51 +0100 | |
| changeset 18259 | 7b14579c58f2 |
| parent 18247 | b17724cae935 |
| child 18282 | 98431741bda3 |
| 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 |
|
| 18217 | 9 |
(*NOTE: for simplifying development, this package contains |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
10 |
some stuff which will finally be moved upwards to HOL*) |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
11 |
|
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
12 |
signature CODEGEN_PACKAGE = |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
13 |
sig |
| 18217 | 14 |
type deftab; |
15 |
type codegen_type; |
|
16 |
type codegen_expr; |
|
17 |
type defgen; |
|
18 |
val add_codegen_type: string * codegen_type -> theory -> theory; |
|
19 |
val add_codegen_expr: string * codegen_expr -> theory -> theory; |
|
20 |
val add_defgen: string * defgen -> theory -> theory; |
|
21 |
val add_lookup_tyco: string * string -> theory -> theory; |
|
22 |
val add_lookup_const: (string * typ) * CodegenThingol.iexpr -> theory -> theory; |
|
23 |
val add_syntax_tyco: string -> (xstring * string) |
|
24 |
* (string option * (string * string list)) option |
|
25 |
-> theory -> theory; |
|
26 |
val add_syntax_tyco_i: string -> (string * CodegenThingol.itype Codegen.mixfix list) |
|
27 |
* (string * (string * string list)) option |
|
28 |
-> theory -> theory; |
|
29 |
val add_syntax_const: string -> ((xstring * string option) * string) |
|
30 |
* (string option * (string * string list)) option |
|
31 |
-> theory -> theory; |
|
32 |
val add_syntax_const_i: string -> (string * CodegenThingol.iexpr Codegen.mixfix list) |
|
33 |
* (string * (string * string list)) option |
|
34 |
-> theory -> theory; |
|
35 |
val add_alias: string * string -> theory -> theory; |
|
36 |
val set_is_datatype: (theory -> string -> bool) -> theory -> theory; |
|
37 |
||
38 |
val idf_of_name: theory -> string -> string -> string; |
|
39 |
val name_of_idf: theory -> string -> string -> string option; |
|
| 18231 | 40 |
val idf_of_inst: theory -> deftab -> class * string -> string; |
41 |
val inst_of_idf: theory -> deftab -> string -> (class * string) option; |
|
| 18217 | 42 |
val idf_of_tname: theory -> string -> string; |
43 |
val tname_of_idf: theory -> string -> string option; |
|
44 |
val idf_of_cname: theory -> deftab -> string * typ -> string; |
|
45 |
val cname_of_idf: theory -> deftab -> string -> (string * typ) option; |
|
46 |
||
47 |
val invoke_cg_type: theory -> deftab |
|
48 |
-> typ -> CodegenThingol.transact -> CodegenThingol.itype * CodegenThingol.transact; |
|
49 |
val invoke_cg_expr: theory -> deftab |
|
50 |
-> term -> CodegenThingol.transact -> CodegenThingol.iexpr * CodegenThingol.transact; |
|
51 |
val ensure_def_tyco: theory -> deftab |
|
52 |
-> string -> CodegenThingol.transact -> string * CodegenThingol.transact; |
|
53 |
val ensure_def_const: theory -> deftab |
|
54 |
-> string -> CodegenThingol.transact -> string * CodegenThingol.transact; |
|
55 |
||
56 |
val codegen_let: (int -> term -> term list * term) |
|
57 |
-> codegen_expr; |
|
58 |
val codegen_split: (int -> term -> term list * term) |
|
59 |
-> codegen_expr; |
|
60 |
val codegen_number_of: (term -> IntInf.int) -> (term -> term) |
|
61 |
-> codegen_expr; |
|
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
62 |
val codegen_case: (theory -> string -> (string * int) list option) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
63 |
-> codegen_expr; |
| 18217 | 64 |
val defgen_datatype: (theory -> string -> (string list * string list) option) |
65 |
-> defgen; |
|
66 |
val defgen_datacons: (theory -> string * string -> typ list option) |
|
67 |
-> defgen; |
|
68 |
val defgen_recfun: (theory -> string * typ -> (term list * term) list * typ) |
|
69 |
-> defgen; |
|
70 |
||
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
71 |
val print_codegen_generated: theory -> unit; |
| 18217 | 72 |
val mk_deftab: theory -> deftab; |
| 18231 | 73 |
structure CodegenData: THEORY_DATA; |
74 |
structure Insttab: TABLE; |
|
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
75 |
end; |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
76 |
|
| 18217 | 77 |
structure CodegenPackage : CODEGEN_PACKAGE = |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
78 |
struct |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
79 |
|
| 18217 | 80 |
open CodegenThingol; |
81 |
||
82 |
(* auxiliary *) |
|
83 |
||
84 |
fun perhaps f x = f x |> the_default x; |
|
85 |
||
86 |
||
87 |
(* code generator instantiation, part 1 *) |
|
88 |
||
89 |
structure Insttab = TableFun( |
|
90 |
type key = string * string |
|
91 |
val ord = prod_ord fast_string_ord fast_string_ord |
|
92 |
); |
|
93 |
||
94 |
type deftab = ((typ * string) list Symtab.table |
|
95 |
* (string * typ) Symtab.table) |
|
96 |
* (term list * term * typ) Symtab.table |
|
97 |
* (string Insttab.table |
|
98 |
* (string * string) Symtab.table |
|
| 18231 | 99 |
* class Symtab.table); |
| 18217 | 100 |
|
| 18231 | 101 |
type codegen_sort = theory -> deftab -> (sort, sort) gen_codegen; |
| 18217 | 102 |
type codegen_type = theory -> deftab -> (typ, itype) gen_codegen; |
103 |
type codegen_expr = theory -> deftab -> (term, iexpr) gen_codegen; |
|
104 |
type defgen = theory -> deftab -> gen_defgen; |
|
105 |
||
106 |
||
107 |
(* namespace conventions *) |
|
108 |
||
109 |
val nsp_class = "class"; |
|
110 |
val nsp_type = "type"; |
|
111 |
val nsp_const = "const"; |
|
112 |
val nsp_mem = "mem"; |
|
113 |
val nsp_inst = "inst"; |
|
114 |
val nsp_eq_class = "eq_class"; |
|
115 |
val nsp_eq = "eq"; |
|
116 |
||
117 |
||
118 |
(* serializer *) |
|
119 |
||
120 |
val serializer_ml = |
|
121 |
let |
|
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
122 |
val name_root = "Generated"; |
| 18217 | 123 |
val nsp_conn_ml = [ |
124 |
[nsp_class, nsp_type, nsp_eq_class], [nsp_const, nsp_inst, nsp_mem, nsp_eq] |
|
125 |
]; |
|
126 |
in CodegenSerializer.ml_from_thingol nsp_conn_ml name_root end; |
|
127 |
||
128 |
fun serializer_hs _ _ _ _ = |
|
129 |
error ("haskell serialization not implemented yet");
|
|
130 |
||
131 |
||
132 |
(* theory data for codegen *) |
|
133 |
||
134 |
type gens = {
|
|
| 18231 | 135 |
codegens_sort: (string * (codegen_sort * stamp)) list, |
| 18217 | 136 |
codegens_type: (string * (codegen_type * stamp)) list, |
137 |
codegens_expr: (string * (codegen_expr * stamp)) list, |
|
138 |
defgens: (string * (defgen * stamp)) list |
|
139 |
}; |
|
140 |
||
141 |
val empty_gens = {
|
|
| 18231 | 142 |
codegens_sort = Symtab.empty, codegens_type = Symtab.empty, |
| 18217 | 143 |
codegens_expr = Symtab.empty, defgens = Symtab.empty |
144 |
}; |
|
145 |
||
| 18231 | 146 |
fun map_gens f { codegens_sort, codegens_type, codegens_expr, defgens } =
|
| 18217 | 147 |
let |
| 18231 | 148 |
val (codegens_sort, codegens_type, codegens_expr, defgens) = |
149 |
f (codegens_sort, codegens_type, codegens_expr, defgens) |
|
150 |
in { codegens_sort = codegens_sort, codegens_type = codegens_type,
|
|
| 18217 | 151 |
codegens_expr = codegens_expr, defgens = defgens } end; |
152 |
||
153 |
fun merge_gens |
|
| 18231 | 154 |
({ codegens_sort = codegens_sort1, codegens_type = codegens_type1,
|
| 18217 | 155 |
codegens_expr = codegens_expr1, defgens = defgens1 }, |
| 18231 | 156 |
{ codegens_sort = codegens_sort2, codegens_type = codegens_type2,
|
| 18217 | 157 |
codegens_expr = codegens_expr2, defgens = defgens2 }) = |
| 18231 | 158 |
{ codegens_sort = AList.merge (op =) (eq_snd (op =)) (codegens_sort1, codegens_sort2),
|
| 18217 | 159 |
codegens_type = AList.merge (op =) (eq_snd (op =)) (codegens_type1, codegens_type2), |
160 |
codegens_expr = AList.merge (op =) (eq_snd (op =)) (codegens_expr1, codegens_expr2), |
|
161 |
defgens = AList.merge (op =) (eq_snd (op =)) (defgens1, defgens2) }; |
|
162 |
||
163 |
type lookups = {
|
|
164 |
lookups_tyco: string Symtab.table, |
|
165 |
lookups_const: (typ * iexpr) list Symtab.table |
|
166 |
} |
|
167 |
||
168 |
val empty_lookups = {
|
|
169 |
lookups_tyco = Symtab.empty, lookups_const = Symtab.empty |
|
170 |
}; |
|
171 |
||
172 |
fun map_lookups f { lookups_tyco, lookups_const } =
|
|
173 |
let |
|
174 |
val (lookups_tyco, lookups_const) = |
|
175 |
f (lookups_tyco, lookups_const) |
|
176 |
in { lookups_tyco = lookups_tyco, lookups_const = lookups_const } end;
|
|
177 |
||
178 |
fun merge_lookups |
|
179 |
({ lookups_tyco = lookups_tyco1, lookups_const = lookups_const1 },
|
|
180 |
{ lookups_tyco = lookups_tyco2, lookups_const = lookups_const2 }) =
|
|
181 |
{ lookups_tyco = Symtab.merge (op =) (lookups_tyco1, lookups_tyco2),
|
|
182 |
lookups_const = Symtab.merge (op =) (lookups_const1, lookups_const2) }; |
|
183 |
||
184 |
type logic_data = {
|
|
185 |
is_datatype: ((theory -> string -> bool) * stamp) option, |
|
186 |
alias: string Symtab.table * string Symtab.table |
|
187 |
}; |
|
188 |
||
189 |
fun map_logic_data f { is_datatype, alias } =
|
|
190 |
let |
|
191 |
val (is_datatype, alias) = |
|
192 |
f (is_datatype, alias) |
|
193 |
in { is_datatype = is_datatype, alias = alias } end;
|
|
194 |
||
195 |
fun merge_logic_data |
|
196 |
({ is_datatype = is_datatype1, alias = alias1 },
|
|
197 |
{ is_datatype = is_datatype2, alias = alias2 }) =
|
|
198 |
let |
|
199 |
fun merge_opt _ (x1, NONE) = x1 |
|
200 |
| merge_opt _ (NONE, x2) = x2 |
|
201 |
| merge_opt eq (SOME x1, SOME x2) = |
|
202 |
if eq (x1, x2) then SOME x1 else error ("incompatible options during merge");
|
|
203 |
in |
|
204 |
{ is_datatype = merge_opt (eq_snd (op =)) (is_datatype1, is_datatype2),
|
|
205 |
alias = (Symtab.merge (op =) (fst alias1, fst alias2), |
|
206 |
Symtab.merge (op =) (snd alias1, snd alias2)) } |
|
207 |
end; |
|
208 |
||
209 |
type serialize_data = {
|
|
210 |
serializer: CodegenSerializer.serializer, |
|
211 |
primitives: CodegenSerializer.primitives, |
|
212 |
syntax_tyco: itype Codegen.mixfix list Symtab.table, |
|
213 |
syntax_const: iexpr Codegen.mixfix list Symtab.table |
|
214 |
}; |
|
215 |
||
216 |
fun map_serialize_data f { serializer, primitives, syntax_tyco, syntax_const } =
|
|
217 |
let |
|
218 |
val (primitives, syntax_tyco, syntax_const) = |
|
219 |
f (primitives, syntax_tyco, syntax_const) |
|
220 |
in { serializer = serializer, primitives = primitives,
|
|
221 |
syntax_tyco = syntax_tyco, syntax_const = syntax_const } end; |
|
222 |
||
223 |
fun merge_serialize_data |
|
224 |
({ serializer = serializer, primitives = primitives1,
|
|
225 |
syntax_tyco = syntax_tyco1, syntax_const = syntax_const1 }, |
|
226 |
{ serializer = _, primitives = primitives2,
|
|
227 |
syntax_tyco = syntax_tyco2, syntax_const = syntax_const2 }) = |
|
228 |
{ serializer = serializer,
|
|
229 |
primitives = CodegenSerializer.merge_prims (primitives1, primitives2) : CodegenSerializer.primitives, |
|
230 |
syntax_tyco = Symtab.merge (op =) (syntax_tyco1, syntax_tyco2), |
|
231 |
syntax_const = Symtab.merge (op =) (syntax_const1, syntax_const2) }; |
|
232 |
||
233 |
structure CodegenData = TheoryDataFun |
|
234 |
(struct |
|
235 |
val name = "Pure/codegen_package"; |
|
236 |
type T = {
|
|
237 |
modl: module, |
|
238 |
gens: gens, |
|
239 |
lookups: lookups, |
|
240 |
logic_data: logic_data, |
|
241 |
serialize_data: serialize_data Symtab.table |
|
242 |
}; |
|
243 |
val empty = {
|
|
244 |
modl = empty_module, |
|
| 18231 | 245 |
gens = { codegens_sort = [], codegens_type = [], codegens_expr = [], defgens = [] } : gens,
|
| 18217 | 246 |
lookups = { lookups_tyco = Symtab.empty, lookups_const = Symtab.empty } : lookups,
|
247 |
logic_data = { is_datatype = NONE, alias = (Symtab.empty, Symtab.empty) } : logic_data,
|
|
248 |
serialize_data = |
|
249 |
Symtab.empty |
|
250 |
|> Symtab.update ("ml",
|
|
| 18231 | 251 |
{ serializer = serializer_ml : CodegenSerializer.serializer,
|
| 18217 | 252 |
primitives = |
253 |
CodegenSerializer.empty_prims |
|
254 |
|> CodegenSerializer.add_prim ("fst", ("fun fst (x, _) = x;", []))
|
|
255 |
|> CodegenSerializer.add_prim ("snd", ("fun snd (_, y) = y;", []))
|
|
256 |
|> CodegenSerializer.add_prim ("wfrec", ("fun wfrec f x = f (wfrec f) x;", [])),
|
|
257 |
syntax_tyco = Symtab.empty, syntax_const = Symtab.empty }) |
|
258 |
|> Symtab.update ("haskell",
|
|
| 18231 | 259 |
{ serializer = serializer_hs : CodegenSerializer.serializer, primitives = CodegenSerializer.empty_prims,
|
| 18217 | 260 |
syntax_tyco = Symtab.empty, syntax_const = Symtab.empty }) |
261 |
} : T; |
|
262 |
val copy = I; |
|
263 |
val extend = I; |
|
264 |
fun merge _ ( |
|
265 |
{ modl = modl1, gens = gens1, lookups = lookups1,
|
|
266 |
serialize_data = serialize_data1, logic_data = logic_data1 }, |
|
267 |
{ modl = modl2, gens = gens2, lookups = lookups2,
|
|
268 |
serialize_data = serialize_data2, logic_data = logic_data2 } |
|
269 |
) = {
|
|
270 |
modl = merge_module (modl1, modl2), |
|
271 |
gens = merge_gens (gens1, gens2), |
|
272 |
lookups = merge_lookups (lookups1, lookups2), |
|
273 |
logic_data = merge_logic_data (logic_data1, logic_data2), |
|
274 |
serialize_data = Symtab.join (K (merge_serialize_data #> SOME)) |
|
275 |
(serialize_data1, serialize_data2) |
|
276 |
}; |
|
277 |
fun print thy _ = writeln "sorry, this stuff is too complicated..."; |
|
278 |
end); |
|
279 |
||
280 |
fun map_codegen_data f thy = |
|
281 |
case CodegenData.get thy |
|
282 |
of { modl, gens, lookups, serialize_data, logic_data } =>
|
|
283 |
let val (modl, gens, lookups, serialize_data, logic_data) = |
|
284 |
f (modl, gens, lookups, serialize_data, logic_data) |
|
285 |
in CodegenData.put { modl = modl, gens = gens, lookups = lookups,
|
|
286 |
serialize_data = serialize_data, logic_data = logic_data } thy end; |
|
287 |
||
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
288 |
val print_codegen_generated = writeln o Pretty.output o pretty_module o #modl o CodegenData.get; |
| 18217 | 289 |
|
| 18231 | 290 |
fun add_codegen_sort (name, cg) = |
| 18217 | 291 |
map_codegen_data |
292 |
(fn (modl, gens, lookups, serialize_data, logic_data) => |
|
293 |
(modl, |
|
294 |
gens |> map_gens |
|
| 18231 | 295 |
(fn (codegens_sort, codegens_type, codegens_expr, defgens) => |
296 |
(codegens_sort |
|
| 18217 | 297 |
|> Output.update_warn (op =) ("overwriting existing class code generator " ^ name) (name, (cg, stamp ())),
|
298 |
codegens_type, codegens_expr, defgens)), lookups, serialize_data, logic_data)); |
|
299 |
||
300 |
fun add_codegen_type (name, cg) = |
|
301 |
map_codegen_data |
|
302 |
(fn (modl, gens, lookups, serialize_data, logic_data) => |
|
303 |
(modl, |
|
304 |
gens |> map_gens |
|
| 18231 | 305 |
(fn (codegens_sort, codegens_type, codegens_expr, defgens) => |
306 |
(codegens_sort, |
|
| 18217 | 307 |
codegens_type |
308 |
|> Output.update_warn (op =) ("overwriting existing type code generator " ^ name) (name, (cg, stamp ())),
|
|
309 |
codegens_expr, defgens)), lookups, serialize_data, logic_data)); |
|
310 |
||
311 |
fun add_codegen_expr (name, cg) = |
|
312 |
map_codegen_data |
|
313 |
(fn (modl, gens, lookups, serialize_data, logic_data) => |
|
314 |
(modl, |
|
315 |
gens |> map_gens |
|
| 18231 | 316 |
(fn (codegens_sort, codegens_type, codegens_expr, defgens) => |
317 |
(codegens_sort, codegens_type, |
|
| 18217 | 318 |
codegens_expr |
319 |
|> Output.update_warn (op =) ("overwriting existing expression code generator " ^ name) (name, (cg, stamp ())),
|
|
320 |
defgens)), |
|
321 |
lookups, serialize_data, logic_data)); |
|
322 |
||
323 |
fun add_defgen (name, dg) = |
|
324 |
map_codegen_data |
|
325 |
(fn (modl, gens, lookups, serialize_data, logic_data) => |
|
326 |
(modl, |
|
327 |
gens |> map_gens |
|
| 18231 | 328 |
(fn (codegens_sort, codegens_type, codegens_expr, defgens) => |
329 |
(codegens_sort, codegens_type, codegens_expr, |
|
| 18217 | 330 |
defgens |
331 |
|> Output.update_warn (op =) ("overwriting existing definition code generator " ^ name) (name, (dg, stamp ())))),
|
|
332 |
lookups, serialize_data, logic_data)); |
|
333 |
||
334 |
val get_lookups_tyco = #lookups_tyco o #lookups o CodegenData.get; |
|
335 |
||
336 |
fun add_lookup_tyco (src, dst) = |
|
337 |
map_codegen_data |
|
338 |
(fn (modl, gens, lookups, serialize_data, logic_data) => |
|
339 |
(modl, gens, |
|
340 |
lookups |> map_lookups |
|
341 |
(fn (lookups_tyco, lookups_const) => |
|
342 |
(lookups_tyco |> Symtab.update_new (src, dst), |
|
343 |
lookups_const)), |
|
344 |
serialize_data, logic_data)); |
|
345 |
||
346 |
fun add_lookup_const ((src, ty), dst) = |
|
347 |
map_codegen_data |
|
348 |
(fn (modl, gens, lookups, serialize_data, logic_data) => |
|
349 |
(modl, gens, |
|
350 |
lookups |> map_lookups |
|
351 |
(fn (lookups_tyco, lookups_const) => |
|
352 |
(lookups_tyco, |
|
353 |
lookups_const |> Symtab.update_multi (src, (ty, dst)))), |
|
354 |
serialize_data, logic_data)); |
|
355 |
||
356 |
fun set_is_datatype f = |
|
357 |
map_codegen_data |
|
358 |
(fn (modl, gens, lookups, serialize_data, logic_data) => |
|
359 |
(modl, gens, lookups, serialize_data, |
|
360 |
logic_data |
|
361 |
|> map_logic_data (apfst (K (SOME (f, stamp ())))))); |
|
362 |
||
363 |
fun add_alias (src, dst) = |
|
364 |
map_codegen_data |
|
365 |
(fn (modl, gens, lookups, serialize_data, logic_data) => |
|
366 |
(modl, gens, lookups, serialize_data, |
|
367 |
logic_data |> map_logic_data |
|
368 |
(apsnd (fn (tab, tab_rev) => |
|
369 |
(tab |> Symtab.update (src, dst), |
|
370 |
tab_rev |> Symtab.update (dst, src)))))); |
|
371 |
||
372 |
||
373 |
(* code generator name mangling *) |
|
374 |
||
375 |
val is_number = is_some o Int.fromString; |
|
376 |
||
377 |
val dtype_mangle = "dtype"; |
|
378 |
fun is_datatype thy = |
|
379 |
case (#is_datatype o #logic_data o CodegenData.get) thy |
|
380 |
of NONE => K false |
|
381 |
| SOME (f, _) => f thy; |
|
382 |
||
383 |
fun idf_of_name thy shallow name = |
|
384 |
if is_number name |
|
385 |
then name |
|
386 |
else |
|
387 |
name |
|
388 |
|> NameSpace.unpack |
|
389 |
|> split_last |
|
390 |
|> apsnd ((perhaps o Symtab.lookup) ((fst o #alias o #logic_data o CodegenData.get) thy) #> single #> cons shallow) |
|
391 |
|> (op @) |
|
392 |
|> NameSpace.pack; |
|
393 |
||
394 |
fun name_of_idf thy nsp idf = |
|
395 |
let |
|
396 |
val idf' = NameSpace.unpack idf; |
|
397 |
val (idf'', idf_base) = split_last idf'; |
|
398 |
val (modl, shallow) = split_last idf''; |
|
399 |
in |
|
400 |
if nsp = shallow |
|
401 |
then SOME (NameSpace.pack (modl @ [ |
|
402 |
(perhaps o Symtab.lookup) ((snd o #alias o #logic_data o CodegenData.get) thy) idf_base])) |
|
403 |
else NONE |
|
404 |
end; |
|
405 |
||
406 |
fun idf_of_inst thy (_, _, (clstab, _, _)) (cls, tyco) = |
|
407 |
(the o Insttab.lookup clstab) (cls, tyco); |
|
408 |
||
409 |
fun inst_of_idf thy (_, _, (_, clstab_rev, _)) idf = |
|
410 |
Symtab.lookup clstab_rev idf; |
|
411 |
||
412 |
fun idf_of_tname thy tyco = |
|
413 |
if not (Symtab.defined (get_lookups_tyco thy) tyco) |
|
414 |
andalso tyco <> "nat" andalso is_datatype thy tyco |
|
415 |
then |
|
416 |
tyco |
|
417 |
|> (fn tyco => NameSpace.append tyco nsp_type) |
|
418 |
|> (fn tyco => NameSpace.append tyco dtype_mangle) |
|
419 |
else |
|
420 |
tyco |
|
421 |
|> idf_of_name thy nsp_type; |
|
422 |
||
423 |
fun tname_of_idf thy idf = |
|
424 |
if NameSpace.base idf = dtype_mangle |
|
425 |
andalso (NameSpace.base o NameSpace.drop_base) idf = nsp_type |
|
426 |
then |
|
427 |
if is_datatype thy ((NameSpace.drop_base o NameSpace.drop_base) idf) |
|
428 |
then (NameSpace.drop_base o NameSpace.drop_base) idf |> SOME |
|
429 |
else name_of_idf thy nsp_type idf |
|
430 |
else name_of_idf thy nsp_type idf; |
|
431 |
||
| 18231 | 432 |
fun idf_of_cname thy ((overl, _), _, _) (name, ty) = |
| 18217 | 433 |
case Symtab.lookup overl name |
| 18231 | 434 |
of NONE => idf_of_name thy nsp_const name |
435 |
| SOME tab => (the o AList.lookup (Sign.typ_instance thy) tab) ty |
|
| 18217 | 436 |
|
437 |
fun cname_of_idf thy ((_, overl_rev), _, _) idf = |
|
438 |
case Symtab.lookup overl_rev idf |
|
439 |
of NONE => |
|
440 |
(case name_of_idf thy nsp_const idf |
|
| 18231 | 441 |
of NONE => (case name_of_idf thy nsp_mem idf |
| 18217 | 442 |
of NONE => NONE |
443 |
| SOME n => SOME (n, Sign.the_const_constraint thy n)) |
|
| 18231 | 444 |
| SOME n => SOME (n, Sign.the_const_constraint thy n)) |
| 18217 | 445 |
| s => s; |
446 |
||
447 |
||
448 |
(* auxiliary *) |
|
449 |
||
450 |
fun find_lookup_expr thy (f, ty) = |
|
451 |
Symtab.lookup_multi ((#lookups_const o #lookups o CodegenData.get) thy) f |
|
452 |
|> (fn tab => AList.lookup (Sign.typ_instance thy) tab ty) |
|
453 |
||
454 |
fun name_of_tvar (TFree (v, _)) = v |> unprefix "'" |
|
455 |
| name_of_tvar (TVar ((v, i), _)) = |
|
456 |
(if i=0 then v else v ^ string_of_int i) |> unprefix "'" |
|
457 |
||
458 |
||
459 |
(* code generator instantiation, part 2 *) |
|
460 |
||
| 18231 | 461 |
fun invoke_cg_sort thy defs sort trns = |
| 18217 | 462 |
gen_invoke |
| 18231 | 463 |
((map (apsnd (fn (cg, _) => cg thy defs)) o #codegens_sort o #gens o CodegenData.get) thy) |
464 |
("generating sort " ^ (quote o Sign.string_of_sort thy) sort) sort trns;
|
|
| 18217 | 465 |
|
466 |
fun invoke_cg_type thy defs ty trns = |
|
467 |
gen_invoke |
|
468 |
((map (apsnd (fn (cg, _) => cg thy defs)) o #codegens_type o #gens o CodegenData.get) thy) |
|
| 18231 | 469 |
("generating type " ^ (quote o Sign.string_of_typ thy) ty) ty trns;
|
| 18217 | 470 |
|
471 |
fun invoke_cg_expr thy defs t trns = |
|
472 |
gen_invoke |
|
473 |
((map (apsnd (fn (cg, _) => cg thy defs)) o #codegens_expr o #gens o CodegenData.get) thy) |
|
| 18231 | 474 |
("generating expression " ^ (quote o Sign.string_of_term thy) t) t trns;
|
| 18217 | 475 |
|
476 |
fun get_defgens thy defs = |
|
477 |
(map (apsnd (fn (dg, _) => dg thy defs)) o #defgens o #gens o CodegenData.get) thy; |
|
478 |
||
479 |
fun ensure_def_class thy defs cls_or_inst trns = |
|
| 18231 | 480 |
trns |
481 |
|> debug 4 (fn _ => "generating class or instance " ^ quote cls_or_inst) |
|
482 |
|> gen_ensure_def (get_defgens thy defs) ("generating class/instance " ^ quote cls_or_inst) cls_or_inst
|
|
| 18217 | 483 |
|> pair cls_or_inst; |
484 |
||
485 |
fun ensure_def_tyco thy defs tyco trns = |
|
486 |
if NameSpace.is_qualified tyco |
|
487 |
then case Option.mapPartial (Symtab.lookup (get_lookups_tyco thy)) (tname_of_idf thy tyco) |
|
488 |
of NONE => |
|
489 |
trns |
|
| 18231 | 490 |
|> debug 4 (fn _ => "generating type constructor " ^ quote tyco) |
491 |
|> gen_ensure_def (get_defgens thy defs) ("generating type constructor " ^ quote tyco) tyco
|
|
| 18217 | 492 |
|> pair tyco |
493 |
| SOME tyco => |
|
494 |
trns |
|
495 |
|> pair tyco |
|
496 |
else (tyco, trns); |
|
497 |
||
498 |
fun ensure_def_const thy defs f trns = |
|
499 |
if NameSpace.is_qualified f |
|
500 |
then case Option.mapPartial (find_lookup_expr thy) (cname_of_idf thy defs f) |
|
501 |
of NONE => |
|
502 |
trns |
|
| 18231 | 503 |
|> debug 4 (fn _ => "generating constant " ^ quote f) |
504 |
|> invoke_cg_type thy defs (cname_of_idf thy defs f |> the |> snd) |
|
505 |
||> gen_ensure_def (get_defgens thy defs) ("generating constant " ^ quote f) f
|
|
| 18217 | 506 |
|-> (fn ty' => pair f) |
507 |
| SOME (IConst (f, ty)) => |
|
508 |
trns |
|
509 |
|> pair f |
|
510 |
else (f, trns); |
|
511 |
||
512 |
fun mk_fun thy defs eqs ty trns = |
|
513 |
let |
|
514 |
val sortctxt = ClassPackage.extract_sortctxt thy ty; |
|
515 |
fun mk_sortvar (v, sort) trns = |
|
516 |
trns |
|
| 18231 | 517 |
|> invoke_cg_sort thy defs sort |
| 18217 | 518 |
|-> (fn sort => pair (unprefix "'" v, sort)) |
519 |
fun mk_eq (args, rhs) trns = |
|
520 |
trns |
|
521 |
|> fold_map (invoke_cg_expr thy defs) args |
|
522 |
||>> invoke_cg_expr thy defs rhs |
|
523 |
|-> (fn (args, rhs) => pair (map ipat_of_iexpr args, rhs)) |
|
524 |
in |
|
525 |
trns |
|
526 |
|> fold_map mk_eq eqs |
|
527 |
||>> invoke_cg_type thy defs ty |
|
528 |
||>> fold_map mk_sortvar sortctxt |
|
529 |
|-> (fn ((eqs, ty), sortctxt) => pair (Fun (eqs, (sortctxt, ty)))) |
|
530 |
end; |
|
531 |
||
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
532 |
fun fix_nargs thy defs gen i (t, ts) trns = |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
533 |
if length ts < i |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
534 |
then |
| 18217 | 535 |
trns |
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
536 |
|> debug 10 (fn _ => "eta-expanding") |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
537 |
|> gen (strip_comb (Codegen.eta_expand t ts i)) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
538 |
else |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
539 |
trns |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
540 |
|> debug 10 (fn _ => "splitting arguments (" ^ string_of_int i ^ ", " ^ string_of_int (length ts) ^ ")")
|
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
541 |
|> gen (t, Library.take (i, ts)) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
542 |
||>> fold_map (invoke_cg_expr thy defs) (Library.drop (i, ts)) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
543 |
|-> pair o mk_apps; |
| 18217 | 544 |
|
545 |
local |
|
546 |
open CodegenThingolOp; |
|
547 |
infix 8 `%%; |
|
548 |
infixr 6 `->; |
|
549 |
infixr 6 `-->; |
|
550 |
infix 4 `$; |
|
551 |
infix 4 `$$; |
|
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
552 |
infixr 5 `|->; |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
553 |
infixr 5 `|-->; |
| 18217 | 554 |
in |
555 |
||
556 |
(* code generators *) |
|
557 |
||
| 18231 | 558 |
fun codegen_sort_default thy defs sort trns = |
| 18217 | 559 |
trns |
| 18231 | 560 |
|> fold_map (ensure_def_class thy defs) |
561 |
(sort |> filter (ClassPackage.is_class thy) |> map (idf_of_name thy nsp_class)) |
|
562 |
|-> (fn sort => succeed sort) |
|
| 18217 | 563 |
|
564 |
fun codegen_type_default thy defs (v as TVar (_, sort)) trns = |
|
565 |
trns |
|
| 18231 | 566 |
|> invoke_cg_sort thy defs sort |
| 18217 | 567 |
|-> (fn sort => succeed (IVarT (name_of_tvar v, sort))) |
568 |
| codegen_type_default thy defs (v as TFree (_, sort)) trns = |
|
569 |
trns |
|
| 18231 | 570 |
|> invoke_cg_sort thy defs sort |
| 18217 | 571 |
|-> (fn sort => succeed (IVarT (name_of_tvar v, sort))) |
572 |
| codegen_type_default thy defs (Type ("fun", [t1, t2])) trns =
|
|
573 |
trns |
|
574 |
|> invoke_cg_type thy defs t1 |
|
575 |
||>> invoke_cg_type thy defs t2 |
|
576 |
|-> (fn (t1', t2') => succeed (t1' `-> t2')) |
|
577 |
| codegen_type_default thy defs (Type (tyco, tys)) trns = |
|
578 |
trns |
|
579 |
|> ensure_def_tyco thy defs (idf_of_tname thy tyco) |
|
580 |
||>> fold_map (invoke_cg_type thy defs) tys |
|
581 |
|-> (fn (tyco, tys) => succeed (tyco `%% tys)) |
|
582 |
||
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
583 |
fun codegen_expr_default thy defs (Const (f, ty)) trns = |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
584 |
let |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
585 |
val _ = debug 5 (fn _ => "making application of " ^ quote f) (); |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
586 |
val ty_def = Sign.the_const_constraint thy f; |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
587 |
val _ = debug 10 (fn _ => "making application (2)") (); |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
588 |
fun mk_lookup (ClassPackage.Instance (i, ls)) trns = |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
589 |
trns |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
590 |
|> ensure_def_class thy defs ((idf_of_name thy nsp_class o fst) i) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
591 |
||>> ensure_def_class thy defs (idf_of_inst thy defs i) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
592 |
||>> (fold_map o fold_map) mk_lookup ls |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
593 |
|-> (fn ((cls, i), ls) => pair (ClassPackage.Instance ((cls, i), ls))) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
594 |
| mk_lookup (ClassPackage.Lookup (clss, (v, i))) trns = |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
595 |
trns |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
596 |
|> fold_map (ensure_def_class thy defs) (map (idf_of_name thy nsp_class) clss) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
597 |
|-> (fn clss => pair (ClassPackage.Lookup (clss, (name_of_tvar (TFree (v, [])), i)))); |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
598 |
val _ = debug 10 (fn _ => "making application (3)") (); |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
599 |
fun mk_itapp e [] = e |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
600 |
| mk_itapp e lookup = IInst (e, lookup); |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
601 |
in |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
602 |
trns |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
603 |
|> debug 10 (fn _ => "making application (4): " ^ f ^ "::" ^ Sign.string_of_typ thy ty ^ " <~> " ^ Sign.string_of_typ thy ty_def) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
604 |
|> ensure_def_const thy defs (idf_of_cname thy defs (f, ty)) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
605 |
|> debug 10 (fn _ => "making application (5)") |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
606 |
||>> (fold_map o fold_map) mk_lookup (ClassPackage.extract_sortlookup thy (ty_def, ty)) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
607 |
|> debug 10 (fn _ => "making application (6)") |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
608 |
||>> invoke_cg_type thy defs ty |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
609 |
|> debug 10 (fn _ => "making application (7)") |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
610 |
|-> (fn ((f, lookup), ty) => |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
611 |
succeed (mk_itapp (IConst (f, ty)) lookup)) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
612 |
end |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
613 |
| codegen_expr_default thy defs (Free (v, ty)) trns = |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
614 |
trns |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
615 |
|> invoke_cg_type thy defs ty |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
616 |
|-> (fn ty => succeed (IVarE (v, ty))) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
617 |
| codegen_expr_default thy defs (Var ((v, i), ty)) trns = |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
618 |
trns |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
619 |
|> invoke_cg_type thy defs ty |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
620 |
|-> (fn ty => succeed (IVarE (if i=0 then v else v ^ string_of_int i, ty))) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
621 |
| codegen_expr_default thy defs (Abs (v, ty, t)) trns = |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
622 |
trns |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
623 |
|> invoke_cg_type thy defs ty |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
624 |
||>> invoke_cg_expr thy defs (subst_bound (Free (v, ty), t)) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
625 |
|-> (fn (ty, e) => succeed ((v, ty) `|-> e)) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
626 |
| codegen_expr_default thy defs (t1 $ t2) trns = |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
627 |
trns |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
628 |
|> invoke_cg_expr thy defs t1 |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
629 |
||>> invoke_cg_expr thy defs t2 |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
630 |
|-> (fn (e1, e2) => succeed (e1 `$ e2)); |
| 18217 | 631 |
|
632 |
(*fun codegen_eq thy defs t trns = |
|
633 |
let |
|
634 |
fun cg_eq (Const ("op =", _), [t, u]) =
|
|
635 |
trns |
|
636 |
|> invoke_cg_type thy defs (type_of t) |
|
637 |
|-> (fn ty => invoke_ensure_eqinst nsp_eq_class nsp_eq ty #> pair ty) |
|
638 |
||>> invoke_cg_expr thy defs t |
|
639 |
||>> invoke_cg_expr thy defs u |
|
640 |
|-> (fn ((ty, t'), u') => succeed ( |
|
641 |
IConst (fun_eq, ty `-> ty `-> Type_bool) |
|
642 |
`$ t' `$ u')) |
|
643 |
| cg_eq _ = |
|
644 |
trns |
|
645 |
|> fail ("no equality: " ^ Sign.string_of_term thy t)
|
|
646 |
in cg_eq (strip_comb t) end;*) |
|
647 |
||
648 |
fun codegen_neg thy defs t trns = |
|
649 |
let |
|
650 |
val (u, ts) = strip_comb t; |
|
651 |
fun cg_neg (Const ("neg", _)) =
|
|
652 |
trns |
|
653 |
|> invoke_cg_expr thy defs (hd ts) |
|
654 |
|-> (fn e => succeed (Fun_lt `$ e `$ IConst ("0", Type_integer)))
|
|
655 |
| cg_neg _ = |
|
656 |
trns |
|
657 |
|> fail ("no negation: " ^ Sign.string_of_term thy t)
|
|
658 |
in cg_neg u end; |
|
659 |
||
660 |
||
661 |
(* definition generators *) |
|
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
662 |
|
| 18231 | 663 |
fun defgen_tyco_fallback thy defs tyco trns = |
| 18217 | 664 |
if Symtab.fold (fn (_, { syntax_tyco, ... }) => fn b => b orelse Symtab.defined syntax_tyco tyco)
|
665 |
((#serialize_data o CodegenData.get) thy) false |
|
666 |
then |
|
667 |
trns |
|
| 18231 | 668 |
|> debug 5 (fn _ => "trying defgen tyco fallback for " ^ quote tyco) |
| 18217 | 669 |
|> succeed (Nop, []) |
670 |
else |
|
671 |
trns |
|
672 |
|> fail ("no code generation fallback for " ^ quote tyco)
|
|
673 |
||
| 18231 | 674 |
fun defgen_const_fallback thy defs f trns = |
| 18217 | 675 |
if Symtab.fold (fn (_, { syntax_const, ... }) => fn b => b orelse Symtab.defined syntax_const f)
|
676 |
((#serialize_data o CodegenData.get) thy) false |
|
677 |
then |
|
678 |
trns |
|
| 18231 | 679 |
|> debug 5 (fn _ => "trying defgen const fallback for " ^ quote f) |
| 18217 | 680 |
|> succeed (Nop, []) |
681 |
else |
|
682 |
trns |
|
683 |
|> fail ("no code generation fallback for " ^ quote f)
|
|
684 |
||
685 |
fun defgen_defs thy (defs as (_, defs', _)) f trns = |
|
686 |
case Symtab.lookup defs' f |
|
687 |
of SOME (args, rhs, ty) => |
|
688 |
trns |
|
| 18231 | 689 |
|> debug 5 (fn _ => "trying defgen def for " ^ quote f) |
| 18217 | 690 |
|> mk_fun thy defs [(args, rhs)] ty |
691 |
|-> (fn def => succeed (def, [])) |
|
692 |
| _ => trns |> fail ("no definition found for " ^ quote f);
|
|
693 |
||
694 |
fun defgen_clsdecl thy defs cls trns = |
|
695 |
case name_of_idf thy nsp_class cls |
|
696 |
of SOME cls => |
|
697 |
trns |
|
| 18231 | 698 |
|> debug 5 (fn _ => "trying defgen class declaration for " ^ quote cls) |
| 18217 | 699 |
|> fold_map (ensure_def_class thy defs) |
700 |
(map (idf_of_name thy nsp_class) (ClassPackage.get_superclasses thy cls)) |
|
701 |
|-> (fn supcls => succeed (Class (supcls, [], []), |
|
702 |
map (idf_of_name thy nsp_mem) (ClassPackage.the_consts thy cls) |
|
| 18231 | 703 |
@ map (curry (idf_of_inst thy defs) cls) ((map fst o ClassPackage.the_tycos thy) cls))) |
| 18217 | 704 |
| _ => |
705 |
trns |
|
706 |
|> fail ("no class definition found for " ^ quote cls);
|
|
707 |
||
708 |
fun defgen_clsmem thy (defs as (_, _, _)) f trns = |
|
709 |
case name_of_idf thy nsp_mem f |
|
710 |
of SOME clsmem => |
|
711 |
let |
|
712 |
val cls = idf_of_name thy nsp_class ((the o ClassPackage.lookup_const_class thy) clsmem); |
|
713 |
val (tvar, ty) = ClassPackage.get_const_sign thy clsmem; |
|
714 |
in |
|
715 |
trns |
|
| 18231 | 716 |
|> debug 5 (fn _ => "trying defgen class member for " ^ quote f) |
| 18217 | 717 |
|> invoke_cg_type thy defs ty |
718 |
|-> (fn ty => succeed (Classmember (cls, name_of_tvar (TFree (tvar, [])), ty), [])) |
|
719 |
end |
|
720 |
| _ => |
|
721 |
trns |> fail ("no class member found for " ^ quote f)
|
|
722 |
||
723 |
fun defgen_clsinst thy defs clsinst trns = |
|
724 |
case inst_of_idf thy defs clsinst |
|
725 |
of SOME (cls, tyco) => |
|
726 |
let |
|
727 |
val arity = (map o map) (idf_of_name thy nsp_class) |
|
728 |
(ClassPackage.get_arities thy [cls] tyco) |
|
729 |
val clsmems = map (idf_of_name thy nsp_mem) |
|
730 |
(ClassPackage.the_consts thy cls); |
|
731 |
val instmem_idfs = map (idf_of_cname thy defs) |
|
732 |
(ClassPackage.get_inst_consts_sign thy (tyco, cls)); |
|
733 |
in |
|
734 |
trns |
|
| 18231 | 735 |
|> debug 5 (fn _ => "trying defgen class instance for (" ^ quote cls ^ ", " ^ quote tyco ^ ")")
|
| 18217 | 736 |
|> ensure_def_class thy defs (idf_of_name thy nsp_class cls) |
737 |
||>> ensure_def_tyco thy defs (idf_of_tname thy tyco) |
|
738 |
||>> (fold_map o fold_map) (ensure_def_class thy defs) arity |
|
739 |
||>> fold_map (ensure_def_const thy defs) clsmems |
|
740 |
||>> fold_map (ensure_def_const thy defs) instmem_idfs |
|
741 |
|-> (fn ((((cls, tyco), arity), clsmems), instmem_idfs) => |
|
742 |
succeed (Classinst (cls, (tyco, arity), clsmems ~~ instmem_idfs), [])) |
|
743 |
end |
|
744 |
| _ => |
|
745 |
trns |> fail ("no class instance found for " ^ quote clsinst);
|
|
746 |
||
747 |
||
748 |
(* parametrized generators, for instantiation in HOL *) |
|
749 |
||
750 |
fun codegen_let strip_abs thy defs t trns = |
|
751 |
let |
|
752 |
fun dest_let (l as Const ("Let", _) $ t $ u) =
|
|
753 |
(case strip_abs 1 u |
|
754 |
of ([p], u') => apfst (cons (p, t)) (dest_let u') |
|
755 |
| _ => ([], l)) |
|
756 |
| dest_let t = ([], t); |
|
757 |
fun mk_let (l, r) trns = |
|
758 |
trns |
|
759 |
|> invoke_cg_expr thy defs l |
|
760 |
||>> invoke_cg_expr thy defs r |
|
761 |
|-> (fn (l, r) => pair (r, ipat_of_iexpr l)); |
|
762 |
fun cg_let' ([], _) _ = |
|
763 |
trns |
|
764 |
|> fail ("no let expression: " ^ Sign.string_of_term thy t)
|
|
765 |
| cg_let' (lets, body) args = |
|
766 |
trns |
|
767 |
|> fold_map mk_let lets |
|
768 |
||>> invoke_cg_expr thy defs body |
|
769 |
||>> fold_map (invoke_cg_expr thy defs) args |
|
770 |
|-> (fn ((lets, body), args) => |
|
771 |
succeed (Library.foldr (fn ((e, p), body) => ICase (e, [(p, body)])) (lets, body) `$$ args)) |
|
772 |
fun cg_let (t1 as Const ("Let", _), t2 :: t3 :: ts) =
|
|
773 |
cg_let' (dest_let (t1 $ t2 $ t3)) ts |
|
774 |
| cg_let _ = |
|
775 |
trns |
|
776 |
|> fail ("no let expression: " ^ Sign.string_of_term thy t);
|
|
777 |
in cg_let (strip_comb t) end; |
|
778 |
||
779 |
fun codegen_split strip_abs thy defs t trns = |
|
780 |
let |
|
781 |
fun cg_split' ([p], body) args = |
|
782 |
trns |
|
783 |
|> invoke_cg_expr thy defs p |
|
784 |
||>> invoke_cg_expr thy defs body |
|
785 |
||>> fold_map (invoke_cg_expr thy defs) args |
|
786 |
|-> (fn (((IVarE v), body), args) => succeed (IAbs (v, body) `$$ args)) |
|
787 |
| cg_split' _ _ = |
|
788 |
trns |
|
789 |
|> fail ("no split expression: " ^ Sign.string_of_term thy t);
|
|
790 |
fun cg_split (t1 as Const ("split", _), t2 :: ts) =
|
|
791 |
cg_split' (strip_abs 1 (t1 $ t2)) ts |
|
792 |
| cg_split _ = |
|
793 |
trns |
|
794 |
|> fail ("no split expression: " ^ Sign.string_of_term thy t);
|
|
795 |
in cg_split (strip_comb t) end; |
|
796 |
||
797 |
fun codegen_number_of dest_binum mk_int_to_nat thy defs (Const ("Numeral.number_of",
|
|
798 |
Type ("fun", [_, Type ("IntDef.int", [])])) $ bin) trns =
|
|
799 |
trns |
|
800 |
|> (succeed (IConst ((IntInf.toString o dest_binum) bin, Type_integer)) |
|
801 |
handle TERM _ |
|
802 |
=> fail ("not a number: " ^ Sign.string_of_term thy bin))
|
|
803 |
| codegen_number_of dest_binum mk_int_to_nat thy defs (Const ("Numeral.number_of",
|
|
804 |
Type ("fun", [_, Type ("nat", [])])) $ bin) trns =
|
|
805 |
trns |
|
806 |
|> invoke_cg_expr thy defs (mk_int_to_nat bin) |
|
807 |
|-> (fn expr => succeed expr) |
|
808 |
| codegen_number_of dest_binum mk_int_to_nat thy defs t trns = |
|
809 |
trns |
|
810 |
|> fail ("not a number: " ^ Sign.string_of_term thy t);
|
|
811 |
||
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
812 |
fun codegen_case get_case_const_data thy defs t trns = |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
813 |
let |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
814 |
fun cg_case_d gen_names dty (((cname, i), ty), t) trns = |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
815 |
let |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
816 |
val vs = gen_names i; |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
817 |
val tys = Library.take (i, (fst o strip_type) ty); |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
818 |
val frees = map2 Free (vs, tys); |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
819 |
val t' = Envir.beta_norm (list_comb (t, frees)); |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
820 |
in |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
821 |
trns |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
822 |
|> invoke_cg_expr thy defs (list_comb (Const (cname, tys ---> dty), frees)) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
823 |
||>> invoke_cg_expr thy defs t' |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
824 |
|-> (fn (ep, e) => pair (ipat_of_iexpr ep, e)) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
825 |
end; |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
826 |
fun cg_case dty cs (_, ts) trns = |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
827 |
let |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
828 |
val (ts', t) = split_last ts |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
829 |
val _ = debug 10 (fn _ => " in " ^ Sign.string_of_typ thy dty ^ ", pairing " |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
830 |
^ (commas o map (fst o fst)) cs ^ " with " ^ (commas o map (Sign.string_of_term thy)) ts') (); |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
831 |
fun gen_names i = |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
832 |
variantlist (replicate i "x", foldr add_term_names |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
833 |
(map (fst o fst o dest_Var) (foldr add_term_vars [] ts)) ts) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
834 |
in |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
835 |
trns |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
836 |
|> invoke_cg_expr thy defs t |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
837 |
||>> fold_map (cg_case_d gen_names dty) (cs ~~ ts') |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
838 |
|-> (fn (t, ds) => pair (ICase (t, ds))) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
839 |
end; |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
840 |
in case strip_comb t |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
841 |
of (t as Const (f, ty), ts) => |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
842 |
(case get_case_const_data thy f |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
843 |
of NONE => |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
844 |
trns |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
845 |
|> fail ("not a case constant: " ^ quote f)
|
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
846 |
| SOME cs => |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
847 |
let |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
848 |
val (tys, dty) = (split_last o fst o strip_type) ty; |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
849 |
in |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
850 |
trns |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
851 |
|> debug 9 (fn _ => "for case const " ^ f ^ "::" |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
852 |
^ Sign.string_of_typ thy ty ^ ",\n with " ^ AList.string_of_alist I string_of_int cs |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
853 |
^ ",\n given as args " ^ (commas o map (Sign.string_of_term thy)) ts |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
854 |
^ ",\n with significant length " ^ string_of_int (length cs + 1)) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
855 |
|> fix_nargs thy defs (cg_case dty (cs ~~ tys)) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
856 |
(length cs + 1) (t, ts) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
857 |
|-> succeed |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
858 |
end |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
859 |
) |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
860 |
| _ => |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
861 |
trns |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
862 |
|> fail ("not a case constant expression: " ^ Sign.string_of_term thy t)
|
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
863 |
end; |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
864 |
|
| 18217 | 865 |
fun defgen_datatype get_datatype thy defs tyco trns = |
866 |
case tname_of_idf thy tyco |
|
867 |
of SOME dtname => |
|
868 |
(case get_datatype thy tyco |
|
869 |
of SOME (vs, cnames) => |
|
870 |
trns |
|
| 18231 | 871 |
|> debug 5 (fn _ => "trying defgen datatype for " ^ quote dtname) |
| 18217 | 872 |
|> succeed (Datatype (map (rpair [] o unprefix "'") vs, [], []), |
873 |
cnames |
|
874 |
|> map (idf_of_name thy nsp_const) |
|
875 |
|> map (fn "0" => "const.Zero" | c => c)) |
|
876 |
(*! VARIABLEN, EQTYPE !*) |
|
877 |
| NONE => |
|
878 |
trns |
|
879 |
|> fail ("no datatype found for " ^ quote tyco))
|
|
880 |
| NONE => |
|
881 |
trns |
|
882 |
|> fail ("not a type constructor: " ^ quote tyco)
|
|
883 |
end; |
|
884 |
||
885 |
fun defgen_datacons get_datacons thy defs f trns = |
|
886 |
let |
|
887 |
fun the_type "0" = SOME "nat" |
|
888 |
| the_type c = |
|
889 |
case strip_type (Sign.the_const_constraint thy c) |
|
890 |
of (_, Type (dtname, _)) => SOME dtname |
|
891 |
| _ => NONE |
|
892 |
in |
|
893 |
case cname_of_idf thy defs f |
|
894 |
of SOME (c, _) => |
|
895 |
(case the_type c |
|
896 |
of SOME dtname => |
|
897 |
(case get_datacons thy (c, dtname) |
|
898 |
of SOME tyargs => |
|
899 |
trns |
|
| 18231 | 900 |
|> debug 5 (fn _ => "trying defgen datatype constructor for " ^ quote c) |
| 18217 | 901 |
|> ensure_def_tyco thy defs (idf_of_tname thy dtname) |
902 |
||>> fold_map (invoke_cg_type thy defs) tyargs |
|
903 |
|-> (fn (dtname, tys) => succeed (Datatypecons (dtname, tys), [])) |
|
904 |
| NONE => |
|
905 |
trns |
|
906 |
|> fail ("no datatype constructor found for " ^ quote f))
|
|
907 |
| NONE => |
|
908 |
trns |
|
909 |
|> fail ("no datatype constructor found for " ^ quote f))
|
|
910 |
| _ => |
|
911 |
trns |
|
912 |
|> fail ("not a constant: " ^ quote f)
|
|
913 |
end; |
|
914 |
||
915 |
fun defgen_recfun get_equations thy defs f trns = |
|
916 |
case cname_of_idf thy defs f |
|
917 |
of SOME (f, ty) => |
|
918 |
let |
|
919 |
val (eqs, ty) = get_equations thy (f, ty); |
|
920 |
in |
|
921 |
case eqs |
|
922 |
of (_::_) => |
|
923 |
trns |
|
| 18231 | 924 |
|> debug 5 (fn _ => "trying defgen recfun for " ^ quote f) |
| 18217 | 925 |
|> mk_fun thy defs eqs ty |
926 |
|-> (fn def => succeed (def, [])) |
|
927 |
| _ => |
|
928 |
trns |
|
929 |
|> fail ("no recursive definition found for " ^ quote f)
|
|
930 |
end |
|
931 |
| NONE => |
|
932 |
trns |
|
933 |
|> fail ("not a constant: " ^ quote f);
|
|
934 |
||
935 |
||
936 |
(* theory interface *) |
|
937 |
||
938 |
fun mk_deftab thy = |
|
939 |
let |
|
940 |
fun mangle_tyname (ty_decl, ty_def) = |
|
941 |
let |
|
942 |
fun mangle (Type (tyco, tys)) = |
|
943 |
NameSpace.base tyco :: Library.flat (List.mapPartial mangle tys) |> SOME |
|
944 |
| mangle _ = |
|
945 |
NONE |
|
946 |
in |
|
947 |
Vartab.empty |
|
948 |
|> Sign.typ_match thy (ty_decl, ty_def) |
|
949 |
|> map (snd o snd) o Vartab.dest |
|
950 |
|> List.mapPartial mangle |
|
951 |
|> Library.flat |
|
952 |
|> null ? K ["x"] |
|
953 |
|> space_implode "_" |
|
954 |
end; |
|
955 |
fun add_def (name, [(ty, (_, (args, rhs)))]) (overl, defs, clstab) = |
|
956 |
(overl, |
|
957 |
defs |> Symtab.update_new (idf_of_name thy nsp_const name, (args, rhs, ty)), |
|
958 |
clstab) |
|
959 |
| add_def (name, ds) ((overl, overl_rev), defs, clstab) = |
|
960 |
let |
|
961 |
val ty_decl = Sign.the_const_constraint thy name; |
|
962 |
fun mk_idf ("0", Type ("nat", [])) = "const.Zero"
|
|
963 |
| mk_idf ("1", Type ("nat", [])) = "."
|
|
964 |
| mk_idf (nm, ty) = |
|
965 |
if is_number nm |
|
966 |
then nm |
|
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
967 |
else idf_of_name thy nsp_const nm |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
968 |
^ "_" ^ mangle_tyname (ty_decl, ty) |
| 18217 | 969 |
val overl_lookups = map |
970 |
(fn (ty, (_, (args, rhs))) => (ty, mk_idf (name, ty), args, rhs)) ds; |
|
971 |
in |
|
972 |
((overl |> Symtab.update_new (name, map (fn (ty, idf, _, _) => (ty, idf)) overl_lookups), |
|
973 |
overl_rev |> fold Symtab.update_new (map (fn (ty, idf, _, _) => (idf, (name, ty))) overl_lookups)), |
|
974 |
defs |> fold Symtab.update_new (map (fn (ty, idf, args, rhs) => (idf, (args, rhs, ty))) overl_lookups), |
|
975 |
clstab) |
|
976 |
end; |
|
977 |
fun mk_instname thyname (cls, tyco) = |
|
978 |
idf_of_name thy nsp_inst |
|
979 |
(NameSpace.append thyname (NameSpace.base cls ^ "_" ^ NameSpace.base tyco)) |
|
| 18231 | 980 |
fun add_clsmems classtab ((overl, overl_rev), defs, (clstab, clstab_rev, clsmems)) = |
981 |
((overl |
|
982 |
|> Symtab.fold |
|
983 |
(fn (class, (clsmems, _)) => |
|
984 |
fold |
|
985 |
(fn clsmem => |
|
986 |
Symtab.default (clsmem, []) |
|
987 |
#> Symtab.map_entry clsmem |
|
988 |
(cons (Sign.the_const_type thy clsmem, idf_of_name thy nsp_mem clsmem)) |
|
989 |
) clsmems |
|
990 |
) classtab, |
|
991 |
overl_rev |
|
992 |
|> Symtab.fold |
|
993 |
(fn (class, (clsmems, _)) => |
|
994 |
fold |
|
995 |
(fn clsmem => |
|
996 |
Symtab.update_new |
|
997 |
(idf_of_name thy nsp_mem clsmem, (clsmem, Sign.the_const_type thy clsmem)) |
|
998 |
) clsmems |
|
999 |
) classtab), |
|
1000 |
defs, |
|
| 18217 | 1001 |
(clstab |
1002 |
|> Symtab.fold |
|
1003 |
(fn (cls, (_, clsinsts)) => fold |
|
1004 |
(fn (tyco, thyname) => Insttab.update ((cls, tyco), mk_instname thyname (cls, tyco))) clsinsts) |
|
1005 |
classtab, |
|
1006 |
clstab_rev |
|
1007 |
|> Symtab.fold |
|
1008 |
(fn (cls, (_, clsinsts)) => fold |
|
1009 |
(fn (tyco, thyname) => Symtab.update (mk_instname thyname (cls, tyco), (cls, tyco))) clsinsts) |
|
1010 |
classtab, |
|
1011 |
clsmems |
|
1012 |
|> Symtab.fold |
|
1013 |
(fn (class, (clsmems, _)) => fold |
|
| 18231 | 1014 |
(fn clsmem => Symtab.update (clsmem, class)) clsmems) |
| 18217 | 1015 |
classtab)) |
1016 |
in |
|
1017 |
((Symtab.empty, Symtab.empty), Symtab.empty, (Insttab.empty, Symtab.empty, Symtab.empty)) |
|
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
1018 |
|> add_clsmems (ClassPackage.get_classtab thy) |
| 18217 | 1019 |
|> fold add_def (Codegen.mk_deftab thy |> Symtab.dest) |
1020 |
end; |
|
1021 |
||
| 18231 | 1022 |
fun expand_module defs gen thy = |
| 18217 | 1023 |
let |
1024 |
fun put_module modl = |
|
1025 |
map_codegen_data (fn (_, gens, lookups, serialize_data, logic_data) => |
|
1026 |
(modl, gens, lookups, serialize_data, logic_data)); |
|
1027 |
val _ = put_module : module -> theory -> theory; |
|
1028 |
in |
|
1029 |
(#modl o CodegenData.get) thy |
|
1030 |
|> start_transact (gen thy defs) |
|
1031 |
|-> (fn x => fn modl => (x, put_module modl thy)) |
|
1032 |
end; |
|
1033 |
||
1034 |
||
1035 |
(* syntax *) |
|
1036 |
||
| 18231 | 1037 |
fun gen_add_syntax_tyco prep_tyco prep_mfx prep_primname serial_name ((raw_tyco, raw_mfx), primdef) thy = |
| 18217 | 1038 |
let |
1039 |
val tyco = prep_tyco thy raw_tyco; |
|
1040 |
val _ = if member (op =) prims tyco |
|
1041 |
then error ("attempted to re-define primitive " ^ quote tyco)
|
|
1042 |
else () |
|
1043 |
fun add_primdef NONE = I |
|
1044 |
| add_primdef (SOME (name, (def, deps))) = |
|
1045 |
CodegenSerializer.add_prim (prep_primname thy tyco name, (def, deps)) |
|
1046 |
in |
|
1047 |
thy |
|
1048 |
|> prep_mfx raw_mfx |
|
1049 |
|-> (fn mfx => map_codegen_data |
|
1050 |
(fn (modl, gens, lookups, serialize_data, logic_data) => |
|
1051 |
(modl, gens, lookups, |
|
| 18231 | 1052 |
serialize_data |> Symtab.map_entry serial_name |
| 18217 | 1053 |
(map_serialize_data |
1054 |
(fn (primitives, syntax_tyco, syntax_const) => |
|
1055 |
(primitives |> add_primdef primdef, |
|
1056 |
syntax_tyco |> Symtab.update_new (tyco, mfx), |
|
1057 |
syntax_const))), |
|
1058 |
logic_data))) |
|
1059 |
end; |
|
1060 |
||
1061 |
val add_syntax_tyco_i = gen_add_syntax_tyco (K I) pair ((K o K) I); |
|
1062 |
val add_syntax_tyco = |
|
1063 |
let |
|
1064 |
fun mk_name _ _ (SOME name) = name |
|
1065 |
| mk_name thy tyco NONE = |
|
1066 |
let |
|
1067 |
val name = Sign.extern_type thy tyco |
|
1068 |
in |
|
1069 |
if NameSpace.is_qualified name |
|
1070 |
then error ("no unique identifier for syntax definition: " ^ quote tyco)
|
|
1071 |
else name |
|
1072 |
end; |
|
1073 |
fun prep_mfx mfx thy = |
|
1074 |
let |
|
1075 |
val proto_mfx = Codegen.parse_mixfix |
|
1076 |
(typ_of o read_ctyp thy) mfx; |
|
1077 |
fun generate thy defs = fold_map (invoke_cg_type thy defs) |
|
1078 |
(Codegen.quotes_of proto_mfx); |
|
1079 |
in |
|
1080 |
thy |
|
| 18231 | 1081 |
|> expand_module (mk_deftab thy) generate |
| 18217 | 1082 |
|-> (fn tys => pair (Codegen.replace_quotes tys proto_mfx)) |
1083 |
end; |
|
1084 |
in |
|
1085 |
gen_add_syntax_tyco (fn thy => idf_of_tname thy o Sign.intern_type thy) |
|
1086 |
prep_mfx mk_name |
|
1087 |
end; |
|
1088 |
||
| 18231 | 1089 |
fun gen_add_syntax_const prep_const prep_mfx prep_primname serial_name ((raw_f, raw_mfx), primdef) thy = |
| 18217 | 1090 |
let |
1091 |
val f = prep_const thy raw_f; |
|
1092 |
val _ = if member (op =) prims f |
|
1093 |
then error ("attempted to re-define primitive " ^ quote f)
|
|
1094 |
else () |
|
1095 |
fun add_primdef NONE = I |
|
1096 |
| add_primdef (SOME (name, (def, deps))) = |
|
1097 |
CodegenSerializer.add_prim (prep_primname thy f name, (def, deps)) |
|
1098 |
in |
|
1099 |
thy |
|
1100 |
|> prep_mfx raw_mfx |
|
1101 |
|-> (fn mfx => map_codegen_data |
|
1102 |
(fn (modl, gens, lookups, serialize_data, logic_data) => |
|
1103 |
(modl, gens, lookups, |
|
| 18231 | 1104 |
serialize_data |> Symtab.map_entry serial_name |
| 18217 | 1105 |
(map_serialize_data |
1106 |
(fn (primitives, syntax_tyco, syntax_const) => |
|
1107 |
(primitives |> add_primdef primdef, |
|
1108 |
syntax_tyco, |
|
1109 |
syntax_const |> Symtab.update_new (f, mfx)))), |
|
1110 |
logic_data))) |
|
1111 |
end; |
|
1112 |
||
1113 |
val add_syntax_const_i = gen_add_syntax_const (K I) pair ((K o K) I); |
|
1114 |
val add_syntax_const = |
|
1115 |
let |
|
1116 |
fun prep_const thy (raw_f, raw_ty) = |
|
1117 |
let |
|
1118 |
val defs = mk_deftab thy; |
|
1119 |
val f = Sign.intern_const thy raw_f; |
|
1120 |
val ty = |
|
1121 |
raw_ty |
|
1122 |
|> Option.map (Sign.read_tyname thy) |
|
1123 |
|> the_default (Sign.the_const_constraint thy f); |
|
1124 |
in idf_of_cname thy defs (f, ty) end; |
|
1125 |
fun mk_name _ _ (SOME name) = name |
|
1126 |
| mk_name thy f NONE = |
|
1127 |
let |
|
1128 |
val name = Sign.extern_const thy f |
|
1129 |
in |
|
1130 |
if NameSpace.is_qualified name |
|
1131 |
then error ("no unique identifier for syntax definition: " ^ quote f)
|
|
1132 |
else name |
|
1133 |
end; |
|
1134 |
fun prep_mfx mfx thy = |
|
1135 |
let |
|
1136 |
val proto_mfx = Codegen.parse_mixfix |
|
1137 |
(term_of o read_cterm thy o rpair TypeInfer.logicT) mfx; |
|
1138 |
fun generate thy defs = fold_map (invoke_cg_expr thy defs) |
|
1139 |
(Codegen.quotes_of proto_mfx); |
|
1140 |
in |
|
1141 |
thy |
|
| 18231 | 1142 |
|> expand_module (mk_deftab thy) generate |
| 18217 | 1143 |
|-> (fn es => pair (Codegen.replace_quotes es proto_mfx)) |
1144 |
end; |
|
1145 |
in |
|
1146 |
gen_add_syntax_const prep_const prep_mfx mk_name |
|
1147 |
end; |
|
1148 |
||
1149 |
||
1150 |
(* code generation *) |
|
1151 |
||
| 18231 | 1152 |
fun get_serializer thy serial_name = |
1153 |
(#serializer o (fn data => (the oo Symtab.lookup) data serial_name) |
|
1154 |
o #serialize_data o CodegenData.get) thy; |
|
1155 |
||
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
1156 |
fun mk_const thy (f, s_ty) = |
| 18217 | 1157 |
let |
| 18231 | 1158 |
val f' = Sign.intern_const thy f; |
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
1159 |
val ty = case s_ty |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
1160 |
of NONE => Sign.the_const_constraint thy f' |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
1161 |
| SOME s => Sign.read_typ (thy, K NONE) s; |
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
1162 |
in (f', ty) end; |
| 18231 | 1163 |
|
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
1164 |
fun generate_code consts thy = |
| 18231 | 1165 |
let |
1166 |
val defs = mk_deftab thy; |
|
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
1167 |
val consts' = map (idf_of_cname thy defs o mk_const thy) consts; |
| 18231 | 1168 |
fun generate thy defs = fold_map (ensure_def_const thy defs) consts' |
| 18217 | 1169 |
in |
1170 |
thy |
|
| 18231 | 1171 |
|> expand_module defs generate |
1172 |
|-> (fn _ => pair consts') |
|
| 18217 | 1173 |
end; |
1174 |
||
1175 |
fun serialize_code serial_name filename consts thy = |
|
1176 |
let |
|
1177 |
fun mk_sfun tab name args f = |
|
1178 |
Symtab.lookup tab name |
|
1179 |
|> Option.map (fn ms => Codegen.fillin_mixfix ms args (f : 'a -> Pretty.T)) |
|
1180 |
val serialize_data = |
|
1181 |
thy |
|
1182 |
|> CodegenData.get |
|
1183 |
|> #serialize_data |
|
1184 |
|> (fn data => (the oo Symtab.lookup) data serial_name) |
|
| 18231 | 1185 |
val serializer' = (get_serializer thy serial_name) |
| 18217 | 1186 |
((mk_sfun o #syntax_tyco) serialize_data) |
1187 |
((mk_sfun o #syntax_const) serialize_data) |
|
| 18231 | 1188 |
(#primitives serialize_data); |
1189 |
val _ = serializer' : string list option -> module -> Pretty.T; |
|
| 18217 | 1190 |
val compile_it = serial_name = "ml" andalso filename = "-"; |
1191 |
fun use_code code = |
|
1192 |
if compile_it |
|
1193 |
then use_text Context.ml_output false code |
|
1194 |
else File.write (Path.unpack filename) (code ^ "\n"); |
|
1195 |
in |
|
1196 |
thy |
|
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
1197 |
|> (if is_some consts then generate_code (the consts) else pair []) |
| 18231 | 1198 |
|-> (fn [] => `(serializer' NONE o #modl o CodegenData.get) |
1199 |
| consts => `(serializer' (SOME consts) o #modl o CodegenData.get)) |
|
| 18217 | 1200 |
|-> (fn code => ((use_code o Pretty.output) code; I)) |
1201 |
end; |
|
1202 |
||
1203 |
||
1204 |
(* toplevel interface *) |
|
1205 |
||
1206 |
local |
|
1207 |
||
1208 |
structure P = OuterParse |
|
1209 |
and K = OuterKeyword |
|
1210 |
||
1211 |
in |
|
1212 |
||
1213 |
val (generateK, serializeK, extractingK, aliasK, definedK, dependingK, syntax_tycoK, syntax_constK) = |
|
1214 |
("code_generate", "code_serialize", "extracting", "defined_by", "depending_on", "code_alias", "code_syntax_tyco", "code_syntax_const");
|
|
1215 |
||
1216 |
val generateP = |
|
1217 |
OuterSyntax.command generateK "generate executable code for constants" K.thy_decl ( |
|
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
1218 |
Scan.repeat1 (P.name -- Scan.option (P.$$$ "::" |-- P.typ)) |
| 18217 | 1219 |
>> (fn consts => |
| 18231 | 1220 |
Toplevel.theory (generate_code consts #> snd)) |
| 18217 | 1221 |
); |
1222 |
||
1223 |
val serializeP = |
|
| 18231 | 1224 |
OuterSyntax.command serializeK "serialize executable code for constants" K.thy_decl ( |
| 18217 | 1225 |
P.name |
1226 |
-- P.name |
|
1227 |
-- Scan.option ( |
|
| 18231 | 1228 |
P.$$$ extractingK |
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
1229 |
|-- Scan.repeat1 (P.name -- Scan.option (P.$$$ "::" |-- P.typ)) |
| 18217 | 1230 |
) |
1231 |
>> (fn ((serial_name, filename), consts) => |
|
1232 |
Toplevel.theory (serialize_code serial_name filename consts)) |
|
1233 |
); |
|
1234 |
||
1235 |
val aliasP = |
|
1236 |
OuterSyntax.command aliasK "declare an alias for a theory identifier" K.thy_decl ( |
|
1237 |
P.name |
|
1238 |
-- P.name |
|
1239 |
>> (fn (src, dst) => Toplevel.theory (add_alias (src, dst))) |
|
1240 |
); |
|
1241 |
||
1242 |
val syntax_tycoP = |
|
1243 |
OuterSyntax.command syntax_tycoK "define code syntax for type constructor" K.thy_decl ( |
|
1244 |
P.string |
|
1245 |
-- Scan.repeat1 ( |
|
1246 |
P.xname -- (P.$$$ "(" |-- P.string --| P.$$$ ")")
|
|
1247 |
-- Scan.option ( |
|
1248 |
P.$$$ definedK |
|
1249 |
|-- Scan.option (P.$$$ "(" |-- P.string --| P.$$$ ")")
|
|
1250 |
-- (P.string -- Scan.optional (P.$$$ dependingK |-- P.list1 P.string) []) |
|
1251 |
) |
|
1252 |
) |
|
| 18231 | 1253 |
>> (fn (serial_name, xs) => |
| 18217 | 1254 |
(Toplevel.theory oo fold) |
1255 |
(fn ((tyco, raw_mfx), raw_def) => |
|
| 18231 | 1256 |
add_syntax_tyco serial_name ((tyco, raw_mfx), raw_def)) xs) |
| 18217 | 1257 |
); |
1258 |
||
1259 |
val syntax_constP = |
|
1260 |
OuterSyntax.command syntax_constK "define code syntax for constant" K.thy_decl ( |
|
1261 |
P.string |
|
1262 |
-- Scan.repeat1 ( |
|
1263 |
(P.xname -- Scan.option (P.$$$ "::" |-- P.typ)) -- (P.$$$ "(" |-- P.string --| P.$$$ ")")
|
|
1264 |
-- Scan.option ( |
|
1265 |
P.$$$ definedK |
|
1266 |
|-- Scan.option (P.$$$ "(" |-- P.string --| P.$$$ ")")
|
|
1267 |
-- (P.string -- Scan.optional (P.$$$ dependingK |-- P.list1 P.string) []) |
|
1268 |
) |
|
1269 |
) |
|
| 18231 | 1270 |
>> (fn (serial_name, xs) => |
| 18217 | 1271 |
(Toplevel.theory oo fold) |
1272 |
(fn ((f, raw_mfx), raw_def) => |
|
| 18231 | 1273 |
add_syntax_const serial_name ((f, raw_mfx), raw_def)) xs) |
| 18217 | 1274 |
); |
1275 |
||
1276 |
val _ = OuterSyntax.add_parsers [generateP, serializeP, aliasP, syntax_tycoP, syntax_constP]; |
|
1277 |
val _ = OuterSyntax.add_keywords [extractingK, definedK, dependingK]; |
|
1278 |
||
1279 |
||
1280 |
(* setup *) |
|
1281 |
val _ = |
|
1282 |
let |
|
1283 |
val bool = Type ("bool", []);
|
|
1284 |
val nat = Type ("nat", []);
|
|
1285 |
val int = Type ("IntDef.int", []);
|
|
1286 |
fun list t = Type ("List.list", [t]);
|
|
1287 |
fun pair t1 t2 = Type ("*", [t1, t2]);
|
|
1288 |
val A = TVar (("'a", 0), []);
|
|
1289 |
val B = TVar (("'b", 0), []);
|
|
1290 |
in Context.add_setup [ |
|
1291 |
CodegenData.init, |
|
| 18231 | 1292 |
add_codegen_sort ("default", codegen_sort_default),
|
| 18217 | 1293 |
add_codegen_type ("default", codegen_type_default),
|
1294 |
add_codegen_expr ("default", codegen_expr_default),
|
|
1295 |
(* add_codegen_expr ("eq", codegen_eq), *)
|
|
1296 |
add_codegen_expr ("neg", codegen_neg),
|
|
1297 |
add_defgen ("clsdecl", defgen_clsdecl),
|
|
| 18231 | 1298 |
add_defgen ("tyco_fallback", defgen_tyco_fallback),
|
1299 |
add_defgen ("const_fallback", defgen_const_fallback),
|
|
| 18217 | 1300 |
add_defgen ("defs", defgen_defs),
|
1301 |
add_defgen ("clsmem", defgen_clsmem),
|
|
1302 |
add_defgen ("clsinst", defgen_clsinst),
|
|
1303 |
add_alias ("op <>", "neq"),
|
|
1304 |
add_alias ("op >=", "ge"),
|
|
1305 |
add_alias ("op >", "gt"),
|
|
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
1306 |
add_alias ("op <=", "le"),
|
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
1307 |
add_alias ("op <", "lt"),
|
|
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
1308 |
add_alias ("op +", "add"),
|
| 18217 | 1309 |
add_alias ("op -", "minus"),
|
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
1310 |
add_alias ("op *", "times"),
|
| 18217 | 1311 |
add_alias ("op @", "append"),
|
1312 |
add_lookup_tyco ("bool", type_bool),
|
|
1313 |
add_lookup_tyco ("IntDef.int", type_integer),
|
|
1314 |
add_lookup_tyco ("List.list", type_list),
|
|
1315 |
add_lookup_tyco ("*", type_pair),
|
|
| 18231 | 1316 |
add_lookup_const (("True", bool), Cons_true),
|
| 18217 | 1317 |
add_lookup_const (("False", bool), Cons_false),
|
1318 |
add_lookup_const (("Not", bool --> bool), Fun_not),
|
|
1319 |
add_lookup_const (("op &", bool --> bool --> bool), Fun_and),
|
|
1320 |
add_lookup_const (("op |", bool --> bool --> bool), Fun_or),
|
|
1321 |
add_lookup_const (("HOL.If", bool --> A --> A --> A), Fun_if),
|
|
| 18231 | 1322 |
add_lookup_const (("List.list.Cons", A --> list A --> list A), Cons_cons),
|
| 18217 | 1323 |
add_lookup_const (("List.list.Nil", list A), Cons_nil),
|
1324 |
add_lookup_const (("Pair", A --> B --> pair A B), Cons_pair),
|
|
1325 |
add_lookup_const (("fst", pair A B --> A), Fun_fst),
|
|
1326 |
add_lookup_const (("snd", pair A B --> B), Fun_snd),
|
|
1327 |
add_lookup_const (("1", nat),
|
|
1328 |
IApp ( |
|
1329 |
IConst ("const.Suc", IFun (IType ("type.nat", []), IFun (IType ("type.nat", []), IType ("type.nat", [])))),
|
|
1330 |
IConst ("const.Zero", IType ("type.nat", []))
|
|
1331 |
)), |
|
1332 |
add_lookup_const (("0", int), Fun_0),
|
|
1333 |
add_lookup_const (("1", int), Fun_1),
|
|
1334 |
add_lookup_const (("op +", int --> int --> int), Fun_add),
|
|
1335 |
add_lookup_const (("op *", int --> int --> int), Fun_mult),
|
|
1336 |
add_lookup_const (("uminus", int --> int), Fun_minus),
|
|
1337 |
add_lookup_const (("op <", int --> int --> bool), Fun_lt),
|
|
1338 |
add_lookup_const (("op <=", int --> int --> bool), Fun_le),
|
|
1339 |
add_lookup_const (("Wellfounded_Recursion.wfrec", ((A --> B) --> A --> B) --> A --> B), Fun_wfrec),
|
|
1340 |
add_lookup_const (("op =", A --> A --> bool), Fun_eq)
|
|
1341 |
] end; |
|
1342 |
||
1343 |
(* "op /" ??? *) |
|
1344 |
||
1345 |
end; (* local *) |
|
1346 |
||
1347 |
end; (* struct *) |