haftmann@37744
|
1 |
(* Title: Tools/Code/code_target.ML
|
haftmann@24219
|
2 |
Author: Florian Haftmann, TU Muenchen
|
haftmann@24219
|
3 |
|
haftmann@39136
|
4 |
Generic infrastructure for target language data.
|
haftmann@24219
|
5 |
*)
|
haftmann@24219
|
6 |
|
haftmann@24219
|
7 |
signature CODE_TARGET =
|
haftmann@24219
|
8 |
sig
|
haftmann@36467
|
9 |
val cert_tyco: theory -> string -> string
|
haftmann@36467
|
10 |
val read_tyco: theory -> string -> string
|
haftmann@39291
|
11 |
val read_const_exprs: theory -> string list -> string list
|
haftmann@36467
|
12 |
|
haftmann@39159
|
13 |
val export_code_for: theory -> Path.T option -> string -> int option -> string -> Token.T list
|
haftmann@39144
|
14 |
-> Code_Thingol.naming -> Code_Thingol.program -> string list -> unit
|
haftmann@39159
|
15 |
val produce_code_for: theory -> string -> int option -> string -> Token.T list
|
haftmann@49583
|
16 |
-> Code_Thingol.naming -> Code_Thingol.program -> string list -> (string * string) list * string option list
|
haftmann@39159
|
17 |
val present_code_for: theory -> string -> int option -> string -> Token.T list
|
haftmann@39155
|
18 |
-> Code_Thingol.naming -> Code_Thingol.program -> string list * string list -> string
|
haftmann@39144
|
19 |
val check_code_for: theory -> string -> bool -> Token.T list
|
haftmann@39144
|
20 |
-> Code_Thingol.naming -> Code_Thingol.program -> string list -> unit
|
haftmann@39144
|
21 |
|
haftmann@39144
|
22 |
val export_code: theory -> string list
|
haftmann@39159
|
23 |
-> (((string * string) * Path.T option) * Token.T list) list -> unit
|
haftmann@39155
|
24 |
val produce_code: theory -> string list
|
haftmann@49583
|
25 |
-> string -> int option -> string -> Token.T list -> (string * string) list * string option list
|
haftmann@39155
|
26 |
val present_code: theory -> string list -> (Code_Thingol.naming -> string list)
|
haftmann@39159
|
27 |
-> string -> int option -> string -> Token.T list -> string
|
haftmann@39144
|
28 |
val check_code: theory -> string list
|
haftmann@39144
|
29 |
-> ((string * bool) * Token.T list) list -> unit
|
haftmann@39144
|
30 |
|
haftmann@49583
|
31 |
val generatedN: string
|
haftmann@41592
|
32 |
val evaluator: theory -> string -> Code_Thingol.naming -> Code_Thingol.program
|
haftmann@41592
|
33 |
-> string list -> ((string * class list) list * Code_Thingol.itype) * Code_Thingol.iterm
|
haftmann@49583
|
34 |
-> (string * string) list * string
|
haftmann@41592
|
35 |
|
haftmann@28054
|
36 |
type serializer
|
haftmann@34141
|
37 |
type literals = Code_Printer.literals
|
haftmann@37822
|
38 |
val add_target: string * { serializer: serializer, literals: literals,
|
wenzelm@42811
|
39 |
check: { env_var: string, make_destination: Path.T -> Path.T, make_command: string -> string } }
|
wenzelm@42811
|
40 |
-> theory -> theory
|
haftmann@28663
|
41 |
val extend_target: string *
|
haftmann@28663
|
42 |
(string * (Code_Thingol.naming -> Code_Thingol.program -> Code_Thingol.program))
|
haftmann@28054
|
43 |
-> theory -> theory
|
haftmann@28054
|
44 |
val assert_target: theory -> string -> string
|
haftmann@39144
|
45 |
val the_literals: theory -> string -> literals
|
haftmann@28054
|
46 |
type serialization
|
wenzelm@36969
|
47 |
val parse_args: 'a parser -> Token.T list -> 'a
|
haftmann@39142
|
48 |
val serialization: (int -> Path.T option -> 'a -> unit)
|
haftmann@49583
|
49 |
-> (string list -> int -> 'a -> (string * string) list * (string -> string option))
|
haftmann@39151
|
50 |
-> 'a -> serialization
|
haftmann@39144
|
51 |
val set_default_code_width: int -> theory -> theory
|
haftmann@39143
|
52 |
|
haftmann@53274
|
53 |
type ('a, 'b, 'c, 'd, 'e, 'f) symbol_attr_decl
|
haftmann@53274
|
54 |
type const_syntax = Code_Printer.const_syntax
|
haftmann@34141
|
55 |
type tyco_syntax = Code_Printer.tyco_syntax
|
haftmann@53274
|
56 |
val set_printings: (const_syntax, tyco_syntax, string, unit, unit, (string * string list)) symbol_attr_decl
|
haftmann@53274
|
57 |
-> theory -> theory
|
haftmann@53274
|
58 |
val add_const_syntax: string -> string -> const_syntax option -> theory -> theory
|
haftmann@53274
|
59 |
val add_tyco_syntax: string -> string -> tyco_syntax option -> theory -> theory
|
haftmann@39149
|
60 |
val add_class_syntax: string -> class -> string option -> theory -> theory
|
haftmann@39149
|
61 |
val add_instance_syntax: string -> class * string -> unit option -> theory -> theory
|
haftmann@28054
|
62 |
val add_reserved: string -> string -> theory -> theory
|
haftmann@33968
|
63 |
val add_include: string -> string * (string * string list) option -> theory -> theory
|
haftmann@53274
|
64 |
val allow_abort: string -> theory -> theory
|
haftmann@39870
|
65 |
|
haftmann@39983
|
66 |
val codegen_tool: string (*theory name*) -> string (*export_code expr*) -> unit
|
wenzelm@44450
|
67 |
|
wenzelm@44450
|
68 |
val setup: theory -> theory
|
haftmann@24219
|
69 |
end;
|
haftmann@24219
|
70 |
|
haftmann@28054
|
71 |
structure Code_Target : CODE_TARGET =
|
haftmann@24219
|
72 |
struct
|
haftmann@24219
|
73 |
|
haftmann@28054
|
74 |
open Basic_Code_Thingol;
|
haftmann@34141
|
75 |
|
haftmann@34141
|
76 |
type literals = Code_Printer.literals;
|
haftmann@53274
|
77 |
type ('a, 'b, 'c, 'd, 'e, 'f) symbol_attr_decl =
|
haftmann@53274
|
78 |
(string * (string * 'a option) list, string * (string * 'b option) list,
|
haftmann@53274
|
79 |
class * (string * 'c option) list, (class * class) * (string * 'd option) list,
|
haftmann@53274
|
80 |
(class * string) * (string * 'e option) list,
|
haftmann@53274
|
81 |
string * (string * 'f option) list) Code_Symbol.attr;
|
haftmann@53274
|
82 |
|
haftmann@34141
|
83 |
type tyco_syntax = Code_Printer.tyco_syntax;
|
haftmann@37849
|
84 |
type const_syntax = Code_Printer.const_syntax;
|
haftmann@34141
|
85 |
|
haftmann@24219
|
86 |
|
haftmann@53274
|
87 |
(** serializations and serializer **)
|
haftmann@53274
|
88 |
|
haftmann@53274
|
89 |
(* serialization: abstract nonsense to cover different destinies for generated code *)
|
haftmann@24219
|
90 |
|
haftmann@39155
|
91 |
datatype destination = Export of Path.T option | Produce | Present of string list;
|
haftmann@49583
|
92 |
type serialization = int -> destination -> ((string * string) list * (string -> string option)) option;
|
haftmann@27014
|
93 |
|
haftmann@39885
|
94 |
fun serialization output _ content width (Export some_path) =
|
haftmann@39885
|
95 |
(output width some_path content; NONE)
|
haftmann@39885
|
96 |
| serialization _ string content width Produce =
|
haftmann@39885
|
97 |
string [] width content |> SOME
|
haftmann@39885
|
98 |
| serialization _ string content width (Present stmt_names) =
|
haftmann@39885
|
99 |
string stmt_names width content
|
haftmann@49583
|
100 |
|> (apfst o map o apsnd) (Pretty.output (SOME width) o Pretty.str)
|
haftmann@39885
|
101 |
|> SOME;
|
haftmann@24219
|
102 |
|
haftmann@39885
|
103 |
fun export some_path f = (f (Export some_path); ());
|
haftmann@39885
|
104 |
fun produce f = the (f Produce);
|
haftmann@49583
|
105 |
fun present stmt_names f = space_implode "\n\n" (map snd (fst (the (f (Present stmt_names)))));
|
haftmann@39143
|
106 |
|
haftmann@24219
|
107 |
|
haftmann@53274
|
108 |
(* serializers: functions producing serializations *)
|
haftmann@27103
|
109 |
|
haftmann@39381
|
110 |
type serializer = Token.T list
|
haftmann@39381
|
111 |
-> {
|
haftmann@39152
|
112 |
labelled_name: string -> string,
|
haftmann@39152
|
113 |
reserved_syms: string list,
|
haftmann@39152
|
114 |
includes: (string * Pretty.T) list,
|
haftmann@39152
|
115 |
module_alias: string -> string option,
|
haftmann@39152
|
116 |
class_syntax: string -> string option,
|
haftmann@39152
|
117 |
tyco_syntax: string -> Code_Printer.tyco_syntax option,
|
haftmann@41590
|
118 |
const_syntax: string -> Code_Printer.activated_const_syntax option }
|
haftmann@41590
|
119 |
-> Code_Thingol.program
|
haftmann@28054
|
120 |
-> serialization;
|
haftmann@27103
|
121 |
|
haftmann@53274
|
122 |
datatype description =
|
haftmann@53274
|
123 |
Fundamental of { serializer: serializer,
|
haftmann@39136
|
124 |
literals: literals,
|
haftmann@37822
|
125 |
check: { env_var: string, make_destination: Path.T -> Path.T,
|
wenzelm@42811
|
126 |
make_command: string -> string } }
|
haftmann@39135
|
127 |
| Extension of string *
|
haftmann@39135
|
128 |
(Code_Thingol.naming -> Code_Thingol.program -> Code_Thingol.program);
|
haftmann@27103
|
129 |
|
haftmann@53274
|
130 |
|
haftmann@53274
|
131 |
(** theory data **)
|
haftmann@53274
|
132 |
|
haftmann@28054
|
133 |
datatype target = Target of {
|
haftmann@28054
|
134 |
serial: serial,
|
haftmann@37821
|
135 |
description: description,
|
haftmann@28054
|
136 |
reserved: string list,
|
haftmann@39151
|
137 |
module_alias: string Symtab.table,
|
haftmann@53274
|
138 |
printings: (Code_Printer.const_syntax, Code_Printer.tyco_syntax, string, unit, unit,
|
haftmann@53274
|
139 |
(Pretty.T * string list)) Code_Symbol.data
|
haftmann@28054
|
140 |
};
|
haftmann@27103
|
141 |
|
haftmann@53274
|
142 |
fun make_target ((serial, description), ((reserved, module_alias), printings)) =
|
wenzelm@44450
|
143 |
Target { serial = serial, description = description, reserved = reserved,
|
haftmann@53274
|
144 |
module_alias = module_alias, printings = printings };
|
haftmann@53274
|
145 |
fun map_target f ( Target { serial, description, reserved, module_alias, printings } ) =
|
haftmann@53274
|
146 |
make_target (f ((serial, description), ((reserved, module_alias), printings)));
|
haftmann@37821
|
147 |
fun merge_target strict target (Target { serial = serial1, description = description,
|
haftmann@53274
|
148 |
reserved = reserved1, module_alias = module_alias1, printings = printings1 },
|
haftmann@37821
|
149 |
Target { serial = serial2, description = _,
|
haftmann@53274
|
150 |
reserved = reserved2, module_alias = module_alias2, printings = printings2 }) =
|
haftmann@28054
|
151 |
if serial1 = serial2 orelse not strict then
|
haftmann@37821
|
152 |
make_target ((serial1, description),
|
wenzelm@51783
|
153 |
((merge (op =) (reserved1, reserved2),
|
haftmann@53274
|
154 |
Symtab.join (K snd) (module_alias1, module_alias2)),
|
haftmann@53274
|
155 |
(Code_Symbol.merge_data (printings1, printings2))
|
haftmann@28054
|
156 |
))
|
haftmann@28054
|
157 |
else
|
haftmann@37821
|
158 |
error ("Incompatible targets: " ^ quote target);
|
haftmann@27436
|
159 |
|
haftmann@37821
|
160 |
fun the_description (Target { description, ... }) = description;
|
haftmann@28054
|
161 |
fun the_reserved (Target { reserved, ... }) = reserved;
|
haftmann@28054
|
162 |
fun the_module_alias (Target { module_alias , ... }) = module_alias;
|
haftmann@53274
|
163 |
fun the_printings (Target { printings, ... }) = printings;
|
haftmann@27436
|
164 |
|
haftmann@34243
|
165 |
structure Targets = Theory_Data
|
haftmann@34071
|
166 |
(
|
haftmann@34071
|
167 |
type T = (target Symtab.table * string list) * int;
|
haftmann@34071
|
168 |
val empty = ((Symtab.empty, []), 80);
|
haftmann@34071
|
169 |
val extend = I;
|
haftmann@34071
|
170 |
fun merge (((target1, exc1), width1), ((target2, exc2), width2)) : T =
|
haftmann@34071
|
171 |
((Symtab.join (merge_target true) (target1, target2),
|
haftmann@34071
|
172 |
Library.merge (op =) (exc1, exc2)), Int.max (width1, width2));
|
haftmann@34071
|
173 |
);
|
haftmann@28054
|
174 |
|
haftmann@34243
|
175 |
val abort_allowed = snd o fst o Targets.get;
|
haftmann@34071
|
176 |
|
haftmann@34243
|
177 |
fun assert_target thy target = if Symtab.defined ((fst o fst) (Targets.get thy)) target
|
haftmann@33968
|
178 |
then target
|
haftmann@33968
|
179 |
else error ("Unknown code target language: " ^ quote target);
|
haftmann@28054
|
180 |
|
haftmann@28054
|
181 |
fun put_target (target, seri) thy =
|
haftmann@27437
|
182 |
let
|
haftmann@34243
|
183 |
val lookup_target = Symtab.lookup ((fst o fst) (Targets.get thy));
|
haftmann@28054
|
184 |
val _ = case seri
|
haftmann@37821
|
185 |
of Extension (super, _) => if is_some (lookup_target super) then ()
|
haftmann@28054
|
186 |
else error ("Unknown code target language: " ^ quote super)
|
haftmann@28054
|
187 |
| _ => ();
|
haftmann@37821
|
188 |
val overwriting = case (Option.map the_description o lookup_target) target
|
haftmann@28663
|
189 |
of NONE => false
|
haftmann@37821
|
190 |
| SOME (Extension _) => true
|
haftmann@37821
|
191 |
| SOME (Fundamental _) => (case seri
|
haftmann@37821
|
192 |
of Extension _ => error ("Will not overwrite existing target " ^ quote target)
|
haftmann@28663
|
193 |
| _ => true);
|
haftmann@28663
|
194 |
val _ = if overwriting
|
haftmann@28054
|
195 |
then warning ("Overwriting existing target " ^ quote target)
|
wenzelm@44450
|
196 |
else ();
|
haftmann@27103
|
197 |
in
|
haftmann@28054
|
198 |
thy
|
haftmann@34243
|
199 |
|> (Targets.map o apfst o apfst o Symtab.update)
|
haftmann@31599
|
200 |
(target, make_target ((serial (), seri), (([], Symtab.empty),
|
haftmann@53274
|
201 |
Code_Symbol.empty_data)))
|
haftmann@27103
|
202 |
end;
|
haftmann@27000
|
203 |
|
haftmann@37821
|
204 |
fun add_target (target, seri) = put_target (target, Fundamental seri);
|
haftmann@28054
|
205 |
fun extend_target (target, (super, modify)) =
|
haftmann@37821
|
206 |
put_target (target, Extension (super, modify));
|
haftmann@27000
|
207 |
|
haftmann@28054
|
208 |
fun map_target_data target f thy =
|
haftmann@27304
|
209 |
let
|
haftmann@28054
|
210 |
val _ = assert_target thy target;
|
haftmann@27000
|
211 |
in
|
haftmann@28054
|
212 |
thy
|
haftmann@34243
|
213 |
|> (Targets.map o apfst o apfst o Symtab.map_entry target o map_target) f
|
haftmann@27000
|
214 |
end;
|
haftmann@27000
|
215 |
|
haftmann@28054
|
216 |
fun map_reserved target =
|
haftmann@28054
|
217 |
map_target_data target o apsnd o apfst o apfst;
|
haftmann@53274
|
218 |
fun map_module_alias target =
|
haftmann@28054
|
219 |
map_target_data target o apsnd o apfst o apsnd;
|
haftmann@53274
|
220 |
fun map_printings target =
|
haftmann@53274
|
221 |
map_target_data target o apsnd o apsnd;
|
haftmann@27000
|
222 |
|
haftmann@34243
|
223 |
fun set_default_code_width k = (Targets.map o apsnd) (K k);
|
haftmann@34071
|
224 |
|
haftmann@27000
|
225 |
|
haftmann@34021
|
226 |
(** serializer usage **)
|
haftmann@34021
|
227 |
|
haftmann@34021
|
228 |
(* montage *)
|
haftmann@34021
|
229 |
|
haftmann@37822
|
230 |
fun the_fundamental thy =
|
haftmann@34021
|
231 |
let
|
haftmann@34243
|
232 |
val ((targets, _), _) = Targets.get thy;
|
haftmann@37822
|
233 |
fun fundamental target = case Symtab.lookup targets target
|
haftmann@37821
|
234 |
of SOME data => (case the_description data
|
haftmann@37822
|
235 |
of Fundamental data => data
|
haftmann@37822
|
236 |
| Extension (super, _) => fundamental super)
|
haftmann@34021
|
237 |
| NONE => error ("Unknown code target language: " ^ quote target);
|
haftmann@37822
|
238 |
in fundamental end;
|
haftmann@37822
|
239 |
|
haftmann@37822
|
240 |
fun the_literals thy = #literals o the_fundamental thy;
|
haftmann@34021
|
241 |
|
haftmann@40059
|
242 |
fun collapse_hierarchy thy =
|
haftmann@39153
|
243 |
let
|
haftmann@40059
|
244 |
val ((targets, _), _) = Targets.get thy;
|
wenzelm@44450
|
245 |
fun collapse target =
|
haftmann@39153
|
246 |
let
|
haftmann@39153
|
247 |
val data = case Symtab.lookup targets target
|
haftmann@39153
|
248 |
of SOME data => data
|
haftmann@39153
|
249 |
| NONE => error ("Unknown code target language: " ^ quote target);
|
haftmann@39153
|
250 |
in case the_description data
|
haftmann@39724
|
251 |
of Fundamental _ => (K I, data)
|
haftmann@39153
|
252 |
| Extension (super, modify) => let
|
haftmann@40059
|
253 |
val (modify', data') = collapse super
|
haftmann@39724
|
254 |
in (fn naming => modify' naming #> modify naming, merge_target false target (data', data)) end
|
haftmann@39153
|
255 |
end;
|
haftmann@40059
|
256 |
in collapse end;
|
haftmann@40059
|
257 |
|
haftmann@40059
|
258 |
local
|
haftmann@40059
|
259 |
|
haftmann@40059
|
260 |
fun activate_target thy target =
|
haftmann@40059
|
261 |
let
|
haftmann@48447
|
262 |
val ((_, abortable), default_width) = Targets.get thy;
|
haftmann@40059
|
263 |
val (modify, data) = collapse_hierarchy thy target;
|
haftmann@39724
|
264 |
in (default_width, abortable, data, modify) end;
|
haftmann@39153
|
265 |
|
haftmann@53274
|
266 |
fun activate_const_syntax thy literals cs_data naming =
|
haftmann@53274
|
267 |
(Symtab.empty, naming)
|
haftmann@53274
|
268 |
|> fold_map (fn (c, data) => fn (tab, naming) =>
|
haftmann@53274
|
269 |
case Code_Thingol.lookup_const naming c
|
haftmann@53274
|
270 |
of SOME name => let
|
haftmann@53274
|
271 |
val (syn, naming') =
|
haftmann@53274
|
272 |
Code_Printer.activate_const_syntax thy literals c data naming
|
haftmann@53274
|
273 |
in (SOME name, (Symtab.update_new (name, syn) tab, naming')) end
|
haftmann@53274
|
274 |
| NONE => (NONE, (tab, naming))) cs_data
|
haftmann@34021
|
275 |
|>> map_filter I;
|
haftmann@34021
|
276 |
|
haftmann@53274
|
277 |
fun activate_syntax lookup_name things =
|
haftmann@53274
|
278 |
Symtab.empty
|
haftmann@53274
|
279 |
|> fold_map (fn (thing_identifier, data) => fn tab => case lookup_name thing_identifier
|
haftmann@53274
|
280 |
of SOME name => (SOME name, Symtab.update_new (name, data) tab)
|
haftmann@53274
|
281 |
| NONE => (NONE, tab)) things
|
haftmann@34021
|
282 |
|>> map_filter I;
|
haftmann@34021
|
283 |
|
haftmann@53274
|
284 |
fun activate_symbol_syntax thy literals naming printings =
|
haftmann@34021
|
285 |
let
|
haftmann@53274
|
286 |
val (names_const, (const_syntax, _)) =
|
haftmann@53274
|
287 |
activate_const_syntax thy literals (Code_Symbol.dest_constant_data printings) naming;
|
haftmann@53274
|
288 |
val (names_tyco, tyco_syntax) =
|
haftmann@53274
|
289 |
activate_syntax (Code_Thingol.lookup_tyco naming) (Code_Symbol.dest_type_constructor_data printings);
|
haftmann@53274
|
290 |
val (names_class, class_syntax) =
|
haftmann@53274
|
291 |
activate_syntax (Code_Thingol.lookup_class naming) (Code_Symbol.dest_type_class_data printings);
|
haftmann@53274
|
292 |
val names_inst = map_filter (Code_Thingol.lookup_instance naming o fst)
|
haftmann@53274
|
293 |
(Code_Symbol.dest_class_instance_data printings);
|
haftmann@39153
|
294 |
in
|
haftmann@53274
|
295 |
(names_const @ names_tyco @ names_class @ names_inst,
|
haftmann@53274
|
296 |
(const_syntax, tyco_syntax, class_syntax))
|
haftmann@39153
|
297 |
end;
|
haftmann@39153
|
298 |
|
haftmann@39153
|
299 |
fun project_program thy abortable names_hidden names1 program2 =
|
haftmann@39153
|
300 |
let
|
wenzelm@43232
|
301 |
val ctxt = Proof_Context.init_global thy;
|
haftmann@34021
|
302 |
val names2 = subtract (op =) names_hidden names1;
|
wenzelm@47485
|
303 |
val program3 = Graph.restrict (not o member (op =) names_hidden) program2;
|
haftmann@39162
|
304 |
val names4 = Graph.all_succs program3 names2;
|
haftmann@34021
|
305 |
val empty_funs = filter_out (member (op =) abortable)
|
haftmann@34021
|
306 |
(Code_Thingol.empty_funs program3);
|
wenzelm@43230
|
307 |
val _ =
|
wenzelm@43230
|
308 |
if null empty_funs then ()
|
wenzelm@43230
|
309 |
else error ("No code equations for " ^
|
wenzelm@43232
|
310 |
commas (map (Proof_Context.extern_const ctxt) empty_funs));
|
wenzelm@47485
|
311 |
val program4 = Graph.restrict (member (op =) names4) program3;
|
haftmann@39162
|
312 |
in (names4, program4) end;
|
haftmann@39153
|
313 |
|
haftmann@53274
|
314 |
fun prepare_serializer thy abortable (serializer : serializer) literals reserved module_alias
|
haftmann@53274
|
315 |
printings module_name args naming proto_program names =
|
haftmann@39153
|
316 |
let
|
haftmann@53274
|
317 |
val (names_hidden, (const_syntax, tyco_syntax, class_syntax)) =
|
haftmann@53274
|
318 |
activate_symbol_syntax thy literals naming printings;
|
haftmann@39153
|
319 |
val (names_all, program) = project_program thy abortable names_hidden names proto_program;
|
haftmann@39724
|
320 |
fun select_include (name, (content, cs)) =
|
haftmann@39724
|
321 |
if null cs orelse exists (fn c => case Code_Thingol.lookup_const naming c
|
haftmann@39724
|
322 |
of SOME name => member (op =) names_all name
|
haftmann@39724
|
323 |
| NONE => false) cs
|
haftmann@39724
|
324 |
then SOME (name, content) else NONE;
|
haftmann@53274
|
325 |
val includes = map_filter select_include (Code_Symbol.dest_module_data printings);
|
haftmann@34021
|
326 |
in
|
haftmann@41590
|
327 |
(serializer args {
|
haftmann@39153
|
328 |
labelled_name = Code_Thingol.labelled_name thy proto_program,
|
haftmann@39152
|
329 |
reserved_syms = reserved,
|
haftmann@39152
|
330 |
includes = includes,
|
haftmann@39159
|
331 |
module_alias = if module_name = "" then Symtab.lookup module_alias else K (SOME module_name),
|
haftmann@53274
|
332 |
const_syntax = Symtab.lookup const_syntax,
|
haftmann@39153
|
333 |
tyco_syntax = Symtab.lookup tyco_syntax,
|
haftmann@53274
|
334 |
class_syntax = Symtab.lookup class_syntax },
|
haftmann@41590
|
335 |
program)
|
haftmann@34021
|
336 |
end;
|
haftmann@34021
|
337 |
|
haftmann@41590
|
338 |
fun mount_serializer thy target some_width module_name args naming program names =
|
haftmann@34021
|
339 |
let
|
haftmann@39724
|
340 |
val (default_width, abortable, data, modify) = activate_target thy target;
|
haftmann@39147
|
341 |
val serializer = case the_description data
|
haftmann@39153
|
342 |
of Fundamental seri => #serializer seri;
|
haftmann@34021
|
343 |
val reserved = the_reserved data;
|
wenzelm@44450
|
344 |
val module_alias = the_module_alias data
|
haftmann@34021
|
345 |
val literals = the_literals thy target;
|
haftmann@41590
|
346 |
val (prepared_serializer, prepared_program) = prepare_serializer thy
|
haftmann@53274
|
347 |
abortable serializer literals reserved module_alias (the_printings data)
|
haftmann@53274
|
348 |
module_name args naming (modify naming program) names
|
haftmann@34071
|
349 |
val width = the_default default_width some_width;
|
haftmann@41592
|
350 |
in (fn program => prepared_serializer program width, prepared_program) end;
|
haftmann@41592
|
351 |
|
haftmann@41592
|
352 |
fun invoke_serializer thy target some_width module_name args naming program names =
|
haftmann@41592
|
353 |
let
|
haftmann@41592
|
354 |
val (mounted_serializer, prepared_program) = mount_serializer thy
|
haftmann@41592
|
355 |
target some_width module_name args naming program names;
|
haftmann@41592
|
356 |
in mounted_serializer prepared_program end;
|
haftmann@34021
|
357 |
|
haftmann@39159
|
358 |
fun assert_module_name "" = error ("Empty module name not allowed.")
|
haftmann@39159
|
359 |
| assert_module_name module_name = module_name;
|
haftmann@39159
|
360 |
|
haftmann@49441
|
361 |
fun using_master_directory thy =
|
haftmann@49441
|
362 |
Option.map (Path.append (File.pwd ()) o Path.append (Thy_Load.master_directory thy));
|
haftmann@49386
|
363 |
|
haftmann@34021
|
364 |
in
|
haftmann@34021
|
365 |
|
haftmann@49583
|
366 |
val generatedN = "Generated_Code";
|
haftmann@49583
|
367 |
|
haftmann@39724
|
368 |
fun export_code_for thy some_path target some_width module_name args =
|
haftmann@49386
|
369 |
export (using_master_directory thy some_path)
|
haftmann@49386
|
370 |
ooo invoke_serializer thy target some_width module_name args;
|
haftmann@39144
|
371 |
|
haftmann@39724
|
372 |
fun produce_code_for thy target some_width module_name args =
|
haftmann@39336
|
373 |
let
|
haftmann@41592
|
374 |
val serializer = invoke_serializer thy target some_width (assert_module_name module_name) args;
|
haftmann@39724
|
375 |
in fn naming => fn program => fn names =>
|
haftmann@39885
|
376 |
produce (serializer naming program names) |> apsnd (fn deresolve => map deresolve names)
|
haftmann@39724
|
377 |
end;
|
haftmann@39155
|
378 |
|
haftmann@39724
|
379 |
fun present_code_for thy target some_width module_name args =
|
haftmann@39724
|
380 |
let
|
haftmann@41592
|
381 |
val serializer = invoke_serializer thy target some_width (assert_module_name module_name) args;
|
haftmann@39724
|
382 |
in fn naming => fn program => fn (names, selects) =>
|
haftmann@39885
|
383 |
present selects (serializer naming program names)
|
haftmann@39724
|
384 |
end;
|
haftmann@39144
|
385 |
|
haftmann@39144
|
386 |
fun check_code_for thy target strict args naming program names_cs =
|
haftmann@37824
|
387 |
let
|
haftmann@37824
|
388 |
val { env_var, make_destination, make_command } =
|
haftmann@37824
|
389 |
(#check o the_fundamental thy) target;
|
wenzelm@42810
|
390 |
fun ext_check p =
|
wenzelm@44450
|
391 |
let
|
haftmann@37824
|
392 |
val destination = make_destination p;
|
haftmann@41592
|
393 |
val _ = export (SOME destination) (invoke_serializer thy target (SOME 80)
|
haftmann@49583
|
394 |
generatedN args naming program names_cs);
|
haftmann@49583
|
395 |
val cmd = make_command generatedN;
|
wenzelm@44721
|
396 |
in
|
wenzelm@44721
|
397 |
if Isabelle_System.bash ("cd " ^ File.shell_path p ^ " && " ^ cmd ^ " 2>&1") <> 0
|
haftmann@37824
|
398 |
then error ("Code check failed for " ^ target ^ ": " ^ cmd)
|
haftmann@37824
|
399 |
else ()
|
haftmann@37824
|
400 |
end;
|
wenzelm@44721
|
401 |
in
|
wenzelm@44721
|
402 |
if getenv env_var = ""
|
haftmann@37825
|
403 |
then if strict
|
haftmann@37825
|
404 |
then error (env_var ^ " not set; cannot check code for " ^ target)
|
haftmann@37825
|
405 |
else warning (env_var ^ " not set; skipped checking code for " ^ target)
|
wenzelm@42810
|
406 |
else Isabelle_System.with_tmp_dir "Code_Test" ext_check
|
haftmann@37824
|
407 |
end;
|
haftmann@37824
|
408 |
|
haftmann@41612
|
409 |
fun evaluation mounted_serializer prepared_program consts ((vs, ty), t) =
|
haftmann@41592
|
410 |
let
|
haftmann@41592
|
411 |
val _ = if Code_Thingol.contains_dict_var t then
|
haftmann@41592
|
412 |
error "Term to be evaluated contains free dictionaries" else ();
|
wenzelm@44206
|
413 |
val v' = singleton (Name.variant_list (map fst vs)) "a";
|
haftmann@41592
|
414 |
val vs' = (v', []) :: vs;
|
haftmann@41592
|
415 |
val ty' = Code_Thingol.fun_tyco `%% [ITyVar v', ty];
|
haftmann@41592
|
416 |
val value_name = "Value.value.value"
|
haftmann@41592
|
417 |
val program = prepared_program
|
haftmann@41592
|
418 |
|> Graph.new_node (value_name,
|
haftmann@41592
|
419 |
Code_Thingol.Fun (Term.dummy_patternN, (((vs', ty'), [(([IVar NONE], t), (NONE, true))]), NONE)))
|
haftmann@41612
|
420 |
|> fold (curry (perhaps o try o Graph.add_edge) value_name) consts;
|
haftmann@41592
|
421 |
val (program_code, deresolve) = produce (mounted_serializer program);
|
haftmann@41592
|
422 |
val value_name' = the (deresolve value_name);
|
haftmann@41592
|
423 |
in (program_code, value_name') end;
|
haftmann@41592
|
424 |
|
haftmann@41612
|
425 |
fun evaluator thy target naming program consts =
|
haftmann@41592
|
426 |
let
|
haftmann@41592
|
427 |
val (mounted_serializer, prepared_program) = mount_serializer thy
|
haftmann@49583
|
428 |
target NONE generatedN [] naming program consts;
|
haftmann@41612
|
429 |
in evaluation mounted_serializer prepared_program consts end;
|
haftmann@41592
|
430 |
|
haftmann@34021
|
431 |
end; (* local *)
|
haftmann@34021
|
432 |
|
haftmann@34021
|
433 |
|
haftmann@34021
|
434 |
(* code generation *)
|
haftmann@34021
|
435 |
|
haftmann@34021
|
436 |
fun transitivly_non_empty_funs thy naming program =
|
haftmann@34021
|
437 |
let
|
haftmann@34021
|
438 |
val cs = subtract (op =) (abort_allowed thy) (Code_Thingol.empty_funs program);
|
haftmann@34021
|
439 |
val names = map_filter (Code_Thingol.lookup_const naming) cs;
|
haftmann@34021
|
440 |
in subtract (op =) (Graph.all_preds program names) (Graph.keys program) end;
|
haftmann@34021
|
441 |
|
haftmann@34021
|
442 |
fun read_const_exprs thy cs =
|
haftmann@34021
|
443 |
let
|
haftmann@34021
|
444 |
val (cs1, cs2) = Code_Thingol.read_const_exprs thy cs;
|
haftmann@36271
|
445 |
val (names2, (naming, program)) = Code_Thingol.consts_program thy true cs2;
|
haftmann@36271
|
446 |
val names3 = transitivly_non_empty_funs thy naming program;
|
haftmann@36271
|
447 |
val cs3 = map_filter (fn (c, name) =>
|
haftmann@36271
|
448 |
if member (op =) names3 name then SOME c else NONE) (cs2 ~~ names2);
|
haftmann@36271
|
449 |
in union (op =) cs3 cs1 end;
|
haftmann@34021
|
450 |
|
haftmann@39144
|
451 |
fun prep_destination "" = NONE
|
haftmann@39144
|
452 |
| prep_destination "-" = NONE
|
haftmann@39144
|
453 |
| prep_destination s = SOME (Path.explode s);
|
haftmann@39144
|
454 |
|
haftmann@34021
|
455 |
fun export_code thy cs seris =
|
haftmann@34021
|
456 |
let
|
haftmann@37824
|
457 |
val (names_cs, (naming, program)) = Code_Thingol.consts_program thy false cs;
|
haftmann@39144
|
458 |
val _ = map (fn (((target, module_name), some_path), args) =>
|
haftmann@39144
|
459 |
export_code_for thy some_path target NONE module_name args naming program names_cs) seris;
|
haftmann@34021
|
460 |
in () end;
|
haftmann@34021
|
461 |
|
haftmann@39144
|
462 |
fun export_code_cmd raw_cs seris thy = export_code thy (read_const_exprs thy raw_cs)
|
haftmann@39144
|
463 |
((map o apfst o apsnd) prep_destination seris);
|
haftmann@39144
|
464 |
|
haftmann@39155
|
465 |
fun produce_code thy cs target some_width some_module_name args =
|
haftmann@39144
|
466 |
let
|
haftmann@39144
|
467 |
val (names_cs, (naming, program)) = Code_Thingol.consts_program thy false cs;
|
haftmann@39155
|
468 |
in produce_code_for thy target some_width some_module_name args naming program names_cs end;
|
haftmann@39155
|
469 |
|
haftmann@39155
|
470 |
fun present_code thy cs names_stmt target some_width some_module_name args =
|
haftmann@39155
|
471 |
let
|
haftmann@39155
|
472 |
val (names_cs, (naming, program)) = Code_Thingol.consts_program thy false cs;
|
haftmann@39155
|
473 |
in present_code_for thy target some_width some_module_name args naming program (names_cs, names_stmt naming) end;
|
haftmann@34021
|
474 |
|
haftmann@37824
|
475 |
fun check_code thy cs seris =
|
haftmann@37824
|
476 |
let
|
haftmann@37824
|
477 |
val (names_cs, (naming, program)) = Code_Thingol.consts_program thy false cs;
|
haftmann@39144
|
478 |
val _ = map (fn ((target, strict), args) =>
|
haftmann@39144
|
479 |
check_code_for thy target strict args naming program names_cs) seris;
|
haftmann@37824
|
480 |
in () end;
|
haftmann@37824
|
481 |
|
haftmann@37824
|
482 |
fun check_code_cmd raw_cs seris thy = check_code thy (read_const_exprs thy raw_cs) seris;
|
haftmann@37824
|
483 |
|
haftmann@39720
|
484 |
local
|
haftmann@39720
|
485 |
|
haftmann@39720
|
486 |
val parse_const_terms = Scan.repeat1 Args.term
|
haftmann@39720
|
487 |
>> (fn ts => fn thy => map (Code.check_const thy) ts);
|
haftmann@39720
|
488 |
|
haftmann@39720
|
489 |
fun parse_names category parse internalize lookup =
|
haftmann@39720
|
490 |
Scan.lift (Args.parens (Args.$$$ category)) |-- Scan.repeat1 parse
|
haftmann@39720
|
491 |
>> (fn xs => fn thy => fn naming => map_filter (lookup naming o internalize thy) xs);
|
wenzelm@44450
|
492 |
|
haftmann@39720
|
493 |
val parse_consts = parse_names "consts" Args.term
|
haftmann@39720
|
494 |
Code.check_const Code_Thingol.lookup_const ;
|
haftmann@39720
|
495 |
|
haftmann@39720
|
496 |
val parse_types = parse_names "types" (Scan.lift Args.name)
|
haftmann@39720
|
497 |
Sign.intern_type Code_Thingol.lookup_tyco;
|
haftmann@39720
|
498 |
|
haftmann@39720
|
499 |
val parse_classes = parse_names "classes" (Scan.lift Args.name)
|
haftmann@39720
|
500 |
Sign.intern_class Code_Thingol.lookup_class;
|
haftmann@39720
|
501 |
|
haftmann@39720
|
502 |
val parse_instances = parse_names "instances" (Scan.lift (Args.name --| Args.$$$ "::" -- Args.name))
|
haftmann@39720
|
503 |
(fn thy => fn (raw_tyco, raw_class) => (Sign.intern_class thy raw_class, Sign.intern_type thy raw_tyco))
|
haftmann@39720
|
504 |
Code_Thingol.lookup_instance;
|
haftmann@39720
|
505 |
|
haftmann@39720
|
506 |
in
|
haftmann@39720
|
507 |
|
wenzelm@44450
|
508 |
val antiq_setup =
|
wenzelm@44450
|
509 |
Thy_Output.antiquotation @{binding code_stmts}
|
wenzelm@44450
|
510 |
(parse_const_terms --
|
wenzelm@44450
|
511 |
Scan.repeat (parse_consts || parse_types || parse_classes || parse_instances)
|
wenzelm@44450
|
512 |
-- Scan.lift (Args.parens (Args.name -- Scan.option Parse.int)))
|
wenzelm@44450
|
513 |
(fn {context = ctxt, ...} => fn ((mk_cs, mk_stmtss), (target, some_width)) =>
|
wenzelm@44450
|
514 |
let val thy = Proof_Context.theory_of ctxt in
|
wenzelm@44450
|
515 |
present_code thy (mk_cs thy)
|
wenzelm@44450
|
516 |
(fn naming => maps (fn f => f thy naming) mk_stmtss)
|
wenzelm@44450
|
517 |
target some_width "Example" []
|
wenzelm@44450
|
518 |
end);
|
haftmann@39720
|
519 |
|
haftmann@39720
|
520 |
end;
|
haftmann@39720
|
521 |
|
haftmann@34021
|
522 |
|
haftmann@28054
|
523 |
(** serializer configuration **)
|
haftmann@28054
|
524 |
|
haftmann@53274
|
525 |
(* checking and parsing *)
|
haftmann@24219
|
526 |
|
haftmann@53274
|
527 |
fun cert_const thy const =
|
haftmann@24992
|
528 |
let
|
haftmann@53274
|
529 |
val _ = if Sign.declared_const thy const then ()
|
haftmann@53274
|
530 |
else error ("No such constant: " ^ quote const);
|
haftmann@53274
|
531 |
in const end;
|
haftmann@24992
|
532 |
|
haftmann@24992
|
533 |
fun cert_tyco thy tyco =
|
haftmann@24992
|
534 |
let
|
haftmann@24992
|
535 |
val _ = if Sign.declared_tyname thy tyco then ()
|
haftmann@24992
|
536 |
else error ("No such type constructor: " ^ quote tyco);
|
haftmann@24992
|
537 |
in tyco end;
|
haftmann@24992
|
538 |
|
haftmann@27103
|
539 |
fun read_tyco thy = cert_tyco thy o Sign.intern_type thy;
|
haftmann@24219
|
540 |
|
haftmann@53274
|
541 |
fun cert_class thy class =
|
haftmann@53274
|
542 |
let
|
haftmann@53274
|
543 |
val _ = Axclass.get_info thy class;
|
haftmann@53274
|
544 |
in class end;
|
haftmann@53274
|
545 |
|
haftmann@53274
|
546 |
fun read_class thy = cert_class thy o Sign.intern_class thy;
|
haftmann@53274
|
547 |
|
haftmann@53274
|
548 |
val parse_classrel_ident = Parse.class --| @{keyword "<"} -- Parse.class;
|
haftmann@53274
|
549 |
|
haftmann@34071
|
550 |
fun cert_inst thy (class, tyco) =
|
haftmann@34071
|
551 |
(cert_class thy class, cert_tyco thy tyco);
|
haftmann@34071
|
552 |
|
haftmann@34071
|
553 |
fun read_inst thy (raw_tyco, raw_class) =
|
haftmann@34071
|
554 |
(read_class thy raw_class, read_tyco thy raw_tyco);
|
haftmann@34071
|
555 |
|
haftmann@53274
|
556 |
val parse_inst_ident = Parse.xname --| @{keyword "::"} -- Parse.class;
|
haftmann@53274
|
557 |
|
haftmann@53274
|
558 |
fun cert_syms thy =
|
haftmann@53274
|
559 |
Code_Symbol.map_attr (apfst (cert_const thy)) (apfst (cert_tyco thy))
|
haftmann@53274
|
560 |
(apfst (cert_class thy)) ((apfst o pairself) (cert_class thy)) (apfst (cert_inst thy)) I;
|
haftmann@53274
|
561 |
|
haftmann@53274
|
562 |
fun read_syms thy =
|
haftmann@53274
|
563 |
Code_Symbol.map_attr (apfst (Code.read_const thy)) (apfst (read_tyco thy))
|
haftmann@53274
|
564 |
(apfst (read_class thy)) ((apfst o pairself) (read_class thy)) (apfst (read_inst thy)) I;
|
haftmann@53274
|
565 |
|
haftmann@53274
|
566 |
fun check_const_syntax thy c syn =
|
haftmann@53274
|
567 |
if Code_Printer.requires_args syn > Code.args_number thy c
|
haftmann@53274
|
568 |
then error ("Too many arguments in syntax for constant " ^ quote c)
|
haftmann@53274
|
569 |
else syn;
|
haftmann@53274
|
570 |
|
haftmann@53274
|
571 |
fun check_tyco_syntax thy tyco syn =
|
haftmann@53274
|
572 |
if fst syn <> Sign.arity_number thy tyco
|
haftmann@53274
|
573 |
then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco)
|
haftmann@53274
|
574 |
else syn;
|
haftmann@53274
|
575 |
|
haftmann@53274
|
576 |
fun arrange_printings prep_const thy =
|
haftmann@24219
|
577 |
let
|
haftmann@53274
|
578 |
fun arrange check (sym, target_syns) =
|
haftmann@53274
|
579 |
map (fn (target, some_syn) => (target, (sym, Option.map (check thy sym) some_syn))) target_syns;
|
haftmann@53274
|
580 |
in
|
haftmann@53274
|
581 |
Code_Symbol.maps_attr'
|
haftmann@53274
|
582 |
(arrange check_const_syntax) (arrange check_tyco_syntax)
|
haftmann@53274
|
583 |
(arrange ((K o K) I)) (arrange ((K o K) I)) (arrange ((K o K) I))
|
haftmann@53274
|
584 |
(arrange (fn thy => fn _ => fn (raw_content, raw_cs) =>
|
haftmann@53274
|
585 |
(Code_Printer.str raw_content, map (prep_const thy) raw_cs)))
|
haftmann@53274
|
586 |
end;
|
haftmann@24219
|
587 |
|
haftmann@53274
|
588 |
fun cert_printings thy = cert_syms thy #> arrange_printings cert_const thy;
|
haftmann@24219
|
589 |
|
haftmann@53274
|
590 |
fun read_printings thy = read_syms thy #> arrange_printings Code.read_const thy;
|
haftmann@34071
|
591 |
|
haftmann@24219
|
592 |
|
haftmann@53274
|
593 |
(* custom symbol names *)
|
haftmann@53274
|
594 |
|
haftmann@53274
|
595 |
fun add_module_alias target (thyname, "") =
|
haftmann@53274
|
596 |
map_module_alias target (Symtab.delete thyname)
|
haftmann@53274
|
597 |
| add_module_alias target (thyname, modlname) =
|
haftmann@53274
|
598 |
let
|
haftmann@53274
|
599 |
val xs = Long_Name.explode modlname;
|
haftmann@53274
|
600 |
val xs' = map (Name.desymbolize true) xs;
|
haftmann@53274
|
601 |
in if xs' = xs
|
haftmann@53274
|
602 |
then map_module_alias target (Symtab.update (thyname, modlname))
|
haftmann@53274
|
603 |
else error ("Invalid module name: " ^ quote modlname ^ "\n"
|
haftmann@53274
|
604 |
^ "perhaps try " ^ quote (Long_Name.implode xs'))
|
haftmann@53274
|
605 |
end;
|
haftmann@24219
|
606 |
|
haftmann@40059
|
607 |
fun add_reserved target sym thy =
|
haftmann@24219
|
608 |
let
|
haftmann@40059
|
609 |
val (_, data) = collapse_hierarchy thy target;
|
haftmann@40059
|
610 |
val _ = if member (op =) (the_reserved data) sym
|
haftmann@24219
|
611 |
then error ("Reserved symbol " ^ quote sym ^ " already declared")
|
haftmann@40059
|
612 |
else ();
|
haftmann@40059
|
613 |
in
|
haftmann@40059
|
614 |
thy
|
haftmann@40059
|
615 |
|> map_reserved target (insert (op =) sym)
|
haftmann@40059
|
616 |
end;
|
haftmann@24992
|
617 |
|
haftmann@53274
|
618 |
|
haftmann@53274
|
619 |
(* custom printings *)
|
haftmann@53274
|
620 |
|
haftmann@53274
|
621 |
fun set_printing (target, sym_syn) = map_printings target (Code_Symbol.set_data sym_syn);
|
haftmann@53274
|
622 |
|
haftmann@53274
|
623 |
fun gen_set_printings prep_print_decl raw_print_decls thy =
|
haftmann@53274
|
624 |
fold set_printing (prep_print_decl thy raw_print_decls) thy;
|
haftmann@53274
|
625 |
|
haftmann@53274
|
626 |
val set_printings = gen_set_printings cert_printings;
|
haftmann@53274
|
627 |
val set_printings_cmd = gen_set_printings read_printings;
|
haftmann@53274
|
628 |
|
haftmann@53274
|
629 |
fun gen_add_syntax Symbol prep_x prep_syn target raw_x some_raw_syn thy =
|
haftmann@24992
|
630 |
let
|
haftmann@53274
|
631 |
val x = prep_x thy raw_x;
|
haftmann@53274
|
632 |
in set_printing (target, Symbol (x, Option.map (prep_syn thy x) some_raw_syn)) thy end;
|
haftmann@28926
|
633 |
|
haftmann@53274
|
634 |
fun gen_add_const_syntax prep_const =
|
haftmann@53274
|
635 |
gen_add_syntax Code_Symbol.Constant prep_const check_const_syntax;
|
haftmann@24219
|
636 |
|
haftmann@53274
|
637 |
fun gen_add_tyco_syntax prep_tyco =
|
haftmann@53274
|
638 |
gen_add_syntax Code_Symbol.Type_Constructor prep_tyco check_tyco_syntax;
|
haftmann@53274
|
639 |
|
haftmann@53274
|
640 |
fun gen_add_class_syntax prep_class =
|
haftmann@53274
|
641 |
gen_add_syntax Code_Symbol.Type_Class prep_class ((K o K) I);
|
haftmann@53274
|
642 |
|
haftmann@53274
|
643 |
fun gen_add_instance_syntax prep_inst =
|
haftmann@53274
|
644 |
gen_add_syntax Code_Symbol.Class_Instance prep_inst ((K o K) I);
|
haftmann@53274
|
645 |
|
haftmann@53274
|
646 |
fun gen_add_include prep_const target (name, some_content) thy =
|
haftmann@53274
|
647 |
gen_add_syntax Code_Symbol.Module (K I)
|
haftmann@53274
|
648 |
(fn thy => fn _ => fn (raw_content, raw_cs) => (Code_Printer.str raw_content, map (prep_const thy) raw_cs))
|
haftmann@53274
|
649 |
target name some_content thy;
|
haftmann@53274
|
650 |
|
haftmann@53274
|
651 |
|
haftmann@53274
|
652 |
(* abortable constants *)
|
haftmann@24219
|
653 |
|
haftmann@28663
|
654 |
fun gen_allow_abort prep_const raw_c thy =
|
haftmann@24841
|
655 |
let
|
haftmann@28663
|
656 |
val c = prep_const thy raw_c;
|
haftmann@34243
|
657 |
in thy |> (Targets.map o apfst o apsnd) (insert (op =) c) end;
|
haftmann@24841
|
658 |
|
haftmann@27000
|
659 |
|
haftmann@27103
|
660 |
(* concrete syntax *)
|
haftmann@27000
|
661 |
|
haftmann@34021
|
662 |
local
|
haftmann@34021
|
663 |
|
haftmann@48447
|
664 |
fun zip_list (x :: xs) f g =
|
haftmann@34141
|
665 |
f
|
haftmann@37854
|
666 |
:|-- (fn y =>
|
haftmann@34141
|
667 |
fold_map (fn x => g |-- f >> pair x) xs
|
haftmann@37854
|
668 |
:|-- (fn xys => pair ((x, y) :: xys)));
|
haftmann@34141
|
669 |
|
haftmann@37854
|
670 |
fun process_multi_syntax parse_thing parse_syntax change =
|
haftmann@37854
|
671 |
(Parse.and_list1 parse_thing
|
wenzelm@47823
|
672 |
:|-- (fn things => Scan.repeat1 (@{keyword "("} |-- Parse.name --
|
haftmann@53205
|
673 |
(zip_list things (Scan.option parse_syntax) @{keyword "and"}) --| @{keyword ")"})))
|
haftmann@37854
|
674 |
>> (Toplevel.theory oo fold)
|
haftmann@37854
|
675 |
(fn (target, syns) => fold (fn (raw_x, syn) => change target raw_x syn) syns);
|
haftmann@24219
|
676 |
|
haftmann@24219
|
677 |
in
|
haftmann@24219
|
678 |
|
haftmann@53274
|
679 |
val add_reserved = add_reserved;
|
haftmann@53274
|
680 |
val add_const_syntax = gen_add_const_syntax (K I);
|
haftmann@53274
|
681 |
val add_tyco_syntax = gen_add_tyco_syntax cert_tyco;
|
haftmann@39149
|
682 |
val add_class_syntax = gen_add_class_syntax cert_class;
|
haftmann@39149
|
683 |
val add_instance_syntax = gen_add_instance_syntax cert_inst;
|
haftmann@53274
|
684 |
val add_include = gen_add_include (K I);
|
haftmann@27103
|
685 |
val allow_abort = gen_allow_abort (K I);
|
haftmann@24219
|
686 |
|
haftmann@53274
|
687 |
val add_const_syntax_cmd = gen_add_const_syntax Code.read_const;
|
haftmann@53274
|
688 |
val add_tyco_syntax_cmd = gen_add_tyco_syntax read_tyco;
|
haftmann@39149
|
689 |
val add_class_syntax_cmd = gen_add_class_syntax read_class;
|
haftmann@39149
|
690 |
val add_instance_syntax_cmd = gen_add_instance_syntax read_inst;
|
haftmann@53274
|
691 |
val add_include_cmd = gen_add_include Code.read_const;
|
haftmann@31156
|
692 |
val allow_abort_cmd = gen_allow_abort Code.read_const;
|
haftmann@24219
|
693 |
|
haftmann@28054
|
694 |
fun parse_args f args =
|
wenzelm@36969
|
695 |
case Scan.read Token.stopper f args
|
haftmann@28054
|
696 |
of SOME x => x
|
haftmann@28054
|
697 |
| NONE => error "Bad serializer arguments";
|
haftmann@28054
|
698 |
|
haftmann@28054
|
699 |
|
haftmann@27304
|
700 |
(** Isar setup **)
|
haftmann@27103
|
701 |
|
wenzelm@47823
|
702 |
val code_expr_argsP = Scan.optional (@{keyword "("} |-- Args.parse --| @{keyword ")"}) [];
|
haftmann@27103
|
703 |
|
haftmann@30492
|
704 |
val code_exprP =
|
haftmann@37824
|
705 |
Scan.repeat1 Parse.term_group :|-- (fn raw_cs =>
|
wenzelm@47823
|
706 |
((@{keyword "checking"} |-- Scan.repeat (Parse.name
|
wenzelm@47823
|
707 |
-- ((@{keyword "?"} |-- Scan.succeed false) || Scan.succeed true) -- code_expr_argsP))
|
haftmann@37824
|
708 |
>> (fn seris => check_code_cmd raw_cs seris)
|
wenzelm@47823
|
709 |
|| Scan.repeat (@{keyword "in"} |-- Parse.name
|
wenzelm@47823
|
710 |
-- Scan.optional (@{keyword "module_name"} |-- Parse.name) ""
|
wenzelm@47823
|
711 |
-- Scan.optional (@{keyword "file"} |-- Parse.name) ""
|
haftmann@37824
|
712 |
-- code_expr_argsP) >> (fn seris => export_code_cmd raw_cs seris)));
|
haftmann@27103
|
713 |
|
haftmann@53274
|
714 |
fun parse_single_symbol_pragma parse_keyword parse_isa parse_target =
|
haftmann@53274
|
715 |
parse_keyword |-- Parse.!!! parse_isa --| (@{keyword "\<rightharpoonup>"} || @{keyword "=>"})
|
haftmann@53274
|
716 |
-- Parse.and_list1 (@{keyword "("} |-- (Parse.name --| @{keyword ")"} -- Scan.option parse_target));
|
haftmann@53274
|
717 |
|
haftmann@53274
|
718 |
fun parse_symbol_pragma parse_const parse_tyco parse_class parse_classrel parse_inst parse_module =
|
haftmann@53274
|
719 |
parse_single_symbol_pragma @{keyword "constant"} Parse.term_group parse_const
|
haftmann@53274
|
720 |
>> Code_Symbol.Constant
|
haftmann@53274
|
721 |
|| parse_single_symbol_pragma @{keyword "type_constructor"} Parse.xname parse_tyco
|
haftmann@53274
|
722 |
>> Code_Symbol.Type_Constructor
|
haftmann@53274
|
723 |
|| parse_single_symbol_pragma @{keyword "type_class"} Parse.xname parse_class
|
haftmann@53274
|
724 |
>> Code_Symbol.Type_Class
|
haftmann@53274
|
725 |
|| parse_single_symbol_pragma @{keyword "class_relation"} parse_classrel_ident parse_classrel
|
haftmann@53274
|
726 |
>> Code_Symbol.Class_Relation
|
haftmann@53274
|
727 |
|| parse_single_symbol_pragma @{keyword "class_instance"} parse_inst_ident parse_inst
|
haftmann@53274
|
728 |
>> Code_Symbol.Class_Instance
|
haftmann@53274
|
729 |
|| parse_single_symbol_pragma @{keyword "code_module"} Parse.name parse_module
|
haftmann@53274
|
730 |
>> Code_Symbol.Module;
|
haftmann@53274
|
731 |
|
haftmann@53274
|
732 |
fun parse_symbol_pragmas parse_const parse_tyco parse_class parse_classrel parse_inst parse_module =
|
haftmann@53274
|
733 |
Parse.enum1 "|" (Parse.group (fn () => "code symbol pragma")
|
haftmann@53274
|
734 |
(parse_symbol_pragma parse_const parse_tyco parse_class parse_classrel parse_inst parse_module));
|
haftmann@53274
|
735 |
|
haftmann@53274
|
736 |
val _ =
|
haftmann@53274
|
737 |
Outer_Syntax.command @{command_spec "code_printing"} "declare dedicated printing for code symbols"
|
haftmann@53274
|
738 |
(parse_symbol_pragmas (Code_Printer.parse_const_syntax) (Code_Printer.parse_tyco_syntax)
|
haftmann@53274
|
739 |
Parse.string (Parse.minus >> K ()) (Parse.minus >> K ())
|
haftmann@53274
|
740 |
(Parse.text -- Scan.optional (@{keyword "attach"} |-- Scan.repeat1 Parse.term) [])
|
haftmann@53274
|
741 |
>> (Toplevel.theory o fold set_printings_cmd));
|
haftmann@53274
|
742 |
|
haftmann@53274
|
743 |
val _ =
|
haftmann@53274
|
744 |
Outer_Syntax.command @{command_spec "code_const"} "define code syntax for constant"
|
haftmann@53274
|
745 |
(process_multi_syntax Parse.term_group Code_Printer.parse_const_syntax
|
haftmann@53274
|
746 |
add_const_syntax_cmd);
|
haftmann@53274
|
747 |
|
haftmann@53274
|
748 |
val _ =
|
haftmann@53274
|
749 |
Outer_Syntax.command @{command_spec "code_type"} "define code syntax for type constructor"
|
haftmann@53274
|
750 |
(process_multi_syntax Parse.xname Code_Printer.parse_tyco_syntax
|
haftmann@53274
|
751 |
add_tyco_syntax_cmd);
|
haftmann@53274
|
752 |
|
wenzelm@24867
|
753 |
val _ =
|
wenzelm@47836
|
754 |
Outer_Syntax.command @{command_spec "code_class"} "define code syntax for class"
|
haftmann@53205
|
755 |
(process_multi_syntax Parse.xname Parse.string
|
wenzelm@47836
|
756 |
add_class_syntax_cmd);
|
haftmann@24219
|
757 |
|
wenzelm@24867
|
758 |
val _ =
|
wenzelm@47836
|
759 |
Outer_Syntax.command @{command_spec "code_instance"} "define code syntax for instance"
|
haftmann@53205
|
760 |
(process_multi_syntax (Parse.xname --| @{keyword "::"} -- Parse.xname) (Parse.minus >> K ())
|
wenzelm@47836
|
761 |
add_instance_syntax_cmd);
|
haftmann@24219
|
762 |
|
wenzelm@24867
|
763 |
val _ =
|
wenzelm@47836
|
764 |
Outer_Syntax.command @{command_spec "code_reserved"}
|
wenzelm@47836
|
765 |
"declare words as reserved for target language"
|
wenzelm@47836
|
766 |
(Parse.name -- Scan.repeat1 Parse.name
|
wenzelm@47836
|
767 |
>> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds));
|
haftmann@24219
|
768 |
|
wenzelm@24867
|
769 |
val _ =
|
wenzelm@47836
|
770 |
Outer_Syntax.command @{command_spec "code_include"}
|
wenzelm@47836
|
771 |
"declare piece of code to be included in generated code"
|
wenzelm@47836
|
772 |
(Parse.name -- Parse.name -- (Parse.text :|--
|
wenzelm@47836
|
773 |
(fn "-" => Scan.succeed NONE
|
wenzelm@47836
|
774 |
| s => Scan.optional (@{keyword "attach"} |-- Scan.repeat1 Parse.term) [] >> pair s >> SOME))
|
wenzelm@47836
|
775 |
>> (fn ((target, name), content_consts) =>
|
wenzelm@47836
|
776 |
(Toplevel.theory o add_include_cmd target) (name, content_consts)));
|
haftmann@24992
|
777 |
|
haftmann@24992
|
778 |
val _ =
|
wenzelm@47836
|
779 |
Outer_Syntax.command @{command_spec "code_modulename"} "alias module to other name"
|
wenzelm@47836
|
780 |
(Parse.name -- Scan.repeat1 (Parse.name -- Parse.name)
|
wenzelm@47836
|
781 |
>> (fn (target, modlnames) => (Toplevel.theory o fold (add_module_alias target)) modlnames));
|
haftmann@24219
|
782 |
|
wenzelm@24867
|
783 |
val _ =
|
wenzelm@47836
|
784 |
Outer_Syntax.command @{command_spec "code_abort"}
|
wenzelm@47836
|
785 |
"permit constant to be implemented as program abort"
|
wenzelm@47836
|
786 |
(Scan.repeat1 Parse.term_group >> (Toplevel.theory o fold allow_abort_cmd));
|
haftmann@24219
|
787 |
|
haftmann@27103
|
788 |
val _ =
|
wenzelm@47836
|
789 |
Outer_Syntax.command @{command_spec "export_code"} "generate executable code for constants"
|
wenzelm@47836
|
790 |
(Parse.!!! code_exprP >> (fn f => Toplevel.keep (f o Toplevel.theory_of)));
|
haftmann@30492
|
791 |
|
haftmann@24219
|
792 |
end; (*local*)
|
haftmann@24219
|
793 |
|
haftmann@39870
|
794 |
|
haftmann@39870
|
795 |
(** external entrance point -- for codegen tool **)
|
haftmann@39870
|
796 |
|
haftmann@39983
|
797 |
fun codegen_tool thyname cmd_expr =
|
haftmann@39870
|
798 |
let
|
haftmann@39870
|
799 |
val thy = Thy_Info.get_theory thyname;
|
haftmann@39870
|
800 |
val parse = Scan.read Token.stopper (Parse.!!! code_exprP) o
|
haftmann@39870
|
801 |
(filter Token.is_proper o Outer_Syntax.scan Position.none);
|
haftmann@39870
|
802 |
in case parse cmd_expr
|
haftmann@39870
|
803 |
of SOME f => (writeln "Now generating code..."; f thy)
|
haftmann@39870
|
804 |
| NONE => error ("Bad directive " ^ quote cmd_expr)
|
haftmann@39870
|
805 |
end;
|
haftmann@39870
|
806 |
|
wenzelm@44450
|
807 |
|
wenzelm@44450
|
808 |
(** theory setup **)
|
wenzelm@44450
|
809 |
|
wenzelm@44450
|
810 |
val setup = antiq_setup;
|
wenzelm@44450
|
811 |
|
haftmann@24219
|
812 |
end; (*struct*)
|