author | haftmann |
Tue, 25 Jul 2006 16:43:47 +0200 | |
changeset 20191 | b43fd26e1aaa |
parent 20183 | fd546b0c8a7c |
child 20192 | 956cd30ef3be |
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; |
20175 | 23 |
val set_get_all_datatype_cons: (theory -> (string * string) list) |
18454 | 24 |
-> theory -> theory; |
20175 | 25 |
val purge_code: 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 |
|
20183 | 247 |
nsp_dtcon |
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 |
|
20175 | 376 |
val purge_code = map_codegen_data (fn (_, appgens, target_data, logic_data) => |
377 |
(empty_module, appgens, target_data, logic_data)); |
|
19956 | 378 |
|
19038 | 379 |
(* advanced name handling *) |
18217 | 380 |
|
381 |
fun add_alias (src, dst) = |
|
382 |
map_codegen_data |
|
18702 | 383 |
(fn (modl, gens, target_data, logic_data) => |
384 |
(modl, gens, target_data, |
|
18217 | 385 |
logic_data |> map_logic_data |
386 |
(apsnd (fn (tab, tab_rev) => |
|
387 |
(tab |> Symtab.update (src, dst), |
|
388 |
tab_rev |> Symtab.update (dst, src)))))); |
|
389 |
||
19038 | 390 |
val _ = alias_ref := (perhaps o Symtab.lookup o fst o #alias o #logic_data o CodegenData.get, |
391 |
perhaps o Symtab.lookup o snd o #alias o #logic_data o CodegenData.get); |
|
18702 | 392 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
393 |
fun idf_of_co thy (tabs as (_, (_, _, dtcontab))) (co, dtco) = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
394 |
case CodegenTheorems.get_datatypes thy dtco |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
395 |
of SOME ((_, cos), _) => if AList.defined (op =) cos co |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
396 |
then try (DatatypeconsNameMangler.get thy dtcontab) (co, dtco) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
397 |
|> the_default co |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
398 |
|> idf_of_name thy nsp_dtcon |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
399 |
|> SOME |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
400 |
else NONE |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
401 |
| NONE => NONE; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
402 |
|
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
403 |
fun co_of_idf thy (tabs as (_, (_, _, dtcontab))) idf = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
404 |
case name_of_idf thy nsp_dtcon idf |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
405 |
of SOME idf' => let |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
406 |
val (c, dtco) = case try (DatatypeconsNameMangler.rev thy dtcontab) idf' |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
407 |
of SOME c_dtco => c_dtco |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
408 |
| 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
|
409 |
of Type (dtco, _) => (idf', dtco) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
410 |
| _ => (idf', "nat") (*a hack*) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
411 |
in SOME (c, dtco) end |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
412 |
| NONE => NONE; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
413 |
|
19884 | 414 |
fun idf_of_const thy (tabs as (_, (_, (overltab1, overltab2), _))) |
18865 | 415 |
(c, ty) = |
18454 | 416 |
let |
18515 | 417 |
fun get_overloaded (c, ty) = |
19202 | 418 |
(case Symtab.lookup overltab1 c |
19136 | 419 |
of SOME tys => |
18515 | 420 |
(case find_first (curry (Sign.typ_instance thy) ty) tys |
19136 | 421 |
of SOME ty' => ConstNameMangler.get thy overltab2 |
422 |
(c, ty') |> SOME |
|
18515 | 423 |
| _ => NONE) |
19202 | 424 |
| _ => NONE) |
18515 | 425 |
fun get_datatypecons (c, ty) = |
426 |
case (snd o strip_type) ty |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
427 |
of Type (tyco, _) => idf_of_co thy tabs (c, tyco) |
18515 | 428 |
| _ => NONE; |
18702 | 429 |
in case get_datatypecons (c, ty) |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
430 |
of SOME idf => idf |
18702 | 431 |
| NONE => case get_overloaded (c, ty) |
18515 | 432 |
of SOME idf => idf |
20105 | 433 |
| NONE => case AxClass.class_of_param thy c |
18515 | 434 |
of SOME _ => idf_of_name thy nsp_mem c |
435 |
| NONE => idf_of_name thy nsp_const c |
|
436 |
end; |
|
18217 | 437 |
|
19884 | 438 |
fun idf_of_const' thy (tabs as (_, (_, (overltab1, overltab2), _))) |
439 |
(c, ty) = |
|
440 |
let |
|
441 |
fun get_overloaded (c, ty) = |
|
442 |
(case Symtab.lookup overltab1 c |
|
443 |
of SOME tys => |
|
444 |
(case find_first (curry (Sign.typ_instance thy) ty) tys |
|
445 |
of SOME ty' => ConstNameMangler.get thy overltab2 |
|
446 |
(c, ty') |> SOME |
|
447 |
| _ => NONE) |
|
448 |
| _ => NONE) |
|
449 |
in case get_overloaded (c, ty) |
|
450 |
of SOME idf => idf |
|
20105 | 451 |
| NONE => case AxClass.class_of_param thy c |
19884 | 452 |
of SOME _ => idf_of_name thy nsp_mem c |
453 |
| NONE => idf_of_name thy nsp_const c |
|
454 |
end; |
|
455 |
||
19177 | 456 |
fun recconst_of_idf thy (_, (_, (_, overltab2), _)) idf = |
18454 | 457 |
case name_of_idf thy nsp_const idf |
18702 | 458 |
of SOME c => SOME (c, Sign.the_const_type thy c) |
18454 | 459 |
| NONE => ( |
460 |
case dest_nsp nsp_overl idf |
|
461 |
of SOME _ => |
|
462 |
idf |
|
19136 | 463 |
|> ConstNameMangler.rev thy overltab2 |
18454 | 464 |
|> SOME |
465 |
| NONE => NONE |
|
466 |
); |
|
467 |
||
18865 | 468 |
|
469 |
(* further theory data accessors *) |
|
470 |
||
20105 | 471 |
fun gen_add_appconst prep_const (raw_c, appgen) thy = |
18454 | 472 |
let |
18865 | 473 |
val c = prep_const thy raw_c; |
20105 | 474 |
val i = (length o fst o strip_type o Sign.the_const_type thy) c |
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 |
|
512 |
(* definition and expression generators *) |
|
513 |
||
19884 | 514 |
fun check_strict thy f x ((false, _, _), _) = |
515 |
false |
|
516 |
| check_strict thy f x ((_, SOME targets, _), _) = |
|
517 |
exists ( |
|
518 |
is_none o (fn tab => Symtab.lookup tab x) o f o the o (Symtab.lookup ((#target_data o CodegenData.get) thy)) |
|
519 |
) targets |
|
520 |
| check_strict thy f x ((true, _, _), _) = |
|
521 |
true; |
|
522 |
||
523 |
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
|
524 |
|
18865 | 525 |
fun ensure_def_class thy tabs cls trns = |
18454 | 526 |
let |
19956 | 527 |
fun defgen_class thy (tabs as (_, ((insttab, thynametab), _, _))) cls trns = |
18865 | 528 |
case name_of_idf thy nsp_class cls |
529 |
of SOME cls => |
|
530 |
let |
|
19283 | 531 |
val (v, cs) = (ClassPackage.the_consts_sign thy) cls; |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
532 |
val sortctxts = map (ClassPackage.sortcontext_of_typ thy o snd) cs; |
18865 | 533 |
val idfs = map (idf_of_name thy nsp_mem o fst) cs; |
534 |
in |
|
535 |
trns |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
536 |
|> debug_msg (fn _ => "trying defgen class declaration for " ^ quote cls) |
18865 | 537 |
|> 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
|
538 |
||>> (fold_map (exprgen_type thy tabs) o map snd) cs |
18865 | 539 |
||>> (fold_map o fold_map) (exprgen_tyvar_sort thy tabs) sortctxts |
540 |
|-> (fn ((supcls, memtypes), sortctxts) => succeed |
|
19283 | 541 |
(Class (supcls, (unprefix "'" v, idfs ~~ (sortctxts ~~ memtypes))))) |
18865 | 542 |
end |
543 |
| _ => |
|
544 |
trns |
|
545 |
|> fail ("no class definition found for " ^ quote cls); |
|
546 |
val cls' = idf_of_name thy nsp_class cls; |
|
18454 | 547 |
in |
548 |
trns |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
549 |
|> debug_msg (fn _ => "generating class " ^ quote cls) |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
550 |
|> ensure_def (defgen_class thy tabs) true ("generating class " ^ quote cls) cls' |
18865 | 551 |
|> pair cls' |
552 |
end |
|
553 |
and ensure_def_tyco thy tabs tyco trns = |
|
554 |
let |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
555 |
val tyco' = idf_of_name thy nsp_tyco tyco; |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
556 |
val strict = check_strict thy #syntax_tyco tyco' tabs; |
18963 | 557 |
fun defgen_datatype thy (tabs as (_, (_, _, dtcontab))) dtco trns = |
558 |
case name_of_idf thy nsp_tyco dtco |
|
559 |
of SOME dtco => |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
560 |
(case CodegenTheorems.get_datatypes thy dtco |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
561 |
of SOME ((vars, cos), _) => |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
562 |
trns |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
563 |
|> debug_msg (fn _ => "trying defgen datatype for " ^ quote dtco) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
564 |
|> fold_map (exprgen_tyvar_sort thy tabs) vars |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
565 |
||>> fold_map (fn (c, tys) => |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
566 |
fold_map (exprgen_type thy tabs) tys |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
567 |
#-> (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
|
568 |
|-> (fn (vars, cos) => succeed (Datatype |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
569 |
(vars, cos))) |
18963 | 570 |
| NONE => |
571 |
trns |
|
572 |
|> fail ("no datatype found for " ^ quote dtco)) |
|
573 |
| NONE => |
|
574 |
trns |
|
575 |
|> fail ("not a type constructor: " ^ quote dtco) |
|
18865 | 576 |
in |
577 |
trns |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
578 |
|> debug_msg (fn _ => "generating type constructor " ^ quote tyco) |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
579 |
|> ensure_def (defgen_datatype thy tabs) strict ("generating type constructor " ^ quote tyco) tyco' |
18865 | 580 |
|> pair tyco' |
581 |
end |
|
582 |
and exprgen_tyvar_sort thy tabs (v, sort) trns = |
|
18516 | 583 |
trns |
18885 | 584 |
|> fold_map (ensure_def_class thy tabs) (ClassPackage.operational_sort_of thy sort) |
18865 | 585 |
|-> (fn sort => pair (unprefix "'" v, sort)) |
586 |
and exprgen_type thy tabs (TVar _) trns = |
|
18516 | 587 |
error "TVar encountered during code generation" |
18702 | 588 |
| exprgen_type thy tabs (TFree v_s) trns = |
18516 | 589 |
trns |
18702 | 590 |
|> exprgen_tyvar_sort thy tabs v_s |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
591 |
|-> (fn (v, sort) => pair (ITyVar v)) |
18516 | 592 |
| exprgen_type thy tabs (Type ("fun", [t1, t2])) trns = |
593 |
trns |
|
594 |
|> exprgen_type thy tabs t1 |
|
595 |
||>> exprgen_type thy tabs t2 |
|
596 |
|-> (fn (t1', t2') => pair (t1' `-> t2')) |
|
597 |
| exprgen_type thy tabs (Type (tyco, tys)) trns = |
|
598 |
trns |
|
599 |
|> ensure_def_tyco thy tabs tyco |
|
600 |
||>> fold_map (exprgen_type thy tabs) tys |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
601 |
|-> (fn (tyco, tys) => pair (tyco `%% tys)); |
18516 | 602 |
|
18885 | 603 |
fun exprgen_classlookup thy tabs (ClassPackage.Instance (inst, ls)) trns = |
18517 | 604 |
trns |
18885 | 605 |
|> ensure_def_inst thy tabs inst |
606 |
||>> (fold_map o fold_map) (exprgen_classlookup thy tabs) ls |
|
607 |
|-> (fn (inst, ls) => pair (Instance (inst, ls))) |
|
19253 | 608 |
| exprgen_classlookup thy tabs (ClassPackage.Lookup (clss, (v, (i, j)))) trns = |
18516 | 609 |
trns |
18517 | 610 |
|> fold_map (ensure_def_class thy tabs) clss |
19253 | 611 |
|-> (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
|
612 |
and mk_fun thy tabs (c, ty) trns = |
19806 | 613 |
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
|
614 |
of eq_thms as eq_thm :: _ => |
18865 | 615 |
let |
19884 | 616 |
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
|
617 |
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
|
618 |
val sortcontext = ClassPackage.sortcontext_of_typ thy ty; |
18865 | 619 |
fun dest_eqthm eq_thm = |
620 |
let |
|
621 |
val ((t, args), rhs) = |
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
622 |
(apfst strip_comb o Logic.dest_equals o Logic.legacy_unvarify o prop_of) eq_thm; |
18865 | 623 |
in case t |
624 |
of Const (c', _) => if c' = c then (args, rhs) |
|
625 |
else error ("illegal function equation for " ^ quote c |
|
626 |
^ ", actually defining " ^ quote c') |
|
627 |
| _ => error ("illegal function equation for " ^ quote c) |
|
628 |
end; |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
629 |
fun exprgen_eq (args, rhs) trns = |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
630 |
trns |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
631 |
|> fold_map (exprgen_term thy tabs) args |
19884 | 632 |
||>> exprgen_term thy tabs rhs; |
20105 | 633 |
fun checkvars (args, rhs) = |
634 |
if CodegenThingol.vars_distinct args then (args, rhs) |
|
635 |
else error ("repeated variables on left hand side of function") |
|
18865 | 636 |
in |
18517 | 637 |
trns |
19884 | 638 |
|> message msg (fn trns => trns |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
639 |
|> fold_map (exprgen_eq o dest_eqthm) eq_thms |
20105 | 640 |
|-> (fn eqs => pair (map checkvars eqs)) |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
641 |
||>> fold_map (exprgen_tyvar_sort thy tabs) sortcontext |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
642 |
||>> exprgen_type thy tabs ty |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
643 |
|-> (fn ((eqs, sortctxt), ty) => (pair o SOME) ((eqs, (sortctxt, ty)), map snd sortcontext))) |
18865 | 644 |
end |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
645 |
| [] => (NONE, trns) |
19956 | 646 |
and ensure_def_inst thy (tabs as (_, ((insttab, thynametab), _, _))) (cls, tyco) trns = |
18865 | 647 |
let |
19956 | 648 |
fun defgen_inst thy (tabs as (_, ((insttab, thynametab), _, _))) inst trns = |
649 |
case Option.map (InstNameMangler.rev thy insttab o NameSpace.base) |
|
650 |
(name_of_idf thy nsp_inst inst) |
|
651 |
of SOME (class, tyco) => |
|
18865 | 652 |
let |
18885 | 653 |
val (arity, memdefs) = ClassPackage.the_inst_sign thy (class, tyco); |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
654 |
val arity_typ = Type (tyco, (map TFree arity)); |
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
655 |
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
|
656 |
of [] => NONE |
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
657 |
| sort => SOME (v, sort)) arity; |
18865 | 658 |
fun gen_suparity supclass trns = |
659 |
trns |
|
19136 | 660 |
|> ensure_def_class thy tabs supclass |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
661 |
||>> fold_map (exprgen_classlookup thy tabs) |
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
662 |
(ClassPackage.sortlookup thy ([supclass], arity_typ)); |
18865 | 663 |
fun gen_membr (m, ty) trns = |
664 |
trns |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
665 |
|> mk_fun thy tabs (m, ty) |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
666 |
|-> (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
|
667 |
^ quote m ^ " :: " ^ Sign.string_of_typ thy ty) |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
668 |
| SOME (funn, sorts) => fold_map (fn (sort, sort_ctxt) => |
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
669 |
fold_map (exprgen_classlookup thy tabs) |
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
670 |
(ClassPackage.sortlookup thy (sort, TFree sort_ctxt))) |
20191 | 671 |
(sorts ~~ operational_arity) |
19253 | 672 |
#-> (fn lss => |
673 |
pair (idf_of_name thy nsp_mem m, ((idf_of_name thy nsp_instmem m, funn), lss)))); |
|
18865 | 674 |
in |
675 |
trns |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
676 |
|> debug_msg (fn _ => "trying defgen class instance for (" ^ quote cls |
18865 | 677 |
^ ", " ^ quote tyco ^ ")") |
18885 | 678 |
|> ensure_def_class thy tabs class |
18865 | 679 |
||>> ensure_def_tyco thy tabs tyco |
680 |
||>> fold_map (exprgen_tyvar_sort thy tabs) arity |
|
18885 | 681 |
||>> fold_map gen_suparity (ClassPackage.the_superclasses thy class) |
18865 | 682 |
||>> fold_map gen_membr memdefs |
18885 | 683 |
|-> (fn ((((class, tyco), arity), suparities), memdefs) => |
684 |
succeed (Classinst (((class, (tyco, arity)), suparities), memdefs))) |
|
18865 | 685 |
end |
686 |
| _ => |
|
687 |
trns |> fail ("no class instance found for " ^ quote inst); |
|
19956 | 688 |
val thyname = (the o Symtab.lookup ((the o Symtab.lookup thynametab) cls)) tyco; |
689 |
val inst = (idf_of_name thy nsp_inst o NameSpace.append thyname o InstNameMangler.get thy insttab) |
|
690 |
(cls, tyco); |
|
18865 | 691 |
in |
692 |
trns |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
693 |
|> 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
|
694 |
|> ensure_def (defgen_inst thy tabs) true |
18865 | 695 |
("generating instance " ^ quote cls ^ " / " ^ quote tyco) inst |
696 |
|> pair inst |
|
697 |
end |
|
19213 | 698 |
and ensure_def_const thy (tabs as (_, (_, overltab, dtcontab))) (c, ty) trns = |
18865 | 699 |
let |
700 |
fun defgen_funs thy tabs c trns = |
|
701 |
case recconst_of_idf thy tabs c |
|
702 |
of SOME (c, ty) => |
|
703 |
trns |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
704 |
|> mk_fun thy tabs (c, ty) |
19253 | 705 |
|-> (fn SOME (funn, _) => succeed (Fun funn) |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
706 |
| NONE => fail ("no defining equations found for " ^ |
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
707 |
(quote o Display.raw_string_of_term o Const) (c, ty))) |
18865 | 708 |
| NONE => |
709 |
trns |
|
710 |
|> fail ("not a constant: " ^ quote c); |
|
18963 | 711 |
fun defgen_clsmem thy tabs m trns = |
712 |
case name_of_idf thy nsp_mem m |
|
713 |
of SOME m => |
|
714 |
trns |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
715 |
|> debug_msg (fn _ => "trying defgen class member for " ^ quote m) |
20105 | 716 |
|> 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
|
717 |
|-> (fn cls => succeed Bot) |
18963 | 718 |
| _ => |
719 |
trns |> fail ("no class member found for " ^ quote m) |
|
18865 | 720 |
fun defgen_datatypecons thy (tabs as (_, (_, _, dtcontab))) co trns = |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
721 |
case co_of_idf thy tabs co |
18865 | 722 |
of SOME (co, dtco) => |
723 |
trns |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
724 |
|> debug_msg (fn _ => "trying defgen datatype constructor for " ^ quote co) |
18865 | 725 |
|> ensure_def_tyco thy tabs dtco |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
726 |
|-> (fn dtco => succeed Bot) |
18865 | 727 |
| _ => |
728 |
trns |
|
729 |
|> fail ("not a datatype constructor: " ^ quote co); |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
730 |
fun get_defgen tabs idf strict = |
18963 | 731 |
if (is_some oo name_of_idf thy) nsp_const idf |
732 |
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
|
733 |
then defgen_funs thy tabs strict |
18963 | 734 |
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
|
735 |
then defgen_clsmem thy tabs strict |
18963 | 736 |
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
|
737 |
then defgen_datatypecons thy tabs strict |
18963 | 738 |
else error ("illegal shallow name space for constant: " ^ quote idf); |
739 |
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
|
740 |
val strict = check_strict thy #syntax_const idf tabs; |
18865 | 741 |
in |
742 |
trns |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
743 |
|> 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
|
744 |
|> ensure_def (get_defgen tabs idf) strict ("generating constant " ^ quote c) idf |
18963 | 745 |
|> pair idf |
18865 | 746 |
end |
18517 | 747 |
and exprgen_term thy tabs (Const (f, ty)) trns = |
748 |
trns |
|
749 |
|> appgen thy tabs ((f, ty), []) |
|
18516 | 750 |
|-> (fn e => pair e) |
18912 | 751 |
| exprgen_term thy tabs (Var _) trns = |
752 |
error "Var encountered during code generation" |
|
18516 | 753 |
| exprgen_term thy tabs (Free (v, ty)) trns = |
754 |
trns |
|
18912 | 755 |
|> exprgen_type thy tabs ty |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
756 |
|-> (fn ty => pair (IVar v)) |
19967 | 757 |
| exprgen_term thy tabs (Abs (raw_v, ty, raw_t)) trns = |
19136 | 758 |
let |
20175 | 759 |
val (v, t) = Term.variant_abs (Name.alphanum raw_v, ty, raw_t); |
19136 | 760 |
in |
761 |
trns |
|
762 |
|> exprgen_type thy tabs ty |
|
763 |
||>> exprgen_term thy tabs t |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
764 |
|-> (fn (ty, e) => pair ((v, ty) `|-> e)) |
19136 | 765 |
end |
18516 | 766 |
| exprgen_term thy tabs (t as t1 $ t2) trns = |
767 |
let |
|
768 |
val (t', ts) = strip_comb t |
|
769 |
in case t' |
|
770 |
of Const (f, ty) => |
|
771 |
trns |
|
18517 | 772 |
|> appgen thy tabs ((f, ty), ts) |
18516 | 773 |
|-> (fn e => pair e) |
774 |
| _ => |
|
775 |
trns |
|
776 |
|> exprgen_term thy tabs t' |
|
777 |
||>> fold_map (exprgen_term thy tabs) ts |
|
778 |
|-> (fn (e, es) => pair (e `$$ es)) |
|
18865 | 779 |
end |
780 |
and appgen_default thy tabs ((c, ty), ts) trns = |
|
781 |
trns |
|
782 |
|> ensure_def_const thy tabs (c, ty) |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
783 |
||>> exprgen_type thy tabs ty |
18885 | 784 |
||>> (fold_map o fold_map) (exprgen_classlookup thy tabs) |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
785 |
(ClassPackage.sortlookups_const thy (c, ty)) |
18912 | 786 |
||>> fold_map (exprgen_term thy tabs) ts |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
787 |
|-> (fn (((c, ty), ls), es) => |
19202 | 788 |
pair (IConst (c, (ls, ty)) `$$ es)) |
18865 | 789 |
and appgen thy tabs ((f, ty), ts) trns = |
20105 | 790 |
case Symtab.lookup ((#appgens o CodegenData.get) thy) f |
791 |
of SOME (i, (ag, _)) => |
|
792 |
if length ts < i then |
|
18865 | 793 |
let |
20105 | 794 |
val tys = Library.take (i - length ts, ((fst o strip_type) ty)); |
20175 | 795 |
val vs = Name.give_names (Name.declare f Name.context) "a" tys; |
18865 | 796 |
in |
797 |
trns |
|
18912 | 798 |
|> fold_map (exprgen_type thy tabs) tys |
20105 | 799 |
||>> ag thy tabs ((f, ty), ts @ map Free vs) |
800 |
|-> (fn (tys, e) => pair (map2 (fn (v, _) => pair v) vs tys `|--> e)) |
|
18865 | 801 |
end |
20105 | 802 |
else if length ts > i then |
18865 | 803 |
trns |
20105 | 804 |
|> ag thy tabs ((f, ty), Library.take (i, ts)) |
805 |
||>> fold_map (exprgen_term thy tabs) (Library.drop (i, ts)) |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
806 |
|-> (fn (e, es) => pair (e `$$ es)) |
18865 | 807 |
else |
808 |
trns |
|
809 |
|> ag thy tabs ((f, ty), ts) |
|
810 |
| NONE => |
|
811 |
trns |
|
812 |
|> appgen_default thy tabs ((f, ty), ts); |
|
18516 | 813 |
|
18702 | 814 |
|
18217 | 815 |
(* parametrized generators, for instantiation in HOL *) |
816 |
||
19884 | 817 |
fun appgen_number_of int_of_bin thy tabs (app as (c as (_, ty), [bin])) trns = |
818 |
case try (int_of_bin thy) bin |
|
819 |
of SOME i => if i < 0 then error ("negative numeral: " ^ IntInf.toString i) |
|
820 |
(*preprocessor eliminates nat and negative numerals*) |
|
821 |
else |
|
822 |
trns |
|
823 |
|> pair (CodegenThingol.INum (i, IVar "")) |
|
824 |
(*|> exprgen_term thy (no_strict tabs) (Const c) |
|
825 |
||>> exprgen_term thy (no_strict tabs) bin |
|
826 |
|-> (fn (e1, e2) => pair (CodegenThingol.INum (i, e1 `$ e2)))*) |
|
827 |
| NONE => |
|
828 |
trns |
|
829 |
|> appgen_default thy tabs app; |
|
18217 | 830 |
|
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
831 |
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
|
832 |
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
|
833 |
of SOME i => |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
834 |
trns |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
835 |
|> exprgen_type thy tabs ty |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
836 |
||>> appgen_default thy tabs app |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
837 |
|-> (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
|
838 |
| NONE => |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
839 |
trns |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
840 |
|> appgen_default thy tabs app; |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
841 |
|
20105 | 842 |
fun appgen_case dest_case_expr thy tabs (app as (c_ty, ts)) trns = |
843 |
let |
|
844 |
val SOME ([], ((st, sty), ds)) = dest_case_expr thy (list_comb (Const c_ty, ts)); |
|
845 |
fun clausegen (dt, bt) trns = |
|
846 |
trns |
|
847 |
|> exprgen_term thy tabs dt |
|
848 |
||>> exprgen_term thy tabs bt; |
|
849 |
in |
|
850 |
trns |
|
851 |
|> exprgen_term thy tabs st |
|
852 |
||>> exprgen_type thy tabs sty |
|
853 |
||>> fold_map clausegen ds |
|
854 |
||>> appgen_default thy tabs app |
|
855 |
|-> (fn (((se, sty), ds), e0) => pair (ICase (((se, sty), ds), e0))) |
|
856 |
end; |
|
857 |
||
858 |
fun appgen_let thy tabs (app as (_, [st, ct])) trns = |
|
859 |
trns |
|
860 |
|> exprgen_term thy tabs ct |
|
861 |
||>> exprgen_term thy tabs st |
|
862 |
||>> appgen_default thy tabs app |
|
863 |
|-> (fn (((v, ty) `|-> be, se), e0) => |
|
864 |
pair (ICase (((se, ty), case be |
|
865 |
of ICase (((IVar w, _), ds), _) => if v = w then ds else [(IVar v, be)] |
|
866 |
| _ => [(IVar v, be)] |
|
867 |
), e0)) |
|
868 |
| (_, e0) => pair e0); |
|
869 |
||
19038 | 870 |
fun appgen_wfrec thy tabs ((c, ty), [_, tf, tx]) trns = |
871 |
let |
|
19806 | 872 |
val ty_def = (op ---> o apfst tl o strip_type o Logic.legacy_unvarifyT o Sign.the_const_type thy) c; |
19038 | 873 |
val ty' = (op ---> o apfst tl o strip_type) ty; |
874 |
val idf = idf_of_const thy tabs (c, ty); |
|
875 |
in |
|
876 |
trns |
|
19884 | 877 |
|> ensure_def ((K o fail) "no extraction of wfrec") false ("generating wfrec") idf |
19136 | 878 |
|> exprgen_type thy tabs ty' |
879 |
||>> (fold_map o fold_map) (exprgen_classlookup thy tabs) |
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19884
diff
changeset
|
880 |
(ClassPackage.sortlookups_const thy (c, ty)) |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
881 |
||>> exprgen_type thy tabs ty_def |
19136 | 882 |
||>> exprgen_term thy tabs tf |
883 |
||>> exprgen_term thy tabs tx |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
884 |
|-> (fn ((((_, ls), ty), tf), tx) => pair (IConst (idf, (ls, ty)) `$ tf `$ tx)) |
19038 | 885 |
end; |
886 |
||
18217 | 887 |
|
18516 | 888 |
|
889 |
(** theory interface **) |
|
18217 | 890 |
|
19884 | 891 |
fun mk_tabs thy targets = |
18217 | 892 |
let |
19956 | 893 |
fun mk_insttab thy = |
894 |
let |
|
895 |
val insts = Symtab.fold |
|
896 |
(fn (tyco, classes) => cons (tyco, map fst classes)) |
|
897 |
((#arities o Sorts.rep_algebra o Sign.classes_of) thy) |
|
898 |
[] |
|
899 |
in ( |
|
900 |
InstNameMangler.empty |
|
901 |
|> fold |
|
902 |
(fn (tyco, classes) => fold |
|
903 |
(fn class => InstNameMangler.declare thy (class, tyco) #> snd) classes) |
|
904 |
insts, |
|
905 |
Symtab.empty |
|
906 |
|> fold |
|
907 |
(fn (tyco, classes) => fold |
|
908 |
(fn class => Symtab.default (class, Symtab.empty) |
|
909 |
#> Symtab.map_entry class (Symtab.update (tyco, thyname_of_instance thy (class, tyco)))) classes) |
|
910 |
insts |
|
911 |
) end; |
|
19136 | 912 |
fun mk_overltabs thy = |
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
913 |
(Symtab.empty, ConstNameMangler.empty) |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
914 |
|> Symtab.fold |
19136 | 915 |
(fn (c, _) => |
916 |
let |
|
19571 | 917 |
val deftab = Theory.definitions_of thy c |
20105 | 918 |
val is_overl = (is_none o AxClass.class_of_param thy) c |
19571 | 919 |
andalso case deftab (* is_overloaded (!?) *) |
19136 | 920 |
of [] => false |
19571 | 921 |
| [{lhs = ty, ...}] => not (Sign.typ_equiv thy (ty, Sign.the_const_type thy c)) |
19136 | 922 |
| _ => true; |
923 |
in if is_overl then (fn (overltab1, overltab2) => ( |
|
924 |
overltab1 |
|
19571 | 925 |
|> Symtab.update_new (c, map #lhs deftab), |
19136 | 926 |
overltab2 |
19571 | 927 |
|> fold_map (fn {lhs = ty, ...} => ConstNameMangler.declare thy (c, ty)) deftab |
19136 | 928 |
|-> (fn _ => I))) else I |
929 |
end) ((#2 o #constants o Consts.dest o #consts o Sign.rep_sg) thy) |
|
930 |
|> (fn (overltab1, overltab2) => |
|
931 |
let |
|
20076 | 932 |
val c = "op ="; (* FIXME depends on object-logic!? *) |
19136 | 933 |
val ty = Sign.the_const_type thy c; |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
934 |
fun inst tyco = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
935 |
let |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
936 |
val ty_inst = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
937 |
tyco |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
938 |
|> 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
|
939 |
|> (fn SOME (Type.LogicalType i, _) => i) |
20076 | 940 |
|> Name.invent_list [] "'a" |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
941 |
|> map (fn v => (TVar ((v, 0), Sign.defaultS thy))) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
942 |
|> (fn tys => Type (tyco, tys)) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
943 |
in map_atyps (fn _ => ty_inst) ty end; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
944 |
val tys = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
945 |
(Type.logical_types o Sign.tsig_of) thy |
19466 | 946 |
|> 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
|
947 |
|> map inst |
19136 | 948 |
in |
949 |
(overltab1 |
|
950 |
|> Symtab.update_new (c, tys), |
|
951 |
overltab2 |
|
952 |
|> fold (fn ty => ConstNameMangler.declare thy (c, ty) #> snd) tys) |
|
953 |
end); |
|
18454 | 954 |
fun mk_dtcontab thy = |
955 |
DatatypeconsNameMangler.empty |
|
956 |
|> fold_map |
|
18455 | 957 |
(fn (_, co_dtco) => DatatypeconsNameMangler.declare_multi thy co_dtco) |
958 |
(fold (fn (co, dtco) => |
|
959 |
let |
|
19008 | 960 |
val key = ((NameSpace.drop_base o NameSpace.drop_base) co, NameSpace.base co); |
18455 | 961 |
in AList.default (op =) (key, []) #> AList.map_entry (op =) key (cons (co, dtco)) end |
962 |
) (get_all_datatype_cons thy) []) |
|
18454 | 963 |
|-> (fn _ => I); |
964 |
val insttab = mk_insttab thy; |
|
19136 | 965 |
val overltabs = mk_overltabs thy; |
18454 | 966 |
val dtcontab = mk_dtcontab thy; |
19884 | 967 |
in ((true, targets, Symtab.empty), (insttab, overltabs, dtcontab)) end; |
18217 | 968 |
|
18756 | 969 |
fun get_serializer target = |
970 |
case Symtab.lookup (!serializers) target |
|
971 |
of SOME seri => seri |
|
19008 | 972 |
| NONE => Scan.fail_with (fn _ => "unknown code target language: " ^ quote target) (); |
18335 | 973 |
|
18516 | 974 |
fun map_module f = |
18702 | 975 |
map_codegen_data (fn (modl, gens, target_data, logic_data) => |
976 |
(f modl, gens, target_data, logic_data)); |
|
18516 | 977 |
|
19884 | 978 |
fun purge_defs NONE thy = |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
979 |
map_module (K CodegenThingol.empty_module) thy |
20191 | 980 |
| purge_defs (SOME []) thy = |
981 |
thy |
|
19884 | 982 |
| purge_defs (SOME cs) thy = |
20191 | 983 |
map_module (K CodegenThingol.empty_module) thy; |
984 |
(*let |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
985 |
val tabs = mk_tabs thy NONE; |
19884 | 986 |
val idfs = map (idf_of_const' thy tabs) cs; |
987 |
fun purge idfs modl = |
|
988 |
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
|
989 |
in |
19884 | 990 |
map_module (purge idfs) thy |
20191 | 991 |
end;*) |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
992 |
|
19884 | 993 |
fun expand_module targets init gen arg thy = |
19597 | 994 |
thy |
995 |
|> CodegenTheorems.notify_dirty |
|
996 |
|> `(#modl o CodegenData.get) |
|
997 |
|> (fn (modl, thy) => |
|
19884 | 998 |
(start_transact init (gen thy (mk_tabs thy targets) arg) modl, thy)) |
19597 | 999 |
|-> (fn (x, modl) => map_module (K modl) #> pair x); |
18516 | 1000 |
|
1001 |
fun rename_inconsistent thy = |
|
18217 | 1002 |
let |
18516 | 1003 |
fun get_inconsistent thyname = |
1004 |
let |
|
1005 |
val thy = theory thyname; |
|
1006 |
fun own_tables get = |
|
19884 | 1007 |
get thy |
18516 | 1008 |
|> fold (Symtab.fold (Symtab.remove (K true)) o get) (Theory.parents_of thy) |
1009 |
|> Symtab.keys; |
|
1010 |
val names = own_tables (#2 o #types o Type.rep_tsig o Sign.tsig_of) |
|
18960 | 1011 |
@ own_tables (#2 o #constants o Consts.dest o #consts o Sign.rep_sg); |
18516 | 1012 |
fun diff names = |
1013 |
fold (fn name => |
|
1014 |
if is_prefix (op =) (NameSpace.unpack thyname) (NameSpace.unpack name) |
|
1015 |
then I |
|
1016 |
else cons (name, NameSpace.append thyname (NameSpace.base name))) names []; |
|
1017 |
in diff names end; |
|
19466 | 1018 |
val inconsistent = map get_inconsistent (ThyInfo.names ()) |> flat; |
18516 | 1019 |
fun add (src, dst) thy = |
1020 |
if (is_some oo Symtab.lookup o fst o #alias o #logic_data o CodegenData.get) thy src |
|
1021 |
then (warning ("code generator alias already defined for " ^ quote src ^ ", will not overwrite"); thy) |
|
1022 |
else add_alias (src, dst) thy |
|
1023 |
in fold add inconsistent thy end; |
|
1024 |
||
20105 | 1025 |
fun codegen_term t thy = |
1026 |
thy |
|
20175 | 1027 |
|> expand_module (SOME [] (*(Symtab.keys (#target_data (CodegenData.get thy)))*)) NONE exprgen_term t; |
19136 | 1028 |
|
1029 |
val is_dtcon = has_nsp nsp_dtcon; |
|
1030 |
||
1031 |
fun consts_of_idfs thy = |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1032 |
map (the o const_of_idf thy (mk_tabs thy NONE)); |
19150 | 1033 |
|
1034 |
fun idfs_of_consts thy = |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1035 |
map (idf_of_const thy (mk_tabs thy NONE)); |
19150 | 1036 |
|
19967 | 1037 |
fun get_root_module thy = |
1038 |
thy |
|
1039 |
|> CodegenTheorems.notify_dirty |
|
1040 |
|> `(#modl o CodegenData.get); |
|
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1041 |
|
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1042 |
fun get_ml_fun_datatype thy resolv = |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1043 |
let |
19150 | 1044 |
val target_data = |
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1045 |
((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
|
1046 |
in |
20183 | 1047 |
CodegenSerializer.ml_fun_datatype nsp_dtcon |
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1048 |
((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
|
1049 |
(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
|
1050 |
resolv |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1051 |
end; |
18516 | 1052 |
|
1053 |
||
1054 |
(** target languages **) |
|
1055 |
||
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1056 |
(* syntax *) |
18516 | 1057 |
|
18702 | 1058 |
fun read_typ thy = |
1059 |
Sign.read_typ (thy, K NONE); |
|
1060 |
||
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1061 |
fun read_const thy raw_t = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1062 |
let |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1063 |
val t = Sign.read_term thy raw_t |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1064 |
in case try dest_Const t |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1065 |
of SOME c => c |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1066 |
| 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
|
1067 |
end; |
18517 | 1068 |
|
18963 | 1069 |
fun read_quote get reader gen raw thy = |
18912 | 1070 |
thy |
20191 | 1071 |
|> expand_module (SOME (Symtab.keys (#target_data (CodegenData.get thy)))) ((SOME o get) thy) |
19136 | 1072 |
(fn thy => fn tabs => gen thy tabs o single o reader thy) raw |
18912 | 1073 |
|-> (fn [x] => pair x); |
18702 | 1074 |
|
18865 | 1075 |
fun gen_add_syntax_class prep_class class target pretty thy = |
1076 |
thy |
|
1077 |
|> map_codegen_data |
|
1078 |
(fn (modl, gens, target_data, logic_data) => |
|
1079 |
(modl, gens, |
|
1080 |
target_data |> Symtab.map_entry target |
|
1081 |
(map_target_data |
|
1082 |
(fn (syntax_class, syntax_tyco, syntax_const) => |
|
1083 |
(syntax_class |
|
1084 |
|> Symtab.update (prep_class thy class, pretty), syntax_tyco, syntax_const))), |
|
1085 |
logic_data)); |
|
1086 |
||
1087 |
val add_syntax_class = gen_add_syntax_class Sign.intern_class; |
|
1088 |
||
18963 | 1089 |
fun parse_syntax_tyco raw_tyco = |
18217 | 1090 |
let |
18963 | 1091 |
fun check_tyco thy tyco = |
1092 |
if Sign.declared_tyname thy tyco |
|
1093 |
then tyco |
|
1094 |
else error ("no such type constructor: " ^ quote tyco); |
|
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1095 |
fun prep_tyco thy raw_tyco = |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1096 |
raw_tyco |
18963 | 1097 |
|> Sign.intern_type thy |
1098 |
|> check_tyco thy |
|
1099 |
|> idf_of_name thy nsp_tyco; |
|
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1100 |
fun no_args_tyco thy raw_tyco = |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1101 |
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
|
1102 |
(Sign.intern_type thy raw_tyco) |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1103 |
|> (fn SOME ((Type.LogicalType i), _) => i); |
18963 | 1104 |
fun mk reader target thy = |
18702 | 1105 |
let |
18756 | 1106 |
val _ = get_serializer target; |
18702 | 1107 |
val tyco = prep_tyco thy raw_tyco; |
1108 |
in |
|
1109 |
thy |
|
1110 |
|> reader |
|
1111 |
|-> (fn pretty => map_codegen_data |
|
1112 |
(fn (modl, gens, target_data, logic_data) => |
|
1113 |
(modl, gens, |
|
1114 |
target_data |> Symtab.map_entry target |
|
1115 |
(map_target_data |
|
18865 | 1116 |
(fn (syntax_class, syntax_tyco, syntax_const) => |
1117 |
(syntax_class, syntax_tyco |> Symtab.update |
|
18702 | 1118 |
(tyco, (pretty, stamp ())), |
1119 |
syntax_const))), |
|
1120 |
logic_data))) |
|
1121 |
end; |
|
18217 | 1122 |
in |
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1123 |
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
|
1124 |
(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
|
1125 |
(fn thy => fn tabs => fold_map (exprgen_type thy tabs))) |
18702 | 1126 |
#-> (fn reader => pair (mk reader)) |
18217 | 1127 |
end; |
1128 |
||
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1129 |
fun add_pretty_syntax_const c target pretty = |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1130 |
map_codegen_data |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1131 |
(fn (modl, gens, target_data, logic_data) => |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1132 |
(modl, gens, |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1133 |
target_data |> Symtab.map_entry target |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1134 |
(map_target_data |
18865 | 1135 |
(fn (syntax_class, syntax_tyco, syntax_const) => |
1136 |
(syntax_class, syntax_tyco, |
|
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1137 |
syntax_const |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1138 |
|> Symtab.update |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1139 |
(c, (pretty, stamp ()))))), |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1140 |
logic_data)); |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1141 |
|
18963 | 1142 |
fun parse_syntax_const raw_const = |
18217 | 1143 |
let |
18963 | 1144 |
fun prep_const thy raw_const = |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1145 |
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
|
1146 |
fun no_args_const thy raw_const = |
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1147 |
(length o fst o strip_type o snd o read_const thy) raw_const; |
18963 | 1148 |
fun mk reader target thy = |
18702 | 1149 |
let |
18756 | 1150 |
val _ = get_serializer target; |
18963 | 1151 |
val c = prep_const thy raw_const; |
18702 | 1152 |
in |
1153 |
thy |
|
1154 |
|> reader |
|
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1155 |
|-> (fn pretty => add_pretty_syntax_const c target pretty) |
18702 | 1156 |
end; |
18217 | 1157 |
in |
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1158 |
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
|
1159 |
(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
|
1160 |
(fn thy => fn tabs => fold_map (exprgen_term thy tabs))) |
18702 | 1161 |
#-> (fn reader => pair (mk reader)) |
18217 | 1162 |
end; |
1163 |
||
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1164 |
fun add_pretty_list raw_nil raw_cons (target, seri) thy = |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1165 |
let |
18756 | 1166 |
val _ = get_serializer target; |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1167 |
val tabs = mk_tabs thy NONE; |
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1168 |
fun mk_const raw_name = |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1169 |
let |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1170 |
val name = Sign.intern_const thy raw_name; |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1171 |
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
|
1172 |
val nil' = mk_const raw_nil; |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1173 |
val cons' = mk_const raw_cons; |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1174 |
val pr' = CodegenSerializer.pretty_list nil' cons' seri; |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1175 |
in |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1176 |
thy |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1177 |
|> add_pretty_syntax_const cons' target pr' |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1178 |
end; |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1179 |
|
18217 | 1180 |
|
18516 | 1181 |
|
19884 | 1182 |
(** code basis change notifications **) |
1183 |
||
1184 |
val _ = Context.add_setup (CodegenTheorems.add_notify purge_defs); |
|
1185 |
||
1186 |
||
1187 |
||
18756 | 1188 |
(** toplevel interface **) |
1189 |
||
1190 |
local |
|
19150 | 1191 |
|
19884 | 1192 |
fun generate_code targets (SOME raw_consts) thy = |
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1193 |
let |
18756 | 1194 |
val consts = map (read_const thy) raw_consts; |
19884 | 1195 |
val _ = case targets of SOME targets => (map get_serializer targets; ()) | _ => (); |
18756 | 1196 |
in |
1197 |
thy |
|
19884 | 1198 |
|> expand_module targets NONE (fold_map oo ensure_def_const) consts |
18756 | 1199 |
|-> (fn cs => pair (SOME cs)) |
1200 |
end |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1201 |
| generate_code _ NONE thy = |
18756 | 1202 |
(NONE, thy); |
1203 |
||
1204 |
fun serialize_code target seri raw_consts thy = |
|
18217 | 1205 |
let |
18756 | 1206 |
fun serialize cs thy = |
18702 | 1207 |
let |
18756 | 1208 |
val module = (#modl o CodegenData.get) thy; |
18702 | 1209 |
val target_data = |
1210 |
thy |
|
1211 |
|> CodegenData.get |
|
1212 |
|> #target_data |
|
1213 |
|> (fn data => (the oo Symtab.lookup) data target); |
|
19884 | 1214 |
val s_class = #syntax_class target_data |
1215 |
val s_tyco = #syntax_tyco target_data |
|
1216 |
val s_const = #syntax_const target_data |
|
1217 |
in |
|
1218 |
(seri ( |
|
1219 |
Symtab.lookup s_class, |
|
1220 |
(Option.map fst oo Symtab.lookup) s_tyco, |
|
1221 |
(Option.map fst oo Symtab.lookup) s_const |
|
20191 | 1222 |
) (Symtab.keys s_class @ Symtab.keys s_tyco @ Symtab.keys s_const, cs) module : unit; thy) |
19884 | 1223 |
end; |
18217 | 1224 |
in |
1225 |
thy |
|
19884 | 1226 |
|> generate_code (SOME [target]) raw_consts |
18756 | 1227 |
|-> (fn cs => serialize cs) |
18217 | 1228 |
end; |
1229 |
||
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1230 |
fun purge_consts raw_ts thy = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1231 |
let |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1232 |
val cs = map (read_const thy) raw_ts; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1233 |
in fold CodegenTheorems.purge_defs cs thy end; |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1234 |
|
18217 | 1235 |
structure P = OuterParse |
1236 |
and K = OuterKeyword |
|
1237 |
||
1238 |
in |
|
1239 |
||
18850 | 1240 |
val (generateK, serializeK, |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1241 |
syntax_classK, syntax_tycoK, syntax_constK, |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1242 |
purgeK, aliasK) = |
18850 | 1243 |
("code_generate", "code_serialize", |
19884 | 1244 |
"code_classapp", "code_typapp", "code_constapp", |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1245 |
"code_purge", "code_alias"); |
18335 | 1246 |
|
18217 | 1247 |
val generateP = |
18282 | 1248 |
OuterSyntax.command generateK "generate executable code for constants" K.thy_decl ( |
19884 | 1249 |
(Scan.option (P.$$$ "(" |-- P.list1 P.name --| P.$$$ ")") |
1250 |
>> (fn SOME ["-"] => SOME [] | ts => ts)) |
|
19816
a8c8ed1c85e0
removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents:
19806
diff
changeset
|
1251 |
-- Scan.repeat1 P.term |
19884 | 1252 |
>> (fn (targets, raw_consts) => |
1253 |
Toplevel.theory (generate_code targets (SOME raw_consts) #> snd)) |
|
18217 | 1254 |
); |
1255 |
||
1256 |
val serializeP = |
|
18282 | 1257 |
OuterSyntax.command serializeK "serialize executable code for constants" K.thy_decl ( |
18217 | 1258 |
P.name |
19136 | 1259 |
-- Scan.option (Scan.repeat1 P.term) |
18756 | 1260 |
#-> (fn (target, raw_consts) => |
18850 | 1261 |
P.$$$ "(" |
1262 |
|-- get_serializer target |
|
1263 |
--| P.$$$ ")" |
|
18756 | 1264 |
>> (fn seri => |
1265 |
Toplevel.theory (serialize_code target seri raw_consts) |
|
1266 |
)) |
|
18217 | 1267 |
); |
1268 |
||
18865 | 1269 |
val syntax_classP = |
19884 | 1270 |
OuterSyntax.command syntax_classK "define code syntax for class" K.thy_decl ( |
18865 | 1271 |
Scan.repeat1 ( |
1272 |
P.xname |
|
1273 |
-- Scan.repeat1 ( |
|
1274 |
P.name -- P.string |
|
1275 |
) |
|
1276 |
) |
|
1277 |
>> (Toplevel.theory oo fold) (fn (raw_class, syns) => |
|
1278 |
fold (fn (target, p) => add_syntax_class raw_class target p) syns) |
|
1279 |
); |
|
1280 |
||
18217 | 1281 |
val syntax_tycoP = |
1282 |
OuterSyntax.command syntax_tycoK "define code syntax for type constructor" K.thy_decl ( |
|
18702 | 1283 |
Scan.repeat1 ( |
1284 |
P.xname |
|
18963 | 1285 |
#-> (fn raw_tyco => Scan.repeat1 ( |
1286 |
P.name -- parse_syntax_tyco raw_tyco |
|
1287 |
)) |
|
18702 | 1288 |
) |
18963 | 1289 |
>> (Toplevel.theory oo fold o fold) |
1290 |
(fn (target, modifier) => modifier target) |
|
18217 | 1291 |
); |
1292 |
||
1293 |
val syntax_constP = |
|
1294 |
OuterSyntax.command syntax_constK "define code syntax for constant" K.thy_decl ( |
|
18702 | 1295 |
Scan.repeat1 ( |
19136 | 1296 |
P.term |
18963 | 1297 |
#-> (fn raw_const => Scan.repeat1 ( |
1298 |
P.name -- parse_syntax_const raw_const |
|
1299 |
)) |
|
18702 | 1300 |
) |
19008 | 1301 |
>> (Toplevel.theory oo fold o fold) |
18963 | 1302 |
(fn (target, modifier) => modifier target) |
18217 | 1303 |
); |
1304 |
||
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1305 |
val purgeP = |
20175 | 1306 |
OuterSyntax.command purgeK "purge all incrementally generated code" K.thy_decl |
1307 |
(Scan.succeed (Toplevel.theory purge_code)); |
|
18516 | 1308 |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1309 |
val aliasP = |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1310 |
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
|
1311 |
Scan.repeat1 (P.name -- P.name) |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1312 |
>> (Toplevel.theory oo fold) add_alias |
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1313 |
); |
18516 | 1314 |
|
19884 | 1315 |
val _ = OuterSyntax.add_parsers [generateP, serializeP, |
1316 |
syntax_classP, syntax_tycoP, syntax_constP, |
|
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19283
diff
changeset
|
1317 |
purgeP, aliasP]; |
18217 | 1318 |
|
1319 |
end; (* local *) |
|
1320 |
||
1321 |
end; (* struct *) |