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