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