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