author | wenzelm |
Tue, 11 Jul 2006 12:17:08 +0200 | |
changeset 20083 | 717b1eb434f1 |
parent 20076 | def4ad161528 |
child 20105 | 454f4be984b7 |
permissions | -rw-r--r-- |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1 |
(* Title: Pure/Tools/codegen_package.ML |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
2 |
ID: $Id$ |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
3 |
Author: Florian Haftmann, TU Muenchen |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
4 |
|
18217 | 5 |
Code generator from Isabelle theories to |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
6 |
intermediate language ("Thin-gol"). |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
7 |
*) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
8 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
9 |
signature CODEGEN_PACKAGE = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
10 |
sig |
19884 | 11 |
val codegen_term: term -> theory -> CodegenThingol.iexpr * theory; |
12 |
val is_dtcon: string -> bool; |
|
13 |
val consts_of_idfs: theory -> string list -> (string * typ) list; |
|
14 |
val idfs_of_consts: theory -> (string * typ) list -> string list; |
|
19967 | 15 |
val get_root_module: theory -> CodegenThingol.module * theory; |
19884 | 16 |
val get_ml_fun_datatype: theory -> (string -> string) |
17 |
-> ((string * CodegenThingol.funn) list -> Pretty.T) |
|
18 |
* ((string * CodegenThingol.datatyp) list -> Pretty.T); |
|
18702 | 19 |
|
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
20 |
val add_pretty_list: string -> string -> string * (int * string) |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
21 |
-> theory -> theory; |
18217 | 22 |
val add_alias: string * string -> theory -> theory; |
18455 | 23 |
val set_get_all_datatype_cons : (theory -> (string * string) list) |
18454 | 24 |
-> theory -> theory; |
18963 | 25 |
val set_get_datatype: (theory -> string -> ((string * sort) list * (string * typ list) list) option) |
26 |
-> theory -> theory; |
|
18702 | 27 |
val set_int_tyco: string -> theory -> theory; |
18217 | 28 |
|
19884 | 29 |
type appgen; |
30 |
val add_appconst: xstring * ((int * int) * appgen) -> theory -> theory; |
|
31 |
val add_appconst_i: string * ((int * int) * appgen) -> theory -> theory; |
|
18702 | 32 |
val appgen_default: appgen; |
18335 | 33 |
val appgen_let: (int -> term -> term list * term) |
34 |
-> appgen; |
|
35 |
val appgen_split: (int -> term -> term list * term) |
|
36 |
-> appgen; |
|
19597 | 37 |
val appgen_number_of: (theory -> term -> IntInf.int) -> appgen; |
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
38 |
val appgen_char: (term -> int option) -> appgen; |
19038 | 39 |
val appgen_wfrec: appgen; |
19597 | 40 |
val add_case_const: string -> (string * int) list -> theory -> theory; |
18217 | 41 |
|
19008 | 42 |
val print_code: theory -> unit; |
18454 | 43 |
val rename_inconsistent: theory -> theory; |
18515 | 44 |
|
45 |
(*debugging purpose only*) |
|
18454 | 46 |
structure InstNameMangler: NAME_MANGLER; |
47 |
structure ConstNameMangler: NAME_MANGLER; |
|
48 |
structure DatatypeconsNameMangler: NAME_MANGLER; |
|
18231 | 49 |
structure CodegenData: THEORY_DATA; |
19884 | 50 |
type auxtab; |
51 |
val mk_tabs: theory -> string list option -> auxtab; |
|
18454 | 52 |
val alias_get: theory -> string -> string; |
18515 | 53 |
val idf_of_name: theory -> string -> string -> string; |
54 |
val idf_of_const: theory -> auxtab -> string * typ -> string; |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
55 |
val idf_of_co: theory -> auxtab -> string * string -> string option; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
56 |
end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
57 |
|
18217 | 58 |
structure CodegenPackage : CODEGEN_PACKAGE = |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
59 |
struct |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
60 |
|
18850 | 61 |
open CodegenThingol; |
18217 | 62 |
|
18702 | 63 |
(* shallow name spaces *) |
18217 | 64 |
|
19038 | 65 |
val alias_ref = ref (fn thy : theory => fn s : string => s, fn thy : theory => fn s : string => s); |
66 |
fun alias_get name = (fst o !) alias_ref name; |
|
67 |
fun alias_rev name = (snd o !) alias_ref name; |
|
68 |
||
18885 | 69 |
val nsp_module = ""; (* a dummy by convention *) |
18217 | 70 |
val nsp_class = "class"; |
18454 | 71 |
val nsp_tyco = "tyco"; |
18217 | 72 |
val nsp_const = "const"; |
18454 | 73 |
val nsp_overl = "overl"; |
74 |
val nsp_dtcon = "dtcon"; |
|
18217 | 75 |
val nsp_mem = "mem"; |
76 |
val nsp_inst = "inst"; |
|
19213 | 77 |
val nsp_instmem = "instmem"; |
18217 | 78 |
|
19038 | 79 |
fun add_nsp shallow name = |
80 |
name |
|
81 |
|> NameSpace.unpack |
|
82 |
|> split_last |
|
83 |
|> apsnd (single #> cons shallow) |
|
84 |
|> (op @) |
|
85 |
|> NameSpace.pack; |
|
86 |
||
87 |
fun dest_nsp nsp idf = |
|
88 |
let |
|
89 |
val idf' = NameSpace.unpack idf; |
|
90 |
val (idf'', idf_base) = split_last idf'; |
|
91 |
val (modl, shallow) = split_last idf''; |
|
92 |
in |
|
93 |
if nsp = shallow |
|
94 |
then (SOME o NameSpace.pack) (modl @ [idf_base]) |
|
95 |
else NONE |
|
96 |
end; |
|
97 |
||
98 |
fun idf_of_name thy shallow name = |
|
99 |
name |
|
100 |
|> alias_get thy |
|
101 |
|> add_nsp shallow; |
|
102 |
||
103 |
fun name_of_idf thy shallow idf = |
|
104 |
idf |
|
105 |
|> dest_nsp shallow |
|
106 |
|> Option.map (alias_rev thy); |
|
107 |
||
18217 | 108 |
|
19956 | 109 |
(* theory name lookup *) |
110 |
||
111 |
fun thyname_of thy f x = |
|
112 |
let |
|
113 |
fun thy_of thy = |
|
114 |
if f thy x |
|
115 |
then SOME (the_default thy (get_first thy_of (Theory.parents_of thy))) |
|
116 |
else NONE; |
|
117 |
in Option.map Context.theory_name (thy_of thy) end; |
|
118 |
||
119 |
fun thyname_of_instance thy inst = |
|
120 |
let |
|
121 |
fun test_instance thy (class, tyco) = |
|
122 |
can (Sorts.mg_domain (Sign.classes_of thy) tyco) [class] |
|
123 |
in case thyname_of thy test_instance inst |
|
124 |
of SOME name => name |
|
125 |
| NONE => error ("thyname_of_instance: no such instance: " ^ quote (fst inst) ^ ", " ^ quote (snd inst)) |
|
126 |
end; |
|
127 |
||
128 |
fun thyname_of_tyco thy tyco = |
|
129 |
case thyname_of thy Sign.declared_tyname tyco |
|
130 |
of SOME name => name |
|
131 |
| NONE => error ("thyname_of_tyco: no such type constructor: " ^ quote tyco); |
|
132 |
||
133 |
fun thyname_of_thm thy thm = |
|
134 |
let |
|
135 |
fun thy_of thy = |
|
136 |
if member eq_thm ((flat o map snd o NameSpace.dest_table o PureThy.theorems_of) thy) thm |
|
137 |
then SOME thy |
|
138 |
else get_first thy_of (Theory.parents_of thy) |
|
139 |
in case thy_of thy |
|
140 |
of SOME thy => Context.theory_name thy |
|
141 |
| NONE => error ("thyname_of_thm: no such thm: " ^ string_of_thm thm) |
|
142 |
end; |
|
143 |
||
144 |
||
18702 | 145 |
(* code generator basics *) |
18454 | 146 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
147 |
type deftab = (typ * thm) list Symtab.table; |
19038 | 148 |
|
19571 | 149 |
fun is_overloaded thy c = case Theory.definitions_of thy c |
150 |
of [] => true (* FIXME false (!?) *) |
|
151 |
| [{lhs = ty, ...}] => not (Sign.typ_equiv thy (ty, Sign.the_const_type thy c)) |
|
19136 | 152 |
| _ => true; |
153 |
||
18454 | 154 |
structure InstNameMangler = NameManglerFun ( |
155 |
type ctxt = theory; |
|
19956 | 156 |
type src = class * string; |
157 |
val ord = prod_ord string_ord string_ord; |
|
158 |
fun mk thy ((cls, tyco), i) = |
|
159 |
(NameSpace.base o alias_get thy) cls ^ "_" ^ (NameSpace.base o alias_get thy) tyco ^ implode (replicate i "'"); |
|
18454 | 160 |
fun is_valid _ _ = true; |
161 |
fun maybe_unique _ _ = NONE; |
|
162 |
fun re_mangle _ dst = error ("no such instance: " ^ quote dst); |
|
163 |
); |
|
164 |
||
165 |
structure ConstNameMangler = NameManglerFun ( |
|
19136 | 166 |
type ctxt = theory; |
167 |
type src = string * typ; |
|
168 |
val ord = prod_ord string_ord Term.typ_ord; |
|
169 |
fun mk thy ((c, ty), i) = |
|
18454 | 170 |
let |
19038 | 171 |
val c' = idf_of_name thy nsp_overl c; |
19202 | 172 |
val prefix = |
19571 | 173 |
case (find_first (fn {lhs, ...} => Sign.typ_equiv thy (ty, lhs)) |
174 |
(Theory.definitions_of thy c)) |
|
175 |
of SOME {module, ...} => NameSpace.append module nsp_overl |
|
20076 | 176 |
| NONE => if c = "op =" (* FIXME depends on object-logic!? *) |
19202 | 177 |
then |
178 |
NameSpace.append |
|
19956 | 179 |
((thyname_of_tyco thy o fst o dest_Type o hd o fst o strip_type) ty) |
19202 | 180 |
nsp_overl |
181 |
else |
|
182 |
NameSpace.drop_base c'; |
|
19136 | 183 |
val c'' = NameSpace.append prefix (NameSpace.base c'); |
18454 | 184 |
fun mangle (Type (tyco, tys)) = |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19466
diff
changeset
|
185 |
(NameSpace.base o alias_get thy) tyco :: flat (map_filter mangle tys) |> SOME |
18454 | 186 |
| mangle _ = |
187 |
NONE |
|
188 |
in |
|
189 |
Vartab.empty |
|
19435 | 190 |
|> Type.raw_match (Sign.the_const_type thy c, ty) |
18454 | 191 |
|> map (snd o snd) o Vartab.dest |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19466
diff
changeset
|
192 |
|> map_filter mangle |
19466 | 193 |
|> flat |
18454 | 194 |
|> null ? K ["x"] |
19038 | 195 |
|> cons c'' |
18454 | 196 |
|> space_implode "_" |
197 |
|> curry (op ^ o swap) ((implode oo replicate) i "'") |
|
198 |
end; |
|
199 |
fun is_valid _ _ = true; |
|
19150 | 200 |
fun maybe_unique thy (c, ty) = |
19136 | 201 |
if is_overloaded thy c |
202 |
then NONE |
|
203 |
else (SOME o idf_of_name thy nsp_const) c; |
|
204 |
fun re_mangle thy idf = |
|
205 |
case name_of_idf thy nsp_const idf |
|
206 |
of NONE => error ("no such constant: " ^ quote idf) |
|
207 |
| SOME c => (c, Sign.the_const_type thy c); |
|
18454 | 208 |
); |
209 |
||
210 |
structure DatatypeconsNameMangler = NameManglerFun ( |
|
211 |
type ctxt = theory; |
|
212 |
type src = string * string; |
|
213 |
val ord = prod_ord string_ord string_ord; |
|
18702 | 214 |
fun mk thy ((co, dtco), i) = |
18454 | 215 |
let |
216 |
fun basename 0 = NameSpace.base co |
|
18455 | 217 |
| basename 1 = NameSpace.base dtco ^ "_" ^ NameSpace.base co |
18454 | 218 |
| basename i = NameSpace.base dtco ^ "_" ^ NameSpace.base co ^ "_" ^ (implode oo replicate) (i-1) "'"; |
219 |
fun strip_dtco name = |
|
220 |
case (rev o NameSpace.unpack) name |
|
221 |
of x1::x2::xs => |
|
222 |
if x2 = NameSpace.base dtco |
|
223 |
then NameSpace.pack (x1::xs) |
|
224 |
else name |
|
225 |
| _ => name; |
|
226 |
in |
|
227 |
NameSpace.append (NameSpace.drop_base dtco) (basename i) |
|
228 |
|> strip_dtco |
|
229 |
end; |
|
230 |
fun is_valid _ _ = true; |
|
231 |
fun maybe_unique _ _ = NONE; |
|
232 |
fun re_mangle _ dst = error ("no such datatype constructor: " ^ quote dst); |
|
233 |
); |
|
234 |
||
19884 | 235 |
type auxtab = (bool * string list option * deftab) |
19956 | 236 |
* ((InstNameMangler.T * string Symtab.table Symtab.table) * (typ list Symtab.table * ConstNameMangler.T) |
18865 | 237 |
* DatatypeconsNameMangler.T); |
18702 | 238 |
type eqextr = theory -> auxtab |
19008 | 239 |
-> string * typ -> (thm list * typ) option; |
240 |
type eqextr_default = theory -> auxtab |
|
241 |
-> string * typ -> ((thm list * term option) * typ) option; |
|
18865 | 242 |
type appgen = theory -> auxtab |
243 |
-> (string * typ) * term list -> transact -> iexpr * transact; |
|
18217 | 244 |
|
18702 | 245 |
val serializers = ref ( |
246 |
Symtab.empty |
|
247 |
|> Symtab.update ( |
|
248 |
#ml CodegenSerializer.serializers |
|
249 |
|> apsnd (fn seri => seri |
|
18865 | 250 |
(nsp_dtcon, nsp_class, K false) |
19213 | 251 |
[[nsp_module], [nsp_class, nsp_tyco], [nsp_const, nsp_overl, nsp_dtcon, nsp_class, nsp_mem, nsp_inst, nsp_instmem]] |
18702 | 252 |
) |
253 |
) |
|
254 |
|> Symtab.update ( |
|
255 |
#haskell CodegenSerializer.serializers |
|
256 |
|> apsnd (fn seri => seri |
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
257 |
(nsp_dtcon, [nsp_module, nsp_class, nsp_tyco, nsp_dtcon]) |
19213 | 258 |
[[nsp_module], [nsp_class], [nsp_tyco], [nsp_const, nsp_overl, nsp_mem], [nsp_dtcon], [nsp_inst], [nsp_instmem]] |
18702 | 259 |
) |
260 |
) |
|
261 |
); |
|
18217 | 262 |
|
263 |
||
18454 | 264 |
(* theory data for code generator *) |
18217 | 265 |
|
18912 | 266 |
fun merge_opt _ (x1, NONE) = x1 |
267 |
| merge_opt _ (NONE, x2) = x2 |
|
268 |
| merge_opt eq (SOME x1, SOME x2) = |
|
269 |
if eq (x1, x2) then SOME x1 else error ("incompatible options during merge"); |
|
270 |
||
18217 | 271 |
type gens = { |
18516 | 272 |
appconst: ((int * int) * (appgen * stamp)) Symtab.table, |
19008 | 273 |
eqextrs: (string * (eqextr_default * stamp)) list |
18217 | 274 |
}; |
275 |
||
18865 | 276 |
fun map_gens f { appconst, eqextrs } = |
18217 | 277 |
let |
18865 | 278 |
val (appconst, eqextrs) = |
279 |
f (appconst, eqextrs) |
|
280 |
in { appconst = appconst, eqextrs = eqextrs } : gens end; |
|
18217 | 281 |
|
282 |
fun merge_gens |
|
18865 | 283 |
({ appconst = appconst1 , eqextrs = eqextrs1 }, |
284 |
{ appconst = appconst2 , eqextrs = eqextrs2 }) = |
|
18516 | 285 |
{ appconst = Symtab.merge |
18865 | 286 |
(fn ((bounds1, (_, stamp1)), (bounds2, (_, stamp2))) => bounds1 = bounds2 |
287 |
andalso stamp1 = stamp2) |
|
18516 | 288 |
(appconst1, appconst2), |
18865 | 289 |
eqextrs = AList.merge (op =) (eq_snd (op =)) (eqextrs1, eqextrs2) |
18702 | 290 |
} : gens; |
18217 | 291 |
|
292 |
type logic_data = { |
|
18455 | 293 |
get_all_datatype_cons: ((theory -> (string * string) list) * stamp) option, |
18963 | 294 |
get_datatype: ((theory -> string -> ((string * sort) list * (string * typ list) list) option) * stamp) option, |
18217 | 295 |
alias: string Symtab.table * string Symtab.table |
296 |
}; |
|
297 |
||
18963 | 298 |
fun map_logic_data f { get_all_datatype_cons, get_datatype, alias } = |
18217 | 299 |
let |
18963 | 300 |
val ((get_all_datatype_cons, get_datatype), alias) = |
301 |
f ((get_all_datatype_cons, get_datatype), alias) |
|
302 |
in { get_all_datatype_cons = get_all_datatype_cons, |
|
303 |
get_datatype = get_datatype, alias = alias } : logic_data end; |
|
18217 | 304 |
|
305 |
fun merge_logic_data |
|
18963 | 306 |
({ get_all_datatype_cons = get_all_datatype_cons1, |
307 |
get_datatype = get_datatype1, alias = alias1 }, |
|
308 |
{ get_all_datatype_cons = get_all_datatype_cons2, |
|
309 |
get_datatype = get_datatype2, alias = alias2 }) = |
|
18217 | 310 |
let |
311 |
in |
|
18963 | 312 |
{ get_all_datatype_cons = merge_opt (eq_snd (op =)) |
18865 | 313 |
(get_all_datatype_cons1, get_all_datatype_cons2), |
18963 | 314 |
get_datatype = merge_opt (eq_snd (op =)) |
315 |
(get_datatype1, get_datatype2), |
|
18217 | 316 |
alias = (Symtab.merge (op =) (fst alias1, fst alias2), |
18304 | 317 |
Symtab.merge (op =) (snd alias1, snd alias2)) } : logic_data |
18217 | 318 |
end; |
319 |
||
18702 | 320 |
type target_data = { |
18865 | 321 |
syntax_class: string Symtab.table, |
18516 | 322 |
syntax_tyco: (itype CodegenSerializer.pretty_syntax * stamp) Symtab.table, |
323 |
syntax_const: (iexpr CodegenSerializer.pretty_syntax * stamp) Symtab.table |
|
18217 | 324 |
}; |
325 |
||
18865 | 326 |
fun map_target_data f { syntax_class, syntax_tyco, syntax_const } = |
18217 | 327 |
let |
18865 | 328 |
val (syntax_class, syntax_tyco, syntax_const) = |
329 |
f (syntax_class, syntax_tyco, syntax_const) |
|
330 |
in { |
|
331 |
syntax_class = syntax_class, |
|
332 |
syntax_tyco = syntax_tyco, |
|
333 |
syntax_const = syntax_const } : target_data |
|
18454 | 334 |
end; |
18217 | 335 |
|
18702 | 336 |
fun merge_target_data |
18865 | 337 |
({ syntax_class = syntax_class1, syntax_tyco = syntax_tyco1, syntax_const = syntax_const1 }, |
338 |
{ syntax_class = syntax_class2, syntax_tyco = syntax_tyco2, syntax_const = syntax_const2 }) = |
|
339 |
{ syntax_class = Symtab.merge (op =) (syntax_class1, syntax_class2), |
|
340 |
syntax_tyco = Symtab.merge (eq_snd (op =)) (syntax_tyco1, syntax_tyco2), |
|
18702 | 341 |
syntax_const = Symtab.merge (eq_snd (op =)) (syntax_const1, syntax_const2) } : target_data; |
18217 | 342 |
|
343 |
structure CodegenData = TheoryDataFun |
|
344 |
(struct |
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
345 |
val name = "Pure/codegen_package"; |
18217 | 346 |
type T = { |
347 |
modl: module, |
|
348 |
gens: gens, |
|
349 |
logic_data: logic_data, |
|
18702 | 350 |
target_data: target_data Symtab.table |
18217 | 351 |
}; |
352 |
val empty = { |
|
353 |
modl = empty_module, |
|
18865 | 354 |
gens = { appconst = Symtab.empty, eqextrs = [] } : gens, |
18963 | 355 |
logic_data = { get_all_datatype_cons = NONE, |
356 |
get_datatype = NONE, |
|
18454 | 357 |
alias = (Symtab.empty, Symtab.empty) } : logic_data, |
18702 | 358 |
target_data = |
18217 | 359 |
Symtab.empty |
18702 | 360 |
|> Symtab.fold (fn (target, _) => |
18865 | 361 |
Symtab.update (target, |
362 |
{ syntax_class = Symtab.empty, syntax_tyco = Symtab.empty, syntax_const = Symtab.empty }) |
|
18702 | 363 |
) (! serializers) |
18217 | 364 |
} : T; |
365 |
val copy = I; |
|
366 |
val extend = I; |
|
367 |
fun merge _ ( |
|
18702 | 368 |
{ modl = modl1, gens = gens1, |
369 |
target_data = target_data1, logic_data = logic_data1 }, |
|
370 |
{ modl = modl2, gens = gens2, |
|
371 |
target_data = target_data2, logic_data = logic_data2 } |
|
18217 | 372 |
) = { |
373 |
modl = merge_module (modl1, modl2), |
|
374 |
gens = merge_gens (gens1, gens2), |
|
375 |
logic_data = merge_logic_data (logic_data1, logic_data2), |
|
19025 | 376 |
target_data = Symtab.join (K merge_target_data) (target_data1, target_data2) |
18217 | 377 |
}; |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
378 |
fun print thy (data : T) = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
379 |
let |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
380 |
val module = #modl data |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
381 |
in |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
382 |
(Pretty.writeln o Pretty.chunks) [pretty_module module, pretty_deps module] |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
383 |
end; |
18217 | 384 |
end); |
385 |
||
18708 | 386 |
val _ = Context.add_setup CodegenData.init; |
387 |
||
18217 | 388 |
fun map_codegen_data f thy = |
389 |
case CodegenData.get thy |
|
18702 | 390 |
of { modl, gens, target_data, logic_data } => |
391 |
let val (modl, gens, target_data, logic_data) = |
|
392 |
f (modl, gens, target_data, logic_data) |
|
393 |
in CodegenData.put { modl = modl, gens = gens, |
|
394 |
target_data = target_data, logic_data = logic_data } thy end; |
|
18217 | 395 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
396 |
val print_code = CodegenData.print; |
18217 | 397 |
|
19956 | 398 |
|
19038 | 399 |
(* advanced name handling *) |
18217 | 400 |
|
401 |
fun add_alias (src, dst) = |
|
402 |
map_codegen_data |
|
18702 | 403 |
(fn (modl, gens, target_data, logic_data) => |
404 |
(modl, gens, target_data, |
|
18217 | 405 |
logic_data |> map_logic_data |
406 |
(apsnd (fn (tab, tab_rev) => |
|
407 |
(tab |> Symtab.update (src, dst), |
|
408 |
tab_rev |> Symtab.update (dst, src)))))); |
|
409 |
||
19038 | 410 |
val _ = alias_ref := (perhaps o Symtab.lookup o fst o #alias o #logic_data o CodegenData.get, |
411 |
perhaps o Symtab.lookup o snd o #alias o #logic_data o CodegenData.get); |
|
18702 | 412 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
413 |
fun idf_of_co thy (tabs as (_, (_, _, dtcontab))) (co, dtco) = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
414 |
case CodegenTheorems.get_datatypes thy dtco |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
415 |
of SOME ((_, cos), _) => if AList.defined (op =) cos co |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
416 |
then try (DatatypeconsNameMangler.get thy dtcontab) (co, dtco) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
417 |
|> the_default co |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
418 |
|> idf_of_name thy nsp_dtcon |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
419 |
|> SOME |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
420 |
else NONE |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
421 |
| NONE => NONE; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
422 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
423 |
fun co_of_idf thy (tabs as (_, (_, _, dtcontab))) idf = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
424 |
case name_of_idf thy nsp_dtcon idf |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
425 |
of SOME idf' => let |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
426 |
val (c, dtco) = case try (DatatypeconsNameMangler.rev thy dtcontab) idf' |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
427 |
of SOME c_dtco => c_dtco |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
428 |
| NONE => case (snd o strip_type o Sign.the_const_type thy) idf' |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
429 |
of Type (dtco, _) => (idf', dtco) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
430 |
| _ => (idf', "nat") (*a hack*) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
431 |
in SOME (c, dtco) end |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
432 |
| NONE => NONE; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
433 |
|
19884 | 434 |
fun idf_of_const thy (tabs as (_, (_, (overltab1, overltab2), _))) |
18865 | 435 |
(c, ty) = |
18454 | 436 |
let |
18515 | 437 |
fun get_overloaded (c, ty) = |
19202 | 438 |
(case Symtab.lookup overltab1 c |
19136 | 439 |
of SOME tys => |
18515 | 440 |
(case find_first (curry (Sign.typ_instance thy) ty) tys |
19136 | 441 |
of SOME ty' => ConstNameMangler.get thy overltab2 |
442 |
(c, ty') |> SOME |
|
18515 | 443 |
| _ => NONE) |
19202 | 444 |
| _ => NONE) |
18515 | 445 |
fun get_datatypecons (c, ty) = |
446 |
case (snd o strip_type) ty |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
447 |
of Type (tyco, _) => idf_of_co thy tabs (c, tyco) |
18515 | 448 |
| _ => NONE; |
18702 | 449 |
in case get_datatypecons (c, ty) |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
450 |
of SOME idf => idf |
18702 | 451 |
| NONE => case get_overloaded (c, ty) |
18515 | 452 |
of SOME idf => idf |
19956 | 453 |
| NONE => case AxClass.class_of thy c |
18515 | 454 |
of SOME _ => idf_of_name thy nsp_mem c |
455 |
| NONE => idf_of_name thy nsp_const c |
|
456 |
end; |
|
18217 | 457 |
|
19884 | 458 |
fun idf_of_const' thy (tabs as (_, (_, (overltab1, overltab2), _))) |
459 |
(c, ty) = |
|
460 |
let |
|
461 |
fun get_overloaded (c, ty) = |
|
462 |
(case Symtab.lookup overltab1 c |
|
463 |
of SOME tys => |
|
464 |
(case find_first (curry (Sign.typ_instance thy) ty) tys |
|
465 |
of SOME ty' => ConstNameMangler.get thy overltab2 |
|
466 |
(c, ty') |> SOME |
|
467 |
| _ => NONE) |
|
468 |
| _ => NONE) |
|
469 |
in case get_overloaded (c, ty) |
|
470 |
of SOME idf => idf |
|
19956 | 471 |
| NONE => case AxClass.class_of thy c |
19884 | 472 |
of SOME _ => idf_of_name thy nsp_mem c |
473 |
| NONE => idf_of_name thy nsp_const c |
|
474 |
end; |
|
475 |
||
19177 | 476 |
fun recconst_of_idf thy (_, (_, (_, overltab2), _)) idf = |
18454 | 477 |
case name_of_idf thy nsp_const idf |
18702 | 478 |
of SOME c => SOME (c, Sign.the_const_type thy c) |
18454 | 479 |
| NONE => ( |
480 |
case dest_nsp nsp_overl idf |
|
481 |
of SOME _ => |
|
482 |
idf |
|
19136 | 483 |
|> ConstNameMangler.rev thy overltab2 |
18454 | 484 |
|> SOME |
485 |
| NONE => NONE |
|
486 |
); |
|
487 |
||
18865 | 488 |
|
489 |
(* further theory data accessors *) |
|
490 |
||
491 |
fun gen_add_appconst prep_const (raw_c, (bounds, ag)) thy = |
|
18454 | 492 |
let |
18865 | 493 |
val c = prep_const thy raw_c; |
494 |
in map_codegen_data |
|
495 |
(fn (modl, gens, target_data, logic_data) => |
|
496 |
(modl, |
|
497 |
gens |> map_gens |
|
498 |
(fn (appconst, eqextrs) => |
|
499 |
(appconst |
|
500 |
|> Symtab.update (c, (bounds, (ag, stamp ()))), |
|
501 |
eqextrs)), target_data, logic_data)) thy |
|
18454 | 502 |
end; |
18217 | 503 |
|
18865 | 504 |
val add_appconst = gen_add_appconst Sign.intern_const; |
505 |
val add_appconst_i = gen_add_appconst (K I); |
|
506 |
||
507 |
fun set_get_all_datatype_cons f = |
|
508 |
map_codegen_data |
|
509 |
(fn (modl, gens, target_data, logic_data) => |
|
510 |
(modl, gens, target_data, |
|
511 |
logic_data |
|
18963 | 512 |
|> map_logic_data ((apfst (fn (get_all_datatype_cons, get_datatype) |
513 |
=> (SOME (f, stamp ()), get_datatype)))))); |
|
18865 | 514 |
|
515 |
fun get_all_datatype_cons thy = |
|
516 |
case (#get_all_datatype_cons o #logic_data o CodegenData.get) thy |
|
517 |
of NONE => [] |
|
518 |
| SOME (f, _) => f thy; |
|
519 |
||
18963 | 520 |
fun set_get_datatype f = |
18865 | 521 |
map_codegen_data |
522 |
(fn (modl, gens, target_data, logic_data) => |
|
523 |
(modl, gens, target_data, |
|
524 |
logic_data |
|
18963 | 525 |
|> map_logic_data ((apfst (fn (get_all_datatype_cons, get_datatype) |
526 |
=> (get_all_datatype_cons, SOME (f, stamp ()))))))); |
|
18865 | 527 |
|
18963 | 528 |
fun get_datatype thy = |
529 |
case (#get_datatype o #logic_data o CodegenData.get) thy |
|
530 |
of NONE => K NONE |
|
531 |
| SOME (f, _) => f thy; |
|
18865 | 532 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
533 |
fun const_of_idf thy (tabs as (_, (_, _, dtcontab))) idf = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
534 |
case recconst_of_idf thy tabs idf |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
535 |
of SOME c_ty => SOME c_ty |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
536 |
| NONE => case dest_nsp nsp_mem idf |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
537 |
of SOME c => SOME (c, Sign.the_const_constraint thy c) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
538 |
| NONE => case co_of_idf thy tabs idf |
19785 | 539 |
of SOME (c, dtco) => |
540 |
let |
|
541 |
val (vars, cos) = (fst o the o CodegenTheorems.get_datatypes thy) dtco |
|
542 |
in |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
543 |
SOME (c, (the o AList.lookup (op =) cos) c ---> Type (dtco, map TFree vars) |> Logic.varifyT) |
19785 | 544 |
end |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
545 |
| NONE => NONE; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
546 |
|
18865 | 547 |
fun set_int_tyco tyco thy = |
548 |
(serializers := ( |
|
549 |
! serializers |
|
550 |
|> Symtab.update ( |
|
551 |
#ml CodegenSerializer.serializers |
|
552 |
|> apsnd (fn seri => seri |
|
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
553 |
(nsp_dtcon, nsp_class, fn tyco' => tyco' = idf_of_name thy nsp_tyco tyco) |
19213 | 554 |
[[nsp_module], [nsp_class, nsp_tyco], [nsp_const, nsp_overl, nsp_dtcon, nsp_mem, nsp_inst, nsp_instmem]] |
18865 | 555 |
) |
556 |
) |
|
557 |
); thy); |
|
558 |
||
559 |
||
560 |
(* definition and expression generators *) |
|
561 |
||
19884 | 562 |
fun check_strict thy f x ((false, _, _), _) = |
563 |
false |
|
564 |
| check_strict thy f x ((_, SOME targets, _), _) = |
|
565 |
exists ( |
|
566 |
is_none o (fn tab => Symtab.lookup tab x) o f o the o (Symtab.lookup ((#target_data o CodegenData.get) thy)) |
|
567 |
) targets |
|
568 |
| check_strict thy f x ((true, _, _), _) = |
|
569 |
true; |
|
570 |
||
571 |
fun no_strict ((_, targets, deftab), tabs') = ((false, targets, deftab), tabs'); |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
572 |
|
18865 | 573 |
fun ensure_def_class thy tabs cls trns = |
18454 | 574 |
let |
19956 | 575 |
fun defgen_class thy (tabs as (_, ((insttab, thynametab), _, _))) cls trns = |
18865 | 576 |
case name_of_idf thy nsp_class cls |
577 |
of SOME cls => |
|
578 |
let |
|
19283 | 579 |
val (v, cs) = (ClassPackage.the_consts_sign thy) cls; |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
580 |
val sortctxts = map (ClassPackage.sortcontext_of_typ thy o snd) cs; |
18865 | 581 |
val idfs = map (idf_of_name thy nsp_mem o fst) cs; |
582 |
in |
|
583 |
trns |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
584 |
|> debug_msg (fn _ => "trying defgen class declaration for " ^ quote cls) |
18865 | 585 |
|> fold_map (ensure_def_class thy tabs) (ClassPackage.the_superclasses thy cls) |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
586 |
||>> (fold_map (exprgen_type thy tabs) o map snd) cs |
18865 | 587 |
||>> (fold_map o fold_map) (exprgen_tyvar_sort thy tabs) sortctxts |
588 |
|-> (fn ((supcls, memtypes), sortctxts) => succeed |
|
19283 | 589 |
(Class (supcls, (unprefix "'" v, idfs ~~ (sortctxts ~~ memtypes))))) |
18865 | 590 |
end |
591 |
| _ => |
|
592 |
trns |
|
593 |
|> fail ("no class definition found for " ^ quote cls); |
|
594 |
val cls' = idf_of_name thy nsp_class cls; |
|
18454 | 595 |
in |
596 |
trns |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
597 |
|> debug_msg (fn _ => "generating class " ^ quote cls) |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
598 |
|> ensure_def (defgen_class thy tabs) true ("generating class " ^ quote cls) cls' |
18865 | 599 |
|> pair cls' |
600 |
end |
|
601 |
and ensure_def_tyco thy tabs tyco trns = |
|
602 |
let |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
603 |
val tyco' = idf_of_name thy nsp_tyco tyco; |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
604 |
val strict = check_strict thy #syntax_tyco tyco' tabs; |
18963 | 605 |
fun defgen_datatype thy (tabs as (_, (_, _, dtcontab))) dtco trns = |
606 |
case name_of_idf thy nsp_tyco dtco |
|
607 |
of SOME dtco => |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
608 |
(case CodegenTheorems.get_datatypes thy dtco |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
609 |
of SOME ((vars, cos), _) => |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
610 |
trns |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
611 |
|> debug_msg (fn _ => "trying defgen datatype for " ^ quote dtco) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
612 |
|> fold_map (exprgen_tyvar_sort thy tabs) vars |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
613 |
||>> fold_map (fn (c, tys) => |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
614 |
fold_map (exprgen_type thy tabs) tys |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
615 |
#-> (fn tys' => pair ((the o idf_of_co thy tabs) (c, dtco), tys'))) cos |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
616 |
|-> (fn (vars, cos) => succeed (Datatype |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
617 |
(vars, cos))) |
18963 | 618 |
| NONE => |
619 |
trns |
|
620 |
|> fail ("no datatype found for " ^ quote dtco)) |
|
621 |
| NONE => |
|
622 |
trns |
|
623 |
|> fail ("not a type constructor: " ^ quote dtco) |
|
18865 | 624 |
in |
625 |
trns |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
626 |
|> debug_msg (fn _ => "generating type constructor " ^ quote tyco) |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
627 |
|> ensure_def (defgen_datatype thy tabs) strict ("generating type constructor " ^ quote tyco) tyco' |
18865 | 628 |
|> pair tyco' |
629 |
end |
|
630 |
and exprgen_tyvar_sort thy tabs (v, sort) trns = |
|
18516 | 631 |
trns |
18885 | 632 |
|> fold_map (ensure_def_class thy tabs) (ClassPackage.operational_sort_of thy sort) |
18865 | 633 |
|-> (fn sort => pair (unprefix "'" v, sort)) |
634 |
and exprgen_type thy tabs (TVar _) trns = |
|
18516 | 635 |
error "TVar encountered during code generation" |
18702 | 636 |
| exprgen_type thy tabs (TFree v_s) trns = |
18516 | 637 |
trns |
18702 | 638 |
|> exprgen_tyvar_sort thy tabs v_s |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
639 |
|-> (fn (v, sort) => pair (ITyVar v)) |
18516 | 640 |
| exprgen_type thy tabs (Type ("fun", [t1, t2])) trns = |
641 |
trns |
|
642 |
|> exprgen_type thy tabs t1 |
|
643 |
||>> exprgen_type thy tabs t2 |
|
644 |
|-> (fn (t1', t2') => pair (t1' `-> t2')) |
|
645 |
| exprgen_type thy tabs (Type (tyco, tys)) trns = |
|
646 |
trns |
|
647 |
|> ensure_def_tyco thy tabs tyco |
|
648 |
||>> fold_map (exprgen_type thy tabs) tys |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
649 |
|-> (fn (tyco, tys) => pair (tyco `%% tys)); |
18516 | 650 |
|
18885 | 651 |
fun exprgen_classlookup thy tabs (ClassPackage.Instance (inst, ls)) trns = |
18517 | 652 |
trns |
18885 | 653 |
|> ensure_def_inst thy tabs inst |
654 |
||>> (fold_map o fold_map) (exprgen_classlookup thy tabs) ls |
|
655 |
|-> (fn (inst, ls) => pair (Instance (inst, ls))) |
|
19253 | 656 |
| exprgen_classlookup thy tabs (ClassPackage.Lookup (clss, (v, (i, j)))) trns = |
18516 | 657 |
trns |
18517 | 658 |
|> fold_map (ensure_def_class thy tabs) clss |
19253 | 659 |
|-> (fn clss => pair (Lookup (clss, (v |> unprefix "'", if j = 1 then ~1 else i)))) |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
660 |
and mk_fun thy tabs (c, ty) trns = |
19806 | 661 |
case CodegenTheorems.get_funs thy (c, Logic.legacy_varifyT ty) (* FIXME *) |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
662 |
of eq_thms as eq_thm :: _ => |
18865 | 663 |
let |
19884 | 664 |
val msg = cat_lines ("generating code for theorems " :: map string_of_thm eq_thms); |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
665 |
val ty = (Logic.legacy_unvarifyT o CodegenTheorems.extr_typ thy) eq_thm |
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
666 |
val sortcontext = ClassPackage.sortcontext_of_typ thy ty; |
18865 | 667 |
fun dest_eqthm eq_thm = |
668 |
let |
|
669 |
val ((t, args), rhs) = |
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
670 |
(apfst strip_comb o Logic.dest_equals o Logic.legacy_unvarify o prop_of) eq_thm; |
18865 | 671 |
in case t |
672 |
of Const (c', _) => if c' = c then (args, rhs) |
|
673 |
else error ("illegal function equation for " ^ quote c |
|
674 |
^ ", actually defining " ^ quote c') |
|
675 |
| _ => error ("illegal function equation for " ^ quote c) |
|
676 |
end; |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
677 |
fun exprgen_eq (args, rhs) trns = |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
678 |
trns |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
679 |
|> fold_map (exprgen_term thy tabs) args |
19884 | 680 |
||>> exprgen_term thy tabs rhs; |
18865 | 681 |
in |
18517 | 682 |
trns |
19884 | 683 |
|> message msg (fn trns => trns |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
684 |
|> fold_map (exprgen_eq o dest_eqthm) eq_thms |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
685 |
||>> fold_map (exprgen_tyvar_sort thy tabs) sortcontext |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
686 |
||>> exprgen_type thy tabs ty |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
687 |
|-> (fn ((eqs, sortctxt), ty) => (pair o SOME) ((eqs, (sortctxt, ty)), map snd sortcontext))) |
18865 | 688 |
end |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
689 |
| [] => (NONE, trns) |
19956 | 690 |
and ensure_def_inst thy (tabs as (_, ((insttab, thynametab), _, _))) (cls, tyco) trns = |
18865 | 691 |
let |
19956 | 692 |
fun defgen_inst thy (tabs as (_, ((insttab, thynametab), _, _))) inst trns = |
693 |
case Option.map (InstNameMangler.rev thy insttab o NameSpace.base) |
|
694 |
(name_of_idf thy nsp_inst inst) |
|
695 |
of SOME (class, tyco) => |
|
18865 | 696 |
let |
18885 | 697 |
val (arity, memdefs) = ClassPackage.the_inst_sign thy (class, tyco); |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
698 |
val arity_typ = Type (tyco, (map TFree arity)); |
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
699 |
val operational_arity = map_filter (fn (v, sort) => case ClassPackage.operational_sort_of thy sort |
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
700 |
of [] => NONE |
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
701 |
| sort => SOME (v, sort)) arity; |
18865 | 702 |
fun gen_suparity supclass trns = |
703 |
trns |
|
19136 | 704 |
|> ensure_def_class thy tabs supclass |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
705 |
||>> fold_map (exprgen_classlookup thy tabs) |
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
706 |
(ClassPackage.sortlookup thy ([supclass], arity_typ)); |
18865 | 707 |
fun gen_membr (m, ty) trns = |
708 |
trns |
|
19956 | 709 |
|> tap (fn _ => writeln ("(1) " ^ m)) |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
710 |
|> mk_fun thy tabs (m, ty) |
19956 | 711 |
|> tap (fn _ => writeln "(2)") |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
712 |
|-> (fn NONE => error ("could not derive definition for member " |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
713 |
^ quote m ^ " :: " ^ Sign.string_of_typ thy ty) |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
714 |
| SOME (funn, sorts) => fold_map (fn (sort, sort_ctxt) => |
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
715 |
fold_map (exprgen_classlookup thy tabs) |
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
716 |
(ClassPackage.sortlookup thy (sort, TFree sort_ctxt))) |
19956 | 717 |
(print sorts ~~ print operational_arity) |
718 |
#> tap (fn _ => writeln "(3)") |
|
19253 | 719 |
#-> (fn lss => |
720 |
pair (idf_of_name thy nsp_mem m, ((idf_of_name thy nsp_instmem m, funn), lss)))); |
|
18865 | 721 |
in |
722 |
trns |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
723 |
|> debug_msg (fn _ => "trying defgen class instance for (" ^ quote cls |
18865 | 724 |
^ ", " ^ quote tyco ^ ")") |
18885 | 725 |
|> ensure_def_class thy tabs class |
18865 | 726 |
||>> ensure_def_tyco thy tabs tyco |
727 |
||>> fold_map (exprgen_tyvar_sort thy tabs) arity |
|
18885 | 728 |
||>> fold_map gen_suparity (ClassPackage.the_superclasses thy class) |
18865 | 729 |
||>> fold_map gen_membr memdefs |
18885 | 730 |
|-> (fn ((((class, tyco), arity), suparities), memdefs) => |
731 |
succeed (Classinst (((class, (tyco, arity)), suparities), memdefs))) |
|
18865 | 732 |
end |
733 |
| _ => |
|
734 |
trns |> fail ("no class instance found for " ^ quote inst); |
|
19956 | 735 |
val thyname = (the o Symtab.lookup ((the o Symtab.lookup thynametab) cls)) tyco; |
736 |
val inst = (idf_of_name thy nsp_inst o NameSpace.append thyname o InstNameMangler.get thy insttab) |
|
737 |
(cls, tyco); |
|
18865 | 738 |
in |
739 |
trns |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
740 |
|> debug_msg (fn _ => "generating instance " ^ quote cls ^ " / " ^ quote tyco) |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
741 |
|> ensure_def (defgen_inst thy tabs) true |
18865 | 742 |
("generating instance " ^ quote cls ^ " / " ^ quote tyco) inst |
743 |
|> pair inst |
|
744 |
end |
|
19213 | 745 |
and ensure_def_const thy (tabs as (_, (_, overltab, dtcontab))) (c, ty) trns = |
18865 | 746 |
let |
747 |
fun defgen_funs thy tabs c trns = |
|
748 |
case recconst_of_idf thy tabs c |
|
749 |
of SOME (c, ty) => |
|
750 |
trns |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
751 |
|> mk_fun thy tabs (c, ty) |
19253 | 752 |
|-> (fn SOME (funn, _) => succeed (Fun funn) |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
753 |
| NONE => fail ("no defining equations found for " ^ |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
754 |
(quote o Display.raw_string_of_term o Const) (c, ty))) |
18865 | 755 |
| NONE => |
756 |
trns |
|
757 |
|> fail ("not a constant: " ^ quote c); |
|
18963 | 758 |
fun defgen_clsmem thy tabs m trns = |
759 |
case name_of_idf thy nsp_mem m |
|
760 |
of SOME m => |
|
761 |
trns |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
762 |
|> debug_msg (fn _ => "trying defgen class member for " ^ quote m) |
19956 | 763 |
|> ensure_def_class thy tabs ((the o AxClass.class_of thy) m) |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
764 |
|-> (fn cls => succeed Bot) |
18963 | 765 |
| _ => |
766 |
trns |> fail ("no class member found for " ^ quote m) |
|
18865 | 767 |
fun defgen_datatypecons thy (tabs as (_, (_, _, dtcontab))) co trns = |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
768 |
case co_of_idf thy tabs co |
18865 | 769 |
of SOME (co, dtco) => |
770 |
trns |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
771 |
|> debug_msg (fn _ => "trying defgen datatype constructor for " ^ quote co) |
18865 | 772 |
|> ensure_def_tyco thy tabs dtco |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
773 |
|-> (fn dtco => succeed Bot) |
18865 | 774 |
| _ => |
775 |
trns |
|
776 |
|> fail ("not a datatype constructor: " ^ quote co); |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
777 |
fun get_defgen tabs idf strict = |
18963 | 778 |
if (is_some oo name_of_idf thy) nsp_const idf |
779 |
orelse (is_some oo name_of_idf thy) nsp_overl idf |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
780 |
then defgen_funs thy tabs strict |
18963 | 781 |
else if (is_some oo name_of_idf thy) nsp_mem idf |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
782 |
then defgen_clsmem thy tabs strict |
18963 | 783 |
else if (is_some oo name_of_idf thy) nsp_dtcon idf |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
784 |
then defgen_datatypecons thy tabs strict |
18963 | 785 |
else error ("illegal shallow name space for constant: " ^ quote idf); |
786 |
val idf = idf_of_const thy tabs (c, ty); |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
787 |
val strict = check_strict thy #syntax_const idf tabs; |
18865 | 788 |
in |
789 |
trns |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
790 |
|> debug_msg (fn _ => "generating constant " ^ (quote o Display.raw_string_of_term o Const) (c, ty)) |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
791 |
|> ensure_def (get_defgen tabs idf) strict ("generating constant " ^ quote c) idf |
18963 | 792 |
|> pair idf |
18865 | 793 |
end |
18517 | 794 |
and exprgen_term thy tabs (Const (f, ty)) trns = |
795 |
trns |
|
796 |
|> appgen thy tabs ((f, ty), []) |
|
18516 | 797 |
|-> (fn e => pair e) |
18912 | 798 |
| exprgen_term thy tabs (Var _) trns = |
799 |
error "Var encountered during code generation" |
|
18516 | 800 |
| exprgen_term thy tabs (Free (v, ty)) trns = |
801 |
trns |
|
18912 | 802 |
|> exprgen_type thy tabs ty |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
803 |
|-> (fn ty => pair (IVar v)) |
19967 | 804 |
| exprgen_term thy tabs (Abs (raw_v, ty, raw_t)) trns = |
19136 | 805 |
let |
19967 | 806 |
val (v, t) = Term.variant_abs (CodegenTheorems.proper_name raw_v, ty, raw_t); |
19136 | 807 |
in |
808 |
trns |
|
809 |
|> exprgen_type thy tabs ty |
|
810 |
||>> exprgen_term thy tabs t |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
811 |
|-> (fn (ty, e) => pair ((v, ty) `|-> e)) |
19136 | 812 |
end |
18516 | 813 |
| exprgen_term thy tabs (t as t1 $ t2) trns = |
814 |
let |
|
815 |
val (t', ts) = strip_comb t |
|
816 |
in case t' |
|
817 |
of Const (f, ty) => |
|
818 |
trns |
|
18517 | 819 |
|> appgen thy tabs ((f, ty), ts) |
18516 | 820 |
|-> (fn e => pair e) |
821 |
| _ => |
|
822 |
trns |
|
823 |
|> exprgen_term thy tabs t' |
|
824 |
||>> fold_map (exprgen_term thy tabs) ts |
|
825 |
|-> (fn (e, es) => pair (e `$$ es)) |
|
18865 | 826 |
end |
827 |
and appgen_default thy tabs ((c, ty), ts) trns = |
|
828 |
trns |
|
829 |
|> ensure_def_const thy tabs (c, ty) |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
830 |
||>> exprgen_type thy tabs ty |
18885 | 831 |
||>> (fold_map o fold_map) (exprgen_classlookup thy tabs) |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
832 |
(ClassPackage.sortlookups_const thy (c, ty)) |
18912 | 833 |
||>> fold_map (exprgen_term thy tabs) ts |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
834 |
|-> (fn (((c, ty), ls), es) => |
19202 | 835 |
pair (IConst (c, (ls, ty)) `$$ es)) |
18865 | 836 |
and appgen thy tabs ((f, ty), ts) trns = |
837 |
case Symtab.lookup ((#appconst o #gens o CodegenData.get) thy) f |
|
838 |
of SOME ((imin, imax), (ag, _)) => |
|
839 |
if length ts < imin then |
|
840 |
let |
|
841 |
val d = imin - length ts; |
|
20076 | 842 |
val vs = Name.invent_list (add_term_names (Const (f, ty), [])) "x" d; |
18865 | 843 |
val tys = Library.take (d, ((fst o strip_type) ty)); |
844 |
in |
|
845 |
trns |
|
18912 | 846 |
|> fold_map (exprgen_type thy tabs) tys |
18865 | 847 |
||>> ag thy tabs ((f, ty), ts @ map2 (curry Free) vs tys) |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
848 |
|-> (fn (tys, e) => pair (vs ~~ tys `|--> e)) |
18865 | 849 |
end |
850 |
else if length ts > imax then |
|
851 |
trns |
|
852 |
|> ag thy tabs ((f, ty), Library.take (imax, ts)) |
|
853 |
||>> fold_map (exprgen_term thy tabs) (Library.drop (imax, ts)) |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
854 |
|-> (fn (e, es) => pair (e `$$ es)) |
18865 | 855 |
else |
856 |
trns |
|
857 |
|> ag thy tabs ((f, ty), ts) |
|
858 |
| NONE => |
|
859 |
trns |
|
860 |
|> appgen_default thy tabs ((f, ty), ts); |
|
18516 | 861 |
|
18702 | 862 |
|
18217 | 863 |
(* parametrized generators, for instantiation in HOL *) |
864 |
||
19213 | 865 |
fun appgen_split strip_abs thy tabs (app as (c_ty, [t])) trns = |
866 |
case strip_abs 1 (Const c_ty $ t) |
|
19202 | 867 |
of ([vt], bt) => |
19150 | 868 |
trns |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
869 |
|> exprgen_term thy tabs vt |
19202 | 870 |
||>> exprgen_type thy tabs (type_of vt) |
871 |
||>> exprgen_term thy tabs bt |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
872 |
||>> appgen_default thy tabs app |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
873 |
|-> (fn (((ve, vty), be), e0) => pair (IAbs (((ve, vty), be), e0))) |
19150 | 874 |
| _ => |
875 |
trns |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
876 |
|> appgen_default thy tabs app; |
18335 | 877 |
|
19202 | 878 |
fun appgen_let strip_abs thy tabs (app as ((c, ty), [dt, ct])) trns = |
879 |
case strip_abs 1 ct |
|
880 |
of ([st], bt) => |
|
881 |
trns |
|
882 |
|> exprgen_term thy tabs dt |
|
883 |
||>> exprgen_type thy tabs (type_of dt) |
|
884 |
||>> exprgen_term thy tabs st |
|
885 |
||>> exprgen_term thy tabs bt |
|
886 |
||>> appgen_default thy tabs app |
|
887 |
|-> (fn ((((de, dty), se), be), e0) => pair (ICase (((de, dty), [(se, be)]), e0))) |
|
888 |
| _ => |
|
889 |
trns |
|
890 |
|> appgen_default thy tabs app; |
|
891 |
||
19884 | 892 |
fun appgen_number_of int_of_bin thy tabs (app as (c as (_, ty), [bin])) trns = |
893 |
case try (int_of_bin thy) bin |
|
894 |
of SOME i => if i < 0 then error ("negative numeral: " ^ IntInf.toString i) |
|
895 |
(*preprocessor eliminates nat and negative numerals*) |
|
896 |
else |
|
897 |
trns |
|
898 |
|> pair (CodegenThingol.INum (i, IVar "")) |
|
899 |
(*|> exprgen_term thy (no_strict tabs) (Const c) |
|
900 |
||>> exprgen_term thy (no_strict tabs) bin |
|
901 |
|-> (fn (e1, e2) => pair (CodegenThingol.INum (i, e1 `$ e2)))*) |
|
902 |
| NONE => |
|
903 |
trns |
|
904 |
|> appgen_default thy tabs app; |
|
18217 | 905 |
|
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
906 |
fun appgen_char char_to_index thy tabs (app as ((_, ty), _)) trns = |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
907 |
case (char_to_index o list_comb o apfst Const) app |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
908 |
of SOME i => |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
909 |
trns |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
910 |
|> exprgen_type thy tabs ty |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
911 |
||>> appgen_default thy tabs app |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
912 |
|-> (fn (_, e0) => pair (IChar (chr i, e0))) |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
913 |
| NONE => |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
914 |
trns |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
915 |
|> appgen_default thy tabs app; |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
916 |
|
19038 | 917 |
fun appgen_wfrec thy tabs ((c, ty), [_, tf, tx]) trns = |
918 |
let |
|
19806 | 919 |
val ty_def = (op ---> o apfst tl o strip_type o Logic.legacy_unvarifyT o Sign.the_const_type thy) c; |
19038 | 920 |
val ty' = (op ---> o apfst tl o strip_type) ty; |
921 |
val idf = idf_of_const thy tabs (c, ty); |
|
922 |
in |
|
923 |
trns |
|
19884 | 924 |
|> ensure_def ((K o fail) "no extraction of wfrec") false ("generating wfrec") idf |
19136 | 925 |
|> exprgen_type thy tabs ty' |
926 |
||>> (fold_map o fold_map) (exprgen_classlookup thy tabs) |
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
927 |
(ClassPackage.sortlookups_const thy (c, ty)) |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
928 |
||>> exprgen_type thy tabs ty_def |
19136 | 929 |
||>> exprgen_term thy tabs tf |
930 |
||>> exprgen_term thy tabs tx |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
931 |
|-> (fn ((((_, ls), ty), tf), tx) => pair (IConst (idf, (ls, ty)) `$ tf `$ tx)) |
19038 | 932 |
end; |
933 |
||
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
934 |
fun appgen_datatype_case cos thy tabs (app as ((_, ty), ts)) trns = |
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
935 |
let |
18517 | 936 |
val (ts', t) = split_last ts; |
937 |
val (tys, dty) = (split_last o fst o strip_type) ty; |
|
938 |
fun gen_names i = |
|
20076 | 939 |
Name.invent_list (foldr add_term_names |
940 |
(map (fst o fst o dest_Var) (foldr add_term_vars [] ts)) ts) "xa" i; |
|
18517 | 941 |
fun cg_case_d (((cname, i), ty), t) trns = |
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
942 |
let |
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
943 |
val vs = gen_names i; |
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
944 |
val tys = Library.take (i, (fst o strip_type) ty); |
18330 | 945 |
val frees = map2 (curry Free) vs tys; |
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
946 |
val t' = Envir.beta_norm (list_comb (t, frees)); |
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
947 |
in |
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
948 |
trns |
18516 | 949 |
|> exprgen_term thy tabs (list_comb (Const (cname, tys ---> dty), frees)) |
950 |
||>> exprgen_term thy tabs t' |
|
18865 | 951 |
|-> (fn (ep, e) => pair (ep, e)) |
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
952 |
end; |
18517 | 953 |
in |
954 |
trns |
|
955 |
|> exprgen_term thy tabs t |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
956 |
||>> exprgen_type thy tabs dty |
18517 | 957 |
||>> fold_map cg_case_d ((cos ~~ tys) ~~ ts') |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
958 |
||>> appgen_default thy tabs app |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
959 |
|-> (fn (((de, dty), bses), e0) => pair (ICase (((de, dty), bses), e0))) |
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
960 |
end; |
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
961 |
|
19597 | 962 |
fun add_case_const c cos thy = |
18517 | 963 |
let |
964 |
val n_eta = length cos + 1; |
|
965 |
in |
|
966 |
thy |
|
967 |
|> add_appconst_i (c, ((n_eta, n_eta), appgen_datatype_case cos)) |
|
968 |
end; |
|
969 |
||
18217 | 970 |
|
18516 | 971 |
|
972 |
(** theory interface **) |
|
18217 | 973 |
|
19884 | 974 |
fun mk_tabs thy targets = |
18217 | 975 |
let |
19956 | 976 |
fun mk_insttab thy = |
977 |
let |
|
978 |
val insts = Symtab.fold |
|
979 |
(fn (tyco, classes) => cons (tyco, map fst classes)) |
|
980 |
((#arities o Sorts.rep_algebra o Sign.classes_of) thy) |
|
981 |
[] |
|
982 |
in ( |
|
983 |
InstNameMangler.empty |
|
984 |
|> fold |
|
985 |
(fn (tyco, classes) => fold |
|
986 |
(fn class => InstNameMangler.declare thy (class, tyco) #> snd) classes) |
|
987 |
insts, |
|
988 |
Symtab.empty |
|
989 |
|> fold |
|
990 |
(fn (tyco, classes) => fold |
|
991 |
(fn class => Symtab.default (class, Symtab.empty) |
|
992 |
#> Symtab.map_entry class (Symtab.update (tyco, thyname_of_instance thy (class, tyco)))) classes) |
|
993 |
insts |
|
994 |
) end; |
|
19136 | 995 |
fun mk_overltabs thy = |
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
996 |
(Symtab.empty, ConstNameMangler.empty) |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
997 |
|> Symtab.fold |
19136 | 998 |
(fn (c, _) => |
999 |
let |
|
19571 | 1000 |
val deftab = Theory.definitions_of thy c |
19956 | 1001 |
val is_overl = (is_none o AxClass.class_of thy) c |
19571 | 1002 |
andalso case deftab (* is_overloaded (!?) *) |
19136 | 1003 |
of [] => false |
19571 | 1004 |
| [{lhs = ty, ...}] => not (Sign.typ_equiv thy (ty, Sign.the_const_type thy c)) |
19136 | 1005 |
| _ => true; |
1006 |
in if is_overl then (fn (overltab1, overltab2) => ( |
|
1007 |
overltab1 |
|
19571 | 1008 |
|> Symtab.update_new (c, map #lhs deftab), |
19136 | 1009 |
overltab2 |
19571 | 1010 |
|> fold_map (fn {lhs = ty, ...} => ConstNameMangler.declare thy (c, ty)) deftab |
19136 | 1011 |
|-> (fn _ => I))) else I |
1012 |
end) ((#2 o #constants o Consts.dest o #consts o Sign.rep_sg) thy) |
|
1013 |
|> (fn (overltab1, overltab2) => |
|
1014 |
let |
|
20076 | 1015 |
val c = "op ="; (* FIXME depends on object-logic!? *) |
19136 | 1016 |
val ty = Sign.the_const_type thy c; |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1017 |
fun inst tyco = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1018 |
let |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1019 |
val ty_inst = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1020 |
tyco |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1021 |
|> Symtab.lookup ((snd o #types o Type.rep_tsig o Sign.tsig_of) thy) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1022 |
|> (fn SOME (Type.LogicalType i, _) => i) |
20076 | 1023 |
|> Name.invent_list [] "'a" |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1024 |
|> map (fn v => (TVar ((v, 0), Sign.defaultS thy))) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1025 |
|> (fn tys => Type (tyco, tys)) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1026 |
in map_atyps (fn _ => ty_inst) ty end; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1027 |
val tys = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1028 |
(Type.logical_types o Sign.tsig_of) thy |
19466 | 1029 |
|> filter (fn tyco => can (Sign.arity_sorts thy tyco) (Sign.defaultS thy)) |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1030 |
|> map inst |
19136 | 1031 |
in |
1032 |
(overltab1 |
|
1033 |
|> Symtab.update_new (c, tys), |
|
1034 |
overltab2 |
|
1035 |
|> fold (fn ty => ConstNameMangler.declare thy (c, ty) #> snd) tys) |
|
1036 |
end); |
|
18454 | 1037 |
fun mk_dtcontab thy = |
1038 |
DatatypeconsNameMangler.empty |
|
1039 |
|> fold_map |
|
18455 | 1040 |
(fn (_, co_dtco) => DatatypeconsNameMangler.declare_multi thy co_dtco) |
1041 |
(fold (fn (co, dtco) => |
|
1042 |
let |
|
19008 | 1043 |
val key = ((NameSpace.drop_base o NameSpace.drop_base) co, NameSpace.base co); |
18455 | 1044 |
in AList.default (op =) (key, []) #> AList.map_entry (op =) key (cons (co, dtco)) end |
1045 |
) (get_all_datatype_cons thy) []) |
|
18454 | 1046 |
|-> (fn _ => I); |
1047 |
val insttab = mk_insttab thy; |
|
19136 | 1048 |
val overltabs = mk_overltabs thy; |
18454 | 1049 |
val dtcontab = mk_dtcontab thy; |
19884 | 1050 |
in ((true, targets, Symtab.empty), (insttab, overltabs, dtcontab)) end; |
18217 | 1051 |
|
18756 | 1052 |
fun get_serializer target = |
1053 |
case Symtab.lookup (!serializers) target |
|
1054 |
of SOME seri => seri |
|
19008 | 1055 |
| NONE => Scan.fail_with (fn _ => "unknown code target language: " ^ quote target) (); |
18335 | 1056 |
|
18516 | 1057 |
fun map_module f = |
18702 | 1058 |
map_codegen_data (fn (modl, gens, target_data, logic_data) => |
1059 |
(f modl, gens, target_data, logic_data)); |
|
18516 | 1060 |
|
19884 | 1061 |
fun purge_defs NONE thy = |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1062 |
map_module (K CodegenThingol.empty_module) thy |
19884 | 1063 |
| purge_defs (SOME cs) thy = |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1064 |
let |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1065 |
val tabs = mk_tabs thy NONE; |
19884 | 1066 |
val idfs = map (idf_of_const' thy tabs) cs; |
1067 |
fun purge idfs modl = |
|
1068 |
CodegenThingol.purge_module (filter (can (get_def modl)) idfs) modl |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1069 |
in |
19884 | 1070 |
map_module (purge idfs) thy |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1071 |
end; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1072 |
|
19884 | 1073 |
fun expand_module targets init gen arg thy = |
19597 | 1074 |
thy |
1075 |
|> CodegenTheorems.notify_dirty |
|
1076 |
|> `(#modl o CodegenData.get) |
|
1077 |
|> (fn (modl, thy) => |
|
19884 | 1078 |
(start_transact init (gen thy (mk_tabs thy targets) arg) modl, thy)) |
19597 | 1079 |
|-> (fn (x, modl) => map_module (K modl) #> pair x); |
18516 | 1080 |
|
1081 |
fun rename_inconsistent thy = |
|
18217 | 1082 |
let |
18516 | 1083 |
fun get_inconsistent thyname = |
1084 |
let |
|
1085 |
val thy = theory thyname; |
|
1086 |
fun own_tables get = |
|
19884 | 1087 |
get thy |
18516 | 1088 |
|> fold (Symtab.fold (Symtab.remove (K true)) o get) (Theory.parents_of thy) |
1089 |
|> Symtab.keys; |
|
1090 |
val names = own_tables (#2 o #types o Type.rep_tsig o Sign.tsig_of) |
|
18960 | 1091 |
@ own_tables (#2 o #constants o Consts.dest o #consts o Sign.rep_sg); |
18516 | 1092 |
fun diff names = |
1093 |
fold (fn name => |
|
1094 |
if is_prefix (op =) (NameSpace.unpack thyname) (NameSpace.unpack name) |
|
1095 |
then I |
|
1096 |
else cons (name, NameSpace.append thyname (NameSpace.base name))) names []; |
|
1097 |
in diff names end; |
|
19466 | 1098 |
val inconsistent = map get_inconsistent (ThyInfo.names ()) |> flat; |
18516 | 1099 |
fun add (src, dst) thy = |
1100 |
if (is_some oo Symtab.lookup o fst o #alias o #logic_data o CodegenData.get) thy src |
|
1101 |
then (warning ("code generator alias already defined for " ^ quote src ^ ", will not overwrite"); thy) |
|
1102 |
else add_alias (src, dst) thy |
|
1103 |
in fold add inconsistent thy end; |
|
1104 |
||
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1105 |
fun codegen_term t = |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1106 |
expand_module NONE NONE exprgen_term t; |
19136 | 1107 |
|
1108 |
val is_dtcon = has_nsp nsp_dtcon; |
|
1109 |
||
1110 |
fun consts_of_idfs thy = |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1111 |
map (the o const_of_idf thy (mk_tabs thy NONE)); |
19150 | 1112 |
|
1113 |
fun idfs_of_consts thy = |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1114 |
map (idf_of_const thy (mk_tabs thy NONE)); |
19150 | 1115 |
|
19967 | 1116 |
fun get_root_module thy = |
1117 |
thy |
|
1118 |
|> CodegenTheorems.notify_dirty |
|
1119 |
|> `(#modl o CodegenData.get); |
|
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1120 |
|
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1121 |
fun get_ml_fun_datatype thy resolv = |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1122 |
let |
19150 | 1123 |
val target_data = |
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1124 |
((fn data => (the o Symtab.lookup data) "ml") o #target_data o CodegenData.get) thy; |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1125 |
in |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1126 |
CodegenSerializer.ml_fun_datatype (nsp_dtcon, nsp_class, K false) |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1127 |
((Option.map fst oo Symtab.lookup o #syntax_tyco) target_data, |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1128 |
(Option.map fst oo Symtab.lookup o #syntax_const) target_data) |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1129 |
resolv |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1130 |
end; |
18516 | 1131 |
|
1132 |
||
1133 |
(** target languages **) |
|
1134 |
||
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1135 |
(* syntax *) |
18516 | 1136 |
|
18702 | 1137 |
fun read_typ thy = |
1138 |
Sign.read_typ (thy, K NONE); |
|
1139 |
||
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1140 |
fun read_const thy raw_t = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1141 |
let |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1142 |
val t = Sign.read_term thy raw_t |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1143 |
in case try dest_Const t |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1144 |
of SOME c => c |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1145 |
| NONE => error ("not a constant: " ^ Sign.string_of_term thy t) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1146 |
end; |
18517 | 1147 |
|
18963 | 1148 |
fun read_quote get reader gen raw thy = |
18912 | 1149 |
thy |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1150 |
|> expand_module NONE ((SOME o get) thy) |
19136 | 1151 |
(fn thy => fn tabs => gen thy tabs o single o reader thy) raw |
18912 | 1152 |
|-> (fn [x] => pair x); |
18702 | 1153 |
|
18865 | 1154 |
fun gen_add_syntax_class prep_class class target pretty thy = |
1155 |
thy |
|
1156 |
|> map_codegen_data |
|
1157 |
(fn (modl, gens, target_data, logic_data) => |
|
1158 |
(modl, gens, |
|
1159 |
target_data |> Symtab.map_entry target |
|
1160 |
(map_target_data |
|
1161 |
(fn (syntax_class, syntax_tyco, syntax_const) => |
|
1162 |
(syntax_class |
|
1163 |
|> Symtab.update (prep_class thy class, pretty), syntax_tyco, syntax_const))), |
|
1164 |
logic_data)); |
|
1165 |
||
1166 |
val add_syntax_class = gen_add_syntax_class Sign.intern_class; |
|
1167 |
||
18963 | 1168 |
fun parse_syntax_tyco raw_tyco = |
18217 | 1169 |
let |
18963 | 1170 |
fun check_tyco thy tyco = |
1171 |
if Sign.declared_tyname thy tyco |
|
1172 |
then tyco |
|
1173 |
else error ("no such type constructor: " ^ quote tyco); |
|
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1174 |
fun prep_tyco thy raw_tyco = |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1175 |
raw_tyco |
18963 | 1176 |
|> Sign.intern_type thy |
1177 |
|> check_tyco thy |
|
1178 |
|> idf_of_name thy nsp_tyco; |
|
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1179 |
fun no_args_tyco thy raw_tyco = |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1180 |
AList.lookup (op =) ((NameSpace.dest_table o #types o Type.rep_tsig o Sign.tsig_of) thy) |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1181 |
(Sign.intern_type thy raw_tyco) |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1182 |
|> (fn SOME ((Type.LogicalType i), _) => i); |
18963 | 1183 |
fun mk reader target thy = |
18702 | 1184 |
let |
18756 | 1185 |
val _ = get_serializer target; |
18702 | 1186 |
val tyco = prep_tyco thy raw_tyco; |
1187 |
in |
|
1188 |
thy |
|
1189 |
|> reader |
|
1190 |
|-> (fn pretty => map_codegen_data |
|
1191 |
(fn (modl, gens, target_data, logic_data) => |
|
1192 |
(modl, gens, |
|
1193 |
target_data |> Symtab.map_entry target |
|
1194 |
(map_target_data |
|
18865 | 1195 |
(fn (syntax_class, syntax_tyco, syntax_const) => |
1196 |
(syntax_class, syntax_tyco |> Symtab.update |
|
18702 | 1197 |
(tyco, (pretty, stamp ())), |
1198 |
syntax_const))), |
|
1199 |
logic_data))) |
|
1200 |
end; |
|
18217 | 1201 |
in |
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1202 |
CodegenSerializer.parse_syntax (fn thy => no_args_tyco thy raw_tyco) |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1203 |
(read_quote (fn thy => prep_tyco thy raw_tyco) read_typ |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1204 |
(fn thy => fn tabs => fold_map (exprgen_type thy tabs))) |
18702 | 1205 |
#-> (fn reader => pair (mk reader)) |
18217 | 1206 |
end; |
1207 |
||
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1208 |
fun add_pretty_syntax_const c target pretty = |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1209 |
map_codegen_data |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1210 |
(fn (modl, gens, target_data, logic_data) => |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1211 |
(modl, gens, |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1212 |
target_data |> Symtab.map_entry target |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1213 |
(map_target_data |
18865 | 1214 |
(fn (syntax_class, syntax_tyco, syntax_const) => |
1215 |
(syntax_class, syntax_tyco, |
|
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1216 |
syntax_const |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1217 |
|> Symtab.update |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1218 |
(c, (pretty, stamp ()))))), |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1219 |
logic_data)); |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1220 |
|
18963 | 1221 |
fun parse_syntax_const raw_const = |
18217 | 1222 |
let |
18963 | 1223 |
fun prep_const thy raw_const = |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1224 |
idf_of_const thy (mk_tabs thy NONE) (read_const thy raw_const); |
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1225 |
fun no_args_const thy raw_const = |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1226 |
(length o fst o strip_type o snd o read_const thy) raw_const; |
18963 | 1227 |
fun mk reader target thy = |
18702 | 1228 |
let |
18756 | 1229 |
val _ = get_serializer target; |
18963 | 1230 |
val c = prep_const thy raw_const; |
18702 | 1231 |
in |
1232 |
thy |
|
1233 |
|> reader |
|
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1234 |
|-> (fn pretty => add_pretty_syntax_const c target pretty) |
18702 | 1235 |
end; |
18217 | 1236 |
in |
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1237 |
CodegenSerializer.parse_syntax (fn thy => no_args_const thy raw_const) |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1238 |
(read_quote (fn thy => prep_const thy raw_const) Sign.read_term |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1239 |
(fn thy => fn tabs => fold_map (exprgen_term thy tabs))) |
18702 | 1240 |
#-> (fn reader => pair (mk reader)) |
18217 | 1241 |
end; |
1242 |
||
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1243 |
fun add_pretty_list raw_nil raw_cons (target, seri) thy = |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1244 |
let |
18756 | 1245 |
val _ = get_serializer target; |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1246 |
val tabs = mk_tabs thy NONE; |
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1247 |
fun mk_const raw_name = |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1248 |
let |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1249 |
val name = Sign.intern_const thy raw_name; |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1250 |
in idf_of_const thy tabs (name, Sign.the_const_type thy name) end; |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1251 |
val nil' = mk_const raw_nil; |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1252 |
val cons' = mk_const raw_cons; |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1253 |
val pr' = CodegenSerializer.pretty_list nil' cons' seri; |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1254 |
in |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1255 |
thy |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1256 |
|> add_pretty_syntax_const cons' target pr' |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1257 |
end; |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1258 |
|
18217 | 1259 |
|
18516 | 1260 |
|
19884 | 1261 |
(** code basis change notifications **) |
1262 |
||
1263 |
val _ = Context.add_setup (CodegenTheorems.add_notify purge_defs); |
|
1264 |
||
1265 |
||
1266 |
||
18756 | 1267 |
(** toplevel interface **) |
1268 |
||
1269 |
local |
|
19150 | 1270 |
|
19884 | 1271 |
fun generate_code targets (SOME raw_consts) thy = |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1272 |
let |
18756 | 1273 |
val consts = map (read_const thy) raw_consts; |
19884 | 1274 |
val _ = case targets of SOME targets => (map get_serializer targets; ()) | _ => (); |
18756 | 1275 |
in |
1276 |
thy |
|
19884 | 1277 |
|> expand_module targets NONE (fold_map oo ensure_def_const) consts |
18756 | 1278 |
|-> (fn cs => pair (SOME cs)) |
1279 |
end |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1280 |
| generate_code _ NONE thy = |
18756 | 1281 |
(NONE, thy); |
1282 |
||
1283 |
fun serialize_code target seri raw_consts thy = |
|
18217 | 1284 |
let |
18756 | 1285 |
fun serialize cs thy = |
18702 | 1286 |
let |
18756 | 1287 |
val module = (#modl o CodegenData.get) thy; |
18702 | 1288 |
val target_data = |
1289 |
thy |
|
1290 |
|> CodegenData.get |
|
1291 |
|> #target_data |
|
1292 |
|> (fn data => (the oo Symtab.lookup) data target); |
|
19884 | 1293 |
val s_class = #syntax_class target_data |
1294 |
val s_tyco = #syntax_tyco target_data |
|
1295 |
val s_const = #syntax_const target_data |
|
1296 |
in |
|
1297 |
(seri ( |
|
1298 |
Symtab.lookup s_class, |
|
1299 |
(Option.map fst oo Symtab.lookup) s_tyco, |
|
1300 |
(Option.map fst oo Symtab.lookup) s_const |
|
1301 |
) ([] (*TODO: add seri_defs here*), cs) module : unit; thy) |
|
1302 |
end; |
|
18217 | 1303 |
in |
1304 |
thy |
|
19884 | 1305 |
|> generate_code (SOME [target]) raw_consts |
18756 | 1306 |
|-> (fn cs => serialize cs) |
18217 | 1307 |
end; |
1308 |
||
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1309 |
fun purge_consts raw_ts thy = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1310 |
let |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1311 |
val cs = map (read_const thy) raw_ts; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1312 |
in fold CodegenTheorems.purge_defs cs thy end; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1313 |
|
18217 | 1314 |
structure P = OuterParse |
1315 |
and K = OuterKeyword |
|
1316 |
||
1317 |
in |
|
1318 |
||
18850 | 1319 |
val (generateK, serializeK, |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1320 |
syntax_classK, syntax_tycoK, syntax_constK, |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1321 |
purgeK, aliasK) = |
18850 | 1322 |
("code_generate", "code_serialize", |
19884 | 1323 |
"code_classapp", "code_typapp", "code_constapp", |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1324 |
"code_purge", "code_alias"); |
18335 | 1325 |
|
18217 | 1326 |
val generateP = |
18282 | 1327 |
OuterSyntax.command generateK "generate executable code for constants" K.thy_decl ( |
19884 | 1328 |
(Scan.option (P.$$$ "(" |-- P.list1 P.name --| P.$$$ ")") |
1329 |
>> (fn SOME ["-"] => SOME [] | ts => ts)) |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1330 |
-- Scan.repeat1 P.term |
19884 | 1331 |
>> (fn (targets, raw_consts) => |
1332 |
Toplevel.theory (generate_code targets (SOME raw_consts) #> snd)) |
|
18217 | 1333 |
); |
1334 |
||
1335 |
val serializeP = |
|
18282 | 1336 |
OuterSyntax.command serializeK "serialize executable code for constants" K.thy_decl ( |
18217 | 1337 |
P.name |
19136 | 1338 |
-- Scan.option (Scan.repeat1 P.term) |
18756 | 1339 |
#-> (fn (target, raw_consts) => |
18850 | 1340 |
P.$$$ "(" |
1341 |
|-- get_serializer target |
|
1342 |
--| P.$$$ ")" |
|
18756 | 1343 |
>> (fn seri => |
1344 |
Toplevel.theory (serialize_code target seri raw_consts) |
|
1345 |
)) |
|
18217 | 1346 |
); |
1347 |
||
18865 | 1348 |
val syntax_classP = |
19884 | 1349 |
OuterSyntax.command syntax_classK "define code syntax for class" K.thy_decl ( |
18865 | 1350 |
Scan.repeat1 ( |
1351 |
P.xname |
|
1352 |
-- Scan.repeat1 ( |
|
1353 |
P.name -- P.string |
|
1354 |
) |
|
1355 |
) |
|
1356 |
>> (Toplevel.theory oo fold) (fn (raw_class, syns) => |
|
1357 |
fold (fn (target, p) => add_syntax_class raw_class target p) syns) |
|
1358 |
); |
|
1359 |
||
18217 | 1360 |
val syntax_tycoP = |
1361 |
OuterSyntax.command syntax_tycoK "define code syntax for type constructor" K.thy_decl ( |
|
18702 | 1362 |
Scan.repeat1 ( |
1363 |
P.xname |
|
18963 | 1364 |
#-> (fn raw_tyco => Scan.repeat1 ( |
1365 |
P.name -- parse_syntax_tyco raw_tyco |
|
1366 |
)) |
|
18702 | 1367 |
) |
18963 | 1368 |
>> (Toplevel.theory oo fold o fold) |
1369 |
(fn (target, modifier) => modifier target) |
|
18217 | 1370 |
); |
1371 |
||
1372 |
val syntax_constP = |
|
1373 |
OuterSyntax.command syntax_constK "define code syntax for constant" K.thy_decl ( |
|
18702 | 1374 |
Scan.repeat1 ( |
19136 | 1375 |
P.term |
18963 | 1376 |
#-> (fn raw_const => Scan.repeat1 ( |
1377 |
P.name -- parse_syntax_const raw_const |
|
1378 |
)) |
|
18702 | 1379 |
) |
19008 | 1380 |
>> (Toplevel.theory oo fold o fold) |
18963 | 1381 |
(fn (target, modifier) => modifier target) |
18217 | 1382 |
); |
1383 |
||
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1384 |
val purgeP = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1385 |
OuterSyntax.command purgeK "purge all defintions for constant" K.thy_decl ( |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1386 |
Scan.repeat1 P.term |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1387 |
>> (Toplevel.theory o purge_consts) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1388 |
); |
18516 | 1389 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1390 |
val aliasP = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1391 |
OuterSyntax.command aliasK "declare an alias for a theory identifier" K.thy_decl ( |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1392 |
Scan.repeat1 (P.name -- P.name) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1393 |
>> (Toplevel.theory oo fold) add_alias |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1394 |
); |
18516 | 1395 |
|
19884 | 1396 |
val _ = OuterSyntax.add_parsers [generateP, serializeP, |
1397 |
syntax_classP, syntax_tycoP, syntax_constP, |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1398 |
purgeP, aliasP]; |
18217 | 1399 |
|
1400 |
end; (* local *) |
|
1401 |
||
1402 |
end; (* struct *) |