author | haftmann |
Mon, 01 Sep 2008 10:18:37 +0200 | |
changeset 28064 | d4a6460c53d1 |
parent 28054 | 2b84d34c5d02 |
child 28090 | 29af3c712d2b |
permissions | -rw-r--r-- |
24219 | 1 |
(* Title: Tools/code/code_target.ML |
2 |
ID: $Id$ |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
||
28054 | 5 |
Serializer from intermediate language ("Thin-gol") to target languages. |
24219 | 6 |
*) |
7 |
||
8 |
signature CODE_TARGET = |
|
9 |
sig |
|
28054 | 10 |
include CODE_PRINTER |
11 |
||
12 |
type serializer |
|
28064 | 13 |
val add_target: string * (serializer * literals) -> theory -> theory |
28054 | 14 |
val extend_target: string * (string * (Code_Thingol.program -> Code_Thingol.program)) |
15 |
-> theory -> theory |
|
16 |
val assert_target: theory -> string -> string |
|
24219 | 17 |
|
28054 | 18 |
type destination |
19 |
type serialization |
|
20 |
val parse_args: (OuterLex.token list -> 'a * OuterLex.token list) |
|
21 |
-> OuterLex.token list -> 'a |
|
22 |
val stmt_names_of_destination: destination -> string list |
|
23 |
val code_of_pretty: Pretty.T -> string |
|
24 |
val code_writeln: Pretty.T -> unit |
|
25 |
val mk_serialization: string -> ('a -> unit) option |
|
26 |
-> (Path.T option -> 'a -> unit) |
|
27 |
-> ('a -> string * string list) |
|
28 |
-> 'a -> serialization |
|
29 |
val serialize: theory -> string -> string option -> Args.T list |
|
30 |
-> Code_Thingol.program -> string list -> serialization |
|
28064 | 31 |
val serialize_custom: theory -> string * (serializer * literals) |
28054 | 32 |
-> Code_Thingol.program -> string list -> string * string list |
33 |
val compile: serialization -> unit |
|
34 |
val export: serialization -> unit |
|
35 |
val file: Path.T -> serialization -> unit |
|
36 |
val string: string list -> serialization -> string |
|
37 |
||
38 |
val code_of: theory -> string -> string -> string list -> string list -> string |
|
39 |
val shell_command: string (*theory name*) -> string (*export_code expr*) -> unit |
|
40 |
val code_width: int ref |
|
41 |
||
42 |
val allow_abort: string -> theory -> theory |
|
24219 | 43 |
val add_syntax_class: string -> class |
28054 | 44 |
-> (string * (string * string) list) option -> theory -> theory |
45 |
val add_syntax_inst: string -> string * class -> bool -> theory -> theory |
|
46 |
val add_syntax_tyco: string -> string -> tyco_syntax option -> theory -> theory |
|
24219 | 47 |
val add_syntax_tycoP: string -> string -> OuterParse.token list |
28054 | 48 |
-> (theory -> theory) * OuterParse.token list |
49 |
val add_syntax_const: string -> string -> const_syntax option -> theory -> theory |
|
24219 | 50 |
val add_syntax_constP: string -> string -> OuterParse.token list |
28054 | 51 |
-> (theory -> theory) * OuterParse.token list |
52 |
val add_reserved: string -> string -> theory -> theory |
|
24219 | 53 |
|
28064 | 54 |
val add_literal_list: string -> string -> string -> theory -> theory |
55 |
val add_literal_list_string: string -> string -> string |
|
28054 | 56 |
-> string -> string list -> theory -> theory |
28064 | 57 |
val add_literal_char: string -> string -> string list -> theory -> theory |
58 |
val add_literal_numeral: string -> bool -> bool -> string -> string -> string |
|
28054 | 59 |
-> string -> string -> theory -> theory |
28064 | 60 |
val add_literal_message: string -> string -> string list -> string |
28054 | 61 |
-> string -> string -> theory -> theory |
24219 | 62 |
end; |
63 |
||
28054 | 64 |
structure Code_Target : CODE_TARGET = |
24219 | 65 |
struct |
66 |
||
28054 | 67 |
open Basic_Code_Thingol; |
68 |
open Code_Printer; |
|
24219 | 69 |
|
70 |
(** basics **) |
|
71 |
||
27304 | 72 |
datatype destination = Compile | Export | File of Path.T | String of string list; |
27436 | 73 |
type serialization = destination -> (string * string list) option; |
27014
a5f53d9d2b60
yet another attempt to circumvent printmode problems
haftmann
parents:
27001
diff
changeset
|
74 |
|
27103 | 75 |
val code_width = ref 80; (*FIXME after Pretty module no longer depends on print mode*) |
76 |
fun code_setmp f = PrintMode.setmp [] (Pretty.setmp_margin (!code_width) f); |
|
77 |
fun code_of_pretty p = code_setmp Pretty.string_of p ^ "\n"; |
|
78 |
fun code_writeln p = Pretty.setmp_margin (!code_width) Pretty.writeln p; |
|
26998 | 79 |
|
27103 | 80 |
(*FIXME why another code_setmp?*) |
81 |
fun compile f = (code_setmp f Compile; ()); |
|
27304 | 82 |
fun export f = (code_setmp f Export; ()); |
27103 | 83 |
fun file p f = (code_setmp f (File p); ()); |
27436 | 84 |
fun string cs f = fst (the (code_setmp f (String cs))); |
27304 | 85 |
|
86 |
fun stmt_names_of_destination (String stmts) = stmts |
|
87 |
| stmt_names_of_destination _ = []; |
|
27014
a5f53d9d2b60
yet another attempt to circumvent printmode problems
haftmann
parents:
27001
diff
changeset
|
88 |
|
28054 | 89 |
fun mk_serialization target (SOME comp) _ _ code Compile = (comp code; NONE) |
90 |
| mk_serialization target NONE _ _ _ Compile = error (target ^ ": no internal compilation") |
|
91 |
| mk_serialization target _ output _ code Export = (output NONE code ; NONE) |
|
92 |
| mk_serialization target _ output _ code (File file) = (output (SOME file) code; NONE) |
|
93 |
| mk_serialization target _ _ string code (String _) = SOME (string code); |
|
27550 | 94 |
|
24219 | 95 |
|
28054 | 96 |
(** pretty syntax **) |
24219 | 97 |
|
98 |
(* list, char, string, numeral and monad abstract syntax transformations *) |
|
99 |
||
100 |
fun implode_list c_nil c_cons t = |
|
101 |
let |
|
102 |
fun dest_cons (IConst (c, _) `$ t1 `$ t2) = |
|
103 |
if c = c_cons |
|
104 |
then SOME (t1, t2) |
|
105 |
else NONE |
|
106 |
| dest_cons _ = NONE; |
|
28054 | 107 |
val (ts, t') = Code_Thingol.unfoldr dest_cons t; |
24219 | 108 |
in case t' |
109 |
of IConst (c, _) => if c = c_nil then SOME ts else NONE |
|
110 |
| _ => NONE |
|
111 |
end; |
|
112 |
||
113 |
fun decode_char c_nibbles (IConst (c1, _), IConst (c2, _)) = |
|
114 |
let |
|
115 |
fun idx c = find_index (curry (op =) c) c_nibbles; |
|
116 |
fun decode ~1 _ = NONE |
|
117 |
| decode _ ~1 = NONE |
|
118 |
| decode n m = SOME (chr (n * 16 + m)); |
|
119 |
in decode (idx c1) (idx c2) end |
|
120 |
| decode_char _ _ = NONE; |
|
121 |
||
122 |
fun implode_string c_char c_nibbles mk_char mk_string ts = |
|
123 |
let |
|
124 |
fun implode_char (IConst (c, _) `$ t1 `$ t2) = |
|
125 |
if c = c_char then decode_char c_nibbles (t1, t2) else NONE |
|
126 |
| implode_char _ = NONE; |
|
127 |
val ts' = map implode_char ts; |
|
128 |
in if forall is_some ts' |
|
129 |
then (SOME o str o mk_string o implode o map_filter I) ts' |
|
130 |
else NONE |
|
131 |
end; |
|
132 |
||
27304 | 133 |
fun implode_numeral thm negative c_pls c_min c_bit0 c_bit1 = |
24219 | 134 |
let |
25936 | 135 |
fun dest_bit (IConst (c, _)) = if c = c_bit0 then 0 |
136 |
else if c = c_bit1 then 1 |
|
27304 | 137 |
else nerror thm "Illegal numeral expression: illegal bit" |
138 |
| dest_bit _ = nerror thm "Illegal numeral expression: illegal bit"; |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24621
diff
changeset
|
139 |
fun dest_numeral (IConst (c, _)) = if c = c_pls then SOME 0 |
25936 | 140 |
else if c = c_min then |
141 |
if negative then SOME ~1 else NONE |
|
27304 | 142 |
else nerror thm "Illegal numeral expression: illegal leading digit" |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26010
diff
changeset
|
143 |
| dest_numeral (t1 `$ t2) = |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26010
diff
changeset
|
144 |
let val (n, b) = (dest_numeral t2, dest_bit t1) |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26010
diff
changeset
|
145 |
in case n of SOME n => SOME (2 * n + b) | NONE => NONE end |
27304 | 146 |
| dest_numeral _ = nerror thm "Illegal numeral expression: illegal term"; |
25936 | 147 |
in dest_numeral #> the_default 0 end; |
24219 | 148 |
|
149 |
||
28064 | 150 |
(* literal syntax printing *) |
24219 | 151 |
|
152 |
fun default_list (target_fxy, target_cons) pr fxy t1 t2 = |
|
153 |
brackify_infix (target_fxy, R) fxy [ |
|
154 |
pr (INFX (target_fxy, X)) t1, |
|
155 |
str target_cons, |
|
156 |
pr (INFX (target_fxy, R)) t2 |
|
157 |
]; |
|
158 |
||
28064 | 159 |
fun pretty_list c_nil c_cons literals = |
24219 | 160 |
let |
28064 | 161 |
val mk_list = literal_list literals; |
27304 | 162 |
fun pretty pr thm pat vars fxy [(t1, _), (t2, _)] = |
24219 | 163 |
case Option.map (cons t1) (implode_list c_nil c_cons t2) |
164 |
of SOME ts => mk_list (map (pr vars NOBR) ts) |
|
28064 | 165 |
| NONE => default_list (infix_cons literals) (pr vars) fxy t1 t2; |
24219 | 166 |
in (2, pretty) end; |
167 |
||
28064 | 168 |
fun pretty_list_string c_nil c_cons c_char c_nibbles literals = |
24219 | 169 |
let |
28064 | 170 |
val mk_list = literal_list literals; |
171 |
val mk_char = literal_char literals; |
|
172 |
val mk_string = literal_string literals; |
|
27304 | 173 |
fun pretty pr thm pat vars fxy [(t1, _), (t2, _)] = |
24219 | 174 |
case Option.map (cons t1) (implode_list c_nil c_cons t2) |
26448 | 175 |
of SOME ts => (case implode_string c_char c_nibbles mk_char mk_string ts |
24219 | 176 |
of SOME p => p |
26448 | 177 |
| NONE => mk_list (map (pr vars NOBR) ts)) |
28064 | 178 |
| NONE => default_list (infix_cons literals) (pr vars) fxy t1 t2; |
24219 | 179 |
in (2, pretty) end; |
180 |
||
28064 | 181 |
fun pretty_char c_char c_nibbles literals = |
24219 | 182 |
let |
28064 | 183 |
val mk_char = literal_char literals; |
27304 | 184 |
fun pretty _ thm _ _ _ [(t1, _), (t2, _)] = |
24219 | 185 |
case decode_char c_nibbles (t1, t2) |
186 |
of SOME c => (str o mk_char) c |
|
27304 | 187 |
| NONE => nerror thm "Illegal character expression"; |
24219 | 188 |
in (2, pretty) end; |
189 |
||
28064 | 190 |
fun pretty_numeral unbounded negative c_pls c_min c_bit0 c_bit1 literals = |
24219 | 191 |
let |
28064 | 192 |
val mk_numeral = literal_numeral literals; |
27304 | 193 |
fun pretty _ thm _ _ _ [(t, _)] = |
194 |
(str o mk_numeral unbounded o implode_numeral thm negative c_pls c_min c_bit0 c_bit1) t; |
|
24219 | 195 |
in (1, pretty) end; |
196 |
||
28064 | 197 |
fun pretty_message c_char c_nibbles c_nil c_cons literals = |
24219 | 198 |
let |
28064 | 199 |
val mk_char = literal_char literals; |
200 |
val mk_string = literal_string literals; |
|
27304 | 201 |
fun pretty _ thm _ _ _ [(t, _)] = |
24219 | 202 |
case implode_list c_nil c_cons t |
203 |
of SOME ts => (case implode_string c_char c_nibbles mk_char mk_string ts |
|
204 |
of SOME p => p |
|
27304 | 205 |
| NONE => nerror thm "Illegal message expression") |
206 |
| NONE => nerror thm "Illegal message expression"; |
|
24219 | 207 |
in (1, pretty) end; |
208 |
||
27000 | 209 |
|
28054 | 210 |
(** theory data **) |
211 |
||
212 |
datatype name_syntax_table = NameSyntaxTable of { |
|
213 |
class: class_syntax Symtab.table, |
|
214 |
inst: unit Symtab.table, |
|
215 |
tyco: tyco_syntax Symtab.table, |
|
216 |
const: const_syntax Symtab.table |
|
217 |
}; |
|
27000 | 218 |
|
28054 | 219 |
fun mk_name_syntax_table ((class, inst), (tyco, const)) = |
220 |
NameSyntaxTable { class = class, inst = inst, tyco = tyco, const = const }; |
|
221 |
fun map_name_syntax_table f (NameSyntaxTable { class, inst, tyco, const }) = |
|
222 |
mk_name_syntax_table (f ((class, inst), (tyco, const))); |
|
223 |
fun merge_name_syntax_table (NameSyntaxTable { class = class1, inst = inst1, tyco = tyco1, const = const1 }, |
|
224 |
NameSyntaxTable { class = class2, inst = inst2, tyco = tyco2, const = const2 }) = |
|
225 |
mk_name_syntax_table ( |
|
226 |
(Symtab.join (K snd) (class1, class2), |
|
227 |
Symtab.join (K snd) (inst1, inst2)), |
|
228 |
(Symtab.join (K snd) (tyco1, tyco2), |
|
229 |
Symtab.join (K snd) (const1, const2)) |
|
230 |
); |
|
231 |
||
232 |
type serializer = |
|
233 |
string option (*module name*) |
|
234 |
-> Args.T list (*arguments*) |
|
235 |
-> (string -> string) (*labelled_name*) |
|
236 |
-> string list (*reserved symbols*) |
|
237 |
-> (string * Pretty.T) list (*includes*) |
|
238 |
-> (string -> string option) (*module aliasses*) |
|
239 |
-> (string -> class_syntax option) |
|
240 |
-> (string -> tyco_syntax option) |
|
241 |
-> (string -> const_syntax option) |
|
242 |
-> Code_Thingol.program |
|
243 |
-> string list (*selected statements*) |
|
244 |
-> serialization; |
|
27000 | 245 |
|
28064 | 246 |
datatype serializer_entry = Serializer of serializer * literals |
28054 | 247 |
| Extends of string * (Code_Thingol.program -> Code_Thingol.program); |
248 |
||
249 |
datatype target = Target of { |
|
250 |
serial: serial, |
|
251 |
serializer: serializer_entry, |
|
252 |
reserved: string list, |
|
253 |
includes: Pretty.T Symtab.table, |
|
254 |
name_syntax_table: name_syntax_table, |
|
255 |
module_alias: string Symtab.table |
|
256 |
}; |
|
27103 | 257 |
|
28054 | 258 |
fun mk_target ((serial, serializer), ((reserved, includes), (name_syntax_table, module_alias))) = |
259 |
Target { serial = serial, serializer = serializer, reserved = reserved, |
|
260 |
includes = includes, name_syntax_table = name_syntax_table, module_alias = module_alias }; |
|
261 |
fun map_target f ( Target { serial, serializer, reserved, includes, name_syntax_table, module_alias } ) = |
|
262 |
mk_target (f ((serial, serializer), ((reserved, includes), (name_syntax_table, module_alias)))); |
|
263 |
fun merge_target strict target (Target { serial = serial1, serializer = serializer, |
|
264 |
reserved = reserved1, includes = includes1, |
|
265 |
name_syntax_table = name_syntax_table1, module_alias = module_alias1 }, |
|
266 |
Target { serial = serial2, serializer = _, |
|
267 |
reserved = reserved2, includes = includes2, |
|
268 |
name_syntax_table = name_syntax_table2, module_alias = module_alias2 }) = |
|
269 |
if serial1 = serial2 orelse not strict then |
|
270 |
mk_target ((serial1, serializer), |
|
271 |
((merge (op =) (reserved1, reserved2), Symtab.merge (op =) (includes1, includes2)), |
|
272 |
(merge_name_syntax_table (name_syntax_table1, name_syntax_table2), |
|
273 |
Symtab.join (K snd) (module_alias1, module_alias2)) |
|
274 |
)) |
|
275 |
else |
|
276 |
error ("Incompatible serializers: " ^ quote target); |
|
27103 | 277 |
|
28054 | 278 |
structure CodeTargetData = TheoryDataFun |
27436 | 279 |
( |
28054 | 280 |
type T = target Symtab.table * string list; |
281 |
val empty = (Symtab.empty, []); |
|
282 |
val copy = I; |
|
283 |
val extend = I; |
|
284 |
fun merge _ ((target1, exc1) : T, (target2, exc2)) = |
|
285 |
(Symtab.join (merge_target true) (target1, target2), Library.merge (op =) (exc1, exc2)); |
|
27436 | 286 |
); |
287 |
||
28054 | 288 |
fun the_serializer (Target { serializer, ... }) = serializer; |
289 |
fun the_reserved (Target { reserved, ... }) = reserved; |
|
290 |
fun the_includes (Target { includes, ... }) = includes; |
|
291 |
fun the_name_syntax (Target { name_syntax_table = NameSyntaxTable x, ... }) = x; |
|
292 |
fun the_module_alias (Target { module_alias , ... }) = module_alias; |
|
27437 | 293 |
|
28054 | 294 |
val abort_allowed = snd o CodeTargetData.get; |
27436 | 295 |
|
28054 | 296 |
fun assert_target thy target = |
297 |
case Symtab.lookup (fst (CodeTargetData.get thy)) target |
|
298 |
of SOME data => target |
|
299 |
| NONE => error ("Unknown code target language: " ^ quote target); |
|
300 |
||
301 |
fun put_target (target, seri) thy = |
|
27304 | 302 |
let |
28054 | 303 |
val defined_target = is_some o Symtab.lookup (fst (CodeTargetData.get thy)); |
304 |
val _ = case seri |
|
305 |
of Extends (super, _) => if defined_target super then () |
|
306 |
else error ("Unknown code target language: " ^ quote super) |
|
307 |
| _ => (); |
|
308 |
val _ = if defined_target target |
|
309 |
then warning ("Overwriting existing target " ^ quote target) |
|
310 |
else (); |
|
311 |
in |
|
312 |
thy |
|
313 |
|> (CodeTargetData.map o apfst oo Symtab.map_default) |
|
314 |
(target, mk_target ((serial (), seri), (([], Symtab.empty), |
|
315 |
(mk_name_syntax_table ((Symtab.empty, Symtab.empty), (Symtab.empty, Symtab.empty)), |
|
316 |
Symtab.empty)))) |
|
317 |
((map_target o apfst o apsnd o K) seri) |
|
318 |
end; |
|
27436 | 319 |
|
28054 | 320 |
fun add_target (target, seri) = put_target (target, Serializer seri); |
321 |
fun extend_target (target, (super, modify)) = |
|
322 |
put_target (target, Extends (super, modify)); |
|
27436 | 323 |
|
28054 | 324 |
fun map_target_data target f thy = |
27436 | 325 |
let |
28054 | 326 |
val _ = assert_target thy target; |
327 |
in |
|
328 |
thy |
|
329 |
|> (CodeTargetData.map o apfst o Symtab.map_entry target o map_target) f |
|
330 |
end; |
|
27304 | 331 |
|
28054 | 332 |
fun map_reserved target = |
333 |
map_target_data target o apsnd o apfst o apfst; |
|
334 |
fun map_includes target = |
|
335 |
map_target_data target o apsnd o apfst o apsnd; |
|
336 |
fun map_name_syntax target = |
|
337 |
map_target_data target o apsnd o apsnd o apfst o map_name_syntax_table; |
|
338 |
fun map_module_alias target = |
|
339 |
map_target_data target o apsnd o apsnd o apsnd; |
|
27000 | 340 |
|
341 |
||
28054 | 342 |
(** serializer configuration **) |
27000 | 343 |
|
344 |
(* data access *) |
|
24219 | 345 |
|
346 |
local |
|
347 |
||
24992 | 348 |
fun cert_class thy class = |
349 |
let |
|
350 |
val _ = AxClass.get_info thy class; |
|
351 |
in class end; |
|
352 |
||
27103 | 353 |
fun read_class thy = cert_class thy o Sign.intern_class thy; |
24992 | 354 |
|
355 |
fun cert_tyco thy tyco = |
|
356 |
let |
|
357 |
val _ = if Sign.declared_tyname thy tyco then () |
|
358 |
else error ("No such type constructor: " ^ quote tyco); |
|
359 |
in tyco end; |
|
360 |
||
27103 | 361 |
fun read_tyco thy = cert_tyco thy o Sign.intern_type thy; |
24219 | 362 |
|
363 |
fun gen_add_syntax_class prep_class prep_const target raw_class raw_syn thy = |
|
364 |
let |
|
24841 | 365 |
val class = prep_class thy raw_class; |
28054 | 366 |
val class' = Code_Name.class thy class; |
24841 | 367 |
fun mk_classparam c = case AxClass.class_of_param thy c |
28054 | 368 |
of SOME class'' => if class = class'' then Code_Name.const thy c |
24219 | 369 |
else error ("Not a class operation for class " ^ quote class ^ ": " ^ quote c) |
370 |
| NONE => error ("Not a class operation: " ^ quote c); |
|
24841 | 371 |
fun mk_syntax_params raw_params = AList.lookup (op =) |
372 |
((map o apfst) (mk_classparam o prep_const thy) raw_params); |
|
24219 | 373 |
in case raw_syn |
24841 | 374 |
of SOME (syntax, raw_params) => |
24219 | 375 |
thy |
27024 | 376 |
|> (map_name_syntax target o apfst o apfst) |
24841 | 377 |
(Symtab.update (class', (syntax, mk_syntax_params raw_params))) |
24219 | 378 |
| NONE => |
379 |
thy |
|
27024 | 380 |
|> (map_name_syntax target o apfst o apfst) |
24841 | 381 |
(Symtab.delete_safe class') |
24219 | 382 |
end; |
383 |
||
384 |
fun gen_add_syntax_inst prep_class prep_tyco target (raw_tyco, raw_class) add_del thy = |
|
385 |
let |
|
28054 | 386 |
val inst = Code_Name.instance thy (prep_class thy raw_class, prep_tyco thy raw_tyco); |
24219 | 387 |
in if add_del then |
388 |
thy |
|
27024 | 389 |
|> (map_name_syntax target o apfst o apsnd) |
24219 | 390 |
(Symtab.update (inst, ())) |
391 |
else |
|
392 |
thy |
|
27024 | 393 |
|> (map_name_syntax target o apfst o apsnd) |
24219 | 394 |
(Symtab.delete_safe inst) |
395 |
end; |
|
396 |
||
397 |
fun gen_add_syntax_tyco prep_tyco target raw_tyco raw_syn thy = |
|
398 |
let |
|
399 |
val tyco = prep_tyco thy raw_tyco; |
|
28054 | 400 |
val tyco' = if tyco = "fun" then "fun" else Code_Name.tyco thy tyco; |
24219 | 401 |
fun check_args (syntax as (n, _)) = if n <> Sign.arity_number thy tyco |
402 |
then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco) |
|
403 |
else syntax |
|
404 |
in case raw_syn |
|
405 |
of SOME syntax => |
|
406 |
thy |
|
27024 | 407 |
|> (map_name_syntax target o apsnd o apfst) |
24219 | 408 |
(Symtab.update (tyco', check_args syntax)) |
409 |
| NONE => |
|
410 |
thy |
|
27024 | 411 |
|> (map_name_syntax target o apsnd o apfst) |
24219 | 412 |
(Symtab.delete_safe tyco') |
413 |
end; |
|
414 |
||
415 |
fun gen_add_syntax_const prep_const target raw_c raw_syn thy = |
|
416 |
let |
|
417 |
val c = prep_const thy raw_c; |
|
28054 | 418 |
val c' = Code_Name.const thy c; |
419 |
fun check_args (syntax as (n, _)) = if n > Code_Unit.no_args thy c |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
420 |
then error ("Too many arguments in syntax for constant " ^ quote c) |
24219 | 421 |
else syntax; |
422 |
in case raw_syn |
|
423 |
of SOME syntax => |
|
424 |
thy |
|
27024 | 425 |
|> (map_name_syntax target o apsnd o apsnd) |
24219 | 426 |
(Symtab.update (c', check_args syntax)) |
427 |
| NONE => |
|
428 |
thy |
|
27024 | 429 |
|> (map_name_syntax target o apsnd o apsnd) |
24219 | 430 |
(Symtab.delete_safe c') |
431 |
end; |
|
432 |
||
433 |
fun add_reserved target = |
|
434 |
let |
|
435 |
fun add sym syms = if member (op =) syms sym |
|
436 |
then error ("Reserved symbol " ^ quote sym ^ " already declared") |
|
437 |
else insert (op =) sym syms |
|
27103 | 438 |
in map_reserved target o add end; |
24992 | 439 |
|
440 |
fun add_include target = |
|
441 |
let |
|
442 |
fun add (name, SOME content) incls = |
|
443 |
let |
|
444 |
val _ = if Symtab.defined incls name |
|
445 |
then warning ("Overwriting existing include " ^ name) |
|
446 |
else (); |
|
447 |
in Symtab.update (name, str content) incls end |
|
448 |
| add (name, NONE) incls = |
|
449 |
Symtab.delete name incls; |
|
27103 | 450 |
in map_includes target o add end; |
24219 | 451 |
|
27103 | 452 |
fun add_module_alias target = |
28054 | 453 |
map_module_alias target o Symtab.update o apsnd Code_Name.check_modulename; |
24219 | 454 |
|
27103 | 455 |
fun gen_allow_abort prep_cs raw_c thy = |
24841 | 456 |
let |
457 |
val c = prep_cs thy raw_c; |
|
28054 | 458 |
val c' = Code_Name.const thy c; |
24841 | 459 |
in thy |> (CodeTargetData.map o apsnd) (insert (op =) c') end; |
460 |
||
24219 | 461 |
fun zip_list (x::xs) f g = |
462 |
f |
|
463 |
#-> (fn y => |
|
464 |
fold_map (fn x => g |-- f >> pair x) xs |
|
465 |
#-> (fn xys => pair ((x, y) :: xys))); |
|
466 |
||
27000 | 467 |
|
27103 | 468 |
(* concrete syntax *) |
27000 | 469 |
|
24219 | 470 |
structure P = OuterParse |
471 |
and K = OuterKeyword |
|
472 |
||
473 |
fun parse_multi_syntax parse_thing parse_syntax = |
|
474 |
P.and_list1 parse_thing |
|
475 |
#-> (fn things => Scan.repeat1 (P.$$$ "(" |-- P.name -- |
|
476 |
(zip_list things parse_syntax (P.$$$ "and")) --| P.$$$ ")")); |
|
477 |
||
478 |
in |
|
479 |
||
480 |
val parse_syntax = parse_syntax; |
|
481 |
||
482 |
val add_syntax_class = gen_add_syntax_class cert_class (K I); |
|
483 |
val add_syntax_inst = gen_add_syntax_inst cert_class cert_tyco; |
|
484 |
val add_syntax_tyco = gen_add_syntax_tyco cert_tyco; |
|
485 |
val add_syntax_const = gen_add_syntax_const (K I); |
|
27103 | 486 |
val allow_abort = gen_allow_abort (K I); |
28054 | 487 |
val add_reserved = add_reserved; |
24219 | 488 |
|
28054 | 489 |
val add_syntax_class_cmd = gen_add_syntax_class read_class Code_Unit.read_const; |
24219 | 490 |
val add_syntax_inst_cmd = gen_add_syntax_inst read_class read_tyco; |
491 |
val add_syntax_tyco_cmd = gen_add_syntax_tyco read_tyco; |
|
28054 | 492 |
val add_syntax_const_cmd = gen_add_syntax_const Code_Unit.read_const; |
493 |
val allow_abort_cmd = gen_allow_abort Code_Unit.read_const; |
|
24219 | 494 |
|
28054 | 495 |
fun add_syntax_tycoP target tyco = parse_syntax I |
496 |
>> add_syntax_tyco_cmd target tyco; |
|
497 |
fun add_syntax_constP target c = parse_syntax fst |
|
498 |
>> (add_syntax_const_cmd target c o Code_Printer.simple_const_syntax); |
|
24219 | 499 |
|
28064 | 500 |
fun the_literals thy = |
501 |
let |
|
502 |
val (targets, _) = CodeTargetData.get thy; |
|
503 |
fun literals target = case Symtab.lookup targets target |
|
504 |
of SOME data => (case the_serializer data |
|
505 |
of Serializer (_, literals) => literals |
|
506 |
| Extends (super, _) => literals super) |
|
507 |
| NONE => error ("Unknown code target language: " ^ quote target); |
|
508 |
in literals end; |
|
509 |
||
510 |
fun add_literal_list target nill cons thy = |
|
24219 | 511 |
let |
28054 | 512 |
val nil' = Code_Name.const thy nill; |
513 |
val cons' = Code_Name.const thy cons; |
|
28064 | 514 |
val pr = pretty_list nil' cons' (the_literals thy target); |
24219 | 515 |
in |
516 |
thy |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
517 |
|> add_syntax_const target cons (SOME pr) |
24219 | 518 |
end; |
519 |
||
28064 | 520 |
fun add_literal_list_string target nill cons charr nibbles thy = |
521 |
let |
|
522 |
val nil' = Code_Name.const thy nill; |
|
523 |
val cons' = Code_Name.const thy cons; |
|
524 |
val charr' = Code_Name.const thy charr; |
|
525 |
val nibbles' = map (Code_Name.const thy) nibbles; |
|
526 |
val pr = pretty_list_string nil' cons' charr' nibbles' (the_literals thy target); |
|
527 |
in |
|
528 |
thy |
|
529 |
|> add_syntax_const target cons (SOME pr) |
|
530 |
end; |
|
531 |
||
532 |
fun add_literal_char target charr nibbles thy = |
|
24219 | 533 |
let |
28054 | 534 |
val charr' = Code_Name.const thy charr; |
535 |
val nibbles' = map (Code_Name.const thy) nibbles; |
|
28064 | 536 |
val pr = pretty_char charr' nibbles' (the_literals thy target); |
24219 | 537 |
in |
538 |
thy |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
539 |
|> add_syntax_const target charr (SOME pr) |
24219 | 540 |
end; |
541 |
||
28064 | 542 |
fun add_literal_numeral target unbounded negative number_of pls min bit0 bit1 thy = |
24219 | 543 |
let |
28054 | 544 |
val pls' = Code_Name.const thy pls; |
545 |
val min' = Code_Name.const thy min; |
|
546 |
val bit0' = Code_Name.const thy bit0; |
|
547 |
val bit1' = Code_Name.const thy bit1; |
|
28064 | 548 |
val pr = pretty_numeral unbounded negative pls' min' bit0' bit1' (the_literals thy target); |
24219 | 549 |
in |
550 |
thy |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
551 |
|> add_syntax_const target number_of (SOME pr) |
24219 | 552 |
end; |
553 |
||
28064 | 554 |
fun add_literal_message target charr nibbles nill cons str thy = |
24219 | 555 |
let |
28054 | 556 |
val charr' = Code_Name.const thy charr; |
557 |
val nibbles' = map (Code_Name.const thy) nibbles; |
|
558 |
val nil' = Code_Name.const thy nill; |
|
559 |
val cons' = Code_Name.const thy cons; |
|
28064 | 560 |
val pr = pretty_message charr' nibbles' nil' cons' (the_literals thy target); |
24219 | 561 |
in |
562 |
thy |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
563 |
|> add_syntax_const target str (SOME pr) |
24219 | 564 |
end; |
565 |
||
24867 | 566 |
|
28054 | 567 |
(** serializer usage **) |
568 |
||
569 |
(* montage *) |
|
570 |
||
571 |
fun invoke_serializer thy modify abortable serializer reserved includes |
|
572 |
module_alias class inst tyco const module args program1 cs1 = |
|
573 |
let |
|
574 |
val program2 = modify program1; |
|
575 |
val hidden = Symtab.keys class @ Symtab.keys inst @ Symtab.keys tyco @ Symtab.keys const; |
|
576 |
val cs2 = subtract (op =) hidden cs1; |
|
577 |
val program3 = Graph.subgraph (not o member (op =) hidden) program2; |
|
578 |
val all_cs = Graph.all_succs program2 cs2; |
|
579 |
val program4 = Graph.subgraph (member (op =) all_cs) program3; |
|
580 |
val empty_funs = filter_out (member (op =) abortable) |
|
581 |
(Code_Thingol.empty_funs program3); |
|
582 |
val _ = if null empty_funs then () else error ("No defining equations for " |
|
583 |
^ commas (map (Code_Name.labelled_name thy) empty_funs)); |
|
584 |
in |
|
585 |
serializer module args (Code_Name.labelled_name thy) reserved includes |
|
586 |
(Symtab.lookup module_alias) (Symtab.lookup class) |
|
587 |
(Symtab.lookup tyco) (Symtab.lookup const) |
|
588 |
program4 cs2 |
|
589 |
end; |
|
590 |
||
591 |
fun mount_serializer thy alt_serializer target = |
|
592 |
let |
|
593 |
val (targets, abortable) = CodeTargetData.get thy; |
|
594 |
fun collapse_hierarchy target = |
|
595 |
let |
|
596 |
val data = case Symtab.lookup targets target |
|
597 |
of SOME data => data |
|
598 |
| NONE => error ("Unknown code target language: " ^ quote target); |
|
599 |
in case the_serializer data |
|
600 |
of Serializer _ => (I, data) |
|
601 |
| Extends (super, modify) => let |
|
602 |
val (modify', data') = collapse_hierarchy super |
|
603 |
in (modify' #> modify, merge_target false target (data', data)) end |
|
604 |
end; |
|
605 |
val (modify, data) = collapse_hierarchy target; |
|
28064 | 606 |
val (serializer, _) = the_default (case the_serializer data |
28054 | 607 |
of Serializer seri => seri) alt_serializer; |
608 |
val reserved = the_reserved data; |
|
609 |
val includes = Symtab.dest (the_includes data); |
|
610 |
val module_alias = the_module_alias data; |
|
611 |
val { class, inst, tyco, const } = the_name_syntax data; |
|
612 |
in |
|
613 |
invoke_serializer thy modify abortable serializer reserved |
|
614 |
includes module_alias class inst tyco const |
|
615 |
end; |
|
616 |
||
617 |
fun serialize thy = mount_serializer thy NONE; |
|
618 |
||
619 |
fun serialize_custom thy (target_name, seri) program cs = |
|
620 |
mount_serializer thy (SOME seri) target_name NONE [] program cs (String []) |
|
621 |
|> the; |
|
622 |
||
623 |
fun parse_args f args = |
|
624 |
case Scan.read OuterLex.stopper f args |
|
625 |
of SOME x => x |
|
626 |
| NONE => error "Bad serializer arguments"; |
|
627 |
||
628 |
||
629 |
(* code presentation *) |
|
630 |
||
631 |
fun code_of thy target module_name cs stmt_names = |
|
632 |
let |
|
633 |
val (cs', program) = Code_Thingol.consts_program thy cs; |
|
634 |
in |
|
635 |
string stmt_names (serialize thy target (SOME module_name) [] program cs') |
|
636 |
end; |
|
637 |
||
638 |
||
639 |
(* code generation *) |
|
640 |
||
641 |
fun read_const_exprs thy cs = |
|
642 |
let |
|
643 |
val (cs1, cs2) = Code_Name.read_const_exprs thy cs; |
|
644 |
val (cs3, program) = Code_Thingol.consts_program thy cs2; |
|
645 |
val cs4 = Code_Thingol.transitivly_non_empty_funs program (abort_allowed thy); |
|
646 |
val cs5 = map_filter |
|
647 |
(fn (c, c') => if member (op =) cs4 c' then SOME c else NONE) (cs2 ~~ cs3); |
|
648 |
in fold (insert (op =)) cs5 cs1 end; |
|
649 |
||
650 |
fun cached_program thy = |
|
651 |
let |
|
652 |
val program = Code_Thingol.cached_program thy; |
|
653 |
in (Code_Thingol.transitivly_non_empty_funs program (abort_allowed thy), program) end |
|
654 |
||
655 |
fun export_code thy cs seris = |
|
656 |
let |
|
657 |
val (cs', program) = if null cs then cached_program thy |
|
658 |
else Code_Thingol.consts_program thy cs; |
|
659 |
fun mk_seri_dest dest = case dest |
|
660 |
of NONE => compile |
|
661 |
| SOME "-" => export |
|
662 |
| SOME f => file (Path.explode f) |
|
663 |
val _ = map (fn (((target, module), dest), args) => |
|
664 |
(mk_seri_dest dest (serialize thy target module args program cs'))) seris; |
|
665 |
in () end; |
|
666 |
||
667 |
fun export_code_cmd raw_cs seris thy = export_code thy (read_const_exprs thy raw_cs) seris; |
|
668 |
||
27103 | 669 |
|
27304 | 670 |
(** Isar setup **) |
27103 | 671 |
|
672 |
val (inK, module_nameK, fileK) = ("in", "module_name", "file"); |
|
673 |
||
674 |
fun code_exprP cmd = |
|
27757
650af1991b8b
fall back on P.term_group, to avoid problems with inner_syntax markup (due to CodeName.read_const_exprs);
wenzelm
parents:
27710
diff
changeset
|
675 |
(Scan.repeat P.term_group |
27103 | 676 |
-- Scan.repeat (P.$$$ inK |-- P.name |
677 |
-- Scan.option (P.$$$ module_nameK |-- P.name) |
|
678 |
-- Scan.option (P.$$$ fileK |-- P.name) |
|
27809
a1e409db516b
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27757
diff
changeset
|
679 |
-- Scan.optional (P.$$$ "(" |-- Args.parse --| P.$$$ ")") [] |
27103 | 680 |
) >> (fn (raw_cs, seris) => cmd raw_cs seris)); |
681 |
||
28054 | 682 |
val _ = List.app OuterKeyword.keyword [inK, module_nameK, fileK]; |
24867 | 683 |
|
684 |
val _ = |
|
24992 | 685 |
OuterSyntax.command "code_class" "define code syntax for class" K.thy_decl ( |
24219 | 686 |
parse_multi_syntax P.xname |
687 |
(Scan.option (P.string -- Scan.optional (P.$$$ "where" |-- Scan.repeat1 |
|
27757
650af1991b8b
fall back on P.term_group, to avoid problems with inner_syntax markup (due to CodeName.read_const_exprs);
wenzelm
parents:
27710
diff
changeset
|
688 |
(P.term_group --| (P.$$$ "\<equiv>" || P.$$$ "==") -- P.string)) [])) |
24219 | 689 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
690 |
fold (fn (raw_class, syn) => add_syntax_class_cmd target raw_class syn) syns) |
|
691 |
); |
|
692 |
||
24867 | 693 |
val _ = |
24992 | 694 |
OuterSyntax.command "code_instance" "define code syntax for instance" K.thy_decl ( |
24219 | 695 |
parse_multi_syntax (P.xname --| P.$$$ "::" -- P.xname) |
696 |
((P.minus >> K true) || Scan.succeed false) |
|
697 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
|
698 |
fold (fn (raw_inst, add_del) => add_syntax_inst_cmd target raw_inst add_del) syns) |
|
699 |
); |
|
700 |
||
24867 | 701 |
val _ = |
24992 | 702 |
OuterSyntax.command "code_type" "define code syntax for type constructor" K.thy_decl ( |
24219 | 703 |
parse_multi_syntax P.xname (parse_syntax I) |
704 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
|
705 |
fold (fn (raw_tyco, syn) => add_syntax_tyco_cmd target raw_tyco syn) syns) |
|
706 |
); |
|
707 |
||
24867 | 708 |
val _ = |
24992 | 709 |
OuterSyntax.command "code_const" "define code syntax for constant" K.thy_decl ( |
27757
650af1991b8b
fall back on P.term_group, to avoid problems with inner_syntax markup (due to CodeName.read_const_exprs);
wenzelm
parents:
27710
diff
changeset
|
710 |
parse_multi_syntax P.term_group (parse_syntax fst) |
24219 | 711 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
28054 | 712 |
fold (fn (raw_const, syn) => add_syntax_const_cmd target raw_const |
713 |
(Code_Printer.simple_const_syntax syn)) syns) |
|
24219 | 714 |
); |
715 |
||
24867 | 716 |
val _ = |
24992 | 717 |
OuterSyntax.command "code_reserved" "declare words as reserved for target language" K.thy_decl ( |
24219 | 718 |
P.name -- Scan.repeat1 P.name |
719 |
>> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds) |
|
24841 | 720 |
); |
24219 | 721 |
|
24867 | 722 |
val _ = |
24992 | 723 |
OuterSyntax.command "code_include" "declare piece of code to be included in generated code" K.thy_decl ( |
724 |
P.name -- P.name -- (P.text >> (fn "-" => NONE | s => SOME s)) |
|
725 |
>> (fn ((target, name), content) => (Toplevel.theory o add_include target) |
|
726 |
(name, content)) |
|
727 |
); |
|
728 |
||
729 |
val _ = |
|
730 |
OuterSyntax.command "code_modulename" "alias module to other name" K.thy_decl ( |
|
24219 | 731 |
P.name -- Scan.repeat1 (P.name -- P.name) |
27103 | 732 |
>> (fn (target, modlnames) => (Toplevel.theory o fold (add_module_alias target)) modlnames) |
733 |
); |
|
734 |
||
735 |
val _ = |
|
736 |
OuterSyntax.command "code_abort" "permit constant to be implemented as program abort" K.thy_decl ( |
|
27757
650af1991b8b
fall back on P.term_group, to avoid problems with inner_syntax markup (due to CodeName.read_const_exprs);
wenzelm
parents:
27710
diff
changeset
|
737 |
Scan.repeat1 P.term_group >> (Toplevel.theory o fold allow_abort_cmd) |
24841 | 738 |
); |
24219 | 739 |
|
24867 | 740 |
val _ = |
27103 | 741 |
OuterSyntax.command "export_code" "generate executable code for constants" |
742 |
K.diag (P.!!! (code_exprP export_code_cmd) >> (fn f => Toplevel.keep (f o Toplevel.theory_of))); |
|
743 |
||
744 |
fun shell_command thyname cmd = Toplevel.program (fn _ => |
|
27845 | 745 |
(use_thy thyname; case Scan.read OuterLex.stopper (P.!!! (code_exprP export_code_cmd)) |
746 |
((filter OuterLex.is_proper o OuterSyntax.scan Position.none) cmd) |
|
27103 | 747 |
of SOME f => (writeln "Now generating code..."; f (theory thyname)) |
748 |
| NONE => error ("Bad directive " ^ quote cmd))) |
|
749 |
handle TOPLEVEL_ERROR => OS.Process.exit OS.Process.failure; |
|
750 |
||
24219 | 751 |
end; (*local*) |
752 |
||
753 |
end; (*struct*) |