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